Dictionary of Applied Machine Learning · bagging
Updated on 2026-11-03
Numerical companion to the entry bagging: it recomputes what the entry states and prints one line per check
Backs the entry's claims with numbers: a decision tree grown until it reproduces every label changes its predictions noticeably when a handful of training points are replaced, while a straight line fitted to the same points barely does; bagging reduces the variance of the tree's predictions while changing its squared bias by far less; it does little for the straight line; the variance of an average of M bagged trees follows rho sigma^2 + (1 - rho) sigma^2 / M on training sets it was not fitted to; and two resamples of one training set share about 40% of its data points, which is where rho comes from. Self-contained (numpy/matplotlib only), fixed seed.
Run it without installing anything:uv run https://dictionaryofml.org/terms/bagging.py
uv downloads this script and the pinned NumPy and Matplotlib it needs, then runs it; the script fetches any input file it uses. To keep the output files, download bagging.py into a folder and run uv run bagging.py there. With NumPy and Matplotlib already installed, python3 bagging.py, from any directory — it writes its output files into the current directory. Fixed seeds, so the printed numbers reproduce exactly. Download bagging.py · Notebook · Open in Colab
Before running a downloaded copy, scan it: uvx bandit -q -t B102,B307,B602,B605 bagging.py (B102,B307,B602,B605 are the exec, eval and shell checks of bandit) reports nothing: the script runs no shell command and evaluates no generated code.
One cell per block of the script: the code, and what that code printed when it last ran here
"""
bagging.py — numerical companion to the glossary entry 'bagging'.
Purpose
-------
Backs the entry's claims with numbers: a decision tree grown until it
reproduces every label changes its predictions noticeably when a handful
of training points are replaced, while a straight line fitted to the same points barely
does; bagging reduces the variance of the tree's predictions while
changing its squared bias by far less; it does little for the straight
line; the variance of an average of M bagged trees follows
rho sigma^2 + (1 - rho) sigma^2 / M on training sets it was not fitted
to; and two resamples of one training set share about 40% of its data
points, which is where rho comes from. Self-contained (numpy/matplotlib only),
fixed seed.
Setup
-----
Regression with one feature x in [0, 1] and label
y = sin(2 pi x) + 0.3 x + noise (noise variance 0.09). A training set
holds 50 points. Base learners: a decision tree grown until every
leaf holds a single training point (so it reproduces every label) and a
straight line fitted to the training points. Bagging draws M bootstrap resamples of
the training set, trains one base learner on each, and averages their
predictions. Variance and bias are measured on a grid of 200 feature
values over 100 independent training sets.
Blocks
------
[B-krems] 24 days of 2020 at the GeoSphere station Krems an der Donau,
12 from winter and 12 from summer: feature = minimum
temperature of the day, label = maximum temperature of the
same day. Two depth-one decision trees, each trained on five
of the days chosen at random, split at different thresholds
and disagree by several degrees across the feature range,
which is the instability bagging averages away.
[B-unstable] Replacing 5 of the 50 training points, repeated 50 times,
changes the tree's predictions by more than five times as
much as the straight line's, measured by the largest change
over the grid (the tree's change is local but large, the
line's global but small).
[B-variance] Over 100 training sets, bagging with M = 25 lowers the
variance of the tree's predictions by a factor above 2
while its squared bias stays below 0.01 (as does the single
tree's);
for the straight line the variance drops by less than a
factor 1.3.
[B-nofit] The average loss of the bagged tree on its own training
set does not fall toward zero as M grows: at M = 100 it is
still above half of its value at M = 10, unlike for a
method that accumulates its base learners.
[B-overlap] Two resamples of the same training set are not disjoint: a
resample holds about 1 - 1/e ~ 63.2% of the distinct data
points and two of them share about (1 - 1/e)^2 ~ 40.0%. That
shared material is where the correlation rho comes from.
[B-rho] The variance of an average of M bagged trees matches
rho sigma^2 + (1 - rho) sigma^2 / M. sigma^2 and rho are
estimated on 400 training sets with two trees each; the
prediction is then tested on 150 INDEPENDENT training sets for
each M in {2, 3, 5, 10, 20}, so the check is a measurement and
not the algebraic identity between the two sample moments.
Outputs
-------
bagging_krems.csv : the 24 Krems days, minimum and maximum
temperature, and which tree's subset each is in.
bagging_kremstrees.csv : the two depth-one trees and their average, on a
grid of minimum temperatures.
bagging_rho.csv : M, the measured variance of the average of M
bagged trees, the variance the identity predicts,
and the relative error.
bagging_variance.csv : per base learner: variance and squared bias of
the single and of the bagged (M = 25) predictions,
averaged over the grid.
bagging_trainloss.csv : M and the average loss of the bagged tree on
the training set.
bagging.png : matplotlib preview of the entry's first and third
figures (checking only).
"""
# /// script
# requires-python = ">=3.10"
# dependencies = ["matplotlib==3.10.7", "numpy==2.3.4"]
# ///
import json
import urllib.request
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from pathlib import Path
OUT_DIR = Path(__file__).parent
report = []
def check(name, ok):
report.append((name, bool(ok)))
print(f" [{'ok' if ok else 'FAIL'}] {name}")
rng = np.random.default_rng(0)
m = 50
grid = np.linspace(0.0, 1.0, 200)
def truth(x):
return np.sin(2 * np.pi * x) + 0.3 * x
def draw_trainset(size=m):
x = np.sort(rng.random(size))
return x, truth(x) + 0.3 * rng.standard_normal(size)
# ------------------------------------------------------- base learners
def tree_fit(x, y):
"""Decision tree grown until every leaf holds one training point: a list
of (threshold, value) leaves, i.e. a piecewise-constant map."""
order = np.argsort(x); x, y = x[order], y[order]
cuts = []
def split(lo, hi): # indices lo..hi-1
if hi - lo <= 1:
return
best, pos = None, None
for k in range(lo + 1, hi):
if x[k] == x[k - 1]:
continue
l, r = y[lo:k], y[k:hi]
sse = ((l - l.mean()) ** 2).sum() + ((r - r.mean()) ** 2).sum()
if best is None or sse < best:
best, pos = sse, k
if pos is None:
return
cuts.append(0.5 * (x[pos] + x[pos - 1]))
split(lo, pos); split(pos, hi)
split(0, len(x))
cuts = np.sort(np.array(cuts))
bins = np.searchsorted(cuts, x, side="right")
values = np.array([y[bins == b].mean() for b in range(len(cuts) + 1)])
return lambda z: values[np.searchsorted(cuts, z, side="right")]
def linreg_fit(x, y):
w = np.polyfit(x, y, 1)
return lambda z: np.polyval(w, z)
def bagged(fit, x, y, n_learners):
hs = []
for _ in range(n_learners):
idx = rng.integers(0, len(x), len(x)) # bootstrap resample
hs.append(fit(x[idx], y[idx]))
return lambda z: np.mean([h(z) for h in hs], axis=0)
24 days of 2020 at the GeoSphere station Krems an der Donau, 12 from winter and 12 from summer: feature = minimum temperature of the day, label = maximum temperature of the same day. Two depth-one decision trees, each trained on five of the days chosen at random, split at different thresholds and disagree by several degrees across the feature range, which is the instability bagging averages away.
# Days of 2020 at the GeoSphere Austria station Krems an der Donau, from the
# winter and the summer months: the feature is the minimum temperature of the
# day, the label the maximum temperature of the same day. Two depth-one
# decision trees are each trained on five of those days chosen at random; the
# entry's opening figure draws each tree in the same style as the five days
# it was trained on, and shows how far apart the two trees fall.
URL = ("https://dataset.api.hub.geosphere.at/v1/station/historical/"
"klima-v2-1d?parameters=tlmin,tlmax&station_ids=3805"
"&start=2020-01-01&end=2020-12-31")
with urllib.request.urlopen(URL, timeout=120) as resp:
payload = json.load(resp)
cols = payload["features"][0]["properties"]["parameters"]
stamps = [t[:10] for t in payload["timestamps"]]
tmin = np.array(cols["tlmin"]["data"], dtype=float)
tmax = np.array(cols["tlmax"]["data"], dtype=float)
check("[B-krems] 366 days of 2020 downloaded, none missing",
len(tmin) == 366 and not np.isnan(tmin).any() and not np.isnan(tmax).any())
krng = np.random.default_rng(8)
month = np.array([int(d[5:7]) for d in stamps])
winter = np.flatnonzero(np.isin(month, (1, 2, 12)))
summer = np.flatnonzero(np.isin(month, (6, 7, 8)))
days = np.sort(np.concatenate([krng.choice(winter, 12, replace=False),
krng.choice(summer, 12, replace=False)]))
kx, ky = tmin[days], tmax[days]
print(f" 24 days of 2020 at Krems, 12 from winter and 12 from summer: min temp "
f"{kx.min():.1f} to {kx.max():.1f}, max temp {ky.min():.1f} to {ky.max():.1f} deg C")
def stump_fit(x, y):
"""A depth-one decision tree on the single feature: one threshold, and
the mean label on each side of it."""
o = np.argsort(x); xs, ys = x[o], y[o]
best, cut = None, None
for k in range(1, len(xs)):
if xs[k] == xs[k - 1]:
continue
l, r = ys[:k], ys[k:]
sse = ((l - l.mean()) ** 2).sum() + ((r - r.mean()) ** 2).sum()
if best is None or sse < best:
best, cut = sse, 0.5 * (xs[k] + xs[k - 1])
if cut is None: # every feature value identical
m0 = float(ys.mean())
return 0.0, m0, m0
return cut, float(ys[xs <= cut].mean()), float(ys[xs > cut].mean())
# two base learners, each trained on five of the 24 days chosen at random
# (ten distinct days, so every day belongs to at most one tree)
subset = np.zeros(len(days), dtype=int)
ten = krng.choice(len(days), 10, replace=False)
stumps = []
for b in (1, 2):
pick = ten[5 * (b - 1):5 * b]
subset[pick] = b
stumps.append(stump_fit(kx[pick], ky[pick]))
kcuts = [c for c, _, _ in stumps]
print(f" tree 1 splits at {kcuts[0]:.1f} deg C, tree 2 at {kcuts[1]:.1f} deg C")
kgrid = np.linspace(kx.min() - 1.0, kx.max() + 1.0, 200)
def stump_eval(s, z):
cut, lo, hi = s
return np.where(np.asarray(z, dtype=float) <= cut, lo, hi)
kpreds = np.array([stump_eval(s, kgrid) for s in stumps])
kspread = float(np.max(np.abs(kpreds[0] - kpreds[1])))
kdiff = float(np.mean(np.abs(kpreds[0] - kpreds[1]) > 1.0))
check(f"[B-krems] the two trees give predictions more than one degree apart "
f"on {kdiff*100:.0f}% of the feature range", kdiff > 0.5)
check(f"[B-krems] and disagree by up to {kspread:.1f} deg C", kspread > 1.0)
with open(OUT_DIR / "bagging_krems.csv", "w") as fh: # one column per style
fh.write("tmin,tmax,subset,tmax_tree1,tmax_tree2,tmax_other\n")
for a, b, c in zip(kx, ky, subset):
cells = [f"{b:g}" if c == k else "nan" for k in (1, 2, 0)]
fh.write(f"{a:g},{b:g},{c}," + ",".join(cells) + "\n")
with open(OUT_DIR / "bagging_kremstrees.csv", "w") as fh:
fh.write("tmin,tree1,tree2,mean\n")
for j, g in enumerate(kgrid):
fh.write(f"{g:g},{kpreds[0][j]:g},{kpreds[1][j]:g},{kpreds[:, j].mean():g}\n")
[ok] [B-krems] 366 days of 2020 downloaded, none missing
24 days of 2020 at Krems, 12 from winter and 12 from summer: min temp -1.4 to 17.7, max temp -0.1 to 31.7 deg C
tree 1 splits at 5.4 deg C, tree 2 at 9.2 deg C
[ok] [B-krems] the two trees give predictions more than one degree apart on 100% of the feature range
[ok] [B-krems] and disagree by up to 21.9 deg C
Replacing 5 of the 50 training points, repeated 50 times, changes the tree's predictions by more than five times as much as the straight line's, measured by the largest change over the grid (the tree's change is local but large, the line's global but small).
chg_tree, chg_lin = [], []
for _ in range(50):
x0, y0 = draw_trainset()
x1, y1 = x0.copy(), y0.copy()
swap = rng.choice(m, 5, replace=False) # a handful
x1[swap] = rng.random(5); y1[swap] = truth(x1[swap]) + 0.3 * rng.standard_normal(5)
chg_tree.append(np.abs(tree_fit(x0, y0)(grid) - tree_fit(x1, y1)(grid)).max())
chg_lin.append(np.abs(linreg_fit(x0, y0)(grid) - linreg_fit(x1, y1)(grid)).max())
chg_tree, chg_lin = float(np.mean(chg_tree)), float(np.mean(chg_lin))
check(f"[B-unstable] replacing 5 of 50 training points changes the tree's predictions "
f"by up to {chg_tree:.2f} (average over 50 repetitions), the straight line's "
f"by up to {chg_lin:.2f}", chg_tree > 5 * chg_lin)
[ok] [B-unstable] replacing 5 of 50 training points changes the tree's predictions by up to 0.86 (average over 50 repetitions), the straight line's by up to 0.09
Over 100 training sets, bagging with M = 25 lowers the variance of the tree's predictions by a factor above 2 while its squared bias stays below 0.01 (as does the single tree's); for the straight line the variance drops by less than a factor 1.3.
K, M_BAG = 100, 25
res = {}
for name, fit in (("tree", tree_fit), ("linreg", linreg_fit)):
single, bag = [], []
for _ in range(K):
x, y = draw_trainset()
single.append(fit(x, y)(grid)); bag.append(bagged(fit, x, y, M_BAG)(grid))
single, bag = np.array(single), np.array(bag)
res[name] = dict(
var_single=float(single.var(axis=0).mean()), var_bag=float(bag.var(axis=0).mean()),
bias_single=float(((single.mean(axis=0) - truth(grid)) ** 2).mean()),
bias_bag=float(((bag.mean(axis=0) - truth(grid)) ** 2).mean()))
t, l = res["tree"], res["linreg"]
check(f"[B-variance] tree: variance {t['var_single']:.3f} -> {t['var_bag']:.3f} "
f"(factor {t['var_single'] / t['var_bag']:.1f}), squared bias "
f"{t['bias_single']:.3f} -> {t['bias_bag']:.3f}; straight line: variance "
f"{l['var_single']:.4f} -> {l['var_bag']:.4f} (factor {l['var_single'] / l['var_bag']:.2f})",
t["var_single"] > 2 * t["var_bag"]
and max(t["bias_bag"], t["bias_single"]) < 0.01
and l["var_single"] < 1.3 * l["var_bag"])
[ok] [B-variance] tree: variance 0.094 -> 0.046 (factor 2.1), squared bias 0.001 -> 0.002; straight line: variance 0.0127 -> 0.0133 (factor 0.95)
The average loss of the bagged tree on its own training set does not fall toward zero as M grows: at M = 100 it is still above half of its value at M = 10, unlike for a method that accumulates its base learners.
x, y = draw_trainset()
Ms = [1, 5, 10, 25, 50, 100]
train_loss = [float(np.mean((y - bagged(tree_fit, x, y, n)(x)) ** 2)) for n in Ms]
check("[B-nofit] average loss of the bagged tree on its training set: "
+ ", ".join(f"M={n}: {v:.3f}" for n, v in zip(Ms, train_loss)),
train_loss[-1] > 0.5 * train_loss[2])
[ok] [B-nofit] average loss of the bagged tree on its training set: M=1: 0.076, M=5: 0.018, M=10: 0.025, M=25: 0.018, M=50: 0.017, M=100: 0.018
a resample of 200 points holds 63.2% of them (1 - (1 - 1/m)^m = 63.3%, limit 1 - 1/e = 63.2%)
two resamples share 40.1% of them ((1 - 1/e)^2 = 40.0%)
Two resamples of the same training set are not disjoint: a resample holds about 1 - 1/e ~ 63.2% of the distinct data points and two of them share about (1 - 1/e)^2 ~ 40.0%. That shared material is where the correlation rho comes from.
# Where rho comes from: two resamples of the same training set are not
# disjoint. A given data point is left out of one resample with probability
# (1 - 1/m)^m -> 1/e, so it is present with probability 1 - 1/e ~ 0.632, and
# present in BOTH of two independent resamples with probability ~ 0.400.
# That shared material is the dependence averaging cannot remove.
ov_m = 200
ov_rng = np.random.default_rng(5)
ov_both, ov_one = [], []
for _ in range(4000):
a = set(ov_rng.integers(0, ov_m, ov_m).tolist())
b = set(ov_rng.integers(0, ov_m, ov_m).tolist())
ov_one.append(len(a) / ov_m)
ov_both.append(len(a & b) / ov_m)
ov_one_m, ov_both_m = float(np.mean(ov_one)), float(np.mean(ov_both))
ov_pred_one = 1.0 - (1.0 - 1.0 / ov_m) ** ov_m
print(f" a resample of {ov_m} points holds {ov_one_m*100:.1f}% of them "
f"(1 - (1 - 1/m)^m = {ov_pred_one*100:.1f}%, limit 1 - 1/e = "
f"{(1-1/np.e)*100:.1f}%)")
print(f" two resamples share {ov_both_m*100:.1f}% of them "
f"((1 - 1/e)^2 = {(1-1/np.e)**2*100:.1f}%)")
check(f"[B-overlap] a resample holds {ov_one_m*100:.1f}% of the distinct "
f"data points, matching 1 - 1/e", abs(ov_one_m - ov_pred_one) < 0.01)
check(f"[B-overlap] two resamples share {ov_both_m*100:.1f}% of them, "
f"matching (1 - 1/e)^2 = {(1-1/np.e)**2*100:.1f}%",
abs(ov_both_m - (1 - 1 / np.e) ** 2) < 0.015)
[ok] [B-overlap] a resample holds 63.2% of the distinct data points, matching 1 - 1/e
[ok] [B-overlap] two resamples share 40.1% of them, matching (1 - 1/e)^2 = 40.0%
estimated from 400 training sets: sigma^2 = 0.097, rho = 0.448 (averaged over the grid)
The variance of an average of M bagged trees matches rho sigma^2 + (1 - rho) sigma^2 / M. sigma^2 and rho are estimated on 400 training sets with two trees each; the prediction is then tested on 150 INDEPENDENT training sets for each M in {2, 3, 5, 10, 20}, so the check is a measurement and not the algebraic identity between the two sample moments.
# The variance of an average of M identically distributed predictions, each
# with variance sigma^2 and pairwise correlation rho, is
# rho sigma^2 + (1 - rho) sigma^2 / M. Bagged trees are identically
# distributed by construction -- the resamples are exchangeable -- so the
# identity should describe them. sigma^2 and rho are estimated on one set of
# runs and the prediction is then tested on INDEPENDENT runs: estimating both
# sides from the same sample moments would make the check an algebraic
# identity rather than a measurement.
RH_GRID = np.linspace(0.05, 0.95, 40) # interior: the edges are sparse
def rh_pair_runs(reps, n_trees):
"""reps independent training sets, n_trees bagged trees on each.
Returns an array of shape (reps, n_trees, len(RH_GRID)) holding every
tree's prediction on the grid."""
out = np.empty((reps, n_trees, len(RH_GRID)))
for r in range(reps):
xs, ys = draw_trainset()
for b in range(n_trees):
idx = rng.integers(0, len(xs), len(xs)) # bootstrap resample
out[r, b] = tree_fit(xs[idx], ys[idx])(RH_GRID)
return out
# two trees per training set are enough to estimate sigma^2 and rho
rh_cal = rh_pair_runs(400, 2)
rh_c = rh_cal - rh_cal.mean(axis=(0, 1)) # centre per grid point
rh_sigma2 = (rh_c ** 2).mean(axis=(0, 1)) # variance of one tree
rh_cov = (rh_c[:, 0] * rh_c[:, 1]).mean(axis=0) # cov of two trees
rh_rho = rh_cov / rh_sigma2
print(f" estimated from 400 training sets: sigma^2 = {rh_sigma2.mean():.3f}, "
f"rho = {rh_rho.mean():.3f} (averaged over the grid)")
check("[B-rho] the estimated correlation is a correlation, in (0, 1)",
bool(np.all(rh_rho > 0) and np.all(rh_rho < 1)))
# prediction, tested on independent runs
rh_Ms = [2, 3, 5, 10, 20]
rh_rows, rh_err, rh_pt = [], [], []
for M in rh_Ms:
runs = rh_pair_runs(150, M)
measured = runs.mean(axis=1).var(axis=0) # variance of the average
predicted = rh_rho * rh_sigma2 + (1 - rh_rho) * rh_sigma2 / M
# two errors: over the grid as a whole, and point by point. The second is
# dominated by the estimation noise of 150 runs at a single feature value,
# so the first is what tests the identity.
agg = float(abs(measured.mean() - predicted.mean()) / predicted.mean())
pt = float(np.abs(measured - predicted).mean() / predicted.mean())
rh_err.append(agg)
rh_pt.append(pt)
rh_rows.append((M, float(measured.mean()), float(predicted.mean()), agg, pt))
print(f" M={M:2d}: measured {measured.mean():.4f} "
f"predicted {predicted.mean():.4f} "
f"({agg*100:.1f}% over the grid, {pt*100:.0f}% point by point)")
check(f"[B-rho] over the grid the identity predicts the variance of the "
f"average to within {max(rh_err)*100:.0f}% at every M, on runs it was "
f"not fitted to", max(rh_err) < 0.10)
rh_floor = rh_rho * rh_sigma2
check(f"[B-rho] averaging cannot go below the floor rho sigma^2 = "
f"{rh_floor.mean():.4f}: at M=20 the measured variance is still "
f"{rh_rows[-1][1] / rh_floor.mean():.2f} times it",
rh_rows[-1][1] > rh_floor.mean())
with open(OUT_DIR / "bagging_rho.csv", "w") as fh:
fh.write("M,var_measured,var_predicted,rel_error_grid,rel_error_pointwise\n")
for M, meas, pred, agg, pt in rh_rows:
fh.write(f"{M},{meas:.5f},{pred:.5f},{agg:.4f},{pt:.4f}\n")
# ---------------------------------------------------------------- CSV
with open(OUT_DIR / "bagging_variance.csv", "w") as fh:
fh.write("learner,var_single,var_bagged,bias2_single,bias2_bagged\n")
for name in ("tree", "linreg"):
r = res[name]
fh.write(f"{name},{r['var_single']:.4f},{r['var_bag']:.4f},"
f"{r['bias_single']:.4f},{r['bias_bag']:.4f}\n")
with open(OUT_DIR / "bagging_trainloss.csv", "w") as fh:
fh.write("M,train_loss\n")
for n, v in zip(Ms, train_loss):
fh.write(f"{n},{v:.4f}\n")
# -------------------------------------------------------------- preview
fig, (ax, ax2) = plt.subplots(1, 2, figsize=(10.0, 3.9))
for k, (style, mk, name) in enumerate([("-", "o", "tree 1"), ("--", "s", "tree 2")]):
ax.step(kgrid, kpreds[k], style, where="post", color=["k", "0.45"][k], lw=1.4,
label=f"{name}, and the five days it was trained on")
sel = subset == k + 1
ax.plot(kx[sel], ky[sel], mk, color=["k", "0.45"][k], ms=6, ls="none")
rest = subset == 0
ax.plot(kx[rest], ky[rest], ".", color="0.7", ms=5, ls="none", label="the other days")
ax.set_xlabel("minimum temperature of the day (deg C)")
ax.set_ylabel("maximum temperature of the day (deg C)")
ax.set_title("two depth-one trees, five random days each")
ax.legend(frameon=False, fontsize=7, loc="upper left")
ax2.plot([r[0] for r in rh_rows], [r[1] for r in rh_rows], "ko", ms=5, label="measured")
ax2.plot([r[0] for r in rh_rows], [r[2] for r in rh_rows], "k--", lw=1.2,
label="rho sigma^2 + (1 - rho) sigma^2 / M")
ax2.axhline(float(rh_floor.mean()), color="0.6", ls=":", lw=1, label="floor rho sigma^2")
ax2.set_xlabel("number M of base learners")
ax2.set_ylabel("variance of the average")
ax2.set_title("the identity, tested on independent training sets")
ax2.legend(frameon=False, fontsize=7)
fig.tight_layout()
fig.savefig(OUT_DIR / "bagging.png", dpi=110)
n_ok = sum(ok for _, ok in report)
print(f"\n{n_ok}/{len(report)} checks pass")
print(f"wrote {OUT_DIR / 'bagging_krems.csv'}, {OUT_DIR / 'bagging_kremstrees.csv'}, "
f"{OUT_DIR / 'bagging_rho.csv'}, {OUT_DIR / 'bagging_variance.csv'}, "
f"{OUT_DIR / 'bagging_trainloss.csv'}, {OUT_DIR / 'bagging.png'}")
if n_ok != len(report):
raise SystemExit(1)
[ok] [B-rho] the estimated correlation is a correlation, in (0, 1)
M= 2: measured 0.0701 predicted 0.0705 (0.5% over the grid, 11% point by point)
M= 3: measured 0.0603 predicted 0.0615 (2.0% over the grid, 9% point by point)
M= 5: measured 0.0549 predicted 0.0543 (1.1% over the grid, 11% point by point)
M=10: measured 0.0516 predicted 0.0489 (5.4% over the grid, 13% point by point)
M=20: measured 0.0469 predicted 0.0463 (1.3% over the grid, 12% point by point)
[ok] [B-rho] over the grid the identity predicts the variance of the average to within 5% at every M, on runs it was not fitted to
[ok] [B-rho] averaging cannot go below the floor rho sigma^2 = 0.0436: at M=20 the measured variance is still 1.08 times it
11/11 checks pass
wrote /Users/junga1/dictionaryappliedml/pythondemos/bagging_krems.csv, /Users/junga1/dictionaryappliedml/pythondemos/bagging_kremstrees.csv, /Users/junga1/dictionaryappliedml/pythondemos/bagging_rho.csv, /Users/junga1/dictionaryappliedml/pythondemos/bagging_variance.csv, /Users/junga1/dictionaryappliedml/pythondemos/bagging_trainloss.csv, /Users/junga1/dictionaryappliedml/pythondemos/bagging.png

B-rho writes when the script runs