title | emoji | colorFrom | colorTo | sdk | sdk_version | app_file | pinned | license |
---|---|---|---|---|---|---|---|---|
Bayesian Game |
๐ฒ |
blue |
purple |
gradio |
4.44.0 |
app.py |
false |
mit |
A Bayesian Game implementation featuring a Belief-based Agent using domain-driven design. This interactive game demonstrates Bayesian inference in action as Player 2 attempts to deduce a hidden target die value based on evidence from dice rolls.
The Setup:
- Judge and Player 1 can see the target die value (1-6)
- Player 2 must deduce the target value using Bayesian inference
- Each round: Player 1 rolls dice and reports "higher"/"lower"/"same" compared to target
- Player 2 only receives the comparison result, NOT the actual dice roll value
- Game runs for 10 rounds (configurable)
- Judge ensures truth-telling
The Challenge: Player 2 starts with uniform beliefs about the target value and updates their beliefs after each piece of evidence using Bayes' rule. The key insight is that Player 2 must calculate the probability that ANY dice roll would produce the observed comparison result for each possible target value.
Built using Domain-Driven Design with clean separation of concerns:
- Pure evidence generation - no probability knowledge
EnvironmentEvidence
: Dataclass for dice roll resultsEnvironment
: Generates target values and dice roll comparisons
- Pure Bayesian inference - receives only comparison results, no dice roll values
BeliefUpdate
: Dataclass containing only comparison resultsBayesianBeliefState
: Calculates likelihood P(comparison_result | target) for each possible target
- Thin orchestration layer - coordinates between domains
GameState
: Tracks current game stateBayesianGame
: Main game orchestration class
- Interactive Gradio web interface
- Real-time belief visualization
- Game controls and statistics display
- Python 3.10+
uv
package manager (recommended) orpip
- Clone and navigate to the project:
git clone <repository-url>
cd bayesian_game
- Set up virtual environment:
# Using uv (recommended)
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Or using pip
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
# Using uv
uv pip install -r requirements.txt
# Or using pip
pip install -r requirements.txt
- Set up pre-commit hooks (optional for development):
pre-commit install
Launch the interactive web interface:
python app.py
The game will be available at http://localhost:7860
Run from command line (for development):
from domains.coordination.game_coordination import BayesianGame
# Create and start a game
game = BayesianGame(seed=42)
game.start_new_game(target_value=3)
# Play rounds
for round_num in range(5):
state = game.play_round()
evidence = state.evidence_history[-1]
print(f"Round {round_num + 1}: Rolled {evidence.dice_roll} โ {evidence.comparison_result}")
print(f"Most likely target: {state.most_likely_target}")
print(f"Belief entropy: {state.belief_entropy:.2f}")
Run the comprehensive test suite:
# Run all tests
python -m pytest tests/ -v
# Run specific domain tests
python -m pytest tests/test_environment_domain.py -v
python -m pytest tests/test_belief_domain.py -v
python -m pytest tests/test_game_coordination.py -v
# Run with coverage
python -m pytest tests/ --cov=domains --cov-report=html
Test Coverage:
- 56 comprehensive tests
- All core functionality covered
- Edge cases and error handling tested
- Reproducibility and randomness testing
The Gradio interface provides:
- Game Controls: Start new games, play rounds, reset settings
- Real-time Visualization: Belief probability distribution chart
- Game Statistics: Entropy, accuracy, round information
- Evidence History: Complete log of dice rolls and comparisons
- Customization: Adjustable dice sides and round count
- ๐ Belief Distribution Chart: Visual representation of Player 2's beliefs
- ๐ฏ Target Highlighting: True target and most likely guess highlighted
- ๐ Evidence Log: Complete history of all dice rolls and results
- โ๏ธ Game Settings: Customize dice sides (2-20) and max rounds (1-50)
- ๐ Reset & Replay: Easy game reset and replay functionality
bayesian_game/
โโโ domains/ # Core domain logic
โ โโโ environment/ # Evidence generation
โ โ โโโ environment_domain.py
โ โโโ belief/ # Bayesian inference
โ โ โโโ belief_domain.py
โ โโโ coordination/ # Game orchestration
โ โโโ game_coordination.py
โโโ ui/ # User interface
โ โโโ gradio_interface.py
โโโ tests/ # Comprehensive test suite
โ โโโ test_environment_domain.py
โ โโโ test_belief_domain.py
โ โโโ test_game_coordination.py
โโโ app.py # Main entry point
โโโ requirements.txt # Dependencies
โโโ CLAUDE.md # Project specifications
โโโ README.md # This file
- Proper Bayesian Updates: Uses Bayes' rule for belief updates
- Entropy Calculation: Measures uncertainty in beliefs
- Evidence Integration: Combines multiple pieces of evidence
- Impossible Evidence Handling: Gracefully handles contradictory evidence
- Seeded Randomness: Reproducible results for testing
- Deterministic Behavior: Same seed produces same game sequence
- Statistical Analysis: Track accuracy and convergence
- Domain Separation: Pure domains with no cross-dependencies
- Testable Components: Each domain independently testable
- Extensible Design: Easy to add new features or modify rules
This implementation demonstrates:
- Bayesian Inference: Real-world application of Bayes' rule
- Uncertainty Quantification: How beliefs evolve with evidence
- Information Theory: Entropy as a measure of uncertainty
- Domain-Driven Design: Clean software architecture patterns
- Test-Driven Development: Comprehensive testing strategies
gradio
: Web interface frameworknumpy
: Numerical computations for Bayesian inferencematplotlib
: Belief distribution visualizationpytest
: Testing frameworkpre-commit
: Code quality automationruff
: Fast Python linter and formatter (replaces Black, isort, flake8)
# Install pre-commit hooks
pre-commit install
# Run pre-commit manually
pre-commit run --all-files
# Run tests with coverage
python -m pytest tests/ --cov=domains --cov=ui --cov-report=html
# Code formatting and linting (automatic with pre-commit)
ruff check --fix .
ruff format .
mypy .
- GitHub Actions: Automated testing on Python 3.10, 3.11, 3.12
- Pre-commit hooks: Code quality checks (Ruff, mypy, bandit)
- Test coverage: Comprehensive coverage reporting
- Security scanning: Trivy vulnerability scanner
- Auto-deployment: Pushes to Hugging Face Spaces on main branch
- Pure Functions: Domains contain pure, testable functions
- Immutable Data: Evidence and belief updates are immutable
- Clear Interfaces: Well-defined boundaries between domains
- Comprehensive Testing: Every component thoroughly tested
- Follow the existing domain-driven architecture
- Add tests for any new functionality
- Maintain clean separation between domains
- Update documentation for new features
Round 1: Evidence "higher" (dice roll > target)
โโ P(roll>1)=5/6, P(roll>2)=4/6, ..., P(roll>6)=0/6
โโ Lower targets become more likely
โโ Entropy: 2.15 bits
Round 2: Evidence "lower" (dice roll < target)
โโ P(roll<1)=0/6, P(roll<2)=1/6, ..., P(roll<6)=5/6
โโ Higher targets become more likely
โโ Entropy: 1.97 bits
Round 3: Evidence "same" (dice roll = target)
โโ P(roll=target) = 1/6 for all targets
โโ Beliefs remain proportional to previous round
โโ Entropy: 1.97 bits (unchanged)
The repository includes automated deployment to Hugging Face Spaces via GitHub Actions. To set this up:
- Create a Hugging Face Space: Go to hf.co/new-space and create a new Gradio space
- Get your HF Token: Visit hf.co/settings/tokens and create a token with write access
- Add GitHub Secret: In your GitHub repository, go to Settings > Secrets and variables > Actions, and add:
- Name:
HF_TOKEN
- Value: Your Hugging Face token
- Name:
- Update workflow: Edit
.github/workflows/deploy.yml
and replace:HF_USERNAME
: Your Hugging Face usernameHF_SPACE_NAME
: Your space name
The deployment will automatically trigger after successful CI runs on the main branch.
- Local Server: Built-in Gradio server (
python app.py
) - Cloud Platforms: Standard Python web app deployment
Built with โค๏ธ using Domain-Driven Design and Bayesian Inference