Multivariate normal random numbers vs. random numbers from normal distribution

5 views (last 30 days)
I sampled random numbers using both samples from the multivariate normal distribution and samples from the normal distribution with specified mean and variance.
I have a mean vector mu and a covariance matrix sigma:
mu = [-0.25, 0.03, 0.01]'
sigma = eye(length(mu))
Using multivariate normal random numbers, I get the following results:
rng default % for reproducibility
R = mvnrnd(mu, sigma, 3)
R =
0.2877 0.8922 -0.4236
1.5839 0.3488 0.3526
-2.5088 -1.2777 3.5884
Using random numbers from the normal distribution with a specific mean (b) and variance (a), I get the following results:
a = 1; % std
b = mu(1); % mean
rng default
y = a.*randn(3,1) + b
y =
0.2877 0.5677
1.5839 1.8639
-2.5088 -2.2288
To create the second column, I used b = mu(2).
I wonder why the results are different in these cases. For the first column the result is the same, but from the second column it changes. Shouldn't this be the same? If not, what is the right way to draw random numbers for the mean vector mu.

Accepted Answer

Steven Lord
Steven Lord on 28 May 2021
So you did this?
mu = [-0.25, 0.03, 0.01]';
a = 1; % std
b = mu(1); % mean
rng default
y(:, 1) = a.*randn(3,1) + b;
rng default
y(:, 2) = a.*randn(3, 1) + mu(2)
y = 3×2
0.2877 0.5677 1.5839 1.8639 -2.5088 -2.2288
That second column isn't exactly random. Given the first column and the mu vector I can tell you exactly what the second column will be. I can even compute it without any additional randn calls.
y(:, 3) = y(:, 1) + (mu(2)-mu(1))
y = 3×3
0.2877 0.5677 0.5677 1.5839 1.8639 1.8639 -2.5088 -2.2288 -2.2288
norm(y(:, 3) - y(:, 2)) % Effectively equal
ans = 4.4409e-16
Now if you'd left out that second call to rng default or written it so the second column was generated using the state of the generator where the first one "left off":
rng default
z(:, 1) = a.*randn(3, 1) + mu(1);
z(:, 2) = a.*randn(3, 1) + mu(2)
z = 3×2
0.2877 0.8922 1.5839 0.3488 -2.5088 -1.2777
rng default
z = a.*randn(3, 2) + mu(1:2).' % Using implicit expansion
z = 3×2
0.2877 0.8922 1.5839 0.3488 -2.5088 -1.2777

More Answers (1)

Paul
Paul on 28 May 2021
I'm not quite sure what you did to create y, since the code generates y as a single column.
It looks like y was produced by:
mu = [-0.25, 0.03, 0.01]';
sigma = eye(length(mu));
y = zeros(3,2);
rng default
y(:,1) = randn(3,1)+mu(1);
rng default
y(:,2) = randn(3,1)+mu(2)
y = 3×2
0.2877 0.5677 1.5839 1.8639 -2.5088 -2.2288
However, that's not correct because y(:,2) needs to be generated from samples of RVs that are independent of those used to generate y(:,1).
The output from mvnrnd() in this example can be reconstructed as follows using nine samples of randn (three samples of a 3-dimensional random vector) :
rng default % for reproducibility
R = mvnrnd(mu, sigma, 3)
R = 3×3
0.2877 0.8922 -0.4236 1.5839 0.3488 0.3526 -2.5088 -1.2777 3.5884
rng default
R1 = randn(3,3) + mu.'
R1 = 3×3
0.2877 0.8922 -0.4236 1.5839 0.3488 0.3526 -2.5088 -1.2777 3.5884
R - R1
ans = 3×3
0 0 0 0 0 0 0 0 0

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!