\lab{Job Search Model}{Job Search Model}

\section*{Discrete Choice (Threshold) Problems}\label{SecDiscrChoice}

One powerful application of dynamic programming is
to models that have both continuous and discrete state variables. These models are sometimes referred to as
discrete choice problems or optimal stopping problems.  Examples include models of employment that involve both
the choice of whether to work and how much to work, models of firm entry and exit that involve the choice of both
whether to produce and how much to produce, and models of marriage that involve the choice of whether to date
(get married or keep dating) and how much to date.
This application illustrates the versatility of dynamic programming as a dynamic solution method

In this lab, we follow a simple version of a standard job search model.
Assume that workers live infinitely long.
We will split a worker's life into discrete time periods, and in each period the worker is either
employed or unemployed, and receives a job offer. The worker must make a choice between discrete actions
(such as accepting or rejecting a job offer), with the goal of maximizing some utility function (hence,
this is a \emph{discrete choice} problem).

We can state this problem in terms of dynamic programming by defining an appropriate value function.
Let the value of entering a period with most recent wage $w$,
current job offer wage $w'$, and employment status $s$ be given by the following value function,
\begin{equation}\label{EqV}
   V(w,w',s) = \begin{cases}
                  V^E(w)    \quad&\text{if}\quad s = E \\
                  V^U(w,w') \quad&\text{if}\quad s = U \\
               \end{cases}
\end{equation}
where employment status is a binary variable $s\in\{E,U\}$ ($E$ indicates ``employed" and $U$ indicates ``unemployed");
a person can be either employed or unemployed.

As in the cake eating problem, the value function is calculated as the sum of some reward (based on the
current state) and the discounted value of entering the next period in some particular state.
The reward function, denoted (as usual) by $u$, gives the utility of spending available funds.
Assuming that a worker receives some wage $x$ in a given period and spends all available money in the period,
the utility of consumption is given by
\[
u(x).
\]
Calculating the value for the next period depends on the employment status in the current period, so we address
this separately for each case.

Let us first consider the case where the individual is unemployed ($s = U$). As is customary, let $s'$ denote the
employment status of the worker in the next period.
In this unemployed state, the worker receives unemployment benefits equal to a fraction of her most recent wage,
i.e. $\alpha w$, where $\alpha \in (0, 1)$.
Hence, the utility of consumption in the current unemployed state is given by
\[
u(\alpha w).
\]

The worker also receives one wage offer ($w'$) per period, and will obtain
this wage in the next period provided that he chooses to accept employment, i.e. provided $s' = E$.
The worker must decide whether to accept the current wage offer $w'$ or to remain unemployed in the next period,
i.e. he must decide on the value of $s'$. How
does he make this choice? He must weigh the value of entering the next period as an employed worker with wage
$w'$ (given by $V^E(w')$) versus the value of entering the next period as an unemployed worker with
previous wage $w$ and unknown wage offer $w''$ (given by $V^U(w,w'')$). Because the worker cannot know
what the future wage offer $w''$ will be, it is treated as a random variable with a particular probability
distribution. Hence, the worker must actually compute the \emph{expected} value of entering the next
period unemployed. This term is simply
\[
\mathbb{E}_{w''}V^U(w,w''),
\]
where $\mathbb{E}_{w''}$ denotes the expectation operator with respect to the probability distribution of future
wage offers $w''$.
To sum up, the worker chooses to accept the wage offer $w'$ or remain unemployed in the next period based on
which option gives the greater expected value, and the value of this decision is given by
\[
\max\Bigl\{V^E(w'), \,\, \mathbb{E}_{w''}V^U(w,w'')\Bigr\}.
\]

The overall value of the current unemployed state with previous wage $w$ and current wage offer $w'$ is
just the utility of consumption plus the discounted value of the next period, i.e.
\begin{equation}\label{EqVu}
V^U(w,w') = u(\alpha w) + \beta \max\Bigl\{V^E(w'), \,\, \mathbb{E}_{w''}V^U(w,w'')\Bigr\},
\end{equation}
where $\beta$ is the discount factor.

Now we turn to the case where the job status is employed ($s = E$).
In this case, the worker receives a wage $w$ in the current period, and so the utility of consumption is
just
\[
u(w).
\]
In the next period, the worker will have most recent wage $w$, she will receive wage offer $w''$, and will
have employment status $s'$. As in the unemployed case, $w''$ is unknown and treated as a random variable.
Unlike the unemployed case, however, the worker's future employment status $s'$ is not under her control,
but rather is also a random variable. The reason for this is that the worker will remain employed
until she loses the job, a random event that occurs with some fixed probability in each time period.
Hence, we must calculate the expected value of the next period with respect to both $w''$ and $s'$.
We may write the entire value function for the employed case as
\begin{equation}\label{EqVe1}
   V^E(w) = u(w) + \beta \mathbb{E}_{w'',s'}V(w,w'',s').
\end{equation}

To calculate the expectation term, we need to know the joint probability distribution over $w''$ and $s'$.
This can be characterized in the following way.
We assume that $s'$ and $w''$ are independent. Hence, we can split the joint expectation
operator into the composition of the two individual expectation operators:
\[
\mathbb{E}_{w'',s'} = \mathbb{E}_{w''}\mathbb{E}_{s'}.
\]
Let $\gamma$ represent the probability that an employed worker becomes unemployed in the next period,
so that $1-\gamma$ is the probability of remaining employed in the next period.
If the worker stays employed in the next period ($s' = E$), then next period's wage equals the current
period's wage, and the term inside the expectation is
\[
V(w,w'',E) = V^E(w).
\]
We then have
\begin{align*}
\mathbb{E}_{s'}V(w,w'',s') &= (1-\gamma)V(w,w'',E) + \gamma V(w,w'',U)\\
&= (1-\gamma)V^E(w) + \gamma V^U(w,w'').
\end{align*}
Notice that the term $(1-\gamma)V^E(w)$ is constant with respect to $w''$. Then
\begin{align*}
\mathbb{E}_{w''}\mathbb{E}_{s'}V(w,w'',s') &= \mathbb{E}_{w''}\left[(1-\gamma)V^E(w) + \gamma V^U(w,w'')\right]\\
&= \mathbb{E}_{w''}(1-\gamma)V^E(w) + \mathbb{E}_{w''}\gamma V^U(w,w'')\\
&= (1-\gamma)V^E(w) + \gamma \mathbb{E}_{w''}V^U(w,w'').
\end{align*}
Hence, we can rewrite \eqref{EqVe1} as follows:
\begin{equation}\label{EqVe2}
   V^E(w) = u(w) + \beta \Bigl[(1-\gamma)V^E(w) + \gamma \mathbb{E}_{w''}V^U(w,w'')\Bigr].
\end{equation}

We have now completely described the value function. What about the policy function?
The policy function for the unemployed worker gives his decision on whether to accept the job $s'=E$
or to reject the job $s'= U$.
This will be a function of both the most recent wage $w$ and the current wage offer $w'$.
The employment status $s'$ in the next period is determined by the policy function $\psi$:
\[
s' = \psi(w,w').
\]

These discrete choice problems are often called threshold
problems because the policy choice depends on whether the state variable is greater than or less than
some threshold level. That is, an unemployed worker will accept a job if and only if the offer wage is
above some set amount that depends on the most recent wage $w$. In the labor search model,
the threshold level is called the ``reservation wage'' $w_R'$. The reservation wage $w_R'$ is defined as
the wage offer such that the worker is indifferent between accepting the job $s' = E$ and
staying unemployed $s' = U$. Hence, this reservation wage satisfies the equation
\begin{equation}\label{EqWR}
   V^E(w_R') = E_{w''}\left[V^U(w,w'')\right].
\end{equation}
The policy function will then take the form of accepting the job if $w' \geq w_R'$ or
rejecting the job offer and remaining unemployed if $w' < w_R'$:
\begin{equation}\label{EqSprime}
   s' = \psi(w,w') = \begin{cases}
                      E \quad\text{if}\quad w' \geq w_R' \\
                      U \quad\text{if}\quad w' < w_R'.
                   \end{cases}
\end{equation}
Figure \ref{fig:disc_policy} shows an example of the discrete policy function.

\begin{figure}
\includegraphics[width=\textwidth]{disc_policy.pdf}
\caption{Here is the policy function for fixed $w = 50$.  Numerically we let 0 represent unemployment, $U$,
and 1 represent employment, $E$.  Thus we see that an individual will choose to take a new job, given
their old wage was 50, at a wage of roughly 35.  Thus for a previous wage of 50, we say the reservation wage is 35.}
\label{fig:disc_policy}
\end{figure}

In summary, the labor search discrete choice problem is characterized by the value functions \eqref{EqV}, \eqref{EqVu},
and \eqref{EqVe2}, the reservation wage \eqref{EqWR}, and the policy function \eqref{EqSprime}. Because wage offers
are distributed according to some given probability distribution (denote the cdf by $F(w')$),
and because the policy function takes the form of \eqref{EqSprime},
the probability that the unemployed worker receives a wage offer that he will reject is $F(w_R')$ and the probability
that he receives a wage offer that he will accept is $1 - F(w_R')$. Just like the continuous choice cake eating
problems, this problem can be solved by value function, policy function, or modified policy function iteration.

The value function iteration solution method for the equilibrium in the labor search problem is analogous to the
value function iteration from the previous labs. The only difference is that two value functions ($V^E$ and $V^U$)
must converge to a fixed point in this problem instead of just one value function converging in the previous problems.
Although there are two value functions to consider, there is only one policy function, since decisions are only made
in the unemployed state.  Thus, there is only one policy function on which to iterate in the case of policy or modified
policy iteration.

In the following problems, you will solve the job search problem using value function iteration and modified policy
function iteration. Assume that the consumption utility function $u$ is given by
\[
u(w) = \sqrt{w}.
\]
Assume that the probability of becoming unemployed in a given period is $\gamma = 0.10$, the fraction of wages paid
in unemployment benefits is $\alpha = 0.5$, and the discount factor is $\beta = 0.9$.

Assume that the log of wage offers are distributed normally.  We then say that offers are distributed
lognormally and write
\[
w'\sim \text{LogN}(\mu,\sigma).
\]
This is a convenient choice for the distribution of
wage offers.  Among other things, it guarantees that wage offers will be positive.
A mean of $20$ and variance of $200$ are typical parameters of such a wage distribution,
and we will use these parameters in the following problems.

As usual when dealing with continuous variables, we form a discrete approximation of
the possible wage values. In particular, approximate the wage values by a vector of
length $N = 500$ of equally-spaced values from $w_{min} = 0$ to $w_{max} = 100$, inclusive.
We then form a corresponding discrete approximation of the probability density function
$f(w')$ for the lognormal wage offers using code provided in the file \li{discretelognorm.py},
as follows, where \li{w} is the length-$N$ vector of wage values, $m$ is the mean, and $v$ is
the variance as specified above:
\begin{lstlisting}
>>> from discretelognorm import discretelognorm
>>> f = discretelognorm(w, m, v)
\end{lstlisting}
The function \li{discretelognorm} computes the discrete pdf of the specified lognormal distribution in much
the same way that you calculate the discrete normal pdf when solving the stochastic cake eating problem.

\begin{problem}
Solve the job search problem using value function iteration. Return the converged value functions
$V^E$ and $V^U$, as well as the converged policy function $\psi$. The following steps provide detailed
instructions. Note that there are multiple ways to proceed, and the following is simply one (fairly good)
possibility.
\begin{enumerate}

   \item As described above, represent the possible wage values by an array \li{w} of length
   $N$. Denote this array entrywise by
   \[
   w = (w_1,w_2,\ldots,w_N).
   \]
   Calculate the corresponding discrete lognormal pdf \li{f}, exactly as shown above.

   \item Note that $u(w)$ and $u(\alpha w)$ are needed when computing the value functions.
   Since these quantities do not change from one iteration to another, it is smart to
   compute them once at the outset. Denote $u(w)$ by \li{uw} and $u(\alpha w)$ by \li{uaw}.
   These are easily calculated as follows:
\begin{lstlisting}
>>> uw = u(w)
>>> uaw = u(alpha*w).reshape((N,1))
\end{lstlisting}
    where \li{u} is the square root function. We must reshape \li{uaw} because of array broadcasting
    issues that arise in the code snippets below.

   \item Since $V^E$ is a function of only $w$, it will be represented by a vector of length $N$, where
   the $i$-th entry gives $V^E(w_i)$. The unemployed value function $V^U$, however,
   is a function of both $w$ and $w'$, so it will be represented by an $N \times N$ array,
   where the $(i,j)$-th entry gives $V^U(w_i,w_j)$. Initialize the entries of these arrays to 0:
\begin{lstlisting}
>>> VE = np.zeros(N)        #employed value function
>>> VU = np.zeros((N,N))    #unemployed value function
\end{lstlisting}

   \item Note that $\mathbb{E}_{w''}V^U(w,w'')$  is needed to calculate both $V^E$ and $V^U$.
   This expectation depends on $w$, and so can be represented by a length $N$ array, where the
   $i$-th entry is $\mathbb{E}_{w''}V^U(w_i,w'')$.
   It is convenient to assign a variable to this array to keep track of it throughout the iterations.
   We denote the expectation by \li{EVU}, and initialize it to zeros:
\begin{lstlisting}
>>> EVU = np.zeros(N)
\end{lstlisting}

   \item For reasons that will soon become apparent, we will need to create a $N\times N$ helper array
   whose rows are equal to \li{VE} (call this array \li{MVE}), and a $N \times N$ helper array whose columns
   are equal to \li{EVU} (call this array \li{MEVU}).
   At the outset, simply initialize these arrays to zeros.

   \item Because job status is a binary variable, the policy function returns one of two possible values. It is
   convenient to represent ``employed" by $1$ and ``unemployed" by $0$. Now the policy function depends
   on $w$ and $w'$, so it will also be represented by an $N\times N$ array \li{PSI} of zeros and ones,
   where the $(i,j)$-th entry gives $\psi(w_i, w_j)$.

   \item Now we are ready to begin the iteration.
   A single iteration involves computing the updated value functions $V^E$ and $V^U$ from
   equations \eqref{EqVe2} and \eqref{EqVu} and then calculating the $2$-norm distance between
   both pairs of old and updated value functions to test for convergence. If both of these
   $2$-norm distances are less than $10^{-9}$, terminate the iteration.

   Before calculating the updated value functions, we first update our helper arrays \li{MVE} and
   \li{MEVU}. The rows of \li{MVE} need to equal \li{VE}. We can use array broadcasting:
\begin{lstlisting}
>>> MVE[:,:] = VE.reshape((1,N))
\end{lstlisting}
   The columns of \li{MEVU} need to equal \li{EVU}, so use a similar technique:
\begin{lstlisting}
>>> MEVU[:,:] = EVU.reshape((N,1))
\end{lstlisting}

   Now let us address how to compute the updated $V^U$, which we denote by \li{VU1}.
   Equation \eqref{EqVu} shows that it is the sum of
   two terms. The first, $u(\alpha w)$, we have already computed and stored in the variable \li{uaw}.
   The second term involves a maximization between two alternatives. One can imagine writing
   a double for-loop ranging over the values of $w'$ and $w$ to compute each individual
   $\max\{V^E(w'), \mathbb{E}_{w''}V^U(w,w'')\}$, but we can take advantage of the helper
   arrays \li{MVE} and \li{MEVU} to do this computation in one efficient line of code.
   Note that the $(i,j)$-th entry of \li{MVE} is just $V^E(w_j)$ and the $(i,j)$-th
   entry of \li{MEVU} is $\mathbb{E}_{w''}(w_i, w'')$, and
   \[
   V^U(w_i,w_j) = u(\alpha w_i) + \beta\max\{V^E(w_j), \mathbb{E}_{w''}V^U(w_i,w'')\}.
   \]
   Hence, taking the entrywise maximum of the arrays \li{MVE} and \li{MEVU} gives us the appropriate
   max term for $V^U$. To get the entrywise maximum of two arrays, stack the arrays along a new
   axis using \li{np.dstack}, and maximize along that axis. The computation for \li{VU}, then, is
   \begin{lstlisting}
>>> VU1 = uaw + beta*np.max(np.dstack([MEVU, MVE]), axis=2)
   \end{lstlisting}

   Calculating the updated $V^E$, denoted by \li{VE1}, is more straightforward.
   Equation \eqref{EqVe2} shows that it is just
   a particular linear combination of the arrays \li{uw}, \li{VE}, and \li{EVU}:
   \begin{lstlisting}
>>> VE1 = uw + beta*((1-gamma)*VE + gamma*EVU)
   \end{lstlisting}

   We can now calculate the 2-norm distances between old and updated value functions.
   It remains to update \li{VE}, \li{VU}, and \li{EVU}.
   The first two updates are trivial, and calculating \li{EVU} is equivalent to the
   matrix-vector multiplication of \li{VU} with \li{f}. This is similar to how we computed
   expectations in previous labs:
   \begin{lstlisting}
>>> EVU = np.dot(VU,f).ravel()
   \end{lstlisting}
   We use the \li{ravel} function to ensure that \li{EVU} is a flat array.

   \item Notice that it is not necessary to iteratively update the policy function, as it is not needed
   to update the value functions. Thus, we need only compute the policy function once, after convergence
   of the value functions has been achieved. This is done in a manner similar to calculating
   $\max\{V^E(w'), \mathbb{E}_{w''}V^U(w,w'')\}$ as described above, except we need to take the \emph{argmax}:
\begin{lstlisting}
>>> PSI = np.argmax(np.dstack([MEVU,MVE]), axis=2)
\end{lstlisting}

   \item Compute the reservation wage $w_R'$ as a function of the current wage $w$. It will be represented
   by a length $N$ array called \li{wr}. The reservation wage is the
   value of $w'$ where the policy function changes from zeros to ones (the optimal choice changes from remaining
   unemployed to accepting the job offer). We can calculate this as follows:
   \begin{lstlisting}
>>> wr_ind = np.argmax(np.diff(PSI), axis = 1)
>>> wr = w[wr_ind]
   \end{lstlisting}

   \item Plot the equilibrium reservation wage $w_R'$ of the converged problem as a function of the current
   wage $w$ with the current wage on the $x$-axis and the reservation wage $w_R'$ on the $y$-axis. This is
   the most common way to plot discrete choice policy functions. The reservation wage represents the wage
   that makes the unemployed worker indifferent between taking a job offer and rejecting it. So any wage
   above the reservation wage line represents $s' = E$ and any wage below the reservation wage line represents
   $s' = U$. Your plot should resemble that in Figure \ref{fig:res_wage}.

\end{enumerate}
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{reservation_wage.pdf}
\caption{The reservation wage as a function of previous wage for the job search problem.}
\label{fig:res_wage}
\end{figure}

In the previous problem, it was necessary to iterate on two value functions.
Consequently the convergence is relatively slow.
We can improve upon this situation by using modified policy function iteration.

\begin{problem}
Solve the same problem, this time using modified policy function iteration with $15$ value function iterations
within each policy iteration.  You should be able to re-use much of your code from the previous problem.

Start off by initializing all of the same variables. Additionally, initialize your policy function array \li{PSI},
say
\begin{lstlisting}
>>> PSI = 2*np.ones((N,N))
\end{lstlisting}

Next comes the iteration.
Essentially, the iteration will consist of an outer while-loop (which terminates once the 2-norm distance
between successive policy functions passes below $10^{-9}$), and an inner for-loop (with $15$ loops).

The first step in the while-loop is to calculate the new policy function \li{PSI1}, just as in the previous
problem. Next, perform the inner for-loop, which consists simply of the value function iteration, but this
time using the current policy function. This means the line of code
\begin{lstlisting}
>>> VU = uaw + beta*np.max(np.dstack([MEVU, MVE]), axis=2)
\end{lstlisting}
is no longer valid, as it does not use the policy function. We must instead have
\begin{lstlisting}
>>> VU = uaw + beta*(MVE*PSI1 + MEVU*(1 - PSI1))
\end{lstlisting}
Why is this code correct?

Finally, after exiting the for-loop, calculate the 2-norm distance between the old and the new policy function,
and then update your old policy function, i.e.
\begin{lstlisting}
>>> PSI = PSI1
\end{lstlisting}

After convergence is achieved, once again compute the reservation wage array, and plot it as in the previous
problem. Then return the converged policy function.
\end{problem}

\begin{problem}
How many iterations did value function iteration take?
How many iterations did modified policy function iteration take?
Which was faster?
\end{problem}


\documentclass[bigger]{beamer}

\input{header-beam} % change to header-handout for handouts

% ====================
\title[Lecture 5]{Logic I F13 Lecture 5}
\date{September 24, 2013}
% ====================

\include{header}

\section[Expressive Power]{Expressive Power of the Boolean Connectives}

\subsec{Truth Tables for the Boolean Connectives}{

\[
\begin{array}{c|c}
P & \lnot P\\
\hline
\T & \F \\
\F & \T
\end{array}
\qquad
\begin{array}{cc|c}
P & Q & (P \land Q)\\
\hline
\T & \T & \T\\
\T & \F & \F\\
\F & \T & \F\\
\F & \F & \F
\end{array}
\qquad
\begin{array}{cc|c}
P & Q & (P \lor Q)\\
\hline
\T & \T & \T\\
\T & \F & \T\\
\F & \T & \T\\
\F & \F & \F
\end{array}
\]

}

\subsec{Neither \dots nor \dots}{

\bits
\item Neither a nor b is a cube
\item $\sf \lnot Cube(a) \land \lnot Cube(b)$
\item $\sf \lnot (Cube(a) \lor Cube(b))$
\eit

}

\subsec{Not Both}{

\bits
\item a and b are not both large
\item $\sf \lnot(Large(a) \land Large(b))$
\item $\sf \lnot Large(a) \lor \lnot Large(b)$
\eit

}

\subsec{Exclusive ``Or''}{

\bits
\item a or b is a cube (but not both)
\item $\sf (Cube(a) \lor Cube(b)) \land \lnot(Cube(a) \land Cube(b))$
\item $\sf (Cube(a) \land \lnot Cube(b)) \lor (\lnot Cube(a) \land Cube(b))$
\eit
}

\subsec{At Least One Of}{

\bits
\item At least one of a, b, and c is small
\item $\sf Small(a) \lor (Small(b) \lor Small(c))$
\eit

}

\subsec{At Least Two Of}{

\bits
\item At least two of a, b, and c are small
\item $\sf (Small(a) \land Small(b)) \lor (Small(a) \land Small(c)) \lor (Small(b) \land Small(c))$
\eit

}

\subsec{At Least As Large As and $\le$}{

\bits
\item a is at least as large as b
\bits
\item $\sf Larger(a, b) \lor SameSize(a, b)$
\item $\sf \lnot Smaller(a, b)$, $\sf\lnot Larger(b, a)$
\eit
\item $2 \le 3$
\bits
\item $(1+1) < (1+(1+1)) \lor (1+1) = (1+(1+1))$
\item $\lnot (1 + (1+1)) < (1+1)$
\eit\eit

}
\section{Step-by-Step Translations}

\subsec{Step-by-Step Method of Translation}{

\bit
\item a and b are either left of c or right of d, \emph{but}\\ e is neither left of c nor right of d.
\bit
\item a and b are either left of c or right of d
\item but
\item e is neither left of c nor right of d
\eit\eit
}

\subsec{Step-by-Step Method of Translation}{

\bits
\item a \emph{and} b are either left of c or right of d
\bit
\item a is left of c \emph{or} right of d
\bit
\item a is left of c or a is right of d\\
$\sf LeftOf(a, c) \lor RightOf(a, d)$
\eit
\item b is left of c \emph{or} right of d
\bit
\item b is left of c or b is right of d\\
$\sf LeftOf(b, c) \lor RightOf(b, d)$
\eit
\eit
\eit
\begin{align*}
\sf (LeftOf(a, c) \lor {} & \sf RightOf(a, d)) \land {}\\
\sf (LeftOf(b, c) \lor {} & \sf RightOf(b, d))
\end{align*}

}

\subsec{Step-by-Step Method of Translation}{

\bits
\item e is neither left of c nor right of d
\[\sf \lnot(LeftOf(e, c) \lor RightOf(e, d))\]
\eit
}

\subsec{Step-by-Step Method of Translation}{

\bit
\item a and b are either left of c or right of d, but\\ e is neither left of c nor right of d.
\bit
\item a and b are either left of c or right of d
\begin{align*}
\sf (LeftOf(a, c) \lor {} & \sf RightOf(a, d)) \land {}\\
\sf (LeftOf(b, c) \lor {} & \sf RightOf(b, d))
\end{align*}
\item but
$\land$
\item e is neither left of c nor right of d
\[\sf \lnot(LeftOf(e, c) \lor RightOf(e, d))\]
\eit\eit
\begin{align*}
\sf((LeftOf(a, c) \lor {} & \sf RightOf(a, d)) \land {} \\
\sf (LeftOf(b, c) \lor {} & \sf RightOf(b, d))) \land {}\\
\sf \lnot(LeftOf(e, c) \lor {} &\sf RightOf(e, d))
\end{align*}

}

\section{Ambiguity}

\subsec{Ambiguity in English}{

\bit
\item Lexical ambiguity: one word---many meanings \\
e.g., ``bank'', ``crane''
\item Syntactic ambiguity: one sentence---many readings\\
e.g.,
\bit
\item ``Flying planes can be dangerous'' (Chomsky)
\item ``One morning I shot an elephant in my pajamas.\\ How he got in my pajamas, I don't know.'' (Groucho Marx)
\eit
\eit

}

\subsec{Connectives and Ambiguity}{

\bits
\item a adjoins b and c or d
\item a adjoins [[b and c] or d]\\
$\sf (Adjoins(a, b) \land Adjoins(a, c)) \lor Adjoins(a, d)$
\item a adjoins [b and [c or d]]\\
$\sf Adjoins(a, b) \land (Adjoins(a, c) \lor Adjoins(a, d))$
\eit

}

\subsec{The Man Who Was Hanged by a Comma}{

\bit
\item Sir Roger Casement (1864--1916)
\item British consul to Congo and Peru
\item Tried to recruit Irish revolutionaries in Germany during WWI
\item Tried for treason
\eit

}

\subsec{Treason Act of 1351}{

\small
ITEM, Whereas divers Opinions have been before this Time in what Case
Treason shall be said, and in what not; the King, at the Request of
the Lords and of the Commons, hath made a Declaration in the Manner as
hereafter followeth, that is to say; When a Man doth compass or
imagine the Death of our Lord the King, or of our Lady his Queen or of
their eldest Son and Heir; or if a Man do violate the King's
Companion, or the King's eldest Daughter unmarried, or the Wife of the
King's eldest Son and Heir; or \textbf{if a Man do levy War against our Lord
the King in his Realm, or be adherent to the King's Enemies in his
Realm, giving to them Aid and Comfort in the Realm\onslide<2>{\emph{,}} or elsewhere}, and
thereof be probably attainted of open Deed by the People of their
Condition: \dots And it is to be
understood, that in the Cases above rehearsed, that ought to be judged
Treason which extends to our Lord the King, and his Royal Majesty:
\dots

}

\subsec{R v. Casement in the Blocks Language}{

\bits
\item a is a cube in front of b, or a tetrahedron in front of b[,] or in back of b.

\item Without comma:
\begin{align*}
\sf(Cube(a) \land {} & \sf FrontOf(a, b)) \lor {}\\
\sf(Tet(a) \land {} & \sf(FrontOf(a, b) \lor BackOf(a, b)))
\end{align*}
\item With comma:
\begin{align*}
\sf(Cube(a) \land {} & \sf(FrontOf(a, b) \lor BackOf(a, b))) \lor {}\\
\sf(Tet(a) \land {} & \sf(FrontOf(a, b) \lor BackOf(a, b))
\end{align*}
\eit
}

\subsec{More Scope Ambiguity}{

\bits
\item a and b are left of c or right of d
\item a is left of c or right of d, and so is b
\begin{align*}
\sf (LeftOf(a, c) \lor {} & \sf RightOf(a, d)) \land {}\\
\sf (LeftOf(b, c) \lor {} & \sf RightOf(b, d))
\end{align*}
\item a and b are both left of c, or a and b are both right of d
\begin{align*}
\sf (LeftOf(a, c) \land {} & \sf LeftOf(b, c)) \lor {}\\
\sf (RightOf(a, d) \land {} & \sf RightOf(b, d))
\end{align*}
\eit

}

\end{document}


\gotosection{1}{2}
\subsection{Introducing the actors: matrices}

\begin{exercise}{2}
  \begin{enumerate}
    \item $\begin{xmatrix}1 & 2 & 3 \\ 4 & 5 & 6\end{xmatrix}
           \begin{xmatrix}7 & 8 \\ 9 & 0 \\ 1 & 2\end{xmatrix} =
           \begin{xmatrix}
              7 + 18 + 3 &  8 + 0 +  6 \\
             28 + 45 + 6 & 32 + 0 + 12
           \end{xmatrix} =
           \begin{xmatrix}28 & 14 \\ 79 & 44\end{xmatrix}$

    \item Incompatible matrices: the first matrix has 2 columns while the second
          one has 3 rows.

    \item $\begin{aligned}[t]
            \begin{xmatrix}
               1 & -1 &  1 \\
              -1 &  0 &  2 \\
              -1 &  1 &  1
            \end{xmatrix}
            \begin{xmatrix}
               0 &  1 & -1 \\
              -1 &  1 &  2 \\
               2 &  0 & -2
            \end{xmatrix} &=
            \begin{xmatrix}
              0 + 1 + 2 &  1 - 1 + 0 & -1 - 2 - 2 \\
              0 + 0 + 4 & -1 + 0 + 0 &  1 + 0 - 4 \\
              0 - 1 + 2 & -1 + 1 + 0 &  1 + 2 - 2
            \end{xmatrix} \\ &=
            \begin{xmatrix}
              3 &  0 & -5 \\
              4 & -1 & -3 \\
              1 &  0 &  1
            \end{xmatrix}
          \end{aligned}$

    \item $\begin{xmatrix}7 & 1 \\ -1 & 0 \\ 2 & 3\end{xmatrix}
           \begin{xmatrix}5 \\ -4\end{xmatrix} =
           \begin{xmatrix}
             35 - 4 \\
             -5 + 0 \\
             10 - 12
           \end{xmatrix} =
           \begin{xmatrix}31 \\ -5 \\ -2\end{xmatrix}$

    \item \def \tmp{\begin{bmatrix*}[r]0 & 1 \\ -1 & 3\end{bmatrix*}}
          $\begin{aligned}[t]
            \begin{xmatrix}1 & 2 \\ 0 & 3\end{xmatrix}
            \begin{xmatrix}1 & 4 \\ 1 & 3\end{xmatrix}
            \tmp &=
            \begin{xmatrix}
              1 + 2 & 4 + 6 \\
              0 + 3 & 0 + 9
            \end{xmatrix}
            \tmp \\ &=
            \begin{xmatrix}3 & 10 \\ 3 & 9\end{xmatrix}
            \tmp \\ &=
            \begin{xmatrix}
              0 + 3 &   0 +  9 \\
              -3 + 9 & -10 + 27
            \end{xmatrix} \\
            &= \begin{xmatrix}3 & 9 \\ 6 & 17\end{xmatrix}
          \end{aligned}$

    \item Incompatible matrices: the first matrix has 3 columns while the second
          one has 2 rows.
  \end{enumerate}
\end{exercise}

\begin{exercise}{3}
  \begin{enumerate}
    \item $\begin{pmatrix}AB_{13}\\AB_{23}\end{pmatrix} =
           \begin{xmatrix}1 & 2 & 0\\3 & 1 & -1\end{xmatrix}
           \begin{pmatrix}1 \\ 2 \\ 3\end{pmatrix} =
           \begin{pmatrix}1+4 \\ 3+2-3\end{pmatrix} =
           \begin{pmatrix}5 \\ 2\end{pmatrix}$
    \item $\begin{aligned}[t]
            \begin{xmatrix}AB_{21} & AB_{22} & AB_{23}\end{xmatrix} &=
            \begin{xmatrix}3 & 1 & -1\end{xmatrix}
            \begin{xmatrix}
              2 & 5 & 1 \\
              1 & 4 & 2 \\
              1 & 3 & 3
            \end{xmatrix}
            \\ &=
            \begin{xmatrix}6 + 1 - 1 & 15 + 4 - 3 & 3 + 2 - 3\end{xmatrix}
            \\ &=
            \begin{xmatrix}6 & 16 & 2\end{xmatrix}
          \end{aligned}$
  \end{enumerate}
\end{exercise}

\begin{exercise}{5}
  Since $A$ is symmetric, $A = A^{\top}$.

  \begin{enumerate}
    \item True. $B^{\top}A = B^{\top}A^{\top} = (AB)^{\top}$ \QED
    \item True. $B^{\top}A^{\top} = (AB)^{\top} = (A^{\top}B)^{\top}$ \QED
    \item False. $(A^{\top}B)^{\top} = B^{\top}A \neq BA$,
          since $B^{\top} = B$ doesn't hold if $B$ is not symmetric.
    \item False. $(AB)^{\top} = B^{\top}A^{\top} \neq A^{\top}B^{\top}$,
          since $XY = YX$ iff $X$ is the inverse of $Y$.
  \end{enumerate}
\end{exercise}

\begin{exercise}{8}
  The only value of $a$ such that $AB = BA$ is $0$, as:
  \medskip

  $AB = \begin{xmatrix}1 & 1 \\ 1 & 0\end{xmatrix}
        \begin{xmatrix}1 & 0 \\ a & 1\end{xmatrix}
      = \begin{xmatrix}1 + a & 1 \\ 1 & 0\end{xmatrix}$
  \medskip

  $AB = \begin{xmatrix}1 & 0 \\ a & 1\end{xmatrix}
        \begin{xmatrix}1 & 1 \\ 1 & 0\end{xmatrix}
      = \begin{xmatrix}1 & 1 \\ 1 + a & a\end{xmatrix}$
  \medskip

  $\left\{
    \begin{aligned}
      1 + a &= 1 \\
          a &= 0
    \end{aligned}
  \right.\quad \Longrightarrow \quad a = 0$
\end{exercise}

\begin{exercise}{10}
  Follow the formula for the inverse of $2\times2$-matrix:

  \begin{align*}
    A^{-1} &= \frac{1}{a^2-0}\begin{xmatrix}a & -b\\0 & a\end{xmatrix} \\
           &= \begin{xmatrix}
                \frac{1}{a} & -\frac{b}{a^2} \\
                          0 &  \frac{1}{a}
              \end{xmatrix}
  \end{align*}
\end{exercise}

\begin{exercise}{15}
  Let $A = \begin{xmatrix}
             1 & a & b \\
             0 & 1 & c \\
             0 & 0 & 1
           \end{xmatrix}$,
  and $B = \begin{xmatrix}
             1 & x & y \\
             0 & 1 & z \\
             0 & 0 & 1
           \end{xmatrix}$.
  The product is

  $$AB =
    \begin{xmatrix}
      1 & x + a & y + az + b \\
      0 &     1 &      z + c \\
      0 &     0 &          1
    \end{xmatrix} =
    I_3 = \begin{xmatrix}1&0&0\\0&1&0\\0&0&1\end{xmatrix}$$

  Then we have:
  \medskip

  $\left\{
    \begin{aligned}
           x + a &= 0 \\
      y + az + b &= 0 \\
           z + c &= 0
    \end{aligned}
  \right.\quad \Longrightarrow \quad
  \left\{
    \begin{aligned}
               x &= -a \\
               z &= -c \\
      y - ac + b &= 0
    \end{aligned}
  \right.\quad \Longrightarrow \quad
  \left\{
    \begin{aligned}
      x &= -a     \\
      y &= ac - b \\
      z &= -c
    \end{aligned}
  \right.$
\end{exercise}

\begin{exercise}{16}
  From the definition of transpose and symmetric matrix,
  it is obvious that $(A^{\top})^{\top} = A$,
  and that if $A = A^{\top}$, then $A$ is symmetric.

  For any matrix $A$, since $(A^{\top}A)^{\top} = A^{\top}(A^{\top})^{\top} = A^{\top}A$,
  namely $A^{\top}A$ equal to its transpose, $A^{\top}A$ is symmetric. \QED
\end{exercise}


\input{../common/common.tex}

\title{Math notes - Last three digits before decimal point}
\author{Uwe Hoffmann}
\hypersetup{colorlinks, pdftitle={Math notes - Last three digits before decimal point}}

\begin{document}

\setcounter{chapter}{1}
\section*{Last three digits before decimal point}

\newthought{Recurrence relations} and modulo arithmetic are the topics of the problem \footnote{\bibentry{negruseri08}} in this note.\index{recurence relations}

\vspace{10 mm}
\begin{problem}
Find the last three digits before the decimal point for the number $(3 + \sqrt{5})^n$. For example, when $n = 5$, $(3 + \sqrt{5})^5 = 3935.73982\ldots$, the answer is $935$. For $n = 2$, $(3 + \sqrt{5})^2 = 27.4164079\ldots$, the answer is $027$.  The value of $n$ is in the range $2 \le n \le 2000000000$.
\end{problem}

Looking at the numbers $(3 + \sqrt{5})^n$, we can see that in general they are not integers. Ideally we would like to deal with integers. This sparks the idea of introducing the complement of $(3 + \sqrt{5})$ into the mix, namely $(3 - \sqrt{5})$. Let's look at the binomial expansion
\footnote{Binomial expansion:
\[
(a + b)^n = \sum^{n}_{i=0} \binom{n}{i} a^{i}b^{n-i}
\]
} of $(3~+~\sqrt{5})^n$:
\[
	(3 + \sqrt{5})^n = \sum^{n}_{i=0} \binom{n}{i} 3^{i}(\sqrt{5})^{n-i}
\]

\noindent Compare this to the binomial expansion of $(3 - \sqrt{5})^n$:
\[
	(3 - \sqrt{5})^n = \sum^{n}_{i=0} \binom{n}{i} 3^{i}(-\sqrt{5})^{n-i}
\]

\noindent When $n-i$ is even, then $(\sqrt{5})^{n-i}$ and $(-\sqrt{5})^{n-i}$ are integers. When $n-i$ is odd, then the binomial terms  for $(\sqrt{5})^{n-i}$ and $(-\sqrt{5})^{n-i}$  in the binomial expansions cancel each other out. So it follows that
\[
 	\forall n \in \mathbb{N}: (3 + \sqrt{5})^n + (3 - \sqrt{5})^n \in \mathbb{N}
\]

\noindent This is encouraging, so we define for all $n$:
\[
\begin{split}
	a_n & = (3 + \sqrt{5})^n \\
	b_n & = (3 - \sqrt{5})^n \\
	c_n & = a_n + b_n
\end{split}
\]

\noindent We see that $\forall n \in \mathbb{N}: 0 < b_n < 1$, so $c_n=\lceil a_n \rceil$.

Concentrating on $c_n$, lets try to find the hundreds digit, the tens digit and the units digit of $c_n$.

Consider the polynomial:
\[
	(x - (3 + \sqrt{5})) (x - (3 - \sqrt{5})) = x^2 - 6x + 4
\]

\noindent It leads to the recurrence relation: $f_n = 6 f_{n-1} - 4 f_{n-2}$, for which any linear combination of  $a_n$ and $b_n$ is a solution \footnote{In-depth treatment of recurrence relations can be found in Chapter 10, \bibentry{grimaldi93}}. We set the initial values of $f_n$ such that the linear combination $c_n=a_n + b_n$ is the solution: $f_0=2, f_1=6$.

\noindent Therefore $c_n$ satisfies the recurrence:
\[
\begin{split}
	c_n & = 6 c_{n-1} - 4 c_{n -2} \\
	c_0 & = 2 \\
	c_1 & = 6
\end{split}
\]

In theory we could just use this recurrence to compute $c_n$ and then extract the hundreds digit, the tens digit and the units digit. Unfortunately this is not feasible for large $n$, since $c_n$ grows quickly to very large values. But since we only need the last three digits of the values,  we don't need to compute the values completely, computing them modulo $1000$ will suffice.

Fortunately according to modulo arithmetic, the recurrence relation for $c_n$ is still valid when doing modulo $1000$. Let:
\[
	d_n \equiv c_n \mod{1000}
\]
and so
\[
	d_n \equiv 6 d_{n-1} - 4 d_{n -2} \mod{1000}
\]

\noindent Now consider the ordered pairs $(d_n, d_{n+1}), n \in \mathbb{N}$. Because $d_n \in \{0, 1, 2, \ldots, 999\}$, there are only $10^6$ distinct pairs of $(d_n, d_{n+1})$ possible. So it must be that there exist two indices $i, j \in \mathbb{N}^+$ such that:
\[
	(d_i, d_{i+1}) = (d_j, d_{j+1})
\]
\noindent From the recurrence it  follows that:
\[
	\forall k \in \mathbb{N}: (d_{i+k}, d_{i + k + 1}) = (d_{j + k}, d_{j + k + 1})
\]

\begin{marginfigure}[1.0in]
\begin{tikzpicture}
\draw (-4,-1) -- (0,-1);
\foreach[count=\i, evaluate=\i as \y using int(\i)] \x in {0,...,3}{
	\path[draw, fill=blue] (\x - 4,-1) circle[radius=2pt] node [below=2 mm] {\tiny{$(d_\x, d_\y)$}};
}
\draw circle [radius=1];
\foreach \a/\k/\h in {270/4/5, 300/5/6, 330/6/7, 0/7/8, 30/8/9, 60/9/10, 90/10/11, 120/11/12, 150/12/13, 180/13/14, 210/14/15}{
	  \draw  (\a: 1.5) node {\tiny{$(d_{\k},d_{\h})$}};
      \path[draw, fill=blue]  (\a: 1) circle[radius=2pt];
}
\end{tikzpicture}
\caption{Periodic sequence of pairs preceded by a prefix of pairs.}
\label{fig:looppairs}
\end{marginfigure}

The sequence of ordered pairs $(d_n, d_{n+1})$ is periodic with a period $p$ of at most $10^6$. We can construct a lookup table holding values of $d_n$ from one period $p$ and then compute $d_n$ for large $n$ by going into the lookup table at $n \mod{p}$.

The periodic part of the sequence doesn't necessarily start with the first pair in the sequence or with the second or the third etc... There might be a sequence prefix of ordered pairs that don't repeat before it goes into the sequence loop of repeating pairs.
{}
We write a function that computes the prefix and the period of the sequence using Floyd's cycle finding algorithm\footnote{Floyd's algorithm is described at \url{https://en.wikipedia.org/wiki/Cycle_detection\#Tortoise_and_hare}. We use the Haskell implementation from \url{https://wiki.haskell.org/Floyd's_cycle-finding_algorithm}}.

Luckily it turns out that in this case the prefix is  $2, 6, 28$  and the periodic sequence has period $100$.

With the lookup table we can compute $d_n$ for any large $n$ in constant time. $d_n$ gives us the last three digits of $c_n$. Our goal though was to compute the last three digits before the decimal point of $a_n$.

We know $c_n=\lceil a_n \rceil$, so $c_n - 1=\lfloor a_n \rfloor$. This means that to get the digits for $a_n$, we need to extract them from $d_n - 1$. Listing \ref{lst:last3digitshaskell} has the complete Haskell implementation.

\begin{fullwidth}

\lstinputlisting[language=Haskell, basicstyle=\small, label={lst:last3digitshaskell}, frame=trBL, caption={Haskell code to compute last 3 digits}]{Last3Digits.hs}

To check our computation we can use this Mathematica function:

\begin{mmaCell}[morepattern={n, n_Integer}]{Code}
  last3Digits[n_Integer] := Mod[IntegerPart[(3 + Sqrt[5])^n], 1000]
\end{mmaCell}

\end{fullwidth}

\bibliographystyle{plainnat}
\bibliography{../common/math}

\end{document}


%---------------------------------------------------------------------------------
\chapter{Fitzhugh-Nagumo Model Example}
\label{chap:fitzhugh-nagumo}
%---------------------------------------------------------------------------------
\section{Background}
\label{sec:background}
We will look into Fitzhugh-Nagumo model as an application of the Python package. The Fitzhugh-Nagumo model describes an excitable system, such as the action potential of cardiac cells. The action potential was first described by Hodgkin and Huxley. Their model were then simplified to the Fitzhugh-Nagumo model, retaining the fast-slow phase and the excitability of the Hodgkin \& Huxley model. \cite{Keener2009}

This model is relevant to my D.Phil.~project as my project will be related to action potential of cardiac muscle cells, an excitable system. I will be working on sodium ion channels of cardiac muscle cells, studying the effect of the flow of sodium ions across the cell membrane on the cell's action potential. Moreover, Fitzhugh-Nagumo model captures all the important features of an action potential, the excitability and the fast-slow phase. Therefore, it is a good simple model to start with.

\section{Fitzhugh-Nagumo model}
\label{sec:FHN}
The definition of Fitzhugh-Nagumo model is
\begin{align}
\label{eqn:FHN}
    \epsilon \frac{dv}{dt} &= f(v) - w + I_{app} \\
    \frac{dw}{dt} &= v - \gamma w \label{eqn:FHN-end}
\end{align}
where $f(v) = v(1-v)(v-\alpha)$, $0 < \alpha < 1$, $\epsilon \ll 1$, $I_{app}$ is the applied current and $t$ is time. The fast $v$ is the excitation variable, while the slow $w$ is the recovery variable.

In this implementation, the parameters are chosen to be $\alpha = 0.1$, $\gamma = 0.5$, $\epsilon = 0.01$ and $I_{app} = 0.026$, taking reference from \cite{Chapwanya2018}.
The initial values are taken to be near the origin, which are $(v_0, w_0) = (0.01, 0.01)$. The model is solved for time $t$ from 0 to 1.

Fitzhugh-Nagumo model is solved with the various numerical methods implemented in the software that I have developed. The solutions of the model are in a notebook at the link:  \href{https://nbviewer.jupyter.org/github/FarmHJ/numerical-solver/blob/main/examples/fitzhugh_nagumo.ipynb}{\underline{\emph{Fitzhugh-Nagumo model notebook}}}.

\begin{figure}
    \includegraphics[width=0.95\columnwidth]{FHN_Euler_explicit}
    \caption{(\textit{A}) Phase plane of $v$ and $w$ by Euler's explicit method. (\textit{B}) Graph of $v$ and $w$ against time by Euler's explicit method.}
    \label{fig:FHN_Euler_explicit}
\end{figure}

From Figure \ref{fig:FHN_Euler_explicit} B, we can see that $v$, the excitation variable is excited in the early stage. While $v$ increases significantly, the change in $w$ is small. After $v$ reaches its peak and starts to reduce, $w$ increases slowly. This can be observed in both the phase plane (Figure \ref{fig:FHN_Euler_explicit} A) and the variable graph (Figure \ref{fig:FHN_Euler_explicit} B). When the variable $v$ starts to recover to its original value, $w$ is at its maximum. The scattering of points in the phase plane captures the feature of the model, where the change in $v$ is rapid while the change in $w$ is slow. When the change in $v$ is significantly larger than the change in $w$, the points are sparse. On the other hand, the points are packed when $w$ increases or decreases more than $v$. However, in the adaptive methods, such insights cannot be interpreted directly from the phase plane. Therefore, green triangles were plotted in Figure \ref{fig:FHN_adaptive} B to indicate the adapted mesh points. The mesh points are adapted towards large change in $v$ or $w$ over a short period of time.

\begin{figure}
    \includegraphics[width=0.95\columnwidth]{FHN_adaptive}
    \caption{(\textit{A}) Phase plane of $v$ and $w$ for adaptive method BS23. (\textit{B}) Graph of $v$ and $w$ against time for adaptive method BS23}
    \label{fig:FHN_adaptive}
\end{figure}

\section{Convergence of Fitzhugh-Nagumo model}
\label{sec:FHN-convergence}
A notebook (accessible from the link: \href{https://nbviewer.jupyter.org/github/FarmHJ/numerical-solver/blob/main/examples/fhn_model_convergence.ipynb}{\underline{\emph{Fitzhugh-Nagumo convergence notebook}}}) is created to test the convergence of the solution of the Fitzhugh-Nagumo model. Since the model has no analytical solution, it is tested against a reference solution. The reference solutions are assumed to be sufficiently accurate. For methods with fixed step size, which are the one-step methods and predictor-corrector method, the reference solutions are constructed by using a much smaller step size of $1^{-7}$, as compared to $1^{-5}$, the smallest step size for other numerical solutions. For methods with adaptive step size, reference solutions are obtained by using a much smaller tolerance value of $1^{-8}$, as compared to $1^{-5}$, the smallest tolerance value for other numerical solutions. This notebook shows the numerical solution computed for different methods at different step sizes or tolerance values. The numerical solutions are then compared with their respective reference solutions. In both methods, the error decreases as the step size or the tolerance value decreases, as shown in Figure \ref{fig:Euler_explicit_error} and Figure \ref{fig:adaptive_error}.

\begin{figure}
    \includegraphics[width=0.95\columnwidth]{FHN_Euler_explicit_error_behaviour}
    \caption{Error at a mesh point for Euler's explicit method. The Fitzhugh-Nagumo model Eqs.~\eqref{eqn:FHN}-\eqref{eqn:FHN-end} is solved with Euler's explicit method at different step sizes. The error is the absolute difference between reference solution and numerical solution at $x=0.7$. The reference solution is taken at step size of $1^{-7}$. Note that $\log(1^{-7})\simeq-16.118$.}
    \label{fig:Euler_explicit_error}
 \end{figure}
\begin{figure}
   \includegraphics[width=0.95\columnwidth]{FHN_adaptive_error_behaviour}
   \caption{Sum of error for adaptive method BS23. The Fitzhugh-Nagumo model Eqs.~\eqref{eqn:FHN}-\eqref{eqn:FHN-end} is solved with BS23 method at different absolute tolerance values. The relative tolerance is fixed at $1^{-15}$. The error is the sum of absolute difference between reference solution and numerical solution at all mesh points. The reference solution is taken at relative tolerance of $1^{-8}$. Note that $\log(1^{-8})\simeq-18.421$.}
   \label{fig:adaptive_error}
\end{figure}


\input{../../style/preamble}
\input{../../latex-math/basic-math}
\input{../../latex-math/basic-ml}

\newcommand{\titlefigure}{figure/lasso_ridge_enet_2d.png}
\newcommand{\learninggoals}{
  \item Know the elastic net as compromise between Ridge and Lasso regression
  \item Know regularized logistic regression
}

\title{Introduction to Machine Learning}
\date{}

\begin{document}

\lecturechapter{Elastic Net and Regularization for GLMs}
\lecture{Introduction to Machine Learning}

% \section{Elastic Net}

\begin{vbframe} {Elastic Net}

Elastic Net combines the $L_1$ and $L_2$ penalties:

$$
\mathcal{R}_{\text{elnet}}(\thetab) =  \sumin (\yi - \thetab^\top \xi)^2 + \lambda_1 \|\thetab\|_1 + \lambda_2 \|\thetab\|_2^2.
$$

\begin{figure}
\includegraphics[width=0.6\textwidth]{figure/lasso_ridge_enet_2d.png}\\
\end{figure}

\begin{itemize}
\item Correlated predictors tend to be either selected or zeroed out together.
\item Selection of more than $n$ features possible for $p>n$.
\end{itemize}

\framebreak
\footnotesize
Simulating two examples with each 50 data sets and 100 observations each: \\

$$\ydat =\Xmat \boldsymbol{\beta}+\sigma \epsilon, \quad \epsilon \sim N(0,1), \quad \sigma = 1$$

  \begin{columns}
\begin{column}{0.5\textwidth}
\begin{center}
\textbf{Ridge} performs better for: \\
$\boldsymbol{\beta}=(\underbrace{2,\ldots,2}_{5},\underbrace{0,\ldots,0}_{5})$\\
$ \operatorname{corr}(\Xmat_{i},\Xmat_{j})=0.8^{|i-j|}$ for all $i$ and $j$
  \end{center}
\end{column}
\begin{column}{0.5\textwidth}
\begin{center}
\textbf{Lasso} performs better for: \\
$\boldsymbol{\beta}=(2, 2, 2,\underbrace{0,\ldots,0}_{7})$ \\
$\operatorname{corr}(\Xmat_{i},\Xmat_{j})= 0$ for all $i \neq j$, otherwise 1
\end{center}
\end{column}
\end{columns}

\begin{figure}
\includegraphics[width=1\textwidth]{figure_man/elastic-net02.png}\\
\end{figure}

\framebreak

\begin{figure}
\includegraphics[width=0.9\textwidth]{figure_man/elastic-net03.png}\\
\end{figure}

\normalsize
Since Elastic Net offers a compromise between Ridge and Lasso, it is suitable for both data situations.

\end{vbframe}

% \section{Regularized Logistic Regression}

\begin{vbframe}{Regularized Logistic Regression}

Regularizers can be added very flexibly to basically any model which is based on ERM.

\lz

Hence, we can, e.g., construct $L_1$- or $L_2$-penalized logistic regression to enable coefficient shrinkage and variable selection in this model.

% \lz
% We can add a regularizer to the risk of logistic regression

\begin{align*}
\riskrt &= \risket + \lambda \cdot J(\thetab) \\
%&= \sumin \mathsf{log} \left[1 + \exp \left(-\yi f\left(\left.\xi~\right|~ \thetab\right)\right)\right] + \lambda \cdot J(\thetab) \\
&= \sumin \mathsf{log}\left[1 + \mathsf{exp}\left(-2\yi f\left(\left.\xi~\right|~ \thetab\right)\right)\right] + \lambda \cdot J(\thetab)
\end{align*}

% The other parts of the logistic regression remain exactly the same,
% except for the fitting algorithm to find \(\hat{\thetab}_{\text{reg}}\) (no closed-form solution, numerical optimization methods are necessary).

\end{frame}

\begin{frame}{Regularized Logistic Regression}

We fit a logistic regression model using polynomial features for \(x_1\)
and \(x_2\) with maximum degree of \(7\). We add an $L_2$ penalty. We
see for

\begin{itemize}

\item
  \(\lambda = 0\): The unregularized model seems to overfit.
\item
  \(\lambda = 0.0001\): Regularization helps to learn the underlying
  mechanism.
\item
  \(\lambda = 1\): The real data-generating process is captured very well.
\end{itemize}

\scriptsize

\begin{figure}
\includegraphics[width=0.8\textwidth]{figure_man/logistic-reg.png}\\
\end{figure}

\normalsize

\end{vbframe}

\endlecture
\end{document}


\subsection{Constraint Satisfaction Problem}

A CSP problem is one where we don't care about the path, we just want to identify the goal state.

For example, solving a sudoku

\subsection{Defining a CSP}

A CSP has:

\begin{itemize}
\item Variables \(X_i\).
\item Domain for each variable \(D_i\).
\item Constraints \(C_j\).
\end{itemize}

In a CSP there are a range of variables each with a domain. There are on top constraints on combinations of values.

A solution does not violate any constraint.

To solve, start with no allocations of variables. successor function assigns a value to an unassigned variable. goal test

Use heuristic minimum remaining value MRV: choose variable with fewest remaining legal values

Least constraining value: choose item in domain which constrains the least other moves

Forward checking. keep track of remaining legal moves for each variable. terminate if none left

After each move, update legal moves for each

Implement all this with recursive backtrack function, which returns a solution or failure. This is a depth first search

\subsection{Arc-consistency}

X-Y is arc consistent if all of domain of X is consistnet with some value of Y.

\subsection{Node-consistency}

X is node consistent if all of domain satisfies all unary constraint.

\subsection{Path-consistency}

Arc consistency for additional variables.

\subsection{Constraint propagation}

Constraint propagation can be used to prevent bad choices

We can check for:

\begin{itemize}
\item node-consistency
\item arc-consistency
\item path-consistency
\end{itemize}

\subsection{AC-3}

AC-3 algorihm makes a CSP arc-consistent

Take all arcs.

It may be possible to break the problem down into sub problems, making the problems much easier to solve.

Can do this before/after other algorithm.


\chapter{Networks from Distances}

So far we have been using existing network graphs. But we can also generate a graph from regular tabular data. Let us take the \emph{iris} data set. For now, we only have flowers as rows and their characteristics as columns. So how do we transform this to nodes and edges?

By looking at similarities. Rows will be nodes and the edge will be created between two rows if their similarity is above a certain threshold. Nice, isn't it?

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{workflow.png}
    \caption{$\;$}
\end{figure}

First, we need distance matrix, which we can compute with the \widget{Distances} widget. Let's stick with Euclidean distances as it works well with this 4-dimensional data set.

Then pass the distance matrix to \widget{Network from Distances}. The widget enables setting the distances threshold, which we will set to 2.0 percentiles (meaning we keep the top 2\% of the edges that connect the most similar nodes).

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.45]{net-from-distances.png}
    \caption{$\;$}
\end{figure}

\newpage

Finally, pass the constructed graph to \widget{Network Explorer}. Remember, this is not a scatter plot. The position of the points is not mapped to any sort of attribute. In the graph, you can simply see which elements are most similar to one another.

\begin{figure*}[h]
    \centering
    \includegraphics[width=\linewidth]{network-explorer.png}
    \caption{$\;$}
\end{figure*}


\section{Background}
\frame{\tableofcontents[currentsection, hideothersubsections]}

\begin{frame}
\frametitle{Background: MDP}
\textbf{The environment}, $E$: \\
a Markov decision process with a state space $S$, action space $A = \mathbb{R}^N$,
an initial state distribution $p(s_1)$, transition dynamics $p(s_{t+1}|s_t, a_t)$, and
reward function $r(s_t, a_t)$.
\vspace{2.5mm}

\textbf{At each discrete timestep} $t$, \\
the agent receives an observation $x_t = s_t$ (fully observable),
takes an action $a_t$ and receives a scalar reward $r_t$.
\vspace{2.5mm}

\textbf{The return from a state}: \\
$R_t= \sum_{i=t}^T  \gamma^{(i-t)} r(s_i, a_i)$ with a discounting factor $\gamma \in [0, 1]$.
\vspace{2.5mm}

\textbf{Goal}: \\
to learn a policy, $\pi: S \mapsto P(A)$, which
maximizes $J = \mathbb{E}_{r_i,s_i \sim E,a_i \sim \pi} [R_1]$.
\vspace{2.5mm}

\textbf{Action-value function}:\\
$Q^{\pi}(s_t,a_t) = \mathbb{E}_{r_{i \ge t},s_{i>t} \sim E,a_{i>t} \sim \pi} [R_t|s_t,a_t]$.
\end{frame}

\begin{frame}
\frametitle{Background: MDP with deterministic policy}
Bellman equation (the recursive relationship):
\begin{equation}
Q^{\pi} (s_t,a_t) = \mathbb{E}_{r_{t},s_{t+1} \sim E} \Big[ r(s_t,a_t) + \gamma \mathbb{E}_{a_{t+1} \sim \pi} [Q^{\pi}(s_{t+1},a_{t+1})] \Big]
\end{equation}

If the target policy is deterministic, $\mu: S \mapsto A$, then:
\begin{equation}
Q^{\mu} (s_t,a_t) = \mathbb{E}_{r_{t},s_{t+1} \sim E} \Big[ r(s_t,a_t) + \gamma Q^{\mu}(s_{t+1},\mu(s_{t+1})) \Big]
\end{equation}

Thus:
\begin{itemize}
\item the expectation depends only on the environment $E$,
\item possible to learn $Q^{\mu}$ off-policy, using transitions which
are generated from a different stochastic behavior policy $\beta$.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Background: Fn approximator}
Consider fn approximators parameterized by $\theta^Q$, \\
which we optimize by minimizing the loss:
\begin{equation} \label{equ:qloss}
L(\theta^Q) =\mathbb{E}_{s_t \sim \rho^{\beta}, a \sim \beta, r_t \sim E} \Big[ \Big( Q (s_t,a_t|\theta^Q) - y_t \Big)^2 \Big]
\end{equation}
where:\\
$y_t = r(s_t,a_t) + \gamma Q(s_{t+1},\mu(s_{t+1}) | \theta^Q)$, and \\
$\rho^{\beta}$: the discounted state visitation distribution for a policy $\beta$.
\vspace{5mm}

Typically, ignore the fact that $y_t$ is also dependent on $\theta^Q$.\\
\end{frame}

\begin{frame}
\frametitle{Background: Actor-critic approach}
\begin{figure}
    \centering
    \includegraphics[scale=0.35]{actorcritic_arch}
\end{figure}
\end{frame}

% \begin{frame}
% \frametitle{Background: Actor-critic \cite{Sutton1998}}
% The Actor-Critic Algorithm is essentially a hybrid method to combine the policy gradient method and the value function method together.
% The policy function is known as the actor, while the value function is referred to as the critic.
% Essentially, the actor produces the action aa given the current state of the environment ss, while the critic produces a signal to criticizes the actions made by the actor.
% \end{frame}

% deep Q-network:
% \begin{itemize}
%   \item innovation:
%   \begin{itemize}
%     \item the network is trained off-policy with samples from a replay buffer to minimize correlations between samples;
%     the use of a replay buffer
%     \item the network is trained with a target Q network to give consistent targets during temporal difference backups.
%     a separate target network for calculating $y_t$
%   \end{itemize}
%   \item able to:
%   \begin{itemize}
%     \item solves problems with high-dimensional observation spaces,
%     \item can only handle discrete and low-dimensional action spaces.
%   \end{itemize}
% \end{itemize}


\subsection{Selection Based on Spline regression}
\begin{itemize}
\item Spline regression \cite{racine} was also considered as a basis for scatterplot clustering applying the following algorithm:
%\item In spline regression a curve $y=s(x)$ is represented as $\mathbf{y}_i=\mathbf{B}_i\mathbf{c}$ where
%\begin{itemize}
%\item $\mathbf{B}_i =\left[ B_{1p}\mathbf{x}_i,B_{2p}\mathbf{x}_i,\dots,B_{Lp}\mathbf{x}_i \right]$ the spline basis matrix and
%\item $\mathbf{c}$ is the vector of spline coefficients.
%\end{itemize}

%\item This suggests the following method (and algorithm) for detecting L--shaped genes based on \textbf{Clustering Spline Coefficients}:
\begin {enumerate}
\item Select genes with significant negative correlation.
\item For each selected gene fit a cubic splines regression model.
\item Obtain a distance matrix between all genes using the $1-\rho$ distance computed on spline coefficients.
\item Perform a hierarchical clustering and
\item Select genes in the \textit{L-shaped cluster(s)}.
\end{enumerate}
\end{itemize}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\section{2x2 complex matrix}
Consider the matrix
\[
  \left[
    \begin{array}{cc}
      \alpha & \beta\\
      \gamma & \delta
    \end{array}
  \right]
\]
The eigenvalues, $\lambda$, are solution of the characteristic equation
\[
  \alpha\delta(1-\lambda)^2 - \beta\gamma = \alpha\delta\lambda^2
  -2\alpha\delta\lambda + \alpha\delta - \beta\gamma = 0
\]
The solution depends only on the value of the product $\alpha\delta$
and we consider only the case $\delta=\alpha$ without loss of
generality.

\begin{align*}
  \lambda &= \frac{2\alpha^2 \pm \sqrt{4\alpha^4 -
            4\alpha^2(\alpha^2 - \beta\gamma)}}{2\alpha^2}\\
  &  = \alpha \pm \sqrt{\beta\gamma}
\end{align*}

There is a complex paire of eigenvalues if and only if $\beta$ and
$\gamma$ have opposite sign. The modulus of the eigenvalues is
\[
  |\lambda| = \sqrt{\alpha^2 + |\beta\gamma|}
\]
There is a paire of complex unit roots when $\alpha^2 + \beta\gamma =
1$ and the roots are smaller than one in modulus when $\alpha^2 + \beta\gamma <
1$
\begin{enumerate}
\item Generating a random 2x2 matrix with a paire of complex unit
  roots:
  \begin{itemize}
  \item Choose random $\alpha$ and $\beta$
  \item Set $\gamma = -\frac{1 - \alpha^2}{\beta}$
  \end{itemize}
\item Generating a random 2x2 matrxi with a paire of roots smalle than
  1 in modulus:
  \begin{itemize}
  \item Choose random $\alpha$ and $\beta$
  \item If $\beta < 0$, choose random $\gamma$ over $]0; -\frac{1 -
      \alpha^2}{\beta}[$,
  \item else choose random $\gamma$ over $]-\frac{1 -
      \alpha^2}{\beta}; 0[$
  \end{itemize}
\end{enumerate}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\subsection{Electroweak theory}
\label{ewktheory}
The electroweak interaction is the unified description of two of the four known fundamental interactions of nature: electromagnetism and the weak interaction.
It is based on the gauge group $SU(2)_{L} \times SU(1)_{Y}$, in which $L$ is the left-handed fields and $Y$ is the weak hypercharge~\cite{Langacker:2009my}.
It follows the Lagrangian of
\begin{equation} \label{eq:Lew}
	L_{EW} = L_{gauge} + L_{Higgs} + L_{fermion} + L_{Yukawa}
\end{equation}

$L_{gauge}$ is the \textbf{gauge term} part:
\begin{equation}
	L_{gauge} = -\frac{1}{4} W^{i}_{\mu\nu} W^{\mu\nu i} - \frac{1}{4} B_{\mu\nu} B^{\mu\nu}
\end{equation}
where $W^{i}_{\mu}$ and $B_{\mu}$ present the $SU(2)_{L}$ and $SU(1)_{Y}$ gauge fields respectively, with the corresponding field strength tensors of
\begin{equation}
\begin{split}
	& B_{\mu\nu} = \partial_{\mu} B_{\nu} - \partial_{\nu} B_{\mu} \\
	& W^{i}_{\mu\nu} = \partial_{\mu} W^{i}_{\nu} - \partial_{\nu} W^{i}_{\mu} - g \epsilon_{ijk} W^{j}_{\mu} W^{k}_{\nu}
\end{split}
\end{equation}
In the equations above, $g$ denotes the $SU(2)_{L}$ gauge coupling and $\epsilon_{ijk}$ denotes the totally antisymmetric tensor.
The gauge Lagrangian has three and four -point self interactions of $W^{i}$ that results in triple and quartic gauge boson couplings.

The second term is the \textbf{scaler part}:
\begin{equation} \label{eq:Lhiggs}
	{L}_{Higgs} = \left(D^{\mu}\phi\right)^{\dagger}D_{\mu}\phi - V(\phi)
\end{equation}
where $\phi = \binom{\phi^{+}}{\phi^{0}}$ denotes a complex Higgs scalar,
and $V(\phi)$ is the Higgs potential which is restricted into the form of
\begin{equation} \label{eq:Vhiggs}
	V(\phi) = +\mu^{2}\phi^{\dagger}\phi + \lambda\left(\phi^{\dagger}\phi\right)^{2}
\end{equation}
due to the combination of $SU(2)_{L} \times SU(1)_{Y}$ invariance and renormalizability.
In Eq.~\ref{eq:Vhiggs}, $\mu$ is a mass-dependent parameter and $\lambda$ is the quartic Higgs scalar coupling,
which represents a quartic self-interaction between the scalar fields.
When $\mu^{2} < 0$, there will be spontaneous symmetry breaking (more details in section~\ref{symbreaking}).
To maintain vacuum stability, $\lambda > 0$ is required.
And in Eq.~\ref{eq:Lhiggs}, the gauge covariant derivative is defined as~\cite{Langacker:2009my}s
\begin{equation}
	D_{\mu}\phi = \left(\partial_{\mu} +ig\frac{\tau^{i}}{2}W_{\mu}^{i} + \frac{ig'}{2}B_{\mu}\right)\phi
\end{equation}
in which $\tau^{i}$ represents the Pauli matrices, and $g'$ is the $U(1)_{Y}$ gauge coupling.
The square of the covariant derivative results in three and four -point interactions between the gauge and scalar fields.

The third term of the Lagrangian is the \textbf{fermion part}
\begin{equation} \label{eq:Lfermion}
\begin{split}
  	{L}_{fermion} = \sum_{m=1}^{F} & ( \bar{q}_{mL^{i}}^{0}\gamma_{\mu}D_{\mu}q_{mL}^{0} + \bar{l}_{mL^{i}}^{0}\gamma_{\mu}D_{\mu}l_{mL}^{0} + \bar{u}_{mR^{i}}^{0}\gamma_{\mu}D_{\mu}u_{mR}^{0} \\
  	& + \bar{d}_{mR^{i}}^{0}\gamma_{\mu}D_{\mu}d_{mR}^{0} + \bar{e}_{mR^{i}}^{0}\gamma_{\mu}D_{\mu}e_{mR}^{0} + \bar{\nu}_{mR^{i}}^{0}\gamma_{\mu}D_{\mu}\nu_{mR}^{0})
\end{split}
\end{equation}
In Eq.~\ref{eq:Lfermion}, m is the family index of fermions, F is the number of families.
The subscripts $L (R)$ stand for the left (right) chiral projection $\psi_{L(R)} \equiv \left(1 \mp \gamma_{5} \right) \psi/2$.
\begin{equation}
	q_{mL}^{0} = \binom{u_{m}^{0}}{d_{m}^{0}}_{L}   \qquad    l_{mL}^{0} = \binom{\nu_{m}^{0}}{e_{m}^{-0}}_{L}
\end{equation}
are the $SU(2)$ doublets of left-hand quarks and leptons, while
$u_{mR}^{0}$, $d_{mR}^{0}$, $e_{mR}^{-0}$ and $\nu_{mR}^{0}$ are the right-hand singlets.

The last term in Eq.~\ref{eq:Lew} is \textbf{Yukawa term}
\begin{equation}
\begin{split}
	{L}_{Yukawa} =& -\sum_{m,n=1}^{F} [\Gamma_{mn}^{u}\bar{q}_{mL}^{0}\widetilde{\phi}u_{nR}^{0} + \Gamma_{mn}^{d}\bar{q}_{mL}^{0}\phi d_{nR}^{0} \\
	& + \Gamma_{mn}^{e}\bar{l}_{mn}^{0}\phi e_{nR}^{0} + \Gamma_{mn}^{\nu}\bar{l}_{mL}^{0}\widetilde{\phi}\nu_{nR}^{0}]+h.c.
\end{split}
\end{equation}
the matrices $\Gamma_{mn}$ refer to the Yukawa couplings between single Higgs doublet ($\phi$) and the various flavors of quarks (m) and leptons (n).


\section{Utility Functions}
With the control panel defined and explained, let us move over to some utility functions. Functions that can be used in all kinds of calculations, which we might need more often. In general it
concerns functions like calculating the gradient, the lacplacian or interpolation.

\subsection{Gradients}
Let us define the gradient in the $x, y$ and $z$ directions. The functions can be found in \autoref{alg:gradient x}, \autoref{alg:gradient y} and \autoref{alg:gradient z}. We use these functions
in various other algorithms as the gradient (also known as derivative) is often used in physics. It denotes the rate of change, how much something changes over time. Velocity for instance denotes
how far you move in a given time. Which is a rate of change, how much your distance to a given point changes over time.

In \autoref{alg:gradient z} $a.dimensions$ is the attribute that tells us how deeply nested the array $a$ is. If the result is $1$ we have just a normal array, if it is $2$ we have a double array
(an array at each index of the array) which is also called a matrix and if it is $3$ we have a triple array. We need this because we have a one-dimensional case, for when we do not use multiple
layers and a three-dimensional case for when we do use multiple layers. This distinction is needed to avoid errors being thrown when running the model with one or multiple layers.

This same concept can be seen in \autoref{alg:gradient x} and \autoref{alg:gradient y}, though here we check if $k$ is defined or \texttt{NULL}. We do this as sometimes we want to use this
function for matrices that does not have the third dimension. Hence we define a default value for $k$ which is \texttt{NULL}. \texttt{NULL} is a special value in computer science. It represents
nothing. This can be useful sometimes if you declare a variable to be something but it is referring to something that has been deleted or it is returned when some function fails. It usually
indicates that something special is going on. So here we use it in the special case where we do not want to consider the third dimension in the gradient. We also use forward differencing
(calculating the gradient by taking the difference of the cell and the next/previous cell, multiplied by $2$ to keep it fair) in \autoref{alg:gradient y} as that gives better results for the
calculations we will do later on.

\begin{algorithm}[hbt]
    \caption{Calculating the gradient in the $x$ direction}
    \label{alg:gradient x}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Matrix (double array) $a$, first index $i$, second index $j$, third index $k$ with default value \texttt{NULL}}
    \Output{Gradient in the $x$ direction}
    \eIf{$k = \texttt{NULL}$}{
        $grad \leftarrow \frac{a[i, (j + 1)\text{ mod } nlon] - a[i, (j - 1) \text{ mod } nlon]}{\delta x[i]}$ \;
    }{
        $grad \leftarrow \frac{a[i, (j + 1)\text{ mod } nlon, k] - a[i, (j - 1) \text{ mod } nlon, k]}{\delta x[i]}$ \;
    }
    \Return{$grad$} \;
\end{algorithm}

\begin{algorithm}[hbt]
    \caption{Calculating the gradient in the $y$ direction}
    \label{alg:gradient y}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Matrix (double array) $a$, first index $i$, second index $j$, third index $k$ with default value \texttt{NULL}}
    \Output{Gradient in the $y$ direction}
    \eIf{$k = \texttt{NULL}$}{
        \uIf{$i == 0$}{
            $grad \leftarrow 2 \frac{a[i + 1, j] - a[i, j]}{\delta y}$ \;
        }\uElseIf{$i = nlat - 1$}{
            $grad \leftarrow 2 \frac{a[i, j] - a[i - 1, j]}{\delta y}$ \;
        }\uElse{
            $grad \leftarrow \frac{a[i + 1, j] - a[i - 1 j]}{\delta y}$ \;
        }
    }{
        \uIf{$i = 0$}{
            $grad \leftarrow 2 \frac{a[i + 1, j, k] - a[i, j, k]}{\delta y}$ \;
        }\uElseIf{$i = nlat - 1$}{
            $grad \leftarrow 2 \frac{a[i, j, k] - a[i - 1, j, k]}{\delta y}$ \;
        }\uElse{
            $grad \leftarrow \frac{a[i + 1, j] - a[i - 1 j]}{\delta y}$ \;
        }
    }
    \Return $grad$ \;
\end{algorithm}

\begin{algorithm}[hbt]
    \caption{Calculating the gradient in the $z$ direction}
    \label{alg:gradient z}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Array $a$, array $p_z$, index $k$}
    \Output{Gradient in the $z$ direction}
    $nlevels \leftarrow p_z.length$ \;
    \uIf{$k = 0$}{
        $grad \leftarrow \frac{a[k + 1] - a[k]}{p_z[k + 1] - p_z[k]}$ \;
    }\uElseIf{$k = nlevels - 1$}{
        $grad \leftarrow \frac{a[k] - a[k - 1]}{p_z[k] - p_z[k - 1]}$ \;
    }\uElse{
        $grad \leftarrow \frac{a[k + 1] - a[k - 1]}{p_z[k + 1] - p_z[k - 1]}$ \;
    }

    \Return $-grad$ \;
\end{algorithm}

\subsection{Laplacian Operator} \label{sec:laplace}
The Laplacian operator ($\nabla^2$, sometimes also seen as $\Delta$) has two definitions, one for a vector field and one for a scalar field. The two concepts are not indpendent, a vector field
is composed of scalar fields \cite{vectorscalarfields}. Let us define a vector field first. A vector field is a function whose domain and range are a subset of the Eucledian $\mathbb{R}^3$ space.
A scalar field is then a function consisting out of several real variables (meaning that the variables can only take real numbers as valid values). So for instance the circle equation
$x^2 + y^2 = r^2$ is a scalar field as $x, y$ and $r$ are only allowed to take real numbers as their values.

With the vector and scalar fields defined, let us take a look at the Laplacian operator. For a scalar field $\phi$ the laplacian operator is defined as the divergence of the gradient of $\phi$
\cite{laplacian}. But what are the divergence and gradient? The gradient is defined in \autoref{eq:gradient} and the divergence is defined in \autoref{eq:divergence}. Here $\phi$ is a vector
with components $x, y, z$ and $\Phi$ is a vector field with components $x, y, z$. $\Phi_1, \Phi_2$ and $\Phi_3$ refer to the functions that result in the corresponding $x, y$ and $z$ values
\cite{vectorscalarfields}. Also, $i, j$ and $k$ are the basis vectors of $\mathbb{R^3}$, and the multiplication of each term with their basis vector results in $\Phi_1, \Phi_2$ and $\Phi_3$
respectively. If we then combine the two we get the Laplacian operator, as in \autoref{eq:laplacian scalar}.

\begin{subequations}
    \begin{equation}
        \label{eq:gradient}
        \text{grad } \phi = \nabla \phi = \frac{\delta \phi}{\delta x}i + \frac{\delta \phi}{\delta y}j + \frac{\delta \phi}{\delta z}k
    \end{equation}
    \begin{equation}
        \label{eq:divergence}
        \text{div} \Phi = \nabla \cdot \Phi = \frac{\delta \Phi_1}{\delta x} + \frac{\delta \Phi_2}{\delta y} + \frac{\delta \Phi_3}{\delta z}
    \end{equation}
    \begin{equation}
        \label{eq:laplacian scalar}
        \nabla^2 \phi = \nabla \cdot \nabla \phi = \frac{\delta^2 \phi}{\delta x^2} + \frac{\delta^2 \phi}{\delta y^2} + \frac{\delta^2 \phi}{\delta z^2}
    \end{equation}
\end{subequations}

For a vector field $\Phi$ the Laplacian operator is defined as in \autoref{eq:laplacian vector}. Which essential boils down to taking the Laplacian operator of each function and multiply it by
the basis vector.

\begin{equation}
    \label{eq:laplacian vector}
    \nabla^2 \Phi = (\nabla^2 \Phi_1)i + (\nabla^2 \Phi_2)j + (\nabla^2 \Phi_3)k
\end{equation}

The code can be found in \autoref{alg:laplacian}. $\Delta_x$ and $\Delta_y$ in \autoref{alg:laplacian} represents the calls to \autoref{alg:gradient x} and \autoref{alg:gradient y}
respectively.

\begin{algorithm}[hbt]
    \caption{Calculate the laplacian operator over a matrix a}
    \label{alg:laplacian}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{A matrix (double array) a}
    \Output{A matrix (double array) with results for the laplacian operator for each element}
    \eIf{$a.dimensions = 2$}{
        \For{$lat \leftarrow 1$ \KwTo $nlat - 1$}{
            \For{$lon \leftarrow 0$ \KwTo $nlon$}{
                $output[lat, lon] \leftarrow \frac{\Delta_x(a, lat, (lon + 1) \text{ mod } nlon) - \Delta_x(a, lat, (lon - 1) \text{ mod } nlon)}{\delta x[lat]} + \frac{\Delta_y(a, lat + 1, lon) -
                \Delta_y(a, lat - 1, lon)}{\delta y}$\;
            }
        }
    }{
        \For{$lat \leftarrow 1$ \KwTo $nlat - 1$}{
            \For{$lon \leftarrow 0$ \KwTo $nlon$}{
                \For{$k \leftarrow 0$ \KwTo $nlevels - 1$}{
                    $output[lat, lon, k] \leftarrow \frac{\Delta_x(a, lat, (lon + 1) \text{ mod } nlon, k) - \Delta_x(a, lat, (lon - 1) \text{ mod } nlon, k)}{\delta x[lat]} + \frac{\Delta_y(a,
                    lat + 1, lon, k) - \Delta_y(a, lat - 1, lon, k)}{\delta y} + \frac{\Delta_z(a, lat, lon, k + 1) - \Delta_z(a, lat, lon, k + 1)}{2\delta z[k]}$\;
                }
            }
        }
    }

    \Return{$ouput$} \;
\end{algorithm}

\subsection{Divergence}
As we expect to use the divergence operator more often throughout our model, let us define a seperate function for it in \autoref{alg:divergence}. $\Delta_x$ and $\Delta_y$ in
\autoref{alg:divergence} represents the calls to \autoref{alg:gradient x} and \autoref{alg:gradient y} respectively. We do the multiplication with the velocity vectors $u, v$ and $w$ here already,
as we expect that we might use it in combination with the divergence operator more frequently. What those vectors are and represent we will discuss in \autoref{sec:momentum}.

\begin{algorithm}[!hbt]
    \caption{Calculate the result of the divergence operator on a vector}
    \label{alg:divergence}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{A matrix (triple array) $a$, pressure field $p_z$}
    \Output{A matrix (triple array) containing the result of the divergence operator taken over that element}
    \For{$i \leftarrow 0$ \KwTo $a.length$}{
        \For{$j \leftarrow 0$ \KwTo $a[i].length$}{
            \For{$k \leftarrow 0$ \KwTo $a[i, j].length$}{
                $output[i, j, k] \leftarrow \Delta_x(au, i, j, k) + \Delta_y(av, i, j, k) + \Delta_z(aw[i, j, :], p_z, k)$ \;
            }
        }
    }
    \Return{$output$} \;
\end{algorithm}

\subsection{Interpolation} \label{sec:interpolation}
Interpolation is a form of estimation, where one has a set of data points and desires to know the values of other data points that are not in the original set of data points\cite{interpolation}.
Based on the original data points, it is estimated what the values of the new data points will be. There are various forms of interpolation like linear interpolation, polynomial interpolation
and spline interpolation. The CLAuDE model uses linear interpolation which is specified in \autoref{eq:interpolation}. Here $z$ is the point inbetween the known data points $x$ and $y$.
$\lambda$ is the factor that tells us how close $z$ is to $y$ in the interval $[0, 1]$. If $z$ is very close to $y$, $\lambda$ will have the value on the larger end of the interval, like 0.9.
Whereas if $z$ is close to $x$ then $\lambda$ will have a value on the lower end of the interval, like 0.1.

\begin{equation}
    \label{eq:interpolation}
    z = (1 - \lambda)x + \lambda y
\end{equation}

\subsection{3D smoothing} \label{sec:3dsmooth}
As you can imagine the temperature, pressure and the like vary quite a lot over the whole planet. Which is something that we kind of want but not really. What we really want is to limit how
much variety we allow to exist. For this we are going to use Fast Fourier Transforms, also known as FFTs. A Fourier Transform decomposes a wave into its frequences. The fast bit comes from the
algorithm we use to calculate it. This is because doing it via the obvious way is very slow, in the order of $O(n^2)$ (for what that means, please visit \autoref{sec:runtime}). Whereas if we use
the FFT, we reduce the running time to $O(n\log(n))$. There are various ways to calculate the FFT, but we use the Cooley–Tukey algorithm \cite{fft}. To explain it, let us first dive into a normal
Fourier Transform.

The best way to explain what a Fourier Transform does is to apply it to a sound. Sound is vibrations travelling through the air that reach your air and make the inner part of your air vibrate.
If you plot the air pressure reaching your air versus the time, the result will have the form of a sinoidal wave. However, it is only a straight forward sinoidal wave (as if you plotted the
$\cos$ function) if the tone is pure. That is often not the case, and sounds are combinations of tones. This gives waves that are sinoidal but not very alike to the $\cos$ function. The FT will
transform this "unpure" wave and splits them up into a set of waves that are all of pure tone. To do that we need complex numbers which are explained here \autoref{sec:complex}.

With that explanation out of the way, we now know that with Euler's formula (\autoref{eq:euler}) we can rotate on the complex plane. If we rotate one full circle per second, the formula changes
to \autoref{eq:euler rotate}, as the circumference of the unit circle is $2\pi$ and $t$ is in seconds. This rotates in the clock-wise direction, but we want to rotate in the clockwise direction,
so we need to add a $-$ to the exponent. If we also want to control how fast the rotation happens (which is called the frequency) then we change the equation to \autoref{eq:euler freq}. Note that
the frequency unit is $Hz$ which is defined as $s^{-1}$, which means that a frequency of $10 Hz$ means 10 revolutions per second. Now we get our wave which we call $g(t)$ and plonk it in front
of the equation up until now. Which results in \autoref{eq:euler wave}. Visually, this means that we take the sound wave and wrap it around the origin. This might sound strange at first but bear
with me. If you track the center of mass (the average of all the points that form the graph) you will notice that it hovers around the origin. If you now change the frequency of the rotation ($f$)
you will see that the center of mass moves a bit, usually around the origin. However, if the frequency of the rotation matches a frequency of the wave, then the center of mass is suddenly a
relatively long distance away from the origin. This indicates that we have found a frequency that composes the sound wave. Now how do we track the center of mass? That is done using integration,
as in \autoref{eq:euler int}. Now to get to the final form, we forget about the fraction part. This means that the center of mass will still hover around the origin for the main part of the
rotation, but has a huge value for when the rotation is at the same frequency as one of the waves in the sound wave. The larger the difference between $t_2$ and $t_1$, the larger the value of the
Fourier Transform. The final equation is given in \autoref{eq:ft}.

\begin{subequations}
    \begin{equation}
        \label{eq:euler}
        e^{ix} = \cos(x) + i\sin(x)
    \end{equation}
    \begin{equation}
        \label{eq:euler rotate}
        e^{2\pi it}
    \end{equation}
    \begin{equation}
        \label{eq:euler freq}
        e^{-2\pi ift}
    \end{equation}
    \begin{equation}
        \label{eq:euler wave}
        g(t)e^{-2\pi ift}
    \end{equation}
    \begin{equation}
        \label{eq:euler int}
        \frac{1}{t_2 - t_1}\int^{t_2}_{t_1}g(t)e^{-2\pi ift}
    \end{equation}
    \begin{equation}
        \label{eq:ft}
        \hat{g}(f) = \int^{t_2}_{t_1}g(t)e^{-2\pi ift}
    \end{equation}
\end{subequations}

These Fourier Transforms have the great property that if you add them together you still have the relatively large distances of the center of mass to the origin at the original frequencies. It
is this property that enables us to find the frequencies that compose a sound wave.

Before moving on we first need to discuss some important properties of FTs. Actually, these properties only apply to complex roots of unity (the $e^{2\pi ift}$ part is a complex root of unity).
Due to the FT only being a coefficient $g(t)$ in front of a complex root of unity all these properties apply to FTs as well. Let us first start with the cancellation lemma as described in
\autoref{lemma:cancellation} \cite{cancellation}. Note that $\omega^k_n = e^{\frac{2\pi k}{n}}$ which is very similar to the form we discussed previously if you realise that $f = \frac{1}{T}$.
So replace $k$ by $t$ and $n$ by $T = \frac{1}{f}$ and you get the form we have discussed earlier.

\begin{lemma}[Cancellation Lemma] \label{lemma:cancellation}
    $\omega^{dk}_{dn} = \omega^k_n$, for all $k \geq 0, n \geq 0$ and $d > 0$.
\end{lemma}

With that we also need to talk about the halving lemma (\autoref{lemma:halving}). This means that $(\omega^{k + \frac{n}{2}}_n)^2 = (\omega^k_n)^2$.

\begin{lemma}[Halving Lemma] \label{lemma:halving}
    If $n > 0$ is even, then the squares of the $n$-complex $n^{\text{th}}$ roots of unity are the $\frac{n}{2}$-complex $\frac{n}{2}^{\text{th}}$ roots of unity.
\end{lemma}

Now that we know what a Fourier Transform is, we need to make it a Fast Fourier Transform, as you can imagine that calculating such a thing is quite difficult. Some smart people have thought
about this and they came up with quite a fast algorithm, the Cooley-Tukey algorithm \cite{fft}, named after the people that thought of it. They use something we know as a Discrete Fourier
Transform which is described by \autoref{eq:dft}. Here $N$ is the total amount of samples from the continuous sound wave. This means that $0 \leq k \leq N - 1$ and $0 \leq n \leq N - 1$.

Now with the DFT out of the way we can discuss the algorithm. Before we do, we assume that we have an input of $n$ elements, where $n$ is an exact power of $2$. Meaning $n = 2^k$ for some
integer $k$.

\begin{algorithm}
    \caption{One dimensional Fast Fourier Transformation}
    \label{alg:FFT}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{array $A$}
    \Output{array $B$ with length $A.length - 1$ containing the DFT}
    $n \leftarrow A.length$ \;
    \uIf{$n = 1$}{
        \Return{$A$} \;
    }
    $\omega_n \leftarrow e^{\frac{2\pi i}{n}}$ \;
    $\omega \leftarrow 1$ \;
    $a^0 \leftarrow (A[0], A[2], \dots, A[n - 2])$ \;
    $a^1 \leftarrow (A[1], A[3], \dots, A[n - 1])$ \;
    $y^0 \leftarrow $ \texttt{FFT}($a^0$) \;
    $y^1 \leftarrow $ \texttt{FFT}($a^1$) \;
    \For{$k \leftarrow 0$ \KwTo $\frac{n}{2} - 1$}{
        $B[k] \leftarrow y^0[k] + \omega y^1[k]$ \;
        $B[k + \frac{n}{2}] \leftarrow y^0[k] - \omega y^1[k]$ \;
        $\omega \leftarrow \omega \omega_n$ \;
    }
    \Return{B}
\end{algorithm}

There is just this one problem we have, the algorithm in \autoref{alg:FFT} can only handle one dimension, and we need to support multidimensional arrays. So let us define a multidimensional FFT
first in \autoref{eq:NFFT}. Here $N$ and $M$ are the amount of indices for the dimensions, where $M$ are the amount of indices for the first dimension and $N$ are the amount of indices for the
second dimension. This can of course be extended in the same way for a $p$-dimensional array.

\begin{equation}
    \label{eq:NFFT}
    X_{k,l} = \sum_{m = 0}^{M - 1}\sum_{n = 0}^{N - 1} x_{m, n}e^{-2i\pi(\frac{mk}{M} + \frac{nl}{N})}
\end{equation}

It is at this point that the algorithm becomes very complicated. Therefore I would like to invite you to use a library for these kinds of calculations, like Numpy \cite{numpy} for Python. If you
really want to use your own made version, or if you want to understand how the libraries sort of do it, then you may continue. I still advise you to use a library, as other people have made the
code for you.

We can calculate the result of such a multidimensional FFT by computing one-dimensional FFTs along each dimension in turn. Meaning we first calculate the result of the FFT along one dimension,
then we do that for the second dimension and so forth. Now the order of calculating the FFTs along each dimension does not matter. So you can calculate the third dimension first, and the first
dimension after that if you wish. This has the advantage that we can program a generic function without keeping track of the order of the dimensions. Now, the code is quite complicated but it
boils down to this: calculate the FFT along one dimension, then repeat it for the second dimension and multiply the two together and keep repeating that for all dimensions.

With that out of the way, we need to create a smoothing operation out of it. We do this in \autoref{alg:smooth}. Keep in mind that \texttt{FFT} the call is to the
multidimensional Fast Fourier Transform algorithm, \texttt{IFFT} the call to the inverse of the multidimensional Fast Fourier Transform algorithm (also on the TODO list) and that the $int()$
function ensures that the number in brackets is an integer. Also note that the inverse of the FFT might give complex answers, and we only want real answers which the $.real$ ensures. We only
take the real part and return that. $v$ is an optional parameter with default value $0.5$ which does absolutely nothing in this algorithm.

\begin{algorithm}
    \caption{Smoothing function}
    \label{alg:smooth}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Array $a$, smoothing factor $s$, vertical smoothing factor $v \leftarrow 0.5$}
    \Output{Array $A$ with less variation}
    $nlat \leftarrow a.length$ \;
    $nlon \leftarrow a[0].length$ \;
    $nlevels \leftarrow a[0][0].length$ \;
    $temp \leftarrow \texttt{FFT}(a)$ \;
    $temp[int(nlat s):int(nlat(1 - s)),:,:] \leftarrow 0$ \;
    $temp[:,int(nlon s):int(nlon(1 - s)),:] \leftarrow 0$ \;
    $temp[:,:,int(nlevels v):int(nlevels(1 - v))] \leftarrow 0$ \;
    \Return $\texttt{IFFT}(temp).real$ \;
\end{algorithm}


\subsection{Continuum Procedure}\label{sec:continuum}
The starting point is a contact interaction\footnote{This derivation generalizes to a tower of contact interactions where $C(\Lambda)$ is replaced by $\sum_n C_{2n}(\Lambda) p^{2n}$ \cite{Kaplan:1998we,Beane:2003da} and dimensional regularization is used to absorb power-law divergencies.} such that the tree amplitude in the center of mass frame is given by
\begin{equation}
    \mathcal A(\Lambda) = + i C(\Lambda)
\end{equation}
where $p$ denotes the relative momentum of incoming nucleons and the interaction strengths $ C(\Lambda)$ depend on the regulator $\Lambda$ and carry dimension-dependent units.
The scattering amplitude is given by the bubble sum depicted in \Figref{bubbleSum}.

\begin{figure}[ht!]
\center
\includegraphics[width=.675\columnwidth]{figure/bubbleSum.pdf}
\hfill
\includegraphics[width=.275\columnwidth]{figure/I0.pdf}
\caption{(Left) The bubble sum. Each line represents a propagator, each vertex represents $-i C(\Lambda)$, and the bubble is given by $I_D$.
(Right) The single loop diagram needed to calculate $I_D$ in the bubble sum.
\label{fig:bubbleSum}}
\end{figure}

This bubble sum is a geometric series and, restricting our attention to the contact interaction causes all other partial wave than the S-wave to vanish.
This restriction gives for the standard on-shell $T$-matrix
\begin{equation}\label{eq:T matrix}
T_{D\l}(p, \Lambda) = \delta_{\l 0} \frac{C(\Lambda)}{1-I_D(p,\Lambda) C(\Lambda)},
\end{equation}
where $p$ is the relative on-shell momentum, $\Lambda$ the regularization scale.
The physical result for the T-matrix is recovered once the parameter $C$ is chosen such that one can remove the regularization scale---in the limit of $\Lambda \to \infty$ for a hard momentum cutoff, for example.

$I_D(p,\Lambda)$ is a $D$-dependent function that arises from integrating the loop shown in the right panel of \Figref{bubbleSum},
\begin{align}
    I_D(p, \Lambda)
    &=-i\int^{\Lambda}
        \frac { \mathrm {d}q_0}{2\pi}\ \frac{\mathrm { d } ^ { D } \vec{ q } } { (2\pi)^ { D } }
        \left( \frac { i } { \frac{E}{2} + q _ { 0 } - \frac{\vec{q}^2}{2m_1} + i \epsilon } \right)
        \left( \frac { i } { \frac{E}{2} - q _ { 0 } - \frac{\vec{q}^2}{2m_2} + i \epsilon } \right)
    \label{eq:I0 in two particle language}\\
    &=\frac{\Omega_D}{(2\pi)^D}\int^{\Lambda}  \mathrm { d } q \ q^{D-1}\left[\PV \left( \frac { 1 } { E - \frac{\vec{q}^2}{2\mu} } \right)
-i\frac{\pi \mu}{q}\delta(q-\sqrt{2 \mu E})\right]
    \label{eq:I0 in relative coordinates}
    \\
    &=\frac{\Omega_D}{(2\pi)^2}\frac{2\mu}{L^{D-2}}\int^{\Lambda L/2\pi}  \mathrm { d } n \ n^{D-1}\left[\PV \left( \frac { 1 } { \left(\frac{pL}{2\pi}\right)^2 - n^2 } \right)
-i\frac{\pi^2}{L n}\delta\left(\frac{2\pi}{L}n -p\right)\right]
    \label{eq:I0}
\end{align}
where $\PV$ refers to Principal (Cauchy) Value, we have used the on-shell condition $2\mu E=p^2$, and the geometric factor
\begin{equation}
\Omega_D=\frac{2\pi^{D/2}}{\Gamma(D/2)}=
    \begin{cases}
    	2       &   (D=1)\\
		2\pi    &   (D=2)\\
        4\pi    &   (D=3)
    \end{cases}\ ,
\end{equation}
accounts for the angular integration in $D$ dimensions.

Because we are focusing on the contact interaction, we can restrict our attention to the $s$-wave, $\l=0$.
Dropping the $\l$ dependence in \eqref{on-shell-T}, the momentum-dependent $T$-matrix is related to the phase shift when
\begin{equation}\label{eq:spherical FD}
    \F_{D}(p)
    \equiv
    \begin{cases}
        p/2     & (D=1)\\
        1       & (D=2)\\
        \pi/p   & (D=3)\\
        \vdots  & \vdots
\end{cases}
\end{equation}
is a dimension-dependent kinematic factor determined by requiring the imaginary parts of the $T$-matrix \eqref{T matrix} from the bubble sum \eqref{I0 in relative coordinates} exactly matches the imaginary part of the amplitude \eqref{on-shell-T}.
This fixes the coefficients $C(\Lambda)$ as a function of the scattering data,
\begin{equation}\label{eq:IV pole}
    \frac{\mu}{2 \F_{D}(p)}\left(\cot \delta_{D}(p) - i\right)
    =
    \lim\limits_{\Lambda \to \infty} \left[ I_D(p, \Lambda) - \frac{1}{C(\Lambda)} \right].
\end{equation}

In a finite volume, the energy eigenstates $E$ appear at poles of the $T$-matrix, so that
\begin{equation}\label{eq:FV pole}
    \frac{1}{2\mu E C_\FV(\Lambda) } - I_{D, \FV}(\sqrt{2\mu E}, \Lambda) = 0
\end{equation}
and the infinite-volume integral $I_D$ has been replaced by the matching finite-volume sum which introduces another scale $L$,
\begin{align}
I_{D,\FV}(\sqrt{2\mu E}, \Lambda)
    &=-i\int \frac { \mathrm {d}q_0}{2\pi} \frac{1}{L^D}\sum_{\vec{q}}^{q < \Lambda} \left( \frac { i } { \frac{E}{2} + q _ { 0 } - \frac{\vec{q}^2}{2m_1} + i \epsilon } \right) \left( \frac { i } { \frac{E}{2} - q _ { 0 } - \frac{\vec{q}^2}{2m_2} + i \epsilon } \right)
    \\
    \label{eq:I0 FV}
    &=\frac{1}{L^D}\sum_{\vec{q}}^{q < \Lambda} \frac { 1 } { E - \frac{\vec{q}^2}{2\mu} }
    =\frac{2\mu}{(2\pi)^2 L^{D-2}} \sum_{\vec{n}}^{n < \frac{\Lambda L}{2\pi}} \frac{1}{x-n^2}
    &
    x &= \frac{2\mu E L^2}{4\pi^2}
    \, .
\end{align}
Combining the infinite-volume and finite-volume relations \eqref{IV pole} and \eqref{FV pole} yields
\begin{equation}\label{eq:spherical zeta}
    \frac{\mu}{2\F_{D}(\sqrt{2\mu E})}(\cot\delta_{D}(\sqrt{2\mu E})-i)
    =
    \lim\limits_{\Lambda \to \infty} \left[ I_D(\sqrt{2\mu E}) - I_{D,\FV}(\sqrt{2\mu E}) \right] \, ,
\end{equation}
the finite-volume quantization condition.
Note that both equations are explicitly evaluated for the same interactions $C_\FV(\Lambda) = C(\Lambda)$ independent of the volume $L$ and using the same regulator.
Furthermore \eqref{spherical zeta} is only valid if evaluated at momenta corresponding to finite-volume eigenenergies $E$.

Plugging our results for the integrals in, one finds
\begin{multline}
    \frac{1}{2\F_{D}(\sqrt{2\mu E})}\left(\cot \delta_{D}(\sqrt{2\mu E}) - i\right)
    =\\
    \frac{2}{(2\pi)^2 L^{D-2}}
    \lim\limits_{\Lambda \to \infty}
    \left[
    	\left(\mathcal{P}\int_{\vec{n}} - \sum_{\vec{n}}\right) \frac{1}{x-n^2} +
		\frac{-i \pi^2\Omega_D}{L} \int \mathrm{d}n\ n^{D-2} \delta\left(\frac{2\pi}{L}n - \sqrt{2\mu E}\right)
	\right]
\end{multline}
where both the sum and integral are cut off by a restriction on the magnitude of $n$, $n^2 < (\Lambda L / 2\pi)^2$,
The principle value integration implicitly carries a factor of $\Omega_D n^{D-1}$ (see \eqref{I0}).
The imaginary part on the left hand side exactly cancels the last term on the right when $E\ge0$.  When $E<0$ the last term on the RHS vanishes and so we have
%--demanding this cancellation is how the kinematic factor $\F_D$ of the $T$ matrix is determined----and we are left with
\begin{multline}\label{eq:general luscher}
   \frac{1}{2\F_{D}(\sqrt{2\mu E})}  \left( \cot \delta_{D}(p)-i\theta(-E)\right)
    =
   \frac{2}{(2\pi)^2 L^{D-2}}
    \lim\limits_{\Lambda \to \infty}\left(\sum_{\vec{n}}-\mathcal{P}\int_{\vec{n}}\right) \frac{1}{n^2-x}\\
    \implies
      \cot \delta_{D}(p)= \frac{\F_{D}(\sqrt{2\mu E})}{\pi^2 L^{D-2}}\left[
    \lim\limits_{\Lambda \to \infty}\left(\sum_{\vec{n}}-\mathcal{P}\int_{\vec{n}}\right) \frac{1}{n^2-x}\right]
    +i\theta(-x)
    \ ,
\end{multline}
with $x$ as in \eqref{I0 FV}, $\theta(x)$ is the heavyside function, and we switched the sign of the sum and integral as well as the sign of the denominator.  In the second line above we moved the term proportional to the $\theta(-E)$ to the RHS.
Because we cut off the sum and the integral in exactly the same way, in dimensions where $I_D$ diverges with $\Lambda$, the divergence cancels against the divergence in the sum.
Let $N=\Lambda L/\pi$.
Then, with a finite cutoff on magnitude $N/2$, we define
\begin{equation}\label{eq:spherical cutoff S}
    S^{\spherical N}_D(x) =
    \left(\sum_{\vec{n}}- \mathcal{P}\int_{\vec{n}}\right) \frac{1}{n^2-x}
    + i \frac{(2\pi)^D}{4 \F_D\left(\sqrt{x}\right)}\theta(-x)\ ,
\end{equation}
where it was used that $\F_D(p) \sim p^{2-D}$ and the $\spherical$ superscript reminds us that we cut off our sum and integral in a spherical way, based on the magnitude of $n<N/2$.   By performing the principal value integral and taking the limit $N\to \infty$, we recover the usual \Luscher zeta functions,
\begin{equation}\label{eq:spherical S}
    S^\spherical_D(x)
    =
    \lim_{N\goesto\infty} S^{\spherical N}_D(x)
    =
    \lim_{N\rightarrow\infty} \sum_{\vec{n}}^{n < N/2}
    \begin{cases}
     \frac{1}{n^2-x} - \counterterm_3^\spherical \frac{N}{2}& (D=3)\\
     \frac{1}{n^2-x} - 2\pi\log\left(\counterterm_2^\spherical\frac{N}{2}x^{-1/2}\right)& (D=2)\\
    \frac{1}{n^2-x} & (D=1)
     \end{cases}
\end{equation}
where the dimension-dependent coefficients $\counterterm_D^\spherical$ of the counterterms come from the principal value integral; we evaluate the spherical-cutoff integrals and extract these coefficients in \Appref{counterterm/spherical}.\footnote{In higher dimensions there will be additional divergences which cancel, for example, in five spatial dimensions there will be a cubic and linear divergence.
}
Finally, we can write the quantization condition~\eqref{general luscher} using the zeta function~\eqref{spherical S},
\begin{equation}\label{eq:spherical quantization}
    \cot \delta_{D}(p) = \frac{\F_{D}(p)}{\pi^2 L^{D-2}} S^\spherical_D(x)
\end{equation}
where we traded the energy dependence for momentum on the left-hand side.  Our result is consistent with those given in~\Ref{Zhu:2019dho}\footnote{In~\Ref{Zhu:2019dho} the zeta functions~\eqref{spherical cutoff S} were defined \emph{without} the term proportional to the heavyside function.  Thus their zeta functions have a different behavior for $x<0$ as ours.  We note that our definition is more common in the literature.}.
This is the \Luscher finite-volume quantization condition, and finite-volume energy levels calculated in the continuum should be fed through it to produce continuum scattering data.
In three dimensions it is common to move the momentum dependence in $\F_{D}$ to the other side, as $p \cot\delta_{D}(p)$ is what appears in the effective range expansion \eqref{ere}.
In two dimensions, it will prove useful to explicitly separate the logarithmic divergence as $N\to\infty$ from the logarithmic singularity as $x\to 0$, and we will rearrange this equation and slightly redefine $S^\spherical_2$ as needed in \Secref{2D}.  Finally, the sum in~\eqref{spherical S} can be analytically done in $D=1$, as we will show in \Secref{1D}.

%Derived in the zero-temperature continuum, only cold continuum-extrapolated spectra ought to be fed through the quantization condition \eqref{spherical quantization} to extract continuum phase shifts.

To approach the continuum limit, the authors of \Ref{Lee:2007ae} proposed tuning the interaction until the ground state, when fed through $S^\spherical$, produced the desired amplitude that corresponds to the desired scattering length.
We will show in \Secref{3D} that this procedure induces a momentum dependence in the scattering amplitude sensitive to discretization.
In the next subsection we give a procedure that produces a momentum-independent amplitude as one approaches the continuum, and discuss the limiting procedure itself.