%% ================================================================
\newpage
\section{The coin-flipping experiments}

This section is an extended worked example, tying together various concepts.
We apply the Central Limit Theorem first to repeated tosses of a single coin,
then to repeated collections of tosses.

%% ----------------------------------------------------------------
\subsection{Single coin flips}

The first experiment is tossing a single coin which has probability $p$ of
heads.  Then $\Omega=\{\mathrm{T},\mathrm{H}\}$.  Let $X$ be the random
variable which takes value $0$ for tails and $1$ for heads.  As discussed in
section \ref{sec:drv_catalog}, $X$ has the Bernoulli distribution with
parameter $p$.  I will allow $p$ to vary throughout this section, although I
will focus on $p=0.5$ and $p=0.6$.  Recall that $X$ has mean
$$
	\mu_X=p.
$$
(In section $\ref{sec:drv_catalog}$ we took $1$ for tails which is the opposite
convention from the one here.)
Its standard deviation is
$$
	\sigma_X = \sqrt{p(1-p)},
$$
which is $\sqrt{0.25}= 0.5$ and $\sqrt{0.24}\approx 0.4899$ for $p=0.5$ and
$p=0.6$, respectively.

Now flip the coin a large number $n$ of times --- say, $n=1000$ --- and count
the number of heads.  Using the notation of section \ref{sec:Sn_and_Xnbar}, the
number of heads is $S_n$.  There are two ways to look at this.

\begin{itemize}

	\item On one hand, from section \ref{sec:drv_catalog} we know that $S_n$ is
	geometric with parameters $p$ and $n$ --- if we think of the 1000 tosses as
	a single experiment.  (This is precisely what we will do in the next
	section.) The PMF of $S_n$ is the one involving binomial coefficients; we
	would expect
	$$
		\mu_{S_n}=np
	$$
	i.e. $500$ or $600$ and
	$$
		\sigma_{S_n}=\sqrt{np(1-p)}
	$$
	which is $\sqrt{250}\approx 15.81$ or $240\approx 15.49$ for $p=0.5$ and
	$p=0.6$, respectively.

	\item On the other hand, the Central Limit Theorem (section \ref{sec:CLT})
	says that as $n$ increases, the distribution of $S_n$ begins to look
	normal.  This PDF involves the exponential function, as shown in section
	\ref{sec:Sn_and_Xnbar}.  The mean of the sums $S_n$ is
	$$
		\mu_{S_n} \approx n\mu_X=np
	$$
	(again 500 or 600).  The standard deviation of those sums about the means
	500 or 600 is
	$$
		\sigma_{S_n} \approx \sqrt{n} \sigma_X = \sqrt{n p(1-p)}
	$$
	which are again 15.81 and 15.49, respectively.
\end{itemize}

Note that the binomial PMF and the normal PDF are \emph{not} the same, even
though they produced the same means and standard deviations:  the geometric
random variable has an integer-valued PMF; $P(499.1 \le S_n \le 499.9)=0$ and
likewise $S_n$ can never be anything out of the range from 0 to 1000.  The
normal PDF, on the other hand, gives $P(499.1 \le S_n \le 499.9)\ne 0$ since we
are taking area under a curve where the function is non-negative.  Since the
output of the exponential function is never 0, the normal PDF gives non-zero
(although admittedly very, very tiny) probability of $S_n$ being less than 0 or
greater than 1000.  (See also \cite{bib:mm} for some very nice plots.)

Now we can ask about fairness of coins.  The probabilistic point of view is to
fix $p$ and ask about the probabilities of various values of $S_n$.  If the
coin is fair, what are my chances of flipping anywhere between 470 and 530
heads?  Using the geometric PMF is a mess --- in fact, my calculator can't
compute ${1000\choose 470}$ without overflow.  Using the normal approximation,
though, is easy.  I asked my TI-83 to integrate its \texttt{normalpdf}
function, with $\mu=500$ and $\sigma=15.81$, from 470 to 530 and it told me
0.9422.

How surprised should I be if I toss 580 heads?  The \plainidx{standardization},
which \cite{bib:mm} call the \bfidx{$z$-score}, of $S_n$ is (definition
\ref{defn:stdizn})
$$
	z = \frac{S_n-\mu_{S_n}}{\sigma_{S_n}}.
$$
This counts how many standard deviations away from the mean a given observation
is.  I have $80/15.81 \approx 5.06$ so this result is more than five standard
deviations away from the mean.  I would not think the coin is fair.  If I re-do
that computation with $p=0.6$, $\mu_{S_n}=600$, and $\sigma_{S_n}=15.49$, I get
a $z$-score of $-1.29$ which is not surprising if the coin has parameter
$p=0.6$.

The point of view used in statistics is to start with the data, and from that
try to estimate with various levels of confidence what the parameters are.
Given a suspicious coin, what experiments would we have to run to be 95\% sure
that we've find out what the coin's parameter $p$ is, to within, say, $\pm
0.01$?  Continuing the example from section \ref{sec:confidence_intervals},
we ask for $\veps=0.01$.  We had
$$
	\veps = \frac{1.96\sigma_X}{\sqrt{n}}
$$
so, setting $\veps=0.01$ and solving for $n$, we have
$$
	n = \left(\frac{1.96\sigma_X}{0.01}\right)^2
	= \left(\frac{1.96\sqrt{p(1-p)}}{0.01}\right)^2
	\approx 38416 \; p(1-p).
