- Let's import NumPy and matplotlib.

In [ ]:

```
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
```

- Let's specify the
`rate`

: the average number of events per second.

In [ ]:

```
rate = 20. # average number of events per second
```

- First, we will simulate the process using small time bins of 1 millisecond.

In [ ]:

```
dt = .001 # time step
n = int(1./dt) # number of time steps
```

In [ ]:

```
x = np.zeros(n)
x[np.random.rand(n) <= rate*dt] = 1
```

`x`

contains zeros and ones on all time bins, *one* corresponding to the occurrence of an event.

In [ ]:

```
x[:10]
```

- Let's display the simulated process. We draw a vertical line on each non-zero time bin.

In [ ]:

```
plt.figure(figsize=(6,2));
plt.vlines(np.nonzero(x)[0], 0, 1);
plt.xticks([]); plt.yticks([]);
```

- Another way of representing that same object consists in considering the associated
**counting process**$N(t)$: the number of events that have occurred until time $t$. Here, we can display this process using the function`cumsum`

.

In [ ]:

```
plt.figure(figsize=(6,4));
plt.plot(np.linspace(0., 1., n), np.cumsum(x));
plt.xlabel("Time");
plt.ylabel("Counting process");
```

In [ ]:

```
y = np.cumsum(np.random.exponential(1./rate, size=int(rate)))
```

`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.

In [ ]:

```
y[:10]
```

- Finally, let's display the simulated process.

In [ ]:

```
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).