Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Historical Correlation

4 views
Skip to first unread message

Britt Schuurs

unread,
Feb 10, 2012, 4:06:28 AM2/10/12
to
Hello all,

I need to calculate the 30-day historical (realized) correlation between 45 different stocks i and j, based on the log Returns of the stocks which are in a 1400x45 matrix, so 1400 rows with the data for different dates, t=0...1400 and 45 columns, namely the data for every stock.

As far as I know, Matlab doesn't provide a function to calculate the historical correlation in such a way, based on x day stock data and instead of between 2 stocks, between x stocks.

Does anyone have an idea how to do this?? I am trying to program it manually but to do so I need so many loops that it becomes extremely cumbersome to do this relevatively 'standard' calculation.

http://en.wikipedia.org/wiki/Correlation_and_dependence
---> sample correlation

Help would be highly appreciated because I have been trying all day!!! :-)

Of course this is just about the correlation between different variables, not specificaly on stocks....

TideMan

unread,
Feb 10, 2012, 3:16:42 PM2/10/12
to
Cross correlation is very easy to calculate with Matlab:
cxy=x(lag+1:end)*y(1:end-lag)'/(n-lag);
This assumes x and y are a column vectors (e.g., columns of your
matrix).
Does this give you a clue how to proceed?

TideMan

unread,
Feb 10, 2012, 7:35:59 PM2/10/12
to
Ooops, should be:
cxy=x(lag+1:end)'*y(1:end-lag)/(n-lag);

Tina Wong

unread,
Feb 10, 2012, 8:11:28 PM2/10/12
to
Thanks,

I guess lag is the period you use to calculate the correlation and n is the total number of observations.

However, I don't see why the formula 'cxy=x(lag+1:end)'*y(1:end-lag)/(n-lag)' would calculate the cross correlation between x and y. I would say it's just a matrix multiplication of transpose column x, cells (lag+1:end) and column y, cells (1:end-lag) divided by the scalar n-lag?

TideMan

unread,
Feb 10, 2012, 10:18:29 PM2/10/12
to
Yes, but it produces just one number which is the cross correlation
for that lag.
Try it.

It's also called the sum of lagged products.
In the old days before FFT, this was an important step in the
calculation of the Fourier spectrum.

Britt Schuurs

unread,
Feb 12, 2012, 6:51:31 PM2/12/12
to
I see, okay, thanks a lot!

Tina Wong

unread,
Feb 18, 2012, 1:02:19 AM2/18/12
to
Hi,

I am so sorry to bother you again, but I have tried the formula many times and I don't get the historical correlation. I might do something wrong, but could you please be so kind to clarify your calculation a bit and why it gives the moving corr?

For example, what is n? and why divide by n-lag and nog the lag (as the Pearson formula says)? And where is the mean and standard dev. calculated necessary for the correlation coeff? If I follow the formula step by step I just get the matrix multiplication of x and y.

Would be great if you could help me a bit, I must be wrong but I just don't see it.

Thanks in advance :-)
0 new messages