Covariance matrix computation

杀马特。学长 韩版系。学妹 提交于 2019-12-10 02:58:39

问题


Input : random vector X=xi, i=1..n.
vector of means for X=meanxi, i=1..n
Output : covariance matrix Sigma (n*n).
Computation :
1) find all cov(xi,xj)= 1/n * (xi-meanxi) * (xj-meanxj), i,j=1..n
2) Sigma(i,j)=cov(xi,xj), symmetric matrix.
Is this algorithm correct and has no side-effects?


回答1:


Each xi should be a vector (random variable) with it's own variance and mean.

Covariance matrix is symmetric, so you just need to compute one half of it (and copy the rest) and has variance of xi at main diagonal.

 S = ...// your symmetric matrix n*n
 for(int i=0; i<n;i++)
   S(i,i) = var(xi);
   for(j = i+1; j<n; j++)
     S(i,j) = cov(xi, xj);
     S(j,i) = S(i,j);
   end
 end

where variance (var) of xi:

v = 0;
for(int i = 0; i<xi.Count; i++)
  v += (xi(i) - mean(xi))^2;
end
v = v / xi.Count;

and covariance (cov)

cov(xi, xj) = r(xi,xj) * sqrt(var(xi)) * sqrt(var(xj))

where r(xi, xj) is Pearson product-moment correlation coefficient

EDIT
or, since cov(X, Y) = E(X*Y) - E(X)*E(Y)

cov(xi, xj) = mean(xi.*xj) - mean(xi)*mean(xj);

where .* is Matlab-like element-wise multiplication.
So if x = [x1, x2], y = [y1, y2] then z = x.*y = [x1*y1, x2*y2];



来源:https://stackoverflow.com/questions/3307082/covariance-matrix-computation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!