Ensembler (Blending Math)¶
ensembler
¶
Consensus across base models — calculated + learned — pure logic, all in pandas.
The outer loop combines every base model's forecast per ts_id into a
consensus. Two families, both pure here (no GCP calls):
- Calculated (
mean,median,inverse_error,champion,horizon_specialist) — heuristics and per-series selectors that need no cross-series meta-learner training.combine_calculatedblends the baseforecast_predictionsin pandas and returnsensemble_<s>prediction rows the caller appends via the Storage Write API — the same append path the learned strategies use (every append-only cell-table write goes through the Write API, noINSERT…SELECTDML). - Learned (
nnls,ridge,xgb) — meta-learners that train on the backtest OOF to learn per-model trust weights.fit_learnedfits them; because it only ever seesbacktest_oof(never in-sample fits) leakage is structurally impossible, and it refuses to run when backtesting is off.
A run may request several strategies at once (ensemble.strategies is a list); each yields
its own weights/rows so calculated and learned consensuses sit side-by-side in one run.
Public surface: combine_calculated, combine_oof, fit_learned,
calibrate_ensemble_predictions, ensemble_oof_bounds (plus the pure combine helpers
mean_combine, median_combine, inverse_error_weights).
calibrate_ensemble_predictions
¶
Calibrate ensemble future prediction intervals from ens_oof & apply output constraints.
When cfg.output.calibrate_intervals is True and ens_oof has >= 5 residual rows for a
(ts_id, model_type) cell, replaces the linearly blended bounds with empirical 10th/90th
residual quantiles from calibration.calibrate_from_oof. Always enforces physical output
constraints (calibration.apply_output_constraints) when configured.
Source code in src/scale_forecasting/ensembler.py
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 | |
combine_calculated
¶
Blend base predictions into ensemble_<s> rows for every calculated strategy (pure).
Builds the ensemble_<s> rows in pandas rather than via SQL INSERT…SELECT: reads the base
forecast_predictions (long-format ts_id, model_type, forecast_date, yhat, yhat_lower,
yhat_upper) and returns prediction-row dicts the caller stamps with run_id/ensemble_id
and appends via the Storage Write API. Supports mean, median, inverse_error,
champion (per-series single winner), and horizon_specialist (per-series,
per-horizon-step inverse-error blend when oof_df is supplied, falling back to series-level
inverse_error otherwise), as well as top_k and parsimony_margin pruning.
Source code in src/scale_forecasting/ensembler.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | |
combine_oof
¶
Blend base-model OOF forecasts into ensemble OOF, per requested strategy (pure).
The scoring counterpart to the future-prediction consensus: because the base
forecast_predictions are a true beyond-data forecast (no actuals to join), each ensemble is
scored on the backtest OOF window — exactly the window the base models are scored on. This
applies the same consensus rules in OOF space (where y_true lives) and returns long-format
(ts_id, model_type='ensemble_<s>', fold_id, cutoff_date, forecast_date, y_true, yhat) for
the caller to score with metrics.compute_metrics. The cutoff is carried out as well as joined
on, because the caller cuts each series' history there to get the fold's MASE denominator.
Blends over whichever base models are present per (ts_id, cutoff_date, forecast_date) key
(see _fold_key for why the cutoff and not the fold ordinal):
mean/median are unweighted; inverse_error weights each model per ts_id by
1/WAPE over that model's OOF (self-contained — the same signal forecast_metadata would
carry); learned strategies apply learned_weights[strategy] (skipped if absent). Returns an
empty frame when the OOF is empty or no strategy produces a blend.
Bounds are not blended, so an ensemble's coverage/pinball/interval_score are
NaN. The base models' are not, any more — the OOF frame now carries yhat_lower and
yhat_upper per fold, and the base cells score them. Blending them is a separate question
with a real answer to choose (averaging two models' 80% intervals does not give an 80%
interval), so the columns stay NaN rather than being filled with something plausible and
wrong. An ensemble is compared to its base models on the point metrics.
Source code in src/scale_forecasting/ensembler.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
ensemble_oof_bounds
¶
Empirical 80% prediction intervals for one fold of an ensemble's OOF rows (pure).
Uses leave-one-fold-out OOF residuals (y_true - yhat from all other folds in
ens_oof_group, falling back to all folds when only a single fold exists) so every
ensemble_* pseudo-model earns non-NaN interval metrics (coverage_80, pinball_loss,
winkler_score, interval_width, wis) on the same footing as base models. Applies
calibration.apply_output_constraints when configured.
Source code in src/scale_forecasting/ensembler.py
ensemble_oof_bounds_from_arrays
¶
Fast numpy-array equivalent of ensemble_oof_bounds for one fold of a cell.
Source code in src/scale_forecasting/ensembler.py
filter_variants_for_ensemble
¶
Select the ensemble candidate pool across <base_model>@<tag> variants (pure).
Controlled by cfg.ensemble.variant_policy:
- "all": keep all configured model variants.
- "primary_only": keep at most one variant per base_model_name(m) — preferring the
untagged base model if present, else the first variant of that base model in config order.
- "best_per_base_model" (default): when multiple variants of the same base_model exist,
keep the variant with the lowest inner-fold OOF loss (or mean decision_metric loss in
metric_df when oof_df is absent), preventing collinear multi-variant dilution.
Source code in src/scale_forecasting/ensembler.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
fit_learned
¶
Fit every learned strategy in cfg on the backtest OOF's inner folds.
Returns (weights, artifacts, scoring_basis):
* weights[strategy] maps base-model name → learned weight,
* artifacts[strategy] is the pickled, uploadable meta-learner payload,
* scoring_basis is "holdout" when the newest fold was successfully reserved from the
fit and "in_sample" when it could not be — the caller writes it to the registry beside
the metrics it qualifies.
The newest fold is not in X. A meta-learner that trains and is scored on the same folds
reports a fit statistic, and it lands in the same leaderboard column as the base models'
genuinely out-of-fold numbers, where nothing distinguishes the two. Holding the newest fold out
of the fit is what makes ensemble_nnls comparable to theta — see inner_fold_mask.
Refuses to run when backtesting is off — a learned ensemble has no legitimate,
leakage-free data to train on (the guard that keeps stacking honest). Base models come
from cfg.models in order; native vs python doesn't matter — only their OOF forecasts do.
Source code in src/scale_forecasting/ensembler.py
inner_fold_mask
¶
Which OOF rows a fit is allowed to see, and what that says about the resulting numbers.
Returns (mask, basis). mask is True on the rows belonging to a fold other than
backtest.holdout_fold_id — the folds a stacker, an inverse_error weight or a
hyperparameter search may learn from. basis is "holdout" when a real split exists and
"in_sample" when it does not, in which case the mask is all-True and the caller fits on
everything it will later be scored on.
Two ways the split can fail to exist, and both are ordinary rather than exceptional: the run
asked for n_folds: 1, or a short series in a ragged panel achieved only the newest fold.
Falling back is the right behaviour — a stacker with no rows is worse than an optimistic one --
but it has to be said, because an in-sample number is indistinguishable from an honest one
once it is sitting in a metric column. That is what basis is written to the registry for.
Source code in src/scale_forecasting/ensembler.py
inverse_error_weights
¶
Weights ∝ 1/error, normalized to sum to 1.
Lower-error models get more weight. A zero-error model would divide by zero, so zeros are treated as "perfectly trusted" and share the weight equally among themselves. Falls back to uniform weights when no error is finite and positive.
errors must be losses — non-negative, zero is perfect — not raw metric values. That is
what metrics.loss_of produces, and every caller here routes through it. Handing this function
a metric where larger is better inverts the weighting silently, which is exactly what used to
happen with decision_metric="coverage".
Source code in src/scale_forecasting/ensembler.py
mean_combine
¶
median_combine
¶
Row-wise median across models — robust to a single wild base forecast.
smooth_oof_losses_by_similarity
¶
smooth_oof_losses_by_similarity(
losses_by_series,
similarity_matrix,
*,
smoothing=0.0,
top_k_neighbors=5,
series_ids=None,
)
Smooth per-series OOF loss vectors across the top-$K$ trajectory-similar neighbors.
For each series $i$ and candidate model $m$: $$\tilde{L}{i, m} = (1 - \alpha)\,L + \alpha \sum_{j \in \mathcal{N}K(i)} \tilde{S}$$ where $\alpha = \text{smoothing} \in [0, 1]$ and $\tilde{S}}\,L_{j, m{i, j}$ are the normalized positive trajectory similarities over the top-$K$ peers $j \ne i$ with $S > 0$.
Source code in src/scale_forecasting/ensembler.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |