Skip to main content
 首页 » 编程设计

python之不可预测的泊松噪声

2025年05月04日10duanxz

我正在比较我应用泊松噪声的两组值。 下面是我的代码和相应的结果:

import numpy as np 
import pylab 
 
size = 14000 
 
# 1) Creating first array 
np.random.seed(1) 
sample = np.zeros((size),dtype="int")+1000 
# Applying poisson noise 
random_sample1 = np.random.poisson(sample) 
 
# 2) Creating the second array (with some changed values) 
# Update some of the value to 2000... 
for x in range(size): 
  if not(x%220): 
    sample[x]=2000 
# Reset the seed to the SAME as for the first array 
# so that poisson shall rely on same random. 
np.random.seed(1) 
# Applying poisson noise 
random_sample2 = np.random.poisson(sample) 
 
# Display diff result 
pylab.plot(random_sample2-random_sample1) 
pylab.show() 

我的问题是:为什么我在 [10335-12542] 附近有这个奇怪的值,而我期望的只是一个完美的零?

我在 poisson() 中搜索信息没有成功的文档。

我(仅)在 python 版本 1.7.6 和 1.7.9 中测试并重现该问题(它可能出现在其他版本上)。 测试的 Numpy 版本:1.6.2 和 1.9.2

更多详细信息,如果我打印相关值:

random_sample1[10335:10345] 
[ 977 1053  968 1032 1051  953 1036 1035  967  954] 
#  OK  OK    OK   OK   OK  OK!  ???  ???  ???  ??? 
random_sample2[10335:10345] 
[ 977 1053  968 1032 1051 2051 1035  967  954 1034] 
#  OK  OK    OK   OK   OK  OK!  ???  ???  ???  ??? 

我们清楚地看到索引 10339 之前的值与索引 10340 完全相同,因为我们有 sample[10340] == 2000 这正是我们想要的。但是接下来的值不是我们期望的!它们似乎从 1 个索引偏移了!

请您参考如下方法:

这隐含在计算泊松分布随机样本的算法中。查看source code here.

随机样本是在条件循环中计算的,它会获取一个新的随机值,并在该值高于基于 lambda 的某个阈值时返回。对于不同的 lambda,可能需要不同的尝试次数。随后的随机值将被抵消,导致您看到不同的结果。 Lateron,随机值再次同步。

在您的具体示例中,它使用一个额外的随机值来获取样本 #10340。之后,所有值都偏移一个。