[Returnanalytics-commits] r3257 - pkg/PerformanceAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Nov 14 20:14:08 CET 2013


Author: peter_carl
Date: 2013-11-14 20:14:08 +0100 (Thu, 14 Nov 2013)
New Revision: 3257

Modified:
   pkg/PerformanceAnalytics/R/Return.annualized.excess.R
Log:
- fixed example to use managers dataset

Modified: pkg/PerformanceAnalytics/R/Return.annualized.excess.R
===================================================================
--- pkg/PerformanceAnalytics/R/Return.annualized.excess.R	2013-11-14 19:08:37 UTC (rev 3256)
+++ pkg/PerformanceAnalytics/R/Return.annualized.excess.R	2013-11-14 19:14:08 UTC (rev 3257)
@@ -1,78 +1,77 @@
-#' calculates an annualized excess return for comparing instruments with different
-#' length history
-#' 
-#' An average annualized excess return is convenient for comparing excess 
-#' returns.
-#' 
-#' Annualized returns are useful for comparing two assets. To do so, you must
-#' scale your observations to an annual scale by raising the compound return to
-#' the number of periods in a year, and taking the root to the number of total 
-#' observations:
-#' \deqn{prod(1+R_{a})^{\frac{scale}{n}}-1=\sqrt[n]{prod(1+R_{a})^{scale}}-
-#' 1}{prod(1 + Ra)^(scale/n) - 1}
-#' 
-#' where scale is the number of periods in a year, and n is the total number of
-#' periods for which you have observations.
-#' 
-#' Finally having annualized returns for portfolio and benchmark we can compute
-#' annualized excess return as difference in the annualized portfolio and 
-#' benchmark returns in the arithmetic case:
-#' \deqn{er = R_{pa} - R_{ba}}{er = Rpa - Rba}
-#' 
-#' and as a geometric difference in the geometric case:
-#' \deqn{er = \frac{(1 + R_{pa})}{(1 + R_{ba})} - 1}{er = (1 + Rpa) / (1 + Rba) - 1}
-#' 
-#' @param Rp an xts, vector, matrix, data frame, timeSeries or zoo object of
-#' portfolio returns
-#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of 
-#' benchmark returns
-#' @param scale number of periods in a year (daily scale = 252, monthly scale =
-#' 12, quarterly scale = 4)
-#' @param geometric generate geometric (TRUE) or simple (FALSE) excess returns,
-#' default TRUE
-#' @author Andrii Babii
-#' @seealso \code{\link{Return.annualized}},
-#' @references Bacon, Carl. \emph{Practical Portfolio Performance Measurement
-#' and Attribution}. Wiley. 2004. p. 206-207
-#' @keywords ts multivariate distribution models
-#' @examples
-#' 
-#' data(attrib)
-#' Return.annualized.excess(Rp = attrib.returns[, 21], Rb = attrib.returns[, 22])
-#' 
-#' @export
-Return.annualized.excess <- 
-function (Rp, Rb, scale = NA, geometric = TRUE )
-{ # @author Andrii Babii
-    Rp = checkData(Rp)
-    Rb = checkData(Rb)
-    
-    Rp = na.omit(Rp)
-    Rb = na.omit(Rb)
-    n = nrow(Rp)
-    if(is.na(scale)) {
-      freq = periodicity(Rp)
-      switch(freq$scale,
-             minute = {stop("Data periodicity too high")},
-             hourly = {stop("Data periodicity too high")},
-             daily = {scale = 252},
-             eekly = {scale = 52},
-             monthly = {scale = 12},
-             quarterly = {scale = 4},
-             yearly = {scale = 1}
-             )
-    }
-    Rpa = apply(1 + Rp, 2, prod)^(scale/n) - 1
-    Rba = apply(1 + Rb, 2, prod)^(scale/n) - 1
-    if (geometric) {
-      # geometric excess returns
-      result = (1 + Rpa) / (1 + Rba) - 1
-    } else {
-      # arithmetic excess returns
-      result = Rpa - Rba
-    }
-    dim(result) = c(1,NCOL(Rp))
-    colnames(result) = colnames(Rp)
-    rownames(result) = "Annualized Return"
-    return(result)
+#' calculates an annualized excess return for comparing instruments with different
+#' length history
+#' 
+#' An average annualized excess return is convenient for comparing excess 
+#' returns.
+#' 
+#' Annualized returns are useful for comparing two assets. To do so, you must
+#' scale your observations to an annual scale by raising the compound return to
+#' the number of periods in a year, and taking the root to the number of total 
+#' observations:
+#' \deqn{prod(1+R_{a})^{\frac{scale}{n}}-1=\sqrt[n]{prod(1+R_{a})^{scale}}-
+#' 1}{prod(1 + Ra)^(scale/n) - 1}
+#' 
+#' where scale is the number of periods in a year, and n is the total number of
+#' periods for which you have observations.
+#' 
+#' Finally having annualized returns for portfolio and benchmark we can compute
+#' annualized excess return as difference in the annualized portfolio and 
+#' benchmark returns in the arithmetic case:
+#' \deqn{er = R_{pa} - R_{ba}}{er = Rpa - Rba}
+#' 
+#' and as a geometric difference in the geometric case:
+#' \deqn{er = \frac{(1 + R_{pa})}{(1 + R_{ba})} - 1}{er = (1 + Rpa) / (1 + Rba) - 1}
+#' 
+#' @param Rp an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' portfolio returns
+#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of 
+#' benchmark returns
+#' @param scale number of periods in a year (daily scale = 252, monthly scale =
+#' 12, quarterly scale = 4)
+#' @param geometric generate geometric (TRUE) or simple (FALSE) excess returns,
+#' default TRUE
+#' @author Andrii Babii
+#' @seealso \code{\link{Return.annualized}},
+#' @references Bacon, Carl. \emph{Practical Portfolio Performance Measurement
+#' and Attribution}. Wiley. 2004. p. 206-207
+#' @keywords ts multivariate distribution models
+#' @examples
+#' data(managers)
+#' Return.annualized.excess(Ra = managers[,1], Rb = managers[,8])
+#' 
+#' @export
+Return.annualized.excess <- 
+function (Rp, Rb, scale = NA, geometric = TRUE )
+{ # @author Andrii Babii
+    Rp = checkData(Rp)
+    Rb = checkData(Rb)
+    
+    Rp = na.omit(Rp)
+    Rb = na.omit(Rb)
+    n = nrow(Rp)
+    if(is.na(scale)) {
+      freq = periodicity(Rp)
+      switch(freq$scale,
+             minute = {stop("Data periodicity too high")},
+             hourly = {stop("Data periodicity too high")},
+             daily = {scale = 252},
+             eekly = {scale = 52},
+             monthly = {scale = 12},
+             quarterly = {scale = 4},
+             yearly = {scale = 1}
+             )
+    }
+    Rpa = apply(1 + Rp, 2, prod)^(scale/n) - 1
+    Rba = apply(1 + Rb, 2, prod)^(scale/n) - 1
+    if (geometric) {
+      # geometric excess returns
+      result = (1 + Rpa) / (1 + Rba) - 1
+    } else {
+      # arithmetic excess returns
+      result = Rpa - Rba
+    }
+    dim(result) = c(1,NCOL(Rp))
+    colnames(result) = colnames(Rp)
+    rownames(result) = "Annualized Return"
+    return(result)
 }
\ No newline at end of file



More information about the Returnanalytics-commits mailing list