Pig Weights: Histogram smoothing with structured precision matrix

This example is a histogram-smoothing problem adapted from Congdon (2001), example 5.9. The data are the weight gains of n = 522 pigs, already grouped into s = 21 ordered bins, and all that is retained is the count y in each bin. The counts rise from 1 in the lowest bin to a peak of 72 around the twelfth and fall away again to 1 in the highest, but the tails are ragged: three bins hold a single pig and two hold none at all. The question is what smooth distribution of weight gain lies behind that noisy histogram.

The counts are multinomial over the 21 bins, and the bin probabilities are the softmax of a vector of logits $\gamma$. Smoothing is imposed entirely through the prior on $\gamma$, which is multivariate normal with a constant mean of $-\log s$ in every bin, corresponding to a flat histogram, and a precision matrix built to represent a first-order autoregressive process along the bin index. Neighbouring bins are therefore pulled towards each other, and the strength of that pull is estimated from the data through $\rho$ rather than fixed by the analyst.

\[y \sim \text{Multinomial}(\theta, n), \qquad \theta_i = \frac{e^{\gamma_i}}{\sum_k e^{\gamma_k}}, \qquad \gamma \sim \text{MVNormal}(\mu, T)\]

The point of interest is how $T$ is written. Rather than forming the AR(1) covariance with entries $\rho^{|i-j|}/\tau$ and inverting it, which the model shows as a commented-out alternative, the precision matrix is specified directly in tridiagonal form: $\tau$ at the two corners of the diagonal, $\tau(1 + \rho^2)$ on the interior diagonal, $-\tau\rho$ on the two off-diagonals, and zero everywhere else. That is the exact inverse of the AR(1) covariance and it avoids a matrix inversion at every iteration, so the same model runs considerably faster. The priors are $\rho \sim \text{Uniform}(0, 1)$, which keeps the correlation between adjacent bins positive, and $\tau \sim \text{Uniform}(0.5, 10)$; note that $\tau$ is a precision here, as it is throughout BUGS. The smoothed bin frequencies Sm[i] = $n\,\theta_i$ are the quantities reported. This example is part of Volume 3 of the classic BUGS examples; see the MultiBUGS examples page and the OpenBUGS Pigweights page.

The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_3.pig_weights, which ships with the package.

Model

using JuliaBUGS

example = JuliaBUGS.BUGSExamples.VOLUME_3.pig_weights
example.model_def
quote
    y[1:s] ~ dmulti(th[1:s], n)
    var"sum.g" = sum(g[:])
    for i = 1:s
        Sm[i] = n * th[i]
        g[i] = exp(gam[i])
        th[i] = g[i] / var"sum.g"
    end
    rho ~ dunif(0, 1)
    tau ~ dunif(0.5, 10)
    gam[1:s] ~ dmnorm(mu[:], T[:, :])
    for j = 1:s
        mu[j] = -(log(s))
    end
    for j = 2:s - 1
        T[j, j] = tau * (1 + pow(rho, 2))
    end
    T[1, 1] = tau
    T[s, s] = tau
    for j = 1:s - 1
        T[j, j + 1] = -tau * rho
        T[j + 1, j] = T[j, j + 1]
    end
    for i = 1:s - 1
        for j = 2 + i:s
            T[i, j] = 0
            T[j, i] = 0
        end
    end
end

The program as it appears in the original BUGS distribution:

print(example.original_syntax_program)
model{
      y[1:s] ~ dmulti(th[1 : s] , n)
      sum.g <- sum(g[])
# smoothed frequencies
   for (i in 1 : s) {
         Sm[i] <- n * th[i]
      g[i] <- exp(gam[i])
         th[i] <- g[i] / sum.g
      }
# prior on elements of AR Precision Matrix
   rho ~ dunif(0, 1)
   tau ~ dunif(0.5, 10)
# MVN for logit parameters
   gam[1 : s] ~ dmnorm(mu[], T[ , ])
   for (j in 1:s) {
         mu[j] <- -log(s)
      }
   # Define Precision Matrix
      for (j in 2 : s - 1) {
         T[j, j] <- tau * (1 + pow(rho, 2))
      }
      T[1, 1] <- tau
      T[s, s] <- tau
      for (j in 1 : s -1 ) {
         T[j, j + 1] <- -tau * rho
         T[j + 1, j] <- T[j, j + 1]
      }
      for (i in 1 : s - 1) {
         for (j in 2 + i : s) {
            T[i, j] <- 0; T[j, i] <- 0
         }
      }
# Or Could do in terms of covariance, which is simpler to write but slower
#      for (i in 1 : s) {
#         for (j in 1 : s) {
#            cov[i, j] <- pow(rho, abs(i - j)) / tau
#         }
#      }
#      T[1 : s, 1 : s] <- inverse(cov[ , ])
   }

Data

example.data
(y = [1, 1, 0, 7, 5, 10, 30, 30, 41, 48  …  72, 56, 46, 45, 22, 24, 12, 5, 0, 1], n = 522, s = 21)

Compiling the model

model = JuliaBUGS.compile(example.model_def, example.data, example.inits)
BUGSModel (parameters are in transformed (unconstrained) space, with dimension 23):

  Model parameters:
    rho
    gam[1:21]
    tau

  Variable sizes and types:
    th: size = (21,), type = Vector{Float64}
    n: type = Int64
    rho: type = Float64
    gam: size = (21,), type = Vector{Float64}
    s: type = Int64
    mu: size = (21,), type = Vector{Float64}
    Sm: size = (21,), type = Vector{Float64}
    g: size = (21,), type = Vector{Float64}
    tau: type = Float64
    T: size = (21, 21), type = Matrix{Float64}
    sum.g: type = Float64
    y: size = (21,), type = Vector{Int64}

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"Sm[1]" = (mean = 1.539, std = 0.8977), var"Sm[2]" = (mean = 1.575, std = 0.8046), var"Sm[3]" = (mean = 1.956, std = 0.9074), var"Sm[4]" = (mean = 5.02, std = 1.716), var"Sm[5]" = (mean = 6.098, std = 1.915), var"Sm[6]" = (mean = 10.94, std = 2.756), var"Sm[7]" = (mean = 27.5, std = 4.68), var"Sm[8]" = (mean = 30.42, std = 4.877), var"Sm[9]" = (mean = 40.67, std = 5.781), var"Sm[10]" = (mean = 48.26, std = 6.399), var"Sm[11]" = (mean = 65.42, std = 7.298), var"Sm[12]" = (mean = 71.03, std = 7.519), var"Sm[13]" = (mean = 56.14, std = 6.582), var"Sm[14]" = (mean = 46.26, std = 6.019), var"Sm[15]" = (mean = 43.25, std = 5.965), var"Sm[16]" = (mean = 23.44, std = 4.195), var"Sm[17]" = (mean = 22.19, std = 4.193), var"Sm[18]" = (mean = 11.46, std = 2.839), var"Sm[19]" = (mean = 4.966, std = 1.7), var"Sm[20]" = (mean = 2.056, std = 1.001), var"Sm[21]" = (mean = 1.81, std = 0.9979))