A clinic estimates the rate p of a rare side effect from n patients. If k of them have the side effect, a rule turns k into an estimate: the plain proportion k / n, a smoothed version such as (k + 1) / (n + 2), or anything else. How good is a rule? Before any data is collected, k is random (binomial with n trials and success probability p), and so is the estimate.
Write rule_report(n, p, rule), where rule is a function that takes k and returns an estimate. Return a tuple of three floats, computed exactly from the binomial probabilities of k = 0, 1, ..., n (no simulation):
- the bias: the expected estimate minus
p, - the standard deviation of the estimate around its own expected value,
- the root mean squared error: the square root of the expected value of
(estimate - p) ** 2.
Examples
Input: n = 4, p = 0.5, rule = lambda k: k / 4
Output: (0.0, 0.25, 0.25)
Input: n = 4, p = 0.5, rule = lambda k: (k + 1) / 6
Output: (0.0, 0.16666666666666666, 0.16666666666666666)
Explanation: at p = 0.5 both rules are unbiased, and the smoothed one scatters less.
Input: n = 10, p = 0.05, rule = lambda k: (k + 1) / 12
Output: (0.075, 0.05743353646704256, 0.09446486707295527)
Explanation: near 0 the smoothed rule is pulled up by 0.075 and its error is larger
than the plain proportion's 0.0689.
Constraints
1 <= n <= 2000,0 < p < 1rule(k)returns a float for everykfrom 0 ton- floats are compared with a tolerance of
1e-6
Goals
- Compute an estimator's bias, spread and error exactly by summing over every possible sample
- Evaluate binomial probabilities for large n without overflow or underflow
- See that smoothing a proportion helps for some true rates and hurts for others