The R function rbinom
(on-line
help)
simulates binomial random variables (Chapter 12 in the textbook).
At sample size 1, the binomial is a biased coin flip
with success
probability p
, which we set to be 1 / 2 here by default.
The command x <- rbinom(n, 1, p)
makes x
a sequence
of length n
of zeros and ones, the ones having probability
p
and the zeros having probability 1 - p
.
The first plot is just like Figure 9.1 in the textbook except our random numbers are not exactly the same as their random numbers. In fact, if you do the plot over and over, you get a different random pattern every time.
The second plot shows the actual numbers, not averages. A point with
horizontal coordinate t (number of tosses
)
has vertical coordinate that is the number of successes (heads
)
in the first t tosses minus the expected number,
which is t p.
The first plot is very boring if n
is large.
The path always converges to the expected proportion (horizontal line).
The second plot is more interesting and contains a large part of what we will learn in the probability section of the course (Chapters 9–12 in the book).
The pattern in the second plot is very irregular, and it is hard to see
what is going on. But if you redo the plot over and over, you will see
that the vertical is about the same each time. So while the details of
the pattern are haphazard, the number of heads minus the expected number
of heads is about the same size every time you redo the computer
experiment
.