Skip to content

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.

julia
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 -> :G

A 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.

julia
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],
)
10×3 DataFrame
RowWRG
SymbolSymbolSymbol
1sunnynodry
2sunnynodry
3sunnynodry
4sunnynowet
5sunnynodry
6sunnyyeswet
7cloudyyeswet
8cloudyyeswet
9cloudyyeswet
10cloudynodry

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 X, over the configurations πj of its parents:

L(θ∣D)=∏X∏j∏i=1kθijnij,

where θij=P(X=xi∣Pa(X)=πj), k is the number of states of X, nij counts the rows where the parents are in configuration πj and X is in state xi, and nj=∑inij counts the rows with configuration πj at all. Each column (θ1j,…,θkj) of a CPT can therefore be estimated on its own, from its counts n1j,…,nkj alone. An estimator is precisely a rule for that one column, and the same rule is applied to every column of every node.

learn takes the estimator as its third argument:

julia
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              R

The returned network is fully specified but not ordered; call order! before inference or sampling.

Precise estimates ​

PreciseDirichletModel(α) estimates each entry as

θ^ij=nij+αnj+kα,α≥0.

This is the posterior mean of θ⋅j under a symmetric Dirichlet prior with α pseudo-counts per state, the conjugate prior of the multinomial, so the posterior is again a Dirichlet and its mean has this closed form.

  • α=0 is maximum likelihood, the relative frequency nij/nj. A state never observed under a configuration gets probability exactly zero.

  • α=1 is Laplace smoothing, and any other α>0 is additive smoothing: every estimate is pulled towards the uniform 1/k, more strongly the fewer observations the configuration has.

A parent configuration absent from the data, nj=0, gets the uniform distribution in both cases. Under maximum likelihood that is a convention, since the data leave 0/0; with α>0 it is what the formula gives.

julia
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.25

The row W=foggy has no data, so it holds the uniform 1/2. That number is not a measurement but the prior speaking alone, and nothing in a precise CPT tells the two apart.

Imprecise estimates ​

ImpreciseDirichletModel

The imprecise Dirichlet model [14] replaces the single prior by the set of all Dirichlet priors of strength s, with any mean. Under a Dirichlet prior the posterior mean of θij is the predictive probability that the next case with parents in πj has X=xi, so the set of priors gives a lower and an upper predictive probability, and ImpreciseDirichletModel(s) reports them:

P―(xi∣πj,D)=nijnj+s,P―(xi∣πj,D)=nij+snj+s,s>0.

The lower bound places the s prior observations outside the state xi, the upper bound places them all inside it. The interval always contains the precise estimate with α=s/k, and its width s/(nj+s) shrinks as data accumulate, so the imprecision measures how little the data say. Walley recommends s=1 or s=2.

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 1 and the upper bounds to at least 1;

  • a configuration absent from the data gets the vacuous interval [0,1], which states that nothing is known, rather than a uniform distribution that looks like knowledge.

julia
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 χ2 test. With A the χk−12 quantile at the chosen confidence level,

θij∈A+2nij±A(A+4nij(nj−nij)/nj)2(nj+A).

The k−1 degrees of freedom make the coverage hold for all k states of the column at once, not one state at a time; for a binary node the interval reduces to the Wilson score interval. A configuration absent from the data again gets [0,1].

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 πj has X=xi, given the data, under near-ignorance about the prior. That is exactly what an entry of a credal CPT means, a set of possible distributions for the node given its parents, so the interval can be used in a CredalNetwork as it is, and inference keeps its meaning. Its width s/(nj+s) is the part of the uncertainty that comes from not knowing which prior to use: wide when there are few data, [0,1] when there are none, and vanishing as data accumulate, when every prior leads to nearly the same answer.

A QuesenberryHurst interval bounds the parameter θij of the process that generated the data: it is a confidence region, the output of a procedure that, over repeated samples, covers all the true parameters of a column at once with the chosen confidence. Its width follows the sampling error, of order 1/nj, and the coverage is approximate, since the construction relies on a large-sample approximation, and holds column by column. Placing it in a 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 nij/nj, which differs from θij by a sampling error of order 1/nj, while the interval itself narrows as 1/nj: with many data it is a thin band next to the true parameter rather than around it. That is the behaviour of any estimate from finite data, the maximum-likelihood one included, which also converges to θij without ever equalling it, and it is not a defect: the predictive probability given the data is close to the observed frequency, and the interval says so.

ImpreciseDirichletModelQuesenberryHurst
Boundsthe predictive probability of the next casethe parameter of the generating process
Uncertainty representedthe choice of prior, i.e. the lack of datasampling variability
Width shrinks as1/nj1/nj
Converges to the true parameteryesyes
Contains the true parameter at finite njnot guaranteed, and usually not when nj is largein about the chosen fraction of samples, per column
Configuration without data[0,1][0,1]
Incomplete dataexact bounds over every completionrefused
Parameterprior strength s, usually 1 or 2confidence, for instance 0.95

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 γ holds for each column separately. The posterior bounds returned by 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 γ. As a rule of thumb, for m imprecise columns covering independently it is about γm: the 9 columns of a small network learned at γ=0.95 give posterior bounds with a confidence of only about 0.959≈0.63. Report the posterior bounds with that reduced confidence, and say which estimator produced them.

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.

julia
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    R

It 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 θ(0), each iteration

  • E-step: replaces every incomplete row by all its completions, each weighted by its probability given the observed values under the current network, P(missing∣observed;θ(t)), so that the counts nij become expected counts;

  • M-step: applies the node's estimator to those expected counts, giving θ(t+1). With α=0 this is the maximum-likelihood step; with α>0 it is the same smoothed estimate, computed from the expected counts.

The iteration stops when no CPT entry changes by more than tol (default 10−4), or after max_iter iterations (default 100), in which case 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.

julia
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.297943

Row 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 πj of node X, the incomplete rows that could belong to it come in three kinds: rows with the parents in πj and X missing (nj?), rows with X missing and the parents only partly observed but consistent with πj (nj??), and rows with X=xi observed and the parents consistent with πj but incomplete (mij). A row contradicted by any observed parent is excluded, however much else is unknown. The extreme completions ascribe these rows to the configuration and the state so as to push each bound as far as it goes, which adds a virtual count to the ImpreciseDirichletModel formula:

P―(xi∣πj,D)=nijnj+v―ij+s,P―(xi∣πj,D)=nij+v―ij+snj+v―ij+s,v―ij=nj?+nj??+mij,v―ij=nj?+nj??+∑l≠imlj.

For the upper bound, every row whose value of X is unknown is taken to be xi, together with every row observed as xi that might belong to the configuration; for the lower bound, the same unknown rows are taken to be some other state, and so are the rows observed in other states. On complete data all virtual counts are zero and the formula is the ordinary one.

julia
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 W=sunny gave [0.71,0.86] for :no; here it gives [0.5,0.86]. The upper bound counts row 2, rain unknown, as :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:

  • QuesenberryHurst on 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. Use ImpreciseDirichletModel instead.

  • 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.

julia
learn(dag, incomplete, QuesenberryHurst())   # error: QuesenberryHurst cannot be fitted from incomplete data

Using the learned network ​

A learned network is an ordinary BayesianNetwork or CredalNetwork, and once ordered it supports everything described in the Inference chapter.

julia
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: 96

On 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:

N=∏X∏j|extKj(X)|,

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 612≈2⋅109 extreme nodes on its own. infer counts N before building any network and raises an error when it exceeds the max_networks keyword (default 106), instead of starting a computation that cannot finish.

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 N explode.