๐งฉ API Reference¶
This page provides detailed documentation for all public interfaces in the Probability Calculator package.
๐ฉ Hat Class¶
A container that stores colored balls and allows random draws without replacement.
๐ Constructor¶
Hat(**kwargs)
Parameters¶
- Each keyword argument represents a color and the associated number of balls.
Example:
hat = Hat(red=3, blue=2)
๐ฝ draw(num_balls)¶
Draws a number of balls at random without replacement.
Parameters¶
num_balls(int) โ number of balls to draw.
Returns¶
A list of strings representing the drawn balls.
Behavior¶
- If
num_ballsโฅ total balls, all balls are returned. - Otherwise, a random subset is returned.
Example:
hat.draw(4)
๐งต __repr__()¶
Provides a developer-friendly string representation:
Hat(['red', 'blue', 'green'])
๐งช experiment() Function¶
Runs repeated Monte-Carlo trials to estimate the probability of drawing a specific combination of balls.
๐ Signature¶
experiment(hat, expected_balls, num_balls_drawn, num_experiments)
Parameters¶
| Name | Type | Description |
|---|---|---|
| hat | Hat |
The original hat object (deep copied internally) |
| expected_balls | dict[str, int] |
Required colors and their minimum counts |
| num_balls_drawn | int |
Number of balls drawn per experiment |
| num_experiments | int |
Number of Monte-Carlo repetitions |
๐ Process¶
- Deep copy the hat
- Draw
num_balls_drawnballs - Count colors
- Check if all expected colors meet/exceed their required counts
- Repeat for
num_experimentstrials - Return
successes / num_experiments
๐ค Returns¶
A float between 0 and 1, the estimated probability.
Example:
from probability_calculator import Hat, experiment
hat = Hat(black=6, red=4, green=3)
probability = experiment(
hat,
expected_balls={"red": 2, "green": 1},
num_balls_drawn=5,
num_experiments=3000
)
print(probability)
๐งญ Notes¶
- The original
hatis not modified - More experiments โ more accurate results
- Uses random sampling without replacement
Proceed to Examples to see full workflows in action! ๐