[Uwgarp-commits] r58 - in pkg/GARPFRM: . R man sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Feb 2 19:37:20 CET 2014
Author: tfillebeen
Date: 2014-02-02 19:37:20 +0100 (Sun, 02 Feb 2014)
New Revision: 58
Added:
pkg/GARPFRM/R/EWMA.R
pkg/GARPFRM/sandbox/test_EWMA_GARCH.R
Modified:
pkg/GARPFRM/DESCRIPTION
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/capm.R
pkg/GARPFRM/man/CAPM.Rd
pkg/GARPFRM/man/getAlphas.Rd
pkg/GARPFRM/man/getBetas.Rd
pkg/GARPFRM/man/getStatistics.Rd
pkg/GARPFRM/man/hypTest.Rd
pkg/GARPFRM/man/monteCarlo.Rd
Log:
EWMA + start of GARCH(1,1) and forecasting desired functions
Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/DESCRIPTION 2014-02-02 18:37:20 UTC (rev 58)
@@ -13,6 +13,3 @@
Suggests:
quadprog
License: GPL
-Collate:
- 'capm.R'
- 'monte_carlo.R'
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/NAMESPACE 2014-02-02 18:37:20 UTC (rev 58)
@@ -1,6 +1,13 @@
-S3method(hypTest,capm_mv)
+S3method(getAlphas,capm_mlm)
+S3method(getAlphas,capm_uv)
+S3method(getBetas,capm_mlm)
+S3method(getBetas,capm_uv)
+S3method(getStatistics,capm_mlm)
+S3method(getStatistics,capm_uv)
+S3method(hypTest,capm_mlm)
S3method(hypTest,capm_uv)
export(CAPM)
+export(EWMA)
export(chartSML)
export(getAlphas)
export(getBetas)
@@ -8,11 +15,3 @@
export(hypTest)
export(plot.capm_mv)
export(plot.capm_uv)
-S3method(getAlphas,capm_mlm)
-S3method(getAlphas,capm_uv)
-S3method(getBetas,capm_mlm)
-S3method(getBetas,capm_uv)
-S3method(getStatistics,capm_mlm)
-S3method(getStatistics,capm_uv)
-S3method(hypTest,capm_mlm)
-S3method(hypTest,capm_uv)
Added: pkg/GARPFRM/R/EWMA.R
===================================================================
--- pkg/GARPFRM/R/EWMA.R (rev 0)
+++ pkg/GARPFRM/R/EWMA.R 2014-02-02 18:37:20 UTC (rev 58)
@@ -0,0 +1,40 @@
+#' Exponential Weight Moving Average (EWMA)
+#'
+#' Description of EWMA. The function handles UV and MLE objects and returns either cov or cor.
+#'
+#' @param object EWMA (either cov or corr, default = cov)
+#' @export
+EWMA <- function(object, lambda=0.96, cor=FALSE) {
+ if ((is.data.frame(object)) & (lambda<1 || lambda > 0)){
+ object.names = colnames(object)
+ t.object = nrow(object)
+ k.object = ncol(object)
+ object = as.matrix(object)
+ t.names = rownames(object)
+
+ covEWMA = array(,c(t.object,k.object,k.object))
+ # Note it is unconditional cov
+ cov.f = var(object)
+ FF = (object[1,]- mean(object)) %*% t(object[1,]- mean(object))
+ covEWMA[1,,] = (1-lambda)*FF + lambda*cov.f
+ for (i in 2:t.object) {
+ FF = (object[i,]- mean(object)) %*% t(object[i,]- mean(object))
+ covEWMA[i,,] = (1-lambda)*FF + lambda*covEWMA[(i-1),,]
+ }
+
+ } else {
+ stop("object handled as data.frame class || exp-decay lambda must be ]0:1[")
+ }
+
+ dimnames(covEWMA) = list(t.names, object.names, object.names)
+
+ if(cor) {
+ corEWMA = covEWMA
+ for (i in 1:dim(corEWMA)[1]) {
+ corEWMA[i, , ] = cov2cor(covEWMA[i, ,])
+ }
+ return(corEWMA)
+ } else{
+ return(covEWMA)
+ }
+ }
Modified: pkg/GARPFRM/R/capm.R
===================================================================
--- pkg/GARPFRM/R/capm.R 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/R/capm.R 2014-02-02 18:37:20 UTC (rev 58)
@@ -121,7 +121,7 @@
if(!inherits(object, "capm_uv")) stop("object must be of class capm_uv")
tmp_sm <- summary.lm(object)
# gets t-value, and p-value of model
- result = coef(tmp_sm)[,c(2:4)]
+ result = coef(tmp_sm)[,c(1:4)]
rownames(result) = cbind(c(paste("alpha.", colnames(object$y_data))),c(paste("beta. ", colnames(object$y_data))))
return(result)
}
@@ -193,7 +193,7 @@
#' CAPM hypTest
#'
-#' Description of CAPM beta/alpha test
+#' Description of CAPM beta/alpha hypothesis test
#'
#' @param object a capm object created by \code{\link{CAPM}}
#' @export
Modified: pkg/GARPFRM/man/CAPM.Rd
===================================================================
--- pkg/GARPFRM/man/CAPM.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/CAPM.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -2,7 +2,7 @@
\alias{CAPM}
\title{Capital Asset Pricing Model}
\usage{
- CAPM(R, Rmkt)
+CAPM(R, Rmkt)
}
\arguments{
\item{R}{asset returns}
@@ -10,8 +10,8 @@
\item{Rmkt}{market returns}
}
\description{
- Description of CAPM Retrieves alphas, betas, as well as
- pvalue and tstats
+Description of CAPM Retrieves alphas, betas, as well as
+pvalue and tstats
}
\examples{
data(crsp.short)
Modified: pkg/GARPFRM/man/getAlphas.Rd
===================================================================
--- pkg/GARPFRM/man/getAlphas.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/getAlphas.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -2,13 +2,13 @@
\alias{getAlphas}
\title{CAPM alphas}
\usage{
- getAlphas(object)
+getAlphas(object)
}
\arguments{
\item{object}{a capm object created by
\code{\link{CAPM}}}
}
\description{
- Description of CAPM alphas
+Description of CAPM alphas
}
Modified: pkg/GARPFRM/man/getBetas.Rd
===================================================================
--- pkg/GARPFRM/man/getBetas.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/getBetas.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -2,13 +2,13 @@
\alias{getBetas}
\title{CAPM betas}
\usage{
- getBetas(object)
+getBetas(object)
}
\arguments{
\item{object}{a capm object created by
\code{\link{CAPM}}}
}
\description{
- Description of CAPM betas
+Description of CAPM betas
}
Modified: pkg/GARPFRM/man/getStatistics.Rd
===================================================================
--- pkg/GARPFRM/man/getStatistics.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/getStatistics.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -2,14 +2,14 @@
\alias{getStatistics}
\title{CAPM statistics}
\usage{
- getStatistics(object)
+getStatistics(object)
}
\arguments{
\item{object}{a capm object created by
\code{\link{CAPM}}}
}
\description{
- Description of CAPM statistics (standard error, t-values,
- and p-values)
+Description of CAPM statistics (standard error, t-values,
+and p-values)
}
Modified: pkg/GARPFRM/man/hypTest.Rd
===================================================================
--- pkg/GARPFRM/man/hypTest.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/hypTest.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -1,14 +1,14 @@
\name{hypTest}
\alias{hypTest}
-\title{CAPM hypthTest}
+\title{CAPM hypTest}
\usage{
- hypTest(object, CI)
+hypTest(object, CI)
}
\arguments{
\item{object}{a capm object created by
\code{\link{CAPM}}}
}
\description{
- Description of CAPM beta/alpha test
+Description of CAPM beta/alpha hypothesis test
}
Modified: pkg/GARPFRM/man/monteCarlo.Rd
===================================================================
--- pkg/GARPFRM/man/monteCarlo.Rd 2014-02-02 06:15:23 UTC (rev 57)
+++ pkg/GARPFRM/man/monteCarlo.Rd 2014-02-02 18:37:20 UTC (rev 58)
@@ -2,8 +2,8 @@
\alias{monteCarlo}
\title{Monte Carlo Price Path Simulation}
\usage{
- monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
- starting_value = 100, log = TRUE)
+monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
+ starting_value = 100, log = TRUE)
}
\arguments{
\item{mu}{annualized expected return}
@@ -22,10 +22,10 @@
rather than S; where S is the price of the asset.}
}
\value{
- matrix of Monte Carlo simulated price paths
+matrix of Monte Carlo simulated price paths
}
\description{
- Description for Monte Carlo. Geometric brownian motion.
- mu and sigma are assumed constant
+Description for Monte Carlo. Geometric brownian motion. mu
+and sigma are assumed constant
}
Added: pkg/GARPFRM/sandbox/test_EWMA_GARCH.R
===================================================================
--- pkg/GARPFRM/sandbox/test_EWMA_GARCH.R (rev 0)
+++ pkg/GARPFRM/sandbox/test_EWMA_GARCH.R 2014-02-02 18:37:20 UTC (rev 58)
@@ -0,0 +1,173 @@
+library(PerformanceAnalytics)
+library(rugarch)
+library(GARPFRM)
+library(rmgarch)
+data(managers)
+options(digits=4)
+
+# Remember: log-returns for GARCH analysis
+temp_1 = managers[,3]
+temp_2 = managers[,8]
+
+# create combined data series
+temp = merge(temp_1,temp_2)
+
+# scatterplot of returns
+plot(coredata(temp_2), coredata(temp_2), xlab=colnames(temp_1), ylab=colnames(temp_2),
+ main ="Scatterplot of Returns")
+abline(h=0,v=0,lty=3)
+
+# compute rolling cor
+# chart.RollingCorrelation( temp_1, temp_2, width=20)
+
+cor.fun = function(x){
+ cor(x)[1,2]
+}
+
+cov.fun = function(x){
+ cov(x)[1,2]
+}
+
+roll.cov = rollapply(as.zoo(temp), FUN=cov.fun, width=20,
+ by.column=FALSE, align="right")
+roll.cor = rollapply(as.zoo(temp), FUN=cor.fun, width=20,
+ by.column=FALSE, align="right")
+par(mfrow=c(2,1))
+# First Rolling Cov
+plot(roll.cov, main="20-Day Rolling Cov",
+ ylab="covariance", lwd=3, col="blue")
+grid()
+abline(h=cov(temp)[1,2], lwd=3, col="red")
+
+# Second Rolling Cor
+plot(roll.cor, main="20-Day Rolling Cor",
+ ylab="correlation", lwd=3, col="blue")
+grid()
+abline(h=cor(temp)[1,2], lwd=3, col="red")
+par(mfrow=c(1,1))
+
+# Calculate EWMA cov and cor, applying default lambda - 0.96
+covEwma <- EWMA(as.data.frame(temp))
+
+# Extract conditional var and cor
+assetCondCov <- covEwma[,2,1];
+t <- length(covEwma[,1,1]);
+assetCondCov<- rep(0,t);
+for (i in 1:t) {
+ assetCondCov[i]<- cov2cor(covEwma[i,,])[1,2];
+}
+# Plots
+par(mfrow=c(2,1))
+plot(x=time(as.zoo(temp)), y=assetCondCov,
+ type="l", xlab="Time", ylab="Covariance", lwd=2, col="blue",
+ main="EWMA Covariance");
+grid()
+abline(h=cov(temp)[1,2], lwd=2, col="red")
+plot(x=time(as.zoo(temp)), y=assetCondCov,
+ type="l", xlab="Time", ylab="Correlation", lwd=2, col="blue",
+ main="EWMA Correlation");
+grid()
+abline(h=cor(temp)[1,2], lwd=2, col="red")
+par(mfrow=c(1,1))
+
+
+# Compute rolling cov and cor using new window
+roll.cov = rollapply(as.zoo(temp), FUN=cov.fun, width=120,
+ by.column=FALSE, align="right")
+roll.cor = rollapply(as.zoo(temp), FUN=cor.fun, width=120,
+ by.column=FALSE, align="right")
+par(mfrow=c(2,1))
+plot(roll.cov, main="120-Day rolling cov",
+ ylab="covariance", lwd=2, col="blue")
+grid()
+abline(h=cov(temp)[1,2], lwd=2, col="red")
+plot(roll.cor, main="120-Day rolling cor",
+ ylab="correlation", lwd=2, col="blue")
+grid()
+abline(h=cor(temp)[1,2], lwd=2, col="red")
+par(mfrow=c(1,1))
+
+
+# compute EWMA cov and cor for longer half-life of
+halfLife = log(0.5)/log(0.94) + 5
+lambda = exp(log(0.5)/halfLife)
+covEwma <- EWMA(as.data.frame(temp), lambda)
+
+
+# Extract conditional var and cor
+assetCondCov <- covEwma[,2,1]
+t <- length(covEwma[,1,1])
+assetCondCov<- rep(0,t)
+for (i in 1:t) {
+ assetCondCov[i]<- cov2cor(covEwma[i,,])[1,2]
+}
+# Plots
+par(mfrow=c(2,1))
+plot(x=time(as.zoo(temp)), y=assetCondCov,
+ type="l", xlab="Time", ylab="Cov", lwd=2, col="blue",
+ main="EWMA Cov")
+grid()
+abline(h=cov(temp)[1,2], lwd=2, col="red")
+plot(x=time(as.zoo(temp)), y=assetCondCov,
+ type="l", xlab="Time", ylab="Cor", lwd=2, col="blue",
+ main="EWMA Cor")
+grid()
+abline(h=cor(temp)[1,2], lwd=2, col="red")
+par(mfrow=c(1,1))
+
+# Dynamic Conditional Cor
+# UV N~GARCH(1,1) for each series
+garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
+ variance.model = list(garchOrder = c(1,1),
+ model = "sGARCH"),
+ distribution.model = "norm")
+
+# DCC specification: GARCH(1,1) for conditional cor
+dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
+ dccOrder = c(1,1), distribution = "mvnorm")
+dcc.garch11.spec
+
+dcc.fit = dccfit(dcc.garch11.spec, data = temp)
+class(dcc.fit)
+slotNames(dcc.fit)
+names(dcc.fit at mfit)
+names(dcc.fit at model)
+
+# many extractor functions - see help on DCCfit object
+# coef, likelihood, rshape, rskew, fitted, sigma,
+# residuals, plot, infocriteria, rcor, rcov
+# show, nisurface
+
+# show dcc fit
+dcc.fit
+
+# plot method
+plot(dcc.fit)
+# Make a plot selection (or 0 to exit):
+# Where 1: Conditional Mean (vs Realized Returns)
+# Where 2: Conditional Sigma (vs Realized Absolute Returns)
+# Where 3: Conditional Covariance
+# Where 4: Conditional Correlation
+# Where 5: EW Portfolio Plot with conditional density VaR limits
+
+# conditional sd of each series
+plot(dcc.fit, which=2)
+
+# conditional cor
+plot(dcc.fit, which=4)
+
+# extracting cor series
+ts.plot(rcor(dcc.fit)[1,2,])
+
+# Forecasting conditional vol and cor
+dcc.fcst = dccforecast(dcc.fit, n.ahead=100)
+class(dcc.fcst)
+slotNames(dcc.fcst)
+class(dcc.fcst at mforecast)
+names(dcc.fcst at mforecast)
+
+# many method functions - see help on DCCforecast class
+# rshape, rskew, fitted, sigma, plot, rcor, rcov, show
+
+# show forecasts
+dcc.fcst
\ No newline at end of file
More information about the Uwgarp-commits
mailing list