🎲 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
- Conditional probability tables (CPTs) — P(X | parents) specification
- Enumeration inference — exact marginal/posterior computation via summation
- Variable elimination — efficient inference by factor multiplication and marginalization
- D-separation — conditional independence testing in DAGs
- Record types — structured representation of nodes, CPTs, and networks
- Classic networks — alarm, sprinkler, and Asia diagnostic models
Key Functions
| Function | Description |
|---|---|
find_node | Retrieve a node by variable name |
lookup_cpt | Look up P(X=true | parent values) from a CPT |
enumerate_ask | Exact posterior P(X | evidence) by enumeration |
variable_elimination | Factor-based inference with marginalization |
d_separated | Test conditional independence between variables |
children_of | Find 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
| Method | Complexity | Approach |
|---|---|---|
| Enumeration | O(2ⁿ) | Sum over all hidden variable assignments |
| Variable Elimination | O(2ʷ) | Factor operations with optimal ordering (w = treewidth) |