A student's simulation prints "the probability is 0.33334". With 100,000 trials, only the first three decimals of that number mean anything. Write a helper that reports a simulation's result honestly.
results is the list of outcomes of the independent trials of a simulation: numbers, or True/False for "did the event happen" (which count as 1 and 0). Write honest_report(results) that returns a tuple (estimate, se, text):
estimateis the mean of the results,seis its standard error,s / sqrt(n), wheresis the sample standard deviation of the results (dividing byn - 1),textis"<estimate> +/- <se>", where both numbers are formatted with the same number of decimalsd: the position of the first significant digit ofse. Forse = 0.0773that isd = 2, forse = 0.0015it isd = 3, and forse >= 1it isd = 0(whole numbers). Format with exactlyddecimals, keeping trailing zeros, as an f-string such asf"{x:.2f}"does.
Examples
Input: results = [1, 0, 0, 1, 1, 0, 1, 0]
Output: (0.5, 0.1889822365046136, "0.5 +/- 0.2")
Explanation: the sample standard deviation is 0.5345, and 0.5345 / sqrt(8) = 0.189,
whose first significant digit is in the first decimal place.
Input: results = [1200, 800, 3000, 150, 99]
Output: (1049.8, 529.2400589524568, "1050 +/- 529")
Constraints
2 <= len(results) <= 10**5, and not all results are equal (sose > 0)- floats are compared with a tolerance of
1e-6; the text must match exactly
Goals
- Treat a simulation's result as a sample mean with a standard error
- Compute the standard error s / sqrt(n) from the simulated outcomes
- Report an estimate with no more decimals than its standard error supports