LCE Homepage

S-114.600 Introduction to Bayesian Modeling - Hints


Exercise 2.11.

  1. You can create the grid with the command theta=0:(1/M):1 or theta=linspace(0,1,M+1). When plotting the image, divide the density with the step size (1/M) and scale the y-axis properly.
  2. You can sample from the posterior with the inverse cdf method (Gelman et al., page 25). For example
    for i=1:1000,
    Note that now the sum of the posterior MUST be 1, i.e. not divided by 1/M. The division with the step size is simply for plotting the image in a).
    The number of bins in the histogram is controlled by the parameter n in hist(x,n).
  3. The Cauchy distribution equals the Student-t distribution with one degree of freedom. You can draw from the standardized Student-t distribution (scale 1, mode 0) with the command trnd.

Exercise 3.3.

  1. Trnd creates standardized Student-t random numbers. To get samples from a different t-distribution, multiply the standardized random numbers with the scale parameter and add the mean. Prctile can be used to compute the posterior interval.

Exercise 3.4.

  1. Betarnd draws from the beta distribution.

Exercise 3.6.

  1. Compute the posterior density in a grid created by meshgrid and sample as in Exercise 2.11. M-file

Exercise 4.1.

  1. Instead of the prior given in the exercise, assume that the prior on theta is uniform on [-5,5]. You can check your result by directly minimizing the negative log posterior with fminunc: [x,t1,t2,t3,t4,H]=fminunc('f1',w0) where x=posterior mode, H=Hessian and w0=starting value.

Exercise 6.1.

  1. The parameters of the identical effect model are given on page 143 in Gelman et al. Use these and the sigmas on page 140 to generate replicated data sets. Sort the replicates (with sort) and compare the smallest (largest, 3rd largest etc.) elements with the corresponding element from the observed data.

Exercise 11.3.

  1. Write an m-file for the inverse logit function as well as the joint posterior distribution and modify your Metropolis code to do the sampling. Use PSRF to monitor the convergence. psrf.m is available on the course website.


  1. The user interface of BUGS is rather cumbersome. To perform sampling, you need three open windows: the model, the data and the initial values. These can be loaded from the File -> Open menu (after selecting the file, choose "load as text" in the following dialog).
  2. To compile the model, highlight the model window, select Model -> Specification and choose "check model". All errors and such are printed out at the foot of the screen. Next, highlight the data window and press "load data". If the data loads, press "compile". After this, highlight the initial value window and select "load inits", followed by "gen inits" if the model contains uninitialized variables. The model should now be ready for sampling.
  3. To sample from the posterior, first open the Sample Monitor Tool from Inference -> Samples, write the names of the variables (one at a time) you are interested in and press "set". To produce the actual samples, go Model -> Update, select the number of samples and press "update". When the sampling finishes, go back to the Sample Monitor Tool, select the variable from the menu (or write an asterisk in the field) and use the buttons to obtain densities, quantiles etc.
  4. The Flash movie at also illustrates the process.

This page is maintained by
This page has been updated 13. 5. 2004