pyAgrum-nightly: Probabilistic Graphical Models

2.3.2.9.dev202604161770834561 · active · verified Thu Apr 16

pyAgrum-nightly is a Python wrapper for the scientific C++ aGrUM library, providing a high-level interface for Bayesian networks, Markov Networks, Influence Diagrams, and other Probabilistic Graphical Models. As a nightly build, it offers the latest features and bug fixes from the development branch. It is actively maintained with frequent updates reflecting ongoing development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple Bayesian network (the 'Water Sprinkler' example), define its topology and conditional probability tables, and perform inference with evidence. It highlights the use of `pyagrum` (lowercase) for imports and dictionary-based CPT assignments.

import pyagrum as gum

# Create a Bayesian Network
bn = gum.BayesNet("WaterSprinkler")

# Add variables
id_c = bn.add(gum.LabelizedVariable("c", "cloudy ?", 2))
id_s = bn.add(gum.LabelizedVariable("s", "sprinkler ?", 2))
id_r = bn.add(gum.LabelizedVariable("r", "rain ?", 2))
id_w = bn.add(gum.LabelizedVariable("w", "wet grass ?", 2))

# Add arcs (dependencies)
bn.addArc(id_c, id_s)
bn.addArc(id_c, id_r)
bn.addArc(id_s, id_w)
bn.addArc(id_r, id_w)

# Define Conditional Probability Tables (CPTs) using dictionaries
bn.cpt("c").fillWith([0.5, 0.5])
bn.cpt("s")[{"c": 0}] = [0.5, 0.5]
bn.cpt("s")[{"c": 1}] = [0.9, 0.1]
bn.cpt("r")[{"c": 0}] = [0.8, 0.2]
bn.cpt("r")[{"c": 1}] = [0.2, 0.8]
bn.cpt("w")[{"s": 0, "r": 0}] = [1, 0]
bn.cpt("w")[{"s": 0, "r": 1}] = [0.1, 0.9]
bn.cpt("w")[{"s": 1, "r": 0}] = [0.1, 0.9]
bn.cpt("w")[{"s": 1, "r": 1}] = [0.01, 0.99]

# Perform inference
ie = gum.LazyPropagation(bn)
ie.setEvidence({"w": 1}) # Wet grass is true
ie.makeInference()

# Get posterior probabilities
posterior_c = ie.posterior("c")
print(f"P(c|w=1): {posterior_c}")

view raw JSON →