The two-proportion Z-test (also called the two-sample proportion Z-test) is a statistical hypothesis test for assessing whether two groups differ in the proportion of a binary outcome, in such a significant way that is beyond chance. For example, the proportion of patients responding positively to a treatment in a clinical trial versus control, the defect rate in quality control for two production lines, or the click-through rate in an A/B test of two alternative webpage designs.

The test is appropriate when each observation is independent from another, can be classified as a success or failure (i.e., a Bernoulli trial) and the sample sizes are large enough that the sampling distribution of each sample proportion is well approximated by the central limit theorem. Under those conditions the observed difference of sample proportions can be converted to a standardized z-statistic (using a pooled standard error) and compared to the standard normal distribution to obtain p-values or form confidence intervals for the difference in proportions (derived slightly differently). This also allows to determine required sample-size for a minimum-detectable-effect calculations.

The test is related to other well known tests such as Pearson's chi-squared test, Fisher's exact test for small samples, and McNemar's test for paired binary data.

Definition

The two-proportion Z-test or two-sample proportion Z-test is a statistical method used to determine whether the difference between the proportions of two groups, coming from a binomial distribution is statistically significant. This approach relies on the observation that (for a sufficiently large samples) the sample proportions follow a normal distribution under the Central Limit Theorem, allowing the construction of a z-test for hypothesis testing (a Score test) and confidence interval estimation (a Wald test). It is used in various fields to compare success rates, response rates, or other proportions across different groups.

Hypothesis test

The z-test for comparing two proportions is a frequentist statistical hypothesis test used to evaluate whether two independent samples have different population proportions for a binary outcome. Under mild regularity conditions (sufficiently large sample sizes and independent sampling), the sample proportions (which is the average of observations coming from a Bernoulli distribution) are approximately normally distributed under the central limit theorem, which permits using a z-statistic constructed from the difference of sample proportions and an estimated standard error.

The test involves two competing hypotheses:

  • null hypothesis (H0): The proportions in the two populations are equal, i.e., p 1 = p 2 {\displaystyle p_{1}=p_{2}}.
  • alternative hypothesis (H1): The proportions in the two populations are not equal, i.e., p 1 ≠ p 2 {\displaystyle p_{1}\neq p_{2}} (two-tailed) or p 1 > p 2 {\displaystyle p_{1}>p_{2}} / p 1 < p 2 {\displaystyle p_{1}<p_{2}} (one-tailed).

The z-statistic for comparing two proportions is computed using:z = p ^ 1 − p ^ 2 p ^ ( 1 − p ^ ) ( 1 n 1 + 1 n 2 ) , {\displaystyle z={\frac {{\hat {p}}_{1}-{\hat {p}}_{2}}{\sqrt {{\hat {p}}(1-{\hat {p}})\left({\frac {1}{n_{1}}}+{\frac {1}{n_{2}}}\right)}}},}where p ^ 1 {\displaystyle {\hat {p}}_{1}} and p ^ 2 {\displaystyle {\hat {p}}_{2}} are the sample proportion in the first and second sample, n 1 {\displaystyle n_{1}} and n 2 {\displaystyle n_{2}} are the size of the first and second sample, respectively, p ^ {\displaystyle {\hat {p}}} is pooled proportion, calculated as p ^ = x 1 + x 2 n 1 + n 2 {\displaystyle {\hat {p}}={\frac {x_{1}+x_{2}}{n_{1}+n_{2}}}}, where x 1 {\displaystyle x_{1}} and x 2 {\displaystyle x_{2}} are the counts of successes in the two samples The pooled proportion is used to estimate the shared probability of success under the null hypothesis, and the standard error accounts for variability across the two samples.

The z-test determines statistical significance by comparing the calculated z-statistic to a critical value. E.g., for a significance level of α = 0.05 {\displaystyle \alpha =0.05} we reject the null hypothesis if | z | > 1.96 {\displaystyle |z|>1.96} (for a two-tailed test). Or, alternatively, by computing the p-value and rejecting the null hypothesis if p < α {\displaystyle p<\alpha }.

Confidence interval

