\chapter{Counting and Probability}

\section{Counting}

\begin{enumerate}

\item[C.1{-}1] {How many $k$-substrings does an $n$-string have? (Consider
identical $k$-substrings at different positions to be different.) How many
substrings does an $n$-string have in total?}

\begin{framed}
For every position $i$ of the $n$-string, $i = 1, \dots, n - k + 1$, there is
one $k$-substring the starts at $i$ and ends at $i + k - 1$. Thus, the number of
$k$-substrings in a $n$-string is
\[
  \sum_{i = 1}^{n - k + 1} 1 = n - k + 1.
\]

Thus, the number of substrings (of all sizes) in an $n$-string is
\begin{equation*}
\begin{aligned}
  \sum_{k = 1}^{n} n - k + 1 &= n^2 + n - \sum_{k = 1}^{n} {k}\\
                             &= n^2 + n - \frac{n (n + 1)}{2}\\
                             &= n (n + 1) - \frac{n (n + 1)}{2}\\
                             &= \frac{n (n + 1)}{2}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.1{-}2] {An $n$-input, $m$-output \textbf{\emph{boolean function}} is
a function from $\{\texttt{TRUE}, \texttt{FALSE}\}^n$ to
$\{\texttt{TRUE}, \texttt{FALSE}\}^m$. How many $n$-input, $1$-output boolean
functions are there? How many $n$-input, $m$-output boolean functions are
there?}

\begin{framed}
We can view the number of possible inputs of size $n$ as the number of binary
$n$-strings, which is $2^n$.

Now, consider a single-valued function from $\{\texttt{TRUE},
\texttt{FALSE}\}^n$ to $\{\texttt{TRUE}\}$. In this case, the number of
possible functions is the number of possible inputs, which is $2^n$. Since an
$1$-output boolean function has two possible output values, each of the $2^n$
functions we referred in the case of a single-valued function now has two ways
to pick the output value. We can view this number as the number of binary
$2^n$-strings, which is $2^{2^n}$. As for an $n$-output function, each of the
$2^n$ functions we referred in the case of a single-valued function now has
$2^m$ ways to pick the output value. Thus, there are $({2^m})^{2^n}$ of those.
\end{framed}

\item[C.1{-}3] {In how many ways can $n$ professors sit around a circular
conference table? Consider two seatings to be the same if one can be rotated to
form the other.}

\begin{framed}
For two seatings to be different from each other, the ordering of professors in
each seating needs to be different. This number can be viewed as the number
of permutations of a set $n$ elements, which is $n!$. However, note that for
each permutation that starts with professor $k$, $1 \le k \le n$, there are
$n - 1$ other permutations that are just a rotation of it. For instance, the
seatings $\{2, 3, 1\}$ and $\{3, 1, 2\}$ are a rotation of $\{1, 2, 3\}$. Thus,
the number of different seatings can be viewed as fixing the seat of the first
professor and computing the number of permutations of the remaining $n - 1$
professors, which is $(n - 1)!$.
\end{framed}

\item[C.1{-}4] {In how many ways can we choose three distinct numbers from the
set $\{1, 2, \dots, 99\}$ so that their sum is even?}

\begin{framed}
The set has $50$ odd numbers and $49$ even numbers. For the sum be even, we have
to choose three even numbers or one even and two odds. For the case with three
even numbers, there are $49!/(3! \cdot (49 - 3)!) = 18424$ ways of choosing $3$
distincts numbers among the 49 even numbers. As for the case with one even and
two odds, there are 49 ways to choose one even number and
$50!/(2! \cdot (50 - 2)!) = 1225$ ways of choosing $2$ distincts numbers among the
50 odd numbers. Thus, there are $18424 + 49 \cdot 1225 = 78449$ ways to get an
even sum.
\end{framed}

\newpage

\item[C.1{-}5] {Prove the identity
\[
  \binom{n}{k} = \frac{n}{k} \binom{n - 1}{k - 1}
\]
for $0 < k \le n$.
}

\begin{framed}
\begin{equation*}
\begin{aligned}
  \binom{n}{k} &= \frac{n!}{k! \cdot (n - k)!}\\
               &= \frac{n \cdot (n - 1)!}{k \cdot (k - 1)! \cdot (n - k)!}\\
               &= \frac{n}{k} \frac{(n - 1)!}{(k - 1)! \cdot ((n - 1) - (k - 1))!}\\
               &= \frac{n}{k} \binom{n - 1}{k - 1}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.1{-}6] {Prove the identity
\[
  \binom{n}{k} = \frac{n}{n - k} \binom{n - 1}{k}
\]
for $0 \le k < n$.
}

\begin{framed}
\begin{equation*}
\begin{aligned}
  \binom{n}{k} &= \frac{n!}{k! \cdot (n - k)!}\\
               &= \frac{n \cdot (n - 1)!}{k! \cdot (n - k) \cdot (n - k - 1)!}\\
               &= \frac{n}{n - k} \frac{(n - 1)!}{k! \cdot ((n - 1) - k)!}\\
               &= \frac{n}{n - k} \binom{n - 1}{k}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.1{-}7] {To choose $k$ objects from $n$, you can make one of the objects
distinguished and consider whether the distinguished object is chosen. Use this
approach to prove that
\[
  \binom{n}{k} = \binom{n - 1}{k} + \binom{n - 1}{k - 1}.
\]
}

\begin{framed}
Let $S = \{s_1, s_2, \dots, s_{n - 1}\}$ and $s_0$ the
distinguished element. To choose $k$ from the $n$ elements, we have to consider
two cases:
\begin{enumerate}
  \item If $s_0$ is selected, it will be necessary to choose the $k - 1$
    remaining elements from $S$. There are $\binom{n - 1}{k - 1}$ combinations.
  \item If $s_0$ is not selected, it will be necessary to choose the $k$
    remaining elements from $S$. There are $\binom{n - 1}{k}$ combinations.
