\section{Setting}
\label{sec:setting}
% In this section,
In this section, we introduce our setting and notation.

\spara{Markov Chain:}
Consider a weighted directed graph
$G=(V,E,p)$ with $|V|=n$ and $|E|=m$.
We use $\pi(v)\subseteq V$ and $\children(v)\subseteq V$ to
denote the set of parent and child nodes of node $v$, respectively.
\begin{align*}
\pi(v) & = \{u\mid u\in V, e(u\rightarrow v)\in E\} \\
\kappa(v) & = \{u\mid u\in V, e(v\rightarrow u)\in E\}
\end{align*}
Edges $e(u\rightarrow v)\in E$ are associated with
real-valued weights $p(u\rightarrow v)\in [0,1]$
such that $\forall u\in V$:
$
  \sum_{v\in V} p(u\rightarrow v) = 1$.

These weights give rise to a \markovchain\
with transition matrix $\transition$ --
where $\transition(u,v) = p(u,v)$ denotes
the probability of a transition from node $u$ to node $v$.
% Since
% $\transition$ is the transition matrix of a \markovchain, it has the property
% that $\sum_{v\in V}\transition(u,v)=1$.
Moreover, we assume that a set of items
are distributed among the nodes of $G$.
We use the row vector $\initial$ to denote the
the {\it initial} number of items per node;
that is, $\initial(u)$ is the number of items {initially}
at node $u$.
For the entirety of this paper,
transition matrix $\transition$ and distribution $\initial$ are
assumed known and part of the input.

