\section{Method}

Our work starts with a comprehensive overview of the underlying core concepts. As graph states lie at the heart of measurement-based quantum computing, it is necessary to introduce the terminology and some fundamental results. To make this introduction, we use two different approaches to define graph states. Next, we will introduce the one-way quantum computer, an abstract machine for quantum computing. We use this machine in the following section to discuss various results about measurement-based quantum computing.

\subsection{Graph States}

We start by introducing the necessary terminology regarding graphs, which are mathematical objects commonly used in computer science composed of vertices and edges. A graph is defined by a set \(V\) of \(N\) vertices and a set \(E\) of \(M\) edges\cite{clrs}.
\begin{gather}
  V = \Set{1, 2, \dots, N} \\
  E \subseteq [V]^2 \;\text{where}\; \abs{E\,} = M
\end{gather}
We usually represent graphs via diagrams. Such a diagram is given in Figure \ref{fig:graph_example}. As we do not impose order among the vertices that are connected by an edge, an edge is used only to denote \emph{connectivity}. Two vertices \(a, b \in V\) are called \emph{adjacent} if they are connected by an edge. The \emph{neighbourhood} of a vertex \(a \in V\) is the set of all the vertices \(a\) is adjacent to.
\begin{equation}
  N_a = \Set{\,b\in V \given \Set{a, b}\in E\,}
\end{equation}
Graphs that contain no loops---edges connecting a vertex to itself---or multiple edges between any two of its vertices are called \emph{simple graphs}\cite{hein2006}. The main type of graphs we deal with are simple graphs.

\begin{figure}[h]
  \centering
  \tikz \graph [
    ] {
      1 -- {2 -- 3, 4};
      3 -- 4;
    };
  \caption{The diagram of graph \(G = \{\Set{1,2,3,4}\,,\, \{\Set{1,2}, \Set{1,4}, \Set{2,3}, \) \(\Set{1,4}\}\} \) }\label{fig:graph_example}
\end{figure}

Simple graphs can be used to represent some interactions of qubit systems. We call these systems \emph{graph states}. Formally defining a graph state can be done in two different ways. The first one is by using the system's interaction patterns.

A graph state described by the graph \(G = \Set{V, E}\) is made up of qubits that are labelled by the vertices of \(G\). Any two qubits \(a,b\in V\) connected by an edge \(\Set{a,b} \in E\) interact via an Ising type interaction. We also impose the following conditions apart from the restriction on the qubits' interaction type.
\begin{enumerate}
  \item Since an edge only denotes connectivity, all two-particle unitaries acting on a vertex must commute.
  \begin{equation}
    [U_{ab}, U_{bc}] = 0 \quad \forall a, b, c \in V
  \end{equation}
  \item Since edges do not denote a direction, unitaries must be symmetric.
  \begin{equation}
    [U_{ab}, U_{ba}] = 0 \quad \forall a, b \in V
  \end{equation}
  \item All the particles must interract through the same unitary.
\end{enumerate}
The general form of an Ising type interaction that satisfies these additional constraints is given by the following unitary, parameterized by the \emph{interaction strength}.
\begin{equation}
  U_{ab}^I(\psi_{ab}) = e^{-i \psi_{ab} \, \sigma_z^a \, \sigma_z^b}
\end{equation}
This interraction pattern is useful to us because of the entanglement patterns it produces. Note that such a unitary with parameter \(\psi_{ab} = \pi/4\) is equivalent to a controlled \(\sigma_z\) gate---denoted by CZ---up to some additional \(\pi/4\)-rotations around the \(z\) axis for each qubit.
\begin{equation}
\begin{aligned}
  e^{-i\frac{\pi}{4}} e^{i\sigma_z^a\frac{\pi}{4}} e^{i\sigma_z^b\frac{\pi}{4}} U^I_{ab}\p*{\frac{\pi}{4}} &= e^{-i\frac{\pi}{4}} e^{i\sigma_z^a\frac{\pi}{4}} e^{i\sigma_z^b\frac{\pi}{4}} e^{-i \frac{\pi}{4} \, \sigma_z^a \, \sigma_z^b}\\
  &= e^{-i\frac{\pi}{4}}\\
  &= \begin{bmatrix}
    1 & 0 & 0 & 0 \\
    0 & 1 & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & -1
  \end{bmatrix}
\end{aligned}
\end{equation}
Using CZ to construct edges makes sure that a resulting edege
\begin{equation}
  U_{ab}\ket+^a\ket+^b = \frac{1}{\sqrt2}(\ket0^a\ket+^b + \ket1^a\ket-^b)
\end{equation}
is maximally entangled. Furhtermore \(U_{ab}\)\/ is Hermitian so it can be used to delete an edge as well.

A graph state \(\ket{G}\) which correspond to the simple graph \(G=\p{V, E}\) is defined to be
\begin{equation}
  \ket{G} = \prod_{\p{a,b} \, \in E} U_{ab}\,\ket+^V,
\end{equation}
and is prepared via the following procedure\cite{hein2006}.
\begin{enumerate}
  \item For each vertex in \(V\), prepare the corresponding qubit with the positive \(\sigma_x\) eigenstate \(\ket+\).
  \item For each edge \(\p{a,b}\in E\), apply \(U_{ab}\) to the system.
\end{enumerate}

\begin{figure}[bt]
  \centering
  \subcaptionbox{The diagram of \(G\)}[0.85\linewidth]{\input{{fig/graph.tikz}}}
  \vspace{1em}

  \subcaptionbox{Interraction pattern representation of \(\ket{G}\)}[0.43\linewidth]{%
    \begin{align*}
      \ket{G}
      &= U_{24}U_{23}U_{12}\ket{+}^{\otimes 4} \\
      &= \begin{aligned}[t]
        &\ket0\ket0\ket0\ket+ -\ket0\ket0\ket1\ket-\\
        &+\ket0\ket1\ket0\ket+ -\ket0\ket1\ket1\ket-\\
        &+\ket1\ket0\ket0\ket+ +\ket1\ket0\ket1\ket-\\
        &-\ket1\ket1\ket0\ket+ +\ket1\ket1\ket1\ket-
      \end{aligned}
  \end{align*}}
  \hfill
  \subcaptionbox{Stabilizer representation of \(\ket{G}\)}[0.43\linewidth]{%
  \begin{gather*}
    \sigma_x^1\sigma_z^2\\
    \sigma_x^2\sigma_z^1\sigma_z^3\sigma_z^4\\
    \sigma_x^3\sigma_z^2\\
    \sigma_x^4\sigma_z^2
  \end{gather*}}

  \caption{Representations of the graph state \(\ket{G}\) which correspons to the graph \(G = \Set{V, E}\) where \(V = \Set{1,2,3}\) and \(E = \Set{\Set{1,2}, \Set{2,3}, \Set{2,4}}\)}\label{fig:graph_state}
\end{figure}

An alternative definition of graph states with a more compact representation uses stabilizer formalism\cite{caves2014, quant-ph/9705052}. A stabilizer is defined to be a commutative subgroup of the \(N\)-qubit Pauli group \(\symcal{P}^V\) over the qubits in \(V\) that does not contain \(-\id_V\) or \(\pm i\id_V\)\cite{Briegel_2001,pusey2011}. For a given simple graph \(G=\p{G, E}\), the corresponding graph state \(\ket{G}\) is defined as the unique and common eigenvector to the set of independent commuting observables
\begin{equation}
  K_a = \paulix^a \pauliz^{N_a} = \paulix^a \prod_{b\in N_a} \pauliz^{b}
\end{equation}
with eigenvalues \(+1\). These observables, \(K_a\), are called \emph{correlation operators}. The commutative subgroup of \(\symcal{P}^V\) generated by the set \(\Set{K_a \given a \in V}\) is called \emph{the stabilizer of}\/ \(\ket{G}\). Due to the common eigenvalues of its generators, a stabilizer provides the following set of measurement correlations.
\begin{equation}
  s_x^a\prod_{b\in N_a}s_z^b = 1
\end{equation}
It is also possible to make a measurement on the graph state in a non-generator basis. A general Pauli measurement operator \(g\in\symcal{P}^V\) can have the following effects on the stabilizer with generators \(\langle g_1, g_2,\dots,g_n\rangle\)\cite{Nielsen2009}.
\begin{itemize}
  \item \(g\) might commute with all the generators of the stabilizer. In this case, either \(g\) or \(-g\) is a generator and a measurement will leave the state invariant.
  \item \(g\) might anti-commute with a generator \(g_1\). In this case, the observation will yield \(\pm1\) with equal probabilities and the new state will be stabilized by \(\langle g, g_2, \dots, g_n \rangle\)
\end{itemize}

\subsection{One-Way Quantum Computer}

Quantum computing literature is no stranger to various abstract machines like quantum Turing machines\cite{deutsch1985}, quantum finite automata\cite{SayY14}, or, most notably, quantum circuits. A common trait among these formulations is using unitary transformations to perform calculations. For each of these machines, their program states are traced by quantum state vectors modified only by unitary transformations until the program state is observed. We now define a measurement-based abstract machine, the one-way computer\cite{russendorf2001}.

A one-way quantum computer has two components:
\begin{description}[
  topsep=0pt,
  itemsep=-1ex,
  partopsep=1ex,
  parsep=1ex,
  leftmargin=2.5em,
  labelindent=1.5em,
  ]
  \item[A cluster \(\symcal{C}\)] that is made up of a \(d\) dimensional array of qubits. The state of a cluster can be described by a graph state with a regular lattice shape. A 2 dimensional, 2 by 3 cluster can be depicted by the following diagram.
  \begin{equation}
    \tikz[ baseline={([yshift=-.5ex]current bounding box.center)} ] \graph [
      nodes={draw, circle, inner sep=2.5pt, thick},
      empty nodes,
      edges = {thick}
    ] {
      a -- b -- c;
      d -- e -- f;
      a -- d; b -- e; c -- f;
    };
  \end{equation}
  \item[A random access measurement device] that is used to govern the program execution. The one-way computer takes a measurement patterns, composed of local measurement directions for each qubit, as its inputs.
  \begin{equation}
    \symcal{M} = \Set*{\,\vec{r}_a \given a \in \symcal{C}\,}
  \end{equation}
  The measurement device then applies these patterns to the cluster.
\end{description}

An essential feature of the one-way quantum computer is its universality. This feature can be proven constructively by providing measurement patterns that implement \(\cnot\) and arbitrary one qubit rotation unitary gates, which are proven to be a universal set of quantum gates\cite{Deutsch1995}. We now proceed to show that any unitary transformation, \(U\), can be simulated via a one-way computer up to a \emph{by-product} operator \(U_\Sigma\). The one-way computer simulates
\begin{equation}
  U' = U_\Sigma \, U
\end{equation}
These by-product operators result from the randomness inherent to the quantum measurements and are parameterized by the measurement outcomes. Since the measurement outcomes are acquired after each measurement, the one-way computer can apply corrections to its measurement basis adaptively to remove the effects of these operators.

A one-qubit rotation \(R(\alpha, \beta, \gamma)\) can be decomposed into the following relation using Euler angles\cite{daSilva2013}.
\begin{equation}
  R(\alpha, \beta, \gamma) = R_z(\gamma) R_x(\beta) R_z(\alpha)
\end{equation}
To implement \(R_z\) and \(R_x\) rotations, the following measurement basis \(\symcal{B}(\varphi) = \Set*{\ket{0_\varphi},\ket{1_\varphi} }\) is used.
\begin{equation}
  \symcal{B}(\varphi) = \Set*{\frac{\,\ket0 + e^{i\varphi}\ket1}{2}, \frac{\,\ket0 - e^{i\varphi}\ket1}{2} \,}
