# Two-proportion Z-test

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 assumption that the sample proportions follow a normal distribution under the Central Limit Theorem, allowing the construction of a z-test for hypothesis testing and confidence interval estimation. 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 Statistical hypothesis test for evaluating whether the proportion of a certain characteristic differs significantly between two independent samples. This test leverages the property that the sample proportions (which is the average of observations coming from a Bernoulli distribution) are asymptotically normal under the Central Limit Theorem, enabling the construction of a z-test.
The test involves two competing hypotheses:
* Null hypothesis (H$_0$): The proportions in the two populations are equal, i.e., $p_{1}=p_{2}$.
* Alternative hypothesis (H$_1$): The proportions in the two populations are not equal, i.e., $p_{1}\neq p_{2}$ (two-tailed) or $p_{1}>p_{2}$ / $p_{1}<p_{2}$ (one-tailed).
The z-statistic for comparing two proportions is computed using:
$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:
* ${\hat {p}}_{1}$ = sample proportion in the first sample
* ${\hat {p}}_{2}$ = sample proportion in the second sample
* $n_{1}$ = size of the first sample
* $n_{2}$ = size of the second sample
* ${\hat {p}}$ = pooled proportion, calculated as ${\hat {p}}={\frac {x_{1}+x_{2}}{n_{1}+n_{2}}}$, where $x_{1}$ and $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 $\alpha =0.05$ we reject the null hypothesis if $|z|>1.96$ (for a two-tailed test). Or, alternatively, by computing the p-value and rejecting the null hypothesis if $p<\alpha$.

## Confidence interval

The confidence interval for the difference between two proportions, based on the definitions above, is:
$({\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_{\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.
Using the z-test confidence intervals for hypothesis testing would give the same results as the chi-squared test for a two-by-two contingency table.$^{:216–7}$$^{:875}$ Fisher’s exact test is more suitable for when the sample sizes are small.
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.

## Minimum detectable effect (MDE)

The minimum detectable effect (MDE) is the smallest difference between two proportions ($p_{1}$ and $p_{2}$) that a statistical test can detect for a chosen Type I error level ($\alpha$), statistical power ($1-\beta$), and sample sizes ($n_{1}$ and $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 $\alpha$ and $1-\beta$, and the standard errors of the proportions:
${\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-\alpha /2}$: Critical value for the significance level.
* $z_{1-\beta }$: Quantile for the desired power.
* $p_{0}=p_{1}=p_{2}$: When assuming the null is correct.
The MDE depends on the sample sizes, baseline proportions ($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 $\Delta =|p_{1}-p_{2}|$, that satisfies two essential criteria in hypothesis testing:
* The null hypothesis ($H_{0}:p_{1}=p_{2}$) is rejected at the specified significance level ($\alpha$).
* Statistical power ($1-\beta$) is achieved under the alternative hypothesis ($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}|$ will be such that the critical value for rejecting the null ($X_{\text{critical}}$) is exactly in the location in which the probability of exceeding this value, under the null, is ($\alpha$), and also that the probability of exceeding this value, under the alternative, is $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_{\text{critical}}$ to ensure that the probability of exceeding it under the alternative hypothesis is at least $1-\beta$.
Condition 1: Rejecting $H_{0}$
Under the null hypothesis, the test statistic is based on the pooled standard error (${\text{SE}}_{\text{null}}$): $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}$ might be estimated (as described above).
To reject $H_{0}$, the observed difference must exceed the critical threshold ($Z_{\text{critical}}=z_{\alpha /2}$) after properly inflating it to the SE: $|p_{1}-p_{2}|\geq X_{critical}=z_{\alpha /2}\cdot {\text{SE}}_{\text{null}}$
If the MDE is defined solely as $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-\beta$
Under the alternative hypothesis, the standard error is (${\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_{\text{critical}}$), then the minimal $|p_{1}-p_{2}|$ must be at least larger than $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-\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: ${\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}$ at significance level $\alpha$ and achieving statistical power of at least $1-\beta$.

## Assumptions and conditions

To ensure valid results, the following assumptions must be met:
* Independent random samples: The samples must be drawn independently from the populations of interest.
* Large sample sizes: Typically, $n_{1}$ and $n_{2}$ should exceed 30.
* Success/failure condition:
** $n_{1}{\hat {p}}_{1}>10$ and $n_{1}(1-{\hat {p}}_{1})>10$
** $n_{2}{\hat {p}}_{2}>10$ and $n_{2}(1-{\hat {p}}_{2})>10$
The z-test is most reliable when sample sizes are large, and all assumptions are satisfied.

## Software implementation
### 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: