[R-SIG-Finance] possible bug in PerformanceAnalytics 1.0.3.2 (Return.rebalancing)

Andrew Vishnyakov avishn at gmail.com
Thu Jun 9 20:30:24 CEST 2011


When calculating Return.rebalancing, if the last rebalancing date
falls on the last date of the R (input) timeseries, the function
returns dups for the next to last rebalancing period.

Return.portfolio.R --

 # loop:
    for (row in 1:nrow(weights)){
        from =as.Date(index(weights[row,]))+1
        if (row == nrow(weights)){
           to = as.Date(index(last(R))) # this is correct
        } else {
           to = as.Date(index(weights[(row+1),]))
        }
        if(row==1){
            startingwealth=1
        }
        tmpR<-R[paste(from,to,sep="/"),]
        if (nrow(tmpR)>=1){
            resultreturns=Return.portfolio(tmpR,weights=weights[row,], ...=...)
        }
        if(row==1){
            result = resultreturns
        } else {
            result = rbind(result,resultreturns)
        }
        startingwealth=last(cumprod(1+result)*startingwealth)
    }

In the loop above if row == nrow(weights) and the last date in weights
== last date in R, then "from" date ends up being beyond the last row
of R. This causes tmpR to be empty and the last "if" statement ends up
rbind'ing resultreturns from the previous iteration to result the
second time.



More information about the R-SIG-Finance mailing list