NAG Library Function Document

nag_regsn_ridge_opt (g02kac)


    1  Purpose
    7  Accuracy


nag_regsn_ridge_opt (g02kac) calculates a ridge regression, optimizing the ridge parameter according to one of four prediction error criteria.


#include <nag.h>
#include <nagg02.h>
void  nag_regsn_ridge_opt (Nag_OrderType order, Integer n, Integer m, const double x[], Integer pdx, const Integer isx[], Integer ip, double tau, const double y[], double *h, Nag_PredictError opt, Integer *niter, double tol, double *nep, Nag_EstimatesOption orig, double b[], double vif[], double res[], double *rss, Integer *df, Nag_OptionLOO optloo, double perr[], NagError *fail)


A linear model has the form:
y = c+Xβ+ε ,  
Let X~ be the mean-centred X and y~ the mean-centred y. Furthermore, X~ is scaled such that the diagonal elements of the cross product matrix X~TX~ are one. The linear model now takes the form:
y~ = X~ β~ + ε .  
Ridge regression estimates the parameters β~ in a penalised least squares sense by finding the b~ that minimizes
X~ b~ - y~ 2 + h b~ 2 , h>0 ,  
where · denotes the 2-norm and h is a scalar regularization or ridge parameter. For a given value of h, the parameter estimates b~ are found by evaluating
b~ = X~T X~+hI -1 X~T y~ .  
Note that if h=0 the ridge regression solution is equivalent to the ordinary least squares solution.
Rather than calculate the inverse of (X~TX~+hI) directly, nag_regsn_ridge_opt (g02kac) uses the singular value decomposition (SVD) of X~. After decomposing X~ into UDVT where U and V are orthogonal matrices and D is a diagonal matrix, the parameter estimates become
b~ = V DTD+hI -1 D UT y~ .  
A consequence of introducing the ridge parameter is that the effective number of parameters, γ, in the model is given by the sum of diagonal elements of
DT D DT D+hI-1 ,  
see Moody (1992) for details.
Any multi-collinearity in the design matrix X may be highlighted by calculating the variance inflation factors for the fitted model. The jth variance inflation factor, vj, is a scaled version of the multiple correlation coefficient between independent variable j and the other independent variables, Rj, and is given by
vj = 1 1-Rj , j=1,2,,m .  
The m variance inflation factors are calculated as the diagonal elements of the matrix:
X~T X~+hI -1 X~T X~ X~T X~+hI -1 ,  
which, using the SVD of X~, is equivalent to the diagonal elements of the matrix:
V DTD+hI -1 DT D DTD+hI -1 VT .  
Although parameter estimates b~ are calculated by using X~, it is usual to report the parameter estimates b associated with X. These are calculated from b~, and the means and scalings of X. Optionally, either b~ or b may be calculated.
The method can adopt one of four criteria to minimize while calculating a suitable value for h:
(a) Generalized cross-validation (GCV):
ns n-γ 2 ;  
(b) Unbiased estimate of variance (UEV):
s n-γ ;  
(c) Future prediction error (FPE):
1n s+ 2γs n-γ ;  
(d) Bayesian information criterion (BIC):
1n s + lognγs n-γ ;  
where s is the sum of squares of residuals. However, the function returns all four of the above prediction errors regardless of the one selected to minimize the ridge parameter, h. Furthermore, the function will optionally return the leave-one-out cross-validation (LOOCV) prediction error.


Hastie T, Tibshirani R and Friedman J (2003) The Elements of Statistical Learning: Data Mining, Inference and Prediction Springer Series in Statistics
Moody J.E. (1992) The effective number of parameters: An analysis of generalisation and regularisation in nonlinear learning systems In: Neural Information Processing Systems (eds J E Moody, S J Hanson, and R P Lippmann) 4 847–854 Morgan Kaufmann San Mateo CA


1:     order Nag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by order=Nag_RowMajor. See Section in How to Use the NAG Library and its Documentation for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     n IntegerInput
On entry: n, the number of observations.
Constraint: n>1.
3:     m IntegerInput
On entry: the number of independent variables available in the data matrix X.
Constraint: mn.
4:     x[dim] const doubleInput
Note: the dimension, dim, of the array x must be at least
  • max1,pdx×m when order=Nag_ColMajor;
  • max1,n×pdx when order=Nag_RowMajor.
The i,jth element of the matrix X is stored in
  • x[j-1×pdx+i-1] when order=Nag_ColMajor;
  • x[i-1×pdx+j-1] when order=Nag_RowMajor.
On entry: the values of independent variables in the data matrix X.
5:     pdx IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
  • if order=Nag_ColMajor, pdxn;
  • if order=Nag_RowMajor, pdxm.
