Inference for Proportions and Means

The four standard comparisons, one proportion, two proportions, one mean and two means, carried out in scipy and statsmodels on samples from NHANES adults, with the defaults that decide the answer. A proportion test in statsmodels that is a Wald test unless prop_var is set, with a true error rate of 5.4% against 3.6% for the exact test; an interval function that defaults to the Wald interval; Yates's correction on by default in chi2_contingency; ttest_ind pooling variances by default, which rejected a true null 12% of the time with unequal groups; paired data analyzed as independent; and a difference of 1.8 mm Hg that is highly significant and practically small.

On samples from the 5,182 NHANES adults of 2017 to 2018, the defaults of scipy and statsmodels change the answers of the four standard tests. statsmodels' one-sample proportions_ztest is a Wald test unless prop_var is set: for 53 of 80 against 0.545 it gives $p = 0.0262$, against 0.0348 for the score test and 0.0427 exact, and at $n = 30$, $p_0 = 0.2$ its true Type I error rate is 0.0537 against 0.0361. proportion_confint defaults to the Wald interval, and chi2_contingency applies Yates's correction (0.0328 becomes 0.0464). Both libraries' ttest_ind pool the variances: with a small, more variable group (12 against 36) the pooled test rejected a true null 12.0% of the time and Welch's 5.1% (30 seeds). Ignoring pairing turned $p = 0.0378$ into 0.6650.

Most questions that end in a test are one of four kinds: is a proportion equal to a stated value, do two groups have the same proportion, is a mean equal to a stated value, do two groups have the same mean. Each has a standard test and interval, each is available in scipy and statsmodels, and the libraries do not agree on the defaults. The same 53 adults with hypertension out of 80 give a two-sided p-value of 0.0262, 0.0348 or 0.0427 depending on which function is called with its default arguments, and the same two samples of blood pressures give 0.0001 or 0.0034 depending on one keyword. None of these is a bug. Each function computes a well-defined procedure, and choosing among them is part of the analysis.

The procedures themselves are derived, with their assumptions and exact error rates, in the Applied Statistics course pages: Inference on Proportions (the normal approximation, intervals for a proportion, tests and sample size, two proportions), Confidence Intervals and Hypothesis Testing for a mean, and Comparison of Two Means (the pooled and Welch $t$-tests, paired samples, sample size). The general ideas, standard errors, p-values and multiple testing, are in Inferential Statistics in Python. This article maps each of the four questions to the functions that answer it and shows, on samples from the 5,182 adults whose blood pressure the National Health and Nutrition Examination Survey (NHANES) measured in 2017 and 2018, where their answers part ways. Hypertension follows the 2017 definition of the American College of Cardiology and American Heart Association, as in Data Collection and Sampling.

One proportion

NCHS estimates that 54.5% of US adults aged 40 to 59 had hypertension in 2017 to 2018 (Ostchega et al., 2020). The question here is whether the share is the same among the NHANES participants of that age, and the answer below is that it is not. A random sample of 80 of them (seed 3) contains 53 with hypertension, $\hat{p} = 0.6625$. The null hypothesis is $H_0: p = 0.545$.

Three tests are in common use. The score test standardizes by the standard error under the null hypothesis,

$$z = \frac{\hat{p} - p_0}{\sqrt{p_0(1 - p_0)/n}} = \frac{0.6625 - 0.545}{\sqrt{0.545 \times 0.455 / 80}} = 2.1105,$$

the Wald test by the standard error estimated from the sample, $\sqrt{\hat{p}(1-\hat{p})/n}$, and the exact binomial test uses the binomial distribution itself instead of a normal approximation. That approximation, and when its skewness makes it untrustworthy for small $np$, is derived in The Normal Approximation to the Binomial. Where the score and Wald tests come from, as two readings of the log-likelihood, is the subject of Likelihood Ratio, Wald and Score Tests. In Python:

Call Test Statistic p (two-sided)
proportions_ztest(53, 80, value=0.545) (statsmodels default) Wald 2.2226 0.0262
proportions_ztest(53, 80, value=0.545, prop_var=0.545) score 2.1105 0.0348
chisquare([53, 27], [80 * 0.545, 80 * 0.455]) (scipy) score, squared 4.4541 0.0348
binomtest(53, 80, p=0.545) (scipy) exact 0.0427

