def test_hawkes_cumulants_solve_l2(self): """...Test that hawkes cumulant reached expected value with l2 penalization """ timestamps, baseline, adjacency = Test.get_train_data(decay=3.) learner = HawkesCumulantMatching( 100., cs_ratio=0.9, max_iter=299, print_every=30, step=1e-2, solver='adam', penalty='l2', C=0.1, tol=1e-5) learner.fit(timestamps) expected_R_pred = [[0.516135, -0.484529, -0.323191], [-0.265853, 0.291741, -0.35285], [0.482819, 0.331344, 1.591535]] np.testing.assert_array_almost_equal(learner.solution, expected_R_pred) expected_baseline = [17.066997, 17.79795, -6.07811] np.testing.assert_array_almost_equal(learner.baseline, expected_baseline) expected_adjacency = [[-1.310854, -2.640152, -1.054596], [-1.004887, -2.886297, -1.065671], [0.910245, 1.610029, 0.913469]] np.testing.assert_array_almost_equal(learner.adjacency, expected_adjacency) np.testing.assert_array_almost_equal( learner.objective(learner.adjacency), 149232.94041039888) np.testing.assert_array_almost_equal( learner.objective(R=learner.solution), 149232.94041039888)
def test_hawkes_cumulants_solve_l1(self): """...Test that hawkes cumulant reached expected value with l1 penalization """ timestamps, baseline, adjacency = Test.get_train_data(decay=3.) learner = HawkesCumulantMatching( 100., cs_ratio=0.9, max_iter=299, print_every=30, step=1e-2, solver='adam', penalty='l1', C=1, tol=1e-5) learner.fit(timestamps) expected_R_pred = [[0.434197, -0.552021, -0.308883], [-0.299366, 0.272764, -0.347764], [0.48448, 0.331059, 1.591587]] np.testing.assert_array_almost_equal(learner.solution, expected_R_pred) expected_baseline = [32.788801, 29.324684, -13.275885] np.testing.assert_array_almost_equal(learner.baseline, expected_baseline) expected_adjacency = [[-2.925945, -5.54899, -1.97438], [-2.201373, -5.009153, -1.740234], [1.652958, 2.939054, 1.334677]] np.testing.assert_array_almost_equal(learner.adjacency, expected_adjacency) np.testing.assert_array_almost_equal( learner.objective(learner.adjacency), 149061.5590630687) np.testing.assert_array_almost_equal( learner.objective(R=learner.solution), 149061.5590630687)
def run_cumulants(data): def objective(h): model = HawkesCumulantMatching(h, max_iter=300) model.fit(data) return model.objective(model.adjacency) best = fmin(objective, space=hp.uniform('h', 1, 100), algo=tpe.suggest, max_evals=20) half_width = best['h'] model = HawkesCumulantMatching(half_width, max_iter=300, verbose=True) model.fit(data) return model
def test_hawkes_cumulants_solve(self): """...Test that hawkes cumulant reached expected value """ timestamps, baseline, adjacency = Test.get_train_data(decay=3.) learner = HawkesCumulantMatching(100., cs_ratio=0.9, max_iter=299, print_every=30, step=1e-2, solver='adam', C=1e-3, tol=1e-5) learner.fit(timestamps) expected_R_pred = [[0.423305, -0.559607, -0.307212], [-0.30411, 0.27066, -0.347162], [0.484648, 0.331057, 1.591584]] np.testing.assert_array_almost_equal(learner.solution, expected_R_pred) expected_baseline = [36.808583, 32.304106, -15.123118] np.testing.assert_array_almost_equal(learner.baseline, expected_baseline) expected_adjacency = [[-3.34742247, -6.28527387, -2.21012092], [-2.51556256, -5.55341413, -1.91501755], [1.84706793, 3.2770494, 1.44302449]] np.testing.assert_array_almost_equal(learner.adjacency, expected_adjacency) np.testing.assert_array_almost_equal( learner.objective(learner.adjacency), 149029.4540306161) np.testing.assert_array_almost_equal( learner.objective(R=learner.solution), 149029.4540306161) # Ensure learner can be fit again timestamps_2, baseline, adjacency = Test.get_train_data(decay=2.) learner.step = 1e-1 learner.penalty = 'l2' learner.fit(timestamps_2) expected_adjacency_2 = [[-0.021966, -0.178811, -0.107636], [0.775206, 0.384494, 0.613925], [0.800584, 0.581281, 0.60177]] np.testing.assert_array_almost_equal(learner.adjacency, expected_adjacency_2) learner_2 = HawkesCumulantMatching( 100., cs_ratio=0.9, max_iter=299, print_every=30, step=1e-1, solver='adam', penalty='l2', C=1e-3, tol=1e-5) learner_2.fit(timestamps_2) np.testing.assert_array_almost_equal(learner.adjacency, expected_adjacency_2) # Check cumulants are not computed again learner_2.step = 1e-2 learner_2.fit(timestamps_2)
def objective(h): model = HawkesCumulantMatching(h, max_iter=300) model.fit(data) return model.objective(model.adjacency)
np.random.seed(7168) n_nodes = 3 baselines = 0.3 * np.ones(n_nodes) decays = 0.5 + np.random.rand(n_nodes, n_nodes) adjacency = np.array([ [1, 1, -0.5], [0, 1, 0], [0, 0, 2], ], dtype=float) adjacency /= 4 end_time = 1e5 integration_support = 5 n_realizations = 5 simu_hawkes = SimuHawkesExpKernels( baseline=baselines, adjacency=adjacency, decays=decays, end_time=end_time, verbose=False, seed=7168) simu_hawkes.threshold_negative_intensity(True) multi = SimuHawkesMulti(simu_hawkes, n_simulations=n_realizations, n_threads=-1) multi.simulate() nphc = HawkesCumulantMatching(integration_support, cs_ratio=.15, tol=1e-10, step=0.3) nphc.fit(multi.timestamps) plot_hawkes_kernel_norms(nphc)