# Fwd: runMult instead of runSum

 Classic List Threaded
11 messages
Reply | Threaded
Open this post in threaded view
|

## Fwd: runMult instead of runSum

 -------- Original-Nachricht -------- Datum: Fri, 28 Oct 2011 18:13:14 +0200 Von: "Martina Bauer" <[hidden email]> An: [hidden email] Betreff: runMult instead of runSum Hello, Maybe my approach is wrong from the beginning but I would like to calculate from monthly data - one months and two months and lastly three months return I know that I have to multiply the monthly performance instead to sum up - but I dont know how to do in R. Finally I should also rank the result according to n period performance (no clue how to archive this in R) here my code require(quantmod) stock=c("DD","DIS","PFE") DD.sp=Ad(DD) dd.m=monthlyReturn(DD.sp) dd1=runSum(dd.m,n=1) dd2=runSum(dd.m,n=2) dd3=runSum(dd.m,n=3) DIS.sp=Ad(DIS) dis.m=monthlyReturn(DIS.sp) dis1=runSum(DIS.m,n=1) dis2=runSum(DIS.m,n=2) dis3=runSum(DIS.m,n=3) PFE.sp=Ad(PFE) pfe.m=monthlyReturn(PFE.sp) pfe1=runSum(pfe.m,n=1) pfe2=runSum(pfe.m,n=2) pfe3=runSum(pfe.m,n=3) I know that my R code is very poor any idea how to improve ? THanks in advance Best Regards martin -- -- _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 Hi Martin,Attached is a script I made for you to demonstrate a few of the many possible approaches you could take.  Below, I pasted the output from the script.HTH,Garrett R> # There are lots of ways to do this. First, I'll do something similar to R> # what you have.  i.e. use the "monthlyReturn" function and do each stock separately R> R> library(quantmod) R> s <- c("DD", "DIS", "PFE") R> getSymbols(s)[1] "DD"  "DIS" "PFE" R> ### 1R> R> # use leading=FALSE so that monthlyReturn won't replace the first observation R> # The first row will be NA, so use -1 subset to remove it R> DD.m1 <- monthlyReturn(Ad(DD), leading=FALSE)[-1,]R> # apply a rolling function to convert 1 month returns to 2 and 3 month returnsR> DD.m2 <- rollapplyr(DD.m1, width=2, FUN=function(x) prod(1 + x) - 1) R> DD.m3 <- rollapplyr(DD.m1, width=3, FUN=function(x) prod(1 + x) - 1)R> # then you'd do this for each symbolR> R> ### 2R> R> # Or, you can use log returns so that you can use the runSum function, then "unlog" them at the end R> DD.m <- monthlyReturn(Ad(DD), type='log', leading=FALSE)[-1,]R> DD.m1 <- exp(runSum(DD.m, n=1)) - 1R> DD.m2 <- exp(runSum(DD.m, n=2)) - 1R> DD.m3 <- exp(runSum(DD.m, n=3)) - 1 R> R> ### 3R> R> # Alternatively, you could convert the prices to monthly first, and then calculate the returnsR> mDD <- to.monthly(DD)R> # For univariate series, you can use Delt or ROC R> DD.m1 <- Delt(Ad(mDD))R> DD.m2 <- ROC(Ad(mDD), n=2, type='discrete')R> DD.m3 <- Delt(Ad(mDD), k=3) R> # then you'd do this for each symbol R> R> ### 4R> R> # Since you're only dealing with the Adjusted price of each Symbol, it's probably betterR> # to merge all the relevant prices into a data.frame R> p <- cbind(Ad(DD), Ad(DIS), Ad(PFE))R> # convert to monthly pricesR> mp <- to.monthly(p, OHLC=FALSE)R> # calculate 1 month, 2 month, and 3 month returns with ROC R> ret1 <- ROC(mp, n=1, type='discrete')R> ret2 <- ROC(mp, n=2, type='discrete')R> ret3 <- ROC(mp, n=3, type='discrete')R> head(ret1)           DD.Adjusted DIS.Adjusted PFE.AdjustedJan 2007           NA           NA           NAFeb 2007  0.031112200 -0.025954654  -0.03851709Mar 2007 -0.025659301  0.005206738   0.01201803 Apr 2007 -0.005120702  0.015843998   0.04750124May 2007  0.071813725  0.013197361   0.05007085Jun 2007 -0.028355820 -0.036708111  -0.06972560R> head(ret2)           DD.Adjusted DIS.Adjusted PFE.AdjustedJan 2007           NA           NA           NAFeb 2007           NA           NA           NAMar 2007  0.004654581  -0.02088305  -0.02696196 Apr 2007 -0.030648610   0.02113323   0.06009014May 2007  0.066325287   0.02925046   0.09995052Jun 2007  0.041421569  -0.02399520  -0.02314596R> # for many different return periods, you can use lapply (or a for loop if you prefer) R> rets <- lapply(1:5, function(n) ROC(mp, n=n, type='discrete'))R> head(rets[[1]]) # 1 month returns          DD.Adjusted DIS.Adjusted PFE.AdjustedJan 2007           NA           NA           NA Feb 2007  0.031112200 -0.025954654  -0.03851709Mar 2007 -0.025659301  0.005206738   0.01201803Apr 2007 -0.005120702  0.015843998   0.04750124May 2007  0.071813725  0.013197361   0.05007085 Jun 2007 -0.028355820 -0.036708111  -0.06972560R> head(rets[[2]]) # 2 month returns          DD.Adjusted DIS.Adjusted PFE.AdjustedJan 2007           NA           NA           NA Feb 2007           NA           NA           NAMar 2007  0.004654581  -0.02088305  -0.02696196Apr 2007 -0.030648610   0.02113323   0.06009014May 2007  0.066325287   0.02925046   0.09995052 Jun 2007  0.041421569  -0.02399520  -0.02314596R> # etc. R> R> # If you have a lot of symbols, you may be interested in the "PF" function from R> # my "qmao" package (which is on R-Forge) which will do the "cbind"ing for you.R> # install.packages("qmao", repos='http://r-forge.r-project.org') R> # Just to have a little fun, here is how you could quickly calculate 6 month returns for all Dow componentsR> R> library(qmao)R> library(XML)R> #First get the symbols R> DJIsymbols <- function() {R+     djicomp <- readHTMLTable('http://finance.yahoo.com/q/cp?s=^DJI+Components', as.data.frame=FALSE) R+     as.data.frame(djicomp[[10]], stringsAsFactors=FALSE)[,1]R+ }R> syms <- DJIsymbols()R> getSymbols(syms)  [1] "AA"   "AXP"  "BA"   "BAC"  "CAT"  "CSCO" "CVX"  "DD"    [9] "DIS"  "GE"   "HD"   "HPQ"  "IBM"  "INTC" "JNJ"  "JPM" [17] "KFT"  "KO"   "MCD"  "MMM"  "MRK"  "MSFT" "PFE"  "PG"   [25] "T"    "TRV"  "UTX"  "VZ"   "WMT"  "XOM" R> P <- PF(syms, prefer='Adjusted', silent=TRUE)R> mP <- to.monthly(P, OHLC=FALSE) R> # 6 month simple return of each Dow componentR> tail(ROC(mP, n=6, type='discrete'))                  AA          AXP          BA        BACMay 2011  0.28407351 0.2035731077  0.23722861  0.0751604 Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000                CAT        CSCO         CVX          DDMay 2011  0.2616811 -0.12031662  0.31547017  0.15129714Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233                   DIS          GE          HD        HPQMay 2011  0.152866242  0.25913838  0.20906631 -0.1050471Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491                IBM        INTC        JNJ         JPM May 2011 0.20426720  0.08098933 0.11277945  0.16361650Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128                KFT         KO        MCD          MMMMay 2011 0.17773973 0.06532988 0.05772496  0.137328642 Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100                   MRK          MSFT          PFEMay 2011  0.0901365706  0.0024203308  0.342405063Jun 2011  0.0014359563 -0.0565899963  0.200353565 Jul 2011  0.0521060842  0.0003673769  0.078142695Aug 2011  0.0394026057  0.0137195122  0.006896552Sep 2011  0.0133250697 -0.0071798963 -0.111111111Oct 2011 -0.0008537279  0.0543180930 -0.035053554                    PG           T         TRV         UTXMay 2011  0.114454776  0.16844512  0.16457143  0.17840249Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192                   VZ          WMT           XOM May 2011  0.183733948  0.040425128  0.2131675201Jun 2011  0.067766647 -0.001135933  0.1252609603Jul 2011  0.016647196 -0.047159505 -0.0001261352Aug 2011  0.005071851  0.044784915 -0.1239199905 Sep 2011 -0.020248380  0.010907674 -0.1260979425Oct 2011  0.023110386  0.053844735 -0.0624784260On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer wrote: -------- Original-Nachricht -------- Datum: Fri, 28 Oct 2011 18:13:14 +0200 Von: "Martina Bauer" <[hidden email]> An: [hidden email] Betreff: runMult instead of runSum Hello, Maybe my approach is wrong from the beginning but I would like to calculate from monthly data - one months and two months and lastly three months return I know that I have to multiply the monthly performance instead to sum up - but I dont know how to do in R. Finally I should also rank the result according to n period performance (no clue how to archive this in R) here my code require(quantmod) stock=c("DD","DIS","PFE") DD.sp=Ad(DD) dd.m=monthlyReturn(DD.sp) dd1=runSum(dd.m,n=1) dd2=runSum(dd.m,n=2) dd3=runSum(dd.m,n=3) DIS.sp=Ad(DIS) dis.m=monthlyReturn(DIS.sp) dis1=runSum(DIS.m,n=1) dis2=runSum(DIS.m,n=2) dis3=runSum(DIS.m,n=3) PFE.sp=Ad(PFE) pfe.m=monthlyReturn(PFE.sp) pfe1=runSum(pfe.m,n=1) pfe2=runSum(pfe.m,n=2) pfe3=runSum(pfe.m,n=3) I know that my R code is very poor any idea how to improve ? THanks in advance Best Regards martin -- -- _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. MartinReturns.R (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 Hi Martin, hi Garrett, just for completion ... why did you not simply do a runsum on the difference of the logarithmics [1]. You said you have monthly data, so why not simply doing: oneMonthRet = diff(log(yourMonthlyData)) twoMonthRet = runSum(oneMonthRet, n=2) threeMonthRet = runSum(oneMonthRet, n=3) Regards, Ulrich References: [1] http://oakroadsystems.com/math/loglaws.htm#MultiplyOn Sat, Oct 29, 2011 at 11:14 PM, G See <[hidden email]> wrote: > Hi Martin, > > Attached is a script I made for you to demonstrate a few of the many > possible approaches you could take.  Below, I pasted the output from the > script. > > HTH, > Garrett > > R> # There are lots of ways to do this. First, I'll do something similar > to > R> # what you have.  i.e. use the "monthlyReturn" function and do each > stock separately > R> > R> library(quantmod) > R> s <- c("DD", "DIS", "PFE") > R> getSymbols(s) > [1] "DD"  "DIS" "PFE" > > R> ### 1 > R> > R> # use leading=FALSE so that monthlyReturn won't replace the first > observation > R> # The first row will be NA, so use -1 subset to remove it > R> DD.m1 <- monthlyReturn(Ad(DD), leading=FALSE)[-1,] > R> # apply a rolling function to convert 1 month returns to 2 and 3 month > returns > R> DD.m2 <- rollapplyr(DD.m1, width=2, FUN=function(x) prod(1 + x) - 1) > R> DD.m3 <- rollapplyr(DD.m1, width=3, FUN=function(x) prod(1 + x) - 1) > R> # then you'd do this for each symbol > R> > R> ### 2 > R> > R> # Or, you can use log returns so that you can use the runSum function, > then "unlog" them at the end > R> DD.m <- monthlyReturn(Ad(DD), type='log', leading=FALSE)[-1,] > R> DD.m1 <- exp(runSum(DD.m, n=1)) - 1 > R> DD.m2 <- exp(runSum(DD.m, n=2)) - 1 > R> DD.m3 <- exp(runSum(DD.m, n=3)) - 1 > R> > R> ### 3 > R> > R> # Alternatively, you could convert the prices to monthly first, and > then calculate the returns > R> mDD <- to.monthly(DD) > R> # For univariate series, you can use Delt or ROC > R> DD.m1 <- Delt(Ad(mDD)) > R> DD.m2 <- ROC(Ad(mDD), n=2, type='discrete') > R> DD.m3 <- Delt(Ad(mDD), k=3) > R> # then you'd do this for each symbol > R> > R> ### 4 > R> > R> # Since you're only dealing with the Adjusted price of each Symbol, > it's probably better > R> # to merge all the relevant prices into a data.frame > R> p <- cbind(Ad(DD), Ad(DIS), Ad(PFE)) > R> # convert to monthly prices > R> mp <- to.monthly(p, OHLC=FALSE) > R> # calculate 1 month, 2 month, and 3 month returns with ROC > R> ret1 <- ROC(mp, n=1, type='discrete') > R> ret2 <- ROC(mp, n=2, type='discrete') > R> ret3 <- ROC(mp, n=3, type='discrete') > > R> head(ret1) >           DD.Adjusted DIS.Adjusted PFE.Adjusted > Jan 2007           NA           NA           NA > Feb 2007  0.031112200 -0.025954654  -0.03851709 > Mar 2007 -0.025659301  0.005206738   0.01201803 > Apr 2007 -0.005120702  0.015843998   0.04750124 > May 2007  0.071813725  0.013197361   0.05007085 > Jun 2007 -0.028355820 -0.036708111  -0.06972560 > > R> head(ret2) >           DD.Adjusted DIS.Adjusted PFE.Adjusted > Jan 2007           NA           NA           NA > Feb 2007           NA           NA           NA > Mar 2007  0.004654581  -0.02088305  -0.02696196 > Apr 2007 -0.030648610   0.02113323   0.06009014 > May 2007  0.066325287   0.02925046   0.09995052 > Jun 2007  0.041421569  -0.02399520  -0.02314596 > > R> # for many different return periods, you can use lapply (or a for loop > if you prefer) > R> rets <- lapply(1:5, function(n) ROC(mp, n=n, type='discrete')) > > R> head(rets[[1]]) # 1 month returns >           DD.Adjusted DIS.Adjusted PFE.Adjusted > Jan 2007           NA           NA           NA > Feb 2007  0.031112200 -0.025954654  -0.03851709 > Mar 2007 -0.025659301  0.005206738   0.01201803 > Apr 2007 -0.005120702  0.015843998   0.04750124 > May 2007  0.071813725  0.013197361   0.05007085 > Jun 2007 -0.028355820 -0.036708111  -0.06972560 > > R> head(rets[[2]]) # 2 month returns >           DD.Adjusted DIS.Adjusted PFE.Adjusted > Jan 2007           NA           NA           NA > Feb 2007           NA           NA           NA > Mar 2007  0.004654581  -0.02088305  -0.02696196 > Apr 2007 -0.030648610   0.02113323   0.06009014 > May 2007  0.066325287   0.02925046   0.09995052 > Jun 2007  0.041421569  -0.02399520  -0.02314596 > > R> # etc. > R> > R> # If you have a lot of symbols, you may be interested in the "PF" > function from >  R> # my "qmao" package (which is on R-Forge) which will do the "cbind"ing > for you. > R> # install.packages("qmao", repos='http://r-forge.r-project.org') > R> # Just to have a little fun, here is how you could quickly calculate 6 > month returns for all Dow components > R> > R> library(qmao) > R> library(XML) > R> #First get the symbols > R> DJIsymbols <- function() { > R+     djicomp <- readHTMLTable('http://finance.yahoo.com/q/cp?s=^DJI+Components', > as.data.frame=FALSE) >  R+     as.data.frame(djicomp[[10]], stringsAsFactors=FALSE)[,1] > R+ } > R> syms <- DJIsymbols() > R> getSymbols(syms) > > > >  [1] "AA"   "AXP"  "BA"   "BAC"  "CAT"  "CSCO" "CVX"  "DD" >  [9] "DIS"  "GE"   "HD"   "HPQ"  "IBM"  "INTC" "JNJ"  "JPM" > [17] "KFT"  "KO"   "MCD"  "MMM"  "MRK"  "MSFT" "PFE"  "PG" > [25] "T"    "TRV"  "UTX"  "VZ"   "WMT"  "XOM" > > R> P <- PF(syms, prefer='Adjusted', silent=TRUE) > R> mP <- to.monthly(P, OHLC=FALSE) > R> # 6 month simple return of each Dow component > R> tail(ROC(mP, n=6, type='discrete')) >                   AA          AXP          BA        BAC > May 2011  0.28407351 0.2035731077  0.23722861  0.0751604 > Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917 > Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536 > Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667 > Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 > Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000 >                 CAT        CSCO         CVX          DD > May 2011  0.2616811 -0.12031662  0.31547017  0.15129714 > Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 > Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712 > Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423 > Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307 > Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233 >                   DIS          GE          HD        HPQ > May 2011  0.152866242  0.25913838  0.20906631 -0.1050471 > Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122 > Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 > Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780 > Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805 > Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491 >                 IBM        INTC        JNJ         JPM > May 2011 0.20426720  0.08098933 0.11277945  0.16361650 > Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089 > Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412 > Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 > Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178 > Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128 >                 KFT         KO        MCD          MMM > May 2011 0.17773973 0.06532988 0.05772496  0.137328642 > Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326 > Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746 > Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806 > Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 > Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100 >                    MRK          MSFT          PFE > May 2011  0.0901365706  0.0024203308  0.342405063 > Jun 2011  0.0014359563 -0.0565899963  0.200353565 > Jul 2011  0.0521060842  0.0003673769  0.078142695 > Aug 2011  0.0394026057  0.0137195122  0.006896552 > Sep 2011  0.0133250697 -0.0071798963 -0.111111111 > Oct 2011 -0.0008537279  0.0543180930 -0.035053554 >                    PG           T         TRV         UTX > May 2011  0.114454776  0.16844512  0.16457143  0.17840249 > Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091 > Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 > Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715 > Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512 > Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192 >                    VZ          WMT           XOM > May 2011  0.183733948  0.040425128  0.2131675201 > Jun 2011  0.067766647 -0.001135933  0.1252609603 > Jul 2011  0.016647196 -0.047159505 -0.0001261352 > Aug 2011  0.005071851  0.044784915 -0.1239199905 > Sep 2011 -0.020248380  0.010907674 -0.1260979425 > Oct 2011  0.023110386  0.053844735 -0.0624784260 > > > > > On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <[hidden email]> wrote: > >> -------- Original-Nachricht -------- >> Datum: Fri, 28 Oct 2011 18:13:14 +0200 >> Von: "Martina Bauer" <[hidden email]> >> An: [hidden email] >> Betreff: runMult instead of runSum >> >> Hello, >> >> Maybe my approach is wrong from the beginning but I would like to >> calculate from monthly data - one months and two months and lastly three >> months return >> I know that I have to multiply the monthly performance instead to sum up >> - but I dont know how to do in R. Finally I should also rank the result >> according to n period performance (no clue how to archive this in R) >> >> >> here my code >> >> require(quantmod) >> stock=c("DD","DIS","PFE") >> DD.sp=Ad(DD) >> dd.m=monthlyReturn(DD.sp) >> dd1=runSum(dd.m,n=1) >> dd2=runSum(dd.m,n=2) >> dd3=runSum(dd.m,n=3) >> >> DIS.sp=Ad(DIS) >> dis.m=monthlyReturn(DIS.sp) >> dis1=runSum(DIS.m,n=1) >> dis2=runSum(DIS.m,n=2) >> dis3=runSum(DIS.m,n=3) >> >> PFE.sp=Ad(PFE) >> pfe.m=monthlyReturn(PFE.sp) >> pfe1=runSum(pfe.m,n=1) >> pfe2=runSum(pfe.m,n=2) >> pfe3=runSum(pfe.m,n=3) >> >> >> I know that my R code is very poor any idea how to improve ? >> >> THanks in advance >> Best Regards >> >> martin >> >> -- >> >> >> >> >> -- >> >> _______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>> -- Subscriber-posting only. If you want to post, subscribe first. >> -- Also note that this is not the r-help list where general R questions >> should go. >> > > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 Upps, the fool caught me, I apologize. oneMonthRet = diff(log(yourMonthlyData)) twoMonthRet = exp(runSum(log(1+oneMonthRet), n=2)) - 1 threeMonthRet = exp(runSum(log(1+oneMonthRet), n=3)) - 1 On Sun, Oct 30, 2011 at 5:36 PM, Ulrich Staudinger < [hidden email]> wrote: > Hi Martin, hi Garrett, > > just for completion ... why did you not simply do a runsum on the > difference of the logarithmics [1]. > > You said you have monthly data, so why not simply doing: > > oneMonthRet = diff(log(yourMonthlyData)) > twoMonthRet = runSum(oneMonthRet, n=2) > threeMonthRet = runSum(oneMonthRet, n=3) > > > Regards, > Ulrich > > > > > References: > > [1] http://oakroadsystems.com/math/loglaws.htm#Multiply> > > On Sat, Oct 29, 2011 at 11:14 PM, G See <[hidden email]> wrote: > >> Hi Martin, >> >> Attached is a script I made for you to demonstrate a few of the many >> possible approaches you could take.  Below, I pasted the output from the >> script. >> >> HTH, >> Garrett >> >> R> # There are lots of ways to do this. First, I'll do something similar >> to >> R> # what you have.  i.e. use the "monthlyReturn" function and do each >> stock separately >> R> >> R> library(quantmod) >> R> s <- c("DD", "DIS", "PFE") >> R> getSymbols(s) >> [1] "DD"  "DIS" "PFE" >> >> R> ### 1 >> R> >> R> # use leading=FALSE so that monthlyReturn won't replace the first >> observation >> R> # The first row will be NA, so use -1 subset to remove it >> R> DD.m1 <- monthlyReturn(Ad(DD), leading=FALSE)[-1,] >> R> # apply a rolling function to convert 1 month returns to 2 and 3 month >> returns >> R> DD.m2 <- rollapplyr(DD.m1, width=2, FUN=function(x) prod(1 + x) - 1) >> R> DD.m3 <- rollapplyr(DD.m1, width=3, FUN=function(x) prod(1 + x) - 1) >> R> # then you'd do this for each symbol >> R> >> R> ### 2 >> R> >> R> # Or, you can use log returns so that you can use the runSum function, >> then "unlog" them at the end >> R> DD.m <- monthlyReturn(Ad(DD), type='log', leading=FALSE)[-1,] >> R> DD.m1 <- exp(runSum(DD.m, n=1)) - 1 >> R> DD.m2 <- exp(runSum(DD.m, n=2)) - 1 >> R> DD.m3 <- exp(runSum(DD.m, n=3)) - 1 >> R> >> R> ### 3 >> R> >> R> # Alternatively, you could convert the prices to monthly first, and >> then calculate the returns >> R> mDD <- to.monthly(DD) >> R> # For univariate series, you can use Delt or ROC >> R> DD.m1 <- Delt(Ad(mDD)) >> R> DD.m2 <- ROC(Ad(mDD), n=2, type='discrete') >> R> DD.m3 <- Delt(Ad(mDD), k=3) >> R> # then you'd do this for each symbol >> R> >> R> ### 4 >> R> >> R> # Since you're only dealing with the Adjusted price of each Symbol, >> it's probably better >> R> # to merge all the relevant prices into a data.frame >> R> p <- cbind(Ad(DD), Ad(DIS), Ad(PFE)) >> R> # convert to monthly prices >> R> mp <- to.monthly(p, OHLC=FALSE) >> R> # calculate 1 month, 2 month, and 3 month returns with ROC >> R> ret1 <- ROC(mp, n=1, type='discrete') >> R> ret2 <- ROC(mp, n=2, type='discrete') >> R> ret3 <- ROC(mp, n=3, type='discrete') >> >> R> head(ret1) >>           DD.Adjusted DIS.Adjusted PFE.Adjusted >> Jan 2007           NA           NA           NA >> Feb 2007  0.031112200 -0.025954654  -0.03851709 >> Mar 2007 -0.025659301  0.005206738   0.01201803 >> Apr 2007 -0.005120702  0.015843998   0.04750124 >> May 2007  0.071813725  0.013197361   0.05007085 >> Jun 2007 -0.028355820 -0.036708111  -0.06972560 >> >> R> head(ret2) >>           DD.Adjusted DIS.Adjusted PFE.Adjusted >> Jan 2007           NA           NA           NA >> Feb 2007           NA           NA           NA >> Mar 2007  0.004654581  -0.02088305  -0.02696196 >> Apr 2007 -0.030648610   0.02113323   0.06009014 >> May 2007  0.066325287   0.02925046   0.09995052 >> Jun 2007  0.041421569  -0.02399520  -0.02314596 >> >> R> # for many different return periods, you can use lapply (or a for loop >> if you prefer) >> R> rets <- lapply(1:5, function(n) ROC(mp, n=n, type='discrete')) >> >> R> head(rets[[1]]) # 1 month returns >>           DD.Adjusted DIS.Adjusted PFE.Adjusted >> Jan 2007           NA           NA           NA >> Feb 2007  0.031112200 -0.025954654  -0.03851709 >> Mar 2007 -0.025659301  0.005206738   0.01201803 >> Apr 2007 -0.005120702  0.015843998   0.04750124 >> May 2007  0.071813725  0.013197361   0.05007085 >> Jun 2007 -0.028355820 -0.036708111  -0.06972560 >> >> R> head(rets[[2]]) # 2 month returns >>           DD.Adjusted DIS.Adjusted PFE.Adjusted >> Jan 2007           NA           NA           NA >> Feb 2007           NA           NA           NA >> Mar 2007  0.004654581  -0.02088305  -0.02696196 >> Apr 2007 -0.030648610   0.02113323   0.06009014 >> May 2007  0.066325287   0.02925046   0.09995052 >> Jun 2007  0.041421569  -0.02399520  -0.02314596 >> >> R> # etc. >> R> >> R> # If you have a lot of symbols, you may be interested in the "PF" >> function from >>  R> # my "qmao" package (which is on R-Forge) which will do the >> "cbind"ing for you. >> R> # install.packages("qmao", repos='http://r-forge.r-project.org') >> R> # Just to have a little fun, here is how you could quickly calculate 6 >> month returns for all Dow components >> R> >> R> library(qmao) >> R> library(XML) >> R> #First get the symbols >> R> DJIsymbols <- function() { >> R+     djicomp <- readHTMLTable('http://finance.yahoo.com/q/cp?s=^DJI+Components', >> as.data.frame=FALSE) >>  R+     as.data.frame(djicomp[[10]], stringsAsFactors=FALSE)[,1] >> R+ } >> R> syms <- DJIsymbols() >> R> getSymbols(syms) >> >> >> >>  [1] "AA"   "AXP"  "BA"   "BAC"  "CAT"  "CSCO" "CVX"  "DD" >>  [9] "DIS"  "GE"   "HD"   "HPQ"  "IBM"  "INTC" "JNJ"  "JPM" >> [17] "KFT"  "KO"   "MCD"  "MMM"  "MRK"  "MSFT" "PFE"  "PG" >> [25] "T"    "TRV"  "UTX"  "VZ"   "WMT"  "XOM" >> >> R> P <- PF(syms, prefer='Adjusted', silent=TRUE) >> R> mP <- to.monthly(P, OHLC=FALSE) >> R> # 6 month simple return of each Dow component >> R> tail(ROC(mP, n=6, type='discrete')) >>                   AA          AXP          BA        BAC >> May 2011  0.28407351 0.2035731077  0.23722861  0.0751604 >> Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917 >> Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536 >> Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667 >> Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 >> Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000 >>                 CAT        CSCO         CVX          DD >> May 2011  0.2616811 -0.12031662  0.31547017  0.15129714 >> Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 >> Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712 >> Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423 >> Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307 >> Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233 >>                   DIS          GE          HD        HPQ >> May 2011  0.152866242  0.25913838  0.20906631 -0.1050471 >> Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122 >> Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 >> Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780 >> Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805 >> Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491 >>                 IBM        INTC        JNJ         JPM >> May 2011 0.20426720  0.08098933 0.11277945  0.16361650 >> Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089 >> Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412 >> Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 >> Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178 >> Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128 >>                 KFT         KO        MCD          MMM >> May 2011 0.17773973 0.06532988 0.05772496  0.137328642 >> Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326 >> Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746 >> Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806 >> Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 >> Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100 >>                    MRK          MSFT          PFE >> May 2011  0.0901365706  0.0024203308  0.342405063 >> Jun 2011  0.0014359563 -0.0565899963  0.200353565 >> Jul 2011  0.0521060842  0.0003673769  0.078142695 >> Aug 2011  0.0394026057  0.0137195122  0.006896552 >> Sep 2011  0.0133250697 -0.0071798963 -0.111111111 >> Oct 2011 -0.0008537279  0.0543180930 -0.035053554 >>                    PG           T         TRV         UTX >> May 2011  0.114454776  0.16844512  0.16457143  0.17840249 >> Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091 >> Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 >> Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715 >> Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512 >> Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192 >>                    VZ          WMT           XOM >> May 2011  0.183733948  0.040425128  0.2131675201 >> Jun 2011  0.067766647 -0.001135933  0.1252609603 >> Jul 2011  0.016647196 -0.047159505 -0.0001261352 >> Aug 2011  0.005071851  0.044784915 -0.1239199905 >> Sep 2011 -0.020248380  0.010907674 -0.1260979425 >> Oct 2011  0.023110386  0.053844735 -0.0624784260 >> >> >> >> >> On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <[hidden email]> wrote: >> >>> -------- Original-Nachricht -------- >>> Datum: Fri, 28 Oct 2011 18:13:14 +0200 >>> Von: "Martina Bauer" <[hidden email]> >>> An: [hidden email] >>> Betreff: runMult instead of runSum >>> >>> Hello, >>> >>> Maybe my approach is wrong from the beginning but I would like to >>> calculate from monthly data - one months and two months and lastly three >>> months return >>> I know that I have to multiply the monthly performance instead to sum up >>> - but I dont know how to do in R. Finally I should also rank the result >>> according to n period performance (no clue how to archive this in R) >>> >>> >>> here my code >>> >>> require(quantmod) >>> stock=c("DD","DIS","PFE") >>> DD.sp=Ad(DD) >>> dd.m=monthlyReturn(DD.sp) >>> dd1=runSum(dd.m,n=1) >>> dd2=runSum(dd.m,n=2) >>> dd3=runSum(dd.m,n=3) >>> >>> DIS.sp=Ad(DIS) >>> dis.m=monthlyReturn(DIS.sp) >>> dis1=runSum(DIS.m,n=1) >>> dis2=runSum(DIS.m,n=2) >>> dis3=runSum(DIS.m,n=3) >>> >>> PFE.sp=Ad(PFE) >>> pfe.m=monthlyReturn(PFE.sp) >>> pfe1=runSum(pfe.m,n=1) >>> pfe2=runSum(pfe.m,n=2) >>> pfe3=runSum(pfe.m,n=3) >>> >>> >>> I know that my R code is very poor any idea how to improve ? >>> >>> THanks in advance >>> Best Regards >>> >>> martin >>> >>> -- >>> >>> >>> >>> >>> -- >>> >>> _______________________________________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>>> -- Subscriber-posting only. If you want to post, subscribe first. >>> -- Also note that this is not the r-help list where general R questions >>> should go. >>> >> >> >> _______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>> -- Subscriber-posting only. If you want to post, subscribe first. >> -- Also note that this is not the r-help list where general R questions >> should go. >> > >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 In reply to this post by Ulrich Staudinger-3 On Sun, Oct 30, 2011 at 12:36 PM, Ulrich Staudinger < [hidden email]> wrote: > Hi Martin, hi Garrett, > > just for completion ... why did you not simply do a runsum on the > difference of the logarithmics [1]. > > You said you have monthly data, so why not simply doing: > > Because, as I understood it, he wanted arithmetic returns, not log returns. > oneMonthRet = diff(log(yourMonthlyData)) > twoMonthRet = runSum(oneMonthRet, n=2) > threeMonthRet = runSum(oneMonthRet, n=3) > > > Regards, > Ulrich > > > References: > > [1] http://oakroadsystems.com/math/loglaws.htm#Multiply> > > Garrett         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 In reply to this post by Ulrich Staudinger-3 I did that.  It's number 2 in the script. On Sun, Oct 30, 2011 at 12:39 PM, Ulrich Staudinger < [hidden email]> wrote: > Upps, the fool caught me, I apologize. > > > oneMonthRet = diff(log(yourMonthlyData)) > twoMonthRet = exp(runSum(log(1+oneMonthRet), n=2)) - 1 > threeMonthRet = exp(runSum(log(1+oneMonthRet), n=3)) - 1 > > > > > On Sun, Oct 30, 2011 at 5:36 PM, Ulrich Staudinger < > [hidden email]> wrote: > >> Hi Martin, hi Garrett, >> >> just for completion ... why did you not simply do a runsum on the >> difference of the logarithmics [1]. >> >> You said you have monthly data, so why not simply doing: >> >> oneMonthRet = diff(log(yourMonthlyData)) >> twoMonthRet = runSum(oneMonthRet, n=2) >> threeMonthRet = runSum(oneMonthRet, n=3) >> >> >> Regards, >> Ulrich >> >> >> >> >> References: >> >> [1] http://oakroadsystems.com/math/loglaws.htm#Multiply>> >> >> On Sat, Oct 29, 2011 at 11:14 PM, G See <[hidden email]> wrote: >> >>> Hi Martin, >>> >>> Attached is a script I made for you to demonstrate a few of the many >>> possible approaches you could take.  Below, I pasted the output from the >>> script. >>> >>> HTH, >>> Garrett >>> >>> R> # There are lots of ways to do this. First, I'll do something similar >>> to >>> R> # what you have.  i.e. use the "monthlyReturn" function and do each >>> stock separately >>> R> >>> R> library(quantmod) >>> R> s <- c("DD", "DIS", "PFE") >>> R> getSymbols(s) >>> [1] "DD"  "DIS" "PFE" >>> >>> R> ### 1 >>> R> >>> R> # use leading=FALSE so that monthlyReturn won't replace the first >>> observation >>> R> # The first row will be NA, so use -1 subset to remove it >>> R> DD.m1 <- monthlyReturn(Ad(DD), leading=FALSE)[-1,] >>> R> # apply a rolling function to convert 1 month returns to 2 and 3 >>> month returns >>> R> DD.m2 <- rollapplyr(DD.m1, width=2, FUN=function(x) prod(1 + x) - 1) >>> R> DD.m3 <- rollapplyr(DD.m1, width=3, FUN=function(x) prod(1 + x) - 1) >>> R> # then you'd do this for each symbol >>> R> >>> R> ### 2 >>> R> >>> R> # Or, you can use log returns so that you can use the runSum >>> function, then "unlog" them at the end >>> R> DD.m <- monthlyReturn(Ad(DD), type='log', leading=FALSE)[-1,] >>> R> DD.m1 <- exp(runSum(DD.m, n=1)) - 1 >>> R> DD.m2 <- exp(runSum(DD.m, n=2)) - 1 >>> R> DD.m3 <- exp(runSum(DD.m, n=3)) - 1 >>> R> >>> R> ### 3 >>> R> >>> R> # Alternatively, you could convert the prices to monthly first, and >>> then calculate the returns >>> R> mDD <- to.monthly(DD) >>> R> # For univariate series, you can use Delt or ROC >>> R> DD.m1 <- Delt(Ad(mDD)) >>> R> DD.m2 <- ROC(Ad(mDD), n=2, type='discrete') >>> R> DD.m3 <- Delt(Ad(mDD), k=3) >>> R> # then you'd do this for each symbol >>> R> >>> R> ### 4 >>> R> >>> R> # Since you're only dealing with the Adjusted price of each Symbol, >>> it's probably better >>> R> # to merge all the relevant prices into a data.frame >>> R> p <- cbind(Ad(DD), Ad(DIS), Ad(PFE)) >>> R> # convert to monthly prices >>> R> mp <- to.monthly(p, OHLC=FALSE) >>> R> # calculate 1 month, 2 month, and 3 month returns with ROC >>> R> ret1 <- ROC(mp, n=1, type='discrete') >>> R> ret2 <- ROC(mp, n=2, type='discrete') >>> R> ret3 <- ROC(mp, n=3, type='discrete') >>> >>> R> head(ret1) >>>           DD.Adjusted DIS.Adjusted PFE.Adjusted >>> Jan 2007           NA           NA           NA >>> Feb 2007  0.031112200 -0.025954654  -0.03851709 >>> Mar 2007 -0.025659301  0.005206738   0.01201803 >>> Apr 2007 -0.005120702  0.015843998   0.04750124 >>> May 2007  0.071813725  0.013197361   0.05007085 >>> Jun 2007 -0.028355820 -0.036708111  -0.06972560 >>> >>> R> head(ret2) >>>           DD.Adjusted DIS.Adjusted PFE.Adjusted >>> Jan 2007           NA           NA           NA >>> Feb 2007           NA           NA           NA >>> Mar 2007  0.004654581  -0.02088305  -0.02696196 >>> Apr 2007 -0.030648610   0.02113323   0.06009014 >>> May 2007  0.066325287   0.02925046   0.09995052 >>> Jun 2007  0.041421569  -0.02399520  -0.02314596 >>> >>> R> # for many different return periods, you can use lapply (or a for >>> loop if you prefer) >>> R> rets <- lapply(1:5, function(n) ROC(mp, n=n, type='discrete')) >>> >>> R> head(rets[[1]]) # 1 month returns >>>           DD.Adjusted DIS.Adjusted PFE.Adjusted >>> Jan 2007           NA           NA           NA >>> Feb 2007  0.031112200 -0.025954654  -0.03851709 >>> Mar 2007 -0.025659301  0.005206738   0.01201803 >>> Apr 2007 -0.005120702  0.015843998   0.04750124 >>> May 2007  0.071813725  0.013197361   0.05007085 >>> Jun 2007 -0.028355820 -0.036708111  -0.06972560 >>> >>> R> head(rets[[2]]) # 2 month returns >>>           DD.Adjusted DIS.Adjusted PFE.Adjusted >>> Jan 2007           NA           NA           NA >>> Feb 2007           NA           NA           NA >>> Mar 2007  0.004654581  -0.02088305  -0.02696196 >>> Apr 2007 -0.030648610   0.02113323   0.06009014 >>> May 2007  0.066325287   0.02925046   0.09995052 >>> Jun 2007  0.041421569  -0.02399520  -0.02314596 >>> >>> R> # etc. >>> R> >>> R> # If you have a lot of symbols, you may be interested in the "PF" >>> function from >>>  R> # my "qmao" package (which is on R-Forge) which will do the >>> "cbind"ing for you. >>> R> # install.packages("qmao", repos='http://r-forge.r-project.org') >>> R> # Just to have a little fun, here is how you could quickly calculate >>> 6 month returns for all Dow components >>> R> >>> R> library(qmao) >>> R> library(XML) >>> R> #First get the symbols >>> R> DJIsymbols <- function() { >>> R+     djicomp <- readHTMLTable('http://finance.yahoo.com/q/cp?s=^DJI+Components', >>> as.data.frame=FALSE) >>>  R+     as.data.frame(djicomp[[10]], stringsAsFactors=FALSE)[,1] >>> R+ } >>> R> syms <- DJIsymbols() >>> R> getSymbols(syms) >>> >>> >>> >>>  [1] "AA"   "AXP"  "BA"   "BAC"  "CAT"  "CSCO" "CVX"  "DD" >>>  [9] "DIS"  "GE"   "HD"   "HPQ"  "IBM"  "INTC" "JNJ"  "JPM" >>> [17] "KFT"  "KO"   "MCD"  "MMM"  "MRK"  "MSFT" "PFE"  "PG" >>> [25] "T"    "TRV"  "UTX"  "VZ"   "WMT"  "XOM" >>> >>> R> P <- PF(syms, prefer='Adjusted', silent=TRUE) >>> R> mP <- to.monthly(P, OHLC=FALSE) >>> R> # 6 month simple return of each Dow component >>> R> tail(ROC(mP, n=6, type='discrete')) >>>                   AA          AXP          BA        BAC >>> May 2011  0.28407351 0.2035731077  0.23722861  0.0751604 >>> Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917 >>> Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536 >>> Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667 >>> Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 >>> Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000 >>>                 CAT        CSCO         CVX          DD >>> May 2011  0.2616811 -0.12031662  0.31547017  0.15129714 >>> Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 >>> Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712 >>> Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423 >>> Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307 >>> Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233 >>>                   DIS          GE          HD        HPQ >>> May 2011  0.152866242  0.25913838  0.20906631 -0.1050471 >>> Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122 >>> Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 >>> Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780 >>> Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805 >>> Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491 >>>                 IBM        INTC        JNJ         JPM >>> May 2011 0.20426720  0.08098933 0.11277945  0.16361650 >>> Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089 >>> Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412 >>> Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 >>> Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178 >>> Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128 >>>                 KFT         KO        MCD          MMM >>> May 2011 0.17773973 0.06532988 0.05772496  0.137328642 >>> Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326 >>> Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746 >>> Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806 >>> Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 >>> Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100 >>>                    MRK          MSFT          PFE >>> May 2011  0.0901365706  0.0024203308  0.342405063 >>> Jun 2011  0.0014359563 -0.0565899963  0.200353565 >>> Jul 2011  0.0521060842  0.0003673769  0.078142695 >>> Aug 2011  0.0394026057  0.0137195122  0.006896552 >>> Sep 2011  0.0133250697 -0.0071798963 -0.111111111 >>> Oct 2011 -0.0008537279  0.0543180930 -0.035053554 >>>                    PG           T         TRV         UTX >>> May 2011  0.114454776  0.16844512  0.16457143  0.17840249 >>> Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091 >>> Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 >>> Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715 >>> Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512 >>> Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192 >>>                    VZ          WMT           XOM >>> May 2011  0.183733948  0.040425128  0.2131675201 >>> Jun 2011  0.067766647 -0.001135933  0.1252609603 >>> Jul 2011  0.016647196 -0.047159505 -0.0001261352 >>> Aug 2011  0.005071851  0.044784915 -0.1239199905 >>> Sep 2011 -0.020248380  0.010907674 -0.1260979425 >>> Oct 2011  0.023110386  0.053844735 -0.0624784260 >>> >>> >>> >>> >>> On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <[hidden email]>wrote: >>> >>>> -------- Original-Nachricht -------- >>>> Datum: Fri, 28 Oct 2011 18:13:14 +0200 >>>> Von: "Martina Bauer" <[hidden email]> >>>> An: [hidden email] >>>> Betreff: runMult instead of runSum >>>> >>>> Hello, >>>> >>>> Maybe my approach is wrong from the beginning but I would like to >>>> calculate from monthly data - one months and two months and lastly three >>>> months return >>>> I know that I have to multiply the monthly performance instead to sum >>>> up - but I dont know how to do in R. Finally I should also rank the result >>>> according to n period performance (no clue how to archive this in R) >>>> >>>> >>>> here my code >>>> >>>> require(quantmod) >>>> stock=c("DD","DIS","PFE") >>>> DD.sp=Ad(DD) >>>> dd.m=monthlyReturn(DD.sp) >>>> dd1=runSum(dd.m,n=1) >>>> dd2=runSum(dd.m,n=2) >>>> dd3=runSum(dd.m,n=3) >>>> >>>> DIS.sp=Ad(DIS) >>>> dis.m=monthlyReturn(DIS.sp) >>>> dis1=runSum(DIS.m,n=1) >>>> dis2=runSum(DIS.m,n=2) >>>> dis3=runSum(DIS.m,n=3) >>>> >>>> PFE.sp=Ad(PFE) >>>> pfe.m=monthlyReturn(PFE.sp) >>>> pfe1=runSum(pfe.m,n=1) >>>> pfe2=runSum(pfe.m,n=2) >>>> pfe3=runSum(pfe.m,n=3) >>>> >>>> >>>> I know that my R code is very poor any idea how to improve ? >>>> >>>> THanks in advance >>>> Best Regards >>>> >>>> martin >>>> >>>> -- >>>> >>>> >>>> >>>> >>>> -- >>>> >>>> _______________________________________________ >>>> [hidden email] mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>>>> -- Subscriber-posting only. If you want to post, subscribe first. >>>> -- Also note that this is not the r-help list where general R questions >>>> should go. >>>> >>> >>> >>> _______________________________________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance>>> -- Subscriber-posting only. If you want to post, subscribe first. >>> -- Also note that this is not the r-help list where general R questions >>> should go. >>> >> >> >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 In reply to this post by Martin Bauer Hi, How can I order this matrix ? I have tried this ret1=(ROC(mP, n=6, type='discrete') tail(order(ret1[NROW(ret1)])) ret1[NROW(ret1),4] ret1[NROW(ret1),1] ret1[NROW(ret1),3] ret1[NROW(ret1),2] but I only get the ordered the first part (AA,AXP,BA,BAC) of the matrix instead the hole matrix any idea why ? Have a great weekend Martin > > R> P <- PF(syms, prefer='Adjusted', silent=TRUE) > R> mP <- to.monthly(P, OHLC=FALSE) > R> # 6 month simple return of each Dow component > R> tail(ROC(mP, n=6, type='discrete')) >                   AA          AXP          BA        BAC > May 2011  0.28407351 0.2035731077  0.23722861  0.0751604 > Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917 > Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536 > Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667 > Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 > Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000 >                 CAT        CSCO         CVX          DD > May 2011  0.2616811 -0.12031662  0.31547017  0.15129714 > Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 > Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712 > Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423 > Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307 > Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233 >                   DIS          GE          HD        HPQ > May 2011  0.152866242  0.25913838  0.20906631 -0.1050471 > Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122 > Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 > Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780 > Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805 > Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491 >                 IBM        INTC        JNJ         JPM > May 2011 0.20426720  0.08098933 0.11277945  0.16361650 > Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089 > Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412 > Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 > Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178 > Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128 >                 KFT         KO        MCD          MMM > May 2011 0.17773973 0.06532988 0.05772496  0.137328642 > Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326 > Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746 > Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806 > Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 > Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100 >                    MRK          MSFT          PFE > May 2011  0.0901365706  0.0024203308  0.342405063 > Jun 2011  0.0014359563 -0.0565899963  0.200353565 > Jul 2011  0.0521060842  0.0003673769  0.078142695 > Aug 2011  0.0394026057  0.0137195122  0.006896552 > Sep 2011  0.0133250697 -0.0071798963 -0.111111111 > Oct 2011 -0.0008537279  0.0543180930 -0.035053554 >                    PG           T         TRV         UTX > May 2011  0.114454776  0.16844512  0.16457143  0.17840249 > Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091 > Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 > Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715 > Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512 > Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192 >                    VZ          WMT           XOM > May 2011  0.183733948  0.040425128  0.2131675201 > Jun 2011  0.067766647 -0.001135933  0.1252609603 > Jul 2011  0.016647196 -0.047159505 -0.0001261352 > Aug 2011  0.005071851  0.044784915 -0.1239199905 > Sep 2011 -0.020248380  0.010907674 -0.1260979425 > Oct 2011  0.023110386  0.053844735 -0.0624784260 > > > > > On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <[hidden email]> wrote: > > > -------- Original-Nachricht -------- > > Datum: Fri, 28 Oct 2011 18:13:14 +0200 > > Von: "Martina Bauer" <[hidden email]> > > An: [hidden email] > > Betreff: runMult instead of runSum > > > > Hello, > > > > Maybe my approach is wrong from the beginning but I would like to > > calculate from monthly data - one months and two months and lastly three > > months return > > I know that I have to multiply the monthly performance instead to sum up > - > > but I dont know how to do in R. Finally I should also rank the result > > according to n period performance (no clue how to archive this in R) > > > > > > here my code > > > > require(quantmod) > > stock=c("DD","DIS","PFE") > > DD.sp=Ad(DD) > > dd.m=monthlyReturn(DD.sp) > > dd1=runSum(dd.m,n=1) > > dd2=runSum(dd.m,n=2) > > dd3=runSum(dd.m,n=3) > > > > DIS.sp=Ad(DIS) > > dis.m=monthlyReturn(DIS.sp) > > dis1=runSum(DIS.m,n=1) > > dis2=runSum(DIS.m,n=2) > > dis3=runSum(DIS.m,n=3) > > > > PFE.sp=Ad(PFE) > > pfe.m=monthlyReturn(PFE.sp) > > pfe1=runSum(pfe.m,n=1) > > pfe2=runSum(pfe.m,n=2) > > pfe3=runSum(pfe.m,n=3) > > > > > > I know that my R code is very poor any idea how to improve ? > > > > THanks in advance > > Best Regards > > > > martin > > > > -- > > > > > > > > > > -- > > > > _______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> > -- Subscriber-posting only. If you want to post, subscribe first. > > -- Also note that this is not the r-help list where general R questions > > should go. > > -- _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Skewness function for intraday data return distribution

 In reply to this post by Ulrich Staudinger-3 Hi All, I have written a skew function to calculate stock skewness using intraday 5 min data. However, going through literature I have come across two very similar formulas, though they produce different skewness coefficient and wonder if anyone else has used similar formulas and know which one is the correct one. mq_skewness = function(data){ data = dataformatc(data);   #returns midquote log returns as xts object   mq_ret = mq_return(data);     #returns midquote log returns as xts object   mq_ret_sqr = mq_ret^2;   #returns realized daily volatility as xts object   RVar=sum(  mq_ret_sqr)   n = length(mq_ret);     #first formula to measure skewness   mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3)    # second formula to measure skewness   mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3)     return(mq_skewness); }   #first formula to measure skewness   mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3) [1] 0.0003991095   # second formula to measure skewness   mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3) [1] 0.09759751 # this is output from base package function but this may not be appropriate to use for intraday data skewness(mq_ret) [1] 0.3035787 __________________________________________________ Darko Roupell ************** IMPORTANT MESSAGE *****************************       This e-mail message is intended only for the addressee(s) and contains information which may be confidential. If you are not the intended recipient please advise the sender by return email, do not use or disclose the contents, and delete the message and any attachments from your system. Unless specifically indicated, this email does not constitute formal advice or commitment by the sender or the Commonwealth Bank of Australia (ABN 48 123 123 124) or its subsidiaries. We can be contacted through our web site: commbank.com.au. If you no longer wish to receive commercial electronic messages from us, please reply to this e-mail by typing Unsubscribe in the subject line. _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Skewness function for intraday data return distribution

 The skewness of c * X should be equal to the skewness of X.  If I'm reading the formulas correctly, that is true of the second one but not the first. It would be interesting to hear if you find different results intraday than I did with daily data over a year: http://www.portfolioprobe.com/2011/10/03/predictability-of-kurtosis-and-skewness-in-sp-constituents/On 31/10/2011 05:18, Roupell, Darko wrote: > Hi All, > > I have written a skew function to calculate stock skewness using intraday 5 min data. However, going through literature I have come across two very similar formulas, though they produce different skewness coefficient and wonder if anyone else has used similar formulas and know which one is the correct one. > > mq_skewness = function(data){ > data = dataformatc(data); >    #returns midquote log returns as xts object >    mq_ret = mq_return(data); >    #returns midquote log returns as xts object >    mq_ret_sqr = mq_ret^2; >    #returns realized daily volatility as xts object >    RVar=sum(  mq_ret_sqr) >    n = length(mq_ret); > >    #first formula to measure skewness >    mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3) > >   # second formula to measure skewness >    mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3) > >    return(mq_skewness); > } > >    #first formula to measure skewness >    mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3) > [1] 0.0003991095 >    # second formula to measure skewness >    mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3) > [1] 0.09759751 > # this is output from base package function but this may not be appropriate to use for intraday data > skewness(mq_ret) > [1] 0.3035787 > __________________________________________________ > Darko Roupell > > > > ************** IMPORTANT MESSAGE ***************************** > This e-mail message is intended only for the addressee(s) and contains information which may be > confidential. > If you are not the intended recipient please advise the sender by return email, do not use or > disclose the contents, and delete the message and any attachments from your system. Unless > specifically indicated, this email does not constitute formal advice or commitment by the sender > or the Commonwealth Bank of Australia (ABN 48 123 123 124) or its subsidiaries. > We can be contacted through our web site: commbank.com.au. > If you no longer wish to receive commercial electronic messages from us, please reply to this > e-mail by typing Unsubscribe in the subject line. > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions should go. > -- Patrick Burns [hidden email] http://www.burns-stat.comhttp://www.portfolioprobe.com/blogtwitter: @portfolioprobe _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: [SPAM] - Re: Skewness function for intraday data returndistribution - Email found in subject

 Pretty sure the first one is a typo - if the last power is 3/2, they match up. HTH, -- David -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Patrick Burns Sent: Monday, October 31, 2011 3:10 AM To: [hidden email] Subject: [SPAM] - Re: [R-SIG-Finance] Skewness function for intraday data returndistribution - Email found in subject The skewness of c * X should be equal to the skewness of X.  If I'm reading the formulas correctly, that is true of the second one but not the first. It would be interesting to hear if you find different results intraday than I did with daily data over a year: http://www.portfolioprobe.com/2011/10/03/predictability-of-kurtosis-and-skewness-in-sp-constituents/On 31/10/2011 05:18, Roupell, Darko wrote: > Hi All, > > I have written a skew function to calculate stock skewness using intraday 5 min data. However, going through literature I have come across two very similar formulas, though they produce different skewness coefficient and wonder if anyone else has used similar formulas and know which one is the correct one. > > mq_skewness = function(data){ > data = dataformatc(data); >    #returns midquote log returns as xts object >    mq_ret = mq_return(data); >    #returns midquote log returns as xts object >    mq_ret_sqr = mq_ret^2; >    #returns realized daily volatility as xts object >    RVar=sum(  mq_ret_sqr) >    n = length(mq_ret); > >    #first formula to measure skewness >    mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3) > >   # second formula to measure skewness >    mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3) > >    return(mq_skewness); > } > >    #first formula to measure skewness >    mq_skewness = sqrt(n) * (sum(mq_ret^3))/sum(mq_ret_sqr)^(2/3) > [1] 0.0003991095 >    # second formula to measure skewness >    mq_skewness = sqrt(n) * sum((mq_ret/sqrt(RVar))^3) [1] 0.09759751 # > this is output from base package function but this may not be > appropriate to use for intraday data > skewness(mq_ret) > [1] 0.3035787 > __________________________________________________ > Darko Roupell > > > > ************** IMPORTANT MESSAGE ***************************** This > e-mail message is intended only for the addressee(s) and contains > information which may be confidential. > If you are not the intended recipient please advise the sender by > return email, do not use or disclose the contents, and delete the > message and any attachments from your system. Unless specifically > indicated, this email does not constitute formal advice or commitment by the sender or the Commonwealth Bank of Australia (ABN 48 123 123 124) or its subsidiaries. > We can be contacted through our web site: commbank.com.au. > If you no longer wish to receive commercial electronic messages from > us, please reply to this e-mail by typing Unsubscribe in the subject line. > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions should go. > -- Patrick Burns [hidden email] http://www.burns-stat.comhttp://www.portfolioprobe.com/blogtwitter: @portfolioprobe _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates. THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE. _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

## Re: Fwd: runMult instead of runSum

 In reply to this post by Martin Bauer Martin, I'm sorry, I do not understand what you want. e.g. `ret1[NROW(ret1), 4]` is no different than `last(ret1[,4])` So, just to take a shot in the dark, something like this will sort the last row of returns from lowest to highest last(ret1)[ , order(last(ret1))] Please try again to explain what you want.  What do you mean by "order the matrix?"  (it's already ordered by date ;-) What do you expect to get for output?  How many rows? How many columns? Also, if your question has more to do with beginner R help than finance, maybe a better place to ask follow up questions is on the r-help list. Best, Garrett On Sun, Oct 30, 2011 at 4:25 PM, Martin Bauer <[hidden email]> wrote: > Hi, > > How can I order this matrix ? > > I have tried this > > ret1=(ROC(mP, n=6, type='discrete') > > tail(order(ret1[NROW(ret1)])) > > ret1[NROW(ret1),4] > ret1[NROW(ret1),1] > ret1[NROW(ret1),3] > ret1[NROW(ret1),2] > > but I only get the ordered the first part (AA,AXP,BA,BAC) of the matrix > instead the hole matrix > > any idea why ? > > Have a great weekend > > Martin > > > > > R> P <- PF(syms, prefer='Adjusted', silent=TRUE) > > R> mP <- to.monthly(P, OHLC=FALSE) > > R> # 6 month simple return of each Dow component > > R> tail(ROC(mP, n=6, type='discrete')) > >                   AA          AXP          BA        BAC > > May 2011  0.28407351 0.2035731077  0.23722861  0.0751604 > > Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917 > > Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536 > > Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667 > > Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496 > > Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000 > >                 CAT        CSCO         CVX          DD > > May 2011  0.2616811 -0.12031662  0.31547017  0.15129714 > > Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054 > > Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712 > > Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423 > > Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307 > > Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233 > >                   DIS          GE          HD        HPQ > > May 2011  0.152866242  0.25913838  0.20906631 -0.1050471 > > Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122 > > Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774 > > Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780 > > Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805 > > Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491 > >                 IBM        INTC        JNJ         JPM > > May 2011 0.20426720  0.08098933 0.11277945  0.16361650 > > Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089 > > Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412 > > Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951 > > Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178 > > Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128 > >                 KFT         KO        MCD          MMM > > May 2011 0.17773973 0.06532988 0.05772496  0.137328642 > > Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326 > > Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746 > > Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806 > > Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905 > > Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100 > >                    MRK          MSFT          PFE > > May 2011  0.0901365706  0.0024203308  0.342405063 > > Jun 2011  0.0014359563 -0.0565899963  0.200353565 > > Jul 2011  0.0521060842  0.0003673769  0.078142695 > > Aug 2011  0.0394026057  0.0137195122  0.006896552 > > Sep 2011  0.0133250697 -0.0071798963 -0.111111111 > > Oct 2011 -0.0008537279  0.0543180930 -0.035053554 > >                    PG           T         TRV         UTX > > May 2011  0.114454776  0.16844512  0.16457143  0.17840249 > > Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091 > > Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465 > > Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715 > > Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512 > > Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192 > >                    VZ          WMT           XOM > > May 2011  0.183733948  0.040425128  0.2131675201 > > Jun 2011  0.067766647 -0.001135933  0.1252609603 > > Jul 2011  0.016647196 -0.047159505 -0.0001261352 > > Aug 2011  0.005071851  0.044784915 -0.1239199905 > > Sep 2011 -0.020248380  0.010907674 -0.1260979425 > > Oct 2011  0.023110386  0.053844735 -0.0624784260 > > > > > > > > > > On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <[hidden email]> > wrote: > > > > > -------- Original-Nachricht -------- > > > Datum: Fri, 28 Oct 2011 18:13:14 +0200 > > > Von: "Martina Bauer" <[hidden email]> > > > An: [hidden email] > > > Betreff: runMult instead of runSum > > > > > > Hello, > > > > > > Maybe my approach is wrong from the beginning but I would like to > > > calculate from monthly data - one months and two months and lastly > three > > > months return > > > I know that I have to multiply the monthly performance instead to sum > up > > - > > > but I dont know how to do in R. Finally I should also rank the result > > > according to n period performance (no clue how to archive this in R) > > > > > > > > > here my code > > > > > > require(quantmod) > > > stock=c("DD","DIS","PFE") > > > DD.sp=Ad(DD) > > > dd.m=monthlyReturn(DD.sp) > > > dd1=runSum(dd.m,n=1) > > > dd2=runSum(dd.m,n=2) > > > dd3=runSum(dd.m,n=3) > > > > > > DIS.sp=Ad(DIS) > > > dis.m=monthlyReturn(DIS.sp) > > > dis1=runSum(DIS.m,n=1) > > > dis2=runSum(DIS.m,n=2) > > > dis3=runSum(DIS.m,n=3) > > > > > > PFE.sp=Ad(PFE) > > > pfe.m=monthlyReturn(PFE.sp) > > > pfe1=runSum(pfe.m,n=1) > > > pfe2=runSum(pfe.m,n=2) > > > pfe3=runSum(pfe.m,n=3) > > > > > > > > > I know that my R code is very poor any idea how to improve ? > > > > > > THanks in advance > > > Best Regards > > > > > > martin > > > > > > -- > > > > > > > > > > > > > > > -- > > > > > > _______________________________________________ > > > [hidden email] mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> > > -- Subscriber-posting only. If you want to post, subscribe first. > > > -- Also note that this is not the r-help list where general R questions > > > should go. > > > > > -- > NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie! > Jetzt informieren: http://www.gmx.net/de/go/freephone>         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.