Spaces:
Sleeping
Sleeping
File size: 9,893 Bytes
821a5f5 571f3d0 d989c27 571f3d0 d989c27 571f3d0 73accd1 571f3d0 eb818c1 571f3d0 73accd1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
---
title: Bayesian Game
emoji: ๐ฒ
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: "4.44.0"
app_file: app.py
pinned: false
license: mit
---
# ๐ฒ 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. **Clone and navigate to the project:**
```bash
git clone <repository-url>
cd bayesian_game
```
2. **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
```
3. **Install dependencies:**
```bash
# Using uv
uv pip install -r requirements.txt
# Or using pip
pip install -r requirements.txt
```
4. **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. **Pure Functions**: Domains contain pure, testable functions
2. **Immutable Data**: Evidence and belief updates are immutable
3. **Clear Interfaces**: Well-defined boundaries between domains
4. **Comprehensive Testing**: Every component thoroughly tested
### Contributing
1. Follow the existing domain-driven architecture
2. Add tests for any new functionality
3. Maintain clean separation between domains
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. **Create a Hugging Face Space**: Go to [hf.co/new-space](https://hf.co/new-space) and create a new Gradio space
2. **Get your HF Token**: Visit [hf.co/settings/tokens](https://hf.co/settings/tokens) and create a token with write access
3. **Add GitHub Secret**: In your GitHub repository, go to Settings > Secrets and variables > Actions, and add:
- Name: `HF_TOKEN`
- Value: Your Hugging Face token
4. **Update workflow**: Edit `.github/workflows/deploy.yml` and replace:
- `HF_USERNAME`: Your Hugging Face username
- `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**
|