Andrew Bannerman
Andrew Bannerman

Reputation: 1305

Regarding Return.Cumulative, how to manually calculate so that each data point is visible

I have a return stream that looks like this:

  rets <- c(0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,  8.543399e-02,  0.000000e+00, -3.188801e-02,
             1.281113e-01,  0.000000e+00, -1.095815e-02, -7.140427e-03,  0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,
             0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00 , 0.000000e+00)
  dates <-  as.Date(c("2009-01-06", "2009-01-07", "2009-01-08" ,"2009-01-09", "2009-01-12" ,"2009-01-13", "2009-01-14" ,"2009-01-15", "2009-01-16" ,"2009-01-20"
  ,"2009-01-21", "2009-01-22", "2009-01-23", "2009-01-26", "2009-01-27" ,"2009-01-28", "2009-01-29", "2009-01-30", "2009-02-02", "2009-02-03",
  "2009-02-04", "2009-02-05", "2009-02-06", "2009-02-09"))
  df <- data.frame(rets,Date=dates)

It is from a specific rule based system hence there are times of 0, no activity.

Next we compute the cumulative return:

 cum.ret <-sapply(df[1] + 1, cumprod)

The output:

> sapply(df[1] + 1, cumprod)
          rets
 [1,] 1.000000
 [2,] 1.000000
 [3,] 1.000000
 [4,] 1.000000
 [5,] 1.000000
 [6,] 1.000000
 [7,] 1.085434
 [8,] 1.085434
 [9,] 1.050822
[10,] 1.185444
[11,] 1.185444
[12,] 1.172454
[13,] 1.164082
[14,] 1.164082
[15,] 1.164082
[16,] 1.164082
[17,] 1.164082
[18,] 1.164082
[19,] 1.164082
[20,] 1.164082
[21,] 1.164082
[22,] 1.164082
[23,] 1.164082
[24,] 1.164082

It has added +1 to all my zero's (is this accurate?). If I then take the product of these returns, I obtain:

> prod(cum.ret)
[1] 12.62976

If I check against Return.Cumulative

 # check to Return.Cumulative 
 df.xts <- xts(df$rets, order.by=as.Date(df$Date, format="%y-%m-%d"))
 Return.cumulative(df.xts)

> Return.cumulative(df.xts)
                       [,1]
Cumulative Return 0.1640817

I guess my question is, how would I manually calculate cumulative returns for all my data, Return.Cumulative provides the output, but I want each and every day point to be visible for plotting etc etc... not just the end result

Upvotes: 1

Views: 123

Answers (1)

duckmayr
duckmayr

Reputation: 16920

You get cumulative return by calculating prod(1 + df$rets) - 1, which can be confirmed by Hack-R's excellent suggestion in a comment to your question to look at the source code for Return.cumulative.

Since you want the cumulative return at each step, you can just do this:

cum.ret <- cumprod(1 + df$rets) - 1

Which results in:

 [1] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.08543399
 [8] 0.08543399 0.05082166 0.18544379 0.18544379 0.17245352 0.16408170 0.16408170
[15] 0.16408170 0.16408170 0.16408170 0.16408170 0.16408170 0.16408170 0.16408170
[22] 0.16408170 0.16408170 0.16408170

In other words, you were just missing the step of subtracting 1 from each element of the vector. You may notice the last (several) elements of this vector are equal to the result of Return.cumulative(df.xts).

Upvotes: 1

Related Questions