############################################################################################ # # 2-component mixture of bivariate normals: # # Rejection Method # Sampling importance resampling # Random-walk Metropolis-Hastings algorithm # Independent Metropolis-Hastings algorithm # ############################################################################################ # # HEDIBERT FREITAS LOPES # Associate Professor of Econometrics and Statistics # The University of Chicago Booth School of Business # 5807 South Woodlawn Avenue # Chicago, Illinois, 60637 # Email : hlopes@ChicagoBooth.edu # URL: http://faculty.chicagobooth.edu/hedibert.lopes # ############################################################################################ rm(list=ls()) post = function(th){pi1*dmvnorm(th,m1,S1)+pi2*dmvnorm(th,m2,S2)+pi3*dmvnorm(th,m3,S3)} m1 = c(1,4) m2 = c(4,2) m3 = c(6.5,2) S1 = matrix(c(1,-0.9,-0.9,1),2,2) S2 = matrix(c(1,-0.5,-0.5,1),2,2) S3 = matrix(c(1,-0.5,-0.5,1),2,2) pi1 = 1/3 pi2 = 0.0 pi3 = 1-pi1-pi2 N = 100 th1 = seq(-5,15,length=N) th2 = seq(-10,15,length=N) den = matrix(0,N,N) for (i in 1:N) for (j in 1:N) den[i,j] = post(c(th1[i],th2[j])) # Contour plot par(mfrow=c(1,1)) contour(th1,th2,den,xlab=expression(theta[1]),ylab=expression(theta[2]),drawlabels=FALSE) # 3D plot par(mfrow=c(1,1)) persp(th1,th2,den,theta=30,phi=30,expand=0.5,col="lightblue", ltheta=120,shade=0.75,ticktype="detailed",xlab="theta1", ylab="theta2",zlab=expression(f(theta))) # Proposal distribution mean1 = c(4,2) Var1 = 9*matrix(c(1,-0.25,-0.25,1),2,2) den1 = matrix(0,N,N) for (i in 1:N) for (j in 1:N) den1[i,j] = dmvnorm(c(th1[i],th2[j]),mean1,Var1) par(mfrow=c(1,1)) image(th1,th2,den,xlab=expression(theta[1]),ylab=expression(theta[2]),col=topo.colors(12)) contour(th1,th2,den1,add=TRUE,drawlabels=FALSE,lwd=3,col=grey(0.9)) # Accept/reject method set.seed(1231647) M = 10000 draws = rmvnorm(M,mean1,Var1) prob = rep(0,M) for (j in 1:M) prob[j] = post(draws[j,])/(10*dmvnorm(draws[j,],mean1,Var1)) ind = runif(M)