Skip to main content
v2026.11,986 entries · CC-BY 4.0

Probabilistic Graphical Models: Concepts and a pgmpy Guide

What probabilistic graphical models are, and how to learn, fit and query one with free software: pgmpy in Python and bnlearn in R.

Written and maintained by CASRAI Editorial Board

Published

On this page

A probabilistic graphical model (PGM) is a model that uses a graph to describe how a set of random variables depend on one another: each node is a random variable, and the arcs (edges) between nodes encode conditional-independence assumptions. In research practice you build one to represent uncertain knowledge, to learn its structure or parameters from data, and to compute the probability of some variables given evidence about others. This guide explains the vocabulary and walks through those three tasks with pgmpy, a free Python library, and bnlearn, a free R package.

What probabilistic graphical models are used for in research

Murphy’s introduction to graphical models and Bayesian networks (University of British Columbia, 1998) lists the kinds of applications that graphical models serve: medical diagnosis (the QMR-DT system), speech recognition with hidden Markov models, tracking with Kalman filters, turbo error-correcting codes and genetic linkage analysis. The common thread is a system with many uncertain quantities whose relationships are only partly known, where a researcher needs to ask “what is the probability of this variable if I observe those?”

Directed and undirected models

The same source separates two families. A Bayesian network is a directed graph in which each node carries a conditional probability distribution (a conditional probability table, CPT, when the variables are discrete) given its parents. An undirected graphical model, called a Markov random field or Markov network, uses separation in an undirected graph to express independence, and a directed model can be converted to an undirected one by a step Murphy calls moralization. Murphy also notes that the name “Bayesian network” reflects the use of Bayes’ rule for inference, not a commitment to Bayesian statistics, so a network can be fitted by maximum likelihood without being a Bayesian analysis in the statistical sense. For where such models sit in the wider field, see the machine learning overview; for one of the sampling techniques used for approximate inference, see the guide to Markov chain Monte Carlo.

Reading the graph: conditional independence

The graph encodes which variables become independent once others are known. Murphy’s tutorial introduces d-separation (taught through a procedure nicknamed “Bayes Ball”) as the rule for reading these independences off a directed graph, and the “explaining away” effect: when two causes both lead to one effect, learning that one cause is present can make the other less likely. Both ideas matter in practice because they determine which variables you need to measure and condition on. The separate DAGitty guide covers a tool built around reading causal diagrams in this way.

The three tasks: representation, inference and learning

Murphy organises the field around three problems, and the software below follows the same split.

  • Representation. Choose the variables and the arcs, and specify a conditional distribution for each node given its parents.
  • Inference. Compute the distribution of query variables given observed evidence. Murphy lists exact methods (variable elimination, the junction tree algorithm) and approximate methods (variational methods, sampling methods such as Markov chain Monte Carlo, and loopy belief propagation).
  • Learning. Estimate parameters and, when it is unknown, the structure from data.

The data table below maps the three learning situations in Murphy’s tutorial to the method he gives and to what each free package documents for it.

Which learning method applies

Murphy distinguishes three cases. If the structure is known and every variable is observed in every record, parameters are estimated by maximum likelihood, which for discrete variables amounts to counting. If the structure is known but some data are missing, expectation-maximisation (EM) is used. If the structure is unknown, learning becomes a search over structures using a penalised score such as BIC. pgmpy’s own description says it offers structure learning, parameter estimation for each node, and posterior inference by exact or approximate methods; bnlearn’s documentation groups its structure-learning functions into score-based, constraint-based, hybrid and local-discovery families and gives separate functions for parameter fitting and conditional probability queries.

Install or access pgmpy and bnlearn

pgmpy is documented at pgmpy.org as a “Python library for causal and probabilistic reasoning with graphical models”. Its installation page states that it supports Python 3.10 through 3.14 and gives these official routes:

  • pip install pgmpy
  • conda install -c conda-forge pgmpy (this installs the base package only; install any extras with pip in the same environment)
  • pip install git+https://github.com/pgmpy/pgmpy.git@dev for the development version

The optional extras named on that page are torch (PyTorch and Pyro), optional (torch plus daft-pgm, xgboost, litellm and pygraphviz), tests, docs and all.

