pgm-handbook 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pgm_handbook-0.1.0/LICENSE +21 -0
- pgm_handbook-0.1.0/MANIFEST.in +4 -0
- pgm_handbook-0.1.0/PKG-INFO +141 -0
- pgm_handbook-0.1.0/README.md +101 -0
- pgm_handbook-0.1.0/examples/01_burglary_alarm_inference.py +132 -0
- pgm_handbook-0.1.0/examples/02_local_cpds_and_plates.py +236 -0
- pgm_handbook-0.1.0/examples/03_monte_carlo_and_mcmc.py +212 -0
- pgm_handbook-0.1.0/examples/04_bnn_sgld_uncertainty.py +136 -0
- pgm_handbook-0.1.0/notebooks/pgm_handbook_demo.ipynb +958 -0
- pgm_handbook-0.1.0/pyproject.toml +59 -0
- pgm_handbook-0.1.0/setup.cfg +4 -0
- pgm_handbook-0.1.0/src/pgm_handbook/__init__.py +19 -0
- pgm_handbook-0.1.0/src/pgm_handbook/deep_bayes/__init__.py +15 -0
- pgm_handbook-0.1.0/src/pgm_handbook/deep_bayes/bnn.py +170 -0
- pgm_handbook-0.1.0/src/pgm_handbook/deep_bayes/dynamic_bnn.py +105 -0
- pgm_handbook-0.1.0/src/pgm_handbook/deep_bayes/sgld.py +116 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/__init__.py +26 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/exact.py +157 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/importance.py +47 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/inverse_transform.py +35 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/mcmc.py +78 -0
- pgm_handbook-0.1.0/src/pgm_handbook/inference/rejection.py +75 -0
- pgm_handbook-0.1.0/src/pgm_handbook/representation/__init__.py +29 -0
- pgm_handbook-0.1.0/src/pgm_handbook/representation/bayesian_network.py +190 -0
- pgm_handbook-0.1.0/src/pgm_handbook/representation/local_structures.py +241 -0
- pgm_handbook-0.1.0/src/pgm_handbook/representation/markov_network.py +114 -0
- pgm_handbook-0.1.0/src/pgm_handbook/representation/template_models.py +134 -0
- pgm_handbook-0.1.0/src/pgm_handbook.egg-info/PKG-INFO +141 -0
- pgm_handbook-0.1.0/src/pgm_handbook.egg-info/SOURCES.txt +36 -0
- pgm_handbook-0.1.0/src/pgm_handbook.egg-info/dependency_links.txt +1 -0
- pgm_handbook-0.1.0/src/pgm_handbook.egg-info/requires.txt +13 -0
- pgm_handbook-0.1.0/src/pgm_handbook.egg-info/top_level.txt +1 -0
- pgm_handbook-0.1.0/tests/test_bnn.py +209 -0
- pgm_handbook-0.1.0/tests/test_exact_inference.py +126 -0
- pgm_handbook-0.1.0/tests/test_examples.py +23 -0
- pgm_handbook-0.1.0/tests/test_notebook.py +20 -0
- pgm_handbook-0.1.0/tests/test_representation.py +291 -0
- pgm_handbook-0.1.0/tests/test_sampling.py +211 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pgm-handbook contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pgm-handbook
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Probabilistic graphical models: representation, exact/approximate inference and Bayesian deep learning.
|
|
5
|
+
Author: Dwaipayan Dutta
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/DwaipayanDutta/pgm-notes-and-implementations
|
|
8
|
+
Project-URL: Repository, https://github.com/DwaipayanDutta/pgm-notes-and-implementations
|
|
9
|
+
Project-URL: Issues, https://github.com/DwaipayanDutta/pgm-notes-and-implementations/issues
|
|
10
|
+
Keywords: probabilistic graphical models,bayesian networks,markov networks,mcmc,gibbs sampling,variable elimination,bayesian neural networks,sgld
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: numpy
|
|
29
|
+
Requires-Dist: networkx
|
|
30
|
+
Provides-Extra: deep
|
|
31
|
+
Requires-Dist: torch; extra == "deep"
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest; extra == "dev"
|
|
34
|
+
Requires-Dist: torch; extra == "dev"
|
|
35
|
+
Requires-Dist: scipy; extra == "dev"
|
|
36
|
+
Requires-Dist: matplotlib; extra == "dev"
|
|
37
|
+
Requires-Dist: jupyter; extra == "dev"
|
|
38
|
+
Requires-Dist: nbconvert; extra == "dev"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# pgm-handbook
|
|
42
|
+
|
|
43
|
+
A compact, readable Python package for **probabilistic graphical models** — from representation and
|
|
44
|
+
exact inference to Monte-Carlo / MCMC and Bayesian deep learning. Built for learning: small pure-Python
|
|
45
|
+
implementations that you can step through, each verified against known results.
|
|
46
|
+
|
|
47
|
+
| Area | Module | What you get |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| Bayesian networks | `representation/bayesian_network.py` | DAG, `TabularCPD`, joint factorisation, d-separation |
|
|
50
|
+
| Markov networks | `representation/markov_network.py` | clique potentials, log-linear energy, exact / IS partition function |
|
|
51
|
+
| Local structure | `representation/local_structures.py` | Deterministic, Tree, Multiplexer, Noisy-OR, Noisy-MAX, Logistic, Linear-Gaussian CPDs |
|
|
52
|
+
| Template models | `representation/template_models.py` | `PlateModel` grounding, `DynamicBayesianNetwork` (2-slice → unrolled HMM) |
|
|
53
|
+
| Exact inference | `inference/exact.py` | enumeration, variable elimination |
|
|
54
|
+
| Sampling | `inference/{importance,inverse_transform,rejection}.py` | IS, self-normalised IS, inverse CDF (continuous + discrete), rejection (envelope + box) |
|
|
55
|
+
| MCMC | `inference/mcmc.py` | Metropolis–Hastings, Gibbs |
|
|
56
|
+
| Bayesian deep learning | `deep_bayes/` | `BayesianLinear/MLP`, SGLD optimiser + posterior-sample helpers, recurrent `DynamicBNN` |
|
|
57
|
+
|
|
58
|
+
## Install
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install pgm-handbook # numpy, networkx
|
|
62
|
+
pip install "pgm-handbook[deep]" # + torch, for deep_bayes
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Python ≥ 3.9. `import pgm_handbook` works without PyTorch; `pgm_handbook.deep_bayes` needs it.
|
|
66
|
+
|
|
67
|
+
## Quick start
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from pgm_handbook.representation import BayesianNetwork, TabularCPD
|
|
71
|
+
from pgm_handbook.inference import enumeration_ask, variable_elimination
|
|
72
|
+
|
|
73
|
+
bn = BayesianNetwork()
|
|
74
|
+
for n in ("B", "E", "A"):
|
|
75
|
+
bn.add_node(n)
|
|
76
|
+
bn.add_edge("B", "A"); bn.add_edge("E", "A")
|
|
77
|
+
bn.set_cpd(TabularCPD("B", (), {(): {True: .001, False: .999}}))
|
|
78
|
+
bn.set_cpd(TabularCPD("E", (), {(): {True: .002, False: .998}}))
|
|
79
|
+
bn.set_cpd(TabularCPD("A", ("B", "E"), {
|
|
80
|
+
(True, True): {True: .95, False: .05}, (True, False): {True: .94, False: .06},
|
|
81
|
+
(False, True): {True: .29, False: .71}, (False, False): {True: .001, False: .999}}))
|
|
82
|
+
|
|
83
|
+
print(enumeration_ask("B", {"A": True}, bn)) # {False: 0.626, True: 0.374}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The full 5-node burglary network reproduces the textbook value
|
|
87
|
+
`P(Burglary | JohnCalls, MaryCalls) ≈ 0.284` (`examples/01_burglary_alarm_inference.py`).
|
|
88
|
+
|
|
89
|
+
## Examples and notebook
|
|
90
|
+
|
|
91
|
+
The examples and the demo notebook live in the
|
|
92
|
+
[GitHub repository](https://github.com/DwaipayanDutta/pgm-notes-and-implementations)
|
|
93
|
+
(they are also included in the source distribution). From a checkout:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
python examples/01_burglary_alarm_inference.py # exact inference, d-separation, explaining away
|
|
97
|
+
python examples/02_local_cpds_and_plates.py # local CPDs, plate model, DBN
|
|
98
|
+
python examples/03_monte_carlo_and_mcmc.py # IS, inverse transform, rejection, MH, Gibbs
|
|
99
|
+
python examples/04_bnn_sgld_uncertainty.py # BNN + SGLD, epistemic vs aleatoric uncertainty
|
|
100
|
+
jupyter lab notebooks/pgm_handbook_demo.ipynb # everything above, with plots
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Each example asserts its numerical results, so a clean run means the numbers match theory.
|
|
104
|
+
|
|
105
|
+
## Development
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
git clone https://github.com/DwaipayanDutta/pgm-notes-and-implementations
|
|
109
|
+
cd pgm-notes-and-implementations/probabilistic-graphical-models
|
|
110
|
+
pip install -e ".[dev]" # + pytest, torch, scipy, matplotlib, jupyter
|
|
111
|
+
pytest # unit tests, example smoke tests, notebook execution
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Torch-dependent tests and examples are skipped automatically when PyTorch is not installed.
|
|
115
|
+
|
|
116
|
+
## API notes
|
|
117
|
+
|
|
118
|
+
* **Rejection sampling.** `rejection_sample(target, proposal, draw, M, n)` requires the envelope
|
|
119
|
+
`target(x) <= M * proposal(x)`. `rejection_sample_box(target, lower, upper, M, n)` uses a uniform
|
|
120
|
+
proposal on the box, and `M` must bound `target` itself on the box. Both stop with a
|
|
121
|
+
`RuntimeError` after `max_proposals` draws (default `max(10_000, 1000 * n)`) rather than looping
|
|
122
|
+
forever on a bad envelope.
|
|
123
|
+
* **SGLD.** `sgld_posterior_samples(params, neg_log_lik, ...)` expects the minibatch negative
|
|
124
|
+
log-likelihood already rescaled to the full data-set size (`N/B * sum`); the Gaussian prior is
|
|
125
|
+
supplied through `prior_precision`. `ensemble_predict` turns the collected samples into a predictive
|
|
126
|
+
distribution with `mean`, `epistemic_std`, `aleatoric_std` and `total_std`.
|
|
127
|
+
* **Scaling.** Exact inference is exponential in the number of variables by design — it is a teaching
|
|
128
|
+
implementation, intended for small networks.
|
|
129
|
+
|
|
130
|
+
## Layout
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
src/pgm_handbook/{representation,inference,deep_bayes}/
|
|
134
|
+
examples/ four runnable scripts
|
|
135
|
+
notebooks/ pgm_handbook_demo.ipynb (executed, outputs included)
|
|
136
|
+
tests/ pytest suite
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
MIT
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# pgm-handbook
|
|
2
|
+
|
|
3
|
+
A compact, readable Python package for **probabilistic graphical models** — from representation and
|
|
4
|
+
exact inference to Monte-Carlo / MCMC and Bayesian deep learning. Built for learning: small pure-Python
|
|
5
|
+
implementations that you can step through, each verified against known results.
|
|
6
|
+
|
|
7
|
+
| Area | Module | What you get |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| Bayesian networks | `representation/bayesian_network.py` | DAG, `TabularCPD`, joint factorisation, d-separation |
|
|
10
|
+
| Markov networks | `representation/markov_network.py` | clique potentials, log-linear energy, exact / IS partition function |
|
|
11
|
+
| Local structure | `representation/local_structures.py` | Deterministic, Tree, Multiplexer, Noisy-OR, Noisy-MAX, Logistic, Linear-Gaussian CPDs |
|
|
12
|
+
| Template models | `representation/template_models.py` | `PlateModel` grounding, `DynamicBayesianNetwork` (2-slice → unrolled HMM) |
|
|
13
|
+
| Exact inference | `inference/exact.py` | enumeration, variable elimination |
|
|
14
|
+
| Sampling | `inference/{importance,inverse_transform,rejection}.py` | IS, self-normalised IS, inverse CDF (continuous + discrete), rejection (envelope + box) |
|
|
15
|
+
| MCMC | `inference/mcmc.py` | Metropolis–Hastings, Gibbs |
|
|
16
|
+
| Bayesian deep learning | `deep_bayes/` | `BayesianLinear/MLP`, SGLD optimiser + posterior-sample helpers, recurrent `DynamicBNN` |
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install pgm-handbook # numpy, networkx
|
|
22
|
+
pip install "pgm-handbook[deep]" # + torch, for deep_bayes
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Python ≥ 3.9. `import pgm_handbook` works without PyTorch; `pgm_handbook.deep_bayes` needs it.
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from pgm_handbook.representation import BayesianNetwork, TabularCPD
|
|
31
|
+
from pgm_handbook.inference import enumeration_ask, variable_elimination
|
|
32
|
+
|
|
33
|
+
bn = BayesianNetwork()
|
|
34
|
+
for n in ("B", "E", "A"):
|
|
35
|
+
bn.add_node(n)
|
|
36
|
+
bn.add_edge("B", "A"); bn.add_edge("E", "A")
|
|
37
|
+
bn.set_cpd(TabularCPD("B", (), {(): {True: .001, False: .999}}))
|
|
38
|
+
bn.set_cpd(TabularCPD("E", (), {(): {True: .002, False: .998}}))
|
|
39
|
+
bn.set_cpd(TabularCPD("A", ("B", "E"), {
|
|
40
|
+
(True, True): {True: .95, False: .05}, (True, False): {True: .94, False: .06},
|
|
41
|
+
(False, True): {True: .29, False: .71}, (False, False): {True: .001, False: .999}}))
|
|
42
|
+
|
|
43
|
+
print(enumeration_ask("B", {"A": True}, bn)) # {False: 0.626, True: 0.374}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The full 5-node burglary network reproduces the textbook value
|
|
47
|
+
`P(Burglary | JohnCalls, MaryCalls) ≈ 0.284` (`examples/01_burglary_alarm_inference.py`).
|
|
48
|
+
|
|
49
|
+
## Examples and notebook
|
|
50
|
+
|
|
51
|
+
The examples and the demo notebook live in the
|
|
52
|
+
[GitHub repository](https://github.com/DwaipayanDutta/pgm-notes-and-implementations)
|
|
53
|
+
(they are also included in the source distribution). From a checkout:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python examples/01_burglary_alarm_inference.py # exact inference, d-separation, explaining away
|
|
57
|
+
python examples/02_local_cpds_and_plates.py # local CPDs, plate model, DBN
|
|
58
|
+
python examples/03_monte_carlo_and_mcmc.py # IS, inverse transform, rejection, MH, Gibbs
|
|
59
|
+
python examples/04_bnn_sgld_uncertainty.py # BNN + SGLD, epistemic vs aleatoric uncertainty
|
|
60
|
+
jupyter lab notebooks/pgm_handbook_demo.ipynb # everything above, with plots
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Each example asserts its numerical results, so a clean run means the numbers match theory.
|
|
64
|
+
|
|
65
|
+
## Development
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
git clone https://github.com/DwaipayanDutta/pgm-notes-and-implementations
|
|
69
|
+
cd pgm-notes-and-implementations/probabilistic-graphical-models
|
|
70
|
+
pip install -e ".[dev]" # + pytest, torch, scipy, matplotlib, jupyter
|
|
71
|
+
pytest # unit tests, example smoke tests, notebook execution
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Torch-dependent tests and examples are skipped automatically when PyTorch is not installed.
|
|
75
|
+
|
|
76
|
+
## API notes
|
|
77
|
+
|
|
78
|
+
* **Rejection sampling.** `rejection_sample(target, proposal, draw, M, n)` requires the envelope
|
|
79
|
+
`target(x) <= M * proposal(x)`. `rejection_sample_box(target, lower, upper, M, n)` uses a uniform
|
|
80
|
+
proposal on the box, and `M` must bound `target` itself on the box. Both stop with a
|
|
81
|
+
`RuntimeError` after `max_proposals` draws (default `max(10_000, 1000 * n)`) rather than looping
|
|
82
|
+
forever on a bad envelope.
|
|
83
|
+
* **SGLD.** `sgld_posterior_samples(params, neg_log_lik, ...)` expects the minibatch negative
|
|
84
|
+
log-likelihood already rescaled to the full data-set size (`N/B * sum`); the Gaussian prior is
|
|
85
|
+
supplied through `prior_precision`. `ensemble_predict` turns the collected samples into a predictive
|
|
86
|
+
distribution with `mean`, `epistemic_std`, `aleatoric_std` and `total_std`.
|
|
87
|
+
* **Scaling.** Exact inference is exponential in the number of variables by design — it is a teaching
|
|
88
|
+
implementation, intended for small networks.
|
|
89
|
+
|
|
90
|
+
## Layout
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
src/pgm_handbook/{representation,inference,deep_bayes}/
|
|
94
|
+
examples/ four runnable scripts
|
|
95
|
+
notebooks/ pgm_handbook_demo.ipynb (executed, outputs included)
|
|
96
|
+
tests/ pytest suite
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Example 01 — classic Burglary / Earthquake / Alarm / John / Mary network.
|
|
2
|
+
|
|
3
|
+
Reproduces the textbook d-separation and inference result:
|
|
4
|
+
|
|
5
|
+
P(B = True | J = True, M = True) ≈ 0.284
|
|
6
|
+
|
|
7
|
+
and verifies enumeration and variable elimination agree exactly.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from pgm_handbook.representation.bayesian_network import (
|
|
13
|
+
BayesianNetwork,
|
|
14
|
+
TabularCPD,
|
|
15
|
+
)
|
|
16
|
+
from pgm_handbook.inference.exact import (
|
|
17
|
+
enumeration_ask,
|
|
18
|
+
variable_elimination,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def build_burglary_network() -> BayesianNetwork:
|
|
23
|
+
"""Build the classic Pearl (1988) burglary network."""
|
|
24
|
+
bn = BayesianNetwork()
|
|
25
|
+
|
|
26
|
+
for node in ("Burglary", "Earthquake", "Alarm", "JohnCalls", "MaryCalls"):
|
|
27
|
+
bn.add_node(node)
|
|
28
|
+
|
|
29
|
+
bn.set_cpd(
|
|
30
|
+
TabularCPD(
|
|
31
|
+
"Burglary",
|
|
32
|
+
(),
|
|
33
|
+
{(): {True: 0.001, False: 0.999}},
|
|
34
|
+
)
|
|
35
|
+
)
|
|
36
|
+
bn.set_cpd(
|
|
37
|
+
TabularCPD(
|
|
38
|
+
"Earthquake",
|
|
39
|
+
(),
|
|
40
|
+
{(): {True: 0.002, False: 0.998}},
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
bn.set_cpd(
|
|
44
|
+
TabularCPD(
|
|
45
|
+
"Alarm",
|
|
46
|
+
("Burglary", "Earthquake"),
|
|
47
|
+
{
|
|
48
|
+
(True, True): {True: 0.95, False: 0.05},
|
|
49
|
+
(True, False): {True: 0.94, False: 0.06},
|
|
50
|
+
(False, True): {True: 0.29, False: 0.71},
|
|
51
|
+
(False, False): {True: 0.001, False: 0.999},
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
bn.set_cpd(
|
|
56
|
+
TabularCPD(
|
|
57
|
+
"JohnCalls",
|
|
58
|
+
("Alarm",),
|
|
59
|
+
{
|
|
60
|
+
(True,): {True: 0.90, False: 0.10},
|
|
61
|
+
(False,): {True: 0.05, False: 0.95},
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
bn.set_cpd(
|
|
66
|
+
TabularCPD(
|
|
67
|
+
"MaryCalls",
|
|
68
|
+
("Alarm",),
|
|
69
|
+
{
|
|
70
|
+
(True,): {True: 0.70, False: 0.30},
|
|
71
|
+
(False,): {True: 0.01, False: 0.99},
|
|
72
|
+
},
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
return bn
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def main() -> None:
|
|
79
|
+
bn = build_burglary_network()
|
|
80
|
+
print("BN structure:", bn.nodes())
|
|
81
|
+
print("BN edges :", bn.edges())
|
|
82
|
+
|
|
83
|
+
# ---------- 1. Posterior via enumeration ----------
|
|
84
|
+
evidence = {"JohnCalls": True, "MaryCalls": True}
|
|
85
|
+
post_enum = enumeration_ask("Burglary", evidence, bn)
|
|
86
|
+
print("\nEnumeration P(B | J, M):", post_enum)
|
|
87
|
+
|
|
88
|
+
# ---------- 2. Posterior via variable elimination ----------
|
|
89
|
+
# Order chosen to demonstrate that factors get combined sensibly.
|
|
90
|
+
order = ["JohnCalls", "MaryCalls", "Alarm", "Earthquake"]
|
|
91
|
+
post_ve = variable_elimination("Burglary", evidence, bn, order)
|
|
92
|
+
print("Var. Elim. P(B | J, M):", post_ve)
|
|
93
|
+
|
|
94
|
+
# ---------- 3. Verification ----------
|
|
95
|
+
p_true = post_enum[True]
|
|
96
|
+
print(f"\nP(B=True | J=True, M=True) = {p_true:.6f}")
|
|
97
|
+
assert abs(p_true - 0.284) < 1e-3, f"Expected ~0.284, got {p_true}"
|
|
98
|
+
assert abs(post_enum[True] - post_ve[True]) < 1e-9
|
|
99
|
+
assert abs(post_enum[False] - post_ve[False]) < 1e-9
|
|
100
|
+
print("✓ Enumeration matches Variable Elimination")
|
|
101
|
+
print("✓ Posterior ≈ 0.284 (textbook value)")
|
|
102
|
+
|
|
103
|
+
# ---------- 4. Evidence changes the picture ----------
|
|
104
|
+
# Knowing John called but *not* Mary makes burglary more likely than
|
|
105
|
+
# the trivial prior — but less so than when both call.
|
|
106
|
+
p_j_only = enumeration_ask("Burglary", {"JohnCalls": True}, bn)
|
|
107
|
+
p_prior = enumeration_ask("Burglary", {}, bn)
|
|
108
|
+
print(f"\nP(B=True | J=True) = {p_j_only[True]:.6f}")
|
|
109
|
+
print(f"P(B=True) = {p_prior[True]:.6f}")
|
|
110
|
+
assert p_j_only[True] > p_prior[True]
|
|
111
|
+
assert p_j_only[True] < p_true
|
|
112
|
+
print("✓ Single-call posterior lies between prior and joint-call posterior")
|
|
113
|
+
|
|
114
|
+
# ---------- 5. d-separation sanity checks ----------
|
|
115
|
+
# John and Mary are d-separated given Alarm.
|
|
116
|
+
assert bn.is_d_separated(["JohnCalls"], ["MaryCalls"], ["Alarm"])
|
|
117
|
+
# ...but *dependent* when Alarm is unobserved (they share a parent).
|
|
118
|
+
assert not bn.is_d_separated(["JohnCalls"], ["MaryCalls"], [])
|
|
119
|
+
# Alarm *explains away*: given Alarm, evidence of Earthquake reduces
|
|
120
|
+
# belief in Burglary.
|
|
121
|
+
p_b_alarm = enumeration_ask("Burglary", {"Alarm": True}, bn)
|
|
122
|
+
p_b_alarm_eq = enumeration_ask(
|
|
123
|
+
"Burglary", {"Alarm": True, "Earthquake": True}, bn
|
|
124
|
+
)
|
|
125
|
+
print(f"\nP(B=True | Alarm=True) = {p_b_alarm[True]:.6f}")
|
|
126
|
+
print(f"P(B=True | Alarm=True, Earthquake=True) = {p_b_alarm_eq[True]:.6f}")
|
|
127
|
+
assert p_b_alarm_eq[True] < p_b_alarm[True]
|
|
128
|
+
print("✓ Explaining-away confirmed (v-structure)")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
if __name__ == "__main__":
|
|
132
|
+
main()
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"""Example 02 — compact local CPDs, plate models and dynamic Bayesian networks.
|
|
2
|
+
|
|
3
|
+
Demonstrates:
|
|
4
|
+
* DeterministicCPD / MultiplexerCPD / NoisyORCPD / NoisyMAXCPD
|
|
5
|
+
/ LogisticCPD / LinearGaussianCPD
|
|
6
|
+
* PlateModel grounding a replicated template into a BayesianNetwork
|
|
7
|
+
* DynamicBayesianNetwork unrolling an HMM-style 2-slice model
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import math
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
|
|
16
|
+
from pgm_handbook.representation.bayesian_network import (
|
|
17
|
+
BayesianNetwork,
|
|
18
|
+
TabularCPD,
|
|
19
|
+
)
|
|
20
|
+
from pgm_handbook.representation.local_structures import (
|
|
21
|
+
DeterministicCPD,
|
|
22
|
+
MultiplexerCPD,
|
|
23
|
+
NoisyORCPD,
|
|
24
|
+
NoisyMAXCPD,
|
|
25
|
+
LogisticCPD,
|
|
26
|
+
LinearGaussianCPD,
|
|
27
|
+
)
|
|
28
|
+
from pgm_handbook.representation.template_models import (
|
|
29
|
+
PlateModel,
|
|
30
|
+
DynamicBayesianNetwork,
|
|
31
|
+
)
|
|
32
|
+
from pgm_handbook.inference.exact import enumeration_ask
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ---------------------------------------------------------------------------
|
|
36
|
+
# 1. Local CPDs
|
|
37
|
+
# ---------------------------------------------------------------------------
|
|
38
|
+
def demo_local_cpds() -> None:
|
|
39
|
+
print("=" * 70)
|
|
40
|
+
print("1. Local CPDs")
|
|
41
|
+
print("=" * 70)
|
|
42
|
+
|
|
43
|
+
# Deterministic
|
|
44
|
+
det = DeterministicCPD(
|
|
45
|
+
variable="Out", parents=("A", "B"), func=lambda a, b: a and b
|
|
46
|
+
)
|
|
47
|
+
print("Deterministic (A AND B):",
|
|
48
|
+
{v: det.prob(v, (True, True)) for v in [True, False]})
|
|
49
|
+
|
|
50
|
+
# Multiplexer
|
|
51
|
+
mux = MultiplexerCPD(
|
|
52
|
+
variable="Out",
|
|
53
|
+
selector="Sel",
|
|
54
|
+
choices=("A", "B", "C"),
|
|
55
|
+
domain=["x", "y", "z"],
|
|
56
|
+
)
|
|
57
|
+
# Sel = 1 -> choose B
|
|
58
|
+
print("Multiplexer (Sel=1, B='y'):",
|
|
59
|
+
{v: mux.prob(v, (1, "x", "y", "z")) for v in ["x", "y", "z"]})
|
|
60
|
+
|
|
61
|
+
# Noisy-OR
|
|
62
|
+
nor = NoisyORCPD(
|
|
63
|
+
variable="Fever",
|
|
64
|
+
parents=("Flu", "Cold"),
|
|
65
|
+
q=(0.95, 0.4),
|
|
66
|
+
leak=0.01,
|
|
67
|
+
)
|
|
68
|
+
for fa, ca in [(False, False), (True, False), (False, True), (True, True)]:
|
|
69
|
+
print(f" NoisyOR Flu={fa} Cold={ca}: P(Fever=True)={nor.prob(True, (fa, ca)):.4f}")
|
|
70
|
+
|
|
71
|
+
# Noisy-MAX
|
|
72
|
+
nmax = NoisyMAXCPD(
|
|
73
|
+
variable="Symptom",
|
|
74
|
+
parents=("Flu", "Cold"),
|
|
75
|
+
distributions={
|
|
76
|
+
"Flu": {0: 0.1, 1: 0.2, 2: 0.7},
|
|
77
|
+
"Cold": {0: 0.6, 1: 0.3, 2: 0.1},
|
|
78
|
+
},
|
|
79
|
+
default=0,
|
|
80
|
+
)
|
|
81
|
+
print("NoisyMAX P(Symptom=1 | Flu=1, Cold=0):",
|
|
82
|
+
f"{nmax.prob(1, (True, False)):.4f}")
|
|
83
|
+
|
|
84
|
+
# Logistic
|
|
85
|
+
logit = LogisticCPD(
|
|
86
|
+
variable="Click",
|
|
87
|
+
parents=("Age", "Income"),
|
|
88
|
+
weights=(0.02, 0.0001),
|
|
89
|
+
bias=-1.0,
|
|
90
|
+
)
|
|
91
|
+
print("Logistic P(Click=True | Age=35, Income=50000):",
|
|
92
|
+
f"{logit.prob(True, (35, 50000)):.4f}")
|
|
93
|
+
|
|
94
|
+
# Linear-Gaussian
|
|
95
|
+
lg = LinearGaussianCPD(
|
|
96
|
+
variable="Sensor",
|
|
97
|
+
parents=("Temp",),
|
|
98
|
+
weights=(1.0,),
|
|
99
|
+
bias=0.0,
|
|
100
|
+
variance=0.25,
|
|
101
|
+
)
|
|
102
|
+
rng = np.random.default_rng(0)
|
|
103
|
+
samples = [lg.sample((20.0,), rng) for _ in range(5)]
|
|
104
|
+
print("LinearGaussian mean(20) =", lg.mean((20.0,)),
|
|
105
|
+
"| sample:", [round(s, 3) for s in samples])
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ---------------------------------------------------------------------------
|
|
109
|
+
# 2. Plate model: student grades
|
|
110
|
+
# ---------------------------------------------------------------------------
|
|
111
|
+
def demo_plate() -> None:
|
|
112
|
+
print()
|
|
113
|
+
print("=" * 70)
|
|
114
|
+
print("2. Plate model — 3 students with Difficulty/Intelligence -> Grade")
|
|
115
|
+
print("=" * 70)
|
|
116
|
+
|
|
117
|
+
plate = PlateModel(index_name="i", index_values=[0, 1, 2])
|
|
118
|
+
|
|
119
|
+
# Prior on Intelligence
|
|
120
|
+
plate.add_template(
|
|
121
|
+
"Int_{i}",
|
|
122
|
+
(),
|
|
123
|
+
lambda name, _parents: TabularCPD(
|
|
124
|
+
name, (), {(): {0: 0.7, 1: 0.3}}
|
|
125
|
+
),
|
|
126
|
+
)
|
|
127
|
+
# Prior on Difficulty
|
|
128
|
+
plate.add_template(
|
|
129
|
+
"Diff_{i}",
|
|
130
|
+
(),
|
|
131
|
+
lambda name, _parents: TabularCPD(
|
|
132
|
+
name, (), {(): {0: 0.4, 1: 0.6}}
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
# Grade | Intelligence, Difficulty
|
|
136
|
+
grade_table = {
|
|
137
|
+
(0, 0): {0: 0.3, 1: 0.4, 2: 0.3},
|
|
138
|
+
(0, 1): {0: 0.05, 1: 0.25, 2: 0.7},
|
|
139
|
+
(1, 0): {0: 0.9, 1: 0.08, 2: 0.02},
|
|
140
|
+
(1, 1): {0: 0.5, 1: 0.3, 2: 0.2},
|
|
141
|
+
}
|
|
142
|
+
plate.add_template(
|
|
143
|
+
"Grade_{i}",
|
|
144
|
+
("Int_{i}", "Diff_{i}"),
|
|
145
|
+
lambda name, parents: TabularCPD(name, parents, grade_table),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
bn = plate.ground()
|
|
149
|
+
print("Grounded nodes:", sorted(bn.nodes()))
|
|
150
|
+
print("Grounded edges:", sorted(bn.edges()))
|
|
151
|
+
assert len(bn.nodes()) == 9
|
|
152
|
+
assert len(bn.edges()) == 6
|
|
153
|
+
|
|
154
|
+
# Every student's marginal P(Grade) should match the prior-averaged table.
|
|
155
|
+
# (First student only — conditional independence on the plate.)
|
|
156
|
+
p_grade = enumeration_ask("Grade_0", {}, bn)
|
|
157
|
+
print("P(Grade_0):", {k: round(v, 3) for k, v in p_grade.items()})
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
# ---------------------------------------------------------------------------
|
|
161
|
+
# 3. HMM as a dynamic Bayesian network
|
|
162
|
+
# ---------------------------------------------------------------------------
|
|
163
|
+
def demo_dbn() -> None:
|
|
164
|
+
print()
|
|
165
|
+
print("=" * 70)
|
|
166
|
+
print("3. Dynamic Bayesian Network — 3-step HMM")
|
|
167
|
+
print("=" * 70)
|
|
168
|
+
|
|
169
|
+
dbn = DynamicBayesianNetwork(state_vars=["Weather"])
|
|
170
|
+
# Initial prior
|
|
171
|
+
dbn.set_initial(
|
|
172
|
+
TabularCPD(
|
|
173
|
+
"Weather",
|
|
174
|
+
(),
|
|
175
|
+
{(): {"Sun": 0.6, "Rain": 0.4}},
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
# Transition (parents are in slice t-1, remapped automatically)
|
|
179
|
+
dbn.set_transition(
|
|
180
|
+
"Weather",
|
|
181
|
+
TabularCPD(
|
|
182
|
+
"Weather",
|
|
183
|
+
("Weather",),
|
|
184
|
+
{
|
|
185
|
+
("Sun",): {"Sun": 0.7, "Rain": 0.3},
|
|
186
|
+
("Rain",): {"Sun": 0.4, "Rain": 0.6},
|
|
187
|
+
},
|
|
188
|
+
),
|
|
189
|
+
)
|
|
190
|
+
# Observation (same slice)
|
|
191
|
+
dbn.add_observation(
|
|
192
|
+
"Umbrella",
|
|
193
|
+
TabularCPD(
|
|
194
|
+
"Umbrella",
|
|
195
|
+
("Weather",),
|
|
196
|
+
{
|
|
197
|
+
("Sun",): {"yes": 0.1, "no": 0.9},
|
|
198
|
+
("Rain",): {"yes": 0.8, "no": 0.2},
|
|
199
|
+
},
|
|
200
|
+
),
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
bn = dbn.unroll(T=3)
|
|
204
|
+
print("Unrolled nodes:", sorted(bn.nodes()))
|
|
205
|
+
print("Unrolled edges:", sorted(bn.edges()))
|
|
206
|
+
assert "Weather_0" in bn.nodes()
|
|
207
|
+
assert "Weather_2" in bn.nodes()
|
|
208
|
+
assert ("Weather_0", "Weather_1") in bn.edges()
|
|
209
|
+
assert ("Weather_1", "Weather_2") in bn.edges()
|
|
210
|
+
|
|
211
|
+
# Filtered P(Weather_1 | Umbrella_1 = yes)
|
|
212
|
+
p = enumeration_ask("Weather_1", {"Umbrella_1": "yes"}, bn)
|
|
213
|
+
print("P(Weather_1 | Umbrella_1=yes):",
|
|
214
|
+
{k: round(v, 3) for k, v in p.items()})
|
|
215
|
+
|
|
216
|
+
# Smoothing: P(Weather_1 | Umbrella_0=yes, Umbrella_1=yes)
|
|
217
|
+
p2 = enumeration_ask(
|
|
218
|
+
"Weather_1", {"Umbrella_0": "yes", "Umbrella_1": "yes"}, bn
|
|
219
|
+
)
|
|
220
|
+
print("P(Weather_1 | Umbrella_0=yes, Umbrella_1=yes):",
|
|
221
|
+
{k: round(v, 3) for k, v in p2.items()})
|
|
222
|
+
|
|
223
|
+
# Sanity: seeing an umbrella today increases P(Rain).
|
|
224
|
+
assert p["Rain"] > 0.4
|
|
225
|
+
assert p2["Rain"] > 0.4
|
|
226
|
+
print("✓ Umbrella evidence raises P(Rain)")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def main() -> None:
|
|
230
|
+
demo_local_cpds()
|
|
231
|
+
demo_plate()
|
|
232
|
+
demo_dbn()
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
if __name__ == "__main__":
|
|
236
|
+
main()
|