Return.rebalancing and rebalancing on consecutive days

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Return.rebalancing and rebalancing on consecutive days

Paul Ramer
When I rebalance my portfolio on consecutive days, I get an error:
Error in `[.xts`(result, 2:length(result)) : subscript out of bounds.

So, I wonder what I am doing wrong.

Below is a code snippet both with and without the error. The first is
rebalanced on consecutive days whereas the second one has at least a
day between all rebalancing. I ran debug on Return.rebalancing. With
consecutive rebalancing days, the only thing I could find was the
'from' and 'to' variables had the same date when the 'R' variable was
subset.

data(edhec)
data(weights)

days <- c("2000-05-01 18:00:00 MDT", "2000-05-02 18:00:00 MDT",
          "2000-05-03 18:00:00 MDT", "2000-05-04 18:00:00 MDT",
          "2000-05-07 18:00:00 MDT", "2000-05-08 18:00:00 MDT",
          "2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
          "2000-05-11 18:00:00 MDT", "2000-05-14 18:00:00 MDT",
          "2000-05-15 18:00:00 MDT", "2000-05-16 18:00:00 MDT",
          "2000-05-17 18:00:00 MDT", "2000-05-18 18:00:00 MDT",
          "2000-05-21 18:00:00 MDT", "2000-05-22 18:00:00 MDT",
          "2000-05-23 18:00:00 MDT", "2000-05-24 18:00:00 MDT",
          "2000-05-25 18:00:00 MDT", "2000-05-29 18:00:00 MDT",
          "2000-05-30 18:00:00 MDT", "2000-05-31 18:00:00 MDT")

may.reb <- c("2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
             "2000-05-15 18:00:00 MDT")

may.reb.2 <- c("2000-05-09 18:00:00 MDT", "2000-05-11 18:00:00 MDT",
               "2000-05-15 18:00:00 MDT")

e1 <- edhec[1:length(days), ]
index(e1) <- as.Date(days)

w1 <- weights[1:3, ]
index(w1) <- as.Date(may.reb)
Return.rebalancing(e1, w1)

w2 <- weights[1:3, ]
index(w2) <- as.Date(may.reb.2)
Return.rebalancing(e1, w2)

Session Info:
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
 [1] reshape_0.8.4              plyr_1.8
 [3] latticeExtra_0.6-24        RColorBrewer_1.0-5
 [5] Hmisc_3.10-1               survival_2.36-14
 [7] PerformanceAnalytics_1.1.0 quantmod_0.4-0
 [9] Defaults_1.1-1             TTR_0.21-1
[11] xts_0.9-3.2                zoo_1.7-10
[13] lattice_0.20-13

loaded via a namespace (and not attached):
[1] cluster_1.14.3  compiler_2.15.2 grid_2.15.2     tools_2.15.2

Any guidance is appreciated.

Thank you,

----
Paul Ramer, CAIA, CFA
[hidden email]
(303) 359-4192

