[Uwgarp-commits] r106 - pkg/GARPFRM/sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Mar 6 02:12:09 CET 2014


Author: rossbennett34
Date: 2014-03-06 02:12:08 +0100 (Thu, 06 Mar 2014)
New Revision: 106

Modified:
   pkg/GARPFRM/sandbox/ross_EWMA.R
Log:
Updates to testing for EWMA functions

Modified: pkg/GARPFRM/sandbox/ross_EWMA.R
===================================================================
--- pkg/GARPFRM/sandbox/ross_EWMA.R	2014-03-05 18:55:21 UTC (rev 105)
+++ pkg/GARPFRM/sandbox/ross_EWMA.R	2014-03-06 01:12:08 UTC (rev 106)
@@ -1,14 +1,13 @@
 
 ##### functions #####
 
-# function to compute EWMA volatility estimates
+# function to compute EWMA volatility estimates of univariate returns
 volEWMA <- function(R, lambda=0.94, initialWindow=10){
   # Check for lambda between 0 and 1 & initialWindow must be greater than ncol(R)
   if ((lambda > 1) || (lambda < 0)) stop("lambda must be greater than 0 or less than 1")
   if (initialWindow > nrow(R)) stop("Initial window must be less than the number of observations")
   
   # R must be an xts object and have only 1 column of data for univariate EWMA estimate
-  # R must be an xts object and have only 2 columns of data for bivariate EWMA estimate
   if(ncol(R) > 1){
     warning("Only using first column of data")
     R <- R[,1]
@@ -21,14 +20,16 @@
   # Compute initial variance estimate
   varOld <- var(initialR)
   
-  # initialize output vector
+  # Initialize output vector
   tmp <- vector("numeric", nrow(testR))
   
+  # Loop through and recursively update variance estimate
   for(i in 1:nrow(testR)){
     tmp[i] <- lambda * varOld + (1 - lambda) * testR[i]^2
     varOld <- tmp[i]
   }
-  # pad with leading NA and compute sqrt of variance vector
+  
+  # Pad with leading NA and compute sqrt of variance vector
   out <- xts(c(rep(NA, initialWindow), sqrt(tmp)), index(R))
   colnames(out) <- colnames(R)
   return(out)
@@ -53,20 +54,22 @@
   # Compute initial estimate
   covOld <- cov(initialR[,1], initialR[,2])
   
-  # initialize output vector
+  # Initialize output vector
   tmp <- vector("numeric", nrow(testR))
   
+  # Loop through and recursively update covariance estimate
   for(i in 1:nrow(testR)){ 
     tmp[i] <- covOld + (1 - lambda) * (testR[i, 1] * testR[i, 2]  - covOld)
     covOld <- tmp[i]
   }
-  # pad with leading NA
+  
+  # Pad with leading NA
   out <- xts(c(rep(NA, initialWindow), tmp), index(R))
   colnames(out) <- paste(colnames(R), collapse=".")
   return(out)
 }
 
-# function to compute EWMA correlation estimates
+# function to compute EWMA correlation estimates of a bivariate dataset
 corEWMA <- function(R, lambda=0.94, initialWindow=10){
   # Check for lambda between 0 and 1 & initialWindow must be greater than ncol(R)
   if ((lambda > 1) || (lambda < 0)) stop("lambda must be greater than 0 or less than 1")
@@ -87,23 +90,27 @@
   varOldX <- var(initialR[,1])
   varOldY <- var(initialR[,2])
   
-  # initialize output vector
+  # Initialize output vector
   tmp <- vector("numeric", nrow(testR))
   
+  # Loop through and recursively update estimates
   for(i in 1:nrow(testR)){
-    # compute the covariance EWMA estimate
+    # Compute the covariance EWMA estimate
     tmpCov <- covOld + (1 - lambda) * (testR[i, 1] * testR[i, 2]  - covOld)
-    # compute the variance EWMA estimate for each asset
+    # Compute the variance EWMA estimate for each asset
     tmpVarX <- lambda * varOldX + (1 - lambda) * testR[i,1]^2
     tmpVarY <- lambda * varOldY + (1 - lambda) * testR[i,2]^2
-    # compute correlation with the covariance and volatility of each asset
+    
+    # Compute correlation with the covariance and volatility of each asset
     tmp[i] <- tmpCov / (sqrt(tmpVarX) * sqrt(tmpVarY))
-    # now update the old values
+    
+    # Now update the old values
     covOld <- tmpCov
     varOldX <- tmpVarX
     varOldY <- tmpVarY
   }
-  # pad with leading NA
+  
+  # Pad with leading NA
   out <- xts(c(rep(NA, initialWindow), tmp), index(R))
   colnames(out) <- paste(colnames(R), collapse=".")
   return(out)
@@ -112,18 +119,19 @@
 ##### testing #####
 library(GARPFRM)
 
+# data and parameters for EWMA estimate
 data(crsp.short)
 R <- largecap.ts[, 1:2]
 lambda <- 0.94
 initialWindow <- 15
 
-# volatility estimate via EWMA
+# volatility estimate of univariate data
 vol1 <- volEWMA(R[,1], lambda, initialWindow)
 
-# covariance estimate via EWMA
+# covariance estimate of bivariate data
 cov1 <- covEWMA(R, lambda, initialWindow)
 
-# correlation estimate via EWMA
+# correlation estimate of bivariate data
 cor1 <- corEWMA(R, lambda, initialWindow)
 
 plot(vol1)



More information about the Uwgarp-commits mailing list