5

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?

2 Answers2

14

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.

Robert Kern
  • 13,118
  • 3
  • 35
  • 32
3

You can use scipy.stats.binom.pmf(k) for this. For example:

n = 10
p = 0.3
k = np.arange(0,21)
binomial = scipy.stats.binom.pmf(k,n,p)
print(binomial)
  • Hi Anmol, welcome to StackOverflow. You have not actually asked a question here you have just posted some code. In order to get help from our community please edit your question to add some clarifying detail. It helps if you explain to people what it is you are having trouble with and the help you require. Best of luck. – Simon McClive Sep 10 '18 at 19:24
  • Code seems to be taken from this site : http://bigdata-madesimple.com/how-to-implement-these-5-powerful-probability-distributions-in-python/ – baxx Sep 17 '18 at 12:41