前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >泊松过程的模拟

泊松过程的模拟

作者头像
用户3577892
发布2020-06-11 09:32:51
1.1K0
发布2020-06-11 09:32:51
举报
文章被收录于专栏:数据科学CLUB

模拟泊松过程给定时间,求发生次数给定发生次数,求所需时间非齐时泊松过程

代码语言:javascript
复制
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')

模拟泊松过程

给定时间,求发生次数
代码语言:javascript
复制
# 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()
给定发生次数,求所需时间
代码语言:javascript
复制
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()

非齐时泊松过程

考虑强度函数 的非齐时泊松过程

代码语言:javascript
复制
rate = lambda x: 2 * x
m = lambda x: x ** 2
代码语言:javascript
复制
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()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据科学CLUB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模拟泊松过程
    • 给定时间,求发生次数
      • 给定发生次数,求所需时间
        • 非齐时泊松过程
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档