############################################################################################################## # # Figure1.4 (page 32) – Inadequacy of the SIR method when the overlap between the proposal density # and the target density is relatively small. # # p(x) = 0.7*N(x;5,6.25) + 0.3*N(x;0,4) # q(x) = t(-5,1.44,2) # ############################################################################################################## # # Author : Hedibert Freitas Lopes # Graduate School of Business # University of Chicago # 5807 South Woodlawn Avenue # Chicago, Illinois, 60637 # Email : hlopes@ChicagoGSB.edu # ############################################################################################################### set.seed(13883) M = 500 par(mfrow=c(1,1)) x = seq(-15,15,length=5000) p = 0.7*dnorm(x,5,2.5)+0.3*dnorm(x,0,2) q = 0.694*dt((x+5)/1.2,2) plot(x,p,xlab="",ylab="",main="",ylim=c(0,0.25),type="l",axes=F) axis(1) axis(2,at=round(seq(0,0.25,length=6),2)) lines(x,q,lty=1) text(-3.5,0.225,"q",cex=2) text(7.5,0.1,expression(pi),cex=2) # Sampling from q # --------------- x1 = -5+1.2*rt(M,2) for (i in 1:M) segments(x1[i],0,x1[i],-0.005) # Weighted resampling # ------------------- w = (0.7*dnorm(x1,5,2.5)+0.3*dnorm(x1,0,2))/(0.694*dt((x1+5)/1.2,2)) w = w/sum(w) for (i in 1:M) segments(x1[i],0,x1[i],0.75*w[i],lty=1) abline(h=0)