statsmodels' one-sample proportions_ztest computes the variance from the sample proportion unless prop_var supplies another value; with prop_var=p0 it becomes the score test, which is also the chi-squared goodness-of-fit test with one degree of freedom (The Chi-Square Goodness-of-Fit Test proves the identity). All three reject at 0.05 here, and the null hypothesis is in fact false for this population: the share among the 5,182 examined adults aged 40 to 59 is 0.5839. NHANES oversamples groups with higher prevalence, so its participants are not a miniature of the country, and the national 54.5% is a weighted figure (Data Collection and Sampling explains the weights, and Survey Weights and the Horvitz–Thompson Estimator the estimator behind them). The test has found the difference between the sample design and the population, which is a real finding about these data even if not about health.

Which test holds its level

The differences between the tests matter most in small samples. Because the count is discrete, the exact probability of rejecting a true null hypothesis can be computed by adding the binomial probabilities of the counts each test rejects. For $n = 30$ and $p_0 = 0.2$ at the 5% level:

Test Counts rejected (out of 30) True Type I error rate
Wald (proportions_ztest default) 0, 1, 2 and 12 or more 0.0537
Score (prop_var=p0) 0, 1 and 11 or more 0.0361
Exact (binomtest) 0, 1 and 11 or more 0.0361

The Wald test rejects too often, partly because a sample with 1 or 2 events has a tiny estimated standard error and an enormous $z$: for one event in 30, statsmodels' default reports $z = -5.085$ and a p-value of $3.7 \times 10^{-7}$. The score and exact tests reject the same counts here and keep the rate below 5%, at the price of rejecting less often than the nominal level, the conservatism of exact tests that Tests and Sample Size for a Proportion examines. The general advice from that page and from Confidence Intervals for a Proportion carries over to code: avoid the Wald forms for small samples or proportions near 0 or 1.

Intervals

For the interval, statsmodels' proportion_confint defaults to method="normal", the Wald interval, while scipy's binomtest(...).proportion_ci() defaults to the exact (Clopper–Pearson) interval:

Seven 95% intervals for the share with hypertension from the same sample, 53 of 80, with the two library defaults in orange. The Wald interval is centered on the sample proportion, the only one symmetric about it, while the others are pulled toward one half; Clopper-Pearson is the widest. All exclude the null value 0.545 and contain the population share 0.584.
Seven 95% intervals for the share with hypertension from the same sample, 53 of 80, with the two library defaults in orange. The Wald interval is centered on the sample proportion, the only one symmetric about it, while the others are pulled toward one half; Clopper-Pearson is the widest. All exclude the null value 0.545 and contain the population share 0.584.
Method statsmodels method= scipy method= 95% interval Width
Wald "normal" (default) 0.5589 to 0.7661 0.2072
Wilson score "wilson" "wilson" 0.5536 to 0.7565 0.2030
Agresti–Coull "agresti_coull" 0.5533 to 0.7568 0.2035
Jeffreys "jeffreys" 0.5546 to 0.7589 0.2043
Clopper–Pearson "beta" "exact" (default) 0.5481 to 0.7645 0.2164
Wilson with continuity correction "wilsoncc" 0.5472 to 0.7621

With 80 observations and $\hat{p}$ in the middle of the range, the intervals differ by less than a percentage point at either end. statsmodels' "jeffreys" is the equal-tailed interval of the posterior under Jeffreys' prior, and its documentation notes that $\alpha$ is not adjusted at the boundaries. So for $x = 0$ or $x = n$ it keeps a limit short of 0 or 1, where the Jeffreys interval as usually defined sets it to 0 or 1 (Credible Intervals and How They Differ from Confidence Intervals). The coverage of each method across sample sizes and true proportions, computed exactly, is the subject of Confidence Intervals for a Proportion, which recommends Wilson's interval for general use. Note also that statsmodels takes alpha=0.05 for a 95% interval, while scipy's proportion_ci takes confidence_level=0.95.

Two proportions

Among 100 men and 100 women sampled from the adults (seed 4), 63 men and 48 women have hypertension, a difference of 0.15; in the whole population the shares are 0.5856 and 0.5108. The functions:

