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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 25 17:39:11 CEST 2012


Author: matthieu_lestel
Date: 2012-07-25 17:39:11 +0200 (Wed, 25 Jul 2012)
New Revision: 2205

Added:
   pkg/PerformanceAnalytics/R/AppraisalRatio.R
   pkg/PerformanceAnalytics/R/M2Sortino.R
   pkg/PerformanceAnalytics/R/ProspectRatio.R
   pkg/PerformanceAnalytics/man/AppraisalRatio.Rd
   pkg/PerformanceAnalytics/man/BernardoLedoitRatio.Rd
   pkg/PerformanceAnalytics/man/DrawdownPeak.Rd
   pkg/PerformanceAnalytics/man/M2Sortino.Rd
   pkg/PerformanceAnalytics/man/ProspectRatio.Rd
Modified:
   pkg/PerformanceAnalytics/NAMESPACE
   pkg/PerformanceAnalytics/R/BernadoLedoitratio.R
   pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R
   pkg/PerformanceAnalytics/R/SystematicRisk.R
   pkg/PerformanceAnalytics/man/CAPM.jensenAlpha.Rd
Log:
M2Sortino, AppraisalRatio and ProspectRatio with examples and documentation + some doc I forgot to add in the svn commit

Modified: pkg/PerformanceAnalytics/NAMESPACE
===================================================================
--- pkg/PerformanceAnalytics/NAMESPACE	2012-07-25 11:14:43 UTC (rev 2204)
+++ pkg/PerformanceAnalytics/NAMESPACE	2012-07-25 15:39:11 UTC (rev 2205)
@@ -14,6 +14,7 @@
     AdjustedSharpeRatio,
     apply.fromstart,
     apply.rolling,
+    AppraisalRatio,
     BernardoLedoitRatio,
     BetaCoKurtosis,
     BetaCoSkewness,
@@ -54,6 +55,7 @@
     Kappa,
     KellyRatio,
     kurtosis,
+    M2Sortino,
     maxDrawdown,
     MeanAbsoluteDeviation,
     mean.geometric,
@@ -67,6 +69,7 @@
 #    pfolioReturn,
     PainIndex,
     PainRatio,
+    ProspectRatio,
     Return.annualized,
     Return.calculate,
     Return.centered,

Added: pkg/PerformanceAnalytics/R/AppraisalRatio.R
===================================================================
--- pkg/PerformanceAnalytics/R/AppraisalRatio.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/AppraisalRatio.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,64 @@
+#' Appraisal ratio of the return distribution
+#'
+#' Appraisal ratio is the Jensen's alpha adjusted for systemeatic risk. The numerator
+#' is divided by specific risk instead of total risk.
+#'
+#' \deqn{Appraisal ratio = \frac{alpha}{\sigma_{\epsilon}}}{Appraisal ratio = Jensen's alpha / specific risk}
+#'
+#' where \eqn{alpha} is the Jensen's alpha and \eqn{\sigma_{epsilon}} is the specific risk.
+#'
+#' @aliases AppraisalRatio
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param Rb return vector of the benchmark asset
+#' @param Rf risk free rate, in same period as your returns
+#' @param period number of periods in a year monthly scale = 12, quarterly = 4)
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.72
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' data(portfolio_bacon)
+#' print(AppraisalRatio(portfolio_bacon[,1], portfolio_bacon[,2])) #expected -0.0952
+#'
+#' data(managers)
+#' print(AppraisalRatio(managers['1996',1], managers['1996',8]))
+#' print(AppraisalRatio(managers['1996',1:5], managers['1996',8]))
+#'
+#' @export 
+
+AppraisalRatio <-
+function (Ra, Rb, Rf = 0, period = 12, ...)
+{
+    Ra = checkData(Ra, method="matrix")
+    Rb = checkData(Rb, method="matrix")
+
+    if (ncol(Ra)==1 || is.null(Ra) || is.vector(Ra)) {
+     calcul = FALSE   
+     for (i in (1:length(Ra))) {
+     	 if (!is.na(Ra[i])) {
+     	    calcul = TRUE
+	 }
+      }
+
+     if (calcul) {
+        Period = Frequency(Ra)
+        result = CAPM.jensenAlpha(Ra,Rb,Rf,Period)/SystematicRisk(Ra,Rb,Rf,Period)
+     }    
+     else {
+        result = NA
+     }
+      return(result)
+    }
+    else {
+        Ra = checkData(Ra)
+        result = apply(Ra, MARGIN = 2, AppraisalRatio, Rb = Rb, Rf = Rf, ...)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("Appraisal ratio (Risk free = ",Rf,")", sep="")
+        return(result)
+    }
+}

