1

enter image description here

I am learning about probability distribution with python and I'm currently looking at binomial distribution and have seen both the scipy way and numpy method, what I'm trying to understand is that why do they have two different output? but are calculating the same thing?

I also have noticed the same pattern for other probability distributions. The code below:

from scipy.stats import binom
data_binom = binom.rvs(n=15,p=0.5,size=15)

import numpy as np
x = np.random.binomial(n=15, p=0.5, size=15)

print(data_binom)
print('-'*10)
print(x)

output:

[ 8  6  7  3 11  8  6  8  7  8  4 10  5  8  7]
----------
[ 4  7  6  8  6  4  7  6  4  7  5 10  8  7  9]

2 Answers2

2

You will get the same output if you use the same seed. For example,

np.random.seed(0)
np.random.binomial(10,0.5,10)

and

np.random.seed(0)
binom.rvs(n=10,p=0.5,size=10)

would both give the same output-> array([5, 6, 5, 5, 5, 6, 5, 7, 8, 5])

1

binom.rvs() and random.binomial() function does not return a distribution, but generates random numbers with binomial distribution. If you rerun your code you will get different numbers for both cases.

Gábor Pálovics
  • 455
  • 4
  • 12