crabbymetrics
  • Home
  • API
    • API Overview
    • Regression And GLMs
    • Survival / Event-Time
    • Causal Inference And Panels
    • MPE_CBPS
    • Hypothesis Testing And Utilities
    • Transforms
    • Estimation Interfaces
  • Internals
  • Regression
    • OLS
    • ABC OLS
    • Anytime-Valid Confidence Sequences
    • Ridge
    • Bagged Polynomial Regression
    • Fixed Effects OLS
    • ElasticNet
    • Logit
    • Multinomial Logit
    • Poisson
    • MLE Prediction Interface
    • Survival / Recurrent Events
    • GMM
    • MEstimator Poisson
  • Causal / Panels
    • Balancing Weights
    • Chronos LTV Balancing
    • Cressie-Read And Rényi Balancing
    • EPLM
    • Average Derivative
    • Double ML And AIPW
    • Richer Regression
    • TwoSLS
    • Synthetic Control
    • Synthetic DID
    • Augmented Balancing For Panel Data
    • Horizontal Panel Ridge
    • Matrix Completion
    • Interactive Fixed Effects
    • Staggered Panel Event Study
    • Joint Hypothesis Tests
    • Dynamic Treatment Effects
  • Transforms
    • PCA And Kernel Basis
    • Sparse Factor Rotations
  • Ablations
    • Variance Estimators
    • Semiparametric Estimator Comparisons
    • Two-Period Semiparametric DID
    • Bridging Finite And Superpopulation
    • Panel Estimator DGP Comparisons
    • Same Root Panel Case Studies
    • Randomized Sketching And Least Squares
    • Estimator Scaling And References
  • Optimization
    • Optimizers
    • GMM With Optimizers
  • Ding
    • Chapter Index
    • Foundations (1-4)
    • Design And Adjustment (5-8)
    • Finite And Superpopulation (9)
    • Observational Studies (11-13, 27)
    • Instrumental Variables (21, 23)

ElasticNet Example

Fits center both predictors and the response for an unpenalized intercept, then return the intercept in the original predictor coordinates. Predictions are therefore invariant to adding constants to predictor columns. This corrects the older outcome-only centering behavior. Pure L2 (l1_ratio=0) and zero-penalty fits use a closed-form QR solution; other fits retain duality-gap checks.

1 Fit A Regularized Linear Model

import numpy as np
from pprint import pprint

from crabbymetrics import ElasticNet

np.set_printoptions(precision=4, suppress=True)
rng = np.random.default_rng(1)
n = 600
k = 6
beta = np.array([2.0, -1.5, 0.0, 0.0, 0.8, -0.3])
intercept = -0.4

x = rng.normal(size=(n, k))
y = intercept + x @ beta + rng.normal(scale=0.7, size=n)

model = ElasticNet(penalty=0.1, l1_ratio=0.5)
model.fit(x, y)

print("true intercept:", intercept)
print("true coef:", beta)
pprint(model.summary())
true intercept: -0.4
true coef: [ 2.  -1.5  0.   0.   0.8 -0.3]
{'coef': array([ 1.8487, -1.3858,  0.    ,  0.    ,  0.6956, -0.2889]),
 'coef_se': None,
 'converged': True,
 'duality_gap': 9.242381770491193e-05,
 'duality_gap_tolerance': 0.4427918256609508,
 'inference_available': False,
 'intercept': -0.3917545657840523,
 'intercept_se': None,
 'iterations': 4,
 'l1_ratio': 0.5,
 'objective': 0.6281914787348275,
 'penalty': 0.1,
 'termination_reason': 'Duality gap tolerance reached'}

2 Predictions and feature shifts

The intercept absorbs shifts in predictor origins. This check uses the same penalty and verifies predictions in the corresponding coordinates.

shift = np.array([10.0, -4.0, 7.0, 0.5, -2.0, 3.0])
shifted = ElasticNet(penalty=0.1, l1_ratio=0.5)
shifted.fit(x + shift, y)
np.testing.assert_allclose(model.predict(x), shifted.predict(x + shift), atol=1e-7)
print("Predictions:", model.predict(x[:5]))

l2 = ElasticNet(penalty=0.1, l1_ratio=0.0)
l2.fit(x, y)
print("Pure-L2 coefficients:", l2.summary()["coef"])
Predictions: [-0.3906 -2.3279 -1.4154 -0.8715 -2.7003]
Pure-L2 coefficients: [ 1.8114 -1.3647  0.017   0.0319  0.7053 -0.3211]

Feature centering does not standardize feature scales. Rescaling a column changes the effective penalty; use a training-fitted scaling transformation when coefficients should be penalized on comparable scales.

crabbymetrics 0.9.0

 
  • v0.9 migration

  • Reproduction