\chapter{Hull-White Single-Factor Model}
\label{chapter:hullwhitemodel}
The following chapter will study the Hull-White Single-Factor Model to understand the algorithm behind it and prepare a strategy for a parallel implementation. It will first introduce a general overview of how a trinomial tree is being constructed, and later show how prices are being discounted from it.

\section{Hull-White Trinomial Tree}
In this project, we implement the trinomial tree numerical method to discretize the Hull-White model.  In contrast to the standard trinomial tree, the tree used in the Hull-White model incorporates the mean-reversion of the interest rate, by using a width limit and modified branching methods for the tree. Standard branching (see fig.~\ref{fig:background:standardbranching}) remains the same throughout the tree. At the bottom of the tree, where interest rates are very low, the \enquote{up one/straight along/down one} (see fig.~\ref{fig:background:altbranchingbottom}) branching is used. At the top of the tree, where interest rates are very high, the \enquote{straight along/down one/down two} branching is used (see fig.~\ref{fig:background:altbranchingtop}).

\begin{figure}
\centering
\begin{subfigure}{.3\textwidth}
  \centering
  \includegraphics[width=.7\linewidth]{img/standardbranch.jpg}
  \caption{Standard branching}
  \label{fig:background:standardbranching}
\end{subfigure}
\begin{subfigure}{.3\textwidth}
  \centering
  \includegraphics[width=.93\linewidth]{img/bottombranch.jpg}
  \caption{Bottom branching}
  \label{fig:background:altbranchingbottom}
\end{subfigure}
\begin{subfigure}{.3\textwidth}
  \centering
  \includegraphics[width=.7\linewidth]{img/topbranch.jpg}
  \caption{Top branching}
  \label{fig:background:altbranchingtop}
\end{subfigure}
\caption{Alternative branching methods for a trinomial tree.}
\label{fig:background:allbranchings}
\source{Based on Options, Futures and Other Derivatives\cite[pg. 698]{ofod}.}
\end{figure}

We observe that this pruning characteristic allows us to asses the size of the tree upfront and use this knowledge to enable more specific implementation optimizations, in particular how to map the tree to the parallel device thread and memory architecture. Otherwise, the tree could grow infinitely in its width, making it impossible to map to limited parallel architecture memory resources and invalidating certain parallel implementations.

\section{Overview}
Pricing a single option using Hull-White short-rate\footnote{The short rate, \textit{r}, at time \textit{t} is the rate that applies to an infinitesimally short period of time at time \textit{t}\cite[pg. 682]{ofod}} single-factor trinomial tree model enables the term structure of interest rates at any given time to be obtained from the value of the short rate \textit{r} at that time and the risk-neutral process for \textit{r}. This shows that, once the process for \textit{r} has been defined, everything about the initial zero curve and its evolution through time can be determined\cite[pg. 683]{ofod}.
\\\\
The model consists of two steps. The first (forward propagation along the tree) is the construction of the trinomial tree in order to obtain a list of alpha values for each time step. These alphas are later used in the second step (backward propagation along the tree) to fit the option/bond prices and obtain the option value back at the root node of the tree. The input fed to the algorithm consists of an option, which includes its strike price, maturity, time step length, mean-reversion rate\footnote{denoted as $a$ - determines the relative volatilities of long and short rates\cite[pg.9]{npfits}} and volatility\footnote{denoted as $\sigma$ - determines the overall level of volatility \cite[pg. 9]{npfits}}. The output is the estimated price of the option/bond. The two steps can be generalized as follows:

\begin{enumerate}
    \item \textbf{Forward propagation step:} Construct a term structure for the underlying asset by progressing one time step at a time. Determine neutral risk rate for a new time step using estimated yield curve data and estimated current asset values.
    \item \textbf{Backward propagation step:} Discount the asset prices to estimate option payoff at maturity going from the leaves of the tree to its root.
\end{enumerate}

Algorithm~\ref{alg:loops} shows a high-level overview of a function implementing this procedure for pricing one option. The input of the algorithm is an option and a yield curve (used for the computation of alphas) and the output is the estimated price of the option. The function consists primarily of two sequential (convergence) loops of count tree height, which contain inner parallel operators of count tree width, where tree height and width are specific to each option (and thus vary across options). The tree height is dependent on the number of time steps, i.e., maturity of the underlying bond and precision. The tree width is dependent on the number of terms and input parameters.

Different option/bond maturities (leading to different tree heights) and different level of pricing accuracy (number of simulated time steps leading to different tree dimensions) make the choice of an effective parallelization strategy difficult. It is necessary to have a deep understanding of the algorithm itself to achieve maximum parallelization efficiency. The book by John Hull\cite{ofod} provides a solid background on the topic, describing the mechanics of interest rates, markets, as well as application of binomial trees and eventually trinomial trees to option pricing. Chapter 30 further narrows the topic of using trinomial trees as a numerical method and introduces a step by step walk-through of applying the algorithm on a basic example. While some of the calculation details are omitted in the book, the authors provide references to previous articles\cite{npfits}\cite{uhwirt}, where they provide a thorough explanation backed with more detailed examples.

\newpage
It is important to mention that the construction of a trinomial tree is a discrete-time, lattice-based\footnote{A model that takes into account expected changes in various parameters e.g. interest rate over the duration of the option} numerical method, but the example in the book is simplified by cutting the tree at a certain height and using analytic formulas to produce a concrete result for a specific financial instrument - a zero-coupon bond maturing at time $(m + 1) * \triangle t$ \cite[pg. 704]{ofod}. These formulas have been found and proven to be effective by the authors of the book and the articles. While this simplification gives more precise results in the above mentioned specific case, constructing the entire tree and using all of the time steps provides a foundation for pricing other options with more sophisticated cashflows. All the implementations of this thesis will be focused on the described numerical approach.

The following sub-chapters are focused primarily on the intuition behind the algorithm, with the sole purpose to provide the reader with a general overview for it. For this reason, many of the details and formulas of calculating specific values are omitted, however they are thoroughly described in the book and the articles by Hull and White. As the model is best understood visually, we have included some of the supplementary images from the Hull and White book in order to support our algorithm explanation.

\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\caption{High-level overview of pricing a single option using Hull-White Single-Factor Model\label{alg:loops}}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

\Input{Option, YieldCurve}
\Output{Price approximation}
\;
alphas[0] = Compute yield at initial interest rate\;
Qs[0][$\text{width}/2$] = 1\tcc*{Initialize the root node to 1\textdollar}
\;
\tcc{Forward propagation (convergence) loop}
\For{$i = 0$ \KwTo height} {
    \tcc{Compute Qs at the next time step}
    \For{$j = 0$ \KwTo width} {
        Qs[$\text{i} + 1$][j] = Compute Q from Qs[i] and alphas[i]\;
    }
    Compute alphas[$\text{i} + 1$] from Qs[i]\;
}
\;
\tcc{Initialize prices at the last time step to 100\textdollar}
Prices[$\text{height} - 1$] = 100\;
\;
\tcc{Backward propagation (convergence) loop}
\For{$i = \mathit{height} - 1$ \KwTo 0} {
    \tcc{Compute prices at the previous time step}
    \For{$j = 0$ \KwTo width} {
        Prices[i][j] = Compute price from Prices[$\text{i}+1$] using alphas[i]\;
    }
}
\;
\tcc{Return price at the root node}
\Return Prices[0][$\text{width}/2$]\;
\end{algorithm}

\section{Forward Propagation}
\label{section:hullwhite:forwardpropagation}
The forward propagation by itself consists of two stages. Each of them computes different values on the same tree. While the tree height can grow indefinitely, depending on the number of time steps, the width of the tree is limited by mean-reversion (as reasoned for in chapter \ref{chapter:background}), by determining its max. width (or as we refer to it for simplicity - width).

\paragraph{Indexing} Since the tree is 2-dimensional, locating and computing values on individual nodes boils down to locating them by index first. Hence, we introduce the two indexes - \textit{i} and \textit{j}, used to indicate the tree height and its width respectively. To describe the meaning of \textit{i} and \textit{j} visually, we can use fig. \ref{fig:treeconststage1}, where nodes along the height - A, C, G - can be indexed as i = 0, 1, 2. Indexing across the width is different, as we intuitively denote the core of the tree - nodes A, C, G - with $j=0$. Going down along the three decreases the value of $j$, while going up increases it. This means that nodes D,H are located on $j=-1$, nodes B,F are located on $j=1$ and so on. We denote the highest node across the width as $j_{max}$ and $j_{min}$ as the lowest. Note that $j_{min}=-j_{max}$ due to the symmetry of the tree, making it possible to omit $j_{min}$ occurrences in our implementations by replacing them with $-j_{max}$. On fig. \ref{fig:treeconststage1} nodes E and I are at the top and at the bottom of the tree width, hence their $j$ indexes are equal to $j_{max}$ and $j_{min}$ respectively. The index values in this example are $j_{max}=2$ and $j_{min}=-2$. Note that our code cannot always be aligned with our intuition and indexes cannot be negative in our programs. Hence we can shift the indexing between $j_{min}$ and $j_{max}$ and count from $0$ to $2*j_{max}+1$ instead, making the root node to be located at index $j_{max}$. Despite that, we have used $j_{min}$ and $j_{max}$ as width boundaries throughout this report, as they are easier to comprehend.

\paragraph{Stage 1}
aims to construct a tree for a variable $R^*$ that is initially 0 and follows the Ornstein–Uhlenbeck stochastic\footnote{With a random probability distribution or pattern that may be analyzed statistically but may not be predicted precisely.} process\footnote{Tends to drift towards its long-term mean (also called mean-reverting).} $dR^*=-aR^*dt + \sigma dz$ which is symmetrical about $R^*=0$\cite[pg.698-699]{ofod}. Together with $R^*$, $p_u$, $p_m$ and $p_d$ are calculated to match the respective up, mid and down probabilities that match the expected change and variance of the change in R over the next interval $\triangle t$. Since the width of the tree is limited between $j_{min}$ and $j_{max}$, some of the branching is calculated differently, thus $p_u$, $p_m$ and $p_d$ depend on the node position. Naturally the tree construction happens iteratively, node by node, starting from the root. In the end of the stage, this first tree will always have a symmetrical shape\footnote{Trinomial trees are recombining, meaning that at any time, an up move followed by a down move has exactly the same effect on the price as a down move followed by an up move.} similar\footnote{Note that the width and height of the tree may differ based on the number of time steps and the maturity of the financial instrument} to fig.\ref{fig:treeconststage1}.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\textwidth]{img/treeconststage1wh.jpg}
	\caption{Example of the trinomial tree for $R^*$.}
	\source{Modified by the authors, based on Options, Futures and Other Derivatives\cite[pg. 699]{ofod}.}
	\label{fig:treeconststage1}
\end{figure}

An important property of this tree include first of all that it is self-recombining, causing it to be symmetric. The probabilities on the lower part of the tree will be the negative of the probabilities on the upper part of the tree, e.g. probability that node A reaches node D is minus the probability of node A reaching node B. Furthermore, also due to symmetry, all unique probabilities can be stored in an array of size the width of the tree, because e.g. the probability of node A reaching node B is the same as the probability of node C reaching node F and so on. Probabilities are used both in stage 2 of the forward propagation, but also in the backward propagation, thus it is necessary to contain them to the end, if they are to be stored. Last but not least, the way probabilities are calculated is different on $j_{min}$ or $j_{max}$, because of the difference in branching. This can be seen on fig. \ref{fig:treeconststage1} where nodes E and I branch out differently in comparison to all other nodes.

\paragraph{Stage 2}
In this stage, the rates at each node in the tree at each time step are shifted up by an amount - $\alpha$, chosen so that the revised tree correctly prices discount bonds \cite[pg. 6]{uhwirt}. This is done by defining $Q_{i,j}$ as the present value of a security that pays off \$1 if node (i, j) is reached and 0 otherwise. The starting point is to set $Q_{0,0}=0$ and $\alpha_0$ to the interest rate at time $\triangle t$. $Q$s at the next time step are then calculated by using the generalized formula \cite[pg.705]{ofod}:
\begin{equation}
\begin{gathered}
\begin{aligned}
Q_{m+1, j} = \sum_k Q_{m,k}q(k,j)exp[-(\alpha_m+k\triangle r)\triangle t]
\nonumber
\end{aligned}
\end{gathered}
\end{equation}
Assuming that we start at step $m$, to calculate the $Q$s on step $m+1$, we need to have the $\alpha$ on step $m$. Furthermore, once the $Q$s on step $m+1$ have been calculated, they are used to also find the $\alpha$ on $m+1$ later. This leads to conclude that $\alpha$s and $Q$s are interrelated on each time step. $\alpha$s are calculated using the generalized formula \cite[pg.703]{ofod}:
\begin{equation}
\begin{gathered}
\begin{aligned}
\alpha_{m} = \dfrac{\sum_{j=-n_m}^{n_m} Q_{m,j}e^{-j\triangle r\triangle t} - \ln{P_{m + 1}}}{\triangle t}
\nonumber
\end{aligned}
\end{gathered}
\end{equation}
At the end of this stage, the new tree will have changed visually. For example, the tree from fig. \ref{fig:treeconststage1} can be re-shaped as shown on fig. \ref{fig:treeconststage2}.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\textwidth]{img/treeconststage2.png}
	\caption{Example of the trinomial tree for $R$.}
	\source{Options, futures and other derivatives fig. 30.9 \cite[pg. 702]{ofod}}
	\label{fig:treeconststage2}
