Skip to main content

Bayesian network calculator v2

Project description

BayesCalc2: A Bayesian Network Calculator

Category Badges
Package PyPI version Python 3.10+
License License: MIT
Release GitHub release
CI/CD CI Coverage
Code Quality Code style: black Checked with mypy Linting: flake8
Repo URL GitHub
Documentation User Guide, Developer Guide

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/

Changelog

See CHANGELOG.md for version history and updates.

Support

  • Documentation: See docs/user_guide.md for detailed usage instructions
  • Developer guide: See docs/developer_guide.md for how to get started to contribute and overview of key dev practices and algorithms.
  • Issues: Report bugs or request features on GitHub Issues

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.2}
}

License

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

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

bayescalc2-1.0.2.tar.gz (129.5 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.2-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bayescalc2-1.0.2.tar.gz
Algorithm Hash digest
SHA256 5abc48ee6925c891d951a14421155d2d2c0def58f8570ace5186a403814fd4da
MD5 ecc40ecfdd679a4d26a37ac9f61787f7
BLAKE2b-256 2a26d3447738d30a6c5fb4855514c69350e2a7d1800740038eb1754d18069092

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bayescalc2-1.0.2-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.7

File hashes

Hashes for bayescalc2-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3066f4dde9d314d71694b3e9daaa5d45fe73fe3f4b6885aa439dd6516637b560
MD5 a474fc92cc056552cd4d40d888300aca
BLAKE2b-256 70adbeefdf1bb44e6e041412b64a487748bef44d963bd452e099ba3e229b2657

See more details on using hashes here.

Supported by

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