Multinomial Distribution

🎲 Multinomial Distribution in Python

The Multinomial Distribution generalizes the binomial distribution to more than two outcomes per trial.
It models the number of occurrences of each outcome in a fixed number of independent trials.


✅ 1. Characteristics of Multinomial Distribution

  • n → number of trials

  • pvals → list of probabilities for each category (sum must = 1)

  • size → number of experiments / samples

  • Returns counts of outcomes for each category

Example Use Cases:

  • Rolling a dice multiple times

  • Drawing colored balls from a bag

  • Simulating categorical outcomes


✅ 2. Generate Multinomial Data Using NumPy

import numpy as np

# Parameters
n = 10 # number of trials
pvals = [0.2, 0.3, 0.5] # probabilities of each category
size = 5 # number of experiments

# Generate multinomial random data
data = np.random.multinomial(n, pvals, size)

print(data)

Output (example):

[[2 3 5]
[1 3 6]
[3 2 5]
[0 4 6]
[2 2 6]]
  • Each row → one experiment of n trials

  • Each column → counts of a category

  • Sum of each row = n


✅ 3. Visualize Multinomial Distribution

Example: 3 categories

import matplotlib.pyplot as plt

# Sum of counts per category across experiments
category_counts = data.sum(axis=0)
categories = ['A', 'B', 'C']

plt.bar(categories, category_counts, color=['red','green','blue'])
plt.title('Multinomial Distribution Counts')
plt.xlabel('Category')
plt.ylabel('Total Counts')
plt.show()

  • Shows total occurrences per category across all experiments


✅ 4. Change Probabilities or Trials

# More trials
data2 = np.random.multinomial(n=20, pvals=[0.1, 0.3, 0.6], size=5)
print(data2)
  • Probabilities and number of trials directly influence the counts

  • Sum of counts per row always = n


✅ 5. Compare Multiple Experiments

# 1000 experiments of 10 trials
data_large = np.random.multinomial(10, [0.2,0.5,0.3], size=1000)

# Average counts per category
avg_counts = data_large.mean(axis=0)
print("Average counts per category:", avg_counts)

  • Average counts approach n * pvals as number of experiments increases


🧠 Summary Table

Function Parameters Description
np.random.multinomial() n, pvals, size Generates counts per category in multinomial experiments
n Number of trials Total trials per experiment
pvals List of probabilities Must sum to 1
size Number of experiments Rows in the output array

🎯 Practice Exercises

  1. Roll a 6-sided dice 10 times, repeat 1000 experiments, and plot average counts.

  2. Simulate drawing balls from a bag with 3 colors (probabilities [0.1,0.3,0.6]) for 20 draws and 5 experiments.

  3. Verify that the sum of counts per row always equals n.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *