\section{Slater-backflow wavefunction implementation details}

For simplicity, consider $N$ identical fermions of the same spin (e.g., up electrons) at spatial locations $\{\bs{r}_1,\bs{r}_2,\dots,\bs{r}_{N}\}$. Then the Slater determinant can be written as
\begin{align}
S=\det M\:,
\end{align}
where each entry in the determinant is an SPO evaluated at a particle position
\begin{align}
M_{ij} = \phi_i(\bs{r}_j)\:.
\end{align}

When backflow transformation is applied to the determinant, the particle coordinates $\bs{r}_i$ that go into the SPOs are replaced by quasi-particle coordinates $\bs{x}_i$:
\begin{align}
M_{ij} = \phi_i(\bs{x}_j)\:, \label{eq:psiM}
\end{align}
where
\begin{align}
\bs{x}_i=\bs{r}_i+\sum\limits_{j=1,j\neq i}^N\eta(r_{ij})(\bs{r}_i-\bs{r}_j)\:. \label{eq:quasi}
\end{align}
$r_{ij}=\vert\bs{r}_i-\bs{r}_j\vert$. The integers i,j label the particle/quasi-particle. There is a one-to-one correspondence between the particles and the quasi-particles, which is simplest when $\eta=0$.

\subsection{Value}
The evaluation of the Slater-backflow wavefunction is almost identical to that of a Slater wavefunction. The only difference is that the quasi-particle coordinates are used to evaluate the SPOs. The actual value of the determinant is stored during the inversion of the matrix $M$ (\verb|cgetrf|$\rightarrow$\verb|cgetri|). Suppose $M=LU$, then $S=\prod\limits_{i=1}^N L_{ii} U_{ii}$. \\

\begin{lstlisting}
// In DiracDeterminantWithBackflow::evaluateLog(P,G,L)
Phi->evaluate(BFTrans->QP, FirstIndex, LastIndex, psiM,dpsiM,grad_grad_psiM);
psiMinv = psiM;
LogValue=InvertWithLog(psiMinv.data(),NumPtcls,NumOrbitals
  ,WorkSpace.data(),Pivot.data(),PhaseValue);
\end{lstlisting}

QMCPACK represents the complex value of the wavefunction in polar coordinates $S=e^Ue^{i\theta}$. Specifically, \verb|LogValue| $U$ and \verb|PhaseValue| $\theta$ are handled separately. In the following, we will consider derivatives of the log value only.

\subsection{Gradient}
To evaluate particle gradient of the log value of the Slater-backflow wavefunction, we can use the $\log\det$ identity in Equation~\ref{eq:logdet}. This identity maps the derivative of $\log\det M$ with respect to a real variable $p$ to a trace over $M^{-1}dM$:
\begin{align}
\frac{\partial}{\partial p}\log\det M = \tr\left( M^{-1} \frac{\partial M}{\partial p} \right) \label{eq:logdet}.
\end{align}

Following Kwon, Ceperley, and Martin~\cite{Kwon1993backflow}, the particle gradient
\begin{align}
G_i^\alpha \equiv \frac{\partial}{\partial r_i^\alpha} \log\det M = \sum\limits_{j=1}^N \sum\limits_{\beta=1}^3 F_{jj}^\beta A_{jj}^{\alpha\beta}\:, \label{eq:grad}
\end{align}
where the quasi-particle gradient matrix
\begin{align}
A_{ij}^{\alpha\beta} \equiv \frac{\partial x_j^\beta}{\partial r_i^\alpha}\:,
\end{align}
and the intermediate matrix
\begin{align}
F_{ij}^\alpha\equiv\sum\limits_k M^{-1}_{ik} dM_{kj}^\alpha\:,
\end{align}
with the SPO derivatives (w.r. to quasi-particle coordinates)
\begin{align}
dM_{ij}^\alpha \equiv \frac{\partial M_{ij}}{\partial x_j^\alpha}\:.
\end{align}
Notice that we have made the name change of $\phi\rightarrow M$ from the notations of ref.~\cite{Kwon1993backflow}. This name change is intended to help the reader associate M with the QMCPACK variable \verb|psiM|.
\begin{lstlisting}
// In DiracDeterminantWithBackflow::evaluateLog(P,G,L)
for(int i=0; i<num; i++) // k in above formula
{
  for(int j=0; j<NumPtcls; j++)
  {
    for(int k=0; k<OHMMS_DIM; k++) // alpha in above formula
    {
      myG(i) += dot(BFTrans->Amat(i,FirstIndex+j),Fmat(j,j));
    }
  }
}
\end{lstlisting}

Equation~\ref{eq:grad} is still relatively simple to understand. The $A$ matrix maps changes in particle coordinates $d\bs{r}$ to changes in quasi-particle coordinates $d\bs{x}$. Dotting A into F propagates $d\bs{x}$ to $dM$. Thus $F\cdot A$ is the term inside the trace operator of Equation~\ref{eq:logdet}. Finally, performing the trace completes the evaluation of the derivative.

\subsection{Laplacian}
The particle Laplacian is given in ref.~\cite{Kwon1993backflow} as
\begin{align}
L_i \equiv \sum\limits_{\beta} \frac{\partial^2}{\partial (r_i^\beta)^2} \log\det M = \sum\limits_{j\alpha} B_{ij}^\alpha F_{jj}^\alpha - \sum\limits_{jk}\sum\limits_{\alpha\beta\gamma} A_{ij}^{\alpha\beta}A_{ik}^{\alpha\gamma}\times\left(F_{kj}^\alpha F_{jk}^\gamma -\delta_{jk}\sum\limits_m M^{-1}_{jm} d2M_{mj}^{\beta\gamma}\right), \label{eq:lap}
\end{align}
where the quasi-particle Laplacian matrix
\begin{align}
B_{ij}^{\alpha} \equiv \sum\limits_\beta \frac{\partial^2 x_j^\alpha}{\partial (r_i^\beta)^2}\:,
\end{align}
with the second derivatives of the single-particles orbitals being
\begin{align}
d2M_{ij}^{\alpha\beta} \equiv \frac{\partial^2 M_{ij}}{\partial x_j^\alpha\partial x_j^\beta}\:.
\end{align}

Schematically, $L_i$ has contributions from three terms of the form $BF, AAFF, and \tr(AA,Md2M)$, respectively. $A, B, M ,d2M,$ and $F$ can be calculated and stored before the calculations of $L_i$. The first $BF$ term can be directly calculated in a loop over quasi-particle coordinates $j\alpha$.
\begin{lstlisting}
// In DiracDeterminantWithBackflow::evaluateLog(P,G,L)
for(int j=0; j<NumPtcls; j++)
  for(int a=0; a<OHMMS_DIM; k++)
    myL(i) += BFTrans->Bmat_full(i,FirstIndex+j)[a]*Fmat(j,j)[a];
\end{lstlisting}
Notice that $B_{ij}^\alpha$ is stored in \verb|Bmat_full|, NOT \verb|Bmat|.

The remaining two terms both involve $AA$. Thus, it is best to define a temporary tensor $AA$:
\begin{align}
{}_iAA_{jk}^{\beta\gamma} \equiv \sum\limits_\alpha A_{ij}^{\alpha\beta} A_{ij}^{\alpha\gamma}\:,
\end{align}
which we will overwrite for each particle $i$. Similarly, define $FF$:
\begin{align}
FF_{jk}^{\alpha\gamma} \equiv F_{kj}^\alpha F_{jk}^\gamma\:,
\end{align}
which is simply the outer product of $F\otimes F$. Then the $AAFF$ term can be calculated by fully contracting $AA$ with $FF$.
\begin{lstlisting}
// In DiracDeterminantWithBackflow::evaluateLog(P,G,L)
for(int j=0; j<NumPtcls; j++)
  for(int k=0; k<NumPtcls; k++)
    for(int i=0; i<num; i++)
    {
      Tensor<RealType,OHMMS_DIM> AA = dot(transpose(BFTrans->Amat(i,FirstIndex+j)),BFTrans->Amat(i,FirstIndex+k));
      HessType FF = outerProduct(Fmat(k,j),Fmat(j,k));
      myL(i) -= traceAtB(AA,FF);
    }
\end{lstlisting}
Finally, define the SPO derivative term:
\begin{align}
Md2M_j^{\beta\gamma} \equiv \sum\limits_m M^{-1}_{jm} d2M_{mj}^\beta\:,
\end{align}
then the last term is given by the contraction of $Md2M$ (\verb|q_j|) with the diagonal of $AA$.
\begin{lstlisting}
for(int j=0; j<NumPtcls; j++)
{
  HessType q_j;
  q_j=0.0;
  for(int k=0; k<NumPtcls; k++)
    q_j += psiMinv(j,k)*grad_grad_psiM(j,k);
  for(int i=0; i<num; i++)
  {
    Tensor<RealType,OHMMS_DIM> AA = dot(
      transpose(BFTrans->Amat(i,FirstIndex+j)),
      BFTrans->Amat(i,FirstIndex+j)
    );
    myL(i) += traceAtB(AA,q_j);
  }
}
\end{lstlisting}

\subsection{Wavefunction parameter derivative}
To use the robust linear optimization method of ref.~\cite{Toulouse2007linear}, the trial wavefunction needs to know its contributions to the overlap and hamiltonian matrices. In particular, we need derivatives of these matrices with respect to wavefunction parameters. As a consequence, the wavefunction $\psi$ needs to be able to evaluate $\frac{\partial}{\partial p} \ln \psi$ and $\frac{\partial}{\partial p} \frac{\mathcal{H}\psi}{\psi}$, where $p$ is a parameter.

When 2-body backflow is considered, a wavefunction parameter $p$ enters the $\eta$ function only (Equation~\ref{eq:quasi}). $\bs{r}$, $\phi$, and $M$ do not explicitly dependent on $p$. Derivative of the log value is almost identical to particle gradient. Namely, Equation~\ref{eq:grad} applies upon the substitution $r_i^\alpha\rightarrow p$.
\begin{align}
\frac{\partial}{\partial p} \ln\det M = \sum\limits_{j=1}^N \sum\limits_{\beta=1}^3 F_{jj}^\beta \left({}_pC_{j}^{\beta}\right)\:,
\end{align}
where the quasi-particle derivatives are stored in \verb|Cmat|
\begin{align}
{}_pC_{i}^{\alpha} \equiv \frac{\partial}{\partial p} x_{i}^{\alpha}\:.
\end{align}

