Figure 5.7:

A unit Poisson process with more events; sample path (top) and frequency distribution of event times \tau .

Code for Figure 5.7

Text of the GNU GPL.

main.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import matplotlib.pyplot as plt

# Set the number of samples and random seed for reproducibility
nsam = 1001
np.random.seed(0)

# Generate random samples for the Poisson process
p = np.random.rand(nsam, 1)
tau = -np.log(p)
time = np.concatenate(([0], np.cumsum(tau).flatten()))

# Generate step plot (stairs in MATLAB)
Y = np.arange(nsam + 1)
plt.figure()
xx, yy = np.repeat(time, 2)[1:-1], np.repeat(Y, 2)[:-2]  # Equivalent to stairs without the last element
plt.plot(xx, yy)
plt.title('Poisson Process Stairs Plot')
plt.xlabel('Time')
plt.ylabel('Count')
plt.grid(True)
plt.show(block=False)

# Plot histogram of tau
plt.figure()
nbins = 20
ntau, bins, _ = plt.hist(tau, bins=nbins, edgecolor='black')
# make bar plot data for gnuplot
barx = np.kron(bins,[1,1,1])[1:-1]
bary = np.append(np.kron(ntau, [0, 1, 1]), 0)
plt.title('Histogram of Inter-arrival Times (tau)')
plt.xlabel('Tau')
plt.ylabel('Frequency')
plt.grid(True)
plt.show(block=False)

# Prepare tables to save data
table1 = np.column_stack((xx, yy))
#
table2 = np.column_stack((barx, bary))

# Save data in readable .dat format using fmt='%f'
with open('poissonlots.dat', 'w') as f:
    np.savetxt(f, table1, fmt='%f', header="table1")
    f.write("\n\n")
    np.savetxt(f, table2, fmt='%f', header="table2")