# An Illustration of Importance Sampling

##### Posted on (Update: ) 0 Comments

This report shows how to use importance sampling to estimate the expectation.

We want to evaluate the quantity

where $\pi(x)$ is pdf and $h(x)$ is the target function.

## An Example

Consider a target function given by

where $(x, y)\in [-1, 1]\times [-1,1]$.

We can plot the following function image

For mean, we have

By taking $m$ random samplers, $(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), \ldots, (x^{(m)}, y^{(m)})$, uniformly in $[-1,1]\times [-1, 1]$

then

The implement code is as follows

## Basic Idea

Suppose one is interested in evaluating

The procedure of a simple form of the **importance sampling algorithm** is as follows.

- Draw $\mathbf x^{(1)}, \ldots, \mathbf x^{(m)}$ from a
**trial distribution**$g$ - Calculate the importance weight $w^{(j)} = \pi (\mathbf x^{(j)})/ g(\mathbf x^{(j)}), \; for \; j=1, \ldots, m$
- Approximate $\mu$ by $\hat \mu = \frac{\sum_{j=1}^m w^{(j)}h(\mathbf x^{(j)})}{\sum_{j=1}^m w^{(j)}}$

The implementation code is as follows.