[Uwgarp-commits] r103 - in pkg/GARPFRM: R demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 5 17:46:28 CET 2014


Author: rossbennett34
Date: 2014-03-05 17:46:28 +0100 (Wed, 05 Mar 2014)
New Revision: 103

Added:
   pkg/GARPFRM/demo/EWMA.R
Modified:
   pkg/GARPFRM/R/EWMA.R
Log:
Updating docs and adding demo for EWMA

Modified: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R	2014-03-05 16:21:01 UTC (rev 102)
+++ pkg/GARPFRM/R/EWMA.R	2014-03-05 16:46:28 UTC (rev 103)
@@ -9,6 +9,12 @@
 #' @param initialWindow initial window of observations used in estimating the 
 #' initial covariance or correlation
 #' @param TRUE/FALSE to return a correlation matrix. Default cor = FALSE.
+#' @examples
+#' data(crsp.short)
+#' # Use the first 5 assets in largecap.ts for the returns data
+#' R <- largecap.ts[, 1:5]
+#' # Estimate the covariance matrix via EWMA
+#' covEst <- EWMA(R, 0.94, 15)
 #' @export
 EWMA <- function(R, lambda=0.94, initialWindow=10, cor=FALSE){
   # Check for lambda between 0 and 1 & initialWindow must be greater than ncol(R)
@@ -63,6 +69,16 @@
 #' @param assets character vector or numeric vector. If 
 #' \code{assets} is of length 1, then the variance will be returned. 
 #' The assets can be specified by name or index.
+#' @examples
+#' data(crsp.short)
+#' # Use the first 5 assets in largecap.ts for the returns data
+#' R <- largecap.ts[, 1:5]
+#' # Estimate the covariance matrix via EWMA
+#' covEst <- EWMA(R, 0.94, 15)
+#' # get the covariance between AMAT and CAT
+#' covAMATCAT <- getCov(covEst, assets=c("AMAT", "CAT"))
+#' cov13 <- getCov(covEst, assets=c(1, 3))
+#' all.equal(covAMATCAT, cov13)
 #' @export
 getCov <- function(EWMA, assets){
   UseMethod("getCov")
@@ -121,6 +137,16 @@
 #' @param object an EWMA object created by \code{EWMA}
 #' @param assets character vector or numeric vector. The assets can be 
 #' specified by name or index.
+#' @examples
+#' data(crsp.short)
+#' # Use the first 5 assets in largecap.ts for the returns data
+#' R <- largecap.ts[, 1:5]
+#' # Estimate the correlation matrix via EWMA
+#' corEst <- EWMA(R, 0.94, 15, TRUE)
+#' # get the correlation between AMAT and CAT
+#' corAMATCAT <- getCov(corEst, assets=c("AMAT", "CAT"))
+#' cor13 <- getCov(corEst, assets=c(1, 3))
+#' all.equal(corAMATCAT, cor13)
 #' @export
 getCor <- function(EWMA, assets){
   UseMethod("getCor")

Added: pkg/GARPFRM/demo/EWMA.R
===================================================================
--- pkg/GARPFRM/demo/EWMA.R	                        (rev 0)
+++ pkg/GARPFRM/demo/EWMA.R	2014-03-05 16:46:28 UTC (rev 103)
@@ -0,0 +1,37 @@
+# EWMA Demo
+
+library(GARPFRM)
+data(crsp.short)
+
+# Use the first 5 assets in largecap.ts for the returns data
+R <- largecap.ts[, 1:5]
+
+# Estimate the covariance matrix via EWMA
+covEst <- EWMA(R, 0.94, 15)
+names(covEst)
+covEst
+
+# get the covariance between AMAT and CAT
+covAMATCAT <- getCov(covEst, assets=c("AMAT", "CAT"))
+cov13 <- getCov(covEst, assets=c(1, 3))
+all.equal(covAMATCAT, cov13)
+
+# Plot the covariance estimate between AMAT and CAT
+# Note that we are passing the covEst object created by the EWMA function
+plot(covEst, assets=c("AMAT", "CAT"))
+
+# specifying a single asset will extract the variance from the EWMA estimate
+varAMAT <- getCov(covEst, assets="AMAT")
+
+# Estimate the correlation matrix
+corEst <- EWMA(R, 0.94, 25, TRUE)
+corEst
+
+# get the correlation between AMGN and DD
+corAMGNDD <- getCor(corEst, assets=c("AMGN", "DD"))
+cor24 <- getCor(corEst, assets=c(2, 4))
+all.equal(corAMGNDD, cor24)
+
+# Plot the correlation estimate between AMGN and DD
+# Note that we are passing the covEst object created by the EWMA function
+plot(corEst, assets=c("AMGN", "DD"))



More information about the Uwgarp-commits mailing list