\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.} (20) For a certain random variable $X$, 

$$
P(X = i) = ci, ~ i = 1,2
$$

What is the value of $c$?

{\bf 2.} (20) The {\bf matrix()} function in R has an optional argument
{\bf byrow}.  Here is an example:

\begin{lstlisting}
> matrix(c(5,0,2,0,0,6,8,88,168),ncol=3,
     byrow=TRUE)
     [,1] [,2] [,3]
[1,]    5    0    2
[2,]    0    0    6
[3,]    8   88  168
\end{lstlisting}

Fill in the blank:  This argument is used to conveniently input by rows,
even though R uses \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ storage.

{\bf 3.} Consider the 3-heads-in-a-row example of Markov chains, Sec.\
5.4.

\begin{itemize}

\item [(a)] (20) Look at the code on p.106, and think of what happens during the
call to {\bf findpi1()}.  The latter calls {\bf solve()}.  State the
value of {\bf imp} just before that call.

\item [(b)] (10) Suppose spectators are watching this game, and every
time the player tosses a tail after having two consecutive heads, the
crowd moans, ``Oh no!'', since the player came so close to winning but
did not win.  After the player has tossed the coin 10000 times,
approximately how many times will the crowd have said ``Oh no!''?

\end{itemize}

{\bf 4.} (15) Consider once again our Bus Ridership exmaple, with the
constraint added in Sec.\ 5.8 that the bus has a capacity of 20
passengers.  But let's change the distribution of the $B_i$ to be Poisson
with $\lambda$ = 0.1.  Find $p_{2,2}$.

{\bf 5.} (15) We wish to have a function that, for a given Markov
transition matrix {\bf p} and given pair of states {\bf r} and {\bf s}
will find $P(X_{i+k} = s ~|~ X_i = r$) for any nonnegative integer $k$.
Fill in the blanks:

\begin{lstlisting}
> pkrs <- function(p,k,r,s) {
   pwr <- p
   if (k > 1)
      for (i in 1:(k-1)) 
         pwr <- # blank (a)
   # blank (b)
}

\end{lstlisting}

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.} The probabilities must sum to 1, so $c = 1/3$.

{\bf 2.}  column-major

{\bf 3.a}

$$
\left (
\begin{array}{ccc}
0.5 & -0.5 & -1 \\
-0.5 & 1 & 0 \\
1 & 1 & 1 
\end{array}
\right )
$$

{\bf 3.b} 710

{\bf 4.}

%$$
% 0.2 (1-0.1)^{1-1} 0.1 +
% 0.2^2 (1-0.1)^{2-1} 0.1 
%$$

\begin{lstlisting}
dbinom(0,2,0.2) * dpois(0,0.1) + dbinom(1,2,0.2) * dpois(1,0.1) + dbinom(2,2,0.1) * dpois(2,0.2) 
\end{lstlisting}

{\bf 5.}

\begin{lstlisting}
> pkrs
function(p,k,r,s) {
   pwr <- p
   if (k > 1)
      for (i in 1:(k-1)) 
         pwr <- pwr %*% p
   pwr[r,s]
}
\end{lstlisting}

\end{document}

