KatInterview

Users Needed Per Group for a Two-Proportion Test

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingstatisticssample-sizepower-analysis

Last 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_a be the standard normal quantile at 1 - significance_level / 2.
  • Let z_b be the standard normal quantile at target_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:

  • p0 or p1 is not strictly between 0 and 1.
  • significance_level or target_power is not strictly between 0 and 1.
  • delta is 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_rate is a float, normally in (0, 1).
  • relative_uplift is a float. It may be negative, but must not be zero.
  • significance_level is a float in (0, 1).
  • target_power is 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.