Skip to contents

Background

Classical spatial models for discrete data typically use a Markov random field (MRF), which specifies a joint distribution over an undirected graph. However, the MRF likelihood involves an intractable normalizing constant (the partition function), making exact Bayesian inference computationally burdensome.

The Mixture of Directed Graphical Models (MDGM) is an alternative that takes the same undirected graph as input but defines a mixture over compatible directed acyclic graphs (DAGs). Each DAG admits a tractable factorization, so the MDGM avoids the partition function entirely. The joint distribution marginalizes over the DAG space: p(𝐳𝛏)=Dp(D)p(𝐳D,𝛏)p(\mathbf{z} \mid \boldsymbol{\xi}) = \sum_D p(D)\, p(\mathbf{z} \mid D, \boldsymbol{\xi}).

The mdgm package supports both spatial model types through a unified interface: use srf_model() with either an mdgm() or mrf() configuration.

For full details, see Carter and Calder (2024).

Creating models

All models are created via srf_model() with a spatial configuration:

# MDGM with spanning tree
model <- srf_model(nug, spatial = mdgm(dag_type = "spanning_tree"))

# MRF with exchange algorithm
model <- srf_model(nug, spatial = mrf(method = "exchange"))

# Add an emission layer for hierarchical models
model <- srf_model(nug, spatial = mdgm(), emission = "bernoulli")
model <- srf_model(nug, spatial = mrf(method = "pseudo_likelihood"),
                   emission = "gaussian", n_colors = 3L)

The MDGM prior

Let G=(V,E)G = (V, E) be an undirected graph (the “natural undirected graph” or NUG) encoding potential neighbor relationships. The MDGM places a prior over DAGs DD that are compatible with GG: every directed edge (u,v)(u, v) in DD corresponds to an undirected edge {u,v}\{u, v\} in GG.

Three DAG constructions are supported:

Spanning trees

A spanning tree TT of GG is a connected, acyclic subgraph containing all vertices. Edges are directed from child to parent. The posterior sampling uses Wilson’s algorithm with data-dependent edge weights:

w(u,v)=exp(ψ𝟏[zu=zv])w(u, v) = \exp\bigl(\psi \cdot \mathbf{1}[z_u = z_v]\bigr)

where ψ>0\psi > 0 is the spatial dependence parameter and zz is the color assignment. This provides a direct (non-MH) posterior sample of the spanning tree.

Acyclic orientations

An acyclic orientation assigns a direction to every edge in GG such that no directed cycle exists. Equivalently, this is defined by a vertex permutation σ\sigma: edge {u,v}\{u, v\} is directed as (u,v)(u, v) if σ(u)>σ(v)\sigma(u) > \sigma(v). The MCMC proposes new permutations and accepts via a Metropolis-Hastings step based on the exact DAG log-likelihood ratio.

Rooted DAGs

A rooted DAG is constructed from GG by choosing a root vertex and orienting edges via a breadth-first or depth-first traversal. The MCMC updates the root via random walk proposals on GG.

The MRF model

The Markov random field (MRF) defines a Potts/Ising model on the undirected graph GG. The joint distribution is:

p(𝐳ψ)=1C(ψ)exp(ψ{u,v}E𝟏[zu=zv])p(\mathbf{z} \mid \psi) = \frac{1}{C(\psi)} \exp\Bigl(\psi \sum_{\{u,v\} \in E} \mathbf{1}[z_u = z_v]\Bigr)

where C(ψ)C(\psi) is the intractable normalizing constant (partition function). Unlike the MDGM, the graph structure is fixed — no DAG sampling is performed.

Inference methods for psi

The partition function makes standard Metropolis-Hastings infeasible for ψ\psi. Two alternatives are provided:

Exchange algorithm (method = "exchange")

The exchange algorithm (Murray, Ghahramani, and MacKay, 2006) cancels the partition function by introducing an auxiliary variable. At each MCMC iteration:

  1. Propose ψ*q(ψ)\psi^* \sim q(\cdot \mid \psi) (normal random walk)
  2. Sample an auxiliary field 𝐳*\mathbf{z}^* from the MRF at ψ*\psi^* via Gibbs sweeps
  3. Accept with probability: min(1,exp[S(𝐳)(ψ*ψ)+S(𝐳*)(ψψ*)]p(ψ*)p(ψ))\min\Bigl(1,\; \exp\bigl[S(\mathbf{z})(\psi^* - \psi) + S(\mathbf{z}^*)(\psi - \psi^*)\bigr] \cdot \frac{p(\psi^*)}{p(\psi)}\Bigr)

where S(𝐳)={u,v}E𝟏[zu=zv]S(\mathbf{z}) = \sum_{\{u,v\} \in E} \mathbf{1}[z_u = z_v] is the sufficient statistic. The n_aux_sweeps parameter controls the number of Gibbs sweeps used to approximately sample 𝐳*\mathbf{z}^* (default: 200).

This method is exact (up to the quality of the auxiliary sample) but more expensive per iteration.

Pseudo-likelihood (method = "pseudo_likelihood")

The pseudo-likelihood replaces the joint likelihood with the product of full conditionals:

PL(ψ)=iVp(zizi,ψ)=iVexp(ψni,zi)kexp(ψnik)\text{PL}(\psi) = \prod_{i \in V} p(z_i \mid z_{-i}, \psi) = \prod_{i \in V} \frac{\exp(\psi \cdot n_{i,z_i})}{\sum_k \exp(\psi \cdot n_{ik})}