\end{figure}
An important observation here is that the only outcome of this tree that is used in the backward propagation is the array of $\alpha$s. $Q$s are in this case intermediary values, used to compute the $\alpha$ on each step and for this reason, the $Q$ values do not need to be stored any longer once all $\alpha$s have been computed.

\section{Backward Propagation}
The backward propagation starts with the previously constructed tree during the forward propagation step, in particular with array of $\alpha$s. At each time-step the option payoff is computed as the discounted value of the expected value at the next step \cite[pg. 6]{uhwirt}. From this it follows that the nodes at time step $i$ (e.g. the nodes without assigned letters in figures \ref{fig:treeconststage1} and \ref{fig:treeconststage2} above) are the starting point of the backward propagation. Their values are set to 100\$ and are used to compute the previous set of nodes (at time step $i-1$). That is done by discounting bond price values up until the exercise of the option. At the option expiration time step, we decide if we exercise the option or let it expire worthless. To achieve that, we calculate the difference between bond price and the strike price. The positive values mean exercise, while non-positive mean expiry worthless and are set to $0$. We discount the option prices further down to the root of the tree to get the approximation of the option price on the valuation day. This is the output of the algorithm. We use the array of $\alpha$s computed during the forward propagation through this procedure. It is important to note that determining the option price depends on the type of option (whether it is a put or a call option).

\section*{Summary}
This chapter has provided a detailed overview of the Hull-White Single-Factor Model, in particular its two-stage procedure fo propagating along a trinomial tree. It will be used in the following chapter, which will introduce the challenges of implementing a sequential version of the algorithm in C++.


\input{template.tex}

\title{Reminder on ordinal arithmetic}

\newcommand{\cf}{\mathrm{cf}}

\begin{document}

\maketitle

\part{Definitions}
\section{Well-orders}
Ordinals are the mother of all well-orders. A total order is called a \emph{well-order} if one the following equivalent conditions applies
\begin{itemize}
	\item Any nonempty set of element has a least element
	\item There is no infinite descending sequence of elements
\end{itemize}
Every subset of a well-ordered set is again well-ordered. Every element $a \in P$ that is not maximal has an immediate successor $a^+ = \min \{ b : b > a \}$. Every bounded subset of a well-order has a supremum. For every element $a \in P$, $a$ is either the minimal element, a \emph{successor element} ($a = b^+$) or a so-called \emph{limit element}. In this case, it is the supremum of the elements strictly below it. By the axiom of choice, every set can be well-ordered. \\

\textbf{Proposition} [Trichotomy]
If $P,Q$ are two well-orders, then we have the following trichotomy
\begin{itemize}
	\item $P$ is order-isomorphic to a proper initial segment of $Q$
	\item $P$ is order-isomorphic to $Q$
	\item A proper initial segment of $P$ is order-isomorphic to $Q$.
\end{itemize}