Modified: pkg/PerformanceAnalytics/R/BernadoLedoitratio.R
===================================================================
--- pkg/PerformanceAnalytics/R/BernadoLedoitratio.R	2012-07-25 11:14:43 UTC (rev 2204)
+++ pkg/PerformanceAnalytics/R/BernadoLedoitratio.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -19,7 +19,7 @@
 #' @keywords ts multivariate distribution models
 #' @examples
 #' data(portfolio_bacon)
-#' print(BernardoLedoitRatio(portfolio_bacon)) #expected 1.78
+#' print(BernardoLedoitRatio(portfolio_bacon[,1])) #expected 1.78
 #'
 #' data(managers)
 #' print(BernardoLedoitRatio(managers['1996']))

Modified: pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R
===================================================================
--- pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R	2012-07-25 11:14:43 UTC (rev 2204)
+++ pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -8,7 +8,7 @@
 #' where \eqn{r_f} is the risk free rate, \eqn{\beta_r} is the regression beta,
 #' \eqn{r_p} is the portfolio return and b is the benchmark return
 #'
-#' @aliases Jensen's alpha
+#' @aliases Jensen'sAlpha
 #' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
 #' asset returns
 #' @param Rb return vector of the benchmark asset

Added: pkg/PerformanceAnalytics/R/M2Sortino.R
===================================================================
--- pkg/PerformanceAnalytics/R/M2Sortino.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/M2Sortino.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,69 @@
+#' M squared for Sortino of the return distribution
+#'
+#' M squared for Sortino is a M^2 calculated for Downside risk instead of Total Risk
+#'
+#' \deqn{M^2_S = r_P + Sortino ratio * (\sigma_{DM} - \sigma_D)}{M^2 (Sortino) = Rp + Sortino ratio * (DownsideRiskBenchmark - DownsideRiskPortfolio)}
+#'
+#' where \eqn{M^2_S} is MSquared for Sortino, \eqn{r_P} is the annualised portfolio return,
+#' \eqn{\sigma_{DM}} is the benchmark annualised downside risk and \eqn{D} is the portfolio
+#' annualised downside risk
+#'
+#' @aliases M2Sortino
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset return
+#' @param Rb return vector of the benchmark asset 
+#' @param MAR the minimum acceptable return
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.102-103
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' data(portfolio_bacon)
+#' MAR = 0.005
+#' print(M2Sortino(portfolio_bacon[,1], portfolio_bacon[,2], MAR)) #expected 0.1035
+#'
+#' data(managers)
+#' MAR = 0
+#' print(MSquaredExcess(managers['1996',1], managers['1996',8], MAR))
+#' print(MSquaredExcess(managers['1996',1:5], managers['1996',8], MAR))
+#'
+#' @export 
+M2Sortino <-
+function (Ra, Rb, MAR = 0, ...)
+{
+    Ra = checkData(Ra)
+    Rb = checkData(Rb)
+
+    if (ncol(Ra)==1 || is.null(Ra) || is.vector(Ra)) {
+    calcul = FALSE    
+     for (i in (1:length(Ra))) {
+     	 if (!is.na(Ra[i])) {
+     	    calcul = TRUE
+	 }
+      }
+
+     if (calcul) {
+     	Period = Frequency(Rb)
+	Rp = Return.annualized(Ra)
+	SigmaD = DownsideDeviation(Ra,MAR)*sqrt(Period)
+	SigmaDM = DownsideDeviation(Rb,MAR)*sqrt(Period)
+	SR = SortinoRatio(Ra,MAR)
+
+	result = Rp + SR * (SigmaDM - SigmaD)
+     }    
+     else {
+        result = NA
+     }
+      return(result)
+    }
+    else {
+        result = apply(Ra, MARGIN = 2, M2Sortino, Rb = Rb, MAR = MAR, ...)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("M2Sortino (MAR = ",MAR,")", sep="")
+        return(result)
+    }
+}

