\section{Methodology}\label{sec:metodo}

\subsection{Forward problem}

Let $\mathbf{d}^{o}$ be the observed data vector, whose $i$th element $d^{o}_{i}$, $i = 1, \dots, N$, is the total-field anomaly produced by a 3-D source (Fig. \ref{fig:obs}a) at the point $(x_{i}, y_{i}, z_{i})$ of a Cartesian coordinate system with $x$-, $y$- and $z$- axes pointing to north, east and down, respectively. We assume that the direction of the total magnetization vector of the source is constant and known. We approximate the volume of the source by a set of $L$ vertically juxtaposed 3-D prisms (Fig. \ref{fig:obs}b) by following the same approach of \cite{oliveirajr-etal2011} and \cite{oliveirajr-barbosa2013}. The depth to the top of the shallowest prism is defined by $z_{0}$, and $m_{0}$ is the constant total-magnetization intensity of all prisms. The horizontal cross-section of each prism (Fig. \ref{fig:prism_parameters})
is described by a polygon with a fixed number $V$ of vertices equi-angularly spaced from $0^{\circ}$ to $360^{\circ}$, which are described in polar coordinates referred to an internal origin $O^{k}$. The radii of the vertices ($r^{k}_{j}$, $j=1,\dots , V$, $k=1,\dots ,L$), the horizontal coordinates ($x_{0}^{k}$ and $y_{0}^{k}$, $k=1,\dots ,L$) of the origins $O^{k}$, $k=1,\dots ,L$, and the thickness $dz$ of the $L$ vertically stacked prisms (Fig. \ref{fig:obs}b) are arranged in a $M \times 1$ parameter vector $\mathbf{p}$, $M = L (V + 2) + 1$, given by
\begin{equation}
\mathbf{p} = \left[ \begin{array}{@{}*{12}{c}@{}}
{\mathbf{r}^{1}}^{\mathsf{T}} & x_{0}^{1} & y_{0}^{1} & \dots & {\mathbf{r}^{L}}^{\mathsf{T}} & x_{0}^{L} & y_{0}^{L} & dz \\
\end{array} \right]^{\mathsf{T}} \: ,
\label{eq:p-vector}
\end{equation}
where ``$^{\mathsf{T}}$" denotes transposition and $\mathbf{r}^{k}$ is a $V \times 1$ vector containing the radii $r^{k}_{j}$
of the $k$th prism.
Let $\mathbf{d} (\mathbf{p}, m_{0}, z_{0})$ be the predicted data vector,
whose $i$th element
\begin{equation}
d_{i} (\mathbf{p}, m_{0}, z_{0}) \equiv m_{0} \: \sum\limits_{k=1}^{L} f_{i}(\mathbf{r}^{k}, x_{0}^{k}, y_{0}^{k}, dz, z_{0}), \quad i = 1, \dots, N \: ,
\label{eq:predicted-data-i}
\end{equation}
is the total-field anomaly produced by the ensemble of $L$ prisms at the $i$th observation point ($x_{i}, y_{i}, z_{i}$). In Eq. \ref{eq:predicted-data-i}, $f_{i}(\mathbf{r}^{k}, x_{0}^{k}, y_{0}^{k}, dz, z_{0})$ is the total-field anomaly produced, at the observation point ($x_{i}, y_{i}, z_{i}$), by the $k$th prism with unitary magnetization intensity and depth to the top $z_{1}^{k} = z_{0} + (k-1)dz$. We calculate $d_{i} (\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:predicted-data-i}) by using the Python package Fatiando a Terra \cite[]{uieda-etal2013}, which implements the formulas proposed by \cite{plouff1976}.

\subsection{Inverse problem for estimating $\mathbf{p}$}

Given the depth to the top of shallowest prism $z_{0}$ and the total-magnetization
intensity $m_{0}$ of all prisms, we solve a constrained nonlinear problem to estimate the parameter vector $\mathbf{p}$ (Eq. \ref{eq:p-vector}) by minimizing
the goal function
\begin{equation}
\Gamma (\mathbf{p}, m_{0}, z_{0}) = \phi (\mathbf{p}, m_{0}, z_{0}) +
\sum\limits^{7}_{\ell =1} \alpha_{\ell} \, \varphi_{\ell}(\mathbf{p}) \: ,
\label{eq:gamma}
\end{equation}
subject to the inequality constraints
\begin{equation}
p_{l}^{min} < p_{l} < p_{l}^{max}, \quad l = 1, \dots, M \: .
\label{eq:inequality-constraint}
\end{equation}
The first term in the right side of Eq. \ref{eq:gamma} is the data-misfit
function given by
\begin{equation}\label{eq:misfit}
\phi (\mathbf{p}, m_{0}, z_{0}) = \frac{1}{N} \| \mathbf{d}^{o} -
\mathbf{d}(\mathbf{p}, m_{0}, z_{0}) \|_{2}^{2} \: ,
\end{equation}
which represents the normalized squared Euclidean norm of the difference between the
observed $\mathbf{d}^{o}$ and predicted $\mathbf{d}(\mathbf{p}, m_{0}, z_{0})$ data
vector, whose $i$th element is the predicted data $d_{i} (\mathbf{p}, m_{0}, z_{0})$
(Eq. \ref{eq:predicted-data-i}).
The second term in the right side of Eq. \ref{eq:gamma} represents the
weighted sum of the seven constraint functions $\varphi_{\ell}(\mathbf{p}), \:
\ell = 1, \dots, 7$, described in the following section \textit{Constraint functions}.
In Eq. \ref{eq:gamma}, $\alpha_{\ell}$ is a positive number representing
the weight of the $\ell$th constraint function $\varphi_{\ell}(\mathbf{p})$.
In the inequality constraints (Eq. \ref{eq:inequality-constraint}),
$p_{l}^{min}$ and $p_{l}^{max}$ are, respectively, the lower and upper limits for
the $l$th element $p_{l}$ of the parameter vector $\mathbf{p}$
(Eq. \ref{eq:p-vector}).
These limits are defined by the interpreter based on the knowledge about the
horizontal and total depth extensions of the magnetic source.
Details about how we set the weights $\alpha_{\ell}$ and the limits in the
inequality constraints are presented in the section
\textit{Computational procedures} later in this article.

To solve our nonlinear inverse problem we use a gradient-based method and,
consequently, we need to define an initial approximation $\hat{\mathbf{p}}_{(0)}$
for the parameter vector $ \mathbf{p} $ (Eq. \ref{eq:p-vector}).
Then our method iteratively updates this initial approximation to obtain
an estimated parameter vector $\hat{\mathbf{p}}_{(f)}$ minimizing the goal function
$\Gamma (\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:gamma}),
for given values of total-magnetization intensity $m_{0}$ and depth to the top
of the shallowest prism $z_{0}$.
Here, we use the superscript hat ``~$\hat{}$~" to denote initial approximation or
estimated parameter vector.

Since we are using a gradient-based method, we need to define the
gradient vector $\boldsymbol{\nabla}\Gamma(\mathbf{p})$ and Hessian matrix
$\mathbf{H}(\mathbf{p})$ of the goal function $\Gamma(\mathbf{p}, m_{0}, z_{0})$
(Eq. \ref{eq:gamma}), both of them computed with respect to the parameter vector
$\mathbf{p}$. That is why we define them by omitting the parameters $m_{0}$ and
$z_{0}$. They are given by:
\begin{equation}\label{eq:gamma_gradient}
\boldsymbol{\nabla}\Gamma (\mathbf{p}) = \boldsymbol{\nabla}\phi (\mathbf{p}) +
\sum\limits^{7}_{\ell =1} \alpha_{\ell} \, \boldsymbol{\nabla}\varphi_{\ell}(\mathbf{p})
\end{equation}
and
\begin{equation}\label{eq:gamma_hessian}
\mathbf{H} (\mathbf{p}) = \mathbf{H}_\phi (\mathbf{p}) + \sum\limits^{7}_{\ell =1} \alpha_{\ell} \, \mathbf{H}_\ell \: ,
\end{equation}
where the gradient vector and the Hessian matrix of the misfit function
$\phi(\mathbf{p})$ (Eq. \ref{eq:gamma}) are respectively given by:
\begin{equation}\label{eq:phi_gradient}
\boldsymbol{\nabla} \phi(\mathbf{p}) = - \frac{2}{N}\mathbf{G}(\mathbf{p})^{\mathsf{T}}[\mathbf{d}^o -
\mathbf{d}(\mathbf{p}, m_{0}, z_{0})]
\end{equation}
and
\begin{equation}\label{eq:phi_hessian}
\mathbf{H}_{\phi}(\mathbf{p}) = \frac{2}{N}\mathbf{G}(\mathbf{p})^{\mathsf{T}}\mathbf{G}(\mathbf{p}) \: .
\end{equation}
In Eqs \ref{eq:gamma_gradient} and \ref{eq:gamma_hessian}, the terms
$\boldsymbol{\nabla} \varphi_{\ell}(\mathbf{p})$ and $\mathbf{H}_{\ell}$,
$\ell = 1, \dots, 7$, are the gradient vectors and Hessian matrices of the constraint functions, respectively. In Eqs \ref{eq:phi_gradient} and \ref{eq:phi_hessian},
$\mathbf{G}(\mathbf{p})$ is an $N \times M$ matrix whose element $il$ is the derivative of the predicted data $d_{i}(\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:predicted-data-i}) with respect to the $l$th element $p_{l}$ of
the parameter vector $\mathbf{p}$ (Eq. \ref{eq:p-vector}), $l = 1, \dots, M$.
Details about the constraint functions $\varphi_\ell(\mathbf{p})$, $\ell = 1, \dots, 7$, as well as the numerical procedure to solve this nonlinear inverse problem are given in the following sections.

\subsection{Constraint functions}\label{sec:constraints}

To explain the constraint functions $\varphi_{\ell}(\mathbf{p})$ (Eq. \ref{eq:gamma}), $\ell = 1, \dots, 7$, used here to obtain stable solutions and introduce prior information about the magnetic source, we have organized them into the following three groups.

\subsubsection{Smoothness constraints}

This group is formed by variations of the first-order Tikhonov regularization \cite[][ p. 103]{aster-etal2019} that imposes smoothness on the radii $r_{j}^{k}$ and the Cartesian coordinates $x_{0}^{k}$ and $y_{0}^{k}$ of the origin $O^{k}$, $j = 1, \dots, V$, $k = 1, \dots, L$, defining the horizontal section of each prism (Fig.\ref{fig:obs}b).
They were proposed by \cite{oliveirajr-etal2011} and \cite{oliveirajr-barbosa2013} and play a very important role in introducing prior information about the shape of the source.

The first constraint of this group is the \textit{smoothness constraint on the adjacent radii defining the horizontal section of each prism}. This constraint imposes that adjacent radii $r_{j}^{k}$ and $r_{j+1}^{k}$ within each prism must be close to each other. It forces the estimated prism to be approximately cylindrical. Mathematically, the constraint is given by \begin{equation}\label{eq:phi1}
\begin{split}
\varphi_{1}(\mathbf{p}) &= \sum\limits^{L}_{k=1}\left[\left(r^{k}_{V}-r^{k}_{1}\right)^2 + \sum\limits^{V-1}_{j=1}\left(r^{k}_{j}-r^{k}_{j+1}\right)^2\right]\\
 &= \mathbf{p}^{\mathsf{T}} \mathbf{R}^{\mathsf{T}}_{1}\mathbf{R}_{1} \mathbf{p} \quad ,
\end{split}
\end{equation}
where
\begin{equation}
\mathbf{R}_{1} =
\begin{bmatrix}
\mathbf{S}_{1} &
\mathbf{0}_{LV \times 1} \\
\end{bmatrix}_{LV \times M} \quad ,
\label{eq:R1-matrix}
\end{equation}
\begin{equation}
\mathbf{S}_{1} =
\mathbf{I}_{L} \otimes
\begin{bmatrix}
\left( \mathbf{I}_{V} - \mathbf{D}_{V}^\mathsf{T} \right) & \mathbf{0}_{V \times 2} \\
\end{bmatrix}_{V \times (V+2)} \quad ,
\label{eq:S1-matrix}
\end{equation}
$ \mathbf{0}_{LV \times 1} $ is an $ LV \times 1 $ vector with null elements,
$\mathbf{I}_{L}$ is the identity matrix of order $L$, ``$\otimes$" denotes the Kronecker product \cite[][ p. 243]{horn_johnson1991}, $\mathbf{0}_{V \times 2}$ is a $V \times 2$ matrix with null elements,
$\mathbf{I}_{V}$ is the identity matrix of order $V$ and $\mathbf{D}_{V}^\mathsf{T}$ is the upshift permutation matrix of order $V$ \cite[][ p. 20]{golub-vanloan2013}. The gradient and Hessian of function $\varphi_{1}(\mathbf{p})$ (Eq. \ref{eq:phi1}) are given by:
\begin{equation}\label{eq:phi1_grad}
\boldsymbol{\nabla}\varphi_{1}(\mathbf{p}) = 2 \mathbf{R}^\mathsf{T}_{1}\mathbf{R}_{1}\mathbf{p} \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi1_hessian}
\mathbf{H}_{1}(\mathbf{p}) = 2\mathbf{R}^\mathsf{T}_{1}\mathbf{R}_{1} \quad .
\end{equation}