The change in local kinetic energy is a lot more difficult to calculate
\begin{align}
\frac{\partial T_{\text{local}}}{\partial p} = \frac{\partial}{\partial p} \left\{ \left( \sum\limits_{i=1}^N \frac{1}{2m_i} \nabla^2_i \right) \ln \det M \right\} = \sum\limits_{i=1}^N \frac{1}{2m_i} \frac{\partial}{\partial p} L_i\:, \label{eq:dK}
\end{align}
where $L_i$ is the particle Laplacian defined in Equation~\ref{eq:lap}. To evaluate Equation~\ref{eq:dK}, we need to calculate parameter derivatives of all three terms defined in the Laplacian evaluation. Namely $(B)(F)$, $(AA)(FF)$, and $\tr(AA,Md2M)$, where we have put parentheses around previously identified data structures. After $\frac{\partial}{\partial p}$ hits, each of the three terms will split into two terms by the product rule. Each smaller term will contain a contraction of two data structures. Therefore, we will need to calculate the parameter derivatives of each data structure defined in the Laplacian evaluation:
\begin{align}
{}_pX_{ij}^{\alpha\beta} \equiv \frac{\partial}{\partial p} A_{ij}^{\alpha\beta}\:, \\
{}_pY_{ij}^{\alpha} \equiv \frac{\partial}{\partial p} B_{ij}^{\alpha}\:, \\
{}_pdF_{ij}^{\alpha} \equiv \frac{\partial}{\partial p} F_{ij}^{\alpha}\:, \\
{}_{pi}{AA'}_{jk}^{\beta\gamma} \equiv \frac{\partial}{\partial p}  {}_iAA_{jk}^{\beta\gamma}\:, \\
{}_p {FF'}_{jk}^{\alpha\gamma} \equiv \frac{\partial}{\partial p} FF_{jk}^{\alpha\gamma}\:, \\
{}_p {Md2M'}_{j}^{\beta\gamma} \equiv \frac{\partial}{\partial p} Md2M_j^{\beta\gamma}\:.
\end{align}
X and Y are stored as \verb|Xmat| and \verb|Ymat_full| (NOT \verb|Ymat|) in the code. dF is \verb|dFa|. $AA'$ is not fully stored; intermediate values are stored in \verb|Aij_sum| and \verb|a_j_sum|. $FF'$ is calculated on the fly as $dF\otimes F+F\otimes dF$. $Md2M'$ is not stored; intermediate values are stored in \verb|q_j_prime|.


\chapter{Future work}
In this thesis we examined Bayesian neural network exclusively using Markov chain Monte Carlo methods. These methods are often computationally complex, especially in neural network where the posterior is high-dimensional due to number of neural network weights. Therefore it could have been interesting to look at methods that could be more easily scaled to large distributions. Methods such as variational Inference (\cite{VI}) has gained a lot of popularity due to its scalability to more complex models. Unlike Markov chain Monte Carlo methods, variational inference is not an exact method. Instead of sampling directly from the posterior the idea is to have a parametric distribution $q\lr{\boldsymbol{\phi}}$, called the variational distribution, to sample from instead. The parameters of the variational distribution are optimized in such a way that the variational distribution is as close as possible to the true posterior distribution in terms of a measure called the evidence lower bound. In this context it could have been interesting to look into the work by \cite{ADVI}, who has built an automatic differentiation variational inference algorithm, that can automatically optimize the parameters for the variational distribution.
\\
\\
Another interesting way to design Bayesian neural networks is proposed by \cite{blundell2015weight} and is called Bayes-by-backprop, which is a variational inference methods combined with reparamitzation trick to ensure that backpropagation works as we described in section \ref{alg:back_prop}, but over the variational distribution parameters $\boldsymbol{\phi}$, which makes it possible to learn the variational distribution by using optimization algorithms like the ones described in section \ref{sec:gradient_optimization}.
\\
\\
Another popular option worth examining is modelling uncertainty in neural networks by using dropout (\cite{srivastava2014dropout}) to approximate the variational distribution. \cite{mc_dropout} do this by using a type of ensemble learning, where they generate random predictions for test examples by the dropout method and interpret these as coming from a distribution. They call this method Monte Carlo dropout and states, that it produces faster results than both MCMC methods and variational inference.


\section{Overview}
This report contains figures related to the calibration
process of autocorrelator data from Odin-SMR.\\
\newline
The topic examined is:\\
\newline
Calibration of sky signals using linear and quadratic interpolations\\
The calibration of sky signals using two different interpolation
methods are compared.
The results from the two methods are very comparable
(depending a bit on the settings for the quadratic interpolation),
with statistically similar mean and median values
and similar distribution.

\clearpage
\newpage

\section{Calibration of sky signals}
We know from earlier studies that mean values
of calibrated sky signals differs somewhat from
the expected \(T_{sky}\sim0 K\). In this report we examine
if a weighted quadratic interpolation routine
will improve the results.

\subsection{Basics}
The calibration of sky signals is done by:
\begin{equation}
\label{eq:skysig}
T^{'}_{sky}=\frac{c_{sky}-c^{'}_{sky}}{g^{'}}+T_{sky}\approx(\frac{g}{g^{'}}-1)T_{sys},
\end{equation}
where \(T^{'}_{sky}\) is the estimation of the brightness temperature
of the sky signal, \(c_{sky}\approx g(T_{sys}+T_{sky})\approx gT_{sys}\) is the measured sky signal,
 \(c^{'}_{sky}\) is the reference estimated sky signal
which is retrieved using linear/quadratic interpolation using
the measured sky signals around the target \(c_{sky}\),
\(g\) is the true
gain, and \(g^{'}\) is the estimated gain, \(T_{sys}\) is the system
noise temperature.
\subsection{Reference interpolation}
For each channel (\(c_{i}\)) of the autocorrelators
we fit a second degree polynomial,
\begin{equation}
c_{i}(t)=a+bt+ct^{2},
\end{equation}
to the time series of reference measurements (\(c_{i}(t_{0})\),\(c_{i}(t_{1})\),...,\(c_{i}(t_{n})\)). For time zero a weighted quadratic fit can
be estimated as
\begin{equation}
c_{i}(0)=\frac{1}{\Delta}\left|
\begin{array}{ccc}
\sum \frac{c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{2}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\sum \frac{t_{j}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{2}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{3}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\sum \frac{t_{j}^{2}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{3}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{4}c_{i}(t_{j})w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\end{array}
\right|,
\end{equation}

\begin{equation}
\Delta=\left|
\begin{array}{ccc}
\sum \frac{w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{2}w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\sum \frac{t_{j}w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{2}w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{3}w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\sum \frac{t_{j}^{2}w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{3}w^{2}(t_{j})}{\sigma^{2}(t_{j})} &
\sum \frac{t_{j}^{4}w^{2}(t_{j})}{\sigma^{2}(t_{j})}\\
\end{array}
\right|,
\end{equation}
and we use an exponential drop off weighting
\begin{equation}
w(t_{j})=exp\left(-\frac{|t_{j}|}{\lambda} \right),
\end{equation}
where \(\lambda\) controls the weighting,
and \(\sigma(t_{j})\) represents the uncertainty of each measurement
\begin{equation}
\sigma(t_{j})\sim\frac{1}{\sqrt{t_{int}}},
\end{equation}
where \(t_{int}\) is the integration time.

\subsection{Test results}

In this section we compare calibration of sky signals
using linear interpolation and the previously described
quadratic interpolation. Data from six orbits, including around
10000 sky signals, are considered.

\subsubsection{Linear interpolation}
\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4linear.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using linear interpolation.
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4linear.png}
\end{figure}

The upper panel of Figure 1 shows a mean spectrum
of calibrated sky signals from 6 orbits (~10000 sky signals).
It can clearly be seen
that this spectrum deviates from 0 K (approximately the true brightness
temperature of the sky signal).

The lower panel shows some additional statistics.
The standard deviation of the spectrum is around 2 K,
which agrees well with the expected theoretical noise level
(\(\Delta T =T_{sys}/\sqrt{B*\tau} \approx 3000 K / \sqrt{2MHz*0.8s}=2.37 K\)).
The percentiles (for example the 25 and 75) are fairly evenly
displaced from the 50\% percentile (median),
which means that the errors are at least close to Gaussian
distributed.
The median has the same pattern as the mean (which is hard to see
due to the scale).

The reason for the median deviation from 0 K is most likely due to
that the gain does not vary
completely linearly with time between three sky signals
measurements.

\subsubsection{Quadratic interpolation setup}
\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_dem.png}\\
\caption{The upper panel shows mean of band1 of level1a data
from several scans.
The middle panel shows a zoom in of the black dots in the upper panel,
and the black square is the fitted value
(the fit is performed for each channel).
The lower panel shows the corresponding weights (x=8) of each measurement
in the fit. }
\label{fig:study4_dem.png}
\end{figure}

The quadratic fit of a reference measurement at time \(t\)
is done in the following way (see Figure 2).
We first find out the time-interval \(\Delta T\) of the scan that
the measurement belongs to.
In the fit, we then consider all reference measurements
that where performed during the time interval
\(t-\Delta T\) to \(t+\Delta T\).
The weighting parameter \(\lambda\) is then given
the value  \(\Delta T /x\), where we tested the performance
of several values of x (4,8,16,32,64,and 128).

\clearpage
\newpage
\subsubsection{Quadratic interpolation}
\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_4.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=4).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_4.png}
\end{figure}

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_8.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=8).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_8.png}
\end{figure}

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_16.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=16).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_16.png}
\end{figure}

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_32.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=32).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_32.png}
\end{figure}

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_64.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=64).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_64.png}
\end{figure}

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_128.png}\\
\caption{The upper panel shows a mean spectrum of calibrated
sky signals from AC2 stratospheric mode 1,
using quadratic interpolation (x=128).
The lower panel show a standard deviation spectrum
and percentiles spectra.}
\label{fig:study4_.png}
\end{figure}

Figures 3 to 8 show similar data as Figure 1 but using quadratic
interpolation with different setups
(weight parameter \(\lambda = \Delta T /x, x=4,8,16,32,64,128\)).
In general, the results from the quadratic interpolations
are very similar as the result from the linear interpolation.
The same pattern in mean and median values are observed, and
similar distributions of data.
The result from x=32 setup seems to be slightly better
than for other values of x in terms of standard deviation,
and the results agree very well with the result from linear interpolation.
For such high value of \(x\) fairly few number of data
points are effectively used in the fit (see Figure 9).
Figure 9 indicates that the fit in this case should be
very close to a linear interpolation.

\begin{figure}[!t]
\centering
\includegraphics[scale=0.35]{study4_demo32.png}\\
\caption{The upper panel shows mean of band1 of level1a data
from several scans.
The middle panel shows a zoom in of the black dots in the upper panel,
and the black square is the fitted value
(the fit is performed for each channel).
The lower panel shows the corresponding weights (x=32) of each measurement
in the fit. }
\label{fig:study4_dem32.png}
\end{figure}


\documentclass{homework}
\course{Math 5522H}
\author{Alex Li}
\input{preamble}

\begin{document}
\maketitle

