I am trying to implement Monte Carlo integration with importance sampling. I've created a trivial example - I wish to integrate h(x), which is has a student t-distribution (mu =1, sigma =1, df=100), but us scaled-up 4 times - I want to integrate over the interval [-2,2] - f(x), the pdf of my h(x), is then t(1,1,100) - my proposal distribution is g(x), and normal (0,1)
I can't get this work... I am confused with how to implement Importance Sampling and using a probability-density-function for h(x) and a proposal distribution g(x). I'm sure my implementation is wrong. I was hoping someone could please help me?
xtemp<-rnorm(100000)
x<-xtemp[which(xtemp>=-2 & xtemp<=2)]
hx<-dt(x,100)*4
fx<-dt(x,100)
gx<-dnorm(x)
IntMC<-sum(hx*fx/gx)/length(hx)
IntAn <-(pt(2,100)-pt(-2,100))*4