模拟泊松过程给定时间,求发生次数给定发生次数,求所需时间非齐时泊松过程
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from scipy import stats
from tqdm import tqdm, trange
sns.set()
sns.set_context('talk')
sns.set_style('ticks')
# rate = 1 # the rate of possion process
time = 10 # the total time of a path of possion process
interval = 0.001
plt.figure(figsize=(15, 10))
k = 0
for rate in (1/2, 1, 2, 5):
k += 1
plt.subplot(2, 2, k)
lambda_ = rate * interval # the parameter of possion distribution
x = np.arange(0, time, interval)
y = np.cumsum(np.random.poisson(lambda_, size=int(time/interval)).clip(0, 1))
for i in np.unique(y):
where = np.where(y == i)
plt.plot(x[where], y[where], c='r')
sns.despine()
plt.title(f'A path of possion process with rate $\lambda={rate}$', fontdict={'fontsize':15})
plt.suptitle('Possion Process With Different Rate', y=1, fontsize=20)
# plt.savefig('p1.png', dpi=400)
plt.show()
size = 10
plt.figure(figsize=(15, 10))
k = 0
for rate in (0.5, 1, 2, 5):
k += 1
plt.subplot(2, 2, k)
sample = np.random.exponential(1/rate, size=size)
time = np.cumsum(sample)
for i in range(size-1):
plt.plot((time[i], time[i+1]), (i, i), c='r')
sns.despine()
plt.title(f'A path of possion process with rate $\lambda={rate}$', fontdict={'fontsize':15})
plt.suptitle('Possion Process With Different Rate', y=1, fontsize=20)
# plt.savefig('p2.png', dpi=400)
plt.show()
考虑强度函数 的非齐时泊松过程
rate = lambda x: 2 * x
m = lambda x: x ** 2
time = 10 # the total time of a path of possion process
interval = 0.0001
plt.figure(figsize=(9, 5.5))
x = np.arange(0, time, interval)
tmp = [np.random.poisson(rate(i) * interval) for i in x]
y = np.cumsum([np.random.poisson(rate(i) * interval, size=1).clip(0, 1) for i in x])
for i in np.unique(y):
where = np.where(y == i)
plt.plot(x[where], y[where], c='r')
plt.title('A path of possion process with rate $\lambda(x)=2x$', fontdict={'fontsize':15})
sns.despine();plt.show()