Compare one-sided and two-sided sample sizes for a two-proportion A/B test

Quick Overview

Write a Python function that uses statsmodels power analysis to find the per-group sample size of a two-proportion A/B test under a two-sided and a one-sided test, plus how many users the one-sided test saves. It tests Cohen's h, the choice of alternative hypothesis, and careful rounding.

Compare one-sided and two-sided sample sizes for a two-proportion A/B test

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `one_vs_two_sided_n` in `solution.py`. For a two-proportion A/B test, it computes the per-group sample size needed under a two-sided test and under a one-sided test, and how many observations per group the one-sided test saves. ```python def one_vs_two_sided_n(p_control: float, p_treatment: float, alpha: float = 0.05, power: float = 0.8) -> dict: ... ``` The function must follow these rules: - Compute `effect_size = proportion_effectsize(p_treatment, p_control)`, then call `NormalIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, ratio=1.0, alternative=...)`. - Return a dict with exactly the keys `'two_sided'`, `'one_sided'` and `'saved'`, each a Python `int`. - `'two_sided'` is `math.ceil(n)` with `alternative='two-sided'`. - `'one_sided'` is `math.ceil(n)` with `alternative='larger'` (not `'smaller'`). - `'saved'` is `two_sided - one_sided` computed from the two rounded integers, not the ceiling of the raw difference. Example: ```python one_vs_two_sided_n(0.10, 0.12) # {'two_sided': 3835, 'one_sided': 3021, 'saved': 814} ``` ```hint Predict the gap first Under the normal approximation, the required n grows with the square of (critical value + power quantile). Compare the two-sided and one-sided critical values at alpha 0.05 to predict the ratio of the two sizes before you run any code. ``` ```hint Rounding Try both ceil(a) - ceil(b) and ceil(a - b) on the raw sizes for the example and check whether they agree. ``` ### What a Strong Answer Covers - Computes the arcsine effect size once, with treatment as the first argument so a lift gives a positive h. - Calls the solver twice with only `alternative` changed, using `'larger'` for the one-sided case. - Rounds each size up separately, returns plain `int` values, and takes `saved` from the rounded sizes. - Explains why the one-sided size is smaller and roughly by how much at alpha 0.05 and 80% power. - Names the inputs where the function breaks, such as equal proportions or a treatment rate below control under the `'larger'` test. ### Follow-up Questions - When is a one-sided test appropriate, and what is the risk of using one? - How would you extend the function to unequal group allocation? - Why might a textbook pooled-variance sample size formula give a slightly different number for the same inputs?

Overview: Write a Python function that uses statsmodels power analysis to find the per-group sample size of a two-proportion A/B test under a two-sided and a one-sided test, plus how many users the one-sided test saves. It tests Cohen's h, the choice of alternative hypothesis, and careful rounding.

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

Implement one_vs_two_sided_n in solution.py. For a two-proportion A/B test, it computes the per-group sample size needed under a two-sided test and under a one-sided test, and how many observations per group the one-sided test saves.

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

The function must follow these rules:

  • Compute effect_size = proportion_effectsize(p_treatment, p_control) , then call NormalIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, ratio=1.0, alternative=...) .
  • Return a dict with exactly the keys 'two_sided' , 'one_sided' and 'saved' , each a Python int .
  • 'two_sided' is math.ceil(n) with alternative='two-sided' .
  • 'one_sided' is math.ceil(n) with alternative='larger' (not 'smaller' ).
  • 'saved' is two_sided - one_sided computed from the two rounded integers, not the ceiling of the raw difference.

Example:

one_vs_two_sided_n(0.10, 0.12)
# {'two_sided': 3835, 'one_sided': 3021, 'saved': 814}

What a Strong Answer Covers Guidance

  • Computes the arcsine effect size once, with treatment as the first argument so a lift gives a positive h.
  • Calls the solver twice with only alternative changed, using 'larger' for the one-sided case.
  • Rounds each size up separately, returns plain int values, and takes saved from the rounded sizes.
  • Explains why the one-sided size is smaller and roughly by how much at alpha 0.05 and 80% power.
  • Names the inputs where the function breaks, such as equal proportions or a treatment rate below control under the 'larger' test.

Follow-up Questions Guidance

  • When is a one-sided test appropriate, and what is the risk of using one?
  • How would you extend the function to unequal group allocation?
  • Why might a textbook pooled-variance sample size formula give a slightly different number for the same inputs?