aboutsummaryrefslogtreecommitdiff

beliefs

A library for performing inference with Bayesian Networks for a special use case, derived from pgmpy.

Motivation

Exact inference

This library provides the ability to perform exact inference in a computationally tractable* way for a specific but useful case: Bayesian Networks with * polytree structure * consisting of Bernoulli random variables whose relationship to their parents in the probabilistic graphical model are described by AND or OR logic

Non-deterministic conditional probability distributions for multinomial, discrete random variables are also supported, although the algorithm is specifically optimized for the case of Bernoulli AND and Bernoulli OR variables.

*See the "Many parents model" in the jupyter notebook under the examples/ directory for an example of a case in which inference becomes computationally intractable with pgmpy but can be handled by beliefs optimized algorithm.

Additional features

  • In addition to being able to perform inference based on direct observation of a variable in the PGM, beliefs also provides the ability to specify virtual evidence, i.e. evidence that modifies the belief, or marginal probability, of a variable by affecting its likelihood based on observations of variables not in the PGM, while not pinning the variable into a definite (observed) state.
  • The ability to catch conflicting evidence errors during inference, which manifest as numpy NaNs in pgmpy's inference results.
  • Utility for gathering the direct observations that influenced the beliefs of variables that were inferred to be in a definite state.

Getting started

Installation

Using conda:

conda install -c driver beliefs

Example

Perform inference on a Bayesian Network:

from beliefs.inference.belief_propagation import BeliefPropagation
from beliefs.models.belief_update_node_model import (
    BeliefUpdateNodeModel,
    BernoulliOrNode
)

# directed edges for a polytree Bayes Net
edges = [('u', 'x'), ('v', 'x'), ('x', 'y'), ('x', 'z')

# initialize model w/ edges, default to OR CPD for all variables
model = BeliefUpdateNodeModel.init_from_edges(edges, BernoulliOrNode)

# initialize inference
infer = BeliefPropagation(model)

# perform inference, with 'x' is observed to be True.
result = infer.query(evidence={'x': np.array([0, 1])})

Tests

From the project root directory:

pytest tests -vv

License

This project is licensed under the terms of the MIT license.