Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. ---
  2. title: "TDDE07 Bayesian Learning - Lab 4"
  3. author: "Erik Linder-Norén - erino397"
  4. date: "`r format(Sys.time(), '%d %B, %Y')`"
  5. output:
  6. pdf_document: default
  7. word_document: default
  8. ---
  9. ```{r setup, include=FALSE}
  10. knitr::opts_chunk$set(echo = FALSE)
  11. ```
  12. ## 1. Poisson regression - the MCMC way.
  13. ### (a)
  14. The $\beta_{MLE}$ found by fitting a Poisson regression model with glm can be seen in Table 1. Significant covariates are MinBidShare, Sealed, VerifyID and MajBlem.
  15.  
  16. ``` {r, echo=FALSE, eval=TRUE, results='asis', message=FALSE}
  17. require(MASS)
  18. require(geoR)
  19. library(xtable)
  20. require(mvtnorm)
  21. require(LaplacesDemon)
  22.  
  23. data = read.table("data/eBayNumberOfBidderData.dat", header=TRUE)
  24.  
  25. n = length(data)
  26. n_features = ncol(data) - 1 # Except y and const
  27.  
  28. feature_labels = colnames(data[,2:ncol(data)])
  29.  
  30. y = data$nBids
  31. X = as.matrix(data[,2:ncol(data)])
  32.  
  33. X_X = t(X)%*%X
  34.  
  35. glm_model = glm(nBids ~ 0 + ., data = data, family = poisson)
  36.  
  37. # Table 1
  38. options(xtable.comment=FALSE)
  39. print(xtable(t(as.matrix(glm_model$coefficients)), digits=rep(3, n_features+1), caption='MLE of beta by glm model fitting'), type='latex')
  40. ```
  41.  
  42.  
  43. ### (b)
  44. By approximating the posterior distribution of beta as a multivariate normal and determining the value for $\beta_{MLE}$ by numerical optimization I got the values seen in Figure 2. They closely resemble the values I got by fitting a Poisson regression model using glm in (a). My implementation of the log of the poisson model with a normal prior can be found in Appendix A.
  45.  
  46. ``` {r, echo=FALSE, eval=TRUE, results='asis', message=FALSE}
  47. # Beta prior (Zellner’s g-prior)
  48. mu0 = rep(0, n_features)
  49. covar0 = 100 * ginv(X_X)
  50. init_beta = mvrnorm(n=1, mu0, covar0)
  51.  
  52. # This is the log of the Poisson model
  53. logPostPoiNorm <- function(betas, X, y){
  54.  
  55. log_prior = dmvnorm(betas, mu0, covar0, log=TRUE)
  56.  
  57. lambda = exp(X%*%betas)
  58.  
  59. # Assume independence among samples and take the sum of
  60. # log(p(y_i|lambda)), where lambda is exp(X.dot(beta)) and p ~ Poisson
  61. log_lik = sum(dpois(y, lambda, log=TRUE))
  62.  
  63. return (log_lik + log_prior)
  64. }
  65.  
  66. log_post = logPostPoiNorm
  67.  
  68. opt_results = optim(init_beta,
  69. log_post,
  70. gr=NULL,
  71. X,
  72. y,
  73. method=c("BFGS"),
  74. control=list(fnscale=-1),
  75. hessian=TRUE)
  76.  
  77. # MLE beta
  78. post_mode = opt_results$par
  79. # Covariance (J^-1(beta hat))
  80. post_cov = -solve(opt_results$hessian)
  81.  
  82. # Table 2
  83. names(post_mode) = names(glm_model$coefficients)
  84. options(xtable.comment=FALSE)
  85. print(xtable(t(as.matrix(post_mode)), digits=rep(3, n_features+1), caption='MLE of beta by numerical optimization'), type='latex')
  86. ```
  87.  
  88.  
  89. ### (c)
  90. After having implemented the Metropolis Hastings simulation method and simulated 20000 draws from the posterior distribution from (b)
  91. I calculated the $\beta$ coefficients as the mean of the draws, while omitting the first 10% of the draws because of the burn-in phase. These values
  92. can be seen in Table 3.
  93.  
  94. ``` {r, echo=FALSE, eval=TRUE, results='asis', message=FALSE}
  95.  
  96.  
  97. # -----
  98. # (c)
  99. # -----
  100.  
  101. Sigma = post_cov
  102. c = .6
  103.  
  104. n_draws = 20000
  105.  
  106. metropolisHastings = function(logPostFunc, theta, c, ...){
  107. theta_draws = matrix(0, n_draws, length(theta))
  108. # Set initial
  109. theta_c = mvrnorm(n=1, theta, c*Sigma)
  110. prob_sum = 0
  111. for(i in 1:n_draws){
  112. # 1: Draw new proposal theta
  113. theta_p = mvrnorm(n=1, theta_c, c*Sigma)
  114. # 2: Determine the acceptance probability
  115. p_prev = logPostFunc(theta_c, ...)
  116. p_new = logPostFunc(theta_p, ...)
  117. acc_prob = min(c(1, exp(p_new - p_prev)))
  118. prob_sum = prob_sum + acc_prob
  119. # 3: Set new value with prob = acc_prob
  120. if(rbern(n=1, p=acc_prob)==1){
  121. theta_c = theta_p
  122. }
  123. theta_draws[i,] = theta_c
  124. }
  125.  
  126.  
  127. return (theta_draws)
  128. }
  129.  
  130. init_beta = mvrnorm(n=1, mu0, covar0)
  131. beta_draws = metropolisHastings(logPostPoiNorm, init_beta, c, X, y)
  132.  
  133.  
  134. # Calculate mean of batches of 2 draws to visualize the
  135. # auto correlation between sequential draws
  136. mean_draws = matrix(0, n_draws/2, n_features)
  137. for (i in seq(2,n_draws,2)){
  138. mean_draws[i/2,] = colMeans(beta_draws[c(i-1,i),])
  139. }
  140.  
  141. # Avoid first 10% of the draws
  142. burn_in = floor(n_draws / 10)
  143. beta_draws = beta_draws[burn_in:nrow(beta_draws),]
  144.  
  145. beta_means = colMeans(beta_draws)
  146.  
  147. # Table 3
  148. names(beta_means) = names(glm_model$coefficients)
  149. options(xtable.comment=FALSE)
  150. print(xtable(t(as.matrix(beta_means)), digits=rep(3, n_features+1), caption='Mean values of betas drawn during Metropolis Hastings simulation'), type='latex')
  151.  
  152.  
  153. # -----
  154. # (d)
  155. # -----
  156.  
  157. sample = c(
  158. Constant = 1,
  159. PowerSeller = 1,
  160. VerifyID = 1,
  161. Sealed = 1,
  162. MinBlem = 0,
  163. MajBlem = 0,
  164. LargNeg = 0,
  165. LogBook = 1,
  166. MinBidShare = 0.5
  167. )
  168.  
  169. # Calculate lambda of pred. dens.
  170. lambda = exp(beta_means%*%sample)
  171.  
  172. # Determine the predictive density of the sample
  173. beta_grid = 0:max(y)
  174. pred_dens = dpois(beta_grid, lambda)
  175. names(pred_dens) = beta_grid
  176.  
  177. # Remove dependent variables that have prob. < .1%
  178. pred_dens = pred_dens[pred_dens > .001]
  179.  
  180. # Probability that the sample has 0 bidders
  181. prob = pred_dens[1]
  182.  
  183. ```
  184.  
  185. The convergence of the parameters can be seen in Figure 1, where I have taken the mean value of every two sequental beta drawn from the posterior and
  186. plotted it to visualize the auto-correlation between draws, and to see how the draws asymptotically approaches somewhat stationary values.
  187. My implementation of the Metropolis Hastings algorithm can be found in Appendix A.
  188.  
  189. $\pagebreak$
  190.  
  191. ![Convergence of betas drawn during Metropolis Hastings simulation](./plots/4_1_2_beta_conv.pdf)
  192.  
  193. $\pagebreak$
  194.  
  195. ### (d)
  196. After having determined the predictive distribution of the sample $\hat{x}$ as $p(\hat{y}|\lambda)\sim{Poisson(\lambda)}$, where $\lambda=e^{\hat{x}\beta}$ using the mean of the betas drawn during the simulation in (c), I got the distribution seen in Figure 2. In this figure I have removed the dependent variables that got a probability less than 0.1%. The probabilities of the remaining candidates can also be seen in Table 4. The probability that the sample has zero bidders is $p(\hat{y}=0|\lambda)=$ `r round(prob, 3)`.
  197.  
  198. ![Predictive distribution of sample](./plots/4_1_3_pred_distr.pdf)
  199.  
  200.  
  201. ``` {r, echo=FALSE, eval=TRUE, results='asis', message=FALSE}
  202. options(xtable.comment=FALSE)
  203. pred_dens = t(as.matrix(pred_dens))
  204. rownames(pred_dens) = c("Probability")
  205. print(xtable(pred_dens, digits=rep(3, ncol(pred_dens)+1), caption='Probabilities of the different number of bidders for the sample'), type='latex')
  206. ```
  207.  
  208. $\pagebreak$
  209.  
  210. # Appendix A
  211. ## Code for Lab 4
  212. ```{r, echo=TRUE, eval=FALSE}
  213. require(MASS)
  214. require(geoR)
  215. require(mvtnorm)
  216. require(LaplacesDemon)
  217.  
  218. # -------
  219. # Lab 4
  220. # -------
  221.  
  222. data = read.table("data/eBayNumberOfBidderData.dat", header=TRUE)
  223.  
  224.  
  225. n = length(data)
  226. n_features = ncol(data) - 1 # Except y and const
  227.  
  228. feature_labels = colnames(data[,2:ncol(data)])
  229.  
  230. y = data$nBids
  231. X = as.matrix(data[,2:ncol(data)])
  232.  
  233. X_X = t(X)%*%X
  234.  
  235. # -----
  236. # (a)
  237. # -----
  238.  
  239. glm_model = glm(nBids ~ 0 + ., data = data, family = poisson)
  240.  
  241. pdf("./plots/4_1_1_mle_beta.pdf", width=7, height=7)
  242.  
  243. par(oma = c(0, 0, 3, 0))
  244. layout(matrix(c(0,1,1,0,2,3,4,5,6,7,8,9), 3, 4, byrow = TRUE))
  245. for (i in 1:ncol(X)){
  246. mean = glm_model$coefficients[i]
  247. std_dev = summary(glm_model)[["coefficients"]][,2][i]
  248. x_grid = seq(mean-4*std_dev, mean+4*std_dev, 0.001)
  249. plot(x_grid,
  250. dnorm(x_grid, mean=mean, sd=std_dev),
  251. type="l",
  252. ylab="Density",
  253. xlab=expression(beta),
  254. main=feature_labels[i])
  255. }
  256. title("Normal approximation of MLE of beta", outer=TRUE, cex=1.5)
  257.  
  258. dev.off()
  259.  
  260. # -----
  261. # (b)
  262. # -----
  263.  
  264. # Beta prior (Zellner’s g-prior)
  265. mu0 = rep(0, n_features)
  266. covar0 = 100 * ginv(X_X)
  267. init_beta = mvrnorm(n=1, mu0, covar0)
  268.  
  269. # This is the log of the Poisson model
  270. logPostPoiNorm <- function(betas, X, y){
  271.  
  272. log_prior = dmvnorm(betas, mu0, covar0, log=TRUE)
  273.  
  274. lambda = exp(X%*%betas)
  275.  
  276. # Assume independence among samples and take the sum of
  277. # log(p(y_i|lambda)), where lambda is exp(X.dot(beta)) and p ~ Poisson
  278. log_lik = sum(dpois(y, lambda, log=TRUE))
  279.  
  280. return (log_lik + log_prior)
  281. }
  282.  
  283. log_post = logPostPoiNorm
  284. opt_results = optim(init_beta,
  285. log_post,
  286. gr=NULL,
  287. X,
  288. y,
  289. method=c("BFGS"),
  290. control=list(fnscale=-1),
  291. hessian=TRUE)
  292.  
  293. # MLE beta
  294. post_mode = opt_results$par
  295. # Covariance (J^-1(beta hat))
  296. post_cov = -solve(opt_results$hessian)
  297.  
  298.  
  299. # -----
  300. # (c)
  301. # -----
  302.  
  303. Sigma = post_cov
  304. c = .6
  305.  
  306. n_draws = 20000
  307.  
  308. metropolisHastings = function(logPostFunc, theta, c, ...){
  309. theta_draws = matrix(0, n_draws, length(theta))
  310. # Set initial
  311. theta_c = mvrnorm(n=1, theta, c*Sigma)
  312. prob_sum = 0
  313. for(i in 1:n_draws){
  314. # 1: Draw new proposal theta
  315. theta_p = mvrnorm(n=1, theta_c, c*Sigma)
  316. # 2: Determine the acceptance probability
  317. p_prev = logPostFunc(theta_c, ...)
  318. p_new = logPostFunc(theta_p, ...)
  319. acc_prob = min(c(1, exp(p_new - p_prev)))
  320. prob_sum = prob_sum + acc_prob
  321. # 3: Set new value with prob = acc_prob
  322. if(rbern(n=1, p=acc_prob)==1){
  323. theta_c = theta_p
  324. }
  325. theta_draws[i,] = theta_c
  326. }
  327.  
  328. print(paste('Avg. acc. prob. = ', round(prob_sum/n_draws, 2)))
  329.  
  330. return (theta_draws)
  331. }
  332.  
  333. init_beta = mvrnorm(n=1, mu0, covar0)
  334. beta_draws = metropolisHastings(logPostPoiNorm, init_beta, c, X, y)
  335.  
  336.  
  337. # Calculate mean of batches of 2 draws to visualize the
  338. # auto correlation between sequential draws
  339. mean_draws = matrix(0, n_draws/2, n_features)
  340. for (i in seq(2,n_draws,2)){
  341. mean_draws[i/2,] = colMeans(beta_draws[c(i-1,i),])
  342. }
  343.  
  344. # Avoid first 10% of the draws
  345. burn_in = floor(n_draws / 10)
  346. beta_draws = beta_draws[burn_in:nrow(beta_draws),]
  347.  
  348. beta_means = colMeans(beta_draws)
  349.  
  350. pdf("./plots/4_1_2_beta_conv.pdf", width=7, height=7)
  351.  
  352. par(oma = c(0, 0, 3, 0))
  353. layout(matrix(c(0,1,1,0,2,3,4,5,6,7,8,9), 3, 4, byrow = TRUE))
  354. x_grid = 1:nrow(mean_draws)
  355. for (i in 1:ncol(X)){
  356. plot(x_grid,
  357. mean_draws[,i],
  358. type="l",
  359. ylab="",
  360. xlab="Iteration",
  361. col="lightgray",
  362. main=feature_labels[i])
  363. }
  364. title("Convergence of beta during Metropolis Hastings", outer=TRUE, cex=1.5)
  365.  
  366. dev.off()
  367.  
  368. # -----
  369. # (d)
  370. # -----
  371.  
  372. sample = c(
  373. Constant = 1,
  374. PowerSeller = 1,
  375. VerifyID = 1,
  376. Sealed = 1,
  377. MinBlem = 0,
  378. MajBlem = 0,
  379. LargNeg = 0,
  380. LogBook = 1,
  381. MinBidShare = 0.5
  382. )
  383.  
  384. # Calculate lambda of pred. dens.
  385. lambda = exp(beta_means%*%sample)
  386.  
  387. # Determine the predictive density of the sample
  388. beta_grid = 0:max(y)
  389. pred_dens = dpois(beta_grid, lambda)
  390. names(pred_dens) = beta_grid
  391.  
  392. # Remove dependent variables that have prob. < .1%
  393. pred_dens = pred_dens[pred_dens > .001]
  394.  
  395. # Probability that the sample has 0 bidders
  396. prob = pred_dens[1]
  397.  
  398. pdf("./plots/4_1_3_pred_distr.pdf", width=5, height=4)
  399.  
  400. # Plot the predictive distribution
  401. pred_plot = barplot(pred_dens,
  402. col="white",
  403. xaxt="n",
  404. xlab="Number of bidders",
  405. ylab="Probability",
  406. main="Predictive distribution of sample")
  407.  
  408. axis(1, at=pred_plot, labels=names(pred_dens))
  409.  
  410. dev.off()
  411.  
  412.  
  413. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement