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 <
ustaudinger@activequant.org> 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 <gsee000@gmail.com> 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)
>>
>> <snip>
>>
>>  [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 <Bauermartin@gmx.at> wrote:
>>
>>> -------- Original-Nachricht --------
>>> Datum: Fri, 28 Oct 2011 18:13:14 +0200
>>> Von: "Martina Bauer" <Bauermartin@gmx.at>
>>> An: r-sig-finance@r-project.or
>>> 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
>>>
>>> --
>>>
>>>
>>>
>>>
>>> --
>>>
>>> _______________________________________________
>>> R-SIG-Finance@r-project.org 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.
>>>
>>
>>
>> _______________________________________________
>> R-SIG-Finance@r-project.org 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]]

