\subsection{Orthogonal projections}

An important use of the Gram-Schmidt Process is in orthogonal projections, the focus of this section.

You may recall that a subspace of $\R^n$ is a set of vectors
which contains the zero vector, and is closed under addition and
scalar multiplication. Let's call such a subspace $W$. In particular,
a plane in $\R^n$ which contains the origin, $ (0,0,\ldots,0)$, is a subspace of $\R^n$.

Suppose a point $Y$ in $\R^n$ is not contained in $W$, then what
point $Z$ in $W$ is closest to $Y$? Using the Gram-Schmidt Process, we
can find such a point. Let $\vect{y}, \vect{z}$ represent the position
vectors of the points $Y$ and $Z$ respectively, with
$\vect{y}-\vect{z}$ representing the vector connecting the two points
$Y$ and $Z$.  It will follow that if $Z$ is the point on $W$ closest
to $Y$, then $\vect{y} - \vect{z}$ will be perpendicular to $W$ (can you see why?); in
other words, $\vect{y} - \vect{z}$ is orthogonal to $W$ (and to every
vector contained in $W$) as in the following diagram.

\begin{center}
\begin{tikzpicture}
\draw[fill=lightgray](-3,0,2)--(-2,0.75,0)--(2,0.75,0)--(1,0,2)--(-3,0,2);
\draw(-2,0,0) rectangle (-1.8, 0.2,0);
\draw[thick,->](0,0,0)--(-2,0,0);
\draw[thick,->](0,0,0)--(-2,2,0);
\draw[red,thick,->](-2,2,0)--(-2,0,0);
\node[right] at (0,0,0){$0$};
\node[above] at (-2,2,0){$Y$};
\node[below left] at (-2,0,0){$Z$};
\node[below] at (-1,0,0){$\vect{z}$};
\node[above right] at (-1,1,0){$\vect{y}$};
\node[left] at (-2,1,0){$\vect{y}-\vect{z}$};
\node at (1,0.5,0){$W$};
\end{tikzpicture}
\end{center}

The vector $\vect{z}$ is called the \textbf{orthogonal projection} of
$\vect{y}$ on $W$. The definition is given as follows.

\begin{definition}{Orthogonal projection}{orthogonal-projection}
Let $W$ be a subspace of $\R^n$, and $Y$ be any point in
$\R^n$. Then the orthogonal projection of $Y$ onto $W$ is given by
\[
\vect{z} = \proj_{W}(\vect{y})
=
\paren{\frac{\vect{y} \dotprod \vect{w}_1}{ \norm{\vect{w}_1}^2}} \vect{w}_1
+
\paren{\frac{\vect{y} \dotprod \vect{w}_2}{ \norm{\vect{w}_2}^2}} \vect{w}_2
+
\ldots
+
\paren{\frac{\vect{y} \dotprod \vect{w}_m}{ \norm{\vect{w}_m}^2}} \vect{w}_m
\]
where $\set{\vect{w}_1, \vect{w}_2,\ldots, \vect{w}_m}$ is any orthogonal basis of $W$.
\end{definition}

Therefore, in order to find the orthogonal projection, we must first
find an orthogonal basis for the subspace. Note that one could use an
orthonormal basis, but it is not necessary in this case since as you
can see above the normalization of each vector is included in the
formula for the projection.

Before we explore this further through an example, we show that the
orthogonal projection does indeed yield a point $Z$ (the point whose position
vector is the vector $\vect{z}$ above) which is the point of $W$
closest to $Y$.

\begin{theorem}{Approximation theorem}{approximation}
Let $W$ be a subspace of $\R^n$ and $Y$ any point in
$\R^n$. Let $Z$ be the point whose position vector is the
orthogonal projection of $Y$ onto $W$. \\
Then, $Z$ is the point in $W$ closest to $Y$.
\end{theorem}

\begin{proof}
First $Z$ is certainly a point in $W$  since it is in the span of a basis of $W$.

To show that $Z$ is the point in $W$ closest to $Y$, we wish to show
that $|\vect{y}-\vect{z}_1| > |\vect{y}-\vect{z}|$ for all $\vect{z}_1
\neq \vect{z} \in W$.  We begin by writing $\vect{y}-\vect{z}_1 =
(\vect{y} - \vect{z}) + (\vect{z} -
\vect{z}_1)$.  Now, the vector $\vect{y} - \vect{z}$ is orthogonal to
$W$, and $\vect{z} - \vect{z}_1$ is contained in $W$. Therefore these
vectors are orthogonal to each other. By the Pythagorean Theorem, we
have that
\[
\norm{\vect{y} - \vect{z}_1}^2 = \norm{\vect{y} - \vect{z}} ^2 + \norm{\vect{z} -\vect{z}_1} ^2 > \norm{\vect{y} - \vect{z}}^2
\]
This follows because $\vect{z} \neq \vect{z}_1$ so
$\norm{\vect{z} -\vect{z}_1}^2 > 0$.

Hence, $\norm{\vect{y} - \vect{z}_1}^2 > \norm{
\vect{y} - \vect{z}}^2$. Taking the square root of each
side, we obtain the desired result.
\end{proof}

Consider the following example.

\begin{example}{Orthogonal projection}{orthogonal-projection}
Let $W$ be the plane through the origin given by the equation $x - 2y
+ z = 0$. \\
Find the point in $W$ closest to the point $Y = (1,0,3)$.
\end{example}

\begin{solution}
We must first find an orthogonal basis for $W$. Notice that $W$ is
characterized by all points $(a,b,c)$ where $c = 2b-a$. In other
words,
\[
W =
\begin{mymatrix}{c}
a \\
b \\
2b - a
\end{mymatrix}
=
a \begin{mymatrix}{c}
1 \\
0 \\
-1
\end{mymatrix}
+
b \begin{mymatrix}{c}
0 \\
1 \\
2
\end{mymatrix},
\;
a,b \in \R
\]

We can thus write $W$ as
\begin{eqnarray*}
W &=& \mbox{span} \set{\vect{u}_1, \vect{u}_2 } \\
 &=& \mbox{span}
\set{
\begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix},
\begin{mymatrix}{c}
0 \\
1 \\
2
\end{mymatrix}
}
\end{eqnarray*}

Notice that this span is a basis of $W$ as it is linearly
independent. We will use the Gram-Schmidt Process to convert this to
an orthogonal basis, $\set{\vect{w}_1, \vect{w}_2 }$. In this
case, as we remarked it is only necessary to find an orthogonal basis, and it is not
required that it be orthonormal.

\[
\vect{w}_1 = \vect{u}_1 = \begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix}
\]
\begin{eqnarray*}
\vect{w}_2 &=& \vect{u}_2 - \paren{\frac{ \vect{u}_2 \dotprod \vect{w}_1}{\norm{\vect{w}_1}^2}}  \vect{w}_1\\
&=& \begin{mymatrix}{c}
0 \\
1 \\
2
\end{mymatrix}
-
\paren{
\frac{-2}{2}}
\begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix}
\\
&=&
\begin{mymatrix}{c}
0 \\
1 \\
2
\end{mymatrix}
+
\begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix} \\
&=&
\begin{mymatrix}{c}
1 \\
1 \\
1
\end{mymatrix}
\end{eqnarray*}

Therefore an orthogonal basis of $W$ is
\[
\set{\vect{w}_1, \vect{w}_2 } =
\set{
\begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix},
\begin{mymatrix}{c}
1 \\
1 \\
1
\end{mymatrix}
}
\]

We can now use this basis to find the orthogonal projection of the
point $Y=(1,0,3)$ on the subspace $W$. We will write the position
vector $\vect{y}$ of $Y$ as $\vect{y} = \begin{mymatrix}{c} 1 \\ 0 \\ 3
\end{mymatrix}$. Using Definition~\ref{def:orthogonal-projection}, we compute the projection as follows:
\begin{eqnarray*}
\vect{z} &=& \proj_{W}(\vect{y})\\
&=&
\paren{\frac{\vect{y} \dotprod \vect{w}_1}{ \norm{\vect{w}_1}^2}} \vect{w}_1
+
\paren{\frac{\vect{y} \dotprod \vect{w}_2}{ \norm{\vect{w}_2}^2}} \vect{w}_2 \\
&=&
\paren{\frac{-2}{2}} \begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix}
+
\paren{\frac{4}{3}}
\begin{mymatrix}{c}
1 \\
1 \\
1
\end{mymatrix} \\
&=&
\begin{mymatrix}{c}
\vspace{0.05in}\frac{1}{3} \\
\vspace{0.05in}\frac{4}{3} \\
\vspace{0.05in}\frac{7}{3}
\end{mymatrix}
\end{eqnarray*}

Therefore the point $Z$ on $W$ closest to the point $(1,0,3)$  is $\paren{\frac{1}{3}, \frac{4}{3}, \frac{7}{3}}$.

\end{solution}

Recall that the vector $\vect{y} - \vect{z}$ is perpendicular
(orthogonal) to all the vectors contained in the plane $W$. Using a
basis for $W$, we can in fact find all such vectors which are
perpendicular to $W$. We call this set of vectors the
\textbf{orthogonal complement}\index{orthogonal complement} of $W$ and
denote it $W^{\perp}$.

\begin{definition}{Orthogonal complement}{orthogonal-complement}
Let $W$ be a subspace of $\R^n$. Then the orthogonal
complement of $W$, written $W^{\perp}$, is the set of all vectors
$\vect{x}$ such that $\vect{x} \dotprod \vect{z} = 0$ for all vectors
$\vect{z}$ in $W$.
\[
W^{\perp} = \set{\vect{x} \in \R^n \; \mbox{such that} \;
\vect{x} \dotprod \vect{z} = 0 \; \mbox{for all} \; \vect{z} \in W}
\]
\end{definition}

The orthogonal complement is defined as the set of all vectors which are orthogonal to all vectors in the original subspace. It turns out that it is sufficient that the vectors in the orthogonal complement be orthogonal to a spanning set of the original space.

\begin{proposition}{Orthogonal to spanning set}{orthogonal-spanning-set}
Let $W$ be a subspace of $\R^n$ such that $W = \sspan \set{\vect{w}_1, \vect{w}_2,\ldots, \vect{w}_m }$. Then $W^{\perp}$ is the set of all vectors which are orthogonal to each $\vect{w}_i$ in the spanning set.
\end{proposition}

The following proposition demonstrates that the orthogonal complement of a subspace is itself a subspace.

\begin{proposition}{The orthogonal complement}{subspace-complement}
Let $W$ be a subspace of $\R^n$. Then the orthogonal complement $W^{\perp}$ is also a subspace of $\R^n$.
\end{proposition}

Consider the following proposition.

\begin{proposition}{Orthogonal complement of $\R^n$}{complement-of-rn}
The complement of $\R^n$ is the set containing the zero vector:
\[
 (\R^n)^{\perp} = \set{\vect{0} }
\]
Similarly,
\[
\set{\vect{0} }^{\perp} = (\R^n)
\]
\end{proposition}

\begin{proof}
Here, $\vect{0}$ is the zero vector of $\R^n$.
Since $\vect{x}\dotprod\vect{0}=0$ for all $\vect{x}\in\R^n$,
$\R^n\subseteq\set{\vect{0}}^{\perp}$.
Since $\set{\vect{0}}^{\perp}\subseteq\R^n$, the equality follows,
i.e., $\set{\vect{0}}^{\perp}=\R^n$.

Again, since $\vect{x}\dotprod\vect{0}=0$ for all $\vect{x}\in\R^n$,
$\vect{0}\in (\R^n)^{\perp}$, so $\set{\vect{0}}\subseteq(\R^n)^{\perp}$.
Suppose $\vect{x}\in\R^n$, $\vect{x}\neq\vect{0}$.
Since $\vect{x}\dotprod\vect{x}=||\vect{x}||^2$ and $\vect{x}\neq\vect{0}$,
$\vect{x}\dotprod\vect{x}\neq 0$, so $\vect{x}\not\in(\R^n)^{\perp}$.
Therefore $(\R^n)^{\perp}\subseteq \set{\vect{0}}$, and thus
$(\R^n)^{\perp}=\set{\vect{0}}$.
\end{proof}

In the next example, we will look at how to find
$W^{\perp}$.

\begin{example}{Orthogonal complement}{orthogonal-complement}
Let $W$ be the  plane through the origin given by the equation  $x - 2y + z = 0$. Find
a basis for the orthogonal complement of $W$.
\end{example}

\begin{solution}

From Example~\ref{exa:orthogonal-projection} we know that we can write $W$ as
\[
W = \mbox{span} \set{\vect{u}_1, \vect{u}_2 } = \mbox{span}
\set{
\begin{mymatrix}{r}
1 \\
0 \\
-1
\end{mymatrix},
\begin{mymatrix}{c}
0 \\
1 \\
2
\end{mymatrix}
}
\]

In order to find $W^{\perp}$, we need to find all $\vect{x}$ which are orthogonal to every vector in this span.

Let $\vect{x} = \begin{mymatrix}{c}
x_1 \\
x_2 \\
x_3
\end{mymatrix}$.
In order to satisfy $\vect{x} \dotprod \vect{u}_1 = 0$, the following equation must hold.
\[
x_1 - x_3 = 0
\]

In order to satisfy $\vect{x} \dotprod \vect{u}_2 = 0$, the following equation must hold.
\[
x_2 + 2x_3 = 0
\]

Both of these equations must be satisfied, so we have the following system of equations.
\[
\begin{array}{c}
x_1 - x_3 = 0 \\
x_2 + 2x_3 = 0
\end{array}
\]

To solve, set up the augmented matrix.

\[
\begin{mymatrix}{rrr|r}
1 & 0 & -1 & 0 \\
0 & 1 & 2 & 0
\end{mymatrix}
\]

Using Gaussian Elimination, we find that $W^{\perp} = \mbox{span} \set{\begin{mymatrix}{r}
1 \\
-2 \\
1
\end{mymatrix}
}$, and hence
$\set{\begin{mymatrix}{r}
1 \\
-2 \\
1
\end{mymatrix}
}$ is a basis for  $W^{\perp}$.
\end{solution}

The following results summarize the important properties of the orthogonal projection.

