Skip to content

Commit da28eaa

Browse files
committed
docs: improve the Asia network example
1 parent 8aae976 commit da28eaa

File tree

5 files changed

+114
-58
lines changed

5 files changed

+114
-58
lines changed

docs/make.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ makedocs(;
3232
"Home" => "index.md",
3333
"Background" => "background.md",
3434
"Examples" => [
35+
"Overview" => "examples-overview.md",
3536
"Asia network" => "generated/asia/main.md",
3637
],
3738
"UAI file formats" => "uai-file-formats.md",
3839
"Performance tips" => "performance.md",
3940
"API" => [
40-
"Public" => "api/public.md",
41-
"Internal" => "api/internal.md"
41+
"Public" => "api/public.md",
42+
"Internal" => "api/internal.md"
4243
],
4344
],
4445
doctest = false,

docs/src/examples-overview.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Examples
2+
3+
```@contents
4+
Pages = [
5+
"generated/asia/main.md",
6+
]
7+
Depth = 1
8+
```

docs/src/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ Solutions to the most common probabilistic inference tasks, including:
2828
```@contents
2929
Pages = [
3030
"background.md",
31-
"generated/asia/README.md",
31+
"examples-overview.md",
3232
"uai-file-formats.md",
3333
"performance.md",
34-
"ref.md",
34+
"api/public.md",
35+
"api/internal.md",
3536
]
3637
Depth = 1
3738
```

examples/asia/README.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/asia/main.jl

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,116 @@
1+
# # The ASIA network
2+
3+
# The graph below corresponds to the *ASIA network*, a simple Bayesian model
4+
# used extensively in educational settings. It was introduced by Lauritzen in
5+
# 1988 [^lauritzen1988local].
6+
7+
# ```
8+
# ┌─┐ ┌─┐
9+
# │A│ ┌──┤S├──┐
10+
# └┬┘ │ └─┘ │
11+
# │ │ │
12+
# ▼ ▼ ▼
13+
# ┌─┐ ┌─┐ ┌─┐
14+
# │T│ │L│ │B│
15+
# └┬┘ └┬┘ └┬┘
16+
# │ ┌─┐ │ │
17+
# └─►│E│◄─┘ │
18+
# └┬┘ │
19+
# ┌─┐ │ ┌─┐ │
20+
# │X│◄─┘ │D│◄─────┘
21+
# └─┘ └─┘
22+
# ```
23+
24+
# The table below explains the meanings of each random variable used in the
25+
# ASIA network model.
26+
27+
# | **Random variable** | **Meaning** |
28+
# | :---: | :--- |
29+
# | ``A`` | Recent trip to Asia |
30+
# | ``T`` | Patient has tuberculosis |
31+
# | ``S`` | Patient is a smoker |
32+
# | ``L`` | Patient has lung cancer |
33+
# | ``B`` | Patient has bronchitis |
34+
# | ``E`` | Patient hast ``T`` and/or ``L`` |
35+
# | ``X`` | Chest X-Ray is positive |
36+
# | ``D`` | Patient has dyspnoea |
37+
38+
# ---
39+
40+
# We now demonstrate how to use the TensorInference.jl package for conducting a
41+
# variety of inference tasks on the Asia network.
42+
43+
# Import the TensorInference package, which provides the functionality needed
44+
# for working with tensor networks and probabilistic graphical models.
145
using TensorInference
246

3-
# Load the model that detailed in the README and `asia.uai`.
47+
# ---
48+
49+
# Load the ASIA network model from the `asia.uai` file located in the examples directory.
50+
# See [Model file format (.uai)](@ref) for a description of the format of this file.
451
instance = read_instance(pkgdir(TensorInference, "examples", "asia", "asia.uai"))
5-
tnet = TensorNetworkModel(instance)
652

7-
# Get the probabilities (PR)
8-
probability(tnet)
53+
# ---
54+
55+
# Create a tensor network representation of the loaded model.
56+
tn = TensorNetworkModel(instance)
57+
58+
# ---
59+
60+
# Calculate the ``\log_{10}`` partition function
61+
probability(tn) |> first |> log10
62+
63+
# ---
964

10-
# Get the marginal probabilities (MAR)
11-
marginals(tnet) .|> first
65+
# Calculate the marginal probabilities of each random variable in the model.
66+
marginals(tn)
1267

13-
# The corresponding variables are
14-
get_vars(tnet)
68+
# ---
1569

16-
# Set the evidence variables "X-ray" (7) to be positive.
70+
# Retrieve the variables associated with the tensor network model.
71+
get_vars(tn)
72+
73+
# ---
74+
75+
# Set an evidence: Assume that the "X-ray" result (variable 7) is positive.
1776
set_evidence!(instance, 7=>0)
1877

19-
# Since the evidence variable may change the contraction order, we re-compute the tensor network.
20-
tnet = TensorNetworkModel(instance)
78+
# ---
79+
80+
# Since setting an evidence may affect the contraction order of the tensor network, recompute it.
81+
tn = TensorNetworkModel(instance)
82+
83+
# ---
84+
85+
# Calculate the maximum log-probability among all configurations.
86+
maximum_logp(tn)
87+
88+
# ---
2189

22-
# Get the maximum log-probabilities (MAP)
23-
maximum_logp(tnet)
90+
# Generate 10 samples from the probability distribution represented by the model.
91+
sample(tn, 10)
2492

25-
# To sample from the probability model
26-
sample(tnet, 10)
93+
# ---
2794

28-
# Get not only the maximum log-probability, but also the most probable conifguration
29-
# In the most probable configuration, the most probable one is the patient smoke (3) and has lung cancer (4)
30-
logp, cfg = most_probable_config(tnet)
95+
# Retrieve not only the maximum log-probability but also the most probable configuration.
96+
# In this configuration, the most likely outcomes are that the patient smokes (variable 3) and has lung cancer (variable 4).
97+
logp, cfg = most_probable_config(tn)
3198

32-
# Get the maximum log-probabilities (MMAP)
33-
# To get the probability of lung cancer, we need to marginalize out other variables.
99+
# ---
100+
101+
# Compute the most probable values of certain variables (e.g., 4 and 7) while marginalizing over others.
102+
# This is known as Maximum a Posteriori (MAP) estimation.
34103
mmap = MMAPModel(instance; queryvars=[4,7])
35-
# We get the most probable configurations on [4, 7]
104+
105+
# ---
106+
107+
# Get the most probable configurations for variables 4 and 7.
36108
most_probable_config(mmap)
37-
# The total probability of having lung cancer is roughly half.
109+
110+
# ---
111+
112+
# Compute the total log-probability of having lung cancer. The results suggest that the probability is roughly half.
38113
log_probability(mmap, [1, 0]), log_probability(mmap, [0, 0])
114+
115+
# [^lauritzen1988local]:
116+
# Steffen L Lauritzen and David J Spiegelhalter. Local computations with probabilities on graphical structures and their application to expert systems. *Journal of the Royal Statistical Society: Series B (Methodological)*, 50(2):157–194, 1988.

0 commit comments

Comments
 (0)