Compute an A/B test readout for a ratio metric with delta-method standard errors

Quick Overview

Implement an A/B test readout for a ratio metric such as clicks per page view, using the delta method for each arm's standard error and a normal confidence interval for the lift. It tests the variance formula, the covariance term and how independent arm variances combine.

Compute an A/B test readout for a ratio metric with delta-method standard errors

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

An A/B test randomizes users, and the metric is a ratio of two per-user counts, such as clicks per page view. Each arm gives you per-user arrays `num` (numerator events) and `den` (denominator events). Implement `ratio_metric_lift_readout` in `solution.py`: ```python def ratio_metric_lift_readout(num_control, den_control, num_treatment, den_treatment, alpha=0.05) -> dict: ... ``` Return a dict with exactly the keys `'rate_control'`, `'rate_treatment'`, `'absolute_lift'`, `'se_control'`, `'se_treatment'`, `'ci_lower'`, `'ci_upper'`. Requirements: - Per arm: `R = num.sum() / den.sum()`. - Delta-method SE per arm, with `n = len(num)`, `xbar = den.mean()` and `cov = np.cov(num, den, ddof=1)`: `se = sqrt((1/(n*xbar**2)) * (cov[0,0] + R**2*cov[1,1] - 2*R*cov[0,1]))`. Keep the cross term. - `absolute_lift = rate_treatment - rate_control`. - `se_lift = sqrt(se_control**2 + se_treatment**2)`. - `z = scipy.stats.norm.ppf(1 - alpha/2)`, `ci_lower = absolute_lift - z*se_lift` and `ci_upper = absolute_lift + z*se_lift`. Example (with the default `alpha=0.05`): ```python num_control = [3, 0, 2, 5, 1] den_control = [10, 8, 12, 20, 5] num_treatment = [4, 2, 3, 6, 2] den_treatment = [9, 7, 11, 18, 6] { 'rate_control': 0.2, 'rate_treatment': 0.3333333333333333, 'absolute_lift': 0.1333333333333333, 'se_control': 0.044163482914513164, 'se_treatment': 0.027341830932486145, 'ci_lower': 0.031528552409623434, 'ci_upper': 0.23513811425704317, } ``` ```hint Reading np.cov `np.cov(num, den)` returns a 2x2 matrix. Check which entry holds the numerator's variance, which holds the denominator's, and which holds their covariance before you plug them into the formula. ``` ```hint A second check The expression inside the square root, before dividing by `n*xbar**2`, equals the sample variance of one per-user array you can build from `num`, `den` and `R`. Finding it gives you an independent check of your numbers. ``` ### Constraints & Assumptions - `numpy` and `scipy.stats` are available. - Within each arm, `num` and `den` are non-empty and have equal length. - No p-value is required. ### What a Strong Answer Covers - The pooled ratio per arm as a sum over a sum, and why the user is the unit for the variance - The delta-method variance with the covariance term, the right entries of `np.cov` and `ddof=1` - Combining the two arm variances for the lift and building a two-sided interval from `alpha` - The seven keys exactly as named, with the example values reproduced - Inputs that break the readout: an arm with one user, a zero denominator sum, arms too small for the normal approximation ### Follow-up Questions - Why does the delta method beat a naive per-user-rate t-test here? - How would you extend this when randomization is at the user level but the metric unit is finer (clustering)? - How would you report the lift as a relative change, and what is its standard error? - When would you use a bootstrap instead of the delta method?

Overview: Implement an A/B test readout for a ratio metric such as clicks per page view, using the delta method for each arm's standard error and a normal confidence interval for the lift. It tests the variance formula, the covariance term and how independent arm variances combine.

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

An A/B test randomizes users, and the metric is a ratio of two per-user counts, such as clicks per page view. Each arm gives you per-user arrays num (numerator events) and den (denominator events). Implement ratio_metric_lift_readout in solution.py:

def ratio_metric_lift_readout(num_control, den_control, num_treatment, den_treatment, alpha=0.05) -> dict:
    ...

Return a dict with exactly the keys 'rate_control', 'rate_treatment', 'absolute_lift', 'se_control', 'se_treatment', 'ci_lower', 'ci_upper'.

Requirements:

  • Per arm: R = num.sum() / den.sum() .
  • Delta-method SE per arm, with n = len(num) , xbar = den.mean() and cov = np.cov(num, den, ddof=1) : se = sqrt((1/(n*xbar**2)) * (cov[0,0] + R**2*cov[1,1] - 2*R*cov[0,1])) . Keep the cross term.
  • absolute_lift = rate_treatment - rate_control .
  • se_lift = sqrt(se_control**2 + se_treatment**2) .
  • z = scipy.stats.norm.ppf(1 - alpha/2) , ci_lower = absolute_lift - z*se_lift and ci_upper = absolute_lift + z*se_lift .

Example (with the default alpha=0.05):

num_control   = [3, 0, 2, 5, 1]
den_control   = [10, 8, 12, 20, 5]
num_treatment = [4, 2, 3, 6, 2]
den_treatment = [9, 7, 11, 18, 6]

{
  'rate_control': 0.2,
  'rate_treatment': 0.3333333333333333,
  'absolute_lift': 0.1333333333333333,
  'se_control': 0.044163482914513164,
  'se_treatment': 0.027341830932486145,
  'ci_lower': 0.031528552409623434,
  'ci_upper': 0.23513811425704317,
}

Constraints & Assumptions

  • numpy and scipy.stats are available.
  • Within each arm, num and den are non-empty and have equal length.
  • No p-value is required.

What a Strong Answer Covers Guidance

  • The pooled ratio per arm as a sum over a sum, and why the user is the unit for the variance
  • The delta-method variance with the covariance term, the right entries of np.cov and ddof=1
  • Combining the two arm variances for the lift and building a two-sided interval from alpha
  • The seven keys exactly as named, with the example values reproduced
  • Inputs that break the readout: an arm with one user, a zero denominator sum, arms too small for the normal approximation

Follow-up Questions Guidance

  • Why does the delta method beat a naive per-user-rate t-test here?
  • How would you extend this when randomization is at the user level but the metric unit is finer (clustering)?
  • How would you report the lift as a relative change, and what is its standard error?
  • When would you use a bootstrap instead of the delta method?