Obtain a Monte Carlo estimate of
by importance of sampling.
#Sequence Generation
x<-seq(1,6,0.01)
y<- (x^2/sqrt(2*pi))* exp(-(x^2/2))
plot(y~x,type="h")
#initialization of variables
m<-1000
theta.hat<-se<-numeric(1)
foo<- function(x)
{
(x^2/sqrt(2*pi))* exp(-(x^2/2))
}
x<-rexp(m,1)
fg<-foo(x)/exp(-x)
theta.hat[1]<-mean(fg)
se[1]<-sd(fg)
#The Cauchy Distribution
x<-rcauchy(m)
i<-c(which(x>1),which(x<0))
x[i]<-2
fg<- foo(x)/dcauchy(x)
theta.hat[2]<-mean(fg)
se[2]<-sd(fg)
rbind(theta.hat,se)
## [,1] [,2]
## theta.hat 0.4985743 2.628723
## se 0.5287391 1.313270