Parameter Learning
Parameter learning fills in the conditional probability tables (CPTs) of a network from data, given its structure. You declare which nodes exist and how they depend on one another, and learn estimates every CPT from a DataFrame of observations. The estimator decides what kind of network comes out: a precise estimator gives a Bayesian Network (BN), an imprecise one gives a Credal Network (CN) whose entries are intervals.
Declaring the structure
The structure is a DirectAcyclicGraph: nodes and edges, without any probability. Nodes are declared with add_node!, parents first, and the edges are wired as each node is added, so a cycle cannot be built.
dag = DirectAcyclicGraph()
add_node!(dag, :W; states = [:foggy]) # root, :foggy kept even though it never occurs in the data
add_node!(dag, :R; parents = [:W]) # edge :W -> :R
add_node!(dag, :G; parents = [:R]) # edge :R -> :GA node's domain is the set of states observed in the data, together with any declared through the states keyword. Declaring a state is the way to keep one that the data happen not to contain: under maximum likelihood it gets probability exactly zero, while the other estimators assign it whatever they assign an unobserved state.
The data are a DataFrame with one Symbol column per node; columns that are not nodes of the DAG are ignored with a warning.
df = DataFrame(
W = [:sunny, :sunny, :sunny, :sunny, :sunny, :sunny, :cloudy, :cloudy, :cloudy, :cloudy],
R = [:no, :no, :no, :no, :no, :yes, :yes, :yes, :yes, :no],
G = [:dry, :dry, :dry, :wet, :dry, :wet, :wet, :wet, :wet, :dry],
)| Row | W | R | G |
|---|---|---|---|
| Symbol | Symbol | Symbol | |
| 1 | sunny | no | dry |
| 2 | sunny | no | dry |
| 3 | sunny | no | dry |
| 4 | sunny | no | wet |
| 5 | sunny | no | dry |
| 6 | sunny | yes | wet |
| 7 | cloudy | yes | wet |
| 8 | cloudy | yes | wet |
| 9 | cloudy | yes | wet |
| 10 | cloudy | no | dry |
Learning the parameters
Learning with a known structure splits into independent local problems. For complete data the likelihood of a network factorises over its nodes and, within each node
where
learn takes the estimator as its third argument:
bn = learn(dag, df, PreciseDirichletModel())BayesianNetwork
Nodes: 3
Edges: 2
Topology:
# Node States Parents
--------------------------------------------------------------------------------
1 W cloudy, foggy, sunny -
2 R no, yes W
3 G dry, wet RThe returned network is fully specified but not ordered; call order! before inference or sampling.
Precise estimates
PreciseDirichletModel(α) estimates each entry as
This is the posterior mean of
is maximum likelihood, the relative frequency . A state never observed under a configuration gets probability exactly zero. is Laplace smoothing, and any other is additive smoothing: every estimate is pulled towards the uniform , more strongly the fewer observations the configuration has.
A parent configuration absent from the data,
bn = learn(dag, df, PreciseDirichletModel(1))
order!(bn)
bn.nodes[bn.topology[:R]]DiscreteNode: R
Parents: W
States: no, yes
Type: Precise
6×3 DataFrame
Row │ W R Π
│ Symbol Symbol Union…
─────┼──────────────────────────
1 │ cloudy no 0.333333
2 │ cloudy yes 0.666667
3 │ foggy no 0.5
4 │ foggy yes 0.5
5 │ sunny no 0.75
6 │ sunny yes 0.25The row
Imprecise estimates
ImpreciseDirichletModel
The imprecise Dirichlet model [14] replaces the single prior by the set of all Dirichlet priors of strength ImpreciseDirichletModel(s) reports them:
The lower bound places the
Two properties make it the natural default for credal learning:
the credal set of every column is never empty: the lower bounds sum to at most
and the upper bounds to at least ; a configuration absent from the data gets the vacuous interval
, which states that nothing is known, rather than a uniform distribution that looks like knowledge.
cn = learn(dag, df, ImpreciseDirichletModel())
order!(cn)
cn.nodes[cn.topology[:R]]DiscreteNode: R
Parents: W
States: no, yes
Type: Credal
6×3 DataFrame
Row │ W R Π
│ Symbol Symbol Union…
─────┼───────────────────────────────────────────────────
1 │ cloudy no [0.2, 0.4]
2 │ cloudy yes [0.6, 0.8]
3 │ foggy no [0.0, 1.0]
4 │ foggy yes [0.0, 1.0]
5 │ sunny no [0.7142857142857143, 0.857142857…
6 │ sunny yes [0.14285714285714285, 0.28571428…QuesenberryHurst
QuesenberryHurst(confidence) gives intervals of a different nature: the simultaneous confidence intervals for multinomial proportions of [15], obtained by inverting Pearson's
The
Interpreting the intervals
Both imprecise estimators return an Interval per CPT entry, and both give a CredalNetwork, but the intervals are statements about different quantities.
An ImpreciseDirichletModel interval bounds a predictive probability: the probability that the next case with parents in CredalNetwork as it is, and inference keeps its meaning. Its width
A QuesenberryHurst interval bounds the parameter CredalNetwork reads the parameter is probably in here as the probability is somewhere in here, a reasonable but imposed interpretation.
The difference shows when the true parameter is known, as in an example that samples its data from a given network. Both kinds of interval converge to the true parameter as the data grow, but only the confidence region is built to contain it at a finite sample size. An imprecise Dirichlet interval is centred on the observed frequency
ImpreciseDirichletModel | QuesenberryHurst | |
|---|---|---|
| Bounds | the predictive probability of the next case | the parameter of the generating process |
| Uncertainty represented | the choice of prior, i.e. the lack of data | sampling variability |
| Width shrinks as | ||
| Converges to the true parameter | yes | yes |
| Contains the true parameter at finite | not guaranteed, and usually not when | in about the chosen fraction of samples, per column |
| Configuration without data | ||
| Incomplete data | exact bounds over every completion | refused |
| Parameter | prior strength | confidence, for instance |
Use ImpreciseDirichletModel to learn a credal network whose entries mean what credal entries mean: what the data support, without leaning on an arbitrary prior, in particular with small samples, rare or unobserved configurations, and values missing for unknown reasons. Use QuesenberryHurst when the intervals are meant as classical error bars on the parameters, with enough complete data for the large-sample approximation to hold. The Imprecise Parameters Learning example compares the two on the same data.
Inference does not carry the confidence level
infer treats every interval of a credal network as a set of possible distributions and bounds the posterior over all of them. For an ImpreciseDirichletModel network this is exactly what the intervals mean, so the posterior bounds are lower and upper predictive probabilities of the query. For a QuesenberryHurst network the intervals are confidence intervals, and the confidence level infer contain the true posterior whenever every column's interval contains its true parameters, so their confidence is the probability that all the columns cover at once, which is smaller than
One estimator per node
Instead of one estimator for the whole network, each node can declare its own through the estimator keyword of add_node!, and learn is then called without one. This makes it possible to be imprecise only where the data are thin. A single imprecise node makes the result a CredalNetwork.
mixed = DirectAcyclicGraph()
add_node!(mixed, :W; states = [:foggy], estimator = ImpreciseDirichletModel())
add_node!(mixed, :R; parents = [:W], estimator = PreciseDirichletModel(1))
add_node!(mixed, :G; parents = [:R], estimator = PreciseDirichletModel(1))
learn(mixed, df)CredalNetwork
Nodes: 3
Edges: 2
Precise nodes: 2
Credal nodes: 1
Topology:
# Node Precision Parents
--------------------------------------------------------------------------------
1 W Credal -
2 R Precise W
3 G Precise RIt is all or nothing: a DAG where some nodes declare an estimator and others do not is rejected, and so is passing an estimator to learn for a DAG that already declares them.
Incomplete data
Entries of the data frame may be missing. What that means depends on the estimators, and learn picks the treatment from them rather than from a separate option.
Precise networks: expectation maximisation
When every node is precise, the CPTs are fitted by expectation maximisation (EM) [16]. Starting from uniform CPTs
E-step: replaces every incomplete row by all its completions, each weighted by its probability given the observed values under the current network,
, so that the counts become expected counts; M-step: applies the node's estimator to those expected counts, giving
. With this is the maximum-likelihood step; with it is the same smoothed estimate, computed from the expected counts.
The iteration stops when no CPT entry changes by more than tol (default max_iter iterations (default learn warns that it did not converge.
EM assumes the data are missing at random: whether a value is missing may depend on what was observed, but not on the missing value itself. When that fails, for instance when a sensor drops out precisely when the reading is extreme, the estimates are biased, and nothing in the result shows it.
incomplete = allowmissing(df)
incomplete[2, :R] = missing
incomplete[7, :W] = missing
incomplete[9, :G] = missing
bn_em = learn(dag, incomplete, PreciseDirichletModel(1))
order!(bn_em)
bn_em.nodes[bn_em.topology[:R]]DiscreteNode: R
Parents: W
States: no, yes
Type: Precise
6×3 DataFrame
Row │ W R Π
│ Symbol Symbol Union…
─────┼──────────────────────────
1 │ cloudy no 0.363105
2 │ cloudy yes 0.636895
3 │ foggy no 0.475676
4 │ foggy yes 0.524324
5 │ sunny no 0.702057
6 │ sunny yes 0.297943Row 2, whose rain is unknown, is split between :no and :yes in proportion to the current estimates. Row 7, whose weather is unknown, is spread over every weather state, :foggy included, which is why the :foggy column is no longer exactly uniform: EM treats a missing value as one more thing to estimate, never as a reason for doubt.
Imprecise networks: bounds over every completion
When every node is imprecise, learn assumes nothing about why the values are missing. Each entry is bounded over every completion of the data consistent with what was observed, using the robust Bayesian estimator of [17], whose bounds are available in closed form.
For a column ImpreciseDirichletModel formula:
For the upper bound, every row whose value of
cn_missing = learn(dag, incomplete, ImpreciseDirichletModel())
order!(cn_missing)
cn_missing.nodes[cn_missing.topology[:R]]DiscreteNode: R
Parents: W
States: no, yes
Type: Credal
6×3 DataFrame
Row │ W R Π
│ Symbol Symbol Union…
─────┼────────────────────────────────────────────
1 │ cloudy no [0.2, 0.5]
2 │ cloudy yes [0.5, 0.8]
3 │ foggy no [0.0, 1.0]
4 │ foggy yes [0.0, 1.0]
5 │ sunny no [0.5, 0.8571428571428571]
6 │ sunny yes [0.14285714285714285, 0.5]The intervals are wider than on complete data by exactly as much as the missing values could matter. On complete data the column :no; here it gives :no; the lower bound counts it as :yes, and also ascribes row 7, observed as :yes with unknown weather, to the sunny configuration. This is the honest alternative to EM when the missing-at-random assumption cannot be defended.
What is refused
Two combinations have no coherent answer, and learn raises an error rather than produce one:
QuesenberryHurston incomplete data. Its region has no closed-form envelope over the completions, and bounding each entry separately would no longer carry the stated simultaneous confidence. UseImpreciseDirichletModelinstead.A DAG mixing precise and imprecise nodes on incomplete data. EM is a property of the whole network, since its E-step needs the joint distribution, while bounding over the completions is local to each node, so the two cannot be combined. Declare one kind of estimator throughout, or complete the data first.
learn(dag, incomplete, QuesenberryHurst()) # error: QuesenberryHurst cannot be fitted from incomplete dataUsing the learned network
A learned network is an ordinary BayesianNetwork or CredalNetwork, and once ordered it supports everything described in the Inference chapter.
infer(cn, :G, Evidence(:W => :sunny))CredalPosterior P(G | W=sunny)
G Interval
------------------------
dry [0.510204, 0.763265]
wet [0.236735, 0.489796]
Extreme posteriors: 96On a credal network, infer runs one variable elimination per extreme network of the credal set. Their number is the product, over every node and every parent configuration, of the number of vertices of that column's credal set:
so it grows exponentially with the number of imprecise columns. A learned network makes this concrete: every column becomes imprecise, and a node with three states and twelve parent configurations already contributes up to infer counts max_networks keyword (default
Keeping credal inference feasible
Declare an imprecise estimator only on the nodes whose data are too thin to trust, with per-node estimators, and keep the rest precise. Imprecise roots and nodes with few parents and states are cheap; an imprecise node with many parent configurations is what makes