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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 16 18:49:54 CEST 2012


Author: ababii
Date: 2012-07-16 18:49:54 +0200 (Mon, 16 Jul 2012)
New Revision: 2168

Added:
   pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R
Log:
- dynamic conditional alphas and betas

Added: pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R	2012-07-16 16:49:54 UTC (rev 2168)
@@ -0,0 +1,89 @@
+#' Time-varying conditional beta
+#' 
+#' CAPM is estimated assuming that betas and alphas change over time. It is 
+#' assumed that the market prices of securities fully reflect readily available
+#' and public information. A matrix of market information variables, \eqn{Z}
+#' measures this information. Possible variables in \eqn{Z} could be the
+#' divident yield, Tresaury yield, etc. The betas of stocks and managed
+#' portfolios are allowed to change with market conditions:
+#' \deqn{\beta_{p}(z_{t})=b_{0p}+B_{p}'z_{t}}{beta(zt) = b0 + Bp'zt}
+#' where \eqn{z_{t}=Z_{t}-E[Z]}{zt = Zt - E[Z]} - a normalized vector of the 
+#' deviations of \eqn{Z_{t}}{Zt}, \eqn{B_{p}}{Bp} - a vector with the same 
+#' dimension as \eqn{Z_{t}}{Zt}. The coefficient \eqn{b_{0p}}{b0} can be 
+#' interpreted as the "average beta" or the beta when all infromation variables
+#' are at their means. The elements of \eqn{B_{p}}{Bp} measure the sensitivity 
+#' of the conditional beta to the deviations of the \eqn{Z_{t}}{Zt} from their
+#' means. 
+#' In the similar way the time-varying conditional alpha is modeled:
+#' \deqn{\alpha_{pt}=\alpha_{p}(z_{t})=\alpha_{0p}+A_{p}'z_{t}}{alpha(zt) = 
+#' a0 + Ap'zt}
+#' The modified regression is therefore:
+#' \deqn{r_{pt+1}=\alpha_{0p}+A_{p}'z_{t}+b_{0p}r_{bt+1}+B_{p}'[z_{t}r_{bt+1}]+
+#' \mu_{pt+1}}
+#' 
+#' @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. On the contrary to other similar functions it
+#' works only with a single benchmark (of dimenstion T x 1)
+#' @param Rf risk free rate, in same period as your returns
+#' @param Z an xts, vector, matrix, data frame, timeSeries or zoo object of 
+#' k variables that reflect public information
+#' @param lags number of lags before the current period on which the alpha and
+#' beta are conditioned
+#' @param \dots any other passthrough parameters
+#' @author Andrii Babii
+#' @seealso \code{\link{CAPM.beta}}
+#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio 
+#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill. Chapter 12. 
+#' \cr Wayne E. Ferson and Rudi Schadt, "Measuring Fund Strategy and 
+#' Performance in Changing Economic Conditions," \emph{Journal of Finance}, 
+#' vol. 51, 1996, pp.425-462 \cr
+#' @examples
+#' 
+#' data(managers)
+#' CAPM.dynamic(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12, Z=managers[, 9:10], lags = 2)
+#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,7,drop=FALSE], Rf=managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10], lags = 1)
+#' 
+#' @export
+CAPM.dynamic <- function (Ra, Rb, Rf = 0, Z, lags = 1, ...)
+{ # @author Andrii Babii
+    
+    # FUNCTION
+  
+    Ra = checkData(Ra)
+    Rb = checkData(Rb)
+    Z = checkData(Z)
+    if (!is.null(dim(Rf))) 
+      Rf = checkData(Rf)
+    Ra.ncols = NCOL(Ra)
+    Rb.ncols = NCOL(Rb)
+    pairs = expand.grid(1:Ra.ncols)
+    
+    xRa = Return.excess(Ra, Rf)[1:(nrow(Ra) - 1)]
+    xRb = Return.excess(Rb, Rf)[1:(nrow(Rb) - 1)]
+    z = Z - matrix(rep(mean(Z), nrow(Z)), nrow(Z), ncol(Z), byrow = TRUE)
+    # Construct the matrix with information regressors (lagged values)
+    inform = lag(z)
+    if (lags > 1){
+      for (i in 2:lags) {
+        inform = cbind(inform, lag(z, i))
+      }
+    }
+    z = inform[(lags + 1):nrow(z), ]
+        
+    dynamic <- function (xRa, xRb, z){
+      y = xRa[1:nrow(z)]
+      X = cbind(rep(1, length(index(z))), z, coredata(xRb[1:nrow(z)]), z * matrix(rep(xRb[1:nrow(z)], ncol(z)), nrow(z), ncol(z)))
+      bhat = solve(t(X) %*% X) %*% t(X) %*% y
+      return(bhat)
+    }
+    result = apply(pairs, 1, FUN = function(n, xRa, xRb, z) 
+      dynamic(xRa[, n[1]], xRb, z), xRa = xRa, xRb = xRb, z = z)
+    
+    a = paste(rep(colnames(Z), lags), "alpha at t -", expand.grid(1:ncol(Z), 1:lags)[, 2])
+    b = paste(rep(colnames(Z), lags), "beta at t -", expand.grid(1:ncol(Z), 1:lags)[, 2])
+    rownames(result) = c("Average alpha", a, "Average beta", b)
+    colnames(result) = colnames(Ra)
+    return(result)
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list