1

In my MATLAB program, I have the occurrences of the m-dimensional variable X given in a matrix as

X = [x_11 x_12 ... x_1m; 
     x_21 x_22 ... x_2m;
     .
     .
     .
     x_n1 x_n2 ... x_nm;]

where each is an case for X.

and the probability of each case is given by:

pX = [p_x1 p_x2 ... p_xn];

I am looking for a simple way to calculate covariance matrix of X (a matrix that shows how the dimensions of X is related to each other).

I hope there is a simple way to calculate it in MATLAB, just like Expected value of X which is calculated by:

EX = pX * X;

Edit:

X is a random variable with discrete occurrences specified by X matrix. pX shows probability of each occurrence.

Isaac
  • 2,332
  • 6
  • 33
  • 59
  • 1
    Your question is not very clear: what is a "probability of occurrence"? based on what? why pX and pY have the dimensions of the columns, while you call them "the probability of occurrence" which is along rows? if you talk about probabilities (and not densities), does it mean x and y are discrete? – Itamar Katz Nov 28 '10 at 10:55
  • I edited to question to fix this issue. But talking about probabilities, the X matrix specifies each occurrence itself while the pX vector specifies probability of that occurrence. – Isaac Nov 28 '10 at 11:07
  • X occurrences are discrete (and the X matrix shows each occurrence) – Isaac Nov 28 '10 at 11:10
  • @karpathy: this is true when probability of each occurrence be same. But in this problem, with have different probability for each occurrence and the weight of each X in covariance calculation is differs. Anyway, it is possible to calculate the covariance using basic definitions and from scratch. But i am looking for a simple and clever way to calculate it. – Isaac Nov 28 '10 at 20:13

1 Answers1

2
EX = px*X;
nmeas = size(X,1);
XB = X - repmat(EX,nmeas,1);

PD = zeros(nmeas, nmeas);
PD(logical(eye(size(PD)))) = px; % thanks http://stackoverflow.com/questions/3963565/matlab-how-to-assign-values-on-the-diagonal

CX = XB'*PD*XB;

PDij is the joint probability of getting measurement xi and xj, which is pXj if j=i, and 0 otherwise.

So

CX1,1 = pX1 * xB11^2 + px2*XB21^2 +...

CX2,1 = CX1,2 = px1 * xB11*xB12 + px2*XB21*XB22 + ...

which is the definition of covariance.

Marc
  • 5,315
  • 5
  • 30
  • 36
  • But I am not sure covariance has square of `pX`. For example look at the Var(X) formula at http://en.wikipedia.org/wiki/Variance#Discrete_case (and variance is special case of covariance right?) – Isaac Nov 30 '10 at 10:11