\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% Main maths packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{mathtools}

\begin{document}

\section*{Piecewise functions}

\subsection*{The environment \emph{cases}}

The best practices to define piecewise functions is to the environment \emph{cases}.

\[
	\operatorname{abs}(x)=
	\begin{cases}
		-x	& \text{if } x < 0 \\
		x	& \text{if } x \ge 0
	\end{cases}
\]

\subsection*{The manual way using \emph{aligned}}

Piecewise functions can also be done using a brace and the environment \emph{aligned}.

\[
	\operatorname{abs}(x)=
	\left\{
	\begin{aligned}
		-x	& : x < 0 \\
		x	& : x \ge 0
	\end{aligned}
	\right.
\]

Within cases, \emph{text} style math is used with results such as:

\[
	f(x)=
	\begin{cases}
		\int_{0}^{10} x\, dx & \text{if } x \ge 0 \\
		\frac{1}{x} & \text{if } x < 0
	\end{cases}
\]

\emph{Display} style may be used instead, by using the environment \emph{dcases} with the package \emph{mathtools}:

\[
	f(x)=
	\begin{dcases}
		\int_{0}^{10} x\, dx & \text{if } x \ge 0 \\
		\frac{1}{x} & \text{if } x < 0
	\end{dcases}
\]

Often the second column consists mostly of normal text. To set it in the normal font of the document, the \emph{dcases*} environment may be used:

\[
	f(x)=
	\begin{dcases*}
		\int_{0}^{10} x\, dx & when $x$ is even \\
		\frac{1}{x} & when $x$ is odd
	\end{dcases*}
\]

\subsection*{The manual way using \emph{array}}

Piecewise functions can also be done using a brace and the environment \emph{array}. This enables to control the alignment of the elements.

\[
	\operatorname{abs}(x)=
	\left\{
	\begin{array}{lr}
		-x	& : x < 0 \\
		x	& : x \ge 0
	\end{array}
	\right.
\]

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS671: Machine Learning
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 4}

Let $\mathcal{C}_\infty$, $\mathcal{C}_\epsilon$ be two collections of sets.
Define  $\mathcal{C}_\infty \wedge \mathcal{C}_\epsilon = \{ \mathcal{C}_\infty \cap \mathcal{C}_\epsilon | \mathcal{C}_\infty \in \mathcal{C}_\infty, \mathcal{C}_\epsilon \in \mathcal{C}_\epsilon \}$.
Show that $\prod_\mathcal{C}(m) \leq \prod_{\mathcal{C}_\infty}(m) \prod_{\mathcal{C}_\epsilon}(m)$.

\subsection*{Solution}

We assume an arbitrary set $X$ of size $m$.
Let $Y_1, ..., Y_k$ be the set of intersections of concepts of $C_1$ with $X$.
By definition of $\prod_{C_1}(X)$,
$k \leq \prod_{C_1}(X) \leq \prod_{C_1}(m)$.
Similarly, from definition of $\prod_{C_2}(Y_i)$, the intersections of the concepts from $C_2$ with $Y_i$ are at most $\prod_{C_2}(Y_i) \leq \prod_{C_2}(m)$.

Therefore, the number of sets intersections of concepts of $C$ with $X$ is at most
$k \prod_{C_2} (Y_i) \leq \prod_{C_1}(m) \prod_{C_2}(m)$.
Since we already showed $\prod_{C_1}(X)$,
$k \leq \prod_{C_1}(X)$, therefore, the proof is complete and $\prod_\mathcal{C}(m) \leq \prod_{\mathcal{C}_\infty}(m) \prod_{\mathcal{C}_\epsilon}(m)$.


\section{The transpose}

\begin{outcome}
  \begin{enumerate}
  \item Calculate the transpose of a matrix.
  \item Determine whether a matrix is symmetric, antisymmetric, or neither.
  \item Manipulate algebraic expressions involving the transpose of matrices.
  \end{enumerate}
\end{outcome}

Another important operation on matrices is that of taking the
\textbf{transpose}. The transpose of a matrix is obtained by turning
the rows into columns and vice versa.

\begin{definition}{The transpose of a matrix}{matrix-transpose}
  Let $A$ be an $m\times n$-matrix.  Then the \textbf{transpose}%
  \index{matrix!transpose}%
  \index{transpose of a matrix} of $A$, denoted $A^T$, is the
  $n\times m$-matrix whose $(i,j)$-entry is the $(j,i)$-entry of $A$.
  \begin{equation*}
    A = \begin{mymatrix}{ccc}
      a_{11} & \cdots & a_{1n} \\
      a_{21} & \cdots & a_{2n} \\
      \vdots & \ddots & \vdots \\
      a_{m1} & \cdots & a_{mn} \\
    \end{mymatrix},
    \quad
    A^T = \begin{mymatrix}{cccc}
      a_{11} & a_{21} & \cdots & a_{m1} \\
      \vdots & \vdots & \ddots & \vdots \\
      a_{1n} & a_{2n} & \cdots & a_{mn} \\
    \end{mymatrix}.
  \end{equation*}
\end{definition}

\begin{example}{The transpose of a matrix}{transpose-matrix}
  Find the transpose of the following matrix:
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & 2 & 6 \\
      3 & 5 & 4
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  The transpose is
  \begin{equation*}
    A^T =
    \begin{mymatrix}{rr}
      1 & 3 \\
      2 & 5 \\
      6 & 4
    \end{mymatrix}.
  \end{equation*}
  Notice that $A$ is a $2\times 3$-matrix, while $A^T$ is a
  $3\times 2$-matrix.
\end{solution}

We have already used a special case of the transpose since
Chapter~\ref{cha:vectors-rn}, when we wrote
$\begin{mymatrix}{rrr}1&2&3\end{mymatrix}^T$ as a space-saving
notation for the column vector
\begin{equation*}
  \begin{mymatrix}{c}
    1 \\
    2 \\
    3 \\
  \end{mymatrix}.
\end{equation*}

The transpose of a matrix satisfies the following
properties%
\index{transpose of a matrix!properties}%
\index{matrix!transpose!properties}%
\index{matrix!properties of transpose}:

\begin{proposition}{Properties of the transpose}{transpose-properties}
  Let $A$ and $B$ be matrices of appropriate sizes, and $r$ a
  scalar. Then the following hold.
  \begin{enumerate}
  \item $(A^T)^T = A$.
  \item $(A+B)^T=A^T+B^T$.\label{matrix-transpose-2}
  \item $(rA)^T=rA^T$.\label{matrix-transpose-3}
  \item $(AB)^T=B^TA^T$.\label{matrix-transpose-4}
  \item $0^T = 0$.
  \item $I^T = I$.
  \item $(A^{-1})^T = (A^T)^{-1}$, if $A$ is invertible.
  \end{enumerate}
\end{proposition}

Recall that a column vector is the same thing as a $n\times
1$-matrix. Using the transpose, we can make precise the connection
between the dot product and the matrix product%
\index{dot product!from matrix product}%
\index{vector!dot product!from matrix product}. Namely, let
\begin{equation*}
  \vect{v}=\begin{mymatrix}{c}v_1\\\vdots\\v_n\end{mymatrix}
  \quad\mbox{and}\quad
  \vect{w}=\begin{mymatrix}{c}w_1\\\vdots\\w_n\end{mymatrix}
\end{equation*}
by column vectors. Then
\begin{equation*}
  \vect{v}\dotprod\vect{w}
  ~=~
  v_1w_1 + \ldots + v_nw_n
  ~=~
  \begin{mymatrix}{ccc} v_1 & \cdots & v_n \end{mymatrix}
  \begin{mymatrix}{c} w_1 \\ \vdots \\ w_n \end{mymatrix}
  ~=~
  \vect{v}^T\vect{w}.
\end{equation*}
In other words, the dot product of column vectors $\vect{v}$ and
$\vect{w}$ is the same thing as the matrix product
$\vect{v}^T\vect{w}$.

We can also use the notion of transpose to define what it means for a
matrix to be \textbf{symmetric} and \textbf{antisymmetric}.

\begin{definition}{Symmetric and antisymmetric matrices}{symmetric-and-antisymmetric}
  An $n\times n$-matrix $A$ is said to be \textbf{symmetric}%
  \index{matrix!symmetric}%
  \index{symmetric matrix} if $A^T=A$. It is said to be
  \textbf{antisymmetric}%
  \index{matrix!antisymmetric}%
  \index{antisymmetric matrix} (sometimes also called \textbf{skew
    symmetric}%
  \index{matrix!skew symmetric}%
  \index{skew symmetric matrix}) if $A^T=-A$.
\end{definition}

\begin{example}{Symmetric and antisymmetric matrices}{symmetric-matrix}
  Let
  \begin{equation*}
    A=\begin{mymatrix}{rrr}
      2 & 1 & 3 \\
      1 & 5 & -3 \\
      3 & -3 & 7
    \end{mymatrix},
    \quad
    B=\begin{mymatrix}{rrr}
      0 & 1 & 3 \\
      -1 & 0 & 2 \\
      -3 & -2 & 0
    \end{mymatrix},
    \quad\mbox{and}\quad
    C=\begin{mymatrix}{rrr}
      0 & 1 &  3 \\
      1 & 5 & -3 \\
      1 & 3 &  0
    \end{mymatrix}.
  \end{equation*}
  Then $A$ is symmetric because $A^T=A$, $B$ is antisymmetric
  because $B^T=-B$, and $C$ is neither symmetric nor antisymmetric
  because $C^T$ is equal to neither $C$ nor $-C$.
\end{example}


\section{Sparse Coding}

\subsection*{Orthogonal Basis}
Pros: fast inverse; preserves energy.
For $\mathbf{x}$ and orthog. mat. $\mathbf{U}$ compute $\mathbf{z} = \mathbf{U}^\top \mathbf{x} $. Approx $ \mathbf{\hat{x}} = \mathbf{U\hat{z}}$, $\hat{z}_i = z_i$ if $ \lvert z_i \rvert > \epsilon$ else 0.
Reconstruction Error $\|\mathbf{x}-\mathbf{\hat{x}}\|^2 = \sum_{d\notin\sigma}\langle\mathbf{x},\mathbf{u}_d\rangle ^2$.
Choice of base depends on signal. Fourier for global, wavelet for local support. PCA basis optimal for given $\Sigma$. Stripes \& check patterns: hi-freq in Fourier.

\subsection*{Haar Wavelets (form orthogonal basis)}
scaling fcn $\phi(x)=[1,1,1,1]$, mother $W(x)=[1,1,-1,-1]$, dilated $W(2x)=[1,-1,0,0]$, translated $W(2x-1)=[0,0,1,-1]$

\subsection*{Overcomplete Basis}
$\mathbf{U} \in \mathbb{R}^{D \times  L}$ for \# atoms $ = L > D = \mathsf{dim}\text{(data)}$. Decoding involved $\rightarrow$ add constraint $\mathbf{z}^\star \in \argmin_\mathbf{z} \lVert \mathbf{z} \rVert_0$ s.t. $\mathbf{x} = \mathbf{Uz}$. NP-hard $\rightarrow$ approximate with 1-norm (convex) or with MP.

\textbf{Coherence}
\begin{inparaitem}[\color{red}\textbullet]
	\item $m(\mathbf{U}) = \max_{i,j:\, i \neq j} | \mathbf{u}_i^\top \mathbf{u}_j |$
	\item $m(\mathbf{B}) = 0$ if $\mathbf{B}$ orthogonal matrix
	\item $m([\mathbf{B}, \mathbf{u}]) \geq \frac{1}{\sqrt{D}}$ if atom $\mathbf{u}$ is added to orthogonal basis $\mathbf{B}$ (o.n.b. = orthonormal base)
\end{inparaitem}

\textbf{Matching Pursuit (MP)}
approximation of $\mathbf{x}$ onto $\mathbf{U}$, using $K$ entries.
Objective: $\mathbf{z}^\star \in \argmin_{\mathbf{z}} \|\mathbf{x} - \mathbf{Uz} \|_2$, s.t. $\|\mathbf{z}\|_0 \leq K$
\begin{inparaenum}[\color{red}1.]
	\item init: $z \leftarrow 0, r \leftarrow x$
	\item while $\|\mathbf{z}\|_0 < K$ do
	\item select atom with smallest angle $i^\star = \argmax_i |\langle \mathbf{u}_i, \mathbf{r} \rangle|$
	\item update coefficients: $z_{i^\star} \leftarrow z_{i^\star} + \langle \mathbf{u}_{i^\star}, \mathbf{r} \rangle$
	\item update residual: $\mathbf{r} \leftarrow \mathbf{r} - \langle \mathbf{u}_{i^\star}, \mathbf{r} \rangle \mathbf{u}_{i^\star}$.
\end{inparaenum}
\\\textbf{Exact recovery} when: $K<1/2( 1+1/m(\mathbf{U}))$

\textbf{Compressive Sensing}: Compress data while gathering:
\begin{inparaitem}[\color{red}\textbullet]
  \item $\mathbf{x} \in \mathbb{R}^D$, $K$-sparse in o.n.b. $\mathbf{U}$. $\mathbf{y} \in \mathbb{R}^M$ with $y_i = \langle \mathbf{w}_i, \mathbf{x}\rangle $: $M$ lin. combinations of signal; $\mathbf{y} = \mathbf{Wx} = \mathbf{WUz} = \mathbf{\Theta z}$, $\Theta \in \mathbb{R}^{M \times D}$
  \item Reconstruct $\mathbf{x} \in \mathbb{R}^D$ from $\mathbf{y}$; find $\mathbf{z}^\star \in \argmin_{\mathbf{z}}\|\mathbf{z}\|_0$, s.t. $\mathbf{y} = \mathbf{\Theta z}$ (e.g. with MP, or convex it with 1-norm: canbe eq!). Given $\mathbf{z}$, reconstruct $\mathbf{x} = \mathbf{Uz}$
\end{inparaitem}
\\Any orthogonal $\mathbf{U}$ sufficient if:
\begin{inparaitem}[\color{red}\textbullet]
  \item $\mathbf{W} = $ Gaussian random projection, i.e. $w_{ij}\sim\mathcal{N}(0, \frac{1}{D})$
  \item M $\geq cK log(\frac{D}{K})$, where $c$ is some constant
\end{inparaitem}


%
% Chapter 2.6
%

\section*{2.6 Implicit Differentiation}

Implicit differentiation is a way of solving a derivative by differentiating both sides of an equation with respect to \(x\) and the solving the resulting equation for \(y'\).

\subsubsection*{Example}

Find \(\frac{dy}{dx} \) of \( x^2+y^2=25 \).

\subsubsection*{Solution}

Differentiate both side of the equation.
$$ \frac{d}{dx}(x^2+y^2) = \frac{d}{dx}(25) $$
$$ \frac{d}{dx}(x^2)+\frac{d}{dx}(y^2)=0$$
\(y\) is a function of \(x\) so we use the chain rule.
$$ \frac{d}{dx}(y^2) = \frac{d}{dy} (y^2) \frac{dy}{dx} = 2y\frac{dy}{dx}$$
$$ 2x + 2y \frac{dy}{dx} = 0 $$
Solve for \(\frac{dy}{dx}\).
$$ \frac{dy}{dx}=-\frac{x}{y} $$


\section{Gradient Descent}
\frame{\tableofcontents[currentsection, hideothersubsections]}

\begin{frame}
\frametitle{Gradient Descent: Intro}

Recall:\\
\begin{itemize}
    \item hypotheses as vectors $\mathbf{w}$ that \\
         come from a convex hypothesis class, $\mathcal{H}$
    \item goal of learning:\\
          to minimize the risk function $L_D(\mathbf{w})$;\\
          \textbf{not} the empirical risk $L_S (h)$
    \item gradient def:
        \begin{figure}
            \centering
            \includegraphics[scale=0.30]{grad_def_p185}
        \end{figure}
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Gradient Descent: Intro}
Gradient descent:
\begin{itemize}
\item an iterative optimization procedure
\item at each step: \\
    improve the solution by
    taking a step along the negative of the gradient of the function to be minimized at the current point

    \begin{figure}
    \centering
    \includegraphics[scale=0.30]{eq_14_1}
    \end{figure}
\item after $T$ iterations, output either
    \begin{itemize}
        \item averaged vector
            \footnote{taking the average turns out to be rather useful, especially
            when we generalize gradient descent to nondifferentiable functions and to the stochastic case}, \textbf{or}
        \item last vector, \textbf{or}
        \item the best performing vector
    \end{itemize}
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Gradient Descent: Analysis of GD for Convex-Lipschitz Fn}

