[R-SIG-Finance] How to produce vector of correlation matrices

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 17 18:10:39 CEST 2007


Use embed.data.frame from this post:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/38589.html

The first group of lines creates the data frame, DF,
and the last two lines lapply cor and set the names.

Lines.raw <- "AAA     BBB     CCC     DDD     EEE
01/01/2007      10%     60%     49%     99%     99%
02/01/2007      42%     91%     72%     57%     79%
03/01/2007      25%     100%    76%     40%     17%
04/01/2007      99%     98%     81%     100%    13%
05/01/2007      74%     49%     85%     24%     69%
06/01/2007      9%      43%     57%     57%     1%
07/01/2007      77%     53%     84%     70%     57%
08/01/2007      98%     66%     24%     28%     77%
09/01/2007      1%      43%     72%     82%     1%
10/01/2007      14%     22%     18%     43%     27%
11/01/2007      15%     5%      62%     34%     3%
12/01/2007      70%     63%     59%     92%     57%
13/01/2007      27%     58%     12%     31%     68%
14/01/2007      15%     90%     48%     16%     66%
15/01/2007      86%     6%      52%     95%     3%"
Lines <- readLines(textConnection(Lines.raw))
Lines <- gsub("%", "", Lines)
DF <- read.table(textConnection(Lines), header = TRUE)

L <- lapply(embed.data.frame(DF, 10), cor)
names(L) <- head(rownames(DF), length(L))



On 4/17/07, Murali Menon <feanor0 at hotmail.com> wrote:
> Folks,
>
> I have a matrix of equity returns with rownames as dates and colnames as
> stock codes. I'd like to compute
> a rolling vector of correlation matrices, one matrix for each date. Is there
> a nifty way to accomplish this?
>
> E.g if my matrix is
>
>                AAA     BBB     CCC     DDD     EEE
> 01/01/2007      10%     60%     49%     99%     99%
> 02/01/2007      42%     91%     72%     57%     79%
> 03/01/2007      25%     100%    76%     40%     17%
> 04/01/2007      99%     98%     81%     100%    13%
> 05/01/2007      74%     49%     85%     24%     69%
> 06/01/2007      9%      43%     57%     57%     1%
> 07/01/2007      77%     53%     84%     70%     57%
> 08/01/2007      98%     66%     24%     28%     77%
> 09/01/2007      1%      43%     72%     82%     1%
> 10/01/2007      14%     22%     18%     43%     27%
> 11/01/2007      15%     5%      62%     34%     3%
> 12/01/2007      70%     63%     59%     92%     57%
> 13/01/2007      27%     58%     12%     31%     68%
> 14/01/2007      15%     90%     48%     16%     66%
> 15/01/2007      86%     6%      52%     95%     3%
>
> and I take a roll period of 10 days, my vector of correlation matrices might
> look like:
>
> 10/01/2007:
>
>        AAA     BBB     CCC     DDD     EEE
> AAA     1.00    0.38    0.19    -0.17   0.29
> BBB     0.38    1.00    0.44    0.16    0.10
> CCC     0.19    0.44    1.00    0.26    -0.15
> DDD     -0.17   0.16    0.26    1.00    -0.11
> EEE     0.29    0.10    -0.15   -0.11   1.00
>
> 11/01/2007:
>
>        AAA     BBB     CCC     DDD     EEE
> AAA     1.00    0.47    0.14    0.07    0.61
> BBB     0.47    1.00    0.37    0.32    0.30
> CCC     0.14    0.37    1.00    0.41    -0.06
> DDD     0.07    0.32    0.41    1.00    -0.38
> EEE     0.61    0.30    -0.06   -0.38   1.00
>
> etc.
>
> I looked at the fMultivar and fPortfolio packages which do funky stuff with
> return series,
> but they (as far as I can tell) produce an overall covariance matrix, not a
> rolling one as I need.
>
> Thanks,
>
> Murali
> PS: Of course, if my equity returns looked as they do above, I'd have
> retired aeons ago :-)
>
> _________________________________________________________________
>
> $771/month*
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list