Results¶
- class penalized_stream.PenalizedResult[source]¶
A penalized least-squares fit along a path of penalties, and the penalty chosen on it.
Coefficients are on the covariates’ own scale (the standardization is undone). There are no standard errors: after selection by a penalty they would not be valid. For inference on a coefficient of interest, use
double_selection_stream.- Attributes:
formula (str)
depvar (str)
coefnames (list of str) – The covariates, in design order, penalized and unpenalized.
unpenalized (list of str)
fe_names (list of str) – The fixed effects partialled out (not penalized).
l1_ratio (float) – 1 for the lasso, 0 for ridge, in between for the elastic net.
lambdas (numpy.ndarray) – The penalties of the path, from the largest.
coef_path (numpy.ndarray, shape (n_lambdas, n_coef))
intercept_path (numpy.ndarray) – Zero with fixed effects (they absorb it).
penalty (float) – The chosen penalty.
penalty_rule (str) – How it was chosen:
"lambda_min"or"lambda_1se"(cross-validation),"aic","bic","ebic","aicc","rigorous"or"given".index (int) – The chosen penalty’s position on the path.
cv_mean, cv_se (numpy.ndarray or None) – The cross-validated mean squared error at each penalty, and its standard error (None without cross-validation).
n_folds (int or None)
fold_by (str or None) –
"rows", or the column whose values make the folds.criteria (dict) –
{"aic": array, "bic": ..., "ebic": ..., "aicc": ...}along the path.df (numpy.ndarray) – Degrees of freedom of the penalized fit along the path.
n_obs (int)
k_fe (int) – The fixed effects’ degrees of freedom.
n_levels (dict)
dropped (list of str) – Covariates with no variation left after the fixed effects.
groups (dict or None) – With
groups=: each covariate’s group ({name: group}).diagnostics (dict)
- cv_measure = 'mean squared error'¶
what the cross-validated error measures
- coef(which=None)[source]¶
{name: coefficient}at the chosen penalty (or atwhich:"lambda_min","lambda_1se", a criterion name, or a path index), with"Intercept"first when there are no fixed effects.
- selected(which=None)[source]¶
The covariates with a nonzero coefficient (unpenalized ones included).
- cv()[source]¶
The penalty path’s summary:
lambda,n_nonzero,df, the cross-validated error (cv_mean,cv_se) and the criteria.
- predict(newdata=None, which=None)[source]¶
newdata(by default the data fitted), lazily, withxbadded: the covariates’ part of the prediction, plus the intercept when there are no fixed effects. With fixed effects,xbleaves out their effects, which the fit partials out without estimating.
- sample(check=True)[source]¶
The source with
row_id,in_sampleanddropped_becauseadded, ashdfe_stream’s results give them.
- plot_path(ax=None, log=True)[source]¶
The coefficients of the standardized covariates along the path, with the chosen penalty marked.
- plot_cv(ax=None, log=True)[source]¶
The cross-validated error along the path, with one standard error either side, and lambda_min and lambda_1se marked.
- __init__(formula, depvar, coefnames, unpenalized, fe_names, l1_ratio, lambdas, coef_path, intercept_path, penalty, penalty_rule, index, cv_mean, cv_se, n_folds, fold_by, criteria, df, n_obs, k_fe, n_levels, dropped, weights=None, weights_type='aweights', cv_fixed_effects=None, loadings=None, groups=None, diagnostics=<factory>, _exprs=<factory>, _cp=None)¶
- class penalized_stream.PenalizedLogitResult[source]¶
A penalized logit, probit or Poisson model along a path of penalties, and the penalty chosen on it. As
PenalizedResult, with the cross-validated error the mean deviance (per unit of weight) of the held-out rows – for the logit and probit -2 log-likelihood, for Poisson twice the excess of the saturated model’s log-likelihood, as glmnet’s – and:- Attributes:
family (str) –
"logit","probit"or"poisson".effects (numpy.ndarray) – The group effects at the chosen penalty (the intercept, alone, with no fixed effect).
fe_levels (polars.DataFrame or None) – The fixed effect’s groups (its columns), in the order of
effects.deviance (numpy.ndarray) – The full-sample fit’s deviance along the path.
- cv_measure = 'deviance'¶
what the cross-validated error measures
- predict(newdata=None, which=None, type='response')[source]¶
newdata(by default the data fitted), lazily, withxb(the linear predictor, the group’s effect included) and withtype="response"the mean:probabilityfor the logit and probit,meanfor Poisson. With a fixed effect, a row of a group not in the fit gets nulls; only the chosen penalty’s effects are kept, sowhichmust be the chosen one.
- sample(check=True)[source]¶
The source with
row_id,in_sampleanddropped_becauseadded, ashdfe_stream’s results give them.
- __init__(formula, depvar, coefnames, unpenalized, fe_names, l1_ratio, lambdas, coef_path, intercept_path, penalty, penalty_rule, index, cv_mean, cv_se, n_folds, fold_by, criteria, df, n_obs, k_fe, n_levels, dropped, weights=None, weights_type='aweights', cv_fixed_effects=None, loadings=None, groups=None, diagnostics=<factory>, _exprs=<factory>, _cp=None, effects=None, fe_levels=None, deviance=None, family='logit', _source=None)¶
- class penalized_stream.PenalizedCategoricalResult[source]¶
A penalized multinomial logit, or ordered logit or probit, along a path of penalties, and the penalty chosen on it. As
PenalizedResult, with the cross-validated error the mean deviance (-2 log-likelihood per unit of weight) of the held-out rows, and:The coefficients (
coefnames,coef_path) are, for the multinomial logit, one per outcome level and covariate, named"<level>:<covariate>"(glmnet’s symmetric form: only their differences between levels enter the probabilities, and the penalty identifies them; an unpenalized covariate’s are centered to sum to zero over the levels, as are the intercepts). For the ordered model, one per covariate, with P(y <= j) = F(cut_j - x’b) (a positive coefficient moves the outcome up); the cutpoints are incutpoints_path.- Attributes:
model (str) –
"multinomial"or"ordered".link (str) –
"logit"or"probit"(the ordered model’s).grouped (bool) – Whether the multinomial logit’s penalty is grouped: each covariate’s coefficients for every level enter or leave together (then they are unique and sum to zero over the levels).
levels (list) – The outcome’s levels, in order.
intercept_path (numpy.ndarray) – The multinomial logit’s intercepts, shape (n_lambdas, J), without a fixed effect; zeros otherwise (and for the ordered model).
cutnames (list of str) – The ordered model’s cutpoints (
"cut1", …; with a fixed effect the first is 0, absorbed by the effects, and not listed).cutpoints_path (numpy.ndarray or None) – Their values along the path, shape (n_lambdas, len(cutnames)).
effects (numpy.ndarray or None) – The fixed effect’s group effects at the chosen penalty: shape (G, J) for the multinomial logit (each level’s, -inf for a level the group never has), (G,) for the ordered model (its location).
fe_levels (polars.DataFrame or None) – The fixed effect’s groups (its columns), in the order of
effects.deviance (numpy.ndarray) – The full-sample fit’s deviance (-2 log-likelihood) along the path.
cv_folds (numpy.ndarray or None) – Each fold’s held-out mean deviance along the path, shape (n_folds, n_lambdas).
- cv_measure = 'deviance'¶
what the cross-validated error measures
- coef(which=None)[source]¶
{name: coefficient}at the chosen penalty (or atwhich), with the multinomial logit’s intercepts first ("<level>:Intercept", without a fixed effect) and the ordered model’s cutpoints last.
- tidy(which=None)[source]¶
One row per coefficient:
term,estimate,penalized(the intercepts and cutpoints are not).
- fixef()[source]¶
The fixed effect’s estimated group effects at the chosen penalty:
effect(the ordered model’s location) or oneeffect_<level>column per outcome level (the multinomial logit).
- predict(newdata=None, which=None, type='response')[source]¶
newdata(by default the data fitted), lazily, with the linear predictors and, withtype="response", each outcome level’s probability,probability_<level>.The multinomial logit’s linear predictors are
xb_<level>(the intercept or the group’s effect included); the ordered model’s isxb(x’b, plus the group’s effect). With a fixed effect, a row of a group not in the fit gets nulls; only the chosen penalty’s effects are kept, sowhichmust be the chosen one.
- sample(check=True)[source]¶
The source with
row_id,in_sampleanddropped_becauseadded, ashdfe_stream’s results give them.
- plot_path(ax=None, log=True)[source]¶
The coefficients of the standardized covariates along the path (one line per level and covariate for the multinomial logit), with the chosen penalty marked.
- __init__(formula, depvar, coefnames, unpenalized, fe_names, l1_ratio, lambdas, coef_path, intercept_path, penalty, penalty_rule, index, cv_mean, cv_se, n_folds, fold_by, criteria, df, n_obs, k_fe, n_levels, dropped, weights=None, weights_type='aweights', cv_fixed_effects=None, loadings=None, groups=None, diagnostics=<factory>, _exprs=<factory>, _cp=None, model='multinomial', link='logit', grouped=False, levels=None, covariates=None, cutnames=None, cutpoints_path=None, effects=None, fe_levels=None, deviance=None, cv_folds=None, _source=None)¶
- class penalized_stream.DoubleSelection[source]¶
What the selection steps chose (
result.double_selection).- __init__(treatments, controls, unpenalized, selected, union, penalty, penalties, loadings_cluster)¶
- class penalized_stream.DoubleSelectionIV[source]¶
What the selection steps chose (
result.double_selection), with the instruments:selectedhas the controls selected for the outcome, the treatment and each instrument (or, withselect_instruments, for the outcome, the treatment, the first stage and its fit).- __init__(treatments, controls, unpenalized, selected, union, penalty, penalties, loadings_cluster, endogenous='', exogenous=<factory>, instruments=<factory>, instruments_used=<factory>, select_instruments=False, method='union', sup_score=None)¶
- class penalized_stream.SupScoreTest[source]¶
The sup-score test of the treatment’s effect (
result.double_selection.sup_score).statistic,critical_value,p_valueandrejectedare for the null valuenull;confidence_setis the set of values the test does not reject atlevel, a list of (low, high) intervals (-inforinfwhere unbounded; empty when it rejects every value). With the bootstrap the set is read offgrid, andat_grid_edgesays whether it reaches the grid’s first or last value.gridhas the test at each value.- __init__(null, statistic, critical_value, p_value, rejected, confidence_set, level, method, variance, n_instruments, c, draws, grid, at_grid_edge=(False, False))¶
- class penalized_stream.DoubleMLResult[source]¶
The treatments’ effects, their standard errors, intervals and tests (
summary,tidy,confint,wald_test;etablethrough hdfe_stream.etable).Beyond
RegressionResult’s attributes:- Attributes:
controls (list of str)
n_folds, n_repeats (int)
penalty (str or float) – How the nuisance lassos’ penalties were chosen.
theta_repeats (numpy.ndarray) – (n_repeats, treatments): each repetition’s estimates.
nuisance (list of dict) – For each repetition, fold and treatment: the outcome’s and the treatment’s lasso penalties and number of controls selected.
fe_names (list of str)
- __init__(*, fml, depvar, coefnames, beta, vcov, vcov_type, df_t, n_obs, all_vcovs=<factory>, n_clusters=<factory>, collin_vars=<factory>, diagnostics=<factory>, weights=None, weights_type='aweights', controls=<factory>, n_folds=5, n_repeats=1, penalty=None, theta_repeats=None, nuisance=<factory>, fe_names=<factory>)¶
- classmethod __new__(*args, **kwargs)¶
- class penalized_stream.DebiasedLassoResult[source]¶
Debiased lasso coefficients, their standard errors, intervals and tests (
summary,tidy,confint,wald_test,etablethrough hdfe_stream.etable, as for any of the suite’s regression results).Beyond
RegressionResult’s attributes:- Attributes:
lasso (dict) – The initial lasso’s coefficients (
{name: value}), all covariates.penalty (float) – The initial lasso’s penalty, and
penalty_rulehow it was chosen.node_penalties (dict) – Each debiased coefficient’s nodewise lasso penalty.
sigma (float) – The residual standard deviation of the initial lasso, sqrt(RSS / (N - nonzero coefficients - intercept or fixed-effect parameters)).
nodewise (numpy.ndarray) – (covariates x debiased coefficients): the rescaled directions z_j.
fe_names (list of str)
- __init__(*, fml, depvar, coefnames, beta, vcov, vcov_type, df_t, n_obs, all_vcovs=<factory>, n_clusters=<factory>, collin_vars=<factory>, diagnostics=<factory>, weights=None, weights_type='aweights', lasso=<factory>, penalty=nan, penalty_rule='', node_penalties=<factory>, sigma=nan, nodewise=None, fe_names=<factory>, covariates=<factory>)¶
- classmethod __new__(*args, **kwargs)¶
- class penalized_stream.StabilityResult[source]¶
Stability selection: each candidate’s selection probability, and the stable set.
- Attributes:
formula (str)
candidates (list of str) – The penalized covariates, or with
groupsthe groups.probabilities (dict) –
{candidate: share of the subsamples that select it}.selected (list of str) – The stable set: probability at or above
cutoff.cutoff (float)
q (int) – Candidates selected on each subsample (at most).
pfer (float) – The bound on the expected number of false selections.
assumption (str)
n_subsamples (int)
path (numpy.ndarray) – (steps, candidates): the share selected at each step of the path (stabs’
phat, transposed).groups (dict or None) – Each covariate’s group, with
groups=.subsamples (list of list of int) – Each subsample’s shards (pairs: entries 2i and 2i + 1).
- selected_covariates()[source]¶
The covariates in the stable set (with groups, every member of a stable group), unpenalized ones included.
- __init__(formula, candidates, probabilities, selected, cutoff, q, pfer, assumption, n_subsamples, path=None, groups=None, unpenalized=<factory>, n_obs=0, fe_names=<factory>, subsamples=<factory>)¶