Boulder-Investment-Technologies / lppls

Library for fitting the LPPLS model to data.
MIT License
350 stars 108 forks source link

ValueError of LPPL_CMAES in NASDAQ Example #69

Closed Zhoux26 closed 2 years ago

Zhoux26 commented 2 years ago
from lppls import lppls, data_loader
import numpy as np
import pandas as pd
from datetime import datetime as dt
%matplotlib inline

data = data_loader.nasdaq_dotcom()
time = [pd.Timestamp.toordinal(dt.strptime(t1, '%Y-%m-%d')) for t1 in data['Date']]

price = np.log(data['Adj Close'].values)

observations = np.array([time, price])

from lppls import lppls_cmaes
lppls_model = lppls_cmaes.LPPLSCMAES(observations=observations)
tc, m, w, a, b, c, c1, c2, O, D = lppls_model.fit(max_iteration=2500, pop_size=4)

res = lppls_model.mp_compute_nested_fits(
    workers=8,
    window_size=120, 
    smallest_window_size=30, 
    outer_increment=1, 
    inner_increment=5, 
    max_searches=25,

)   
lppls_model.plot_confidence_indicators(res)

(80_w,160)-aCMA-ES (mu_w=42.4,w_1=5%) in dimension 3 (seed=470772, Wed Jan  5 15:43:08 2022)
Iterat #Fevals   function value  axis ratio  sigma  min&max std  t[m:s]
1    160 9.527592475130895e-01 1.0e+00 1.02e+00  7e-02  4e+04 0:00.1
2    320 9.456107662596909e-01 2.0e+00 1.09e+00  8e-02  3e+04 0:00.1
3    480 9.137850385070987e-01 2.9e+00 1.09e+00  8e-02  2e+04 0:00.2
Array must not contain infs or NaNs.
/opt/anaconda3/lib/python3.9/site-packages/scipy/stats/stats.py:6685: RuntimeWarning: divide by zero encountered in 
double_scalars
relative_diff = (np.abs(f_obs_sum - f_exp_sum) /

ValueError Traceback (most recent call last) /var/folders/s2/86r_kl2925q1brkhmg1nmfr40000gn/T/ipykernel_25457/2648792023.py in 19 lppls_model = lppls_cmaes.LPPLSCMAES(observations=observations) 20 ---> 21 tc, m, w, a, b, c, c1, c2, O, D = lppls_model.fit(max_iteration=2500, pop_size=4) 22 23 # visualize the fit

/opt/anaconda3/lib/python3.9/site-packages/lppls/lppls_cmaes.py in fit(self, max_iteration, factor_sigma, pop_size, obs) 80 while not es.stop() and es.countiter <= max_iteration: 81 solutions = es.ask() ---> 82 solution = [self.fun_restricted(s, obs) for s in solutions] 83 es.tell(solutions, solution) 84 es.logger.add() # write data to disc to be plotted

/opt/anaconda3/lib/python3.9/site-packages/lppls/lppls_cmaes.py in (.0) 80 while not es.stop() and es.countiter <= max_iteration: 81 solutions = es.ask() ---> 82 solution = [self.fun_restricted(s, obs) for s in solutions] 83 es.tell(solutions, solution) 84 es.logger.add() # write data to disc to be plotted

/opt/anaconda3/lib/python3.9/site-packages/lppls/lppls_cmaes.py in funrestricted(self, x, obs) 41 42 # calculate the chi square ---> 43 error, = chisquare(f_obs=res, f_exp=obs[1, :]) 44 return error 45

/opt/anaconda3/lib/python3.9/site-packages/scipy/stats/stats.py in chisquare(f_obs, f_exp, ddof, axis) 6850 6851 """ -> 6852 return power_divergence(f_obs, f_exp=fexp, ddof=ddof, axis=axis, 6853 lambda="pearson") 6854

/opt/anaconda3/lib/python3.9/site-packages/scipy/stats/stats.py in power_divergence(f_obs, fexp, ddof, axis, lambda) 6692 f"of {rtol}, but the percent differences are:\n" 6693 f"{relative_diff}") -> 6694 raise ValueError(msg) 6695 6696 else:

ValueError: For each axis slice, the sum of the observed frequencies must agree with the sum of the expected frequencies to a relative tolerance of 1e-08, but the percent differences are: inf