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.