BayesCalc2: A Bayesian Network Calculator
| Category | Link |
|---|---|
| Package | |
| Documentation | |
| License | |
| Release | |
| CI/CD | |
| Code Quality | |
| Repo URL |
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 probabilityP(A|B)- Conditional probabilityP(A,B|C)- Joint conditional probabilityP(A|B)*P(B)/P(A)- Arithmetic expressions
Network Analysis
printCPT(X)- Display conditional probability tableprintJPT()- Display joint probability tableparents(X)- Show parent variableschildren(X)- Show child variablesshowGraph()- Display network structurevisualize(file.pdf)- Generate network visualization with CPT tablesload(file.net)- Load a different network file
Independence Testing
isindependent(A,B)- Test marginal independenceiscondindependent(A,B|C)- Test conditional independence
Information Theory
entropy(X)- Shannon entropyconditional_entropy(X|Y)- Conditional entropymutual_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 examplemedical_test.net- The classical Medical diagnosis scenariostudent_network.net- Academic performance modelasia_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
- Documentation: See the online documentation for both user and developer guides.
- Issues: Report bugs or request features on GitHub Issues
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.
Metadata
Release files for bayescalc2 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bayescalc2-1.0.3.tar.gz | 136.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bayescalc2-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 176.9 kB
Release files / bayescalc2-1.0.3.tar.gz
| Download URL | bayescalc2-1.0.3.tar.gz |
|---|---|
| Size | 136.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fcb6db5f849fd5e5f1bc0d3019288390383fac8efbac88d38241264e7a9d3a5a
|
|
BLAKE2b-256 checksum How to use checksums |
ae29fea300ad7d79cfb9a45afc3511423851ac9ca01e5a10bf7916af99dbdcc2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.8
|
Release files / bayescalc2-1.0.3-py3-none-any.whl
| Download URL | bayescalc2-1.0.3-py3-none-any.whl |
|---|---|
| Size | 40.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9ee3c041d45d1988522ec864abeb3fe96ebd2a18344263c8e30023402d737a36
|
|
BLAKE2b-256 checksum How to use checksums |
bfb6f91b192449eab9574385ecd1a3c2beeaaf4bbfa6334535631faa1985ce76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.8
|