\documentclass[11pt,onecolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\newcommand{\fm}{A}
\newcommand{\FM}{\mathbf{A}}
\newcommand{\x}{\mathbf{x}}

\title{Notes on FET for fission eigenvalue computations}
\author{James Paul Holloway}

\begin{document}

\maketitle

\section{Introduction}

The $k$-eigenvalue problem is
\begin{equation}
T \psi = \frac{1}{k} F \psi
\end{equation}
where $T$ is transport, removal, inscatter and boundary conditions, and $F$ is the fission operator.  This can be written as $\psi = (1/k) T^{-1} F \psi$, and applying $F$ to this gives $F \psi = (1/k) F T^{-1} F \psi$.

So let the $k$-eigenvalue problem be written as
\begin{equation}
G q = k q
\end{equation}
where $q$ is the fission source and the linear operator $G = F T^{-1}$ first transports source particles and then computes a fission source from the resulting particle population.  The action of $G$ can be computed, in some sense, using Monte Carlo.  What Monte Carlo really can do is sample a fission source $q$ and from this compute a sample of the distribution $G q$.  Typically this sample is a set of locations stored in a ``fission bank'', from which the outgoing, next generation, fission neutrons can again be sampled.

The fission source $q$ lives in a linear space $Q$, and $G: Q \to Q$.
Let $P: Q \to T$ be a projection operator onto a finite dimensional linear subspace $T$ of $Q$.  Because this operator is to be a projection, $P^2 = I$, and because it is a projection onto $T$, $P q = q$ for any function $q \in T$.  The space $T$ might be a set of piecewise constant functions on a uniform grid, which we normally call the histogram tally.  Or $T$ might represent the span of some other set of basis functions that give another functional expansion tally.  For example, $T$ might be spanned by a set of Legendre polynomials.  More usefully, $T$ might be a space of piecewise linear functions in space, each of which is a fission spectrum in energy.\footnote{Because we are only going to expand the fission source, we do not need much richness to expand in energy.  It could be a generic fission spectrum, or a linear combination of fission spectra from key fissioners, e.g. U-235, U-238 and Pu-240.}

We can use the projection $P$ to approximate the eigenvalue problem as
\begin{equation}
PGP q = k q
\end{equation}
where $PGP = \fm : T \to T$.  We call the linear operator $A$ the \emph{fission matrix} (at some danger of confusing the operator with any particular matrix representation of it).

The quality of this approximation is apparently determined by the error in approximating the fundamental eigenmode by an element of $T$.  If $q_0$ is the fundamental, and if $q_0 \in T$, then $PGP q_0 = PGq_0 = k_0 P q_0 = k q_0$ and so $PGP$ has the fundamental mode as an eigenfunction, with the correct eigenvalue.  Less clear is the question: is $k_0$ the fundamental (largest) eigenvalue of $PGP$ just because it is the largest eigenvalue of $G$?  (Lots of unanswered questions here about truncation error and related issues on eigenvalue.)

\subsection{The fission matrix}

Let $\{p_n\}$ be a basis for $T$, and let $\{p^\dagger_n\}$ be the biorthonormal basis defined such that $\langle p^\dagger_n, p_m\rangle = \delta_{n,m}$.  Then we can construct the action of $P: G \to T$ as $Pq = \sum_n p_n \langle p_n^\dagger, q \rangle$.  For any $q \in T$ we can write $q = \sum_n x_n p_n$ for scalars $x_n= \langle p_n^\dagger, q\rangle$, and so for $q \in T$
\begin{align}
\fm q &= PGP q = P G q = P \sum_n x_n G p_n \\
&= \sum_m p_m \bigl\langle p_m^\dagger, \sum_n x_n G p_n \bigr\rangle \\
&= \sum_m p_m \sum_n x_n \langle p_m^\dagger, G p_n \rangle
\end{align}
The inner sum is a matrix multiply of the matrix with elements $A_{m,n} = \langle p_m^\dagger, G p_n \rangle$ on the vector with elements $x_n$.  This inner sum
\begin{equation}
y_m = \sum_n x_n \langle p_m^\dagger, G p_n \rangle
\end{equation}
gives the vector elements $y_m$ that represent the value of $A q$ in the basis.
So now define the matrix $\FM$ with matrix elements $A_{m,n} = \langle p_m^\dagger, G p_n \rangle$, and consider the vector $\x$ whose elements are $x_n$.  Then the action of $\fm$ on $q$ is contained in the matrix multiplication $\FM  \x$.

\subsection{Interpretation of the fission matrix}

Consider the case where $T$ is spanned by a histogram basis (and we ignore energy for the moment). In this case the dual basis functions are selected to be
\begin{equation}
 p_n^\dagger =
 \begin{cases}
   1 & x \in \text{bin $n$}\\
   0 & x \notin \text{bin $n$}
 \end{cases}
\end{equation}
and the basis functions
\begin{equation}
 p_n = \frac{p_n^\dagger}{\text{Vol}(n)}
\end{equation}
where $\text{Vol}(n)$ is the volume of bin $n$. The source $q$ is thereby represented by the bin values $x_n$, which can be interpreted physically as the number of fission neutrons born into a bin $n$ because $\langle p_n^\dagger, q\rangle$ is just the integral of the fission source $q$ over the bin.

Suppose all $x_{n'} = 0$ for $n' = 1, 2 \ldots, n' \ne n $, except for one bin with unit source $x_n = 1$; then $\FM \x$ gives the binned fission source due to the unit strength fission source in bin $n$.  In this fission source $A_{m,n}$ is the fission source induced in bin $m$ by a unit fission source in bin $n$. Column $n$ of the fission matrix $\FM$ is the fission source induced by a unit strength fission source in bin $n$.  All of these matrix elements are non-negative.

Note that $A_{m,n}$ \emph{is not} the probability of a fission neutron being born in cell $m$ due to a fission neutron born in cell $n$.   That probability is
\begin{equation}
P_{m,n} = \frac{A_{m,n}}{\sum_m A_{m,n}} \,,
\end{equation}
with the understanding that $P_{m,n} = 0$ if the numerator is zero (the issue arises only if $\sum_m A_{m,n} = 0$).
Introducing the diagonal matrix $\mathbf{N}$ with diagonal elements
\begin{equation}
N_{n,n} = {\sum_m A_{m,n}} = \text{total number of fiss. neutrons induced by a fiss. neutron from bin $n$}
\end{equation}
we can write $\FM = \mathbf{P} \mathbf{N}$.

It's not immediately clear to me what $\mathbf{P}$ is good for, that $\FM$ is not.

It's tempting to try to relate $N_{n,n} = {\sum_m A_{m,n}}$ to $\nu$, the mean number of neutrons produced in fission, but that does lot look fruitful.  $\nu$ can vary with space, so which value of $\nu$ would be relevant?  $\nu$ is the number of neutrons produced \emph{if a fission occurs}.  By contrast, $N_{n,n}$ is the number of fission neutrons induced by one source neutron; it includes the probability that a fission occurs, and could in principle be zero.  A small bit of fissile material that's 10 km away from the core will yield a very small value for $N_{n,n}$!

\subsection{Interpretation of the fission matrix--Redux}

More generally, $A_{m,n}$ is a coefficient in the FET expansion of one generation's fission source in mode $m$, induced by a unit expansion coefficient for mode $n$ in the previous generation.   As such, $A_{m,n}$ can be positive or negative, and cannot be scaled to be interpreted as a probability.  It does not seem fruitful to try.

\section{Working with $\FM$}

Suppose we need to compute $\FM \x$, as we do in eigenvalue iterations like the power method and Arnoldi's method.  If we had precomputed all the matrix elements then we could do this by a matrix-vector multiply.  But if we have a fine mesh with $N$ modes per dimension, then this matrix may be very large, some matrix elements may be hard to estimate well.  But we can compute $\FM x$ in another way, without storing the matrix.

Observe that we can sample the distribution $q = \sum_n x_n p_n$.  We can then transport this source particle, accumulating its contributions to a FET expasion of a fission source $q'$ at each interaction.  The expansion coefficients of this are unbiased estimates of the elements $y_n$ of the vector $\mathbf{y} = \FM \x$.

In this way, we can compute the action of the matrix on a vector, without ever computing or storing the matrix.  FET modes that are statistically not significantly different from zero can be automatically dropped.

If we compute the matrix elements, using $M$ FET modes, then we must do $M$ Monte Carlo runs with $M$ tallies each, and store $M \times M$ matrix elements.  Further, some of the coefficients may not be well computed because they are not easily excited by the source $q$.  The eigenvalue iteration will then require $M^2$ work for the matrix-vector multiply, per iteration.  So the cost will be like (ignoring constants)
\begin{equation}
M \times \text{Monte Carlo Cost} + I (M^2 + \text{iteration cost})
\end{equation}
where $I$ is the number of eigenvalue iterations and ``iteration cost'' is the rest of the cost of an iteration (exclusive of the cost of computing $\FM \x$).
For example, in Arnoldi, ``iteration cost'' is the cost of orthogonalizing the latest matrix-vector product against the previous basis vectors.
In contrast,
if we compute the action of $\FM$ directly, we need to do only one Monte Carlo run with $M$ tallies per iteration.   We will save the cost $M^2$ of matrix-vector multiplies on each iteration, and the cost will be
\begin{equation}
I (\text{Monte Carlo Cost} + \text{iteration cost})
\end{equation}
We want to use direct Monte Carlo computation of $\FM \x$ when
\begin{equation}
I (\text{Monte Carlo Cost} + \text{iteration cost})
< M \times \text{Monte Carlo Cost} + I (M^2 + \text{iteration cost})
\end{equation}
or, equivalently,
\begin{equation}
I (\text{Monte Carlo Cost} - M^2)
< M \times \text{Monte Carlo Cost}
\end{equation}
If the cost of the Monte Carlo is less than the cost of a matrix-vector multiply ($\text{Monte Carlo Cost} < M^2$), then we win.  In the more likely case where the Monte Carlo is very expensive compared to one matrix-vector multiply ($\text{Monte Carlo Cost} \gg M^2$) and $I \ll M$ then we again win.  The intermediate case is not so clear.  Arnoldi requires at worst $I = M$, in which case we win; but the impact of statistical noise and roundoff makes this case unclear.

\section{The Arnoldi Algorithm}

The Arnoldi algorithm is a way to generate a Krylov subspace, a set of orthogonal basis vectors for it, and a projection of a matrix onto the subspace in the form of a upper Hessenburg matrix $H$.   The eigenvalues of $H$ often approximate the extreme eigenvalues of $\FM$.  The algorithm could be driven by Monte Carlo like this:

\begin{itemize}
\item Select initial fission source $q_0$
\item Repeat for $k = 2, 3, 4 \ldots $

\begin{itemize}
\item Compute new fission source $q_k = \FM q_0$ by Monte Carlo (sample $q_0$ from its FET and compute the FET expansion coefficients of the new fission source)
\item for $j$ from $1$ to $k - 1$
\begin{itemize}
  \item $h_{j, k-1} = \langle q_j, q_k \rangle$ (Compute matrix elements)
  \item $q_k = q_k - h_{j,k-1} q_j$ (Orthoganalize)
\end{itemize}
\item $h_{k,k-1} = \| q_k \|$
\item $q_k = \frac{q_k}{h_{k,k-1}}$
\end{itemize}

\end{itemize}

This is exactly the standard Arnolid process.  Only one step, computing $\FM q_k$ requires Monte Carlo.  It requires sampling an arbitrary source (which will contain both positive and negative source densities) that is described in terms of its FET coefficients (the components of the vector $q_k$).  The result should be the FET coefficients of the new fission source.   In the case of a binned FET with constant or linear expansions within each bin it is easy to tell where the source is positive or negative, and so easy to carry out the needed sampling.

The eigenvalues of $H$ are then used to approximate the eigenvalues of $\FM$.

\end{document}


\section*{Postulates}
\begin{enumerate}
\item States describe the system
\item States evolve
\item States yield Probabilistic measurements
\item Systems can be composite states
\end{enumerate}
\emph{Notation}
\begin{itemize}
\item [$U$:] Unitary operator
\item [$M_m$:] Measurement operator with outcome $m$
\item [$p(m)$:] Probability to measure outcome $m$
\item [$p_i$:] Ensemble probability to be in the $i$th state
\end{itemize}

\subsection*{Vector States}
\begin{itemize}
\item [state:] $\ket{\psi}$
\item [evolution:] $\ket{\psi} \Rightarrow U\ket{\psi}$
\item [measurement:] $p(m) = \expectationvalue{M^\dagger_m M_m}{\psi}$ and
  $\ket{\psi_m} = M_m\ket{\psi} / p(m)$
\item [composition:] $\ket{\psi} = \ket{\psi_1} \otimes \ket{\psi_2} \otimes
  \dotsm \otimes \ket{\psi_n}$
\end{itemize}
\subsection*{Ensemble States}
\begin{itemize}
\item [state:] $\rho = \sum_i p_i\outerproduct{\psi_i}$
\item [evolution:] $\rho \Rightarrow \sum_i p_iU\outerproduct{\psi_i}U^\dagger$
\item [measurement:] $p(m) = \sum_i p_i \trace(M^\dagger_m M_m\outerproduct{\psi_i})$ and
  $\rho_m = \sum_i p_i M_m\outerproduct{\psi_i} M_m^\dagger/ p(m)$
\item [composition:] $\rho = \sum_i p_i \rho_i = \sum_i p_i \sum_j p_{ij}\outerproduct{\psi_{ij}}$
\end{itemize}
\subsection*{Density Operator}
\begin{itemize}
\item [state:] $\rho = \sum_i p_i\rho_i$
\item [evolution:] $\rho \Rightarrow U\rho U^\dagger$
\item [measurement:] $p(m) = \trace(M^\dagger_m M_m\rho)$ and
  $\rho_m = M_m\rho M^\dagger_m / p(m)$
\item [composition:] $\rho = \rho_1\otimes \rho_2\otimes \dotsm \otimes\rho_n$
\end{itemize}


%!TEX root =  ../main.tex

\marginfig[1in]{\chapdir/pics/Factor_Tree_of_42.png}{Prime decomposition}
\section{Factoring and Primes}\label{sec:primes}
Humans seeks to understand the fundamental nature of numbers.  We have learned
many things, but the underlying, deep structure is not fully known.  We believe will know
a great deal more if we can understand the distribution of Prime Numbers.

\begin{derivation}{Prime Number}
A natural number greater than 1 that has no positive divisors other than 1 and itself.
\end{derivation}

Every number is either prime or can be written as the product of primes.  Primes turn up
in many unexpected places, such as the infinite sums of the Zeta function, or the infinite
list of all possible Pythagorean Triples.

\begin{derivation}{Fundamental Theorem of Arithmetic}
Every integer greater than 1 either is prime itself or is the unique product of prime numbers.
\end{derivation}

Factorizing numbers show the algebraic nature of all natural numbers.  As we seek
to simplify algebraic expression through factorization, we find many helpful patterns which
are important to know both forwards (distribution of multiplication over addition) and
backwards.

\subsection{Patterns of Distribution}
\marginfig[-0.5in]{\chapdir/pics/Kvadreringsregeln.png}{Squaring a binomial}
The square of a binomial always follows a pattern, which can be seen algebraically or
via algebra blocks.  A binomial multiplied by its conjugate produces the difference of squares.
When the first element of two binomial is the same, the result will have like terms that combine
as a sum.

\begin{equation}
(a + b)^2 = a^2 + 2ab + b^2
\end{equation}
\begin{equation}
(a + b) (a - b) = a^2 - b^2
\end{equation}
\begin{equation}
(x + a) (x + b) = x^2 + (a+b)x + ab
\end{equation}

\subsection{Factoring Quadratics}
\marginfig[0.5in]{\chapdir/pics/alge_tiles_ex_5.JPG}{Algebra tiles can be a helpful way to visualize factoring simple 	polynomials.}

When factoring, the first check should always be for a common factor to take out.  Then,
the above techniques seen in distribution work in reverse for factoring.  If a quadratic expression
is of the form $ax^2+bx+c$, there are two very different techniques, depending upon whether
$a=1$ of not.

\begin{derivation}{Simple Squared Term}
After checking for a GCF,\\
Perfect Square Binomial: $a^2 + 2ab + b^2 = (a+b)^2$\\
Difference of Squares: $a^2 - b^2 = (a+b)(a-b)$\\
``Adds to, Multiplies to'': $x^2 + (a+b)x + ab = (x + a)(x + b)$\\
\end{derivation}

\subsubsection{Split the middle}
When $a\ne1$, more elaborate techniques are called for.  Typically, working with $a<0$ is much
more difficult, so factoring out a negative is helpful.  Next, splitting the linear term in two may
yield results.  Looking at $ax^2+bx+c$, seek to find factors of $a\cdot c$ which add to $b$.
Factor the terms, two at a time (called ``factoring by grouping''.  In large polynomials, groups of
arbitrary size are possible.)  You will know you have succeed when you have two different
terms multiplied agains the same term in parentheses.

\begin{example}
\exProblem
Factor $15x^2-11x-14$.

\exSolution
Begin by multiplying $15\cdot(-14)=-210$.  We are looking for factors of $-210$ which are 11 apart.
Only 10 and and $-21$ fit the bill.

$15x^2-11x-14$\\
$15x^2+10x-21x-14$\\
$5x(3x+2)-7(3x+2)$\\
$(5x-7)(3x+2)$
\end{example}

Sometimes, none of the above techniques yields any results.  There are two equally valid
moves at that point, but one of which is made from the other.

\subsection{Complete the Square and the Quadratic Formula}
First, if $a\ne1$, factor it out of the terms with an unknown.
Second, use the definition of a Perfect Square Binomial
to discover what constant would be needed to ``complete the square''.  Insert this term and its
additive inverse (which is like adding zero) and factor to a square.

\begin{example}
\exProblem
Rewrite by completing the square: $3x^2 + 12x + 7$

\exSolution
$3(x^2+4x) + 7$\\
$3(x^2+4x+4-4) + 7$\\
$3[(x+2)^2-4] + 7$\\
$3(x+2)^2-5$
\end{example}

It's derivation is an exercise, but it is quite possible to complete the square on $ax^2+bx+c=0$
and produce the famous Quadratic Formula:

\begin{derivation}{Quadratic Formula}
For any quadratic $y=ax^2+bx+c$, the values of $x$ where $y=0$ are the two solutions
$x=\dfrac{-b\pm\sqrt{b^2-4ac}}{2a}$
\end{derivation}


\section{Sum-of-Costs in ASP}
The encoding we presented in the previous section still does not produce cost-optimal solutions. Indeed, once fed into an ASP will return a model only if a solution with makespan $\mathtt{T}$ exists. In this section we explain how we can obtain solutions for sum-of-costs MAPF.

There is a natural way to encode sum-of-costs minimization: to minimize the number of actions performed by each agent before stopping at the goal. We noticed, however, that this yields an encoding whose size grows linearly with the size of the grid, $|V|$. This motivated us to look for a more compact encoding which would not depend on $|V|$. Even though, as we see in our empirical evaluation below, the grid-independent encoding performs better in practice, we describe both approaches here since the grid-dependent encoding is more natural and is a contribution on its own since sum-of-costs had not been encoded in ASP before.

\subsection{Grid-Dependent Encoding}
This encoding is similar to the approach used in MDD-SAT~\cite{SurynekFSB16}: the idea to minimize the actions performed by the agent at each cell before stopping at the goal. At a first glance one might think that we just need to count every action performed away from the goal and minimize this number. This approach, however does not work because a \Wait at the goal at time $t$ \emph{should} be counted if the agent will move away from the goal at some instant $t'$ greater than $t$.

To identify time instants at which we know the agent will not move away from the goal, we introduce the predicate $at\_goal\_back(a,t)$, which specifies that agent $a$ has reached the goal at time $t$  and will not move away in the future:
{\small\begin{eqnarray*}
    at\_goal\_back(A,\mathtt{T}) &\leftarrow& agent(A), \\
    at\_goal\_back(A,T-1) &\leftarrow &at\_goal\_back(A,T),\\
    &&exec(A,wait,T-1).
  \end{eqnarray*}}

Now we define predicate $cost$, such that thre is an atom of the form $cost(a,t,1)$ in the model whenever agent $a$ performes an action at time $t$ before stopping at the goal. First we express that moving an agent from a cell that is not the goal is penalized by one unit:
\begin{equation*}\small
cost(A,T,1) \leftarrow at(A,X,Y,T), not \; goal(A,X,Y).
\end{equation*}
Second, moving an agent away from the goal is also penalized by one:
{\small \begin{equation*}\small
  \begin{split}
    cost(A,T,1) \leftarrow &at(A,X,Y,t), goal(A,X,Y), \\
    &exec(A,M,t), M \neq \Wait.
  \end{split}
  \end{equation*}

Third, if an agent performs a \Wait at the goal, but moves at a later time instant, then this is also penalized:
\begin{equation*}\small
  \begin{split}
    cost(A,T,1) \leftarrow &at(A,X,Y,t), goal(A,X,Y), \\
    &exec(A,wait,T), not \; at\_goal\_back(A,T).
  \end{split}
  \end{equation*}

Finally, via an optimization statement, we minimize the number of atoms of the form $cost(A,T,1)$ in the model:
\[ \#minimize \{C,T,A : cost(A,T,C)\}.\]

After grounding the number of rules is in $\Theta(|\mathcal{A}|\cdot|V| \cdot \mathtt{T})$.

\subsection{Grid-Independent Encoding}
For this encoding, we define the atom $optimal(a, c_a)$, for each agent $a\in \mathcal{A}$, where $c_a$ corresponds to the cost of the optimal path from $init(a)$ to $goal(a)$ ignoring both vertex and swap conflicts. In other words, $c_a$ is the result of solving a relaxation of the problem that ignores other agents. We compute such a value using Dijkstra's algorithm, before generating the encoding.

In contrast to the first encoding, we maximize the slack between the makespan $T$ and the time instant at which an agent has stopped at the goal, by simply adding:
\begin{equation*}\small
  \begin{split}
    penalty(A,T,1) \leftarrow &optimal(A,C), T > C, \\
    &at\_goal\_back(A,T-1).
\end{split}
\end{equation*}

Note that since no reference to the grid cells is made, the grounding generates a number of rules in $\Theta(|\mathcal{A}|\cdot\mathtt{T})$. Finally, we use the following maximization statement.
{\small\[ \small \#maximize \{P,T,A : penalty(A,T,P)\}.\]}

%Finally, the cost of the solution is $C = \mathtt{T} \times A - penalty $


\newpage
\subsection{Goal}\label{subsec:goal}

The use of path search algorithms on weighted graphs (Dijkstra's, A* and Bellman-Ford algorithms)

\subsection{Formulation of the problem}\label{subsec:formulation-of-the-problem}

\paragraph{I.} Generate a random adjacency matrix for a simple undirected weighted graph of 100 vertices and 500 edges with assigned random positive integer weights (note that the matrix should be symmetric and contain only 0s and weights as elements).
Use Dijkstra's and Bellman-Ford algorithms to find shortest paths between a random starting vertex and other vertices.
Measure the time required to find the paths for each algorithm.
Repeat the experiment 10 times for the same starting vertex and calculate the average time required for the paths search of each algorithm.
Analyse the results obtained.

\paragraph{II.} Generate a $10\times 10$ cell grid with 30 obstacle cells.
Choose two random non-obstacle cells and find a shortest path between them using A* algorithm.
Repeat the experiment 5 times with different random pair of cells.
Analyse the results obtained

\paragraph{III.} Describe the data structures and design techniques used within the algorithms.

\subsection{Brief theoretical part}\label{subsec:brief-theoretical-part}

A graph with no loops and no parallel edges is called a \textit{simple graph}.

\textit{Undirected graph} it is $G = (V, E)$, consisting of the set $V$ of nodes and the set $E$ of edges, which are unordered pairs of elements of $V$.

If edges in your graph have weights then your graph is said to be a \textit{weighted graph}, if the edges do not have weights, the graph is said to be \textit{unweighted}.

\textit{Dijkstra's Algorithm} (DA) is an algorithm for computing the shortest path between two vertices of a graph where all edges have nonnegative weight.
It is based on repeatedly expanding the closest vertex which has not yet been reached.
The complexity of the algorithm is estimated from $O(\lvert V \rvert \log{\lvert V \rvert})$ to $O(\lvert V \rvert^2)$, depending on the modification used.
This algorithm belongs to the class of greedy algorithms

The \textit{Bellman-Ford algorithm} (BFA) is an algorithm for computing the shortest path between two points in a weighted graph.
Unlike with \textit{Dijkstra's algorithm}, the edge weights are allowed to be negative.
On the down side, it has a larger running time, $O(\lvert V \rvert \lvert E \rvert)$.
This algorithm is related to Dynamic Programming Problems.

\textit{A*} search is a search algorithm which makes use of a heuristic which estimates the distance from a node to a goal state.
It iteratively expands nodes in order by their path cost plus their heuristic value.
If the heuristic satisfies certain "admissibility" conditions, then \textit{A*} is guaranteed to find an optimal path.
The time complexity of the algorithm is $O(\lvert E \rvert)$.
note that the algorithm \textit{A*} yields \textit{DA} if the heuristic is omitted.
This algorithm belongs to the class of greedy algorithms

\subsection{Results}\label{subsec:results}

Empirical comparison of the running time of algorithms \textit{DA} and \textit{BFA}:

\begin{verbatim}
    Dijkstra: 0.232 | Bellman-Ford: 0.843 | ration: 3.638
\end{verbatim}

\textit{Dijkstra's algorithm} is better when it comes to reducing the time complexity.
However, when we have negative weights, we have to go with the \textit{Bellman-Ford algorithm}.
Also, if we want to know whether the graph contains negative cycles or not, the \textit{Bellman-Ford algorithm} can help us with that.

Just one thing to remember, in case of negative weights or even negative cycles, the \textit{Bellman-Ford algorithm} can only help us with directed graphs.

The Figure~\ref{ris:plot} shows examples of the A* algorithm for finding the shortest distance for different pairs of vertices:

\begin{figure}[H]
    \center
    \includegraphics[width=\textwidth]{img/plot.png}
    \caption{Example of A* algorithm.}
    \label{ris:plot}
\end{figure}

If the heuristic gives an estimate of the shortest path from below (such a heuristic is called "acceptable"), then the A* search algorithm finds the shortest path.
In particular, the heuristic can always return 0.
in this case, the A* algorithm will be equivalent to the Dijkstra algorithm with a stop when the target vertex is reached.

If there is at least one path between the specified vertices in the graph, the A* algorithm always finds some path (regardless of the properties of the heuristic), since it is a generalization of the breadth-first search algorithm.

\subsection{Conclusion}\label{subsec:conclusion}

In this task, we applied and analyzed path-finding algorithms on weighted graphs (Dijkstra's algorithm, A*, and the Bellman-Ford algorithm).

\subsection{Appendix}\label{subsec:appendix}

The source code is located \href{https://github.com/vanSultan/anal_dev_algo/tree/lab_06}{here}: \url{https://github.com/vanSultan/anal_dev_algo/tree/lab_06}.


Several particle types can be introduced in GIFMod each having different properties and different constitutive models can be assigned to each group. The transport of particles in a GI system is governed by the following general mass balance equation:
\begin{equation}
\label{eq:20}
\begin{split}
\frac{d\Gamma_{i,l} G_{p,l,i}}{dt} =\\ \beta_{p,l} \bigg[\sum_{j=1}^{nj} pos \big(Q_{ij}+v_{s,p,ij}A_{ij}\big)G_{p,l,j}-\sum_{j=1}^{nj} pos \big(-Q_{ij}-v_{s,p,ij}A_{ij}\big)G_{p,l,i}\bigg]\\
-S_i \big(\sum_{l'=1}^{nl_p}\textbf{K}_{p,l,l'}G_{p,l,i}-\sum_{l'=1}^{nl_p}\textbf{K}_{p,l,l'}G_{p,l',i}\big) + \beta_{p,l} \sum_{j=1}^{nj} A_{ij}\frac{D_{p,ij}}{d_{ij}}(G_{p,l,j}-G_{p,l,i})
\end{split}
\end{equation}
In Eq. \ref{eq:20}, the first term on the right hand side is advection due to flow and settling, the second term represents the exchange of particles between phases (i.e. mobile, attached, etc.), and the last term represent dispersion and diffusion of particles.
Three pre-defined models for particle transport are provided in GIFMod respectively named Single Phase, Dual Phase and Triple Phase. In the single phase model a particle is assumed to only reside in mobile phase with no interaction with the solid phases. If a particle type is designated to be controlled by a dual phase model, it can interact between mobile phase and attached phase. Finally a triple phase model means that particles can be in mobile, reversibly attached and irreversibly attached phases.
\begin{itemize}
    \item \textbf{Single phase mode: } If a particle type is assigned to have a single phase model, there will be only one phase with $\alpha_{p,1}=1$ and there will be no mass exchange between phases. The interaction between aqueous particles and the air-water interface is assumed to occur instantaneously based on an aqueous -AWI partitioning coefficient $K_{aw}$. $\Gamma$ in Eq. \ref{eq:20} in this case will be a single member vector equal to $S_i + K_{aw} S_{aw}$. By default the air-water interface area is assumed to be proportional to the volume of air-phase (i.e. $S_aw = \sout{V}_i - S_i$) under unsaturated condition in the unsaturated soil block.
    \item \textbf{Dual phase model: } In a dual phase models particles can be in two phases including mobile and attached. $\Gamma$, $\textbf{K}$ matrix and $\alpha_p$ will respectively be:
    \begin{equation}
    \label{eq:21}
    \vec{\Gamma}_i =
    \begin{bmatrix}
    S_i \\
    \sout{V}_i \rho_i
    \end{bmatrix}
    \end{equation}
    \\
    \begin{equation}
    \label{eq:22}
    \vec{K}_p =
    \begin{bmatrix}
    0 & f_i \alpha_p \eta_p \  |v_i| \ (1-\frac{G_{p,s}}{G_{s,max}}) \\
    f_i k_{p,rel}  pos(|v_i|-v_{crit}) & 0\\
    \end{bmatrix}
    \end{equation}
    \\
    \begin{equation}
    \label{eq:23}
    \vec{\beta}_p =
    \begin{bmatrix}
    1 \\
    0 \\
    \end{bmatrix}
    \end{equation}

    \item \textbf{Triple phase model: } In a triple phase models particles can be in three phases including mobile and irreversibly attached and reversibly attached. $\Gamma$, $\textbf{K}$ matrix and $\alpha_p$ will respectively be:
    \begin{equation}
    \label{eq:24}
    \vec{\Gamma}_i =
    \begin{bmatrix}
    S_i \\
    \sout{V}_i \rho_i \\
    \sout{V}_i \rho_i
    \end{bmatrix}
    \end{equation}
    \\
    \noindent
    \begin{equation}
    \label{eq:25}
    \noindent
    \vec{K}_p =
    \begin{bmatrix}
    0 & \zeta_p f_i \alpha_p \eta_p \  |v_i| \ (1-\frac{G_{p,s}}{G_{s,max}}) & (1-\zeta_p) f_i \alpha_p \eta_p \  |v_i| \ (1-\frac{G_{p,s}}{G_{s,max}})\\
    0 & 0 & 0\\
    f_i k_{p,rel}  pos(|v_i|-v_{crit}) & 0 & 0\\
    \end{bmatrix}
    \end{equation}
    \\
    \begin{equation}
    \label{eq:26}
    \vec{\beta}_p =
    \begin{bmatrix}
    1 \\
    0 \\
    0 \\
    \end{bmatrix}
    \end{equation}
\end{itemize}
\subsection{Particle properties}
\begin{itemize}
\item \textbf{Attachment efficiency: } or sticking efficiency $\alpha_p$ in Eqs. \ref{eq:22} and \ref{eq:25}. This is the probability of a particle encountering a collector to stick to it. Not available for single phase particle model.
\item \textbf{Collection efficiency: } or transport efficiency $\eta_p$ in Eqs. \ref{eq:22} and \ref{eq:25}. The probability of a particle approaching a collector to encounter it. Not available for single phase particle model.
\item \textbf{Critical velocity for release: } Attached particles will start getting released when the velocity exceeds this value. The rate of release of attached particles is proportional to difference between the velocity in a block and the critical velocity (Eqs. \ref{eq:22} and \ref{eq:25}. Not available for single phase particle model.
\item \textbf{Diffusion coefficient: } $D_c$, Diffusion coefficient of the particle type beind defined.
\item \textbf{Dispersivity: } Dispersivity of the particle type. The mechanical dispersion coefficient will be calculated as: $D_{p,ij} = \alpha_{D,p} |v_ij| + D_{c,p}$;
\item \textbf{Irreversible collection fraction: } $\zeta_p$; in Eq. \ref{eq:25}. Only availble for the triple-phase particle model.
\item \textbf{Medium bulk density: } if entered this over-writes the bulk density specified in block properties in particle transport module.
\item \textbf{Partitioning coefficient to air-water interface: } The value of $k_aw$.
\item \textbf{Settling velocity: } Settling velocity $v_{s,p}$ in Eq. \ref{eq:20}.
\item \textbf{Specific surface area: } $f$ in Eqs. \ref{eq:22} and \ref{eq:25}
\end{itemize}

\subsection{Example: Colloid transport in a one dimensional saturated soil column under steady flow condition: }
In this example we show the creation of a simple model of multi-disperse particle transport and filtration in a one-dimensional column under steady-state flow condition using GIFMod. The column will be assumed to have $50cm$ lenght and an area of $100cm^2$ and it which is  descretized into 10 layers. A dummy reservoir will also be added a the bottom of the column to ease imposition of downstream boundary condition through prescribed flow feature.
\begin{itemize}
\item \textbf{Create the first block and assign the properties: } Add a Darcy block from the top menu. Set the following properties:
- \textbf{Bottom Area: }\textit{$0.01m^2$}
- \textbf{Depth: }\textit{$0.05m$}
- \textbf{Bulk Density: }\textit{$1600 kg/m^3$}
- \textbf{Saturated moisture content: } \textit{$0.4$}
- \textbf{Initial moisture content: } \textit{$0.4$}
- \textbf{storitivity: } \textit{1$m^{-1}$}
\item \textbf{Create a vertical array: } Right-click on the block that was just created and then click on "Make grid of blocks" item. Select "Vertical 2D Grid" and then change the number of rows to 10. Click "Ok". \\
- \textbf{Note: } When a vertical grid is made the bottom area of the original block will be copied as the interface area of the connectors.
\item \textbf{Introducing particles: } We will model three different particle types with three different sticking rate coefficients $alpha_p$ values. In order to introduce the particles, from the Project Explorer right-click on \textbf{Water Quality}$\rightarrow$\textbf{Particles} and then select "Add Particles" from the drop-down menu.
\item \textbf{Setting particle properties: } Set the following properties for the particle:
- \textbf{Name: } \textit{Particle I}
- \textbf{Attachment Efficiency: }\textit{1}
- \textbf{Collection Efficiency: } \textit{1e-6}
- \textbf{Model: }\textit{Dual Phase}
- \textbf{Specific Surface Area: }\textit{10000$m^-1$}
\\\\
Add two more particle types with the following properties:
- \textbf{Name: }\textit{Particle II}
- \textbf{Attachment Efficiency: }\textit{1}
- \textbf{Collection Efficiency: }\textit{1e-5}
- \textbf{Model: }\textit{Dual Phase}
- \textbf{Specific Surface Area: }\textit{10000$m^-1$}
\\\\
- \textbf{Name: }\textit{Particle III}
- \textbf{Attachment Efficiency: }\textit{1}
- \textbf{Collection Efficiency: }\textit{1e-4}
- \textbf{Model: }\textit{Dual Phase}
- \textbf{Specific Surface Area: }\textit{10000$m^-1$}
\item \textbf{Save: } It is now a good time to save the work.
\item \textbf{Inflow file: } Here we create the inflow file. It is assumed that the flow rate is constant and equal to $0.01m^3/day$ which results in a velocity of $2.5m/day$ over the entire experiment which takes 1 day and the particle concentration for all three types of particles in $10mg/L$ for a period of 1hr and which is followed by no particles in the inflow. The inflow file will look like figure \ref{fig:12}.
\begin{figure}[!ht]\label{fig:12}
\begin{center}
\includegraphics[width=8cm]{Images/Figure12.png} \\
\caption{Inflow file for particle transport in soil example}
\end{center}
\end{figure}
Create or download "inflow.txt" from the example folder. Click on block \textbf{Darcy (1)} and choose the inflow file from inflow time series field in the properties dialog box. \\
Right-click on the \textbf{Darcy (1)} block and visualize the time series using \textbf{Plot Inflow Properties} menu item. The inflow concentration for particle I for example should look like Figure \ref{fig:13}
\begin{figure}[!ht]\label{fig:13}
\begin{center}
\includegraphics[width=8cm]{Images/Figure13.png} \\
\caption{Concentration of Particle I in the inflow}
\end{center}
\end{figure}
\item \textbf{Outflow: } In order to impose the outflow boundary condition a dummy reservoir will be added to the bottom of the soil column. The type of the block used for this reservoir is not important because we will use the \textbf{prescribed flow} feature to control the outflow. Let's use a storage block to create the dummy outflow reservoir. Add a storage by clicking on the storage icon \includegraphics[width=0.5cm]{Icons/storage_icon.png} on the top tool bar. Move the newly added storage block to the bottom of the column and connect it to the block named \textbf{Darcy (10)}. \\
- Enter a non-zero \textbf{length} for the newly added connector and non-zero \textbf{bottom area} and  \textbf{depth} for the newly added storage block. These two values are required for the model to run. Also to prevent the storage block to become over-saturated enter a value of zero in the \textbf{Head-Storage relationship} property of the newly added storage block and set the bulk density of the storage block to \textit{1600$kg/m^3$}. //
\textbf{Note: } Note that if the value of \textbf{Medium Bulk Density} is entered in particle properties of a particle type, it will be used as the media density when performing the mass-balance for that particle class.
\item \textbf{Outflow rate time-series: } We want the outflow rate to be equal to the inflow rate. This can be accomplished in two ways. One is to turn on the "Steady State Hydraulics" in the \textbf{Setting$\rightarrow$Project Settings} or by assigning a \textbf{prescribed flow} to the bottom connector. We are going to use the second approach here. The flow time series to be prescribed to the \textbf{Darcy (10)-Pond (1)} connector should look like Figure \ref{fig:14}. Name the file "outflow.txt".
\begin{figure}[!ht]\label{fig:14}
\begin{center}
\includegraphics[width=8cm]{Images/Figure14.png} \\
\caption{Outflow file}
\end{center}
\end{figure}
Please note that the heading file in figure \label{fig:14} is ignored by the program and is only necessary for user documentation. \\
- Click on the \textbf{Darcy (10)-Pond (1)} connector, and choose "outflow.txt" from the \textbf{Prescribed flow} property. \\
- Set \textbf{Use Prescribed Flow} to "Yes". \\
- Save the project.
- Now the model is ready to run. Click on the run icon \includegraphics[width=0.5cm]{Icons/run_icon.png} on the left side tool bar and wait for the simulation to end. \\
- Right-click on the block named \textbf{Darcy (10)} and choose \textbf{Water Quality Results$\rightarrow$ Pericles I$\rightarrow$ Mobile} to see the breakthrough curve for particle type I \\
Do the same thing for \textbf{Particle II} and \textbf{Particle III}.
Right-click on the graph containing particle II results and click on \textbf{Copy Curve} item, then click on the graph containing particle type I results, right-click and select \textbf{Paste} item. \\ - Do the same thing with particle III to see the three breakthrough curves for the three particle types in a single graph. You can change the format of each curve by right-clicking on the graph and selecting the name of each curve (fig \ref{fig:30}).
\begin{figure}[!ht]
\begin{center}
\includegraphics[width=8cm]{Images/Figure30.png} \\
\caption{Particle breakthrough curves for the three particle classes}\label{fig:30}
\end{center}
\end{figure}

\end{itemize}
\newpage
\subsection{Example: Particle settling in a one-dimensional water body: }
In this example we create a simple model of particles settling in a one dimensional water column. Similar to the previous example, three particles classes will be considered but this time their settling velocities will be different. We will use an array of storage block to create water column. The water column is assumed to have a height of 1.5m which is descritized into 10 equally sized layers each represented by a single storage block. The particles are assumed to be initially uniformly distributed and are allowed to settle under gravity.
\begin{itemize}
\item \textbf{Create a new project} \\
\item \textbf{Introducing particle classes: }
From the \textbf{Project Explorer} right-click on \textbf{Water Quality}$\rightarrow$\textbf{Particles} and then select "Add Particles" from the drop-down menu.
\item \textbf{Setting particle properties: } Set the following properties for the particle:
- \textbf{Name: } \textit{Particle I}
- \textbf{Model: }\textit{Single Phase}
- \textbf{Settling velocity: }\textit{10$m/day$}
\\\\
Add two more particle types with the following properties:
- \textbf{Name: } \textit{Particle II}
- \textbf{Model: }\textit{Single Phase}
- \textbf{Settling velocity: }\textit{1$m/day$}
\\\\
- \textbf{Name: } \textit{Particle III}
- \textbf{Model: }\textit{Single Phase}
- \textbf{Settling velocity: }\textit{0.1$m/day$}

\item \textbf{Save: } It is now a good time to save the
\item \textbf{Create a storage block and set the properties: } Add a storage by clicking on the storage icon \includegraphics[width=0.5cm]{Icons/storage_icon.png} on the top tool bar. \\
- Set the following properties for the storage block:
- \textbf{Bottom Area: }\textit{10$m^2$}
- \textbf{Depth: }\textit{0.15m} \\
- \textbf{Initial water depth: }\textit{0.15m} \\
- \textbf{Saturated moisture content: } \textit{1} (There is no solid media in the storage blocks) \\
- \textbf{Initial particle concentration: } Click on the "..." symbol in front of the \textbf{Particle Initial Concentration} label in properties windows and enter a concentration of 10 for the three particle classes as is shown in figure \ref{fig:17}.  \\
\begin{figure}[!ht]\label{fig:17}
\begin{center}
\includegraphics[width=8cm]{Images/Figure17.png} \\
\caption{Particle initial concentration}
\end{center}
\end{figure}
- \textbf{Creating an array of storage blocks: } Right-click on the storage block that was just created and choose \textbf{Make Grid of Blocks} item from the menu. Choose \textbf{vertical 2D grid} option and enter 10 in the \textbf{Number of rows} box.
\item \textbf{Turn settling on for all the connectors: } By default the settling option for the connectors is set to \textbf{No}. In order to allow settling transport to occur via connectors the \textbf{Settling} option should be turned to \textbf{Yes}. Select all the connectors one by one and set the \textbf{Settling} property to \textbf{Yes}.
\item \textbf{Run the model: } Click on the run bottom on the left hand side tool bar \includegraphics[width=0.5cm]{Icons/run_icon.png} and wait for the simulation to end.
\item \textbf{Checking the results: } Right click on the \textbf{Storage (10)} block and choose \textbf{Plot Water Quality Results}$\rightarrow$\textbf{Particle I}$\rightarrow$\textbf{Mobile} to see the temporal variation of \textbf{Particle I} particle class at the bottom of the column. Do the same thing for \textbf{Particle II} and \textbf{Particle III} particle groups. You can copy and paste all curves into one window to compare the results for each particle class (Figure \ref{fig:31}).
\begin{figure}[!ht]
\begin{center}
\includegraphics[width=8cm]{Images/Figure31.png} \\
\caption{Particle initial concentration}\label{fig:31}
\end{center}
\end{figure}
\item \textbf{Adding particle diffusion: } Change \textbf{Diffusion coefficient} for all the particles to 0.1$m^2/day$ by clicking on each particle size from \textbf{Project Explorer}$\rightarrow$\textbf{Water Quality}$\rightarrow$\textbf{Particles}$\rightarrow$ and changing the \textbf{diffusion coefficient} property of all particles to 0.1$m^2/day$ .Run the simulation again. The diffusion should prevent all particles to settle to the bottom block.

\end{itemize}


\documentclass[12pt,a4paper]{article}
\usepackage{algorithm, algpseudocode, amsmath, amssymb, amsthm, csquotes, empheq, geometry, graphicx, hyperref, listings, multirow, siunitx, subcaption, upgreek}
\usepackage[italicdiff]{physics}
\usepackage[section]{placeins}
\usepackage[justification=centering]{caption}

\title{Computational Physics\\Problem Set 6}
\author{Saleh Shamloo Ahmadi\\Student Number: 98100872}
\date{November 1, 2021}

\hypersetup{colorlinks=true, urlcolor=cyan}
\newcommand{\fig}{../fig}
\newcommand{\ddfrac}[2]{{\displaystyle\frac{\displaystyle #1}{\displaystyle #2}}}

\begin{document}
	\maketitle
    \section{Central Limit Theorem}
    The central limit theorem states that the sum of independent random variables tends towards a normal distribution.
    Specifically, if $X_1, X_2, \dots, X_n$ are n random samples from a population with overall mean $\mu$ and finite
    variance $\sigma ^{2}$, and if $\bar{X}_{n}$ is the sample mean, then the limiting form of the distribution
    \begin{equation}
        Z=\lim _{n\to\infty}\frac{\sqrt{n}}{\sigma}\qty(\bar{X}_{n}-\mu)
    \end{equation}
    is a standard normal distribution, which is
    \begin{equation}
        \mathcal{N}(0, 1) = \frac{e^{-\flatfrac{x^2}{2}}}{\sqrt{2\pi}}.
    \end{equation}
    In the following figures, I generated random numbers between 0 and 1 and then compared the results (sample sums)
    to a perfect normal distribution with the same mean and variance as the generated sample.
    As you can see, the distribution of the sample is very close to a normal distribution in all
    cases, and as $N$ grows, the distribution becomes more similar to a normal distribution.

    Note that random walk generates a normal distribution exactly because of this theorem; The final position of the
    walker is the sum of a random sample from the numbers $-1$ and $1$. Each $X_n$ corresponds to a $-1$ or $1$ and
    the probability of each step is independent from other steps.
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/normal-5}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/normal-10}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/normal-100}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/normal-1000}
    \end{figure}

    \section[Generating a Gaussian Distribution with an RNG]
    {Generating a Gaussian Distribution with an RNG\footnote{Random Number Generator}}
    The probability density function of a random number generator is constant in the range $[0,1]$ and zero elsewhere.
    If the probability density function of a random variable $x$ is given by the function $f(x)$, then with a change of
    variables $y$ can be chosen as a function of $x$ such that the probability density function of $y$ is the arbitrary
    normalized function $g(y)$; The probability of associated $x$ and $y$ is equal, so
    \begin{equation}
        f(x)\dd{x} = g(y)\dd{y} \implies \int_{-\infty}^x f(x)\dd{x} = \int_{-\infty}^y g(y)\dd{y}.
    \end{equation}
    If $x$ is generated by an RNG, then
    \begin{equation}
        x = \int_{-\infty}^y g(y)\dd{y} = G(y)
    \end{equation}
    since $G(y)$ is monotonically increasing ($g(y) > 0$, because it is a probability density function),
    it is invertible and
    \begin{equation}
        y = G^{-1}(x).
    \end{equation}

    For a gaussian (normal) distribution
    \begin{equation}
        g(y) = \ddfrac{e^{-\frac{y^2}{2\sigma^2}}}{\sqrt{2\pi\sigma^2}}.
    \end{equation}
    In this form, $G(y)$ cannot be expressed in elementary functions. to solve this issue, we can generate two random
    numbers $y_1$ and $y_2$ from $x$ instead of one:
    \begin{equation}
        g(y_1, y_2)\dd{y_1}\dd{y_2} = g(y_1)g(y_2)\dd{y_1}\dd{y_2}
        = \ddfrac{e^{-\frac{y_1^2 + y_2^2}{2\sigma^2}}}{2\pi\sigma^2}\dd{y_1}\dd{y_2}
    \end{equation}
    With a change of coordinates to polar coordinates
    \begin{gather}
        \left\{
            \begin{aligned}
                y_1 = \rho\sin{\theta}\\
                y_2 = \rho\cos{\theta}
            \end{aligned}
        \right. \\
        g(y_1, y_2)\dd{y_1}\dd{y_2} = g(\rho, \theta)\rho\dd{\rho}\dd{\theta}
        = \ddfrac{e^{-\frac{\rho^2}{2\sigma^2}}}{2\pi\sigma^2}\rho\dd{\rho}\dd{\theta}
    \end{gather}
    For normalized distributions in terms of $\rho$ and $\theta$, we can use
    \begin{empheq}[left=\empheqlbrace]{align}
        g_1(\rho) &= \ddfrac{e^{-\frac{\rho^2}{2\sigma^2}}}{\sigma^2}\rho \\
        g_2(\theta) &= \frac{1}{2\pi}
    \end{empheq}
    Now, two random numbers with gaussian distribution $y_1$ and $y_2$ can be generated from two random numbers form RNG
    $x_1$ and $x_2$:
    \begin{empheq}[left=\empheqlbrace]{align}
        G_1(\rho) &= \int_0^\rho \ddfrac{e^{-\frac{\rho^2}{2\sigma^2}}}{\sigma^2}\rho\dd{\rho}
        = 1-e^{-\frac{\rho^2}{2\sigma^2}} \\
        G_2(\theta) &= \int_0^\theta \frac{1}{2\pi}\dd{\theta} = \frac{\theta}{2\pi}
    \end{empheq}
    \begin{empheq}[left=\empheqlbrace]{align}
        \rho &= G_1^{-1}(x_1) = \sigma\sqrt{2\ln(\frac{1}{1-x_1})} \\
        \theta &= G_2^{-1}(x_2) = 2\pi x_2
    \end{empheq}
    \begin{empheq}[box=\fbox, left=\empheqlbrace]{align}
        y_1 &= \sigma\sin(2\pi x_2)\sqrt{2\ln(\frac{1}{1-x_1})} \\
        y_2 &= \sigma\cos(2\pi x_2)\sqrt{2\ln(\frac{1}{1-x_1})}
    \end{empheq}
    To create a normal distribution with mean $\mu$ and variance $\sigma^2$ it suffices to add $\mu$ to $y_1$ and $y_2$.
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/gaussian-overlay}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/gaussian-spread}
    \end{figure}
    \begin{figure}
        \centering
        \includegraphics[width=\linewidth]{\fig/gaussian-check}
    \end{figure}
\end{document}


\documentclass{subfile}

\begin{document}
	\section{USAMO}\label{sec:usamo}

		\begin{problem}[USAMO $2020$, problem $6$]
			Let $n\geq2$ be an integer and $x_{1}\geq\ldots\geq x_{n},y_{1}\geq\ldots\geq y_{n}$ be $2n$ real numbers such that
				\begin{align*}
					0 = x_{1}+\ldots+x_{n}
						& = y_{1}+\ldots+y_{n}\\
					1 = x_{1}^{2}+\ldots+x_{n}^{2}
						& = y_{1}^{2}+\ldots+y_{n}^{2}
				\end{align*}
			Prove that
				\begin{align*}
					\sum_{i=1}^{n}(x_{i}y_{i}-x_{i}y_{n+i-1})^{2}
						& \geq\dfrac{2}{\sqrt{n-1}}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2018$, problem $1$]
			Let $a,b,c$ be positive real numbers such that $a+b+c=4\sqrt[3]{abc}$. Prove that
				\begin{align*}
					2(ab+bc+ca)+4\min\{a^{2},b^{2},c^{2}\}
						& \geq a^{2}+b^{2}+c^{2}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2017$, problem $6$]
			Find the minimum possible value of
				\begin{align*}
					\dfrac{a}{b^{3}+4}+\dfrac{b}{c^{3}+4}+\dfrac{c}{d^{3}+4}+\dfrac{d}{a^{3}+4}
				\end{align*}
			given that $a,b,c,d$ are non-negative real numbers such that $a+b+c+d=4$.
		\end{problem}

		\begin{problem}[USAMO $2013$, problem $4$]
			Find all real numbers $x,y,z\ge1$ such that
				\begin{align*}
					\min\{\sqrt{x+xyz},\sqrt{y+xyz},\sqrt{z+xyz}\}
						& = \sqrt{x-1}+\sqrt{y-1}+\sqrt{z-1}
				\end{align*}
		\end{problem}
	This is actually a special case of the following.
		\begin{problem}
			Prove that for real numbers $a,b,c\geq1$, the following inequality holds:
				\begin{align*}
					\sqrt{a-1}+\sqrt{b-1}+\sqrt{c-1}
						& \leq\sqrt{a(bc+1)}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2012$, problem $6$]
			Let $n\geq2$ be an integer and $x_{1}\geq\ldots\geq x_{n},y_{1}\geq\ldots\geq y_{n}$ be $2n$ real numbers such that
				\begin{align*}
					0 = x_{1}+\ldots+x_{n}
						& = y_{1}+\ldots+y_{n}\\
					1 = x_{1}^{2}+\ldots+x_{n}^{2}
						& = y_{1}^{2}+\ldots+y_{n}^{2}
				\end{align*}
			For each subset $A$ of $\{1,2,\ldots,n\}$, define
				\begin{align*}
					S_{A}
						& = \sum_{i\in A}x_{i}
				\end{align*}
			$S_{A}=0$ if $A$ is empty. Prove that for any positive number $\lambda$, the number of sets $A$ satisfying $S_{A}>\lambda$ is at most
				\begin{align*}
					\dfrac{2^{n-3}}{\lambda^{2}}
				\end{align*}
			For which choices of $x_{1},\ldots,x_{n},\lambda$ does equality hold?
		\end{problem}

		\begin{problem}[USAMO $2011$, problem $1$]
			Let $a,b,c$ be positive real numbers such that
				\begin{align*}
					a^{2}+b^{2}+c^{2}+(a+b+c)^{2}
						& \leq 4
				\end{align*}
			Prove that
				\begin{align*}
					\dfrac{ab+1}{(a+b)^{2}}+\dfrac{bc+1}{(b+c)^{2}}+\dfrac{ca+1}{(c+a)^{2}}
						& \geq3
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2009$, problem $4$]
			Let $n\geq2$ be an $a_{1},\ldots,a_{n}$ be positive real numbers such that
				\begin{align*}
					(a_{1}+\ldots+a_{n})\left(\dfrac{1}{a_{1}}+\ldots+\dfrac{1}{a_{n}}\right)
						& \leq \left(n+\dfrac{1}{2}\right)^{2}
				\end{align*}
			Prove that
				\begin{align*}
					\max\{a_{1},\ldots,a_{n}\}
						& \leq4\min\{a_{1},\ldots,a_{n}\}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2004$, problem $5$]
			Let $a,b,c$ be positive real numbers. Prove that
				\begin{align*}
					(a^{5}-a^{2}+3)(b^{5}-b^{2}+3)(c^{5}-c^{2}+3)
						& \geq (a+b+c)^{3}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2003$, problem $5$]
			Let $a,b,c$ be positive real numbers. Prove that
				\begin{align*}
					\dfrac{(2a+b+c)^{2}}{2a^{2}+(b+c)^{2}}+\dfrac{(2b+c+a)^{2}}{2b^{2}+(c+a)^{2}}+\dfrac{(2c+a+b)^{2}}{2c^{2}+(a+b)^{2}}
						& \leq 8
				\end{align*}

				\begin{solution}
					We have already solved it in \autoref{prob:usamo2003}.
				\end{solution}
		\end{problem}

		\begin{problem}[USAMO $2001$, problem $3$]
			Let $a,b,c$ be positive real numbers such that $a^{2}+b^{2}+c^{2}+abc=4$. Prove that
				\begin{align*}
					0
						& \leq ab+bc+ca-abc\leq 2
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $2000$, problem $6$]
			Let $a_{1},\ldots,a_{n},b_{1},\ldots,b_{n}$ be non-negative real numbers. Prove that
				\begin{align*}
					\sum_{i,j=1}^{n}\min\{a_{i}a_{j},b_{i}b_{j}\}
						& \leq\sum_{i,j=1}^{n}\min\{a_{i}b_{j},a_{j}b_{i}\}
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $1999$, problem $4$]
			Let $n>3$ be an integer and $a_{1},\ldots,a_{n}$ be positive real numbers such that
				\begin{align*}
					a_{1}+\ldots+a_{n}
						& \geq n\quad\mbox{and}\\
					a_{1}^{2}+\ldots+a_{n}^{2}
						& \geq n^{2}
				\end{align*}
			Prove that $\max\{a_{1},\ldots,a_{n}\}\geq2$.
		\end{problem}

		\begin{problem}[USAMO $1996$, problem $3$]
			Let $a_{1},\ldots,a_{n}$ be real numbers in the interval $(0,\pi/2)$ such that
				\begin{align*}
					\tan\left(a_{0}-\dfrac{\pi}{4}\right)+\ldots+\tan\left(a_{1}-\dfrac{\pi}{4}\right)
						& \geq n-1
				\end{align*}
			Prove that
				\begin{align*}
					\tan{a_{0}}\cdots\tan{a_{n}}
						& \geq n^{n+1}
				\end{align*}

				\begin{solution}
					See \eqref{prob:usa1996-3}.
				\end{solution}
		\end{problem}

		\begin{problem}[USAMO $1994$, problem $4$]
			Let $a_{1},\ldots,a_{n}$ be a sequence of positive real numbers such that
				\begin{align*}
					\sum_{i=1}^{n}a_{i}^{2}
						& \geq \sqrt{n}
				\end{align*}
			for all $n$. Prove that
				\begin{align*}
					\sum_{i=1}^{n}a_{i}^{2}
						& > \dfrac{1}{4}\left(1+\dfrac{1}{2}+\ldots+\dfrac{1}{n}\right)
				\end{align*}
		\end{problem}

		\begin{problem}[USAMO $1993$, problem $5$]
			Let $a_{0},\ldots,a_{n}$ be positive real numbers such that $a_{i-1}a_{i+1}\leq a_{i}^2$ (such a sequence is called \textit{log concave}). Show that for $n>1$,
				\begin{align*}
					\dfrac{a_{0}+\ldots+a_{n}}{n+1}\dfrac{a_{1}+\ldots+a_{n-1}}{n-1}
						& \geq\dfrac{a_{0}+\ldots+a_{n-1}}{n}\dfrac{a_{1}+\ldots+a_{n}}{n}
				\end{align*}
		\end{problem}
\end{document}


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

\subsection{Correlation}

\marginpar{Tuesday\\ 2020-10-20, \\ compiled \\ \today}

The correlation between the component \(x_i\) and the component \(j\) is given by
%
\begin{align}
\expval{(x_i - \mu _i) (x_j - \mu _j)}
= \int \dd[n]{x} (x_i - \mu _i) (x_j - \mu _j) \mathcal{N}(\vec{x} | \vec{\mu}, C) = C_{ij}
\,.
\end{align}

If \(i = j\), this is the mean value of \((x_i - \mu _i)^2\): we can integrate out all the other parameters, so that we get the variance of that parameter.

We can show that the covariance is indeed given by the expression above: we define \(\vec{y} = \vec{x} - \vec{\mu}\) as usual, so we have
%
\begin{align}
\int \dd[n]{y} y_i y_j \mathcal{N}(\vec{y}| \vec{0}, C) = \frac{1}{N} \int \dd[n]{y} y_i y_j \exp(- \frac{1}{2} \vec{y}^{\top} V \vec{y})
= \expval{y_i y_j}
\,,
\end{align}
%
for the usual normalization \(N\) and \(V = C^{-1}\).
We can compute it with a trick:
%
\begin{align}
\expval{y_i y_j} &= 2 \pdv{}{V_{ij}} \qty[\log \int \dd[n]{y} \exp(- \frac{1}{2} \vec{y}^{\top} V \vec{y})]  \\
&= -2 \pdv{}{V_{ij}} \qty[\log \qty( \frac{(2 \pi )^{n/2}}{\sqrt{\det V}})]  \marginnote{The \(2 \pi \) factor disappears since the log of a ratio is the difference of the logs, so it becomes an additive constant.}
\\
&= 2 \pdv{}{V_{ij}} \qty[ \frac{1}{2} \det V]  \\
&= \frac{1}{\det V} \pdv{}{V_{ij}} \det V
\,,
\end{align}
%
and we can express the determinant using the Laplace formula:
%
\begin{align}
\det V = \sum _{p=1}^{n} V_{ip} K_{ip}
\,,
\end{align}
%
where \(K_{ip}\) is a cofactor, which crucially does not depend on the coefficient \(V_{ip}\). Now, since \(V\) is symmetric the cofactor also is; therefore we have
%
\begin{align}
(V^{-1})_{ij} = \frac{1}{\det V} (K^{\top})_{ij} = \frac{K_{ij}}{\det V}
\,,
\end{align}
%
therefore
%
\begin{align}
\expval{y_i y_j} &= \frac{1}{\det V}\pdv{}{V_{ij}} \underbrace{\qty[\sum _{p=1}^{n} V_{ip} K_{ip} ]}_{\det V} \\
&= \frac{K_{ij}}{\det V} = \qty(V^{-1})_{ij} = C_{ij}
\,.
\end{align}

\section{Two-parameter estimation revisited}

We have data \(\vec{d}\), for which the likelihood with a Gaussian model is
%
\begin{align}
\mathscr{L} = \mathbb{P}(\vec{d} | \mu , \sigma ) = \prod_{i=1}^{N} \mathscr{L}_i = \frac{1}{(2\pi )^{n/2}\sigma^{n}} \exp(- \frac{\sum _{i} (d_i - \mu)^2}{2 \sigma^2})
\,.
\end{align}

We want to estimate the average, \(\mu \), but we know neither \(\mu \) nor \(\sigma \): therefore, we will need to marginalize over \(\sigma \).

We choose improper uniform priors, for \(\sigma \in \mathbb{R}^{+}\) and \(\mu \in \mathbb{R}\).
Then, the posterior is proportional to the likelihood:
%
\begin{align}
P = \mathbb{P}(\mu , \sigma | \vec{d}) = \frac{1}{(2 \pi )^{n/2} \sigma^{n}} \exp(- \frac{1}{2} \frac{\sum _{i} (d_i - \mu)^2}{\sigma^2})
\,
\end{align}
%
for \(\sigma > 0\), 0 for \(\sigma <0\). Integrating over \(\sigma \) to marginalize means we have to compute
%
\begin{align}
\mathbb{P}(\mu | \vec{d}) =
\int \dd{\sigma } \frac{1}{\sigma^{n}} \exp(- \frac{1}{2} \frac{\sum _{i} (d_i - \mu)^2}{\sigma^2})
\,.
\end{align}

We make a change of variables \(\sigma  = 1/t\), so \(\dd{\sigma } = - \dd{t} / t^2 \) then
%
\begin{align}
\mathbb{P}(\mu | \vec{d})
&= - \int \dd{t} t^{n-2} \exp(- \frac{t^2}{2} \sum _{i}(d_i - \mu)^2)
\,,
\end{align}
%
and now set \(\tau^2 = t^2 \sum _i (d_i - \mu)^2\), so \(\dd{\tau } = \dd{t} \sqrt{\sum_i (d_i - \mu)^2}\). The integral then reads
%
\begin{align}
\mathbb{P}(\mu | \vec{d})
&= \qty[\sum _{i} (d_i - \mu)^2]^{- \frac{n}{2} + 1 - \frac{1}{2}}
\underbrace{\int \dd{\tau } \tau^{n-2} \exp(- \frac{\tau^2}{2})}_{\text{constant}}
\,.
\end{align}

Therefore, the log-posterior reads
%
\begin{align}
L = -\frac{n-1}{2} \log \qty(\sum _{i} (d_i - \mu)^2)
\,.
\end{align}

The value \(\mu_0\) is defined so that \(L(\mu )\) is stationary there, which means that
%
\begin{align}
\dv{L}{\mu } &= -\frac{n-1}{2}  \frac{1}{\sum _{i} (d_i - \mu)^2} \dv{}{\mu } \qty[\sum _{i} (d_i - \mu)^2] \overset{!}{=} 0  \\
&= -\frac{n-1}{2}  \frac{1}{\sum _{i} (d_i - \mu)^2} \sum _{i} 2 (d_i - \mu ) (-1)  \\
&= \frac{n-1}{2} \frac{\sum _{i} (d_i - \mu ) }{\sum_i (d_i - \mu )^2}
\,,
\end{align}
% \todo[inline]{Should there not be an extra \(n\) here from the differentiation?}
%
meaning that \(\mu_0 = n^{-1} \sum d_i\), our estimator for the mean of the distribution is the arithmetic mean of the data.

\todo[inline]{Unless \(n =1\)! if we only made one measurement and we know \emph{absolutely nothing} about \(\sigma \), then the mean could be \emph{whatever}.}

The error is then defined by
%
\begin{align}
\eval{\dv[2]{L}{\mu }}_{\mu_0 } &= \dv{}{\mu } \qty[(n-1) \frac{\sum _{i} (d_i - \mu)}{\sum _{i } (d_i - \mu )^2}]  \\
&= (n-1) \frac{- n \sum _{i} (d_i - \mu)^2 + \sum _{i} (d_i - \mu) \times 2 \overbrace{\qty[\sum_i (d_i - \mu )]}^{= 0} (n)}{\qty[\sum _{i} (d_i - \mu)^2]^2}  \\
&= - \frac{(n-1)n}{\sum _{i} (d_i - \mu)^2}
\,,
\end{align}
%
therefore
%
\begin{align}
\hat{\sigma}^2 = - \frac{1}{\dv[2]{L}{\mu }} = \frac{\sum _{i}(d_i - \mu)^2}{n ( n-1)}
\,.
\end{align}

This is the usual estimator for the variance of the mean of a set of data with Gaussian errors.

\section{Multiparameter estimation in the abstract}

We always seek a parameter vector \(\vec{x}_0\) in the form
%
\begin{align}
\eval{\pdv{\log P}{x_i} }_{\vec{x}_0} = 0
\,,
\end{align}
%
or more compactly \(\nabla L (\vec{x}_0) =0 \).
Typically, if this is linear we can do it, if it is nonlinear then it is a mess.
Let us then start with the linear case, in which \(\vec{\nabla} L  = A \vec{x} + \vec{c}\).

The solution then reads \(\vec{x}_0 = -A^{-1} \vec{c}\).
Matrix inversion is an \(\order{N^3}\) problem: often, even in this seemingly simple case, we cannot do the computation analytically.

\end{document}


\subsection{Well-ordered sets}\label{subsec:well_ordered_sets}

This section may fit in \fullref{sec:order_theory}, however it deals with well-foundedness of set membership and with induction principles which are more fitting here.

\begin{definition}\label{def:well_founded_relation}
  Let \( \prec \) be a \hyperref[def:binary_relation]{binary relation} on a set \( A \) (not necessarily satisfying any axioms).

  An \term{infinitely descending sequence} is a \hyperref[def:sequence]{sequence} \( \seq{ x_k }_{k=1}^\infty \) such that \( x_{k+1} \prec x_k \) for all \( k \in 1, 2, \ldots \). That is,
  \begin{equation*}
    \cdots \prec x_3 \prec x_2 \prec x_1.
  \end{equation*}

  The relation \( \prec \) is called \term{well-founded} if there exists no infinitely descending sequence.

  We cannot easily formulate the theory of well-founded relations as a \hyperref[def:first_order_theory]{first-order theory}. This is why we only discuss well-relations sets in the context of \logic{ZFC}.
\end{definition}

\begin{proposition}\label{thm:set_membership_is_well_founded}
  Set membership is \hyperref[def:well_founded_relation]{well-founded} in \logic{ZFC}. More precisely, given a set \( A \), if we regard \( \in \) as a binary relation between members of \( A \), then we would obtain that \( \in \) is a well-founded relation.

  This proposition generalizes \fullref{thm:simple_foundation_theorems}.
\end{proposition}
\begin{proof}
  The empty set is vacuously well-founded, so suppose that \( A \) is nonempty. Suppose also that \( \in \) is not well-founded on \( A \). Then there exists an infinitely descending sequence \( \seq{ x_k }_{k=1}^\infty \subseteq A \) such that
  \begin{equation*}
    \cdots \in x_3 \in x_2 \in x_1.
  \end{equation*}

  Denote by \( B \) the set \( \set{ x_k \given k = 1, 2, \ldots } \). By the \hyperref[def:zfc/foundation]{axiom of foundation}, \( B \) contains a set \( C \) which is disjoint from \( B \).

  Clearly \( C \) coincides with at least one of \( x_1, x_2, \ldots \). Let \( C = x_{k_0} \). Since \( x_{k_0} \cap B = \varnothing \), then \( x_{k_0 + 1} \) is either not a member of \( x_{k_0} \) or of \( B \). But it is a member of both by our assumption that the sequence is infinitely descending.

  The obtained contradiction proves that \( \in \) is well-founded on \( A \).
\end{proof}

\begin{corollary}\label{thm:simple_foundation_theorems}
  We list several statements, which are consequences of the \hyperref[def:zfc/foundation]{axiom of foundation} via \fullref{thm:set_membership_is_well_founded}:

  \begin{thmenum}
    \thmitem{thm:simple_foundation_theorems/member_of_itself} No set is a member of itself.

    \thmitem{thm:simple_foundation_theorems/mutual_membership} For any two sets \( A \) and \( B \), it is not possible for both \( A \in B \) and \( B \in A \) to hold.

    \thmitem{thm:simple_foundation_theorems/ordinal_successor_is_injective} The \hyperref[def:ordinal_successor]{ordinal successor operation} is injective. That is, for every two set \( A \) and \( B \) from \( \op{succ}(A) = \op{succ}(B) \) it follows that \( A = B \).
  \end{thmenum}
\end{corollary}
\begin{proof}
  \SubProofOf{thm:simple_foundation_theorems/member_of_itself} The sequence
  \begin{equation*}
    \cdots A \in A \in A
  \end{equation*}
  is infinitely descending, hence \( A \) cannot be a member of itself.

  \SubProofOf{thm:simple_foundation_theorems/mutual_membership} The sequence
  \begin{equation*}
    \cdots A \in B \in A \in B \in A
  \end{equation*}
  is infinitely descending, hence \( A \in B \) and \( B \in A \) cannot simultaneously hold.

  \SubProofOf{thm:simple_foundation_theorems/ordinal_successor_is_injective} Note that \( A \in \op{succ}(A) = \op{succ}(B) \), hence either \( A = B \) or \( A \in B \).

  We can analogously conclude that either \( A = B \) or \( B \in A \).

  But \( A \in B \) and \( B \in A \) cannot simultaneously hold due to \fullref{thm:simple_foundation_theorems/mutual_membership}.

  Hence, it remains for \( A = B \) to hold.
\end{proof}

\begin{proposition}\label{thm:infinite_descent_partial_order}
  In a \hyperref[def:partially_ordered_set]{partially ordered set} \( (\mscrP, \leq) \), the strict relation \( < \) is \hyperref[def:well_founded_relation]{well-founded} if and only if every nonempty subset \( A \subseteq \mscrP \) has a \hyperref[def:partially_ordered_set_extremal_points/maximal_and_minimal_element]{minimal element}.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Suppose that \( < \) is well-founded and let \( A \subseteq \mscrP \). Suppose that \( A \) has no minimal element.

  If \( A \) is finite, we cannot construct an infinitely descending sequence because of irreflexivity of \( < \). If \( A \) is infinite, then we can construct an infinitely descending sequence using well-founded recursion as follows:
  \begin{itemize}
    \item Let \( x_1 \) be any element of \( A \).
    \item Given \( x_k \), let \( x_{k+1} \) be any element of \( A \) such that \( x_{k+1} < x_k \). We know that such an element exists because \( x_k \) is not minimal in \( A \).
  \end{itemize}

  This construction contradicts the well-foundedness of  \( \leq \), hence \( A \) must have a minimal element.

  \NecessitySubProof Suppose that every subset of \( \mscrP \) has a minimal element. Suppose that there exists an infinitely descending sequence \( \seq{ x_k }_{k=1}^\infty \). Then the set \( \set{ x_k \given x \in \set{ 1, 2, \ldots } } \) has no minimal element, which contradicts our assumption. Hence, no sequence in \( \mscrP \) is infinitely descending and \( < \) is well-founded.
\end{proof}

\begin{definition}\label{def:well_ordered_set}\mcite[def. 63.1]{OpenLogicFull}
  A \hyperref[def:totally_ordered_set]{totally ordered set} \( (\mscrP, \leq) \) is said to be \term{well-ordered} if either of the following equivalent conditions hold:
  \begin{thmenum}
    \thmitem{def:well_ordered_set/direct} Every nonempty subset of \( \mscrP \) has a \hyperref[def:partially_ordered_set_extremal_points/maximum_and_minimum]{minimum}.
    \thmitem{def:well_ordered_set/well_founded} The strict order \( < \) is \hyperref[def:well_founded_relation]{well-founded}.
  \end{thmenum}

  The \hyperref[def:binary_relation/irreflexive]{irreflexivity} of \( < \) is redundant because it follows from the well-foundedness --- if the strict relation is not irreflexive, then there exists some element \( x \in \mscrP \) such that \( x < x \) and thus \( \seq{ x }_{k=1}^\infty \) is an infinitely descending sequence.
\end{definition}
\begin{proof}
  The equivalence of the conditions follows from \fullref{thm:infinite_descent_partial_order} and \fullref{thm:totally_ordered_minimal_element_is_minimum}.
\end{proof}

\begin{theorem}[Well-founded induction]\label{thm:well_founded_induction}\mcite[prop. 63.3]{OpenLogicFull}
  Let \( \mscrL \) be the \hyperref[def:first_order_syntax]{first-order language} with no functional symbols and a single predicate symbol \( \prec \). We have already mentioned that we cannot formalize the concept of well-foundedness in first-order logic alone, so we will work with \hyperref[def:first_order_structure]{structures} directly.

  Every \hyperref[def:well_founded_relation]{well-founded} structure \( \mscrX = (A, I) \) over \( \mscrL \) satisfies an inductive axiom schema. For every formula \( \varphi \) over \( \mscrL \) not containing \( \eta \) nor \( \zeta \) as free variables, \( \mscrX \) satisfies
  \begin{equation}\label{eq:thm:well_founded_induction}
    \qforall \eta
    \parens[\Big]
      {
        \overbrace
          {
            \underbrace{ \parens[\Big]{ \qforall {\zeta \prec \eta} \varphi[\xi \mapsto \zeta] } }_{\mathclap{\substack{\T{inductive} \\ \T{hypothesis}}}}
            \rightarrow
            \underbrace{ \varphi[\xi \mapsto \eta] }_{\mathclap{\substack{\T{inductive step} \\ \T{conclusion}}}}
          }^{\T{inductive step}}
      }
    \rightarrow
    \underbrace{ \qforall \eta \varphi[\xi \mapsto \eta] }_{\T{conclusion}}.
  \end{equation}

  See the comments in \fullref{def:peano_arithmetic/PA3} regarding variables and quantification in axiom schemas and \fullref{rem:induction} for a general discussion of induction.

  In the special case where \( \mscrX = \BbbN \), this is called \term{strong induction} compared to the usual natural number induction \eqref{eq:def:peano_arithmetic/PA3}. This is discussed in \fullref{rem:induction/well_founded}.
\end{theorem}
\begin{proof}
  Fix a formula \( \varphi \) in \( \mscrL \). Fix a variable assignment \( v \) in \( \mscrX \). We will show that the \hyperref[def:material_implication/contrapositive]{contraposition} of \eqref{eq:thm:well_founded_induction} holds in this model.

  Suppose that there exists a value \( x \in A \) such that \( \varphi\Bracks{v_{\xi \mapsto x}} = F \). That is,
  \begin{equation*}
    \qexists \eta \neg \varphi[\xi \mapsto \eta].
  \end{equation*}
  holds.

  Let \( x_0 \) be the smallest such value (which is guaranteed to exist because \( A \) is well-founded by \( \prec \)). Thus, the inductive hypothesis \( \qforall {\zeta < x_0} \varphi[\xi \mapsto \zeta] \) holds, but the inductive step conclusion \( \varphi[\xi \mapsto x_0] \) does not.

  Since \( v \) was chosen arbitrarily, this is true for all variable assignments in \( \mscrX \). Formally,
  \begin{equation}\label{eq:thm:well_founded_induction/contraposition}
    \mscrX
    \vDash
    \qexists \eta \neg \varphi[\xi \mapsto \eta]
    \rightarrow
    \qexists \eta
    \parens[\Big]
      {
        \qforall {\zeta \prec \eta} \varphi[\xi \mapsto \zeta] \wedge \neg \varphi[\xi \mapsto \eta]
      }
  \end{equation}

  This is precisely the contrapositive of \eqref{eq:thm:well_founded_induction}. Since we are working in classical logic, the contrapositive is semantically equivalent to its original implication, hence \( \mscrX \vDash \eqref{eq:thm:well_founded_induction} \).
\end{proof}

\begin{theorem}[Epsilon induction]\label{thm:epsilon_induction}
  For every formula \( \varphi \) in the language of set theory not containing \( \eta \) nor \( \zeta \) as free variables, the following is a theorem of \logic{ZFC}:
  \begin{equation*}
    \qforall \eta
    \parens[\Big]
      {
        \overbrace
          {
            \underbrace{ \parens[\Big]{ \qforall {\zeta \in \eta} \varphi[\xi \mapsto \zeta] } }_{\mathclap{\substack{\T{inductive} \\ \T{hypothesis}}}}
            \rightarrow
            \underbrace{ \varphi[\xi \mapsto \eta] }_{\mathclap{\substack{\T{inductive step} \\ \T{conclusion}}}}
          }^{\T{inductive step}}
      }
    \rightarrow
    \underbrace{ \qforall \eta \varphi[\xi \mapsto \eta] }_{\T{conclusion}}.
  \end{equation*}

  This induction schema is called \enquote{\( \varepsilon \)-induction} because the set membership symbol \( \in \) is derived from \( \varepsilon \) as explained in \fullref{rem:epsilon_and_set_membership}.

  See the comments in \fullref{def:peano_arithmetic/PA3} regarding variables and quantification in axiom schemas and \fullref{rem:induction} for a general discussion of induction.
\end{theorem}
\begin{proof}
  Every model of \logic{ZFC} is well-founded by \( \in \) due to \fullref{thm:set_membership_is_well_founded}. The corollary then follows from \fullref{thm:well_founded_induction}.
\end{proof}

\begin{lemma}\label{thm:well_ordered_embedding_inflationary}
  Any \hyperref[def:partially_ordered_set/homomorphism]{order embedding} on a well-ordered set is \hyperref[def:inflationary_function]{inflationary}.

  That is, if \( (\mscrP, \leq) \) is a well-ordered set and \( f: \mscrP \to \mscrP \) is an order embedding, then \( x \leq f(x) \) for any \( x \in \mscrP \).
\end{lemma}
\begin{proof}
  We proceed by induction on \( \mscrP \). Fix \( x_0 \) and suppose that \( y \leq f(y) \) for all \( y < x_0 \).

  Aiming at a contradiction, suppose that \( f(x_0) < x_0 \). Thus, there exists some \( y_0 < x_0 \) such that \( f(x_0) = y_0 \). By the inductive hypothesis we have \( f(x_0) = y_0 \leq f(y_0) \). Thus, either \( f(x_0) < f(y_0) \), which contradicts that \( f \) is an order homomorphism, or \( f(x_0) = f(y_0) \), which contradicts the injectivity of \( f \).

  The obtained contradiction demonstrates that \( x \leq f(x) \) for all \( x \in \mscrP \).
\end{proof}

\begin{proposition}\label{thm:well_ordered_isomorphism_is_unique}
  There is at most one isomorphism between any pair of well-ordered sets.
\end{proposition}
\begin{proof}
  Fix two well-ordered sets \( (\mscrP, \leq_\mscrP) \) and \( (\mscrQ, \leq_\mscrQ) \) and let \( f: \mscrP \to \mscrQ \) and \( g: \mscrP \to \mscrQ \) be two order isomorphisms.

  For any member \( x \) of \( \mscrP \) we have the following possibilities:
  \begin{itemize}
    \item If \( f(x) <_\mscrQ g(x) \), then \( g^{-1}(f(x)) <_\mscrP x \) and \( f(g^{-1}(f(x))) <_\mscrQ f(x) \). Then we can use \hyperref[rem:natural_number_recursion]{natural number recursion} to build an infinitely descending sequence of members of \( \mscrQ \). But this is a contradiction because \( \mscrQ \) is well-founded.

    \item We can build a similar sequence if \( g(x) <_\mscrQ f(x) \).

    \item It remains for \( f(x) = g(x) \) to hold.
  \end{itemize}

  Since \( x \in \mscrP \) was arbitrary, we conclude that \( f = g \).
\end{proof}

\begin{proposition}\label{thm:well_ordered_lexicographic_order_is_well_ordered}
  If \( (\mscrP, \leq_\mscrP) \) and \( (\mscrQ, \leq_\mscrQ) \) are \hyperref[def:well_ordered_set]{well-ordered sets}, then the \hyperref[eq:def:lexicographic_order]{lexicographic} and \hyperref[eq:def:lexicographic_order/reverse]{reverse lexicographic} orders on \( \mscrP \times \mscrQ \) are well-ordering relations.

  Compare this result to \fullref{thm:lexicographic_order_is_partial_order} and \fullref{thm:total_lexicographic_order_is_total_order}.
\end{proposition}
\begin{proof}
  We have already shown in \fullref{thm:total_lexicographic_order_is_total_order} and these total orders. It only remains to check well-foundedness.

  \SubProofOf[def:well_founded_relation]{well-foundedness} Let \( \prec \) be the lexicographic order on \( \mscrP \times \mscrQ \).

  Suppose that there exists an infinitely descending sequence
  \begin{equation*}
    \cdots \prec (a_3, b_3) \prec (a_2, b_2) \prec (a_1, b_1).
  \end{equation*}

  Since \( \mscrP \) is well-founded, the corresponding sequence \( \seq{ a_k }_{k=1}^\infty \). Then there exists an index \( k_0 \) such that \( a_{k_0} = a_k \) for \( k \geq k_0 \). Therefore, the sequence \( \set{ b_k }_{k=k_0}^\infty \) must be infinitely descending. But this contradicts the well-foundedness of \( \mscrQ \).

  Therefore, no infinitely descending sequence exists in the totally ordered set \( (\mscrP \times \mscrQ, \prec) \) and thus it is well-ordered.
\end{proof}