The second constraint of this group is the \textit{smoothness constraint on the adjacent radii of the vertically adjacent prisms}, which imposes that adjacent radii $r_{j}^{k}$ and $r_{j}^{k+1}$ within vertically adjacent prisms must be close to each other. This constraint forces the shape of all prisms to be similar to each other
and is given by
\begin{equation}\label{eq:phi2}
\begin{split}
\varphi_{2}(\mathbf{p}) &= \sum\limits^{L-1}_{k=1}\left[\sum\limits^{V}_{j=1}\left(r^{k+1}_{j}-r^{k}_{j}\right)^2\right] \\
&= \mathbf{p}^{\mathsf{T}} \mathbf{R}^{\mathsf{T}}_{2}\mathbf{R}_{2}\mathbf{p}
\end{split} \quad ,
\end{equation}
where
\begin{equation}
\mathbf{R}_{2} =
\begin{bmatrix}
\mathbf{S}_{2} & \mathbf{0}_{(L-1)V \times 1} \\
\end{bmatrix}_{(L-1)V \times M} \quad ,
\label{eq:R2-matrix}
\end{equation}
\begin{equation}
\mathbf{S}_{2} =
\left(
\begin{bmatrix} \mathbf{I}_{L-1} & \mathbf{0}_{(L-1) \times 1} \end{bmatrix} -
\begin{bmatrix} \mathbf{0}_{(L-1) \times 1} & \mathbf{I}_{L-1} \end{bmatrix}
\right) \otimes
\begin{bmatrix} \mathbf{I}_{V} & \mathbf{0}_{V \times 2} \end{bmatrix} \quad ,
\label{eq:S2-matrix}
\end{equation}
$\mathbf{0}_{(L-1)V \times 1}$ is an $(L-1)V \times 1$ vector with null elements,
$\mathbf{0}_{(L-1) \times 1}$ is an $(L-1) \times 1$ vector with null elements and
$\mathbf{I}_{L-1}$ is the identity matrix of order $L-1$. The gradient and Hessian of function $\varphi_{2}(\mathbf{p})$ (Eq. \ref{eq:phi2}) are given by:
\begin{equation}\label{eq:phi2_grad}
\boldsymbol{\nabla}\varphi_{2}(\mathbf{p}) = 2\mathbf{R}^\mathsf{T}_{2}\mathbf{R}_{2}\mathbf{p} \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi2_hessian}
\mathbf{H}_{2}(\mathbf{p}) = 2\mathbf{R}^\mathsf{T}_{2}\mathbf{R}_{2} \quad .
\end{equation}

The last constraint of this group is the \textit{smoothness constraint on the horizontal position of
the arbitrary origins of the vertically adjacent prisms}. This constraint imposes that the estimated horizontal
Cartesian coordinates $(x_{0}^{k}, y_{0}^{k})$ and $(x_{0}^{k+1}, y_{0}^{k+1})$ of the origins $O^{k}$ and $O^{k+1}$
of adjacent prisms must be close to each other. It forces the centers of the prisms to be vertically aligned. This constraint
is given by
\begin{equation}\label{eq:phi3}
\begin{split}
\varphi_{3}(\mathbf{p}) &= \sum\limits^{L-1}_{k=1}\left[\left(x_{0}^{k+1} - x_{0}^{k}\right)^2 + \left(y_{0}^{k+1} - y_{0}^{k}\right)^2 \right] \\
&= \mathbf{p}^{\mathsf{T}} \mathbf{R}^{\mathsf{T}}_{3}\mathbf{R}_{3}\mathbf{p}
\end{split} \quad ,
\end{equation}
where
\begin{equation}
\mathbf{R}_{3} =
\begin{bmatrix}
\mathbf{S}_{3} & \mathbf{0}_{(L-1)2 \times 1} \\
\end{bmatrix}_{(L-1)2 \times M} \quad ,
\label{eq:R3-matrix}
\end{equation}
\begin{equation}
\mathbf{S}_{3} =
\left(
\begin{bmatrix} \mathbf{I}_{L-1} & \mathbf{0}_{(L-1) \times 1} \end{bmatrix} -
\begin{bmatrix} \mathbf{0}_{(L-1) \times 1} & \mathbf{I}_{L-1} \end{bmatrix}
\right) \otimes
\begin{bmatrix} \mathbf{0}_{2 \times V} & \mathbf{I}_{2} \end{bmatrix} \quad ,
\label{eq:S3-matrix}
\end{equation}
$\mathbf{0}_{(L-1)2 \times 1}$ is an $(L-1)2 \times 1$ vector with null elements,
$\mathbf{0}_{2 \times V}$ is a $2 \times V$ matrix with null elements and
$\mathbf{I}_{2}$ is the identity matrix of order $2$. The gradient and Hessian of function $\varphi_{3}(\mathbf{p})$ (Eq. \ref{eq:phi3}) are given by:
\begin{equation}\label{eq:phi3_grad}
\boldsymbol{\nabla}\varphi_{3}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{3}\mathbf{R}_{3}\mathbf{p} \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi3_hessian}
\mathbf{H}_{3}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{3}\mathbf{R}_{3} \quad .
\end{equation}

\subsubsection{Equality constraints}

This group is formed by two constraints that were proposed by \cite{oliveirajr-etal2011} and \cite{oliveirajr-barbosa2013} by following the same approach proposed \cite{barbosa-etal1997} and \cite{barbosa-1999a}.
They introduce a priori information about the shallowest prism and are suitable for outcropping sources.

The \textit{source’s outcrop constraint} imposes that the horizontal cross-section of the shallowest prism must be close to known outcropping boundary of the geologic source.
The horizontal cross-section of the known outcropping boundary separating the geologic source from the host rock is described by the radii $\tilde{r}_{1}^{0} \dots \tilde{r}_{V}^{0}$. Mathematically, this constraint is given by
\begin{equation}\label{eq:phi4}
\begin{split}
\varphi_{4}(\mathbf{p}) &= \left[ \sum\limits^{V}_{j=1}\left(r^{1}_{j}-\tilde{r}^{0}_{j}\right)^2\right] \\
&= \left(\mathbf{R}_{4} \mathbf{p} - \mathbf{a} \right)^{\mathsf{T}}
\left(\mathbf{R}_{4} \mathbf{p} - \mathbf{a} \right)
\end{split} \quad ,
\end{equation}
where $\mathbf{a}$ is a $V \times 1$ vector containing the radii of the polygon defining the outcropping boundary
\begin{equation}
\mathbf{a} = \left[ \begin{array}{@{}*{12}{c}@{}}
\tilde{r}_{1}^{0} & \dots & \tilde{r}_{V}^{0} \\
\end{array} \right]^{\mathsf{T}} \: ,
\label{eq:a-vector}
\end{equation}
and
\begin{equation}
\mathbf{R}_{4} =
\begin{bmatrix}
\mathbf{I}_{V} & \mathbf{0}_{V \times (M-V)} \\
\end{bmatrix}_{V\times M},
\label{eq:R4-matrix}
\end{equation}
where $\mathbf{I}_{V}$ is the identity matrix of order $V$ and
$\mathbf{0}_{V \times (M-V)}$ is a $V \times (M-V)$ matrix with null elements.
The gradient and Hessian of function $\varphi_{4}(\mathbf{p})$ (Eq. \ref{eq:phi4}) are given by:
\begin{equation}\label{eq:phi4_grad}
\boldsymbol{\nabla}\varphi_{4}(\mathbf{p}) = 2 \mathbf{R}_{4}^{\mathsf{T}}
\left(\mathbf{R}_{4} \mathbf{p} - \mathbf{a} \right) \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi4_hessian}
\mathbf{H}_{4}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{4}\mathbf{R}_{4} \quad .
\end{equation}

The \textit{source's horizontal location constraint} imposes that the horizontal Cartesian coordinates of the origin within
the shallowest prism must be as close as possible to a known outcropping point given by the horizontal Cartesian coordinates $(\tilde{x}_{0}^{0}, \: \tilde{y}_{0}^{0})$.
This constraint is given by
\begin{equation}\label{eq:phi5}
\begin{split}
\varphi_{5}(\mathbf{p}) &= \left[\left(x_{0}^{1} - \tilde{x}_{0}^{0}\right)^2
+ \left(y_{0}^{1} - \tilde{y}_{0}^{0}\right)^2\right] \\
&= \left(\mathbf{R}_{5} \mathbf{p} - \mathbf{b} \right)^{\mathsf{T}}
\left(\mathbf{R}_{5} \mathbf{p} - \mathbf{b}\right)
\end{split} \quad ,
\end{equation}
where $\mathbf{b}$ is a $2 \times 1$ vector containing the horizontal Cartesian coordinates of the outcropping point
\begin{equation}
\mathbf{b} = \left[ \begin{array}{@{}*{12}{c}@{}}
\tilde{x}_{0}^{0} & \tilde{y}_{0}^{0} \\
\end{array} \right]^{\mathsf{T}} \: ,
\label{eq:b-vector}
\end{equation}
and
\begin{equation}
\mathbf{R}_{5} =
\begin{bmatrix}
\mathbf{0}_{2 \times V} & \mathbf{I}_{2} & \mathbf{0}_{2 \times (M-V-2)} \\
\end{bmatrix}_{2 \times M},
\label{eq:R5-matrix}
\end{equation}
where $\mathbf{0}_{2 \times (M-V-2)}$ is a $2 \times (M-V-2)$ matrix
with null elements.
The gradient and Hessian of function $\varphi_{5}(\mathbf{p})$ (Eq. \ref{eq:phi5}) are given by:
\begin{equation}\label{eq:phi5_grad}
\boldsymbol{\nabla}\varphi_{5}(\mathbf{p}) = 2\mathbf{R}_{5}^{\mathsf{T}}
\left(\mathbf{R}_{5} \mathbf{p} - \mathbf{b}\right) \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi5_hessian}
\mathbf{H}_{5}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{5}\mathbf{R}_{5} \quad .
\end{equation}

\subsubsection{Minimum Euclidean norm constraints}

Two constraints use the zeroth-order Tikhonov regularization with the purpose of obtaining stable solutions without introducing prior information about the shape of the source.
However, these two constraints combined with the interpretation model impose
source compactness.

The \textit{Minimum Euclidean norm of the radii} imposes that
all estimated radii within each prism must be close to null values. This constraint was proposed by \cite{oliveirajr-etal2011} and \cite{oliveirajr-barbosa2013} and can be rewritten as follows
\begin{equation}\label{eq:phi6}
\begin{split}
\varphi_{6}(\mathbf{p}) &= \sum\limits^{L}_{k=1}\sum\limits^{V}_{j=1}\left(r_{j}^{k}\right)^2 \\
&= \mathbf{p}^{\mathsf{T}} \mathbf{R}_{6}^{\mathsf{T}} \mathbf{R}_{6} \mathbf{p}
\end{split} \quad ,
\end{equation}
where
\begin{equation}
\mathbf{R}_{6} =
\begin{bmatrix}
\mathbf{S}_{6} & \mathbf{0}_{(M-1) \times 1} \\
\mathbf{0}_{1 \times (M-1)} & 0 \\
\end{bmatrix}_{M\times M} \quad ,
\label{eq:R6-matrix}
\end{equation}
and
\begin{equation}
\mathbf{S}_{6} =
\mathbf{I}_{L} \otimes
\begin{bmatrix}
\mathbf{I}_{V}      & \mathbf{0}_{V \times 2} \\
\mathbf{0}_{2 \times V} & \mathbf{0}_{2 \times 2} \\
\end{bmatrix}_{(V+2) \times (V+2)} \quad ,
\label{eq:S6-matrix}
\end{equation}
where $\mathbf{0}_{2 \times 2}$ is a $2 \times 2$ matrix with null elements,
$\mathbf{0}_{V \times 2}$ is a $V \times 2$ matrix with null elements and
$\mathbf{0}_{2 \times V}$ is a $2 \times V$ matrix with null elements.
The gradient and Hessian of function $\varphi_{6}(\mathbf{p})$ (Eq. \ref{eq:phi6}) are given by:
\begin{equation}\label{eq:phi6_grad}
\boldsymbol{\nabla}\varphi_{6}(\mathbf{p}) = 2 \mathbf{R}_{6}^{\mathsf{T}} \mathbf{R}_{6} \mathbf{p} \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi6_hessian}
\mathbf{H}_{6}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{6}\mathbf{R}_{6} \quad .
\end{equation}

