Probability Mass Function of a Binomial Distribution in Python

user7120460 picture user7120460 · May 17, 2017 · Viewed 18.4k times · Source

I am using Python3 to compute the Probability Mass Function (PMF) of this wikipedia example:

enter image description here

I tried to follow this scipy documentation:

https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.stats.binom.html

The documentation clearly says:

Notes

The probability mass function for binom is:

binom.pmf(k) = choose(n, k) * p**k * (1-p)**(n-k)

for k in {0, 1,..., n}.

binom takes n and p as shape parameters.

Well, I tried to implement this having the wikipedia example in mind. This is my code:

from scipy.stats import binom

n = 6

p = 0.3

binom.pmf(k) = choose(n, k) * p**k * (1-p)**(n-k)

print  (binom.pmf(1))

However, I get this error's message:

  File "binomial-oab.py", line 7
    binom.pmf(k) = choose(n, k) * p**k * (1-p)**(n-k)
    ^
SyntaxError: can't assign to function call

How can I solve this?

Answer

Robert Kern picture Robert Kern · May 17, 2017

Just call binom.pmf(1, n, p) to get your result for k=1. The expression in the documentation is just showing you how the PMF is mathematically defined and is not an actual code snippet that you are expected to execute.