Skip to main content

Linear Gaussian Bayesian Networks -Representation, Learning and Inference

Build Status PyPI - License PyPI - Python Version Documentation Status codecov Downloads

A Bayesian Network (BN) is a probabilistic graphical model that represents a set of variables and their conditional dependencies via graph. Gaussian BN is a special case where set of continuous variables are represented by Gaussian Distributions. Gaussians are surprisingly good approximation for many real world continuous distributions.

This package helps in modelling the network, learning parameters through data and running inference with evidence(s). Two types of Gaussian BNs are implemented:

  1. Linear Gaussian Network: A directed BN where CPDs are linear gaussian.

  2. Gaussian Belief Propagation: An undirected BN where it runs message passing algorithm to iteratively solve precision matrix and find out marginals of variables with or without conditionals.

Installation

$ pip install lgnpy

or clone the repository.

$ git clone https://github.com/ostwalprasad/lgnpy

Getting Started

Here are steps for Linear Gaussian Network. Gaussian Belief Propagation Model is also similar.

1. Create Network

import pandas as pd
import numpy as np
from lgnpy import LinearGaussian

lg = LinearGaussian()
lg.set_edges_from([('A', 'D'), ('B', 'D'), ('D', 'E'), ('C', 'E')])

2 Create Data and assign to it to network.

​ Create synthetic data for network using pandas and bind network with the data. There's no need to separately calculate means and covariance matrix.

np.random.seed(42)
n=100
data = pd.DataFrame(columns=['A','B','C','D','E'])
data['A'] = np.random.normal(5,2,n)
data['B'] = np.random.normal(10,2,n)
data['D'] = 2*data['A'] + 3*data['B'] + np.random.normal(0,2,n)
data['C'] = np.random.normal(-5,2,n)
data['E'] = 3*data['C'] + 3*data['D'] + np.random.normal(0,2,n)

lg.set_data(data)

3. Set Evidence(s)

Evidence are optional and can be set before running inference.

 lg.set_evidences({'A':5,'B':10})

4. Run Inference

For each node, CPT (Conditional Probability Distribution) is defined as::


where, its parameters are calculated using conditional distribution of parent(s) and nodes:


run_inference() returns inferred means and variances of each nodes.

lg.run_inference(debug=False)

Additional Functions:

lg.plot_distributions(save=False)


lg.network_summary()


lg.draw_network(filename='sample_network',open=True)



Examples

Notebook: Linear Gaussian Networks

Known Issues

GaussianBP algorithm does not converge for some specific precision matrices (inverse covariances). Solution is to use Graphcial Lasso or similar estimator methods to find precision matrix. Pull requests are welcome.

References:

Linear Gaussian Networks
  1. Probabilistic Graphical Models - Principles and Techniques , Daphne Koller, Chapter 7.2

  2. Gaussian Bayesian Networks, Sargur Srihari

Gaussian Belief Propagation
  1. Probabilistic Graphical Models - Principles and Techniques , Daphne Koller, Chapter 14.2.3
  2. Gaussian Belief Propagation: Theory and Aplication, Danny Bickson

License

MIT License Copyright (c) 2020, Prasad Ostwal

Metadata

Release files for LGNpy 1.0.0

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

Source distribution (sdist)

Source distribution for LGNpy 1.0.0
File Size Uploaded
LGNpy-1.0.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for LGNpy 1.0.0
File Interpreter ABI Platform
LGNpy-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.5 kB

Release files / LGNpy-1.0.0.tar.gz

Download URL LGNpy-1.0.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
39738c6ce8781befd6ce5ff797ce06e08236cf30a6cb455e9ce29108ae076908
BLAKE2b-256 checksum
How to use checksums
9ea696e2a02a42ba23015d502586f4ed305f38d963660e276cd9b66862097852
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / LGNpy-1.0.0-py3-none-any.whl

Download URL LGNpy-1.0.0-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0fe72042e91809f1cb20a48ec1e57ac71a4caf2d3193d5af8ec1a2abae918c8
BLAKE2b-256 checksum
How to use checksums
86430df5f66c67455ae1d070000a87a2e8eec5893cb37c35e066f4eecd33499a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.5

2 release files

0.0.4

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