1
Francisco José Vázquez Polo[www.personales.ulpgc.es/fjvpolo.dmc]
Miguel Ángel Negrín Hernández[www.personales.ulpgc.es/mnegrin.dmc]
{fjvpolo or mnegrin}@dmc.ulpgc.es
Course on Bayesian Methods
Basics (continued):
Models for proportions and means
Markov Chain Monte Carlo Methods
Goals
-To make inference about model parameters
- To make predictions
¿Simulation?
Posterior distribution is not analytically tractable
Monte Carlo integration and MCMC
Monte Carlo integration
-Draw independent samples from required distribution
-Use sample averages to approximate expectations
Markov Chain Monte Carlo (MCMC)
- Draws samples by running a Markov chain that isconstructed so that its limiting distribution is the jointdistribution of interest
Markov chains
A Markov chain is a sequence of random variables X0, X1, X2,…
At each time t ≥ 0 the next state Xt+1 is sampled from adistribution
P(Xt+1 | Xt)
that depends only on the state at time t
- Called “transition kernel”
Under certain regularity conditions, the iterates from a Markovchain will gradually converge to draws from a unique stationaryor invariant distribution
-i.e. chain will forget its initial state
-As t increases, samples points Xwill look increasingly like(correlated) samples from the stationary distribution
Suppose
-MC is run for N (large number) iterations
- We throw away output from first m iterations (burn-in)
-Regularity conditions are met
Then by ergodic theorem
-We can use averages of remaining samples to estimatemeans
Markov chains
Gibbs sampling: one way to construct the transitionkernel
Seminal references
Geman and Geman (IEEE Trans. Pattn. Anal. Mach. Intel.,1984)
Gelfand and Smith (JASA, 1990)
Hasting (Biometrika, 1970)
Metropolis, Rosenbluth, et al. (J. Chem. Phys, 1953)
Subject to regularity conditions, joint distribution isuniquely determined by “full conditional distributions”
- Full conditional distribution for a model quantity isdistribution of that quantity conditional on assumedknown values of all the other quantities in the model
Break complicated, high-dimensional problem into a largenumber of simpler, low-dimensional problems
Gibbs sampling: one way to construct the transition kernel
Example: inference about normal mean and variance,both unknown.
Model
Priors
We want posterior means, posterior means, posteriorcredible sets for µ, σ2
Gibbs sampling: one way to construct the transition kernel
(σ2|x) 
h((n+n0)/2-1) exp{(-1/2)[b0(-a0)+s0h+hi(xi-)²]}
Gibbs sampling: one way to construct the transition kernel
Posterior distribution (Bayes’ theorem):
This is not a known distribution
We can obtain the conditional distributions:
Gibbs sampling: one way to construct the transition kernel
(|h, x) =                      =
(h|x)
    (h|x)
(h|x)
(h|x)d
(h|x)
    (|x)
(h|x)
(h|x)dh
(h|x) =                      =
where,
 (|h, x)  exp{                                                  }
(b0+nh)( -                     )2
-1
2
a0b0 +hn
b0+nh
a0b0 +hn
b0+nh
1
b0+nh
N(                  ,                   )
2
n0+n
(s0+i(xi-)²)
2
 G(              ,                          )
h     exp{-                          ·h}
n0+n
2
(s0+i(xi-)²)
2
 (h|x) 
-1
Gibbs sampling: one way to construct the transition kernel
Gibbs sampler algorithm for Normal
1.Choose initial values µ(0) σ2(0)
2.At each iteration t, generate new value for eachparameter, conditional on most recent value of allother parameters
Gibbs sampling: one way to construct the transition kernel
 Gibbs sampling:
Step 0. Initial values: (0) = (0, h0)
Step 1. How to generate the next simulation (1) = (1, h1):
We can simulate 1 from (|h=h0, x),
(Normal distribution)
We can simulate h1 from (h|1, x),
(Gamma distribution)
We can update (0, h0) to (1, h1),
·
·
·
Step k. Update (k) = (k, hk),  from (k-1) .
Gibbs sampling: one way to construct the transition kernel
Other MCMC techniques
 Metropolis Hasting
