[Returnanalytics-commits] r2101 - in pkg/PerformanceAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 4 17:37:52 CEST 2012


Author: matthieu_lestel
Date: 2012-07-04 17:37:52 +0200 (Wed, 04 Jul 2012)
New Revision: 2101

Modified:
   pkg/PerformanceAnalytics/R/SystematicRisk.R
   pkg/PerformanceAnalytics/R/TotalRisk.R
   pkg/PerformanceAnalytics/R/TreynorRatio.R
   pkg/PerformanceAnalytics/man/SystematicRisk.Rd
   pkg/PerformanceAnalytics/man/TreynorRatio.Rd
Log:
Modified Treynor ratio with examples and documentation

Modified: pkg/PerformanceAnalytics/R/SystematicRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-03 23:05:39 UTC (rev 2100)
+++ pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-04 15:37:52 UTC (rev 2101)
@@ -2,7 +2,8 @@
 #'
 #' Systematic risk as defined by Bacon(2008) is the product of beta by market 
 #' risk. Be careful ! It's not the same definition as the one given by Michael
-#' Jensen. Market risk is the standard deviation of the benchmark.
+#' Jensen. Market risk is the standard deviation of the benchmark. The systematic
+#' risk is annualized
 #'
 #' \deqn{\sigma_s = \beta * \sigma_m} 
 #' {systematic risk = beta * market risk}
@@ -15,6 +16,7 @@
 #' asset returns
 #' @param Rb return vector of the benchmark asset
 #' @param Rf risk free rate, in same period as your returns
+#' @param Period the number of return in a year in the asset return 
 #' @param \dots any other passthru parameters
 #' @author Matthieu Lestel
 #' @references Carl Bacon, \emph{Practical portfolio performance measurement 
@@ -24,7 +26,7 @@
 #' @examples
 #'
 #' data(portfolio_bacon)
-#' print(SystematicRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 3.75
+#' print(SystematicRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 13.00
 #'
 #' data(managers)
 #' print(SystematicRisk(managers['1996',1], managers['1996',8]))
@@ -33,7 +35,7 @@
 #' @export 
 
 SystematicRisk <-
