Binomial Probability Mass Function in Python

I am using Python3 to calculate the probability weighting function (PMF) of this example on wikipedia:

enter image description here

I tried following this meager documentation:

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

The documentation clearly states:

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.

      

Ok, I tried to implement this with 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 am getting this error 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?

+3


source to share


1 answer


Just call binom.pmf(1, n, p)

to get the result for k=1

. The expression in the documentation just shows you how the PMF is mathematically defined and is not the actual piece of code that you expect to execute.



+3


source







All Articles