#!/usr/bin/env python3
"``naginterfaces.library.correg.lars_param`` Python Example."
# NAG Copyright 2018-2019.
# pylint: disable=invalid-name,too-many-locals
from naginterfaces.library import correg
[docs]def main():
"""
Example for :func:`naginterfaces.library.correg.lars_param`.
Least angle regression, additional parameter estimates.
>>> main()
naginterfaces.library.correg.lars_param Python Example Results.
Parameter Estimates from lars_xtx
Step Parameter Estimate
-----------------------------------------------------------------
1 0.000 0.000 3.125 0.000 0.000 0.000
2 0.000 0.000 3.792 0.000 0.000 -0.713
3 -0.446 0.000 3.998 0.000 0.000 -1.151
4 -0.628 -0.295 4.098 0.000 0.000 -1.466
5 -1.060 -1.056 4.110 -0.864 0.000 -1.948
6 -1.073 -1.132 4.118 -0.935 -0.059 -1.981
Additional Parameter Estimates from lars_param
nk Parameter Estimate
-----------------------------------------------------------------
0.2 0.000 0.000 0.625 0.000 0.000 0.000
1.2 0.000 0.000 3.258 0.000 0.000 -0.143
3.2 -0.483 -0.059 4.018 0.000 0.000 -1.214
4.5 -0.844 -0.676 4.104 -0.432 0.000 -1.707
5.2 -1.062 -1.071 4.112 -0.878 -0.012 -1.955
"""
print('naginterfaces.library.correg.lars_param Python Example Results.')
n = 20
mtype = 1
m = 6
# Initialize dataset
d = [[10.28, 1.77, 9.69, 15.58, 8.23, 10.44],
[9.08, 8.99, 11.53, 6.57, 15.89, 12.58],
[17.98, 13.10, 1.04, 10.45, 10.12, 16.68],
[14.82, 13.79, 12.23, 7.00, 8.14, 7.79],
[17.53, 9.41, 6.24, 3.75, 13.12, 17.08],
[7.78, 10.38, 9.83, 2.58, 10.13, 4.25],
[11.95, 21.71, 8.83, 11.00, 12.59, 10.52],
[14.60, 10.09, -2.70, 9.89, 14.67, 6.49],
[3.63, 9.07, 12.59, 14.09, 9.06, 8.19],
[6.35, 9.79, 9.40, 12.79, 8.38, 16.79],
[4.66, 3.55, 16.82, 13.83, 21.39, 13.88],
[8.32, 14.04, 17.17, 7.93, 7.39, -1.09],
[10.86, 13.68, 5.75, 10.44, 10.36, 10.06],
[4.76, 4.92, 17.83, 2.90, 7.58, 11.97],
[5.05, 10.41, 9.89, 9.04, 7.90, 13.12],
[5.41, 9.32, 5.27, 15.53, 5.06, 19.84],
[9.77, 2.37, 9.54, 20.23, 9.33, 8.82],
[14.28, 4.34, 14.23, 14.95, 18.16, 11.03],
[10.17, 6.80, 3.17, 8.57, 16.07, 15.93],
[5.39, 2.67, 6.37, 13.56, 10.68, 7.35]]
# Initialize observed values
y = [-46.47, -35.80, -129.22, -42.44, -73.51,
-26.61, -63.90, -76.73, -32.64, -83.29,
-16.31, -5.82, -47.75, 18.38, -54.71,
-55.62, -45.28, -22.76, -104.32, -55.94]
# Create augmented matrix [d:y]
dy = [drow + [yrow] for drow, yrow in zip(d, y)]
# Calculate cross product (upper triangle) of augmented matrix
dytdy = correg.ssqmat(dy).c
# pm = amount of elements in triangle of m x m matrix
pm = (m*m+m)//2
# Cross product matrix of d (dtd) is stored in first pm elements
dtd = [dytdy[:pm]]
# Cross product of d and y (dty) are next m elements
dty = dytdy[pm:pm+m]
# Last element is sums of squares of y (yty)
yty = dytdy[-1]
# Fit model
ip, nstep, b, fitsum = correg.lars_xtx(mtype, n, dtd, dty, yty)
# Calculate addition parameters for intermediate lars steps
nk = [0.2, 1.2, 3.2, 4.5, 5.2]
nb = correg.lars_param(b, fitsum, 1, nk)
print('Parameter Estimates from lars_xtx')
print('Step' + ' '*15 + 'Parameter Estimate')
sep_len = 5 + ip*10
print('-'*sep_len)
for k in range(nstep):
print(' ' + str(k+1) + ' ' +
' '.join('{:6.3f}'.format(bi)
for bi in b[:ip, k]))
print('Additional Parameter Estimates from lars_param')
print(' nk ' + ' '*15 + 'Parameter Estimate')
sep_len = 5 + ip*10
print('-'*sep_len)
for i, v in enumerate(nk):
print(' ' + str(v) + ' ' +
' '.join('{:6.3f}'.format(bi)
for bi in nb[:ip, i]))
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)