Mixture Model

class pyagrum.bnmixture.BNMixture

Experimental model. The class contains a list of BNs. Each BN has its own weight.

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

static loadBIF(filename)

Retrieve a BNMixture from a file.

Parameters:

filename (str) – Zip file containing the mixture.

Returns:

The stored BNMixture

Return type:

BNMixture

names()
Returns:

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

Return type:

List[str]

normalize()

Normalizes the weights.

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.

saveBIF(fname)

Saves a Mixture in BIF format and zip it.

Parameters:

fname (str) – Name of the file (without extenstion).

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:
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.

variable(name)
Parameters:

name (str) – Name of the variable.

Returns:

The corresponding variable.

Return type:

pyagrum.LabelizedVariable

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)

Experimental model base on bootstraping. The class contains reference BN and a list of BNs. Each BN has its own weight except for the reference BN. The reference BN is used so that every other BN added later contains the same variables as the reference BN.

Parameters:
  • name (str) – Name of the first BN to add to the model. It is used for reference.

  • bn (pyagrum.BayesNet) – BN to add. Adding new BNs to the model is allowed only if the variables are the same in the first and new BN.

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

static loadBIF(filename)

Retrieve a BootstrapMixture from a file.

Parameters:

filename (str) – Zip file containing the mixture.

Returns:

The stored BootstrapMixture.

Return type:

BootstrapMixture

names()
Returns:

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

Return type:

List[str]

normalize()

Normalizes the weights.

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.

saveBIF(fname)

Saves a Mixture in BIF format and zip it.

Parameters:

fname (str) – Name of the file (without extenstion).

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:
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.LabelizedVariable

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]