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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 8 07:29:40 CET 2014


Author: tfillebeen
Date: 2014-02-08 07:29:39 +0100 (Sat, 08 Feb 2014)
New Revision: 69

Modified:
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/EWMA.R
   pkg/GARPFRM/R/garch11.R
   pkg/GARPFRM/sandbox/test_EWMA_GARCH.R
Log:
EWMA update + garch11 modifications

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-02-08 05:39:59 UTC (rev 68)
+++ pkg/GARPFRM/NAMESPACE	2014-02-08 06:29:39 UTC (rev 69)
@@ -10,6 +10,11 @@
 S3method(getStatistics,capm_uv)
 S3method(hypTest,capm_mlm)
 S3method(hypTest,capm_uv)
+S3method(plot,EWMACor)
+S3method(plot,EWMACovar)
+S3method(plot,EWMAVar)
+S3method(plot,capm_mlm)
+S3method(plot,capm_uv)
 export(CAPM)
 export(EWMA)
 export(chartSML)
@@ -21,8 +26,3 @@
 export(getCov)
 export(getStatistics)
 export(hypTest)
-export(plot.EWMACor)
-export(plot.EWMACovar)
-export(plot.EWMAVar)
-export(plot.capm_mlm)
-export(plot.capm_uv)

Modified: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R	2014-02-08 05:39:59 UTC (rev 68)
+++ pkg/GARPFRM/R/EWMA.R	2014-02-08 06:29:39 UTC (rev 69)
@@ -39,20 +39,22 @@
 #' 
 #' @param R
 #' @param lambda
-#' @param inWnd
-#' @param cor option (default = FALSE) 
+#' @param initialWindow is the initializing window
+#' @param correlation option (cor by default = FALSE) 
 #' @export
 #' 
 #' 
-EWMA <- function(R, lambda=0.94, inWnd=10, cor=FALSE){
-  # Check for lambda between 0 and 1 & inWnd must be greater than ncol(R)
-  if (((lambda<1 || lambda > 0)) & inWnd< nrow(R)) {
+EWMA <- function(R, lambda=0.94, initialWindow=10, cor=FALSE){
+  # Check for lambda between 0 and 1 & initialWindow must be greater than ncol(R)
+  if (((lambda>1 || lambda < 0))) {stop("For exponential decay lambda must belong to ]0:1[")}
+  if (initialWindow> nrow(R)){stop("Initialization window is too large")}
+  
   # Separate data into a initializing window and a testing window
-  inR = R[1:inWnd,]
-  testR = R[(inWnd+1):nrow(R),]
+  initialR = R[1:initialWindow,]
+  testR = R[(initialWindow+1):nrow(R),]
   
   # Initialization of covariance matrix
-  lagCov = cov(inR)
+  lagCov = cov(initialR)
   covTmp = vector("list", nrow(testR))
   for(i in 1:nrow(testR)){
     # Extract R for the ith time step
@@ -70,13 +72,11 @@
     class(out) <- c("EWMACor")
     }else if(cor & ncol(R)==1) {stop("EWMA correlation is only to be estimated for two or more assets")}
     
+    # Check for Covar or Var
     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
-  
-  } else {
-           stop("For exponential decay lambda must belong to ]0:1[ and/or window is too large") 
-  }
+  # Bind initial data to EWMA object in order to plot a comparison
+  out$y_data <- R
   return(out)
   
 }
@@ -95,8 +95,10 @@
 #' @S3method getCov EWMACovar
 getCov.EWMACovar <- function(object, asset1, asset2){
   if(!inherits(object, "EWMACovar")) stop("object must be of class EWMACovar")
-    # Check if asset is a character 
+  # Manipulate object for feasible use   
   object[[length(object)]] = NULL
+  
+  # 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")
@@ -107,8 +109,10 @@
       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)
 }
 
@@ -116,8 +120,10 @@
 #' @S3method getCov EWMAVar
 getCov.EWMAVar <- function(object, asset1){
   if(!inherits(object, "EWMAVar")) stop("object must be of class EWMAVar")
+  # Manipulate object for feasible use  
+  object[[length(object)]] = NULL
+  
   # Check if asset is a character
-  object[[length(object)]] = NULL
   if(is.character(asset1)){
     idx1 = grep(asset1, colnames(object[[1]]))
     if(length(idx1) == 0) stop("name for asset1 not in object")
@@ -127,6 +133,7 @@
   }
   out = xts(unlist(lapply(object, function(object) object[idx1])), as.Date(names(object)))
   colnames(out) = paste(asset1, sep=".")
+  
   return(out)
 }
 