LET:
\begin{itemize}
\item $\mathbf{w^*}$ be any vector and
\item $B$ be an upper bound on $\parallel \mathbf{w^*} \parallel$
\end{itemize}
\vspace{5mm}

GOAL:\\
to obtain an upper bound on  $f(\bar{\mathbf{w}}) - f(\mathbf{w^*})$,
where $\bar{\mathbf{w}} = \frac{1}{T} \sum_{1}^{T} \mathbf{w}^{(t)}$
\vspace{5mm}

RESULT:\\
From the definition of $\bar{\mathbf{w}}$, and using Jensen's inequality, we obtain:
\begin{figure}
    \centering
    \includegraphics[scale=0.25]{eq_14_3b}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Gradient Descent: Analysis of GD for Convex-Lipschitz Fn}

\begin{figure}
    \centering
    \includegraphics[scale=0.25]{lemma_14_1}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Gradient Descent: Analysis of GD for Convex-Lipschitz Fn}

\begin{figure}
    \centering
    \includegraphics[scale=0.2]{corollary_14_2}
\end{figure}

\end{frame}


\section*{Ex.34.5-7}
\subsection*{Longest--simple--cycle problem (LSC)}

The decision problem: Is there a cycle of simple path of size $k$

To show NP:
\\
Get the vertices $v_1,\ldots, v_n$ in sorted order, and verify that edges $(v_i, v_{i+1})$ exists in $G$ for $i\in\{1,\ldots,n\}$ and let $v_{n+1} = v_1$. This can be done in polynomial time. Hence LSC is NP.

To show NP completeness we want to show that HAM--CYCLE $\leq_P$ LSC:
\\
If you solve LSC with  $k=\abs*{V}$ then you get 1 if there is a HAM--CYCLE and 0 else. Hence there exists a polynomial function which transform HAM--CYCLE to LSC. So LSC is NP complete.


% jam 2004-09-05

\section{Barycentric coordinates}
\label{sec:barycentric-coordinates}

In this section, I describe how to compute
the barycentric coordinates of a point $\q$
with respect to a $n$-simplex in
a finite dimensional inner product space $\Vspace$
(with $\dimension (\Vspace ) = m \ge n$) ---
and the derivatives with respect to the vertex positions.

Let $S$ be the geometric $n$-simplex
with vertex positions $\{\p_0  \ldots  \p_n\}$;
in $\Vspace$.

The {\it span} of $S$, $\affine_span ( S )$,
is the affine span of its vertex positions
$\affine_span \{\p_0  \ldots  \p_n\}$,
that is, the set of points $\q$ such that
$\q = \sum_{j=n}^{m} b_j \p_j $ where $1 = \sum_{j=0}^{n} b_j $.
$b_j$ are {\it barycentric coordinates} of $\q$ with respect to $S$.
Barycentric coordinates of an arbitrary point $\q \in \Vspace$,
are barycentric coordinates of the closest point in $\affine_span(S)$.
If $S$ is non-degenerate (the dimension of its span is $m$),
then the barycentric coordinates of any point are unique.

Assume in what follows that $S$ is non-degenerate.
When faced with for a degenerate simplex, there are several options.
One can use the minimum norm $\b$.
It is often possible to use the barycentric coordinates
with respect to the 'largest' non-degenerate face instead.
We may also want to choose $\b$ to be as close to convex,
$0 \le \b_j \le 1$, as possible. {\it (How?)}

Let $\{ \v_0 \ldots \v_{n-1} \} = \{ (\p_0 - \p_n) \ldots (\p_{n-1} - \p_n)\},$
and let $\Vmap = \sum_{j=0}^{n-1} ( \v_j \otimes \e_j^{\Reals^n} )$,
the linear map from $\Reals^n \mapsto \Vspace$,
whose 'columns' are the $\v_j$.
Let $\b = \left( \b_0 \ldots \b_{n-1} \right) \in \Reals^n$
be the unconstrained vector of the first $n$ barycentric coordinates.
For $\q \in \affine_span ( S )$, $\q = \Vmap \b + \p_m$
and $\b = \Vmap^{-1} \left ( \q - \p_m \right)$.
For arbitrary $\q \in \Vspace$,
$\b = \Vmap^{-} (\q - \p_m)$
(see \autoref{sec:Inverses-and-pseudo-inverses}).


\section{Fundamentals of Hypothesis Testing}\label{S:HypTest}
%This section is under Jazz-\work\\{\scriptsize [and will evolve from face-to-face interactions with students of mathematics at Uppsala University]}.

The subset of {\bf all posable hypotheses} that remain {\bf falsifiable} is the space of {\bf scientific hypotheses}.  Roughly, a falsifiable hypothesis is one for which a statistical experiment can be designed to produce data that an experimenter can use to falsify or reject it.  In the statistical decision problem of hypothesis testing, we are interested in empirically falsifying a scientific hypothesis, i.e.~we attempt to reject an hypothesis on the basis of empirical observations or data.  Thus, hypothesis testing has its roots in the philosophy of science and is based on Karl Popper's falsifiability criterion for demarcating scientific hypotheses from the set of all posable hypotheses.

\subsection{Introduction}\label{S:HypTestIntro}
Usually, the hypothesis we attempt to reject or falsify is called the {\bf null hypothesis} or $H_0$ and its complement is called the {\bf alternative hypothesis} or $H_1$.  For example, consider the following two hypotheses:

$H_0$:  The average waiting time at an Orbiter bus stop is less than or equal to $10$ minutes.\\
$H_1$:  The average waiting time at an Orbiter bus stop is more than $10$ minutes.

If the sample mean $\overline{x}_n$ is much larger than $10$ minutes then we may be inclined to reject the null hypothesis that the average waiting time is less than or equal to $10$ minutes.  We will learn to formally test hypotheses in the sequel.

Suppose we are interested in the following hypothesis test for the bus-stop  problem:

$H_0$:  The average waiting time at an Orbiter bus stop is equal to $10$ minutes.\\
$H_1$:  The average waiting time at an Orbiter bus stop is not $10$ minutes.

Once again we can use the sample mean as the test statistic.  Our procedure for rejecting this null hypothesis is different and is often called the Wald test.

%\section{Parametric Hypothesis Testing}

More generally, suppose $X_1,X_2,\ldots,X_n \overset{IID}{\sim} F(x_1;\theta^*)$, with an unknown and fixed $\theta^* \in \BB{\Theta}$.  Let us partition the parameter space $\BB{\Theta}$ into $\BB{\Theta}_0$, the null parameter space, and $\BB{\Theta}_1$, the alternative parameter space, ie,
$$\BB{\Theta}_0 \cup \BB{\Theta}_1 = \BB{\Theta}, \qquad \text{and} \qquad \BB{\Theta}_0 \cap \BB{\Theta}_1 = \emptyset \ .$$
Then, we can formalise testing the null hypothesis versus the alternative as follows:
\[
H_0 : \theta^* \in \BB{\Theta}_0 \qquad \text{versus} \qquad H_1 :  \theta^* \subset \BB{\Theta}_1 \ .
\]
The basic idea involves finding an appropriate rejection region $\Xz_R$ within the data space $\Xz$ and rejecting $H_0$ if the observed data $x:=(x_1,x_2,\ldots,x_n)$ falls inside the rejection region $\Xz_R$,
\[
\text{If $x:=(x_1,x_2,\ldots,x_n) \in \Xz_R \subset \Xz$, then reject $H_0$, else do not reject $H_0$.}
\]
Typically, the rejection region $\Xz_R$ is of the form:
\[
\Xz_R := \{ x:=(x_1,x_2,\ldots,x_n)  : T(x) > c \}
\]
where, $T$ is the {\bf test statistic} and $c$ is the {\bf critical value}.  Thus, the problem of finding $\Xz_R$ boils down to that of finding $T$ and $c$ that are appropriate.  Once the rejection region is defined, the possible outcomes of a hypothesis test are summarised in the following table.
\begin{table}[htbp]
\begin{center}
\caption{Outcomes of an hypothesis test.}
\begin{tabular}{c|c|c}\hline
& Do not Reject $H_0$ & Reject $H_0$ \\ \hline
$H_0$ is True & OK & Type I Error \\ \hline
$H_1$ is True & Type II Error & OK \\ \hline
\end{tabular}
\end{center}
\end{table}

\begin{definition}[Power, Size and Level of a Test]
The {\bf power function} of a test with rejection region $\Xz_R$ is
\begin{equation}\label{E:power}
\beta(\theta) := \P_{\theta}(x \in \Xz_R) \ .
\end{equation}
So $\beta(\theta)$ is the power of the test at the parameter value $\theta$, i.e.~the probability that the observed data $x$, sampled from the distribution specified by $\theta$, falls in $\Xz_R$ and thereby leads to a rejection of the null hypothesis.

The $\mathsf{size}$ of a test with rejection region $\Xz_R$ is the supreme power under the null hypothesis, i.e.~the supreme probability of rejecting the null hypothesis when the null hypothesis is true:
\begin{equation}\label{E:size}
\mathsf{size} := \sup_{\theta \in \BB{\Theta}_0} \beta(\theta) := \sup_{\theta \in \BB{\Theta}_0} \P_{\theta}(x \in \Xz_R) \ .
\end{equation}
The $\mathsf{size}$ of a test is often denoted by $\alpha$.  A test is said to have $\mathsf{level}$ $\alpha$ if its $\mathsf{size}$ is less than or equal to $\alpha$.
\end{definition}
Let us familiarize ourselves with some terminology in hypothesis testing next.
\begin{table}[htbp]
\begin{center}
\caption{Some terminology in hypothesis testing.}
\begin{tabular}{c|c|c}\hline
$\BB{\Theta}$ & Test: $H_0$ versus $H_1$ & Nomenclature \\ \hline
$\BB{\Theta} \subset \Rz^m, m \geq 1$ & $H_0: \theta^* = \theta_0$ versus $H_1: \theta^* \neq \theta_1$ & Simple Hypothesis Test \\ \hline
$\BB{\Theta} \subset \Rz^m, m \geq 1$ & $H_0: \theta^* \in \BB{\Theta}_0$ versus $H_1: \theta^* \in \BB{\Theta}_1 $ & Composite Hypothesis Test \\ \hline
$\BB{\Theta} \subset \Rz^1$ & $H_0: \theta^* = \theta_0$ versus $H_1: \theta^* \neq \theta_0$ & Two-sided Hypothesis Test \\ \hline
$\BB{\Theta} \subset \Rz^1$ & $H_0: \theta^* \geq \theta_0$ versus $H_1: \theta^* < \theta_0$ & One-sided Hypothesis Test \\ \hline
$\BB{\Theta} \subset \Rz^1$ & $H_0: \theta^* \leq \theta_0$ versus $H_1: \theta^* > \theta_0$ & One-sided Hypothesis Test \\ \hline
\end{tabular}
\end{center}
\end{table}

We introduce some widely used tests next.

