Skip to main content

The sum-product algorithm. Belief propagation (message passing) for factor graphs

Project description


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

.. figure::
:alt: Simple factor graph

Simple factor graph
The factor graph used in ````.


Check ```` for details, but:

Create a factor graph


g = FactorGraph() # init the graph
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([
])) # create a factor node potentials for p(x1 | x2)
# connect the parents to their children
g.append('f12', x2)
g.append('f12', x1)

Run Inference

sum-product algorithm


g.compute_marginals(max_iter=500, tolerance=1e-6)

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.


res = g.brute_force()

Implementation Details

See block comments in the code's methods for details, but implementation
strategy comes from Chapter 5 of `David Barber's
book <>`__.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sumproduct-0.0.1.tar.gz (7.0 kB view hashes)

Uploaded source

Built Distribution

sumproduct-0.0.1-py2.py3-none-any.whl (8.0 kB view hashes)

Uploaded 2 7

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page