\section{Introduction}
When studying intelligence, insects, reptiles, and humans have been found to possess neurons with the capacity to hold integers, real numbers, and perform arithmetic operations \cite{nieder-neuronal-number,rugani-arithmetic-chicks,gallistel-numbers-in-brain}.
In our quest to mimic intelligence we have put much faith in neural networks, which in turn has provided unparalleled and often superhuman performance in tasks requiring high cognitive abilities \cite{natureGo,bert,openai-learning-dexterous}.
However, when using neural networks to learn simple arithmetic problems, such as counting, multiplication, or comparison they systematically fail to extrapolate onto unseen ranges \cite{stillNotSystematic,suzgun2019evaluating,trask-nalu}.
The absence of inductive bias makes it difficult for neural networks to extrapolate well on arithmetic tasks as they lack the underlying logic to represent the required operations.

We would like to achieve a neural network component that can take an arbitrary hidden input, learn to select the appropriate elements, and apply the desired arithmetic operation.
A recent attempt to achieve this goal is the Neural Arithmetic Logic Unit (NALU), by \citet{trask-nalu}.

The NALU consists of two sub-units: the $\text{NAC}_{+}$ for addition/subtraction and the $\text{NAC}_{\bullet}$ for multiplication/division.
The sub-units are softly gated using a sigmoid function in order to exclusively select one of the sub-units.
However, we find that the soft gating mechanism and the $\text{NAC}_{\bullet}$ are fragile and hard to learn.

In this paper, we analyze and improve upon the $\text{NAC}_{+}$ and $\text{NAC}_{\bullet}$ with respect to addition, subtraction, and multiplication.
Our proposed improvements, namely the Neural Addition Unit (NAU) and Neural Multiplication Unit (NMU), are more theoretically founded and improve performance regarding stability, speed of convergence, and interpretability of results.
Most importantly, the NMU can support a large hidden input-size.

The improvements, based on a theoretical analysis of the NALU and its components, are achieved by a simplification of the parameter matrix for a better gradient signal, a sparsity regularizer, and a new multiplication unit that can be optimally initialized and supports both negative and small numbers.
The NMU do not support division.
However, we find that the $\text{NAC}_{\bullet}$ in practice also only supports multiplication and cannot learn division (for theoretical findings on why division is hard to learn, see section \ref{sssec:nac-mul}).

To analyze the impact of each improvement we introduce several variants of the $\text{NAC}_{\bullet}$.
We find that, allowing division makes optimization for multiplication harder, linear and regularized weights improve convergence, and that the NMU style of multiplication is critical when increasing the hidden input size.

Furthermore, we improve upon existing benchmarks in \citet{trask-nalu} by expanding the ``simple function task'', using a multiplicative variant of ``MNIST Counting and Arithmetic Tasks'', and use an improved success-criterion \citet{maep-madsen-johansen-2019}.
A success-criterion is important because the arithmetic layers are solving a logical problem.
We propose the MNIST multiplication variant as we want to test the NMU's and $\text{NAC}_{\bullet}$'s ability to learn from real data.
%Hence, the solution found is either correct or wrong.
%To test this we propose using a success-criteria to evaluate model performance.
%A success-criterion enables measuring sensitivity to the initialization seed as well as the number of iterations until convergence.

\begin{figure}[t]
\centering
\includegraphics[scale=0.7]{graphics/nmu.pdf}
\caption{Visualization of NMU for a single output scalar $z_1$, this construction repeats for every element in the output vector $\mathbf{z}$.}
\end{figure}

\subsection{Learning a 10 parameter function}
Consider the static function $t = (x_1 + x_2) \cdot (x_1 + x_2 + x_3 + x_4)$ for $x \in \mathbb{R}^4$. To illustrate the ability of $\mathrm{NAC}_{\bullet}$, NALU, and our proposed NMU, we conduct 100 experiments for each model, where we attempt to fit this function. Table \ref{tab:very-simple-function-results} shows that NMU has a higher success rate and converge faster.
%When inspecting the $6\%$ that did not converge, we found the issue to be underflow when $w = 0$ in the multiplication layer.
\input{results/simple_mul.tex}


\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{booktabs}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\usepackage{color}
\usepackage{url}

%% display solutions or not
\newif\ifsol
\soltrue % comment out to hide solutions

\title{Section 8: HMMs}
\author{CS 182 - Artificial Intelligence}
\date{}

\begin{document}
\maketitle

\section*{Probability Review}

A {\bf{random variable}} $X$ is a variable which could take on various values with specified probabilities (its distribution). Two random variables $X$ and $Y$ are independent if $p(x, y) = p(x)p(y)$. Then, the probability of an event that depends on both $X$ and $Y$ (for example, the event that $X == 1$ and $Y == 2$) will be given by a value in the {\bf{joint probability distribution}} $p(x, y)$. \\

\noindent The {\bf{marginal probability distribution}} is the probability distribution of a subset of variables from a {\bf{joint probability distribution}}. For example, the marginal probability distribution $p(y)$ can be found by summing across all values of $x$ in $p(x, y)$:
\begin{align*}
p(x) = \sum_{y \in \mathcal{Y}} p(x, y) \\
p(x) = \int_{\mathcal{Y}} p(x,y) dy
\end{align*}

\noindent When we know that an event $B$ has happened, that could influence the probability of another event $A$. The new probability of $A$ given $B$ is the {\bf{conditional probability}} $P(A|B)$. If $B$ changes the distribution of a random variable $X$, we write the new random variable as $X|B$, and the new distribution $P(X|B)$ is the conditional distribution:
$$ P(A | B) = \frac{P(A \;\; \text{and} \;\; B)}{P(B)}$$

\noindent Note that this has some direct implications: for instance, if $A$ is the event $X == x$ and $B$ is the event $Y == y$, we have an expression relating distributions (sometimes called the {\bf{product rule}}):
$$ p(x | y) = \frac{p(x, y)}{p(y)}$$
which implies that
$$ p(x, y) = p(x | y) p(y) = p(y | x) p(x)$$

