powerlaw icon indicating copy to clipboard operation
powerlaw copied to clipboard

FloatingPointError when Fit() is invoked

Open harshays opened this issue 7 years ago • 1 comments

powerlaw.Fit(discrete=True, data=[1]*1000 + [10]*100 + [100]*10 + [1000])
Output:
Calculating best minimal value for power law fit
---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)
<ipython-input-320-4afe2d64269c> in <module>()
      1 # TODO, floating point error/bug in powerlaw lib
----> 2 powerlaw.Fit(discrete=True, data=[1,10,100,1000,100000])

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in __init__(self, data, discrete, xmin, xmax, fit_method, estimate_discrete, discrete_approximation, sigma_threshold, parameter_range, fit_optimizer, xmin_distance, **kwargs)
    127             self.fixed_xmin=False
    128             print("Calculating best minimal value for power law fit", file=sys.stderr)
--> 129             self.find_xmin()
    130 
    131         self.data = self.data[self.data>=self.xmin]

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in find_xmin(self, xmin_distance)
    226             return getattr(pl, xmin_distance), pl.alpha, pl.sigma, pl.in_range()
    227 
--> 228         fits = asarray(list(map(fit_function, xmins)))
    229         # logging.warning(fits.shape)
    230         setattr(self, xmin_distance+'s', fits[:,0])

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in fit_function(xmin)
    223                            data=self.data,
    224                            parameter_range=self.parameter_range,
--> 225                            parent_Fit=self)
    226             return getattr(pl, xmin_distance), pl.alpha, pl.sigma, pl.in_range()
    227 

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in __init__(self, estimate_discrete, **kwargs)
   1103     def __init__(self, estimate_discrete=True, **kwargs):
   1104         self.estimate_discrete = estimate_discrete
-> 1105         Distribution.__init__(self, **kwargs)
   1106 
   1107     def parameters(self, params):

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in __init__(self, xmin, xmax, discrete, fit_method, data, parameters, parameter_range, initial_parameters, discrete_approximation, parent_Fit, **kwargs)
    600 
    601         if (data is not None) and not (parameter_range and self.parent_Fit):
--> 602             self.fit(data)
    603 
    604 

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in fit(self, data)
   1139             if not self.in_range():
   1140                 Distribution.fit(self, data, suppress_output=True)
-> 1141             self.KS(data)
   1142         else:
   1143             Distribution.fit(self, data, suppress_output=True)

/usr/local/lib/python2.7/site-packages/powerlaw.pyc in KS(self, data)
    690         self.Asquare = sum((
    691                             (CDF_diff**2) /
--> 692                             (Theoretical_CDF * (1 - Theoretical_CDF))
    693                             )[1:]
    694                            )

FloatingPointError: invalid value encountered in divide
```

harshays avatar Mar 14 '17 19:03 harshays

Does it still return a result? If so, then this is a duplicate of https://github.com/jeffalstott/powerlaw/issues/25

If it doesn't return a result, then I can't replicate this. I get:

> fit = powerlaw.Fit(discrete=True, data=[1]*1000 + [10]*100 + [100]*10 + [1000])
Calculating best minimal value for power law fit
C:\Users\jeff\Anaconda3\lib\site-packages\powerlaw.py:692: RuntimeWarning: invalid value encountered in true_divide
  (Theoretical_CDF * (1 - Theoretical_CDF))
> fit.alpha
2.0547757242410278

jeffalstott avatar Mar 15 '17 00:03 jeffalstott