Esempio n. 1
0
def simulate_NHP(run_time, T, Y, dt_, track):
    #run_time = 30

    #T = np.arange((run_time * 0.9) * 5, dtype=float) / 5
    #Y = np.maximum(
    #    15 * np.sin(T) * (np.divide(np.ones_like(T),
    #                                np.sqrt(T + 1) + 0.1 * T)), 0.001)

    tf = TimeFunction((T, Y), dt=dt_)

    # We define a 1 dimensional inhomogeneous Poisson process with the
    # intensity function seen above
    in_poi = SimuInhomogeneousPoisson([tf],
                                      end_time=run_time,
                                      verbose=True,
                                      seed=3)  #, max_jumps=sum(Y))

    # We activate intensity tracking and launch simulation
    in_poi.track_intensity(track)
    in_poi.threshold_negative_intensity(allow=True)

    in_poi.simulate()

    # We plot the resulting inhomogeneous Poisson process with its
    # intensity and its ticks over time
    plot_point_process(in_poi)
    return list(in_poi.tracked_intensity[0]), list(
        in_poi.intensity_tracked_times)
Esempio n. 2
0
def generate_times_opt(max_t, delta, vreme_c, c_var, vreme, Lambda):
    time = np.arange(delta,max_t, delta)
    c_value_t = C_value(time,vreme_c,c_var)
    lamb_val_t = Lambda_value(time,vreme,Lambda)/c_value_t
    tf = TimeFunction((time, lamb_val_t), dt=delta)
    Psim = SimuInhomogeneousPoisson([tf], end_time = max_t, verbose = False)
    Psim.simulate()
    simulacija = Psim.timestamps 
    plot_point_process(Psim)
    return simulacija
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
dt = 0.01
hawkes.track_intensity(dt)
hawkes.end_time = run_time
hawkes.simulate()

fig, ax = plt.subplots(hawkes.n_nodes, 1, figsize=(14, 8))
plot_point_process(hawkes, t_max=20, ax=ax)

plt.show()
Esempio n. 4
0
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,
                                 ax=ax_list)

plot_point_process(hawkes_exp_kernels, plot_intensity=True,
                   t_min=t_min, t_max=t_max, ax=ax_list)

# Enhance plot
for ax in ax_list:
    # Set labels to both plots
    ax.lines[0].set_label('estimated')
    ax.lines[1].set_label('original')

    # Change original intensity style
    ax.lines[1].set_linestyle('--')
    ax.lines[1].set_alpha(0.8)

    # avoid duplication of scatter plots of events
    ax.collections[1].set_alpha(0)

    ax.legend()
Esempio n. 5
0
    def plot_estimated_intensity(self,
                                 events,
                                 n_points=10000,
                                 plot_nodes=None,
                                 t_min=None,
                                 t_max=None,
                                 intensity_track_step=None,
                                 max_jumps=None,
                                 show=True,
                                 ax=None):
        """Plot value of intensity for a given realization with the fitted
        parameters

        events : `list` of `np.ndarray`, default = None
            One Hawkes processes realization, a list of n_node for
            each component of the Hawkes. Namely `events[i]` contains a
            one-dimensional `numpy.array` of the events' timestamps of
            component i.

        n_points : `int`, default=10000
            Number of points used for intensity plot.

        plot_nodes : `list` of `int`, default=`None`
            List of nodes that will be plotted. If `None`, all nodes are
            considered

        t_min : `float`, default=`None`
            If not `None`, time at which plot will start

        t_max : `float`, default=`None`
            If not `None`, time at which plot will stop

        intensity_track_step : `float`, default=`None`
            Defines how often intensity will be computed. If this is too low,
            computations might be long. By default, a value will be
            extrapolated from (t_max - t_min) / n_points.

        max_jumps : `int`, default=`None`
            If not `None`, maximum of jumps per coordinate that will be plotted.
            This is useful when plotting big point processes to ensure a only
            readable part of them will be plotted

        show : `bool`, default=`True`
            if `True`, show the plot. Otherwise an explicit call to the show
            function is necessary. Useful when superposing several plots.

        ax : `list` of `matplotlib.axes`, default=None
            If not None, the figure will be plot on this axis and show will be
            set to False.
        """

        simu = self._corresponding_simu()
        end_time = max(map(max, events))

        if t_max is not None:
            end_time = max(end_time, t_max)

        if intensity_track_step is None:
            display_start_time = 0
            if t_min is not None:
                display_start_time = t_min
            display_end_time = end_time
            if t_min is not None:
                display_start_time = t_min

            intensity_track_step = (display_end_time - display_start_time) \
                                   / n_points

        simu.track_intensity(intensity_track_step)
        simu.set_timestamps(events, end_time)

        plot_point_process(simu,
                           plot_intensity=True,
                           n_points=n_points,
                           plot_nodes=plot_nodes,
                           t_min=t_min,
                           t_max=t_max,
                           max_jumps=max_jumps,
                           show=show,
                           ax=ax)
