Tuesday, September 11, 2007
Brute-force computation of a posterior
Suppose we observe y that is normal with mean theta and standard deviation sigma. Instead of using a conjugate prior, suppose that theta has a t distribution with location mu, scale tau, and degrees of freedom df. Although there is not a nice form for the posterior density, it is straightforward to compute the posterior by use of the "prior x likelihood" recipe. We write a function post.norm.t.R that computes the posterior.
# we source this function into R
# define parameters of problem
# set up grid of values of theta
# compute the posterior on the grid
# convert the posterior value to probabilities
# sample from discrete distribution on grid
# construct a histogram of simulated sample
# and place exact posterior on top
con=sum(d*post) # this is normalizing constant
From the simulated sample, we can compute any summary of the posterior of interest.