\begin{inspiration}
  On April 9, 1975, Congressman Robert Michel brandished a list of new
    NSF grants on the floor of the House of Representatives and selected
      a few that he thought might represent a waste of the taxpayers'
        money. One of them \ldots was called ``Studies in Complex
          Analysis.'' Michel's comment was, `` `Simple Analysis' would,
            hopefully, be cheaper.''
              \byline{Gerald B. Folland} % the American Mathematical Monthly (vol 780, Oct 1998, pg. 780)
              \end{inspiration}

              \section{Terminology}

              \begin{problem}
                What is a \textbf{simply-connected} domain?
                \end{problem}
                \begin{solution}
                A open set $U\subset \C$ is said to be a simply connected domain if it is connected and, any two piecewise smooth closed curves $\gamma_1, \gamma_2: [0, 1]\mapsto U$ with the same start and end point are \textbf{homologous}.
                \end{solution}
                \begin{problem}
                  What does it mean to say that two curves $\alpha: [0, 1]\mapsto U$ and $\beta: [0, 1]\mapsto U$ are
                    \textbf{homologous}?
                    \end{problem}
                    \begin{solution}
                    It means that $\alpha, \beta$ are piecewise smooth and $\alpha(0) = \beta(0)$ and $\alpha(1) = \beta(1)$ and that $\alpha - \beta$ is null homotopic.
                    \end{solution}
                    \begin{problem}
                      What is a \textbf{null homotopic} path?
                      \end{problem}
                      \begin{solution}
                      A path (or formal sum of paths $\sum_{i} \gamma_i$) of a function $f: U \mapsto \C$ is said to be null homotopic if for all $x\in \C \setminus U$, the winding number of the paths sum to 0: $\sum_i n(\gamma_i, x) = 0.$
                      \end{solution}
                      \begin{problem}
                        Given a function $f : U \to \C$, what is meant by a \textbf{branch} of $\log f$?
                        \end{problem}
                        \begin{solution}
                        A branch of $\log f$ is an analytic function $g: U \to \C$ such that $e^g = f$.
                        \end{solution}

                        \section{Numericals}

                        \begin{problem}
                          Let $f(z) = (z^2-z-1)/z^3$, and consider the curve
                            $\gamma : [0,2\pi] \to \C$ by $\gamma(\theta) = f(e^{i\theta})$.
                              Compute the winding number $n(\gamma,0)$.
                              \end{problem}
                              \begin{solution}
                              Let $u=e^{it}$
                              \begin{align*}
                                  n(\gamma, 0) &= \frac{1}{2\pi i}\int_\gamma \frac{1\, d\gamma}{\gamma}\\
                                      &= \frac{1}{2\pi i}\int_{t=0}^{2\pi} \frac{u^3\frac{d}{du}(\frac{1}{u} - \frac{1}{u^2} - \frac{1}{u^3})z'}{(u^2-u-1)}dt\\
                                          &= \frac{1}{2\pi i}\int_{t=0}^{2\pi} \frac{(-u + 2 + 3zu{-1})u'}{(u^2-u-1)}dt
                                          \end{align*}
                                          Let $\gamma_2$ be the positively oriented unit circle. Since $\gamma_2' = z'$,
                                          \begin{align*}
                                              n(\gamma, 0) &= \frac{1}{2\pi i}\int_{\gamma_2} \frac{(-z + 2 + 3z^{-1})\, dz}{(z^2-z-1)}\\
                                                  &= \frac{1}{2\pi i}\int_{\gamma_2} \frac{(-z^2 + 2z + 3)dz}{z(z^2-z-1)}\\
                                                      &= \frac{1}{2\pi i}\int_{\gamma_2} \frac{-3}{z} + \frac{1}{z - \frac{1+\sqrt{5}}{2}} + \frac{1}{z + \frac{1-\sqrt{5}}{2}}dz \quad\color{purple} \text{Partial Fraction Decomp}\\
                                                          &= -3n(\gamma_2, 0) + n(\gamma_2,\frac{1+\sqrt{5}}{2}) + n(\gamma_2, \frac{1-\sqrt{5}}{2})\\
                                                              &= -3 + 0 + 1 = -2
                                                              \end{align*}
                                                              \end{solution}
                                                              \begin{problem}
                                                                Evaluate the \textbf{Dirichlet integral}
                                                                  \[
                                                                      \int_0^\infty \frac{\sin x}{x} \, dx.
                                                                        \]
                                                                        \end{problem}
                                                                        \begin{solution}
                                                                        Let
                                                                        \[
                                                                        I(t) = \int_0^\infty e^{-tx}\frac{\sin x}{x}\, dx
                                                                        \]
                                                                        We would like to find $I(0)$. Consider differentiating w.r.t t:
                                                                        \begin{align*}
                                                                        \frac{d}{dt}I(t) = -\int_0^\infty e^{-tx}\sin x\, dx
                                                                        \end{align*}
                                                                        Now, do integration by parts twice to compute \(I_2 = \int_0^\infty e^{-tx}\sin x\, dx\):
                                                                        \begin{align*}
                                                                        &\color{purple} u=e^{-tx}, u'=-te^{-tx}, v = \cos x, v' = -\sin x\\
                                                                        I_2 &= -e^{-tx}\cos x\big|_{x=0}^\infty - \int_0^\infty te^{-tx}\cos x\, dx\\
                                                                        &= 1 - t\int_0^\infty e^{-tx}\cos x\, dx\\
                                                                        &\color{purple} u=e^{-tx}, u'=-te^{-tx}, v = \sin x, v' = \cos x\\
                                                                        \int_0^\infty e^{-tx}\cos x\, dx &= -e^{-tx}\sin x|_{0}^{\infty} + \int_0^\infty te^{-tx}\sin x dx\\
                                                                        &= tI_2\, dx
                                                                        \end{align*}
                                                                        Thus
                                                                        \[
                                                                        I_2 = 1 - t^2I_2 \implies (t^2+1)I_2 = 1 \implies I_2 = \frac{1}{t^2+1} \implies \frac{d}{dt}I(t) = \frac{-1}{t^2+1}
                                                                        \]
                                                                        With the goal of recovering $I(0)$, we want to integrate. To remove the constant term, let's compute $I(\infty) = \lim_{t\to\infty} \int_0^\infty e^{-tx}\frac{\sin x}{x}$. As this converges pointwise to 0, $I(\infty) = 0$.
                                                                        \begin{align*}
                                                                        I(0) - I(\infty) = I(0) = \int_\infty^0 \frac{-dt}{t^2 + 1} = \tan^{-1}(t)\big|_\infty^0 = -0 - -\pi/2 = \pi/2
                                                                        \end{align*}
                                                                        \end{solution}
                                                                        \begin{problem}
                                                                          Define $\gamma : [0,2\pi] \to \C$ by $\gamma(\theta) = e^{i\theta}$.
                                                                            Suppose $p \in \C[z]$ is a degree $n$ polynomial with distinct roots
                                                                              $z_1,\ldots,z_n$ in the unit disk, and evaluate
                                                                                \[
                                                                                \int_\gamma \frac{z^m \, p'(z)}{p(z)} \, dz.
                                                                                \]
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                \begin{align}
                                                                                \int_\gamma \frac{z^m \, p'(z)}{p(z)} \, dz &= \int_\gamma z^m\sum_{i=1}^n \frac{1}{z-z_i} \, \, dz\\
                                                                                \label{sum_of_int_zm_z_zi}
                                                                                &= \sum_{i=1}^n \int_\gamma \frac{z^m}{z-z_i} \, \, dz
                                                                                \end{align}
                                                                                Now Cauchy's integral formula shows us that for any root $z_i$ and holomorphic function $f$,
                                                                                \begin{align*}
                                                                                f(z_i) &= \frac{1}{2\pi i}\int_\gamma \frac{f(z)}{z - z_i}\,dz\\
                                                                                2\pi i z_i^m &= \int_\gamma \frac{z^m}{z - z_i}\,dz
                                                                                \end{align*}
                                                                                Plugging this into \ref{sum_of_int_zm_z_zi},
                                                                                \begin{align*}
                                                                                \int_\gamma \frac{z^m \, p'(z)}{p(z)} \, dz &= 2\pi i\sum_{i=1}^n z^m
                                                                                \end{align*}
                                                                                \end{solution}

                                                                                \begin{problem}
                                                                                Again define $\gamma : [0,2\pi] \to \C$ by
                                                                                $\gamma(\theta) = e^{i\theta}$, and then evaluate
                                                                                \[
                                                                                \int_\gamma \frac{\conj{w}}{w - z} \, dw
                                                                                \] for $z \in \C$ with $\abs{z} < 1$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                \begin{align*}
                                                                                \int_\gamma \frac{\conj{w}}{w - z} \, dw &=
                                                                                \int_0^{2\pi} \frac{e^{-it}(-ie^{it})}{e^{it} - z} \, dt\\
                                                                                &= -i\int_0^{2\pi} \frac{e^{-it}}{1 - ze^{-it}} \, dt
                                                                                \end{align*}
                                                                                The denominator has positive real part - by assumption $\abs{z}< 1$, and as $\abs{e^{it}} =1$, $\abs{ze^{-it}}  < 1$ so $\Re(1-ze^{-it}) > \abs{1} - \abs{ze^{-it}} > 0$. Thus, $\Log$ is a branch of the logarithm function defined in a simply connnected domain containing the curve $\gamma(t) = 1-ze^{-it}$.

                                                                                Notice that $\frac{d}{dt}\log(1-ze^{-it}) = \frac{-ize^{-it}}{1-ze^{-it}}$, so we can simplify our expression further:
                                                                                \begin{align*}
                                                                                \int_\gamma \frac{\conj{w}}{w - z} \, dw &=
                                                                                \frac{1}{z}\Log(1 - ze^{-it})|_0^{2\pi} = 0
                                                                                \end{align*}

                                                                                \end{solution}
                                                                                \begin{problem}
                                                                                Consider subsets of the real line $A = (-\infty,-1] \cup [1,\infty)$
                                                                                and $B = [-1,1]$.  Is it possible to define a branch of the
                                                                                logarithm of $z^2 - 1$ on $\C \setminus A$?  What about on
                                                                                $\C \setminus B$?
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                From Theorem 4.1 of Palka, it's possible to define a branch of the logarithm on a set $U$ iff $\int_{\gamma} \frac{f'(z)dz}{f(z)} \, dz= 0$ for every closed curve $\gamma\subset U$.

                                                                                \begin{align*}
                                                                                \int_\gamma \frac{2z}{z^2 - 1}\, dz &=     \int_\gamma \frac{1}{z- 1}\,dz + \int_\gamma\frac{1}{z+1}\, dz\\
                                                                                &= n(\gamma, 1) + n(\gamma, -1)
                                                                                \end{align*}
                                                                                It's geometrically clear that any curve $\gamma\subset \C\setminus A$ must have $n(\gamma, 1) = n(\gamma, -1) = 0$, so there is a branch of the log function here.

                                                                                On the other hand, on the set $\C\setminus B$, the curve $\gamma = 2e^{it}$ for $0\leq t \leq 2\pi$ has $n(\gamma, 1)+ n(\gamma, -1) = 1 + 1 \neq 0$, so we cannot define a branch of the logarithm function here.
                                                                                \end{solution}
                                                                                \section{Exploration}

                                                                                \begin{problem}
                                                                                If we can find a branch of $\log f$ then we can define a
                                                                                single-valued $\sqrt{f(z)}$ via $e^{(1/2) \, \log f}$.  Does the
                                                                                converse hold?  Find an open set $U \subset \C$ and a holomorphic
                                                                                function $f : U \to \C$ so that there is \textit{no} branch of
                                                                                $\log f$ but it is nevertheless possible to define a single-valued
                                                                                $\sqrt{f(z)}$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $U = \C \setminus \{0\}$ and $f(z) = z^2$. There is no branch of $\log f$ here: suppose that $e^g = f$ for some holomorphic $g$, then
                                                                                \[
                                                                                g'e^{g} g\cdot f= f' \implies g' = \frac{f'}{f}
                                                                                \]
                                                                                Then since $g$ is holomorphic, so is $g'$. Thus the integral of $g$ around the positively oriented unit circle $\gamma$ is nonzero:
                                                                                \[
                                                                                \int_\gamma g(z) dz =\int_\gamma \frac{f'}{f} dz = \int_\gamma \frac{2}{z} dz = 4\pi i \neq 0
                                                                                \]
                                                                                Thus, $g$ isn't holomorphic, so there is no branch of $\log f$.

                                                                                On the other hand, $z = \sqrt{f(z)}$ is a branch of the square root function that is analytic on the whole set.
                                                                                \end{solution}

                                                                                \begin{problem}
                                                                                Liouville's theorem (\ref{liouville-theorem}) states that a
                                                                                nonconstant holomorphic function $f : \C \to \C$ is not bounded.
                                                                                Seeing \ref{entire-is-dense}, we might ask how ``small'' can the
                                                                                image of holomorphic function be.  Find a holomorphic function
                                                                                $f : \C \to \C$ with image equal to $\C \setminus \{w\}$.
                                                                                (Incidentally, by \textbf{Picard's little theorem}, you will have
                                                                                trouble finding an entire function with image missing two points.)
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Notice that $f(x) = e^x$ has image $\C \setminus \{0\}$, since $f^{-1}(re^{i\theta}) = \log r + i\theta$. However, the real function $\log r$ is not invertible for $r=0$, so the image doesn't contain only 0.

                                                                                Thus, $f(x-w)$ has image $\C\setminus \{w\}$.
                                                                                \end{solution}

                                                                                \begin{problem}\label{cauchy-inequalities-2}Suppose
                                                                                $f(z) = \sum_{n=0}^\infty a_n z^n$ is holomorphic in the disk
                                                                                $B_R(0)$.  If $0 < r < R$, show that
                                                                                \[
                                                                                \abs{a_n} \leq \frac{1}{r^n} \displaystyle\sup_{z \in \partial B_r(0)} \abs{f(z)}.
                                                                                \]
                                                                                This extends \ref{cauchy-inequalities}.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $\gamma$ be the positively oriented border of $B_R(0)$.
                                                                                By Cauchy's integral formula,
                                                                                \begin{align*}
                                                                                f^{(n)}(0) &= \frac{n!}{2\pi i}\int_{\gamma} \frac{f(z)\, dz}{z^{n+1}}\\
                                                                                &= \frac{n!}{2\pi}\abs{\int_{\gamma} \frac{f(z)\, dz}{z^{n+1}}}\\
                                                                                &\leq \frac{n!}{2\pi}\int_{\gamma} \frac{\abs{f(z)}\, dz}{r^{n+1}}\\
                                                                                &\leq n!\frac{\sup_{z\in\partial B_r(0)} \abs{f(z)}}{r^{n}}\quad \color{purple} \int_\gamma d|\gamma| = 2\pi r
                                                                                \end{align*}

                                                                                Also, by taking derivatives,
                                                                                \begin{align*}
                                                                                f^{(n)}(z) = \frac{\partial^n \sum_{k=0}^\infty a_kz^k }{\partial ^n} = n!a_n + \sum_{k=n+1}^\infty k!a_k z^{n-k}
                                                                                \end{align*}
                                                                                When $z=0$, this implies $f^{(n)}(0) = n!a_n$.
                                                                                Comparing the two expressions,
                                                                                \begin{align*}
                                                                                n!a_n \leq n!\frac{\sup_{z\in\partial B_r(0)} \abs{f(z)}}{r^{n}}
                                                                                a_n \leq \frac{\sup_{z\in\partial B_r(0)} \abs{f(z)}}{r^{n}}.
                                                                                \end{align*}
                                                                                \end{solution}

                                                                                \begin{problem}
                                                                                Consider a nonconstant polynomial $p \in \mathbb{C}[z]$ of degree $n$.  Compute
                                                                                \[
                                                                                \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{p'(z)}{p(z)} - \frac{n}{z} \right) dz.
                                                                                \]
                                                                                Can you perform this computation \textit{without} appealing to the
                                                                                fundamental theorem of algebra?
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $\displaystyle{p(z) = \sum_{k=0}^n a_kz^k}$.
                                                                                \begin{align*}
                                                                                \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{p'(z)}{p(z)} - \frac{n}{z} \right) dz &= \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{\sum_{k=1}^n a_kz^{k-1} }{\sum_{k=0}^n a_kz^{k}} - \frac{n}{z} \right) dz \\
                                                                                &= \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{\sum_{k=1}^n ka_kz^{k-1} }{\sum_{k=0}^n a_kz^{k}} - \frac{n}{z} \right) dz \\
                                                                                &= \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{\sum_{k=1}^n ka_kz^{k} - n\sum_{k=0}^n a_kz^{k+1} }{\sum_{i=0}^k a_kz^{k+1}} \right) dz \\
                                                                                &= \lim_{r \to \infty} \int_{\partial B_r(0)} \left( \frac{\sum_{k=0}^{n-1} (k-n)a_kz^{k} }{\sum_{k=0}^n a_kz^{k+1}} \right) dz = f(r)
                                                                                \end{align*}
                                                                                This is the integral of something $O(\frac{1}{z^2})$, so it's 0. More precisely, choose $r$ such that $\abs{a_{n-1}r^{n-1}} > \sum_{k=0}^{n-1} \abs{(k-n)a_kr^{k}}$ and $\abs{a_nr^{n+1}} > \frac{1}{2}(\sum_{k=0}^{n-1} \abs{a_kr^{k+1}})$
                                                                                \begin{align*}
                                                                                f(r) &\leq \int_{\partial B_r(0)} \left( \abs{\frac{\sum_{k=0}^{n-2} (k-n)a_kz^{k} }{\sum_{k=0}^n a_kz^{k+1}}}\right) dz\\
                                                                                &\leq \int_{\partial B_r(0)} \left( \frac{\abs{a_{n-1}r^{n-1}}+\sum_{k=0}^{n-1} \abs{(k-n)a_kr^{k}}}{\abs{a_nr^{n+1}} - \sum_{k=0}^{n-1} \abs{a_kr^{k+1}}}\right) dz \\
                                                                                &\leq \int_{\partial B_r(0)} \left( \frac{\abs{2a_{n-1}r^{n-1}}}{\abs{\frac{1}{2}a_nr^{n+1}}}\right) dz \qquad \color{purple}C:=\frac{4a_{n-1}}{a_n}\\
                                                                                &= C\int_{\partial B_r(0)} \abs{\frac{1}{r^2}} dz \leq C\frac{1}{r^2}2\pi r
                                                                                \end{align*}
                                                                                As $r\to 0$, this expression also goes to 0.
                                                                                \end{solution}

                                                                                \begin{problem}\label{uniformly-approximate-conj}For $\epsilon > 0$, is there a polynomial $p \in \mathbb{C}[z]$ so
                                                                                that $\abs{p(z) - \conj{z}} < \epsilon$ for $z \in B_1(0)$?  In
                                                                                other words, can we uniformly approximate $\conj{z}$ by a polynomial
                                                                                in $z$?
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                No. Suppose $p(z) - \conj{z} < 1$ for all points in $B_1(0)$.
                                                                                Let $\gamma:[0,2\pi]$ with $\gamma(t) = .5e^{it}$. Then
                                                                                \begin{align*}
                                                                                \int_{\gamma} p(z) - \conj{z}dz =
                                                                                \int_{\gamma} p(z) - \int_{\gamma} \frac{1}{z} dz = 0-2\pi i
                                                                                \end{align*}
                                                                                However,
                                                                                \begin{align*}
                                                                                \abs{\int_{\gamma} p(z) - \conj{z} dz} &=
                                                                                \abs{\int_0^{2\pi} \gamma'(p(\gamma(t)) - \conj{\gamma(t)}) dt}\\
                                                                                &\leq \int_0^{2\pi} .5 \abs{p(\gamma(t)) - \conj{\gamma(t)})} dz \leq \pi
                                                                                \end{align*}
                                                                                And since $|2\pi i|\not\leq \pi$, we have a contradiction.
                                                                                \end{solution}

                                                                                \section{Prove or Disprove and Salvage if Possible}

                                                                                \begin{problem}\label{entire-is-dense}If $f : \C \to \C$ is
                                                                                holomorphic, then the image of $f$ is dense in $\C$.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                No, let $f(z)=0$. That's pretty holomorphic and pretty not dense.

                                                                                To show that the image is dense, it suffices to show that for any $w$, there is no open ball around $w$ in the image such that no point of $f$ maps to the open ball around $w$. If $w\in \text{Image} f$ then we are done! Otherwise, consider the function $g(z) =\frac{1}{f(z) - w}$, it's holomorphic since $f(z) - w$ has no zeros, and hence it is unbounded, and thus $\forall \epsilon > 0$ there is a point $z$ such that $\frac{1}{f(z) - w} > \frac{1}{\epsilon}$, so $f(z) - w < \epsilon$. Hence there are points arbitrarily close to $w$ in the image of $f$, implying density.
                                                                                \end{solution}

                                                                                \begin{problem}\label{identity-dominate-entire}Suppose $f : \C \to \C$
                                                                                is holomorphic and for all $z \in \C$ we have
                                                                                $\abs{f(z)} \leq \abs{z}$.  Then $f(z) = \lambda z$ for some
                                                                                $\lambda \in \C$.
                                                                                \end{problem}
                                                                                \begin{solution}

                                                                                Since $f$ is holomorphic in $\C$, it is analytic and can be represented by an everywhere convergent power series.
                                                                                \[
                                                                                f(z) = \sum_{i=0}^\infty a_iz^i
                                                                                \]
                                                                                Since $\abs{f(0)} \leq \abs{0}$, $f(0)=0$ and thus $a_0=0$. Define $g(z) = \sum_{i=0}^\infty a_{i+1}z^i$ so that
                                                                                \begin{align}\label{f_z_is_z_g_z}
                                                                                f(z) = z\sum_{i=0}^\infty a_{i+1}z^i = zg(z)
                                                                                \end{align}

                                                                                $g(z)$ must be convergent everywhere, since if $z = 0$ $g(z)=a_1$ and otherwise, multiplication by $z$ gives $f(z)$. Thus, $g(z)$ is an analytic function.

                                                                                Since $f(|z|)\leq |z|$, $f(0)=0$, and dividing out we have that at other points, $\abs{\frac{f(z)}{z}}\leq 1$. Combining this with \ref{f_z_is_z_g_z},
                                                                                \[
                                                                                \frac{f(z)}{z} = g(z) \leq 1
                                                                                \]
                                                                                Thus liouville's theorem shows that $g(z)$ is a constant $\lambda$, and therefore
                                                                                \[f(z) = z\lambda.\]
                                                                                \end{solution}
                                                                                \begin{problem}
                                                                                There is a nonconstant holomorphic function $f : \C \to \C$ so that
                                                                                for all $n \in \Z$ we have $f(z + n) = f(z)$.  (Such a function is
                                                                                \textbf{periodic}.)
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Let $f(z) = \sin(2\pi z)$. This is clearly not constant, and
                                                                                \[
                                                                                f(z+n) = \sin(2\pi (z + n)) + \sin(2\pi z + 2\pi n) = \sin(2\pi z) = f(z) .
                                                                                \]
                                                                                $f$ is holomorphic since $\sin = \frac{e^{i\theta} - e^{-i\theta}}{2i}$ is holomorphic and we are just composing it with addition and multiplication by a constant.
                                                                                \end{solution}

                                                                                \begin{problem}\label{doubly-periodic}There is a nonconstant holomorphic function $f : \C \to \C$ so that
                                                                                for all $\omega \in \Z[i]$ we have $f(z + \omega) = f(z)$.  (Such a
                                                                                function is \textbf{doubly periodic}.)
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                False. If so, then the image of $f:\C \mapsto \C$ is the same as the image of $f$ restricted to the unit square. As $f$ is continuous, and the unit square is compact, the image of $f$ is bounded, contradicting the fact that $f$ is entire.

                                                                                Since any constant function is holomorphic, we can salvage by saying that for any holomorphic $f$, $f$ is doubly periodic iff it is constant.
                                                                                \end{solution}

                                                                                \begin{problem}\label{maximum-modulus-principle}
                                                                                Consider a connected open subset $U \subset \C$ and a holomorphic
                                                                                function $f : U \to \C$.  If there is a point $z_0 \in U$ so that
                                                                                for all $z \in U$ we have $\abs{f(z_0)} \geq \abs{f(z)}$, then $f$
                                                                                is constant.  \textit{Hint:} show that $g(z) = \abs{f(z)}$ is
                                                                                constant and invoke \ref{open-mapping-theorem-preview}.  This is the
                                                                                \textbf{maximum modulus principle}.
                                                                                \end{problem}
                                                                                \begin{solution}
                                                                                Choose a ball of radius $r$ around $z_0$ contained in $U$ parameterized by $\gamma$. By Cauchy's integral formula,
                                                                                \begin{align*}
                                                                                f(z_0) = \frac{1}{2\pi i}\int_{\gamma} \frac{f(z) \, dz}{z - z_0}\\
                                                                                \abs{f(z_0)} = \frac{1}{2\pi}\abs{\int_{\gamma} \frac{f(z) \, dz}{z-z_0}}\\
                                                                                = \frac{1}{2\pi}\abs{\int_0^{2\pi} \frac{f(e^{it})(rie^{it}) \, dz}{z - z_0}}\\
                                                                                \leq \frac{1}{2\pi}\int_0^{2\pi} \frac{\abs{f(e^{it})(rie^{it})} \, dz}{r}\\
                                                                                \leq \frac{1}{2\pi}\int_0^{2\pi} \abs{f(e^{it})} \, dz
                                                                                \leq    \frac{1}{2\pi}\int_0^{2\pi} \abs{f(e^{it})} \, dz
                                                                                \end{align*}
                                                                                This means that the average value of $f(z)$ around the point $z_0$ is at least $z_0$, but since $f(z_0)$ is the max, $f(z)=f(z_0)$ on $\gamma$. As this argument holds for any ball of radius $\gamma$ contained in $U$, the set $V = \{z:f(z) = f(z_0)\}$ is open. The set $V^c = \{z:f(z)\neq f(z_0)\}$ is also open since it's the preimage of an open set, so $V$ is closed. Since $U$ is connected, $V=U$.
                                                                                \end{solution}

                                                                                \end{document}


\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{fancyvrb}
\usepackage{float}
\usepackage{graphicx}
\usepackage{bm}
\begin{document}

\title{APPM 5510 HW 5}
\author{Zane Jakobs}

\maketitle

\subsection*{1} Let $\bm{\Pi}_{i;j}$ be the Jacobian of the flow map $\Psi$ at time $i$ with respect to the initial condition at time $j$. Varying one parameter at a time, starting with $\bm{x}_0$, we have
\[
\begin{aligned}
\nabla J_1 \cdot \delta\bm{x}_0 &= \nabla J_0 \cdot \bm{x}_0 + \lambda^T\bm{\Pi}_{1;0} \cdot \bm{x}_0\\
&= 2(\bm{x}_0 - \bm{x}_b)^T\bm{B}_0^{-1}\cdot \delta\bm{x}_0 + 2 (\bm{x}_1 - \Psi(\bm{x}_0))^T\bm{B}_1^{-1}\Pi_{1;0}^T \cdot \delta\bm{x}_0 + 2(\bm{y}_0 - \bm{H}_0\bm{x}_0)^T\bm{R}_0^{-1}\bm{H}_0\cdot \delta\bm{x}_0
\end{aligned}
\]
and with $\bm{x}_1$, we get
\[
\begin{aligned}
\nabla J_1 \cdot \delta\bm{x}_1 &=  \nabla J_0 \cdot \delta\bm{x}_1 - \lambda^T \cdot\delta\bm{x}_1\\
&= 2(\bm{x}_1 - \Psi(\bm{x}_0))^T\bm{B}_1^{-1} \cdot\delta\bm{x}_1 + 2(\bm{y}_1 - \bm{H}_1\bm{x}_1)^T\bm{R}_1^{-1}\bm{H}_1\cdot \delta\bm{x}_1 - \bm{\lambda}^T\cdot\delta\bm{x}_1
\end{aligned}
\]
We now have the equation
\[
\begin{aligned}
0 &= \nabla J_1 \cdot\bm{x}\\
&= 2(\bm{x}_0 - \bm{x}_b)^T\bm{B}_0^{-1}\cdot \delta\bm{x}_0 + 2(\bm{x}_1 - \Psi(\bm{x}_0))^T\bm{B}_1^{-1} (\bm{I} \cdot \delta\bm{x}_1+ \Pi_{1;0}^T\cdot\bm{x}_0) \\
&- (\bm{y}_0 -  \bm{H}_0\bm{x}_0)^T\bm{R}_0^{-1}\bm{H}_0\cdot \delta\bm{x}_0 - (\bm{y}_1 - \bm{H}_1\bm{x}_1)^T\bm{R}_1^{-1}\bm{H}_1\cdot \delta\bm{x}_1 - \bm{\lambda}^T\cdot\delta\bm{x}_1.
\end{aligned}
\]

Dividing out differentials and rearranging terms, we see that
\[
\begin{aligned}
\bm{\lambda} &= 2\bm{B}_0^{-T}(\bm{x}_0 - \bm{x}_b) + 2(\bm{I} + \Pi_{1;0}^T)^T\bm{B}_1^{-T}(\bm{x}_1 - \Psi(\bm{x}_0)) \\
&- \bm{H}_0^T\bm{R}_0^{-T}(\bm{y}_0 - \bm{H}_0\bm{x}_0) - \bm{H}_1^T\bm{R}_1^{-T}(\bm{y}_1 - \bm{H}_1\bm{x}_1)
\end{aligned}
\]
The difference between this and the gradient in the notes is that here, we have a term depending on the actual value of $\bm{x}_1$ (instead of the flow map from $\bm{x}_0$ to time $1$), and we only need to compute one $\bm{\lambda}$, instead of two (for two times) because of the constraint that $\bm{x}_1 = \Psi(\bm{x}_0).$

\subsection*{2}
(Note: code attached to the back of the assignment)
\begin{figure}[H]
\includegraphics[scale=0.5]{/Users/zanejakobs/Desktop/4dvarlossr.png}
\end{figure}

\subsection*{3}
Letting $\bm{x} = (x,y)^T$, we have $x_{j+1} = a_j x_j + y_j + \eta_0$, $y_{j+1} = x_j + 2y_j$, and $a_{j+1} = a_j.$ Since $a$ is independent of $x$, differentiating each equation w.r.t. each element of $(x,y,a)$ gives the following Jacobian matrix:
\[
\begin{bmatrix}a & 1 & 0 \\ 1 & 2 & 0 \\ 0 & 0 & 1 \end{bmatrix}
\]
\subsection*{Code}
\begin{Verbatim}
import matplotlib.pyplot as plt
import numpy as np

# x_t -> x_{t+1}
def xmap(x):
    return 3.95 * x * (1 - x)

def psi(x0,t):
    i = 0
    x = x0
    while i < t:
        x = xmap(x)
        i += 1
    return x

def H(x):
    return x + np.random.normal(0, 0.01)

def obs_error(y, x, t):
    xt = psi(x,t)
    return y - H(xt)

def varobjective4d(x0, y):
    xb = 1./3.
    b0 = 0.1 ** 2

    R = 0.01 ** 2

    J = (1/b0) * (x0 - xb) ** 2

    for i in range(len(y)):
        e = obs_error(y[i], x0, i)

        J += (1./R) * (e ** 2)

    return J

if __name__ == '__main__':

    true_obs = [0.25]
    for i in range(4):
        true_obs.append(xmap(true_obs[-1]))

    obs = [H(x) for x in true_obs]

    Jvals = []

    for i in range(1,len(obs)):
        Jvals.append(varobjective4d(obs[0], obs[:i]))

    plt.plot(Jvals)
    plt.title("4DVAR objective function vs number of data points")
    plt.xlabel("Number of observations")
    plt.ylabel("4DVAR objective")
    plt.show()
\end{Verbatim}

\end{document}


%!TEX root = TTK4215-Summary.tex
\section{Model reference adaptive control (MRAC)}
MRAC requires a plant and a reference model. A controller is made so that the controller and plant together behave similar to the reference model. An adaptive algorithm estimates the controller parameters $\theta$. There are two main categories:
\begin{itemize}
	\item \emph{Direct}, where $\theta$ is equal to the controller gains.
	\item \emph{Indirect}, where the controller gains are a function of $\theta$.
\end{itemize}
Huge drawback: Requires plant of minimum phase. Also requires known relative degree and bounded plant order.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width = \textwidth]{MRAC}
\caption{MRAC structure}
% \label{fig:MRAC}
\end{center}
\end{figure}

\subsection{How to... MRAC}
Given a plant $y(s) = \frac{b}{s+a}u(s)$ and a reference model $y_m(s) = \frac{b_m}{s+a_m}r$ where $r \in L_\infty$.
Then the optimal ideal controller has the structure $u^* = -\theta_1^* y + \theta_2^* r$, where $\theta_1^*$ and $\theta_2^*$ is optimal controller parameters. The optimal parameters can be found by comparison between $y$ and $y_m$:
\begin{equation}
\begin{split}
y &= \frac{b}{s+a}(-\theta_1^* y + \theta_2^* r) \\
y(1+\frac{b\theta_1^*}{s+a} ) &= \frac{b\theta_2^*}{s+a}r \\
y &= \frac{b\theta_2^*}{s+a+b\theta_1^*}r
\end{split}
\end{equation}
which gives us $\theta_1^* = \frac{a_m-a}{b}$ and $\theta_2^* = \frac{b_m}{b}$. The closed loop differential equations is

\begin{equation}
\begin{split}
\dot{y} &= -ay +bu \\
& =-ay + b (-\theta_1 y + \theta_2 r) \\
& = (-a- b \theta_1) y + b \theta_2 r \\
\dot{y_m} &= -a_m y_m + b_m r
\end{split}
\end{equation}
We now define $e \triangleq y - y_m$, $\tilde{\theta_1}  \triangleq \theta_1 - \theta_1^*$, $\tilde{\theta_2}  \triangleq \theta_2 - \theta_2^*$ and derive the error dynamics.

\begin{equation}
\begin{split}
\dot{e} &= \dot{y}-\dot{y_m} \\
&= (-a- b \theta_1) y + b \theta_2 r - ( -a_m y_m + b_m r ) \\
&= -a_m y +  a_m y_m + (-a + a_m- b \theta_1) y + b \theta_2 r - b_m r \\
&= -a_m e + (-a + a_m- b (\tilde{\theta_1} +\frac{a_m-a}{b})) y + b (\tilde{\theta_2} + \frac{b_m}{b})r - b_m r \\
&= -a_m e - \tilde{\theta_1}b y + \tilde{\theta_2}b r
\end{split}
\end{equation}
We now use a Lyapunov (ish?) function $V = \frac{1}{2}e^2 + \frac{b}{2\gamma_1}\tilde{\theta_1^2} + \frac{b}{2\gamma_2}\tilde{\theta_2^2} $.

\begin{equation}
\begin{split}
\dot{V} &= e\dot{e} 	+ \frac{b}{\gamma_1}\tilde{\theta_1}\dot{\tilde{\theta_1}}
				+ \frac{b}{\gamma_2} \tilde{\theta_2}\dot{\tilde{\theta_2}} \\
&= 	e (-a_m e - \tilde{\theta_1}b y + \tilde{\theta_2}b r)
	+ \frac{b}{\gamma_1}\tilde{\theta_1}\dot{\theta_1}
	+ \frac{b}{\gamma_2}\tilde{\theta_2}\dot{\theta_2} \\
&= -a_m e^2 	+ \frac{b}{\gamma_1}\tilde{\theta_1}(\dot{\theta_1}  - \gamma_1 y e)
			+ \frac{b}{\gamma_2}\tilde{\theta_2}(\dot{\theta_2} + \gamma_2 r e )
\end{split}
\end{equation}
The update laws are selected such that the derivative Lyapunov function are negative semi-definite.

\begin{equation}
\begin{split}
(\dot{\theta_1}  - \gamma_1 y e) &= 0 \Rightarrow \dot{\theta_1} = \gamma_1 y e \\
(\dot{\theta_2} + \gamma_2 r e ) &= 0 \Rightarrow \dot{\theta_2} = \gamma_2 r e
\end{split}
\end{equation}
It can now be shown that $e,\tilde{\theta_1}, \tilde{\theta_2},r,y_m,y,\dot{e} \in L_\infty$ and $e \in L_2$, which leads to $\lim_{t \to \infty} e(t) \rightarrow 0$ from Lemma 3.2.5.

TTK4215 System identification and adaptive control/sec-mrac.tex


\section{A case study in clustering}\label{section:examples}

\subsection{\(k\)-means clustering}

The following examples act as a form of validation for EDO, and also highlight
some of the nuances in its use. The objective of these examples is to use the
proposed method to reproduce some known results about the clustering of data in
the absence of any external forces, and to examine how clustering algorithms are
typically evaluated. In particular, the focus will be on the well-known
\(k\)-means (Lloyd's) algorithm. Clustering was chosen as it is a
well-understood problem that is easily accessible \-- especially when restricted
to two dimensions. The \(k\)-means algorithm is an iterative, centroid-based
method that aims to minimise the `inertia' of the current partition, \(Z =
\left\{Z_1, \ldots, Z_k\right\}\), of some dataset \(X\):
\begin{equation}
    I(Z, X) := \frac{1}{|X|} \sum_{j=1}^{k} \sum_{x \in Z_j} {d(x, z_j)}^2
    \label{eq:inertia}
\end{equation}

A full statement of the algorithm to minimise~(\ref{eq:inertia}) is given
in~\ref{app:kmeans}.

This inertia function is taken as the objective of the \(k\)-means algorithm,
and is used for evaluating the final clustering. This is particularly true when
the algorithm is not being considered an unsupervised classifier where accuracy
may be used~\cite{Huang1998}. With that, the first example will use this inertia
as the fitness function in EDO.\ That is, to find datasets which minimise \(I\).

For the purposes of visualisation, EDO is restricted to the space containing
only two-dimensional datasets, i.e.\ \(C = \left((2, 2)\right)\). In addition to
this, all columns are formed from uniform distributions where the bounds are
sampled from the unit interval. Thus, the only family in \(\mathcal{P}\) is:
\begin{equation}
    \mathcal{U} := \left\{U(a, b)~|~a, b \in [0, 1]\right\}
\end{equation}

The remaining parameters are as follows: \(N~=~100\), \(R~=~(3, 100)\),
\(M~=~1000\), \(b~=~0.2\), \(l~=~0\), \(p_m~=~0.01\), and shrinkage is excluded.
Figure~\ref{fig:small-inertia-50} shows an example of the fitness (above) and
dimension (below) progression of the evolutionary algorithm under these
conditions up until the \(50^{th}\) epoch.

There is a steep learning curve here; within the first 50 generations an
individual is found with a fitness of roughly \(10^{-10}\) which could not be
improved on for a further 900 epochs. The same quick convergence is seen in the
number of rows. This behaviour is quickly recognised as preferable and was
dominant across all the trials conducted in this work. This preference for
datasets with fewer rows is expected given that \(I\) is the sum of the mean
error from each cluster centre. With that, when \(k\) is fixed \textit{a
priori}, reducing the number of points in each cluster (i.e.\ the terms of the
second summation) quickly reduces the mean error of that cluster and thus the
value of \(I\).

\addtocounter{figure}{1}
\begin{figure}[htbp]
    \ContinuedFloat%
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig7a-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig7a-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for final inertia and dimension across the first 50
        epochs with \(R~=~(3,100)\).
    }\label{fig:small-inertia-50}
\end{figure}

\begin{figure}[htbp]
    \ContinuedFloat%
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig7b-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig7b-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for final inertia and dimension across the first 50 epochs
        with \(R~=~(50,100)\).
    }\label{fig:large-inertia-50}
\end{figure}

However, something that may be seen as unwanted is a compaction of the cluster
centres. Referring to Figure~\ref{fig:small-inertia-inds}, the best and median
individuals show two clusters that are essentially the same point whereas the
worst is a random cloud across the whole of \(\mathcal{U}\) which was found in
the initial population. The kind of behaviour exhibited by the best performing
individuals here occurs in part because it is allowed. There are two immediate
ways in which this allowed: first, that a near-trivial case is included in \(R\)
and, secondly, that the fitness function does nothing to penalise the proximity
of the inter-cluster means, as well as aiming to reduce the intra-cluster means.
This kind of unwanted behaviour highlights a subtlety in how EDO should be used;
that experimentation and rigour are required to properly understand an
algorithm's quality.

\begin{figure}[htbp]
    \centering
    \subfloat[][]{%
        \label{fig:small-inertia-inds}
        \centering
        \includegraphics[width=\imgwidth]{Fig8a.pdf}
    }\\

    \subfloat[][]{%
        \label{fig:large-inertia-inds}
        \centering
        \includegraphics[width=\imgwidth]{Fig8b.pdf}
    }
    \caption[]{%
        Representative individuals based on inertia with:
        \subref{fig:small-inertia-inds} \(R~=~(3,100)\);
        \subref{fig:large-inertia-inds} \(R~=~(50,100)\). Centroids displayed as
        crosses.
    }\label{fig:inertia-inds}
\end{figure}

Hence, consider Figure~\ref{fig:large-inertia-inds} where the individuals have
been generated with the same parameters as previously except with adjusted row
limits, \(R = (50, 100)\), so as to exclude this trivial case. In these trials,
the results are equivalent: the worst performing individuals are without
structure whilst the best-performing individuals display clusters that are dense
about a single point despite the minimum number of rows being increased.
Supposing this was not already a known result, we can see mounting evidence in
favour of this compaction being `optimal' behaviour in a dataset for \(k\)-means
clustering.

However, the fitness function may be addressed still, and more extensive
studying may be done. Indeed, the final inertia could be considered a flawed or
fragile fitness function if it is supposed to evaluate the efficacy of the
\(k\)-means algorithm. Incorporating the inter-cluster spread to the fitness
of an individual dataset would reduce this observed compaction. For instance,
the silhouette coefficient is a metric used to evaluate the appropriateness of a
clustering to a dataset and does precisely that. The silhouette coefficient of a
clustering of a dataset is given by the mean of the silhouette value,
\(S(x)\), of each point \(x \in Z_j\) in each cluster:
\begin{equation}
    \begin{gathered}
        A(x) := \frac{1}{|Z_j| - 1} \sum_{y \in Z_j \setminus \{x\}} d(x, y),
        \\
        B(x) := \min_{k \neq j} \frac{1}{|Z_k|} \sum_{w \in Z_k} d(x, w),
        \\
        S(x) :=
            \begin{cases}
                \frac{B(x) - A(x)}{\max\left\{A(x), B(x)\right\}}
                &\quad \text{if } |Z_j| > 1\\
                0 &\quad \text{otherwise}
            \end{cases}
    \end{gathered}\label{eq:silhouette}
\end{equation}\\

The optimisation of the silhouette coefficient is analogous to finding a dataset
which increases both the intra-cluster cohesion (the inverse of \(A\)) and
inter-cluster separation (\(B\)). Hence, the objective of minimising inertia is
addressed by maximising cohesion. Meanwhile, the additional desire to spread out
the clusters is considered by maximising separation.

Repeating the trials with the same parameters as with inertia, the silhouette
fitness function yields the results summarised in
Figures~\ref{fig:small-silhouette}~and~\ref{fig:large-silhouette}. Irrespective
of row limits, the datasets produced show increased separation from one another
whilst maintaining low values in the final inertia of the clustering as shown in
Figure~\ref{fig:silhouette-inds}. Again, the form of the individual clusters is
much the same. The low values of inertia correspond to tight clusters, and the
tightest clusters are those with a minimal number of points, i.e.\ a single
point. As with the previous example, albeit at a much slower rate, the
preferable individuals are those leading toward this case. That this gradual
reduction in the dimension of the individuals occurs despite adjusting the
fitness function and considering the space which excludes the trivial case
bolsters the claim that the base case is also optimal.

At this point, it should be noted that, due to the nature of the implementation,
any individual from any generation may be retrieved and studied should the final
results be too concentrated on any given case. The summary provided here is one
particular way of studying the body of datasets generated with this method and
this transparency in the history and progression of the proposed method is
something that sets it apart from other methods such as GANs which have a
reputation of providing so-called `black box' solutions.

\addtocounter{figure}{1}
\begin{figure}[htbp]
    \ContinuedFloat%
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig9a-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig9a-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for silhouette and dimension across 1000 epochs at 100
        epoch intervals with \(R~=~(3, 100)\).
    }\label{fig:small-silhouette}
\end{figure}

\begin{figure}
    \ContinuedFloat%
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig9b-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig9b-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for silhouette and dimension across 1000 epochs at 100
        epoch intervals with \(R~=~(50,100)\).
    }\label{fig:large-silhouette}
\end{figure}

\begin{figure}[htbp]
    \centering
    \subfloat[][]{%
        \label{fig:small-silhouette-inds}
        \centering
        \includegraphics[width=\imgwidth]{Fig10a.pdf}
    }\\

    \subfloat[][]{%
        \label{fig:large-silhouette-inds}
        \centering
        \includegraphics[width=\imgwidth]{Fig10b.pdf}
    }
    \caption[]{%
        Representative individuals based on silhouette with:
        \subref{fig:small-silhouette-inds} \(R~=~(3,100)\);
        \subref{fig:large-silhouette-inds} \(R~=~(50,100)\). Centroids displayed
        as crosses.
    }\label{fig:silhouette-inds}
\end{figure}

\subsection{Comparison with DBSCAN}\label{subsec:dbscan}

The extent of the capabilities EDO holds as a tool to better understand an
algorithm are especially apparent when comparing an algorithm against another
(or set of others) simultaneously. This is done by utilising the freedom of
choice in a fitness function for EDO.\ Consider two algorithms, \(A\) and \(B\),
and some common metric between them, \(g\). Then their similarities and
contrasts can be explored by considering the differences in this metric on the
two algorithms. In terms of EDO, this means using \(f = g_A - g_B\), \(f = g_B -
g_A\) or \(f = \left| g_B - g_A \right|\) as the fitness function. By doing so,
pitfalls, edge cases or fundamental conditions for the method may be
highlighted. Overall, this process allows the researcher to more deeply learn
about the method of interest beyond the traditional method of literature
comparison on a particular example.

Consider the following use case with another clustering algorithm of a different
form, Density Based Spatial Clustering of Applications with Noise (DBSCAN). In
this particular case, the objective is to find datasets for which the method of
interest, \(k\)-means, outperforms its alternative, DBSCAN.\ Here there is no
concept of inertia as DBSCAN is density-based and is able to identify
outliers~\cite{Ester1996}. As such, a valid metric must be chosen. One such
metric is the silhouette score as defined in~(\ref{eq:silhouette}).

In this case, however, an adjustment to the fitness function must be made so as
to accommodate for the condition of the silhouette coefficient that there must
be more than one cluster present. Let \(S_k (X)\) and \(S_D (X)\) denote the
silhouette coefficients of the clustering found by \(k\)-means and DBSCAN
respectively. Then the fitness function is defined to be:
\begin{equation}
    f(X) =
        \begin{cases}
            S_D (X) - S_k (X), &\quad \text{%
                \begin{tabular}{l}%
                    if DBSCAN identifies two or
                    \\
                    more clusters (inc.\ noise)
                \end{tabular}
            }\\
            \infty &\quad \ \ \text{otherwise.}
        \end{cases}\label{eq:dbscan-fitness}
\end{equation}

There are several remarks to be made here. First, note the order of the
subtraction here as EDO minimises fitness functions by default. Also, \(f\)
takes values in the range \([-2, 2]\) where \(-2\) is the best, i.e.\ \(S_D(X) =
-1\) and \(S_k(X) = 1\). Likewise, 2 is the worst score. Finally, the silhouette
coefficient requires at least two clusters to be present and so if DBSCAN
identifies a single cluster then that individual will be penalised heavily under
this fitness function when, in fact, that clustering may be of high quality. As
such, this fitness function may require adjustment.

It must also be acknowledged that \(k\)-means and DBSCAN share no common
parameters and so direct comparison is more difficult. For the purposes of this
example, only one set of parameters is used but a thorough investigation should
include a parameter sweep in similar, real-world use cases. The parameters being
used are \(k~=~3\) for \(k\)-means, and \(\epsilon~=~0.1,\ MinPoints~=~5\) for
DBSCAN.\ This set was chosen following informal experimentation using the Python
library Scikit-learn~\cite{scikit} to find comparable parameters in the given
search space defined by the EDO parameters used previously with
\(R~=~(50,100)\).

\begin{figure}[htbp]
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig11-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig11-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for difference in silhouette (\(k\)-means-preferable) and
        dimension across 1000 epochs at 100 epoch intervals.
    }\label{fig:dbscan-silhouette}
\end{figure}

\begin{figure}[htbp]
    \centering
    \subfloat[][]{%
        \label{fig:dbscan-inds-k}
        \centering
        \includegraphics[width=\imgwidth]{Fig12a.pdf}
    }\\
    \subfloat[][]{%
        \label{fig:dbscan-inds-d}
        \centering
        \includegraphics[width=\imgwidth]{Fig12b.pdf}
    }
    \caption[]{%
        Representative individuals from a \(k\)-means-preferable run with
        clustering by: \subref{fig:dbscan-inds-k} \(k\)-means;
        \subref{fig:dbscan-inds-d} DBSCAN.\ Concave and convex hulls illustrated
        by shading and outline respectively.
    }\label{fig:dbscan-inds}
\end{figure}

Figure~\ref{fig:dbscan-silhouette} shows a summary of the progression of EDO
for this use case. As with the previous examples where \(R~=~(50, 100)\), the
variation in the population fitness is unstable but there is a clear trend of
improvement in the best individual over the course of the run. There is also a
convergence seen in the number of rows a dataset has. The resting dimension
varied across the trials conducted in this work but none exhibited a dramatic
shift toward the lower limit of 50 rows as with previous examples. This is
suggestive of a more competitive environment for individuals where slight
changes to an individual can drastically alter their fitness.

The effect of such changes can be seen in Figure~\ref{fig:dbscan-inds} where
representative individuals are shown for this example. Here, the best performing
individual, when clustered by \(k\)-means, shows three clear and nicely
separated clusters. Note that they are not so tightly packed; again, this
suggests that the route to an optimal individual is less clearly defined. In
contrast, when the same dataset is clustered by DBSCAN a single cluster is found
with a single noise point held within the convex hull of the cluster, i.e.\
there are overlapping clusters (since noise points form a single cluster).
Hence, along with the fact that the larger cluster is widely spread, it follows
that the clustering has a relatively small, negative silhouette coefficient.

Another point of interest here is the convexity of the clusters. A known
condition for the success of \(k\)-means is that the presented clusters are of
roughly equal size and are convex. This is due to the overall objective being to
approximate the centroidal Voronoi tessellation~\cite{Du2006}. Without this
condition, up to the correct choice of \(k\), the algorithm will fail to produce
adequate results for either inertia or silhouette. DBSCAN, on the other hand,
does not have this condition and is able to detect non-convex clusters so long
as they are dense enough. Figure~\ref{fig:dbscan-inds} shows the clustering
found by each method and the respective convex and concave hulls of the clusters
found. The `concave hull' of a cluster is taken to be the \(\alpha\)-shape of
the cluster's data points~\cite{Edelsbrunner1983} where \(\alpha\) is determined
to be the smallest value such that all the points in the cluster are contained
in a single polygon. The convexity of cluster \(Z_j\), denoted
\(\mathcal{C}_j\), is then determined to be the ratio of the area of its concave
hull, \(H_c\), to the area of its convex hull, \(H_v\)~\cite{Sonka1993}:
\begin{equation}
    \mathcal{C}_j := \frac{area(H_c)}{area(H_v)}
\end{equation}

With this definition, it should be clear that a perfectly convex cluster, such
as a single point or line, would have \(\mathcal{C}_j = 1\).

It can be seen that the convexity of the clustering found by \(k\)-means appears
to be higher than that by DBSCAN.\ This was apparent across all trials conducted
in this work and indicates that the condition for convex clusters is being
sought out during the optimisation process. Meanwhile, however, it is not clear
whether the performance of DBSCAN falls owing to its parameters or the method
itself. This is a point where parameter sweeping would prove most useful so as
to determine a crossing point for these two driving forces.

Now, to add to the discussion above, the inverse optimisation should be
considered. That is, using the same parameters, the datasets for which DBSCAN
outperforms \(k\)-means with respect to the silhouette coefficient are to be
investigated. This is equivalent to using \(-f\) as the fitness function
except with the same penalty of \(\infty\) for the case set out
in~(\ref{eq:dbscan-fitness}).

\begin{figure}[htbp]
    \centering
    \begin{tabular}{c}
        \includegraphics[width=\imgwidth]{Fig13-1.pdf}
        \\
        \includegraphics[width=\imgwidth]{Fig13-2.pdf}
    \end{tabular}
    \caption{%
        Progressions for difference in silhouette (DBSCAN-preferable) and
        dimension across 1000 epochs at 100 epoch intervals.
    }\label{fig:negative-prog}
\end{figure}

\begin{figure}[htbp]
    \centering
    \subfloat[][]{%
        \label{fig:neg-inds-k}
        \centering
        \includegraphics[width=\imgwidth]{Fig14a.pdf}
    }\\
    \subfloat[][]{%
        \label{fig:neg-inds-d}
        \centering
        \includegraphics[width=\imgwidth]{Fig14b.pdf}
    }
    \caption[]{%
        Representative individuals from a DBSCAN-preferable run with clustering
        by: \subref{fig:dbscan-inds-k} \(k\)-means; \subref{fig:dbscan-inds-d}
        DBSCAN.\ Concave and convex hulls illustrated by shading and outline
        respectively.
    }\label{fig:negative-inds}
\end{figure}

Figures~\ref{fig:negative-prog}~and~\ref{fig:negative-inds} show the same
summary as above with the revised fitness function. Inspecting the former, it is
seen that the best fitness found is worse than with the previous example. This,
in part, is due to the fact that \(k\)-means cannot find a clustering with
negative values as no clusters may overlap. It can, however, produce results
with small silhouette scores where the clusters are tightly packed. Hence, the
best fitness score is now \(-1\) whereas the worst is 2, still.

Note in the first two frames of Figure~\ref{fig:neg-inds-k} how \(k\)-means is
forced to split what is evidently a single cluster in two whereas DBSCAN is able
to identify the single cluster and the outlying noise
(Figure~\ref{fig:neg-inds-d}). The proximity of these clusters has then dragged
the silhouette score down for \(k\)-means. Referring to
Figure~\ref{fig:neg-inds-d}, this kind of behaviour is certainly preferable for
DBSCAN under these parameters: the beginning individuals are likely random
clouds (as seen in the rightmost two frames of the figure) and the simplest step
toward a fit dataset is one that maintains that vaguely dense body with minimal
noise points far from it.

As has already been stated, the software implementation of the EDO method
has been produced in line with the best practices of open source software
development and reproducible research. In aid of this, all of the source code
used in these examples (including to create the figures) has been archived
under the DOI
\href{https://doi.org/10.5281/zenodo.3492236}{10.5281/zenodo.3492236}.
Likewise, all of the data produced to support this case study have been archived
under the DOI
\href{https://doi.org/10.5281/zenodo.3492228}{10.5281/zenodo.3492228}.


\section{Exercise 01}
\subsection{}

\begin{frame}
\frametitleTC{Direct synthesis for set point tracking}
\framesubtitleTC{Some examples}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Example 1 (OK):
       \begin{displaymath}
        \begin{array}{lcl}
         P(z)              = \frac{(z-0.5)}{(z-0.4)^2},\,
         G_{yw}^{\circ}(z) = \frac{0.2}{z-0.8} &
         \Rightarrow &
         C(z)              = \frac{0.2(z-0.4)^2}{(z-1)(z-0.5)}.
        \end{array}
       \end{displaymath}
 \item Example 2 (\red{NO}, process pole outside the circle $\Rightarrow$ unstable hidden part):
       \begin{displaymath}
        \begin{array}{lcl}
         P(z)              = \frac{1}{\red{z-2}},\,
         G_{yw}^{\circ}(z) = \frac{0.2}{z-0.8} &
         \Rightarrow &
         C(z)              = \frac{0.2\red{(z-2)}}{z-1}.
        \end{array}
       \end{displaymath}
 \item Example 3 (\red{NO}, infeasible relative degree):
       \begin{displaymath}
        \begin{array}{lcl}
         P(z)              = \frac{1}{(z-0.4)^2},\,
         G_{yw}^{\circ}(z) = \frac{0.2}{z-0.8} &
         \Rightarrow &
         C(z)              = \frac{0.2(z-0.4)^{\textcolor{red}{2}}}{z-1}.
        \end{array}
       \end{displaymath}
 \item Try one/two cases of your own, verify that everything is clear,\\
       ask questions if necessary.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Direct synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item If the process has poles on or outside the circle, we must preserve them in the desired loop transfer
       function
       \begin{displaymath}
        L^{\circ}(z) = \frac{G_{yw}^{\circ}(z)}{1-G_{yw}^{\circ}(z)}
       \end{displaymath}
       so as to not have $C(z)$ cancel them and generate an unstable hidden part.
 \item If the process has zeroes on or outside the circle, we must include them in $G_{yw}^{\circ}(z)$\\
       -- hence in $L^{\circ}(z)$ -- so that $C(z)$ does not cancel them either; this may\\
       require to also add poles to have at least as many as there are zeroes.
 \item If the relative degree of $G_{yw}^{\circ}(z)$ is infeasible, we need to add ``fast''\\
       (near to the origin) poles and/or delay terms.
 \item\vfill Let us clarify with a few simulation examples.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Direct synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations -- Scilab script (1/3, copy{\&}paste to try at home)}
{\tiny
\def\baselinestretch{0.3}
\begin{verbatim}
// Direct synthesis examples -- set point tracking
clear; clc; z = %z;
// Reference and time vectors
w  = ones(1,30);
k  = 0:length(w)-1;
// Process 1, asymptotically stable, no zeroes
P1 = syslin('d',2/(z-0.5)^2);
// Infeasible relative degree
To = syslin('d',0.2/(z-0.8));
R  = 1/P1*To/(1-To); disp(R); y1 = dsimul(tf2ss(To),w);
// Adding a fast pole for a realisable R
To = syslin('d',0.16/(z-0.8)/(z-0.2));
R  = 1/P1*To/(1-To); disp(R); y2 = dsimul(tf2ss(To),w);
// Adding a faster pole
To = syslin('d',0.19/(z-0.8)/(z-0.05));
R  = 1/P1*To/(1-To); disp(R); y3 = dsimul(tf2ss(To),w);
// Adding a one-step delay
To = syslin('d',0.2/(z-0.8)/z);
R  = 1/P1*To/(1-To); disp(R); y4 = dsimul(tf2ss(To),w);
// Using two poles, both faster than the design one
p1 = 0.75;
p2 = 0.45;
To = syslin('d',(1-p1)*(1-p2)/(z-p1)/(z-p2));
R  = 1/P1*To/(1-To); disp(R); y5 = dsimul(tf2ss(To),w);
\end{verbatim}
}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Direct synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations -- Scilab script (2/3)}
{\tiny
\def\baselinestretch{0.3}
\begin{verbatim}
// Process 2, asymptotically stable,, 1 zero outside the circle
P2 = syslin('d',(z-2)/(z-0.5)^2);
// Infeasible To
To = syslin('d',0.2/(z-0.8));
R  = 1/P2*To/(1-To); disp(R); y6 = dsimul(tf2ss(To),w);
// Adding the required zero to To
To = syslin('d',-0.18*(z-2)/(z-0.8)/(z-0.1));
R  = 1/P2*To/(1-To); disp(R); y7 = dsimul(tf2ss(To),w);
// Improving by acting on the To poles
p1 = 0.78;
p2 = 0.02;
To = syslin('d',-(1-p1)*(1-p2)*(z-2)/(z-p1)/(z-p2));
R  = 1/P2*To/(1-To); disp(R);
y8 = dsimul(tf2ss(To),w);
\end{verbatim}
}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Direct synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations -- Scilab script (3/3)}
{\tiny
\def\baselinestretch{0.3}
\begin{verbatim}
// Plot the results for process 1
h               = scf(0); clf;
h.figure_size   = [500,500];
title("${\Large \text{Cases 1--5}$");
xlabel("k");
plot(k,y1,'r',k,y1,'r.');
plot(k,y2,'b',k,y2,'b.');
plot(k,y3,'m',k,y3,'m.');
plot(k,y4,'g',k,y4,'g.');
plot(k,y5,'k',k,y5,'k.');
ax              = gca();
ax.data_bounds  = [0,0;max(k),1.1];
ax.tight_limits = "on";
// Plot the results for process 2
h               = scf(1); clf;
h.figure_size   = [500,500];
title("${\Large \text{Cases 6--8}$");
xlabel("k");
plot(k,y6,'r',k,y6,'r.');
plot(k,y7,'b',k,y7,'b.');
plot(k,y8,'k',k,y8,'k.');
ax              = gca();
ax.data_bounds  = [0,-0.3;max(k),1.1];
ax.tight_limits = "on";
\end{verbatim}
}
\end{frame}

\begin{frame}
\frametitleTC{Synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations -- summary}
\myPause
\begin{center}
 {\scriptsize
 \begin{tabular}{llll}
  \hline
Case & Process                   & Requirement                                                           & Controller  \\
\hline\hline
1& $P_1(z)=\frac{2}{(z-0.5)^2}$  & $G_{yw,11}^{\circ}(z)=\frac{0.2}{z-0.8}$                              & Infeasible  \\
2&                               & $G_{yw,12}^{\circ}(z)=\frac{0.16}{(z-0.8)(z-0.2)}$                    & $C_{12}(z)$ \\
3&                               & $G_{yw,13}^{\circ}(z)=\frac{0.19}{(z-0.8)(z-0.05)}$                   & $C_{13}(z)$ \\
4&                               & $G_{yw,14}^{\circ}(z)=\frac{0.2}{z(z-0.8)}$                           & $C_{14}(z)$ \\
5&                               & $G_{yw,15}^{\circ}(z)=\frac{(1-0.75)(1-0.45)}{(z-0.75)(z-0.45)}$      & $C_{15}(z)$ \\
\hline
6& $P_2(z)=\frac{z-2}{(z-0.5)^2}$& $G_{yw,21}^{\circ}(z)=\frac{0.2}{z-0.8}$                              & Infeasible  \\
7&                               & $G_{yw,22}^{\circ}(z)=-\frac{0.18(z-2)}{(z-0.8)(z-0.1)}$              & $C_{22}(z)$ \\
8&                               & $G_{yw,23}^{\circ}(z)=-\frac{(1-0.78)(1-0.02)(z-2)}{(z-0.78)(z-0.02)}$& $C_{23}(z)$ \\
  \hline
 \end{tabular}
 }
\end{center}
\end{frame}

\begin{frame}
\frametitleTC{Synthesis for set point tracking}
\framesubtitleTC{Dealing with the limitations -- results}
\myPause
\begin{center}
 {\scriptsize
 \begin{tabular}{ll}
  \includegraphics[width=0.35\textwidth]{./Unit-05/img/PS02-ex01-res-1to5.pdf} &
  \includegraphics[width=0.35\textwidth]{./Unit-05/img/PS02-ex01-res-6to8.pdf}      \\
   \red{$P_1,G_{yw,11}^{\circ}\Rightarrow$ infeasible} &  \red{$P_2,G_{yw,21}^{\circ}\Rightarrow$ infeasible} \\
  \blue{$P_1,G_{yw,12}^{\circ}\Rightarrow$ $C_{12}$}   & \blue{$P_2,G_{yw,22}^{\circ}\Rightarrow$ $C_{22}$}   \\
   \mgt{$P_1,G_{yw,13}^{\circ}\Rightarrow$ $C_{13}$}   &       $P_2,G_{yw,23}^{\circ}\Rightarrow$ $C_{23}$    \\
   \grn{$P_1,G_{yw,14}^{\circ}\Rightarrow$ $C_{14}$}   \\
        $P_1,G_{yw,15}^{\circ}\Rightarrow$ $C_{15}$    \\
 \end{tabular}
 }
\end{center}
\end{frame}

\begin{frame}
\frametitleTC{Synthesis for set point tracking}
\framesubtitleTC{Summary}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The technique is powerful, however there are \TC{objective} limits.
 \item In many of the cases seen the controller has one or two zeroes and one or two poles,
       one of which in $z=1$.
 \item We shall soon call this a PI(D).
 \item \vfill You can try at home: open Scilab, launch the code editor SciNotes\\
       from the \texttt{Applications} menu, enter the code from the previous\\
       slides (copy{\&}paste) and run with the \texttt{Execute} command\\
       --- all IDEs look pretty much the same, in the end...
 \end{itemize}
\end{frame}