Skip to content

Precise Parameters Learning ​

The networks in the other examples are written out by hand, with every conditional probability given. Often the structure is known but the numbers are not: they have to be learned from data. This example estimates the CPTs of a BayesianNetwork with PreciseDirichletModel: by maximum likelihood and by Laplace smoothing on complete data, and by expectation maximisation when some observations are missing. The theory behind each estimator is in the Parameter Learning chapter of the manual.

To have a ground truth to compare against, we first build a fully specified network, draw synthetic data from it, and then try to recover its parameters.

julia
using EnhancedBayesianNetworks
using DataFrames
using Random

Random.seed!(1)    # fixed seed, so that the samples and the numbers below are reproducible

The reference network ​

The familiar sprinkler network: the weather drives whether it rains and whether the sprinkler runs, and both wet the grass. It is used only to generate data; the learning step never sees these probabilities.

julia
W = DiscreteNode(:W)
W[:W => :sunny] = 0.7
W[:W => :cloudy] = 0.3

R = DiscreteNode(:R, [:W])
R[:W => :sunny, :R => :yesR] = 0.1
R[:W => :sunny, :R => :noR] = 0.9
R[:W => :cloudy, :R => :yesR] = 0.8
R[:W => :cloudy, :R => :noR] = 0.2

S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :onS] = 0.5
S[:W => :sunny, :S => :offS] = 0.5
S[:W => :cloudy, :S => :onS] = 0.1
S[:W => :cloudy, :S => :offS] = 0.9

G = DiscreteNode(:G, [:R, :S])
G[:R => :yesR, :S => :onS, :G => :wetG] = 0.99
G[:R => :yesR, :S => :onS, :G => :dryG] = 0.01
G[:R => :yesR, :S => :offS, :G => :wetG] = 0.9
G[:R => :yesR, :S => :offS, :G => :dryG] = 0.1
G[:R => :noR, :S => :onS, :G => :wetG] = 0.9
G[:R => :noR, :S => :onS, :G => :dryG] = 0.1
G[:R => :noR, :S => :offS, :G => :wetG] = 0.0
G[:R => :noR, :S => :offS, :G => :dryG] = 1.0

bn = BayesianNetwork([W, R, S, G])
add_child!(bn, :W, :R)
add_child!(bn, :W, :S)
add_child!(bn, :R, :G)
add_child!(bn, :S, :G)
order!(bn)

gplot(bn, background_color = "white")

Generating a dataset ​

sample draws independent realizations of the whole network. A thousand rows form our observed dataset, one column per node and one row per draw; only the first ten are shown.

julia
df = sample(bn, 1000)
first(df, 10)
10×4 DataFrame
RowWRSG
SymbolSymbolSymbolSymbol
1sunnynoRoffSdryG
2sunnynoRoffSdryG
3sunnynoRonSwetG
4sunnynoRoffSdryG
5cloudynoRoffSdryG
6sunnyyesRoffSwetG
7cloudynoRoffSdryG
8cloudyyesRoffSdryG
9sunnynoRonSwetG
10sunnynoRoffSdryG

Declaring the structure ​

Learning needs a structure to fill: a DirectAcyclicGraph, built node by node with add_node!, parents first. It holds no probabilities at all.

julia
dag = DirectAcyclicGraph()
add_node!(dag, :W)
add_node!(dag, :R; parents = [:W])
add_node!(dag, :S; parents = [:W])
add_node!(dag, :G; parents = [:R, :S])

Maximum likelihood ​

PreciseDirichletModel(), with the default α = 0, is maximum likelihood: every CPT entry is the relative frequency of the state among the rows with the same parent configuration.

julia
learned = learn(dag, df, PreciseDirichletModel())
order!(learned)
learned.nodes[learned.topology[:R]]
DiscreteNode: R
Parents: W
States: noR, yesR
Type: Precise

4×3 DataFrame
 Row │ W       R       Π        
     │ Symbol  Symbol  Union…   
─────┼──────────────────────────
   1 │ cloudy  noR     0.183673
   2 │ cloudy  yesR    0.816327
   3 │ sunny   noR     0.892351
   4 │ sunny   yesR    0.107649