\subsection{The Wald Test}\label{S:WaldTest}
The Wald test is based on a direct relationship between the $1-\alpha$ confidence interval and a $\mathsf{size}$ $\alpha$ test.  It can be used for testing simple hypotheses involving a scalar parameter.
\begin{definition}[The Wald Test]
Let $\widehat{\Theta}_n$ be an asymptotically normal estimator of the fixed and possibly unknown parameter $\theta^* \in \BB{\Theta} \subset \Rz$ in the parametric IID experiment:
\[
X_1,X_2,\ldots,X_n \overset{IID}{\sim} F(x_1;\theta^*) \enspace .
\]
Consider testing:
\[
H_0: \theta^* = \theta_0 \qquad \text{versus} \qquad H_1: \theta^* \neq \theta_0 \enspace .
\]
Suppose that the null hypothesis is true and the estimator $\widehat{\Theta}_n$ of $\theta^*=\theta_0$ is asymptotically normal:
\[
\theta^*=\theta_0, \qquad \frac{\widehat{\Theta}_n - \theta_0}{\widehat{\mathsf{se}}_n} \rightsquigarrow \normal(0,1) \enspace .
\]
Then, the Wald test based on the test statistic $W$ is:
\[
\boxed{
\text{Reject $H_0$ when $|W|>z_{\alpha/2}$, where $W:=W((X_1,\ldots,X_n))=\frac{\widehat{\Theta}_n ((X_1,\ldots,X_n)) - \theta_0}{\widehat{\mathsf{se}}_n}$.
}
}
\]
The rejection region for the Wald test is:
\[
\boxed{
\Xz_R = \{ x:=(x_1,\ldots,x_n) : |W (x_1,\ldots,x_n) | > z_{\alpha/2} \} \enspace .
}
\]
\end{definition}
\begin{prop}[Asymptotic $\mathsf{size}$ of a Wald test]
As the sample size $n$ approaches infinity, the $\mathsf{size}$ of the Wald test approaches $\alpha$ :
\[
\boxed{
\mathsf{size} = \P_{\theta_0} \left( |W| > z_{\alpha/2} \right) \to \alpha \enspace .}
\]
\end{prop}
\begin{proof}
Let $Z \sim \normal(0,1)$.  The $\mathsf{size}$ of the Wald test, i.e.~the supreme power under $H_0$ is:
\begin{flalign*}
\mathsf{size}
& := \sup_{\theta \in \BB{\Theta}_0} \beta(\theta) := \sup_{\theta \in \{\theta_0\}} \P_{\theta}(x \in \Xz_R) = \P_{\theta_0}(x \in \Xz_R) \\
& = \P_{\theta_0} \left( |W| > z_{\alpha/2} \right)  = \P_{\theta_0} \left( \frac{|\widehat{\theta}_n - \theta_0|}{\widehat{\mathsf{se}}_n} > z_{\alpha/2} \right) \\
& \to \P \left( |Z| > z_{\alpha/2} \right)\\
& = \alpha \enspace .
\end{flalign*}
\end{proof}
Next, let us look at the power of the Wald test when the null hypothesis is false.
\begin{prop}[Asymptotic power of a Wald test]
Suppose $\theta^* \neq \theta_0$.  The power $\beta(\theta^*)$, which is the probability of correctly rejecting the null hypothesis, is approximately equal to:
\[
\boxed{
\Phi \left( \frac{\theta_0-\theta^*}{\widehat{\mathsf{se}}_n} - z_{\alpha/2} \right) +
\left( 1- \Phi \left( \frac{\theta_0-\theta^*}{\widehat{\mathsf{se}}_n} + z_{\alpha/2} \right) \right) \enspace ,
}
\]
where, $\Phi$ is the DF of $\normal(0,1)$ RV.  Since ${\widehat{\mathsf{se}}_n} \to 0$ as $n \to 0$ the power increase with sample $\mathsf{size}$ $n$.  Also, the power increases when $|\theta_0-\theta^*|$ is large.
\end{prop}
Now, let us make the connection between the $\mathsf{size}$ $\alpha$ Wald test and the $1-\alpha$ confidence interval explicit.
\begin{prop}[The $\mathsf{size}$ Wald test]
The $\mathsf{size}$ $\alpha$ Wald test rejects:
\[
\boxed{
\text{ $H_0: \theta^*=\theta_0$ versus $H_1: \theta^* \neq \theta_0$ if and only if $\theta_0 \notin C_n := (\widehat{\theta}_n-{\widehat{\mathsf{se}}_n} z_{\alpha/2}, \widehat{\theta}_n+{\widehat{\mathsf{se}}_n} z_{\alpha/2})$.
}}
\]
Therefore, testing the hypothesis is equivalent to verifying whether the null value $\theta_0$ is in the confidence interval.
\end{prop}
\begin{example}[Wald test for the mean waiting times at our Orbiter bus-stop]
Let us use the Wald test to attempt to reject the null hypothesis that the mean waiting time at our Orbiter bus-stop is $10$ minutes under an IID $\exponential(\lambda^*)$ model.  Let $\alpha=0.05$ for this test.  We can formulate this test as follows:
\[
H_0: \lambda^* = \lambda_0= \frac{1}{10} \quad \text{versus} \quad H_1: \lambda^* \neq \frac{1}{10}, \quad \text{where, } \quad X_1\ldots,X_{132} \overset{IID}{\sim} \exponential(\lambda^*) \enspace .
\]
Based on \hyperref[EX:ExponentialFisherInfo]{Example~\ref*{EX:ExponentialFisherInfo}} and \hyperref[LW:ExponentialMLECIOrbiter]{Labwork~\ref*{LW:ExponentialMLECIOrbiter}} we obtained the $95\%$ confidence interval to be $[0.0914, 0.1290]$. Since our null value $\lambda_0=0.1$ belongs to this confidence interval, we fail to reject the null hypothesis from a $\mathsf{size}$ $\alpha=0.05$ Wald test.
\end{example}

We can use bootstrap-based confidence interval $C_n$ in conjunction with Wald test as shown by the next example.

\begin{example}[Wald test of the bootstrapped correlation coefficient]
Recall the problem of estimating the confidence interval for the correlation coefficient between the LSAT scores ($Y_1,\ldots,Y_{15}$) and the GPA ($Z_1,\ldots,Z_{15}$) in \hyperref[LW:LSATGPACorrBoot]{Labwork~\ref*{LW:LSATGPACorrBoot}}.  We assumed that the bivariate data $(Y_i,Z_i) \overset{IID}{\sim} F^*$, such that $F^* \in \{ \text{all bivariate DFs} \}$. Suppose we are interested in testing the null hypothesis that the true correlation coefficient $\theta^*$ is $0$:
\[
H_0: \theta^* = \theta_0 = 0 \quad \text{versus} \quad H_1: \theta^* \neq 0, \quad \text{where} \quad  \theta^* = \frac{\int \int (y-\E(Y))(z-\E(Z))dF(y,z)}{\sqrt{\int (y-\E(Y))^2 dF(y) \int (z-\E(Z))^2 dF(z)}} \enspace .
\]
Since the percentile-based $95\%$ bootstrap confidence interval for the plug-in estimate of the correlation coefficient from \hyperref[LW:LSATGPACorrBoot]{Labwork~\ref*{LW:LSATGPACorrBoot}} was $[0.2346, 0.9296]$ and this interval does not contain $0$, we can reject the null hypothesis that the correlation coefficient is $0$ using a $\mathsf{size}$ $\alpha=0.05$ Wald test.
\end{example}

\subsection{A Composite Hypothesis Test}
Often, we are interested in a testing a composite hypothesis, i.e.~one in which  the null hypothesis is not a singleton set.  We revisit the Orbiter waiting time problem from this perspective next.
\begin{figure}[htpb]
\caption{Plot of power function $\beta(\lambda)$ for different values of the critical value $c$ and the $\mathsf{size}$ $\alpha$ as function of the critical values.
\label{F:ExponentialTestOrbiter}}
\centering   \makebox{\includegraphics[width=6.5in]{figures/ExponentialTestOrbiter}}
\end{figure}

\begin{example}[Testing the Mean Waiting Time at an Orbiter Bus-stop]
Let us test the following null hypothesis $H_0$.

$H_0$:  The average waiting time at an Orbiter bus stop is less than or equal to $10$ minutes.\\
$H_1$:  The average waiting time at an Orbiter bus stop is more than $10$ minutes.

We have observations of $n=132$ waiting times $x_1,x_2,\ldots,x_{132}$ at the Orbter bus-stop with $\overline{x}_{132}=9.0758$.  Let us assume a parametric model, say,
\[
X_1,X_2,\ldots,X_n \overset{IID}{\sim} \exponential(\lambda^*)
\]
with an unknown and fixed $\lambda^* \in \BB{\Lambda}=(0,\infty)$.  Since the parameter $\lambda$ of an $\exponential(\lambda)$ RV is the reciprocal of the mean waiting time, we can formalise the above hypothesis testing problem of $H_0$ versus $H_1$ as follows:
\[
H_0 : \lambda^* \in \BB{\Lambda}_0 = [1/10,\infty) \qquad \text{versus} \qquad H_1 :  \lambda^* \in \BB{\Lambda}_1 = (0,1/10)
\]
Consider the test:
\[
\text{Reject $H_0$ if $T>c$.}
\]
where the test statistic $T=\overline{X}_n$ and the rejection region is:
\[
\Xz_R = \{ (x_1,x_2,\ldots,x_n) : T(x_1,x_2,\ldots,x_n) > c \} \enspace .
\]
Since the sum of $n$ IID $\exponential(\lambda)$ RVs is $\gammA(\lambda,n)$ distributed, the power function is:
\begin{flalign*}
\beta(\lambda) &= \P_{\lambda} \left(\overline{X}_n > c \right) = \P_{\lambda}\left(\sum_{i=1}^n{X_i} > nc \right) = 1- \P_{\lambda}\left(\sum_{i=1}^n{X_i} \leq nc \right)\\
&= 1 - F\left( nc; \lambda, n \right)
= 1- \frac{1}{\Gamma(n)}\int_0^{\lambda n c} y^{n-1} \exp(-y) dy \\
&= 1- {\tt gammainc}(\lambda n c, n)
\end{flalign*}
Clearly, $\beta(\lambda)$ is a decreasing function of $\lambda$ as shown in \hyperref[F:ExponentialMLECIOrbiter]{Figure~\ref*{F:ExponentialTestOrbiter}}.  Hence the $\mathsf{size}$ of the test as a function of the critical region specified by the critical value $c$ is:
\begin{flalign*}
\mathsf{size}
&= \sup_{\lambda \in \BB{\Lambda}_0} \beta(\lambda)
= \sup_{\lambda \geq 1/10} \beta(\lambda)=\beta(1/10)=1- {\tt gammainc}(132 c/10, 132)
\end{flalign*}
For a $\mathsf{size}$ $\alpha=0.05$ test we numerically solve for the critical value $c$ that satisfies: $$0.05=1- {\tt gammainc}(132 c/10, 132)$$ by trial and error as follows:
\begin{VrbM}
>> lambda0=1/10
lambda0 =    0.1000
>> S=@(C)(1-gammainc(lambda0*n*C,n)); % size as a function of c
>> Cs=[10 11 11.474 12 13] % some critical values c
Cs =   10.0000   11.0000   11.4740   12.0000   13.0000
>> Size=arrayfun(S,Cs) % corresponding size
Size =    0.4884    0.1268    0.0499    0.0143    0.0007
\end{VrbM}
Thus, we reject $H_0$ when $\overline{X}_n>11.4740$ for a level $\alpha=0.05$ test.  Since our observed test statistic $\overline{x}_{132}=9.0758 < 11.4740$ we fail to reject the null hypothesis that the mean waiting time is less than or equal to $10$ minutes.  Therefore, there is no evidence that the Orbiter bus company is violating its promise of an average waiting time of no more than $10$ minutes.
\end{example}

\subsection{p-values}\label{S:p-values}
It is desirable to have a more informative decision than simply reporting "reject $H_0$'' or ``fail to reject $H_0$.''  For instance, we could ask whether the test rejects $H_0$ for each $\mathsf{size}=\alpha$.  Typically, if the test rejects at $\mathsf{size}$ $\alpha$ it will also reject at a larger $\mathsf{size}$ $\alpha' > \alpha$.  Therefore, there is a smallest $\mathsf{size}$ $\alpha$ at which the test rejects $H_0$ and we call this $\alpha$ the $\pvalue$ of the test.

\begin{figure}[h]
\caption{The smallest $\alpha$ at which a $\mathsf{size}$ $\alpha$ test rejects the null hypothesis $H_0$ is the $\pvalue$.}\label{F:pvalue}
\begin{center}
\fbox{
\setlength{\unitlength}{1mm}
\begin{picture}(130,50)(0,-2)
\thicklines
\put(10,30){\fbox{Reject $H_0$?~}}
\put(30,10){{No~}}
\put(30,40){{Yes~}}

\put(40,5){\vector(0,1){40}}
\put(40,5){\vector(1,0){70}}
\put(59,4){$\bullet$}
\put(70,15){\vector(-1,-1){10}}
\put(70,15){$\pvalue$}

\put(40,1){$0$}
\put(90,1){$1$}
\put(65,-1){$\mathsf{size}$}

\put(40,10){\line(1,0){20}}
\put(60,10){\line(0,1){30}}
\put(60,40){\line(1,0){30}}

\end{picture}
}
\end{center}
\end{figure}

\begin{definition}[p-value]
Suppose that for every $\alpha \in (0,1)$ we have a $\mathsf{size}$ $\alpha$ test with rejection region $\Xz_{R,\alpha}$ and test statistic $T$.  Then,
\[
\pvalue := \inf \{ \alpha: T(X) \in \Xz_{R,\alpha} \} \enspace .
\]
That is, the $\pvalue$ is the smallest $\alpha$ at which a $\mathsf{size}$ $\alpha$ test rejects the null hypothesis.
\end{definition}