Call Method Statistic p
proportions_ztest([63, 48], [100, 100]) pooled $z$ (score) 2.1343 0.0328
test_proportions_2indep(63, 100, 48, 100) default: Agresti–Caffo 2.1362 0.0327
test_proportions_2indep(..., method="score") score 2.1289 0.0333
chi2_contingency(table, correction=False) Pearson chi-squared 4.5551 ($= z^2$) 0.0328
chi2_contingency(table) (scipy default) with Yates's correction 3.9680 0.0464
fisher_exact(table) exact conditional odds ratio 1.8446 0.0461

Unlike the one-sample case, statsmodels' two-sample proportions_ztest uses the pooled proportion under the null hypothesis, so it equals the Pearson chi-squared test on the $2 \times 2$ table, whose statistic is $z^2$; Comparing Two Proportions proves the identity. scipy's chi2_contingency applies Yates's continuity correction to $2 \times 2$ tables unless correction=False, which moves the p-value from 0.0328 to 0.0464, close to Fisher's exact test. For the interval, confint_proportions_2indep defaults to Newcombe's method, 0.0124 to 0.2799 for the difference, against 0.0138 to 0.2862 for the Wald interval (method="wald"). Both exclude zero, consistent with the tests.

One mean

For a mean the standard procedure is the one-sample $t$-test and interval (Hypothesis Tests for a Mean). A sample of 40 adults (seed 6) has mean systolic pressure 125.08 mm Hg and standard deviation 17.60. Against $H_0: \mu = 120$, scipy's ttest_1samp(x, 120) gives $t = 1.8267$ on 39 degrees of freedom and $p = 0.0754$, with 95% interval 119.455 to 130.712 from its confidence_interval() method; statsmodels' DescrStatsW(x).ttest_mean(120) and tconfint_mean() return the same. Here the libraries agree, and the remaining traps are elsewhere.

Missing values. With one NaN appended to the sample, ttest_1samp returns a p-value of NaN under its default nan_policy="propagate"; nan_policy="omit" drops the missing value and returns 0.0754 again. pandas' mean() would have skipped it silently. The scipy behavior is the safer one, since it forces a decision, but code that passes pandas columns to scipy needs dropna() or nan_policy="omit" made explicit.

Return values. scipy's tests return a result object that unpacks into the statistic and p-value (t, p = ttest_1samp(...)) and also carries df and confidence_interval(). statsmodels' ttest_mean and its two-sample ttest_ind return a plain tuple of three values: statistic, p-value and degrees of freedom. Code that unpacks two values from a statsmodels test fails.

Two means

Twelve adults aged 60 and over and 36 aged 18 to 39 (seed 8) have mean systolic pressures of 130.78 and 113.69 mm Hg, with standard deviations 15.77 and 10.84. In the population the groups' standard deviations are 20.93 and 12.89 mm Hg: the older group is both smaller and more variable.

Call Test $t$ df p 95% interval for the difference
stats.ttest_ind(a, b) (scipy default) pooled 4.2001 46 0.000121 8.896 to 25.271
stats.ttest_ind(a, b, equal_var=False) Welch 3.4887 14.63 0.003410 6.623 to 27.544
ttest_ind(a, b) (statsmodels default) pooled 4.2001 46 0.0001
ttest_ind(a, b, usevar="unequal") (statsmodels) Welch 3.4887 14.63 0.0034

Both libraries pool the variances by default, the test whose $t$ distribution on $n_1 + n_2 - 2$ degrees of freedom The Two-Sample t-Test proves under equal variances; Welch's test must be requested, with equal_var=False in scipy and usevar="unequal" in statsmodels (whose CompareMeans.tconfint_diff takes the same argument for the interval). R's t.test does the opposite and uses Welch's test unless told otherwise, as Welch's t-Test and Unequal Variances notes. Here both tests reject, but the pooled test is overconfident by a factor of almost 30 in its p-value, because it averages the two variances in proportion to the group sizes and so gives the variance of the smaller, more variable group of older adults too little weight.

