Skip to content

Commit 0e8c3fe

Browse files
committed
Deploying to gh-pages from @ db91be2 🚀
1 parent 3d71c8e commit 0e8c3fe

19 files changed

Lines changed: 172 additions & 155 deletions

articles/Chapter08.html

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

articles/Chapter08.md

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,48 @@ parms.proposal
699699
#> [3,] -0.003188927 0.0002195915 0.0364108173
700700
```
701701

702-
Next we set up the independence Metropolis-Hastings algorithm to
703-
estimate the model parameters.
702+
To set up the independence Metropolis-Hastings algorithm for the Poisson
703+
model, we first write a short program for the MH step to sample the
704+
regression effects.
705+
706+
``` r
707+
sample_beta<- function(y,X,e, b0, B0, qmean, qvar, beta.old){
708+
709+
beta.proposed <- as.vector(mvtnorm::rmvnorm(1, mean = qmean, sigma = qvar))
710+
711+
# Compute log proposal density at proposed and old value
712+
lq_proposed <- mvtnorm::dmvnorm(beta.proposed, mean = qmean, sigma = qvar,
713+
log = TRUE)
714+
lq_old <- mvtnorm::dmvnorm(beta.old, mean = qmean, sigma = qvar,
715+
log = TRUE)
716+
717+
# Compute log prior of proposed and old value
718+
lpri_proposed <- mvtnorm::dmvnorm(beta.proposed, mean = b0, sigma = B0,
719+
log = TRUE)
720+
lpri_old <- mvtnorm::dmvnorm(beta.old, mean = b0, sigma = B0,
721+
log = TRUE)
722+
# Compute log likelihood of proposed and old value
723+
lh_proposed <- dpois(y, e * exp(X %*% beta.proposed), log = TRUE)
724+
lh_old <- dpois(y, e * exp(X %*% beta.old), log = TRUE)
725+
726+
maxlik <- max(lh_old, lh_proposed)
727+
ll <- sum(lh_proposed - maxlik) - sum(lh_old - maxlik)
728+
729+
# Compute acceptance probability and accept or not
730+
log_acc <- min(0, ll + lpri_proposed - lpri_old + lq_old - lq_proposed)
731+
732+
if (log(runif(1)) < log_acc) {
733+
beta <- beta.proposed
734+
acc <- 1
735+
} else {
736+
beta <- beta.old
737+
acc <- 0
738+
}
739+
return(res = list(beta=beta, acc=acc))
740+
}
741+
```
742+
743+
We use this program to sample from the posterior.
704744

705745
``` r
706746
poisson <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar,
@@ -717,43 +757,13 @@ poisson <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar,
717757
beta <- as.vector(mvtnorm::rmvnorm(1, mean = qmean, sigma = qvar))
718758

