当您在 Excel(或 OpenOffice Calc)中使用 POISSON 函数时,它需要两个参数:
When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:
- 一个整数
- 平均"数字
并返回一个浮点数.
在 Python 中(我尝试过 RandomArray 和 NumPy),它返回一个随机泊松数数组.我真正想要的是这个事件发生的百分比(它是一个常数,数组每次都有不同的数字 - 那么它是平均值吗?).
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?).
例如:
print poisson(2.6,6)返回[1 3 3 0 1 3](每次运行它都不一样).
returns [1 3 3 0 1 3] (and every time I run it, it's different).
我从 calc/excel 得到的数字是 3.19 (POISSON(6,2.16,0)*100).
The number I get from calc/excel is 3.19 (POISSON(6,2.16,0)*100).
我是否使用了蟒蛇的泊松错误(不是双关语!)还是我遗漏了什么?
Am I using the python's poisson wrong (no pun!) or am I missing something?
推荐答案手工操作很容易,但这样做可能会溢出.您可以在循环中执行指数和阶乘以避免溢出:
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