#NEWLINE# def fit( self , y , x = None , weights = None , pen = 0.0 ) : #NEWLINE# #INDENT# '\n        Fit the smoothing spline to a set of (x,y) pairs.\n\n        INPUTS:\n           y       -- response variable\n           x       -- if None, uses self.x\n           weights -- optional array of weights\n           pen     -- constant in front of Gram matrix\n\n        OUTPUTS: None\n           The smoothing spline is determined by self.coef,\n           subsequent calls of __call__ will be the smoothing spline.\n\n        ALGORITHM:\n           Formally, this solves a minimization:\n\n           fhat = ARGMIN_f SUM_i=1^n (y_i-f(x_i))^2 + pen * int f^(2)^2\n\n           int is integral. pen is lambda (from Hastie)\n\n           See Chapter 5 of\n\n           Hastie, Tibshirani and Friedman (2001). "The Elements of Statistical\n           Learning." Springer-Verlag. 536 pages.\n\n           for more details.\n\n        TODO:\n           Should add arbitrary derivative penalty instead of just\n           second derivative.\n        ' #NEWLINE# banded = True #NEWLINE# if ( x is None ) : #NEWLINE# #INDENT# x = self . _x #NEWLINE# bt = self . _basisx . copy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# bt = self . basis ( x ) #NEWLINE# #UNINDENT# if ( pen == 0.0 ) : #NEWLINE# #INDENT# banded = False #NEWLINE# #UNINDENT# if ( x . shape != y . shape ) : #NEWLINE# #INDENT# raise ValueError ( "x and y shape do not agree, by default x are                the Bspline's internal knots" ) #NEWLINE# #UNINDENT# if ( pen >= self . penmax ) : #NEWLINE# #INDENT# pen = self . penmax #NEWLINE# #UNINDENT# if ( weights is not None ) : #NEWLINE# #INDENT# self . weights = weights #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . weights = 1.0 #NEWLINE# #UNINDENT# _w = np . sqrt ( self . weights ) #NEWLINE# bt *= _w #NEWLINE# mask = np . flatnonzero ( ( 1 - np . alltrue ( np . equal ( bt , 0 ) , axis = 0 ) ) ) #NEWLINE# bt = bt [ : , mask ] #NEWLINE# y = y [ mask ] #NEWLINE# self . df_total = y . shape [ 0 ] #NEWLINE# bty = np . squeeze ( np . dot ( bt , ( _w * y ) ) ) #NEWLINE# self . N = y . shape [ 0 ] #NEWLINE# if ( not banded ) : #NEWLINE# #INDENT# self . btb = np . dot ( bt , bt . T ) #NEWLINE# _g = _band2array ( self . g , lower = 1 , symmetric = True ) #NEWLINE# ( self . coef , _ , self . rank ) = L . lstsq ( ( self . btb + ( pen * _g ) ) , bty ) [ 0 : 3 ] #NEWLINE# self . rank = min ( self . rank , self . btb . shape [ 0 ] ) #NEWLINE# del _g #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . btb = np . zeros ( self . g . shape , np . float64 ) #NEWLINE# ( nband , nbasis ) = self . g . shape #NEWLINE# for i in range ( nbasis ) : #NEWLINE# #INDENT# for k in range ( min ( nband , ( nbasis - i ) ) ) : #NEWLINE# #INDENT# self . btb [ ( k , i ) ] = ( bt [ i ] * bt [ ( i + k ) ] ) . sum ( ) #NEWLINE# #UNINDENT# #UNINDENT# bty . shape = ( 1 , bty . shape [ 0 ] ) #NEWLINE# self . pen = pen #NEWLINE# ( self . chol , self . coef ) = solveh_banded ( ( self . btb + ( pen * self . g ) ) , bty , lower = 1 ) #NEWLINE# #UNINDENT# self . coef = np . squeeze ( self . coef ) #NEWLINE# self . resid = ( ( y * self . weights ) - np . dot ( self . coef , bt ) ) #NEWLINE# self . pen = pen #NEWLINE# del bty #NEWLINE# del mask #NEWLINE# del bt