[Uwgarp-commits] r148 - in pkg/GARPFRM: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Mar 29 03:50:21 CET 2014
Author: rossbennett34
Date: 2014-03-29 03:50:15 +0100 (Sat, 29 Mar 2014)
New Revision: 148
Added:
pkg/GARPFRM/man/getEstimate.Rd
Modified:
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/EWMA.R
pkg/GARPFRM/demo/EWMA.R
pkg/GARPFRM/demo/backtest_VaR.R
Log:
Adding support for EWMA to estimate vol for each asset individually for multivariate data
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-03-27 22:58:40 UTC (rev 147)
+++ pkg/GARPFRM/NAMESPACE 2014-03-29 02:50:15 UTC (rev 148)
@@ -23,6 +23,7 @@
export(getBetas)
export(getCor)
export(getCov)
+export(getEstimate)
export(getFit)
export(getSpec)
export(getStatistics)
@@ -52,6 +53,8 @@
S3method(getBetas,capm_uv)
S3method(getCor,mvEWMAcor)
S3method(getCov,mvEWMAcov)
+S3method(getEstimate,EWMA)
+S3method(getEstimate,mvEWMAvol)
S3method(getFit,uvGARCH)
S3method(getSpec,uvGARCH)
S3method(getStatistics,capm_mlm)
@@ -67,3 +70,4 @@
S3method(plot,MonteCarlo)
S3method(print,backtestVaR)
S3method(print,EWMA)
+S3method(print,mvEWMAvol)
Modified: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R 2014-03-27 22:58:40 UTC (rev 147)
+++ pkg/GARPFRM/R/EWMA.R 2014-03-29 02:50:15 UTC (rev 148)
@@ -96,6 +96,17 @@
class <- "uvEWMAvol"
}
+ # recursive EWMA call for multivariate data set
+ # note that we are returning here
+ if(((data == "mv") | (data == "bv")) & (type == "volatility")){
+ out <- vector("list", ncol(R))
+ for(i in 1:length(out)){
+ out[[i]] <- EWMA(R=R[,i], lambda=lambda, initialWindow=initialWindow, n=n, type=type)
+ }
+ class(out) <- c("mvEWMAvol", "EWMA")
+ return(out)
+ }
+
# bivariate covariance estimate
if((data == "bv") & (type == "covariance")){
if(is.null(lambda)){
@@ -148,7 +159,7 @@
out <- structure(list(estimate=est,
model=parameters,
data=data),
- class=c("EWMA", class))
+ class=c(class, "EWMA"))
return(out)
}
@@ -563,6 +574,15 @@
}
}
+#' @method print mvEWMAvol
+#' @S3method print mvEWMAvol
+print.mvEWMAvol <- function(x, ...){
+ for(i in 1:length(x)){
+ print(x[[i]], ...=...)
+ cat("\n*****\n")
+ }
+}
+
# extract the covariance between two assets from an mvEWMAcov object
#' EWMA Covariance
@@ -766,11 +786,51 @@
if(inherits(x, "mvEWMAcor")){
estValues <- getCor(x, assets)
}
+ } else if(inherits(x, "mvEWMAvol")){
+ estValues <- getEstimate(x)
}
- # plot the values
- plot.xts(x=estValues, ...=..., type="l", ylab=type, main=main)
- if(!is.null(legendLoc)){
- legend(legendLoc, legend=c("EWMA Estimate"),
- lty=1, col="black", bty="n", cex=legendCex)
+ if(inherits(x, "mvEWMAvol")){
+ cnames <- colnames(estValues)
+ ylim <- range(na.omit(estValues))
+ plot.xts(estValues[,1], ...=..., ylim=ylim, type="n", ylab="volatility", main=main)
+ for(i in 1:ncol(estValues)){
+ lines(estValues[,i], col=i)
+ }
+ if(!is.null(legendLoc)){
+ legend(legendLoc, legend=cnames,
+ lty=rep(1, ncol(estValues)), col=1:ncol(estValues), bty="n", cex=legendCex)
+ }
+ } else {
+ # plot the values
+ plot.xts(x=estValues, ...=..., type="l", ylab=type, main=main)
+ if(!is.null(legendLoc)){
+ legend(legendLoc, legend=c("EWMA Estimate"),
+ lty=1, col="black", bty="n", cex=legendCex)
+ }
}
}
+
+#' Get Estimated Values
+#'
+#' Get the estimated values from the model
+#'
+#' @param object fitted model (currently only EWMA)
+#' @param /dots passthrough parameters (not currently used)
+#' @return model estimate
+#' @author Ross Bennett
+#' @export
+getEstimate <- function(model, ...){
+ UseMethod("getEstimate")
+}
+
+#' @method getEstimate EWMA
+#' @S3method getEstimate EWMA
+getEstimate.EWMA <- function(object, ...){
+ object$estimate
+}
+
+#' @method getEstimate mvEWMAvol
+#' @S3method getEstimate mvEWMAvol
+getEstimate.mvEWMAvol <- function(object, ...){
+ do.call(cbind, lapply(object, FUN=function(x) x$estimate))
+}
\ No newline at end of file
Modified: pkg/GARPFRM/demo/EWMA.R
===================================================================
--- pkg/GARPFRM/demo/EWMA.R 2014-03-27 22:58:40 UTC (rev 147)
+++ pkg/GARPFRM/demo/EWMA.R 2014-03-29 02:50:15 UTC (rev 148)
@@ -1,37 +1,48 @@
# EWMA Demo
library(GARPFRM)
-data(crsp.short)
+data(crsp_weekly)
# Use the first 5 assets in largecap.ts for the returns data
-R <- largecap.ts[, 1:5]
+R <- largecap_weekly[, 1:5]
+# Estimate volatility via EWMA
+volEst <- EWMA(R[,1], lambda=NULL, initialWindow=52, n=26, type="volatility")
+volEst
+tail(getEstimate(volEst))
+
+# Estimate volatility of each asset
+mvVolEst <- EWMA(R, lambda=NULL, initialWindow=52, n=26, type="volatility")
+mvVolEst
+tail(getEstimate(mvVolEst))
+plot(mvVolEst, legendLoc="topright")
+
# Estimate the covariance matrix via EWMA
-covEst <- EWMA(R, 0.94, 15)
+covEst <- EWMA(R, 0.94, 15, type="covariance")
names(covEst)
-covEst
+covEst$estimate
-# get the covariance between AMAT and CAT
-covAMATCAT <- getCov(covEst, assets=c("AMAT", "CAT"))
+# get the covariance between ORCL and HON
+covORCLHON <- getCov(covEst, assets=c("ORCL", "HON"))
cov13 <- getCov(covEst, assets=c(1, 3))
-all.equal(covAMATCAT, cov13)
+all.equal(covORCLHON, cov13)
-# Plot the covariance estimate between AMAT and CAT
+# Plot the covariance estimate between MSFT and DELL
# Note that we are passing the covEst object created by the EWMA function
-plot(covEst, assets=c("AMAT", "CAT"))
+plot(covEst, assets=c("MSFT", "DELL"))
# specifying a single asset will extract the variance from the EWMA estimate
-varAMAT <- getCov(covEst, assets="AMAT")
+varMSFT <- getCov(covEst, assets="MSFT")
# Estimate the correlation matrix
-corEst <- EWMA(R, 0.94, 25, TRUE)
+corEst <- EWMA(R, 0.94, 25, TRUE, type="correlation")
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)
+# get the correlation between MSFT and DELL
+corMSFTDELL <- getCor(corEst, assets=c("MSFT", "DELL"))
+cor24 <- getCor(corEst, assets=c(2, 5))
+all.equal(corMSFTDELL, cor24)
-# Plot the correlation estimate between AMGN and DD
+# Plot the correlation estimate between ORCL and EMC
# Note that we are passing the covEst object created by the EWMA function
-plot(corEst, assets=c("AMGN", "DD"))
+plot(corEst, assets=c("ORCL", "EMC"))
Modified: pkg/GARPFRM/demo/backtest_VaR.R
===================================================================
--- pkg/GARPFRM/demo/backtest_VaR.R 2014-03-27 22:58:40 UTC (rev 147)
+++ pkg/GARPFRM/demo/backtest_VaR.R 2014-03-29 02:50:15 UTC (rev 148)
@@ -35,4 +35,8 @@
"Boot Historical VaR (0.05)"),
lty=rep(1,3), col=c("black", "blue", "red"), bty="n", cex=0.75)
+# Backtest GARCH Model VaR
+garchModel <- uvGARCH(R[, "MSFT"], armaOrder=c(0,0))
+btVaR.GARCH <- backtestVaR.GARCH(garchModel, p=0.95, refitEvery=5, window=100)
+btVaR.GARCH
Added: pkg/GARPFRM/man/getEstimate.Rd
===================================================================
--- pkg/GARPFRM/man/getEstimate.Rd (rev 0)
+++ pkg/GARPFRM/man/getEstimate.Rd 2014-03-29 02:50:15 UTC (rev 148)
@@ -0,0 +1,21 @@
+\name{getEstimate}
+\alias{getEstimate}
+\title{Get Estimated Values}
+\usage{
+ getEstimate(model, ...)
+}
+\arguments{
+ \item{object}{fitted model (currently only EWMA)}
+
+ \item{/dots}{passthrough parameters (not currently used)}
+}
+\value{
+ model estimate
+}
+\description{
+ Get the estimated values from the model
+}
+\author{
+ Ross Bennett
+}
+
More information about the Uwgarp-commits
mailing list