@@ -30,6 +30,8 @@ abline(h = probs, lty = 3)
3030mtext(probs , side = 2 , at = probs , adj = c(0 , 1 ), cex = .8 , col = " dimgrey" )
3131```
3232
33+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-3-1.png )
34+
3335### Example 9.2: CHF exchange rate data - single predictions
3436
3537We load the data and then plot the pdf and cdf for the predictive
@@ -64,6 +66,8 @@ abline(h = probs, lty = 3)
6466mtext(probs , side = 2 , at = probs , adj = c(0 , 1 ), cex = .8 , col = " dimgrey" )
6567```
6668
69+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-4-1.png )
70+
6771We inspect the parameters of the Student-t distribution.
6872
6973``` r
@@ -98,6 +102,8 @@ for (i in seq_along(Ns)) {
98102}
99103```
100104
105+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-6-1.png )
106+
101107### Example 9.4: Road safety data - posterior predictive credible interval
102108
103109We verify that a 95% posterior predictive interval is given by \[ 1, 9\]
@@ -232,6 +238,8 @@ abline(h = q_t, col = 2, lty = 2, lwd = 1.5)
232238legend(" topleft" , c(" Normal" , " Student t" ), lty = 1 : 2 , col = c(4 , 2 ), lwd = 1.5 )
233239```
234240
241+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-14-1.png )
242+
235243### Example 9.9: Road safety data - sampling-based prediction
236244
237245We use a sampling-based approach to obtain draws from the posterior
@@ -265,6 +273,8 @@ abline(h = probs, lty = 3)
265273mtext(probs , side = 2 , at = probs , adj = c(0 , 1 ), cex = .8 , col = " dimgrey" )
266274```
267275
276+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-16-1.png )
277+
268278### Example 9.11: Predicting the probability of future successes
269279
270280We illustrate the variance of the purely sampling-based estimator versus
@@ -333,6 +343,8 @@ boxplot(pk2, xlab = "k", range = 0, main = "Rao-Blackwellized",
333343points(pk3 , col = 3 , cex = 1.5 , pch = 16 )
334344```
335345
346+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-20-1.png )
347+
336348## Section 9.4 Posterior Predictive Distributions in Regression Analysis
337349
338350### Example 9.12: Road Safety Data; potential outcome analysis
@@ -506,6 +518,8 @@ matplot(as.vector(time(e.pred)), pred.int, col = "blue", type = "l",
506518abline(v = 1994.75 , col = " red" )
507519```
508520
521+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-25-1.png )
522+
509523We see that the prediction intervals after the intervention are much too
510524wide which again indicates that there is an intervention effect. Note
511525that whereas the risk for a child to be killed or seriously injured in
@@ -581,6 +595,8 @@ for (p in 2:4) {
581595}
582596```
583597
598+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-28-1.png )
599+
584600### Example 9.15: US GDP data - multi-step forecasting
585601
586602Now, we want to “sample the future” up to 12 steps ahead for $` p = 2 ` $.
@@ -644,6 +660,8 @@ for (i in 1:4) {
644660}
645661```
646662
663+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-30-1.png )
664+
647665And now we plot the predictions.
648666
649667``` r
@@ -665,6 +683,8 @@ polygon(pxs, c(quants["5%", 1], quants["95%", ], rev(quants["5%", ])),
665683abline(h = 0 , lty = 3 )
666684```
667685
686+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-31-1.png )
687+
668688### Example 9.16: US GDP data - forecasting non-linear functionals
669689
670690Let us compute the probability of seeing negative growth rates a least
@@ -709,3 +729,107 @@ polygon(pxs, c(quants["5%", 1], quants["95%", ], rev(quants["5%", ])),
709729 col = rgb(1 , 0 , 0 , .2 ), border = NA )
710730abline(h = 0 , lty = 3 )
711731```
732+
733+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-34-1.png )
734+
735+ ### Section 2.5.3: Forecasting Volatility via GARCH(1,1) Models
736+
737+ We start by defining the parameter transformation.
738+
739+ ``` r
740+
741+ unrestricted2restricted <- function (theta ) {
742+ alpha0 <- exp(theta [1 ])
743+ exptheta2 <- exp(theta [2 ])
744+ exptheta3 <- exp(theta [3 ])
745+ denom <- 1 + exptheta2 + exptheta3
746+ alpha1 <- exptheta2 / denom
747+ gamma1 <- exptheta3 / denom
748+ c(alpha0 , alpha1 , gamma1 )
749+ }
750+ ```
751+
752+ Next, we define the log likelihood function and the log posterior.
753+
754+ ``` r
755+
756+ loglik <- function (y , theta ) {
757+ pars <- unrestricted2restricted(theta )
758+ sigma2 <- rep(NA_real_ , length(y ))
759+
760+ # initialize at unconditional variance
761+ sigma2 [1 ] <- pars [1 ] / (1 - pars [2 ] - pars [3 ])
762+
763+ for (i in 2 : length(y )) {
764+ sigma2 [i ] <- pars [1 ] + pars [2 ] * y [i - 1 ]^ 2 + pars [3 ] * sigma2 [i - 1 ]
765+ }
766+
767+ - 0.5 * log(2 * pi ) * length(y ) - sum(log(sigma2 ) + y ^ 2 / sigma2 )
768+ }
769+
770+ logpost <- function (y , theta , priormeans = rep(0 , 3 ), priorsds = rep(10 , 3 )) {
771+ loglik(y , theta ) +
772+ dnorm(theta [1 ], priormeans [1 ], priorsds [1 ], log = TRUE ) +
773+ dnorm(theta [2 ], priormeans [2 ], priorsds [2 ], log = TRUE ) +
774+ dnorm(theta [3 ], priormeans [3 ], priorsds [3 ], log = TRUE )
775+ }
776+ ```
777+
778+ Finally, we define our random walk MH posterior sampler.
779+
780+ ``` r
781+
782+ sampler <- function (y , M = 20000L , nburn = 5000L , propsds = c(0.1 , 0.1 , 0.1 )) {
783+ # allocate storage
784+ thetas <- matrix (NA_real_ , M , 3 )
785+
786+ # starting values
787+ theta <- rep(0 , 3 )
788+ currentlogpost <- logpost(y , theta )
789+
790+ naccepts <- 0L
791+ for (m in seq_len(nburn + M )) {
792+ prop <- rnorm(3 , theta , propsds )
793+ proplogpost <- logpost(y , prop )
794+ logR <- proplogpost - currentlogpost
795+
796+ if (log(runif(1 )) < logR ) {
797+ theta <- prop
798+ currentlogpost <- proplogpost
799+ if (m > nburn ) naccepts <- naccepts + 1L
800+ }
801+
802+ if (m > nburn ) thetas [m - nburn , ] <- theta
803+ }
804+ thetas
805+ }
806+ ```
807+
808+ We are now ready to apply the sampler to our EUR-CHF exchange rate
809+ percentage log returns. After running the sampler, we need to transform
810+ back to our original parameters.
811+
812+ ``` r
813+
814+ data(" exrates" , package = " stochvol" )
815+ y <- 100 * diff(log(exrates $ USD / exrates $ CHF ))
816+
817+ # Run the sampler
818+ thetas <- sampler(y )
819+
820+ # Transform back to the usual parameterization
821+ draws <- t(apply(thetas , 1 , unrestricted2restricted ))
822+ colnames(draws ) <- c(" alpha0" , " alpha1" , " gamma1" )
823+
824+ # Visually check convergence
825+ plot.ts(thetas )
826+ ```
827+
828+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-38-1.png )
829+
830+ ``` r
831+
832+ plot.ts(draws )
833+ ```
834+
835+ ![ ] ( Chapter09_files/figure-html/unnamed-chunk-38-2.png )
0 commit comments