6:     isx[m] const IntegerInput
On entry: indicates which m independent variables are included in the model.
The jth variable in x will be included in the model.
Variable j is excluded.
Constraint: isx[j-1]=0 ​ or ​ 1, for j=1,2,,m.
7:     ip IntegerInput
On entry: m, the number of independent variables in the model.
  • 1ipm;
  • Exactly ip elements of isx must be equal to 1.
8:     tau doubleInput
On entry: singular values less than tau of the SVD of the data matrix X will be set equal to zero.
Suggested value: tau=0.0.
Constraint: tau0.0.
9:     y[n] const doubleInput
On entry: the n values of the dependent variable y.
10:   h double *Input/Output
On entry: an initial value for the ridge regression parameter h; used as a starting point for the optimization.
Constraint: h>0.0.
On exit: h is the optimized value of the ridge regression parameter h.
11:   opt Nag_PredictErrorInput
On entry: the measure of prediction error used to optimize the ridge regression parameter h. The value of opt must be set equal to one of:
Generalized cross-validation (GCV);
Unbiased estimate of variance (UEV)
Future prediction error (FPE)
Bayesian information criteron (BIC).
Constraint: opt=Nag_GCV, Nag_UEV, Nag_FPE or Nag_BIC.
12:   niter Integer *Input/Output
On entry: the maximum number of iterations allowed to optimize the ridge regression parameter h.
Constraint: niter1.
On exit: the number of iterations used to optimize the ridge regression parameter h within tol.
13:   tol doubleInput
On entry: iterations of the ridge regression parameter h will halt when consecutive values of h lie within tol.
Constraint: tol>0.0.
14:   nep double *Output
On exit: the number of effective parameters, γ, in the model.
15:   orig Nag_EstimatesOptionInput
On entry: if orig=Nag_EstimatesOrig, the parameter estimates b are calculated for the original data; otherwise orig=Nag_EstimatesStand and the parameter estimates b~ are calculated for the standardized data.
Constraint: orig=Nag_EstimatesOrig or Nag_EstimatesStand.
16:   b[ip+1] doubleOutput
On exit: contains the intercept and parameter estimates for the fitted ridge regression model in the order indicated by isx. The first element of b contains the estimate for the intercept; b[j] contains the parameter estimate for the jth independent variable in the model, for j=1,2,,ip.
17:   vif[ip] doubleOutput
On exit: the variance inflation factors in the order indicated by isx. For the jth independent variable in the model, vif[j-1] is the value of vj, for j=1,2,,ip.
18:   res[n] doubleOutput
On exit: res[i-1] is the value of the ith residual for the fitted ridge regression model, for i=1,2,,n.
19:   rss double *Output
On exit: the sum of squares of residual values.
20:   df Integer *Output
On exit: the degrees of freedom for the residual sum of squares rss.
21:   optloo Nag_OptionLOOInput
On entry: if optloo=Nag_WantLOO, the leave-one-out cross-validation estimate of prediction error is calculated; otherwise no such estimate is calculated and optloo=Nag_NoLOO.
Constraint: optloo=Nag_NoLOO or Nag_WantLOO.
22:   perr[5] doubleOutput
On exit: the first four elements contain, in this order, the measures of prediction error: GCV, UEV, FPE and BIC.
If optloo=Nag_WantLOO, perr[4] is the LOOCV estimate of prediction error; otherwise perr[4] is not referenced.
23:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

On entry, ip=value; m=value.
Constraint: 1ipm.
Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, n=value.
Constraint: n>1.
On entry, niter=value.
Constraint: niter1.
On entry, pdx=value.
Constraint: pdx>0.
On entry, m=value and n=value.
Constraint: mn.
On entry, pdx=value; n=value.
Constraint: pdxn.
On entry, pdx=value and m=value.
Constraint: pdxm.
On entry, ip=value.
Constraint: sumisx=ip.
On entry, isx[value]=value.
Constraint: isx[j-1]=0 or 1.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, h=value.
Constraint: h>0.0.
On entry, tau=value.
Constraint: tau0.0.
On entry, tol=value.
Constraint: tol>0.0.
SVD failed to converge.
Maximum number of iterations used.


Not applicable.

Parallelism and Performance

nag_regsn_ridge_opt (g02kac) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_regsn_ridge_opt (g02kac) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Further Comments

nag_regsn_ridge_opt (g02kac) allocates internally max 5 × n-1 , 2×ip×ip +n+3 × ip+n  elements of double precision storage.


This example reads in data from an experiment to model body fat, and a ridge regression is calculated that optimizes GCV prediction error.

Program Text

Program Text (g02kace.c)

Program Data

Program Data (g02kace.d)

Program Results

Program Results (g02kace.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017