I am using Python3 to compute the Probability Mass Function (PMF) of this wikipedia example:
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?
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.
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)
来源:https://stackoverflow.com/questions/44034191/probability-mass-function-of-a-binomial-distribution-in-python