Esempio n. 6
0
decay = 0.1
adjacency = np.array([[0.5]])

hawkes = SimuHawkesExpKernels(adjacency,
                              decay,
                              baseline=baselines,
                              seed=2093,
                              verbose=False)
hawkes.track_intensity(0.1)
hawkes.end_time = 6 * period_length
hawkes.simulate()

fig, ax = plt.subplots(1, 1, figsize=(10, 4))

plot_point_process(hawkes, ax=ax)

t_values = np.linspace(0, hawkes.end_time, 1000)
ax.plot(t_values,
        hawkes.get_baseline_values(0, t_values),
        label='baseline',
        ls='--',
        lw=1)
ax.set_ylabel("$\lambda(t)$", fontsize=18)
ax.legend()

plt.title("Intensity Hawkes process with exponential kernel and varying "
          "baseline")
fig.tight_layout()
plt.show()
Esempio n. 7
0
"""
1 dimensional Hawkes process simulation
=======================================
"""

from tick.plot import plot_point_process
from tick.hawkes import SimuHawkes, HawkesKernelSumExp
import matplotlib.pyplot as plt

run_time = 40

hawkes = SimuHawkes(n_nodes=1, end_time=run_time, verbose=False, seed=1398)
kernel = HawkesKernelSumExp([.1, .4, .1, .3], [3., 3., 7., 31])
hawkes.set_kernel(0, 0, kernel)
hawkes.set_baseline(0, 1.)

dt = 0.01
hawkes.track_intensity(dt)
hawkes.simulate()
timestamps = hawkes.timestamps
intensity = hawkes.tracked_intensity
intensity_times = hawkes.intensity_tracked_times

_, ax = plt.subplots(1, figsize=(8, 4))
plot_point_process(hawkes, n_points=50000, t_min=2, max_jumps=10, ax=ax)
plt.show()
This example show how to simulate any inhomogeneous Poisson process. Its 
intensity is modeled through `tick.base.TimeFunction`
"""

import numpy as np
from tick.base import TimeFunction

from tick.plot import plot_point_process
from tick.simulation.inhomogeneous_poisson import SimuInhomogeneousPoisson

run_time = 30

T = np.arange((run_time * 0.9) * 5, dtype=float) / 5
Y = np.maximum(15 * np.sin(T) * (np.divide(np.ones_like(T),
                                           np.sqrt(T + 1) + 0.1 * T)), 0.001)

tf = TimeFunction((T, Y), dt=0.01)

# We define a 1 dimensional inhomogeneous Poisson process with the
# intensity function seen above
in_poi = SimuInhomogeneousPoisson([tf], end_time=run_time, verbose=False)

# We activate intensity tracking and launch simulation
in_poi.track_intensity(0.1)
in_poi.simulate()

# We plot the resulting inhomogeneous Poisson process with its
# intensity and its ticks over time
plot_point_process(in_poi)
Esempio n. 9
0
from tick.hawkes import SimuPoissonProcess
from tick.plot import plot_point_process

run_time = 10
intensity = 5

poi = SimuPoissonProcess(intensity, end_time=run_time, verbose=False)
poi.simulate()
plot_point_process(poi)