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: .
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 be an undirected graph (the “natural undirected graph” or NUG) encoding potential neighbor relationships. The MDGM places a prior over DAGs that are compatible with : every directed edge in corresponds to an undirected edge in .
Three DAG constructions are supported:
Spanning trees
A spanning tree of 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:
where is the spatial dependence parameter and 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 such that no directed cycle exists. Equivalently, this is defined by a vertex permutation : edge is directed as if . The MCMC proposes new permutations and accepts via a Metropolis-Hastings step based on the exact DAG log-likelihood ratio.
The MRF model
The Markov random field (MRF) defines a Potts/Ising model on the undirected graph . The joint distribution is:
where 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 . 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:
- Propose (normal random walk)
- Sample an auxiliary field from the MRF at via Gibbs sweeps
- Accept with probability:
where
is the sufficient statistic. The n_aux_sweeps parameter
controls the number of Gibbs sweeps used to approximately sample
(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:
where is the number of neighbors of vertex with color . This is a tractable approximation and uses the same MH random walk as the MDGM. It is fast but may underestimate .
Spatial field model
Given a DAG (MDGM) or the undirected graph (MRF), the conditional distribution of the color given its neighbors is:
MDGM (parents in the DAG):
MRF (neighbors in the undirected graph):
where 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 is observed directly. The MCMC updates the dependence parameter and (for MDGM) the DAG structure . This is useful when the data are categorical labels on a spatial domain.
Hierarchical model
In the hierarchical model, is a latent field and observations are generated through an emission distribution:
model <- srf_model(nug, spatial = mdgm(), emission = "bernoulli")
model <- srf_model(nug, spatial = mrf(method = "pseudo_likelihood"),
emission = "gaussian")Currently supported emission families:
- Bernoulli: , with identifiability constraint enforced via truncated Beta posterior sampling.
- Gaussian: , with identifiability constraint . Independent Normal and Inverse-Gamma conjugate updates for and .
- Poisson: , with identifiability constraint . Conjugate truncated Gamma updates.
The MCMC additionally updates (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 has a half-Cauchy prior:
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
has a
prior.
emission_prior_params = c(a, b). -
Gaussian:
and
,
independently.
emission_prior_params = c(mu_0, sigma2_0, alpha_0, beta_0). -
Poisson: Each
has a
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:
- 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.
- Update (hierarchical only) — Gibbs scan over vertices in random order. The full conditional combines the spatial prior with the emission likelihood.
-
Update
— Metropolis-Hastings with normal random walk proposal (default), or
exchange algorithm for MRF with
method = "exchange". - Update (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) |