$$
Now, $p(1-p)$ has a maximum at $p=0.5$, for which $n=9604$, so that many flips
would determine $p$ to within $\pm 0.01$ with 95\% confidence.  Re-doing the
arithmetic with $0.001$ in place of $0.01$ gives $n=960,400$.  Generalizing, we
see that each additional decimal place costs 100 times as many runs.

%% ----------------------------------------------------------------
\subsection{Batches of coin flips}

The second experiment is 1000 tosses of a coin where each coin has probability
$p$ of heads.  (Or, think of simultaneously tossing 1000 identical such coins.)
Then $\#\Omega = 2^{1000}\approx 10^{301}$.  Let $Y:\Omega\to\R$ be the random
variable which counts the number of heads.  This is an example where the random
variable is far easier to deal with than the entire sample space (which is
huge).

As discussed in section \ref{sec:drv_catalog}, $Y$ has the binomial
distribution with parameters $p$ and $n=1000$.  Recall from the previous
section that that $Y$ has mean
$$
	\mu_Y=1000p,
$$
e.g. 500 or 600.  Likewise, its standard deviation is
$$
	\sigma_Y = \sqrt{1000p(1-p)},
$$
which is $\sqrt{250}\approx 15.81$ or $240\approx 15.49$ for $p=0.5$ and
$p=0.6$, respectively.

Let $\YNbar$ be the average of $N$ runs of this experiment --- that is, the
\bfidx{sample mean}.  The Central Limit Theorem (section \ref{sec:CLT}) says
that as $N$ increases, the distribution of $\YNbar$ begins to look normal.  The
mean of the sample means is
$$
	\mu_{\YNbar} \approx \mu_Y=1000p,
$$
which is again 500 or 600.  The standard deviation of the sample means is
$$
	\sigma_{\YNbar} \approx \frac{\sigma_Y}{\sqrt{N}} = \frac{\sqrt{n p(1-p)}}{N}.
$$
This is $15.81/\sqrt{N}$ or $15.49/\sqrt{N}$, respectively.

%xxx make an EPS.
%\includegraphics{figures/cf1k.jpg}

Here is the (crucial) interpretation: Given the parameters $p$ and $n$ of $Y$,
there is a true population mean and a true population standard deviation.  If
$p=0.5$, then $\sigma_Y\approx 15.81$ and even if we're on the three millionth
iteration of the flip-1000-coins experiment, it's still going to be quite
likely as ever that we'll get a 514 or a 492 and so on.  If we don't know the
true $p$ of the coins then, while the true population mean $\mu_Y$ and the true
population standard deviation $\sigma_Y$ exist, we don't know what they are.
All we have is some suspicious-looking identical coins and our laboratory
equipment.  As we run and re-run the flip-1000-coins experiment, the following
happens:

	\begin{itemize}
	\item The sample mean $\mu_\YNbar$, for increasingly larger $N$, will
		approach the population mean $\mu_Y$.
	\item The variations \emph{of the sample mean} will decrease.  We might say
		the error in our estimate of the population mean is shrinking.
	\item The population standard deviation appears in the above formulas
		via its effects on the standard deviation \emph{of the sample mean}.
	\item Nothing we have done so far has given us a reliable connection
		between the sample standard deviation and the population standard
		deviation.  We can guess that the sample standard deviation approaches
		the population standard deviation, but (in this course) we have not
		developed any information about the error in that computation.
	\end{itemize}

Here is a numerical example.  I run (simulated on a computer) the
flip-1000-coins experiment 400 times.  The first time I get 482, so the sample
mean is 482.  The second time I get 521, so the sample mean is $(482+521)/2$.
The third time I get 494, so the sample mean is $(482+521+494)/3$ and so on.

Here is $p=0.5$:

\begin{tabular}{|r|r|l|c|}
\hline $N$ & $Y$ & $\YNbar$ & Sample std. dev. \\
\hline
\hline   1 & 482 & 482.000 &  N/A \\
\hline   2 & 521 & 501.500 & 27.577 \\
\hline   3 & 494 & 499.000 & 19.975 \\
\hline   4 & 512 & 502.250 & 17.557 \\
\hline   5 & 485 & 498.800 & 17.050 \\
\hline   6 & 507 & 500.167 & 15.613 \\
\hline $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
\hline 395 & 493 & 500.258 & 16.163 \\
\hline 396 & 505 & 500.270 & 16.144 \\
\hline 397 & 501 & 500.272 & 16.124 \\
\hline 398 & 494 & 500.256 & 16.107 \\
\hline 399 & 501 & 500.258 & 16.086 \\
\hline 400 & 474 & 500.192 & 16.120 \\
\hline
\end{tabular}

Here is $p=0.6$:

\begin{tabular}{|r|r|l|c|}
\hline $N$ & $Y$ & $\YNbar$ & Sample std. dev. \\
\hline
\hline   1 & 616 & 616.000 &  N/A \\
\hline   2 & 584 & 600.000 & 22.627 \\
\hline   3 & 620 & 606.667 & 19.732 \\
\hline   4 & 617 & 609.250 & 16.919 \\
\hline   5 & 583 & 604.000 & 18.775 \\
\hline   6 & 613 & 605.500 & 17.190 \\
\hline $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
\hline 395 & 608 & 599.484 & 17.031 \\
\hline 396 & 615 & 599.523 & 17.027 \\
\hline 397 & 609 & 599.547 & 17.013 \\
\hline 398 & 607 & 599.565 & 16.995 \\
\hline 399 & 604 & 599.576 & 16.975 \\
\hline 400 & 611 & 599.605 & 16.964 \\
\hline
\end{tabular}

