Mixture Model

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)
Parameters:

name (str) – Name of the variable.

Returns:

A copy of the BN with name name in the model.

Return type:

pyagrum.BayesNet

Raises:

pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.

BNs()
Returns:

A list containing a copy of all BNs in the model.

Return type:

list[pyagrum.BayesNet]

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 (pyagrum.BayesNet) – BN to add.

  • w (float) – Weight of the BN.

Raises:
Return type:

None

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.

Returns:

The number of time arc a -> b appears.

Return type:

int

isNormalized()

Checks if the model is normalized (the sum of the weights equals 1).

Return type:

bool

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()
Returns:

The list of names of the BNs in the model (reference BN not included).

Return type:

list[str]

normalize()

Normalizes the weights.

Raises:

pyagrum.InvalidArgument – If all weights are zero (cannot normalize).

Return type:

None

property refBN: BayesNet | None

Read-only access to the reference BN.

property refName: str

Read-only access to the reference BN name.

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)

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.

Raises:
Return type:

None

size()
Returns:

The number of BNs in the model (reference BN not included).

Return type:

int

updateRef()

Updates the reference BN. The new reference BN is the one with maximum weight.

Return type:

None

variable(name)
Parameters:

name (str) – Name of the variable.

Returns:

The corresponding variable.

Return type:

pyagrum.DiscreteVariable

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()
Returns:

The weights of all the BNs in the model.

Return type:

dict[str, float]

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)

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 (pyagrum.BayesNet) – The reference BN. Every BN added later must share the same variables.

Notes

This is an experimental model. Use BNMBootstrapLearner to build one automatically from a database.

BN(name)
Parameters:

name (str) – Name of the variable.

Returns:

A copy of the BN with name name in the model.

Return type:

pyagrum.BayesNet

Raises:

pyagrum.NotFound – If the given name doesn’t correspond to the name of a BN in the model.

BNs()
Returns:

A list containing a copy of all BNs in the model.

Return type:

list[pyagrum.BayesNet]

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 (pyagrum.BayesNet) – BN to add.

  • w (float) – Weight of the BN.

Raises:
Return type:

None

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.

Returns:

The number of time arc a -> b appears.

Return type:

int

isNormalized()

Checks if the model is normalized (the sum of the weights equals 1).

Return type:

bool

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()
Returns:

The list of names of the BNs in the model (reference BN not included).

Return type:

list[str]

normalize()

Normalizes the weights.

Raises:

pyagrum.InvalidArgument – If all weights are zero (cannot normalize).

Return type:

None

property refBN: BayesNet | None

Read-only access to the reference BN.

property refName: str

Read-only access to the reference BN name.

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)

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.

Raises:
Return type:

None

size()
Returns:

The number of BNs in the model (reference BN not included).

Return type:

int

variable(name)
Parameters:

name (str) – Name of the variable.

Returns:

The corresponding variable.

Return type:

pyagrum.DiscreteVariable

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()
Returns:

The weights of all the BNs in the model.

Return type:

dict[str, float]

zeroBNs()
Returns:

The names of the BNs in the model that have weight with value 0.

Return type:

Set[str]