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 every extreme Bayesian network of the credal set; 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)
infer(cn::CredalNetwork, query, evidence::Evidence, scorefun=fill_factor_score)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.
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