Eye Tracking: dirichlet process prior
This example is adapted from Congdon (2001), example 6.27. The data are 101 counts from an eye-tracking study, one per subject. The counts are very unevenly spread: 46 of the 101 subjects contribute a zero, most of the rest are small, and a thin right tail runs out to 34. A single Poisson rate cannot describe that shape, and there is no covariate to explain it, so the question is how many distinct rate levels the subjects fall into and what those levels are.
The model answers this with a Poisson mixture whose mixing distribution is given a Dirichlet process prior, truncated at C = 10 components and written in its constructive stick-breaking form. Each subject carries a latent label $S_i$ drawn from the mixture weights, and the count is Poisson with the rate belonging to that label's component. The weights are built from independent $r_j \sim \text{Beta}(1, \alpha)$ variables, where $\alpha$ is the precision parameter of the Dirichlet process, fixed at 1 here (a dgamma(0.1, 0.1) prior on it is included in the program as a commented-out alternative). Because the stick is cut off after C pieces the raw weights p do not quite sum to one, so they are rescaled by their own sum. The component rates come from a common $\text{Gamma}(A, B)$ prior with $A$ and $B$ themselves estimated, which lets the data decide how spread out the rate levels are.
\[\begin{aligned} x_i &\sim \text{Poisson}(\theta_{S_i}), \qquad S_i \sim \text{Categorical}(\pi) \\ \theta_j &\sim \text{Gamma}(A, B), \qquad r_j \sim \text{Beta}(1, \alpha) \\ \pi_j &= p_j \Big/ \textstyle\sum_k p_k, \qquad p_j = r_j \prod_{l < j} (1 - r_l) \end{aligned}\]
The number of components actually used is recovered as a derived quantity rather than being fixed in advance: SC[i, j] indicates that subject i was assigned to component j, sumSC[j] is the resulting size of component j, cl[j] is 1 when that component is occupied, and K is their sum. One caveat about the reference summaries shown further down: the source ships two published sets, and example.reference_results, the set rendered below, is the run in which $A$ and $B$ were held fixed. The model as coded samples them, and the matching figures for that version are recorded as reference_results_variable in the example's source file, src/BUGSExamples/Volume_3/02_Eye_Tracking.jl, where $K$ averages 7.35 rather than 6.92. This example is part of Volume 3 of the classic BUGS examples; see the MultiBUGS examples page and the OpenBUGS Eyetracking page.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_3.eye_tracking, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_3.eye_tracking
example.model_defquote
for i = 1:N
S[i] ~ dcat(pi[:])
mu[i] = theta[S[i]]
x[i] ~ dpois(mu[i])
for j = 1:C
SC[i, j] = equals(j, S[i])
end
end
alpha = 1
p[1] = r[1]
for j = 2:C
p[j] = (r[j] * (1 - r[j - 1]) * p[j - 1]) / r[j - 1]
end
p_sum = sum(p[:])
for j = 1:C
theta[j] ~ dgamma(A, B)
r[j] ~ dbeta(1, alpha)
pi[j] = p[j] / p_sum
end
A ~ dexp(0.1)
B ~ dgamma(0.1, 0.1)
K = sum(cl[:])
for j = 1:C
sumSC[j] = sum(SC[:, j])
cl[j] = _step(sumSC[j] - 1)
end
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model{
for( i in 1 : N ) {
S[i] ~ dcat(pi[])
mu[i] <- theta[S[i]]
x[i] ~ dpois(mu[i])
for (j in 1 : C) {
SC[i, j] <- equals(j, S[i])
}
}
# Precision Parameter
alpha <- 1
# alpha~ dgamma(0.1,0.1)
# Constructive DPP
p[1] <- r[1]
for (j in 2 : C) {
p[j] <- r[j] * (1 - r[j - 1]) * p[j -1 ] / r[j - 1]
}
p.sum <- sum(p[])
for (j in 1:C){
theta[j] ~ dgamma(A, B)
r[j] ~ dbeta(1, alpha)
# scaling to ensure sum to 1
pi[j] <- p[j] / p.sum
}
# hierarchical prior on theta[i] or preset parameters
A ~ dexp(0.1)
B ~ dgamma(0.1, 0.1)
# A <- 1
# B <- 1
# total clusters
K <- sum(cl[])
for (j in 1 : C) {
sumSC[j] <- sum(SC[ , j])
cl[j] <- step(sumSC[j] -1)
}
}Data
example.data(x = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 12, 12, 14, 15, 15, 17, 17, 22, 24, 34], N = 101, C = 10)Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 123):
Model parameters:
A
B
S[101], S[100], S[99], S[98], S[97], S[96], S[95], S[94], S[93], S[92], S[91], S[90], S[89], S[88], S[87], S[86], S[85], S[84], S[83], S[82], S[81], S[80], S[79], S[78], S[77], S[76], S[75], S[74], S[73], S[72], S[71], S[70], S[69], S[68], S[67], S[66], S[65], S[64], S[63], S[62], S[61], S[60], S[59], S[58], S[57], S[56], S[55], S[54], S[53], S[52], S[51], S[50], S[49], S[48], S[47], S[46], S[45], S[44], S[43], S[42], S[41], S[40], S[39], S[38], S[37], S[36], S[35], S[34], S[33], S[32], S[31], S[30], S[29], S[28], S[27], S[26], S[25], S[24], S[23], S[22], S[21], S[20], S[19], S[18], S[17], S[16], S[15], S[14], S[13], S[12], S[11], S[10], S[9], S[8], S[7], S[6], S[5], S[4], S[3], S[2], S[1]
theta[10], theta[9], theta[8], theta[7], theta[6], theta[5], theta[4], theta[3], theta[2], theta[1]
r[10], r[9], r[8], r[7], r[6], r[5], r[4], r[3], r[2], r[1]
Variable sizes and types:
alpha: type = Int64
p: size = (10,), type = Vector{Float64}
B: type = Float64
pi: size = (10,), type = Vector{Float64}
K: type = Float64
S: size = (101,), type = Vector{Float64}
x: size = (101,), type = Vector{Int64}
sumSC: size = (10,), type = Vector{Float64}
N: type = Int64
cl: size = (10,), type = Vector{Float64}
A: type = Float64
mu: size = (101,), type = Vector{Float64}
p_sum: type = Float64
r: size = (10,), type = Vector{Float64}
SC: size = (101, 10), type = Matrix{Float64}
theta: size = (10,), type = Vector{Float64}
C: type = Int64
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(K = (mean = 6.918, std = 1.491), var"mu[92]" = (mean = 13.37, std = 2.949))