\end{equation}
A 2 qubit cluster prepared with an input state \(\ket\psi = a\ket0 + b\ket1\) can be expressed as
\begin{align}
  \ket{C} &= \operatorname{CZ} \ket\psi\ket+ \\
  &= \frac{1}{\sqrt2}\left( a \ket{00} + a \ket{01} + b \ket{10} + b\ket{11}\right).
\end{align}
a measurement of the first qubit of the cluster in \(\symcal{B}\) will project the the second qubit to \Lfrac{1/2}\([(a + e^{-i\varphi}b)\ket0 + (a - e^{-i\varphi}b)\ket1]\) if the measurement yields 0, and it will project the the second qubit to \Lfrac{1/2}\([(a - e^{-i\varphi}b)\ket0 + (a + e^{-i\varphi}b)\ket1]\) otherwise. Hence, measuring a qubit on \(\symcal{B}(\varphi)\) results in the unitary transformation
\begin{equation}
  \sigma_x^s \, H P(\varphi) = \sigma_x^s \, J(\varphi),
\end{equation}
where \(s\) is the measurement outcome and \(\sigma_x^s\) is the by-product operator. By measuring \(\symcal{B}\) with different angles, it is possible to implement \(R_z\) and \(R_x\) rotations. Using the relations
\begin{gather}
  R_z(\varphi) = J(0)J(\varphi) = H H P(\varphi) = P(\varphi) \\
  R_x(\varphi) = J(\varphi)J(0) = H P(\varphi) H = H R_z(\varphi) H
\end{gather}
it is possible to write \(R\) in terms of \(J\).
\begin{equation}
  R(\alpha, \beta, \gamma) = J(0)J(\gamma)J(\beta)J(\alpha)
\end{equation}
Performing four measurements on a 5 qubit linear cluster in basis \(\symcal{B}\) with angles \(\theta_1, \theta_2, \theta_3\) and 0 yields the following.
\begin{equation}
  \sigma_x^{s_4} J(0) \sigma_x^{s_3} J(\theta_3) \sigma_x^{s_2} J(\theta_2) \sigma_x^{s_1} J(\theta_1)
\end{equation}
By slightly modifying the measurement operators, it is possible to implement \(R\) up to a by-product operator. Let the new angles be
\begin{equation}
  \theta_1' = \alpha \quad \theta'_2 = (-1)^{s_1} \beta \quad \theta_3' = (-1)\gamma.
