def __init__(self, adjacency, decays, baseline=None, end_time=None, period_length=None, max_jumps=None, seed=None, verbose=True, force_simulation=False): if isinstance(adjacency, list): adjacency = np.array(adjacency) if isinstance(decays, list): decays = np.array(decays) n_nodes = adjacency.shape[0] n_decays = decays.shape[0] if adjacency.shape != (n_nodes, n_nodes, n_decays): raise ValueError("adjacency matrix shape should be %s but its " "shape is %s" % (str((n_nodes, n_nodes, n_decays)), str(adjacency.shape))) self.adjacency = adjacency self.decays = decays kernels = self._build_sumexp_kernels() SimuHawkes.__init__(self, kernels=kernels, baseline=baseline, end_time=end_time, period_length=period_length, max_jumps=max_jumps, seed=seed, verbose=verbose, force_simulation=force_simulation)
def __init__(self, adjacency, decays, baseline=None, end_time=None, max_jumps=None, seed=None, verbose=True, force_simulation=False): if isinstance(adjacency, list): adjacency = np.array(adjacency) if isinstance(decays, list): decays = np.array(decays) n_nodes = adjacency.shape[0] if adjacency.shape != (n_nodes, n_nodes): raise ValueError("adjacency matrix should be squared and its " "shape is %s" % str(adjacency.shape)) decays_is_number = isinstance(decays, (int, float)) if not decays_is_number and decays.shape != (n_nodes, n_nodes): raise ValueError("decays should be either a float or an array of " "shape %s but its shape is %s" % (str((n_nodes, n_nodes)), str(decays.shape))) self.adjacency = adjacency self.decays = decays kernels = self._build_exp_kernels() SimuHawkes.__init__(self, kernels=kernels, baseline=baseline, end_time=end_time, max_jumps=max_jumps, seed=seed, verbose=verbose, force_simulation=force_simulation)
def test_hawkes_set_baseline_piecewiseconstant(self): """...Test Hawkes process baseline set with time and value arrays """ baselines = [[1., 2., 1.5, 4.], [2., 1.5, 4., 1.]] hawkes = SimuHawkes(baseline=baselines, period_length=3.5, kernels=self.kernels, verbose=False) hawkes.end_time = 10 hawkes.simulate() self.assertGreater(hawkes.n_total_jumps, 1)
def test_hawkes_set_baseline_timefunction(self): """...Test Hawkes process baseline set with TimeFunction """ t_values = [0.5, 1., 2., 3.5] y_values_1 = [1., 2., 1.5, 4.] y_values_2 = [2., 1.5, 4., 1.] timefunction1 = TimeFunction((t_values, y_values_1)) timefunction2 = TimeFunction((t_values, y_values_2)) hawkes = SimuHawkes(baseline=[timefunction1, timefunction2], kernels=self.kernels, verbose=False) hawkes.end_time = 10 hawkes.simulate() self.assertGreater(hawkes.n_total_jumps, 1)
def test_simu_hawkes_no_seed(self): """...Test hawkes multi can be simulated even if no seed is given """ T1 = np.array([0, 2, 2.5], dtype=float) Y1 = np.array([0, .6, 0], dtype=float) tf = TimeFunction([T1, Y1], inter_mode=TimeFunction.InterConstRight, dt=0.1) kernel = HawkesKernelTimeFunc(tf) hawkes = SimuHawkes(baseline=[.1], end_time=100, verbose=False) hawkes.set_kernel(0, 0, kernel) multi_hawkes_1 = SimuHawkesMulti(hawkes, n_simulations=5) multi_hawkes_1.simulate() multi_hawkes_2 = SimuHawkesMulti(hawkes, n_simulations=5) multi_hawkes_2.simulate() # If no seed are given, realizations must be different self.assertNotEqual(multi_hawkes_1.timestamps[0][0][0], multi_hawkes_2.timestamps[0][0][0])
def test_simu_hawkes_multi_attrs(self): """...Test multiple simulations via SimuHawkesMulti vs. single Hawkes See that multiple simulations has same attributes as a single Hawkes simulation, but different results """ hawkes = SimuHawkes(kernels=self.kernels, baseline=self.baseline, end_time=10, verbose=False, seed=504) multi = SimuHawkesMulti(hawkes, n_threads=4, n_simulations=10) multi.simulate() hawkes.simulate() np.testing.assert_array_equal(hawkes.simulation_time, multi.simulation_time) np.testing.assert_array_equal(hawkes.n_nodes, multi.n_nodes) np.testing.assert_array_equal(hawkes.end_time, multi.end_time) np.testing.assert_array_equal(hawkes.max_jumps, multi.max_jumps) np.testing.assert_array_equal(hawkes.spectral_radius(), multi.spectral_radius) self.assertTrue( all(np.array_equal(hawkes.mean_intensity(), np.array(x)) for x in multi.mean_intensity)) self.assertFalse( np.array_equal(hawkes.n_total_jumps, multi.n_total_jumps))
def test_simu_hawkes_multi_time_func(self): """...Test that hawkes multi works correctly with HawkesKernelTimeFunc """ run_time = 100 t_values1 = np.array([0, 1, 1.5], dtype=float) y_values1 = np.array([0, .2, 0], dtype=float) tf1 = TimeFunction([t_values1, y_values1], inter_mode=TimeFunction.InterConstRight, dt=0.1) kernel1 = HawkesKernelTimeFunc(tf1) t_values2 = np.array([0, 2, 2.5], dtype=float) y_values2 = np.array([0, .6, 0], dtype=float) tf2 = TimeFunction([t_values2, y_values2], inter_mode=TimeFunction.InterConstRight, dt=0.1) kernel2 = HawkesKernelTimeFunc(tf2) baseline = np.array([0.1, 0.3]) hawkes = SimuHawkes(baseline=baseline, end_time=run_time, verbose=False, seed=2334) hawkes.set_kernel(0, 0, kernel1) hawkes.set_kernel(0, 1, kernel1) hawkes.set_kernel(1, 0, kernel2) hawkes.set_kernel(1, 1, kernel2) hawkes_multi = SimuHawkesMulti(hawkes, n_simulations=5, n_threads=4) hawkes_multi.simulate()
def test_simu_hawkes_constructor_errors(self): """...Test error messages raised by SimuHawkes constructor """ bad_baseline = np.random.rand(4) bad_kernels = self.kernels[:, 0:1] msg = '^kernels and baseline have different length. kernels has ' \ 'length 2, whereas baseline has length 4\.$' with self.assertRaisesRegex(ValueError, msg): SimuHawkes(kernels=self.kernels, baseline=bad_baseline) msg = "^n_nodes will be automatically calculated if baseline or " \ "kernels is set$" with self.assertRaisesRegex(ValueError, msg): SimuHawkes(kernels=self.kernels, n_nodes=2) with self.assertRaisesRegex(ValueError, msg): SimuHawkes(baseline=self.baseline, n_nodes=2) msg = "^n_nodes must be given if neither kernels, nor baseline are " \ "given$" with self.assertRaisesRegex(ValueError, msg): SimuHawkes() msg = '^kernels shape should be \(2, 2\) instead of \(2, 1\)$' with self.assertRaisesRegex(ValueError, msg): SimuHawkes(kernels=bad_kernels) msg = '^n_nodes must be positive but equals -1$' with self.assertRaisesRegex(ValueError, msg): SimuHawkes(n_nodes=-1)
def test_simu_hawkes_constructor(self): """...Test SimuHawkes constructor """ hawkes = SimuHawkes(kernels=self.kernels) self.assertEqual(hawkes.n_nodes, 2) np.testing.assert_array_equal(hawkes.baseline, np.zeros(2)) for i, j in product(range(2), range(2)): self.assertEqual(hawkes.kernels[i, j], self.kernels[i, j]) hawkes = SimuHawkes(baseline=self.baseline) self.assertEqual(hawkes.n_nodes, 2) np.testing.assert_array_equal(hawkes.baseline, self.baseline) for i, j in product(range(2), range(2)): self.assertEqual(hawkes.kernels[i, j].__class__, HawkesKernel0) self.assertEqual(hawkes.kernels[i, j], hawkes._kernel_0) hawkes = SimuHawkes(n_nodes=2) self.assertEqual(hawkes.n_nodes, 2) np.testing.assert_array_equal(hawkes.baseline, np.zeros(2)) for i, j in product(range(2), range(2)): self.assertEqual(hawkes.kernels[i, j].__class__, HawkesKernel0) self.assertEqual(hawkes.kernels[i, j], hawkes._kernel_0)
def test_hawkes_mean_intensity(self): """...Test that Hawkes obtained mean intensity is consistent """ hawkes = SimuHawkes(kernels=self.kernels, baseline=self.baseline, seed=308, end_time=300, verbose=False) self.assertLess(hawkes.spectral_radius(), 1) hawkes.track_intensity(0.01) hawkes.simulate() mean_intensity = hawkes.mean_intensity() for i in range(hawkes.n_nodes): self.assertAlmostEqual(np.mean(hawkes.tracked_intensity[i]), mean_intensity[i], delta=0.3)
def test_simu_hawkes_force_simulation(self): """...Test force_simulation parameter of SimuHawkes """ diverging_kernel = [[HawkesKernelExp(2, 3)]] hawkes = SimuHawkes(kernels=diverging_kernel, baseline=[1], verbose=False) hawkes.end_time = 10 msg = '^Simulation not launched as this Hawkes process is not ' \ 'stable \(spectral radius of 2\). You can use ' \ 'force_simulation parameter if you really want to simulate it$' with self.assertRaisesRegex(ValueError, msg): hawkes.simulate() msg = "^This process has already be simulated until time 0.000000$" with self.assertWarnsRegex(UserWarning, msg): hawkes.end_time = 0 hawkes.force_simulation = True hawkes.simulate()
def test_simu_hawkes_multi_seed(self): """...Test seeded Hawkes simu is re-seeded under multiple simulations """ seed = 504 hawkes = SimuHawkes(kernels=self.kernels, baseline=self.baseline, end_time=10, verbose=False) seeded_hawkes = SimuHawkes(kernels=self.kernels, baseline=self.baseline, end_time=10, verbose=False, seed=seed) multi_1 = SimuHawkesMulti(seeded_hawkes, n_threads=4, n_simulations=1) multi_2 = SimuHawkesMulti(hawkes, n_threads=4, n_simulations=1) multi_2.seed = seed hawkes.seed = seed multi_3 = SimuHawkesMulti(hawkes, n_threads=4, n_simulations=1) multi_1.simulate() multi_2.simulate() multi_3.simulate() np.testing.assert_array_equal(multi_1.n_total_jumps, multi_2.n_total_jumps) np.testing.assert_array_equal(multi_1.n_total_jumps, multi_3.n_total_jumps) timestamps_1 = multi_1.timestamps timestamps_2 = multi_2.timestamps timestamps_3 = multi_3.timestamps self.assertEqual(len(timestamps_1), len(timestamps_2)) self.assertEqual(len(timestamps_1), len(timestamps_3)) for (t1, t2, t3) in zip(timestamps_1, timestamps_2, timestamps_3): np.testing.assert_array_equal(t1[0], t2[0]) np.testing.assert_array_equal(t1[1], t2[1]) np.testing.assert_array_equal(t1[0], t3[0]) np.testing.assert_array_equal(t1[1], t3[1])
def test_hawkes_negative_intensity_fail(self): """...Test simulation with negative kernel without threshold_negative_intensity """ run_time = 40 hawkes = SimuHawkes(n_nodes=1, end_time=run_time, verbose=False, seed=1398) kernel = HawkesKernelExp(-1.3, .8) hawkes.set_kernel(0, 0, kernel) hawkes.set_baseline(0, 0.3) msg = 'Simulation stopped because intensity went negative ' \ '\(you could call ``threshold_negative_intensity`` to allow it\)' with self.assertRaisesRegex(RuntimeError, msg): hawkes.simulate()
def test_hawkes_set_kernel(self): """...Test Hawkes process kernels can be set after initialization """ hawkes = SimuHawkes(n_nodes=2) for i, j in product(range(2), range(2)): hawkes.set_kernel(i, j, self.kernels[i, j]) for i, j in product(range(2), range(2)): self.assertEqual(hawkes.kernels[i, j], self.kernels[i, j]) hawkes.set_kernel(1, 1, self.time_func_kernel) self.assertEqual(hawkes.kernels[1, 1], self.time_func_kernel)
def test_hawkes_negative_intensity(self): """...Test simulation with negative kernel """ run_time = 40 hawkes = SimuHawkes(n_nodes=1, end_time=run_time, verbose=False, seed=1398) kernel = HawkesKernelExp(-1.3, .8) hawkes.set_kernel(0, 0, kernel) hawkes.set_baseline(0, 0.3) hawkes.threshold_negative_intensity() dt = 0.1 hawkes.track_intensity(dt) hawkes.simulate() self.assertAlmostEqual(hawkes.tracked_intensity[0].min(), 0) self.assertAlmostEqual(hawkes.tracked_intensity[0].max(), hawkes.baseline[0]) self.assertGreater(hawkes.n_total_jumps, 1)
""" 1 dimensional Hawkes process simulation ======================================= """ from tick.plot import plot_point_process from tick.simulation import SimuHawkes, HawkesKernelSumExp import matplotlib.pyplot as plt run_time = 40 hawkes = SimuHawkes(n_nodes=1, end_time=run_time, verbose=False, seed=1398) kernel = HawkesKernelSumExp([.1, .2, .1], [1., 3., 7.]) hawkes.set_kernel(0, 0, kernel) hawkes.set_baseline(0, 1.) dt = 0.01 hawkes.track_intensity(dt) hawkes.simulate() timestamps = hawkes.timestamps intensity = hawkes.tracked_intensity intensity_times = hawkes.intensity_tracked_times _, ax = plt.subplots(1, 2, figsize=(16, 4)) plot_point_process(hawkes, n_points=50000, t_min=2, max_jumps=10, ax=ax[0]) plot_point_process(hawkes, n_points=50000, t_min=2, t_max=20, ax=ax[1]) plt.show()
t_values = np.array([0, 1, 1.5], dtype=float) y_values = np.array([0, .2, 0], dtype=float) tf1 = TimeFunction([t_values, y_values], inter_mode=TimeFunction.InterConstRight, dt=0.1) kernel_1 = HawkesKernelTimeFunc(tf1) t_values = np.array([0, .1, 2], dtype=float) y_values = np.array([0, .4, -0.2], dtype=float) tf2 = TimeFunction([t_values, y_values], inter_mode=TimeFunction.InterLinear, dt=0.1) kernel_2 = HawkesKernelTimeFunc(tf2) hawkes = SimuHawkes(kernels=[[kernel_1, kernel_1], [HawkesKernelExp(.07, 4), kernel_2]], baseline=[1.5, 1.5], verbose=False, seed=23983) run_time = 40 dt = 0.01 hawkes.track_intensity(dt) hawkes.end_time = run_time hawkes.simulate() fig, ax = plt.subplots(hawkes.n_nodes, 1, figsize=(14, 8)) plot_point_process(hawkes, t_max=20, ax=ax) plt.show()
# We first simulate a similar Hawkes process def g1(t): return np.cos(np.pi * t / 10) + 1.1 def g2(t): return np.cos(np.pi * (t / 10 + 1)) + 1.1 t_values = np.linspace(0, 20, 1000) u_values = [(0.007061, 0.001711), (0.005445, 0.003645), (0.003645, 0.005445), (0.001790, 0.007390)] hawkes = SimuHawkes(baseline=[1e-5, 1e-5], seed=1093, verbose=False) for i, j in itertools.product(range(2), repeat=2): u1, u2 = u_values[2 * i + j] y_values = g1(t_values) * u1 + g2(t_values) * u2 kernel = HawkesKernelTimeFunc(t_values=t_values, y_values=y_values) hawkes.set_kernel(i, j, kernel) hawkes.end_time = end_time hawkes.simulate() ticks = hawkes.timestamps # And then perform estimation with two basis kernels kernel_support = 20 n_basis = 2 em = HawkesBasisKernels(kernel_support,
from tick.inference import 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)
def _corresponding_simu(self): """Create simulation object corresponding to the obtained coefficients """ return SimuHawkes()
t_values1 = np.array([0, 1, 1.5, 2., 3.5], dtype=float) y_values1 = np.array([0, 0.2, 0, 0.1, 0.], dtype=float) tf1 = TimeFunction([t_values1, y_values1], inter_mode=TimeFunction.InterConstRight, dt=0.1) kernel1 = HawkesKernelTimeFunc(tf1) t_values2 = np.linspace(0, 4, 20) y_values2 = np.maximum(0., np.sin(t_values2) / 4) tf2 = TimeFunction([t_values2, y_values2]) kernel2 = HawkesKernelTimeFunc(tf2) baseline = np.array([0.1, 0.3]) hawkes = SimuHawkes(baseline=baseline, end_time=run_time, verbose=False, seed=2334) hawkes.set_kernel(0, 0, kernel1) hawkes.set_kernel(0, 1, HawkesKernelExp(.5, .7)) hawkes.set_kernel(1, 1, kernel2) hawkes.simulate() em = HawkesEM(4, kernel_size=16, n_threads=8, verbose=False, tol=1e-3) em.fit(hawkes.timestamps) fig = plot_hawkes_kernels(em, hawkes=hawkes, show=False) for ax in fig.axes: ax.set_ylim([0, 1])