Skip to content

Imprecise Parameters Learning ​

The Precise Parameters Learning example estimates every CPT entry as a single number, so a learned 0.5 looks the same whether a thousand rows support it or none at all. Imprecise estimators report an Interval instead, and the learned network is a CredalNetwork. This example learns the same network from the same data with ImpreciseDirichletModel and QuesenberryHurst, shows that their intervals bound two different quantities, bounds the estimates over every completion of incomplete data, and keeps credal inference feasible with per-node estimators. The theory behind each estimator is in the Parameter Learning chapter of the manual.

julia
using EnhancedBayesianNetworks
using DataFrames
using Random

Random.seed!(1)    # the same seed as the precise example, so the data are the same too

The reference network ​

The sprinkler network of the precise example: the weather drives whether it rains and whether the sprinkler runs, and both wet the grass. It is used only to generate data.

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)

df = sample(bn, 1000)

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])

Imprecise Dirichlet model ​

ImpreciseDirichletModel(), with the default prior strength s = 1, gives each entry the lower and upper predictive probability, the probability that the next case with these parents is in this state, over every Dirichlet prior of that strength. This is exactly what an entry of a credal CPT means. A precise entry is printed as a number, an imprecise one as an interval:

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

4×3 DataFrame
 Row │ W       R       Π                                 
     │ Symbol  Symbol  Union…                            
