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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 18 12:49:36 CEST 2012


Author: ababii
Date: 2012-07-18 12:49:36 +0200 (Wed, 18 Jul 2012)
New Revision: 2173

Added:
   pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
Removed:
   pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R
   pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R
Log:
- Replace MartonHenriksson and TreynorMazuy by MarketTiming
- currently returns only gamma, but works with multiple assets and multiple benchmarks

Added: pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R	2012-07-18 10:49:36 UTC (rev 2173)
@@ -0,0 +1,92 @@
+#' Market timing models
+#' 
+#' Allows to estimate Treynor-Mazuy or Merton-Henriksson 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.
+#' 
+#' The basic idea of the Merton-Henriksson 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 the 
+#' 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 method used to select between Treynor-Mazuy and Henriksson-Merton
+#' models. May be any of: \itemize{ \item TM - Treynor-Mazuy model, 
+#' \item HM - Henriksson-Merton model}}
+#' @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. 127-133.
+#' \cr J. L. Treynor and K. Mazuy, "Can Mutual Funds Outguess the Market?" 
+#' \emph{Harvard Business Review}, vol44, 1966, pp. 131-136 
+#' \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)
+#' MarketTiming(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12, method = "HM")
+#' MarketTiming(managers[80:120,1:6], managers[80:120,7,drop=FALSE], managers[80:120,10,drop=FALSE])
+#' MarketTiming(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE], method = "TM")
+#'
+#' @export
+MarketTiming <- function (Ra, Rb, Rf = 0, method = c("TM", "HM"))
+{ # @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)
+    method = method[1]
+    xRa = Return.excess(Ra, Rf)
+    xRb = Return.excess(Rb, Rf)
+
+  
+    mt <- function (xRa, xRb)
+    {
+      switch(method,
+             "HM" = { S = xRb > 0 },
+             "TM" = { S = xRb }
+      )
+      R = merge(xRa, xRb, xRb*S)
+      R.df = as.data.frame(R)
+      model = lm(R.df[, 1] ~ 1 + ., data = R.df[, -1])
+      return(coef(model)[3])
+    }
+  
+    result = apply(pairs, 1, FUN = function(n, xRa, xRb) 
+      mt(xRa[, n[1]], xRb[, n[2]]), xRa = xRa, xRb = xRb)
+  
+    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

Deleted: pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R	2012-07-17 08:46:48 UTC (rev 2172)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/MertonHenriksson.R	2012-07-18 10:49:36 UTC (rev 2173)
@@ -1,72 +0,0 @@
-#' 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 the 
-#' 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)
-    
-    xRa = Return.excess(Ra, Rf)
-    xRb = Return.excess(Rb, Rf)
-    
-    mh <- function (xRa, xRb)
-    {
-      D = pmax(0, xRb)
-      y = xRa
-      X = cbind(rep(1, length(xRa)), xRb, D)
-      bhat = solve(t(X) %*% X) %*% t(X) %*% y
-      return(bhat[3])
-    }
-    
-    result = apply(pairs, 1, FUN = function(n, xRa, xRb) 
-      mh(xRa[, n[1]], xRb[, n[2]]), xRa = xRa, xRb = xRb)
-    
-    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

Deleted: pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R	2012-07-17 08:46:48 UTC (rev 2172)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/TreynorMazuy.R	2012-07-18 10:49:36 UTC (rev 2173)
@@ -1,67 +0,0 @@
-#' 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