Multivariate normal random numbers vs. random numbers from normal distribution
6 次查看(过去 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.
0 个评论
采纳的回答
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)
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 个评论
更多回答(1 个)
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)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!