def fit(self, timestamps):
        """
        Parameters
        ----------
        timestamps : list of float
            Ordered list containing event times.
        """

        self.res = self._estimator.fit(timestamps)

        self.estimator = self.res.x
        self.estimated_loss = self.res.fun

        if self.return_model:
            self.model = exp_thinning_hawkes(self.estimator[0],
                                             self.estimator[1],
                                             self.estimator[2])
            self.model.set_time_intensity(timestamps)
示例#2
0
import numpy as np
from code.hawkes_process import exp_thinning_hawkes
from code.estimator_class import loglikelihood_estimator

if __name__ == "__main__":

    # Set seed
    np.random.seed(7)

    lambda_0 = 1.2
    alpha = -0.4
    beta = 0.9

    # Create timestamps from exponential Hawkes process
    hawkes = exp_thinning_hawkes(lambda_0, alpha, beta, max_jumps=100)
    hawkes.simulate()
    tList = hawkes.timestamps

    # Estimate using the estimator class, the L-BFGS-B algorithm and the real loglikelihood
    model = loglikelihood_estimator()
    model.fit(tList)

    print("Estimated parameters are:", model.estimator
          )  # Estimated parameters are: [ 1.49351268 -0.35849889  0.43911736]
    print(
        "With a loglikelihood of:",
        -model.estimated_loss)  # With a loglikelihood of: -112.56222084709412
示例#3
0
import numpy as np
from matplotlib import pyplot as plt
from code.hawkes_process import exp_thinning_hawkes

if __name__ == "__main__":

    # Set seed
    np.random.seed(0)

    lambda_0 = 1.05
    alpha = -0.7
    beta = 0.8

    # Create a process with given parameters and maximal number of jumps.
    hawkes = exp_thinning_hawkes(lambda_0=lambda_0,
                                 alpha=alpha,
                                 beta=beta,
                                 max_jumps=15)
    hawkes.simulate()

    # Plotting function of intensity and step functions.
    hawkes.plot_intensity()

    plt.show()
                    arrowprops=dict(arrowstyle='|-|,widthA=0.3,widthB=0.3', facecolor='red'),
                    annotation_clip=False)  # This enables the arrow to be outside of the plot

        ax.annotate(parameters_str[i % 3], xy=(positions1[i] - 0.1, 1.5e-6), xytext=(positions2[i] - 0.8, 1.5e-6),
                    annotation_clip=False)

    legend_str = ['exact', "approx"]
    legend_elements = [Patch(facecolor=colors[i], edgecolor='k', label=legend_str[i]) for i in range(2)]
    ax.legend(handles=legend_elements, loc='best')

    ax.grid()
    ax.set_yscale('log')

    ## Plot intensity examples for each parameter set.

    for i in range(prefixed):
        np.random.seed(1)
        hawkes = exp_thinning_hawkes(theta_list[i, 0], theta_list[i, 1], theta_list[i, 2], max_jumps=max_jumps)
        hawkes.simulate()
        hawkes.plot_intensity(ax=intensity_ax[i], plot_N=False)
        intensity_ax[i].set_xlim(0, 20)
        intensity_ax[i].legend().set_visible(False)
        intensity_ax[i].set_title(str(round(100 * proportion_list[i], 4)) + "%")

    intensity_ax[0].set_ylim(0.475, 0.5001)

    ## Save image as pdf.
    # plt.savefig('intensity_boxplot_log_cropped.pdf', bbox_inches='tight', format="pdf")

    plt.show()