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)
| Parameter | Type | Default | Description |
|---|---|---|---|
features | list[list[float]] | required | Feature matrix (n_samples x n_features) |
targets | list[float] | required | Target values (n_samples) |
alpha | float | 0.1 | Overall regularization strength. Higher = more regularization. |
l1_ratio | float | 0.5 | Mix of L1 vs L2 penalty. 1.0 = pure lasso, 0.0 = pure ridge. |
max_iters | int | 1000 | Maximum coordinate descent iterations |
tol | float | 1e-6 | Convergence tolerance |
ElasticNetResult Type
| Field | Type | Description |
|---|---|---|
coefficients | list[float] | Fitted coefficients (one per feature) |
intercept | float | Fitted intercept term |
n_nonzero | int | Number of non-zero coefficients (selected features) |
r_squared | float | In-sample R-squared (coefficient of determination) |
mse | float | Mean squared error on training data |
selected_features | list[int] | Indices of features with non-zero coefficients |
hz.elastic_net_predict
Generate predictions from a fitted elastic net model.| Parameter | Type | Description |
|---|---|---|
features | list[list[float]] | Feature matrix for prediction (n_samples x n_features) |
coefficients | list[float] | Fitted coefficients from elastic_net_fit |
intercept | float | Fitted intercept from elastic_net_fit |
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.| Parameter | Type | Default | Description |
|---|---|---|---|
features | list[list[float]] | required | Feature matrix (n_samples x n_features) |
targets | list[float] | required | Target values |
alpha | float | 0.1 | Regularization strength |
max_iters | int | 1000 | Maximum iterations |
tol | float | 1e-6 | Convergence tolerance |
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.| Parameter | Type | Default | Description |
|---|---|---|---|
features | list[list[float]] | required | Feature matrix |
targets | list[float] | required | Target values |
alpha | float | 1.0 | Regularization strength |
max_iters | int | 1000 | Maximum iterations |
tol | float | 1e-6 | Convergence tolerance |
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.| Parameter | Type | Default | Description |
|---|---|---|---|
features | list[list[float]] | required | Feature matrix |
targets | list[float] | required | Target values |
l1_ratio | float | 0.5 | L1/L2 mix ratio |
n_alphas | int | 50 | Number of alpha values to test (log-spaced) |
n_folds | int | 5 | Number of cross-validation folds |
max_iters | int | 1000 | Maximum iterations per fit |
CV Result Type
| Field | Type | Description |
|---|---|---|
best_alpha | float | Alpha with lowest cross-validation MSE |
best_mse | float | Cross-validation MSE at the best alpha |
best_model | ElasticNetResult | Full model fitted at the best alpha |
alphas | list[float] | All alpha values tested |
cv_mses | list[float] | Mean CV MSE at each alpha |
cv_stds | list[float] | Standard deviation of CV MSE at each alpha |
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.
| Parameter | Type | Default | Description |
|---|---|---|---|
target_feed | str | required | Feed representing the target variable |
signal_feeds | list[str] | required | Feeds to use as features |
lookback | int | 200 | Observations to retain for training |
l1_ratio | float | 0.7 | L1/L2 mix ratio (higher = more feature selection) |
retrain_interval | int | 100 | Retrain the model every N observations |
n_folds | int | 5 | Cross-validation folds for alpha selection |
param_name | str | "signal" | Key in ctx.params |
Injected Parameters
| Key | Type | Description |
|---|---|---|
ctx.params["signal"]["fair_value"] | float | Model-predicted fair value |
ctx.params["signal"]["r_squared"] | float | In-sample R-squared of the current model |
ctx.params["signal"]["n_signals"] | int | Number of non-zero (selected) signal feeds |
ctx.params["signal"]["weights"] | dict[str, float] | Feed name to coefficient mapping |
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.