Skip to main content
sumproduct
==========

An implementation of Belief Propagation for factor graphs, also known as
the sum product algorithm.

::

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``.

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 potentials for p(x1 | x2)
# connect the parents to their children
g.add(f12)
g.append('f12', x2)
g.append('f12', 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])

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

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>`__.

Metadata

Release files for sumproduct 0.0.2

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.2
File Size Uploaded
sumproduct-0.0.2.tar.gz 7.3 kB Details

Built distribution (wheel)

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

Total release size: 15.7 kB

Release files / sumproduct-0.0.2.tar.gz

Download URL sumproduct-0.0.2.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0470e8f2abc78a757794ccfa03b99ed60118c8b77a43b5f06de8a6277da9bef6
BLAKE2b-256 checksum
How to use checksums
8b2137e3d939c9cf7f853f38ad2d38e53118114930bb8e3114f9ee5feedf5ffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL sumproduct-0.0.2-py2.py3-none-any.whl
Size 8.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f709998eb330d871e11785e270705dee010e60db82f99bdd0e0539898bf25882
BLAKE2b-256 checksum
How to use checksums
f683ce40f732cdca28cbe17b42d5abb876e4bbc4e203590edcbf98b40cb3e32d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

This release

0.0.2 This release

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