{"id":7499,"library":"pgmpy","title":"pgmpy: Probabilistic Graphical Models","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/pgmpy/pgmpy","tags":["probabilistic graphical models","bayesian networks","markov models","inference","causal reasoning","machine learning","statistics"],"install":[{"cmd":"pip install pgmpy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Numerical operations and array handling.","package":"numpy","optional":false},{"reason":"Scientific computing, statistical functions.","package":"scipy","optional":false},{"reason":"Graph representation and manipulation for PGM structures.","package":"networkx","optional":false},{"reason":"Data manipulation, especially for learning algorithms and data input.","package":"pandas","optional":false},{"reason":"For visualizing graphical models and inference results.","package":"matplotlib","optional":true}],"imports":[{"note":"The class was renamed from `BayesianModel` to `BayesianNetwork` in v1.0.0.","wrong":"from pgmpy.models import BayesianModel","symbol":"BayesianNetwork","correct":"from pgmpy.models import BayesianNetwork"},{"note":"The class was renamed from `MarkovModel` to `MarkovNetwork` in v1.0.0.","wrong":"from pgmpy.models import MarkovModel","symbol":"MarkovNetwork","correct":"from pgmpy.models import MarkovNetwork"},{"note":"Used for defining Conditional Probability Distributions for discrete variables.","symbol":"TabularCPD","correct":"from pgmpy.factors.discrete import TabularCPD"},{"note":"Exact inference algorithms are now explicitly under `pgmpy.inference.exact`.","wrong":"from pgmpy.inference import VariableElimination","symbol":"VariableElimination","correct":"from pgmpy.inference.exact import VariableElimination"}],"quickstart":{"code":"from pgmpy.models import BayesianNetwork\nfrom pgmpy.factors.discrete import TabularCPD\nfrom pgmpy.inference import VariableElimination\n\n# 1. Define the network structure\n# D: Difficulty (Easy, Hard), I: Intelligence (Low, High)\n# G: Grade (A, B, C), L: Letter (Good, Bad), S: SAT (Low, High)\nmodel = BayesianNetwork([('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')])\n\n# 2. Define Conditional Probability Distributions (CPDs)\ncpd_d = TabularCPD(variable='D', variable_card=2, values=[[0.6], [0.4]])\ncpd_i = TabularCPD(variable='I', variable_card=2, values=[[0.7], [0.3]])\n\n# G depends on I and D. Order of evidence: I, D\ncpd_g = TabularCPD(variable='G', variable_card=3,\n                   values=[[0.3, 0.05, 0.9, 0.5],\n                           [0.4, 0.25, 0.08, 0.3],\n                           [0.3, 0.7, 0.02, 0.2]],\n                   evidence=['I', 'D'], evidence_card=[2, 2])\n\ncpd_l = TabularCPD(variable='L', variable_card=2,\n                   values=[[0.1, 0.4, 0.99],\n                           [0.9, 0.6, 0.01]],\n                   evidence=['G'], evidence_card=[3])\n\ncpd_s = TabularCPD(variable='S', variable_card=2,\n                   values=[[0.95, 0.2],\n                           [0.05, 0.8]],\n                   evidence=['I'], evidence_card=[2])\n\n# 3. Add CPDs to the model\nmodel.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)\n\n# 4. Check if the model is valid (optional but good practice)\nassert model.check_model(), \"Model is not valid!\"\n\n# 5. Perform inference\ninference = VariableElimination(model)\n\n# Query for P(L | D=0, I=1)\nresult = inference.query(variables=['L'], evidence={'D': 0, 'I': 1})\nprint(\"P(L | D=0, I=1):\")\nprint(result)\n\n# Query for P(G | S=0)\nresult_g_s = inference.query(variables=['G'], evidence={'S': 0})\nprint(\"\\nP(G | S=0):\")\nprint(result_g_s)","lang":"python","description":"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."},"warnings":[{"fix":"Update class imports from `BayesianModel` to `BayesianNetwork` and `MarkovModel` to `MarkovNetwork`.","message":"Major breaking changes were introduced in v1.0.0. Specifically, `BayesianModel` was renamed to `BayesianNetwork` and `MarkovModel` to `MarkovNetwork`. Code using the old class names will fail with an `AttributeError`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Carefully align the `values` matrix with the `evidence` variables' order and `evidence_card`. Consult the documentation for the specific indexing order (e.g., last variable changes fastest).","message":"The order of evidence variables and their cardinality is critical when defining `TabularCPD`s. An incorrect order or mismatch between `evidence` and `evidence_card` will lead to incorrect probability distributions or errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For large models, explore approximate inference algorithms (e.g., `pgmpy.inference.sampling`) or simplify the model structure.","message":"Inference on large or densely connected graphical models can be computationally expensive and memory intensive, especially for exact inference methods like Variable Elimination. Consider approximate inference for such cases.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The `BayesianModel` class was renamed to `BayesianNetwork`. Update your code to `from pgmpy.models import BayesianNetwork`.","cause":"Attempting to import or use the `BayesianModel` class in pgmpy versions 1.0.0 or later.","error":"AttributeError: module 'pgmpy.models' has no attribute 'BayesianModel'"},{"fix":"Review the `values` array in your `TabularCPD` definition. Ensure that each column (representing a parent configuration) sums to 1. Double-check the order of variables and their cardinalities.","cause":"When defining a `TabularCPD`, the probabilities for each state combination of the parent variables must sum to 1. This error indicates a mathematical inconsistency in your CPD definition.","error":"ValueError: The sum of the probability values for Variable [variable_name] is not 1."},{"fix":"Import `VariableElimination` from its specific submodule: `from pgmpy.inference.exact import VariableElimination`.","cause":"The module path for exact inference algorithms changed. `VariableElimination` is no longer directly under `pgmpy.inference`.","error":"ImportError: cannot import name 'VariableElimination' from 'pgmpy.inference'"}]}