Home / Blog / Math & Scientific

Math & Scientific

Confidence Interval Formula for a Population Proportion

Compute a sample proportion, compare Wald and Wilson intervals, and handle zero successes without claiming zero population uncertainty.

Confidence Interval Formula for a Population Proportion

Define successes and the sampling model

For n independent binary trials with a common success probability p, let x be the number of successes. The sample proportion is p̂=x/n, with integer counts satisfying 0≤x≤n and n>0. Define success before collecting the observations so that the denominator consistently represents eligible trials. A confidence interval estimates the underlying success probability under that model. Clustering, survey weights, nonresponse and changing trial probabilities can require a different uncertainty calculation even when the observed fraction itself is easy to compute.

Calculate the observed proportion

Suppose 84 of 200 trials are successes. Then p̂=84/200=0.42, or 42%. Enter favorable outcomes 84 and total outcomes 200 in the local probability form to check this ratio. When used with observed counts, the fraction is an empirical estimate; it does not establish that a future trial has a known probability exactly equal to 0.42. The form's complement is the observed failure fraction 0.58, not a confidence limit.

Recognize the simple normal approximation

The familiar Wald interval is p̂±z√[p̂(1−p̂)/n]. At approximately 95% confidence, using z=1.96 gives standard error √(0.42×0.58/200)≈0.0349 and endpoints about 0.3516 and 0.4884. This approximation can perform poorly with small samples or proportions near zero or one, and it can produce impossible endpoints outside [0,1]. A formula being symmetric and convenient does not make it suitable for every binomial count.

Use the Wilson score formula

Let d=1+z²/n. The Wilson centre is c=(p̂+z²/(2n))/d, and its half-width is h=z√[p̂(1−p̂)/n+z²/(4n²)]/d. The endpoints are c−h and c+h. For x=84, n=200 and z=1.96, they are approximately 0.353735 and 0.489281, or 35.37% to 48.93%. The centre is slightly different from the observed proportion because this interval comes from a different construction, not from a transcription error in the input fraction.

Check zero observed successes

For x=0 and n=20, the observed fraction is zero. The Wald formula collapses to [0,0], incorrectly suggesting no remaining uncertainty about an unobserved success probability. The Wilson interval with z=1.96 instead runs from zero to about 0.161130. Checking 0/20 in the local probability form should return zero for the observed ratio; the positive upper confidence bound must be calculated separately. No successes in a finite sample does not prove that the population probability is exactly zero.

Reproduce the two Wilson calculations

The code below checks integer counts and implements the stated formula using the rounded critical value 1.96. It constrains endpoints to [0,1] only to suppress tiny floating-point excursions such as a negative value near −10⁻¹⁷ at an exact zero boundary. It does not repair an invalid sampling model. Other methods, including exact binomial intervals, use different constructions and can produce different endpoints. State the selected method and critical value when comparing results across software.

from math import sqrt

def wilson95(x, n):
    if (isinstance(x, bool) or isinstance(n, bool)
            or not isinstance(x, int) or not isinstance(n, int)
            or n <= 0 or not 0 <= x <= n):
        raise ValueError("use integer counts with 0 <= x <= n and n > 0")
    z, p = 1.96, x/n
    denominator = 1 + z*z/n
    center = (p + z*z/(2*n)) / denominator
    half = z * sqrt(p*(1-p)/n + z*z/(4*n*n)) / denominator
    return max(0.0, center-half), min(1.0, center+half)

print(wilson95(84, 200))  # about (0.353735, 0.489281)
print(wilson95(0, 20))    # about (0, 0.161130)

Keep percentages and probability units separate

Use p̂=0.42 inside the formula, not the percentage number 42. The interval endpoints are probabilities until multiplied by 100 for percentage reporting. A margin of five percentage points means 0.05 on the probability scale; it is different from a five-percent relative change in an estimate. Confidence level also has its own role: raising it increases the critical value and generally widens the interval. It does not change the observed success fraction x/n.

Report uncertainty with the design

For the first example, report 84 successes among 200 trials, an observed proportion of 42%, and an approximately 95% Wilson interval of 35.37% to 48.93%. The repeated-sampling coverage statement concerns the procedure under its assumptions, not a probability assigned to an already fixed population parameter without a Bayesian model. Retain the success definition and data-collection context. If observations are not independent trials with a common probability, choose an interval method that reflects the actual design.

Calculator for the supported step

The linked probability form checks the observed fraction x/n only. It does not calculate a confidence interval. The Wilson interval below is computed from its explicit formula and Python example; the site’s mean-interval form is not a substitute for a binomial proportion method.

Open Probability Calculator

Sources and method

NIST: confidence intervals for binomial proportions

NIST: binomial confidence-limit methods