crabbymetrics
  • Home
  • API
    • API Overview
    • Regression And GLMs
    • Survival / Event-Time
    • Causal Inference And Panels
    • Hypothesis Testing And Utilities
    • Transforms
    • Estimation Interfaces
  • Binding Crash Course
  • Regression And GLMs
    • 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
    • FTRL
    • MEstimator Poisson
  • Causal Inference
    • Balancing Weights
    • EPLM
    • Average Derivative
    • Double ML And AIPW
    • Richer Regression
    • TwoSLS
    • Synthetic Control
    • Synthetic DID
    • Horizontal Panel Ridge
    • Matrix Completion
    • Interactive Fixed Effects
    • Staggered Panel Event Study
    • Joint Hypothesis Tests
  • Transforms
    • PCA And Kernel Basis
  • 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
  • Optimization
    • Optimizers
    • GMM With Optimizers
  • Ding: First Course
    • Overview And TOC
    • Ch 1 Correlation And Simpson
    • Ch 2 Potential Outcomes
    • Ch 3 CRE And Fisher RT
    • Ch 4 CRE And Neyman
    • Ch 9 Bridging Finite And Superpopulation
    • Ch 11 Propensity Score
    • Ch 12 Double Robust ATE
    • Ch 13 Double Robust ATT
    • Ch 21 Experimental IV
    • Ch 23 Econometric IV
    • Ch 27 Mediation

On this page

  • 1 Where it fits
  • 2 Criterion and solver
  • 3 Inference
  • 4 Performance and numerical behavior
  • 5 Python API
  • 6 Minimal example
  • 7 summary() contract

ElasticNet

Coordinate-descent elastic net regression

from _api_doc_utils import *

1 Where it fits

Group: Regression

ElasticNet estimates a penalized linear model with a convex combination of L1 and L2 penalties:

\[ \min_{\alpha,\beta}\; \frac{1}{2n}\sum_i (y_i-\alpha-x_i'\beta)^2 + \lambda\left(\rho\|\beta\|_1 + \frac{1-\rho}{2}\|\beta\|_2^2\right). \]

It is useful when the goal is prediction or sparse regularized coefficients rather than classical inference.

2 Criterion and solver

The delegated Linfa coordinate-descent solver minimizes

\[ \frac{1}{2n}\|y-\alpha\mathbf 1-X\beta\|_2^2 +\lambda\rho\|\beta\|_1 +\frac{\lambda(1-\rho)}{2}\|\beta\|_2^2, \]

where \(\lambda\) is the penalty and \(\rho\) is the L1 ratio. The intercept is fit after centering and is not penalized. Coordinate updates use soft thresholding, and convergence is checked with coefficient-change and duality-gap criteria up to the configured iteration budget. The wrapper delegates parameter validation to Linfa.

The class does not standardize \(X\). Because both the L1 and L2 penalties act on raw coefficients, users must scale features explicitly when a common penalty across columns is intended.

3 Inference

The summary deliberately returns no analytic covariance or standard errors. L1 selection makes naive inverse-Hessian inference inappropriate, and the implementation does not provide debiasing, selective inference, or cross-validated penalty selection. The pairs bootstrap refits the same fixed \((\lambda,\rho)\) in every resample and returns raw intercept and coefficient draws. Those draws can describe algorithmic and sampling stability, but the class does not turn them into confidence intervals and does not account for tuning uncertainty.

4 Performance and numerical behavior

One complete coordinate sweep is \(O(np)\) for dense input, so runtime is approximately \(O(Inp)\) for \(I\) iterations, plus \(O(np)\) storage and residual work. Sparse coefficients do not reduce the stored dense design. Highly correlated or poorly scaled columns can slow coordinate descent and make the selected support unstable. At \(\rho=0\) this solver is a coordinate-descent ridge fit with a different penalty normalization from the package’s dedicated Ridge class; the same numeric penalty therefore does not imply the same objective.

5 Python API

Constructor: cm.ElasticNet

Use ElasticNet(penalty, l1_ratio, tolerance, max_iterations), then fit(x, y), predict(x), summary(), and optionally bootstrap(B, seed=None). The summary reports point estimates and marks analytic inference unavailable. Bootstrap coefficient draws are stability diagnostics, not automatic confidence intervals.

print(inspect.signature(cm.ElasticNet))
(penalty=1.0, l1_ratio=0.5, tolerance=0.0001, max_iterations=1000)
cls = cm.ElasticNet
display(HTML(html_table(["Public method"], public_methods(cls))))
Public method
bootstrap(self, /, n_bootstrap, seed=None)
fit(self, /, x, y)
predict(self, /, x)
summary(self, /)

6 Minimal example

rng = np.random.default_rng(4)
x = rng.normal(size=(180, 8))
y = 0.4 + x[:, :3] @ np.array([1.0, -0.8, 0.5]) + rng.normal(scale=0.5, size=180)
model = cm.ElasticNet(penalty=0.05, l1_ratio=0.7)
model.fit(x, y)
print(model.summary()['coef'])
print(model.predict(x[:3]))
[ 0.90319681 -0.79760879  0.45662966 -0.01442286 -0.01857245 -0.
  0.         -0.        ]
[ 0.6312702  -1.2665473  -2.43483936]

7 summary() contract

The table below is generated by fitting the live class in this repository and then inspecting summary(). Shapes are shown because most values are plain NumPy arrays or scalars.

rng = np.random.default_rng(104)
x = rng.normal(size=(90, 5))
y = 0.4 + x[:, :2] @ np.array([1, -0.8]) + rng.normal(size=90) * 0.3
model = cm.ElasticNet(penalty=0.05, l1_ratio=0.7)
model.fit(x, y)
summary = model.summary()
display(HTML(html_table(["summary() key", "shape"], summary_shape_rows(summary))))
summary() key shape
intercept ()
coef (5,)
penalty ()
l1_ratio ()
inference_available ()
intercept_se ()
coef_se ()