[Uwgarp-commits] r66 - in pkg/GARPFRM: . R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 8 06:06:48 CET 2014


Author: tfillebeen
Date: 2014-02-08 06:06:46 +0100 (Sat, 08 Feb 2014)
New Revision: 66

Modified:
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/EWMA.R
Log:
Correlation + plot

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-02-08 02:33:06 UTC (rev 65)
+++ pkg/GARPFRM/NAMESPACE	2014-02-08 05:06:46 UTC (rev 66)
@@ -3,6 +3,7 @@
 S3method(getAlphas,capm_uv)
 S3method(getBetas,capm_mlm)
 S3method(getBetas,capm_uv)
+S3method(getCor,EWMACor)
 S3method(getCov,EWMACovar)
 S3method(getCov,EWMAVar)
 S3method(getStatistics,capm_mlm)
@@ -16,9 +17,11 @@
 export(garch11)
 export(getAlphas)
 export(getBetas)
+export(getCor)
 export(getCov)
 export(getStatistics)
 export(hypTest)
+export(plot.EWMACor)
 export(plot.EWMACovar)
 export(plot.EWMAVar)
 export(plot.capm_mlm)

Modified: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R	2014-02-08 02:33:06 UTC (rev 65)
+++ pkg/GARPFRM/R/EWMA.R	2014-02-08 05:06:46 UTC (rev 66)
@@ -64,20 +64,23 @@
   out <- covTmp
   # Properly assign list key to date
   names(out) <- index(testR)
+  
   # Check correlation option
-  if(cor) out <- lapply(out, cov2cor)
-    
-  if(ncol(R) > 1) { class(out) <- c("EWMACovar")
-  } else if (ncol(R) == 1){class(out) <- c("EWMAVar")}
+  if(cor & ncol(R)>1) {out <- lapply(out, cov2cor)
+  class(out) <- c("EWMACor")
+  }else if(cor & ncol(R)==1) {stop("EWMA correlation is only to be estimated for two or more assets")}
+  
+  if(cor == FALSE & ncol(R) > 1) { class(out) <- c("EWMACovar")
+  } else if (cor == FALSE & ncol(R) == 1){class(out) <- c("EWMAVar")}
   out$y_data <- R
   return(out)
   
   } else {
-           stop("For exponential decay lambda must belong to ]0:1[ ") 
+           stop("For exponential decay lambda must belong to ]0:1[ and/or window is too large") 
   }
 }
 
-#' EWMA volatility/cross-volatility
+#' EWMA Volatility/Cross-Volatility
 #' 
 #' Description of EWMA Vola
 #' 
@@ -126,7 +129,38 @@
   return(out)
 }
 
+#' EWMA Correlation
+#' 
+#' Description of EWMA Correlation, requires two assets
+#' 
+#' @param object a EWMA object created by \code{\link{EWMA}}
+#' @export
+getCor <- function(object, asset1, asset2){
+  UseMethod("getCor")
+}
 
+#' @method getCor EWMACor
+#' @S3method getCor EWMACor
+getCor.EWMACor <- function(object, asset1, asset2){
+  if(!inherits(object, "EWMACor")) stop("object must be of class EWMACor")
+  # Check if asset is a character 
+  object[[length(object)]] = NULL
+  if(is.character(asset1) & is.character(asset2)){
+    idx1 = grep(asset1, colnames(object[[1]]))
+    if(length(idx1) == 0) stop("name for asset1 not in object")
+    idx2 = grep(asset2, colnames(object[[1]]))
+    if(length(idx2) == 0) stop("name for asset2 not in object")
+  } else {
+    # Then dimensions are enough to find covar
+    idx1 = asset1
+    idx2 = asset2
+  }
+  out = xts(unlist(lapply(object, function(object) object[idx1, idx2])), as.Date(names(object)))
+  colnames(out) = paste(asset1, asset2, sep=".")
+  return(out)
+}
+
+
 # EWMA plotting for covar
 #' @export
 plot.EWMACovar <- function(object, asset1, asset2){
@@ -142,8 +176,8 @@
     idx2 = asset2
   }
   tmp = getCov(object,asset1, asset2)
-  plot(x=time(as.zoo(tmp)), y=tmp, type="l", xlab="Time", ylab="Covar", lwd=2, col="blue",
-       main="EWMA Covar");
+  plot(x=time(as.zoo(tmp)), y=tmp, type="l", xlab="Time", ylab="Covariance", lwd=2, col="blue",
+       main="EWMA Covariance");
   grid()
   abline(h=var(object$y_data)[idx1,idx2], lwd=2, col="red")
   }
@@ -152,9 +186,30 @@
 #' @export
 plot.EWMAVar <- function(object,asset1){
   tmp = getCov(object,asset1)
-  plot(x=time(as.zoo(tmp)),y=tmp, type="l", xlab="Time", ylab="Var", lwd=2, col="blue",
-       main="EWMA Var");
+  plot(x=time(as.zoo(tmp)),y=tmp, type="l", xlab="Time", ylab="Variance", lwd=2, col="blue",
+       main="EWMA Variance");
   grid()
   abline(h=var(object$y_data), lwd=2, col="red")
 }
 
+# EWMA plotting for correlation
+#' @export
+plot.EWMACor <- function(object, asset1, asset2){
+  # Check if asset is a character 
+  if(is.character(asset1) & is.character(asset2)){
+    idx1 = grep(asset1, colnames(object[[1]]))
+    if(length(idx1) == 0) stop("name for asset1 not in object")
+    idx2 = grep(asset2, colnames(object[[1]]))
+    if(length(idx2) == 0) stop("name for asset2 not in object")
+  } else {
+    # Then dimensions are enough to find covar
+    idx1 = asset1
+    idx2 = asset2
+  }
+  tmp = getCor(object,asset1, asset2)
+  plot(x=time(as.zoo(tmp)), y=tmp, type="l", xlab="Time", ylab="Correlation", lwd=2, col="blue",
+       main="EWMA Correlation");
+  grid()
+  abline(h=cor(object$y_data)[idx1,idx2], lwd=2, col="red")
+}
+



More information about the Uwgarp-commits mailing list