There is a simple method, called the SIR algorithm, of taking a simulated sample of draws from one distribution p, and using these draws to produce a sample from a different distribution g. We illustrate this method for the Cauchy sampling model example introduced in the last post.
Suppose that we have a proposal density g(theta) that we believe is a rough approximation to the posterior (in terms of location and spread). Here we suppose that a t density with mean 7, variance 9 and degrees of freedom 3 is a rough approximation to our bimodal posterior density for theta.
There are three steps in the SIR algorithm.
1. (S) We Sample 1000 draws from the proposal density p. We are storing these in the vector theta.p.
2. (I) We compute Importance sampling weights for this sample equal to the ratios of the target density (g) to the proposal density (p).
The following figure plots the simulated draws from the proposal density against the weights.
3. (R) We resample 1000 draws with replacement from the simulated draws theta.p, where the sampling probabilities are proportional to the weights.
The values in the vector theta should be (approximately) from the posterior density g.
The sir algorithm with a t proposal density is implemented in the function sir.R in the LearnBayes algorithm.
To illustrate this function, remember the definition of the log posterior is in cpost and the data is stored in the vector y. We create a list tpar that contains the components of the t proposal density.
MEAN=7; VAR=9; DF=3
Then we implement the algorithm using the sir function -- the output is a vector of simulated draws from the posterior.