\noindent Finally, you may have heard of {\bf{Bayes' Theorem}} (also known as {\bf{Bayes' Rule}} or {\bf{Bayes' Law}}), a very prominent statement that relates conditional probabilities between events:
$$ p(x | y) = \frac{p(y | x) p(x)}{p(y)} $$ \\
In machine learning, we are often looking for parameters $\theta$ based on the distribution of data $y$. Then, we interpret $p(\theta)$ as the {\bf{prior}} (the distribution of $\theta$ without knowing about $y$), $p(y)$ as the {\bf{evidence}} (the overall probability of this data without considering our parameters), $p( y | \theta)$ as the {\bf{likelihood}} (how likely are we to collect this data given the parameters?) and $p( \theta | y)$ as the {\bf{posterior}} (the distribution of $\theta$ after knowing about $y$).
\begin{align*}
\underbrace{p(\theta | y)}_{\text{posterior}}
&= \frac{\overbrace{ p(y | \theta) }^{\text{likelihood}}
	\overbrace{ p(\theta) }^{\text{prior}}}
{\underbrace{p(y)}_{\text{evidence}}}
\end{align*}

\section*{Markov Models}
\noindent A Markov model shows the probabilistic transitions of a \textbf{state} $X$. The system must obey the {\bf{Markov assumption}} that the conditional probability distribution of future states of the process (conditional on both past and present states) depends only upon the present state:
\[
P(X_T|X_1, \ldots, X_{T-1}) = P(X_T|X_{T-1}).
\]
And therefore all we need to fully define the model is:
\begin{itemize}
    \item An initial distribution $P(X_0)$
    \item Transition probabilities $P(X_{t}|X_{t-1})$
\end{itemize}
Furthermore, we can express the probability of a sequence of states as
\[
P(X_1, \ldots, X_T) = P(X_1) \prod_{t=2}^T P(X_t|X_{t-1})
\]
Note that as $t$ becomes large, the probabilities $P(X_t)$ will converge to a \textbf{stationary distribution} regardless of the initial distribution $P(X_1)$. We can think of this as uncertainty growing over time. \\

\noindent We can now think of a Markov model as a generalization of an MDP that is embedded in probability distributions over actions. The statement that $P(X_T|X_1, \ldots, X_{T-1}) = P(X_T|X_{T-1})$ is the same as saying that all transitions only depend on the current state over random actions $T(s, a, s')$. Again we have a starting state and potentially a terminal state, or absorbing state. We could also have a Markov Model give off rewards at states although general Markov Models do not need to have rewards.

\section*{Hidden Markov Models}

In Hidden Markov Models (HMMs), you can no longer directly observe the state itself. You can only observe (noisy) emissions $e$ from those states. Unlike in RL, where we were no longer able to know the transition probabilities, here we don't know the actual value of the state at each time. Thus, an HMM is defined by the following:
\begin{enumerate}
\item An initial distribution $P(X_1)$
\item Transition probabilities $P(X_{t}|X_{t-1})$
\item Emission probabilities $P(E_t|X_t)$
\end{enumerate}
We can use those to compute the joint distribution
\[
P(X_1, E_1, \ldots, X_T, E_T) = P(X_1) P(E_1|X_1) \prod_{t=2}^T P(X_{t}|X_{t-1}) P(E_t|X_t)
\]
Finally, we can define the \textbf{belief} at time $t$ as the probability of being in a specific state given all of the evidence seen so far:
\[
B_t(x) = P(X_t=x|e_{1:t}).
\]
We can therefore solve for our belief of the state throughout time (\textbf{filtering}) through the \textbf{forward algorithm} by updating for (1) the passage of time and (2) receiving/observing new evidence:
\begin{enumerate}
\item $P(x_t|e_{1:t-1}) = \sum_{x_{t-1}} P(x_{t-1}|e_{1:t-1}) \cdot P(x_t|x_{t-1})$
\item $P(x_t|e_{1:t}) \propto P(x_t|e_{1:t-1}) \codt P(e_t|x_t)$
\end{enumerate}
These equations can be derived by applying Bayes rule, the chain rule, and conditional independence to the definition of the belief at each time step under the HMM model. There is also a MDP equivalent for HMM which are called POMDPs and these are some of the hardest problems to solve in robotics due to the large state spaces and uncertainty. \textbf{Particle Filters} are designed to attempt to overcome this issue of needing to explicitly write out the belief distribution across all states by instead tracking a series of particles to compute a discrete, sample-based, approximation of the belief distribution. Particles are passed through a transition model for the elapse time step and then weighted based on the emissions seen in the observe step. Particles are then resampled according to this distribution to normalize before the process is repeated.

\clearpage
\section*{Practice Problems}

\begin{enumerate}
    \item Suppose you have a Markov Process defined by the following table:
        \begin{figure}[h]
            \centering
            \includegraphics[width=.5\textwidth]{figs/sunny-foggy}
        \end{figure}

    \begin{enumerate}
        \item Transform the table into a finite state automaton representation.\\
        \ifsol
            \begin{figure}[h]
                \centering
                \includegraphics[width=.45\textwidth]{figs/1-sol}
            \end{figure}
        \else
            \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
        \fi
        \item Given that today is \emph{sunny}, what is the probability that the next two days are first \emph{sunny} and then \emph{rainy}?
            \ifsol
                \textcolor{blue}{\begin{align*}
                P(X_2 = s, X_3 = r | X_1 = s) &= P(X_1=s) * P(X_2=s|X_1=s) * P(X_3=r|X_2=s) \\
                							  &= P(X_2=s|X_1=s) * P(X_3=r|X_2=s) \\
                                              &= 0.05 * 0.8 \\
                                              &= 0.04
                \end{align*}}
            \else
                \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
            \fi
        \item Given that today is foggy, what is the probability that it rains in two days? \\
        \ifsol
            \textcolor{blue}{
            All three possibilities for $X_2$ can lead from $X_1=f$ to $X_3=r$. Therefore, we sum:
            \begin{align*}
            P(X_3=r|X_1=f) &=                     P(X_2=f | X_1=f) * P(X_3=r|X_2=f) +\\
                           &\mathrel{\phantom{=}} P(X_2=r | X_1=f) * P(X_3=r|X_2=r) +\\
                           &\mathrel{\phantom{=}} P(X_2=s | X_1=f) * P(X_3=r|X_2=s) \\
                           &= 0.3 * 0.5 + 0.6*0.3 + 0.05 * 0.2 \\
                           &= 0.34
            \end{align*}}
        \else
            \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
        \fi

        \item Suppose today is foggy weather. What is the probability of all possible weather in two days? \\
        \ifsol
            \textcolor{blue}{\begin{table}[h]
            \centering
            \begin{tabular}{@{}lllll@{}}
            \toprule
              & $X_1$ & $X_2$ & $X_3$                           &  \\ \midrule
            s & 0.0   & 0.2   & 0.2*0.80+0.3*0.2+0.5*0.2 = 0.32  &  \\
            r & 0.0   & 0.3   & 0.2*0.05+0.3*0.6+0.5*0.3 = 0.34 &  \\
            f & 1.0   & 0.5   & 0.2*0.15+0.3*0.2+0.5*0.5 = 0.34 &  \\ \bottomrule
            \end{tabular}
            \end{table}}
        \else
            \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
        \fi
    \end{enumerate}

    \clearpage
    \item Imagine you are a computer scientist and therefore do not leave your basement, ever. Now, the only evidence for the weather is whether the person who brings your daily meal carries an umbrella. You know the following emission probabilities $E$:\\
    \begin{figure}[h]
        \centering
        \includegraphics[width=.4\textwidth]{figs/umbrella}
    \end{figure}

    Yesterday, you were forced to go outside because of a fire drill -- the worst -- and saw that it was sunny. Today, your caregiver has an umbrella on them. What it is the probability that it is raining today?

    \ifsol
        \textcolor{blue}{ We need to marginalize over all the probabilities of seeing an umbrella \begin{align*}
        \frac{p(X_2=r|X_1=s)p(E_2=u|X_2=r)}{p(E_2=u|X_1=s)} &= \frac{p(X_2=r|X_1=s)p(E_2=u|X_2=r)}{\sum_{i \in [s,r,f]} p(E_2=u|X_2=i) p(X_2=i|X_1=s)} \\
            &= \frac{0.05 * 0.8}{0.8*0.1 + 0.05*0.8 + 0.15*0.3} \\
            &\approx 0.2424
        \end{align*}}
    \else
        \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
    \fi

    \item Now consider the following conditional probabilities.
        \begin{figure}[h]
        \centering
        \includegraphics[width=0.8\textwidth]{figs/hmmtask2.png}
        \end{figure}

        \begin{enumerate}
            \item Draw the full HMM model for this set of probabilities:\\
            \ifsol
                \begin{center}
                \begin{tikzpicture}[scale=0.15]
                \tikzstyle{every node}+=[inner sep=0pt]
                \draw [black] (37.7,-7) circle (3);
                \draw (37.7,-7) node {Start};
                \draw [black] (26.6,-17.1) circle (3);
                \draw (26.6,-17.1) node {$0$};
                \draw [black] (48.8,-17.1) circle (3);
                \draw (48.8,-17.1) node {$1$};
                \draw [black] (29.6,-34) circle (3);
                \draw (29.6,-34) node {$A$};
                \draw [black] (46.3,-34) circle (3);
                \draw (46.3,-34) node {$B$};
                \draw [black] (40.137,-5.257) arc (65:22:17);
                \fill [black] (47.8,-13.41) -- (48.4,-14.2) -- (49.0,-13.41);
                \draw (47.137,-9.257) node [right] {$0.7$};
                \draw [black] (35.263,-5.257) arc (-65:-22:-17);
                \fill [black] (26.4,-13.41) -- (26.8,-14.2) -- (27.6,-13.41);
                \draw (28.263,-9.257) node [left] {$0.3$};
                \draw [black] (29.037,-15.357) arc (120.52398:59.47602:17.056);
                \draw (37.7,-12.49) node [above] {$0.6$};
                \draw [black] (46.408,-18.904) arc (-58.17768:-121.82232:16.515);
                \fill [black] (28.99,-18.9) -- (29.41,-19.75) -- (29.94,-18.9);
                \draw (37.7,-21.89) node [below] {$0.8$};
                \draw [black] (50.797,-14.877) arc (165.80141:-122.19859:2.25);
                \fill [black] (46.36,-15.36) -- (45.93,-14.52) -- (45.42,-15.38);
                \draw (55.81,-13.71) node [right] {$0.2$};
                \fill [black] (51.78,-17.33) -- (52.43,-18.01) -- (52.68,-17.04);
                \draw [black] (23.617,-17.277) arc (301.12921:13.12921:2.25);
                \draw (19.64,-13.54) node [left] {$0.4$};
                \fill [black] (24.64,-14.84) -- (24.66,-13.9) -- (23.8,-14.41);
                \draw [black] (26.72,-33.212) arc (-114.93875:-224.92927:8.916);
                \fill [black] (26.72,-33.21) -- (26.21,-32.42) -- (25.78,-33.33);
                \draw (20.98,-26.94) node [left] {$0.9$};
                \draw [black] (51.159,-18.931) arc (42.7293:-59.55867:9.087);
                \fill [black] (49.09,-32.93) -- (50.03,-32.96) -- (49.52,-32.09);
                \draw (54.17,-26.61) node [right] {$0.5$};
                \draw [black] (43.463,-33.029) arc (-111.62882:-149.62165:31.312);
                \fill [black] (43.46,-33.03) -- (42.9,-32.27) -- (42.53,-33.2);
                \draw (29.86,-25.18) node [below] {$0.1$};
                \draw [black] (47.485,-19.795) arc (-28.90492:-68.38608:29.67);
                \fill [black] (32.44,-33.04) -- (33.37,-33.21) -- (33,-32.28);
                \draw (45.88,-25.18) node [below] {$0.5$};
                \end{tikzpicture}
                \end{center}
            \else
                \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
            \fi
            \item Suppose that $O_1 = A$ and $O_2 = B$ is observed. Use the Forward algorithm to compute the probability distribution $p(X2| O1 = A, O2 = B) = B_2(X2)$.\\
            \ifsol
                \begin{tabular}{lll}
                \toprule
                $X_1$   & $Pr(X_1, O_1 = A)$ & Normalized \\ \midrule
                0       & 0.3 * 0.9 = 0.27 &  27/62 \\
                1       & 0.7 * 0.5 = 0.35 &  35/62 \\\bottomrule
                $X_2$   & $Pr(X_2, O_1 = A, O_2 = B)$ & Normalized \\ \midrule
                0       & 0.1 * [0.4 * 27/62 + 0.8 * 35/62] = 97/1550 &  97/387 $\approx$ 0.25 \\
                1       & 0.5 * [0.6 * 27/62 + 0.2 * 35/62] = 29/155 &  290/387 $\approx$ 0.75 \\ \bottomrule
                \end{tabular}
                % \begin{figure}[h]
                % \centering
                % \includegraphics[width=0.5\textwidth]{figs/sol1}
                % \end{figure}
            \else
                \bigskip \bigskip \bigskip
            \fi
        \end{enumerate}
\end{enumerate}
\end{document}


\documentclass{scrartcl}
\usepackage{natbib}
\usepackage{amsmath, amsfonts, amssymb, bbm, graphicx}
\numberwithin{equation}{section}
\bibliographystyle{unsrtnat}

%opening
\title{Comparing Hamiltonian Monte Carlo and Elliptical Slice Sampling for constrained Gaussian distributions}
\subtitle{732A76 Research Project Report}
\author{Bayu Brahmantio (baybr878)}

\begin{document}

\maketitle

\section{Background}
High-dimensional multivariate gaussian distribution is used in various models and applications. In some cases, we need to generate from a certain distribution which applies constraints to a multivariate Gaussian distribution (\cite{gelfand1992GS} and \cite{RodrguezYam2004EfficientGS}). Sampling from this distribution is still a challenging issue, particularly because it is not straightforward to compute the normalizing constant for the density function.

The gibbs sampler has proven to be a suitable choices to sample from truncated multivariate Gaussian distributions (\cite{gelfand1992GS}). Recently, more sophisticated methods have been developed to generate samples from truncated multivariate Gaussian distributions. In this research project, two methods, namely Exact Hamiltonian Monte Carlo (\cite{pakman2013exact}) and Analytic Elliptical Slice Sampling (\cite{Fagan2016ESSwEP}), will be compared.

\section{Definitions}
\subsection{Truncated Multivariate Gaussian Distribution}
The truncated multivariate Gaussian distribution is a probability distribution obtained from a multivariate Gaussian random variable by bounding it under some linear (or quadratic) constraints.

Let $\textbf{w}$ be a $d$-dimensional Gaussian random variable with mean vector $\boldsymbol{\mu}$ and covariance matrix $\boldsymbol{\Sigma}$. The corresponding truncated multivariate Gaussian distribution can be defined as
\begin{equation}\label{eq:tmg}
	p(\boldsymbol{\textbf{x}}) = \frac{\exp\{-\frac{1}{2}(\textbf{x}-\boldsymbol{\mu})^{\intercal} \boldsymbol{\Sigma}^{-1}(\textbf{x}-\boldsymbol{\mu})\}}{\int_{\textbf{F}\textbf{x} + \textbf{g} \geq 0} \exp\{-\frac{1}{2}(\textbf{x}-\boldsymbol{\mu})^{\intercal} \boldsymbol{\Sigma}^{-1}(\textbf{x}-\boldsymbol{\mu})\} d\textbf{x}}\mathbbm{1}(\textbf{F}\textbf{x} + \textbf{g} \geq 0)
\end{equation}
where $\textbf{x}$ is a $d$-dimensional truncated Gaussian random variable, $\mathbbm{1}$ is an indicator function, and $\textbf{F}$ is an $m \times d$ matrix, which, together with the $m \times 1$ vector of $\textbf{g}$, defines all $m$ constraints of $p(\boldsymbol{\textbf{x}})$.  We denote this as $\textbf{x} \sim TN(\boldsymbol{\mu}, \boldsymbol{\Sigma},\textbf{F},\textbf{g})$.

We can rewrite $p(\boldsymbol{\textbf{x}})$ as
\begin{equation}\label{eq:tmg2}
	p(\textbf{x}) = \frac1Z\exp\bigg\{-\frac{1}{2}\textbf{x}^{\intercal} \boldsymbol{\Lambda} \textbf{x} + \boldsymbol{\nu}^{\intercal}\textbf{x}\bigg\}\mathbbm{1}(\textbf{F}\textbf{x} + \textbf{g} \geq 0)
\end{equation}
where $\boldsymbol{\Lambda} = \boldsymbol{\Sigma}^{-1}$, $\boldsymbol{\nu} = \boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}$, and $Z$ is the normalizing constant. Through linear change of variables, \eqref{eq:tmg2} can be transformed into
\begin{equation}\label{eq:tmg3}
	p(\textbf{x}) = \frac1Z\exp\bigg\{-\frac{1}{2}\textbf{x}^{\intercal}\textbf{x}\bigg\}\mathbbm{1}(\textbf{F}^*\textbf{x} + \textbf{g}^* \geq 0)
\end{equation}
such that $\textbf{x} \sim TN(\textbf{0}, \textbf{I}_d,\textbf{F}^*,\textbf{g}^*)$, for some values of $\textbf{F}^*$ and $\textbf{g}^*$.

\subsection{Exact Hamiltonian Monte Carlo for Truncated Multivariate Gaussians}
\subsubsection{Hamiltonian Monte Carlo}
Hamiltonian Monte Carlo (HMC) is a Markov Chain Monte Carlo method that makes use of an ideal system that follow Hamiltonian dynamics. A particle in this system can be described by a Hamiltonian energy function
\begin{equation}\label{eq:hml}
	H(\textbf{x}, \textbf{s}) = U(\textbf{x}) + K(\textbf{s})
\end{equation}
where $U(\textbf{x})$ is the potential energy term as a function of particle's position ($\textbf{x}$) and $K(\textbf{s})$ is the kinetic energy term as a function of particle's momentum ($\textbf{s}$). Both $\textbf{x}$ and $\textbf{s}$ are of $d$-dimensions.
The change of position and momentum over time $t$ can be described by Hamilton's equations
\begin{equation}\label{eq:heqs}
\begin{split}
	\frac{\partial x_i}{\partial t} & = \frac{\partial H}{\partial s_i} \\
	\frac{\partial s_i}{\partial t} & = -\frac{\partial H}{\partial x_i}, \qquad i=1,...,d.\\
\end{split}
\end{equation}

To sample from a distribution using HMC, we can relate the target distribution to the current energy state of the particle through canonical distribution:
\begin{equation}\label{eq:can}
	p(\textbf{x}) \propto \exp\{-E(\textbf{x})\}
\end{equation}
where the target distribution, $p(\textbf{x})$, depends on the value of energy function $E(\textbf{x})$. In a Hamiltonian system, we have $H(\textbf{x}, \textbf{s})$ as our energy function, which results in the canonical distribution:
\begin{equation}\label{eq:can2}
\begin{split}
	p(\textbf{x}, \textbf{s}) &\propto \exp\{-H(\textbf{x}, \textbf{s})\} \\
	&\propto \exp\{-U(\textbf{x})\} \exp\{-K(\textbf{s})\} \\
	&\propto p(\textbf{x})p(\textbf{s}).
\end{split}
\end{equation}

Hence, $\textbf{x}$ and $\textbf{s}$ are independent. To sample from the target distribution $p(\textbf{x})$, we can sample from the joint distribution $p(\textbf{x}, \textbf{s})$ and ignore the variable $\textbf{s}$.

\subsubsection{Exact HMC for Truncated Multivariate Gaussians}
Exact Hamiltonian Monte Carlo (HMC) for Truncated Multivariate Gaussians (TMG) (\cite{pakman2013exact}) considers the exact paths of particle trajectories in a Hamiltonian system that is proportional to the negative logarithm of TMG distributions density. Suppose our target distribution $p(\textbf{x})$ is a truncated multivariate Gaussian distribution as in ($\ref{eq:tmg3}$). We can set our momenta to be normally distributed, that is $\textbf{s} \sim \mathcal{N}(\textbf{0}, \textbf{I}_d)$. Therefore, the Hamiltonian system can be described as:
\begin{equation}\label{eq:hsys}
	H = U(\textbf{x}) + K(\textbf{s}) = \frac{1}{2}\textbf{x}^{\intercal}\textbf{x} + \frac{1}{2}\textbf{s}^{\intercal}\textbf{s}
\end{equation}
subject to:
\begin{equation}\label{eq:st}
	\textbf{F}\textbf{x} + \textbf{g} \geq 0.
\end{equation}
for some values of $\textbf{F}$ and $\textbf{g}$.

The equations of motion for the Hamiltonian system in \eqref{eq:hsys} are:
\begin{equation}\label{eq:heqs2}
\begin{split}
	\frac{\partial x_i}{\partial t} & = \frac{\partial H}{\partial s_i} = s_i \\
	\frac{\partial s_i}{\partial t} & = -\frac{\partial H}{\partial x_i} -x_i, \qquad i=1,...,d.\\
\end{split}
\end{equation}

In this sense, we want the particles in Hamiltonian system to only move around inside the constrained space. The exact trajectory of a particle using the equations above is:
\begin{equation}\label{eq:path}
	x_i(t) = s_i(0)\sin(t) + x_i(0)\cos(t).
\end{equation}

A particle will follow the trajectory above until it hits a wall, or in other words, until $\textbf{F}\textbf{x} + \textbf{g} = 0$. Let $t_h$ be the time when the particle hits wall $h$, or when $\textbf{F}_h \cdot \textbf{x}(t_h) + \text{g}_h = 0$. It will hit the wall with velocity $\dot{\textbf{x}}(t_h)$ which can be decomposed into:
\begin{equation}\label{eq:bounce}
	\dot{\textbf{x}}(t_h) = proj_{\textbf{n}}\dot{\textbf{x}}(t_h) + proj_{\textbf{F}_h}\dot{\textbf{x}}(t_h)
\end{equation}
where $proj_{\textbf{n}}\dot{\textbf{x}}(t_h)$ is the projection of $\dot{\textbf{x}}(t_h)$ on the normal vector $\textbf{n}$ perpendicular to $\textbf{F}_h$ and
\begin{equation}\label{eq:proj}
\begin{split}
	proj_{\textbf{F}_h}\dot{\textbf{x}}(t_h) &= \frac{\textbf{F}_h \cdot \dot{\textbf{x}}(t_h)}{||\textbf{F}_h||}\frac{\textbf{F}_h}{||\textbf{F}_h||}\\
	&= \frac{\textbf{F}_h \cdot \dot{\textbf{x}}(t_h)}{||\textbf{F}_h||^2}\textbf{F}_h \\
	&= \alpha_h\textbf{F}_h.
\end{split}
\end{equation}
By inverting the direction of $proj_{\textbf{n}}\dot{\textbf{x}}(t_h)$, we can obtain the reflected velocity as
\begin{equation}\
\begin{split}
	\dot{\textbf{x}}_R(t_h) & = -proj_{\textbf{n}}\dot{\textbf{x}}(t_h) + proj_{\textbf{F}_h}\dot{\textbf{x}}(t_h) \\
	& = -\dot{\textbf{x}}(t_h) + 2\alpha_h\textbf{F}_h
\end{split}
\end{equation}
which can be used as the new initial velocity ($s_i(0)$) in \eqref{eq:path} for the particle to continue its path. It can be shown that exact HMC follows the detailed balanced condition (\cite{pakman2013exact}) which leaves the canonical distribution invariant.

The process of sampling using exact HMC can be summarized into two steps:
\begin{enumerate}
	\item Sample $\textbf{s}$ from $\mathcal{N}(\textbf{0}, \textbf{I}_d)$.
	\item Use $\textbf{s}$ and the last value of $\textbf{x}$, and move the particle for a time $T$ until we reach $\textbf{x}^*$ and $\textbf{s}^*$. Use $\textbf{x}^*$ as initial positions for the next iteration.
\end{enumerate}

\subsection{Analytic Elliptical Slice Sampling}
\subsubsection{Slice Sampling}
Slice sampling (\cite{Neal2003}) is a method that tries to sample uniformly from the ($n+1$)-dimensional region under $f(x)$ that is proportional to density function$p(x)$. It is done by using Gibbs sampling to sample from the joint distribution $(x, y)$ where $y$ is an auxiliary variable. We do this by alternately sampling from $y|x \sim \text{U}(0, f(x))$ and $x|y \sim \text{U}(S)$ where $S = \{x: f(x)>y\}$. To obtain samples from $x$, we can sample from $p(x,y)$ and ignore the $y$ later.

\subsubsection{Elliptical Slice Sampling}
Elliptical slice sampling (ESS, \cite{murray2010}) is a form of slice sampling that uses Gaussian prior to sample from a posterior distribution of the form
\begin{equation}\label{eq:ESSpost}
	p^*(\textbf{x}) = \frac1Z\mathcal{N}(\textbf{x};\textbf{0}, \boldsymbol{\Sigma}) L(\textbf{x})
\end{equation}
where $Z$ is the normalization constant, $\mathcal{N}(\textbf{0},\boldsymbol{\Sigma})$ is a multivariate Gaussian prior, and $L$ is a likelihood function. To do this, we introduce an auxiliary variable $\boldsymbol{\nu} \sim \mathcal{N}(\textbf{0}, \boldsymbol{\Sigma})$ so that given an initial state $\textbf{x}$, the update ellipse is
\begin{equation}\label{eq:rule}
	\textbf{x}' = \textbf{x}\cos(\theta) + \boldsymbol{\nu}\sin(\theta), \quad  \theta \in [0, 2\pi]
\end{equation}

The algorithm begins by sampling the slice height $y \sim \text{U}(0, L(\textbf{x}))$ and the auxiliary variable $\boldsymbol{\nu} \sim \mathcal{N}(\textbf{0}, \boldsymbol{\Sigma})$ that determines the ellipse in \eqref{eq:rule}. We then sample $\theta$ uniformly from $[0, 2\pi]$ and shrink the interval towards $\theta=0$ (initial state) until we found $\theta$ that satisfies $L(\textbf{x}') > y$. The new point is accepted as the next state and we use it as the initial state for the next iteration.

ESS is analogous to slice sampling in the sense that it samples uniformly from the height of function and from the interval where the function value is more than the height. Furthermore, it can be shown that the transition operator of ESS is reversible which implies that the Markov chain converges to the target posterior distribution (\cite{murray2010}).

\subsubsection{Analytic Elliptical Slice Sampling}
Analytic Elliptical Slice Sampling (\cite{Fagan2016ESSwEP}) tries to cut the process of shrinking the ellipse in ESS by pre-determining the possible sampling interval. First, we define
\begin{equation}\label{eq:ellipse}
	\mathcal{E} = \{\textbf{x}':\textbf{x}'(\theta) = \textbf{x}\cos(\theta) + \boldsymbol{\nu}\sin(\theta)\}
\end{equation}
as the set of possible new states and define
\begin{equation}\label{eq:slice}
	\mathcal{S}(y,\mathcal{E}) = \{ \textbf{x}'\in \mathcal{E}: L(\textbf{x}')>y\}
\end{equation}
as the slice of acceptable proposed state. To form a markov chain that converges to the posterior distribution, we need to sample uniformly from the analytically pre-determined slice $\mathcal{S}(y,\mathcal{E})$ (\cite{Fagan2016ESSwEP}). That is, the algorithm is similar to ESS except that the process of shrinking the ellipse is replaced by determining $\mathcal{S}(y,\mathcal{E})$ so that sampling from $\theta$ guarantees to yield a new state $\textbf{x}'$.

In a truncated multivariate Gaussian setting, we have
\begin{equation}\label{eq:tmgESS}
\begin{split}
	p^*(\textbf{x}) &= \frac1Z\mathcal{N}(\textbf{x};\textbf{0}, \boldsymbol{\Sigma}) TN(\textbf{x};\textbf{0},\textbf{I}_d,\textbf{F},\textbf{g}) \\
	&\propto \mathcal{N}(\textbf{x};\textbf{0},\boldsymbol{\Sigma}) \mathcal{N}(\textbf{x};\textbf{0},\textbf{I}_d)\mathbbm{1}(\textbf{F}\textbf{x} + \textbf{g}\geq 0)
\end{split}
\end{equation}
Given $\textbf{x}$, $\boldsymbol{\nu}$, and $y$, we can determine the slice of possible new states as
\begin{equation}\label{eq:exactslice}
\begin{split}
	\mathcal{S}(y,\mathcal{E}) &= \{\theta \in [0,2\pi]:  L(\textbf{x}')>y\} \\
	&= \cap^m_{j=1}\ \{\theta \in [0,2\pi]: F_j^{\intercal}\textbf{x} + g_j \geq0 \} \\
	& \quad \cap\{\theta \in [0,2\pi]: \mathcal{N}(\textbf{x};\textbf{0},\textbf{I}_d)>y\}.
\end{split}
\end{equation}

\cite{Fagan2016ESSwEP} also demonstrates how to sample $J$ points from a single ESS iteration. In an ESS setting, it is done by 'recycling' the interval that has been sampled until $J$ points are accepted. One of the $J$ points is then randomly selected as the next initial state. In the case of analytic ESS, $J$ different values of y are sampled and for each of them we find the corresponding $\mathcal{S}(y,\mathcal{E})$ and uniformly sample one point.

\section{Results}
In this section we are going to compare both methods (Exact HMC for TMG and Analytic ESS) under different settings of truncated multivariate gaussians.

Figure 1 shows how both algorithms sample a two-dimensional gaussian distribution with some constraints that results in a narrow support, similar to Figure 1 in \cite{pakman2013exact}. From both cases, we can see that they worked as expected. The generated samples rapidly converged to the mean while also complying the constraints.

\begin{figure}[h!]
	\includegraphics[width=\linewidth]{fig1.png}
	\caption{Comparison of generated points from $\mathcal{N}((4,4)^T,\textbf{I}_2)$ under the constraints $x_1 \leq x_2 \leq1.1 x_1$ and $x_1,x_2 \geq 0$ with initial points (2, 2.1). First column: 1000 iterations. Second column: trace plots of the first 400 iterations of $x_2$. Third column: Autocorrelation of $x_2$. In this example we used $T=\pi/2$ for HMC and $J=1$ for ESS. Both methods converges around $x_2=4$ and have relatively low autocorrelation scores.}
\end{figure}

Notice that the maximum traveling time $(T)$ and number of samples per iteration $(J)$ affect the computation time for Exact HMC and Analytic ESS respectively. The higher the maximum traveling time ($T$), the longer it takes for a particle in an HMC system to move while also increases the possibility of bouncing. At the same time, an increase in the value of $J$ lets us sample multiple points from the same ellipse without a significant increase in computation time.

From the experiments, we observed a large disparity between the runtimes of both algorithms. As seen in the Table 1, Exact HMC performs much faster than the fastest Analytic ESS.

\begin{table}
\centering
\begin{tabular}{|c|c|}
	\hline
	Method & Avg. computation time per sample (second) \\
	\hline
	HMC($T=\frac{\pi}{2}$) & 0.0001728 \\
	\hline
	HMC($T=\frac{3\pi}{4}$) & 0.000173 \\
	\hline
	ESS($J=1$) & 0.0271275 \\
	\hline
	ESS($J=5$) & 0.0250084 \\
	\hline
	ESS($J=10$) & 0.0236001 \\
	\hline
\end{tabular}
\caption{Average computation time per sample for both models under different settings for 10-dimensional TMG under the constraints $-1 \leq x_i \leq 1, i=1,...,10$ using CPU. For HMC, T is the maximum travel time. For ESS, J is the number of points sampled in each iteration. The computation time for Exact HMC is overall much faster than Analytic ESS.}
\end{table}

\section{Discussion}
In this research project, we have compared Exact HMC and Analytic ESS. In terms of the generated samples, they are not significantly different. Exact HMC also performs much faster than Analytic ESS. More rigorous measurements can be made to evaluate whether the samples are autocorrelated within a chain or not by measuring their effective sample size.

There are, however, some caveats to the computation of Analytic ESS in this case. We expected the runtime to be somewhat similar to Exact HMC, but it runs more than 100 times slower. This could be to the fact that external packages are used for the computation of the intervals and slices which could have been replaced by customized functions that are more specific to the task, hence the faster runtime. The programming language used $\texttt{R}$ is also not popularly chosen because of its speed. Thus, the algorithm might need to be rewritten in a language that is proven to be computationally faster (e.g. $\texttt{Python}$).

\newpage
\bibliography{ref}

\end{document}


\chapter{Storing a Path as a Tree}
\label{append:treap}
\vspace*{2em}

In phase 3 (\autoref{algo:dham:ph3}) of the DHAM, we construct a path out of the 2 cycles being patched and search (DFS/BFS) the tree formed on this path by double rotation operations. Now, there are 2 parts to this problem: One is to store the path in such a way that the double rotations operations can be done efficiently, and the other is to identify valid double rotations, given such a path.

If we make the obvious decision to store the path as a link list to make the double rotation operation constant time (by just swapping 2 of the links), the process of identifying a valid operation ends up being linear due to having to traverse the path.

Instead, if we store the path as an array that making generation of valid operations faster, we realize that executing the operation itself now needs updating up to linear number of elements in the array.

To get around this difficulty, we store our path as a Randomized Search Tree \cite{treap} to get a $\bigO(\log n)$ bound on both of the operations.

\section{Randomized Search Tree}
The Randomized Search Tree (or treap) as given by Aragon and Seidel \cite{treap} is based on 2 auxiliary operations - split (around a given key) and merge (assuming elements within the trees are in order of keys), to implement the main operations like insertion, deletion, union etc. The reason we choose this data structure is because we can use these auxiliary operations to also implement a double rotation by 'splitting' out a sub-array, and 'merging' it at the end.

To attain all the desired functionality, we make some modifications to the data structure

\subsection{Implicit Keys}
Doing a double rotation changes the position of elements in the structure. In this situation the keys(or index) of these elements becomes inaccurate. So, instead of storing the key as a value within the node, we maintain the keys of all nodes implicitly.

This is achieved by storing the size of the tree rooted at a node, and using this value to compute keys on the fly.

The updated node looks like

\begin{lstlisting}
struct Node {
    Node *left, *right, *parent;
    int val, y, subtree_size = 1;
    void recalc();
};
int cnt(Node* n) { return n ? n->subtree_size : 0; }
void Node::recalc() { c = cnt(l) + cnt(r) + 1; }
\end{lstlisting}

The key of a node is the number of elements with a smaller key, which can be looked on as the number of elements in the left subtree (because of the BST structure). So while traversing down the tree, we can keep track of the sum of their sizes (every time we traverse to the right) to implicitly maintain the key of a node we are currently visiting.
Modifying the split function accordingly, we get
\begin{lstlisting}
pair<Node*, Node*> split(Node* n, int k)
	if (!n) return {};
	if (cnt(n->l) >= k) {
		auto pa = split(n->l, k);
		n->l = pa.second;
		n->recalc();
		return {pa.first, n};
	} else {
		auto pa = split(n->r, k - cnt(n->l) - 1);
		n->r = pa.first;
		n->recalc();
		return {n, pa.second};
	}
\end{lstlisting}

The merge function remains largely the same since we do not access the keys of a node, assuming them to already be in order.

\subsection{key $\iff$ value conversions}
With keys being automatically updated, we only need to add the functionality to return the value given a key (accessing an element) and return a key given the value (search operation). These operations will allow us to generate valid operations efficiently.
These can simply be implemented in $\bigO(\log n)$ as shown below
\begin{lstlisting}
int key(Node* root, Node* x) {
    if (tree==nullptr || x==nullptr) return -1;
    int ans = cnt(x->l);
    while(x != root) {
        auto par = x->p;
        if (par->r == x)
            ans += 1 + cnt(par->l);
        x = par;
    }
    return ans;
}

int value(Node *n, int key) {
    if (!n) return -1;
    if (cnt(n->l) == key) return n->val;
    else if (cnt(n->l) > key) return value(n->l, key);
    else return value(n->r, key - cnt(n->l) - 1);
}
\end{lstlisting}

\newpage
\section{Double Rotation}
Armed with the tools described above, we can implement the double rotation operation to move the subarray $[l, r)$ to the index $k$ in $\bigO(\log n)$ time.

\begin{lstlisting}
void move(Node*& t, int l, int r, int k) {
    Node *a, *b, *c;
    tie(a,b) = split(t, l); tie(b,c) = split(b, r - l);
    if (k <= l) t = merge(ins(a, b, k), c);
    else t = merge(a, ins(c, b, k - r));
}
\end{lstlisting}


\section{Current Operator in Landau Levels}

Now consider about the current density operator for $N$th Landau level. Since we have already found the extact solution for our time depenedent Hamiltonian and we have derive them as Floquet states with quesi energies. Using second quantization notaion we can represent our Hamiltonian using creating and anhilation operators for Floquet states which are eigen function of this Hamiltonian.

\vspace{5mm}
\noindent
Let's introduce our creation and anhilation operators in momentum space for the $N$th Landau level states as follows
\begin{equation} \label{7.1}
  c_{k_x}^{\dagger} \ket{0} = \ket{\psi_{N,k_x}} \quad \text \quad
  c_{k_x} \ket{\psi_{N,k_x}}  = \ket{0}.
\end{equation}
Therefore with second quantization we can represent our Hamiltonian as
\begin{equation} \label{7.2}
  \hat{H}_N = \sum_{k_x} \varepsilon_N c_{k_x}^{\dagger}c_{k_x}
\end{equation}
and particle density operator as
\begin{equation} \label{7.3}
  \hat{\rho}_N = \sum_{{k'}_x} c_{{k'}_x}^{\dagger}c_{{k'}_x}.
\end{equation}

\noindent
Now we can find the commutation relationship with each other as
\begin{equation} \label{7.4}
  \qty[\hat{H}_N , \hat{\rho}_N] =
  \qty[ \sum_{k_x} \varepsilon_N c_{k_x}^{\dagger}c_{k_x},
  \sum_{{k'}_x} c_{{k'}_x}^{\dagger}c_{{k'}_x}]
\end{equation}
and this can be simplified using fermions commutation relationship and one can derive that
\begin{equation} \label{7.5}
  \qty[\hat{H}_N , \hat{\rho}_N] =
   \sum_{k_x} \varepsilon_N c_{k_x}^{\dagger}c_{k_x}.
\end{equation}
Now using \textit{Liouville-Von Neumann equation} we can derive that
\begin{equation} \label{7.6}
  \pdv{\hat{\rho}_N}{t} =
   -\frac{i}{\hbar}\qty[\hat{H}_N , \hat{\rho}_N] =
   \sum_{k_x} -\frac{i}{\hbar}\varepsilon_N c_{k_x}^{\dagger}c_{k_x}
\end{equation}
and using famous \textit{continuty equation} we can make relationship with probability current density ($\mb{j}(\mb{r},t)$) operator as follows
\begin{equation} \label{7.7}
  \pdv{\hat{\rho}_N}{t} = - \grad \cdot \hat{\mb{j}}(\mb{r},t).
\end{equation}
However, we can assume that the current flow of this system only can be happed in $x$ direction due to $y$ direction restriction by magnetic leangth. Therefore we can re-write the above equation as follows
\begin{equation} \label{7.8}
  \pdv{\hat{\rho}_N}{t} = - \pdv{\hat{jx}^x(\mb{r},t)}{x}
\end{equation}
and using this on Eq. \eqref{7.6} we can derive that
\begin{equation} \label{7.9}
 - \pdv{\hat{j^x}(\mb{r},t)}{x} =
 \sum_{k_x} -\frac{i}{\hbar}\varepsilon_N c_{k_x}^{\dagger}c_{k_x}
\end{equation}
and this leads to
\begin{equation} \label{7.10}
 \hat{j}^x(\mb{r},t) =
 \sum_{k_x} -\frac{i L_x}{\hbar}\varepsilon_N c_{k_x}^{\dagger}c_{k_x}.
\end{equation}
Therefore we can identify the momentum space component of the current density operator as
\begin{equation} \label{7.11}
 - {j}^x(k_x,t) =
 -\frac{i L_x}{\hbar}\varepsilon_N
\end{equation}
and find the time space fourier series components and this will vanish all the components expect $s=0$ components because
\begin{equation} \label{7.12}
 \int dt e^{-is\omega t} = 2\pi \delta_{s,0}
\end{equation}
and then we can find the time fourier series components of current operator as follows
\begin{equation} \label{7.13}
 {j}^x_{s=0} (k_x,t) =
 \frac{i 2\pi L_x}{\hbar}\varepsilon_N =
 i 2\pi \omega_0 L_x \qty(N + \frac{1}{2})
\end{equation}
and for electric current flow we can introfuce the electron's charge as $-e$ and this will be modified to
\begin{equation} \label{7.14}
 {j}^x_{s=0} (k_x,t) =
 -i 2\pi e \omega_0 L_x \qty(N + \frac{1}{2})
\end{equation}
\hfill$\blacksquare$

\noindent
Now we can use this in our previously derived conductivity formula \eqref{6.20} and get
\begin{equation} \label{7.15}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] =
    \frac{\hbar}{4\pi A L_x}
    \sum_{{k_x}}
    \qty[-i 2\pi e \omega_0 L_x \qty(N + \frac{1}{2})]^2
    \frac{\hbar^2}
    {\qty[\Gamma^{00}_{N}(\varepsilon_F,k_x)]^2}
    \qty[
      1 -
      4\qty(\frac{{\varepsilon_F}-{\varepsilon_N}}{\Gamma^{00}_{N}(\varepsilon_F,k_x)})^2
    ]
  \end{aligned}
\end{equation}
and this leads to
\begin{equation} \label{7.16}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] =
    \frac{\pi e^2 \omega_0^2 \hbar}{L_x}
    \sum_{{k_x}}
    \qty(N + \frac{1}{2})^2
    \frac{\hbar^2}
    {\qty[\Gamma^{00}_{N}(\varepsilon_F,k_x)]^2}
    \qty[
      1 -
      4\qty(\frac{{\varepsilon_F}-{\varepsilon_N}}{\Gamma^{00}_{N}(\varepsilon_F,k_x)})^2
    ].
  \end{aligned}
\end{equation}
However we know that $\Gamma^{00}_{N}(\varepsilon_F,k_x)$ is independent of $k_x$ and we can get summation over availble all momentum through the summation. However by the condition that the center of the force of the oscillator $y_0$ must physically liw within the system $-L_y/2 < y_0 < L_y/2$, one can derive that
\begin{equation} \label{7.17}
 -\frac{m\omega_0 Ly}{2\hbar} \leq k_x \leq \frac{m\omega_0 Ly}{2\hbar}
\end{equation}
Therefore the Eq. \eqref{7.16} can be simplified to
\begin{equation} \label{7.18}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] =
    \frac{\pi e^2 \omega_0^2 \hbar}{L_x}
    \frac{m\omega_0 Ly}{\hbar}
    \qty(N + \frac{1}{2})^2
    \frac{\hbar^2}
    {\qty[\Gamma^{00}_{N}]^2}
    \qty[
      1 -
      4\qty(\frac{{\varepsilon_F}-{\varepsilon_N}}{\Gamma^{00}_{N}})^2
    ]
  \end{aligned}
\end{equation}
and this will becomes
\begin{equation} \label{7.19}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] =
    {\pi e^2 \omega_0 m }
    \frac{\qty(\hbar \omega_0)^2}
    {\qty[\Gamma^{00}_{N}]^2}
    \qty(N + \frac{1}{2})^2
    \qty[
      1 -
      4\qty(\frac{{\varepsilon_F}-{\varepsilon_N}}{\Gamma^{00}_{N}})^2
    ].
  \end{aligned}
\end{equation}
\hfill$\blacksquare$


% Created 2021-07-22 Thu 11:00
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{pifont}
\newcommand{\cmark}{\textcolor{green!80!black}{\ding{51}}}
\usepackage{amssymb}
\usepackage{pgfplotstable}
\DeclareMathOperator{\shift}{q}
\DeclareMathOperator{\diff}{p}
\usepackage{khpreamble, euscript}
\DeclareMathOperator{\atantwo}{atan2}
\newcommand*{\ctrb}{\EuScript{C}}
\newcommand*{\obsv}{\EuScript{O}}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{State space models}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={State space models},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{PMSM - sysid}
\label{sec:orgd8d4281}

\begin{frame}[label={sec:org9a749ce}]{Obtain state-space model from discrete-time pulse-transfer function}
\end{frame}

\begin{frame}[label={sec:org7a551b6}]{The permanent magnet synchronous motor}
\begin{center}
\includegraphics[width=0.9\linewidth]{../../figures/permanent-motor.jpg}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgb40e192}]{The PMSM}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/pmsm_control_block_diag.png}
\end{center}
{\footnotesize De Liu and Li  ``Speed control for PMSM servo system'', IEEE Transactions on Industrial Electronics, 2012.}
\end{frame}
\begin{frame}[label={sec:orgfaa79fd}]{Identified model}
Two poles, two zeros, one delay
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=10mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[block, right of=input] (delay1)  {$\frac{1}{z}$};
    \node[block, right of=delay1, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z^2 + a_1 z + a_2}$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (delay1);
    \draw[->] (delay1) -- node[above, pos=0.3] {} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

    \begin{scope}[yshift=-1cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}
    \begin{scope}[yshift=-3cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}

    \node[coordinate, below of=input, node distance=2cm] (input2) {};
    \node[block, right of=input2, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z^2 + a_1 z + a_2}$};
    \node[block, right of=plant] (delay2)  {$\frac{1}{z}$};
    \node[coordinate, right of=delay2] (output) {};

    \draw[->] (input2) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, pos=0.3] {} (delay2);
    \draw[->] (delay2) -- node[above, near end] {$y(k)$} (output);

    \node[coordinate, below of=input2, node distance=2cm] (input3) {};
    \node[block, right of=input3, node distance=30mm] (plant)  {$\frac{b_0z^2 + b_1z + b_2}{z(z^2 + a_1 z + a_2})$};
    \node[coordinate, right of=plant, node distance=30mm] (output) {};

    \draw[->] (input3) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org663463f}]{Identified model}
