Several draws from multivariate normal distribution

조회 수: 7 (최근 30일)
MRC
MRC 2014년 8월 11일
편집: John D'Errico 2014년 8월 11일
Let
MU=[1 2; 3 4; 5 6]
SIGMA=[2 0; 0 2]
I want to write one or two lines of code to draw R=10 unobservables from Normal((MU(1,:),SIGMA), Normal((MU(2,:),SIGMA), Normal((MU(3,:),SIGMA) without looping and store the results in a matrix 3x(R*2).

채택된 답변

Christopher Berry
Christopher Berry 2014년 8월 11일
Cris,
Since your sigma matrix is diagonal, there is no need to use a multivariate distribution - your variables are completely independent - so what you are asking for is the same as selecting 10 samples each from 6 independent single variable normal distributions.
If this is truly the case, you can then create 3x20 matrices of mu and sigma and call normrnd like this:
mu = [repmat([1;3;5],[1,10]) repmat([2;4;6],[1,10])];
sigma = 2*ones(3,20);
normrnd(mu,sigma,3,20);
This will give you your 3x20 matrix, but will only work with diagonal co-variance matrices sigma.

추가 답변 (1개)

Christopher Berry
Christopher Berry 2014년 8월 11일
The function you are looking for is mvnrnd. You will still have to call mvrnd one distribution at a time, and hence looping would probably make the most sense, especially for more than 3 distributions.
mu = [1 2;3 4;5 6];
SIGMA = [2 0;0 2];
rng('default'); % For reproducibility
r1 = mvnrnd(mu(1,:),SIGMA,10);
r2 = mvnrnd(mu(2,:),SIGMA,10);
r3 = mvnrnd(mu(3,:),SIGMA,10)
R = [r1(:)';r2(:)';r3(:)']
The final output R will be 3x20 and have var1 values in columns 1:10 and var2 values in 11:20.
  댓글 수: 2
MRC
MRC 2014년 8월 11일
I know how to do it in a separate way. I would like to have some lines of code to do all the draws at the same time without looping.
John D'Errico
John D'Errico 2014년 8월 11일
편집: John D'Errico 2014년 8월 11일
No, you don't need to call mvnrnd multiple times. Once will suffice. Simply supply a diagonal covariance matrix and a vector of means.
As easily, this is trivial to do using just randn.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by