how to Use the Multinomial Distribution in Python

The multinomial distribution in Python can be used to calculate the probability of getting a certain number of outcomes in a multi-category experiment. It is implemented in the scipy.stats.multinomial class, which takes a list of probabilities associated with each event, the number of trials, and the number of outcomes as the parameters. It then provides methods to calculate the probability of getting a certain number of outcomes. This can be used to calculate the probability of outcomes in a wide variety of situations, such as in sports, elections, and business decisions.


The describes the probability of obtaining a specific number of counts for k different outcomes, when each outcome has a fixed probability of occurring.

If a  X follows a multinomial distribution, then the probability that outcome 1 occurs exactly x1 times, outcome 2 occurs exactly x2 times, etc. can be found by the following formula:

Probability = n! * (p1x1 * p2x2 * … * pkxk) /  (x1! * x2! … * xk!)

where:

  • n: total number of events
  • x1number of times outcome 1 occurs
  • p1probability that outcome 1 occurs in a given trial

The following examples show how to use the function in Python to answer different probability questions regarding the multinomial distribution.

Example 1

In a three-way election for mayor, candidate A receives 10% of the votes, candidate B receives 40% of the votes, and candidate C receives 50% of the votes.

If we select a random sample of 10 voters, what is the probability that 2 voted for candidate A, 4 voted for candidate B, and 4 voted for candidate C?

We can use the following code in Python to answer this question:

from scipy.stats import multinomial

#calculate multinomial probability
multinomial.pmf(x=[2, 4, 4], n=10, p=[.1, .4, .5])

0.05040000000000001

The probability that exactly 2 people voted for A, 4 voted for B, and 4 voted for C is 0.0504.

Example 2

Suppose an urn contains 6 yellow marbles, 2 red marbles, and 2 pink marbles.

If we randomly select 4 balls from the urn, with replacement, what is the probability that all 4 balls are yellow?

We can use the following code in Python to answer this question:

from scipy.stats import multinomial

#calculate multinomial probability
multinomial.pmf(x=[4, 0, 0], n=4, p=[.6, .2, .2])

0.1295999999999999

Example 3

Suppose two students play chess against each other. The probability that student A wins a given game is 0.5, the probability that student B wins a given game is 0.3, and the probability that they tie in a given game is 0.2.

If they play 10 games, what is the probability that player A wins 4 times, player B wins 5 times, and they tie 1 time?

We can use the following code in Python to answer this question:

from scipy.stats import multinomial

#calculate multinomial probability
multinomial.pmf(x=[4, 5, 1], n=10, p=[.5, .3, .2])

0.03827249999999997

The probability that player A wins 4 times, player B wins 5 times, and they tie 1 time is about 0.038.

The following tutorials provide additional information about the multinomial distribution:

x