\[ H(z) = \frac{6.91z^2 + 16.48z -17.87}{z(z^2 - 1.766z + 0.7665)} = \frac{6.91(z+3.19)(z-0.81)}{z(z-0.998)(z-0.768)}\]

\begin{center}
\includegraphics[width=0.6\linewidth]{../../figures/validation-result-2020-07-24.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org6ba2fba}]{From pulse-transfer function to state space model}
\begin{center}
  \begin{tikzpicture}[node distance=32mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[block, right of=input] (plant)  {$H(z) = \frac{b_0z^2 + b_1z + b_2}{z(z^2 + a_1 z + a_2)}$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);

    \begin{scope}[yshift=-2cm, xshift = 3cm]
    \node {$\Updownarrow$};
    \end{scope}

    \begin{scope}[yshift=-4cm, node distance=50mm, xshift=-2cm]
    \node[coordinate] (input) {};
    \node[block, right of=input, align=center] (plant)  {$x(k+1) = \Phi x(k) + \Gamma u(k)$\\$y(k) = C x(k)$};
    \node[coordinate, right of=plant] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(k)$} (plant);
    \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);
    \end{scope}

  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:orga09dbeb}]{Canonical forms}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]
Find a representation in state space form
\begin{align*}
 x(k+1) &= \Phi x(k) + \Gamma u(k) \\
 y(k) &= C x(k)
 \end{align*}

