Skip to content

Hierarchical Programs

A hierarchical model gives related groups their own parameters and ties those parameters together through shared higher-level ones, so that groups inform one another (partial pooling) (Gelman et al., 2013). QVR expresses such models with parametric programs, which reuse one template for several grouping factors, and with grouped marginalization, which sums a discrete class out once per group. Probabilistic Programs describes the runtime of a compiled program.

Hierarchical models with parametric templates

In the model below every response belongs to both a subject and a verb (crossed effects). Each subject has an offset, and the shared scale sigma controls how much subjects tend to differ, so data for one subject inform that scale and through it the estimates for the others. The verbs work the same way. The index arrays subject_idx and verb_idx pick out each response's offsets, and adding them to an intercept gives the response's log-odds.

object Subject : FinSet 200
object Verb : FinSet 100
object Resp : FinSet 5000

program random_intercepts[G : FinSet](prior_scale : Real) : G -> Real 1
    let sigma <- sample HalfNormal(scale=prior_scale)
    let v : G <- sample Normal(loc=0.0, scale=sigma)
    return v

program crossed : Resp -> Resp
    let intercept <- sample Normal(loc=0.0, scale=1.0)

    let by_subject <- program random_intercepts[Subject](1.0)
    let by_verb    <- program random_intercepts[Verb](1.0)

    let eta = intercept + by_subject[subject_idx] + by_verb[verb_idx]
    observe response : Resp <- Bernoulli(logits=eta)
    return intercept

export crossed

The template random_intercepts takes a static grouping object G in brackets and a runtime prior scale in parentheses. This separates the object that fixes the latent plate's type from the value used in its prior. Each call inlines a copy of its body with its names prefixed by the bound name (by_subject$sigma, by_subject$v, and so on), so the two grouping factors share the template's structure but not its latent variables. The responses and the index arrays are given at run time in the observations dict, the index arrays as data inputs. A monotone ordinal effect is the cumsum of HalfNormal increments, whose partial sums increase because each increment is positive, and a discrete latent class is summed out with a marginalize block.

Grouped marginalization

Suppose two measurements belong to one item whose class is unknown. Given class zero their likelihoods are 0.8 and 0.7, and given class one they are 0.2 and 0.3. With equal class probabilities the joint likelihood is 0.5 * (0.8 * 0.7) + 0.5 * (0.2 * 0.3) = 0.31. Multiplying two separately mixed likelihoods would give 0.25, which is the different model with a separate class choice for each measurement (Stan Development Team, n.d.-b).

An index map is a function \(\iota : N \to G\) from observation rows to groups, given as an integer array. Its fiber over a group \(g\) is the set \(\iota^{-1}(g) = \{n : \iota(n) = g\}\) of rows assigned to \(g\) (fiber). A marginalize block with [over=G] draws one class per element of G, and every observe in its body names with [via=idx] the index map from its rows to G. The block multiplies the likelihoods of the rows in each fiber under each class and then sums over the class:

object Item : FinSet 3
object RespA : FinSet 6
object RespB : FinSet 4
object K : FinSet 2
object Out : Real 1

program grouped : Out -> Out
    let probs <- sample Dirichlet(concentration=1.0) [over=K]
    let mu_a : K <- sample Normal(loc=0.0, scale=1.0)
    let theta_b : K <- sample Beta(concentration1=1.0, concentration0=1.0)
    marginalize c : K <- Categorical(probs=probs) [over=Item]
        observe r_a : RespA <- Normal(loc=mu_a[c], scale=1.0) [via=idx_a]
        observe r_b : RespB <- Bernoulli(probs=theta_b[c]) [via=idx_b]
    return probs

With \(\pi\) the class probabilities, \(\iota_m\) the index map of the \(m\)-th observe, and \(\ell_m(n, k)\) the log likelihood of its row \(n\) under class \(k\), the block adds

\[ \sum_{g \in G} \log \sum_{k=1}^{K} \exp\Bigl[\log \pi(g, k) + \sum_{m} \sum_{n \in \iota_m^{-1}(g)} \ell_m(n, k)\Bigr] \]