\begin{theorem}{Orthogonal projection}{orthogonal-projection}
Let $W$ be a subspace of $\R^n$, $Y$ be any point in $\R^n$, and let $Z$ be the point in $W$ closest to $Y$. Then,
\begin{enumerate}
\item
The position vector $\vect{z}$ of the point $Z$ is given by $\vect{z} = \proj_{W}(\vect{y})$
\item
$\vect{z} \in W$ and $\vect{y} - \vect{z} \in W^{\perp}$
\item
$| Y - Z | < | Y - Z_1 |$ for all $Z_1 \neq Z \in W$
\end{enumerate}
\end{theorem}

Consider the following example of this concept.

\begin{example}{Find a vector closest to a given vector}{vector-closest-vector}

Let
\[ \vect{x}_1=\begin{mymatrix}{c} 1\\ 0\\ 1\\ 0 \end{mymatrix},
\vect{x}_2=\begin{mymatrix}{c} 1\\ 0\\ 1\\ 1 \end{mymatrix},
\vect{x}_3=\begin{mymatrix}{c} 1\\ 1\\ 0\\ 0 \end{mymatrix},
\mbox{ and }
\vect{v}=\begin{mymatrix}{c} 4\\ 3\\ -2\\ 5 \end{mymatrix}. \]
We want to find the vector in
$W =\sspan\set{\vect{x}_1, \vect{x}_2,\vect{x}_3}$
closest to $\vect{y}$.
\end{example}

\begin{solution}
We will first use the Gram-Schmidt Process to construct the orthogonal basis, $B$, of $W$:
\[ B=\set{
\begin{mymatrix}{c} 1\\ 0\\ 1\\ 0 \end{mymatrix},
\begin{mymatrix}{c} 0\\ 0\\ 0\\ 1 \end{mymatrix},
\begin{mymatrix}{r} 1\\ 2\\ -1\\ 0 \end{mymatrix}
}.\]

By Theorem~\ref{thm:orthogonal-projection},
\[ \proj_U(\vect{v}) =
\frac{2}{2} \begin{mymatrix}{c} 1\\ 0\\ 1\\ 0 \end{mymatrix} +
\frac{5}{1}\begin{mymatrix}{c} 0\\ 0\\ 0\\ 1 \end{mymatrix} +
\frac{12}{6}\begin{mymatrix}{r} 1\\ 2\\ -1\\ 0 \end{mymatrix}
= \begin{mymatrix}{r} 3\\ 4\\ -1\\ 5 \end{mymatrix}
\]
is the vector in $U$ closest to $\vect{y}$.
\end{solution}

Consider the next example.

\begin{example}{Vector written as a sum of two vectors}{sum-of-two-vectors}
Let $W$ be a subspace given by $W = \mbox{span} \set{
\begin{mymatrix}{c}
1 \\
0 \\
1 \\
0 \\
\end{mymatrix},
\begin{mymatrix}{c}
0 \\
1 \\
0 \\
2 \\
\end{mymatrix}
}$, and $Y = (1,2,3,4)$. \\
Find the point $Z$ in $W$ closest to $Y$, and moreover write $\vect{y}$ as the sum of a  vector in $W$ and a vector in $W^{\perp}$.
\end{example}

\begin{solution}
From Theorem~\ref{thm:approximation}, the point $Z$ in $W$ closest to
$Y$ is given by $\vect{z} = \proj_{W}(\vect{y})$.

Notice that since the above vectors already give an orthogonal basis for $W$, we have:

\begin{eqnarray*}
\vect{z} &=& \proj_{W}(\vect{y})\\
&=&
\paren{\frac{\vect{y} \dotprod \vect{w}_1}{ \norm{\vect{w}_1}^2}} \vect{w}_1
+
\paren{\frac{\vect{y} \dotprod \vect{w}_2}{ \norm{\vect{w}_2}^2}} \vect{w}_2 \\
&=&
\paren{\frac{4}{2}} \begin{mymatrix}{c}
1 \\
0 \\
1 \\
0
\end{mymatrix}
+
\paren{\frac{10}{5}}
\begin{mymatrix}{c}
0 \\
1 \\
0 \\
2
\end{mymatrix} \\
&=&
\begin{mymatrix}{c}
2 \\
2 \\
2 \\
4
\end{mymatrix}
\end{eqnarray*}

Therefore the point in $W$ closest to $Y$ is $Z = (2,2,2,4)$. \\

Now, we need to write $\vect{y}$ as the sum of a vector in $W$ and a
vector in $W^{\perp}$. This can easily be done as follows:
\[
\vect{y} = \vect{z} + (\vect{y} - \vect{z})
\]
since $\vect{z}$ is in $W$ and as we have seen $\vect{y} - \vect{z}$ is in  $W^{\perp}$. \\
The vector $\vect{y} - \vect{z}$ is given by
\[
\vect{y} - \vect{z} = \begin{mymatrix}{c}
1 \\
2 \\
3 \\
4
\end{mymatrix}
-
\begin{mymatrix}{c}
2 \\
2 \\
2 \\
4
\end{mymatrix}
 =
\begin{mymatrix}{r}
-1 \\
0 \\
1 \\
0
\end{mymatrix}
\]
Therefore, we can write $\vect{y}$ as
\[
\begin{mymatrix}{c}
1 \\
2 \\
3 \\
4
\end{mymatrix}
=
\begin{mymatrix}{c}
2 \\
2 \\
2 \\
4
\end{mymatrix}
+
\begin{mymatrix}{r}
-1 \\
0 \\
1 \\
0
\end{mymatrix}
\]
\end{solution}

\begin{example}{Point in a plane closest to a given point}{closest-plane}
Find the point $Z$ in the plane $3x+y-2z=0$ that is closest to
the point $Y=(1,1,1)$.
\end{example}

\begin{solution}
The solution will proceed as follows.
\begin{enumerate}
\item Find a basis $X$ of the subspace $W$ of $\R^3$ defined by
the equation  $3x+y-2z=0$.
\item Orthogonalize the basis $X$ to get an orthogonal basis
$B$ of $W$.
\item Find the projection on $W$ of the position vector of
the point $Y$.
\end{enumerate}

We now begin the solution.
\begin{enumerate}
\item $3x+y-2z=0$ is a system of one equation in three variables.
Putting the augmented matrix in {\rref}:
\[
\begin{mymatrix}{rrr|r} 3 & 1 & -2 & 0 \end{mymatrix}
\rightarrow
\begin{mymatrix}{rrr|r} 1 & \frac{1}{3} & -\frac{2}{3} & 0 \end{mymatrix}
\]
gives general solution $x=\frac{1}{3}s+\frac{2}{3}t$, $y=s$, $z=t$
for any $s,t\in\R$.
Then
\[
W=\sspan \set{
\begin{mymatrix}{r} -\frac{1}{3} \\ 1 \\ 0 \end{mymatrix},
\begin{mymatrix}{r} \frac{2}{3} \\ 0 \\ 1 \end{mymatrix}}
\]
Let
$X=\set{
\begin{mymatrix}{r} -1 \\ 3 \\ 0 \end{mymatrix},
\begin{mymatrix}{r} 2 \\ 0 \\ 3 \end{mymatrix}}$.
Then $X$ is linearly independent and $\sspan(X)=W$, so $X$ is a basis of $W$.

\item Use the Gram-Schmidt Process to get an
orthogonal basis of $W$:

\[ \vect{f}_1=\begin{mymatrix}{r} -1 \\ 3 \\ 0 \end{mymatrix}
\mbox{ and }
\vect{f}_2 =
\begin{mymatrix}{r} 2 \\ 0 \\ 3 \end{mymatrix}
-\frac{-2}{10}\begin{mymatrix}{r} -1 \\ 3 \\ 0 \end{mymatrix}
=\frac{1}{5}\begin{mymatrix}{r} 9 \\ 3 \\ 15 \end{mymatrix}.\]
Therefore
$B=\set{\begin{mymatrix}{r} -1 \\ 3 \\ 0 \end{mymatrix},
\begin{mymatrix}{r} 3 \\ 1 \\ 5 \end{mymatrix} }$ is
an orthogonal basis of $W$.
\item To find
the point $Z$ on $W$ closest to $Y=(1,1,1)$, compute
\begin{eqnarray*}
\proj_{W}\begin{mymatrix}{r} 1 \\ 1 \\ 1 \end{mymatrix}
& = &
\frac{2}{10} \begin{mymatrix}{r} -1 \\ 3 \\ 0 \end{mymatrix}
+ \frac{9}{35}\begin{mymatrix}{r} 3 \\ 1 \\ 5 \end{mymatrix}\\
& = &
\frac{1}{7}\begin{mymatrix}{r} 4 \\ 6 \\ 9 \end{mymatrix}.
\end{eqnarray*}
Therefore, $Z=\paren{\frac{4}{7}, \frac{6}{7}, \frac{9}{7}}$.
\end{enumerate}
\end{solution}


% !TEX root = ../main.tex

\section{Graph metrics}

What is essential on the graph?

Types of properties:
\begin{enumerate}
  \item Local: property of the surrounding node
  \item Global: characteristic of the entire graph
\end{enumerate}

\subsection{Graph metrics}
\subsubsection{Degree}

Degree $d_j$ of node $j$: number of neighbours of $j$

\begin{figure}
  \centering
  \begin{tikzpicture}
    \node[state] (1) {1};
    \node[state, right = 2 of 1] (3) {3};
    \node[state, below right = of 1] (2) {2};
    \node[state, below right = of 3] (4) {4};
    \node[state, below = 2 of 1] (6) {6};
    \node[state, below = 2 of 3] (5) {5};

    \draw[-]    (1)   edge    (3)
                      edge[bend right]    (2)
                      edge    (6)
                (2)   edge[bend left]     (6)
                      edge[bend left]     (5)
                      edge    (3)
                (5)   edge    (4)
                      edge[bend left]     (6)
                (4)   edge    (3);

  \end{tikzpicture}
\end{figure}

$$
\sum_{j=1}^N d_j = 2L
$$

Average degree in $\mathcal{G}$ equals:
$$
E[D] = \frac{1}{N} \sum_{j=1}^N d_j = \frac{2L}{N}
$$

Bounds (connected graph):
$$
2 - \frac{2}{N} \le E[D] \le N-1
$$

$$
L(K_N) =
\begin{pmatrix}
  N \\ 2
\end{pmatrix} = \frac{N (N-1)}{2}
$$

Degree vector (row sum A): $d = Au$
\begin{itemize}
  \item Coordinate representation of the vector: $u = (1, 1, ..., 1)$
  \item Matrix representation of the vector $u^T = [1 1 ... 1]$. A vector in $m$ dimensions is
  a $m \times 1$ matrix (elements in a column)
\end{itemize}

\emph{Basic law of the degree}:
$$
u^T d = 2L \xRightarrow{thus} u^Td = u^T Au = 2L
$$

\begin{itemize}
  \item At least two nodes in G have the same degree
  \begin{itemize}
    \item A degree $N-1$ node excludes the existence of a zero degree node
  \end{itemize}
  \item The number of nodes with \textbf{odd} degree is \textbf{even}
\end{itemize}

You can also create a histogram plotting all the degrees. This allows you to have the average
and the variance of the degrees (slide 6).

Example: degree of an airport
The probability is almost linear in a log scale. It has been found that many real life problems
have an almost linear degree distribution.
$$
Pr[D_{\text{Air}} = k] \sim k^{-1.21}
$$

Internet:
$$
Pr[D_{\text{Internet}} = k] \sim k^{-\tau}, \quad \tau \in (2.2, 2.5)
$$

Many networks have a power law degree distribution (scale-free networks)

\subsubsection{Clustering coefficient}
The degree can be seen as a global metric. This one goes a bit step further.

The clustering coefficient of node $v$ is
$$
c_G(v) = \frac{2y}{d_v(d_v - 1)} = \frac{y}{y_{max}}
$$

where $y$ is the number of links between neighbors and
$
y_{max} = \begin{pmatrix}
  d_v \\ 2
\end{pmatrix}
$

This number tells you how connected your environment is. The clustering coefficient of a graph G:
$$
c_G = \frac{1}{N} \sum_{v = 1}^N c_G(v)
$$

\subsubsection{Adjacency matrix A and walks}
\begin{itemize}
  \item Walk of length $k$ from node $i$ to $j$: succession of $k$ links (arcs)
  $n_0 \rightarrow n_1)(n_1 \rightarrow n_2) ... (n_{k-1} \rightarrow n_k)$ where $n_0 = i$ and
  $n_k = j$.
  \item Path: a walk in which all nodes/vertices are different
  \item Number of $k$-hop walks between node $i$ and $j$: $(A^k)_{ij}$
  \item Total number of $k$-hop walks in G: $N_k = u^T A^k u = \sum_{i=1}^N \sum_{j=1}^N (A^k)_{ij}$
  \item Total number of closed $k$-hop walks in G:
  $$
  W_k = \sum_{j=1}^N (A^k)_{jj} = \text{trace}(A^k)
  $$
\end{itemize}

\subsubsection{Weight of a path P}
Consider a weighted graph, where each link $l$ has a link weight $w_l$.

The weight of a path P is defined as:
$$
w(P) = \sum_{l \in P} w_l
$$

where $l = n_i \rightarrow n_j$ is a link in $G$ from node $n_i$ to $n_j$ and part of the path
$P = (n_0 \rightarrow n_1)(n_01 \rightarrow n_2)...(n_i \rightarrow n_j)...(n_{h-1} \rightarrow n_h)$
The path $P = P_{n_0 \rightarrow n_h}$ from node $n_0$ to $n_h$ consists of $h$ \emph{different,
consecutive} links

\subsubsection{Hopcount}
\begin{itemize}
  \item Hopcount of a path $P$ is $h(P) = \sum_{l \in P} 1$, this link weight $w_l = 1$.
  \item Hopcount from node $i$ to node $j$:
  $H_{i \rightarrow j} = \min_{P_{i \rightarrow j}} h(P_{i \Rightarrow j})
  = h(P_{i \rightarrow j}^{*})$ where $P_{i \rightarrow j}^*$ is the shortest hop path from
  $i$ to $j$
  \item Number of $k$-hop walks between node $i$ and $j$: $(A^k)_{ij}$
\end{itemize}

Algebraic algorithm to find the shortest path
$$
\begin{pmatrix}
  (A)_{ij} = 0 \\
  (A^2)_{ij} = 0 \\
  \vdots \\
  (A^{k -1})_{ij} = 0 \\
  (A^k)_{ij} = m > 0
