CoolFace
Apppublic

thompsonson/bayesian_game

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐ŸŽฒ Bayesian Game

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.

๐ŸŽฏ Game Overview

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.

๐Ÿ—๏ธ Architecture

Built using Domain-Driven Design with clean separation of concerns:

1. Environment Domain (domains/environment/)

  • โ€”Pure evidence generation - no probability knowledge
  • โ€”EnvironmentEvidence: Dataclass for dice roll results
  • โ€”Environment: Generates target values and dice roll comparisons

2. Belief Domain (domains/belief/)

  • โ€”Pure Bayesian inference - receives only comparison results, no dice roll values
  • โ€”BeliefUpdate: Dataclass containing only comparison results
  • โ€”BayesianBeliefState: Calculates likelihood P(comparison_result | target) for each possible target

3. Game Coordination (domains/coordination/)

  • โ€”Thin orchestration layer - coordinates between domains
  • โ€”GameState: Tracks current game state
  • โ€”BayesianGame: Main game orchestration class

4. UI Layer (ui/)

  • โ€”Interactive Gradio web interface
  • โ€”Real-time belief visualization
  • โ€”Game controls and statistics display

๐Ÿš€ Quick Start

Prerequisites

  • โ€”Python 3.10+
  • โ€”uv package manager (recommended) or pip

Installation

  1. 1.Clone and navigate to the project:
bash
git clone <repository-url>
cd bayesian_game
  1. 1.Set up virtual environment:
bash
# 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
  1. 1.Install dependencies:
bash
# Using uv
uv pip install -r requirements.txt

# Or using pip
pip install -r requirements.txt
  1. 1.Set up pre-commit hooks (optional for development):
bash
pre-commit install

Running the Game

Launch the interactive web interface:

bash
python app.py

The game will be available at http://localhost:7860

Run from command line (for development):

python
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}")

๐Ÿงช Testing

Run the comprehensive test suite:

bash
# 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

๐ŸŽฎ Game Interface

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

Interface Features

  • โ€”๐Ÿ“Š 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

๐Ÿ“ Project Structure

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

๐Ÿ”ฌ Key Features

Bayesian Inference Engine

  • โ€”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

Reproducible Experiments

  • โ€”Seeded Randomness: Reproducible results for testing
  • โ€”Deterministic Behavior: Same seed produces same game sequence
  • โ€”Statistical Analysis: Track accuracy and convergence

Clean Architecture

  • โ€”Domain Separation: Pure domains with no cross-dependencies
  • โ€”Testable Components: Each domain independently testable
  • โ€”Extensible Design: Easy to add new features or modify rules

๐ŸŽ“ Educational Value

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

๐Ÿ› ๏ธ Development

Key Dependencies

  • โ€”gradio: Web interface framework
  • โ€”numpy: Numerical computations for Bayesian inference
  • โ€”matplotlib: Belief distribution visualization
  • โ€”pytest: Testing framework
  • โ€”pre-commit: Code quality automation
  • โ€”ruff: Fast Python linter and formatter (replaces Black, isort, flake8)

Development Workflow

bash
# 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 .

CI/CD Pipeline

  • โ€”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

Design Principles

  1. 1.Pure Functions: Domains contain pure, testable functions
  2. 2.Immutable Data: Evidence and belief updates are immutable
  3. 3.Clear Interfaces: Well-defined boundaries between domains
  4. 4.Comprehensive Testing: Every component thoroughly tested

Contributing

  1. 1.Follow the existing domain-driven architecture
  2. 2.Add tests for any new functionality
  3. 3.Maintain clean separation between domains
  4. 4.Update documentation for new features

๐Ÿ“Š Example Game Flow

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)

๐Ÿš€ Deployment

Hugging Face Spaces (Automated)

The repository includes automated deployment to Hugging Face Spaces via GitHub Actions. To set this up:

  1. 1.Create a Hugging Face Space: Go to hf.co/new-space and create a new Gradio space
  2. 2.Get your HF Token: Visit hf.co/settings/tokens and create a token with write access
  3. 3.Add GitHub Secret: In your GitHub repository, go to Settings > Secrets and variables > Actions, and add:
  4. 4.Name: HF_TOKEN
  5. 5.Value: Your Hugging Face token
  6. 6.Update workflow: Edit .github/workflows/deploy.yml and replace:
  7. 7.HF_USERNAME: Your Hugging Face username
  8. 8.HF_SPACE_NAME: Your space name

The deployment will automatically trigger after successful CI runs on the main branch.

Other Deployment Options

  • โ€”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