\emph{Mathematics} underpins proof systems such as sagaproofs because it offers a well established framework for rigorous analysis necessary to justify security.
Here we are referring to the use of mathematics in the \emph{construction} of proof systems, but other types of mathematics are used in the \emph{analysis} of proof systems.
Here we discuss the motivations behind the use of finite algebraic structures (in particular rings and fields) throughout sagaproofs and other proof systems.
We also introduce the important algebraic concepts of quotients and extensions.
This discussion is informal and formal definitions are clarified elsewhere when appropriate.

\section{Restricting to finite structures}

While mathematical structures are many and varied, only those suited for constructing proof systems are of interest for our purposes.
What exactly constitutes suitability for proof systems is not yet fully qualified, but a minimal requirement is efficient representation and manipulation on a computer.
Infinite structures can be challenging to handle in this sense, especially when enforcing consistency across computers, which is crucial in the case of proof systems involving many parties.
Therefore, narrowing focus to finite structures seems justifiable.

By many metrics, the majority of mathematical research targets infinite structures, so narrowing focus to finite structures excludes the majority of possibilities.
This may be seen as both a boon and a bane in that while limiting opportunity it also focuses attention.
The rich class of structures just beyond finite structures is finitely \emph{generated} structures, which cannot be entirely ruled out, and as such the choice of finite structures is not fully justified.
Nevertheless, finite structures are the structures of choice for now.

\section{Choice of finite structures}

Restricting to finite structures still leaves an infinite number of possibilities.
We must further restrict attention to those structures in which we see opportunity for proof systems.
In fact, we must restrict even further to those that seem appropriate for \emph{practical} proof systems.

Diverging for a moment, the term `practical' for qualifying proof systems, or more generally any solution to a technical problem, can be ambiguous.
Some solutions are capable of implementation in the near future, say the next decade.
These solutions are largely dependent on current technologies and their near-term trajectories.
Other solutions are only capable of implementation some time in the distant future.
These solutions are largely insensitive to the current state of technology.
Throughout this project we refer to the former types of solutions as `practical' and the latter types as `impractical'.
In both cases, academic literature may refer to a solution as `efficient' because it requires bounded resources as a function of the problem size.
Specifically, bounds are taken to be polynomial functions in order that efficient solutions may be combined in various ways while remaining efficient.
Do not confuse `efficient' with `practical' as many solutions are efficient but impractical.
A solution may be efficient since it meets certain resource bounds, and yet impractical since the resources demanded are more than current technology can provide.

\begin{remark}
    While there is some fundamental difference between efficient and inefficient solutions, there is no fundamental difference between practical and impractical solutions.
    The practicality of a solution is dependent on the capability of modern technology, which in turn is dependent on the ever-changing scale and sophistication of human civilization.
    The efficiency of a solution, on the other hand, is fixed and independent of human civilization.
    There is no reason the efficiency and practicality of solutions should exhibit any correlation.

    On a spectrum from the Plank length to the diameter of the observable universe, the size of human civilization sits in a rather arbitrary position roughly in the middle.
    Given our relative size, we are lucky to control enough atoms in this universe to implement many efficient solutions, and yet our limited control leaves so many efficient solutions out of practical reach.
\end{remark}

In our context the solutions we seek are proof systems, and the technologies of concern are computing and networking technologies.
In particular, we are concerned with the affordable speed and volume of computer calculations and network signals.
These metrics are what separate the practical from the impractical.

Academic literature on proof systems can be roughly split into the `theoretical' type and the `applied' type.
Theoretical research is motivated by efficient solutions, which may be impractical.
Applied research is motivated by practical solutions, which are always efficient.
Since theoretical research pursues solutions independent of current technology, any mathematical structure amenable for proof systems suffices.
Since applied research pursues solutions highly dependent on current technology, choices of mathematical structures are more severely limited.

A set of finite structures of popular use in theoretical research but of rare use in applied research is graphs, that is the set of structures each consisting of a finite number of objects and the connections between them.
For example, expander graphs played a central role in \cite{Din07} to prove the PCP Theorem, likely the most important result in theoretical proof systems.
Graphs are extensively studied, including the expander graphs that may be of use for our purposes.
The use of graphs in proof systems, however, seems to require resources beyond the capacity of modern technology.

On the other hand, a set of finite structures of popular use in both theoretical and applied research is finite fields, that is a certain set of highly symmetric algebraic structures.
Finite fields are extensively studied and possess many homomorphic properties desirable for our purposes.
Furthermore, many uses of finite fields in proof systems require resources within the capacity of modern computing and networking technologies.

Much of our focus in this project is devoted to finite fields.
Finite fields are the finite objects of study in `field theory' which itself is embedded in `ring theory.'
Our choice of finite structures is finite rings, and much of the time we restrict attention to finite fields.
If this choice of finite structures seems arbitrary, see the section below regarding arithmetic.
Ring theory may be viewed as the natural generalization of arithmetic, and arithmetic is anything but arbitrary.

\section{Representing structures}

Our mathematical structures of choice, rings and in particular finite fields, belong to a larger class of mathematical structures called \emph{algebraic} structures.
An algebraic structure consists of a set of objects, called \emph{elements}, and relationships between those elements.
A defining characteristic of algebraic structures is that relationships between elements are described by the elements themselves.
This is in contrast to other types of structures like graphs, where the connections between objects (i.e. edges between vertices) are not described by objects but by truth values.
The circular nature of algebraic relationships may be what gives them so much richness that has attracted so much attention for study.

In an algebraic structure, the most basic relationship is the familiar notion of equality, and every element is considered equal to itself.
Equality is a unary relationship as it pertains to every element individually.
Unary relationships can be represented as unary functions between the elements.
In the case of equality the unary function would be the identity function, that which sends every element to itself.

One step beyond unary relationships are binary relationships.
A binary relationship applies to every pair of elements, describing the relationship of each pair by a third element.
Binary relationships can be represented as binary functions between the elements.
Beyond binary relationships are ternary relationships applying to every triple of elements, and one may continue further with relationships of every higher arity.
It turns out, however, that most study does not go beyond binary relationships.
This is likely because binary relationships capture enough complexity for most purposes of study, and relationships of higher arity either yield no additional complexity or yield so much complexity only special cases can be meaningfully studied.

Since relationships are represented as functions from elements to elements, they are called \emph{operations} in that they operate on inputs elements to produce an output element.
Formally, an algebraic structure consists of a set of elements together with one or more operations defined on those elements.
Informally, however, an algebraic structure is often identified only by the set of elements and the operations are left to be determined from context.
Henceforth in our study of structures, all structures are algebraic structures, represented by sets and operations that can be gleaned from context when not specified.

A ring is an algebraic structure with addition and multiplication operations, from which one may derive others such as subtraction and exponentiation.
A finite field a ring that also allows for a division operation.

\section{Building structures}

Complex algebraic structures can be built from simpler structures by a sequence of steps through intermediate structures.
There may exist multiple paths from one structure to another, and the steps in any path may not be strictly ordered.
A step from one structure to another is a transformation from one to another.
There are three different kinds of transformations.

\begin{itemize}

    \item{Derived definition}
    Derived definitions are different in nature than the quotients and extensions described below.
    Given a structure, we may define a new set of elements in terms of existing elements, and new operations in terms of existing operations.
    An example is matrix algebra where new elements are blocks of existing elements, and new operations are formulated in terms of existing addition and multiplication operations.

    \item{Quotient}
    A \emph{quotient} is best thought of as a contraction, the dual of an extension (see below).
    The name `contraction' would be most fitting, but we follow the standard name `quotient.'
    Given a structure, we may declare two elements to be equal.
    Doing so yields a new structure with the same operations and fewer elements, thus a contraction of the original structure.

    Such a transformation can naturally be expressed by an equation involving any existing elements.
    When the equation holds in the original structure the transformation is trivial and no contraction occurs.
    When the equation does not hold in the original structure, the transformation is non-trivial and any amount of contraction may occur.

    An example is taking integers with addition and multiplication and declaring the equation $2=0$.
    This yields a structure with two elements identifiable as the two truth values.
    Addition now corresponds to exclusive disjunction (XOR) and multiplication to conjunction (AND).

    \item{Extension}
    An \emph{extension} is best thought of as an expansion, the dual of a quotient.
    The name `expansion' would be most fitting, but we follow the standard name `extension.'
    Given a structure, we may introduce a new element.
    Doing so yields a new structure with the same operations and more elements, thus an expansion of the original structure.

    Such a transformation can naturally be expressed by an optional equation describing the new element in terms of existing elements.
    When the equation is not given, the new element is called \emph{transcendental} over the original structure, and infinite expansion occurs.
    When the equation is given, the new element is called \emph{algebraic} over the original structure, and any amount of expansion may occur.
    The word `transcendental' reflects how the new element transcends anything expressible in the original structure.
    The word `algebraic' reflects how the new element is algebraically expressible in the original structure.

    An example is taking real numbers with addition and multiplication and introducing the element $i$ with the equation $i^2+1=0$.
    This yields the complex numbers.

\end{itemize}

In the special case of rings and finite fields, we may express quotients and (usually) extensions in special ways and special notation is used for each.
In both cases, we may replace the equation specifying the new structure with only an expression to act as one side of the equation with the other side predetermined.
In the case of quotients, the predetermined side is zero, an element present in every ring structure.
In the case of extensions, the predetermined side is the new element.
Isolating the new element on one side of the equation is usually doable using ring operations, namely addition, multiplication, exponentiation, and their inverted versions when they exist (as they do for finite fields).

