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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 11 20:55:49 CEST 2012


Author: matthieu_lestel
Date: 2012-06-11 20:55:48 +0200 (Mon, 11 Jun 2012)
New Revision: 2005

Added:
   pkg/PerformanceAnalytics/R/UpsideRisk.R
   pkg/PerformanceAnalytics/man/UpsideRisk.Rd
Modified:
   pkg/PerformanceAnalytics/man/DownsideDeviation.Rd
Log:
Upside Risk, Variance and Potential with their documentation documentation

Added: pkg/PerformanceAnalytics/R/UpsideRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/UpsideRisk.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/UpsideRisk.R	2012-06-11 18:55:48 UTC (rev 2005)
@@ -0,0 +1,111 @@
+#' upside risk, variance and potential of the return distribution
+#'
+#' Upside Risk is the similar of semideviation taking the return above the
+#' Minimum Acceptable Return instead of using the mean return or zero.
+
+#' To calculate it, we take the subset of returns that are more than the target
+#' (or Minimum Acceptable Returns (MAR)) returns and take the differences of
+#' those to the target.  We sum the squares and divide by the total number of
+#' returns and return the square root.
+#'
+#' \deqn{ UpsideRisk(R , MAR) = \sqrt{\sum^{n}_{t=1}\frac{
+#' max[(R_{t} - MAR), 0]^2}{n}}} {UpsideRisk(R, MAR) = sqrt(1/n * sum(t=1..n)
+#' ((max(R(t)-MAR, 0))^2))}
+#'
+#' \deqn{ UpsideVariance(R, MAR) = \sum^{n}_{t=1}\frac{max[(R_{t} - MAR), 0]^2} {n}} 
+#' {UpsideVariance(R, MAR) = 1/n * sum(t=1..n)((max(R(t)-MAR, 0))^2)}
+#'
+#' \deqn{UpsidePotential(R, MAR) = \sum^{n}_{t=1}\frac{max[(R_{t} - MAR), 0]} {n}} 
+#' {DownsidePotential(R, MAR) =  1/n * sum(t=1..n)(max(R(t)-MAR, 0))}
+#'
+#' where \eqn{n} is either the number of observations of the entire series or
+#' the number of observations in the subset of the series falling below the
+#' MAR.
+#'
+#' @aliases UpsideRisk
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param MAR Minimum Acceptable Return, in the same periodicity as your
+#' returns
+#' @param method one of "full" or "subset", indicating whether to use the
+#' length of the full series or the length of the subset of the series below
+#' the MAR as the denominator, defaults to "subset"
+#' @param method one of "risk", "variance" or "potential" indicating whether
+#' to return the Upside risk, variance or potential
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' portfolio_return <- c(0.3,2.6,1.1,-1.0,1.5,2.5,1.6,6.7,-1.4,4.0,-0.5,8.1,4.0
+#' ,-3.7,-6.1,1.7,-4.9,-2.2,7.0,5.8,-6.5,2.4,-0.5,-0.9)
+#' MAR = 0.5
+#' print(UpsideRisk(portfolio_return, MAR, stat="risk")) #expected 2.937
+#' print(UpsideRisk(portfolio_return, MAR, stat="variance")) #expected 8.628
+#' print(UpsideRisk(portfolio_return, MAR, stat="potential")) #expected 1.771
+#'
+#' @export 
+
+#TODO tests for multi columns data
+
+UpsideRisk <-
+function (R, MAR = 0, method=c("full","subset"), stat=c("risk","variance","potential"), ...)
+{
+    method = method[1]
+    stat = stat[1]
+
+    R0 <- R
+    R = checkData(R, method="matrix")
+
+    if (ncol(R)==1 || is.null(R) || is.vector(R)) {
+       R = na.omit(R)
+       r = subset(R, R > MAR)
+
+        if(!is.null(dim(MAR))){
+            if(is.timeBased(index(MAR))){
+                MAR <-MAR[index(r)] #subset to the same dates as the R data
+            } 
+	    else{
+                MAR = mean(checkData(MAR, method = "vector"))
+                # we have to assume that Ra and a vector of Rf passed in for MAR both cover the same time period
+            }
+        }
+        
+        switch(method,
+            full   = {len = length(R)},
+            subset = {len = length(r)} #previously length(R)
+        ) # end switch
+
+	switch(stat,
+	    risk = {result = sqrt(sum((r - MAR)^2/len))},
+	    variance = {result = sum((r - MAR)^2/len)},
+	    potential = {result = sum((r - MAR)/len)}
+	    )
+	reclass(result, R0)
+        return(result)
+    }
+    else {
+        R = checkData(R)
+        result = apply(R, MARGIN = 2, UpsideRisk, MAR = MAR, method = method, stat = stat, ...)
+        result<-t(result)
+        colnames(result) = colnames(R)
+	print(MAR)
+        rownames(result) = paste("Upside Risk (MAR = ",MAR,"%)", sep="")
+        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: UpsideRisk.R 1989 2012-06-06 18:18:50Z matthieu_lestel $
+#
+###############################################################################

Modified: pkg/PerformanceAnalytics/man/DownsideDeviation.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/DownsideDeviation.Rd	2012-06-11 16:56:19 UTC (rev 2004)
+++ pkg/PerformanceAnalytics/man/DownsideDeviation.Rd	2012-06-11 18:55:48 UTC (rev 2005)
@@ -3,10 +3,16 @@
 \alias{DownsidePotential}
 \alias{SemiDeviation}
 \alias{SemiVariance}
+\alias{UpsideDeviation,}
+\alias{UpsidePotential}
+\alias{UpsideVariance,}
 \title{downside risk (deviation, variance) of the return distribution}
 \usage{
   DownsideDeviation(R, MAR = 0,
     method = c("full", "subset"), ..., potential = FALSE)
+
+  DownsideDeviation(R, MAR = 0,
+    method = c("full", "subset"), ..., potential = FALSE)
 }
 \arguments{
   \item{R}{an xts, vector, matrix, data frame, timeSeries
@@ -24,6 +30,25 @@
 
   \item{potential}{if TRUE, calculate downside potential
   instead, default FALSE}
+
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{MAR}{Minimum Acceptable Return, in the same
+  periodicity as your returns}
+
+  \item{method}{one of "full" or "subset", indicating
+  whether to use the length of the full series or the
+  length of the subset of the series below the MAR as the
+  denominator, defaults to "subset"}
+
+  \item{author}{one of "Bacon", "Sortino", indicating
+  whether to}
+
+  \item{\dots}{any other passthru parameters}
+
+  \item{potential}{if TRUE, calculate downside potential
+  instead, default FALSE}
 }
 \description{
   Downside deviation, semideviation, and semivariance are
@@ -58,7 +83,7 @@
   \deqn{DownsidePotential(R, MAR) =
   \sum^{n}_{t=1}\frac{min[(R_{t} - MAR), 0]} {n}}
   {DownsidePotential(R, MAR) = 1/n *
-  sum(t=1..n)((min(R(t)-MAR, 0)))}
+  sum(t=1..n)(min(R(t)-MAR, 0))}
 
   where \eqn{n} is either the number of observations of the
   entire series or the number of observations in the subset
@@ -117,9 +142,29 @@
 SemiDeviation(managers[,1:6])
 SemiVariance (managers[,1,drop=FALSE])
 SemiVariance (managers[,1:6]) #calculated using method="subset"
+#with data used in Bacon 2008
+
+portfolio_return <- c(0.3,2.6,1.1,-1.0,1.5,2.5,1.6,6.7,-1.4,4.0,-0.5,8.1,4.0,-3.7,
+-6.1,1.7,-4.9,-2.2,7.0,5.8,-6.5,2.4,-0.5,-0.9)
+MAR = 0.5
+DownsideDeviation(portfolio_return, MAR) #expected 2.55
+DownsidePotential(portfolio_return, MAR) #expected 1.37
+
+#with data of managers
+
+data(managers)
+apply(managers[,1:6], 2, sd, na.rm=TRUE)
+DownsideDeviation(managers[,1:6])  # MAR 0\%
+DownsideDeviation(managers[,1:6], MAR = .04/12) #MAR 4\%
+SemiDeviation(managers[,1,drop=FALSE])
+SemiDeviation(managers[,1:6])
+SemiVariance (managers[,1,drop=FALSE])
+SemiVariance (managers[,1:6]) #calculated using method="subset"
 }
 \author{
   Peter Carl, Brian G. Peterson, Matthieu Lestel
+
+  Peter Carl, Brian G. Peterson, Matthieu Lestel
 }
 \references{
   Sortino, F. and Price, L. Performance Measurement in a
@@ -138,6 +183,23 @@
   especially end note 10
 
   \url{http://en.wikipedia.org/wiki/Semivariance}
+
+  Sortino, F. and Price, L. Performance Measurement in a
+  Downside Risk Framework. \emph{Journal of Investing}.
+  Fall 1994, 59-65. \cr Carl Bacon, \emph{Practical
+  portfolio performance measurement and attribution},
+  second edition 2008
+
+  Plantinga, A., van der Meer, R. and Sortino, F. The
+  Impact of Downside Risk on Risk-Adjusted Performance of
+  Mutual Funds in the Euronext Markets. July 19, 2001.
+  Available at SSRN: \url{http://ssrn.com/abstract=277352}
+  \cr
+
+  \url{http://www.sortino.com/htm/performance.htm} see
+  especially end note 10
+
+  \url{http://en.wikipedia.org/wiki/Semivariance}
 }
 \keyword{distribution}
 \keyword{models}

Added: pkg/PerformanceAnalytics/man/UpsideRisk.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/UpsideRisk.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/UpsideRisk.Rd	2012-06-11 18:55:48 UTC (rev 2005)
@@ -0,0 +1,70 @@
+\name{UpsideRisk}
+\alias{UpsideRisk}
+\title{upside risk, variance and potential of the return distribution}
+\usage{
+  UpsideRisk(R, MAR = 0, method = c("full", "subset"),
+    stat = c("risk", "variance", "potential"), ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{MAR}{Minimum Acceptable Return, in the same
+  periodicity as your returns}
+
+  \item{method}{one of "full" or "subset", indicating
+  whether to use the length of the full series or the
+  length of the subset of the series below the MAR as the
+  denominator, defaults to "subset"}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  Upside Risk is the similar of semideviation taking the
+  return above the Minimum Acceptable Return instead of
+  using the mean return or zero. To calculate it, we take
+  the subset of returns that are more than the target (or
+  Minimum Acceptable Returns (MAR)) returns and take the
+  differences of those to the target.  We sum the squares
+  and divide by the total number of returns and return the
+  square root.
+}
+\details{
+  \deqn{ UpsideRisk(R , MAR) = \sqrt{\sum^{n}_{t=1}\frac{
+  max[(R_{t} - MAR), 0]^2}{n}}} {UpsideRisk(R, MAR) =
+  sqrt(1/n * sum(t=1..n) ((max(R(t)-MAR, 0))^2))}
+
+  \deqn{ UpsideVariance(R, MAR) =
+  \sum^{n}_{t=1}\frac{max[(R_{t} - MAR), 0]^2} {n}}
+  {UpsideVariance(R, MAR) = 1/n *
+  sum(t=1..n)((max(R(t)-MAR, 0))^2)}
+
+  \deqn{UpsidePotential(R, MAR) =
+  \sum^{n}_{t=1}\frac{max[(R_{t} - MAR), 0]} {n}}
+  {DownsidePotential(R, MAR) = 1/n *
+  sum(t=1..n)(max(R(t)-MAR, 0))}
+
+  where \eqn{n} is either the number of observations of the
+  entire series or the number of observations in the subset
+  of the series falling below the MAR.
+}
+\examples{
+portfolio_return <- c(0.3,2.6,1.1,-1.0,1.5,2.5,1.6,6.7,-1.4,4.0,-0.5,8.1,4.0
+,-3.7,-6.1,1.7,-4.9,-2.2,7.0,5.8,-6.5,2.4,-0.5,-0.9)
+MAR = 0.5
+print(UpsideRisk(portfolio_return, MAR, stat="risk")) #expected 2.937
+print(UpsideRisk(portfolio_return, MAR, stat="variance")) #expected 8.628
+print(UpsideRisk(portfolio_return, MAR, stat="potential")) #expected 1.771
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list