\end{pmatrix}
$$

The sequence tells us that there are no walks between $i$ and $j$ with less than $k$ hops.

Hence: the shortest walk between $i$ and $j$ is also a shortest path, with hopcount $H_{ij} = k$

There are $m$ different shortest paths with $k$ hops

\subsubsection{Hopcount \& Distance matrix}
A distance matrix $H$ satisfies vector norm or distance relations:
\begin{enumerate}
  \item $h_{ij} \ge 0$
  \item zero diagonal elements: $h_{ij} = 0$
  \item triangle inequality: $h_{ik} + h_{kj} \ge h_{ij}$
\end{enumerate}

\textbf{Diameter} $\rho$:
\begin{itemize}
  \item Sequence with most zeros (or maximum $k$)
  \item All elements in $\sum_{k =0}^{\rho} f_k A^k$ with all $f_k > 0$ are positive
\end{itemize}

Choose $f_k = \begin{pmatrix}
  \rho \\ k
\end{pmatrix} > 0$
then $\sum_{k = 0}^{\rho} \begin{pmatrix}
  \rho \\ k
\end{pmatrix} A^k = (I + A)^{\rho}$.

The \textbf{small world problem}:
\begin{itemize}
  \item 160 letters: 1 target (name, address, occupation, personal info)
  \item 160 random starters
  \item Each starter forwards a letter to a single acquaintance
  \item Outcome experiment: Average hops of path = 6: everybody is connected by (on average) 6 hops.
\end{itemize}

\subsubsection{Betweenness}

The betweenness $B_l(B_n)$ of a link $l$ (node $n$) equals the number of shortest paths traversink
link $l$ (node $n$) in $G$).

The average betweenness is related to the average hopcount:
$$
H_G = \sum_{i = 1}^N \sum_{j = i + 1}^N H_{ij} = \sum_{l = 1}^L B_l
\implies
E[B_l] = \frac{1}{L} \sum_{l = 1}^L B_l = \frac{1}{L}
\begin{pmatrix}
  N \\ 2
\end{pmatrix}
E[H_G] \ge E[H_G]
$$

If there are many shortest paths we need to adjust the definition. If there are $k$ shortest
paths you count each one of them as $\frac{1}{k}$

\subsubsection{Linear correlation coefficient}
Consider $n$ realizations $\{(x_i, y_i)\}_{1 \le i\le m}$ of two random variables $X$ and $Y$.

Goodness of the fit can be expressed by the linear correlation coefficient
$$
\rho(X, Y) = \frac{E[XY] - E[X]E[Y]}{\sqrt{Var[X]}\sqrt{Var[Y]}}
$$

\begin{itemize}
  \item $\rho(X,Y) = 0$: No linear correlation
  \item $\rho(X, Y) = 1$: Perfect fit
\end{itemize}

\subsubsection{Degree assortativity}

Correlation between the left part of the link and the right part. How are the degrees $D_i$
and $D_j$ at both sides of a link $l$ correlated?

\begin{itemize}
  \item A network is assortative if $\rho_D > 0$
  \item A network is disassortative if $\rho_D < 0$
\end{itemize}

$$
\rho_D = \frac{N_1 N_3 - N_2^2}{N_1 \sum_{j=1}^N d_j^3 - N_2^2}
$$

Where $N_k = u^T A^k u$ is the total number of walks with $k$ hops

\subsubsection{Degree preserving rewiring}
Between two pairs of nodes you keep the degree, however you rewire the network, and, by doing
that you change the shape of the network and the connectivity. How many rewiring do you have to
make in order to change the network from assortative to disassortative?

\subsubsection{Connectivity of the complement $G^C$}
If the graph G is disconnected, then ist complement $G^C$ is connected


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 1}

Let $\mathcal{X}$, the set of examples, be the set of natural numbers.
The hypotheses space $H$ consists of all intervals of the form $[a,b]$ with $a \leq b$.
The concept that must be learned is an interval $[c,d]$, where all examples reside.

\begin{enumerate}[label=(\alph*)]
\item Let $h_1$, $h_2$ be two hypotheses.
What does it mean in this context that $h_1$ is more specific than $h_2$?
\item Design an algorithm that learns the target concept.
\end{enumerate}

\subsection*{Solution}

\begin{enumerate}[label=(\alph*)]
\item Since hypotheses space $H$ is the set of intervals of the form $[a, b]$ with $a \leq b$, let us present hypothesis $h_1$ and $h_2$ as intervals $[x_1, y_1]$ and $[x_2, y_2]$ respectively, where $x_1 \leq y_1$ and $x_2 \leq y_2$.

By definition, hypothesis $h_1$ is more specific than $h_2$ if for every concept (natural number) $i \in h_1$, $i \in h_2$ as well.
For this to happen, for any integer $i$ in the interval $[x_1, y_1]$ we should have $i \in [x_2, y_2]$.
Alternatively, $h_1$ is more specific than $h_2$ when $[x_1, y_1] \subseteq [x_2, y_2]$.

\item The objective is to propose an algorithm to find the consistent hypothesis (target concept) using both positive and negative examples.
To achieve this objective, we initialize most general hypothesis as the interval $[-\infty, \infty]$ and least general hypothesis as the empty set $\emptyset$.
For any concept $c$, consistent hypothesis can be updated using \textsc{Update-Concept} algorithm whose pseudo-code is given in Algorithm 1 and in which \textit{SB} and \textit{GB} are respectively maximally specific and maximally general positive hypothesis boundaries.

\begin{algorithm}[H]
\caption{\textsc{LearnExample($c$)}}\label{euclid}
\begin{algorithmic}[1]
\If {concept is positive}
\State add concept to SB
\For {$i \leftarrow 1$ to number of GB intervals}
\If {concept $\notin$ interval $i$}
\State remove interval $i$ from GB
\EndIf
\EndFor
\Else
\For {$i \leftarrow 1$ to number of GB intervals}
\If {concept $\in$ interval $i$}
\State remove concept from interval $i$
\Comment divides interval $i$ into two intervals
\EndIf
\EndFor
\EndIf
\end{algorithmic}
\end{algorithm}

\end{enumerate}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{ {./} }
\usepackage{amsmath}

\title{ACSL Final Test}
\author{Alexander Sun}
\date{April 2019}

\begin{document}
\maketitle

\newpage

\section{Computer Number Systems}
\begin{enumerate}
    \item{Problem 1 (3) Let $N$ be the number of positive integers that are less than or equal to $2003$ and whose base-$2$ representation has more $1$'s than $0$'s. Find the remainder when $N$ is divided by $1000$}

    \item{Problem 2 (2) Convert to base 4, 8, 16 and 32}

    010111010100010101110101010001010111010100010001011110010010010101010101010010100

    101001110100010
\end{enumerate}
\section{Recursive Functions}
\begin{enumerate}

    \item{Problem 1 (3) Find A(4,3):}

    Ackerman's function is defined as:

