[R-SIG-Finance] Return.portfolio() in PerformanceAnalytics and discrete returns

susan22 at mail.com susan22 at mail.com
Thu Jun 21 09:22:53 CEST 2012


Thanks Joshua,
 
 in the meantime, I have taken a closer look at function Return.portfolio() in package PerformanceAnalytics and something seems quite odd. I hope I am not making a basic mistake here in my thinking, but it seems that this function does not handle discrete returns properly.
 
 Things start out fine with a weighted wealth index being constructed by geometrically linking discrete period returns:
 
       wealthindex.assets=cumprod(1+R[,colnames(weights)])
 
       wealthindex.weighted = matrix(nrow=nrow(R),ncol=ncol(R[,colnames(weights)]))
       colnames(wealthindex.weighted)=colnames(wealthindex.assets)
       rownames(wealthindex.weighted)=as.character(index(wealthindex.assets))
       # weight the results
       for (col in colnames(weights)){
           wealthindex.weighted[,col]=weights[,col]*wealthindex.assets[,col]
       }
       wealthindex=apply(wealthindex.weighted,1,sum)
 
 But then after line 181 the function stops in its tracks, abandons its results so far except for the unweighted individual gross cumulative returns and instead of doing something along the lines of the following to decompose wealthindex into discrete portfolio period returns
 
 result <- as-matrix(c(wealthindex[1]-1, wealthindex[2]/wealthindex[1]-1,  wealthindex[3]/wealthindex[2]-1, ... ), ncol = 1)
 
 the returns are treated as if they were additive log returns
 
       # weighted cumulative returns
       weightedcumcont=t(apply (wealthindex.assets,1, function(x,weights){ as.vector((x-1)* weights)},weights=weights))
       weightedreturns=diff(rbind(0,weightedcumcont)) # compound returns
       colnames(weightedreturns)=colnames(wealthindex.assets)
       if (!wealth.index){
         result=as.matrix(apply(weightedreturns,1,sum),ncol=1)
       } else {
         wealthindex=matrix(cumprod(1 + as.matrix(apply(weightedreturns,1, sum), ncol = 1)),ncol=1)
       }


 
  ----- Original Message -----
  From: Joshua Ulrich
  Sent: 06/20/12 09:52 PM
  To: susan22 at mail.com
  Subject: Re: [R-SIG-Finance] issue with xts affectin Return.calculate in PerformanceAnalytics
 
   
On Tue, Jun 19, 2012 at 1:43 PM,  <susan22 at mail.com> wrote:
> I came across the issue below trying to calculate simple returns of an xts
> object using Return.calculate() in PerformanceAnalytics. This returns a
> series of all zeros. The root of the problem, however, seems to be with xts
> for which division seems to be a challenge. What am I missing? Coercion to
> numeric is not really a reasonable solution.
>
Return.calculate was fixed on R-Forge on April 4, 2012. Please see
this SO question about installing from R-Forge:
http://stackoverflow.com/q/11105131/271616

Division is only a challenge when you try to divide two objects that
do not have any index values in common. Use lag() to align the index
instead.

>> p <- to.monthly(as.xts(s.ts), OHLC = FALSE)
>> head(aap)
>                    s1           s2
> Dec 1999      1000.00      1000.00
> Jan 2000      1021.27       959.85
> Feb 2000      1017.30       962.06
> Mar 2000      1022.99      1008.44
> Apr 2000      1015.95       980.95
> May 2000      1044.30       963.79
>
>> r <- Return.calculate(aap, method = "simple")
>> head(r)
>                    s1           s2
> Dec 1999            0            0
> Jan 2000            0            0
> Feb 2000            0            0
> Mar 2000            0            0
> Apr 2000            0            0
> May 2000            0            0
>
>> lr <- Return.calculate(aap, method = "compound")
>> head(lr)
>                         s1                s2
> Dec 1999                NA                NA
> Jan 2000  0.02104695084364 -0.04097825672856
> Feb 2000 -0.00389489202608  0.00229979652957
> Mar 2000  0.00557765293308  0.04708304254280
> Apr 2000 -0.00690557640376 -0.02763837145911
> May 2000  0.02752266915648 -0.01764806131103
>
>> p[2]/p[1]-1
> Data:
> numeric(0)
>
> Index:
> NULL
>
>> as.numeric(p[2])/as.numeric(p[1])-1
> [1]  0.02127 -0.04015
>

Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
   

 

 



More information about the R-SIG-Finance mailing list