Professional Documents
Culture Documents
PROGRAM
PROGRAM
r
#
# R examples for lectures on multivariate GARCH models
#
# Eric Zivot
# May 8th, 2012
# update history
# load libraries
library(PerformanceAnalytics)
library(quantmod)
library(rugarch)
library(car)
library(FinTS)
library(rmgarch)
options(digits=4)
computer = "work"
if (computer == "home") {
setwd("C:\\Users\\ezivot\\Dropbox\\econ589\\R\\")
}
if (computer == "work") {
setwd("C:\\Users\\ezivot.SOCIOLOGY\\Dropbox\\econ589\\R\\")
}
source("covEWMA.r")
# download data
symbol.vec = c("MSFT", "^GSPC")
getSymbols(symbol.vec, from ="2000-01-03", to = "2012-04-03")
colnames(MSFT)
start(MSFT)
end(MSFT)
# plot prices
plot(MSFT)
plot(GSPC)
# plot returns
plot(MSFT.ret)
plot(GSPC.ret)
# scatterplot of returns
plot( coredata(GSPC.ret), coredata(MSFT.ret), xlab="GSPC", ylab="MSFT",
type="p", pch=16, lwd=2, col="blue")
abline(h=0,v=0)
#
# compute rolling correlations
#
# chart.RollingCorrelation(MSFT.ret, GSPC.ret, width=20)
cor.fun = function(x){
cor(x)[1,2]
}
cov.fun = function(x){
cov(x)[1,2]
}
#
# calculate EWMA covariances and correlations
#
lambda <- 0.94
cov.ewma <- covEWMA(as.data.frame(MSFT.GSPC.ret), lambda=lambda)
#
# DCC estimation
#
# plot method
plot(dcc.fit)
# Make a plot selection (or 0 to exit):
#
# 1: Conditional Mean (vs Realized Returns)
# 2: Conditional Sigma (vs Realized Absolute Returns)
# 3: Conditional Covariance
# 4: Conditional Correlation
# 5: EW Portfolio Plot with conditional density VaR limits
# conditional correlation
plot(dcc.fit, which=4)
#
# forecasting conditional volatility and correlations
#
# show forecasts
dcc.fcst
# plot forecasts
4