Skip to content

Commit d87685f

Browse files
committed
Deploying to gh-pages from @ 2c2e862 🚀
1 parent 7c749d9 commit d87685f

10 files changed

Lines changed: 227 additions & 9 deletions

File tree

articles/Chapter09.html

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

articles/Chapter09.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abline(h = probs, lty = 3)
3030
mtext(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

3537
We load the data and then plot the pdf and cdf for the predictive
@@ -64,6 +66,8 @@ abline(h = probs, lty = 3)
6466
mtext(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+
6771
We 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

103109
We 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)
232238
legend("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

237245
We use a sampling-based approach to obtain draws from the posterior
@@ -265,6 +273,8 @@ abline(h = probs, lty = 3)
265273
mtext(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

270280
We illustrate the variance of the purely sampling-based estimator versus
@@ -333,6 +343,8 @@ boxplot(pk2, xlab = "k", range = 0, main = "Rao-Blackwellized",
333343
points(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",
506518
abline(v = 1994.75, col = "red")
507519
```
508520

521+
![](Chapter09_files/figure-html/unnamed-chunk-25-1.png)
522+
509523
We see that the prediction intervals after the intervention are much too
510524
wide which again indicates that there is an intervention effect. Note
511525
that 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

586602
Now, 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+
647665
And now we plot the predictions.
648666

649667
``` r
@@ -665,6 +683,8 @@ polygon(pxs, c(quants["5%", 1], quants["95%", ], rev(quants["5%", ])),
665683
abline(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

670690
Let 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)
710730
abline(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)
-5.57 KB
Loading
169 KB
Loading
166 KB
Loading

articles/Chapter10.html

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

articles/Chapter10.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ knitr::kable(cbind(BF, logBF, PrM1, PrM2))
3232
| 403.4287935 | 6 | 0.9975274 | 0.0024726 |
3333
| 1096.6331584 | 7 | 0.9990889 | 0.0009111 |
3434

35-
### Example 10.5: CHF exchange rate data - Testing the null hypothesis $`\mu = 0`$
35+
### Example 10.5: CHF exchange rate data - Testing for zero mean
3636

3737
Before producing Figure 10.1, we begin with a concrete example. We load
3838
the data, compute the required parameters, and evaluate the two marginal
@@ -801,7 +801,7 @@ argmax <- which(probs[5:8, ] == max(probs[5:8, ]), arr.ind = TRUE)
801801
plot(xis, post_xis[argmax[1, 1], argmax[1, 2], ], type = "l", lwd = 2,
802802
xlim = c(0.27, 0.43),
803803
ylim = c(0, 1.17 * max(post_xis[probs[5:8, ] > 0.0001])),
804-
lty = 2, main = "Most probable and BMA posterior", xlab = expression(xi))
804+
lty = 2, main = "MAP and BMA posterior", xlab = expression(xi))
805805
lines(xis, bma_post_xis, lwd = 2)
806806

807807
for (i in seq_len(nrow(weighted_post_xis))) {
@@ -812,7 +812,7 @@ for (i in seq_len(nrow(weighted_post_xis))) {
812812
}
813813
}
814814

815-
legend("topright", c("Most probable posterior", "BMA posterior",
815+
legend("topright", c("MAP posterior", "BMA posterior",
816816
"Weighted components"),
817817
col = 1, lty = c(2, 1, 1), lwd = c(2, 2, 1))
818818
abline(h = 0, lwd = 1.5)
-508 Bytes
Loading

pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ articles:
1313
Chapter09: Chapter09.html
1414
Chapter10: Chapter10.html
1515
Chapter11: Chapter11.html
16-
last_built: 2026-05-20T18:12Z
16+
last_built: 2026-05-20T21:07Z
1717
urls:
1818
reference: https://gregorkastner.github.io/BayesianLearningCode/reference
1919
article: https://gregorkastner.github.io/BayesianLearningCode/articles

search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)