# importance sampling from posterior distribution in R

Today I read that Importance Sampling can be used to draw posterior distribution samples just like Rejection Sampling. However, my understanding of Importance Sampling is that its main purpose is to just compute expectations (ie. integrals) that would otherwise be hard to compute. How can Importance Sampling be used to draw posterior distribution samples from a Bayesian Model?

I only know the posterior positive ratio up to some constant, i.e., a constant term in the normalisation.

The boundary between estimating expectations and producing simulations is rather vague in the sense that, once given an importance sampling sample $$(x_1,\omega_1),\ldots,(x_T,\omega_T)\qquad\omega_t=f(x_t)/g(x_t)$$ estimating $$\mathbb E_f[h(X)]$$ by $$\frac{1}{T}\,\sum_{t=1}^T \omega_t h(x_t)$$ and estimating the cdf $$F(\cdot)$$ by$$\hat F(x)=\frac{1}{T}\,\sum_{t=1}^T\omega_t \Bbb I_{x\le x_t}$$ is of the same nature. Simulating from $$\hat F$$ is easily done by inversion and is also the concept at the basis of weighted bootstrap.

In the case where the density $$f$$ is not properly normalised, as in most Bayesian settings, renormalising the $$\omega_t$$'s by their sum is also a converging approximation.

$$\qquad\qquad$$

The field of particle filters and sequential Monte Carlo (SMC) is taking advantage of this principle to handle sequential targets and state-space models. As in the illustrations above and below:

$$\qquad\qquad$$

by using IS(importance sampling) you can calculate a expectation of posterior, that is enough to take prior as Importance distribution. to generate random sample from posterior, you should re-sampling from sample that generated from prior. you should use, SIR (sampling importance resampling).

SIR is a extended of IS that by using it you can draw sample from a posterior distribution

SIR

to generate $$m$$ sample from the posterior distribution,

$$\pi(\theta|X) \propto f(x|\theta) \pi(\theta)$$, follow this step:

step 1: draw $$k$$ i.i.d sample from $$\pi(\theta)$$,(k>m)

$$\{\theta_i \}_{i=1}^{k} \overset{i.i.d}{\sim} \pi(\theta)$$

step 2

appoint weight

$$\begin{array}{c|cccc} \theta & \theta_1 & \theta_2 & \cdots & \theta_k \\ \hline \\ w & w_1=\frac{p(x|\theta_1)}{\sum_{i=1}^{m} p(x|\theta_i)} & w_2= \frac{p(x|\theta_2)}{\sum_{i=1}^{m} p(x|\theta_i)} & \cdots & w_k=\frac{p(x|\theta_m)}{\sum_{i=1}^{m} p(x|\theta_i)} \end{array}$$

now by sampling $$\{\theta_i \}_{i=1}^{m}$$ (without replacement) form

$$\{\theta_i \}_{i=1}^{k}$$ with probability $$\{w_i \}_{i=1}^{k}$$ , you can obtain random sample from posterior.