\end{equation}
Then these modified measurements with \(\theta'_i\) yield the following operator.
\begin{equation}
  \underbrace{\sigma_x^{s_2 + s_4} \sigma_z^{s_1 + s_3}}_{U_{\Sigma,R}} \underbrace{J(0) J(\gamma) J(\beta) J(\alpha)}_{R(\alpha, \beta,\gamma)}
\end{equation}
This operation can be represented as a diagram as
\begin{equation}
  \tikz[ baseline={([yshift=-.5ex]current bounding box.center)} ] \graph [
    nodes={draw, circle, inner sep=2.5pt, thick},
    empty nodes,
    edges = {thick}
  ] {
    d[fill=zx_green] -- e[label={$\scriptscriptstyle \alpha$}]  -- f[label={$\scriptscriptstyle \beta$}] -- g[label={$\scriptscriptstyle \gamma$}] -- h;
  };
\end{equation}
where a green vertex ( \tikz {\node[draw, circle, inner sep=1.5pt, fill=zx_green, thick] at (0,0) {};} )  representes a measurement in \(\sigma_z\), and an angle labelled vertex denotes a measurement along \(\symcal{B}\). Note that as a special case of this pattern, Hadamard operator can be expressed just by three \(\sigma_y\) measurements.
\begin{equation}
  \tikz[ baseline={([yshift=-.5ex]current bounding box.center)} ] \graph [
    nodes={draw, circle, inner sep=2.5pt, thick},
    empty nodes,
    edges = {thick}
  ] {
    d[fill=zx_green] -- e[fill=zx_yellow]  -- f[fill=zx_yellow] -- g[fill=zx_yellow] -- h;
  };
\end{equation}
Yellow vertices ( \tikz {\node[draw, circle, inner sep=1.5pt, fill=zx_yellow, thick] at (0,0) {};} ) represent measurements along \(\sigma_y\).

Simulating \(\cnot\) with a one-way computer is also possible. To do this, we implement the following measurement pattern,
\begin{equation}
  \tikz[ baseline={([yshift=-.5ex]current bounding box.center)} ]
  \graph [
    nodes={draw, circle, inner sep=2.5pt, thick},
    empty nodes,
    edges = {thick}
  ] {
    d[fill=zx_red, label=left:{Input}] -- e[fill=zx_red] -- f[label=right:{Output}];
    a[draw=none] --[draw=none]  g[label=right:{Control}] -- e;
  };
\end{equation}
where a red vertex ( \tikz {\node[draw, circle, inner sep=1.5pt, fill=zx_red, thick] at (0,0) {};} ) denotes a measurement along \(\sigma_x\). To see how this operation works, consider a graph state with its input qubit initialized to \(\ket{\psi_{i}} = a\ket0+b\ket1\) and control qubit initialized to \(\ket{\psi_c} = c\ket0+d\ket1\).
\begin{align}
  \ket{G} &= CZ^{\scriptscriptstyle(24)}CZ^{\scriptscriptstyle(23)}CZ^{\scriptscriptstyle(12)}\ket{\psi_i} ^{\scriptscriptstyle(1)}\ket+^{\scriptscriptstyle(2)}\ket+^{\scriptscriptstyle(3)}\ket{\psi_c}^{\scriptscriptstyle(4)} \\
  &= \begin{multlined}[t]
    a\ket0^{\scriptscriptstyle(1)} \left[\ket0^{\scriptscriptstyle(2)}\ket+^{\scriptscriptstyle(3)}(c\ket0+d\ket1)^{\scriptscriptstyle(4)} + \ket1^{\scriptscriptstyle(2)}\ket-^{\scriptscriptstyle(3)}(c\ket0-d\ket1)^{\scriptscriptstyle(4)} \right] \\
    + b\ket1^{\scriptscriptstyle(1)}\left[ \ket0^{\scriptscriptstyle(2)}\ket+^{\scriptscriptstyle(3)}(c\ket0+d\ket1)^{\scriptscriptstyle(4)} - \ket1^{\scriptscriptstyle(2)}\ket-^{\scriptscriptstyle(3)}(c\ket0-d\ket1)^{\scriptscriptstyle(4)}\right]
  \end{multlined}
\end{align}
A measurement of the first two qubits will which resulted in \(s_1=0\) an \(s_2=0\) will yield project the state of qubits into
\begin{align}
  \ket{G'}^{\scriptscriptstyle(34)} &= \bra+^{\scriptscriptstyle(1)}\bra+^{\scriptscriptstyle(2)}\ket{G}^{\scriptscriptstyle(1234)}\\
  &= ac\ket{00}^{\scriptscriptstyle(34)} + bd\ket{01}^{\scriptscriptstyle(34)} + bc{10}^{\scriptscriptstyle(34)} + ad\ket{11}^{\scriptscriptstyle(34)} \\
  &= \cnot^{\scriptscriptstyle(43)} \ket{\psi_c}^{\scriptscriptstyle(4)} \ket{\psi_i}^{\scriptscriptstyle(3)}
\end{align}
Hence the operation implemented \(\cnot\) on the input and control qubits and wrote the outcome to the output qubit. For different measurement values for \(s_1\) or \(s_2\), \(\cnot\) is implemented up to the following by-product operator.
\begin{equation}
  U_{\Sigma, \cnot} = \left(\sigma_x^{\scriptscriptstyle(3)}\right)^{s_2} \left(\sigma_z^{\scriptscriptstyle(3)}\right)^{s_1}\left(\sigma_z^{\scriptscriptstyle(4)}\right)^{s_1}
\end{equation}

As we have shown, both \(\cnot\) and arbitrary rotation gates can be implemented on a one-way computer. Therefore, it is possible to simulate any computation that is possible on a quantum circuit with a one-way quantum computer by combining these measurement patterns, as described by Figure \ref{fig:flow}.

\begin{figure}[htb]
  \centering
  \includegraphics[
    width=0.75\textwidth
    ]{fig/flow.eps}
  \caption{Simulation of a quantum circuit with a one-way computer. Horizontal direction simulates the circuit's time evolution and vertical direction represents the qubit register. \(\odot\) vertices denote a measurement along \(\sigma_z\), a vertical arrow denotes a measurement along \(\sigma_x\) and a tilted arrow denotes a measurement along the \(xy\) plane. The image by Raussendorf et. al.\cite{russendorf2003}} \label{fig:flow}
\end{figure}

After introducing a general framework for simulating quantum circuits, we introduce two additional measurement patterns. The first one among these is a measurement by \(\sigma_z\). This measurement effectively deletes a vertex from the cluster without affecting the program logic. A 2 qubit cluster state with an input \(\ket{\psi} = a\ket0+b\ket1\) has the following quantum state.
\begin{align}
  \ket{G} &= CZ\ket\psi\ket+\\
  &= \frac{1}{\sqrt2}(a\ket{00} + a\ket{01} + b\ket{10} - b\ket{11})
\end{align}
Measuring the second qubit will yield \(a\ket0+b\ket1\) if the outcome is 0, and it will yield \(a\ket0-b\ket1\) if otherwise. So a measurement on \(\sigma_z\) removes the qubit with a side effect operator \(\sigma_z^s\).

The last measurement pattern is the \(\sigma_x\) measurement, which propagates the information in a linear cluster to the next vertex. The by-product operators are cyclic, with a period of four propagations.
\begin{equation}
  U_\Sigma =  \sigma_x^{s_1} \sigma_z^{s_2}
\end{equation}


\input{../header_class}

%---------- start document ---------- %
 \section{lattice -- Lattice}\linkedzero{lattice}
 \begin{itemize}
 \item {\bf Classes}
   \begin{itemize}
   \item \linkingone{lattice}{Lattice}
   \item \linkingone{lattice}{LatticeElement}
   \end{itemize}
 \item {\bf Functions}
   \begin{itemize}
   \item \linkingone{lattice}{LLL}
   \end{itemize}
 \end{itemize}
%
  \subsection{Lattice -- lattice}\linkedone{lattice}{Lattice}
  \initialize
  \func{Lattice}{
    \hiki{basis}{\linkingone{matrix}{RingSquareMatrix}},\ \hiki{quadraticForm}{\linkingone{matrix}{RingSquareMatrix}}}{\out{Lattice}}\\
  \spacing
  % document of basic document
  \quad Create Lattice object. \\
  \spacing
  % added document
  \spacing
  % input, output document
  \begin{at}
    \item[basis]\linkedtwo{lattice}{Lattice}{basis}: The basis of \param{self} lattice.
    \item[quadraticForm]\linkedtwo{lattice}{Lattice}{quadraticForm}: The quadratic form corresponding the inner product.
  \end{at}
\C
  \method
  \subsubsection{createElement -- create element}\linkedtwo{lattice}{Lattice}{createElement}
  \func{createElement}{\param{self}, \ \hiki{compo}{list}}{\out{\linkingone{lattice}{LatticeElement}}}\\
  \spacing
  % document of basic document
  \quad Create the element which has coefficients with given \param{compo}. \\
  \spacing
  % add document
  %\spacing
  % input, output document
%
  \subsubsection{bilinearForm -- bilinear form}\linkedtwo{lattice}{Lattice}{bilinearForm}
  \func{bilinearForm}{\param{self}, \ \hiki{v\_1}{\linkingone{vector}{Vector}}, \, \hiki{v\_2}{\linkingone{vector}{Vector}} }{\out{integer}}\\
  \spacing
  % document of basic document
  \quad Return the inner product of $v_1$ and $v_2$ with \linkingtwo{lattice}{Lattice}{quadraticForm}. \\
  \spacing
  % add document
  %\spacing
  % input, output document
%
  \subsubsection{isCyclic -- Check whether cyclic lattice or not}\linkedtwo{lattice}{Lattice}{isCyclic}
  \func{isCyclic}{\param{self}}{\out{bool}}\\
  \spacing
  % document of basic document
  \quad Check whether \param{self} lattice is a cyclic lattice or not.
  \spacing
  % add document
  \quad
  %\spacing
  % input, output document
%
  \subsubsection{isIdeal -- Check whether ideal lattice or not}\linkedtwo{lattice}{Lattice}{isIdeal}
  \func{isIdeal}{\param{self}}{\out{bool}}\\
  \spacing
  % document of basic document
  \quad Check whether \param{self} lattice is an ideal lattice or not.
  \spacing
  % add document
  %\spacing
  % input, output document
\C
  \subsection{LatticeElement -- element of lattice}\linkedone{lattice}{LatticeElement}
  \initialize
  \func{LatticeElement}{
   \hiki{lattice}{\linkingone{lattice}{Lattice}},\
   \hiki{compo}{list},\
  }{
   \out{LatticeElement}
  }\\
  \spacing
  % document of basic document
  \quad Create LatticeElement object. \\
  \spacing
  % added document
  \quad Elements of lattices are represented as linear combinations of basis.
  The class inherits \linkingone{matrix}{Matrix}. Then, intances are regarded as $n \times 1$ matrix whose coefficients consist of \param{compo}, where $n$ is the dimension of lattice.\\
  \spacing
  % input, output document
  \quad \param{lattice} is an instance of Lattice object. \param{compo} is coeeficients list of basis.
  %
  \begin{at}
    \item[lattice]\linkedtwo{lattice}{LatticeElement}{lattice}: the lattice which includes \param{self}
  \end{at}
\C
  \method
  \subsubsection{getLattice -- Find lattice belongs to}\linkedtwo{lattice}{LatticeElement}{getLattice}
  \func{getLattice}{\param{self}}{\out{\linkingone{lattice}{Lattice}}}\\
  \spacing
  % document of basic document
  \quad Obtain the Lattice object corresponding to \param{self}. \\
  \spacing
  % add document
  %\spacing
  % input, output document
\C
  \subsection{LLL(function) -- LLL reduction}\linkedone{lattice}{LLL}
  \func{LLL}{\hiki{M}{\linkingone{matrix}{RingSquareMatrix}}}{\out{\hiki{L}{RingSquareMatrix}}, \ \out{\hiki{T}{RingSquareMatrix}}} \\
  \spacing
  % document of basic document
  \quad Return LLL-reduced basis for the given basis \param{M}. \\
  \spacing
  % add document
  % \spacing
  % input, output document
  \quad The output \param{L} is the LLL-reduced basis. \param{T} is the transportation matrix from the original basis to the LLL-reduced basis.
%
\begin{ex}
>>> M=mat.Matrix(3,3,[1,0,12,0,1,26,0,0,13]);
>>> lat.LLL(M);
([1, 0, 0]+[0, 1, 0]+[0, 0, 13], [1L, 0L, -12L]+[0L, 1L, -26L]+[0L, 0L, 1L])
\end{ex}%Don't indent!(indent causes an error.)
\C

%---------- end document ---------- %

\input{../footer}


\subsection{Illustrative Example}
Despite the differences in the statistical interpretations, we formulate a problem where $\observed$ and $\initial$ match the forms of $L_\dspace$ and $\prior$.
However, we still observe differences between $\updated$ and $\posterior$ due to the use of a normalizing constant $C$ in $\posterior$ and the use of $\predicted$ in $\updated$.
We explore the impact of this difference in the denominators of the solutions in the following example.

%%%%%

\begin{ex}
Suppose $\pspace = [-1,1]\subset\RR$ and $Q(\param)=\param^5$ so that $\dspace = [-1,1]$.
For the data-consistent framework, we assume $\initial\sim \mathcal{U}([-1,1])$ and $\observed\sim N(0.25,0.1^2)$.
The push-forward of initial PDF, the observed PDF, and the updated PDF are shown in Fig.~\ref{fig:bayes-comparison}.

For the Bayesian inverse problem, we assume $d\in \dspace$ with $d=Q(\paramref)+\xi$ where $\xi\sim N(0,0.1^2)$.
%In particular, we assume that $d=0.25$ and follow the process of \cite{Stuart10} to form the data-likelihood function so that it matches the observed density.
We then construct $\pi_{\text{post}}(\param \, |\, d)$ for this example assuming a uniform prior (to match the initial density) with an assumed observed value of $d=0.25$ so that the data-likelihood function matches the observed density.
The posterior and its push-forward are also shown in Fig.~\ref{fig:bayes-comparison}.

\begin{figure}[htbp]
\centering
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-1.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-pf-1.png}
 \caption{(Left) The initial/prior PDF $\initial$ (blue solid curve), updated PDF $\updated$ (black dashed curve), and posterior PDF $\pi_\text{post}$ (green dashed-dotted curve) on $\Lambda$.
 (Right) The push-forward (PF) of the initial/prior PDF $\predicted$ (blue solid curve), observed/likelihood PDF (red solid curve), PF of the updated PDF $\updated$ (black dashed curve), and the PF of the posterior PDF $\pi_\text{post}$ (green dashed-dotted curve) for the QoI.}
 \label{fig:bayes-comparison}
\end{figure}

While the updated and posterior densities in Fig.~\ref{fig:bayes-comparison} share certain similarities (e.g., they are uni-modal with similar locations of the mode), they are otherwise visibly distinct.
The differences between these densities is made more evident by examining their push-forwards.
The push-forward of the updated density agrees well with the observed density, which is to be expected.
However, the push-forward of the posterior is bi-modal and does not match the observed density, which we recall is identical to the data-likelihood function in this case.
%with peaks that appear to align fairly well with the two distinct peaks of the predicted density and observed density.
%Recall that the observed density and data-likelihood are, in this case, identical.
%Moreover, with the setup described above, the predicted density can also be interpreted as the push-forward of the prior density.
%This demonstrates the regularizing impact of the prior on the posterior and how i.

%
%Hierarchical Bayesian methods \cite{} extend this typical framework to problems where aleatoric uncertainties are present, but are still fundamentally developed from a  point estimation perspective.
%Specifically, prior distributions are specified from a parametric family of distributions, such as Gaussian distributions, and the hyper-parameters used to define that family of distributions, such as the means and variances, become a focal point of estimation by the methodology.

\end{ex}

The takeaway to the above discussion and example is that each density is solving a {\em different} inverse problem.
The posterior density is intended to provide point estimates of a true parameter value whereas the updated density is intended to quantitatively characterize natural variations in parameter values.
We reformulate the previous example to make the role of data collection more central in the follow example.

\begin{ex}
For the (Bayesian) Deterministic Inverse Problem, suppose $Q(\paramref)=0.25$ and noisy measurement data are drawn from a $N(0.25,0.1^2)$, i.e., we assume that each datum is given by $d=Q(\paramref)+\xi$ where $\xi\sim N(0,0.1^2)$.
For the SIP, we use the sample mean and variance of data to estimate the ``exact'' observed $N(0.25,0.1^2)$ distribution.
The observed density and data-likelihood become significantly different from one another as more data are collected.
The data-likelihood is in fact given by a product of normal densities.

We draw $M=5, 10, \text{ and}, 20$ samples to form estimates of $\observed$ and the likelihood functions.
We show the results in Figure~\ref{fig:bayes-comparison-convergence}.

\begin{figure}[htbp]
\centering
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-5.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-pf-5.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-10.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-pf-10.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-20.png}
   \includegraphics[width=0.49\linewidth]{figures/bip-vs-sip-pf-20.png}
 \caption{(Top to Bottom): $S=5, 10, \text{ and}, 20$ samples are used to solve the SIP and DIP for comparison. (Left) The initial/prior PDF $\initial$ (blue solid curve), updated PDF $\updated$ (black dashed curve), and posterior PDF $\pi_\text{post}$ (green dashed-dotted curve) on $\Lambda$.
 (Right) The push-forward (PF) of the initial/prior PDF $\predicted$ (blue solid curve), observed/likelihood PDF (red solid curve), PF of the updated PDF $\updated$ (black dashed curve), and the PF of the posterior PDF $\pi_\text{post}$ (green dashed-dotted curve) for the QoI.}
 \label{fig:bayes-comparison-convergence}
\end{figure}

For all values of $M$, the push-forward of the initial remains the same, and the push-forward of the update matches the observed.
By contrast, the posterior increases in confidence alongside the predictions it produces.
This further illustrates that the DIP and SIP are fundamentally different problems (they are addressing different questions).
As more data are incorporated, the goal of the DIP is to reduce epistemic uncertainty; for the SIP, it is to quantify the aleotoric uncertainty.

\end{ex}

%%%%%

In summary, it is not the goal of Bayesian inference to construct a pullback distribution.
Bayesian inverse problems are fundamentally posed as parameter-identification problems, not distribution estimation problems.
However, one could assume that a posterior on $\pspace$ can be expressed as a Gaussian distribution, and solve for the most likely mean and standard deviation that characterizes it \citep{Smith, Tarantola_book}.
This defines what is commonly referred to a as a Hierarchical Bayesian Inverse Problem.

More complex densities can be approximated by mixture models.
For example, one can assume that the posterior can be given by a linear combination of four Gaussian distributions, and solve for eight parameter values (four standard deviations and means).
However, the operative word here is \emph{assume}; in order to capture a density using a Bayesian framework, one needs to impose some sort of explicit structure on the posterior.
No such assumption is required in the DCI framework.
Distributions (or measures) can be solved for directly, regardless of any nonlinear/non-parameteric structure by leveraging the measure-theoretic approaches described in \cite{BE13} or \cite{BJW18a}.

It is important to note that Hierarchical Bayesian inverse problem still casts a distribution-estimation problem in the context of parameter identification.
As a complementary line of reasoning, we seek to formulate a parameter identification problem in a DCI framework.
For example, the mean of the updated density could be used as an estimator to address the parameter identification problem.
However, collecting more data does not improve confidence if used to estimate $\observed$ as described above unless we revisit the results with a focus on parameter estimation by considering alternative data-driven ways to construct the QoI (and subsequently $\observed$).

In Chapter~\ref{chapter:mud}, we motivate the use of the maximal updated density point (maximizing the update), as a means of providing a useful point estimate to parameters.
Before we proceed, we finish summarizing key results about the stability and numerical convergence of the updated solution \eqref{eq:updated-pdf} in the next section.

\FloatBarrier


	\documentclass{standalone}
	\begin{document}
	\chapter{Partial Fractions}
	\section{Introduction}
	\emph{Consider the expression and suppose it is simplified:}

	\begin{alignat*}{2}
		\frac{2}{x+1}+\frac{3}{2x-5} & = \frac{2(2x-5)+3(x+1)}{(x+1)(2x-5)} \\
		& =\frac{4x-10+3x+3}{(x+1)(2x-5)}      \\
		& =\frac{4x-10+3x+3}{(x+1)(2x-5)}      \\
		& =\frac{7x-7}{(x+1)(2x-5)}            \\
	\end{alignat*}

	In this chapter we reverse the approach above, hence decomposing one fraction to its corresponding partial fractions.
	\newpage
	\section{Types of partial fraction cases}
	\subsection*{\underline{Type 1: Linear Factors in denominator}}
	\begin{example}
		Decompose $ \frac{7x-7}{\left(x+1\right)}$ into Partial Fractions
	\end{example}
	\begin{alignat*}{2}
		&            & \frac{7x-7}{\left(x+1\right)} & \equiv \frac{A}{\left(x+1\right)} + \frac{B}{\left(2x-5\right)} \\
		& \implies   & 7x-7                          & = A\left(2x-5\right)+B\left(x+1\right))                         \\
		& \implies   & 7(-1)-7                       & = A(2(-1)-5)\tag*{$x = -1$}                                     \\
		& \implies   & -14                           & = -7A                                                           \\\leqnomode
		& \implies   & A                             & = 2\tag{..1}                                                    \\
		& \implies   & 7\left(\frac{5}{2}\right)-7   & =B\left(\left(\frac{5}{2}\right)+1\right)\tag*{$x=\frac{5}{2}$} \\
		& \implies   & \frac{35}{2} - 7              & = \frac{7B}{2}                                                  \\
		& \implies   & B                             & =3\tag{..2}                                                     \\\\
		& \therefore & \frac{7x-7}{\left(x+1\right)} & = \frac{2}{\left(x+1\right)} + \frac{3}{\left(2x-5\right)}
	\end{alignat*}

	\subsection*{\underline{Type 2: Irreducible Quadratic Factor in Denominator}}
	\begin{example}
		Decompose $\frac{x^2 +1}{(2x+1)(x^2 +3)}$ into its corresponding partial fractions.
	\end{example}
	\begin{alignat*}{2}
		&            & \frac{x^2 +1}{(2x+1)(x^2 +3)} & \equiv \frac{A}{2x+1}+\frac{Bx+C}{x^2 +3}       \\
		& \implies   & x^2 + 1                       & \equiv A(x^ 2 + 3) + (Bx+C)(2x+1)               \\
		& \implies   & x^2 + 1                       & \equiv x^2(A+2B) + x(2B + 2C) + (3A+C)\tag{*..} \\
		\intertext{\quad At this stage, since both equations are identical, we analyse the different coefficients and constants to form a system of equations to solve.}
		\intertext{Comparing coefficients of $x^2\colon$}
		&            & 1                             & = A+2B\tag{1..}                                 \\
		\intertext{Comparing coefficients of $x\colon$}
		&            & 0                             & = B+C\tag{2..}                                  \\
		\intertext{Comparing constants$\colon$}
		&            & C                             & = 1-3A\tag{3..}                                 \\
		\intertext{Substituting  3.. in 2..}
		&            & 0                             & =B+1-3A                                         \\
		& \implies   & B                             & =3A-1\tag{4..}                                  \\
		\intertext{Substituting 4.. in 1..}
		&            & 1                             & = A + 2(3A-1)                                   \\
		& \implies   & A                             & = \frac{-1}{7} \tag{5..}                        \\
		\intertext{Substituting 5.. in 1..}
		&            & 1                             & = \frac{-1}{7} +2B                              \\
		& \implies   & -7                            & = 1-14B                                         \\
		& \implies   & B                             & =\frac{4}{7}\tag{6..}                           \\
		\intertext{Substituting 5.. in 3..}
		&            & C                             & = 1-3\left(\frac{-1}{7}\right)                  \\
		& \implies   & C                             & = \frac{10}{7}                                  \\
		& \therefore & \frac{x^2 +1}{(2x+1)(x^2 +3)} & \equiv \frac{A}{2x+1}+\frac{Bx+C}{x^2 +3}
	\end{alignat*}
	\newpage
	\subsection*{\underline{Type 3: Repeated factor in the denominator}}
	\begin{example}
		Decompose $\frac{x+1}{(x+2)(x-3)^2}$ into its corresponding partial fractions.
	\end{example}
	\begin{alignat*}{2}
		&            & \frac{x+1}{(x+2)(x-3)^2} & \equiv \frac{A}{x+2} + \frac{B}{x-3} + \frac{C}{(x-3)^2}           \\
		& \implies   & x+1                      & \equiv A(x-3)^2 + B(x-3)(x+2) + C(x+2)                             \\
		& \implies   & x+1                      & \equiv Ax^2 - 6Ax +9A + Bx^2-Bx-6B + Cx+2C)                        \\
		& \implies   & -2+1                     & =A(-2-3)^2\tag*{$x=-2$}                                            \\
		& \implies   & A                        & = \frac{-1}{25}                                                    \\
		& \implies   & 3+1                      & =C(3+2)\tag*{$x=3$}                                                \\
		& \implies   & C                        & =\frac{4}{5}                                                       \\
		\intertext{Comparing coefficients of $x^2\colon$}
		& \implies   & 0                        & = A + B                                                            \\
		& \implies   & B                        & = \frac{1}{25}                                                     \\
		& \therefore & \frac{x+1}{(x+2)(x-3)^2} & \equiv \frac{1}{25(x-3)} + \frac{4}{5(x-3)^2} - \frac{-1}{25(x-3)} \\
	\end{alignat*}

	The approach above, is similar to the previous one, with the addition of the fact that each repeated factor has to be listed in order of powers until its own.
	\newpage
	\subsection*{\underline{Type 4: Improper fraction}}
	\begin{example}
		Decompose $\frac{2x^2 -8x +11}{2x-5}$ into its corresponding partial fractions.
	\end{example}
	Since the fraction is improper, or top-heavy\footnotemark[2] it is required to perform a polynomial long division and acquire the proper terms. \footnotetext[2]{Improper fractions containing a variable are recognized by the order of the exponent when the expression is expanded. i.e $\frac{x^2}{x+5}$ is regarded as improper}
	\begin{center}
		\polylongdiv{2x^2 -8x +11}{2x-5}
	\end{center}
	\begin{alignat*}{2}
		& \therefore\quad & \frac{2x^2 -8x +11}{2x-5} & \equiv 	x - \frac{3}{2} + \frac{7}{2(2x-5)}
	\end{alignat*}
	\newpage
	\end{document}