Metropolis et al. (1953) and Hasting (1970), Tierney (1994),
Chib and Greenberg (1995), Robert and Casella (1999)
- Data augmentation
Tanner and Wrong (1987)
What is WinBUGS?
“Bayesian inference Using Gibbs Sampling”
General purpose program for fitting Bayesian models
Developed by David Spiegelhater, Andrew Thomas, NickyBest, and Wally Gilks at the Medical Research CouncilBiostatistics Unit, Institute of Public Health, in Cambridge,UK
Excellent documentation, including two volumes ofexamples
Web page:
www.mrc-bsu.cam.ac.uk/bugs/
Software: WinBUGS
What does WinBUGS do?
Enable user to specify model in simple language
Construct the transition kernel of a Markov chain with thejoint posterior as its stationary distribution, and simulate asample path of the resulting chain
-Generate random variables from standard densitiesusing standard algorithms.
-WinBUGS uses Metropolis algorithm to generate fromnonstandard full conditionals
Topics. WinBUGS
Deciding how many chains to run
Choosing initial values
 - Do not confuse initial values with priors!
-Priors are part of the model. Initial values are part of thecomputing strategy used to fit the model
-Priors must not be based on the current data.
-The best choices of initial values are values that are in ahigh-posterior-density region of the parameter space. Ifthe prior is not very strong, then maximum likelihoodestimates (from the current data) are excellent choices ofinitial values if they can be calculated.
Initial values
Initial values are not like priors!
Choosing initial values
- Run more than one chain with initial values selected togive you information about sampler performance
- Initial values may be generated from priors
- Initial values may be based on frequentist estimates.
-WinBUGS usually can automatically generate initialvalues for other parameters
-But it’s often advantageous to specify even thoseWinBUGS can generate.
Initial values must be specified for variance components
Initial values
In the simple models we have encountered so far, theMCMC sampler will converge quickly even with a poorchoice of initial values.
-In more complicated models, choosing initial values inlow posterior density regions may make the sampler takea huge number of iterations to finally star drawing from agood approximation to the true posterior.
“Assessing, whether sampler has converged”.
-How many initial iterations need to be discarded in orderthat remaining samples are drawn from a distributionclose enough to the true stationary distribution to beusable for estimation and inference?
Topics. WinBUGS
Convergence assessment
How many initial iterations need to be discarded in orderthat remaining samples are drawn form a distributionclose enough to the true stationary distribution to beusable for estimation and inference?
-“burn-in”
(0)(1)., (m)(m+1)., (N).
”burn in”
sample
-Once we are drawing from the right distribution, howmany samples are needed in order to provide the desiredprecision in estimation and inference?
Choosing model parameterizations and MCMCalgorithms that will lead to convergence, in a reasonableamount of time.
Topics. WinBUGS
N-m
²
Monte Carlo error:
On “stats” output
Similar to standard error of the mean but adjusted forautocorrelated sample
It will get smaller as more iterations are run
Use it to guide your decisions as to how many iterationsyou need to run after burn-in is done
Monte Carlo errors
Monte Carlo errors
Decide whether to
- Discard some initial burn-in iterations and use remainingsampler output for inference
- or take some corrective action
Run more iterations
Change parameterization of model
Using MCMC for Bayesian inference
1.Specify a Bayesian model
2.Construct a Markov chain whose target distribution isthe joint posterior distribution of interest
3.Run one or more chain(s) as long as you can stand it
4.Assess convergence
 - Burn-in
