Bayes Factors: Using pseudo priors with Carlin and Chib method
This example, called Pines in the original BUGS distribution, demonstrates the Carlin and Chib method for computing a Bayes factor between two non-nested models. The data are 42 observations of a response Y together with two competing single-covariate predictors x and z. Both explain the response well on their own, with correlations against Y of about 0.93 and 0.95, and they are also strongly correlated with each other, at about 0.96. Choosing between two regressions this similar is exactly the situation in which a Bayes factor is wanted and in which ordinary posterior summaries from either model separately say nothing about the comparison.
The trick is to make the model index itself a parameter. All three variables are standardised inside the program, a categorical node j picks model 1 or model 2, and the likelihood is written once as $Y^s_i \sim \text{Normal}(\mu_{j,i}, \tau_j)$ with $\mu_{1,i} = \alpha + \beta x^s_i$ and $\mu_{2,i} = \gamma + \delta z^s_i$. Both models' parameters exist at every iteration, but which prior they see depends on j: the selected model's parameters are given their genuine vague estimation priors and are updated by the data, while the unselected model's parameters are drawn from pseudo-priors. Those pseudo-priors are deliberately tight approximations to what the parameters look like under their own model, for instance a precision of 256 on $\alpha$ and $\beta$ when model 2 is active and 400 on $\gamma$ and $\delta$ when model 1 is active. They do not enter the marginal posterior for j, but they keep the dormant parameters in a plausible region so that the chain can switch models at a workable rate instead of getting stuck.
The prior over models is deliberately lopsided, p[1] = 0.9995 against p[2] = 0.0005. The data favour the second model strongly enough that a uniform prior would leave model 1 essentially unvisited, so the prior is tilted the other way to keep both states in play; the tilt is then divided back out. The monitored quantity is pM2 = step(j - 1.5), whose posterior mean estimates $P(M_2 \mid \text{data})$, and the Bayes factor is the posterior odds divided by the prior odds:
\[B_{21} = \frac{P(M_2 \mid \text{data})}{1 - P(M_2 \mid \text{data})} \times \frac{p_1}{p_2}\]
With the bundled reference value of pM2 this comes out in the thousands, decisively in favour of the second model. Note that j is a discrete parameter, so this example needs a sampler that can update discrete nodes. This example is part of Volume 3 of the classic BUGS examples; see the MultiBUGS examples page and the OpenBUGS Pines page.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_3.bayes_factors, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_3.bayes_factors
example.model_defquote
for i = 1:N
Ys[i] = (Y[i] - mean(Y[:])) / sd(Y[:])
xs[i] = (x[i] - mean(x[:])) / sd(x[:])
zs[i] = (z[i] - mean(z[:])) / sd(z[:])
end
j ~ dcat(p[:])
p[1] = 0.9995
p[2] = 1 - p[1]
pM2 = _step(j - 1.5)
for i = 1:N
mu[1, i] = alpha + beta * xs[i]
mu[2, i] = gamma + delta * zs[i]
Ys[i] ~ dnorm(mu[j, i], tau[j])
end
alpha ~ dnorm(var"mu.alpha"[j], var"tau.alpha"[j])
beta ~ dnorm(var"mu.beta"[j], var"tau.beta"[j])
tau[1] ~ dgamma(r1[j], l1[j])
var"mu.alpha"[1] = 0
var"tau.alpha"[1] = 1.0e-6
var"mu.beta"[1] = 0
var"tau.beta"[1] = 0.0001
r1[1] = 0.0001
l1[1] = 0.0001
var"mu.alpha"[2] = 0
var"tau.alpha"[2] = 256
var"mu.beta"[2] = 1
var"tau.beta"[2] = 256
r1[2] = 30
l1[2] = 4.5
gamma ~ dnorm(var"mu.gamma"[j], var"tau.gamma"[j])
delta ~ dnorm(var"mu.delta"[j], var"tau.delta"[j])
tau[2] ~ dgamma(r2[j], l2[j])
var"mu.gamma"[1] = 0
var"tau.gamma"[1] = 400
var"mu.delta"[1] = 1
var"tau.delta"[1] = 400
r2[1] = 46
l2[1] = 4.5
var"mu.gamma"[2] = 0
var"tau.gamma"[2] = 1.0e-6
var"mu.delta"[2] = 0
var"tau.delta"[2] = 0.0001
r2[2] = 0.0001
l2[2] = 0.0001
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model{
# standardise data
for(i in 1:N){
Ys[i] <- (Y[i] - mean(Y[])) / sd(Y[])
xs[i] <- (x[i] - mean(x[])) / sd(x[])
zs[i] <- (z[i] - mean(z[])) / sd(z[])
}
# model node
j ~ dcat(p[])
p[1] <- 0.9995 p[2] <- 1 - p[1] # use for joint modelling
# p[1] <- 1 p[2] <- 0 # include for estimating Model 1
# p[1] <- 0 p[2] <-1 # include for estimating Model 2
pM2 <- step(j - 1.5)
# model structure
for(i in 1 : N){
mu[1, i] <- alpha + beta * xs[i]
mu[2, i] <- gamma + delta*zs[i]
Ys[i] ~ dnorm(mu[j, i], tau[j])
}
# Model 1
alpha ~ dnorm(mu.alpha[j], tau.alpha[j])
beta ~ dnorm(mu.beta[j], tau.beta[j])
tau[1] ~ dgamma(r1[j], l1[j])
# estimation priors
mu.alpha[1]<- 0 tau.alpha[1] <- 1.0E-6
mu.beta[1] <- 0 tau.beta[1] <- 1.0E-4
r1[1] <- 0.0001 l1[1] <- 0.0001
# pseudo-priors
mu.alpha[2]<- 0 tau.alpha[2] <- 256
mu.beta[2] <- 1 tau.beta[2] <- 256
r1[2] <- 30 l1[2] <- 4.5
# Model 2
gamma ~ dnorm(mu.gamma[j], tau.gamma[j])
delta ~ dnorm(mu.delta[j], tau.delta[j])
tau[2] ~ dgamma(r2[j], l2[j])
# pseudo-priors
mu.gamma[1] <- 0 tau.gamma[1] <- 400
mu.delta[1] <- 1 tau.delta[1] <- 400
r2[1] <- 46 l2[1] <- 4.5
# estimation priors
mu.gamma[2] <- 0 tau.gamma[2] <- 1.0E-6
mu.delta[2] <- 0 tau.delta[2] <- 1.0E-4
r2[2] <- 0.0001 l2[2] <- 0.0001
}Data
example.data(N = 42, Y = [3040, 2470, 3610, 3480, 3810, 2330, 1800, 3110, 3160, 2310 … 2850, 1590, 3770, 3850, 2480, 3570, 2620, 1890, 3030, 3030], x = [29.2, 24.7, 32.3, 31.3, 31.5, 24.5, 19.9, 27.3, 27.1, 24.0 … 26.7, 22.1, 30.3, 32.0, 23.2, 30.3, 29.9, 20.8, 33.2, 28.2], z = [25.4, 22.2, 32.2, 31.0, 30.9, 23.9, 19.2, 27.2, 26.3, 23.9 … 25.9, 21.4, 29.8, 30.6, 22.6, 30.3, 23.8, 18.4, 29.4, 28.2])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data, example.inits)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 7):
Model parameters:
alpha
beta
gamma
j
tau[1], tau[2]
delta
Variable sizes and types:
alpha: type = Int64
gamma: type = Int64
p: size = (2,), type = Vector{Float64}
j: type = Int64
mu.beta: size = (2,), type = Vector{Int64}
l2: size = (2,), type = Vector{Float64}
delta: type = Int64
tau.alpha: size = (2,), type = Vector{Float64}
mu.gamma: size = (2,), type = Vector{Int64}
tau.delta: size = (2,), type = Vector{Float64}
r2: size = (2,), type = Vector{Float64}
r1: size = (2,), type = Vector{Float64}
tau.gamma: size = (2,), type = Vector{Float64}
l1: size = (2,), type = Vector{Float64}
x: size = (42,), type = Vector{Float64}
N: type = Int64
mu: size = (2, 42), type = Matrix{Float64}
Ys: size = (42,), type = Vector{Float64}
tau.beta: size = (2,), type = Vector{Float64}
Y: size = (42,), type = Vector{Int64}
z: size = (42,), type = Vector{Float64}
pM2: type = Float64
tau: size = (2,), type = Vector{Float64}
mu.delta: size = (2,), type = Vector{Int64}
beta: type = Int64
zs: size = (42,), type = Vector{Float64}
mu.alpha: size = (2,), type = Vector{Int64}
xs: size = (42,), type = Vector{Float64}
The example's own initial values are passed in here. Several of these models fail from random starting values, so example.inits is not optional in practice; a second set is available as example.inits_alternative.
See Getting Started for the recipe that takes a compiled model to posterior samples.
Reference results
The posterior summaries published with the original example. A converged chain should reproduce these up to Monte Carlo error.
example.reference_results(pM2 = (mean = 0.6402, std = 0.48),)