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
selfoperator.
- __init__(self: pybnesian.Operator, delta: float) None
Initializes an
Operatorwith a givendelta.- 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
Operatorgiven themodel. 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
selfoperator would be applied.- Returns:
The opposite operator of
self.
- class pybnesian.ArcOperator
Bases:
OperatorThis 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
ArcOperatorof the arcsource->targetwith delta scoredelta.- 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:
ArcOperatorThis operator adds the arc
source->target.
- class pybnesian.RemoveArc
Bases:
ArcOperatorThis operator removes the arc
source->target.
- class pybnesian.FlipArc
Bases:
ArcOperatorThis operator flips (reverses) the arc
source->target.
- class pybnesian.ChangeNodeType
Bases:
OperatorThis operator changes the
FactorTypeof a node.- __init__(self: pybnesian.ChangeNodeType, node: str, node_type: pybnesian.FactorType, delta: float) None
Initializes the
ChangeNodeTypeoperator to change the type of thenodeto a newnode_type.- Parameters:
node – Name of the source node.
node_type – The new
FactorTypeof thenode.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
FactorTypeof theChangeNodeType.- Returns:
New
FactorTypeof the node.
Operator Sets
- class pybnesian.OperatorSet
The
OperatorSetcoordinates 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_cacheis True, aLocalScoreCacheis automatically initialized whenOperatorSet.cache_scores()is called. Also, the local score cache is automatically updated on eachOperatorSet.update_scores()call. Therefore, the local score cache is always updated. You can always get the local score cache usingOperatorSet.local_score_cache(). The local score values can be accessed usingLocalScoreCache.local_score().If
calculate_local_cacheis 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
Scoreobject 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
Noneif 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 atabu_setof operators.This method must not return an operator in the
tabu_setin addition to the restrictions ofOperatorSet.find_max().- Parameters:
model – Bayesian network model.
tabu_set – Tabu set of operators.
- Returns:
The best valid operator, or
Noneif 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
LocalScoreCacheof thisOperatorSet.- Returns:
LocalScoreCacheof 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
FactorTypethat 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
FactorTypethat 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_nodesdetermines the nodes whose local score has changed after applying the operator.- Parameters:
model – Bayesian network model.
score – The
Scoreobject to cache the scores.changed_nodes – The nodes whose local score has changed.
- class pybnesian.ArcOperatorSet
Bases:
OperatorSetThis 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
ArcOperatorSetwith 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:
OperatorSetThis 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
ChangeNodeTypeSetwith blacklisted and whitelistedFactorType.- Parameters:
type_blacklist – The list of blacklisted
FactorType.type_whitelist – The list of whitelisted
FactorType.
- class pybnesian.OperatorPool
Bases:
OperatorSetThis set of operators can join a list of
OperatorSet, so that they can act as a singleOperatorSet.- __init__(self: pybnesian.OperatorPool, opsets: list[pybnesian.OperatorSet]) None
Initializes an
OperatorPoolwith a list ofOperatorSet.- Parameters:
opsets – List of
OperatorSet.
Other
- class pybnesian.OperatorTabuSet
An
OperatorTabuSetthat 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.
__init__(self: pybnesian.LocalScoreCache) -> None
Initializes an empty
LocalScoreCache.__init__(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase) -> None
Initializes a
LocalScoreCachefor the givenmodel.- 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
Scoreobject 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
ValidatedScoreobject to calculate the score.
- local_score(self: pybnesian.LocalScoreCache, model: pybnesian.BayesianNetworkBase, node: str) float
Returns the local score of the
nodein themodel.- Parameters:
model – A Bayesian network model.
node – A node name.
- Returns:
Local score of
nodeinmodel.
- 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
nodein themodel.- Parameters:
model – A Bayesian network model.
score – A
Scoreobject 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
nodein themodel.- Parameters:
model – A Bayesian network model.
score – A
ValidatedScoreobject to calculate the score.node – A node name.