Introduction To Bayesian Monte Carlo Methods in WINBUGS
Introduction To Bayesian Monte Carlo Methods in WINBUGS
Summary
1. Probability as a means of representing uncertainty
7. Examples
1-1 1-2
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
1-3 1-4
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
1-5 1-6
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
0 1 2 3 4 5
0 1 2 3 4 5
0.8
0.8
0.8
0.4
0.4
0.4
0.0
0.0
0.0
0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
success rate success rate success rate
0.14
0.4
0.4
0 1 2 3 4 5
12
0 1 2 3 4 5
0.2
0.2
0.06
4
0.0
0.0
0.0
0
1-7 1-8
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Gamma[1,b] distribution is exponential with mean 1/b • A Beta[9.2,13.8] distribution has these properties
1-9 1-10
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Making predictions
Before observing a quantity Y , can provide its predictive distribution by integrating
out unknown parameter
!
p(Y ) = p(Y |θ)p(θ)dθ.
probability of response
Beta[9.2, 13.8] prior distribution supporting response rates between 0.2 and 0.6,
1-11 1-12
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
3
the complex form
density
density
Γ(a + b) n Γ(a + yn )Γ(b + n − yn )
2
p(yn ) = .
Γ(a)Γ(b) yn Γ(a + b + n)
1
a
E(Yn ) = n
a+b
0
If a = b = 1 (Uniform distribution), p(yn )is uniform over 0,1,...,n. 0.0 0.4 0.8 0 5 10 15 20
probability of response number of successes
But in WinBUGS we can just write
theta ~ dbeta(a,b)
Y ~ dbin(theta,n) (a) is the Beta prior distribution
(b) is the predictive Beta-Binomial distribution of the number of successes Y in
and the integration is automatically carried out and does not require algebraic the next 20 trials
cleverness. From Beta-binomial distribution, can calculate P (Yn ≥ 15) = 0.015.
1-13 1-14
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Example: a Monte Carlo approach to estimating tail-areas of distributions A simulation approach uses a computer to toss the coins!
An algebraic approach:
10 ' (
& 1
Pr(≥ 8 heads) = p z|π = , n = 10
z=8
2
' ( 8 ' (2 ' (9 ' ( 1 ' (10 ' (0
10 10 10
= 1 1
+ 1 1
+ 1 1
8 2 2 9 2 2 10 2 2
= 0.0547.
0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10
A physical approach would be to repeatedly throw a set of 10 coins and count Number of heads Number of heads Number of heads
the proportion of throws that there were 8 or more heads.
Proportion with 8 or more ’heads’ in 10 tosses:
(a) After 100 ’throws’ (0.02); (b) after 10,000 throws (0.0577); (c) the true
Binomial distribution (0.0547)
1-15 1-16
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
1-17 1-18
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Running WinBUGS for Monte Carlo analysis (no Using WinBUGS for Monte Carlo
data) The model for the ’coin’ example is
1. Open Specification tool from Model menu. Y ∼ Binomial(0.5, 10)
2. Program responses are shown on bottom-left of screen. and we want to know P (Y ≥ 8).
3. Highlight model by double-click. Click on Check model. This model is represented in the BUGS language as
model{
4. Click on Compile.
Y ~ dbin(0.5,10)
5. Click on Gen Inits. P8 <- step(Y-7.5)
}
6. Open Update from Model menu, and Samples from Inference menu.
P8 is a step function which will take on the value 1 if Y -7.5 is ≥ 0, i.e. Y is 8 or
7. Type nodes to be monitored into Sample Monitor, and click set after each. more, 0 if 7 or less.
Running this simulation for 100, 10000 and 1000000 iterations, and then taking
8. Type * into Sample Monitor, and click trace to see sampled values.
the empirical mean of P8, provided the previous estimated probabilities that Y will
9. Click on Update to generate samples. be 8 or more.
10. Type * into Sample Monitor, and click stats etc to see results on all monitored
nodes.
1-19 1-20
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• <- represents logical dependence, e.g. m <- a + b*x • p <- step(x-.7) = 1 if x ≥ 0.7, 0 otherwise. Hence monitoring p and recording
• ~ represents stochastic dependence, e.g. r ~ dunif(a,b) its mean will give the probability that x≥ 0.7.
• Can use arrays and loops
• p <- equals(x,.7) = 1 if x = 0.7, 0 otherwise.
for (i in 1:n){
r[i] ~ dbin(p[i],n[i]) • tau <- 1/pow(s,2) sets τ = 1/s2 .
p[i] ~ dunif(0,1)
} √
• s <- 1/ sqrt(tau) sets s = 1/ τ .
• Some functions can appear on left-hand-side of an expression, e.g.
)
• p[i,k] <- inprod(pi[], Lambda[i,,k]) sets pik = j πj Λijk . inprod2 may be
logit(p[i])<- a + b*x[i] faster.
log(m[i]) <- c + d*y[i]
• mean(p[]) to take mean of whole array, mean(p[m:n]) to take mean of elements • See ’Model Specification/Logical nodes’ in manual for full syntax.
m to n. Also for sum(p[]).
• dnorm(0,1)I(0,) means the prior will be restricted to the range (0, ∞).
1-21 1-22
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
NB. The normal is parameterised in terms of its mean and precision = 1/ variance # In BUGS syntax:
= 1/sd2. model{
theta ~ dbeta(9.2,13.8) # prior distribution
See ’Model Specification/The BUGS language: stochastic nodes/Distributions’ y ~ dbin(theta,20) # sampling distribution
in manual for full syntax. P.crit <- step(y-14.5) # =1 if y >= 15, 0 otherwise
1-23 1-24
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Note that the mean of the 0-1 indicator P.crit provides the estimated tail-area
probability.
1-25 1-26
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Graphical representation of models Script for running Drug Monte Carlo example
Run from Model/Script menu
1-27 1-28
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Example: Power — uncertainty in a power calculation Suppose we wish to express uncertainty concerning both θ and σ, e.g.
1-29 1-30
Lecture 2.
Introduction to conjugate Bayesian
inference
For n= 63, the median power is 80%, and a trial of 63 patients per group could
be seriously underpowered. There is a 37% chance that the power is less than
70%.
1-31 2-1
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
2-2 2-3
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
A clinical trial is carried out to collect evidence about an unknown ‘treatment • a reasonable opinion concerning the plausibility of different values of the
effect’ treatment effect excluding the evidence from the trial (the prior distribution)
Conventional analysis • the support for different values of the treatment effect based solely on data
from the trial (the likelihood),
• p-value for H0: treatment effect is zero
and to combine these two sources to produce
• Point estimate and CI as summaries of size of treatment effect
• a final opinion about the treatment effect (the posterior distribution)
Aim is to learn what this trial tells us about the treatment effect
Bayesian analysis The final combination is done using Bayes theorem, which essentially weights
the likelihood from the trial with the relative plausibilities defined by the prior
• Asks: ‘how should this trial change our opinion about the treatment effect?’ distribution
One can view the Bayesian approach as a formalisation of the process of learning
from experience
2-4 2-5
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Posterior distribution forms basis for all inference — can be summarised to provide
Bayes theorem and its link with Bayesian inference
• point and interval estimates of treatment effect
Bayes’ theorem Provable from probability axioms
• point and interval estimates of any function of the parameters
Let A and B be events, then
• probability that treatment effect exceeds a clinically relevant value
p(B|A)p(A)
• prediction of treatment effect in a new patient p(A|B) = .
p(B)
• prior information for future trials - )
If Ai is a set of mutually exclusive and exhaustive events (i.e. p( i Ai ) = i p(Ai ) =
1), then
• inputs for decision making
• .... p(B|Ai)p(Ai)
p(Ai |B) = ) .
j p(B|Aj )p(Aj )
2-6 2-7
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• A new HIV test is claimed to have “95% sensitivity and 98% specificity” • The vital issue is how should this test result change our belief that patient is
• In a population with an HIV prevalence of 1/1000, what is the chance that HIV positive?
patient testing positive actually has HIV?
• The disease prevalence can be thought of as a ‘prior’ probability (p = 0.001)
Let A be the event that patient is truly HIV positive, A be the event that they
are truly HIV negative. • Observing a positive result causes us to modify this probability to p = 0.045.
Let B be the event that they test positive. This is our ‘posterior’ probability that patient is HIV positive.
We want p(A|B). • Bayes theorem applied to observables (as in diagnostic testing) is uncontro-
versial and established
“95% sensitivity” means that p(B|A) = .95.
“98% specificity” means that p(B|A) = .02. • More controversial is the use of Bayes theorem in general statistical analyses,
where parameters are the unknown quantities, and their prior distribution
Now Bayes theorem says needs to be specified — this is Bayesian inference
p(B|A)p(A)
p(A|B) = .
p(B|A)p(A) + p(B|A)p(A)
.95×.001
Hence p(A|B) = .95×.001+.02×.999
= .045.
Thus over 95% of those testing positive will, in fact, not have HIV.
2-8 2-9
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
! in the frequentist framework, parameters are fixed non-random quantities The prior distribution p(θ), expresses our uncertainty about θ before seeing the
and the probability statements concern the data data.
The posterior distribution p(θ | x), expresses our uncertainty about θ after seeing
the data.
2-10 2-11
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Inference on proportions using a continuous prior To represent external evidence that some response rates are more plausible than
others, it is mathematically convenient to use a Beta(a, b) prior distribution for θ
Suppose we now observe r positive responses out of n patients.
p(θ) ∝ θa−1 (1 − θ)b−1
Assuming patients are independent, with common unknown response rate θ, leads
to a binomial likelihood Combining this with the binomial likelihood gives a posterior distribution
n p(θ | r, n) ∝ p(r | θ, n)p(θ)
p(r|n, θ) = θr (1 − θ)n−r ∝ θr (1 − θ)n−r
r ∝ θr (1 − θ)n−r θa−1 (1 − θ)b−1
θ needs to be given a continuous prior distribution. = θr+a−1 (1 − θ)n−r+b−1
Suppose that, before taking account of the evidence from our trial, we believe all ∝ Beta(r + a, n − r + b)
1
values for θ are equally likely (is this plausible?) ⇒ θ ∼ Unif(0, 1) i.e. p(θ) = 1−0 =1
Posterior is then
p(θ|r, n) ∝ θr (1 − θ)(n−r) × 1
This has form of the kernel of a Beta(r+1, n-r+1) distribution (see lect 1), where
Γ(a + b) a−1
θ ∼ Beta(a, b) ≡ θ (1 − θ)b−1
Γ(a)Γ(b)
2-12 2-13
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• When the prior and posterior come from the same family of distributions the • Recall example from lecture 1, where we consider early investigation of a new
prior is said to be conjugate to the likelihood drug
– Occurs when prior and likelihood have the same ‘kernel’
• Experience with similar compounds has suggested that response rates be-
• Recall from lecture 1 that a Beta(a, b) distribution has tween 0.2 and 0.6 could be feasible
mean = a/(a/ + b), • We interpreted this as a distribution with mean = 0.4, standard deviation 0.1
variance = ab/ (a + b)2(a + b + 1)
0
and showed that a Beta(9.2,13.8) distribution has these properties
Hence posterior mean is E(θ|r, n) = (r + a)/(n + a + b)
• Suppose we now treat n = 20 volunteers with the compound and observe
y = 15 positive responses
• a and b are equivalent to observing a priori a − 1 successes in a + b − 2 trials
→ can be elicited
• With fixed a and b, as r and n increase, E(θ|r, n) → r/n (the MLE), and the
variance tends to zero
– This is a general phenomenon: as n increases, posterior distribution gets
more concentrated and the likelihood dominates the prior
2-14 2-15
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
(a) (b)
Beta(9.2, 13.8) prior distribution
supporting response rates
between 0.2 and 0.6
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0 10 20 30 40
Parameters of the Beta (a) Beta posterior distribution after having observed 15 successes in 20 trials
distribution are updated to (b) predictive Beta-Binomial distribution of the number of successes ỹ40 in the
(a+15, b+20-15) = (24.2, 18.8): next 40 trials with mean 22.5 and standard deviation 4.3
mean 24.2/(24.2+18.8) = 0.56
Suppose we would consider continuing a development program if the drug man-
0.0 0.2 0.4 0.6 0.8 1.0 aged to achieve at least a further 25 successes out of these 40 future trials
Probability or response
From Beta-binomial distribution, can calculate P (ỹ40 ≥ 25) = 0.329
2-16 2-17
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Can just specify the prior and likelihood separately # In BUGS syntax:
• WinBUGS contains algorithms to evaluate the posterior given (almost) arbi- # Model description ’
trary specification of prior and likelihood model {
theta ~ dbeta(a,b) # prior distribution
– posterior doesn’t need to be closed form y ~ dbin(theta,m) # sampling distribution
– but can (usually) recognise conjugacy when it exists y.pred ~ dbin(theta,n) # predictive distribution
P.crit <- step(y.pred-ncrit+0.5) # =1 if y.pred >= ncrit, 0 otherwise
}
2-18 2-19
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Alternatively, in this simple example, we could have put all data and constants
into model description:
model{
theta ~ dbeta(9.2,13.8) # prior distribution
y ~ dbin(theta,20) # sampling distribution
y.pred ~ dbin(theta,40) # predictive distribution
P.crit <- step(y.pred-24.5) # =1 if y.pred >= ncrit, 0 otherwise
Note that adding data to a model is simply extending the graph. y <- 15
}
2-20 2-21
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
list(N=12,n = c(47,148,119,810,211,196,
148,215,207,97,256,360),
r = c(0,18,8,46,8,13,9,31,14,8,29,24))
2-22 2-23
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
WinBUGS output
Running WinBUGS for MCMC analysis (single chain)
1. Open Specification tool from Model menu.
2. Program responses are shown on bottom-left of screen.
3. Highlight model by double-click. Click on Check model.
4. Highlight start of data. Click on Load data.
5. Click on Compile.
6. Highlight start of initial values. Click on Load inits.
7. Click on Gen Inits if more initial values needed.
8. Open Update from Model menu.
9. Click on Update to burn in.
10. Open Samples from Inference menu.
11. Type nodes to be monitored into Sample Monitor, and click set after each.
12. Perform more updates.
13. Type * into Sample Monitor, and click stats etc to see results on all monitored
nodes.
2-24 2-25
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
WinBUGS output and exact answers Bayesian inference using the Normal distribution
node mean sd MC error 2.5% median 97.5% start sample Known variance, unknown mean
theta 0.5633 0.07458 4.292E-4 0.4139 0.5647 0.7051 1001 30000
y.pred 22.52 4.278 0.02356 14.0 23.0 31.0 1001 30000 Suppose we have a sample of Normal data xi ∼ N(θ, σ 2 ) (i = 1, ..., n).
P.crit 0.3273 0.4692 0.002631 0.0 0.0 1.0 1001 30000
For now assume σ 2 is known and θ has a Normal prior θ ∼ N(µ, σ 2/n0)
Exact answers from conjugate analysis
Same standard deviation σ is used in the likelihood and the prior. Prior variance
• θ: mean 0.563 and standard deviation 0.075 is based on an ‘implicit’ sample size n0
MCMC results are within Monte Carlo error of the true values
2-26 2-27
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
2-28 2-29
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Suppose historical data on THM levels in other zones supplied from the same
source showed that the mean THM concentration was 120 µg/l with standard
deviation 10 µg/l
2 /0.25)
• so our prior can be written as θz ∼ Normal(120, σ[e]
giving 95% interval for θz of 122.4 to 135.4µg/l 80 100 120 140 160 180
2-30 2-31
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Denoting the posterior mean and variance as µn = (n0µ + nx)/(n0 + n) and σn2 = • Suppose the water company will be fined if THM levels in the water supply
σ 2 /(n0 + n), the predictive distribution for a new observation x̃ is exceed 145µg/l
So the predictive distribution is centred around the posterior mean with variance
equal to sum of the posterior variance and the sample variance of x̃
2-32 2-33
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
The kernel of the Poisson likelihood (as a function of µ) has the same form as
that of a Gamma(a, b) prior for µ: ∝ µa+nx−1 e−(b+n)µ
ba a−1 −bµ
p(µ) = µ e
Γ(a) = Gamma(a + nx, b + n).
The posterior is another (different) Gamma distribution.
Note: A Gamma(a, b) density has mean a/b and variance a/b2
The Gamma distribution is said to be the conjugate prior.
' ( ' (
a + nx n a n
E(µ | x) = = x + 1−
b+n n+b b n+b
So posterior mean is a compromise between the prior mean a/b and the MLE x
2-34 2-35
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• If the hits are random, a Poisson distribution with constant hit rate θ should
fit the data
2-36 2-37
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
These observations are generally true, when the MLE exists and is unique • Computations for non-conjugate priors are harder, but possible using MCMC
(see next lecture)
2-38 2-39
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Lee (2004) (Good intro to Bayesian inference; more mathematical than Berry;
• Interfaces developed for R, Splus, SAS, Matlab 3rd edition contains WinBUGS examples)
• See www.mrc-bsu.cam.ac.uk/bugs/welcome.shtml Bernardo and Smith (1994) (Advanced text on Bayesian theory)
• Andrew Gelman’s bugs function for R is most developed - reads in data, writes
script, monitors output etc. Now packaged as R2WinBUGS.
2-40 2-41
Introduction to Bayesian Analysis and WinBUGS
• p(x|θ ) and p(θ ) will often be available in closed form, but p(θ |x) is usually
Lecture 3. not analytically tractable, and we want to
.. .
Introduction to MCMC – obtain marginal posterior p(θi |x) =
notes the vector of θ’s excluding θi
... p(θ |x) dθ (−i) where θ (−i) de-
.
– calculate
.∞ properties of p(θi |x), such as mean (= θi p(θi |x)dθi ), tail areas
(= T p(θi |x)dθi ) etc.
3-1 3-2
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
We have already seen that Monte Carlo methods can be used to simulate values • We want samples from joint posterior distribution p(θ |x)
from prior distributions and from closed form posterior distributions
• Independent sampling from p(θ |x) may be difficult
If we had algorithms for sampling from arbitrary (typically high-dimensional) pos-
terior distributions, we could use Monte Carlo methods for Bayesian estimation: • BUT dependent sampling from a Markov chain with p(θ |x) as its stationary
• Suppose we can draw samples from the joint posterior distribution for θ , i.e. (equilibrium) distribution is easier
(θ1(1) , ..., θk(1) ), (θ1(2) , ..., θk(2) ), ..., (θ1(N ) , ..., θk(N ) ) ∼ p(θ |x) • A sequence of random variables θ(0) , θ(1) , θ(2) , ... forms a Markov chain if
θ(i+1) ∼ p(θ|θ(i) ) i.e. conditional on the value of θ(i) , θ(i+1) is independent
• Then of θ(i−1) , ..., θ(0)
– θ1(1) , ..., θ1(N ) are a sample from the marginal posterior p(θ1|x) • Several standard ‘recipes’ available for designing Markov chains with required
(i) stationary distribution p(θ |x)
– E(g(θ1)) = g(θ1)p(θ1 |x)dθ1 ≈ N1 N
. )
i=1 g(θ1 )
– Metropolis et al. (1953); generalised by Hastings (1970)
→ this is Monte Carlo integration – Gibbs Sampling (see Geman and Geman (1984), Gelfand and Smith
(1990), Casella and George (1992)) is a special case of the Metropolis-
→ theorems exist which prove convergence in limit as N → ∞ even if the sample Hastings algorithm which generates a Markov chain by sampling from full
is dependent (crucial to the success of MCMC) conditional distributions
– See Gilks, Richardson and Spiegelhalter (1996) for a full introduction and
many worked examples.
3-3 3-4
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Let our vector of unknowns θ consist of k sub-components θ = (θ1 , θ2 , ..., θk ) Example with k = 2
!2
1) Choose starting values θ1(0) , θ2(0) , ..., , θk(0)
(1)
!
p(!)
2) Sample θ1(1) from p(θ1 |θ2(0) , θ3(0) , ..., , θk(0) , x) !
(2)
(0)
!
Sample θ2(1)
from p(θ2 |θ1(1) , θ3(0) , ..., , θk(0) , x)
.....
!1
Sample θk(1) from p(θk |θ1(1) , θ2(1) , ..., , θk−1
(1)
, x)
3-5 3-6
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Checking convergence
Using MCMC methods This is the users responsibility!
There are two main issues to consider
• Note: Convergence is to target distribution (the required posterior), not to
a single value.
• Convergence (how quickly does the distribution of θ (t) approach p(θ |x)?)
• Once convergence reached, samples should look like a random scatter about
• Efficiency (how well are functionals of p(θ |x) estimated from {θ (t) }?) a stable mean value
3-7 3-8
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Convergence assessed by quantifying whether sequences are much further Fit a logistic curve with ‘centred’ covariate (xi − x):
apart than expected based on their internal variability
ri ∼ Bin(pi, ni )
logit pi = α + β(xi − x)
• Diagnostic uses components of variance of the multiple sequences
α ∼ N(0, 10000)
β ∼ N(0, 10000)
3-9 3-10
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
3-11 3-12
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
3-13 3-14
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
bivariate posteriors
1.0
centred un-centred
alpha alpha
0.0
1.5 -40.0
-50.0
1 20000 iteration 40000 60000 1.0
-60.0
0.5
Drop first 40,000 iterations as burn-in -70.0
0.0 -80.0
node mean sd MC error 2.5% median 97.5% start sample
20.0 30.0 40.0 25.0 35.0
beta 33.97 2.955 0.1734 28.7 33.89 40.3 40001 40000 beta beta
3-15 3-16
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
3-17 3-18
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Classical inference cannot provide probability statements about parameters • Classical inference about a function of the parameters g(θ) requires con-
(e.g. p-value is not Pr(H0 true), but probability of observing data as or more struction of a specific estimator of g(θ). Obtaining appropriate error can be
extreme than we obtained, given that H0 is true) difficult.
• In Bayesian inference, it is simple to calculate e.g. Pr(θ > 1): • Easy using MCMC: just calculate required function g(θ) as a logical node at
each iteration and summarise posterior samples of g(θ)
= Area under posterior distribution curve to the right of 1
= Proportion of values in posterior sample of θ which are > 1 In dose-response example, suppose we want to estimate the ED95: that is the
dose that will provide 95% of maximum efficacy.
logit 0.95 = α + β(ED95 − x)
Posterior Distribution of theta
• In WinBUGS use the step function: ED95 = (logit 0.95 − α)/β + x
p.theta <- step(theta - 1)
Shaded Area
= Simply add into model
Prob(theta>1) • For discrete parameters, may also be
interested in Pr(δ = δ0 ): ED95 <- (logit(0.95) - alpha)/beta + mean(x[])
p.delta <- equals(delta, delta0)
node mean sd MC error 2.5% median 97.5% start sample
0.5 1.0 1.5 2.0 2.5 3.0
• Posterior means of p.theta and p.delta ED95 1.857 0.007716 8.514E-5 1.843 1.857 1.874 1001 10000
theta give the required probabilities
3-19 3-20
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Recent trend in UK towards ranking ‘institutional’ performance e.g. schools, • 22 trials of beta-blockers used in WinBUGS manual to illustrate random-
hospitals effects meta-analysis.
• Might also want to rank treatments, answer ‘which is the best‘ etc • Consider just treatment arms: which trial has the lowest mortality rate?
• Rank of a point estimate is a highly unreliable summary statistic • Assume independent ‘Jeffreys’ beta[0.5, 0.5] prior for each response rate.
3-21 3-22
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
[14]
[2]
[4]
[2] 0.0
[4] [14]
[21] [21]
[10] [6] 0.0 10.0 20.0
[11] [10]
[6]
[3]
[11]
[3]
Trial
[5] [5]
[1] [1]
[9]
[8]
[9]
[8] Ranking methods may be useful when
[7] [7]
[17] [17]
[20] [20]
[16]
[15]
[16]
[15]
• comparing alternative treatments
[12] [12]
• comparing subsets
0.0 0.1 0.2 0.0 10.0 20.0 • comparing response-rates, cost-effectiveness or any summary measure
3-23 3-24
Introduction to Bayesian Analysis and WinBUGS
Further reading
Gelfand and Smith (1990) (key reference to use of Gibbs sampling for Bayesian
calculations)
3-25 4-1
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Specify form of relationship between response and explanatory variables • Easy to make inference about functions of regression parameters and/or pre-
dictions
• Specify prior distributions for regression coefficients and any other unknown
(nuisance) parameters • Easily extended to handle missing data and covariate measurement error
4-2 4-3
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
4-4 4-5
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Specifying categorical covariates using the BUGS language BUGS model code is then
4-6 4-7
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
2. Alternatively, input explanatory variable as single vector coded by its level: Then use ’double indexing’ feature of BUGS language
4-8 4-9
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Raw data
Change in theft rate per 1% increase in police manpower
15
15
10
3.0
change in theft rate
!5
0.1
0.05 95% interval (-5.1, 6.6)
!10
!10
0.0
-20.0 -10.0 0.0 10.0
4-10 4-11
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• 95% intervals for DIST effect both include zero • Influential point corresponds to 20th Precinct
→ drop DIST from model (see later for Bayesian model comparison criteria)
• During 2nd period, manpower assigned to this Precinct was experimentally
Change in theft rate per 1% increase in police manpower increased by about 40%
beta chains 1:2 sample: 10000
4.0 Posterior mean -0.18 • No experimental increases in any other Precinct
3.0
2.0 95% interval (-0.39, 0.04)
1.0
0.0 → Robustify model assuming t-distributed errors
-1.0 -0.5 0.0
for (i in 1:23) {
THEFT[i] ~ dt(mu[i], tau, 4) # robust likelihood (t on 4 df)
mu[i] <- alpha + beta*MAN[i]
}
alpha ~ dnorm(0, 0.00001)
beta ~ dnorm(0, 0.00001)
change in theft rate
20.0
tau ~ dgamma(0.001, 0.001)
10.0 sigma2 <- 1/tau
Observed data
0.0
Fitted value, dummy <- DIST[1] # ensures all variables in data file appear in model code
-10.0
mu[i]
-20.0
95% interval for
-20.0 0.0 20.0 40.0 mu[i]
% change in police manpower
4-12 4-13
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
10.0
2.0 – Equivalent to fitting separate (saturated) model to Precinct 20
1.0
0.0
0.0 for(i in 1:23) {
-1.0 -0.5 0.0
-10.0 THEFT[i] ~ dt(mu[i], tau, 4) # robust likelihood (t on 4 df)
Posterior mean -0.18 mu[i] <- alpha + beta*MAN[i] + delta*PREC20[i] # separate term for precinct 20
-20.0
95% interval (-0.39, 0.04) }
-20.0 0.0 20.0 40.0 alpha ~ dnorm(0, 0.000001)
beta ~ dnorm(0, 0.000001)
delta ~ dnorm(0, 0.000001)
Model with Student t errors tau ~ dgamma(0.001, 0.001)
sigma2 <- 1/tau # residual error variance
beta chains 1:2 sample: 10000
4.0 dummy <- DIST[1] # ensures all variables in data file appear in model code
20.0 3.0
2.0
10.0 1.0 # Create indicator variable for precinct 20
0.0 # (alternatively, could add this variable to data file)
0.0
-1.0 -0.5 0.0 for(i in 1:13) { PREC20[i] <- 0 }
-10.0
PREC20[14] <- 1
-20.0 Posterior mean -0.13
for(i in 15:23) { PREC20[i] <- 0 }
-20.0 0.0 20.0 40.0 95% interval (-0.36, 0.18)
4-14 4-15
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
4-16 4-17
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
4-18 4-19
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• p(θ) = N(0, V ) where V is an appropriately large value for the variance, Jeffreys proposed defining a non-informative prior for θ as p(θ) ∝ I(θ)1/2 where
e.g. N(0, 100000) I(θ) is Fisher information for θ
6 2 7 8' (2 9
∂ log p(X|θ) ∂ log p(X|θ)
• Recall that WinBUGS parameterises Normal in terms of mean and precision, I(θ) = −IEX|θ = IEX|θ
so vague normal prior will be, e.g. theta ~ dnorm(0, 0.00001) ∂θ2 ∂θ
• High curvature occurs wherever small changes in parameter values are asso-
ciated with large changes in the likelihood
– Jeffreys’ prior gives more weight to these parameter values
– data provide strong information about parameter values in this region
– ensures data dominate prior everywhere
4-20 4-21
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
s n
log p(x|v) = −n/2 log v − ⇒ I(v) = Prior best placed on interpretable parameters
2v 2v 2
v −1
So Jeffreys’ prior for v is ∝ Great caution needed in complex models that an apparently innocuous uniform
This improper distribution is approximated by a Gamma(), )) distribution with prior is not introducing substantial information
)→0
Note: p(v) ∝ v −1 is equivalent to a uniform prior on log v ‘There is no such thing as a ‘noninformative’ prior. Even improper priors give
information: all possible values are equally likely’ (Fisher, 1996)
4-22 4-23
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Uniform prior on a wide range, or a Normal prior with a large variance can • Sample variance σ 2: standard ‘reference’ (Jeffreys’) prior
be used, e.g.
1
p(σ 2 ) ∝ ∝ Gamma(0,0)
θ ∼ Unif(−100, 100) theta ~ dunif(-100, 100) σ2
θ ∼ Normal(0, 100000) theta ~ dnorm(0, 0.00001) p(log(σ)) ∝ Uniform(−∞, ∞)
Prior will be locally uniform over the region supported by the likelihood
• Note that Jeffreys’ prior on the inverse variance (precision), τ = σ −2 is also
– ! remember that WinBUGS parameterises the Normal in terms of mean and
1
precision so a vague Normal prior will have a small precision p(τ ) ∝
∝ Gamma(0, 0)
τ
– ! ‘wide’ range and ‘small’ precision depend on the scale of measurement which may be approximated by a ‘just proper’ prior
of θ
τ ∼ Gamma(), ))
This is also the conjugate prior and so is widely used as a ‘vague’ proper prior
for the precision of a Normal likelihood
In BUGS language: tau ~ dgamma(0.001, 0.001)
or alternatively tau <- 1/exp(logsigma2); logsigma2 ~ dunif(-100, 100)
4-24 4-25
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Posterior from one problem (today’s temperature) becomes the prior for an- • Competing theories:
other problem (tomorrow’s temperature)
– Wallerstein: union density depends on the size of the civilian labour force
(LabF)
• Priors elicited from experts can be used to take account of domain-specific
knowledge, judgement, experience – Stephens: union density depends on industrial concentration (IndC)
– Note: These two predictors correlate at -0.92.
• Priors can also be used to impose constraints on variables (e.g. based on
physical or assumed properties) and bound variables to plausible ranges
4-26 4-27
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Data: n = 20 countries with a continuous history of democracy since World Trace plots, posterior estimates and MC error for regression coefficients
War II
• Variables: Union density (Uden), (log) labour force size (LabF), industrial
Without centering covariates
mean sd MC
concentration (IndC), left wing government (LeftG), measured in late 1970s b3 chains 1:2 error
100.0
• Fit linear regression model to compare theories b0 61.7 62.8 5.19
50.0
Udeni ∼ N(µi , σ ) 2 0.0 b1 0.27 0.08 .002
µi = b0 + b1(LeftGi − LeftG) + b2(LabFi − LabF) + b3(IndCi − IndC) -50.0 b2 -4.14 4.18 0.34
Vague priors: 1001 2000 4000 6000
iteration b3 12.1 20.6 1.67
1/σ 2 ∼ Gamma(0.001, 0.001)
b0 ∼ N(0, 100000)
b1 ∼ N(0, 100000) With centered covariates
mean sd MC
b2 ∼ N(0, 100000) b3 chains 1:2 error
b3 ∼ N(0, 100000) 100.0
b0 54.0 2.48 0.02
50.0
0.0 b1 0.27 0.08 .001
-50.0 b2 -6.33 3.96 0.13
1001 2000 4000 6000
iteration b3 0.98 20.2 0.67
4-28 4-29
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Posterior distribution of regression coefficients Motivation for Bayesian approach with informative priors
• Because of small sample size and multicollinear variables, not able to adjudi-
box plot: b cate between theories
[3]
40.0 • Data tend to favour Wallerstein (union density depends on labour force size),
but neither coefficient estimated very precisely
20.0 • Other historical data are available that could provide further relevant infor-
mation
[1] [2]
0.0 • Incorporation of prior information provides additional structure to the data,
which helps to uniquely identify the two coefficients
-20.0
-40.0
4-30 4-31
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Believes in negative labour force effect • Believes in positive industrial concentration effect
4-32 4-33
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Both Wallerstein and Stephens priors Posterior distribution of regression coefficients under different priors
b1 ∼ N(0.3, 0.152)
0.0
• Vague prior b0 ∼ N(0, 100000) assumed for intercept
-5.0 0.0
-10.0
-15.0 -50.0
4-34 4-35
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Comments
Multivariate responses
• Effects of LabF and IndC estimated more precisely
• In many applications, it is common to collect data on a number of different
• Both sets of prior beliefs support inference that labour-force size decreases outcomes measured on the same units, e.g.
union density
– sample survey, where respondents asked several different questions
• Only Stephens prior supports conclusion that industrial concentration in- – experiment with several different outcomes measured on each unit
creases union density
• May wish to fit regression model to each response
• Choice of prior is subjective – if no consensus, can we be satisfied that data
– May have different covariates in each regression
have been interpreted fairly?
– But errors may be correlated
– Sensitivity to priors (e.g. repeat analysis using priors with increasing – Might also wish to impose cross-equation parameter restrictions
variance) — see Practical exercises
– Sensitivity to data (e.g. residuals, influence diagnostics) — see later → Seemingly Unrelated Regressions (SUR) (Zellner, 1962)
lecture
Bayesian approach to SUR models → model vector of response for each unit as
multivariate normal (could also have robust version using multivariate t)
4-36 4-37
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Example: Analysis of compositional data • Two main modelling strategies, treating vector of proportions as the data
(sufficient statistics)
• Compositional data are vectors of proportions pi = (pi1, ..., piJ ) representing
relative contributions of each of J categories to the whole, e.g. – Model pi using a Dirichlet likelihood (multivariate generalisation of a beta
distribution)
– Proportion of income spent on different categories of expenditure ∗ assumes the ratios of “compositions” (i.e. proportions) are independent
– Proportion of electorate voting for different political parties – Multivariate logistic normal model (Aitchison, 1986) — apply additive log
– Relative abundance of different species in a habitat ratio (alr) transformation, yij = log(pij /piJ ) and model y i as multivariate
normal
– Chemical composition of a rock or soil sample
∗ allows dependence between ratios of proportions
– Proportion of deaths from different causes in a population
∗ this can be thought of as a type of SUR model
• Regression models for compositional data must satisfy two constraints
• To allow for sampling variability in the observed counts (including zero counts),
0 ≤ pij ≤ 1 model counts (rather than proportions) as multinomial (see later)
&
pij = 1
j
4-38 4-39
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
Define • Data originally analysed by Katz and King (1999), and formulated as BUGS
yij = log(pij /piJ ) example by Simon Jackman
the log ratios of proportions in each category relative to a reference category J
) • Data consist of vote proportions for Conservative (j=1), Labour (j=2) and
Note that piJ = 1 − j-=J pij , so Lib-Dem (j=3) parties from 1992 General Election for each of 521 con-
exp yij stituencies
pij = )
1+ j-=J exp yij • Additive log ratio transformation applied to proportions, taking Lib-Dem vote
as reference category
Since yij are unconstrained, can model vector y i = {yij , j -= J} as multivariate
normal • Covariates include lagged values of the log ratios from previous election, and
indicators of the incumbency status of each party’s candidate
4-40 4-41
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
## Priors for regression coefficients If we use the Wishart distrubution as a prior distribution for a precision matrix Ω
for(j in 1:2){ in sampling from Np (µ, Ω−1 ), we find, generalising the univariate case above, that
for(k in 1:6) { we get the same form for the posterior for Ω – another Wishart distribution.
beta[j,k] ~ dnorm(0, 0.000001) In view of the result above for the expectation of the Wishart distribution, we
} usually set (1/k)R to be a prior guess at the unknown true variance matrix. A
} common choice is to take k = p.
4-42 4-43
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• In BUGS language, you must specify the dimension of vectors or arrays on • Interpretation of parameter estimates on a multivariate log-odds scale is dif-
the left hand side of multivariate distributions ficult
• In above example, each row (observation) of the 521 × 2 matrix y is a vector • Using the inverse alr transformation, easy to recover estimates of expected
of length 2, hence y[i,1:2] ~ dmnorm..... proportions or predicted counts in different categories
• Likewise, prec is a 2 × 2 matrix, hence prec[1:2, 1:2] ~ dwish..... • Effect of covariates can be examined by calculating difference or ratio of
expected proportions for different values of the covariate
• You cannot specify the dimension to be a parameter (even if the value of
the parameter is specified elsewhere in the code or data file), e.g. – Using MCMC, easy to obtain uncertainty intervals for such contrasts
4-44 4-45
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
exp.mu.lab[j] <- exp(mu.lab[j]); p.lab[j] <- exp.mu.lab[j]/(1 + sum(exp.mu.lab[])) Note: results for incumbency advantage agree with those in Tomz et al (2002)
but not with Katz and King, who analysed data from 10 consecutive elections
exp.mu.open[j] <- exp(mu.open[j]); p.open[j] <- exp.mu.open[j]/(1 + sum(exp.mu.open[])) and used empirical Bayes shrinkage priors on the β coefficients across years
}
4-46 4-47
Introduction to Bayesian Analysis and WinBUGS Introduction to Bayesian Analysis and WinBUGS
• Multivariate logistic normal for compositional data relies on assumption that • Only need to change 2 lines of code
the log ratios are approximately multivariate normal
1. Likelihood:
• Katz and King (1999) argue that this assumption is not appropriate for British ## y[i,1:2] ~ dmnorm(mu[i,1:2], prec[ , ])
election data y[i,1:2] ~ dmt(mu[i,1:2], prec[ , ], nu)
2. Specify either fixed value for degrees of freedom, nu, or a suitable prior
– majority of constituencies tend to be more clustered, and a minority more
## nu <- 4
widely dispersed, than the multivariate normal implies
nu ~ dunif(2, 250)
• K&K propose replacing multivariate normal by a heavier-tailed multivariate
student t distribution
4-48 4-49
Results
Normal Student t
Expected vote (Cons), area 1 44.8% (44.2%, 45.4%) 44.7% (44.2%, 45.2%)
Expected vote (Lab), area 1 46.1% (45.3%, 46.8%) 46.4% (45.7%, 47.0%)
Expected vote (LibDem), area 1 9.1% (8.7%, 9.5%) 8.8% (8.6%, 9.3%)
incumbency advantage (Cons) −0.06% (−0.75%, 0.70%) 0.00% (-0.65%, 0.60%)
incumbency advantage (Lab) −0.30% (−1.6%, 1.0%) −0.50% (−1.7%, 0.70%)
incumbency advantage (LibDem) 8.6% (5.1%, 12.3%) 2.3% (−2.0%, 8.3%)
ρ 0.87 (0.85, 0.89) 0.87 (0.85, 0.90)
ν – 4.5 (3.4, 5.9)
DIC −480 −1220
pD 14.5 11.8
4-50