Learning Operators

This section includes learning operators that are used to make small, local changes to a given Bayesian network structure. This is used for the score-and-search learning algorithms such as GreedyHillClimbing, MMHC and DMMHC.

There are two type of classes in this section: operators and operator sets:

  • The operators are the representation of a change in a Bayesian network structure.

  • The operator sets coordinate sets of operators. They can find the best operator over the set and update the score and availability of each operator in the set.

Operators

class pybnesian.Operator

An operator is the representation of a change in a Bayesian network structure. Each operator has a delta score associated that measures the difference in score when the operator is applied to the Bayesian network.

__eq__(self: pybnesian.Operator, other: pybnesian.Operator) bool
__hash__(self: pybnesian.Operator) int

Returns the hash value of this operator. Two equal operators (without taking into account the delta value) must return the same hash value.

Returns

Hash value of self operator.

__init__(self: pybnesian.Operator, delta: float) None

Initializes an Operator with a given delta.

Parameters

delta – Delta score of the operator.

__str__(self: pybnesian.Operator) str
apply(self: pybnesian.Operator, model: pybnesian.BayesianNetworkBase) None

Apply the operator to the model.

Parameters

model – Bayesian network model.

delta(self: pybnesian.Operator) float

Gets the delta score of the operator.

Returns

Delta score of the operator.

nodes_changed(self: pybnesian.Operator, model: BayesianNetworkBase or ConditionalBayesianNetworkBase) List[str]

Gets the list of nodes whose local score changes when the operator is applied.

Parameters

model – Bayesian network model.

Returns

List of nodes whose local score changes when the operator is applied.

opposite(self: pybnesian.Operator, model: BayesianNetworkBase or ConditionalBayesianNetworkBase) Operator

Returns an operator that reverses this Operator given the model. For example:

>>> from pybnesian import AddArc, RemoveArc, GaussianNetwork
>>> gbn = GaussianNetwork(["a", "b"])
>>> add = AddArc("a", "b", 1)
>>> assert add.opposite(gbn) == RemoveArc("a", "b", -1)
Parameters

model – The model where the self operator would be applied.

Returns

The opposite operator of self.

class pybnesian.ArcOperator

Bases: Operator

This class implements an operator that perfoms a change in a single arc.

__init__(self: pybnesian.ArcOperator, source: str, target: str, delta: float) None

Initializes an ArcOperator of the arc source -> target with delta score delta.

Parameters
  • source – Name of the source node.

  • target – Name of the target node.

  • delta – Delta score of the operator.

source(self: pybnesian.ArcOperator) str

Gets the source of the ArcOperator.

Returns

Name of the source node.

target(self: pybnesian.ArcOperator) str

Gets the target of the ArcOperator.

Returns

Name of the target node.

class pybnesian.AddArc

Bases: ArcOperator

This operator adds the arc source -> target.

__init__(self: pybnesian.AddArc, source: str, target: str, delta: float) None

Initializes the AddArc operator of the arc source -> target with delta score delta.

Parameters
  • source – Name of the source node.

  • target – Name of the target node.

  • delta – Delta score of the operator.

class pybnesian.RemoveArc

Bases: ArcOperator

This operator removes the arc source -> target.

__init__(self: pybnesian.RemoveArc, source: str, target: str, delta: float) None

Initializes the RemoveArc operator of the arc source -> target with delta score delta.

Parameters
  • source – Name of the source node.

  • target – Name of the target node.

  • delta – Delta score of the operator.

class pybnesian.FlipArc

Bases: ArcOperator

This operator flips (reverses) the arc source -> target.

__init__(self: pybnesian.FlipArc, source: str, target: str, delta: float) None

Initializes the FlipArc operator of the arc source -> target with delta score delta.

Parameters
  • source – Name of the source node.

  • target – Name of the target node.

  • delta – Delta score of the operator.

class pybnesian.ChangeNodeType

Bases: Operator

This operator changes the FactorType of a node.

__init__(self: pybnesian.ChangeNodeType, node: str, node_type: pybnesian.FactorType, delta: float) None

Initializes the ChangeNodeType operator to change the type of the node to a new node_type.

