Compute the delta-method variance of a ratio metric per experiment variant in pandas

Quick Overview

Compute the delta-method variance of a ratio-of-sums metric for each variant of an A/B test from a per-user pandas DataFrame. It tests the variance formula, the covariance between numerator and denominator, consistent degrees of freedom and using the user as the analysis unit.

Compute the delta-method variance of a ratio metric per experiment variant in pandas

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

An A/B test randomizes users and tracks a ratio metric, such as clicks per page view, computed as a ratio of sums over each variant's users. Implement `delta_method_ratio_variance(df)` in `solution.py`. The frame has one row per user with `variant` (object, `'control'` or `'treatment'`), `num` (float64) and `den` (float64). Return the delta-method variance of `R = sum(num)/sum(den)` for each variant: ```python Var(R) = (1 / (n * mu_den**2)) * ( var_num + (mu_num/mu_den)**2 * var_den - 2*(mu_num/mu_den)*cov ) ``` Requirements: - For each variant group of `n` users: `mu_num = mean(num)`, `mu_den = mean(den)`. - `var_num = np.var(num, ddof=1)`, `var_den = np.var(den, ddof=1)`, `cov = np.cov(num, den, ddof=1)[0, 1]`. - Return `{variant_name: variance}` with `str` keys and `float` values, one entry per variant present. Example: ```python df = pd.DataFrame({ "variant": ["control", "treatment", "control", "treatment", "control", "treatment"], "num": [1.0, 1.0, 1.0, 6.0, 4.0, 5.0], "den": [2.0, 3.0, 4.0, 4.0, 6.0, 5.0], }) delta_method_ratio_variance(df) # {'control': 0.020833333333333332, 'treatment': 0.08333333333333333} ``` Control has `num = [1, 1, 4]` and `den = [2, 4, 6]`, so `R = 6/12 = 0.5` and the variance works out to exactly 1/48. Treatment has `num = [1, 6, 5]` and `den = [3, 4, 5]`, so `R = 1` and the variance is 1/12. ```hint Stay inside the group Every quantity in the formula, including `n`, belongs to one variant's users. Check that nothing is computed over the whole frame. ``` ```hint Library defaults `np.var` and `np.cov` do not use the same default degrees of freedom. Look up both before you rely on either default. ``` ### Constraints & Assumptions - One row per user, and `den > 0` for every row. - `pandas` and `numpy` are available. Hypothesis testing is out of scope. ### What a Strong Answer Covers - Computing every statistic within each variant group, with `n` equal to that group's user count - Using `ddof=1` for both variances and the covariance, and knowing where library defaults differ - The covariance cross term and why the user, as the randomization unit, is the unit for the variance - The output shape: plain `str` keys, `float` values, one entry per variant present - Inputs that break the formula, such as a variant with a single user ### Follow-up Questions - Why is the naive per-user-ratio variance wrong here? - How would you extend this to clustered randomization (for example, by session or account)? - How would you turn these two variances into a confidence interval for the difference between variants? - How would you compute the same variance in one SQL query over a users table?

Overview: Compute the delta-method variance of a ratio-of-sums metric for each variant of an A/B test from a per-user pandas DataFrame. It tests the variance formula, the covariance between numerator and denominator, consistent degrees of freedom and using the user as the analysis unit.

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

An A/B test randomizes users and tracks a ratio metric, such as clicks per page view, computed as a ratio of sums over each variant's users. Implement delta_method_ratio_variance(df) in solution.py. The frame has one row per user with variant (object, 'control' or 'treatment'), num (float64) and den (float64). Return the delta-method variance of R = sum(num)/sum(den) for each variant:

Var(R) = (1 / (n * mu_den**2)) * (
    var_num
    + (mu_num/mu_den)**2 * var_den
    - 2*(mu_num/mu_den)*cov
)

Requirements:

  • For each variant group of n users: mu_num = mean(num) , mu_den = mean(den) .
  • var_num = np.var(num, ddof=1) , var_den = np.var(den, ddof=1) , cov = np.cov(num, den, ddof=1)[0, 1] .
  • Return {variant_name: variance} with str keys and float values, one entry per variant present.

Example:

df = pd.DataFrame({
    "variant": ["control", "treatment", "control", "treatment", "control", "treatment"],
    "num":     [1.0, 1.0, 1.0, 6.0, 4.0, 5.0],
    "den":     [2.0, 3.0, 4.0, 4.0, 6.0, 5.0],
})

delta_method_ratio_variance(df)
# {'control': 0.020833333333333332, 'treatment': 0.08333333333333333}

Control has num = [1, 1, 4] and den = [2, 4, 6], so R = 6/12 = 0.5 and the variance works out to exactly 1/48. Treatment has num = [1, 6, 5] and den = [3, 4, 5], so R = 1 and the variance is 1/12.

Constraints & Assumptions

  • One row per user, and den > 0 for every row.
  • pandas and numpy are available. Hypothesis testing is out of scope.

What a Strong Answer Covers Guidance

  • Computing every statistic within each variant group, with n equal to that group's user count
  • Using ddof=1 for both variances and the covariance, and knowing where library defaults differ
  • The covariance cross term and why the user, as the randomization unit, is the unit for the variance
  • The output shape: plain str keys, float values, one entry per variant present
  • Inputs that break the formula, such as a variant with a single user

Follow-up Questions Guidance

  • Why is the naive per-user-ratio variance wrong here?
  • How would you extend this to clustered randomization (for example, by session or account)?
  • How would you turn these two variances into a confidence interval for the difference between variants?
  • How would you compute the same variance in one SQL query over a users table?