Labeling the expensive Paramak model $f(x)$, a surrogate \textcolor{red}{model is a function}
$\hat{f}(x)$ such that $f(x)$ and $\hat{f}(x)$ minimize a selected dissimilarity
metric. In order to be considered \textit{viable}, $\hat{f}(x)$ is required to
achieve \textcolor{red}{an} expected evaluation time lower than that of~$f(x)$. In this work, we
consider \textcolor{red}{two} methods of producing viable surrogates: (1)~a conventional decoupled
approach, which evaluates $f(x)$ on a set of \textcolor{red}{uniformly-random samples} and
trains surrogates in a supervised scheme, and (2)~an adaptive approach, which attempts to
compensate for localized regression performance \textcolor{red}{insufficiencies} by interleaving
multiple epochs of sampling and training. \textcolor{red}{Several high-accuracy and deployment-ready surrogate models are developed using the decoupled approach, and their performance characterized numerically, while the adaptive approach is studied as a proof-of-concept.}

\begin{table}[t]
	\setlength\tabcolsep{1pt}
	\renewcommand{\arraystretch}{0.95}
	\caption{\label{tbl:surrogates}Considered surrogate model families, their
		selected abbreviations and implementations. $\mathcal{H}$~denotes the
		set of hyperparameters, \textcolor{red}{family-dependent priors that control the
		learning process, and are tuned separately. Families with fewer
		hyperparameter represents a smaller surogate domain to explore.}}
	\begin{indented}
	\item[]
		\begin{tabular}{lllr}
		\toprule
		Surrogate family & Abbr. & Impl. & $|\mathcal{H}|$ \\
		\midrule
		Support vector machines~\cite{fan2008liblinear}	& SVM & SciKit~\cite{scikit-learn} & 3 \\
		Gradient boosted trees~\cite{friedman2001greedy,friedman1999stochastic,hastie2009elements}	& GBT & SciKit & 11 \\
		Extremely randomized trees~\cite{geurts2006extremely}	& ERT & SciKit & 7 \\
		AdaBoosted decision trees$^\text{a}$~\cite{drucker1997improving}	& ABT & SciKit & 3 \\
		Gaussian process regression~\cite{williams2006gaussian}	& GPR & SciKit & 2 \\
		$k$ nearest neighbours	& KNN & SciKit & 3 \\
		Artificial neural networks	& ANN & Keras~\cite{chollet2015keras} & 2 \\
		Inverse distance weighing~\cite{shepard1968two} & IDW & SMT~\cite{SMT2019} & 1 \\
		Radial basis functions & RBF & SMT & 3 \\
		\bottomrule
		\end{tabular}\\%
		{\footnotesize $^\text{a}$Note that ABTs can be viewed as a subclass of GBTs.}
	\end{indented}
\end{table}

\begin{table*}[t]
	\renewcommand{\arraystretch}{0.95}
	\caption{\label{tbl:metrics}Metrics recorded in experiments. In
	formulations, we work with a training set of size $N_0$ and a test set of
size $N$, values $y^{(i)}=f(x^{(i)})$ and $\hat{y}^{(i)}=\hat{f}(x^{(i)})$
denote images of the $i$th testing sample in Paramak and the surrogate
respectively. The mean $\overline{y}=N^{-1}\sum_{i=1}^N y^{(i)}$ and $P$ is the
number of input features.}
	\begin{indented}
	\item[]
		\begin{tabularx}{\textwidth}{Xrl}
		\toprule
		Regression performance metrics& Notation	& Mathematical formulation\\
		\midrule
		Mean absolute error	& MAE & $N^{-1}\sum_{i=1}^N |y^{(i)}-\hat{y}^{(i)}|$ \\
		Standard deviation of error & $S$	& $\text{StdDev}_{i=1}^N\left\{ |y^{(i)} -
		\hat{y}^{(i)}| \right\} $ \\
			Coefficient of determination & $R^2$	& $1-\sum_{i=1}^N
			\left(y^{(i)}-\hat{y}^{(i)} \right)^2\left[\sum_{i=1}^N \left(
			y^{(i)}-\overline{y} \right)^2\right]^{-1} $ \\
			Adjusted $R^2$ & $R^2_\text{adj.}$	& $1-(1-R^2)(N-1)(N-P-1)^{-1}$ \\
		\midrule
		Computational complexity metrics	& {}	& {} \\
		\midrule
		Mean training time & $\overline{t}_{\text{trn.}}$	& $(\text{wall training time of
		$\hat{f}(x)$})N_0^{-1}$  \\
			Mean prediction time & $\overline{t}_{\text{pred.}}$	& $(\text{wall prediction time of
			$\hat{f}(x)$})N^{-1}$ \\
				Relative speedup & $\omega$	& $(\text{wall evaluation$^\text{b}$ time of $f(x)$})
				(N\overline{t}_{\text{pred.}})^{-1}$ \\
		\bottomrule
		\end{tabularx}\\%
		{\footnotesize $^\text{b}$This corresponds to evaluation of Paramak
		 on all points of the test set. In surrogates, the equivalent
		time period is referred to as \textcolor{red}{the} ``prediction time.''}
	\end{indented}
\end{table*}

