Mixture Model
class pyagrum.bnmixture.BNMixture
Section titled “class pyagrum.bnmixture.BNMixture”A mixture of Bayesian networks where each network carries a positive weight.
The reference BN (refBN) is the BN with the highest weight; it is
recomputed by updateRef(). It serves as the structural template for
visualisation and variable look-ups, but plays no special role during inference:
BNMixtureInference returns the weight-averaged
posterior over all BNs in the mixture.
Notes
This is an experimental model.
BN(name)
Section titled “BN(name)”- Parameters:
name (
str) – Name of the variable. - Returns:
A copy of the BN with name
namein the model. - Return type:
BayesNet - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Returns: A list containing a copy of all BNs in the model.
- Return type:
list[BayesNet]
add(name, bn, w=1)
Section titled “add(name, bn, w=1)”Adds a BN to the model. If the model doesn’t have a reference BN when trying to add an element, the BN (before adding new element) with highest weight becomes the new reference.
- Parameters:
- name (
str) – Name of the BN to add. - bn (
BayesNet) – BN to add. - w (
float) – Weight of the BN.
- name (
- Raises:
- pyagrum.InvalidArgument – If the weight is negative.
- pyagrum.InvalidArgument – If the names of the variables in the BN to add are differents from the one in the reference BN.
- pyagrum.InvalidArgument – If the variables in the BN to add are differents from the one in the reference BN.
- pyagrum.InvalidArgument – If the name in argument is the same as the reference BN’s name.
- pyagrum.ArgumentError – If the name in argument already exists for a different BN in the model.
- Return type:
None
existsArc(a, b)
Section titled “existsArc(a, b)”Counts the number of time arc a -> b appears among all BNs in the model.
- Parameters:
- a (
str|int) – Tail of the arc. - b (
str|int) – Head of the arc.
- a (
- Returns:
The number of time arc
a->bappears. - Return type:
int
isNormalized()
Section titled “isNormalized()”Checks if the model is normalized (the sum of the weights equals 1).
- Return type:
bool
isValid()
Section titled “isValid()”Checks if all the weights are equal to 0. Valid if sum of the weights is not 0.
- Returns: True if weights are valid. False otherwise.
- Return type:
bool
names()
Section titled “names()”- Returns: The list of names of the BNs in the model (reference BN not included).
- Return type:
list[str]
normalize()
Section titled “normalize()”Normalizes the weights.
- Raises: pyagrum.InvalidArgument – If all weights are zero (cannot normalize).
- Return type:
None
property refBN : BayesNet | None
Section titled “property refBN : BayesNet | None”Read-only access to the reference BN.
property refName : str
Section titled “property refName : str”Read-only access to the reference BN name.
remove(name)
Section titled “remove(name)”Removes a BN from the model.
- Parameters:
name (
str) – Name of the BN to remove. - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Return type:
None
setWeight(name, w)
Section titled “setWeight(name, w)”Changes the weight of a BN in the model.
- Parameters:
- name (
str) – Name of the BN to modify. - w (
float) – Value of the new weight.
- name (
- Raises:
- pyagrum.InvalidArgument – If the weight is negative.
- pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Return type:
None
size()
Section titled “size()”- Returns: The number of BNs in the model (reference BN not included).
- Return type:
int
updateRef()
Section titled “updateRef()”Updates the reference BN. The new reference BN is the one with maximum weight.
- Return type:
None
variable(name)
Section titled “variable(name)”- Parameters:
name (
str) – Name of the variable. - Returns: The corresponding variable.
- Return type:
DiscreteVariable
weight(name)
Section titled “weight(name)”- Parameters:
name (
str) – Name of the BN. - Returns:
The weight of the BN with name
name. - Return type:
float - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
weights()
Section titled “weights()”- Returns: The weights of all the BNs in the model.
- Return type:
dict[str,float]
zeroBNs()
Section titled “zeroBNs()”- Returns: The names of the BNs in the model that have weight with value 0.
- Return type:
set[str]
class pyagrum.bnmixture.BootstrapMixture(name, bn)
Section titled “class pyagrum.bnmixture.BootstrapMixture(name, bn)”A mixture of Bayesian networks built by Bayesian bootstrapping a single database.
The reference BN (refBN) is learned from the original (uniformly
weighted) database. It is the primary estimate: its posteriors are the ones
returned by BootstrapMixtureInference.
The other BNs are learned from Bayesian bootstrap resamples of the same database (Dirichlet-drawn record weights simulate resampling with replacement). They are used exclusively to quantify the stability of the reference estimate: arc-confidence scores and quantile intervals on posteriors.
- Parameters:
- name (
str) – Name given to the reference BN. Acts as a guard: no BN added later may carry this same name. - bn (
BayesNet) – The reference BN. Every BN added later must share the same variables.
- name (
Notes
This is an experimental model. Use BNMBootstrapLearner
to build one automatically from a database.
BN(name)
Section titled “BN(name)”- Parameters:
name (
str) – Name of the variable. - Returns:
A copy of the BN with name
namein the model. - Return type:
BayesNet - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Returns: A list containing a copy of all BNs in the model.
- Return type:
list[BayesNet]
add(name, bn, w=1)
Section titled “add(name, bn, w=1)”Adds a BN to the model. If the model doesn’t have a reference BN when trying to add an element, the BN (before adding new element) with highest weight becomes the new reference.
- Parameters:
- name (
str) – Name of the BN to add. - bn (
BayesNet) – BN to add. - w (
float) – Weight of the BN.
- name (
- Raises:
- pyagrum.InvalidArgument – If the weight is negative.
- pyagrum.InvalidArgument – If the names of the variables in the BN to add are differents from the one in the reference BN.
- pyagrum.InvalidArgument – If the variables in the BN to add are differents from the one in the reference BN.
- pyagrum.InvalidArgument – If the name in argument is the same as the reference BN’s name.
- pyagrum.ArgumentError – If the name in argument already exists for a different BN in the model.
- Return type:
None
existsArc(a, b)
Section titled “existsArc(a, b)”Counts the number of time arc a -> b appears among all BNs in the model.
- Parameters:
- a (
str|int) – Tail of the arc. - b (
str|int) – Head of the arc.
- a (
- Returns:
The number of time arc
a->bappears. - Return type:
int
isNormalized()
Section titled “isNormalized()”Checks if the model is normalized (the sum of the weights equals 1).
- Return type:
bool
isValid()
Section titled “isValid()”Checks if all the weights are equal to 0. Valid if sum of the weights is not 0.
- Returns: True if weights are valid. False otherwise.
- Return type:
bool
names()
Section titled “names()”- Returns: The list of names of the BNs in the model (reference BN not included).
- Return type:
list[str]
normalize()
Section titled “normalize()”Normalizes the weights.
- Raises: pyagrum.InvalidArgument – If all weights are zero (cannot normalize).
- Return type:
None
property refBN : BayesNet | None
Section titled “property refBN : BayesNet | None”Read-only access to the reference BN.
property refName : str
Section titled “property refName : str”Read-only access to the reference BN name.
remove(name)
Section titled “remove(name)”Removes a BN from the model.
- Parameters:
name (
str) – Name of the BN to remove. - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Return type:
None
setWeight(name, w)
Section titled “setWeight(name, w)”Changes the weight of a BN in the model.
- Parameters:
- name (
str) – Name of the BN to modify. - w (
float) – Value of the new weight.
- name (
- Raises:
- pyagrum.InvalidArgument – If the weight is negative.
- pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
- Return type:
None
size()
Section titled “size()”- Returns: The number of BNs in the model (reference BN not included).
- Return type:
int
variable(name)
Section titled “variable(name)”- Parameters:
name (
str) – Name of the variable. - Returns: The corresponding variable.
- Return type:
DiscreteVariable
weight(name)
Section titled “weight(name)”- Parameters:
name (
str) – Name of the BN. - Returns:
The weight of the BN with name
name. - Return type:
float - Raises: pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.
weights()
Section titled “weights()”- Returns: The weights of all the BNs in the model.
- Return type:
dict[str,float]
zeroBNs()
Section titled “zeroBNs()”- Returns: The names of the BNs in the model that have weight with value 0.
- Return type:
set[str]