Skip to main content
Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
What is this? When you have many potential trading signals, elastic net regression automatically selects the ones that actually predict returns and discards the rest. It’s a regularized regression that balances between LASSO (which selects few signals) and ridge (which shrinks all signals). Use it to build parsimonious alpha models that don’t overfit.

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 into ctx.params.

Injected Parameters


Example: Feature Selection Workflow


Mathematical Background

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.
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.
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.
Features should be standardized (zero mean, unit variance) before fitting elastic net. Unstandardized features with different scales will cause the regularization to penalize large-scale features disproportionately.