The confidence interval for the difference between two proportions, based on the definitions above, is:( p ^ 1 − p ^ 2 ) ± z α / 2 p ^ 1 ( 1 − p ^ 1 ) n 1 + p ^ 2 ( 1 − p ^ 2 ) n 2 , {\displaystyle ({\hat {p}}_{1}-{\hat {p}}_{2})\pm z_{\alpha /2}{\sqrt {{\frac {{\hat {p}}_{1}(1-{\hat {p}}_{1})}{n_{1}}}+{\frac {{\hat {p}}_{2}(1-{\hat {p}}_{2})}{n_{2}}}}},}where z α / 2 {\displaystyle z_{\alpha /2}} is the critical value of the standard normal distribution (e.g., 1.96 for a 95% confidence level).

This interval provides a range of plausible values for the true difference between population proportions.

Notice how the variance estimation is different between the hypothesis testing and the confidence intervals. The first uses a pooled variance (based on the null hypothesis), while the second has to estimate the variance using each sample separately (so as to allow for the confidence interval to accommodate a range of differences in proportions). This difference may lead to slightly different results if using the confidence interval as an alternative to the hypothesis testing method.

Sample size determination and Minimum detectable effect

Sample size determination is the act of choosing the number of observations to include in each group for running the statistical test. For the Two-proportion Z-test, this is closely-related with deciding on the minimum detectable effect.

For finding the required sample size (given some effect size | p 1 − p 2 | {\displaystyle |p_{1}-p_{2}|}, power π = ( 1 − β ) {\displaystyle \pi =(1-\beta )}, and type I error α {\displaystyle \alpha }), we define that n 1 = κ n 2 {\displaystyle n_{1}=\kappa n_{2}}, (when κ = 1, equal sample size is assumed for each group), then:

n 2 = ( Z α / 2 + Z β ) 2 ( p 1 − p 2 ) 2 × ( p 1 ( 1 − p 1 ) κ + p 2 ( 1 − p 2 ) ) 2 {\displaystyle n_{2}={\frac {(Z_{\alpha /2}+Z_{\beta })^{2}}{(p_{1}-p_{2})^{2}}}\times \left({\frac {p_{1}(1-p_{1})}{\kappa }}+p_{2}(1-p_{2})\right)^{2}}

The minimum detectable effect or MDE is the smallest difference between two proportions (p 1 {\displaystyle p_{1}} and p 2 {\displaystyle p_{2}}) that a statistical test can detect for a chosen type I error level (α {\displaystyle \alpha }), statistical power (1 − β {\displaystyle 1-\beta }), and sample sizes (n 1 {\displaystyle n_{1}} and n 2 {\displaystyle n_{2}}). It is commonly used in study design to determine whether the sample sizes allows for a test with sufficient sensitivity to detect meaningful differences.

The MDE for when using the (two-sided) z-test formula for comparing two proportions, incorporating critical values for α {\displaystyle \alpha } and 1 − β {\displaystyle 1-\beta }, and the standard errors of the proportions:MDE = | p 1 − p 2 | = z 1 − α / 2 p 0 ( 1 − p 0 ) ( 1 n 1 + 1 n 2 ) + z 1 − β p 1 ( 1 − p 1 ) n 1 + p 2 ( 1 − p 2 ) n 2 , {\displaystyle {\text{MDE}}=|p_{1}-p_{2}|=z_{1-\alpha /2}{\sqrt {p_{0}(1-p_{0})\left({\frac {1}{n_{1}}}+{\frac {1}{n_{2}}}\right)}}+z_{1-\beta }{\sqrt {{\frac {p_{1}(1-p_{1})}{n_{1}}}+{\frac {p_{2}(1-p_{2})}{n_{2}}}}},}where z 1 − α / 2 {\displaystyle z_{1-\alpha /2}} is critical value for the significance level, z 1 − β {\displaystyle z_{1-\beta }} is quantile for the desired power, and p 0 = p 1 = p 2 {\displaystyle p_{0}=p_{1}=p_{2}} is when assuming the null is correct.

The MDE depends on the sample sizes, baseline proportions (p 1 , p 2 {\displaystyle p_{1},p_{2}}), and test parameters. When the baseline proportions are not known, they need to be assumed or roughly estimated from a small study. Larger samples or smaller power requirements leads to a smaller MDE, making the test more sensitive to smaller differences. Researchers may use the MDE to assess the feasibility of detecting meaningful differences before conducting a study.

