Probstat/coin toss experiments

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา

In this part, we will perform a few simulations on coin tosses. Below are example codes for generating random numbers in Java and Python. You can definitely use other languages.

Some code

The following is a Java code for randomly tossing a fair coin for 100 times.

import java.util.Random;

public class CoinToss {

  public static void main(String[] args) {
    Random random = new Random();
    
    int N = 100;
    int c = 0;
    
    for (int i = 0; i < N; i++) {
      boolean head = random.nextDouble() >= 0.5;
      if (head) {
        c++;
      }
    }
    System.out.println("Heads: " + c);
    System.out.println("Freq/N: " + ((double) c)/N);    
  }
}

And this is an equivalent one in Python.

import random

N = 100
c = 0
for i in range(N):
    head = random.random() >= 0.5
    if head:
        c += 1

print "Heads:",c
print "Freq/N:",float(c)/N

Distributions

Plotting distributions

Run the simulations for the following experiments many times (at least 1000 times for smaller ones) and plot the histograms (as bar charts) of the number of heads.

  • Make 2 coin tosses.
  • Make 3 coin tosses.
  • Make 5 coin tosses.
  • Make 10 coin tosses.
  • Make 50 coin tosses.
  • Make 1000 coin tosses. (In your histogram, you may want to try to use bucket size = 10 so that your bar chart is manageable.)

Conditional distributions

Try the same experiments for 50 coin tosses and 1000 coin tosses, but if you get less than 22 (for 50 coin-toss case) or 480 (for 1000 coin-toss case) you run the experiment again. Plot the histogram for the whole range of possible numbers of heads. Compare the histograms with the ones from the previous experiments.

Number of trials

From the previous experiments we see that as we increase N, the results seem to be closer to the middle of the range.

In this part, we will see how the number of repeated trials affects the estimations. In all of the following experiments, to improve visualization, we will plot the histogram on a fixed number of buckets. Let's use 101 buckets from 0 - 1.

Run the following experiments and plot their histograms.

  • Perform 1 coin toss, report # of heads.
  • Perform 2 coin toss, report (# of heads)/2.
  • Perform 5 coin toss, report (# of heads)/5.
  • Perform 10 coin toss, report (# of heads)/10.
  • Perform 50 coin toss, report (# of heads)/50.
  • Perform 100 coin toss, report (# of heads)/100.
  • Perform 1000 coin toss, report (# of heads)/1000.

Probability estimations (optional)

In this last part, we will try to use computers to help us estimate various probability of "interesting" events. From the previous experiments, you know that you need large enough trials to get good estimates.

Try to estimate the probability of the following "events". Can you also figure out (analytically) the correct probabilities of these events?

  • Toss a coin 2 times and get 2 heads.
  • Toss a coin 5 times and get 0 heads.
  • Toss a coin 10 times and get 0 heads.
  • Toss a coin 10 times and get 2 heads.
  • Toss a coin 20 times and get exactly 10 heads.
  • Toss a coin 20 times and get at most 7 heads.
  • Toss a coin 20 times and get at most 5 heads.
  • Toss a coin 20 times and get 1 head.
  • Toss a coin 100 times and get at least 50 heads.
  • Toss a coin 100 times and get at most 40 heads.
  • Toss a coin 100 times and get at most 30 heads.