## 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

source(url("http://bayes.bgsu.edu/m648/post.norm.t.R"))

# define parameters of problem

s=list(y=125,sigma=15/2,mu=100,tau=6.85,df=2)

# set up grid of values of theta

theta=seq(80,160,length=100)

# compute the posterior on the grid

post=post.norm.t(theta,s)

# convert the posterior value to probabilities

post.prob=post/sum(post)

# sample from discrete distribution on grid

sim.theta=sample(theta,size=10000,replace=TRUE,prob=post.prob)

# construct a histogram of simulated sample

# and place exact posterior on top

hist(sim.theta, freq=FALSE)

d=diff(theta[1:2])

con=sum(d*post) # this is normalizing constant

lines(theta,post/con)

From the simulated sample, we can compute any summary of the posterior of interest.

Subscribe to:
Post Comments (Atom)

## No comments:

Post a Comment