[Uwgarp-commits] r73 - in pkg/GARPFRM: . R man sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Feb 8 19:04:59 CET 2014
Author: rossbennett34
Date: 2014-02-08 19:04:58 +0100 (Sat, 08 Feb 2014)
New Revision: 73
Added:
pkg/GARPFRM/man/getCor.Rd
Modified:
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/EWMA.R
pkg/GARPFRM/man/EWMA.Rd
pkg/GARPFRM/man/getCov.Rd
pkg/GARPFRM/sandbox/ross_EWMA.R
Log:
Cleaning up EWMA function and adding some examples to ross_EWMA in sandbox
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-02-08 18:02:05 UTC (rev 72)
+++ pkg/GARPFRM/NAMESPACE 2014-02-08 18:04:58 UTC (rev 73)
@@ -10,11 +10,6 @@
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)
@@ -26,3 +21,8 @@
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 18:02:05 UTC (rev 72)
+++ pkg/GARPFRM/R/EWMA.R 2014-02-08 18:04:58 UTC (rev 73)
@@ -42,8 +42,6 @@
#' @param initialWindow is the initializing window
#' @param correlation option (cor by default = FALSE)
#' @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)
if (((lambda>1 || lambda < 0))) {stop("For exponential decay lambda must belong to ]0:1[")}
@@ -63,24 +61,41 @@
# Update lagCov to be covTmp from the current period
lagCov <- covTmp[[i]]
}
- out <- covTmp
+ # Final estimated EWMA of covariance
+ estEWMA <- covTmp
# Properly assign list key to date
- names(out) <- index(testR)
+ names(estEWMA) <- index(testR)
- # Check correlation option
- if(cor & ncol(R)>1) {out <- lapply(out, cov2cor)
+ # Add R as a separate element to object to return
+ # Do we want to return the entire R object or just testR?
+ out <- list(EWMA=estEWMA, R=R)
+
+ # Check correlation option
+ # use proper indentation, makes it easier to read
+ if(cor & ncol(R) > 1) {
+ out$EWMA <- lapply(out$EWMA, cov2cor)
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")}
+ } else if(cor & ncol(R)==1) {
+ stop("EWMA correlation is only to be estimated for two or more assets")
+ }
+
+ # Check for Covar or Var
+ # use proper indentation, makes it easier to read
+ if((cor == FALSE) & (ncol(R) > 1)) {
+ class(out) <- c("EWMACovar")
+ } else if ((cor == FALSE) & (ncol(R) == 1)){
+ class(out) <- c("EWMAVar")
+ }
# Bind initial data to EWMA object in order to plot a comparison
- out$y_data <- R
+ # out$y_data <- R
+ # out$y_data <- R adds R to the list element
+ # The EWMA estimate and R should be separate elements in the list returned
return(out)
-
}
+# The arguments for getCov.* must match the arguments for your getCov generic
+# method
+
#' EWMA Volatility/Cross-Volatility
#'
#' Description of EWMA Vola
@@ -96,24 +111,24 @@
getCov.EWMACovar <- function(object, asset1, asset2){
if(!inherits(object, "EWMACovar")) stop("object must be of class EWMACovar")
# Manipulate object for feasible use
- object[[length(object)]] = NULL
+ # 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")
- 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
- }
+ if(is.character(asset1) & is.character(asset2)){
+ idx1 = grep(asset1, colnames(object$EWMA[[1]]))
+ if(length(idx1) == 0) stop("name for asset1 not in object")
+ idx2 = grep(asset2, colnames(object$EWMA[[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=".")
+ out = xts(unlist(lapply(object$EWMA, function(X) X[idx1, idx2])), as.Date(names(object$EWMA)))
+ colnames(out) = paste(asset1, asset2, sep=".")
- return(out)
+ return(out)
}
#' @method getCov EWMAVar
@@ -121,18 +136,18 @@
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
+ # object[[length(object)]] = NULL
# Check if asset is a character
if(is.character(asset1)){
- idx1 = grep(asset1, colnames(object[[1]]))
+ idx1 = grep(asset1, colnames(object$EWMA[[1]]))
if(length(idx1) == 0) stop("name for asset1 not in object")
} else {
# Then dimensions are enough to find covar
idx1 = asset1
}
- out = xts(unlist(lapply(object, function(object) object[idx1])), as.Date(names(object)))
- colnames(out) = paste(asset1, sep=".")
+ out = xts(unlist(lapply(object$EWMA, function(x) x[idx1])), as.Date(names(object$EWMA)))
+ colnames(out) = asset1
return(out)
}
@@ -152,48 +167,55 @@
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
+ # 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")
- idx2 = grep(asset2, colnames(object[[1]]))
- if(length(idx2) == 0) stop("name for asset2 not in object")
+ idx1 = grep(asset1, colnames(object$EWMA[[1]]))
+ if(length(idx1) == 0) stop("name for asset1 not in object")
+ idx2 = grep(asset2, colnames(object$EWMA[[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)))
+ out = xts(unlist(lapply(object$EWMA, function(x) x[idx1, idx2])), as.Date(names(object$EWMA)))
colnames(out) = paste(asset1, asset2, sep=".")
return(out)
}
+# The generic method for plot is
+# plot(x, y, ...)
+# The first arguments for your plot.* methods must match the generic plot method
+# plot.EWMACovar(x, y, ..., asset1, asset2)
+
# EWMA plotting for covar
#' @export
plot.EWMACovar <- 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")
+ 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 = getCov(object,asset1, asset2)
+ tmp = getCov(object, asset1, asset2)
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")
- }
+ abline(h=var(object$R)[idx1,idx2], lwd=2, col="red")
+}
+# plot.EWMAVar(x, y, ..., asset1)
+
# EWMA plotting for var
#' @export
plot.EWMAVar <- function(object,asset1){
@@ -201,26 +223,28 @@
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")
+ abline(h=var(object$R), lwd=2, col="red")
}
+# plot.EWMACor(x, y, ..., asset1, asset2)
+
# 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")
+ 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)
+ 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")
-}
\ No newline at end of file
+ abline(h=cor(object$R)[idx1,idx2], lwd=2, col="red")
+}
Modified: pkg/GARPFRM/man/EWMA.Rd
===================================================================
--- pkg/GARPFRM/man/EWMA.Rd 2014-02-08 18:02:05 UTC (rev 72)
+++ pkg/GARPFRM/man/EWMA.Rd 2014-02-08 18:04:58 UTC (rev 73)
@@ -2,16 +2,16 @@
\alias{EWMA}
\title{Exponential Weighted Moving Average (EWMA)}
\usage{
- EWMA(R, lambda = 0.94, inWnd = 10, cor = FALSE)
+ EWMA(R, lambda = 0.94, initialWindow = 10, cor = FALSE)
}
\arguments{
\item{R}{}
\item{lambda}{}
- \item{inWnd}{}
+ \item{initialWindow}{is the initializing window}
- \item{cor}{option (default = FALSE)}
+ \item{correlation}{option (cor by default = FALSE)}
}
\description{
Description of EWMA. The function handles UV and MLM
Added: pkg/GARPFRM/man/getCor.Rd
===================================================================
--- pkg/GARPFRM/man/getCor.Rd (rev 0)
+++ pkg/GARPFRM/man/getCor.Rd 2014-02-08 18:04:58 UTC (rev 73)
@@ -0,0 +1,14 @@
+\name{getCor}
+\alias{getCor}
+\title{EWMA Correlation}
+\usage{
+ getCor(object, asset1, asset2)
+}
+\arguments{
+ \item{object}{a EWMA object created by
+ \code{\link{EWMA}}}
+}
+\description{
+ Description of EWMA Correlation, requires two assets
+}
+
Modified: pkg/GARPFRM/man/getCov.Rd
===================================================================
--- pkg/GARPFRM/man/getCov.Rd 2014-02-08 18:02:05 UTC (rev 72)
+++ pkg/GARPFRM/man/getCov.Rd 2014-02-08 18:04:58 UTC (rev 73)
@@ -1,6 +1,6 @@
\name{getCov}
\alias{getCov}
-\title{EWMA volatility/cross-volatility}
+\title{EWMA Volatility/Cross-Volatility}
\usage{
getCov(object, asset1, asset2)
}
Modified: pkg/GARPFRM/sandbox/ross_EWMA.R
===================================================================
--- pkg/GARPFRM/sandbox/ross_EWMA.R 2014-02-08 18:02:05 UTC (rev 72)
+++ pkg/GARPFRM/sandbox/ross_EWMA.R 2014-02-08 18:04:58 UTC (rev 73)
@@ -7,80 +7,88 @@
# This is a list of two elements
names(ewma_est)
-# for the EWMA estimate
+# extract the EWMA estimate
ewma_est$EWMA
-# for the data
+# extract the data
ewma_est$R
-getCov(ewma_est, 1, 2)
+tmpCov <- getCov(ewma_est, 1, 2)
-plot(ewma_est, asset1=1, asset2=2)
+# This should throw an error because ewma_est is a covariance estimate
+getCor(ewma_est, 1, 2)
-# might need a separate function for univariate time series of returns
+# correlation between assets 1 and 4
+estCor <- getCor(EWMA(R, cor=TRUE), 1, 4)
-# estimate covariance or correlation using EWMA for a multivariate data set
-rbEWMA <- function(R, lambda=0.94, training_period=10, cor=FALSE){
- # check for training_period must be greater than ncol(R)
- # check for lambda between 0 and 1
-
- # Separate data into a training set and a testing set
- R_training <- R[1:training_period,]
- R_testing <- R[(training_period+1):nrow(R),]
-
- # calculate a starting covariance matrix
- cov_start <- cov(R_training)
- cov_lag <- cov_start
- tmp_cov <- vector("list", nrow(R_testing))
-
- for(i in 1:nrow(R_testing)){
- # extract R for the ith time step
- tmpR <- R_testing[i,]
- tmp_cov[[i]] <- lambda * (t(tmpR) %*% tmpR) + (1 - lambda) * cov_lag
- # update cov_lag to be tmp_cov from the current period
- cov_lag <- tmp_cov[[i]]
- }
- out <- tmp_cov
- names(out) <- index(R_testing)
- if(cor) out <- lapply(out, cov2cor)
- return(out)
-}
+plot(ewma_est, asset1=1, asset2=2)
+plot(EWMA(R, cor=TRUE), asset1=1, asset2=2)
-
-getCov <- function(object, asset1, asset2){
- # check for
- 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 {
- # checks for dimensions
- idx1 <- asset1
- idx2 <- asset2
- }
- out <- xts(unlist(lapply(x, function(x) x[idx1, idx2])), as.Date(index(x)))
- colnames(out) <- paste(asset1, asset2, sep=".")
- # detect if estimating cor or cov and set a class
- # (i.e. EWMA_cov or EWMA_cor classes)
- # This will change how we handle getCov or getCor
- # For example, if someone uses EWMA with cor = TRUE, we probably don't want
- # them to be able to call getCov on correlations estimated with EWMA. It would
- # still return the right value based on the index, but it is misleading that
- # they used getCov to return the correlations.
- return(out)
-}
-
-
-x <- rbEWMA(R, training_period=20)[[1]]
-x <- rbEWMA(R[,1])
-x
-covAMATAMGN <- getCov(x, "AMATGLG", "AMGN")
-
-# should have a plot method that takes an EWMA_cov or EWMA_cor object and then
-# use getCov or getCor to extract the time series to plot
-
-
-plot(covAMATAMGN)
-
+# # might need a separate function for univariate time series of returns
+#
+# # estimate covariance or correlation using EWMA for a multivariate data set
+# rbEWMA <- function(R, lambda=0.94, training_period=10, cor=FALSE){
+# # check for training_period must be greater than ncol(R)
+# # check for lambda between 0 and 1
+#
+# # Separate data into a training set and a testing set
+# R_training <- R[1:training_period,]
+# R_testing <- R[(training_period+1):nrow(R),]
+#
+# # calculate a starting covariance matrix
+# cov_start <- cov(R_training)
+# cov_lag <- cov_start
+# tmp_cov <- vector("list", nrow(R_testing))
+#
+# for(i in 1:nrow(R_testing)){
+# # extract R for the ith time step
+# tmpR <- R_testing[i,]
+# tmp_cov[[i]] <- lambda * (t(tmpR) %*% tmpR) + (1 - lambda) * cov_lag
+# # update cov_lag to be tmp_cov from the current period
+# cov_lag <- tmp_cov[[i]]
+# }
+# out <- tmp_cov
+# names(out) <- index(R_testing)
+# if(cor) out <- lapply(out, cov2cor)
+# return(out)
+# }
+#
+#
+#
+# getCov <- function(object, asset1, asset2){
+# # check for
+# 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 {
+# # checks for dimensions
+# idx1 <- asset1
+# idx2 <- asset2
+# }
+# out <- xts(unlist(lapply(x, function(x) x[idx1, idx2])), as.Date(index(x)))
+# colnames(out) <- paste(asset1, asset2, sep=".")
+# # detect if estimating cor or cov and set a class
+# # (i.e. EWMA_cov or EWMA_cor classes)
+# # This will change how we handle getCov or getCor
+# # For example, if someone uses EWMA with cor = TRUE, we probably don't want
+# # them to be able to call getCov on correlations estimated with EWMA. It would
+# # still return the right value based on the index, but it is misleading that
+# # they used getCov to return the correlations.
+# return(out)
+# }
+#
+#
+# x <- rbEWMA(R, training_period=20)[[1]]
+# x <- rbEWMA(R[,1])
+# x
+# covAMATAMGN <- getCov(x, "AMATGLG", "AMGN")
+#
+# # should have a plot method that takes an EWMA_cov or EWMA_cor object and then
+# # use getCov or getCor to extract the time series to plot
+#
+#
+# plot(covAMATAMGN)
+#
More information about the Uwgarp-commits
mailing list