Categories &

Functions List

Function Reference: lasso

statistics: B = lasso (X, y)

statistics: [B, FitInfo] = lasso (X, y)

statistics: […] = lasso (…, Name, Value)

Lasso and elastic-net regularized least-squares regression.

B = lasso (X, y) fits a series of regularized linear models of the response y on the predictor matrix X by lasso, over a sequence of values of the regularization parameter Lambda. B is a P×L matrix whose column k holds the coefficient estimates for the k-th Lambda, in ascending order of Lambda. y is a vector with one element per row of X; a row vector is taken as a column.

[B, FitInfo] = lasso (…) additionally returns a structure FitInfo with fields Intercept, Lambda, Alpha, DF (number of non-zero coefficients), and MSE (mean squared error), one entry per value of Lambda.

The following Name-Value pairs are supported:

NameValue
'Alpha'The elastic-net mixing parameter in (0, 1]. 1 (default) is the lasso penalty; smaller values add a ridge penalty.
'Lambda'A vector of non-negative regularization parameters. By default a geometric sequence of 'NumLambda' values is used, from the smallest value that drives all coefficients to zero down to 'LambdaRatio' times that value.
'NumLambda'The number of Lambda values in the default sequence (default 100).
'LambdaRatio'The ratio of the smallest to the largest Lambda in the default sequence (default 1e-4, or 1e-2 when the number of observations is below the number of predictors).
'Standardize'Whether to standardize X to zero mean and unit variance before fitting (default true). Coefficients are always returned on the original scale.
'Weights'A real, finite, non-negative vector of class single or double, with one element per row of X. The weights of rows removed because of NaN values are dropped with those rows, and the remaining weights are normalized to sum to one.
'RelTol'Convergence tolerance for the coordinate descent (default 1e-4).
'MaxIter'Maximum number of coordinate-descent iterations, a non-negative integer (default 1e5). A value of 0 returns the starting point, every coefficient 0, where MATLAB’s lasso refuses 0 and its lassoglm accepts it.
'DFmax'The maximum number of non-zero coefficients; the default sequence stops once this is exceeded.
'Intercept'Whether to fit a constant term (default true).
'PredictorNames'A cell array of predictor names, kept in FitInfo.
'CV'The number of folds K for K-fold cross-validation of the mean squared error, or a cvpartition object.
'MCReps'The number of Monte-Carlo repetitions of the cross-validation (default 1).

Observations with NaN values in X or y are removed before fitting. At least two observations must remain and, if 'Weights' is given, at least two of them must have a non-zero weight.

When 'CV' is used, FitInfo.MSE is the cross-validated error, plus SE, LambdaMinMSE, IndexMinMSE, Lambda1SE, and Index1SE, which report the Lambda with the lowest error and the largest Lambda within one standard error of it. The fold assignment is random, so these selections are not reproducible without fixing the random seed.

See also: ridge, regress, lassoglm

Source Code: lasso

Lasso path drives coefficients to zero as the penalty grows

 rng (42);
 X = rand (50, 6);
 b = [3; 0; -2; 0; 1.5; 0];
 y = X * b + 0.1 * randn (50, 1);
 [B, FitInfo] = lasso (X, y);
 plot (log (FitInfo.Lambda), B');
 xlabel ("log (Lambda)");  ylabel ("coefficient");
plotted figure