DEV Community

Cover image for Introduction to Probability Using Python
Lisandra Melo
Lisandra Melo

Posted on

Introduction to Probability Using Python

This article is an English translation of my article which was written on Brazilian Portuguese and posted here on my profile.

What Is Probability?

When we roll a dice or throw a coin we deal with an attribute very common on some of our daily lives events, randomness, in these type situations we do not have full control of the output we are going to receive.
We use probability studies to try to empower ourselves with a bit of control in this type of situation, which is to say, we try to predict what outcomes are easier or harder to occur in an event. Thus, we can say that probability is the study of how likely a result is in an event.

In general, we represent a probability as the ratio between the outcomes we want to receive and the possible results of the event. So, being P(E) the probability of an event, we have the following equation.

P(E)=n(wantedOutcomes)n(allOutcomes) P(E) = \frac{n(wantedOutcomes)}{n(allOutcomes)}

This equation results in a value in a number between 0 and 1 and this probability represents the likeability of occurrence of one or more outcomes in each execution of the event. However, it is important to remember that generally in the real world we do not record the exact calculated values, after all, the event is random, the probability determines what usually tends to happen, that is, it is an approximation.

For example, if we want to know how likely it is that an even number will be obtained in a die roll, we must perform the following process:

  • First, we must separate our wanted cases, that is, the even numbers in a dice which are 2, 4, and 6 (three possibilities).
  • Then we must obtain all of the possible outcomes which are the numbers 1,2,3,4,5,6 six possibilities).
  • After that, we will apply these values in the equation presented above, so we will have:
    P(E)=36=12=0,5P(E) = \frac{3}{6} = \frac{1}{2} = 0,5
    Thus, we will have a probability of 0.5 to get an even number on a die roll, which means that out of 10 rolls we will probably get 5 even numbers and from 10000, 5000 even numbers.

Now we will check the behavior of the mentioned situation using programming to simulate it.

Random Events Using Python

We will create a program that, when executed, shows us the number of even numbers obtained in 10,000 dice rolls.

For it, we will use the function random.choice () from the Python library numpy which can choose, randomly, between values of an array, so we will need to import the numpy library at the beginning of the program.

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Now we will define all the outcomes, the wanted outcomes, the counter of the times an even number was obtained, and a constant with the number of times our "dice" will be rolled in the program.

# Defining a array with all possible outcomes
possible_outcomes = [1,2,3,4,5,6]

# Defining an array with all wanted outcomes
wanted_outcomes = [2,4,6]

# Defining the counter of the times an even number was obtained
number_of_even = 0

# Defining the number of times our "dice" will be rolled in the program
# We must use capitalized letters to represent that we are creating a constant
Enter fullscreen mode Exit fullscreen mode

Now we will define our iterator that will go through our code 10,000 times choosing a random value in our outcomes (dice) each time and checking if that value is even if so, we will add 1 to the number_of_even variable.

# Now we will write an iterator for dice rolling
for i in range (NUMBER_OF_EXECUTIONS):
    # np.random.choice() chooses between the values of  all the outcomes of the event
    x = np.random.choice(possible_outcomes)
    If the chosen value is in our wanted outcomes we will increment the counter of the times an even number was obtained 
    if(x in wanted_outcomes):
        number_of_even += 1
Enter fullscreen mode Exit fullscreen mode

Finally, we will print the value of number_of_even, that is, our number of even launches and the probability that it will be with the equation presented previously.

# Calculating our probability
probability_of_even  = number_of_even/NUMBER_OF_EXECUTIONS

# Printing the results
print("{0: d} of the outcomes were even, so, we had a probability of around {1:.2f}".format(number_of_even, probability_of_even))
Enter fullscreen mode Exit fullscreen mode

When executing the program you will notice that the probability value rarely differs from 0.5 (in every execution of the program the value returned in numpy.random.choice () are different, so the number of even launches changes) which is what we predict initially as a probability of rolling dice with an even result.

Hence, is noticeable that probability creates a very good approximation of how real random situations tend to occur. Therefore, using tools from this area we can reach the most diverse conclusions about any random events.

The developed project is available in my gitlab repository. I hope that I have helped you in any way, and if you have any doubts, comments or problems feel free to leave a comment in this post ;).

Top comments (0)