Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!postnews.google.com!c65g2000hsa.googlegroups.com!not-for-mail
From: msmscarlatti@googlemail.com
Newsgroups: sci.stat.math,comp.soft-sys.matlab
Subject: correlation matrix estimation
Date: Fri, 16 May 2008 13:33:42 -0700 (PDT)
Organization: http://groups.google.com
Lines: 49
Message-ID: <e963b995-e578-4c43-b848-711ec847fdf8@c65g2000hsa.googlegroups.com>
NNTP-Posting-Host: 195.7.253.9
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1210970023 29292 127.0.0.1 (16 May 2008 20:33:43 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 16 May 2008 20:33:43 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: c65g2000hsa.googlegroups.com; posting-host=195.7.253.9; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) 
Xref: news.mathworks.com sci.stat.math:82560 comp.soft-sys.matlab:468937


I have written some code which calculates an EWMA estimate for the
correlation matrix of two time series. Suppose that X and Y are our
time series vectors of length N (say N daily observations of two
market interest rates) with X_1 the oldest observation and X_N the
most recent one.

First the code transforms X and Y by subtracting off the average, i.e.

X --> X - E(X)
Y --> Y - E(Y)

Then it concatenates X and Y forming a (Nx2) matrix, let's call it Z.
Then it multiplies the ith row by lambda*(N-i). The covariance matrix
is calculated by

Cov = (1-lambda) * Z' * Z

Finally, the correlation matrix is computed in the usual way by

Corr = S*Cov*S

where S is a diagonal matrix whose elements are given by 1/
sqrt(Cov_ii).

My code seems to work fine for small N, but when I try it for very
large N (several hundreds) I get nonsensical results. Have I missed
something obvious?

I include my MATLAB code below.

function [CovMat,CorrMat]=EWMACovariance(X,Y,lambda)
% COVARIANCE Computes covariances and correlations
n=size(X,1);
m=mean(X); % Compute the means
X=X-m(ones(n,1),:); % Subtract the means
m = mean(Y);
Y=Y-m(ones(n,1),:);
lambdaVector = zeros(n,1);
for i = 1:n
   lambdaVector(i) = lambda^(n-i);
end
X = [X Y];
lambdaMatrix = diag(lambdaVector);
X = lambdaMatrix*X;
CovMat=(1-lambda)*X'*X; % Compute the covariance
if nargout==2 % Compute the correlation, if requested
   s=sqrt(diag(CovMat));
   CorrMat=CovMat./(s*s');
end