Let’s flip a coin in Python

1. Let’s flip a coin in Python

Hi, I'm Alexander Ramirez, the CEO at Synergy Vision, which focuses on financial data science. This course is intended for people with basic knowledge about Python and familiarity with statistics. At the end of the course you'll have an understanding of basic probability concepts like random variables, calculations, probability distributions, and important results like the law of large numbers. We'll illustrate these concepts using simulations with Python.

2. Probability

Probability is the foundation of many of the methods and models in data science. Statistical inference methods use data to understand the underlying processes behind the data. With probability, we can study the regularities that arise in random phenomena.

3. Gain intuition

Let's try to gain some intuition about probability by starting with the classic random experiment, a coin flip. We'll start simulating with Python and learn to calculate probabilities and work with probability distributions.

4. Only two outcomes

If you flip a coin there are two possible outcomes: heads or tails. This random experiment is called a Bernoulli trial, after Jacob Bernoulli. A Bernoulli trial is one where the possible outcomes are binary: they can be modeled as success or failure, yes or no, or on or off. In this case the experiment is the flip of a coin; the possible outcomes are success (getting heads) or failure (getting tails). Each outcome is called an event. You assign probabilities to events. With a fair coin you have a 50% chance of getting heads and a 50% chance of getting tails for each event.

5. Flipping a coin in Python

To simulate the coin flips, we will use the bernoulli object from the Python library scipy dot stats. We can call bernoulli dot rvs, specifying two arguments, to generate random variates. The first argument is p, the probability of success, which in this case is 0.5; the second is size, which is the number of coin flips, in this case 1. The result of the first call was 0, or tails. After executing the call a second time we got 1, or heads.

6. Flipping multiple coins

For 10 coin flips, size equals 10. We can use sum to know how many heads we got. The first time, we got 5 heads and 5 tails.

7. Flipping multiple coins (Cont.)

After another draw, we got 2 heads and 8 tails. So, we can appreciate the randomness.

8. Flipping multiple coins (Cont.)

A sequence of independent Bernoulli trials follows the binomial distribution. So, instead of using the bernoulli object and adding the outcomes, we can use the binomial distribution and the binom object. We'll use three arguments: n for the number of coin flips, p for the probability of success, and size for the number of draws of the same experiment. On the first execution we got 7 heads out of 10 flips. Let's try 10 more times. As we can see, 5 is the result that repeats most often for a fair coin.

9. Flipping multiple coins (Cont.)

We can also change the probability of getting heads to 0.3, and see how that affects the results. As you may notice already, we can observe different outcomes using biased coins.

10. Random generator seed

We use random number generators to simulate the outcome of random experiment. If you run the same command with the same random seed, you will always get the same result. In Python we need to set a seed for the generator to produce similar outcomes in each experiment. Then we can check if the results are what we expected. We have two options to configure the generator: using the random_state parameter of the rvs function or using numpy dot random dot seed.

11. Random generator seed (Cont.)

For convenience, we will mostly use the numpy method throughout this course.

12. Let's practice flipping coins in Python

We can do some more experiments like this to get a sense about the outcomes that are most likely. So, let's practice flipping coins in Python!