josemiotto / pylevy

Levy distributions for Python
GNU General Public License v3.0
34 stars 18 forks source link

levy,fit_levy #2

Closed mindtd closed 7 years ago

mindtd commented 7 years ago

PyLevy did really well on modeling. I had difficulty getting fit_levy to work. First try it on field data, ran into IndexError (below), then try to fit "normal" Levy pdf (one in Python 2.7). Run into same IndexError.

Any idea what the root cause is? Summary 1) forward model levy import levy alpha, beta, mu, sigma = 1.0, 0.3, 0.0, 1.0 x = np.linspace(-2.0, 2.0, 40) plt.plot(x, levy.levy(x, alpha, beta, mu, sigma), 'k-', color='orange', lw=2, label='levy_stable pdf') all works nicely.

2) try on "normal" Levy pdf from scipy.stats import levy as levy0 rv = levy0(mu-1.0, sigma)

fit to the normal distribution

(alpha, beta, mu, sigma) = levy.fit_levy(rv.pdf(x))

get IndexError: index 51 is out of bounds for axis 0 with size 50 (detail follows)

Traceback (most recent call last): File "C:\Users\user\Documents\Python Quants\Test_stable_levy.py", line 57, in (alpha, beta, mu, sigma) = levy.fit_levy(rv.pdf(x)) File "C:\Python27\lib\site-packages\levy.py", line 369, in fit_levy parameters.x = optimize.fmin(neglog_density, parameters.x, disp=0) File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 374, in fmin res = _minimize_neldermead(func, x0, args, callback=callback, *_opts) File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 466, in _minimize_neldermead fxe = func(xe) File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 282, in functionwrapper return function((wrapper_args + args)) File "C:\Python27\lib\site-packages\levy.py", line 367, in neglog_density return np.sum(neglog_levy(x, alpha, beta, mu, sigma)) File "C:\Python27\lib\site-packages\levy.py", line 315, in neglog_levy return -np.log(np.maximum(1e-100, levy(x, alpha, beta, mu, sigma, par=par))) File "C:\Python27\lib\site-packages\levy.py", line 285, in levy l = limits[alpha_index, beta_index] IndexError: index 51 is out of bounds for axis 0 with size 50

mindtd commented 7 years ago

Workaround - using the same data, fix both alpha and beta, fit_levy runs. When only alpha is fixed, let fit_levy work out beta, mu and sigma, sometimes it runs, otherwise fails (same error, out of bounds).

Observation - fit_levy is quite sensitive to underlying data. Is that shared experience?

aavanian commented 7 years ago

yep, had the case too recently but didn't have time to look into it much. Switching to one of the bounded solver in scipy.optimize.minimize (i.e.: one of L-BFGS-B, TNC, COBYLA and SLSQP) would do the trick but need to do a few tests re perf and choice between those 4.

josemiotto commented 7 years ago

Hi mindtd! two things: the levy method of scipy is a different distribution. You should use the levy_stable method; the problem you mention appears anyway, because of how the indices are defined; I changed that in the new version (if I can I push it today). By the way, I also changed the method to L-BFGS-B because of efficiency reasons.

mindtd commented 7 years ago

Thank you, Jose.