Penalized regression¶
One function per method. They share the formula and data arguments, the
penalty, the folds, weights, standardization, unpenalized predictors and
hdfe-stream’s options (workdir, n_threads, max_memory_gb, …).
- penalized_stream.lasso_stream(fml, data, *, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, cv_fixed_effects='full_sample', n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, groups=None, cluster=None, rigorous_options=None, **options)[source]¶
Lasso: least squares with an L1 penalty on the covariates, from one pass over the data (two or more for
penalty="rigorous").Minimizes
1/(2W) sum_i w_i (y_i - b0 - x_i'b)^2 + penalty * sum_j |b_j|over standardized covariates, W the sum of the weights, after the fixed effects are partialled out: glmnet’s and scikit-learn’s lasso.- Parameters:
fml (str) – A pyfixest formula,
"y ~ x1 + x2 + i(region) | worker + firm": the covariates are penalized; the fixed effects after|are partialled out (never penalized). One model; no instruments or varying slopes.data (str, Path, polars.LazyFrame or polars.DataFrame) – A Parquet path or glob, a LazyFrame or a DataFrame.
penalty (str or float, default "cv") – How to choose the penalty:
"cv"(the one with the smallest cross-validated mean squared error, lambda_min),"cv_1se"(the largest within one standard error of it, lambda_1se), an information criterion ("aic","bic","ebic","aicc"),"rigorous"(Belloni, Chen, Chernozhukov and Hansen’s plug-in penalty, as hdm’s rlasso), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized: a right-hand side (
"age + i(year)", added to the model) or names of the formula’s covariates.groups ("factors" or dict, optional) – Keep or drop covariates together (the group lasso; grpreg’s): with
"factors"each factor’s indicators (C(x),i(x), a string column, an interaction of factors) are one group and every other covariate is a group of its own; a dict{name: [covariates]}names the groups. Each group is orthonormalized and penalized by the norm of its coefficients times the square root of its size, so a factor’s selection does not depend on its reference level (andstandardizehas no effect). Not withpenalty="rigorous".weights (str or polars.Expr, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5) – Folds for cross-validation (used by
penalty="cv"and"cv_1se").fold_by (str, optional) – What the folds are made of:
"rows"(each row hashed by its position) or a column (each value’s rows in one fold). Default: the groups of the fixed effect with the most levels (the one hdfe-stream streams), or rows without fixed effects. With one fixed effect, folds of its groups make the cross-validation exact.seed (int, default 0) – Seed of the fold assignment.
cv_fixed_effects ({"full_sample", "refit"}, default "full_sample") – With more than one fixed effect:
"full_sample"partials them out once, from all the rows, so each fold’s held-out rows are residualized with effects their own outcomes helped estimate (one pass);"refit"estimates them again on each fold’s training rows (exact, 8 to 15 times the time in simulations). The two choose the same penalty or a neighboring one, with the same prediction error; but with small cells in every dimension the default’s cross-validated error can be 10% or more below the out-of-sample error, so use"refit"when that number itself matters. See docs/fixed_effects_cv.md.n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – The smallest penalty of the path as a share of the largest; default 1e-4 when there are more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance (the coefficients are reported on their own scale).
tol (float, default 1e-7) – Convergence tolerance of the coordinate descent (glmnet’s).
**options – hdfe-stream’s options:
workdir,n_threads,max_memory_gb,fixef_rm,connected,stream,verbose, …
- Returns:
PenalizedResult
cluster (str, optional) – With
penalty="rigorous": cluster-robust penalty loadings, by this column ("a^b"for a combination).rigorous_options (dict, optional) – With
penalty="rigorous":c(1.1),gamma(0.1 / log n),max_iter(15),loadings_tol(1e-5),homoskedastic(False).
- penalized_stream.ridge_stream(fml, data, *, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, cv_fixed_effects='full_sample', n_lambdas=100, lambda_min_ratio=None, standardize=True, **options)[source]¶
Ridge regression: least squares with an L2 penalty on the covariates, from one pass over the data, solved exactly at each penalty.
Minimizes 1/(2W) sum_i w_i (y_i - b0 - x_i’b)^2 + penalty/2 * sum_j b_j^2 over standardized covariates. The default path starts at glmnet’s largest ridge penalty (1000 times the lasso’s).
- Parameters:
fml (str) – A pyfixest formula,
"y ~ x1 + x2 + i(region) | worker + firm": the covariates are penalized; the fixed effects after|are partialled out (never penalized). One model; no instruments or varying slopes.data (str, Path, polars.LazyFrame or polars.DataFrame) – A Parquet path or glob, a LazyFrame or a DataFrame.
penalty (str or float, default "cv") – How to choose the penalty:
"cv"(the one with the smallest cross-validated mean squared error, lambda_min),"cv_1se"(the largest within one standard error of it, lambda_1se), an information criterion ("aic","bic","ebic","aicc"), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized: a right-hand side (
"age + i(year)", added to the model) or names of the formula’s covariates.weights (str or polars.Expr, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5) – Folds for cross-validation (used by
penalty="cv"and"cv_1se").fold_by (str, optional) – What the folds are made of:
"rows"(each row hashed by its position) or a column (each value’s rows in one fold). Default: the groups of the fixed effect with the most levels (the one hdfe-stream streams), or rows without fixed effects. With one fixed effect, folds of its groups make the cross-validation exact.seed (int, default 0) – Seed of the fold assignment.
cv_fixed_effects ({"full_sample", "refit"}, default "full_sample") – With more than one fixed effect:
"full_sample"partials them out once, from all the rows, so each fold’s held-out rows are residualized with effects their own outcomes helped estimate (one pass);"refit"estimates them again on each fold’s training rows (exact, 8 to 15 times the time in simulations). The two choose the same penalty or a neighboring one, with the same prediction error; but with small cells in every dimension the default’s cross-validated error can be 10% or more below the out-of-sample error, so use"refit"when that number itself matters. See docs/fixed_effects_cv.md.n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – The smallest penalty of the path as a share of the largest; default 1e-4 when there are more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance (the coefficients are reported on their own scale).
tol (float, default 1e-7) – Convergence tolerance of the coordinate descent (glmnet’s).
**options – hdfe-stream’s options:
workdir,n_threads,max_memory_gb,fixef_rm,connected,stream,verbose, …
- Returns:
PenalizedResult
- penalized_stream.elastic_net_stream(fml, data, *, l1_ratio=0.5, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, cv_fixed_effects='full_sample', n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, groups=None, **options)[source]¶
Elastic net: least squares with a mix of L1 and L2 penalties on the covariates, from one pass over the data.
Minimizes
1/(2W) sum_i w_i (y_i - b0 - x_i'b)^2 + penalty * sum_j (l1_ratio |b_j| + (1 - l1_ratio)/2 b_j^2)over standardized covariates: scikit-learn’s ElasticNet. glmnet’s elastic net (alpha = l1_ratio) differs unless the outcome’s standard deviation is 1.- l1_ratiofloat, default 0.5
The L1 share of the penalty: 1 is the lasso, 0 ridge.
- Parameters:
fml (str) – A pyfixest formula,
"y ~ x1 + x2 + i(region) | worker + firm": the covariates are penalized; the fixed effects after|are partialled out (never penalized). One model; no instruments or varying slopes.data (str, Path, polars.LazyFrame or polars.DataFrame) – A Parquet path or glob, a LazyFrame or a DataFrame.
penalty (str or float, default "cv") – How to choose the penalty:
"cv"(the one with the smallest cross-validated mean squared error, lambda_min),"cv_1se"(the largest within one standard error of it, lambda_1se), an information criterion ("aic","bic","ebic","aicc"), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized: a right-hand side (
"age + i(year)", added to the model) or names of the formula’s covariates.groups ("factors" or dict, optional) – Keep or drop covariates together (the group lasso; grpreg’s): with
"factors"each factor’s indicators (C(x),i(x), a string column, an interaction of factors) are one group and every other covariate is a group of its own; a dict{name: [covariates]}names the groups. Each group is orthonormalized and penalized by the norm of its coefficients times the square root of its size, so a factor’s selection does not depend on its reference level (andstandardizehas no effect). Not withpenalty="rigorous".weights (str or polars.Expr, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5) – Folds for cross-validation (used by
penalty="cv"and"cv_1se").fold_by (str, optional) – What the folds are made of:
"rows"(each row hashed by its position) or a column (each value’s rows in one fold). Default: the groups of the fixed effect with the most levels (the one hdfe-stream streams), or rows without fixed effects. With one fixed effect, folds of its groups make the cross-validation exact.seed (int, default 0) – Seed of the fold assignment.
cv_fixed_effects ({"full_sample", "refit"}, default "full_sample") – With more than one fixed effect:
"full_sample"partials them out once, from all the rows, so each fold’s held-out rows are residualized with effects their own outcomes helped estimate (one pass);"refit"estimates them again on each fold’s training rows (exact, 8 to 15 times the time in simulations). The two choose the same penalty or a neighboring one, with the same prediction error; but with small cells in every dimension the default’s cross-validated error can be 10% or more below the out-of-sample error, so use"refit"when that number itself matters. See docs/fixed_effects_cv.md.n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – The smallest penalty of the path as a share of the largest; default 1e-4 when there are more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance (the coefficients are reported on their own scale).
tol (float, default 1e-7) – Convergence tolerance of the coordinate descent (glmnet’s).
**options – hdfe-stream’s options:
workdir,n_threads,max_memory_gb,fixef_rm,connected,stream,verbose, …
- Returns:
PenalizedResult
- penalized_stream.lasso_logit_stream(fml, data, *, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, rigorous_options=None, solver='auto', **options)¶
The lasso logit: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum_j |b_j|over standardized covariates, W the sum of the weights: glmnet’s binomial objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a 0/1 outcome, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance),"rigorous"(Belloni, Chernozhukov and Wei’s plug-in penalty, as hdm’s rlassologit: c qnorm(1 - gamma / (2p)) / (4 sqrt(n));rigorous_optionssetsc(1.1) andgamma(0.1 / log n)), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
rigorous_options (dict, optional) – With
penalty="rigorous":candgamma.
- penalized_stream.elastic_net_logit_stream(fml, data, *, l1_ratio=0.5, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net logit: a mix of L1 and L2 penalties.
Minimizes
-(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, W the sum of the weights: glmnet’s binomial objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a 0/1 outcome, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.
- penalized_stream.lasso_probit_stream(fml, data, *, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, rigorous_options=None, solver='auto', **options)¶
The lasso probit: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum_j |b_j|over standardized covariates, W the sum of the weights: glmnet’s binomial, with the probit link in place of the logit, objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a 0/1 outcome, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
- penalized_stream.elastic_net_probit_stream(fml, data, *, l1_ratio=0.5, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net probit: a mix of L1 and L2 penalties.
Minimizes
-(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, W the sum of the weights: glmnet’s binomial, with the probit link in place of the logit, objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a 0/1 outcome, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.
- penalized_stream.lasso_poisson_stream(fml, data, *, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, rigorous_options=None, solver='auto', **options)¶
The lasso Poisson model: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum_j |b_j|over standardized covariates, W the sum of the weights: glmnet’s Poisson objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a nonnegative outcome (a count), the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that is always zero) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
- penalized_stream.elastic_net_poisson_stream(fml, data, *, l1_ratio=0.5, penalty='cv', unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net Poisson model: a mix of L1 and L2 penalties.
Minimizes
-(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, W the sum of the weights: glmnet’s Poisson objective. The intercept, or with one absorbed fixed effect each group’s effect, is not penalized. The data are stored once, by hdfe-stream. With the streamed solver each Newton step is one read of them, shared by the cross-validation’s models, and a path takes a few reads per penalty; the in-memory solver (solver) holds them in memory instead.- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": a nonnegative outcome (a count), the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that is always zero) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effect would be infinite in a fold’s training rows (or that has none) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than covariates, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (glmnet’s; with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them;"streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows fit withinmax_memory_gb(else the machine’s available memory) and either the design has at least 512 covariates (where the streamed solver’s information matrices are costly: with about 1,000 indicator columns the in-memory solver was 17 to 130 times faster from 10,000 rows to 100,000) or there are at most 200,000 rows. Otherwise it streams. On narrow designs the solvers cross over at about 400,000 to 500,000 rows on an 8-core laptop and 150,000 to 200,000 on a 16-vCPU server; between those, setting the solver can be faster than"auto".**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedLogitResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.
- penalized_stream.lasso_mlogit_stream(fml, data, *, penalty='cv', grouped=False, levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The lasso multinomial logit: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum |b|over standardized covariates, W the sum of the weights: glmnet’s multinomial objective, with its ungrouped penalty (each level’s coefficient on its own); every covariate has a coefficient for each outcome level, identified by the penalty (glmnet’s symmetric form). The intercepts, or with one absorbed fixed effect each group’s effects, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an outcome with two or more unordered levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (their order only names the coefficients); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (an outcome that never varies, or a level its held-out rows have and its training rows do not) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
grouped (bool, default False) – glmnet’s
type.multinomial = "grouped": penalize each covariate’s coefficients for all the levels by their norm, so that the covariate enters or leaves every level’s equation together (its coefficients are then unique, and sum to zero over the levels). The default penalizes each coefficient on its own (glmnet’s"ungrouped").
- penalized_stream.elastic_net_mlogit_stream(fml, data, *, l1_ratio=0.5, penalty='cv', grouped=False, levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net multinomial logit: a mix of L1 and L2 penalties.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum (l1_ratio |b| + (1 - l1_ratio)/2 b^2)over standardized covariates, W the sum of the weights: glmnet’s multinomial objective, with its ungrouped penalty (each level’s coefficient on its own); every covariate has a coefficient for each outcome level, identified by the penalty (glmnet’s symmetric form). The intercepts, or with one absorbed fixed effect each group’s effects, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an outcome with two or more unordered levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (an outcome that never varies) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (their order only names the coefficients); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (an outcome that never varies, or a level its held-out rows have and its training rows do not) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.
grouped (bool, default False) – glmnet’s
type.multinomial = "grouped": penalize each covariate’s coefficients for all the levels by their norm, so that the covariate enters or leaves every level’s equation together (its coefficients are then unique, and sum to zero over the levels). The default penalizes each coefficient on its own (glmnet’s"ungrouped").
- penalized_stream.lasso_ologit_stream(fml, data, *, penalty='cv', levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The lasso ordered logit: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum |b|over standardized covariates, W the sum of the weights: ordinalNet’s for its parallel cumulative logit, with P(y <= j) = F(cut_j - x’b) (ordinalNet’s slopes have the opposite sign). The cutpoints, and with one absorbed fixed effect each group’s location, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an ordered outcome with two or more levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (every row at the lowest level, or every row at the highest) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (lowest first); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (every training row at the lowest level, or every one at the highest) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
- penalized_stream.elastic_net_ologit_stream(fml, data, *, l1_ratio=0.5, penalty='cv', levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net ordered logit: a mix of L1 and L2 penalties.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum (l1_ratio |b| + (1 - l1_ratio)/2 b^2)over standardized covariates, W the sum of the weights: ordinalNet’s for its parallel cumulative logit, with P(y <= j) = F(cut_j - x’b) (ordinalNet’s slopes have the opposite sign). The cutpoints, and with one absorbed fixed effect each group’s location, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an ordered outcome with two or more levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (every row at the lowest level, or every row at the highest) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (lowest first); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (every training row at the lowest level, or every one at the highest) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.
- penalized_stream.lasso_oprobit_stream(fml, data, *, penalty='cv', levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The lasso ordered probit: an L1 penalty on the covariates.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum |b|over standardized covariates, W the sum of the weights: ordinalNet’s for its parallel cumulative probit, with P(y <= j) = Phi(cut_j - x’b) (ordinalNet’s slopes have the opposite sign). The cutpoints, and with one absorbed fixed effect each group’s location, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an ordered outcome with two or more levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (every row at the lowest level, or every row at the highest) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (lowest first); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (every training row at the lowest level, or every one at the highest) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
- penalized_stream.elastic_net_oprobit_stream(fml, data, *, l1_ratio=0.5, penalty='cv', levels=None, unpenalized=None, weights=None, weights_type='aweights', n_folds=5, fold_by=None, seed=0, n_lambdas=100, lambda_min_ratio=None, standardize=True, tol=1e-07, solver='auto', **options)¶
The elastic-net ordered probit: a mix of L1 and L2 penalties.
Minimizes
-(1/W) sum_i w_i loglik_i + penalty * sum (l1_ratio |b| + (1 - l1_ratio)/2 b^2)over standardized covariates, W the sum of the weights: ordinalNet’s for its parallel cumulative probit, with P(y <= j) = Phi(cut_j - x’b) (ordinalNet’s slopes have the opposite sign). The cutpoints, and with one absorbed fixed effect each group’s location, are not penalized. Each step of the fit is one read of the data (stored once, by logit-stream), shared by the cross-validation’s models; a path takes a few reads per penalty. Needs logit-stream (pip install 'penalized-stream[logit]').- Parameters:
fml (str) –
"y ~ x1 + x2 + i(region)"or"y ~ x1 + x2 | firm": an ordered outcome with two or more levels, the covariates (penalized) and at most one fixed effect. Groups of the fixed effect whose effects would be infinite (every row at the lowest level, or every row at the highest) are dropped.data (str, Path, polars.LazyFrame or polars.DataFrame)
penalty (str or float, default "cv") –
"cv"(the smallest cross-validated deviance, lambda_min),"cv_1se", an information criterion ("aic","bic","ebic","aicc", from the deviance), or a value.levels (list, optional) – The outcome’s levels, in order (lowest first); default its sorted values (an Enum’s own order).
unpenalized (str or list of str, optional) – Covariates left unpenalized, as for
lasso_stream.weights (str, optional) – Analytic (or, with
weights_type="fweights", frequency) weights.n_folds (int, default 5)
fold_by (str, optional) –
"rows"(the default) or a column whose values make the folds. Not the fixed effect: a held-out group would have no estimated effect. A group whose effects would be infinite in a fold’s training rows (every training row at the lowest level, or every one at the highest) is left out of that fold’s model, and its held-out rows out of that fold’s deviance.seed (int, default 0)
n_lambdas (int, default 100)
lambda_min_ratio (float, optional) – Default 1e-4 with more rows than penalized coefficients, else 1e-2 (glmnet’s).
standardize (bool, default True) – Penalize the covariates as if scaled to unit variance, by their standard deviation over each model’s rows (with a fixed effect too, not the within-group deviation).
tol (float, default 1e-7) – Convergence of each penalty’s Newton steps: the largest change in a coefficient, squared and weighted by its information.
solver ({"auto", "in_memory", "streaming"}, default "auto") – How each penalty’s fit is computed.
"in_memory"holds the rows in memory and fits by coordinate descent over them (Friedman, Hastie and Tibshirani 2010): the faster choice when the rows fit."streaming"reads the stored rows once per Newton step and needs memory only for the information matrices."auto"uses the in-memory solver when the rows of the full-sample and cross-validation models fit withinmax_memory_gb(else the machine’s available 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. Past that, coordinate descent waits on memory for the rows it reads, and the streamed solver is faster. Where the solvers cross over depends on the machine: about 150,000 rows of a four-level outcome on an 8-core laptop with a 12 MB cache, and about 50,000 on a 16-vCPU server. Near the crossover, setting the solver can be faster than"auto". The ordered models use the streamed solver.**options – Options for the stored rows:
workdir,storage,dtype,block_mb,chunk_mb,max_memory_gb,verbose; andn_threads.
- Returns:
PenalizedCategoricalResult
l1_ratio (float, default 0.5) – The L1 share of the penalty (glmnet’s alpha): 1 is the lasso.