forked from cheerzzh/R_for_Quantitative_Finance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stochvol-package-introduction.R
52 lines (38 loc) · 1.35 KB
/
stochvol-package-introduction.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
set.seed(123)
library("stochvol")
data("exrates")
ret <- logret(exrates$USD, demean =TRUE)
par(mfrow=c(2,1), mar = c(1.9,1.9,1.9,0.5),mgp = c(2,0.6,0))
plot(exrates$date, exrates$USD, type = "l",main = "Price of 1 EUR in USD")
plot(exrates$date[-1],ret, type="l",main="Demeaned log-returns")
# built-in data generator: svsim
# produce realizations of an sv process
# return a object of svsim class, has its own print, summary, plot methods
sim <- svsim(500, mu=-0.9, phi = 0.99, sigma = 0.1)
par(mfrow=c(2,1))
plot(sim)
# svsample()
# a R wrapper for sampler code in C
res <- svsample(ret, priormu = c(-10,1), priorphi = c(20,1.1), priorsigma = 0.1)
# assessing the output and displaying the results
summary(res, showlatent = FALSE)
# by default, display 5%, 50%, 95% quantiles
volplot(res, forecast = 100, dates = exrates$date[-1])
# display different posterior quantiles
res <- updatesummary(res, quantiles = c(0.01,0.1,0.5,0.9,0.99))
volplot(res, forecast = 100, dates = exrates$date[-1])
# paratraceplot()
# trace plots for parameters, burn-in has already been discarded
par(mfrow=c(3,1))
paratraceplot(res)
# paradensplot()
# kernal density estimate for parameters
# with posterior and prior density
par(mfrow=c(1,3))
paradensplot(res)
# combine all plot
plot(res)
# residuals
myresid <- resid(res)
# by default, mean-standardized residuals
plot(myresid,ret)