def test_cyclic(self): """...Test cyclic border type """ last_value = 2.3 T = np.linspace(0, last_value) Y = np.cos(T * np.pi) tf = TimeFunction([T, Y], border_type=TimeFunction.Cyclic) self.assertAlmostEqual(tf.value(0.3), tf.value(last_value + 0.3), delta=1e-8)
def test_pickle(self): """...Test TimeFunction's pickling ability """ T = np.array([0.0, 1.0, 2.0]) Y = np.array([1.0, 0.0, -1.0]) tf = TimeFunction([T, Y], inter_mode=TimeFunction.InterLinear, dt=0.2) recon = pickle.loads(pickle.dumps(tf)) self.assertEqual(tf.value(1), recon.value(1)) self.assertEqual(tf.value(2), recon.value(2)) self.assertEqual(tf.value(1.5), recon.value(1.5)) self.assertEqual(tf.value(0.75), recon.value(0.75))
def test_values(self): """...Test that TimeFunction returns correct values on randomly selected times """ for t_array, y_array, inter_mode in self.samples: # take random t on TimeFunction support t_inside_support = np.random.uniform(t_array[0], t_array[-1], 10) t_before_support = np.random.uniform(t_array[0] - 4, t_array[0], 5) t_after_support = np.random.uniform(t_array[-1], t_array[-1] + 4, 5) test_t = np.hstack( (t_before_support, t_inside_support, t_after_support, t_array)) true_values = dichotomic_value(test_t, t_array, y_array, inter_mode=inter_mode) tf = TimeFunction([t_array, y_array], inter_mode=inter_mode) tf_values = tf.value(test_t) errors = np.abs(true_values - tf_values) # If we do not find the same value ensure that the error is # controlled by max_error different_values = errors > 1e-6 for t, error in zip(test_t[different_values], errors[different_values]): if inter_mode == TimeFunction.InterLinear: self.assertLess(error, tf._max_error(t)) else: distance_point = np.array(t - t_array).min() self.assertLess(distance_point, tf.dt)
def test_simulation_1d_inhomogeneous_poisson(self): """...Test if simulation of a 1d inhomogeneous Poisson process No statistical guarantee on the result""" run_time = 30 t_values = np.linspace(0, run_time - 3, 100) y_values = np.maximum(0.5 + np.sin(t_values), 0) tf = TimeFunction((t_values, y_values)) tf_zero = TimeFunction(0) inhomo_poisson_process = SimuInhomogeneousPoisson([tf, tf_zero], seed=2937, end_time=run_time, verbose=False) inhomo_poisson_process.simulate() timestamps = inhomo_poisson_process.timestamps # Ensure that non zero TimeFunction intensity ticked at least 2 times self.assertGreater(len(timestamps[0]), 2) # Ensure that zero TimeFunction intensity never ticked self.assertEqual(len(timestamps[1]), 0) # Ensure that intensity was non zero when the process did tick self.assertEqual(np.prod(tf.value(timestamps[0]) > 0), 1)
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")
def test_track_intensity(self): """...Test that point process intensity is tracked correctly """ t_values = np.linspace(0, self.run_time - 3, 100) y_values_1 = np.maximum(0.5 + np.sin(t_values), 0) y_values_2 = np.maximum(1. / (1 + t_values), 0) tf_1 = TimeFunction((t_values, y_values_1)) tf_2 = TimeFunction((t_values, y_values_2)) inhomo_poisson_process = SimuInhomogeneousPoisson( [tf_1, tf_2], end_time=self.run_time, seed=2937, verbose=False) inhomo_poisson_process.track_intensity(0.1) inhomo_poisson_process.simulate() tracked_intensity = inhomo_poisson_process.tracked_intensity intensity_times = inhomo_poisson_process.intensity_tracked_times # Ensure that intensity recorded is equal to the given TimeFunction np.testing.assert_array_almost_equal(tracked_intensity[0], tf_1.value(intensity_times)) np.testing.assert_array_almost_equal(tracked_intensity[1], tf_2.value(intensity_times))