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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 15 14:58:31 CEST 2012


Author: matthieu_lestel
Date: 2012-07-15 14:58:31 +0200 (Sun, 15 Jul 2012)
New Revision: 2160

Added:
   pkg/PerformanceAnalytics/R/DrawdownPeak.R
   pkg/PerformanceAnalytics/man/PainIndex.Rd
Modified:
   pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R
   pkg/PerformanceAnalytics/R/PainIndex.R
   pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd
Log:
documentation of PainIndex, correction of PainIndex.R, addition of DrawdownPeak to calculate drawdown since previous peak

Added: pkg/PerformanceAnalytics/R/DrawdownPeak.R
===================================================================
--- pkg/PerformanceAnalytics/R/DrawdownPeak.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/DrawdownPeak.R	2012-07-15 12:58:31 UTC (rev 2160)
@@ -0,0 +1,48 @@
+DrawdownPeak <- function (R, ...)
+{
+   R0 <- R
+   R = checkData(R, method="matrix")
+
+    if (ncol(R)==1 || is.null(R) || is.vector(R)) {
+	calcul = FALSE
+        for (i in (1:length(R))) {
+     	     if (!is.na(R[i])) {
+     	    	calcul = TRUE
+	     }
+        }		      
+        if (!calcul) {
+	result = NaN
+	}
+	else {
+        R = na.omit(R)
+	drawdownpeak = c()
+	length(drawdownpeak) = length(R)
+	peak = 0
+	for(i in (1:length(R))) {
+	      val = 1
+	      borne = peak+1
+	      for(j in (borne:i)) {
+	      	    val = val*(1+R[j]/100)
+	      }
+	      if (val > 1) {
+	      	 peak = i
+		 drawdownpeak[i] = 0
+	      }
+	      else {
+	      	   drawdownpeak[i] = (val-1)*100
+	      }
+	}
+	result = drawdownpeak
+	}
+       	reclass(result, R0)	
+	return(result)
+}
+    else {
+        R = checkData(R)
+        result = apply(R, MARGIN = 2, DrawdownPeak, ...)
+        result<-t(result)
+        colnames(result) = colnames(R)
+        rownames(result) = paste("DrawdownPeak", sep="")
+        return(result)
+    }
+}
\ No newline at end of file

Modified: pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R
===================================================================
--- pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R	2012-07-14 21:20:50 UTC (rev 2159)
+++ pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R	2012-07-15 12:58:31 UTC (rev 2160)
@@ -3,7 +3,7 @@
 #' To calculate Mean absolute deviation we take the sum of the absolute value of the difference between the returns and the mean of the returns and we divide it by the number of returns.
 #'
 #' \deqn{MeanAbsoluteDeviation = \frac{\sum^{n}_{i=1}\mid r_i - \overline{r}\mid}{n}}
-#' {MeanAbsoluteDeviation = sum(r-mean(r))/n }
+#' {MeanAbsoluteDeviation = sum(|r-mean(r)|)/n }
 #'
 #' where \eqn{n} is the number of observations of the entire series, \eqn{r_i} is the
 #' return in month i and \eqn{\overline{r}} is the mean return
@@ -23,7 +23,7 @@
 #'
 #' data(managers)
 #' print(MeanAbsoluteDeviation(managers['1996']))
-#' print(MeanAbsoluteDeviation(managers['1996',1])) #expected 4.598
+#' print(MeanAbsoluteDeviation(managers['1996',1]))
 #'
 #' @export 
 

