Compute per-arm sample sizes for an A/B test with unequal traffic allocation

Quick Overview

Write a Python function that sizes a two-proportion A/B test where treatment gets a fixed multiple of control traffic, returning per-arm and total sample sizes from statsmodels. It tests how unequal allocation changes a power calculation and how to round arm sizes consistently.

Compute per-arm sample sizes for an A/B test with unequal traffic allocation

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `unequal_allocation_n` in `solution.py` to compute per-arm sample sizes for a two-proportion A/B test when treatment gets `ratio` times as many users as control (for example, `ratio=2.0` puts twice as many users in treatment). Compute the effect size with `proportion_effectsize(p_treatment, p_control)` and solve for the control-arm size with `NormalIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, ratio=ratio, alternative='two-sided')`. ```python def unequal_allocation_n(p_control: float, p_treatment: float, ratio: float, alpha: float = 0.05, power: float = 0.8) -> dict: ... ``` The function must follow these rules: - `nobs1` is the control arm, and `ratio` is `nobs2 / nobs1`, that is, treatment over control. - Return a dict with exactly the keys `'n_control'`, `'n_treatment'` and `'total'`. - `'n_control'` is `math.ceil(n1)` as an int. - `'n_treatment'` is `math.ceil(n1 * ratio)` as an int. Round the raw product up, not `ceil(n1) * ratio`. - `'total'` is `n_control + n_treatment`, the sum of the two rounded ints. Example: ```python unequal_allocation_n(0.10, 0.12, 2.0) # {'n_control': 2876, 'n_treatment': 5752, 'total': 8628} ``` ```hint How allocation enters The precision of the difference depends on 1/n_control + 1/n_treatment. Rewrite that in terms of the control size and the ratio to see how the control arm changes as the ratio grows. ``` ```hint Test the rounding The example happens to give the same treatment size under both rounding orders. Try a ratio such as 1.5 or 3.0 and compare ceil(n1 * ratio) with ceil(n1) * ratio. ``` ### What a Strong Answer Covers - Maps control to `nobs1` and passes the treatment-to-control ratio as `ratio`. - Applies the specified rounding to each arm from the raw solver output and returns ints. - Computes the total from the rounded arm sizes so the three numbers are consistent. - Explains why a skewed split needs more users in total than an equal split, and roughly how many more at 2:1. - Names inputs that need care, such as a ratio below 1, a ratio of zero or less, and equal proportions. ### Follow-up Questions - Why would a team accept the larger total that an unequal split requires? - Does `ratio=0.5` give the same total as `ratio=2.0` for these rates? - With several treatment arms sharing one control, how would you size the control arm?

Overview: Write a Python function that sizes a two-proportion A/B test where treatment gets a fixed multiple of control traffic, returning per-arm and total sample sizes from statsmodels. It tests how unequal allocation changes a power calculation and how to round arm sizes consistently.

General logo
General
Nov 25, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement unequal_allocation_n in solution.py to compute per-arm sample sizes for a two-proportion A/B test when treatment gets ratio times as many users as control (for example, ratio=2.0 puts twice as many users in treatment). Compute the effect size with proportion_effectsize(p_treatment, p_control) and solve for the control-arm size with NormalIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, ratio=ratio, alternative='two-sided').

def unequal_allocation_n(p_control: float, p_treatment: float, ratio: float,
                         alpha: float = 0.05, power: float = 0.8) -> dict:
    ...

The function must follow these rules:

  • nobs1 is the control arm, and ratio is nobs2 / nobs1 , that is, treatment over control.
  • Return a dict with exactly the keys 'n_control' , 'n_treatment' and 'total' .
  • 'n_control' is math.ceil(n1) as an int.
  • 'n_treatment' is math.ceil(n1 * ratio) as an int. Round the raw product up, not ceil(n1) * ratio .
  • 'total' is n_control + n_treatment , the sum of the two rounded ints.

Example:

unequal_allocation_n(0.10, 0.12, 2.0)
# {'n_control': 2876, 'n_treatment': 5752, 'total': 8628}

What a Strong Answer Covers Guidance

  • Maps control to nobs1 and passes the treatment-to-control ratio as ratio .
  • Applies the specified rounding to each arm from the raw solver output and returns ints.
  • Computes the total from the rounded arm sizes so the three numbers are consistent.
  • Explains why a skewed split needs more users in total than an equal split, and roughly how many more at 2:1.
  • Names inputs that need care, such as a ratio below 1, a ratio of zero or less, and equal proportions.

Follow-up Questions Guidance

  • Why would a team accept the larger total that an unequal split requires?
  • Does ratio=0.5 give the same total as ratio=2.0 for these rates?
  • With several treatment arms sharing one control, how would you size the control arm?