pgm-handbook
A compact, readable Python package for probabilistic graphical models — from representation and exact inference to Monte-Carlo / MCMC and Bayesian deep learning. Built for learning: small pure-Python implementations that you can step through, each verified against known results.
| Area | Module | What you get |
|---|---|---|
| Bayesian networks | representation/bayesian_network.py |
DAG, TabularCPD, joint factorisation, d-separation |
| Markov networks | representation/markov_network.py |
clique potentials, log-linear energy, exact / IS partition function |
| Local structure | representation/local_structures.py |
Deterministic, Tree, Multiplexer, Noisy-OR, Noisy-MAX, Logistic, Linear-Gaussian CPDs |
| Template models | representation/template_models.py |
PlateModel grounding, DynamicBayesianNetwork (2-slice → unrolled HMM) |
| Exact inference | inference/exact.py |
enumeration, variable elimination |
| Sampling | inference/{importance,inverse_transform,rejection}.py |
IS, self-normalised IS, inverse CDF (continuous + discrete), rejection (envelope + box) |
| MCMC | inference/mcmc.py |
Metropolis–Hastings, Gibbs |
| Bayesian deep learning | deep_bayes/ |
BayesianLinear/MLP, SGLD optimiser + posterior-sample helpers, recurrent DynamicBNN |
Install
pip install pgm-handbook # numpy, networkx
pip install "pgm-handbook[deep]" # + torch, for deep_bayes
Python ≥ 3.9. import pgm_handbook works without PyTorch; pgm_handbook.deep_bayes needs it.
Quick start
from pgm_handbook.representation import BayesianNetwork, TabularCPD
from pgm_handbook.inference import enumeration_ask, variable_elimination
bn = BayesianNetwork()
for n in ("B", "E", "A"):
bn.add_node(n)
bn.add_edge("B", "A"); bn.add_edge("E", "A")
bn.set_cpd(TabularCPD("B", (), {(): {True: .001, False: .999}}))
bn.set_cpd(TabularCPD("E", (), {(): {True: .002, False: .998}}))
bn.set_cpd(TabularCPD("A", ("B", "E"), {
(True, True): {True: .95, False: .05}, (True, False): {True: .94, False: .06},
(False, True): {True: .29, False: .71}, (False, False): {True: .001, False: .999}}))
print(enumeration_ask("B", {"A": True}, bn)) # {False: 0.626, True: 0.374}
The full 5-node burglary network reproduces the textbook value
P(Burglary | JohnCalls, MaryCalls) ≈ 0.284 (examples/01_burglary_alarm_inference.py).
Examples and notebook
The examples and the demo notebook live in the GitHub repository (they are also included in the source distribution). From a checkout:
python examples/01_burglary_alarm_inference.py # exact inference, d-separation, explaining away
python examples/02_local_cpds_and_plates.py # local CPDs, plate model, DBN
python examples/03_monte_carlo_and_mcmc.py # IS, inverse transform, rejection, MH, Gibbs
python examples/04_bnn_sgld_uncertainty.py # BNN + SGLD, epistemic vs aleatoric uncertainty
jupyter lab notebooks/pgm_handbook_demo.ipynb # everything above, with plots
Each example asserts its numerical results, so a clean run means the numbers match theory.
Development
git clone https://github.com/DwaipayanDutta/pgm-notes-and-implementations
cd pgm-notes-and-implementations/probabilistic-graphical-models
pip install -e ".[dev]" # + pytest, torch, scipy, matplotlib, jupyter
pytest # unit tests, example smoke tests, notebook execution
Torch-dependent tests and examples are skipped automatically when PyTorch is not installed.
API notes
- Rejection sampling.
rejection_sample(target, proposal, draw, M, n)requires the envelopetarget(x) <= M * proposal(x).rejection_sample_box(target, lower, upper, M, n)uses a uniform proposal on the box, andMmust boundtargetitself on the box. Both stop with aRuntimeErroraftermax_proposalsdraws (defaultmax(10_000, 1000 * n)) rather than looping forever on a bad envelope. - SGLD.
sgld_posterior_samples(params, neg_log_lik, ...)expects the minibatch negative log-likelihood already rescaled to the full data-set size (N/B * sum); the Gaussian prior is supplied throughprior_precision.ensemble_predictturns the collected samples into a predictive distribution withmean,epistemic_std,aleatoric_stdandtotal_std. - Scaling. Exact inference is exponential in the number of variables by design — it is a teaching implementation, intended for small networks.
Layout
src/pgm_handbook/{representation,inference,deep_bayes}/
examples/ four runnable scripts
notebooks/ pgm_handbook_demo.ipynb (executed, outputs included)
tests/ pytest suite
License
MIT
Metadata
Release files for pgm-handbook 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pgm_handbook-0.1.0.tar.gz | 748.8 kB | Without provenance | Details for pgm_handbook-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pgm_handbook-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pgm_handbook-0.1.0-py3-none-any.whl |
Total release size: 773.3 kB
Release files / pgm_handbook-0.1.0.tar.gz
| Download URL | pgm_handbook-0.1.0.tar.gz |
|---|---|
| Size | 748.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1cfecf2adbb55e841e4636029b436750c158f9ff303e5c56cb8bcf4e890d10bc
|
|
BLAKE2b-256 checksum How to use checksums |
1254a4ab644b70703de25fc3e7f73452d37cc4584e8e6336fd7f602253579ed7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / pgm_handbook-0.1.0-py3-none-any.whl
| Download URL | pgm_handbook-0.1.0-py3-none-any.whl |
|---|---|
| Size | 24.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d3c2bd7f126f0eb426858b6b0a24260f48edba280ea08d9f12a88a739a75ff9
|
|
BLAKE2b-256 checksum How to use checksums |
44bb98f8c225cf77ab2d5238e41ea79028b139ee743f0badc943657d7adfb2f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for pgm-handbook 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.