#NEWLINE# @ staticmethod #NEWLINE# def fitDiscretePowerLaw( x , xmins = None ) : #NEWLINE# #INDENT# '\n        Take a sample of discrete data points which are drawn from a power law probability\n        distribution (p(x) = x-alpha / zeta(alpha, xmin)) and return the exponent.\n        If xmins is supplied then it searches through the set of xmins rather than\n        using all possible xmins. Most of the time it helps to keep xmins low. \n\n        Returns the goodness of fit, best alpha and xmin. If there is only 1 unique \n        value of x then -1, -1 min(x) is returned.\n\n        ' #NEWLINE# xmax = numpy . max ( x ) #NEWLINE# if ( xmins == None ) : #NEWLINE# #INDENT# xmin = numpy . max ( numpy . array ( [ numpy . min ( x ) , 1 ] ) ) #NEWLINE# xmins = numpy . arange ( xmin , xmax ) #NEWLINE# #UNINDENT# if ( xmins . shape [ 0 ] == 0 ) : #NEWLINE# #INDENT# return ( ( - 1 ) , ( - 1 ) , numpy . min ( x ) ) #NEWLINE# #UNINDENT# alphas = numpy . arange ( 1.5 , 3.5 , 0.01 ) #NEWLINE# ksAlpha = numpy . zeros ( ( xmins . shape [ 0 ] , 2 ) ) #NEWLINE# for j in range ( xmins . shape [ 0 ] ) : #NEWLINE# #INDENT# xmin = xmins [ j ] #NEWLINE# z = x [ ( x >= xmin ) ] #NEWLINE# n = z . shape [ 0 ] #NEWLINE# sumLogx = numpy . sum ( numpy . log ( z ) ) #NEWLINE# likelyhoods = numpy . zeros ( alphas . shape [ 0 ] ) #NEWLINE# for i in range ( alphas . shape [ 0 ] ) : #NEWLINE# #INDENT# likelyhoods [ i ] = ( ( ( - n ) * numpy . log ( scipy . special . zeta ( alphas [ i ] , xmin ) ) ) - ( alphas [ i ] * sumLogx ) ) #NEWLINE# #UNINDENT# k = numpy . argmax ( likelyhoods ) #NEWLINE# cdf = numpy . cumsum ( ( numpy . bincount ( z ) [ xmin : xmax ] / float ( n ) ) ) #NEWLINE# fit = ( ( numpy . arange ( xmin , xmax ) ** ( - alphas [ k ] ) ) / scipy . special . zeta ( alphas [ k ] , xmin ) ) #NEWLINE# fit = numpy . cumsum ( xmins ) #NEWLINE# ksAlpha [ ( j , 0 ) ] = numpy . max ( numpy . abs ( ( cdf - fit ) ) ) #NEWLINE# ksAlpha [ ( j , 1 ) ] = alphas [ k ] #NEWLINE# #UNINDENT# i = numpy . argmin ( ksAlpha [ : , 0 ] ) #NEWLINE# return ( ksAlpha [ ( i , 0 ) ] , ksAlpha [ ( i , 1 ) ] , xmins [ i ] )