Main Content

mnrnd

Multinomial random numbers

Description

R = mnrnd(n,P) returns random numbers sampled from the multinomial distribution with the number of trials n and outcome probabilities P.

example

R = mnrnd(n,P,m) returns a matrix of m random number vectors sampled from the multinomial distribution defined by n and P, where n is a positive integer and P is a vector.

example

Examples

collapse all

Suppose that you select 100 balls at random from a bin where 20% of the balls are red, 30% are blue, and 50% are green. After each selection, you return the ball to the bin. Generate a single random vector from the multinomial distribution that represents the number of balls drawn of each color.

rng(0,"twister") % For reproducibility
r = mnrnd(100,[0.2 0.3 0.5])
r = 1×3

    20    26    54

The random vector indicates that 20 red balls, 26 blue balls, and 54 green balls were drawn.

Define multinomial probability distributions for three different processes, where each process has four possible outcomes.

P = [1/4 1/4 1/8 3/8; 1/10 2/10 3/10 2/5; 1/5 1/5 1/5 2/5];

Each row of P contains the outcome probabilities for each process.

Generate a set of random vectors for 100 independent trials of each process.

rng(0,"twister") % For reproducibility
R = mnrnd(100,P)
R = 3×4

    22    24    10    44
    11    24    30    35
    16    22    24    38

Each row of R corresponds to a process, and each column contains the number of instances of a particular outcome.

Generate 50 random vectors from a multinomial distribution with 30 trials and the probability 0.2 for the first outcome, 0.4 for the second outcome, and 0.4 for the third outcome.

rng(0,"twister") % For reproducibility
m = 50;
n = 30;
p = [0.2 0.4 0.4];
Y = mnrnd(n,p,m);

Create a 3-D plot of the random vectors. Add a drop-down line from each marker and add a plane defined by x + y + z = n.

scatter3(Y(:,1),Y(:,2),Y(:,3),"filled")
xlabel("n1")
ylabel("n2")
zlabel("n3")
hold on
% Add drop-down lines
for i = 1:m  
plot3([Y(i,1) Y(i,1)],[Y(i,2) Y(i,2)],[0 Y(i,3)],"k:")
end
xlim([0 n])
ylim([0 n])
zlim([0 n])
% Add a plane
[x,y] = meshgrid(0:2:n, 0:2:n);
z = n - x - y;
surf(x,y,z,FaceColor="interp",EdgeColor="interp",FaceAlpha=0.2)
hold off
view([-155 30])

Figure contains an axes object. The axes object with xlabel n1, ylabel n2 contains 52 objects of type scatter, line, surface.

The coordinates of the plot markers correspond to the frequency of each outcome in an experiment with 30 trials. For example, the marker at [6, 8, 16] represents an experiment with 6 instances of outcome 1, 8 instances of outcome 2, and 16 instances of outcome 3. All of the markers lie in the plane defined by n1 + n2 + n3 = n. Because outcome 1 has the lowest probability, the markers are concentrated on the right side of the plot.

Input Arguments

collapse all

Number of trials for each multinomial outcome, specified as a positive integer or a column vector of positive integers.

If you specify m, then n must be a positive integer.

If you do not specify m, and P is a j-by-k matrix with at least two rows:

  • mnrnd replicates n to a j-by-1 vector when n is a positive integer.

  • n must have j elements when it is a column vector.

Data Types: single | double

Outcome probabilities, specified as one of the following, where k is the number of multinomial bins (or categories) and P must contain only nonnegative scalar values:

  • k-by-1 vector that sums to 1. In this case, mnrnd transposes P into a 1-by-k vector.

  • 1-by-k vector. If n is a column vector, then mnrnd replicates P into a matrix with the same number of rows as n.

  • j-by-k matrix, where each row corresponds to a multinomial probability distribution. If n is a column vector, then P and n must have the same number of rows. If you specify m, you cannot specify P as a matrix.

If a row of P does not sum to 1, the corresponding row in R is NaN.

Data Types: single | double

Number of multinomial random number vectors, specified as a positive integer. If you specify m, then n must be a positive integer, and P must be a vector. Each row of R contains a vector drawn from the multinomial distribution defined by n and P.

Data Types: single | double

Output Arguments

collapse all

Multinomial random vectors, returned as a numeric matrix with the same number of columns as P.

  • If n is a positive integer and P is a row vector, then R contains m rows. If you do not specify m, then R contains only one row. Each row of R contains a vector drawn from the multinomial distribution defined by n and P.

  • If n is a j-by-1 vector or P is a j-by-k matrix, then R contains j rows. Each row of R contains a vector drawn from the multinomial distribution defined by the corresponding rows of n and P.

  • If a row of P does not sum to 1, the corresponding row in R is NaN.

Version History

Introduced in R2006b