where nikn_{ik} is the number of neighbors of vertex ii with color kk. This is a tractable approximation and uses the same MH random walk as the MDGM. It is fast but may underestimate ψ\psi.

Spatial field model

Given a DAG DD (MDGM) or the undirected graph GG (MRF), the conditional distribution of the color ziz_i given its neighbors is:

MDGM (parents in the DAG): p(zi=kzpa(i),ψ)exp(ψjpa(i)𝟏[zj=k]+αk)p(z_i = k \mid z_{\text{pa}(i)}, \psi) \propto \exp\Bigl(\psi \sum_{j \in \text{pa}(i)} \mathbf{1}[z_j = k] + \alpha_k\Bigr)

MRF (neighbors in the undirected graph): p(zi=kzi,ψ)exp(ψjnbr(i)𝟏[zj=k])p(z_i = k \mid z_{-i}, \psi) \propto \exp\Bigl(\psi \sum_{j \in \text{nbr}(i)} \mathbf{1}[z_j = k]\Bigr)

where αk\alpha_k are marginal log-probabilities (currently fixed at 0 for a uniform marginal in MDGM).

Standalone vs. hierarchical models

Standalone model

In the standalone model, the spatial field zz is observed directly. The MCMC updates the dependence parameter ψ\psi and (for MDGM) the DAG structure DD. This is useful when the data are categorical labels on a spatial domain.

model <- srf_model(nug, spatial = mdgm(dag_type = "spanning_tree"))
model <- srf_model(nug, spatial = mrf(method = "exchange"))

Hierarchical model

In the hierarchical model, zz is a latent field and observations yiy_i are generated through an emission distribution:

yijzi,θf(yijθzi)y_{ij} \mid z_i, \theta \sim f(y_{ij} \mid \theta_{z_i})

model <- srf_model(nug, spatial = mdgm(), emission = "bernoulli")
model <- srf_model(nug, spatial = mrf(method = "pseudo_likelihood"),
                   emission = "gaussian")

Currently supported emission families:

  • Bernoulli: yijzi=kBernoulli(pk)y_{ij} \mid z_i = k \sim \text{Bernoulli}(p_k), with identifiability constraint p0<p1<p_0 < p_1 < \cdots enforced via truncated Beta posterior sampling.
  • Gaussian: yijzi=k𝒩(μk,σk2)y_{ij} \mid z_i = k \sim \mathcal{N}(\mu_k, \sigma_k^2), with identifiability constraint μ0<μ1<\mu_0 < \mu_1 < \cdots. Independent Normal and Inverse-Gamma conjugate updates for μk\mu_k and σk2\sigma_k^2.
  • Poisson: yijzi=kPoisson(λk)y_{ij} \mid z_i = k \sim \text{Poisson}(\lambda_k), with identifiability constraint λ0<λ1<\lambda_0 < \lambda_1 < \cdots. Conjugate truncated Gamma updates.

The MCMC additionally updates zz (Gibbs scan over vertices) and the emission parameters (conjugate updates). See the Emission Models vignette for worked examples.

Prior specification

Dependence parameter

The spatial dependence parameter ψ>0\psi > 0 has a half-Cauchy prior:

p(ψ)=2π(1+ψ2),ψ>0p(\psi) = \frac{2}{\pi(1 + \psi^2)}, \quad \psi > 0

Updates use a Metropolis-Hastings random walk with a normal proposal (MDGM and MRF pseudo-likelihood) or the exchange algorithm (MRF exchange).

Emission parameters

  • Bernoulli: Each pkp_k has a Beta(a,b)\text{Beta}(a, b) prior. emission_prior_params = c(a, b).
  • Gaussian: μk𝒩(μ0,σ02)\mu_k \sim \mathcal{N}(\mu_0, \sigma^2_0) and σk2InverseGamma(α0,β0)\sigma_k^2 \sim \text{InverseGamma}(\alpha_0, \beta_0), independently. emission_prior_params = c(mu_0, sigma2_0, alpha_0, beta_0).
  • Poisson: Each λk\lambda_k has a Gamma(α0,β0)\text{Gamma}(\alpha_0, \beta_0) prior (rate parameterization). emission_prior_params = c(alpha_0, beta_0).

All conjugate posteriors use truncated sampling to enforce parameter ordering for identifiability.

MCMC algorithm

Each iteration of the MCMC sampler performs:

  1. Update graph (MDGM only) — Sample a new spanning tree (direct posterior sample via Wilson’s) or propose a new acyclic orientation/root (MH step). For MRF, this step is a no-op.
  2. Update zz (hierarchical only) — Gibbs scan over vertices in random order. The full conditional combines the spatial prior with the emission likelihood.
  3. Update ψ\psi — Metropolis-Hastings with normal random walk proposal (default), or exchange algorithm for MRF with method = "exchange".
  4. Update θ\theta (hierarchical only) — Conjugate posterior sampling with identifiability constraints.

Choosing between MDGM and MRF

Criterion MDGM MRF
Partition function Avoided (tractable DAG likelihood) Requires exchange algorithm or pseudo-likelihood
Graph structure Sampled (posterior over DAGs) Fixed
Edge inclusion probabilities Yes Not applicable
Speed (per iteration) Fast Fast (pseudo-likelihood) or slow (exchange)
Accuracy of psi Exact Exact (exchange) or approximate (pseudo-likelihood)