泊松过程测试

时间:2013-09-16 19:23:39

标签: python math statistics scipy

我想对零假设进行一些测试,即我所拥有的事件的时间是从均匀泊松过程中创建的(参见例如http://en.wikipedia.org/wiki/Poisson_process)。因此,对于固定数量的事件,时间应该看起来像是在适当范围内的均匀分布的排序版本。在http://docs.scipy.org/doc/scipy-0.7.x/reference/generated/scipy.stats.kstest.html有一个Kolmogorov-Smirnov测试的实现,但我不知道如何在这里使用它作为scipy.stats似乎不知道泊松过程。

作为一个简单的例子,这个样本数据应该为任何这样的测试提供高p值。

import random
nopoints = 100
max = 1000

points = sorted([random.randint(0,max) for j in xrange(nopoints)])

如何对此问题进行合理的测试?

来自www.stat.wmich.edu/wang/667/classnotes/pp/pp.pdf我看到了

” 备注6.3(测试POISSON)上述定理也可用于检验假设 给定的计数过程是泊松过程。这可以通过观察固定时间t的过程来完成。如果在这段时间内我们观察到n次出现并且如果过程是泊松,那么无序的出现时间将独立且均匀地分布在(0,t)上。因此,我们可以通过测试假设来测试该过程是否为泊松。 n个出现时间来自均匀(0,t)种群。这可以通过标准统计程序完成,例如Kolmogorov-Smirov检验。“

3 个答案:

答案 0 :(得分:3)

警告:写得很快,有些细节未经过验证

适用于chisquare测试的指数,自由度的估算器

基于讲义

同质性的含义不会因三项测试中的任何一项而被拒绝。 说明如何使用scipy.stats的kstest和chisquare测试

# -*- coding: utf-8 -*-
"""Tests for homogeneity of Poissson Process

Created on Tue Sep 17 13:50:25 2013

Author: Josef Perktold
"""

import numpy as np
from scipy import stats

# create an example dataset
nobs = 100
times_ia = stats.expon.rvs(size=nobs) # inter-arrival times
times_a = np.cumsum(times_ia) # arrival times
t_total = times_a.max()

# not used
#times_as = np.sorted(times_a)
#times_ia = np.diff(times_as)

bin_limits = np.array([ 0. ,  0.5,  1. ,  1.5,  2. ,  np.inf])
nfreq_ia, bins_ia = np.histogram(times_ia, bin_limits)


# implication: arrival times are uniform for fixed interval
# using times.max() means we don't really have a fixed interval
print stats.kstest(times_a, stats.uniform(0, t_total).cdf)

# implication: inter-arrival times are exponential
lambd = nobs * 1. / t_total
scale = 1. / lambd

expected_ia = np.diff(stats.expon.cdf(bin_limits, scale=scale)) * nobs
print stats.chisquare(nfreq_ia, expected_ia, ddof=1)

# implication: given total number of events, distribution of times is uniform
# binned version
n_mean_bin = 10
n_bins_a = nobs // 10
bin_limits_a = np.linspace(0, t_total+1e-7, n_bins_a + 1)
nfreq_a, bin_limits_a = np.histogram(times_a, bin_limits_a)
# expect uniform distributed over every subinterval
expected_a = np.ones(n_bins_a) / n_bins_a * nobs
print stats.chisquare(nfreq_a, expected_a, ddof=1)

答案 1 :(得分:1)

KS test,在确定两个分布是否不同时,只是它们之间的最大差异:

enter image description here

这很容易计算自己。下面的程序计算具有不同参数集的两个泊松过程的KS统计量:

import numpy as np

N = 10**6
X  = np.random.poisson(10, size=N)
X2 = np.random.poisson(7, size=N)

bins = np.arange(0, 30,1)
H1,_ = np.histogram(X , bins=bins, normed=True)
H2,_ = np.histogram(X2, bins=bins, normed=True)

D = np.abs(H1-H2)

idx = np.argmax(D)
KS = D[idx]

# Plot the results
import pylab as plt
plt.plot(H1, lw=2,label="$F_1$")
plt.plot(H2, lw=2,label="$F_2$")
text = r"KS statistic, $\sup_x |F_1(x) - F_2(x)| = {KS:.4f}$"
plt.plot(D, '--k', label=text.format(KS=KS),alpha=.8)
plt.scatter([bins[idx],],[D[idx],],s=200,lw=0,alpha=.8,color='k')
plt.axis('tight')
plt.legend()

enter image description here

答案 2 :(得分:0)

问题在于,您链接的文档建议:"The KS test is only valid for continuous distributions.",而泊松分布是离散的。

我建议您使用此链接中的示例: http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter1_Introduction/Chapter1_Introduction.ipynb (查找“#####示例:从文本消息数据中推断行为”)

在该链接中,他们根据泊松过程检查他们假设分配的特定数据集的相应lambda。