Coin Flipping

Author
Affiliation

K.C. Cupido

St. Francis Xavier University

Coin Flip Example

For a random variable \(X\) that follows the Bernoulli distribution with probability \(p\), each realization of the random variable has two possible outcomes (e.g., Success or Failure, 1 or 0, Heads or Tails, etc.). More specifically, the Bernoulli distribution is a discrete probability distribution such that \(P(X=1)=p\) and \(P(X=0)=1-p\).

As a simple example, we can design an experiment where we flip a coin and record the outcome using the following code:

Now let’s try flipping the coin a few times:

Let’s do this again, but this time we’ll keep track of the number of Heads and Tails that occur.

By default, the sample function gives every outcome an equal chance of happening. If we want to make certain events more likely than others, we can do this by adding a prob argument. For example, we can make it so that our coin has a 75% chance of landing on heads for any single flip by using the following code:

Finally, let’s try flipping the coin 100 times and keep track of how the frequency of Heads progresses over the tosses.