If the evidence against $H_0$ is strong then the $\pvalue$ will be small.  However, a large $\pvalue$ is not strong evidence in favour of $H_0$.  This is because a large $\pvalue$ can occur for two reasons:
\begin{enumerate}
\item $H_0$ is true.
\item $H_0$ is false but the test has low power.
\end{enumerate}
Finally, it is important to realise that $\pvalue$ is not the probability that the null hypothesis is true, i.e.~$\pvalue \neq \P(H_0|x)$, where $x$ is the data.  The following tabulation of evidence scale is useful.
\begin{table}
\caption{Evidence scale against the null hypothesis in terms of the range of $\pvalue$.}\label{T:pvalueEvidenceScale}
\begin{center}
\begin{tabular}{|c|c|}
\hline
$\pvalue$ range & Evidence \\ \hline
$(0, 0.01]$ & very strong evidence against $H_0$\\
$(0.01, 0.05]$ & strong evidence against $H_0$\\
$(0.05, 0.1]$ & weak evidence against $H_0$\\
$(0.1,1)$ & little or no evidence against $H_0$\\
\hline
\end{tabular}
\end{center}
\end{table}
The next proposition gives a convenient expression for the $\pvalue$ for certain tests.
\begin{prop}[The $\pvalue$ of a hypothesis test]
Suppose that the $\mathsf{size}$ $\alpha$ test based on the test statistic $T$ and critical value $c_{\alpha}$ is of the form:
\[
\text{Reject $H_0$ if and only if $T:=T((X_1,\ldots,X_n))> c_{\alpha}$,}
\]
then
\[
\pvalue = \sup_{\theta \in \BB{\Theta}_0} \P_{\theta}(T((X_1,\ldots,X_n)) \geq t:=T((x_1,\ldots,x_n))) \enspace ,
\]
where, $(x_1,\ldots,x_n)$ is the observed data and $t$ is the observed value of the test statistic $T$.  In words, the $\pvalue$ is the supreme probability under $H_0$ of observing a value of the test statistic the same as or more extreme than what was actually observed.
\end{prop}
Let us revisit the Orbiter waiting times example from the $\pvalue$ perspective.
\begin{example}[$\pvalue$ for the parametric Orbiter experiment]
Let the waiting times at our bus-stop be $X_1,X_2,\ldots,X_{132} \overset{IID}{\sim} \exponential(\lambda^*)$.  Consider the following testing problem:
\[
H_0: \lambda^*=\lambda_0=\frac{1}{10} \quad \text{versus} \quad H_1: \lambda^* \neq \lambda_0 \enspace .
\]
We already saw that the Wald test statistic is:
\[
W:=W(X_1,\ldots,X_n)= \frac{\widehat{\Lambda}_n-\lambda_0}{\widehat{\mathsf{se}}_n(\widehat{\Lambda}_n)} = \frac{\frac{1}{\overline{X}_n}-\lambda_0}{\frac{1}{\sqrt{n}\overline{X}_n}} \enspace .
\]
The observed test statistic is:
\[
w=W(x_1,\ldots,x_{132})=
\frac{\frac{1}{\overline{X}_{132}}-\lambda_0}{\frac{1}{\sqrt{132}\overline{X}_{132}}}
= \frac{\frac{1}{9.0758}-\frac{1}{10}}{\frac{1}{\sqrt{132} \times 9.0758}} = 1.0618 \enspace .
\]
Since, $W \rightsquigarrow Z \sim \normal(0,1)$, the $\pvalue$ for this Wald test is:
\begin{flalign*}
\pvalue
&= \sup_{\lambda \in \BB{\Lambda}_0} \P_{\lambda} (|W|>|w|)= \sup_{\lambda \in \{\lambda_0\}} \P_{\lambda} (|W|>|w|) =  \P_{\lambda_0} (|W|>|w|) \\
& \to \P (|Z|>|w|)=2 \Phi(-|w|)=2 \Phi(-|1.0618|)=2 \times 0.1442=0.2884 \enspace .
\end{flalign*}
Therefore, there is little or no evidence against $H_0$ that the mean waiting time under an IID $\exponential$ model of inter-arrival times is exactly ten minutes.
\end{example}
%\section{Parametric Bootstrap-based Test}

\subsection{Permutation Test for the equality of any two DFs}\label{S:PermTest}

Permutation test is a non-parametric exact method for testing whether two distributions are the same.  It is non-parametric because we do not impose any restrictions on the class of DFs that the unknown DF should belong to.  It is exact because we do not have any asymptotic approximations involving sample size approaching infinity.  So this test works for any sample size.

Formally, we suppose that:
\[
X_1,X_2,\ldots,X_m \overset{IID}{\sim} F^* \quad \text{and} \quad X_{m+1}, X_{m+2},\ldots,X_{m+n} \overset{IID}{\sim} G^* \enspace ,
\]
are two sets of independent samples.  The possibly unknown DFs $F^*,G^* \in \{ \text{all DFs} \}$.  Now, consider the following hypothesis test:
\[
H_0: F^*=G^* \quad \text{versus} \quad H_1: F^* \neq G^* \enspace .
\]
Let our test statistic $T(X_1,\ldots,X_m,X_{m+1},\ldots,X_{m+n})$ be some sensible one -- $T$ is large when $F^*$ is too different from $G^*$, say:
\[
T:=T(X_1,\ldots,X_m,X_{m+1},\ldots,X_{m+n})= \abs \left( \frac{1}{m} \sum_{i=1}^m X_i -  \frac{1}{n} \sum_{i=m+1}^n X_i  \right) \enspace .
\]
Then the idea of a permutation test is as follows:
\begin{enumerate}
\item Let $N:=m+n$ be the pooled sample size and consider all $N!$ permutations of the observed data $x_{\mathsf{obs}}:=(x_1,x_2,\ldots,x_m,x_{m+1},x_{m+2},\ldots,x_{m+n})$.
\item For each permutation of the data compute the statistic $T(\text{permuted data $x$})$ and denote these $N!$ values of $T$ by $t_1,t_2,\ldots,t_{N!}$.
\item Under $H_0: X_1,\ldots,X_m,X_{m+1},\ldots,X_{m+n} \overset{IID}{\sim}F^*=G^*$, each of the permutations of $x= (x_1,x_2,\ldots,x_m,x_{m+1},x_{m+2},\ldots,x_{m+n})$ has the same joint probability $\prod_{i=1}^{m+n} f(x_i)$, where $f(x_i)=dF(x_i)=dG(x_i)$.  Therefore, the transformation of the data by our statistic $T$ also has the same probability over the values of $T$, namely $\{t_1,t_2,\ldots,t_{N!}\}$.  Let $\P_0$ be this permutation distribution that is dicrete and uniform over  $\{t_1,t_2,\ldots,t_{N!}\}$.
\item Let $t_{\mathsf{obs}} := T(x_{\mathsf{obs}})$ be the observed value of the statistic.
\item Assuming we reject $H_0$ when $T$ is large, the $\pvalue$ is:
\[
\pvalue = \P_0 \left( T \geq t_{\mathsf{obs}} \right) = \frac{1}{N!} \left( \sum_{j=1}^{N!} \BB{1} (t_j  \geq t_{\mathsf{obs}}) \right), \qquad  \BB{1} (t_j \geq t_{\mathsf{obs}}) =
\begin{cases}
1 & \text{if } \quad t_j \geq t_{\mathsf{obs}} \\
0 & \text{otherwise}
\end{cases}
\]
\end{enumerate}

Let us look at a small example involving the diameters of coarse venus shells ({\em Dosinia anus}) that Guo Yaozong and Chen Shun found on the left and right sides of the New Brighton prier in Spring 2007.  We are interested in testing the hypothesis that the distribution of shell diameters for this bivalve species is the same on both sides of the pier.
\begin{example}[Guo-Chen Experiment with Venus Shell Diameters]
Let us look at the first two samples $x_1$ and $x_2$ from the left of pier and the first sample from the right side of pier, namely $x_3$.  Since the permutation test is exact, we can use this small data set with merely three samples to conduct the following hypothesis test:
\[
H_0: X_1,X_2,X_3 \overset{IID}{\sim} F^*=G^*, \qquad H_1: X_1,X_2 \overset{IID}{\sim} F^*, X_3 \overset{IID}{\sim} G^*, \quad F^* \neq G^* \enspace .
\]
Let us use the test statistic:
\[
T(X_1,X_2,X_3)=\abs \left( \frac{1}{2} \sum_{i=1}^2 X_i -  \frac{1}{1} \sum_{i=2+1}^3 X_i  \right)=\abs \left( \frac{X_1+x_2}{2} -  \frac{X_3}{1} \right) \enspace .
\]
The data giving the shell diameters in millimetres and $t_{\mathsf{obs}}$ are:
\[
(x_1,x_2,x_3) = (52,54,58) \quad \text{and} \quad t_{\mathsf{obs}}=\abs \left( \frac{52+54}{2} - \frac{58}{1} \right) = \abs (53-58) = \abs(-5)=5 \enspace .
\]
Let us tabulate the $(2+1)!=3!=3\times2\times1=6$ permutations of the data $(x_1,x_2,x_3) = (52,54,58)$, the corresponding values of $T$ and their probabilities under the null hypothesis, i.e., the permutation distribution $\P_0(T)$.
\begin{center}
\begin{tabular}{c c c}
\hline
Permutation & $t$ & $\P_0(T=t)$ \\ \hline
$(52,54,58)$ & $5$ & $\frac{1}{6}$ \\
$(54,52,58)$ & $5$ & $\frac{1}{6}$  \\
$(52,58,54)$ & $1$ & $\frac{1}{6}$  \\
$(58,52,54)$ & $1$ &$\frac{1}{6}$  \\
$(58,54,52)$ & $4$ & $\frac{1}{6}$  \\
$(54,58,52)$ & $4$ & $\frac{1}{6}$  \\ \hline
\end{tabular}
\end{center}
From the table, we get:
\[
\pvalue = \P_0 \left( T \geq t_{\mathsf{obs}}\right) =  \P_0 \left( T \geq 5 \right) = \frac{1}{6}+ \frac{1}{6}= \frac{2}{6}= \frac{1}{3} \approxeq 0.333 \enspace .
\]
Therefore, there is little to no evidence against $H_0$.
\end{example}

When the pooled sample size $N=m+n$ gets large, $N!$ would be too numerous to tabulate exhaustively.  In this situation, we can use a Monte Carlo approximation of the $\pvalue$ by generating a large number of random permutations of the data according to the following Steps:

\begin{enumerate}
\item[\sf{Step 1}:] Compute the observed statistic $t_{\mathsf{obs}} := T(x_{\mathsf{obs}})$ of data $x_{\mathsf{obs}}:=(x_1,\ldots,x_m,x_{m+1},\ldots,x_{m+n})$.
\item[\sf{Step 2}:] Randomly permute the data and compute the statistic again from the permuted data.
\item[\sf{Step 3}:] Repeat {\sf Step 2} $B$ times and let $t_1,\ldots,t_B$ denote the resulting values ($B$ is large, say $1000$).
\item[\sf{Step 4}:] The (Monte Carlo) approximate $\pvalue$ is:
\[
\frac{1}{B} \sum_{j=1}^B \BB{1}(t_j \geq t_{\mathsf{obs}} ) \enspace .
\]
\end{enumerate}
Next we implement the above algorithm on the full data set of Guo and Chen obtained from coarse venus shells sampled from the two sides of the New Brighton pier.
\begin{labwork}[Approximate $\pvalue$ of a permutation test of shell diameters]\label{LW:Shells}
Test the null hypothesis that the distribution of the diameters of coarse venus shells are the same on both sides of the New Brighton pier.
\VrbMf[label=Shells.m]{scripts/Shells.m}
When we execute the script to perform a permutation test and approximate the $\pvalue$, we obtain:
\begin{VrbM}
>> Shells
ApproxPValue =    0.8576
\end{VrbM}
Therefore, there is little or no evidence against the null hypothesis.
\end{labwork}