The Minimal Detectable Effect (MDE) is the smallest difference, denoted as Δ = | p 1 − p 2 | {\displaystyle \Delta =|p_{1}-p_{2}|}, that satisfies two essential criteria in hypothesis testing:

  1. The null hypothesis (H 0 : p 1 = p 2 {\displaystyle H_{0}:p_{1}=p_{2}}) is rejected at the specified significance level (α {\displaystyle \alpha }).
  2. Statistical power (1 − β {\displaystyle 1-\beta }) is achieved under the alternative hypothesis (H a : p 1 ≠ p 2 {\displaystyle H_{a}:p_{1}\neq p_{2}}).

Given that the distribution is normal under the null and the alternative hypothesis, for the two criteria to happen, it is required that the distance of | p 1 − p 2 | {\displaystyle |p_{1}-p_{2}|} will be such that the critical value for rejecting the null (X critical {\displaystyle X_{\text{critical}}}) is exactly in the location in which the probability of exceeding this value, under the null, is (α {\displaystyle \alpha }), and also that the probability of exceeding this value, under the alternative, is 1 − β {\displaystyle 1-\beta }.

The first criterion establishes the critical value required to reject the null hypothesis. The second criterion specifies how far the alternative distribution must be from X critical {\displaystyle X_{\text{critical}}} to ensure that the probability of exceeding it under the alternative hypothesis is at least 1 − β {\displaystyle 1-\beta }.

Condition 1: Rejecting H 0 {\displaystyle H_{0}}

Under the null hypothesis, the test statistic is based on the pooled standard error (SE null {\displaystyle {\text{SE}}_{\text{null}}}): Z test = | p 1 − p 2 | SE null , where SE null = p 0 ( 1 − p 0 ) ( 1 n 1 + 1 n 2 ) . {\displaystyle Z_{\text{test}}={\frac {|p_{1}-p_{2}|}{{\text{SE}}_{\text{null}}}},\quad {\text{where }}{\text{SE}}_{\text{null}}={\sqrt {p_{0}(1-p_{0})\left({\frac {1}{n_{1}}}+{\frac {1}{n_{2}}}\right)}}.}

p 0 {\displaystyle p_{0}} might be estimated (as described above).

To reject H 0 {\displaystyle H_{0}}, the observed difference must exceed the critical threshold (Z critical = z α / 2 {\displaystyle Z_{\text{critical}}=z_{\alpha /2}}) after properly inflating it to the SE: | p 1 − p 2 | ≥ X c r i t i c a l = z α / 2 ⋅ SE null {\displaystyle |p_{1}-p_{2}|\geq X_{critical}=z_{\alpha /2}\cdot {\text{SE}}_{\text{null}}}

If the MDE is defined solely as M D E = z α / 2 ⋅ SE null {\displaystyle MDE=z_{\alpha /2}\cdot {\text{SE}}_{\text{null}}}, the statistical power would be only 50% because the alternative distribution is symmetric about the threshold. To achieve a higher power level, an additional component is required in the MDE calculation.

Condition 2: Achieving power 1 − β {\displaystyle 1-\beta }

Under the alternative hypothesis, the standard error is (SE alt = p 1 ( 1 − p 1 ) n 1 + p 2 ( 1 − p 2 ) n 2 {\displaystyle {\text{SE}}_{\text{alt}}={\sqrt {{\frac {p_{1}(1-p_{1})}{n_{1}}}+{\frac {p_{2}(1-p_{2})}{n_{2}}}}}}). It means that if the alternative distribution was centered around some value (e.g., X critical {\displaystyle X_{\text{critical}}}), then the minimal | p 1 − p 2 | {\displaystyle |p_{1}-p_{2}|} must be at least larger than z α / 2 ⋅ SE null {\displaystyle z_{\alpha /2}\cdot {\text{SE}}_{\text{null}}} to ensure that the probability of detecting the difference under the alternative hypothesis is at least 1 − β {\displaystyle 1-\beta }.

Combining conditions

To meet both conditions, the total detectable difference incorporates components from both the null and alternative distributions. The MDE is defined as: MDE = z 1 − α / 2 ⋅ SE null + z 1 − β ⋅ SE alt . {\displaystyle {\text{MDE}}=z_{1-\alpha /2}\cdot {\text{SE}}_{\text{null}}+z_{1-\beta }\cdot {\text{SE}}_{\text{alt}}.}

By summing the critical thresholds from the null and adding to it the relevant quantile from the alternative distributions, the MDE ensures the test satisfies the dual requirements of rejecting H 0 {\displaystyle H_{0}} at significance level α {\displaystyle \alpha } and achieving statistical power of at least 1 − β {\displaystyle 1-\beta }.

Assumptions and conditions

