def _sim_single_exposures(self): if not self.sparse: raise ValueError( "'single_exposure' exposures can only be simulated" " as sparse feature matrices") if self.hawkes_exp_kernels is None: np.random.seed(self.seed) decays = .002 * np.ones((self.n_features, self.n_features)) baseline = 4 * np.random.random(self.n_features) / self.n_intervals mult = np.random.random(self.n_features) adjacency = mult * np.eye(self.n_features) if self.n_correlations: comb = list(permutations(range(self.n_features), 2)) if len(comb) > 1: idx = itemgetter( *np.random.choice(range(len(comb)), size=self.n_correlations, replace=False)) comb = idx(comb) for i, j in comb: adjacency[i, j] = np.random.random(1) self._set( 'hawkes_exp_kernels', SimuHawkesExpKernels(adjacency=adjacency, decays=decays, baseline=baseline, verbose=False, seed=self.seed)) self.hawkes_exp_kernels.adjust_spectral_radius( .1) # TODO later: allow to change this parameter hawkes = SimuHawkesMulti(self.hawkes_exp_kernels, n_simulations=self.n_cases) run_time = self.n_intervals hawkes.end_time = [1 * run_time for _ in range(self.n_cases)] dt = 1 self.hawkes_exp_kernels.track_intensity(dt) hawkes.simulate() self.hawkes_obj = hawkes features = [[ np.min(np.floor(f)) if len(f) > 0 else -1 for f in patient_events ] for patient_events in hawkes.timestamps] features = [ self.to_coo(feat, (run_time, self.n_features)) for feat in features ] # Make sure patients have at least one exposure? exposures_filter = itemgetter( *[i for i, f in enumerate(features) if f.sum() > 0]) features = exposures_filter(features) n_samples = len(features) return features, n_samples
def SimExp(baseline, adjacency, decays, num_clusters, data): hawkes = SimuHawkesExpKernels(adjacency=adjacency, decays=decays, baseline=baseline, verbose=False) #dt = 0.001 #millisecond granularity #hawkes.track_intensity(dt) # turning this on will eat up memory #need to compute and draw from the cluster length distrbution from the original data cluster_lengths = ComputeClusterLengths(data) multi = SimuHawkesMulti(hawkes, n_simulations=num_clusters) multi.end_time = np.random.choice(cluster_lengths, size=num_clusters, replace=True) multi.simulate() sim_inner_timestamps = multi.timestamps l = 0 for realisation in sim_inner_timestamps: for series in realisation: l += len(series) print(f"Simulated {l} points") return sim_inner_timestamps
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_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 simulate_sparse_realization(self): """Simulate realization in which some nodes are sometimes empty """ baseline = np.array([0.3, 0.001]) adjacency = np.array([[0.5, 0.8], [0., 1.3]]) sim = SimuHawkesExpKernels(adjacency=adjacency, decays=self.decay, baseline=baseline, verbose=False, seed=13487, end_time=500) sim.adjust_spectral_radius(0.8) multi = SimuHawkesMulti(sim, n_simulations=100) adjacency = sim.adjacency multi.simulate() # Check that some but not all realizations are empty self.assertGreater(max(map(lambda r: len(r[1]), multi.timestamps)), 1) self.assertEqual(min(map(lambda r: len(r[1]), multi.timestamps)), 0) return baseline, adjacency, multi.timestamps
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])
# %% [markdown] # ### SimuHawkesMulti # %% Simulate with Multi hawkes_m1 = SimuHawkes(n_nodes=1, end_time=10000) hawkes_m1.set_baseline(0, 1.) hawkes_m2 = SimuHawkes(n_nodes=1, end_time=10000) hawkes_m2.set_baseline(0, 1.) hawkes_m1.set_kernel(0, 0, kernel_1) hawkes_m2.set_kernel(0, 0, kernel_2) # %% Run Multi multi_1 = SimuHawkesMulti(hawkes_m1, n_simulations=100) multi_1.simulate() multi_2 = SimuHawkesMulti(hawkes_m2, n_simulations=100) multi_2.simulate() # %% Get attributes from Multi multi_1_timestamps = multi_1.timestamps # multi_1_mean_intensity = multi_1.mean_intensity multi_2_timestamps = multi_2.timestamps # multi_2_mean_intensity = multi_2.mean_intensity # %% [markdown] # ## Learning
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)
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])
if __name__ == '__main__': end_time = 50000 n_realizations = 1 decay = 3 baseline =np.ones(6) * .03 adjacency = np.zeros((6, 6)) adjacency[2:, 2:] = np.ones((4, 4)) * 0.1 adjacency[:3, :3] = np.ones((3, 3)) * 0.15 hawkes_exp_kernels = SimuHawkesExpKernels(adjacency=adjacency, decays=decay, baseline=baseline, end_time=end_time, verbose=False, seed=1039) multi = SimuHawkesMulti(hawkes_exp_kernels, n_simulations=n_realizations) multi.end_time = [(i + 1) / n_realizations * end_time for i in range(n_realizations)] multi.simulate() type_seq = [] time_seq = [] for i, d in enumerate(multi.timestamps[0]): for j in d: type_seq.append(i) time_seq.append(j) type_seq = np.array(type_seq) time_seq = np.array(time_seq) type_seq = type_seq[np.argsort(time_seq)] time_seq = time_seq[np.argsort(time_seq)]
baselines = [[0.3, 0.5, 0.6, 0.4, 0.2, 0], [0.8, 0.5, 0.2, 0.3, 0.3, 0.4]] n_baselines = len(baselines[0]) decays = [.5, 2., 6.] adjacency = [[[0, .1, .4], [.2, 0., .2]], [[0, 0, 0], [.6, .3, 0]]] # simulation hawkes = SimuHawkesSumExpKernels(baseline=baselines, period_length=period_length, decays=decays, adjacency=adjacency, seed=2093, verbose=False) hawkes.end_time = 1000 hawkes.adjust_spectral_radius(0.5) multi = SimuHawkesMulti(hawkes, n_simulations=4) multi.simulate() # estimation learner = HawkesSumExpKern(decays=decays, n_baselines=n_baselines, period_length=period_length) learner.fit(multi.timestamps) # plot fig = plot_hawkes_baseline_and_kernels(learner, hawkes=hawkes, show=False) fig.tight_layout() plt.show()
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)