95% Z-Interval for a Mean with Known Standard Deviation
Arrays & HashingAuto-graded · 18 test casesLast updated
Compute a 95% z-based confidence interval for a population mean from a sample mean, known population standard deviation, and sample size, using the standard error.
Interval for an Experiment Metric
You are analyzing a metric from one arm of an experiment, for example average revenue per user. You have the sample mean and you know the true population standard deviation of the metric. The sample is large enough that the Central Limit Theorem applies, so the sample mean is approximately normally distributed around the true mean.
Write a function that returns the 95% two-sided confidence interval for the population mean.
Definition
- The standard error of the mean is
population_std / sqrt(n). - Use the fixed critical value 1.96 for 95% coverage. Do not use a t-distribution or look up other values.
- The margin of error is
1.96 * population_std / sqrt(n). - The interval is
[sample_mean - margin, sample_mean + margin].
Input
sample_mean(float): the observed mean of the sample.population_std(float): the known population standard deviation.n(int): the number of observations in the sample.
Output
Return a list of two floats, [lower, upper], with the lower bound first. Do not round the values.
Constraints
n >= 1population_std >= 0sample_meanmay be negative, zero, or positive.- Use a z-interval with critical value exactly 1.96. The sample is assumed to be independent and large enough for the normal approximation to hold.
Examples
Example 1
Input: z_interval_known_sigma(3, 1, 400)
Output: [2.902, 3.098]
Example 2
Input: z_interval_known_sigma(0, 2, 100)
Output: [-0.392, 0.392]
Example 3
Input: z_interval_known_sigma(12.5, 4, 64)
Output: [11.52, 13.48]
Hints
Hint 1
The sample mean varies much less than individual observations do. Its spread is the standard error, population_std divided by the square root of n.
Hint 2
A 95% two-sided normal interval extends 1.96 standard errors on each side of the center.
Hint 3
Compute the margin once as 1.96 * population_std / sqrt(n), then subtract it from and add it to the sample mean.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
By the Central Limit Theorem, for large n the sample mean is approximately normal with mean equal to the true mean and standard deviation sigma / sqrt(n). This quantity is the standard error. In a normal distribution, about 95% of the probability lies within 1.96 standard deviations of the center. Because sigma is known, a z-interval is appropriate and no t-distribution is needed.
Steps:
- Compute the standard error:
population_std / sqrt(n). - Multiply by 1.96 to get the margin of error.
- Subtract the margin from the sample mean for the lower bound, and add it for the upper bound.
Why it is correct
The standardized mean (x_bar - mu) / (sigma / sqrt(n)) is approximately standard normal. The central 95% of that distribution lies between -1.96 and 1.96. Rearranging the inequality for mu gives x_bar ± 1.96 * sigma / sqrt(n). Over repeated sampling, about 95% of intervals built this way contain the true mean.
For example, with mean 3, sigma 1 and n = 400, the standard error is 0.05, the margin is 0.098, and the interval is [2.902, 3.098].
A common mistake is using sigma rather than sigma / sqrt(n). That describes the spread of individual observations, not of the mean.
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
import math
from typing import List
def z_interval_known_sigma(sample_mean: float, population_std: float, n: int) -> List[float]:
margin = 1.96 * population_std / math.sqrt(n)
return [sample_mean - margin, sample_mean + margin]