\documentclass[twoside,12pt]{article}

\setlength{\textwidth}{6.5 in}
\setlength{\textheight}{8.5 in}
\setlength{\topmargin}{0 in}
\setlength{\headheight}{0.25 in}
\setlength{\headsep}{0.25 in}
\setlength{\evensidemargin}{0 in}
\setlength{\oddsidemargin}{0 in}
\setlength{\parskip}{1ex}

\usepackage{graphics}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{latexsym}

\newenvironment{comment}[1]{}{}
\newenvironment{proof}{\noindent {\em Proof.}}{$\Box$}

\newtheorem{theorem}{Theorem}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}

\newcommand{\beqn}{\begin{eqnarray*}}
\newcommand{\eeqn}{\end{eqnarray*}}
\newcommand{\pr}[1]{\Pr\left\{#1\right\}}
\newcommand{\ex}[1]{\text{Ex}\left[#1\right]}

\begin{document}

\begin{center}
{\Large Lecture 7 - Expected Value} \\
6.042 - February 27, 2003 \\
\bigskip
\end{center}

\footnotetext{Copyright \copyright 2003, Charles Leiserson, Srinivas
Devadas, Eric Lehman.  All rights reserved.}

This lecture concerns the {\em expected value} of a random variable.
Informally, this is the average value that a random variable takes on
when an experiment is repeated many times.  Knowing an expected value
can be really handy, because this one number conveys a lot of
information about a random variable.  For example, suppose that we
take the quiz 1 score of a 6.042 student selected uniformly at random.
The expected value of that random variable is just the class average.
If you know that one number, you have a pretty good idea how you did
relative to the class as a whole, even though you don't know the
distribution of scores in full detail.

\newcommand{\sspace} S
\newcommand{\range}[1]{\text{Range}(#1)}

\section{Two Equivalent Definitions}

There are two equivelent definitions of the expected value of a random
variable.  Sometimes one is more convenient, sometimes the other.  

\begin{definition}
The {\em expected value} of a random variable $R$ over a sample space
$S$ is denoted $\ex{R}$ and defined by:

\beqn
\ex{R} & = & \sum_{x \in S} R(x) \cdot \pr{x}
\eeqn

\noindent or, equivalently, by:

\beqn
\ex{R} & = & \sum_{r \in R(S)} r \cdot \pr{R = r}
\eeqn
\end{definition}

\noindent The expression $R(S)$, used above, is shorthand for the set
$\{R(x) \mid x \in S\}$; that is, the set of all the different values
that the random variable $R$ takes on over the whole sample space $S$.

The expected value of a random variable is also sometimes called the
{\em expectation}, the {\em average}, or the {\em mean}.

\subsection{The Expected Value of One Die}

Suppose we roll a fair, six-sided die.  Let the random variable $R$ be
the number that comes up.  We can compute the expected value of $R$
using either definition; in this case, they amount to about the same
thing.  Let's use the second one:

\begin{eqnarray*}
\ex{R}  & = &   \sum_{i=1}^6 i \cdot \pr{R = i} \\
        & = &	1 \cdot \frac{1}{6} + 2 \cdot \frac{1}{6} +
        	3 \cdot \frac{1}{6} + 4 \cdot \frac{1}{6} +
		5 \cdot \frac{1}{6} + 6 \cdot \frac{1}{6} \\
	& = &	\frac{7}{2}
\end{eqnarray*}

\noindent Thus, the expected value of the number rolled on a fair die
is $\frac{7}{2}$.  This means that if you roll a die many times, you
should expect the average value to be about $\frac{7}{2}$.  Obviously,
you should not expect the value $\frac{7}{2}$ ever to come up on an
individual roll!

\subsection{Equivalence of the Two Definitions}

Let's prove that the two definitions of expected value given at the
beginning of this section actually are equivalent.  In the first
definition, there is one term in the summation for each outcome in the
sample space.  By grouping together outcomes on which the random
variable $R$ takes the same value, we get the second definition.  Here
are the gory details:

\beqn
\sum_{x \in S} R(x) \cdot \pr{x}
	& = & \sum_{r \in R(S)} \sum_{x \in S: R(x) = r} R(x) \cdot \pr{x} \\
	& = & \sum_{r \in R(S)} \sum_{x \in S: R(x) = r} r \cdot \pr{x} \\
	& = & \sum_{r \in R(S)} r \cdot
		\left( \sum_{x \in S: R(x) = r} \pr{x} \right)\\
	& = & \sum_{r \in R(S)} r \cdot \pr{R = r}
\eeqn

\noindent Here the notation $x \in S: R(x) = r$ refers to all outcomes
$x$ in the sample space $S$ such that $R(x) = r$.  After the first
step, we're summing up exactly the same terms as before, but indexing
them a different way.  The second step uses the fact that $R(x) = r$
for all terms in the inner summation.  In the third step, we pull $r$
out of the inner sum.  This inner sum is then the very definition of
the probability of the event $R = r$, so we replace it by $\pr{R = r}$
in the last step.  Since one can equally well reason from the last
expression back to the first, the two definitions are equivalent.

\section{Chuck-a-Luck}

Here's yet another fun 6.042 game!  You pick a number between 1 and 6.
Then you roll three fair, independent dice.

\begin{itemize}

\item If your number never comes up, then you lose a dollar.

\item If your number comes up once, then you win a dollar.

\item If your number comes up twice, then you win two dollars.

\item If your number comes up three times, you win three dollars!

\end{itemize}

\noindent Is this a good game to play?  That is, if you play many
times, do you expect to win money overall?

We can formulate these questions mathematically by defining a random
variable $R$, which denotes the amount of money that you win in a game
of Chuck-a-Luck.  Then, for example, we have $R = 1$ when your number
comes up once and $R = -1$ when it never comes up.  Asking whether you
should expect to win money playing Chuck-a-Luck for a long time is
equivalent to asking whether the expectation of $R$ is positive; that
is, is $\ex{R} > 0$?

\begin{figure}[htpb]
\centerline{\resizebox{!}{3.0in}{\includegraphics{lec7-tree.eps}}}
\caption{Tree diagram for Chuck-a-Luck.}
\label{fig:lec7-tree}
\end{figure}

Tree diagrams can often be useful for computing expectations as well
as probabilities.  Figure~\ref{fig:lec7-tree} shows a tree diagram for
Chuck-a-Luck.  The branches marked with probability $\frac{1}{6}$
correspond to die rolls where your number comes up; those marked
$\frac{5}{6}$ correspond to rolls where your number does not come up.
In addition to noting the probability of each outcome, we've indicated
the value of the random variable $R$ as well.  This is precisely the
information we need to compute the expected value of $R$:

\beqn
\ex{R}	& = &	\sum_{r \in R(S)} r \cdot \pr{R = y} \\
	& = &	-1 \cdot \left(\frac{125}{216}\right) +
		1 \cdot \left(3 \cdot \frac{25}{216}\right) +
		2 \cdot \left(3 \cdot \frac{5}{216}\right) +
		3 \cdot \left(\frac{1}{216}\right) \\
	& = &	- \frac{17}{216}
\eeqn

\noindent If you play Chuck-a-Luck many times, you should expect to
lose about 8 cents per game!

\section{Linearity of Expectation}

Expectation obeys a simple, but remarkably powerful rule called {\em
linearity of expectation}.  This rule is stated and proved below.

\begin{theorem}
For all random variables $X$ and $Y$ and all real numbers $a$ and $b$:

\beqn
\ex{aX + bY} & = & a \ex{X} + b \ex{Y}
\eeqn
\end{theorem}

\begin{proof}
Let $S$ be the sample space.  The left side can be transformed into
the right side as follows:

\beqn
\ex{a X + a Y}
	& = &	\sum_{s \in S} (a X(s) + b Y(s)) \cdot \pr{s} \\
	& = &	\sum_{s \in S} a X(s) \cdot \pr{s} +
		\sum_{s \in S} b Y(s) \cdot \pr{s} \\
	& = &	a \sum_{s \in S} X(s) \cdot \pr{s} +
		b \sum_{s \in S} Y(s) \cdot \pr{s} \\
	& = &	a \ex{X} + b \ex{Y}
\eeqn

The first step uses the definition of expectation.  In the second
step, we split the single sum into two.  Then we pull the constants
$a$ and $b$ out of their summations and finish by applying the
definition of expectation again.
\end{proof}

Linearity of expectation is most often used to compute the expected
value of a sum of random variables.  In that case, the constants $a$
and $b$ are both equal to 1, and the theorem says that the expectation
of a sum of random variables is the sum of their expectations:

\beqn
\ex{X + Y} & = & \ex{X} + \ex{Y}
\eeqn

The great thing about linearity of expectation is that {\em no
independence is required}.  This is really useful, because dealing
with independence is a pain, and we often need to work with random
variables that are not independent.

\subsection{Expected Value of Two Dice}

What is the expected value of the sum of two fair dice?

Let the random variable $X$ be the number on the first die, and let
$Y$ be the number on the second.  We observed earlier that the
expected value of one die is $\frac{7}{2}$.  We can find the expected
value of the sum using linearity of expectation:

\beqn
\ex{X + Y}
	& = &	\ex{X} + \ex{Y} \\
	& = &	\frac{7}{2} + \frac{7}{2} \\
	& = &	7
\eeqn

Notice that we did {\em not} have to assume that the two dice were
independent.  The expected sum of two dice is 7, even if they are
glued together!  (This is provided that gluing does not change weights
to make the individual dice unfair.)

Proving that the expected sum is 7 with a tree diagram would be hard;
there are 36 cases.  And if we did not assume that the dice were
independent, the job would be a nightmare!

\subsection{The Hat-Check Problem}

There is a dinner party where $n$ men check their hats.  The hats are
mixed up during dinner, so that afterward each man receives a random
hat.  In particular, each man gets his own hat with probability $1/n$.
What is the expected number of men who get their own hat?

Without linearity of expectation, this would be a very difficult
question to answer.  We might try the following.  Let the random
variable $R$ be the number of men that get their own hat.  We want to
compute $\ex{R}$.  By the definition of expectation, we have:

\beqn
\ex{R} & = & \sum_{k=0}^{n} k \cdot \pr{R = k}
\eeqn

Now we are in trouble, because evaluating $\pr{R = k}$ is a mess and
we then need to substitute this mess into a summation.  Furthermore,
to have any hope, we would need to fix the probability of each
permutation of the hats.  For example, we might assume that all
permutations of hats are equally likely, even though this is not in
the statement of the problem.  As a result, our solution would apply
only in this restricted circumstance.

So let's use linearity of expectation.  As before, let the random
variable $R$ be the number of men that get their own hat.  The first
trick is to express $R$ as a sum of indicator variables.  In
particular, let $R_i$ be an indicator for the event that the $i$-th
man gets his own hat.  That is, $R_i = 1$ is the event that he gets
his own hat, and $R_i = 0$ is the event that he gets a wrong hat.  The
number of men that get their own hat is the sum of these indicators:

\beqn
R & = & R_1 + R_2 + \cdots + R_n
\eeqn

These indicator variables are {\em not} mutually independent.  For
example, if $n-1$ men all get their own hats, then the last man is
certain to receive his own hat.  So $R_n$ is not independent of the
other indicator variables.  But, since we plan to use linearity of
expectation, we \emph{don't care} whether the indicator variables are
independent.  No matter what, we can take the expected value of both
sides of the equation above and apply linearity of expectation:

\beqn
\ex{R}	& = &	\ex{R_1 + R_2 + \ldots + R_n} \\
	& = &	\ex{R_1} + \ex{R_2} + \ldots + \ex{R_n}
\eeqn

Now our problem is to compute the expected value of each indicator
variable $R_i$.  To do that, let's go back to the definition of
expectation, bearing in mind that an indicator variable takes on only
the values 0 and 1.

\beqn
\ex{R_i}
	& = &	\sum_{r \in R_i(S)} r \cdot \pr{R_i = r} \\
	& = &	0 \cdot \pr{R_i = 0} + 1 \cdot \pr{R_i = 1} \\
	& = &	\pr{R_i = 1}
\eeqn

\noindent The calculation above illustrates a general rule: {\em the
expectation of an indicator is equal to the probability that the
indicator is 1}.

In this case, the indicator $R_i$ is equal 1 precisely when the $i$-th
man gets his hat back.  According to the problem statement, this
happens with probability $1/n$.  Therefore, we have:

\beqn
\ex{R}	& = &	\ex{R_1} + \ex{R_2} + \ldots + \ex{R_n} \\
	& = &	\pr{R_1 = 1} + \pr{R_2 = 1} + \ldots + \pr{R_n = 1} \\
	& = &	\frac{1}{n} + \frac{1}{n} + \ldots + \frac{1}{n} \\
	& = &	1
\eeqn

\noindent On average, exactly one man gets his own hat back.
Amazingly, this is true whether only two men went to dinner or a
thousand!

\section{A Bar Game}

Three guys in a bar are playing a game.  First, each player puts two
dollars into the pot.  Then each player secretly writes ``heads'' or
``tails'' on a napkin.  The bartender then flips a fair coin.  The
players reveal their napkins and the pot is divided evenly among all
players that correctly predicted the outcome of the coin toss.  If no
one predicted correctly, everyone takes their money back.

\begin{figure}[htpb]
\centerline{\resizebox{!}{3.0in}{\includegraphics{lec7-tree2.eps}}}
\caption{Tree diagram for the bar game.}
\label{fig:lec7-tree2}
\end{figure}

On first inspection, this appears to be a perfectly fair game.  Since
the roles of the three players are symmetric, there is no way for one
player to get a special advantage.

But let's look more closely.  Let $x$, $y$, and $z$ be the
probabilities that the first, second, and third players choose
``heads''.  The expected payoff for the first player is computed via
the tree diagram in Figure~\ref{fig:lec7-tree2} and some routine, but
messy algebra.  The conclusion is:

\beqn
\ex{\text{player 1 payoff}}
	& = &	x (1 - y - z) + 2 y z - \frac{y + z}{2}
\eeqn

This equation requires some interpretation.  Initially, suppose that
the second and third players are both equally likely to predict heads
and tails ($y = z = \frac{1}{2}$).  Then the expression above is zero,
meaning that the first player breaks even.

But now suppose that the second player always guesses heads ($y = 1$)
and the third player always guesses tails ($z = 0$) or vice versa.
Either way, we have $y + z = 1$ and $yz = 0$.  In this case, the
expected payoff for the first player is $- \frac{1}{2}$.  In other
words, if the second and third players secretly collude by always
betting oppositely, the first player will lose an average of fifty
cents per game, no matter what he does!

\end{document}