─────┼───────────────────────────────────────────────────
   1 │ cloudy  noR     [0.18305084745762712, 0.18644067…
   2 │ cloudy  yesR    [0.8135593220338984, 0.816949152…
   3 │ sunny   noR     [0.8910891089108911, 0.892503536…
   4 │ sunny   yesR    [0.1074964639321075, 0.108910891…

Here the true network is known, so we can compare the learned intervals with the true parameters it was sampled from. Node by node, we check whether every true CPT entry lies inside its learned interval, how far it lies outside when it does not, and how wide the widest interval is. As in the precise example, the CPTs are joined on their state columns.

julia
function interval_check(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)
            lower = [p isa Interval ? p.lb : p for p in joined.Π_1]
            upper = [p isa Interval ? p.ub : p for p in joined.Π_1]
            distance = max.(lower .- joined.Π, joined.Π .- upper, 0.0)
            (node = node.name, contains_truth = all(iszero, distance), max_distance = maximum(distance), max_width = maximum(upper .- lower))
        end
    )
end

interval_check(learned_idm, bn)
4×4 DataFrame
Rownodecontains_truthmax_distancemax_width
SymbolBoolFloat64Float64
1Wfalse0.005294710.000999001
2Rfalse0.01355930.00338983
3Sfalse0.02881360.00338983
4Gfalse0.04207320.0144928

The intervals are narrow, close to the true parameters, and do not contain them. This is what an imprecise Dirichlet interval should do. It bounds the predictive probability given these data, which is centred on the observed frequency, and the observed frequency differs from the true parameter by a sampling error of order one over the square root of the number of rows. The interval itself has width s / (n + s) for a configuration seen n times: it measures how much the answer depends on the prior, and with a thousand rows the prior hardly matters. So the interval is a thin band next to the true parameter, just as the maximum-likelihood estimate of the precise example is a single number next to it.

Quesenberry–Hurst confidence intervals ​

QuesenberryHurst() bounds a different quantity: the parameter of the process that generated the data. Its intervals are simultaneous 95% confidence intervals for the proportions of each column: over repeated samples, all the true entries of a column are covered at once in 95% of them.

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

4×3 DataFrame
 Row │ W       R       Π                                 
     │ Symbol  Symbol  Union…                            
─────┼───────────────────────────────────────────────────
   1 │ cloudy  noR     [0.14358900077236814, 0.23191768…
   2 │ cloudy  yesR    [0.7680823158953488, 0.856410999…
   3 │ sunny   noR     [0.867329069720119, 0.9131268942…
   4 │ sunny   yesR    [0.08687310576389103, 0.13267093…
julia
interval_check(learned_qh, bn)
4×4 DataFrame
Rownodecontains_truthmax_distancemax_width
SymbolBoolFloat64Float64
1Wtrue0.00.0563886
2Rtrue0.00.0883287
3Strue0.00.0767969
4Gfalse0.01467480.0813732

The intervals are wider, and contain the truth for every node but G. The miss is the column with no rain and the sprinkler on: the true probability of dry grass is 0.1, but this sample has only 18 dry days out of 327, a sampling fluctuation the 95% interval does not reach. A 95% guarantee per column means that about one column in twenty is expected to miss, and this network has nine.

julia
learned_qh.nodes[learned_qh.topology[:G]]
DiscreteNode: G
Parents: R, S
States: dryG, wetG
Type: Credal

8×4 DataFrame
 Row │ R       S       G       Π                                 
     │ Symbol  Symbol  Symbol  Union…                            
─────┼───────────────────────────────────────────────────────────
   1 │ noR     offS    dryG    [0.989354164476419, 1.0]
   2 │ noR     offS    wetG    [0.0, 0.010645835523580968]
   3 │ yesR    offS    dryG    [0.08606271554397639, 0.16743589…
   4 │ yesR    offS    wetG    [0.832564109239651, 0.9139372844…
   5 │ noR     onS     dryG    [0.03509942642515286, 0.08532519…
   6 │ noR     onS     wetG    [0.9146748007971811, 0.964900573…
   7 │ yesR    onS     dryG    [0.0, 0.05347133652007052]
   8 │ yesR    onS     wetG    [0.9465286634799294, 1.0]

As data accumulate both kinds of interval close in on the true parameters, at different rates. A confidence interval narrows as one over the square root of the sample size, the rate of the sampling error itself, so it usually contains the true values, and misses them by little when it does not. An imprecise Dirichlet interval narrows as one over the sample size, faster than its centre approaches the truth, so it seldom contains it; but its distance from the truth vanishes all the same:

julia
DataFrame(
    map([100, 1_000, 10_000]) do n
        data = sample(bn, n)
        idm = interval_check(learn(dag, data, ImpreciseDirichletModel()), bn)
        qh = interval_check(learn(dag, data, QuesenberryHurst()), bn)
        (
            rows = n,
            idm_width = maximum(idm.max_width),
            idm_distance = maximum(idm.max_distance),
            qh_width = maximum(qh.max_width),
            qh_distance = maximum(qh.max_distance),
        )
    end
)
3×5 DataFrame
Rowrowsidm_widthidm_distanceqh_widthqh_distance
Int64Float64Float64Float64Float64
11000.1666670.06633660.4344820.0
210000.01818180.02722060.1151350.000216368
3100000.001703580.009801490.02837110.0

Both are valid credal networks, but their intervals mean different things: the imprecise Dirichlet model bounds predictive probabilities, which is the native meaning of a credal CPT entry, while Quesenberry–Hurst bounds parameters, and placing those bounds in a credal network is an interpretation. A result should say which one produced it.

Configurations without data ​

Where the two families of estimators differ most from a precise one is where there are no data at all. Declaring a third weather state, :foggy, that never occurs in 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])

learned_foggy = learn(dag_foggy, df, ImpreciseDirichletModel())
learned_foggy.nodes[learned_foggy.topology[:R]]
DiscreteNode: R
Parents: W
States: noR, yesR
Type: Credal

6×3 DataFrame
 Row │ W       R       Π                                 
     │ Symbol  Symbol  Union…                            
─────┼───────────────────────────────────────────────────
   1 │ cloudy  noR     [0.18305084745762712, 0.18644067…
   2 │ cloudy  yesR    [0.8135593220338984, 0.816949152…
   3 │ foggy   noR     [0.0, 1.0]
   4 │ foggy   yesR    [0.0, 1.0]
   5 │ sunny   noR     [0.8910891089108911, 0.892503536…
   6 │ sunny   yesR    [0.1074964639321075, 0.108910891…

The rain distribution on a foggy day is the vacuous interval [0, 1]: the data say nothing about it, and the network says so, where a precise estimator would have reported the uniform 0.5 as if it had been measured.

Incomplete data: bounds over every completion ​

As in the precise example, we delete 30% of the rain observations at random:

julia
df_missing = allowmissing(df)
df_missing.R[randperm(nrow(df))[1:300]] .= missing

With an imprecise estimator, learn assumes nothing about why the values are missing. Every entry is bounded over all the completions of the data consistent with what was observed, so the intervals of the rain node widen by exactly as much as the 300 unknown values could matter:

julia
learned_missing = learn(dag, df_missing, ImpreciseDirichletModel())
order!(learned_missing)
learned_missing.nodes[learned_missing.topology[:R]]
DiscreteNode: R
Parents: W
States: noR, yesR
Type: Credal

4×3 DataFrame
 Row │ W       R       Π                                 
     │ Symbol  Symbol  Union…                            
─────┼───────────────────────────────────────────────────
   1 │ cloudy  noR     [0.13220338983050847, 0.41355932…
   2 │ cloudy  yesR    [0.5864406779661017, 0.867796610…
   3 │ sunny   noR     [0.6294200848656294, 0.939179632…
   4 │ sunny   yesR    [0.06082036775106082, 0.37057991…
julia
interval_check(learned_missing, bn)
4×4 DataFrame
Rownodecontains_truthmax_distancemax_width
SymbolBoolFloat64Float64
1Wfalse0.005294710.000999001
2Rtrue0.00.30976
3Sfalse0.02881360.00338983
4Gfalse0.02050210.381662

The rain intervals widen by as much as the unknown values could change the predictive probability, here enough to contain the true parameters. The grass intervals widen too: rain is a parent of the grass, so a row whose rain is unknown could belong to either rain column of the grass node. The weather and sprinkler nodes do not involve rain and are unaffected. One grass column still lies next to its true parameter rather than around it, the one with no rain and the sprinkler on that already missed with Quesenberry–Hurst, because its sample has too few dry days: bounding over the completions accounts for the missing values, not for the sampling error.

For comparison, expectation maximisation, which assumes the values are missing at random, gives single numbers that here lie inside the rain bounds:

julia
learned_em = learn(dag, df_missing, PreciseDirichletModel())
learned_em.nodes[learned_em.topology[:R]]
DiscreteNode: R
Parents: W
States: noR, yesR
Type: Precise

4×3 DataFrame
 Row │ W       R       Π         
     │ Symbol  Symbol  Union…    
─────┼───────────────────────────
   1 │ cloudy  noR     0.170911
   2 │ cloudy  yesR    0.829089
   3 │ sunny   noR     0.902573
   4 │ sunny   yesR    0.0974275

QuesenberryHurst cannot be fitted from incomplete data, since its confidence region has no closed-form envelope over the completions, and learn refuses it:

julia
try
    learn(dag, df_missing, QuesenberryHurst())
catch e
    println(e.msg)
end
Invalid estimator: column(s) [:R] contain missing values, which QuesenberryHurst on node(s) [:G, :R, :S, :W] cannot be fitted from; its simultaneous confidence region has no closed-form envelope over the completions of the data, and bounding each entry separately would no longer carry the stated confidence. Use ImpreciseDirichletModel, whose bounds over the completions are exact

Inference on a learned credal network ​

A learned credal network is queried with infer like any other, and returns bounds. Asking again for the probability of rain given that the grass is wet:

julia
infer(learned_idm, :R, Evidence(:G => :wetG))
CredalPosterior P(R | G=wetG)

R	Interval
------------------------
noR	[0.514997, 0.522052]
yesR	[0.477948, 0.485003]

Extreme posteriors: 512
julia
infer(learned_missing, :R, Evidence(:G => :wetG))
CredalPosterior P(R | G=wetG)

R	Interval
------------------------
noR	[0.30429, 0.775745]
yesR	[0.224255, 0.69571]

Extreme posteriors: 512

Under the true network the probability of rain given wet grass is 0.496. The complete-data bounds are narrow and lie next to it, as the CPT intervals they come from lie next to the true parameters; the bounds from the incomplete data are wide and contain it. In both cases infer visited 512 extreme networks, one for every combination of a vertex of each column's credal set: here 9 columns with 2 vertices each.

Keeping credal inference feasible ​

The number of extreme networks is a product over every column of every imprecise node, so it grows exponentially with the size of the network. infer counts them before building any, and refuses a network with more than max_networks, which defaults to 10^6:

julia
try
    infer(learned_idm, :R, Evidence(:G => :wetG); max_networks = 100)
catch e
    println(e.msg)
end
Invalid CredalNetwork: exact credal inference enumerates every extreme Bayesian network, and this one has 512 of them, above max_networks = 100; raise max_networks or reduce the imprecision of the network

The remedy is to be imprecise only where the data are thin. With per-node estimators, declared through the estimator keyword of add_node!, a DAG can learn most nodes precisely and only a few as intervals. On a small sample of thirty rows, the grass node is the one whose rare configurations are poorly observed, so it alone is learned imprecisely:

julia
small = sample(bn, 30)

mixed = DirectAcyclicGraph()
add_node!(mixed, :W; estimator = PreciseDirichletModel(1))
add_node!(mixed, :R; parents = [:W], estimator = PreciseDirichletModel(1))
add_node!(mixed, :S; parents = [:W], estimator = PreciseDirichletModel(1))
add_node!(mixed, :G; parents = [:R, :S], estimator = ImpreciseDirichletModel())

learned_mixed = learn(mixed, small)
order!(learned_mixed)
infer(learned_mixed, :R, Evidence(:G => :wetG))
CredalPosterior P(R | G=wetG)

R	Interval
------------------------
noR	[0.531301, 0.632669]
yesR	[0.367331, 0.468699]

Extreme posteriors: 16

Only the four columns of the grass node are imprecise, so inference visits 16 extreme networks instead of 512, and the bounds show how much the answer depends on the poorly observed part of the network alone.


This page was generated using Literate.jl.