Bayesian network calculator v2
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcb6db5f849fd5e5f1bc0d3019288390383fac8efbac88d38241264e7a9d3a5a
|
|
| MD5 |
f9a6cc96fc63f19136b2243e25cb4317
|
|
| BLAKE2b-256 |
ae29fea300ad7d79cfb9a45afc3511423851ac9ca01e5a10bf7916af99dbdcc2
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ee3c041d45d1988522ec864abeb3fe96ebd2a18344263c8e30023402d737a36
|
|
| MD5 |
5e0264e7003ba5a43a6e30f90264c5ea
|
|
| BLAKE2b-256 |
bfb6f91b192449eab9574385ecd1a3c2beeaaf4bbfa6334535631faa1985ce76
|