Parameters
  • node – Name of the source node.

  • node_type – The new FactorType of the node.

  • delta – Delta score of the operator.

node(self: pybnesian.ChangeNodeType) str

Gets the node of the ChangeNodeType.

Returns

Node of the operator.

node_type(self: pybnesian.ChangeNodeType) pybnesian.FactorType

Gets the new FactorType of the ChangeNodeType.

Returns

New FactorType of the node.

Operator Sets

class pybnesian.OperatorSet

The OperatorSet coordinates a set of operators. It caches/updates the score of each operator in the set and finds the operator with the best score.

__init__(self: pybnesian.OperatorSet, calculate_local_cache: bool = True) None

Initializes an OperatorSet.

If calculate_local_cache is True, a LocalScoreCache is automatically initialized when OperatorSet.cache_scores() is called. Also, the local score cache is automatically updated on each OperatorSet.update_scores() call. Therefore, the local score cache is always updated. You can always get the local score cache using OperatorSet.local_score_cache(). The local score values can be accessed using LocalScoreCache.local_score().

If calculate_local_cache is False, there is no local cache.

Parameters

calculate_local_cache – If True automatically initializes and updates a LocalScoreCache.

cache_scores(self: pybnesian.OperatorSet, model: pybnesian.BayesianNetworkBase, score: pybnesian.Score) None

Caches the delta score values of each operator in the set.

Parameters
  • model – Bayesian network model.

  • score – The Score object to cache the scores.

find_max(self: pybnesian.OperatorSet, model: pybnesian.BayesianNetworkBase) pybnesian.Operator

Finds the best operator in the set to apply to the model. This function must not return an invalid operator:

  • An operator that creates cycles.

  • An operator that contradicts blacklists, whitelists or max indegree.

If no valid operator is available in the set, it returns None.

Parameters

model – Bayesian network model.

Returns

The best valid operator, or None if there is no valid operator.

find_max_tabu(self: pybnesian.OperatorSet, model: pybnesian.BayesianNetworkBase, tabu_set: pybnesian.OperatorTabuSet) pybnesian.Operator

This method is similar to OperatorSet.find_max(), but it also receives a tabu_set of operators.

This method must not return an operator in the tabu_set in addition to the restrictions of OperatorSet.find_max().

Parameters
  • model – Bayesian network model.

  • tabu_set – Tabu set of operators.

Returns

The best valid operator, or None if there is no valid operator.

finished(self: pybnesian.OperatorSet) None

Marks the finalization of the algorithm. It clears the state of the object, so OperatorSet.cache_scores() can be called again.

local_score_cache(self: pybnesian.OperatorSet) pybnesian.LocalScoreCache

Returns the current LocalScoreCache of this OperatorSet.

Returns

LocalScoreCache of this operator set.

set_arc_blacklist(self: pybnesian.OperatorSet, arc_blacklist: List[Tuple[str, str]]) None

Sets the arc blacklist (a list of arcs that cannot be added).

Parameters

arc_blacklist – The list of blacklisted arcs.

set_arc_whitelist(self: pybnesian.OperatorSet, arc_whitelist: List[Tuple[str, str]]) None

Sets the arc whitelist (a list of arcs that are forced).

Parameters

arc_whitelist – The list of whitelisted arcs.

set_max_indegree(self: pybnesian.OperatorSet, max_indegree: int) None

Sets the max indegree allowed. This may change the set of valid operators.

Parameters

max_indegree – Max indegree allowed.

set_type_blacklist(self: pybnesian.OperatorSet, type_blacklist: List[Tuple[str, pybnesian.FactorType]]) None

Sets the type blacklist (a list of FactorType that are not allowed).

Parameters

type_blacklist – The list of blacklisted FactorType.

set_type_whitelist(self: pybnesian.OperatorSet, type_whitelist: List[Tuple[str, pybnesian.FactorType]]) None

Sets the type whitelist (a list of FactorType that are forced).

Parameters

type_whitelist – The list of whitelisted FactorType.

update_scores(self: pybnesian.OperatorSet, model: pybnesian.BayesianNetworkBase, score: pybnesian.Score, changed_nodes: List[str]) None