\pause

\begin{itemize}
\item Controlable canonical form
\item Observable canonical form
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org8bf8145}]{Controlable canonical form}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]

\begin{align*}
 x(k+1) &= \begin{bmatrix} -a_1 & -a_2 & -a_3\\1 & 0 & 0\\0 & 1 & 0\end{bmatrix} x(k) + \begin{bmatrix}1\\0\\0\end{bmatrix} u(k) \\
 y(k) &= \begin{bmatrix} b_1 & b_2 & b_3 \end{bmatrix} x(k)
 \end{align*}
\end{frame}

\begin{frame}[label={sec:orga3d2da4}]{Observable canonical form}
Given pulse-transfer function
\[ H(z) = \frac{b_1 z^2 + b_2 z + b_3}{z^3 + a_1z^2 + a_2z + a_3}.\]

\begin{align*}
 x(k+1) &= \begin{bmatrix} -a_1 & 1 & 0\\-a_2 & 0 & 1\\-a_3 & 0 & 0\end{bmatrix} x(k) + \begin{bmatrix}b_1\\b_2\\b_3\end{bmatrix} u(k) \\
 y(k) &= \begin{bmatrix} 1 & 0 & 0 \end{bmatrix} x(k)
 \end{align*}
\end{frame}

\begin{frame}[label={sec:orgcb7a0f5}]{Canonical forms}
\alert{Activity} Find the controlable abd observable canonical forms for the pulse-transfer function of the motor. Answer on Canvas (questions 1 and 2 on today's exercises).

\[ H(z) = \frac{6.91z^2 + 16.48z -17.87}{z(z^2 - 1.766z + 0.7665)} = \frac{6.91(z+3.19)(z-0.81)}{z(z-0.998)(z-0.768)}\]
\end{frame}

\section{Apollo moon lander}
\label{sec:org0898f55}
\begin{frame}[label={sec:org0461feb}]{Discrete-time state-space  from continuous-time state space}
A.k.a. discretization
\end{frame}

\begin{frame}[label={sec:orgb2a2f80}]{Example - the Apollo lunar module}
\begin{center}
\includegraphics[width=\linewidth]{fig-apollo}
\end{center}
\end{frame}
\begin{frame}[label={sec:orgacd27c5}]{Example - the Apollo lunar module}
\begin{center}
\includegraphics[width=0.8\linewidth]{fig-apollo}
\end{center}
\alert{Activity} Which is the transfer function of the system?
\[1: \; G(s) = \frac{k_1 k_2}{s^2}\qquad 2: \; G(s) = \frac{k_1 k_2}{s(s^2 + 1)} \qquad 3: \; G(s) = \frac{k_1 k_2}{s^3}\]
\end{frame}

\begin{frame}[label={sec:orgd29c635}]{Example - the Apollo lunar module}
\begin{center}
\includegraphics[width=0.8\linewidth]{fig-apollo}
\end{center}
\alert{Activity} What sensors are needed by the control system?
\end{frame}

\begin{frame}[label={sec:org1d01b3c}]{Example - the Apollo lunar module}
\begin{center}
\includegraphics[width=0.7\linewidth]{fig-apollo}
\end{center}

State variables: \(x = \begin{bmatrix} x_1 & x_2 & x_3 \end{bmatrix}^T = \begin{bmatrix} \dot{\theta} & \theta & \dot{z} \end{bmatrix}^T\). With the dynamics
\[ \begin{cases} \dot{x}_1 =  \ddot{\theta} = k_1 u\\ \dot{x}_2 = \dot{\theta} = x_1\\ \dot{x}_3 = \ddot{z} = k_2\theta = k_2x_2 \end{cases} \]
\end{frame}

\begin{frame}[label={sec:org380c798}]{Example - the Apollo lunar module}
State variables: \(x = \begin{bmatrix} x_1 & x_2 & x_3 \end{bmatrix}^T = \begin{bmatrix} \dot{\theta} & \theta & \dot{z} \end{bmatrix}^T\). With dynamics
\[ \begin{cases} \dot{x}_1 =  \ddot{\theta} = k_1 u\\ \dot{x}_2 = \dot{\theta} = x_1\\ \dot{x}_3 = \ddot{z} = k_2\theta = k_2x_2 \end{cases} \]

\alert{Activity} Fill the matrix \(A\) and vector \(B\).

\[ \dot{x} = \begin{bmatrix} \dot{x}_1\\\dot{x}_2\\\dot{x}_3\end{bmatrix} = \underbrace{\begin{bmatrix} \textcolor{white}{0} & \textcolor{white}{0} &\textcolor{white}{0} \\\textcolor{white}{1} & \textcolor{white}{0}& \textcolor{white}{0}\\ \textcolor{white}{0}& \textcolor{white}{k_2} &\textcolor{white}{0} \end{bmatrix}}_{A} \begin{bmatrix} x_1\\x_2\\x_3\end{bmatrix} + \underbrace{\begin{bmatrix} \textcolor{white}{k_1} \\ \textcolor{white}{0} \\\textcolor{white}{0}  \end{bmatrix}}_{B} u \]
\end{frame}

\begin{frame}[label={sec:orgd8545c5}]{Example - the Apollo lunar module}
\end{frame}

\section{Discretization}
\label{sec:orge61a8be}

\begin{frame}[label={sec:org8990824}]{Discretizing a continuous-time state-space model}
\end{frame}
\begin{frame}[label={sec:orgedf0d91}]{Discretización}
The general solution to a linear, continuous-time state-space system
\begin{align*}
x(t_k+\tau)& = \mathrm{e}^{A(\tau)} x(t_k) + \int_{0}^\tau \mathrm{e}^{As} B u\big((t_k+\tau)-s) ds
\end{align*}

\begin{center}
  \begin{tikzpicture}
    \draw[->] (-3,0) -- (6,0) node[below] {$t$};
    \draw (-2, 0.2) -- ( -2, 0) node[below] {$t_k=kh$};
    \draw (1, 0.2) -- ( 1, 0) node[below] {$t_{k+1}=kh+h$};
    \draw (4, 0.2) -- ( 4, 0) node[below] {$kh+2h$};
    \draw[thick, orange!90!black] (-3,0.3) -- (-2, 0.3) -- (-2,1) -- (1, 1) -- (1,0.8) -- (4, 0.8) --(4, 0.5) --(5.5, 0.5) node[pos=0.1, coordinate, pin=30:{$u(t)$}] {} ;
    \draw[->] (-2, -0.7) -- (0, -0.7) node[below] {$\tau$};
  \end{tikzpicture}
\end{center}

 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\end{frame}

\begin{frame}[label={sec:orgaa1f3f9}]{Discretization - The matrix exponential}
Square matrix \(A\). Scalar variable \(t\).
\[ \mathrm{e}^{At} = 1 + At + \frac{t^2}{2!}A^2 + \frac{t^3}{3!} A^3 + \cdots\]
Laplace transform
\[ \laplace{\mathrm{e}^{At}} = (sI - A)^{-1}\]
\end{frame}

\begin{frame}[label={sec:orge94101e}]{Discretization - example}
 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\[ A = \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & k_2 & 0\end{bmatrix}, \quad A^2 = \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & k_2 & 0\end{bmatrix}\begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & k_2 & 0\end{bmatrix}= \begin{bmatrix} 0 & 0 & 0\\0 & 0 & 0\\k_2 & 0  & 0\end{bmatrix}, \quad A^3 = 0\]
So,
\begin{align*}
 \Phi(h) &= \mathrm{e}^{Ah} = 1 + Ah + A^2 h^2/2  + \cdots \\
 &= \begin{bmatrix} 1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{bmatrix} + \begin{bmatrix} 0 & 0 & 0\\1 & 0 & 0\\0 & k_2 & 0\end{bmatrix}h + \begin{bmatrix} 0 & 0 & 0\\0 & 0 & 0\\k_2 & 0 & 0\end{bmatrix}\frac{h^ 2}{2}= \begin{bmatrix} 1 & 0 & 0\\h & 1 & 0\\\frac{h^2k_2}{2} & hk_2 & 1\end{bmatrix}
 \end{align*}
\end{frame}

\begin{frame}[label={sec:orge5dc4ca}]{Discretization - example}
 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)