- Monte Carlo error
5.  Inference
History plots: Early graphical methods of convergenceassessment
Trajectories of sampler output for each model unknown
Can quickly reveal failure to reach stationarity
Can give qualitative information about sampler behaviour
Cannot confirm that convergence have occurred
Using MCMC for Bayesian inference
Monitor
-All types of model parameters, not only parameters ofsubstantive interest
-Sample paths graphically
-Autocorrelations
-Cross-correlations between parameters
Apply more than one diagnostic, including one or morethat uses information about the specific model.
Conclusions
MCMC methods have enabled the fitting of complex,realistic models.
Use of MCMC methods requires careful attention to
-Model parameterization
-MCMC sampler algorithms
-Choice of initial values
-Convergence assessment
-Output Analysis
Ongoing research in theoretical verification ofconvergence, MCMC acceleration, and exact samplingholds great promise.
WinBUGS
WinBUGS
Where to get the WinBUGS software
From the web site of the MRC Biostatistics Unit inCambridge.  The BUGS home page is
http://www.mrc-bsu.cam.ac.uk/bugs/Welcome.html
Once you have downloaded the files you need to emailthe BUGS project for a key that will let you use the fullversion.
Manuals
-The WinBUGS manual available online.
-WinBUGS examples, volumes 1 and 2 with explanations,available online.
Specifying the model
The first stage in model fitting is to specify your model inthe BUGS language. This can either be done graphically(and then code written form your graph) or in the BUGSlanguage.
Graphically – DOODLE
Specifying the model
Starting WinBUGS
-Click on the WinBUGS icon or program file. You will get amessage about the license conditions that you can read,and then close. Now explores the menus.
-HELP – you see manuals and examples.
-FILE – allows you to open existing files, or to start a newfile to program your own example in the BUGS language
-DOODLE – NEW is what you need to use to start a filefor a new model specified in graphical format
Specifying the model
Example – a simple proportion
Inference is required for the proportion (pi) of people whoare willing to pay a certain amount to visit a park.
X = (0,0,0,0,1,0,0,0,1,1,1,0,1,1,1) ( 1: yes, 0: no)
Sample size = 15
 x1x2., xiid Bin(1, or Bernouilli()
Example – a simple proportion
Press ‘ok’
Starting WinBUGS
DODDLE NEW
Example – a simple proportion
Basic instructions
Click left mouse button to create a “doodle” with
Click CTRL + Supr to delete a “doodle”
To create a “plate” press
CLICK + CTRL
Click CTRL + Supr to delete a “plate”
Nodes:Constants
Stochastic nodes
Deterministic nodes
The relationship between node: Arrows
stochastic dependence
logical function
To create narrow we have to illuminate the “destiny”node and the press CTRL CLICK on the “origin” node.
If we repeat the process we have to repeat the process.
Example – a simple proportion
Example – a simple proportion
We have to create “node” for each variable or constant included
In the model:
An oval for stochastic nodes (we choose
the density and the parameters)
rectangle for the constants
4Nodes:
4Plate for the node xi
 Now we add the arrows that represents the relations
between nodes:
Example – a simple proportion
Copy and paste thedoodle in a new file
Example – a simple proportion
Data
Now you will have to enter the data.The following listformat will do  (notice that WinBUGS is case sensitive withthe capital letters).
Initial values (opcional, WinBUGS can generate them)
list(n 15, alpha 1, beta 1, x=c(0, 0, 0, 0, 1, ...))
list(phi =0.1)
Example – a simple proportion
Specifying the model
The first stage in model fitting is to specify your model in theBUGS language (doodle or code).
You then go to the model menu and you will get a specificationtool with buttons.
Click on the check model button. Anyerror messages will appear in the greybar at the bottom of the screen. If all iswell, you will get the message “model issyntactically correct”. You will aso seethat the compile and load data buttonswill have their text darkened. You areready for the next step.
Example – a simple proportion
Load Data
Data can be entered either in list format or in rectangularformat (columns). Once you have typed in your data,highlight either the word list. You can use the load databutton on the specification tool to read the data.
Compile
You are now ready to compile your model using compilebutton on the specification tool. Again, check errormessages. If all is well you will get a “model compiled”message.
Example – a simple proportion
Initial values
All nodes that are not given as data, or derived from othernodes, need to be given initial values. This is done with thespecification tool menu either by setting them specificallyform values you type in (set inits button) or by generatinga random value form the prior (gen inits button).
WinBUGS 1.4 allows you to run more than one chain at thesame item; see specification tool above. If you want morechains you will need to set different initial values for eachone.
Example – a simple proportion
Generating the samples - updating
You are now ready to generate samples and to examine thesimulated output. To start the sampler, go to model and thenupdate and you will get an updating tool.
You can select howmany updates you get for eachpress of the update button andhow often the screen isrefreshed to show howsampling is proceeding.
Updating does the sampling, but does not store any values. InMCMC methods you usually want to run the sampler for sometime (perhaps 10000 iterations) to be sure it is stable beforeyou start storing values.
Example – a simple proportion
Storing values and summarising results
After an initial run values go to the inference menu andsamples and you will get a sample monitoring tool.
You start by entering the parameters you want to monitor inthe node box, and for each one press ser. If you also presstrace you will see a plot of the samples as they are generated.Now go back to the updating tool and generate somesamples.
Example – a simple proportion
Now go back to the sampling tool to look at the various waysof displaying your results or summary statistics. The mostuseful buttons are:
history – shows you a plot of all the samples you havegenerated
density – gives a kernel density estimate of the posterior
stats – gives summary statistics including mean, s.d., medianand percentiles that can be set with the panel on the right.These can be used for credible intervals. You will also get aMonterCarlo error for the mean that will indicate how well themean of the posterior has been estimated form your number ofsamples.
AutoC – a plot of the autocorrelation in the chains
Ü “click” in “stats”:
 mean 0.4693 (real mean= 0.4705)
 median 0.4683
 Bayesian interval 95% (0.246, 0.6988)
 MC error 0.001148
Example – a simple proportion
Results:
Ü “click” in “density”:
Ü “click” in “history”:
(all the chain)
Example – a simple proportion
Exercise:
Repeat the analysis 10000 new simulations
Example – a simple proportion
Code: Doodle – Write Code