################################################################################ # # Third homework assignment - Solution # PhD in Business Economics # Advanced Bayesian Econometrics # Hedibert Freitas Lopes # Due at 9am, February 18th, 2021. # # Nonlinear Gaussian regression # # Let us consider the context of Example 6.1 (pages 192-194) of # Gamerman and Lopes (2006), where the response variable y, is # the velocity of an enzymatic reaction (in counts/min/min) and # the regressor, x, is substrate concentration (in ppm), where # the enzyme has been treated with Puromycin. Check the book # webpage at http://www.dme.ufrj.br/mcmc/chapter6.html. # # Reference: Carlin and Louis (1998) Bayes and Empirical Bayes Methods # for Data Analysis, Chapman & Hall/CRC, pages 233-234. # ################################################################################ g = function(gamma,x){x/(gamma+x)} log.full.gamma = function(beta,gamma,sigma2){ dnorm(gamma,g0,tau0,log=TRUE)+ sum(dnorm(y,beta[1]+beta[2]*g(gamma,x),sqrt(sigma2),log=TRUE)) } x = c(0.02,0.02,0.06,0.06,0.11,0.11,0.22,0.22,0.56,0.56,1.10,1.10) y = c(76,47,97,107,123,139,159,152,191,201,207,200) n = length(x) pdf(file="data.pdf",width=6,height=5) plot(x,y,ylim=range(y,q1,q2),pch=16,xlab="Substrate concentration (ppm)", ylab="Velocity of enzymatic reaction (counts/min/min)",col=2) dev.off() # Prior hyperparameters p = 2 b0 = c(50,170) B0 = diag(3,p) g0 = 0.05 tau20 = 0.01 nu0 = 5 sigma20 = 126 tau0 = sqrt(tau20) # Sufficient statistics and other constants par1 = (nu0+n)/2 iB0 = solve(B0) iB0b0 = iB0%*%b0 nu0sigma20 = nu0*sigma20 # MCMC setup and nitial values burnin = 10000 M = 10000 skip = 10 niter = burnin+skip*M beta = b0 gamma = g0 sigma2 = sigma20 # Running algorithms 1 or 2 set.seed(12345) sdg = 0.1 # standard deviation of RW-MH proposal for (algorithm in 1:2){ draws = matrix(0,niter,4) for (iter in 1:niter){ X = cbind(1,g(gamma,x)) # sampling from sigma form # sigma|x,y,beta,gamma (full conditional or from # sigma|x,y,gamma (marginal) B1 = solve(t(X)%*%X+iB0) b1 = B1%*%(t(X)%*%y+iB0b0) if (algorithm==1){ par2 = (nu0sigma20+sum((y-X%*%beta)^2))/2 }else{ par2 = (nu0sigma20+sum((y-X%*%b1)*y)+t(b0-b1)%*%iB0b0)/2 } sigma2 = 1/rgamma(1,par1,par2) # sampling beta|x,y,sigma2,gamma beta = b1 + sqrt(sigma2)*t(chol(B1))%*%rnorm(2) # sampling gamma|x,y,beta,sigma2 gamma1 = rnorm(1,gamma,sdg) lognum = log.full.gamma(beta,gamma1,sigma2) logden = log.full.gamma(beta,gamma,sigma2) logaccept = min(0,lognum-logden) if (log(runif(1))