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_HawkesKernelTimeFunc_time_function(self): """...Test HawkesKernelTimeFunc time_function parameter """ t_values = np.linspace(-1, 100, 1000) np.testing.assert_array_equal( self.hawkes_kernel_time_func.time_function.value(t_values), self.time_function.value(t_values)) self.hawkes_kernel_time_func = \ HawkesKernelTimeFunc(t_values=self.t_values, y_values=self.y_values) np.testing.assert_array_equal( self.hawkes_kernel_time_func.time_function.value(t_values), self.time_function.value(t_values))
def TrainInnerClusterEM(clusters, k_time=1, k_size=100): #merge all the clusters, the learner seems to only be able to fit a single long datastream num_clusters = len(clusters) data = ConcatClusters(clusters, 0) #kernel size is the granularity #kernel support is something... (is it the size of each step?) em_learner = HawkesEM(kernel_support=k_time, kernel_size=k_size, n_threads=8, verbose=True, tol=1e-5, max_iter=1000) em_learner.fit(data) """#train the em learner on each cluster cluster_num = 0 for cluster in clusters: if (cluster_num % 10 == 0): #print out training progress s = f"Cluster: {cluster_num}/{num_clusters}" print(f"\r{' '*l}\r", end='') print(f"Cluster: {cluster_num}/{num_clusters}", end='', flush=True) l = len(s) print(em_learner.baseline) print(em_learner.kernel) print("==========") if (cluster_num == 0): em_learner.fit(cluster) else: em_learner.fit(cluster, baseline_start=em_learner.baseline, kernel_start=em_learner.kernel) cluster_num += 1""" #maybe add variation in kernel sie later? #use em_learner.score() to evaluate goodness print(f"\nEM Score: {em_learner.score()}") fig = plot_hawkes_kernels(em_learner) #TODO, remove this? t = np.linspace(0, k_time, endpoint=False, num=k_size) m = [] for i in range(2): for j in range(2): m.append(max(em_learner.kernel[i][j])) #normalise to make a proper hawkes process spectral_radius = max(m) if (spectral_radius < 1): spectral_radius = 1 #create a 2x2 array of time func kernels k = [[], []] for i in range(2): for j in range(2): k[i].append( HawkesKernelTimeFunc(t_values=t, y_values=em_learner.kernel[i][j] / np.linalg.norm(em_learner.kernel[i][j]))) #return k, em_learner.baseline #the kernel, baseline return em_learner
def test_HawkesKernelTimeFunc_pickle(self): """...Test pickling ability of HawkesKernelTimeFunc """ size = 10 y_values = np.random.rand(size) t_values = np.arange(size, dtype=float) time_function = TimeFunction([t_values, y_values]) obj = HawkesKernelTimeFunc(time_function) pickled = pickle.loads(pickle.dumps(obj)) self.assertEqual(obj.time_function.value(1), pickled.time_function.value(1)) self.assertEqual(obj.time_function.value(2), pickled.time_function.value(2)) self.assertEqual(obj.time_function.value(1.5), pickled.time_function.value(1.5)) self.assertEqual(obj.time_function.value(0.75), pickled.time_function.value(0.75)) np.testing.assert_array_equal(obj.get_values(self.random_times), obj.get_values(self.random_times))
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 setUp(self): np.random.seed(28374) self.kernels = np.array([ [HawkesKernel0(), HawkesKernelExp(0.1, 3)], [HawkesKernelPowerLaw(0.2, 4, 2), HawkesKernelSumExp([0.1, 0.4], [3, 4])] ]) t_values = np.linspace(0, 10, 10) y_values = np.maximum(0.5 + np.sin(t_values), 0) self.time_func_kernel = HawkesKernelTimeFunc(t_values=t_values, y_values=y_values) self.baseline = np.random.rand(2)
def goodness_of_fit_nonpar(nplearner, arrivals, timestep, method): # setup simulation object hp = SimuHawkes(baseline=nplearner.baseline) # set kernels support = np.arange(0, np.max(nplearner.get_kernel_supports()), timestep) for i, j in itertools.product(range(nplearner.n_nodes), repeat=2): print('Kernel {} set.'.format([i, j])) y = nplearner.get_kernel_values(i, j, support) kernel = HawkesKernelTimeFunc(t_values=support, y_values=y) hp.set_kernel(i, j, kernel) hp.track_intensity(timestep) hp.set_timestamps(timestamps=arrivals) dimension = hp.n_nodes intensities = hp.tracked_intensity x = hp.intensity_tracked_times residuals = [resid(x, intensities, arrivals, dim, method) for dim in range(dimension)] return residuals
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 MakeTimeKernel(self, kernel): #check the spectral radius m = [] for process in kernel: for intensity in process: m.append(max(intensity)) self.spectral_radius = max(m) if (self.spectral_radius > 1): #oh no self.time_kernel = None return #create a time kernel for simulation t = np.linspace(0, self.k_time, endpoint=False, num=self.k_size) k = [] for process in kernel: for intensity in process: k.append(HawkesKernelTimeFunc(t_values=t, y_values=intensity)) k = np.reshape(k, kernel.shape[:2]) self.time_kernel = k
class Test(unittest.TestCase): def setUp(self): size = 10 self.y_values = np.random.rand(size) self.t_values = np.arange(size, dtype=float) self.time_function = TimeFunction([self.t_values, self.y_values]) self.hawkes_kernel_time_func = HawkesKernelTimeFunc(self.time_function) def test_HawkesKernelTimeFunc_time_function(self): """...Test HawkesKernelTimeFunc time_function parameter """ t_values = np.linspace(-1, 100, 1000) np.testing.assert_array_equal( self.hawkes_kernel_time_func.time_function.value(t_values), self.time_function.value(t_values)) self.hawkes_kernel_time_func = \ HawkesKernelTimeFunc(t_values=self.t_values, y_values=self.y_values) np.testing.assert_array_equal( self.hawkes_kernel_time_func.time_function.value(t_values), self.time_function.value(t_values)) def test_HawkesKernelTimeFunc_str(self): """...Test HawkesKernelTimeFunc string representation """ self.assertEqual(str(self.hawkes_kernel_time_func), "KernelTimeFunc") def test_HawkesKernelTimeFunc_repr(self): """...Test HawkesKernelTimeFunc string in list representation """ self.assertEqual(str([self.hawkes_kernel_time_func]), "[KernelTimeFunc]") def test_HawkesKernelTimeFunc_strtex(self): """...Test HawkesKernelTimeFunc string representation """ self.assertEqual(self.hawkes_kernel_time_func.__strtex__(), "TimeFunc Kernel")
plot_timefunction(tf_1) # %% [markdown] # ![Tick_Figure_1_0_TF.png](attachment:7ec4d7d7-df41-4fc7-9238-6aa7396b1c67.png) # %% Plot tf_2 plot_timefunction(tf_2) # %% [markdown] # ![Tick_Figure_2_t0_TF.png](attachment:0d61b956-0013-41ad-877d-698cc574ff6d.png) # %% [markdown] # ## Defining Non-parametric Kernels # %% Define kernel_1 kernel_1 = HawkesKernelTimeFunc(tf_1) # %% Define kernel_2 kernel_2 = HawkesKernelTimeFunc(tf_2) # %% [markdown] # ## Defining Parametric Kernels # %% Define kernel_sumexp # kernel_sumexp = HawkesKernelSumExp( # intensities=np.array([0.1, 0.2, 0.1]), # decays=np.array([1.0, 3.0, 7.0])) # %% [markdown] # ## Simulations
Simulation of Hawkes processes with usage of custom kernels """ import matplotlib.pyplot as plt import numpy as np from tick.base import TimeFunction from tick.hawkes import SimuHawkes, HawkesKernelExp, HawkesKernelTimeFunc from tick.plot import plot_point_process 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
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, n_basis=n_basis, kernel_size=kernel_size, C=C, n_threads=4,
def setUp(self): size = 10 self.y_values = np.random.rand(size) self.t_values = np.arange(size, dtype=float) self.time_function = TimeFunction([self.t_values, self.y_values]) self.hawkes_kernel_time_func = HawkesKernelTimeFunc(self.time_function)
def test_HawkesEM(): print('\n##############################') print('\nstarting: test_HawkesEM()\n') run_time = 30000 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]) ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### realizations = list() for i in range(0, 1000): print('') temp_seed = int(1000 + 1000 * random.random()) print('i = ' + str(i) + ', temp_seed = ' + str(temp_seed)) hawkes = SimuHawkes(baseline=baseline, end_time=run_time, verbose=False, seed=temp_seed) hawkes.set_kernel(0, 0, kernel1) hawkes.set_kernel(0, 1, HawkesKernelExp(.5, .7)) hawkes.set_kernel(1, 1, kernel2) hawkes.simulate() temp_realization = hawkes.timestamps print('i = ' + str(i) + ', ' + 'event counts = (' + str(len(temp_realization[0])) + ',' + str(len(temp_realization[1])) + ')') print(temp_realization) realizations.append(temp_realization) ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### em = HawkesEM(4, kernel_size=16, n_threads=8, verbose=False, tol=1e-3) em.fit(events=realizations) fig = plot_hawkes_kernels(em, hawkes=hawkes, show=False) outputFILE = 'test-HawkesEM.png' for ax in fig.axes: ax.set_ylim([0, 1]) plt.savefig(fname=outputFILE, bbox_inches='tight', pad_inches=0.2) ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### print('\nexitng: test_HawkesEM()') print('\n##############################') return (None)
import numpy as np import matplotlib.pyplot as plt from tick.hawkes import (SimuHawkes, HawkesKernelTimeFunc, HawkesKernelExp, HawkesEM) from tick.base import TimeFunction from tick.plot import plot_hawkes_kernels run_time = 30000 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))
import numpy as np from tick.plot import plot_hawkes_kernels from tick.hawkes import (SimuHawkes, SimuHawkesMulti, HawkesKernelExp, HawkesKernelTimeFunc, HawkesKernelPowerLaw, 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()