Example #1
0
class Test(unittest.TestCase):
    def setUp(self):
        np.random.seed(238924)
        decays = np.array([1., 3.])
        self.n_nodes = 2

        baseline = np.ones(self.n_nodes)
        adjacency = np.random.rand(self.n_nodes, self.n_nodes, len(decays))
        self.hawkes_simu = SimuHawkesSumExpKernels(decays=decays,
                                                   baseline=baseline,
                                                   adjacency=adjacency,
                                                   max_jumps=100,
                                                   verbose=False,
                                                   seed=32098)
        self.hawkes_simu.simulate()

    def test_plot_hawkes_kernels(self):
        """...Test plot_hawkes_history rendering given a fitted Hawkes
        learner
        """
        decays = np.array([1.5, 3.5])
        hawkes_sumexp = HawkesSumExpKern(decays, max_iter=0)
        # We set some specific coeffs to be free from any future learner
        # modifications
        # With 0 iteration and coeffs as start point it should remain there
        coeffs = np.array(
            [0.99, 0.99, 0.55, 0.37, 0.39, 0.16, 0.63, 0.49, 0.49, 0.30])
        hawkes_sumexp.fit(self.hawkes_simu.timestamps, start=coeffs)

        n_points = 10
        for support in [None, 4]:
            fig = plot_hawkes_kernels(hawkes_sumexp,
                                      hawkes=self.hawkes_simu,
                                      show=False,
                                      n_points=n_points,
                                      support=support)

            if support is None:
                max_support = hawkes_sumexp.get_kernel_supports().max() * 1.2
            else:
                max_support = support

            for i, j in itertools.product(range(self.n_nodes), repeat=2):
                index = i * self.n_nodes + j
                ax = fig.axes[index]
                ax_t_axis, ax_estimated_kernel = ax.lines[0].get_xydata().T
                t_axis = np.linspace(0, max_support, n_points)
                np.testing.assert_array_equal(ax_t_axis, t_axis)

                estimated_kernel = hawkes_sumexp.get_kernel_values(
                    i, j, t_axis)
                np.testing.assert_array_equal(ax_estimated_kernel,
                                              estimated_kernel)

                _, ax_true_kernel = ax.lines[1].get_xydata().T
                true_kernel = self.hawkes_simu.kernels[i, j].get_values(t_axis)
                np.testing.assert_array_equal(ax_true_kernel, true_kernel)
Example #2
0
    def setUp(self):
        np.random.seed(238924)
        decays = np.array([1., 3.])
        self.n_nodes = 2

        baseline = np.ones(self.n_nodes)
        adjacency = np.random.rand(self.n_nodes, self.n_nodes, len(decays))
        self.hawkes_simu = SimuHawkesSumExpKernels(decays=decays,
                                                   baseline=baseline,
                                                   adjacency=adjacency,
                                                   max_jumps=100,
                                                   verbose=False,
                                                   seed=32098)
        self.hawkes_simu.simulate()
    def setUp(self):
        np.random.seed(23982)
        self.n_nodes = 3
        self.n_decays = 4
        self.baseline = np.random.rand(self.n_nodes)
        self.adjacency = np.random.rand(self.n_nodes, self.n_nodes,
                                        self.n_decays) / 10
        self.decays = np.random.rand(self.n_decays)

        self.adjacency[0, 0, :] = 0
        self.adjacency[-1, -1, :] = 0

        self.hawkes = SimuHawkesSumExpKernels(self.adjacency,
                                              self.decays,
                                              baseline=self.baseline,
                                              seed=203,
                                              verbose=False)
def fit_exp_hawkes_and_simulate(train_times, decays, end_time):
    learner = HawkesSumExpKern(decays,
                               penalty='elasticnet',
                               elastic_net_ratio=0.8)
    learner.fit(train_times)
    score = learner.score()
    print(f'obtained {score}\n\n')

    # simulation
    simulation = SimuHawkesSumExpKernels(adjacency=learner.adjacency,
                                         decays=decays,
                                         baseline=learner.baseline,
                                         end_time=end_time,
                                         verbose=False,
                                         seed=42)
    simulation.simulate()

    return learner, simulation
    def test_hawkes_sumexp_constructor_errors(self):
        bad_adjacency = np.random.rand(self.n_nodes, self.n_nodes,
                                       self.n_decays + 1)

        msg = "^adjacency matrix shape should be \(3, 3, 4\) but its shape " \
              "is \(3, 3, 5\)$"
        with self.assertRaisesRegex(ValueError, msg):
            SimuHawkesSumExpKernels(bad_adjacency,
                                    self.decays,
                                    baseline=self.baseline)
