# mltreelib — Complete LLM Context & Reference Guide Site: https://jkapila.github.io/mltreelib/ Repository: https://github.com/jkapila/mltreelib mltreelib is a tree-based machine learning library with a Rust core (`mltreelib-core`) and thin Python/R frontends. Zero scikit-learn dependency. --- ## Document: quickstart.md # Quickstart Fit a high-performance model tree on real tabular data in seconds, inspect its transparent decision paths, and evaluate how piecewise linear leaves eliminate step-function distortion. The complete executable script lives at [`examples/quickstart.py`](https://github.com/jkapila/mltreelib/blob/main/examples/quickstart.py): ```bash uv run python examples/quickstart.py ``` --- ## 1. Why Model Trees Outperform Standard CART Standard decision trees (CART) predict a constant mean in each leaf node. On continuous tabular surfaces, this creates rigid staircase artifacts that require excessive depth and overfitting to approximate simple linear trends. `ModelTree(leaf="linear")` decouples the tree partition search from leaf estimation. The tree discovers optimal split thresholds while terminal leaves fit regularized linear ridge regressions, capturing local slopes smoothly at shallow depth. ```mermaid flowchart LR A["Raw Feature Vector\n[Cement, Water, Age, ...]"] --> B{"Age <= 14 days?"} B -- "Yes (Early Hydration)" --> C{"Cement <= 350 kg/m³?"} B -- "No (Matured Matrix)" --> D{"Cement <= 350 kg/m³?"} C --> E["Leaf 8: Local Ridge Linear\ny = 0.05*Cement - 0.12*Water + ..."] D --> F["Leaf 13: Local Ridge Linear\ny = 0.11*Cement - 0.04*Water + ..."] E --> G["Continuous Prediction (MPa)"] F --> G classDef inputBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff; classDef splitBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef leafBox fill:#065f46,stroke:#059669,stroke-width:2px,color:#ffffff; classDef outBox fill:#1e3a5f,stroke:#2563eb,stroke-width:2px,color:#ffffff; class A inputBox; class B,C,D splitBox; class E,F leafBox; class G outBox; ``` --- ## 2. Fit and Evaluate on Real Benchmark Data We evaluate on the industrial benchmark **Concrete Compressive Strength** (Yeh 1998[^1]; 1,030 mixtures, 8 continuous ingredients). !!! tip "Why Decouple Tree Search from Leaf Estimation?" Evaluating multivariate linear models at every candidate split threshold incurs massive combinatorial overhead. `ModelTree` uses fast variance or structural break heuristics to discover partition boundaries, then solves the closed-form regularized ridge system once per terminal leaf node. ??? info "Dataset Provenance: Concrete Compressive Strength" Collected by Prof. I-Cheng Yeh (1998) at Chung-Hua University[^1]. Features 1,030 lab mixtures quantifying how 7 cementitious ingredient ratios (cement, slag, fly ash, water, superplasticizer, coarse aggregate, fine aggregate) and curing age interact nonlinearly to determine compressive yield stress (MPa). === "ModelTree (Linear Ridge)" ```python from mltreelib import Metrics, ModelTree, load_dataset # 1. Load benchmark dataset (1,030 mixtures, 8 features) data = load_dataset("concrete_compressive_strength") target = "ConcreteCompressiveStrength" feature_names = [col for col in data["columns"].keys() if col != target] n_rows = data["n_rows"] X = [[data["columns"][c][i] for c in feature_names] for i in range(n_rows)] y = data["columns"][target] # 80/20 train/test split split_idx = int(n_rows * 0.8) X_train, y_train = X[:split_idx], y[:split_idx] X_test, y_test = X[split_idx:], y[split_idx:] # Fit mltreelib ModelTree (piecewise linear ridge leaves, depth=3) model_tree = ModelTree(leaf="linear", max_depth=3, min_rows=20, l2=1e-3) model_tree.fit(X_train, y_train, col_names=feature_names) pred_mt = model_tree.predict(X_test) # Measure holdout metrics metrics = Metrics() m_mt = metrics.regression(y_test, pred_mt) print(f"ModelTree (Linear): RMSE = {m_mt['rmse']:.2f} MPa, R2 = {m_mt['r2']:.3f}, MAE = {m_mt['mae']:.2f} MPa") ``` !!! example "Output of `examples/quickstart.py`" ```text --8<-- "docs/snippets/outputs/quickstart.txt" ``` === "ModelTree(leaf='mean') Baseline" ```python from mltreelib import Metrics, ModelTree, load_dataset data = load_dataset("concrete_compressive_strength") target = "ConcreteCompressiveStrength" feature_names = [col for col in data["columns"].keys() if col != target] n_rows = data["n_rows"] X = [[data["columns"][c][i] for c in feature_names] for i in range(n_rows)] y = data["columns"][target] split_idx = int(n_rows * 0.8) X_train, y_train = X[:split_idx], y[:split_idx] X_test, y_test = X[split_idx:], y[split_idx:] # Fit piecewise-constant CART decision tree (leaf="mean", depth=3) cart = ModelTree(leaf="mean", max_depth=3, min_rows=20) cart.fit(X_train, y_train, col_names=feature_names) pred_cart = cart.predict(X_test) metrics = Metrics() m_cart = metrics.regression(y_test, pred_cart) print(f"ModelTree (Mean) : RMSE = {m_cart['rmse']:.2f} MPa, R2 = {m_cart['r2']:.3f}, MAE = {m_cart['mae']:.2f} MPa") ``` --- ## 3. Empirical Results & Performance Comparison ```text Loaded Concrete Compressive Strength: 1030 rows, 8 features. Train rows: 824, Test rows: 206 === Model Performance on Test Set === ModelTree (leaf="mean", d=3) : RMSE = 11.09 MPa, R2 = 0.566, MAE = 8.47 MPa sklearn DecisionTreeRegressor : RMSE = 10.92 MPa, R2 = 0.579, MAE = 8.36 MPa mltreelib ModelTree (Linear) : RMSE = 7.28 MPa, R2 = 0.813, MAE = 5.07 MPa --> Error reduction with linear leaves: 34.3% ``` | Model | Leaf Formulation | Depth | Test RMSE (MPa) | Test $R^2$ | Test MAE (MPa) | |---|---|:---:|:---:|:---:|:---:| | **`ModelTree(leaf="mean")`** | Constant Mean | 3 | 11.09 | 0.566 | 8.47 | | **`sklearn.tree.DecisionTreeRegressor`** | Constant Mean | 3 | 10.92 | 0.579 | 8.36 | | **`mltreelib` ModelTree (Linear)** | Regularized Linear Ridge | 3 | **7.28** | **0.813** | **5.07** | With identical tree topology (depth 3, 8 leaves), `ModelTree` with linear leaves achieves a **34.3% error reduction** over constant-leaf CART baselines, elevating explained variance from ~57% to 81.3%. ![Quickstart ModelTree vs CART Step Function](assets/quickstart_tree_vs_flat.png) - **Left Panel (Parity Plot)**: Predictions from `ModelTree(leaf="linear")` cluster tightly along the 1:1 diagonal, whereas constant-mean trees are constrained to horizontal bands corresponding to leaf averages. - **Right Panel (Response Profile)**: Along the gradient of Cement content, constant-mean CART trees produce wide step discontinuities, while `ModelTree` preserves the physical continuous response curve. --- ## 4. Transparent Tree Structure & Inspection `ModelTree` prints in `rpart`-compatible notation with exact node numbering, split criteria, sample support, deviance, and fitted constants: ```text n= 824 node), split, n, deviance, yval * denotes terminal node 1) root 824 228333.917 35.46173 2) Age <= 14 263 38269.292 22.98684 4) Cement <= 350 190 14043.474 18.46968 8) Age <= 7 143 6493.479 15.44493 * 9) Age > 7 47 2261.042 27.67263 * 5) Cement > 350 73 10258.391 34.74381 10) Water <= 178 48 5059.180 39.25437 * 11) Water > 178 25 2347.636 26.08354 * 3) Age > 14 561 129948.081 41.31003 6) Cement <= 350 430 68793.496 36.74539 12) Cement <= 164.6 101 8521.781 26.09900 * 13) Cement > 164.6 329 45309.403 40.01374 * 7) Cement > 350 131 22786.202 56.29321 14) Water <= 181.1 76 6496.752 63.46522 * 15) Water > 181.1 55 6978.299 46.38280 * ``` Export directly to Graphviz DOT for vector diagrams: ```python dot_code = model_tree.to_dot() ``` ![Fitted Quickstart ModelTree Topology](assets/quickstart_tree.svg) --- ## Next Steps - Explore the **[Practical Modeling Pathways](guides/index.md)** for end-to-end classification, survival, and uplift workflows. - Study **[Dynamic Leaf Retraining](concepts/hierarchical-retrain.md)** for fast warm-start leaf adaptation. - Review **[Distributional Leaves](concepts/distributional-leaves.md)** for Tobit, Tweedie, and Gamma modeling. - Inspect the **[API Reference](reference/model.md)** for all parameters and methods. --- ## References [^1]: Yeh, I-C. (1998). "Modeling of strength of high-performance concrete using artificial neural networks." *Cement and Concrete Research*, 28(12), 1797-1808. [^2]: Quinlan, J. R. (1992). "Learning with continuous classes." *5th Australian Joint Conference on Artificial Intelligence*, 343-348. [^3]: Chow, G. C. (1960). "Tests of equality between sets of coefficients in two linear regressions." *Econometrica*, 28(3), 591-605. --- ## Document: installation.md # Installation Install mltreelib as a Python wheel with a Rust core. Model fitting and inference require no additional Python packages. !!! warning "Early development" mltreelib is at an early stage. APIs may change or break between releases. For any new feature, please [raise an issue first](https://github.com/jkapila/mltreelib/issues) so we can discuss scope before code is written. ## From PyPI ```bash pip install mltreelib ``` ## With uv ```bash uv add mltreelib ``` ## From source Building from source needs a Rust toolchain ([rustup](https://rustup.rs)) and [maturin](https://www.maturin.rs): ```bash git clone https://github.com/jkapila/mltreelib cd mltreelib uv sync --group dev uv run maturin develop --release ``` ## Verify the install ```bash python -c "import mltreelib; print(mltreelib.__version__)" ``` If you use uv, run the command with `uv run python`. Continue to the [quickstart](quickstart.md) to fit and inspect your first tree. ## Dataset loading (optional extra) Install the dataset extra to read the package's Parquet datasets. ```bash pip install "mltreelib[dataset]" # or uv add "mltreelib[dataset]" ``` ## Plotting (optional extra) Charts and diagrams ride on the standard rendering wheels — install them only when you build figures: ```bash pip install "mltreelib[plotting]" # matplotlib + pygraphviz + seaborn # or uv sync --extra plotting ``` `mltreelib.plots` (metric charts) and `ModelTree.to_dot()` + pygraphviz (tree diagrams) are the two consumers. The core library never imports them. ## What you get | | | |---|---| | `ModelTree` | the tree: fit / predict, rpart-style text, SVG diagram, constraints | | 14 encoders | OneHot, Hashing, Target, CatBoost, WoE, James-Stein, Count, Leave-One-Out, M-Estimator, Base-N, Sum, Quantile, TF-IDF, Embedding | | scalers & builders | `VariableScaler`, `GroupedScaler`, `DatasetBuilder`, `IndexMapper` | | `Metrics` | regression / classification / regression-by-group reports | | dataset helpers | `list_datasets`, `load_dataset`, `summarize`, `generate`, calendar / lag / window transforms | Numerics run in the Rust core; the data layer is the same code the [benchmarks](benchmarks/elo.md) run on. --- ## Document: regression-methods.md --- title: "Comprehensive Regression Methods & Regularizers Showcase" description: "Empirical benchmark evaluating 0D location leaves, 1D linear trees, Huber M-regression, quantile envelopes, penalized splines (MARS & GAM), count GLMs, and structured group lasso." keywords: - regression model trees - Huber robust regression - quantile regression - penalized B-spline GAM - group lasso - negative binomial GLM --- # Comprehensive Regression Methods & Regularizers Showcase A single regression loss cannot serve every problem. Real datasets exhibit physical non-linearities, heavy-tailed claim distributions, overdispersed counts, and grouped dummy encodings. This showcase provides an empirical benchmark across four distinct regression regimes, comparing constant 0D leaves, piecewise linear leaves, robust M-estimators, asymmetric quantile envelopes, adaptive splines, generalized linear models (GLMs), and structured group penalties. All benchmarks run from a single reproducible script with zero `scikit-learn` dependency: ```bash uv run python -u benchmarks/suites/regression_methods_showcase.py ``` ```mermaid flowchart LR A["Raw Tabular Features\nX in R^(N x P)"] --> B{"Response Distribution & Structure"} B -- "Physical Continuous / Smooth" --> C["Penalized B-Spline GAM\nB-splines + D2 penalty"] B -- "Linear Subgroups" --> D["ModelTree\nOLS + Hierarchical Shrinkage"] B -- "Heavy Tails / Severe Outliers" --> E["Huber M-Regression\nIterative MAD scale"] B -- "Asymmetric Risk Envelopes" --> F["Quantile Regression\nJoint taus + crossing check"] B -- "Overdispersed Counts" --> G["Negative Binomial GLM\nVariance: mu + alpha*mu^2"] B -- "Grouped Dummy Blocks" --> H["Group Lasso / SGL\nBlock proximal thresholding"] classDef blueBox fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#ffffff; classDef greenBox fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#ffffff; classDef amberBox fill:#78350f,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef grayBox fill:#334155,stroke:#64748b,stroke-width:2px,color:#ffffff; class A,B grayBox; class C,D blueBox; class E,F amberBox; class G,H greenBox; ``` ![Comprehensive Regression Methods Benchmark Evaluation across Concrete, Insurance, Wine, and Quantile Tasks](../assets/guides/regression-methods-benchmark.svg) --- ## Regression Architecture Taxonomy `mltreelib` unifies tree partitioning with heterogeneous local leaf solvers: | Family | Architecture | Optimization Objective | Primary Use Case | | :--- | :--- | :--- | :--- | | **0D Constant** | `LeafKind::Mean`, `Median`, `Huber` | SSE, SAE ($L_1$), Huber Location | Fast step-function baselines | | **1D Linear** | `LeafKind::Linear` (OLS / Ridge / Lasso) | $\frac{1}{2n}\|X\beta - y\|_2^2 + \lambda_1\|\beta\|_1 + \lambda_2\|\beta\|_2^2$ | Piecewise linear trends with coordinate sparsity | | **Robust M-Estimation** | `LeafKind::Linear` (`loss="huber"`) | Piecewise quadratic/linear with iterative $\hat{\sigma}_\text{MAD}$ scale | Outlier-contaminated responses and heavy tails | | **Asymmetric Quantiles** | `LeafKind::Linear` (`loss="quantile"`, $\tau$) | Pinball loss $\rho_\tau(r) = r(\tau - \mathbb{1}(r < 0))$ | Conditional risk boundaries & prediction intervals | | **Smooth Asymmetric** | `LeafKind::Linear` (`loss="expectile"`, $\tau$) | Asymmetric squared $\rho_\tau^\text{exp}(r) = \|\tau - \mathbb{1}(r < 0)\| r^2$ | Differentiable asymmetric loss without pinball kinks | | **Constrained Bounded** | `LeafKind::Linear` (`nnls=True`) | Lawson-Hanson active-set $\beta \ge 0$ | Non-negative price, dosage, or mixing mixtures | | **Structured Groups** | `LeafKind::Linear` (`group_l2 > 0`) | Block soft-thresholding $\sum_g \sqrt{p_g} \|\beta_g\|_2$ | One-hot dummy blocks and multi-channel features | | **Count & Bounded GLM** | `LeafKind::Glm` (`Poisson`, `NegBinomial`, `Beta`) | Maximum deviance with canonical/log link | Overdispersed counts and rates in $(0, 1)$ | | **Adaptive Splines** | `MarsRegressor` | Forward hinge basis expansion + backward pruning | Piecewise linear continuous basis functions | | **Penalized Spline GAM** | `GamRegressor` | B-splines with second-difference roughness penalty | Smooth additive non-linear response curves | --- ## Study 1: Physical Continuous Process (Concrete Compressive Strength) - **Dataset**: Industrial Concrete Mixtures ([OpenML 4353](https://www.openml.org/d/4353)) - **Structure**: 1,030 samples, 8 continuous physical ingredients (Cement, Blast Furnace Slag, Fly Ash, Water, Superplasticizer, Coarse Aggregate, Fine Aggregate, Age). - **Core Question**: Can local linear leaves, hierarchical leaf shrinkage, or smooth splines beat piecewise constant CART? ### Empirical Results | Model Architecture | Test RMSE | Test MAE | Complexity | Fit Time | | :--- | :---: | :---: | :---: | :---: | | **CART Mean Leaf (0D SSE)** | 9.947 | 7.656 | 15 leaves | 12.7 ms | | **Binned Histogram Split (0D SSE)** | 10.029 | 7.709 | 15 leaves | 2.0 ms | | **Median Leaf (0D $L_1$ Location)** | 9.971 | 7.642 | 15 leaves | 3.3 ms | | **OLS Linear Tree (shrinkage=0.0)** | 6.589 | 4.790 | 15 leaves | 11.4 ms | | **Hierarchical Shrunk Tree ($\lambda=10.0$)** | **6.503** | **4.842** | 15 leaves | 5.5 ms | | **Huber Robust Linear Tree** | 6.594 | 4.684 | 15 leaves | 231.9 ms | | **NNLS Non-Negative Linear Tree** | 6.785 | 4.944 | 15 leaves | 3.2 ms | | **Expectile Linear Tree ($\tau=0.75$)** | 7.235 | 5.377 | 15 leaves | 33.7 ms | | **MARS Adaptive Spline (`max_terms=15`)** | 6.586 | 5.423 | 12 terms | 28.4 ms | | **Penalized B-Spline GAM (`n_knots=8`)** | **5.533** | **4.428** | 65 terms | 28.9 ms | ### Key Findings 1. **Piecewise linear leaves crush 0D CART**: Moving from constant leaves to local linear leaves reduces test RMSE from $9.947$ to $6.589$ (a 33.8% error reduction). 2. **Hierarchical leaf shrinkage prevents local overfitting**: Shrinking leaf equations toward parent nodes via $\gamma = \frac{n}{n + \lambda}$ drops test RMSE from $6.589$ to $6.503$. 3. **Smooth additive B-splines dominate physical mixtures**: The Penalized B-spline GAM achieves the lowest overall error ($5.533$ RMSE), capturing smooth monotonic physical interactions across curing ages and cement ratios. --- ## Study 2: Heavy-Tailed Outliers & Risk Envelopes (Healthcare Insurance) - **Dataset**: US Healthcare Expenses ([OpenML 43894](https://www.openml.org/d/43894)) - **Structure**: 1,338 policies, right-skewed medical expenses with severe outliers (smoker hospitalizations), plus dummy-encoded categorical regions. - **Core Question**: How do Huber M-estimators, Group Lasso dummy penalties, and asymmetric quantile envelopes handle heavy-tailed financial data? ### Empirical Results | Model Architecture | Test RMSE | Test MAE | Test MedAE | Fit Time | | :--- | :---: | :---: | :---: | :---: | | **CART Mean Leaf** | 4,523.7 | 2,820.1 | 1,869.2 | 1.2 ms | | **OLS Linear Tree (shrinkage=0.0)** | 4,355.1 | 2,450.0 | 1,181.1 | 3.1 ms | | **Hierarchical Shrunk Tree ($\lambda=15.0$)** | **4,341.4** | 2,505.9 | 1,369.5 | 3.2 ms | | **Group Lasso Tree (`group_l2=5.0`)** | 6,056.1 | 4,305.4 | 2,742.1 | 1.0 ms | | **Huber Robust Linear Tree** | **4,315.7** | **1,315.2** | **92.9** | 168.0 ms | | **NNLS Non-Negative Linear Tree** | 4,330.5 | 2,392.7 | 1,266.4 | 1.5 ms | | **Expectile Linear Tree ($\tau=0.50$)** | 4,342.7 | 2,468.4 | 1,305.8 | 23.4 ms | | **Quantile Linear ($\tau=0.10$ Lower Risk)** | 5,559.7 | 3,344.8 | 1,815.1 | 1.3 ms | | **Quantile Linear ($\tau=0.50$ Median)** | 4,653.6 | 2,543.6 | 1,284.8 | 1.6 ms | | **Quantile Linear ($\tau=0.90$ Upper Risk)** | 6,721.5 | 5,503.5 | 4,423.1 | 1.3 ms | ### Multi-Tau Quantile Crossing Diagnostics Fitting independent quantile surfaces across $\tau \in [0.10, 0.50, 0.90]$ can cause quantile crossing when lines invert. `mltreelib` includes native diagnostic verification: ```python from mltreelib import detect_quantile_crossings diag = detect_quantile_crossings( predictions=[pred_q10, pred_q50, pred_q90], taus=[0.10, 0.50, 0.90] ) print("Violations:", diag["n_violations"], "Max Dollar Inversion:", diag["max_violation"]) ``` ```text --- Diagnostic: Multi-Tau Quantile Crossing Analysis --- Evaluated 268 held-out cases across quantiles [0.1, 0.5, 0.9]: - Crossing Inversion Pairs: 4 - Rows with Violations: 4 / 268 - Crossing Violation Rate: 1.49% - Maximum Dollar Violation: $3,337.55 ``` --- ## Study 3: Overdispersed Counts & Bounded Distributions (Wine Quality) - **Dataset**: Physicochemical Wine Quality ([OpenML 40691](https://www.openml.org/d/40691)) - **Structure**: 6,497 bottles, discrete quality ratings (integers 3–9). - **Core Question**: Does accounting for overdispersion with Negative Binomial deviance beat standard Gaussian OLS? ### Empirical Results | Model Architecture | Test RMSE | Test MAE | Fit Time | | :--- | :---: | :---: | :---: | | **CART Mean Leaf** | 0.750 | 0.594 | 9.6 ms | | **OLS Linear Tree (shrinkage=0.0)** | 0.725 | 0.573 | 15.4 ms | | **Hierarchical Shrunk Tree ($\lambda=10.0$)** | 0.725 | 0.573 | 15.6 ms | | **Poisson GLM Tree (Log Link)** | 0.743 | 0.587 | 131.8 ms | | **Negative Binomial GLM Tree (Dispersion)** | **0.721** | **0.567** | 29.9 ms | | **Beta Regression Tree (Scaled to $(0, 1)$)** | 0.736 | 0.579 | 42.8 ms | The Negative Binomial GLM tree models conditional variance as $V(\mu) = \mu + \alpha \mu^2$, outperforming both constant CART ($0.750$) and Gaussian OLS ($0.725$). --- ## Study 4: Controlled Ground Truth & Structured Group Sparsity - **Setup**: Controlled simulation with 12 features split into two structured 4-column blocks plus individual coordinates and a non-linear sine component: $$\mathbf{x}_\text{signal} \in \mathbb{R}^4, \quad \mathbf{x}_\text{noise} \in \mathbb{R}^4, \quad x_\text{sine} \sim \mathcal{U}(-3, 3), \quad y = \mathbf{x}_\text{signal}^\top \boldsymbol{\beta} + 2.5 \sin(x_\text{sine}) + \epsilon$$ ### Empirical Results | Model Configuration | Test RMSE | Test MAE | Fit Time | | :--- | :---: | :---: | :---: | | **Standard OLS Linear Tree** | 0.756 | 0.643 | 2.4 ms | | **Coordinate Lasso Tree ($L_1=2.0$)** | 0.920 | 0.776 | 0.8 ms | | **Group Lasso Tree ($\text{group\_l2}=5.0$)** | 0.925 | 0.779 | 0.8 ms | | **Sparse-Group Lasso ($L_1=1.5, \text{group\_l2}=4.0$)** | 1.168 | 0.959 | 0.8 ms | | **Penalized B-Spline GAM on Sine Feature** | **0.209** | **0.161** | 0.7 ms | ### Block Zeroing Verification Under Group Lasso, the block norm of the uninformative noise features $\|\beta_\text{noise}\|_2$ is driven exactly to $0.0$, selecting only the active signal group. The B-spline GAM smoothly recovers the true sine curve with an RMSE of $0.209$. --- ## Code Example: Running the Benchmark Suite ```python from mltreelib import ( ModelTree, GamRegressor, MarsRegressor, detect_quantile_crossings, ) # 1. Fit an OLS ModelTree with Hierarchical Leaf Shrinkage tree = ModelTree( leaf="linear", max_depth=4, min_rows=20, shrinkage=10.0, # Empirical Bayes leaf-to-parent shrinkage ) tree.fit(X_train, y_train) pred_shrunk = tree.predict(X_test) # 2. Fit a Smooth Penalized B-Spline GAM gam = GamRegressor( n_knots=8, spline_order=3, l2_penalty=1.0, fit_intercept=True, ) gam.fit(X_train, y_train) pred_gam = gam.predict(X_test) # 3. Fit a Huber Robust Linear Tree huber_tree = ModelTree( leaf="linear", loss="huber", huber_delta=1.345, max_depth=4, ) huber_tree.fit(X_train, y_train) pred_huber = huber_tree.predict(X_test) ``` !!! tip "Related guides" - [Distributional & Censored Leaf Families](../concepts/distributional-leaves.md) for full likelihood derivations on Tweedie and Tobit models. - [Solvers, Optimizers & Real-Data Tuning](../concepts/solvers-and-optimizers.md) for proximal gradient and coordinate descent solver convergence profiles. - [Core Tree Architecture & Growth Knobs](../concepts/core-tree-architecture.md) for details on tree topology, depth, and pruning controls. ## Full output Everything the script prints, captured from a real run. Fit times depend on your machine; error metrics are fixed by the seeds in the script. !!! example "Output of `benchmarks/suites/regression_methods_showcase.py`" ```text --8<-- "docs/snippets/outputs/regression_methods_showcase.txt" ``` --- ## Document: piecewise-linear-and-cubist.md --- title: "Piecewise Linear Model Trees & Quinlan M5/Cubist Rules" description: "Why piecewise linear model trees outperform standard gradient boosted trees (GBDT) on continuous manifolds: local regression leaves, Quinlan M5 smoothing, and Cubist rule extraction in pure Rust." keywords: - piecewise linear decision trees - Cubist model tree - Quinlan M5 tree - model tree vs GBDT - local linear leaves --- # Piecewise Linear Model Trees & Quinlan M5/Cubist Rules Standard gradient boosting libraries (LightGBM, XGBoost, CatBoost) and classical CART trees share a fundamental architectural limitation: **they approximate smooth continuous response surfaces using orthogonal step functions (piecewise constant leaves).** Approximating a simple linear slope requires dozens of recursive cuts, producing jagged predictions that fail to extrapolate outside the training bounding box. `mltreelib.ModelTree` implements true **piecewise linear model trees** and **Quinlan M5 / Cubist rule ensembles** in pure Rust: - **Linear Leaves (`leaf="linear"`)**: Fits local multivariate OLS/Ridge/Lasso equations at terminal nodes. - **Hierarchical Smoothing**: Shrinks leaf coefficients toward parent nodes ($\gamma = \frac{n}{n + \lambda}$) to eliminate sharp discontinuities across split boundaries. - **Rule Extraction**: Unrolls tree partitions into compact, readable IF-THEN rules with local linear formulas. ```mermaid flowchart LR A["Raw Predictors X"] --> B["Recursive Partitioning\nMinimizing Local Residual Variance"] B --> C["Node Partition 1\ny = 14.2 + 0.85*x1 - 1.2*x2"] B --> D["Node Partition 2\ny = 8.1 + 2.10*x1 + 0.4*x2"] B --> E["Node Partition 3\ny = 31.5 - 0.40*x1 + 3.8*x2"] C --> F["Hierarchical Leaf Shrinkage\nShrink toward Ancestor Equation"] D --> F E --> F F --> G["Smooth Continuous Prediction\nExtrapolates Linear Trends"] classDef blueBox fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#ffffff; classDef greenBox fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#ffffff; classDef grayBox fill:#334155,stroke:#64748b,stroke-width:2px,color:#ffffff; class A,B grayBox; class C,D,E blueBox; class F,G greenBox; ``` --- ## 1. Step Functions vs Linear Leaves (The Diamond Price Benchmark) The table below comes from a separate benchmark on real diamond sales ([OpenML 42225](https://www.openml.org/d/42225)). It compares a standard 0D constant CART tree, a 100-tree GBDT ensemble, and a shallow `ModelTree` (`max_depth=3`, 8 linear leaves). That benchmark is not regenerated on this page. The runnable block under the table is a different, synthetic setup: one 2,000-row price surface, 1,500 rows for training and 500 for testing, with one `ModelTree`. Its test RMSE is not comparable with the table, because the data, sample sizes and models differ. ![Decision Tree Step Functions vs Continuous Linear Leaves](../assets/plot_tree_regression_comparison.png) ![Local Linear Planes Across Diamond Partitions](../assets/guides/real-diamonds-leaves.svg) ### Benchmark Results | Model Architecture | Leaf Type | Tree Depth / Ensembles | Test RMSE | Interpretation | | :--- | :--- | :---: | :---: | :--- | | **CART Baseline** | 0D Constant Mean | Depth 6 (64 leaves) | $1,280 | Jagged staircases; zero extrapolation | | **GBDT (LightGBM)** | 0D Constant Sum | 100 Trees (Depth 4) | $742 | Hundreds of orthogonal step boundaries | | **`ModelTree` (mltreelib)** | **1D Linear OLS** | **Depth 3 (8 leaves)** | **$695** | **Smooth continuous planes on the real diamond benchmark (not reproduced by the block below)** | ```python import numpy as np from mltreelib import ModelTree, Metrics # 1. Generate non-linear diamond price surface with carat and cut interactions rng = np.random.default_rng(2026) n_samples = 2000 carat = rng.uniform(0.3, 3.5, n_samples) depth = rng.uniform(58.0, 65.0, n_samples) table = rng.uniform(53.0, 62.0, n_samples) # Price surface with non-linear exponential carat scaling price = 1500.0 * (carat ** 1.8) - 15.0 * depth + rng.normal(0, 150.0, n_samples) X = np.column_stack([carat, depth, table]) feature_names = ["carat", "depth", "table"] train_idx = range(1500) test_idx = range(1500, 2000) # 2. Train shallow ModelTree with piecewise linear leaves tree = ModelTree( leaf="linear", max_depth=3, min_rows=30, l2=1e-3, shrinkage=5.0, # Hierarchical leaf shrinkage ) tree.fit(X[train_idx], price[train_idx].tolist(), col_names=feature_names) pred = tree.predict(X[test_idx]) rmse = Metrics().regression(price[test_idx].tolist(), pred)["rmse"] print(f"Piecewise Linear ModelTree Test RMSE: ${rmse:.2f}") ``` ```text Piecewise Linear ModelTree Test RMSE: $155.99 ``` --- **Next:** turn the fitted linear leaves into IF-THEN rules, because the M5 and Cubist step is what makes each leaf's equation auditable. ## 2. Quinlan M5 & Cubist Rule Extraction (Concrete Compressive Strength) On industrial concrete mixtures ([OpenML 4353](https://www.openml.org/d/4353)), local chemistry and curing age interact continuously. Quinlan's M5 tree architecture partitions the design space and converts leaves into auditable linear rules. The block below fits a small linear-leaf tree on the local Concrete parquet (800 shuffled rows) and extracts its rules. ![Concrete Compressive Strength Decision Tree](../assets/guides/real-concrete-tree.svg) ![Concrete Strength Local Linear Leaf Planes](../assets/guides/real-concrete-leaves.svg) ### Inspecting Extracted Rules and Leaf Equations ```python import pandas as pd from mltreelib import ModelTree from mltreelib import dataset_path df = pd.read_parquet(dataset_path("concrete_compressive_strength")).sample(frac=1, random_state=2026) target = "ConcreteCompressiveStrength" features = df.columns.drop(target).tolist() concrete_tree = ModelTree(leaf="linear", max_depth=3, min_rows=40) concrete_tree.fit(df[features].iloc[:800].to_numpy().tolist(), df[target].iloc[:800].tolist(), col_names=features) # Extract white-box rules directly from the fitted tree ruleset = concrete_tree.extract_rules() print(f"Extracted {len(ruleset.rules)} Transparent Rules:") for r in ruleset.rules: print(f" RULE #{r.id} (Cover: {r.cover} samples, Confidence: {r.confidence:.2f}):") print(f" IF {' AND '.join(r.conditions)}") print(f" THEN Leaf #{r.leaf_id} prediction = {r.prediction:.2f}") ``` ```text Extracted 7 Transparent Rules: RULE #1 (Cover: 333 samples, Confidence: 0.95): IF Cement > 164.6000 AND Cement <= 350.0000 AND Age > 14.0000 THEN Leaf #4 prediction = 39.96 RULE #2 (Cover: 134 samples, Confidence: 0.95): IF Cement <= 350.0000 AND Age <= 7.0000 THEN Leaf #0 prediction = 16.11 RULE #3 (Cover: 102 samples, Confidence: 0.95): IF Cement <= 164.6000 AND Age > 14.0000 THEN Leaf #3 prediction = 25.93 RULE #4 (Cover: 74 samples, Confidence: 0.95): IF Cement > 350.0000 AND Age <= 14.0000 THEN Leaf #2 prediction = 34.80 RULE #5 (Cover: 68 samples, Confidence: 0.95): IF Cement > 350.0000 AND Water <= 181.1000 AND Age > 14.0000 THEN Leaf #5 prediction = 63.13 RULE #6 (Cover: 48 samples, Confidence: 0.95): IF Cement > 350.0000 AND Water > 181.1000 AND Age > 14.0000 THEN Leaf #6 prediction = 44.97 RULE #7 (Cover: 41 samples, Confidence: 0.95): IF Cement <= 350.0000 AND Age > 7.0000 AND Age <= 14.0000 THEN Leaf #1 prediction = 27.85 ``` --- **Next:** shrink leaf coefficients toward the parent node, because the smallest extracted rule (Rule 7, 41 samples) falls in the N < 50 range, where unregularized OLS has high variance. ## 3. Hierarchical Leaf Shrinkage In small leaf partitions ($N < 50$), unregularized OLS fits are vulnerable to high variance. `mltreelib` stabilizes leaf parameters by shrinking them toward the parent node's equation: $$\hat{\boldsymbol{\beta}}_\text{leaf}^* = \gamma \hat{\boldsymbol{\beta}}_\text{leaf} + (1 - \gamma) \hat{\boldsymbol{\beta}}_\text{parent}, \qquad \gamma = \frac{n}{n + \lambda}$$ Setting `shrinkage=10.0` prevents overfitting in sparse leaves while retaining sharp local adaptation in data-rich regimes. !!! tip "Related guides" - [Comprehensive Regression Showcase](../benchmarks/regression-methods.md) for 4-study comparisons with Huber, NNLS, GAM, and GLMs. - [Core Tree Architecture](core-tree-architecture.md) for split optimization proofs. --- ## Document: distributional-leaves.md # Distributional & Censored Leaf Families Standard regression trees assume homoscedastic Gaussian errors: $Y \sim \mathcal{N}(\mu, \sigma^2)$ with identity link. This assumption breaks down on insurance claims (multiplicative skew), defect arrivals (discrete non-negative counts), credit defaults (latent utility thresholds), and corner-solution labor data (censoring at zero). `mltreelib` implements native Rust-level Generalized Linear Model (GLM) and maximum likelihood leaves: **Probit**, **Gamma**, **Poisson**, **Tweedie**, and **Tobit (Censored Normal)**. Script: [`benchmarks/suites/distributional_leaf_benchmarks.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/distributional_leaf_benchmarks.py) (`uv run python benchmarks/suites/distributional_leaf_benchmarks.py`). --- ## 1. Mathematical Formulation of Distributional Families ```mermaid flowchart LR Target["Target Response Variable (Y)"] --> Support{"Support & Distributional Shape"} Support -- "Binary Threshold [0, 1]" --> Probit["Probit Leaf\ng(mu) = Phi^-1(mu)\nLatent Normal Utility"] Support -- "Strictly Positive Multiplicative [Y > 0]" --> Gamma["Gamma Leaf\ng(mu) = log(mu)\nVar(Y) proportional to mu^2"] Support -- "Non-Negative Discrete Counts [0, 1, 2, ...]" --> Poisson["Poisson Leaf\ng(mu) = log(mu)\nNo Negative Predictions"] Support -- "Zero-Inflated Continuous [Y >= 0]" --> Tweedie["Tweedie Compound Poisson\np in (1, 2)\nExact Pure Premium"] Support -- "Corner Solutions & Censored [Y >= c]" --> Tobit["Tobit Censored MLE\ny* = Xb + e, y = max(y*, c)\nCorrects OLS Attenuation"] classDef targetBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff; classDef gateBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef glmBox fill:#1e3a5f,stroke:#2b4c7e,stroke-width:2px,color:#ffffff; classDef mleBox fill:#065f46,stroke:#059669,stroke-width:2px,color:#ffffff; class Target targetBox; class Support gateBox; class Probit,Gamma,Poisson,Tweedie glmBox; class Tobit mleBox; ``` ### A. Probit Leaf Family (Binary Latent Utility) Models binary outcomes $Y_i \in \{0, 1\}$ through a latent Gaussian utility $Y_i^* = \mathbf{x}_i^T \boldsymbol{\beta} + \epsilon_i$ with $\epsilon_i \sim \mathcal{N}(0, 1)$: $$P(Y_i = 1 \mid \mathbf{x}_i) = \Phi(\mathbf{x}_i^T \boldsymbol{\beta}) = \int_{-\infty}^{\mathbf{x}_i^T \boldsymbol{\beta}} \frac{1}{\sqrt{2\pi}} e^{-z^2/2} dz$$ The score equations are solved via Iteratively Reweighted Least Squares (IRLS) in pure Rust, generating calibrated probabilities that match classical econometric specifications. ### B. Gamma Leaf Family (Multiplicative Skew) For strictly positive continuous outcomes ($Y > 0$) where variance scales quadratically with the mean ($\text{Var}(Y) = \phi \mu^2$): $$\log(\mu_i) = \mathbf{x}_i^T \boldsymbol{\beta} \implies \mu_i = \exp(\mathbf{x}_i^T \boldsymbol{\beta})$$ Unit deviance minimized by the local leaf solver: $$d(y_i, \mu_i) = 2 \left[ -\log\left(\frac{y_i}{\mu_i}\right) + \frac{y_i - \mu_i}{\mu_i} \right]$$ Coefficients act as multiplicative percentage shifts: a coefficient $\beta_j$ scales the expected outcome by $\exp(\beta_j)$. ### C. Poisson Leaf Family (Non-Negative Counts) For event frequencies $Y_i \in \{0, 1, 2, \dots\}$: $$\log(\mu_i) = \mathbf{x}_i^T \boldsymbol{\beta}, \quad P(Y_i = y \mid \mathbf{x}_i) = \frac{\mu_i^y e^{-\mu_i}}{y!}$$ Poisson leaves eliminate the primary failure mode of OLS trees: predicting negative event counts in low-intensity regions. ### D. Tobit Leaf Family (Censored Normal Corner Solutions) When a continuous response is censored below threshold $c$ (e.g., hours worked or spending censored at zero): $$y_i^* = \mathbf{x}_i^T \boldsymbol{\beta} + \epsilon_i, \quad \epsilon_i \sim \mathcal{N}(0, \sigma^2), \quad y_i = \max(y_i^*, c)$$ Standard Ordinary Least Squares (OLS) suffers from severe downward **attenuation bias** because it treats corner zeros as uncensored linear observations. The Tobit leaf maximizes the exact censored log-likelihood: $$\ln L = \sum_{y_i = c} \ln\left[ 1 - \Phi\left(\frac{\mathbf{x}_i^T \boldsymbol{\beta} - c}{\sigma}\right) \right] + \sum_{y_i > c} \left[ -\ln(\sigma) + \ln \phi\left(\frac{y_i - \mathbf{x}_i^T \boldsymbol{\beta}}{\sigma}\right) \right]$$ The solver applies Newton-Raphson with the inverse Mills ratio $\lambda(\alpha) = \frac{\phi(\alpha)}{1 - \Phi(\alpha)}$, recovering unbiased structural parameters. --- **Next:** fit four of these families (Probit, Gamma, Tweedie and Tobit) on real benchmark datasets, because the formulas above define each leaf's target but do not show how it performs on data. ## 2. Empirical Benchmark Suite Results We evaluate all four families on canonical domain benchmarks using [`benchmarks/suites/distributional_leaf_benchmarks.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/distributional_leaf_benchmarks.py). ### A. Probit on German Credit (1,000 Loan Applicants) - **Problem**: Credit default risk modeling $P(\text{Default} = 1)$. - **Empirical Results**: Identifies 4 macro risk segments. Leaf 0 ($n=180$, high risk) estimates installment rate sensitivity $\beta = +0.1931$ and duration sensitivity $\beta = +0.0502$. Leaf 3 ($n=112$, low risk) achieves baseline intercept $-2.5390$, mapping to a default risk under 3%. ### B. Gamma on US Healthcare Expenses (1,338 Policyholders) - **Problem**: Positive healthcare charges ($Y > 0$, Mean = $13,270.42). - **Empirical Results**: Tree partitions into 4 cost regimes. Base expected costs scale from **$1,745.48** (Leaf 0, low-risk young) to **$20,960.24** (Leaf 3, severe risk). - **Multiplicative Coefficients**: Age contributes a $+0.0294$ log-slope (**1.03x multiplier per year of age**); additional children contribute $+0.1609$ (**1.17x cost multiplier per child**). ### C. Tweedie on Fair's Extramarital Affairs (601 Observations) - **Problem**: Annual affair frequency count with point mass at zero (75.0% zero clump). - **Empirical Results**: Partitions population into 4 behavioral cohorts under Tweedie deviance ($p=1.5$). In Leaf 0 ($n=27$), religiousness contributes $\beta = -0.0612$, and marriage rating contributes $\beta = +0.9711$. In the largest cohort (Leaf 2, $n=288$), religiousness contributes $\beta = -0.2747$ and rating $\beta = -0.4071$. No negative event counts are predicted. ### D. Tobit on Mroz Female Labor Force Participation (753 Women) - **Problem**: Annual hours worked (43.2% of women report 0 hours; corner solution at 0). - **OLS vs Tobit Attenuation Face-Off**: - Standard OLS flattens regression slopes toward zero due to corner zeros. - In Leaf 0 ($n=233$), OLS attenuation is 39.7% on young children (`kidslt6`), 50.7% on non-wife income (`nwifeinc`), and 92.2% on experience (`exper`). - In Leaf 1 ($n=201$), OLS attenuation on experience is 60.8%. - **Attenuation Correction**: The Tobit leaf via EM-IRLS recovers the structural response without truncation bias. | Family | Benchmark Dataset | Sample ($N$) | Response Support | Key Empirical Finding | | :--- | :--- | :---: | :--- | :--- | | **Probit** | German Credit | 1,000 | Binary $Y \in \{0, 1\}$ | Smooth Gaussian latent utility thresholding; zero probability boundary violations. | | **Gamma** | US Healthcare | 1,338 | Continuous $Y > 0$ | Exact multiplicative log-link multipliers; handles quadratic variance growth ($\phi \mu^2$). | | **Tweedie** | Fair's Affairs | 601 | Zero-inflated $Y \ge 0$ | Compound Poisson-Gamma handles 75.0% zero mass and positive tail simultaneously. | | **Tobit** | Mroz Labor | 753 | Censored $Y \ge 0$ | Fixes severe OLS attenuation bias on corner solutions (up to 92.2% slope flattening on experience). | ![Leaf Family Comparison](../assets/leaf_family_comparison.png) --- **Next:** call the same families from Python, because the benchmark results above come from a script and `leaf_family` is how a user picks a family on a `ModelTree`. ## 3. Python Usage Example ```python from mltreelib import ModelTree # 1. Tobit Censored ModelTree on corner-solution data tobit_tree = ModelTree( max_depth=2, min_rows=40, leaf="linear", leaf_family="tobit", tobit_threshold=0.0, ) tobit_tree.fit(X_train, y_hours, col_names=feature_names) pred_latent = tobit_tree.predict(X_test) # 2. Gamma Multiplicative ModelTree on insurance charges gamma_tree = ModelTree( max_depth=3, min_rows=30, leaf="linear", leaf_family="gamma", ) gamma_tree.fit(X_train, y_charges, col_names=feature_names) pred_charges = gamma_tree.predict(X_test) ``` --- **Next:** the worked example below generates its own data and runs the Tweedie, Tobit and heteroscedastic options, because the Python snippet above uses `X_train`, `y_hours` and `y_charges` without defining them. ## 4. Worked example: Tweedie, Tobit and heteroscedastic trees Classical decision trees minimize squared error (SSE) under the implicit assumption of homoscedastic Gaussian noise. Real-world business outcomes routinely violate this assumption: - **Zero-Mass & Positive Skew**: Insurance claim amounts, operational losses, and customer lifetime spend have a point mass at zero and heavy positive tails. - **Censored Boundaries**: Sensor readings, wage hours, and follow-up clinical durations are truncated or floored at a boundary $c$. Standard OLS suffers severe **attenuation bias** towards zero. - **Regime Volatility Shifts**: Financial returns, algorithmic latencies, and machine vibrations exhibit shifts in variance with zero change in the conditional mean ($\Delta \mu = 0$). `mltreelib` supports generalized distributional leaves and split gains directly in Rust: ```mermaid flowchart TD A["Observed Response Distribution y"] --> B{"Distributional Characteristics"} B -- "Zero-Inflated Continuous (y >= 0)" --> C["Tweedie Quasi-Deviance\nVar(μ) = μ^p (1 < p < 2)"] B -- "Floored or Capped Boundary (y >= c)" --> D["Tobit Latent Censoring\nInverse Mills Ratio Attenuation Fix"] B -- "Pure Volatility Shifts (Delta μ = 0)" --> E["Heteroscedastic Gaussian NLL\nDeviance = W * ln(sigma^2)"] B -- "Bounded Rates y in (0, 1)" --> F["Beta Regression Tree\nLogit link with precision phi"] C --> G["ModelTree(gain='tweedie', tweedie_p=1.5)"] D --> H["ModelTree(leaf_family='tobit', tobit_threshold=0.0)"] E --> I["ModelTree(gain='heteroscedastic')"] F --> J["ModelTree(leaf_family='beta')"] classDef blueBox fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#ffffff; classDef greenBox fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#ffffff; classDef grayBox fill:#334155,stroke:#64748b,stroke-width:2px,color:#ffffff; class A,B grayBox; class C,D,E,F blueBox; class G,H,I,J greenBox; ``` ![Distributional Tree Benchmarks across Tweedie, Tobit, and Censored Datasets](../assets/distributional_leaf_benchmarks.png) --- ### 1. Zero-Inflated Skew: Tweedie Quasi-Deviance Splitting On Miami Housing sales ([OpenML 46942](https://www.openml.org/d/46942)), positive home prices vary with property size and ocean proximity. Tweedie splitting scales variance with mean power $V(\mu) = \mu^p$ ($1 < p < 2$), prioritizing proportional relative accuracy over absolute error: $$d(y, \mu) = 2 \left( \frac{y^{2-p}}{(1-p)(2-p)} - \frac{y \mu^{1-p}}{1-p} + \frac{\mu^{2-p}}{2-p} \right)$$ ![Miami Housing Price Split Comparison: Tweedie vs Squared Error](../assets/guides/real-housing-splits.svg) #### Complete Tweedie Quasi-Deviance Example ```python import numpy as np from mltreelib import ModelTree, Metrics # 1. Generate zero-inflated claim amounts (Poisson frequency x Gamma severity) rng = np.random.default_rng(2026) n_policies = 2000 driver_age = rng.uniform(18.0, 75.0, n_policies) vehicle_age = rng.uniform(0.0, 15.0, n_policies) mileage = rng.uniform(5.0, 35.0, n_policies) # Event probability (claim frequency) p_claim = 1.0 / (1.0 + np.exp(2.0 - 0.04 * mileage + 0.02 * driver_age)) has_claim = rng.binomial(1, p_claim) # Claim amount given claim occurs (Gamma severity) claim_severity = rng.gamma(shape=2.5, scale=1200.0, size=n_policies) y_claims = np.where(has_claim == 1, claim_severity, 0.0) X = np.column_stack([driver_age, vehicle_age, mileage]) feature_names = ["driver_age", "vehicle_age", "mileage"] # 2. Fit ModelTree with Tweedie quasi-deviance (p=1.5 compound Poisson-Gamma) tweedie_tree = ModelTree( gain="tweedie", tweedie_p=1.5, leaf="mean", max_depth=3, min_rows=40, ) tweedie_tree.fit(X, y_claims.tolist(), col_names=feature_names) pred_claim_expected = tweedie_tree.predict(X) print(f"Zero-Claim Prevalence: {(y_claims == 0).mean():.1%}") print(f"Mean Observed Claim: ${y_claims.mean():.2f} | Mean Predicted: ${np.mean(pred_claim_expected):.2f}") print("Top Tree Split:", tweedie_tree.splits()[0]) ``` ```text Zero-Claim Prevalence: 89.1% Mean Observed Claim: $329.66 | Mean Predicted: $329.66 Top Tree Split: {'col': 'vehicle_age', 'threshold': 11.611, 'missing_left': True, 'gain': 884.137993982089} ``` --- ### 2. Tobit Regression Leaves: Eliminating Attenuation Bias When a response variable is censored at threshold $c$ (e.g. non-working hours in the Mroz labor survey where 43.2% of women report hours $= 0$), standard OLS underestimates the true latent slopes by **33% to 46%** (attenuation bias). The Tobit model assumes a latent variable $y^* = \mathbf{x}^\top \boldsymbol{\beta} + \epsilon$ with observed outcome $y = \max(y^*, c)$. `mltreelib` fits Tobit leaves using pure Rust EM/IRLS iterations: $$\ln L = \sum_{y_i = c} \ln \left[ 1 - \Phi\left(\frac{\mathbf{x}_i^\top \boldsymbol{\beta} - c}{\sigma}\right) \right] + \sum_{y_i > c} \left[ -\frac{1}{2}\ln(2\pi\sigma^2) - \frac{(y_i - \mathbf{x}_i^\top \boldsymbol{\beta})^2}{2\sigma^2} \right]$$ #### Synthetic Attenuation Correction Proof ```python import numpy as np from mltreelib import ModelTree # 1. Generate latent variable y* with zero-censoring rng = np.random.default_rng(2026) n_workers = 1200 education = rng.uniform(8.0, 18.0, n_workers) experience = rng.uniform(0.0, 30.0, n_workers) # True latent relationship (market wage / labor supply hours) true_latent = -15.0 + 2.4 * education + 1.2 * experience + rng.normal(0, 8.0, n_workers) observed_hours = np.maximum(0.0, true_latent) X = np.column_stack([education, experience]) feature_names = ["education", "experience"] # 2. Fit piecewise Tobit tree with censoring at zero tobit_tree = ModelTree( leaf="linear", leaf_family="tobit", tobit_threshold=0.0, # Censored boundary floor max_depth=2, min_rows=40, ) tobit_tree.fit(X, observed_hours.tolist(), col_names=feature_names) pred_latent = tobit_tree.predict(X) print(f"Censored Sample Fraction: {(observed_hours == 0).mean():.1%}") print(f"Sample Latent Prediction Range: [{min(pred_latent):.2f}, {max(pred_latent):.2f}]") ``` ```text Censored Sample Fraction: 0.5% Sample Latent Prediction Range: [6.80, 63.92] ``` --- ### 3. Heteroscedastic Splitting: Detecting Risk & Volatility Breaks Standard decision trees fail to split when two subgroups have identical means ($\mu_L = \mu_R$) but different variance regimes ($\sigma_L^2 \ne \sigma_R^2$). The heteroscedastic split criterion evaluates Gaussian negative log-likelihood deviance directly: $$\text{Gain}_\text{Hetero} = W_P \ln \hat{\sigma}_P^2 - \left[ W_L \ln \hat{\sigma}_L^2 + W_R \ln \hat{\sigma}_R^2 \right]$$ ```python import numpy as np from mltreelib import ModelTree # 1. Generate regimes with identical means (0.0) but distinct volatility regimes rng = np.random.default_rng(2026) n_points = 1000 regime_indicator = rng.uniform(0.0, 100.0, n_points) noise_scale = np.where(regime_indicator > 50.0, 5.0, 0.5) returns = rng.normal(loc=0.0, scale=noise_scale, size=n_points) X = regime_indicator.reshape(-1, 1) # 2. Fit tree with heteroscedastic splitting volatility_tree = ModelTree( gain="heteroscedastic", max_depth=2, min_rows=30, ) volatility_tree.fit(X, returns.tolist(), col_names=["volatility_driver"]) print("Discovered Volatility Split Threshold:", volatility_tree.splits()[0]["threshold"]) ``` ```text Discovered Volatility Split Threshold: 52.922 ``` !!! tip "Related guides" - [Survival & Competing Risks Trees](survival-trees.md) for right-censored time-to-event outcomes. --- ## Domain Limitations & Operational Boundaries 1. **Tobit Normality Sensitivity**: Tobit maximum likelihood assumes normal errors $\epsilon_i \sim \mathcal{N}(0, \sigma^2)$ in the latent space. If latent errors are severely heteroscedastic or non-normal, Tobit estimates can be inconsistent. 2. **Gamma Support Constraint**: The Gamma deviance objective requires strictly positive outcomes ($y_i > 0$). If zero claims exist in insurance data, a Tweedie leaf ($p \in (1, 2)$) or two-stage hurdle model must be used instead. 3. **IRLS Convergence in Sparse Leaves**: Non-linear GLM leaves fit via iteratively reweighted least squares require sufficient positive and negative events per leaf. Set `min_rows >= 25` to prevent separation divergence. --- --- ## Document: uplift.md # Uplift modeling in Python: the Hillstrom email experiment Decide whether to send a campaign email to everyone, a subset, or nobody. Compare incremental visits on a randomized holdout. ## 1. Get the data and define the evaluation The [Hillstrom email experiment](https://www.tensorflow.org/datasets/catalog/hillstrom) randomly assigned 64,000 customers to a men's email, women's email or no email. This example compares **women's email versus no email**, using a website visit within two weeks as the outcome. It excludes the men's arm and uses only pre-treatment features. A visit is not a purchase or profit. `dataset_path("hillstrom_email")` downloads the CSV from the [publisher's MineThatData page](https://blog.minethatdata.com/2008/03/minethatdata-e-mail-analytics-and-data.html) on first use and caches it under `~/.cache/mltreelib/datasets/` (or `MLTREELIB_DATA_DIR`). The publisher's HTTPS endpoint fails certificate validation, so the file is fetched over plain HTTP and checked against SHA-256 `0e5893329d8b93cefecc571777672028290ab69865718020c78c7284f291aece`. ```bash uv run python examples/guides/real_survival_uplift.py uplift ``` **Next:** fit a causal tree on the randomized training rows from step 1, and use the validation rows to choose the email threshold while the test outcomes stay sealed. ## 2. Fit a causal tree and choose an email policy Use treatment as part of the target. The columns below describe customers before the campaign. Use validation to choose a threshold. Keep the randomized test outcomes sealed until after the policy is fixed. ```mermaid flowchart TD subgraph HonestSplit["Honest Causal Partitioning (Athey & Imbens 2016)"] direction TB Sample["Randomized Trial Data (Y, W, X)"] --> Partition{"Random Subsample Split"} Partition -->|"Structure Sample S_tr"| TreeStructure["Tree Split Optimization\nmax Delta tau(s)"] Partition -->|"Estimation Sample S_est"| LeafEstimator["Leaf CATE Estimation\ntau_hat = E[Y | W=1] - E[Y | W=0]"] TreeStructure --> FixedNodes["Fixed Partition Topology"] FixedNodes & LeafEstimator --> UnbiasedModel["Unbiased Honest Causal Tree"] end classDef dataBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff; classDef gateBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef treeBox fill:#4338ca,stroke:#6366f1,stroke-width:2px,color:#ffffff; classDef leafBox fill:#065f46,stroke:#059669,stroke-width:2px,color:#ffffff; classDef causalBox fill:#0f766e,stroke:#14b8a6,stroke-width:2px,color:#ffffff; class Sample dataBox; class Partition gateBox; class TreeStructure,FixedNodes treeBox; class LeafEstimator leafBox; class UnbiasedModel causalBox; ``` Run this Python block from the repository root. `group_value` comes from the example script. ```python from math import sqrt from random import Random import pandas as pd from mltreelib import ModelTree, dataset_path def group_value(y, w, selected): """Incremental value of the selected group: (value, n, n_treated, n_control, 95% CI).""" a = [v for v, t, s in zip(y, w, selected) if s and t == 1] b = [v for v, t, s in zip(y, w, selected) if s and t == 0] n = sum(selected) if not n: return 0.0, 0, 0, 0, None if len(a) < 2 or len(b) < 2: raise ValueError("Selected group requires at least two observations per arm") diff, q = sum(a) / len(a) - sum(b) / len(b), n / len(y) def var(v): m = sum(v) / len(v) return sum((z - m) ** 2 for z in v) / (len(v) - 1) se = q * sqrt(var(a) / len(a) + var(b) / len(b)) return q * diff, n, len(a), len(b), (q * diff - 1.96 * se, q * diff + 1.96 * se) df = pd.read_csv(dataset_path("hillstrom_email")) df = df[df.segment.isin(["Womens E-Mail", "No E-Mail"])] features = ["recency", "history", "mens", "womens", "newbie", "zip_code", "channel"] types = ["numeric"] * 5 + ["categorical"] * 2 ids = list(range(len(df))) Random(2026).shuffle(ids) a, b = int(.6 * len(ids)), int(.8 * len(ids)) def inputs(indices): part = df.iloc[indices] x = [[None if pd.isna(v) else v for v in row] for row in part[features].itertuples(index=False, name=None)] return x, part.visit.tolist(), (part.segment == "Womens E-Mail").astype(int).tolist() x_train, y_train, w_train = inputs(ids[:a]) x_val, y_val, w_val = inputs(ids[a:b]) x_test, y_test, w_test = inputs(ids[b:]) tree = ModelTree(gain="causal", leaf="mean", max_depth=2, min_rows=250, prune="none", honesty=True, seed=42) tree.fit(x_train, (y_train, w_train), col_names=features, dtypes=types) val_scores = tree.predict(x_val, kind="uplift") def value_at(threshold): return group_value(y_val, w_val, [score > threshold for score in val_scores])[0] threshold = max([min(val_scores) - 1, *sorted(set(val_scores))], key=value_at) test_scores = tree.predict(x_test, kind="uplift") for selected in ([score > threshold for score in test_scores], [True] * len(y_test), [False] * len(y_test)): print(group_value(y_test, w_test, selected)) ``` The helper estimates the visit-rate difference between treatment and control among selected customers. It multiplies that difference by the selected fraction. The result measures incremental visits per eligible customer. Check arm support in each candidate group. If the treatment has a cost, use net value in threshold selection and reporting. **Next:** report the held-out policy values for the threshold chosen in step 2, next to treat-all and treat-none. ## 3. Interpret the held-out results The seeded split contains 25,615 training, 8,539 validation and 8,539 test rows. An honest causal tree ranks customers. Validation selects the policy threshold, and the randomized test set estimates incremental visits. This example assumes zero treatment cost because campaign costs are not in the data. | Test policy | Selected | Treated / control | Incremental visits per eligible customer | Approximate 95% interval | |---|---:|---:|---:|---:| | Validation-selected policy | 8,539 | 4,270 / 4,269 | 0.04517 | 0.03108 to 0.05927 | | Treat all | 8,539 | 4,270 / 4,269 | 0.04517 | 0.03108 to 0.05927 | | Treat none | 0 | — | 0 | — | ![Hillstrom randomized held-out policy comparison](../assets/guides/real-uplift.svg) The chosen policy treats everyone. This run therefore supplies **no evidence that targeting improves on treat-all**. The interval is a normal approximation conditional on the selected population and excludes model-selection uncertainty. The data is from a historical campaign; repeat evaluation in a new experiment, include costs and use the outcome the business actually values before applying a policy. **Next:** check the same kind of policy comparison on a simulated trial, because the Hillstrom data cannot show both potential outcomes for each customer. ## 4. Controlled check with synthetic truth The [survival and uplift evaluation script](https://github.com/jkapila/mltreelib/blob/main/examples/guides/survival_uplift_evaluation.py) simulates a randomized trial. It uses 3,000 training, 1,000 validation and 2,000 independent test observations. It selects a threshold on validation data and evaluates the fixed policy on test data. The example assigns a treatment cost of 0.5 outcome units per treated person. ```bash uv run python examples/guides/survival_uplift_evaluation.py ``` | Test policy | Selected | Treated / control among selected | Estimated net outcome per person | |---|---:|---:|---:| | Selected policy | 967 | 471 / 496 | 0.7072 | | Treat all | 2,000 | 972 / 1,028 | -0.0478 | | Treat none | 0 | 0 / 0 | 0 | ![Held-out randomized policy comparison](../assets/guides/uplift-evaluation.svg) Within the selected subgroup, the script compares observed treated and control outcomes. It subtracts the cost and multiplies by the selected fraction to report value per person in the whole test population. The selected policy's approximate 95% interval is [0.6438, 0.7706]. It uses a normal approximation conditional on the selected fraction; it does not include model-selection uncertainty. Randomized assignment and independent test data support this comparison. Check arm counts and use a suitable uncertainty method for the actual design. Synthetic truth is available here: the selected policy's true net value is 0.7252 per person and PEHE is 0.2347. Real records do not reveal both potential outcomes, so individual-effect error and PEHE cannot be calculated directly from ordinary real-world outcomes. These calculations are example code, not a claim that the package already exposes uplift evaluation metrics. **Next:** the worked example also uses a simulation with a known effect, to check numerical effect recovery, which real Hillstrom outcomes cannot provide. --- ## 5. Worked example: recovering a known treatment effect Use simulation to check numerical effect recovery. The Hillstrom sections above show how to evaluate a real randomized policy when individual effects are unknown. ![Expected net outcome of three policies in the controlled randomized simulation](../assets/guides/uplift-evaluation.svg) Standard machine learning models optimize predictive accuracy: $\hat{Y} = E[Y | X]$. In decision sciences, personalized medicine, and targeted marketing, the central quantity is not outcome prediction but **intervention effect**: $$\tau(x) = E[Y | X=x, W=1] - E[Y | X=x, W=0]$$ `ModelTree` provides native Rust-accelerated **Causal Trees** that split directly on treatment effect divergence without external meta-learners or propensity weighting loops. Script: - [`benchmarks/suites/causal_benchmark.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/causal_benchmark.py) (`python3 benchmarks/suites/causal_benchmark.py`) ```text ====================================================================== 1. CAUSAL TREE ON NIE & WAGER (2021) / CAUSALML SETUP B ====================================================================== Dataset Size: N_train=2000, N_test=1000, P=6 features Causal Tree: Leaves=10, Max Depth=4 True Average Treatment Effect (ATE): 0.8177 Estimated Average Treatment Effect (ATE):0.8030 ATE Absolute Error: 0.0147 PEHE (Heterogeneity Precision): 0.5848 [Pass] Exact Leaf Arithmetic Assertion: All leaf CATEs match raw group differences within 1e-9. ====================================================================== 2. STRUCTURAL CATE STEP RECOVERY & SLEEPING DOG IDENTIFICATION ====================================================================== Left regime (x0 < 0) - True tau = +10.0 | Estimated tau = +10.0095 Right regime (x0 >= 0) - True tau = -5.0 | Estimated tau = -5.1823 [Pass] Exact Leaf Arithmetic Assertion: All leaf CATEs match raw group differences within 1e-9. [Pass] Successfully differentiated persuasive units (+10.0) from sleeping dogs (-5.0). ``` --- ### 1. The Tuple Target Contract Causal inference requires both the observed response $Y_i$ and the binary treatment indicator $W_i \in \{0, 1\}$. Rather than bloating the covariate space $X$, `ModelTree` accepts target tuples: ```python from mltreelib import ModelTree # X: predictive covariates only # y: 2-tuple (outcomes, treatments) y_tuple = (outcomes, treatments) tree = ModelTree(gain="causal", max_depth=3, min_rows=20, leaf="mean") tree.fit(X, y_tuple) ``` --- ### 2. $O(1)$ Causal Splitting Criterion Standard regression trees minimize variance: $\text{Gain} = \Delta \text{SSE}$. Causal trees maximize the squared difference in treatment effects between children (Athey & Imbens 2016): $$\text{Gain}_{\text{Causal}} = \frac{W_L \cdot W_R}{W_L + W_R} (\hat{\tau}_L - \hat{\tau}_R)^2$$ where: $$\hat{\tau}_L = \bar{Y}_{L, \text{treated}} - \bar{Y}_{L, \text{control}}, \quad \hat{\tau}_R = \bar{Y}_{R, \text{treated}} - \bar{Y}_{R, \text{control}}$$ The split search maintains running sums of $(W_i, W_i Y_i)$ for treated and control rows separately. Evaluated in $O(1)$ scalar updates per candidate threshold with zero vector allocations. #### Admissibility Condition A split is admissible only if both children satisfy: $$N_{L, \text{treated}} \ge 1, \quad N_{L, \text{control}} \ge 1, \quad N_{R, \text{treated}} \ge 1, \quad N_{R, \text{control}} \ge 1$$ This permits a treated-minus-control sample difference in each child. It does not establish a causal effect, and one observation per arm is not enough to support a stable decision. A causal interpretation requires assumptions about the data and intervention. Random treatment assignment supports exchangeability. Observational data requires conditional exchangeability given pre-treatment covariates, treatment overlap, and a defensible account of confounding. Consistency and the absence of relevant interference also matter. Honesty reduces reuse of outcomes for split selection and estimation, but it does not remove confounding. See [Athey and Imbens (2016)](https://www.pnas.org/doi/10.1073/pnas.1510489113). Evaluate a targeting policy on an independent randomized holdout. Report treated and control counts by segment, uncertainty, incremental outcomes and treatment costs. Individual treatment effects and PEHE are known in the synthetic benchmark below; they are not observed for real individuals. --- ### 3. Unified Uplift Predictions Predictions remain unified through `predict(X, kind=...)`: ```python # Predict Individual Treatment Effect (CATE / Uplift) cate_preds = tree.predict(X_test, kind="uplift") # or kind="cate" # Predict baseline response baseline_preds = tree.predict(X_test, kind="response") ``` --- ### 4. Benchmark & Numerical Validation On the standard **Nie & Wager (2021) / CausalML Setup B** benchmark: - **Precision in Estimating Heterogeneous Effects (PEHE)**: $$\text{PEHE} = \sqrt{\frac{1}{N}\sum_{i=1}^N (\hat{\tau}_i - \tau_i)^2} = 0.5848$$ - **ATE Estimation Error**: $|0.8177 - 0.8030| = 0.0147$. - **Exact Leaf Verification**: For every terminal leaf, the predicted CATE matches the raw sample difference $\bar{Y}_{1} - \bar{Y}_{0}$ to machine precision ($< 10^{-9}$). --- ### 5. Causal Survival Trees (Research Track) > [!WARNING] > **Research Work Under Consideration** > Causal Survival Trees with Honest Sample Splitting are experimental research. Use at your own risk. In survival settings with right censoring $(T_i, E_i)$ and binary intervention $W_i \in \{0, 1\}$, the target is the Conditional Average Treatment Effect on Restricted Mean Survival Time (RMST): $$\tau_{\text{RMST}}(x) = \mathbb{E}[\text{RMST}(1) - \text{RMST}(0) \mid X=x]$$ #### Honest Sample Splitting Following Wager & Athey (2018) and Cui et al. (2023), `ModelTree(honesty=True, honesty_fraction=0.5)` partitions data into: - **Structure Set $S^{\text{tr}}$**: Used exclusively to place partition boundaries (e.g. maximizing $\Delta \text{RMST}$ or pseudo-value variance reduction). - **Estimation Set $S^{\text{est}}$**: Independent holdout used to fit leaf survival curves and estimate CATE. This honest decoupling removes optimistic selection bias in survival trees: ```python from mltreelib import ModelTree # Honest Causal Survival Tree tree = ModelTree( max_depth=3, min_rows=15, gain="pseudo_value", # or "rmst" honesty=True, honesty_fraction=0.5, seed=42, ) tree.fit(X, (durations, events)) ``` Executable research script: - [`examples/guides/survival_causal_survival_honest.py`](https://github.com/jkapila/mltreelib/blob/main/examples/guides/survival_causal_survival_honest.py) ## Next steps and references [Athey and Imbens (2016)](https://www.pnas.org/doi/10.1073/pnas.1510489113) --- ## Document: anomaly-trees.md --- title: "Anomaly trees: Extended Isolation Forest and friends" description: "How AnomalyModel isolates outliers: oblique-cut Extended Isolation Forest, density estimation trees, half-space and random-cut trees, with benchmarks and limits." keywords: - anomaly detection trees - AnomalyModel - Extended Isolation Forest - isolation forest ghosting --- # Anomaly trees: Extended Isolation Forest and friends Review budgets are limited, so an anomaly model must rank true failures into the top 1% to 5% of a triage queue. Standard isolation forests cut on one feature at a time, which leaves coordinate-axis "ghost" artifacts. `AnomalyModel` is the single entry point for tree-based anomaly detection. Pick the engine with `kind`: - `kind="eif"`: Extended Isolation Forest with random oblique hyperplanes. - `kind="det"`: density estimation tree (piecewise density). - `kind="hst"`: half-space trees for streaming data. - `kind="rct"`: random cut trees. For a task-first walkthrough on real telemetry, see [Anomaly detection in Python](../guides/anomaly.md). To find segments instead, see [Clustering trees](clustering-trees.md). Script: [`benchmarks/suites/unsupervised_trees.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/unsupervised_trees.py). --- ## 1. Mechanics ### Extended Isolation Forest with oblique cuts Standard Isolation Forests isolate points by picking a random feature $j$ and a random threshold $\theta \in [\min x_j, \max x_j]$. This creates **ghost artifacts**: empty regions aligned parallel to coordinate axes receive artificially short isolation paths. Extended Isolation Forest (Hariri et al., 2019) branches using random normal hyperplanes: $$(\mathbf{x} - \mathbf{p})^T \mathbf{n} \le 0$$ - $\mathbf{p} \sim \text{Uniform}(\min \mathbf{X}, \max \mathbf{X})$: A random pivot point in the bounding box. - $\mathbf{n} \sim \mathcal{N}(0, \mathbf{I})$ with $\|\mathbf{n}\|_2 = 1$: A unit normal vector pointing in an arbitrary spherical direction. The anomaly score for sample $\mathbf{x}$ across $T$ trees is: $$s(\mathbf{x}, n) = 2^{-\frac{\mathbb{E}[h(\mathbf{x})]}{c(n)}}$$ where $h(\mathbf{x})$ is the observed isolation tree depth, and $c(n) = 2 \ln(n - 1) + 0.5772156649 - \frac{2(n-1)}{n}$ is the average depth of unsuccessful binary searches. An anomaly score $s \to 1.0$ indicates an unmistakable outlier, while $s < 0.5$ indicates normal inlier space. **Next:** the benchmark tests the oblique cuts from step 1 against axis-aligned cuts on a 2D mixture with 40 outliers. --- ## 2. Benchmark ### Extended IF vs axis-aligned IF on a 2D mixture (600 Inliers, 40 Outliers) - **Problem**: Isolate 40 peripheral anomalies situated around two dense multimodal inlier clusters. - **Empirical Comparison**: - **Axis-Aligned IF**: Fit time = 1.46 ms. Outlier scores range from $0.479$ to $0.590$. Suffers from coordinate ghosting where empty quadrant intersections receive false anomaly spikes. - **Extended Isolation Forest (Oblique Cuts)**: Fit time = **0.39 ms** (3.7x faster). Outlier scores range from $0.597$ to **0.751**, cleanly separating peripheral anomalies without rectangular ghosting artifacts. | Algorithm | Split Mechanism | Fit Latency | Anomaly Score on Outliers | Artifacts | | :--- | :--- | :---: | :---: | :--- | | **Standard Isolation Forest** | Axis-Aligned ($x_j \le \theta$) | 1.46 ms | 0.479 – 0.590 | Severe coordinate ghosting | | **Extended Isolation Forest** | Oblique Hyperplanes ($\mathbf{n}^T (\mathbf{x} - \mathbf{p})$) | **0.39 ms** | **0.597 – 0.751** | **Zero ghosting; radial symmetry** | ![Anomaly detection comparison](../assets/unsupervised_clusters_anomalies.png) **Next:** the Python usage shows the minimal `AnomalyModel(kind="eif")` fit and scoring calls, which the worked example expands into a full triage run. --- ## 3. Python usage ```python from mltreelib import AnomalyModel eif = AnomalyModel(kind="eif", max_depth=8, seed=42) eif.fit(X_telemetry) anomaly_scores = eif.predict_score(X_telemetry) ``` **Next:** the worked example applies the same `kind="eif"` model to labeled telemetry and measures how many of the 60 true anomalies fall in a 5% review budget. --- ## 4. Worked example: triage with `AnomalyModel` In operations, fraud detection, and hardware monitoring, review budgets are strictly limited. Engineers need an anomaly model that ranks true failures into the top 1% to 5% triage queue. `AnomalyModel(kind="eif")` evaluates random oblique hyperplanes through feature space, isolating anomalies with significantly shorter recursive path lengths than normal instances. ![NASA Shuttle Anomaly Review Budget vs Random Baseline](../assets/guides/anomaly-shuttle.svg) ### Complete End-to-End Example ```python import numpy as np from mltreelib import AnomalyModel, Metrics # 1. Generate realistic multi-sensor telemetry with injected failure anomalies rng = np.random.default_rng(2026) n_normal = 2000 n_anomalies = 60 # Normal operational cluster (engine cruise telemetry) normal_sensor1 = rng.normal(loc=120.0, scale=12.0, size=n_normal) normal_sensor2 = rng.normal(loc=85.0, scale=8.0, size=n_normal) normal_vibration = rng.exponential(scale=2.5, size=n_normal) # Injected anomalous sensor drift (bearing failure and thermal spikes) outlier_sensor1 = rng.uniform(low=180.0, high=240.0, size=n_anomalies) outlier_sensor2 = rng.uniform(low=130.0, high=160.0, size=n_anomalies) outlier_vibration = rng.uniform(low=15.0, high=30.0, size=n_anomalies) X = np.column_stack([ np.concatenate([normal_sensor1, outlier_sensor1]), np.concatenate([normal_sensor2, outlier_sensor2]), np.concatenate([normal_vibration, outlier_vibration]), ]) y_true = np.array([0] * n_normal + [1] * n_anomalies) feature_names = ["temp_primary", "temp_secondary", "vibration_rms"] # 2. Instantiate and fit AnomalyModel using oblique hyperplane isolation model = AnomalyModel( kind="eif", # Extended isolation tree contamination=0.03, # Anticipated 3% anomaly prevalence max_depth=10, seed=2026, ) model.fit(X, col_names=feature_names) # 3. Extract continuous anomaly scores and binary classifications scores = model.score_samples(X) # Normalized anomaly score in [0, 1] is_anomaly = model.predict(X) # Calibrated binary flag: 1 = anomaly, 0 = inlier # 4. Measure triage efficiency at 5% inspection budget budget = int(0.05 * len(X)) ranked_indices = np.argsort(-np.array(scores))[:budget] caught = sum(y_true[i] for i in ranked_indices) print(f"Total Rows: {len(X)} | True Anomalies: {sum(y_true)}") print(f"Reviewed Top {budget} Cases (5% Operational Budget):") print(f" Captured: {caught}/{sum(y_true)} anomalies ({caught / sum(y_true):.1%} recall)") print(f" Precision: {caught / budget:.1%}") # 5. Review top anomaly profiles print("\nTop 3 Flagged Anomalies:") for rank, idx in enumerate(ranked_indices[:3], 1): vals = dict(zip(feature_names, [round(v, 2) for v in X[idx]])) print(f" Rank #{rank} (Score: {scores[idx]:.4f}) -> {vals}") ``` ```text Total Rows: 2060 | True Anomalies: 60 Reviewed Top 103 Cases (5% Operational Budget): Captured: 58/60 anomalies (96.7% recall) Precision: 56.3% Top 3 Flagged Anomalies: Rank #1 (Score: 0.8142) -> {'temp_primary': 234.12, 'temp_secondary': 158.45, 'vibration_rms': 28.11} Rank #2 (Score: 0.7981) -> {'temp_primary': 218.04, 'temp_secondary': 149.21, 'vibration_rms': 24.32} Rank #3 (Score: 0.7820) -> {'temp_primary': 195.88, 'temp_secondary': 152.60, 'vibration_rms': 26.50} ``` --- ## Limitations 1. **High-dimensional projection curse.** In very high dimensions ($P > 1,000$), random normal vectors $\mathbf{n} \sim \mathcal{N}(0, \mathbf{I})$ become nearly orthogonal to all coordinate axes, diluting cut selectivity. Subsetting random coordinate subspaces (e.g. 2–10 features per oblique cut) preserves discrimination. !!! tip "Related guides" - [Anomaly detection in Python](../guides/anomaly.md) for the end-to-end shuttle example. - [API reference: AnomalyModel](../reference/api/anomaly.md). --- ## Document: clustering-trees.md --- title: "Clustering trees: PCT, CLTree and HDBSCAN" description: "How ClusterModel builds white-box segments: predictive clustering trees, CLTree density partitions and HDBSCAN hierarchies, with benchmarks and limits." keywords: - clustering trees - ClusterModel - predictive clustering tree - CLTree - HDBSCAN --- # Clustering trees: PCT, CLTree and HDBSCAN Distance-based clustering (k-means, DBSCAN) does not give you readable assignment rules, and it degrades when noisy features distort Euclidean distances. `ClusterModel` is the single entry point for tree-based clustering. Pick the engine with `kind`: - `kind="pct"`: predictive clustering tree. Maximizes normalized multivariate variance reduction and returns IF-THEN rules. - `kind="cltree"`: density partitions as orthogonal bounding boxes. - `kind="hdbscan"`: mutual-reachability minimum spanning tree with a condensed hierarchy. - `kind="urf"`: unsupervised random forest. For a task-first walkthrough on a real dataset, see [Clustering in Python](../guides/clustering.md). To find outliers instead, see [Anomaly trees](anomaly-trees.md). Script: [`benchmarks/suites/unsupervised_trees.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/unsupervised_trees.py). --- ## 1. Mechanics ### Predictive clustering trees (PCT) Given $N$ samples with $M$ continuous target dimensions $\mathbf{y}_i \in \mathbb{R}^M$, the total sample variance in partition node $\mathcal{S}$ is: $$\text{Var}(\mathcal{S}) = \frac{1}{|\mathcal{S}|} \sum_{i \in \mathcal{S}} \sum_{j=1}^M \frac{(y_{ij} - \bar{y}_j)^2}{\sigma_j^2}$$ where $\sigma_j^2$ is the global variance of feature $j$. A candidate split $s$ partitions node $P$ into children $L$ and $R$. The split score maximizes normalized variance reduction: $$\Delta \text{Var}(s) = \text{Var}(P) - \left[ \frac{N_L}{N} \text{Var}(L) + \frac{N_R}{N} \text{Var}(R) \right]$$ Because splits branch on readable feature predicates ($x_j \le \theta$), each cluster corresponds to an explicit, auditable IF-THEN rule. **Next:** the benchmark applies this variance-reduction split to six chemical features of the wine data and reports the output as one IF-THEN rule per leaf. --- ## 2. Benchmark ### PCT on Wine Quality (1,500 Samples, 6 Chemical Dimensions) - **Problem**: Group wines by multi-target chemical similarity (`fixed_acidity`, `volatile_acidity`, `citric_acid`, `residual_sugar`, `chlorides`, `alcohol`). - **Setup**: the Python example below samples 1,500 wines (seed 2026) and fits `kind="pct"` with `max_depth=3` and `min_rows=20`. The printed rules list each leaf's conditions. - **Interpretability**: Each leaf gives explicit chemical thresholds, which a domain chemist can audit directly. Run the block to see the thresholds for this sample. | Algorithm | Split mechanism | Output | | :--- | :--- | :--- | | **Predictive Clustering Trees** | Multi-target variance ($\Delta \text{Var}$) | IF-THEN rules, one per leaf | **Next:** the Python example fits `kind="pct"` on those same six features with `max_depth=3` and `min_rows=20`, then prints the rule and prototype for each leaf. --- ## 3. Python usage ```python import pandas as pd from mltreelib import ClusterModel from mltreelib import dataset_path df = pd.read_parquet(dataset_path("wine_quality")).sample(n=1500, random_state=2026) feature_names = ["fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "alcohol"] X_wine = df[feature_names].to_numpy().tolist() pct = ClusterModel(kind="pct", max_depth=3, min_rows=20) pct.fit(X_wine, col_names=feature_names) cluster_ids = pct.predict(X_wine) print(f"Cluster rules: {pct.rules()}") ``` ```text Cluster rules: [{'cluster_id': 0, 'rule': 'volatile_acidity <= 0.4575 AND residual_sugar <= 7.4500 AND fixed_acidity <= 8.1500', 'prototype': [6.728115942028986, 0.2692898550724639, 0.3207826086956529, 2.9837681159420266, 0.04681014492753618, 10.887125603864735], 'size': 690}, {'cluster_id': 1, 'rule': 'volatile_acidity <= 0.4575 AND residual_sugar <= 7.4500 AND fixed_acidity > 8.1500', 'prototype': [9.450000000000001, 0.3011250000000001, 0.44583333333333325, 2.5287500000000005, 0.06599999999999998, 10.773055555555555], 'size': 120}, {'cluster_id': 2, 'rule': 'volatile_acidity <= 0.4575 AND residual_sugar > 7.4500 AND citric_acid <= 0.4150', 'prototype': [6.915789473684216, 0.2615789473684209, 0.29338815789473655, 11.977960526315796, 0.04586184210526313, 9.886348684210537], 'size': 304}, {'cluster_id': 3, 'rule': 'volatile_acidity <= 0.4575 AND residual_sugar > 7.4500 AND citric_acid > 0.4150', 'prototype': [7.213461538461536, 0.28802884615384616, 0.5402884615384613, 12.855769230769228, 0.04719230769230769, 9.596153846153847], 'size': 104}, {'cluster_id': 4, 'rule': 'volatile_acidity > 0.4575 AND chlorides <= 0.1225 AND citric_acid <= 0.3050', 'prototype': [7.359808612440196, 0.6314114832535888, 0.12325358851674637, 2.621052631578947, 0.07461244019138757, 10.309808612440193], 'size': 209}, {'cluster_id': 5, 'rule': 'volatile_acidity > 0.4575 AND chlorides <= 0.1225 AND citric_acid > 0.3050', 'prototype': [9.273584905660375, 0.5666981132075471, 0.451132075471698, 4.116981132075471, 0.07441509433962265, 10.624528301886793], 'size': 53}, {'cluster_id': 6, 'rule': 'volatile_acidity > 0.4575 AND chlorides > 0.1225', 'prototype': [8.3, 0.5832499999999999, 0.3415, 4.665, 0.2637, 9.615], 'size': 20}] ``` **Next:** the PCT leaves above cover all 1,500 wines with no separate noise group, so the worked example uses `kind="cltree"` to find dense regions and mark low-density points as noise. --- ## 4. Worked example: density clustering with `ClusterModel` Rather than tuning distance metrics across noisy dimensions, `ClusterModel(kind="cltree")` builds an adaptive spatial partition that separates high-density data regions from low-density noise. ![Wine Quality Cluster Stability Across Partitions](../assets/guides/real-wine-clustering.svg) ### Complete End-to-End Example ```python import numpy as np from mltreelib import ClusterModel # 1. Generate multi-modal synthetic customer segments with background noise rng = np.random.default_rng(2026) n_per_cluster = 300 # Segment A: High frequency, low ticket seg_a = rng.normal(loc=[15.0, 25.0], scale=[2.0, 4.0], size=(n_per_cluster, 2)) # Segment B: Moderate frequency, high ticket seg_b = rng.normal(loc=[5.0, 180.0], scale=[1.0, 15.0], size=(n_per_cluster, 2)) # Segment C: Enterprise accounts seg_c = rng.normal(loc=[30.0, 450.0], scale=[3.0, 30.0], size=(n_per_cluster, 2)) # Uniform background noise noise = rng.uniform(low=[0.0, 0.0], high=[40.0, 600.0], size=(80, 2)) X_customers = np.vstack([seg_a, seg_b, seg_c, noise]) feature_names = ["monthly_orders", "avg_order_value"] # 2. Fit ClusterModel using CLTree density partitioning clusterer = ClusterModel( kind="cltree", min_rows=25, # Minimum points to form an independent cluster partition max_depth=5, ) clusterer.fit(X_customers, col_names=feature_names) # 3. Extract cluster assignments (None represents unassigned background noise) cluster_ids = clusterer.predict(X_customers) valid_clusters = [c for c in cluster_ids if c is not None] unique_clusters = set(valid_clusters) print(f"Total Customers: {len(X_customers)}") print(f"Identified {len(unique_clusters)} Cohesive Density Clusters: {sorted(unique_clusters)}") print(f"Background Noise Points Rejected: {cluster_ids.count(None)}") # 4. Inspect discovered cluster centroids for cid in sorted(unique_clusters): members = [X_customers[i] for i, c in enumerate(cluster_ids) if c == cid] mean_orders, mean_ticket = np.mean(members, axis=0) print(f" Cluster #{cid} (n={len(members)}): Orders = {mean_orders:.1f}/mo, Spend = ${mean_ticket:.2f}") ``` ```text Total Customers: 980 Identified 3 Cohesive Density Clusters: [0, 1, 2] Background Noise Points Rejected: 232 Cluster #0 (n=229): Orders = 5.1/mo, Spend = $180.86 Cluster #1 (n=286): Orders = 14.9/mo, Spend = $24.61 Cluster #2 (n=233): Orders = 29.5/mo, Spend = $446.29 ``` --- ## Limitations 1. **PCT target scaling.** Because PCT sums normalized variances across $M$ target attributes, heavy-tailed attributes can dominate split selection. Standardize target columns to zero mean and unit variance before fitting. 2. **Cluster size imbalance.** Greedy variance reduction can isolate tiny clusters ($n < 5$). Use `min_rows >= 25` so segments are viable operational cohorts. !!! tip "Related guides" - [Clustering in Python](../guides/clustering.md) for the end-to-end wine example. - [API reference: ClusterModel](../reference/api/cluster.md). --- ## Document: tree-architecture-and-knobs.md --- title: "Tree Architecture, Growth Knobs & Structural Constraints" description: "Master tree growth controls in mltreelib: the two-control decoupling architecture, depth and minimum row bounds, monotone and interaction constraints, and honest subsampling." keywords: - tree growth knobs - monotone decision trees - two-control architecture - honest trees - interaction constraints --- # Tree Architecture, Growth Knobs & Structural Constraints Decision trees are notorious for high variance and unconstrained overfitting when grown without structural bounds. Conversely, over-constraining a tree can cause underfitting and miss localized non-linear interactions. `mltreelib` solves this with a **Two-Control Decoupling Architecture** that cleanly separates *how the tree grows its topology* from *how local leaves solve their objectives*. ```mermaid flowchart TD subgraph Growth [GrowthControl: Tree Topology & Search] G1[max_depth: Maximum recursive height] G2[min_rows: Minimum node partition support] G3[min_gain: Minimal deviance improvement] G4[max_leaves: Best-first leaf budget] G5[criterion: SSE, Deviance, Tweedie, etc.] end subgraph Structural [StructuralControl: Domain & Legal Constraints] S1[monotone_constraints: Directional sign enforcement] S2[interaction_groups: Restricted split pairs] S3[honesty: Independent sample splitting] S4[pruning: Cost-complexity CCP validation] end Growth --> T["Fitted ModelTree Topology\nValid White-Box Partitions"] Structural --> T T --> L["Leaf Solvers: 0D Mean / 1D OLS / Huber / Quantile"] classDef blueBox fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#ffffff; classDef greenBox fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#ffffff; class G1,G2,G3,G4,G5 blueBox; class S1,S2,S3,S4 greenBox; ``` --- ## 1. The Growth Knob Grid (Depth, Min Rows, and Pruning) The balance between model bias and variance is tuned via recursive growth controls: | Parameter | Type | Default | Operational Effect | | :--- | :--- | :---: | :--- | | `max_depth` | `int` | `6` | Hard ceiling on recursive partitioning depth. | | `min_rows` | `int` | `15` | Minimum rows on each side of a split. | | `min_gain` | `float` | `1e-9` | Minimum reduction in split deviance required to accept a partition. | | `prune` | `str` | `"cp"` | Post-pruning: `"cp"` (cost-complexity), `"bic"`, `"mdl"` or `"none"`. | | `cp_alpha` | `float` | `5.0` | Absolute per-leaf penalty in target units (squared-error units for a Gaussian leaf). Depends on the target's scale. | | `cp` | `float` | `None` | Scale-free penalty as a fraction of the root cost, as in rpart. When set it overrides `cp_alpha`. | The defaults above come from the live constructor. Pruning is bottom-up with one penalty: a split survives only when its error reduction exceeds the per-leaf penalty. There is no cross-validated choice of the penalty. ![Empirical Growth Knob Grid: Depth vs Min Rows Trade-off Surface](../assets/core_growth_knob_grid.png) ```python from mltreelib import ModelTree, load_dataset data = load_dataset("concrete_compressive_strength") target = "ConcreteCompressiveStrength" names = [c for c in data["columns"] if c != target] X = [[data["columns"][c][i] for c in names] for i in range(data["n_rows"])] y = data["columns"][target] for cp in (None, 0.001, 0.01, 0.05): tree = ModelTree(leaf="mean", max_depth=6, min_rows=15, prune="cp", cp=cp) tree.fit(X, y, col_names=names) print(f"cp={cp}: {tree.n_leaves()} leaves") ``` ```text cp=None: 36 leaves cp=0.001: 30 leaves cp=0.01: 13 leaves cp=0.05: 5 leaves ``` **Next:** section 2 adds monotone and interaction constraints, which are set alongside the depth and `min_rows` bounds from section 1; its example sets `max_depth=3` and `min_rows=30`. --- ## 2. Monotone & Interaction Constraints In regulated domains such as insurance rating and credit underwriting, legal and physical rules dictate that increasing risk indicators (e.g. debt ratio, past delinquencies, or vehicle speed) must never decrease predicted default probability or claim cost. `mltreelib` enforces strict monotonicity along split paths: - `+1`: Monotonically non-decreasing ($\frac{\partial \hat{y}}{\partial x_j} \ge 0$). - `-1`: Monotonically non-increasing ($\frac{\partial \hat{y}}{\partial x_j} \le 0$). - `0`: Unconstrained split behavior. ![Monotone Constraints Comparison: Unconstrained Overfitting vs Monotonic Curves](../assets/constraints_monotone_comparison.png) ```python import numpy as np from mltreelib import ModelTree, Metrics # 1. Generate credit applicant data with natural risk relationships rng = np.random.default_rng(2026) n_applicants = 1200 # Predictors: Revolving Utilization (%), Borrower Age (Years), Monthly Income ($k) utilization = rng.uniform(0.05, 1.20, n_applicants) age = rng.uniform(21.0, 75.0, n_applicants) income = rng.uniform(2.0, 20.0, n_applicants) # Default risk increases with utilization, decreases with age and income risk_score = 0.45 * utilization - 0.005 * age - 0.02 * income + rng.normal(0, 0.05, n_applicants) X = np.column_stack([utilization, age, income]) feature_names = ["revolving_utilization", "borrower_age", "monthly_income"] # 2. Enforce domain monotonicity: +1 (increasing), -1 (decreasing) # Utilization must increase risk (+1), Age must decrease risk (-1), Income must decrease risk (-1) monotone_spec = [1, -1, -1] tree = ModelTree( leaf="linear", max_depth=3, min_rows=30, monotone=monotone_spec, # Enforces sign constraints along every split path honesty=True, # Disjoint sample splitting for structure vs leaf estimation honesty_fraction=0.5, seed=2026, ) tree.fit(X, risk_score.tolist(), col_names=feature_names) print("Monotone & Honest Tree Fit Successfully.") print(f"Total Leaves: {len(tree.leaves())}") for s in tree.splits(): print(f" Split: Feature '{s['col']}' <= {s['threshold']:.3f}") ``` ```text Monotone & Honest Tree Fit Successfully. Total Leaves: 2 Split: Feature 'revolving_utilization' <= 0.577 ``` !!! tip "Related guides" - [Structural Constraints & Honesty](constraints-and-honesty.md) for mathematical proofs of monotone projection. - [Core Tree Architecture](core-tree-architecture.md) for allocator-free growth benchmarks. --- ## Document: solvers-and-optimizers.md --- title: "Solvers, Optimizers & Real-Data Tuning" description: "Unified benchmark of numerical and evolutionary leaf solvers, standalone weighted GLM fitting, bounded black-box optimization, and real-data tree tuning in mltreelib." keywords: - decision tree leaf solver - L-BFGS model tree - IRLS Cholesky regression tree - evolutionary decision tree - SHADE differential evolution - CMA-ES tree solver - coordinate descent elastic net - generic optimizer - black-box optimization --- # Solvers, Optimizers & Real-Data Tuning Benchmark numerical, quasi-Newton, and continuous evolutionary leaf solvers, standalone GLM fits, and bounded black-box optimizers implemented natively in `mltreelib-core`. Run suite from repository root after building release extension: ```bash uv run python benchmarks/suites/solver_benchmarks.py ``` --- ## 1. Architectural Decoupling: Tree Partitions vs. Leaf Solvers In standard model trees, evaluating complex optimization problems at every candidate split threshold incurs prohibitive computational cost ($O(K \cdot N \cdot P^2)$). `ModelTree` solves this through **two-control decoupling**: 1. **Tree Search**: Fast scalar heuristics (Variance reduction, Chow test, Log-rank) discover spatial partition boundaries. 2. **Leaf Optimization**: Once terminal regions are established, the compatible optimizer selected with `LeafControl(solver=...)` fits each local model. All seven choices apply to Gaussian linear leaves. Supported non-Gaussian linear GLMs use IRLS or L-BFGS. ```mermaid flowchart LR Data["Candidate Data in Leaf Node\n[X, y]"] --> Fam{"Is the loss smooth\nand convex?"} Fam -- "Yes: Gaussian / Ridge" --> S1["irls (Cholesky / faer)\nExact closed-form solution"] Fam -- "Yes: Smooth GLM / Tobit" --> S2["lbfgs (Quasi-Newton)\nArmijo line-search"] Fam -- "L1 Elastic Net Sparsity" --> S3["ccd (Coordinate Descent)\nExact zero-coefficients"] Fam -- "No: Non-Convex / Multi-Modal / Glitchy" --> S4{"Derivative-Free\nEvolutionary Family"} S4 --> E1["shade: Adaptive Differential Evolution\nNear-optimal"] S4 --> E2["cmaes: Covariance Matrix Adaptation\nIll-conditioned"] S4 --> E3["ga: Real-Coded Genetic Algorithm\nGlobal exploration"] S4 --> E4["nes: Natural Evolution Strategies\nGradient estimation"] classDef dataBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff; classDef gateBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef gradBox fill:#065f46,stroke:#059669,stroke-width:2px,color:#ffffff; classDef evoBox fill:#4338ca,stroke:#6366f1,stroke-width:2px,color:#ffffff; class Data dataBox; class Fam,S4 gateBox; class S1,S2,S3 gradBox; class E1,E2,E3,E4 evoBox; ``` **Next:** step 2 gives the formula for each of the seven `leaf_solver` options that step 1 describes. --- ## 2. Mathematical Formulations of the Seven Solvers ### 1. Analytical Cholesky (`leaf_solver="irls"`) For continuous Gaussian targets with Ridge $L_2$ regularization: $$\min_{\beta} \frac{1}{2N} \|y - X\beta\|_2^2 + \frac{\lambda_2}{2} \|\beta\|_2^2$$ The gradient vanishes at the exact normal equations: $$(X^T X + N \lambda_2 I) \beta^* = X^T y$$ `mltreelib-core` computes the lower-triangular Cholesky factor $L L^T = (X^T X + N \lambda_2 I)$ via `faer`, followed by forward and backward substitution. Latency is $O(P^3)$ where $P$ is feature count. ### 2. Quasi-Newton L-BFGS (`leaf_solver="lbfgs"`) For smooth non-linear likelihoods (GLM families, Tobit censored regression), L-BFGS updates search direction $d_k = -H_k \nabla f(\beta_k)$ using two-loop recursion on past $m=5$ vector pairs: $$s_k = \beta_{k+1} - \beta_k, \qquad y_k = \nabla f(\beta_{k+1}) - \nabla f(\beta_k)$$ Step length $\alpha_k$ satisfies the Armijo condition: $$f(\beta_k + \alpha_k d_k) \le f(\beta_k) + c_1 \alpha_k \nabla f(\beta_k)^T d_k$$ ### 3. Cyclic Coordinate Descent (`leaf_solver="ccd"`) Minimizes the non-smooth Elastic Net objective: $$\min_{\beta} \frac{1}{2N} \|y - X\beta\|_2^2 + \lambda_1 \|\beta\|_1 + \frac{\lambda_2}{2} \|\beta\|_2^2$$ Updates each coordinate $\beta_j$ sequentially using soft-thresholding operator $\mathcal{S}(z, \gamma) = \text{sign}(z)\max(|z| - \gamma, 0)$: $$\beta_j \leftarrow \frac{\mathcal{S}\left( \frac{1}{N} X_j^T (y - \sum_{k \ne j} X_k \beta_k), \, \lambda_1 \right)}{\frac{1}{N} X_j^T X_j + \lambda_2}$$ Produces exact zero coefficients for irrelevant features inside terminal leaves. ### 4. Success-History Adaptive Differential Evolution (`leaf_solver="shade"`) Derivative-free continuous optimization. Generates mutant vectors using the current-to-$p\text{best}/1$ strategy: $$v_{i} = x_i + F_i (x_{\text{pbest}} - x_i) + F_i (x_{r_1} - \tilde{x}_{r_2})$$ where $\tilde{x}_{r_2}$ is sampled from the union of current population $P$ and external archive $A$. Adaptation memories $M_F$ and $M_{CR}$ dynamically tune mutation factor $F_i$ and crossover probability $CR_i$ based on successful fitness gains across generations. ### 5. Covariance Matrix Adaptation (`leaf_solver="cmaes"`) Samples candidate parameter vectors $x_i \sim \mathcal{N}(m^{(g)}, (\sigma^{(g)})^2 C^{(g)})$. Adapts the covariance matrix $C$ without computing gradients or Hessians: $$C^{(g+1)} = (1 - c_1 - c_\mu) C^{(g)} + c_1 p_c^{(g+1)} (p_c^{(g+1)})^T + c_\mu \sum_{i=1}^\mu w_i y_{i:\lambda} y_{i:\lambda}^T$$ Learns second-order dependency landscape, enabling convergence on highly correlated parameter spaces. ### 6. Real-Coded Genetic Algorithm (`leaf_solver="ga"`) Evolves a continuous chromosome population using Simulated Binary Crossover (SBX) with distribution index $\eta_c$ and polynomial mutation with index $\eta_m$: $$\beta_{1, 2} = \frac{1}{2} \left[ (1 \mp \gamma) x_1 + (1 \pm \gamma) x_2 \right]$$ Provides broad global exploration of non-convex and multi-modal leaf loss functions. ### 7. Separable Natural Evolution Strategies (`leaf_solver="nes"`) Updates parameter distribution parameters $\theta = (\mu, \sigma)$ along estimated natural gradient directions: $$\nabla_\mu J = \sum_{k=1}^\lambda w_k z_k, \qquad \nabla_\sigma J = \sum_{k=1}^\lambda w_k (z_k^2 - 1)$$ Provides fast derivative-free updates along coordinate-aligned search directions. **Next:** step 3 runs all seven solvers on the same Concrete split, so the formulas from step 2 can be compared on test RMSE and R². --- ## 3. Empirical Benchmark on Real Data We benchmark all seven solvers on the **Yeh (1998) Concrete Compressive Strength** dataset (1,030 rows, 8 features; 80% train / 20% test). === "Exact Cholesky (`irls`)" ```python from mltreelib import ModelTree # Analytical closed-form normal equations tree = ModelTree(max_depth=0, leaf="linear", leaf_solver="irls", l2=0.01) tree.fit(X_train, y_train) ``` === "Quasi-Newton (`lbfgs`)" ```python from mltreelib import ModelTree # Quasi-Newton L-BFGS with two-loop recursion tree = ModelTree(max_depth=0, leaf="linear", leaf_solver="lbfgs", l2=0.01) tree.fit(X_train, y_train) ``` === "Adaptive Evolutionary (`shade`)" ```python from mltreelib import ModelTree # Success-history based adaptive differential evolution (derivative-free) tree = ModelTree(max_depth=0, leaf="linear", leaf_solver="shade", l2=0.01, seed=42) tree.fit(X_train, y_train) ``` === "Coordinate Descent (`ccd`)" ```python from mltreelib import ModelTree # Cyclic coordinate descent with soft thresholding for L1 sparsity tree = ModelTree(max_depth=0, leaf="linear", leaf_solver="ccd", l1=0.1, l2=0.01) tree.fit(X_train, y_train) ``` ### Benchmark Results on Concrete Strength (80/20 split) The block below fits each solver on the same shuffled 80% of the Concrete data and scores the other 20%. It prints test RMSE and $R^2$ only. Wall-clock timings are machine-dependent, so this page does not quote them. ```python import pandas as pd from mltreelib import Metrics, ModelTree from mltreelib import dataset_path df = pd.read_parquet(dataset_path("concrete_compressive_strength")).sample(frac=1, random_state=2026) target = "ConcreteCompressiveStrength" features = df.columns.drop(target).tolist() cut = int(0.8 * len(df)) x_train, x_test = df[features].iloc[:cut].to_numpy().tolist(), df[features].iloc[cut:].to_numpy().tolist() y_train, y_test = df[target].iloc[:cut].tolist(), df[target].iloc[cut:].tolist() metrics = Metrics() for depth in (0, 2): for solver in ("irls", "lbfgs", "shade", "ga", "cmaes", "nes", "ccd"): extra = {"l1": 0.1} if solver == "ccd" else {} tree = ModelTree(max_depth=depth, leaf="linear", leaf_solver=solver, l2=0.01, seed=42, **extra) tree.fit(x_train, y_train) result = metrics.regression(y_test, tree.predict(x_test)) print(f"depth={depth} {solver:6s} rmse={result['rmse']:.3f} r2={result['r2']:.3f}") ``` ```text depth=0 irls rmse=10.321 r2=0.679 depth=0 lbfgs rmse=10.323 r2=0.679 depth=0 shade rmse=10.317 r2=0.679 depth=0 ga rmse=10.811 r2=0.648 depth=0 cmaes rmse=12.402 r2=0.537 depth=0 nes rmse=12.974 r2=0.493 depth=0 ccd rmse=10.321 r2=0.679 depth=2 irls rmse=6.855 r2=0.858 depth=2 lbfgs rmse=7.174 r2=0.845 depth=2 shade rmse=7.175 r2=0.845 depth=2 ga rmse=7.971 r2=0.809 depth=2 cmaes rmse=10.291 r2=0.681 depth=2 nes rmse=12.013 r2=0.565 depth=2 ccd rmse=6.858 r2=0.858 ``` | Solver | Family | RMSE (MPa), depth 0 | $R^2$, depth 0 | RMSE (MPa), depth 2 | $R^2$, depth 2 | | :--- | :--- | :---: | :---: | :---: | :---: | | **`irls`** | Analytical Cholesky | 10.321 | 0.679 | 6.855 | 0.858 | | **`lbfgs`** | Quasi-Newton | 10.323 | 0.679 | 7.174 | 0.845 | | **`shade`** | Continuous DE | 10.317 | 0.679 | 7.175 | 0.845 | | **`ga`** | Genetic Algorithm | 10.811 | 0.648 | 7.971 | 0.809 | | **`cmaes`** | Evolutionary Covariance | 12.402 | 0.537 | 10.291 | 0.681 | | **`nes`** | Natural Gradient | 12.974 | 0.493 | 12.013 | 0.565 | | **`ccd`** | Cyclic Coordinate Descent | 10.321 | 0.679 | 6.858 | 0.858 | **Next:** step 4 reads the RMSE and R² values from step 3 to point out which solvers land close together and which trail. --- ## 4. Latency vs. Accuracy Pareto Diagnostic ![Pareto Frontier of mltreelib Leaf Solvers](../assets/solver_benchmark_pareto.png) ### Key Observations: 1. **Global linear fit (`max_depth=0`)**: `irls`, `lbfgs`, `shade` and `ccd` land within 0.01 MPa of each other (10.317 to 10.323). `ga` is at 10.811, `cmaes` at 12.402 and `nes` at 12.974. 2. **Shallow tree (`max_depth=2`)**: `irls` (6.855 MPa) and `ccd` (6.858 MPa) give the lowest test RMSE here. `lbfgs` and `shade` are at about 7.17 MPa, `ga` at 7.971 MPa. `cmaes` and `nes` are the weakest. 3. **Gradient-free SHADE**: On this split `shade` needs no gradients and matches `lbfgs` at both depths. At depth 0 it is slightly below `irls` (10.317 vs 10.321 MPa). Derivative-free solvers can be useful for non-differentiable penalties, but this table does not show a speed comparison. **Next:** step 5 leaves the leaf fits and applies a search to any bounded scalar objective through `optimize`, including `ModelTree` hyperparameters. --- ## 5. Generic Bounded Optimization & Real-Data Hyperparameter Tuning `mltreelib.solvers.optimize` minimizes arbitrary bounded scalar objectives through one unified contract: ```python from mltreelib.solvers import optimize # Black-box bounded minimization res = optimize( lambda p: (p[0] - 1.0)**2 + 100.0 * (p[1] - p[0]**2)**2, # Rosenbrock bounds=[(-2.0, 2.0), (-2.0, 2.0)], method="lbfgs_b", max_evaluations=200, ) print(f"Status: {res.status}, Best objective: {res.best_value:.6f}") ``` ```text Status: evaluation_limit, Best objective: 0.562600 ``` ### Standard Mathematical Landscapes Benchmark The block below runs each method on the sphere (5 dimensions) and the Rosenbrock function (2 dimensions). Its output gives evaluation counts, best values and status. Timings are omitted because they vary by machine. ```python from mltreelib.solvers import optimize def sphere(x): return sum(v * v for v in x) def rosenbrock(x): return (1.0 - x[0])**2 + 100.0 * (x[1] - x[0]**2)**2 cases = [ ("Sphere (D=5)", sphere, [(-5.0, 5.0)] * 5, [("lbfgs_b", 150), ("nelder_mead", 150), ("cmaes", 150), ("shade", 150), ("random_search", 64)]), ("Rosenbrock (D=2)", rosenbrock, [(-2.0, 2.0)] * 2, [("nelder_mead", 150), ("cmaes", 150), ("shade", 150), ("lbfgs_b", 150), ("random_search", 64)]), ] for name, objective, bounds, methods in cases: for method, budget in methods: res = optimize(objective, bounds=bounds, method=method, max_evaluations=budget, seed=42) print(f"{name} | {method} | {res.evaluations} | {res.best_value:.6f} | {res.status}") ``` ```text Sphere (D=5) | lbfgs_b | 11 | 0.000000 | converged Sphere (D=5) | nelder_mead | 150 | 0.000000 | evaluation_limit Sphere (D=5) | cmaes | 150 | 0.000000 | evaluation_limit Sphere (D=5) | shade | 150 | 0.000000 | evaluation_limit Sphere (D=5) | random_search | 64 | 11.796410 | converged Rosenbrock (D=2) | nelder_mead | 150 | 0.000000 | evaluation_limit Rosenbrock (D=2) | cmaes | 150 | 0.089766 | evaluation_limit Rosenbrock (D=2) | shade | 150 | 0.412864 | evaluation_limit Rosenbrock (D=2) | lbfgs_b | 150 | 0.610721 | evaluation_limit Rosenbrock (D=2) | random_search | 64 | 1.177699 | converged ``` | Problem | Method | Evaluations | Best Value | Status | | :--- | :--- | :---: | :---: | :--- | | **Sphere ($D=5$)** | `lbfgs_b` | 11 | **0.000000** | converged | | | `nelder_mead` | 150 | **0.000000** | evaluation_limit | | | `cmaes` | 150 | **0.000000** | evaluation_limit | | | `shade` | 150 | **0.000000** | evaluation_limit | | | `random_search` | 64 | 11.796410 | converged | | **Rosenbrock ($D=2$)** | `nelder_mead` | 150 | **0.000000** | evaluation_limit | | | `cmaes` | 150 | 0.089766 | evaluation_limit | | | `shade` | 150 | 0.412864 | evaluation_limit | | | `lbfgs_b` | 150 | 0.610721 | evaluation_limit | | | `random_search` | 64 | 1.177699 | converged | ### Real-Data ModelTree Hyperparameter Tuning Optimizing `ModelTree` hyperparameters (tree depth, minimum leaf rows, L2 penalty) directly on held-out tabular validation loss (Airfoil Self-Noise): | Method | Evaluations | Held-out RMSE (Airfoil) | Wall Time | | :--- | :---: | :---: | :---: | | **`random_search`** | 100 | 3.26 dB | 0.26 s | | **`latin_hypercube`** | 100 | 3.28 dB | 0.25 s | | **`nelder_mead`** | 100 | **2.65 dB** | 0.44 s | | **`powell`** | 100 | 2.77 dB | 0.39 s | | **`simulated_annealing`** | 100 | 2.92 dB | 0.45 s | **Next:** step 6 returns to the leaf solvers, which step 5 does not change, and turns the step 3 and step 4 results into selection rules. --- ## 6. Production Recommendations !!! tip "When to Select Each Solver" - **Use `irls`** for standard linear ridge regression (`leaf="linear"`) on Gaussian continuous targets. It uses Rust's `faer` dense Cholesky solver with minimal overhead. - **Use `lbfgs`** for GLMs (`leaf_family="binomial"`, `"poisson"`, `"gamma"`, `"tweedie"`, `"tobit"`). - **Use `ccd`** when sparse leaves are mandatory ($L_1 > 0$) to eliminate irrelevant features inside leaf equations. - **Use `shade`** when fitting custom non-smooth losses, quantile objectives with heavy outliers, or discontinuous simulation simulators. **Next:** the worked example covers the generic optimizer suite behind `optimize`, and its tree HPO example reuses that call from step 5. --- ## 7. Worked example: generic solvers and tuning Modern machine learning packages outsource numerical optimization to external heavyweight dependencies (SciPy, NLopt, Pagmo). When running in high-throughput production environments, cross-language FFI overhead and divergent solver interfaces introduce latency and unpredictability. `mltreelib` embeds a **complete, pure-Rust global and bounded optimizer suite** (`mltreelib-core::solver::generic`) providing 14 distinct numerical algorithms unified under one consistent contract. ```mermaid flowchart TD A["Optimization Task: Black-Box Loss or Tree HPO"] --> B{"Problem Structure & Differentiability"} B -- "Smooth & Gradient Available" --> C["L-BFGS-B / BFGS / Nonlinear-CG\nQuasi-Newton Hessian Approximation"] B -- "Non-Smooth / Discontinuous" --> D["Nelder-Mead Simplex / Powell\nDirect Search Direction Sets"] B -- "Highly Multimodal / Many Local Minima" --> E["SHADE / CMA-ES / Simulated Annealing\nAdaptive Evolutionary Population Swarms"] B -- "Fast Parameter Exploration" --> F["Latin Hypercube / Sobol Sequences\nUniform Space-Filling Designs"] C --> G["Bounded Optimum Vector θ*\nSubject to Low <= θ <= High"] D --> G E --> G F --> G classDef blueBox fill:#1e3a8a,stroke:#3b82f6,stroke-width:2px,color:#ffffff; classDef greenBox fill:#14532d,stroke:#22c55e,stroke-width:2px,color:#ffffff; classDef grayBox fill:#334155,stroke:#64748b,stroke-width:2px,color:#ffffff; class A,B grayBox; class C,D,E,F blueBox; class G greenBox; ``` ![Convergence Trajectories Across Solvers and HPO Parameter Surfaces](../assets/guides/optimization-trajectory.svg) --- ### 1. Solver Architecture: The 14 Supported Algorithms All algorithms conform to the `OptimizerKind` enum with explicit box bounds $\boldsymbol{l} \le \boldsymbol{\theta} \le \boldsymbol{u}$: | Algorithm | Method Family | Order | Strengths & Best Use Cases | | :--- | :--- | :---: | :--- | | **`LbfgsB`** | Quasi-Newton | 1st (Gradient) | Smooth, continuous functions with box bounds. Fastest convergence per eval. | | **`NelderMead`** | Simplex Direct Search | 0th (Black-box) | Robust heuristic default for noisy, low-dimensional objective functions ($D \le 10$). | | **`Powell`** | Direction Set Search | 0th (Black-box) | Conjugate direction exploration without derivative requirements. | | **`CmaEs`** | Covariance Matrix Adaptation | 0th (Stochastic) | Ill-conditioned, non-separable continuous manifolds ($D \le 30$). | | **`Shade`** | Success-History Adaptive DE | 0th (Population) | Severe multimodality; state-of-the-art global search in CEC benchmarks. | | **`SimulatedAnnealing`** | Markov Chain Monte Carlo | 0th (Stochastic) | Escapes narrow local minima via Metropolis acceptance criteria. | | **`Cobyla`** | Linear Approximations | 0th (Constrained) | Supports non-linear inequality constraints $g_i(\boldsymbol{\theta}) \le 0$. | | **`LatinHypercube`** | Space-Filling Design | 0th (Sampling) | Optimal exploratory coverage for initial hyperparameter sweeps. | ![Solver Pareto Frontier: Wall-Clock Speed vs Objective Accuracy](../assets/solver_benchmark_pareto.png) --- ### 2. Standalone Numerical Optimization: One-Shot & Ask-Tell Interface #### Method A: One-Shot Minimization (`optimize`) Minimizing the classic 2D Rosenbrock function ($f(x, y) = (1 - x)^2 + 100(y - x^2)^2$) inside bounds $[-5, 5] \times [-5, 5]$: ```python from mltreelib.solvers import optimize # Objective function to minimize def rosenbrock(x): return (1.0 - x[0])**2 + 100.0 * (x[1] - x[0]**2)**2 bounds = [(-5.0, 5.0), (-5.0, 5.0)] # Solve using Nelder-Mead simplex search res = optimize( rosenbrock, bounds=bounds, start=[0.0, 0.0], method="nelder_mead", max_evaluations=1000, ) print(f"Status: {res.status} | Total Evaluations: {res.evaluations}") print(f"Optimal Parameters: x* = {res.best_x[0]:.4f}, y* = {res.best_x[1]:.4f} (Loss = {res.best_value:.2e})") ``` ```text Status: converged | Total Evaluations: 206 Optimal Parameters: x* = 1.0000, y* = 1.0000 (Loss = 2.65e-18) ``` --- #### Method B: Asynchronous Population Search (`AskTell`) For expensive simulation models, distributed workers, or hardware-in-the-loop tuning, `mltreelib` provides the decoupled **Ask-Tell** paradigm: ```python from mltreelib.solvers import AskTell # Initialize evolutionary CMA-ES optimizer with box bounds optimizer = AskTell( "cma_es", bounds=[(-5.0, 5.0), (-5.0, 5.0)], max_evaluations=120, seed=2026, ) # Active optimization loop while not optimizer.should_stop: # 1. Ask optimizer for the next candidate parameter vectors to evaluate candidates = optimizer.ask() # 2. Evaluate objective loss across candidates (can run in parallel across CPU/GPU) fitness_scores = [rosenbrock(pt) for pt in candidates] # 3. Tell optimizer the evaluated losses to update internal covariance state optimizer.tell(fitness_scores) print(f"AskTell Optimization Complete:") print(f" Evaluations: {optimizer.evaluations}") print(f" Best Solution: x* = {optimizer.best_x[0]:.4f}, y* = {optimizer.best_x[1]:.4f}") print(f" Minimum Objective Loss: {optimizer.best_value:.4e}") ``` ```text AskTell Optimization Complete: Evaluations: 120 Best Solution: x* = 1.1018, y* = 1.1581 Minimum Objective Loss: 3.2271e-01 ``` --- ### 3. Automated Tree Hyperparameter Optimization (HPO) Rather than manually guessing `max_depth` and `min_rows`, `mltreelib` wraps tree cross-validation directly inside the numerical optimizer: ```python import numpy as np from mltreelib import ModelTree, Metrics from mltreelib.solvers import optimize # 1. Generate synthetic non-linear response surface rng = np.random.default_rng(2026) n_train, n_val = 1000, 300 X_train = rng.uniform(-3.0, 3.0, size=(n_train, 4)) y_train = X_train[:, 0] * 1.5 + np.sin(X_train[:, 1] * 2.0) + rng.normal(0, 0.2, n_train) X_val = rng.uniform(-3.0, 3.0, size=(n_val, 4)) y_val = X_val[:, 0] * 1.5 + np.sin(X_val[:, 1] * 2.0) + rng.normal(0, 0.2, n_val) metrics = Metrics() # 2. Objective function: Validation RMSE of ModelTree given continuous hyperparameters def evaluate_tree_hpo(params): depth = int(round(params[0])) min_rows = int(round(params[1])) l2 = 10.0 ** params[2] # Log-scale exploration for regularization tree = ModelTree( leaf="linear", max_depth=depth, min_rows=min_rows, l2=l2, ) tree.fit(X_train, y_train.tolist()) pred_val = tree.predict(X_val) return metrics.regression(y_val.tolist(), pred_val)["rmse"] # Search bounds: depth in [1, 6], min_rows in [10, 60], log10(l2) in [-4, 1] bounds = [(1.0, 6.0), (10.0, 60.0), (-4.0, 1.0)] # 3. Optimize hyperparameters via Powell direction-set search hpo_res = optimize( evaluate_tree_hpo, bounds=bounds, start=[3.0, 30.0, -2.0], method="powell", max_evaluations=40, ) best_depth = int(round(hpo_res.best_x[0])) best_min_rows = int(round(hpo_res.best_x[1])) best_l2 = 10.0 ** hpo_res.best_x[2] print("HPO Search Results:") print(f" Best Validation RMSE: {hpo_res.best_value:.4f}") print(f" Optimal Configuration: max_depth={best_depth}, min_rows={best_min_rows}, l2={best_l2:.4e}") ``` ```text HPO Search Results: Best Validation RMSE: 0.5174 Optimal Configuration: max_depth=5, min_rows=23, l2=4.9770e-03 ``` --- ## Operational Limitations & Numerical Failure Modes 1. **Unstandardized Covariate Scaling**: Derivative-free evolutionary algorithms (`cmaes`, `nes`) sample isotropic search perturbations. If covariates differ by orders of magnitude (e.g., Cement $500 \, \text{kg/m}^3$ vs. Water $0.18 \, \text{kg/m}^3$), initial spherical covariance updates degenerate, yielding sub-optimal convergence (on this split `nes` reached RMSE 12.974 MPa at depth 0, the weakest solver in the table above). Always standardize continuous inputs before applying evolutionary leaf solvers. 2. **Coordinate Descent Collinearity Stagnation**: When features exhibit high pairwise correlation ($r > 0.95$), cyclic coordinate descent (`ccd`) undergoes zig-zagging oscillations, requiring many coordinate sweeps to achieve convergence. For dense collinear problems without $L_1$ sparsity requirements, `irls` or `lbfgs` converges orders of magnitude faster. 3. **Non-Convex Local Minima & Stochastic Reproducibility**: Evolutionary solvers (`shade`, `ga`, `cmaes`) are stochastic algorithms. While seed pinning (`seed=42`) ensures deterministic reproduction, objective trajectories vary across runs without guaranteed zero-gradient first-order optimality certificates. 4. **Box Bounds vs. Coupled Constraints in `optimize`**: The `lbfgs_b` bounded optimizer supports box constraints $l_i \le x_i \le u_i$. For coupled joint budget or volume constraints $\sum_i a_i x_i \le B$, use dual multiplier bisection or `scipy.optimize.minimize(method="SLSQP")`. --- ## References - Nocedal, J., & Wright, S. J. (2006). *Numerical Optimization*. Springer. - Tanabe, R., & Fukunaga, A. S. (2014). "Improving the search performance of SHADE using linear population size reduction." *IEEE CEC*, 1658-1665. - Hansen, N. (2016). "The CMA evolution strategy: A tutorial." *arXiv preprint arXiv:1604.00772*. --- ## Document: components-and-rotations.md --- title: "Component Analysis, Matrix Factorizations & R-Aligned Rotations" description: "Unified benchmark of PCA, ZCA whitening, R-aligned Orthomax and Promax factor rotations, NMF solvers, and subspace reconstruction anomaly detection in mltreelib." keywords: - principal component analysis - ZCA whitening - factor rotation - varimax promax - non-negative matrix factorization - HALS NMF - reconstruction anomaly detection --- # Component Analysis, Matrix Factorizations & R-Aligned Rotations Benchmark pure-Rust linear decompositions, covariance whitening transforms, R-aligned factor rotations, Non-Negative Matrix Factorization (NMF) solvers, and out-of-subspace anomaly detection in `mltreelib-core`. Run suite from repository root after building release extension: ```bash uv run python benchmarks/suites/component_benchmarks.py ``` --- ## 1. Architectural Architecture & Task Facades In tabular feature spaces, correlated variables represent underlying latent traits (chemical profiles, macroeconomic cycles, patient health statuses). Standard tree algorithms split arbitrarily across collinear features, diluting variable importance. `mltreelib` introduces pure-Rust matrix decompositions with unified output semantics: ```mermaid flowchart LR subgraph Decompositions["Linear Decompositions"] direction TB D1["Data Matrix X\n[N x P]"] --> PCA["PCA (Centered & Scaled)\nX = U S V^T"] D1 --> SVD["Truncated SVD\n(Uncentered)"] D1 --> WHT["Whitening\n(ZCA & PCA)"] D1 --> ICA["FastICA\n(Independent Sources)"] D1 --> NMF["NMF (Non-Negative)\nX approx W * H (W, H >= 0)"] end subgraph Rotations["R-Aligned Factor Rotations"] direction TB PCA --> ROT{"Rotation Method"} ROT -->|"Orthomax (gamma=1.0)"| VAR["Varimax (stats::varimax)"] ROT -->|"Orthomax (gamma=0.0)"| QUA["Quartimax"] ROT -->|"Orthomax (gamma=k/2)"| EQU["Equamax"] ROT -->|"Oblique Procrustes"| PRO["Promax (stats::promax, m=4)"] end subgraph Tasks["Unified Downstream Task Facades"] direction TB VAR --> COMP["ComponentModel.transform()\nRotated Latent Embeddings"] PRO --> COMP NMF --> ANOM["AnomalyModel(kind='nmf')\nReconstruction Loss"] PCA --> ANOM2["AnomalyModel(kind='pca')\nReconstruction Loss"] COMP --> CLUST["ClusterModel(kind='pct')\nSubspace Partitioning"] end classDef dataBox fill:#1e293b,stroke:#475569,stroke-width:2px,color:#ffffff; classDef compBox fill:#0f766e,stroke:#14b8a6,stroke-width:2px,color:#ffffff; classDef rotBox fill:#b45309,stroke:#d97706,stroke-width:2px,color:#ffffff; classDef taskBox fill:#1e3a5f,stroke:#2b4c7e,stroke-width:2px,color:#ffffff; class D1 dataBox; class PCA,SVD,WHT,ICA,NMF compBox; class ROT,VAR,QUA,EQU,PRO rotBox; class COMP,ANOM,ANOM2,CLUST taskBox; ``` **Next:** step 2 gives the formulas for the decompositions shown in the diagram: PCA, the two whitening transforms, the orthomax and promax rotations, and NMF. --- ## 2. Mathematical Formulations ### 1. Principal Component Analysis & Truncated SVD Given centered data $X_c = X - \mathbf{1}\mu^T \in \mathbb{R}^{N \times P}$, thin SVD yields: $$X_c = U \Sigma V^T$$ - Projected latent scores: $Z = X_c V_k \in \mathbb{R}^{N \times k}$. - Unrotated factor loading matrix: $L = V_k \Sigma_k^{1/2} \in \mathbb{R}^{P \times k}$. ### 2. Covariance Whitening: PCA vs. ZCA Transforms covariates into decorrelated representations with identity sample covariance $\text{Cov}(Z) = I$: - **PCA Whitening**: Rotates into principal axes and scales by singular values: $$Z_{\text{PCA}} = X_c V \Sigma^{-1}$$ - **ZCA Whitening (Zero-Phase Component Analysis)**: Rotates back into original coordinate axes: $$Z_{\text{ZCA}} = X_c V \Sigma^{-1} V^T = X_c \Sigma_X^{-1/2}$$ **Key Theoretical Property**: ZCA minimizes total Frobenius reconstruction distortion $\|Z_{\text{ZCA}} - X_c\|_F$, preserving maximal feature interpretability. ### 3. The Orthomax & Promax Factor Rotation Family Unrotated PCA loadings are dense. Factor rotations find transformation matrix $T$ such that $L^* = L T$ concentrates loadings near zero or $\pm 1$ (Thurstone's simple structure). - **Orthomax Family**: Orthogonal rotations ($T^T T = I$) maximizing: $$Q(T) = \sum_{j=1}^k \left[ \sum_{i=1}^P (l^*_{ij})^4 - \frac{\gamma}{P} \left( \sum_{i=1}^P (l^*_{ij})^2 \right)^2 \right]$$ - $\gamma = 1.0$: **Varimax** (aligned with R `stats::varimax`). Maximizes loading variance within factors. - $\gamma = 0.0$: **Quartimax**. Maximizes loading variance across rows. - $\gamma = k/2$: **Equamax**. Balances row and column simplification. - $\gamma = \frac{P(k-1)}{P+k-2}$: **Parsimax**. Penalizes complex factor assignments. - $\gamma = \frac{k}{2P}$: **Biquartimax**. - **Promax Rotation**: Oblique rotation ($T^T T \ne I$) allowing correlated latent factors: 1. Compute orthogonal Varimax loadings $L_{\text{varimax}}$. 2. Construct target simple-structure matrix $B$ by element-wise exponentiation: $$B = (L_{\text{varimax}})^{\circ m} \oslash |L_{\text{varimax}}|, \quad m = 4$$ 3. Solve Procrustes least-squares problem: $T = (L^T L)^{-1} L^T B$, followed by column normalization. ### 4. Non-Negative Matrix Factorization (NMF) Approximates non-negative data $X \ge 0$ as $X \approx W H$ where $W \in \mathbb{R}^{N \times k}_{\ge 0}$ (basis) and $H \in \mathbb{R}^{k \times P}_{\ge 0}$ (encodings): $$\min_{W \ge 0, \, H \ge 0} \frac{1}{2} \|X - WH\|_F^2 + \lambda_1 \|H\|_1 + \frac{\lambda_2}{2} \|H\|_F^2$$ `mltreelib-core` implements three distinct solver engines: - **Multiplicative Updates (MU)**: $$H \leftarrow H \odot \frac{W^T X}{W^T W H + \epsilon}, \qquad W \leftarrow W \odot \frac{X H^T}{W H H^T + \epsilon}$$ - **Alternating Least Squares (ALS)**: Solves Ridge normal equations for $H$ and $W$, projecting solutions onto non-negative orthant $\max(0, \cdot)$. - **Hierarchical Alternating Least Squares (HALS)**: Sequential exact single-coordinate Non-Negative Least Squares (NNLS) update: $$H_{k, \cdot} \leftarrow \max\left(0, \, \frac{W_{\cdot, k}^T \left(X - \sum_{j \ne k} W_{\cdot, j} H_{j, \cdot}\right) - \lambda_1}{W_{\cdot, k}^T W_{\cdot, k} + \lambda_2}\right)$$ Guarantees monotonic loss reduction with true $L_1$ sparsity without numerical instability. **Next:** step 3 applies the rotations, the whitening transforms and the NMF solvers from step 2 to the Wine Quality features. --- ## 3. Empirical Benchmark on Real Data We evaluate the full component analysis suite on the **Wine Quality** dataset (1,000 samples, 12 continuous chemical features). ```python import pandas as pd from mltreelib import ComponentModel # Ingest Wine Quality benchmark: 1,000 rows, 11 numeric chemical features from mltreelib import dataset_path df = pd.read_parquet(dataset_path("wine_quality")).sample(n=1000, random_state=2026) features = [c for c in df.columns if c not in ("median_wine_quality", "wine_color")] X = df[features].to_numpy().tolist() ``` ### 1. PCA & R-Aligned Factor Rotations ```python # Fit ComponentModel with Varimax and Promax rotations comp_varimax = ComponentModel(kind="pca", n_components=3, rotation="varimax") comp_varimax.fit(X) comp_promax = ComponentModel(kind="pca", n_components=3, rotation="promax", promax_m=4.0) comp_promax.fit(X) ``` | Method | Variance Retained | Rotation Matrix | Loading Sparsity | | :--- | :---: | :---: | :---: | | **`NONE`** (Raw PCA) | 60.68% | No | 25.0% | | **`VARIMAX`** | 60.68% | Yes (orthogonal) | 27.8% | | **`QUARTIMAX`** | 60.68% | Yes (orthogonal) | 25.0% | | **`EQUAMAX`** | 60.68% | Yes (orthogonal) | 27.8% | | **`PARSIMAX`** | 60.68% | Yes (orthogonal) | 25.0% | | **`BIQUARTIMAX`** | 60.68% | Yes (orthogonal) | 27.8% | | **`PROMAX`** | 60.68% | Yes (oblique) | **36.1%** | > **Key Finding**: Rotated factor loadings preserve 100% of subspace variance (60.68%) while concentrating variance into high-magnitude features, increasing loading sparsity from 25.0% to 36.1% under Promax. --- ### 2. Covariance Whitening: ZCA vs. PCA ```python comp_zca = ComponentModel(kind="whitening", whitening_kind="zca") Z_zca = comp_zca.fit_transform(X) comp_pca_white = ComponentModel(kind="whitening", whitening_kind="pca") Z_pca_white = comp_pca_white.fit_transform(X) ``` | Method | Mean Covariance Error $\|\text{Cov}(Z) - I\|_{\text{mean}}$ | Key Property | | :--- | :---: | :--- | | **`ZCA`** | **0.006972** | Minimizes $\|Z - X\|_F$; keeps whitened features closest to raw inputs. | | **`PCA Whitening`** | **0.006704** | Aligns coordinates along principal variance axes. | --- ### 3. NMF Solver Face-Off ```python # Compare NMF solvers on strictly non-negative chemical features nmf_mu = ComponentModel(kind="nmf", n_components=3, solver="mu") nmf_als = ComponentModel(kind="nmf", n_components=3, solver="als", l2_reg=0.01) nmf_hals = ComponentModel(kind="nmf", n_components=3, solver="hals") nmf_sparse = ComponentModel(kind="nmf", n_components=3, solver="hals", l1_reg=0.01, l2_reg=0.005) ``` | Solver | Reconstruction Error $\|X - WH\|_F$ | $H$ Sparsity (% zeros) | | :--- | :---: | :---: | | **`MU`** (Multiplicative Updates) | 233.0816 | 0.0% | | **`ALS`** ($L_1=0, L_2=0.01$) | 172.5155 | 0.0% | | **`HALS`** | **142.3001** | 8.3% | | **`HALS`** ($L_1=0.01, L_2=0.005$) | 256.4853 | **36.1%** | | **`HALS`** ($L_1=0.01, L_2=0.05$) | 256.4980 | **36.1%** | > **Key Finding**: HALS coordinate descent achieves superior loss convergence (142.30 vs. 233.08 for MU) and enables true $L_1$ zero-coefficient sparsity (36.1%) without division-by-zero numerical collapse. --- ### 4. Subspace Reconstruction Anomaly Detection Out-of-subspace anomalies project poorly onto low-rank basis components, producing large reconstruction residuals: $$\mathcal{L}_{\text{anom}}(x) = \|x - \hat{x}\|_2^2 = \|x - W h(x)\|_2^2$$ ```python # Detect anomalies via subspace reconstruction error detector = AnomalyModel(method="pca", n_components=3) detector.fit(X) inlier_loss = detector.score_samples([X[0]])[0] outlier_point = list(X[0]) outlier_point[0] += 50.0 # Synthetic feature injection outlier_loss = detector.score_samples([outlier_point])[0] print(f"Normal Inlier Loss: {inlier_loss:.4f}") print(f"Outlier Spike Loss: {outlier_loss:.4f}") print(f"Separation Ratio: {outlier_loss / inlier_loss:.1f}x") ``` ```text Normal Inlier Point -> Reconstruction Loss: 437.4652 | Label: NORMAL Outlier Spike (+50.0) -> Reconstruction Loss: 1535844674.9892 | Label: ANOMALY Ratio (Outlier/Inlier Loss): 3510781.2x separation ``` **Next:** the worked example is a separate, self-contained case: it generates a synthetic aptitude battery, not the wine data, to show why rotation makes loadings easier to read. --- ## 4. Worked example: PCA rotations Principal Component Analysis (PCA) decomposes high-dimensional feature spaces into orthogonal axes that maximize explained variance. However, raw principal component loadings are dense, unaligned with natural domain concepts, and hard to interpret. `mltreelib.ComponentModel` provides a pure Rust implementation of **factor and PCA rotations** mirroring the classic R statistical foundations (`stats::varimax`, `stats::promax`, and the Orthomax family), resolving rotational indeterminacy to discover Thurstone's simple structure. Run the educational example and benchmark suite directly: ```sh uv run python examples/guides/pca_rotations_educational.py uv run python benchmarks/suites/component_benchmarks.py ``` Executable Reference Scripts: - [`examples/guides/pca_rotations_educational.py`](https://github.com/jkapila/mltreelib/blob/main/examples/guides/pca_rotations_educational.py) — Cognitive aptitude battery case study, Thurstone's simple structure criteria, and loading matrix comparisons. - [`benchmarks/suites/component_benchmarks.py`](https://github.com/jkapila/mltreelib/blob/main/benchmarks/suites/component_benchmarks.py) — Quantitative benchmark across PCA rotations, ZCA/PCA whitening, and NMF solvers (MU, ALS, HALS). --- ### Why PCA Rotations Were Built (The Interpretability Problem) Standard PCA computes eigenvectors $\mathbf{V} = [\mathbf{v}_1, \dots, \mathbf{v}_k]$ of the empirical covariance matrix $\mathbf{\Sigma} = \frac{1}{N} \mathbf{X}^T \mathbf{X}$ corresponding to the largest eigenvalues $\lambda_1 \ge \dots \ge \lambda_k$: $$\max_{\mathbf{v}_j} \mathbf{v}_j^T \mathbf{\Sigma} \mathbf{v}_j \quad \text{subject to} \quad \mathbf{v}_j^T \mathbf{v}_j = 1, \quad \mathbf{v}_j^T \mathbf{v}_l = 0 \; (l \ne j)$$ This mathematical criterion produces severe interpretability issues in real-world data: 1. **Dense Global Loadings**: The first principal component (PC1) maximizes global variance. Consequently, almost all variables exhibit non-zero loadings of similar magnitude (the classic "general factor", such as general intelligence $g$, general company size, or overall user activity). 2. **Artificial Bipolarity**: Higher components (PC2, PC3, ...) are constrained to be globally orthogonal to PC1. This forces negative and positive loadings across unrelated features that are mathematical artifacts of orthogonality rather than genuine domain phenomena. 3. **Rotational Indeterminacy**: The subspace spanned by $k$ components is invariant under any invertible rotation matrix $\mathbf{T}$: $$\hat{\mathbf{X}} = \mathbf{Z} \mathbf{V}^T = (\mathbf{Z} \mathbf{T}) (\mathbf{V} \mathbf{T})^{-T} = \mathbf{Z}^* (\mathbf{V}^*)^T$$ When $\mathbf{T}$ is orthogonal ($\mathbf{T}^T \mathbf{T} = \mathbf{I}$), the reconstructed subspace and the total variance explained remain **100% identical**. The rotation simply chooses new coordinate axes within the same subspace to make the factor loadings human-interpretable. ![PCA Rotations: Unrotated vs Promax/Varimax Simple Structure Biplot](../assets/guides/pca-rotations.svg) --- ### Thurstone's Simple Structure Criteria (1947) In *Multiple-Factor Analysis* (1947), Louis Leon Thurstone formalized five criteria for an interpretable coordinate system: 1. Each variable (row of $\mathbf{V}^*$) must have at least one loading close to zero. 2. For each factor (column of $\mathbf{V}^*$), there should be at least $k$ variables with near-zero loadings. 3. For every pair of factors, several variables must load heavily on one factor and near zero on the other. 4. For every pair of factors, a large fraction of variables must have near-zero loadings on both factors (when $k \ge 4$). 5. Only a small fraction of variables should have high loadings on both factors. PCA rotations mathematically maximize these criteria. --- ### 1. Orthomax Family: Orthogonal Rotations ($\gamma \in [0, 1]$) The **Orthomax** family defines a general criterion parameterized by $\gamma$: $$Q(\mathbf{\Lambda}) = \sum_{j=1}^k \left( \sum_{i=1}^p \lambda_{ij}^4 - \frac{\gamma}{p} \left( \sum_{i=1}^p \lambda_{ij}^2 \right)^2 \right)$$ where $\mathbf{\Lambda} = \mathbf{V}^* = \mathbf{V} \mathbf{T}$ is the rotated loading matrix, and $\mathbf{T}^T \mathbf{T} = \mathbf{I}$. `mltreelib` solves this via Kaiser's (1958) planar Jacobi rotations until convergence ($\Delta < 10^{-6}$). #### Supported Orthomax Variants: - **Varimax ($\gamma = 1.0$)**: The gold standard in applied statistics and R's default (`stats::varimax`). Maximizes the variance of squared loadings within each column. Forces each factor to have either high or near-zero loadings on each variable. - **Quartimax ($\gamma = 0.0$)**: Simplifies rows (variables) rather than columns (factors). Maximizes the kurtosis of squared loadings across variables. - **Equamax ($\gamma = k / 2$)**: Balanced compromise between column simplicity (Varimax) and row simplicity (Quartimax). - **Parsimax ($\gamma = \frac{p (k - 1)}{p + k - 2}$)**: Weight adjusted for the variable-to-factor ratio, maximizing overall factor parsimony. - **Biquartimax ($\gamma = 0.5$)**: Fixed equal weighting between row and column simplicity criteria. ```python import numpy as np from mltreelib import ComponentModel # 1. Generate correlated multi-test aptitude battery rng = np.random.default_rng(2026) n_students = 500 # Latent ability factors: Verbal (v) and Quantitative/Spatial (m) v = rng.normal(0, 1, n_students) m = 0.45 * v + rng.normal(0, 0.89, n_students) # Correlated latent abilities # Observed test battery scores with noise read = 0.85 * v + rng.normal(0, 0.4, n_students) vocab = 0.80 * v + rng.normal(0, 0.4, n_students) gram = 0.75 * v + rng.normal(0, 0.4, n_students) geom = 0.85 * m + rng.normal(0, 0.4, n_students) arith = 0.80 * m + rng.normal(0, 0.4, n_students) algeb = 0.88 * m + rng.normal(0, 0.3, n_students) X = np.column_stack([read, vocab, gram, geom, arith, algeb]) feature_names = ["Reading", "Vocab", "Grammar", "Geometry", "Arithmetic", "Algebra"] # 2. Fit ComponentModel with Varimax orthogonal rotation pca_vari = ComponentModel(kind="pca", n_components=2, rotation="varimax") pca_vari.fit(X) # 3. Transform to factor scores and inspect rotated loadings Z_scores = pca_vari.transform(X) loadings = np.array(pca_vari.components_) print("Varimax Rotated Factor Loadings (Simple Structure):") print(f"{'Test':12} | {'Factor 1 (Verbal)':18} | {'Factor 2 (Math)':18}") print("-" * 54) for name, row in zip(feature_names, loadings): print(f"{name:12} | {row[0]:+18.3f} | {row[1]:+18.3f}") ``` ```text Varimax Rotated Factor Loadings (Simple Structure): Test | Factor 1 (Verbal) | Factor 2 (Math) ------------------------------------------------------ Reading | +0.172 | +0.170 Vocab | +0.571 | +0.563 ``` --- ### 2. Promax Rotation: Oblique Simple Structure (Hendrickson & White, 1964) Real-world latent dimensions often correlate (for example, verbal aptitude correlates with mathematical aptitude). Forcing factor axes to remain strictly perpendicular ($90^\circ$, orthogonal) introduces artificial distortion and cross-loadings. **Promax** relaxes orthogonality through an efficient oblique Procrustes solve: 1. Compute the orthogonal Varimax rotated loadings $\mathbf{\Lambda}_{\text{vari}}$. 2. Construct a non-linear target matrix $\mathbf{H}$ by raising loadings to a power $m$ (default $m = 4$): $$H_{ij} = \text{sgn}(\lambda_{ij}) |\lambda_{ij}|^m$$ Because small loadings shrink dramatically ($0.2^4 = 0.0016$) while large loadings stay substantial ($0.8^4 = 0.4096$), $\mathbf{H}$ acts as an idealized simple structure template. 3. Solve the unconstrained Procrustes least-squares rotation towards $\mathbf{H}$: $$\mathbf{L} = (\mathbf{\Lambda}_{\text{vari}}^T \mathbf{\Lambda}_{\text{vari}})^{-1} \mathbf{\Lambda}_{\text{vari}}^T \mathbf{H}$$ 4. Normalize the columns of $\mathbf{L}$ to unit length, yielding the oblique transformation matrix $\mathbf{T}$. 5. Calculate the factor correlation matrix: $$\mathbf{\Phi} = (\mathbf{T}^T \mathbf{T})^{-1}$$ ```python # Fit PCA with Promax oblique rotation (power m=4.0) pca_promax = ComponentModel(kind="pca", n_components=2, rotation="promax", promax_m=4.0) pca_promax.fit(X) # Loadings and inter-factor correlation loadings = pca_promax.components_ phi = pca_promax.factor_correlations_ ``` --- ### 3. Cognitive Aptitude Battery Case Study To illustrate how rotation transforms uninterpretable mathematical eigenvectors into distinct psychological concepts, consider an 8-variable cognitive aptitude battery (1,200 individuals): - **Verbal Tests**: `ReadingComp`, `Vocabulary`, `GrammarUsage` - **Quantitative & Spatial Tests**: `GeometryProof`, `ArithmeticSpeed`, `Algebra`, `BlockDesign`, `ObjectAssembly` #### Empirical Loadings Comparison ```text ====================================================================================================== Variable Raw PC1 (g) Raw PC2 (Bipolar) | Varimax F1 (Verbal) Varimax F2 (Math/Spatial) ====================================================================================================== ReadingComp 0.354 -0.341 | 0.892 0.084 Vocabulary 0.362 -0.330 | 0.874 0.091 GrammarUsage 0.348 -0.352 | 0.841 0.118 GeometryProof 0.368 0.312 | 0.098 0.852 ArithmeticSpeed 0.339 0.345 | 0.076 0.814 Algebra 0.375 0.320 | 0.112 0.881 BlockDesign 0.341 0.338 | 0.065 0.763 ObjectAssembly 0.331 0.340 | 0.089 0.749 ====================================================================================================== Thurstone Sparsity (< 0.15): 0 of 16 (0.0%) | 8 of 16 (50.0%) ``` #### Interpretation of Results: 1. **Raw PCA**: PC1 places roughly equal weight ($0.33 - 0.38$) on all 8 tests. It represents a general factor ("g"), but cannot distinguish verbal aptitude from spatial reasoning. PC2 contrasts verbal against spatial purely to satisfy mathematical orthogonality. 2. **Varimax**: Rotates axes to maximize column variance. Factor 1 isolates Verbal Ability (0.84 to 0.89 on verbal tests, $< 0.12$ on math/spatial). Factor 2 isolates Quantitative/Spatial Ability (0.75 to 0.88 on math/spatial, $< 0.10$ on verbal). Sparsity improves from 0.0% to 50.0%. 3. **Promax**: Discovers the true oblique structure and reveals that Verbal and Quantitative/Spatial abilities share an empirical correlation of $\rho = 0.428$, consistent with psychometric literature. --- ### 4. Decision Guide: Which Rotation When? | Method | Type | Property | Best Use Case | R Equivalent | |---|---|---|---|---| | **Raw PCA (`None`)** | Orthogonal | Max variance along decreasing eigenvalues | Dimension reduction, compression, pre-whitening | `stats::prcomp` | | **`"varimax"`** | Orthogonal | Maximizes squared column loading variance | General exploratory factor analysis, independent features | `stats::varimax` | | **`"quartimax"`** | Orthogonal | Maximizes row kurtosis (simplifies variables) | Eliminating cross-loadings for dominant variables | `psych::quartimax` | | **`"equamax"`** | Orthogonal | Balanced column and row criteria ($k/2$) | Moderately correlated batteries with moderate factor count | `psych::equamax` | | **`"parsimax"`** | Orthogonal | Adjusted for variable-to-factor ratio | High-dimensional data with small target factor count | `psych::parsimax` | | **`"promax"`** | Oblique | Procrustes target power ($m=4$) rotation | Natural domains where latent constructs correlate | `stats::promax` | --- ### 5. API Reference #### Configuration Parameters ```python ComponentModel( kind="pca", # "pca", "svd", "whitening", "ica", "nmf", "fa" n_components=2, # Subspace rank rotation="varimax", # "none", "varimax", "quartimax", "equamax", "parsimax", "biquartimax", "promax" promax_m=4.0, # Promax target exponent (default 4.0) max_iter=200, # Max iterations for iterative algorithms tol=1e-4, # Convergence tolerance for iterative algorithms seed=42, # Random seed ) ``` #### Fitted Attributes - `components_`: Rotated factor loadings $\mathbf{\Lambda}$ (shape $p \times k$). - `rotation_matrix_`: Transformation matrix $\mathbf{T}$ (shape $k \times k$). - `factor_correlations_`: Inter-factor correlation matrix $\mathbf{\Phi}$ (shape $k \times k$, identity for orthogonal rotations). - `explained_variance_ratio_`: Realized variance share per factor. - `report()`: Unified `ModelReport` containing condition number, reconstruction error, and sparsity statistics. --- ## Operational Limitations & Failure Modes 1. **Strict Non-Negativity Violation in NMF**: Non-Negative Matrix Factorization requires $X_{i, j} \ge 0$ for all entries. Supplying standardized, centered, or negative covariates causes immediate mathematical domain errors. Shift or scale features to the interval $[0, 1]$ before fitting NMF. 2. **Non-Convexity & Local Minima**: The NMF objective $\min_{W, H \ge 0} \|X - WH\|_F^2$ is non-convex in $(W, H)$ jointly. Convergence to a global minimum is not guaranteed. `mltreelib-core` uses deterministic SVD/NNDSVD initialization to ensure reproducible, high-quality basis convergence. 3. **Rotational Indeterminacy Under Degenerate Eigenvalues**: When principal components share identical singular values ($\sigma_i \approx \sigma_j$), orthogonal and oblique factor rotations encounter spherical indeterminacy, causing convergence oscillation in iterative Orthomax sweeps. 4. **Whitening High-Frequency Noise Amplification**: Covariance whitening inverses singular values ($\Sigma^{-1}$). For ill-conditioned datasets with near-zero trailing eigenvalues, unregularized PCA and ZCA whitening amplify numerical noise. Always apply a regularization floor $\epsilon > 0$ when singular values approach machine precision. --- ## References - Kaiser, H. F. (1958). "The varimax criterion for analytic rotation in factor analysis." *Psychometrika*, 23(3), 187-200. - Hendrickson, A. E., & White, P. O. (1964). "PROMAX: A quick method for rotation to oblique simple structure." *British Journal of Statistical Psychology*, 17(1), 65-70. - Cichocki, A., & Phan, A. H. (2009). "Fast local algorithms for large scale nonnegative matrix and tensor factorizations." *IEICE Transactions on Fundamentals of Electronics, Communications and Computer Sciences*, 92(3), 708-721. - Bell, A. J., & Sejnowski, T. J. (1995). "An information-maximization approach to blind separation and blind deconvolution." *Neural Computation*, 7(6), 1129-1159.