Simulate bagged polynomial fits and decompose the ensemble variance by member correlation

Quick Overview

Simulate bagged polynomial regressors on noisy sine data and split the ensemble's variance into average member variance and average pairwise covariance across training draws. It tests reproducible random simulation, covariance computation in NumPy and the variance formula for an average of correlated predictors.

Simulate bagged polynomial fits and decompose the ensemble variance by member correlation

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement two functions. `simulate_bagged_predictions(n_draws, n_estimators, n_train, x_test, noise_sd, degree, seed)` fits bagged polynomial regressors on `y = sin(pi*x) + noise` and returns predictions of shape `(n_draws, n_estimators, len(x_test))`. `decompose_ensemble_variance(preds)` takes `preds` of shape `(R, M, T)` and returns a dict of length-`T` arrays: `member_variance`, `mean_pairwise_covariance`, `correlation`, `ensemble_variance` and `predicted_ensemble_variance` (`rho*sigma^2 + (1-rho)*sigma^2/M`). Example: ```python preds = np.array([[[1.0], [3.0]], [[3.0], [1.0]]]) # R=2, M=2, T=1 decompose_ensemble_variance(preds) # member_variance=[1.0], mean_pairwise_covariance=[-1.0], correlation=[-1.0] # ensemble_variance=[0.0], predicted_ensemble_variance=[0.0] ``` ```hint One matrix per test point At each test point, the `R` by `M` block of predictions gives an `M` by `M` covariance matrix across draws. Every value in the returned dict can be read off that matrix or off the member mean. ``` ```hint Test on cases you can predict Before trusting the decomposition, feed it inputs whose answer you know: identical members, members that are independent across draws, and the example above. ``` ### Constraints & Assumptions - Draw order with `rng = np.random.default_rng(seed)`: for each draw, `x = rng.uniform(-1, 1, n_train)`, then `rng.normal(0, noise_sd, n_train)`, then for each member `rng.integers(0, n_train, n_train)` bootstrap indices. - Each member is fit by least squares with `np.linalg.lstsq(..., rcond=None)` on `np.vander(x, degree+1, increasing=True)`. - All variances and covariances are taken across the `R` draws with `ddof=0`. The covariance is averaged over ordered pairs `m != n`, and `ensemble_variance` is the variance of the member mean. - `correlation = mean_pairwise_covariance / member_variance`, set to `1.0` when `M == 1` (covariance `0`) or when the member variance is `0`. - If `preds` is not 3-D, raise `ValueError`. ### What a Strong Answer Covers - A simulation that follows the pinned draw order and fits each bootstrap member by least squares on the increasing Vandermonde matrix. - Variances and covariances across draws with `ddof=0`, computed for all member pairs and test points without Python loops over them. - The mean pairwise covariance over ordered pairs, the correlation with its two special cases, and the variance of the member mean. - Why the predicted ensemble variance equals the empirical one at every test point. - Input validation, complexity, and what the decomposition says about bagging. ### Follow-up Questions - Why does the formula match the empirical ensemble variance exactly here, even with unequal member variances? - What does the ensemble variance approach as `n_estimators` grows, and what limits the benefit of adding members? - Bagged members in one draw share the same training set. How does that affect their correlation, and when does the bagged mean beat a single fit on the full training set? - How would you extend the simulation to report bias and noise as well, so the parts add up to the expected squared error?

Overview: Simulate bagged polynomial regressors on noisy sine data and split the ensemble's variance into average member variance and average pairwise covariance across training draws. It tests reproducible random simulation, covariance computation in NumPy and the variance formula for an average of correlated predictors.

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

Implement two functions.

simulate_bagged_predictions(n_draws, n_estimators, n_train, x_test, noise_sd, degree, seed) fits bagged polynomial regressors on y = sin(pi*x) + noise and returns predictions of shape (n_draws, n_estimators, len(x_test)).

decompose_ensemble_variance(preds) takes preds of shape (R, M, T) and returns a dict of length-T arrays: member_variance, mean_pairwise_covariance, correlation, ensemble_variance and predicted_ensemble_variance (rho*sigma^2 + (1-rho)*sigma^2/M).

Example:

preds = np.array([[[1.0], [3.0]], [[3.0], [1.0]]])  # R=2, M=2, T=1
decompose_ensemble_variance(preds)
# member_variance=[1.0], mean_pairwise_covariance=[-1.0], correlation=[-1.0]
# ensemble_variance=[0.0], predicted_ensemble_variance=[0.0]

Constraints & Assumptions

  • Draw order with rng = np.random.default_rng(seed) : for each draw, x = rng.uniform(-1, 1, n_train) , then rng.normal(0, noise_sd, n_train) , then for each member rng.integers(0, n_train, n_train) bootstrap indices.
  • Each member is fit by least squares with np.linalg.lstsq(..., rcond=None) on np.vander(x, degree+1, increasing=True) .
  • All variances and covariances are taken across the R draws with ddof=0 . The covariance is averaged over ordered pairs m != n , and ensemble_variance is the variance of the member mean.
  • correlation = mean_pairwise_covariance / member_variance , set to 1.0 when M == 1 (covariance 0 ) or when the member variance is 0 .
  • If preds is not 3-D, raise ValueError .

What a Strong Answer Covers Guidance

  • A simulation that follows the pinned draw order and fits each bootstrap member by least squares on the increasing Vandermonde matrix.
  • Variances and covariances across draws with ddof=0 , computed for all member pairs and test points without Python loops over them.
  • The mean pairwise covariance over ordered pairs, the correlation with its two special cases, and the variance of the member mean.
  • Why the predicted ensemble variance equals the empirical one at every test point.
  • Input validation, complexity, and what the decomposition says about bagging.

Follow-up Questions Guidance

  • Why does the formula match the empirical ensemble variance exactly here, even with unequal member variances?
  • What does the ensemble variance approach as n_estimators grows, and what limits the benefit of adding members?
  • Bagged members in one draw share the same training set. How does that affect their correlation, and when does the bagged mean beat a single fit on the full training set?
  • How would you extend the simulation to report bias and noise as well, so the parts add up to the expected squared error?