#151Delta Method for Ratio Metric VarianceHard

Delta Method for Ratio Metric Variance

Background

Ratio metrics (CTR, sessions-per-user, GMV-per-visit) don't have an obvious tt-test: you can't just test the numerator's mean. The delta method linearises the ratio around the sample means via a first-order Taylor expansion, giving a usable variance estimate. The standard tool in production experimentation platforms for variance on ratio KPIs.

Problem statement

Implement delta_method_variance(numerator, denominator):

Var ⁣(XˉYˉ)    1n ⁣(σX2Yˉ22XˉCov(X,Y)Yˉ3+Xˉ2σY2Yˉ4)\mathrm{Var}\!\Bigl(\tfrac{\bar X}{\bar Y}\Bigr) \;\approx\; \tfrac{1}{n}\!\left(\tfrac{\sigma_X^2}{\bar Y^2} - 2\,\tfrac{\bar X\,\mathrm{Cov}(X,Y)}{\bar Y^3} + \tfrac{\bar X^2\,\sigma_Y^2}{\bar Y^4}\right)

Use sample variances and sample covariance (Bessel-corrected).

Input

  • numerator — 1-D np.ndarray of floats, length nn.
  • denominator — 1-D np.ndarray of floats, length nn, same length as numerator.

Output

Returns a Python float >= 0, the estimated variance of the ratio.

Examples

Example 1 — constant denominator collapses to Var(X) / n

Input:  numerator = random N(0, 1), denominator = ones
Output: ≈ Var(X) / n

Example 2 — both noisy → positive variance

Input:  both N(10, 2) and N(20, 3)
Output: > 0

Example 3 — length mismatch

Input:  shape (3,) and (4,)
Output: ValueError

Constraints

  • numerator and denominator must have the same length, 2\ge 2.
  • Use n1n - 1 Bessel correction for variance and covariance.
  • Returns a Python float.

Notes

  • Sensitivity to small Yˉ\bar Y. If Yˉ\bar Y is near zero, the ratio's variance blows up. Production code adds a "clip Yˉ\bar Y" or a guard before reporting.
  • Pair with CUPED. Delta method on the ratio of CUPED-adjusted metrics is the standard ratio-A/B test at scale.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • reference: constant denominator reduces to Var(X)/n
  • example: hand-checked ratio on a tiny fixed dataset equals 1/12
  • sample: two noisy streams give a positive variance