# Multinomial Distribution in Python

## In this tutorial you will learn:

• What is Multinomial Distribution?
• Multinomial Distribution Implementation in python
• Visualization of Multinomial Distribution

## Multinomial Distribution

Multinomial Distribution is a probability distribution which is used to calculate the distributions of experiments involving two or more variables/ outcomes. Binomial Distribution is a type of Multinomial Distribution with only two outcomes for example head/ tail and true/ false. In Multinomial Distribution multiple repeated trials are carried out with equal probability of each outcome and with each trial producing a specific outcome, as an example the outcome of tossing a coin can be head or tail.

## Multinomial Distribution in Python

Multinomial distribution in python is implemented using an inbuilt function multinomial() which is included in the random module of NumPy library. The multinomial() function takes in two mandatory parameters and one optional parameters. First parameter “size” is the mandatory parameter and it is size of the output array which could be 1D, 2D, 3D or n-dimensional (depending on the programmer’s requirements). The second parameter is the number of possible outcomes defined by “n”. Third parameter “pvals”, it is the list of probabilities of outcomes and it is a 1D array of size (1, n).
Lets take an example, we will generate a 1D array(1,7) of multinomial distribution with 4 outcomes, with each outcome with the probability of 0.25.

`#importing the random modulefrom numpy import random#applying the multinomial function with 4 outcomes, with probability 0.25 of each outcomeres_arr= random.multinomial(n=4, pvals=[1/4,1/4,1/4,1/4], size=7)#printing the resultsprint('1D array of size(1,7) having having multinomial distribution with 4 outcomes with ¼ probability of each outcome:\n')print(res_arr)`

In this example we will generate a 2D array of multinomial distribution having size (3,5), in the first line of code we are importing random module from numpy library and in third line we are calling the multinomial function with 3 outcomes, with probability of each outcome equal to 1/3
`#importing the random modulefrom numpy import random#here we are using multinomial function to generate multinomial distribution of size 3 x 5 with 3 outcome with probability 1/3 of each outcomeres_arr = random.multinomial (n = 3, pvals=[1/3,1/3,1/3], size=(3,5))print('2D Multinomial Distribution as output from multimonial() function:\n')#printing the resultprint(res_arr)`

Lets take another example, in this example we will generate a 3D array of multinomial distribution of the size(3,3,5) with 8 possible outcomes with probability of 0.125 each
`#importing the random modulefrom numpy import random#here we are using logistic function to generate logistic distribution of size 3 x 3 x 5res = random.multinomial(size=(3,3,5), n = 8, pvals=[1/8,1/8,1/8,1/8,1/8,1/8,1/8,1/8])print('3D Multinomial Distribution as output from multinomial() function:\n')#printing the resultprint(res)`

## Visualization of Multinomial Distribution

In this example we will visualize the Multinomial Distribution with 3 outcomes, with 1/3 probability of each outcome. Here we can observe that the plotted graphs has three peaks which directly correspond with the number of outcomes.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using multinomial function to generate distributions of size 1000 with 3 outcomes each having probability 1/3sb.distplot(random.multinomial(size=1000,n=3,pvals=[1/3,1/3,1/3]), hist=False, label='normal')#plotting the graphmpl.show()`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.