How can I generate two correlated random vectors with values drawn from a normal distribution?
조회 수: 58 (최근 30일)
이전 댓글 표시
I would like to generate two normally distributed random vectors with a specified correlation.
채택된 답변
MathWorks Support Team
2011년 1월 25일
The idea is to generate a random matrix M with 2 columns (using RANDN) corresponding to the 2 vectors that are to exhibit the desired correlation. That is, the elements of these vectors are drawn from a standard normal distribution. Multiplying M with sigma and adding mu yields a matrix with values drawn from a normal distribution with mean mu and variance sigma^2.
As can be seen from the code below, the trick is to multiply M with the upper triangular matrix L obtained from the Cholesky decomposition of the desired correlation matrix R (which is trivially symmetric and positive definite) in order to set the correlation as needed. In this particular example, the desired correlation is 0.75.
mu = 50
sigma = 5
M = mu + sigma*randn(1000,2);
R = [1 0.75; 0.75 1];
L = chol(R)
M = M*L;
x = M(:,1);
y = M(:,2);
corr(x,y)
The correlation of the resulting vectors can be verified with CORR.
댓글 수: 0
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!