[Uwgarp-commits] r120 - in pkg/GARPFRM: R sandbox vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 15 20:43:24 CET 2014


Author: rossbennett34
Date: 2014-03-15 20:43:23 +0100 (Sat, 15 Mar 2014)
New Revision: 120

Modified:
   pkg/GARPFRM/R/EWMA.R
   pkg/GARPFRM/sandbox/ross_EWMA.R
   pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.Rnw
   pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.pdf
Log:
Revisions to EWMA code for multivariate estimates

Modified: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R	2014-03-15 03:07:15 UTC (rev 119)
+++ pkg/GARPFRM/R/EWMA.R	2014-03-15 19:43:23 UTC (rev 120)
@@ -275,34 +275,76 @@
 }
 
 # multivariate EWMA covariance estimate
+# mvEWMAcov <- function(R, lambda, initialWindow){
+#   # Separate data into a initializing window and a testing window
+#   initialR = R[1:initialWindow,]
+#   testR = R[(initialWindow+1):nrow(R),]
+#   
+#   # Initialize starting values
+#   lagCov = cov(initialR)
+#   oldR = as.numeric(colMeans(initialR))
+#   
+#   est = vector("list", nrow(testR))
+#   for(i in 1:nrow(testR)){
+#     est[[i]] = lambda * (oldR %*% t(oldR)) + (1 - lambda) * lagCov
+#     # Update values from the current period
+#     lagCov = est[[i]]
+#     oldR = as.numeric(testR[i,])
+#   }
+#   # Properly assign list key to date
+#   names(est) <- index(testR)
+#   return(est)
+# }
+
+# multivariate EWMA covariance estimate
 mvEWMAcov <- function(R, lambda, initialWindow){
-  # Separate data into a initializing window and a testing window
-  initialR = R[1:initialWindow,]
-  testR = R[(initialWindow+1):nrow(R),]
-  
-  # Initialize starting values
-  lagCov = cov(initialR)
-  oldR = as.numeric(colMeans(initialR))
-  
-  est = vector("list", nrow(testR))
-  for(i in 1:nrow(testR)){
-    est[[i]] = lambda * (oldR %*% t(oldR)) + (1 - lambda) * lagCov
-    # Update values from the current period
-    lagCov = est[[i]]
-    oldR = as.numeric(testR[i,])
+  # This computes the covariance matrix for the final time period
+  cnames <- colnames(R)
+  n <- ncol(R)
+  mat <- matrix(0, n, n)
+  rownames(mat) <- cnames
+  colnames(mat) <- cnames
+  for(i in 1:n){
+    for(j in i:n){
+      # EWMA covariance estimate
+      tmpCov <- uvEWMAcov(cbind(R[,i], R[,j]), lambda, initialWindow)
+      mat[i,j] <- as.numeric(last(tmpCov))
+      if(i != j){
+        mat[j,i] <- as.numeric(last(tmpCov))
+      }
+    }
   }
-  # Properly assign list key to date
-  names(est) <- index(testR)
-  return(est)
+  return(mat)
 }
 
 # multivariate EWMA correlation estimate
+# mvEWMAcor <- function(R, lambda, initialWindow){
+#   # Separate data into a initializing window and a testing window
+#   initialR = R[1:initialWindow,]
+#   testR = R[(initialWindow+1):nrow(R),]
+#   
+#   # Initialize starting values
+#   lagCov = cov(initialR)
+#   oldR = as.numeric(colMeans(initialR))
+#   
+#   est = vector("list", nrow(testR))
+#   for(i in 1:nrow(testR)){
+#     # est[[i]] = lambda * (oldR %*% t(oldR)) + (1 - lambda) * lagCov
+#     tmp = lambda * (oldR %*% t(oldR)) + (1 - lambda) * lagCov
+#     # Update values from the current period
+#     # lagCov = est[[i]]
+#     est[[i]] <- cov2cor(tmp)
+#     lagCov <- tmp
+#     oldR = as.numeric(testR[i,])
+#   }
+#   # Properly assign list key to date
+#   names(est) <- index(testR)
+#   return(est)
+# }
+
+# multivariate EWMA correlation estimate
 mvEWMAcor <- function(R, lambda, initialWindow){
-  cov_est <- mvEWMAcov(R=R, lambda=lambda, initialWindow=initialWindow)
-  est <- lapply(cov_est, cov2cor)
-  # Properly assign list key to date
-  # names(est) <- index(testR)
-  return(est)
+  cov2cor(mvEWMAcov(R, lambda, initialWindow))
 }
 
 #' Realized Volatility
@@ -489,7 +531,11 @@
   cat("type: ", x$model$type, "\n\n", sep="")
   
   cat("Final Period EWMA Estimate: \n")
-  print(last(x$estimate))
+  if(inherits(x, "mvEWMAcov") | inherits(x, "mvEWMAcor")){
+    print(x$estimate)
+  } else {
+    print(last(x$estimate))
+  }
 }
 
 # extract the covariance between two assets from an mvEWMAcov object
