A small lab tests how many hours a new rechargeable battery runs a sensor. Each test takes a day, so the lab has only a handful of measurements, and it wants to report the average running time together with an honest range.
Write battery_interval(hours, crit, target) that returns a tuple of five values (mean, se, low, high, needed):
mean: the sample mean ofhours,se: its standard errors / sqrt(n), wheresis the sample standard deviation (dividing byn - 1) andn = len(hours),low,high: the intervalmean - crit * setomean + crit * se(critis the multiplier, for example 1.96, or a t value for a small sample),needed: the smallest whole number of measurementsmfor which the half-widthcrit * s / sqrt(m)would be at mosttarget, assumingsandcritstay as they are.
The setup provides battery_hours(n, seed), which returns n simulated measurements.
Examples
Input: hours = [41, 38, 45, 40, 36], crit = 2.776, target = 1.0
Output: (40.0, 1.51657508881031, 35.78998755346258, 44.21001244653742, 89)
Explanation: s = 3.391, so se = 3.391 / sqrt(5) = 1.517 and the half-width is
2.776 * 1.517 = 4.21. A half-width of 1 hour needs (2.776 * 3.391 / 1)^2 = 88.6,
so 89 measurements.
Input: hours = [12.5, 13.1, 11.8, 12.9, 12.2, 13.4, 12.0, 12.7], crit = 1.96, target = 0.5
Output: (12.575, 0.19616865921227794, 12.190509427943935, 12.959490572056064, 5)
Constraints
2 <= len(hours) <= 10**5, and the values are not all equalcrit > 0,target > 0- floats are compared with a tolerance of
1e-6
Goals
- Compute the standard error of a sample mean from the data
- Build a confidence interval as the mean plus or minus a multiplier times the standard error
- Work out how many measurements a narrower interval would need