Introduction to Bayesian Graphical Models

Published: December 2024 · 10 min read

Bayesian graphical models represent a powerful framework for reasoning under uncertainty. They combine the mathematical rigor of probability theory with the intuitive representation of dependencies through directed graphs. In this article, we will explore the core concepts, learn how to visualize them, and examine real-world examples.

What are Graphical Models?

A graphical model is a probabilistic model where a graph expresses the conditional dependence structure between random variables. The graph consists of nodes representing variables and edges representing probabilistic dependencies between them.

"The great power of graphical models lies in their ability to represent complex joint probability distributions through simple local relationships."

There are two main types of graphical models: directed graphical models (also known as Bayesian networks) and undirected graphical models (also known as Markov random fields). We focus here on directed acyclic graphs (DAGs).

Pumps: conjugate gamma-Poisson hierarchical model

To understand the utility of these models, let's look at the Pumps example (George et al., 1993). This model analyzes the failure rates of 10 pumps in a nuclear power plant.

This is a conjugate gamma-Poisson hierarchical model. The number of failures ($x_i$) for each pump is modeled using a Poisson distribution: $x_i \sim Poisson(\theta_i t_i)$. Crucially, the failure rates ($\theta_i$) are drawn from a common Gamma prior, $\theta_i \sim Gamma(\alpha, \beta)$, with hyperparameters $\alpha$ and $\beta$ given Exponential and Gamma priors respectively. This structure leads to a non-standard posterior for $\alpha$, necessitating the use of Gibbs sampling.

Rats: a normal hierarchical model

Now let's look at a temporal model from Gelfand et al. (1990). The Rats example tracks the weight of 30 rats measured weekly over five weeks.

This is a random effects linear growth curve model. The weights $Y_{ij}$ are normally distributed with a mean that is a linear function of time: $\alpha_i + \beta_i(x_j - \bar{x})$. Note that the time points $x_j$ are standardized around their mean ($\bar{x} = 22$) to reduce dependence between the intercept $\alpha_i$ and slope $\beta_i$. We estimate these random effects to capture both individual trajectories and population-level growth.

Why Use Graphical Models?

As seen in the examples above, graphical models offer distinct advantages:

Interpretability: The visual structure makes it easy to understand the relationships between variables. In the Rats model, seeing the connection between the population mean and individual rats helps clarify the hierarchical assumption.

Modularity: Complex models can be built from simpler components. We can easily add a new layer to our hierarchy without rewriting the entire mathematical derivation.

Seeds: Random effect logistic regression

Finally, let's look at a random effects logistic regression from Crowder (1978). The Seeds example investigates germination rates across 21 plates arranged in a 2x2 factorial design by seed type and root extract.

The model accounts for overdispersion—variation greater than expected in a standard Binomial distribution—by introducing a random effect term $b_i$ for each plate. The probability of germination is modeled as $logit(p_i) = \alpha_0 + \alpha_1 x_{1i} + \alpha_2 x_{2i} + \alpha_{12} x_{1i}x_{2i} + b_i$, where $b_i \sim Normal(0, \tau)$, effectively capturing the unobserved heterogeneity.

Under the Hood: The BUGS Language

BUGS (Bayesian inference Using Gibbs Sampling) is a probabilistic programming language for specifying statistical models. The visual representations in the widgets above are automatically converted to valid BUGS code that can be executed in various inference engines.

For example, a simple hierarchical model might look like this:

model {
  for (i in 1:N) {
    y[i] ~ dnorm(mu, tau)
  }
  mu ~ dnorm(0, 0.001)
  tau ~ dgamma(0.001, 0.001)
}

Getting Started with JuliaBUGS

JuliaBUGS is a Julia implementation of the BUGS language that provides efficient inference for Bayesian graphical models. Combined with the DoodleBUGS visual editor, it offers a complete workflow from model design to inference.

The widgets you see above are designed to integrate seamlessly with JuliaBUGS, allowing you to export your visual models as executable Julia code. This bridges the gap between intuitive model building and production-ready code.

Conclusion

Bayesian graphical models provide a principled way to reason about uncertainty. With tools like DoodleBUGS and JuliaBUGS, building and running these models has never been more accessible. We encourage you to explore the interactive widgets above to deepen your understanding of probabilistic modeling.