def ksstat(x, cdf, alternative='two_sided', args=()):
 nobs = float(len(x))
 if isinstance(cdf, string_types):
  cdf = getattr(stats.distributions, cdf).cdf
 elif hasattr(cdf, 'cdf'):
  cdf = getattr(cdf, 'cdf')
 x = np.sort(x)
 cdfvals = cdf(x, *args)
 if (alternative in ['two_sided', 'greater']):
  Dplus = ((np.arange(1.0, (nobs + 1)) / nobs) - cdfvals).max()
  if (alternative == 'greater'):
   return Dplus
 if (alternative in ['two_sided', 'less']):
  Dmin = (cdfvals - (np.arange(0.0, nobs) / nobs)).max()
  if (alternative == 'less'):
   return Dmin
 D = np.max([Dplus, Dmin])
 return D

def ksstat ( x , cdf , alternative = 'two_sided' , args = ( ) ) : nobs = float ( len ( x ) ) if isinstance ( cdf , string_types ) : cdf = getattr ( stats . distributions , cdf ) . cdf elif hasattr ( cdf , 'cdf' ) : cdf = getattr ( cdf , 'cdf' ) x = np . sort ( x ) cdfvals = cdf ( x , * args ) if ( alternative in [ 'two_sided' , 'greater' ] ) : Dplus = ( ( np . arange ( 1.0 , ( nobs + 1 ) ) / nobs ) - cdfvals ) . max ( ) if ( alternative == 'greater' ) : return Dplus if ( alternative in [ 'two_sided' , 'less' ] ) : Dmin = ( cdfvals - ( np . arange ( 0.0 , nobs ) / nobs ) ) . max ( ) if ( alternative == 'less' ) : return Dmin D = np . max ( [ Dplus , Dmin ] ) return D
calculate statistic for the kolmogorov-smirnov test for goodness of fit this calculates the test statistic for a test of the distribution g(x) of an observed variable against a given distribution f(x) .