Modified: pkg/PerformanceAnalytics/R/PainIndex.R
===================================================================
--- pkg/PerformanceAnalytics/R/PainIndex.R	2012-07-14 21:20:50 UTC (rev 2159)
+++ pkg/PerformanceAnalytics/R/PainIndex.R	2012-07-15 12:58:31 UTC (rev 2160)
@@ -1,17 +1,46 @@
-PainIndex <-
-function (R, ...) {
+#' Pain index of the return distribution
+#'
+#' The pain index is the mean value of the drawdowns over the entire 
+#' analysis period. The measure is similar to the Ulcer index except that 
+#' the drawdowns are not squared.  Also, it's different than the average
+#' drawdown, in that the numerator is the total number of observations 
+#' rather than the number of drawdowns.
+#' 
+#' Visually, the pain index is the area of the region that is enclosed by 
+#' the horizontal line at zero percent and the drawdown line in the 
+#' Drawdown chart.
+#'
+#' \deqn{Pain index = \sum^{n}_{i=1} \frac{\mid D'_i \mid}{n}}
+#' {Pain index = sum(|D'i|/n)}
+#'
+#' where \eqn{n} is the number of observations of the entire series, \eqn{D'_i} is
+#' the drawdown since previous peak in period i
+#'
+#' @aliases PainIndex
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.89, Becker, Thomas (2006) Zephyr Associates 
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#' data(portfolio_bacon[,1])
+#' print(PainIndex(portfolio_bacon)) #expected 4.0
+#'
+#' data(managers)
+#' print(PainIndex(100*managers['1996']))
+#' print(PainIndex(100*managers['1996',1])) 
+#'
+#' @export 
+
+
+PainIndex <- function (R, ...) 
+{
     
     # DESCRIPTION:
-    # The pain index is the mean value of the drawdowns over the entire 
-    # analysis period. The measure is similar to the Ulcer index except that 
-    # the drawdowns are not squared.  Also, it's different than the average
-    # drawdown, in that the numerator is the total number of observations 
-    # rather than the number of drawdowns.
     # 
-    # Visually, the pain index is the area of the region that is enclosed by 
-    # the horizontal line at zero percent and the drawdown line in the 
-    # Drawdown chart.
-    # 
     # PI = sum[i=1,2,...,n](abs(D'_i)/n) where
     # D'_i = drawdown since previous peak in period i
     # 
@@ -20,8 +49,9 @@
     R = checkData(R)
 
     pi <- function(R) {
-        R = na.omit(R)
-        result = sqrt(sum(abs(Drawdowns(R)))/length(R))
+        result = sum(abs(DrawdownPeak(R)))
+	R = na.omit(R)
+	result = result / length(R)
         return(result)
     }
 
@@ -30,16 +60,4 @@
     colnames(result) = colnames(R)
     rownames(result) = "Pain Index"
     return (result)
-}
-
-###############################################################################
-# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
-#
-# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
-#
-# This R package is distributed under the terms of the GNU Public License (GPL)
-# for full details see the file COPYING
-#
-# $Id$
-#
-###############################################################################
\ No newline at end of file
+}
\ No newline at end of file

Modified: pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd	2012-07-14 21:20:50 UTC (rev 2159)
+++ pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd	2012-07-15 12:58:31 UTC (rev 2160)
@@ -19,7 +19,7 @@
 \details{
   \deqn{MeanAbsoluteDeviation = \frac{\sum^{n}_{i=1}\mid
   r_i - \overline{r}\mid}{n}} {MeanAbsoluteDeviation =
-  sum(r-mean(r))/n }
+  sum(|r-mean(r)|)/n }
 
   where \eqn{n} is the number of observations of the entire
   series, \eqn{r_i} is the return in month i and
@@ -31,7 +31,7 @@
 
 data(managers)
 print(MeanAbsoluteDeviation(managers['1996']))
-print(MeanAbsoluteDeviation(managers['1996',1])) #expected 4.598
+print(MeanAbsoluteDeviation(managers['1996',1]))
 }
 \author{
   Matthieu Lestel

Added: pkg/PerformanceAnalytics/man/PainIndex.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/PainIndex.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/PainIndex.Rd	2012-07-15 12:58:31 UTC (rev 2160)
@@ -0,0 +1,53 @@
+\name{PainIndex}
+\alias{PainIndex}
+\title{Pain index of the return distribution}
+\usage{
+  PainIndex(R, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  The pain index is the mean value of the drawdowns over
+  the entire analysis period. The measure is similar to the
+  Ulcer index except that the drawdowns are not squared.
+  Also, it's different than the average drawdown, in that
+  the numerator is the total number of observations rather
+  than the number of drawdowns.
+}
+\details{
+  Visually, the pain index is the area of the region that
+  is enclosed by the horizontal line at zero percent and
+  the drawdown line in the Drawdown chart.
+
+  \deqn{Pain index = \sum^{n}_{i=1} \frac{\mid D'_i
+  \mid}{n}} {Pain index = sum(|D'i|/n)}
+
+  where \eqn{n} is the number of observations of the entire
+  series, \eqn{D'_i} is the drawdown since previous peak in
+  period i
+}
+\examples{
+data(portfolio_bacon[,1])
+print(PainIndex(portfolio_bacon)) #expected 4.0
+
+data(managers)
+print(PainIndex(100*managers['1996']))
+print(PainIndex(100*managers['1996',1]))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.89,
+  Becker, Thomas (2006) Zephyr Associates
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list