How it works

The lasso, ridge and elastic net: one pass, then small matrices

With a squared-error loss, a penalized regression needs the data only through its cross-products: X’WX, X’Wy, y’Wy, and the weighted sums and counts that center them. The pass reads the rows once, in batches, partials the fixed effects out of every variable (as hdfe-stream’s regressions do: the alternating projections run once, for all the variables together), and adds each batch’s products into those matrices. Memory and the work after the pass grow with the square of the number of predictors, not with the number of rows: 1,000 predictors take 8 MB per matrix.

The penalty path is then solved from the matrices alone, by coordinate descent with covariance updates and warm starts down a path of penalties (Friedman, Hastie and Tibshirani 2010), and the sequential strong rule (Tibshirani et al. 2012) to skip the predictors that cannot enter. A path of 100 penalties takes milliseconds to a second, and never touches the rows.

The pass keeps the cross-products in a numerically safe form: the variables are shifted by a pilot mean before they are multiplied (so centering later does not cancel digits), and a training set’s matrix is the sum of the other folds’ matrices, never the total less the held-out fold.

Cross-validation from the same pass

The pass keeps one set of cross-products per fold, by the row’s fold number (of rows, or of the groups of fold_by=). Fold k’s training matrix is the sum of the others; its held-out error at any coefficients b is

(y'Wy - 2 b'X'Wy + b'X'WX b) / W

from fold k’s own matrix. So every fold’s training path and every held-out error along it come from the K matrices, and cross-validation costs no more reading than one fit. The same matrices give the information criteria (their residual sum of squares), stability selection’s half-samples (each a union of shards, from 20 shard matrices) and double machine learning’s cross-fitting.

With fixed effects, the pass partials them out once, from all the rows (cv_fixed_effects="full_sample"). "refit" estimates them again on each fold’s training rows; see Fixed effects in the cross-validation.

The plug-in penalty

Belloni, Chen, Chernozhukov and Hansen’s penalty has a loading for each predictor, sqrt(mean of x_j² e²) at the current residuals e (or, with clusters, from each cluster’s sum of x_j e). The residuals change with the fit, so it iterates: each iteration is one pass over a saved copy of the partialled-out rows to recompute the loadings, then a lasso from the cross-products. hdm’s settings (c = 1.1, gamma = 0.1 / log n, up to 15 iterations) are the defaults, and the selections match hdm’s.

Inference

Post-double-selection runs its lassos from one pass (the outcome, each treatment, and for the IV version each instrument or the first stage, all columns of the same cross-products), then fits the final regression on the union of the selected controls with hdfe-stream, so it has hdfe-stream’s standard errors, tables and tests. Double machine learning takes each fold’s nuisance fits from the cross-products of the other folds and the residuals in one more pass. The debiased lasso’s nodewise lassos (one per coefficient) all come from the same matrix.

The penalized logit, probit and Poisson model

A logit’s loss (or a probit’s, or a Poisson model’s) is not quadratic, so its cross-products change with the coefficients. Each proximal Newton step (Lee, Sun and Saunders 2014) is one pass over the rows that forms the gradient and the information matrix at the current coefficients; the step then solves a weighted lasso from that matrix. Every cross-validation model (the full sample and each fold’s training rows) moves along the path together, so one pass serves them all, and the strong rule keeps each pass’s matrix to the predictors that can be active. A row of indicators has few nonzeros, and each pass forms its products from those alone, once for all the models. A path of 100 penalties takes about two to five passes per penalty, read from hdfe-stream’s compact copy of the rows rather than from the source: that is the cost to plan for on large data.

When the rows fit in memory, the in-memory solver replaces the passes and the information matrices with coordinate descent over the rows themselves, each update costing the nonzeros of one column. It is the faster solver on wide designs at any size, and on narrow designs up to a few hundred thousand rows (conventions).

Sources

The algorithms are the published ones, implemented in this package’s own code:

  • Coordinate descent for the lasso, elastic net and penalized GLMs, with covariance updates (from cross-products) or a working residual (from the rows), warm starts down a path of penalties, the active-set strategy, and the multinomial logit’s symmetric form: J. Friedman, T. Hastie and R. Tibshirani (2010), “Regularization paths for generalized linear models via coordinate descent,” Journal of Statistical Software 33(1).

  • The sequential strong rule, with the optimality conditions checked after: R. Tibshirani, J. Bien, J. Friedman, T. Hastie, N. Simon, J. Taylor and R. J. Tibshirani (2012), “Strong rules for discarding predictors in lasso-type problems,” Journal of the Royal Statistical Society B 74(2).

  • The proximal Newton iteration, its line search, and solving each step’s quadratic only as accurately as the iteration’s progress warrants: J. D. Lee, Y. Sun and M. A. Saunders (2014), “Proximal Newton-type methods for minimizing composite functions,” SIAM Journal on Optimization 24(3); G.-X. Yuan, C.-H. Ho and C.-J. Lin (2012), “An improved GLMNET for L1-regularized logistic regression,” Journal of Machine Learning Research 13.

  • The grouped multinomial penalty’s block update: N. Simon, J. Friedman and T. Hastie (2013), “A blockwise descent algorithm for group-penalized multiresponse and multinomial regression,” arXiv:1311.6529.

  • Anderson extrapolation of the iterates, kept only when it lowers the objective: D. G. Anderson (1965), “Iterative procedures for nonlinear integral equations,” Journal of the ACM 12(4); D. Scieur, A. d’Aspremont and F. Bach (2016), “Regularized nonlinear acceleration,” NeurIPS; Q. Bertrand and M. Massias (2021), “Anderson acceleration of coordinate descent,” AISTATS. The in-memory solvers extrapolate the Newton iterates, where Bertrand and Massias extrapolate coordinate descent’s sweeps.

  • Reusing the curvature for a few Newton iterations: N. Doikov, E. M. Chayti and M. Jaggi (2023), “Second-order optimization with lazy Hessians,” ICML.

  • The plug-in penalty: A. Belloni, D. Chen, V. Chernozhukov and C. Hansen (2012), “Sparse models and methods for optimal instruments with an application to eminent domain,” Econometrica 80(6).

The in-memory solvers center the design’s columns implicitly, with sums, so that a sparse design stays sparse; with an absorbed fixed effect they center each column within the effect’s groups, weighted by the curvature (the Frisch-Waugh-Lovell partialling of the group indicators), which decouples the columns from the group effects. The fixed-effect handling, the cross-validation from shared passes, and the choice between the streamed and in-memory solvers are this package’s.