This post is about a stochastic process called the Hawkes process. I offer some Python code for generating synthetic sequences and doing parameter estimation, and also cover some theoretical preliminaries.
I am using the Hawkes process in some on-going research — I found that it is popular enough to have a large, interdisciplinary literature, but specialized enough that the available software packages are few and far between. At the end of this blog, I link to several repos already out there for this family of models. They are high quality, production-level packages, and miles beyond what I offer here, but are in my opinion overkill for someone just getting started and needing some basic code to fiddle with.
I’m hoping to bridge this gap by offering both a beginner-friendly math exposition and an immediately useable, if relatively basic, code library in Python.
We can define a point process as a random and finite series of events governed by a probablistic rule. For example, the ubiquitous Poisson process is a series of points along the nonnegative real line such that the probability of points on any interval length is given by a Poisson distribution with parameter . The bottom series of points in the figure above is an example of a Poisson process (with rate ).
We may even consider a -dimensional Poisson process, with different Poisson processes generating events in , each with a different rate . Now, the overall number of points in a particular interval (or now more appropriately, volume) is again given by a Poisson distribution with parameter , where the rate (the Poisson superposition theorem). This additive property allows us to compute the probability that a particular event originated from a particular dimension as .
You can also show that the number of events in disjoint subsets are independent of each other. This leads to the critical observation that the Poisson process is memoryless. Another way to state this is that the interarrival time between two successive events is an exponential random variable, and therefore the probability of the next interarrival time is independent of the previous. This memoryless property makes the Poisson process an extremely tractable modeling tool.
However, the memoryless property of Poisson processes means that it is unable to capture a dependence on history, or in other words, interaction between events. For example, we may want the event of an arrival to increase the probability of arrivals in the next small interval of time.
For this, we introduce the Hawkes process, which gives an additive, decaying increase to the intensity function for each new arrival. Now, the intensity function is only conditionally Poisson: that is, given the history of events up to , the conditional intensity at , that is , is Poisson.
Definition. Hawkes process. Consider a sequence of events consisting of a time and dimension (i.e. the -th event occurred at time in dimension ), for and . This sequence is a Hawkes process if the conditional intensity function has the parameterized form
where are the model parameters and , is the matrix of triggering kernels (also sometimes called the excitation function or decay kernel) which is varying with and .
In other words, imagine streams of events, all happening simultaneously. And imagine that, within a stream , any previous event has the capability of increasing the likelihood of a future event occurring at time , according to some function which we control with parameters . Further, imagine that streams can influence not only themselves, but also each other, according to the same function, and parameters (for the effect of stream on ).
We’d like to model the triggering kernel as something that decays with time: as previous events become more and more distant, they have less and less effect on the present. A natural choice is the exponential function . We may also separate the exponential function itself from a scaling factor, so we write:
and we might even set globally, and only tune the ’s (which still gives us parameters to learn).
A univariate example is given above, and compared to a Poisson process with the same base rate . In the Hawkes process, we see that events cause a spike in the intensity , which frequently leads to more events, more spikes … this results in a burst of activity (which happens to well-model the actual way many event sequences behave, such as the communication habits of people).
In the multivariate example above, we have 3 different streams of events, call them , , and . We have engineered it by setting the parameters such that has an influence on itself and , and has an influence on . We also set the background rate to zero for all the streams except for — this way, if we see an event on or we know it came from a previous event on the parent stream, not a random event.
This creates a cascading effect that we see almost immediately: a background event in causes a “child” event in , which then leads to a child event in . Notice that in the second cascade, there is no event in even though it experienced a spike in probability of an event happening. Because is self-exciting, we see temporal clustering there which translates to bursts in its child processes.
How did we generate the data in the figures above? A well-worn approach is known as Ogata’s thinning method, which essentially generates new events from an exponential distribution parameterized by the Hawkes intensity at that time, but then rejects some events with some probability that decreases as the time since the last event increases.
In the multivariate case, this is only slightly more complicated, since we must also attribute each generated event to a particular dimension based on the proportional likelihood the new event came from that dimension. In expositions like this excellent slide deck, these two steps are called Reject-Attribute.
The details are apparent in the repo, but let us mention two important modifications.
The algorithm as typically described requires operations to draw samples over dimensions, which is prohibitive for large or . Instead, we modify an approach mentioned in this paper. Given the rates at the last event (which note do not include effects of ), we can calculate for by
which we can do in , and only requires saving the rates at the most recent event. Note also that when , this reduces to
or in other words, the previous rate plus the maximum contribution the event can make since it has just occurred.
Secondly, we find that texts describing the algorithm typically frame the attribution/rejection test as finding an index such that a uniformly random number on is between the normalized successive sums of intensities around that index. But note that this entire procedure amounts to a weighted random sample over the integers where the probabilities are the normalized rates, and selecting is equivalent to the ``rejection’’ condition. This allows us to use optimized package software for weighted random samples, instead of something like a for-loop (as is present in even production-level Hawkes process software, like the hawkes
package in R see below).
Lastly, we will mention a few approaches for estimating the parameters of a Hawkes process. It would be impossible to reasonably cover the entire literature, or even a single method, in any depth here, but I’ll try to cover the major “streams” (haha) of technique (including the approach I take in the repo’d code).
The sheer number of parameters also leads to massive overfitting and problems of sparsity. As a result, most methods apply some type of regularization (typically on the ’s), such as an L2-norm (here), or L1-norm and nuclear norm (here), etc.
Bayesian frameworks. We can achieve this regularization instead by placing a prior on the various parameters. This paper places a Gamma prior on the scaling parameters , a log-normal prior on the triggering kernel params , etc., and uses a collapsed Gibbs sampling procedure.
Expectation-maximization (EM). In the univariate case, where sparsity is not a concern but the challenges of MLE persist, many apply EM. Briefly, EM is designed to estimate parameters when there is missing data. It takes a guess at the missing entries based on the current parameter estimate, then re-estimates the parameters using these guesses, then iterates.
In the Hawkes application, our missing data is the unknown matrix of which event caused some subsequent event. That is, each observed event is either a parent (i.e. a background event), or a child (i.e. result of some excitation from a previous event). By incorporating this magic, completely unknown knowledge, we suddenly add a lot of structure to the problem that helps stabilize parameter estimation. (As a bonus, we get an estimate of the parent-child relationships of each event, although I have not seen research making use of this byproduct.)
Some papers using this approach in seismology, email data (with a nice exposition), and a nonparametric version (with a tie-in to gradient descent). There are many others.
Maximum aposteriori (MAP) EM. We can incorporate some regularization into the EM framework by introducing a prior and doing MAP EM. This is a simple extension of the EM methods, but allows us to reasonably estimate in the multivariate case. This extension comes up as majorization-minimization in this paper, and it is the subject of an unpublished report I did with my colleague Phil Chodrow last year posted here. I extend this to a multivariate version that is discussed in my masters thesis here.
The repo includes this MAP EM approach. It is still a bit rough: it treats as a global hyperparameter, does not incorporate a prior on the background rates , and I think the expression I am using for the “complete data log-likelihood” in the multivariate case is actually a tight lower bound on the real value as they show here.
Hopefully it is helpful/interesting to you! Feel free to send any feedback/questions, via Twitter or an email (see my about page).
Some of what’s out there, that I’ve found:
Tick is a Python library for statistical learning of point processes, with some additional optimization toolkit tools as well. This library was recently brought to my attention, but from what I can tell it is likely the best of the libraries I list here, in terms of completeness and documentation.
Package hawkes
(for R), CRAN here. Core functions are: a simulationHawkes
function to generate sequences, and a likelihoodHawkes
function which returns the (negative) likelihood of a particular sequence. All functions support both uni- and muti-variate sequences. The sequence generation uses Ogata thinning, as we discussed before. The likelihood can be used with R’s optimize
function for a rough MLE parameter estimation approach. I have not done extensive testing of this package myself, but it seems valuable to have a sister package offering this functionality in Python.
This repo is an amazing collection of tools in Python for parameter estimation, using a wide variety of methods, based on Scott Linderman’s doctoral research. The core of the repo is code for estimating parameters of a network version of a Hawkes process, where each stream corresponds to a node in a network, using a fully Bayesian framework and Gibbs sampling techniques. Here’s an excellent paper by Linderman and Adams using some of this machinery.
PtPack is a C++ package for modeling multivariate point processes, courtesy of Nan Du of Georgia Tech. He has some great work on Hawkes processes, with some interesting applications like recommender systems and nice blending of techniques like using a Dirichlet process to regularize by clustering in the parameter space. The repo itself is very robust and worth checking out.
Many of Nan Du’s colleagues/collaborators at GA Tech have done work in this area too — I particularly liked Ke Zhou’s ADMM-MM technique for parameter estimation — but I was unable to find code repositories for this other work.
If you know of other useful repositories, I would love to hear from you and at a minimum, add to this list!
Written on June 26th, 2017 by Steven Morse