This is the target I want to integrate with Monte Carlo with control variate method: $$\theta = \int_{1}^{\infty}\frac{x^2}{\sqrt{2\pi}}e^{-x^2/2}dx$$ I have checked with Wolfram that it is 0.400626, so the control variate should be converge to this value. I use standard normal distribution and gamma distribution (shape = 3 and rate = 1) as two control variate, but fail to converge it! What's wrong with my code? or Idea?
Here is my R code and output.
sample_size <- seq(from = 100, to = 10^4, by = 10)
target <- function(x){
x^2 * exp(-(x^2)/2) /sqrt(2*pi)
}
Sim4.1.theta <- numeric(length(sample_size))
Sim4.1.se <- numeric(length(sample_size))
Sim4.2.theta <- numeric(length(sample_size))
Sim4.2.se <- numeric(length(sample_size))
MC.sim.4 <- function(size){
u1 <- rnorm(size)
f2.1 <- u1 <- u1[u1>=1]
T1.1 <- target(u1)
u2 <- rgamma(size, shape = 3, rate = 1)
f2.2 <- u2 <- u2[u2>=1]
T1.2 <- target(u2)
c.star.1 <- -lm(T1.1~f2.1)$coeff[2]
c.star.2 <- -lm(T1.2~f2.2)$coeff[2]
T2.1 <- T1.1 + c.star.1*(f2.1 - pnorm(1,lower.tail = FALSE))
T2.2 <- T1.2 + c.star.2*(f2.2 - pgamma(1,shape = 3, rate = 1, lower.tail = FALSE))
control1.estimate <- mean(T2.1[u1>=1])
control2.estimate <- mean(T2.2[u2>=1])
control1.se <- sd(T2.1)/sqrt(size)
control2.se <- sd(T2.2)/sqrt(size)
return(rbind(control1.estimate,control2.estimate,control1.se,control2.se))
}
for (i in 1:length(sample_size)) {
tem <- MC.sim.4(sample_size)
Sim4.1.theta[i] <- tem[1]
Sim4.1.se[i] <- tem[2]
Sim4.2.theta[i] <- tem[3]
Sim4.2.se[i] <- tem[4]
}
plot(x = sample_size, y = Sim4.1.theta, type = 'l',col = '#2166AC', ylim = c(0,0.5), xlab = '# of sampling size')
lines(x = sample_size, y = Sim4.2.theta, col = '#B2182B')
abline(a=0.400626,b=0,col='red')