Interactive simulator
Select the mode, adjust the parameters, and click Simulate.
True value (unknown in practice) of the population mean that the simulator uses to generate the samples and check whether each CI covers it.
Standard deviation of the population the samples are drawn from. With the z method it is used directly in each interval; with the t method it only generates the data, and each interval uses the standard deviation s of its own sample.
With known σ, all intervals have the same width. With unknown σ, each interval uses the standard deviation s of its sample and the Student's t critical value: widths change from sample to sample, but coverage is still nominal.
Number of observations in each simulated sample. The width of the CI is proportional to 1/√n; coverage does not change with n.
Nominal level each interval is built with. It is also the expected fraction of intervals that will cover the true μ in the long run.
How many repetitions of the experiment are simulated. With more intervals, the empirical coverage gets closer to the method's actual coverage: its simulation error is ≈ √(p(1−p)/N), about ±0.7 points with 1000 intervals at 95%.
True value (unknown in practice) of the population proportion that the simulator uses to generate the binomial samples.
Number of observations in each simulated sample. With small n or p near the extremes, the Wald method tends to give worse coverage than Wilson.
Nominal level each interval is built with. It is also the expected fraction of intervals that will cover the true p in the long run.
How many repetitions of the experiment are simulated. With more intervals, the empirical coverage gets closer to the method's actual coverage: its simulation error is ≈ √(p(1−p)/N), about ±0.7 points with 1000 intervals at 95%.
Wilson inverts Pearson's score test and usually gives coverage closer to the nominal level; Wald uses the direct normal approximation and degrades with small n or p near 0 or 1; Clopper-Pearson uses the exact binomial and guarantees coverage of at least the nominal level, with somewhat wider intervals.
Simulated intervals
What does a 95% confidence interval actually mean?
The correct interpretation of a 95% confidence interval is frequently misunderstood, even by researchers with statistical training. The correct statement is:
"If we repeated the experiment infinitely many times and built the confidence interval on each repetition, 95% of those intervals would contain the true parameter."
This is the statement the simulator makes visible: each row in the chart is one repetition of the experiment. The blue bars are the intervals that captured the parameter; the red ones did not. If the procedure is correct, roughly 5% of the intervals will be red.
What it does NOT mean: "there is a 95% probability that the parameter is in this particular interval." Once you've computed the interval from your data, the parameter either is inside it or it isn't. There's no probability in that classical sense: the parameter is a fixed (though unknown) value, and the computed interval is also fixed. The 95% probability refers to the construction procedure, not to the individual interval.
This distinction matters because it has practical consequences: you can't say "I'm 95% sure μ is between 47 and 53." What you can say is "the method I used to compute this interval produces intervals that cover the true μ 95% of the time across repetitions of the experiment."
The Bayesian interpretation — "the probability that the parameter lies in this range is 95%" — is valid for Bayesian credible intervals, which require specifying a prior distribution and are conceptually different.
How the simulator works, step by step
The simulator does what can never be done in practice: repeat the same study many times while knowing the true value. Each time you click Simulate:
- It fixes the truth. It takes the value you entered as the real one: the mean μ (and the standard deviation σ) or the proportion p.
- It draws a sample of size n from that population: n values from a normal N(μ, σ) in Mean mode, or n success/failure trials with probability p in Proportion mode.
- It builds the interval from that sample, using the chosen method, just as you would with real data: without looking at the true value.
- It checks whether it is right: the interval "covers" if the true value lies inside. It is drawn in blue if it covers and in red if it does not.
- It repeats steps 2 to 4 as many times as the number of intervals you asked for, and counts what percentage covered: that is the empirical coverage.
On the chart, each row is a different study. The central dot of each segment is that sample's estimate (its mean or proportion), and the dashed vertical line is the true value. Note that the true value does not move: what changes from sample to sample is the interval. At the 95% level, in the long run 5 out of every 100 intervals end up red, and nothing in the data of those samples warns that it was their turn.
The cards at the top summarise the simulation: the chosen nominal level, the empirical coverage, how many intervals covered and the mean width. With 100 intervals, it is normal for the empirical coverage to wobble a few points around the nominal level (91% or 98% do not indicate a problem); with more intervals, it gets closer.
Mean mode: known σ versus unknown σ
With both methods the samples are generated in the same way, from the true μ and σ you entered. The difference lies in which information each interval is allowed to use:
| Known σ (z) | Unknown σ (s and Student's t) | |
|---|---|---|
| Standard deviation used by the interval | The true σ, the same for every sample | The standard deviation s of each sample, different every time |
| Critical value | Normal z (1.96 at 95%) | Student's t with n − 1 degrees of freedom (2.776 with n = 5; 2.045 with n = 30) |
| Formula | \(\bar{x} \pm z_{\alpha/2}\,\dfrac{\sigma}{\sqrt{n}}\) | \(\bar{x} \pm t_{\alpha/2;\,n-1}\,\dfrac{s}{\sqrt{n}}\) |
| Width of the intervals | All the same | Changes from sample to sample |
| Actual coverage | Exactly nominal | Exactly nominal |
With known σ, the only thing that varies between samples is the centre of the interval, \(\bar{x}\): on the chart you will see segments of the same length shifted left and right. This is the textbook case, but in practice σ is almost never known.
With unknown σ, each sample also estimates the spread, sometimes underestimating it and sometimes overestimating it. That is why you will see short and long intervals mixed together. A sample that happens to be tightly clustered gives a narrow interval, which is more likely to miss. To compensate for that extra uncertainty, Student's t is used; its critical value is larger than the normal one, especially with small samples: the intervals are wider on average, and thanks to that the coverage is nominal again.
What would happen without t. If the sample standard deviation s were used with the normal critical value 1.96, a common mistake, the intervals would be too narrow: with n = 5 they would cover only 88% of the time instead of 95%; with n = 10, 92%; with n = 30, 94%. That is the whole point of Student's t.
Try it: choose n = 5 and simulate with each method. With known σ, all intervals have the same length; with unknown σ, their lengths differ a lot and the mean width is larger, but coverage still hovers around 95%. Go up to n = 100 and the two methods become almost indistinguishable.
With both methods, the simulator generates normal data. With real non-normal data, coverage is only approximate and improves as n grows, by the central limit theorem.
Proportion mode: how the Wald, Wilson and Clopper-Pearson intervals are built
Both intervals come from the same reasoning and only part ways at the last step. It is worth going through it slowly, because that is where the reason one works much better than the other lies.
Starting point: the sample proportion is nearly normal
If there are X successes in n independent trials and the probability of success is p, X follows a binomial distribution and the sample proportion \(\hat{p} = X/n\) has
By the central limit theorem, when n is large enough, the standardised proportion behaves like a standard normal:
Writing \(z = z_{\alpha/2}\) for the normal critical value (1.96 at 95%), the probability that Z lies between −z and z is approximately \(1-\alpha\):
The confidence interval is made up of the values of p that satisfy the inequality inside. The difficulty is that p appears on both sides: in \(|\hat{p} - p|\) and inside the square root. The two methods deal with that in different ways.
Wald: replace p with p̂ inside the root
Step 1. The root is the standard deviation of \(\hat{p}\), but it depends on the unknown p. Wald replaces it with its estimate, the standard error:
With large samples the change hardly matters, because \(\hat{p}\) gets close to p.
Step 2. The root is now a fixed number, computed from the data, and p only appears in the absolute value:
which is equivalent to
Step 3. Solving for p:
which is the Wald interval:
The interval is centred on \(\hat{p}\) and symmetric. Its weak point is step 1: with small samples or p close to 0 or 1, \(\hat{p}\) is a poor estimate of the spread. The extreme case is a sample with no successes: \(\hat{p} = 0\), the standard error is 0 and the interval collapses to the single point 0, as if there were absolute certainty. And if \(\hat{p}\) is small but not zero, the lower end can come out negative and has to be truncated to 0.
Wilson: solve for p without replacing anything
Wilson keeps the unknown p inside the root and looks for all the values of p that satisfy the inequality.
Step 1. Square both sides. Both sides are positive, so the inequality is equivalent to
Step 2. Expand.
Step 3. Collect powers of p. Moving everything to the left gives a quadratic inequality in p:
Since a is positive, the parabola \(a p^2 - b p + c\) opens upwards and is negative exactly between its two roots. Those two roots are the ends of the interval.
Step 4. Solve the quadratic equation.
The discriminant simplifies a lot, because the \(\hat{p}^2\) terms cancel:
Step 5. Substitute and divide by 2. This gives the Wilson interval:
What the Wilson formula tells you
The centre is not \(\hat{p}\). Multiplying top and bottom by n, the centre can be written in two revealing ways:
The first is equivalent to adding \(z^2/2 \approx 2\) fictitious successes and 2 fictitious failures to the sample. The second shows that the centre is a weighted average of \(\hat{p}\) and 0.5: with small samples 0.5 carries more weight, and as n grows the centre moves towards \(\hat{p}\).
The width is never zero. The \(z^2/4n^2\) term in the root does not depend on \(\hat{p}\), so even with 0 successes the interval has a sensible width.
It never leaves [0, 1], because its ends are values of p that satisfy an inequality with \(p(1-p) \ge 0\).
When n is large, it matches Wald. The terms \(z^2/n\), \(z^2/2n\) and \(z^2/4n^2\) become negligible and the formula reduces to \(\hat{p} \pm z\sqrt{\hat{p}(1-\hat{p})/n}\).
It is the test turned around. The starting inequality is the non-rejection rule of the one-proportion test, which uses the standard error computed with \(p_0\). That is why the Wilson interval contains exactly the values \(p_0\) that the test would not reject with your data; it is said to "invert" the test. The Wald interval, by contrast, does not correspond exactly to that test.
Clopper-Pearson: using the exact binomial
The third method approximates nothing. The number of successes \(X\) follows a binomial \(B(n, p)\) exactly, and Clopper and Pearson (1934) define the ends of the interval with its tails: \(p_L\) is the value of \(p\) for which getting \(x\) successes or more has probability \(\alpha/2\), and \(p_U\), the value for which getting \(x\) or fewer does.
How it is solved. The binomial tails coincide with the distribution function of a beta. If you generate \(n\) uniforms between 0 and 1, the number of them below \(p\) is a \(B(n, p)\), and there are at least \(x\) below \(p\) exactly when the \(x\)-th smallest, which follows a \(\mathrm{Beta}(x,\, n - x + 1)\), is below \(p\). Therefore:
and the ends are beta quantiles:
with \(p_L = 0\) if \(x = 0\) and \(p_U = 1\) if \(x = n\). This is what the simulator computes for each sample.
Why it never covers less than promised. For a fixed true \(p\), the interval leaves \(p\) out on the upper side only when an \(x\) comes out so small that \(P(X \le x \mid p) < \alpha/2\). Those values of \(x\) form a run \(0, \ldots, x^*\) whose total probability is \(P(X \le x^* \mid p) < \alpha/2\). The same happens on the lower side, so the probability of missing is at most \(\alpha\) and the coverage at least \(1 - \alpha\), for any \(p\) and any \(n\). Since \(X\) only takes whole values, the tails almost never equal exactly \(\alpha/2\) and the actual coverage ends up higher: the method is conservative.
The full step-by-step derivation is in the confidence interval calculator for a proportion.
A worked example
With 2 successes in 20 trials (\(\hat{p} = 0.10\)) at the 95% level:
| Method | Centre | Interval |
|---|---|---|
| Wald | 0.100 | from −0.031 to 0.231, truncated to [0, 0.231] |
| Wilson | 0.164 | [0.028, 0.301] |
| Clopper-Pearson | — | [0.012, 0.317] |
The arithmetic, step by step, with \(z = 1.96\) and \(z^2 = 3.8416\):
- Wilson, denominator: \(1 + 3.8416/20\) \(= 1.1921\).
- Wilson, centre: \((0.10 + 0.0960)/1.1921\) \(= 0.164\).
- Wilson, half-width: \(1.96\sqrt{0.0045 + 0.0024}\) \(/\,1.1921\) \(= 0.137\). Interval: \(0.164 \pm 0.137\).
- Wald: \(SE = \sqrt{0.10 \cdot 0.90/20}\) \(= 0.067\), and the interval is \(0.10 \pm 1.96 \cdot 0.067\) \(= 0.10 \pm 0.131\).
With 0 successes in 20, Wald gives the interval [0, 0], whereas Wilson gives [0, 0.161] and Clopper-Pearson [0, 0.168]. Clopper-Pearson is not built as a centre plus a margin, which is why it has no "centre" in the table.
What you will see in the simulator
The actual coverage of each method (the probability that a 95% interval contains the true value) can be computed exactly, and it is what the simulator reproduces:
| Case | Wald | Wilson | Clopper-Pearson |
|---|---|---|---|
| p = 0.05, n = 20 | 63.9% | 92.5% | 98.4% |
| p = 0.05, n = 100 | 87.7% | 96.6% | 98.3% |
| p = 0.10, n = 50 | 87.9% | 97.0% | 97.0% |
| p = 0.50, n = 30 | 95.7% | 95.7% | 95.7% |
With p = 0.05 and n = 20, more than a third of the "95%" Wald intervals do not contain the true value, mainly because many samples come out with 0 successes. With p = 0.5 the three agree. None hits 95% exactly in every case, because the number of successes can only take whole values: Wilson stays close, sometimes above and sometimes below, and Clopper-Pearson never drops below 95%, at the cost of almost always overshooting. The confidence interval calculator for a proportion computes all three from your data, so you can compare them.
Nominal coverage vs actual coverage
The nominal level is the confidence value we specify when constructing the CI (90%, 95%, 99%). The actual (or empirical) coverage is the fraction of intervals that actually cover the true parameter across a long series of repetitions.
For the CI of the mean with a normal population, as in this simulator (both with known σ and with Student's t), the actual coverage matches the nominal level exactly for any sample size, because the sample mean is exactly normal. If the population is not normal, the match is only approximate and improves with n by the CLT.
For proportions, the situation is more complicated. The parameter p is continuous, but the binomial variable X ~ Bin(n, p) is discrete, and this discretization causes the actual coverage to fluctuate around the nominal level:
- Wald interval: uses the normal approximation directly, \(\hat{p} \pm z_{\alpha/2}\sqrt{\hat{p}(1-\hat{p})/n}\). It works well for large n and p away from 0 and 1, but its coverage can be noticeably below the nominal level when n is small or p is near the extremes.
- Wilson interval: obtained by inverting Pearson's score test. It tends to have actual coverage closer to the nominal level, especially at the extremes, though it can be slightly conservative (coverage above nominal) in some cases.
- Clopper-Pearson interval: obtained by inverting the exact binomial test. Its actual coverage never falls below the nominal level, but almost always exceeds it: it is conservative.
You can observe this phenomenon directly with the simulator: select Proportion mode, set p = 0.05 and n = 20, and compare the empirical coverage of Wald, Wilson and Clopper-Pearson.
How does sample size affect it?
The sample size n has a direct and clear effect on the width of the confidence interval. For the CI of the mean with known σ:
The width is proportional to \(1/\sqrt{n}\): if we quadruple n, the width is cut in half. To reduce the width to a third, we need to multiply n by 9.
However, increasing n does not change the coverage: a 95% interval is still 95% regardless of whether n = 10 or n = 1000. What changes is how narrow that interval is, i.e. the precision of the estimate. Narrower intervals are more useful in practice, even though both have the same nominal coverage guarantee.
You can verify this in the simulator: keep the confidence level fixed at 95% and the number of intervals at 100, and vary n between 5 and 100. You'll observe that:
- The fraction of blue intervals stays at roughly 95% for all values of n.
- The intervals become progressively narrower as n increases.
- With small n, there's more variability in the position of the CI's endpoints.
Frequently asked questions
- If the CI doesn't contain the true value, was it calculated wrong? No. A 95% CI is designed to fail to cover the parameter roughly 5% of the time. Seeing red intervals in the simulation is exactly what's expected; it doesn't indicate any calculation error. If 95% of the intervals are blue, the procedure is working correctly.
- Why does the Wilson method give better coverage than Wald? The Wald interval applies the normal approximation directly to \(\hat{p}\), which deteriorates when \(\hat{p}\) is close to 0 or 1 or when n is small (the distribution of \(\hat{p}\) isn't symmetric enough). The Wilson interval gets its bounds by inverting Pearson's score test, which accounts for the curvature of the binomial distribution. This produces empirical coverage much closer to the nominal level across the whole range of p.
- Can I interpret the CI in Bayesian terms? Only if you use a Bayesian credible interval, which has a completely different construction. The frequentist CI doesn't assign probabilities to the parameter (which is treated as fixed but unknown). The Bayesian credible interval does express P(θ ∈ CI | data) = 1 − α, but it requires specifying a prior distribution over θ. Both tools are valid in their own context, but they shouldn't be confused.
- How many intervals do I need to simulate to see the coverage converge? With 20 intervals there's a lot of variability: the empirical coverage can swing between 80% and 100% even if the procedure is correct. With 100 intervals, the standard error of the proportion is ≈ √(0.95 × 0.05 / 100) ≈ 2.2%, so the empirical coverage will typically fall between 91% and 99%. With 200 intervals, the variability drops to ≈ 1.5%.
- Does the simulator use an exact normal distribution for the mean? Yes: in Mean mode each observation is generated from an N(μ, σ) using the Box-Muller method. The sample mean has an exactly N(μ, σ/√n) distribution for any n, so the actual coverage is exactly nominal even for small n; the empirical coverage only departs from it through simulation error.
References and further reading
- Confidence interval — Wikipedia. Definition, interpretation and general construction.
- Binomial proportion confidence interval — Wikipedia. Comparison of the Wald, Wilson, Agresti-Coull and Clopper-Pearson intervals, with their formulas.
- Neyman, J. (1937). Outline of a theory of statistical estimation based on the classical theory of probability. Philosophical Transactions of the Royal Society A, 236, 333–380. The paper that defines confidence intervals and their interpretation as long-run coverage.
- Student (W. S. Gosset) (1908). The probable error of a mean. Biometrika, 6(1), 1–25. The origin of the t distribution, for the unknown-σ case.
- Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22(158), 209–212. The original paper on the Wilson interval.
- Clopper, C. J. and Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika, 26(4), 404–413. The original paper on the exact interval.
- Agresti, A. and Coull, B. A. (1998). Approximate is better than "exact" for interval estimation of binomial proportions. The American Statistician, 52(2), 119–126.
- Brown, L. D., Cai, T. T. and DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical Science, 16(2), 101–133. The reference study on the poor coverage of the Wald interval; it recommends Wilson.