Updates the delta score values of the operators in the set after applying an operator in the model. changed_nodes determines the nodes whose local score has changed after applying the operator.

Parameters
  • model – Bayesian network model.

  • score – The Score object to cache the scores.

  • changed_nodes – The nodes whose local score has changed.

class pybnesian.ArcOperatorSet

Bases: OperatorSet

This set of operators contains all the operators related with arc changes (AddArc, RemoveArc, FlipArc)

__init__(self: pybnesian.ArcOperatorSet, blacklist: List[Tuple[str, str]] = [], whitelist: List[Tuple[str, str]] = [], max_indegree: int = 0) None

Initializes an ArcOperatorSet with optional sets of arc blacklists/whitelists and maximum indegree.

Parameters
  • blacklist – List of blacklisted arcs.

  • whitelist – List of whitelisted arcs.

  • max_indegree – Max indegree allowed.

class pybnesian.ChangeNodeTypeSet

Bases: OperatorSet

This set of operators contains all the possible operators of type ChangeNodeType.

__init__(self: pybnesian.ChangeNodeTypeSet, type_blacklist: List[Tuple[str, pybnesian.FactorType]] = [], type_whitelist: List[Tuple[str, pybnesian.FactorType]] = []) None

Initializes a ChangeNodeTypeSet with blacklisted and whitelisted FactorType.

Parameters
  • type_blacklist – The list of blacklisted FactorType.

  • type_whitelist – The list of whitelisted FactorType.

class pybnesian.OperatorPool

Bases: OperatorSet

This set of operators can join a list of OperatorSet, so that they can act as a single OperatorSet.

__init__(self: pybnesian.OperatorPool, opsets: List[pybnesian.OperatorSet]) None

Initializes an OperatorPool with a list of OperatorSet.

Parameters

opsets – List of OperatorSet.

Other

class pybnesian.OperatorTabuSet

An OperatorTabuSet that contains forbidden operators.

__init__(self: pybnesian.OperatorTabuSet) None

Creates an empty OperatorTabuSet.

clear(self: pybnesian.OperatorTabuSet) None

Erases all the operators from the set.

contains(self: pybnesian.OperatorTabuSet, operator: pybnesian.Operator) bool

Checks whether this tabu set contains operator.

Parameters

operator – The operator to be checked.

Returns

True if the tabu set contains the operator, False otherwise.

empty(self: pybnesian.OperatorTabuSet) bool

Checks if the set has no operators

Returns

True if the set is empty, False otherwise.

insert(self: pybnesian.OperatorTabuSet, operator: pybnesian.Operator) None

Inserts an operator into the tabu set.

Parameters

operator – Operator to insert.

class pybnesian.LocalScoreCache

This class implements a cache for the local score of each node.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pybnesian.LocalScoreCache) -> None

Initializes an empty LocalScoreCache.

  1. __init__(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase) -> None

Initializes a LocalScoreCache for the given model.

Parameters

model – A Bayesian network model.

cache_local_scores(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, score: pybnesian.Score) None

Caches the local score for all the nodes.

Parameters
  • model – A Bayesian network model.

  • score – A Score object to calculate the score.

cache_vlocal_scores(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, score: pybnesian.ValidatedScore) None

Caches the validation local score for all the nodes.

Parameters
  • model – A Bayesian network model.

  • score – A ValidatedScore object to calculate the score.

local_score(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, node: str) float

Returns the local score of the node in the model.

Parameters
  • model – A Bayesian network model.

  • node – A node name.

Returns

Local score of node in model.

sum(self: pybnesian.LocalScoreCache) float

Sums the local score for all the variables.

Returns

Total score.

update_local_score(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, score: pybnesian.Score, node: str) None

Updates the local score of the node in the model.

Parameters
  • model – A Bayesian network model.

  • score – A Score object to calculate the score.

  • node – A node name.

update_vlocal_score(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, score: pybnesian.ValidatedScore, node: str) None

Updates the validation local score of the node in the model.

Parameters
  • model – A Bayesian network model.

  • score – A ValidatedScore object to calculate the score.

  • node – A node name.