\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|.


\subsection{Maxout layer}

Single node, spits out the max of all inputs.


\ignore{
\documentstyle[11pt]{report}
\textwidth 13.7cm
\textheight 21.5cm
\newcommand{\myimp}{\verb+ :- +}
\newcommand{\ignore}[1]{}
\def\definitionname{Definition}

\makeindex
\begin{document}
}

\chapter{\label{chapter:nn}The \texttt{nn} (Neural Networks) Module}

The \texttt{nn} module provides a high-level interface between Picat and the FANN\footnote{\url{http://leenissen.dk/fann/wp/}} neural networks library, which implements feedforward neural networks.\footnote{The Picat-FANN interface was implemented by Sanders Hernandez.}  A feedforward neural network consists of neurons organized in layers from an input layer to an output layer, possibly with a number of hidden layers. Neurons in a layer (except for the input layer) are connected neurons in the previous layer. The connections have weights. A feedforward network represents a function from input to output. The neurons in the input layers receive the input. The information is propagated forward through the layers until it reaches the output layers, where the output is returned. The information that a neuron receives is determined by the connected predecessor neurons, the weights of the connections, and an activation function. The connection weights of a neural network are normally adjusted through training on a given set of input-output pairs, called training data. Once a neural network is trained, it can be used to predict the output for a given input.

The following gives an example program which creates a neural network for the \texttt{xor} function,  and trains it on a set of data stored in a file:
\begin{verbatim}
import nn.

main =>
    NN = new_nn({2,3,1}),
    nn_train(NN,"xor.data"),
    nn_save(NN,"xor.net"),
    nn_destroy_all.
\end{verbatim}
The function \texttt{new\_nn(\{2,3,1\})} returns a neural network with three layers, where the input layer has 2 neurons, the hidden layer has 3 neurons, and the output layer has 1 neuron. The program does not specify any activation functions used between layers, entailing that the default activation function, which is \texttt{sym\_sigmoid}, will be used. The predicate \texttt{nn\_train(NN,"xor.data")} trains the neural network with the training data stored in the file \texttt{"xor.data"}. The user is able to specify an algorithm to be used in the training and several parameters that affect the behavior of the algorithm, such as the maximum number of iterations (called \textit{epoches}), the learning rate, and the error function. This example does not specify a training algorithm or any of the training parameters, entailing that the default algorithm, which is \texttt{rprop}, is used with the default setting. The predicate \texttt{nn\_save} saves the trained neural network into a file named \texttt{"xor.net"}. The \texttt{nn\_destroy\_all}clears up the neural network and the internal data used during training.

The text file \texttt{"xor.data"} contains the following training data:
\begin{verbatim}
4 2 1
-1 -1
-1
-1 1
1
1 -1
1
1 1
-1
\end{verbatim}
The three integers in the first line states, respectively, that the number of input-output pairs is 4, the number of input values is 2, and the number of output values is 1. The remaining lines give the input-output pairs.

The following program performs the same task as the above program, except that it trains the neural network with internal data:
\begin{verbatim}
import nn.

main =>
    NN = new_nn({2,3,1}),
    nn_train(NN,[({-1,-1}, -1),
                 ({-1,1}, 1),
                 ({1,-1}, 1),
                 ({1,1}, -1)]),
    nn_save(NN,"xor.net"),
    nn_destroy_all.
\end{verbatim}
The predicate \texttt{nn\_train} is overloaded. When the second argument is a file name, Picat reads training data from the file. Otherwise, Picat expects a list of input-output pairs.

The following example program illustrate how to use a trained network:
\begin{verbatim}
import nn.

main =>
    NN = nn_load("xor.net"),
    printf("xor(-1,1) = %w\n",nn_run(NN,{-1,1})),
    nn_destroy_all.
\end{verbatim}
The function \texttt{nn\_load} loads a neural network. The function \texttt{nn\_run} uses a network to predict the output for an input.

\section{Create and Destroy Neural Networks}
\begin{itemize}
\item \texttt{new\_nn($Layers$) = $NN$}\index{\texttt{new\_nn/1}}: This function creates a fully connected neural network of the structure $Layers$, which is a list or an array of positive integers. For example, the function \texttt{new\_nn(\{2,3,1\})} returns a neural network that has an input layer of 2 neurons, a hidden layer of 3 neurons, and an output layer of 1 neuron.

\item \texttt{new\_standard\_nn($Layers$) = $NN$}\index{\texttt{new\_standard\_nn/1}}: This function is the same as \texttt{new\_nn($Layers$)}.

\item \texttt{new\_sparse\_nn($Layers$,$Rate$) = $NN$}\index{\texttt{new\_sparse\_nn/2}}: This function creates a sparse neural network that has the structure $Layers$ and the connection rate $Rate$. The connection rate determines the sparseness of the network, with 1 indicating that the network is fully connected, and 0 indicating that the network is not connected at all.

\item \texttt{new\_shortcut\_nn($Layers$) = $NN$}\index{\texttt{new\_shortcut\_nn/1}}: This function creates a fully connected neural network of the structure $Layers$ that has shortcut connections between neurons in each layer and all neurons in later layers.

\item \texttt{nn\_destroy($NN$)}\index{\texttt{nn\_destroy/1}}: This predicate destroys the neural network $NN$.

\item \texttt{nn\_destroy\_all}\index{\texttt{nn\_destroy\_all/0}}: This predicate destroys all the neural networks.
\end{itemize}

\section{Activation Functions}
An activation function for a neuron determines how information is propagated to it from its predecessor neurons. When a new neural network is created, it uses the default activation function \texttt{sym\_sigmoid} for all of its non-input neurons. The following predicates can be utilized to set activation functions.

\begin{itemize}
\item \texttt{nn\_set\_activation\_function\_layer($NN$, $Func$, $Layer$)}\index{\texttt{nn\_set\_activation\_function\_layer}}: This predicate sets the activation function to $Func$ for all the neurons in layer $Layer$ in the neural network $NN$. Let the number of layers in $NN$ is $n$. Since the first layer is numbered 1, $Layer$ must satisfy $2\ \le\ Layer\ \le n$. The following activation functions are available:
\begin{itemize}
\item \texttt{linear}
\item \texttt{threshold}
\item \texttt{sym\_threshold}: symetric threshold.
\item \texttt{sigmoid}
\item \texttt{step\_sigmoid}: stepped sigmoid
\item \texttt{sym\_sigmoid}: symetric threshold
\item \texttt{gaussian}
\item \texttt{sym\_gaussian}: symetric gaussian
\item \texttt{elliot}: an alternative for sigmoid
\item \texttt{sym\_elliot}: symetric elliot
\item \texttt{linear\_piece}
\item \texttt{sym\_linear\_piece}: symetric linear piece
\item \texttt{sin}
\item \texttt{sym\_sin}: symetric sin
\item \texttt{cos}
\item \texttt{sym\_cos}: symetric cos
\end{itemize}
\end{itemize}
The detault activation function is \texttt{sym\_sigmoid}. Each of these functions has a correspondidng name in FANN. Please refer to FANN documentation for a more detailed description of these functions.

\item \texttt{nn\_set\_activation\_function\_hidden($NN$, $Func$)}\index{\texttt{nn\_set\_activation\_function\_hidden}}: This predicate sets the activation function to $Func$ for all of the hidden layers in the neural network $NN$.

\item \texttt{nn\_set\_activation\_function\_output($NN$, $Func$)}\index{\texttt{nn\_set\_activation\_function\_output}}: This predicate sets the activation function to $Func$ for the output layer in the neural network $NN$.

\item \texttt{nn\_set\_activation\_steepness\_layer($NN$, $Steepness$, $Layer$)}\index{\texttt{nn\_set\_activation\_steepness\_layer}}: This predicate sets the activation steepness for all of the neurons in layer number layer, where $-1\ \le\ Steepness\ \le 1$ and $2 \le Layer n$ (n is the number of layers in $NN$).  A high steepness value means a more aggressive training. The default steepness is 0.5.

\item \texttt{nn\_set\_activation\_steepness\_hidden($NN$, $Steepness$)}\index{\texttt{nn\_set\_activation\_steepness\_hidden}}: This predicate sets the activation steepness to $Steepness$ for all of the hidden layers in the neural network $NN$.

\item \texttt{nn\_set\_activation\_steepness\_output($NN$, $Steepness$)}\index{\texttt{nn\_set\_activation\_steepness\_output}}: This predicate sets the activation steepness to $Steepness$ for the output layer in the neural network $NN$.

\section{Training Data}
A training dataset can be supplied to FANN either through a text file or a Picat collection. A training dataset file must have the following format:

\begin{verbatim}
num_train_data num_input num_output
inputdata separated by space
outputdata separated by space

.
.
.

inputdata separated by space
outputdata separated by space
\end{verbatim}
A training dataset stored in a Picat collection must be either a list or an array of input-output pairs. An input-output pair has the form \texttt{($Input$,$Output$)}, where $Input$ is an array of numbers or a single number, and so is $Output$.

\begin{itemize}
\item \texttt{nn\_train\_data\_size($Data$) = $Size$}\index{\texttt{nn\_train\_data\_sizen/1}}: This function returns the number of input-output pairs in the dataset $Data$, which is either a file name or a Picat collection.

\item \texttt{nn\_train\_data\_get($Data$, $I$) = $Pair$}\index{\texttt{nn\_train\_data\_get/2}}: This function returns the $I$th pair in the dataset $Data$. Notice that while this function takes linear time when $Data$ is a list, it takes constant time when $Data$ is a file name or an array.\footnote{The data is loaded into an internal array when the dataset if accessed the first time.}
\end{itemize}
\end{itemize}

\section{Train Neural Networks}
\begin{itemize}
\item \texttt{nn\_train($NN$,$Data$,$Ops$)}\index{\texttt{nn\_train/3}}: This predicate trains the neural network $NN$ using the dataset in $Data$, which is either a text file name or a Picat collection, and under the control of training options $Ops$. The following traning options are supported:
\begin{itemize}
\item \texttt{maxep($X$)}: $X$ is the maximum epochs for training.
\item \texttt{report($X$)}:	reporting every $X$ number or epochs.
\item \texttt{derror($X$)}:	$X$ is the desired error in training.
\item \texttt{train\_func($X$)}: $X$ is the training algorithm, which can be one of the following: \texttt{batch}, \texttt{inc}, \texttt{qprop} (quick prop), \texttt{rprop} (, and \texttt{sprop}.
\item \texttt{lrate($X$)}: $X$ is the learning rate.
\item \texttt{momentum($X$)}: $X$ is the learning momentum.
\item \texttt{effor\_func($X$)}: $X$ is the error function, which can be either \texttt{linear} or \texttt{tanh}.
\item \texttt{stop\_func($X$)}:	$X$ is the stop function, which is either \texttt{bit} or \texttt{mse}.
\item \texttt{bfl($X$)}:  $X$ is the bit fail limit.
\item \texttt{qp\_decay($X$)}:	 $X$ is the \texttt{qprop} decay.
\item \texttt{qp\_mu($X$)} : $X$ is \texttt{qprop}  mu factor.
\item \texttt{rp\_increase($X$)}:	$X$ is the \texttt{rprop} increase factor.
\item \texttt{rp\_decrease($X$)}: $X$ is the \texttt{rprop} decrease factor.
\item \texttt{rp\_deltamin($X$)}: $X$ is the \texttt{rprop} delta min.
\item \texttt{rp\_deltama($X$)}: $X$ is the \texttt{rprop} delta max.
\item \texttt{rp\_deltazero($X$)}: $X$ is the  \texttt{rprop} delta zero.
\item \texttt{sp\_weight($X$)}:	$X$	 \texttt{sarprop} weight decay.
\item \texttt{sp\_thresh($X$)}: $X$	\texttt{sarprop} step error threshold factor.
\item \texttt{sp\_shift($X$)}: $X$	\texttt{sarprop} step error shift.
\item \texttt{sp\_temp($X$)}: $X$	\texttt{sarprop} temperature.
\item \texttt{scale(In\_min, In\_max, Out\_min, Out\_max)}: use training data scaling.
\item \texttt{inscale(in\_min, in\_max)}:  use input data scaling.
\item \texttt{outscale(out\_min, out\_max)}: use output data scaling.
\end{itemize}

\item \texttt{nn\_train($NN$,$Data$)}\index{\texttt{nn\_train/2}}: This predicate is the same as \texttt{nn\_train($NN$,$Data$,[])}.
\end{itemize}

\section{Save and Load Neural Networks}
\begin{itemize}
\item \texttt{nn\_save($NN$,$File$)}\index{\texttt{nn\_save/2}}: This predicate saves the neural network $NN$ to a file named $File$.

\item \texttt{nn\_load($File$) = $NN$}\index{\texttt{nn\_load/1}}: This function creates a neural network from a FANN neural network file named $File$.
\end{itemize}

\section{Run Neural Networks}
\begin{itemize}
\item \texttt{nn\_run($NN$, $Input$, $Opts$) = $Output$}\index{\texttt{nn\_run/3}}: This function predicts the output of a given input using the trained neural network $NN$, where $Input$ is an array of numbers or a single number, and $Opts$ is a list of testing options. The supported testing options include:
\begin{itemize}
\item \texttt{scaleIn($X$)}: $X$ indicates whether or not the input is scaled, with -1 meaning descale, 1 meaning scale, and 0 meaning nothing.
\item \texttt{scaleOut($X$)}: $X$ indicates whether or not the output is scaled,  with -1 meaning descale, 1 meaning scale, and 0 meaning nothing.
\item \texttt{resetMSE}: resets the current Mean Squared error of the network.
\end{itemize}

\item \texttt{nn\_run($NN$, $Input$) = $Output$}\index{\texttt{nn\_run/3}}: This function is the same as \texttt{nn\_run($NN$, $Input$, []).
\end{itemize}
\ignore{
\end{document}
}


\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