@@ -521,7 +567,11 @@
   
   if(length(assets) == 1) assets[2] <- assets[1]
   
-  cnames <- colnames(EWMA$estimate[[1]])
+  # cnames <- colnames(EWMA$estimate[[1]])
+  cnames <- colnames(EWMA$estimate)
+  R <- EWMA$data$R
+  lambda <- EWMA$model$lambda
+  initialWindow <- EWMA$model$initialWindow
   
   # Check if asset is a character
   if(is.character(assets[1]) & is.character(assets[2])){
@@ -534,10 +584,10 @@
     idx1 = assets[1]
     idx2 = assets[2]
   }
-  
-  out = xts(unlist(lapply(EWMA$estimate, function(X) X[idx1, idx2])), as.Date(names(EWMA$estimate)))
-  colnames(out) = paste(cnames[idx1], cnames[idx2], sep=".")
-  
+  #out = xts(unlist(lapply(EWMA$estimate, function(X) X[idx1, idx2])), as.Date(names(EWMA$estimate)))
+  #colnames(out) = paste(cnames[idx1], cnames[idx2], sep=".")
+  # estimate the EWMA cov between the assets specified
+  out <- uvEWMAcov(R=R[,c(idx1,idx2)], lambda=lambda, initialWindow=initialWindow)
   return(out)
 }
 
@@ -602,7 +652,11 @@
   
   if(length(assets) == 1) assets[2] <- assets[1]
   
-  cnames <- colnames(EWMA$estimate[[1]])
+  # cnames <- colnames(EWMA$estimate[[1]])
+  cnames <- colnames(EWMA$estimate)
+  R <- EWMA$data$R
+  lambda <- EWMA$model$lambda
+  initialWindow <- EWMA$model$initialWindow
   
   # Check if asset is a character
   if(is.character(assets[1]) & is.character(assets[2])){
@@ -615,10 +669,10 @@
     idx1 = assets[1]
     idx2 = assets[2]
   }
-  
-  out = xts(unlist(lapply(EWMA$estimate, function(X) X[idx1, idx2])), as.Date(names(EWMA$estimate)))
-  colnames(out) = paste(cnames[idx1], cnames[idx2], sep=".")
-  
+  #out = xts(unlist(lapply(EWMA$estimate, function(X) X[idx1, idx2])), as.Date(names(EWMA$estimate)))
+  #colnames(out) = paste(cnames[idx1], cnames[idx2], sep=".")
+  # estimate the EWMA cov between the assets specified
+  out <- uvEWMAcor(R=R[,c(idx1,idx2)], lambda=lambda, initialWindow=initialWindow)
   return(out)
 }
 

Modified: pkg/GARPFRM/sandbox/ross_EWMA.R
===================================================================
--- pkg/GARPFRM/sandbox/ross_EWMA.R	2014-03-15 03:07:15 UTC (rev 119)
+++ pkg/GARPFRM/sandbox/ross_EWMA.R	2014-03-15 19:43:23 UTC (rev 120)
@@ -48,7 +48,6 @@
 cov_mv
 # Extract the estimated covariance between ORCL and MSFT
 tail(getCov(cov_mv, assets=c("ORCL", "MSFT")))
-
 # These two are equivalent
 plot(cov_mv, assets=c("ORCL", "MSFT"))
 plot(cov_mv, assets=c(1, 2))
@@ -65,6 +64,3 @@
 plot(cor_mv, assets=c(1, 4))
 
 
-
-
-

Modified: pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.Rnw
===================================================================
--- pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.Rnw	2014-03-15 03:07:15 UTC (rev 119)
+++ pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.Rnw	2014-03-15 19:43:23 UTC (rev 120)
@@ -67,8 +67,6 @@
 
 Where $\alpha_i$ is the weight given to the observation $i$ days ago.
 
