How can I generate two correlated random vectors with values drawn from a normal distribution?

300 ビュー (過去 30 日間)
I would like to generate two normally distributed random vectors with a specified correlation.


MathWorks Support Team
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);
The correlation of the resulting vectors can be verified with CORR.
  4 件のコメント
Jaime De La Mota Sanchis
Jaime De La Mota Sanchis 2021 年 8 月 5 日
This is very interesting. Is there a way to produce 2D random fields like the ones resulting from the random field simulation code instead of two random vectors?
I have tried to simply reshape the resulting vector into a square but that doesn't seem to work


その他の回答 (4 件)

Makarand 2018 年 7 月 18 日
Chol Might fail if covarince matrix is singular or near singular. so use svd I do it as follows where is mu is mean of required random variables.
[U S V]=svd(Sigma);
s=randn(n, d) * S * U'+mu(ones(n,1),:);

Muhammad Adil
Muhammad Adil 2021 年 5 月 16 日
Will this method generate two correlated vector from normal distribution? What if some other distribution is required?
  2 件のコメント
Muhammad Adil
Muhammad Adil 2021 年 5 月 17 日
I have to generate correlated vectors V1 and V2 both from generalized gamma distribution for which the CDF is available. HOw should I proceed? The CDF is:
(1/gamma(m))*(gamma(m)-igamma(m, (m/Omeg)*x^beta))
for m>=1/2
beta and omega positive


Muhammad Adil
Muhammad Adil 2021 年 5 月 17 日
How can I generate two correlated random vectors with values drawn from a Generalized Gamma distribution with CDF (1/gamma(m))*(gamma(m)-igamma(m, (m/Omeg)*x^beta))?

P. Shankar
P. Shankar 2021 年 8 月 25 日
You can generate a pair of correlated gamma variables first and then ssale each of them with (1/beta). This will result in a pair of correlated gamma variables with parameters (a, b, beta) and correlation coefficient rho.
%% correlated generalized (identical) gamma variables (a, b, nu)
clear;clc;close all
rho=0.7;% correlation of the pair
u=copularnd('Gaussian',rho,100); % a correlated pair of U[0,1]
u1=u(:,1); u2=u(:,2); % U(0,1)
% generate a pair of gamma random variables using the notion of the inverse of CDF
% x and y are gamma pair
x=icdf(fname,u1,a1,b1); y=icdf(fname,u2,a2,b2);
% create the generalized gamma pair
x=x.^(1/beta); y=y.^(1/beta);
hist([x,y]),title('correlated pair of generalized gamma variates')


Community Treasure Hunt

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

Start Hunting!

Translated by