python 均匀采样_如何以比这更快的速度在Python中采样非均匀...

这是一个运行速度快约75倍并实现相同功能的版本:

def generate_times_opt(rate_function,max_t,delta):

t = numpy.arange(delta,max_t, delta)

avg_rate = (rate_function(t) + rate_function(t + delta)) / 2.0

avg_prob = 1 - numpy.exp(-avg_rate * delta / 1000.0)

rand_throws = numpy.random.uniform(size=t.shape[0])

return t[avg_prob >= rand_throws].tolist()

输出:

11:59:07 [70]: %timeit generate_times(rate_function, max_t, delta)

10 loops, best of 3: 75 ms per loop

11:59:23 [71]: %timeit generate_times_opt(rate_function, max_t, delta)

1000 loops, best of 3: 1.15 ms per loop

旁注:不过,这不是模拟非均匀泊松过程的最佳方法.从Wikipedia开始:

An inhomogeneous Poisson process with intensity function λ(t) can be simulated by rejection sampling from a homogeneous Poisson process with fixed rate λ: choose a sufficiently large λ so that λ(t) = λ p(t) and simulate a Poisson process with rate parameter λ. Accept an event from the Poisson simulation at time t with probability p(t).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值