_______________________________________________
[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: Return.rebalancing and rebalancing on consecutive days

Jeffrey Ryan
Not having tried your code from my phone, you can start by setting TZ to UTC

Ideally the data source is agnostic on this but a simple fix should be Sys.setenv(TZ='UTC')

HTH
Jeff

Jeffrey Ryan    |    Founder    |    [hidden email]

www.lemnica.com

On Mar 2, 2013, at 7:18 PM, Paul Ramer <[hidden email]> wrote:

> When I rebalance my portfolio on consecutive days, I get an error:
> Error in `[.xts`(result, 2:length(result)) : subscript out of bounds.
>
> So, I wonder what I am doing wrong.
>
> Below is a code snippet both with and without the error. The first is
> rebalanced on consecutive days whereas the second one has at least a
> day between all rebalancing. I ran debug on Return.rebalancing. With
> consecutive rebalancing days, the only thing I could find was the
> 'from' and 'to' variables had the same date when the 'R' variable was
> subset.
>
> data(edhec)
> data(weights)
>
> days <- c("2000-05-01 18:00:00 MDT", "2000-05-02 18:00:00 MDT",
>          "2000-05-03 18:00:00 MDT", "2000-05-04 18:00:00 MDT",
>          "2000-05-07 18:00:00 MDT", "2000-05-08 18:00:00 MDT",
>          "2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>          "2000-05-11 18:00:00 MDT", "2000-05-14 18:00:00 MDT",
>          "2000-05-15 18:00:00 MDT", "2000-05-16 18:00:00 MDT",
>          "2000-05-17 18:00:00 MDT", "2000-05-18 18:00:00 MDT",
>          "2000-05-21 18:00:00 MDT", "2000-05-22 18:00:00 MDT",
>          "2000-05-23 18:00:00 MDT", "2000-05-24 18:00:00 MDT",
>          "2000-05-25 18:00:00 MDT", "2000-05-29 18:00:00 MDT",
>          "2000-05-30 18:00:00 MDT", "2000-05-31 18:00:00 MDT")
>
> may.reb <- c("2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>             "2000-05-15 18:00:00 MDT")
>
> may.reb.2 <- c("2000-05-09 18:00:00 MDT", "2000-05-11 18:00:00 MDT",
>               "2000-05-15 18:00:00 MDT")
>
> e1 <- edhec[1:length(days), ]
> index(e1) <- as.Date(days)
>
> w1 <- weights[1:3, ]
> index(w1) <- as.Date(may.reb)
> Return.rebalancing(e1, w1)
>
> w2 <- weights[1:3, ]
> index(w2) <- as.Date(may.reb.2)
> Return.rebalancing(e1, w2)
>
> Session Info:
> R version 2.15.2 (2012-10-26)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] splines   stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] reshape_0.8.4              plyr_1.8
> [3] latticeExtra_0.6-24        RColorBrewer_1.0-5
> [5] Hmisc_3.10-1               survival_2.36-14
> [7] PerformanceAnalytics_1.1.0 quantmod_0.4-0
> [9] Defaults_1.1-1             TTR_0.21-1
> [11] xts_0.9-3.2                zoo_1.7-10
> [13] lattice_0.20-13
>
> loaded via a namespace (and not attached):
> [1] cluster_1.14.3  compiler_2.15.2 grid_2.15.2     tools_2.15.2
>
> Any guidance is appreciated.
>
> Thank you,
>
> ----
> Paul Ramer, CAIA, CFA
> [hidden email]
> (303) 359-4192
>
> _______________________________________________
> [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
|

Re: Return.rebalancing and rebalancing on consecutive days

Paul Ramer
Changing the TZ to UTC results in the same error.

Paul
----
Paul Ramer, CAIA, CFA
[hidden email]
(303) 359-4192



On Sat, Mar 2, 2013 at 6:34 PM, Jeff Ryan <[hidden email]> wrote:

> Not having tried your code from my phone, you can start by setting TZ to UTC
>
> Ideally the data source is agnostic on this but a simple fix should be Sys.setenv(TZ='UTC')
>
> HTH
> Jeff
>
> Jeffrey Ryan    |    Founder    |    [hidden email]
>
> www.lemnica.com
>
> On Mar 2, 2013, at 7:18 PM, Paul Ramer <[hidden email]> wrote:
>
>> When I rebalance my portfolio on consecutive days, I get an error:
>> Error in `[.xts`(result, 2:length(result)) : subscript out of bounds.
>>
>> So, I wonder what I am doing wrong.
>>
>> Below is a code snippet both with and without the error. The first is
>> rebalanced on consecutive days whereas the second one has at least a
>> day between all rebalancing. I ran debug on Return.rebalancing. With
>> consecutive rebalancing days, the only thing I could find was the
>> 'from' and 'to' variables had the same date when the 'R' variable was
>> subset.
>>
>> data(edhec)
>> data(weights)
>>
>> days <- c("2000-05-01 18:00:00 MDT", "2000-05-02 18:00:00 MDT",
>>          "2000-05-03 18:00:00 MDT", "2000-05-04 18:00:00 MDT",
>>          "2000-05-07 18:00:00 MDT", "2000-05-08 18:00:00 MDT",
>>          "2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>>          "2000-05-11 18:00:00 MDT", "2000-05-14 18:00:00 MDT",
>>          "2000-05-15 18:00:00 MDT", "2000-05-16 18:00:00 MDT",
>>          "2000-05-17 18:00:00 MDT", "2000-05-18 18:00:00 MDT",
>>          "2000-05-21 18:00:00 MDT", "2000-05-22 18:00:00 MDT",
>>          "2000-05-23 18:00:00 MDT", "2000-05-24 18:00:00 MDT",
>>          "2000-05-25 18:00:00 MDT", "2000-05-29 18:00:00 MDT",
>>          "2000-05-30 18:00:00 MDT", "2000-05-31 18:00:00 MDT")
>>
>> may.reb <- c("2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>>             "2000-05-15 18:00:00 MDT")
>>
>> may.reb.2 <- c("2000-05-09 18:00:00 MDT", "2000-05-11 18:00:00 MDT",
>>               "2000-05-15 18:00:00 MDT")
>>
>> e1 <- edhec[1:length(days), ]
>> index(e1) <- as.Date(days)
>>
>> w1 <- weights[1:3, ]
>> index(w1) <- as.Date(may.reb)
>> Return.rebalancing(e1, w1)
>>
>> w2 <- weights[1:3, ]
>> index(w2) <- as.Date(may.reb.2)
>> Return.rebalancing(e1, w2)
>>
>> Session Info:
>> R version 2.15.2 (2012-10-26)
>> Platform: i386-w64-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252
>> [2] LC_CTYPE=English_United States.1252
>> [3] LC_MONETARY=English_United States.1252
>> [4] LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] splines   stats     graphics  grDevices utils     datasets  methods
>> [8] base
>>
>> other attached packages:
>> [1] reshape_0.8.4              plyr_1.8
>> [3] latticeExtra_0.6-24        RColorBrewer_1.0-5
>> [5] Hmisc_3.10-1               survival_2.36-14
>> [7] PerformanceAnalytics_1.1.0 quantmod_0.4-0
>> [9] Defaults_1.1-1             TTR_0.21-1
>> [11] xts_0.9-3.2                zoo_1.7-10
>> [13] lattice_0.20-13
>>
>> loaded via a namespace (and not attached):
>> [1] cluster_1.14.3  compiler_2.15.2 grid_2.15.2     tools_2.15.2
>>
>> Any guidance is appreciated.
>>
>> Thank you,
>>
>> ----
>> Paul Ramer, CAIA, CFA
>> [hidden email]
>> (303) 359-4192
>>
>> _______________________________________________
>> [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
|

Re: Return.rebalancing and rebalancing on consecutive days

Peter Carl-2
In reply to this post by Paul Ramer
Paul,

This looks like a situation in Return.portfolio where it is being passed a
single row of returns that is causing the error.

> Return.portfolio(e1[7,],weights=w1[1,])
Error in `[.xts`(result, 2:length(result)) : subscript out of bounds
> Return.portfolio(e1[7:8,],weights=w1[1,])
           portfolio.returns
2000-05-09       0.029817496
2000-05-10      -0.002608741

This has to do with how the weights and returns are assumed to be
synchronized through time.  Both returns and weights are thought to be
timestamped at the end-of-period, so the weights are shifted forward to
apply to the next month's returns.  From the documentation:

Rebalancing periods can be thought of as taking effect immediately after
the close of the bar. So, a March 31 rebalancing date will actually be in
effect for April 1. A December 31 rebalancing date will be in effect on
Jan 1, and so forth. This convention was chosen because it fits with
common usage, and because it simplifies xts Date subsetting via endpoints.

So this appears to be a corner case where a single row of returns is being
passed in and the weights get shifted out of bounds.

Thanks for the reproducible error.  We'll take a look and find a solution.

pcc
--
Peter Carl
http://www.braverock.com/peter

> When I rebalance my portfolio on consecutive days, I get an error:
> Error in `[.xts`(result, 2:length(result)) : subscript out of bounds.
>
> So, I wonder what I am doing wrong.
>
> Below is a code snippet both with and without the error. The first is
> rebalanced on consecutive days whereas the second one has at least a
> day between all rebalancing. I ran debug on Return.rebalancing. With
> consecutive rebalancing days, the only thing I could find was the
> 'from' and 'to' variables had the same date when the 'R' variable was
> subset.
>
> data(edhec)
> data(weights)
>
> days <- c("2000-05-01 18:00:00 MDT", "2000-05-02 18:00:00 MDT",
>           "2000-05-03 18:00:00 MDT", "2000-05-04 18:00:00 MDT",
>           "2000-05-07 18:00:00 MDT", "2000-05-08 18:00:00 MDT",
>           "2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>           "2000-05-11 18:00:00 MDT", "2000-05-14 18:00:00 MDT",
>           "2000-05-15 18:00:00 MDT", "2000-05-16 18:00:00 MDT",
>           "2000-05-17 18:00:00 MDT", "2000-05-18 18:00:00 MDT",
>           "2000-05-21 18:00:00 MDT", "2000-05-22 18:00:00 MDT",
>           "2000-05-23 18:00:00 MDT", "2000-05-24 18:00:00 MDT",
>           "2000-05-25 18:00:00 MDT", "2000-05-29 18:00:00 MDT",
>           "2000-05-30 18:00:00 MDT", "2000-05-31 18:00:00 MDT")
>
> may.reb <- c("2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>              "2000-05-15 18:00:00 MDT")
>
> may.reb.2 <- c("2000-05-09 18:00:00 MDT", "2000-05-11 18:00:00 MDT",
>                "2000-05-15 18:00:00 MDT")
>
> e1 <- edhec[1:length(days), ]
> index(e1) <- as.Date(days)
>
> w1 <- weights[1:3, ]
> index(w1) <- as.Date(may.reb)
> Return.rebalancing(e1, w1)
>
> w2 <- weights[1:3, ]
> index(w2) <- as.Date(may.reb.2)
> Return.rebalancing(e1, w2)
>
> Session Info:
> R version 2.15.2 (2012-10-26)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] splines   stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
>  [1] reshape_0.8.4              plyr_1.8
>  [3] latticeExtra_0.6-24        RColorBrewer_1.0-5
>  [5] Hmisc_3.10-1               survival_2.36-14
>  [7] PerformanceAnalytics_1.1.0 quantmod_0.4-0
>  [9] Defaults_1.1-1             TTR_0.21-1
> [11] xts_0.9-3.2                zoo_1.7-10
> [13] lattice_0.20-13
>
> loaded via a namespace (and not attached):
> [1] cluster_1.14.3  compiler_2.15.2 grid_2.15.2     tools_2.15.2
>
> Any guidance is appreciated.
>
> Thank you,
>
> ----
> Paul Ramer, CAIA, CFA
> [hidden email]
> (303) 359-4192
>
> _______________________________________________
> [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
|

Re: Return.rebalancing and rebalancing on consecutive days

Paul Ramer
Thanks for looking at the problem. What is standard procedure at this
point? Will the thread be updated with suggested solution or should I
watch the package updates for the solution?

Thank you,

Paul
----
Paul Ramer, CAIA, CFA
[hidden email]
(303) 359-4192



On Sat, Mar 2, 2013 at 7:35 PM, Peter Carl <[hidden email]> wrote:

> Paul,
>
> This looks like a situation in Return.portfolio where it is being passed a
> single row of returns that is causing the error.
>
>> Return.portfolio(e1[7,],weights=w1[1,])
> Error in `[.xts`(result, 2:length(result)) : subscript out of bounds
>> Return.portfolio(e1[7:8,],weights=w1[1,])
>            portfolio.returns
> 2000-05-09       0.029817496
> 2000-05-10      -0.002608741
>
> This has to do with how the weights and returns are assumed to be
> synchronized through time.  Both returns and weights are thought to be
> timestamped at the end-of-period, so the weights are shifted forward to
> apply to the next month's returns.  From the documentation:
>
> Rebalancing periods can be thought of as taking effect immediately after
> the close of the bar. So, a March 31 rebalancing date will actually be in
> effect for April 1. A December 31 rebalancing date will be in effect on
> Jan 1, and so forth. This convention was chosen because it fits with
> common usage, and because it simplifies xts Date subsetting via endpoints.
>
> So this appears to be a corner case where a single row of returns is being
> passed in and the weights get shifted out of bounds.
>
> Thanks for the reproducible error.  We'll take a look and find a solution.
>
> pcc
> --
> Peter Carl
> http://www.braverock.com/peter
>
>> When I rebalance my portfolio on consecutive days, I get an error:
>> Error in `[.xts`(result, 2:length(result)) : subscript out of bounds.
>>
>> So, I wonder what I am doing wrong.
>>
>> Below is a code snippet both with and without the error. The first is
>> rebalanced on consecutive days whereas the second one has at least a
>> day between all rebalancing. I ran debug on Return.rebalancing. With
>> consecutive rebalancing days, the only thing I could find was the
>> 'from' and 'to' variables had the same date when the 'R' variable was
>> subset.
>>
>> data(edhec)
>> data(weights)
>>
>> days <- c("2000-05-01 18:00:00 MDT", "2000-05-02 18:00:00 MDT",
>>           "2000-05-03 18:00:00 MDT", "2000-05-04 18:00:00 MDT",
>>           "2000-05-07 18:00:00 MDT", "2000-05-08 18:00:00 MDT",
>>           "2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>>           "2000-05-11 18:00:00 MDT", "2000-05-14 18:00:00 MDT",
>>           "2000-05-15 18:00:00 MDT", "2000-05-16 18:00:00 MDT",
>>           "2000-05-17 18:00:00 MDT", "2000-05-18 18:00:00 MDT",
>>           "2000-05-21 18:00:00 MDT", "2000-05-22 18:00:00 MDT",
>>           "2000-05-23 18:00:00 MDT", "2000-05-24 18:00:00 MDT",
>>           "2000-05-25 18:00:00 MDT", "2000-05-29 18:00:00 MDT",
>>           "2000-05-30 18:00:00 MDT", "2000-05-31 18:00:00 MDT")
>>
>> may.reb <- c("2000-05-09 18:00:00 MDT", "2000-05-10 18:00:00 MDT",
>>              "2000-05-15 18:00:00 MDT")
>>
>> may.reb.2 <- c("2000-05-09 18:00:00 MDT", "2000-05-11 18:00:00 MDT",
>>                "2000-05-15 18:00:00 MDT")
>>
>> e1 <- edhec[1:length(days), ]
>> index(e1) <- as.Date(days)
>>
>> w1 <- weights[1:3, ]
>> index(w1) <- as.Date(may.reb)
>> Return.rebalancing(e1, w1)
>>
>> w2 <- weights[1:3, ]
>> index(w2) <- as.Date(may.reb.2)
>> Return.rebalancing(e1, w2)
>>
>> Session Info:
>> R version 2.15.2 (2012-10-26)
>> Platform: i386-w64-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252
>> [2] LC_CTYPE=English_United States.1252
>> [3] LC_MONETARY=English_United States.1252
>> [4] LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] splines   stats     graphics  grDevices utils     datasets  methods
>> [8] base
>>
>> other attached packages:
>>  [1] reshape_0.8.4              plyr_1.8
>>  [3] latticeExtra_0.6-24        RColorBrewer_1.0-5
>>  [5] Hmisc_3.10-1               survival_2.36-14
>>  [7] PerformanceAnalytics_1.1.0 quantmod_0.4-0
>>  [9] Defaults_1.1-1             TTR_0.21-1
>> [11] xts_0.9-3.2                zoo_1.7-10
>> [13] lattice_0.20-13
>>
>> loaded via a namespace (and not attached):
>> [1] cluster_1.14.3  compiler_2.15.2 grid_2.15.2     tools_2.15.2
>>
>> Any guidance is appreciated.
>>
>> Thank you,
>>
>> ----
>> Paul Ramer, CAIA, CFA
>> [hidden email]
>> (303) 359-4192
>>
>> _______________________________________________
>> [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.