Decompose prediction variance into data and seed parts with the law of total variance

Quick Overview

Implement a NumPy function that splits the variance of model predictions across training datasets and random seeds into a data part and a seed part using the law of total variance. It tests axis handling, population variance, input validation, and how the split guides choices such as bagging versus seed ensembling.

Decompose prediction variance into data and seed parts with the law of total variance

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

You trained the same model several times on each of several training datasets, changing only the random seed. Predictions on a fixed test set are stored in `preds` with shape `(n_datasets, n_seeds, n_test)`. Implement `decompose_prediction_variance(preds)` using the law of total variance: - the seed part is $E_D[\mathrm{Var}_S(\text{pred} \mid D)]$, - the data part is $\mathrm{Var}_D(E_S[\text{pred} \mid D])$, - the total is the variance over all (dataset, seed) pairs. Return a dict with keys `total_per_point`, `data_per_point`, `seed_per_point` (arrays of shape `(n_test,)`) and `total`, `data`, `seed` (Python floats: the per-point means over test points). Also implement `variance_shares(preds)`, returning `(data_share, seed_share)` as fractions of the scalar total. Example with shape `(2, 2, 1)`, so one test point: ```python preds[:, :, 0] = [[1, 3], [5, 7]] # dataset 0: seeds give 1 and 3; dataset 1: seeds give 5 and 7 decompose_prediction_variance(preds) # seed = 1.0, data = 4.0, total = 5.0 variance_shares(preds) # (0.8, 0.2) ``` ```hint Map the formula to axes Write the law of total variance with the dataset as the conditioning variable, then decide which array axis each inner and each outer operation runs over. ``` ```hint Test with the identity With `ddof=0` and the same number of seeds for every dataset, the data and seed parts should add up to the total exactly. Assert that on random input. ``` ### Constraints & Assumptions - All variances use `ddof=0`. - Input that is not 3-D, or that has any dimension of size 0, raises `ValueError`. - `variance_shares` returns `(0.0, 0.0)` when the total is at most `1e-15`. - The input is a rectangular numeric array, so every dataset has the same number of seeds. ### What a Strong Answer Covers - Which array axis each term of the law of total variance reduces over, and in what order. - The per-point arrays and their scalar means, returned with the required keys and types. - Input validation (dimensionality, empty dimensions) and the near-zero total in `variance_shares`. - Why data plus seed equals the total exactly in this setting, and a test that checks it. - Time and space cost of the vectorized computation. ### Follow-up Questions - With only a few seeds, why is the seed term biased, and how would you correct it? - How would you use this decomposition to decide between bagging and seed ensembling? - What changes if datasets have different numbers of seeds?

Overview: Implement a NumPy function that splits the variance of model predictions across training datasets and random seeds into a data part and a seed part using the law of total variance. It tests axis handling, population variance, input validation, and how the split guides choices such as bagging versus seed ensembling.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

You trained the same model several times on each of several training datasets, changing only the random seed. Predictions on a fixed test set are stored in preds with shape (n_datasets, n_seeds, n_test).

Implement decompose_prediction_variance(preds) using the law of total variance:

  • the seed part is ED[VarS(pred∣D)]E_D[\mathrm{Var}_S(\text{pred} \mid D)] ,
  • the data part is VarD(ES[pred∣D])\mathrm{Var}_D(E_S[\text{pred} \mid D]) ,
  • the total is the variance over all (dataset, seed) pairs.

Return a dict with keys total_per_point, data_per_point, seed_per_point (arrays of shape (n_test,)) and total, data, seed (Python floats: the per-point means over test points).

Also implement variance_shares(preds), returning (data_share, seed_share) as fractions of the scalar total.

Example with shape (2, 2, 1), so one test point:

preds[:, :, 0] = [[1, 3], [5, 7]]   # dataset 0: seeds give 1 and 3; dataset 1: seeds give 5 and 7
decompose_prediction_variance(preds)
# seed = 1.0, data = 4.0, total = 5.0
variance_shares(preds)  # (0.8, 0.2)

Constraints & Assumptions

  • All variances use ddof=0 .
  • Input that is not 3-D, or that has any dimension of size 0, raises ValueError .
  • variance_shares returns (0.0, 0.0) when the total is at most 1e-15 .
  • The input is a rectangular numeric array, so every dataset has the same number of seeds.

What a Strong Answer Covers Guidance

  • Which array axis each term of the law of total variance reduces over, and in what order.
  • The per-point arrays and their scalar means, returned with the required keys and types.
  • Input validation (dimensionality, empty dimensions) and the near-zero total in variance_shares .
  • Why data plus seed equals the total exactly in this setting, and a test that checks it.
  • Time and space cost of the vectorized computation.

Follow-up Questions Guidance

  • With only a few seeds, why is the seed term biased, and how would you correct it?
  • How would you use this decomposition to decide between bagging and seed ensembling?
  • What changes if datasets have different numbers of seeds?