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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 19 21:12:28 CEST 2012


Author: matthieu_lestel
Date: 2012-07-19 21:12:28 +0200 (Thu, 19 Jul 2012)
New Revision: 2184

Added:
   pkg/PerformanceAnalytics/R/MSquared.R
   pkg/PerformanceAnalytics/man/MSquared.Rd
Modified:
   pkg/PerformanceAnalytics/R/DownsideDeviation.R
   pkg/PerformanceAnalytics/R/SystematicRisk.R
   pkg/PerformanceAnalytics/man/DownsideDeviation.Rd
   pkg/PerformanceAnalytics/man/SystematicRisk.Rd
Log:
better with the addition of the file

Modified: pkg/PerformanceAnalytics/R/DownsideDeviation.R
===================================================================
--- pkg/PerformanceAnalytics/R/DownsideDeviation.R	2012-07-19 18:05:46 UTC (rev 2183)
+++ pkg/PerformanceAnalytics/R/DownsideDeviation.R	2012-07-19 19:12:28 UTC (rev 2184)
@@ -16,9 +16,7 @@
 #' returns to get a below-target semi-variance.
 #' 
 #'
-#' \deqn{ DownsideDeviation(R , MAR)= \delta_{MAR} = \sqrt{\sum^{n}_{t=1}\frac{
-#' min[(R_{t} - MAR), 0]^2}{n}} {DownsideDeviation(R, MAR) = sqrt(1/n * sum(t=1..n)
-#' ((min(R(t)-MAR, 0))^2))}}
+#' \deqn{ DownsideDeviation(R , MAR)= \delta_{MAR} = \sqrt{\sum^{n}_{t=1}\frac{min[(R_{t} - MAR), 0]^2}{n}}{DownsideDeviation(R, MAR) = sqrt(1/n * sum(t=1..n)((min(R(t)-MAR, 0))^2))}}
 #'
 #' \deqn{ DownsideVariance(R, MAR) = \sum^{n}_{t=1}\frac{min[(R_{t} - MAR), 0]^2} {n}}
 #' {DownsideVariance(R, MAR) = 1/n * sum(t=1..n)((min(R(t)-MAR, 0))^2)}

Added: pkg/PerformanceAnalytics/R/MSquared.R
===================================================================
--- pkg/PerformanceAnalytics/R/MSquared.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/MSquared.R	2012-07-19 19:12:28 UTC (rev 2184)
@@ -0,0 +1,67 @@
+#' M squared of the return distribution
+#'
+#' M squared is a risk adjusted return useful to judge the size of relative
+#' performance between differents portfolios. With it you can compare portfolios
+#' with different levels of risk.
+#'
+#' \deqn{M^2 = r_P + SR * (\sigma_M - \sigma_P) = (r_P - r_F) * \frac{\sigma_M}{\sigma_P} + r_F}
+#' {M squared = Rp + SR * (Market risk - Portfolio risk) = (Rp - Rf) * Market risk / Portfolio risk + Rf}
+#'
+#' where \eqn{r_P} is the portfolio return annualized, \eqn{\sigma_M} is the market
+#' risk and \eqn{\sigma_P} is the portfolio risk
+#'
+#' @aliases MSquared
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset return
+#' @param Rb return vector of the benchmark asset 
+#' @param Rf risk free rate, in same period as your returns
+#' @param Period the number of return in a year in the asset return 
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.67-68
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' data(portfolio_bacon)
+#' print(MSquared(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 0.1068
+#'
+#' data(managers)
+#' print(MSquared(managers['1996',1], managers['1996',8]))
+#' print(MSquared(managers['1996',1:5], managers['1996',8]))
+#'
+#' @export 
+MSquared <-
+function (Ra, Rb, Rf = 0, Period = 12, ...)
+{
+    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) {
+        Rp = Return.annualized(Ra)
+     	sigp = sqrt(var(Ra)*(length(Ra)-1)/length(Ra))*sqrt(Period)
+     	sigm = sqrt(var(Rb)*(length(Rb)-1)/length(Rb))*sqrt(Period)
+        result = (Rp - Rf) * sigp / sigm + Rf
+     }    
+     else {
+        result = NA
+     }
+      return(result)
+    }
+    else {
+        result = apply(Ra, MARGIN = 2, MSquared, Rb = Rb, Rf = Rf, Period = Period, ...)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("MSquared (Risk free = ",Rf,")", sep="")
+        return(result)
+    }
+}

