Feature Engineering¶
features
¶
Feature engineering for the Python models — pure.
Everything here is declarative-from-config and runs inside the execution node, so no data crosses the network. Two entry points:
build_features(series, cfg) -> (y, X)— turn one raw series frame into the fit inputs:y(target Series indexed by ds, transform applied) andX(exog + holiday- Fourier + level-shift + lag columns aligned to
y, or None when no feature is configured). build_future_features(y, X, cfg) -> DataFrame | None— the forecast-horizon design frame, indexed by the future dates, with the same columns in the same order.holiday_frame(cfg) -> DataFrame— the one canonical holiday calendar (ds,holidaycolumns) computed fromfeatures.holidays, fed to both Python models and the BQML custom-holiday input so "holiday" is identical everywhere.
Two frames, because a forecast row is not a history row. Most of what we build is a
deterministic function of the date — holiday flags, Fourier phase, the level-shift step —
so the horizon's true values are computable, not guessable, and computing them is the only
way an exog-aware model sees the right seasonal phase for the dates it is forecasting. Only
user-supplied features.exog is genuinely unknown ahead of time; that alone falls back to
a documented stand-in. Backtesting never needed this (its "future" is in-sample, so
backtest_cell slices the real X) — which is exactly why the gap was easy to miss: the
folds scored on correct features while the shipped forecast did not.
Transforms come in two flavors. none/log1p are stateless — a model inverts with
only ctx.transform (a name). boxcox is stateful: its λ is fit per series (MLE),
so it must travel with the cell. We fit λ once in the worker (fit_transform_lambda),
put it on ctx.transform_lambda, and every apply/invert call passes that same λ —
never refit at predict, so the backtest folds and the final fit use one λ. λ = None
for the stateless transforms.
Public surface: build_features, build_future_features, holiday_frame,
apply_transform, invert_transform, fit_transform_lambda, level_shift_step.
ModelVariantSpec
dataclass
¶
ModelVariantSpec(
model_name,
base_model,
variant_tag,
scope_capability,
training_mode,
cov_profile,
is_univariate,
covariates_used,
effective_cfg,
clean_params,
model_cls,
intended_features=None,
)
Resolved execution contract and orthogonal taxonomy for one models entry.
add_similarity_features
¶
Attach fold-safe similarity_cluster_id and similarity_to_medoid to panel_df.
When cutoff_ds is provided (e.g., inside a walk-forward backtest fold), similarity
clustering is computed strictly on rows with ds <= cutoff_ds so future validation targets
never leak into the cluster assignment or medoid proximity score.
Source code in src/scale_forecasting/features.py
apply_transform
¶
Apply the configured target transform to y (forward direction).
lam is the fitted Box-Cox λ from fit_transform_lambda (or the resolved simplex_sum
when transform in ("clr", "ilr"); ignored by the stateless transforms).
Source code in src/scale_forecasting/features.py
build_features
¶
Build (y, X) fit inputs for one series.
series is one ts_id's rows with the configured date/target (and optional covariates)
columns. y is returned indexed by ds, sorted, with fold-safe target hygiene
(grid_policy, trim_leading_zeros, censored_col, outlier_policy) and the transform
applied, and with the index's frequency set whenever the dates are regular (see
_with_inferred_freq). X carries any configured dynamic covariates (exog,
future_covariates, past_covariates), an is_holiday flag, Fourier terms,
trading_days terms, level_shift, is_outlier (when outlier_policy="flag"),
similarity cluster features (when similarity_cluster_feature=True), and lagged covariates
— aligned to y — or None when nothing is configured.
Source code in src/scale_forecasting/features.py
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 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 | |
build_future_features
¶
The design frame for the forecast horizon, indexed by the future dates.
Source code in src/scale_forecasting/features.py
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 | |
clean_model_params
¶
Return a copy of params with per-variant feature-override meta-keys stripped (pure).
Source code in src/scale_forecasting/features.py
clean_training_target
¶
Apply fold-safe censored demand recovery (censored_col) and outlier treatment
(outlier_policy) strictly on the provided training window (pure).
Returns (cleaned_y, outlier_flag_array_or_None).
Source code in src/scale_forecasting/features.py
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 | |
compute_cov_profile
¶
Compute the realized covariate profile after narrowing to model_cls capabilities.
Source code in src/scale_forecasting/features.py
compute_degree_days_df
¶
Append Heating Degree-Day (hdd_<col>) and Cooling Degree-Day (cdd_<col>) columns.
For each (col, base_temp) entry in degree_day_cols:
- hdd_<col> = max(0, base_temp - df[col])
- cdd_<col> = max(0, df[col] - base_temp)
Source code in src/scale_forecasting/features.py
compute_demand_profile
¶
Syntetos–Boylan–Croston (2005) demand regime classification (pure).
Computes the Average Demand Interval (adi = n_obs / n_nonzero), the squared coefficient of
variation of non-zero demand (cv2 = (std / mean)^2), zero_share, and n_obs, and
assigns one of five canonical regimes:
"cold_start":n_obs < min_cold_start_obsor fewer than 3 non-zero observations"smooth":adi < 1.32andcv2 < 0.49"erratic":adi < 1.32andcv2 >= 0.49"intermittent":adi >= 1.32andcv2 < 0.49"lumpy":adi >= 1.32andcv2 >= 0.49
Source code in src/scale_forecasting/features.py
effective_config_for_model
¶
Return a RunConfig narrowed to model_name's variant overrides and model_cls tiers.
Used inside the worker and HPO so that in a multi-model or multi-variant benchmark under
on_unsupported_covariates="fallback" (the default), a univariate model or a variant with
narrowed covariates never has its target truncated by exog_lags of an unused covariate tier
or receives columns it should not consume. The run identity (run_id and model_hash) is
always computed from the original cfg before calling this helper.
Source code in src/scale_forecasting/features.py
expand_model_variants
¶
Expand include_univariate_baselines and ablation_modes into concrete models and
model_params with capability-aware filtering and semantic deduplication (pure).
Source code in src/scale_forecasting/features.py
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 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 | |
extract_static_covariates
¶
Validate and extract series-constant static covariates for one ts_id (pure).
Static covariates (features.static_covariates, e.g. region, category, archetype)
carry series-level metadata for global/hybrid models and hierarchical grouping. Because they are
constant within a single series (and may be categorical strings), they are deliberately omitted
from the single-series local design matrix X — where a constant column is collinear with the
model intercept — and surfaced here as a clean {col: value} mapping.
Source code in src/scale_forecasting/features.py
fit_transform_lambda
¶
Fit the transform's stateful parameter on y (the full series' target).
boxcox: its λ, fit by maximum likelihood (scipy.stats.boxcox_normmax) on the raw target. Box-Cox is defined only for strictly positive data.clr/ilr: the resolved compositionalsimplex_sum($S = 1.0$ or $100.0$, or the explicitsimplex_sumoverride), validating thaty >= 0.none/log1p: returnsNone(stateless).
Source code in src/scale_forecasting/features.py
holiday_frame
¶
Canonical holiday calendar for the run.
features.holidays lists ISO country codes (e.g. ["US"]). Returns a frame with
ds (datetime64[ns]) and holiday (name) columns, empty when none configured.
A generous fixed year window keeps the calendar deterministic; callers filter by ds.
Source code in src/scale_forecasting/features.py
invert_transform
¶
Invert a transform on forecasts/bounds so the frame is in original units.
lam is the same fitted Box-Cox λ (or simplex_sum for clr/ilr) used in the
forward direction, so invert(apply(y)) == y.
Source code in src/scale_forecasting/features.py
level_shift_step
¶
Detect one abrupt level shift in y and return it as a 0/1 step dummy.
The shipped example data contains exactly this pattern — data_gen.generator gives every
archetype a level_shift_prob and applies a single additive jump partway through the
history — and it is the common real shape too (a re-baselined product, a store
reopening, a units→cases change). A model that cannot see it fits the average of two
regimes and is biased for the whole horizon.
The encoding is a step, not a spike: 0 before the changepoint, 1 from it onward, and
(in build_future_features) 1 across the entire horizon, because a level shift persists
— that is what distinguishes it from an outlier. A regression model then learns the jump
as one coefficient and carries it forward.
Detection is a single-changepoint binary segmentation, computed in one vectorized pass
(O(n) via cumulative sums) because this runs once per series across millions of them: for
every admissible split, standardize the gap between segment means by a robust noise
estimate and take the argmax. Returns all zeros when nothing clears _LEVEL_SHIFT_SIGMA
or the series is too short to split — never a spurious constant column.
Source code in src/scale_forecasting/features.py
local_seasonal_baseline
¶
Fast, fold-safe local trend + seasonal baseline for outlier and stockout imputation.
Source code in src/scale_forecasting/features.py
regime_compatible_models
¶
Filter models to those suited for demand_class, never returning an empty list.
Source code in src/scale_forecasting/features.py
regularize_series_grid
¶
Regularize missing calendar timestamps (grid_policy) and trim pre-launch leading zeros
(trim_leading_zeros) for one series (pure).
Source code in src/scale_forecasting/features.py
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 | |
resolve_model_variant
¶
Resolve a <base_model> or <base_model>@<tag> entry into its ModelVariantSpec.
Source code in src/scale_forecasting/features.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 | |
select_models_for_series
¶
Return the active model list for series under cfg.features.regime_routing.
Source code in src/scale_forecasting/features.py
should_stamp_variant_diagnostics
¶
Whether run_cell / run_panel should stamp taxonomy diagnostics onto CellResult.
Source code in src/scale_forecasting/features.py
variant_diagnostics_for
¶
Return the canonical scope/covariate taxonomy and spec-hash metadata for model_name.