def test_hawkes_set_timestamps(self): """...Test simulation after some timestamps have been set manually """ decay = 0.5 baseline = [0.2, 0.4] adjacency = [[0.2, 0.1], [0, 0.3]] hawkes = SimuHawkesExpKernels( baseline=baseline, decays=decay, adjacency=adjacency, verbose=False, seed=1393) # timestamps generated by a hawkes process with the same # characteristics simulated up to time 10 original_timestamps = [ np.array([7.096244, 9.389927]), np.array([0.436199, 0.659153, 2.622352, 3.095093, 7.189881, 8.068153, 9.240032]), ] hawkes.track_intensity(1) hawkes.set_timestamps(original_timestamps, 10) hawkes.end_time = 100 hawkes.simulate() # Intensity up to time 10 is known first_intensities = hawkes.tracked_intensity[0][:10] np.testing.assert_array_almost_equal( first_intensities, [0.2, 0.2447256, 0.27988282, 0.24845138, 0.23549475, 0.27078386, 0.26749709, 0.27473586, 0.24532959, 0.22749379] ) # Ensure other jumps have occured afterwards self.assertGreater(hawkes.n_total_jumps, sum(map(len, original_timestamps)))
def simulate(self): n_nodes = 1 baseline = [0.1] adjacency = [[0.1]] end_time = 10000 # max_jumps=1000; a_sim = SimuHawkesExpKernels(adjacency, decays, baseline=baseline, end_time=end_time, verbose=True) a_sim.track_intensity(0.01) a_sim.simulate() # print(a_sim.timestamps) # print('Tracked intensity: ', a_sim.tracked_intensity) with open('sample_timestamps.txt', 'w') as f: f.write(str(list(a_sim.timestamps[0])))
period_length = 100 t_values = np.linspace(0, period_length) y_values = 0.2 * np.maximum(np.sin(t_values * (2 * np.pi) / period_length), 0.2) baselines = np.array( [TimeFunction((t_values, y_values), border_type=TimeFunction.Cyclic)]) decay = 0.1 adjacency = np.array([[0.5]]) hawkes = SimuHawkesExpKernels(adjacency, decay, baseline=baselines, seed=2093, verbose=False) hawkes.track_intensity(0.1) hawkes.end_time = 6 * period_length hawkes.simulate() fig, ax = plt.subplots(1, 1, figsize=(10, 4)) plot_point_process(hawkes, ax=ax) t_values = np.linspace(0, hawkes.end_time, 1000) ax.plot(t_values, hawkes.get_baseline_values(0, t_values), label='baseline', ls='--', lw=1) ax.set_ylabel("$\lambda(t)$", fontsize=18) ax.legend()
class Test(unittest.TestCase): def setUp(self): np.random.seed(23982) self.n_nodes = 3 self.baseline = np.random.rand(self.n_nodes) self.adjacency = np.random.rand(self.n_nodes, self.n_nodes) / 2 self.decays = np.random.rand(self.n_nodes, self.n_nodes) self.adjacency[0, 0] = 0 self.adjacency[-1, -1] = 0 self.hawkes = SimuHawkesExpKernels(self.adjacency, self.decays, baseline=self.baseline, seed=203, verbose=False) def test_hawkes_exponential_kernels(self): """...Test creation of a Hawkes Process with exponential kernels """ kernel_0 = None for i, j in product(range(self.n_nodes), range(self.n_nodes)): kernel_ij = self.hawkes.kernels[i, j] if self.adjacency[i, j] == 0: self.assertEqual(kernel_ij.__class__, HawkesKernel0) # We check that all 0 adjacency share the same kernel 0 # This might save lots of memory with very large, # very sparse adjacency matrices if kernel_0 is None: kernel_0 = kernel_ij else: self.assertEqual(kernel_0, kernel_ij) else: self.assertEqual(kernel_ij.__class__, HawkesKernelExp) self.assertEqual(kernel_ij.decay, self.decays[i, j]) self.assertEqual(kernel_ij.intensity, self.adjacency[i, j]) np.testing.assert_array_equal(self.baseline, self.hawkes.baseline) def test_hawkes_spectral_radius_exp_kernel(self): """...Hawkes Process spectral radius and adjust spectral radius methods """ self.assertAlmostEqual(self.hawkes.spectral_radius(), 0.6645446549735008) self.hawkes.adjust_spectral_radius(0.6) self.assertAlmostEqual(self.hawkes.spectral_radius(), 0.6) def test_hawkes_mean_intensity(self): """...Test that Hawkes obtained mean intensity is consistent """ self.assertLess(self.hawkes.spectral_radius(), 1) self.hawkes.end_time = 1000 self.hawkes.track_intensity(0.01) self.hawkes.simulate() mean_intensity = self.hawkes.mean_intensity() for i in range(self.hawkes.n_nodes): self.assertAlmostEqual(np.mean(self.hawkes.tracked_intensity[i]), mean_intensity[i], delta=0.1)