<s> """<STR_LIT>""" <EOL> from statsmodels . compat . python import range <EOL> import numpy as np <EOL> from scipy . linalg import lstsq <EOL> def lowess ( endog , exog , frac = <NUM_LIT> / <NUM_LIT:3> , it = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> x = exog <EOL> if exog . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if endog . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if endog . shape [ <NUM_LIT:0> ] != x . shape [ <NUM_LIT:0> ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> n = exog . shape [ <NUM_LIT:0> ] <EOL> fitted = np . zeros ( n ) <EOL> k = int ( frac * n ) <EOL> index_array = np . argsort ( exog ) <EOL> x_copy = np . array ( exog [ index_array ] ) <EOL> y_copy = endog [ index_array ] <EOL> fitted , weights = _lowess_initial_fit ( x_copy , y_copy , k , n ) <EOL> for i in range ( it ) : <EOL> _lowess_robustify_fit ( x_copy , y_copy , fitted , <EOL> weights , k , n ) <EOL> out = np . array ( [ x_copy , fitted ] ) . T <EOL> out . shape = ( n , <NUM_LIT:2> ) <EOL> return out <EOL> def _lowess_initial_fit ( x_copy , y_copy , k , n ) : <EOL> """<STR_LIT>""" <EOL> weights = np . zeros ( ( n , k ) , dtype = x_copy . dtype ) <EOL> nn_indices = [ <NUM_LIT:0> , k ] <EOL> X = np . ones ( ( k , <NUM_LIT:2> ) ) <EOL> fitted = np . zeros ( n ) <EOL> for i in range ( n ) : <EOL> left_width = x_copy [ i ] - x_copy [ nn_indices [ <NUM_LIT:0> ] ] <EOL> right_width = x_copy [ nn_indices [ <NUM_LIT:1> ] - <NUM_LIT:1> ] - x_copy [ i ] <EOL> width = max ( left_width , right_width ) <EOL> _lowess_wt_standardize ( weights [ i , : ] , <EOL> x_copy [ nn_indices [ <NUM_LIT:0> ] : nn_indices [ <NUM_LIT:1> ] ] , <EOL> x_copy [ i ] , width ) <EOL> _lowess_tricube ( weights [ i , : ] ) <EOL> weights [ i , : ] = np . sqrt ( weights [ i , : ] ) <EOL> X [ : , <NUM_LIT:1> ] = x_copy [ nn_indices [ <NUM_LIT:0> ] : nn_indices [ <NUM_LIT:1> ] ] <EOL> y_i = weights [ i , : ] * y_copy [ nn_indices [ <NUM_LIT:0> ] : nn_indices [ <NUM_LIT:1> ] ] <EOL> beta = lstsq ( weights [ i , : ] . reshape ( k , <NUM_LIT:1> ) * X , y_i ) [ <NUM_LIT:0> ] <EOL> fitted [ i ] = beta [ <NUM_LIT:0> ] + beta [ <NUM_LIT:1> ] * x_copy [ i ] <EOL> _lowess_update_nn ( x_copy , nn_indices , i + <NUM_LIT:1> ) <EOL> return fitted , weights <EOL> def _lowess_wt_standardize ( weights , new_entries , x_copy_i , width ) : <EOL> """<STR_LIT>""" <EOL> weights [ : ] = new_entries <EOL> weights -= x_copy_i <EOL> weights /= width <EOL> def _lowess_robustify_fit ( x_copy , y_copy , fitted , weights , k , n ) : <EOL> """<STR_LIT>""" <EOL> nn_indices = [ <NUM_LIT:0> , k ] <EOL> X = np . ones ( ( k , <NUM_LIT:2> ) ) <EOL> residual_weights = np . copy ( y_copy ) <EOL> residual_weights . shape = ( n , ) <EOL> residual_weights -= fitted <EOL> residual_weights = np . absolute ( residual_weights ) <EOL> s = np . median ( residual_weights ) <EOL> residual_weights /= ( <NUM_LIT:6> * s ) <EOL> too_big = residual_weights >= <NUM_LIT:1> <EOL> _lowess_bisquare ( residual_weights ) <EOL> residual_weights [ too_big ] = <NUM_LIT:0> <EOL> for i in range ( n ) : <EOL> total_weights = weights [ i , : ] * np . sqrt ( residual_weights [ nn_indices [ <NUM_LIT:0> ] : <EOL> nn_indices [ <NUM_LIT:1> ] ] ) <EOL> X [ : , <NUM_LIT:1> ] = x_copy [ nn_indices [ <NUM_LIT:0> ] : nn_indices [ <NUM_LIT:1> ] ] <EOL> y_i = total_weights * y_copy [ nn_indices [ <NUM_LIT:0> ] : nn_indices [ <NUM_LIT:1> ] ] <EOL> total_weights . shape = ( k , <NUM_LIT:1> ) <EOL> beta = lstsq ( total_weights * X , y_i ) [ <NUM_LIT:0> ] <EOL> fitted [ i ] = beta [ <NUM_LIT:0> ] + beta [ <NUM_LIT:1> ] * x_copy [ i ] <EOL> _lowess_update_nn ( x_copy , nn_indices , i + <NUM_LIT:1> ) <EOL> def _lowess_update_nn ( x , cur_nn , i ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> if cur_nn [ <NUM_LIT:1> ] < x . size : <EOL> left_dist = x [ i ] - x [ cur_nn [ <NUM_LIT:0> ] ] <EOL> new_right_dist = x [ cur_nn [ <NUM_LIT:1> ] ] - x [ i ] <EOL> if new_right_dist < left_dist : <EOL> cur_nn [ <NUM_LIT:0> ] = cur_nn [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> cur_nn [ <NUM_LIT:1> ] = cur_nn [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> else : <EOL> break <EOL> else : <EOL> break <EOL> def _lowess_tricube ( t ) : <EOL> """<STR_LIT>""" <EOL> t [ : ] = np . absolute ( t ) <EOL> _lowess_mycube ( t ) <EOL> t [ : ] = np . negative ( t ) <EOL> t += <NUM_LIT:1> <EOL> _lowess_mycube ( t ) <EOL> def _lowess_mycube ( t ) : <EOL> """<STR_LIT>""" <EOL> t2 = t * t <EOL> t *= t2 <EOL> def _lowess_bisquare ( t ) : <EOL> """<STR_LIT>""" <EOL> t *= t <EOL> t [ : ] = np . negative ( t ) <EOL> t += <NUM_LIT:1> <EOL> t *= t </s>