The true values are 0.1 and 0.8 for rain on a sunny and on a cloudy day. To compare the whole network at once we measure, node by node, the largest absolute difference between a learned CPT entry and the true one. The CPTs are joined on their state columns, since the learned rows need not come in the same order as the true ones.

julia
function cpt_errors(learned, truth)
    return DataFrame(
        map(truth.nodes) do node
            t = node.cpt.data
            l = learned.nodes[learned.topology[node.name]].cpt.data
            joined = innerjoin(t, l; on = setdiff(propertynames(t), [:Π]), makeunique = true)
            (node = node.name, max_error = maximum(abs.(joined.Π .- joined.Π_1)))
        end
    )
end

cpt_errors(learned, bn)
4×2 DataFrame
Rownodemax_error
SymbolFloat64
1W0.006
2R0.0163265
3S0.0292517
4G0.0449541

The error comes from sampling alone, so it shrinks as the dataset grows, roughly as one over the square root of the sample size:

julia
DataFrame(
    map([100, 1_000, 10_000]) do n
        errors = cpt_errors(learn(dag, sample(bn, n), PreciseDirichletModel()), bn)
        (rows = n, max_error = maximum(errors.max_error))
    end
)
3×2 DataFrame
Rowrowsmax_error
Int64Float64
11000.0714286
210000.027977
3100000.00983556

Small samples and Laplace smoothing ​

On a small sample some parent configurations are seen only a handful of times, and maximum likelihood takes those few rows at face value. With thirty rows, the configurations where it rains and the sprinkler is on are rare, and their entries come out as hard 0 and 1:

julia
small = sample(bn, 30)
learned_small = learn(dag, small, PreciseDirichletModel())
learned_small.nodes[learned_small.topology[:G]]
DiscreteNode: G
Parents: R, S
States: dryG, wetG
Type: Precise

8×4 DataFrame
 Row │ R       S       G       Π        
     │ Symbol  Symbol  Symbol  Union…   
─────┼──────────────────────────────────
   1 │ noR     offS    dryG    1.0
   2 │ noR     offS    wetG    0.0
   3 │ yesR    offS    dryG    0.111111
   4 │ yesR    offS    wetG    0.888889
   5 │ noR     onS     dryG    0.0
   6 │ noR     onS     wetG    1.0
   7 │ yesR    onS     dryG    0.0
   8 │ yesR    onS     wetG    1.0

PreciseDirichletModel(1) is Laplace smoothing: one pseudo-observation per state pulls every estimate towards the uniform, strongly where data are scarce and hardly at all where they are plentiful. No entry is exactly 0 or 1 any more, so the learned network never rules out an event just because the sample missed it.

julia
learned_laplace = learn(dag, small, PreciseDirichletModel(1))
learned_laplace.nodes[learned_laplace.topology[:G]]
DiscreteNode: G
Parents: R, S
States: dryG, wetG
Type: Precise

8×4 DataFrame
 Row │ R       S       G       Π         
     │ Symbol  Symbol  Symbol  Union…    
─────┼───────────────────────────────────
   1 │ noR     offS    dryG    0.944444
   2 │ noR     offS    wetG    0.0555556
   3 │ yesR    offS    dryG    0.181818
   4 │ yesR    offS    wetG    0.818182
   5 │ noR     onS     dryG    0.166667
   6 │ noR     onS     wetG    0.833333
   7 │ yesR    onS     dryG    0.333333
   8 │ yesR    onS     wetG    0.666667

Declaring an unobserved state ​

A node's states are read from the data, but extra states can be declared with the states keyword of add_node! even if they never occur. Here a third weather state, :foggy, is possible in principle but absent from the sample.

julia
dag_foggy = DirectAcyclicGraph()
add_node!(dag_foggy, :W; states = [:foggy])
add_node!(dag_foggy, :R; parents = [:W])
add_node!(dag_foggy, :S; parents = [:W])
add_node!(dag_foggy, :G; parents = [:R, :S])

Under maximum likelihood an unobserved state gets probability exactly zero, and the rain distribution on a foggy day, a parent configuration with no data at all, falls back to the uniform:

julia
learned_foggy = learn(dag_foggy, df, PreciseDirichletModel())
learned_foggy.nodes[learned_foggy.topology[:W]]
DiscreteNode: W
Parents: none
States: cloudy, foggy, sunny
Type: Precise

