Source code for naginterfaces.​library.​examples.​correg.​lars_ex

#!/usr/bin/env python
"``naginterface.library.correg.lars`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name,too-many-locals

from math import floor

import numpy as np

from naginterfaces.library import correg

[docs]def main(): """ Example for :func:`naginterfaces.library.correg.lars`. Least angle regression. >>> main() naginterfaces.library.correg.lars Python Example Results. Least angle regression. 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 ----------------------------------------------------------------- alpha: -50.037 ----------------------------------------------------------------- Step Sum RSS df Cp Ck Step Size ----------------------------------------------------------------- 1 72.446 8929.855 2 13.355 123.227 72.446 2 103.385 6404.701 3 7.054 50.781 24.841 3 126.243 5258.247 4 5.286 30.836 16.225 4 145.277 4657.051 5 5.309 19.319 11.587 5 198.223 3959.401 6 5.016 12.266 24.520 6 203.529 3954.571 7 7.000 0.910 2.198 ----------------------------------------------------------------- sigma^2: 304.198 """ print('naginterfaces.library.correg.lars Python Example Results.') print('Least angle regression.') # The data: d = np.array([ [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], ]) # The model type: mtype = 1 # The dependent observations: 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, ] ip, nstep, b, fitsum = correg.lars( mtype, d, y, mnstep=6, ) sep_len = 5 + ip*10 print('Step' + ' '*15 + 'Parameter Estimate') print('-'*sep_len) for k in range(nstep): print( ' ' + str(k+1) + ' ' + ' '.join('{:6.3f}'.format(bi) for bi in b[:ip, k]) ) print('-'*sep_len) print('alpha: {:.3f}'.format(fitsum[0, nstep])) print('-'*sep_len) print('Step Sum RSS df Cp Ck Step Size') print('-'*sep_len) for k in range(nstep): print( '{:3d} {:9.3f} {:9.3f} {:6.0f} {:9.3f} {:9.3f} {:9.3f}'.format( k+1, fitsum[0, k], fitsum[1, k], floor(fitsum[2, k] + 0.5), fitsum[3, k], fitsum[4, k], fitsum[5, k] ) ) print('-'*sep_len) print('sigma^2: {:9.3f}'.format(fitsum[4, nstep]))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )