Compute the delta-method standard error of a ratio metric in Python
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `delta_method_se_ratio(numerator, denominator)` in `solution.py`. The inputs are equal-length per-user arrays of length n, for example clicks and impressions per user in one experiment arm. Return the delta-method standard error of the ratio `R = numerator.sum() / denominator.sum()` as an unrounded `float`.
Compute it from these quantities:
- `xbar = denominator.mean()`.
- `cov = np.cov(numerator, denominator, ddof=1)` with `var_y = cov[0,0]`, `var_x = cov[1,1]`, `cov_yx = cov[0,1]`.
- `Var(R) = (1 / (n * xbar**2)) * (var_y + R**2 * var_x - 2 * R * cov_yx)`, and the function returns `sqrt(Var(R))`.
Example:
```python
numerator = [3, 0, 2, 5, 1, 0, 4, 2, 1, 3]
denominator = [10, 8, 12, 20, 5, 7, 15, 9, 6, 11]
# R = 0.20388349514563106
delta_method_se_ratio(numerator, denominator) # -> 0.029321652409662752
```
```hint Matrix layout
`np.cov(a, b)` returns a 2x2 matrix whose row order follows the argument order. Check which diagonal cell belongs to the numerator before you name the variables.
```
```hint Degrees of freedom
Every variance and covariance has to use the same convention. `np.var` defaults to `ddof=0`, so mixing it with `np.cov` changes the result.
```
### Constraints & Assumptions
- `numerator` and `denominator` have the same length n, and n is at least 2 so a sample covariance exists.
- The inputs may be Python lists or NumPy arrays of non-negative numbers.
- The denominator total is positive, so `R` and `xbar` are defined.
- The function returns the raw float with no rounding.
### What a Strong Answer Covers
- Computing `R` as a ratio of sums and `xbar` as the denominator mean, then scaling by `n * xbar**2`.
- Reading `var_y`, `var_x` and `cov_yx` from the correct cells of a `ddof=1` covariance matrix.
- Keeping the `-2 * R * cov_yx` term and explaining what it accounts for.
- Handling fewer than two users, a zero denominator total, and list input.
### Follow-up Questions
- Why is the `-2 * R * cov_yx` term required?
- How does this relate to a two-sample test comparing ratios across variants?
- Why is the binomial formula over all impressions the wrong variance for clicks per impression when users are the randomization unit?
- When does the delta-method approximation become unreliable, and what would you use instead?
Overview: Implement a Python function that returns the delta-method standard error of a ratio metric, such as clicks per impression, from per-user numerator and denominator arrays. It tests sample covariance handling, the delta method, and how user-level randomization changes the variance of a per-event metric.