Linear Gaussian Networks - Inference, Parameter Learning and Representation
Project description
Linear Gaussian Bayesian Networks -Representation, Learning and Inference
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:
-
Linear Gaussian Network: A directed BN where CPDs are linear gaussian.
-
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
-
Probabilistic Graphical Models - Principles and Techniques , Daphne Koller, Chapter 7.2
-
Gaussian Bayesian Networks, Sargur Srihari
Gaussian Belief Propagation
- Probabilistic Graphical Models - Principles and Techniques , Daphne Koller, Chapter 14.2.3
- Gaussian Belief Propagation: Theory and Aplication, Danny Bickson
License
MIT License Copyright (c) 2020, Prasad Ostwal
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
Built Distribution
File details
Details for the file LGNpy-1.0.0.tar.gz
.
File metadata
- Download URL: LGNpy-1.0.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39738c6ce8781befd6ce5ff797ce06e08236cf30a6cb455e9ce29108ae076908 |
|
MD5 | d273c0b698acac7d114d01181212ea2e |
|
BLAKE2b-256 | 9ea696e2a02a42ba23015d502586f4ed305f38d963660e276cd9b66862097852 |
File details
Details for the file LGNpy-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: LGNpy-1.0.0-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0fe72042e91809f1cb20a48ec1e57ac71a4caf2d3193d5af8ec1a2abae918c8 |
|
MD5 | 2a88dd6ecd66182cca14221c85fb1553 |
|
BLAKE2b-256 | 86430df5f66c67455ae1d070000a87a2e8eec5893cb37c35e066f4eecd33499a |