pgmpy: Probabilistic Graphical Models

1.1.0 · active · verified Thu Apr 16

pgmpy is a Python library for working with Probabilistic Graphical Models (PGMs). It provides implementations of various models, inference algorithms, and learning algorithms for Bayesian Networks, Markov Networks, and other causal and probabilistic reasoning tasks. The current version is 1.1.0, with minor releases occurring periodically and major versions like 1.0.0 introducing significant breaking changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Bayesian Network, add Conditional Probability Distributions (CPDs) to it, check its validity, and perform exact inference using the Variable Elimination algorithm. The example builds a small network for a student's performance.

from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination

# 1. Define the network structure
# D: Difficulty (Easy, Hard), I: Intelligence (Low, High)
# G: Grade (A, B, C), L: Letter (Good, Bad), S: SAT (Low, High)
model = BayesianNetwork([('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')])

# 2. Define Conditional Probability Distributions (CPDs)
cpd_d = TabularCPD(variable='D', variable_card=2, values=[[0.6], [0.4]])
cpd_i = TabularCPD(variable='I', variable_card=2, values=[[0.7], [0.3]])

# G depends on I and D. Order of evidence: I, D
cpd_g = TabularCPD(variable='G', variable_card=3,
                   values=[[0.3, 0.05, 0.9, 0.5],
                           [0.4, 0.25, 0.08, 0.3],
                           [0.3, 0.7, 0.02, 0.2]],
                   evidence=['I', 'D'], evidence_card=[2, 2])

cpd_l = TabularCPD(variable='L', variable_card=2,
                   values=[[0.1, 0.4, 0.99],
                           [0.9, 0.6, 0.01]],
                   evidence=['G'], evidence_card=[3])

cpd_s = TabularCPD(variable='S', variable_card=2,
                   values=[[0.95, 0.2],
                           [0.05, 0.8]],
                   evidence=['I'], evidence_card=[2])

# 3. Add CPDs to the model
model.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)

# 4. Check if the model is valid (optional but good practice)
assert model.check_model(), "Model is not valid!"

# 5. Perform inference
inference = VariableElimination(model)

# Query for P(L | D=0, I=1)
result = inference.query(variables=['L'], evidence={'D': 0, 'I': 1})
print("P(L | D=0, I=1):")
print(result)

# Query for P(G | S=0)
result_g_s = inference.query(variables=['G'], evidence={'S': 0})
print("\nP(G | S=0):")
print(result_g_s)

view raw JSON →