We call two well-orders \emph{of the same order-type} if they are order-isomorphic. The order-types are thus totally ordered by `is isomorphic to an initial segment of'. This will turn out to be a well-order.

\section{Ordinals}
Ordinal numbers are certain well-ordered sets, serving as distinguished representatives for order-types. For every well-ordered set $P$, there is a unique ordinal $\alpha$ such that $\alpha$ and $P$ are order-isomorphic. The proper class of ordinal numbers is well-ordered, and every well-ordered set is order-isomorphic to an initial segment of the ordinal numbers.

We use the von Neumann-construction of ordinals, i.e. for every ordinal $\lambda$, we let

\[ \lambda = \{ \alpha : \alpha < \lambda \} = [0,\lambda) \]

as a set. We have for all ordinals $\alpha,\lambda$ that
\[ \alpha < \lambda \Leftrightarrow \alpha \in \lambda \Leftrightarrow \alpha \subset \lambda \Leftrightarrow \alpha \text{ proper initial segment of } \lambda \]
The ordinals are as sets defined recursively via

\[
\begin{cases}
	0 = \emptyset \\
	\alpha^+ = \alpha \cup \{\alpha\} \\
	\lambda = \bigcup_{\alpha < \lambda} \alpha
\end{cases}
\]

For example we have $2 = \{0,1\} = \{0,\{0\}\} = \{\emptyset, \{\emptyset\}\}$ and we get the first transfinite ordinal $\omega$ via
\[ \omega := \bigcup_{n \text{ finite }} n = \{0,1,2,\ldots\} = \mathbb N \]
and its successor
\[ \omega^+ := \mathbb N \cup \{ \mathbb N \} = \{0,1,2,\ldots\, \mathbb N \}. \]

Every \emph{set} $\Gamma$ of ordinals has a supremum in the ordinals, and we use the following three notations interchangably.

\[ \sup \Gamma = \lim_{\gamma \in \Gamma} \gamma := \bigcup_{\gamma \in \Gamma} \gamma \]

\section{Cardinals}
We model cardinals as special ordinals, namely we identify every cardinal $\mathfrak a$ with the least ordinal $\alpha$ that is in bijection with $\mathfrak a$. Note that now the cardinal $\alpha$ is itself the canonical representative of a set of cardinality $\alpha$, as
\[ |\alpha| = |\{ \beta : \beta < \alpha \}| = \alpha. \]

Note that $\omega$ is the first infinite ordinal and thus a cardinal
\[ \omega =: \aleph_0 \]

Cardinals are again well-ordered, leading to their own notion of cardinal sucessors. We can thus enumerate the infinite cardinals by ordinals
\[
\begin{cases}
	\aleph_0 = \omega \\
	\aleph_{\alpha^+} = (\aleph_\alpha)^+ \\
	\aleph_\lambda = \bigcup_{\alpha < \lambda} \aleph_\alpha
\end{cases}
\]

We let $\omega_{\alpha} = \aleph_{\alpha}$, so e.g. $\omega_1$ is the first uncountable ordinal. \\

\textbf{Nonexample:} Let's appreciate some of the non-intuitiveness of $\omega_1$. It is an uncountable well-ordered set, but every initial segment is countable. Unlike $\omega = \sup \{0,1,2,\ldots\}$, $\omega_1$ is not the supremum of any countable subset. Let $\omega_1 = \sup \Gamma, \Gamma \subseteq \omega_1$ countable, then
\[ \omega_1 = \bigcup \Gamma, \] thus $\omega_1$ is countable union of countable sets, hence countable; contradiction.

\section{Cofinality}
The phenomenon from above is captured by the notion of cofinality. A subset $N \subseteq P$ of an ordered set is called \emph{cofinal} if
\[ \forall p \in P \exists n \in N : p \leq n. \]
The cofinality $\cf(P)$ is the least cardinality of a cofinal subset of $P$. We have $\cf(P) = 1$ iff $P$ has a maximial element, otherwise at least $\cf(P) \geq \omega$. \\

For an ordinal $\alpha$, $\cf(\alpha)$ is the least cardinality of a subset $\Gamma \subseteq \alpha$ with $\alpha = \sup \Gamma$. We have shown that $\cf(\omega_1) = \omega_1$. \\

A cardinal $\lambda$ is called \emph{regular} iff $\cf(\lambda) = \lambda$. \\

\textbf{Proposition: } For every successor cardinal $\alpha$, $\aleph_\alpha$ is regular. For every limit ordinal $\lambda$, we have $\cf(\aleph_\lambda) = \cf(\lambda)$ instead. \\

\textbf{Example: } $\aleph_\omega$ is not regular, as
\[ \cf(\aleph_\omega) = \cf(\omega) = \omega. \]
In fact, by definition $\aleph_\omega = \sup \{ \aleph_0, \aleph_1, \ldots \}$ is supremum of a countable subset.

\section{Topology}
On every totally ordered set, there is an induced topology called the \emph{order topology} given by the basis of open rays and open intervals. For example, the order topology on $\mathbb R$ with the usual order is just the euclidean one.

Every ordinal $\alpha$ becomes a topological space of its own in the order topology. For example the ordinal $\omega$ is homeomorphic to the subspace $\mathbb N \subset \mathbb R$, whereas $\omega^+$ is homeomorphic to
\[ \{ 1 - 1/n : n = 1,2, \ldots\} \cup \{ 1 \}. \]
At some point, larger ordinals will not be metric spaces any more. \\

\pagebreak

\part{Ordinal arithmetic}
\section{Addition}
Ordinal arithmetic is defined recursively with recursion on the right operand. This leads to a certain asymmetry and enforces certain rules only on the right side.

\[
\begin{cases}
	\alpha + 0 = \alpha \\
	\alpha + (\beta^+) = (\alpha + \beta)^+ \\
	\alpha + \lambda = \bigcup_{\beta < \lambda} \alpha + \beta
\end{cases}
\]

\textbf{Proposition: } Addition is strictly monotonic and continuous in the right argument. Addition is associative

\textbf{Non-examples: } Addition is merely non-decreasing in the left argument and not continuous. Addition is not commutative.

We have
\[ 2 + \omega = \bigcup_{n < \omega} 2 + n = \sup \{ 2, 3, 4, \ldots \} = \omega. \]
From there, we get $0 + \omega = \omega = 2 + \omega$ and $\omega + 2 \neq 2 + \omega$. Regarding continuity
\[ \omega + 2 = \sup \{0,1,2,\ldots\} + 2 \neq \sup \{0 + 2, 1 + 2, \ldots\} = \omega. \]

\textbf{Proposition} Let $P,Q$ be two well-orders with order types $\alpha,\beta$. Then $\alpha+\beta$ is the order type of the well-order on $P \sqcup Q$ where every element of $P$ comes before every element of $Q$. E.g.

\[ \omega + \omega + 1 \cong \{ 0 < 1 < 2 < \ldots 0' < 1' < 2' < \ldots 0'' \} \]

\section{Ordinal multiplication}
We define multiplication by continuity and distributivity on the right.

\[
\begin{cases}
	\alpha \cdot 0 = 0 \\
	\alpha \cdot (\beta + 1) = (\alpha \cdot \beta) + \alpha \\
	\alpha \cdot \lambda = \bigcup_{\beta < \lambda} \alpha \cdot \beta
\end{cases}
\]

\textbf{Proposition: } Multiplication is strictly monotonic and continuous on the right and associative. $1$ is both-sided identity.

\textbf{Non-examples: } Multiplication is merely non-decreasing in the left argument and not continuous. Multiplication is not commutative and not distributive in the left argument. \\

We have $\omega \cdot 2 = \omega + \omega$, whereas

\[ 2 \cdot \omega = \bigcup_{n<\omega} 2\cdot n = \sup \{ 0, 2, 4, \ldots \} = \omega. \]

Therefore we get $\omega \cdot 2 \neq 2 \cdot \omega$ and $1\cdot \omega = 2\cdot \omega \neq (1+1)\times \omega$. Regarding continuity,
\[ \omega \cdot \omega = \sup \{ 1, 2, 3, \ldots \} \cdot \omega \neq \sup \{ 1 \cdot \omega, 2 \cdot \omega, 3 \cdot \omega, \ldots \} = \sup \{ \omega, \omega, \omega, \ldots \}. \]

\textbf{Proposition: } Let $P,Q$ be two well-orders with order types $\alpha,\beta$. Then $\alpha \cdot \beta$ is the order type of $P \times Q$ regarded as $Q$ copies of $P$ (reverse lexical order), i.e.

\[ \omega\cdot 2 \cong \{ 0 < 1 < 2 < \ldots 0' < 1' < 2' < \ldots \} \cong \omega + \omega \]

whereas

\[ 2 \cdot \omega \cong \{ 0 < 1 < 0' < 1' < 0'' < 1'' < \ldots \} \cong \omega \]

\section{Ordinal exponentiation}
Ordinal exponentation is defined recursively with continuity and the power-law $\alpha^{\beta + \gamma} = \alpha^\beta \cdot \alpha^\gamma$ in mind.

\[
\begin{cases}
	\alpha^0 = 1 \\
	\alpha^{\beta + 1} = \alpha^\beta \cdot \alpha \\
	\alpha^\lambda = \bigcup_{\beta < \lambda} \alpha^\beta
\end{cases}
\]

\textbf{Proposition} Exponentation is strictly monotonic and continuous in the exponent. It respects multiplication in the exponent, i.e. $\alpha^{\beta\gamma} = (\alpha^\beta)^\gamma$.

\textbf{Non-examples: } Exponentiation is merely non-decreasing and not continuous in the base. It does not respect multiplication in the base.

First of all, for all $1 < k < \omega$ we have
\[ k^\omega = \sup \{ k^n : n < \omega \} = \omega \]
Thus $2^\omega = 3^\omega$. Also
\[ \omega^2 \neq \sup \{ n^2 : n < \omega \} = \omega. \]
Regarding multiplication in the base, using associativity, we get
\[ (\omega \cdot 2)^2 = (\omega \cdot 2)(\omega \cdot 2) = \omega \cdot (2\omega) \cdot 2 = \omega \cdot \omega \cdot 2 = \omega^2 \cdot 2. \]

But then $(\omega \cdot 2)^2 \neq \omega^2 \cdot 2^2 = \omega^2 \cdot 4$.

\textbf{Proposition}
Let $P,Q$ be two well-orders with order types $\alpha, \beta$. Then $\alpha^\beta$ is the order type of the set of function with finite support $\alpha^{(\beta)}$ in reverse lexical order.

\section{Fixed-points}
Consider the power towers
\[ \omega \uparrow\uparrow 1 = \omega, \omega \uparrow \uparrow (n + 1) = \omega^{\omega \uparrow \uparrow n}. \]
We define their supremum as
\[ \epsilon_0 := \sup \{ \omega \uparrow \uparrow n : n < \omega \}. \]
\textbf{Proposition: } $\epsilon_0$ satisfies the equation $\omega^{\epsilon_0} = \epsilon_0$.

By continuity in the exponent, we get
\[ \omega^{\epsilon_0} = \sup_{n < \omega} \left(\omega^{\omega \uparrow \uparrow n}\right) = \sup_{n < \omega} (\omega \uparrow \uparrow (n+1)) = \epsilon_0 \]
and $\epsilon_0$ is the least fixed point of $\omega^\cdot$. \\

\textbf{Proposition} $\epsilon < \omega_1$, i.e. $\epsilon_0$ is still countable.
This is because for infinite ordinals of cardinality at most $\kappa$, all arithmetic operations still produce ordinals of size at most $\kappa$. Note that by our constructions, the resulting ordinals are in bijection with just sums, products and functions with finite support of their underlying sets.
Now
\[ \epsilon_0 = \bigcup_{n < \omega} \omega \uparrow \uparrow n \]
is countable union of countable sets, thus countable. \\

Note that we can produce other fixed-points for the continuous arithmetical operations in the very same fasion

\begin{align*}
\omega^{\epsilon_0} &= \epsilon_0 \\
\omega\cdot \omega^\omega &= \omega^\omega \\
\omega + \omega^2 &= \omega^2 \\
1 + \omega &= \omega.
\end{align*}

\section{Cantor normal form}
The ordinals in the interval $[0,\epsilon_0)$ are closed under ordinal addition, multiplication and exponentation. Every number $\alpha$ in that interval can be expressed as a unique so-called \emph{Cantor normal form} (CNF).
\[ \alpha = \omega^{\beta_1}c_1 + \ldots + \omega^{\beta_n}c_n \]
where $\alpha > \beta_1 > \ldots > \beta_n$ and $c_i \in \mathbb N$.
We can recursively transform the exponents $\beta_i$ in CNF and get a hereditary representation in base $\omega$ after finitely many steps. We see that $[0,\epsilon_0)$ is actually generated by the natural numbers and $\omega$ under the arithmetical operations.  Arithmetic in CNF has simple computational descriptions.

\subsection{Order}
CNFs can be compared lexicographically. Highest exponents first, then coefficients.

\subsection{Addition}
We see that
\[
\omega^\beta c + \omega^{\beta'}c' = \begin{cases}
	\omega^\beta c + \omega^{\beta'}c' & \text{ if } \beta > \beta' \\
	\omega^\beta (c+c') & \text{ if } \beta = \beta' \\
	\omega^{\beta'}c' & \text{ if } \beta < \beta'
\end{cases}
\]
If exponents are decreasing, they are already in CNF. If exponents are the same, simplify. If exponents are increasing, the bigger term absorbs the smaller one.
\textbf{Example}
\[ \omega^2 + \omega^3 = \omega^2 + \omega^2\omega = \omega^2(1+\omega) = \omega^2\omega = \omega^3. \]
Thus in order to add a term to a CNF, insert it at the correct position, simplify and drop all further terms.

\subsection{Multiplication}
Multiplication is distributive on the right. Thus we just need to understand how to multiply a CNF with a single term on the right. Let
\[ \alpha = \omega^{\beta_1}c_1 + \ldots + \omega^{\beta_n}c_n \]
then for $\beta > 0$
\[ \alpha \cdot \omega^\beta = (\omega^{\beta_1}c_1 + \ldots + \omega^{\beta_n}c_n) \cdot \omega^\beta = \omega^{\beta_1 + \beta}. \]
Through repeated summation, we get for $n \in \mathbb N \setminus \{0\}$
\[ \alpha \cdot n = \omega^{\beta_1}c_1 n + \ldots + \omega^{\beta_n}c_n. \]
So multiplication just acts on the highest term. \textbf{For example}
\[ (\omega^3 + \omega^2)\cdot 3 = \omega^3 + \omega^2 + \omega^3 + \omega^2 + \omega^3 + \omega^2 = \omega^3\cdot 3 + \omega^2. \]
In the limit case, we get
\begin{align*}
 (\omega^3 \cdot 2 + \omega^2)\omega &= \sup_{n < \omega} (\omega^3 \cdot 2 + \omega^2)n \\
 &= \sup_{n < \omega} (\omega^3 \cdot 2n + \omega^2) \\
 &= \omega^3 \omega = \omega^4.
\end{align*}

\subsection{Exponentiation}
Exponentiation with base $\omega$ is easy. For any $\beta$ in CNF, $\omega^\beta$ is already in CNF. Exponentiation with arbitrary base is more complicated. Let again

\[ \alpha = \omega^{\beta_1}c_1 + \ldots + \omega^{\beta_n}c_n \]

By the power laws
\[
 \alpha^{\beta + \gamma} = \alpha^\beta \cdot \alpha^\gamma, \quad \alpha^{\beta \gamma} = (\alpha^\beta)^\gamma, \]
we only need to unterstand taking powers with a single term. We distinguish finite and transfinite terms.

\subsubsection{Finite exponents}
For $0 < r < \omega$, we can compute $\alpha^r$ by repeated multiplication. \\

\subsubsection{Transfinite exponents}
For exponents $\omega^\beta$ with $\beta > 0$, first distinguish if $\alpha$ is finite. If it is, note that
\[ \alpha^\omega = \omega, \]
so if $\beta$ is finite, then
\[ \alpha^{\omega^\beta} = \omega^{\omega^{\beta-1}}. \]
If $\beta$ is transfinite, then $\beta = 1 + \beta$, so
\[ \alpha^{\omega^\beta} = \alpha^{\omega^{1+\beta}} = \alpha^{\omega\omega^\beta} = \omega^{\omega^\beta}. \]
If $\alpha$ is transfinite, we simply get
\[ \alpha^{\omega^\beta} = \omega^{\beta_1\omega^\beta}. \]

\textbf{Example}
\begin{align*}
(\omega^2 \cdot 2)^\omega &= \sup_{n<\omega} (\omega^2 \cdot 2)^n \\
&= \sup_{n < \omega} \omega^{2n}\cdot 2 \\
&= \omega^\omega.
\end{align*}

\section{Goodstein's sequence}
Goodstein's theorem is a very surprising theorem in number theory. Fix a basis $b \geq 2$. Take any number $n$ and write it in base-$b$,
\[ n = b^{k_1}c_1 + \ldots + b^{k_\ell} c_\ell. \]
Now $n > k_1 > \ldots > k_\ell$, so we can write the exponents in base-$b$ again and obtain a hereditary base-$b$ representation. \textbf{Example}
\[ 13 = 8 + 4 + 1 = 2^3 + 2^2 + 1 = 2^{2+1} + 2^2 + 1. \]

Fix a number $n$. We define the \emph{Goodstein sequence} $g^n(2), g^n(3), \ldots$ recursively. Let $g^n(2) = n$. Now write $g^n(2)$ in hereditary base-$2$ and bump the base, i.e. replace all $2$s by $3$s. Subtract one. That gives us $g^n(3)$. Repeat that, so in general
\[ g^n(b+1) = \text{ write $g^n(b)$ in base-$b$, replace $b\to b+1$, subtract $1$}. \]
 \textbf{For example}
\begin{align*}
	g^4(2) &= 4 = 2^2 \\
	g^4(3) &= 3^3 - 1 = 26 \\
	       &= 3^2\cdot 2 + 3\cdot 2 + 2 \\
	g^4(4) &= 4^2 \cdot 2 + 4 \cdot 2 + 1 \\
	g^4(5) &= 5^2 \cdot 2 + 5 \cdot 2 \\
	g^4(6) &= 6^2 \cdot 2 + 6 \cdot 2 - 1 \\
	       &= 6^2 \cdot 2 + 6 + 5 \\
	g^4(7) &= 7^2 \cdot 2 + 7 + 4
\end{align*}
The numerical values of this sequence are
\[ 4, 26, 41, 60, 83, 109, \ldots \]
\textbf{Theorem: }[Goodstein] For every integer $n$, the Goodstein sequence $g^n$ eventually becomes $0$. \\

The thing is: This takes long, extreemly, ridiculously long. The Goodstein function $G : \mathbb N \to \mathbb N$
\[ G(n) = \min \{ b : g^n(b) = 0 \} \]
is one of the fastest-growing functions ocurring in mathematics. We have
\[ G(3) = 7, \] that is $g^3$ terminates at base $7$. How long might $g^4$ need? We have
\[ G(4) = 3\cdot 2^{402653211}-1. \]
That is a number with roughly 121 million \emph{decimal digits}. The numbers of the sequence are of the same order of magnitude. Even though the numbers seem to get larger and larger, something forces them to become 0 at the end. They seem to lose some complexity throughout the operations. We can precisely pin down this complexity using ordinals and CNF. \\

\textbf{Proof of Goodstein's theorem} We know how the number $g^n(b)$ can be written in hereditary base-$b$. Define a second sequence $w^n(b)$ of ordinals in CNF by taking that representation and replacing $b$ with $\omega$. For example

\begin{align*}
	w^4(2) &= \omega^\omega \\
	w^4(3) &= \omega^2 \cdot 2 + \omega \cdot 2 + 2 \\
	w^4(4) &= \omega^2 \cdot 2 + \omega \cdot 2 + 1 \\
	w^4(5) &= \omega^2 \cdot 2 + \omega \cdot 2 \\
	w^4(6) &= \omega^2 \cdot 2 + \omega + 5
\end{align*}

This sequence of ordinals is strictly decreasing, thus can only have finite length. So $g^n$ can only have finitely many elements before stabilizing at zero.
\end{document}


\chapter{Replacements for t-tests and ANOVA}
ANOVA is a common procedure in classical statistics, and is related to the
simpler idea of a t-test. These classical tests were designed for particular
kinds of problems, and in this chapter we will study similar problems but
solve them from a Bayesian point of view.
We will also use these examples to discuss some issues about the choice
of prior distributions when there are more than a few parameters. When there
are only a few parameters it is usually safe to assign a vague, wide prior
to describe your initial uncertainty (unless, of course, you have more
information than that). In higher dimensions, problems can arise if you do
this. One way of getting around these problems is to use a
{\it hierarchical model}.

\section{A T-Test Example}
This example is based on one given in a 1976 article by physicist E. T. Jaynes,
called ``Confidence Intervals vs. Bayesian Intervals''. This is a very strongly
worded paper and might be an interesting read for
those who are interested in the battle between frequentist and Bayesian statistics
when the latter was making its comeback in the second half of the 20th century.
It's also where I got the crazy confidence interval example from.

Two manufacturers, $1$ and $2$, both make ``widgets'', and we are interested
in figuring out which manufacturer makes the best widgets (on average), as
measured by their lifetime. To determine this, we obtain 9 widgets from
manufacturer $1$ and 4 widgets from manufacturer $2$, and measure their
lifetimes, in days. The results are given below:
\begin{eqnarray}
x^1 &=& \{41.26, 35.81, 36.01, 43.59, 37.50, 52.70, 42.43, 32.52, 56.20\}\\
x^2 &=& \{54.97, 47.07, 57.12, 40.84\}
\end{eqnarray}
These measurements can be summarised by the means and standard deviations, which
are $42 \pm 7.48$ for group $1$ and $50 \pm 6.48$ for group $2$.
The question is: given this data, is there evidence that one of the manufacturers
is better than the other, and if so, by how much?
In classical statistics the standard procedure for this situation would be a
two sample $t$0-test. However, before we do anything I'd like you to consider
the numbers and use your intuition: what do {\it you} think about what the
evidence says?

An underlying assumption of a classical $t$-test is that the data are normally
distributed around the mean values for each group\footnote{Strictly speaking,
it's the probability distribution for the data given the parameters
that is normal, the data may or may not look normally distributed.}.
We may as well adopt this
assumption for our Bayesian model. If we call the group 1
data points $\{x^1_1, x^1_2, ..., x^1_{N_1}\}$ and the group 2 data points
$\{x^2_1, x^2_2, ..., x^2_{N_1}\}$, then the likelihood is:
\begin{eqnarray}
x^1_i &\sim& \mathcal{N}\left(\mu_1, \sigma^2\right)\nonumber\\
x^2_i &\sim& \mathcal{N}\left(\mu_2, \sigma^2\right)\label{eq:ttest_likelihood}
\end{eqnarray}
Where all the data points are independent given the parameters. Note the assumption that
the two groups have the same underlying (``population'') standard deviation $\sigma$. This is a popular
assumption in this kind of analysis but it is not necessarily well justified!
We will build our Bayesian models using this assumption, but it is not that
difficult to relax it if you want to. You could just include multiple
$\sigma$ parameters in the model,
just like how we will include the multiple $\mu$ parameters.

Instead of just one model for this situation, we will study three different
versions. Each model will have the same likelihood
as given above in Equation~\ref{eq:ttest_likelihood}, and the same prior
for $\sigma$. However, the models will all have different priors for $\mu_1$
and $\mu_2$.
We will be able to see that the choice of prior does
influence the results (of course), but in ways that make sense. Which of these
models is more appropriate in a practical situation would depend on the exact
situation. There is no ``one size fits all'' model.

\subsection{Likelihood}
To implement our model in JAGS, we can begin by specifying the likelihood
part like so:

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Sampling distribution/likelihood
for(i in 1:N1)
{
  x1[i] ~ dnorm(mu1, 1/sigma^2)
}
for(i in 1:N2)
{
  x2[i] ~ dnorm(mu2, 1/sigma^2)
}
\end{minted}
We have called our data arrays {\tt x1} and {\tt x2}, and we have also
assumed that the sample sizes {\tt N1} and {\tt N2} are defined, so our
{\tt data} list will need to be consistent with these choices. The parameters
we will be estimating are {\tt mu1}, {\tt mu2}, and {\tt sigma}, so we will
need to specify prior distributions for them. In the following sections, we'll
use the same prior for {\tt sigma}, so we may as well specify that now.
Let's use a log-uniform prior where $\sigma$ is between $e^{-10}$ and
$e^{10}$.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Prior for sigma
log_sigma ~ dunif(-10, 10)
sigma <- exp(log_sigma)
\end{minted}

\subsection{Prior 1: Very Vague}
The last missing ingredients to finish the JAGS model are the priors for
{\tt mu1} and {\tt mu2}. For our first model, let's be really naive and assign
super-wide uniform priors.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Prior 1: Very Vague
mu1 ~ dnorm(0, 1/1000^2)
mu2 ~ dnorm(0, 1/1000^2)
\end{minted}

At first glance, this might seem like a fairly reasonable thing to do. In many
problems, it doesn't make much difference if we just use vague priors and get
on with the calculation (as opposed to thinking really hard about the prior,
and what is actually known about the parameters).

However, this prior has a number of properties that suggest it might not be
quite right: firstly, what is the probability that $\mu_1 = \mu_2$?
In classical t-tests, the whole point is to test the hypothesis that
the two ``population means'' (parameters) are equal. However, our prior
actually implies that the probability they are equal is 0! Therefore, no matter
what data we get, the posterior probability of $\mu_1 = \mu_2$ will always be
zero.

\subsection{Prior 2: They might be equal!}
The problem with Prior 1 is that we may think $\mu_1$ might exactly equal
$\mu_2$, and Prior 1 doesn't allow for this. So here's another way we might
set up the prior. We'll start by defining the prior for $\mu_1$ as we did
before. Then, when we consider $\mu_2$, we need a way of giving it a
50\% probability of equalling $\mu_1$, and if not, then it should have
a ``bi-exponential'' distribution centered around $\mu_1$.
Here is our solution. Read it carefully and make sure you understand what this
prior does.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# First mean
mu1 ~ dnorm(0, 1/1000^2)

# Prior for difference, mu2 - mu1
u ~ dunif(-1, 1)

# Length of exponential prior given difference != 0
L <- 5
size_of_difference <- step(u)*(-L*log(1 - u))

# To make the difference positive or negative
C ~ dbin(0.5, 1)
difference <- (2*C - 1)*size_of_difference

# Second mean
mu2 <- mu1 + difference
\end{minted}

\subsection{Prior 3: Alright, they're not equal, but they might be {\it close}}
Prior 2 is also a little bit strange, if you think about it. If we're comparing
these two manufacturers of widgets, why would we think it is possible that the
two manufacturers are {\tt exactly} equal? Maybe we just think the parameters
$\mu_1$ and $\mu_2$ are likely to be {\it similar} in value.
In other words, we shouldn't worry so much about the
prior probablity of $\mu_1 = \mu_2$, but we should at least make sure there's
a moderate prior probability that $\mu_1 \approx \mu_2$.

One way we could do this is by applying a normal prior to both $\mu_1$ and
$\mu_2$ with some mean (let's call it the ``grand mean'')
and some standard deviation (let's call it the ``diversity'').
That way, $\mu_1$ and
$\mu_2$ would both be likely to be somewhere around the grand mean, and
they would likely be different by roughly the size of the diversity.
The challenge now seems to be the choice of appropriate values for the grand
mean and the diversity. Fortunately, we don't actually have to! What we can
do instead is apply priors for them instead.

This is our first example of a {\it hierarchical model}. In a hierarchical
model, instead of directly assigning priors to our parameters, we imagine that
we knew the values of some other parameters (called ``hyperparameters''), and
assign our prior for the parameters {\it given} the hyperparameters. Then we
assign a prior for they hyperparameters as well, to complete the model.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Hierarchical prior for the means
# Hyperparameters
grand_mean ~ dnorm(0, 1/1000^2)
log_diversity ~ dunif(-10, 10)
diversity <- exp(log_diversity)

# Prior for the parameters given the hyperparameters
mu1 ~ dnorm(grand_mean, 1/diversity^2)
mu2 ~ dnorm(grand_mean, 1/diversity^2)
\end{minted}

Samples (obtained using JAGS) of the three priors are shown in
Figure~\ref{fig:ttest1}.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.8]{Figures/ttest1.pdf}
\caption{\it The three different priors we are trying for our Bayesian
equivalent of a t-test. The first prior simply asserts a large amount of
prior ignorance about the value of the two parameters $\mu_1$ and $\mu_2$.
The second is similar but applies 50\% probability to the proposition
$\mu_1 = \mu_2$. The third prior does not allow the two parameters to be
exactly equal, but enhances the probability that they are quite similar
in value.\label{fig:ttest1}}
\end{center}
\end{figure}

The posteriors are shown in Figure~\ref{fig:ttest2}.
The inferences are different, as you would expect, and that's entirely down
to the choice of the prior. Any summaries we make will therefore depend on
which prior we want to use.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.8]{Figures/ttest2.pdf}
\caption{\it The posterior distributions, given the widget data,
based on the three different priors for $\mu_1$ and $\mu_2$.\label{fig:ttest2}}
\end{center}
\end{figure}

The original question was whether manufacturer two was better, equal, or worse
than manufacturer one. We can answer that question by calculating the
posterior probabilities of $\mu_1 = \mu_2$, $\mu_1 < \mu_2$, and
$\mu_1 > \mu_2$. The results are shown in Table~\ref{tab:ttest_results}.

\begin{table}
\begin{center}
{\bf Prior Probabilities}:\\
\vspace{0.3cm}
\begin{tabular}{|l|c|c|c|}
\hline
Prior	&	$\mu_1 < \mu_2$	& $\mu_1 = \mu_2$	& $\mu_1 > \mu_2$\\
\hline
1 	&	0.5		&	0		&	0.5\\
2 	&	0.25		&	0.5		&	0.25\\
3 	&	0.5		&	0		&	0.5\\
\hline
\end{tabular}\\
\vspace{0.5cm}
{\bf Posterior Probabilities}:\\
\vspace{0.3cm}
\begin{tabular}{|l|c|c|c|}
\hline
Prior	&	$\mu_1 < \mu_2$	& $\mu_1 = \mu_2$	& $\mu_1 > \mu_2$\\
\hline
1 	&	0.945		&	0		&	0.055\\
2 	&	0.491		&	0.430		&	0.079\\
3 	&	0.629		&	0		&	0.372\\
\hline
\end{tabular}
\caption{\it Prior and posterior probabilities for three different hypotheses about
the two manufacturers, based on the models with the three different priors.
As you can see, the conclusions are quite sensitive to the choice of prior in
this case.
\label{tab:ttest_results}}
\end{center}
\end{table}

Remember that Prior 1 did not assign any probability to the possibility of the
two parameters being equal. Therefore, no possible evidence can increase
make the posterior probability nonzero. However, according to this model, there
is quite strong evidence that $\mu_1 < \mu_2$, as the probability changed from
0.5 to 0.946.

Prior 2 did allow the two parameters to be equal, and if we use Prior 2, we
seem to have found very weak evidence that they are not in fact equal. The probability
decreased from 0.5 to 0.424. According to Prior 2, if $\mu_1 \neq \mu_2$, then
$\mu_1 < \mu_2$ is the next most likely scenario. However, Prior 2 has an issue
associated with it. Our prior says that if $\mu_2$ is not equal to $\mu_1$, then
it is likely to be close to $\mu_1$. Exactly how close we expect it to be is
set by the variable {\tt L} in the model.
If we were to make {\tt L} very large, then the data would go from weak evidence
against $\mu_1 = \mu_2$ to strong evidence for it! Why does this happen? Well,
if we increased {\tt L}, the prior probability that $\mu_2$ and $\mu_1$ are
close given that they're different is decreased. Then, the hypothesis that
the $\mu$s are different does not predict our data as well, since our data
looks like the $\mu$s are close together. Since it doesn't predict the data as
well as before, its posterior probability will be lower.
 Some people think this sensitivity to the prior is a
danger of Bayesian inference (if you want, you can do a web search for the
``Jeffreys-Lindley paradox''),
but it is behaving logically: the wider we make
the prior, the lower we make the prior probability that $\mu_1$ and $\mu_2$
are close but not equal, giving the model no choice but to believe that they're
equal. If the results are sensitive to the prior, that's important, and you
should think about the logic of the problem to understand why.

Prior 3 seems like it's what we might want in general. It's often silly to think
two parameters might be {\it exactly} equal. What we really think is that there
is a difference, and it might be very small, or moderate or large.

\section{One Way Anova}
One-way ANOVA can be considered as a generalisation of a t-test to more than
two groups. The question is usually phrased as a test of the hypothesis that
the group means are the same, versus the alternative that there is some difference.
As we saw in the Bayesian ``t-test'', it is possible (using clever tricks) to
make a model that has some prior probability that the group means are equal.
However, this gets more tricky with multiple groups. Therefore we will build our
one-way ANOVA model in a similar way to the ``hierarchical model'' version of the
t-test model. There will be one other major difference, but it is a difference
in the way the model is coded, not a conceptual difference.

In the t-test section our data set was composed of measurements in two groups
and our data list contained two vectors of measurements, called {\tt x1} and
{\tt x2}. The sampling distribution/likelihood part of our JAGS model also
needed two {\tt for} loops, one for each group.
If we have many groups (in the following example we will have four),
it can get awkward having to write all those loops. Therefore, when we develop
our ``one-way ANOVA'' model, we will format the data differently by putting
all measurements into a single vector {\tt x}. To make this work, we'll need
an extra vector in the dataset, which tells us which group each data point
belongs to.

We'll use an example dataset on the masses of starlings (a type of bird).
The masses of some starlings were measured at four locations. We are interested
in the differences between the locations. How similar are they in terms of the
average weight of starlings? Are they basically the same, radically different,
or something in between? A boxplot of the data is shown in
Figure~\ref{fig:starling}, which seems to show substantial differences between
the locations. However, only ten starlings were measured at each location, so
we can't be absolutely sure of this, and our goal is to investigate how
sure we should be.

To solve this problem in a Bayesian way, we will treat it as a parameter
estimation problem with four $\mu$ parameters, one for each of the locations.
We will also need at least one parameter describing the standard deviation
of the starling masses at each location. For convenience we'll assume that's
the same across all locations, but it is straightforward to relax this
assumption later.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/starling.pdf}
\caption{\it The masses of starlings as measured at four different locations.
It seems as though the mean mass varies somewhat according to the location, and
our results will tell us how plausible this is.
\label{fig:starling}}
\end{center}
\end{figure}

\subsection{Hierarchical Model}
Our ``one-way ANOVA'' model is very much the same as our final ``t-test'' model,
except for the format of the dataset.
The main advantage of this model is that it generalises to more than two groups
in a very straightforward way; we no longer need to write separate for loops
for each group. As with the third ``t-test'' model, we are not seriously
considering the hypothesis that all of the group means (i.e. the $\mu$
parameters) are exactly equal, but we are allowing them to be quite close
together, or quite distinct in value, by using the hierarchical model
structure with the {\tt diversity} parameter.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
model
{
    # Log-uniform prior for the scatter
    log_sigma ~ dunif(-10, 10)
    sigma <- exp(log_sigma)

    # Hierarchical prior for the means
    # Hyperparameters
    grand_mean ~ dnorm(0, 1/1000^2)
    log_diversity ~ dunif(-10, 10)
    diversity <- exp(log_diversity)

    # Parameters
    for(i in 1:N)
    {
      mu[i] ~ dnorm(grand_mean, 1/diversity^2)
    }

    # Sampling distribution/likelihood
    for(i in 1:N)
    {
        x[i] ~ dnorm(mu[group[i]], 1/sigma^2)
    }
}
\end{minted}

After running this model on the starling data, we can plot any results we
wish. In Figure~\ref{fig:trace_starlings}, I have plotted a trace plot of
$\mu_1$, the parameter for the mean weight of starlings at location 1. This
is a healthy trace plot, although there is a strange feature near iteration
1000 which we will discuss in the next section. Figure~\ref{fig:diversity}
shows the posterior distribution for the {\tt log\_diversity} hyperparameter,
which quantifies how different the groups really are. Our prior for this
parameter was U(-10, 10), and the posterior peaks at around 1.5, which
corresponds to {\tt diversity} $\approx$ 4.5, although there is a fair bit of
uncertainty. Notice also the long tail of the posterior on the left hand side.
Although we never allowed the $\mu$s to be exactly the same, we did allow them
to be close (and this corresponds to the diversity being low). The fact that
some posterior samples landed between -10 and 0 suggests there is a small
probability that the differences between groups are very small, despite the
fact that the data doesn't look that way.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/trace_starlings.pdf}
\caption{\it A trace plot of $\mu_1$ from a JAGS run on the starling data. Things
appear to be mixing well, except for an odd feature near iteration 1000.
\label{fig:trace_starlings}}
\end{center}
\end{figure}

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/diversity.pdf}
\caption{\it The posterior distribution for {\tt log\_diversity}.\label{fig:diversity}}
\end{center}
\end{figure}

As usual, we can use our posterior samples to calculate the posterior
probability of any hypothesis that we can think of based on the parameters.
Here are a couple of interesting examples:
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Is mu2 really greater than mu3?
> mean(results$mu[,2] > results$mu[,3])
[1] 0.672
# Is the diversity really less than 1 (log diversity less than 0)?
> mean(results$diversity < 0)
[1] 0.0272
\end{minted}

\subsection{MCMC Efficiency}
The hierarchical ``one-way ANOVA'' model given above works, but a quick look
at the trace plot suggests the mixing (how easily the MCMC algorithm is
able to move around) did have some difficulties
(see Figure~\ref{fig:trace_starlings}). In this particular example the problem
wasn't fatal, but this problem could be more severe with a different data set.
In some models, it makes sense to
consider the {\it parameterisation} of the model. There are actually different
ways to implement exactly the same model assumptions, but in a way that helps
the efficiency of the MCMC sampling. This is done by changing which parameters
are defined by ``{\verb|~|}'' and which are defined by ``{\tt <-}'', in a way
that keeps the meaning of the model intact, but forces JAGS to do the
exploration differently.

Let's look at a small subset of the above model: just the hierarchical prior
for the $\mu$s. Here it is:
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Hierarchical prior for the means
# Hyperparameters
grand_mean ~ dnorm(0, 1/1000^2)
log_diversity ~ dunif(-10, 10)
diversity <- exp(log_diversity)

# Parameters
for(i in 1:N)
{
  mu[i] ~ dnorm(grand_mean, 1/diversity^2)
}
\end{minted}

To understand why this causes problems, we need to understand a little about
how JAGS works internally. JAGS uses two main MCMC methods, known as
{\it Gibbs Sampling} and {\it Slice Sampling}. Both of these methods usually
work by updating a single parameter or hyperparameter at a time, while keeping
all of the others fixed. Because JAGS is sampling the posterior, each
parameter will tend to move about as far as it can without the new value
becoming inconsistent with the data or the (joint) prior distribution. But
the above model doesn't just have problems exploring the posterior efficiently,
but would also have problems exploring the prior!

For example, if the current values of {\tt grand\_mean} and {\tt diversity}
are 50 and 5, and JAGS is moving the parameter {\tt mu[3]}, it will probably
move it to somewhere within the range 50 $\pm$ 5, roughly speaking, since
the prior for {\tt mu[3]} given {\tt grand\_mean}=50 and {\tt diversity}=5
is Normal($50, 5^2$). But another possibility that is (speaking loosely again)
compatible with the prior is to have {\tt grand\_mean}=-1500,
{\tt diversity}=10000, and {\tt mu[3]}=5600. How would the sampler move from
having {\tt mu[3]}=5 to having {\tt mu[3]}=5600? It certainly couldn't do this
while {\tt diversity} was still 5. Somehow, {\tt diversity} would have to be
much greater than 5. Yet when the sampler tries to increase the value of
{\tt diversity}, it won't be able to move very far, because that would make
it inconsistent with the values of the other {\tt mu} parameters!

Many MCMC methods (and importantly for us, the ones used by JAGS)
are inefficient when the posterior distribution has strong
{\it dependence} between different parameters. Unfortunately, in our one-way
ANOVA model, it's not just the posterior that has strong dependence, but
even the prior has strong dependence!

\subsection{An Alternative Parameterisation}
We will now look at an alternative way of implementing the hierarchical model,
that entails exactly the same assumptions (the same prior distributions and
sampling distribution), yet has computational advantages.
The alternative parameterisation is given below.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Hierarchical prior for the means
# Hyperparameters
grand_mean ~ dnorm(0, 1/1000^2)
log_diversity ~ dunif(-10, 10)
diversity <- exp(log_diversity)

# Parameters
for(i in 1:N)
{
  n[i] ~ dnorm(0, 1)
  mu[i] <- grand_mean + diversity*n[i]
}
\end{minted}
The only difference between this implementation and the original is the part
within the loop. Instead of defining the prior for the $\mu$s directly, we
have defined different parameters called {\tt n}, with standard normal priors.
We then compute the {\tt mu}s deterministically from the {\tt n}s. In this
alternative parameterisation, the prior for the {\tt n}s is completely
independent of {\tt grand\_mean} and {\tt diversity}, so sampling from the
prior would be extremely efficient, yet the implied prior for the {\tt mu}s
is exactly the same as before. Of course, the posterior (what we actually want
to sample) will still probably have dependence, but hopefully less.

Running this new version of the model on the starling data gives the trace
plot in Figure~\ref{fig:trace_starlings2}, which doesn't have any strange
features.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/trace_starlings2.pdf}
\caption{A trace plot of the parameter $\mu_1$ using the revised model.
\label{fig:trace_starlings2}}
\end{center}
\end{figure}


\subsection{Hilbert space}

A complete space with an inner product. That is, a Banach space where the norm is derived from an inner product.


\documentclass[12pt]{article}

\usepackage[margin=0.75in, top=1in, bottom=1in, a4paper]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{indentfirst}

\newcommand{\mo}[1]{\lvert #1 \rvert}
\newcommand{\mos}[1]{\lvert #1 \rvert^2}
\newcommand{\mov}[1]{\lvert \vec{#1} \rvert}
\newcommand{\RR}{\mathbb{R}}
\newcommand{\p}{\partial}
\newcommand{\iv}[1]{\langle #1 \rangle}
\newcommand{\adj}{\text{adj}}
\newcommand{\pmax}{\text{pmax}}
\newcommand*\diff{\mathop{}\!\mathrm{d}}

\theoremstyle{definition}
\newtheorem{prop}{Proposition}[section]
\newtheorem{prob}[prop]{Problem}
\newtheorem{ex}{Exercise}

\title{\vspace{-2.0cm}Convex Optimization Homework 1}
\author{Linxuan Ma}

\begin{document}
	\maketitle

	\section{Convex Sets}

	\begin{prop}
		A polyhedron $\{x \in \RR^n : Ax \leq b\}$ for $A \in \RR^{m \times n}$ and $b \in \RR^m$ is both convex and closed.
	\end{prop}
	\begin{proof}
		The polyhedron $P = \{x \in \RR^n : Ax \leq b\}$ can be described as the intersection of $n(P)$ half-spaces, denoted by set $H$:
		\begin{gather*}
			A_{1,1}x_1 + A_{1,2}x_2 + \dots + A_{1,n}x_n \leq  b_1 \\
			\dots \\
			A_{m,1}x_1 + A_{m,2}x_2 + \dots + A_{m,n}x_n \leq  b_m
		\end{gather*}

		$\forall a, b \in P$, both $a$ and $b$ are in all of the above half-spaces (by definition of intersection $P \in H_i$ for all $i$), and the line connecting them can be obtained via their convex combination $z(t) = tx + (1-t)y$ for $t \in [0, 1]$. Half-spaces are convex, and therefore for any half-space $H$, $x, y \in H \Rightarrow z(t) \in H$. Since the above is satisfied for all above half-spaces,  $z(t) \in P$ (definition of intersection). Therefore, $P$ satisfies that $x, y \in P \Rightarrow tx + (1-t)y \in P$, proving that $P$ is indeed convex.

		The closeness of $P$ can be proven by considering the open set complement $\overline{H_i}$ of every half-space $H_i$. $\bigcup_i \overline{H_i}$ is an open set, and $P = \bigcup_i H_i$ is its complement. Therefore $P$ is a closed set (definition of closeness).
	\end{proof}

	\begin{prop}
		Given a set of convex sets $S$, the intersection $\bigcap_i S_i$ is convex. Similarly, given a set of closed sets $S$, the intersection $\bigcap_i S_i$ is closed.
	\end{prop}

	\begin{proof}
		The intersection $P = \bigcap_i S_i$ is a subset of every $S_i$ (definition of intersection). For all $x, y \in P, t \in [0, 1]$, $tx + (1-t)y \in S_i$ for all $i$ (definition of convex set). The line $tx + (1-t)y$ is contained in every $S_i$, therefore it is contained in the intersection $P$ of all $S_i$ (definition of intersection).

		(Same as \textit{1.1}) The closeness of $P$ can be proven by considering the open set complement $\overline{H_i}$ of every half-space $H_i$. $\bigcup_i \overline{H_i}$ is an open set, and $P = \bigcup_i H_i$ is its complement. Therefore $P$ is a closed set (definition of closeness).
	\end{proof}

	\begin{prob}
		Give an example of a closed set in $\RR^2$ whose convex set is not closed.
	\end{prob}

	\begin{proof}
		The set of $(x, y)$ satisfying
		\begin{gather*}
			y \geq \frac{1}{x^2 + c}
		\end{gather*}
		where $c \in \RR_+$ is a closed set, while its convex hull is any $(x_c, y_c)$ satisfying $y > 0$ (an open set).
	\end{proof}

	\begin{prop}
		Consider matrix $A \in \RR^{m \times n}$. If set $S \subseteq \RR^m$ is convex, then its pre-image of $S$ under $A$ is also convex. The same applies for closeness.
	\end{prop}

	\begin{proof}
		Let $P$ be the pre-image of $S$ under $A$. Consider points $x, y \in P$ and the image of their convex combination under $A$:
		\begin{align*}
			&A(tx + (1 - t)y) \\
			\iff &A(tx) + A((1 - t)y) \\
			\iff &t*Ax + (1-t) * Ay
		\end{align*}

		$t*Ax + (1-t) * Ay \in S$ (definition of convexity) as $Ax$ and $Ay$ are both contained in $S$ (definition of pre-image). Therefore, $A(tx + (1 - t)y) \in S \Rightarrow tx + (1 - t)y \in P$.
	\end{proof}

	\begin{prop}
		Consider matrix $A \in \RR^{m \times n}$. If set $S \subseteq \RR^n$ is convex, then its image under $A$ is also convex.
	\end{prop}

	\begin{proof}
		Consider points $x, y \in A(S)$ and some points $x_p, y_p \in S$ such that $A(x_p) = x$, $A(y_p) = y$. Since $S$ is convex, then the convex combination of $x$ and $y$ can be is equivalent to:
		\begin{align*}
			&tx + (1 - t)y \\
			\iff &t * A(x_p) + (1 - t) * A(y_p) \\
			\iff &A(tx_p) + A((1 - t)y_p) \\
			\iff &A(tx_p + (1 - t)y_p)
		\end{align*}

		Since $S$ is convex, $tx_p + (1 - t)y_p \in S$. Therefore, $A(tx_p + (1 - t)y_p) \in A(S)$, and equivalently, $tx + (1 - t)y \in A(S)$.
	\end{proof}

	\begin{prop}
		Given an example of a matrix $A \in \RR^{m \times n}$ and a set $S \subseteq \RR^n$ that is closed and convex but such that $A(S)$ is not closed.
	\end{prop}

	\begin{proof}
		Consider the matrix $A: \RR^2 \to \RR^2$ that discards the $x$-component:
		\begin{equation*}
		\begin{bmatrix}
			0&0\\0&1
		\end{bmatrix}
		\end{equation*}
	\end{proof}

	The image of the set $S = \{(x, y):y \geq 0.5^x\}$ is both closed and convex, yet its image under $A$ is $0 \times (0, \infty]$, which is not closed.

\end{document}


\subsection{Bounds for Data Types} \label{sec:overview:data}

Bounded refinements are also very useful in the first-order
setting, for example, when working with data types.

\subsubsection*{Abstracting Refinements over Data}

Lets start by abstracting refinements over data
definitions~\cite{vazou13}. Consider the @List@ type
abstractly refined with @p :: a -> a -> Prop@, a binary relation
between two values of type @a@:
%
\begin{code}
    data List <p> a
      = [] | (:) {h :: a, t :: List<p> a<p h>}
\end{code}
%
Intuitively, the definition states that each "tail" is a list of
elements that are @p@-related to the ``head'' @h@. That is,
in a list $[x_1,\ldots,x_n]$ for each $1 \leq i < j \leq n$, we
have $(\cc{p}\ x_i\ x_j)$.

\paragraph{Ordered Lists} To see why this abstraction is useful,
observe that we can define concrete refinements:
%
\begin{code}
    inc = \hd v -> hd <= v
\end{code}
%
and use them to specify and verify that sorting routines return
lists in increasing order:
%
\begin{code}
   isort :: List a -> List<inc> a
   isort = foldr insert []

   insert :: a -> List<inc> a -> List<inc> a
   insert =  -- elided for brevity
\end{code}

\paragraph{QuickSort} However, when we try to verify:
%
\begin{code}
    qsort        :: List a -> List<inc> a
    qsort []     = []
    qsort (x:xs) = qsort ls ++ x : qsort rs
      where
        ls       = [y | y <- xs, y <= x]
        rs       = [z | z <- xs, x <  z]
\end{code}
%
we run into a surprising problem: how can we type the \emph{append}
function @(++)@ in a way that lets us prove that the concatenation
above preserves order?

\subsubsection*{Appending Lists}

Actually, even talking about order is presumes that we are interested
in a particular instantiation for the @List@ refinement. How can we
generically ensure that some abstract refinement @p@? Here's how; we
define a bound:
%
\begin{code}
    bound Meet p q r = \x1 x2 ->
      q x1 => r x2 => p x1 x2
\end{code}
%
that states that if two values @x1@ and @x2@ respectively satisfy
the (unary) properties @q x1@ and @r x2@ then they satisfy the
binary property @p x1 x2@. We can now use this to give append a
type that preserves the abstract binary relation @r@:
%
\begin{code}
(++) :: (Meet p q r) =>
          List<p> a<q> -> List<p> a<r> -> List<p> a
[]     ++ ys = ys
(x:xs) ++ ys = x : xs ++ ys
\end{code}

\paragraph{To verify \cc{qsort}} \toolname automatically instantiates the
refinements at the call to @(++)@ as:
%
\begin{code}
    p |-> \hd v -> hd <= v      -- inc
    q |-> \v -> v <= x
    r |-> \v -> x < v
\end{code}
%
This instantiation is permitted as upon instantiation, the bound
yields the VC:
%
\begin{code}
    x1 <= x => x < x2 => x1 <= x2
\end{code}
%
which is easily validated by the SMT solver, thereby verifying that
the concatenation and hence @qsort@ produces increasingly ordered
lists of type @List<inc> a@.

\subsubsection*{Reversing Lists}

As a final example, consider the tail recursive list @reverse@ function:
%
\begin{code}
reverse []          = []
reverse (x:xs)      = go x [] xs
  where
    go x acc []     = x : acc
    go x acc (y:xs) = go y (x : acc) xs
\end{code}

As with append, we would like to assign @reverse@ a
refinement-generic type that does not bake order into the
signature. The natural specification is that if the input was a
@List<p> a@ then the output must be a @List<q> a@ where the
binary relation @q@ is the inverse of @p@.

\paragraph{We can specify inversion} via a bound:
%
\begin{code}
    bound Inverse p q = \x y -> p x y => q y x
\end{code}
%
and use it to type
%
\begin{code}
    reverse :: (Inverse p q)
            => List<p> a -> List<q> a
\end{code}

\paragraph{We can use \cc{reverse}} to specify and verify that @decsort@
returns lists sorted in decreasing order:
%
\begin{code}
    decsort :: List a -> List<dec> a
    decsort = reverse `compose` qsort
\end{code}
%
where @dec = \hd v -> hd >= v@. Verification proceeds by
by automatically instantiating @p@ and @q@ with the (valid)
concrete refinements:
%
\begin{code}
    p |-> \hd v -> hd <= v
    q |-> \hd v -> v  <= hd
\end{code}

%% -- inferring
%% go :: x:a -> List<q> a<q x> -> List<p> a<p x>
%%    -> List<q> a
%% \end{code}


\lab{Applications}{Correlation and Covariance}{Correlation and Covariance}
\label{Stats1}

\objective{This section addresses applications of inner product spaces to topics in statistics.}

% Source of the datasets: http://people.sc.fsu.edu/~jburkardt/datasets/regression/regression.html
% under the GNU LGPL liscence

\section*{Shifting Data by the Mean}

Consider the table below representing students scores in a class.\\

\begin{figure}[h!]
\begin{center}
\begin{tabular}{|c|r|r|r|r|}
	\hline
Student & Homework & Exam 1  & Exam 2 & Final \\
\hline
S1  & 89 & 91 & 77 & 75 \\
S2  & 67 & 72 & 76 & 66 \\
S3  & 72 & 77 & 69 & 70 \\
S4  & 56 & 60 & 55 & 61 \\
S5  & 92 & 98 & 89 & 86 \\
S6  & 83 & 88 & 90 & 84 \\
S7  & 45 & 60 & 55 & 48 \\
\hline
Average  & 72 & 78 & 73 & 70\\
\hline
\end{tabular}\\
\end{center}
\end{figure}

We can shift our data set by subtracting each column by its average value.  This makes it so that the average of each column in the matrix below is zero.  If $W$ represents the matrix of scores, the following Python command will subtract out the average.
\begin{lstlisting}
>>> W = np.array([[89, 91, 77, 75],
                  [67, 72, 76, 66],
                  [72, 77, 69, 70],
                  [56, 60, 55, 61],
                  [92, 98, 89, 86],
                  [83, 88, 90, 84],
                  [45, 60, 55, 48]])
>>> X = W - W.mean(axis=0)
>>> X
array([[ 17.,  13.,   4.,   5.],
       [ -5.,  -6.,   3.,  -4.],
       [  0.,  -1.,  -4.,   0.],
       [-16., -18., -18.,  -9.],
       [ 20.,  20.,  16.,  16.],
       [ 11.,  10.,  17.,  14.],
       [-27., -18., -18., -22.]])
\end{lstlisting}

Once we have subtracted out the mean from a vector, it is a simple task to compute its variance and standard deviation.
The \emph{variance} of an $n$-dimensional vector $\mathbf{v}$, often denoted $\sigma^2$, is defined by the formula
$$
\sigma^2 = \frac{1}{n}\displaystyle\sum_{i=1}^n (v_i - \mu)^2,
$$
where $\mu$ is the mean of $\mathbf{v}$. The \emph{standard deviation} of $\mathbf{v}$ is simply $\sigma$, i.e. the square
root of the variance. These quantities measure the spread of the entries of the vector. When all of the entries are clustered
closely around the mean, the variance is small. See Figure \ref{fig:variance} for an illustration of variance and mean-shifted data.

(Note: \emph{unbiased variance} is a quantity closely related to variance, but defined slightly differently. It comes into play
in particular for small datasets or vectors. Further discussion is beyond the scope of this lab, but be aware that there is
more than one type of variance.)

\begin{figure}[h]
\includegraphics[width=\textwidth]{variance}
\caption{On the left, the red values come from a vector with relatively high variance,
         and the blue values come from a vector with relatively low variance.
         On the right, the green values have been shifted by the mean from the blue values.}
\label{fig:variance}
\end{figure}

Calculating the variance and the standard deviation in Python is straight forward.
Given the shifted data $X$, we simply square each entry, sum along the columns, and divide by the number of rows
to obtain the variance. Take the square root of the result to get the standard deviation.
\begin{lstlisting}
>>> var = (X**2).sum(axis=0)/X.shape[0]
>>> var
array([ 260.     ,  193.42857,  176.28571,  151.14285])
>>> std = np.sqrt(var)
>>> std
array([ 16.124515,  13.907860,  13.277263,  12.294017])
\end{lstlisting}
Observe that the scores for the homework exhibited the largest amount of spread.

As with many things in Python, there is an even shorter, more convenient way to calculate these quantities.
We may calculate the variance and standard deviation of the columns directly from $W$, without having to shift
by the mean.

\begin{lstlisting}
>>> var = W.var(axis = 0)
>>> var
array([ 260.     ,  193.42857,  176.28571,  151.14285])
>>> std = W.std(axis = 0)
>>> std
array([ 16.124515,  13.907860,  13.277263,  12.294017])
\end{lstlisting}

\begin{problem}
\label{prob:shiftdata}
Import the dataset contained in the \li{weight_age_fat.txt} file. The first row is a header, and contains the names
of each column, but no actual data. There are five columns, but the first two do not contain any data of interest.
The last three columns contain data on the weight (in kilograms), age (in years), and blood fat content of 25
individuals. Distinct data entries are separated by whitespace.

Write a function \li{shiftByMean} that shifts the columns of an input array by their respective means, and returns the result.
Next, write a function \li{computeVariance} that calculates and returns the variance of each column of an input array.
Finally, write a function \li{reportStDev} that accepts no parameters and returns nothing, and simply contains a print
statement that prints out the name of the column with the smallest standard deviation as well as the numerical value of
its standard deviation.

\end{problem}

\section*{The Inner Product and Angles Formula}

Inner products give information about lengths of vectors and angles between vectors.
Recall that the standard inner product on $\mathbb{R}^n$ between vectors $x$ and $y$ is given by
$$
\ipt{v}{u} = v^T u = \displaystyle\sum_{i=1}^n v_iu_i.
$$
We can take advantage of convenient syntax in NumPy to quickly calculate the inner product between two vectors:
\begin{lstlisting}
>>> v = np.array([1., -2., 4.])
>>> u = np.arary([2., 3., -1.])
>>> ( v* u).sum()
-8.0
\end{lstlisting}
The 2-norm of a vector can be easily recovered from the inner product:
\begin{lstlisting}
>>> v_norm = np.sqrt((v * v).sum())
>>> v_norm
4.5825756949558398
\end{lstlisting}
Another option to calculate the norm of an array is to use the \li{scipy.linalg.norm} function, which has the
capability of computing a variety of different types of norms. For the purposes of this lab, however, our own
approach is perfectly sufficient (and even marginally faster).

Recall that the angle $\theta$ between two nonzero
vectors is given by
\[
\cos{\theta} = \frac{\ipt{v}{u}}{\norm{v}\norm{u}}
\]
where $\norm{v} = \sqrt{\ipt{v}{v}}$ denotes the 2-norm of $x$.
By bringing the constants into the inner product, we see that the angle satisfies
\[
\cos{\theta} = \left\langle\frac{v}{\norm{v}},\frac{u}{\norm{u}}\right\rangle.
\]
Hence, if we view the columns of $X$ as vectors in $\mathbb{R}^7$, we can find the cosine of the angles between these columns by dividing each
by its length, and then computing pairwise inner products. To divide the columns by their respective lengths, we may execute the following
code.
\begin{lstlisting}
>>> Y = X / np.sqrt((X**2).sum(axis = 0))
>>> Y
array([[ 0.39848615,  0.35329218,  0.11386819,  0.15371887],
       [-0.11720181, -0.16305793,  0.08540114, -0.12297509],
       [ 0.        , -0.02717632, -0.11386819,  0.        ],
       [-0.37504578, -0.48917378, -0.51240685, -0.27669396],
       [ 0.46880723,  0.54352643,  0.45547275,  0.49190037],
       [ 0.25784398,  0.27176321,  0.4839398 ,  0.43041282],
       [-0.63288976, -0.48917378, -0.51240685, -0.67636301]])
\end{lstlisting}

Finally, we get the cosines of the angles between columns by computing $Y^T Y$. To justify this calculation, consider
the $(i,j)^{th}$ entry of $Y^T Y$:
\begin{align*}
(Y^T Y)_{i,j} &= \sum_{k=1}^n (Y_{i,k})(Y^T)_{k,j} \\
&= \sum_{k=1}^n Y_{i,k}Y_{j,k} \\
&= \left\langle Y_{col i}, Y_{col j} \right\rangle,
\end{align*}
where $Y_{col i}$ denotes the $i^{th}$ column of $Y$, and $Y_{col j}$ the $j^{th}$ column.
Thus, we may obtain the cosine of the angle between each pair of columns of $X$ by computing $Y^T Y$,
which can be easily done in Python as follows:
\begin{lstlisting}
>>> np.dot(Y.T,Y)
array([[ 1.        ,  0.97782999,  0.89014869,  0.94908967],
       [ 0.97782999,  1.        ,  0.90978843,  0.92490144],
       [ 0.89014869,  0.90978843,  1.        ,  0.9276955 ],
       [ 0.94908967,  0.92490144,  0.9276955 ,  1.        ]])
\end{lstlisting}

We remark that the diagonals are always equal to one because the angle between a vector and itself is zero, and the cosine of zero is one.

%It is also worth noting that the matrix resulting from the operation $A \cdot A^T$ is symmetric and positive definite, so it compliant
%with the Cholesky decomposition that was discussed at the end of Lab \ref{lab:LUdecomp}.
% ^^ I'm not totally sure what is going on with this last statement. What exactly is A \cdot A^T? I'm assuming we mean A^T A, in which case
% the matrix is in fact only guaranteed to be positive semi-definite. It still has a Cholesky decomposition in theory, although I think it can only be
% computed for positive definite matrices. Anyway, I don't think the comment is relevant, so I have it removed for now.

\section*{Correlation}
In statistics, \emph{correlation} is a broad term that refers to various types of statistical relationships and dependence between
variables of interest.
In this setting, the \emph{Pearson correlation coefficient} of two vectors $u$ and $v$ is defined to be the cosine of the angle between the
vectors $\overline{u}$ and $\overline{v}$, where $\overline{u}$ is the vector obtained by subtracting the mean of $u$ from each of its entries,
and similarly for $\overline{v}$.
Two vectors are said to be
\begin{itemize}
\item Perfectly correlated if their correlation coefficient is one.
\item Positively correlated if their correlation coefficient is between zero and one.
\item Uncorrelated if their correlation coefficient is zero.
\item Negatively correlated if their correlation coefficient is between negative one and zero.
\item Perfectly anticorrelated if their correlation coefficient is negative one.
\end{itemize}

If we have a whole array with the columns as the vectors of interest, we may calculate a correlation matrix
by first shifting the columns by their mean, and then computing the matrix of angles as described in the
previous section. The $(i,j)$ entry of the resulting correlation matrix gives the correlation coefficient
of columns $i$ and $j$.

\begin{problem}
Write a function \li{corrMatrix} that calculates and returns the correlation matrix of the input array.
\end{problem}

The notion of correlation is important in establishing linear relationships between measurements.
Parallel vectors pointing in the same direction are perfectly correlated, since they lie in the same line.
Orthogonal vectors, on the other hand, are uncorrelated.
Given two vectors of the same length, one can visually check for correlation by viewing a scatter plot.
This can be done in Python as follows:
\begin{lstlisting}
>>> from matplotlib import pyplot as plt
>>> x = np.random.rand(100)
>>> y = np.random.rand(100)
>>> plt.scatter(x,y)
>>> plt.show()
>>> plt.clf()
\end{lstlisting}
You will observe that the scatter plot does not indicate any obvious linear relationship between the two
vectors. You can calculate the correlation coefficient and confirm that it is close to zero.
See Figure \ref{fig:correlation} for examples of correlated and uncorrelated data.

\begin{figure}[h]
\includegraphics[width=\textwidth]{correlation}
\caption{Left: scatter plot of two slightly positively correlated vectors.
         Middle: scatter plot of two uncorrelated vectors.
         Right: scatter plot of two highly negatively correlated vectors.}
\label{fig:correlation}
\end{figure}

\begin{problem}
Import the data contained in the \li{mortality.txt} file. The first 17 rows are headers, providing the names of the
17 columns, and do not contain data. The first column is simply an index column, and may also be omitted.
The following 16 columns provide various demographic and environmental data for 60 countries, with the
final column giving the mortality rate for that country.
Distinct data entries are separated by whitespace.

Write a function that prints the answers to the following three questions, and generates plots as
described below.
Between which pair of distinct columns is the highest correlation?
Which column (apart from the last column) has the most negative correlation coefficient with mortality rate?
Which column is most nearly uncorrelated with mortality rate (i.e. the correlation coefficient is closest to 0)?
Make scatter plots of all three of these pairs of columns, and include them on the same subplot panel.
In all relevant cases, the mortality rate column should be the second argument passed to \li{plt.scatter}.
\end{problem}

It's important to understand that the high correlation between quantities does not necessarily imply
a causal relationship. For example, high correlation between violent crime rates and ice cream sales has
been observed. This does not mean that ice cream causes crime or that increases in crime makes people want
to eat more ice cream. Rather, both rates happen to increase during the summer and decrease during the winter.

Another import consideration is that the correlation coefficient does not provide information about
non-linear relationships and dependencies between data. Any thorough analysis will go well beyond a simple
calculation of the correlation matrix.

\begin{figure}[h]
\includegraphics[width=\textwidth]{nonlinear_dependence}
\caption{There is a clear nonlinear relationship present in this data, but the Pearson correlation
         coefficient, which has a value of -0.00056, fails to capture it.}
\label{fig:correlation}
\end{figure}

Finally, the concepts of variance, standard deviation, correlation, and covariance apply to a much broader class of objects
known as \emph{random variables}, which are central to statistics and probability theory. These topics will be
covered in detail later on.


\section{Markov chain model}
The following Markov chain represents the transition between states of a service
centre
while capturing the interactions between it and a buffer centre.
The service centre accepts two types of individuals; Class 1 and Class 2.
Class 2 individuals are accepted until a pre-determined threshold \(T\) of
individuals is reached.
When reached, all Class 2 individuals that arrive will remain \textit{``blocked''}
in the buffer centre until the number of people in the system is
reduced below \(T\).
Additionally, if the people in the service centre keep rising, they may exceed
the number
of servers \(C\) available, which will in turn mean that every new person will
have to wait for a server to become free.
The states of the Markov chain are denoted by \((u,v)\) where:

\begin{itemize}
    \item \(u\) = number of Class 2 individuals blocked
    \item \(v\) = number of Class 1 individuals in the service centre
\end{itemize}

\begin{figure}
    \centering
    \begin{tikzpicture}[-, node distance = 0.9cm, auto, every node/.style={scale=0.7}]

        % Markov chain variables
        \tikzmath{
            let \initdist = 0.5cm;
            let \altdist = 1.2cm;
            let \minsz = 1.6cm;
        }

        % S_1 and S_2 rectangles
        \tikzmath{
            let \leftOne = -0.8;
            let \rightOne = 2.7;
            let \upOne = 0.8;
            let \downOne = -2.7;
            let \leftTwo = 2.8;
            let \rightTwo = 13;
            let \upTwo = -2.95;
            let \downTwo = -16.4;
        }

        % General case variables
        \tikzmath{
            let \GCsmallx = 8.3;
            let \GCsmally = -9.5;
            let \GCbigx = 4.1;
            let \GCbigy = -11.8;
        }

        % Rectangle for S1
        \draw[ultra thin, dashed] (\leftOne, \downOne) -- (\leftOne, \upOne);
        \draw[ultra thin, dashed] (\leftOne, \upOne) -- (\rightOne, \upOne);
        \draw[ultra thin, dashed] (\rightOne, \upOne) -- node
        {\Huge{\( \quad S_1 \)}}(\rightOne, \downOne);
        \draw[ultra thin, dashed] (\rightOne, \downOne) -- (\leftOne, \downOne);

        % Rectangle for S2
        \draw[ultra thin, dashed] (\leftTwo, \downTwo) -- node
        {\Huge{\( S_2 \quad \)}}(\leftTwo, \upTwo);
        \draw[ultra thin, dashed] (\leftTwo, \upTwo) -- (\rightTwo, \upTwo);
        \draw[ultra thin, dashed] (\rightTwo, \upTwo) -- (\rightTwo, \downTwo);
        \draw[ultra thin, dashed] (\rightTwo, \downTwo) -- (\leftTwo, \downTwo);

        % Small square of general case
        \draw [thick] (\GCsmallx, \GCsmally) -- node {}
        (\GCsmallx + 0.4, \GCsmally);
        \draw [thick] (\GCsmallx + 0.4, \GCsmally) -- node {}
        (\GCsmallx + 0.4, \GCsmally - 0.4);
        \draw [thick] (\GCsmallx + 0.4, \GCsmally - 0.4) -- node {}
        (\GCsmallx, \GCsmally - 0.4);
        \draw [thick] (\GCsmallx, \GCsmally - 0.4) -- node {}
        (\GCsmallx, \GCsmally);

        % Dashed lines to from small square to big one
        \draw [ultra thin] (\GCsmallx, \GCsmally) -- node {}
        (\GCbigx, \GCbigy);
        \draw [ultra thin] (\GCsmallx + 0.4, \GCsmally) -- node {}
        (\GCbigx + 4, \GCbigy);
        \draw [ultra thin] (\GCsmallx, \GCsmally - 0.4) -- node {} (7, \GCbigy);
        \draw [ultra thin] (\GCsmallx + 0.4, \GCsmally - 0.4) -- node {}
        (\GCbigx + 4, \GCbigy - 4);

        % Big Square of general case
        \draw [ultra thick] (\GCbigx, \GCbigy) -- node {} (\GCbigx + 4, \GCbigy);
        \draw [ultra thick] (\GCbigx + 4, \GCbigy) -- node {}
        (\GCbigx + 4, \GCbigy - 4);
        \draw [ultra thick] (\GCbigx + 4, \GCbigy - 4) -- node {General Case}
        (\GCbigx, \GCbigy - 4);
        \draw [ultra thick] (\GCbigx, \GCbigy - 4) -- node {} (\GCbigx, \GCbigy);

        % First Line
        \node[state, minimum size=1.5cm] (zero) {(0,0)};
        \node[state, node distance = \initdist, minimum size=\minsz, below right=of zero]
        (one) {(0,1)};
        \node[draw=none, node distance = \initdist, minimum size=\minsz, below right=of one]
        (two) {\textbf{\( \ddots \)}};
        \node[state, node distance = \initdist, minimum size=\minsz, below right=of two]
        (three) {(0,T)};
        \node[state, node distance = \altdist, minimum size=\minsz, right=of three]
        (four) {(0,T+1)};
        \node[draw=none, node distance = \altdist, minimum size=\minsz, right=of four]
        (five) {\textbf{\dots}};
        \node[state, minimum size=\minsz, right=of five] (six) {(0,C)};
        \node[draw=none, minimum size=\minsz, right=of six] (seven) {\textbf{\dots}};

        % Second Line
        \node[state, minimum size=\minsz, below=of three] (three_one) {(1,T)};
        \node[state, minimum size=\minsz, below=of four] (four_one) {(1,T+1)};
        \node[draw=none, minimum size=\minsz, below=of five] (five_one) {\textbf{\dots}};
        \node[state, minimum size=\minsz, right=of five_one] (six_one) {(1,C)};
        \node[draw=none, minimum size=\minsz, right=of six_one] (seven_one) {\textbf{\dots}};

        % Third Line
        \node[state, minimum size=\minsz, below=of three_one] (three_two) {(2,T)};
        \node[state, minimum size=\minsz, below=of four_one] (four_two) {(2,T+1)};
        \node[draw=none, minimum size=\minsz, below=of five_one] (five_two)
        {\textbf{\dots}};
        \node[state, minimum size=\minsz, right=of five_two] (six_two) {(2,C)};
        \node[draw=none, minimum size=\minsz, right=of six_two] (seven_two)
        {\textbf{\dots}};

        % Fourth line
        \node[draw=none, node distance = \altdist, minimum size=\minsz, below=of three_two]
        (three_three) {\textbf{\vdots}};
        \node[draw=none, node distance = \altdist, minimum size=\minsz, below=of four_two]
        (four_three) {\textbf{\vdots}};
        \node[draw=none, node distance = 2cm, minimum size=\minsz, below=of five_two]
        (five_three) {};
        \node[draw=none, node distance = \altdist, minimum size=\minsz, below=of six_two]
        (six_three) {\textbf{\vdots}};

        % Fifth line
        % \node[state, node distance = \altdist, minimum size=\minsz, below=of five_three]
        % (general_case_mid) {\( (u_i, v_i) \)};
        \node[draw=none, node distance = 0.3cm, minimum size=\minsz, below=of four_three]
        (general_case_up) {};
        \node[state, node distance = \altdist, minimum size=\minsz, below=of general_case_up]
        (general_case_mid) {\( (u_i, v_i) \)};

        \node[draw=none, node distance = \altdist, minimum size=\minsz, below=of general_case_mid]
        (general_case_down) {};
        \node[draw=none, node distance = \altdist, minimum size=\minsz, left=of general_case_mid]
        (general_case_left) {};
        \node[draw=none, node distance = \altdist, minimum size=\minsz, right=of general_case_mid]
        (general_case_right) {};

        \draw[every loop]
            % First Horizontal Edges
            (zero) edge[bend left] node {\( \Lambda \)} (one)
            (one) edge[bend left] node {\( \mu \)} (zero)
            (one) edge[bend left] node {\( \Lambda \)} (two)
            (two) edge[bend left] node {\( 2 \mu \)} (one)
            (two) edge[bend left] node {\( \Lambda \)} (three)
            (three) edge[bend left] node {\( T \mu \)} (two)
            (three) edge[bend left] node {\( \lambda_1 \)} (four)
            (four) edge[bend left] node {\( (T+1) \mu \)} (three)
            (four) edge[bend left] node {\( \lambda_1 \)} (five)
            (five) edge[bend left] node {\( (T+2) \mu \)} (four)
            (five) edge[bend left] node {\( \lambda_1 \)} (six)
            (six) edge[bend left] node {\( C\mu \)} (five)
            (six) edge[bend left] node {\( \lambda_1 \)} (seven)
            (seven) edge[bend left] node {\( C\mu \)} (six)

            % Second Horizontal Edges
            (three_one) edge[bend left] node {\( \lambda_1 \)} (four_one)
            (four_one) edge[bend left] node {\( (T+1) \mu \)} (three_one)
            (four_one) edge[bend left] node {\( \lambda_1 \)} (five_one)
            (five_one) edge[bend left] node {\( (T+2) \mu \)} (four_one)
            (five_one) edge[bend left] node {\( \lambda_1 \)} (six_one)
            (six_one) edge[bend left] node {\( C\mu \)} (five_one)
            (six_one) edge[bend left] node {\( \lambda_1 \)} (seven_one)
            (seven_one) edge[bend left] node {\( C\mu \)} (six_one)

            % Third Horizontal Edges
            (three_two) edge[bend left] node {\( \lambda_1 \)} (four_two)
            (four_two) edge[bend left] node [below] {\( (T+1) \mu \)} (three_two)
            (four_two) edge[bend left] node {\( \lambda_1 \)} (five_two)
            (five_two) edge[bend left] node {\( (T+2) \mu \)} (four_two)
            (five_two) edge[bend left] node {\( \lambda_1 \)} (six_two)
            (six_two) edge[bend left] node {\( C\mu \)} (five_two)
            (six_two) edge[bend left] node {\( \lambda_1 \)} (seven_two)
            (seven_two) edge[bend left] node {\( C\mu \)} (six_two)

            % First Vertical Edges
            (three) edge[bend left] node {\( \lambda_2 \)} (three_one)
            (three_one) edge[bend left] node {\( T \mu \)} (three)
            (three_one) edge[bend left] node {\( \lambda_2 \)} (three_two)
            (three_two) edge[bend left] node {\( T\mu \)} (three_one)
            (three_two) edge[bend left] node {\( \lambda_2 \)} (three_three)
            (three_three) edge[bend left] node {\( T\mu \)} (three_two)

            % Second Vertical Edges
            (four) edge node {\( \lambda_2 \)} (four_one)
            (four_one) edge node {\( \lambda_2 \)} (four_two)
            (four_two) edge node {\( \lambda_2 \)} (four_three)

            % Fourth Vertical Edges
            (six) edge node {\( \lambda_2 \)} (six_one)
            (six_one) edge node {\( \lambda_2 \)} (six_two)
            (six_two) edge node {\( \lambda_2 \)} (six_three)

            % General Case
            (general_case_left) edge[bend left] node {\( \lambda_1 \)} (general_case_mid)
            (general_case_mid) edge[bend left] node {\( v_i \mu \)} (general_case_left)
            (general_case_right) edge[bend left] node {\( (v_i +1) \mu \)} (general_case_mid)
            (general_case_mid) edge[bend left] node {\( \lambda_1 \)} (general_case_right)
            % (five_three) edge node {\( \lambda_2 \)} (general_case_mid)
            (general_case_up) edge node {\( \lambda_2 \)} (general_case_mid)
            (general_case_mid) edge node {\( \lambda_2 \)} (general_case_down)
            ;
    \end{tikzpicture}
    \caption{Markov chain}
    \label{markov_model}
\end{figure}

\subsection{Markov-chain state mapping function}
The transition matrix of the Markov-chain representation described above can be
denoted by a state mapping function.
The state space of this function is defined as:

\begin{align}
    S(T) =& S_1(T) \cup S_2(T) \text{ where:} \nonumber \\
    S_1(T) =& \left\{(0, v)\in\mathbb{N}_0^2 \; | \; v < T \right\} \label{eq:state_space} \\
    S_2(T) =& \{(u, v)\in\mathbb{N}_0^2 \; | \; v \geq T \} \nonumber
\end{align}

Therefore, the entries of the transition matrix \(Q\), can be given by
\( q_{i,j} = q_{(u_i, v_i),(u_j, v_j)} \) which is the transition rate from state
\( i = (u_i, v_i) \) to state \( j = (u_j , v_j) \) for all
\( (u_i, v_i), (u_j, v_j) \in S \).

\begin{equation} \label{eq:markov_transition_rate}
    q_{i, j} =
    \begin{cases}
        \Lambda, & \textbf{if } (u_i, v_i) - (u_j, v_j) = (0,-1) \textbf{ and }
        v_i < \text{t} \\
        \lambda_1, & \textbf{if } (u_i, v_i) - (u_j, v_j) = (0,-1) \textbf{ and }
        v_i \geq \text{t} \\
        \lambda_2, & \textbf{if } (u_i, v_i) - (u_j, v_j) = (-1,0) \\
        v_i \mu, & \textbf{if } (u_i, v_i) - (u_j, v_j) = (0,1) \textbf{ and }
        v_i \leq C \textbf{ or} \\ & \hspace{0.37cm}(u_i, v_i) - (u_j, v_j) = (1,0)
        \textbf{ and } v_i = T \leq C \\
        C \mu, & \textbf{if } (u_i, v_i) - (u_j, v_j) = (0,1) \textbf{ and } v_i > C
        \textbf{ or} \\ & \hspace{0.37cm}(u_i, v_i) - (u_j, v_j) = (1,0) \textbf{ and }
        v_i = T > C\\
        -\sum_{j=1}^{|Q|}{q_{i,j}} & \textbf{if } i = j \\
        0, & \textbf{otherwise}
    \end{cases}
\end{equation}

In order to acquire an exact solution of the problem a slight adjustment needs to
be considered.
The problem defined above assumes no upper boundary to the number of individuals
that can wait for service or the ones that are blocked in the buffer centre.
Therefore, a different state space \( \tilde S \) needs to be constructed where
\( \tilde S \subseteq S \) and there is a maximum allowed number of people \( N \)
that can be in the system and a maximum allowed number of people \( M \) that can
be blocked in the buffer centre:

\begin{equation}
    \tilde S = \left\{ (u, v) \in S\;| u \leq M, v\leq N \right\}
\end{equation}

\subsection{Steady State}
Having calculated the transition matrix \( Q \) for a given set of parameters the
probability vector \( \pi \) needs to be considered.
The vector \( \pi \) is commonly used to study such stochastic systems and it's
main purpose is to keep track of the probability of being at any given state of
the system.
The term \textit{steady state} refers to the instance of the vector \( \pi \) where
the probabilities of being at any state become stable over time.
Thus, by considering the steady state vector \( \pi \) the relationship between
it and \(Q \) is given by:

\[
\frac{d\pi}{dt} = \pi Q = 0
\]

There are numerous methods that can be used to solve problems of such kind.
In this paper only numeric and algebraic approaches will be considered.

\subsubsection{Numeric integration}
The first approach to be considered is to solve the differential equation numerically
by observing the behaviour of the model over time.
The solution is obtained via python's SciPy library.
The functions odeint and solve\textunderscore ivp have been used in order to find
a solution to the problem.
Both of these functions can be used to solve any system of first order ODEs.

\subsubsection{Linear algebraic approach}
Another approach to be considered is the linear algebraic method.
The steady state vector can be found algebraically by satisfying the following set
of equations:
\[ \pi Q = 0 \]
\[ \sum_{i} \pi_i = 1 \]

These equations can be solved by slightly altering \( Q \) such that the final column
is replaced by a vector of ones.
Thus, the resultant solution occurs from solving the equation \( \tilde{Q}^T \pi = b \)
where \( \tilde{Q} \) and \( b \) are defined as:

\[
\tilde{q_{i, j}} =
\begin{cases}
    1, & \textbf{if } j = |Q| \\
    q_{i,j}, & \textbf{otherwise}
\end{cases}
\]

\[
b =
\begin{bmatrix}
    x_{1} \\
    x_{2} \\
    \vdots \\
    x_{m}
\end{bmatrix}
\]

\subsubsection{Least Squares approach}
Finally, the last approach to be considered is the least squares method.
This approach is considered because while the problem becomes more complex (in terms
of input parameters) the computational time required to solve it increases exponentially.
Thus, one may obtain the steady state vector \( \pi \) by solving the following
equation.

\[
\pi = \text{argmin}_{x\in\mathbb{R}^{d}}\|Mx-b\|_2^2
\]


\section{Fourier Spectral Methods}

	In the following sections we will work with the problem obtained by the transformation of the problem (\ref{Hopf_tranform}) given by
	\begin{align}
		u(x, t) = - 2 \alpha \frac{\partial_{x} \varphi(x, t)}{\varphi(x, t)} = - 2 \alpha \left( \log{\varphi(x, t)} \right)_x
	\end{align}
	with $\alpha > 0$, and $\varphi$ solves the following initial value problem
	\begin{align}
	\label{Bugers_Lineal}
		\left \lbrace \begin{array}{ll}
			\frac{\partial \varphi}{\partial t} = \alpha \varphi_{xx}, \hspace{2mm} 0 < t \leq T, \hspace{2mm} x \in I \\
			\\
			\varphi(x, 0) = \displaystyle \varphi_0 (x) = e^{- \int_{0}^{x} \frac{u_0(y)}{2 \alpha} dy}, \hspace{2mm} x \in I
		\end{array}  \right .
	\end{align}
	\input{burgers_equation/deterministic/methods/Galerkin}
	\input{burgers_equation/deterministic/methods/Collocation}