\documentclass[twocolumn]{article}
% \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}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

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

Directions: MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\bf 1.} (25) Consider the bus ridership example, Section 2.12. State in
which ones of the following pairs the events $A$ and $B$ are disjoint.
Your answer might be, (i) and (v), for instance.

\begin{itemize}

\item [(i)]  
$A$ is $B_1 = 2$, $B$ is $B_2 = 2$.

\item [(ii)] 
$A$ is $B_1 = 0$, $B$ is $L_2 = 1$.

\item [(iii)] 
$A$ is $B_1 = 1$, $B$ is $L_2 = 1$.

\item [(iv)]  
$A$ is $B_1 = 0$, $B$ is $B_2+B_3 = 4$.

\item [(v)] 
$A$ is $L_3 = 0$, $B$ is $\textrm{two people alight at stop 4}$.

\end{itemize}

{\bf 2.} (25) Again consider the bus ridership example, Section 2.12.
Find $P(B_1 = 0 ~|~ L_2 = 0)$.

{\bf 3.} (25) The function {\bf alohasim()} below simulates {\bf nreps}
repetitions of the ALOHA model for {\bf nepochs} epochs, {\bf nnodes}
nodes, with {\bf p}, {\bf q} as in the book. The return value is a
matrix of {\bf nepochs} rows and {\bf nreps} columns, showing the number
of active nodes at the end of each epoch.  
(25) Fill in the blanks.

\begin{lstlisting}
alohasim <- 
   function(nreps,nepochs,nnodes,p,q) {
   replicate(nreps,
      simepochs(nepochs,nnodes,p,q))
}

simepochs <- function(nepochs,nnodes,p,q) {
   # make space for results
   nactivevals <- vector(length=nepochs)
   # initial condtion
   nactive <- nnodes
   for (i in 1:  blank (a)  ) {
      if (nactive < nnodes) {
         ninactive <- nnodes - nactive
         nactive <- 
            nactive + nheads(ninactive,  
               blank (b) )
      }
      ntrysend <-  blank (c)
      if (ntrysend ==  blank (d)  )
         nactive <- nactive - 1
      nactivevals[i] <- nactive
   }
   # R auto returns last value computed
   nactivevals  
}

# simulate tossing nc coins, each with 
# probability r of heads; return number 
# of heads
nheads <- function(nc,r) {
    tmp <- runif(nc)
    sum(  blank (e)  )
}
\end{lstlisting}

{\bf 4.} (25) (Continuation of Problem 3.) State, using symbols like
P(), X5 and so on, what approximate probability the following is
finding.  

\begin{lstlisting}
z <- alohasim(5000,2,2,0.4,0.8)
w <- which(z[1, ] == 1)
z1 <- z[ ,w]
mean(z1[2,] == 2)
\end{lstlisting}

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.} (v)

{\bf 2.}  

% Just make small changes to the computation of $P(B_1 = 2 | L_2
% = 0)$ in the text.  $P(B_1 = 0) = 0.5$, and 
% $P(L_2 = 0 | B_1 = 0) = 1$.

\begin{equation}
P(B_1 = 0 ~|~ L_2 = 0)
= 
\frac{P(B_1 = 0, L_2 = 0)}{P(L_2  = 0)}
\end{equation}

The denominator was already computed in the textbook, as 0.292.  The
numerator is

\begin{equation}
P(B_1 = 0) ~ P(L_2 = 0 ~|~ B_1 = 0) = 0.5^2
\end{equation}

So the answer is $0.5^2 / 0.292$.

{\bf 3.a}

\begin{lstlisting}
alohasim <- function(nreps,nepochs,nnodes,p,q) {
   replicate(nreps,simepochs(nepochs,nnodes,p,q))
}

# simulates and records nepochs epochs; return value has the number of
# active nodes at the end of each epoch
simepochs <- function(nepochs,nnodes,p,q) {
   # make space for results
   nactivevals <- vector(length=nepochs)
   # initial condtion
   nactive <- nnodes
   # simulate the epochs
   for (i in 1:nepochs) {
      if (nactive < nnodes) {
         ninactive <- nnodes - nactive
         nactive <- nactive + nheads(ninactive,q)
      }
      ntrysend <- nheads(nactive,p)
      if (ntrysend == 1)
         nactive <- nactive - 1
      nactivevals[i] <- nactive
   }
   nactivevals  # R auto returns last value computed
}

# simulate tossing nc coins, each with probability r of heads; return
# number of heads
nheads <- function(nc,r) {
    tmp <- runif(nc)
    sum(tmp < r)
}
\end{lstlisting}

{\bf 3.b} $P(X_2 = 2 ~|~ X_1 = 1)$

\end{document}
