Error using mvncdf: "SIGMA must be a square, symmetric, positive definite matrix."
Mostrar comentarios más antiguos
When running mvncdf I get an error: "SIGMA must be a square, symmetric, positive definite matrix." Here is my code:
x = [.125,.125,.125,.125,.125,.125,.125,.195,.195,.11,.135,.135,.145];
z = [466.087,480,483.4783,486.9565,466.087,424.3478,...
463.3043,468.2171,390.6977,335.7143,417.094,478.6325,450.4202];
sigx = std(x);
sigz = std(z);
cov = E((x-E(x)).*(z-E(z)));
cor = cov/(sigx*sigz);
X = [1,1];
mu = [mean(x) mean(z)];
sigma = [sigx^2, cor*sigx*sigz; cor*sigx*sigz, sigz^2];
Y = mvncdf([0,0],X,mu,sigma);
Where E is a short function to find the expected value of a random variable:
function expect = E(q)
expect = 0;
for i=1:length(q)
expect = expect + q(i)*normpdf(q(i),mean(q),std(q));
end
end
What is causing this error? As far as I can tell everything should be fine with my sigma matrix.
Respuesta aceptada
Más respuestas (1)
John BG
el 9 de Mzo. de 2017
Josh
your function E doesn't seem to work correctly for some input values, for instance
E([5 -5])
ans =
0
but
E([1 1])
=
NaN
E([1 1 1 1 1])
=
NaN
mean([1 1 1 1 1])
=
1
if instead of E() mean is used, and taking into account that MATLAB definition of cov includes 1/(N-1) here it's 1/12 then replacing
cov = E((x-E(x)).*(z-E(z)));
with
cov = mean((x-mean(x)).*(z-mean(z)))*1/12
then the negative values that generate a negative eigenvalue is 2 orders of magnitude smaller
sigma =
1.0e+03 *
0.000000710897436 -0.000004496636588
-0.000004496636588 1.952922555278590
then correcting (it error can be neglected) values <0 to 0:
sigma_ =
1.0e+03 *
0.000000710897436 0
0 1.952922555278590
Y = mvncdf([0,0],X,mu,sigma_)
Y =
6.151101794521690e-25
or perhaps you would like to consider abridging your code to the following:
clear all;
remove cov as variable otherwise MATLAB function cov doesn't work
x = [.125,.125,.125,.125,.125,.125,.125,.195,.195,.11,.135,.135,.145];
z = [466.087,480,483.4783,486.9565,466.087,424.3478,...
463.3043,468.2171,390.6977,335.7143,417.094,478.6325,450.4202];
mu=mean([x;z],2)
sigma=cov(x,z)
X = [1,1];
Y = mvncdf([0,0],X,mu',sigma)
Y =
6.151102493733966e-25
it returns almost the same result
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
Categorías
Más información sobre Exploration and Visualization en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!