As for notation, suppose we have existing ring structure $R$ and expression $E$.
In the case of quotients we may express the new structure as $R\mod E$ or $R/E$.
The former is read as `R modulo E' and the latter as `R over E.'
In the case of extensions we may express the new structure as $R[E]$, and iterated extensions of the form $R[E_1][E_2],\dots,[E_n]$ may be simplified to $R[E_1,E_2,\dots,E_n]$.
This is read as `adjoining elements $E_1,E_2,\dots,E_n$ to $R$.'
In the quotient and extension examples above, the relevant expressions are $2$ and $i^2+1$ respectively, and the relevant notations are $\Z\mod 2$ and $\R[\sqrt{-1}]$ respectively.

A common sequence of steps worth mentioning is an infinite expansion followed by a contraction.
Beginning with $R$ we first adjoin $x$ for a transcendental extension $R[x]$.
Then we set some expression $f(x)$ to zero for a contraction $R[x]/f(x)$.

\section{Arithmetic and its importance}

To conclude this discussion justifying our choice of rings and finite fields, it may be motivating to examine their relationship to arithmetic.
To clarify, by arithmetic we mean numbers under addition and multiplication.
All rings and finite fields can be obtained using arithmetic as a starting structure and applying a sequence of the three transformations above, namely derived definitions, quotients, and extensions.
Furthermore, the translation from arithmetic to any ring structure is \emph{natural}, usually only involving a few simple transformations.
Many other structures in mathematics, in contrast, do not flow so naturally from a single source like arithmetic.
If we accept this as justification for rings conditioned on arithmetic as our starting point, we may shift attention to justification for arithmetic.

Mathematics originates from the study of quantities, specifically via their representations as numbers and the ways in which they can be manipulated.
This original study is known as arithmetic, and our promised justification for arithmetic is tasked with showing the naturality of quantities and their ways of manipulation.

Regarding quantities, represented as numbers, their naturality and ubiquity is clear and taken for granted.
Analysis of nearly any structure in mathematics involves arithmetic simply because quantities of one thing or another are inevitably relevant.
Even the empty structure consisting of nothing, the simplest structure conceivable, involves the quantity zero.
A natural next step beyond a structure with \emph{nothing} is a structure with \emph{something}, from which we may conceive the quantity \emph{one}.
Continuing this process one may derive all natural numbers, repeatedly defining a new structure different from all previous structures, containing something they do not.

Regarding the manipulations of quantities, that is operations on numbers, we begin with addition and observe that other operations immediately follow.
The most basic operation performed on numbers is addition, and we take this operation for granted.
With zero as the most basic number and addition as the most basic operation, one may ask what numbers add to zero, and doing so one conceives the negative numbers along with the subtraction operation.
Another natural way to create a new operation is to extend an existing operation by repeating it more than once, or any number of times.
Note how the repetition parameter is itself a \emph{number}.
Repeating addition a certain number of times yields the multiplication operation.
In this case both the operator and the operand are of the same type (a number), a property unique to arithmetic because in other structures where the operands are not numbers, this symmetry cannot exist.
One may ask what numbers multiply to one, and doing so one conceives the rational numbers, along with the division operation.
One may go further, defining exponentiation, logarithms, roots, and all other arithmetic concepts.
Each structure is naturally derived from the previous.


\section{Quick Methodology}

\begin{itemize}
    \item Fix the parameters \( \Lambda \), \( \lambda_i^o \), \( \mu_i \) and
    \( C_i \).
    \item \( \forall \hspace{0.2cm} \hat{c_i} \in \{1,2, \dots, C_A\} \) and
    \( \forall \hspace{0.2cm} \hat{c_j} \in \{1,2, \dots, C_B\} \)
    \item Calculate \( p_A \) and \( p_B = 1-p_A \) s.t. \( (W_q)_A = (W_q)_B \).
    \item Calculate the probability \(P((W_q)_i \leq 4 \) hours
    \item Fill matrix A with \( U_{\hat{c_i}, \hat{c_j}}^A =
    1 - |0.95 - P((W_q)_A \leq 4)| \) and
    \item fill matrix B with \( U_{\hat{c_i}, \hat{c_j}}^B =
    1 - |0.95 - P((W_q)_B \leq 4)| \)
\end{itemize}

\begin{table}[h]
    \centering
    A =
    \begin{tabular}{|l|l|l|l|}
    \hline
    \( U_{1,1}^A \) & \( U_{1,2}^A \) & \dots & \( U_{1,C_B}^A \) \\ \hline
    \( U_{2,1}^A \) & \( U_{2,2}^A \) & \dots & \( U_{2,C_B}^A \) \\ \hline
    \vdots & \vdots & \( \ddots \) & \vdots \\ \hline
    \( U_{C_A,1}^A \) & \( U_{C_A,2}^A \) & \dots & \( U_{C_A,C_B}^A \) \\ \hline
    \end{tabular}
\end{table}

\begin{table}[h]
    \centering
    B =
    \begin{tabular}{|l|l|l|l|}
    \hline
    \( U_{1,1}^B \) & \( U_{1,2}^B \) & \dots & \( U_{1,C_B}^B \) \\ \hline
    \( U_{2,1}^B \) & \( U_{2,2}^B \) & \dots & \( U_{2,C_B}^B \) \\ \hline
    \vdots & \vdots & \( \ddots \) & \vdots \\ \hline
    \( U_{C_A,1}^B \) & \( U_{C_A,2}^B \) & \dots & \( U_{C_A,C_B}^B \) \\ \hline
    \end{tabular}
\end{table}

\begin{itemize}
    \item Ambulance decides the proportion of people to distribute to each hospital
    based on optimal patient distribution.
\end{itemize}


\section{Application: A formula for the inverse of a matrix}
\label{sec:adjugate}

\begin{outcome}
  \begin{enumerate}
  \item Find the cofactor matrix and the adjugate of a matrix.
  \item Find the inverse of a matrix using the adjugate formula.
  \end{enumerate}
\end{outcome}

The determinant of a matrix also provides a way to find the inverse of
a matrix.  Recall the definition of the inverse of a matrix from
Definition~\ref{def:invertible-matrix}. If $A$ is an
$n\times n$-matrix, we say that $A^{-1}$ is the inverse of $A$ if
$AA^{-1} = I$ and $A^{-1}A=I$.

We now define a new matrix called the \textbf{cofactor matrix} of $A$.
The cofactor matrix of $A$ is the matrix whose $\ijth$ entry is the
$\ijth$ cofactor of $A$.

\begin{definition}{The cofactor matrix}{cofactor-matrix}
  Let $A$ be an $n\times n$-matrix. Then the \textbf{cofactor matrix
    of $A$}%
  \index{cofactor matrix}%
  \index{matrix!cofactor matrix}, denoted $\cof(A)$, is defined by
  \begin{equation*}
    \cof(A)
    ~=~ \mat{\cofactor{A}{ij}}
    ~=~ \begin{mymatrix}{cccc}
      \cofactor{A}{11} & \cofactor{A}{12} & \cdots & \cofactor{A}{1n} \\
      \cofactor{A}{21} & \cofactor{A}{22} & \cdots & \cofactor{A}{2n} \\
      \vdots & \vdots & \ddots & \vdots \\
      \cofactor{A}{n1} & \cofactor{A}{n2} & \cdots & \cofactor{A}{nn} \\
    \end{mymatrix},
  \end{equation*}
  where $\cofactor{A}{ij}$ is the $\ijth$ cofactor of $A$.
\end{definition}

We will use the cofactor matrix to create a formula for the inverse of
$A$. First, we define the \textbf{adjugate}%
\index{adjugate of a matrix}%
\index{matrix!adjugate} of $A$, denoted $\adj(A)$, to be the transpose
of the cofactor matrix:
\begin{equation*}
  \adj(A) = \cof(A)^T.
\end{equation*}
The adjugate is also sometimes called the
\textbf{classical adjoint}%
\index{classical adjoint}%
\index{matrix!classical adjoint} of $A$.

\begin{example}{Cofactor matrix and adjugate}{cofactor-matrix-and-adjugate}
  Find the cofactor matrix and the adjugate of $A$, where
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We first find $\cof(A)$. To do so, we need to compute the cofactors
  of $A$. We have:
  \begin{eqnarray*}
    \cofactor{A}{11} ~=~ +\minor{A}{11}
    &=&
    \begin{absmatrix}{ccc}
      \strikeh{3.2em}{\strikev{3.2em}{1}} & 2 & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{absmatrix}
    ~=~ \begin{absmatrix}{rr}
      0 & 1 \\
      2 & 1 \\
    \end{absmatrix}
    ~=~ -2,
    \\
    \cofactor{A}{12} ~=~ -\minor{A}{12}
    &=&
    -\begin{absmatrix}{ccc}
      \strikeh{3.2em}{1} & \strikev{3.2em}{2} & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{absmatrix}
    ~=~ -\begin{absmatrix}{rr}
      3 & 1 \\
      1 & 1 \\
    \end{absmatrix}
    ~=~ -2,
    \\
    \cofactor{A}{13} ~=~ +\minor{A}{13}
    &=&
    \begin{absmatrix}{ccc}
      \strikeh{3.2em}{1} & 2 & \strikev{3.2em}{3} \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{absmatrix}
    ~=~ \begin{absmatrix}{rr}
      3 & 0 \\
      1 & 2 \\
    \end{absmatrix}
    ~=~ 6,
    \\
    \cofactor{A}{21} ~=~ -\minor{A}{21}
    &=&
    -\begin{absmatrix}{ccc}
      \strikev{3.2em}{1} & 2 & 3 \\
      \strikeh{3.2em}{3} & 0 & 1 \\
      1 & 2 & 1 \\
    \end{absmatrix}
    ~=~ -\begin{absmatrix}{rr}
      2 & 3 \\
      2 & 1 \\
    \end{absmatrix}
    ~=~ 4,
  \end{eqnarray*}
  and so on. Continuing in this way, we find the cofactor matrix
  \begin{equation*}
    \cof(A)
    =
    \begin{mymatrix}{rrr}
      -2 & -2 & 6 \\
      4 & -2 & 0 \\
      2 & 8 & -6 \\
    \end{mymatrix}.
  \end{equation*}
  Finally, the adjugate is the transpose of the cofactor matrix:
  \begin{equation*}
    \adj(A) = \cof(A)^T =
    \begin{mymatrix}{rrr}
      -2 & 4 & 2 \\
      -2 & -2 & 8 \\
      6 & 0 & -6 \\
    \end{mymatrix}.
  \end{equation*}
\end{solution}

The following theorem provides a formula for $A^{-1}$ using the
determinant and the adjugate of $A$.

\begin{theorem}{Formula for the inverse}{inverse-and-determinant}
  Let $A$ be an $n\times n$-matrix. Then
  \begin{equation*}
    A \, \adj(A) = \adj(A)\,A = \det(A)\,I.
  \end{equation*}
  Moreover, if $A$ is invertible if and only if $\det(A) \neq 0$. In
  this case, we have:
  \begin{equation*}
    A^{-1} = \frac{1}{\det(A)} \adj(A).
  \end{equation*}
  We call this the \textbf{adjugate formula}%
  \index{determinant!formula for matrix inverse}%
  \index{adjugate formula}%
  \index{inverse!of a matrix!adjugate formula}%
  \index{matrix!inverse!adjugate formula} for the matrix inverse.
\end{theorem}

\begin{proof}
  Recall that the $(i,j)$-entry of $\adj(A)$ is equal to
  $\cofactor{A}{ji}$.  Thus the $(i,j)$-entry of $B=A\,\adj(A)$ is:
  \begin{eqnarray*}
    B_{ij}
    &=& a_{i1}\adj(A)_{1j} + a_{i2}\adj(A)_{2j} + \ldots + a_{in}\adj(A)_{nj} \\
    &=& a_{i1}\cofactor{A}{j1} + a_{i2}\cofactor{A}{j2} + \ldots + a_{in}\cofactor{A}{jn}.
  \end{eqnarray*}
  By the cofactor expansion theorem, we see that this expression for
  $B_{ij}$ is equal to the determinant of the matrix obtained from $A$
  by replacing its $j$th row by $\mat{a_{i1}, a_{i2}, \dots a_{in}}$,
  i.e., by its $i$th row.

  If $i=j$ then this matrix is $A$ itself and therefore
  $B_{ii}=\det(A)$. If on the other hand $i\neq j$, then this matrix
  has its $i$th row equal to its $j$th row, and therefore $B_{ij}=0$
  in this case. Thus we obtain:
  \begin{equation*}
    A \, \adj(A) = {\det(A)} I.
  \end{equation*}
  By a similar argument (using columns instead of rows), we can verify that:
  \begin{equation*}
    \adj(A)\,A = {\det(A)} I.
  \end{equation*}
  This proves the first part of the theorem. For the second part,
  assume that $A$ is invertible. Then by
  Theorem~\ref{thm:determinant-invertible}, $\det(A)\neq 0$. Dividing the
  formula from the first part of the theorem by $\det(A)$, we obtain
  \begin{equation*}
    A\paren{\frac{1}{\det(A)}\adj(A)} ~=~ \paren{\frac{1}{\det(A)}\adj(A)}A ~=~ I,
  \end{equation*}
  and therefore
  \begin{equation*}
    A^{-1} = \frac{1}{\det(A)} \adj(A).
  \end{equation*}
  This completes the proof.
\end{proof}

\begin{example}{Finding the inverse using a formula}{inverse-and-determinant}
  Use the adjugate formula to find the inverse of the matrix
  \begin{equation*}
    A=\begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We must compute
  \begin{equation*}
    A^{-1} ~=~ \frac{1}{\det(A)} \adj(A).
  \end{equation*}
  We will start by computing the determinant. We expand along the
  second row:
  \begin{equation*}
    \det(A)
    ~=~ \begin{absmatrix}{rrr}
      1 & 2 & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1 \\
    \end{absmatrix}
    ~=~ -3 \begin{absmatrix}{rr}
      2 & 3 \\
      2 & 1 \\
    \end{absmatrix}
    - 1 \begin{absmatrix}{rr}
      1 & 2 \\
      1 & 2 \\
    \end{absmatrix}
    ~=~ -3(-4) -1(0) ~=~ 12.
  \end{equation*}
  We have already calculated the adjugate $\adj(A)$ in
  Example~\ref{exa:cofactor-matrix-and-adjugate}:
  \begin{equation*}
    \adj(A) ~=~
    \begin{mymatrix}{rrr}
      -2 & 4 & 2 \\
      -2 & -2 & 8 \\
      6 & 0 & -6 \\
    \end{mymatrix}.
  \end{equation*}
  Therefore, the inverse of $A$ is given by
  \begin{equation*}
    \def\arraystretch{1.4}
    A^{-1}
    ~=~ \frac{1}{\det(A)}\adj(A)
    ~=~ \frac{1}{12}\begin{mymatrix}{rrr}
      -2 & 4 & 2 \\
      -2 & -2 & 8 \\
      6 & 0 & -6 \\
    \end{mymatrix}
    ~=~ \begin{mymatrix}{rrr}
      -\frac{1}{6} & \frac{1}{3} & \frac{1}{6} \\
      -\frac{1}{6} & -\frac{1}{6} & \frac{2}{3} \\
      \frac{1}{2} & 0 & -\frac{1}{2} \\
    \end{mymatrix}.
  \end{equation*}
  Since it is very easy to make a mistake in this calculation, we
  double-check our answer by computing $A^{-1}A$:
  \begin{equation*}
    \def\arraystretch{1.4}
    A^{-1}A ~=~
    \allowbreak \begin{mymatrix}{rrr}
      -\frac{1}{6} & \frac{1}{3} &
      \frac{1}{6} \\
      -\frac{1}{6} & -\frac{1}{6} &
      \frac{2}{3} \\
      \frac{1}{2} & 0 & -\frac{1}{2}
    \end{mymatrix} \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      3 & 0 & 1 \\
      1 & 2 & 1
    \end{mymatrix} ~=~ \begin{mymatrix}{rrr}
      1 & 0 & 0 \\
      0 & 1 & 0 \\
      0 & 0 & 1
    \end{mymatrix}
    ~=~
    I.
  \end{equation*}
\end{solution}

\begin{example}{Finding the inverse using a formula}{inverse-formula}
  Use the adjugate formula to find the inverse of the matrix
  \begin{equation*}
    A=\begin{mymatrix}{rrr}
      0  &  2 &  1 \\
      -1 &  2 &  2 \\
      2  & -2 & -2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We start by calculating the determinant:
  \begin{equation*}
    \det(A)
    ~=~ \begin{absmatrix}{rrr}
      0  &  2 &  1 \\
      -1 &  2 &  2 \\
      2  & -2 & -2 \\
    \end{absmatrix}
    ~=~ -2 \begin{absmatrix}{rr}
      -1 &  2 \\
      2  & -2 \\
    \end{absmatrix}
    + 1 \begin{absmatrix}{rrr}
      -1 &  2 \\
      2  & -2 \\
    \end{absmatrix}
    ~=~ -2\cdot (-2) + 1\cdot(-2) ~=~ 2.
  \end{equation*}
  Next, we compute the cofactor matrix:
  \begin{equation*}
    \cof(A)
    ~=~
    \begin{mymatrix}{ccc}
      ~~~\begin{absmatrix}{rr}
       2 &  2 \\
       -2 & -2 \\
      \end{absmatrix}
      &
      -\begin{absmatrix}{rr}
      -1 &  2 \\
      2  & -2 \\
      \end{absmatrix}
      &
      ~~~\begin{absmatrix}{rr}
      -1 &  2 \\
      2  & -2 \\
      \end{absmatrix}
      \\\\[-1ex]
      -\begin{absmatrix}{rr}
      2 &  1 \\
      -2 & -2 \\
      \end{absmatrix}
      &
      ~~~\begin{absmatrix}{rr}
      0 &  1 \\
      2 & -2 \\
      \end{absmatrix}
      &
      -\begin{absmatrix}{rr}
      0  &  2 \\
      2  & -2 \\
      \end{absmatrix}
      \\\\[-1ex]
      ~~~\begin{absmatrix}{rr}
      2 &  1 \\
      2 &  2 \\
      \end{absmatrix}
      &
      -\begin{absmatrix}{rr}
      0  &  1 \\
      -1 &  2 \\
      \end{absmatrix}
      &
      ~~~\begin{absmatrix}{rr}
      0  &  2 \\
      -1 &  2 \\
      \end{absmatrix}
    \end{mymatrix}
    ~=~ \begin{mymatrix}{rrr}
      0 &  2 & -2 \\
      2 & -2 &  4 \\
      2 & -1 &  2 \\
    \end{mymatrix}.
  \end{equation*}
  The adjugate is the transpose of the cofactor matrix:
  \begin{equation*}
    \adj(A) ~=~ \cof(A)^T
    ~=~ \begin{mymatrix}{rrr}
      0  &  2 &  2 \\
      2  & -2 & -1 \\
      -2 &  4 &  2 \\
    \end{mymatrix}.
  \end{equation*}
  We therefore have
  \begin{equation*}
    \def\arraystretch{1.2}
    A^{-1}
    ~=~
    \frac{1}{\det(A)}\adj(A)
    ~=~
    \frac{1}{2}
    \begin{mymatrix}{rrr}
      0  &  2 &  2 \\
      2  & -2 & -1 \\
      -2 &  4 &  2 \\
    \end{mymatrix}
    ~=~
    \begin{mymatrix}{rrr}
      0  &  1 &  1 \\
      1  & -1 & -\frac{1}{2} \\
      -1 &  2 &  1 \\
    \end{mymatrix}.
  \end{equation*}
  Once again, we double-check our work by computing $A^{-1}A$:
  \begin{equation*}
    \def\arraystretch{1.2}
    A^{-1}A ~=~
    \begin{mymatrix}{rrr}
      0  &  1 &  1 \\
      1  & -1 & -\frac{1}{2} \\
      -1 &  2 &  1 \\
    \end{mymatrix}
    \begin{mymatrix}{rrr}
      0  &  2 &  1 \\
      -1 &  2 &  2 \\
      2  & -2 & -2 \\
    \end{mymatrix}
    ~=~ \begin{mymatrix}{rrr}
      1 & 0 & 0 \\
      0 & 1 & 0 \\
      0 & 0 & 1
    \end{mymatrix}.
  \end{equation*}
\end{solution}

It is always a good idea to double-check your work.  At the end of the
calculation, it is very easy to compute $A^{-1}A$ and check whether it
is equal to $I$. If they are not equal, be sure to go back and
double-check each step. One common mistake is to forget to take the
transpose of the cofactor matrix, so be sure not to forget this step.

In practice, it is usually much faster to compute the inverse by the
method of Section~\ref{ssec:computing-inverses}, because this only
requires solving a single system of equations, rather than computing a
large number of cofactors. However, there are some situations where
the adjugate formula is useful. One such situation is when the matrix
has complicated entries that are functions rather than numbers. The
following example illustrates this.

\begin{example}{Inverse for non-constant matrix}{inverse-non-constant-matrix}
  Let
  \begin{equation*}
    A(t) =\begin{mymatrix}{ccc}
      e^{t} & 0 & 0 \\
      0 & \cos t & \sin t \\
      0 & -\sin t & \cos t
    \end{mymatrix}.
  \end{equation*}
  Show that $A(t)^{-1}$ exists and find it.
\end{example}

\begin{solution}
  First note that
  \begin{equation*}
    \det(A(t)) ~=~ e^{t}(\cos^2 t + \sin^2 t) ~=~ e^{t}\neq 0.
  \end{equation*}
  Therefore $A(t)^{-1}$ exists for all values of the variable $t$. The
  cofactor matrix is
  \begin{equation*}
    \cof(A(t))
    ~=~ \begin{mymatrix}{ccc}
      1 & 0 & 0 \\
      0 & e^{t}\cos t & e^{t}\sin t \\
      0 & -e^{t}\sin t & e^{t}\cos t
    \end{mymatrix}.
  \end{equation*}
  The adjugate is the transpose of the cofactor matrix, and therefore
  the inverse is
  \begin{equation*}
    A(t)^{-1}
    ~=~ \frac{1}{\det(A(t))}\adj(A(t))
    ~=~
    \frac{1}{e^{t}}\begin{mymatrix}{ccc}
      1 & 0 & 0 \\
      0 & e^{t}\cos t & -e^{t}\sin t \\
      0 & e^{t}\sin t & e^{t}\cos t
    \end{mymatrix}
    ~=~ \begin{mymatrix}{ccc}
      e^{-t} & 0 & 0 \\
      0 & \cos t & -\sin t \\
      0 & \sin t & \cos t
    \end{mymatrix}.
  \end{equation*}
\end{solution}

Another situation where the adjugate formula is useful is the case of a
$2\times 2$-matrix. In this case both the determinant and the adjugate
are especially easy to compute. For a $2\times 2$-matrix
\begin{equation*}
  A ~=~ \begin{mymatrix}{rr}
    a & b \\
    c & d
  \end{mymatrix},
\end{equation*}
we have
\begin{eqnarray*}
  \det(A) &=& ad-bc
  \\
  \adj(A) &=&
  \begin{mymatrix}{rr}
    d & -b \\
    -c & a
  \end{mymatrix}.
\end{eqnarray*}
Therefore, $A$ is invertible if and only if $ad-bc\neq 0$, and in
that case, the inverse is given by
\begin{equation}\label{eqn:inverse-2-by-2}
  A^{-1} ~=~
  \frac{1}{ad-bc}
  \begin{mymatrix}{rr}
    d & -b \\
    -c & a
  \end{mymatrix}.
\end{equation}

\begin{example}{Inverse of a $2\times 2$-matrix}{inverse-2-by-2}
  Find the inverse of
  \begin{equation*}
    A ~=~ \begin{mymatrix}{rr}
      7 & 5 \\
      2 & 2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We use formula {\eqref{eqn:inverse-2-by-2}} to compute the inverse:
  \begin{equation*}
    \def\arraystretch{1.4}
    A^{-1} ~=~
    \frac{1}{7\cdot 2-2\cdot 5}
    \begin{mymatrix}{rr}
      2 & -5 \\
      -2 & 7
    \end{mymatrix}
    ~=~
    \begin{mymatrix}{rr}
      \frac{1}{2} & -\frac{5}{4} \\
      -\frac{1}{2} & \frac{7}{4}
    \end{mymatrix}.
  \end{equation*}
\end{solution}


Our equations look like:
\begin{equation}
\frac{\partial\Ub}{\partial t} = \nabla\cdot\Fb + \Sb_{\rm react} + \Sb,
\end{equation}
where $\Fb$ is the flux vector, $\Sb_{\rm react}$ are the reaction source terms, and $\Sb$ are the non-reaction source terms, which includes any user-defined external sources, $\Sb_{\rm ext}$.  We use Strang splitting to discretize the advection-reaction equations.  In summary, for each time step, we update the conservative variables, $\Ub$, by reacting for half a time step, advecting for a full time step (ignoring the reaction terms), and reacting for half a time step.  In summary,
\begin{equation}
\Ub^n = \Ub^n + \frac{\dt}{2}\Sb_{\rm react}^n,
\end{equation}
\begin{equation}
\Ub^{n+1} = \Ub^n - \Delta t \nabla \cdot\Fb^\nph + \dt\frac{\Sb^n + \Sb^{n+1}}{2},
\end{equation}
\begin{equation}
\Ub^{n+1} = \Ub^{n+1} + \frac{\dt}{2}\Sb_{\rm react}^{n+1},
\end{equation}
The construction of $F$ is purely explicit, and based on an unsplit second-order Godunov method.  We predict the standard primitive variables, as well as $\rho e$, at time-centered edges and use an approximate Riemann solver construct fluxes.  At the beginning of the time step, we assume that $\Ub$ and $\phi$ are defined consistently, i.e., $\rho^n$ and $\phi^n$ satisfy equation (\ref{eq:Self Gravity}).\\

CASTRO also supports radiation (Chapter \ref{Chap:Radiation}) and level sets (Chapter \ref{Chap:Level Sets}).  We omit the details in this section.  Here is the single-level algorithm:
\begin{description}
\item[Step 1:] {\em React $\Delta t/2$.}

Update the solution due to the effect of reactions over half a time step.
\begin{eqnarray}
(\rho E)^n &=& (\rho E)^n - \frac{\dt}{2}\sum_k(\rho q_k\omegadot_k)^n,\\
(\rho X_k)^n &=& (\rho X_k)^n + \frac{\dt}{2}(\rho\omegadot_k)^n.
\end{eqnarray}
\item[Step 2:] {\em Solve for gravity.}

Solve for gravity using:
\begin{equation}
\gb^n = \nabla\phi^n, \qquad
\Delta\phi^n = -4\pi G\rho^n,
\end{equation}
or use one of the simpler gravity types.

\item[Step 3:] {\em Compute explicit source terms.}

We now compute explicit source terms for each variable in $\Qb$ and $\Ub$.  The primitive variable source terms will be used to construct time-centered fluxes.  The conserved variable source will be used to advance the solution.  We neglect reaction source terms since they are accounted for in {\bf Steps 1} and {\bf 6}.  The source terms are:
\begin{equation}
\Sb_{\Qb}^n =
\left(\begin{array}{c}
S_\rho \\
\Sb_{\ub} \\
S_p \\
S_{\rho e} \\
S_{A_k} \\
S_{X_k} \\
S_{Y_k}
\end{array}\right)^n
=
\left(\begin{array}{c}
S_{{\rm ext},\rho} \\
\gb + \frac{1}{\rho}\Sb_{{\rm ext},\rho\ub} \\
\frac{1}{\rho}\frac{\partial p}{\partial e}S_{{\rm ext},\rho E} + \frac{\partial p}{\partial\rho}S_{{\rm ext}\rho} \\
\nabla\cdot\kappa\nabla T + S_{{\rm ext},\rho E} \\
\frac{1}{\rho}S_{{\rm ext},\rho A_k} \\
\frac{1}{\rho}S_{{\rm ext},\rho X_k} \\
\frac{1}{\rho}S_{{\rm ext},\rho Y_k}
\end{array}\right)^n,
\end{equation}
\begin{equation}
\Sb_{\Ub}^n =
\left(\begin{array}{c}
\Sb_{\rho\ub} \\
S_{\rho E} \\
S_{\rho A_k} \\
S_{\rho X_k} \\
S_{\rho Y_k}
\end{array}\right)^n
=
\left(\begin{array}{c}
\rho \gb + \Sb_{{\rm ext},\rho\ub} \\
\rho \ub \cdot \gb + \nabla\cdot\kappa\nabla T + S_{{\rm ext},\rho E} \\
S_{{\rm ext},\rho A_k} \\
S_{{\rm ext},\rho X_k} \\
S_{{\rm ext},\rho Y_k}
\end{array}\right)^n.
\end{equation}

\item[Step 3:] {\em Advect $\Delta t$.}

The goal is to advance
\begin{equation}
\Ub^{n+1} = \Ub^n - \dt\nabla\cdot\Fb^\nph + \dt\Sb^n.
\end{equation}
neglecting reaction terms.  Note that since the source term is not time centered, this is not a second-order method.  After the advective update, we correct the solution, effectively time-centering the source term. The advection step is complicated, and more detail is given in Section \ref{Sec:Advection Step}.  Here is the summarized version:
\begin{enumerate}
\item Compute primitive variables.
\item Predict primitive variables to time-centered edges.
\item Solve the Riemann problem.
\item Compute fluxes and update.
\end{enumerate}
\item[Step 4:] {\em Solve for updated gravity.}

Solve for gravity using:
\begin{equation}
\gb^{n+1} = \nabla\phi^{n+1}; \qquad \Delta\phi^{n+1} = -4\pi G\rho^{n+1},
\end{equation}
or use one of the simpler gravity types.
\item[Step 5:] {\em Correct solution with time-centered source terms.}

We need to correct the solution by effectively time-centering the source terms.  These corrections are to be performed sequentially since new source term evaluations may depend on previous corrections.

First, we correct the solution with the updated gravity:
\begin{eqnarray}
(\rho\ub)^{n+1} &=& (\rho\ub)^{n+1} + \frac{\dt}{2}\left[(\rho\gb)^{n+1} - (\rho\gb)^n\right], \\
(\rho E)^{n+1} &=& (\rho E)^{n+1} + \frac{\dt}{2}\left[\left(\rho\ub\cdot\gb\right)^{n+1} - \left(\rho\ub\cdot\gb\right)^n\right].
\end{eqnarray}

Next, we correct $\Ub$ with updated external sources.  For example, for the momentum, we correct using
\begin{equation}
(\rho\ub)^{n+1} = (\rho\ub)^{n+1} + \frac{\dt}{2}\left(\Sb_{{\rm ext},\rho\ub}^{n+1} - \Sb_{{\rm ext},\rho\ub}^n\right).
\end{equation}
We correct $\rho E, \rho A_k, \rho X_k$, and $\rho Y_k$ in an analogous manner.

Finally, we correct the solution with updated thermal diffusion using
\begin{equation}
(\rho E)^{n+1} = (\rho E)^{n+1} + \frac{\dt}{2}\left(\nabla\cdot\kappa\nabla T^{n+1} - \nabla\cdot\kappa\nabla T^n\right).
\end{equation}
\item[Step 6:] {\em React $\Delta t/2$.}

Update the solution due to the effect of reactions over half a time step.
\begin{eqnarray}
(\rho E)^{n+1} &=& (\rho E)^{n+1} - \frac{\dt}{2}\sum_k(\rho q_k\omegadot_k)^{n+1},\\
(\rho X_k)^{n+1} &=& (\rho X_k)^{n+1} + \frac{\dt}{2}(\rho\omegadot_k)^{n+1}.
\end{eqnarray}
\item[Step 7:] {\em Modify auxiliary variables.}

This is problem-dependent.  By default we treat the auxiliary variables as
advected quantities, so no additional steps are required.
\end{description}
Thus concludes the single-level algorithm description.

\subsection{Nyx::advance()}

if (doGrav)

\hspace{.1in}  define oldGravityVector

end if

AdvanceSolution()

if (doGrav)

\hspace{.1in}  define newGravityVector

\hspace{.1in}  correct solution due to new gravity

end if

\section{Advection Step}\label{Sec:Advection Step}
There are four major steps in the advective update, detailed below.
\subsection{Compute Primitive Variables}\label{Sec:Compute Primitive Variables}
We compute the primtive variables from the conserved variables.
\begin{itemize}
\item $\rho, \rho e$ - directly copy these from the conserved state vector
\item $\ub, A_k, X_k, Y_k$ - copy these from the conserved state vector, dividing by $\rho$
\item $p,T$ - use the EOS.  First, we do the following:
\begin{enumerate}
\item Use the EOS to set $e = e(\rho,T_{\rm small},X_k)$.
\item If $e < 0$, abort the program with an error message.
\end{enumerate}
Now, use the EOS to compute $p,T = p,T(\rho,e,X_k)$.
\end{itemize}
We also compute the flattening coefficient, $\chi\in[0,1]$, used in the edge state prediction to further limit slopes near strong shocks.  We use the same flattening procedure described in the the FLASH paper.  A flattening coefficient of 1 indicates that no additional limiting takes place; a flattening coefficient of 0 means we effectively drop order to a first-order Godunov scheme (this convention is opposite of that used in the FLASH paper).  For each cell, we compute the flattening coefficient for each spatial direction, and choose the minimum value over all directions.  As an example, to compute the flattening for the x-direction, here are the steps:
\begin{enumerate}
\item Define $\zeta$
\begin{equation}
\zeta_i = \frac{p_{i+1}-p_{i-1}}{\max\left(p_{\rm small},|p_{i+2}-p_{i-2}|\right)}.
\end{equation}
\item Define $\tilde\chi$
\begin{equation}
\tilde\chi_i = \min\left\{1,\max[0,a(\zeta_i - b)]\right\},
\end{equation}
where $a=10$ and $b=0.75$ are tunable parameters.  We are essentially setting $\tilde\chi_i=a(\zeta_i-b)$, and then constraining $\tilde\chi_i$ to lie in the range $[0,1]$.  Then, if either $u_{i+1}-u_{i-1}<0$ or
\begin{equation}
\frac{p_{i+1}-p_{i-1}}{\min(p_{i+1},p_{i-1})} \le c,
\end{equation}
where $c=1/3$ is a tunable parameter, then set $\tilde\chi_i=0$.
\item Define $\chi$
\begin{equation}
\chi_i =
\begin{cases}
1 - \max(\tilde\chi_i,\tilde\chi_{i-1}) & p_{i+1}-p_{i-1} > 0 \\
1 - \max(\tilde\chi_i,\tilde\chi_{i+1}) & \text{otherwise}
\end{cases}.
\end{equation}
\end{enumerate}
\subsection{Edge State Prediction}
We wish to compute a left and right state of primitive variables at each edge to be used as inputs to the Riemann problem.  We use a version of the Colella and Sekora 2009 PPM algorithm, which has been further modified to eliminate sensitivity due to roundoff error (modifications via personal communication with Colella).  Note that CASTRO also has options for the original PPM algorithm of Colella and Woodward 1984, and piecewise-linear algorithm described in Saltzman 1994.  We also use characteristic tracing with corner coupling in 3D, as described in Miller and Colella 2002.  We give full details of the PPM algorithm, as it has not appeared before in the literature, and summarize the developments from Miller and Colella 2002.

The PPM algorithm is used to compute time-centered edge states by extrapolating the base-time data in space and time.  The edge states are dual-valued, i.e., at each face, there is a left state and a right state estimate.  The spatial extrapolation is one-dimensional, i.e., transverse derivatives are ignored.  We also use a flattening procedure to further limit the edge state values.  The Miller and Colella 2002 algorithm, which we describe later, incorporates the transverse terms, and also describes the modifications required for equations with additional characteristics besides the fluid velocity.  There are four steps to compute these dual-valued edge states (here, we use $s$ to denote an arbitrary scalar from $\Qb$, and we write the equations in 1D, for simplicity):
\begin{itemize}
\item {\bf Step 1}: Compute $s_{i,+}$ and $s_{i,-}$, which are spatial interpolations of $s$ to the hi and lo side of the face with special limiters, respectively.  Begin by interpolating $s$ to edges using a 4th-order interpolation in space:
\begin{equation}
s_{i+\myhalf} = \frac{7}{12}\left(s_{i+1}+s_i\right) - \frac{1}{12}\left(s_{i+2}+s_{i-1}\right).
\end{equation}
Then, if $(s_{i+\myhalf}-s_i)(s_{i+1}-s_{i+\myhalf}) < 0$, we limit $s_{i+\myhalf}$
a nonlinear combination of approximations to the second derivative.  The steps are as follows:
\begin{enumerate}
\item Define:
\begin{eqnarray}
(D^2s)_{i+\myhalf} &=& 3\left(s_{i}-2s_{i+\myhalf}+s_{i+1}\right) \\
(D^2s)_{i+\myhalf,L} &=& s_{i-1}-2s_{i}+s_{i+1} \\
(D^2s)_{i+\myhalf,R} &=& s_{i}-2s_{i+1}+s_{i+2}
\end{eqnarray}
\item Define
\begin{equation}
s = \text{sign}\left[(D^2s)_{i+\myhalf}\right],
\end{equation}
\begin{equation}
(D^2s)_{i+\myhalf,\text{lim}} = s\max\left\{\min\left[Cs\left|(D^2s)_{i+\myhalf,L}\right|,Cs\left|(D^2s)_{i+\myhalf,R}\right|,s\left|(D^2s)_{i+\myhalf}\right|\right],0\right\},
\end{equation}
where $C=1.25$ as used in Colella and Sekora 2009.  The limited value of $s_{i+\myhalf}$ is
\begin{equation}
s_{i+\myhalf} = \frac{1}{2}\left(s_{i}+s_{i+1}\right) - \frac{1}{6}(D^2s)_{i+\myhalf,\text{lim}}.
\end{equation}
\end{enumerate}
Now we implement an updated implementation of the Colella and Sekora 2009 algorithm which eliminates sensitivity to roundoff.  First we need to detect whether a particular cell corresponds to an ``extremum''.  There are two tests.
\begin{itemize}
\item For the first test, define
\begin{equation}
\alpha_{i,\pm} = s_{i\pm\myhalf} - s_i.
\end{equation}
If $\alpha_{i,+}\alpha_{i,-} \ge 0$, then we are at an extremum.
\item We only apply the second test if either $|\alpha_{i,\pm}| > 2|\alpha_{i,\mp}|$.
If so, we define:
\begin{eqnarray}
(Ds)_{i,{\rm face},-} &=& s_{i-\myhalf} - s_{i-\sfrac{3}{2}} \\
(Ds)_{i,{\rm face},+} &=& s_{i+\sfrac{3}{2}} - s_{i-\myhalf}
\end{eqnarray}
\begin{equation}
(Ds)_{i,{\rm face,min}} = \min\left[\left|(Ds)_{i,{\rm face},-}\right|,\left|(Ds)_{i,{\rm face},+}\right|\right].
\end{equation}
\begin{eqnarray}
(Ds)_{i,{\rm cc},-} &=& s_{i} - s_{i-1} \\
(Ds)_{i,{\rm cc},+} &=& s_{i+1} - s_{i}
\end{eqnarray}
\begin{equation}
(Ds)_{i,{\rm cc,min}} = \min\left[\left|(Ds)_{i,{\rm cc},-}\right|,\left|(Ds)_{i,{\rm cc},+}\right|\right].
\end{equation}
If $(Ds)_{i,{\rm face,min}} \ge (Ds)_{i,{\rm cc,min}}$, set
$(Ds)_{i,\pm} = (Ds)_{i,{\rm face},\pm}$.  Otherwise, set
$(Ds)_{i,\pm} = (Ds)_{i,{\rm cc},\pm}$.  Finally, we are at an extreumum if
$(Ds)_{i,+}(Ds)_{i,-} \le 0$.
\end{itemize}
Thus concludes the extremum tests.  The remaining limiters depend on whether we are at an extremum.
\begin{itemize}
\item If we are at an extremum, we modify $\alpha_{i,\pm}$.  First, we define
\begin{eqnarray}
(D^2s)_{i} &=& 6(\alpha_{i,+}+\alpha_{i,-}) \\
(D^2s)_{i,L} &=& s_{i-2}-2s_{i-1}+s_{i} \\
(D^2s)_{i,R} &=& s_{i}-2s_{i+1}+s_{i+2} \\
(D^2s)_{i,C} &=& s_{i-1}-2s_{i}+s_{i+1}
\end{eqnarray}
Then, define
\begin{equation}
s = \text{sign}\left[(D^2s)_{i}\right],
\end{equation}
\begin{equation}
(D^2s)_{i,\text{lim}} = \max\left\{\min\left[s(D^2s)_{i},Cs\left|(D^2s)_{i,L}\right|,Cs\left|(D^2s)_{i,R}\right|,Cs\left|(D^2s)_{i,C}\right|\right],0\right\}.
\end{equation}
Then,
\begin{equation}
\alpha_{i,\pm} = \frac{\alpha_{i,\pm}(D^2s)_{i,\text{lim}}}{\max\left[(D^2s)_{i},1\times 10^{-10}\right]}
\end{equation}
\item If we are not at an extremum and $|\alpha_{i,\pm}| > 2|\alpha_{i,\mp}|$, then define
\begin{equation}
s = \text{sign}(\alpha_{i,\mp})
\end{equation}
\begin{equation}
\delta\mathcal{I}_{\text{ext}} = \frac{-\alpha_{i,\pm}^2}{4\left(\alpha_{j,+}+\alpha_{j,-}\right)},
\end{equation}
\begin{equation}
\delta s = s_{i\mp 1} - s_i,
\end{equation}
If $s\delta\mathcal{I}_{\text{ext}} \ge s\delta s$, then we perform the following test.
If $s\delta s - \alpha_{i,\mp} \ge 1\times 10^{-10}$, then
\begin{equation}
\alpha_{i,\pm} =  -2\delta s - 2s\left[(\delta s)^2 - \delta s \alpha_{i,\mp}\right]^{\myhalf}
\end{equation}
otherwise,
\begin{equation}
\alpha_{i,\pm} =  -2\alpha_{i,\mp}
\end{equation}
\end{itemize}
Finally, $s_{i,\pm} = s_i + \alpha_{i,\pm}$.
\item {\bf Step 2}: Construct a quadratic profile using $s_{i,-},s_i$, and $s_{i,+}$.
\begin{equation}
s_i^I(x) = s_{i,-} + \xi\left[s_{i,+} - s_{i,-} + s_{6,i}(1-\xi)\right],\label{Quadratic Interp}
\end{equation}
\begin{equation}
s_6 = 6s_{i} - 3\left(s_{i,-}+s_{i,+}\right),
\end{equation}
\begin{equation}
\xi = \frac{x - ih}{h}, ~ 0 \le \xi \le 1.
\end{equation}
\item {\bf Step 3:} Integrate quadratic profiles.  We are essentially computing the average value swept out by the quadratic profile across the face assuming the profile is moving at a speed $\lambda_k$.\\ \\
Define the following integrals, where $\sigma_k = |\lambda_k|\Delta t/h$:
\begin{eqnarray}
\mathcal{I}_{i,+}(\sigma_k) &=& \frac{1}{\sigma_k h}\int_{(i+\myhalf)h-\sigma_k h}^{(i+\myhalf)h}s_i^I(x)dx \\
\mathcal{I}_{i,-}(\sigma_k) &=& \frac{1}{\sigma_k h}\int_{(i-\myhalf)h}^{(i-\myhalf)h+\sigma_k h}s_i^I(x)dx
\end{eqnarray}
Plugging in (\ref{Quadratic Interp}) gives:
\begin{eqnarray}
\mathcal{I}_{i,+}(\sigma_k) &=& s_{i,+} - \frac{\sigma_k}{2}\left[s_{i,+}-s_{i,-}-\left(1-\frac{2}{3}\sigma_k\right)s_{6,i}\right], \\
\mathcal{I}_{i,-}(\sigma_k) &=& s_{i,-} + \frac{\sigma_k}{2}\left[s_{i,+}-s_{i,-}+\left(1-\frac{2}{3}\sigma_k\right)s_{6,i}\right].
\end{eqnarray}
\item {\bf Step 4:} Obtain 1D edge states by performing a 1D extrapolation to get
left and right edge states.  Note that we include an explicit source term contribution.
\begin{eqnarray}
s_{L,i+\myhalf} &=& s_i - \chi_i\sum_{k:\lambda_k \ge 0}\lb_k\cdot\left[s_i-\mathcal{I}_{i,+}(\sigma_k)\right]\rb_k + \frac{\dt}{2}S_i^n, \\
s_{R,i-\myhalf} &=& s_i - \chi_i\sum_{k:\lambda_k < 0}\lb_k\cdot\left[s_i-\mathcal{I}_{i,-}(\sigma_k)\right]\rb_k + \frac{\dt}{2}S_i^n.
\end{eqnarray}
Here, $\rb_k$ is the $k^{\rm th}$ right column eigenvector of $\Rb(\Ab_d)$ and $\lb_k$ is the $k^{\rm th}$ left row eigenvector lf $\Lb(\Ab_d)$.  The flattening coefficient is $\chi_i$.
\end{itemize}
In order to add the transverse terms in an spatial operator unsplit framework, the details follow exactly as given in Section 4.2.1 in Miller and Colella 2002, except for the details of the Riemann solver, which are given below.
\subsection{Riemann Problem}
Inputs from the edge state prediction are $\rho_{L/R}, u_{L/R}, v_{L/R}, p_{L/R}$, and $(\rho e)_{L/R}$ ($v$ represents all of the transverse velocity components).  We also compute $\gamma$ at cell centers and copy these to edges directly to get the left and right states, $\gamma_{L/R}$.  We also define $c_{\rm avg}$ as a face-centered value that is the average of the neighboring cell-centered values of $c$.  We have also computed $\rho_{\rm small}, p_{\rm small}$, and $c_{\rm small}$ using cell-centered data.

Here are the steps.  First, define $(\rho c)_{\rm small} = \rho_{\rm small}c_{\rm small}$. Then, define:
\begin{equation}
(\rho c)_{L/R} = \max\left[(\rho c)_{\rm small},\left|\gamma_{L/R},p_{L/R},\rho_{L/R}\right|\right].
\end{equation}
Define star states:
\begin{equation}
p^* = \max\left[p_{\rm small},\frac{\left[(\rho c)_L p_R + (\rho c)_R p_L\right] + (\rho c)_L(\rho c)_R(u_L-u_R)}{(\rho c)_L + (\rho c)_R}\right],
\end{equation}
\begin{equation}
u^* = \frac{\left[(\rho c)_L u_L + (\rho c)_R u_R\right]+ (p_L - p_R)}{(\rho c)_L + (\rho c)_R}.
\end{equation}
If $u^* \ge 0$ then define $\rho_0, u_0, p_0, (\rho e)_0$ and $\gamma_0$ to be the left state.  Otherwise, define them to be the right state.  Then, set
\begin{equation}
\rho_0 = \max(\rho_{\rm small},\rho_0),
\end{equation}
and define
\begin{equation}
c_0 = \max\left(c_{\rm small},\sqrt{\frac{\gamma_0 p_0}{\rho_0}}\right),
\end{equation}
\begin{equation}
\rho^* = \rho_0 + \frac{p^* - p_0}{c_0^2},
\end{equation}
\begin{equation}
(\rho e)^* = (\rho e)_0 + (p^* - p_0)\frac{(\rho e)_0 + p_0}{\rho_0 c_0^2},
\end{equation}
\begin{equation}
c^* = \max\left(c_{\rm small},\sqrt{\left|\frac{\gamma_0 p^*}{\rho^*}\right|}\right)
\end{equation}
Then,
\begin{eqnarray}
c_{\rm out} &=& c_0 - {\rm sign}(u^*)u_0, \\
c_{\rm in} &=& c^* - {\rm sign}(u^*)u^*, \\
c_{\rm shock} &=& \frac{c_{\rm in} + c_{\rm out}}{2}.
\end{eqnarray}
If $p^* - p_0 \ge 0$, then $c_{\rm in} = c_{\rm out} = c_{\rm shock}$.  Then, if $c_{\rm out} = c_{\rm in}$, we define $c_{\rm temp} = \epsilon c_{\rm avg}$.  Otherwise, $c_{\rm temp} = c_{\rm out} - c_{\rm in}$.  We define the fraction
\begin{equation}
f = \half\left[1 + \frac{c_{\rm out} + c_{\rm in}}{c_{\rm temp}}\right],
\end{equation}
and constrain $f$ to lie in the range $f\in[0,1]$.

To get the final ``Godunov'' state, for the transverse velocity, we upwind based on $u^*$.
\begin{equation}
v_{\rm gdnv} =
\begin{cases}
v_L, & u^* \ge 0 \\
v_R, & {\rm otherwise}
\end{cases}.
\end{equation}
Then, define
\begin{eqnarray}
\rho_{\rm gdnv} &=& f\rho^* + (1-f)\rho_0, \\
u_{\rm gdnv} &=& f u^* + (1-f)u_0, \\
p_{\rm gdnv} &=& f p^* + (1-f)p_0, \\
(\rho e)_{\rm gdnv} &=& f(\rho e)^* + (1-f)(\rho e)_0.
\end{eqnarray}
Finally, if $c_{\rm out} < 0$, set $\rho_{\rm gdnv}=\rho_0, u_{\rm gdnv}=u_0, p_{\rm gdnv}=p_0$, and $(\rho e)_{\rm gdnv}=(\rho e)_0$.  If $c_{\rm in}\ge 0$, set $\rho_{\rm gdnv}=\rho^*, u_{\rm gdnv}=u^*, p_{\rm gdnv}=p^*$, and $(\rho e)_{\rm gdnv}=(\rho e)^*$.
\subsection{Compute Fluxes and Update}
Compute the fluxes as a function of the primitive variables, and then advance the solution:
\begin{equation}
\Ub^{n+1} = \Ub^n - \dt\nabla\cdot\Fb^\nph + \dt\Sb^n.
\end{equation}
Again, note that since the source term is not time centered, this is not a second-order method.  After the advective update, we correct the solution, effectively time-centering the source term.


\chapter{Approximate Bayesian Computation}
\label{chap:abc}

We are now in possession of a sampler able to approximate the posterior distribution $p(\btheta \mid \by_{1:T})$ even when the model likelihood is not tractable. As such, the sampler can be used in general non-linear SSMs, as long as the observation model $\obs_t(\by_t \mid \bx_t, \btheta)$ is a well-defined probability density. This requirement can be relaxed by introducing the method of Approximate Bayesian Computation (ABC). The algorithm derived in this chapter utilizes the ABC framework to approximate the SSM likelihood even when the observation model is misspecified or given only as a deterministic mapping $\bx_t \mapsto \by_t$. This allows to infer $\btheta$ even when $\obs_t(\by_t \mid \bx_t, \btheta)$ is not given in terms of a probability density function.

We first motivate the use of ABC methods in our problem in \autoref{sec:abc-motivation}. Then, in \autoref{sec:abc-general}, we describe the method in general and discuss some limitations. \autoref{sec:abc-ssm} introduces ABC to our state-space model framework and addresses some potential issues through kernel functions. Finally, in \autoref{sec:abcmh}, we summarize how exactly is the ABC method used in our model, and provide an alternative variant of the Metropolis-Hastings algorithm which relies on ABC instead of the particle filter to estimate the likelihood.

\section{Motivation} \label{sec:abc-motivation}
In the previous chapter, we derived a way to bypass the likelihood function evaluation when calculating the Metropolis-Hastings acceptance ratio. The method relies on the particle filter to calculate a set of weights $w_t^{(i)} \propto \obs_t(\by_t \mid \bx_t^{(i)}, \btheta)$, where $\obs_t(\by_t \mid \bx_t^{(i)}, \btheta)$ is the observation model defined in \eqref{eq:factorization}. These weights are used to estimate the likelihood $p(\by_{1:T} \mid \btheta)$ as given in \eqref{eq:likelihood-estimate}. However, calculating the weights in such way requires full knowledge of this observation model.

In practice, one may not have access to a correct observation model in the form of a probability density $\obs_t(\by_t \mid \bx_t, \btheta)$. Instead, only a model of the process which generates an observation $\by_t$ from the latent state $\bx_t$ may be available. This generative process may take the form of a differential equation, chemical reaction, simulation, etc. One is then in possession of a mean to generate an observation, but not to evaluate how probable it is. By attempting to fit an arbitrary probability distribution to this generative model, an error is necessarily introduced. The particle filter weights might then not reflect reality, and would lead to incorrect results when using such misspecified model for $\obs_t(\by_t \mid \bx_t, \btheta)$.

As an alternative way to approximate the likelihood ${p(\by_{1:T} \mid \btheta)}$, we can utilize our knowledge of the generative process $\bx_t \mapsto \by_t$ to simulate a number of pseudo-observations $\bu_t$. A surrogate for the observation density $\obs_t(\by_t \mid \bx_t, \btheta)$ is then calculated by evaluating the closeness of these pseudo-observations to the true measurement $\by_t$. Intuitively, if a large number of the simulated observations fall close to $\by_t$, we would expect the true probability density to be high in that region. By bypassing the evaluation of $\obs_t(\by_t \mid \bx_t, \btheta)$, inference can proceed even without knowing the observation model density. This is exactly the idea behind the approximate Bayesian computation methodology; it is discussed in \autoref{sec:abc-general}.

Unfortunately, such approximation comes at a price. In \autoref{chap:inference}, it has been shown that using the particle filter does not introduce any approximation error, since the likelihood estimate is unbiased and leaves the limiting distribution of the Metropolis-Hastings Markov chain intact. This is not the case when applying ABC methods, see \autoref{sec:abc-ssm}.

\section{ABC in general} \label{sec:abc-general}
Before describing how to apply ABC to state-space models, we first summarize the underlying ideas. The ABC method is introduced in the context of general Bayesian inference under a misspecified likelihood function. Later on, we build on these foundations when applying ABC to our SSM framework.

One thing to note is that ABC has traditionally been applied to estimate the posterior $p(\btheta \mid \by)$ for some parameter $\btheta$ and observation $\by$. The method is first considered with this application in mind, and in \autoref{sec:abc-ssm}, we describe how use it to estimate the SSM likelihood instead.

\paragraph{Approximate Bayesian Computation}
The methodology of ABC dates back to \cite{abc-old-old}, where a procedure using simulated psedudo-observations to approximate the posterior distribution was first described. Lately, ABC methods have gained popularity in modelling biological processes \citep{abc-old}. A more recent review can be found in \cite{abc-recent, abc-super-recent}.

In its classical formulation, ABC provides a way to approximate an intractable posterior ${p(\btheta \mid \by) \propto p(\by \mid \btheta) \pprior(\btheta)}$ by introducing an auxiliary variable $\bu$. The posterior approximation is then constructed by integrating over this variable and considering only values sufficiently close to the true measurement \citep{jasra-filtering}. It takes the form of
\begin{equation} \label{eq:abc-integral}
p(\btheta \mid \by) \approx p^\epsilon(\btheta \mid \by) = \frac{\int \I_{\A_{\epsilon, \by}}(\bu) p(\bu \mid \btheta) \pprior(\btheta) \; \dx{\bu}}{\int_{\A_{\epsilon, \by}} \dx{\bu}},
\end{equation}
where $\I_{\A_{\epsilon, \by}}$ is the indicator function of a set $\A_{\epsilon, \by} = \left\{\bu \in \R^{d_y} : \rho(\bu, \by) \leq \epsilon \right\}$ and $\rho: \R^{d_y} \times \R^{d_y} \to \R$ is a metric, typically the Euclidean distance.

The motivation behind \eqref{eq:abc-integral} is that such integral can be approximated by randomly sampling from the likelihood $p(\cdot \mid \btheta)$ without needing to evaluate it. This way, the likelihood can exist only conceptually, and we are able to simulate samples $\bu$ from a model reflecting some real-world process, without considering the underlying probability density.

The hyper-parameter $\epsilon \geq 0$ controls how far the auxiliary variable $\bu$ can be from the true measurement $\by$ to be considered similar. Clearly, if we set $\epsilon = 0$, the integral becomes $p(\by \mid \btheta) \pprior(\btheta)$, and we recover the true posterior. In general, the smaller $\epsilon$, the better approximation is obtained, though at the cost of increased computational complexity.

To avoid the curse of dimensionality, a summary statistic $\bm{s}: \R^{d_y} \to \R^p$, $1 \leq p < d_y$ is often introduced. Instead of comparing $\rho(\bu, \by) \leq \epsilon$, one then compares $\rho(\bm{s}(\bu), \bm{s}(\by)) \leq \epsilon$ (assuming that the metric has been redefined to $\rho: \R^p \times \R^p \to \R$).

It can be shown that if $\bm{s}$ is a sufficient statistic for the parameter $\btheta$, the probability density $p^\epsilon(\btheta \mid \by)$ converges to $p(\btheta \mid \by)$ as $\epsilon \to 0$ \citep{jasra-time-series}. However, it is typically impossible to find such statistic outside of the exponential family of distributions. Otherwise, using a statistic that is not sufficient introduces an additional approximation error.

\paragraph{Basic version of the ABC simulation}
We now give a basic variant of a sampling-based approximation to $p^\epsilon(\btheta \mid \by)$. In the spirit of \eqref{eq:abc-integral}, \autoref{alg:abc-rejection} performs rejection sampling by comparing whether a sampled $\bu$ is in $\A_{\epsilon, \by}$ or not. After describing the algorithm, we discuss some limitations of this basic approach.
\begin{algorithm}[ht]
    \caption{ABC Rejection Algorithm}
    \label{alg:abc-rejection}
    \begin{algorithmic}[1]
        \Input $\text{Number of samples } M, \text{ observation } \by, \text{ metric } \rho, \text{ maximum distance } \epsilon.$

        \State $i \gets 1$

        \While{$i \leq M$}
        \State $\text{Sample } \btheta^\prime \sim \pprior(\cdot).$ \Comment{Sample from the prior.}
        \State $\text{Simulate } \bu \text{ from } p(\cdot \mid \btheta^\prime).$ \Comment{Simulate a pseudo-observation.}

        \If {$\rho(\bu, \by) \leq \epsilon$}
        \State $\btheta^{(i)} \gets \btheta^\prime$ \Comment{Accept the proposed sample.}
        \State $i \gets i + 1$
        \EndIf
        \EndWhile

        \Output $\text{Accepted samples } \left\{ \btheta^{(1)}, \ldots, \btheta^{(M)} \right\}.$
    \end{algorithmic}
\end{algorithm}

ABC rejection iteratively samples parameters $\btheta^\prime$ from the prior, plugs them into the likelihood $p(\cdot \mid \btheta^\prime)$, and simulates pseudo-observations $\bu$. These are then compared to the true measurement $\by$ using the metric $\rho$. If the proposed parameter $\btheta^\prime$ gave rise to a pseudo-observation similar enough to the true $\by$ (i.e., $\bu \in \A_{\epsilon, \by}$), the parameter is kept under the assumption that the true data are likely under $\btheta^\prime$. The ABC approximation is then given in terms of the accepted samples $\btheta^{(1)}, \ldots, \btheta^{(M)}$ as the empirical distribution
\begin{equation*}
p(\btheta \mid \by) \approx \frac{1}{M} \sum_{i=1}^M \delta_{\btheta^{(i)}}(\btheta).
\end{equation*}

Setting a low value of $\epsilon$ increases the approximation accuracy, at the cost of increased rejection rate. On the other hand, setting $\epsilon$ too large causes the algorithm to accept more often, but leads to simulating pseudo-measurements dissimilar to $\by$ and, in turn, incorrect $\btheta^{(i)}$. Setting a suitable value of $\epsilon$ is therefore the main difficulty when using ABC. Several approaches are discussed by \cite{jasra-filtering, jasra-time-series}. One particular way \citep{dedecius} is used in \autoref{sec:abc-ssm} in the context of SSMs.

There are many improvement to the basic ABC of \autoref{alg:abc-rejection}, discussed for instance by \cite{abc-recent}. In particular, more sophisticated sampling approaches relying again on MCMC are described. This is not an issue relevant to the SSM framework, as the samples are generated in a different fashion, given in the next section.

\section{ABC in SSMs} \label{sec:abc-ssm}
Next, we describe how exactly is the ABC methodology applied in the context of SSMs.

\autoref{sec:abc-general} states that the typical use case of ABC arises in cases where we have knowledge about the data-generating process, but are unable to evaluate the probability of such data. In the context of SSMs, this translates into knowing how the observed values $\by_t$ have been generated from the latent states $\bx_t$, but being unable to evaluate the density $\obs_t(\by_t \mid \bx_t, \btheta)$. This prevents us from calculating the importance weights $w_t$ through the particle filter, which relies on the availability of $\obs_t(\by_t \mid \bx_t, \btheta)$.

\cite{tina-toni} and \cite{jasra-filtering} describe how a filter could be constructed using the ABC approximation. Additionally, \cite{jasra-time-series} applies this filter in the context of SSMs. We first discuss the construction of this filter. Afterwards, we address a particular limitation of this approach through kernel functions.

\paragraph{Filter construction through ABC}
In SSMs, the dimensionality of the observation space is typically low; the observations are often scalar quantities. It is then not necessary to consider any summary statistics.

\cite{jasra-filtering} consider a modification of the particle filter (\autoref{alg:particle-filter}) which simulates pseudo-observations according to the observation model, and calculates the importance weights based on their closeness to the true measurements. The pseudocode is given in \autoref{alg:abc-filter}.

\begin{algorithm}[ht]
    \caption{ABC-based filter}
    \label{alg:abc-filter}
    \begin{algorithmic}[1]
        \Input $\text{Number of particles } N,\ \text{current parameter value } \btheta, \text{maximum distance } \epsilon,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Sample } \bx_0^{(i)} \sim \sprior(\bx_0 \mid \btheta), \quad i = 1, \ldots, N.$ \Comment{Initialize $N$ particles.}

        \State $w_0^{(i)} \gets \frac{1}{N}, \quad i = 1, \ldots, N.$ \Comment{Initialize uniform weights.}

        \For{$t = 1\ \mathbf{to}\ T$}
        \State $\text{Sample } \bx_t^{(i)} \sim \trans_t(\bx_t \mid \bx_{t-1}^{(i)}, \btheta), \quad i = 1, \ldots, N.$ \Comment{Sample $N$ new particles.}

        \State $\text{Simulate } \bu_t^{(i)} \text{ from } \obs_t(\bu_t \mid \bx_t^{(i)}, \btheta), \quad i = 1, \ldots, N.$ \Comment{Simulate $N$ pseudo-observations.}

        \State $\text{Set } w_t^{(i)} \propto \I_{\A_{\epsilon, \by_t}}(\bu_t^{(i)}) w_{t-1}^{(i)}, \quad i = 1, \ldots, N.$

        \State $\text{Resample } \bx_t^{(i)} \text{ and reset } w_t^{(i)} \text{ using \autoref{alg:resampling}}, \quad i = 1, \ldots, N.$
        \EndFor
    \end{algorithmic}
\end{algorithm}

The algorithm proceeds similarly to \autoref{alg:particle-filter} except for the way the weights are computed. Instead of evaluating the unavailable density $\obs_t(\by_t \mid \bx_t, \btheta)$ at the true observation $\by_t$, a pseudo-observation $\bu_t$ is simulated. The weight is then set to a non-zero value if ${\bu_t \in \A_{\epsilon, \by_t}}$, and 0 otherwise. It may seem that the weights for the same particle $i$ necessarily collapse to 0 after a number of time steps due to the recursive multiplication in step 6. However, step 7 resets the weights uniformly after resampling, so such collapse does not occur.

Analogously to \autoref{sec:particle-filter-estimate}, the weights are then used to approximate the likelihood $p(\by_{1:T} \mid \btheta)$. According to \cite{jasra-time-series}, the estimate is given by
\begin{equation} \label{eq:abc-likelihood}
\widehat{\aux} = \prod_{t=1}^T \frac{1}{N} \sum_{i=1}^N \frac{w_t^{(i)}}{\int_{\A_{\epsilon, \by_t}} \dx{\bu}}.
\end{equation}
The integral in the denominator essentially normalizes the weights $w_t^{(i)}$ to be equal to the probability density of $\mathcal{U}(\by_t; \epsilon)$, the uniform distribution in a sphere centered at $\by_t$ with radius $\epsilon$ given in terms of the metric $\rho$.

\paragraph{Bias}
The use of this ABC filter introduces bias to the parameter inference. Recall that in \autoref{sec:particle-filter-estimate}, we required $\widehat{\aux}$ to be an unbiased estimator of $p(\by_{1:T} \mid \btheta)$. This was the case when the weights were calculated according to $w_t^{(i)} \propto \obs_t(\by_t \mid \bx_t) w_{t-1}^{(i)}$. This unbiasedness is not preserved here, since $\widehat{\aux}$ estimates
\begin{equation*}
\begin{split}
p^\epsilon(\by_{1:T} \mid \btheta) &= \int p^\epsilon(\bx_{0:T}, \by_{1:T} \mid \btheta) \; \dx{\bx_{0:T}} \\
&= \int \sprior(\bx_0 \mid \btheta) \prod_{t=1}^T \trans_t(\bx_t \mid \bx_{t-1}, \btheta) \obs_t^\epsilon(\by_t \mid \bx_t, \btheta) \; \dx{\bx_{0:T}}
\end{split}
\end{equation*}
(compare the inside of the integral with \eqref{eq:factorization}), as noted by \cite{jasra-time-series}. Here the approximate observation density is given by the ABC form
\begin{equation*}
\obs_t^\epsilon(\by_t \mid \bx_t, \btheta) = \frac{\int \I_{\A_{\epsilon, \by_t}}(\bu) \obs_t(\bu \mid \bx_t, \btheta) \; \dx{\bu}}{\int_{\A_{\epsilon, \by_t}} \dx{\bu}},
\end{equation*}
similarly to \eqref{eq:abc-integral}. This essentially means that by plugging \eqref{eq:abc-likelihood} into the Metropolis-Hastings acceptance ratio, the limiting distribution of the underlying Markov chain becomes $p^\epsilon(\btheta \mid \by_{1:T}) \propto p^\epsilon(\by_{1:T} \mid \btheta) \pprior(\btheta)$, instead of the correct $p(\btheta \mid \by_{1:T})$. In general, this bias cannot be dealt with, and is a price to pay for using the incorrect observation model.

An interesting way to address this deficiency has been proposed by \cite{noisy-abc1} and by \cite{noisy-abc2}. The authors note that one uses data $\by_{1:T}$ assumed to have been generated according to \eqref{eq:factorization}, $p(\by_{1:T} \mid \btheta)$, but fitting the ABC approximation $p^\epsilon(\by_{1:T} \mid \btheta)$. In an attempt to bring the data closer the model being really fitted, the authors use a sequence of perturbed observations $\bm{z}_t = \by_t + \bm{v}$, $\bm{v} \sim \mathcal{U}(\bm{0}; \epsilon)$ which denotes the uniform distribution in a sphere given by $\rho$, with radius $\epsilon$ and centered at the origin. It is proved that if $\btheta$ is estimated according to maximum likelihood, the estimate is consistent when using the perturbed sequence $\bm{z}_{1:T}$. This approach is called the Noisy ABC.

\paragraph{Use of kernel functions}
A limitation of \autoref{alg:abc-rejection} and \autoref{alg:abc-filter} lies in the use of the indicator function $\I_{\A_{\epsilon, \by}}$. There are two problems:
\begin{enumerate}
    \item A practical one; it may happen that no samples are accepted and the output is null in the case of \autoref{alg:abc-rejection}, or too many weights become zero in the case of \autoref{alg:abc-filter} and the filter collapses.
    \item A more fundamental one, the simulated pseudo-observations $\bu_t$ are all assigned equal weights, regardless of how far they lie from the true measurement $\by_t$. Intuitively, a pseudo-observation closer to the true $\by_t$ should be assigned a higher weight than one which is further away.
\end{enumerate}
Both issues can be mitigated by considering a general kernel function in place of the indicator $\I_{\A_{\epsilon, \by}}$. Let a kernel of width $\epsilon$ centered at $\by$ and evaluated at $\bu$ be denoted by $\kappa(\bu; \by, \epsilon)$. In machine learning, kernel functions are often taken \emph{proportional} to some symmetric probability density function \citep{elements}. However, as we aim to replace the indicator function $\I_{\A_{\epsilon, \by}}$ by such kernel, we require the kernel to be properly normalized (integrate to unity) to mirror the normalization in \eqref{eq:abc-likelihood}.

With the kernel function, we can write $w_t^{(i)} \propto \kappa(\bu_t; \by_t, \epsilon) w_{t-1}^{(i)}$. The likelihood estimate \eqref{eq:abc-likelihood} becomes
\begin{equation} \label{eq:abc-likelihood-kernel}
\widehat{\aux} = \prod_{t=1}^T \frac{1}{N} \sum_{i=1}^N w_t^{(i)}
\end{equation}
due to the kernel being normalized. This way, the weights are no longer uniform but reflect the distance of $\bu_t$ from $\by_t$. There is also no risk of the filter collapsing due to majority of the weights becoming zero.

Introducing the kernel function to the weights computation is in principle similar to using importance sampling rather than simple rejection sampling \citep{information-theory}. Instead of accepting/rejecting the generated samples based on whether they match some criterion, they are all accepted and weighted according to \emph{how well} they match it.

With the kernel functions in mind, we describe a procedure for automatic tuning of the kernel width $\epsilon$, which has been ignored so far. The adopted method has been derived in a one-dimensional setting, i.e., $\bu, \by \in \R$. We correspondingly denote $\by$ by $y$ and $\bu$ by $u$. When considering multivariate observations, the kernels are applied coordinate-wise (assuming independence between the components of $\by$ or $\bu$, respectively).

Before describing the kernel tuning procedure, we give several examples of commonly used kernel functions and comment on their usage. The kernels are shown in \autoref{fig:kernels}.
\begin{enumerate}
    \item \textbf{Gaussian kernel} The Gaussian kernel takes the form
    \begin{equation*}
    \kappa(u; y, \epsilon) = \frac{1}{\sqrt{2 \pi \epsilon^2}} \exp \left\{-\frac{\left(u - y\right)^2}{2 \epsilon^2}\right\}.
    \end{equation*}
    It is one of the most-commonly used kernel functions.
    \item \textbf{Cauchy  kernel} The Cauchy kernel takes the form
    \begin{equation*}
    \kappa(u; y, \epsilon) = \frac{1}{\pi \epsilon \left[ 1 + \left(\frac{u - y}{\epsilon}\right)^2 \right]}.
    \end{equation*}
    As opposed to the Gaussian distribution, the Cauchy distribution has heavier tails, which make it suitable for situations with potentially distant observations. This kernel typically assigns non-trivial probability even to distant pseudo-observations, preventing the filter from collapsing under outliers.

    \item \textbf{Uniform kernel} The uniform kernel takes the form
    \begin{equation*}
    \kappa(u; y, \epsilon) = \begin{cases}
    \frac{1}{2 \epsilon}, & y - \epsilon < u < y + \epsilon; \\
    0, & \text{otherwise}.
    \end{cases}
    \end{equation*}
    Using this kernel, we recover the standard ABC which accepts $u$ if $u \in \A_{\epsilon, y} = \left\{u : \left| u - y \right| < \epsilon \right\}$. If we are in a situation with multivariate observations $\by_t$ and apply the uniform kernel coordinate-wise, the set of accepted samples coincides with the standard ABC using $\rho(\bu, \by) = \max_{i=1}^{d_y} \left|u_i - y_i\right|$, the $L_\infty$ distance.
\end{enumerate}

\begin{figure}[ht]
    \centering
    \includegraphics[width=\linewidth]{kernels}
    \caption{The Gaussian, Cauchy and uniform kernel functions centered at $y = 0$ with width $\epsilon = 3$.}
    \label{fig:kernels}
\end{figure}

The Noisy ABC procedure described above is then naturally generalized by perturbing the observations by samples from a given kernel, instead of the uniform distribution in a ball.

\paragraph{Kernel width tuning}
In this paragraph, we address the issue of tuning the kernel width $\epsilon$. Since the previous section has shown that the indicator function $\I_{\A_{\epsilon, \by}}$ is recovered by a particular kernel, the method also applies to the standard ABC formulation.

A careful setting of $\epsilon$ is necessary. When the kernel width is too low, most of the $N$ generated pseudo-observations are assigned with low probabilities, and the importance weights are close to zero. On the other hand, when the width is too high, even outlying pseudo-observations are assigned a non-trivial probability and shift the filter to incorrect values. In addition, the kernel becomes flat and close to the uniform variant. A manual setting of $\epsilon$ is thus a non-obvious task without any guidelines in the data. Additionally, the width should somehow reflect the filter evolution over time, since all observations $\by_t$ are different and may require different kernel widths.

In this thesis, we adopt a procedure described by \cite{dedecius}, which is briefly reviewed below. More details can be found in the original paper.

The method is based on the idea that the true observation model $\obs_t(\by_t \mid \bx_t, \btheta)$ should cover a given number of generated pseudo-observations $\bu_t^{(i)}, i = 1, \ldots, N$ by a $100p\%$ high probability region ($p$-HPR), where $p \in \left(0, 1\right)$ is a given constant. If this is true, the pseudo-observations can be expected to describe the distribution $\obs_t(\by_t \mid \bx_t, \btheta)$ sufficiently-well. As this distribution is not known, it is approximated by the kernel $\kappa$ evaluated at $\bu_t^{(i)}, i = 1, \ldots, N$.

As given in \eqref{eq:abc-likelihood-kernel}, the kernel is evaluated at each pseudo-observation $\bu_t^{(i)}, i = 1, \ldots, N$ while centered at $\by_t$. We then need to tune the width at time $t$, denoted $\epsilon_t$, so that a given fraction $\frac{\alpha}{N}$ of the pseudo-observations is covered by the $p$-HPR of the kernel. For the procedure to work, the kernel function $\kappa$ must be invariant under translation and scaling, i.e., belong to the location-scale family of distributions. Many popular kernels including the three discussed above, belong to this family.

The tuning procedure involves two steps:
\begin{enumerate}
    \item Identify $u_t^{[\alpha]}$, the $\alpha$th closest pseudo-observation to $y_t$.
    \item Center the kernel $\kappa$ at $y_t$ and set the width $\epsilon_t$ so that
    \begin{equation} \label{eq:kernel-tuning-integral}
    \left| \int_{y_t}^{u_t^{[\alpha]}} \kappa(u_t; y_t, \epsilon_t) \; \dx{u_t} \right| = \frac{p}{2},
    \end{equation}
    meaning that $u_t^{[\alpha]}$ lies at the boundary of the $p$-HPR of $\kappa(\cdot; y_t, \epsilon_t)$.
\end{enumerate}
These two steps are visualized in \autoref{fig:kernel-tuning}. In the case of multidimensional $\by_t$ and $\bu_t$, this procedure is performed coordinate-wise.

\begin{figure}[ht]
    \centering
    \includegraphics[width=\linewidth]{kernel_tuning}
    \caption{Visualization of the kernel tuning procedure. In this picture, ${\alpha = 2}$, ${p = 0.95}$, and the kernel is Gaussian. Plotted is the kernel along with a number of pseudo-observations $u_t^{(i)}$ and the true measurement $y_t$. Equation \eqref{eq:kernel-tuning-integral} states that the shaded area has volume $p/2 = 0.475$.}
    \label{fig:kernel-tuning}
\end{figure}

The meaning of equation \eqref{eq:kernel-tuning-integral} is that $u_t^{[\alpha]}$ is either the $\frac{1-p}{2}$-quantile or the $\frac{1+p}{2}$-quantile of $\kappa(\cdot; y_t, \epsilon_t)$, depending on whether $u_t^{[\alpha]} \leq y_t$ or $u_t^{[\alpha]} \geq y_t$. If we restrict ourselves to symmetric kernels, we may get rid of this case division by exploiting kernel symmetry.

Let $F$ denote the cumulative distribution function of the kernel $\kappa(\cdot; 0, 1)$ centered at 0 with width $\epsilon = 1$. Let its quantile function be denoted by $F^{-1}$. From $\kappa$ belonging to the location-scale family, we get that the quantile function of a general kernel $\kappa(\cdot; y_t, \epsilon_t)$ is
\begin{equation} \label{eq:location-scale-quantile}
Q(\beta) = y_t + \epsilon_t F^{-1}(\beta), \quad \beta \in \left(0, 1\right).
\end{equation}
As \eqref{eq:kernel-tuning-integral} and the assumed kernel symmetry require $\left| u_t^{[\alpha]} \right|$ to be the $\frac{1+p}{2}$-quantile of $\kappa(\cdot; y_t, \epsilon_t)$, we can substitute $u_t^{[\alpha]}$ for $Q(\beta)$ in \eqref{eq:location-scale-quantile} and solve for $\epsilon_t$, obtaining
\begin{equation} \label{eq:kernel-tuning}
\epsilon_t = \frac{\left| u_t^{[\alpha]} - y_t \right|}{F^{-1}(\frac{1+p}{2})},
\end{equation}
where the absolute value comes from the kernel being symmetric, so it is irrelevant whether we consider pseudo-observations lower or greater than the true observation $y_t$. The quantile function $F^{-1}$ is uniquely associated with each kernel, and the only free parameters are $\alpha$ and $p$.

\section{Likelihood estimate through ABC} \label{sec:abcmh}
The main contribution of this thesis is the utilization of the ABC framework to make inference possible even in SSMs with an unknown observation model. We have already derived all the necessary elements needed to state our main result. It remains to put them together by formalizing the entire inference process using ABC as a likelihood estimator.

\autoref{alg:abc-filter-complete} presents a modification of the particle filter which uses the ABC approximation as a surrogate for the unknown observation model. Compared to the particle filter, our formulation is applicable even when the observation model $\obs_t(\by_t \mid \bx_t, \btheta)$ is not given as a probability density. Unlike the basic ABC filter described in \autoref{alg:abc-filter}, our variant employs kernel functions to measure observation similarity, reflecting the fact that the closer a pseudo-measurement is to the true observation, the higher weight it should be assigned. In addition, we account for automatic tuning of the kernel widths by adapting them so that they cover a sufficient number of the simulated pseudo-measurements.

\begin{algorithm}[ht]
    \caption{ABC-based filter with automatic kernel tuning}
    \label{alg:abc-filter-complete}
    \begin{algorithmic}[1]
        \Input $\text{Number of particles } N,\ \text{current parameter value } \btheta, \text{ HPR } p, \newline \text{number of covered pseudo-observations } \alpha,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Sample } \bx_0^{(i)} \sim \sprior(\cdot \mid \btheta), \quad i = 1, \ldots, N.$ \Comment{Initialize $N$ particles.}

        \State $w_0^{(i)} \gets \frac{1}{N}, \quad i = 1, \ldots, N.$ \Comment{Initialize uniform weights.}

        \For{$t = 1\ \mathbf{to}\ T$}
        \State $\text{Sample } \bx_t^{(i)} \sim \trans_t(\bx_t \mid \bx_{t-1}^{(i)}, \btheta), \quad i = 1, \ldots, N.$ \Comment{Sample $N$ new particles.}

        \State $\text{Simulate } \bu_t^{(i)} \text{ from } \obs_t(\cdot \mid \bx_t^{(i)}), \quad i = 1, \ldots, N.$ \Comment{Simulate $N$ pseudo-observations.}

        \State $\text{Identify } \bu_t^{[\alpha]}.$ \Comment{Find the $\alpha$th closest pseudo-observation to $\by_t$.}

        \State $\epsilon_t \gets \frac{\left| u_t^{[\alpha]} - y_t \right|}{F^{-1}(\frac{1+p}{2})}$ \Comment{Set the kernel width at time $t$ according to \eqref{eq:kernel-tuning}.}

        \State $\text{Set } w_t^{(i)} \propto \kappa(\bu_t^{(i)}; \by_t, \epsilon_t) w_{t-1}^{(i)}, \quad i = 1, \ldots, N.$

        \State $\text{Resample } \bx_t^{(i)} \text{ and reset } w_t^{(i)} \text{ using \autoref{alg:resampling}}, \quad i = 1, \ldots, N.$
        \EndFor
    \end{algorithmic}
\end{algorithm}

Finally, \autoref{alg:marginal-metropolis-hastings-abc} reformulates the marginal Metropolis-Hastings according to the ABC methodology. Under the new formulation, the estimator $\widehat{\aux}$ of $p(\by_{1:T} \mid \btheta)$ is constructed by evaluating \eqref{eq:abc-likelihood-kernel} on a set of importance weights calculated by \autoref{alg:abc-filter-complete}. The result is used to compute the acceptance probability which again controls whether a proposed $\btheta^\prime$ and the corresponding $\widehat{\aux}^\prime$ are accepted or not.

\begin{algorithm}[ht]
    \caption{Marginal Metropolis-Hastings with ABC filter}
    \label{alg:marginal-metropolis-hastings-abc}
    \begin{algorithmic}[1]
        \Input $\text{Number of samples } M,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Initialize } \btheta^{(0)}.$
        \State $\text{Run \autoref{alg:abc-filter-complete} with } \btheta^{(0)} \text{ to obtain the weights } w_{0,t}^{(i)}, \quad t = 1, \ldots, T,\ i = 1, \ldots, N.$
        \State $\text{Calculate } \widehat{\aux}^{(0)} \text{ according to \eqref{eq:abc-likelihood-kernel} using } w_{0,t}^{(i)}.$

        \For{$m = 1\ \mathbf{to}\ M$}
        \State $\text{Sample } \btheta^\prime \sim \prop(\cdot \mid \btheta^{(m-1)}).$
        \State $\text{Run \autoref{alg:abc-filter-complete} with } \btheta^\prime \text{ to obtain the weights } w_{m,t}^{(i)}, \quad t = 1, \ldots, T, \ i = 1, \ldots, N.$
        \State $\text{Calculate } \widehat{\aux}^\prime \text{ according to \eqref{eq:abc-likelihood-kernel} using } w_{m,t}^{(i)}.$
        \State $\text{Calculate the aceptance probability } $ \begin{equation*} \label{eq:acceptance-probability-tractable-abc}
        \alpha = \min \left\{1, \frac{\widehat{\aux}^\prime \pprior(\btheta^\prime)}{\widehat{\aux}^{(m-1)} \pprior(\btheta^{(m-1)})} \frac{\prop(\btheta^{(m-1)} \mid \btheta^\prime)}{\prop(\btheta^\prime \mid \btheta^{(m-1)})} \right\}.
        \end{equation*}
        \State $\text{Sample } u \sim \mathcal{U}(0,1).$
        \If {$u \leq \alpha$}
        \State $\left( \btheta^{(m)}, \widehat{\aux}^{(m)} \right) \gets \left( \btheta^\prime, \widehat{\aux}^\prime \right)$ \Comment{With probability $\alpha$, accept the proposed sample.}
        \Else
        \State $\left( \btheta^{(m)}, \widehat{\aux}^{(m)} \right) \gets \left( \btheta^{(m-1)}, \widehat{\aux}^{(m-1)} \right)$ \Comment{With probability $1 - \alpha$, reject the proposed sample.}
        \EndIf
        \EndFor

        \Output $\left\{ \btheta^{(1)}, \ldots, \btheta^{(M)} \right\}$
    \end{algorithmic}
\end{algorithm}