Calculate poisson probability percentage
Asked Answered
F

3

16

When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:

  • 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?).

for example:

print poisson(2.6,6)

returns [1 3 3 0 1 3] (and every time I run it, it's different).

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?

Fro answered 11/11, 2008 at 12:51 Comment(0)
M
14

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
Maybe answered 11/11, 2008 at 13:24 Comment(0)
A
26

scipy has what you want

>>> 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.

Aphelion answered 11/11, 2008 at 13:17 Comment(1)
Alternative import would be: from scipy.stats import poisson then poisson.pmf(6, 2.6) = 0.031867055625524499Naoma
M
14

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
Maybe answered 11/11, 2008 at 13:24 Comment(0)
J
1

This page explains why you get an array, and the meaning of the numbers in it, at least.

Jujitsu answered 11/11, 2008 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.