\[ A(x,y)  \left\{
\begin{array}{ll}
      y + 1 & if x = 0 \\
      A(x - 1, 1) & if x \neq 0 and y = 0 \\
      A(x - 1,A(x, y - 1)) & if x \neq 0 and  y \neq 0 \\
\end{array}
\right. \]

    \item{Problem 2 (1) Find hanoi(26)}
    \[ hanoi(x)  \left\{
\begin{array}{ll}
      1 & if x = 1 \\
      2 * hanoi(n-1) + 1 & if x > 1 \\

\end{array}
\right. \]
\end{enumerate}
\section{Program Simulation}
\begin{enumerate}
    \item {Problem 1 (1) Find C[4]}
    \begin{description}
    \item A(0) = 12: A(1) = 41: A(2) = 52
\item A(3) = 57: A(4) = 77: A(5) = -100
\item B(0) = 17: B(1) = 34: B(20 = 81
\item J = 0: K = 0: N = 0
\item while A(J) $\textgreater$ 0
\item while B(K) $\leq$ A(J)
\item C(N) = B(K)
\item N = N + 1
\item k = k + 1
\item end while
\item C(N) = A(J): N = N + 1: J = J + 1
\item end while
C(N) = B(K)
    \end{description}
    \item{Problem 2 (1) Find the final value of NUM}
    \begin{description}
    \item A = “BANANAS”
    \item NUM = 0: T = “”
    \item for J = len(A) - 1 to 0 step –1
    \item T = T + A[j]
    \item next
    \item for J = 0 to len(A) - 1
    \item if A[J] == T[J] then NUM = NUM + 1
 \item next
    \end{description}
\end{enumerate}

\section{Prefix/Postfix/Infix (Satan  Math)}
\begin{enumerate}
    \item{Problem 1 (1) Evaluate}

    Define  @ a b c $= Max(a,b) / Min(b,c)$

    Define $!\:a = a!$
    $$@\:(@\:4\:19\:3)\:(\wedge\:+\:*\:3\:4\:/\:8\:2\:–\:7\:5)\:(!\:@\:7\:9\:12)$$
    \bigskip

     \item{Problem 2 (1) Convert to Prefix and Infix}
    $$\frac{a * b^2}{c+1} - \frac{a + b}{a^2 * b}$$
    \bigskip

\end{enumerate}

\section{Bit String Flicking}
\begin{enumerate}
    \item{Problem 1 (3) What combination of operations does X represent?}

    1111110111000 XNOR (111010111111 NOR (0001010001001 XOR NOT LCIRC-4 ((RSHIFT-3 1111111111111) OR 00000000000000)))
    = X(1110111011111)
\end{enumerate}
\section{LISP}
\begin{enumerate}
    \item{Problem 1 (1) Solve:}
    Given the function definitions for HY and FY as follows:

    (DEF HY(PARMS)(REVERSE(CDR PARMS)))

    (DEF FY(PARMS)(CAR(HY(CDR PARMS))))

    What is the value of the following?

    (FY'(DO RE(MI FA)SO))
    \item{Problem 2 (1)}
    Evaluate the following expression:

    (EXP ( MULT 2(SUB 5(DIV(ADD 5 3 4) 2 )) 3) 3)
\end{enumerate}
\section{Boolean Algebra}
\begin{enumerate}
    \item{Problem 1 (3)}
    Prove:
    $$\overline{x} + \overline{y} + xy\overline{z} = \overline{x} + \overline{y} + \overline{z}$$
    \bigskip
    \bigskip

    \item{Problem 2 (2)}
    Simplify:
    $$(p + q)(\overline{pq})$$
    \bigskip

    \item{Problem 3 (1)}Assume a 50/50 probability for the value of each variable. What is the probability that x = 0?:
    $$X = (A+B)(A+\overline{C})(A+D)(A+\overline{E})(A+F)(A+\overline{G})....$$

    \bigskip
    \bigskip
    \bigskip

\end{enumerate}
\section{Data Structures}
\begin{enumerate}
    \item{Problem 1 (2)} Put the following phrase into a Min-Heap, What are the letters that fill the bottom row and of what frequency?

    wecanonlyseeashortdistanceaheadbutwecanseeplentytherethatneedstobedonebyalanturing

    \bigskip
    \item{Problem 2 (2)} What would be its external path and internal path length?
    \bigskip
    \item{Problem 3 (2)} What search algorithms are derived from a stack and a queue, and what are they used for and why within each algorithm?
    \bigskip

\end{enumerate}

\section{FSA/Regular Expressions}
\begin{enumerate}
    \item{Problem 1 (2) Simplify the following regex expression(no plus symbol)}

    $$a*(ba*)* (d*c*)* (e* U f)*(gh)*g(ij U ik)$$

    \item{Problem 2 (1) Does the following string comply with the regex above?}

    $$aabadccddceeeffggghghij$$

\end{enumerate}
\section{Graph Theory}
\begin{enumerate}
    \item {Problem 1 (1) Multiply the following matrices}
    $$
    \begin{bmatrix}
    -1 & 2 \\
    5 & 4 \\
    -4 & -3 \\
    -1 & 0
    \end{bmatrix}
    \begin{bmatrix}
    7 & 8 & 9 \\
    4 & -3 & 2
    \end{bmatrix}
    $$
    \item {Problem 2 (1) Multiply the following matrices}
     $$
    \begin{bmatrix}
    1 & 0 & 1 & 0 & 0 & 0 \\
    0 & 1 & 1 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 1\\
    1 & 0 & 1 & 0 & 0 & 0 \\
    1 & 0 & 1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 1
    \end{bmatrix}
    \begin{bmatrix}
    1 & 0 & 1 & 0 & 0 & 0 \\
    0 & 1 & 1 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 1\\
    1 & 0 & 1 & 0 & 0 & 0 \\
    1 & 0 & 1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 1
    \end{bmatrix}
    $$
\end{enumerate}

\section{Digital Electronics}
\begin{enumerate}
    \item {Problem 1 (2)}

    \includegraphics[scale=.75]{DigitalElectronicsACSL}
\end{enumerate}
\section{Assembly Language}
\begin{enumerate}
    \item {Problem 1 (2) Write the following Assembly code as a piece wise function as find what is printed at the end(Ignore the messed up indentation)}

\begin{description}
\item X\quad\quad\enspace DC \quad\quad\quad\enspace 3
\item Y\quad\quad\enspace DC \quad\quad\quad\enspace 5
\item Check LOAD \quad\quad\enspace X
\item \quad\quad\quad BE \quad\quad\quad\enspace Yellow
\item  \quad\quad\quad LOAD\quad\quad\enspace Y
\item \quad\quad\quad BE \quad\quad\quad\enspace Red
\item \quad\quad\quad BU \quad\quad\quad\enspace Blue
\item Blue \space\space LOAD \quad\quad\enspace X
\item \quad\quad\quad ADD \quad\quad\enspace\enspace 1
\item \quad\quad\quad STORE \quad\enspace X
\item \quad\quad\quad LOAD\quad\quad\enspace Y
\item \quad\quad\quad SUB \quad\quad\enspace\enspace 1
\item \quad\quad\quad STORE \quad\enspace Y
\item Red \space\space LOAD\quad\quad\enspace X
\item \quad\quad\quad  SUB \quad\quad\enspace\enspace 1
\item \quad\quad\quad STORE \quad\enspace X
\item Yellow LOAD\quad\quad\enspace X
\item \quad\quad\quad  ADD  \quad\quad\enspace\enspace 1
\item \quad\quad\quad STORE \quad\enspace X
\item \quad\quad\quad PRINT \quad\enspace X
\end{description}
    \item{Problem 2 (1) Define LOC, OPCODE, LABEL, and ACC and place them in order of a command}
\end{enumerate}

\end{document}


\section{Elements of Numerical Integration}
\begin{align*}
    \int_a^bf(x)\D x &= \int_a^b\sum_{i=0}^nf(x_i)L_i(x)\D x + \int_a^b\prod_{i=0}^n(x-x_i)\frac{f^{(n+1)}(\xi(x))}{(n+1)!}\D x \\
    &= \int_a^ba_if(x_i)\D x + \frac{1}{(n+1)!}\int_a^b\prod_{i=0}^n(x-x_i)f^{(n+1)}(\xi(x)\D x,
\end{align*}
where $a_i=\int_a^bL_i(x)\D x$ for each $i=0,1,\ldots,n$.

\subsection{The Trapezoidal Rule}
To derive the Trapezoidal rule for approximating $\int_a^bf(x)\D x$, let $x_0=a$, $x_1=b$, $h=b-a$.
\begin{align*}
    \int_a^bf(x)\D x &= \int_a^b\left[\frac{(x-x_0)}{(x_1-x_0)f(x_1)}+\frac{(x-x_1)}{(x_0-x_1)f(x_0)}\right]\D x + \frac{1}{2}\int_a^b(x-x_0)(x-x_1)f''(\xi(x))\D x \\
    &= \int_{x_0}^{x_1}\frac{(x-x_0)f(x_1)-(x-x_1)f(x_0)}{x_1-x_0}\D x + \frac{f''(\xi)}{2}\left[\frac{x^3}{3}-\frac{(x_0+x_1)}{2}x^2+x_0x_1x\right]_{x_0}^{x_1} \\
    &= \left[\frac{(x-x_0)^2f(x_1)-(x-x_1)^2f(x_0)}{2(x_0-x_1)}\right]_{x_0}^{x_1} - \frac{h^3}{12}f''(\xi) \\
    &= \frac{h}{2}\left[f(x_0)+f(x_1)\right]-\frac{h^3}{12}f''(\xi).
\end{align*}

\subsection{Simpson's Rule}
\begin{align*}
    \int_{x_0}^{x_2}f(x)\D x &= \left[f(x_1)(x-x_1)+\frac{f'(x_1)}{2}(x-x_1)^2+\frac{f''(x_1)}{6}(x-x_1)^3+\frac{f^{(3)}(x_1)}{24}(x-x_1)^4\right]_{x_0}^{x_2} \\
    &\phantom{=} + \frac{1}{24}\int_{x_0}^{x_2}f^{(4)}(\xi(x))(x-x_1)^4\D x \\
    &= 2hf(x_1) + \frac{h^3}{3}f''(x_1) + \frac{f^{(4)}(\xi_1)}{60}h^5 \\
    &= 2hf(x_1)+\frac{h^3}{3}\left\{\frac{1}{h^2}\left[f(x_0)-2f(x_1)+f(x_2)\right]-\frac{h^2}{12}f^{(4)}(\xi_2)\right\} + \frac{f^{(4)}(\xi_1)}{60}h^5 \\
    &= \frac{h}{3}\left[f(x_0)+4f(x_1)+f(x_2)\right]-\frac{h^5}{12}\left[\frac{1}{3}f^{(4)}(\xi_2)-\frac{1}{5}f^{(4)}(\xi_1)\right] \\
    &= \frac{h}{3}\left[f(x_0)+4f(x_1)+f(x_2)\right]-\frac{h^5}{90}f^{(4)}(\xi)
\end{align*}

\subsection{Measuring Precision}
\begin{defn}[The degree of accuracy or precision]\hfill\\
The largest positive integer $n$ such that the formula is exact for $x^k$ for $k=0,1,\ldots,n$.
\end{defn}

\subsection{New-tom-Cotes Formulas}
The Trapezoidal and Simpson's rules are examples of a class of methods known as Newton-Cotes formulas. There are two types of Newton-Cotes formulas, open and closed.

\subsubsection{Closed Newton-Cotes Formulas}
The $(n+1)$-point closed Newton-Cotes uses nodes $x_i=x_0+ih$, for $i=0,1,\ldots,n$, where $x_0=a$, $x_n=b$ and $h=(b-a)/n$.
\begin{theo}
If $n$ is even and $f\in C^{n+2}[a,b]$
\[
\int_a^bf(x)\D x=\sum_{i=0}^na_if(x_i)+\frac{h^{n+3}f^{(n+2)}(\xi)}{(n+2)!}\int_0^nt^2(t-1)\ldots(t-n)\D t.
\]
If $n$ is odd and $f\in C^{n+1}[a,b]$
\[
\int_a^bf(x)\D x=\sum_{i=0}^na_if(x_i)+\frac{h^{n+2}f^{(n+1)}(\xi)}{(n+1)!}\int_0^nt(t-1)\ldots(t-n)\D t.
\]

$\xi\in(a,b)$.
\end{theo}

\begin{enumerate}[n=1]
    \item Trapezoidal rule
        \begin{align*}
        \int_{x_0}^{x_1}f(x)\D x=\frac{h}{2}\left[f(x_0)+f(x_1)\right]-\frac{h^3}{12}f''(\xi)
        \end{align*}
    \item Simpson's rule
        \begin{align*}
        \int_{x_0}^{x_2}f(x)\D x=\frac{h}{3}\left[f(x_0)+4f(x_1)+f(x_2)\right]-\frac{h^5}{90}f^{(4)}(\xi)
        \end{align*}
    \item Simpson's Three-Eighths rule
        \begin{align*}
        \int_{x_0}^{x_3}f(x)\D x=\frac{3h}{8}\left[f(x_0)+3f(x_1)+3f(x_2)+f(x_3)\right]-\frac{3h^5}{80}f^{(4)}(\xi)
        \end{align*}
    \item \phantom{Unknown Name}
        \begin{align*}
        \int_{x_0}^{x_4}f(x)\D x=\frac{2h}{45}\left[7f(x_0)+32f(x_1)+12f(x_2)+32f(x_3)+7f(x_4)\right]-\frac{8h^7}{945}f^{(6)}(\xi)
        \end{align*}
\end{enumerate}

\subsubsection{Open Newton-Cotes Formulas}
The open Newton-Cotes formulas do not include the endpoints of $[a,b]$ as nodes. They use the nodes $x_i=x_0+ih$, for $i=0,1,\ldots,n$, where $h=(b-a)/(n+2)$ and $x_0=a+h$, $x_n=b-h$.
\begin{theo}
If $n$ is even and $f\in C^{n+2}[a,b]$
\[
\int_a^bf(x)\D x=\sum_{i=0}^na_if(x_i)+\frac{h^{n+3}f^{(n+2)}(\xi)}{(n+2)!}\int_{-1}^{n+1}t^2(t-1)\ldots(t-n)\D t.
\]
If $n$ is odd and $f\in C^{n+1}[a,b]$
\[
\int_a^bf(x)\D x=\sum_{i=0}^na_if(x_i)+\frac{h^{n+2}f^{(n+1)}(\xi)}{(n+1)!}\int_{-1}^{n+1}t(t-1)\ldots(t-n)\D t.
\]

$\xi\in(a,b)$.
\end{theo}

\begin{enumerate}[n=1]\addtocounter{enumi}{-1}
    \item Midpoint rule
        \begin{align*}
        \int_{x_{-1}}^{x_1}f(x)\D x=2hf(x_0)+\frac{h^3}{3}f''(\xi)
        \end{align*}
    \item \phantom{Unknown Name}
        \begin{align*}
        \int_{x_{-1}}^{x_2}f(x)\D x=\frac{3h}{2}\left[f(x_0)+f(x_1)\right]-\frac{3h^3}{4}f''(\xi)
        \end{align*}
    \item \phantom{Unknown Name}
        \begin{align*}
        \int_{x_{-1}}^{x_3}f(x)\D x=\frac{4h}{3}\left[2f(x_0)-f(x_1)+2f(x_2)\right]+\frac{14h^5}{45}f^{(4)}(\xi)
        \end{align*}
    \item \phantom{Unknown Name}
        \begin{align*}
        \int_{x_{-1}}^{x_4}f(x)\D x=\frac{5h}{24}\left[11f(x_0)+f(x_1)+f(x_2)+11f(x_3)\right]+\frac{95h^7}{144}f^{(4)}(\xi)
        \end{align*}
\end{enumerate}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Basic Arithmetic Operations}
The simplest math operations in Jac.
\begin{description}
    \item[Code] \texttt{}
          \begin{lstlisting}[caption={Basic arithmetic operations}]
walker init {
    a = 4 + 4;
    b = 4 * -5;
    c = 4 / 4;  # Evaluates to a floating point number
    d = 4 - 6;
    e = a + b + c + d;
    std.out(a, b, c, d, e);
}
    \end{lstlisting}
    \item[Output] \texttt{ }
          \begin{lstlisting}[language=shell]
8 -20 1.0 -2 -13.0
        \end{lstlisting}
    \item[Description] \texttt{}
\end{description}

\noindent Additionally, Jac supports power and modulo operations.
\begin{description}
    \begin{lstlisting}[caption={Additional arithmetic operations}]
walker init {
    a = 4 ^ 4; b = 9 % 5; std.out(a, b);
}
    \end{lstlisting}
    \item[Output] \texttt{ }
          \begin{lstlisting}[language=shell]
256 4
        \end{lstlisting}
    \item[Description] \texttt{}
\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Comparison Operations}
\begin{description}
    \begin{lstlisting}[caption={Comparision operations}]
walker init {
    a = 5; b = 6;
    std.out(a == b,
            a != b,
            a < b,
            a > b,
            a <= b,
            a >= b,
            a == b-1);
}
    \end{lstlisting}
    \item[Output] \texttt{ }
          \begin{lstlisting}[language=shell]
false true true false true false true
        \end{lstlisting}
    \item[Description] \texttt{}
\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Logical Operations}
\begin{description}
    \begin{lstlisting}[caption={Logical operations}]
walker init {
    a = true; b = false;
    std.out(a,
            !a,
            a && b,
            a || b,
            a and b,
            a or b,
            !a or b,
            !(a and b));
}
    \end{lstlisting}
    \item[Output] \texttt{ }
          \begin{lstlisting}[language=shell]
true false false true false true false true
        \end{lstlisting}
    \item[Description] \texttt{}
\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Assignment Operations}
\begin{description}
    \begin{lstlisting}[caption={Assignment operations}]
walker init {
    a = 4 + 4; std.out(a);
    a += 4 + 4; std.out(a);
    a -= 4 * -5; std.out(a);
    a *= 4 / 4; std.out(a);
    a /= 4 - 6; std.out(a);

    # a := here; std.out(a);
    # Noting existence of copy assign, described later
}
    \end{lstlisting}
    \item[Output] \texttt{ }
          \begin{lstlisting}[language=shell]
8
16
36
36.0
-18.0
        \end{lstlisting}
    \item[Description] \texttt{}
\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Foreshadowing Unique Graph Operations}
\begin{description}
    \begin{lstlisting}[caption={Preview of graph operators},
        label={code:moremath}]
edge back;

walker init {
    node_a = spawn node;
    here --> node_a;
    here <-[back]- node_a;

    node_b = spawn here <-> node;
    node_b --> node_a
}
    \end{lstlisting}
    \item[Output] \texttt{ }
    \item[Description] \texttt{}

          \begin{tikzpicture}[node distance = {1.0cm and 1.5cm}, v/.style = {draw, circle}]
              \graph[nodes={circle, draw}, grow right=2.25cm, branch down=1.75cm]{
              H -> A,
              A -> ["back"] H,
              H -- B,
              B -> A,
              };
          \end{tikzpicture}
\end{description}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Precedence}

\begin{table}[h]
    \small
    \centering
    \begin{tabular}{l l l}
        \toprule
        \textbf{Rank} & \textbf{Symbol}          & \textbf{Description}                           \\
        \midrule
        1             & () [] . :: --> <-- spawn & Parenthetical/grouping, node/edge manipulation \\
        2             & \textasciicircum         & Exponent                                       \\
        3             & * / \%                   & Multiplication, division, modulo               \\
        4             & + -                      & Addition, subtraction                          \\
        5             & == != >= <= > <          & Comparison                                     \\
        6             & \&\& || and or           & Logical                                        \\
        7             & = += -= *= /= :=         & Assignment                                     \\
        \bottomrule
    \end{tabular}
    \caption{Precedence of operations in Jac}
    \label{tab:jacprecedence} % Unique label used for referencing the table in-text
    %\addcontentsline{toc}{table}{Table \ref{tab:jacprecedence}} % Uncomment to add the table to the table of contents
\end{table}


\subsection{Matrices}
\noindent
Matrices are an array of mathematical objects, most often numbers.
They are often used to represent linear transformations between two spaces and systems of linear equations.
We denote the size of a matrix by saying the number of rows followed by the number of columns.

\begin{example}
	Below is a 2 x 4 matrix.
	\begin{equation*}
		\begin{bmatrix}
			1 & 3 & 2 & -1 \\
			-5 & 7 & 3 & 0 \\
		\end{bmatrix}
	\end{equation*}
\end{example}

% Types of matrices
\input{../common/vectorsMatrices/typesOfMatrices.tex}
% Row Reduction
\input{../common/vectorsMatrices/rowReduction.tex}
% Determinants
\input{../common/vectorsMatrices/determinants.tex}
% Eigenvalues/vectors
\input{../common/vectorsMatrices/eigenvaluesEigenvectors.tex}


\section{Procedures}

\begin{itemize}

	\item A procedure is a named block of code used for structure and to enable reuse

	\item Given $ \Procedure ~ R() \defeq S $ and $ w : [P, Q] \refsto S $, we have that $ w : [P, Q] \refsto R $

	\item The \textit{formal} parameter is used in the function and the \textit{actual} parameter is what's passed in

	\item Parameter types

	\begin{itemize}

		\item $ \Value $

		\begin{itemize}

			\item Sets the formal parameter to the value of a variable or expression when the procedure runs

			\item Modifying the formal parameter in the procedure doesn't affect the actual parameter

			\item Given $ \Procedure ~ R(\Value ~ z) \defeq S $  and $ w, z : [P, Q] \refsto S$:\\
			$ ~~~~ w : [P[z \backslash a], Q[z_0 \backslash a_0]] \refsto R(a) $ where $ a_0 = a[w \backslash w_0] $

			\item The postcondition $ Q $ should not contain $ z $ since it is local to $ R $

		\end{itemize}

		\item $ \Result $

		\begin{itemize}

			\item The actual parameter takes the value of the formal parameter when the procedure terminates

			\item The actual parameter must be a variable, not an expression and its initial value is not defined

			\item Given $ \Procedure ~ R(\Result z) \defeq S $ and $ w, z : \refsto S $:\\
			$ ~~~~ w : [P, Q[z \backslash a]] \refsto R(a) $

			\item The precondition $ P $ should not contain $ z $, and the postcondition $ Q $ should not contain $ z_0 $

		\end{itemize}

		\item $ \ValueResult $

		\begin{itemize}

			\item The formal parameter takes the value of the actual parameter when the procedure starts

			\item The actual parameter takes the value of the formal parameter when the procedure terminates

			\item Given $ \Procedure ~ R(\ValueResult ~ z) \defeq S $ and $ w, z : [P, Q] \refsto S $:\\
			$ ~~~~ w, a : [P[z \backslash a], Q[z_0, z \backslash a_0, a]] \refsto R(a) $

			\item There are no constraints on how $ z $ and $ z_0 $ may appear in $ P $ and $ Q $

		\end{itemize}

		\item A procedure may have multiple parameters of different types

		\begin{itemize}

			\item e.g. $ \Procedure ~ R(\Result ~ x, y; \Value ~ z) \defeq x, y := 0, z + 1 $

		\end{itemize}

	\end{itemize}

	\item Introducing procedures and procedure calls when refining

	\begin{enumerate}

		\item Identify a suitable specification $ x, y, z : [P, Q] $ and choose a name $ R $

		\item Identify parameters and their types

		\begin{itemize}

			\item Variables in $ P $ only are likely $ \Value $ parameters

			\item Variables in $ Q $ only are likely $ \Result $ parameters

			\item Variables in both are likely $ \ValueResult $ parameters

		\end{itemize}

		$ \Procedure ~ R(\Value ~ x; \Result ~ y; \ValueResult ~ z) \defeq x, y, z : [P, Q] $

		\item If the formal parameter appears only in the precondition, use a $ \Value $ parameter

		\item Refine the body of $ R $ to code

		\item Refine the main program with variables $ a, b, c $ to the specification:\\
		$ ~~~~ b, c : [P[x, z \backslash a, c], Q[x_0, y, z_0, z \backslash a_0, b, c_0, c]]$

		\item Replace the above specification with $ R(a, b, c) $

	\end{enumerate}

\end{itemize}

\newpage

\subsection{Recursion}

\begin{itemize}

	\item Procedures may be called recursively (i.e. from within themselves), as per any procedure call

	\item Need to use a variant $ V $ to ensure the recursion reaches a base case

	\begin{itemize}

		\item The variant may refer to variables including parameters (aside from $ \Result $ parameters)

		\item Given procedure $ R $ with specification $ w : [P, Q] $, let $ V = N $ when the procedure is first called

		\item Then to refine to a call outside the function, $ R $ has specification $ w : [P \land (V = N), Q] $

		\item To refine to a recursive call within $ R $, we require the specification $ w : [P \land (0 \le V < N), Q] $

	\end{itemize}

\end{itemize}

$ ~ $

Example: $ \Procedure ~ Factorial(\Value ~ n, \Result ~ f) \defeq n : f [n \ge 0, f = n_0!] $.

Let $ n $ be the variant and introduce $ n = N $ into the precondition (for the initial call).\\
\form{n, f : [n \ge 0 \land n = N, f = n_0!]}
\hint{\refsto}{Selection: $ n \ge 0 \land n = N \entails n = 0 \lor n > 0 $}
\gcl{0}{\If ~ n = 0 \rightarrow n, f : [n \ge 0 \land n = 0 \land n = N, f = n_0!]}
\gcl{0}{\Choice ~ n > 0 \rightarrow n, f : [n \ge 0 \land n > 0 \land n = N, f = n_0!]}
\gcl{0}{\Fi}
\hint{\refsto}{Assignment: $ n \ge 0 \land n = 0 \land n = N \entails (f = n_0!)[f \backslash 1] $}
\gcl{0}{\If ~ n = 0 \rightarrow f := 1}
\gcl{0}{\Choice ~ n > 0 \rightarrow n, f : [n \ge 0 \land n > 0 \land n = N, f = n_0!]}
\gcl{0}{\Fi}

The remaining specification $ n, f : [n \ge 0 \land n > 0 \land n = N, f = n_0!] $ is refined as follows:
\form{n, f : [n \ge 0 \land n > 0 \land n = N, f = n_0!]}
\hint{\refsto}{Contract frame: $ n $}
\form{f : [n \ge 0 \land n > 0 \land n = N, f = n!]}
\hint{\refsto}{Following assignment: $ f := f \times n $}
\gcl{0}{f : [n \ge 0 \land n > 0 \land n = N, (f = n!)[f \backslash f \times n]]; f := f \times n}

The remaining specification $ f : [n \ge 0 \land n > 0 \land n = N, (f \times n) = n!] $ is refined into a recursive call:\\
\form{f : [n \ge 0 \land n > 0 \land n = N, (f \times n) = n!]}
\hint{\refsto}{Apply substitution}
\form{f : [n \ge 0 \land n > 0 \land n = N, (f \times n) = n!]}
\hint{\refsto}{Divide both sides of $ f \times n = n! $ by $ n $}
\form{f : [n \ge 0 \land n > 0 \land n = N, f = (n - 1)!]}
\hint{\refsto}{Weaken precondition: $ n \ge 0 \land n > 0 \land n = N \entails n - 1 \ge 0 \land (0 \le n - 1 < N) $}
\form{f : [n - 1 \ge 0 \land (0 \le n - 1 < N), f = (n - 1)!]}
\hint{\refsto}{Apply substitution backwards}
\form{f : [(n \ge 0 \land (0 \le n < N))[n \backslash n - 1], (f = n!)[n_0, f \backslash n_0 - 1, f]]}
\hint{\refsto}{Introduce recursive call with $ \Value $ parameter $ n $ and $ \Result $ parameter $ f $}
\form{Factorial(n - 1, f)}

This produces the final program:\\
\gcl{0}{\If ~ n = 0 \rightarrow f := 1}
\gcl{0}{\Choice ~ n > 0 \rightarrow Factorial(n - 1, f); f := f \times n}
\gcl{0}{\Fi}


\section{A Real Time Spectrum Analyser Using Least Mean Square}

\begin{enumerate}[label=\alph*), leftmargin=*]

%% a)
\item
%

