What does the np.random.binomial function return?


#1

Question

In Numpy, what does the np.random.binomial() function return?

Answer

The function returns a list of samples from a binomial distribution based on the inputted parameters when calling np.random.binomial(n, p, size).

For example,

# n = 500 (samples or trials)
# p = 0.5 (probability of success)
# size = 10000 (number of experiments)

tests = np.random.binomial(500, 0.5, size=10000)

print(tests)
# The result is 10000 randomly selected 
# experiments from the distribution
# [241, 262, ..., 248, 255, 235]

In the example above, the result of tests is consistent with our probability of 50% success. Each value can be calculated as "out of n".

241/500, 262/500, ..., 248/500, 255/500, 235/500
=
0.48, 0.52, ..., 0.496, 0.51, 0.47

which are close to 50%.