Skip to content

Determines the approximate probability of drawing certain balls randomly from a hat.

Notifications You must be signed in to change notification settings

rickydebojeet/probability-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Probability Calculator

Suppose there is a hat containing 5 blue balls, 4 red balls, and 2 green balls. What is the probability that a random draw of 4 balls will contain at least 1 red ball and 2 green balls? While it would be possible to calculate the probability using advanced mathematics, an easier way is to write a program to perform a large number of experiments to estimate an approximate probability.

For this project, the program determines the approximate probability of drawing certain balls randomly from a hat.

A Hat class in prob_calculator.py takes a variable number of arguments that specify the number of balls of each color that are in the hat. For example, a class object could be created in any of these ways:

hat1 = Hat(yellow=3, blue=2, green=6)
hat2 = Hat(red=5, orange=4)
hat3 = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9)

A hat will always be created with at least one ball. The arguments passed into the hat object upon creation is converted to a contents instance variable. contents is a list of strings containing one item for each ball in the hat. Each item in the list is a color name representing a single ball of that color. For example, if your hat is {"red": 2, "blue": 1}, contents is ["red", "red", "blue"].

The Hat class have a draw method that accepts an argument indicating the number of balls to draw from the hat. This method removes balls at random from contents and return those balls as a list of strings. The balls does not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, all the balls are returned.

An experiment function in prob_calculator.py (not inside the Hat class) accepts the following arguments:

  • hat: A hat object containing balls that should be copied inside the function.
  • expected_balls: An object indicating the exact group of balls to attempt to draw from the hat for the experiment. For example, to determine the probability of drawing 2 blue balls and 1 red ball from the hat, set expected_balls to {"blue":2, "red":1}.
  • num_balls_drawn: The number of balls to draw out of the hat in each experiment.
  • num_experiments: The number of experiments to perform. (The more experiments performed, the more accurate the approximate probability will be.)

The experiment function returns a probability.

For example, let's say that you want to determine the probability of getting at least 2 red balls and 1 green ball when you draw 5 balls from from a hat containing 6 black, 4 red, and 3 green. To do this, we perform N experiments, count how many times M we get at least 2 red balls and 1 green ball, and estimate the probability as M/N. Each experiment consists of starting with a hat containing the specified balls, drawing a number of balls, and checking if we got the balls we were attempting to draw.

Here is how the experiment function is called based on the example above with 2000 experiments:

hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat, 
                  expected_balls={"red":2,"green":1},
                  num_balls_drawn=5,
                  num_experiments=2000)

Since this is based on random draws, the probability will be slightly different each time the code is run.

Development

Write your code in prob_calculator.py. For development, you can use main.py to test your code. Click the "run" button and main.py will run.

Testing

The unit tests for this project are in test_module.py. We imported the tests from test_module.py to main.py for your convenience. The tests will run automatically whenever you hit the "run" button.

About

Determines the approximate probability of drawing certain balls randomly from a hat.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages