|
bitsim
Model, simulate, estimate and control robots in C++
|
The vanilla Monte Carlo method we saw in example Monte Carlo intergration requires that we sample from a known distribution \(f\). However, there may be cases where it is difficult to sample from it.
In this example we will look at imporatnce sampling which allows us to overcome the dificculty of sampling from a difficult distribution.
Let us consider once again the integral
\[I=\int_a^b h(x) dx\]
and rewrite it as
\[I=\int_a^b \omega(x)f(x)\]
Importance sampling introduces a new probability distribution \(g\), also known as the proposal distribution [2], that it is easier to sample from. Thus we rewrite the integral as
\[I=\int_a^b \frac{\omega(x)f(x)}{g(x)}g(x)dx=E_g \left[Y \right]\]
where \(Y\) is the random variable defined by
\[Y=\frac{\omega(x)f(x)}{g(x)}\]
We can now sample from \(g\) and estimate \(I\) as
\[\hat{I}=\frac{1}{N}\sum_i Y_i\]
Just like we did in the Monte Carlo integration section, we can use the law of large numbers and show that \(\hat{I}\rightarrow I\) in probability.
In importance sampling we draw samples from \(g\) and re-weight the integral using importance weights so that the correct distribution is targeted [2]. However, \(g\) in general has to have a similar shape with \(f\). Moreover, it has to have thicker tails than \(f\) otherwise the integral may become infinite [1]. Indeed, consider the second moment of \(Y\):
\[E_g\left[ Y^2 \right]=\int Y^2g(x)dx=\int \frac{\omega^2(x)f^2(x)}{g(x)}dx \]
Thinner tails for \(g\) means that it goes fatser to zero than what \(f\) does.
All in all, a good choice for \(g\) is a distribution that is similar to \(f\) but with thicker tails. In fact, the optimal choice for \(g\) is given by the following theorem [1]
Theorem
The choice of \(g\) that minimizes the variance of \(\hat{I}\) is
\[g(x)=\frac{|h(x)|f(x)}{\int |h(s)|f(s)ds}\]
The example we will consider is taken from [1]. We want to estimate the following probability; \(P(Z > 3)\) where \(Z\sim N(0,1)\) This is just the integral:
\[P(Z > 3) = \int_{3}^{+\infty}f(x)dx = \int_{-\infty}^{+\infty}h(x)f(x)dx\]
where \(h(x)\) is 1 if \(x > 3\) and 0 otherwise and \(f(x)\) is the PDF for the standard normal distribution.
The driver code for this tutorial is shown below.
Running the driver code produces the following