Skip to main content

BayesCalc2: A Bayesian Network Calculator

Category Link
Package PyPI version Python 3.10+
Documentation Documentation
License License: MIT
Release GitHub release
CI/CD CI Doc build Coverage
Code Quality Code style: black Checked with mypy Linting: flake8
Repo URL GitHub

Overview

A Bayesian network calculator designed for learning probabilistic reasoning using Bayesian networks. This tool allows you to define Bayesian networks, calculate probabilities, and perform various probabilistic operations using an efficient variable-elimination algorithm that scales well with network complexity.

Features

  • Efficient Inference: Uses variable elimination algorithm instead of exponentially-growing joint probability tables
  • Interactive REPL: Command-line interface with tab completion and command history
  • Batch Processing: Execute multiple queries from files or command strings
  • Rich Query Language: Support for conditional probabilities, arithmetic expressions, and independence tests
  • Information Theory: Built-in entropy, mutual information, and conditional entropy calculations
  • Network Analysis: Graph structure analysis with parent/child relationships
  • Network Visualization: Generate network diagrams with CPT tables (PDF, PNG, SVG)
  • Educational Focus: Clear output formatting ideal for learning and teaching

Installation

Requirements

  • Python 3.10 or higher
  • NumPy >= 2.3.3
  • prompt_toolkit >= 3.0.0
  • graphviz >= 0.20.0 (for visualization)

Install from PyPI

pip install bayescalc2

For visualization support install the graphviz system package:

# macOS
brew install graphviz

# Ubuntu/Debian
sudo apt-get install graphviz

# Fedora/Redhat
sudo dnf install graphviz

# Windows
# Download from https://graphviz.org/download/

Install from Source

git clone https://github.com/johan162/bayescalc2.git
cd bayescalc2
pip install -e .

Quick Start

# Download an example network
wget https://raw.githubusercontent.com/johan162/bayescalc2/main/examples/rain_sprinkler_grass.net

# Launch interactive mode
bayescalc rain_sprinkler_grass.net

# Try some queries
>> P(Rain)
>> P(Rain|GrassWet=True)
>> entropy(Rain)
>> exit

Usage

BayesCalc2 can be used in two modes:

1. Interactive Mode

bayescalc network_file.net

This launches an interactive REPL where you can enter probability queries and commands.

2. Batch Mode

bayescalc network_file.net --batch commands.txt

or

bayescalc network_file.net --cmd "P(Rain|GrassWet=Yes);printCPT(Rain)"

Network File Format

Create a Bayesian network definition in a .net file:

# Example network definition
boolean Rain
variable Sprinkler {On, Off}
variable GrassWet {Yes, No}

# CPT definitions
Rain {
    P(True) = 0.2
    # P(False) will be auto-filled
}

Sprinkler | Rain {
    P(On | True) = 0.01
    P(On | False) = 0.4
    # P(Off | parent) auto-filled
}

GrassWet | Rain, Sprinkler {
    P(Yes | True, On) = 0.99
    P(Yes | True, Off) = 0.8
    P(Yes | False, On) = 0.9
    # Remaining CPTs auto-completed
}

Available Commands

Probability Queries

  • P(A) - Marginal probability
  • P(A|B) - Conditional probability
  • P(A,B|C) - Joint conditional probability
  • P(A|B)*P(B)/P(A) - Arithmetic expressions

Network Analysis

  • printCPT(X) - Display conditional probability table
  • printJPT() - Display joint probability table
  • parents(X) - Show parent variables
  • children(X) - Show child variables
  • showGraph() - Display network structure
  • visualize(file.pdf) - Generate network visualization with CPT tables
  • load(file.net) - Load a different network file

Independence Testing

  • isindependent(A,B) - Test marginal independence
  • iscondindependent(A,B|C) - Test conditional independence

Information Theory

  • entropy(X) - Shannon entropy
  • conditional_entropy(X|Y) - Conditional entropy
  • mutual_information(X,Y) - Mutual information

Visualization Examples

# Generate PDF with CPT tables
>> visualize(network.pdf)

# Generate PNG without CPT tables
>> visualize(simple_network.png, show_cpt=False)

# Generate SVG with horizontal layout
>> visualize(network.svg, rankdir=LR)

Examples

The examples/ directory contains various Bayesian networks demonstrating different use cases:

  • rain_sprinkler_grass.net - Classic sprinkler example
  • medical_test.net - The classical Medical diagnosis scenario
  • student_network.net - Academic performance model
  • asia_chest_clinic.net - Medical expert system

Use Cases

  • Education: Teaching probabilistic reasoning and Bayesian networks
  • Research: Prototyping and testing Bayesian models
  • Analysis: Exploring conditional dependencies in data
  • Validation: Verifying hand-calculated probability results

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

Please read the docs/developer_guide.md for specific information about architecture and code base and how to contribute.

git clone https://github.com/johan162/bayescalc2.git
cd bayescalc2
python -m venv venv
source .venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -e ".[dev]"  # Quotes needed for zsh shell
python -m pytest tests/

Documentation & Support

Changelog

See CHANGELOG.md for version history and updates.

Citation

If you use BayesCalc2 in academic work, please cite:

@software{bayescalc2,
  title={BayesCalc2: A Bayesian Network Calculator},
  author={Johan Persson},
  year={2025},
  url={https://github.com/johan162/bayescalc2},
  version={v1.0.3}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bayescalc2-1.0.3.tar.gz (136.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bayescalc2-1.0.3-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

Details for the file bayescalc2-1.0.3.tar.gz.

File metadata

  • Download URL: bayescalc2-1.0.3.tar.gz
  • Upload date:
  • Size: 136.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for bayescalc2-1.0.3.tar.gz
Algorithm Hash digest
SHA256 fcb6db5f849fd5e5f1bc0d3019288390383fac8efbac88d38241264e7a9d3a5a
MD5 f9a6cc96fc63f19136b2243e25cb4317
BLAKE2b-256 ae29fea300ad7d79cfb9a45afc3511423851ac9ca01e5a10bf7916af99dbdcc2

See more details on using hashes here.

File details

Details for the file bayescalc2-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: bayescalc2-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 40.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for bayescalc2-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9ee3c041d45d1988522ec864abeb3fe96ebd2a18344263c8e30023402d737a36
MD5 5e0264e7003ba5a43a6e30f90264c5ea
BLAKE2b-256 bfb6f91b192449eab9574385ecd1a3c2beeaaf4bbfa6334535631faa1985ce76

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 files

1.0.2

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page