3

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?

Ivanhercaz
  • 731
  • 1
  • 11
  • 31
Gent Bajko
  • 78
  • 1
  • 5

1 Answers1

3

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
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thanks a lot! That was exactly what I was looking for. I tried it earlier but with np.arange but it didn't work. Can you explain how this works? – Gent Bajko May 15 '20 at 23:45
  • 1
    ok for np.arange, it has to be sequential, like np.arange(1,5,step=2) or np.arange(5,1,step=-2). np.array converts a list to an array, so you use that on your list – StupidWolf May 15 '20 at 23:48
  • 1
    you can do poisson.pmf(np.arange(0,5,step=1),1) for example, it will give you probability from 0 to 4.. with lambda 1 – StupidWolf May 15 '20 at 23:50
  • 1
    Thanks. This will probably remove 50 lines of excess code :D – Gent Bajko May 15 '20 at 23:51