# Importance Sampling

Let's say we have two distribution $$f(x)$$​ and $$g(x).$$​Now the problem is let's say you want to estimate the expectation of $$g(x)$$​but you can't sample from it, hence you won't be able to use sample mean to estimate the expectation. So what we do here is, we use samples from $$f(x)$$​to estimate the expectation of $$g(x)$$​.&#x20;

So we know we can estimate the expectation of $$f(x)$$​ using sample mean by:

$$
E\_f\[x] \approx \frac{1}{n} \sum\_{i=1}^n x\_i, \space x\_i \sim f(x)
$$

​Now to estimate $$E\_g\[x]$$ we go trick

$$
E\_g\[x] = \sum xg(x) = \sum x\frac{g(x)}{f(x)} f(x) = E\_f\[x\frac{g(x)}{f(x)}]\approx \frac{1}{n} \sum\_{i=1}^n x\_i\frac{g(x\_i)}{f(x\_i)}, \space x\_i \sim f(x)
$$

### Importance Sampling to estimate probability using Monte Carlo Method

{% embed url="<https://youtu.be/pAbQHKr_Zqo>" %}

{% embed url="<https://youtu.be/C3p2wI4RAi8>" %}
