Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Elastic Net Selection
Horizon provides Rust-native regularized regression (elastic net, lasso, ridge) for feature selection and signal construction. These methods identify which market signals carry predictive power and automatically shrink or eliminate noisy features, producing sparse, interpretable models suitable for real-time trading.Elastic Net
Combined L1 + L2 regularization. Balances feature selection (lasso) with coefficient stability (ridge).
Lasso (L1)
Pure L1 regularization. Drives uninformative coefficients to exactly zero for automatic feature selection.
Ridge (L2)
Pure L2 regularization. Shrinks all coefficients toward zero without eliminating any. Handles multicollinearity.
Cross-Validation
Automated alpha/lambda selection via k-fold CV. Finds the regularization strength that minimizes out-of-sample error.
hz.elastic_net_fit
Fit an elastic net regression model with combined L1 and L2 penalties. The objective minimizes:(1/2n) * ||y - X*beta||^2 + alpha * (l1_ratio * ||beta||_1 + 0.5 * (1-l1_ratio) * ||beta||_2^2)
ElasticNetResult Type
hz.elastic_net_predict
Generate predictions from a fitted elastic net model.
Returns
list[float]: predicted values.
hz.lasso_fit
Convenience function for pure L1 regularization (elastic net with l1_ratio=1.0). Drives uninformative coefficients to exactly zero.
Returns an
ElasticNetResult (same type as elastic_net_fit).
hz.ridge_fit
Convenience function for pure L2 regularization (elastic net with l1_ratio=0.0). Shrinks all coefficients toward zero without eliminating any. Preferred when all features are potentially relevant and multicollinearity is present.
Returns an
ElasticNetResult.
Ridge regression always keeps all features (n_nonzero equals the total number of features). Use lasso or elastic net when you need automatic feature selection.
hz.elastic_net_cv
Automated regularization parameter selection using k-fold cross-validation. Tests a grid of alpha values and returns the model with the lowest out-of-sample MSE.CV Result Type
Pipeline Integration
hz.signal_selector
Creates a pipeline function that uses elastic net to select and weight signals from multiple feeds, injecting a composite signal intoctx.params.
Injected Parameters
Example: Feature Selection Workflow
Mathematical Background
Elastic Net Objective
Elastic Net Objective
The elastic net minimizes:L(beta) = (1/2n) * ||y - X*beta||^2 + alpha * [l1_ratio * ||beta||_1 + 0.5 * (1 - l1_ratio) * ||beta||_2^2]When l1_ratio=1, this is the lasso (L1 only). When l1_ratio=0, this is ridge regression (L2 only). The L1 term produces sparsity (feature selection); the L2 term handles correlated features and improves numerical stability.
Coordinate Descent
Coordinate Descent
Horizon solves the elastic net using cyclic coordinate descent. For each feature j, the update is:beta_j = soft_threshold(partial_residual_j, alpha * l1_ratio) / (1 + alpha * (1 - l1_ratio))where soft_threshold(z, gamma) = sign(z) * max(|z| - gamma, 0). The algorithm cycles through all features until convergence.
Cross-Validation for Alpha Selection
Cross-Validation for Alpha Selection
The regularization strength alpha controls the bias-variance tradeoff. K-fold CV splits the data into K folds, trains on K-1, and evaluates on the held-out fold. The alpha with the lowest average MSE across folds is selected. The alpha grid is log-spaced from alpha_max (where all coefficients are zero) down to alpha_max / 1000.