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
sourcecontains 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 (seeupdateState): the reference database starts from the default settings ofpyagrum.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
Adds a new BNLearner(its parameters) to the learner. |
|
Learns the BNs from the database and return them stored in a BNMixture with corresponding weights. |
|
Updates a learner: it first copies the state of the reference learner (if it exists and is not |
- 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.copyStateto 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, seepyagrum.bnmixture.BNMBootstrapLearner.useIter).
Notes
Each bootstrap BN is learned from the whole database with random record weights:
ntimes 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:
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.
Bradley Efron. Bootstrap methods: another look at the jackknife. The Annals of Statistics, 7(1):1–26, 1979. URL: https://projecteuclid.org/journals/annals-of-statistics/volume-7/issue-1/Bootstrap-Methods-Another-Look-at-the-Jackknife/10.1214/aos/1176344552.full, doi:10.1214/aos/1176344552.
Nir Friedman, Moises Goldszmidt, and Abraham Wyner. Data analysis with Bayesian networks: a bootstrap approach. In Proceedings of the Fifteenth Conference on Uncertainty in Artificial Intelligence (UAI 1999), 196–205. 1999. URL: https://arxiv.org/abs/1301.6695.
Donald B. Rubin. The Bayesian bootstrap. The Annals of Statistics, 9(1):130–134, 1981. URL: https://projecteuclid.org/journals/annals-of-statistics/volume-9/issue-1/The-Bayesian-Bootstrap/10.1214/aos/1176345338.full, doi:10.1214/aos/1176345338.
Methods
Learns a reference BN from the database. |
|
Updates a learner: it first copies the state of the reference learner (if it exists and is not |
|
Set the number of bootstrap iterations used for learning. |
|