X-DataInitiative/tick

HawkesEM error when one of the event arrays is empty

Closed this issue · 2 comments

Hi!

I have an error while fitting HawkesEM on the data set possibly has an empty sequence among 2 numpy arrays (D=2 for this data set).
I saw from the previous question that this issue can be resolved by manual install, but the same error is occurring after removing & manual install of tick from source following the instruction.
When I used HawkesSumExpKern, it worked well.

em = HawkesEM(4)
em.fit(data_set)
fig = plot_hawkes_kernels(em, show=False)

for ax in fig.axes:
    ax.set_ylim([0, 1])
plt.show()

The following is the error message.

  File "/Users/---/hawkes_sumexp.py", line 28, in <module>
    em.fit(data_set)
  File "/Users/---/lib/python3.6/site-packages/tick/hawkes/inference/hawkes_em.py", line 143, in fit
    self.solve(baseline_start=baseline_start, kernel_start=kernel_start)
  File "/Users/---/lib/python3.6/site-packages/tick/solver/base/solver.py", line 109, in solve
    self._solve(*args, **kwargs)
  File "/Users/---/lib/python3.6/site-packages/tick/hawkes/inference/hawkes_em.py", line 184, in _solve
    self._learner.solve(self.baseline, self._flat_kernels)
  File "/Users/---/lib/python3.6/site-packages/tick/hawkes/inference/build/hawkes_inference.py", line 811, in solve
    return _hawkes_inference.HawkesEM_solve(self, mu, kernels)

IndexError: lib/include/tick/array/array.h:106 in T &Array<double>::operator[](const ulong) [T = double]: Bad index: 0 should be in [0, 0]
C++ extension backtrace: 
0   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18abb20b _ZN4tick12TemporaryLogINS_11LogExitNoopEE16insert_backtraceEv + 43
1   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18abb125 _ZN5ArrayIdEixEy + 501
2   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18b3f5ca _ZN8HawkesEM22compute_intensities_urEyRK5ArrayIdER7Array2dId8RowMajorENSt3__18functionIFvdEEEb + 2298
3   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18b3e51e _ZN8HawkesEM8solve_urEyRK5ArrayIdER7Array2dId8RowMajorE + 814
4   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18b3de3f _Z12parallel_runIM8HawkesEMFvyRK5ArrayIdER7Array2dId8RowMajorEEPS0_JRS2_S8_EEvjyT_T0_DpOT1_ + 111
5   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18b3d89e _ZN8HawkesEM5solveER5ArrayIdER7Array2dId8RowMajorE + 254
6   _hawkes_inference.cpython-36m-darwin.so 0x0000001a18a8eb89 _ZL20_wrap_HawkesEM_solveP7_objectS0_ + 521
7   python                              0x0000000101a0af68 _PyCFunction_FastCallDict + 200
8   python                              0x0000000101ae038f call_function + 143
9   python                              0x0000000101addef2 _PyEval_EvalFrameDefault + 46818

Process finished with exit code 1

Thanks for the library!

Hi,

Indeed, the HawkesEM algorithm was probably not meant to handle empty sequences of events.
I think the error comes from line https://github.com/X-DataInitiative/tick/blob/master/lib/cpp/hawkes/inference/hawkes_em.cpp#L204 that could probably be fixed by changing

if (t_i < timestamps_v[last_indices[node_v]]) continue;

to

 if (timestamps_v.size() == 0 || t_i < timestamps_v[last_indices[node_v]]) continue;

Since you have just manually installed tick, you can probably do this change yourself and test it very easily. If it works, we would be very pleased if you can submit a PR with the code !