Linear models¶
Bias-reduced and Bayesian logistic regression, lasso variants that select variables more reliably or shrink large effects less, and flexible additive regression.
FirthLogisticRegression ¶
FirthLogisticRegression(correction='none', fit_intercept=True, max_iter=100, tol=1e-08, max_step=5.0, max_halfstep=25)
Binary logistic regression with Firth's bias-reducing penalty.
Firth's penalty (a Jeffreys prior) removes the first-order small-sample bias of the maximum likelihood estimate and gives finite coefficients even under complete separation, where ordinary logistic regression diverges.
Firth's estimate pulls predicted probabilities toward 0.5, which hurts calibration when events are rare. Puhr et al. (2017) proposed two fixes:
correction="flic"keeps Firth's slopes and re-estimates the intercept by maximum likelihood, so the average predicted probability equals the observed event rate.correction="flac"refits by maximum likelihood on the data augmented with pseudo-observations weighted by the hat values, plus an indicator for the pseudo-data.
In aspective's benchmark (bench/RESULTS.md, 23 PMLB binary
datasets) Firth is non-inferior to maximum likelihood in Brier score. With
only 100 training rows neither Firth nor FLIC/FLAC was shown to be better
or worse than maximum likelihood, so the corrections are offered for
calibration under rare events, as studied by Puhr et al., not as a
general improvement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
correction
|
(none, flic, flac)
|
Intercept correction for better calibrated probabilities. |
"none"
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
100
|
tol
|
float
|
Convergence threshold on the largest coefficient change. |
1e-8
|
max_step
|
float
|
Largest allowed change of any coefficient in one Newton step. |
5.0
|
max_halfstep
|
int
|
Maximum step halvings per iteration. |
25
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (2,)
|
|
coef_ |
ndarray of shape (1, n_features)
|
|
intercept_ |
ndarray of shape (1,)
|
|
loglik_ |
float
|
Penalized log-likelihood of the Firth fit. |
n_iter_ |
int
|
|
References
D. Firth. Bias Reduction of Maximum Likelihood Estimates. Biometrika, 80(1):27-38, 1993.
R. Puhr, G. Heinze, M. Nold, L. Lusa and A. Geroldinger. Firth's logistic regression with rare events: accurate effect estimates and predictions? Statistics in Medicine, 36(14):2302-2317, 2017.
CauchyLogisticRegression ¶
CauchyLogisticRegression(prior_scale=2.5, intercept_prior_scale=10.0, prior_df=1.0, scale_inputs=True, max_iter=1000, tol=1e-08)
Binary logistic regression with the Gelman et al. default prior.
Gelman, Jakulin, Pittau and Su proposed a default prior for logistic regression that every analysis can use unchanged: put inputs on a common scale, then give each coefficient an independent Cauchy prior with centre 0 and scale 2.5 (the intercept gets scale 10). The prior says a change of one unit on the common scale rarely moves the probability from 0.01 to 0.5, so it is weak, but its heavy tails leave large effects alone. Estimates are the posterior mode, which is always finite, also under separation, where maximum likelihood diverges and ridge shrinks every coefficient by the same rule.
With scale_inputs=True (the paper's recommendation) inputs with more
than two distinct values are centred and scaled to standard deviation
0.5, and binary inputs are centred, so that coefficients of both kinds
are comparable. coef_ is reported on the original scale. Setting
prior_df to another value gives the Student-t family the paper also
discusses; prior_df=np.inf is a Gaussian prior.
Prefer it over maximum-likelihood or ridge logistic regression for
small or sparse data, separation, and when you want a default with no
penalty to tune. :class:FirthLogisticRegression is the alternative
that targets bias of the coefficients rather than a prior belief.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior_scale
|
float
|
Scale of the prior on each coefficient, on the common input scale. |
2.5
|
intercept_prior_scale
|
float
|
Scale of the prior on the intercept (on centred inputs). |
10.0
|
prior_df
|
float
|
Degrees of freedom of the Student-t prior; 1 is Cauchy. |
1.0
|
scale_inputs
|
bool
|
Put inputs on the paper's common scale before fitting. |
True
|
max_iter
|
int
|
|
1000
|
tol
|
float
|
Gradient tolerance of the L-BFGS solver. |
1e-8
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (2,)
|
|
coef_ |
ndarray of shape (1, n_features)
|
|
intercept_ |
ndarray of shape (1,)
|
|
n_iter_ |
int
|
|
References
A. Gelman, A. Jakulin, M. G. Pittau and Y.-S. Su. A Weakly Informative Default Prior Distribution for Logistic and Other Regression Models. The Annals of Applied Statistics, 2(4):1360-1383, 2008.
RelaxedLasso ¶
Relaxed Lasso with fixed penalties.
The lasso uses one penalty both to select variables and to shrink them,
so a penalty strong enough to drop noise variables also over-shrinks the
true ones. The relaxed lasso separates the two: Lasso(alpha) selects
the support, then the selected variables are refitted with the weaker
penalty phi * alpha. phi=1 is the lasso and phi=0 is least
squares on the lasso support.
Use :class:RelaxedLassoCV to choose alpha and phi by
cross-validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Selection penalty, as in :class: |
1.0
|
phi
|
float in [0, 1]
|
Relaxation factor for the refit. |
0.5
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
1000
|
tol
|
float
|
|
1e-4
|
Attributes:
| Name | Type | Description |
|---|---|---|
coef_ |
ndarray of shape (n_features,)
|
|
intercept_ |
float
|
|
support_ |
ndarray of int
|
Indices of the selected features. |
n_iter_ |
int
|
Largest number of coordinate descent iterations of the two fits. |
References
N. Meinshausen. Relaxed Lasso. Computational Statistics & Data Analysis, 52(1):374-393, 2007.
T. Hastie, R. Tibshirani and R. Tibshirani. Best Subset, Forward Stepwise or Lasso? Analysis and Recommendations Based on Extensive Comparisons. Statistical Science, 35(4):579-592, 2020.
RelaxedLassoCV ¶
RelaxedLassoCV(n_alphas=50, eps=0.001, phis=(0.0, 0.25, 0.5, 0.75, 1.0), cv=5, fit_intercept=True, max_iter=1000, tol=0.0001)
Relaxed Lasso with alpha and phi chosen by cross-validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_alphas
|
int
|
Number of selection penalties on a log grid below |
50
|
eps
|
float
|
Ratio of the smallest to the largest |
1e-3
|
phis
|
array - like
|
Relaxation factors to try. |
(0, 0.25, 0.5, 0.75, 1)
|
cv
|
int, cross-validation generator or iterable
|
|
5
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
1000
|
tol
|
float
|
|
1e-4
|
Attributes:
| Name | Type | Description |
|---|---|---|
alpha_ |
float
|
|
phi_ |
float
|
|
alphas_ |
ndarray of shape (n_alphas,)
|
|
mse_path_ |
ndarray of shape (n_alphas, n_phis)
|
Mean cross-validated squared error for every pair. |
coef_, intercept_, support_, n_iter_ |
Refit on the full data with the chosen pair. |
References
N. Meinshausen. Relaxed Lasso. Computational Statistics & Data Analysis, 52(1):374-393, 2007.
AdaptiveLasso ¶
Adaptive Lasso with a fixed penalty.
The lasso penalizes every coefficient equally, so to drop the noise
variables it must also shrink the large, true effects, and it selects the
right variables only under a restrictive irrepresentable condition. The
adaptive lasso uses a weighted penalty alpha * sum_j w_j |beta_j| with
w_j = 1 / |beta_init_j| ** gamma from an initial root-n consistent fit.
Variables that look large are barely penalized, small ones heavily. Zou
proved that it has the oracle property: it selects the true variables
with probability tending to one and estimates them as efficiently as if
the true model were known. It is solved as a lasso on rescaled inputs.
Use :class:AdaptiveLassoCV to choose alpha and gamma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Penalty, as in :class: |
1.0
|
gamma
|
float
|
Exponent of the adaptive weights. |
1.0
|
initial
|
(ridge, ols)
|
Initial estimator for the weights. Zou (2006) used least squares and recommended ridge regression when the inputs are collinear; the ridge penalty is chosen by efficient leave-one-out CV. |
"ridge"
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
1000
|
tol
|
float
|
|
1e-4
|
Attributes:
| Name | Type | Description |
|---|---|---|
coef_ |
ndarray of shape (n_features,)
|
|
intercept_ |
float
|
|
support_ |
ndarray of int
|
|
weights_ |
ndarray of shape (n_features,)
|
The adaptive penalty weights (infinite for a dropped feature). |
n_iter_ |
int
|
Coordinate descent iterations of the lasso fit. |
References
H. Zou. The Adaptive Lasso and Its Oracle Properties. Journal of the American Statistical Association, 101(476):1418-1429, 2006.
AdaptiveLassoCV ¶
AdaptiveLassoCV(gammas=(0.5, 1.0, 2.0), n_alphas=100, eps=0.001, cv=5, initial='ridge', fit_intercept=True, max_iter=1000, tol=0.0001)
Adaptive Lasso with alpha and gamma chosen by cross-validation.
For every gamma the weights come from the initial fit on the full
training data, as in Zou (2006); then the lasso path on the rescaled
inputs is cross-validated with the same folds for every gamma, and
the pair with the lowest error is refitted on all the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gammas
|
array - like
|
Exponents to try; Zou (2006) used 0.5, 1 and 2. |
(0.5, 1, 2)
|
n_alphas
|
int
|
|
100
|
eps
|
float
|
|
1e-3
|
cv
|
int, cross-validation generator or iterable
|
|
5
|
initial
|
(ridge, ols)
|
|
"ridge"
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
1000
|
tol
|
float
|
|
1e-4
|
Attributes:
| Name | Type | Description |
|---|---|---|
alpha_ |
float
|
|
gamma_ |
float
|
|
cv_mse_ |
ndarray of shape (n_gammas,)
|
Best mean cross-validated squared error for each gamma. |
coef_, intercept_, support_, weights_, n_iter_ |
Refit on all the data with the chosen pair. |
References
H. Zou. The Adaptive Lasso and Its Oracle Properties. Journal of the American Statistical Association, 101(476):1418-1429, 2006.
NonconvexPenaltyRegression ¶
NonconvexPenaltyRegressionCV ¶
NonconvexPenaltyRegressionCV(penalty='mcp', gamma=None, n_alphas=50, eps=None, cv=5, max_iter=1000, tol=1e-06)
Bolasso ¶
Bolasso(alpha=None, n_bootstrap=128, threshold=1.0, fit_intercept=True, max_iter=1000, random_state=None)
Bootstrap-intersected Lasso with a least-squares refit.
The lasso only selects the true variables consistently under the strict irrepresentable condition. Bach (2008) showed that running the lasso on bootstrap replicates and keeping the variables selected in all of them gives consistent selection under much weaker conditions. The selected set is then refitted by ordinary least squares.
threshold=1 is the strict intersection of the paper; a value like 0.9
is the "soft" Bolasso-S variant, which is more robust in practice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float or None
|
Lasso penalty used on every replicate. When None it is chosen once on
the full data by :class: |
None
|
n_bootstrap
|
int
|
Number of bootstrap replicates (Bach used 128). |
128
|
threshold
|
float in (0, 1]
|
Minimum selection frequency for a variable to be kept. |
1.0
|
fit_intercept
|
bool
|
|
True
|
max_iter
|
int
|
|
1000
|
random_state
|
int, RandomState instance or None
|
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
alpha_ |
float
|
Penalty that was used. |
selection_frequencies_ |
ndarray of shape (n_features,)
|
Fraction of replicates in which each variable was selected. |
support_ |
ndarray of int
|
Indices of the kept variables. |
coef_ |
ndarray of shape (n_features,)
|
|
intercept_ |
float
|
|
n_iter_ |
int
|
Largest number of coordinate descent iterations over the replicates. |
References
F. R. Bach. Bolasso: Model Consistent Lasso Estimation through the Bootstrap. In Proceedings of the 25th International Conference on Machine Learning (ICML), 2008.
L2BoostRegressor ¶
Componentwise linear L2 boosting with the number of steps chosen by CV.
Gradient boosting with squared loss where each base learner is a simple
linear regression on one feature: at every step the feature that best
fits the current residuals is chosen and its coefficient moves a small
step learning_rate toward the least-squares fit. Stopping early
shrinks and selects coefficients much like the lasso, while the
coefficient paths are smoother and correlated features enter more
gradually. Buehlmann (2006) proved consistency for high-dimensional
sparse linear models with many more features than samples. The number
of steps is the only tuning parameter and is chosen here by K-fold
cross-validation over the whole path, which is computed incrementally
in O(n_features) per step after one Gram matrix.
This is the default linear model of the R package mboost (Buehlmann & Hothorn, 2007). Prefer it over the lasso when features are strongly correlated, when you want a sparse model whose coefficients are not over-shrunk at a single penalty, or as a fast, interpretable baseline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iter
|
int
|
Largest number of boosting steps considered. |
2000
|
learning_rate
|
float in (0, 1]
|
Step length |
0.1
|
cv
|
int, cross-validation generator or iterable
|
How the number of steps is chosen; None uses |
5
|
Attributes:
| Name | Type | Description |
|---|---|---|
n_iter_ |
int
|
Number of boosting steps used in the final model. |
cv_mse_ |
ndarray of shape (max_iter + 1,)
|
Mean cross-validated squared error after each number of steps. |
coef_ |
ndarray of shape (n_features,)
|
|
intercept_ |
float
|
|
support_ |
ndarray of int
|
Features that were selected at least once. |
References
P. Buehlmann and B. Yu. Boosting with the L2 Loss: Regression and Classification. Journal of the American Statistical Association, 98(462):324-339, 2003.
P. Buehlmann. Boosting for High-Dimensional Linear Models. The Annals of Statistics, 34(2):559-583, 2006.
P. Buehlmann and T. Hothorn. Boosting Algorithms: Regularization, Prediction and Model Fitting. Statistical Science, 22(4):477-505, 2007.
MARSRegressor ¶
Multivariate adaptive regression splines (MARS).
A linear model in a basis of hinge functions max(0, x_j - t) and
max(0, t - x_j) (and, with max_degree > 1, their products) that
are chosen from the data. A forward pass adds, at each step, the
reflected pair of hinges (on a variable, at a knot, multiplying an
existing term) that most reduces the residual sum of squares; a
backward pass then removes terms one by one and keeps the subset with
the best generalized cross-validation score
GCV = RSS / n / (1 - C(M) / n)^2, C(M) = M + penalty (M - 1) / 2.
MARS keeps what linear regression and regression trees each do well:
it is continuous and piecewise linear, picks variables and
interactions automatically, and extrapolates linearly instead of by a
constant. Friedman found it more accurate than recursive partitioning
on smooth functions in moderate dimension; with max_degree=1 it
fits an additive model, the default of the R package earth.
Candidate knots are n_knots interior quantiles of each feature
(Friedman's minspan/endspan rules play the same role: they keep
knots away from the extremes and from each other). The forward search
updates the projections of every candidate onto the current basis
incrementally, so a step costs one pass over the candidate hinges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_terms
|
int or None
|
Largest number of terms (with the intercept) in the forward pass;
None uses earth's |
None
|
max_degree
|
int
|
Largest number of hinges multiplied in one term. |
1
|
penalty
|
float or None
|
GCV cost per knot; None uses 2 for |
None
|
n_knots
|
int
|
Candidate knots per feature. |
20
|
threshold
|
float
|
The forward pass stops when the best step raises R^2 by less. |
0.001
|
Attributes:
| Name | Type | Description |
|---|---|---|
terms_ |
list of tuple
|
Selected terms, each a tuple of hinges |
coef_ |
ndarray of shape (n_terms,)
|
|
gcv_ |
float
|
|
References
J. H. Friedman. Multivariate Adaptive Regression Splines. The Annals of Statistics, 19(1):1-67, 1991.
S. Milborrow. earth: Multivariate Adaptive Regression Splines. R package, derived from mda:mars by T. Hastie and R. Tibshirani.
PSplineGAMRegressor ¶
Additive model with penalized B-splines (P-splines), smoothness chosen by GCV.
Linear regression fits one slope per feature. A generalized additive
model (Hastie & Tibshirani, 1986) replaces each slope by a smooth
function, y = b0 + f_1(x_1) + ... + f_p(x_p), which keeps the model
additive and readable (one curve per feature) while capturing
nonlinear effects. Following Eilers and Marx, each f_j is a cubic
B-spline on n_knots equally spaced knots with a second-order
difference penalty on neighbouring coefficients. The penalty shrinks
every curve toward a straight line, so the strongest smoothing gives
back ordinary linear regression within the training range. The penalty
weight is chosen by generalized cross-validation from a log-spaced
grid, all at the cost of one eigendecomposition.
Features with fewer than min_unique distinct values enter
linearly. Outside the training range each curve stays at its value at
the boundary: a linearly extrapolated spline can reach values far
outside anything seen in training on a single unusual point. One
smoothing parameter is shared by all features (on standardized
curves), as in the mgcv "one smoothing parameter" setting; use it
for moderate numbers of features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_knots
|
int
|
Equally spaced knots per feature ( |
10
|
alphas
|
array - like or None
|
Candidate penalty weights; None uses 25 values from 1e-4 to 1e6, relative to the average curvature of the basis. |
None
|
min_unique
|
int
|
Features with fewer distinct training values enter linearly. |
6
|
Attributes:
| Name | Type | Description |
|---|---|---|
alpha_ |
float
|
Selected penalty weight. |
edf_ |
float
|
Effective degrees of freedom of the fit. |
gcv_ |
ndarray of shape (n_alphas,)
|
|
coef_ |
ndarray of shape (n_basis,)
|
|
intercept_ |
float
|
|
spline_features_ |
ndarray of int
|
Features modelled by splines; the others are linear. |
References
T. Hastie and R. Tibshirani. Generalized Additive Models. Statistical Science, 1(3):297-310, 1986.
P. H. C. Eilers and B. D. Marx. Flexible Smoothing with B-splines and Penalties. Statistical Science, 11(2):89-121, 1996.
B. D. Marx and P. H. C. Eilers. Direct Generalized Additive Modeling with Penalized Likelihood. Computational Statistics & Data Analysis, 28(2):193-209, 1998.