Parameter Learning
Index
EnhancedBayesianNetworks.learnEnhancedBayesianNetworks.learn_parameters_emEnhancedBayesianNetworks.learn_parameters_mle
Methods
EnhancedBayesianNetworks.learn Function
learn(dag::DirectAcyclicGraph, df::DataFrame; alpha=0, max_iter=100, tol=1e-4)Learn the CPTs of dag from df, choosing the algorithm from the data: with no missing entries it uses learn_parameters_mle (closed-form, exact); with any missing entries it uses learn_parameters_em. alpha is the Laplace/Dirichlet pseudo-count (both algorithms); max_iter and tol control EM's convergence. Returns a fully-specified BayesianNetwork; call order! before inference or sampling. To force a specific algorithm, call learn_parameters_mle or learn_parameters_em directly.
Examples
learn(dag, df) # complete data -> MLE
learn(dag, df_with_missing) # has missing -> EM
learn(dag, df; alpha = 1) # smoothing (either algorithm)
learn(dag, df_with_missing; tol = 1e-6, max_iter = 500)EnhancedBayesianNetworks.learn_parameters_mle Function
learn_parameters_mle(dag::DirectAcyclicGraph, df::DataFrame; alpha=0)Estimate the CPTs of dag from complete data df by maximum likelihood, returning a fully-specified BayesianNetwork (call order! on it before inference or sampling).
Each node's domain is the states observed in df together with any extra states declared on the dag, so declared-but-unobserved states appear with probability 0 (or alpha-smoothed mass). For every node and every parent configuration, P(node = s | parents = config) is (count + alpha) / (total + alpha * k), with alpha a Laplace/Dirichlet pseudo-count (alpha = 0 is pure MLE, k the number of node states). A parent configuration absent from the data falls back to a uniform distribution. dag is left untouched.
Examples
dag = DirectAcyclicGraph()
add_node!(dag, :V, [:maybe])
add_node!(dag, :T; parents = [:V])
learned = learn_parameters_mle(dag, df)
order!(learned)EnhancedBayesianNetworks.learn_parameters_em Function
learn_parameters_em(dag::DirectAcyclicGraph, df::DataFrame; alpha=0, max_iter=100, tol=1e-4)Estimate the CPTs of dag from data df that may contain missing entries, by Expectation-Maximization, returning a fully-specified BayesianNetwork (call order! before inference or sampling).
Starting from uniform CPTs, each iteration does:
E-step — every row with missing values is expanded into all completions of its missing variables, each weighted by
P(missing | observed)under the current network (fromjoint_probability); fully-observed rows keep weight1.M-step — the CPTs are re-estimated by the same counting as
learn_parameters_mle, summing these weights instead of counting rows.
Iteration stops when no CPT entry changes by more than tol, or after max_iter steps. alpha is the Laplace/Dirichlet pseudo-count; node domains are the observed states plus any extra states declared on the dag. With no missing values EM reduces exactly to learn_parameters_mle. Convergence is to a local optimum, so the (uniform) initialization matters. dag is left untouched.
Examples
dag = DirectAcyclicGraph()
add_node!(dag, :V)
add_node!(dag, :T; parents = [:V])
learned = learn_parameters_em(dag, df) # df may contain `missing` entries
order!(learned)