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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 19 17:06:07 CEST 2012


Author: ababii
Date: 2012-07-19 17:06:06 +0200 (Thu, 19 Jul 2012)
New Revision: 2181

Modified:
   pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R
   pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
Log:
- changed direct estimation to the lm function
- support multiple benchmarks, return return all coefficients

Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R	2012-07-19 01:28:20 UTC (rev 2180)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/CAPM.dynamic.R	2012-07-19 15:06:06 UTC (rev 2181)
@@ -24,8 +24,7 @@
 #' @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)
+#' the benchmark asset return
 #' @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
@@ -42,9 +41,10 @@
 #' @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)
-#' 
+#' CAPM.dynamic(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12, Z=managers[, 9:10])
+#' 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])
+#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10])
+#'
 #' @export
 CAPM.dynamic <- function (Ra, Rb, Rf = 0, Z, lags = 1, ...)
 { # @author Andrii Babii
@@ -74,16 +74,27 @@
         
     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)
+      X = cbind(z, coredata(xRb[1:nrow(z)]), z * matrix(rep(xRb[1:nrow(z)], ncol(z)), nrow(z), ncol(z)))
+      X.df = as.data.frame(X)
+      model = lm(xRa[1:nrow(z)] ~ 1 + ., data = X.df)
+      return(coef(model))
     }
     result = apply(pairs, 1, FUN = function(n, xRa, xRb, z) 
-      dynamic(xRa[, n[1]], xRb, z), xRa = xRa, xRb = xRb, z = z)
+      dynamic(xRa[, n[1]], xRb[, 1], z), xRa = xRa, xRb = xRb, z = z)
+    result = t(result)
     
+    if (ncol(Rb) > 1){
+      for (i in 2:ncol(xRb)){
+        res = apply(pairs, 1, FUN = function(n, xRa, xRb, z) 
+          dynamic(xRa[, n[1]], xRb[, i], z), xRa = xRa, xRb = xRb, z = z)
+        res = t(res)
+        result = rbind(result, res)
+      }
+    }
+    
     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)
+    colnames(result) = c("Average alpha", a, "Average beta", b)
+    rownames(result) = paste(rep(colnames(Ra), ncol(Rb)), "to",  rep(colnames(Rb), each = ncol(Ra)))
     return(result)
 }
\ No newline at end of file

Modified: pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R	2012-07-19 01:28:20 UTC (rev 2180)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/MarketTiming.R	2012-07-19 15:06:06 UTC (rev 2181)
@@ -60,7 +60,7 @@
       Rf = checkData(Rf)
     Ra.ncols = NCOL(Ra)
     Rb.ncols = NCOL(Rb)
-    pairs = expand.grid(1:Ra.ncols, 1:Rb.ncols)
+    pairs = expand.grid(1:Ra.ncols, 1)
     method = method[1]
     xRa = Return.excess(Ra, Rf)
     xRb = Return.excess(Rb, Rf)
@@ -74,18 +74,23 @@
       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])
+      return(coef(model))
     }
   
     result = apply(pairs, 1, FUN = function(n, xRa, xRb) 
-      mt(xRa[, n[1]], xRb[, n[2]]), xRa = xRa, xRb = xRb)
+      mt(xRa[, n[1]], xRb[, 1]), xRa = xRa, xRb = xRb)
+    result = t(result)
+    
+    if (ncol(Rb) > 1){
+      for (i in 2:ncol(xRb)){
+        res = apply(pairs, 1, FUN = function(n, xRa, xRb) 
+          mt(xRa[, n[1]], xRb[, i]), xRa = xRa, xRb = xRb)
+        res = t(res)
+        result = rbind(result, res)
+      }
+    }
   
-    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)
-    }
+    rownames(result) = paste(rep(colnames(Ra), ncol(Rb)), "to",  rep(colnames(Rb), each = ncol(Ra)))
+    colnames(result) = c("Alpha", "Beta", "Gamma")
+    return(result)
 }
\ No newline at end of file



More information about the Returnanalytics-commits mailing list