How much that matters can be measured by simulation. With normal populations of equal means and the population standard deviations above, 20.9 in a group of 12 and 12.9 in a group of 36, the pooled test rejected the true null hypothesis in 12.0% of 4,000 replications (median over 30 seeds; IQR 11.7% to 12.4%), and Welch's test in 5.1% (IQR 5.0% to 5.4%). The pooled test is safe when the variances are equal or the groups are of equal size; when the smaller group has the larger variance, as here, it is not. Welch's t-Test and Unequal Variances maps the error rates across designs and shows that testing the variances first and then choosing is worse than using Welch's test throughout. Welch's test is sometimes described as scipy's default; in scipy 1.18 it is the pooled test that is the default (equal_var=True).

Paired data

When each person is measured twice, the two measurements are not independent samples, and the analysis has to know it. NHANES takes up to three systolic readings per person in one sitting, and the first is on average higher: across the 4,782 adults with a first and third reading, by 0.786 mm Hg. In a sample of 300 of them (seed 9) the mean difference is 0.613 mm Hg:

Call $t$ p 95% interval
stats.ttest_rel(first, third) (paired) 2.0865 0.0378 0.035 to 1.192
stats.ttest_ind(first, third) (pairing ignored) 0.4332 0.6650 $-2.167$ to 3.394

The paired test works on the 300 differences, whose standard deviation is 5.09 mm Hg; the unpaired test compares two columns whose standard deviations are about 17.3, because people differ from each other far more than one reading differs from the next (the two readings correlate at 0.957). Ignoring the pairing throws that information away, and an interval almost five times as wide cannot see a difference of less than 1 mm Hg. Both functions accept the same two arrays, so the software cannot tell which design produced them; The Paired Samples t-Test works the same contrast on Student's sleep data. The signed-rank test on the same differences gives 0.0556; by default it drops the 45 zero differences, pairs whose two readings were equal, a choice its zero_method argument controls (The Sign Test and the Wilcoxon Signed-Rank Test).

Statistical and practical significance

With all 5,182 adults, men's mean systolic pressure, 127.40 mm Hg, exceeds women's, 125.60, by 1.80 mm Hg. Welch's test gives $t = 3.239$ and $p = 0.0012$, and the 95% interval for the difference is 0.71 to 2.89 mm Hg. The difference is real in the sense that it is unlikely to be chance. It is also small: about a tenth of a standard deviation (standardized difference 0.090), much less than the variation between two readings taken minutes apart in the same person. A p-value measures how well the data separate the difference from zero, which depends on the sample size as much as on the difference; it does not measure whether the difference matters (Wasserstein and Lazar, 2016). The interval says both things at once, which is the main reason to report it alongside, or instead of, the p-value.

Choosing the function

Question Recommended in scipy Recommended in statsmodels Default to watch
One proportion, test binomtest(x, n, p0) proportions_ztest(x, n, p0, prop_var=p0) statsmodels' Wald variance
One proportion, interval binomtest(...).proportion_ci(method="wilson") proportion_confint(x, n, method="wilson") statsmodels' "normal"
Two proportions chi2_contingency(t, correction=False), fisher_exact(t) test_proportions_2indep, confint_proportions_2indep scipy's Yates correction
One mean ttest_1samp(x, mu0).confidence_interval() DescrStatsW(x).ttest_mean(mu0) nan_policy="propagate"
Two means ttest_ind(a, b, equal_var=False) ttest_ind(a, b, usevar="unequal") pooled variances in both
Paired means ttest_rel(a, b) DescrStatsW(a - b).ttest_mean(0) none: but the design must be known

The recommendations follow the course pages linked above: Wilson's interval for a proportion, the exact or score test for small samples, and Welch's test whenever the variances may differ. Whatever is chosen, the function and its arguments belong in the report, since the same data can give different p-values under different defaults, and a reader cannot reproduce a number without knowing which one was used.

Common misstatements

Four statements about these functions circulate in tutorials; each is corrected against the libraries' behavior in the versions named under "In Python".

Example (Welch's test is scipy's default). The default of ttest_ind is equal_var=True, the pooled test; Welch's test must be requested.

