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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 14 17:19:37 CEST 2012


Author: matthieu_lestel
Date: 2012-07-14 17:19:37 +0200 (Sat, 14 Jul 2012)
New Revision: 2158

Added:
   pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R
   pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd
Log:
Mean absolute deviation with examples and documentation

Added: pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R
===================================================================
--- pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R	2012-07-14 15:19:37 UTC (rev 2158)
@@ -0,0 +1,49 @@
+#' Mean absolute deviation of the return distribution
+#'
+#' 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 }
+#'
+#' 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
+#' 
+#' @aliases MeanAbsoluteDeviation
+#' @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.62
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#' data(portfolio_bacon[,1])
+#' print(MeanAbsoluteDeviation(portfolio_bacon)) #expected 1.78
+#'
+#' data(managers)
+#' print(MeanAbsoluteDeviation(managers['1996']))
+#' print(MeanAbsoluteDeviation(managers['1996',1])) #expected 4.598
+#'
+#' @export 
+
+MeanAbsoluteDeviation <- function (R, ...)
+{
+    R0 <- R
+    R = checkData(R, method="matrix")
+
+    if (ncol(R)==1 || is.null(R) || is.vector(R)) {
+       R = na.omit(R)
+       result = sum(abs(R - mean(R)))/length(R)
+       reclass(result, R0)
+       return(result)
+    }  
+    else {
+        R = checkData(R)
+        result = apply(R, MARGIN = 2, MeanAbsoluteDeviation, ...)
+        result<-t(result)
+        colnames(result) = colnames(R)
+        rownames(result) = paste("Mean absolute deviation", sep="")
+        return(result)
+    }
+}
\ No newline at end of file

Added: pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd	2012-07-14 15:19:37 UTC (rev 2158)
@@ -0,0 +1,47 @@
+\name{MeanAbsoluteDeviation}
+\alias{MeanAbsoluteDeviation}
+\title{Mean absolute deviation of the return distribution}
+\usage{
+  MeanAbsoluteDeviation(R, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  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.
+}
+\details{
+  \deqn{MeanAbsoluteDeviation = \frac{\sum^{n}_{i=1}\mid
+  r_i - \overline{r}\mid}{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
+}
+\examples{
+data(portfolio_bacon[,1])
+print(MeanAbsoluteDeviation(portfolio_bacon)) #expected 1.78
+
+data(managers)
+print(MeanAbsoluteDeviation(managers['1996']))
+print(MeanAbsoluteDeviation(managers['1996',1])) #expected 4.598
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.62
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list