Hierarchical Leaf Retrain & Micro-Model Routing¶
Tree partitions carve heterogeneous feature spaces into homogeneous sub-populations. While standard model trees fit models using the same features and targets at split and leaf time, complex empirical settings require hierarchical model routing:
- A macro-tree splits on aggregated or cohort-level features (e.g. city, make, model year, fleet tier) to segment cohorts.
- A retrain pass updates leaf nodes on granular telemetry (e.g. mileage, sensor readings, localized pricing) using specialized estimators (Linear Ridge, Robust Huber, Quantile, or GLM) on the frozen topology.
- In production, routing observations down a frozen tree structure and refitting terminal leaves achieves millisecond retraining latency (~2.2 ms on 1,030 rows across 1,000 iterations, 10x–20x faster than re-partitioning from scratch).
Script: benchmarks/suites/stump_leaf_retrain.py (uv run python benchmarks/suites/stump_leaf_retrain.py).
1. Architectural Mechanics: Frozen Partitions vs In-Place Solvers¶
flowchart LR
subgraph Phase1["Phase 1: Macro-Topology Discovery (Expensive)"]
direction TB
HistoricalData["Historical Macro Dataset\n(Cohorts, Demographics, Seasons)"] --> GreedySearch["Greedy Partition Search\nO(K * N log N)"]
GreedySearch --> FrozenTree["Frozen Decision Topology\n(Fixed Split Gates & Boundaries)"]
end
subgraph Phase2["Phase 2: Fast Leaf Retraining (~2.2 ms)"]
direction TB
StreamingData["New Telemetry / Stream Data\n(Local Sensors, Glitches, Pricing)"] --> Route["Route Rows Down Frozen Tree\nO(N * Depth)"]
FrozenTree -.-> Route
Route --> Partitions["Partitioned Leaf Subsets"]
Partitions --> SolverChoice{"Choose Leaf Family"}
SolverChoice -- "Gaussian OLS / Ridge" --> LinearLeaf["Linear Ridge Leaves\n0.68 ms Solve"]
SolverChoice -- "Gross Sensor Glitches" --> HuberLeaf["Robust Huber Leaves\nBounded Influence"]
SolverChoice -- "Safety / Risk Bands" --> QuantileLeaf["Quantile Corridors\n10th, 50th, 90th Pct"]
SolverChoice -- "Class Probabilities" --> BinomialLeaf["Binomial Logit Leaves\nLogistic Odds"]
end
classDef macroBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff;
classDef gateBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff;
classDef streamBox fill:#1e3a5f,stroke:#2b4c7e,stroke-width:2px,color:#ffffff;
classDef solverBox fill:#065f46,stroke:#059669,stroke-width:2px,color:#ffffff;
class HistoricalData,GreedySearch,FrozenTree macroBox;
class SolverChoice,FrozenTree gateBox;
class StreamingData,Route,Partitions streamBox;
class LinearLeaf,HuberLeaf,QuantileLeaf,BinomialLeaf solverBox;
A. Mathematical Formulation¶
Let a macro tree \(\mathcal{T}\) define \(M\) terminal partitions \(\mathcal{R}_1, \mathcal{R}_2, \dots, \mathcal{R}_M\). When calling tree.retrain(X, y):
1. Split boundaries \(\{s_k, \theta_k\}\) are frozen. Each observation \(i\) is routed to its terminal leaf index \(m(i) \in \{1, \dots, M\}\) in \(O(\text{depth})\) time.
2. The local optimizer fits parameter vector \(\boldsymbol{\beta}_m\) on partition subset \(\mathcal{I}_m = \{i \mid m(i) = m\}\):
B. Robust Huber Loss vs Outlier Breakdown¶
Under severe sensor anomalies (e.g., faulty voltage spikes), least-squares leaves collapse. The Huber loss transitions from quadratic error for small residuals to linear penalty for large errors:
The influence function \(\psi(r) = \frac{\partial L_\delta}{\partial r} = \text{clip}(r, -\delta, \delta)\) bounds the gradient. A single \(10,000\) unit outlier cannot shift the leaf regression plane.
C. Pinball Loss for Quantile Corridors¶
Retraining leaves with leaf="quantile" minimizes the asymmetric pinball loss for target quantile \(\tau \in (0, 1)\):
Fitting \(\tau \in \{0.10, 0.50, 0.90\}\) on frozen cohorts constructs empirical prediction corridors without refitting tree splits.
Next: run the retrain on the Yeh concrete data, because the solver choices above (mean, Ridge, Huber, quantile and binomial) only show their trade-offs once timings and outputs are measured on real data.
2. Empirical Benchmark: Yeh (1998) Concrete Retrain Laboratory¶
We evaluate the speed and resilience of leaf retraining on Yeh (1998) Concrete Compressive Strength using benchmarks/suites/stump_leaf_retrain.py.
==============================================================================
=== Stump Retrain Laboratory: Real Industrial Benchmark ===
Dataset: Concrete Compressive Strength (Yeh 1998, UCI Repository)
Problem: Segment concrete curing regimes (Early-stage vs Mature) and
retrain leaf families instantaneously on frozen partitions.
==============================================================================
1. Base Partition Search: Completed in 24.63 ms
Identified Split on 'Age' at threshold 14.0 days (Gain=71,243.3)
Interpretation: Hydration kinetics split concrete into Early Curing (<= 14d) vs Mature Curing (> 14d).
2. [Mean Leaf Retrain]: Completed in 1.775 ms (13.9x speedup)
Early Curing Leaf Mean Strength: 23.54 MPa (n=324)
Mature Curing Leaf Mean Strength: 41.45 MPa (n=706)
Interpretation: Mature concrete achieves 17.9 MPa higher average strength.
3. [Linear Leaf Retrain (l2=1e-4)]: Completed in 0.679 ms
Early Curing (<= 14d) (n=324):
Intercept: +36.37 MPa
d(Strength)/d(Cement): +0.0778 MPa per kg/m3
d(Strength)/d(Water): -0.2417 MPa per kg/m3
Mature Curing (> 14d) (n=706):
Intercept: +66.92 MPa
d(Strength)/d(Cement): +0.0798 MPa per kg/m3
d(Strength)/d(Water): -0.2764 MPa per kg/m3
4. [Robust Huber vs Polluted Mean under 10,000 MPa Sensor Glitch]:
Standard Mean Prediction: 54.34 MPa (Severely distorted by single outlier)
Robust Huber Prediction: 25.85 MPa (Impervious to outlier breakdown)
5. [Quantile Risk Corridors on Frozen Stump (Cement=300, Water=180)]:
Early Curing: 10th Pct = 9.73 MPa | 50th Pct = 21.50 MPa | 90th Pct = 41.10 MPa
Mature Curing: 10th Pct = 23.52 MPa | 50th Pct = 39.58 MPa | 90th Pct = 64.02 MPa
6. [Binomial Logit Family Retrain on Frozen Partitions]:
P(Strength >= 35 MPa | Early Curing 7d): 3.9%
P(Strength >= 35 MPa | Mature Curing 28d): 69.8%
| Operation | Model / Objective | Solve Latency | Key Empirical Takeaway |
|---|---|---|---|
| Base Tree Fit | Greedy Variance Split Search | 24.63 ms | Splits on curing age at 14.0 days (hydration boundary). |
| Mean Retrain | Partition Sample Averages | 1.78 ms | 13.9x speedup; Early curing: 23.54 MPa, Mature: 41.45 MPa. |
| Linear Ridge Retrain | Regularized OLS (\(L_2 = 10^{-4}\)) | 0.68 ms | Cement slope: \(+0.078\) Early vs \(+0.080\) Mature; Water: \(-0.24\) vs \(-0.28\). |
| Huber Retrain (Glitch) | Robust Huber Loss under Outlier | 3.12 ms | 25.85 MPa (True ~24 MPa) vs Polluted Mean 54.34 MPa (+110% error). |
| Quantile Corridors | Pinball Loss (\(\tau = 0.1, 0.5, 0.9\)) | 2.45 ms | Generates rigorous asymmetric engineering safety bands. |
| Binomial Logit Retrain | Logistic Probability (\(Y \ge 35\)) | 1.95 ms | Early curing 3.9% default odds vs Mature curing 69.8% odds. |