The other constraint, the \textit{Minimum Euclidean norm of the prism thickness}, imposes that the thickness of all prisms must be close to zero. We present this constraint to introduce a priori information about the maximum depth extent of the source which in turn is dependent on the depth to the top of the shallowest prism $z_{0}$. It is given by
\begin{equation}\label{eq:phi7}
\begin{split}
\varphi_{7}(\mathbf{p}) &= dz^2 \\
&= \mathbf{p}^{\mathsf{T}} \mathbf{R}_{7}^{\mathsf{T}} \mathbf{R}_{7} \mathbf{p}
\end{split} \quad ,
\end{equation}
where
\begin{equation}
\mathbf{R}_{7} =
\begin{bmatrix}
\mathbf{0}_{(M-1) \times (M-1)} & \mathbf{0}_{(M-1) \times 1} \\
\mathbf{0}_{1 \times (M-1)} & 1 \\
\end{bmatrix}_{ M \times M } \quad .
\end{equation}
The gradient and Hessian of function $\varphi_{7}(\mathbf{p})$ (Eq. \ref{eq:phi7}) are given by:
\begin{equation}\label{eq:phi7_grad}
\boldsymbol{\nabla}\varphi_{7}(\mathbf{p}) = 2 \mathbf{R}_{7}^{\mathsf{T}} \mathbf{R}_{7} \mathbf{p} \quad ,
\end{equation}
and
\begin{equation}\label{eq:phi7_hessian}
\mathbf{H}_{7}(\mathbf{p}) = 2 \mathbf{R}^{\mathsf{T}}_{7}\mathbf{R}_{7} \quad .
\end{equation}

\subsection{Computational procedures}

In this section, we present the computational procedures
to solve the nonlinear inverse problem for estimating a parameter vector
$\hat{\mathbf{p}}_{(f)}$ that minimizes the goal function
$\Gamma(\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:gamma}), subject to the inequality
constraints (Eq. \ref{eq:inequality-constraint}), for previously defined values of
depth to the top of the shallowest prism $z_{0}$ and total-magnetization intensity
$m_{0}$. We use an iterative gradient-based algorithm for solving the nonlinear
inverse problem.

This section is divided into four parts. The first part describes how to define
the number of prisms $L$, the number of vertices $V$, total-magnetization
direction and thickness $dz$ of all prisms, the initial approximation
$\hat{\mathbf{p}}_{(0)}$ for the parameter vector $ \mathbf{p} $
(Eq. \ref{eq:p-vector}), as well as the upper and lower limits in the inequality
constraints ($p_{l}^{min}$ and $p_{l}^{max}$ in Eq. \ref{eq:inequality-constraint}).
All these variables are previously defined by the interpreter and never updated
in the nonlinear inverse problem.
The second part of this section explains how we define different
tentative values for the depth to the top $z_{0}$ and total-magnetization
intensity $m_{0}$, as well as how we choose their optimum values.
In the third part we present the gradient-based algorithm for solving the nonlinear
inverse problem for a given tentative pair ($m_{0}$, $z_{0}$).
Finally, we describe how to set the weights $ \alpha_{\ell} $
(Eq. \ref{eq:gamma}) in the fourth part of this section.

\subsubsection{Initial approximation $\hat{\mathbf{p}}_{(0)}$ and inequality constraints}

Our initial approximation $\hat{\mathbf{p}}_{(0)}$ is a uniformly-magnetized
cylinder-like body formed by
$L$ prisms, all of them with the same number of vertices $V$. The radii of the
vertices of all prisms ($r^{k}_{j}$, $j=1,\dots , V$, $k=1,\dots ,L$) have the
same constant value, so that the initial approximation approaches a vertical
cylinder.
The number of prisms $L$ and vertices $V$ are set based on the expected complexity
of the magnetic source.
We start by computing the reduction to pole (RTP) anomaly, which has three purposes.
The first one is verifying if the used total-magnetization direction is valid.
The second purpose is defining the upper and lower limits of the inequality
constraints (Eq. \ref{eq:inequality-constraint}).
Finally, the third purpose of the RTP anomaly is defining the radius and horizontal
Cartesian coordinates of the center of the cylinder-like initial approximation.

It is well known that if the source has a uniform magnetization direction,
the RTP anomaly is predominantly positive and decays to zero close to its
horizontal boundaries \cite[e.g.,][p. 331]{blakely1996}.
To compute this transformation, however, the interpreter must use declination and
inclination values close to those defining the true total-magnetization direction
of the source.
Hence, the interpreter can validate the total-magnetization direction of the source
by verifying if the computed RTP anomaly shows predominantly positive values.

By using the computed RTP anomaly, we also define the upper and lower limits
($p_{l}^{min}$ and $p_{l}^{max}$ in Eq. \ref{eq:inequality-constraint}) for
the parameters representing the radii $r^{k}_{j}$ of the vertices and the
horizontal coordinates $x_{0}^{k}$ and $y_{0}^{k}$ of the origins $O^{k}$
of all prisms ($j=1,\dots , V$, $k=1,\dots ,L$). For the parameters representing
the radii $r^{k}_{j}$, the lower limit $p_{l}^{min}$ is close to zero and the
upper limit $p_{l}^{max}$ is approximately defined by the radius of a circular area
encompassing the region where the RTP anomaly is positive and decays to zero.
The lowermost and uppermost $x-$ and $y-$ coordinates of this circular area
are used to define, respectively, the lower and upper limits for the
horizontal coordinates $x_{0}^{k}$ and $y_{0}^{k}$.

Next, we set the same thickness $dz$ for all prisms so that the resulting
total thickness ($L \, dz$) is greater than that we expect for the true source.
The lower and upper limits ($p_{l}^{min}$ and $p_{l}^{max}$ in
Eq. \ref{eq:inequality-constraint}) for the parameter $dz$ are set to be,
respectively, a value close to zero and a large value resulting in a
total thickness ($L \, dz$) greater than that we expect for the true source.
At this point, we have fully defined the vector $\hat{\mathbf{p}}_{(0)}$
representing the cylinder-like initial approximation for the parameter
vector $ \mathbf{p} $ (Eq. \ref{eq:p-vector}).

\subsubsection{Definition of optimum values for $m_{0}$ and $z_{0}$}

After defining the initial approximation $\hat{\mathbf{p}}_{(0)}$, we need
to set the depth to the top of the shallowest prism $z_0$
and the total-magnetization intensity $m_0$ for all prisms.
These two parameters are also previously defined by the interpreter and
remain fixed along the iterations of our gradient-based algorithm for solving the
nonlinear inverse problem.

In the absence of a priori information, the finding of values close to the true ones
for $z_0$ and $m_0$ is a very difficult task due to the inherent ambiguity
in magnetic inversion. It is expected that estimated magnetic sources obtained by
using different combinations of $z_0$ and $m_0$ may produce similar data fits.
Because of that, we do not estimate their values in the nonlinear inverse problem.
Instead, we set ranges of tentative values for them.
For each tentative pair ($m_0$, $z_0$), we use the same previously defined
cylinder-like initial approximation $\hat{\mathbf{p}}_{(0)}$ to
obtain an independent estimated parameter vector $\hat{\mathbf{p}}_{(f)}$
minimizing the goal function $\Gamma (\mathbf{p}, m_{0}, z_{0})$
(Eq. \ref{eq:gamma}).
Note that this approach results in a set of estimated magnetic sources with
different depths to the top $z_0$, total-magnetization intensities $m_0$ and
geometries defined by different estimated parameter vectors $\hat{\mathbf{p}}_{(f)}$.
Each estimated magnetic source results in a different value
$\Gamma (\hat{\mathbf{p}}_{(f)}, m_{0}, z_{0})$ for the goal function.
On the discrete map of the goal function produced by the set of estimated magnetic
sources, each one associated with a tentative pair ($m_0$, $z_0$),
we select the optimum values of $m_{0}$ and $z_{0}$ as those producing the smallest
value of $\Gamma (\hat{\mathbf{p}}_{(f)}, m_{0}, z_{0})$.
We stress that all estimated parameter vectors $\hat{\mathbf{p}}_{(f)}$ are obtained
by using the same values for the weights $\alpha_{\ell}$ (Eq. \ref{eq:gamma}).
Details about how we set these weights are presented later, in the section
\textit{Setting the weights $\alpha_{1}-\alpha_{7}$}.

Usually, the range of $z_0$ includes the topography ($z_0 = 0$) and the range of
$m_0$ is based on a priori information such as petrophysical studies.
In order to verify our initial approximation $\hat{\mathbf{p}}_{(0)}$ and the
ranges of tentative values for $z_0$ and $m_0$, we compute a discrete map
of the data-misfit function $\phi (\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:misfit}).
All values in this map are produced by using the same previously defined
initial approximation $\hat{\mathbf{p}}_{(0)}$. The only differences are the
tentative values for $z_0$ and $m_0$.
The computation of this discrete map is carried out, before performing the
inversions, with the purpose of verifying if there is at least one pair of $ m_0 $ and $ z_0 $
associated with a data-misfit function $\phi (\hat{\mathbf{p}}_{(0)}, m_{0}, z_{0})$
showing a relatively low value. This means that the initial approximation $\hat{\mathbf{p}}_{(0)}$
and tentative values for $m_{0}$ and $z_{0}$ produce a predicted data vector
$\mathbf{d}(\hat{\mathbf{p}}_{(0)}, m_{0}, z_{0})$ close to the observed
data vector $\mathbf{d}^{o}$.
We stress that, at this step, we do not need a good data fit.
However, if we cannot identify any point on the discrete map of the data-misfit function
$\phi (\hat{\mathbf{p}}_{(0)}, m_{0}, z_{0})$ associated with a satisfactory
data fit, the initial approximation $\hat{\mathbf{p}}_{(0)}$
and the ranges of $ z_0 $ and $ m_0 $ should be redefined before recomputing the discrete map.

\subsubsection{Inversion algorithm for estimating $\mathbf{p}$}

We use the Levenberg-Marquardt algorithm \cite[e.g., ][ p. 240]{aster-etal2019}
to solve the nonlinear inverse problem for estimating the
parameter vector $\hat{\mathbf{p}}_{(f)}$ minimizing the goal function
$\Gamma (\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:gamma}),
subject to the inequality constraints (Eq. \ref{eq:inequality-constraint}),
for given values of total-magnetization intensity $m_{0}$ and depth to the top
of the shallowest prism $z_{0}$.
The Levenberg-Marquardt algorithm is an iterative gradient-based method that, at each iteration $n$, updates the estimate parameter vector $\hat{\mathbf{p}}_{(n)}$ to obtain a new estimated parameter vector $\hat{\mathbf{p}}_{(n + 1)}$.
We compute this update by following the same strategy of \cite{barbosa-1999b}, \cite{oliveirajr-etal2011} and \cite{oliveirajr-barbosa2013} to incorporate the inequality constraints (Eq. \ref{eq:inequality-constraint}).
This strategy consists in transforming each element $\hat{p}_{l} \in (p_{l}^{min}, p_{l}^{max})$ of the estimated parameter vector $\hat{\mathbf{p}}_{(n)}$
into the element $\hat{p}^{\dagger}_{l} \in (- \infty, + \infty)$ of an
unconstrained vector $\hat{\mathbf{p}}^{\dagger}_{(n)}$ as follows:
\begin{equation}\label{eq:inequality-function}
\hat{p}^{\dagger}_{l} = -\ln\left(\frac{p_{l}^{max} - \hat{p}_{l}}{\hat{p}_{l} - p_{l}^{min}}\right) \: ,
\end{equation}
where $p_{l}^{min}$ and $p_{l}^{max}$ are defined in the inequality constraints
(Eq. \ref{eq:inequality-constraint}).
The inverse transformation of each element $\hat{p}^{\dagger}_{l} \in (- \infty, + \infty)$ into the element $\hat{p}_{l} \in (p_{l}^{min}, p_{l}^{max})$ is the following:
\begin{equation}\label{eq:inv-inequality-function}
\hat{p}_{l} = p_{l}^{min} + \left(\frac{p_{l}^{max} - p_{l}^{min}}{ 1 + e^{-\hat{p}^{\dagger}_{l}} }\right) \: .
\end{equation}

At each iteration $n$ of our algorithm, a correction
$\boldsymbol{\Delta}\hat{\mathbf{p}}^{\dagger}_{(n)}$ for the
unconstrained vector $\hat{\mathbf{p}}^{\dagger}_{(n)}$
is computed by solving the following linear system:
\begin{equation}\label{eq:linear-system}
\mathbf{Q}_{(n)}^{-1} \left[\mathbf{Q}_{(n)} \mathbf{H}^{\dagger}(\hat{\mathbf{p}}_{(n)}) \mathbf{Q}_{(n)} + \lambda_{(n)} \mathbf{I}_{M} \right]
\mathbf{Q}_{(n)}^{-1} \boldsymbol{\Delta} \hat{\mathbf{p}}^{\dagger}_{(n)}
= -\boldsymbol{\nabla}\Gamma(\hat{\mathbf{p}}_{(n)}) \: ,
\end{equation}
where $\lambda_{(n)}$ is a positive scalar (known as Marquardt parameter) which is adjusted at each iteration and is associated with the Levenberg-Marquardt method \cite[e.g., ][ p. 240]{silva-2001,aster-etal2019},
$\mathbf{I}_{M}$ is the identity matrix with order $M$,
$\boldsymbol{\nabla}\Gamma(\hat{\mathbf{p}}_{(n)})$
is the gradient of the goal function (Eq. \ref{eq:gamma_gradient}),
$\mathbf{H}^{\dagger}(\hat{\mathbf{p}}_{(n)})$ is a matrix given by
\begin{equation}\label{eq:H-dagger}
\mathbf{H}^{\dagger}(\hat{\mathbf{p}}_{(n)}) = \mathbf{H}(\hat{\mathbf{p}}_{(n)})\mathbf{T}(\hat{\mathbf{p}}_{(n)})
\end{equation}
and $\mathbf{Q}_{(n)}$ is a diagonal matrix proposed by \cite{marquardt_algorithm_1963} for scaling the parameter $\lambda_{(n)}$
at each iteration.
The element $ll$ of this diagonal matrix is given by
\begin{equation}\label{eq:Q-matrix}
q_{ll} = \frac{1}{\sqrt{h^{\dagger}_{ll}}} \: ,
\end{equation}
where $h^{\dagger}_{ll}$ is the element $ll$ of the matrix $\mathbf{H}^{\dagger}(\hat{\mathbf{p}}_{(n)})$ (Eq. \ref{eq:H-dagger}).
In Eq. \ref{eq:H-dagger}, $\mathbf{H}(\hat{\mathbf{p}}_{(n)})$ is the Hessian matrix of the goal function (Eq. \ref{eq:gamma_hessian}) and $\mathbf{T}(\hat{\mathbf{p}}_{(n)})$ is a diagonal matrix whose element $ll$ is given by
\begin{equation}\label{eq:inequality-diag}
t_{ll} = \frac{(p_{l}^{max} - \hat{p}_{l} + \epsilon)(\hat{p}_{l} - p_{l}^{min} + \epsilon)}{p_{l}^{max} - p_{l}^{min}} \: ,
\end{equation}
where $\hat{p}_{l}$ is the $l$th element of the estimated parameter vector
$\hat{\mathbf{p}}_{(n)}$ and $\epsilon$ is a small positive number
($\approx 10^{-2}$) used to prevent null values.

