Learning mixtures

class pyagrum.bnmixture.BNMLearner(weights, source, template=None, states=None)

Allows to learn mutiple BNs from a database. Learned BNs are given a weight and are stored in a BNMixture.

Notes

  • How is memory handled? First, to reduce memory consumption, only one BNLearner is instancied at most at a given time. Their are more improvements if source contains files. In that case, dataframes are loaded one at a time to reduce memory consumption. Otherwise all DataFrames are stored together.

  • We create a reference learner using the source with maximum weight.

Parameters:
  • weights (list[float]) – Weights of each sample of the database.

  • source (list[str] | list[pandas.DataFrame]) – Samples to learn from (csv format for now).

  • states (list[dict], optional) – Settings of the learner of each database (see updateState): the reference database starts from the default settings of pyagrum.BNLearner, the other ones from those of the reference.

  • template (pyagrum.BayesNet, optional) – BN to use to find modalities.

Raises:

pyagrum.ArgumentError – If arguments don’t have the same dimensions.

Scientific notes

BNMLearner learns a pyagrum.bnmixture.BNMixture from several databases on the same variables: one Bayesian network per database, learned by a pyagrum.BNLearner, with the weight given by the user to this database. Identical learned networks are merged, their weights being added. The reference network is the one learned from the database of highest weight. The mixture is queried by pyagrum.bnmixture.BNMixtureInference (Bayesian model averaging [1]).

Each database has its own learning settings (algorithm, score, correction, prior). The reference database starts from the default settings of pyagrum.BNLearner, the other ones from the settings of the reference; the settings given for a database replace these ones, the missing ones being kept.

Methods

add()

Adds a new BNLearner(its parameters) to the learner.

learnBNM()

Learns the BNs from the database and return them stored in a BNMixture with corresponding weights.

updateState()

Updates a learner: it first copies the state of the reference learner (if it exists and is not learner itself), then applies the settings given in parameters.

class pyagrum.bnmixture.BNMBootstrapLearner(source, template=None, N=100)

Allows to learn a BN and bootsrap-generated BNs. Learning a BN is not the only goal of this class. The purpose of bootstraping is to have an accuracy indicator about the BN learned from a given database.

Notes

  • How is memory handled? First, to reduce memory consumption, only one BNLearner is instancied at most at a given time.

  • To keep one BNLearner at a time, we create a reference learner. To apply a method for the learning algorithm of all bootstraped BNs, “use” methods modify the reference learner. Then the other learners make use of BNLearner.copyState to update themself according to the reference learner.

Parameters:
  • source (str | pandas.DataFrame) – Database to learn from (csv format for now).

  • template (pyagrum.BayesNet | Optional) – BN to use to find modalities.

  • N (int) – Number of bootstrap BNs (100 by default, see pyagrum.bnmixture.BNMBootstrapLearner.useIter).

Notes

Each bootstrap BN is learned from the whole database with random record weights: n times a Dirichlet(1, …, 1) vector (Bayesian bootstrap), whose means and variances are close to those of the counts of a resampling with replacement.

Scientific notes

BNMBootstrapLearner measures the confidence in a Bayesian network learned from a database by the bootstrap [2], applied to structure learning as in [3]: the network learned from the database (the reference) is compared to the networks learned from \(B\) perturbed copies of this database (N, 100 by default). Every network is learned by the same pyagrum.BNLearner, whose settings (algorithm, score, prior…) are those given to BNMBootstrapLearner, by default those of pyagrum.BNLearner. The result is a pyagrum.bnmixture.BootstrapMixture.

The weights of the records. No record is drawn: as in the Bayesian bootstrap [4], each copy keeps the \(n\) records and gives them random weights, used as record weights by the learner:

\[(p_1, \dots, p_n) \sim \mathrm{Dirichlet}(1, \dots, 1), \qquad w_j = n\, p_j .\]

The \(p_j\) are distributed as the gaps between \(n-1\) sorted uniform draws on \([0, 1]\). The weights sum to \(n\), with \(\mathbb{E}[w_j] = 1\) and \(\mathrm{Var}(w_j) = (n-1)/(n+1)\), close to the moments of the counts of the resampling with replacement (mean 1, variance \((n-1)/n\)) [4], but with continuous weights, so that no record is left out. The effective sample size \(1/\sum_j p_j^2\) of a copy (see pyagrum.WeightedSampling) is about \((n+1)/2\), as for the bootstrap.

Implementation limit. The confidence in an arc (pyagrum.bnmixture.BootstrapMixture) is counted on the arcs of the learned networks, not on the features of their Markov equivalence classes as in [3]: the orientation of an arc that is not compelled may change from one copy to the other.

Methods

learnBNM()

Learns a reference BN from the database.

updateState()

Updates a learner: it first copies the state of the reference learner (if it exists and is not learner itself), then applies the settings given in parameters.

useBDeuPrior()

useDirichletPrior()

useGreedyHillClimbing()

useIter()

Set the number of bootstrap iterations used for learning.

useK2()

useLocalSearchWithTabuList()

useMDLCorrection()

useMIIC()

useNMLCorrection()

useNoCorrection()

useNoPrior()

useScoreAIC()

useScoreBD()

useScoreBDeu()

useScoreBIC()

useScoreK2()

useScoreLog2Likelihood()

useScorefNML()

useSmoothingPrior()