Let the $L_{2}$-norm of the prediction error being the objective function $\mathcal{J}$:

\begin{equation}
    \mathcal{J}(\vw) = \| \vy - \hat{\vy} \|^{2} = \| \vy - \mathbf{F} \vw \|^{2} = (\vy - \mathbf{F} \vw)^{H} (\vy - \mathbf{F} \vw)
\label{eq:J_OLS}
\end{equation}

In order to minimise the objective function $\mathcal{J}$ with respect to the parameters (weights) $\vw$, the first order condition is:

\begin{equation}
    \frac{\partial \mathcal{J}(\vw)}{\partial \vw} \bigg\vert_{\vw=\vw_{*}}  = 0
\end{equation}

Substituting from (\ref{eq:J_OLS}):

\begin{align}
    \frac{\partial}{\partial \vw} \bigg( \vy^{H} \vy - \vy^{H} \mathbf{F} \vw - \vw^{H} \mathbf{F}^{H} \vy + \vw^{H} \mathbf{F}^{H} \mathbf{F} \vw \bigg) \bigg\vert_{\vw=\vw_{*}}  &= 0 \\
    0 - \mathbf{F}^{H} \vy - \mathbf{F}^{H} \vy + 2 \mathbf{F}^{H} \mathbf{F} \vw_{*} &= 0
\end{align}

Assuming that $\mathbf{F}^{H} \mathbf{F}$ is invertible (\textbf{semi}-positive as covariance matrix), we solve for $\vw_{*}$, concluding the proof:

\begin{equation}
    \vw_{*} = \bigg( \mathbf{F}^{H} \mathbf{F} \bigg)^{-1} \mathbf{F}^{H} \vy
\label{proof:OLS}
\end{equation}

The Inverse Discrete Fourier Transform (IDFT) of a signal $x(n)$ is given by:

\begin{align}
    \hat{x}(n)  &= \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} X(k) e^{j \frac{2\pi}{N} n k}
                 = \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} X(k) F_{N}^{nk}
\end{align}

where $F_{N} = e^{j \frac{2\pi}{N}}$. If we define:

\begin{equation}
    \renewcommand\arraystretch{1.5}
    \vf_{n}^{H} = \frac{1}{\sqrt{N}}
    \begin{bmatrix}
        1, & F_{N}^{n}, & F_{N}^{2n}, & \ldots, & F_{N}^{n(N-1)}
    \end{bmatrix}
\end{equation}

Arranging the sample estimates in a vector, we obtain:

\begin{equation}
    \renewcommand\arraystretch{1.5}
    \hat{\vx} =
    \begin{bmatrix}
        \hat{x}(0) \\ \hat{x}(1) \\ \vdots \\ \hat{x}(N-1)
    \end{bmatrix}
    =
    \begin{bmatrix}
        \vf_{0}^{H} \mathbf{X} \\ \vf_{1}^{H} \mathbf{X} \\ \vdots \\ \vf_{N-1}^{H} \mathbf{X}
    \end{bmatrix}
    =
    \mathbf{F} \mathbf{X}
\label{eq:DFT_OLS}
\end{equation}

where:

\begin{equation}
    \renewcommand\arraystretch{1.5}
    \mathbf{F} =
    \begin{bmatrix}
        \vf_{0}^{H} \\ \vf_{1}^{H} \\ \vdots \\ \vf_{N-1}^{H}
    \end{bmatrix}
    = \frac{1}{\sqrt{N}}
    \begin{bmatrix}
        1 & 1 & 1 & \cdots & 1 \\
        1 & F_{N} & F_{N}^{2} & \cdots & F_{N}^{N-1} \\
        \vdots & \vdots & \vdots & \ddots & \vdots \\
        1 & F_{N}^{N-1} & F_{N}^{2(N-1)} & \cdots & F_{N}^{(N-1)^{2}} \\
    \end{bmatrix}
\end{equation}

Hence from (\ref{eq:DFT_OLS}), we conclude that IDFT is a linear transformation, where $\mathbf{F}$ the transformation matrix comprises of N harmonically related sinusoids,
with mutually orthonormal columns\footnote{Note that the $\frac{1}{\sqrt{N}}$ term is introduce to deal with unit-length columns.}. Then the Fourier coefficients, $\mathbf{X}$, are given by (\ref{proof:OLS}):

\begin{equation}
    \mathbf{X} = \bigg( \mathbf{F}^{H} \mathbf{F} \bigg)^{-1} \mathbf{F}^{H} \hat{\vx} = \mathbf{F}^{H} \hat{\vx}
\label{proof:DFT_as_OLS}
\end{equation}

where the fact that $\mathbf{F}$ is unitary is used\footnote{See Problem \& Answer Sets for the proof.}.
Therefore, since (\ref{proof:OLS}) is the optimal least mean squares solution, minimising the squared error and the Fourier coefficients, $\vw$, follow (\ref{proof:OLS}),
then Inverse Discrete Fourier Transform, $\hat{x}(n)$, is a linear approximation of the original signal $x(n)$, minimising the squared error:

\begin{equation}
    \underset{\mathbf{X}}{min} \| \vx - \hat{\vx} \|^{2}
\end{equation}

% The Discrete Fourier Transform (DFT) of a signal $x(n)$ is given by:

% \begin{align}
%     X(k)    &= \frac{1}{\sqrt{N}} \sum_{n=0}^{N-1} x(n) e^{-j \frac{2\pi}{N} n k}
%              = \frac{1}{\sqrt{N}} \sum_{n=0}^{N-1} x(n) W_{N}^{nk}
% \end{align}

% where $W_{N} = e^{-j \frac{2\pi}{N}}$. If we define:

% \begin{equation}
%     \renewcommand\arraystretch{1.5}
%     \vw_{k}^{H} = \frac{1}{\sqrt{N}}
%     \begin{bmatrix}
%         1, & W_{N}^{k}, & W_{N}^{2k}, & \ldots, & W_{N}^{k(N-1)}
%     \end{bmatrix}
% \end{equation}

% Arranging the DFT coefficients in a vector, we obtain:

% \begin{equation}
%     \renewcommand\arraystretch{1.5}
%     \mathbf{X} =
%     \begin{bmatrix}
%         X(0) \\ X(1) \\ \vdots \\ X(N-1)
%     \end{bmatrix}
%     =
%     \begin{bmatrix}
%         \vw_{0}^{H} \vx \\ \vw_{1}^{H} \vx \\ \vdots \\ \vw_{N-1}^{H} \vx
%     \end{bmatrix}
%     =
%     \mathbf{W} \vx
% \end{equation}

% where:

% \begin{equation}
%     \renewcommand\arraystretch{1.5}
%     \mathbf{W} =
%     \begin{bmatrix}
%         \vw_{0}^{H} \\ \vw_{1}^{H} \\ \vdots \\ \vw_{N-1}^{H}
%     \end{bmatrix}
%     = \frac{1}{\sqrt{N}}
%     \begin{bmatrix}
%         1 & 1 & 1 & \cdots & 1 \\
%         1 & W_{N} & W_{N}^{2} & \cdots & W_{N}^{N-1} \\
%         \vdots & \vdots & \vdots & \ddots & \vdots \\
%         1 & W_{N}^{N-1} & W_{N}^{2(N-1)} & \cdots & W_{N}^{(N-1)^{2}} \\
%     \end{bmatrix}
% \end{equation}

% The Fourier estimation of a signal $x(n)$, or equivalently its Inverse Discrete Fourier Transform (IDFT), satisfy:

