**Last updated:** 2017-03-04

**Code version:** 5d0fa13282db4a97dc7d62e2d704e88a5afdb824

Be familiar with basic probability and Bayesian calculations.

The goal here is simply to point out that everything you want to compute in Bayesian calculations is an integral.

Consider inference for a parameter \(\theta\) from data \(D\).

The posterior distribution of \(\theta\) is given by Bayes Theorem

\[p(\theta | D) = p(\theta) p(D | \theta)/ p(D)\]

First note that the denominator \(p(D)\) is an integral: \[p(D) = \int p(D | \theta) p(\theta) d\theta\].

Now suppose we want to estimate \(\theta\) by its posterior mean. This is \[E(\theta | D) = \int \theta p(\theta |D ) d\theta.\]

And if we want to find a 90% posterior credible interval for \(\theta\) then we want to find \(A\) and \(B\) such that \(\Pr(\theta \in [A,B] | D) = 0.9\). Note that the LHS of this is \[\Pr(\theta \in [A,B]|D) = \int I(\theta \in [A,B]) p(\theta | D) d\theta,\] where \(I(E)\) denotes the indicator function for the event \(E\), which takes the value 1 if \(E\) is true and \(0\) otherwise.

Of course, if \(\theta\) is discrete then the integrals above all become sums.

For example \[E(\theta | D) = \sum_n \theta_n \Pr(\theta=\theta_n | D)\] where \(\theta_1,\theta_2,\dots\) are the possible values for \(\theta\).

Pretty much all the things you want to compute when doing Bayesian inference are integrals (or sums) of one kind or anotherâ€¦

If you are computing 1-dimensional integrals then numerical methods are often useful. For example, Simpsons Rule, Gaussian Quadrature. These can also work in 2-dimensions, and maybe even 3 or 4.

Other simple methods that can work for low dimensions: naive Monte Carlo, and Importance Sampling. Also Laplace approximation.

For higher dimensions we usually resort to Markov Chain Monte Carlo.

`sessionInfo()`

```
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/en_NZ.UTF-8/C/en_NZ.UTF-8/en_NZ.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyr_0.4.1 dplyr_0.5.0 ggplot2_2.1.0 knitr_1.15.1
[5] MASS_7.3-45 expm_0.999-0 Matrix_1.2-6 viridis_0.3.4
[9] workflowr_0.3.0 rmarkdown_1.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.5 git2r_0.18.0 plyr_1.8.4 tools_3.3.0
[5] digest_0.6.9 evaluate_0.10 tibble_1.1 gtable_0.2.0
[9] lattice_0.20-33 shiny_0.13.2 DBI_0.4-1 yaml_2.1.14
[13] gridExtra_2.2.1 stringr_1.2.0 gtools_3.5.0 rprojroot_1.2
[17] grid_3.3.0 R6_2.1.2 reshape2_1.4.1 magrittr_1.5
[21] backports_1.0.5 scales_0.4.0 htmltools_0.3.5 assertthat_0.1
[25] mime_0.5 colorspace_1.2-6 xtable_1.8-2 httpuv_1.3.3
[29] labeling_0.3 stringi_1.1.2 munsell_0.4.3
```

This site was created with R Markdown