penalized-stream¶
Lasso, ridge and elastic net for data that do not fit in memory, with any number of absorbed fixed effects, cross-validated from one pass over the data, and inference after selection: post-double-selection (with instruments, too), double machine learning, the debiased lasso and stability selection; the penalized logit, probit and Poisson model, with post-double-selection for a binary outcome; and the penalized multinomial logit and ordered logit and probit. Results are tested against R’s glmnet, grpreg, hdm, hdi, stabs, ordinalNet and DoubleML, and scikit-learn.
from penalized_stream import double_selection_stream, lasso_stream
fit = lasso_stream(
"log_wage ~ C(age) + C(education) + C(occupation) + C(industry) | puma_id",
"acs/*.parquet", # a Parquet path or glob, never fully loaded
workdir="scratch", # where intermediates go: disk, not memory
)
fit.summary() # the selected predictors at lambda_min
fit.plot_cv()
gap = double_selection_stream(
"log_wage ~ female | puma_id",
"acs/*.parquet",
controls="C(age) + C(education) + C(occupation) + C(industry) + C(hours)",
vcov={"CRV1": "puma_id"},
)
gap.summary() # the gap, with clustered standard errors
Formulas follow pyfixest
syntax and are compiled by
hdfe-stream, so coefficient names
and coding match pyfixest’s. The fixed effects after | are partialled out as
hdfe-stream’s regressions partial them out, and are never penalized.
When to use this, and when not to¶
To choose a method, see Choosing a method.
Use penalized-stream when the data are large (from about 100,000 rows to more than fits in memory), when there are fixed effects with many levels, or both, and the number of predictors is in the tens to thousands:
a lasso, ridge, elastic net or group lasso, with the penalty chosen by cross-validation, an information criterion or the plug-in rule;
a penalized logit, probit, Poisson, multinomial logit or ordered logit or probit, with no fixed effect or one;
cross-validation with folds of rows or of groups, which costs no more reading than one fit;
the effect of a treatment with many candidate controls (post-double-selection, double machine learning), with robust or clustered standard errors;
confidence intervals for many coefficients at once (the debiased lasso), or a reliable list of selected predictors (stability selection).
On the benchmarks, five models fitted to the American Community Survey from 10,000 rows to all 8 million people, penalized-stream was the fastest library on every model from about 100,000 rows up, and fitted every model on all the rows in under 9 GB of memory:
Least squares (the cross-validated lasso of the wage on about 2,000 indicators): faster than glmnet from 30,000 rows, by 4 times at 100,000 and 33 times at 3 million; scikit-learn’s
LassoCVand biglasso took more than an hour at 1 million rows, and hdm’s post-double-selection ran out of 32 GB at 300,000.Logit, probit and Poisson. Two solvers, chosen by
solver="auto": one that works on the rows in memory, used up to 200,000 rows or for a wide design (512 covariates or more) when the rows fit the memory budget, and one that streams the rows, used beyond. On the employment logit penalized-stream was faster than glmnet at every size, 7 times on all 8 million people, in 3.2 GB where glmnet needed 36 GB.Multinomial logit: the in-memory solver is the fastest library at 10,000 and 30,000 rows; from 100,000 up the streamed one is, 2 to 4 times faster than glmnet, which ran out of 64 GB at 3 million rows. With the grouped penalty it was 18 times faster than glmnet’s at 1 million.
Ordered logit: 110 to 400 times faster than ordinalNet, which did not finish 1 million rows in 6 hours.
Use something else when:
There are more predictors than rows, or, for least squares and for a logit, probit or Poisson whose rows do not fit in memory, more than about 20,000 predictors: the cross-products grow with the square of the width, and a solver that works on the rows (glmnet, scikit-learn, skglm) is the right tool.
The data are small, and seconds matter: at 10,000 rows glmnet is as fast or a little faster (3.6 s against 4.9 s on the wage lasso, 30 s against 36 s on the grouped multinomial logit), and uses less memory.
You need a penalized logit, probit, Poisson, multinomial or ordered regression with several fixed effects: not available (they take no fixed effect or one); use glmnet, glum or scikit-learn with the fixed effects as dummies.
You need ordinalNet’s semi-parallel or nonparallel ordered models: not available.
You want machine learning beyond linear models (forests, boosting, neural nets).
How it is fast¶
One pass, then small matrices. A squared-error loss needs the data only through its cross-products. One pass over the rows (with the fixed effects partialled out) accumulates them, and the whole penalty path is solved from them, without touching the rows again.
Cross-validation in the same pass. The pass keeps each fold’s cross-products apart, so each fold’s training fit and held-out error come from the same small matrices: a path of 100 penalties over 5 folds takes well under a second, whatever the number of rows.
Penalized GLMs from shared reads, or on the rows in memory. For a logit, probit, Poisson, multinomial or ordered model, each Newton step is one read of a compact copy of the rows, shared by every cross-validation model. When the rows fit in memory and that is faster, coordinate descent on the rows replaces the reads.
Fixed effects without dummies. The fixed effects are partialled out by hdfe-stream, as its regressions do, so a fixed effect with millions of levels costs no more columns.
See How it works.