47Averages and the central limit theorem

easypython
30:00

47 Averages and the central limit theorem

A Netflix data science candidate reports a statistics question on the CLT and on the mean and variance of a uniform distribution (1 report). This is practice in that shape, not a leaked question.

Set a 30-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/47_clt_kit -k level1. Earlier levels must keep passing.

Level 1. Means and variances. Each function here returns a tuple (mean, variance), in that order. uniform_moments(a, b) returns them for a continuous uniform from a to b. die_moments(n) returns them for a fair die with faces 1 to n. moments(values, probs) returns them for any discrete distribution, where probs[i] is the chance of values[i]. Raise ValueError if the probabilities don't add up to 1, within 1e-9 on either side: a sum of 0.9999 is as wrong as 1.1. Compare with a tolerance, not ==, because six copies of 1/6 add to 0.9999999999999999.

Level 2. Averages. standard_error(sd, n) is the standard deviation of the average of n independent draws with standard deviation sd. n_for_standard_error(sd, target) is the smallest whole n whose standard error is at most target. prob_mean_above(mu, sd, n, threshold) is the chance that the average of n draws lands above threshold, using the central limit theorem's normal curve. statistics.NormalDist has a cdf.

Level 3. When the CLT is slow. skewness(xs) is the average of the cubed distances from the mean, each in standard deviations, with the standard deviation and the average both dividing by n. users_for_normal_mean(skew) is Kohavi and colleagues' rule of thumb for how many users make an average close to normal: 355 times the skewness squared, rounded up. covers(xs, mu, z=1.96) returns True if the interval mean ± z × s / √n contains mu, where s is the standard deviation dividing by n - 1. The interval includes its two endpoints.

Things to get right: the square in the uniform's variance, the square root in the standard error, rounding up, and which standard deviation each function uses. Follow-ups from the lesson: why skewed metrics need far more users, and the standard error against the standard deviation.

solution.py
Run the tests to see which pass.