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

Murali Menon feanor0 at hotmail.com
Tue Apr 17 17:22:38 CEST 2007


Hi Guillaume,

Thanks for this. I couldn't get your lapply() code to work as is because of 
the following error:

Error in FUN(X[[1]], ...) : unused argument(s) (function (i, X, win)

but the following did the trick:

>res <- lapply(vec, function(i) cor(X[(i - win + 1) : i, ]))

where 'win' and 'X' were as per your definitions.

Cheers,
Murali




>From: guillaume.nicoulaud at halbis.com
>To: "Murali Menon" <feanor0 at hotmail.com>
>Subject: Réf. : [R-SIG-Finance] How to produce vector of correlation 
>matrices
>Date: Tue, 17 Apr 2007 16:48:39 +0200
>
># with X, your matrix
>
>
>stp = 10   # step
>
>win = 260  # window
>
>
>
>vec <- seq(win, nrow(X), by = stp)
>
>lapply(vec, function(i, X, win) { cor( X[(i-win+1):i, ] ) }, X = X, win = 
>win ) -> res
>
>
>
># "res" should be a list of correlation matrices
>
>
>
>Guillaume NICOULAUD
>Halbis Capital Management (France)
>Strategy & Quantitative Analysis
>
>Halbis Capital Management (France)
>75419 Paris Cedex 08
>Tel. :   +33 (0) 1 58 13 99 97
>Mob :  +33 (0) 6 87 84 97 75
>guillaume.nicoulaud at halbis.com
>www.halbis.com
>
>
>
>
>
>                                                         Pour :   
>r-sig-finance at stat.math.ethz.ch
>                                                         cc :
>                                                         Objet :  
>[R-SIG-Finance] How to produce vector of correlation matrices
>              "Murali Menon" <feanor0 at hotmail.com>
>              Envoyé par :
>              r-sig-finance-bounces at stat.math.ethz.
>              ch
>
>
>              17/04/2007 16:28
>
>
>
>
>
>
>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.
>
>
>
>
>Les informations contenues dans ce message sont confidentielles et peuvent 
>constituer des informations privilegiees. Si vous n etes pas le 
>destinataire de ce message, il vous est interdit de le copier, de le faire 
>suivre, de le divulguer ou d en utiliser tout ou partie. Si vous avez recu 
>ce message par erreur, merci de le supprimer de votre systeme, ainsi que 
>toutes ses copies, et d en avertir immediatement l expediteur par message 
>de retour.
>Il est impossible de garantir que les communications par messagerie 
>electronique arrivent en temps utile, sont securisees ou denuees de toute 
>erreur ou virus. En consequence, l expediteur n accepte aucune 
>responsabilite du fait des erreurs ou omissions qui pourraient en resulter.
>--- ----------------------------------------------------- ---
>The information contained in this e-mail is confidential. It may also be 
>legally privileged. If you are not the addressee you may not copy, forward, 
>disclose or use any part of it. If you have received this message in error, 
>please delete it and all copies from your system and notify the sender 
>immediately by return e-mail.
>E-mail communications cannot be guaranteed to be timely secure, error or 
>virus-free. The sender does not accept liability for any errors or 
>omissions which arise as a result.
>

_________________________________________________________________
Download Messenger. Join the i’m Initiative. Help make a difference today.



More information about the R-SIG-Finance mailing list