% \begin{align}
%     \hat{x}(n)  &= \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} X(k) e^{j 2\pi k n / N} \\
%                 &= \frac{1}{\sqrt{N}} \bigg[ X(0) + X(1) e^{j 2\pi n / N} + X(2) e^{(j 2\pi n / N) 2} + \ldots + X(N-1) e^{(j 2\pi n / N) (N-1)} \bigg]
% \end{align}

% Rewriting the equation above in matrix form:

% \begin{align}
%     \renewcommand\arraystretch{1.5}
%     \begin{bmatrix}
%         \hat{x}(0) \\ \hat{x}(1) \\ \vdots \\ \hat{x}(N-1)
%     \end{bmatrix}
%     = \frac{1}{\sqrt{N}}
%     \begin{bmatrix}
%         1 & 1 & \cdots & 1 \\
%         1 & e^{j \frac{2\pi}{N}(1)(1)} & \cdots & e^{j \frac{2\pi}{N}(1)(N-1)} \\
%         \vdots & \vdots & \ddots & \vdots \\
%         1 & e^{j \frac{2\pi}{N}(1)(N-1)} & \cdots & e^{j \frac{2\pi}{N}(N-1)(N-1)}
%     \end{bmatrix}
%     \begin{bmatrix}
%         X(0) \\ X(1) \\ \vdots \\ X(N-1)
%     \end{bmatrix}
% \end{align}

% Letting $a = e^{j\frac{2\pi}{N}}$, we notice a pattern, such that:

% \begin{align}
%     \renewcommand\arraystretch{1.5}
%     \underbrace{
%         \begin{bmatrix}
%             \hat{x}(0) \\ \hat{x}(1) \\ \vdots \\ \hat{x}(N-1)
%         \end{bmatrix}
%     }_{\hat{\vx}}
%     = \frac{1}{\sqrt{N}}
%     \underbrace{
%         \begin{bmatrix}
%             1 & 1 & \cdots & 1 \\
%             1 & a & \cdots & a^{(N-1)} \\
%             \vdots & \vdots & \ddots & \vdots \\
%             1 & a^{(N-1)} & \cdots & a^{(N-1)(N-1)}
%         \end{bmatrix}
%     }_{\mathbf{F}}
%     \underbrace{
%         \begin{bmatrix}
%             X(0) \\ X(1) \\ \vdots \\ X(N-1)
%         \end{bmatrix}
%     }_{\vw}
% \end{align}

% We conclude that the IDFT is a linear transformation, where $\mathbf{F}$ the transformation matrix comprises of N harmonically related sinusoids, with mutually orthonormal columns\footnote{Note that the
% $\frac{1}{\sqrt{N}}$ term is introduce to deal with unit-length columns.}. Then the Fourier coefficients, $\vw$, are given by (\ref{proof:OLS}):

% \begin{equation}
%     \vw = \bigg( \mathbf{F}^{H} \mathbf{F} \bigg)^{-1} \mathbf{F}^{H} \vx = \mathbf{F}^{H} \vx
% \end{equation}

% where the fact that $\mathbf{F}$ is unitary is used\footnote{See Problem \& Answer Sets for the proof.}.

% Hence, since (\ref{proof:OLS}) is the optimal least mean squares solution, minimising the squared error and the Fourier coefficients, $\vw$, follow (\ref{proof:OLS}),
% then Inverse Discrete Fourier Transform, $\hat{x}(n)$, is a linear approximation of the original signal $x(n)$, minimising the squared error:

% \begin{equation}
%     \underset{\vw}{min} \| \vx - \hat{\vx} \|^{2}
% \end{equation}

%% b)
\item
%

According to equation (\ref{proof:DFT_as_OLS}), the Fourier coefficients, $\mathbf{X}$, are a linear combination of the columns of the transformation matrix $\mathbf{F}$, whose columns are orthonormal:

\begin{equation}
    \vf_{k}^{H} \vf_{l} = \frac{1}{N} \sum_{k=0}^{N-1} e^{j \frac{2\pi}{N} (k-l)} = \left\{
    \begin{array}{ll}
      1 & \text{if } k=l\\
      0 & \text{otherwise}
\end{array} \right.
\end{equation}

Therefore DFT operation can be seen as a projection of a vector, $\vx$, in time domain to the $\mathbf{F}$ matrix subspace, spanned by their orthonormal columns,
representing $N$ harmonically related sinusoids, wnere each sinusoid is a multiple of the frequency $\frac{f_{s}}{N}$, with $f_{s}$ the sampling frequency.

%% c)
\item
%

The $K$-points DFT-CLMS method is applied to the frequency modulated (FM) non-stationary signal, $f(n)$. Figure \ref{fig:4_3_c_1} illustrates the time-frequency diagram obtained.
The algorithm adapts perfectly to the behaviour of the frequency signal, $f(n)$, for $n < 500$, where the frequency is constant over time, while for larger values we notice
that the trends (both linear and quadratic) are generally captured. Surprisingly though, once a strong frequency component is picked up by the DFT-CLMS filter, this component is not updated,
hence the diagram has Fourier coefficients superimposed over time. CLMS is a gradient algorithm, which updates weights (DFT coefficients)
towards the mean squared error descent direction. The coefficients are not block-based calculated, but in an adaptive manner, hence error back-propagation is slow, causing this
lasting effect. Moreover, the $K$-points DFT-CLMS filter has $K$ dimensional weights, where $K=2048$, thus the curse of dimensionality prevents error gradients to propagate back and update the weights.

\begin{figure}[h]
    \centering
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/c/dft_clms-gamma_0.00}.pdf}
    \end{subfigure}
    \caption{FM: DFT-CLMS time-frequency plot with $\gamma = 0$ (unbiased).}
    \label{fig:4_3_c_1}
\end{figure}

Regularisation, such as adding a leakage coefficient $\gamma$, similar to the Leaky LMS variant, enables accurate time-frequency modelling, as shown in figure \ref{fig:4_3_c_2}.
This is achieved thanks to the forget mechanism that the Leaky CLMS algorithm provides:

\begin{equation}
    \vw(n+1) = (1 -\gamma \mu) \vw(n) + \mu e^{*}(n) \vx(n)
\end{equation}

where the greater $\gamma$ values allow to ignore, forget previous timestep's weights $\vw(n)$.

Note that for very small values of the leakage coefficient (i.e $\gamma = 0.01$) the Fourier coefficient superimposing effect is still visible.
However, larger values of $\gamma$ (i.e $\gamma = 0.05, 0.1$) introduce some bias, but obtain correct modelling. Finally, bias for larger values of $\gamma$ (i.e $\gamma \geq 0.5$) lead to inaccurate results.

\begin{figure}[h]
    \centering
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/c/dft_clms-gamma_0.01}.pdf}
    \end{subfigure}
    ~
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/c/dft_clms-gamma_0.05}.pdf}
    \end{subfigure}
    ~
    ~
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/c/dft_clms-gamma_0.10}.pdf}
    \end{subfigure}
    ~
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/c/dft_clms-gamma_0.50}.pdf}
    \end{subfigure}
    \caption{FM: DFT-CLMS time-frequency plot with $\gamma > 0$ (biased).}
    \label{fig:4_3_c_2}
\end{figure}

%% d)
\item
%

The $K$-points DFT-CLMS algorithm is applied to the EEG \texttt{POz} data, of length $N=1200$.
The time-frequency diagrams for different $\gamma$ values are obtained and illustrated in figure \ref{fig:4_3_d}.
The Leaky CLMS does not perform any better than the standard CLMS algorithm, due to the stationary nature of the signal
under study.

\begin{figure}[h]
    \centering
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/d/dft_clms-gamma_0.000}.pdf}
    \end{subfigure}
    ~
    \begin{subfigure}{0.49\textwidth}
        \centering
        \includegraphics[height=1.5in]{{report/widely-linear-filtering-and-adaptive-spectrum-estimation/a-real-time-spectrum-analyser-using-least-mean-square/assets/d/dft_clms-gamma_0.001}.pdf}
    \end{subfigure}
    \caption{EEG \texttt{POz}: DFT-CLMS time-frequency plots.}
    \label{fig:4_3_d}
\end{figure}

The strong $50 Hz$ component is visible in both implementations and so are the first two harmonics of SSEVP, at
frequencies $f_{1} = 13 Hz$ and $f_{2} = 26 Hz$, respectively. The third harmonic is not distinguishable though, with any
of the two algorithms.

%
\end{enumerate}


\section{Prologue}

\subsection{Exercises}

\subsubsection{Exercise 1}
The first graph has exactly 2 vertices of odd degree, so it has a Eulerian path. The second graph has 4
vertices of odd degree, so it does not.

\subsection{Problems}

\subsubsection{Problem 1}
Each edge in a finite graph increases the total sum of the graph's vertex degrees by 2. Thus, this sum must
be an even number, so we cannot have an odd number of vertices with odd degree (otherwise the sum would be odd).

\subsubsection{Problem 2}
Consider a finite simple graph with $n$ vertices. Each vertex can have a degree between 1 and $n - 1$, since
we cannot have multiple edges or self-loops. Thus, by the pigeonhole principle, at least two of the $n$
vertices must have the same degree (there is no bijection between the $n$ vertices and the $n - 1$ degree
options).

\subsubsection{Problem 3}
If every vertex has even degree, we can construct a set of covering cycles as follows. Choose an arbitrary
vertex with non-zero degree and traverse a cycle starting at that vertex, while deleting all edges traversed along the way.
Repeat this procedure until all edges have been deleted. We are guaranteed to be able to find the
aforementioned cycles since the graph is connected; an edge "leaving" a vertex can be paired with an edge
"coming in" to the vertex.

Once we have a set of covering cycles, we can combine them into a single cycle as follows. Consider a cycle
starting at vertex $a$ and another, edge-disjoint cycle starting at vertex $b$ such that $b$ also occurs
in the cycle starting at $a$. We can then combine the two cycles by starting at vertex $a$ and traversing
its cycle until arriving at vertex $b$, at which point we traverse the cycle starting at vertex $b$. Finally,
we finish the rest of vertex $a$'s cycle from $b$ onwards. Since cycles $a$ and $b$ were edge-disjoint, this
combined cycle does not visit any edge twice. We can repeat this cycle combination procedure until only a
single cycle is left, which is the Eulerian cycle.


%!TEX root = TTK4150-Summary.tex
\section{Stability of perturbed systems}
We consider perturbed systems on the form
\begin{equation}
	\dot{x} = f(t,x) + g(t,x)
\end{equation}
with \emph{nominal} systems
\begin{equation}
	\dot{x} = f(t,x)
	.
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Vanishing perturbation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{Lemma 9.1}
\begin{itemize}
	\item The origin is an ES equilibrium of the nominal system.
	\item $V(t,x)$ is an LF of the nominal system, and satisfies
		\begin{equation}
			c_1 \norm{x}^2 \leq V(t,x) \leq c_2 \norm{x}^2
		\end{equation}
		and
		\begin{equation}
			\norm{\pd{V}{x}} \leq c_4 \norm{x}
			.
		\end{equation}
	\item The perturbation $g(t,x)$ satisfies
		\begin{equation}
			\norm{g(t,x)} \leq \gamma \norm{x}, \quad \gamma < \frac{c_3}{c_4}
			.
		\end{equation}
\end{itemize}
Then $x^* = 0$ of the perturbed system is ES. If the assumptions hold globally, $x = 0$ is GES.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Nonvanishing perturbation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

TTK4150 Nonlinear control systems/sec-stability-of-perturbed-systems.tex


\section{Neural networks\enskip
$\color{section-text-color} \phi_j(x_i) \leftrightarrow \phi(x_i,\theta)$}

Parameterize feature map: $\phi(x,\theta)$ instead of $\phi(x)$, usually: \highlight*{$\phi(x,\theta) = \varphi(\theta^\top x)$} $= \varphi(z)$\\
$\Rightarrow w^* = \arg\min\limits_{w,\theta} \sum_{i=1}^n \ell(y_i; \underbracket[.7pt][.7pt]{\textstyle \sum_{j=1}^m w_j \,\phi(x_i, \theta_j)}_{f(x;w,\theta)})$

% ===
\emph{Activation functions $\color{emph-text-color} \varphi(z)$}

\textbf{Sigmoid:} $\frac{1}{1+\exp(-z)} {\color{gray} \,\in [0,1]}$, \enskip {\small $\varphi'(z) = (1 \!-\! \varphi(z))\cdot\varphi(z)$}\par
\textbf{Tanh:} $\varphi(z) = \tanh(z) = \frac{\exp(z)-\exp(-z)}{\exp(z)+\exp(-z)} \color{gray} \,\in [-1,1]$\par
\textbf{ReLu:}  $\varphi(z) = \max(z,0) \color{gray} \;\to \text{not smooth}$

% ===
\emph{Predict: forward propagation}
\setstretch{0.9}
\begin{highlightbox}
    \begin{enumerate}
        \item $v^{(0)} = x$
        \item $v^{(l)} = \varphi(z^{(l)})$, $z^{(l)} = W^{(l)}v^{(l-1)}${,
            \small $\color{gray}\text{for }l = 1:L\!-\!1$}
        \item $f = z^{(L)} = W^{(l)} v^{(L\!-\!1)}$
        \item \textbf{Pred.}: $\hat y \!=\! f$ (Regr.) \textit{or}
        \begin{minipage}{.4\linewidth}
    		\small\vspace*{-10pt}
    		$\quad\begin{cases}
    			\hat y = \operatorname{sign}(f) \textit{ or}\\
    			\hat y = \arg\max\limits_i(f_i)
    		\end{cases}$
    	\end{minipage}
    \end{enumerate}
\end{highlightbox}

% ===
\emph{Compute gradient: \normalfont\sffamily backpropagation}
\begin{highlightbox}
	\textbf{Output}: {\footnotesize$[\cdots\highlight*{\delta_k^{(L)}}\!\cdots] =$} $\delta^{(L)} \!=\! \ell'(f)$ {\footnotesize$= [\cdots\ell_k'(f_k)\cdots]$}

	\textbf{Hidden layer}: for $\normalfont l=L{-}1 : 1$, \\
	$
	\highlight*{\delta_k^{(l)}\!} {=} \varphi'(z_k)\cdot \hspace{-12pt} \sum\limits_{j\in\textrm{layer}_{(l+1)}} \hspace{-12pt} w_{jk}\delta_j
	\enskip
	\text{\normalcolor \textbf{Grad.}:} \pderiv{\ell}{w_{j,k}} \!\!=\! \delta_j^{(l)}v_k^{(l\!-\!1)}
	$