bnlearn is an R package for Bayesian network learning and inference written by Marco Scutari; its project site at bnlearn.com carries the documentation, the manual pages and the book Bayesian Networks with Examples in R. If you work in R, start from that site and from the package’s own manual index. The comparison with the Python route is in the table near the end of this guide, and the overview of what R is used for gives context on the R side.

Step by step: learn a structure, fit parameters, query the network in pgmpy

The pgmpy quickstart shows the three tasks in order. The code below is the quickstart’s own; the numbering and commentary are ours.

  1. Install pgmpy with pip install pgmpy in a Python 3.10 to 3.14 environment.
  2. Learn a structure from data. The quickstart loads a bundled discrete dataset and runs the PC algorithm with a chi-square conditional-independence test:
    from pgmpy.datasets import load_dataset
    from pgmpy.causal_discovery import PC
    dataset = load_dataset("sachs_discrete")
    est = PC(ci_test="chi_square", return_type="dag")
    est.fit(dataset.data)
    print(est.causal_graph_)
  3. Fit parameters on a known structure. The quickstart loads the benchmark network “bnlearn/alarm”, simulates 1,000 records from it, builds a network from the same edges and estimates each node’s conditional probability distributions by maximum likelihood:
    from pgmpy.estimators import MaximumLikelihoodEstimator
    from pgmpy.models import DiscreteBayesianNetwork
    from pgmpy.example_models import load_model
    model = load_model("bnlearn/alarm")
    df = model.simulate(n_samples=1000, seed=42)
    bn_struct = DiscreteBayesianNetwork(model.edges())
    bn_struct.fit(df, estimator=MaximumLikelihoodEstimator)
    bn_struct.cpds
  4. Query the network with exact inference. Variable elimination returns the distribution of a query variable given evidence:
    from pgmpy.inference import VariableElimination
    infer = VariableElimination(model)
    result = infer.query(variables=["HISTORY"], evidence={"CVP": "LOW", "PCWP": "LOW"})
    print(result)

Step 2 is the unknown-structure case from the table, step 3 is the known-structure, complete-data case, and step 4 is inference. Steps 3 and 4 use the bundled ALARM network, a standard benchmark that bnlearn also bundles, which is why results are reproducible with the fixed seed in the simulation call.

Output and how to read it

est.causal_graph_ is the graph the PC algorithm returns. Because return_type="dag" was requested, it is a directed acyclic graph over the dataset’s variables; treat the arcs as the algorithm’s estimate under its independence tests and the data, not as a proven causal structure. bn_struct.cpds lists the fitted conditional probability distributions, one per node, each conditioned on that node’s parents in the graph. The result from infer.query is the posterior distribution of the queried variable, here HISTORY, given the evidence that CVP and PCWP are LOW. To read it, compare it with the same query run without evidence: the difference shows how much the observations shifted the probabilities.

When reading any network you have learned, keep Murphy’s distinction in mind between the structure (which arcs exist) and the parameters (the probabilities on them). A good fit to the parameters says nothing about whether the arcs are right, and structure learning in the unknown-structure case is a search that depends on the score or test chosen.

Troubleshooting

Import or install fails

Check the Python version first: the installation page lists support for Python 3.10 through 3.14 only. If you installed with conda, remember that the conda-forge package is the base package only; an extra such as torch or optional must be added with pip inside the same environment.

A function or class cannot be imported

The quickstart imports PC from pgmpy.causal_discovery, MaximumLikelihoodEstimator from pgmpy.estimators, DiscreteBayesianNetwork from pgmpy.models, load_model from pgmpy.example_models, load_dataset from pgmpy.datasets and VariableElimination from pgmpy.inference. If a tutorial you are following uses different import paths, compare them line by line with the current quickstart.

Which bnlearn function does what

The bnlearn manual index groups the functions: hc and tabu are score-based structure learning; gs, iamb, mmpc and others are constraint-based; h2pc, mmhc and rsmax2 are hybrid; aracne and chow.liu are local-discovery algorithms; structural.em learns structure from missing data; bn.fit fits parameters; cpquery and cpdist perform conditional probability queries; impute and predict for fitted networks fill in or predict missing values.

Free alternatives: pgmpy and bnlearn compared

Both packages are free to use and both bundle benchmark networks for practice. The data table below on this page sets out where they differ in language, installation and bundled examples, using only what each project documents.

