Thread Subject: Historical Correlation

Subject: Historical Correlation

From: Britt Schuurs

Date: 10 Feb, 2012 09:06:28

Message: 1 of 7

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....

Subject: Historical Correlation

From: TideMan

Date: 10 Feb, 2012 20:16:42

Message: 2 of 7

On Feb 10, 10:06 pm, "Britt Schuurs" <brittschu...@gmail.com> wrote:
> 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....

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?

Subject: Historical Correlation

From: TideMan

Date: 11 Feb, 2012 00:35:59

Message: 3 of 7

On Feb 11, 9:16 am, TideMan <mul...@gmail.com> wrote:
> On Feb 10, 10:06 pm, "Britt Schuurs" <brittschu...@gmail.com> wrote:
>
> > 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....
>
> 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?

Ooops, should be:
cxy=x(lag+1:end)'*y(1:end-lag)/(n-lag);

Subject: Historical Correlation

From: Tina Wong

Date: 11 Feb, 2012 01:11:28

Message: 4 of 7

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?

Subject: Historical Correlation

From: TideMan

Date: 11 Feb, 2012 03:18:29

Message: 5 of 7

On Feb 11, 2:11 pm, "Tina Wong" <brittschu...@gmail.com> wrote:
> 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?

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.

Subject: Historical Correlation

From: Britt Schuurs

Date: 12 Feb, 2012 23:51:31

Message: 6 of 7

On 11 feb, 11:18, TideMan <mul...@gmail.com> wrote:
> On Feb 11, 2:11 pm, "Tina Wong" <brittschu...@gmail.com> wrote:
>
> > 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?
>
> 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.

I see, okay, thanks a lot!

Subject: Historical Correlation

From: Tina Wong

Date: 18 Feb, 2012 06:02:19

Message: 7 of 7

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 :-)

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com