Search code examples
pythonnumpyscipypoisson

Is there an easier way to use scipy.stats.poisson than my current method?


I was using scipy.stats to calculate the poisson probability distribution of goals in different football matches, but I can't help but think that there's an easier way to do this.

Say I am trying to calculate the probability of a game having less than 3 total goals as it's final outcome. Currently I am using something like this (the results are correct from my test runs):

def under25(self, homeS, awayS):
        under25 = 100 * (((poisson.pmf(0, homeS) * poisson.pmf(0, awayS)) +
                         (poisson.pmf(1, homeS) * poisson.pmf(0, awayS)) +
                         (poisson.pmf(0, homeS) * poisson.pmf(1, awayS)) +
                         (poisson.pmf(1, homeS) * poisson.pmf(1, awayS)) +
                         (poisson.pmf(2, homeS) * poisson.pmf(0, awayS)) +
                         (poisson.pmf(0, homeS) * poisson.pmf(2, awayS))))
        return round(float(np.array2string(under25)), 5)

If I pass in the arguments as under25(2, 3) the output is 12.4652 which is correct.

I have tried all the functions under scipy.stats.poisson but they all return numpy arrays and I haven't been able to figure out what to do by myself or online.

Is there a shorter way besides this?


Solution

  • You can always pass the values as a numpy array:

    def fn(homeS,awayS):
        S = sum(poisson.pmf(np.array([0,1,0,1,2,0]),homeS)*poisson.pmf(np.array([0,0,1,1,0,2]),awayS))
        return round(float(np.array2string(100*S)), 5)
    
    fn(2,3)
    12.4652