A counter on a bridge records how many cyclists cross in each minute. The council claims that cyclists arrive at an average rate of claimed per minute. Model the counts as independent Poisson counts with rate lam: a minute has c cyclists with probability lam**c * exp(-lam) / c!.
Write rate_evidence(counts, claimed) that returns a tuple of three floats:
- the rate
lamthat makes the observed counts most probable, - the log-likelihood (natural log of the probability of all the counts) at that rate,
- the log-likelihood at the claimed rate.
The second value is never smaller than the third; how much larger it is tells you how badly the claim fits. Use the full Poisson probability including the c! term, and treat 0 * log(0) as 0 (when every count is zero the best rate is 0 and every minute has probability 1).
The setup provides cyclist_counts(minutes, rate, seed), which simulates the counter for minutes minutes at the given true rate.
Examples
Input: counts = [2, 4, 3, 0, 5, 3, 2, 4, 1, 3], claimed = 3.5
Output: (2.7, -18.087374301004342, -19.080572022907056)
Explanation: the best rate is the mean count 27 / 10. At lam = 2.7 the log-likelihood
is 27 * log(2.7) - 10 * 2.7 - log(2! 4! 3! 0! 5! 3! 2! 4! 1! 3!) = -18.087;
the claimed 3.5 fits worse by about 0.99.
Input: counts = [0, 0, 0], claimed = 1.0
Output: (0.0, 0.0, -3.0)
Constraints
1 <= len(counts) <= 10**5, counts are whole numbers>= 0claimed > 0- floats are compared with a tolerance of
1e-6
Goals
- Write the log-likelihood of a Poisson model for a list of counts
- Find the maximum likelihood rate and the log-likelihood it reaches
- Compare a claimed rate with the best-fitting one on the log scale