MultinomialDistribution
statistics: MultinomialDistribution
Multinomial probability distribution object.
A MultinomialDistribution object consists of parameters, a model
description, and sample data for a multinomial probability distribution.
The multinomial distribution is a discrete probability distribution that models the outcomes of n independent trials of a k-category system, where each trial has a probability of falling into each category. It is defined by the vector of probabilities for each outcome.
There are several ways to create a MultinomialDistribution object.
makedist function.
MultinomialDistribution (Probabilities)
to create a multinomial distribution with specified parameter values.
It is highly recommended to use the makedist function to create
probability distribution objects, instead of the constructor.
Further information about the multinomial distribution can be found at https://en.wikipedia.org/wiki/Multinomial_distribution
See also: makedist, mnpdf, mnrnd
Source Code: MultinomialDistribution
A row vector of probabilities for each outcome. You can access the
Probabilities property using dot name assignment.
## Create a Multinomial distribution with default parameters
pd = MultinomialDistribution ();
## Query parameter 'Probabilities' (outcome probabilities)
pd.Probabilities
## Use this to query the vector of probabilities for each outcome in a
## Multinomial distribution. The probabilities must sum to 1 and represent
## the likelihood of each category in a single trial.
ans =
0.5000 0.5000
|
## Create a Multinomial distribution with specified parameters
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
## Query parameter 'Probabilities'
pd.Probabilities
## Set parameter 'Probabilities'
pd.Probabilities = [0.4, 0.3, 0.3]
## Use this to initialize or modify the probabilities vector in a Multinomial
## distribution. The vector must be positive real scalars summing to 1, useful
## for modeling categorical outcomes like dice rolls or survey responses.
ans =
0.1000 0.2000 0.3000 0.2000 0.1000 0.1000
pd =
MultinomialDistribution
Probabilities:
0.4000 0.3000 0.3000
|
## Create a Multinomial distribution object by calling its constructor
pd = MultinomialDistribution ([1/6, 1/6, 1/6, 1/6, 1/6, 1/6]);
## Query parameter 'Probabilities'
pd.Probabilities
## This demonstrates direct construction with specific probabilities,
## ideal for modeling fair or biased categorical events, such as a dice roll.
ans =
0.1667 0.1667 0.1667 0.1667 0.1667 0.1667
|
A character vector specifying the name of the probability distribution object. This property is read-only.
A scalar integer value specifying the number of parameters characterizing the probability distribution. This property is read-only.
A cell array of character vectors with each element containing the name of a distribution parameter. This property is read-only.
A cell array of character vectors with each element containing a short description of a distribution parameter. This property is read-only.
A numeric vector containing the values of the distribution
parameters. This property is read-only. You can change the distribution
parameters by assigning new values to the Probabilities
property.
A numeric vector specifying the truncation interval for the
probability distribution. First element contains the lower boundary,
second element contains the upper boundary. This property is read-only.
You can only truncate a probability distribution with the
truncate method.
A logical scalar value specifying whether a probability distribution is truncated or not. This property is read-only.
MultinomialDistribution: p = cdf (pd, x)
MultinomialDistribution: p = cdf (pd, x, "upper")
p = cdf (pd, x) computes the CDF of the
probability distribution object, pd, evaluated at the values in
x.
p = cdf (…, returns the complement of
the CDF of the probability distribution object, pd, evaluated at
the values in x.
"upper")
## Plot various CDFs from the Multinomial distribution
x = 1:0.1:6;
pd1 = MultinomialDistribution ([0.4, 0.3, 0.3]);
pd2 = MultinomialDistribution ([0.2, 0.2, 0.2, 0.2, 0.2]);
pd3 = MultinomialDistribution ([1/6, 1/6, 1/6, 1/6, 1/6, 1/6]);
p1 = cdf (pd1, x);
p2 = cdf (pd2, x);
p3 = cdf (pd3, x);
plot (x, p1, "-b", x, p2, "-g", x, p3, "-r")
grid on
legend ({"3 outcomes", "5 outcomes", "6 outcomes (dice)"}, ...
"location", "southeast")
title ("Multinomial CDF")
xlabel ("values in x (outcome index)")
ylabel ("Cumulative probability")
## Use this to compute and visualize the cumulative distribution function
## for different Multinomial distributions, showing how probability
## accumulates across categorical outcomes, useful in decision analysis.
|
MultinomialDistribution: p = icdf (pd, p)
p = icdf (pd, x) computes the quantile (the
inverse of the CDF) of the probability distribution object, pd,
evaluated at the values in x.
## Plot various iCDFs from the Multinomial distribution
p = 0.001:0.001:0.999;
pd1 = MultinomialDistribution ([0.4, 0.3, 0.3]);
pd2 = MultinomialDistribution ([0.2, 0.2, 0.2, 0.2, 0.2]);
pd3 = MultinomialDistribution ([1/6, 1/6, 1/6, 1/6, 1/6, 1/6]);
x1 = icdf (pd1, p);
x2 = icdf (pd2, p);
x3 = icdf (pd3, p);
plot (p, x1, "-b", p, x2, "-g", p, x3, "-r")
grid on
legend ({"3 outcomes", "5 outcomes", "6 outcomes (dice)"}, ...
"location", "northwest")
title ("Multinomial iCDF")
xlabel ("Probability")
ylabel ("values in x (outcome index)")
## This demonstrates the inverse CDF (quantiles) for Multinomial
## distributions, useful for finding outcome thresholds corresponding to
## given probabilities, such as in risk assessment.
|
MultinomialDistribution: r = iqr (pd)
r = iqr (pd) computes the interquartile range of the
probability distribution object, pd.
## Compute the interquartile range for a Multinomial distribution
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
iqr_value = iqr (pd)
## Use this to calculate the interquartile range, which measures the spread
## of the middle 50% of the categorical outcomes, helpful for understanding
## central variability in discrete data.
iqr_value = 2
|
MultinomialDistribution: m = mean (pd)
m = mean (pd) computes the mean of the probability
distribution object, pd.
## Compute the mean for different Multinomial distributions
pd1 = MultinomialDistribution ([0.4, 0.3, 0.3]);
pd2 = MultinomialDistribution ([0.2, 0.2, 0.2, 0.2, 0.2]);
mean1 = mean (pd1)
mean2 = mean (pd2)
## This shows how to compute the expected value for Multinomial
## distributions with different probabilities, representing the average
## outcome index in categorical data.
mean1 = 1.9000
mean2 = 3
|
MultinomialDistribution: m = median (pd)
m = median (pd) computes the median of the probability
distribution object, pd.
## Compute the median for different Multinomial distributions
pd1 = MultinomialDistribution ([0.4, 0.3, 0.3]);
pd2 = MultinomialDistribution ([0.2, 0.2, 0.2, 0.2, 0.2]);
median1 = median (pd1)
median2 = median (pd2)
## Use this to find the median outcome index, which splits the distribution
## into two equal probability halves, robust to uneven probabilities.
median1 = 2
median2 = 3
|
MultinomialDistribution: y = pdf (pd, x)
y = pdf (pd, x) computes the PDF of the
probability distribution object, pd, evaluated at the values in
x.
## Plot various PDFs from the Multinomial distribution
x = 1:0.1:6;
pd1 = MultinomialDistribution ([0.4, 0.3, 0.3]);
pd2 = MultinomialDistribution ([0.2, 0.2, 0.2, 0.2, 0.2]);
pd3 = MultinomialDistribution ([1/6, 1/6, 1/6, 1/6, 1/6, 1/6]);
y1 = pdf (pd1, x);
y2 = pdf (pd2, x);
y3 = pdf (pd3, x);
plot (x, y1, "-b", x, y2, "-g", x, y3, "-r")
grid on
legend ({"3 outcomes", "5 outcomes", "6 outcomes (dice)"}, ...
"location", "northeast")
title ("Multinomial PDF")
xlabel ("values in x (outcome index)")
ylabel ("Probability")
## This visualizes the probability mass function for Multinomial
## distributions, showing the likelihood for each discrete outcome.
|
MultinomialDistribution: plot (pd)
MultinomialDistribution: plot (pd, Name, Value)
MultinomialDistribution: h = plot (…)
plot (pd plots a probability density function (PDF) of the
probability distribution object pd. If pd contains data,
which have been fitted by fitdist, the PDF is superimposed over a
histogram of the data.
plot (pd, Name, Value) specifies additional
options with the Name-Value pair arguments listed below.
| Name | Value | |
|---|---|---|
"PlotType" | A character vector specifying the plot
type. "pdf" plots the probability density function (PDF). When
pd is fit to data, the PDF is superimposed on a histogram of the
data. "cdf" plots the cumulative density function (CDF). When
pd is fit to data, the CDF is superimposed over an empirical CDF.
"probability" plots a probability plot using a CDF of the data
and a CDF of the fitted probability distribution. This option is
available only when pd is fitted to data. | |
"Discrete" | A logical scalar to specify whether to
plot the PDF or CDF of a discrete distribution object as a line plot or a
stem plot, by specifying false or true, respectively. By
default, it is true for discrete distributions and false
for continuous distributions. When pd is a continuous distribution
object, option is ignored. | |
"Parent" | An axes graphics object for plot. If
not specified, the plot function plots into the current axes or
creates a new axes object if one does not exist. |
h = plot (…) returns a graphics handle to the plotted
objects.
## Create a Multinomial distribution with fixed parameters and plot its PDF.
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
plot (pd)
title ("Multinomial distribution PDF")
|
## Create a Multinomial distribution and plot its CDF.
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
plot (pd, "PlotType", "cdf")
title ("Multinomial distribution CDF")
## Use this to visualize the cumulative distribution function,
## useful for understanding probability accumulation across outcomes.
|
MultinomialDistribution: y = random (pd)
MultinomialDistribution: y = random (pd, rows)
MultinomialDistribution: y = random (pd, rows, cols, …)
MultinomialDistribution: y = random (pd, [sz])
r = random (pd) returns a random number from the
distribution object pd.
When called with a single size argument, mnrnd returns a square
matrix with the dimension specified. When called with more than one
scalar argument, the first two arguments are taken as the number of rows
and columns and any further arguments specify additional matrix
dimensions. The size may also be specified with a row vector of
dimensions, sz.
## Generate random samples from a Multinomial distribution
rand ("seed", 21);
pd = MultinomialDistribution ([1/6, 1/6, 1/6, 1/6, 1/6, 1/6]);
samples = random (pd, 500, 1);
hist (samples, 6)
title ("Histogram of 500 random samples from Multinomial (fair dice)")
xlabel ("values in x (outcome index)")
ylabel ("Frequency")
## This generates random categorical outcomes from a Multinomial
## distribution, useful for simulating events like dice rolls or classifications.
|
MultinomialDistribution: s = std (pd)
s = std (pd) computes the standard deviation of the
probability distribution object, pd.
## Compute the standard deviation for a Multinomial distribution
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
std_value = std (pd)
## Use this to calculate the standard deviation, which measures the variability
## in the outcome indices for categorical data.
std_value = 1.4177
|
MultinomialDistribution: t = truncate (pd, lower, upper)
t = truncate (pd) returns a probability distribution
t, which is the probability distribution pd truncated to the
specified interval with lower limit, lower, and upper limit,
upper. If pd is fitted to data with fitdist, the
returned probability distribution t is not fitted, does not contain
any data or estimated values, and it is as it has been created with the
makedist function, but it includes the truncation interval.
## Plot the PDF of a Multinomial distribution truncated at [2, 5] intervals.
## Generate 10000 random samples from this truncated distribution and
## superimpose a histogram.
rand ("seed", 21);
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
t = truncate (pd, 2, 5);
data_all = random (pd, 20000, 1);
data = data_all(data_all >= 2 & data_all <= 5);
data = data(1:10000);
## Plot histogram and truncated PDF
x = 2:5;
y = pdf (t, x);
plot (x, y * numel (data), "bo-")
hold on
hist (data, 4)
hold off
title ("Multinomial distribution truncated at [2, 5]")
legend ("Truncated PDF", "Histogram")
## This demonstrates truncating a Multinomial distribution to a specific
## range of outcomes and visualizing the resulting distribution with random samples.
|
MultinomialDistribution: v = var (pd)
v = var (pd) computes the standard deviation of the
probability distribution object, pd.
## Compute the variance for a Multinomial distribution
pd = MultinomialDistribution ([0.1, 0.2, 0.3, 0.2, 0.1, 0.1]);
var_value = var (pd)
## Use this to calculate the variance, which quantifies the spread of the
## outcome indices in the distribution.
var_value = 2.0100
|
probs = [0.1, 0.2, 0.3, 0.2, 0.1, 0.1];
pd = makedist ("Multinomial", "Probabilities", probs);
rand ("seed", 2);
data = random (pd, 5000, 1);
hist (data, length (probs));
hold on
x = 1:length (probs);
y = pdf (pd, x) * 5000;
stem (x, y, "r", "LineWidth", 2);
hold off
msg = "Multinomial distribution with Probabilities = [%s]";
probs_str = num2str (probs, "%0.1f ");
title (sprintf (msg, probs_str)) |