-function (Ra, Rb, Rf = 0, ...)
+function (Ra, Rb, Rf = 0, Period = 12, ...)
 {
     calcul = FALSE
     Ra = checkData(Ra, method="matrix")
@@ -48,7 +50,7 @@
       }
 
      if (calcul) {
-        result = CAPM.beta(Ra,Rb,Rf) * sqrt(sum((Rb-mean(Rb))^2)/length(Rb))
+        result = CAPM.beta(Ra,Rb,Rf) * sqrt(sum((Rb-mean(Rb))^2)/length(Rb))*sqrt(Period)
      }    
      else {
         result = NA

Modified: pkg/PerformanceAnalytics/R/TotalRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/TotalRisk.R	2012-07-03 23:05:39 UTC (rev 2100)
+++ pkg/PerformanceAnalytics/R/TotalRisk.R	2012-07-04 15:37:52 UTC (rev 2101)
@@ -49,7 +49,7 @@
      if (calcul) {
      	epsilon = Ra - Rb * CAPM.beta(Ra,Rb,Rf) - CAPM.alpha(Ra,Rb,Rf)
 	specifikRisk = sqrt(sum((epsilon - mean(epsilon))^2)/length(epsilon))*sqrt(Period)
-        result = sqrt((SystematicRisk(Ra,Rb,Rf)*sqrt(Period))^2 + specifikRisk^2)
+        result = sqrt((SystematicRisk(Ra,Rb,Rf))^2 + specifikRisk^2)
      }    
      else {
         result = NA

Modified: pkg/PerformanceAnalytics/R/TreynorRatio.R
===================================================================
--- pkg/PerformanceAnalytics/R/TreynorRatio.R	2012-07-03 23:05:39 UTC (rev 2100)
+++ pkg/PerformanceAnalytics/R/TreynorRatio.R	2012-07-04 15:37:52 UTC (rev 2101)
@@ -1,10 +1,15 @@
-#' calculate Treynor Ratio of excess return over CAPM beta
+#' calculate Treynor Ratio or modified Treynor Ratio of excess return over CAPM beta
 #' 
 #' The Treynor ratio is similar to the Sharpe Ratio, except it uses beta as the
 #' volatility measure (to divide the investment's excess return over the beta).
 #' 
+#' To calculate modified Treynor ratio, we divide the numerator by the systematic risk
+#' instead of the beta.
+#'
 #' Equation:
-#' \deqn{\frac{\overline{(R_{a}-R_{f})}}{\beta_{a,b}}}{(mean(Ra-Rf))/(Beta(Ra,Rb))}
+#' \deqn{TreynorRatio = \frac{\overline{(R_{a}-R_{f})}}{\beta_{a,b}}}{(mean(Ra-Rf))/(Beta(Ra,Rb))}
+#' \deqn{ModifiedTreynorRatio = \frac{r_p - r_f}{\sigma_s}}
+#' {ModifiedTreynorRatio = (Rp - Rf)/sytematic risk}
 #' 
 #' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
 #' asset returns
@@ -12,13 +17,18 @@
 #' @param Rf risk free rate, in same period as your returns
 #' @param scale number of periods in a year (daily scale = 252, monthly scale =
 #' 12, quarterly scale = 4)
-#' @author Peter Carl
+#' @param modified a boolean to decide whether to return the Treynor ratio or
+#' Modified Treynor ratio
+#' @author Peter Carl, Matthieu Lestel
 #' @seealso \code{\link{SharpeRatio}} \code{\link{SortinoRatio}}
 #' \code{\link{CAPM.beta}}
-#' @references \url{http://en.wikipedia.org/wiki/Treynor_ratio}
+#' @references \url{http://en.wikipedia.org/wiki/Treynor_ratio}, 
+#' Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.77
 #' @keywords ts multivariate distribution models
 #' @examples
-#' 
+#'
+#' data(portfolio_bacon) 
 #' data(managers)
 #' round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12),4) 
 #' round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4) 
@@ -26,59 +36,106 @@
 #' round(TreynorRatio(managers[,1:6], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
 #' round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf=.035/12),4) 
 #' round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
+#'
+#' print(TreynorRatio(portfolio_bacon[,1], portfolio_bacon[,2], modified = TRUE)) #expected 1.677 
+#'
+#' print(TreynorRatio(managers['1996',1], managers['1996',8], modified = TRUE))
+#' print(TreynorRatio(managers['1996',1:5], managers['1996',8], modified = TRUE)) 
 #' 
-#' 
+
+ModifiedTreynorRatio <-
+function (Ra, Rb, Rf = 0)
+{
+    calcul = FALSE
+    Ra = checkData(Ra, method="matrix")
+    Rb = checkData(Rb, method="matrix")
+
+    if (ncol(Ra)==1 || is.null(Ra) || is.vector(Ra)) {
+    
+     Rp = (prod(0.01*Ra+1)-1)*100 #portfolio total return
+     for (i in (1:length(Ra))) {
+     	 if (!is.na(Ra[i])) {
+     	    calcul = TRUE
+	 }
+      }
+     if (calcul) {
+     	     result = (Rp - Rf) / SystematicRisk(Ra, Rb, Rf)
+     }    
+     else {
+        result = NA
+     }
+      return(result)
+    }
+    else {
+        Ra = checkData(Ra)
+        result = apply(Ra, MARGIN = 2, ModifiedTreynorRatio, Rb = Rb, Rf = Rf)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("Modified Treynor Ratio (Risk free = ",Rf,")", sep="")
+        return(result)
+    }
+}
+
+
 TreynorRatio <-
-function (Ra, Rb, Rf = 0, scale = NA)
-{ # @author Peter Carl
+function (Ra, Rb, Rf = 0, scale = NA, modified = FALSE)
+{ # @author Peter Carl, Matthieu Lestel
 
-    # DESCRIPTION:
-    #
+    if(modified)
+    {
+        ModifiedTreynorRatio(Ra, Rb, Rf)
+    }
+    else
+    {
 
-    # FUNCTION:
-    Ra = checkData(Ra)
-    Rb = checkData(Rb)
-    if(!is.null(dim(Rf)))
-        Rf = checkData(Rf)
+       # DESCRIPTION:
+       #
+       
+       # FUNCTION:
+       Ra =	 checkData(Ra)
+       Rb = checkData(Rb)
+       if(!is.null(dim(Rf)))
+		Rf = checkData(Rf)
 
-    Ra.ncols = NCOL(Ra) 
-    Rb.ncols = NCOL(Rb)
+    		Ra.ncols = NCOL(Ra) 
+    		Rb.ncols = NCOL(Rb)
 
-    pairs = expand.grid(1:Ra.ncols, 1:Rb.ncols)
+    		pairs = expand.grid(1:Ra.ncols, 1:Rb.ncols)
 
-    xRa = Return.excess(Ra, Rf)
-    xRb = Return.excess(Rb, Rf)
+    		xRa = Return.excess(Ra, Rf)
+    		xRb = Return.excess(Rb, Rf)
 
-    if(is.na(scale)) {
-        freq = periodicity(Ra)
-        switch(freq$scale,
-            minute = {stop("Data periodicity too high")},
-            hourly = {stop("Data periodicity too high")},
-            daily = {scale = 252},
-            weekly = {scale = 52},
-            monthly = {scale = 12},
-            quarterly = {scale = 4},
-            yearly = {scale = 1}
-        )
-    }
+    		if(is.na(scale)) {
+        	    freq = periodicity(Ra)
+        	    switch(freq$scale,
+            	    minute = {stop("Data periodicity too high")},
+            	    hourly = {stop("Data periodicity too high")},
+            	    daily = {scale = 252},
+            	    weekly = {scale = 52},
+            	    monthly = {scale = 12},
+            	    quarterly = {scale = 4},
+            	    yearly = {scale = 1}
+        	    )
+          }
 
-    tr <-function (xRa, xRb, scale)
-    {
-        beta = CAPM.beta(xRa, xRb)
-        TR = (Return.annualized(xRa, scale = scale))/beta
-        TR
-    }
+    	  tr <-function (xRa, xRb, scale)
+    	  {
+          beta = CAPM.beta(xRa, xRb)
+          TR = (Return.annualized(xRa, scale = scale))/beta
+          TR
+    	  }
 
-    result = apply(pairs, 1, FUN = function(n, xRa, xRb, scale) tr(xRa[,n[1]], xRb[,n[2]], scale), xRa = xRa, xRb = xRb, scale = scale)
+    	  result = apply(pairs, 1, FUN = function(n, xRa, xRb, scale) tr(xRa[,n[1]], xRb[,n[2]], scale), xRa = xRa, xRb = xRb, scale = scale)
 
-    if(length(result) ==1)
-        return(result)
-    else {
-        dim(result) = c(Ra.ncols, Rb.ncols)
-        colnames(result) = paste("Treynor Ratio:", colnames(Rb))
-        rownames(result) = colnames(Ra)
-        return(t(result))
-    }
+    	  if(length(result) ==1)
+              return(result)
+    	  else {
+               dim(result) = c(Ra.ncols, Rb.ncols)
+               colnames(result) = paste("Treynor Ratio:", colnames(Rb))
+               rownames(result) = colnames(Ra)
+               return(t(result))
+    	  }
+     }
 }
 
 ###############################################################################

Modified: pkg/PerformanceAnalytics/man/SystematicRisk.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/SystematicRisk.Rd	2012-07-03 23:05:39 UTC (rev 2100)
+++ pkg/PerformanceAnalytics/man/SystematicRisk.Rd	2012-07-04 15:37:52 UTC (rev 2101)
@@ -4,7 +4,7 @@
 \alias{SystematicRisk}
 \title{Systematic risk of the return distribution}
 \usage{
-  SystematicRisk(Ra, Rb, Rf = 0, ...)
+  SystematicRisk(Ra, Rb, Rf = 0, Period = 12, ...)
 }
 \arguments{
   \item{Ra}{an xts, vector, matrix, data frame, timeSeries
@@ -14,13 +14,17 @@
 
   \item{Rf}{risk free rate, in same period as your returns}
 
+  \item{Period}{the number of return in a year in the asset
+  return}
+
   \item{\dots}{any other passthru parameters}
 }
 \description{
   Systematic risk as defined by Bacon(2008) is the product
   of beta by market risk. Be careful ! It's not the same
   definition as the one given by Michael Jensen. Market
-  risk is the standard deviation of the benchmark.
+  risk is the standard deviation of the benchmark. The
+  systematic risk is annualized
 }
 \details{
   \deqn{\sigma_s = \beta * \sigma_m} {systematic risk =
@@ -32,7 +36,7 @@
 }
 \examples{
 data(portfolio_bacon)
-print(SystematicRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 3.75
+print(SystematicRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 13.00
 
 data(managers)
 print(SystematicRisk(managers['1996',1], managers['1996',8]))

Modified: pkg/PerformanceAnalytics/man/TreynorRatio.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/TreynorRatio.Rd	2012-07-03 23:05:39 UTC (rev 2100)
+++ pkg/PerformanceAnalytics/man/TreynorRatio.Rd	2012-07-04 15:37:52 UTC (rev 2101)
@@ -1,8 +1,9 @@
 \name{TreynorRatio}
 \alias{TreynorRatio}
-\title{calculate Treynor Ratio of excess return over CAPM beta}
+\title{calculate Treynor Ratio or modified Treynor Ratio of excess return over CAPM beta}
 \usage{
-  TreynorRatio(Ra, Rb, Rf = 0, scale = NA)
+  TreynorRatio(Ra, Rb, Rf = 0, scale = NA,
+    modified = FALSE)
 }
 \arguments{
   \item{Ra}{an xts, vector, matrix, data frame, timeSeries
@@ -14,6 +15,9 @@
 
   \item{scale}{number of periods in a year (daily scale =
   252, monthly scale = 12, quarterly scale = 4)}
+
+  \item{modified}{a boolean to decide whether to return the
+  Treynor ratio or Modified Treynor ratio}
 }
 \description{
   The Treynor ratio is similar to the Sharpe Ratio, except
@@ -21,10 +25,16 @@
   investment's excess return over the beta).
 }
 \details{
+  To calculate modified Treynor ratio, we divide the
+  numerator by the systematic risk instead of the beta.
+
   Equation:
   \deqn{\frac{\overline{(R_{a}-R_{f})}}{\beta_{a,b}}}{(mean(Ra-Rf))/(Beta(Ra,Rb))}
+  \deqn{ModifiedTreynorRatio = \frac{r_p - r_f}{\sigma_s}}
+  {ModifiedTreynorRatio = (Rp - Rf)/sytematic risk}
 }
 \examples{
+data(portfolio_bacon)
 data(managers)
 round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12),4)
 round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
@@ -32,12 +42,19 @@
 round(TreynorRatio(managers[,1:6], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
 round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf=.035/12),4)
 round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
+
+print(TreynorRatio(portfolio_bacon[,1], portfolio_bacon[,2], modified = TRUE)) #expected 1.677
+
+print(TreynorRatio(managers['1996',1], managers['1996',8], modified = TRUE))
+print(TreynorRatio(managers['1996',1:5], managers['1996',8], modified = TRUE))
 }
 \author{
-  Peter Carl
+  Peter Carl, Matthieu Lestel
 }
 \references{
-  \url{http://en.wikipedia.org/wiki/Treynor_ratio}
+  \url{http://en.wikipedia.org/wiki/Treynor_ratio} Carl
+  Bacon, \emph{Practical portfolio performance measurement
+  and attribution}, second edition 2008 p.77
 }
 \seealso{
   \code{\link{SharpeRatio}} \code{\link{SortinoRatio}}



More information about the Returnanalytics-commits mailing list