Why This Matters
A survey says the average American sleeps 7.2 hours per night. But how precise is that number? Is it 7.2 plus or minus 0.1, or plus or minus 2? Without a confidence interval, a point estimate like 7.2 is almost meaningless because it hides how uncertain we are. Confidence intervals put bounds on our uncertainty: "We are 95% confident the true average is between 7.0 and 7.4 hours."
The margin of error determines the width of the interval, and the confidence level (typically 90%, 95%, or 99%) controls how sure we are that the true value falls inside. A higher confidence level means a wider interval — certainty comes at the cost of precision. This tradeoff is central to A/B testing, clinical trials, polling, and any data-driven decision. Understanding confidence intervals is what separates guessing from science.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
A confidence interval wraps a point estimate in a range of uncertainty. The width depends on the standard error and the chosen confidence level.
Code Example
// Confidence interval for the mean
// CI = x-bar +/- z* * (sigma / sqrt(n))
function confidenceInterval(data, confidenceLevel) {
const n = data.length;
const mean = data.reduce((a, b) => a + b, 0) / n;
// Sample standard deviation
const variance = data.reduce((a, b) => a + (b - mean) ** 2, 0) / (n - 1);
const sd = Math.sqrt(variance);
const se = sd / Math.sqrt(n);
// Z-scores for common confidence levels
const zScores = { 0.90: 1.645, 0.95: 1.96, 0.99: 2.576 };
const z = zScores[confidenceLevel];
const marginOfError = z * se;
return {
mean: mean,
marginOfError: marginOfError,
lower: mean - marginOfError,
upper: mean + marginOfError
};
}
// Example: 50 measurements with known properties
const data = [];
for (let i = 0; i < 50; i++) {
// Simulate Normal(100, 15) using Box-Muller
const u1 = Math.random(), u2 = Math.random();
const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
data.push(100 + 15 * z);
}
const ci95 = confidenceInterval(data, 0.95);
console.log(`Mean: ${ci95.mean.toFixed(2)}`);
console.log(`95% CI: (${ci95.lower.toFixed(2)}, ${ci95.upper.toFixed(2)})`);
console.log(`Margin of error: ${ci95.marginOfError.toFixed(2)}`);
const ci99 = confidenceInterval(data, 0.99);
console.log(`99% CI: (${ci99.lower.toFixed(2)}, ${ci99.upper.toFixed(2)})`);
console.log("99% CI is wider than 95% CI");Interactive Experiment
Try these exercises:
- Compute a 95% CI for 10 data points vs. 100 data points from the same distribution. How much narrower is the larger sample?
- Change the confidence level from 90% to 95% to 99%. Watch the interval widen. When would you choose each level?
- Simulate 100 different 95% CIs from the same population. How many contain the true mean? It should be close to 95.
- What happens to the margin of error if you quadruple the sample size? Verify that it halves.
- Try computing a CI for a highly skewed distribution (like income). Does it still work? Why or why not?
Quick Quiz
Coding Challenge
Write a function `computeCI(data, zStar)` that takes an array of numbers and a z-score, and returns a string in the format 'lower,upper' where lower and upper are the bounds of the confidence interval, each rounded to 2 decimal places. Use the formula: CI = mean +/- zStar * (sampleSD / sqrt(n)), where sampleSD uses n-1 in the denominator (Bessel correction).
Real-World Usage
Confidence intervals are essential in every data-driven field:
- A/B testing: When testing a new feature, companies report the difference in metrics with a confidence interval. If the CI for revenue lift is ($0.50, $2.00), the feature is profitable. If it is (-$0.10, $2.00), we are not sure.
- Polling: Election polls report results like "52% +/- 3%." The +/- 3% is the margin of error, and the full CI is (49%, 55%). If 50% is inside the CI, the race is a toss-up.
- Clinical trials: Drug effectiveness is reported with confidence intervals. A drug that reduces blood pressure by 10 mmHg with 95% CI (8, 12) is meaningfully effective. A CI of (-2, 22) is too uncertain.
- Software performance: Load test results might report "p99 latency: 200ms, 95% CI (185ms, 215ms)." This tells engineers how reliable the estimate is.
- Machine learning: Cross-validation accuracy is a mean of k folds. Reporting the CI tells you whether 92% accuracy is meaningfully different from 90%.