\documentclass{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}
\usepackage{listings}

\begin{document}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: {\bf \Large Work only on this sheet} (on both sides, if
needed); do not turn in any supplementary sheets of paper. There is
actually plenty of room for your answers, as long as you organize
yourself BEFORE starting writing.

{\bf 1.} (25) Consider the simple board game, pp.15ff, starting at 0.
Change the game so that it has 16 squares, numbered 0 to 15, but is
otherwise identical to the original one.  Let X denote the square the
player lands on after the first turn.  Find E(X), expressing your answer
as a sum of fractions, e.g. 3/2 + 1/5 (-3/8).

{\bf 2.} (25) Supppose we have a random variable X, and define a new
random variable Y, which is equal to X if X $>$ 8 and equal to 0
otherwise.  Assume X takes on only a finite number of values (just a
mathematical nicety, not really an issue).  Which one of the following
is true:

\begin{itemize}

\item [(i)] $EY \leq EX$.

\item [(ii)] $EY \geq EX$.

\item [(iii)] Either of $EY$ and $EX$ could be larger than the other,
depending on the situation.

\item [(iv)] $EY$ is undefined.

\end{itemize}

{\bf 3.} This problem concerns the binary tree model in our homework.

\begin{itemize}

\item [(a)] (25) Find the probability that the root has exactly 1
grandchild, expressing your answer in terms of p, algebraically
simplified.

\item [(b)] (25) Fill in the blanks in the following code simulating the
function r(k,p):

\begin{lstlisting}
sim1tree <- function(k,p) {
   if (k == 0) return(           )                           # blank
   prevlevelbranches <- 1
   for (m in 1:      ) {                                     # blank
      newbranches <- 0
      for (i in 1:                 ) {                       # blank
         for (j in 1:2) {  
            if (sample(0:1,1,prob=c(1-p,p)) == 1) {
               newbranches <- newbranches + 1
            }
         }
      }
      if (newbranches == 0) return(0)
                                                             # blank
   }
   return(1)
}

treesim <- function(p,k,nreps) {
   count <- 0
   for (i in 1:nreps) {
      treetok <- sim1tree(k,p)
      count <- count + treetok
   }
   return(         )                                         # blank
}
\end{lstlisting}

\end{itemize}


{\bf Solutions:}

{\bf  1.}  X can take on the values 1 through 9.  Then 

\begin{equation}
EX = 1 \cdot P(X = 1) + 2 \cdot P(X = 2) + ... + 9 \cdot P(X = 9)
\end{equation}

$P(X = 1) = \frac{1}{6}$, 
$P(X = 4) = \frac{1}{6} + \frac{1}{36}$, etc. 

{\bf 2.} Answer (iii) is correct.  If we'd had the additional condition
$X \geq 0$, then (i) would have been right.  But without that condition,
then for instance suppose X were always negative; then Y would always be
larger, etc.

{\bf 3.a}  The root will have exactly one grandchild iff it has two
children, and one of them has one child and the other has none.  Thus
the queried probability is

\begin{equation}
p^2 \cdot [2 p (1-p)] = 2p^3(1-p)
\end{equation}

{\bf 3.b}

\begin{lstlisting}
sim1tree <- function(p,k) {
   if (k == 0) return(1)
   prevlevelbranches <- 1
   for (m in 1:k) {  # levels
      newbranches <- 0
      for (i in 1:prevlevelbranches) {
         for (j in 1:2) {  # account for left, right outlinks
            if (sample(0:1,1,prob=c(1-p,p)) == 1) {
               newbranches <- newbranches + 1
            }
         }
      }
      if (newbranches == 0) return(0)
      prevlevelbranches <- newbranches
   }
   return(1)
}

treesim <- function(p,k,nreps) {
   count <- 0
   for (i in 1:nreps) {
      treetok <- sim1tree(p,k)
      count <- count + treetok
   }
   return(count/nreps)
}
\end{lstlisting}


\end{document}

