This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
rate
: the average number of events per second.rate = 20. # average number of events per second
dt = .001 # time step
n = int(1./dt) # number of time steps
x = np.zeros(n)
x[np.random.rand(n) <= rate*dt] = 1
The vector x
contains zeros and ones on all time bins, one corresponding to the occurrence of an event.
x[:10]
plt.figure(figsize=(6,2));
plt.vlines(np.nonzero(x)[0], 0, 1);
plt.xticks([]); plt.yticks([]);
cumsum
.plt.figure(figsize=(6,4));
plt.plot(np.linspace(0., 1., n), np.cumsum(x));
plt.xlabel("Time");
plt.ylabel("Counting process");
y = np.cumsum(np.random.exponential(1./rate, size=int(rate)))
The vector y
contains another realization of our Poisson process, but the data structure is different. Every component of the vector is the time of an event.
y[:10]
plt.figure(figsize=(6,2));
plt.vlines(y, 0, 1);
plt.xticks([]); plt.yticks([]);
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).