To ensure valid results, the following assumptions must be met:

  1. Independent random samples: The samples must be drawn independently from the populations of interest.
  2. Large sample sizes: Typically, n 1 + n 2 {\displaystyle n_{1}+n_{2}} should exceed 20.
  3. Success or failure condition: n 1 p ^ 1 > 10 {\displaystyle n_{1}{\hat {p}}_{1}>10} and n 1 ( 1 − p ^ 1 ) > 10 {\displaystyle n_{1}(1-{\hat {p}}_{1})>10} n 2 p ^ 2 > 10 {\displaystyle n_{2}{\hat {p}}_{2}>10} and n 2 ( 1 − p ^ 2 ) > 10 {\displaystyle n_{2}(1-{\hat {p}}_{2})>10}

The z-test is most reliable when sample sizes are large, and all assumptions are satisfied.

Relation to other statistical methods

Using the z-test for hypothesis testing (Score test) would give the same results as Pearson's chi-squared test for a two-by-two contingency table. Fisher's exact test is more suitable for when the sample sizes are small.

Treatment of 2-by-2 contingency table has been investigated as early as the 19th century, with further work during the 20th century.

Newcombe's method, for the difference of proportions, offers an alternative to the asymptotic method described includes continuity correction, as well as modification that is similar to Wilson score interval.

Notice that:

  • When one or more cell counts are small (e.g. below 5), prefer exact tests (e.g., Fisher's exact test) or exact confidence intervals.
  • For paired or matched binary data use McNemar's test rather than the two-sample z-test.
  • The choice between pooled and unpooled variance matters: pooled variance is appropriate for hypothesis testing of equality (H 0 : p 1 = p 2 {\displaystyle H_{0}:p_{1}=p_{2}}), whereas the unpooled variance is used for confidence intervals.
  • Multiple testing, selection effects, and nonrandom sampling can invalidate p-values and CIs; these design issues should be addressed in the study methods.

In Bayesian inference context, proportions can be modeled using the Beta distribution. The parallel to two proportion z-test is performing similar inference using the difference of two Beta distributions.

Example

Suppose group 1 has 120 successes out of 1000 trials (p ^ 1 = 0.12 {\displaystyle {\hat {p}}_{1}=0.12}) and group 2 has 150 successes out of 1000 trials (p ^ 2 = 0.15 {\displaystyle {\hat {p}}_{2}=0.15}). The pooled proportion is p ^ = ( 120 + 150 ) / ( 1000 + 1000 ) = 0.135 {\displaystyle {\hat {p}}=(120+150)/(1000+1000)=0.135}. The pooled standard error isSE pooled = 0.135 × 0.865 × ( 1 1000 + 1 1000 ) ≈ 0.01529. {\displaystyle {\text{SE}}_{\text{pooled}}={\sqrt {0.135\times 0.865\times \left({\dfrac {1}{1000}}+{\dfrac {1}{1000}}\right)}}\approx 0.01529.}

The z-statistic isz = 0.12 − 0.15 0.01529 ≈ − 1.96 , {\displaystyle z={\dfrac {0.12-0.15}{0.01529}}\approx -1.96,}giving a two-sided p-value of about 0.0497 (just under 0.05). An approximate 95% confidence interval for the difference using the unpooled standard error is( 0.12 − 0.15 ) ± 1.96 0.12 × 0.88 1000 + 0.15 × 0.85 1000 ≈ ( − 0.0599 , − 0.0001 ) . {\displaystyle (0.12-0.15)\pm 1.96{\sqrt {{\dfrac {0.12\times 0.88}{1000}}+{\dfrac {0.15\times 0.85}{1000}}}}\approx (-0.0599,\ -0.0001).}Because the 95% CI (just barely) excludes 0 and the p-value is ≈0.0497, the difference is statistically significant at the 5% level by the usual large-sample criteria (but is borderline; conclusions should account for study context and multiple testing if applicable).

Software implementation

Implementations are available in many statistical environments. See below for implementation details in some popular languages. Other implementations also exists for SPSS, SAS, and Minitab.

R

Use prop.test() with continuity correction disabled:

Output includes z-test equivalent results: chi-squared statistic, p-value, and confidence interval:

Python

Use proportions_ztest from statsmodels:

SQL

Direct implementation of the formulas from above, using Presto flavour of SQL (relying on VALUES, inverse_normal_cdf, and normal_cdf)

See also

External links

  • Sample size / power online calculators: