#!/usr/bin/env python3
"``naginterface.library.correg.ridge_opt`` Python Example."
# NAG Copyright 2017-2019.
# pylint: disable=invalid-name
from naginterfaces.library import correg
[docs]def main():
"""
Example for :func:`naginterfaces.library.correg.ridge_opt`.
Ridge regression, optimizing a ridge regression parameter.
>>> main()
naginterfaces.library.correg.ridge_opt Python Example Results.
Ridge regression optimizing GCV prediction error for a body fat model.
Value of ridge parameter: 0.0712
Sum of squares of residuals: 1.0917e+02
Degrees of freedom: 16
Number of effective parameters: 2.9059
Parameter estimates
1 20.1950
2 9.7934
3 9.9576
4 -2.0125
Number of iterations: 6
Ridge parameter minimises GCV
Estimated prediction errors:
GCV = 7.4718
UEV = 6.3862
FPE = 7.3141
BIC = 8.2380
LOO CV = 7.5495
Residuals
1 -1.9894
2 3.5469
3 -3.0392
4 -3.0309
5 -0.1899
6 -0.3146
7 0.9775
8 4.0157
9 2.5332
10 -2.3560
11 0.5446
12 2.3989
13 -4.0876
14 3.2778
15 0.2894
16 0.7330
17 -0.7116
18 -0.6092
19 -2.9995
20 1.0110
Variance inflation factors
1 0.2928
2 0.4162
3 0.8089
"""
print('naginterfaces.library.correg.ridge_opt Python Example Results.')
print(
'Ridge regression optimizing GCV prediction error '
'for a body fat model.'
)
# The independent variables:
x = [
[19.5, 43.1, 29.1],
[24.7, 49.8, 28.2],
[30.7, 51.9, 37.0],
[29.8, 54.3, 31.1],
[19.1, 42.2, 30.9],
[25.6, 53.9, 23.7],
[31.4, 58.5, 27.6],
[27.9, 52.1, 30.6],
[22.1, 49.9, 23.2],
[25.5, 53.5, 24.8],
[31.1, 56.6, 30.0],
[30.4, 56.7, 28.3],
[18.7, 46.5, 23.0],
[19.7, 44.2, 28.6],
[14.6, 42.7, 21.3],
[29.5, 54.4, 30.1],
[27.7, 55.3, 25.7],
[30.2, 58.6, 24.6],
[22.7, 48.2, 27.1],
[25.2, 51.0, 27.5],
]
# The inclusions:
isx = [1]*len(x[0])
# The dependent variable:
y = [
11.9,
22.8,
18.7,
20.1,
12.9,
21.7,
27.1,
25.4,
21.3,
19.3,
25.4,
27.2,
11.7,
17.8,
12.8,
23.9,
22.6,
25.4,
14.8,
21.1,
]
# The optimization starting point:
h = 0.5
# The prediction-error mode (GCV):
opt = 1
# The iteration limit:
niter = 25
# The termination tolerance:
tol = 1.e-4
# Calculate parameter estimates for the standardized data:
orig = 2
# Calculate leave-one-out cross-validation estimate:
optloo = 2
regn = correg.ridge_opt(
x, isx, y, h, opt, niter, tol, orig, optloo,
)
print('Value of ridge parameter: {:10.4f}'.format(regn.h))
print('Sum of squares of residuals: {:11.4e}'.format(regn.rss))
print('Degrees of freedom: {:d}'.format(regn.df))
print('Number of effective parameters: {:10.4f}'.format(regn.nep))
print('Parameter estimates')
for i, b_i in enumerate(regn.b):
print('{:d} {:11.4f}'.format(i+1, b_i))
print('Number of iterations: {:d}'.format(regn.niter))
print('Ridge parameter minimises GCV')
print('Estimated prediction errors:')
print('GCV = {:10.4f}'.format(regn.perr[0]))
print('UEV = {:10.4f}'.format(regn.perr[1]))
print('FPE = {:10.4f}'.format(regn.perr[2]))
print('BIC = {:10.4f}'.format(regn.perr[3]))
print('LOO CV = {:10.4f}'.format(regn.perr[4]))
print('Residuals')
for i, r_i in enumerate(regn.res):
print('{:d} {:11.4f}'.format(i+1, r_i))
print('Variance inflation factors')
for i, v_i in enumerate(regn.vif):
print('{:d} {:11.4f}'.format(i+1, v_i))
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)