g02ka calculates a ridge regression, optimizing the ridge parameter according to one of four prediction error criteria.


public static void g02ka(
	int n,
	int m,
	double[,] x,
	int[] isx,
	int ip,
	double tau,
	double[] y,
	ref double h,
	int opt,
	ref int niter,
	double tol,
	out double nep,
	int orig,
	double[] b,
	double[] vif,
	double[] res,
	out double rss,
	out int df,
	int optloo,
	double[] perr,
	out int ifail
Visual Basic
Public Shared Sub g02ka ( _
	n As Integer, _
	m As Integer, _
	x As Double(,), _
	isx As Integer(), _
	ip As Integer, _
	tau As Double, _
	y As Double(), _
	ByRef h As Double, _
	opt As Integer, _
	ByRef niter As Integer, _
	tol As Double, _
	<OutAttribute> ByRef nep As Double, _
	orig As Integer, _
	b As Double(), _
	vif As Double(), _
	res As Double(), _
	<OutAttribute> ByRef rss As Double, _
	<OutAttribute> ByRef df As Integer, _
	optloo As Integer, _
	perr As Double(), _
	<OutAttribute> ByRef ifail As Integer _
Visual C++
static void g02ka(
	int n, 
	int m, 
	array<double,2>^ x, 
	array<int>^ isx, 
	int ip, 
	double tau, 
	array<double>^ y, 
	double% h, 
	int opt, 
	int% niter, 
	double tol, 
	[OutAttribute] double% nep, 
	int orig, 
	array<double>^ b, 
	array<double>^ vif, 
	array<double>^ res, 
	[OutAttribute] double% rss, 
	[OutAttribute] int% df, 
	int optloo, 
	array<double>^ perr, 
	[OutAttribute] int% ifail
static member g02ka : 
        n : int * 
        m : int * 
        x : float[,] * 
        isx : int[] * 
        ip : int * 
        tau : float * 
        y : float[] * 
        h : float byref * 
        opt : int * 
        niter : int byref * 
        tol : float * 
        nep : float byref * 
        orig : int * 
        b : float[] * 
        vif : float[] * 
        res : float[] * 
        rss : float byref * 
        df : int byref * 
        optloo : int * 
        perr : float[] * 
        ifail : int byref -> unit 


Type: System..::..Int32
On entry: n, the number of observations.
Constraint: n>1.
Type: System..::..Int32
On entry: the number of independent variables available in the data matrix X.
Constraint: mn.
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: dim1n
On entry: the values of independent variables in the data matrix X.
Type: array<System..::..Int32>[]()[][]
An array of size [m]
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.
Type: System..::..Int32
On entry: m, the number of independent variables in the model.
  • 1ipm;
  • Exactly ip elements of isx must be equal to 1.
Type: System..::..Double
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.
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: the n values of the dependent variable y.
Type: System..::..Double%
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.
Type: System..::..Int32
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=1, 2, 3 or 4.
Type: System..::..Int32%
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.
Type: System..::..Double
On entry: iterations of the ridge regression parameter h will halt when consecutive values of h lie within tol.
Constraint: tol>0.0.
Type: System..::..Double%
On exit: the number of effective parameters, γ, in the model.
Type: System..::..Int32
On entry: if orig=1, the parameter estimates b are calculated for the original data; otherwise orig=2 and the parameter estimates b~ are calculated for the standardized data.
Constraint: orig=1 or 2.
Type: array<System..::..Double>[]()[][]
An array of size [ip+1]
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.
Type: array<System..::..Double>[]()[][]
An array of size [ip]
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.
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: res[i-1] is the value of the ith residual for the fitted ridge regression model, for i=1,2,,n.
Type: System..::..Double%
On exit: the sum of squares of residual values.
Type: System..::..Int32%
On exit: the degrees of freedom for the residual sum of squares rss.
Type: System..::..Int32
On entry: if optloo=2, the leave-one-out cross-validation estimate of prediction error is calculated; otherwise no such estimate is calculated and optloo=1.
Constraint: optloo=1 or 2.
Type: array<System..::..Double>[]()[][]
An array of size [5]
On exit: the first four elements contain, in this order, the measures of prediction error: GCV, UEV, FPE and BIC.
If optloo=2, perr[4] is the LOOCV estimate of prediction error; otherwise perr[4] is not referenced.
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).


A linear model has the form:
  • y is an n by 1 matrix of values of a dependent variable;
  • c is a scalar intercept term;
  • X is an n by m matrix of values of independent variables;
  • β is an m by 1 matrix of unknown values of parameters;
  • ε is an n by 1 matrix of unknown random errors such that variance of ε=σ2I.
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:
Ridge regression estimates the parameters β~ in a penalised least squares sense by finding the b~ that minimizes
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
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, g02ka 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
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
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
The m variance inflation factors are calculated as the diagonal elements of the matrix:
which, using the SVD of X~, is equivalent to the diagonal elements of the matrix:
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):
(b) Unbiased estimate of variance (UEV):
(c) Future prediction error (FPE):
(d) Bayesian information criterion (BIC):
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

Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
Maximum number of iterations used.
On entry,n1;
oropt1, 2, 3 or 4;
oroptloo1 or 2;
ororig1 or 2
On entry,m>n;
orip<1 or ip>m;
orAn element of isx0 or 1;
orip does not equal the sum of elements in isx.
SVD failed to converge.
Internal error. Check all array sizes and calls to g02ka. Please contact NAG.
An error occured, see message report.
Invalid Parameters value
Invalid dimension for array value
Negative dimension for array value
Invalid Parameters value


Not applicable.

Parallelism and Performance


Further Comments

g02ka allocates internally max5×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.

Example program (C#): g02kae.cs

Example program data: g02kae.d

Example program results: g02kae.r

See Also