#NEWLINE# def stetson_mean( x , weight = 100.0 , alpha = 2.0 , beta = 2.0 , tol = 1e-06 , nmax = 20 ) : #NEWLINE# #INDENT# 'An iteratively weighted mean used in the Stetson variability index' #NEWLINE# mu = np . median ( x ) #NEWLINE# for i in range ( nmax ) : #NEWLINE# #INDENT# resid = ( x - mu ) #NEWLINE# resid_err = ( np . abs ( resid ) * np . sqrt ( weight ) ) #NEWLINE# weight1 = ( weight / ( 1.0 + ( ( resid_err / alpha ) ** beta ) ) ) #NEWLINE# weight1 /= weight1 . mean ( ) #NEWLINE# diff = ( np . mean ( ( x * weight1 ) ) - mu ) #NEWLINE# mu += diff #NEWLINE# if ( ( np . abs ( diff ) < ( tol * np . abs ( mu ) ) ) or ( np . abs ( diff ) < tol ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# return mu