Problem 518055 · easy · Level 05 Advanced Algorithms & Graphs

How Long Do the Batteries Last?

confidence interval · standard error · sample standard deviation · sample size

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 of hours,
  • se: its standard error s / sqrt(n), where s is the sample standard deviation (dividing by n - 1) and n = len(hours),
  • low, high: the interval mean - crit * se to mean + crit * se (crit is the multiplier, for example 1.96, or a t value for a small sample),
  • needed: the smallest whole number of measurements m for which the half-width crit * s / sqrt(m) would be at most target, assuming s and crit stay 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 equal
  • crit > 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
Starting Python…