Last updated: 2017-01-02
Code version: 55e11cf8f7785ad926b716fb52e4e87b342f38e1
This illustrates how the prior, likelihood, and posterior behave for inference for a normal mean (μ) from normal-distributed data, with a conjugate prior on μ.
Specifically the prior on μ is N(μ0, τ20) [dotted line] and the data is sampled from a normal distribution N(μ, σ2), which gives the likelihood [black line]. Note that the likelihood is scaled so it fits nicely on the graph (remember, likelihoods only matter up to a constant, so you can scale them however is convenient).
Because the normal distribution is the conjugate prior for normal sampling, the posterior distribution is also a normal distribution, and is shown in red.
By Bayes theorem:
Pr(μ|y,σ2)∝Pr(y|μ,σ2)Pr(μ)
N(μ1,τ21)=N(μ,σ2)N(μ0,τ20)
where the posterior mean:
μ1=μ0τ20+nˉyσ21τ20+nσ2
and the posterior variance:
τ21=(1τ20+nσ2)−1
An interactive app that shows how the posterior distribution will change when the prior and the (scaled) data likelihood changes:
The source code of the app can be found here.
sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.14 MASS_7.3-45 expm_0.999-0 Matrix_1.2-7.1
[5] rmarkdown_1.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 lattice_0.20-34 gtools_3.5.0 digest_0.6.9
[5] assertthat_0.1 mime_0.4 R6_2.1.2 grid_3.3.2
[9] xtable_1.8-2 formatR_1.4 magrittr_1.5 evaluate_0.9
[13] stringi_1.1.1 tools_3.3.2 stringr_1.0.0 shiny_0.13.2
[17] httpuv_1.3.3 yaml_2.1.13 htmltools_0.3.5 tibble_1.2
This site was created with R Markdown