\subsection{Pearson's Chi-Square Test for Multinomial Trials}\label{S:Chi2}

We derive the $\chisquare$ distribution introduced by Karl Pearson in 1900 [{\em Philosophical Magazine}, Series 5, {\bf 50}, 157-175].  This historical work laid the foundations of modern statistics by showing why an experimenter cannot simply plot experimental data and just assert the correctness of his or her hypothesis.  This derivation is adapted from Donald E.~Knuth's treatment [{\em Art of Computer Programming, Vol.~II, Seminumerical Algorithms}, 3rd Ed., 1997, pp.~55-56].  We show how $\demoivre$, $\multinomial$, $\poisson$ and the $\normal$ random vectors conspire to creare the $\chisquare$ random variable.

{\em Part 1: \demoivre~trials}\\
Consider $n$ independent and identically distributed $\demoivre (\theta_1,\ldots,\theta_k)$ random vectors (\rv s):
\[
X_1,X_2,\ldots,X_n \overset{IID}{\sim} \demoivre (\theta_1,\ldots,\theta_k) \enspace .
\]
Recall from \hyperref[M:deMoivreRVec]{Model~\ref*{M:deMoivreRVec}} that $X_1 \sim \demoivre (\theta_1,\ldots,\theta_k)$ means $\P(X_1=e_i)=\theta_i$ for $i\in\{1,\ldots,k\}$, where $e_1,\ldots,e_k$ are ortho-normal basis vectors in $\Rz^k$.
Thus, for each $i \in \{1,2,\ldots,n\}$, the corresponding  $X_i$ has $k$ components, i.e.~$X_i:=(X_{i,1},X_{i,2},\ldots,X_{i,k})$.

{\em Part 2: \multinomial~trial}\\
Suppose we are only interested in the experiment induced by their sum:
\[
Y := (Y_1,\ldots,Y_k) := \sum_{i=1}^n X_i :=  \sum_{i=1}^n (X_{i,1},X_{i,2},\ldots,X_{i,k}) = \left( \sum_{i=1}^n X_{i,1}, \sum_{i=1}^n X_{i,2},\ldots, \sum_{i=1}^n X_{i,k} \right) \enspace .
\]
The \rv~$Y$, being the sum of $n$ IID $\demoivre(\theta_1,\ldots,\theta_k)$ \rv s, is the $\multinomial(n,\theta_1,\ldots,\theta_k)$ \rv~of  \hyperref[M:Multinomial]{Model~\ref*{M:Multinomial}} and the probability that $Y:=(Y_1,\ldots,Y_k) = y := (y_1,\ldots,y_k)$ is:
\[
 \frac{n!}{y_1! y_2! \cdots y_k!} \prod_{i=1}^k \theta_i^{y_i} \enspace .
\]
The support of the \rv~$Y$, i.e.~the set of possible realisations of $y:= (y_1,\ldots,y_k)$ is:
\[
\Yz := \{ (y_1,\ldots,y_k) \in \Zz_+^k : \sum_{i=1}^k y_i = n \} \enspace .
\]
{\em Part 3: Conditional sum of \poisson~trials}\\
Here we consider an alternative formulation of the $\multinomial(n,\theta_1,\ldots,\theta_k)$ \rv~$Y$.  Suppose,
\[
Y_1 \sim \poisson(n \theta_1), Y_2 \sim \poisson(n \theta_2), \ldots, Y_k \sim \poisson(n \theta_k) \enspace ,
\]
and that $Y_1,\ldots,Y_k$ are independent.  Recall from \hyperref[M:Poisson]{Model~\ref*{M:Poisson}} that $Y_i \sim \poisson(n\theta_i)$ means $\P(Y_i=y_i)=e^{-n\theta_i} (n\theta_i)^{y_i}/y_i!$ for $y_i \in \{0,1,\ldots\}$.  Then, the joint probability probability of the \rv~$(Y_1,\ldots,Y_k)$ is the product of the independent $\poisson$ probabilities:
\begin{flalign*}
\P\left( (Y_1,\ldots,Y_k) = (y_1,\ldots,y_k) \right)
&:= \P \left( Y_1=y_1,\ldots,Y_k=y_k \right)
= \prod_{i=1}^k \P(Y_i=y_i) = \prod_{i=1}^k{ \frac{e^{-n\theta_i} (n\theta_i)^{y_i}}{y_i!} } \\
&
= \frac{ \prod_{i=1}^k e^{-n\theta_i} n^{y_i} \theta_i^{y_i}}{ \prod_{i=1}^k{ y_i!} }
=\left(e^{-n \sum_{i=1}^k \theta_i} n^{\sum_{i=1}^k y_i} \prod_{i=1}^k \theta_i^{y_i}\right) \frac{1}{ \prod_{i=1}^k{ y_i!} } \\
& =  \frac{  e^{-n} n^{n} \prod_{i=1}^k \theta_i^{y_i}}{ \prod_{i=1}^k{ y_i!} }
\enspace .
\end{flalign*}
Now, the probability that sum $Y_1+\cdots+Y_k$ will equal $n$ is obtained by summing over the probabilities of all $(y_1,\ldots,y_k) \in \Yz$:
\begin{flalign*}
\P\left(\sum_{i=1}^k Y_i = n \right)
& = \sum_{\substack{(y_1,\ldots,y_k)\\ \in\Yz}}  \P\left( (Y_1,\ldots,Y_k) = (y_1,\ldots,y_k) \right) =  \sum_{\substack{(y_1,\ldots,y_k)\\ \in\Yz}}  \frac{  e^{-n} n^{n} \prod_{i=1}^k \theta_i^{y_i}}{ \prod_{i=1}^k{ y_i!} } \\
& = e^{-n} n^{n} \frac{1}{n!}
\underset {=\P(\Yz)=1}{\underbrace{\left( \sum_{\substack{(y_1,\ldots,y_k)\\ \in\Yz}}   \frac{n!}{ \prod_{i=1}^k{ y_i!} } \prod_{i=1}^k \theta_i^{y_i} \right)}} = \frac{e^{-n} n^{n}}{n!}
\enspace .
\end{flalign*}
Finally, the conditional probability that $(Y_1,\ldots,Y_k)=(y_1,\ldots,y_k)$ given $\sum_{i=1}^k Y_i = n$ is:
\begin{multline*}
\P \left( (Y_1,\ldots,Y_k)=(y_1,\ldots,y_k) \vert \sum_{i=1}^k Y_i = n \right) =
\frac{\P \left( (Y_1,\ldots,Y_k)=(y_1,\ldots,y_k) , \sum_{i=1}^k Y_i = n \right)}{\P \left( \sum_{i=1}^k Y_i = n \right) } \\
= \frac{\P \left( (Y_1,\ldots,Y_k)=(y_1,\ldots,y_k) \right)}{\P \left( \sum_{i=1}^k Y_i = n \right) }
= \frac{  e^{-n} n^{n} \prod_{i=1}^k \theta_i^{y_i}}{ \prod_{i=1}^k{ y_i!} } \frac{n!} {e^{-n} n^{n}} =  \frac{n!}{y_1! y_2! \cdots y_k!} \prod_{i=1}^k \theta_i^{y_i} \enspace .
\end{multline*}
Therefore, we may also think of the random vector $Y:=(Y_1,\ldots,Y_k) \sim \multinomial(n,\theta_1,\ldots,\theta_k)$ as $k$ independent $\poisson$ random variables, $Y_1 \sim \poisson(n \theta_1), \ldots, Y_k \sim \poisson(n \theta_k)$, that have been conditioned on their sum $\sum_{i=1}^nY_i$ being $n$.

{\em Part 4: The $\normal$ approximation of the centred and scaled $\poisson$}\\
Recall from \hyperref[M:Poisson]{Model~\ref*{M:Poisson}} that the expectation and variance of a RV $Y_i \sim \poisson(n\theta_i)$ are $\E(Y_i)=\V(Y_i)=n\theta_i$.  Let $Z_i$ be $\E(Y_i)$-centred and $\sqrt{\V(Y_i)}$-scaled $Y_i$ and \[
Z_i :=  \frac{Y_i - \E(Y_i)}{\sqrt{\V(Y_i)}} = \frac{Y_i - n \theta_i}{\sqrt{n \theta_i}} \enspace .
\]
The condition that $Y_1+\cdots+Y_k=n$ is equivalent to requiring that $\sqrt{\theta_1} Z_1 +\cdots+\sqrt{\theta_k} Z_k = 0$, since:
\begin{multline*}
\sum_{i=1}^k {Y_i} =n
\iff \sum_{i=1}^k {Y_i} -n = 0
\iff \sum_{i=1}^k {Y_i} - n \sum_{i=1}^k {\theta_i} = 0
\iff \sum_{i=1}^k {Y_i - n \theta_i} = 0 \\
\iff \sum_{i=1}^k  \frac{Y_i - n \theta_i}{\sqrt{n }} = 0
\iff \sum_{i=1}^k \sqrt{\theta_i} \frac{Y_i - n \theta_i}{\sqrt{n \theta_i}} = 0
\iff \sum_{i=1}^k \sqrt{\theta_i} Z_i = 0 \enspace .
\end{multline*}
Now consider the support of the \rv~$Z:=(Z_1,\ldots,Z_k)$ conditioned on $\sum_{i=1}^k \sqrt{\theta_i} Z_i = 0$, i.e.~the hyper-plane of $(k-1)$-dimensional vectors:
\[
\Hz := \{ (z_1,\ldots,z_k) : \sqrt{\theta_1} z_1 +\cdots+\sqrt{\theta_k} z_k = 0 \}
\]
Each $Z_i \rightsquigarrow \normal(0,1)$ by the central limit theorem.  Therefore, for large values of $n$, each $Z_i$ is approximately distributed as the $\normal(0,1)$ RV with PDF $f(z_i;0,1)=(2\pi)^{-1/2}\exp(-z_i^2/2)$.  Since the $Z_i$s are independent except for the condition that they lie in $\Hz$, the point in a differential volume $dz_2\ldots dz_k$ of $\Hz$ occur with probability approximately proportional to:
 \[
 \exp(-z_1^2/2) \times \cdots \times \exp(-z_k^2/2)  = \exp(-(z_1^2+\cdots+z_k^2)/2)
 \]

{\em Part 5: $\chisquare$ distribution as the sum of squared $\normal$s}\\
We are interested in the sum of the area of squares with side-lengths $Z_1,\ldots,Z_k$.  Let $V$ be the desired sum of squares:
\[
\qquad V := \sum_{i=1}^k Z_i^2 =  \sum_{i=1}^k \frac{\left(Y_i - n \theta_i \right)^2}{{n \theta_i}}  , \quad \text{such that} \quad Z_i \rightsquigarrow \normal(0,1), \quad \sum_{i=1}^k \sqrt{\theta_i} Z_i = 0 \enspace .
\]
The probability that $V \leq v$ as $n \to \infty$ is:
\[
\frac{\int_{\substack{(z_1,\ldots,z_k) \in \Hz \ \text{and} \ \sum_{i=1}^k{z_i^2} \leq v}} \exp(-(z_1^2+\cdots+z_k^2)/2) \ dz_2\ldots dz_k} {\int_{\substack{(z_1,\ldots,z_k) \in \Hz}} \exp(-(z_1^2+\cdots+z_k^2)/2) \ dz_2\ldots dz_k}
\]
Since the $(k-1)$-dimensional hyper-plane $\Hz$ passes through the origin of  $\Rz^k$, the domain of integration in the numerator above is the interior of a $(k-1)$-dimensional hyper-sphere of radius $\sqrt{v}$ that is centred at the origin.  Using a transformation of the above ratio of integrals into generalised polar co-ordinates with radius $\chi$  and angles $\alpha_1,\ldots,\alpha_{k-2}$, we get:
\[
\frac{\int_{\chi^2\leq v} \exp(-\chi^2/2) \chi^{k-2} g(\alpha_1,\ldots,\alpha_{k-2}) d \chi d \alpha_1\cdots d \alpha_{k-2}} {\int \exp(-\chi^2/2) \chi^{k-2} g(\alpha_1,\ldots,\alpha_{k-2}) d \chi d \alpha_1\cdots d \alpha_{k-2}} \enspace ,
\]
for some function $g$ of the angles [See Problem 15 in {\em Art of Computer Programming, Vol.~II, Seminumerical Algorithms}, 3rd Ed., 1997, pp.~59].   The integration over the $(k-2)$ angles results in the same factor that cancels between the numerator and the denominator.  This yields the formula for the probability that $V \leq v$ as $n \rightarrow \infty$:
\[
\lim_{n \to \infty} \P(V \leq v) =  \frac{\int_0^{\sqrt{v}}\exp(-\chi^2/2) \chi^{k-2} d \chi} {\int_0^{\infty}\exp(-\chi^2/2) \chi^{k-2} \ d \chi} \enspace .
\]
By substituting $t=\chi^2/2$, we can express the integrals in terms of the incomplete Gamma function defined as $\gamma(a,x) :=  \int_0^{a} \exp(-t) t^{a-1} \ dt$ as follows:
\[
\P(V \leq v) = \gamma \left( \frac{k-1}{2},\frac{v}{2} \right) / \Gamma \left(\frac{k-1}{2} \right) \enspace .
\]
This is the DF of the $\chisquare$ distribution with $k-1$ degrees of freedom.

\begin{model}[$\chisquare(k)$ RV]  Given a parameter $k \in \Nz$ called degrees of freedom, we say that $V$ is a $\chisquare(k)$ RV if its PDF is:
\[
f(v; k) :=  \frac{v^{(k/2)-1}e^{-v/2}}{2^{k/2}\Gamma(k/2)} \BB{1}_{\{v \in \Rz: v>0\}}(v)
\]
Also, $\E(V)=k$ and $\V(V) = 2k$.
\end{model}

We can use the {\bf Pearson's Chi-Square test statistic}:
\begin{equation}\label{PearsonsChiSquaredTestStat}
T:= T(Y_1,\ldots,Y_k) = \frac{\left(Y_1 - n \theta^*_1 \right)^2}{n \theta^*_1}+\cdots+\frac{\left(Y_k - n \theta^*_k \right)^2}{n \theta^*_k}
\end{equation}
to test the null hypothesis $H_0$ that may be formalised in three equivalent ways:
\begin{flalign*}
& H_0: X_1,X_2,\ldots,X_n \overset{IID}{\sim} \demoivre(\theta_1^*,\ldots,\theta_k^*)~\rv \\
\iff
& H_0:
Y:= (Y_1,\ldots,Y_k) = \sum_{i=1}^n X_i \sim \multinomial(n,\theta_1^*,\ldots,\theta_k^*)~\rv \\
\iff
& H_0:
Y_1 \overset{IND}{\sim} \poisson(n\theta_1)~\mbox{RV},\ldots,Y_k \overset{IND}{\sim} \poisson(n\theta_k)~\mbox{RV} \ \text{given that} \ \sum_{i=1}^k Y_i = n
\end{flalign*}

We have seen that under $H_0$, the test statistic $T \rightsquigarrow V \sim \chisquare(k-1)$.  Let $t_{\mathsf{obs}}$ be the observed value of the test statistic and let the upper alpha quantile be $\chi^2_{k-1,\alpha} := F^{[-1]}(1-\alpha)$, where $F$ is the CDF of $V \sim \chisquare(k-1)$.  Hence the test:
\[
\boxed{
\text{Reject $H_0$ if $T > \chi^2_{k-1,\alpha}$ is an asymptotically $\mathsf{size}$ $\alpha$ test and the $\pvalue = \P(V > t_{obs})$.}
}
\]

Now, let us look at a concrete example of Perason's Chi-Square test.

Gregor Johann Mendel (1822--1884) was a scientist and an abbot.
Mendel gained posthumous recognition as the founder of the modern science of genetics.
Though it has been known for millennia that crossbreeding of plants and animals could favour specific desirable traits, Mendel's pea plant experiments conducted between 1856 and 1863 was the first to empirically establish many of the rules of heredity.
These rules are now known as the {\em laws of Mendelian inheritance}. Let's look at an experiment performed by Medel to test a null hypothesis on the rules of inheritance.

\begin{example}[Medel's Peas]
Mendel bred two varieties of peas. The first variety had round yellow seeds and the second one had wrinkled green seeds.
The progeny of this breeding experiment were of four types: round yellow, wrinkled yellow, round green, and wrinkled green.
Let us assume that the number of each type of progeny is a $\multinomial(n,\theta)$ RV $X$ with probability $\theta = (\theta_1,\theta_2,\theta_3,\theta_4) \in \Delta^3$.
Mendel's theory of inheritance predicts that $\theta = \theta_0$,
\[
\theta_0 = (\theta_{0,1}, \theta_{0,2}, \theta_{0,3}, \theta_{0,4}) = \left( \frac{9}{16}, \frac{3}{16}, \frac{3}{16}, \frac{1}{16} \right)
\]
He obtained $n=556$ progeny out of which he observed $X = (315,101,108,32)$.

Here, we will test the null hypothesis $H_0: \theta = \theta_0$ versus $H_1: \theta \neq \theta_0$ using Pearson's Chi-Square test statistic \eqref{PearsonsChiSquaredTestStat}.

Since $n \theta_{0,1} = 556 \frac{9}{16} = 312.75$, $n \theta_{0,2} = n \theta_{0,3} = 556 \frac{3}{16} = 104.25$, and $n \theta_{0,4} = 556 \frac{1}{16} = 34.75$, the test statistic is:
\[
T = \sum_{j=1}^4 \frac{(X_j - n \theta_{0,j})^2}{n \theta_{0,j}} =
\frac{(315-312.75)^2}{312.75} + \frac{(101-104.25)^2}{104.25} + \frac{(108-104.25)^2}{104.25} + \frac{(32-34.75)^2}{34.75} = 0.47
\]
For $\alpha=0.05$, the $\chi^2_{3,\alpha}$ value is $7.815$ --- this can be ontained numerically or from a table of $\chisquare$ distribution with $3$ degrees of freedom.
Since the observed test statistic $0.47$ is not larger than $\chi^2_{3,\alpha}=7.815$, we do not reject the null hypothesis.
With $T \sim \chisquare(3)$ under the null hypothesis, the $\pvalue$ is
\[
\pvalue = \P ( T > 0.47) = 0.93
\]
which is little to no evidence against the null hypothesis.
Therefore, the data do not contradict Medel's theory of inheritance.
\end{example}

\newpage

%\section{Kolmogorov-Smirnov Test for continuous DF $F$}


\documentclass{article}

\usepackage{amsmath}

\begin{document}

\section{Algorithms}

Euclid's algorithm (Algorithm E)

Given two positive integers $n$ and $m$, find their GCD.

\begin{enumerate}
  \item If $m < n$, switch $m \longleftrightarrow n$.
  \item Divide $m$ by $n$ and let $r$ be the remainder.
  \item If $r = 0$ then the algorithm terminates and $n$ from Step 2 is the answer.
  \item If $r \neq 0$, set $m \leftarrow n$ and $n \leftarrow r$, then go to Step 2.
\end{enumerate}

A sketch of a formal definition of an algorithm:

A \emph{computational method} is a quadruple $(Q,I,\Omega,f)$, where
\begin{itemize}
  \item $I\subset Q$ is the \emph{input}
  \item $\Omega \subset Q$ is the \emph{output}
  \item $f : Q \rightarrow Q$ is the \emph{computational rule}, which satisfies $f(\omega) = \omega\ \forall \omega \in \Omega$.
\end{itemize}

Each $x\in I$ defines a \emph{computational sequence} $x_0,x_1,\dots,$, where $x_0=x$ and $x_{k+1} = f(x_k)$. The sequence \emph{terminates} in $k$ steps if $k$ is the smallest integer such that $x_k \in \Omega$. An \emph{algorithm} is a computational method that terminates in some finite number of steps for all $x$ in $I$.

As an example, we present Euclid's algorithm in this formalization: let $Q$ be the set of all singletons $\{n\}$, all ordered pairs $(m,n)$, and all ordered quadruples $(m,n,r,1),(m,n,r,2),(m,n,p,3)$ where $m,n,p$ are positive integers and $r$ is a nonnegative integer. Let $I$ be the ordered pairs $(m,n)$ and $\Omega$ the set of singletons $\{n\}$. Define $f$ by

\begin{align*}
  f((m,n)) = (m,n,0,1);\ f((n)) = (n); \\
  f((m,n,r,1)) = (m,n, m\ \% \ n, 2); \\
  f((m,n,r,2)) = (n)\ \mathrm{if}\ r=0,\ (m,n,r,3)\ \mathrm{otherwise}; \\
  f((m,n,p,3)) = (n,p,p,1)
\end{align*}

Note that this definition of an algorithm doensn't include any notions of effectiveness - i.e., the ability for the algorithm to be performed with constituent elementary operations (compass-and-straightedge, MIX instructions, etc). We introduce a ``programming'' notion of effectiveness as follows: let $A$ be a finite set of letters, and let $A^*$ be the set of all strings on $A$. Let $N$ be a nonnegative integer and $Q$ be the set of all $(\sigma,j)$ with $\sigma \in A^*$ and $j$ is an integer such that $0\leq j \leq N$. Let $I \subset J$ be the subset of $J$ where $j=0$ and $\Omega$ be the subset of $J$ where $j=N$. If $\theta,\sigma \in A^*$, we say that $\theta$ occurs in $\sigma$ if there exists $\alpha,\omega\in A^* $ such that $\sigma = \alpha\theta\omega$ (under string concatenation). Finally, let $f$ be a function defined by the strings $\theta_j$ and $\phi_j$, and the integers $a_j$ and $b_j$, for each $0\leq j \leq N$:

\begin{flalign*}
  f((\sigma,j)) = (\sigma,a_j) && \text{if}\ \sigma_j\ \text{does not occur in}\ \theta \\
  f((\sigma,j)) = (\alpha\phi_j\omega,b_j) && \text{if}\ \alpha\ \text{is the shortest possible string such that}\ \sigma=\alpha\theta_j\omega \\
  f((\sigma,N)) = (\sigma,N)
\end{flalign*}

\section{Algorithms - solutions to exercises}
\
\indent \textbf{1.1.1} $t \leftarrow a$, $a \leftarrow b$, $b \leftarrow c$, $c \leftarrow d$, $d\leftarrow t$.

\

\textbf{1.1.2} We have $m \leftarrow n$ and $n \leftarrow r$. Since $r < n$, after assignment $n < m$.

\

\textbf{1.1.3} \textbf{Algorithm F}. Given two positive integers $m$ and $n$, find the greatest common divisor.

\ \textbf{F1} Divide $m$ by $n$.

\ \textbf{F2} Set $m$ equal to the remainder.

\ \textbf{F3} If $m=0$ then the answer is $n$.

\ \textbf{F4} Otherwise divide $n$ by $m$.

\ \textbf{F5} Set $n$ equal to the remainder.

\ \textbf{F6} If $n=0$ then the answer is $m$.

\ \textbf{F7} Go to \textbf{F1}.

\
\textbf{1.1.4} $6099 \% 2166 = 1767 \Rightarrow 2166 \% 1767 = 399$

$\Rightarrow 1767 \%  399 = 171 \Rightarrow 399 \$ 171 = 57 \Rightarrow 171 \% 57 = 0$.

So the GCD is 57.
\

\textbf{1.1.5} Not finite, not definite, not effective.

\textbf{1.1.6}
$n=1$: $1 \% 5 = 1 \Rightarrow 5 \% 1 = 0$, 2 steps

$n=2$: $2 \% 5 = 2 \Rightarrow 5 \% 2 = 1 \Rightarrow 2 \% 1 = 0$, 3 steps

$n=3$: $3 \% 5 = 3 \Rightarrow 5 \% 3 = 2 \Rightarrow 3 \% 2 = 1 \Rightarrow 2 \%1 = 0$, 4 steps

$n=4$: $4 \% 5 = 4 \Rightarrow 5 \% 4 = 4 \Rightarrow 4 \% 4 = 0$, 3 steps

$n=5$: $5 \% 5 = 0$, 1 step

So $T_5 = 2.6$.

\
\textbf{1.1.7} $U_m$ is well-defined: if $n > m$, the first step of the Euclidean algorithm simply swaps $n$ and $m$ (since $m\ \%\ n = m$) and $U_m = T_m + 1$. If $n < m$ then there are only finitely many cases.

\textbf{1.1.8} \textbf{Note: I was a bit confused by this presentation and checked the answers.} Find the GCD of $n$ and $m$, given an alphabet containing $a$ and $b$ and an input $a^m b^n$.

Our intent is to translate Euclid's algorithm, using the hint provided:
\begin{enumerate}
  \item Set $r \leftarrow |m - n|$ and $n \leftarrow \mathrm{min}(m,n)$
  \item If $r = 0$ then $m = n$ and $n$ is the GCD of $m$ and $n$, so the algorithm terminates.
  \item Otherwise set $m \leftarrow n$ and $n \leftarrow r$ and go to Step 1.
\end{enumerate}

We first prove that this is in fact the GCD. If $g$ is the GCD of $m$ and $n$, then either $g = n$ or $g < n$. The first case is correct handled by Step 2. Otherwise, in step 3, $g$ is clearly the GCD of $\min(m,n)$ and by hypothesis $m\neq n$. Suppose $m>n$; then $m - n = ag - bg = g(a - b)$ for some $0 < b < a$ where $g$ does not share any factors with $a$ or $b$ and $g > a,b$. Therefore $g$ is the GCD of $r$ as well.

To translate this algorithm into the formalism, we will need an alphabet character, $c$, to represent the remainder. Phrasing the algorithm as words:
\begin{enumerate}
  \item Start with $a^m b^n$.
  \item Remove $n$ $b$s from the right and $n$ $a$s from the left, then append $n$ $c$s to the left.
  \item There are $|m-n|$ $a$s left over (aka $r$ from above) and $n$ $c$s to the left of the $a$s.
  \item If $m=n$ then there are no $a$s and the number of $c$s (aka $m$ and $n$ is the GCD.
  \item Otherwise swap the $a$s with $b$s and the $c$s with $a$s, then repeat.
\end{enumerate}

This faithfully reproduces the algorithm above. Therefore, in the formalism,

\textbf{1.1.9}

\section{Mathematical Preliminaries - Induction}
Algorithmic proof procedure:

\textbf{Algorithm I - Construct a proof} Given a positive integer $n$ and proposition $P(n)$, this algorithm will output a proof that $P(n)$ is true (if it succeeds).

\textbf{I1} [Prove $P(1)$] Set $k \leftarrow 1$ and use another algorithm to output a proof of $P(1)$.

\textbf{I2} [$k=n$?] If $k=n$, terminate - the required proof was found in the previous step.

\textbf{I3} [$k<n$] Otherwise $k<n$. Use another algorithm to output a proof of the following statement: ``If $P(1)$, $P(2)$,$\dots$,$P(k)$ is true, then $P(k+1)$ is true.'' Then output the statement ``We have already proved $P(1),\dots,P(k)$, hence $P(k+1)$ is true.'' Combine these statements.

\textbf{I4} Set $k \leftarrow k + 1$. Go to step \textbf{I2}.

Here is an inductive proof of a fact about the Fibonacci sequence. Let $F_0 = 0,F_1=1$ and $F_n = F_{n-1} + F_{n-2}$ for $n\geq 2$. Define $\phi = (1 + \sqrt{5})/2$. Then $F_n \leq \phi^{n-1}$ for all positive $n$.

We proceed according to the algorithm above. This is clearly true for $n=0$ and $n=1$,l so we have obtained a proof of $P(1)$. For $P(2)$, $F_2 = 1$ and $\phi > 1.6$, so we have a (computational) proof of $P(2)$. Now assume our target is $k+1$ with $k>1$ and we have $k$ proofs $P(1),\dots,P(k)$. Since $F_{k+1} = F_k + F_{k-1}$, and by hypothesis $F_k \leq \phi^{k-1}$ and $F_{k-1} \leq \phi^{k-2}$,

\begin{equation}
  F_{k+1} \leq \phi^{k-1} + \phi^{k-2} = \phi^{k-2}(1+\phi).
\end{equation}

$\phi$ is actually the positive solution to $1 + \phi = \phi^2$. So plugging this in gives $F_{k+1} \leq \phi^k$, as desired.

Note that our proof would have failed if we didn't have direct proofs of $P(1)$ and $P(2)$: $P(1)$ would fail at the inductive step since the theorem is not true for $n=0$, and for $P(2)$ we couldn't have applied the method at the $F_{k-1} \leq \phi^{k-2}$ step (since $k=1$).

\section{Mathematical Induction - Exercises}

\indent \textbf{1.2.1} The base case $n=0$, then prove $P(0),P(1),dots,P(n)$ implies $P(n+1)$.

\textbf{1.2.2} The proof uses this formula:

\begin{equation*}
  a^{(n+1)-1} = \frac{(a^{n-1})(a^{n-1})}{(a^{(n-2)/(n-1)})^{n-1}}
\end{equation*}

but this formula is not valid for $n=1$. So the proof used in the base case can't be used with this formula in the inductive step.

\textbf{1.2.3} Since there are $n-1$ terms in the expansion, the formula is not valid at $n=1$ and ``clearly $3/2 - 1/n = 1/2$'' is not actually a valid reading of the formula.

\textbf{1.2.4}

\end{document}


%TODOs for this lab
%1. May merge with ImgSegMST

\lab{Algorithm}{Image Segmentation with Minimal Spanning Trees}{Image Segmentation with Minimal Spanning Trees}
\label{Ch:MSTImgSeg}

\objective{This section teaches about how to use minimum spanning trees to segment an image.}

\section*{Image Segmentation}

%Lab \ref{MSTImgSeg}

One application of Minimal Spanning Trees (MSTs) is image segmentation.
Kruskal's algorithm is especially good at this.
You can convert an image into graph. Each pixel is a vertex and you define weights between the nodes. You then build the minimal spanning tree. If you take away the edge with the greatest weight you will get two forests (removing an edge from any graph with no cycles will create two forests). Then you can continue doing this to each forest to split up those forests. Each forest corresponds to a segment of the image.
Let $k$ be the number of divisions that is wanted and $n$ be the number of nodes.
Kruskal's algorithm is performed until $n-(k+1)$ edges are added which is the same as taking out the $k$ edges with greatest weights.

There are many different ways to turn an image into a graph and weight the edges.
A simple, yet effective, version is to make every pixel a node and the edges are the difference in intensities in the four cardinal directions.

This means that there are less than $4n$ edges.
Other image segmentation algorithms have to use $n^2$ space.
This gives the MST algorithm a critical advantage over other image segmentation algorithms.

\begin{problem}
Write a function that takes a black and white image as input and outputs a list of the edges and a list of nodes.
Store the edges using the form \li{(node,node,weight)}.
\end{problem}

The provided Kruskal's algorithm takes as inputs the list of nodes, the list of edges and the number of divisions desired.
The number of divisions often has to be higher than the actual number that is needed because sometimes one or two pixels form a division because the difference between them and the pixels around them is so great.
You will have to adjust the number of divisions until the desired result is found.  See Figure 9.1.

\begin{problem}
Perform the image segmentation algorithm on the image, then graph the original image and the three largest divisions.
(Use the Counter class from collections to find the number of pixels in each division.)
\end{problem}

\begin{problem}
Make a division of the image a different color.
\end{problem}

This algorithm can also be extended to 3D images.
One way to do this is to make each pixel a node and have the edges run in the eight cardinal directions, i.e. the pixel directly above it, the pixel below it, etc.

This algorithm can also be used to segment other data such as connections on Facebook.

\vfill
\begin{figure}[ht]
\begin{minipage}[b]{0.47\linewidth}
\centering
\includegraphics[width=\textwidth]{MSTseg1.jpg}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.47\linewidth}
\centering
\includegraphics[width=\textwidth]{MSTseg2.jpg}
\end{minipage}
\begin{minipage}[b]{0.47\linewidth}
\centering
\includegraphics[width=\textwidth]{MSTseg3.jpg}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.47\linewidth}
\centering
\includegraphics[width=\textwidth]{MSTseg4.jpg}
\end{minipage}
\caption{The original image is in the top left hand corner. The three larges segments are shown in the other corners. The original image was 498x498 and 50000 divisions were used.}
\end{figure}
\vfill


\documentclass{article}
\usepackage{amsmath}

\title{Sum of Finite Geometric Series}
\author{Steven Xia}

\begin{document}
\maketitle

\section{Statement}
$$\sum_{k=0}^n b^k = \frac{b^{n+1} - 1}{b - 1}$$

\section{Proof}

\subsection{Verification by Intuition}
Written in base $b$, $b^k$ is represented as a one followed by $k$ zeroes.
As such, the sum in the statement above is represented as $n+1$ ones in base $b$.

Continuing, $b^{n+1}$ is represented as a one followed by $n+2$ zeroes.
This means that $b^{n+1} - 1$ is represented as $n+1$ characters representing the value of $b - 1$ (in the case of base ten, this character would be nine).
Finally, dividing the number by $b-1$ results in $n+1$ ones, which is equal to the value of the sum as shown initially.

\subsection{Proof by Induction}

The base case, $n=0$, is trivial; both sides evaluate to 1.

\subsubsection{Inductive step: $n\geq1$}
\begin{align}
    \frac{b^{n+1} - 1}{b - 1} &= \frac{b^n - 1}{b - 1} + b^n \\
                              &= \frac{b^n - 1 + (b - 1)b^n}{b - 1} \\
                              &= \frac{(1 + (b - 1))b^n - 1}{b - 1} \\
                              &= \frac{(b)b^n - 1}{b - 1} \\
                              &= \frac{b^{n+1} - 1}{b - 1}
\end{align}

\end{document}


\lab{Algorithms}{Eigenvalue Solvers And Markov Chains}{Eigenvalue Solvers And Markov Chains}
\objective{Implement the QR algorithm for finding eigenvalues and learn Markov chains.}
\label{lab:EigSolve}

\section*{Eigenvalues are hard to find}
Finding the eigenvalues of $n \times n$ matrix $A$ means solving the following equation, where $x$ is a nonzero vector and $\lambda$ is a scalar.
\begin{align}
A x &= \lambda x \notag \\
A x - \lambda x &= 0 \notag \\
(A - \lambda I)x  &= 0 \label{eq:singularity}
\end{align}
Since $x$ is nonzero, \eqref{eq:singularity} means $A-\lambda I$ must be singular.
Thus $\det(A - \lambda I) = 0$.  This determinant is often notated $\det(A - \lambda I) = p(\lambda)$ and is called the \emph{characteristic polynomial} of $A$.
The roots of the characteristic polynomial are the eigenvalues of $A$.

If $A$ is $n \times n$, the degree of $p(\lambda)$ is  $n$.
Finding the roots is easy for small $n$, but it becomes difficult or impossible as $n$ increases.
Abel's Theorem  outlines the problem.

\begin{theorem}[Abel's Impossibility Theorem]
There is no general algebraic solution for solving a polynomial equation of degree $n>4$.
\label{thm:Abel}
\end{theorem}

Therefore, there is no method that will exactly find the eigenvalues of an arbitrary matrix.
This is a significant result.
In practice it means that we often rely on iterative methods, which converge to the eigenvalues.

\section*{The Power Method}
There are many such iterative methods for finding eigenvalues.
The Power Method is a simple fast way to find the largest eigenvalue and eigenvector.
You begin with a vector $x_0$, that has norm 1, and do the iteration.
\[
x_{k+1}=\frac{Ax_k}{\norm{Ax_k}}
\]
it works when
\begin{itemize}
\item The matrix $A$ has a eigenvalue that is strictly greater in magnitude than its other eigenvectors.
\item your starting vector $x_0$ has a nonzero component in the direction of the eigenvector of the dominant eigenvalue.
\end{itemize}

This often converges slowly (geometric with the ration given by $\norm{\lambda_2/\lambda_1}$ but is especially useful when your matrix has all positive values because you are guaranteed to have a dominant eigenvalue and the modulus of dominant eigenvalue is often much greater than the rest of the eigenvalues so the convergence is much faster.
Whenever you have an eigenvector you can compute its corresponding eigenvalue using the Rayleigh quotient
\[
\lambda = \frac{Ax\cdot x}{x\cdot x}
\]
The down side is that it only finds the largest eigenvector and eigenvalue. The next algorithm finds more eigenvalues.
\begin{problem}
Write a method that takes a in matrix and a tolerance and computes its largest eigenvalue and the eigenvector.
Have your starting vector be random.
Use the two-norm and have the iteration stop when the maximun change of the vector is less than the tolerance.
Test your function on positive matrices.
\end{problem}

\begin{comment}
An overview of the proof of the method is that you can write a matrix in Jordan Conical form $A=VJV^{-1}$ where $V$ is the matrix of the generalized eigenspaces.
But the first column is is the eigenvector corresponding to largest eigenvalue and $J$ is a upper trianglar matrix of eigenvalues and ones.
Note that $A^k=VJ^kV^{-1}$. The limit as $k \rightarrow \infty$ of $(\frac{1}{\lambda_1}J)^k$ is a matrix of all zeros except for a one in the upper right hand corner.
So $(\frac{A}{\norm{A}})^k \approx VJ^kV^{-1}$ So the largest eigenvalue dominates.
\end{comment}

\section*{The QR algorithm}
We will explore one of the simplest: the $QR$ algorithm.
The following recurrence describes the $QR$ Algorithm in its most basic form:
\begin{equation*}
A_0 = A, \quad A_k = Q_k R_k, \quad A_{k+1} = R_k Q_k
\end{equation*}
where $Q_k, R_k$ is the $QR$ decomposition of $A_k$.
Yes, it's as easy as it looks.
All this algorithm does at each step is find the $QR$ decomposition of $A_k$ and multiply $Q_k$ and $R_k$ together again but in the opposite order.
How does this simple algorithm find the eigenvalues of $A$?

Observe that $A_{k+1} \sim A_k$ (where $\sim$ denotes matrix similarity).
Then $A_n \sim A$ for all $n$.
This statement shows that $A_n$ has the same eigenvalues as $A$.
Preservation of eigenvalues is the first important feature that makes the algorithm work.
The other important feature is that each iteration of the algorithm effectively transfers some of the ``mass'' from the lower to the upper triangle.
Under very general conditions, $A_n$ will converge to a matrix of the form

\begin{equation*}
\label{eq:Schur form}
S =
     \begin{pmatrix}
          S_1 &* & \cdots & * \\
           0     &S_2  &  \ddots & \vdots \\
           \vdots  & \ddots & \ddots & *  \\
           0 & \cdots & 0 & S_m
    \end{pmatrix}
\end{equation*}
where $S_i$ is either a $1 \times 1$ or a $2 \times 2$ matrix.
For most matrices $A$, all the $S_i$ will be $1 \times 1$, so $S$ will be an upper triangular matrix.
In this case, $S$ is called the \emph{Schur form} of $A$.
The eigenvalues of $A$ are on the main diagonal of $S$.

The only case where $S$ is not upper triangular is when $A$ is a real but not symmetric matrix.
In this case, though $A$ is real, it may have complex eigenvalues.
These eigenvalues occur in complex conjugate pairs.
Each of these pairs corresponds to a $2 \times 2$ block in $S$, where the eigenvalues of the $2 \times 2$ block are the complex conjugate pair of eigenvalues of $A$.
In this case, $S$ is called the \emph{real Schur form} of $A$.

\subsection*{Hessenberg preconditioning}
Recall that an upper Hessenberg matrix looks like
\[
\begin{pmatrix}
* & * & * & \cdots & * \\
* & * & * & \cdots & * \\
0 & * & * & \cdots&* \\
\vdots & & \ddots & \ddots & \vdots \\
0 & \cdots & 0 & * & *\\
\end{pmatrix}
\]
and that every matrix is similar to an upper Hessenberg matrix.
Hessenberg reduction also preserves eigenvalues.
It is a good idea to reduce to Hessenberg form before continuing with the $QR$ algorithm.
You'll converge to the Schur form faster this way, since Hessenberg matrices are already close to upper triangular.

\begin{problem}
Write a function \li{qrSolver} that implements the QR algorithm as described above. Use the QR decomposition built into \li{scipy.linalg}.
Have your function accept a real-valued $n \times n$ matrix $A$, a number of iterations, and a tolerance number,
and return the corresponding estimate for all the eigenvalues of $A$.
Note that you will need to find the eigenvalues of the $2 \times 2$ $S_i$ directly, whenever there are any.
\label{prob:qr_solver}
\end{problem}

Reducing the matrix to Schur form also has the added benefit that each iteration for an $n \times n$ array can be computed in $\mathcal{O} \left( n^2 \right)$ time instead of $\mathcal{O} \left( n^3 \right)$.
One algorithm for computing the QR decomposition of an upper Hessenberg matrix was discussed in Lab \ref{lab:givens}.
This works since $Q$ in the QR factorization of an upper Hessenberg matrix is also upper Hessenberg, the product $R Q$ also turns out to be upper Hessenberg.

\begin{comment}
\begin{problem}
\label{prob:QR_eig_hessenberg}
Write a version of the QR algorithm that performs the QR algorithm by computing the Hessenberg form of a matrix, then computing various QR decompositions of the Hessenberg form of the matrix.
Use your solutions to \ref{prob:hessenberg} (where you computed the Hessenberg form of a matrix) and Problem \ref{prob:givens_hessenberg_modified} to do the necessary computations (where you computed the QR decomposition of a Hessenberg matrix and wrote code for multiplication by $Q$ that works in $\mathcal{O} \left( n^2 \right)$ time).
The solution to Problem \ref{prob:givens_hessenberg_modified} is especially important because it allows the compution of each QR decomposition and each $R Q = \left( Q^T R^T \right)$ in $\mathcal{O} \left( n^2 \right)$ time.
\end{problem}
\end{comment}

\begin{comment}
\begin{problem}
If $A$ is normal, its Schur form is diagonal.
For normal $A$, have your function additionally output the eigenvector corresponding to each eigenvalue.
Hint 1: Test your function on Hermitian and real symmetric matrices; they are both normal.
Hint 2: Your work in Problem \ref{problem:similarity proof} will help.
You have already made all the necessary calculations, you just need to store the information correctly.
\end{problem}
\end{comment}

\begin{problem}
Test your implementation with random matrices.
Try real-valued and symmetric matrices.
Compare your output to the output from the eigenvalue solver.
How many iterations are necessary?
How large can $A$ be?
\end{problem}

Be aware that the algorithm we coded today is a very un-optimized approach.
In practice, the QR algorithm as we have described it here is not generally used.
There is an improved version called the Implicit QR algorithm that iterates much more efficiently.

Further, the QR algorithm is not the only iterative method used to find eigenvalues.
Arnoldi iteration is similar to the QR algorithm but exploits sparsity.
Other methods include the Jacobi method and the Rayleigh quotient method.

As a final note, it is important to remember that eigenvalue solvers can be wrong, particularly for matrices that are ill-conditioned.
Convergence is not unconditionally guaranteed.

\section*{Markov Chains}
A Markov Chain describes a particular type of random process that
undergoes a sequence of transitions among various states. It is
characterized by the fact that all relevant information is related to its current state.
We can easily model this process using matrices.
We will start with a simple example of a frog jumping from one lily pad to another.

Suppose Fredo the frog jumps around between the three lily pads 1, 2, and 3.
These three pads are taken as the possible \emph{states} of the system, and the
lily pad on which Fredo is presently sitting is the \emph{current state}.
If Fredo is on lily pad 1 and jumps, there is a 25\% chance that it will land back on lily pad 1, a 25\% chance that it will land on lily pad 2, and a 50\% chance that it will land on lily pad 3.
We can find similar probabilities if he starts on lily pad 2 or 3.
Such probabilities are known as the \emph{transition probabilities}.
In figure \ref{fig:markov1} we have a transition diagram that reflects the various probabilities of jumping from one lily pad to another.

\begin{figure}
\includegraphics[width=\textwidth]{markov1}
\caption{Transition diagram for Fredo the Frog}
\label{fig:markov1}
\end{figure}

We can convert our transition diagram into a transition matrix, where the $(i,j)$-entry of the matrix corresponds to the probability that Fredo jumps from the $j^{th}$ lily pad to the $i^{th}$ lily pad (where $1$ is the first lily pad, $2$ is the second, and so on).
The transition matrix is
\[
A = \begin{pmatrix}
1/4 & 1/2 & 1/2\\
1/4 & 1/6 & 1/2\\
1/2 & 1/3 & 0
\end{pmatrix}
\]
Note that all of the columns add up to one.
This is important.

If Fredo is on lily pad 1, where will it be after two jumps?
By multiplying the matrix $A$ by itself, we have (approximately)

\[
A^2 = \begin{pmatrix}
0.4375 & 0.3750 & 0.3750\\
0.3542 & 0.3194 & 0.2083\\
0.2083 & 0.3056 & 0.4167
\end{pmatrix}
\]
From this, we infer that there is a 43.75\% chance the frog will still be on lily pad 1 after two jumps.
Note that it might have jumped from 1 to 1 to 1, denoted $1 \rightarrow 1 \rightarrow 1$, or it could have jumped to one of the other lily pads and then back again, that is, either $1 \rightarrow 2 \rightarrow 1$ or $1 \rightarrow 3 \rightarrow 1$.
In addition, there is a 35.42\% chance it will be on lily pad 2 and a 20.83\% chance that it will be on lily pad 3.
Using Python, we can type in our transition matrix and see where Fredo will be after 5, 10, 20 or 100 jumps.

\begin{lstlisting}
#Remember, the 1.'s in the numerator force floating point division
>>> A = np.array([[1./4,1./2,1./2],[1./4,1./6,1./2],[1./2,1./3,0]])
>>> np.linalg.matrix_power(A,5)
>>> np.linalg.matrix_power(A,10)
>>> np.linalg.matrix_power(A,20)
>>> np.linalg.matrix_power(A,100)
\end{lstlisting}

Note that as we take higher powers it appears that the limit goes to
\[
A^\infty = \begin{pmatrix}
0.4 & 0.4 & 0.4\\
0.3 & 0.3 & 0.3\\
0.3 & 0.3 & 0.3
\end{pmatrix},
\]
and this can, in fact, be proven carefully.
This means that after several jumps, the probability that we will find Fredo on a given lily pad will have nothing to do with where he started initially.

We can generalize this notion beyond that of frogs and lily pads.
Let the state distribution of our system be represented by a probability vector
\[
\x = \begin{bmatrix}
x_1\\
x_2\\
\vdots\\
x_n
\end{bmatrix}
\]
where each entry represents the probability of being in that state.
Note that each entry is non-negative and the sum of all the entries adds up to one.
For example, in the frog example, if we know initially that it is on lily pad 1, then we have the probability vector
\[
\x_0 = \begin{bmatrix}
1\\
0\\
0
\end{bmatrix}
\]
because we know for certainty (100\%) that Fredo is in the first state.
After one jump, we have
\[
\x_1 = A \x_0 = \begin{bmatrix}
0.25\\
0.25\\
0.50
\end{bmatrix}
\]
After two jumps, we have
\[
\x_2 = A \x_1 = A^2 \x_0 = \begin{bmatrix}
0.4375\\
0.3542\\
0.2083
\end{bmatrix}
\]
After a large number of jumps $(n>>1)$, we have
\[
\x_n = A \x_{n-1} = \dots = A^n \x_0 \approx \begin{bmatrix}
0.4\\
0.3\\
0.3
\end{bmatrix}
\]
Since all of the columns are the same for $A^\infty$, it follows that for any initial probability vector $\x_0$, we get the same limiting output, or in other words, all initial vectors converge to the same point, call it $\x_\infty$.
Moreover, we have that
\[
\x_\infty = A \x_\infty
\]
This is called a \emph{stable fixed point}.
How can we check that a stable fixed point exists?

Notice this is just the power method iteration without dividing by the norm. Since the all of the columns of $A$ sum to one and $x_0$ sums to one the one norm of $Ax_k=1$ for all $k$. So this goes to a stable fixed point and that fixed point turns out to be the eigenvector corresponding to the largest eigenvalue. Note that all the columns sum up to one and that a matrix and its transpose have the same eigenvalues. It can be shown that if all the rows of a positive matrix sum to the same number that number is the largest eigenvalue and the corresponding eigenvector is the one vector. So the $A.T$ has the eigenvalue $\lambda=1$ so $A$. So for any Markov chain the largest eigenvalue will be $\lambda=1$.

\begin{example}
Consider the Markov chain given by
\[
A = \begin{pmatrix}
0.5 & 0.3 & 0.4\\
0.2 & 0.2 & 0.3\\
0.3 & 0.5 & 0.3
\end{pmatrix}.
\]

We do this via Python:
\begin{lstlisting}
>>> from scipy import linalg as la
>>> A = np.array([[.5,.3,.4],[.2,.2,.3],[.3,.5,.3]])
>>> V = la.eig(A)[1]
\end{lstlisting}
Note that the entries in the $\lambda=1$ eigenvector do not generally add up to one.
Indeed, any multiple of an eigenvector is an eigenvector.
So we need to multiply it by the appropriate constant so that all of the entries add up to one.
\begin{lstlisting}
>>> x = V[:,0]
>>> x = x/np.sum(x);x
array([ 0.41836735,  0.23469388,  0.34693878])
\end{lstlisting}
We can check this answer by taking $A$ to a high exponent, say $A^{100}$.
\end{example}

\begin{problem}
Suppose a basketball player's success at shooting free throws can be described with the following Markov chain
\[
A = \begin{pmatrix}.75&.50\\.25&.50\end{pmatrix}
\]
where the first state corresponds to success and the second state to failure.
\begin{enumerate}
\item If the player makes his first free throw, what is the probability that he also makes his third one?
\item What is the player's average free throw percentage?
\end{enumerate}
\label{prob:markov_freethrow}
\end{problem}

\begin{problem}
Consider the Markov process given by the transition diagram in Figure \ref{fig:markov2}.
\begin{figure}[H]
\includegraphics[width=\textwidth]{markov2}
\caption{Transition diagram}
\label{fig:markov2}
\end{figure}

\begin{enumerate}
\item Find the transition matrix.
\item If the Markov process is in state 1 initially, find the probability that it is in state 2 after two transitions.
\item Find the stable fixed point if it exists.
\end{enumerate}
\label{prob:markov_stablept}
\end{problem}


\subsection{Introduction}

Linear regression is a statistical approach for modelling relationship between a dependent variable with a given set of independent variables.

In order to provide a basic understanding of linear regression, we start with the most basic version of linear regression, i.e. Simple linear regression.

\subsubsection{Simple Linear Regression}
Simple linear regression is an approach for predicting a \textbf{response} using a \textbf{single feature}.

It is assumed that the two variables are linearly related. Hence, we try to find a linear function that predicts the response value(y) as accurately as possible as a function of the feature or independent variable(x).

Now, the task is to find a line which fits best in above scatter plot so that we can predict the response for any new feature values. (i.e a value of x not present in dataset)

This line is called \textbf{Regression line}. This equation of regression line is represented as:

\begin{equation}
h(x_{i}) = \beta_{0} + \beta_{1}x_{i};
\end{equation}

Here, $\bullet$ represents the predicted response value for i-th observation.

$\bullet$ b\_0 and b\_1 are regression coefficients and represent y-intercept and slope of regression line respectively.

To create our model, we must \q{learn} or estimate the values of regression coefficients b\_0 and b\_1. And once we've estimated these coefficients, we can use the model to predict responses!
For this we use Least Squared Technique.

\subsubsection{Least Square Technique}.
\begin{equation}
y_{i}=\beta_{0} + \beta_{1} \times x_{i} + \epsilon_{i} = h(x_{i+}) + \epsilon_{i}
\implies \epsilon_{i} = y_{i} - h(x_{i});
\end{equation}

Here, e\_i is residual error in ith observation.  We try to  minimize the total residual error. We define the squared error or cost function, J as:
\begin{equation}
J(\beta_{0},\beta_{1}) = \frac{1}{2n} \sum_{i=1}^{n} \epsilon_{i}^2;
\end{equation}

and our task is to find the value of b\_0 and b\_1 for which J(b\_0, b\_1) is minimum! Without going into the mathematical details, we present the result here:

\begin{equation}
\beta_{1} = \frac{SS_{xy}}{SS_{xx}};
\end{equation}

\begin{equation}
\beta_{0} =  \bar{y} - \beta_{1}\bar{x};
\end{equation}

where SS\_xy is the sum of cross-deviations of y and x:

\begin{equation}
SS_{xy} = \sum_{i=1}^{n} (x-\bar{x})(y-\bar{y})= \sum_{i=1}^{n} y_{i}x_{i} -n\bar{x}\bar{y};
\end{equation}

and SS\_xx is the sum of squared deviations of x:

\begin{equation}
SS_{xx} = \sum_{i=0}^{n}(x_{i}-\bar{x})^2= \sum_{i=0}^{n} x_{i}^2 - n(\bar{x})^2;
\end{equation}

\subsubsection{Multiple Linaer Regression}
Multiple linear regression attempts to model the relationship between two or more features and a response by fitting a linear equation to observed data. Clearly, it is nothing but an extension of Simple linear regression. Consider a dataset with p features(or independent variables) and one response(or dependent variable). Also, the dataset contains n rows/observations.

X(features matrix) = a matrix of size nXp where x\_{i} denotes the values of jth feautres for ith observation.

So,
$$
\begin{pmatrix}
x_{1,1} & x_{1,2} & \cdots & x_{1,n} \\
x_{2,1} & x_{2,2} & \cdots & x_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
x_{m,1} & x_{m,2} & \cdots & x_{m,n}
\end{pmatrix}
$$

and

y = $
\begin{pmatrix}
y_{1} \\ y_{2} \\ y_{3} \\
\vdots y_{n}
\end{pmatrix}
$

The regression line for p is represented as:

\begin{equation}
h(x_{i}) = \beta_{0} + \beta_{1}x_{i1}+\beta_{2}x_{i2}+ ... +\beta_{p}x_{ip};
\end{equation}

where h(x\_i) is predicted response value for ith observation and b\_0,b\_1, ...,b\_p are the regression coefficients.
Also, we can write:

\begin{equation}
y_{i} = \beta_{0} + \beta_{1}x_{i1} + \beta_{2}x_{i2} + ... +\beta_{p}x_{ip} + \epsilon_{i};
\end{equation}

or

\begin{equation}
y_{i} = h(x_{i}) + \epsilon_{i} \implies \epsilon_{i} = y_{i} - h(x_{i});
\end{equation}

We can generalize our linear model a little bit more by representing feature matrix X as:

$\begin{pmatrix}
1 & x_{1,1} & x_{1,2} & \cdots & x_{1,p} \\
1 & x_{2,1} & x_{2,2} & \cdots & x_{2,p} \\
\vdots  & \vdots  & \ddots & \vdots  \\
1 & x_{n,1} & x_{n,2} & \cdots x a_{n,p}
\end{pmatrix}$

So now, the linear model can be expressed in terms of matrices as:

$$
y = X\beta + \epsilon
$$

where,\newline

$$\beta =
\begin{bmatrix}
\beta_0\\  \beta_1\\  .\\  .\\  \beta_p
\end{bmatrix}$$

and

$$\epsilon =
\begin{bmatrix}
\varepsilon_1\\  \varepsilon_2\\  .\\  .\\  \varepsilon_n
\end{bmatrix}
$$

Now, we determine estimate of b, i.e. b' using Least Squares method.

As already explained, Least Squares method tends to determine b for which total residual error is minimized.

We present the result directly here:
$$\hat{\beta} = (XX')^{-1} {X}'y $$

where 'represents the transpose of the matrix while -1 represents the matrix inverse.

Knowing the least square estimates, b', the multiple linear regression model can now be estimated as:
$$\hat{y} = X\hat{\beta}$$

where $\hat{y}$ is estimated response vector.

\subsubsection{Assumptions}
the basic assumptions that a linear regression model makes regarding a dataset on which it is applied:

\paragraph{Linear relationship:}
Relationship between response and feature variables should be linear. The linearity assumption can be tested using scatter plots. As shown below, 1st figure represents linearly related variables where as variables in 2nd and 3rd figure are most likely non-linear. So, 1st figure will give better predictions using linear regression.

\paragraph{Little or no multi-collinearity:}
It is assumed that there is little or no multicollinearity in the data. Multicollinearity occurs when the features (or independent variables) are not independent from each other.

\paragraph{Little or no auto-correlation:}
Another assumption is that there is little or no autocorrelation in the data. Autocorrelation occurs when the residual errors are not independent from each other. You can refer here for more insight into this topic.

\paragraph{Homoscedasticity:}
Homoscedasticity describes a situation in which the error term (that is, the \q{noise} or random disturbance in the relationship between the independent variables and the dependent variable) is the same across all values of the independent variables. As shown below, figure 1 has homoscedasticity while figure 2 has heteroscedasticity.

\subsection{Application}
Some of the application of linear regression model are :

\begin{enumerate}
\item Trend lines: A trend line represents the variation in some quantitative data with passage of time (like GDP, oil prices, etc.). These trends usually follow a linear relationship. Hence, linear regression can be applied to predict future values. However, this method suffers from a lack of scientific validity in cases where other potential changes can affect the data.
\item Economics: Linear regression is the predominant empirical tool in economics. For example, it is used to predict consumption spending, fixed investment spending, inventory investment, purchases of a country's exports, spending on imports, the demand to hold liquid assets, labor demand, and labor supply.
\item Finance: Capital price asset model uses linear regression to analyze and quantify the systematic risks of an investment.
\item Biology: Linear regression is used to model causal relationships between parameters in biological systems.
\end{enumerate}

\subsection{Citing}
A well known implementation is given in \href{https://www.geeksforgeeks.org/linear-regression-python-implementation/}{Geeks-for-Geeks} and a well discussion is done at \href{https://en.wikipedia.org/wiki/Linear_regression}{Linear regression Wiki}.

For our project we will use simple linear regression (with features the difference in the number of bits).