Modified: pkg/PerformanceAnalytics/R/SystematicRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-19 18:05:46 UTC (rev 2183)
+++ pkg/PerformanceAnalytics/R/SystematicRisk.R	2012-07-19 19:12:28 UTC (rev 2184)
@@ -11,10 +11,10 @@
 #' where \eqn{\sigma_s} is the systematic risk, \eqn{\beta} is the regression beta,
 #' and \eqn{\sigma_m} is the market risk
 #'
-#' @aliases SystematicRrisk
+#' @aliases SystematicRisk
 #' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
 #' asset returns
-#' @param Rb return vector of the benchmark asset
+#' @param Rb return vector of the benchmark asset 
 #' @param Rf risk free rate, in same period as your returns
 #' @param Period the number of return in a year in the asset return 
 #' @param \dots any other passthru parameters
@@ -49,6 +49,8 @@
       }
 
      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 {

Modified: pkg/PerformanceAnalytics/man/DownsideDeviation.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/DownsideDeviation.Rd	2012-07-19 18:05:46 UTC (rev 2183)
+++ pkg/PerformanceAnalytics/man/DownsideDeviation.Rd	2012-07-19 19:12:28 UTC (rev 2184)
@@ -75,9 +75,9 @@
   number of returns to get a below-target semi-variance.
 
   \deqn{ DownsideDeviation(R , MAR)= \delta_{MAR} =
-  \sqrt{\sum^{n}_{t=1}\frac{ min[(R_{t} - MAR), 0]^2}{n}}
-  {DownsideDeviation(R, MAR) = sqrt(1/n * sum(t=1..n)
-  ((min(R(t)-MAR, 0))^2))}}
+  \sqrt{\sum^{n}_{t=1}\frac{min[(R_{t} - MAR),
+  0]^2}{n}}{DownsideDeviation(R, MAR) = sqrt(1/n *
+  sum(t=1..n)((min(R(t)-MAR, 0))^2))}}
 
   \deqn{ DownsideVariance(R, MAR) =
   \sum^{n}_{t=1}\frac{min[(R_{t} - MAR), 0]^2} {n}}

Added: pkg/PerformanceAnalytics/man/MSquared.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/MSquared.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/MSquared.Rd	2012-07-19 19:12:28 UTC (rev 2184)
@@ -0,0 +1,55 @@
+\name{MSquared}
+\alias{MSquared}
+\title{M squared of the return distribution}
+\usage{
+  MSquared(Ra, Rb, Rf = 0, Period = 12, ...)
+}
+\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{Rf}{risk free rate, in same period as your returns}
+
+  \item{Period}{the number of return in a year in the asset
+  return}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  M squared is a risk adjusted return useful to judge the
+  size of relative performance between differents
+  portfolios. With it you can compare portfolios with
+  different levels of risk.
+}
+\details{
+  \deqn{M^2 = r_P + SR * (\sigma_M - \sigma_P) = (r_P -
+  r_F) * \frac{\sigma_M}{\sigma_P} + r_F} {M squared = Rp +
+  SR * (Market risk - Portfolio risk) = (Rp - Rf) * Market
+  risk / Portfolio risk + Rf}
+
+  where \eqn{r_P} is the portfolio return annualized,
+  \eqn{\sigma_M} is the market risk and \eqn{\sigma_P} is
+  the portfolio risk
+}
+\examples{
+data(portfolio_bacon)
+print(MSquared(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 0.1068
+
+data(managers)
+print(MSquared(managers['1996',1], managers['1996',8]))
+print(MSquared(managers['1996',1:5], managers['1996',8]))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.67-68
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+

Modified: pkg/PerformanceAnalytics/man/SystematicRisk.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/SystematicRisk.Rd	2012-07-19 18:05:46 UTC (rev 2183)
+++ pkg/PerformanceAnalytics/man/SystematicRisk.Rd	2012-07-19 19:12:28 UTC (rev 2184)
@@ -1,6 +1,5 @@
 \name{SystematicRisk}
 \alias{SystematicRisk}
-\alias{SystematicRrisk}
 \title{Systematic risk of the return distribution}
 \usage{
   SystematicRisk(Ra, Rb, Rf = 0, Period = 12, ...)



More information about the Returnanalytics-commits mailing list