X-DataInitiative / tick

Module for statistical learning, with a particular emphasis on time-dependent modelling
https://x-datainitiative.github.io/tick/
BSD 3-Clause "New" or "Revised" License
480 stars 105 forks source link

HawkesCumulantMatching missing R #481

Closed csaiedu closed 2 years ago

csaiedu commented 2 years ago

I am trying to retrieve the matrix of events of exogeneous origin using the attribute R, the "Estimated weight, linked to the integrals of Hawkes kernels. Use to derive adjacency and baseline defined in the attributes of the HawkesCumulantMatching" :

https://x-datainitiative.github.io/tick/modules/generated/tick.hawkes.HawkesCumulantMatching.html

But it doesn't seem to be defined

AttributeError: 'HawkesCumulantMatching' object has no attribute 'R'



import numpy as np
from tick.dataset import fetch_hawkes_bund_data
from tick.plot import plot_hawkes_kernel_norms
from tick.hawkes import  HawkesCumulantMamatrix tching

np.random.seed(7168)

n_nodes = 4

integration_support = 500

timestamps_list = fetch_hawkes_bund_data()

nphc = HawkesCumulantMatching(integration_support, cs_ratio=.01, tol=1e-10,
                              step=.5)

nphc.fit(timestamps_list)

plot_hawkes_kernel_norms(nphc,node_names=["P_u", "P_d", "T_a", "T_b"])

print(nphc.R)

reference paper https://arxiv.org/abs/1706.03411 page 3
Mbompr commented 2 years ago

Indeed it does not seem to be correctly exposed, have you tried

with nphc._tf_graph.as_default():
            R = nphc._tf_model_coeffs

inspired from the source code https://x-datainitiative.github.io/tick/_modules/tick/hawkes/inference/hawkes_cumulant_matching.html#HawkesCumulantMatching ?

csaiedu commented 2 years ago

Thanks for the quick reply. Pardon my ignorance but how do I get the value of R, I don't seem to be able to use numpy() or see the coefficients, only this

<tf.Variable 'model/R:0' shape=(4, 4) dtype=float64_ref>

Mbompr commented 2 years ago

Hum indeed, this is only a tensorflow variable. To be honest I do not remember this code well. Maybe you can retrieve the R variable by spawning a new tf session, but possibly this would have been possible only during the training. In such case you should probably edit the class to get the value of R at the end of the training, before the tf session ends.

csaiedu commented 2 years ago

Looking a the code this should be right. I edited my original post where the calculation were not correct.

R=scipy.linalg.inv(np.identity(n_nodes) - nphc.adjacency)
psi=R-np.identity(n_nodes)
trouleau commented 2 years ago

Hi! If I'm not mistaken the HawkesCumulantMatching object has an attribute solution which is a numpy array that holds the solution of the optimisation. The adjacency attribute is actually just a property that's calling solution behind it: https://github.com/X-DataInitiative/tick/blob/bbc561804eb1fdcb4c71b9e3e2d83a66e7b13a48/tick/hawkes/inference/hawkes_cumulant_matching.py#L268-L270

csaiedu commented 2 years ago

Thank you