#NEWLINE# def find_best_fit( self , r , nr ) : #NEWLINE# #INDENT# '\n        Use simple linear regression to tune parameters self._slope and\n        self._intercept in the log-log space based on count and Nr(count)\n        (Work in log space to avoid floating point underflow.)\n        ' #NEWLINE# if ( ( not r ) or ( not nr ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# zr = [ ] #NEWLINE# for j in range ( len ( r ) ) : #NEWLINE# #INDENT# i = ( r [ ( j - 1 ) ] if ( j > 0 ) else 0 ) #NEWLINE# k = ( ( ( 2 * r [ j ] ) - i ) if ( j == ( len ( r ) - 1 ) ) else r [ ( j + 1 ) ] ) #NEWLINE# zr_ = ( ( 2.0 * nr [ j ] ) / ( k - i ) ) #NEWLINE# zr . append ( zr_ ) #NEWLINE# #UNINDENT# log_r = [ math . log ( i ) for i in r ] #NEWLINE# log_zr = [ math . log ( i ) for i in zr ] #NEWLINE# xy_cov = x_var = 0.0 #NEWLINE# x_mean = ( sum ( log_r ) / len ( log_r ) ) #NEWLINE# y_mean = ( sum ( log_zr ) / len ( log_zr ) ) #NEWLINE# for ( x , y ) in zip ( log_r , log_zr ) : #NEWLINE# #INDENT# xy_cov += ( ( x - x_mean ) * ( y - y_mean ) ) #NEWLINE# x_var += ( ( x - x_mean ) ** 2 ) #NEWLINE# #UNINDENT# self . _slope = ( ( xy_cov / x_var ) if ( x_var != 0 ) else 0.0 ) #NEWLINE# if ( self . _slope >= ( - 1 ) ) : #NEWLINE# #INDENT# warnings . warn ( 'SimpleGoodTuring did not find a proper best fit line for smoothing probabilities of occurrences. The probability estimates are likely to be unreliable.' ) #NEWLINE# #UNINDENT# self . _intercept = ( zr_ - ( self . _slope * x_mean ) )