|
1 | | -# Probability Calculator |
2 | | - |
3 | | -## Overview |
4 | | -This project implements a Probability Calculator in Python to estimate the probability of drawing specific combinations of colored balls from a hat. It uses object-oriented programming with a `Hat` class and an `experiment` function to simulate random draws without replacement and calculate empirical probabilities. |
5 | | - |
6 | | -## Features |
7 | | -- **Hat Class**: |
8 | | - - Initialize with keyword arguments specifying ball colors and counts (e.g., `Hat(red=5, blue=2)`). |
9 | | - - Stores balls in a `contents` list, where each ball is represented by a string of its color. |
10 | | - - Method: `draw(num_balls)` to randomly draw and remove balls from the hat, returning them as a list. |
11 | | -- **Experiment Function**: |
12 | | - - Takes a `Hat` object, expected balls (dictionary), number of balls to draw, and number of experiments. |
13 | | - - Returns the probability of drawing at least the specified balls based on multiple experiments. |
14 | | -- **Functionality**: |
15 | | - - Simulates random draws without replacement. |
16 | | - - Uses deep copying to preserve the original hat. |
17 | | - - Handles cases where the number of balls drawn exceeds the hat's contents. |
18 | | - - Provides approximate probabilities through repeated experiments. |
19 | | - |
20 | | -## Installation |
21 | | -1. Clone the repository: |
22 | | - ```bash |
23 | | - git clone https://github.com/thesoulseizure/probability-calculator.git |
24 | | - ``` |
25 | | -2. Navigate to the project directory: |
26 | | - ```bash |
27 | | - cd probability-calculator |
28 | | - ``` |
29 | | -3. Ensure Python 3.x is installed: |
30 | | - ```bash |
31 | | - python --version |
32 | | - ``` |
33 | | - |
34 | | -## Usage |
35 | | -Here's an example of how to use the `Hat` class and `experiment` function: |
| 1 | +# 🎲 Probability Calculator |
| 2 | + |
| 3 | +A Python module for simulating the probability of drawing specific combinations of colored balls from a hat. |
| 4 | +Uses Monte‑Carlo simulation to estimate probabilities. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 📦 Features |
| 9 | + |
| 10 | +- 🎩 **Hat class** for creating bags of colored balls |
| 11 | +- 🔀 Random drawing **without replacement** |
| 12 | +- 📊 Monte‑Carlo probability estimation (`experiment`) |
| 13 | +- 🧪 Fully customizable simulations |
| 14 | +- 🖥️ **Command Line Interface** (`probability-calculator`) |
| 15 | +- 🧾 Detailed documentation (MkDocs Material) |
| 16 | +- ✔️ Fully tested and linted (pytest, flake8, black) |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 🚀 Installation |
| 21 | + |
| 22 | +Clone and install: |
| 23 | + |
| 24 | +```bash |
| 25 | +git clone https://github.com/TheComputationalCore/Probability-Calculator.git |
| 26 | +cd Probability-Calculator |
| 27 | +pip install . |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## 🎩 Basic Usage |
| 33 | + |
| 34 | +### Create a hat |
36 | 35 |
|
37 | 36 | ```python |
38 | | -from main import Hat, experiment |
| 37 | +from probability_calculator import Hat |
39 | 38 |
|
40 | | -# Create a hat with 6 black, 4 red, and 3 green balls |
41 | | -hat = Hat(black=6, red=4, green=3) |
| 39 | +hat = Hat(red=3, blue=2, green=6) |
| 40 | +print(hat.contents) |
| 41 | +``` |
42 | 42 |
|
43 | | -# Define expected balls to draw (at least 2 red and 1 green) |
44 | | -expected_balls = {"red": 2, "green": 1} |
| 43 | +### Draw balls |
45 | 44 |
|
46 | | -# Run experiment: draw 5 balls, 2000 times |
47 | | -probability = experiment( |
48 | | - hat=hat, |
49 | | - expected_balls=expected_balls, |
| 45 | +```python |
| 46 | +drawn = hat.draw(4) |
| 47 | +print(drawn) |
| 48 | +``` |
| 49 | + |
| 50 | +### Run a probability experiment |
| 51 | + |
| 52 | +```python |
| 53 | +from probability_calculator import experiment |
| 54 | + |
| 55 | +prob = experiment( |
| 56 | + hat, |
| 57 | + expected_balls={"red": 2, "green": 1}, |
50 | 58 | num_balls_drawn=5, |
51 | 59 | num_experiments=2000 |
52 | 60 | ) |
53 | | -print(probability) # Output: ~0.356 (varies due to randomness) |
54 | | - |
55 | | -# Example with different configuration |
56 | | -hat2 = Hat(blue=5, red=4, green=2) |
57 | | -expected_balls2 = {"red": 1, "green": 2} |
58 | | -probability2 = experiment( |
59 | | - hat=hat2, |
60 | | - expected_balls=expected_balls2, |
61 | | - num_balls_drawn=4, |
62 | | - num_experiments=2000 |
63 | | -) |
64 | | -print(probability2) # Output: ~0.1 (varies due to randomness) |
| 61 | + |
| 62 | +print(prob) |
65 | 63 | ``` |
66 | 64 |
|
67 | | -## Testing |
68 | | -The code has been tested to meet the following requirements: |
69 | | -1. Correct initialization of the `Hat` object with specified ball counts in `contents`. |
70 | | -2. The `draw` method reduces the number of balls in `contents`. |
71 | | -3. The `draw` method returns all balls when the number of balls to draw exceeds the hat's contents. |
72 | | -4. The `experiment` function returns an approximate probability that varies with each run due to randomness. |
| 65 | +--- |
| 66 | + |
| 67 | +## 🖥️ CLI Usage |
| 68 | + |
| 69 | +Run directly from the terminal: |
| 70 | + |
| 71 | +```bash |
| 72 | +probability-calculator --hat red=3 blue=2 green=6 --expect red=2 green=1 --draw 5 --experiments 2000 |
| 73 | +``` |
| 74 | + |
| 75 | +Output example: |
| 76 | + |
| 77 | +``` |
| 78 | +Estimated Probability: 0.2385 |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## 📚 Documentation |
| 84 | + |
| 85 | +Full documentation is available at: |
| 86 | + |
| 87 | +➡️ **https://thecomputationalcore.github.io/Probability-Calculator** |
| 88 | + |
| 89 | +Includes: |
| 90 | + |
| 91 | +- Usage Guide |
| 92 | +- API Reference |
| 93 | +- CLI Guide |
| 94 | +- Examples |
| 95 | +- Contribution Guide |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## 🧪 Running Tests |
| 100 | + |
| 101 | +```bash |
| 102 | +pytest -q |
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## 🧼 Code Quality |
| 108 | + |
| 109 | +The repo uses: |
| 110 | + |
| 111 | +- **Black** — code formatter |
| 112 | +- **Flake8** — linter |
| 113 | +- **pytest** — tests |
| 114 | + |
| 115 | +GitHub Actions automatically run: |
| 116 | + |
| 117 | +- Lint checks |
| 118 | +- Tests |
| 119 | +- Docs deployment |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## 🤝 Contributing |
| 124 | + |
| 125 | +Contributions are welcome! |
| 126 | +Please read: **CONTRIBUTING.md** |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 🛡 Security |
| 131 | + |
| 132 | +See: **SECURITY.md** |
| 133 | +Email vulnerabilities to: |
| 134 | + |
| 135 | +📧 **dineshchandra962@gmail.com** |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 📄 License |
| 140 | + |
| 141 | +Released under the **MIT License**. |
| 142 | + |
| 143 | +--- |
73 | 144 |
|
74 | | -To run tests, use the example code above in a Python environment. Open the browser console (F12) to see verbose test output if running in a compatible environment. |
75 | 145 |
|
76 | | -## Contributing |
77 | | -Contributions are welcome! Please fork the repository and submit a pull request with your changes. Ensure that your code follows the existing style and passes all tests. |
|
0 commit comments