\end{align*}
\[\mathrm{e}^{As}B &=  \begin{bmatrix} 1 & 0 & 0\\s & 1 & 0\\\frac{s^2k_2}{2} & sk_2 & 1\end{bmatrix} \begin{bmatrix} k_1\\0\\0 \end{bmatrix} = k_1 \begin{bmatrix} 1\\s\\\frac{k_2s^2}{2} \end{bmatrix}
  \]
\begin{align*}
\Gamma (h) &= \int_0^h \mathrm{e}^{As}B ds = k_1 \int_0^h \begin{bmatrix} 1\\s\\\frac{k_2s^2}{2} \end{bmatrix}ds = k_1\begin{bmatrix} h\\ \frac{h^2}{2} \\ \frac{k_2 h^3}{6} \end{bmatrix}
\end{align*}
\end{frame}

\begin{frame}[label={sec:org2d3268c}]{Discretization - example}
 \begin{align*}
  x(kh+h) &= \mathrm{e}^{Ah} x(kh) + \int_{0}^{h} \mathrm{e}^{As} B u(kh+h-s) ds\\
   &= \underbrace{\mathrm{e}^{Ah}}_{\Phi(h)} x(kh) + \underbrace{\left(\int_{0}^h \mathrm{e}^{As} B ds \right)}_{\Gamma(h)} u(kh)\\
   &= \begin{bmatrix} 1 & 0 & 0\\h & 1 & 0\\\frac{h^2k_2}{2} & hk_2 & 1\end{bmatrix} x(kh) + k_1 \begin{bmatrix} h\\ \frac{h^2}{2} \\ \frac{k_2 h^3}{6} \end{bmatrix} u(kh)
