Multivariate normal random numbers vs. random numbers from normal distribution
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Salomon Roth
am 28 Mai 2021
Beantwortet: Steven Lord
am 28 Mai 2021
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.
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 28 Mai 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)
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))
norm(y(:, 3) - y(:, 2)) % Effectively equal
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)
rng default
z = a.*randn(3, 2) + mu(1:2).' % Using implicit expansion
0 Kommentare
Weitere Antworten (1)
Paul
am 28 Mai 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)
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)
rng default
R1 = randn(3,3) + mu.'
R - R1
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!