Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Discrete Bayes Net Example

Discrete Bayes Net example with famous Asia Bayes Network

Open In Colab

Helper Functions

Asia Bayes Network Example

This example demonstrates the famous Asia Bayes Network using discrete Bayes nets.

The Asia network is a classic example in probabilistic reasoning that models relationships between:

  • Visiting Asia (travel history)

  • Smoking habits

  • Diseases: Tuberculosis, Lung Cancer, Bronchitis

  • Symptoms: Dyspnea (shortness of breath)

  • Tests: X-Ray results

The network shows how these variables are conditionally dependent on each other.

Asia 
size: 8
conditional 0:  P( Asia ):
 Choice(Asia) 
 0 Leaf 0.99
 1 Leaf 0.01

conditional 1:  P( Smoking ):
 Leaf  0.5

conditional 2:  P( Tuberculosis | Asia ):
 Choice(Tuberculosis) 
 0 Choice(Asia) 
 0 0 Leaf 0.99
 0 1 Leaf 0.95
 1 Choice(Asia) 
 1 0 Leaf 0.01
 1 1 Leaf 0.05

conditional 3:  P( LungCancer | Smoking ):
 Choice(LungCancer) 
 0 Choice(Smoking) 
 0 0 Leaf 0.99
 0 1 Leaf  0.9
 1 Choice(Smoking) 
 1 0 Leaf 0.01
 1 1 Leaf  0.1

conditional 4:  P( Bronchitis | Smoking ):
 Choice(Bronchitis) 
 0 Choice(Smoking) 
 0 0 Leaf  0.7
 0 1 Leaf  0.4
 1 Choice(Smoking) 
 1 0 Leaf  0.3
 1 1 Leaf  0.6

conditional 5:  P( Either | Tuberculosis LungCancer ):
 Choice(LungCancer) 
 0 Choice(Either) 
 0 0 Choice(Tuberculosis) 
 0 0 0 Leaf    1
 0 0 1 Leaf    0
 0 1 Choice(Tuberculosis) 
 0 1 0 Leaf    0
 0 1 1 Leaf    1
 1 Choice(Either) 
 1 0 Leaf    0
 1 1 Leaf    1

conditional 6:  P( XRay | Either ):
 Choice(Either) 
 0 Choice(XRay) 
 0 0 Leaf 0.95
 0 1 Leaf 0.05
 1 Choice(XRay) 
 1 0 Leaf 0.02
 1 1 Leaf 0.98

conditional 7:  P( Dyspnea | Either Bronchitis ):
 Choice(Bronchitis) 
 0 Choice(Either) 
 0 0 Choice(Dyspnea) 
 0 0 0 Leaf  0.9
 0 0 1 Leaf  0.1
 0 1 Choice(Dyspnea) 
 0 1 0 Leaf  0.3
 0 1 1 Leaf  0.7
 1 Choice(Either) 
 1 0 Choice(Dyspnea) 
 1 0 0 Leaf  0.2
 1 0 1 Leaf  0.8
 1 1 Choice(Dyspnea) 
 1 1 0 Leaf  0.1
 1 1 1 Leaf  0.9

Convert to Factor Graph and solve

mpe: (0, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0)
bayesTree: cliques: 6, variables: 8
bayesTree- P( Smoking LungCancer Bronchitis ):
 Choice(Bronchitis) 
 0 Choice(LungCancer) 
 0 0 Choice(Smoking) 
 0 0 0 Leaf 0.3465
 0 0 1 Leaf 0.18
 0 1 Choice(Smoking) 
 0 1 0 Leaf 0.0035
 0 1 1 Leaf 0.02
 1 Choice(LungCancer) 
 1 0 Choice(Smoking) 
 1 0 0 Leaf 0.1485
 1 0 1 Leaf 0.27
 1 1 Choice(Smoking) 
 1 1 0 Leaf 0.0015
 1 1 1 Leaf 0.03

bayesTree| - P( Either | LungCancer Bronchitis ):
 Choice(LungCancer) 
 0 Choice(Either) 
 0 0 Leaf 0.9896
 0 1 Leaf 0.0104
 1 Choice(Either) 
 1 0 Leaf    0
 1 1 Leaf    1

bayesTree| | - P( Tuberculosis | Either LungCancer ):
 Choice(LungCancer) 
 0 Choice(Either) 
 0 0 Choice(Tuberculosis) 
 0 0 0 Leaf    1
 0 0 1 Leaf    0
 0 1 Choice(Tuberculosis) 
 0 1 0 Leaf    0
 0 1 1 Leaf    1
 1 Choice(Either) 
 1 0 Leaf    0
 1 1 Choice(Tuberculosis) 
 1 1 0 Leaf 0.9896
 1 1 1 Leaf 0.0104

bayesTree| | | - P( Asia | Tuberculosis ):
 Choice(Tuberculosis) 
 0 Choice(Asia) 
 0 0 Leaf 0.99040016
 0 1 Leaf 0.0095998383
 1 Choice(Asia) 
 1 0 Leaf 0.95192308
 1 1 Leaf 0.048076923

bayesTree| | - P( XRay | Either ):
 Choice(Either) 
 0 Choice(XRay) 
 0 0 Leaf 0.95
 0 1 Leaf 0.05
 1 Choice(XRay) 
 1 0 Leaf 0.02
 1 1 Leaf 0.98

bayesTree| | - P( Dyspnea | Either Bronchitis ):
 Choice(Bronchitis) 
 0 Choice(Either) 
 0 0 Choice(Dyspnea) 
 0 0 0 Leaf  0.9
 0 0 1 Leaf  0.1
 0 1 Choice(Dyspnea) 
 0 1 0 Leaf  0.3
 0 1 1 Leaf  0.7
 1 Choice(Either) 
 1 0 Choice(Dyspnea) 
 1 0 0 Leaf  0.2
 1 0 1 Leaf  0.8
 1 1 Choice(Dyspnea) 
 1 1 0 Leaf  0.1
 1 1 1 Leaf  0.9

Add evidence, solve again

mpe2: (0, 1) (1, 1) (2, 0) (3, 0) (4, 1) (5, 0) (6, 0) (7, 1)

Sample from Posterior Distribution


10 samples:
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 1) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 1) (5, 0) (6, 0) (7, 0)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 1)
sample: (0, 1) (1, 1) (2, 0) (3, 0) (4, 1) (5, 0) (6, 0) (7, 1)