Skip to main content
`sumproduct <https://pypi.python.org/pypi/sumproduct>`__
========================================================

|Build Status| |Downloads|

An implementation of Belief Propagation for factor graphs, also known as
the sum-product algorithm
(`Reference <http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=Brml.HomePage>`__).

::

pip install sumproduct

.. figure:: http://f.cl.ly/items/2P021j2y3A2Q191F451h/unnamed0.png
:alt: Simple factor graph

Simple factor graph
The factor graph used in ``test.py`` (image made with
`yEd <http://www.yworks.com/en/products_yed_applicationfeatures.html>`__).

Basic Usage
-----------

Create a factor graph
~~~~~~~~~~~~~~~~~~~~~

::

from sumproduct import Variable, Factor, FactorGraph
import numpy as np

g = FactorGraph(silent=True) # init the graph without message printouts
x1 = Variable('x1', 2) # init a variable with 2 states
x2 = Variable('x2', 3) # init a variable with 3 states
f12 = Factor('f12', np.array([
[0.8,0.2],
[0.2,0.8],
[0.5,0.5]
])) # create a factor, node potential for p(x1 | x2)
# connect the parents to their children
g.add(f12)
g.append('f12', x2) # order must be the same as dimensions in factor potential!
g.append('f12', x1) # note: f12 potential's shape is (3,2), i.e. (x2,x1)

Run Inference
~~~~~~~~~~~~~

sum-product algorithm
^^^^^^^^^^^^^^^^^^^^^

::

>>> g.compute_marginals()
>>> g.nodes['x1'].marginal()
array([ 0.5, 0.5])

Brute force marginalization and conditioning
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The sum-product algorithm can only compute exact marginals for acyclic
graphs. Check against the brute force method (at great computational
expense) if you have a loopy graph.

::

>>> g.brute_force()
>>> g.nodes['x1'].bfmarginal
array([ 0.5, 0.5])

Condition on Observations
^^^^^^^^^^^^^^^^^^^^^^^^^

::

>>> g.observe('x2', 2) # observe state 1 (middle of above f12 potential)
>>> g.compute_marginals(max_iter=500, tolerance=1e-6)
>>> g.nodes['x1'].marginal()
array([ 0.2, 0.8])
>>> g.brute_force()
>>> g.nodes['x1'].bfmarginal
array([ 0.2, 0.8])

Additional Information
^^^^^^^^^^^^^^^^^^^^^^

``sumproduct`` implements a parallel message passing schedule: Message
passing alternates between Factors and Variables sending messages to all
their neighbors until the convergence of marginals.

Check ``test.py`` for a detailed example.

Implementation Details
----------------------

See block comments in the code's methods for details, but the
implementation strategy comes from Chapter 5 of `David Barber's
book <http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=Brml.HomePage>`__.

.. |Build Status| image:: https://travis-ci.org/ilyakava/sumproduct.svg?branch=master
:target: https://travis-ci.org/ilyakava/sumproduct
.. |Downloads| image:: https://pypip.in/download/sumproduct/badge.svg
:target: https://pypi.python.org/pypi/sumproduct/

Metadata

Release files for sumproduct 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sumproduct 0.0.6
File Size Uploaded
sumproduct-0.0.6.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sumproduct 0.0.6
File Interpreter ABI Platform
sumproduct-0.0.6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 17.3 kB

Release files / sumproduct-0.0.6.tar.gz

Download URL sumproduct-0.0.6.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
18f180da6f534b2db3926a7a12c0b07af75faa28979e5c706520fa8d72751b8c
BLAKE2b-256 checksum
How to use checksums
67c48a854a0fcbca6fa8d2e153f8eac6c2c454d210a15bba737c1dfa063cbf74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sumproduct-0.0.6-py2.py3-none-any.whl

Download URL sumproduct-0.0.6-py2.py3-none-any.whl
Size 9.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
766800d17588e103ff63787b05ba2cc743790ff0e36de50e63a19bb5740942fd
BLAKE2b-256 checksum
How to use checksums
1347067d4cba90df7340ac0ca675209a7984310b72650ee6869a0af6acf06097
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.0.7

2 release files

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page