############################################################################################
#
# STOCHASTIC VOLATILITY MODEL
#
############################################################################################
#
# HEDIBERT FREITAS LOPES
# Associate Professor of Econometrics and Statistics
# The University of Chicago Booth School of Business
# 5807 South Woodlawn Avenue
# Chicago, Illinois, 60637
# Email : hlopes@ChicagoGSB.edu
# URL: http://faculty.chicagobooth.edu/hedibert.lopes/research/
#
############################################################################################
rm(list=ls())
source("sv-ar1-routines.R")
# Simulating the data
# -------------------
set.seed(1243)
mu = -0.00645
phi = 0.99
tau2 = 0.15^2
tau = sqrt(tau2)
h0 = 0.0
n = 500
h = rep(0,n)
h[1] = rnorm(1,mu+phi*h0,tau)
for (t in 2:n)
h[t] = rnorm(1,mu+phi*h[t-1],tau)
vol = exp(h)
sd = sqrt(vol)
y = rnorm(n,0,sd)
# Prior hyperparameters
# ---------------------
t
#################################################################################################
# Defining the mixture of 7 normals that approximate the log-chi-square with one degree of freedom
# ------------------------------------------------------------------------------------------------
set.seed(1576)
ms = c(-11.40039,-5.24321,-9.83726,1.50746,-0.65098,0.52478,-2.35859)
s2s = c( 5.79596, 2.61369, 5.17950,0.16735, 0.64009,0.34023, 1.26261)
qs = c( 0.00730, 0.10556, 0.00002,0.04395, 0.34001,0.24566, 0.25750)
mm = sum(qs*ms)
vv = sum(qs*(s2s+ms^2))-(mm)^2
MM = 10000
x = seq(-20,10,length=MM)
den = exp(-(x)/2)*exp(-0.5*exp(x))*exp(x)/sqrt(2*pi)
mix = rep(0,MM)
for (i in 1:MM)
mix = sum(qs*dnorm(x,ms,sqrt(s2s)))
norm = dnorm(x,mm,sqrt(vv))
# Initial values and specific MCMC setup
# --------------------------------------
y1 = log(y^2)
h0 = h0tr
h = htr
mu = ptr[1]
phi = ptr[2]
tau2 = ptr[3]
vh = 0.1
set.seed(12578)
hs = NULL
ps = NULL
for (iter in 1:(M0+M)){
h = svu(y1,h,mu,phi,tau2,m0,C0)
var = 1/(iC0+phi^2/tau2)
mean = var*(iC0m0+phi*(h[1]-mu)/tau2)
h0 = rnorm(1,mean,sqrt(var))
X = cbind(1,c(h0,h[1:(n-1)]))
par = fixedpar(h[1:n],X,theta0,iV0,nu0,s02)
mu = par[1]
phi = par[2]
tau2 = par[3]
ps = rbind(ps,par)
hs = rbind(hs,h)
}
hs.mix = hs
ps.mix = ps
vol = exp(hs[(M0+1):(M0+M),])
mvol = apply(vol,2,median)
lvol = apply(vol,2,quant05)
uvol = apply(vol,2,quant95)
names = c("mu","phi","tau2")
pdf(file="par-mixture.pdf",width=10,height=7)
par(mfrow=c(3,3))
for (i in 1:3){
plot(ps[(M0+1):(M0+M),i],type="l",xlab="iteration",ylab="",main=names)
acf(ps[(M0+1):(M0+M),i],main="")
hist(ps[(M0+1):(M0+M),i],xlab="",ylab="",main="")
}
dev.off()