This lab is an in-class demo for 536 students. It should be turned in along with Lab 4.
The final step in any Bayesian analysis is calculating, or sampling from, the posterior distribution. The posterior distribution, \(p(\theta|y)\) can be thought of an update to the prior belief after observing the data (\(y\)).
In this setting with a normal sampling model and a normal prior distribution, the posterior is also from the normal family.
set.seed(09212018)
library(dplyr)
library(ggplot2)
mu0 = 0
C0 = 100
x <- seq(-25,25, by=1)
prior.dist <- data.frame(x=x, p.x = dnorm(x,mean=mu0,sd=sqrt(C0)))
fig <- ggplot(data=prior.dist, aes(x=x,y=p.x)) + geom_line() + ggtitle('Prior Distribution')
fig
theta.true = 10
sigma.sq.true = 4
y1 <- rnorm(1, mean=theta.true, sd = sqrt(sigma.sq.true))
fig <- fig + annotate("text", x = y1, y = 0, label = "1", color = 'dodgerblue4') + ggtitle('Prior Distribution and first observed point')
fig
mu1 <- mu0 + (C0 / (C0 + sigma.sq.true)) * (y1 - mu0)
C1 <- (sigma.sq.true * C0) / (sigma.sq.true + C0)
post.dist1 <- data.frame(x=x, p.x = dnorm(x,mean=mu1,sd=sqrt(C1)))
fig <- fig + geom_line(data=post.dist1, aes(x=x, y=p.x), color='red') + ggtitle('Updated posterior distribution') + labs(caption='prior distribution in black, posterior after one point in red')
fig