Sample Size per Group for a Two-Sided Two-Proportion z-Test (Pooled Variance)

Quick Overview

Implement the per-group sample size for a two-sided two-proportion z-test using the pooled-variance normal approximation, rounded up to an integer. It tests critical values for alpha and power, where the null and alternative variance terms come from, and how the result scales with the effect size.

Sample Size per Group for a Two-Sided Two-Proportion z-Test (Pooled Variance)

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `sample_size_pooled(p_control: float, p_treatment: float, alpha: float = 0.05, power: float = 0.8) -> int` in `solution.py`. Return the required sample size per group for a two-sided two-proportion z-test, using exactly this pooled-variance formula (not the unpooled variant or the `statsmodels` arcsine result): ```python z_alpha = norm.ppf(1 - alpha/2) z_beta = norm.ppf(power) p_bar = (p_control + p_treatment) / 2 n = (z_alpha*sqrt(2*p_bar*(1-p_bar)) + z_beta*sqrt(p_control*(1-p_control) + p_treatment*(1-p_treatment)))**2 \ / (p_treatment - p_control)**2 return math.ceil(n) ``` Return `math.ceil(n)` as a Python `int` (count per group). Use `norm` from `scipy.stats`. For example: ```python sample_size_pooled(0.10, 0.12) # 3841 ``` With a 10% control rate, a 12% treatment rate and the default alpha and power, the formula gives about 3840.85, which rounds up to 3841 users in each group. ```hint Two variance terms Think about how the difference in sample rates is distributed when the null is true and when the alternative is true. Each square root in the formula belongs to one of those two cases. ``` ```hint Rounding and types Round up once, at the very end, and make sure the function returns a plain Python int. ``` ### Constraints & Assumptions - Inputs are valid: `p_control != p_treatment` and both rates are strictly between 0 and 1. - `alpha` and `power` are probabilities strictly between 0 and 1, with defaults 0.05 and 0.8. - Both groups have the same size, and the function returns the size of one group. ### What a Strong Answer Covers - A faithful implementation of the given formula, with the two-sided critical value and the power quantile. - Rounding up and returning a Python int for one group. - Where each variance term comes from, and why the null term uses the averaged rate `p_bar`. - How the required size changes with the effect size and the baseline rate. ### Follow-up Questions - How does the unpooled formula differ, and when is it preferred? - How would you extend this to unequal group allocation (for example 2:1)? - If the absolute lift you want to detect is halved, roughly how does the required sample size change?

Overview: Implement the per-group sample size for a two-sided two-proportion z-test using the pooled-variance normal approximation, rounded up to an integer. It tests critical values for alpha and power, where the null and alternative variance terms come from, and how the result scales with the effect size.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement sample_size_pooled(p_control: float, p_treatment: float, alpha: float = 0.05, power: float = 0.8) -> int in solution.py. Return the required sample size per group for a two-sided two-proportion z-test, using exactly this pooled-variance formula (not the unpooled variant or the statsmodels arcsine result):

z_alpha = norm.ppf(1 - alpha/2)
z_beta  = norm.ppf(power)
p_bar   = (p_control + p_treatment) / 2
n = (z_alpha*sqrt(2*p_bar*(1-p_bar))
     + z_beta*sqrt(p_control*(1-p_control) + p_treatment*(1-p_treatment)))**2 \
    / (p_treatment - p_control)**2
return math.ceil(n)

Return math.ceil(n) as a Python int (count per group). Use norm from scipy.stats.

For example:

sample_size_pooled(0.10, 0.12)  # 3841

With a 10% control rate, a 12% treatment rate and the default alpha and power, the formula gives about 3840.85, which rounds up to 3841 users in each group.

Constraints & Assumptions

  • Inputs are valid: p_control != p_treatment and both rates are strictly between 0 and 1.
  • alpha and power are probabilities strictly between 0 and 1, with defaults 0.05 and 0.8.
  • Both groups have the same size, and the function returns the size of one group.

What a Strong Answer Covers Guidance

  • A faithful implementation of the given formula, with the two-sided critical value and the power quantile.
  • Rounding up and returning a Python int for one group.
  • Where each variance term comes from, and why the null term uses the averaged rate p_bar .
  • How the required size changes with the effect size and the baseline rate.

Follow-up Questions Guidance

  • How does the unpooled formula differ, and when is it preferred?
  • How would you extend this to unequal group allocation (for example 2:1)?
  • If the absolute lift you want to detect is halved, roughly how does the required sample size change?