Example Notebooks#

A curated set of Jupyter notebooks that demonstrate the most common tasks in pgmpy: building models, learning from data, running inference, and performing causal analysis. Use this page to find a notebook first, then jump to the matching guide or API section for details.

Defining Bayesian Networks#

See the Defining a Custom Model guide for background.

Creating Discrete BN

Build a discrete Bayesian Network from scratch.

Creating Discrete Bayesian Networks
Creating Linear BN

Define a linear Gaussian Bayesian Network.

Creating Linear Gaussian Bayesian Networks
Dynamic BN

Model temporal dependencies with a Dynamic BN.

<no title>
Defining CPDs

Specify conditional probability distributions.

How to define TabularCPD and LinearGaussianCPD
Basic Operations on BN

Inspect, modify, and validate a BN.

Basic Operations on Bayesian Networks

Causal Discovery / Structure Learning#

See the Causal Discovery guide for background.

Structure Learning

Learn a graph structure from data.

Structure Learning in Bayesian Networks
Chow-Liu Tree

Learn tree-structured networks efficiently.

Learning Tree Structure from Data using the Chow-Liu Algorithm
TAN

Learn a tree-augmented Naive Bayes model.

Learning Tree-augmented Naive Bayes (TAN) Structure from Data
Expert Knowledge

Incorporate domain constraints into learning.

Expert Knowledge Integration with Causal Discovery

Parameter Estimation#

See the Parameter Estimation guide for background.

Discrete BN Parameters

Fit CPDs for a discrete BN.

Parameter Learning in Discrete Bayesian Networks
Factor Graph Parameters

Estimate parameters for factor graphs.

Marginal Learning in Discrete Markov Networks

Probabilistic Inference#

See the Probabilistic Inference guide for background.

Inference in Discrete BN

Query posterior probabilities with evidence.

Inference in Discrete Bayesian Network
Monty Hall

Solve the Monty Hall problem with a BN.

Monty Hall Problem

Causal Inference#

See the Causal Identification and Causal Estimation guides for background.

Causal Inference

Estimate causal effects from data.

Causal Inference Examples
Causal Games

Explore causal reasoning via games.

Causal Games

Simulations#

See the Simulations guide for background.

Simulating Data

Generate synthetic samples from a BN.

Simulating Data From Bayesian Networks

Extending pgmpy#

Extending pgmpy

Add custom models, estimators, or utilities.

Extending pgmpy
Functional Bayesian Network

Build functional CPDs for hybrid models.

Functional Bayesian Networks
Junction Tree Inference

Perform inference using junction trees.

Junction Tree Exact Inference

Tutorial Notebooks#

A series of in-depth tutorial notebooks that walk through pgmpy’s core concepts step by step — from probabilistic graphical model basics to real-world applications.

1. Introduction to PGMs

Foundations of probabilistic graphical models.

detailed_notebooks/1.IntroductiontoProbabilisticGraphicalModels
2. Bayesian Networks

Build and reason with Bayesian Networks.

detailed_notebooks/2.BayesianNetworks
3. Causal Bayesian Networks

Causal reasoning with Bayesian Networks.

detailed_notebooks/3.CausalBayesianNetworks
4. Markov Models

Undirected graphical models and Markov Networks.

detailed_notebooks/4.MarkovModels
5. Exact Inference

Variable Elimination, Belief Propagation, and more.

detailed_notebooks/5.ExactInferenceinGraphicalModels
6. Approximate Inference

Sampling-based and variational methods.

detailed_notebooks/6.ApproximateInferenceinGraphicalModels
7. Continuous Variables

Linear Gaussian and hybrid models.

detailed_notebooks/7.ParameterizingwithContinuousVariables
8. Sampling Algorithms

Forward, rejection, and likelihood-weighted sampling.

detailed_notebooks/8.SamplingAlgorithms
9. Reading & Writing Models

Import and export models in various formats.

detailed_notebooks/9.ReadingandWritingfrompgmpyfileformats
10. Learning BNs from Data

Structure and parameter learning end-to-end.

detailed_notebooks/10.LearningBayesianNetworksfromData
11. Energy & Greenhouse Gases

Real-world case study with Italian energy data.

detailed_notebooks/11.ABayesianNetworktomodeltheinfluenceofenergyconsumptionongreenhousegasesinItaly