A/B/n readout: test each arm against control with Holm correction

Quick Overview

Build an A/B/n experiment readout in Python that tests each treatment arm against control with a two-proportion z-test and applies Holm correction to the p-values. It tests multiple-comparison control, keeping results aligned in a pandas DataFrame with exact dtypes, and the edge cases that break the z-test.

A/B/n readout: test each arm against control with Holm correction

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `abn_readout(counts, alpha=0.05)`. `counts` maps arm names to `(conversions, visitors)` and always includes `'control'` plus one or more treatment arms. Test each treatment arm against control, Holm-correct across those comparisons, and return a `pd.DataFrame`. Requirements: - For each treatment arm, run `proportions_ztest([conv_arm, conv_control], [n_arm, n_control])` with its defaults (pooled variance, `alternative='two-sided'`) and keep only the p-value. - Correct only those `m-1` p-values with `multipletests(..., alpha=alpha, method='holm')`. Its `reject` output becomes `'significant'` and its `pvals_corrected` output becomes `'p_adjusted'`. - Return one row per treatment arm (no control row), sorted by arm name ascending, with a clean `RangeIndex` from 0 to m-2. - The columns, in order, are `'arm'` (object), `'abs_lift'` (float64, arm rate minus control rate, where rate = conversions / visitors), `'p_value'` (float64, raw), `'p_adjusted'` (float64) and `'significant'` (bool). Example: ```python counts = { 'control': (200, 2000), 'variant_a': (240, 2000), 'variant_b': (210, 2000), } df = abn_readout(counts, alpha=0.05) # p-values rounded to 4 decimals: # arm abs_lift p_value p_adjusted significant # 0 variant_a 0.020 0.0432 0.0865 False # 1 variant_b 0.005 0.6022 0.6022 False ``` `variant_a` would pass a single test at 0.05, but with two comparisons Holm compares its p-value against 0.05 / 2, so it is not significant after correction. ```hint Keeping the arrays aligned `multipletests` returns plain arrays in the order you passed the p-values. Decide the final row order before you run any test. ``` ```hint Dtypes Build each column with an explicit dtype. Lists of numpy scalars or strings do not always infer the dtype the spec asks for. ``` ### Constraints & Assumptions - `counts` always contains `'control'` and at least one treatment arm. - All visitor counts are positive. - `statsmodels` and `pandas` are available. ### What a Strong Answer Covers - Runs one pooled two-proportion z-test per treatment arm against control, with control always in the same position. - Applies Holm to exactly the treatment-versus-control p-values and explains what that family-wise correction controls. - Keeps row order, p-values and reject flags aligned, and returns the exact columns, dtypes and index. - Checks `counts` inside the function before running any test, so malformed input fails loudly. ### Follow-up Questions - Why Holm here, and not Bonferroni or Benjamini-Hochberg? - How would you extend the readout with confidence intervals for `abs_lift`? - What edge cases could break the z-test, and how would you guard them?

Overview: Build an A/B/n experiment readout in Python that tests each treatment arm against control with a two-proportion z-test and applies Holm correction to the p-values. It tests multiple-comparison control, keeping results aligned in a pandas DataFrame with exact dtypes, and the edge cases that break the z-test.

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

Implement abn_readout(counts, alpha=0.05). counts maps arm names to (conversions, visitors) and always includes 'control' plus one or more treatment arms. Test each treatment arm against control, Holm-correct across those comparisons, and return a pd.DataFrame.

Requirements:

  • For each treatment arm, run proportions_ztest([conv_arm, conv_control], [n_arm, n_control]) with its defaults (pooled variance, alternative='two-sided' ) and keep only the p-value.
  • Correct only those m-1 p-values with multipletests(..., alpha=alpha, method='holm') . Its reject output becomes 'significant' and its pvals_corrected output becomes 'p_adjusted' .
  • Return one row per treatment arm (no control row), sorted by arm name ascending, with a clean RangeIndex from 0 to m-2.
  • The columns, in order, are 'arm' (object), 'abs_lift' (float64, arm rate minus control rate, where rate = conversions / visitors), 'p_value' (float64, raw), 'p_adjusted' (float64) and 'significant' (bool).

Example:

counts = {
    'control': (200, 2000),
    'variant_a': (240, 2000),
    'variant_b': (210, 2000),
}
df = abn_readout(counts, alpha=0.05)
# p-values rounded to 4 decimals:
#          arm  abs_lift  p_value  p_adjusted  significant
# 0  variant_a     0.020   0.0432      0.0865        False
# 1  variant_b     0.005   0.6022      0.6022        False

variant_a would pass a single test at 0.05, but with two comparisons Holm compares its p-value against 0.05 / 2, so it is not significant after correction.

Constraints & Assumptions

  • counts always contains 'control' and at least one treatment arm.
  • All visitor counts are positive.
  • statsmodels and pandas are available.

What a Strong Answer Covers Guidance

  • Runs one pooled two-proportion z-test per treatment arm against control, with control always in the same position.
  • Applies Holm to exactly the treatment-versus-control p-values and explains what that family-wise correction controls.
  • Keeps row order, p-values and reject flags aligned, and returns the exact columns, dtypes and index.
  • Checks counts inside the function before running any test, so malformed input fails loudly.

Follow-up Questions Guidance

  • Why Holm here, and not Bonferroni or Benjamini-Hochberg?
  • How would you extend the readout with confidence intervals for abs_lift ?
  • What edge cases could break the z-test, and how would you guard them?