Users Needed Per Group for a Two-Proportion Test
Arrays & HashingAuto-graded · 21 test casesLast updated
Compute the smallest equal group size for a two-sided two-proportion z-test, given a baseline rate, relative uplift, significance level and target power.
Users Needed Per Group for a Two-Proportion Test
A growth team wants to test whether a new messaging policy raises the share of users who make a purchase within a week. Users are split into two groups of equal size: a control group and a treatment group. Each user's outcome is independent and is either "purchased" or "did not purchase".
Write a function that returns the smallest number of users per group needed to detect the expected uplift with a two-sided test of two proportions.
Setup
- The control purchase rate is
p0 = control_rate. - The treatment purchase rate is
p1 = p0 * (1 + relative_uplift). - The absolute difference is
delta = |p1 - p0|. - The pooled rate is
p_bar = (p0 + p1) / 2. - Let
z_abe the standard normal quantile at1 - significance_level / 2. - Let
z_bbe the standard normal quantile attarget_power.
Use the plain normal approximation, without any continuity correction:
n = ( z_a * sqrt(2 * p_bar * (1 - p_bar))
+ z_b * sqrt(p0 * (1 - p0) + p1 * (1 - p1)) )^2 / delta^2
Return n rounded up to the next integer.
Input validation
Raise a ValueError if any of the following holds:
p0orp1is not strictly between 0 and 1.significance_levelortarget_poweris not strictly between 0 and 1.deltais exactly 0, i.e. the uplift is zero.
A negative relative_uplift is allowed as long as the checks above pass. Only delta, the absolute difference, enters the formula.
Function
Implement required_users_per_group(control_rate, relative_uplift, significance_level, target_power), which returns an int.
Constraints
control_rateis a float, normally in (0, 1).relative_upliftis a float. It may be negative, but must not be zero.significance_levelis a float in (0, 1).target_poweris a float in (0, 1).- Both groups have the same size.
- Use the formula above exactly, with no continuity correction and no extra inflation factors.
Examples
Example 1
Input: required_users_per_group(0.05, 0.08, 0.05, 0.8)
Output: 48364
Example 2
Input: required_users_per_group(0.1, 0.1, 0.05, 0.8)
Output: 14751
Example 3
Input: required_users_per_group(0.02, 0.2, 0.01, 0.9)
Output: 40016
Hints
Hint 1
Start by deriving the treatment rate p1 from the control rate and the relative uplift, then get the absolute gap between the two rates.
Hint 2
The standard normal quantile function (inverse CDF) is available in Python's statistics.NormalDist. You need one quantile for the two-sided significance level and one for the power.
Hint 3
The first square root uses the pooled rate under the null. The second uses the sum of the two separate Bernoulli variances. Square the weighted sum, divide by the squared gap, and round up with ceil.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a direct application of the standard sample-size formula for comparing two independent proportions with equal group sizes.
- Compute the treatment rate
p1 = p0 * (1 + relative_uplift)and the absolute gapdelta = |p1 - p0|. - Validate the inputs. Both rates must lie strictly inside (0, 1),
significance_levelandtarget_powermust lie in (0, 1), anddeltamust be nonzero. - Get the two normal quantiles:
z_aat1 - significance_level / 2, for the two-sided test.z_battarget_power.
- Compute the pooled rate
p_bar = (p0 + p1) / 2. - Evaluate the formula and round up.
n = ( z_a * sqrt(2 * p_bar * (1 - p_bar)) + z_b * sqrt(p0*(1-p0) + p1*(1-p1)) )^2 / delta^2
Why it is correct
Under the null hypothesis both groups share the rate p_bar. The standard error of the difference in sample proportions is then sqrt(2 * p_bar * (1 - p_bar) / n). The test rejects when the observed difference exceeds z_a times this standard error.
Under the alternative the rates differ, so the standard error of the difference is sqrt((p0*(1-p0) + p1*(1-p1)) / n). To have probability target_power of crossing the rejection boundary, the true gap delta must cover the null-based boundary plus z_b alternative standard errors:
delta = z_a * sqrt(2 * p_bar * (1 - p_bar) / n) + z_b * sqrt((p0*(1-p0) + p1*(1-p1)) / n)
Solving for n gives the formula above. Since users come in whole numbers and the requirement is a minimum, round up.
Only delta appears in the denominator, so a negative uplift gives the same kind of answer as a positive one, with p1 being the lower rate.
Complexity
Time is O(1) and space is O(1), since only a few arithmetic operations and two inverse-CDF evaluations are needed.
Reference solution
from math import ceil
from statistics import NormalDist
def required_users_per_group(control_rate: float, relative_uplift: float, significance_level: float, target_power: float) -> int:
p0 = control_rate
p1 = p0 * (1.0 + relative_uplift)
delta = abs(p1 - p0)
if not (0.0 < p0 < 1.0 and 0.0 < p1 < 1.0):
raise ValueError("rates must be strictly between 0 and 1")
if not (0.0 < significance_level < 1.0 and 0.0 < target_power < 1.0):
raise ValueError("significance_level and target_power must be strictly between 0 and 1")
if delta == 0.0:
raise ValueError("relative_uplift must be nonzero")
normal = NormalDist()
z_a = normal.inv_cdf(1.0 - significance_level / 2.0)
z_b = normal.inv_cdf(target_power)
p_bar = (p0 + p1) / 2.0
spread = (
z_a * (2.0 * p_bar * (1.0 - p_bar)) ** 0.5
+ z_b * (p0 * (1.0 - p0) + p1 * (1.0 - p1)) ** 0.5
) ** 2
return ceil(spread / (delta * delta))