Skip to main content

Bayesian network calculator v2

Project description

BayesCalc2: A Bayesian Network Calculator

Category Badge
License License: MIT
Python PyPI version Python 3.10+
Code policy Code style: black Checked with mypy Linting: flake8
Pipeline Continuous Integration Coverage

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
  • Discussions: Ask questions in GitHub Discussions

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

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.0.tar.gz (127.9 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.0-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bayescalc2-1.0.0.tar.gz
  • Upload date:
  • Size: 127.9 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.0.tar.gz
Algorithm Hash digest
SHA256 44e9ca319f00e8ea44180a6ac79c6b49e58595645460b9fcb5a63b13ee359408
MD5 7cb5c97d33f24f8b7ec8ce433613df51
BLAKE2b-256 62bbfb1aabac408a0b6f9fe900d43549ed2f1b9a834aa512d78c28f0f5105fc0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bayescalc2-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 40.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9ecfe36b70d1e513b8fd697e2ae73bec8a31261c0acdd7d8803a9fa6675a7a9
MD5 102d673714de8d69ed4083c1fd8bc452
BLAKE2b-256 ee23182152af40d896e5abd2174a88484823b1982fad6b4b671651f0167b598f

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