def test_HawkesKernel0_pickle(self): """...Test pickling ability of HawkesKernel0 """ obj = HawkesKernel0() pickled = pickle.loads(pickle.dumps(obj)) self.assertTrue(str(obj) == str(pickled)) self.assertEqual(obj.get_support(), pickled.get_support()) np.testing.assert_array_equal(obj.get_values(self.random_times), obj.get_values(self.random_times))
def setUp(self): np.random.seed(28374) self.kernels = np.array([[HawkesKernel0(), HawkesKernelExp(0.1, 3)], [ HawkesKernelPowerLaw(0.2, 4, 2), HawkesKernelSumExp([0.1, 0.4], [3, 4]) ]]) self.baseline = np.random.rand(2)
def simulate_hawkes(self, model_name): self.model_name = model_name def y_func_pos(t_values): y_values = 0.02 * np.exp(-t_values) return y_values def y_func_neg(t_values): y_values = -0.1 * np.exp(-t_values) return y_values if model_name == 'hawkes_neg': y_func = y_func_neg elif model_name == 'hawkes_pos': y_func = y_func_pos t_values = np.linspace(0, 101, 100) y_values = y_func(t_values) tf = TimeFunction([t_values, y_values], inter_mode=TimeFunction.InterLinear, dt=0.1) tf_kernel = HawkesKernelTimeFunc(tf) N_enodes = self.G_e2n.number_of_nodes() # regarded as 'N_enodes' types base_int = 0.2 baselines = [base_int for i in range(N_enodes)] kernels = [[] for i in range(N_enodes)] for i in range(N_enodes): for j in range(N_enodes): if i == j: # kernels[i].append(HawkesKernel0()) kernels[i].append(HawkesKernelExp(.1, 4)) # self influence else: if self.G_e2n.has_edge(self.idx_elabel_map[i], self.idx_elabel_map[j]): kernels[i].append(tf_kernel) else: kernels[i].append(HawkesKernel0()) hawkes = SimuHawkes(kernels=kernels, baseline=baselines, verbose=False, seed=self.seed) hawkes.threshold_negative_intensity(allow=True) run_time = 100 hawkes.end_time = run_time hawkes.simulate() timestamps = hawkes.timestamps self.save(timestamps, self.model_name)
def setUp(self): np.random.seed(28374) self.kernels = np.array([ [HawkesKernel0(), HawkesKernelExp(0.1, 3)], [HawkesKernelPowerLaw(0.2, 4, 2), HawkesKernelSumExp([0.1, 0.4], [3, 4])] ]) t_values = np.linspace(0, 10, 10) y_values = np.maximum(0.5 + np.sin(t_values), 0) self.time_func_kernel = HawkesKernelTimeFunc(t_values=t_values, y_values=y_values) self.baseline = np.random.rand(2)
import numpy as np import matplotlib.pyplot as plt from tick.hawkes import HawkesKernel0, HawkesKernelExp, HawkesKernelPowerLaw, \ HawkesKernelTimeFunc kernel_0 = HawkesKernel0() kernel_exp = HawkesKernelExp(.7, 1.3) kernel_pl = HawkesKernelPowerLaw(.1, .2, 0.7) t_values = np.array([0, 1, 1.5, 1.8, 2.7]) y_values = np.array([0, .6, .34, .2, .1]) kernel_tf = HawkesKernelTimeFunc(t_values=t_values, y_values=y_values) kernels = [[kernel_0, kernel_exp], [kernel_pl, kernel_tf]] fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 4)) t_values = np.linspace(0, 3, 100) for i in range(2): for j in range(2): ax[i, j].plot(t_values, kernels[i][j].get_values(t_values), label=kernels[i][j]) ax[i, j].legend() plt.show()
def setUp(self): self.hawkes_kernel_0 = HawkesKernel0()
HawkesKernelTimeFunc, HawkesKernelPowerLaw, HawkesKernel0, HawkesSumGaussians) end_time = 1000 n_nodes = 2 n_realizations = 10 n_gaussians = 5 timestamps_list = [] kernel_timefunction = HawkesKernelTimeFunc( t_values=np.array([0., .7, 2.5, 3., 4.]), y_values=np.array([.3, .03, .03, .2, 0.])) kernels = [[HawkesKernelExp(.2, 2.), HawkesKernelPowerLaw(.2, .5, 1.3)], [HawkesKernel0(), kernel_timefunction]] hawkes = SimuHawkes(baseline=[.5, .2], kernels=kernels, end_time=end_time, verbose=False, seed=1039) multi = SimuHawkesMulti(hawkes, n_simulations=n_realizations) multi.simulate() learner = HawkesSumGaussians(n_gaussians, max_iter=10) learner.fit(multi.timestamps) plot_hawkes_kernels(learner, hawkes=hawkes, support=4)