### R commands for Section 6.4.1 -- Importance Sampling ### G. Givens & J. Hoeting, Computational Statistics, 2nd edition, Wiley, 2013. #________________________________________________________________ #________________________________________________________________ # IMPORTANCE SAMPLING # # Key idea: Let X~exponential(1). Use importance sampling to find # E(sqrt(x)). Compare the performance of IS using various envelopes. # Some code is commented out for use in exercises in 6.1 below. # Importance sampling (weights are not standardized) samp.size=10000 ## Envelopes ##Case 1: abs(Normal(0,1)) set.seed(1) x=abs(rnorm(samp.size)) wts=dexp(x)/(dnorm(x)/.5) # Importance sampling estimate of the mean mu.is = (1/samp.size)*sum(sqrt(x)*wts) mu.is # 0.8535775 #variance of the weights and effective sample size var(wts) # 0.5812052 samp.size/(1+var(wts)) #ESS # 6324.29 #Importance sampling with standardized weights, estimated mean mu.sis = sum(sqrt(x)*wts)/sum(wts) mu.sis # 0.8654587 ##Case 2: uniform(0,1000) set.seed(1) x=runif(samp.size,0,1000) wts=dexp(x)/(1/1000) # Importance sampling estimate of the mean mu.is = (1/samp.size)*sum(sqrt(x)*wts) mu.is # 1.03526 #variance of the weights and effective sample size var(wts) # 492.2098 samp.size/(1+var(wts)) #ESS # 20.27535 #Importance sampling with standardized weights, estimated mean mu.sis = sum(sqrt(x)*wts)/sum(wts) mu.sis # 0.9489134 ##Case 3: abs(Cauchy(0,1)) set.seed(1) x=abs(rcauchy(samp.size)) wts=dexp(x)/(dcauchy(x)/.5) # Importance sampling estimate of the mean mu.is = (1/samp.size)*sum(sqrt(x)*wts) mu.is # 0.8803712 #variance of the weights and effective sample size var(wts) # 0.1787769 samp.size/(1+var(wts)) #ESS # 8483.37 #Importance sampling with standardized weights, estimated mean mu.sis = sum(sqrt(x)*wts)/sum(wts) mu.sis # 0.8776175