-TODO: add information about the ARCH model
-
 A special case of equation is the Exponentially Weighted Moving Average (EWMA) Model.
 
 \section{Exponentially Weighted Moving Average Model}
@@ -83,7 +81,7 @@
 where:
 \begin{description}
   \item[$\hat{\sigma}_{n-1}^2$] is the estimated variance rate of period $n-1$
-  \item[$u_{n-1}$] is the return of preiod $n-1$
+  \item[$u_{n-1}^2$] is the squared return of preiod $n-1$
   \item[$\lambda$] is a constant between 0 and 1
 \end{description}
 
@@ -107,7 +105,7 @@
 volEst
 @
 
-An important point to note is that we are using weekly returns to estimate weekly volatility while the lambda value used in the RiskMetrics database is for daily volatility. A data driven approach for selecting a value for $\lambda$ is to determine the $\lambda$ that minimizes the sum of squared errors between the realized volatility and the estimated volatility from the EWMA model.
+An important point to note is that we are using weekly returns to estimate weekly volatility while the lambda value used in the RiskMetrics database is for daily volatility estimates. A data driven approach for selecting a value for $\lambda$ is to determine the $\lambda$ that minimizes the mean squared error between the realized volatility and the estimated volatility from the EWMA model.
 
 Here we calculate the realized volatility defined as the equally weighted average of the standard deviation of the subsequent $n$ periods.
 <<>>=
@@ -122,7 +120,7 @@
        col=c("black", "red"), lty=c(1,1), cex=0.8, bty="n")
 @
 
-The \code{estimateLambda} function estimates the value for $\lambda$ by minimizing the mean squared error between the realized volatility and the EWMA model estimated volatility.
+The \code{estimateLambdaVol} function estimates the value for $\lambda$ by minimizing the mean squared error between the realized volatility and the EWMA model estimated volatility.
 <<>>=
 # Estimate lambda
 # Use initialWindow = 15 for the EWMA volatility estimate and
@@ -197,7 +195,7 @@
 plot(corEst, main="EWMA Estimated Correlation")
 @
 
-The previous examples demonstrated using and EWMA model to estimate the volatility of the returns of a single asset, and the correlation and volatility between the returns of two assets. Now we move to using an EWMA model to estimated the covariance and correlation of a multivariate data set.
+The previous examples demonstrated using an EWMA model to estimate the volatility of the returns of a single asset, and the correlation and volatility between the returns of two assets. Now we move to using an EWMA model to estimated the covariance and correlation of a multivariate data set.
 
 <<>>=
 # Use the first 4 columns of the largecap_weekly dataset
@@ -214,7 +212,6 @@
 covEst
 @
 
-TODO: GETTING UNRELIABLE CORRELATION ESTIMATES... NOT SURE WHY
 In a similar fashion, we can also use the EWMA model to estimate the correlation matrix.
 <<>>=
 # calculate the sample covariance matrix
@@ -230,7 +227,7 @@
 
 
 \section{The GARCH(1,1) Model}
-We now present the generalized autoregressive conditional heteroskedasticity (GARCH) as presented by Bollerslev in 1986 as a way to estimate volatility. The general GARCH(p,q) model calculates $\sigma_n^2$ from the most recent $p$ observations of $u^2$ and the most recent $q$ estimates of $\sigma_n^2$. The GARCH(1,1) model refers to the most recent observation of $u^2$ and the most recent estimate of $\sigma_n^2$. The GARCH(1,1) is a popular model and the one we will focus on. The equation for the GARCH(1,1) model is
+We now demonstrate the generalized autoregressive conditional heteroskedasticity (GARCH) as presented by Bollerslev in 1986 as a way to estimate volatility. The general GARCH(p,q) model calculates $\sigma_n^2$ from the most recent $p$ observations of $u^2$ and the most recent $q$ estimates of $\sigma_n^2$. The GARCH(1,1) model refers to the most recent observation of $u^2$ and the most recent estimate of $\sigma_n^2$. The GARCH(1,1) is a popular model and the one we will focus on. The equation for the GARCH(1,1) model is
 
 \begin{equation}
 \sigma_n^2 = \gamma V_L + \alpha u_{n-1}^2 + \beta \sigma_{n-1}^2

Modified: pkg/GARPFRM/vignettes/EstimatingVolatilitiesCorrelation.pdf
===================================================================
(Binary files differ)



More information about the Uwgarp-commits mailing list