to the log joint density, computed with log-sum-exp for numerical stability. This is Stan's target += log_mix(probs, ...) accumulation, once per item (Stan Development Team, n.d.-b).

Special cases

  • Without [over=G], the block has one class for all rows, a single mixture over the whole data set (scoped marginalize).
  • One observe whose index map sends each row to its own group gives a separate mixture per row.
  • One observe whose index map groups rows gives one mixture per group.
  • Several observes that share the index set give a joint mixture across different kinds of response.

Row scores and prediction

The index maps decide what a pointwise score is (units and row scores). With one row per group, ModelFit.log_likelihood returns one score per row, for any index map that sends no two rows to one group. With several rows per group, the rows share a class and only the group has a score, which ModelFit.unit_log_likelihood returns. fit.analysis(units="factors") carries those scores to loo and waic, which then leave out one group at a time.

Prediction draws a class for each group and then the rows of the group at that class. An expectation is the mean of each row weighted by its group's class probabilities.

An intervention on a response of the block (do) removes the response's likelihood and keeps the class prior and the other responses (interventions).

Constraints and runtime

The over object must be declared, and every observe in the body needs a via. The class probabilities have shape (K,), shared by all groups, or (|G|, K), one row per group. The function quivers.continuous.plate.marginalize_grouped(ll, idx, log_prior, num_groups) computes the block. It takes either one (N, K) tensor of log likelihoods with its index array, or a list of (N_m, K) tensors with one index array each.

Product groupings

A group can be identified by a pair, such as a school and a subject. The header then names the product, [over=[School, Subject]], and each observe gives one index array per factor, [via=[school_idx, subject_idx]]. The compiler turns each pair into one group index in row-major order, the last factor varying fastest, and the block collects the rows that share a school and a subject before summing over the class:

object School : FinSet 2
object Subject : FinSet 3
object Row : FinSet 8

program by_pair : Out -> Out
    let probs2 <- sample Dirichlet(concentration=1.0) [over=K]
    let mu : K <- sample Normal(loc=0.0, scale=1.0)
    marginalize c : K <- Categorical(probs=probs2) [over=[School, Subject]]
        observe y : Row <- Normal(loc=mu[c], scale=1.0) [via=[school_idx, subject_idx]]
    return probs2

The number of index arrays must equal the number of factors. Composition rules gives the denotation.

End-to-end fit

The program's input is one row, since the responses and their groups come from the observations (Input rows are independent runs):

import torch
from quivers.dsl import load
from quivers.inference import ELBO, AutoNormalGuide, SVI

model = load("crossed.qvr").morphism  # the crossed program above
observations = {
    "response": response,        # (5000,) zeros and ones
    "subject_idx": subject_idx,  # (5000,) integers in 0..199
    "verb_idx": verb_idx,        # (5000,) integers in 0..99
}

guide = AutoNormalGuide(model, observed_names={"response"})
elbo = ELBO(num_particles=1)
optimizer = torch.optim.Adam(
    list(model.parameters()) + list(guide.parameters()), lr=1e-2,
)
svi = SVI(model, guide, optimizer, elbo)

for _ in range(2000):
    svi.step(torch.zeros(1, 1), observations)

See also

Cited sources

Andrew Gelman, John B. Carlin, Hal S. Stern, David B. Dunson, Aki Vehtari, and Donald B. Rubin. Bayesian Data Analysis. Chapman and Hall/CRC, 3 edition, 2013. ISBN 9781439840955. URL: https://www.routledge.com/Bayesian-Data-Analysis/Gelman-Carlin-Stern-Dunson-Vehtari-Rubin/p/book/9781439840955.

Stan Development Team. Finite mixtures. Stan User's Guide, version 2.40, n.d.-b. URL: https://mc-stan.org/docs/stan-users-guide/finite-mixtures.html (visited on 2026-11-06).

Paul-Christian Bürkner. brms: An R Package for Bayesian Multilevel Models Using Stan. Journal of Statistical Software, 2017. doi:10.18637/jss.v080.i01.