PortfolioAnalytics icon indicating copy to clipboard operation
PortfolioAnalytics copied to clipboard

optimize.portfolio.rebalancing() might have mishandled the situation that training_period is NULL and rolling_window is specified

Open caicaifearless opened this issue 4 years ago • 0 comments

if(is.null(training_period) & !is.null(rolling_window))
            training_period <- rolling_window
          
          if(is.null(training_period)) {if(nrow(R)<36) training_period=nrow(R) else training_period=36}
          if (is.null(rolling_window)){
            # define the index endpoints of our periods
            ep.i<-endpoints(R,on=rebalance_on)[which(endpoints(R, on = rebalance_on)>=training_period)]
            # now apply optimize.portfolio to the periods, in parallel if available
            ep <- ep.i[1]
            out_list<-foreach::foreach(ep=iterators::iter(ep.i), .errorhandling='pass', .packages='PortfolioAnalytics') %dopar% {
              optimize.portfolio(R[1:ep,], portfolio=portfolio, optimize_method=optimize_method, search_size=search_size, trace=trace, rp=rp, parallel=FALSE, ...=...)
            }
          } else {
            # define the index endpoints of our periods
            ep.i<-endpoints(R,on=rebalance_on)[which(endpoints(R, on = rebalance_on)>=training_period)]
            # now apply optimize.portfolio to the periods, in parallel if available
            out_list<-foreach::foreach(ep=iterators::iter(ep.i), .errorhandling='pass', .packages='PortfolioAnalytics') %dopar% {
              optimize.portfolio(R[(ifelse(ep-rolling_window>=1,ep-rolling_window,1)):ep,], portfolio=portfolio, optimize_method=optimize_method, search_size=search_size, trace=trace, rp=rp, parallel=FALSE, ...=...)
            }

As in this code, when training_period is NULL and rolling_window is specified, training_period will be set to equal to rolling_window. Say we have rolling_window = 48, traing_period will also be 48. When defining the index endpoins of our periods, ep.i will be 48, 49, 50, 51, ... . However R[(ifelse(ep-rolling_window>=1,ep-rolling_window,1)):ep,] doesn't keep the rolling window length to be 48 all the time. For ep.i in {48, 49, 50, 51, ... } , the window will be R[1:48, ], R[1:49, ], R[2:50, ], R[3:51, ] ... . The window length becomes 49.

Also when I run extractWeights(optimize.portfolio.rebalancing(R[1:51, ], portfolio = gmvp_spec,optimize_method = 'ROI', rebalance_on='months',rolling_window = 48))[2,] == extractWeights(optimize.portfolio(R[1:49, ], portfolio = gmvp_spec,optimize_method = 'ROI')), the result is TRUE. This means the second window is R[1:49, ]

caicaifearless avatar Aug 07 '20 14:08 caicaifearless