Poisson分布(法语:loi de Poisson,英语:Poisson distribution,译名有泊松分布、普阿松分布、卜瓦松分布、布瓦松分布、布阿松分布、波以松分布、卜氏分配等),是一种统计与概率学里常见到的离散机率分布(discrete probability distribution),由法国数学家西莫恩·德尼·泊松(Siméon-Denis Poisson)在1838年时发表。
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()