In this post we will learn the basics of generating random numbers from binomial distribution in Python with Numpy. We will use Numpy’s random module’s binomial() function to generatee random numbers with multiple examples.
What is Binomial Distribution
The binomial distribution is a probability distribution that describes the outcomes of a series of independent trials with two outcomes, like Head or Tail and Success or Failure. It is useful modeling processes where. there are fixed. number of independent trials with fixed probability of success.
The binomial distribution is defined by two parameters: the number of trials, n, and the probability of success on a single trial, p. If the probability of success on a single trial is p, then the probability of failure on a single trial is 1 – p.
The probability of the number of successes, x, in the series of n trials in a binomial distribution can be computed using the formula below
P(x) = (n choose x) * p^x * (1-p)^(n-x)
where (n choose x) is called the binomial coefficient, which is calculated as:
(n choose x) = n! / (x! * (n-x)!)
The binomial coefficient represents the number of ways in which x successes can occur in n trials.
The binomial distribution is a discrete distribution. By that we mean the possible values for x are integers from 0 to n.
Generating a single Random Number from Binomial distribution with NumPy Python
The numpy.random.binomial() function takes two parameters: the number of trials and the probability of success for each trial. It returns a random integer from the binomial distribution with the specified number of trials and probability of success.
import numpy as np
We will use Numpy’s Random Generator class by instantiating default_rng() function from random module to generate random numbers.
rng = np.random.default_rng(2022)
Let us generate a random number from a binomial distribution with 10 trials and a probability of success of 0.5. Numpy’s binomial() function can take n, the number of trials and p, the probability of success to generate a random number from binomial distribution.
rng.binomial(n=10, p=0.5)
The output could be any integer between 0 and 10, inclusive, depending on the outcome of the 10 Bernoulli trials.
5
We can also generate a random number from the binomial distribution with 10 trials and a probability of success of 0.5 without specifying the argument names.
rng.binomial(10, 0.5) 3
Generating Random Numbers from Binomial distribution with NumPy Python
Here is another example of how to use the Numpy’s binomial() function using Random Generator Class. And this time we generate multiple random numbers.
In the example below, we generate 5 random numbers from a binomial distribution with n=10 trials and a probability of success of p=0.5:
rng.binomial(n=10, p=0.5, size=5) array([3, 6, 6, 3, 2])
Wee can also generate random numbers from a biased coin by changing the probability of success p. Below, we generate an array of 5 random numbers from the binomial distribution with 10 trials and a probability of success of 0.8.
The output will be an array of 5 integers, each of which could be any integer between 0 and 10, inclusive, depending on the outcome of the 10 Bernoulli trials.
rng.binomial(n=10, p=0.8, size=5) array([5, 9, 8, 9, 6])