forked from nickpoison/Stochastic-Volatility-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_simulation_figure_1.r
56 lines (50 loc) · 1.62 KB
/
run_simulation_figure_1.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(astsa)
#
set.seed(1989)
num = 1000
mu = 0
beta = 1
Vsd = 1
phi1 = 0.99 # Model I
sig1 = 0.15 # Model I
phi2 = 0.95 # Model II
sig2 = 0.35 # Model II
x1 = arima.sim(list(order=c(1,0,0), ar=phi1), sd=sig1, n=num) + mu
y1 = beta*exp(x1/2)*rnorm(num,0,Vsd)
x2 = arima.sim(list(order=c(1,0,0), ar=phi2), sd=sig2, n=num) + mu
y2 = beta*exp(x2/2)*rnorm(num,0,Vsd)
y1 = as.vector(y1)
y2 = as.vector(y2)
L = min(y1,y2)
U = max(y1,y2)
SVacf = function(phi,sig,h){
# returns the model acf of the squared returns
sigx2 = sig^2/(1-phi^2)
numer = exp(sigx2*phi^h)-1
denom = 3*exp(sigx2)-1
return(numer/denom)
}
# ACFs
u1 = SVacf(phi1, sig1, 1:30)
u2 = SVacf(phi2, sig2, 1:30)
##
culera = c(rgb(.85,.30,.12,.6), rgb(.12,.65,.85,.6))
culer = c(rgb(.85,.30,.12), rgb(.12,.65,.85))
dev.new(width=9, height=6)
layout(matrix(c(1,2,3, 1,2,4), nrow=3), heights=c(1,1,1.25))
tsplot(y1, col=culera[1], lwd=2, ylab='%', main='Series A', col.main=culer[1])
tsplot(y2, col=culera[2], lwd=2, ylab='%', main='Series B', col.main=culer[2])
acf1(y1^2, 30, main='', ylim=c(0,.6), col=culer[1], lwd=4)
title('Squared Series A', col.main=culer[1])
lines(u1, lwd=2)
lines(u2, lwd=2, lty=2)
op <- par(family = "serif")
legend('topright', lty = 1:2, legend=c('Model I', 'Model II'), lwd=2, cex=1.1, bg='white')
par(op)
acf1(y2^2, 30, main='', ylim=c(0,.6), col=culer[2], lwd=4)
title('Squared Series B', col.main=culer[2])
lines(u1, lwd=2)
lines(u2, lwd=2, lty=2)
op <- par(family = "serif")
legend('topright', lty = 1:2, legend=c('Model I', 'Model II'), cex=1.1, bg='white')
par(op)