Chapter 9: Bayesian Predictive Analysis
Chapter09.RmdSection 9.1
Example 9.1: Road Safety Data
We load the data and extract the observations for the senior people in Linz. We then plot the pdf and cdf for the predictive distribution which corresponds under the flat prior to
data("accidents", package = "BayesianLearningCode")
y <- accidents[, "seniors_accidents"]
aN <- sum(y) + 1
bN <- length(y)
mu <- aN / bN
yf <- 0:20
plot(yf, dnbinom(yf, size = aN, mu = mu),
type = "h", xlab = "", ylab = "")
plot(yf, pnbinom(yf, size = aN, mu = mu),
type = "h", xlab = "", ylab = "")
probs <- c(0.025, 0.975)
abline(h = probs)
mtext(probs, side = 2, at = probs, adj = c(0, 1), cex = .8, col = "dimgrey")
We inspect the parameters of the negative binomial distributation and verify that a 95% predictive interval is given by [1, 9] using the cdf.
Example 9.2: Exchange Rate Data
We load the data and then plot the pdf and cdf for the predictive distribution which corresponds under the improper prior to
par(mfrow = c(1, 2))
library("BayesianLearningCode")
data("exrates", package = "stochvol")
y <- 100 * diff(log(exrates$USD / exrates$CHF))
ybar <- mean(y)
N <- length(y)
b0 <- 0
N0 <- 0
c0 <- -1/2
C0 <- 0
BN <- 1 / (N + N0)
bN <- BN * (N * ybar + N0 * b0)
cN <- c0 + N/2
CN <- C0 + 1/2 * sum((y - ybar)^2) + N0 * N / (2 * (N0 + N)) * (b0 - ybar)^2
x <- seq(-5, 5, length.out = 200)
scale <- CN / cN * (BN + 1)
plot(x, dstudt(x, location = bN, scale = scale, df = 2 * cN),
type = "l", xlab = "", ylab = "")
plot(x, pstudt(x, location = bN, scale = scale, df = 2 * cN),
type = "l", xlab = "", ylab = "")
probs <- c(0.025, 0.975)
abline(h = probs)
mtext(probs, side = 2, at = probs, adj = c(0, 1), cex = .8, col = "dimgrey")
We inspect the parameters of the negative binomial distributation and determine the 95% predictive interval using the quantile function.