omitakahiro / Hawkes

a python package for simulation and inference of Hawkes processes.
MIT License
58 stars 18 forks source link

Overflow encountered in multiply #6

Open BEbillionaireUSD opened 2 years ago

BEbillionaireUSD commented 2 years ago

Hi, I met a troublesome issue. My event sequence is relatively long (with the length of 6000 that is split into 169 sub-intervals). The program generates a warning:

/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:643: RuntimeWarning: overflow encountered in multiply
  Int = Int + alpha[i] * ( np.exp( -beta[i] * x1 ) - np.exp( -beta[i] * x2 ) )
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:655: RuntimeWarning: overflow encountered in multiply
  dInt[('alpha',i)] = np.exp( -beta[i] * x1 ) - np.exp( -beta[i] * x2 )
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:656: RuntimeWarning: overflow encountered in multiply
  dInt[('beta',i) ] = alpha[i] * ( - x1 * np.exp( -beta[i] * x1 ) + x2 * np.exp( -beta[i] * x2 ) )
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:688: RuntimeWarning: overflow encountered in multiply
  r = np.exp(-beta*step)
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:699: RuntimeWarning: overflow encountered in double_scalars
  dl = { ('alpha',i):g[i]/alpha[i] for i in range(num_exp) }
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:709: RuntimeWarning: overflow encountered in add
  g = g + alpha*beta
/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py:690: RuntimeWarning: invalid value encountered in multiply
  g = g*r

It seems the resulting parameters are too large. Could you please give me some suggestions with such a situation? My codes:

seq = [0, 0.001, 1, ...., 844.001, 844.002]
model = hk.estimator().set_kernel('exp').set_baseline('plinear')
model.fit(seq, (0, 844.1))

I would be really grateful if you could give me some help. Thanks in advance!

BTW, I also met another issue:

 File "/opt/anaconda3/envs/env-37/lib/python3.9/site-packages/Hawkes/model.py", line 849, in set_data
    dl,dInt = preprocess_data_nonpara_cython(Data['T'],self.bin_edge,itv[1])

when I set the baseline as 'plinear' and the kernel as 'nonpara'.

omitakahiro commented 2 years ago

Could you share your sample data for which this library didn't work? It would help me resolving the issue.

BEbillionaireUSD commented 2 years ago

Sure! It's so nice of you to reply me so quickly. tmp.txt

The sequence is in the seq.txt file and the observation starts at 0 and ends at 844.

omitakahiro commented 2 years ago

I think it is difficult to apply a Hawkes process to your data, because, in your dataset, all the data points are integer and the same value is repeated several times. I feel It would be better to employ discrete-time models for your case.

samiltur commented 1 year ago

Hi, I faced with similar problem.

d:\Tez\Hawkes\Hawkes\model.py:692: RuntimeWarning: overflow encountered in multiply
  Int = ( g*(1-r)/beta ).sum()
d:\Tez\Hawkes\Hawkes\model.py:693: RuntimeWarning: overflow encountered in multiply
  g = g*r
d:\Tez\Hawkes\Hawkes\model.py:701: RuntimeWarning: overflow encountered in multiply
  g_b = g_b*r - g*step
d:\Tez\Hawkes\Hawkes\model.py:701: RuntimeWarning: invalid value encountered in subtract
  g_b = g_b*r - g*step
d:\Tez\Hawkes\Hawkes\model.py:183: RuntimeWarning: invalid value encountered in divide
  G = { key: (dl[key]/l).sum(axis=-1) - dInt[key] for key in dl }

My data is returns.txt

and my code is

itv = [-1,10]
h2 = hk.estimator().set_kernel('exp').set_baseline('const')
h2.fit(returns,itv,opt=['print'])

When I enabled print, the output is:

...
32
{'mu': 7.4174034730288625, 'alpha': 0.9177978222695085, 'beta': 601.8450378720826}
L = 49262.618, norm(G) = 4.774537e+04

33
{'mu': 7.415109600152086, 'alpha': 0.9178830564619231, 'beta': 735.0951892419736}
L = 59826.811, norm(G) = 5.824598e+04

34
{'mu': 7.413234043852897, 'alpha': 0.9179523864372038, 'beta': 897.8472916504186}
L = 72715.710, norm(G) = 7.106981e+04

35
{'mu': 7.411699239771285, 'alpha': 0.9180089541532814, 'beta': 1096.6331584284596}
L = 88443.675, norm(G) = 8.673119e+04

36
{'mu': 7.410442540456336, 'alpha': 0.918055212475346, 'beta': 1339.4307643944192}
L = 107638.953, norm(G) = 1.058584e+05

d:\Tez\Hawkes\Hawkes\model.py:692: RuntimeWarning: overflow encountered in multiply
  Int = ( g*(1-r)/beta ).sum()
d:\Tez\Hawkes\Hawkes\model.py:693: RuntimeWarning: overflow encountered in multiply
  g = g*r
d:\Tez\Hawkes\Hawkes\model.py:701: RuntimeWarning: overflow encountered in multiply
  g_b = g_b*r - g*step
d:\Tez\Hawkes\Hawkes\model.py:701: RuntimeWarning: invalid value encountered in subtract
  g_b = g_b*r - g*step
d:\Tez\Hawkes\Hawkes\model.py:183: RuntimeWarning: invalid value encountered in divide
  G = { key: (dl[key]/l).sum(axis=-1) - dInt[key] for key in dl }
37
{'mu': 7.409413342693876, 'alpha': 0.9180930751605922, 'beta': 1635.9844299959282}
L = inf, norm(G) = nan

38
{'mu': nan, 'alpha': nan, 'beta': nan}
L = nan, norm(G) = nan

39
{'mu': nan, 'alpha': nan, 'beta': nan}
L = nan, norm(G) = nan

40
{'mu': nan, 'alpha': nan, 'beta': nan}
L = nan, norm(G) = nan

41
{'mu': nan, 'alpha': nan, 'beta': nan}
L = nan, norm(G) = nan
...

Could you help me, please. Thanks.