Delta-Method Variance of a Ratio of Two Sample Means
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Apply a first-order Taylor expansion to get the approximate variance of a ratio of two correlated sample means, including the covariance term.
An experiment dashboard reports a ratio metric: the average of a numerator quantity (such as revenue per bucket) divided by the average of a denominator quantity (such as active users per bucket). Both averages are taken over the same n independent buckets, and the two per-bucket quantities are correlated.
Write a function that returns the first-order (delta-method) approximation of the variance of this ratio of sample means.
Inputs
num_mean: the mean of the per-bucket numerator quantity.den_mean: the mean of the per-bucket denominator quantity.num_var: the per-bucket variance of the numerator quantity.den_var: the per-bucket variance of the denominator quantity.corr: the correlation between the numerator and denominator quantities within a bucket.n: the number of buckets.
Task
Let the ratio be R = (sample mean of numerator) / (sample mean of denominator). Linearize g(x, y) = x / y around (num_mean, den_mean), using the gradient evaluated there.
-
Compute the per-bucket covariance as
corr * sqrt(num_var * den_var). -
Compute the per-bucket variance of the linearized ratio:
num_var / den_mean^2 + num_mean^2 * den_var / den_mean^4 - 2 * num_mean * covariance / den_mean^3 -
Because the means are taken over
nindependent buckets, divide this byn.
Return the result as a float. Do not round it. The covariance term is subtracted, as the formula above shows.
Constraints
den_mean != 0num_var >= 0andden_var >= 0-1 <= corr <= 1n >= 1(integer)- The inputs are plain numbers. No input validation is required.
Examples
Example 1
Input: approximate_ratio_variance(100, 20, 400, 4, 0.6, 100)
Output: 0.006500000000000001
Example 2
Input: approximate_ratio_variance(250, 50, 900, 9, 0.6, 500)
Output: 0.00046799999999999994
Example 3
Input: approximate_ratio_variance(80, 10, 64, 1, 0, 100)
Output: 0.0128
Hints
Hint 1
The gradient of x/y at (mx, my) is (1/my, -mx/my^2). The variance of the linearized ratio is a quadratic form: gradient transposed times the covariance matrix times gradient.
Hint 2
Expand the quadratic form into three terms: var_x/my^2, mx^2*var_y/my^4, and a cross term with a minus sign, -2*mx*cov/my^3. Get the covariance from corr * sqrt(var_x * var_y).
Hint 3
These terms describe a single bucket. The statistic is a mean over n independent buckets, so divide the whole expression by n at the end.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The ratio R = X̄ / Ȳ is a nonlinear function of two sample means. A first-order Taylor expansion around the true means (mx, my) gives
R ≈ mx/my + (1/my)(X̄ - mx) - (mx/my²)(Ȳ - my)
This is a linear combination of X̄ and Ȳ, so its variance follows from the usual rule for linear combinations:
Var(R) ≈ (1/my²)·Var(X̄) + (mx²/my⁴)·Var(Ȳ) - 2·(mx/my³)·Cov(X̄, Ȳ)
For a mean over n independent buckets, Var(X̄) = num_var / n, Var(Ȳ) = den_var / n, and Cov(X̄, Ȳ) = cov / n, where cov = corr * sqrt(num_var * den_var). Every term therefore carries a factor 1/n, which we pull out and apply at the end.
Why it is correct
- The gradient of
x/yis(1/y, -x/y²). Evaluated at the means, it gives the coefficients above. - The cross term has a minus sign because the two gradient components have opposite signs. When the numerator and denominator are positively correlated, the variance of the ratio shrinks relative to the independent case.
- With
corr = 0the cross term vanishes, which is the third sample case:64/100 + 6400/10000 = 1.28, and dividing by 100 gives0.0128.
Complexity
Time O(1), space O(1).
Reference solution
import math
def approximate_ratio_variance(num_mean, den_mean, num_var, den_var, corr, n):
# Per-bucket covariance from the correlation and the two variances.
covariance = corr * math.sqrt(num_var * den_var)
# Gradient of x/y at the means is (1/my, -mx/my^2); take the quadratic form.
per_bucket = (
num_var / (den_mean ** 2)
+ (num_mean ** 2) * den_var / (den_mean ** 4)
- 2.0 * num_mean * covariance / (den_mean ** 3)
)
# Means over n independent buckets shrink the variance by a factor of n.
return per_bucket / n