Skip to content

Welcome to pyAgrum!

pyAgrum is a versatile Python library for creating and reasoning with Probabilistic Graphical Models (PGMs), such as Bayesian Networks and Markov Random Fields. Whether you’re a data scientist, researcher, or Python developer, pyAgrum offers an intuitive interface to model uncertainty and perform probabilistic inference. This guide will help you run your first pyAgrum example and explore its capabilities.

Below is a simple example of creating a Bayesian Network with pyAgrum and performing inference. This example demonstrates how to model two variables and compute probabilities based on evidence.

import pyAgrum as gum
# Create a Bayesian Network
bn = gum.BayesNet('SimpleBN')
bn.add(gum.LabelizedVariable('A', 'Cloudy', ['yes', 'no']))
bn.add(gum.LabelizedVariable('B', 'Rain', ['yes', 'no']))
bn.addArc("A", "B")
# There is also a faster compact syntax
bn = gum.fastBN("A{yes|no}->B{yes|no}")
# Set Conditional Probability Tables (CPTs)
bn.cpt("A").fillWith([0.4, 0.6]) # P(Cloudy=yes)=0.4, P(Cloudy=no)=0.6
bn.cpt("B")[{"A": "yes"}] = [0.8, 0.2] # P(Rain=yes|Cloudy=yes)=0.8
bn.cpt("B")[{"A": "no"}] = [0.1, 0.9] # P(Rain=yes|Cloudy=no)=0.1
# Perform inference
ie = gum.LazyPropagation(bn)
ie.setEvidence({"A": "yes"})
ie.makeInference()
print(ie.posterior("B")) # Output: Posterior probabilities for Rain
  1. Initialize the network: A Bayesian Network named SimpleBN is created with two variables: Cloudy (A) and Rain (B).
  2. Define structure: An arc is added from Cloudy to Rain, indicating that Rain depends on Cloudy.
  3. Set probabilities: Conditional Probability Tables (CPTs) are defined for Cloudy and Rain.
  4. Run inference: The LazyPropagation algorithm computes the posterior probability of Rain given evidence that Cloudy=yes.

This example is a great starting point to understand how pyAgrum models dependencies and performs probabilistic reasoning.

Official Documentation

Dive deeper into pyAgrum with the documentation for detailed guides, tutorials, and API references.

PyPI Page

Download the latest version of pyAgrum from PyPI.

FAQs

Find answers to common questions in our FAQ section.

Installation Guide

Need help setting up? Visit our Installation Guide for step-by-step instructions.

Join our vibrant community to ask questions, share your projects, or contribute to pyAgrum! Connect with other users and developers on our Discord server.

Join Our Discord

Ready to build your own Probabilistic Graphical Models? Try modifying the example above or explore the Official Documentation for advanced use cases. We can’t wait to see what you create with pyAgrum!