Sample Size for a Ratio Metric Using the Delta Method

Quick Overview

Implement a per-group sample size calculation for a ratio metric such as clicks per impression, using delta-method variance and a relative minimum detectable effect. It tests how you derive the variance of a ratio of correlated per-user sums and turn it into a two-sided power calculation.

Sample Size for a Ratio Metric Using the Delta Method

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `ratio_metric_sample_size(...)` in `solution.py`. Given per-user moments of the numerator `X` and denominator `Y` of a ratio metric (for example clicks per impression) and a relative minimum detectable effect (MDE), return the required per-group sample size for a two-sided test using delta-method variance. Defaults are `alpha=0.05` and `power=0.8`. ```python def ratio_metric_sample_size(mean_num: float, mean_den: float, var_num: float, var_den: float, cov_num_den: float, relative_mde: float, alpha: float = 0.05, power: float = 0.8) -> int: ... ``` The function must follow these rules: - `R = mean_num / mean_den` - `var_R = (var_num - 2*R*cov_num_den + R**2 * var_den) / mean_den**2` exactly. Do not drop the covariance term or the `R**2 * var_den` term. - `delta = R * relative_mde`, because the MDE is relative. - `n = (norm.ppf(1-alpha/2) + norm.ppf(power))**2 * 2 * var_R / delta**2`. The factor `2` sums the variances of both arms under equal allocation. - Return `math.ceil(n)` as a Python `int`. The result is per group, not the total. - Use `scipy.stats.norm` for the quantiles and `math.ceil` for rounding. Example: ```text ratio_metric_sample_size(2.0, 10.0, 6.0, 25.0, 8.0, 0.02) -> 37283 ``` ```hint Units of the effect The MDE is relative to the baseline ratio. Turn it into an absolute difference in R before it goes into the denominator of the formula. ``` ```hint Per group or total Check what the factor 2 already accounts for before deciding whether the result needs to be doubled or halved. ``` ### Constraints & Assumptions - The analysis unit is the user. `mean_num`, `var_num`, `mean_den`, `var_den` and `cov_num_den` are moments across users of each user's numerator and denominator totals. - Both arms have the same `var_R` and the same size (50/50 allocation). - `mean_den` is nonzero, `relative_mde` is nonzero, and `alpha` and `power` lie strictly between 0 and 1. ### What a Strong Answer Covers - Computing the ratio and its delta-method variance with both the covariance term and the `R**2 * var_den` term kept - Converting the relative MDE into an absolute effect on R - Using the two-sided quantile for `alpha`, the quantile for `power`, and the factor 2 for equal allocation - Rounding up to a per-group integer and returning a Python `int` - Inputs that break the formula, such as a zero denominator mean, a zero baseline ratio or a zero MDE ### Follow-up Questions - Why is the covariance term required, and in which direction is n biased if you drop it when `X` and `Y` are positively correlated? - How would you extend this to unequal allocation between arms? - When does the first-order delta-method approximation become unreliable, and what would you use instead?

Overview: Implement a per-group sample size calculation for a ratio metric such as clicks per impression, using delta-method variance and a relative minimum detectable effect. It tests how you derive the variance of a ratio of correlated per-user sums and turn it into a two-sided power calculation.

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

Implement ratio_metric_sample_size(...) in solution.py. Given per-user moments of the numerator X and denominator Y of a ratio metric (for example clicks per impression) and a relative minimum detectable effect (MDE), return the required per-group sample size for a two-sided test using delta-method variance. Defaults are alpha=0.05 and power=0.8.

def ratio_metric_sample_size(mean_num: float, mean_den: float, var_num: float,
                             var_den: float, cov_num_den: float,
                             relative_mde: float, alpha: float = 0.05,
                             power: float = 0.8) -> int:
    ...

The function must follow these rules:

  • R = mean_num / mean_den
  • var_R = (var_num - 2*R*cov_num_den + R**2 * var_den) / mean_den**2 exactly. Do not drop the covariance term or the R**2 * var_den term.
  • delta = R * relative_mde , because the MDE is relative.
  • n = (norm.ppf(1-alpha/2) + norm.ppf(power))**2 * 2 * var_R / delta**2 . The factor 2 sums the variances of both arms under equal allocation.
  • Return math.ceil(n) as a Python int . The result is per group, not the total.
  • Use scipy.stats.norm for the quantiles and math.ceil for rounding.

Example:

ratio_metric_sample_size(2.0, 10.0, 6.0, 25.0, 8.0, 0.02) -> 37283

Constraints & Assumptions

  • The analysis unit is the user. mean_num , var_num , mean_den , var_den and cov_num_den are moments across users of each user's numerator and denominator totals.
  • Both arms have the same var_R and the same size (50/50 allocation).
  • mean_den is nonzero, relative_mde is nonzero, and alpha and power lie strictly between 0 and 1.

What a Strong Answer Covers Guidance

  • Computing the ratio and its delta-method variance with both the covariance term and the R**2 * var_den term kept
  • Converting the relative MDE into an absolute effect on R
  • Using the two-sided quantile for alpha , the quantile for power , and the factor 2 for equal allocation
  • Rounding up to a per-group integer and returning a Python int
  • Inputs that break the formula, such as a zero denominator mean, a zero baseline ratio or a zero MDE

Follow-up Questions Guidance

  • Why is the covariance term required, and in which direction is n biased if you drop it when X and Y are positively correlated?
  • How would you extend this to unequal allocation between arms?
  • When does the first-order delta-method approximation become unreliable, and what would you use instead?