[R-SIG-Finance] PerformanceAnalytics - small problem with Return.excess

Brian G. Peterson brian at braverock.com
Tue May 11 16:33:29 CEST 2010


On 05/10/2010 10:52 AM, Giuseppe Milicia wrote:
> Brian,
>
> I have a patched version of the function, I attach it at the end of this mail.

Thanks for your patch, it is in SVN on R-Forge, and will be included in the 
next version of PerformanceAnalytics on CRAN.

> I'm having problems with the SharpeRatio function as well.
>
>> >SharpeRatio(managers[,1,drop=FALSE], FUN="StdDev")
>                                       HAM1
> StdDev Sharpe:  (Rf=0%, p=95%) 0.02562881
>
> But:
>
>> >  mean(managers[, 1, drop = FALSE])/sd(managers[, 1, drop = FALSE])
>       HAM1
> 0.4339932
>
> And
>
>> >  sd(managers[, 1, drop = FALSE])
>        HAM1
> 0.02562881
>
> Debugging the function, it seems that the call:
>
>   result[i, ] = apply(R, 2, srm, xR = xR, Rf = Rf, p = p, FUN = FUNCT, ...)
>
> yields only FUN(R) but the call (while in debug mode)
>
>
> srm(R,xR=xR,FUN=FUNCT)
>
> Gives the correct result... I might be doing something wrong, but I can't see what... Ideas?
>

You were correct, 'FUN' wasn't getting passed correctly.  It looks like I 
introduced the bug when I made it possible to pass multiple functions in for 
creating modified Sharpe ratios.

Peter Carl and I have patched the function so it again works as expected.

Thanks for the report.  The fixed version of the function is attached below. 
If you could confirm that this works as expected again, we'd appreciate it.

Regards,

    - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

--

SharpeRatio <-
function (R, Rf = 0, p = 0.95, FUN=c("StdDev", "VaR","ES"), weights=NULL, ...)
{
     # @TODO: annualize using multiperiod VaR and ES calcs

     # FUNCTION:

     R = checkData(R)

     if(!is.null(dim(Rf)))
         Rf = checkData(Rf)

     if(is.null(weights))
         xR = Return.excess(R, Rf)

     srm <-function (R, ..., Rf, p, FUNC)
     {
         FUNCT <- match.fun(FUNC)
         xR = Return.excess(R, Rf)
         SRM = mean(xR, na.rm=TRUE)/FUNCT(R=R, p=p, ...=..., invert=FALSE)
         SRM
     }

     i=1
     if(is.null(weights)){
         result = matrix(nrow=length(FUN), ncol=ncol(R))
         colnames(result) = colnames(R)
     }
     else {
         result = matrix(nrow=length(FUN))
     }

     tmprownames=vector()

     for (FUNCT in FUN){
         if (is.null(weights)){
             result[i,] = apply(R,MARGIN=2,FUN=srm,Rf=Rf,p=p,FUNC=FUNCT,...)
         }
         else {
             result[i,] = 
weighted.mean(xR,w=weights,na.rm=TRUE)/match.fun(FUNCT)(R, Rf=Rf, p=p, 
weights=weights, portfolio_method="single", ...=...)
         }
         tmprownames = c(tmprownames, paste(FUNCT, " Sharpe: ", " (Rf=", 
round(mean(Rf)*100,1), "%, p=", round(p*100,1),"%)", sep=""))
         i=i+1 #increment counter
     }
     rownames(result)=tmprownames
     return (result)
}



More information about the R-SIG-Finance mailing list