After estimating $\boldsymbol{\Delta} \hat{\mathbf{p}}^{\dagger}_{(n)}$ by
solving the linear system (Eq. \ref{eq:linear-system}), we update the unconstrained
vector by computing
\begin{equation}\label{eq:p-k1-dagger}
\hat{\mathbf{p}}^{\dagger}_{(n + 1)} =
\hat{\mathbf{p}}^{\dagger}_{(n)} +
\boldsymbol{\Delta} \hat{\mathbf{p}}^{\dagger}_{(n)} \: .
\end{equation}
Next, we compute the elements of an updated parameter vector $\hat{\mathbf{p}}_{(n + 1)}$ by using Eq. \ref{eq:inv-inequality-function}.
Finally, we stop the iterative process by evaluating the invariance of the
goal function (Eq. \ref{eq:gamma}) along successive iterations.
Specifically, we check if the inequality
\begin{equation}\label{eq:stop-criterion}
 \left| \frac{\Gamma (\hat{\mathbf{p}}_{(n +1)}, m_{0}, z_{0}) -
 \Gamma (\hat{\mathbf{p}}_{(n)}, m_{0}, z_{0})}
 {\Gamma (\hat{\mathbf{p}}_{(n)}, m_{0}, z_{0})}
 \right| \le \tau
\end{equation}
holds, where $\tau$ is a threshold value on the order of $10^{-3}$ to $10^{-4}$.
Usually, the inversion algorithm converges at $8$-$12$ iterations.
The estimate $\hat{\mathbf{p}}_{(n+1)}$ that is obtained
at the last iteration and satisfies the inequality constraint defined by
Eq. \ref{eq:stop-criterion} is the estimated parameter vector
$\hat{\mathbf{p}}_{(f)}$ minimizing the goal function
$\Gamma (\mathbf{p}, m_{0}, z_{0})$ (Eq. \ref{eq:gamma}), for the given
values of depth to the top $z_{0}$ and total-magnetization intensity $m_{0}$.
In the evaluation of the goal function, we compute the
constraint functions $\varphi_{\ell}(\mathbf{p})$ (Eqs \ref{eq:phi1}, \ref{eq:phi2},
\ref{eq:phi3}, \ref{eq:phi4}, \ref{eq:phi5}, \ref{eq:phi6} and \ref{eq:phi7}),
$\ell = 1, \dots, 7$, by using the expressions which are written as sum of
terms instead of those defined by sparse matrices.

\subsubsection{Choice of the weights $\alpha_{\ell}$}

Attributing values to the weights $ \alpha_{\ell} $ (Eq. \ref{eq:gamma}) is an important feature of our method.
However, there is no analytical rule to define them and their values can be dependent on the particular characteristics of the type of geological setting where the method is being applied \cite[]{silva-2001}.

At this point, we draw attention that the weights $ \alpha_{\ell}$ (Eq. \ref{eq:gamma}) are dimensional quantities.
Note that the units of the data-misfit function (Eq. \ref{eq:misfit}) and the constraint functions
(Eqs \ref{eq:phi1}, \ref{eq:phi2}, \ref{eq:phi3}, \ref{eq:phi4}, \ref{eq:phi5},
\ref{eq:phi6} and \ref{eq:phi7}), are nT$^{2}$ and $m^{2}$, respectively.
Because we set the unit of the goal function (Eq. \ref{eq:gamma}) as nT$^{2}$, the unit of the weights
$ \alpha_{\ell} $ (Eq. \ref{eq:gamma}) is nT$^{2}$m$^{-2}$.

The physical dimensions of the weights $ \alpha_{\ell}$ makes assignment of
their values problem dependent.
To make these weights comparable to each other, we normalize the $ \alpha_{\ell} $
values as follows:
\begin{equation}\label{eq:alphas}
\alpha_{\ell} = \tilde{\alpha}_\ell \frac{E_\phi}{E_\ell}, \quad \ell = 1,\dots, 7,
\end{equation}
where $\tilde{\alpha}_\ell$ is a positive scalar and $ E_\phi/E_\ell $ is a normalizing
factor allowing the $\tilde{\alpha}_\ell$ to be independent of the physical units used.
In Eq. \ref{eq:alphas}, $ E_\ell $ represents the
trace of the Hessian matrix $\mathbf{H}_{\ell}$ (Eqs \ref{eq:phi1_hessian},
\ref{eq:phi2_hessian}, \ref{eq:phi3_hessian}, \ref{eq:phi4_hessian}, \ref{eq:phi5_hessian},
\ref{eq:phi6_hessian}, and \ref{eq:phi7_hessian}) of the $\ell$th constraining function
$\varphi_{\ell}(\mathbf{p})$ (Eqs \ref{eq:phi1}, \ref{eq:phi2}, \ref{eq:phi3},
\ref{eq:phi4}, \ref{eq:phi5}, \ref{eq:phi6}, and \ref{eq:phi7}).
The constant $E_\phi$ is the trace of the Hessian matrix
$\mathbf{H}_{\phi}(\hat{\mathbf{p}}_{(0)})$ (Eq. \ref{eq:phi_hessian}) of the misfit function
$\phi(\mathbf{p})$ (Eq. \ref{eq:misfit}) computed with the initial approximation $\hat{\mathbf{p}}_{(0)}$
for the parameter vector $ \mathbf{p} $ (Eq. \ref{eq:p-vector}) at the beginning of the inversion algorithm.
Note that the trace of the Hessian matrix $\mathbf{H}_{\ell}$ is dimensionless and
the trace of the Hessian matrix $\mathbf{H}_{\phi}(\hat{\mathbf{p}}_{(0)})$ has unit of nT$^{2}$m$^{-2}$.
Thus, the positive scalars $\tilde{\alpha}_\ell$ in Eq. \ref{eq:alphas} are dimensionless quantities.

According to this empirical strategy, the weights $ \alpha_{\ell} $
(Eq. \ref{eq:gamma}) are redefined using Eq. \ref{eq:alphas}, in which the weights
$\tilde{\alpha}_\ell$ are positive scalars, independent of physical units and
less dependent on the particular characteristics of the
interpretation geological setting.

%\subsubsection{Practical considerations}

The values attributed to the dimensionless weights $\tilde{\alpha}_{1} - \tilde{\alpha}_{7}$
(Eq. \ref{eq:alphas}) significantly impact the estimated models and cannot be
automatically set without the interpreter’s judgment.
Based on our practical experience, we suggest some
empirical procedures for setting these parameters.

The parameters $\tilde{\alpha}_1$ and $\tilde{\alpha}_2$ impose prior information
on the shape of the horizontal cross-section of the prisms.
The first one forces all prisms to have a circular horizontal cross-section, while
the second forces all prisms to have a similar horizontal cross-section.
Generally, their values vary from $10^{-5}$ to $10^{-3}$ and differ from
each other by one order of magnitude, at most.
The parameter $\tilde{\alpha}_3$ also varies from $10^{-5}$ to $10^{-3}$ and
controls the relative position of adjacent prisms forming the model.
A high value privileges a vertical estimated body, whereas a small value
tends to generate an inclined estimated body.

In comparison to $\tilde{\alpha}_1$, $\tilde{\alpha}_2$ and $\tilde{\alpha}_3$,
the other parameters usually have smaller values varying from $10^{-8}$ to $10^{-4}$.
The parameters $\tilde{\alpha}_4$ and $\tilde{\alpha}_5$ control the shape and position of the shallowest prism of the interpretativ model. They are used when a priori
information about the source is available at the study area.
The parameter $\tilde{\alpha}_6$ imposes a minimum volume to the estimated source.
The value of parameters $\tilde{\alpha}_4$, $\tilde{\alpha}_5$, and $\tilde{\alpha}_6$ are set to be as small as possible.
The parameter $\tilde{\alpha}_7$ controls the total-vertical extension of the
the estimated body.
The greater its value, the shallower the estimated depth to the bottom of the source
will be and vice versa.

If the data fitting is poor or if the estimated source shows unrealistic shape,
the dimensionless weights $\tilde{\alpha}_{\ell}$ (Eq. \ref{eq:alphas}) must be
fine-tuned
to obtain a realistic solution. After tuning their values, they will be used in all
inversions without changing along the iterations.
Educated guesses for the $\tilde{\alpha}_{\ell}$ values are
$\tilde{\alpha}_1 = 10^{-4}$, $\tilde{\alpha}_2 = 10^{-4}$, $\tilde{\alpha}_3 = 10^{-4}$,
$\tilde{\alpha}_4 = 10^{-7}$, $\tilde{\alpha}_5 = 10^{-7}$, $\tilde{\alpha}_6 = 10^{-7}$,
$\tilde{\alpha}_7 = 10^{-5}$.


\label{sec:DETRavenApproach}
The main subject of this report is to explain the newer Adaptive Dynamic Event Tree (ADET) methodology developed within the RAVEN code. As already mentioned, the DET approach is extremely effective (achiving almost a perfect tradeoff) with respect to computational resources usage and the effective exploration of the uncertain domain (input space). The proposed approach is designed to further focus these features of the DET methodology toward the characterization of some properties of the input space. More specifically, the approach that will be introduced is an adaptive methodology that defines the branching strategy in order to identify more efficiently (lower computational costs) and more accurately the location of transitions in the input space (e.g., failure vs. success). The characterization of the input space is translated into an “optimization-like” problem for the identification of the so-called Limit Surface, and accelerated through the employment of artificial intelligence-based algorithms that help the selection of future branching points.

The concepts mentioned here in brief are explained in detail in the following sub-sections.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Concept of Limit Surface }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:LimitS}
 As already mentioned, the adaptive methodology can be considered as a goal oriented sampling strategy for the research of the limit surfaces (LSs). To define the meaning of the LS, it is necessary to introduce the concept of “goal” function. Without making a rigorous mathematical treatment, the “goal” function is an object that is defined as a part of the system outcome space. In a safety context, the goal function usually represents the success or failure (transition) of the system. Therefore, if $\overline{x}_{f}$ is the final status of the system, $C\left (\overline{x}_{f}\right )$ the goal function ( $C\left (\overline{x}_{f}\right ) = 1$ system success,  $C\left (\overline{x}_{f}\right ) = -1$ system failure), ), it is possible to define $\overline{x}_{f,L}$ as the transition surface in the output space with respect to the goal function. This statement can be translated in the following mathematical expression: $\overline{x}_{f,L} = \left \{ \overline{x}_{f}|\left | \overline{\bigtriangledown} C\left ( \overline{x}_{f} \right ) \right |= \infty \right \}$. If $\overline{x}\left (t,\overline{p},\overline{x}_{0}  \right )$ represents the evolution of the system (that can be considered deterministic, once the initial condition ($\overline{x}_{0} $) and stochastic model parameters ($\overline{p}$) have been chosen), then is it possible to identify the set of pairs $\left ( \overline{p},\overline{x}_{0} \right )_{LS}$, in the input space, which leads the system outcome to match $\overline{x}_{f,L}$. Such set of points $\left ( \overline{p},\overline{x}_{0} \right )$ represents the limit surface, in the input space.The LS represent therefore the boundary between inputs leading to success or failure of the system, and more generally boundaries among transition regions.