\end{align*}
\end{frame}

\begin{frame}[label={sec:orgff35f9c}]{Discretization - exercise}
\alert{Activity} Discretize the system (question 3 on today's exercises on Canvas)
\[ \dot{x} = Ax + Bu = \begin{bmatrix} 0 & 1\\ 0 & 0 \end{bmatrix} x + \begin{bmatrix}0\\1\end{bmatrix}u\]
\end{frame}
\end{document}


\providecommand{\main}{..}
\documentclass[\main/thesis.tex]{subfiles}
\begin{document}

\graphicspath{ {images/}{../images/} }

\chapter{Introduction}\label{introduction}

\section{Positional Numeral Systems}

A numeral system is a writing system for expressing numbers, and humans have
invented various kinds of numeral systems throughout history.
Take the number ``2016'' for example:

\begin{center}
    \begin{tabular}{ | l | r | }
    \textbf{Numeral system} & \textbf{notation}  \\
    \hline
    Chinese numerals    & 兩千零一十六    \\
    Roman numerals      & MMXVI         \\
    Egyptian numerals   & \includegraphics[width=2em]{egyptian/2016.png} \\
    \end{tabular}
\end{center}

Even so, most of the systems we are using today are positional notations\cite{knuth1998art}
because they can express infinite numbers with just a finite set of symbols called \textbf{digits}.

\subsection{Digits}

Any set of symbols can be used as digits as long as we know how to \textit{assign}
each digit to the value it represents.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ | l | *{16}{l} | }
    \textbf{Numeral system} & \multicolumn{16}{c |}{\textbf{Digits} } \\
    \hline
    decimal         & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &    &    &    &    &    &    \\
    binary          & 0 & 1 &   &   &   &   &   &   &   &   &    &    &    &    &    &    \\
    hexadecimal     & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A  & B  & C  & D  & E  & F  \\
    \hline
    \textbf{Assigned value}  & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} & \textbf{13} & \textbf{14} & \textbf{15} \\
    \end{tabular}
    \end{adjustbox}
