Inference
Index
EnhancedBayesianNetworks.CredalPosteriorEnhancedBayesianNetworks.PosteriorEnhancedBayesianNetworks.factor_scoreEnhancedBayesianNetworks.fill_factor_scoreEnhancedBayesianNetworks.fill_scoreEnhancedBayesianNetworks.inferEnhancedBayesianNetworks.joint_probability
Types
EnhancedBayesianNetworks.Posterior Type
PosteriorThe result of infer on a BayesianNetwork: the posterior distribution over the query variables given evidence. Holds the resulting probability Factor, the NetworkSchema needed to map ids back to names/states, and the original query/evidence. Display it to see the labelled probability table.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5
S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1
S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8
bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
p = infer(bn, :S, Evidence(:W => :sunny)) # Posterior P(S | W=sunny)EnhancedBayesianNetworks.CredalPosterior Type
CredalPosteriorThe result of infer on a CredalNetwork: lower and upper posterior probabilities over the query given evidence. lower/upper are the element-wise min/max Factors over the posteriors obtained from the extreme Bayesian networks of the credal set that admit the evidence. An extreme network under which the evidence has probability zero gives a 0/0 posterior and says nothing about the conditional, so it is left out of the bounds rather than folded into them; this is regular extension, and discarded counts the extremes dropped that way. schema, query, and evidence mirror Posterior. Display it to see the labelled [lower, upper] table.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5 S = DiscreteNode(:S, [:W]) S[:W => :sunny, :S => :on] = Interval(0.8, 0.95); S[:W => :sunny, :S => :off] = Interval(0.05, 0.2) S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8 cn = CredalNetwork([W, S]); add_child!(cn, :W, :S); order!(cn)
p = infer(cn, [:S], Evidence(:W => :sunny)) # CredalPosterior with lower/upper bounds ```
sourceMethods
EnhancedBayesianNetworks.infer Function
infer(bn::BayesianNetwork, query, evidence::Evidence, scorefun = fill_factor_score; progress::Bool = isinteractive())
infer(cn::CredalNetwork, query, evidence::Evidence, scorefun = fill_factor_score; progress::Bool = isinteractive(), tol::Real = 0.0)Compute the posterior over query (a Symbol or a vector of them) given evidence, by variable elimination. Returns a Posterior for a Bayesian network, or a CredalPosterior with lower/upper bounds over the credal set's extreme networks for a credal one. scorefun selects the elimination-ordering heuristic — fill_factor_score (default), fill_score, or factor_score. The query must not overlap the evidence, and both must name existing nodes/states. progress shows a progress bar over the work — the eliminated variables for a Bayesian network, the extreme networks for a credal one — and defaults to isinteractive() (shown in the REPL, silent in scripts, tests, and docs); force it with progress=true / progress=false.
On a credal network the bounds are taken over the extreme networks under which the evidence is possible; one under which P(evidence) is zero gives a 0/0 posterior and is discarded (regular extension). tol is the threshold at or below which P(evidence) counts as impossible, and defaults to 0.0, which discards exactly the structurally degenerate extremes. If the evidence is impossible under every extreme network, so that its upper probability is zero, infer raises an error rather than returning a vacuous interval.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5 S = DiscreteNode(:S, [:W]) S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1 S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8 bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
infer(bn, :S, Evidence(:W => :sunny)) # Posterior P(S | W=sunny) infer(bn, :S, Evidence()) # prior marginal P(S) ```
sourceEnhancedBayesianNetworks.joint_probability Function
joint_probability(bn::BayesianNetwork, scenario::Evidence)Return the joint probability of a complete scenario (one state per node) as the product of each node's CPT entry given its parents. Errors if any node is missing from the scenario (use infer for marginals or partial evidence) or if a state is invalid; nodes not in the network are dropped with a warning.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5
S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1
S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8
bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
joint_probability(bn, Evidence(:W => :sunny, :S => :on)) # 0.5 * 0.9 = 0.45EnhancedBayesianNetworks.factor_score Function
factor_score(ig, ns, node)Elimination-ordering heuristic (min-factor flavour): scores a node by the size of the factor its elimination would create — the product of the state-space sizes of the node and its current neighbours. Lower scores are eliminated earlier. Pass as the scorefun argument to infer.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5
S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1
S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8
bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
infer(bn, :S, Evidence(:W => :sunny), factor_score)EnhancedBayesianNetworks.fill_score Function
fill_score(ig, ns, node)Elimination-ordering heuristic (min-fill flavour): scores a node by the ratio of fill-in edges its elimination would add to the edges it would remove (0.0 when it has no neighbours). Lower scores are eliminated earlier. Pass as the scorefun argument to infer.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5
S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1
S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8
bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
infer(bn, :S, Evidence(:W => :sunny), fill_score)EnhancedBayesianNetworks.fill_factor_score Function
fill_factor_score(ig, ns, node)Default elimination-ordering heuristic for infer: a tuple (fill_score, factor_score, node) compared lexicographically — break fill_score ties by the smaller resulting factor (factor_score), then by node id for determinism. Lower is eliminated earlier.
Examples
W = DiscreteNode(:W); W[:W => :sunny] = 0.5; W[:W => :cloudy] = 0.5
S = DiscreteNode(:S, [:W])
S[:W => :sunny, :S => :on] = 0.9; S[:W => :sunny, :S => :off] = 0.1
S[:W => :cloudy, :S => :on] = 0.2; S[:W => :cloudy, :S => :off] = 0.8
bn = BayesianNetwork([W, S]); add_child!(bn, :W, :S); order!(bn)
infer(bn, :S, Evidence(:W => :sunny)) # fill_factor_score is the default
infer(bn, :S, Evidence(:W => :sunny), fill_factor_score) # or pass it explicitly