Covariance matrix of two matrices- how to calculate

2.5k Views Asked by At

In maximum covariance analysis, to extract correlated columns, it is asked to calculate the covariance matrix. For two vectors, corvariance matrix is understood, COV(v1,v2) = v1*v2'

How do I calculate Covariance matrix of two matrices? I failed to find a definition to compute it. Thanks in advance for help.

1

There are 1 best solutions below

0
On

Here is some code the will generate the covariance matrix using Matlab built in function of coding it up yourself.

Output:

meanx =

   -0.2320
    0.0400


CX =

   26.0522    5.8712
    5.8712   26.2326


meany =

   -0.1923
   -0.1358


CY =

   20.2713   -0.0902
   -0.0902   32.0136

Code:

clear all 
close all

sim = 1000;                    % number of order pairs to generate for
                               % X_1 and X_2
x = zeros(2, sim);             % pre-allocating x

% generating the order pairs with specified probabilities
for m = 1:sim
    u = rand(1,1);
    if u <= 0.25
        x(1, m) = -8;
        x(2, m) = 0;
    elseif u > 0.25 && u <= 0.5
        x(1, m) = 0;
        x(2, m) = -8;
    elseif u > 0.5 && u <= 0.75
        x(1, m) = 2;
        x(2, m) = 6;
    else
        x(1, m) = 6;
        x(2, m) = 2;
    end
end

% est mean of x
meanx = [sum(x(1, :))/sim, sum(x(2, :))/sim]'

% covariance code uncomment to see it gives the same the results 
% CX = zeros(2,2);               % pre-allocating est Cx
% xbar= zeros(2, sim);           % pre-allocating xbar
%
% % est covariance matrix of X by eq 9.46
% for m = 1:sim 
%     xbar(:, m) = x(:, m) - meanx;
%     CX = CX + xbar(:, m)*xbar(:, m)'/sim;
% end
% 
% CX
CX = cov(x.')                  % determines the covariance using Matlab's 
                               % built in cov 

% A is the eigenvector matrix of Cx given
A = [1/sqrt(2), -1/sqrt(2); 
     1/sqrt(2), 1/sqrt(2)];
y = zeros(2, sim);             % pre-allocating y

% transform random vector X by Y = AX
for m = 1:sim 
    y(:, m) = A*x(:, m);
end

% est mean of y
meany = [sum(y(1, :))/sim, sum(y(2, :))/sim]'

% covariance code uncomment to see it gives the same the results
% CY = zeros(2, 2);
% ybar = zeros(2, sim);          % pre-allocating ybar         
% 
% % est covariance matrix of Y by eq 9.46
% for m = 1:sim 
%     ybar(:, m) = y(:, m) - meany;
%     CY = CY + ybar(:, m)*ybar(:, m)'/sim;
% end
% 
% CY
CY = cov(y.')                  % determines the covariance using Matlab's 
                               % built in cov