\end{center}

We place a bar above a digit to indicate its assignment.
For instance, these are the assignments of hexadecimal digits.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ *{4}{c} }
    $ \bar{0} \mapsto 0 $ & $ \bar{1} \mapsto 1 $ & $ \bar{2} \mapsto 2 $ & $ \bar{3} \mapsto 3 $ \\
    $ \bar{4} \mapsto 4 $ & $ \bar{5} \mapsto 5 $ & $ \bar{6} \mapsto 6 $ & $ \bar{7} \mapsto 7 $ \\
    $ \bar{8} \mapsto 8 $ & $ \bar{9} \mapsto 9 $ & $ \bar{A} \mapsto 10 $ & $ \bar{B} \mapsto 11 $ \\
    $ \bar{C} \mapsto 12 $ & $ \bar{D} \mapsto 13 $ & $ \bar{E} \mapsto 14 $ & $ \bar{F} \mapsto 15 $ \\
    \end{tabular}
    \end{adjustbox}
\end{center}

Positional numeral systems represent a number by lining up a series of digits:

$$ \xrightarrow{2016} $$

In this case, $ 6 $ is called the \textit{least significant digit},
and $ 2 $ is known as the \textit{most significant digit}.
Except when writing decimal numbers,
we will write down numbers in reverse order,
from the least significant digit to the most significant digit like this

$$ \xleftarrow{6102} $$

\subsection{Syntax and Semantics}

Syntax bears no meaning;
its semantics can only be expressed through the process of \textit{converting} to some other syntax.
Numeral systems are merely syntax.
The same notation can represent different numbers in different context.

Take the notation ``11'' for example; it could have several meanings.

\begin{center}
    \begin{tabular}{ | l | r | }
    \textbf{Numeral system}      & \textbf{number in decimal}  \\
    \hline
    decimal             & 11    \\
    binary              & 3     \\
    hexadecimal         & 17    \\
    \end{tabular}
\end{center}

To make things clear, we call a sequence of digits a \textbf{numeral}, or \textbf{notation};
the number it expresses a \textbf{value}, or simply a \textbf{number};
the process that converts notations to values an \textbf{evaluation}.
From now on, \textbf{numeral systems} only refer to the positional ones.
We will not concern ourselves with other kinds of numeral systems.

\subsection{Evaluating Numerals}

What we mean by a \textit{context} in the previous section is the \textbf{base}
of a numeral system.
The ubiquitous decimal numeral system as we know has the base of 10,
while the binaries that can be found in our machines nowadays has the base of 2.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ | l | l | *{16}{l} | }
    \textbf{Numeral system} & \textbf{Base}  & \multicolumn{16}{c |}{\textbf{Digits} } \\
    \hline
    decimal         & 10 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &    &    &    &    &    &    \\
    binary          & 2  & 0 & 1 &   &   &   &   &   &   &   &   &    &    &    &    &    &    \\
    hexadecimal     & 16 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A  & B  & C  & D  & E  & F  \\
    \hline
    \textbf{Assigned value}  & & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} & \textbf{13} & \textbf{14} & \textbf{15} \\
    \end{tabular}
    \end{adjustbox}
\end{center}

A numeral system of base $n$ has exactly $n$ digits, which are assigned values
from $0$ to $n-1$.

Conventionally, the base of a system is annotated by subscripting it to the
right of a numeral, like $ ({2016})_{10} $.
We replace the parenthesis with a fancy pair of semantics brackets,
like $ [\![ 2016 ]\!]_{10} $ to emphasize its role as the evaluation function.

To evaluate a notation of a certain base:
$$
    [\![d_0d_1d_2...d_n]\!]_{base}
    =
    \bar{d_0}\times base^0 + \bar{d_1}\times base^1 + \bar{d_2}\times base^2 + ... + \bar{d_n}\times base^n
$$
%
where $ d_{n} $ is a digit for all $ n $.

\section{Unary Numbers and Peano Numbers}

Some computer scientists and mathematicians seem to be more comfortable with
unary (base-1) numbers because they are isomorphic to the natural numbers à la Peano.

$$
    [\![1111]\!]_{1} \cong
        \overbrace{\text{suc (suc (suc (suc}}^4 + \text{ zero)))}
$$

Statements established on such construction can be proven using mathematical
induction. Moreover, people have implemented and proven a great deal of functions
and properties on these unary numbers because they are easy to work with.

\paragraph{Problem}
If we are to evaluate unary numerals with the model we have just settled,
the only digit of the unary system would have to be assigned to $ 0 $ and
every numeral would evaluate to zero as a result.

The definition of digit assignments can be modified to allow unary digits to
start counting from $ 1 $.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ | l | l | *{16}{l} | }
    \textbf{Numeral system} & \textbf{Base}  & \multicolumn{16}{c |}{\textbf{Digits} } \\
    \hline
    decimal         & 10 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &    &    &    &    &    &    \\
    binary          & 2  & 0 & 1 &   &   &   &   &   &   &   &   &    &    &    &    &    &    \\
    hexadecimal     & 16 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A  & B  & C  & D  & E  & F  \\
    unary           & 1  &   & 1 &   &   &   &   &   &   &   &   &    &    &    &    &    &    \\
    \hline
    \textbf{Assigned value}  & & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} & \textbf{11} & \textbf{12} & \textbf{13} & \textbf{14} & \textbf{15} \\
    \end{tabular}
    \end{adjustbox}
\end{center}

However, the representation for numeral systems would have to be generalized to
manage the inconsistency of digit assignment among systems of different bases.
This generalization will be introduced in Chapter~\ref{generalizations}.

Moreover, theorems developed on natural numbers à la Peano cannot be migrated
to numbers of other representations, although the proofs are mostly identical.
Because the relation and similarities between these two representations we have
observed and described are uttered with a \textit{metalanguage} (English, in
this case), while those proofs are often written in some \textit{object language}.
We will demonstrate how to \textit{encode} propositions expressed in the
metalanguage to an object language that we have constructed and how to manipulate
them

\section{Binary Numerals in Digital Circults}

Suppose we are asked to anwser the questions below.

\vspace{15pt}
\noindent\begin{minipage}{.45\textwidth}
    \begin{center}
        \begin{tabular}{c@{\,}c@{\,}c@{\,}c@{\,}c@{\,}c@{\,}c@{\,}c@{\,}c@{\,}c}
          & 2 & 5 & 3 & 2 & 9 & 8 & 1 & 2 & 3 \\
        + & 3 & 4 & 7 & 8 & 4 & 4 & 2 & 3 & 5 \\
        \hline
          &   &   &   &   &   &   &   &   & ? \\
        %   & 6 & 0 & 1 & 1 & 4 & 2 & 3 & 5 & 8 \\
        \end{tabular}
    \end{center}
\end{minipage}\hfill
\begin{minipage}{.48\textwidth}
    \begin{center}
        \begin{tabular}{c@{\,}c@{\,}c@{\,}c}
              & 1 & 2 & 3 \\
            + &   & 3 & 4 \\
            \hline
              &   &   & ? \\
        \end{tabular}
    \end{center}
\end{minipage}
\vspace{15pt}

It would take much more effort to perform long addition and anwser the question
on the left, because it has greater numbers. The greater a number is,
the longer its notation will be, which in terms determines the time it takes to
perform operations.

Since a system can only have \textbf{finitely many} digits, operations such as
addition on these digits can be implemented in \textbf{constant time}.
Consequently, the time complexity of operations such as long addition on a
numeral would be $ O(lg n) $ at best.
The choice of the base is immaterial as long as it is not unary
(which would degenerate to $ O(n) $).

However, this is not the case for the binary numeral system implemented in
arithmetic logic units (ALU). These digital circuits are designed to perform
fast arithmetics. Regarding addition, it takes only \textit{constant time}.

It seems that either we have been doing long addition wrong since primary school,
or the chip manufacturers have been cheating all the time. But there's a catch!
Because we are capable of what is called \textit{arbitrary-precision arithmetic},
i.e., we could perform calculations on numbers of arbitrary size
while the binary numbers that reside in machines are bounded by the hardware,
which could only perform \textit{fixed-precision arithmetic}.

\paragraph{Problem}
Judging from the time complexity of operations, the binary numerals running in
digital circuits is certainly different from the ordinary binary numerals we have
known.

\section{Numerical representation}

\paragraph{lists and unary numbers}

One may notice that the structure of unary numbers looks suspiciously similar
to that of lists'. Let's compare their definition in Haskell.

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}
data Nat = Zero
         | Suc Nat
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.48\textwidth}
\begin{lstlisting}
data List a = Nil
            | Cons a (List a)
\end{lstlisting}
\end{minipage}

If we replace every {\lstinline|Cons _|} with {\lstinline|Suc|} and
{\lstinline|Nil|} with {\lstinline|Zero|}, then a list becomes an unary number.
This is precisely what the {\lstinline|length|} function,
a homomorphism from lists to unary numbers, does.

\tikzset{
    one/.style = {draw, circle, inner sep=0pt, minimum size=10pt, fill=black},
    zero/.style = {draw, circle, inner sep=0pt, minimum size=10pt},
    cons/.style = {draw, circle, inner sep=0pt, minimum size=25pt, font=\scriptsize},
    nil/.style = {draw, rectangle, inner sep=0pt, minimum size=20pt, font=\scriptsize},
    txt/.style = {circle}
}

