Simulations and Demonstrations for
Introduction to the Practice of Statistics
Chapter 4

NOTE: This page has been delinked.  It is no longer being maintained, and information on this page may be out of date.

The following example uses the heads program.  If you don't have the heads program, you can download it from within Stata by typing findit heads (see How can I use the findit command to search for programs and get additional help? for more information about using findit).
The heads program can be used to produce results simulating those shown in Figure 4.1, as shown below. First start the heads program by typing
heads
You can then change the number of trials to 1000. When we ran this, our simulated 1000 coin flips looked like the graph below.  Every time you run the heads program, it generates a new set of 1000 flips, so the results come out different every time.   However if you do a large number of coin flips, you will notice that in the long run the proportion of heads always converges on .5.  Try this on your own and see for yourself.

Go ahead and click on quit.

The heads program can be used to simulate the results in Example 4.2. We now type heads, save  to save the coin tosses in a Stata file. In the first example we simulate the 4040 coin tosses by Buffon (who got 2048 heads). Here is the graph we get.

We then click quit. Then we use the tabulate command to see that our simulation, we got 2051 heads, or a proportion of .492. Every time we do this, we get a different proportion of heads.  Try this for yourself and see how many heads you get.

tabulate heads

# heads out |
of 1 tossed |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |       2051       50.77       50.77
          1 |       1989       49.23      100.00
------------+-----------------------------------
      Total |       4040      100.00

summ heads

Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
   heads |    4040    .4923267    .500003          0          1  
Next, we simulate the 24,000 coin tosses by Karl Pearson, and we got 12058 heads (compared to the 12,012 heads that Pearson got).
heads , save
tabulate heads
# heads out |
of 1 tossed |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |     11,942       49.76       49.76
          1 |     12,058       50.24      100.00
------------+-----------------------------------
      Total |     24,000      100.00

summ heads
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       heads |     24000    .5024167    .5000046          0          1

Next, we simulate the 10,000 coin tosses by John Kerrich, and we got 4972 heads (compared to the 5067 heads that Kerrich got).

heads2 1000, save 
tab heads

# heads out |
of 1 tossed |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |       5028       50.28       50.28
          1 |       4972       49.72      100.00
------------+-----------------------------------
      Total |      10000      100.00

summarize heads

Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
   heads |   10000       .4972   .5000172          0          1    

We can use the heads program to simulate the results shown in Figure 4.8 and Example 4.20, which shows the probability histogram for 4 tosses of a coin.  We can do 1000 coin tosses and graph the probabilities and compare those results to Figure 4.8, as illustrated below.  When you try this, you will probably get similar (but not exactly the same) results.
Note that 1000 is the number of tosses, and .5 is the probability of a head, and 4 is the number of coins tossed. We use the save option to save the results.
heads ,  save
We then quit and graph the results below.
histogram heads, discrete
We can show the probability distribution using the tabulate command and compare this to example 4.20.  As you can see, the probabilities we found (from the Percent column) are quite similar to the expected percentages shown in example 4.20.
tab heads
# heads out |
of 4 tossed |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |         54        5.40        5.40
          1 |        235       23.50       28.90
          2 |        397       39.70       68.60
          3 |        244       24.40       93.00
          4 |         70        7.00      100.00
------------+-----------------------------------
      Total |      1,000      100.00
We can get the mean of the distribution with the summarize command. The expected mean is 2.0, and ours is very close and 2.041.
summarize heads
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       heads |      1000       2.041     .987063          0          4

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.