Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Multinomial Distribution



What is the Multinomial Distribution?

The 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 −

  • n: The number of trials or experiments.
  • p: A list or array of probabilities for each outcome. The sum of probabilities must equal 1.

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.

Generating Multinomial Samples in NumPy

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.

Example

Here, each row represents the results of a single experiment, showing the number of occurrences for each outcome −

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)

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]]

Understanding the Mathematical Foundation

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,

  • n: Total number of trials.
  • p1, ..., pk: Probabilities of each outcome.
  • x1, ..., xk: Observed frequencies of each outcome.

Understanding this formula helps you interpret the results and validate the samples generated by NumPy.

Simulating Real-World Scenarios

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:

Example 1: Simulating Dice Rolls

In this example, we simulate rolling a six-sided die 20 times and calculate the frequencies of each face −

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)

Each value represents the frequency of a face appearing in the experiment −

Frequencies of dice rolls: [5 2 5 5 3 0]

Example 2: Predicting Election Results

Suppose an election has three candidates, and we want to simulate 100 voter's choices based on predicted probabilities −

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)

This demonstrates how the multinomial distribution can be used in probabilistic scenarios −

Votes for each candidate: [48 31 21]

Visualizing Multinomial Distribution

Visualization is a powerful way to analyze and interpret data. Using Matplotlib, we can plot the results of the multinomial distribution for better understanding.

Example: Bar Plot for Multinomial Results

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()

The result displays a bar plot showing the frequencies of each outcome from the multinomial distribution −

Multinomial Distribution

Comparing Multinomial and Binomial Distributions

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.

Key Differences

Following are the major differences between multinomial and binomial distributions −

  • Number of outcomes: Binomial has two outcomes, while multinomial has more.
  • Complexity: Multinomial requires specifying probabilities for multiple categories.
  • Applications: Multinomial is more versatile for scenarios involving categorical data.

Customizing Multinomial Simulations

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.

Example: Multiple Simulations with Different Probabilities

The following examples demonstrates how adjusting probabilities affects the outcomes −

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}")

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]
Advertisements