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

rename_variables(mapping)[source]
sum_other()[source]
exception alex.ml.bn.factor.FactorError[source]

Bases: exceptions.Exception

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.BP[source]

Bases: object

Abstract class for Belief Propagation algorithm.

run()[source]

Run inference algorithm.

exception alex.ml.bn.lbp.BPError[source]

Bases: exceptions.Exception

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.

add_layer(layer)[source]
add_layers(layers)[source]

Add layers of nodes to graph.

add_nodes(nodes)[source]

Add nodes to graph.

clear_layers()[source]
clear_nodes()[source]
init_messages()[source]
run(n_iterations=1, from_layer=None)[source]

Run the lbp 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.

add_neighbor(node, parent=True, **kwargs)[source]
init_messages()[source]
message_from(node, message)[source]
message_to(node)[source]
normalize(parents=None)[source]
update()[source]
class alex.ml.bn.node.DirichletParameterNode(name, alpha, aliases=None)[source]

Bases: alex.ml.bn.node.VariableNode

Node containing parameter.

add_neighbor(node)[source]
init_messages()[source]
message_from(node, message)[source]
message_to(node)[source]
normalize(parents=None)[source]
update()[source]
class alex.ml.bn.node.DiscreteFactorNode(name, factor)[source]

Bases: alex.ml.bn.node.FactorNode

Node containing factor.

add_neighbor(node, **kwargs)[source]
init_messages()[source]
message_from(node, message)[source]
message_to(node)[source]
update()[source]
class alex.ml.bn.node.DiscreteVariableNode(name, values, logarithmetic=True)[source]

Bases: alex.ml.bn.node.VariableNode

Node containing variable.

add_neighbor(node, **kwargs)[source]
init_messages()[source]
message_from(node, message)[source]
message_to(node)[source]
most_probable(n=None)[source]
observed(assignment_dict)[source]

Set observation.

update()[source]
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.

add_neighbor(node)[source]
connect(node, **kwargs)[source]

Add a neighboring node.

init_messages()[source]
message_from(node, message)[source]

Save message from neighboring node.

message_to(node)[source]

Compute a message to neighboring node.

normalize(parents=None)[source]

Normalize belief state.

rename_msg(msg)[source]
send_messages()[source]

Send messages to all neighboring nodes.

update()[source]

Update belief state.

exception alex.ml.bn.node.NodeError[source]

Bases: exceptions.Exception

class alex.ml.bn.node.VariableNode(name, aliases=None)[source]

Bases: alex.ml.bn.node.Node

alex.ml.bn.test_factor module

class alex.ml.bn.test_factor.TestFactor(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_add()[source]
test_alphas()[source]
test_apply_op_different()[source]
test_apply_op_same()[source]
test_apply_op_scalar()[source]
test_division()[source]
test_expected_value_squared()[source]
test_fast_div()[source]
test_fast_mul()[source]
test_fast_mul_correct()[source]
test_get_assignment_from_index()[source]
test_get_index_from_assignment()[source]
test_logsubexp()[source]
test_marginalize()[source]
test_mul_div()[source]
test_multiplication()[source]
test_multiplication_different_values()[source]
test_observations()[source]
test_parents_normalize()[source]
test_power()[source]
test_rename()[source]
test_setitem()[source]
test_strides()[source]
test_sum_other()[source]

alex.ml.bn.test_lbp module

class alex.ml.bn.test_lbp.TestLBP(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_ep()[source]
test_ep_tight()[source]
test_layers()[source]
test_network()[source]
test_single_linked()[source]

alex.ml.bn.test_node module

class alex.ml.bn.test_node.TestNode(methodName='runTest')[source]

Bases: unittest.case.TestCase

assertClose(first, second, epsilon=1e-06)[source]
test_dir_tight()[source]
test_network()[source]
test_observed_complex()[source]
test_parameter()[source]
test_parameter_simple()[source]
test_two_factors_one_theta()[source]
test_two_factors_one_theta2()[source]
alex.ml.bn.test_node.same_or_different(assignment)[source]

alex.ml.bn.utils module

alex.ml.bn.utils.constant_factor(variables, variables_dict, length, logarithmetic=True)[source]
alex.ml.bn.utils.constant_factory(value)[source]

Create function returning constant value.

Module contents