@@ -144,20 +151,24 @@
 #' @S3method getCor EWMACor
 getCor.EWMACor <- function(object, asset1, asset2){
   if(!inherits(object, "EWMACor")) stop("object must be of class EWMACor")
+  # Manipulate object for feasible use  
+  object[[length(object)]] = NULL
+  
   # 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")
+      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)
 }
 
@@ -168,9 +179,9 @@
   # 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")
+      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
@@ -199,9 +210,9 @@
   # 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")
+      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
@@ -212,5 +223,4 @@
        main="EWMA Correlation");
   grid()
   abline(h=cor(object$y_data)[idx1,idx2], lwd=2, col="red")
-}
-
+}
\ No newline at end of file

Modified: pkg/GARPFRM/R/garch11.R
===================================================================
--- pkg/GARPFRM/R/garch11.R	2014-02-08 05:39:59 UTC (rev 68)
+++ pkg/GARPFRM/R/garch11.R	2014-02-08 06:29:39 UTC (rev 69)
@@ -2,13 +2,15 @@
 #' 
 #' Description of GARCH(1,1)
 #' 
-#' @param object GARCH(1,1)
+#' @param R GARCH(1,1)
+#' @param model “sGARCH”, “fGARCH”, “eGARCH”, “gjrGARCH”, “apARCH” and “iGARCH” and “csGARCH”
+#' @param distribution.model. Valid choices are “norm” for the normal distibution, “snorm” for the skew-normal distribution, “std” for the student-t, “sstd” for the skew-student, “ged” for the generalized error distribution, “sged” for the skew-generalized error distribution, “nig” for the normal inverse gaussian distribution, “ghyp” for the Generalized Hyperbolic, and “jsu” for Johnson's SU distribution. 
 #' @export
-# UV N~GARCH(1,1) for each series
-garch11 <- function(object){
+# By default we use UV N~GARCH(1,1) and Bollerslev for each series
+garch11 <- function(R, model = "sGarch", distribution.model = "norm"){
 garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)), 
-                          variance.model = list(garchOrder = c(1,1), model = "sGARCH"), 
-                          distribution.model = "norm")
+                          variance.model = list(garchOrder = c(1,1), model), 
+                          distribution.model)
 
 # DCC specification: GARCH(1,1) for conditional cor
 dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ), 
@@ -23,7 +25,8 @@
 #' 
 #' Description of forecast GARCH(1,1)
 #' 
-#' @param object a garch11 object created by \code{\link{GARCH(1,1)}}
+#' @param garch11 object created by \code{\link{GARCH(1,1)}}
+#' @param window is the forecast window (default is set to window = 100)
 #' @export
 fcstGarch11 <- function(object, window){
   UseMethod("fcstGarch11")
@@ -31,7 +34,8 @@
 
 #' @method fcstGarch11 Dccfit
 #' @S3method fcstGarch11 DCCfit
-fcstGarch11.DCCfit <- function(object,window = 100){
+fcstGarch11.DCCfit <- function(object, window = 100){
+  #if ((window > nrow(object))) {stop("Window is too large to forecast")}
   result = dccforecast(garch11, n.ahead=window)
   return(result)
 }
\ No newline at end of file

Modified: pkg/GARPFRM/sandbox/test_EWMA_GARCH.R
===================================================================
--- pkg/GARPFRM/sandbox/test_EWMA_GARCH.R	2014-02-08 05:39:59 UTC (rev 68)
+++ pkg/GARPFRM/sandbox/test_EWMA_GARCH.R	2014-02-08 06:29:39 UTC (rev 69)
@@ -142,7 +142,7 @@
 ts.plot(rcor(garch11)[1,2,])
 
 # Forecasting conditional vol and cor, default wd = 100
-fcstGarch11 = fGarch11(garch11)
+fcstGarch11 = fcstGarch11(garch11)
 
 class(fcstGarch11)
 slotNames(fcstGarch11)



More information about the Uwgarp-commits mailing list