\end{highlightbox}

$W \!=\! [w_{jk}^{(L)} \cdots w_{jk}^{(1)}]_{jk}$, \hfill $L(W) \!\equiv\! L = \sum_j\ell_j(y_j,f_j)$

% ===
\emph{Learning with momentum}
\begin{highlightbox}
	\textbf{1.} $a \leftarrow {\color{OrangeRed}m} a + \eta_t \nabla_W \ell(W;y,x)$\enskip
	\textbf{2.} $W \leftarrow W \!-\! a$
\end{highlightbox}

% ===
\emph{Convolutional NNs}

$\to$ $\overbracket[.7pt][.7pt]{\text{conv. $\to$ pooling}}^{\text{repeat $n$ times}}$
$\to$ $\overbracket[.7pt][.7pt]{\text{fully connected}}^{\text{Perceptron ($m$ times)}}$ $\to$ out

\textbf{Convolution}: for edge regions $\to$ 0-padding

\textbf{pooling} (subsampling): e.g. `max' pooling

\textbf{output dim's}:
$\alpha = \frac{n+2p-f}{s}+1$\\
{\small
where $m$: \# $f\times f$ filters,\enskip
$n$: img. dim.,\enskip
$p$: padding, \# of added zeros,\enskip
$s$: strides (amount by which filter shifts)
}


 \FloatBarrier

\section{Maximization problems}\label{sec:maximization}

The evolutionary dynamics can be used to solve convex optimization problems.
We can use the properties of population games to design games that maximize some function $f(\bs{z})$, where $\bs{z}\in\mathbb{R}^{n}$ is a vector of $n$ variables, i.e., $\bs{z} = [z_1, \ldots, z_k, \ldots, z_n]$. Below we show two alternatives to solve this optimization problem using either a single population or $n$ populations.

\subsection{Single Population Case}

First, let us consider a population where each agent can choose one of the $n+1$ strategies. In this case, the first $n$ strategies correspond one variable of the objective function and the $n+1\th$ strategy can be seen as a slack variable.
Thus, $x_k$ is the proportion of agents that use the $k\th$ strategy, and it corresponds to the $k\th$ variable, i.e., $x_k = z_k$.
We define the fitness function of the $k\th$ strategy $F_k$ as the derivative of the objective function with respect to the $k\th$ variable, thus, $F_k(\bs{x}) \equiv \frac{\partial }{\partial x_k} f(\bs{x})$.

Note that if $f(\bs{x})$ is a concave function, then its gradient is a decreasing function.
Recall that users attempt to increase their fitness by adopting the most profitable strategy in the population, say the $k\th$ strategy. This lead to an increase of $x_k$, which in turns decrease the fitness  $F_k(\bs{x})$.

Furthermore, the equilibrium is reached when all agents that belong to the same population have the same fitness.
Thus, at the equilibrium $F_i(\bs{x}) = F_j(\bs{x})$, where $i,j\in\{1, \ldots, n \}$.
If we define $F_{n+1}(\bs{x}) = 0$, then at the equilibrium we have $F_i(\bs{x}) = 0$  for every strategy $i\in\{1, \ldots, n\}$.
%
Since the fitness function decreases with the action of users, we can conclude that the strategy of the population evolves to make the gradient of the objective function equal to zero (or as close as possible). This resembles a gradient method to solve optimization problems.

% A characteristic of this implementation is that the  function of users depends on their strategy. Specifically, there are $n+1$ different strategy functions.

Recall that the he evolution of the strategies lies in the simplex, that is, $\sum_{i \in S^p} z_i = m$. Hence, this implementation solves the following optimization problem:
\begin{equation}
\begin{aligned}
& \underset{\bs{z}}{\text{maximize}}
& &  f(\bs{z}) \\
& \text{subject to}
& & \sum_{i=1}^n z_i \leq m,
\end{aligned}
\label{eq:opt_problem}
\end{equation}
where m is the total mass of the population.
%Next we present a different implementation in which each user of the same population has the same fitness function.
%
Figure \ref{fig:maximization_a} shows an example of the setting described above for the  function
\begin{equation}\label{eq:objective_f}
 f(\bs{z}) = - (z_1-5)^2 - (z_2-5)^2.
\end{equation}
The simulation is executed during $0.6$ time units.

\begin{figure}[htb]
\centering
 \includegraphics[width=.5\textwidth]{./images/maximization_a.eps}
 \caption{Evolution of the maximization setting using only one population.}
 \label{fig:maximization_a}
\end{figure}

\subsection{Multi-population Case}

Let us consider $n$ populations where each agent can choose one of two strategies.
We define a population per each variable of the maximization problem and also $n$ additional strategies that resemble slack variables.
Thus, $x_i^p$ is the proportion of agents that use the $i\th$ strategy in the $p\th$ population. In this case $x_1^k$ corresponds to the $k\th$ variable, that is, $x_1^k = z_k$, while $x_2^k$ is a slack variable.
%
The fitness function $F_1^k$ of the $k\th$ population is defined as the derivative of the objective function with respect to the $k\th$ variable, that is, $F_1^k(\bs{x}) \equiv \frac{\partial }{\partial x_1^k} f(\bs{x})$. On the other hand, $F_2^k(\bs{x}) = 0$.
This implementation solves the following optimization problem:
\begin{equation}
\begin{aligned}
& \underset{\bs{z}}{\text{maximize}}
& &  f(\bs{z}) \\
& \text{subject to}
& & z_i \leq m^i, i =\{1,\ldots,n\}.
\end{aligned}
\label{eq:opt_problem}
\end{equation}

Figure \ref{fig:maximization_b} shows an example of the setting described above for the function in Eq. (\ref{eq:objective_f}).
The simulation is executed during $0.6$ time units. Note that the implementation using multiple populations reach the optimal value faster than the single population implementation.
\begin{figure}[htb]
\centering
 \includegraphics[width=.5\textwidth]{./images/maximization_b.eps}
 \caption{Evolution of the the maximization setting using $n$ populations.}
 \label{fig:maximization_b}
\end{figure}

The speed of convergence to the optimum depends on the dynamics and their parameters. For instance, we observed that the equilibrium of the BNN dynamics might be closer to the optimal solution $\bs{z}^*$ if the mass of the population $m^p$ is close to $\sum_{i=1}^N z_i$. Note that close to the optimum $\hat{F}^p$ is small, and if $m^p$ is too large, then the slack variable, such as  $x_2^k$, might be too large, making $x_1^k$ small. These conditions might hinder the convergence to the optimum because updates in the strategies are too small.


\lab{Markov Chains}{Markov Chains}
\label{lab:Markov}
\objective{
A \emph{Markov chain} is a collection of states with specified probabilities for transitioning from one state to another.
They are characterized by the fact that the future behavior of the system depends only on its current state.
Markov chains have far ranging applications.
In this lab, we learn to construct, analyze, and interact with Markov chains and apply a Markov chain to natural language processing.}

\section*{State Space Models} % ===============================================

Many systems can be described by a finite number of states.
For example, a board game where players move around the board based on die rolls can be modeled by a Markov chain.
Each space represents a state, and a player is said to be in a state if their piece is currently on the corresponding space.
In this case, the probability of moving from one space to another only depends on the players current location: where the player was on a previous turn does not affect their current turn.

% A Markov chain is a collection of states, together with the probabilities of moving from one state to another.
Finite Markov chains have an associated \emph{transition matrix} that stores the information about the transitions between the states in the chain.
The $(ij)^{th}$ entry of the matrix gives the probability of moving from state $i$ to state $j$.
Thus the rows of the transition matrix must sum to 1.

\begin{info} % Row / column stochasticity
A transition matrix where the rows sum to 1 is called \emph{row stochastic} (or \emph{right stochastic}).
The columns of a \emph{column stochastic} (or \emph{left stochastic}) transition matrix each sum to 1 and the $(i,j)^{th}$ entry of the matrix gives the probability of moving from state $j$ to state $i$.
Both representations are common, but in this lab we exclusively use row stochastic transition matrices for consistency.
\end{info}

Consider a very simple weather model where the probability of being hot or cold depends on the weather of the previous day.
If the probability that tomorrow is hot given that today is hot is 0.7, and the probability that tomorrow is cold given that today is cold is 0.4, then by assigning hot to the $0^{th}$ row and column, and cold to the $1^{st}$ row and column, this Markov chain has the following transition matrix:
%
\begin{align*}
\begin{blockarray}{ccc}
& \text{\textcolor{red}{hot tomorrow}} & \text{\textcolor{blue}{cold tomorrow}} \\
\begin{block}{c(cc)}
\text{\textcolor{red}{hot today}}   & 0.7 & 0.3 \\
\text{\textcolor{blue}{cold today}} & 0.6 & 0.4 \\
\end{block}\end{blockarray}
\end{align*}

If it is hot today, we examine the $0^{th}$ row of the matrix.
There is a $70\%$ chance that tomorrow will be hot ($0^{th}$ column) and a $30\%$ chance that tomorrow will be cold ($1^{st}$ column).
Conversely, if it is cold today, there is a $60\%$ chance that tomorrow will be hot and a $40\%$ chance that tomorrow will be cold.

Markov chains can be represented by a \emph{state diagram}, a type of directed graph.
The nodes in the graph are the states, and the edges indicate the state transition probabilities.
The Markov chain described above has the following state diagram.
%
% TODO: Turn this into a tikzpicture.
\begin{figure}[H]
    \includegraphics[width=.5\linewidth]{figures/WeatherChain.pdf}
\end{figure}
%
\begin{problem} % Problem: stochasticity.
Transition matrices for Markov chains are efficiently stored as NumPy arrays.
Write a function that accepts a dimension $n$ and returns the transition matrix for a random Markov chain with $n$ states.
\\
(Hint: use array broadcasting to avoid looping.)
\end{problem}

\subsection*{Simulating State Transitions} % ----------------------------------

% TODO: Use the binomial distribution instead of uniform. (?)
Since the rows of a transition matrix sum to $1$, the entries of each row partition the interval $[0, 1]$.
We can thus choose the next state to move to by generating a random number between $0$ and $1$.

Consider again the simple weather model and suppose that today is hot.
The row that corresponds to ``hot''in the transition matrix is $[0.7, 0.3]$.
If we generate a random number and it is smaller than $0.3$, then the simulation indicates that tomorrow will be cold.
Conversely, if the random number is between $0.3$ and $1$, then the simulation says that tomorrow will be hot.

\begin{lstlisting}
import numpy as np

def forecast():
	"""Forecast tomorrow's weather given that today is hot."""
	transition_matrix = np.array([[0.7, 0.3], [0.6, 0.4]])
	# Sample from the standard uniform distribution to choose a new state.
	if np.random.random() < transition_matrix[0, 1]:
		return 1              # Tomorrow will be cold.
	else:
		return 0              # Tomorrow will be hot.
\end{lstlisting}

\begin{problem} % Problem: Forecasting over several days.
Modify \li{forecast()} so that it accepts a parameter \li{days} and runs a simulation of the weather for the number of days given.
Return a list containing the day-by-day weather predictions (0 for hot, 1 for cold).
Assume the first day is hot, but do not include the data from the first day in the list of predictions.
The resulting list should therefore have \li{days} entries.
\end{problem}

\subsection*{Larger Chains} % -------------------------------------------------

The \li{forecast()} function makes one random draw from a \emph{uniform} distribution to simulate a state change.
Larger Markov chains require draws from a \emph{multinomial} distribution, a multivariate generalization of the binomial distribution.

A single draw from a binomial distribution with parameter $p$ indicates successes or failure of a single experiment with probability $p$ of success.
The classic example is a coin flip, where the $p$ is the probability that the coin lands heads side up.
A single draw from a multinomial distribution with parameters $\left(p_1, p_2, ..., p_n \right)$ indicates which of $n$ outcomes occurs.
In this case the classic example is a dice roll, with $6$ possible outcomes instead of the $2$ in a coin toss.

\begin{lstlisting}
# To simulate a single dice roll, store the probabilities of each outcome.
>>> die_probabilities = np.array([1./6, 1./6, 1./6, 1./6, 1./6, 1./6])

# Make a single random draw (roll the die once).
>>> np.random.multinomial(1, die_probabilities)
array([0, 0, 0, 1, 0, 0])                       # The roll resulted in a 4.
\end{lstlisting}

\begin{problem} % Problem: 4 states instead of 2. Multinomial transitioning.
Let the following be the transition chain for a Markov chain modeling weather with four states: hot, mild, cold, and freezing.

\begin{align*}
\begin{blockarray}{ccccc}
& \text{\textcolor{red}{hot}} & \text{\textcolor[rgb]{0,.6,0}{mild}} & \text{\textcolor{blue}{cold}} & \text{\textcolor{cyan}{freezing}} \\
\begin{block}{c(cccc)}
\text{\textcolor{red}{hot}}                 & 0.5 & 0.3 & 0.2 & 0 \\
\text{\textcolor[rgb]{0,.6,0}{mild}}       & 0.3 & 0.3 & 0.3 & 0.1 \\
\text{\textcolor{blue}{cold}}               & 0.1 & 0.3 & 0.4 & 0.2 \\
\text{\textcolor{cyan}{freezing}}           & 0 & 0.3 & 0.5 & 0.2 \\
\end{block}\end{blockarray}
\end{align*}

Write a new function that accepts a parameter \li{days} and runs the same kind of simulation as \li{forecast()}, but that uses this new four-state transition matrix.
This time, assume the first day is mild.
Return a list containing the day-to-day results (0 for hot, 1 for mild, 2 for cold, and 3 for freezing).
\label{prob:makov-state-transition}
\end{problem}

% TODO: instead of crappy empirical analysis, teach briefly about steady states and raising the transition matrix to a large power.

\begin{problem} % Problem: Analysis of results.
Write a function that investigates and interprets the results of the simulations in the previous two problems.
Specifically, find the average percentage of days that are hot, mild, cold, and freezing in each simulation.
Does changing the starting day alter the results?
Print a report of your findings.
\end{problem}

\section*{Using Markov Chains to Simulate English} % ==========================
% TODO: is it okay to make this reference?
One of the original applications of Markov chains was to study natural languages.\footnote{In computer science, a \emph{natural language} is a spoken language, like English or Russian. See \url{http://langvillea.people.cofc.edu/MCapps7.pdf} for some details on the early applications of Markov chains, including the study of natural languages.}
In the early $20^{th}$ century, Markov used his chains to model how Russian switched from vowels to consonants.
By mid-century, they had been used as an attempt to model English.
It turns out that Markov chains are, by themselves, insufficient to model very good English.
However, they can approach a fairly good model of bad English, with sometimes amusing results.

By nature, a Markov chain is only concerned with its current state.
Thus a Markov chain simulating transitions between English words is completely unaware of context or even of previous words in a sentence.
For example, a Markov chain's current state may be the word ``continuous.''
Then the chain would say that the next word in the sentence is more likely to be ``function'' rather than ``raccoon.''
However, without the context of the rest of the sentence, even two likely words stringed together may result in gibberish.

We restrict ourselves to a subproblem of modeling the English of a specific file.
The transition probabilities of the resulting Markov chain will reflect the sort of English that the source authors speak.
Thus the Markov chain built from \emph{The Complete Works of William Shakespeare} will differ greatly from, say, the Markov chain built from a collection of academic journals.
We will call the source collection of works in the next problems the \emph{training set}.

\subsection*{Making the Chain} % ----------------------------------------------

With the weather models of the previous sections, we chose a fixed number of days to simulate.
However, English sentences are of varying length, so we do not know beforehand how many words to choose (how many state transitions to make) before ending the sentence.
To capture this feature, we include two extra states in our Markov model: a \emph{start state} (\textcolor[rgb]{0,.6,0}{\$tart}) marking the beginning of a sentence, and a \emph{stop state} (\textcolor{red}{\$top}) marking the end.
Thus if a training set has $N$ unique words, the transition matrix will be $(N+2) \times (N+2)$.

The start state should only transition to words that appear at the beginning of a sentence in the training set, and only words that appear at the end a sentence in the training set should transition to the stop state.
The stop state is called an \emph{absorbing state} because once we reach it, we cannot transition back to another state.
% Because every state has a possible path to the stop state, this model is called an \emph{absorbing Markov chain}.

After determining the states in the Markov chain, we need to determine the transition probabilities between the states and build the corresponding transition matrix.
Consider the following small training set as an example.

\begin{lstlisting}
<<I am Sam Sam I am.
Do you like green eggs and ham?
I do not like them, Sam I am.
I do not like green eggs and ham.>>
\end{lstlisting}

If we include punctuation (so ``ham?'' and ``ham.'' are counted as distinct words) and do not alter the capitalization (so ``Do'' and ``do'' are also different), there are 15 unique words in this training set:
%
\begin{align*}
\text{I\quad am\quad Sam\quad am.\quad Do\quad you\quad like\quad green}
\\
\text{eggs\quad and\quad ham?\quad do\quad not\quad them,\quad ham.}
\end{align*}

With start and stop states, the transition matrix should be $17 \times 17$.
Each state must be assigned a row and column index in the transition matrix.
As easy way to do this is to assign the states an index based on the order that they appear in the training set.
Thus our states and the corresponding indices will be as follows:
%
\begin{align*}
\begin{array}{ccccccc}
\text{\textcolor[rgb]{0,.6,0}{\$tart}} & \text{I} & \text{am} & \text{Sam} & \ldots & \text{ham.} & \text{\textcolor{red}{\$top}}
\\
0 & 1 & 2 & 3 & \ldots & 15 & 16
\end{array}
\end{align*}

The start state should transition to the words ``I'' and ``Do'', and the words ``am.'', ``ham?'', and ``ham.'' should each transition to the stop state.
We first count the number of times that each state transitions to another state:

\begin{align*}
\begin{blockarray}{cccccccc}
& \text{\textcolor[rgb]{0,.6,0}{\$tart}} & \text{I} & \text{am} & \text{Sam} & & \text{ham.} & \text{\textcolor{red}{\$top}} \\
\begin{block}{c(ccccccc)}
\text{\textcolor[rgb]{0,.6,0}{\$tart}} 	& 0 & 3 & 0 & 0 & \ldots & 0 & 0\\
\text{I} 		& 0 & 0 & 1 & 0 & \ldots & 0 & 0\\
\text{am} 		& 0 & 0 & 0 & 1 & \ldots & 0 & 0\\
\text{Sam} 		& 0 & 2 & 0 & 1 & \ldots & 0 & 0\\
& \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\
\text{ham.} 	& 0 & 0 & 0 & 0 & \ldots & 0 & 1\\
\text{\textcolor{red}{\$top}} 		& 0 & 0 & 0 & 0 & \ldots & 0 & 1\\
\end{block}\end{blockarray}
\end{align*}

Now we divide each row by its sum so that each row sums to 1.

\begin{align*}
\begin{blockarray}{cccccccc}
& \text{\textcolor[rgb]{0,.6,0}{\$tart}} & \text{I} & \text{am} & \text{Sam} & & \text{ham.} & \text{\textcolor{red}{\$top}} \\
\begin{block}{c(ccccccc)}
\text{\textcolor[rgb]{0,.6,0}{\$tart}} & 0 & 3/4 & 0 & 0 & \ldots & 0 & 0\\
\text{I}        & 0 & 0 & 1/5 & 0 & \ldots & 0 & 0\\
\text{am}       & 0 & 0 & 0 & 1 & \ldots & 0 & 0\\
\text{Sam}      & 0 & 2/3 & 0 & 1/3 & \ldots & 0 & 0\\
& \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\
\text{ham.}     & 0 & 0 & 0 & 0 & \ldots & 0 & 1\\
\text{\textcolor{red}{\$top}}        & 0 & 0 & 0 & 0 & \ldots & 0 & 1\\
\end{block}\end{blockarray}
\end{align*}

The $3/4$ indicates that 3 out of 4 times, the sentences in the training set start with the word ``I''.
Similarly, the $2/3$ and $1/3$ tell us that ``Sam'' is followed by ``I'' twice and by ``Sam'' once in the training set.
Note that ``am'' (without a period) always transitions to ``Sam'' and that ``ham.'' (with a period) always transitions the stop state.
Finally, to avoid a row of zeros, we place a 1 in the bottom right hand corner of the matrix (so the stop state always transitions to itself).

The entire procedure of creating the transition matrix for the Markov chain with words from a file as states is summarized below in Algorithm \ref{alg:MarkovSentencesTransitionMatrix}.

\begin{algorithm} % Read a file and convert it into a Markov chain.
\begin{algorithmic}[1]
\Procedure{MakeTransitionMatrix}{}
\State Count the number of unique words in the training set.
\State Initialize a square array of zeros of the appropriate size to be the transition \par\quad matrix (remember to account for the start and stop states).
\State Initialize a list of states, beginning with \li{"\$tart"}.
\For {each sentence in the training set}
    \State Split the sentence into a list of words.
    \State Add each \emph{new} word in the sentence to the list of states.
    \State Convert the list of words into a list of indices indicating which row and \par\qquad\enspace column of the transition matrix each word corresponds to.
    \State Add 1 to the entry of the transition matrix corresponding to
    \par\qquad\enspace transitioning from the start state to the first word of the sentence.
    \For {each consecutive pair $(i, j)$ of words in the list of words}
        \State Add 1 to the entry of the transition matrix corresponding to \par\qquad\qquad transitioning from state $i$ to state $j$.
    \EndFor
    \State Add 1 to the entry of the transition matrix corresponding to
    \par\qquad\enspace transitioning from the last word of the sentence to the stop state.
\EndFor
\State Make sure the stop state transitions to itself.
\State Normalize each row by dividing by the row sums (Hint: array broadcasting).
\EndProcedure
\end{algorithmic}
\caption{Convert a training set of sentences into a Markov chain.}
\label{alg:MarkovSentencesTransitionMatrix}
\end{algorithm}

\begin{problem} % Problem: Class that makes a Markov chain from a file.
Write a class called \li{SentenceGenerator}.
The constructor should accept a filename (the training set).
Read the file and build a transition matrix from its contents.
You may assume that the file has one complete sentence written on each line.
\label{problem:markov-random-sentences-init}
\end{problem}

\begin{problem} % Problem: Create random sentences
Add a method to the \li{SentenceGenerator} class called \li{babble()}.
Begin at the start state and use the strategy from Problem \ref{prob:makov-state-transition} to repeatedly transition through the object's Markov chain.
Keep track of the path through the chain and the corresponding sequence of words.
When the stop state is reached, stop transitioning to terminate the simulation.
Return the resulting sentence as a single string.
\newpage
For example, your \li{SentenceGenerator} class should be able to create random sentences that sound somewhat like Yoda speaking.

\begin{lstlisting}
>>> yoda = SentenceGenerator("Yoda.txt")
>>> for i in xrange(5):
... 	print(yoda.babble())
...
<<
Impossible to my size, do not!
For eight hundred years old to enter the dark side of Congress there is.
But beware of the Wookiees, I have.
Fear leads to eat as well.
But agree on this, we must, and find your weapon!>>
\end{lstlisting}

\label{prob:markov-random-sentences-babble}
\end{problem}

\newpage

\section*{Additional Material} % ==============================================

\subsection*{Large Training Sets} % -------------------------------------------

The approach in Problems \ref{problem:markov-random-sentences-init} and \ref{prob:markov-random-sentences-babble} begins to fail as the training set grows larger.
For example, a single Shakespearean play may not be large enough to cause memory problems, but \emph{The Complete Works of William Shakespeare} certainly will.

To accommodate larger data sets, consider use a sparse matrix for the transition matrix in instead of a regular NumPy array. %(use the \li{lil_matrix} from the \li{scipy.sparse} library). % Why lil_matrix?
Ensure that the process still works on small training sets, then proceed to larger training sets.
How are the resulting sentences different if a very large training set is used instead of a small training set?

\subsection*{Variations on the English Model} % -------------------------------

Choosing a different state space for the English Markov model produces different results.
Consider modifying your \li{SentenceGenerator} class so that it can determine the state space in a few different ways.
The following ideas are just a few possibilities.

\begin{itemize}
\item Let each punctuation mark have its own state.
In the example training set, instead of having two states for the words ``ham?'' and ``ham.'', there would be three states: ``ham'', ``?'', and ``.'', with ``ham'' transitioning to both punctuation states.
\item Model paragraphs instead of sentences.
Add a \textcolor[rgb]{0,.6,0}{\$tartParagraph} state that always transitions to \textcolor[rgb]{0,.6,0}{\$tartSentence} and a \textcolor{red}{\$topParagraph} state that is sometimes transitioned to from \textcolor{red}{\$topSentence}.
\item Let the states be individual letters instead of individual words.
Be sure to include a state for the spaces between words.
We will explore this particular state space choice more in Volume III.
\end{itemize}

\subsection*{Natural Language Processing Tools} % -----------------------------

The Markov model of Problems \ref{problem:markov-random-sentences-init} and \ref{prob:markov-random-sentences-babble} is an example of \emph{natural language processing}.
The \li{nltk} module (natural language toolkit) has many tools for parsing and analyzing text.
For example, \li{nltk.sent_tokenize()} reads a single string and splits it up by sentence.

\begin{lstlisting}
>>> from nltk import sent_tokenize
>>> with open("Yoda.txt", 'r') as yoda:
...     sentences = sent_tokenize(yoda.read())
...
>>> print(sentences)
<<['Away with your weapon!',
 'I mean you no harm.',
 'I am wondering - why are you here?',
 ...>>
\end{lstlisting}

The \li{nltk} module is \textbf{not} part of the Python standard library.
For instructions on downloading, installing, and using \li{nltk}, visit \url{http://www.nltk.org/}.


\chapter{Assignment: \protect\\ Hierarchical Clustering}
\label{hw:arheo_hierarchical_clustering}

\newthought{Shards belong to different time periods}. Periods are described with 22 numeric variables, each defining the likelihood of a shard belonging to the said period. First, let us visualize the data in a visualization that shows the value of numeric variables with color - the higher the value, the brighter the number. Such a visualization is called a \widget{Heat Map}.

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.35]{heat-map-original.png}
    \caption{$\;$} % empty caption for proper page setting
\end{figure}

By default, Heat Map shows each data instance in a row and maps the values to the color scale. But our data is so unorganized, there is no way we can make sense of it! First, since we have many rows in our data set, let us join together rows that are the most similar. We will do this with \textit{Merge by k-means} option and set the number of clusters to 100. This is keep only 100 most different rows.

Visualization got simpler, but it is still unorganized. Would it be nice to have similar rows closer together? Of course it would! We can use hierarchical clustering to re-order rows, so that similar rows will be put next to each other. Use \textit{Clustering (opt. ordering)} - the plot now makes much more sense!

\begin{wrapfigure}{o}{1.05\textwidth}
    \vspace{-0.1cm}
    \includegraphics[scale=0.4]{heat-map-clustered.png}
\end{wrapfigure}

In the top left corner, we have shards from the early periods. In the bottom central part shards from Roman and Hellenic periods. And in the middle right part recent or unidentified shards.

We can repeat the same, but a more exact procedure, with \widget{Hierarchical Clustering}. First, let us pass the data to \widget{Distances} to compute the distance matrix. As our data has a fair number of dimensions (22 to be precise), we will use \textit{cosine distance} and \textit{Ward linkage} for the measure of cluster similarity.

In Hierarchial Clustering, we see a large dendrogram. To make it easier to read, set \textit{Max depth} to 10 (this will cut the tree at the tenth split). Use Zoom to zoom out as much as possible.

\begin{figure*}[h]
    \centering
    \includegraphics[scale=0.45]{hierarchical-clustering.png}
    \caption{$\;$} % empty caption for proper page setting
\end{figure*}

\newpage

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.6]{workflow.png}
    \caption{$\;$} % empty caption for proper page setting
\end{figure}

\subsection{Assignment}

Use Box Plot to explore the clusters:
\begin{itemize}
    \item What is a good number of clusters? Why?
    \item Cut the dendrogram at the desired level (up to you). Which period(s) does each of the clusters represent? Could you name them?
    \item What defines each period? Use Box Plot with \textit{Data} output to find out.
\end{itemize}