\note[MM]{For consistency, let's use 'position' instead of 'location' of items.}
Consider now a single step of the \markovchain. During this step, each and
every item transitions from the node $u$ where it resides originally
to another node $v$, according to transition probabilities
$\transition(u,v)$.
At the end of this step, items are
redistributed among the nodes - and we are no longer certain about their
position. We use $\rvFinal$ to denote the random vector with the number
of items at each node after one transition.
The \emph{expected number of items} at each node is given by:
%\[
$\final = E[\rvFinal] = \initial{\transition}$.
%\]

%% MM: Do we need this?
% More generally,
% if the \markovchain\ takes $t$ steps, then the expected number of items
% per node at the end of these steps is given by:
% \[
% \final = \initial{\transition}^t.
% \]

\spara{Quantifying uncertainty:}
We wish to have
% an accurate estimate of the distribution of items over nodes $V$
% -- in other words, we wish to have
good point estimates for values $\final(v)$.
We quantify the quality of estimates in terms of the variance
in the number of items on each node afer the transition step.
Specifically, let us consider the number $\rvFinal(v)$ of items
on node $u$ after the transition step:
an item previously at node $u$ will transition to
node $v$ according to a Bernoulli distribution with
success probability $\transition(u,v)$;
and since items transition from node to node independently from each other,
the variance in the number of items $\rvFinal(v)$ is
\begin{align}
\variance(\rvFinal(v))  =\ & \variance(\rvFinal(v) | \transition, \initial) \nonumber \\
 =\ & \sum_{u\in V}\initial(u)\transition(u,v)\left(1-\transition(u,v)\right).
\end{align}
To obtain an aggregate measure of uncertainty $\uncertainty_0$,
we opt to sum the aforementioned quantity over all nodes.
% with simple summation, as follows:
\begin{align}
\uncertainty_0 & = \sum_{v\in V}{\variance(\rvFinal(v))} \nonumber  \\
 %& = \sum_{v\in V}\sum_{u\in V}\initial(u)\transition(u,v)\left(1-\transition(u,v)\right) \nonumber \\
 & = \sum_{u\in V}\initial(u)\sum_{v\in V}\transition(u,v)\left(1-\transition(u,v)\right)
\label{eq:uncertainty}
\end{align}
% % MM: We have said this earlier.
% Note that variance
% $\variance(\rvFinal(v)) = \variance(\rvFinal(v) | \transition, \initial)$
% is defined conditionally on the
% information we are given as input -- in this case, the transition matrix
% \transition\ and the initial distribution of items $\initial$.
% In the interest of simplicity, we omit the conditional part of the
% variance in notation when it is part of
% the fixed input (such as \initial\ and \transition)
% -- and in what follows, we always assume that variance is
% conditional on all available information.

% % MM: Should we skip this part (uncertainty over time?)
% \spara{Uncertainty over time}
% % $\uncertainty_0$ of
% Equation~\eqref{eq:uncertainty} expresses the
% uncertainty at one transition step. After $t$ steps, uncertainty is given by
% the same equation with $\transition^t$ in place of $\transition$.
% \begin{equation}
% \uncertainty_0^t = \sum_{u\in V}\initial(u)\sum_{v\in V}\transition^t(u,v)\left(1-\transition^t(u,v)\right)
% \end{equation}
% If $\transition^t$ converges for $t\rightarrow\infty$, then $\uncertainty_0^t$
% converges as well. As a special case, if the Markov chain is well-behaving
% (specifically: if it is ergodic~\cite{gallager2012discrete}), then
% $\transition^t$ converges to $\transition^\infty$ with $|V|$ identical rows
% \steadystate, that express a `steady-state' distrbution of items. In that
% case, the probability that any item is located at node $u$ is equal to
% \steadystate(u), regardless of what node it was placed at initially.
% It follows that the expected number of items that are located at node $u$
% at time $t\rightarrow\infty$ is equal to
% \begin{equation}
%   \final^{\infty}(u) = \steadystate(u) \sum_u \initial(u)
% \end{equation}
% and the total uncertainty at time $t\rightarrow\infty$ is equal to
% \begin{equation}
%   \uncertainty_0^\infty = \sum_u \steadystate(u)\left(1 - \steadystate(u)\right)
% \end{equation}

% In what follows, we
% focus on the case of a transition
% step of the \markovchain\ and are interested in
% inferring the distribution of items over the nodes.
% % and discuss the setting of multiple steps in Section~\ref{sec:infinity}.

% \todo[MM]{Let's use this convention: $u$ for parent node, $v$ for child node.}

\spara{Monitoring:}
Given the transition matrix $\transition$ and the
initial distribution of
items $\initial$, we estimate the distribution of items
$\rvFinal$ after
one transition step, with
the uncertainty given in Equation~\eqref{eq:uncertainty}.
After one transition step,
we are allowed to
retrieve information about the position of the items
and thus reduce uncertainty.
We do this by performing ``monitoring operations", i.e. queries on the
position of items on the \markovchain.
These operations are  of the following types:
%\begin{itemize}
\squishlist
	\item{\bf \parentstransitions} Retrieve the number of items that transitioned
  to node $v$ from each  $u\in\pi(v)$;
  \item{\bf \nodeitems} Retrieve the number of items that reside on node
  $v$ after the transition step;
	\item{\bf \edgetransitions} Retrieve the number of items that transitioned
  from node $u$ to node $v$;
	\item{\bf \childrentransitions} Retrieve the number of items that
  transitioned from node $u$ to each child $v\in \children(u)$.
%\end{itemize}
\squishend

From the above four types of monitoring operations, the last one (i.e., {\childrentransitions}) are both a bit
unintuitive and they lead to trivial combinatorial problems. Thus, we omit them from the rest of the discussion.
%%%%%%

\bpara{Expected uncertainty}
Once we retrieve the answer $\answer$ to a set of monitoring operations,
we have more information about the positioning of items over nodes $V$
-- and thus an updated (and non-increased) uncertainty
$\uncertainty(\answer) = \sum_{v\in V}{\variance(\rvFinal(v) | \answer)}$.
In the setting we consider, however, the challenge we face is {\it not} to compute
the uncertainty {\it given} the information retrieved via a monitoring
operation, but rather to {\it select the monitoring operations}
so that the uncertainty we will face {\it after} retrieving $\answer$ is
minimized {\it in expectation}. Therefore, the quantity of interest
is that of \expecteduncertainty\ for a set of operations
that we choose to perform, expressed as
$E[\sum_{v\in V}{\variance(\rvFinal(v) | \answer)}]$. %,
% with expectation taken
% over the possible transitions according to \transition.

Let us now assume we have chosen to perform a set of operations of
either one of the aforementioned types. In what follows, we provide
formulas for the \expecteduncertainty\
in each case.

\spara{Expected uncertainty under \parentstransitions :}
We
perform monitoring operations for
a subset $S\subseteq V$ of nodes --
and obtain an answer
$A_{_\shortparentstransitions}(S) = \{n_{uv}; v\in S,\ e(u\rightarrow v)\in E\}$,
where $n_{uv}$ is the number of transitions to $v$ from its parent node $u$.
The expected value $\objective_{_\shortparentstransitions}(S)$ of the uncertainty
$\uncertainty(A_{_\shortparentstransitions}(S))$
after these operations is given by
\begin{align}
\label{eq:nodevariance}
\objective_{_\shortparentstransitions}(S) = & E[\uncertainty(A_{_\shortparentstransitions}(S))] \nonumber \\
= & \sum_{u\in V}\initial^{\prime}(u)\sum_{v\in V\setminus S}\transition^{\prime}(u,v)\left(1-\transition^{\prime}(u,v)\right)
\end{align}
where
% expectation is taken over the probability of
% possible transitions and thus
% possible answers $A_{_\shortparentstransitions}$ -- and
% the following notation is used.
\begin{align}
\rho(u,S) & = \sum_{v\in S}\transition(u,v) \nonumber \\
 \initial^{\prime}(u) & = \initial(u)\left(1-\rho(u,S)\right) \label{eq:adjustedx}\\
 \transition^{\prime}(u,v) & = \frac{\transition(u,v)}{1-\rho(u,S)}. \label{eq:adjustedP}
\end{align}
Intuitively, $\initial^{\prime}(u)$ expresses the expected number
of items that transition from $u$ to nodes $v$ {\it other than} those in $S$;
and $\transition^{\prime}(u,v)$ expresses the probability an item transitions
from $u$ to $v$ {\it given} that it does {\it not} transition to those in $S$.
We see, then, that Equation~\eqref{eq:nodevariance} has the same form as
Equation~\eqref{eq:uncertainty} but is evaluated on adjusted values of
$\initial$ and $\transition$, to take into account the
information we obtain via $A_{_\shortparentstransitions}$.

Note that the expected uncertainty after
the monitoring operations is no larger than $\uncertainty_0$\footnote{For a proof, see Supplementary Material, Lemma~\ref{lemma:decreased_uncertainty_pt}.}.

\spara{Expected uncertainty under \nodeitems:}
We
perform monitoring operations for
a subset $S\subseteq V$ of the nodes --
and obtain an answer
$A_{_\shortnodeitems}(S)
= \{n_v; v\in S\}$, where $n_v$ is the number of items at node $v$
after the transition.
For an instance of answer $A_{_\shortnodeitems}(S)$,
let also $A_{_\shortparentstransitions}(S) = \{n_{uv}; v\in S,
  e(u\rightarrow v)\in E\}$
be an answer for \parentstransitions\ on the same set $S$ of nodes
-- which by definition
is {\it consistent with} $A_{_\shortnodeitems}(S)$, in the sense that
\begin{equation}
n_v = \sum_{u\in V} n_{uv}, \forall v\in S.
\end{equation}
It can be shown that the \expecteduncertainty\ is equal for the two cases.
That is:
$\objective_{_\shortparentstransitions}(S) = \objective_{_\shortnodeitems}(S)$.
\footnote{For a proof, see Supplementary Material,
Theorem~\ref{theorem:node-equivalence}.}.

%-- i.e., whether we are given $A_{_\shortnodeitems}(S)$
%or $A_{_\shortparentstransitions}(S)$ -- expressed by
%Equation~\eqref{eq:nodevariance}

\spara{Expected uncertainty under \edgetransitions:}
We perform monitoring operations for
a subset $D\subseteq E$ of the edges --
and obtain an answer
$A_{_\shortedgetransitions} = A_{_\shortedgetransitions}(D) = \{n_e; e\in D\}$,
where $n_e$ is the number of transitions over edge $e$.
The expected value $\objective_{_\shortedgetransitions}(D)$ of the uncertainty
$\uncertainty(A_{_\shortedgetransitions}(D))$
after these operations is given by
\begin{align}
\objective_{_\shortedgetransitions}(D) = & E[\uncertainty(A_{_\shortedgetransitions})] \nonumber \\
= & \sum_{u\in V}\initial^{\prime\prime}(u)\sum_{e(u\rightarrow v)\in E\setminus D}\transition^{\prime\prime}(u,v)\left(1-\transition^{\prime\prime}(u,v)\right) \label{eq:edgetransitions}
\end{align}
where
\begin{align}
\rho(u,D) & =\ \sum_{e(u\rightarrow v)\in D}\transition(u,v) \nonumber\\
 \initial^{\prime\prime}(u) & =\ \initial(u)\left(1-\rho(u,D)\right) \label{eq:etinit}\\
 \transition^{\prime\prime}(u,v) & =\ \frac{\transition(u,v)}{1-\rho(u,D)} \label{eq:ettransit}
\end{align}
Similar to \parentstransitions\ and \nodeitems, expected uncertainty
$\objective_{_\shortedgetransitions}(S)$ is no greater than
$\uncertainty_0$.

\iffalse
\spara{Expected uncertainty under \childrentransitions:}
Assume that we perform monitoring operations for
a subset $S\subseteq V$ of the nodes --
and obtain an answer
$A_{_\shortchildrentransitions}(S)
= \{n_{uv}; u\in S, (u, v)\in E\}$, where $n_{uv}$ is the number of items that
transition over edge $(u,v)$.
The expected value $\objective_{_\shortchildrentransitions}(S)$ of the uncertainty
$\uncertainty(A_{_\shortchildrentransitions}(S))$
after these operations is given by
\begin{align}
\label{eq:shortchildrentransitions}
\objective_{_\shortchildrentransitions}(S) & = E[\uncertainty(A_{_\shortchildrentransitions})] = \nonumber \\
& = \sum_{u\in V-S}\initial(u)\sum_{v\in V}\transition(u,v)\left(1-\transition(u,v)\right).
\end{align}
Notice that this quantity is no greater than
$\uncertainty_0$ (Equation~\eqref{eq:uncertainty}), as the outer summation
is performed for a subset of nodes.
\fi


\subsection{Brackets}

Operators can be shown together, with brackets. For example:

\((\alpha \lor \beta )\land \gamma \)

Is not the same as:

\(\alpha \lor (\beta \land \gamma )\)


\section{Constrained linear data-feature mapping from MgNet to interpret ResNet}
In this section, we will establish a new understanding of pre-act ResNet
by involving the idea that the pre-act ResNet block is an iterative scheme for solving some
hidden model in each grid, which is also the fundamental assumption in MgNet.
More details about this mathematical interpreting model for CNN can be found
in~\cite{he2019constrained}.

The main point here is the introduction of
the so-called data and feature space for CNN, which is analogous to the
function space and its duality in the theory of multigrid
methods~\cite{xu2017algebraic}.
Namely, following~\cite{he2019mgnet} we introduce
the next data-feature mapping model in every grid level follows:
\begin{equation}\label{eq:fmapping}
A^{\ell} \ast u^\ell = f^{\ell},
\end{equation}
where $f^\ell$ and $u^\ell$ belong to the data and feature space at $\ell$-th grid.
We now make the following two important observations for this data-feature mapping:
\begin{itemize}
	\item The mapping in \eqref{eq:fmapping} is linear, more specifically it is just a convolution with multichannel, zero
	padding and stride one as in pre-act ResNet.
	\item In each level, namely between two consecutive pooling, there is only one
	data-feature mapping, or we say that $A^\ell$ only depends on $\ell$, but not on number of layers.
\end{itemize}
We note that the assumption that these linear data-feature mapping
depend only on the grid level $\ell$ is motivated from a basic property of
multigrid methods~\cite{xu1992iterative, hackbusch2013multi, xu2017algebraic}.

Besides \eqref{eq:fmapping}, we introducing an important constrained condition
in feature space that
\begin{equation}\label{eq:positive}
u^{\ell,i} \ge 0.
\end{equation}
The rationality of this constraint in feature space can be interpreted as follows.
First of all, from the real neural system, the real neurons will only be
active if the electric signal is greater than certain thresholding value.
Namely, we can think that human brains can only see features
with certain threshold.
On the other hand, the ``shift'' invariant property of feature space in CNNs,
namely, $u+a$ will not change the classification results. This means that $u+a$ should
have the same classification result with $u$. That is to say, we may assume that
$u \ge 0$ to reduce some redundancy of $u$.

Based on these assumptions above, what we need to do next is to
solve the data-feature mapping equation in \eqref{eq:fmapping}.
We will adopt some classical iterative methods~\cite{xu1992iterative} in scientific computing
to solve the system \eqref{eq:fmapping} and obtain that
\begin{equation}\label{BAmapping}
u^{\ell,i} = u^{\ell,i-1} + B^{\ell,i} \ast (f^{\ell} - A^{\ell}\ast u^{\ell,i-1}),~ i = 1:\nu_\ell,
\end{equation}
where $u^{\ell} \approx u^{\ell,\nu_\ell}$.
For more details about iterative methods
in numerical analysis, we refer to~\cite{xu1992iterative, hackbusch1994iterative, golub2012matrix}.
To preserve \eqref{eq:positive}, we naturally use the ReLU activation function $\sigma$
to modify \eqref{BAmapping} as follows
\begin{equation}
\label{eq:uBfAu}
u^{\ell,i} = u^{\ell,i-1} + \sigma \circ B^{\ell,i}\ast \sigma  (f^\ell -  A^\ell
\ast u^{\ell,i-1}), \quad i=1:\nu_\ell,
\end{equation}
which leads the the basic iterative scheme in MgNet.

Now, let us consider the iteration for residual. Because of the linearity of convolution in data-feature mapping,
if we consider the residual $r^{\ell,j} = f^{\ell} - A^{\ell}\ast u^{\ell,j}$,
\eqref{eq:uBfAu} leads to the next iterative forms for residuals
\begin{equation}\label{eq:pre-actResNet_residual}
r^{\ell, i} = r^{\ell,i-1} - A^\ell \ast \sigma \circ B^{\ell,i}\ast \sigma(r^{\ell,i-1}).
\end{equation}
This is the same as \eqref{eq:pre-act ResNet} under the constraint $A^{\ell,i} = A^{\ell}$ in pre-act ResNet.

We summarize the above derivation in the following simple theorem.
\begin{theorem}\label{thm:1} Under the assumption that there is only
	one linear data-feature mapping in each grid $\ell$, i.e. $A^{\ell,i} = A^{\ell}$,
	the iterative form in feature space as in \eqref{BAmapping} is equivalent to
	\eqref{eq:pre-actResNet_residual} if $A^\ell$ is invertible where $r^{\ell,i} = f^\ell - A^{\ell}\ast u^{\ell,i}$.
\end{theorem}


% !TEX root = ../../main.tex
\subsection{The (continuous) master equation for allele frequencies} \label{sec_master_eq}

One of the most powerful tools to study stochastic process is the so-called
master equation. Originally devised to study the stochastic time evolution of
chemical reactions - thereby christened the chemical master equation - this
equation has found applications in many areas of chemistry, physics and
biology. The equation is as statement about the time evolution of the
transition probabilities of a Markov process. That is just a fancy way of
saying that the master equation describes how the transition probabilities
between states change over time.

Let's derive this powerful equation starting from \eref{eq_chapman_kolmogorov}.
For our case of study we want to understand how the frequency of a particular
allele evolves over time, that is a stochastic process $X(t)$. Notice again
that we distinguish between the stochastic process $X(t)$ formed by the
ensemble of all possible realizations $x(t)$. The Chapman-Kolmogorov equation
for this particular case with three time points $t_1 < t_2 < t_3$ takes the
form
\begin{equation}
  P(x_3, t_3 \mid x_1, t_1) = \int_0^1 dx_2\; P(x_3, t_3 \mid x_2, t_2)
                                          P(x_2, t_2 \mid x_1, t_1),
  \label{eq_chapman_freq}
\end{equation}
where the integration limits $[0, 1]$ are the domain of values that an allele
frequency can take. Now let us assume that we observe the frequency at time $t$
and it happens to be at a value $x_1$. Then after a very short time $\Dt$ we
observe again the allele frequency which is now at a value $x_2$. Under this
short time limit we can approximate the transition probability as
\begin{equation}
  P(x_2, t + \Dt \mid x_1, t) = \delta(x_2 - x_1)
  \underbrace{\left[ 1 - a^{(0)}(x_1, t) \Dt \right]}_{\text{probability
  of no transition}} +
  \underbrace{\phi_t(x_2 \mid x_1)\Dt}_\text{probability of transition} +
  \mathcal{O}(\Dt^2).
  \label{eq_transition_short_time}
\end{equation}
Let's break down this equation. We have split the possible things that can
happen on a time window $\Dt$ into two possible cases. The first one
represented by the first term on the right-hand side is the possibility that on
this small time window no transition actually takes place. The
$\delta$-function that is equal to 1 if and only if $x_2 = x_1$ is there to
make sure that this term is added only when there was no transition during that
time and $x_2$ remains the same as $x_1$. Inside the square brackets we wrote
$1 - a^{(0)}(x_1, t) \Dt$, the reason for writing the term $a^{(0)}$ will
become clear later on when we derive the so-called Fokker-Planck equation.
Having a term of the form 1 - ``something'' hints at the fact that this
``something'' must be the probability of transitioning somewhere else rather
than staying at the same position. For the second term we wrote $\phi_t(x_2
\mid x_1)\Dt$ as the probability of transitioning outside of $x_1$ during this
time window. Our function $\phi_t(x_2 \mid x_1)$ represents the transition
probability per unit time between $x_1$ and $x_2$ at time $t$. When we multiply
this rate in time$^{-1}$ units by a small time window, we obtain the
probability of transitioning from $x_1$ to $x_2$.

In order to understand better the term $a^{(0)}(x_1, t)$ in
\eref{eq_transition_short_time} recall that a probability distribution must be
normalized. That means that if we integrate both sides of
\eref{eq_transition_short_time} over all values of $x_2$ it must be true that
\begin{equation}
  \int_0^1 dx_2 \; P(x_2, t + \Dt \mid x_1, t) =
  \int_0^1 dx_2 \; \delta(x_2 - x_1) \left[ 1 - a^{(0)}(x_1, t) \Dt \right]
  + \int_0^1 dx_2 \; \phi_t(x_2 \mid x_1)\Dt
  = 1.
\end{equation}
Integrating over the $\delta$-function implies that we set $x_2 = x_1$,
therefore we obtain
\begin{equation}
  1 = \left[  1 - a^{(0)}(x_1, t) \Dt\right] +
  \int_0^1 dx_2 \; \phi_t(x_2 \mid x_1)\Dt.
\end{equation}
Solving for $a^{(0)}(x_1, t)$ results in
\begin{equation}
  a^{(0)}(x_1, t) = \int_0^1 dx_2 \; \phi_t(x_2 \mid x_1),
  \label{eq_a0}
\end{equation}
proving our previous assertion that $a^{(0)}(x_1, t)$ must be the
probability of jumping from $x_1$ to somewhere else.

Using this approximation for short time steps we will now derive the
differential equation that the transition probability between frequencies must
obey. Specifically for three time points $t_o < t < t + \Dt$ with corresponding
allele frequencies $x_o, x', f$ we have that \eref{eq_chapman_freq} takes the
form
\begin{equation}
  P(x, t + \Dt \mid x_o, t_o) = \int_0^1 dx' \; P(x, t + \Dt \mid x', t)
  P(x', t \mid x_o, t_o).
\end{equation}
Substituting \eref{eq_transition_short_time} results in
\begin{equation}
  P(x, t + \Dt \mid x_o, t_o) = \int_0^1 dx' \;
  \left[ \delta(x - x') \left( 1 - a^{(0)}(x', t)\Dt \right)
  + \phi_t(x \mid x')\Dt \right]
  P(x', t \mid x_o, t_o).
\end{equation}
Evaluating the integral for the first term on the right-hand side gives
\begin{equation}
  P(x, t + \Dt \mid x_o, t_o) = \left[\left( 1 - a^{(0)}(x, t)\Dt \right)\right]
  P(x, t \mid x_o, t_o) +
  \Dt \int_0^1 dx' \; \phi_t(x \mid x') P(x', t \mid x_o, t_o).
\end{equation}
We can then substitute \eref{eq_a0} and rearrange terms to obtain
\begin{equation}
  P(x, t + \Dt \mid x_o, t_o) = P(x, t \mid x_o, t_o)
  + \int_0^1 dx' \left[ \phi_t(x \mid x') P(x', t \mid x_o, t_o) -
  \phi_t(x' \mid x) P(x, t \mid x_o, t_o) \right]\Dt.
\end{equation}
Sending the first term on the right-hand side to the left, dividing both sides
by $\Dt$ and taking the limit $\Dt \rightarrow 0$ gives the differential
equation we were looking for
\begin{equation}
  \dt{P(x, t \mid x_o, t_o)} = \int_0^1 dx' \;
  \underbrace{
  \left[ \phi_t(x \mid x') P(x', t \mid x_o, t_o) \right.}
  _{\text{gain } x' \rightarrow f}  -
  \underbrace{
  \left. \phi_t(x' \mid x) P(x, t \mid x_o, t_o) \right]}
  _{\text{loss } x \rightarrow x'}.
  \label{eq_master_eq_trans}
\end{equation}
This is the integro-differential equation known as the master equation. This
continuous form of the master equation describes the time evolution of the
transition probabilities $P(x, t \mid x_o, t_o)$, not the evolution of the
probability of being at a specific state $P(x, t)$. However we can use the
rules of probability to obtain such description by the following process:
Suppose the stochastic process $X(t)$ describes the time evolution of the
frequency. Assuming $X(t)$ is a stationary Markov process as described in
\secref{sec_stationary_process} means that this process is completely
characterized by two functions - the probability of having a particular value
for the allele frequency $P_X(x)$ that does not depend on time, and a
transition probability $P(x, t \mid x_o, t_o)$. We define a new, non-stationary
process $F^*(t)$ for $t \geq t_o$ by setting
\begin{equation}
  P^*(x, t) = P(x, t\mid x_o, t_o),
\end{equation}
i.e. forcing the initial condition to be a specific value $x_o$ at time $t_o$.
This is a sub-ensemble of the process $X(t)$ since we demanded that $X(t = t_o)
= x_o$. More generally if instead of setting the initial condition to be a
single specific value $P(x, t_o) = \delta(x - x_o)$ we define a probability
distribution for the initial state $P(x, t_o) = \rho(x_o)$, we have a
sub-ensemble of the form
\begin{equation}
  P^*(x, t) = \int_0^1 dx_o \; P(x, t\mid x_o, t_o) \rho(x_o).
  \label{eq_subensemble}
\end{equation}
The interpretation of this sub ensemble is that the system was initially set on
a non-stationary state. The initial state distribution $\rho(x_o)$ does not
depend on time, therefore if we take the time derivative on both sides of
\eref{eq_subensemble} we find that
\begin{equation}
  \dt{P^*(x, t)} = \int_0^1 dx_o \; \dt{P(x, t\mid x_o, t_o)}
                        \rho(x_o).
\label{eq_subensemble_dt}
\end{equation}
Notice that the term with the time derivative on the right-hand side of
\eref{eq_subensemble_dt} is the master equation that we derived in
\eref{eq_master_eq_trans}. Substituting this results in
\begin{equation}
  \dt{P^*(x, t)} = \int_0^1 dx_o \; \rho(x_o)
  \int_0^1 dx' \;
  \left[ \phi_t(x \mid x') P(x', t \mid x_o, t_o) -
  \phi_t(x' \mid x) P(x, t \mid x_o, t_o) \right].
\end{equation}
Redistributing the integrals gives
\begin{equation}
  \dt{P^*(x, t)} = \int_0^1 dx' \; \phi_t(x \mid x')
  \overbrace{
  \int_0^1 dx_o \; P(x', t \mid x_o, t_o) \rho(x_o)}
  ^{P^*(x', t)\text{ by definition}} -
  \int_0^1 dx' \; \phi_t(x' \mid x)
  \overbrace{
  \int_0^1 dx_o \; P(x, t \mid x_o, t_o) \rho(x_o)}
  ^{P^*(x, t)\text{ by definition}}.
\end{equation}
Using the definition of the sub-ensembles shown in \eref{eq_subensemble} we
arrive to a result of the form
\begin{equation}
  \dt{P^*(x, t)} = \int_0^1 dx' \;
  \underbrace{
  \phi_t(x \mid x') P^*(x', t)
  }_{x' \rightarrow x \text{ gain}} -
  \int_0^1 dx' \;
  \underbrace{
  \phi_t(x' \mid x) P^*(x, t)
  }_{f \rightarrow x' \text{ loss}}.
  \label{eq_master_eq_full}
\end{equation}
In this form we can see that the master equation is a balance between gain and
loss of probability at each state $f$. Having said that, the truth about the
continuous master equation is that is extremely complicated to work with. In
general integro-differential equations are challenging mathematical objects to
deal with. That is why in the next section we'll use the powerful tool of
Taylor expansions to simplify the equation. But before that let's discuss some
historic uses of the master equation that might look different to our
derivation
on \eref{eq_master_eq_full}.

\subsubsection{Einstein-Kimura continuous master equations}

In 1905 during the groundbreaking year of Einstein's scientific career he
published a paper in which he attempted to give a molecular explanation to the
phenomena of Brownian motion. For this he derived Fick's second law from of a
statistical argument by Taylor expanding a master equation - more on that in
the next section. In this classic paper Einstein had one of the very first uses
of a continuous master equation applied to a physical problem. The difference
from our approach is that Einstein didn't derive the master equation from the
Chapman-Kolmogorov property of continuous-time continuous-state Markov
processes, but simply proposed its functional form directly.

While the problem Einstein was addressing in his paper had to do with a random
walker moving in real space, the mathematical tools that he proposed can be
directly mapped to the population genetics setup. As a matter of fact Motoo
Kimura himself used an equivalent approach to Einstein's formulation of the
master equation for his formulation of diffusion theory. Kimura used the same
approach as Einstein of Taylor expanding the master equation, with the main
difference being that for population genetics the transition probability
$\phi_t(x \mid x')$ is a function of the current position $x'$ while in real
space free diffusion is independent of the position. For this short section our
objective is to show that our derivation of the master equation is equivalent
to Einstein's and Kimura's proposed functional form. We will focus on Kimura's
version of the master equation since population genetics is what concerns us in
these notes.

Kimura's original derivation of the classic diffusion theory begins stating
that the process of allele frequency changes can be stated as
\begin{equation}
  P(x, t + \Dt) = \int d\varepsilon \;
  P(x - \varepsilon, t) \phi_t(x \mid x - \varepsilon) \Dt,
  \label{eq_kimura_master}
\end{equation}
and that is it. While it took us a while to justify \eref{eq_master_eq_full},
Kimura (and Einstein in the context of Brownian motion) simply stated the
master equation as the starting point. There is nothing intrinsically wrong
about having \eref{eq_kimura_master} as the starting point, but in this set of
extend notes I thought it would be insightful to start from a more fundamental
property of Markov processes and have the master equation be a consequence of
such property. Also I would like to highlight that in all of the population
genetics literature I have come across so far there has never been an explicit
account of the integration limits on the equations. This includes Kimura's
original work as well as textbooks. For this particular case the integration
limits should go from $f$ to $f - 1$ such that the values of $f - \varepsilon$
range from 0 to 1. So the proper form of this integral is given by
\begin{equation}
  P(x, t + \Dt) = \int_{-f}^{f - 1} d\varepsilon \;
  P(x - \varepsilon, t) \phi_t(x \mid x - \varepsilon) \Dt.
  \label{eq_kimura_master_lim}
\end{equation}

Notice that at first glance \eref{eq_master_eq_full} and
\eref{eq_kimura_master_lim} don't seem to be equivalent.
\eref{eq_master_eq_full} is a differential equation that describes how the
probability distribution changes given gains and losses of probability on state
$f$, while \eref{eq_kimura_master_lim} only makes a statement of what would the
probability distribution look like a tiny time step into the future by adding
all the jumps \textbf{into} state $f$, but it doesn't include a term for all
the jumps out of state $f$. To show that these equations are equivalent we have
to do two things:
\begin{enumerate}
  \item On \eref{eq_master_eq_full} we notice that the second term on the
  right-hand side can be written as
  \begin{equation}
    P^*(x, t)\int_0^1 dx' \; \phi_t(x' \mid x) = P^*(x, t),
    \label{eq_integral_transition}
  \end{equation}
  where we took the term $P^*(x, t)$ outside of the integral and used the fact
  that the transition probability per unit time $\phi_t(x' \mid x)$ should be
  normalized regardless of the time window. In other words, the probability of
  transitioning from $f$ to anywhere else (including staying at $f$) should add
  up to one regardless of the time window we observe.
  \mrm{Need to check this statement and that the units make sense}
  \item Having this result we can rewrite \eref{eq_master_eq_full} as
  \begin{equation}
    \dt{P^*(x, t)} = \int_0^1 dx' \;
    \phi_t(x \mid x') P^*(x', t) - P^*(x, t).
    \label{eq_master_eq_rearange}
  \end{equation}
  We are almost there! All is left is to notice that if we were to Taylor
  expand the left-hand side of \eref{eq_kimura_master_lim} with respect to time
  up to first order (as it is often done for time derivatives) we would obtain
  \begin{equation}
    P(x, t + \Dt) = P(x, t) + \dt{P(x, t)}\Dt + \mathcal{O}(\Dt^2).
  \end{equation}
  That means we can send the second term on the the right-hand side of
  \eref{eq_master_eq_rearange} to the left and rewrite the equation as
  \begin{equation}
    {P^*(x, t + \Dt) \over \Dt} = \int_0^1 dx' \;
    \phi_t(x \mid x') P^*(x', t),
  \end{equation}
  which is equivalent to \eref{eq_kimura_master_lim} where instead of having
  the integration over the jump size $\varepsilon$ the integration is done over
  the final position $x'$.
\end{enumerate}


\chapter{Interest}

A day count factor between the dates $t$ and $s$ is denoted by $\dayc{s,t}$. We assume that the day count factor is a sufficiently smooth function and and that $\dayc{s,t} = t-s$ when $s$ and $t$ are close.

\subsection{Zero-coupon bonds}

A $T$-maturity zero-coupon bond is a promise to pay one unit of currency at time $T$. This is instrument is also called as pure discount bond. The price of this bond at time $t$ is denoted by $Z(t,T)$ and $Z(T,T) = 1$ if there is no risk of default. We shall call this a $T$-bond.

In the following we shall assume that for every $0 \leq t$ and $t \leq T$, there exists a frictionless market for $Z(t,T)$ and every zero-coupon bond has no risk of default. We also assume that $Z(t,T)$ is differentiable with respect to the time of maturity $T$.

\subsection{Riskless simple rate of interest}

We assume that $0 < t \leq T < S$. If we enter the following contranct at time $0$. At the time $t$ we shall
  \begin{enumerate}
    \item sell one $T$-bond with price $Z(t,T)$ and
    \item we shall buy $Z(t,T) / Z(t,S)$ $S$-bonds for the price $Z(t,S)$
  \end{enumerate}
At time $t$ the net payments are $0$. We pay $1$ at time $T$ and receive $Z(t,T) / Z(t,S)$ at time $S$. We netted
  \begin{align}
   1 - \frac{Z(t,T)}{Z(t,S)}
  \end{align}
but the payments happened at different instances, so the terms are not of equal value. We say that the simple forward rate for $(T,S)$ contracted at $t$ is $L(t,T,S)$ satisfying the equation
  \begin{align}
   1 + \dayc{T,S} L(t,T,S) = \frac{Z(t,T)}{Z(t,S)}
  \end{align}
or equivalently
  \begin{align}
  \label{simplyinterestrate}
   L(t,T,S) = \frac{Z(t,T) - Z(t,S)}{ \dayc{T,S}  Z(t,S)} .
  \end{align}
If $t=T$, then we denote this rate by $L(t,S)$ and call it as the simple spot rate for $(t,S)$ and thus
  \begin{align}
    L(t,S) = \frac{ 1 - Z(t,S) }{ \dayc{t,S} Z(t,S) } .
  \end{align}

\subsection{Riskless continously compounded rate of interest}

Similarly to the equation \ref{simplyinterestrate}, we define the continously compounded forward rate for $(T,S)$ contracted at $t$ as $R(t,T,S)$ satisfying the equation
  \begin{align}
   \e^{ \dayc{T,S} R(t,T,S)} = \frac{Z(t,T)}{Z(t,S)} .
  \end{align}
or equivalently
  \begin{align}
    R(t,T,S) = \frac{ \log Z(t,T) - \log Z(t,S) }{ \dayc{T,S} }
  \end{align}
If $t=T$, then we denote this rate by $R(t,S)$ and call it as the continously compounded spot rate for $(t,S)$. Now
  \begin{align}
    R(t,S) = - \frac{ \log (Z(t,S)) }{ \dayc{T,S} } .
  \end{align}
The motivation for continous compounding comes from the fact that
  \begin{align}
    \e^x = \lim_{n \rightarrow \infty} \left( 1 + \frac{x}{n} \right)^n,
  \end{align}
where the right-side is the limit of the compounding interest. By the power serie representation
  \begin{align}
    \e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \ldots
  \end{align}
we see that $R(t,T,S) \approx L(t,T,S)$, when $S$ is very close to $T$.

\subsection{Riskless instantaneous rates}

The instantanenous forward rate with maturity $T$ contracted at time $t$ is defined by
  \begin{align}
    f(t,S) &= \lim_{S \rightarrow T^+} L(t,T,S) \\
            &= \lim_{S \rightarrow T^+} \frac{Z(t,T) - Z(t,S)}{ \dayc{T,S}  Z(t,S)} ¸\\
            &= \lim_{S \rightarrow T^+} \frac{1}{ Z(t,S) } \frac{Z(t,T) - Z(t,S)}{ S-T } \\
            &= - \frac{1}{ Z(t,T) } \frac{ \partial Z(t,T) }{ \partial T } \\
            &= - \frac{ \partial \log Z(t,T) }{ \partial T } .
  \end{align}
Thus
  \begin{align}
    Z(t,S) = Z(t,T) \exp \left( - \int_T^S f(t,s) \dx s \right) \\
  \end{align}
and in particulary
  \begin{align}
    Z(t,S) = \exp \left( - \int_t^S f(t,s) \dx s \right) \\
  \end{align}

\subsection{Short rate $r(t)$ and the bank account}

The short rate $r(t)$ at time $t$ is defined by
  \begin{align}
    r(t) = f(t,t).
  \end{align}
The bank account $B(t)$ is defined by
  \begin{align}
    B(t) = \exp \left( \int_0^T r(s) \dx s \right) .
  \end{align}
Note that $B(0) = 1$ and equivalently the dynamics may be given by
  \begin{align}
    &\dx B(t) = r(t) B(t) \dx t \\
    &B(0) = 1
  \end{align}

\section{Short-rate model for interest rates}

The classical approach to price interest rates derivates is the assume the existance of a short-rate process $r(t)$ which is a solution to a stochastic differential equation
  \begin{align}
    \dx r(t) &= \mu(t, r(t)) \dx t + \sigma(t, r(t)) \dx W(t), \\
    r(0) = r_0,
  \end{align}
where $r_0$ is a constant and $W_t$ is brownian motion under the physical probability measure $\Pf$. We assume the existance of risk-free asset $B$ with price process
  \begin{align}
    \dx B(t) = r(t) B(t) \dx t .
  \end{align}
We also assume
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item the market for $T$-bonds exists and is arbitrage free,
    \item the price of a $T$-bond is given by
      \begin{align}
        Z(t,T) = F(t,r(t),T) ,
      \end{align}
    where $F$ is a smooth function and
    \item $Z(T,T) = 1$ for all $r(T)$.
  \end{enumerate}

We have not assumed that $r(t)$ is a tradable asset, but we have assumed that there are markets for derivates of the short-rate. If $F(t,r(t))$ is a smooth function of $t$ and $r(t)$, then we may use It\'{o}'s lemma to derive
  \begin{align}
    \label{interest_rate_derivative_process}
    \dx F = \left( F_t + \mu F_r + \frac{1}{2} \sigma^2 F_{rr} \right) \dx t + \sigma F_r \dx W ,
  \end{align}
where we have supressed arguments and subscripts are partial derivates. We now combine to different short-rate derivates $V_1$ and $V_2$ to a portfolio
  \begin{align}
    V = V_1 - x V_2.
  \end{align}
We assume that the price process $V_i$ is a function of $t$ and $r(t)$ so that $\dx V_i = \mu_i \dx t + \sigma_i \dx W$, where $\mu_i$ and $\sigma_i$ can be derived from equation \ref{interest_rate_derivative_process}. Thus
  \begin{align}
    \dx V = (\mu_1 - x \mu_2) \dx t + (\sigma_1 - x \sigma_2) \dx W .
  \end{align}
If
  \begin{align}
    x = \frac{\sigma_1}{\sigma_2} ,
  \end{align}
then
  \begin{align}
    \dx V &= (\mu_1 - \frac{\sigma_1}{\sigma_2} \mu_2) \dx t \\
      &= r V \dx t \\
      &= r ( V_1 - \frac{\sigma_1}{\sigma_2} V_2 ) \dx t,
  \end{align}
where we have assumed that the absense of arbitrage implies that the now risk-free portfolio must have risk-free interest rate as the growth rate. Thus
  \begin{align}
    \mu_1 - rV_1 = \frac{\sigma_1}{\sigma_2} ( \mu_2 - rV_2 )
  \end{align}
and therefore we have a well-defined function
  \begin{align}
    \lambda(t,r(t)) &= \frac{\mu_1(t,r(t)) - r(t)V_1(t,r(t))}{\sigma_2(t,r(t))} \\ &= \frac{\mu_2(t,r(t)) - r(t)V_2(t,r(t))}{\sigma_2(t,r(t))}
  \end{align}
which depends only on arguments $t$ and $r(t)$ since the derivates can be arbitralily chosen. We call $\lambda(t,r(t))$ as the market-price of risk. Thus the price $F$ of an arbitrary interest derivate must satisfy the differential equation
  \begin{align}
     F_t + ( \mu - \lambda \sigma ) F_r + \frac{1}{2} \sigma^2 F_{rr} - r F = 0 ,
  \end{align}
which is a close cousin to the Black-Scholes differential equation. But is more complicated, because the market price of the risk is present. Since the short-rate is not tradable, it may not be hedged and therefore this market is not complete. Since the function $\lambda$ is exogenous to the model, the solution and therefore the price will not be unique. In other words, we have multiple martingale measures.

\subsection{Short-rate and the stochastic discount factor}

We often assume that there is a short-rate $r_t$ that is applied to a bank account continously. It may or may not be stochastic. The bank account evolves according to the differential equation
  \begin{align}
    \dx B_t = r_t B_t,
  \end{align}
where $B_t$ is value of the account at the time $t$. We assume unless otherwise noted that $B_0 = 1$. Thus
  \begin{align}
    B_t = B_0 \exp \left( \int_0^t r_s \dx s \right) .
  \end{align}
We assume that $r_t \geq 0$ for all $t \geq 0$ and this means that the bank account is riskless in the sense that no deposited money never loses value.

A related concept is the stochastic discount factor $D(t,T)$ between the dates $t < T$, which is defined by
  \begin{align}
    D(t,T) = \frac{B_t}{B_T} = \exp \left( - \int_t^T r_s \dx s \right) .
  \end{align}
It is the present value as seen at the time $t$ of one unit of currency payable at time $T$. If the short-rate is stochastic, then the discount factor is non-deterministic.

\subsection{Zero-coupon bonds}

A $T$-maturity zero-coupon bond is a promise to pay one unit of currency at time $T$. This is instrument is also called as pure discount bond. The price of this bond at time $t$ is denoted by $Z(t,T)$.

If $r_t$ is deterministic and there is no arbitrage, then $D(t,T) = Z(t,T)$, but this does not hold in general. The zero-coupon bond is a derivate itself and can be replicated with a simple buy-and-hold strategy, we have that
  \begin{align}
    Z(t,T) &= B_t \E^{\Pm} \left( \frac{1}{B_T} \ | \ F_t \right) \\
      &= \E^{\Pm} \left( \exp \left( - \int_t^T r_s \dx s \right) \ | \ F_t \right) \\
      &= \E^{\Pm} \left( D(t,T) \ | \ F_t \right) .
  \end{align}
A $T$-maturity zero-coupon bond is often a good choise for num\'{e}raire and the assosiated EMM is called $T$-forward measure. We shall see in Theorem \ref{expectedforwardrate} why this is a descriptive name. Since $Z(T,T) = 1$, the price of a derivate can be calculated by
  \begin{align}
    h_t = Z(t,T) \E^T ( H_T \ | \ \F_t ).
  \end{align}

\subsection{Forward rates}

A forward rate $F(t;T,S)$ between the dates $T < S$ is the rate that can be locked at the time $t$ for investment for the future period $[T,S]$. For simple compounding it is defined by
  \begin{align}
    F(t;T,S) = \frac{1}{\dayc{T,S}} \left( \frac{Z(t,T) - Z(t,S) }{Z(t,S)} \right)
  \end{align}
If $\dayc{T,S} \approx S-T$, when $T \approx S$ and the function $S \mapsto F(t,T,S)$ is smooth, then
  \begin{align}
    \lim_{S \rightarrow T^+} F(t;T,S) &= - \lim_{S \rightarrow T^+} \frac{1}{Z(t,S)} \frac{Z(t,S) - Z(t,T)}{S-T} \\
      &= - \frac{1}{Z(t,S)} \frac{\partial Z(t,T) }{\partial T} \\
      &= - \frac{\partial \log Z(t,T) }{\partial T}
  \end{align}
In practice, the zero curve is constructed so that the smoothness is guarateed and we may define the instantenous forward rate as
  \begin{align}
    f(t,T) &= - \frac{\partial \log Z(t,T) }{\partial T}
  \end{align}
meaning that
  \begin{align}
    Z(t,T) &= \exp \left( - \int_t^T f(t,s) \dx s \right) .
  \end{align}
The instantenous forward rate can be seen as the expectation of the future rate.

\begin{thm}
  \label{expectedforwardrate}
If there is a derivate $H$ with final payout $H_T = r_T$ at the time $T$ and the short-rate process is smooth, then
  \begin{align}
    \E^T ( R_T \ | \ \F_t ) = f(t,T) .
  \end{align}
\end{thm}

\begin{proof}
Under the $T$-forward measure we have that $h_t = Z(t,T) \E^T ( r_T \ | \ \F_t )$ and risk-neutral valuation gives the price
  \begin{align}
    h_t &= B_t \E^{\Pm} \left( \frac{r_T}{B_T} \ | \ F_t \right) \\
      &= \E^{\Pm} \left( \exp \left( -\int_t^T r_s \dx s \right) r_T \ | \ F_t \right) \\
      &= - \E^{\Pm} \left( \frac{ \partial \exp \left( -\int_t^T r_s \dx s \right)}{\partial T} \ | \ F_t \right) \\
      &= - \frac{\partial }{\partial T} \E^{\Pm} \left( \exp \left( -\int_t^T r_s \dx s \right) \ | \ F_t \right) \\
      &= - \frac{\partial Z(t,T)}{\partial T},
  \end{align}
where we assumed that the smoothness allows us the change the order of differentation and integration. Now
  \begin{align}
    \E^T ( r_T \ | \ \F_t ) &= -\frac{1}{Z(t,T)} \frac{\partial Z(t,T)}{\partial T} = f(t,T) .
  \end{align}
\end{proof}

A forward rate agreement (FRA) is a contract that lock a future interest rate . A FRA with a fixed interest rate $K$, expiry time $T$ and maturity time $S$ pays $K \dayc(T,S)$ at the maturity $S$. The fixed rate $K$ is set when the contract

A caplet is a call option on interest rate $L(S,T)$ for some $0 \leq S < T$. With strike $K$, it has pay-off
  \begin{align}
     \Caplet (T,S,T,K) = (L(S,T) - K)^+ .
  \end{align}
at the time $T$. By definition,
  \begin{align}
    Z(S,T) = \frac{1}{1+\tau L(S,T)},
  \end{align}
where $\tau$ is the day-count adjustment between the dates $S$ and $T$. Thus the pay-out of the caplet can be written as
  \begin{align}
    \Caplet (T, S,T,K) &= \left( \frac{1}{\tau} \left( \frac{1}{Z(S,T)} - 1 \right) - K \right)^+.
  \end{align}
As the rate $L(S,T)$ is set at the time $S$, the price of the cap at the time $S$ is
  \begin{align}
     \Caplet (S,S,T,K) &= Z(S,T) \Caplet (T,S,T,K) \\
     &= \frac{1}{\tau} \left(  1 - Z(S,T)  - \tau K Z(S,T) \right)^+ \\
      &= \frac{1}{\tau} \left(  1 - Z(S,T) ( 1 + \tau K ) \right)^+ \\
      &= \frac{ 1 + \tau K }{\tau} \left( \frac{1}{1 + \tau K}  - Z(S,T) \right)^+
  \end{align}
The right-side is the price at the time $S$ of $\frac{ 1 + \tau K }{\tau}$ put options with strike $\frac{1}{1 + \tau K}$ on a zero-coupons bonds with maturity $T$. Therefore the a caplet can be replicated as put options on a zero-coupon bond.

Similarly, a floorlet is a put option on interest rate $L(S,T)$ for some $0 \leq S < T$. With strike $K$, it has pay-off
  \begin{align}
     \Floorlet (T,S,T,K) = (K - L(S,T))^+
  \end{align}
at the time $T$. Now we can use the same argumentation as earlier with only the signs of $K$ and $L(S,T)$ changed and we see that
  \begin{align}
     \Floorlet (S,S,T,K) &= \frac{ 1 + \tau K }{\tau} \left( Z(S,T) - \frac{1}{1 + \tau K}  \right)^+,
  \end{align}
where the caplet has been replicated as call option on a zero-coupon bond $Z(S,T)$.


\subsection{Semiring ideals}\label{subsec:semiring_ideals}

When regarding \hyperref[def:semiring]{semirings} as \hyperref[def:semimodule]{semimodules} over themselves, as per \fullref{thm:semiring_is_semimodule}, we obtain the important notion of ideals.

\begin{definition}\label{def:semiring_ideal}\mimprovised
  Fix a semiring \( R \). A \term{left ideal} of \( R \) is a \hyperref[def:semimodule/submodel]{sub-semimodule} of \( R \) when regarded as a left semimodule over itself, and a \term{right ideal} is defined analogously.

  If \( I \) is both a left and right ideal of \( R \), we say that it is a \term{two-sided ideal} or simply \term{ideal}. More explicitly, \( I \) is a two-sided ideal of \( R \) if it is a \hyperref[def:monoid/submodel]{submonoid} of the additive monoid of \( R \) that is closed under left and right multiplication, i.e. \( RI = I = IR \).

  When \hyperref[def:ring/quotient]{quotient rings} are involved, we will have no choice but to work with two-sided ideals. If multiplication is commutative, every left ideal is a right ideal and there is no distinction between the two. Otherwise, we will usually consider left ideals by default. Right ideals are left ideals in the \hyperref[def:semiring/opposite]{opposite semiring}, and thus we lose no generality.

  \begin{thmenum}
    \thmitem{def:semiring_ideal/generated} For an arbitrary subset \( A \) of \( R \), we call the (left) \hyperref[def:semimodule/submodel]{linear span} of \( A \) the left ideal \term{generated} by \( A \). Explicitly, this is the set
    \begin{equation*}
      \sum_{a \in A} A a = \set*{ \sum_{k=1}^n t_k a_k \given* n > 0 \T{and, for} k < n, t_k \in R \T{and} a_k \in A }.
    \end{equation*}

    If \( A = \set{ a_1, \ldots, a_n } \), we say that the ideal is \term{finitely generated} and write
    \begin{equation*}
      A a_1 + \cdots + A a_n.
    \end{equation*}

    For right ideals, this becomes
    \begin{equation*}
      a_1 A + \cdots + a_n A.
    \end{equation*}

    This is discussed in \fullref{thm:generators_via_polynomials} for the case of commutative rings, where we use the notation \( \braket{ A } \). In general rings, we are more explicit for the sake of avoiding possible confusion.

    \thmitem{def:semiring_ideal/principal} An ideal generated by a single element is called a \term{principal ideal}. In a general ring, there can be left, right and two-sided principal ideals.

    \thmitem{def:semiring_ideal/product} The \term{product ideal} \( IJ \) of \( I \) and \( J \) is
    \begin{equation*}
      \set*{ \sum_{k=1}^n i_k j_k \given* n > 0 \T{and, for} k < n, i_k \in I \T{and} j_k \in J }.
    \end{equation*}

    This notation is unfortunately inconsistent with the pointwise product \( \set{ ij \mid i \in I, j \in J } \) from \fullref{def:magma/power_set}; it is actually the ideal generated by the pointwise product.

    \begin{figure}[h]
      \caption{Some important ideals}\label{fig:ideal_hierarchy}
      \smallskip
      \hfill
      \begin{forest}
        for tree=
          {
            s sep=2.25cm
          }
        [
          {\hyperref[def:semiring_ideal]{ideal}}, name=ideal
            [{\hyperref[def:semiring_ideal/principal]{principal}}, name=principal]
            [
              {\hyperref[def:semiring_ideal/prime]{prime}}, name=prime
                [{\hyperref[def:semiring_ideal/maximal]{maximal}}, name=maximal]
            ]
            [{\hyperref[def:radical_ideal]{radical}}, name=radical]
        ]
        \draw[->, dashed] (prime) to node[below] {\hyperref[def:semiring/commutative]{commutative}} (radical);
        \draw[->, dashed] (prime) to[out=west, in=west] node[left] {\hyperref[def:principal_ideal_domain]{PID}} (maximal);
        \draw[->, dashed] (ideal) to[out=west, in=north] node[above] {\hyperref[def:principal_ideal_domain]{PID}} (principal);
      \end{forest}
      \hfill\hfill
    \end{figure}

    \thmitem{def:semiring_ideal/prime}\mcite[85]{Golan2010} If \( P \) is a proper ideal and if from \( IJ \subseteq P \) it follows that \( I \subseteq P \) or \( J \subseteq P \) (or both), we say that \( P \) is a \term{prime ideal}.

    When working with commutative semirings, \fullref{thm:def:semiring_ideal/prime_pointwise} is instead sometimes taken as the definition of a prime ideal.

    \thmitem{def:semiring_ideal/coprime}\mcite[18]{КоцевСидеров2016} If \( I + J = R \) for proper ideals \( I \) and \( J \), we say that \( I \) and \( J \) are \term{coprime}. Equivalently, \( I \) and \( J \) are coprime if their sum contains a unit.

    \thmitem{def:semiring_ideal/maximal} A (left) \term{maximal ideal} is a proper (left) ideal that is maximal with respect to set inclusion. The maximal ideals are the predecessors of \( R \) in the lattice of (left) ideals described in \fullref{thm:semiring_of_ideals}.
  \end{thmenum}
\end{definition}

\begin{remark}\label{rem:semiring_ideal_as_sub_semiring}
  A proper semiring ideal is a canonical example of a nonunital sub-semiring. As a consequence of \fullref{thm:def:semiring_ideal/ideal_containing_unit}, a proper ideal cannot contain the multiplicative identity \( 1 \), and is thus not a sub-semiring unless we allow sub-semirings to not contain \( 1 \).
\end{remark}

\begin{proposition}\label{thm:def:semiring_ideal}
  The left \hyperref[def:semiring_ideal]{ideals} of a semiring \( R \) have the following basic properties:
  \begin{thmenum}[series=thm:def:semiring_ideal/properties]
    \thmitem{thm:def:semiring_ideal/ideal_containing_unit} An ideal contains a \hyperref[def:divisibility/unit]{unit} if and only if it is not proper. In particular, \( R = \braket{ 1_R } \).

    \thmitem{thm:def:semiring_ideal/units} A semiring element is a \hyperref[def:divisibility/unit]{unit} if and only if it does not belong to any proper ideal.

    \thmitem{thm:def:semiring_ideal/division} We have \( \braket{ x } \subseteq \braket{ y } \) if and only if \( y \mid x \) for two-sided ideals and two-sided divisors.

    More generally, we have \( Rx \subseteq Ry \) if and only if \( y \) is a right divisor of \( x \). Note how \( Rx \) and \( Ry \) are \hi{left} principal ideals but \( y \) is a \hi{right} divisor.

    \thmitem{thm:def:semiring_ideal/union} The union of a \hyperref[eq:def:partially_ordered_set/homomorphism/sequence]{monotone sequence}
    \begin{equation*}
      I_1 \subseteq I_2 \subseteq \cdots
    \end{equation*}
    if ideals is again an ideal.

    \thmitem{thm:def:semiring_ideal/maximal_is_prime} Every (left or right) \hyperref[def:semiring_ideal/maximal]{maximal ideal} is \hyperref[def:semiring_ideal/prime]{prime}.

    \Fullref{thm:def:principal_ideal_domain/prime_ideal_is_maximal} is a converse that holds for \hyperref[def:principal_ideal_domain]{principal ideal domains}.

    \thmitem{thm:def:semiring_ideal/coprime_product} We have \( IJ \subseteq I \cap J \). The converse inclusion holds if \( R \) is \hyperref[def:semiring/commutative]{commutative} and if \( I \) and \( J \) are \hyperref[def:semiring_ideal/coprime]{coprime}.
  \end{thmenum}

  The following require \( R \) to be \hyperref[def:semiring/commutative]{commutative}:
  \begin{thmenum}[resume=thm:def:semiring_ideal/properties]
    \thmitem{thm:def:semiring_ideal/product_of_principal_ideals} In a commutative semiring, the product of the principal ideals \( \braket{x} \) and \( \braket{y} \) is \( \braket{xy} \).

    \thmitem{thm:def:semiring_ideal/prime_pointwise} In a commutative semiring, an equivalent condition to \( P \) being \hyperref[def:semiring_ideal/prime]{prime} is that \( xy \in P \) implies \( x \in P \) or \( y \in P \) (or both).

    \thmitem{thm:def:semiring_ideal/prime_is_radical} In a commutative semiring, every \hyperref[def:semiring_ideal/prime]{prime ideal} is \hyperref[def:radical_ideal]{radical}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:semiring_ideal/ideal_containing_unit} We will prove that there exists a unit \( u \in I \) if and only if \( I = R \).

  \SufficiencySubProof* Let \( u \in I \) be a unit. Then \( 1_R = u^{-1} u \in I \). It follows that \( 1_R \cdot x = x \) for any \( x \in R \), thus \( IR = R \). But \( I \) is an ideal, hence we have that \( IR = I \). Therefore, \( I = IR = R \).

  \NecessitySubProof* If \( I = R \), then obviously \( 1_R \in I \).

  An analogous proof follows for the case when \( I \) is a right ideal.

  \SubProofOf{thm:def:semiring_ideal/units}

  \SufficiencySubProof* Suppose that \( x \) is a unit and that \( x \) belongs to some proper ideal \( I \). Then \( Rx = R \), implying that \( I = R \), which is a contradiction.

  \NecessitySubProof* Suppose that \( x \) does not belong to any proper ideal. Then \( Rx \) is not a proper ideal, implying that \( R = Rx \). There exists some \( y \) such that \( yx = 1_R \). Hence, \( x \) is a unit.

  \SubProofOf{thm:def:semiring_ideal/division}

  \SufficiencySubProof* Suppose that \( Rx \subseteq Ry \). Then \( x \in Ry \), and hence there exists an element \( l \) of \( R \) such that \( x = ly \). So \( y \) is a right divisor of \( x \).

  \NecessitySubProof* Suppose that \( y \) is a right divisor of \( x \). Then there exists an element \( l \) of \( R \) such that \( x = ly \). Thus, \( x \in Ry \), and hence \( Rx \subseteq Ry \).

  \SubProofOf{thm:def:semiring_ideal/union} Follows from \fullref{thm:def:semimodule/union}.

  \SubProofOf{thm:def:semiring_ideal/maximal_is_prime} Let \( M \) be a maximal left ideal and let \( IJ \subseteq M \). Suppose that both \( M \setminus I \) and \( M \setminus J \) are nonempty.

  Then there exist elements \( i \in I \), \( j \in J \), \( m_i \in M \) and \( m_j \in M \) such that
  \begin{equation*}
    i + m_i = j + m_j = 1_R.
  \end{equation*}

  Then
  \begin{equation*}
    1_R = (i + m_i) (j + m_j) = \underbrace{ij}_{IJ} + \overbrace{m_i j}^M + \underbrace{m_j i}_M + \overbrace{m_i m_j}^M.
  \end{equation*}

  Hence,
  \begin{equation*}
    1_R = (i + m_i) (j + m_j) \in M,
  \end{equation*}
  which contradicts our assumption that \( M \) is maximal.

  Therefore, \( M \setminus I \) and \( M \setminus J \) cannot both be nonempty.

  \SubProofOf{thm:def:semiring_ideal/coprime_product} We will first show that \( IJ \subseteq I \cap J \). Let
  \begin{equation*}
    \sum_{k=1}^n x_k y_k \in IJ.
  \end{equation*}

  For each \( k \), \( x_k y_k \) belongs to both \( I \) and to \( J \). Hence, the sum over \( k \) also belongs to the intersection. Therefore,
  \begin{equation*}
    IJ \subseteq I \cap J.
  \end{equation*}

  \SubProofOf{thm:def:semiring_ideal/product_of_principal_ideals}
  \SufficiencySubProof* Let \( z \in \braket{x} \braket{y} \). Then there exist elements \( x_z \) of \( \braket{x} \) and \( y_z \) of \( \braket{y} \) such that \( z = x_z y_z \), and elements \( r_x \) and \( r_y \) of \( R \) such that \( x r_x = x_z \) and \( y r_y = y_z \).

  Therefore,
  \begin{equation*}
    z = \underbrace{(x r_x) (y r_y)}_{(xy) (r_x r_y)} \in \braket{xy}.
  \end{equation*}

  \NecessitySubProof* Let \( z \in \braket{xy} \). Then there exists an element \( r \) of \( R \) such that \( z = rxy = (rx)(y) \), hence \( z \in \braket{x} \braket{y} \).

  \SubProofOf{thm:def:semiring_ideal/prime_pointwise} Suppose that \( R \) is commutative.
  \SufficiencySubProof* Let \( P \) be prime and let \( xy \in P \). Then \( \braket{xy} \subseteq P \). By \fullref{thm:def:semiring_ideal/product_of_principal_ideals}, \( \braket{x} \braket{y} \subseteq P \), and hence \( \braket{x} \subseteq P \) or \( \braket{y} \subseteq P \). Therefore, \( x \in P \) or \( y \in P \).

  \NecessitySubProof* Let \( P \) be an ideal such that \( xy \in P \) implies \( x \in P \) or \( y \in P \). Let \( IJ \subseteq P \) and suppose that there exist \( i \in I \setminus P \) and \( j \in J \setminus P \).

  Obviously \( ij \in I \). But since \( P \) is prime, it follows that \( i \in P \) or \( j \in P \).

  The obtained contradiction shows that \( I \) or \( J \) must be a subset of \( P \). Therefore, \( P \) is prime.

  \SubProofOf{thm:def:semiring_ideal/prime_is_radical} Suppose that \( R \) is commutative, let \( P \) be a prime ideal and let \( x^n \in P \) for \( n > 0 \). We will show that \( x \in P \).

  We proceed via induction on \( n \). The case \( n = 1 \) is trivial. Suppose that \( x^{n-1} \in P \) implies \( x \in P \), and let \( x = x \cdot x^{n-1} \in P \). Since \( P \) is prime and \( R \) is commutative, by \fullref{thm:def:semiring_ideal/prime_pointwise}, \( x \in P \) or \( x^{n-1} \in P \). In the latter case, we use the inductive hypothesis to show that \( x \in P \).

  Generalizing on \( x \), we conclude that \( P \) is a radical ideal.
\end{proof}

\begin{proposition}\label{thm:semiring_of_ideals}
  \hfill
  \begin{thmenum}
    \thmitem{thm:semiring_of_ideals/semiring} The set \( \mscrI \) of all ideals of a semiring \( R \) is itself a semiring with the set addition and multiplication from \hyperref[def:semiring/power_set]{power set semiring} \( \pow(R) \).

    \thmitem{thm:semiring_of_ideals/order} Furthermore, \( \mscrI \) is an \hyperref[def:ordered_semiring]{ordered semiring} with respect to set inclusion.

    \thmitem{thm:semiring_of_ideals/lattice} The \hyperref[def:partially_ordered_set_extremal_points/supremum_and_infimum]{supremum} of \( I \) and \( J \) is their sum \( I + J \) and their \hyperref[def:partially_ordered_set_extremal_points/supremum_and_infimum]{infimum} is their intersection \( I \cap J \). With this, \( \mscrI \) becomes a lattice.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:semiring_of_ideals/semiring} Associativity and commutativity in \( \mscrI \) are inherited from \( R \), as well as both left and right distributivity. Distributivity ensures that \( I + J \) is an ideal, while associativity of multiplication ensures that \( IJ \) is an ideal.

  \SubProofOf{thm:semiring_of_ideals/order} We must now prove that the partial order \( \subseteq \) is compatible with addition and multiplication. Suppose that \( I \subseteq J \) and let \( H \) be any ideal in \( \mscrI \). Then
  \begin{equation*}
    I + H \subseteq J + H
  \end{equation*}
  and
  \begin{equation*}
    IH \subseteq JH.
  \end{equation*}

  Therefore, \( \mscrI \) is an ordered semiring.

  \SubProofOf{thm:semiring_of_ideals/lattice} Since \( 0 \in I \), obviously \( I \subseteq I + J \), and thus \( I + J \) is an upper bound of \( I \) and \( J \). If \( H \) is any other upper bound, it must contain the sums of all elements of \( I \) and all elements of \( J \), hence \( I + J \subseteq H \). Therefore, \( \sup\set{I, J} = I + J \).

  For \( I \cap J \), it is an infimum of \( I \) and \( J \) as a consequence of \fullref{thm:boolean_algebra_of_subsets/meet}.
\end{proof}

\begin{example}\label{ex:def:semiring_ideal}
  We list several examples of \hyperref[def:semiring_ideal]{semiring ideals}.
  \begin{thmenum}
    \thmitem{ex:def:semiring_ideal/not_principal} The simplest example of an ideal that is not principal is the ideal \( \braket{ 2, 3 } \) in \( \BbbN \).

    To see that it is not principal, suppose that \( \braket{ n } = \braket{ 2, 3 } \) for some natural number \( n \). This implies that there exist nonzero numbers \( a \) and \( b \) such that \( n = 2a + 3b \). Hence, \( n > 2a > a \) and \( n > 3b > b \). But then neither \( 2 \) nor \( 3 \) belongs to \( \braket{ n } \), contradicting our assumption.

    \thmitem{ex:def:semiring_ideal/prime_not_maximal} The zero ideal \( \braket{ 0 } \) in \( \BbbN \) is \hyperref[def:semiring_ideal/prime]{prime} but not \hyperref[def:semiring_ideal/maximal]{maximal}.

    Indeed, since \( \BbbN \) is entire, \( \braket{ 0 } = \set{ 0 } \) and thus \( \braket{ 0 } \) is prime. But it is not maximal since it is contained in every other ideal.

    \thmitem{ex:def:semiring_ideal/natural_numbers_principal_ideals} For natural numbers, \( \braket{ n } = \braket{ m } \) implies \( n = m \).

    Indeed, by \fullref{thm:def:semiring_ideal/division}, \( n \mid m \) and \( m \mid n \). Thus, there exist numbers \( a \) and \( b \) such that \( n = am \) and \( m = bn \), hence \( n = abn \). Since the semiring \( \BbbN \) is \hyperref[def:entire_semiring]{entire}, we can cancel \( n \) to obtain \( ab = 1 \). Then \( a = b = 1 \), and hence \( n = m \).

    \thmitem{ex:def:semiring_ideal/prime_numbers} A natural number \( n \) is \hyperref[def:prime_number]{prime} if and only if \( \braket{n} \) is a \hyperref[def:semiring_ideal/prime]{prime ideal} in \( \BbbN \).

    Suppose that \( n \) is prime and let \( n \mid mk \). From \fullref{thm:euclids_lemma} it follows that either \( n \mid k \) or \( n \mid m \), hence \fullref{thm:def:semiring_ideal/prime_pointwise} is satisfied and \( \braket{ n } \) is a prime ideal.

    In the other direction, suppose that \( \braket{ n } \) is a prime ideal and let \( n = ab \). By \fullref{thm:def:semiring_ideal/product_of_principal_ideals}, \( \braket{ n } = \braket{ a } \braket{ b } \). Since \( \braket{ n } \) is a prime ideal, \( \braket{ a } \subseteq \braket{ n } \) or \( \braket{ b } \subseteq \braket{ n } \).

    Therefore, \( \braket{ n } = \braket{ a } \) or \( \braket{ n } = \braket{ b } \). By \fullref{ex:def:semiring_ideal/natural_numbers_principal_ideals}, \( n = a \) or \( n = b \), which in turn implies that the other is a unit.

    Therefore, \( n \) is a prime number.
    \thmitem{ex:def:semiring_ideal/matrices} Consider the matrix algebra \( \BbbZ^{2 \times 2} \). The set
    \begin{equation*}
      \set[\Bigg]
      {
        \begin{pmatrix}
          0 & a \\
          0 & b
        \end{pmatrix}
        \given*
        a, b \in \BbbZ
      }.
    \end{equation*}
    is a left ideal. It is not a right ideal, however, because
    \begin{equation*}
      \begin{pmatrix}
        1 & 0 \\
        1 & 0
      \end{pmatrix}
      \begin{pmatrix}
        0 & 1 \\
        0 & 1
      \end{pmatrix}
      =
      \begin{pmatrix}
        0 & 1 \\
        0 & 1
      \end{pmatrix}.
    \end{equation*}

    \thmitem{ex:def:semiring_ideal/polynomial_ideals} Consider the bivariate \hyperref[def:polynomial_algebra]{polynomial semiring} \( \BbbN[X, Y] \) over natural numbers. Since \( (X + Y)^2 = X^2 + 2XY + Y^2 \), we have
    \begin{equation*}
      \braket{ X^2 + 2XY + Y^2 } \subseteq \braket{ X + Y }.
    \end{equation*}

    \thmitem{ex:def:semiring_ideal/ideal_polynomials} Ideals in polynomial semirings are often studied, but we can also study polynomials in ideal semirings, i.e. polynomials over the semiring \( \mscrI \) of ideals of a semiring \( R \). For example,
    \begin{equation*}
      I^2 J + K
    \end{equation*}
    is a trivariate polynomial function over \( \mscrI \).

    \thmitem{ex:def:semiring_ideal/maximal_induced_coprime} If \( M \) is a maximal ideal and \( x \in R \setminus M \), then \( M \) and \( \braket{ x } \) are \hyperref[def:semiring_ideal/coprime]{coprime}.
  \end{thmenum}
\end{example}

\begin{theorem}[Maximal ideal theorem]\label{thm:maximal_ideal_theorem}\mcite[prop. 6.59]{Golan2010}
  Every proper \hyperref[def:semiring_ideal]{semiring ideal} is contained in a \hyperref[def:semiring_ideal/maximal]{maximal ideal}.

  Within \hyperref[def:zfc]{\logic{ZF}}, this theorem is equivalent to the \hyperref[def:zfc/choice]{axiom of choice} --- see \fullref{thm:axiom_of_choice_equivalences/maximal_ideal}.
\end{theorem}
\begin{proof}
  We will discuss equivalence with \fullref{thm:zorns_lemma}.

  \ImplicationSubProof[thm:zorns_lemma]{Zorn's lemma}[thm:maximal_ideal_theorem]{maximal ideal theorem} Let \( I \) be a proper ideal in the semiring \( R \). Denote by \( \mscrH \) the set of all proper ideals in \( R \) that contain \( I \). The union of every chain in \( \mscrH \) is again an ideal, and by Zorn's lemma, \( \mscrH \) has a maximal element. That is, there exists a maximal ideal in \( \mscrH \) that contains \( I \).

  \ImplicationSubProof[thm:maximal_ideal_theorem]{maximal ideal theorem}[thm:zorns_lemma]{Zorn's lemma} In \cite{Hodges1979}, Hodges proves that the statement \enquote{every \hyperref[def:unique_factorization_domain]{unique factorization domain} has a maximal ideal} implies Zorn's lemma. We have an even stronger antecedent.
\end{proof}

\begin{definition}\label{def:radical_ideal}
  Let \( I \) be an \hyperref[def:semiring_ideal]{ideal} in the \hyperref[def:ring/commutative]{commutative ring} \( R \). The \term{radical} \( \sqrt I \) of \( I \) is a specific ideal containing \( I \) that we will define shortly. A \term{radical ideal} is an ideal that is equal to its radical. The \term{nilradical} of the ring \( R \) is \hyperref[def:radical_ideal]{radical} \( \sqrt {\braket{ 0_R }} \) of the zero ideal, whose elements we call \term{nilpotent elements}.

  The radical of the ideal \( I \) is the ideal defined equivalently through any of the following:
  \begin{thmenum}
    \thmitem{def:radical_ideal/direct}\mcite[15]{КоцевСидеров2016}
    \begin{equation}\label{eq:def:radical_ideal/direct}
      \sqrt I \coloneqq \set{ x \in R \given \qexists {n \in \BbbZ_{>0}} x^n \in I }.
    \end{equation}

    \thmitem{def:radical_ideal/intersection} \( \sqrt I \) is the intersection of all \hyperref[def:semiring_ideal/prime]{prime ideals} of \( R \) containing \( I \).
  \end{thmenum}
\end{definition}
\begin{proof}
  \SubProof{Proof of correctness of \eqref{eq:def:radical_ideal/direct}} We only need to prove that the radical \( \sqrt I \) of the ideal \( I \) is an ideal.

  Multiplicative closure is simpler. If \( x \) belongs to \( \sqrt I \), then there exists a power \( x^n \) that belongs to \( I \). Let \( r \) be any member of \( r \). Then \( rx = rx^n \in I \) since \( I \) is closed with respect to multiplication.

  Additive closure is a bit more involved. If \( x \) and \( y \) both belong to \( \sqrt I \), then there exist powers \( n \) and \( m \) such that \( x^n \in I \) and \( y^m \in I \). Let \( u \coloneqq n + m - 1 \). By \fullref{thm:binomial_theorem},
  \begin{equation*}
    (x + y)^u = \sum_{k=0}^u \binom u k x^k y^{u-k}.
  \end{equation*}

  \begin{itemize}
    \item If \( k < n \), then \( x^k y^{u-k} = (x^k y^{n - k - 1}) y^m \) and, since \( y^m \in I \), we have \( x^k y^{u-k} \in I \).
    \item If \( k \geq n \), then \( x^k y^{u-k} = x^n (x^{k-n} y^{u-k}) \) and, since \( x^n \in I \), we have \( x^k y^{u-k} \in I \).
  \end{itemize}

  Since \( I \) is closed under addition, \( (x + y)^u \in I \).

  \ImplicationSubProof{def:radical_ideal/direct}{def:radical_ideal/intersection} Let \( x \in \sqrt I \). That is, there exists a positive integer \( n \) such that \( x^n \in I \). Let \( P \) be a prime ideal containing \( I \). We will show that \( x \in P \).

  Since \( P \) is prime, by \fullref{thm:def:semiring_ideal/prime_pointwise}, \( x^{n-1} \in P \) or \( x \in P \). If \( x^{n-1} \in P \), then \( x^{n-2} \in P \) or \( x \in P \). Proceeding by induction on \( k \) in \( x^{n-k} \), we eventually obtain that \( x \in P \).

  \ImplicationSubProof{def:radical_ideal/intersection}{def:radical_ideal/direct} Conversely, let \( x \) be a member of every prime ideal containing \( P \). We will show that \( x \in \sqrt I \).

  Suppose that \( x^n \not\in I \) for every \( n \) and consider the following family of ideals:
  \begin{equation*}
    \mscrH \coloneqq \set{ J \T{is an ideal of} R \T{containing} I \given \qforall {n \in \BbbZ_{>0}} x^n \not\in J }.
  \end{equation*}

  It is nonempty because \( I \in \mscrH \).

  For every chain of ideals in \( \mscrH \), their union is also an ideal in \( \mscrH \). By \fullref{thm:zorns_lemma}, \( \mscrH \) has a maximal element \( H \). We will show that \( H \) is prime.

  From \( AB \subseteq H \) it follows that \( AB \in \mscrH \). If we suppose that neither \( A \) nor \( B \) belongs to \( \mscrH \), we obtain that there exist positive integers \( n \) and \( m \) such that \( x^n \in A \) and \( x^m \in B \). But \( x^{n + m} \) must then belong to \( AB \), which contradicts \( AB \in \mscrH \). The obtained contradiction demonstrates that \( H \) is a prime ideal. But this is impossible since, by assumption \( x \) is contained in every prime ideal, and \( x \not\in H \). So this must contradict our previous assumption that \( x^n \neq 0 \) for every \( n \).

  Therefore, \( x \) belongs to \( \sqrt I \).
\end{proof}

\begin{example}\label{ex:def:radical_ideal}
  We list examples of \hyperref[def:radical_ideal]{radical ideal}.

  \begin{thmenum}
    \thmitem{ex:def:radical_ideal/natural_numbers} Suppose that the natural number \( m \) has a prime decomposition
    \begin{equation*}
      m = p_1^{k_1} \cdots p_n^{k_n}.
    \end{equation*}

    Then the radical of its principal ideal is
    \begin{equation*}
      \sqrt{ \braket{ p_1^{k_1} \cdots p_n^{k_n} } } = \braket{ p_1 \cdots p_n }.
    \end{equation*}

    Indeed, for any \( a p_1 \cdots p_n \) from the radical, with \( k \coloneqq \max\set{ k_1, \ldots, k_n } \) we have
    \begin{equation*}
      (a p_1 \cdots p_n)^k = (a p_1^{k-k_1} \cdots p_n^{k-k_n}) p_1^{k_1} \cdots p_n^{k_n}.
    \end{equation*}

    To obtain \( m \), we can take \( k = 1 \) and \( a = p_1^{k_1-1} \cdots p_n^{k_1-1} \):
    \begin{equation*}
      a p_1 \cdots p_n = p_1^{k_1} \cdots p_n^{k_n} = m.
    \end{equation*}

    Particular examples of this are
    \begin{itemize}
      \item The ideal \( { \braket{ 6 } } \) is radical. It is not prime since \( \braket{ 2 } \braket{ 3 } = \braket{ 6 } \), but neither are subsets.

      \item For any prime \( p \), \( \braket{ p } \) is radical

      \item For any prime power \( p^n \), \( \sqrt{\braket{ p^n }} = \braket{ p } \). For example, \( \sqrt{\braket{ 4 }} = \braket{ 2 } \).
    \end{itemize}

    \thmitem{ex:def:radical_ideal/matrices} Consider the matrix ring \( \BbbN^{2 \times 2} \). The matrix
    \begin{equation*}
      A \coloneqq
      \begin{pmatrix}
        0 & 1 \\
        0 & 0 \\
      \end{pmatrix}
    \end{equation*}
    is a \hyperref[thm:def:semiring_ideal/nilradical]{nilpotent element} of \( \BbbN^{n \times n} \) because \( A^2 \) is the zero matrix. We refer to such matrices as \term{nilpotent matrices}.

    The transposed matrix \( A^T \) is also nilpotent. Hence, their linear combinations are also nilpotent.
  \end{thmenum}
\end{example}


\chapter{Numerical Time Integration of Partial/Ordinary Differential Equations}

\section{Introduction}

Many engineering problems are described as either a partial differential equation (PDE) or ordinary differential equation (ODE). A broad class of important problems is called the ``Initial Value Problem'' (IVP). Take for example the continuity equation in semiconductors, which is given as\begin{IEEEeqnarray}{rCl}
\frac{\partial n}{\partial t} & = & -\text{div}\left(\bm{J}\right) + G_\text{n} - R_\text{n} \label{eq:continuityEqn}
\end{IEEEeqnarray}where $n$ is the carrier concentration, $\bm{J}$ is the current density flowing through the semiconductor, $G_\text{n}$ and $R_\text{n}$ are the rates of carrier generation and recombination due to generation and recombination processes, respectively. The IVP is as follows. Assume that at time, $t=0$, the carrier concentration is $n(t=0)={0}$ and we want to find the time-dependent profile of the carrier concentration, $n(t)$.

In general, the PDE is more complicated than that in Equation~(\ref{eq:continuityEqn}), and the time derivative or slope function is denoted as $\dot{y}(t)=f(t, y)$, where $y(t)$ is the quantity we are trying to solve for, having the initial condition $y(t=0)=y_{0}$.

\section{The Forward (Explicit) Euler Method}\label{sec:forwardEuler}

One method to obtain $y(t)$ from $f(t,y)$ and $y_{0}$ is to use the forward Euler method. First, the time range over which to determine $y(t)$ is discretized into a uniform grid. The time difference between each grid point, $\Delta t=h$ is called the \emph{time step}. The IVP is solved by calculating $y(t_{n})$ iteratively starting at $t_{n}=0$ and $y(t=0)=y_0$ using the formula\begin{IEEEeqnarray}{rCl}
y(t_{n}+h) & = & y(t_{n}) + hf(t_{n},y(t_{n})) \label{eq:compExEuler}
\end{IEEEeqnarray}Mathematically, the Equation~(\ref{eq:compExEuler}) is unwieldy and we simplify the expression using $t_{n+1}=t_{n}+h$, $y_{n+1}=y(t_{n+1})$ and $y_{n}=y(t_{n})$, which gives\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + hf(t_{n},y_{n}) \label{eq:simpExEuler}
\end{IEEEeqnarray}

\subsection{Example 1: Solutions with Persistent Oscillations}

Consider the function $y(t) = \sin (t)$ and its time derivative, $\dot{y}(t) = \cos (t)$. The numerical solution for different time steps are plotted in Fig.~\ref{fig:IVP1}. As clearly seen, the error reduces with the size of the time step---a smaller time step returns a numerical result that is closer to the ideal solution.

\subsection{Example 2: Solutions with Damped Relaxations}

Another commonly observed function is $y(t) = \exp(-t)$ and its time derivative, $\dot{y}(t) = -\exp(-t)$. The numerical solution for different time steps are plotted in Fig.~\ref{fig:IVP2}. As clearly seen, the error also reduces with the size of the time step.\afterpage{
\begin{figure}[!t]
\centering
\includegraphics[scale=0.4]{ResearchNotes_TimePDE/figs/eulerTest.eps}
\caption{Numerical solution for the IVP with $\dot{y}(t)=\cos(t)$ and $y_{0}=0$ for different step sizes as compared to the ideal solution $y(t)=\sin(t)$.}
\label{fig:IVP1}
\end{figure}\begin{figure}[!t]
\centering
\includegraphics[scale=0.4]{ResearchNotes_TimePDE/figs/eulerTest2.eps}
\caption{Numerical solution for the IVP with $\dot{y}(t)=-\exp(-t)$ and $y_{0}=1.0$ for different step sizes as compared to the ideal solution $y(t)=\exp(-t)$.}
\label{fig:IVP2}
\end{figure}\clearpage
}

In Equation~(\ref{eq:simpExEuler}), $y_{n+1}$ appears only on the LHS, which means it is \emph{explicitly} defined. Thus, the forward Euler method is an explicit method. In contrast, the \emph{backward} (\emph{implicit}) Euler method is described by\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + hf(t_{n+1},y_{n+1}) \label{eq:compImEuler}
\end{IEEEeqnarray}Furthermore, the Euler methods calculate $y_{n+1}$ using only $y_{n}$ and not previous $y$ values (\emph{i.e.}, $y_{n-1}$, $y_{n-2}$, etc.). Hence, the Euler methods are examples of \emph{single step methods}. The Adams-Bashforth methods, Adams-Moulton methods and the backward differentiation formulas (BDF) are \emph{linear multistep methods}. For example, the two-step Adams-Bashforth method is described by the formula\begin{IEEEeqnarray}{rCl}
y_{n+2} & = & y_{n+1} + \frac{3}{2}hf(t_{n+1},y_{n+1}) - \frac{1}{2}hf(t_{n},y_{n}) \label{eq:AB12}
\end{IEEEeqnarray}where information at $y_{n}$ is used to calculate $y_{n+2}$ from $y_{n+1}$. Consequently, linear multistep methods need to be initialized in order to proceed.

\section{Higher Order Explicit Methods}

Equation~(\ref{eq:simpExEuler}) has a similar form as the power series expansion, which is written as\begin{IEEEeqnarray}{rCl}
y(t_{n+1}) & = & \sum^{+\infty}_{i=0}a_{i}y^{(i)}(t_{n})
\end{IEEEeqnarray}where the superscript in $y^{(i)}$ denotes the $i$-th derivative of $y$. Thus, the forward Euler method may be thought of as a first order estimation for $y(t_{n+1})$ and the full expression of Equation~(\ref{eq:simpExEuler}) is\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + hf(t_{n},y_{n}) + \mathcal{O}(h^{2})
\end{IEEEeqnarray}and the error in using Equation~(\ref{eq:simpExEuler}) to approximate $y(t)$ is $\mathcal{O}(h^{2})$. By expanding the power series further, we can derive higher order explicit methods for $y_{n+1}$ and reduce the error.

\subsection{The Heun Method}

The Heun method is a second order method to approximate $y_{n+1}$ and is given by the expression\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + h\frac{k_{1}+k_{2}}{2},~\text{where} \label{eq:heun} \\
k_{1} & = & f(t_{n},y_{n}) \nonumber \\
k_{2} & = & f(t_{n+1},y_{n}+hk_{1}) \nonumber
\end{IEEEeqnarray}It can be seen that $y_{n+1}$ is determined using two evaluations of $f()$ ($k_{1}$ and $k_{2}$). The evaluation of $k_{1}$ is akin to the explicit Euler method. The evaluation of $k_{2}$ occurs at the point predicted using the Euler method. The average of $k_{1}$ and $k_{2}$ is then used to estimate $y_{n+1}$.

\subsection{The 4-th Order Runge-Kutta (RK) Method}

German mathematicians Carl Runge and Wilhelm Kutta derived methods (also called Runge-Kutta methods or RK methods) to approximate $y_{n+1}$. The 4-th order RK method (RK4) is given by the expression\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + \frac{h}{6}\left(k_{1}+2k_{2}+2k_{3}+k_{4}\right),~\text{where} \\
k_{1} & = & f(t_{n},y_{n}) \nonumber \\
k_{2} & = & f\left(t_{n}+\frac{h}{2}, y_{n}+h\frac{k_{1}}{2}\right) \nonumber \\
k_{3} & = & f\left(t_{n}+\frac{h}{2}, y_{n}+h\frac{k_{2}}{2}\right) \nonumber \\
k_{4} & = & f(t_{n+1},y_{n}+hk_{3}) \nonumber
\end{IEEEeqnarray}In this method $f()$ are evaluated at points within the time step to obtain a better approximation of $y_{n+1}$.

\subsection{The Butcher Tableau}

From the methods discussed so far, it can be seen that the general equation for determining $y_{n+1}$ in single step methods is \begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + h\sum^{s}_{i=1}b_{i}k_{i},~\text{where} \\
k_{1} & = & f(t_{n},y_{n}) \nonumber \\
k_{2} & = & f(t_{n}+c_{2}h,y_{n}+ha_{21}k_{1}) \nonumber \\
k_{3} & = & f(t_{n}+c_{3}h,y_{n}+ha_{31}k_{1}+ha_{32}k_{2}) \nonumber \\
& \vdots & \\
k_{s} & = & f(t_{n}+c_{s}h, y_{n}+h\sum^{s-1}_{j=1}a_{s,j}k_{j}) \nonumber
\end{IEEEeqnarray}where $k_{i}$ is calculated in the $i$-th stage of the method. The mathematician John C. Butcher proposed a concise way of describing the numerical methods using a tableau called the \emph{Butcher tableau} (see Fig.~\ref{fig:butcherTab}).

From the earlier examples, we can see that the Butcher tableaus for the Euler method and RK4 method are given by Figs.~\ref{fig:eulerButcher} and Fig.~\ref{fig:rk4Butcher}. The Heun method is a special case of a more general method called the \emph{midpoint method}, which has the Butcher tableau given in Fig.~\ref{fig:midpointButcher}.

\begin{figure}[!b]
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/ButcherTableau.eps}
\caption{The general form of a Butcher Tableau}
\label{fig:butcherTab}
\end{figure}
\begin{figure}[!b]
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/eulerButcher.eps}
\caption{Butcher Tableau for the Euler method.}
\label{fig:eulerButcher}
\end{figure}
\begin{figure}[!b]
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/rk4Butcher.eps}
\caption{Butcher Tableau for the RK4 method.}
\label{fig:rk4Butcher}are
\end{figure}
\begin{figure}[!b]
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/midpointTableau.eps}
\caption{Butcher Tableau for the midpoint method. The Heun method is the case where $\alpha=1$.}
\label{fig:midpointButcher}
\end{figure}

The algorithm for executing the explicit methods is shown in Algorithm~\ref{algo:algo1}.
\begin{algorithm}[H]
\caption{Numerical method to solve IVP without adaptive time stepping}
\label{algo:algo1}
\hspace*{\algorithmicindent} $\textbf{Inputs:}~a_{i,j},~b_{i}~\text{and}~c_{i}~\text{from Butcher tableau,}~f(t,y),~t_\text{end},~h_\text{init},~\text{and}~y_{0}$ \\
\hspace*{\algorithmicindent} $\textbf{Outputs:}~y~\text{for all discrete time points,~}t\in{}[0,~t_\text{end}] $
\begin{algorithmic}[1]
\State $n \gets 0$
\State $t_{n} \gets 0$
\State $y_{n} \gets y_{0}$
\State $h_{n} \gets h_\text{init}$
\While{$t_{n} < t_\text{end}$}
  \If{$t_\text{end} < t_{n} + h_{n}$}
    \State $h_{n} = t_\text{end} - t_{n}$
  \EndIf
  \State $k_{1} \gets f(t_{n}, y_{n})$
  \State $\delta{}y \gets b_{1}k_{1}$
  \For{$i0 \gets 2$, number of stages defined in the Butcher tableau}
    \State $t_\text{int} \gets t_{n} + c_{i0}h_{n}$
    \State $j \gets$ number of substeps in stage~$i0$
    \State $y_\text{int} \gets y_{n} + h_{n}\sum^{j}_{i1=1}a_{i0,i1}k_{i1}$
    \State $k_{i0} \gets f(t_\text{int},y_\text{int})$
    \State $\delta{}y \gets \delta{}y + b_{i0}k_{i0}$
  \EndFor
  \State $h_{n+1} \gets h_{n}$
  \State $y_{n+1} \gets y_{n} + h_{n}\cdot\delta{}y$
  \State $t_{n+1} \gets t_{n} + h_{n}$
  \State $n \gets n+1$
\EndWhile
\end{algorithmic}
\end{algorithm}

\afterpage{
\begin{figure}[H]
\centering
\begin{minipage}{0.45\linewidth}
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/EmbeddedButcherTableau.eps}
\caption{The general Butcher tableau for embedded methods.}
\label{fig:embeddedButcher}
\end{minipage}
\quad\quad
\begin{minipage}{0.45\linewidth}
\centering
\includegraphics[scale=1.0]{ResearchNotes_TimePDE/figs/rk23.eps}
\caption{The Butcher tableau for the Bogacki-Shampine method. The method has a second order predictor and third order corrector and the LTE is $\bm{O}(h^{3})$.}
\label{fig:rk23}
\end{minipage}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ResearchNotes_TimePDE/figs/fehlberg.eps}
\caption{The Butcher tableau for the Fehlberg method. The method has a fourth order predictor and fifth order corrector and the LTE is $\bm{O}(h^{5})$.}
\label{fig:fehlberg}
\end{figure}\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ResearchNotes_TimePDE/figs/rk45ck.eps}
\caption{The Butcher tableau for the Cash-Karp method. The method has a fourth order predictor and fifth order corrector and the LTE is $\bm{O}(h^{5})$.}
\label{fig:rk45ck}
\end{figure}\begin{figure}[H]
\centering
\includegraphics[scale=0.8]{ResearchNotes_TimePDE/figs/rk45dp.eps}
\caption{The Butcher tableau for the Dormand-Prince method. The method has a fourth order predictor and fifth order corrector and the LTE is $\bm{O}(h^{5})$.}
\label{fig:rk45dp}
\end{figure}\clearpage
}

\section{Adaptive time stepping}

As can be seen in Figs.~\ref{fig:IVP1} and \ref{fig:IVP2}, there is a dependence of the accuracy of the solution returned by the numerical method and the time step used. In engineering simulations, the simulation runtime may be reduced if the time steps can be optimized. Adaptive time stepping algorithms attempt to optimize the time step used by ensuring that the estimated local truncation error (\emph{LTE}) is bounded to some tolerance value. The LTE is calculated by subtracting two approximations of $y_{n+1}$---one with order $p$ and another with order $p-1$, which leads to the LTE having order $p$. These methods are sometimes called \emph{predictor-corrector} methods, since one expression gives a low order predicted value of $y_{n+1}$ and the LTE gives a high order corrector to the predictor.

A family of embedded RK methods exist that is computationally efficient in calculating $y_{n+1}$ and the LTE at the $n$-th step, $\bm{\widehat{r}}_{n}$. These methods are computationally efficient because they reduce the number of calls to evaluate $f()$, which is the most computationally intensive portion of the numerical method. Fig.~\ref{fig:embeddedButcher} shows the general form of the Butcher tableau for an embedded method.

Some commonly used embedded explicit methods are those by Bogacki-Shampine (RK23) \cite{Bogacki1989} (Fig.~\ref{fig:rk23}), Fehlberg (RK45F) \cite{Fehlberg1970} (Fig.~\ref{fig:fehlberg}), Cash-Karp (RF45CK) \cite{Cash1990} (Fig.~\ref{fig:rk45ck}), and Dormand-Prince (RK45DP) \cite{Dormand1980} (Fig.~\ref{fig:rk45dp}). A key difference between the RK45 methods is the number of evaluations of $f()$ needed to estimate $y_{n+1}$. Some methods (such as RK23 and RK45DP) have a ``first same as last'' (FSAL) property, which means the predictor is calculated at the last stage.

Adaptive time stepping is performed by calculating the ratio of an error tolerance to the LTE. The next time step in the numerical method is then scaled according to the equation\begin{IEEEeqnarray}{rCl}
h_{n+1} & = & 0.9 \times h_{n} \times \min\left(\max \left(\left(\frac{\text{tol}}{|\bm{\widehat{r}}_{n}|}\right)^{1/(p+1)},0.3\right), 2\right)\label{eq:adapH0}
\end{IEEEeqnarray}where $p$ is the order of the corrector. The factor of 0.9 is a safety factor to limit the change in $h$ and reduce the risk that $h_{n+1}$ is rejected when calculating $y_{n+2}$. The $\min$ and $\max$ further limit $h_{n+1}$ to the range $[0.27h_{n}, 1.8h_{n}]$.

An alternative scheme was proposed by K. Gustafsson in \cite{Gustafsson1988}. The work is motivated by control theory and seeks to handle issues that arise when solving stiff problems (see Section~\ref{subsec:stabilityExp}) and remain useful for both explicit and the implicit methods (to be discussed after Section~\ref{subsec:stabilityExp}). First, Equation~(\ref{eq:adapH0}) is rewritten as\begin{IEEEeqnarray}{rCl}
h_{n+1} & = & \theta{}h_{n} \label{eq:adapH1} \\
\theta & = & \gamma\left(\frac{\text{tol}}{|\bm{\widehat{r}}_{n}|/h_{n}}\right)^{1/p} = \left(\frac{\gamma^{p}\cdot\text{tol}}{|\bm{\widehat{r}}_{n}|/h_{n}}\right)^{1/p}
\end{IEEEeqnarray}where $\gamma$ is the safety factor ($\gamma=0.9$ in Equation~(\ref{eq:adapH0})), and $|\bm{\widehat{r}}_{n}|\propto{}h_{n}$. Taking the $\log()$ on both sides gives\begin{IEEEeqnarray}{rCl}
\log(h_{n+1}) & = & \log(h_{n}) + \frac{1}{p}\left(\log(\gamma^{p}\cdot\text{tol}) - \log\left(\frac{|\bm{\widehat{r}}_{n}|}{h_{n}}\right)\right)
\end{IEEEeqnarray}In \cite{Gustafsson1988}, $\gamma=1.0$ and,\begin{IEEEeqnarray}{rCl}
e_{n} & = &  \log(\gamma^{p}\cdot\text{tol}) - \log\left(\frac{|\bm{\widehat{r}}_{n}|}{h_{n}}\right) \\
P_{n} & = & K_\text{P}\cdot e_{n} \\
I_\text{temp} & = & I_{n-1} + K_\text{I}\cdot{}e_{n} \\
h_\text{temp} & = & \exp\left(P_{n}+I_\text{temp}\right) \\
h_{n+1} & = & \min\left(h_\text{temp},\theta_\text{max}h_{n}\right) \\
I_{n} & = & I_\text{temp} + \log(h_{n+1}) - \log(h_{n})
\end{IEEEeqnarray}where $\theta_\text{max}$ limits the growth of $h$. Then, the time stepping algorithm calculates the following\begin{IEEEeqnarray}{rCl}
h_\text{temp} & = & \left(\frac{\text{tol}}{|\bm{\widehat{r}}_{n}|/h_{n}}\right)^{K_\text{I}}\left(\frac{|\bm{\widehat{r}}_{n-1}|/h_{n-1}}{|\bm{\widehat{r}}_{n}|/h_{n}}\right)^{K_\text{P}}h_{n} \label{eq:gustafTrack} \\
h_{n+1} & = & \min\left(h_\text{temp},\theta_\text{max}h_{n}\right)
\end{IEEEeqnarray}where it is recommended that $\theta_\text{max}=2.0$. Equation~(\ref{eq:gustafTrack}) states that during the execution of the algorithm, $|\bm{\widehat{r}}_{n}|/h_{n}$ needs to be saved for the next time step for adaptive time stepping. Furthermore, two sets of parameters are recommended depending on whether the time step is rejected (\emph{e.g.}, $|\bm{\widehat{r}}_{n}|/h_{n}>1.2\cdot{}\text{tol}$) or accepted. If the time step is accepted, use $K_\text{P}=0.13$ and $K_\text{I}=1/15$. Otherwise, use $K_\text{P}=0$ and $K_\text{I}=1/5$.

The algorithm for executing the explicit methods with adaptive time stepping is shown in Algorithm~\ref{algo:algo2}, where the function, $adaptH()$, can implement either the conventional method, or the Gustafsson method. \begin{algorithm}[H]
\caption{Numerical method to solve IVP with adaptive time stepping}
\label{algo:algo2}
\hspace*{\algorithmicindent} $\textbf{Inputs:}~p\text{~(order of embedded corrector)},~a_{i,j},~b_{i},~b^{*}_{i}~\text{and}~c_{i}~\text{from Butcher tableau,}~f(t,y),~errTol,$ \\
\hspace*{\algorithmicindent}\hspace*{\algorithmicindent}$adaptH(),~t_\text{end},~h_\text{init},~\text{and}~y_{0}$ \\
\hspace*{\algorithmicindent} $\textbf{Outputs:}~y~\text{for all discrete time points,~}t\in{}[0,~t_\text{end}] $
\begin{algorithmic}[1]
\State $n \gets 0$
\State $t_{n} \gets 0$
\State $y_{n} \gets y_{0}$
\State $h_{n} \gets h_\text{init}$
\While{$t_{n} < t_\text{end}$}
  \If{$t_\text{end} < t_{n} + h_{n}$}
    \State $h_{n} = t_\text{end} - t_{n}$
  \EndIf
  \State $rejectH \gets true$
  \While{$rejectH$~is~$true$}
    \State $k_{1} \gets f(t_{n}, y_{n})$
    \State $\delta{}y \gets b_{1}k_{1}$
    \State $LTE_{n} \gets (b_{1} - b^{*}_{1})k_{1}$
    \For{$i0 \gets 2$, number of stages defined in the Butcher tableau}
      \State $t_\text{int} \gets t_{n} + c_{i0}h_{n}$
      \State $j \gets$ number of substeps in stage~$i0$
      \State $y_\text{int} \gets y_{n} + h_{n}\sum^{j}_{i1=1}a_{i0,i1}k_{i1}$
      \State $k_{i0} \gets f(t_\text{int},y_\text{int})$
      \State $\delta{}y \gets \delta{}y + b_{i0}k_{i0}$
      \State $LTE_{n} \gets LTE_{n} + (b_{i0} - b^{*}_{i0})k_{i0}$
    \EndFor
    \State $rejectH, h_\text{recommend} \gets adaptH(p, h_{n}, errTol, LTE_{n}, \delta{}y)$
    \If{$rejectH$ is $true$}
      \State $h_{n} \gets h_\text{recommend}$
    \Else
      \State $h_{n+1} \gets h_\text{recommend}$
    \EndIf
  \EndWhile
  \State $y_{n+1} \gets y_{n} + h_{n}\cdot\delta{}y$
  \State $t_{n+1} \gets t_{n} + h_{n}$
  \State $n \gets n+1$
\EndWhile
\end{algorithmic}
\end{algorithm}

\subsection{Stability of Explicit Methods and Stiffness of IVP}\label{subsec:stabilityExp}

A major disadvantage of explicit methods is that the errors introduced when used to solve stiff IVPs can be extremely large. Examples of stiff problems are encountered when critically damped and overdamped systems are studied. Consider the example of a non-ideal voltage source charging a fixed capacitor, $C_\text{L}$, through an output resistance, $R_\text{out}$ to a voltage given by $V_\text{DD}$. The circuit equation to solve is the ODE expressed as\begin{IEEEeqnarray}{rCl}
\frac{d V_\text{C}}{dt} & = & \frac{V_\text{DD}-V_\text{C}}{R_\text{out}C_\text{L}}
\end{IEEEeqnarray}The ideal solution for the voltage across the capacitor, $V_\text{C}(t)$, when the initial condition is 0~V is\begin{IEEEeqnarray}{rCl}
V_\text{C}(t) & = & V_\text{DD}\left(1-\exp\left(\frac{-t}{R_\text{out}C_\text{L}}\right)\right)
\end{IEEEeqnarray}Fig.~\ref{fig:falseOscillation} shows a case where a poorly chosen time step leads to a numerical solution that continually oscillates without converging to the steady-state solution.

\begin{figure}[H]
\centering
\includegraphics[scale=0.4]{ResearchNotes_TimePDE/figs/forwardOscillations.eps}
\caption{Poor numerical solution with oscillations was obtained as the problem is too stiff for the explicit numerical method.}
\label{fig:falseOscillation}
\end{figure}

Another problem arises when adaptive time stepping is used. As the solution approaches the final steady-state value, the time step will reduce significantly to manage the error within the error tolerance, which leads to long simulation runtime even though the results are converging to the correct solution. These problems can be overcome by implicit methods.

\section{The Backward (Implicit) Euler Method}

As presented in Equation~(\ref{eq:compImEuler}), the implicit Euler method is described by\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} + hf(t_{n+1},y_{n+1}) \nonumber
\end{IEEEeqnarray}The solution we are trying to solve for, $y_{n+1}$, appears on both sides of the equation, which makes it cumbersome to solve. The general approach to solve the equation utilizes an educated guess for the solution, $y^{*}_{n+1}$, and a residual function, $R(y^{*}_{n+1})$. If we substitute $y^{*}_{n+1}$ into Equation~(\ref{eq:compImEuler}), we obtain\begin{IEEEeqnarray}{rCl}
y^{*}_{n+1} & = & y_{n} + hf(t_{n+1},y^{*}_{n+1}) + R(y^{*}_{n+1}) \\
R(y^{*}_{n+1}) & = & y^{*}_{n+1} - y_{n} - hf(t_{n+1},y^{*}_{n+1}) \label{eq:imEulerRes}
\end{IEEEeqnarray}We clearly see in Equation~(\ref{eq:imEulerRes}) that $R(y^{*}_{n+1})$ is the error in Equation~(\ref{eq:compImEuler}) if the educated guess was used as the solution. The objective now is to search for $y^{*}_{n+1}$ so that $R(y^{*}_{n+1})=0$. This can be achieved using the Newton-Raphson method. We start by assuming $R(y^{*}_{n+1})$ is sufficiently small that\begin{IEEEeqnarray}{rCl}
R'(y^{*}_{n+1}) & \approx & \frac{R(y^{*}_{n+1})}{y^{*}_{n+1} - y^{**}_{n+1}}
\end{IEEEeqnarray}where $R'(y^{*}_{n+1})$ is the derivative of the residual function with respect to $y^{*}_{n+1}$, and $y^{**}_{n+1}$ is our updated guess of $y_{n+1}$. Rearranging the terms, we obtain the equation\begin{IEEEeqnarray}{rCl}
y^{**}_{n+1} & = & y^{*}_{n+1} - \frac{R(y^{*}_{n+1})}{R'(y^{*}_{n+1})} \label{eq:newtonUpdate0}
\end{IEEEeqnarray}We may then check $(y^{**}_{n+1} - y^{*}_{n+1})$ (and possibly $R(y^{**}_{n+1})$) against some convergence criteria. If the criteria is met, $y^{**}_{n+1}$ is accepted as $y_{n+1}$ and we move to calculate the next time step in the implicit Euler method. If the criteria is not met, we substitute $y^{**}_{n+1}$ as $y^{*}_{n+1}$ in Equation~(\ref{eq:newtonUpdate0}) and update our guess for $y_{n+1}$ to check against the convergence criteria, repeating until the convergence criteria is met.

In many engineering problems, Equation~(\ref{eq:compImEuler}) describes a system of equations where $y_{n+1}$ and $y_{n}$ describes values over a spatial grid at a particular time. In many cases, $f()$ is a matrix describing an operator. Consider the 1-D diffusion equation over a uniform spatial grid on the Cartesian axes described by\begin{IEEEeqnarray}{rCl}
\frac{\partial y}{\partial t} & = & -D\overrightarrow{\nabla}^{2}y \\
& = & -\frac{D}{dx^{2}}Ay
\end{IEEEeqnarray}where $D$is the diffusion coefficient, $dx$ is the spacing between grid points, and $A$ is the tridiagonal matrix representing the Laplacian operator in Cartesian coordinates. Substituting into Equation~(\ref{eq:compImEuler}), the discretized diffusion IVP is solved by the implicit Euler method by\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} - h\frac{D}{dx^{2}}Ay_{n+1} \\
\left(I + h\frac{D}{dx^{2}}A\right)y_{n+1} & = & y_{n} \\
y_{n+1} & = & \left(I + h\frac{D}{dx^{2}}A\right)^{-1}y_{n} \label{eq:imEulerDiff}
\end{IEEEeqnarray}where $I$ is the identity matrix. Equation~(\ref{eq:imEulerDiff}) shows that $y_{n+1}$ is found by inverting the matrix $\left(I + h\frac{D}{dx^{2}}A\right)$ and pre-multiplying with $y_{n}$.

\section{Higher Order Implicit Methods}

Just as for explicit methods, a family of higher order implicit methods also exist.

\subsection{The Theta Method}

The Theta method combines the explicit Euler step with an implicit Euler step:\begin{IEEEeqnarray}{rCl}
y_{n+1} & =  y_{n} + h\theta{}f(t_{n},y_{n}) + h(1-\theta)f(t_{n}, y_{n+1})
\end{IEEEeqnarray}The Crank-Nicolson or trapezoidal method is obtained if $\theta=0.5$, and hence\begin{IEEEeqnarray}{rCl}
y_{n+1} & =  y_{n} + h\frac{f(t_{n},y_{n}) + f(t_{n}, y_{n+1})}{2}
\end{IEEEeqnarray}Note that due to the implicit nature of the backward Euler step, the Crank-Nicolson method is not an average between the forward and backward Euler steps. Applying the Crank-Nicolson method to the diffusion IVP as before, we obtain\begin{IEEEeqnarray}{rCl}
y_{n+1} & = & y_{n} - \frac{h}{2}\frac{D}{dx^{2}}Ay_{n} - \frac{h}{2}\frac{D}{dx^{2}}Ay_{n+1} \\
\left(I + \frac{h}{2}\frac{D}{dx^{2}}A\right)y_{n+1} & = & \left(I - \frac{h}{2}\frac{D}{dx^{2}}A\right)y_{n} \\
y_{n+1} & = & \left(I + \frac{h}{2}\frac{D}{dx^{2}}A\right)^{-1}\left(I - \frac{h}{2}\frac{D}{dx^{2}}A\right)y_{n}
\end{IEEEeqnarray}