We selected several state-of-the-art regression algorithms to perform
surrogate training on sampled point sets. Listed in~\Tref{tbl:surrogates}, these
implementations define \textcolor{red}{nine} surrogate families \textcolor{red}{which are detailed} in~\Sref{sec:results}.
We note that each presented algorithm defines hyperparameters that may influence its
performance. Their problem-specific optimal values are searched within the scope
of this work, in particular in Experiments~1 \&~2 that are outlined
in~\Sref{sec:experiment-methodology}.

To compare the quality of the produced surrogates, we define a \textcolor{red}{variety} of metrics listed
in~\Tref{tbl:metrics}. For regression performance analysis, we include a
selection of absolute metrics \textcolor{red}{(MAE, $S$)} to assess the models' approximation capability
and set practical bounds on the expected uncertainty of their predictions. In addition, we also track
relative measures \textcolor{red}{($R^2$, $R^2_\text{adj.}$)} that are better-suited for comparison between this work and others as
they are invariant with respect to the selected domain and image space.
For analysis of computational complexity, surrogates are assessed in terms of wall
time (captured by the Python~3 \texttt{time} package). This is motivated by common practical \textcolor{red}{use-cases} of our work, where surrogate models are trained as replacements for
Paramak. All times reported (training, test, evaluation) are
normalized by the corresponding dataset size, i.e.~correspond to ``time to
process a single datapoint.''

Even though some surrogates support acceleration by means of \textcolor{red}{parallelization}, we
used non-parallelized implementations. The only exception to this \textcolor{red}{is the ANN family},
which \textcolor{red}{requires} a considerable amount of processing power for training on
conventional CPU architectures. Lastly, to prevent undesirable bias by training
set selection, all reported metrics are obtained via 5-fold cross-validation.
In this setting, a sample set is uniformly divided into \textcolor{red}{five} disjoint folds, each of which
is used as a test set for models trained on the remaining \textcolor{red}{four}. Having repeated the
same experiment for each division, the overall value of individual metrics is reported in terms of their mean and standard deviation over all folds.

\subsection{Decoupled Approach}\label{sec:experiment-methodology}

Experiments related to the decoupled approach are organized in \textcolor{red}{four} parts,
further described in this section. In summary, we aim to optimize the hyperparameters of
each surrogate family separately, \textcolor{red}{and later compare the best results between
families.}

The objective of Experiment~1 is to simplify the regression task for
surrogates prone to suboptimal performance in \textcolor{red}{discrete} spaces.
To this end, training points are filtered to a single selected discrete feature
assignment, and surrogates are trained only on the remaining continuous features.
This is repeated several times to explore variances in behavior,
particularly in \textcolor{red}{four} distinct assignments that are obtained by setting blanket and
first wall coolant materials to one
of:~$\{\text{H\textsubscript{2}O},\isotope{He}\}$.
Experiment~2 conventionally measures surrogate performance on the full feature
space without any parameter restrictions. In both experiments, hyperparameter tuning is
facilitated by Bayesian optimisation~\cite{movckus1975bayesian}, where we select the
hyperparameter configuration that produces the model that maximizes $R^2$. The
process is terminated after 1000~iterations or \textcolor{red}{two} days, whichever condition is satisfied first.
The results of Experiments~1 \&~2 are depicted
in Figures~\ref{fig:exp1-time-vs-reg} \&~\ref{fig:exp2-time-vs-reg}
respectively, and described in~\Sref{sec:res-exp12}.

In Experiment~3, the \textcolor{red}{twenty} best-performing hyperparameter configurations
\textcolor{red}{for} each model family are used to train surrogates on sets of various sizes to
investigate their scaling properties. In particular, we track the metrics
from~\Tref{tbl:metrics} as functions of training set size (1, 2, 5, 10, 12, 15 and 20 thousands
of samples) individually \textcolor{red}{for} each family. This allows their comparison based on
observed trends, and estimation of optimal training set sizes.
The results of this experiment are shown in~\Fref{fig:scaling} and discussed
in~\Sref{sec:res-exp3}.

Finally, Experiment~4 aims
to produce surrogates suitable for practical use by retraining selected
well-scaling instances on large training sets\textcolor{red}{.}
The results of this process are displayed in~\Fref{fig:reg-performance} and
in~\Tref{tbl:exp4-detailed-results}, and summarized in~\Sref{sec:res-exp4}.

\subsection{Adaptive Approach}\label{sec:adaptive}

Adaptive sampling techniques appear frequently in the literature and have been
specialized for surrogate modelling, where precision is implicitly limited by the quantity of training samples which are available from the expensive model. Garud's~\cite{Garud2016} ``Smart Sampling Algorithm'' achieved notable success by incorporating surrogate quality and
crowding distance scoring to identify optimal new samples, but was only tested
on a single-parameter domain. We theorized that a nondeterministic sample
generation approach, built around Markov Chain Monte Carlo methods (MCMC), would
fare better for high-dimensional models by more thoroughly exploring all local
optima in the feature space. MCMC produces each sample \textcolor{red}{point} according to a \textcolor{red}{jump step drawn from a} shared proposal
distribution. These sample points will converge to a desired posterior
distribution, so long as the acceptance probability meets certain statistical criteria (see~\cite{Zhou2018} for a review).

\begin{figure}
	\centering
	\hspace*{-5pt}\includegraphics[width=1.28\linewidth]{fig4_qassplan.png}
	\caption{\label{fig:qassplan}Schematic of QASS algorithm}
\end{figure}

Many researchers have embedded surrogate methods into MCMC strategies for
parameter optimisation~\cite{Zhang2020,Gong2017}, in particular the ASMO-PODE
algorithm~\cite{Ginting2011} which makes use of MCMC-based adaptive sampling. Our approach draws inspiration from ASMO-PODE, but instead uses MCMC to generate samples
which increase surrogate precision throughout the entire parameter space.

We designed the Quality-Adaptive Surrogate Sampling algorithm (QASS, \Fref{fig:qassplan}) to iteratively increment the training/test set with sample
points which maximize surrogate error and minimize a crowding distance metric
(CDM)~\cite{Solonen2012} in feature space. \textcolor{red}{Error maximization is desirable for these sample points because it identifies regions of parameter space where the surrogate most needs to be improved.} On each iteration following an initial training of the surrogate on $N$ uniformly random samples, the surrogate was trained and absolute error calculated. MCMC was then performed to sample the error function generated by performing nearest-neighbor interpolation on these test error points. The resultant samples were culled by $50\%$ according to the CDM, and then the $n$ highest-error candidates were selected for reintegration with the training/test set, beginning another training epoch. Validation was also performed during each iteration on independent, uniformly-random sample sets.


\chapter{IMSRG(3) for the pairing Hamiltonian}\label{app:pairing_hamiltonian_imsrg3}

The pairing Hamiltonian is given by
\begin{equation}\label{eq:pairing_hamiltonian}
    H = \delta \sum_{p\sigma} (p - 1) \crea{p\sigma} \annih{p\sigma}
    - \frac{g}{2} \sum_{p q} \crea{p+}\crea{p-} \annih{q-} \annih{q+}\,,
\end{equation}
where we have equally-spaced two-fold degenerate levels indexed by the quantum number $p$
and an attractive (for $g > 0$) pairing interaction.
Cooper first considered this Hamiltonian in 1956~\cite{Coop56pairing_hamiltonian},
which led to the successful Bardeen-Cooper-Schreifer (BCS) theory of superconductivity~\cite{Bard57bcs}.
The exact eigenvalues of the pairing Hamiltonian were given by Richardson in 1963,
where the solutions are obtained via the solution of the non-linear coupled Richardson equations~\cite{Rich63pairing_hamiltonian}.

We focus on a restricted case where $p=1,\ldots,4$ and $\delta=1\mev$,
and we vary the strength of the pairing interaction $g$.
We are interested in the ground state of four fermions,
for which our reference state is the state with the two lowest levels completely filled,
\begin{equation}\label{eq:pairing_hamiltonian_reference}
    \refgnd = \crea{2-}\crea{2+}\crea{1-}\crea{1+}\ket{0}\,.
\end{equation}
This system has a couple of useful properties:
First, the number of single-particle states is only eight,
making the IMSRG(3) calculation relatively tractable.
Additionally, one can increase the number of levels $p_{\text{max}}$ easily
to get a handle on the performance for larger single-particle basis sizes.
Second, in addition to the available exact solution,
this system is easy to construct and diagonalize in the basis
of the reference state and its particle-hole excitations,
\begin{equation}
    \{\refgnd, \refhp{ij}{ab}, \refhp{ijkl}{abcd} \}\,,
\end{equation}
where the odd number particle-hole excitations do not contribute
as Eq.~\eqref{eq:pairing_hamiltonian} only couples pairs.
This makes it easy to obtain an exact solution with which to compare the IMSRG(2)
and IMSRG(3) solutions.
Finally, after normal ordering the Hamiltonian with respect to our reference state,
we find that $\hnoone$ is diagonal,
meaning our reference state is the canonical Hartree-Fock reference state
with the Hartree-Fock energy $\hnozero=E_{\text{HF}}=2 - g$.
This means the IMSRG evolution must only bring in correlation corrections to the energy
without needing to overcome any reference state deficiencies.

\begin{figure}[t]
    \centering
    \includegraphics[width=0.9\textwidth]{thesis/doc/images/pairing_ham_imsrg3.pdf}
    \caption[
        The correlation energy $E_{\text{corr}}$ for the solution of the pairing Hamiltonian
        obtained via exact diagonalization,
        IMSRG(2), and IMSRG(3)
        for $-1 \le g \le 1$.
    ]{
        The correlation energy $E_{\text{corr}}$ for the solution of the pairing Hamiltonian
        obtained via exact diagonalization,
        IMSRG(2), and IMSRG(3)
        for $-1 \le g \le 1$.
        Exact diagonalization results obtained using code published with Ref.~\cite{Liet16lecnotesphysics}.
    }\label{fig:pairing_hamiltonian_fig}
\end{figure}

Thus, we are interested in the correlation energy obtained by the IMSRG(2) and IMSRG(3) solutions,
defined as
\begin{equation}
    E_{\text{corr}} \equiv E(s \rightarrow \infty) - E(s= 0)\,.
\end{equation}
This is plotted in Fig.~\ref{fig:pairing_hamiltonian_fig} for $-1 \le g \le 1$.
We find generally good agreement between the IMSRG correlation energy
and the exact correlation energy,
with the exception of the region for $0.5 \le g \le 1$.
Here, the IMSRG(3) calculation improves upon the relatively large error in the IMSRG(2)
correlation energy,
which in Ref.~\cite{Herg16imsrglecnotes}
was explained as being due to an overcounting in the fourth-order diagrams in MBPT
by a factor of 1/2 present in the IMSRG(2) truncation.
It seems that this overcounting at fourth-order is lifted in the IMSRG(3).
We note here that our results for IMSRG(2) match exactly with those from Ref.~\cite{Herg16imsrglecnotes}.


\section{Antennas}

\quickimage{Propagation/consts}{.5}

The electric permittivity is what relates the Electric Displacement Vector (D) and the Electric Field (E). For a deeper meaning one has to look into the constitutive relations as well.
Likewise, the magnetic permeability relates the Magnetic Induction Field (B) with the Magnetic Field (H).

\quickimage{Propagation/constitutivas.png}{.5}

