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_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 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 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)
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()
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, n_basis=n_basis, kernel_size=kernel_size, C=C, n_threads=4, max_iter=max_iter, verbose=False, ode_tol=1e-5)
support = 2000 hawkes = SimuHawkes(kernels=[[ HawkesKernelPowerLaw(multiplier[0], cutoff, exponent, support), HawkesKernelPowerLaw(multiplier[1], cutoff, exponent, support) ], [ HawkesKernelPowerLaw(multiplier[2], cutoff, exponent, support), HawkesKernelPowerLaw(multiplier[3], cutoff, exponent, support) ]], baseline=[0.05, 0.05], seed=382, verbose=False) hawkes.end_time = 50000 hawkes.simulate() e = HawkesConditionalLaw(claw_method="log", delta_lag=0.1, min_lag=0.002, max_lag=100, quad_method="log", n_quad=50, min_support=0.002, max_support=support, n_threads=-1) e.incremental_fit(hawkes.timestamps) e.compute()