bnlearn’s documentation lists these bundled example networks: the small synthetic data sets learning.test, gaussian.test and clgaussian.test; the literature networks lizards, marks, asia and coronary; and the benchmark networks ALARM, Hailfinder and Insurance. pgmpy offers example datasets and models through load_dataset and load_model, and its quickstart uses the “bnlearn/alarm” model, so the two projects can be practised on the same benchmark.

Frequently asked questions

Is a Bayesian network the same as a probabilistic graphical model?

No. A Bayesian network is one kind of probabilistic graphical model, the directed kind with a conditional probability distribution at each node given its parents. Undirected models, called Markov random fields or Markov networks, are the other main family.

Does using a Bayesian network mean doing Bayesian statistics?

Not necessarily. Murphy’s tutorial states that the name reflects the use of Bayes’ rule for inference, not Bayesian statistics. A network’s parameters can be estimated by maximum likelihood, as in step 3 above.

Can I learn the graph from data?

Yes. Murphy describes structure learning as a search over graphs using a penalised score such as BIC, and both pgmpy and bnlearn provide structure-learning algorithms. The pgmpy quickstart uses the PC algorithm with a chi-square test; bnlearn lists score-based, constraint-based, hybrid and local-discovery families.

What is the difference between exact and approximate inference?

Exact inference computes the query distribution without sampling error, for example by variable elimination or the junction tree algorithm. Approximate methods, listed by Murphy as variational methods, sampling and loopy belief propagation, are used when exact computation is not practical. pgmpy states that it supports both kinds.

Which should I use, pgmpy or bnlearn?

Use the one that matches the language of the rest of your analysis: pgmpy is a Python library and bnlearn is an R package. Both are free and both document structure learning, parameter fitting and inference.

Sources

  • pgmpy installation page, https://pgmpy.org/started/install.html (checked 2026-11-14)
  • pgmpy home page, https://pgmpy.org/ (checked 2026-11-14)
  • pgmpy quickstart, https://pgmpy.org/started/quickstart.html (checked 2026-11-14)
  • bnlearn, About, https://www.bnlearn.com/about/ (checked 2026-11-14)
  • bnlearn documentation, https://www.bnlearn.com/documentation/ (checked 2026-11-14)
  • bnlearn manual page index, https://www.bnlearn.com/documentation/man/index.html (checked 2026-11-14)
  • K. Murphy, A Brief Introduction to Graphical Models and Bayesian Networks (1998), https://www.cs.ubc.ca/~murphyk/Bayes/bayes.html (checked 2026-11-14)

Data: Learning cases for probabilistic graphical models and the free software that documents them

Maps the three learning situations in Murphy's graphical-models tutorial (known structure with complete data, known structure with missing data, unknown structure) to the method he names, what the pgmpy quickstart demonstrates, and the bnlearn functions listed in its manual index. Compiled from the cited pages on 2026-11-14; a cell reading 'Not shown' means the cited page does not demonstrate it.

Learning cases for probabilistic graphical models and the free software that documents them
Learning caseMethod named by Murphypgmpy (quickstart)bnlearn (manual index)Language of package
Known structure, complete dataMaximum likelihood (counting for discrete variables)fit with MaximumLikelihoodEstimator on DiscreteBayesianNetworkbn.fit, bn.net, custom.fit (fit the parameters of a Bayesian network)pgmpy: Python; bnlearn: R
Known structure, missing dataExpectation-maximisation (EM)Not shown in the quickstartstructural.em (structure learning from missing data); impute and predict for missing valuespgmpy: Python; bnlearn: R
Unknown structure, complete dataSearch over structures with a penalised score such as BICPC algorithm with ci_test chi_square, return_type dagScore-based: hc, tabu; constraint-based: gs, iamb, mmpc, pc.stable; hybrid: h2pc, mmhc, rsmax2pgmpy: Python; bnlearn: R
Query given evidenceExact (variable elimination, junction tree) or approximate (variational, sampling, loopy belief propagation)VariableElimination(model).query(variables, evidence)cpquery, cpdist (conditional probability queries)pgmpy: Python; bnlearn: R

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Probabilistic Graphical Models: Concepts and a pgmpy Guide

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →