I want to use self-normalized importance sampling methods to estimate $$\int_{1}^{\infty} \frac{x^2}{\sqrt{2\pi}}e^{\frac{-x^2}{2}} \,dx$$ I choose exponential distribution with rate $\lambda=1$ as my importance function which is $$f(x)=e^{-x}$$ The true value of the integration is about $0.400$ but I get 0.799. The following is my R code. I follow the algorithm in page 32. http://people.sabanciuniv.edu/sinanyildirim/Lecture_notes.pdf I still can't find the error in my code.
N=10000
f=function(x){
return((x^2)*(x>=1))
}
p=function(x){
return(dnorm(x))
}
#((1/sqrt(2*pi))*exp((-x^2)/2))
q = function(x) {
return(exp(-x))
}
x = rexp(N, rate =1)
theta.hat2=sum((p(x)/q(x))*f(x))/sum((p(x)/q(x)))
theta.hat2
Update:
I use t distribution as the important function because it has same support with the target density and I get the desired value. How can I compute the variance of this estimator?
N=10000
f=function(x){
return((x^2))
}
p=function(x){
return(exp((-x^2)/2))
}
q = function(x) {
return(dt(x,df=3))
}
x = rt(N,df=3)
w_u=p(x)/q(x)
w=w_u/sum(w_u)
theta.hat2=sum(w*f(x)*(x>=1))
theta.hat2
# 0.4064571