Next: call both retrain modes from Python, because the benchmark used a fitted macro tree and the mode argument decides whether that tree is changed in place or left untouched.
3. Python Usage: Internal vs External Retrain Modes¶
from mltreelib import ModelTree
# Step 1: Fit macro tree on cohort features
tree = ModelTree(max_depth=2, min_rows=50, leaf="mean")
tree.fit(X_train, y_train, col_names=feature_names)
# Mode A: In-Place Mutation (mode="internal")
# Updates existing tree instance with zero memory allocation
tree.retrain(X_new, y_new, leaf="linear", l2=1e-3)
# Mode B: Non-Destructive Branching (mode="external")
# Leaves original tree untouched and returns a new specialized ModelTree
huber_tree = tree.retrain(
X_new,
y_new,
mode="external",
leaf="huber",
control={"l2": 1e-3, "max_iter": 50},
)
Domain Limitations & Operational Boundaries¶
- Empty Leaf Partitions:
If incoming telemetry in
retrain()contains zero samples falling into a specific leaf \(\mathcal{R}_m\), the leaf model retains its previous coefficients or falls back to the parent node prior. - Frozen Topology Suboptimality: If underlying system dynamics undergo a structural regime change where split boundaries themselves shift, retraining leaf models cannot recover optimal accuracy. Full re-partitioning is required periodically.
- Collinearity in Small Partitions:
When fitting linear leaves on small leaves (\(N < P\)), the design matrix is rank-deficient. An explicit ridge penalty (
l2 >= 1e-3) is mandatory to guarantee invertibility.