KatInterview

95% Z-Interval for a Mean with Known Standard Deviation

Arrays & HashingAuto-graded · 18 test cases
EasyAuto-gradedArrays & Hashingconfidence-intervalcentral-limit-theoremstandard-error

Last 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 >= 1
  • population_std >= 0
  • sample_mean may 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.