The Electric Polarization and Magnetization vectors show how the material changes when to being subjected to a Electric/Magnetic field. This change can be displacement of charges according due to the application of the Electric field or similar in relation to the magnetic induction field. The higher the permittivity, the most charges move with the application of the field. The higher the permeability, the higher the internal magnetic field.

\section{Radio Wave Propagation}

It is absolutely pivotal some nomenclature in order to understand each other.

\quickimage{Propagation/1.png}{.6}

Mainly, everything that is bold is a vector and everything that has a bar on top is a complex amplitude. With these definitions out of the way, we can start:
\vspace{.5cm}

A wave propagating across the z direction has the following (mathematical) shape:

\begin{equation}
    e(z,t) = E_o cos(wt - kz)
\end{equation}

We can only talk about waves when the field disturbance changes in time and propagates in space, thus the dependence with a spatial coordinate and time. Note further that we start off with a very simple wave: is sinusoidal, doesn't have attenuation, only propagates in one direction and only oscillates in one direction.

To write a (slightly) more general formula for a wave, while taking advantage of the complex notation that assumes already a sinusoidal wave - note that the real part of a exponential is a cossine -, we get:

\begin{equation}
    e(z,t) = Re\{E e^{jwt}\}, E = E_o e^{-jkz}
\end{equation}
\begin{equation}
    e(r,t) = E  cos(wt - \boldsymbol{k} \cdot \boldsymbol{r})
\end{equation}

Something important to keep in mind:

\begin{center}
    \bb{In free space, electric and magnetic fields are
    mutually orthogonal and orthogonal to the
    propagation direction.}
\end{center}

Note, however, that this is not true for propagation in matter that is anisotropic or when the waves are contained in a waveguide such like a metallic waveguide where there are TE/TM modes. But we won't consider those cases, at least for now.

Also, the vector $\boldsymbol{k}  = k_x \hat{x} + k_y \hat{y} + k_z \hat{z}$ represents the direction of propagation because each of its components will contribute a phase to the oscillation of the wave.

Further note that the above wave equation is for a plane wave. Fortunately in the far-field all waves are plane waves or can be obtained from them therefore that expression will pop-up fairly often. However, most waves are spherical waves that are no more than plane waves that decay with the radius to the source, therefore the surfaces of equal amplitude are spheres.

\begin{equation}
    \frac{A}{r} e^{j(wt-kr)}
\end{equation}

The more general formula for the complex amplitude is:

\quickimage{Propagation/3.png}{.6}

The last part is important because the expression for the free space impedance $Z_0$ comes directly from the Maxwell equations and it is easier to solve them in the frequency domain (with complex amplitudes/phasors).

\quickimage{Propagation/2.png}{.5}

Resulting in the expression below, from where we can prove the previous statement of orthogonality between fields and direction of propagation.

\quickimage{Propagation/2-5.png}{.5}

Then many expressions come from the Maxwell equations when we consider losses. Because the actual Dispersion Equation is the following:

\quickimage{Propagation/4.png}{.5}

\bb{Note that the amplitudes of the field for each component} can NOT be a positive number. They can be a complex number in the sense that they will influence the polarization of the field. We'll see that in the next section!

In summary:
\quickimage{Propagation/5.png}{.5}

\subsection{Polarization}

\bb{The polarization is the shape the field oscillation describes while looking at the wave from propagation direction with the wave going away from us}. An easier way of finding if it is rotating to the left or to the right, we can use the right-hand, with the thumb along the propagation. If it is rotating along the way our (right-)hand closes, then it is a right circular polarisation.

The reference for horizontal polarisation is the earth.

This derivation of \href{http://kestrel.nmt.edu/~mce/Polarization}{\ul{The general Elliptic polarization}} shows why plane waves propagating freely can have its polarization described generally by an ellipse. Also shows that certain specific parameters of that ellipse can lead to certain more familiar polarizations, such as linear and circular.

\quickimage{Propagation/pol1.png}{.5}
%explain this

\subsection{Reflection}

All reflections' chapter is according to Snell Laws.

A few bullet points to take away from the first class on reflection are:
\begin{itemize}
    \item a dry soil behaves as a metal for a very small angle with the ground, typical in very long distances. ``Behaving as a metal'' means that the reflection coefficient will be -1, for either polarisation;
    \item Brewster's Angle is of incidence for which the parallel component of the field is passes completely to the other side of the surface. The reflected wave does a $90^{\circ}$ angle with the refracted wave and only has H field. Note that this only happens for parallel polarisation. In perpendicular is the H field that will be suppressed on reflection. The perpendicular component is never fully absorbed.
    \item Horizontal polarisation always has bigger reflection coefficients therefore is worst to use because it will cause more variability on the arrival. Other word for incidences with very short angles with the ground is "grazing incidences".
    \item Dependences with frequency increase with dielectric conductivity. Else the losses will simply be too small.
\end{itemize}

One thing that needs to be introduced are the Fresnel coefficients:
\quickimage{Propagation/6.png}{.5}

Note further that the last bullet point mentioned the tangent of the loss angle.

\begin{equation}
    \tan \delta = \frac{\sigma}{\omega \epsilon}
\end{equation}

Note that if the conductivity is too small, the whole expression will be controlled by that and the frequencies would have to be very small as well to make a difference. Since we don't use those frequencies in Radio Propagation, we may say that only for a sufficiently high conductivity, the frequency dependence increases.

\subsection{Spherical Earth}

Surprise: the Earth is not flat, it's spherical. And this curvature needs to be accounted specially because even the direct ray may be influenced by this curvature.


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands
% Prepared by XXX and YYY under the supervision of Arnaud de La Fortelle
% Fall 2017
% 2D wave propagation subsection of the modeling part
% ***********************************************************************************

\paragraph{Control model}

We consider here the simplest model for which a Kalman filter can really bring an added value: a linear, Gaussian and real (i.e. 1D) model:
\begin{eqnarray}
\label{kalman-dynamics.eq1}
x_{k+1} &=& A x_k + B u_k + v_k\\
\label{kalman-dynamics.eq2}
y_k &=& C x_k + w_k
\end{eqnarray}
where $X=(x_k)$ is the state of the system (typically a distance to a prescribed value, in whatever unit), $U=(u_k)$ is the control, $Y=(y_k)$ is the measurement. $V=(v_k)$ (resp. $W=(w_k)$) is a Gaussian noise: the $v_k$ (resp. $w_k$)are independent and identically distributed random variables with 0 mean and variance $Q$ (resp. $R$). The initial state $x_0$ is also a centered random variable with variance $\bar{P}_O$. Note that these notation perfectly adapt to multidimensional systems, with matrices: see any textbook on control to get the matrix equations.

This is not specifically needed for Kalman filter, but the usual optimization criteria is quadratic:
\begin{equation}\label{kalman-cost.eq1}
	J(X,U) = \frac{1}{N}\sum_{k=0}^N \alpha u_k^2 + \beta x_k^2
\end{equation}

The Equations~(\ref{kalman-dynamics.eq1})-(\ref{kalman-dynamics.eq2}) are often written for each time step so the scaling with respect to time has to be careful. In a system with a 100 Hz time step (also refered to as control frequency), typical values are:
\begin{eqnarray*}
A &=& 1.001\\
B &=& .01\\
C &=& 1\\
Q &=& .001\\
R &=& .01
\end{eqnarray*}
We have taken $C=1$ because most of the time we try to have a measurement as close to the state as possible and if $C\neq 1$ it is possible to compensate it. $A>1$ means that the system is unstable: Figure~\ref{Kalman-free.fig} shows the divergent behavior.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Kalman-free}
	\caption{The free system dynamics: it shows instability and the system tends to diverge exponentially. The variance $\sigma_R^2$ (resp. $\sigma_Q^2$ ) is denoted by $R$ (resp. $Q$) in the system model.}
	\label{Kalman-free.fig}
\end{figure}

\paragraph{A naive control}
Now, we would like to stabilize the state of the system as close to zero as possible, and if possible at a reasonable cost. This means we can adjust the weights $\alpha$ and $\beta$ in the cost function~(\ref{kalman-cost.eq1}). Or simpler measure the 2 partial cost functions
\begin{eqnarray}
\label{kalman-costX.eq}
J_1(X) &=& \frac{1}{N}\sum_{k=0}^N  x_k^2\\
\label{kalman-costU.eq}
J_2(U) &=& \frac{1}{N}\sum_{k=0}^N u_k^2
\end{eqnarray}

Now, we introduce the simplest possible control: a proportional control with gain $K$ defined by:
\begin{equation}
\label{kalman-Pcontrol.eq}
	u_k = -K y_k
\end{equation}

Since we have "unit" parameters, let's start with a unit gain $K=1$. This indeed stabilizes the system as we see in Figure~\ref{Kalman-control-Gain1.fig}.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Kalman-control-Gain1}
	\caption{The same system as in Figure~\ref{Kalman-free.fig} with the proportional control as defined by Equation~(\ref{kalman-Pcontrol.eq}) with gain $K=1$. The system is indeed stable with empirical costs $J_1=0.06$ and  $J_2=0.07$.}
	\label{Kalman-control-Gain1.fig}
\end{figure}

Now, if we try to maximize the precision (at all cost), we can try to compensate the term $A x_k$ in Equation~(\ref{kalman-dynamics.eq1}) by the term $B u_k = -BK Y_k = -(BKC) x_k -BK w_k$. Since we have no control onto the noise, and since this noise is centered, let's take $A = BKC$, i.e. in our case $K = A/BC = 100.1$. This lead to the result of Figure~\ref{Kalman-control-Gain100.fig}. What is impressive is not so much the increase in precision, $J_1$ is divided by about 5, that the explosion of the control cost $J_2$, multiplied by about 3,000.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Kalman-control-Gain100}
	\caption{The same system as in Figure~\ref{Kalman-control-Gain1.fig} with $K=100.1$ in order to fully compensate $A$. The system is very stable with empirical costs $J_1=0.011$ and  $J_2=209$.}
	\label{Kalman-control-Gain100.fig}
\end{figure}

In order to learn more about the trade-off between precision and control cost, one can test intermediary gains, and for $K=10$, we get the Figure~\ref{Kalman-control-Gain10.fig}. There we have a surprise: the precision is better than with our naively optimal gain $K=100.1$, not to speak of the decrease of the control cost. There are now 2 paths for us: either --- pragmatically --- adjust the gain to get the best control, or --- analytically --- try to understand what was wrong. Indeed, this numerical experiment clearly shows we are missing an important idea. And the idea we are missing is the following: we can have much better estimates of $x_k$ than $y_k$ (even after compensating some multiples in the $C$ coefficient. What we do with the heavy gain $K=100.1$ is simply to add noise. And if we go even further (e.g. $K=200$), the system becomes unstable.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Kalman-control-Gain10}
	\caption{The same system as in Figure~\ref{Kalman-control-Gain1.fig} with $K=10$. The empirical costs are $J_1=5.8\ 10^{-3}$ and  $J_2=1.59$.}
	\label{Kalman-control-Gain10.fig}
\end{figure}

\paragraph{The Kalman filter}
Now, we introduce the equations of the Kalman filter, which we know are optimal to estimate $x_k$.
\begin{eqnarray}
\label{kalman-filter.eq1}
\hat{x}_0 &=& 0\\
\label{kalman-filter.eq2}
P_0 &=& \bar{P}_0\\
\label{kalman-filter.eq3}
\hat{x}^-_{k+1} &=& A \hat{x}_k + B u_k\\
\label{kalman-filter.eq4}
P^-_{k+1} &=& A P_k A + Q\\
\label{kalman-filter.eq5}
\hat{x}_{k+1} &=& \hat{x}^-_{k+1} + L_{k+1}\left(y_{k+1}-C\hat{x}^-_{k+1}\right)\\
\label{kalman-filter.eq6}
L_{k+1} &=& P^-_{k+1}C\left(CP^-_{k+1}C+R\right)^{-1}\\
\label{kalman-filter.eq7}
P_{k+1} &=& \left(1-L_{k+1}C\right)P^-_{k+1}
\end{eqnarray}
These equations can be found in any textbook on control and are almost true as is for multidimensional states and measures. The most striking here is that the filter works whatever the control.