3×2 DataFrame
 Row │ W       Π      
     │ Symbol  Union… 
─────┼────────────────
   1 │ cloudy  0.294
   2 │ foggy   0.0
   3 │ sunny   0.706
julia
learned_foggy.nodes[learned_foggy.topology[:R]]
DiscreteNode: R
Parents: W
States: noR, yesR
Type: Precise

6×3 DataFrame
 Row │ W       R       Π        
     │ Symbol  Symbol  Union…   
─────┼──────────────────────────
   1 │ cloudy  noR     0.183673
   2 │ cloudy  yesR    0.816327
   3 │ foggy   noR     0.5
   4 │ foggy   yesR    0.5
   5 │ sunny   noR     0.892351
   6 │ sunny   yesR    0.107649

With Laplace smoothing :foggy keeps a small probability instead of a hard zero. The foggy rain distribution is still uniform, and still only the prior speaking: nothing in a precise CPT tells a measured 0.5 from an assumed one. The Imprecise Parameters Learning example reports that case as the interval [0, 1] instead.

julia
learned_foggy = learn(dag_foggy, df, PreciseDirichletModel(1))
learned_foggy.nodes[learned_foggy.topology[:W]]
DiscreteNode: W
Parents: none
States: cloudy, foggy, sunny
Type: Precise

3×2 DataFrame
 Row │ W       Π           
     │ Symbol  Union…      
─────┼─────────────────────
   1 │ cloudy  0.294118
   2 │ foggy   0.000997009
   3 │ sunny   0.704885

Incomplete data: expectation maximisation ​

Real datasets have gaps. Here we delete 30% of the rain observations, chosen at random, and mark them missing. Because the rows to delete are picked independently of every value, the data are missing at random, the assumption under which expectation maximisation is valid.

julia
df_missing = allowmissing(df)
df_missing.R[randperm(nrow(df))[1:300]] .= missing
first(df_missing, 10)
10×4 DataFrame
RowWRSG
Symbol?Symbol?Symbol?Symbol?
1sunnynoRoffSdryG
2sunnynoRoffSdryG
3sunnynoRonSwetG
4sunnymissingoffSdryG
5cloudynoRoffSdryG
6sunnyyesRoffSwetG
7cloudynoRoffSdryG
8cloudyyesRoffSdryG
9sunnynoRonSwetG
10sunnynoRoffSdryG

When every node is precise, learn completes the data by expectation maximisation: each incomplete row is split between the states it could have taken, in proportion to their probability under the current estimate, and the CPTs are re-estimated from those weighted rows until no entry changes by more than tol.

julia
learned_em = learn(dag, df_missing, PreciseDirichletModel())
order!(learned_em)
cpt_errors(learned_em, bn)
4×2 DataFrame
Rownodemax_error
SymbolFloat64
1W0.006
2R0.0099775
3S0.0292517
4G0.0446763

For comparison, simply dropping the incomplete rows also gives unbiased estimates when values are missing at random, but it throws away the weather, sprinkler and grass observations of those 300 rows along with the missing rain:

julia
cpt_errors(learn(dag, dropmissing(df_missing), PreciseDirichletModel()), bn)
4×2 DataFrame
Rownodemax_error
SymbolFloat64
1W0.00428571
2R0.0125604
3S0.0352657
4G0.0502262

Expectation maximisation keeps every observed value. Its estimates are only as good as the missing-at-random assumption, though: if rain went unrecorded mostly on rainy days, both approaches would be biased, and nothing in the result would show it.

Inference on the learned network ​

A learned network is an ordinary BayesianNetwork. Asking for the probability of rain given that the grass is wet, the network learned from complete data, the one learned by expectation maximisation and the true network give close answers:

julia
infer(learned, :R, Evidence(:G => :wetG))
Posterior P(R | G=wetG)

R	Probability
------------------------
noR	0.5180643191130162
yesR	0.48193568088698374
julia
infer(learned_em, :R, Evidence(:G => :wetG))
Posterior P(R | G=wetG)

R	Probability
------------------------
noR	0.5163389871615379
yesR	0.48366101283846213
julia
infer(bn, :R, Evidence(:G => :wetG))
Posterior P(R | G=wetG)

R	Probability
------------------------
yesR	0.4959962317475271
noR	0.504003768252473

This page was generated using Literate.jl.