naginterfaces.library.correg.lars_xtx¶
- naginterfaces.library.correg.lars_xtx(mtype, n, dtd, dty, yty, pred=2, intcpt=1, isx=None, mnstep=None, ropt=None, io_manager=None)[source]¶
lars_xtx
performs Least Angle Regression (LARS), forward stagewise linear regression or Least Absolute Shrinkage and Selection Operator (LASSO) using cross-product matrices.For full information please refer to the NAG Library document for g02mb
https://support.nag.com/numeric/nl/nagdoc_30.3/flhtml/g02/g02mbf.html
- Parameters
- mtypeint
Indicates the type of model to fit.
LARS is performed.
Forward linear stagewise regression is performed.
LASSO model is fit.
A positive LASSO model is fit.
- nint
, the number of observations.
- dtdfloat, array-like, shape
Note: the required extent for this argument in dimension 1 is determined as follows: if : ; if : ; otherwise: .
Note: the required extent for this argument in dimension 2 is determined as follows: if : ; if : ; otherwise: .
, the cross-product matrix, which along with , defines the design matrix cross-product .
If the cross-product matrix is packed into a single row or column of , or must contain the cross-product of the th and th variable, for , for .
That is the cross-product stacked by columns as returned by
ssqmat()
, for example.Otherwise must contain the cross-product of the th and th variable, for , for .
It should be noted that, even though is symmetric, the full matrix must be supplied.
The matrix specified in must be a valid cross-products matrix.
- dtyfloat, array-like, shape
, the cross-product between the dependent variable, , and the independent variables .
- ytyfloat
, the sums of squares of the dependent variable.
- predint, optional
Indicates the type of preprocessing to perform on the cross-products involving the independent variables, i.e., those supplied in and .
No preprocessing is performed.
Each independent variable is normalized, with the th variable scaled by . The scaling factor used by variable is returned in .
- intcptint, optional
Indicates the type of data preprocessing that was perform on the dependent variable, , prior to calling this function.
No preprocessing was performed.
The dependent variable, , was mean centred.
- isxNone or int, array-like, shape , optional
Indicates which independent variables from will be included in the design matrix, .
If is None, all variables are included in the design matrix.
Otherwise must be set as follows, for :
To indicate that the th variable, as supplied in , is included in the design matrix;
To indicate that the th variable, as supplied in , is not included in the design matrix;
and .
- mnstepNone or int, optional
Note: if this argument is None then a default value will be used, determined as follows: if : ; otherwise: .
The maximum number of steps to carry out in the model fitting process.
If , i.e., a LARS is being performed, the maximum number of steps the algorithm will take is if , otherwise .
If , i.e., a forward linear stagewise regression is being performed, the maximum number of steps the algorithm will take is likely to be several orders of magnitude more and is no longer bound by or .
If or , i.e., a LASSO or positive LASSO model is being fit, the maximum number of steps the algorithm will take lies somewhere between that of the LARS and forward linear stagewise regression, again it is no longer bound by or .
- roptNone or float, array-like, shape , optional
Options to control various aspects of the LARS algorithm.
The default value will be used for if , therefore, setting will use the default values for all options and need not be set and may be None.
The default value will also be used if an invalid value is supplied for a particular argument, for example, setting will use the default value for argument .
The minimum step size that will be taken.
Default is is used, where is the machine precision returned by
machine.precision
.General tolerance, used amongst other things, for comparing correlations.
Default is .
If set to , parameter estimates are rescaled before being returned. If set to , no rescaling is performed. This argument has no effect when .
Default is for the parameter estimates to be rescaled.
- io_managerFileObjManager, optional
Manager for I/O in this routine.
- Returns
- ipint
, number of parameter estimates.
If is None, , i.e., the number of variables in .
Otherwise is the number of nonzero values in .
- nstepint
, the actual number of steps carried out in the model fitting process.
- bfloat, ndarray, shape
the parameter estimates, with , the parameter estimate for the th variable, at the th step of the model fitting process, .
By default, when the parameter estimates are rescaled prior to being returned.
If the parameter estimates are required on the normalized scale, then this can be overridden via .
The values held in the remaining part of depend on the type of preprocessing performed.
for .
- fitsumfloat, ndarray, shape
Summaries of the model fitting process. When
, the sum of the absolute values of the parameter estimates for the th step of the modelling fitting process. If , the scaled parameter estimates are used in the summation.
, the residual sums of squares for the th step, where .
, approximate degrees of freedom for the th step.
, a -type statistic for the th step, where .
, correlation between the residual at step and the most correlated variable not yet in the active set , where the residual at step is .
, the step size used at step .
In addition
.
, the residual sums of squares for the null model, where .
, the degrees of freedom for the null model, where if and otherwise.
, a -type statistic for the null model, where .
, where and .
Although the statistics described above are returned when = 122 they may not be meaningful due to the estimate not being based on the saturated model.
- Raises
- NagValueError
- (errno )
On entry, .
Constraint: , , or .
- (errno )
On entry, .
Constraint: or .
- (errno )
On entry, .
Constraint: or .
- (errno )
On entry, .
Constraint: .
- (errno )
On entry, .
Constraint: .
- (errno )
The cross-product matrix supplied in is not symmetric.
- (errno )
On entry, .
Constraint: diagonal elements of must be positive.
- (errno )
On entry, and .
Constraint: diagonal elements of must be positive.
- (errno )
On entry, .
Constraint: or , for all .
- (errno )
On entry, all values of are zero.
Constraint: at least one value of must be nonzero.
- (errno )
On entry, and .
Constraint: or .
- (errno )
On entry, .
Constraint: .
- (errno )
A negative value for the residual sums of squares was obtained. Check the values of , and .
- (errno )
On entry, .
Constraint: .
- (errno )
On entry, .
Constraint: .
- Warns
- NagAlgorithmicWarning
- (errno )
Fitting process did not finished in steps. Try increasing the size of and supplying larger output arrays.
All output is returned as documented, up to step , however, and the statistics may not be meaningful.
- (errno )
is approximately zero and hence the -type criterion cannot be calculated. All other output is returned as documented.
- (errno )
, therefore, sigma has been set to a large value. Output is returned as documented.
- (errno )
Degenerate model, no variables added and . Output is returned as documented.
- Notes
lars_xtx
implements the LARS algorithm of Efron et al. (2004) as well as the modifications needed to perform forward stagewise linear regression and fit LASSO and positive LASSO models.Given a vector of observed values, and an design matrix , where the th column of , denoted , is a vector of length representing the th independent variable , standardized such that , and and a set of model parameters to be estimated from the observed values, the LARS algorithm can be summarised as:
Set and all coefficients to zero, that is .
Find the variable most correlated with , say . Add to the ‘most correlated’ set . If go to (8).
Take the largest possible step in the direction of (i.e., increase the magnitude of ) until some other variable, say , has the same correlation with the current residual, .
Increment and add to .
If go to (8).
Proceed in the ‘least angle direction’, that is, the direction which is equiangular between all variables in , altering the magnitude of the parameter estimates of those variables in , until the th variable, , has the same correlation with the current residual.
Go to (4).
Let .
As well as being a model selection process in its own right, with a small number of modifications the LARS algorithm can be used to fit the LASSO model of Tibshirani (1996), a positive LASSO model, where the independent variables enter the model in their defined direction, forward stagewise linear regression (Hastie et al. (2001)) and forward selection (Weisberg (1985)). Details of the required modifications in each of these cases are given in Efron et al. (2004).
The LASSO model of Tibshirani (1996) is given by
for all values of , where . The positive LASSO model is the same as the standard LASSO model, given above, with the added constraint that
Unlike the standard LARS algorithm, when fitting either of the LASSO models, variables can be dropped as well as added to the set . Therefore, the total number of steps is no longer bounded by .
Forward stagewise linear regression is an iterative procedure of the form:
Initialize and the vector of residuals .
For each calculate . The value is, therefore, proportional to the correlation between the th independent variable and the vector of previous residual values, .
Calculate , the value of with the largest absolute value of .
If then go to (7).
Update the residual values, with
where is a small constant and when and otherwise.
Increment and go to (2).
Set .
If the largest possible step were to be taken, that is then forward stagewise linear regression reverts to the standard forward selection method as implemented in
linregm_fit_onestep()
.The LARS procedure results in models, one for each step of the fitting process. In order to aid in choosing which is the most suitable Efron et al. (2004) introduced a -type statistic given by
where is the approximate degrees of freedom for the th step and
One way of choosing a model is, therefore, to take the one with the smallest value of .
- References
Efron, B, Hastie, T, Johnstone, I and Tibshirani, R, 2004, Least Angle Regression, The Annals of Statistics (Volume 32) (2), 407–499
Hastie, T, Tibshirani, R and Friedman, J, 2001, The Elements of Statistical Learning: Data Mining, Inference and Prediction, Springer (New York)
Tibshirani, R, 1996, Regression Shrinkage and Selection via the Lasso, Journal of the Royal Statistics Society, Series B (Methodological) (Volume 58) (1), 267–288
Weisberg, S, 1985, Applied Linear Regression, Wiley