# Q：对称半正定协方差矩阵的MATLAB

Hi everybody I have this problem:

• I have Dataset of n vectors each has D dimensions.
• I also have a covariance matrix of size D*D, Let It be C.

I perform the following action:

• I choose K vectors from the dataset, and also choose E dimensions randomly. Let M be the sample covariance of the selected data on the selected dimensions.so M is a E*E matrix.
• let P be the partial covariance matrix corresponding to the dimensions E of C, ie. C(E,E) in matlab

is the following matrix positive semi definite?:

X = (1-a)P + aM

where a is a constant like 0.2.

I sometimes get the following error when using mvnrnd(mean,X) : SIGMA must be a symmetric positive semi-definite matrix

My code is:

%%%Dims are randomly choosen dimensions
%%%Inds are randomly choosen Indexes form {1, 2, ...,n}
%%% PP are n D dimensional vectors, composing my data set PP is n*D
%%% Sigmaa is a D*D covariance matrix
co = cov(PP(Inds,Dims));
me = mean(PP(Inds,Dims));
Bettaa = 0.2;

• I have Dataset of n vectors each has D dimensions.
• I also have a covariance matrix of size D*D, Let It be C.

• I choose K vectors from the dataset, and also choose E dimensions randomly. Let M be the sample covariance of the selected data on the selected dimensions.so M is a E*E matrix.
• let P be the partial covariance matrix corresponding to the dimensions E of C, ie. C(E,E) in matlab

x =（1-a）p是

A是常数0.2。

I sometimes get the following error when using mvnrnd(mean,X) : SIGMA must be a symmetric positive semi-definite matrix

%%%Dims are randomly choosen dimensions
%%%Inds are randomly choosen Indexes form {1, 2, ...,n}
%%% PP are n D dimensional vectors, composing my data set PP is n*D
%%% Sigmaa is a D*D covariance matrix
co = cov(PP(Inds,Dims));
me = mean(PP(Inds,Dims));
Bettaa = 0.2;

Simply looking at the matrix dimensions It is not possible to tell if a matrix is positive semi-definite.

To find out if a given matrix is positive semi-definite, you must check if It's eigenvalues are non-negative and it's symmetry:

symmetry = issymmetric(X);
[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>0) & symmetry
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end

Where X is the matrix you are interested in.

Note that if you use the weaker definition of a positive definite matrix (see Extention for non symmetric matrices section), X does not need to be symmetric and you would end up with:

[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>=0)
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end

symmetry = issymmetric(X);
[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>0) & symmetry
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end

[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>=0)
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end
matlab  matrix  covariance