Eyes: Normal Mixture Model
This example uses peak sensitivity wavelengths from microspectrophotometric records of a single monkey's eyes, from the study of Bowmaker et al. (1985). There are $N = 48$ measurements, running from 529.0 to 553.2 nanometres. (The original write-up subtracts 500 from each value; the data bundled here are the untransformed wavelengths.) Plotted as a histogram the measurements are bimodal, which is what one would expect if the records came from two distinct types of photoreceptor rather than one. The question is whether the data really support two groups, and if so where the two peaks sit and in what proportion the records are split between them.
The model is a two-component normal mixture with a common precision. Each observation belongs to an unobserved group $T_i$, and given that group it is normal about the corresponding component mean:
\[\begin{aligned} y_i &\sim \text{Normal}(\lambda_{T_i}, \tau) \\ T_i &\sim \text{Categorical}(P), \qquad P \sim \text{Dirichlet}(1, 1) \end{aligned}\]
As always in BUGS, $\tau$ is a precision rather than a variance, and sigma is the derived standard deviation $1/\sqrt{\tau}$. Mixture models of this kind are only identified up to a relabelling of the components, and there is a further danger that the sampler collapses to a state where every observation is assigned to one component. Two devices in the model guard against this. The component means are ordered by construction, $\lambda_1$ getting a vague normal prior and $\lambda_2 = \lambda_1 + \theta$ with $\theta \sim \text{Uniform}(0, 1000)$, so the second component is always the one with the larger mean. And the group labels of the smallest and largest observations are fixed in the data, T[1] = 1 and T[48] = 2, which keeps both components occupied.
The example appears in Volume 2 of the classic BUGS examples; see also the OpenBUGS version of this example.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_2.eyes, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_2.eyes
example.model_defquote
for i = 1:N
y[i] ~ dnorm(mu[i], tau)
mu[i] = lambda[T[i]]
T[i] ~ dcat(P[:])
end
P[1:2] ~ ddirich(alpha[:])
theta ~ dunif(0.0, 1000)
lambda[2] = lambda[1] + theta
lambda[1] ~ dnorm(0.0, 1.0e-6)
tau ~ dgamma(0.001, 0.001)
sigma = 1 / sqrt(tau)
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model {
for( i in 1 : N ) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- lambda[T[i]]
T[i] ~ dcat(P[])
}
P[1:2] ~ ddirich(alpha[])
theta ~ dunif(0.0, 1000)
lambda[2] <- lambda[1] + theta
lambda[1] ~ dnorm(0.0, 1.0E-6)
tau ~ dgamma(0.001, 0.001)
sigma <- 1 / sqrt(tau)
}Data
example.data(y = [529.0, 530.0, 532.0, 533.1, 533.4, 533.6, 533.7, 534.1, 534.8, 535.3 … 549.4, 549.9, 550.6, 551.2, 551.4, 551.5, 551.6, 552.8, 552.9, 553.2], N = 48, alpha = [1, 1], T = Union{Missing, Int64}[1, missing, missing, missing, missing, missing, missing, missing, missing, missing … missing, missing, missing, missing, missing, missing, missing, missing, missing, 2])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data, example.inits)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 50):
Model parameters:
lambda[1]
P[1:2]
T[47], T[46], T[45], T[44], T[43], T[42], T[41], T[40], T[39], T[38], T[37], T[36], T[35], T[34], T[33], T[32], T[31], T[30], T[29], T[28], T[27], T[26], T[25], T[24], T[23], T[22], T[21], T[20], T[19], T[18], T[17], T[16], T[15], T[14], T[13], T[12], T[11], T[10], T[9], T[8], T[7], T[6], T[5], T[4], T[3], T[2]
theta
tau
Variable sizes and types:
T: size = (48,), type = Vector{Int64}
alpha: size = (2,), type = Vector{Int64}
N: type = Int64
lambda: size = (2,), type = Vector{Float64}
P: size = (2,), type = Vector{Float64}
y: size = (48,), type = Vector{Float64}
mu: size = (48,), type = Vector{Float64}
sigma: type = Float64
theta: type = Int64
tau: type = 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(var"P[1]" = (mean = 0.5982, std = 0.09059), var"P[2]" = (mean = 0.4018, std = 0.09059), var"lambda[1]" = (mean = 536.8, std = 0.9863), var"lambda[2]" = (mean = 548.8, std = 1.403), sigma = (mean = 3.829, std = 0.7176))