🎲 Bayesian Network Inference

Probabilistic graphical models, exact inference, and d-separation

Overview

A complete Bayesian network inference engine supporting directed acyclic graphs of random variables with conditional probability tables. Includes exact inference via enumeration and variable elimination, d-separation testing, and three classic example networks.

Concepts Demonstrated

Key Functions

FunctionDescription
find_nodeRetrieve a node by variable name
lookup_cptLook up P(X=true | parent values) from a CPT
enumerate_askExact posterior P(X | evidence) by enumeration
variable_eliminationFactor-based inference with marginalization
d_separatedTest conditional independence between variables
children_ofFind child nodes in the DAG

Network Structure

type node = {
  name: variable;
  parents: variable list;
  cpt: cpt_entry list;  (* P(true | each parent config) *)
}

(* Classic sprinkler network:
   Cloudy → Sprinkler → WetGrass
   Cloudy → Rain → WetGrass *)

(* Query: P(Rain | WetGrass=true) *)
let p = enumerate_ask sprinkler_net "Rain"
  [("WetGrass", true)]

Inference Methods

MethodComplexityApproach
EnumerationO(2ⁿ)Sum over all hidden variable assignments
Variable EliminationO(2ʷ)Factor operations with optimal ordering (w = treewidth)