# NAG FL Interfaceg02kaf (ridge_​opt)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g02kaf ( n, m, x, ldx, isx, ip, tau, y, h, opt, tol, nep, orig, b, vif, res, rss, df, perr,
 Integer, Intent (In) :: n, m, ldx, isx(m), ip, opt, orig, optloo Integer, Intent (Inout) :: niter, ifail Integer, Intent (Out) :: df Real (Kind=nag_wp), Intent (In) :: x(ldx,m), tau, y(n), tol Real (Kind=nag_wp), Intent (Inout) :: h Real (Kind=nag_wp), Intent (Out) :: nep, b(ip+1), vif(ip), res(n), rss, perr(5)
#include <nag.h>
 void g02kaf_ (const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], const Integer *ip, const double *tau, const double y[], double *h, const Integer *opt, Integer *niter, const double *tol, double *nep, const Integer *orig, double b[], double vif[], double res[], double *rss, Integer *df, const Integer *optloo, double perr[], Integer *ifail)
The routine may be called by the names g02kaf or nagf_correg_ridge_opt.

## 3Description

A linear model has the form:
 $y = c+Xβ+ε ,$
where
• $y$ is an $n×1$ matrix of values of a dependent variable;
• $c$ is a scalar intercept term;
• $X$ is an $n×m$ matrix of values of independent variables;
• $\beta$ is an $m×1$ matrix of unknown values of parameters;
• $\epsilon$ is an $n×1$ matrix of unknown random errors such that variance of $\epsilon ={\sigma }^{2}I$.
Let $\stackrel{~}{X}$ be the mean-centred $X$ and $\stackrel{~}{y}$ the mean-centred $y$. Furthermore, $\stackrel{~}{X}$ is scaled such that the diagonal elements of the cross product matrix ${\stackrel{~}{X}}^{\mathrm{T}}\stackrel{~}{X}$ are one. The linear model now takes the form:
 $y~ = X~ β~ + ε .$
Ridge regression estimates the parameters $\stackrel{~}{\beta }$ in a penalised least squares sense by finding the $\stackrel{~}{b}$ that minimizes
 $‖X~b~-y~‖ 2 + h ‖b~‖ 2 , h>0 ,$