Since the probability of a particular outcome is equivalent to the probability of being in the input space that leads to that outcome, the probability of a system outcome (e.g. success/failure) can be evaluated by computing the probability of being in the space surrounded by the LS.
The determination of the LS is extremely important since its informative content.  Its knowledge: allows to efficiently evaluate risk functions, informs regarding which uncertainties are the most relevant from a risk point of view (ranking of uncertain space), defines safe regions to be explored for plant optimization, identifies risk neutral design direction, etc. Unfortunately, the determination of LSs is very computationally expensive, since a brute-force approach would imply the evaluation of each point of an N-dimensional grid that discretizes the input space. The number of points in this grid is proportional to the requested accuracy. In order to overcome these limitations, a possible approach is the employment of acceleration schemes based on Reduced Order Models (ROMs). ROMs are used to predict the location of the LS in order to drive the exploration of the input space toward its possible location. The Adaptive Dynamic Event Tree is based on this concept.
\label{sec:ADET}
\begin{figure}[h]
  \centering
     \includegraphics[width=0.8\textwidth]{figures/DET_LS_pb.png}
  \caption{Dynamic Event Tree Limit Surface}
  \label{fig:LSDET}
\end{figure}
%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Reduced Order Models}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:ROMs}
As mentioned in the previous section, the Adaptive Dynamic Event Tree uses ROMs in order to accelerate the search of transition boundaries represented by the LS. Simplistically, a ROM is a mathematical model of fast solution trained to predict a response of interest of a physical system. The training process is performed by “sampling” the response of a physical model with respect to variations of parameters that are subject to probabilistic behavior. The results (outcomes of the physical model) of those samplings are fed into the algorithm representing the ROM that tunes itself to replicate those results. More specifically, in RAVEN case the reduced order model is constructed to emulate a numerical representation of a physical system. In fact, as already mentioned, the physical system model would be too expensive to be used to explore the whole input space, exploration that would be needed for the limit surface search. Under certain assumptions, the ROM ability to predict the output space of a system improves, increasing the number of training samples (better representation of the whole domain).  This is not always true, since some of the ROMs used might be subject to over-fitting issues.
In RAVEN, several different Reduced Order Model types are available, such as Support Vector Machine, Neighbors based, multi-class models, Quadratic Discriminants, etc. All those supervised learning algorithms have been imported via an Application Programming Interface (API) within the scikit-learn~\cite{scikitlearn} library.

The acceleration schemes in the adaptive approach use a class of supervised learning algorithms usually referred to as “classifiers.” In essence, classifiers are ROMs specialized to represent a binary response of a system (e.g., failure/success, on/off, etc.), like the goal function of interest in our case.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[h]
  \centering
     \includegraphics[width=0.5\textwidth]{figures/AdaptiveDET.png}
  \caption{Adaptive Dynamic Event Tree Scheme}
  \label{fig:AdaptiveDET}
\end{figure}
\subsection{Adaptive DET}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The main idea of the application of the previously explained adaptive sampling approach to the DET comes from the observation that the DET is intrinsically adaptive.