There is not much difference with Equation~(\ref{kalman-Pcontrol.eq}) in our control model, except that we take the best estimate:
\begin{equation}
\label{kalman-Pcontrol.eq2}
	u_k = -K \hat{x}_k
\end{equation}

This leads to the result of Figure~\ref{Kalman-TrueControl-Gain100.fig}. The precision is better than our previous best control (that was obtained for a lower gain) and the control cost is much lower than the control with same gain but based on Equation~(\ref{kalman-Pcontrol.eq}). Empirically, one sees that this gain is the optimal value in term of precision. As we are curious, we also vary the gain and we get the following results: for $K=10$, $J_1=8\ 10^{-3}$ and  $J_2=0.53$; for $K=1$, $J_1=0.072$ and  $J_2=0.069$. So we see that for lower gains (and this is much closer to realistic values), the precision is not much improved and the control cost improvement is noticeable mainly for high gains. Again, this certainly means we are missing some concepts. And here what we are missing is the notion of a better feedback control: the proportional control is too simple to be optimal. We could try a PID (Proportional-Integral-Derivative) or other controls (e.g. with pole placement), but in any case we learned that a Kalman filter allow to reduce the noise in the estimation of the state $x_k$.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Kalman-TrueControl-Gain100}
	\caption{The system with a proportional control based on Kalman filter with $K=100.1$. The empirical costs are $J_1=3.77\ 10^{-3}$ and  $J_2=10.2$.}
	\label{Kalman-TrueControl-Gain100.fig}
\end{figure}

In conclusion, note that what we learned practically about estimates, noise reduction and control are very general concepts that can be adapted to many situations, including PDEs. However, it generally requires some know-how to apply these notions.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 6}

Give a counter-example to the conjecture that if a directed graph $G$ contains a path from $u$ to $v$, and if $u.d < v.d$ in a depth-first search of $G$, then $v$ is a descendant of $u$ in the depth-first forest produced.

\subsection*{Solution}

Any directed graph $G$ with a path from $u$ to $v$ through the source vertex $s$ would be considered as counter-example for the proposed statement.
Figure \ref{fig61} depicts graph $G$ with source vertex $a$ as one counter-example following required conditions.

\begin{figure}[H]\centering
\tikzstyle{vertex}=[circle,draw,minimum size=0.7cm]
  \begin{tikzpicture}
    \node[vertex] (1) {a};
    \node[vertex] (2) [below left = 0.5cm and 1.5cm of 1] {b};
    \node[vertex] (3) [below right = 0.5cm and 1.5cm of 1] {c};
    \path[draw,thick,->]
	(1) edge [bend left=30] (2)
	(1) edge (3)
    (2) edge [bend left=30] (1)
	;
    \path[draw,ultra thick,->]
    ;
  \end{tikzpicture}
\caption{Example that if $u.d < v.d$ and $u$ has path to $v$,  graph $G$}\label{fig61}
\end{figure}

Starting from $a$, node $a$ is visited first, and vertices $b$ and $c$ are discovered and placed in queue.
In next step, if $b$ is the first node to be visited, the depth-first search algorithm will follow by discovering children of $b$.
Since $b$ has only one child that is already visited, the depth-first search algorithm continues by visiting vertex $c$.
In this case, although there is a path from $b$ to $c$ and $b.d < c.d$, $c$ is not a descendant of $b$.


\subsection*{Querying}
\paragraph{Optimization:} $\forall x. T(\phi)(x) = 0 \Leftrightarrow x \text{ satisfies } \phi$

\centering
\begin{tabular}{ll}
$\phi$ & $T(\phi)$ (non-negative) \\\hline
$t_1 \le t_2$ & $\max(0, t_1-t_2)$ \\
$t_1 \neq t_2$ & $[t_1 = t_2]$ \\
$t_1 = t_2$ & $T(t_1 \le t_2 \land t_2 \le t_1$ \\
$t_1 < t_2$ & $T(t_1 \le t_2 \land t_1 \neq t_2)$ \\
$\phi \lor \psi$ & $T(\phi) \cdot T(\psi)$ \\
$\phi \land \psi$ & $T(\phi) + T(\psi)$
\end{tabular}

\raggedright
\paragraph{Box} Box constraints are encoded separately (e.g., w/ L-BFGS-B optimizer)

\paragraph{Counter-example} Use optimization to find counter-examples to a given property.

\subsection*{Training with Background Knowledge}
\begin{enumerate}
\item $\displaystyle \argmax_\theta \E_{s \sim D} \left[ \forall z. \phi(z, s, \theta) \right]$ (find $\theta$ s.t. exp. val. of property increases)

\item $\displaystyle \argmin_\theta \E_{s \sim D} \left[ \max_z \lnot \phi(z, s, \theta) \right]$ (find $\theta$ s.t. max. violation of $\phi$ is minimized)

\item $\displaystyle \argmin_\theta \E_{s \sim D} \left[ T(\phi)(z_\text{worst}, s, \theta) \right]$,
$\displaystyle z_\text{worst} := \argmin_z T(\lnot \phi)(z, s, \theta)$ (find worst possible violation of $\phi$, then find $\theta$ that minimizes its effect)
\end{enumerate}

To solve inner opt. problem efficiently, split into objective and (efficient) projections on convex set (e.g., $L_\infty$-ball)


\chapter{Linear function}
\section{Defined on $\mdr$}
\begin{theorem}[Solution formula for linear functions on $\mdr$]
    Let $f: \mdr \rightarrow \mdr $ be a linear function
    $f(x) := m \cdot x + t$ with $m \in \mdr \setminus \Set{0}$ and
    $t \in \mdr$ be a linear function.

    Then there is only one point $(x, f(x))$ on the graph of $f$ with
    minimal distance to $P = (x_P, y_P)$. This point is given by
    \[x = \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )\]
\end{theorem}

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north east,
            legend cell align=left,
            axis x line=middle,
            axis y line=middle,
            grid = major,
            width=0.8\linewidth,
            height=8cm,
            grid style={dashed, gray!30},
            xmin= 0, % start the diagram at this x-coordinate
            xmax= 5, % end   the diagram at this x-coordinate
            ymin= 0, % start the diagram at this y-coordinate
            ymax= 3, % end   the diagram at this y-coordinate
            axis background/.style={fill=white},
            xlabel=$x$,
            ylabel=$y$,
            tick align=outside,
            minor tick num=-3,
            enlargelimits=true,
            tension=0.08]
          \addplot[domain=-5:5, thick,samples=50, red] {0.5*x};
          \addplot[domain=-5:5, thick,samples=50, blue, dashed] {-2*x+6};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
          \newcommand{\R}{0.9}
          \addplot [domain=0:2*pi,samples=50, dotted]({\R*cos(deg(x))+2},{\R*sin(deg(x))+2});
          \addplot[blue, nodes near coords=$f_\bot$,every node near coord/.style={anchor=225}] coordinates {(1.5, 3)};
          \addplot[red, nodes near coords=$f$,every node near coord/.style={anchor=225}] coordinates {(0.9, 0.5)};
          \addlegendentry{$f(x)=\frac{1}{2}x$}
          \addlegendentry{$f_\bot(x)=-2x+6$}
        \end{axis}
    \end{tikzpicture}
    \caption{The shortest distance of $P$ to $f$ can be calculated by using the perpendicular}
    \label{fig:linear-min-distance}
\end{figure}

\begin{proof}
    With Theorem~\ref{thm:fermats-theorem} you get:
    \begin{align}
        0 &\stackrel{!}{=} (d_{P,f}(x)^2)'\\
        &= 2(x-x_P) + 2 (f(x) - y_P)f'(x)\\
        \Leftrightarrow 0 &\stackrel{!}{=} x - x_P + (f(x) - y_P) f'(x)\\
        &= x- x_P + (mx+t - y_P)\cdot m\\
        &= x (m+1) + m(t-y_P) - x_P\\
        \Leftrightarrow x &\stackrel{!}{=} \frac{x_p - m(t-y_p)}{m^2+1}\\
        &= \frac{m}{m^2+1} \left ( y_P + \frac{1}{m} \cdot x_P - t \right )\label{eq:solution-linear-r}
    \end{align}
    It is obvious that a minium has to exist, the $x$ from Equation~\ref{eq:solution-linear-r}
    has to be this minimum. $\qed$
\end{proof}
\clearpage

\section{Defined on a closed interval $[a,b] \subseteq \mdr$}
Let $f:[a,b] \rightarrow \mdr$, $f(x) := m\cdot x + t$ with $a,b,m,t \in \mdr$ and
$a \leq b$, $m \neq 0$  be a linear function.

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            legend pos=north east,
            legend cell align=left,
            axis x line=middle,
            axis y line=middle,
            grid = major,
            width=0.8\linewidth,
            height=8cm,
            grid style={dashed, gray!30},
            xmin= 0, % start the diagram at this x-coordinate
            xmax= 5, % end   the diagram at this x-coordinate
            ymin= 0, % start the diagram at this y-coordinate
            ymax= 3, % end   the diagram at this y-coordinate
            axis background/.style={fill=white},
            xlabel=$x$,
            ylabel=$y$,
            tick align=outside,
            minor tick num=-3,
            enlargelimits=true,
            tension=0.08]
          \addplot[domain= 2:3, thick,samples=50, red] {0.5*x};
          \addplot[domain=-5:5, thick,samples=50, blue, dashed] {-2*x+6};
          \addplot[domain=1:1.5, thick, samples=50, orange] {3*x-3};
          \addplot[domain=4:5, thick, samples=50, green] {-x+5};
          \addplot[black, mark = *, nodes near coords=$P$,every node near coord/.style={anchor=225}] coordinates {(2, 2)};
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:1.5,1.5);
          \draw[thick, dashed] (axis cs:2,2) -- (axis cs:4,1);
          \addlegendentry{$f(x)=\frac{1}{2}x, D = [2,3]$}
          \addlegendentry{$f_\bot(x)=-2x+6, D=[-5,5]$}
          \addlegendentry{$h(x)=3x-3, D=[1,1.5]$}
          \addlegendentry{$h(x)=-x+5, D=[4,5]$}
        \end{axis}
    \end{tikzpicture}
    \caption{Different situations when you have linear functions which
             are defined on a closed intervall}
    \label{fig:linear-min-distance-closed-intervall}
\end{figure}

The point with minimum distance can be found by:
\[\underset{x\in[a,b]}{\arg \min d_{P,f}(x)} = \begin{cases}
 S_1(f, P) &\text{if } S_1(f, P) \cap [a,b] \neq \emptyset\\
   \Set{a} &\text{if } S_1(f, P) \ni x < a\\
   \Set{b} &\text{if } S_1(f, P) \ni x > b
    \end{cases}\]

If $S_1(f, P) \cap [a,b] \neq \emptyset$, then $\underset{x\in[a,b]}{\arg \min d_{P,f}(x)} = S_1(f,P) \cap [a,b]$,
because $S_1(f,P)$ gives all global minima of $f$. Those are also
minima for the intervall $[a,b]$. There are not more minima, because
$S_1$ gives all minima of $P$ to $f$.