Example (scipy's $t$-tests return no confidence interval). Current versions return one through the result's confidence_interval() method, as shown above.

Example (prop_var=False is a minor variant). For one sample, statsmodels' default prop_var=False makes proportions_ztest a Wald test, which holds its level less well than the score test in small samples (0.0537 against 0.0361 in the table above).

Example (every test agrees with its interval). That holds when both come from the same statistic, but statsmodels' default interval for a proportion (Wald) and the score test do not share a standard error and can disagree near the boundary. Problem 1 shows the pair that does agree, the score test and Wilson's interval.

Worked problems

The problems extend the article on its own samples. Every number is recomputed, with assertions, in the review-pass block of tools/experiments/data-science/inference-for-proportions-and-means.py.

Problem 1 (Wilson's interval is the inverted score test). Derive Wilson's interval for 53 of 80 from the score test, and check that it agrees with the score test's decision about $p_0 = 0.545$.

Solution. The approach is to collect every $p$ that the score test at level 0.05 would not reject. The test accepts $p$ when $|\hat{p} - p| \le z\sqrt{p(1-p)/n}$, with $z = 1.96$. Squaring gives a quadratic inequality in $p$,

$$(1 + z^2/n)\,p^2 - (2\hat{p} + z^2/n)\,p + \hat{p}^2 \le 0,$$

whose roots are the interval's ends:

$$\frac{\hat{p} + z^2/(2n)}{1 + z^2/n} \pm \frac{z}{1 + z^2/n}\sqrt{\frac{\hat{p}(1-\hat{p})}{n} + \frac{z^2}{4n^2}}.$$

With $\hat{p} = 0.6625$ and $n = 80$, the center is 0.6551 and the half-width 0.1015, giving 0.5536 to 0.7565, the table's Wilson interval. The center is a weighted average of $\hat{p}$ and 1/2, with weight $(z^2/n)/(1 + z^2/n) = 0.0458$ on 1/2, which is why the interval is pulled toward one half while the Wald interval stays centered on $\hat{p}$. By construction the interval excludes 0.545 exactly when the score test rejects it, and both do here ($p = 0.0348$). Interval and test are two views of the same set of accepted values, which is the sense in which they always agree. Likelihood Ratio, Wald and Score Tests derives the same pairing in general and computes the intervals' exact coverage.

Problem 2 (where the pooled test's confidence comes from). For the 12 older and 36 younger adults, compute Welch's degrees of freedom and both standard errors by hand, and show why the pooled $p$-value was 28 times smaller.

Solution. The variances of the two means are estimated by $v_1 = 15.77^2/12 = 20.713$ and $v_2 = 10.84^2/36 = 3.266$. Welch's standard error is $\sqrt{v_1 + v_2} = 4.897$, and the Welch–Satterthwaite degrees of freedom are

$$\frac{(v_1 + v_2)^2}{v_1^2/11 + v_2^2/35} = 14.63,$$

far fewer than $46$ because most of the uncertainty comes from the small group of 12. The pooled test first averages the two variances with weights $11/46 = 0.239$ and $35/46$, giving a pooled standard deviation of 12.202, and then uses $12.202\sqrt{1/12 + 1/36} = 4.067$. That weighting lets the 36 young adults, whose readings vary less, set the variance for the 12 older ones as well, so the standard error is too small by a sixth. With the same difference in means, $t$ is 4.2001 pooled against 3.4887 Welch, and the pooled test also refers it to 46 degrees of freedom instead of 14.63. The two changes together make the pooled $p$-value 28.2 times smaller. Welch's t-Test and Unequal Variances derives the Welch–Satterthwaite approximation.

Problem 3 (what pairing buys). For the 300 adults' first and third readings, derive the standard deviation of the differences from the two readings' standard deviations and their correlation, and compare the paired and unpaired standard errors.

Solution. For any two variables, $\operatorname{Var}(X - Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) - 2\operatorname{Cov}(X, Y)$, and the same identity holds for sample variances and covariances. With standard deviations 17.409 and 17.268 and correlation 0.957,

$$s_d = \sqrt{17.409^2 + 17.268^2 - 2 \times 0.9569 \times 17.409 \times 17.268} = 5.092,$$

the standard deviation of the 300 differences computed directly. The paired standard error is $5.092/\sqrt{300} = 0.2940$. The unpaired test treats the columns as independent and drops the covariance term, giving $\sqrt{17.409^2/300 + 17.268^2/300} = 1.4157$, 4.82 times as large. The whole gain comes from the correlation: people differ from each other by much more than their own readings differ from minute to minute, and the paired analysis subtracts the person out.

Problem 4 (how large a sample a small difference needs). Men's mean systolic pressure exceeds women's by 1.799 mm Hg. How many adults per group would a study need to detect a difference of that size with 80% power at the 5% level, and what power would a study of the same size as the survey have?

Solution. For a two-sided test with known standard deviations $\sigma_1$ and $\sigma_2$, the sample size per group for power $1 - \beta$ against a difference $\delta$ is

$$n = \frac{(z_{0.975} + z_{0.80})^2(\sigma_1^2 + \sigma_2^2)}{\delta^2},$$

because the test statistic must clear $z_{0.975} = 1.96$ with probability 0.80 when its mean is $\delta/\sqrt{(\sigma_1^2 + \sigma_2^2)/n}$, which requires that mean to be $1.96 + 0.84$. With the population standard deviations 18.37 and 21.54 mm Hg and $\delta = 1.799$, $n = 1{,}943.6$, so 1,944 per group (a sample size is rounded up). A new study of 2,541 men and 2,641 women, the survey's numbers, would have power 0.90 if the true difference were 1.8 mm Hg: a significant result would be likely but not guaranteed. That 0.90 is a function of the observed $z = 3.24$ alone, $\Phi(3.24 - 1.96)$, so it adds no information beyond the p-value, and it carries none about whether 1.8 mm Hg matters; Power and Sample Size for a Test of a Mean shows why such "observed power" is fixed by the p-value. Sample Size for Comparing Two Means derives the formula.

Open questions

  • How do the proportion tests' error rates behave across the whole range of $n$ and $p_0$? The exact computation for $n = 30$ and $p_0 = 0.2$ extends directly: computing the true size of the Wald, score and exact tests for every $n$ from 10 to 200 and $p_0$ from 0.05 to 0.5 would map where the Wald test's excess and the exact test's conservatism are large enough to matter.
  • Does the pooled test's excess error rate persist with the actual, non-normal pressures? The 12.0% came from normal populations with the groups' standard deviations. Drawing the 12 and 36 from the two age groups after shifting them to a common mean (30 seeds) would show whether skewness makes it better or worse.
  • Why is the first reading higher than the third? The mean difference of 0.786 mm Hg across 4,782 adults could reflect adaptation to the measurement or regression toward the mean. Comparing the first-to-second and second-to-third differences, and their dependence on the first reading's level, would separate the two explanations.

Where it leads

This article closes the unit with the four standard comparisons and the defaults that decide them. The unit's overview, Probability and Statistics in Practice, lists the articles in order and where the theory lives. The next unit of the collection applies the same tools to models: Introduction to Regression begins with how an outcome depends on inputs. The procedures used here are derived with their assumptions in the Applied Statistics pages linked at the top, and their large-sample theory in the Statistics collection's Likelihood and Estimation unit.

In Python

import numpy as np
from scipy import stats
from statsmodels.stats.proportion import (proportions_ztest, proportion_confint,
                                          test_proportions_2indep, confint_proportions_2indep)
from statsmodels.stats.weightstats import DescrStatsW, CompareMeans

stats.binomtest(53, 80, p=0.545).pvalue                          # exact
proportions_ztest(53, 80, value=0.545, prop_var=0.545)           # score; omit prop_var for Wald
proportion_confint(53, 80, alpha=0.05, method="wilson")

test_proportions_2indep(63, 100, 48, 100, method="score")
stats.chi2_contingency([[63, 37], [48, 52]], correction=False)

stats.ttest_1samp(x, 120, nan_policy="omit").confidence_interval()
res = stats.ttest_ind(old, young, equal_var=False)               # Welch
res.statistic, res.pvalue, res.df, res.confidence_interval()
CompareMeans(DescrStatsW(old), DescrStatsW(young)).tconfint_diff(usevar="unequal")
stats.ttest_rel(first, third)                                    # paired

The numbers in this article are computed by tools/experiments/data-science/inference-for-proportions-and-means.py in the site's repository, which needs statsmodels in addition to NumPy, pandas and scipy (the results shown used NumPy 2.5.4, pandas 3.1.1, scipy 1.18.1 and statsmodels 0.15.0, rerun on 23 November 2026, when the function signatures and defaults named here were also checked in those versions), from the NHANES tables built by psdata.py and the Applied Statistics helper statdata.py; the figure is drawn by tools/figures/inference-for-proportions-and-means.py.

References

Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 10 revisions