% !TEX root = main.tex

%-------------------------------------------------
\section{Probability measures}\label{sec:}

%Let $\Omega$ be the sample space of some random experiment.

\begin{definition}[Kolmogorov's axioms]\label{def:prob_meas}
A \emph{probability measure} on a set $\Omega$ is a function which maps subsets of $\Omega$ to numbers in the interval $[0,1]$ such that $\prob(\emptyset) = 0$, $\prob(\Omega) = 1$ and for any sequence of pairwise disjoint events $A_1,A_2,\ldots$,
\[
\prob\left(\bigcup_{i=1}^\infty A_i\right) = \sum_{i=1}^{\infty} \prob(A_i)
\qquad\text{(countable additivity)}.
\]
The pair $(\Omega,\prob)$ is called a \emph{probability space}.
\end{definition}

\begin{theorem}[Properties of probability measures]\label{thm:props_pmeas}
Probability measures have the following properties.
\ben
\it Complementarity: $\prob(A^c) = 1 - \prob(A)$.
\it Monotonicity: if $A\subseteq B$ then $\prob(A)\leq \prob(B)$.
\it Addition rule: $\prob(A\cup B) = \prob(A) + \prob(B) - \prob(A\cap B)$.
\een
 \end{theorem}

% proof
\begin{proof}
\ben
\it Because $A\cup A^c=\Omega$ is a disjoint union and $\prob(\Omega)=1$, it follows by additivity that
\[
1 = \prob(\Omega) = \prob(A\cup A^c) = \prob(A) + \prob(A^c).
\]
\it Let $A\subseteq B$. We can write $B$ as the disjoint union $A\cup(B\setminus A)$, so by additivity
\[
\prob(B) = \prob\big[A\cup (B\setminus A)\big] = \prob(A) + \prob(B\setminus A).
\]
Thus because $\prob(B\setminus A)\geq 0$ we have $\prob(B) \geq \prob(A)$.
\it We can write the three sets as disjoint unions:
\bit
\it $A\cup B = (A\setminus B) \cup (B\setminus A) \cup (A\cap B)$
\it $A 		 = (A\setminus B) + (A\cap B)$
\it $B 		 = (B\setminus A) + (A\cap B)$
\eit
By additivity,
\bit
\it $\prob(A\cup B) = \prob(A\setminus B) + \prob(B\setminus A) + \prob(A\cap B)$
\it $\prob(A) 		= \prob(A\setminus B) + \prob(A\cap B)$
\it $\prob(B)		= \prob(B\setminus A) + \prob(A\cap B)$
\eit
Hence $\prob(A\cup B) = \prob(A) + \prob(B) - \prob(A\cap B)$, as required.
\een
\end{proof}

%-----------------------------
%\subsection{Continuity}

\begin{theorem}[Continuity of probability measures]\label{thm:cont_pms}
Let $\prob$ be a probability measure on $\Omega$.
\ben
\it If $A_1\subseteq A_2\subseteq \ldots$ is an increasing sequence of subsets then
$\displaystyle\prob\left(\medcup_{n=1}^{\infty} A_n\right) = \lim_{n\to\infty}\prob(A_n)$.
\it If $B_1\supseteq B_2\supseteq \ldots$ is a decreasing sequence of subsets then
$\displaystyle\prob\left(\medcap_{n=1}^{\infty} B_n\right) = \lim_{n\to\infty}\prob(B_n)$.
\een
\end{theorem}

% proof
\begin{proof}
\ben
\it Let $A=\bigcup_{n=1}^{\infty} A_n$. We can write $A$ as a disjoint union:
\[
A = A_1 \cup (A_2\setminus A_1) \cup (A_3\setminus A_2) \cup \ldots
\]
Because the sets $A_{n+1}\setminus A_n$ are disjoint, by countable additivity we have
\begin{equation}\label{eq:onion}
\prob(A) = \prob(A_1) + \prob(A_2\setminus A_1) + \prob(A_3\setminus A_2) + \ldots \tag{*}
\end{equation}

Furthermore, $A_n\subseteq A_{n+1}$ means that $A_{n+1}=(A_{n+1}\setminus A_n)\cup A_n$ is a disjoint union, so
\[
\prob(A_{n+1}\setminus A_n)=\prob(A_{n+1})-\prob(A_n).
\]
Substituting this into Eq. (\ref{eq:onion}),
\begin{align*}
\prob(A)
	& = \prob(A_1) + \big[\prob(A_2) - \prob(A_1)\big] + \big[\prob(A_3) - \prob(A_2)\big] + \ldots \\
	& = \big[\prob(A_1) - \prob(A_1)\big] + \big[\prob(A_2) - \prob(A_2)\big] + \big[\prob(A_3) - \prob(A_3)\big] + \ldots \\
	& = \lim_{n\to\infty} \prob(A_n).
\end{align*}

\it Let $B=\bigcap_{i=1}^{\infty} B_i$. Then $B_1^c\subseteq B_2^c\subseteq\ldots$ and by De Morgan's laws,
\[
B^c = \medcup_{n=1}^{\infty} B_n^c.
\]
By the first part of the theorem, $\prob(B^c) = \lim_{n\to\infty}\prob(B_n^c)$, so
\[
\prob(B)
	= 1 - \prob(B^c)
	= 1 - \lim_{n\to\infty} \prob(B_n^c)
	= \lim_{n\to\infty} [1-\prob(B_n^c)]
	= \lim_{n\to\infty} \prob(B_n)
\]
as required.
\een
\end{proof}

\begin{example}
A fair coin is tossed repeatedly. Show that a head eventually occurs with probability one.
\begin{solution}
Let $A_n$ be the event that a head occurs during the first $n$ tosses, and let $A$ be the event that a head eventually occurs. Then $A_1\subset A_2\subset A_3,\ldots$ is an increasing sequence with
\[
A=\bigcup_{n=1}^{\infty} A_n.
\]
By the continuity property of probability measures,
\[
\prob(A)
	= \prob\left(\bigcup_{n=1}^{\infty}A_n\right)
	= \lim_{n\to\infty} \prob(A_n)
	= \lim_{n\to\infty} \left(1 - \frac{1}{2^n}\right) = 1,
\]
where we have assumed that the tosses are independent, so that
\[
\prob(A_n) = 1 - \prob(\text{no heads in the first $n$ tosses}) = 1 - (1/2)^n.
\]
\end{solution}
\end{example}

%-----------------------------
\begin{exercise}
\begin{questions}
% inclusion-exclusion
\question
\begin{parts}
\part
For any finite collection of events $A_1,A_2,\ldots,A_n$ (where $n\geq 2$), use proof by induction to show that
\small
\[
\prob\left(\bigcup_{i=1}^{n} A_i\right)
	= \sum_i\prob(A_i) - \sum_{i<j}\prob(A_i\cap A_j) + \sum_{i<j<k}\prob(A_i\cap A_j\cap A_k)
		+ \ \ldots\  + (-1)^{n+1}\prob(A_1\cap A_2\cap\ldots\cap A_n).
\]
\normalsize
This is called the \emph{inclusion-exclusion principle}.
\begin{answer}
By Theorem~\ref{thm:props_pmeas} the result is true for $n=2$. Let $m\geq 2$ and suppose the result is true for all $n\leq m$.
Because Theorem~\ref{thm:props_pmeas} result holds for pairs of events it holds for the pair $\cup_{i=1}^n A_i$ and $A_{n+1}$ so
\begin{align*}
\prob\left(\bigcup_{i=1}^{m+1} A_i\right)
	& = \prob\left(\bigcup_{i=1}^{m} A_i\right) +\prob(A_{m+1}) - \prob\left[\left(\bigcup_{i=1}^{m} A_i\right)\cap A_{m+1}\right] \\
	& = \prob\left(\bigcup_{i=1}^{m} A_i\right) +\prob(A_{m+1}) - \prob\left[\bigcup_{i=1}^{m} (A_i\cap A_{m+1})\right].
\end{align*}
By the inductive hypothesis, we can expand the first and last terms on the right-hand side to obtain the result.
\end{answer}
\part
Suppose that at least one of the events $A_1,A_2,\ldots,A_n$ is certain to occur, and that more than two will certainly not occur. If $\prob(A_i)=p$ and $\prob(A_i\cap A_j) = q$ for $i\neq j$, use the inclusion-exclusion principle to show that $p\geq 1/n$ and $q\leq 2/n$.
\begin{answer}
We know that $\prob(\cup_{i=1}^{n} A_i) = 1$ and $\prob(A_i\cap A_j\cap A_k)=0$ whenever $i\neq j\neq k$. By the inclusion-exclusion principle,
\[
1 = \prob\left(\bigcup_{i=1}^{n} A_i\right)
	= \sum_i\prob(A_i) - \sum_{i<j}\prob(A_i\cap A_j)
	= np - \frac{1}{2}n(n-1)q.
\]
The second term is negative so we must have $np\geq 1$, which shows that $p\geq 1/n$. Hence
\[
\frac{1}{2}n(n-1)q = np - 1 \leq n-1, \text{ so } \frac{nq}{2} \leq 1 \text{ and thus $q\leq 2/n$, as required}.
\]
\end{answer}
\end{parts}

% subadditivity
\question
For any countable collection of events $A_1,A_2,\ldots$ show that
\[
\displaystyle
\mathbb{P}\left(\bigcup_{i=1}^{\infty} A_i\right) \leq \sum_{i=1}^{\infty} \mathbb{P}(A_i).
\]
This property is called (countable) \emph{subadditivity}.
\begin{answer}
We will find a sequence of sets $B_1,B_2,\ldots$ whose union coincides with the union of $A_1,A_2,\ldots$, but which are disjoint so that the countable additivity property of probability measures can be applied.

Let $B_1 = A_1$, $B_2 = A_2\setminus A_1$, $B_3 = A_3\setminus(A_1\cup A_2)$, and in general
\[
B_i = A_i \setminus \left(\bigcup_{j=1}^{i-1} A_j\right).
\]

\textbf{Claim 1}: $\cup_{i=1}^{\infty} A_i = \cup_{i=1}^{\infty} B_i$.
To see this, let $\omega\in\cup_{i=1 }^{\infty} B_i$. Then there exists some $B_j$ with $\omega\in B_j$, which implies that $\omega\in A_j$ and hence $\omega\in\cup_{i=1}^{\infty} A_i$. Thus $\cup_{i=1}^{\infty} B_i \subseteq \cup_{i=1}^{\infty} A_i$. Conversely, let $\omega\in\cup_{i=1 }^{\infty} A_i$. Then there exists some $A_j$ with $\omega\in A_j$, which implies that $\omega\in \cup_{i=1}^{j} B_i$ and hence $\omega\in\cup_{i=1}^{\infty} B_i$. Thus $\cup_{i=1}^{\infty} A_i \subseteq \cup_{i=1}^{\infty} B_i$.
\begin{itemize}
\item
This means that $\mathbb{P}\left(\cup_{i=1}^{\infty} A_i\right) = \mathbb{P}\left(\cup_{i=1}^{\infty} B_i\right)$.
\end{itemize}

\textbf{Claim 2}: $B_i \subseteq A_i$. This follows because
\[
B_i = A_i \setminus \left(\bigcup_{j=1}^{i-1} A_j\right) = A_i \cap\left(\bigcup_{j=1}^{i-1} A_j\right)^{c} \subseteq A_i.
\]
By the monotonicity of probability measures, this means that $\mathbb{P}(B_i)\leq\mathbb{P}(A_i)$ for all $i=1,2,\ldots$.

\textbf{Claim 3}: the sets $B_1,B_2,\ldots$ are pairwise disjoint. To see this, consider any two sets $B_i$ and $B_j$ and suppose, without loss of generality, that $i<j$. Then for any $\omega\in B_i$ it follows that $\omega\in A_i$, so $\omega\notin B_j$ (because $i<j$, and $B_j$ excludes all outcomes contained in $A_1,A_2,\ldots,A_{j-1}$). Hence $B_i$ and $B_j$ are disjoint.

Thus, by the countable additivity of probability measures,
\[
\mathbb{P}\left(\bigcup_{i=1}^{\infty} A_i\right)
	= \mathbb{P}\left(\bigcup_{i=1}^{\infty} B_i\right)
	= \sum_{i=1}^{\infty} \mathbb{P}(B_i)%	\qquad\text{by countable additivity}
	\leq \sum_{i=1}^{\infty} \mathbb{P}(A_i),
\]
as required.
\end{answer}

% continuity (GS 1.3.2)
\question
A fair coin is tossed repeatedly. Using the continuity of probability measures show that
\begin{parts}
\part a head eventually occurs with probability one,
\begin{answer}
Let $B_n$ be the event that no heads occur in the first $n$ tosses, and let $B$ be the event that no heads occur at all. Then $B_1\supseteq B_2\supset \ldots$ is a decreasing sequence and $B=\cap_{i=1}^{\infty} B_n$. THence by continuity,
\[
\prob(B) = \prob\left(\bigcap_{n=1}^{\infty}B_n\right)
	= \lim_{n\to\infty} \prob(B_n)
	= \lim_{n\to\infty} \left(\frac{1}{2}\right)^n = 0,
\]
Thus we are certain of eventually observing a head.
\end{answer}
%--------------------
\part a sequence of 10 consecutive tails eventually occurs with probability one, and
\begin{answer}
Let us think of the first $10n$ tosses as disjoint groups of consecutive outcomes, each group of length $10$. The probability any one of the $n$ groups consists of 10 consecutive tails is $2^{-10}$, independently of the other groups. The event that one of the groups consists of 10 consecutive tails is a subset of the event that a sequence of 10 consecutive tails appears anywhere in the first $10n$ tosses. Hence, using the continuity of probability measures,
\begin{align*}
\prob(\text{$10T$ eventually appears})
	& = \lim_{n\to\infty} \prob(\text{$10T$ occurs somewhere in the first $10n$ tosses)} \\
	& \geq \lim_{n\to\infty} \prob(\text{$10T$ occurs as one of the first $n$ groups of $10$}) \\
	& = 1 - \lim_{n\to\infty} \prob(\text{$10T$ does not occur as one of the first $n$ groups of 10}) \\
 	& = 1 - \lim_{n\to\infty} \left(1-\frac{1}{2^{10}}\right)^n = 1.
\end{align*}
Thus we are certain of eventually observing sequence of 10 consecutive tails.
\end{answer}
%--------------------
\part any finite sequence of heads and tails eventually occurs with probability one.
\begin{answer}
Let $s$ be a fixed sequence of length $k$. As in the previous part, we think of the first $kn$ tosses as $n$ distinct groups of length $k$. The event that the one of these groups is exactly equal to $s$ is a subset of the event that first $kn$ tosses contains at least one instance of $s$. Hence
\begin{align*}
\prob(\text{$s$ eventually appears})
	& = \lim_{n\to\infty} \prob(\text{$s$ occurs somewhere in the first $kn$ tosses)} \\
	& \geq \lim_{n\to\infty} \prob(\text{$s$ occurs as one of the first $n$ groups of $k$}) \\
	& = 1 - \lim_{n\to\infty} \prob(\text{$s$ does not occur as one of the first $n$ groups of $k$}) \\
 	& = 1 - \lim_{n\to\infty} \left(1-\frac{1}{2^{k}}\right)^n = 1.
\end{align*}
Thus we are certain of eventually observing the sequence $s$.
\bit
\it In an infinite sequence of coin tosses, anything that can happen, does happen!
\eit
\end{answer}
\end{parts}

\end{questions}
\end{exercise}


In this section we will define a model for an algebra to show that the REA (Resource Entity Action) model can be reduced to a problem of linear algebra. We then show how games that use this model can be further simplified by linguistic constructs.

\subsection{Action algebra}

An action consists of a transfer of resources from a source entity to one or more target entities. We require that each entity has a resource vector, which contains the current amount of resources of the entity. The resource vector is sparse, since most actions involve only few resource types. An action is expressed by a transformation matrix $A$.

Consider a set of target entities $T = \left\lbrace t_{1},t_{2},...,t_{n}\right\rbrace$, which are the targets of the action, and a source entity $e$. Each entity $t_{i}$ (including the source entity type) has a resource vector
$\mathbf{r_{i}}=\left(r_{i_{1}},r_{i_{2}},...,r_{i_{m}} \right)$. The source entity also has a transformation matrix $A$ of size $m \times m$, which defines the interactions between all the resources of the source entity and all the resources of the target entities. We also consider an integrator $dt$ which contains the time difference between the current frame and the previous one. We then compute $\mathbf{w_{e}} = \left(  w_{e_{1}},w_{e_{2}},...,w_{e_{m}} \right) = \mathbf{r_{s}} \times A \cdot dt$. From the definition of matrix multiplication, it immediately follows that each component of $ \mathbf{w_{e}}$ represents how a resource will change by applying the effect of all the other resources to it. We compute the vector $\mathbf{r'_{i}} = \mathbf{r_{i}} + \mathbf{w_{e}} \; \forall e_{i} \in E$
which replaces the resource vector in each target entity.

For instance, consider the action of a spaceship (entity) using laser to damage (resource) an enemy spaceship (entity). This involves a  vector resource of two elements: laser and life points. The action must transfer laser points to subtract from the enemy life points. Suppose that the vector resource of the targeting ship is $r_{s} = (20,500)$ and the vector resource of the targeted ship is $r_{t} = (15,1000)$. Let the transformation matrix be
$A =
\begin{bmatrix}
0 & -1\\
0 & 0\\
\end{bmatrix}
$
which means that the source entity will reduce the life of the target by the number of its laser points.
Thus $w_{e} = r_{s} \times A  \cdot dt = (20,500) \times A  \cdot dt = (0,-20) \cdot dt$. At this point, assuming $dt = 1$ second, we have $r'_{t} = r_{t} + w_{e} = (20,1000) + (0,-20) \cdot dt = (20,980)$.

\subsection{A declarative language extension}

We now describe a language extension that implements the REA design pattern and its associated algebra for the Casanova game programming language \citep{Casanova}. The language extension is purely declarative. Its semantics are described using the SQL query language, which has the advantage of familiarity to most programmers.

%Implementing the action algebra is done using an abstract class which contains an abstract method which performs the action. Each action is a class which extends the previous abstract class and implements the abstract method. This method will fetch the world looking for the information needed to find what entities are affected by the action execution. Each entity of the game will have a collection of actions it can perform, automatically run by Casanova.

To identify the set of target entities $T$ given a source entity and its action, we create a new type definition called \textit{action}. An action is a declarative construct which is used to describe not only the resource exchange between entities, but also what kinds of entities participate in the exchange. The resource exchange is based on \textit{transfers} (Add, Subtract, and Set), while the target determination is based on \textit{predicates}: we filter the game world entities depending on their types, attributes and radius (specifying the distance beyond which the action is not applied). Some actions, called threshold actions, are not continuous and make use of special predicates to delay the execution (Output) until certain conditions are met.

Using actions it is possible to specify an exchange of resources in a fully declarative manner, so that the developer does not have to rewrite similar pieces of code ad hoc for each action.


\section{Experiments}

The following experiments refer to \emph{linearly} and \emph{nonlinearly} separable generated datasets of size 100.

\subsection{Support Vector Classifier}

Below experiments are about the SVC for which I tested different values for the regularization hyperparameter $C$, i.e., from \emph{soft} to \emph{hard margin}, and in case of nonlinearly separable data also different \emph{kernel functions} mentioned above.

\subsubsection{Hinge loss}

\paragraph{Primal formulation}

The experiments results shown in~\ref{primal_svc_hinge_cv_results} referred to \emph{Stochastic Gradient Descent} algorithm are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 and $\beta$, i.e., the \emph{momentum}, equal to 0.4. The batch size is setted to 20. Training is stopped if after 5 iterations the training loss is not lower than the best found so far.

\input{experiments/primal_svc_hinge}

The results provided from the \emph{custom} implementation, i.e., the SGD with different momentum settings, are strongly similar to those of \emph{sklearn} implementation, i.e., \emph{liblinear}~\cite{fan2008liblinear} implementation, in terms of \emph{accuracy} score. More training data points are selected as \emph{support vectors} from the SGD solver but it always requires lower iterations, i.e., epochs, to achieve the same \emph{numerical precision}. \emph{Standard} or \emph{Polyak} and \emph{Nesterov} momentums always perform lower iterations as expected from the theoretical analysis of the convergence rate.

\pagebreak

\paragraph{Linear Dual formulations}

The experiments results shown in~\ref{linear_lagrangian_dual_svc_cv_results} are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 for the \emph{AdaGrad} algorithm. Notice that the \emph{qp} dual refers to the formulation~\eqref{eq:svc_lagrangian_dual}, while the \emph{bcqp} dual refers to the formulation~\eqref{eq:svc_bcqp_lagrangian_dual}.

\input{experiments/linear_dual_svc}

For what about the linear \emph{Wolfe dual} formulation we can immediately notice as higher \emph{regularization hyperparameter} $C$ makes the model harder, so the \emph{custom} implementation of the SMO algorithm and also the \emph{sklearn} implementation, i.e., \emph{libsvm}~\cite{chang2011libsvm} implementation, needs to perform more iterations to achieve the same \emph{numerical precision}; meanwhile the \emph{cvxopt}~\cite{vandenberghe2010cvxopt} seems to be insensitive to the increasing complexity of the model. The results in terms of \emph{accuracy} and number of \emph{support vectors} are strongly similar to each others.

\input{experiments/linear_lagrangian_dual_svc}

For what about the linear \emph{Lagrangian dual} formulation we can see as it seems to be insensitive to the increasing complexity of the model in terms of number of \emph{iterations} but it tends to select many training data points as \emph{support vectors}.

\pagebreak

\paragraph{Nonlinear Dual formulations}

The experiments results shown in~\ref{nonlinear_dual_svc_cv_results} and~\ref{nonlinear_lagrangian_dual_svc_cv_results} are obtained with \emph{d} and \emph{r} hyperparameters equal to 3 and 1 respectively for the \emph{polynomial} kernel; \emph{gamma} is setted to \emph{`scale`} for both \emph{polynomial} and \emph{gaussian RBF} kernels. The experiments results shown in~\ref{nonlinear_lagrangian_dual_svc_cv_results} are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 for the \emph{AdaGrad} algorithm.

\input{experiments/nonlinear_dual_svc}

\input{experiments/nonlinear_lagrangian_dual_svc}

The same considerations made for the previous linear \emph{Wolfe dual} and \emph{Lagrangian dual} formulations are confirmed also in the nonlinearly separable case. In this setting the complexity of the model coming with higher $C$ regularization values seems to be not paying a tradeoff in terms of the number of \emph{iterations} of the algorithm and, moreover, the \emph{bcqp Lagrangian dual} formulation seems to perform better wrt the \emph{qp} formulation, both tends to select even more training data points as \emph{support vectors}.

\subsubsection{Squared Hinge loss}

\paragraph{Primal formulation}

The experiments results shown in~\ref{primal_svc_squared_hinge_cv_results} referred to \emph{Stochastic Gradient Descent} algorithm are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 and $\beta$, i.e., the \emph{momentum}, equal to 0.4. The batch size is setted to 20. Training is stopped if after 5 iterations the training loss is not lower than the best found so far.

\input{experiments/primal_svc_squared_hinge}

Again, the results provided from the \emph{custom} implementation, i.e., the SGD with different momentum settings, are strongly similar to those of \emph{sklearn} implementation, i.e., \emph{liblinear}~\cite{fan2008liblinear} implementation, in terms of \emph{accuracy} score. More training data points are selected as \emph{support vectors} from the SGD solver but it always requires even lower iterations, i.e., epochs, to achieve the same \emph{numerical precision}. \emph{Standard} or \emph{Polyak} and \emph{Nesterov} momentums always perform lower iterations as expected from the theoretical analysis of the convergence rate.

\pagebreak

\subsection{Support Vector Regression}

Below experiments are about the SVR for which I tested different values for regularization hyperparameter $C$, i.e., from \emph{soft} to \emph{hard margin}, the $\epsilon$ penalty value and in case of nonlinearly separable data also different \emph{kernel functions} mentioned above.

\subsubsection{Epsilon-insensitive loss}

\paragraph{Primal formulation}

The experiments results shown in~\ref{primal_svr_eps_cv_results} referred to \emph{Stochastic Gradient Descent} algorithm are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 and $\beta$, i.e., the \emph{momentum}, equal to 0.4. The batch size is setted to 20. Training is stopped if after 5 iterations the training loss is not lower than the best found so far.

\input{experiments/primal_svr_eps}

The results provided from the \emph{custom} implementation, i.e., the SGD with different momentum settings, are strongly similar to those of \emph{sklearn} implementation, i.e., \emph{liblinear}~\cite{fan2008liblinear} implementation, in terms of \emph{r2} score, except in case of $C$ regularization hyperparameter equals to 1 for which those of SGD are lower. Moreover, the SGD solver always requires lower iterations, i.e., epochs, for higher $C$ regularization values, i.e., for $C$ equals to 10 or 100, to achieve the same \emph{numerical precision}. Again, \emph{Standard} or \emph{Polyak} and \emph{Nesterov} momentums always perform lower iterations as expected from the theoretical analysis of the convergence rate. The results in terms of \emph{support vectors} are strongly similar to each others.

\paragraph{Linear Dual formulations}

The experiments results shown in~\ref{linear_lagrangian_dual_svr_cv_results} are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 for the \emph{AdaGrad} algorithm. Notice that the \emph{qp} dual refers to the formulation~\eqref{eq:svr_lagrangian_dual}, while the \emph{bcqp} dual refers to the formulation~\eqref{eq:svr_bcqp_lagrangian_dual}.

\input{experiments/linear_dual_svr}

For what about the linear \emph{Wolfe dual} formulation we can immediately notice as higher \emph{regularization hyperparameter} $C$ and lower $\epsilon$ values makes the model harder, so the \emph{custom} implementation of the SMO algorithm and also the \emph{sklearn} implementation, i.e., \emph{libsvm}~\cite{chang2011libsvm} implementation, needs to perform more iterations to achieve the same \emph{numerical precision}; meanwhile, again, the \emph{cvxopt}~\cite{vandenberghe2010cvxopt} seems to be insensitive to the increasing complexity of the model. The results in terms of \emph{r2} and number of \emph{support vectors} are strongly similar to each others.

\input{experiments/linear_lagrangian_dual_svr}

For what about the linear \emph{Lagrangian dual} formulation we can see as it seems to be insensitive to the increasing complexity of the model in terms of number of \emph{iterations} and require many \emph{iterations} wrt the \emph{Wolfe dual} formulation.

\paragraph{Nonlinear Dual formulations}

The experiments results shown in~\ref{nonlinear_dual_svr_cv_results} and~\ref{nonlinear_lagrangian_dual_svr_cv_results} are obtained with \emph{d} and \emph{r} hyperparameters both equal to 3 for the \emph{polynomial} kernel; \emph{gamma} is setted to \emph{`scale`} for both \emph{polynomial} and \emph{gaussian RBF} kernels. The experiments results shown in~\ref{nonlinear_lagrangian_dual_svc_cv_results} are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 for the \emph{AdaGrad} algorithm.

\input{experiments/nonlinear_dual_svr}

\input{experiments/nonlinear_lagrangian_dual_svr}

The same considerations made for the previous linear \emph{Wolfe dual} and \emph{Lagrangian dual} formulations are confirmed also in the nonlinearly separable case. In this setting, the complexity of the model coming with higher $C$ regularization hyperparameters and lower $\epsilon$ values pays a larger tradeoff in terms of the number of \emph{iterations} of the algorithm.

\subsubsection{Squared Epsilon-insensitive loss}

\paragraph{Primal formulation}

The experiments results shown in~\ref{primal_svr_squared_eps_cv_results} referred to \emph{Stochastic Gradient Descent} algorithm are obtained with $\alpha$, i.e., the \emph{learning rate} or \emph{step size}, setted to 0.001 and $\beta$, i.e., the \emph{momentum}, equal to 0.4. The batch size is setted to 20. Training is stopped if after 5 iterations the training loss is not lower than the best found so far.

\input{experiments/primal_svr_squared_eps}

Again, the results provided from the \emph{custom} implementation, i.e., the SGD with different momentum settings, are strongly similar to those of \emph{sklearn} implementation, i.e., \emph{liblinear}~\cite{fan2008liblinear} implementation, in terms of \emph{r2} score. SGD solver always requires even lower iterations, i.e., epochs, for higher $C$ regularization values, i.e., for $C$ equals to 10 or 100, to achieve the same \emph{numerical precision}. \emph{Standard} or \emph{Polyak} and \emph{Nesterov} momentums always perform lower iterations as expected from the theoretical analysis of the convergence rate.


\section{Discussion \label{sec:discussion}}

We have introduced \limdd, a novel decision diagram-based method to simulate quantum circuits, which enables polynomial-size representation of a strict superset of stabilizer states and of the states represented by polynomially-large \qmdds.
To prove this, we have shown the first lower bounds on the size of \qmdds for stabilizer states:
they are exponential-size for certain families of stabilizer states.
\limdds achieve a more succinct representation by representing states up to
local invertible maps which uses single qubit (local) operations from a group $G$.
We have investigated the choices $G=\pauli$, $G=\braket{Z}$ and $G=\braket{X}$,
and found that any choice suffices for an exponential advantage over \qmdds;
notably, the choice $G=\pauli$ allows us to succinctly represent stabilizer states.
We also defined reduction rules for Pauli-\limdd and showed that for each set of
quantum states which are equivalent under local Pauli operations,
modulo normalization factor, has a unique reduced Pauli-\limdd as representative.

Furthermore, we showed how to simulate arbitrary quantum circuits, encoded as Pauli-\limdds.
The resulting algorithms are often faster than for \qmdds.
In contrast to \qmdds, Clifford circuits (initialized to $\ket{0}$)
can be simulated by Pauli-\limdds in polynomial time.
This in itself is not an interesting feat, since efficient simulation methods for
the stabilizer regime have been known since the Gottesman-Knill theorem.
However, we also showed that Pauli-\limdds can efficiently simulate
a circuit family we call Hamming weight-controlled Clifford circuits.
And we provide empirical evidence that these circuits are hard for stabilizer-rank
based simulation methods.
%we were strengthened in this exception by exploratory numerics.

An obvious next step is to investigate other choices for $G$.
Of interest are both the representational capabilities of such diagrams
(do they represent interesting states?), and the algorithmic capabilities
(can we still find efficient algorithms which make use of these diagrams?).
In this vein, an important question is what the relationship is between \glimdds
(for various choices of $G$) and existing formalisms for the classical simulation of quantum circuits, such as those based on match-gates~\cite{terhal2001classical,jozsa2008matchgates,hebenstreit2020computational} and tensor networks~\cite{orus2014practical}.
It would also be interesting to compare \limdds to graphical depictions of quantum computation,
following similar work for \qmdds~\cite{vilmart2021quantum}.
We leave empirical evaluations into the consequences of \limdd methods for efficient
quantum circuit simulation, for future work. This would obviously include a
comparison with stabilizer rank simulation~\cite{bravyi2019simulation}.

Finally, we note that the current definition of \limdd imposes a strict total order
over the qubits along every path from root to leaf.
It is known that the chosen order can greatly influence the size of the DD~\cite{rudell1993dynamic,wegener2000branching},
making it interesting to investigate variants of \limdds with a flexible ordering.


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

\section{Incrementing a Numeral}\label{increment}

Given a numeral \lstinline|xs| of \lstinline|Numeral b d o|
and a proof of \lstinline|¬ (Maximum xs)|,
with functions and theorems constructed in the last section, we can:

\begin{itemize}
    \item Find the next numeral of \lstinline|xs| using \lstinline|next-numeral|.
    \item Know that the next numeral will be greater than \lstinline|xs|
        by \lstinline|next-numeral-is-greater|.
    \item Know that the next numeral will be the least numeral that is greater
        than \lstinline|xs| by \lstinline|next-numeral-is-immediate|.
\end{itemize}

However, none of the theorems above guarantees that the next numeral will be the
\textbf{successor} of \lstinline|xs|, i.e., the next numeral and \lstinline|xs|
differs by only $ 1 $.
For example, we have seen from the previous section that numerals of
\lstinline|GappedEndpoint| of systems of \lstinline|Proper| have no successors.

\begin{figure}[H]
    \centering
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            % the frame
            \path[clip] (5.5, -3) rectangle (17.5, 1.5);

            % ticks
            \draw[ultra thick, loosely dotted] (6.5,-1.5) -- (6.5,1);
            \draw[ultra thick, loosely dotted] (8.5,-1.5) -- (8.5,1);
            \draw[ultra thick, loosely dotted] (14.5,-1.5) -- (14.5,1);
            \draw[ultra thick, loosely dotted] (16.5,-1.5) -- (16.5,1);

            \draw[ultra thick, decoration={brace,mirror,raise=10},decorate]
                (6.5,-1.5) -- (8.5,-1.5);
            \draw[ultra thick, decoration={brace,mirror,raise=10},decorate]
                (14.5,-1.5) -- (16.5,-1.5);
            \node[below=1] at (11.5, -1.5) {\lstinline|differs by more than 1|};

            % the body
            \foreach \i in {0,...,2} {
                \draw[ultra thick, fill=gray] ({\i*8+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+0.95}, {\i*0.6-0.4});
                \foreach \j in {1,...,5} {
                    \draw[ultra thick] ({\i*8+\j+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+\j+0.95}, {\i*0.6-0.4});
                };
                \draw[ultra thick, fill=black] ({\i*8+6.05}, {\i*0.6-0.8}) rectangle ({\i*8+6.95}, {\i*0.6-0.4});
            };

            \coordinate (A) at (7.5, -2.1);
            \coordinate (B) at (9, -2.8);
            \coordinate (C) at (15.5, -2.1);
            \coordinate (D) at (14, -2.8);

            \path[->, ultra thick] (B) edge node {} (A);
            \path[->, ultra thick] (D) edge node {} (C);

        \end{tikzpicture}
    \end{adjustbox}
\caption{The cause of gaps}
\label{figure:26}
\end{figure}

Suppose we are to define a function called \lstinline|increment| that returns
the successor of a given numeral.
For those numerals that are eligible for increment,
we can simply compute them with \lstinline|next-numeral|;
and for those that are not eligible, we need a predicate for discriminating them.

\subsection{\lstinline|Incrementable|}

Similar to the definition of \lstinline|Bounded|, we define the predicate
\lstinline|Incrementable| as an existential proposition.
To prove that a numeral \lstinline|xs| is \lstinline|Incrementable|,
one is obliged to present the successor \textit{and} a proof to justify it.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
Incrementable : ∀ {b d o} → (xs : Numeral b d o) → Set
Incrementable {b} {d} {o} xs = Σ[ xs' ∈ Numeral b d o ] ⟦ xs' ⟧ ≡ suc ⟦ xs ⟧
\end{lstlisting}

We can then develop lemmata and theorems about \lstinline|Incrementable|.

\subsubsection{Maximum Numerals}

If a numeral is a maximum, then there is no such a thing as the next numeral,
much less a successor.

\begin{lstlisting}
Maximum⇒¬Incrementable : ∀ {b d o}
    → (xs : Numeral b d o)
    → (max : Maximum xs)
    → ¬ (Incrementable xs)
Maximum⇒¬Incrementable xs max (incremented , claim)
    = contradiction
        (max incremented)
        (>⇒≰ (m≡1+n⇒m>n claim))
%
\end{lstlisting}
where \lstinline|incremented| is the claimed successor
and \lstinline|claim : ⟦ incremented ⟧ ≡ suc ⟦ xs ⟧|.
This is proven by contradicting these two propositions:

\begin{itemize}
    \item \lstinline|max incremented : ⟦ xs ⟧ ≥ ⟦ incremented ⟧|
    \item \lstinline|>⇒≰ (m≡1+n⇒m>n claim) : ⟦ xs ⟧ ≰ ⟦ incremented ⟧|
\end{itemize}

\subsubsection{Numerals in Intervals}

Numerals that are located in intervals of digit lines have successors.

\begin{figure}[H]
    \centering
        \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            % the frame
            \path[clip] (5.5, -1.5) rectangle (17.5, 1.5);

            % the body
            \foreach \i in {0,...,2} {
                \foreach \j in {0,...,5} {
                    \draw[ultra thick, fill=black] ({\i*8+\j+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+\j+0.95}, {\i*0.6-0.4});
                };
                \draw[ultra thick, fill=gray] ({\i*8+6.05}, {\i*0.6-0.8}) rectangle ({\i*8+6.95}, {\i*0.6-0.4});
            };

            \foreach \i in {5,...,6} {
                \coordinate (\i) at ({\i + 0.5}, -0.9);
            };
            \foreach \i in {8,...,15} {
                \coordinate (\i) at ({\i + 0.5}, -0.3);
            };
            \foreach \i in {16,...,17} {
                \coordinate (\i) at ({\i + 0.5}, 0.3);
            };

            \foreach \i in {5,...,5} {
                \pgfmathsetmacro{\j}{\i + 1}
                \path[->, ultra thick] (\i) edge[bend right=60] node {} ($ (\j) + (-0.1, 0) $);
            };
            \foreach \i in {8,...,13} {
                \pgfmathsetmacro{\j}{\i + 1}
                \path[->, ultra thick] (\i) edge[bend right=60] node {} ($ (\j) + (-0.1, 0) $);
            };
            \foreach \i in {16,...,16} {
                \pgfmathsetmacro{\j}{\i + 1}
                \path[->, ultra thick] (\i) edge[bend right=60] node {} ($ (\j) + (-0.1, 0) $);
            };

        \end{tikzpicture}
    \end{adjustbox}
\caption{The case of intervals}
\label{figure:27}
\end{figure}

We invoke \lstinline|next-numeral-Proper-Interval-lemma| to establish the
successor relation between the given numeral and the next numeral.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
Interval⇒Incrementable : ∀ {b d o}
    → (xs : Numeral (suc b) (suc d) o)
    → (¬greatest : ¬ (Greatest (lsd xs)))
    → (proper : 2 ≤ suc (d + o))
    → Incrementable xs
Interval⇒Incrementable {b} {d} {o} xs ¬greatest proper
    = (next-numeral-Proper xs proper) , (begin
            ⟦ next-numeral-Proper xs proper ⟧
        ≡⟨ cong ⟦_⟧ (next-numeral-Proper-refine xs proper
            (Interval b d o ¬greatest))
        ⟩
            ⟦ next-numeral-Proper-Interval xs ¬greatest proper ⟧
        ≡⟨ next-numeral-Proper-Interval-lemma xs ¬greatest proper ⟩
            suc ⟦ xs ⟧
        ∎)
\end{lstlisting}

\subsubsection{Numerals at Gapped Endpoints}

Numerals that are located at gapped endpoints do not have successors.

\begin{figure}[H]
    \centering
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            % the frame
            \path[clip] (5.5, -1.5) rectangle (17.5, 1.5);

            % the body
            \foreach \i in {0,...,2} {
                \foreach \j in {0,...,5} {
                    \draw[ultra thick] ({\i*8+\j+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+\j+0.95}, {\i*0.6-0.4});
                };
                \draw[ultra thick, fill=black] ({\i*8+6.05}, {\i*0.6-0.8}) rectangle ({\i*8+6.95}, {\i*0.6-0.4});
            };
        \end{tikzpicture}
    \end{adjustbox}
\caption{The case of gapped endpoints}
\label{figure:27}
\end{figure}

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
GappedEndpoint⇒¬Incrementable : ∀ {b d o}
    → (xs : Numeral (suc b) (suc d) o)
    → (greatest : Greatest (lsd xs))
    → (proper : 2 ≤ suc (d + o))
    → (gapped : Gapped xs proper)
    → ¬ (Incrementable xs)
GappedEndpoint⇒¬Incrementable xs greatest proper gapped (incremented , claim)
    = contradiction ⟦next⟧>⟦incremented⟧ ⟦next⟧≯⟦incremented⟧
\end{lstlisting}

This is proven by contradicting two propositions as well.

First, we show that the next numeral is greater than the claimed successor
by rephrasing the lemma \lstinline|next-numeral-Proper-GappedEndpoint-lemma|.
As a side note, \lstinline|next-numeral-Proper| delegates tasks to other helper
functions. \lstinline|next-numeral-Proper-refine| is here to narrow the term
computed by \lstinline|next-numeral-Proper| down to
\lstinline|next-numeral-Proper-GappedEndpoint|
by providing evidence that the computation is delegated that way.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
⟦next⟧>⟦incremented⟧ : ⟦ next-numeral-Proper xs proper ⟧ > ⟦ incremented ⟧
⟦next⟧>⟦incremented⟧ =
    start
        suc ⟦ incremented ⟧
    ≈⟨ cong suc claim ⟩
        suc (suc ⟦ xs ⟧)
    ≤⟨ next-numeral-Proper-GappedEndpoint-lemma xs greatest proper gapped ⟩
        ⟦ next-numeral-Proper-GappedEndpoint xs proper gapped ⟧
    ≈⟨ cong ⟦_⟧ (sym (next-numeral-Proper-refine
        xs proper (GappedEndpoint b d o greatest gapped)))
    ⟩
        ⟦ next-numeral-Proper xs proper ⟧
    □
\end{lstlisting}

However, the next numeral should only be greater than the given numeral
\lstinline|xs| and nothing else.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
⟦next⟧≯⟦incremented⟧ : ⟦ next-numeral-Proper xs proper ⟧ ≯ ⟦ incremented ⟧
⟦next⟧≯⟦incremented⟧ = ≤⇒≯ (next-numeral-is-immediate-Proper
    xs incremented proper (m≡1+n⇒m>n claim))
\end{lstlisting}

\subsubsection{Numerals at Ungapped Endpoints}

Numerals that are located at ungapped endpoints also possess successors.

\begin{figure}[H]
    \centering
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            % the frame
            \path[clip] (6.5, -1.5) rectangle (18.5, 1.5);

            % the body
            \foreach \i in {0,...,2} {
                \draw[ultra thick] ({\i*8+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+0.95}, {\i*0.6-0.4});
                \draw[ultra thick, fill=gray] ({\i*8+1.05}, {\i*0.6-0.8}) rectangle ({\i*8+1.95}, {\i*0.6-0.4});
                \foreach \j in {2,...,7} {
                    \draw[ultra thick] ({\i*8+\j+0.05}, {\i*0.6-0.8}) rectangle ({\i*8+\j+0.95}, {\i*0.6-0.4});
                };
                \draw[ultra thick, fill=black] ({\i*8+8.05}, {\i*0.6-0.8}) rectangle ({\i*8+8.95}, {\i*0.6-0.4});
            };
            % arrows
            \coordinate (A) at (9.1, -0.6);
            \coordinate (B) at (9.5, -0.3);
            \coordinate (C) at (17.1, 0);
            \coordinate (D) at (17.5, 0.3);

            \path[->, ultra thick] (A) edge[bend right] node {} (B);
            \path[->, ultra thick] (C) edge[bend right] node {} (D);
        \end{tikzpicture}
    \end{adjustbox}
\caption{The case of ungapped endpoints}
\label{figure:28}
\end{figure}

Similar to the case of \lstinline|Interval|, the key to the proof lies in the
help of \lstinline|next-numeral-Proper-UngappedEndpoint-lemma|.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
UngappedEndpoint⇒Incrementable : ∀ {b d o}
    → (xs : Numeral (suc b) (suc d) o)
    → (greatest : Greatest (lsd xs))
    → (proper : 2 ≤ suc (d + o))
    → (¬gapped : ¬ (Gapped xs proper))
    → Incrementable xs
UngappedEndpoint⇒Incrementable {b} {d} {o} xs greatest proper ¬gapped
    = (next-numeral-Proper xs proper) , (begin
          ⟦ next-numeral-Proper xs proper ⟧
      ≡⟨ cong ⟦_⟧ (next-numeral-Proper-refine xs proper
          (UngappedEndpoint b d o greatest ¬gapped)) ⟩
          ⟦ next-numeral-Proper-UngappedEndpoint xs greatest proper ¬gapped ⟧
      ≡⟨ next-numeral-Proper-UngappedEndpoint-lemma xs greatest proper ¬gapped ⟩
          suc ⟦ xs ⟧
      ∎)
\end{lstlisting}

\subsection{Deciding Incrementable Numerals}

With all of the lemmata above, we can finish the most difficult part
of the decider.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
Incrementable?-Proper : ∀ {b d o}
    → (xs : Numeral (suc b) (suc d) o)
    → (proper : 2 ≤ suc (d + o))
    → Dec (Incrementable xs)
Incrementable?-Proper xs proper with nextView xs proper
Incrementable?-Proper xs proper | Interval b d o ¬greatest
    = yes (Interval⇒Incrementable xs ¬greatest proper)
Incrementable?-Proper xs proper | GappedEndpoint b d o greatest gapped
    = no (GappedEndpoint⇒¬Incrementable xs greatest proper gapped)
Incrementable?-Proper xs proper | UngappedEndpoint b d o greatest ¬gapped
    = yes (UngappedEndpoint⇒Incrementable xs greatest proper ¬gapped)
\end{lstlisting}

We can determine if numerals of other categories are incrementable as well.

\begin{lstlisting}
Incrementable? : ∀ {b d o}
    → (xs : Numeral b d o)
    → Dec (Incrementable xs)
Incrementable? xs with Maximum? xs
Incrementable? xs | yes max = no (Maximum⇒¬Incrementable xs max)
Incrementable? {b} {d} {o} xs | no ¬max with numView b d o
Incrementable? xs | no ¬max | NullBase d o
    = yes ((next-numeral-NullBase xs ¬max) ,
        (next-numeral-NullBase-lemma xs ¬max))
Incrementable? xs | no ¬max | NoDigits b o
    = yes (NoDigits-explode xs)
Incrementable? xs | no ¬max | AllZeros b
    = no (contradiction (Maximum-AllZeros xs) ¬max)
Incrementable? xs | no ¬max | Proper b d o proper
    = Incrementable?-Proper xs proper
\end{lstlisting}

The table below sums up whether a non-maximum numeral can be incremented in
systems of each category.

\begin{table}[H]
    \centering
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{|c|c|c|c|c|c|}
        \hline
        \multirow{2}{*}{\textbf{NullBase}} &
        \multirow{2}{*}{\textbf{NoDigits}} &
        \multirow{2}{*}{\textbf{AllZeros}} &
        \multicolumn{3}{c|}{\textbf{Proper}} \\
        \cline{4 - 6}
        & & & \textbf{Inteval} & \textbf{Gapped} & \textbf{Ungapped} \\
        \hline
        yes & yes & no & yes & no & yes \\
        \hline
    \end{tabular}
    \end{adjustbox}
\caption{Summary of whether a non-maximum numeral can be incremented in
systems of each category}
\label{table:12}
\end{table}

Note that the decision of making numerals of \lstinline{NoDigits} incrementable
is completely arbitrary as it will always hold vacuously.

\subsection{\lstinline|increment|}

We can actually do without the previous section about \lstinline|Incrementable?|
and still be able to define a function that increments numerals.
All we have to do is to ask the user to prove that the numeral he or she has
given is incrementable.
By doing so, the user is obliged to provide the actual successor,
and then we can steal it and pretend that we have found the successor.
How outrageous!

\begin{lstlisting}
increment : ∀ {b d o}
    → (xs : Numeral b d o)
    → (incr : Incrementable xs)
    → Numeral b d o
increment xs incr = proj₁ incr
\end{lstlisting}

Obviously this is not the desired implementation of \lstinline|increment|.
The reason why we construct \lstinline|Incrementable?| is that it not only
does the real work of computing successors for us (the credit also goes to \lstinline|next-numeral|)
but also explains why a numeral is incrementable (or not).

The decidability of \lstinline|Incrementable?| enables us to embed it in types.
To filter out numerals that are not eligible for increment, we use the same trick
as when implementing safe \lstinline|head| on lists.

\begin{lstlisting}
True : {P : Set} → Dec P → Set
True (yes _) = ⊤
True (no _) = ⊥
\end{lstlisting}

\lstinline|True| translates positive results of a decidable predicate to
\lstinline|⊤| and negative results to \lstinline|⊥|,
where as \lstinline|toWitness| reclaims the proof of the given proposition for us.

\begin{lstlisting}
toWitness : {P : Set} {Q : Dec P} → True Q → P
toWitness {Q = yes p} _  = p
toWitness {Q = no  _} ()
\end{lstlisting}

Finally, the true implementation of \lstinline|increment| is as follows:

\begin{lstlisting}
increment : ∀ {b d o}
    → (xs : Numeral b d o)
    → (incr : True (Incrementable? xs))
    → Numeral b d o
increment xs incr = proj₁ (toWitness incr)
\end{lstlisting}

\subsection{Properties of \lstinline|increment|}

\begin{lstlisting}
increment-next-numeral : ∀ {b d o}
    → (xs : Numeral b d o)
    → (¬max : ¬ (Maximum xs))
    → (incr : True (Incrementable? xs))
    → increment xs incr ≡ next-numeral xs ¬max
\end{lstlisting}

This property relates \lstinline|increment| with \lstinline|next-numeral|.
It may look trivial, however, the underlying implementation of \lstinline|increment|
does not actually involve \lstinline|next-numeral|, but helper functions like
\lstinline|next-numeral-Proper| and \lstinline|next-numeral-NullBase|.
We dispense with the proof of this property as it is comprised mostly of pattern
matching and seemly meaningless \lstinline|refl|s.
\end{document}


\chapter{The Klein-Gordon Equation}

%%%%%%%
%Section
%%%%%%%
\section{Background}

\footnote{An incomplete but easily accessible mathematical introduction to this equation can be found at \url{http://wiki.math.toronto.edu/DispersiveWiki/index.php/Semilinear_NLW}.}The focusing/defocusing nonlinear Klein-Gordon equation describes the evolution of a possible complex scalar field $u$ according to,
\begin{equation}\label{eq:KleinGordon}
\frac{\partial^2 u}{\partial t^2} - \Delta u +u = \pm \lvert u\rvert^2u,
\end{equation}
where $+$  is the focusing case and $-$ the defocusing case in a similar manner to the nonlinear Schr\"{o}dinger equation. Blow up of three dimensional radially symmetric real solutions to this equation have recently been numerically studied by Donninger and Schlag~\cite{DonSch11}. Two dimensional simulations of the Klein-Gordon equation can be found in Yang~\cite{Yan06}. The linear Klein-Gordon equation occurs as a modification of the linear Schr\"{o}dinger equation that is consistent with special relativity, see for example Landau~\cite{Lan96} or Grenier~\cite{Gre94}. At the present time, there have been no numerical studies of blow up of solutions to this equation without the assumption of radial symmetry. This equation has generated a large mathematical literature and is still poorly understood. Most of this mathematical literature has concentrated on analyzing the equation on an infinite three dimensional space with initial data that either decays exponentially as one tends to infinity or is nonzero on a finite set of the domain. Here, we will simulate this equation in a periodic setting. Since this equation is a wave equation, it has a finite speed of propagation of information, much as a sound wave in air takes time to move from one point to another. Consequently for short time simulations, a simulation of a solution that is only nonzero on a finite part of the domain is similar to a simulation on an infinite domain. However, over long times, the solution can spread out and interact with itself on a periodic domain, whereas on an infinite domain,  the interaction over long times is significantly reduced and the solution primarily spreads out. Understanding the interactions in a periodic setting is an interesting mathematical problem. The Klein-Gordon equation has a conserved energy given by
\begin{equation}
\int  \frac{1}{2}\left( \frac{\partial u}{\partial t}\right)^2 + \frac{u^2}{2}+\frac{1}{2}\left\lvert \nabla u \right\rvert^2 \mp \frac{\left\lvert u \right\rvert^4}{4} \mathrm{d}\bm x.
\end{equation}
The equation is also time reversible. For long time simulations, one wants to construct numerical methods that approximately conserve this energy and are also time reversible. When using Fourier spectral methods, we primarily need to ensure that the time discretization preserves these properties, since the spectral spatial discretization will typically automatically satisfy these properties. Following Donninger and Schlag~\cite{DonSch11}, we use two schemes. First, an implicit-explicit time stepping scheme which is time reversible but only conserves the energy approximately and is given by
\begin{equation}\label{eq:KgImEx}
\frac{u^{n+1}-2u^n+u^{n-1}}{(\delta t)^2} -\Delta \frac{u^{n+1}+2u^n+u^{n-1}}{4} + \frac{u^{n+1}+2u^n+u^{n-1}}{4} = \pm \left\lvert u^{n}\right\rvert^2u^n
\end{equation}
and second, a fully implicit time stepping scheme with fixed point iteration
\begin{align}\label{eq:KgImp}
&{}\frac{u^{n+1,k+1}-2u^n+u^{n-1}}{(\delta t)^2} -\Delta \frac{u^{n+1,k+1}+2u^n+u^{n-1}}{4} + \frac{u^{n+1,k+1}+2u^n+u^{n-1}}{4} \notag
\\&{} = \pm \frac{\left\lvert u^{n+1,k}\right\rvert^4-\left\lvert u^{n-1}\right\rvert^4}{u^{n+1,k}-u^{n-1}}
\end{align}
which conserves a discrete energy exactly
\begin{equation}\label{eq:KgImEn}
\int\frac{1}{2}\left(\frac{u^{n+1}-u^n}{\delta t}\right)^2 + \frac{1}{2}\left(\frac{u^{n+1}+u^n}{2}\right)^2+\frac{1}{2}\left\lvert\nabla\frac{u^{n+1}+u^n}{2}\right\rvert^2 \mp \frac{\left\lvert{u}^{n+1}\right\rvert^4+\left\lvert{u}^{n}\right\rvert^4}{8}.
\end{equation}
As before, the superscript $n$ denotes the time step and $k$ denotes the iterate in the fixed point iteration scheme. Iterations are stopped once the difference between two successive iterates falls below a certain tolerance.

\subsection{Matlab Programs}

Listings \ref{lst:MatKg1D}, \ref{lst:MatKg1Dimp}, \ref{lst:MatKg2D} and \ref{lst:MatKg3D} demonstrate Matlab implementations of these time stepping schemes. In one dimension, the Klein-Gordon equation has easily computable exact solutions, (see for example Nakanishi and Schlag~\cite[p.6]{NakSch11})  which can be used to test the accuracy of the numerical schemes. These equations seem to display three possibilities for the behavior of solutions which are dependent on the initial conditions:
\begin{itemize}
\item the solutions could \emph{disperse} or \emph{thermalize}, that is a given localized initial condition spreads out over the entire space
\item the solutions blow up or become infinite
\item a portion of the solution travels around as a localized particle while the rest of the solution disperses.
\end{itemize}
Since the equations are reversible, there is also the possibility that a solution which is initially distributed over the spatial domain localizes itself.

\lstinputlisting[style=matlab_style,label=lst:MatKg1D,caption={A Matlab program to solve the 1-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImEx}.}]{./KleinGordon/Programs/KleinGordon1D.m}

\lstinputlisting[style=matlab_style,label=lst:MatKg1Dimp,caption={A Matlab program to solve the 1-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}.}]{./KleinGordon/Programs/KleinGordon1Dimp.m}

\lstinputlisting[style=matlab_style,label=lst:MatKg2D,caption={A Matlab program to solve the 2-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImp}.}]{./KleinGordon/Programs/KleinGordonImp2Db.m}

\lstinputlisting[style=matlab_style,label=lst:MatKg3D,caption={A Matlab program to solve the 3-dimensional Klein Gordon equation \eqref{eq:KleinGordon} using the time discretization in eq.\ \eqref{eq:KgImEx}.}]{./KleinGordon/Programs/KleinGordon3Dsliceplot.m}

\subsection{A Two-Dimensional OpenMP Fortran Program}

The programs that we have developed in Fortran have become rather long. Here we add subroutines to make the programs shorter and easier to maintain. Listing \ref{lst:For2dKgOmp} is the main Fortran program which uses OpenMP to solve the 2D Klein-Gordon equation. Notice that by using subroutines, we have made the main program significantly shorter and easier to read. It is still not as simple to read as the Matlab program, but is significantly better than some of the previous Fortran programs. It is also much easier to maintain, and once the subroutines have been written and debugged, they may be reused in other programs. The only drawback in using too many subroutines is that one may encounter a slight decrease in performance due to the overhead of calling a subroutine and passing data to it. The subroutines are in listings \ref{lst:For2dKgOmpGrid}, \ref{lst:For2dKgOmpIniDat}, \ref{lst:For2dKgOmpSavDat}, \ref{lst:For2dKgOmpStoOld}, \ref{lst:For2dKgOmpEneCal}, \ref{lst:For2dKgOmpSavRes} and an example makefile is in listing \ref{lst:Makefile2dKgOmp}. Finally listing \ref{lst:MatlabVideoKg} contains a Matlab program which produces pictures from the binary files that have been computed. One can then use another program to take the images and create a video\footnote{At the present time, Matlab's video commands cannot reliably produce a single video from a very long simulation, so it is better to use Matlab to create still images.}.

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmp,caption={A Fortran program to solve the 2D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/KgSemiImp2d.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpGrid,caption={A Fortran subroutine to get the grid to solve the 2D Klein-Gordon equation on.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/getgrid.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpIniDat,caption={A Fortran subroutine to get the initial data to solve the 2D Klein-Gordon equation for.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/initialdata.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpSavDat,caption={A Fortran program to save a field from the solution of the 2D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/savedata.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpStoOld,caption={A Fortran subroutine to update arrays when solving the 2D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/storeold.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpEneCal,caption={A Fortran subroutine to calculate the energy when solving the 2D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/enercalc.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For2dKgOmpSavRes,caption={A Fortran subroutine to save final results after solving the 2D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/saveresults.f90}

\lstinputlisting[style=make_style,language=make,label=lst:Makefile2dKgOmp,caption={An example makefile for compiling the OpenMP program in listing \ref{lst:For2dKgOmp}.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/makefile}

\lstinputlisting[style=matlab_style,label=lst:MatlabVideoKg,caption={A Matlab program to plot the fields produced by listing \ref{lst:For2dKgOmp}.}]{./KleinGordon/Programs/KleinGordon2dThreadFFT/video.m}

\subsection{A Three-Dimensional MPI Fortran Program using 2DECOMP\&FFT}

We now give a program for the three-dimensional nonlinear Klein-Gordon equation. The program uses the same subroutine structure as the two-dimensional code. To make the program easy to reuse, the subroutine listed in listing \ref{lst:For3dKgMpiReaInp} has been created to read an INPUTFILE which specifies the parameters to use for the program and so the program does not need to be recompiled every time it is run. To enable the program to scale better, the arrays which hold the Fourier frequencies and grid points have also been decomposed so that only the portions of the arrays used on each processor are created and stored on the processor. A further addition is a short postprocessing program to create header files to allow one to use the bov (brick of values) format that allows one to use the parallel visualization software VisIt. The program is listed in listing \ref{lst:For3dKgMpiBovCre}, to use this program simply compile it using gfortran, no special flags are required, and then run it in the directory from which the INPUTFILE and data are stored. The program VisIt can be downloaded from \url{https://wci.llnl.gov/codes/visit/home.html}. This program also run on laptops, desktops as well as parallel computer clusters. Documentation on using VisIt is available here \url{https://wci.llnl.gov/codes/visit/manuals.html} and here \url{http://www.visitusers.org/index.php?title=Main_Page}.  A short video tutorial on how to use VisIt remotely is available at \url{http://cac.engin.umich.edu/resources/software/visit.html}.

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpi,caption={A Fortran program to solve the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/KgSemiImp3d.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiGrid,caption={A Fortran subroutine to get the grid to solve the 3D Klein-Gordon equation on.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/getgrid.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiIniDat,caption={A Fortran subroutine to get the initial data to solve the 3D Klein-Gordon equation for.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/initialdata.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiSavDat,caption={A Fortran program to save a field from the solution of the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/savedata.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiStoOld,caption={A Fortran subroutine to update arrays when solving the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/storeold.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiEneCal,caption={A Fortran subroutine to calculate the energy when solving the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/enercalc.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiSavRes,caption={A Fortran subroutine to save final results after solving the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/saveresults.f90}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiReaInp,caption={A Fortran subroutine to read in the parameters to use when solving the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/readinputfile.f90}

\lstinputlisting[style=make_style,language=make,label=lst:Makefile3dKgMpi,caption={An example makefile for compiling the MPI program in listing \ref{lst:For3dKgMpi}.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/makefile}

\lstinputlisting[style=fortran_style,language=Fortran,label=lst:For3dKgMpiBovCre,caption={A Fortran subroutine to create BOV (Brick of Values) header files after solving the 3D Klein-Gordon equation.}]{./KleinGordon/Programs/KleinGordon3dMpiFFT/bovcreate.f90}

\subsection{Exercises}
\begin{enumerate}
\item[1)] Compare the accuracy of the implicit and semi-implicit time stepping schemes in eqs.\ \eqref{eq:KgImEx} and \eqref{eq:KgImp}. Which scheme produces the most accurate results in the least amount of real time?
\item[2)] Write serial Fortran programs to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq.\ \eqref{eq:KgImp}.
\item[3)] Write OpenMP parallel Fortran programs to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq.\ \eqref{eq:KgImp}.
\item[4)] The MPI command MPI\_BCAST is used in the subroutine readinputfile, listed in list \ref{lst:For3dKgMpiReaInp}. Look up this command (possibly using one of the references listed in the introduction to programming section) and explain what it does.
\item[5)] Write an MPI parallel Fortran program to solve the two- and three-dimensional Klein-Gordon equations using the fully implicit time stepping scheme in eq.\ \eqref{eq:KgImp}.
\item[6)] Compare the results of fully three-dimensional simulations with periodic boundary conditions ($\mathbb{T}^3$) with analytical predictions for blow up on the entire real space ($\mathbb{R}^3$) summarized in Donninger and Schlag~\cite{DonSch11}.
\item[7)] Grenier~\cite[p.~18]{Gre94} explains that the linear Klein-Gordon equation can be written as two coupled Schr\"{o}dinger equations. One can extend this formulation to the nonlinear Klein-Gordon equation. If we let
\begin{equation}\label{eq:KgSchDecomp1}
u=\phi+\xi \quad\text{and}\quad\frac{\partial u}{\partial t}=\phi-\xi
\end{equation}
then the two coupled equations
\begin{align}\label{eq:KgSchDecomp2}
&{} i\frac{\partial }{\partial t}\begin{bmatrix}\phi \\ \xi \end{bmatrix}= \begin{bmatrix} -\Delta  -1 & -\Delta \\ \Delta & \Delta + 1 \end{bmatrix}\begin{bmatrix}\phi \\ \xi \end{bmatrix} \pm\begin{bmatrix}1 \\ -1 \end{bmatrix} \frac{\lvert \phi+\xi\rvert^2(\phi+\xi)}{2}
\end{align}
 are equivalent to the nonlinear Klein-Gordon equation
 \begin{align}\label{eq:KgSchDecomp3}
 &{}\frac{\partial^2u}{\partial t^2} - \Delta u + u = \pm u^3.
 \end{align}
 \begin{enumerate}
 \item[a)] Fill in the details to explain why eqs.\ \eqref{eq:KgSchDecomp1} and \eqref{eq:KgSchDecomp2} are equivalent to eq.\ \eqref{eq:KgSchDecomp3}. In particular show that by adding and subtracting the two equations in eqs.\ \eqref{eq:KgSchDecomp1} and \eqref{eq:KgSchDecomp2}, we get
 \begin{align*}
 &{} i\frac{\partial}{\partial t}\left(\phi+\xi\right)= -\left(\phi-\xi\right)
 \\&{} i\frac{\partial}{\partial t}\left(\phi-\xi\right)=-\Delta \left(\phi+\xi\right) - \left(\phi+\xi\right) \pm \left\lvert \phi+\xi \right\rvert^2\left(\phi+\xi\right).
 \end{align*}
 Differentiating the first of these equations and substituting it into the second, then recalling that we defined $u=\phi+\xi$ in eq.\ \eqref{eq:KgSchDecomp1} gives us the Klein-Gordon equation in eq.\ \eqref{eq:KgSchDecomp3}.
 \item[b)] Solve these two equations using either the implicit midpoint rule or the Crank Nicolson method.
 \end{enumerate}
\end{enumerate}


\externaldocument{paper.tex}
	\section{Conclusions}
		\label{sec:conclusions}

		In this work, we addressed the subject of intrinsic and isometric
	manifold learning. We first showed that the need for methods, which
	preserve the latent Euclidean geometric structure of data manifolds,
	arises naturally when inherently low-dimensional systems are observed
	via high-dimensional non-linear measurements or observations. We presented
	a new manifold learning algorithm which uses local properties of the
	observation function to estimate a local intrinsic metric (the ``push-forward''
	metric of the observation function). This metric was then used to
	estimate intrinsic geometric proprieties of the data directly from
	the observed manifold as if they were calculated in the low-dimensional
	latent space. We discussed a few settings under which estimation of
	the required local observation function properties is possible from
	the observed data itself. Unfortunately, we recognized that due to
	their local nature, these metric estimation methods are not sufficiently
	robust to high curvature of the observation function as well as to
	noise. We suggested to overcome this by parameterizing all estimated
	intrinsic metrics on the observed manifold as the output of a single
	\ac{ANN} and performing a signal optimization in order to estimate
	all local intrinsic metric simultaneously. This procedure has probabilistic
	reasoning and was shown to be equivalent to maximum-likelihood estimation
	under a certain statistical model. We showed that this couples the
	metric estimations at different points on the manifold, regularizes
	the estimation and imposes smoothness of the estimated metric. We
	discussed the possibility of additionally imposing regularization
	on the estimation by the net structure and weight decay terms, which
	is common practice in \acp{ANN}. By combining a robust intrinsic
	metric estimation method and an algorithm which can use these metrics
	to build an intrinsic and isometric embedding, we devised an algorithm,
	which can automatically recover the geometric structure of a latent
	low-dimensional manifold from its observations via an unknown non-linear
	high-dimensional function. Finally we focused on the example of mapping
	and positioning an agent using a sensor network of unknown nature
	and modalities. We showed that our proposed algorithm can recover
	the structure of the space in which the agent moves and can correctly
	position the agent within that space. Due to the intrinsic nature
	of our method, it can perform this mapping and positioning without
	the need for prior knowledge of a measurement model to explain the
	connection between the observed measurements and the position of the
	agent. This invariance to the type of measurement used, was shown
	to be suitable in a setting such as indoor positioning where the exact
	measurement model is usually unknown.

	It is evident that our method outperforms the local metric estimation
	approach for all the tested examples and enables us to learn intrinsically-isometric
	representation for broader cases problems given very sparse sampling
	and observation noise of the same scale of the intrinsic variance
	which is used to estimate the intrinsic metric.

	The use of neural networks as regression functions. provides a powerful
	regularizing factor in intrinsic metric estimation. Their general
	structure with the plethora of optimization algorithms, regularization
	tweaks and efficient implementation methods make them a very good
	candidate for a parametric function family to be used in the problem
	of metric estimation.

	The estimation method described in this chapter was proven to have
	probabilistic reasoning when assuming an intrinsic-isometric \ac{GMM},
	however it also has an intuitive interpretation in a more general
	case. As can be observed in \cref{eq:KL}, the network tries approximate
	locally calculated estimations of the intrinsic metric while adhering
	to a globally smooth and regularized model. This can serve as a good
	general heuristic method to impose global smoothness and regularization
	on locally estimated intrinsic metrics under other probabilistic or
	non-probabilistic setting such as the one presented in
	\cref{ssec:Intrinsic-isotropic-GMM}.

	A somewhat similar method was presented in \cite{bengio2004non} where
	an \ac{ANN} was also used in order to parametrize the spanning vectors
	of the local tangent plane to a manifold. The only criterion provided
	for the estimation was minimization of the distance of observed points
	from the local tangent plane. The estimation was not given probabilistic
	reasoning and the estimated spanning vector had no physical meaning
	and did not provide any metric interpretation of information. In our
	work, we not only retrieve the tangent plane to the manifold at each
	point but also an intrinsic metric and we do so with probabilistic
	reasoning.

	In the case of neural networks ``the devil is in the details'' and
	implementation plays a large role in the final performance of the
	trained network and the subjects of neural network training and regularization
	are vast topics which are out of the scope of this work. Our main
	contribution is to show that neural networks can, in principle be
	used, to regularize estimations and in this specific case intrinsic
	metric estimation and we do not attempt to claim that this architecture
	and optimization procedure achieve an optimal solution.

	The global estimation approach presented in this chapter has some
	secondary advantages in addition to providing additional robustness
	to the intrinsic metric estimation when compared to local metric estimation
	methods. As opposed to local methods which only estimate the intrinsic
	metric for observed sample points, the regression approach produces
	an estimation of the intrinsic metric on the whole observed space.
	This can be possibly used to generate additional points in between
	existing points, thus artificially increasing the sample density.
	This can improve both the short-range intrinsic distance estimation
	described in \cref{ssec:Intrinsic-geometry-approximation} and
	the geodesic distance estimation described in \cref{ssec:Global-geometry-approximation}.
	Both effects should improve the results of the algorithm presented
	in \cref{sec:Intrinsic-isometric-manifold-learning}.

	In \cite{bengio2004non} it was shown that by leaning the tangent
	space to the manifold at each point, one can ``walk'' on the manifold
	by making infinitesimal steps each time in the tangent space. Since
	with the method suggested in this chapter, we do not only estimate
	the tangent space but the intrinsic metric as well, we know how far
	we have gone in terms of the distance, an ability that might be relevant
	to applications such as non-linear interpolation \cite{bregler1995nonlinear}.

	\subsection{Pre-processing}
		\label{subsec:Pre-processing}

	Since our approach does not assume anything about the structure of
	the observed data, one can perform pre-processing stages for reducing
	the dimensionality of the data and possibly removing noise without
	worrying about maintaining the structure of the data. This has the
	advantage of enabling the use of other existing dimensionality reduction
	methods as pre-processing stages to our algorithm. For example, when
	localization and mapping were performed using image data, the algorithm
	did not treat the data as images, which allowed us to use \ac{PCA}
	to lower the dimensionality of the image data greatly. This would
	not be possible for vision based algorithms which exploit the image
	structure since the application of \ac{PCA} would remove this structure
	and such algorithm could no longer be applied. For our method the
	image structure is unimportant and one can do without it.

	\subsection{Practical application to indoor-positioning}
		\label{subsec:Practical-application-to}

	The ability to localize sets of measurements without knowledge of
	the model connection positions and measurements is a problem encountered
	in real life indoor positioning due to the complexity of the models
	and the variations, as described in \cref{sec:introduction}.
	Learning a model for each specific setting requires large amount of
	labeled data. Our algorithm might replace the need for labeled data
	acquisition with a simple prior assumption on the unlabeled measurement
	acquisition process.

	This has been showed to work in theory however it has yet to be applied
	to a realistic indoor positioning system. Basic physical experiments
	using a randomly walking agent in 2-dimensional space are underway.
	In these experiments a robotic iRobot Roomba programmable robotic
	vacuum cleaner was controlled by a Raspberry-Pi mini computer and
	performed a random walk inside a 2-dimensional region. Signal acquisition
	was performed either my measuring the \ac{RSS} from WiFi stations
	or via a wide angle lens which produced 360 degree images. Results
	from the experiments will be published when concluded. Images from
	these experiments are shown in \cref{fig:Roomba-experiments}.

	\iffalse
	\begin{figure}[h]
		\begin{centering}
			\begin{minipage}[t]{0.45\columnwidth}%
				\begin{flushleft}
					\subfloat[\ac{RSS} measurement]{\begin{centering}
							\includegraphics[width=1\textwidth]{figures/Chapter_6/Roomba_wifi}
							\par\end{centering}
					}
					\par\end{flushleft}%
			\end{minipage}\hfill{}%
			\begin{minipage}[t]{0.45\columnwidth}%
				\subfloat[Panoramic camera]{\begin{centering}
						\includegraphics[width=1\textwidth]{figures/Chapter_6/Roomba_pano}
						\par\end{centering}
				}%
			\end{minipage}
			\par\end{centering}
		\caption{Roomba experiments \label{fig:Roomba-experiments}}
	\end{figure}
	\fi

	\subsection{Generalized localization problems}

	Throughout this work and in \cref{sec:introduction} and
	\cref{ssec:localization} specifically, we discussed the problem
	of localization in sensor networks and presented it as an example
	of a problem which requires an intrinsic and isometric manifold learning
	method in order to fully utilize the observed data and recover a mapping
	of the physical space and a localization of an agent within this space.
	However, this should not be regarded as a specific problem (although
	it is by itself an important one) but as a general prototypical problem,
	for which physical localization in 2-dimensional or 3-dimensional,
	is just one intuitive manifestation. Indeed, any problem where the
	uncovering of the global geometric structure of a low-dimensional
	parameter space is desirable, can be regarded as a ``localization
	`` problem. Trivially, this can occur when structure recovery is
	by itself a goal and one can think of many examples where the global
	structure of the parameter space is important or has some special
	meaning (for example for imaging or molecule structure recovery),
	yet this global structure is also crucial when one requires the intrinsic
	structure of a low-dimensional vector space. The process of observation
	via a non-linear function causes the data to lose its global structure,
	and the initially ``flat'' manifold becomes curved. This hinders
	our ability to perform operations on the parameter space which require
	a vector space structure such as addition, subtraction, division and
	multiplication. This are the basic operation required for more high-level
	processing such as averaging, clustering, interpolation, learning
	using $k$-NN (especially when samples are sparse and neighbors are
	far from each other) etc. Embedding the data back into a low-dimensional
	space recovers the vector space structure of the original latent space
	and enables these operations.

	\subsection{Automatic labeled data acquisition}

	Supervised machine learning uses labeled data (consisting of pairs
	of input objects and desired output values) and attempts to ``learn''
	or infer a functional connection between the two. Learning using more
	labeled data increases the learners ability to unseen examples and
	usually leads to better performance of the trained model. Unfortunately
	acquisition of labeled data is usually non-trivial, expensive and
	requires an already existing method to correctly label data. Our proposed
	approach can be seen as a method for automatic acquisition of labels
	as it operates in a completely unsupervised manner and produces data
	labeling as an output. The ability to produce labeled data can thus
	be reduced to the task of exploring the parameter space of the system
	under some restriction or statistical model which allows us to calculate
	a local intrinsic metric as described in this work.

	This automatic acquisition of labeled data can be much easier in many
	cases than producing labeled data using an existing labeling method.
	As an example we return to the localization example discussed in depth
	in \cref{ssec:localization}. To acquire labeled data in
	the described setting, one would be required to perform a large set
	of observations (color images, depth maps, \ac{RSS} values etc.)
	and provide for each such observation the 2-dimensional coordinates
	at which they were taken. This procedure requires time, patience and
	an existing way to measure the correct coordinates. This becomes even
	more complicated if one considers localization in 3-dimensional space
	where accurate positioning requires special equipment. Alternatively,
	with our approach, one could use a randomly waking agent or a simple
	sensor array (as described in \cref{sec:Intrinsic-Metric-Estimation})
	to acquire a large set of unlabeled data, and then use our proposed
	algorithm to retrieve an approximation of their labels. One can then
	use a supervised machine learning algorithm in order to infer the
	label of new observations. In the application described in \cref{ssec:localization}, once labeling of a large set of images
	from the apartment space is acquired using our algorithm, one can
	feed these to a vision based algorithm which can be robust to variation
	in appearance of the space, such as changes of illumination, obstructions,
	or even some changes in the structure of the apartment due to movement
	of some objects.

	\subsection{Holistic approach}

	The algorithm presented in this work suggests a two-stage solution
	for intrinsic-isometric embedding of manifolds in low-dimensional
	Euclidean space:
	\begin{enumerate}
		\item Recovering local intrinsic metrics using prior-knowledge and assumptions
		about the intrinsic process and using them to calculate intrinsic
		inter-point distances.
		\item Constructing and embedding of the observed points into a low-dimensional
		space, where the Euclidean distance respects the calculated intrinsic
		inter-point distances.
	\end{enumerate}
	If the first stage of this approach does not result in accurate enough
	estimations of the intrinsic geometry, the second stage can potentially
	fail. Combining the two stages described above into a one stage embedding
	method could be beneficial. A possible way to implement this is to
	directly learn an embedding function of the observed data into a low-dimensional
	space such that the embedding best fits prior-knowledge or assumption
	we have about the intrinsic system. This could be implemented for
	example with an \ac{ANN} for which the cost function will be the
	likelihood that the embedding originated from the statistical model
	of the intrinsic latent system. Such an implementation would however,
	rarely converge to minima which represent a good embedding of the
	data without being given an initial solution which is close enough
	to the true structure. One can however use the embedding received
	via the presented two stage approach as an initial target embedding
	for such an \ac{ANN}, and then further maximize the likelihood of
	the embedding directly on the embedding space.

	\subsection{Sensor fusion}

	The fact that our proposed algorithm is invariant to the observation
	function, allows sensor fusion. Measurements from different sensors
	modalities can be simply concatenated, creating a higher-dimensional
	observation function. Although this should work in theory, it is clear
	that if the sensors are improperly scaled with respect to each other
	this will not be optimal. Further research should be invested in considering
	the proper way to fuse different measurement modalities in a way that
	optimizes the results of the algorithm.

	\subsection{Towards data-driven Taken's embedding}

	Our algorithm attempts to produce an embedding of the observed manifold
	into a low-dimensional space, where similar observations are embedded
	to similar locations in the constructed embedding space. This causes
	a problem if different intrinsic points have identical or very similar
	observation values. In this work we resolved this by assuming that
	the observation function is invertible. One practical approach to
	deal with this situation is to add observations until any such similar
	points are distinguished. However, when a set of observations is given
	and one cannot add additional ``physical'' observations we require
	a different way required. If temporal information about the dynamical
	observation process is available one can, in principle, distinguish
	points by their respective past and future observations. Such a separation
	between points would be especially beneficial in the presence of high
	levels of observation noise which might make originally distinct measurement
	values similar to each other. Our proposed algorithm currently uses
	temporal side-information only for the stage of estimating the intrinsic
	metric and does not incorporate this information for the purpose of
	the final embedding into low-dimensional space. One possible approach
	for incorporating this information naturally into our algorithm is
	by the observation of several lagged measurements simultaneously,
	by looking far ``enough'' into the past, such that any two points
	become distinguishable. This was proven for the case of deterministic
	latent dynamical system by Taken's embedding theorem \cite{takens1981detecting}.

	\subsection{Implementation using vector-diffusion maps}

	Our proposed manifold learning algorithm computes global intrinsic
	geometric properties by using the shortest-path algorithm in order
	to approximate inter-point geodesic distances. The shortest-path algorithm
	operated by propagating the distance function from a source point
	across the manifold. A similar process of propagating local vector
	information (instead of scalar distance information) could be used
	to allow for direct long range intrinsic Euclidean distance estimation.
	A framework for vector diffusion on Riemannian manifold has been presented
	in \cite{singer2012vector} and could possibly be used in order to
	implement this. Euclidean distance estimation by vector diffusion
	should be unaffected by non-convexity of the intrinsic manifold and
	can allow for use of global distances making the final embedding more
	robust.

	\subsection{Incorporating labeled data}
		\label{subsec:Incorporating-labeled-data}

	The algorithm proposed in this work operates in a completely unsupervised
	manner. The only prior-knowledge used is the assumption about the
	intrinsic data structure, which allows for estimation of the local
	intrinsic metric. While this is a desired property of the algorithm,
	since labeled data is usually harder to gather relative to unlabeled
	data, it does not harness the advantages of labeled data if such data
	does exist. We mention two possible ways to incorporate existing labeled
	data. Labeled data can be used as part of the intrinsic metric learning,
	where similar settings have been used in existing works in order to
	generate metrics which best describe existing label variations on
	data manifold \cite{yang2006distance,xing2003distance}. Alternatively,
	labeled data can be used in the embedding procedure itself, where
	one can easily add additional distance restriction known from labeled
	data. In the localization example given in \cref{ssec:localization}
	for example, we notice a slight distortion in the embedding on a global,
	long-range scale. This is because we only incorporate local distance
	information in the final embedding optimization, which does not fully
	restrict the global conformation in practical noisy situations. The
	short-distance estimations have errors, which induce slight distortions
	in the embedding. These distortions are not significant on small scales
	but they accumulate on larger scales and longer distances. Adding
	a few robustly measured long distances can be extremely beneficial
	for correcting this and even a very low number of such constraints
	could stabilize the embedding considerably, removing this accumulated
	error. Incorporating such robustly measured distances in the algorithm
	suggested in \cref{sec:Intrinsic-isometric-manifold-learning}
	can be done easily by giving such distance constraints much larger
	weights in the stress function presented in (\cref{eq:w-intrinisc_stress})
	than those of distances estimated from the observed data, which are
	less reliable.


\documentclass[12pt, a4paper]{article}
\usepackage[a4paper, bindingoffset=0.2in, %
left=0.5in,right=0.5in,top=0.5in,bottom=0.5in,%
footskip=.25in]{geometry}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{physics}

\title{PSet8 Report}
\author{Ali Abolhassanzadeh Mahani}

\begin{document}
	\maketitle
	\section{First Order Differential Equation}
	The equation we are to solve is
	\begin{equation}
		R \dv{Q}{t} + \frac{Q}{C} = V
	\end{equation}
	for the values $V= 10 volts$, $R = 3000 \Omega$, $C = 1 \mu F$ and the simulation time from zero to $t = 0.5\, ms$.\\
	Since the numbers are a little overwhelming, we make parameter changes to come up with better numbers for our simulation.

	The change of variables is as follows:
	\begin{equation} \label{eq:change}
		\begin{aligned}
			&x \equiv \frac{Q}{R C} - \frac{V}{R}, \quad \tau \equiv \frac{t}{R C}\\
			\Rightarrow  &\dv{x}{\tau} + x = 0
		\end{aligned}
	\end{equation}

	Using this change in variables, the time limit becomes: $\tau = \frac{0.5 ms}{10^{-6} F \times 3000 \Omega} = \frac{1}{6} s$
	Also, if we want the capacitor to start from being empty, the initial value for $x$ will be: $x_0 = \frac{1}{300} \frac{volts}{\Omega}$

	The analytical solution is obtained by integrating the equation $\frac{\dd{x}}{x} = \dd{\tau}$
	which gives:
	\begin{equation} \label{eq:analytical}
		x = x_0 \exp(- \tau) \Rightarrow Q = C V (1 - e^{- \frac{t}{R C}})
	\end{equation}

	I integrated numerically for $x$ and then used a reverse version of our change of variables, to find $Q$. Then, I plotted the analytical solution
	for $Q$ and the integration in the same graph. (Fig.\ref{fig:euler})
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.8\linewidth]{../p1/capacitor.jpg}
		\label{fig:euler}
		\caption{Analytical and numerical solution for the charging capacitor equation. The time step for the numerical solution here is $0.01$}
	\end{figure}

	For the second part, I found the difference in numeric and analytic method, $\delta$, at $t = 0.5 ms$ for various values of the time step.
	The data is available in table \ref{tab:error}, and the plot, in Fig.\ref{fig:error}
	\begin{table}[h!]
		\centering
		\begin{tabular}{|c|c|}
			\hline
			$h$ & $\delta$\\
			\hline
			$0.001$ & $0.00282761$ \\
			\hline
			$0.005$ & $0.00284087$ \\
			\hline
			$0.01$ & $0.002868396$ \\
			\hline
			$0.015$ & $0.00286730$ \\
			\hline
			$0.02$ & $0.002895286$ \\
			\hline
			$0.03$ & $0.002952511$ \\
			\hline
			$0.04$ & $0.002950655$ \\
			\hline
			$0.05$ & $0.003009868$ \\
			\hline
		\end{tabular}
	\label{tab:error}
	\caption{The values of $\delta$ for different values of time step $h$.}
	\end{table}

	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.8\linewidth]{../p1/error.jpg}
		\label{fig:error}
		\caption{Plot for \emph{$\log(\delta)$} vs. \emph{$\log(h)$}. We can see that for a value of $h$ there's a minimum where the error is optimal and then the relation is linear.}
	\end{figure}

	\section{$2^{nd}$ Order ODE}
	I made all the functions to take variables \texttt{x\_init, acc, step, time}. \texttt{x\_init} is the initial position of the mass from the origin.
	\texttt{acc} is the function that returns the \emph{acceleration} as a function of position \texttt{x}.
	The initial conditions are as follows:
	\begin{equation}
		\begin{aligned}
			\dot{x} &= 0\\
			x & = 1
		\end{aligned}
	\end{equation}

	For some methods like \emph{verlat} I had to define extra initial conditions due to the nature of the algorithms.

	I made a dictionary \texttt{data} that stores the results for different methods and makes the job of plotting and other stuff easier.
	I integrated for 60 time units and time step of \texttt{step = 0.01}. Then I plotted them in one graph. (Fig \ref{fig:ODEs})
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.8\linewidth]{../p2/ode2_plots.jpg}
		\label{fig:ODEs}
		\caption{plot of the solutions using different integration methods from $0$ to $t = 60$ with time step of $0.01$}
	\end{figure}
	Then, I plotted $\dot{x}$ as a function of $x$ for each method separately. (Fig\ref{fig:energy})
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.45\linewidth]{../p2/euler.jpg}
		\includegraphics[width=0.45\linewidth]{../p2/euler_cromer.jpg}
		\includegraphics[width=0.45\linewidth]{../p2/verlat.jpg}
		\includegraphics[width=0.45\linewidth]{../p2/velocity_verlat.jpg}
		\includegraphics[width=0.45\linewidth]{../p2/beeman.jpg}
		\label{fig:energy}
		\caption{$\dot{x}$ vs. $x$ for different methods.}
	\end{figure}

	As can be seen from Fig\ref{fig:energy}, the \emph{euler-cromer}, \emph{verlat}, \emph{velocity verlat}, and \emph{beeman} methods have stability for this
	problem.

	\section{Instability in algorithms}
	Here we use the same change in variables that we did in equation\ref{eq:change} to simulate the charging capacitor.
	The algorithm we are going to use here is as follows:
	\begin{equation} \label{eq:instability}
		y_{n + 1} = y_{n - 1} + 2 \dot{y}_n h
	\end{equation}
	where $h$ is the time step. Since this is a two step algorithm, we need a second initial value that we get using the Euler method. From that point
	forward, we use the algorithm in equation\ref{eq:instability}. Then, we plot both the analytical solution (eq.\ref{eq:analytical}) and the
	numerical solution in one graph to compare them.

	In small values for terminating the integration, the numerical solution seems to be stable, but if we wait long enough, we can see that
	from about $\tau = 6.0$, the numerical integration shows signs of periodic motions and oscillation around the analytical solution.
	(Fig.\ref{fig:instability})
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.8\linewidth]{../p3/instability.jpg}
		\label{fig:instability}
		\caption{numerical solution using the algorithm from eq\ref{eq:instability}, shows oscillation about the analytical solution for
			large enough $\tau$, which represents instability for this algorithm in this perticular problem.}
	\end{figure}

	It can also be seen that for higher values of the time step, the instability manifests much sooner and explodes more rapidly.

	\section{Chaos}
	Here I made a function \texttt{stable\_point(r)} which applies the function \texttt{f(r, x)} 1500 times and returns the last 200 values.
	This way we can cover the bifurcations to 128-periodic. Here $f(r, x) = x_{n + 1} = 4rx_{n} (1 - x_{n})$.  The bifurcation plot is available in FIg\ref{fig:bifurcation}
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.9\linewidth]{../p4/bifurcation.jpg}
		\label{fig:bifurcation}
		\caption{bifurcation plot for $f(r, x)$}
	\end{figure}

	I failed to find the values for $\delta, \alpha$. :-(
\end{document}