From raw data, build confidence intervals at three levels and see them widen.
What to build
A program that prints a mean, its standard error and three confidence intervals.
Requirements
- Print
mean M(2 decimal places),standard error S(3 decimal places), then90%: L to U,95%: L to Uand99%: L to U(2 decimal places).
Input
One line: numbers separated by spaces.
Output
Five lines.
Constraints
2 to 1000 numbers.
Example
Input:
162 170 168 175 159 181 166 172 169 177 164 171 174 160 168
Output:
mean 169.07
standard error 1.617
90%: 166.41 to 171.73
95%: 165.90 to 172.24
99%: 164.90 to 173.23
Why it works
Each level uses the z value that leaves that share of a normal curve in the middle, so more confidence multiplies the same standard error by a larger number.
Examples
Example 1 · Input
162 170 168 175 159 181 166 172 169 177 164 171 174 160 168
Expected output
mean 169.07 standard error 1.617 90%: 166.41 to 171.73 95%: 165.90 to 172.24 99%: 164.90 to 173.23
Example 2 · Input
12 15 11 18 14 16 13 17
Expected output
mean 14.50 standard error 0.866 90%: 13.08 to 15.92 95%: 12.80 to 16.20 99%: 12.27 to 16.73
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use statistics.stdev for the sample standard deviation.
Hint 2
The z values are 1.645 for 90%, 1.96 for 95% and 2.576 for 99%.
Solution
Show one correct solution (try it yourself first)
import statistics as st
data = list(map(float, input().split()))
m = st.mean(data)
se = st.stdev(data) / len(data) ** 0.5
print(f"mean {m:.2f}")
print(f"standard error {se:.3f}")
for level, z in (("90%", 1.645), ("95%", 1.96), ("99%", 2.576)):
print(f"{level}: {m - z * se:.2f} to {m + z * se:.2f}")