-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbayesian-network.lino
More file actions
57 lines (48 loc) · 2.11 KB
/
bayesian-network.lino
File metadata and controls
57 lines (48 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Bayesian Network — Sprinkler Example (Directed Acyclic Graph)
#
# Classic sprinkler network encoded as directional links:
#
# Cloudy
# ↓ ↓
# Sprinkler Rain
# ↓ ↓
# Wet Grass
#
# This is a directed acyclic graph (DAG) — edges flow from causes to effects,
# and no directed path leads back to the same node.
#
# Links notation natively describes networks of any complexity.
# Each node's probability is a link, and joint/marginal probabilities
# are computed using product and probabilistic_sum aggregators.
# Define network nodes as terms
(cloudy: cloudy is cloudy)
(sprinkler: sprinkler is sprinkler)
(rain: rain is rain)
(wet-grass: wet-grass is wet-grass)
# Configure probabilistic operators
(and: product) # P(A ∩ B) = P(A) * P(B) for independent events
(or: probabilistic_sum) # P(A ∪ B) = 1 - (1-P(A))*(1-P(B))
# Assign marginal probabilities
((cloudy = true) has probability 0.5)
((sprinkler = true) has probability 0.3)
((rain = true) has probability 0.5)
# Directed conditional dependencies (cause → effect):
# cloudy → sprinkler: P(Sprinkler | Cloudy)
# cloudy → rain: P(Rain | Cloudy)
# sprinkler → wet-grass: P(WetGrass | Sprinkler)
# rain → wet-grass: P(WetGrass | Rain)
# Query marginal probabilities
(? (cloudy = true)) # -> 0.5
(? (sprinkler = true)) # -> 0.3
(? (rain = true)) # -> 0.5
# Query joint probability: P(Cloudy AND Rain)
(? ((cloudy = true) and (rain = true))) # -> 0.25
# Query union probability: P(Sprinkler OR Rain)
(? ((sprinkler = true) or (rain = true))) # -> 0.65
# Multi-way joint: P(Cloudy AND Sprinkler AND Rain) using prefix form
(? (and (cloudy = true) (sprinkler = true) (rain = true))) # -> 0.075
# Chain rule decomposition via arithmetic:
# P(WetGrass) computed from conditionals using binary arithmetic
# P(W|S,R)=0.99, P(W|S,~R)=0.9, P(W|~S,R)=0.9, P(W|~S,~R)=0.01
# P(W) = 0.99*P(S)*P(R) + 0.9*P(S)*(1-P(R)) + 0.9*(1-P(S))*P(R) + 0.01*(1-P(S))*(1-P(R))
(? (((0.99 * 0.15) + (0.9 * 0.15)) + ((0.9 * 0.35) + (0.01 * 0.35)))) # -> 0.602