Multivariate normal random numbers vs. random numbers from normal distribution

조회 수: 5 (최근 30일)
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.

채택된 답변

Steven Lord
Steven Lord 2021년 5월 28일
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

추가 답변 (1개)

Paul
Paul 2021년 5월 28일
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

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by