0
$\begingroup$

I'm trying to generate a covariance matrix between two multivariate vectors with specified variances for each dimension, correlations between dimensions within a single vector, and cross-correlations between dimensions of the two vectors. When I try to generate multivariate normal data using this covariance matrix, I get a matrix not positive definite (PSD) error. Can someone please help me figure out why my matrix is not PSD? My Matlab code is below.

D = 2; % number of X variables
P = 3; % number of Y variables
N = 1000; % number of data points

%%% Choose variances of x's and y's
stdX = gamrnd(1, 1, D, 1);
stdY = gamrnd(1, 1, P, 1);

%%% Choose correlations between x's and y's
corrX = corrcov(wishrnd(eye(D), D+1));
corrY = corrcov(wishrnd(eye(P), P+1));
corrXY = unifrnd(-1, 1, D*P);

%%% Construct covariance matrix for x's
SigXX = nan(D);
for i = 1:D
    for j = 1:D
        if i == j
            SigXX(i,j) = stdX(i).^2;
        else
            SigXX(i,j) = corrX(i,j).*stdX(i)*stdX(j);
        end
    end
end

%%% Construct covariance matrix for y's
SigYY = nan(P);
for i = 1:P
    for j = 1:P
        if i == j
            SigYY(i,j) = stdY(i).^2;
        else
            SigYY(i,j) = corrY(i,j).*stdY(i)*stdY(j);
        end
    end
end

%%% Construct cross-covariance matrix
SigXY = nan(D, P);
for i = 1:D
    for j = 1:P
        SigXY(i,j) = corrXY(i,j)*stdX(i)*stdY(j);
    end
end

%%% Construct full covariance matrix
Sig = [SigXX SigXY; SigXY' SigYY];

%%% Generate fake data
data = mvnrnd(zeros(D+P, 1), Sig, N);
$\endgroup$
3
  • $\begingroup$ It's not PSD because you aren't using a procedure that will always produce valid correlation matrix. You can't specify entries arbitrarily. $\endgroup$
    – whuber
    Commented Mar 21, 2018 at 22:15
  • $\begingroup$ Thanks, @whuber. Are you referring to the lines where I generate Wishart random variables and then convert the draws into correlation matrices? If so, why is that not an acceptable way to specify correlations? $\endgroup$ Commented Mar 21, 2018 at 22:17
  • $\begingroup$ Please see the answers to the duplicate threads. $\endgroup$
    – whuber
    Commented Mar 21, 2018 at 22:19

0

Browse other questions tagged or ask your own question.