\end{enumerate}
Adding the above together, we have
\begin{equation*}
\begin{aligned}
  \binom{n - 1}{k - 1} + \binom{n - 1}{k}
  &= \frac{(n - 1)!}{(k - 1)! \cdot (n - k)!} + \frac{(n - 1)!}{k! \cdot (n - k - 1)!}\\
  &= \frac{k \cdot (n - 1)!}{k! \cdot (n - k)!} + \frac{(n - k) \cdot (n - 1)!}{k! \cdot (n - k)!}\\
  &= \frac{(k + n - k) \cdot (n - 1)!}{k! \cdot (n - k)!}\\
  &= \frac{n!}{k! \cdot (n - k)!}\\
  &= \binom{n}{k}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.1{-}8] {Using the result of Exercise C.1-7, make a table for
$n = 0, 1, \dots, 6$ and $0 \le k \le n$ of the binomial coefficients
$\binom{n}{k}$ with $\binom{0}{0}$ at the top, $\binom{1}{0}$ and $\binom{1}{1}$
on the next line, and so forth. Such a table of binomial coefficients is called
\textbf{\emph{Pascal's triangle}}.}

\begin{framed}
The table with binomials
\begin{gather*}
  \binom{0}{0}\\
  \binom{1}{0} \quad \binom{1}{1}\\
  \binom{2}{0} \quad \binom{2}{1} \quad \binom{2}{2}\\
  \binom{3}{0} \quad \binom{3}{1} \quad \binom{3}{2} \quad \binom{3}{3}\\
  \binom{4}{0} \quad \binom{4}{1} \quad \binom{4}{2} \quad \binom{4}{3} \quad \binom{4}{4}\\
  \binom{5}{0} \quad \binom{5}{1} \quad \binom{5}{2} \quad \binom{5}{3} \quad \binom{5}{4} \quad \binom{5}{5}\\
  \binom{6}{0} \quad \binom{6}{1} \quad \binom{6}{2} \quad \binom{6}{3} \quad \binom{6}{4} \quad \binom{6}{5} \quad \binom{6}{6}
\end{gather*}
Using the above table and the result of C.1-7, we have the Pascal's triangle
\begin{gather*}
  1\\
  1 \quad 1\\
  1 \quad 2 \quad 1\\
  1 \quad 3 \quad 3 \quad 1\\
  1 \quad 4 \quad 6 \quad 4 \quad 1\\
  1 \quad 5 \quad 10 \quad 10 \quad 5 \quad 1\\
  1 \quad 6 \quad 15 \quad 20 \quad 15 \quad 6 \quad 1
\end{gather*}
\end{framed}

\item[C.1{-}9] {Prove that
\[
  \sum_{i = 1}^{n} i = \binom{n + 1}{2}.
\]
}

\begin{framed}
We have
\begin{equation*}
\begin{aligned}
  \binom{n + 1}{2} &= \frac{(n + 1)!}{2! \cdot ((n + 1) - 2)!}\\
                   &= \frac{(n + 1) \cdot n \cdot (n - 1)!}{2 \cdot (n - 1)!}\\
                   &= \frac{n (n + 1)}{2}\\
                   &= \sum_{i = 1}^{n} i,
\end{aligned}
\end{equation*}
which also shows that the third Pascal's diagonal has the triangular numbers.
\end{framed}

\newpage

\item[C.1{-}10] {Show that for any integers $n \ge 0$ and $0 \le k \le n$, the
expression $\binom{n}{k}$ achieves its maximum value when $k = \floor{n/2}$ or
$k = \ceil{n/2}$.}

\begin{framed}
It follows from the Pascal's triangle
\begin{gather*}
  1\\
  1 \quad 1\\
  1 \quad 2 \quad 1\\
  1 \quad 3 \quad 3 \quad 1\\
  1 \quad 4 \quad 6 \quad 4 \quad 1\\
  1 \quad 5 \quad 10 \quad 10 \quad 5 \quad 1\\
  1 \quad 6 \quad 15 \quad 20 \quad 15 \quad 6 \quad 1\\
  \quad \quad \vdots \quad \quad
\end{gather*}

We can prove by induction. The base case, which occurs when $n = 0$,
holds since
\[
  \binom{n}{\floor{n/2}} = \binom{n}{\ceil{n/2}} = \binom{0}{0} = 1
\]
is maximum on row 0.  Now, assume it holds for $n$. Then, if $n + 1$ is even,
from Equation (C.3) we have
\begin{equation*}
\begin{aligned}
  \binom{n + 1}{\floor{\frac{n + 1}{2}}} = \binom{n + 1}{\ceil{\frac{n + 1}{2}}}
  &= \binom{n}{\left(\frac{n + 1}{2} - 1\right)} + \binom{n}{\left(\frac{n + 1}{2}\right)}\\
  &= \binom{n}{\left(\frac{n}{2} - \frac{1}{2}\right)} + \binom{n}{\left(\frac{n}{2} + \frac{1}{2}\right)} & \text{(since $n$ is odd)}\\
  &= \binom{n}{\floor{\frac{n}{2}}} + \binom{n}{\ceil{\frac{n}{2}}},
\end{aligned}
\end{equation*}
which shows that is also holds for $n + 1$ since
\[
  \binom{n}{\floor{\frac{n}{2}}} \text{ and } \binom{n}{\ceil{\frac{n}{2}}}
\]
are both maximum on row $n$. The proof is similar when $n + 1$ is odd.
\end{framed}

\newpage

\item[C.1{-}11] {($\star$) Argue that for any integers $n \ge 0$, $j \ge 0$,
$k \ge 0$, and $j + k \le n$,
\[
  \binom{n}{j + k} \le \binom{n}{j} \binom{n - j}{k}.
\]

Provide both an algebraic proof and an argument based on a method for choosing
$j + k$ items out of $n$. Give an example in which equality does not hold.
}

\begin{framed}
For any integers $a \ge 0, b \ge 0,$ and $a \ge b$, we have
\begin{equation*}
\begin{aligned}
  (a + b)! &=   \underbrace{(a + b) \cdot (a + b - 1) \cdot (a + b - 2) \cdots}_\text{$b$ times} a!\\
           &\ge \underbrace{b \cdot (b - 1) \cdot (b - 2) \cdots}_\text{$b$ times} a!\\
           &=   a! \cdot b!.
\end{aligned}
\end{equation*}

Using the above result, we have
\begin{equation*}
\begin{aligned}
  \binom{n}{j} \binom{n - j}{k} &=   \frac{n!}{j! \cdot (n - j)!} \frac{(n - j)!}{k! \cdot ((n - j) - k)!}\\
                                &=   \frac{n!}{j! \cdot k! \cdot ((n - j) - k)!}\\
                                &\ge \frac{n!}{(j + k)! \cdot (n - (j + k))!}\\
                                &=   \binom{n}{j + k}.
\end{aligned}
\end{equation*}

The expression on the left is the number of ways to choose an $(j + k)$-subset
of an $n$-set (which leaves the reamining $n - (j + k)$ elements). Thus, it is
a partition of the original $n$-set into subsets of cardinalities $(j + k)$ and
$n - (j + k)$. The right hand side has two factors: the first binomial
coefficient is the number of ways to choose a $j$-subset of an $n$-set (which
leaves the reamining $n - j$ elements); the second is the number of ways to
choose a $k$-subset from the remaining $n - j$ elements. Thus, it is a partition
of the original $n$-set into subsets of cardinalities $j, k$, and $n - (j + k)$.
Consider now that we choose the $n - (j + k)$ first, leaving behind the
remaining $j + k$ elements. There is precisely one way to choose an
$(j + k)$-subset out of the remaining $j + k$ elements. On the other hand, when
we first choose $j$ and then we choose $k$, if $j < j + k$, there are
\emph{at least} two ways to choose a $j$-subset from the $(j + k)$-subset and
precisely one way to choose a $k$-subset from the remaining $k$ elements. This
notion also applies to the algebraic proof, since
$(j + k)! = j! \cdot k \iff j = 0$ or $k = 0$.
Also note that while the left expression does not count any permutation of the
$(j + k)$-subsets (since it normalizes by $(j + k)!$), the right
expression, despite not counting permutations of each of the subsets
indepentently (since it normalizes by $j! \cdot k!$), it counts permutations of
two subsets together. For instance, let $A = \{a, b\}$. There is only one way to
choose 2 elements from $A$, which is $ab$. However, there are two ways to choose
one element and then another element from $A$, which are $ab$ and $ba$.

\end{framed}

\item[C.1{-}12] {($\star$) Use induction on all integers $k$ such that
$0 \le k \le n/2$ to prove inequality (C.6), and use equation (C.3) to extend it
to all integers $k$ such that $0 \le k \le n$.}

\begin{framed}
Skipped.
\end{framed}

\item[C.1{-}13] {($\star$) Use Stirling's approximation to prove that
\[
  \binom{2n}{n} = \frac{2^{2n}}{\sqrt{\pi n}} (1 + O(1/n)).
\]
}

\begin{framed}
Skipped.
% \begin{equation*}
% \begin{aligned}
%   \binom{2n}{n} &= \frac{(2n)!}{n! \cdot (2n - n)!}\\
%                 &= \frac{(2n)!}{(n!)^2}\\
%                 &= \frac{\sqrt{2 \pi 2n} \left(\frac{2n}{e}\right)^{2n} \left( 1 + \Theta\left(\frac{1}{2n}\right) \right)}
%                         {(\sqrt{2 \pi n} \left(\frac{n}{e}\right)^{n} \left( 1 + \Theta\left(\frac{1}{n}\right) \right))^2}\\
%                 &= \frac{\sqrt{2 \pi 2n} \left(\frac{2n}{e}\right)^{2n} \left( 1 + O\left(\frac{1}{n}\right) \right)}
%                         {2 \pi n \left(\frac{n}{e}\right)^{2n} \left( 1 + O\left(\frac{1}{n}\right) \right)^2}
% \end{aligned}
% \end{equation*}
\end{framed}

\item[C.1{-}14] {($\star$) By differentiating the entropy function $H(\lambda)$,
show that it achieves its maximum value at $\lambda = 1/2$. What is $H(1/2)$?}

\begin{framed}
Skipped.
\end{framed}

\item[C.1{-}15] {($\star$) Show that for any integer $n \ge 0$,
\[
  \sum_{k = 0}^{n} \binom{n}{k} k = n 2^{n - 1}.
\]
}

\begin{framed}
Skipped.
% \begin{equation*}
% \begin{aligned}
%   \sum_{k = 0}^{n} \binom{n}{k} k
%   &= \sum_{k = 1}^{n} \binom{n}{k} k\\
%   &= \sum_{k = 1}^{n} \frac{n!}{k! \cdot (n - k)!}\\
%   &= \sum_{k = 1}^{n} \frac{n \cdot (n - 1)!}{k \cdot (k - 1)! (n - k) \cdot (n - k - 1)!}
% \end{aligned}
% \end{equation*}
\end{framed}

\end{enumerate}

\newpage

\section{Probability}

\begin{enumerate}

\item[C.2{-}1] {Professor Rosencrantz flips a fair coin once. Professor
Guildenstern flips a fair coin twice. What is the probability that Professor
Rosencrantz obtains more heads than Professor Guildenstern?}

\begin{framed}
The sample space $\{\text{H}, \text{T}\}^3$ has size $2^3 = 8$. Since the only
event that satisfies the condition is $\{\text{HTT}\}$, the probability is $1/8$.
\end{framed}

\item[C.2{-}2] {Prove the \textbf{\emph{Boole's inequality}}: For any finite or
countably infinite sequence of events $A_1, A_2, \dots$,
\[
  \text{Pr}\{A_1 \cup A_2 \cup \cdots\} \le \text{Pr}\{A_1\} + \text{Pr}\{A_2\} + \cdots.
\]
}

\begin{framed}
From (C.13) we have
\[
\text{Pr}\{A_1 \cup A_2\} \le \text{Pr}\{A_1\} + \text{Pr}\{A_2\},
\]
which implies
\begin{equation*}
\begin{aligned}
  \text{Pr}\{A_1 \cup A_2 \cup \cdots\} &=   \text{Pr}\{A_1 \cup (A_2 \cup \cdots)\}\\
                                        &\le \text{Pr}\{A_1\} + \text{Pr}\{A_2 \cup (A_3 \cup \cdots)\}\\
                                        &\le \text{Pr}\{A_1\} + \text{Pr}\{A_2\} + \text{Pr}\{A_3 \cup (A_4 \cup \cdots)\}\\
                                        &\le \text{Pr}\{A_1\} + \text{Pr}\{A_2\} + \text{Pr}\{A_3\} \cdots.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.2{-}3] {Suppose we shuffle a deck of 10 cards, each bearing a distinct
number from 1 to 10, to mix the cards thoroughly. We then remove three cards,
one at a time, from the deck. What is the probability that we select the three
cards in sorted (increasing) order?}

\begin{framed}
Let $a < b < c$ denote the number of the three selected cards. There are $3!$
permutations of $\{a, b, c\}$ and $abc$ is the only one which is in sorted
order. Thus, the probability is $1/3! = 1/6$.
\end{framed}

\item[C.2{-}4] {Prove that
\[
  \text{Pr}\{A\;|\;B\} + \text{Pr}\{\overline{A}\;|\;B\} = 1.
\]
}

\begin{framed}
We have
\begin{equation*}
\begin{aligned}
  \text{Pr}\{B\} &= \text{Pr}\{(B \cap A) \cup (B \cap \overline{A})\}\\
                 &= \text{Pr}\{B \cap A\} + \text{Pr}\{B \cap \overline{A}\}\\
                 &= \text{Pr}\{A\} \text{Pr}\{B\;|\;A\} + \text{Pr}\{\overline{A}\} \text{Pr}\{B\;|\;\overline{A}\}.
\end{aligned}
\end{equation*}
Substituting into (C.17) yields
\begin{equation*}
\begin{aligned}
  \text{Pr}\{A\;|\;B\} + \text{Pr}\{\overline{A}\;|\;B\}
  &= \frac{\text{Pr}\{A\} \text{Pr}\{B\;|\;A\}}{\text{Pr}\{B\}} +
     \frac{\text{Pr}\{\overline{A}\} \text{Pr}\{B\;|\;\overline{A}\}}{\text{Pr}\{B\}}\\
  &= \frac{\text{Pr}\{A\} \text{Pr}\{B\;|\;A\} + \text{Pr}\{\overline{A}\} \text{Pr}\{B\;|\;\overline{A}\}}
          {\text{Pr}\{B\}}\\
  &= \frac{\text{Pr}\{A\} \text{Pr}\{B\;|\;A\} + \text{Pr}\{\overline{A}\} \text{Pr}\{B\;|\;\overline{A}\}}
          {\text{Pr}\{A\} \text{Pr}\{B\;|\;A\} + \text{Pr}\{\overline{A}\} \text{Pr}\{B\;|\;\overline{A}\}}\\
  &= 1.
\end{aligned}
\end{equation*}

\end{framed}

\item[C.2{-}5] {Prove that for any collection of events $A_1, A_2, \dots, A_n$,
\[
  \text{Pr}\{A_1 \cap A_2 \cap \cdots \cap A_n\} =
    \text{Pr}\{A_1\} \cdot \text{Pr}\{A_2\;|\;A_1\} \cdot \text{Pr}\{A_3\;|\;A_1 \cap A_2\} \cdots
    \text{Pr}\{A_n\;|\;A_1 \cap A_2 \cap \cdots \cap A_{n - 1}\}.
\]
}

\begin{framed}
It is trivially valid for $n = 1$. As our base case, consider $n = 2$. From (C.16) we have
\[
  \text{Pr}\{A_1 \cap A_2\} = \text{Pr}\{A_1\} \text{Pr}\{A_2\;|\;A_1\}.
\]
Now assume it holds for $n$. For $n + 1$, we have
\begin{equation*}
\begin{aligned}
  \text{Pr}\{A_1 \cap A_2 \cap \cdots \cap A_{n + 1}\}
  &= \text{Pr}\{(A_1 \cap A_2 \cap \cdots \cap A_n) \cap A_{n + 1}\}\\
  &= \text{Pr}\{A_1 \cap A_2 \cap \cdots \cap A_n\} \text{Pr}\{A_{n + 1}\;|\;A_1 \cap A_2 \cap \cdots \cap A_n\}\\
  &= \text{Pr}\{A_1\} \cdot \text{Pr}\{A_2\;|\;A_1\} \cdot \text{Pr}\{A_3\;|\;A_1 \cap A_2\} \cdots
     \text{Pr}\{A_{n + 1}\;|\;A_1 \cap A_2 \cap \cdots \cap A_{n}\}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.2{-}6] {($\star$) Describe a procedure that takes as input two integers
  $a$ and $b$ such that $0 < a < b$ and, using fair coin flips, produces as
  output heads with probability $a/b$ and tails with probability $(b - a)/b$.
  Give a bound on the expected number of coin flips, which should be $O(1)$.
  (Hint: Represent $a/b$ in binary.)}

\begin{framed}
Consider a continuous uniform probability distribution on
$\interval[open right]{0}{1}$, such that $\text{Pr}\{\interval[open right]{0}{1}\} = 1$. We have
\[
  \text{Pr}\left\{\interval[open right, scaled]{0}{\frac{a}{b}}\right\} = \frac{a}{b},
\]
and
\[
  \text{Pr}\left\{\interval[open right, scaled]{\frac{a}{b}}{1}\right\} = 1 - \frac{a}{b} = \frac{b - a}{b}.
\]

With this notion, we can write a procedure that sorts a real number from
$\interval[open right]{0}{1}$ and return heads if it is lower than $a/b$ or
return tails, otherwise. Using fair coin flips and representing numbers in
binary, for each flip we have a new decimal place from a random number on
$\interval[open right]{0}{1}$ (consider an ``0'' if the coin flip is head and
``1'', otherwise). Then,
\begin{itemize}
\item if the $i$-th flip is 1 and the $i$-th decimal place
of $a/b$ is 0, the sorted number is larger than $a/b$ and we return tails;
\item if the $i$-th flip is 0 and the $i$-th decimal place
of $a/b$ is 1, the sorted number is smaller than $a/b$ and we return head;
\item if the $i$-th flip and the $i$-th decimal place are equal, we sort a new
decimal place.
\end{itemize}

Since we do not know how many decimal places $a/b$ has (if periodic, this number
is infinite), the above procedure does not have a maximum number of iterations.
However, since for each flip we have a probability of 1/2 of returning head or
tails, the probability of terminating at flip $i$, for $i \ge 1$, is
\[
  \underbrace{1/2 \cdot 1/2 \cdots}_\text{$i$ times} = \frac{1}{2^i}.
\]
Thus, by using the notion of expected value and the result (A.8), the expected number of
flips is
\[
  \sum_{i = 1}^{\infty} i \cdot \frac{1}{2^i}
  = \sum_{i = 0}^{\infty} i \cdot \left(\frac{1}{2}\right)^{i}
  = \frac{1/2}{(1 - 1/2)^2} = 2.
\]

\end{framed}

\item[C.2{-}7] {($\star$) Show how to construct a set of $n$ events that are
  paiwise independent but such that no subset of $k > 2$ of them is mutually
  independent.}

\begin{framed}
Skipped.
\end{framed}

\item[C.2{-}8] {($\star$) Two events $A$ and $B$ are \textbf{\emph{conditionally
  independent}}, given $C$, if
  \[
    \text{Pr}\{A \cap B\;|\;C\} = \text{Pr}\{A\;|\;C\} \cdot \text{Pr}\{B\;|\;C\}.
  \]
  Give a simple but nontrivial example of two events that are not independent
  but are conditionally independent given a third event.
}

\begin{framed}
Skipped.
\end{framed}

\newpage

\item[C.2{-}9] {($\star$) You are a contestant in a game show in which a prize
  is hidden behind one of three curtains. You will win the prize if you select
  the correct curtain. After you have picked one curtain but before the curtain
  is lifted, the emcee lifts one of the other curtains, knowing that it will
  reveal an empty stage, and asks if you would like to switch from your current
  selection to the remaining curtain. How would your chances change if you
  switch? (This question is the celebrated \textbf{\emph{Monty Hall problem}},
  named after a game-show host who often presented contestants with just this
  dilemma.)}

\begin{framed}
If you never switch, the only way to win is to choose the right curtain at the
beginning (before the emcee lifts one of the others). In this case, your chance
to win are $1/3$. If you always switch, the only way to loose is to choose the
right curtain at the beginning. In this case, when you choose a curtain without
the prize, the emcee will reveal the other empty curtain and you will
therefore change to the correct one. Thus, your chance to win are $(1
- 1/3) = 2/3$.
\end{framed}

\item[C.2{-}10] {($\star$) A prison warden has randomly picked one prisoner
  among three to go free. The other two will be executed. The guard knows which
  one will go free but is forbidden to give any prisoner information regarding
  his status. Let us call the prisoners $X, Y$, and $Z$. Prisoner $X$ asks the
  guard privately which of $Y$ or $Z$ will be executed, arguing that since he
  already knows that at least one of them must die, the guard won't be revealing
  any information about his own status. The guard tells $X$ that $Y$ is to be
  executed.  Prisoner $X$ feels happier now, since he figures that either he or
  prisoner $Z$ will go free, which means that his probability of going free is
  now $1/2$. Is he right, or are his chances still $1/3$? Explain.}
\begin{framed}
  His chances are still $1/3$. Let $A$ be the event of prisoner $X$ going free
  and $B$ the event that the guard tells $X$ that $Y$ is to be executed. We have
  \[
    \text{Pr}(A\;|\;B) = \frac{\text{Pr}(A) \text{Pr}(B\;|\;A)}{\text{Pr}(B)} = \frac{1/3 \cdot 1/2}{1/2} = \frac{1}{3}.
  \]
\end{framed}

\end{enumerate}

\newpage

\section{Discrete random variables}

\begin{enumerate}

\item[C.3{-}1]{Suppose we roll two ordinary, 6-sided dice. What is the
expectation of the sum of the two values showing? What is the expectation of
the maximum of the two values showing?}

\begin{framed}
There are $36$ elementary events in the sample space. Since they are
ordinary dices, the probability distribution is uniform.

Let $X$ be the random variable of the sum of the two values. The possible
outcomes of $X$ are
\begin{center}
\begin{tabular}{rrrrrrr}
  & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6}\\
  \textbf{1} & 2 & 3 & 4 & 5  & 6  & 7\\
  \textbf{2} & 3 & 4 & 5 & 6  & 7  & 8\\
  \textbf{3} & 4 & 5 & 6 & 7  & 8  & 9\\
  \textbf{4} & 5 & 6 & 7 & 8  & 9  & 10\\
  \textbf{5} & 6 & 7 & 8 & 9  & 10 & 11\\
  \textbf{6} & 7 & 8 & 9 & 10 & 11 & 12
\end{tabular}
\end{center}

Thus, we have
\begin{equation*}
\begin{aligned}
  \text{E}[X] &= \sum_{x = 2}^{12} x \cdot \text{Pr}(X = x)\\
              &= 2 \cdot \frac{1}{36} + 3 \cdot \frac{2}{36} + 4 \cdot \frac{3}{36} + 5 \cdot \frac{4}{36}
               + 6 \cdot \frac{5}{36} + 7 \cdot \frac{6}{36} + 8 \cdot \frac{5}{36} + 9 \cdot \frac{4}{36}
               + 10 \cdot \frac{3}{36} + 11 \cdot \frac{2}{36} + 12 \cdot \frac{1}{36}\\
              &= 7.
\end{aligned}
\end{equation*}

Let $Y$ be the random variable of the maximum of the two values. The possible
outcomes of $Y$ are

\begin{center}
\begin{tabular}{rrrrrrr}
  & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6}\\
  \textbf{1} & 1 & 2 & 3 & 4 & 5 & 6\\
  \textbf{2} & 2 & 2 & 3 & 4 & 5 & 6\\
  \textbf{3} & 3 & 3 & 3 & 4 & 5 & 6\\
  \textbf{4} & 4 & 4 & 4 & 4 & 5 & 6\\
  \textbf{5} & 5 & 5 & 5 & 5 & 5 & 6\\
  \textbf{6} & 6 & 6 & 6 & 6 & 6 & 6
\end{tabular}
\end{center}

Thus, we have
\begin{equation*}
\begin{aligned}
  \text{E}[Y] &= \sum_{x = 1}^{6} x \cdot \text{Pr}(X = x)\\
              &= 1 \cdot \frac{1}{36} + 2 \cdot \frac{3}{36} + 3 \cdot \frac{5}{36} + 4 \cdot \frac{7}{36}
               + 5 \cdot \frac{9}{36} + 6 \cdot \frac{11}{36}\\
              &\approx 4.47.
\end{aligned}
\end{equation*}

\end{framed}

\item[C.3{-}2]{An array $A[1 \dots n]$ contains $n$ distinct numbers that are
randomly ordered, with each permutation of the $n$ numbers being equally
likely. what is the expectation of the index of the maximum element in the
array? What is the expectation of the index of the minimum element in the
array?}

\begin{framed}
Let $X$ and $Y$ be random variables of the index of the maximum and minimum
elements, respectivelly. Since each permutation is equaly likely,
\[
  \text{E}[X] = \text{E}[Y] = \sum_{i = 1}^{n} i \cdot \frac{1}{n} = \frac{1}{n} \frac{n (n + 1)}{2} = \frac{n + 1}{2}.
\]
\end{framed}

\newpage

\item[C.3{-}3]{A carnival game consists of three dice in a cage. A player can
bet a dollar on any of the numbers $1$ through $6$. The cage is shaken, and
the payoff is as follows. If the player's number doesn't appear on any of
the dice, he loses his dollar. Otherwise, if his number appears on exactly
$k$ of the three dice, for $k = 1, 2, 3$, he keeps his dollar and wins $k$
more dollars. What is his expected gain from playing the carnival game
once?}

\begin{framed}
Let $X$ be a random variable of the total gain. The possible outcomes are
$-1, 1, 2, 3$. We have
\begin{equation*}
\begin{aligned}
  & \text{Pr}\{X = -1\} &&= (5/6 \cdot 5/6 \cdot 5/6)         &&= 125/216,\\
  & \text{Pr}\{X = 1\}  &&= (1/6 \cdot 5/6 \cdot 5/6) \cdot 3 &&= 75/216,\\
  & \text{Pr}\{X = 2\}  &&= (1/6 \cdot 1/6 \cdot 5/6) \cdot 3 &&= 15/216,\\
  & \text{Pr}\{X = 3\}  &&= (1/6 \cdot 1/6 \cdot 1/6)         &&= 1/216.
\end{aligned}
\end{equation*}
Thus, we have
\[
  \text{E}[X] = -1 \cdot \frac{125}{216} + 1 \cdot \frac{75}{216} + 2 \cdot \frac{15}{216} + 3 \cdot \frac{1}{216} \approx -0.0787.
\]
\end{framed}

\item[C.3{-}4]{Argue that if $X$ and $Y$ are nonnegative random variables, then
\[
  \text{E}[\max(X, Y)] \le \text{E}[X] + \text{E}[Y].
\]
}

\begin{framed}
The expectation of nonnegative random variables is a summation of nonnegative
numbers. Thus, since $\text{E}[\max(X, Y)]$ is either $\text{E}[X]$ or
$\text{E}[Y]$, it must be equal or lower than $\text{E}[X] + \text{E}[Y]$.
\end{framed}

\item[C.3{-}5]{($\star$) Let $X$ and $Y$ be independent random variables. Prove
that $f(X)$ and $g(Y)$ are independent for any functions $f$ and $g$.}

\begin{framed}
Skipped.

% \begin{equation*}
% \begin{aligned}
%   \text{Pr}\{f(X) = f(x) \text{ and } g(Y) = g(y)\}
%   &= \text{Pr}\{X = x \text{ and } Y = y\}\\
%   &= \text{Pr}\{X = x\} \text{Pr}\{Y = y\}\\
%   &= \text{Pr}\{f(X) = f(x)\} \text{Pr}\{g(Y) = g(y)\}.
% \end{aligned}
% \end{equation*}

% Since $X$ and $Y$ are (discrete) independent random variables, we have
% \begin{equation*}
% \begin{aligned}
% \sum_{x} \sum_{y} \text{Pr}\{X = x \text{ and } Y = y\}
% &= \sum_{x} \sum_{y} \text{Pr}\{X = x\} \text{Pr}\{Y = y\}\\
% &= \sum_{x} \text{Pr}\{X = x\} \sum_{y} \text{Pr}\{Y = y\}.
% \end{aligned}
% \end{equation*}
\end{framed}

\item[C.3{-}6]{($\star$) Let $X$ be a nonnegative random variable, and suppose
that $\text{E}[X]$ is well defined. Prove \textbf{\emph{Markov's inequality}}:
\[
  \text{Pr}\{X \ge t\} \le \text{E}[X]/t
\]
for all $t > 0$.
}

\begin{framed}
We have
\begin{equation*}
\begin{aligned}
  \text{E}[X] &=   \sum_{x}{x \cdot \text{Pr}\{X = x\}}\\
              &\ge \sum_{x \ge t}{x \cdot \text{Pr}\{X = x\}}\\
              &\ge \sum_{x \ge t}{t \cdot \text{Pr}\{X = x\}}\\
              &= t \cdot \sum_{x \ge t}{\text{Pr}\{X = x\}}\\
              &= t \cdot \text{Pr}\{X \ge t\},
\end{aligned}
\end{equation*}
which implies
\[
  \text{Pr}\{X \ge t\} \le \text{E}[X]/t.
\]
\end{framed}

\newpage

\item[C.3{-}7]{($\star$) Let $S$ be a sample space, and let $X$ and $X'$ be
random variables such that $X(s) \ge X'(s)$ for all $s \in S$. Prove that for
any real constant $t$,
\[
  \text{Pr}\{X \ge t\} \ge \text{Pr}\{X' \ge t\}.
\]
}

\begin{framed}
Assuming that the domain of $X$ and $X'$ are the sample space $S$, we have
\begin{equation*}
\begin{aligned}
  \text{Pr}\{X \ge t\} &=   \sum_{s \in S:X(s) \ge t}{\text{Pr}\{X = s\}}\\
                       &=   \sum_{s \in S:X'(s) \ge t}{\text{Pr}\{X' = s\}} + \sum_{s \in S:X(s) \ge t > X'(s)}{\text{Pr}\{X' = s\}}\\
                       &\ge \sum_{s \in S:X'(s) \ge t}{\text{Pr}\{X' = s\}}\\
                       &=   \text{Pr}\{X' \ge t\}.
\end{aligned}
\end{equation*}
\end{framed}

\item[C.3{-}8]{Which is larger: the expectation of the square of a random
variable, or the square of its expectation?}

\begin{framed}
We have from (C.28)
\[
  \text{E}[X^2] = \text{Var}[X] + \text{E}^2[X],
\]
which implies
\[
  \text{E}[X^2] \ge \text{E}^2[X],
\]
since both $\text{Var}[X]$ and $\text{E}^2[X]$ are nonnegative numbers.

\end{framed}

\item[C.3{-}9]{Show that for any random variable X that takes on only the values
0 and 1, we have
\[
  \text{Var}[X] = \text{E}[X] \text{E}[1 - X].
\]
}

\begin{framed}
We have
\[
  \text{E}[X] = 0 \cdot \text{Pr}\{X = 0\} + 1 \cdot \text{Pr}\{X = 1\} = \text{Pr}\{X = 1\},
\]
and
\[
  \text{E}[1 - X] = 1 \cdot \text{Pr}\{X = 0\} + 0 \cdot \text{Pr}\{X = 1\} = \text{Pr}\{X = 0\},
\]
which implies
\begin{equation*}
\begin{aligned}
  \text{Var}[X] &= \text{E}[X^2] - \text{E}^2[X] & \text{(since $X^2 = X$)}\\
                &= \text{E}[X] - \text{E}[X] \text{E}[X]\\
                &= \text{E}[X] (1 - \text{E}[X])\\
                &= \text{E}[X] (1 - \text{Pr}\{X = 1\})\\
                &= \text{E}[X] \text{Pr}\{X = 0\}\\
                &= \text{E}[X] \text{E}[1 - X].
\end{aligned}
\end{equation*}
\end{framed}

\item[C.3{-}10]{Prove that $\text{Var}[aX] = a^2 \text{Var}[X]$ from the
definition (C.27) of variance.}

\begin{framed}
Assuming that $X$ is a random variable and $a$ is a constant, from (C.27) and
(C.22) we have
\begin{equation*}
\begin{aligned}
  \text{Var}[a X] &= \text{E}[(a X - \text{E}[a X])^2]\\
                  &= \text{E}[(a X - a \text{E}[X])^2]\\
                  &= \text{E}[a^2 (X - \text{E}[X])^2]\\
                  &= a^2 \text{E}[(X - \text{E}[X])^2]\\
                  &= a^2 \text{Var}[X].
\end{aligned}
\end{equation*}
\end{framed}

\end{enumerate}


\section{Some Counting and Stirling's Formula}
\subsection{Some Counting}
If we have a finite set $\Omega$, and let $M$ be the number of ways of partition $\Omega$ into $k$ subsets $S_1,S_2,\ldots,S_k$ with $|S_j|=n_j$ with $n_1+n_2+\cdots +n_k=|\Omega|$, then
$$M=\binom{|\Omega|}{n_1,n_2,\ldots,n_k}=\frac{|\Omega|}{n_1!n_2!\cdots n_k!}$$
We suddenly want to count the number of strictly increasing and nondecreasing functions from the set $\{1,2,\ldots,k\}$ to $\{1,2,\ldots,n\}$.
Note that each strictly increasing function in this way are uniquely identified by their image, so the number of such functions equals $\binom{n}{k}$.
\footnote{$\binom{n}{k}=0$ for $k>n$}
To count nondecreasing functions, however, we cannot use this trick.
Nonetheless, we can consider the bijection
$$\{f\text{ nondecreasing}:\{1,2,\ldots,k\}\to\{1,2,\ldots,n\}\}\to$$
$$\{f\text{ strictly increasing}:\{1,2,\ldots,k\}\to\{1,2,\ldots,n+k-1\}\}$$
by assigning a function $f$ in the previous set to the function $g(i)=f(i)+i-1$.
So that number is $\binom{n+k-1}{k}$.
\subsection{Stirling's Formula}
\begin{definition}
    Let $(a_n),(b_n)$ be two positive sequences, we say $a_n\sim b_n$ if $a_n/b_n\to 1$ as $n\to\infty$.
\end{definition}
\begin{theorem}[Stirling]
    $$n!\sim n^n\sqrt{2\pi n}e^{-n}$$
\end{theorem}
\begin{proposition}[Weaker Statement of Stirling's Formula]
    $\log(n!)\sim n\log n$
\end{proposition}
\begin{proof}
    Let $\ell_n=\log(n!)$.
    So we have
    $$\ell(n)=\log 2+\log 3+\cdots+\log n$$
    Note that we have the trivial bound $\log\lfloor x\rfloor\le \log x\le \log\lfloor x+1\rfloor$, hence
    $$\ell_{n-1}\le\int_1^n\log x\,\mathrm dx\le \ell_n$$
    So
    $$n\log(n)-n+1\le \ell_n\le (n+1)\log(n+1)-n$$
    The proposition follows.
\end{proof}
\begin{proof}[Proof of Stirling's Formula]
    Note that
    $$\int_a^bf(x)\,\mathrm dx=\frac{f(a)+f(b)}{2}(b-a)-\frac{1}{2}\int_a^b(x-a)(b-x)f^{\prime\prime}(x)\,\mathrm dx$$
    We take $f=\log$ we have
    $$\int_k^{k+1}\log x\,\mathrm dx=\frac{\log(k)+\log(k+1)}{2}+\frac{1}{2}\int_0^1\frac{x(1-x)}{(x+k)^2}\,\mathrm dx$$
    Summing over $k=1,2,\ldots,n-1$ we have
    $$n\log n-n+1=\log(n!)-\frac{\log n}{2}+\sum_{k=1}^{n-1}a_k$$
    where
    $$a_k=\frac{1}{2}\int_0^1\frac{x(1-x)}{(x+k)^2}\,\mathrm dx$$
    Note that it is easy to see the partial sum of $a_k$ converges, so we define $A=\exp(1-\sum_{k\in\mathbb N}a_k)$ to have
    $$n!=n^n\sqrt{n}e^{-n}A\exp\left(\sum_{k=n}^\infty a_k\right)$$
    Note that the last part goes to $1$ as $n\to\infty$, so it remains to show $A=\sqrt{2\pi}$.\\
    We claim that
    $$2^{-2n}\binom{2n}{n}\sim\frac{1}{\sqrt{\pi n}}$$
    since $2^{-2n}\binom{2n}{n}\sim\sqrt 2/(A\sqrt{n})$, it will prove what we want.
    Consider
    $$I_n=\int_0^{\pi/2}\cos^n\theta\,\mathrm d\theta$$
    It is trivial to see that it equals $I_{2n}=\binom{2n}{n}\pi/2^{2n+1}$ and $I_{2n+1}=2^{2n}\binom{2n}{n}^{-1}/(2n+1)$.
    We want to show that $I_{2n}/I_{2n+1}\to 1$ which will prove the result, but this is obvious since $I_{n+2}/I_n\to 1$ and $I_n$ is decreasing.
\end{proof}


\subsection{Absolute Cartesian Method}

\begin{frame}{Definition}
    \begin{itemize}
        \item Use a fixed reference frame which has $x,y,z$-axes (as opposed to $r,\theta,z$-axes or $\rho,\theta,z$-axes).
        \item In a planar kinematic chain, the position of every link is defined by:
        \begin{itemize}
            \item Joint positions
            \item Center of gravity $g$
            \item Angle $\theta$ with respect to $x$-axis
       \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}
For planar mechanisms, the following relations are considered:
	\begin{itemize}
	    \item[] $\displaystyle (x_A-x_B)^2+(y_A-y_B)^2=AB^2=l_{AB}^2$
	    \item[] $\displaystyle k=\tan{\theta}=\frac{y_B-y_A}{x_B-x_A}$
	    \item[] $\displaystyle\Rightarrow y=ax+b$
	\end{itemize}
where:
	\begin{itemize}
	    \item[-] $A(x_A,y_A)$, $B(x_B,y_B)$ are joint coordinates
	    \item[-] $l_{AB}$ is the length of link $AB$
	    \item[-] $\theta$ is the angle of link $AB$ with respect to $x$-axis
	    \item[-] $a,b$ are coefficients ($a$ is slope of link $AB$, $b$ is intercept)
	\end{itemize}
Solving these equations often yields 2 position coordinates. Depending on positions of other links, choose the correct solution.
\end{frame}


\chapter{Conclusion and Outlook}

\section{Summary}
We defined an algebraic graph structure that expresses the Collatz sequences in the form of a tree. Next, the vertex reachability properties were unveiled by examining the relationship between successive nodes in $H_C$. Moreover, we dealt with graphs that represent other variants of Collatz sequences, for instance $5x+1$ or $181x+1$. The interesting part of both variants is that for these sequences the existence of cycles is known. They serve as the basis for further investigations of the problem.

\section{Further Research}
In subsequent studies, the properties of vertices in $H_C$ might be elaborated upon more closely by taking into account a vertex's label as well as its properties. Moreover we will investigate on the structure of a pruned trees $T_{\ge j}$ including questions on calculating a left-child or right-child within any of these trees.


\subsection{The jackknife}

We have a statistic:

\(S(x_1, x_2,...,x_n)\)

We may want to estimate moments for this statistic, but are unable to do so.

\subsubsection{The jackknife estimator}

The jackknife is an approach for getting moments for statistics.

We start by creating \(n\) statistics each leaving out one observation.

\(\bar S_i(x_1,x_2,...x_{i-1},x_{i+1},...,x_n)\)

We define:

\(\bar S=\dfrac{1}{n}\sum_i\bar S_i\)

\subsubsection{Moments of the jackknife estimator}

We want to know the variance.

\(Var \bar S=\dfrac{n-1}{n}\sum_i(\bar S_i-\bar S)^2\).


% !TEX root = main.tex

%-------------------------------------------------
\section{Conditional distributions}\label{sec:cond_dist}

Recall that for any two events $A$ and $B$ with $\prob(A)>0$, the \emph{conditional probability} of $B$ given $A$ is
\[
\prob(B|A) = \frac{\prob(A\cap B)}{\prob(A)}.
\]
This notion extends to random variables: if $C$ and $D$ are subsets of $\R$, the conditional probability of $\{Y\in D\}$ given $\{X\in C\}$ is
\[
\prob(Y\in D|X\in C) = \frac{\prob(X\in C,Y\in D)}{\prob(X\in C)}.
\]

%-----------------------------
\subsection{Conditioning on events}
Let $A$ be an event and let $Y$ be a random variable.

\begin{definition}\label{def:conditional_on_events}
\ben
\it The \emph{conditional distribution} of $Y|A$ is the function $\prob_{Y|A}$ defined on subsets of $\R$ by
\[
\prob_{Y|A}(D) = \prob(Y\in D|A) = \frac{\prob\big(\{Y\in D\}\cap A\big)}{\prob(A)}\qquad\text{for all $D\subseteq\R$}.
\]
\it The \emph{conditional CDF} of $Y|A$ is the function $F_{Y|A}$ defined on $\R$ by
\[
F_{Y|A}(y) = \prob(Y\leq y|A) = \frac{\prob\big(\{Y\leq y\}\cap A\big)}{\prob(A)}\qquad\text{for all $y\in\R$}.
\]
\it If $Y$ is discrete the, \emph{conditional PMF of $Y|A$} is the function
\[
f_{Y|A}(y) = \prob(Y=y|A) = \frac{\prob\big(\{Y=y\}\cap A\big)}{\prob(A)}.
\]
\it If $Y$ is continuous the \emph{conditional PDF of $Y|A$} is the function
\[
f_{Y|A}(y) = F'_{Y|A}(y).
\]
\it The \emph{conditional expectation of $Y|A$} is taken with respect to the conditional distribution of $Y|A$:
\[
\expe\big(Y|X) = \begin{cases}
	\ \displaystyle\sum_{i=1}^{\infty} y_i f_{Y|A}(y_i)		& \text{if $Y$ is discrete;} \\[3ex]
	\ \displaystyle\int_{-\infty}^{\infty} y f_{Y|A}(y)\,dy	& \text{if $Y$ is continuous.}
\end{cases}
\]
\een
\end{definition}

% example
\begin{example}\label{example:cond_pmf}
A fair coin is tossed repeatedly until a head occurs. Find the conditional PMF of the number of times the coin is tossed, given that the coin is tossed an odd number of times.
\begin{solution}
Let $A$ be the event that the coin is tossed an odd number of times and let $Y$ be the number of times the coin is tossed.
The sample space is $\Omega=\{H,TH,TTH,TTTH,\ldots\}$ and $A=\{H,TTH, TTTTH,\ldots\}$, and because the coin is fair, $\prob(Y=k)=1/2^k$ so
\[
\prob(A) = \frac{1}{2} + \frac{1}{8} + \frac{1}{32} + \frac{1}{128} + \ldots = \frac{2}{3}.
\]
Hence the conditional PMF of $Y|A$ is
\[
f_{Y|A}(k)
	= \frac{\prob(\{Y=k\}\cap A)}{\prob(A)}
	= \begin{cases}
	3/2^{k+1}	& \text{if $k$ is odd,} \\
	0			& \text{if $k$ is even.}
	\end{cases}
\]
Note that the denominator acts as a \emph{scale factor} to ensure that the conditional probabilities $f_{Y|A}(k)$ sum to $1$.
The conditional expectation of $Y|A$ is
\[
\expe(Y|A)
	= \sum_{k=1}^{\infty} k f_{Y|A}(y)
	= \sum_{\stackss{k=1}{k\text{ odd}}}^{\infty} \frac{3k}{2^{k+1}}
	= \sum_{\ell=1}^{\infty} \frac{3(l+1)/2}{2^{(\ell+1)/2+1}}
	= \sum_{\ell=1}^{\infty} \frac{3(l+1)}{4\sqrt{2^{\ell+1}}}
\]
\end{solution}
\end{example}

% example
\begin{example}
Let $Y\sim\text{Uniform}[0,1]$. Find the conditional expectation of $Y$ given that $1/2\leq Y\leq 3/4$.
\begin{solution}
The CDF of the standard uniform distribution is
\[
F_Y(y) = \begin{cases}
	0	& \text{if $y < 0$} \\
	y	& \text{if $0 \leq y \leq 1$} \\
	1	& \text{if $y > 1$.}
\end{cases}
\]
Let $A=\{1/2\leq Y\leq 3/4\}$. Then the conditional CDF of $Y|A$ is
\begin{align*}
F_{Y|A}(y) = \prob(Y\leq y|A)
%	& = \frac{\prob(Y\leq y, A)}{\prob(A)} \\
	& = \frac{\prob(Y\leq y\text{ and }1/2\leq Y\leq 3/4)}{\prob(1/2\leq Y\leq 3/4)} \\
	& = \frac{\prob(Y\leq y)-\prob(Y\leq 1/2)}{\prob(Y\leq 3/4)-\prob(Y\leq 1/2)} \\
	& = \frac{F_Y(y)-F_Y(1/2)}{F_Y(3/4)-F_Y(1/2)}
	= \begin{cases}
    	0	& \text{if $y < 1/2$} \\
    	4y	& \text{if $1/2 \leq y \leq 3/4$} \\
    	1	& \text{if $y > 3/4$.}
    \end{cases}
\end{align*}
The conditional PDF of $Y|A$ is therefore equal to $f_{Y|A}(y) = F'_{Y|X}(y) = 4$ if $1/2\leq y\leq 3/4$ and zero otherwise, which shows that $Y|A\sim\text{Uniform}[1/2,3/4]$. The conditional expectation of $Y|A$ is
\[
\expe(Y|A)
	= \int_{-\infty}^{\infty} y f_{Y|A}(y)\,dy
	= \int_{1/2}^{3/4} 4y \,dy
	= 5/8,
\]
which is the mid-point of the interval $[1/2,3/4]$ as expected.
\end{solution}
\end{example}

%-----------------------------
\subsection{Conditioning on random variables}
Let $X$ and $Y$ be two random variables defined on the same probability space.

\begin{definition}
\ben
\it The conditional distribution of $Y|X$ is the function $\prob_{Y|X}$ defined on pairs of subsets of $\R$ by
\[
\prob_{Y|X}(C,D) = \prob(Y\in D|X\in C) = \frac{\prob(X\in C, Y\in D)}{\prob(X\in C)}\qquad\text{for all $C,D\subseteq\R$}.
\]
This is completely determined by the following.
\it The conditional CDF of $Y|X$ is the function $F_{Y|X}$ defined on $\R^2$ by
\[
F_{Y|X}(x,y) = \prob(Y\leq y|X\leq x) = \frac{\prob(X\leq x, Y\leq y)}{\prob(X\leq x)}\qquad\text{for all $x,y\in\R$}.
\]
\een
\end{definition}

% lemma: cond CDF formula
\begin{lemma}
The conditional CDF of $Y|X$ satisfies
\[
F_{Y|X}(x,y) = \frac{F_{X,Y}(x,y)}{F_X(x)}
\]
where $F_{X,Y}$ is the joint CDF of $X$ and $Y$, and $F_X$ is the marginal CDF of $X$.
\end{lemma}
\begin{proof}
\[
F_{Y|X}(x,y)
	= \prob(Y\leq y\,|\,X\leq x)
	= \frac{\prob(X\leq x, Y\leq y)}{\prob(X\leq x)}
	= \frac{F_{X,Y}(x,y)}{F_X(x)}.
\]
\end{proof}

%% def: cond PMF/PDF
%Suppose we observe the event $\{X=x\}$ where $x$ is a fixed value. If the marginal PMF/PDF of $X$ satisfies $f_X(x)>0$, we write the conditional PMF/PDF of $Y|X=x$ as
%\begin{definition}
%Let $x$ be a fixed value. If $f_X(x)>0$ the \emph{conditional PMF/PDF of $Y|X=x$} is
%\[
%f_{Y|X}(y|x) = \frac{f_{X,Y}(x,y)}{f_X(x)}
%\]
%where $f_{X,Y}$ is the joint PMF/PDF of $X$ and $Y$.
%\end{definition}
%
%\bigskip
%Recall the partition theorem for random events: if $\{A_1,A_2,\ldots\}$ is a partition of $B$ then
%\[
%\prob(B) = \sum_{i=1}^{\infty} \prob(B|A_i)\prob(A_i).
%\]
%We have the following version of the partition theorem for random variables.
%
%% partition theorem
%\begin{theorem}\label{thm:partition_for_rvs}
%The marginal PMF/PDF of $Y$ satisfies
%\[\begin{array}{lll}
%f_Y(y) & = \displaystyle
%	\sum_{i=1}^{\infty} 		f_{Y|X}(y|x_i)f_X(x_i)		& \quad\text{if $X$ is discrete, and} \\[4ex]
%f_Y(y) & = \displaystyle
%	\int_{-\infty}^{\infty} 	f_{Y|X}(y|x)f_X(x)\,dx		& \quad\text{if $X$ is continuous.}
%\end{array}
%\]
%\end{theorem}
%\begin{proof}
%For the continuous case (the discrete case is similar),
%\[
%\int f_{X|Y}(x|y) f_Y(y)\,dy
%	= \int \left(\frac{f_{X,Y}(x,y)}{f_Y(y)}\right) f_Y(y)\,dy
%	= \int f_{X,Y}(x,y)\,dy
%	= f_X(x).
%\]
%\end{proof}
%
%%-----------------------------
%\subsubsection{Change notation}
%
%%-----------------------------

% def: cond PMF/PDF
%Let $x$ be a fixed value and suppose we observe that $X=x$. If $X$ is a continuous random variable, we cannot define the conditional PMF/PDF of $Y|X=x$ using definition~\ref{def:conditional_on_events}, because it might be that $\prob(X=x)=0$, and division by zero is undefined. Instead we use the following.
% where $x$ is a fixed value. If the marginal PMF/PDF of $X$ satisfies $f_X(x)>0$, we write the conditional PMF/PDF of $Y|X=x$ as
\begin{definition}
If $f_X(x)>0$ the \emph{conditional PMF/PDF of $Y|X=x$} is
\[
f_{Y|X=x}(y) = \frac{f_{X,Y}(x,y)}{f_X(x)}
\]
where $f_{X,Y}$ is the joint PMF/PDF of $X$ and $Y$.
\end{definition}

\bigskip
Recall the partition theorem for random events: if $\{A_1,A_2,\ldots\}$ is a partition of $B$ then
\[
\prob(B) = \sum_{i=1}^{\infty} \prob(B|A_i)\prob(A_i).
\]
We have the following version of the partition theorem for random variables.

% partition theorem
\begin{theorem}\label{thm:partition_for_rvs}
The marginal PMF/PDF of $Y$ satisfies
\[\begin{array}{lll}
f_Y(y) & = \displaystyle
	\sum_{i=1}^{\infty} 		f_{Y|X=x_i}(y)f_X(x_i)		& \quad\text{if $X$ is discrete, and} \\[4ex]
f_Y(y) & = \displaystyle
	\int_{-\infty}^{\infty} 	f_{Y|X=x}(y)f_X(x)\,dx		& \quad\text{if $X$ is continuous.}
\end{array}
\]
\end{theorem}
\begin{proof}
For the continuous case (the discrete case is similar),
\[
\int f_{Y|X=x}(y) f_Y(y)\,dx
	= \int \left(\frac{f_{X,Y}(x,y)}{f_Y(y)}\right) f_Y(y)\,dx
	= \int f_{X,Y}(x,y)\,dx
	= f_Y(y).
\]
\end{proof}

\begin{definition}\label{def:cond_expe_x}
Let $x$ be a fixed value. The \emph{conditional expectation of $Y|X=x$} is
\[
\begin{array}{lll}
\expe(Y|X=x) & = \displaystyle\sum_{j=1}^{\infty} y_j\,f_{Y|X=x}(y_j)		& \quad\text{if $Y$ is discrete, or} \\[4ex]
\expe(Y|X=x) & = \displaystyle\int_{-\infty}^{\infty} y\,f_{Y|X=x}(y)\,dy	& \quad\text{if $Y$ is continuous.}
\end{array}
\]
\end{definition}

%-----------------------------
\subsection{Conditional expectation}

For any fixed value of $x$ the conditional expectation $\expe(Y|X=x)$ is just a number. Let us now think of $x$ as a variable quantity, and consider the transformation
\[
\begin{array}{rccl}
	g:	& \R	& \to		& \R \\
		& x		& \mapsto	& \expe(Y|X=x)
\end{array}
\]
This transformation of $X$ yields a new random variable.
\begin{definition}\label{def:cond_expe}
The \emph{conditional expectation of $Y|X$} is the random variable
\[\begin{array}{llll}
\expe(Y|X):	& \Omega 	& \to 		& \R \\
			& \omega	& \mapsto 	& \expe\big[Y|X=X(\omega)\big].
\end{array}\]
\end{definition}

The distribution of $\expe(Y|X)$ depends only on the distribution of $X$ and its expectation is given by
\[
\begin{array}{lll}
\expe\big[\expe(Y|X)\big] & = \displaystyle\sum_{i=1}^{\infty}\expe(Y|X=x_i)f_X(x_i)		& \quad\text{if $X$ is discrete, or} \\[3ex]
\expe\big[\expe(Y|X)\big] & = \displaystyle\int_{-\infty}^{\infty} \expe(Y|X=x)f_X(x)\,dx	& \quad\text{if $X$ is continuous.}
\end{array}
\]

% LTE
\begin{theorem}[Law of total expectation]
Let $X$ and $Y$ be random variables defined on the same probability space. Then
\[
\expe(Y) = \expe\big[\expe(Y|X)\big].
\]
\end{theorem}
\begin{proof}
For discrete random variables (the continuous case is similar):
\begin{align*}
\expe\big[\expe(Y|X)\big]
	= \sum_x \expe(Y|X=x) f_X(x)
	& = \sum_x\left(\sum_y y\,f_{Y|X}(y|x)\right) f_X(x) \\
	& = \sum_x\left(\sum_y y\,\frac{f_{X,Y}(x,y)}{f_X(x)}\right) f_X(x) \\
	& = \sum_x\left(\sum_y y f_{X,Y}(x,y)\right) \\
	& = \sum_x y \left(\sum_y f_{X,Y}(x,y)\right) \\
	& = \sum_x y f_Y(y)
	= \expe(Y).
\end{align*}
\end{proof}

% LTV
\begin{theorem}[Law of total variance]
If $X$ and $Y$ are random variables defined on the same probability space,
\[
\var(Y) = \expe\big[\var(Y|X)\big] + \var\big[\expe(Y|X)\big]
\]
where $\var(Y|X)=\expe(Y^2|X) - \expe(Y|X)^2$.
\end{theorem}
\begin{proof}
By the law of total expectation,
\begin{align*}
\var(Y)
	& = \expe(Y^2) - \expe(Y)^2 \\
	& = \expe\big[\expe(Y^2|X)\big] - \expe\big[\expe(Y|X)\big]^2
\end{align*}
Because $\var(Y|X)=\expe(Y^2|X) - \expe(Y|X)^2$,
\[
\var(Y) = \expe\big[\var(Y|X) + \expe(Y|X)^2\big] - \expe\big[\expe(Y|X)\big]^2
\]
Hence, by the linearity of expectation,
\begin{align*}
\var(Y)
	& = \expe\big[\var(Y|X)\big] + \Big(\expe\big[\expe(Y|X)^2\big] - \expe\big[\expe(Y|X)\big]^2\Big) \\
	& = \expe\big[\var(Y|X)\big] + \var[\expe(Y|X)\big].
\end{align*}
\end{proof}

% example
\begin{example}
Let the joint PDF of the continuous random variables $X$ and $Y$ be
\[
f_{X,Y}(x,y) = \begin{cases}
	cxy & \quad\text{for $x,y\geq 0$ with $x+y\leq 1$}, \\
	0	& \quad\text{otherwise.}
\end{cases}
\]
\ben
\it Sketch the support of $f_{X,Y}$
\it Show that $c=24$.
\it Compute the conditional expectation $\expe(Y|X)$.
\it Verify the identity $\expe\big[\expe(Y|X)\big]=\expe(Y)$.
\een

\begin{solution}
\ben

\it % << (a)
$\supp(f_{X,Y})$ is the lower-left half of the unit square.

\it % << (b)
The marginal PDF of $X$ is
\[
f_X(x) = c\int_{0}^{1-x} xy\,dy = \frac{cx(1-x)^2}{2}
\]
and similarly for $Y$. To find $c$,
\[
\int_{0}^{1} f_X(x)\,dx = 1, \qquad\text{so}\quad c=24.
\]
Thus
\begin{align*}
f_X(x)	& = 12x(1-x)^2 \qquad 0\leq x\leq 1 \\
f_Y(y)	& = 12y(1-y)^2 \qquad 0\leq y\leq 1 \\
\end{align*}
and
\begin{align*}
\expe(X) & = 12\int_0^1 x(1-x)^2\,dx = 2/5 \\
\expe(Y) & = 12\int_0^1 y(1-y)^2\,dy = 2/5 \\
\end{align*}

\it % << (c)
To compute $\expe(Y|X)$,
\begin{align*}
\expe(Y|X=x)
	= \int_0^1 y\left(\frac{f_{X,Y}(x,y)}{f_X(x)}\right)\,dy
	& = \int_0^{1-x} y\left(\frac{24xy}{12x(1-x)^2}\right)\,dy \\
	& = \frac{24x}{12x(1-x)^2}\int_0^{1-x} y^2\,dy \\
	& =  \frac{24x}{12x(1-x)^2}\left[\frac{(1-x)^3}{3}\right]
	= \frac{2}{3}(1-x)
\end{align*}
so $\expe(Y|X) = 2(1-X)/3$.

\it % << (d)
\begin{align*}
\expe\big(\expe(Y|X)\big)
	& = \int_0^1 \expe(Y|X=x) f_X(x)\,dx \\
	& = \frac{2}{3}\int_0^1 (1-x) f_X(x)\,dx \\
	& = \frac{2}{3}\big(1-\expe(X)\big) = \frac{2}{3}\left(1-\frac{2}{5}\right) = \frac{2}{5}
	= \expe(Y).
\end{align*}
\een
\mbox{}
\end{solution}
\end{example}

% exercises
\begin{exercise}
\begin{questions}
\question
Let $X$ and $Y$ be jointly continuous random variables having the following joint PDF,
\[
f_{X,Y}(x,y) =
\begin{cases}
	\frac{21}{4}x^2y		& \quad x^2<y<1, \\
	0						& \quad\text{otherwise.}
\end{cases}
\]
\ben
\it Sketch the support of $f_{X,Y}$.
\it Find the marginal PDFs of $X$ and $Y$.
\it Find the mean and variance of $Y$.
\it Find the conditional PDF of $Y$ given $X=x$.
\it Are $X$ and $Y$ independent?
\it Find the conditional expectation of $Y$ given $X=x$.
\it Find the conditional expectation of $Y$ given $X$.
\it Verify that $\expe(Y)=\expe\big[\expe(Y|X)\big]$.
\een

\begin{answer}
\ben
\it % << (a)
The support of the joint PDF $f(x,y)$ is the set $\{(x,y): x^2 < y < 1\}$.  This is the region of the plane between the vertical lines $x=-1$ and $x=+1$, bounded above by the horizontal line $y=1$ and below by parabola $y=x^2$. In particular,
\bit
\it For fixed $x\in[-1,1]$, $f_{X,Y}(x,y)\neq 0$ only for $y\in[x^2,1]$.
\it For fixed $y\in[0,1]$, $f_{X,Y}(x,y)\neq 0$ only for $x\in[-\sqrt{y},+\sqrt{y}]$.
\eit

\it % << (b)
The marginal distributions are computed as follows:
\begin{align*}
f_X(x)
	& = \int_{-\infty}^{\infty} f(x,y)\,dy
	= \int_{x^2}^1 \frac{21}{4}x^2y\,dy
	= \frac{21}{4}x^2\left[\frac{y^2}{2}\right]_{x^2}^1
	= \begin{cases} \frac{21}{8}x^2(1-x^4)	& -1<x<1, \\ 0 & \text{ otherwise.}\end{cases} \\ [2ex]
f_Y(y)
	& = \int_{-\infty}^{\infty} f(x,y)\,dx
	= \int_{-\sqrt{y}}^{\sqrt{y}} \frac{21}{4}x^2y\,dx
	= \frac{21}{4}y\left[\frac{x^3}{3}\right]_{-\sqrt{y}}^{\sqrt{y}}
	= \begin{cases} \frac{7}{2}y^{5/2} & 0< y< 1, \\ 0 & \text{ otherwise.}\end{cases} \\
\end{align*}

\it % << (c)
The expected value and variance of $Y$ are computed as follows:
\begin{align*}
\expe(Y)
	& = \int_{-\infty}^{\infty} y\,f_Y(y)\,dy 	= \int_0^1 y\left(\frac{7y^{5/2}}{2}\right)\,dy = \frac{7}{9}, \\
\expe(Y^2)
	& = \int_{-\infty}^{\infty} y^2\,f_Y(y)\,dy = \int_0^1 y^2\left(\frac{7y^{5/2}}{2}\right)\,dy = \frac{7}{11}, \\
\var(Y)
	& = \expe(Y^2) - \expe(Y)^2 = \frac{7}{11} - \frac{49}{81} = \frac{28}{891}.
\end{align*}

\it % << (d)
The conditional PDF of $Y$ given $X=x$ is
\[
f_{Y|X=x}(y)
	= \frac{f_{X,Y}(x,y)}{f_X(x)}
	= \frac{(21/4)x^2y}{(21/8)x^2(1-x^4)}
	= \begin{cases}
		\displaystyle\frac{2y}{1-x^4} 	& x^2\leq y\leq 1, \\
	 	0 								& \text{ otherwise}.
	 \end{cases}
\]

\it % << (e)
$X$ and $Y$ are clearly not independent, because the support of $f_{X,Y}$ is not a rectangular region, and moreover, the conditional PDF of $Y$ given $X=x$ depends on $x$.

\it % << (f)
The conditional expected value of $Y$ given that $X=x$ is
\begin{align*}
\expe(Y|X=x)
	& = \int_{-\infty}^{\infty} y f_{Y|X=x}(y\,|\,x)\,dy \\
	& = \int_{x^2}^1 y \left(\frac{2y}{1-x^4}\right)\,dy
	= \frac{2}{1-x^4}\left[\frac{y^3}{3}\right]_{x^2}^1
	= \frac{2(1-x^6)}{3(1-x^4)}
\end{align*}

\it % << (g)
The conditional expectation of $Y$ given $X$ is the random variable
\[
\expe(Y|X)=\displaystyle\frac{2(1-X^6)}{3(1-X^4)}
\]

\it % << (h)
The expected value of $\expe(Y|X)$ is
\begin{align*}
\expe\big[\expe(Y|X)\big]
	& = \int_{-\infty}^{\infty} \expe(Y|X=x)f_X(x)\,dx \\
	& = \frac{2}{3}\int_{-1}^{1} \left(\frac{1-x^6}{1-x^4}\right)\left(\frac{21}{8}x^2(1-x^4)\right)\,dx \\
	& = \frac{7}{4}\int_{-1}^{1} x^2(1-x^6)\,dx
	= \frac{7}{9}.
\end{align*}
Thus $\expe\big[\expe(Y|X)\big]=\expe(Y)$ as required.
\een
\end{answer}

% house for sale
\question
A man puts his house for sale and decides to accept the first offer that exceeds the reserve price of $r$. Let $Y_1,Y_2,\ldots$ represent the sequence of offers received, and suppose that the $Y_i$ are independent and identically distributed random variables, each having exponential distribution with rate parameter $\lambda$.
\begin{parts}
\part
Show that the expected number of offers received before the house is sold is $e^{\lambda r}$.
\begin{answer}
Let $N$ be the number of offers received before the house is sold. Then $\{N=k\}$ is the event that the first $k-1$ offers are at most $r$, each occurring independently with probability $F(r)$, and the $k$th offer exceeds $r$, which occurs with probability $1-F(r)$. Thus $N$ has \emph{geometric} distribution, with `probability of success' equal to $1-F(r)$ (where `success' corresponds to the sale of the house). Hence the expected number of offers received before the house is sold is
\[
\expe(N) = \frac{1}{1-F(r)} = e^{\lambda r}.
\]
\end{answer}
\part
Show that the expected the expected selling price of the house is $r+1/\lambda$.
\begin{answer}
Let $Y\sim\text{Exponential}(\lambda)$ and let $A=\{Y>r\}$. The con
Let $F_S$ be the conditional CDF of $Y_i$ given that $X_i>r$:
\[
F_Y|A(y)
	= \frac{\prob(r < Y \leq y)}{\prob(Y > r)}
	= \frac{F(y)-F(r)}{1-F(r)}
	= \begin{cases}
		1 - e^{-\lambda(y-r)} 	& y > r, \\
		0						& \text{otherwise.}
	\end{cases}
\]
A straightforward calculation yields $\expe(Y|Y>r) = r + 1/\lambda$.
\end{answer}
\end{parts}

\question
\textbf{Compound distributions}. If $Y\sim\text{Poisson}(\lambda)$ then $\expe(Y)=\lambda$ and $\var(Y)=\lambda$. The Poisson distribution has only a single parameter so we cannot shift and scale the distribution by different ammounts, which limits its usefulness in certain practical applications. This problem can be addressed by allowing the parameter itself to be a random variable.

\begin{parts}
\part % exp
Let $Y\sim\text{Poisson}(X)$ where $X\sim\text{Exponential}(\theta)$ and $\theta>0$ is a fixed scale parameter. Use the laws of total expectation and total variance, and the fact that $\expe(X)=\theta$ and $\var(X)=\theta^2$, to show that $\expe(Y)=\theta$ and $\var(Y)=\theta(1+\theta)$.
\begin{answer}
The conditional expectation and conditional variance of $Y$ are
\[
\expe(Y|X) = X \quad\text{and}\quad \var(Y|X) = X.
\]
By the law of total expectation
\[
\expe(Y) = \expe\big[\expe(Y|X)\big] = \expe(X) = \theta,
\]
and by the law of total variance,
\begin{align*}
\var(Y)
	& = \expe\big[\var(Y|X)\big] + \var\big[\expe(Y|X)\big] \\
	& = \expe(X) + \var(X) \\
	& = \theta + \theta^2
	= \theta(1+\theta)
\end{align*}
because $\expe(X)=\theta$ and $\var(X)=\theta^2$ for $X\sim\text{Exponential}(\theta)$.
\end{answer}

\part % gamma
Let $Y\sim\text{Poisson}(X)$ where $X\sim\text{Gamma}(\alpha,\beta)$ for some fixed parameters $\alpha,\beta>0$. Find values for $\alpha$ and $\beta$ such that $\expe(Y)=2$ and $\var(Y)=9$.
\begin{answer}
Because $Y\sim\text{Poisson}(X)$ we have $\expe(Y|X)=X$ and $\var(Y|X)=X$, so both are distributed according to the $\text{Gamma}(\alpha,\beta)$ distribution, whose mean and variance are $\alpha/\beta$ and $\alpha/\beta^2$ respectively.

By the laws of total expectation and total variance,
\begin{align*}
\expe(Y)
	& = \expe\big[\expe(Y|X)\big] = \expe(X) = \alpha/\beta \\
\var(Y)
	& = \expe\big[\var(Y|X)\big] + \var\big[\expe(Y|X)\big] = \expe(X) + \var(X) = \alpha(\beta+1)/\beta^2.
\end{align*}
Solving $\alpha/\beta=2$ and $\alpha(\beta+1)/\beta^2=9$ yields $\alpha=4/7$ and $\beta=2/7$.

\bigskip
If $\alpha$ is an integer, $Y$ has the so-called $\text{NegativeBinomial}(r,p)$ distribution with $r=\alpha$ and $p=1/(1+\beta)$. This is the distribution of the number of successes up to the $r$th failure in a sequence of independent Bernoulli trials where each trial has probability of success $p$.
\end{answer}
\end{parts}
\end{questions}
\end{exercise}


\section{Limit Theorems}
\subsection{Law(s) of Large Numbers}
\begin{theorem}[Weak Law of Large Numbers]
    Let $(X_n:n\in\mathbb N)$ be an iid sequence of random variables with finite expectation $\mu$.
    Set $S_n=X_1+\cdots+X_n$, then for any $\epsilon>0$, we have
    $$\mathbb P\left( \left|\frac{S_n}{n}-\mu\right|>\epsilon \right)\to 0$$
    as $n\to\infty$.
\end{theorem}
We shall prove this assuming $\operatorname{X_1}=\sigma^2<\infty$.
\begin{proof}
    We have $\mathbb E[S_n/n]=\mu$ and $\operatorname{Var}(S_n/n)=\sigma^2/n$, then by Chebyshev's Inequality,
    $$\mathbb P\left(\left|\frac{S_n}{n}-\mu\right|>\epsilon\right)\le\frac{\operatorname{Var}(S_n/n)}{\epsilon^2}=\frac{\sigma^2}{\epsilon^2n}\to 0$$
    As $n\to\infty$.
\end{proof}
\begin{definition}
    A sequence $(X_n)$ converges to $X$ in probability, written as
    $$X_n\xrightarrow{\mathbb P}X,n\to\infty$$
    if $\forall\epsilon>0,\mathbb P(|X_n-X|>\epsilon)\to 0$ as $n\to\infty$.
\end{definition}
So the weak law of large numbers says that $S_n/n\xrightarrow{\mathbb P}\mu$ as $n\to\infty$.
\begin{definition}
    $(X_n)$ converges to $X$ with probability $1$ (or ``almost surely'', a.s.) if
    $$\mathbb P\left(\lim_{n\to\infty}X_n=X\right)=1$$
\end{definition}
\begin{theorem}[Strong Law of Large Numbers]
    Let the setting be as before, then
    $$\mathbb P\left(\lim_{n\to\infty}\frac{S_n}{n}\to\mu\right)=1$$
\end{theorem}
\begin{proof}
    Assume further that the $4^{th}$ moment is finite.
    BY considering $Y_i=X_i-\mu$, WLOG $\mu=0$.
    We have
    $$S_n^4=\left( \sum_{i=1}^nX_i \right)^4=\sum_{i=1}^nX_i^4+6\sum_{1\le i<j\le n}X_i^2X_j^2+R$$
    where $R$ is a sum of terms of the form $X_i^3X_j,X_i^2X_jX_k,X_iX_jX_kX_l$ for $i,j,k,l$ all distinct.
    Since $X_i$ are independent with zero mean, $\mathbb E[R]=0$, so
    \begin{align*}
        \mathbb E[S_n^4]&=n\mathbb E[X_1^4]+3n(n-1)\mathbb E[X_1^2]^2\\
        &\le (n+3n(n-1))\mathbb E[X_1^4]\\
        &\le 3n^2\mathbb E[X_1^4]
    \end{align*}
    Hence
    $$\mathbb E\left[ \sum_{n=1}^\infty\left( \frac{S_n}{n} \right)^4 \right]=\sum_{n=1}^\infty\mathbb E\left[ \left( \frac{S_n}{n} \right)^4 \right]\le 3\mathbb E[X_1^4]\sum_{n=1}^\infty\frac{1}{n^2}<\infty$$
    So
    $$\mathbb P\left(\sum_{n=1}^\infty\left( \frac{S_n}{n} \right)^4<\infty\right)=1\implies\mathbb P\left(\lim_{n\to\infty}\frac{S_n}{n}=0\right)=1$$
    as claimed.
\end{proof}
As the name suggests, he strong law of large numbers implies the weak law of large numbers.
\begin{proposition}
    Suppose $X_n\to\mu$ almost surely, then $X_n\xrightarrow{\mathbb P}\mu$
\end{proposition}
By shifting, it suffices to consider the case where $\mu=0$.
\begin{proof}
    Assuming $X_n\to 0$ almost surely, then we have
    $$\mathbb P(|X_n|\le\epsilon)\ge\mathbb P\left( \bigcap_{m=n}^\infty\{|X_m|\le\epsilon\} \right)$$
    write the event in the right hand side as $A_n$, then $A_n\subset A_{n+1}$ and $\bigcup_nA_n$ is the event that $|X_m|\le\epsilon$ for any sufficiently large $m$.
    So
    $$\lim_{n\to\infty}\mathbb P(|X_n|\le\epsilon)\ge\mathbb P\left( \bigcup_{n\in\mathbb N}A_n \right)\ge\mathbb P(X_n\to 0)=1$$
    So $\mathbb P(|X_n|<\epsilon)=0$.
\end{proof}
\subsection{Central Limit Theorem}
We saw $S_n/n-\mu\to 0$ a.s. from the law of large numbers.
We know also that $\operatorname{Var}(S_n/n)=\sigma^2/n$ where $\sigma^2$ is the variance of $X_1$.
So if we want to normalize,
$$\frac{S_n/n-\mu}{\sqrt{\operatorname{Var}(S_n/n)}}=\frac{S_n-n\mu}{\sigma\sqrt{n}}$$
from which we will expect
\begin{theorem}[Central Limit Theorem]
    Let $(X_n:n\in\mathbb N)$ be a sequence of i.i.d. random variables with mean $\mu$ and variance $\sigma^2$, then set $S_n=X_1+\cdots+X_n$ as before, we have
    $$\mathbb P\left( \frac{S_n-n\mu}{\sigma\sqrt{n}}\le x \right)\to\Phi(x)=\int_{-\infty}^x\frac{e^{-y^2/2}}{\sqrt{2\pi}}\,\mathrm dy$$
    for all $x\in\mathbb R$.
\end{theorem}
In other words, $(S_n-n\mu)/(\sigma\sqrt{n})\to\mathcal N(0,1)$ in distribution.
What this means is that for $n$ large enough, $S_n\approx n\mu+\sigma\sqrt{n}\mathcal N(0,1)=\mathcal N(\mu n,\sigma^2n)$.
In fact, not only does it converge, we can also estimate the rate of convergence, which is sadly beyond the course.
\begin{proof}
    WLOG $\mu=0,\sigma=1$ by considering $(X_i-\mu)/\sigma$.
    Assume further that $\exists\delta>0,\mathbb E[e^{\pm\delta X_1}]<\infty$.
    By continuity of MGFs, it suffices to show that, for $Z\sim\mathcal N(0,1)$,
    $$\mathbb E[e^{\theta S_n/\sqrt{n}}]\to\mathbb E[e^{\theta Z}]=e^{\theta^2/2}$$
    So if let $m$ be the MGF of $X_1$,
    $$\mathbb E[e^{\theta S_n/\sqrt{n}}]=\mathbb E[e^{\theta X_n/\sqrt{n}}]^n=m(\theta/\sqrt{n})^n$$
    Note that when $|\theta|<\delta/2$,
    $$m(\theta)=\mathbb E\left[ \sum_{n=0}^\infty\frac{1}{n!}\theta^nX_1^n \right]=1+\frac{\theta^2}{2}+\mathbb E\left[  \sum_{n=3}^\infty\frac{1}{n!}\theta^nX_1^n \right]$$
    We will prove that the last term is $o(\theta^2)$ as $\theta\to 0$, which immediately implies the result.\\
    We have
    \begin{align*}
        \sum_{n=3}^\infty\frac{1}{n!}|\theta|^n|X_1|^n&=|\theta X_1|^3\sum_{k=0}^\infty\frac{|\theta X_1|^k}{(k+3)!}\\
        &\le|\theta X_1|^3e^{\delta|X_1|/2}\\
        &\le 3!\left( \frac{2\theta}{\delta} \right)^3e^{\delta|X_1|}
    \end{align*}
    Now by Jensen's Inequality
    \begin{align*}
        \left|\mathbb E\left[  \sum_{n=3}^\infty\frac{1}{n!}\theta^nX_1^n \right]\right|&\le\mathbb E\left[ \sum_{n=3}^\infty\frac{1}{n!}|\theta|^n|X_1|^n \right]\\
        &\le 3!\left( \frac{2\theta}{\delta} \right)^3\mathbb E[e^{\delta|X_1|}]\\
        &\le 3!\left( \frac{2\theta}{\delta} \right)^3(\mathbb E[e^{\delta|X_1|}]+\mathbb E[e^{-\delta X_1}])\\
        &=o(|\theta|^2)
    \end{align*}
    As desired.
\end{proof}
There are few important application of central limit theorem.
\begin{example}
    1. Let $(X_n)$ be i.i.d. $\operatorname{Bern}(p)$ and hence $S_n\sim\operatorname{Bin}(n,p)$.
    Recall that $\mathbb E[S_n]=np$ and $\operatorname{Var}(S_n)=np(1-p)$, so
    $$\frac{S_n-np}{\sqrt{np(1-p)}}\to\mathcal N(0,1)$$
    in distribution.
    So for $n$ large, $S_n\approx\mathcal N(np,np(1-p))$ for $n$ large.
    In the Poisson approximation of the binomial, we scaled $p$ to $\lambda/n$, while in this approximation, we kept $p$ constant.\\
    2. We now want $S_n\sim\operatorname{Pois}(n)$, and to accomplish this we can write $S_n=X_1+\cdots+X_n$ where $(X_n)$ are i.i.d. $\operatorname{Pois}(1)$.
    So $(S_n-n)/\sqrt{n}\approx \mathcal N(0,1)$.
\end{example}
\subsection{Sampling Error by Central Limit Theorem}
A proportion $p$ of the population votes ``yes'' and $1-p$ votes ``no'' in a referendum.
We want to estimate $p$ with error at most $\pm 4\%$ in probability at least $0.99$.
We pick $N$ individuals at random.
Let $S_N$ be the number of people who voted ``yes'', so we want to estimate $p$ by $\hat{p}_N=S_N/N$, so what we want is
$$\mathbb P(|\hat{p}_N-p|\le 4\%)\ge 0.99$$
Now $S_N\sim\operatorname{Bin}(N,p)$, so by previous,
$$\hat{p}_N=\frac{S_N}{N}\approx p+\sqrt{\frac{p(1-p)}{N}}Z,Z\sim\mathcal N(0,1)$$
for $N$ large.
So what we want is
$$\mathbb P\left( \sqrt{\frac{p(1-p)}{N}}|Z|\le 4\% \right)\ge 0.99$$
Now $\mathbb P(Z\ge z)=2(1-\Phi(z))$, then $\mathbb P(|Z|\ge 2.58)=0.01$.
So in the worse case where $p=1/2$ gives $N\ge 1040$.


\documentclass[../PHYS306Notes.tex]{subfiles}

\begin{document}
\section{Lecture 16}
\subsection{Lecture Notes - Focault Pendulum}
\subsubsection{Rotating Coordinate System EOM}
\[
\begin{array}{c}
\left(\frac{d \v{Q}}{d t}\right)_{S_{0}}=\left(\frac{d \v{Q}}{d t}\right)_{S}+\vec{\omega} \times \v{Q} \\
m \ddot{\v{r}}=\vec{F}+2 m \dot{\v{r}} \times \v{\Omega}+m(\v{\Omega} \times \v{r}) \times \v{\Omega}+m \v{r} \times \dot{\vec{\Omega}}=\v{F}+\v{F}_{\text {coriolis }}+\v{F}_{\text {centrifugal }}+\v{F}_{\text {Euler }}
\end{array}
\]

\subsubsection{Review Questions}
A bead rests on a wire that extends from the origin at an angle $\theta$ to the vertical. The wire rotates with angular velocity $\Omega$ about the vertical. In the frame rotating with the wire, what is the magnitude and direction of the centrifugal force when the bead is a distance $r$ from the origin?
\begin{s}
By trigonometry, the bead is $r\sin\theta$ away from the rotation axis, so there is a centrifugal force of $m\Omega^2r\sin\theta$ away from the rotation axis.
\end{s}

A bucket of water spins about its central axis. After a relaxation time, the shape of the water surface reaches a steady state. Where is the water surface highest?
\begin{s}
At the edge of the bucket, as the centrifugal force pulls the water towards the edge. The surface of the water is a parabola.
\end{s}
\begin{center}
    \includegraphics[scale=0.5]{Lecture-16/l16-img1.png}
\end{center}
A puck slides from the center towards the edge of a frictionless, rotating merry-go-round. The merry-go-round has angular velocity $\Omega$ and rotates CCW when viewed from above. In the rotating frame, the initial velocity is in the positive $y$ direction. What effect does the Coriolis force have on the velocity of the puck?
\begin{s}
The coriolis force changes the direction of the velocity (deflected left) but does not change the magnitude (we can see from the $\v{v} \times \v{\Omega}$ form that the force does no work).
\end{s}

Consider the same scenario as the previous question. How many rotations does the merry-go-round make before the puck slides off of the edge?

\begin{s}
$\# \text{rotations} = \frac{a\Omega}{2\pi v}$. First consider that the time to reach the edge is simply the distance $a$ (radius) divided by the velocity $v$ of the puck. Then, we may divide this time by the time per rotation (the period), which is $T = \frac{2\pi}{\Omega}$. This yields:
\[\# \text{rotations} = \frac{\Delta t}{T} = \frac{a\Omega}{2\pi v}\]
\end{s}

At which of these points will a person's measured weight be the largest (equator, 30, 40, 60 degrees latitude, or north pole)
\begin{s}
At the north pole; there we have no centrifugal force there (which acts against the gravitational force and decreases the weight of the person).
\end{s}

In the northern hemisphere, which directions are winds from the north and south deflected by the Coriolis force?
\begin{s}
Winds from the N are deflected E and winds from the S are deflected W.
\end{s}

Where are these low pressure areas?
\begin{center}
    \includegraphics[scale=0.5]{Lecture-16/l16-img2.png}
\end{center}
\begin{s}
The left image rotates clockwise, the right one rotates counterclockwise. For a low pressure system, we have air coming in, and in the northern hemisphere, we have right deflection (and vise versa for the southern hemisphere) so we would expect a counterclockwise motion for the northern hemisphere and clockwise for the southern hemisphere.
\end{s}

\subsubsection{The Foucault Pendulum}
We start with our general expression:
\[m\ddot{\v{r}} = \v{F} + 2m\dot{\v{r}}\times \v{\Omega} + m(\v{\Omega} \times \v{r})\times \v{\Omega} + m\v{r}\times \dot{\v{\Omega}}\]
The third term can be neglected as the Earth spins at a constant rate, and the second term can be neglected as $\Omega$ is small. Define $x$ to be north south, $y$ to be east west. $\v{F}$ is the sum of the tension and the gravitational force, that is:
\[\v{F} = \v{T} + m\v{g}\]
Now we consider the given picture:
\begin{center}
    \includegraphics[scale=0.5]{Lecture-16/l16-img4.png}
\end{center}
By similar triangles:
\[T_x = -T\frac{x}{L}\]
\[T_y = -T\frac{y}{L}\]
\[T_z = -T\frac{z-L}{L}\]
But for the $T_z$, we can consider that we do small amplitudes, so $z \approx 0$, and $\dot{z} \approx 0$. Hence, $T_z \approx T \approx mg$. Hence, we have dealt with the tension. Now, we think about $\v{\Omega}$.
\begin{center}
    \includegraphics[scale=0.5]{Lecture-16/l16-img3.png}
\end{center}
If our latitude is $\lambda$ and we use coordinates such that $x$ points towards the equator, $y$ points parallel to the latitude line, and $z$ points away from the center of the earth, what is the rotation vector in this coordinate system?
\begin{s}
We know that $\v{\Omega}_{earth}$ points straight upwards. Projecting this, we get:
\[\v{\Omega} = \omega\m{-\cos\lambda \\ 0 \\ \sin\lambda}\]
\end{s}
Next, what is $\v{\Omega} \times \v{v}$?
\begin{s}
Using that $\v{v} = \m{\dot{x} \\ \dot{y} \\ 0}$ (Assume $\dot{z}$ is negligible) and $\v{\Omega}$ from above, we get:
\[\v{\Omega} \times \v{v} = \m{-\dot{y}\Omega \sin\lambda \\ \dot{x}\Omega\sin\lambda \\ -\dot{y}\Omega\cos\lambda}\]
\end{s}
Now, putting together the equations of motion for $x$ and $y$ we get:
\[m\ddot{x} = -mg\frac{x}{L} + 2m\Omega\sin\lambda \dot{y}\]
\[m\ddot{y} = -mg\frac{y}{L} - 2m\Omega\sin\lambda\dot{x}\]
We have a system of coupled equations. As a trick, multiply both equations by $i$ and add them together, and define $s = x + iy$. We then have:
\[\ddot{s} + 2i\alpha \dot{s} + k^2\dot{s} = 0\]
Where $\alpha = \Omega\sin\lambda$, $k^2 = \frac{g}{L}$. To solve this differential equation, we guess $s(t) = c\exp(\gamma t)$. This yields a characteristic equation:
\[\gamma^2 + 2i\alpha\gamma + k^2 = 0\]
Solving this, we get:
\[\gamma_{1/2} = -i\alpha \pm i\sqrt{\alpha^2 + k^2}\]
The general solution is the sum of these two:
\[s(t) = C_1\exp(\gamma_1 t) + C_2\exp(\gamma_2 t)\]
We assume initial conditions of $s(0) = \xhat, \dot{s}(0) = 0$ (elongation along $x$, with no initial velocity). We may then solve for the coefficients $C_1, C_2$ (homework!). Then, taking the real part of the complex solution, we get the final result:
\[\m{x(t) \\ y(t)} = \m{\cos(\alpha t) & \sin(\alpha t) \\ -\sin(\alpha t) & \cos(\alpha t)}\m{\xhat \cos(\sqrt{\alpha^2 + k^2} t) \\ \xhat\frac{\sin(\sqrt{\alpha^2 + k^2}t)\alpha }{\sqrt{\alpha^2 + k^2}}}\]
Question: We have this result. What is the effect of multiplying through by this matrix?
\begin{s}
We recognize this just as a rotation matrix, rotating by a time dependent angle $\Omega_zt = \alpha t$.
\end{s}
This is a characteristic feature of the Focault pendulum, indeed that it precesses.
\end{document}


\section{Algorithms}
\label{title}
Algorithsm are a process or rules to follow. They should:
\begin{itemize}
	\item take an input, give an output. For example \ldots
	\item be deterministic, i.e. for the same input the output will always be the same, cf functional programming
	\item and finite, cf computability.
\end{itemize}

They could be recursive, then they need a base case and recursive step. Typically the recursive step makes the problem smaller, converging on the base case.

An algorithm could:
\begin{itemize}
	\item involve \textbf{heuristics}, finding decent solutions quickly, but not sure to be optimal
	\item be greedy, so exploit strong local solutions, with no regard to the bigger picture, e.g. hill-climbing
	\item ad-hoc, an impromptu strategy, unique to a particular problem, hard to generalise.
\end{itemize}

\subsection{Flowcharts}

Shapes

\subsection{Orders of Growth}

Order of growth - worst case, proportional to some function of the input size

Comparison is done in extreme cases, e.g. n log n vs n2

Offline vs online problems

Bubble sort, then shuttle
Quick sort

Packing : next fit, first fit, first fit decreasing, full bin

Bin Packing - as a decision problem - is NP Complete

Knapsack problem  - portfolio management,