Penalties in other packages

Packages that fit the same lasso write its objective differently, so the same fit has a different penalty value in each. This page converts them.

This package minimizes, over covariates standardized to unit variance (with standardize=True, the default; the coefficients are reported on the covariates’ own scale):

1/(2W) sum_i w_i (y_i - b0 - x_i'b)^2
    + penalty * sum_j pf_j (l1_ratio |b_j| + (1 - l1_ratio)/2 b_j^2)

where W is the sum of the weights (the number of rows without weights) and pf_j is 0 for an unpenalized covariate and 1 otherwise.

The lasso (l1_ratio=1)

Package

Its penalty

This package’s penalty

glmnet (alpha = 1)

lambda

lambda

glmnet with penalty.factor

lambda

lambda * sum(pf) / p (glmnet rescales the factors to sum to the number of covariates p)

scikit-learn Lasso

alpha

alpha (on standardized covariates; scikit-learn does not standardize)

hdm rlasso

lambda_j = lambda0 * ups_j (objective RSS + sum lambda_j abs(b_j), centered covariates)

lambda0 / (2n), with penalty factors ups_j / sd_j

Stata lassopack lasso2

lambda (objective RSS / n + lambda / n * sum abs(b_j))

lambda / (2n)

The elastic net and ridge

scikit-learn’s ElasticNet(alpha, l1_ratio) is this package’s elastic_net_stream(penalty=alpha, l1_ratio=l1_ratio) on standardized covariates.

glmnet scales the outcome to unit variance before it fits and scales the coefficients back afterwards, which changes its elastic net’s L2 part. A glmnet fit with lambda and alpha and an outcome whose standard deviation (dividing by n) is s is this package’s fit with

penalty  = lambda * (alpha + (1 - alpha) / s)
l1_ratio = alpha / (alpha + (1 - alpha) / s)

When s is 1 the two are the same. The same holds for glmnet’s ridge (alpha = 0).

scikit-learn’s Ridge(alpha) minimizes sum_i w_i (y_i - x_i’b)^2 + alpha |b|^2; it is this package’s ridge_stream(penalty=alpha / W) on standardized covariates.

The plug-in penalty

penalty="rigorous" computes hdm’s rlasso penalty: lambda0 = 2 c sqrt(n) qnorm(1 - gamma / (2p)) with c = 1.1 and gamma = 0.1 / log(n), and heteroskedastic loadings ups_j = sqrt(mean(x_j^2 e^2)) iterated from residuals as hdm iterates them. With cluster=, the loadings are sqrt(sum_g (sum_{i in g} x_ij e_i)^2 / n) (Belloni, Chernozhukov, Hansen and Kozbur 2016). The selected covariates are hdm’s; the coefficients reported are the lasso’s at that penalty, while hdm’s rlasso reports least squares on the selected covariates (its post = TRUE).

Post-double-selection with instruments

double_selection_iv_stream makes hdm’s selections: rlassoIVselectX’s (lassos of the outcome, the treatment and each instrument on the controls) or, with select_instruments=True, rlassoIV’s (a lasso of the treatment on the instruments and the controls, and a lasso of its post-lasso fit on the controls, with the lassos of the outcome and the treatment).

Its default estimate (method="union") is 2SLS with the union of the selected controls, as Stata’s ivlasso reports under “PDS”. method="partialling_out" gives hdm’s estimate (ivlasso’s “CHS”): the outcome, the treatment and each instrument less its post-lasso fit on its own selected controls, then 2SLS on those residuals. With select_instruments=True, the single instrument is the first-stage fit less its post-lasso fit on the controls selected for it, the treatment’s residual subtracts that same fit, and the lasso of the treatment on the controls is not run, as in rlassoIV. The coefficient is hdm’s. The standard errors differ from hdm’s only by the small-sample factor: hdfe-stream’s divide by N − K, with K the intercept (or the fixed effects) and the treatment, while hdm’s homoscedastic SE divides by N − 1 and its robust SE is HC0. Both estimates are valid after selection; they differ in finite samples.

Information criteria

With RSS the weighted sum of squared residuals, N the number of rows (the sum of the weights for frequency weights), and df the degrees of freedom (the number of nonzero coefficients for the lasso; Zou, Hastie and Tibshirani’s trace for the elastic net and ridge) plus one for the intercept, or plus the fixed effects’ degrees of freedom:

AIC  = N log(RSS / W) + 2 df
BIC  = N log(RSS / W) + log(N) df
EBIC = BIC + 2 xi df log(p),   xi = 1 - log(N) / (2 log(p)), kept within [0, 1]
AICc = N log(RSS / W) + 2 df N / (N - df - 1)

The group lasso

With groups=, each group’s covariates are standardized and orthonormalized (their covariance in the sample is the identity), and the penalty is

penalty * sum_g sqrt(K_g) (l1_ratio ||b_g|| + (1 - l1_ratio)/2 ||b_g||^2)

on the orthonormalized coefficients, K_g the group’s rank: grpreg’s grLasso with lambda = penalty and alpha = l1_ratio. grpreg’s degrees of freedom (less one, for its intercept) are this package’s df.

The penalized logit

lasso_logit_stream and elastic_net_logit_stream minimize

-(1/W) sum_i w_i loglik_i + penalty * sum_j (l1_ratio |b_j| + (1 - l1_ratio)/2 b_j^2)

over standardized covariates: glmnet’s family = "binomial" objective, so penalty is glmnet’s lambda and l1_ratio its alpha. scikit-learn’s LogisticRegression uses C = 1 / (n * penalty) with the unscaled covariates. With a fixed effect, the result equals glmnet’s with the fixed effect’s indicators unpenalized and no intercept. Pass glmnet lambda * k / (k + G), because glmnet rescales penalty factors to sum to the number of columns.

The cross-validated error is the held-out mean binomial deviance (glmnet’s type.measure = "deviance"). The information criteria use the deviance where the linear fits use N log(RSS / W), and count the intercept or the G group effects among the parameters.

The plug-in penalty is hdm’s rlassologit: hdm’s lambda0 = c/2 sqrt(n) qnorm(1 - gamma / (2p)) is penalty = lambda0 / (2n) here, with c = 1.1 and gamma = 0.1 / log n. hdm’s own default is c = 0.5 when post = FALSE. Selected coefficients are the lasso’s; refit them with hdfe-stream’s feglm_stream for post-lasso estimates.

The logit, probit and Poisson model have the same two solvers as the multinomial logit (below). solver="streaming" reads the stored rows once per Newton step and forms each model’s information matrix; solver="in_memory" holds the rows in memory and fits by coordinate descent over them (How it works). The in-memory solver is much faster on wide designs, where the information matrix of hundreds of indicator columns is costly to form and to solve: with about 1,000 columns it was 17 to 130 times faster from 10,000 rows to 100,000. On narrow designs it is faster up to a few hundred thousand rows: with 150 to 270 columns and 5-fold cross-validation, the two cross over at about 400,000 to 500,000 rows on an 8-core laptop and at about 150,000 to 200,000 on a 16-vCPU server, whose threads speed the streamed solver’s passes (the in-memory solver runs one thread per model); at a million rows the streamed solver is about twice as fast on the laptop and 1.6 times on the server. solver="auto", the default, uses the in-memory solver when the rows fit in memory and either the design has at least 512 covariates or there are at most 200,000 rows; otherwise it streams. Near the crossover, choosing the solver yourself can be faster than "auto". At the default tol=1e-7, the in-memory solver’s deviance is typically within about 5e-5 of the exact path’s, the streamed solver’s within about 5e-6.

The penalized multinomial logit

lasso_mlogit_stream and elastic_net_mlogit_stream minimize

-(1/W) sum_i w_i loglik_i + penalty * sum_j sum_l (l1_ratio |b_lj| + (1 - l1_ratio)/2 b_lj^2)

over standardized covariates, with a coefficient b_lj for every outcome level l and covariate j: glmnet’s family = "multinomial" objective with type.multinomial = "ungrouped" (its default), so penalty is glmnet’s lambda and l1_ratio its alpha. Only the differences between levels’ coefficients enter the probabilities; the penalty picks the coefficients themselves, as glmnet’s symmetric parametrization does, so the fit does not depend on which level comes first. With the lasso and an even number of levels, a covariate’s coefficients can be shifted together over a range of values without changing the objective: the differences between levels are unique, the coefficients need not be. The intercepts, and the coefficients of unpenalized covariates, which the penalty does not identify, are centered to sum to zero over the levels; glmnet’s intercepts can differ from them by a shift common to the levels.

Packages with a base level (logit-stream’s mlogit_stream, Stata’s mlogit, R’s nnet::multinom) report the differences b_l - b_base. With a fixed effect, the result equals glmnet’s with the fixed effect’s indicators unpenalized and no intercept, passing glmnet lambda * k / (k + G) as for the binary logit. scikit-learn’s LogisticRegression with an L1 penalty and the multinomial loss uses the same symmetric form, with C = 1 / (n * penalty) and the covariates unscaled.

grouped=True is glmnet’s type.multinomial = "grouped": each covariate’s coefficients over the levels are penalized by their norm,

penalty * sum_j (l1_ratio ||b_j|| + (1 - l1_ratio)/2 ||b_j||^2)

so a covariate enters or leaves every level’s equation at once. Its coefficients are then unique and sum to zero over the levels.

The cross-validated error is the held-out mean multinomial deviance, -2 log-likelihood per unit of weight (glmnet’s type.measure = "deviance"). The information criteria count a covariate with n nonzero coefficients as min(n, J - 1) parameters, J the number of levels (J - 1 for a selected covariate with grouped=True), plus the J - 1 intercepts or the fixed effect’s free group effects.

With a fixed effect, a group whose rows never have some level gets an effect of minus infinity for that level: probability zero, as an unpenalized fit gives it. A fold’s model leaves a group out if its held-out rows have a level its training rows do not.

Two solvers fit the same objective. solver="streaming" reads the stored rows once per Newton step and needs memory only for the information matrices. solver="in_memory" holds the rows in memory and fits by coordinate descent over them (How it works): faster on small samples, slower on large ones, where every pass over a column waits on memory. solver="auto", the default, uses the in-memory solver when the rows fit in memory and each model’s per-row state (16 bytes per row and outcome level) fits in both the CPU’s last-level cache and 6 MB, about 100,000 rows of a four-level outcome; otherwise it streams. Where the two cross over depends on the machine. For the benchmarks’ four-level class-of-worker model with 5-fold cross-validation, the crossover is about 150,000 rows on an 8-core laptop with a 12 MB cache and about 50,000 rows on a 16-vCPU server, whose much larger cache is shared with the rest of the processor. Between those sizes, choosing the solver yourself can be faster than "auto": by about 10% on the server below 100,000 rows, and by up to 20% on the laptop above it. The two stop at the same tol by different tests, so their fits can differ by about the tolerance: at the default tol=1e-7, the streamed solver’s deviance is typically within about 1e-5 of the exact path’s, and the in-memory solver’s within about 5e-5, close to glmnet’s at its default thresh.

The penalized ordered logit and probit

lasso_ologit_stream, lasso_oprobit_stream and their elastic-net forms fit P(y <= j) = F(cut_j - x’b) with F the logistic or normal distribution function, minimizing the same objective with one coefficient per covariate and the cutpoints unpenalized: ordinalNet’s family = "cumulative" with parallelTerms = TRUE and nonparallelTerms = FALSE. penalty is ordinalNet’s lambda and l1_ratio its alpha. ordinalNet writes the model as F(c_j + x’b), so its slopes are this package’s with the opposite sign, and its intercepts are the cutpoints.

ordinalNet takes weights through a matrix outcome (each row’s weight in its level’s column), and standardizes the covariates without them; with weights, pass both packages standardize=False, or expect the penalty to weigh the covariates slightly differently.

With a fixed effect, each group has a location a_g, P(y <= j) = F(cut_j - x’b - a_g), and the first cutpoint is 0. ordinalNet’s equivalent has the group indicators (one dropped) unpenalized: a_g = -(c_1 + d_g) and cut_j = c_j - c_1, with d the indicators’ coefficients. A group whose rows are all at the lowest level, or all at the highest, has an infinite location and is dropped.