Quickstart#
Task-oriented examples to help you get started with pgmpy.
The sections below provide a minimal example for various supported tasks and a link to the corresponding User Guide page for the full workflow.
Causal Discovery / Structure Learning#
User Guide: Causal Discovery and Structure Learning
Learn a graph structure directly from data.
from pgmpy.datasets import load_dataset
from pgmpy.causal_discovery import PC
dataset = load_dataset("sachs_discrete")
est = PC(ci_test="chi_square", return_type="dag")
est.fit(dataset.data)
print(est.causal_graph_)
Parameter Estimation#
User Guide: Parameter Estimation
Fit conditional distributions for a known graph structure.
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import DiscreteBayesianNetwork
from pgmpy.example_models import load_model
# Generate some data to use for fitting.
model = load_model("bnlearn/alarm")
df = model.simulate(n_samples=1000, seed=42)
# Create a network structure and fit data to it.
bn_struct = DiscreteBayesianNetwork(model.edges())
bn_struct.fit(df, estimator=MaximumLikelihoodEstimator)
bn_struct.cpds
Probabilistic Inference#
User Guide: Probabilistic Inference
Query posterior distributions from a Bayesian Network.
from pgmpy.example_models import load_model
from pgmpy.inference import VariableElimination
model = load_model("bnlearn/alarm")
infer = VariableElimination(model)
result = infer.query(
variables=["HISTORY"],
evidence={"CVP": "LOW", "PCWP": "LOW"},
)
print(result)
Causal Identification#
User Guide: Causal Identification
Check whether a causal effect is identifiable from the graph alone.
from pgmpy.base import DAG
from pgmpy.identification import Adjustment
dag = DAG(
[("X", "Y"), ("Z", "X"), ("Z", "Y")],
roles={"exposures": "X", "outcomes": "Y"},
)
identified_graph, is_identified = Adjustment(variant="minimal").identify(dag)
identified.get_role("adjustment")
Causal Inference#
User Guide: Causal Estimation
Estimate a causal effect from data once you have a causal graph.
from pgmpy.example_models import load_model
from pgmpy.inference import CausalInference
model = load_model("bnlearn/sachs")
infer = CausalInference(model)
infer.query(variables=["Akt"], do={"PKC": "LOW"})
Example Datasets and Models#
User Guides: Example Datasets | Example Models
Discover built-in datasets and example models.
from pgmpy.datasets import list_datasets, load_dataset
from pgmpy.example_models import list_models, load_model
print(list_datasets(is_discrete=True, has_ground_truth=True)[:3])
dataset = load_dataset("sachs_discrete")
print(dataset.name, dataset.data.shape)
print(list_models()[:3])
model = load_model("bnlearn/alarm")
print(len(model.nodes()), len(model.edges()))
Simulations#
User Guide: Simulations
Generate synthetic data from a model for testing and experimentation.
from pgmpy.example_models import load_model
model = load_model("bnlearn/ecoli70")
data = model.simulate(int(1e3))
data.head()
Extend pgmpy#
User Guide: Extensibility
Implement new datasets, models, metrics, or algorithms from the repository templates. pgmpy provides extension templates for each class of method. To add a new method, you can just follow the TODOs listed in the extension template for that method. All of our extension templates can be found at: https://github.com/pgmpy/pgmpy/tree/dev/devtools/extension_templates
Next Steps#
User Guide – Workflow-oriented documentation for each task area
Examples – Jupyter notebooks with longer walkthroughs
API Reference – Full public API documentation