where $‖·‖$ denotes the ${\ell }_{2}$-norm and $h$ is a scalar regularization or ridge parameter. For a given value of $h$, the parameter estimates $\stackrel{~}{b}$ are found by evaluating
 $b~ = (X~TX~+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 (${\stackrel{~}{X}}^{\mathrm{T}}\stackrel{~}{X}+hI$) directly, g02kaf uses the singular value decomposition (SVD) of $\stackrel{~}{X}$. After decomposing $\stackrel{~}{X}$ into $UD{V}^{\mathrm{T}}$ 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, $\gamma$, in the model is given by the sum of diagonal elements of
 $DT D (DTD+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 $j$th variance inflation factor, ${v}_{j}$, is a scaled version of the multiple correlation coefficient between independent variable $j$ and the other independent variables, ${R}_{j}$, 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~TX~+hI)-1 X~T X~ (X~TX~+hI)-1 ,$
which, using the SVD of $\stackrel{~}{X}$, is equivalent to the diagonal elements of the matrix:
 $V (DTD+hI)-1 DT D (DTD+hI)-1 VT .$
Although parameter estimates $\stackrel{~}{b}$ are calculated by using $\stackrel{~}{X}$, it is usual to report the parameter estimates $b$ associated with $X$. These are calculated from $\stackrel{~}{b}$, and the means and scalings of $X$. Optionally, either $\stackrel{~}{b}$ or $b$ may be calculated.
The method can adopt one of four criteria to minimize while calculating a suitable value for $h$:
1. (a)Generalized cross-validation (GCV):
 $ns (n-γ) 2 ;$
2. (b)Unbiased estimate of variance (UEV):
 $s n-γ ;$
3. (c)Future prediction error (FPE):
 $1n (s+2γs n-γ ) ;$
4. (d)Bayesian information criterion (BIC):
 $1n (s+ log(n)γ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.

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
2: $\mathbf{m}$Integer Input
On entry: the number of independent variables available in the data matrix $X$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
3: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: the values of independent variables in the data matrix $X$.
4: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g02kaf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
5: $\mathbf{isx}\left({\mathbf{m}}\right)$Integer array Input
On entry: indicates which $m$ independent variables are included in the model.
${\mathbf{isx}}\left(j\right)=1$
The $j$th variable in x will be included in the model.
${\mathbf{isx}}\left(j\right)=0$
Variable $j$ is excluded.
Constraint: ${\mathbf{isx}}\left(\mathit{j}\right)=0$ or $1$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
6: $\mathbf{ip}$Integer Input
On entry: $m$, the number of independent variables in the model.
Constraints:
• $1\le {\mathbf{ip}}\le {\mathbf{m}}$;
• Exactly ip elements of isx must be equal to $1$.
7: $\mathbf{tau}$Real (Kind=nag_wp) Input
On entry: singular values less than tau of the SVD of the data matrix $X$ will be set equal to zero.
Suggested value: ${\mathbf{tau}}=0.0$.
Constraint: ${\mathbf{tau}}\ge 0.0$.
8: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n$ values of the dependent variable $y$.
9: $\mathbf{h}$Real (Kind=nag_wp) Input/Output
On entry: an initial value for the ridge regression parameter $h$; used as a starting point for the optimization.
Constraint: ${\mathbf{h}}>0.0$.
On exit: h is the optimized value of the ridge regression parameter $h$.
10: $\mathbf{opt}$Integer Input
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:
${\mathbf{opt}}=1$
Generalized cross-validation (GCV);
${\mathbf{opt}}=2$
Unbiased estimate of variance (UEV)
${\mathbf{opt}}=3$
Future prediction error (FPE)
${\mathbf{opt}}=4$
Bayesian information criteron (BIC).
Constraint: ${\mathbf{opt}}=1$, $2$, $3$ or $4$.
11: $\mathbf{niter}$Integer Input/Output
On entry: the maximum number of iterations allowed to optimize the ridge regression parameter $h$.
Constraint: ${\mathbf{niter}}\ge 1$.
On exit: the number of iterations used to optimize the ridge regression parameter $h$ within tol.
12: $\mathbf{tol}$Real (Kind=nag_wp) Input
On entry: iterations of the ridge regression parameter $h$ will halt when consecutive values of $h$ lie within tol.
Constraint: ${\mathbf{tol}}>0.0$.
13: $\mathbf{nep}$Real (Kind=nag_wp) Output
On exit: the number of effective parameters, $\gamma$, in the model.
14: $\mathbf{orig}$Integer Input
On entry: if ${\mathbf{orig}}=1$, the parameter estimates $b$ are calculated for the original data; otherwise ${\mathbf{orig}}=2$ and the parameter estimates $\stackrel{~}{b}$ are calculated for the standardized data.
Constraint: ${\mathbf{orig}}=1$ or $2$.
15: $\mathbf{b}\left({\mathbf{ip}}+1\right)$Real (Kind=nag_wp) array Output
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; ${\mathbf{b}}\left(\mathit{j}+1\right)$ contains the parameter estimate for the $\mathit{j}$th independent variable in the model, for $\mathit{j}=1,2,\dots ,{\mathbf{ip}}$.
16: $\mathbf{vif}\left({\mathbf{ip}}\right)$Real (Kind=nag_wp) array Output
On exit: the variance inflation factors in the order indicated by isx. For the $\mathit{j}$th independent variable in the model, ${\mathbf{vif}}\left(\mathit{j}\right)$ is the value of ${v}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathbf{ip}}$.
17: $\mathbf{res}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\mathbf{res}}\left(\mathit{i}\right)$ is the value of the $\mathit{i}$th residual for the fitted ridge regression model, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
18: $\mathbf{rss}$Real (Kind=nag_wp) Output
On exit: the sum of squares of residual values.
19: $\mathbf{df}$Integer Output
On exit: the degrees of freedom for the residual sum of squares rss.
20: $\mathbf{optloo}$Integer Input
On entry: if ${\mathbf{optloo}}=2$, the leave-one-out cross-validation estimate of prediction error is calculated; otherwise no such estimate is calculated and ${\mathbf{optloo}}=1$.
Constraint: ${\mathbf{optloo}}=1$ or $2$.
21: $\mathbf{perr}\left(5\right)$Real (Kind=nag_wp) array Output
On exit: the first four elements contain, in this order, the measures of prediction error: GCV, UEV, FPE and BIC.
If ${\mathbf{optloo}}=2$, ${\mathbf{perr}}\left(5\right)$ is the LOOCV estimate of prediction error; otherwise ${\mathbf{perr}}\left(5\right)$ is not referenced.
22: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{h}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{h}}>0.0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>1$.
On entry, ${\mathbf{niter}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{niter}}\ge 1$.
On entry, ${\mathbf{opt}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{opt}}=1$, $2$, $3$ or $4$.
On entry, ${\mathbf{optloo}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{optloo}}=1$ or $2$.
On entry, ${\mathbf{orig}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{orig}}=1$ or $2$.
On entry, ${\mathbf{tau}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tau}}\ge 0.0$.
On entry, ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$; ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{ip}}\le {\mathbf{m}}$.
On entry, ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$.
Constraint: $\mathrm{sum}\left({\mathbf{isx}}\right)={\mathbf{ip}}$.
On entry, ${\mathbf{isx}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{isx}}\left(j\right)=0$ or $1$.
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$; ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=3$
SVD failed to converge.
${\mathbf{ifail}}=-1$
Maximum number of iterations used.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

g02kaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02kaf 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 routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

g02kaf allocates internally $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(5×\left({\mathbf{n}}-1\right),2×{\mathbf{ip}}×{\mathbf{ip}}\right)+\left({\mathbf{n}}+3\right)×{\mathbf{ip}}+{\mathbf{n}}$ elements of double precision storage.

## 10Example

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

### 10.1Program Text

Program Text (g02kafe.f90)

### 10.2Program Data

Program Data (g02kafe.d)

### 10.3Program Results

Program Results (g02kafe.r)