Example #6
0
    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.3], [0.8, 0.2]], [[0., 0.], [1.3,
                                                                    0.9]]])

        sim = SimuHawkesSumExpKernels(adjacency=adjacency,
                                      decays=self.decays,
                                      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
Example #7
0
    def get_train_data(decays, n_nodes=3, n_decays=2):
        np.random.seed(130947)
        baseline = np.random.rand(n_nodes)
        adjacency = np.random.rand(n_nodes, n_nodes, n_decays)

        sim = SimuHawkesSumExpKernels(adjacency=adjacency,
                                      decays=decays,
                                      baseline=baseline,
                                      verbose=False,
                                      seed=13487,
                                      end_time=3000)
        sim.adjust_spectral_radius(0.8)
        adjacency = sim.adjacency
        sim.simulate()

        return sim.timestamps, baseline, adjacency
class Test(unittest.TestCase):
    def setUp(self):
        np.random.seed(23982)
        self.n_nodes = 3
        self.n_decays = 4
        self.baseline = np.random.rand(self.n_nodes)
        self.adjacency = np.random.rand(self.n_nodes, self.n_nodes,
                                        self.n_decays) / 10
        self.decays = np.random.rand(self.n_decays)

        self.adjacency[0, 0, :] = 0
        self.adjacency[-1, -1, :] = 0

        self.hawkes = SimuHawkesSumExpKernels(self.adjacency,
                                              self.decays,
                                              baseline=self.baseline,
                                              seed=203,
                                              verbose=False)

    def test_hawkes_exponential_kernels(self):
        """...Test creation of a Hawkes Process with exponential kernels
        """

        kernel_0 = None
        for i, j in product(range(self.n_nodes), range(self.n_nodes)):
            kernel_ij = self.hawkes.kernels[i, j]

            if np.linalg.norm(self.adjacency[i, j, :]) == 0:
                self.assertEqual(kernel_ij.__class__, HawkesKernel0)

                # We check that all 0 adjacency share the same kernel 0
                # This might save lots of memory with very large,
                # very sparse adjacency matrices
                if kernel_0 is None:
                    kernel_0 = kernel_ij
                else:
                    self.assertEqual(kernel_0, kernel_ij)

            else:
                self.assertEqual(kernel_ij.__class__, HawkesKernelSumExp)
                np.testing.assert_array_equal(kernel_ij.decays, self.decays)
                np.testing.assert_array_equal(kernel_ij.intensities,
                                              self.adjacency[i, j, :])

        np.testing.assert_array_equal(self.baseline, self.hawkes.baseline)

    def test_hawkes_spectral_radius_exp_kernel(self):
        """...Hawkes Process spectral radius and adjust spectral radius
        methods
        """
        self.assertAlmostEqual(self.hawkes.spectral_radius(),
                               0.5202743505580953)

        self.hawkes.adjust_spectral_radius(0.6)
        self.assertAlmostEqual(self.hawkes.spectral_radius(), 0.6)

    def test_hawkes_mean_intensity(self):
        """...Test that Hawkes obtained mean intensity is consistent
        """

        self.assertLess(self.hawkes.spectral_radius(), 1)

        self.hawkes.end_time = 1000
        self.hawkes.track_intensity(0.01)
        self.hawkes.simulate()

        mean_intensity = self.hawkes.mean_intensity()
        for i in range(self.hawkes.n_nodes):
            self.assertAlmostEqual(np.mean(self.hawkes.tracked_intensity[i]),
                                   mean_intensity[i],
                                   delta=0.1)

    def test_hawkes_sumexp_constructor_errors(self):
        bad_adjacency = np.random.rand(self.n_nodes, self.n_nodes,
                                       self.n_decays + 1)

        msg = "^adjacency matrix shape should be \(3, 3, 4\) but its shape " \
              "is \(3, 3, 5\)$"
        with self.assertRaisesRegex(ValueError, msg):
            SimuHawkesSumExpKernels(bad_adjacency,
                                    self.decays,
                                    baseline=self.baseline)
"""

import matplotlib.pyplot as plt

from tick.hawkes import SimuHawkesSumExpKernels, HawkesSumExpKern
from tick.plot import plot_point_process

end_time = 1000

decays = [0.1, 0.5, 1.]
baseline = [0.12, 0.07]
adjacency = [[[0, .1, .4], [.2, 0., .2]], [[0, 0, 0], [.6, .3, 0]]]

hawkes_exp_kernels = SimuHawkesSumExpKernels(adjacency=adjacency,
                                             decays=decays,
                                             baseline=baseline,
                                             end_time=end_time,
                                             verbose=False,
                                             seed=1039)

hawkes_exp_kernels.track_intensity(0.1)
hawkes_exp_kernels.simulate()

learner = HawkesSumExpKern(decays, penalty='elasticnet', elastic_net_ratio=0.8)
learner.fit(hawkes_exp_kernels.timestamps)

t_min = 100
t_max = 200
fig, ax_list = plt.subplots(2, 1, figsize=(10, 6))
learner.plot_estimated_intensity(hawkes_exp_kernels.timestamps,
                                 t_min=t_min,
                                 t_max=t_max,
import matplotlib.pyplot as plt

from tick.plot import plot_hawkes_kernels
from tick.hawkes import SimuHawkesSumExpKernels, SimuHawkesMulti, \
    HawkesSumExpKern

end_time = 1000
n_realizations = 10

decays = [.5, 2., 6.]
baseline = [0.12, 0.07]
adjacency = [[[0, .1, .4], [.2, 0., .2]],
             [[0, 0, 0], [.6, .3, 0]]]

hawkes_exp_kernels = SimuHawkesSumExpKernels(
    adjacency=adjacency, decays=decays, baseline=baseline,
    end_time=end_time, verbose=False, seed=1039)

multi = SimuHawkesMulti(hawkes_exp_kernels, n_simulations=n_realizations)

multi.end_time = [(i + 1) / 10 * end_time for i in range(n_realizations)]
multi.simulate()

learner = HawkesSumExpKern(decays, penalty='elasticnet',
                           elastic_net_ratio=0.8)
learner.fit(multi.timestamps)

fig = plot_hawkes_kernels(learner, hawkes=hawkes_exp_kernels, show=False)

for ax in fig.axes:
    ax.set_ylim([0., 1.])
Example #11
0
from tick.plot import plot_hawkes_baseline_and_kernels
from tick.hawkes import (
    SimuHawkesSumExpKernels, SimuHawkesMulti, HawkesSumExpKern
)

period_length = 300
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
Example #12
0
 def _corresponding_simu(self):
     return SimuHawkesSumExpKernels(
         adjacency=self.adjacency,
         decays=self.decays,
         baseline=self.baseline,
         period_length=self._model_obj.period_length)