Added: pkg/PerformanceAnalytics/R/ProspectRatio.R
===================================================================
--- pkg/PerformanceAnalytics/R/ProspectRatio.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/ProspectRatio.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,64 @@
+#' Prospect ratio of the return distribution
+#'
+#' Prospect ratio is a ratio used to penalise loss since most people feel loss
+#' greater than gain
+#'
+#' \deqn{ProspectRatio(R) = \frac{frac{1}{n}*\sum^{n}_{i=1}(Max(r_i,0)+2.25*Min(r_i,0) - MAR)}{\sigma_D}}{ProspectRatio(R) = (1/n * sum(Max(ri,0) + 2.25 * Min(ri,0)) - MAR) / DownsideRisk}
+#'
+#' where \eqn{n} is the number of observations of the entire series, MAR is the minimum acceptable return and \eqn{\sigma_D} is the downside risk
+#' 
+#' @aliases ProspectRatio
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param MAR the minimum acceptable return
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.100
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#' data(portfolio_bacon)
+#' MAR = 0.05
+#' print(ProspectRatio(portfolio_bacon[,1], MAR)) #expected -0.221
+#'
+#' data(managers)
+#' MAR = 0
+#' print(ProspectRatio(managers['1996'], MAR))
+#' print(ProspectRatio(managers['1996',1], MAR))
+#'
+#' @export 
+
+ProspectRatio <- function (R, MAR, ...)
+{
+    R <- checkData(R)
+    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) {
+	  n = length(R)
+	  SigD = DownsideDeviation(R,MAR)
+          R = na.omit(R)
+          r1 = R[which(R > 0)]
+          r2 = R[which(R < 0)]
+	  result = (sum(r1)+2.25*sum(r2)-MAR)/(SigD*n)
+	}
+	else
+	{
+	 result = NA
+	}
+       return(result)
+    }  
+    else {
+        result = apply(R, MARGIN = 2, ProspectRatio, MAR=MAR, ...)
+        result<-t(result)
+        colnames(result) = colnames(R)
+        rownames(result) = paste("Prospect ratio (MAR = ",MAR,"%)", sep="")
+        return(result)
+    }
+}
\ No newline at end of file