If $S_1(f, P) \cap [a,b] = \emptyset$, then it is not that simple.
But we can calculate the distance function:

\begin{align}
    d_{P,f}(x) &= \sqrt{(x-x_P)^2 + (f(x) - y_P)^2}\\
    &= \sqrt{(x^2 - 2x x_P + x_P^2) + (mx + (t-y_P))^2}\\
    &= \sqrt{(x^2 - 2x x_P + x_P^2) + m^2 x^2 + 2mx(t-y_P) + (t-y_P)^2}\\
    &= \sqrt{x^2(1+m^2) + x(-2 x_P + 2m(t-y_P)) + (x_P^2 + (t-y_P)^2)}
\end{align}

This function (defined on $\mdr$) is symmetry to the axis
\begin{align}
    x_S &= - \frac{-2 x_P + 2m(t-y_P)}{2(1+m^2)}\\
    &= \frac{x_P - m(t-y_P)}{1+m^2}\\
    &= \frac{m}{m^2+1} (y_P + \frac{1}{m} x_P - t)
\end{align}

$f$ is on $(-\infty, x_S]$ strictly monotonically decreasing and
on $[x_S, + \infty)$ strictly monotonically increasing.

Thus we can conclude:
\[\forall x,y \in \mdr: x \leq y < x_S \Rightarrow d_{P,f}(x_S) < d_{P,f}(y) \leq d_{P,f}(x)\]
\[\forall x,y \in \mdr: x_S < y \leq x \Rightarrow d_{P,f}(x_S) < d_{P,f}(y) \leq d_{P,f}(x)\]

When $S_1(f, P) \cap [a,b] = \emptyset$, then you can have two cases:
\begin{itemize}
    \item $a \leq b < x_S$: $b$ has the shortest distance in $[a,b]$
             on the graph of $f$ to $P$.
    \item $x_S < a \leq b$: $a$ has the shortest distance in $[a,b]$
             on the graph of $f$ to $P$.
\end{itemize}


\section{The Kalman Gain}

The Kalman Gain, $\boldsymbol{K}$, is the heart of the kalman filter's \textit{Update} step. It is a matrix that allows uncertainties and covariances to influence the evolution of the state, and it depends on the innovation covariance $\boldsymbol{S}$.
\begin{equation}
    \boldsymbol{S} = \boldsymbol{H} \cdot \boldsymbol{P} \cdot \boldsymbol{H}^T + \boldsymbol{R}
\end{equation}

The inverse of this innovation covariance is directly used to compute the Kalman Gain.

\begin{equation}
    \boldsymbol{K} = \boldsymbol{P} \cdot \boldsymbol{H}^T \cdot \boldsymbol{S}^{-1}
\end{equation}

\begin{quote}
    Note: This equation should work, but we noticed the EKF was blowing up to infinity because of weird divergences in the off-diagonal elements of the Kalman Gain. As such, we made a last-minute hack by multiplying this $\boldsymbol{K}$ component-wise by $\boldsymbol{H}^T$. This has the effect of forcing all off-diagonals to zero, which is definitely bad but fixed the problems in our time of need. Please do something better than this in the future.
\end{quote}

Now that we have $\boldsymbol{K}$, we can update the state and the covariance, using our innovation and the predictions for $\boldsymbol{x}$ and $\boldsymbol{P}$ made in the previous timestep.

\begin{equation}
    \boldsymbol{\hat{x}}_{k,k} = \boldsymbol{\hat{x}}_{k,k-1} + \boldsymbol{K} \cdot \boldsymbol{y}_k
\end{equation}

\begin{equation}
    \boldsymbol{P}_{k,k} = (\boldsymbol{I}_8 - \boldsymbol{K} \cdot \boldsymbol{H}) \cdot \boldsymbol{P}_{k,k-1}
    \label{eq:cov_update}
\end{equation}
where $\boldsymbol{I}_8$ is the 8x8 identity matrix.

Note: Equation (\ref{eq:cov_update}) is a simplified version of the equation which mostly works, but is unstable, and will multiply significant errors which could ruin the whole filter. It works mostly fine, but for reference the more correct equation is included below.
\begin{equation}
    \boldsymbol{P}_{k,k} = (\boldsymbol{I}_8 - \boldsymbol{K} \cdot \boldsymbol{H}) \cdot \boldsymbol{P}_{k,k-1}
    \cdot (\boldsymbol{I}_8 - \boldsymbol{K} \cdot \boldsymbol{H})^T +
    \boldsymbol{K} \cdot \boldsymbol{R} \cdot \boldsymbol{K}^T
\end{equation}


\documentclass[12pt]{scrartcl}

\input{preamble}

\makeatletter
\title{Hack 2.0}\let\Title\@title
\subtitle{Computer Science I -- Honors\\
{\small
\vskip1cm
Department of Computer Science \& Engineering \\
University of Nebraska--Lincoln}
\vskip-1cm}
%\author{Dr.\ Chris Bourke}
\date{~}
\makeatother

\begin{document}

\maketitle

\hrule

\input{instructions.tex}

\section*{Problem Statement}

Consider two locations, an origin and a destination, on the globe
identified by their latitude and longitude.  The distance between
these two locations can be computed using the Spherical Law of
Cosines.  In particular, the distance $d$ is
 $$d = \arccos{(\sin(\varphi_1) \sin(\varphi_2) + \cos(\varphi_1) \cos(\varphi_2) \cos(\Delta) )} \cdot R$$
where
\begin{itemize}
  \item $\varphi_1$ is the latitude of location $A$, $\varphi_2$ is the latitude of location $B$
  \item $\Delta$ is the difference between location $B$'s longitude and location $A$'s longitude
  \item $R$ is the (average) radius of the earth, 6,371 kilometers
\end{itemize}

Write a program that \emph{prompts} the user to enter the latitude
and longitude of two locations and then computes the distance between them using
the above formula.  Note that latitude inputs will be in degrees and in the
range $[-90, 90]$ and longitude will be in degrees in the range $[-180, 180]$.
Negative values correspond to the western and southern hemispheres.

Note that the formula above assumes that latitude and longitude are
measured in radians $r$, $-\pi \leq r \leq \pi$.  You can convert from
degrees $deg$ to radians $r$ using the formula
  $$r = \frac{deg}{180} \cdot \pi$$

Your output should look something like the following.

\begin{minted}{text}
Location Distance
========================
Origin:      (41.948300, -87.655600)
Destination: (40.820600, -96.705600)
Air distance is 764.990931 kms
\end{minted}

\section*{Instructions}

\begin{itemize}
  \item You are encouraged to collaborate any number of students
  before, during, and after your scheduled hack session.
  \item Design at least 3 test cases \emph{before} you begin
  designing or implementing your program.  Test cases are
  input-output pairs that are known to be correct using means
  other than your program.
  \item Include the name(s) of everyone who worked together on
  this activity in your source file's header.
  \item Place your code in a \mintinline{java}{main()} method in
    a source file named \mintinline{text}{AirDistance.java} and
    turn it in via webhandin.  Make sure that it runs and executes
    correctly in the webgrader. Each individual
    student will need to hand in their own copy and will receive their
    own individual grade.
  \item Remember to RTM (Read The Manual) on Java's
    \mintinline{java}{Math} class/library to see which method(s)
    you may find useful and how to use them.
\end{itemize}

\end{document}


\section{Clustering}
\label{sec:ctm-clustering}

The next idea we attempted was using clustering algorithms to group student solutions together. The goal was to put student solutions with similar edit distances, to some subset of reference solutions, in the same cluster.

Under this mindset, we treated each student as an independent \textquote{data point} and their edit distance to each reference solution as an independent \textquote{feature}. Our data thus became an $N \times M$ matrix with $N$ rows for each student and $M$ columns for each reference solution.

We made the assumption that full-mark solutions should have very similar features and thus should be very close to each other in the same cluster. Recall that the majority of the historical student solutions in this course had received full marks. Therefore, the majority of each cluster's points should also be very close to each other as well. As a result, we believed each cluster should theoretically be \textquote{centered} closer to the full-mark solutions than the incorrect solutions; therefore we hypothesized the closer a student solution was to the cluster's center, the higher the probability that the student solution should receive full marks.

%------------------------------------------------------------------------------
\subsubsection{Determining Number of Clusters}
%------------------------------------------------------------------------------

The K-Means and Gaussian Mixture clustering algorithms require us to specify how many clusters we want to find in our data points (student solutions). There are no straightforward approaches to choosing this value because it depends on the input data and use-case.

If we choose too few clusters, then we risk putting too many student solutions in the same cluster despite them not being too closely related. If we choose too many clusters, then we risk not getting sufficient data to compute scores. For example, in the extreme case of $N$ clusters, every student would become its own cluster and, according to our original hypothesis of final score being based on closeness to a cluster's center, should receive full marks.

However, there are techniques such as the \textquote{Elbow Method} \cite{thorndike1953belongs} shown in Figure~\ref{fig:ctm-num-clusters} that can be used for guidance. For our data, the Elbow Method recommended for us to use 4 clusters.

\begin{figure}
\includegraphics[width=\textwidth]{elbow}
\caption[Using Elbow Method to find Optimal Number of Clusters]{The Elbow Method runs the K-Means clustering algorithm with a range of clusters and computes the sum of squared errors. This sum measures how close the predicted clusters match the data points; the greater the error, the less clusters fit the data. This graph converges to 0 at $N$ clusters where each point is its own cluster and thus has zero error. To find an appropriate number of clusters, we need to visually find an \textquote{elbow} point on the graph, i.e. where adding an additional cluster will not significantly reduce the error. In this graph, the elbow point is at 4 clusters.}
\label{fig:ctm-num-clusters}
\end{figure}

%------------------------------------------------------------------------------
\subsubsection{Cleaning Up Data}
%------------------------------------------------------------------------------

Although not essential, we chose to perform Principal Component Analysis (PCA) \cite{wold1987principal} on our data prior to clustering. PCA transforms our set of $M$ features into a smaller set of linearly uncorrelated features or \textquote{components}. The components are sorted in descending order of variance. In other words, the first few components theoretically capture the majority of the \textquote{information} in the source data.

The most obvious advantage of PCA is reducing the runtime of our clustering algorithms because it eliminates features (columns in our data matrix) that represent very little information about our data points. This technique is also useful in visualization as it allows high-dimensional data to be presented in a 2D graph while preserving the majority of the information and relationships between data points.

To use PCA, we need to specify how many components or features to keep. Similar to the Elbow Method to determine the number of clusters, we can look at the graph of cumulative explained variance shown in Figure~\ref{fig:ctm-num-components} to estimate an appropriate number of components. For our data, the graph recommended for us to use 6 components.

\begin{figure}
\includegraphics[width=\textwidth]{explained_variance_ratio}
\caption[Explained Variance Ratio]{The explained variance conceptually represents the amount of information in the original data that each component captures. The first component captures almost 80\% of the original information; the second component captures another 5\%; and so on. This graph show the cumulative explained variance that each additional component captures. A rule-of-thumb for PCA is to choose the number of components at an \textquote{elbow point} where adding an additional component will not capture significantly more information. In this graph, the elbow point is at 6 components.}
\label{fig:ctm-num-components}
\end{figure}

\input{body/conversion-to-mark/k-means}
\input{body/conversion-to-mark/gaussian-mixture}
\input{body/conversion-to-mark/hdbscan}