[Returnanalytics-commits] r2164 - pkg/PortfolioAnalytics/sandbox/attribution/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 16 13:30:36 CEST 2012


Author: ababii
Date: 2012-07-16 13:30:36 +0200 (Mon, 16 Jul 2012)
New Revision: 2164

Added:
   pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R
   pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R
Log:
- market-timing metrics

Added: pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R	2012-07-16 11:30:36 UTC (rev 2164)
@@ -0,0 +1,69 @@
+#' Merton-Henriksson market timing model
+#' 
+#' Test of market-timing skill (the ability to profitably move from one asset
+#' class to another) developed by Henriksson and Merton. The basic idea of the
+#' test is to perform a multiple regression in which the dependent variable 
+#' (portfolio excess return and a second variable that mimics the payoff to an 
+#' option). This second variable is zero when the market excess return is at or
+#' below zero and is 1 when it is above zero:
+#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b}-R_{f})+\gamma D+\varepsilon_{p}}{Rp - 
+#' Rf = alpha + beta * (Rb - Rf) + gamma * D + epsilonp}
+#' where all variables are familiar from the CAPM model, except for up-market 
+#' return \eqn{D=max(0,R_{b}-R_{f}){D = max(0, Rb - Rf)} and market timing 
+#' abilities \eqn{\gamma}{gamma}
+#' 
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' the asset returns
+#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of 
+#' the benchmark asset return
+#' @param Rf risk free rate, in same period as your returns
+#' @param \dots any other passthrough parameters
+#' @author Andrii Babii
+#' @seealso \code{\link{CAPM.beta}}, \code{\link{TreynorMazuy}}
+#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio 
+#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill, p. 127-129.
+#' \cr Roy D. Henriksson and Robert C. Merton, "On Market Timing and Investment
+#' Performance. II. Statistical Procedures for Evaluating Forecast Skills," 
+#' \emph{Journal of Business}, vol.54, October 1981, pp.513-533 \cr
+#' @examples
+#' 
+#' data(managers)
+#' MertonHenriksson(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12)
+#' MertonHenriksson(managers[80:120,1:6], managers[80:120,7,drop=FALSE], managers[80:120,10,drop=FALSE])
+#' MertonHenriksson(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE])
+#' 
+#' @export
+MertonHenriksson <- function (Ra, Rb, Rf = 0, ...)
+{ # @author Andrii Babii
+  
+    # FUNCTION
+  
+    Ra = checkData(Ra)
+    Rb = checkData(Rb)
+    if (!is.null(dim(Rf))) 
+      Rf = checkData(Rf)
+    Ra.ncols = NCOL(Ra)
+    Rb.ncols = NCOL(Rb)
+    pairs = expand.grid(1:Ra.ncols, 1:Rb.ncols)
+    
+    mh <- function (Ra, Rb, Rf)
+    {
+      D = pmax(0, Rb - Rf)
+      y = Ra - Rf
+      X = cbind(rep(1, length(Ra)), Rb - Rf, D)
+      bhat = solve(t(X) %*% X) %*% t(X) %*% y
+      return(bhat[3])
+    }
+    
+    result = apply(pairs, 1, FUN = function(n, Ra, Rb, Rf) 
+      mh(Ra[, n[1]], Rb[, n[2]], Rf), Ra = Ra, Rb = Rb, Rf = Rf)
+    
+    if(length(result) == 1)
+      return(result)
+    else {
+      result = matrix(result, ncol = Ra.ncols, nrow = Rb.ncols, byrow = TRUE)
+      rownames(result) = paste("Gamma:", colnames(Rb))
+      colnames(result) = colnames(Ra)
+      return(result)
+    }
+}
\ No newline at end of file

Added: pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R	2012-07-16 11:30:36 UTC (rev 2164)
@@ -0,0 +1,67 @@
+#' Treynor-Mazuy market timing model
+#' 
+#' The Treynor-Mazuy model is essentially a quadratic extension of the basic
+#' CAPM. It is estimated using a multiple regression. The second term in the
+#' regression is the value of excess return squared. If the gamma coefficient
+#' in the regression is positive, then the estimated equation describes a 
+#' convex upward-sloping regression "line". The quadratic regression is:
+#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b} - R_{f})+\gamma (R_{b}-R_{f})^2+
+#' \varepsilon_{p}}{Rp - Rf = alpha + beta(Rb -Rf) + gamma(Rb - Rf)^2 + 
+#' epsilonp}
+#' \eqn{\gamma}{gamma} is a measure of the curvature of the regression line.
+#' If \eqn{\gamma}{gamma} is positive, this would indicate that the manager's
+#' investment strategy demonstrates market timing ability.
+#' 
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' the asset returns
+#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of 
+#' the benchmark asset return
+#' @param Rf risk free rate, in same period as your returns
+#' @param \dots any other passthrough parameters
+#' @author Andrii Babii
+#' @seealso \code{\link{CAPM.beta}}, \code{\link{MertonHendriksson}}
+#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio 
+#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill, p. 129-133.
+#' \cr J. L. Treynor and K. Mazuy, "Can Mutual Funds Outguess the Market?" 
+#' \emph{Harvard Business Review}, vol44, 1966, pp. 131-136 \cr
+#' @examples
+#' 
+#' data(managers)
+#' TreynorMazuy(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12)
+#' TreynorMazuy(managers[80:120,1:6], managers[80:120,7,drop=FALSE], managers[80:120,10,drop=FALSE])
+#' TreynorMazuy(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE])
+#'
+#' @export
+TreynorMazuy <- function (Ra, Rb, Rf = 0, ...)
+{ # @author Andrii Babii
+  
+    # FUNCTION
+  
+    Ra = checkData(Ra)
+    Rb = checkData(Rb)
+    if (!is.null(dim(Rf))) 
+      Rf = checkData(Rf)
+    Ra.ncols = NCOL(Ra)
+    Rb.ncols = NCOL(Rb)
+    pairs = expand.grid(1:Ra.ncols, 1:Rb.ncols)
+    
+    tm <- function (Ra, Rb, Rf)
+    {
+      y = Ra - Rf
+      X = cbind(rep(1, length(Ra)), Rb - Rf, (Rb - Rf)^2)
+      bhat = solve(t(X) %*% X) %*% t(X) %*% y
+      return(bhat[3])
+    }
+    
+    result = apply(pairs, 1, FUN = function(n, Ra, Rb, Rf) 
+      tm(Ra[, n[1]], Rb[, n[2]], Rf), Ra = Ra, Rb = Rb, Rf = Rf)
+    
+    if(length(result) == 1)
+      return(result)
+    else {
+      result = matrix(result, ncol = Ra.ncols, nrow = Rb.ncols, byrow = TRUE)
+      rownames(result) = paste("Gamma:", colnames(Rb))
+      colnames(result) = colnames(Ra)
+      return(result)
+    }
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list