In order to explain this statement, as an example, fig.~\ref{fig:LSDET} shows a LS generated by the DET sampling methodology currently available in RAVEN. In this case, a goal function based on the clad max temperature has been used:
\begin{equation}
\left \{ C \right \}_{i}=\left \{ \begin{matrix}
1 & T_{C,i}\geq T_{C,F}\\
-1 & T_{C,i}<  T_{C,F}
\end{matrix} \right.
\end{equation}
where $T_{C,i}$ is the clad temperature and $T_{C,F}$ is the failure temperature, sampled by triangular distribtuion.
As it can be seen, the DET method, when one or more uncertain parameters directly influence the outcome of the goal function, tends to search for the LS with accuracy equal to the user defined grid discretization in the input space.
For this reason, it appears natural to use the DET approach to perform a goal-function oriented pre-sampling of the input space. The proposed approach can be described through the following steps (see Figs.~\ref{fig:AdaptiveDET}~\ref{fig:ADETflowChart}):
\begin{enumerate}
\item A limited number of points in the input space $\left ( \overline{p},\overline{x}_{0} \right )_{i}$ are selected via a DET approach
\item The system code is then used to compute all the possible evolution of the system starting from the above selected input points:
\begin{equation}
    \overline{x}_{i}=\overline{x}\left (t,\overline{p}_{i},\overline{x}_{0,i}  \right )
\end{equation}
\item The goal function  $C\left (\overline{x}_{f}\right )$ (in this case of type Boolean) is evaluated at the final phase space coordinate of the system (outcome):
\begin{equation}
 \left \{ C \right \}_{i}=\left \{ \overline{x} \left (  t=t_{end},\overline{p}_{i},\overline{x}_{0,i}\right )\right \}
\end{equation}
\item The set of pairs $\left ( \overline{p},\overline{x}_{0} \right )_{i} \rightarrow \left \{ C \right \}_{i}$ are used to train a ROM
\item The ROM is used to predict the value of the goal function on a regular Cartesian grid  in the domain space. The grid meshing is dictated by the user-requested accuracy in the determination of the LS location:
\begin{equation}
 \begin{matrix}
 \left \{ c \right \}_{j} \cong ROM\left ( \left \{ \overline{x}_{0} \right \}_{j} \right ) & j=1,...,M
\end{matrix}
\end{equation}
where $j=1, ..., M$ points on the grid
\item The regions identified by a change in value of $\left \{ c \right \}_{j}$ (between -1 and 1) are therefore the ROM prediction of the limit surface location:
\begin{equation}
(\overline{p},\overline{x}_{0})_{LS}
\end{equation}
\item The position of the LS is compared with the one at the previous iteration; if no changes are consecutively detected for a user-defined number of times than the iterations stop; otherwise a new point, in the input space, needs to be selected to increase the ROM training set.
\item The next selected point to be added to the training set is the one located on the limit surface that is the farther from all the other already selected points
\item A hierarchical searching process is performed on the DET branches already evaluated and the starting point (branch) for the new evaluation is determined
\item The process restart from point 2.
\end{enumerate}
\begin{figure}[h]
  \centering
     \includegraphics[width=0.6\textwidth]{figures/adaptiveFlowChart.png}
  \caption{Dynamic Event Tree Flow Chart}
  \label{fig:ADETflowChart}
\end{figure}

In order to understand how the algorithm works, it is necessary to explain in details the first and ninth steps of the previous algorithm flow. The ADET methodology starts with a standard DET that performs the initial exploration of the input space, exploiting its capability to investigate the uncertain domain in a reasonably short time. As already mentioned and shown in fig.~\ref{fig:LSDET}, when a goal function is directly influenced by one (or more) uncertain parameter(s), the DET tends to increase the sampling density in proximity of the transition boundaries (LS). This means that the acceleration ROM gets trained with an initial dataset closed to the LS that reasonably helps the convergence of the method.

After the initial DET, the actual adaptive scheme starts to be employed following the calculation flow previously reported. Every time a new point, in the input space, is requested, a search in the DET database is performed. This search is needed to identify the closest branch with respect to the next point needs to be evaluated. Once the closest branch has been identified, the system code is run using that branch as starting point (i.e. the calculation restarts from an advanced point in time). This new evaluation is then added to the DET database and can be used for sub-sequential requests. This approach speeds the adaptive search up and employs an effective exploration of the input space in proximity of the transition boundaries.

In essence this methodology combines the adaptive search algorithm already present in RAVEN with a DET approach, allowing the re-usage of branches of simulations already performed rather than restarting the simulation always from the begin.


\section{One to one and onto transformations}

\begin{outcome}
  \begin{enumerate}
  \item Determine if a linear transformation is onto or one to one.
  \end{enumerate}
\end{outcome}

Let $T: \R^n \to \R^m$ be a linear transformation. We define the \textbf{range}\index{linear transformation!range} or \textbf{image}\index{linear transformation!image} of $T$ as the set of vectors of $\R^{m}$ which are of the form
$T (\vect{x})$ (equivalently, $A\vect{x}$) for some $\vect{x}\in \R^{n}$. It is common
to write $T\R^{n}$, $T(\R^{n})$, or
$\func{Im}(T) $ to denote these vectors.

\begin{lemma}{Range of a matrix transformation}{Ax}

Let $A$ be an $m\times n$-matrix where $A_{1},\ldots, A_{n}$ denote the columns of
$A$\index{range of matrix transformation}. Then, for a vector $\vect{x}=\begin{mymatrix}{c}
x_{1} \\
\vdots \\
 x_{n}
\end{mymatrix}$ in $\R^n$,

\begin{equation*}
A\vect{x}=\sum_{k=1}^{n}x_{k}A_{k}
\end{equation*}

Therefore, $A (\R^n)$ is the collection of all
linear combinations of these products.
\end{lemma}

\begin{proof}
This follows from the definition of matrix multiplication.
\end{proof}

This section is devoted to studying two important characterizations of linear transformations, called one to one and onto. We define them now.

\begin{definition}{One to one}{one-to-one}
Suppose $\vect{x}_1$ and $\vect{x}_2$ are vectors in $\R^n$. A linear transformation $T: \R^n \to \R^m$ is called \textbf{one to one}\index{one to one} (often written as $1-1)$ if whenever
 $\vect{x}_1 \neq \vect{x}_2$ it follows that :
\begin{equation*}
T(\vect{x}_1) \neq T (\vect{x}_2)
\end{equation*}

Equivalently, if $T(\vect{x}_1) =T(\vect{x}_2)$,
then $\vect{x}_1 = \vect{x}_2$. Thus,  $T$ is one to one if it never takes two different
vectors to the same vector.
\end{definition}

The second important characterization is called onto.

\begin{definition}{Onto}{onto}
Let $T: \R^n \to \R^m$ be a linear transformation. Then $T$ is called \textbf{onto}\index{onto} if whenever $\vect{x}_2 \in \R^{m}$ there exists
$\vect{x}_1 \in \R^{n}$ such that $T(\vect{x}_1) = \vect{x}_2. $
\end{definition}

We often call a linear transformation which is one-to-one an \textbf{injection}\index{injection}. Similarly, a linear transformation which is onto is often called a \textbf{surjection}\index{surjection}.

The following proposition is an important result.

\begin{proposition}{One to one}{one-to-one-matrices}
Let $T:\R^n \to \R^m$ be a linear transformation. Then $T$ is one to one if
and only if $T(\vect{x}) = \vect{0}$ implies $\vect{x}=\vect{0}$.
\end{proposition}

\begin{proof}
We need to prove two things here. First, we will prove that if $T$ is one to one, then
$T(\vect{x}) = \vect{0}$ implies that $\vect{x}=\vect{0}$. Second, we will show that if $T(\vect{x})=\vect{0}$ implies that $\vect{x}=\vect{0}$, then
it follows that $T$ is one to one. Recall that a linear transformation has the property that $T(\vect{0}) = \vect{0}$.

%%Note that since $T$ is linear, it is induced by an $m \times n$-matrix $A$. Therefore we can rewrite the statement ``$T_A(\vect{x}) = \vect{0}$ implies $\vect{x}=\vect{0}$'' in terms of the matrix $A$ as ``$A\vect{x}=\vect{0}$ implies $\vect{x}=\vect{0}$''. Therefore we can prove this theorem using $A$.
%%
%%Observe that $A\vect{0}=A(\vect{0}+\vect{0}) =A\vect{0} +A\vect{0}$ and so $A\vect{0}=\vect{0}$.
%%
%%Now suppose $A$ is one to one and $A\vect{x}=\vect{0}$. We need to show that this implies $\vect{x}=\vect{0}$. Since $A$ is one to one, by Definition~\ref{def:one-to-one} $A$ can only map one vector to the zero vector $\vect{0}$. Now $A\vect{x}=\vect{0}$ and $A\vect{0}=\vect{0}$, so it follows that $\vect{x}=\vect{0}$. Thus if $A$ is one to one and $A\vect{x}=\vect{0}$, then $\vect{x}=\vect{0}$.
%%
%%Next assume that $A\vect{x}=\vect{0}$ implies $\vect{x}=\vect{0}$.  We need to show that $A$ is one to one. Suppose $A\vect{x}=A\vect{y}$. Then $A\vect{x} - A\vect{y} = \vect{0}$.
%%Hence $A\vect{x}-A\vect{y} =  A(\vect{x}-\vect{y}) = \vect{0}$. However, we have assumed that $A\vect{x}=\vect{0}$ implies $\vect{x}=\vect{0}$. This means that
%%whenever $A$ times a vector equals $\vect{0}$, that vector is also equal to $\vect{0}$. Therefore,  $\vect{x}-\vect{y} = \vect{0}$ and so $\vect{x}=\vect{y}$.
%%Thus $A$ is one to one by Definition~\ref{def:one-to-one}.
%%\end{proof}

Suppose first that $T$ is one to one and consider $T(\vect{0})$.
\begin{equation*}
T(\vect{0})=T(\vect{0}+\vect{0}) =T(\vect{0})+T(\vect{0})
\end{equation*}
and so, adding the additive inverse of $T(\vect{0})$ to both sides, one sees
that $T(\vect{0})=\vect{0}$. If $T(\vect{x})=\vect{0}$ it must be the
case that $\vect{x}=\vect{0}$ because it was just shown that $T(\vect{0})=\vect{0}$ and $T$ is assumed to be one to one.

Now assume that if $T(\vect{x})=\vect{0}$, then it follows that $\vect{x}=\vect{0}$. If $T(\vect{v})=T(\vect{u})$, then
\[
T(\vect{v})-T(\vect{u})=T(\vect{v}-\vect{u}) =\vect{0}
\]
which shows that $\vect{v}-\vect{u}=0$. In other words, $\vect{v}=\vect{u}$, and $T$ is one to one.
\end{proof}

Note that this proposition says that if $A=\begin{mymatrix}{ccc}
A_{1} & \cdots & A_{n}
\end{mymatrix} $ then $A$ is one to one if and only if whenever
\begin{equation*}
0 = \sum_{k=1}^{n}c_{k}A_{k}
\end{equation*}
it follows that each scalar $c_{k}=0$.

We will now take a look at an example of a one to one and onto linear transformation.

\begin{example}{A one to one and onto linear transformation}{one-to-one-onto-linear-transformation}
Suppose
\begin{equation*}
T\begin{mymatrix}{c}
x \\
y
\end{mymatrix} =\begin{mymatrix}{rr}
1 & 1 \\
1 & 2
\end{mymatrix} \begin{mymatrix}{r}
x \\
y
\end{mymatrix}
\end{equation*}
Then, $T:\R^{2}\rightarrow \R^{2}$ is a linear
transformation. Is $T$ onto? Is it one to one?
\end{example}

\begin{solution} Recall that because $T$ can be expressed as matrix
multiplication, we know that $T$ is a linear transformation.  We will
start by looking at onto.  So suppose $\begin{mymatrix}{c}
a \\
b
\end{mymatrix} \in \R^{2}$. Does there exist $\begin{mymatrix}{c}
x \\
y
\end{mymatrix}  \in \R^2 $ such that $T\begin{mymatrix}{c}
x \\
y
\end{mymatrix} =\begin{mymatrix}{c}
a \\
b
\end{mymatrix}$? If so, then since $\begin{mymatrix}{c}
a \\
b
\end{mymatrix} $ is an arbitrary vector in $\R^{2}$, it will follow that $T$
is onto.

This question is familiar to you. It is asking whether
there is a solution to the equation
\begin{equation*}
\begin{mymatrix}{cc}
1 & 1 \\
1 & 2
\end{mymatrix} \begin{mymatrix}{c}
x \\
y
\end{mymatrix} =\begin{mymatrix}{c}
a \\
b
\end{mymatrix}
\end{equation*}
This is the same thing as asking for a solution to the following system of
equations.
\begin{equation*}
\begin{array}{c}
x+y=a \\
x+2y=b
\end{array}
\end{equation*}
Set up the augmented matrix and row reduce.
\begin{equation}
\begin{mymatrix}{rr|r}
1 & 1 & a \\
1 & 2 & b
\end{mymatrix} \rightarrow \begin{mymatrix}{rr|r}
1 & 0 & 2a-b \\
0 & 1 & b-a
\end{mymatrix}
\label{onto-matrix}
\end{equation}
You can see from this point that the system has a solution. Therefore,
we have shown that for any $a, b$, there is a $
\begin{mymatrix}{c}
x \\
y
\end{mymatrix}$ such that $T\begin{mymatrix}{c}
x \\
y
\end{mymatrix} =\begin{mymatrix}{c}
a \\
b
\end{mymatrix}$.
Thus $T$ is onto.

Now we want to know if $T$ is one to one.
By Proposition~\ref{prop:one-to-one-matrices} it is enough to show that $A\vect{x}=0$ implies $\vect{x}=0$.
Consider the system $A\vect{x}=0$ given by:
\begin{equation*}
\begin{mymatrix}{cc}
1 & 1 \\
1 & 2\\
\end{mymatrix}
\begin{mymatrix}{c}
x\\
y
\end{mymatrix}
=
\begin{mymatrix}{c}
0 \\
0
\end{mymatrix}
\end{equation*}

This is the same as the system given by

\begin{equation*}
\begin{array}{c}
x + y = 0 \\
x + 2y = 0
\end{array}
\end{equation*}

We need to show that the solution to this system is $x = 0$ and $y = 0$. By setting up the augmented matrix and row reducing, we end up with
\begin{equation*} \begin{mymatrix}{rr|r}
1 & 0 & 0 \\
0 & 1 & 0
\end{mymatrix}
\end{equation*}

This tells us that $x = 0$ and $y = 0$. Returning to the original system, this says that if

\begin{equation*}
\begin{mymatrix}{cc}
1 & 1 \\
1 & 2\\
\end{mymatrix}
\begin{mymatrix}{c}
x\\
y
\end{mymatrix}
=
\begin{mymatrix}{c}
0 \\
0
\end{mymatrix}
\end{equation*}

then
\begin{equation*}
\begin{mymatrix}{c}
x \\
y
\end{mymatrix}
=
\begin{mymatrix}{c}
0 \\
0
\end{mymatrix}
\end{equation*}

In other words, $A\vect{x}=0$ implies that $\vect{x}=0$. By
Proposition~\ref{prop:one-to-one-matrices}, $A$ is one to one, and so $T$ is also one to one.

We also could have seen that $T$ is one to one from our above solution for onto. By looking at the matrix given
by {\eqref{onto-matrix}}, you can see that there is a \textbf{unique} solution given
by $x=2a-b$ and $y=b-a$. Therefore, there
is only one vector, specifically
$\begin{mymatrix}{c}
x \\
y
\end{mymatrix}
=
\begin{mymatrix}{c}
2a-b\\
b-a
\end{mymatrix} $ such that $T\begin{mymatrix}{c}
x \\
y
\end{mymatrix} =\begin{mymatrix}{c}
a \\
b
\end{mymatrix}$. Hence by Definition~\ref{def:one-to-one}, $T$ is one to one.
\end{solution}

\begin{example}{An onto transformation}{onto-transformation}
Let $T: \R^4 \to \R^2$ be a linear transformation defined by
\[
T \begin{mymatrix}{c}
a \\
b \\
c \\
d
\end{mymatrix} =
\begin{mymatrix}{c}
a + d \\
b + c
\end{mymatrix}
\mbox{ for all } \begin{mymatrix}{c}
a \\
b \\
c \\
d
\end{mymatrix} \in \R^4
\]
Prove that $T$ is onto but not one to one.
\end{example}

\begin{solution}
You can prove that $T$ is in fact linear.

To show that $T$ is onto, let $\begin{mymatrix}{c}
x \\
y
\end{mymatrix}$ be an arbitrary vector in $\R^2$. Taking the vector $\begin{mymatrix}{c}
x \\
y \\
0 \\
0
\end{mymatrix} \in \R^4$ we have
\[
T \begin{mymatrix}{c}
x \\
y \\
0 \\
0
\end{mymatrix} =
\begin{mymatrix}{c}
x + 0 \\
y + 0
\end{mymatrix}
= \begin{mymatrix}{c}
x \\
y
\end{mymatrix}
\]
This shows that $T$ is onto.

By Proposition~\ref{prop:one-to-one-matrices} $T$ is one to one if and only if $T(\vect{x}) = \vect{0}$ implies that $\vect{x} = \vect{0}$. Observe that
\[
T \begin{mymatrix}{r}
1 \\
0 \\
0 \\
-1
\end{mymatrix} =
\begin{mymatrix}{c}
1 + -1 \\
0 + 0
\end{mymatrix}
= \begin{mymatrix}{c}
0 \\
0
\end{mymatrix}
\]
There exists a non-zero vector $\vect{x}$ in $\R^4$ such that $T(\vect{x}) = \vect{0}$. It follows that $T$ is not one to one.
\end{solution}

The above examples demonstrate a method to determine if a linear transformation $T$ is one to one or onto. It turns out that the matrix $A$ of $T$ can provide this information.

\begin{theorem}{Matrix of a one to one or onto transformation}{matrix-one-to-one-onto}
Let $T: \R^n \to \R^m$ be a linear transformation induced by the $m \times n$-matrix $A$. Then $T$ is one to one if and only if the rank of $A$ is $n$. $T$ is onto if and only if the rank of $A$ is $m$.
\end{theorem}

Consider Example~\ref{exa:onto-transformation}. Above we showed that $T$ was onto but not one to one. We can now use this theorem to determine this fact about $T$.

\begin{example}{An onto transformation}{onto-transformation-matrix}
Let $T: \R^4 \to \R^2$ be a linear transformation defined by
\[
T \begin{mymatrix}{c}
a \\
b \\
c \\
d
\end{mymatrix} =
\begin{mymatrix}{c}
a + d \\
b + c
\end{mymatrix}
\mbox{ for all } \begin{mymatrix}{c}
a \\
b \\
c \\
d
\end{mymatrix} \in \R^4
\]
Prove that $T$ is onto but not one to one.
\end{example}

\begin{solution}
Using Theorem~\ref{thm:matrix-one-to-one-onto} we can show that $T$ is onto but not one to one from the matrix of $T$. Recall that to find the matrix $A$ of $T$, we apply $T$ to each of the standard basis vectors $\vect{e}_i$ of $\R^4$. The result is the $2 \times 4$-matrix A given by
\[
A = \begin{mymatrix}{rrrr}
1 & 0 & 0 & 1 \\
0 & 1 & 1 & 0
\end{mymatrix}
\]
Fortunately, this matrix is already in {\rref}. The rank of $A$ is $2$. Therefore by the above theorem $T$ is onto but not one to one.
\end{solution}

Recall that if $S$ and $T$ are linear transformations, we can discuss their composite denoted $S \circ T$. The following examines what happens if both $S$ and $T$ are onto.

\begin{example}{Composite of onto transformations}{composite-onto}
Let $T: \R^k \to \R^n$ and $S: \R^n \to \R^m$ be linear transformations.
If $T$ and $S$ are onto, then $S \circ T$ is onto.
\end{example}

\begin{solution}
Let $\vect{z}\in \R^m$.
Since $S$ is onto, there exists a vector $\vect{y}\in \R^n$
such that $S(\vect{y})=\vect{z}$.
Furthermore, since $T$ is onto, there exists a vector $\vect{x}\in \R^k$
such that $T(\vect{x})=\vect{y}$.
Thus
\[ \vect{z} = S(\vect{y}) = S(T(\vect{x})) = (ST)(\vect{x}),\]
showing that for each $\vect{z}\in \R^m$ there exists and $\vect{x}\in \R^k$
such that $(ST)(\vect{x})=\vect{z}$.
Therefore, $S \circ T$ is onto.
\end{solution}

The next example shows the same concept with regards to one-to-one transformations.

\begin{example}{Composite of one to one transformations}{composite-one-to-one}
Let $T: \R^k \to \R^n$ and $S: \R^n \to \R^m$ be linear transformations.
Prove that if $T$ and $S$ are one to one, then $S \circ T$
is one-to-one.
\end{example}

\begin{solution}
To prove that $S \circ T$ is one to one, we need to show that if $S(T (\vect{v})) = \vect{0}$ it follows that $\vect{v} = \vect{0}$.
Suppose that  $S(T (\vect{v})) = \vect{0}$. Since $S$ is one to one, it follows that  $T (\vect{v}) = \vect{0}$. Similarly, since $T$ is one to one, it follows that $\vect{v} = \vect{0}$. Hence $S \circ T$ is one to one.
\end{solution}


% Copyright 2018 Melvin Eloy Irizarry-Gelpí
\chapter{WX Quaternion}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A WX quaternion has the form
\begin{equation}
    a_{0} + a_{1} W + a_{2} X + a_{3} WX
\end{equation}
These follow from a parabolic Cayley-Dickson construct on the W binions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Arithmetic Operations}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Multiplication}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Conjugate Operations}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Asterisk Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Quadrance}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Cloak Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Dagger Conjugation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Hodge Star}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Zero-Divisors}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Differential Operators}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{M\"{o}bius Transformations}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Cross-Ratio}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...


\documentclass[11pt]{article}
\usepackage{enumitem}
\usepackage{amsmath}
\begin{document}
\title{MAT1830 --- Assignment 3}
\author{Dylan Pinn --- 24160547}
\maketitle

\section*{Question 1}
Consider the following sentences. Under the interpretation where $x$ and $y$ range over the positive integers and $P(x, y)$ is the predicate "$y=x+7$", state whether each sentence is true or false and give a short explanation of why.

\begin{enumerate}[label= (\alph*)]
	\item $\forall y \exists x P(x, y)$
	"For all $y$ there exists a $x$ such that $y = x + 7$

	This is False, as picking an arbitrary number for $y$ such as 1, gives:
	$$y - x = 7$$
	$$1 - x = 7$$
	$$-x = 6$$
	$$x = -6$$
	which does not fall in the range of positive integers.
	\item $\forall x \exists y P(x, y)$
	"For all $x$ there exists a $y$ such that $y = x + 7$

	This is True, as the expression can be equated to $x = y - 7$ and for every value of $x$ in the range we can find a $y$ also in the range that makes the expression true. $y$ will always be 7 more than $x$.
	\item $\exists y \forall x P(x, y)$
	"There exists a $y$ for all $x$ such that $y = x + 7$

	This is False, as picking an arbitrary value for $y$ such as 1 and picking any positive integer for $x$ such as 1 gives:
	$$1 + 1 = 7$$
	$$2 = 7$$
	Which is False.

\end{enumerate}
\break

\section*{Question 2}
Use logic laws to show that
$$\neg (\forall xA(x) \to \forall x \exists y \neg B(x, y))$$
is logically equivalent to
$$\forall xA(x) \land \exists x \forall y B(x, y)$$

$$\neg (\forall xA(x) \to \forall x \exists y \neg B(x, y))$$
$$\equiv \neg (\neg (\forall xA(x) \lor \forall x \exists y \neg B(x, y)) \text{ (Implication law) }$$
$$\equiv \neg \neg(\forall xA(x)) \land \neg(\forall x\exists y \neg B(x, y)) \text{ (De Morgan's law)}$$
$$\equiv \forall xA(x) \land \neg(\forall x\exists y \neg B(x, y)) \text{ (Double Negation law)} $$
$$\equiv \forall xA(x) \land \neg(\forall x \neg \forall yB(x, y))$$
$$\equiv \forall xA(x) \land \neg(\neg \exists x \forall yB(x, y))$$
$$\equiv \forall xA(x) \land \neg\neg \exists x \forall yB(x, y)$$
$$\equiv \forall xA(x) \land \exists x \forall yB(x, y) \text{ (Double Negation law)} $$

Therefore $$\neg (\forall xA(x) \to \forall x \exists y \neg B(x, y))$$ is logically equivalent to $$\forall xA(x) \land \exists x \forall y B(x, y)$$

\break

\section*{Question 3}
Is the sentence
$$(\exists xQ(x) \land \exists xR(x)) \to \exists x(Q(x) \land R(x))$$
valid?

The expression is not valid and is False. For this to be False the LHS must be True and the RHS must be True.

LSH: $(\exists xQ(x) \land \exists xR(x))$

RSH: $(\exists x(Q(x) \land R(x))$

An implementation of this where this holds is $Q(x) \text{ is } x = 0$ and $R(x) \text{ is } x = 1$. The LHS of this is True as there exists a $x$ for $x=0$ and there exists a $x$ for $x=1$. The RHS is False because there doesn't exist a $x$ for which both $x=0$ and $x=1$.

\break

\section*{Question 4}
Prove using simple induction that, for each integer $n \geq 1$,
$$5+5^2+5^3+\dots + 5^n = \frac{5^{n+1}-5}{4}$$

Let $P(n)$ be the statement $5+5^2+5^3+\dots + 5^n = \frac{5^{n+1}-5}{4}$

\emph{Base step.} The left hand side of $P(1)$ is 5 and the right hand side of $P(1)$ is $\frac{5^{1+1}-5}{4} = \frac{5^2-5}{4}=\frac{25-5}{4} = \frac{20}{4} = 5$. Therefore the base case is true.

\emph{Induction step.} For some integer $k \geq 1$, assume that $P(k)$ is true. That is assume that
$$5 + 5^2 + 5^3 + \dots + 5^k = \frac{5^{k+1}-5}{4}$$

Now we need to prove that $P(k+1)$ is true. So we must show that
$$5 + 5^2 + 5^3 + \dots + 5^{k+1} = \frac{5^{k+1+1}-5}{4}$$

Working with the left hand side of the equation we see that

\begin{align*}
	5+5^2+5^3+ \dots + 5^{k+1} &= (5 + 5^2 + 5^3 + \dots + 5^k) + 5^{k+1} \\
	&= \frac{5^{k+1}-5}{4} + 5^{k+1} \text{ (using our assumption)} \\
	&= \frac{5^{k+1}-5}{4} + \frac{20^{k+1}}{4} \\
	&= \frac{25^{k+1}-5}{4} \\
	&= \frac{5(5^{k+1}-1)}{4} \\
	&= \frac{5(1^{k+1+1}-1)}{4} \\
	&= \frac{5^{k+2}-5}{4}
\end{align*}

Which is the right hand side we required. Thus $P(k+1)$ is true. So we have proved by induction that $5+5^2+5^3+\dots + 5^n = \frac{5^{n+1}-5}{4}$ for each integer $n \geq q$.

\end{document}


\problemname{Disposable Switches}

\illustration{0.42}{cables_small}{\href{https://flic.kr/p/7AMDgK}{Picture} by Ted Sakshaug on Flickr, cc by}%
\noindent
Having recently been hired by Netwerc Industries as a network engineer, your
first task is to assess their large and dated office network. After mapping out
the entire network, which consists of network switches and cables
between them, you get a hunch that, not only are some of the switches
redundant, some of them are not used at all! Before presenting this to
your boss, you decide to make a program to test your claims.

The data you have gathered consists of the map of the network, as well as the
length of each cable. While you do not know the exact time it takes to send a
network packet across each cable, you know that it can be calculated as
$\ell / v + c$, where
\begin{itemize}
    \item $\ell$ is the length of the cable,
    \item $v$ is the propagation speed of the cable, and
    \item $c$ is the overhead of transmitting the packet on and off the cable.
\end{itemize}
You have not been able to measure $v$ and $c$.  The only thing you know about
them is that they are real numbers satisfying $v > 0$ and $c \ge 0$, and that
they are the same
for all cables. You also know that when a network packet is being transmitted
from one switch to another, the network routing algorithms will ensure that the
packet takes an \emph{optimal path}---a path that minimises the total transit
time.

Given the map of the network and the length of each cable, determine which
switches could never possibly be part of an optimal path when transmitting a network packet from
switch $1$ to switch $n$, no matter what the values of $v$ and $c$ are.

\section*{Input}
The input consists of:
\begin{itemize}
    \item One line with two integers $n$, $m$ ($2 \leq n \leq 2\,000$, $1 \leq m \leq 10^4$), the number
    of switches and the number of cables in the network. The switches are numbered
    from $1$ to $n$.
    \item $m$ lines, each with three integers $a$, $b$ and $\ell$ ($1 \leq a, b
    \leq n$, $1\leq \ell \leq 10^9$), representing a network cable of length
    $\ell$ connecting switches $a$ and $b$.
\end{itemize}

There is at most one cable connecting a given pair of switches, and no cable
connects a switch to itself. It is guaranteed that there exists a path between
every pair of switches.

\section*{Output}
First output a line with an integer $k$, the number of switches that could never possibly be
part of an optimal path when a packet is transmitted from switch $1$ to switch $n$.
Then output a line with $k$ integers,
the indices of the $k$ unused switches, in increasing order.


\section{Ballot-polling audits of a tolerable overstatement in votes}
\label{sec:ballotPollError}

In this section we develop a new method for ballot-polling audits that can test numerical margins,
rather than just test whether a candidate won.
This requires a different approach than that taken by \cite{lindemanEtal12}.

Existing ballot-polling methods consider only the fraction of ballots with a vote for either
$w$ or $\ell$ that contain a vote for $w$,
making the statistical test one for a proportion.
To allow the error to be partitioned across the strata via $\lambda_s$,
the necessary inference is about the
\emph{difference} between the number of votes for $w$ and the number of votes for $\ell$.
This introduces a nuisance parameter, the number of ballots with votes for either $w$ or $\ell$.
We deal with the nuisance parameter by maximizing the $P$-value
over all possible values of the nuisance parameter, which ensures that the test is conservative.

\subsection{Conditional tri-hypergeometric test}

We consider a single stratum $s$, containing $N_s$ ballots.
Of the $N_s$ ballots,
$A_{w,s}$ have a vote for $w$ but not for $\ell$, $A_{\ell,s}$ have a vote for $\ell$ but not for $w$, and $A_{u,s} = N_s - N_{w,s} - N_{\ell,s}$ have votes for both $w$ and $\ell$ or neither $w$ nor $\ell$, including undervotes and invalid ballots.
We might draw a simple random sample of $n$ ballots ($n$ fixed ahead of time), or we might draw
sequentially without replacement, so the sample size $B$ could be random.
For instance, the rule for determining $B$ could depend on the data.\footnote{%
   Sampling with replacement leads to simpler arithmetic, but is not as efficient.
}

Regardless, we assume that, conditional on the attained sample size $n$, the ballots are a simple random sample of size $n$ from the $N_s$ ballots in the population.
In the sample, $B_w$ ballots contain a vote for $w$ but not $\ell$, with $B_\ell$ and $B_u$ defined analogously.
The conditional joint distribution of
$(B_w, B_\ell, B_u)$ is tri-hypergeometric:

\begin{equation}
    \mathbb{P}_{A_{w,s}, A_{\ell,s}} \{ B_w = i, B_\ell = j \vert B=n \} =
     \frac{ {A_{w,s } \choose i}{A_{\ell,s} \choose j}{N_s - A_{w,s} - A_{\ell,s} \choose n-i-j}}{{N_s \choose n}}.
\end{equation}

Define the diluted sample margin, $D \equiv (B_w - B_\ell)/B$.
We want to test the compound hypothesis $A_{w,s} - A_{\ell,s} \le c$.
The value of $c$ is inferred from the definition
$\omega_{w\ell,s} \equiv V_{w\ell,s} - A_{w\ell,s} = V_{w,s} - V_{\ell,s} - (A_{w,s} -A_{\ell,s})$.
Thus,

\beq
    c = V_{w,s} - V_{\ell,s} - \omega_{w\ell,s} = V_{w\ell,s} - \lambda_s V_{w\ell}.
\eeq

The alternative is the compound hypothesis
$A_{w,s} - A_{\ell,s} > c$.\footnote{%
    To use Wald's Sequential Probability Ratio Test, we might pick a simple alternative instead, e.g.,
   $A_{w,s} = V_{w,s}$ and $A_{\ell,s} = V_{\ell,s}$, the reported values, provided
   $V_{w,s} - V_{\ell,s} > c$.
}
Hence, we will reject for large values of $D$.
Conditional on $B=n$, the event $D = (B_w - B_\ell)/B = d$ is the same as $B_w - B_\ell = nd$.\footnote{%
In contrast, the BRAVO ballot-polling
method~\cite{lindemanEtal12}
conditions only on $B_w+B_\ell = m$.
}

The $P$-value of the simple hypothesis that there are $A_{w,s}$ ballots with
a vote for $w$ but not for $\ell$, $A_{\ell,s}$ ballots with a vote for $\ell$ but not for $w$,
and $N - A_{w,s} - A_{\ell,s}$ ballots with votes for both $w$ and $\ell$ or neither $w$ nor $\ell$
(including undervotes and
invalid ballots) is the probability that $B_w - B_\ell \geq nd$.
Therefore,

\begin{equation}
   \mathbb{P}_{A_{w,s}, A_{\ell,s}, N_s} \left \{ D \geq d \;\vert\; B = n\right \} =
   \sum_{\substack{(i, j) :  i, j\ge 0 \\ i-j \geq nd \\ i+j \leq n}} \frac{ {A_{w,s } \choose i}{A_{\ell,s} \choose j}{N_s - A_{w,s} - A_{\ell,s} \choose n-i-j}}{{N_s \choose n}}.
\end{equation}

%\subsection{Conditional hypergeometric test}
%Another approach is to condition on both the events $B=n$ and $B_w+B_\ell=m$.
%We describe the hypothesis test here, but do not advocate for using it.
%We found that this approach was inefficient in some simulation experiments.
%
%Given $B=n$, all samples of size $n$ from the ballots are equally likely, by hypothesis.
%Hence, in particular, all samples of size $n$ for which $B_w + B_\ell = m$ are equally likely.
%There are ${A_{w,s}+A_{\ell,s} \choose m}{N_s - A_{w,s}-A_{\ell,s} \choose n-m}$ such samples.
%Among these samples, $B_w$ may take values $i=0, 1, \dots, m$.
%For a fixed $i$, there are ${A_{w,s} \choose i}{A_{\ell, s} \choose m-i}{N_s - A_{w,s} - A_{\ell,s} \choose n-m}$
%samples with $B_w=i$ and $B_\ell = m-i$.
%
%The factor ${N_s - A_{w,s} - A_{\ell,s} \choose n-m}$ counts the number of ways to sample $n-m$ of the
%remaining ballots.
%If we divide out this factor, we simply count the number of ways to sample ballots
%from the group of ballots for $w$ or for $\ell$.
%There are ${A_{w,s}+A_{\ell,s} \choose m}$ equally likely samples of size $m$ from
%the ballots with either a vote for $w$ or for $\ell$, but not both,
%and of these samples, ${A_{w,s} \choose i}{A_{\ell, s} \choose m-i}$ contain $i$ ballots with a vote for $w$ but not $\ell$.
%Therefore, conditional on $B=n$ and $B_w+B_\ell=m$, the probability that $B_w=i$ is
%
%$$\frac{{A_{w,s} \choose i}{A_{\ell, s} \choose m-i}}{{A_{w,s}+A_{\ell,s} \choose m}}.$$
%
%The $P$-value of the simple hypothesis that there are $A_{w,s}$ ballots with
%a vote for $w$ but not for $\ell$, $A_{\ell,s}$ ballots with a vote for $\ell$ but not for $w$,
%and $N - A_{w,s} - A_{\ell,s}$ ballots with votes for both $w$ and $\ell$ or neither $w$ nor $\ell$
%(including undervotes and
%invalid ballots) is the sum of these probabilities for events when $B_w - B_\ell \geq nd$.
%This event occurs for $B_w \geq \frac{m+nd}{2}$.
%Therefore,
%
%\begin{equation}
%   \mathbb{P}_{A_{w,s}, A_{\ell,s}, N_s} \left \{ D \geq d \;\vert\; B = n, B_w+B_\ell = m \right \} =
%   \sum_{i=(m+nd)/2}^{\min\{m, A_{w,s}\}} \frac{{A_{w,s} \choose i}{A_{\ell, s} \choose m-i}}{{A_{w,s}+A_{\ell,s} \choose m}}.
%\end{equation}
%
%
%This conditional $P$-value is thus the tail probability of the hypergeometric distribution
%with parameters $A_{w,s}$ ``good'' items, $A_{\ell,s}$ ``bad'' items, and a sample of size $m$.
%This calculation is numerically stable and fast; tail probabilities of the hypergeometric distribution are available
%and well-tested in all standard statistics software.

\subsection{Maximizing the $P$-value over the nuisance parameter}

The composite null hypothesis does not specify $A_{w,s}$ or $A_{\ell,s}$ separately, only
that $A_{w,s} - A_{\ell,s} \le c$ for
some fixed, known $c$.
Define $\mathcal{S}$ to be the set of pairs $(i, j)$ such that $i, j\ge 0, i-j \ge nd,$ and $ i+j \leq n$.
The (conditional) $P$-value of this composite hypothesis for $D=d$ is the maximum $P$-value for all
values $(A_{w,s}, A_{\ell,s})$ that are possible under the null hypothesis,
\begin{equation}
  \max_{A_{w,s}, A_{\ell,s} \in \{0, 1, \ldots, N \}: A_{w,s} - A_{\ell,s} \le c, A_{w,s} + A_{\ell,s} \le N_s}
   \sum_{\substack{(i, j)\in \mathcal{S}}} \frac{ {A_{w,s } \choose i}{A_{\ell,s} \choose j}{N_s - A_{w,s} - A_{\ell,s} \choose n-i-j}}{{N_s \choose n}},
\end{equation}
wherever the summand is defined.
(Equivalently, define ${m \choose k} \equiv 0$ if $k > m$, $k < 0$, or $m \le 0$.)

\subsubsection{Characterizing the optimal solution}
The following result enables us to only test hypotheses along the boundary of the null set.

\begin{thm}
Assume that $n < A_{w,s}+A_{\ell,s}$.
Suppose the composite null hypothesis is $N_w - N_\ell \leq c$.
The $P$-value is maximized on the boundary of the null region, i.e. when $N_w - N_\ell = c$.
\end{thm}

\begin{proof}
Without loss of generality, let $c=0$ and assume that $A_{u,s}=N_s - A_{w,s} - A_{\ell,s}$ is fixed.
Let $N_{w\ell, s} \equiv A_{w,s}+A_{\ell,s}$ be the fixed, unknown number of ballots for $w$ or for $\ell$ in stratum $s$.
The $P$-value $p_0$ for the simple hypothesis that $c=0$ is

\begin{equation}
  p_0 = \sum_{\substack{(i, j) \in \mathcal{S}}} \frac{ {N_{w\ell, s}/2 \choose i}{N_{w\ell, s}/2 \choose j}{A_{u,s} \choose n-i-j}}{{N_s \choose n}} =  \sum_{\substack{(i, j) \in \mathcal{S}}}T_{ij},
\end{equation}

\noindent where $T_{ij}$ is defined as the $(i, j)$ term in the summand and $T_{ij} \equiv 0$ for pairs $(i, j)$ that don't appear in the summation.

Assume that $c>0$ is given.
The $P$-value $p_c$ for this simple hypothesis is
\begin{align*}
p_c &=   \sum_{\substack{(i, j) \in \mathcal{S}}}  \frac{ {(N_{w\ell, s}+c)/2 \choose i}{(N_{w\ell, s}-c)/2 \choose j}{A_{u,s} \choose n-i-j}}{{N_s \choose n}}  \\
   &=\sum_{\substack{(i, j) \in \mathcal{S}}} T_{ij} \frac{ \frac{N_{w\ell, s}+c}{2}(\frac{N_{w\ell, s}+c}{2}-1)\cdots(\frac{N_{w\ell, s}}{2}+1) (\frac{N_{w\ell, s}-c}{2} -j)\cdots(\frac{N_{w\ell, s}}{2}-1-j) }
   {(\frac{N_{w\ell, s}+c}{2} -i)\cdots(\frac{N_{w\ell, s}}{2}+1-i)(\frac{N_{w\ell, s}-c}{2})\cdots(\frac{N_{w\ell, s}}{2}-1)}.
\end{align*}

Terms in the fraction can be simplified: choose the corresponding pairs in the numerator and denominator.
Fractions of the form $\frac{\frac{N_{w\ell, s}}{2} + a}{\frac{N_{w\ell,s}}{2} + a - i}$ can be expressed as $1 + \frac{i}{\frac{N_{w\ell,s}}{2} + a-i}$.
Fractions of the form $\frac{\frac{N_{w\ell, s}}{2}  - a - j}{\frac{N_{w\ell, s}}{2}  - a}$ can be expressed as $1 - \frac{j}{\frac{N_{w\ell, s}}{2} -a}$.
Thus, the $P$-value can be written as

\begin{align*}
p_c &= \sum_{\substack{(i, j) \in \mathcal{S}}} T_{ij} \prod_{a=1}^{c/2} \left(1 + \frac{i}{\frac{N_{w\ell,s}}{2} + a-i}\right)\left(1 - \frac{j}{\frac{N_{w\ell, s}}{2} - a}\right) \\
&> \sum_{\substack{(i, j) \in \mathcal{S}}}  T_{ij} \left[ \left(1 + \frac{i}{\frac{N_{w\ell,s}+c}{2} -i}\right)\left(1 - \frac{j}{\frac{N_{w\ell, s}}{2}+1}\right) \right]^{c/2} \\
&= \sum_{\substack{(i, j) \in \mathcal{S}}} T_{ij} \left[ 1 + \frac{\frac{N_{w\ell,s}+c}{2}j + \frac{N_{w\ell,s}}{2}i + i}{(\frac{N_{w\ell,s}+c}{2}-i)(\frac{N_{w\ell,s}}{2}+1)}\right]^{c/2} \\
&> \sum_{\substack{(i, j) \in \mathcal{S}}}  T_{ij}\\
&= p_0
\end{align*}

The last inequality follows from the fact that $i$ and $j$ are nonnegative, and
that $i < \frac{N_{w\ell,s}+c}{2}$ (it is a possible outcome under the null hypothesis).

\end{proof}

\subsubsection{Solving the optimization problem}

We have found empirically (but have not proven) that given $N$, $c$, and the observed sample values $B_w$ and $B_\ell$, the tail probability $p_c$, as a function of $A_{w,s}$,
has a unique maximum at one of the endpoints, where $A_{w,s}$ is either as small or as large as possible.
If this empirical result is true in general, then finding the maximum is trivial;
otherwise, computing the unconditional $P$-value is a simple 1-dimensional optimization problem
on a bounded interval.

\subsection{Conditional testing}
If the conditional tests are always conducted at significance level $\alpha$ or less, so that
$\mathbb{P} \{\mbox{Type I error} | B = n\} \le \alpha$, then the
overall procedure has significance level $\alpha$ or less:
\begin{eqnarray}
    \mathbb{P} \{\mbox{Type I error}\} &=& \sum_{n=0}^N  \mathbb{P}\{\mbox{Type I error} |  B = n\} \mathbb{P} \{ B = n \} \nonumber \\
       & \le & \sum_{n=0}^N \alpha \mathbb{P} \{  B = n \}  =  \alpha.
\end{eqnarray}

In particular, this implies that our conditional hypergeometric test will have a conservative $P$-value  unconditionally.


%!TEX root = ../notes.tex
\section{April 20, 2022}
\subsection{Short Vectors}
\begin{ques*}
    How short is the shortest vector in a lattice $L$?
\end{ques*}

A more general question we could ask:
\begin{ques*}
    When does some region contain a nontrivial lattice point?
\end{ques*}

\begin{theorem}[Minkowski's Theorem]
    Let $L\subseteq \RR^n$ be a lattice of dimension $n$. Let $S\subseteq\RR^n$ be a \emph{bounded symmetric convex} set.

    If $\mathsf{Vol}(S)>2^n\det(L)$, then $S\cap L$ contains a nonzero lattice point.
\end{theorem}

\begin{definition}[Bounded Set]
    $\{\text{Lengths of vectors in }S\}$ is bounded.

    In other words, there is some ball that contains $S$.
\end{definition}

\begin{definition}[Symmetric Set]
    If $\bvec{v}\in S$, then $-\bvec{v}\in S$.
\end{definition}

\begin{definition}[Convex Set]
    If $\bvec{v}, \bvec{w}$, then the line segment connecting $\bvec{v}$ and $\bvec{w}$ is a subset of $S$.
\end{definition}

\begin{proof}[Proof of Minkowski's Theorem]
    Let $\mathcal{F}$ be a fundamental domain.

    Any vector $\bvec{w} = t(\bvec{w}) + v(\bvec{w})$ where $t(\bvec{w})\in \mathcal{F}$ and $v(\bvec{w})\in L$.

    Consider map $t: \frac{1}{2}S \mapsto F$ by sending every vector $t: \bvec{w}\mapsto t(\bvec{w})$.

    What does $t$ do to volume? We cut $S$ up into finite number of regions, and `cut-and-paste' them into the fundamental domain.

    Locally, $t$ preserves volume. When must two points in $\frac{1}{2}S$ be sent to the same point in $F$? When we have `carpet' with area greater than room area. That is to say, $\mathsf{Vol}(\frac{1}{2} S) > \mathsf{Vol}(\mathcal{F})$ implies that there is an overlapping point.

    This is to say
    \begin{align*}
        \mathsf{Vol}\left(\frac{1}{2} S\right) & > \mathsf{Vol}(\mathcal{F})           \\
        \frac{1}{2^n}\mathsf{Vol}(S)           & > \mathsf{Vol}(\mathcal{F}) = \det(L) \\
        \mathsf{Vol}(S)                        & > 2^n \det(L)
    \end{align*}

    So given this inequality, there are two points in $\frac{1}{2}S$ such that $t\left( \frac{1}{2}\bvec{w}_1 \right) = t\left( \frac{1}{2}\bvec{w}_2 \right)$. Then we know that
    \[\frac{1}{2}\bvec{w}_1 - \frac{1}{2}\bvec{w}_2 \in L\]
    So then consider
    \[\frac{1}{2}\bvec{w}_1 - \frac{1}{2}\bvec{w}_2 = \frac{1}{2}(\bvec{w}_1 - \bvec{w}_2)\]
    which is the midpoint of $\bvec{w}_1$ and $\bvec{w}_2$, which is in $S$ and in $L$. So $S$ contains a nonzero lattice point.
\end{proof}

\begin{theorem}[Variant of Minkowski's Theorem]
    If $S\subseteq \RR^n$ is bounded, symmetric, convex and \emph{closed} set, then if
    \[\mathsf{Vol}(S)\geq 2^n\det(L)\]
    $S\cap L$ contains a nonzero lattice point.
\end{theorem}
\begin{definition}[Closed Set]
    Every limit point of $S$ is contained in $S$.
\end{definition}
\emph{We added the condition that $S$ be closed, and changed our bound to be a $\geq$. }
\begin{proof}[Proof of variant]
    For any $k$:
    \[\left( 1 + \frac{1}{k} \right)S \cap L\]
    contains $\bvec{v}_k\neq \bvec{0}\in L$ (which is true by our first version).

    The sequence $\bvec{v}_1, \bvec{v}_2, \bvec{v}_3, \dots$ is a sequence in $2S\cap L$. $2S$ is bounded, so we have a finite set of lattice points. There's some $\bvec{v}\neq \bvec{0}$ is contained in $\bigcap_{k}\left( 1 + \frac{1}{k} \right) S = S$ because $S$ is closed.
\end{proof}
\begin{corollary}[Hermite's Theorem]
    Let $L$ be a lattice of dimension $n$ in $\RR^n$. Then, $L$ contains a vector $\bvec{v}$ with
    \[||\bvec{v|}|\leq \sqrt{n}\cdot \det(L)^\frac{1}{n}\]
\end{corollary}
\begin{proof}
    \emph{Application of Minkowski's Theorem.} Apply Minkowski's Theorem to
    \[\left\{ (x_1, \dots, x_n)\Bigm\vert |x_i|\leq \det(L)^{1/n} \right\}\]
    which is a cube with side length $2\cdot \det(L)^{1/n}$. So $\textsf{Vol}(S) = 2^n\cdot \det(L)$. The diagonal has length $\sqrt{n}\det(L)^{1/n}$.
\end{proof}
A variant of Hermite's Theorem is that we can find an entire basis $\bvec{v}_1, \bvec{v}_2, \dots, \bvec{v}_n$ such that
\[||\bvec{v}_1||\cdot||\bvec{v}_2||\cdots ||\bvec{v}_n|| \leq n^{n/2}\det(L)\]
and we define the Hadamard ratio to be
\[\mathcal{H} = \left( \frac{\det(L)}{||\bvec{v}_1||\cdot||\bvec{v}_2||\cdots ||\bvec{v}_n||} \right)^{1/n}\]
where $0< \mathcal{H} \leq 1$ and $\mathcal{H} = 1$ when our basis is orthogonal.