Modified: pkg/PerformanceAnalytics/R/SystematicRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-25 11:14:43 UTC (rev 2204)
+++ pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-25 15:39:11 UTC (rev 2205)
@@ -48,8 +48,6 @@
       }
 
      if (calcul) {
-        print(sqrt(sum((Rb-mean(Rb))^2)/length(Rb)))
-	print(sqrt(var(Rb)*(length(Rb)-1)/length(Rb)))
         result = CAPM.beta(Ra,Rb,Rf) * sqrt(sum((Rb-mean(Rb))^2)/length(Rb))*sqrt(Period)
      }    
      else {

Added: pkg/PerformanceAnalytics/man/AppraisalRatio.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/AppraisalRatio.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/AppraisalRatio.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,52 @@
+\name{AppraisalRatio}
+\alias{AppraisalRatio}
+\title{Appraisal ratio of the return distribution}
+\usage{
+  AppraisalRatio(Ra, Rb, Rf = 0, period = 12, ...)
+}
+\arguments{
+  \item{Ra}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{Rb}{return vector of the benchmark asset}
+
+  \item{Rf}{risk free rate, in same period as your returns}
+
+  \item{period}{number of periods in a year monthly scale =
+  12, quarterly = 4)}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  Appraisal ratio is the Jensen's alpha adjusted for
+  systemeatic risk. The numerator is divided by specific
+  risk instead of total risk.
+}
+\details{
+  \deqn{Appraisal ratio =
+  \frac{alpha}{\sigma_{\epsilon}}}{Appraisal ratio =
+  Jensen's alpha / specific risk}
+
+  where \eqn{alpha} is the Jensen's alpha and
+  \eqn{\sigma_{epsilon}} is the specific risk.
+}
+\examples{
+data(portfolio_bacon)
+print(AppraisalRatio(portfolio_bacon[,1], portfolio_bacon[,2])) #expected -0.0952
+
+data(managers)
+print(AppraisalRatio(managers['1996',1], managers['1996',8]))
+print(AppraisalRatio(managers['1996',1:5], managers['1996',8]))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.72
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+

Added: pkg/PerformanceAnalytics/man/BernardoLedoitRatio.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/BernardoLedoitRatio.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/BernardoLedoitRatio.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,47 @@
+\name{BernardoLedoitRatio}
+\alias{BernardoLedoitRatio}
+\title{Bernardo and Ledoit ratio of the return distribution}
+\usage{
+  BernardoLedoitRatio(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 Bernardo and Ledoit ratio we take the sum of
+  the subset of returns that are above 0 and we divide it
+  by the opposite of the sum of the subset of returns that
+  are below 0
+}
+\details{
+  \deqn{BernardoLedoitRatio(R) =
+  \frac{\frac{1}{n}\sum^{n}_{t=1}{max(R_{t},0)}}{\frac{1}{n}\sum^{n}_{t=1}{max(-R_{t},0)}}}{BernardoLedoitRatio(R)
+  = 1/n*sum(t=1..n)(max(R(t),0)) /
+  1/n*sum(t=1..n)(max(-R(t),0))}
+
+  where \eqn{n} is the number of observations of the entire
+  series
+}
+\examples{
+data(portfolio_bacon)
+print(BernardoLedoitRatio(portfolio_bacon[,1])) #expected 1.78
+
+data(managers)
+print(BernardoLedoitRatio(managers['1996']))
+print(BernardoLedoitRatio(managers['1996',1])) #expected 4.598
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.95
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+

Modified: pkg/PerformanceAnalytics/man/CAPM.jensenAlpha.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/CAPM.jensenAlpha.Rd	2012-07-25 11:14:43 UTC (rev 2204)
+++ pkg/PerformanceAnalytics/man/CAPM.jensenAlpha.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -1,7 +1,6 @@
 \name{CAPM.jensenAlpha}
-\alias{alpha}
 \alias{CAPM.jensenAlpha}
-\alias{Jensen's}
+\alias{Jensen'sAlpha}
 \title{Jensen's alpha of the return distribution}
 \usage{
   CAPM.jensenAlpha(Ra, Rb, Rf = 0, period = 12, ...)

Added: pkg/PerformanceAnalytics/man/DrawdownPeak.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/DrawdownPeak.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/DrawdownPeak.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,24 @@
+\name{DrawdownPeak}
+\alias{DrawdownPeak}
+\title{Drawdawn peak of the return distribution}
+\usage{
+  DrawdownPeak(R, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  Drawdawn peak is for each return its drawdown since the
+  previous peak
+}
+\author{
+  Matthieu Lestel
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+

Added: pkg/PerformanceAnalytics/man/M2Sortino.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/M2Sortino.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/M2Sortino.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,53 @@
+\name{M2Sortino}
+\alias{M2Sortino}
+\title{M squared for Sortino of the return distribution}
+\usage{
+  M2Sortino(Ra, Rb, MAR = 0, ...)
+}
+\arguments{
+  \item{Ra}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset return}
+
+  \item{Rb}{return vector of the benchmark asset}
+
+  \item{MAR}{the minimum acceptable return}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  M squared for Sortino is a M^2 calculated for Downside
+  risk instead of Total Risk
+}
+\details{
+  \deqn{M^2_S = r_P + Sortino ratio * (\sigma_{DM} -
+  \sigma_D)}{M^2 (Sortino) = Rp + Sortino ratio *
+  (DownsideRiskBenchmark - DownsideRiskPortfolio)}
+
+  where \eqn{M^2_S} is MSquared for Sortino, \eqn{r_P} is
+  the annualised portfolio return, \eqn{\sigma_{DM}} is the
+  benchmark annualised downside risk and \eqn{D} is the
+  portfolio annualised downside risk
+}
+\examples{
+data(portfolio_bacon)
+MAR = 0.005
+print(M2Sortino(portfolio_bacon[,1], portfolio_bacon[,2], MAR)) #expected 0.1035
+
+data(managers)
+MAR = 0
+print(MSquaredExcess(managers['1996',1], managers['1996',8], MAR))
+print(MSquaredExcess(managers['1996',1:5], managers['1996',8], MAR))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008
+  p.102-103
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+

Added: pkg/PerformanceAnalytics/man/ProspectRatio.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/ProspectRatio.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/ProspectRatio.Rd	2012-07-25 15:39:11 UTC (rev 2205)
@@ -0,0 +1,50 @@
+\name{ProspectRatio}
+\alias{ProspectRatio}
+\title{Prospect ratio of the return distribution}
+\usage{
+  ProspectRatio(R, MAR, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{MAR}{the minimum acceptable return}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  Prospect ratio is a ratio used to penalise loss since
+  most people feel loss greater than gain
+}
+\details{
+  \deqn{ProspectRatio(R) =
+  \frac{frac{1}{n}*\sum^{n}_{i=1}(Max(r_i,0)+2.25*Min(r_i,0)
+  - MAR)}{\sigma_D}}{ProspectRatio(R) = (1/n *
+  sum(Max(ri,0) + 2.25 * Min(ri,0)) - MAR) / DownsideRisk}
+
+  where \eqn{n} is the number of observations of the entire
+  series, MAR is the minimum acceptable return and
+  \eqn{\sigma_D} is the downside risk
+}
+\examples{
+data(portfolio_bacon)
+MAR = 0.05
+print(ProspectRatio(portfolio_bacon[,1], MAR)) #expected -0.221
+
+data(managers)
+MAR = 0
+print(ProspectRatio(managers['1996'], MAR))
+print(ProspectRatio(managers['1996',1], MAR))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.100
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list