QWorld Bronze: Coin Flip

Published by strangeworks (01/06/2021)

Coin Flip: A Probabilistic Bit

Watch Lecture

A Fair Coin

A coin has two sides: Heads and Tails. After flipping a coin, we can get Heads or Tails. We can represent these two different cases by a single bit:

  • 0 represents Heads
  • 1 represents Tails

Flipping a fair coin If our coin is fair, then the probabilities of getting Heads and Tails are equal:

p=12=0.5p= \dfrac{1}{2} = 0.5

Flipping a fair coin can be represented as an operator:

FairCoin(Tails) =12Heads+12TailsFairCoin(Tails) \ = \frac{1}{2} Heads + \frac{1}{2}Tails

Or, by using 0 and 1:

HeadsTailsHeads1212Tails1212 \begin{array}{c|cc} & \mathbf{Heads} & \mathbf{Tails} \\ \hline \mathbf{Heads} & \dfrac{1}{2} & \dfrac{1}{2} \\ \mathbf{Tails} & \dfrac{1}{2} & \dfrac{1}{2} \end{array}

​Task 1: Simulating FairCoin in Python

  1. Flip a fair coin 100 times. Calculate the total number of heads and tails, and the n check the ratio of the number of heads and the number of tails.
  2. Do the same experiment 1000 times.
  3. Do the same experiment 10,000 times.
  4. Do the same experiment 100,000 times.
  5. Do your results get close to the ideal case (the numbers of heads and tails are equal)?
from random import randrange
#
# you may use method 'randrange' for this task
# randrange(n) returns a value from {0,1,...,n-1} randomly
##
# your solution is here
#

Flipping a biased coin

Our coin may have a bias.
For example, the probability of getting heads is greater than the probability of getting tails.
Here is an example:
BiasedCoin=HeadTailHead0.60.6Tail0.40.4BiasedCoin = \begin{array}{c|cc} & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & 0.6 & 0.6 \\ \mathbf{Tail} & 0.4 & 0.4 \end{array}
Or, by using 0 and 1 as the states:
BiasedCoin=0100.60.610.40.4BiasedCoin = \begin{array}{c|cc} &0 & 1\\ \hline 0 & 0.6 & 0.6 \\ 1 & 0.4 & 0.4 \end{array}

Task 2: Simulating BiasedCoin in Python

Flip the following biased coin 100 times. Calculate the total numbers of heads and tails, and then check the ratio of the number of heads and the number of tails.
  1. Do the same experiment 1000 times.
  2. Do the same experiment 10,000 times.
  3. Do the same experiment 100,000 times.
  4. Do your results get close to the ideal case?