Table of Contents
ToggleThe multinomial distribution is a generalization of the binomial distribution. It represents the probabilities of multiple outcomes in a series of experiments, where each trial has a fixed number of outcomes and a given probability for each. It is defined by two parameters −
For example, consider rolling a six-sided die 10 times. The multinomial distribution can help determine the probabilities of each face appearing a certain number of times.
NumPy provides the numpy.random.multinomial() function to generate random samples from a multinomial distribution. This function requires specifying the number of trials and the probabilities for each outcome.
Here, each row represents the results of a single experiment, showing the number of occurrences for each outcome −
# Open Compiler import numpy as np # Define parameters # Number of trials n = 10 # Probabilities for 4 outcomes p = [0.1, 0.3, 0.4, 0.2] # Generate samples samples = np.random.multinomial(n, p, size=5) print(samples)
Output:
Following is the output obtained −
[[1 2 5 2]
[1 5 2 2]
[0 5 4 1]
[0 4 4 2]
[0 4 4 2]]
The PMF of a multinomial distribution calculates the probability of specific outcomes based on given parameters. Mathematically, it is expressed as −
P(X1 = x1, …, Xk = xk) = (n! / (x1! * … * xk!)) * (p1x1) * … * (pkxk)
Where,
Understanding this formula helps you interpret the results and validate the samples generated by NumPy.
The multinomial distribution is widely used in real-world scenarios such as election results, dice rolls, and genetic probabilities.
Let us simulate a few examples to understand its practical applications:
In this example, we simulate rolling a six-sided die 20 times and calculate the frequencies of each face −
# Open Compiler
import numpy as np
# Simulating dice rolls
# Number of rolls
n = 20
# Equal probability for each face
p = [1/6] * 6
# Generate results
results = np.random.multinomial(n, p)
print("Frequencies of dice rolls:", results)
Output:
Each value represents the frequency of a face appearing in the experiment −
Frequencies of dice rolls: [5 2 5 5 3 0]
Suppose an election has three candidates, and we want to simulate 100 voter’s choices based on predicted probabilities −
# Open Compiler
import numpy as np
# Simulating election results
# Number of voters
n = 100
# Probabilities for each candidate
p = [0.4, 0.35, 0.25]
# Generate election simulation
results = np.random.multinomial(n, p)
print("Votes for each candidate:", results)
Output:
This demonstrates how the multinomial distribution can be used in probabilistic scenarios −
Votes for each candidate: [48 31 21]
Visualization is a powerful way to analyze and interpret data. Using Matplotlib, we can plot the results of the multinomial distribution for better understanding.
import numpy as np
import matplotlib.pyplot as plt
# Visualizing results
outcomes = ['Outcome 1', 'Outcome 2', 'Outcome 3', 'Outcome 4']
frequencies = np.random.multinomial(50, [0.2, 0.3, 0.4, 0.1])
plt.bar(outcomes, frequencies, color='skyblue')
plt.title('Multinomial Distribution Visualization')
plt.xlabel('Outcomes')
plt.ylabel('Frequencies')
plt.show()
Output:
The result displays a bar plot showing the frequencies of each outcome from the multinomial distribution −
Multinomial Distribution
While the binomial distribution deals with two possible outcomes (e.g., success and failure), the multinomial distribution extends this to multiple outcomes.
For instance, while a binomial distribution can model coin tosses, the multinomial distribution can model dice rolls or categorical data with more than two outcomes.
Following are the major differences between multinomial and binomial distributions −
NumPy allows flexibility in customizing simulations by adjusting parameters such as the number of trials, probabilities, and the size of the generated samples.
Let us see an example where we simulate multiple experiments with varying probabilities.
The following example demonstrates how adjusting probabilities affects the outcomes −
# Open Compiler
import numpy as np
# Customizing probabilities
probabilities = [
[0.5, 0.3, 0.2],
[0.4, 0.4, 0.2],
[0.6, 0.2, 0.2]
]
for p in probabilities:
results = np.random.multinomial(20, p)
print(f"Probabilities: {p} => Results: {results}")
Output:
After executing the above code, we get the following output −
Probabilities: [0.5, 0.3, 0.2] => Results: [ 9 10 1]
Probabilities: [0.4, 0.4, 0.2] => Results: [ 4 12 4]
Probabilities: [0.6, 0.2, 0.2] => Results: [12 4 4]
Key Takeaway: Master the multinomial distribution with NumPy at Vista Academy!
