KatInterview

Sample Size Calculator for Two-Proportion Z-Test

Arrays & HashingAuto-graded · 13 test cases
MediumAuto-gradedArrays & Hashingexperimentationstatisticspower-analysis

Last updated

Tests ability to implement statistical power calculations for comparing two binary outcome rates in experimental design.

Sample Size Calculator for Two-Proportion Z-Test

You are designing an experiment comparing two groups where each observation has a binary outcome (success or failure). Your task is to determine how many participants are needed in each group to reliably detect a specific difference in success rates.

Write a function that calculates the minimum sample size per group required to detect a given lift with specified statistical power and significance level.

Inputs

Your function receives four parameters:

  • p_control: The baseline success probability in the control group (a float strictly between 0 and 1)
  • lift: The absolute increase in success probability you want to detect (a positive float)
  • power: The desired statistical power, i.e., probability of detecting the effect if it exists (a float strictly between 0 and 1)
  • alpha: The significance level for a two-sided test (a float strictly between 0 and 1)

Task

Calculate the minimum number of observations needed per group using the two-proportion z-test formula with pooled variance under the null hypothesis:

n = [z_{1-α/2} × √(2 × p̄ × (1 - p̄)) + z_{1-β} × √(p₁ × (1 - p₁) + p₂ × (1 - p₂))]² / δ²

Where:

  • p₁ = p_control (control group rate)
  • p₂ = p_control + lift (treatment group rate)
  • δ = lift (absolute difference)
  • p̄ = (p₁ + p₂) / 2 (pooled rate under null hypothesis)
  • z_{1-α/2} is the two-sided critical value from the standard normal distribution at significance level α
  • z_{1-β} is the quantile of the standard normal distribution at probability equal to power

Output

Return a single integer: the sample size per group, rounded up to the nearest whole number.

Validation Rules

If the input parameters are invalid, raise a ValueError with the exact message:

  • If either p₁ or p₂ is not strictly between 0 and 1: raise ValueError('Both event rates must be strictly between 0 and 1')
  • If either power or alpha is not strictly between 0 and 1: raise ValueError('power and alpha must be between 0 and 1')

Implementation Notes

You must implement the standard normal distribution's quantile function (inverse CDF) yourself. Do not use external libraries like scipy or numpy. You may use only:

  • The math module's erf, sqrt, and ceil functions
  • The standard normal CDF formula: Φ(x) = 0.5 × (1 + erf(x / √2))
  • Binary search to compute the inverse CDF (quantile function)

Constraints

  • 0 < p_control < 1
  • 0 < p_control + lift < 1
  • 0 < power < 1
  • 0 < alpha < 1
  • All inputs are floats
  • You may only import from the math module

Examples

Example 1

Input: sample_size_per_group(0.2, 0.03, 0.9, 0.05)

Output: 3939

Hints

Hint 1

Calculate the treatment success rate by adding the lift to the control rate, then find the pooled rate by averaging them.

Hint 2

Implement the standard normal quantile function using binary search over the range [-10, 10] with the forward CDF computed via the error function.

Hint 3

The forward CDF of the standard normal is: Φ(x) = 0.5 × (1 + erf(x / √2)) where erf is the error function from the math module.

Hint 4

Compute z_{1-α/2} by finding the quantile at (1 - alpha/2) and z_{1-β} by finding the quantile at power, then plug both into the formula and round up.