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.learning.operators.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.learning.operators.Operator, other: pybnesian.learning.operators.Operator) → bool¶
- __hash__(self: pybnesian.learning.operators.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.learning.operators.Operator, delta: float) → None¶
Initializes an
Operator
with a givendelta
.- Parameters
delta – Delta score of the operator.
- __str__(self: pybnesian.learning.operators.Operator) → str¶
- apply(self: pybnesian.learning.operators.Operator, model: pybnesian.models.BayesianNetworkBase) → None¶
Apply the operator to the
model
.- Parameters
model – Bayesian network model.
- delta(self: pybnesian.learning.operators.Operator) → float¶
Gets the delta score of the operator.
- Returns
Delta score of the operator.
- nodes_changed(self: pybnesian.learning.operators.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.learning.operators.Operator, model: BayesianNetworkBase or ConditionalBayesianNetworkBase) → Operator¶
Returns an operator that reverses this
Operator
given themodel
. For example:>>> from pybnesian.learning.operators import AddArc, RemoveArc >>> from pybnesian.models import 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.learning.operators.ArcOperator¶
Bases:
pybnesian.learning.operators.Operator
This class implements an operator that perfoms a change in a single arc.
- __init__(self: pybnesian.learning.operators.ArcOperator, source: str, target: str, delta: float) → None¶
Initializes an
ArcOperator
of the arcsource
->target
with delta scoredelta
.- Parameters
source – Name of the source node.
target – Name of the target node.
delta – Delta score of the operator.
- source(self: pybnesian.learning.operators.ArcOperator) → str¶
Gets the source of the
ArcOperator
.- Returns
Name of the source node.
- target(self: pybnesian.learning.operators.ArcOperator) → str¶
Gets the target of the
ArcOperator
.- Returns
Name of the target node.
- class pybnesian.learning.operators.AddArc¶
Bases:
pybnesian.learning.operators.ArcOperator
This operator adds the arc
source
->target
.
- class pybnesian.learning.operators.RemoveArc¶
Bases:
pybnesian.learning.operators.ArcOperator
This operator removes the arc
source
->target
.- __init__(self: pybnesian.learning.operators.RemoveArc, source: str, target: str, delta: float) → None¶
Initializes the
RemoveArc
operator of the arcsource
->target
with delta scoredelta
.- Parameters
source – Name of the source node.
target – Name of the target node.
delta – Delta score of the operator.
- class pybnesian.learning.operators.FlipArc¶
Bases:
pybnesian.learning.operators.ArcOperator
This operator flips (reverses) the arc
source
->target
.- __init__(self: pybnesian.learning.operators.FlipArc, source: str, target: str, delta: float) → None¶
Initializes the
FlipArc
operator of the arcsource
->target
with delta scoredelta
.- Parameters
source – Name of the source node.
target – Name of the target node.
delta – Delta score of the operator.
- class pybnesian.learning.operators.ChangeNodeType¶
Bases:
pybnesian.learning.operators.Operator
This operator changes the
FactorType
of a node.- __init__(self: pybnesian.learning.operators.ChangeNodeType, node: str, node_type: pybnesian.factors.FactorType, delta: float) → None¶
Initializes the
ChangeNodeType
operator to change the type of thenode
to a newnode_type
.- Parameters
node – Name of the source node.
node_type – The new
FactorType
of thenode
.delta – Delta score of the operator.
- node(self: pybnesian.learning.operators.ChangeNodeType) → str¶
Gets the node of the
ChangeNodeType
.- Returns
Node of the operator.
- node_type(self: pybnesian.learning.operators.ChangeNodeType) → pybnesian.factors.FactorType¶
Gets the new
FactorType
of theChangeNodeType
.- Returns
New
FactorType
of the node.
Operator Sets¶
- class pybnesian.learning.operators.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.learning.operators.OperatorSet, calculate_local_cache: bool = True) → None¶
Initializes an
OperatorSet
.If
calculate_local_cache
is True, aLocalScoreCache
is 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_cache
is False, there is no local cache.- Parameters
calculate_local_cache – If True automatically initializes and updates a
LocalScoreCache
.
- cache_scores(self: pybnesian.learning.operators.OperatorSet, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.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.learning.operators.OperatorSet, model: pybnesian.models.BayesianNetworkBase) → pybnesian.learning.operators.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.learning.operators.OperatorSet, model: pybnesian.models.BayesianNetworkBase, tabu_set: pybnesian.learning.operators.OperatorTabuSet) → pybnesian.learning.operators.Operator¶
This method is similar to
OperatorSet.find_max()
, but it also receives atabu_set
of operators.This method must not return an operator in the
tabu_set
in addition to the restrictions ofOperatorSet.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.learning.operators.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.learning.operators.OperatorSet) → pybnesian.learning.operators.LocalScoreCache¶
Returns the current
LocalScoreCache
of thisOperatorSet
.- Returns
LocalScoreCache
of this operator set.
- set_arc_blacklist(self: pybnesian.learning.operators.OperatorSet, arc_blacklist: List[Tuple[str, str]]) → None¶
Sets the arc blacklist (a list of arcs that can not be added).
- Parameters
arc_blacklist – The list of blacklisted arcs.
- set_arc_whitelist(self: pybnesian.learning.operators.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.learning.operators.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.learning.operators.OperatorSet, type_blacklist: List[Tuple[str, pybnesian.factors.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.learning.operators.OperatorSet, type_whitelist: List[Tuple[str, pybnesian.factors.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.learning.operators.OperatorSet, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.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.learning.operators.ArcOperatorSet¶
Bases:
pybnesian.learning.operators.OperatorSet
This set of operators contains all the operators related with arc changes (
AddArc
,RemoveArc
,FlipArc
)- __init__(self: pybnesian.learning.operators.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.learning.operators.ChangeNodeTypeSet¶
Bases:
pybnesian.learning.operators.OperatorSet
This set of operators contains all the possible operators of type
ChangeNodeType
.- __init__(self: pybnesian.learning.operators.ChangeNodeTypeSet, type_blacklist: List[Tuple[str, pybnesian.factors.FactorType]] = [], type_whitelist: List[Tuple[str, pybnesian.factors.FactorType]] = []) → None¶
Initializes a
ChangeNodeTypeSet
with blacklisted and whitelistedFactorType
.- Parameters
type_blacklist – The list of blacklisted
FactorType
.type_whitelist – The list of whitelisted
FactorType
.
- class pybnesian.learning.operators.OperatorPool¶
Bases:
pybnesian.learning.operators.OperatorSet
This set of operators can join a list of
OperatorSet
, so that they can act as a singleOperatorSet
.- __init__(self: pybnesian.learning.operators.OperatorPool, opsets: List[pybnesian.learning.operators.OperatorSet]) → None¶
Initializes an
OperatorPool
with a list ofOperatorSet
.- Parameters
opsets – List of
OperatorSet
.
Other¶
- class pybnesian.learning.operators.OperatorTabuSet¶
An
OperatorTabuSet
that contains forbidden operators.- __init__(self: pybnesian.learning.operators.OperatorTabuSet) → None¶
Creates an empty
OperatorTabuSet
.
- clear(self: pybnesian.learning.operators.OperatorTabuSet) → None¶
Erases all the operators from the set.
- contains(self: pybnesian.learning.operators.OperatorTabuSet, operator: pybnesian.learning.operators.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.learning.operators.OperatorTabuSet) → bool¶
Checks if the set has no operators
- Returns
True if the set is empty, False otherwise.
- insert(self: pybnesian.learning.operators.OperatorTabuSet, operator: pybnesian.learning.operators.Operator) → None¶
Inserts an operator into the tabu set.
- Parameters
operator – Operator to insert.
- class pybnesian.learning.operators.LocalScoreCache¶
This class implements a cache for the local score of each node.
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: pybnesian.learning.operators.LocalScoreCache) -> None
Initializes an empty
LocalScoreCache
.__init__(self: pybnesian.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase) -> None
Initializes a
LocalScoreCache
for the givenmodel
.- Parameters
model – A Bayesian network model.
- cache_local_scores(self: pybnesian.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.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.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.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.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase, node: str) → float¶
Returns the local score of the
node
in themodel
.- Parameters
model – A Bayesian network model.
node – A node name.
- Returns
Local score of
node
inmodel
.
- sum(self: pybnesian.learning.operators.LocalScoreCache) → float¶
Sums the local score for all the variables.
- Returns
Total score.
- update_local_score(self: pybnesian.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.Score, node: str) → None¶
Updates the local score of the
node
in themodel
.- Parameters
model – A Bayesian network model.
score – A
Score
object to calculate the score.node – A node name.
- update_vlocal_score(self: pybnesian.learning.operators.LocalScoreCache, model: pybnesian.models.BayesianNetworkBase, score: pybnesian.learning.scores.ValidatedScore, node: str) → None¶
Updates the validation local score of the
node
in themodel
.- Parameters
model – A Bayesian network model.
score – A
ValidatedScore
object to calculate the score.node – A node name.