alex.ml.bn package¶
Submodules¶
alex.ml.bn.autopath module¶
self cloning, automatic path configuration
copy this into any subdirectory of pypy from which scripts need to be run, typically all of the test subdirs. The idea is that any such script simply issues
import autopath
and this will make sure that the parent directory containing “pypy” is in sys.path.
If you modify the master “autopath.py” version (in pypy/tool/autopath.py) you can directly run it which will copy itself on all autopath.py files it finds under the pypy root directory.
This module always provides these attributes:
pypydir pypy root directory path this_dir directory where this autopath.py resides
alex.ml.bn.factor module¶
This module implements a factor class, which can be used to do computations with probability distributions.
-
class
alex.ml.bn.factor.
Factor
(variables, variable_values, prob_table, logarithmetic=True)[source]¶ Bases:
object
Basic factor.
-
marginalize
(keep)[source]¶ Marginalize all but specified variables.
Marginalizing means summing out values which are not in keep. The result is a new factor, which contains only variables from keep.
Example:
>>> f = Factor(['A', 'B'], ... {'A': ['a1', 'a2'], 'B': ['b1', 'b2']}, ... { ... ('a1', 'b1'): 0.8, ... ('a2', 'b1'): 0.2, ... ('a1', 'b2'): 0.3, ... ('a2', 'b2'): 0.7 ... }) >>> result = f.marginalize(['A']) >>> print result.pretty_print(width=30) ------------------------------ A Value ------------------------------ a1 1.1 a2 0.9 ------------------------------
Parameters: keep (list of str) – Variables which should be left in marginalized factor. Returns: Marginalized factor. Return type: Factor
-
most_probable
(n=None)[source]¶ Return a list of most probable assignments from the table.
Returns a sorted list of assignment and their values according to their probability. The size of the list can be changed by specifying n.
Parameters: n (int) – The number of most probable elements, which should be returned. Returns: A list of tuples (assignment, value) in descending order. Return type: list of (tuple, float)
-
normalize
(parents=None)[source]¶ Normalize a factor table.
The table is normalized so all elements sum to one. The parents argument is a list of names of parents. If it is specified, then only those rows in table, which share the same parents, are normalized.
Example:
>>> f = Factor(['A', 'B'], ... {'A': ['a1', 'a2'], 'B': ['b1', 'b2']}, ... { ... ('a1', 'b1'): 3, ... ('a1', 'b2'): 1, ... ('a2', 'b1'): 1, ... ('a2', 'b2'): 1, ... }) >>> f.normalize(parents=['B']) >>> print f.pretty_print(width=30) ------------------------------ A B Value ------------------------------ a1 b1 0.75 a1 b2 0.5 a2 b1 0.25 a2 b2 0.5 ------------------------------
Parameters: parents (list) – Parents of the factor.
-
observed
(assignment_dict)[source]¶ Set observation.
Example:
>>> f = Factor( ... ['X'], ... { ... 'X': ['x0', 'x1'], ... }, ... { ... ('x0',): 0.5, ... ('x1',): 0.5, ... }) >>> print f.pretty_print(width=30, precision=3) ------------------------------ X Value ------------------------------ x0 0.5 x1 0.5 ------------------------------ >>> f.observed({('x0',): 0.8, ('x1',): 0.2}) >>> print f.pretty_print(width=30, precision=3) ------------------------------ X Value ------------------------------ x0 0.8 x1 0.2 ------------------------------
Parameters: assignment_dict (dict or None) – Observed values for different assignments of values or None.
-
pretty_print
(width=79, precision=10)[source]¶ Create a readable representation of the factor.
Creates a table with a column for each variable and value. Every row represents one assignemnt and its corresponding value. The default width of the table is 79 chars, to fit to terminal window.
Parameters: - width (int) – Width of the table.
- precision (int) – Precision of values.
Returns: Pretty printed factor table.
Return type: str
-
-
alex.ml.bn.factor.
from_log
(n)[source]¶ Convert number from log arithmetic.
Parameters: n (number or array like) – Number to be converted from log arithmetic. Returns: Number in decimal scale. Return type: number or array like
-
alex.ml.bn.factor.
to_log
(n, out=None)[source]¶ Convert number to log arithmetic.
We want to be able to represent zero, therefore every number smaller than epsilon is considered a zero.
Parameters: - n (number or array like) – Number to be converted.
- out (ndarray) – Output array.
Returns: Number in log arithmetic.
Return type: number or array like
alex.ml.bn.lbp module¶
Belief propagation algorithms for factor graph.
-
class
alex.ml.bn.lbp.
LBP
(strategy='sequential', **kwargs)[source]¶ Bases:
alex.ml.bn.lbp.BP
Loopy Belief Propagation.
LBP is an approximative inference algorithm for factor graphs. LBP works with generic factor graphs. It does accurate inference for trees and is equal to sum-product algorithm there.
It is possible to specify which strategy should be used for choosing next node for update. Sequential strategy will update nodes in exact order in which they were added. Tree strategy will assume the graph is a tree (without checking) and will do one pass of sum-product algorithm.
-
exception
alex.ml.bn.lbp.
LBPError
[source]¶ Bases:
alex.ml.bn.lbp.BPError
alex.ml.bn.node module¶
Node representations for factor graph.
-
class
alex.ml.bn.node.
DirichletFactorNode
(name, aliases=None)[source]¶ Bases:
alex.ml.bn.node.FactorNode
Node containing dirichlet factor.
-
class
alex.ml.bn.node.
DirichletParameterNode
(name, alpha, aliases=None)[source]¶ Bases:
alex.ml.bn.node.VariableNode
Node containing parameter.
-
class
alex.ml.bn.node.
DiscreteFactorNode
(name, factor)[source]¶ Bases:
alex.ml.bn.node.FactorNode
Node containing factor.
-
class
alex.ml.bn.node.
DiscreteVariableNode
(name, values, logarithmetic=True)[source]¶ Bases:
alex.ml.bn.node.VariableNode
Node containing variable.
-
class
alex.ml.bn.node.
FactorNode
(name, aliases=None)[source]¶ Bases:
alex.ml.bn.node.Node
-
exception
alex.ml.bn.node.
IncompatibleNeighborError
[source]¶ Bases:
alex.ml.bn.node.NodeError
-
class
alex.ml.bn.node.
Node
(name, aliases=None)[source]¶ Bases:
object
Abstract class for nodes in factor graph.
-
class
alex.ml.bn.node.
VariableNode
(name, aliases=None)[source]¶ Bases:
alex.ml.bn.node.Node