\begin{center}
    \begin{tikzpicture}[edge from parent/.style = {draw, -latex}]
        \node[cons]{cons}
            child[grow=right] {node[cons]{cons}
                child[grow=right] {node[cons]{cons}
                    child[grow=right] {node[cons]{cons}
                        child[grow=right] {node[nil]{nil}
                            child[grow=down] {node[txt]{zero} edge from parent[-, dashed]}
                        }
                        child[grow=down] {node[txt]{suc} edge from parent[-, dashed]}
                    }
                    child[grow=down] {node[txt]{suc} edge from parent[-, dashed]}
                }
                child[grow=down] {node[txt]{suc} edge from parent[-, dashed]}
            }
            child[grow=down] {node[txt]{suc} edge from parent[-, dashed]};
    \end{tikzpicture}
\end{center}

Now let's compare addition on unary numbers and merge (append) on lists:

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
add : Nat → Nat → Nat
add Zero    y = y
add (Suc x) y =
    Suc (add x y)
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.48\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
append : List a → List a → List a
append Nil         ys = ys
append (Cons x xs) ys =
    Cons x (append xs ys)
\end{lstlisting}
\end{minipage}

Aside from having virtually identical implementations, operations on unary numbers
and lists both have the same time complexity. Incrementing a unary number takes
$ O(1) $, inserting an element into a list also takes $ O(1) $; adding two unary
numbers takes $ O(n) $, appending a list to another also takes $ O(n) $.

\paragraph{binomial heaps and binary numbers}

If we look at implementations and operations of binary numbers and binomial
heaps, the resemblances are also uncanny.

\begin{center}
    \begin{tikzpicture}[
        edge from parent/.style = {draw, -latex},
        node distance=40pt
    ]
        \node[one] (A) {}
            child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
            child[grow=right] {node[zero] (B) {}
                child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
                child[grow=right] {node[one, xshift=40pt] (C) {}
                    child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                    child[grow=down] {node[one] (C-1) {}}
                    child {node[one, left of = C-1] {}
                        child[grow=down] {node[one]{}}
                    }
                    child[grow=right] {node[one, xshift=120pt] (D) {}
                        child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                        child[grow=down] {node[one] (D-1) {}}
                        child {node[one, left of = D-1] (D-2) {}
                            child[grow=down] {node[one]{}}
                        }
                        child {node[one, left of = D-2] (D-3) {}
                            child[grow=down] {node[one] (D-3-1) {}}
                            child {node[one, left of = D-3-1] {}
                                child[grow=down] {node[one]{}}
                            }
                        }
                    }
                }
            };
    \end{tikzpicture}
\end{center}

The figure above is a binomial heap containing $ 13 $ elements.
\footnote{Nodes that contain elements are painted black.}
From left to right, there are \textit{binomial trees} of different \textit{ranks}
attached to the path that we call \textit{``the spine''}.
A binomial heap is composed of binomial trees just as a numeral is composed
of digits. If we read the nodes with binomial trees as $ 1 $ and those without
as $ 0 $, then we get the numeral of $ 13 $ in binary.

\paragraph{building blocks}

Single cells in lists and binomial trees in binomial heaps are all different
kind of simple data structures that are called \textit{building blocks}.
There are also other kinds of building blocks, such as perfect leaf trees.

\begin{center}
    \begin{tikzpicture}[
        edge from parent/.style = {draw, -latex},
        level/.style={sibling distance = 80pt/#1},
        level distance = 20pt
    ]
        \node[zero]{}
            child {node[zero]{}
                child {node[zero]{}
                    child {node[one]{}}
                    child {node[one]{}}
                }
                child {node[zero]{}
                    child {node[one]{}}
                    child {node[one]{}}
                }
            }
            child {node[zero]{}
                child {node[zero]{}
                    child {node[one]{}}
                    child {node[one]{}}
                }
                child {node[zero]{}
                    child {node[one]{}}
                    child {node[one]{}}
                }
            };
    \end{tikzpicture}
\end{center}

These building blocks can have different ranks.
A binary leaf tree of rank n, for instance, would contain $ 2^n $ elements.
The data structures we have addressed so far are all composed of a series of
building blocks that are ordered by their ranks.

However, these building blocks do not necessarily have to be binary based,
as long as multiple building blocks of the same rank can be merged into a
building block of a higher rank or vice versa.

\paragraph{random access lists and binary numbers}

Accessing an element on lists typically takes $ O(n) $.
Instead of using single cells, \textit{random access lists} adopts perfect
leaf trees as building blocks.
This improves the time complexity of random access from $ O(n) $ to $ O(lg n) $
as a random access list would have at most $ O(lg n) $ building blocks, and
the tallest perfect leaf tree takes at most $ O(lglg n) $ to traverse.

\tikzset{
    cltree/.style = {draw,
        font=\scriptsize,
        isosceles triangle, shape border rotate=90}
}
\begin{center}
    \begin{tikzpicture}[
        edge from parent/.style = {draw, -latex},
        node distance = 60pt
    ]
        \node[one](A){}
            child {node[zero, right of = A](B){}
                child {node[one, right of = B](C){}
                    child {node[one, right of = C](D){}
                        child {
                            node[cltree, scale=2.8, yshift=-5pt] {8} edge from parent[draw=none]
                        }
                        { node[cltree, scale=2] {4} }
                    }
                }
                { node[cltree, scale=1, yshift=20pt] {1} }
            };
    \end{tikzpicture}
\end{center}

Similar to that of binomial heaps, random access lists also have spines.
Also, treating building blocks as digits also yields binary numerals of the
container's size.

\subsection{The correspondence}

The strong analogy between data structures and positional numeral systems
suggests that numeral systems can serve as templates for designing containers.
Such data structures are called \textbf{Numerical Representations}\cite{okasaki1996purely}
\cite{hinze1998numerical}.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ l l l }
    a container of size $ n $ & corresponds to & a numeral of $ n $ \\
    a building block of rank $ n $ & corresponds to & a digit of weight $ base^n $ \\
    inserting an element      & corresponds to & incrementing a numeral \\
    merging two containers    & corresponds to & adding two numerals \\
    \end{tabular}
    \end{adjustbox}
\end{center}

\paragraph{Problem}

Retrieving the first element ({\lstinline|head|}) of a list typically takes
only constant time. On the other hand, it takes $ O(lg n) $ on random access lists.
To illustrate the problem, consider a random access list with $ 32 $ elements:

\begin{center}
    \begin{tikzpicture}[
        edge from parent/.style = {draw, -latex},
        node distance = 60pt
    ]
        \node[zero]{}
            child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
            child[grow=right] {node[zero]{}
                child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
                child[grow=right] {node[zero]{}
                    child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
                    child[grow=right] {node[zero]{}
                        child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
                        child[grow=right] {node[zero]{}
                            child[grow=up] {node[txt]{0} edge from parent[-, dashed]}
                            child[grow=right] {node[one]{}
                                child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                                { node[cltree, scale=1, yshift=-20pt] {32} }
                            }
                        }
                    }
                }
            };
    \end{tikzpicture}
\end{center}

To access any elements from the list above, we have to skip
through four empty nodes before reaching the first building block.
Nodes that correspond to the digit ``0'' contain no elements.
They are not only ineffective but also hinders traversal.

However, if we replace the digits ``0'' and ``1'' with ``1'' and ``2'',
then the number $ 32 $ can be represented as $ 21111 $ instead of $ 000001 $.
By doing so, we eliminate empty nodes and shorten the spine, thus improving
the performance of list traversal.

\begin{center}
    \begin{tikzpicture}[
        edge from parent/.style = {draw, -latex},
    ]
        \node[one](root){}
            child[grow=up] {node[txt]{2} edge from parent[-, dashed]}
            child[grow=right] {node[one]{}
                child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                child[grow=right] {node[one]{}
                    child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                    child[grow=right] {node[one]{}
                        child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                        child[grow=right] {node[one]{}
                            child[grow=up] {node[txt]{1} edge from parent[-, dashed]}
                            { node[cltree, scale=2, yshift=-25pt] {16} }
                        }
                        { node[cltree, scale=1.68, yshift=-23pt] {8} }
                    }
                    { node[cltree, scale=1.41, yshift=-22pt] {4} }
                }
                { node[cltree, scale=1.19, yshift=-21pt] {2} }
            }
            child {node[txt, below right = 16pt and 11pt of root]{}
                { node[cltree, scale=0.8, below right = 28pt and 5pt of root] {1} }
            }
            child {node[txt, below left = 16pt and 11pt of root]{}
                { node[cltree, scale=0.8, below left = 28pt and 5pt of root] {1} }
            };
    \end{tikzpicture}
\end{center}

The data structure introduced above is called the \textit{1-2 random access list}.
Its presence suggests that a binary numeral system with digits ``1'' and ``2''
should be admissible.

Hinze argues\cite{hinze1998numerical} that if we add the digit ``0'' back to the
\textit{1-2 random access list}, then the resulting numerical representation,
so-called \textit{0-1-2 random access list},
would even have a better performance of insertion and deletion in certain edge cases.

To accommodate these numerical representations, we need a more versatile
representation for numeral systems.

\section{Motivation and Outline}

We started off with numerical representations, but then we found that their
corresponding numeral systems alone are interesting enough.

\begin{itemize}
    \item How to capture the numeral systems behind these data structures?
    \item What are these numeral systems capable of?
    \item Which kinds of numeral systems are suitable for modelling numerical
        representations? Do they support increment (insertion) or addition (merge)?
    \item What are the relations between these numeral systems and the natural numbers?
    \item How to translate propositions and proofs from the natural numbers to
        our representation?
\end{itemize}

The remainder of the thesis is organized as follows.

\begin{description}
    \item[Chapter~\ref{generalizations}]
        resolves the problems we have addressed in this chapter by proposing
        some generalizations to the conventional positional numeral systems.
   \item[Chapter~\ref{agda}]
        gives an introduction to \textit{Agda}, the language we use to construct
        and reason about the representation.
   \item[Chapter~\ref{props}]
        introduces \textit{equational reasoning} and relevant properties
        of natural numbers exercised in the coming chapters.
   \item[Chapter~\ref{constructions}]
        constructs the representation for these numeral systems,
        searches for suitable systems for modeling data structures by
        defining operations such as increment and addition,
        and investigates the relationships between these systems and the natural numbers.
    \item[Chapter~\ref{translation}]
        demonstrates how to translate propositions and proofs from the natural
        numbers à la Peano to our representation of numbers with universe
        constructions.
    \item[Chapter~\ref{conclusion}] discusses some related topics and concludes
        everything.
\end{description}

\end{document}