I have asked the question here also.
However, there might be something wrong with my theoretical understanding hence I'm asking here as it is more relevant. Kindly do not diss without looking first.
I have referred to these links –
here and here. The latter link is a question of mine, but the answers, as they turn out, aren't quite right.
Hence I am creating asking a question with new attempts made to answer it.
- I have the following integral
$$\int_{0.01}^1 x^{-0.5}\,\text{d}x$$
To solve this using Importance Sampling MC integration, one needs to select an importance pdf that is approximately the same as the function plot
My R code to solve the same is this :
#function 1 - importance sampling
w <- function(x) dunif(x,0.01,1)/dbeta(x,0.7,1)
f <- function(x) x^(-0.5)
X <- rbeta(1000,0.7,1)
Y <- w(X)*f(X)
c(mean(Y),var(Y))
True integral value – 1.8
Using the Importance Sampling code above – 1.82 (where my importance PDF is Beta(0.7,1)
which is quite alright so I'm assuming the code is correct.
- Now I have this integral
$$\int_{0.3}^8 [1+\sinh(2x)\log(x)]^{-1}\text{d}x$$
for which my code is :
#function 2
w <- function(x) dunif(x,0.01,1)/dnorm(x,0.5,0.25)
f <- function(x) (1+sinh(2*x)*log(x))^(-1)
X <- rnorm(1000,0.5,0.25)
Y <- w(X)*f(X)
Y <- Y[!is.na(Y)]
c(mean(Y),var(Y))
True Integral Value ~0.7014
Value from executing above code ~3.25 (where my importance PDF is normal(0.5,sd=0.25)
What am I doing wrong?
1) Take function to be evaluated as f(x).
2) Generate samples from Importance PDF g(x) that is truncated between the intervals.
3) Get the mean(f(x)/g(x)) which is the integral.
Best Answer
When running the code provided for the second function I get
which means that the true value of the integral is close to 3.2, not to 0.70.
If you want to integrate
f
from 0.3 to 8, then the importance function must beand I changed the handling of the NA's in the problem by
which leads to
which shows a good fit even though the Normal importance function N(0.5,0.5) may be too concentrated, i.e., it has too small a variance to cover (0.3,8) with some reasonable probability. If one changes the scale of the Normal,
which shows a fairly good fit. When using a smaller standard deviation like $\sigma=0.1$, I find a poorer fit
even when compared with $\sigma=1$:
To make sense of those variations, I ran the experiment for a range of values of $\sigma$, from $0.1$ to $4$, with $10^6$ normal simulations, and got the following graph (with a log scale on the first axis)
which shows the improvement brought by large enough $\sigma$'s. And an optimum around $\sigma=0.74$.
Here is the relevant R code