719759
for (m in seq_len(burnin + M)) {
720-
beta.old <- beta
721-
beta.proposed <- as.vector(mvtnorm::rmvnorm(1, mean = qmean, sigma = qvar))
722-
723-
# Compute log proposal density at proposed and old value
724-
lq_proposed <- mvtnorm::dmvnorm(beta.proposed, mean = qmean, sigma = qvar,
725-
log = TRUE)
726-
lq_old <- mvtnorm::dmvnorm(beta.old, mean = qmean, sigma = qvar,
727-
log = TRUE)
728-
729-
# Compute log prior of proposed and old value
730-
lpri_proposed <- mvtnorm::dmvnorm(beta.proposed, mean = b0, sigma = B0,
731-
log = TRUE)
732-
lpri_old <- mvtnorm::dmvnorm(beta.old, mean = b0, sigma = B0,
733-
log = TRUE)
734-
735-
# Compute log-likelihood of proposed and old value
736-
lh_proposed <- dpois(y, e * exp(X %*% beta.proposed), log = TRUE)
737-
lh_old <- dpois(y, e * exp(X %*% beta.old), log = TRUE)
738-
739-
maxlik <- max(lh_old, lh_proposed)
740-
ll <- sum(lh_proposed - maxlik) - sum(lh_old - maxlik)
741-
742-
# Compute acceptance probability and accept or not
743-
log_acc <- min(0, ll + lpri_proposed - lpri_old + lq_old - lq_proposed)
744-
745-
if (log(runif(1)) < log_acc) {
746-
beta <- beta.proposed
747-
accept <- 1
748-
} else {
749-
beta <- beta.old
750-
accept <- 0
751-
}
752-
753-
# Store the beta draws
760+
beta.draw <- sample_beta(y,X,e, b0, B0, qmean, qvar,beta)
761+
beta<- beta.draw$beta
762+
763+
# Store the beta draws
754764
if (m > burnin) {
755765
beta.post[m - burnin, ] <- beta
756-
acc[m - burnin] <- accept
766+
acc[m - burnin] <- beta.draw $acc
757767
}
758768
}
759769
return(list(beta.post = beta.post, accept = mean(acc)))
@@ -1212,7 +1222,7 @@ qqplot(alpha.prior,res_check_abc$alpha.post, xlab = "Prior",
12121222
abline(a = 0, b = 1)
12131223
```
12141224

1215-
![](Chapter08_files/figure-html/unnamed-chunk-41-1.png)
1225+
![](Chapter08_files/figure-html/unnamed-chunk-42-1.png)
12161226

12171227
We conclude that the sampler is correct.
12181228

@@ -1345,7 +1355,7 @@ qqplot(alpha.prior,res_check_cba$alpha.post, xlab = "Prior",
13451355
abline(a = 0, b = 1)
13461356
```
13471357

1348-
![](Chapter08_files/figure-html/unnamed-chunk-43-1.png)
1358+
![](Chapter08_files/figure-html/unnamed-chunk-44-1.png)
13491359

13501360
### Example 8.11
13511361

@@ -1370,7 +1380,7 @@ qqplot(alpha.prior,res_check_abc$alpha.post, xlab = "Prior",
13701380
abline(a = 0, b = 1)
13711381
```
13721382

1373-
![](Chapter08_files/figure-html/unnamed-chunk-44-1.png)
1383+
![](Chapter08_files/figure-html/unnamed-chunk-45-1.png)
13741384

13751385
and then in the order (c)-(b)-(a)
13761386

@@ -1392,7 +1402,7 @@ qqplot(alpha.prior,res_check_cba$alpha.post, xlab = "Prior",
13921402
abline(a = 0, b = 1)
13931403
```
13941404

1395-
![](Chapter08_files/figure-html/unnamed-chunk-45-1.png)
1405+
![](Chapter08_files/figure-html/unnamed-chunk-46-1.png)
13961406

13971407
## Section 8.3: Beyond i.i.d. Gaussian error distributions
13981408

@@ -1410,7 +1420,7 @@ plot(starsCYG, pch = 19, xlim = c(3, 5), ylim = c(3, 7),
14101420
xlab = "log temperature", ylab = "log light intensity")
14111421
```
14121422

1413-
![](Chapter08_files/figure-html/unnamed-chunk-46-1.png)
1423+
![](Chapter08_files/figure-html/unnamed-chunk-47-1.png)
14141424

14151425
The four giant stars which can also be identified in the scatter plot
14161426
have the following indices in the data set:
@@ -1462,7 +1472,7 @@ lines(xnew, preds_subset[, "lwr"], lty = 2)
14621472
lines(xnew, preds_subset[, "upr"], lty = 2)
14631473
```
14641474

1465-
![](Chapter08_files/figure-html/unnamed-chunk-49-1.png)
1475+
![](Chapter08_files/figure-html/unnamed-chunk-50-1.png)
14661476

14671477
#### Example 8.13: Star cluster data - heteroskedastic regression analysis with known outliers
14681478

@@ -1557,7 +1567,7 @@ lines(xnew, apply(pred_hetero, 1, quantile, 0.025), lty = 2)
15571567
lines(xnew, apply(pred_hetero, 1, quantile, 0.975), lty = 2)
15581568
```
15591569

1560-
![](Chapter08_files/figure-html/unnamed-chunk-54-1.png)
1570+
![](Chapter08_files/figure-html/unnamed-chunk-55-1.png)
15611571

15621572
### Section 8.3.2 Regression analysis with errors following a Gaussian mixture
15631573

@@ -1642,7 +1652,7 @@ lines(xnew, apply(preds_mix_1, 1, quantile, 0.025), lty = 2)
16421652
lines(xnew, apply(preds_mix_1, 1, quantile, 0.975), lty = 2)
16431653
```
16441654

1645-
![](Chapter08_files/figure-html/unnamed-chunk-59-1.png)
1655+
![](Chapter08_files/figure-html/unnamed-chunk-60-1.png)
16461656

16471657
We now assume that the indices of the giant stars are not known. We only
16481658
assume that a two-component mixture is used as weight distribution where
@@ -1722,7 +1732,7 @@ lines(xnew, apply(preds_mix_2, 1, quantile, 0.025), lty = 2)
17221732
lines(xnew, apply(preds_mix_2, 1, quantile, 0.975), lty = 2)
17231733
```
17241734

1725-
![](Chapter08_files/figure-html/unnamed-chunk-62-1.png)
1735+
![](Chapter08_files/figure-html/unnamed-chunk-63-1.png)
17261736

17271737
Finally, we visualize again the mean and the 95%-HPD region together
17281738
with the data points for the three modeling approaches: (1) a
@@ -1748,7 +1758,7 @@ lines(xnew, apply(preds_mix_2, 1, quantile, 0.025), lty = 2)
17481758
lines(xnew, apply(preds_mix_2, 1, quantile, 0.975), lty = 2)
17491759
```
17501760

1751-
![](Chapter08_files/figure-html/unnamed-chunk-63-1.png)
1761+
![](Chapter08_files/figure-html/unnamed-chunk-64-1.png)
17521762

17531763
The plot indicates that all three modeling approaches result in a fit
17541764
that is robust to the outlying observations.
@@ -1827,7 +1837,7 @@ lines(xnew, apply(preds_norm, 1, quantile, 0.975), lty = 3)
18271837
boxplot(ws, col = 2 * (1:ncol(ws) %in% index))
18281838
```
18291839

1830-
![](Chapter08_files/figure-html/unnamed-chunk-66-1.png)
1840+
![](Chapter08_files/figure-html/unnamed-chunk-67-1.png)
18311841

18321842
#### Example 8.16: CHF exchange rate data - Fitting a Student-$t$ with $\nu$ unknown
18331843

@@ -1938,7 +1948,7 @@ selecta <- sample.int(N, 1)
19381948
ts.plot(ws[, selecta], xlab = "Iteration", ylab = bquote(~omega[.(selecta)]))
19391949
```
19401950

1941-
![](Chapter08_files/figure-html/unnamed-chunk-69-1.png)
1951+
![](Chapter08_files/figure-html/unnamed-chunk-70-1.png)
19421952

19431953
``` r
19441954
grid <- seq(0, 20, by = .1)
@@ -1952,7 +1962,7 @@ IF <- M / coda::effectiveSize(nus)
19521962
title(paste0("Empirical ACF (IF: ", round(IF), ")"))
19531963
```
19541964

1955-
![](Chapter08_files/figure-html/unnamed-chunk-70-1.png)
1965+
![](Chapter08_files/figure-html/unnamed-chunk-71-1.png)
19561966

19571967
### Section 8.3.4 Regression analysis with autocorrelated errors
19581968

@@ -1963,7 +1973,7 @@ data(newcars, package = "BayesianLearningCode")
19631973
plot(newcars)
19641974
```
19651975

1966-
![](Chapter08_files/figure-html/unnamed-chunk-71-1.png)
1976+
![](Chapter08_files/figure-html/unnamed-chunk-72-1.png)
19671977

19681978
Seasonal patterns are evident, as is a trend. To model these, we set up
19691979
an appropriate design matrix. Leveraging the fact the the data is a `ts`
@@ -2067,7 +2077,7 @@ plot(tim, rowMeans(resids), type = 'l', main = "Mean residuals", xlab = "Time",
20672077
abline(h = 0, lty = 3)
20682078
```
20692079

2070-
![](Chapter08_files/figure-html/unnamed-chunk-75-1.png)
2080+
![](Chapter08_files/figure-html/unnamed-chunk-76-1.png)
20712081

20722082
Apart from some outliers (the most prominent ones being related to the
20732083
COVID-outbreak), we still see autocorrelation in the residuals. Thus, we
-1.08 KB
Loading
-594 Bytes
Loading
2.2 KB
Loading
33.7 KB
Loading
-19.4 KB
Loading
17.2 KB
Loading
-913 Bytes
Loading
51.2 KB
Loading

0 commit comments

Comments
 (0)