Bias, Variance and MSE of Variance Estimators
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
A sample variance can divide the sum of squared deviations by $n-1$, $n$ or $n+1$. Write code that measures how these three estimators compare on bias, variance and mean squared error.
Implement `compare_variance_estimators(samples, true_variance)`. `samples` has shape `(n_trials, n)`, and each row is one dataset. For each row, compute `SS = sum((x - row_mean)**2)` and the estimates `SS/(n-1)`, `SS/n` and `SS/(n+1)`, in that order. Return a dict with keys `"denominator"`, `"bias"`, `"variance"` and `"mse"`, each a length-3 array with one entry per estimator.
Also implement `simulate_variance_estimators(n, n_trials, sigma, seed)`, which draws Gaussian samples and runs the comparison against the true variance `sigma**2`.
```hint Compute once, divide three times
A row's sum of squared deviations does not depend on the denominator. Compute it once per row, then divide by all three denominators with broadcasting.
```
```hint A built-in check
With the `ddof=0` variance, the three summary arrays satisfy an exact identity. Use it to test your code on the example.
```
### Example
```text
samples = [[1, 3], [0, 0], [2, 6]]
true_variance = 2.0
denominator = [1, 2, 3]
bias = [4/3, -1/3, -8/9]
variance = [104/9, 26/9, 104/81]
mse = [40/3, 3, 56/27]
```
Here $n = 2$, so the denominators are 1, 2 and 3, and the row sums of squares are 2, 0 and 8. The function returns these values as floats.
### Constraints & Assumptions
- `bias = mean(est) - true_variance`, taken across trials for each estimator.
- `variance` is the spread of each estimator across trials, computed with `ddof=0`.
- `mse = mean((est - true_variance)**2)`.
- Raise `ValueError` if `samples` is not 2-D, if `n < 2`, or if `n_trials < 1`.
- The simulation creates `rng = np.random.default_rng(seed)`, makes exactly one call `rng.normal(0, sigma, size=(n_trials, n))`, and uses `sigma**2` as the true variance.
### What a Strong Answer Covers
- A vectorized computation of the per-row sum of squares and of the estimates for all three denominators, in the required order.
- Bias, `ddof=0` variance and MSE computed across trials, checked against the example.
- Input validation that raises `ValueError` for each of the three invalid cases.
- A reproducible simulation built on one seeded `default_rng` draw and the true variance `sigma**2`.
- How bias, variance and MSE relate, which estimator has the lowest MSE for Gaussian data, and why.
### Follow-up Questions
- For Gaussian data, derive the bias, variance and MSE of `SS/c` as a function of `c`. Which `c` minimizes the MSE?
- How does the MSE-optimal denominator move for heavy-tailed data, such as a t-distribution with few degrees of freedom?
- If `SS/(n+1)` has the lowest MSE, why do R's `var` and pandas' `.var()` default to `n-1`?
- How many trials does the simulation need before the estimated biases are precise enough to rank the estimators, and how would you report the Monte Carlo error?
Overview: Implement NumPy code that compares the sample variance estimators with denominators n-1, n and n+1 by their bias, variance and mean squared error across many trials, plus a seeded Gaussian simulation. It tests vectorized array code and how bias and variance combine into the error of an estimator.