Skip to content
/ pgmpy Public
forked from pgmpy/pgmpy

Python Library for learning (Structure and Parameter), inference (Probabilistic and Causal), and simulations in Bayesian Networks.

License

Notifications You must be signed in to change notification settings

Nimish-4/pgmpy

 
 

Repository files navigation

pgmpy is a Python library for causal and probabilistic modeling using Bayesian Networks and related models. It provides a uniform API for building, learning, and analyzing models such as Bayesian Networks, Dynamic Bayesian Networks, Directed Acyclic Graphs (DAGs), and Structural Equation Models(SEMs). By integrating tools from both probabilistic inference and causal inference, pgmpy enables users to seamlessly transition between predictive and interventional analyses.



Build codecov Version !conda Python Version License Downloads asv

Join the pgmpy Discord server Read the Docs Examples Tutorial

Key Features

Feature Description
Causal Discovery / Structure Learning Learn the model structure from data, with optional integration of expert knowledge.
Causal Validation Assess how compatible the causal structure is with the data.
Parameter Learning Estimate model parameters (e.g., conditional probability distributions) from observed data.
Probabilistic Inference Compute posterior distributions conditioned on observed evidence.
Causal Inference Compute interventional and counterfactual distributions using do-calculus.
Simulations Generate synthetic data under specified evidence or interventions.

Resources and Links

Quickstart

Installation

pgmpy is available on both PyPI and anaconda. To install from PyPI, use:

pip install pgmpy

To install from conda-forge, use:

conda install conda-forge::pgmpy

Discrete Data

from pgmpy.utils import get_example_model

# Load a Discrete Bayesian Network and simulate data.
discrete_bn = get_example_model('alarm')
alarm_df = discrete_bn.simulate(n_samples=100)

# Learn a network from simulated data.
from pgmpy.estimators import PC
dag = PC(data=alarm_df).estimate(ci_test='chi_square', return_type='dag')

# Learn the parameters from the data.
dag_fitted = dag.fit(alarm_df)
dag_fitted.get_cpds()

# Drop a column and predict using the learned model.
evidence_df = alarm_df.drop(columns=['FIO2'], axis=1)
pred_FIO2 = dag_fitted.predict(evidence_df)

Linear Gaussian Data

# Load an example Gaussian Bayesian Network and simulate data
gaussian_bn = get_example_model('ecoli70')
ecoli_df = gaussian_bn.simulate(n_samples=100)

# Learn the network from simulated data.
from pgmpy.estimators import PC
dag = PC(data=ecoli_df).estimate(ci_test='pearsonr', return_type='dag')

# Learn the parameters from the data.
from pgmpy.models import LinearGausianBayesianNetwork
gaussian_bn = LinearGausianBayesianNetwork(dag.edges())
dag_fitted = gaussian_bn.fit(ecoli_df)
dag_fitted.get_cpds()

# Drop a column and predict using the learned model.
evidence_df = ecoli_df.drop(columns=['ftsJ'], axis=1)
pred_ftsJ = dag_fitted.predict(evidence_df)

Contributing

We welcome all contributions --not just code-- to pgmpy. Please refer out contributing guide for more details. We also offer mentorship for new contributors and maintain a list of potential mentored projects. If you are interested in contributing to pgmpy, please join our discord server and introduce yourself. We will be happy to help you get started.

About

Python Library for learning (Structure and Parameter), inference (Probabilistic and Causal), and simulations in Bayesian Networks.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.7%
  • Shell 0.3%