python 计算泊松概率百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/280797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate poisson probability percentage
提问by Alex Coventry
When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:
当您在 Excel(或 OpenOffice Calc)中使用 POISSON 函数时,它需要两个参数:
- an integer
- an 'average' number
- 一个整数
- 一个“平均”数字
and returns a float.
并返回一个浮点数。
In Python (I tried RandomArray and NumPy) it returns an array of random poisson numbers. What I really want is the percentage that this event will occur (it is a constant number and the array has every time different numbers - so is it an average?).
在 Python 中(我尝试过 RandomArray 和 NumPy),它返回一个随机泊松数数组。我真正想要的是这个事件发生的百分比(它是一个常数,数组每次都有不同的数字 - 那么它是平均值吗?)。
for example:
例如:
print poisson(2.6,6)
returns [1 3 3 0 1 3]
(and every time I run it, it's different).
返回[1 3 3 0 1 3]
(每次我运行它时,它都是不同的)。
The number I get from calc/excel is 3.19 (POISSON(6,2.16,0)*100
).
我从 calc/excel 得到的数字是 3.19 ( POISSON(6,2.16,0)*100
)。
Am I using the python's poisson wrong (no pun!) or am I missing something?
我是否错误地使用了蟒蛇的泊松(不是双关语!)还是我遗漏了什么?
采纳答案by Ned Batchelder
It is easy to do by hand, but you can overflow doing it that way. You can do the exponent and factorial in a loop to avoid the overflow:
手工制作很容易,但这样做可能会溢出。您可以在循环中执行指数和阶乘以避免溢出:
def poisson_probability(actual, mean):
# naive: math.exp(-mean) * mean**actual / factorial(actual)
# iterative, to keep the components from getting too large or small:
p = math.exp(-mean)
for i in xrange(actual):
p *= mean
p /= i+1
return p
回答by Alex Coventry
scipy
has what you want
scipy
有你想要的
>>> scipy.stats.distributions
<module 'scipy.stats.distributions' from '/home/coventry/lib/python2.5/site-packages/scipy/stats/distributions.pyc'>
>>> scipy.stats.distributions.poisson.pmf(6, 2.6)
array(0.031867055625524499)
It's worth noting that it's pretty easy to calculate by hand, too.
值得一提的是,它很容易通过手来计算,太。