\documentclass[12pt,a4]{article}

\author{Yujie LU, Jiabao JI, Tong CHEN}
\newcommand{\handoutdate}{Friday, 2020-05-01}
\newcommand{\firstduedate}{Friday, 2020-05-08}
\newcommand{\finalduedate}{Tuesday, 2020-05-19}

\input{preamble}

\setcounter{section}{4}
\section{More on Network Flows}

\begin{exercise}
    Let $G = (V,c)$ be a flow network. Prove that flow is ``transitive'' in the following sense: if $r,s,t$ are vertices,
    and there is an $r$--$s$-flow of value $k$ and an $s$--$t$-flow of value $k$, then there is an $r$--$t$-flow of
    value $k$.
\end{exercise}

\begin{proof}
	Denote the original r-s-flow and s-t-flow as $f_{rs}$ and $f_{st}$ respectively.
    We prove $f_{rt} = k$ by contradiction.

    Suppose $f_{rt} < k$, denote $f_{rt} = p$. By Max-Flow-Min-Cut THM, there is a cut $C$
    with $cap(C) = p < k$. If $s \in C$, we know that $C$ is also a r-s-cut. But by Max-Flow-Min-Cut
    THM, min $cap(r-s-cut) = k > cap(C) = p$, which leads to a contradiction. If $s \notin C$, the proof
    is similar. So $f_{rt} \ge k$, so there is a flow of value k in between $r, t$.

\end{proof}

\subsection{Vertex Disjoint Paths}

Let $G$ be a directed graph. Two paths $p_1, p_2$ from $s$ to $t$ are called {\em vertex disjoint}
if they don't share any vertices except $s$ and $t$.

\begin{theorem}[Menger's Theorem]
   Let $G$ be a graph and $s \ne t$ two vertices therein. Let $k \in \mathbf{N}_0$.
   Then exactly one of the following is true:
   \begin{enumerate}
   \item There are $k$ vertex disjoint paths $p_1,\dots,p_k$ from $s$ to $t$; that is, no two $p_i$, $p_j$ share
   any vertex besides $s$ and $t$.
   \item There are vertices $v_1,\dots,v_{k-1} \in V \setminus \{s,t\}$ such that
   $G - \{v_1,\dots, v_{k-1}\}$ contains no $s$--$t$-path.
   \end{enumerate}
\end{theorem}

\begin{exercise}
   Prove Menger's Theorem. You have to prove two things: first, not both cases above can occur (this is rather easy);
   second, one of them must occur (this requires a tool from the lecture).
\end{exercise}
\begin{proof}

	First we prove the easy part, these two cases will not occur simultaneously.Prove it by contradiction.

    Suppose both cases are true simultaneously, then there are $k$ vertex disjoint paths $p_1, ..., p_k$ from $s$ to $t$.
    and there exists $v_1, ..., v_{k - 1}$ such that $G - \{ v_1, ...,v_{k - 1}\}$ contains no
    s-t-path.

    For any $v_1, ..., v_{k - 1}$, the can take place in at most $k - 1$ paths in $p_1,...,p_k$ since $p_1, ..., p_k$ are
    disjoint vertex paths. Then we know there must be at least one path from $p_1,..,p_k$ left, which connects $s$ and $t$,
    leading to a contradiction.

    Next we prove that one of them must occur.

   For each vertex b, other than $s$ or $t$, replace b by the 2-vertex graph $H(b)$ with vertex set $\{b^+, b^−\}$ and arc $b^−b^+$ with cap $1$. Replace $s$ by $s^+$ and $t$ by $t^−$. Replace each edge $e=ab$, $\{a,b\}$  by the arcs $a^+b^−$ with cap $\inf$.

	If the maxinum flow of the new graph $G'$ is less than $k$. According to MaxFlow-MinCut Theroem and the min-cut must choose the edge $b^-b^+$, we can choose one of the min-cut that makes the graph has no s-t path and the number of points is less than $k$. That satisfies the situation 1.

	Otherwise, the maxinum flow is at least $k$, that means there are at least $k$ augmenting path in new graph $G'$. And we can correspond one augmenting path in new graph to one vertex disjoint paths in origin graph. That satisfies the situation 2.
\end{proof}

Let $V = \{0,1\}^n$. The $n$-dimensional Hamming cube $H_n$ is the graph $(V,E)$ where
$\{u,v\} \in E$ if $u,v$ differ in exactly one coordinate.
Define the $\nth{i}$ level of $H_n$ as
\begin{align*}
  L_i := \{u \in V \ | \ \norm{u}_1 = i \} \ ,
\end{align*}
i.e., those vertices $u$ having exactly $i$ coordinates which are $1$.
\begin{center}
  \includegraphics[width=0.8\textwidth]{figures/hamming-3-dim.pdf}\\
  {\small The $3$-dimensional Hamming cube and the four
    sets $L_0$, $L_1$, $L_2$, $L_3$.}
\end{center}

\begin{exercise}[Matchings in $H_n$]
  Consider the induced bipartite subgraph $H_n[ L_i \cup L_{i+1}]$. This is
  the graph on vertex set $L_i \cup L_{i+1}$ where two edges are connected
  by an edge if and only if they are connected in $H_n$.
  \medskip

  Show that for $i \leq n/2$ the graph $H_n[ L_i \cup L_{i+1}]$
  has a matching of size $|L_i| = {n \choose i}$.

\begin{center}
  \includegraphics[width=0.4\textwidth]{figures/hamming-3-dim-matching.pdf}\\
  {\small A matching of size $3$ between $L_1$ and $L_2$.}
\end{center}
\label{exercise-matchings-in-H}
\end{exercise}
\begin{proof}
	On the left, sets of size $i$. On the right, sets of size $i + 1$, and an edge if the left vertex
is included in the right vertex. Note that every vertex on the left has $n-i$ neighbors, and every
vertex on the right has $i + 1$ neighbors.

	Let $S$ be any family of $L_i$. The number of edges coming out of S is $|S|(n-i)$, therefore,
the number of neighbors of S is at least $|S|(n-i)/(i+1) \ge |S|$. Hall’s theorem then completes
the proof.
\end{proof}
\begin{exercise}
  Let $H_n$ be the $n$-dimensional Hamming cube. For $i < n/2$ consider
  $L_i$ and $L_{n-i}$. Note that
  $|L_i| = {n \choose i} = { n \choose n-i}  = L_{n-i}$, so the
  $L_i$ and $L_{n-i}$ have the same size.   Show that there are ${n \choose i}$ paths $p_1,p_2,\dots,p_{ {n \choose i}}$
  in $H_n$ such that
  (i) each $p_i$ starts in $L_i$ and ends in $L_{n-i}$;
  (ii) two different paths $p_i,p_j$ do not share any vertices.
  \textbf{Hint 1.} Model this problem as a network flow with vertex capacities. What would
  the maximum flow be in this network?
  \textbf{Hint 2.} It's not {\em that} easy. If you try to work from both sides towards
  the middle by combining matchings between levels,
  you will certainly run into problems as how to glue things together in the middle.
  I have never seen any ``meet in the middle'' proof that works.
  \textbf{Hint 3.} There is a ``direct'' proof by induction that does not require anything
  about network flows.

\end{exercise}
\begin{proof}

  We can build a new network like this, create a source vertex $s$ and a terminal vertex $t$.
  Connect $s$ with all nodes in $L_i$ and $v$ with all nodes in $L_{n - i}$, leave all edges between
  $L_i, L_{i + 1}$ as they were. Assign all edges with capacity 1.

  The maximum flow in this network is no larger than $|L_i| = \tbinom{n}{i}$, since the number of edges connecting
  to $s$ is $|L_i|$ and they all have capacity 1. Then we give a flow with value $|L_i|$, this should
  tell us the maximum flow is $\tbinom{n}{i}$

  Here is an example.

  \begin{figure}[h]
    \centering
    \includegraphics[scale=0.3]{figures/2.png}
    \caption{network}
  \end{figure}

  For all edges between $L_j, L_{j + 1}$, we assign it with flow
  $\frac{C_{n}{i}}{C_{n}{j + 1} (n - j)}$ as the edges between $L_j,L_{j + 1}$ is
  $C_{n}{j + 1}(n - j)$. Notice that $L_{j + 1}$ has $C_{n}{j + 1}$ nodes and each
  node in $L_{j + 1}$ is connected with $(n - j)$ nodes in $L_j$.
  Since the graph is symmetrical, such flow can be built.

  Based on the proof of Menger's THM, we know that the maximum number of disjoint paths
  equals the maximum flow in this network. So there're $\tbinom{n}{i}$
  disjoint paths from $L_i$ to $L_{n - i}$.

\end{proof}

\subsection{Matchings and Vertex Covers}

The following exercise was on the final exam of CS 499 (mathematical foundations of computer science) in spring 2019.
\begin{exercise}
    Let $\nu(G)$ denote the size of a maximum matching of $G$. Show that a bipartite graph $G$
    has at most $2^{\nu(G)}$ minimum vertex covers.
\end{exercise}
\begin{proof}

		Suppose the number of minimum vertex covers of graph $G$ is $m\left( G \right) $. We can easily
\begin{figure}[ht]
    \centering
	\incfig[0.5]{example}
    \caption{example}
    \label{fig:example}
\end{figure}
Now we prove that there could be no more than $2^{\nu \left( G \right) }$ minimum vertex cover.
Let $G = A \cup B$, since $G$ is a bipartite graph.
Assume that the maximum matching is $e_1,e_2,\ldots,e_t$, in which $t = \nu\left( G \right) $. By \textbf{Konig's Theorem} we know that the number of vertices in a minimum vertex cover is exactly $t$.

Plus we can prove that every vertex in $K$, the vertex cover, is an endpoint of a matched edge. Hence the result is obvious.
\end{proof}
Obviously, this is not  true for general (non-bipartite) graphs: the triangle $K_3$ has $\nu(K_3) = 1$ but it has
three minimum vertex covers. The five-cycle $C_5$ has $\nu(C_5) = 2$ but has five minimum vertex covers.

\begin{exercise}
   Is there a function $f: \mathbf{N}_0 \rightarrow \mathbf{N}_0$ such that every graph with $\nu(G) = k$ has
   at most $f(k)$ minimum vertex covers? How small a function $f$ can you obtain?
\end{exercise}
\begin{proof}
	Assume that there are $m $ connected componets $G_1,\ldots,G_{m}$, and each have $c_{i} $
	vertexes, hence the number of minimum vertex
cover is
\[
		S  = \prod_{i=1}^{m} c_{i} \le  f\left( k \right)
.\]
And suppose that connected component $G_{i}$ has maximum matching $k_{i}$.
We have $k_{i} \le  \frac{c_{i}}{2}$, therefore
\[
k = \sum_{i=1}^{m} k_i \le \frac{1}{2} \sum_{i=1}^{m} c_i
.\]
Plus
\[
k = \sum_{i=1}^{m} k_i \ge m
.\]
Hence we need to find the smallest function $f$ such that
 \[
		 \prod_{i=1}^{m} c_{i} \le   f\left( k \right)
		 \le f\left( \frac{1}{2} \sum_{i=1}^{m} c_i \right)
.\]
Notice that
\[
		\sum_{i=1}^{m} c_i \ge m \left( \prod_{i=1}^{m} c_i  \right) ^{\frac{1}{m}}
.\]
Let $t = \prod_{i=1}^{m} c_{i} $, we must have
\[
		t\le f\left( \frac{m}{2} t ^{\frac{1}{m}} \right)
.\]
Which is equivalent to find the largest $g = f^{-1}$ such that
\[
	  \forall m, mt ^{\frac{1}{m} } -	2g\left( t \right)  \ge 0
.\]
Thus $g$ cannot be polynomial, the maximum of $g$ could only be something like
$c \ln\left( x \right) $ i.e.
\[
		h\left( t \right) = m t ^{\frac{1}{m}} - 2c\ln\left( t \right) , \forall t\ge 1,m\ge 1
.\]
It easy to find the largest $c$ is  $\frac{1}{2}$. Hence the smallest $f$ is
$f\left( k \right) = e^{2k}$.

\end{proof}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Beamer Presentation
% LaTeX Template
% Version 1.0 (10/11/12)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND THEMES
%----------------------------------------------------------------------------------------

\documentclass{beamer}
\usepackage{animate}
\usepackage{float}
\usepackage{bm}
\usepackage{mathtools}
\usepackage{extarrows}

\newcommand{\ChoL}{\mathsf{L}}
\newcommand{\bx}{\mathbf{x}}
\newcommand{\ii}{\mathrm{i}}
\newcommand{\bxi}{\bm{\xi}}
\newcommand{\bmu}{\bm{\mu}}
\newcommand{\bb}{\mathbf{b}}
\newcommand{\bA}{\mathbf{A}}
\newcommand{\bJ}{\mathbf{J}}
\newcommand{\bB}{\mathbf{B}}
\newcommand{\bM}{\mathbf{M}}

\newcommand{\by}{\mathbf{y}}
\newcommand{\bw}{\mathbf{w}}

\newcommand{\bX}{\mathbf{X}}
\newcommand{\bY}{\mathbf{Y}}
\newcommand{\bs}{\mathbf{s}}
\newcommand{\sign}{\mathrm{sign}}
\newcommand{\bt}[0]{\bm{\theta}}
\newcommand{\bc}{\mathbf{c}}
\newcommand{\bzero}{\mathbf{0}}
\renewcommand{\bf}{\mathbf{f}}
\newcommand{\bu}{\mathbf{u}}
\newcommand{\bv}[0]{\mathbf{v}}

\mode<presentation> {

% The Beamer class comes with a number of default slide themes
% which change the colors and layouts of slides. Below this is a list
% of all the themes, uncomment each in turn to see what they look like.

%\usetheme{default}
%\usetheme{AnnArbor}
%\usetheme{Antibes}
%\usetheme{Bergen}
%\usetheme{Berkeley}
%\usetheme{Berlin}
%\usetheme{Boadilla}
%\usetheme{CambridgeUS}
%\usetheme{Copenhagen}
%\usetheme{Darmstadt}
%\usetheme{Dresden}
%\usetheme{Frankfurt}
%\usetheme{Goettingen}
%\usetheme{Hannover}
%\usetheme{Ilmenau}
%\usetheme{JuanLesPins}
%\usetheme{Luebeck}
\usetheme{Madrid}
%\usetheme{Malmoe}
%\usetheme{Marburg}
%\usetheme{Montpellier}
%\usetheme{PaloAlto}
%\usetheme{Pittsburgh}
%\usetheme{Rochester}
%\usetheme{Singapore}
%\usetheme{Szeged}
%\usetheme{Warsaw}

% As well as themes, the Beamer class has a number of color themes
% for any slide theme. Uncomment each of these in turn to see how it
% changes the colors of your current slide theme.

%\usecolortheme{albatross}
\usecolortheme{beaver}
%\usecolortheme{beetle}
%\usecolortheme{crane}
%\usecolortheme{dolphin}
%\usecolortheme{dove}
%\usecolortheme{fly}
%\usecolortheme{lily}
%\usecolortheme{orchid}
%\usecolortheme{rose}
%\usecolortheme{seagull}
%\usecolortheme{seahorse}
%\usecolortheme{whale}
%\usecolortheme{wolverine}

%\setbeamertemplate{footline} % To remove the footer line in all slides uncomment this line
%\setbeamertemplate{footline}[page number] % To replace the footer line in all slides with a simple slide count uncomment this line

%\setbeamertemplate{navigation symbols}{} % To remove the navigation symbols from the bottom of all slides uncomment this line
}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{soul}
\newcommand{\red}[1]{\textcolor{red}{#1}}
%
%\usepackage{graphicx} % Allows including images
%\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
%
%
%\usepackage{amsthm}
%
%\usepackage{todonotes}
%\usepackage{floatrow}
%
%\usepackage{pgfplots,algorithmic,algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
%\usepackage[toc,page]{appendix}
%\usepackage{float}
%\usepackage{booktabs}
%\usepackage{bm}
%
%\theoremstyle{definition}
%
\newcommand{\RR}[0]{\mathbb{R}}
%
%\newcommand{\bx}{\mathbf{x}}
%\newcommand{\ii}{\mathrm{i}}
%\newcommand{\bxi}{\bm{\xi}}
%\newcommand{\bmu}{\bm{\mu}}
%\newcommand{\bb}{\mathbf{b}}
%\newcommand{\bA}{\mathbf{A}}
%\newcommand{\bJ}{\mathbf{J}}
%\newcommand{\bB}{\mathbf{B}}
%\newcommand{\bM}{\mathbf{M}}
%\newcommand{\bF}{\mathbf{F}}
%
%\newcommand{\by}{\mathbf{y}}
%\newcommand{\bw}{\mathbf{w}}
%\newcommand{\bn}{\mathbf{n}}
%
%\newcommand{\bX}{\mathbf{X}}
%\newcommand{\bY}{\mathbf{Y}}
%\newcommand{\bs}{\mathbf{s}}
%\newcommand{\sign}{\mathrm{sign}}
%\newcommand{\bt}[0]{\bm{\theta}}
%\newcommand{\bc}{\mathbf{c}}
%\newcommand{\bzero}{\mathbf{0}}
%\renewcommand{\bf}{\mathbf{f}}
%\newcommand{\bu}{\mathbf{u}}
%\newcommand{\bv}[0]{\mathbf{v}}

\AtBeginSection[]
{
   \begin{frame}
       \frametitle{Outline}
       \tableofcontents[currentsection]
   \end{frame}
}

%----------------------------------------------------------------------------------------
%    TITLE PAGE
%----------------------------------------------------------------------------------------
\usepackage{bm}
\newcommand*{\TakeFourierOrnament}[1]{{%
\fontencoding{U}\fontfamily{futs}\selectfont\char#1}}
\newcommand*{\danger}{\TakeFourierOrnament{66}}

\title[AD]{Automatic Differentiation for Computational Engineering} % The short title appears at the bottom of every slide, the full title is only on the title page

\author[CME 216]{Kailai Xu and Eric Darve} % Your name
%\institute[] % Your institution as it will appear on the bottom of every slide, may be shorthand to save space
%{
%%ICME, Stanford University \\ % Your institution for the title page
%%\medskip
%%\textit{kailaix@stanford.edu}\quad \textit{darve@stanford.edu} % Your email address
%}
\date{}% Date, can be changed to a custom date
% Mathematics of PDEs

\begin{document}

%\usebackgroundtemplate{%
%\begin{picture}(0,250)
%\centering
%	{{\includegraphics[width=1.0\paperwidth]{../background}}}
%\end{picture}
%  }
%\usebackgroundtemplate{%
%  \includegraphics[width=\paperwidth,height=\paperheight]{figures/back}}
\begin{frame}

\titlepage % Print the title page as the first slide

%dfa
\end{frame}
%\usebackgroundtemplate{}

\section{Overview}

\begin{frame}
	\frametitle{Overview}

	\begin{itemize}
		\item Gradients are useful in many applications
	\begin{itemize}
	\item \red{Mathematical Optimization}
		$$\min_{x\in\RR^n} \; f(x)$$
Using the gradient descent method:
$$x_{n+1} = x_n - \alpha_n \nabla f(x_n)$$
	\item \red{Sensitivity Analysis}
	$$f(x+\Delta x) \approx f'(x) \Delta x$$
	\item  \red{Machine Learning}

	 Training a neural network using automatic differentiation (back-propagation).
	\item \red{Solving Nonlinear Equations} Solve a nonlinear equation $f(x) = 0$ using Newton's method
	$$x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$$
	\end{itemize}

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Terminology}

	\begin{itemize}

	\item Deriving and implementing gradients are a challenging and all-consuming process.

		\item Automatic differentiation: \red{a set of techniques to numerically evaluate the derivative of a function specified by a computer program} (Wikipedia). It also bears other names such as \red{autodiff}, \red{algorithmic differentiation}, \red{computational differentiation}, and \red{back-propagation}.

		\item There are a lot of AD softwares
		\begin{enumerate}
			\item TensorFlow and PyTorch: deep learning frameworks in Python
			\item Adept-2: combined array and automatic differentiation library in \texttt{C++}
			\item autograd: efficiently derivatives computation of NumPy code.
			\item ForwardDiff.jl, Zygote.jl: Julia differentiable programming packages
		\end{enumerate}

		\item This lecture: how to compute gradients using automatic differentiation (AD)

\begin{itemize}
	\item  Forward mode, reverse mode, and AD for implicit solvers
	\end{itemize}

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{AD Software}

	\begin{figure}[hbt]
  \includegraphics[width=1.0\textwidth]{figures/ad_performance}
\end{figure}

{\scriptsize \url{https://github.com/microsoft/ADBench}}

\end{frame}

\begin{frame}
	\frametitle{Finite Differences}

	$$f'(x) \approx \frac{f(x+h) - f(x)}{h},\quad f'(x)\approx \frac{f(x+h) - f(x-h)}{2h}$$
	\begin{itemize}
	\item Derived from the definition of derivatives
	\begin{equation*}
		f'(x) = \lim_{h\rightarrow 0} \frac{f(x+h)-f(x)}{h}
	\end{equation*}
	\item Conceptually simple.
	\item Curse of dimensionalties: to compute the gradients of $f:\RR^m \rightarrow \RR$, you need at least $\mathcal{O}(m)$ function evaluations.
	\item Huge numerical error: roundoff error.
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{Finite Difference}
\begin{equation*}
	f(x) = \sin(x) \quad f'(x) = \cos(x) \quad  x_0 = 0.1
\end{equation*}
	\begin{figure}[hbt]
	\centering
  \includegraphics[width=0.8\textwidth]{figures/roundoff}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Finite Difference}
	\begin{figure}[hbt]
	\centering
  \includegraphics[width=0.8\textwidth]{figures/fd_error}
\end{figure}
\vspace{-0.5cm}
\scriptsize{Baydin, A. G., Pearlmutter, B. A., Radul, A. A., \& Siskind, J. M. (2017). Automatic differentiation in machine learning: a survey. The Journal of Machine Learning Research, 18(1), 5595-5637.}
\end{frame}

\begin{frame}
	\frametitle{Symbolic Differentiation }

	\begin{itemize}
		\item Symbolic differentiation computes exact derivatives (gradients): there is no approximation error.
		\item It works by recursively applies simple rules to \textcolor{red}{symbols}
		\begin{align*}
			\frac{d}{dx}(c) &= 0  & \frac{d}{dx}(x) &= 1\\
			\frac{d}{dx}(u+v) &= \frac{d}{dx}(u) + \frac{d}{dx}(v) & \frac{d}{dx}(uv) &= v\frac{d}{dx}(u)+u\frac{d}{dx}(v)\\
			\ldots
		\end{align*}
		Here $c$ is a variable independent of $x$, and $u, v$ are variables dependent on $x$.

		\item There may not exist convenient expressions for the analytical gradients of some functions. For example, a blackbox function from a third-party library.

	\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Symbolic Differentiation}
\begin{itemize}
\item Symbolic differentiation can lead to complex and redundant expressions
\end{itemize}
	\begin{figure}[hbt]
		\centering
  \includegraphics[width=0.8\textwidth]{figures/symbolic}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Automatic Differentiation}

	\begin{itemize}
		\item AD is neither finite difference nor symbolic differentiation.
		 \item It works by recursively applies simple rules to \textcolor{red}{values}
		\begin{align*}
			\frac{d}{dx}(c) &= 0  & \frac{d}{dx}(x) &= 1\\
			\frac{d}{dx}(u+v) &= \frac{d}{dx}(u) + \frac{d}{dx}(v) & \frac{d}{dx}(uv) &= v\frac{d}{dx}(u)+u\frac{d}{dx}(v)\\
			\ldots
		\end{align*}
		Here $c$ is a variable independent of $x$, and $u, v$ are variables dependent on $x$.

		\item It evaluates numerically gradients of ``function units'' using symbolic differentiation, and chains the computed gradients using the chain rule
		$$\frac{df(g(x))}{dx} = f'(g(x)) g'(x)$$
		\item It is efficient (linear in the cost of computing the function itself) and numerically stable.
	\end{itemize}

\end{frame}

\section{Computational Graph}

\begin{frame}
	\frametitle{Computational Graph}

	\begin{itemize}
	\item The ``language'' for automatic differentiation is computational graph.
	\begin{itemize}
	\item The computational graph is a \red{directed acyclic graph (DAG)}.
	\item Each \red{edge} represents the data: a scalar, a vector, a matrix, or a high dimensional tensor.
	\item Each \red{node} is a function that consumes several incoming edges and outputs some values.
	\end{itemize}
	\begin{minipage}[c]{0.45\textwidth}
		\begin{align*}
  J &= f_4(\mathbf{u}_1, \mathbf{u}_2, \mathbf{u}_3, \mathbf{u}_4), \\
  \mathbf{u}_2 &= f_1(\mathbf{u}_1, \bm {\theta}), \\
       \mathbf{u}_3 &= f_2(\mathbf{u}_2, \bm {\theta}),\\
        \mathbf{u}_4 &= f_3(\mathbf{u}_3, \bm {\theta}).
\end{align*}
	\end{minipage}~
	\begin{minipage}[c]{0.45\textwidth}
		\includegraphics[width=1.0\textwidth]{../adjoint}
	\end{minipage}
	\item  Let's build a computational graph for computing
 $$z=\sin(x_1+x_2) + x_2^2x_3$$

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Building a Computational Graph}

	$$z=\sin(\red{x_1+x_2}) + \red{x_2^2}x_3$$

	\begin{figure}[hbt]
  \includegraphics[width=0.4\textwidth]{figures/fd1}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Building a Computational Graph}

	$$z=\red{\sin(x_1+x_2)} + \red{x_2^2x_3}$$

	\begin{figure}[hbt]
  \includegraphics[width=0.4\textwidth]{figures/fd2}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Building a Computational Graph}

	$$z=\red{\sin(x_1+x_2) + x_2^2x_3}$$

	\begin{figure}[hbt]
  \includegraphics[width=0.4\textwidth]{figures/fd3}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Computing Gradients from a Computational Graph}
	\begin{itemize}
		\item Automatic differentiation works by \textcolor{red}{propagating} gradients in the computational graph.
		\item Two basic modes: forward-mode and backward-mode. Forward-mode propagates gradients in the same direction as forward computation. Backward-mode propagates gradients in the reverse direction of forward computation.
		\end{itemize}
		\begin{figure}[hbt]
		\centering
  \includegraphics[width=1.0\textwidth]{figures/fb}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Computing Gradients from a Computational Graph}
	\begin{itemize}
	\item Different computational graph topologies call for different modes of automatic differentiation.
		\begin{itemize}
		\item One-to-many: forward-propagation$\Rightarrow$forward-mode AD.
		\begin{figure}[hbt]
  \includegraphics[width=0.6\textwidth]{figures/onetomany}
\end{figure}
		\item Many-to-one: back-propagation$\Rightarrow$reverse-mode AD.
			\end{itemize}
	\begin{figure}[hbt]
  \includegraphics[width=0.6\textwidth]{figures/manytoone}
\end{figure}
  \end{itemize}

\end{frame}

\section{Forward Mode}

\begin{frame}
	\frametitle{Automatic Differentiation: Forward Mode AD}

	\begin{itemize}
	\item The forward-mode automatic differentiation uses the chain rule to propagate the gradients.
	$$\frac{\partial f\circ g (x)}{\partial x} =  f'(g(x)) \red{ g'(x)}$$
		\item Compute in the same order as function evaluation.
		\item Each node in the computational graph
		\begin{itemize}
		\item \red{Aggregate} all the gradients from up-streams.
		\item \red{Forward} the gradient to down-stream nodes.
		\end{itemize}
	\end{itemize}

	\begin{figure}[hbt]
  \includegraphics[width=0.4\textwidth]{../fad}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Example: Forward Mode AD}
	\begin{itemize}
	\item 	Let's consider a specific way for computing
	\begin{equation*}
		f(x) = \begin{bmatrix}
			x^4\\
			x^2 + \sin(x) \\
			-\sin(x)
		\end{bmatrix}
	\end{equation*}
	\end{itemize}

	\pause

	\begin{minipage}[b]{0.45\textwidth}
	\includegraphics[width=1.0\textwidth]{../forwardmode}
\end{minipage}~
\begin{minipage}[b]{0.45\textwidth}
  \begin{align*}
\onslide<1->{(y_1, y_1')& = (x^2, 2x)} \\
\onslide<2->{(y_2, y_2') &= (\sin x, \cos x)} \\
\\
\onslide<3->{(y_3, y_3') &= (y_1^2, 2y_1y_1') = (x^4, 4x^3)}\\
 \onslide<4->{(y_4, y_4') &= (y_1+y_1, y_1'+y_2')} \\
\onslide<4->{&= (x^2+\sin x, 2x+\cos x)} \\
 \onslide<5->{(y_5, y_5')& = (-y_2, -y_2') = (-\sin x, -\cos x)}
 	\end{align*}
\end{minipage}
\end{frame}

\begin{frame}
	\frametitle{Summary}
	\begin{itemize}
		\item Forward mode AD reuses gradients from upstreams. Therefore, this mode is useful for few-to-many mappings
		$$f:\mathbb{R}^n\rightarrow \mathbb{R}^m, n\ll m$$
		\item Applications: sensitivity analysis, uncertainty quantification, etc.
		\begin{itemize}
		\item Consider a physical model $f:\mathbb{R}^n\rightarrow \mathbb{R}^m$, let $x\in \RR^n$ be the quantity of interest (usually a low dimensional physical parameter), uncertainty propagation method computes the perturbation of the model output (usually a large dimensional quantity, i.e., $m\gg 1$)
		$$f(x+\Delta x) \approx f(x) + f'(x) \Delta x$$
		\end{itemize}

	\end{itemize}
\end{frame}

\section{Reverse Mode}

\begin{frame}
	\frametitle{Reverse Mode AD}
	$$\frac{df(g(x))}{dx} = \red{f'(g(x))} g'(x)$$
	\begin{itemize}
		\item Computing in the reverse order of forward computation.
		\item Each node in the computational graph
		\begin{itemize}
		\item \red{Aggregates} all the gradients from down-streams
		\item \red{Back-propagates} the gradient to upstream nodes.
		\end{itemize}
		\begin{figure}[hbt]
  \includegraphics[width=0.4\textwidth]{../rad}
\end{figure}

	\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Example: Reverse Mode AD}
$$z=\sin(x_1+x_2) + x_2^2x_3$$
\begin{figure}[hbt]
\centering
  \includegraphics[width=0.5\textwidth]{figures/bd1}
\end{figure}

\end{frame}

\begin{frame}
\frametitle{Example: Reverse Mode AD}
$$z=\sin(x_1+x_2) + x_2^2x_3$$
\begin{figure}[hbt]
\centering
  \includegraphics[width=0.5\textwidth]{figures/bd2}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Example: Reverse Mode AD}
$$z=\sin(x_1+x_2) + x_2^2x_3$$
\begin{figure}[hbt]
\centering
  \includegraphics[width=0.5\textwidth]{figures/bd3}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Example: Reverse Mode AD}
$$z=\sin(x_1+x_2) + x_2^2x_3$$
\begin{figure}[hbt]
\centering
  \includegraphics[width=0.5\textwidth]{figures/bd4}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Summary}

	\begin{itemize}
		\item Reverse mode AD reuses gradients from down-streams. Therefore, this mode is useful for many-to-few mappings
		$$f:\mathbb{R}^n\rightarrow \mathbb{R}^m, n\gg m$$
		\item Typical application:
		\begin{itemize}
		\item Deep learning: $n=$ total number of weights and biases of the neural network, $m=1$ (loss function).
		\item Mathematical optimization: usually there are only a single objective function.
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Summary}

	\begin{itemize}
		\item In general, for a function $f:\RR^n \rightarrow \RR^m$
		 % Please add the following required packages to your document preamble:
% \usepackage{booktabs}
\begin{table}[]
\centering
\begin{tabular}{@{}llll@{}}
\toprule
Mode & Suitable for ... & Complexity\footnote{$\mathrm{OPS}$ is a metric for complexity in terms of fused-multiply adds.} & Application \\ \midrule
Forward & $m\gg n$ & $\leq 2.5\;\mathrm{OPS}(f(x))$ & UQ \\
Reverse & $m\ll n$ & $\leq 4\;\mathrm{OPS}(f(x))$ & Inverse Modeling \\ \bottomrule
\end{tabular}
\end{table}

		\item There are also many other interesting topics
		\begin{itemize}
		\item Mixed mode AD: many-to-many mappings.
		\item Computing sparse Jacobian matrices using AD by exploiting sparse structures.
		\end{itemize}
	\end{itemize}
	{\scriptsize Margossian CC. A review of automatic differentiation and its efficient implementation. Wiley Interdisciplinary Reviews: Data Mining and Knowledge Discovery. 2019 Jul;9(4):e1305.}
\end{frame}

\section{AD for Physical Simulation}

\begin{frame}
	\frametitle{The Demand for Gradients in Physical Simulation}

	\begin{figure}[hbt]
  \includegraphics[width=0.9\textwidth]{figures/illu.png}
\end{figure}

	\begin{itemize}
		\item Solving nonlinear equations
		\item Uncertainty quantification/sensitivity analysis
		\item \red{Inverse problems}
	\end{itemize}

	\scriptsize{Image source: \url{https://mirams.wordpress.com/2016/11/23/uncertainty-in-risk-prediction/}, \url{http://fourier.eng.hmc.edu/e176/lectures/ch2/node5.html}}
\end{frame}

\begin{frame}
	\frametitle{Inverse Problem and Mathematical Optimization}

	\begin{itemize}
		\item Consider a bar under heating with a source term $f(x,t)$. The right hand side has fixed temperature and the left hand side is insulated.
		\item The governing equation for the temperature $u(x,t)$ is
		\begin{align*}
			\frac{\partial u({x}, t)}{\partial t} &= \kappa(x)\Delta u({x}, t) + f({x}, t), \quad t\in (0,T), x\in \Omega\\
			u(1, t) &= 0 \quad t>0\\
			\kappa(0)\frac{\partial u(0,t)}{\partial x} &= 0 \quad t>0
		\end{align*}
		\item The diffusivity coefficient is given by
		$$\kappa(x) = a + bx$$
		where $a$ and $b$ are unknown parameters.
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Inverse Problem and Mathematical Optimization}
	\begin{itemize}
\item Goal: calibrate $a$ and $b$ from $u_0(t) = u(0, t)$
$$\kappa(x) = a + bx$$
	\end{itemize}
	\begin{figure}
		\centering
		\includegraphics[width=1.0\textwidth]{figures/measure}
	\end{figure}

\end{frame}
\begin{frame}
	\frametitle{Inverse Problem and Mathematical Optimization}
	\begin{itemize}
		\item This problem is a standard inverse problem. We can formulate the problem as a PDE-constrained optimization problem
		$$\begin{aligned}
\min_{a, b}\ & \int_{0}^t ( u(0, t)- u_0(t))^2 dt\\
\mathrm{s.t.}\ & \frac{\partial u(x, t)}{\partial t} = \kappa(x)\Delta u(x, t) + f(x, t), \quad t\in (0,T), x\in (0,1) \\
& -\kappa(0)\frac{\partial u(0,t)}{\partial x} = 0, t>0\\
& u(1, t) = 0, t>0\\
& u(x, 0) = 0, x\in [0,1]\\
& \kappa(x) = a x + b
\end{aligned}$$

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Numerical Partial Differential Equation}

	\begin{itemize}
		\item As with many physical modeling techniques, we discretize the PDE using numerical schemes. Here is a finite difference scheme for the PDE $k=1,2,\ldots,m, i=1,2,\ldots, n$
		$$\frac{u^{k+1}_i-u^k_i}{\Delta t} = \kappa_i \frac{u^{k+1}_{i+1}+u^{k+1}_{i-1}-2u^{k+1}_i}{\Delta x^2} + f_i^{k+1}$$
	\end{itemize}
	\begin{minipage}[c]{0.49\textwidth}
	For initial and boundary conditions, we have
		\begin{align*}
			-\kappa_1 \frac{u_2^{k}-u_0^k}{2\Delta x} &= 0\\
			u_{n+1}^k &= 0 \\
			u_i^0 &= 0
		\end{align*}
\end{minipage}~
\begin{minipage}[c]{0.49\textwidth}
  \includegraphics[width=1.0\textwidth]{figures/grid}
\end{minipage}

\end{frame}

\begin{frame}
	\frametitle{Numerical Partial Differential Equation}

	\begin{itemize}
		\item Rewriting the equation as a linear system, we have
		$$A(a,b)U^{k+1} = U^k + F^{k+1}, \quad U^k = \begin{bmatrix}u_1^k\\u_2^k\\\vdots \\u_n^k\end{bmatrix}$$
		Here $\lambda_i = -\kappa_i \frac{\Delta t}{\Delta x^2}$ and
		{\footnotesize
		\begin{equation*}
		A(a,b) = \begin{bmatrix}
2\lambda_1+1 & -2\lambda_1  &  & & \\
-\lambda_2 & 2\lambda_2+1 & -\lambda_2 & & \\
 & -\lambda_3 & 2\lambda_3 + 1 & -\lambda_3 & \\
& &\ddots & & \\
& & &\ddots & -\lambda_{n-1}\\
&&& -\lambda_n & 2\lambda_n+1
\end{bmatrix},\quad F^k = \Delta t \begin{bmatrix}
f_1^{k+1} \\
f_2^{k+1} \\
\vdots\\
f_n^{k+1}
\end{bmatrix}
		\end{equation*}
		}

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Computational Graph for Numerical Schemes}

	\begin{itemize}
		\item The discretized optimization problem is
		\begin{align*}
			\min_{a, b}& \; \sum_{k=1}^m (u^k_1 - u_0( (k-1)\Delta t))^2\\
			\text{s.t.} & \; A(a,b)U^{k+1} = U^k + F^{k+1}, k = 1, 2,\ldots, m \\
			& \; U^0 = 0
		\end{align*}
		\item The computational graph for the forward computation (evaluating the loss function) is
		\begin{figure}[hbt]
		\centering
  \includegraphics[width=0.3\textwidth]{figures/heatcg}
\end{figure}

	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Implementation using an AD system}

	\begin{figure}[hbt]
  \includegraphics[width=0.7\textwidth]{figures/simulation}
\end{figure}

\end{frame}

\section{AD Through Implicit Operators}

\begin{frame}
\frametitle{Challenges in AD}

	\begin{minipage}[t]{0.49\textwidth}
	\vspace{-3cm}
\begin{itemize}
	\item Most AD frameworks only deal with explicit operators, i.e., the functions that has analytical derivatives, or composition of these functions.
	\item Many scientific computing algorithms are \textcolor{red}{iterative} or \textcolor{red}{implicit} in nature.
\end{itemize}
\end{minipage}~
\begin{minipage}[t]{0.49\textwidth}
  \includegraphics[width=1.0\textwidth]{../sim.png}
\end{minipage}

	% Please add the following required packages to your document preamble:
% \usepackage{booktabs}
\begin{table}[]
\begin{tabular}{@{}lll@{}}
\toprule
Linear/Nonlinear & Explicit/Implicit & Expression   \\ \midrule
Linear           & Explicit          & $y=Ax$       \\
Nonlinear        & Explicit          & $y = F(x)$   \\
\textbf{Linear}           & \textbf{Implicit}          & $Ay = x$     \\
\textbf{Nonlinear}        & \textbf{Implicit}          & $F(x,y) = 0$ \\ \bottomrule
\end{tabular}
\end{table}
\end{frame}

\begin{frame}
	\frametitle{Example}

\begin{itemize}
	\item Consider a function $f:x\rightarrow y$, which is implicitly defined by
	$$F(x,y) = x^3 - (y^3+y) = 0$$
If not using the cubic formula for finding the roots, the forward computation consists of iterative algorithms, such as the Newton's method and bisection method
\end{itemize}

\begin{minipage}[t]{0.48\textwidth}
\centering
\begin{algorithmic}
\State $y^0 \gets 0$
\State $k \gets 0$
\While {$|F(x, y^k)|>\epsilon$}
\State $\delta^k \gets F(x, y^k)/F'_y(x,y^k)$
\State $y^{k+1}\gets y^k - \delta^k$
\State $k \gets k+1$
\EndWhile
\State \textbf{Return} $y^k$
\end{algorithmic}
\end{minipage}~
\begin{minipage}[t]{0.48\textwidth}
\centering
\begin{algorithmic}
\State $l \gets -M$, $r\gets M$, $m\gets 0$
\While {$|F(x, m)|>\epsilon$}
\State $c \gets \frac{a+b}{2}$
\If{$F(x, m)>0$}
\State $a\gets m$
\Else
\State $b\gets m$
\EndIf
\EndWhile
\State \textbf{Return} $c$
\end{algorithmic}

\end{minipage}

\end{frame}

\begin{frame}
	\frametitle{Example}

	\begin{itemize}
		\item An efficient way is to apply the \textcolor{red}{implicit function theorem}. For our example, $F(x,y)=x^3-(y^3+y)=0$, treat $y$ as a function of $x$ and take the derivative on both sides
		$$3x^2 - 3y(x)^2y'(x)-1=0\Rightarrow y'(x) = \frac{3x^2-1}{3y(x)^2}$$
	The above gradient is \textcolor{red}{exact}.
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{Implicit Operators in Physical Modeling}
	\begin{itemize}
		\item Return to our bar problem, what if the material property is complex and has a temperature-dependent governing equation?
		$$\frac{\partial u({x}, t)}{\partial t} = \red{\kappa(u)}\Delta u({x}, t) + f({x}, t), \quad t\in (0,T), x\in \Omega$$
		\item An implicit scheme is usually a nonlinear equation, and requires an iterative solver (e.g., the Newton-Raphson algorithm) to solve
		$$\frac{u^{k+1}_i-u^k_i}{\Delta t} = \red{\kappa(u_i^{k+1})} \frac{u^{k+1}_{i+1}+u^{k+1}_{i-1}-2u^{k+1}_i}{\Delta x^2} + f_i^{k+1}$$
		\item Typical AD frameworks cannot handle this operator. We need to differentiate through implicit operators.
		\item This topic will be covered in a future lecture: \red{physics constrained learning}.
	\end{itemize}

\end{frame}

\section{Conclusion}

\begin{frame}
	\frametitle{Conclusion}

	\begin{itemize}
		\item What's covered in this lecture
		\begin{itemize}
		\item Reverse mode automatic differentiation;
		\item Forward mode automatic differentiation;
		\item Using AD to solver inverse problems in physical modeling;
		\item Automatic differentiation through implicit operators.
		\end{itemize}
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{What's Next}

	\begin{itemize}
	\item Physics constrained learning: inverse modeling using automatic differentiation through implicit operators;
	\item Neural networks and numerical schemes: substitute the unknown component in a physical system with a neural network and learn the neural network with AD;
	\item Implementation of inverse modeling algorithms in ADCME.
	\end{itemize}
\end{frame}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS110-2015S: Introduction to Computing
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS110-2015S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \topDirectory {.}
\def \texDirectory {\topDirectory/src/main/tex}

\documentclass[12pt,letterpaper,twoside]{article}
\usepackage{\texDirectory/template/style/directives}
\usepackage{\texDirectory/template/style/assignment}
\input{\texDirectory/template/config}

\begin{document}

\doc{title}{Solution to Quiz 5}
\doc{date-pub}{May 05, 2015 at 01:00 PM}
\doc{date-due}{May 08, 2015 at 11:00 PM}
\doc{points}{4}

\prepare{header}

\section*{Question 1}

Write a program \texttt{QuadraticSolver.java} that asks for three real numbers $a$, $b$ and $c$ from the user and uses a method \texttt{findSolution()} with given method signature to solve for $x$ with three digits of precision, the quadratic expression shown in Equation \ref{eq2}.
\begin{equation}
ax^2+bx+c=0
\label{eq2}
\end{equation}

\lstset{language=java,tabsize=2,numbers=none}
\begin{lstlisting}
private static double[] findSolution(double[] coefficients) throws NegativeDiscriminantException, ArithmeticException;
\end{lstlisting}

Your program is expected to maintain its control flow regardless of the user entries. User should be asked for inputs as long as his previous entries are not acceptable or result in a negative discriminant.

\subsection*{Solution}

\begin{enumerate}
\item Class \texttt{NegativeDiscriminantException.java}
\lstset{language=java, tabsize=2, numbers=left}
\begin{lstlisting}
// As there is no built-in exception to be thrown
// when discriminant of a polynomial expression is negative
// we should build our own exception
// and make sure it extends from class Exceptions
public class NegativeDiscriminantException extends Exception {
	// a simple attribute to show a message to user when exception happens
	private String message;
	// a public constructor to allow instantiation of exceptions
	public NegativeDiscriminantException(String message) {
		this.message = message;
	}
	// a simple getter to override the getMessage() method
	// which comes with class Exception
	public String getMessage() {
		return message;
	}
}
\end{lstlisting}
\item Class \texttt{QuadraticSolver.java}
\begin{lstlisting}
import java.util.InputMismatchException;
import java.util.Scanner;
public class QuadraticSolver {
	private static double[] findSolution(double[] coefficients) throws NegativeDiscriminantException, ArithmeticException {
		// compute discriminant (this is safe)
		double discriminant = Math.pow(coefficients[1],2)-(4*coefficients[0]*coefficients[2]);
		// throw Exception when discriminant is negative
		if (discriminant < 0)
			throw new NegativeDiscriminantException("Discriminant is Negative");
		// throw Exception when coefficient a is zero
		else if (coefficients[0] == 0) {
			throw new ArithmeticException("Coefficient 'a' cannot be zero.");
		}
		// If we have only one solution
		else if (discriminant == 0) {
			double[] output = new double[1];
			output[0] = -coefficients[1]/(2*coefficients[0]);
			return output;
		}
		// If we have two solutions
		else {
			double[] output = new double[2];
			output[0] = ( -coefficients[1] + Math.sqrt(discriminant) )/( 2*coefficients[0] );
			output[1] = ( -coefficients[1] - Math.sqrt(discriminant) )/( 2*coefficients[0] );
			return output;
		}
	} // end of findSolution() method
	public static void main(String[] args) {
		// a Scanner object to read user input
		Scanner input = new Scanner(System.in);
		// an array of size three for coefficients
		double[] params = new double[3];
		// an uninitialized array to store solutions given by findSolution method
		double[] solutions;
		// use a while loop to repeat asking for numbers in case required
		while (true) {
			// surround code with try block to handle possible exceptions
			try {
				// ask for three coefficients a, b and c
				for (int i = 0; i < params.length; i++) {
					System.out.printf("Enter Coefficient %d: ",i);
					// ask the user to input double
					// this might throw InputMismatchException
					params[i] = input.nextDouble();
				}
				// find solutions for expression formed based on user input
				// this might throw NegativeDiscriminantException or ArithmeticException
				solutions = findSolution(params);
				// if control flow reaches this part, it means no exception has happened we're good to go out of while loop
				break;
			}
			// Handle InputMismatchException
			catch (InputMismatchException e) {
				// clear previous tokens
				input.next();
				// for InputMismatchException, we cannot use e.getMessage()
				System.out.println("Please enter coefficients of type double");
				System.out.println("Please Retry.\n");
			}
			// Handle Exceptions thrown by method findSolution()
			catch (NegativeDiscriminantException|ArithmeticException e) {
				System.out.println(e.getMessage());
				System.out.println("Please Retry.\n");
			}
		}
		// close the Scanner object
		input.close();
		// print the solutions with three digits of precision
		System.out.println("Solutions are: ");
		for (int i = 0; i < solutions.length; i++)
			System.out.printf("%.3f\n",solutions[i]);
	}
}
\end{lstlisting}
\end{enumerate}

\end{document}


\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{esint}
\usepackage{babel}
\begin{document}

\section{Derivation}
HJB is
$$0 = \rho \theta V_t(\frac{C_t^{\frac{1-\gamma}{\theta}}}{((1-\gamma)V_t)^{\frac{1}{\theta}}}-1) + \frac{E[dV_t]}{dt}$$
Let's define $G_t$ such that
$$V_t = \frac{C_t^{1-\gamma}}{1-\gamma} G_t$$
By Ito,
$$0 = \rho\theta(G_t^{1-\frac{1}{\theta}}-G_t)   +  G_t E\frac{\frac{dC_t^{1-\gamma}}{C_t^{1-\gamma}}}{dt} + E\frac{dG_t}{dt} + E\frac{dG_t\frac{dC_t^{1-\gamma}}{C_t^{1-\gamma}}}{dt}$$
Denoting $\mu_{C}$ and $\sigma_{C}$ the geometric drift of $C_t$, we have
\begin{align*}
	\frac{dC_{t}^{1-\gamma}}{C_{t}^{1-\gamma}}=((1-\gamma)\mu_{C}-\frac{1}{2}(1-\gamma)\gamma\sigma^{2}_{C})dt + (1-\gamma)\sigma_{C}dW_t
\end{align*}
Injecting this expression into HJB and denoting $\mu_G, \sigma_G$ the arithmetic drift and volatility of $G_t$
$$0 = \rho \theta (G_t^{1-\frac{1}{\theta}}-G_t)  + G_t ((1-\gamma) \mu_{C} - \frac{1}{2}(1-\gamma)\gamma\sigma_{C}'\sigma_{C}) +  \mu_G + \sigma_G'(1-\gamma)\sigma_{C}$$

\section{Long run risk model}

\subsection{Derivation}
We now assume that the evolution of consumption is driven by two state variables $\mu_{t}$ and $\sigma_{t}$:
\begin{align*}
	\frac{dC_{t}}{C_{t}} & =  \mu_{t}dt+\nu_{D}\sqrt{\sigma_{t}}dZ_{t}\\
	d\mu_{t} & =  \kappa_{\mu}(\bar{\mu}-\mu_{t})dt+\nu_{\mu}\sqrt{\sigma_{t}}dZ_{t}^{\mu}\\
	d\sigma_{t} & =  \kappa_{\sigma}(1-\sigma_{t})dt+\nu_{\sigma}\sqrt{\sigma_{t}}dZ_{t}^{\sigma}
\end{align*}
We write $G_t = G(\mu, \sigma)$ and we get the PDE
\begin{align*}
	0&= \rho \theta[G^{1-\frac{1}{\theta}}- G]+G((1-\gamma)\mu-\frac{1}{2}(1-\gamma)\gamma\nu_D^2\sigma)\\
	&+ \kappa_{\mu}(\bar{\mu}-\mu)\frac{\partial G}{\partial\mu}+  \kappa_{\sigma}(1-\sigma)\frac{\partial G}{\partial\sigma}\\
	&+\frac{1}{2}\nu_{\mu}^{2}\sigma\frac{\partial^{2}G}{\partial\mu^{2}}+\frac{1}{2}\nu_{\sigma}^{2}\sigma \frac{\partial^{2}G}{\partial\sigma^{2}}
\end{align*}
\subsubsection{Log linearization method}
We guess $G = e^{A_0+ A_1\mu_t + A_2 \sigma_t}$ and linearize the non linear term in $G$
$$G^{-\frac{1}{\theta}} \approx e^{-\frac{1}{\theta}(A_0 + A_1 \overline{\mu} + A_2)}(1-\frac{1}{\theta}(A_1(\mu_t -\overline{\mu}) + A_2 (\sigma_t-1))$$

We can then solve for $A_0, A_1, A_2$ by setting the constant term, the term in $\mu_t$ and the term in $\sigma_t$ to zero in the linearized PDE.
\subsubsection{Finite Difference Method}
We can discretize this PDE on a grid using a Finite Difference Scheme.
We assume that the processes associated with state variables have reflecting boundaries at the borders (except at the LHS border for $\sigma$). This gives a supplementary condition of the form $\partial G = 0$ at the borders\footnote{At the frontier we have
$$dG_{t} = G'(x) \sigma(x) \sqrt{dt} + G'(x)\mu(x) dt + \frac{1}{2}G''(x)\sigma^2(x)dt + o(dt)$$
Since $dG_t$ is $O(dt)$, we must have $G'(x) = 0$, at least when $\sigma(x) \neq 0$.}

To handle boundary conditions, we upwind the first derivative. This means that we approximate $\partial G$ by a forward difference when the drift is positive and a backward difference when the drift is negative. The second derivative at the border involves points outside the grid. We find the value of $G$ at these points by applying the reflecting boundary condition at these points. \footnote{At the points $j = 0$, the volatility is zero. Although we don't have the border condition $\partial_\sigma G = 0$ or $\partial_\mu G = 0$ anymore, the term involving the second derivative disappears naturally}

To sum up the scheme is
\begin{align*}
	0&= \rho \theta[(G_{ij})^{1-\frac{1}{\theta}}- G_{ij}]+G_{ij}((1-\gamma)\mu_i-\frac{1}{2}(1-\gamma)\gamma\nu_D^2\sigma_j)\\
	&+(\kappa_{\mu_i}(\bar{\mu}-\mu_i))^+\frac{G_{i+1, j}-G_{i, j}}{\Delta \mu}+(\kappa_{\mu_i}(\bar{\mu}-\mu_i))^-\frac{G_{i, j}-G_{i-1, j}}{\Delta \mu}\\
	&+(\kappa_{\sigma_j}(1-\sigma_j))^+\frac{G_{i, j+1}-G_{i,j}}{\Delta \sigma}+(\kappa_{\sigma_j}(1-\sigma_j))^-\frac{G_{i, j}-G_{i,j-1}}{\Delta \sigma}\\
	&+\frac{1}{2}\nu_{\mu_i}^{2}\sigma_j\frac{G_{i+1, j} - 2 G_{i, j} + G_{i-1, j}}{(\Delta\mu)^2}+\frac{1}{2}\nu_{\sigma_j}^{2}\sigma_j\frac{G_{i, j+1} - 2 G_{i, j} + G_{i, j-1}}{(\Delta\sigma)^2}
\end{align*}

Denote $Y$ the vector of  $(G_{ij})_{1 \leq i,j\leq n}$. The scheme  defines a function $F$ such that $F(Y) = 0$. We can solve for $Y$ using one of these methods:
\begin{enumerate}
	\item Use a non linear solver for the system $F(Y) = 0$. These algorithms start with an initial guess, and update based on the Jacobian of $F$. In some PDE (not this one), you need a good initial guess. A technique is to solve the PDE for $\theta = 1$, and use the solution at an initial guess for other values of $\theta$.
	\item Use an ODE solver for the system $F(Y) = \dot{Y}$. The solution when $T\rightarrow +\infty$ is the solution of the PDE. This method is called ``the method of lines''.
\end{enumerate}
Both methods require the jacobian of $F$, which can generally be automatically computed using automatic or numerical differenciation.

A solution that would \textit{not} work is to solve for $G$ by iterating over time
\begin{align*}
	\frac{G_{n+1}-G_n}{\Delta t} &= F(G_n)
\end{align*}
This method can be seen as a special case of a non linear solver (fixed point method) or as a special case of an ODE solver (in this context, it is called the Euler method). The criterion for the convergence of this method is that $F$ is monotonous in $G$ (Barles Souganadis theorem). This is not the case here, due to the non linear term  $\rho \theta[(G_{ij})^{1-\frac{1}{\theta}}- G_{ij}]$.

\subsubsection{Spectral Method (= collocation method)}
We can also solve the PDE by looking at solutions of the form
$$V(\mu, \sigma) = \sum_{kl} a_{kl} \phi_k(\mu)\psi_l(\sigma)$$
where $\phi, \psi$ denote any basis of functions (splines or chebyshev polynomials).
The value function is characterized by its coordinates $a_{kl}$ on this basis. Writing the PDE on a grid gives a non linear system in term of the coordinates, which we can solve. We use the same border condition and initial guess as the Finite Difference method.

\subsection{Comparison}

\begin{tabular}{|c|c|c|c|c|c|}
	\hline
	Name & BY04 & BY04 & This paper & This paper & Link
	\\
	\hline
	\hline
	mean growth rate & $\mu$ & 0.0015 & $\bar{\mu}$ & 0.0015 & $\mu=\bar{\mu}$
	\\
	\hline
	mean volatility & $\sigma^{2}$ & 0.00006084 & $\nu_{D}$ & 0.0078 & $\sqrt{\sigma^{2}}=\nu_{D}$
	\\
	\hline
	growth persistence & $\rho$ & 0.979 & $\kappa_{\mu}$ & 0.0212 & $ - \log(\rho) = \kappa_\mu$
	\\
	\hline
	volatility persistence & $\nu_{1}$ & 0.987 & $\kappa_{\sigma}$ & 0.0131 & $-\log\left(\nu_{1}\right)=\kappa_{\sigma}$
	\\
	\hline
	growth rate volatility & $\varphi_{e}$ & 0.044 & $\nu_{\mu}$ & 0.0003432 & $\varphi_{e}\times\sqrt{\sigma^{2}}=\nu_{\mu}$
	\\
	\hline
	volatility volatility & $\sigma_{w}$ & 0.0000023 & $\nu_{\sigma}$ & 0.0378 & $\sigma_{w}/\sigma^{2}=\nu_{\sigma}$
	\\
	\hline
	time discount & $\delta$ & 0.998 & $\rho$ & 0.002 & $-\log\left(\delta\right)=\rho$
	\\
	\hline
	RRA & $1-\gamma$(RRA) & 7.5 or 10 & $1-\gamma$ & -6.5 or -9 & $1-\text{RRA}=1-\gamma$
	\\
	\hline
	IES & $\psi$ & 1.5 & $\psi$ & 1.5 & $\psi = \psi$
	\\
	\hline
\end{tabular}
Also,  $\theta = (1-\gamma)/(1- 1/\psi)$ = -19.50 or -27.
Let's express the  wealth to consumption ratio $K_t$ in term of state variables.
$$V = G_tK_t^{\gamma - 1}\frac{W^{1-\gamma}}{(1-\gamma)}$$
FOC for consumption can be written
$$K_t^{-1} = \rho^{\psi} K_t^{\psi - 1}G_t^\frac{1-\psi}{1-\gamma}$$
We conclude
$$K_t = \rho^{-1} G_t^{1/\theta}$$
Bansal Yaron find
\begin{align*}
	\log K_t &\propto A_1 \mu_t + A_2 \nu_D^2\sigma_t\\
	A1 &= \frac{1-\frac{1}{\psi}}{1-0.997 e^{-\kappa_\mu}}\\
	A2 &= 0.5\theta\frac{(1 - \frac{1}{\psi})^2 + (A_1  0.997 \frac{\nu_\mu}{\nu_D})^2}{1-0.997e^{-\kappa_\sigma}}
\end{align*}
\end{document}


\section{Optimality Criteria}\label{sec:optimality}
Looking at the previously presented problems, it becomes clear that there are different objective functions or optimality criteria for matching problems. For instance, in the stable marriage problem, matchings are primarily judged by the stability characteristic. However, such a characteristic does not make sense in the case of the student-seminar problem, since stability assumes both-sided preferences. For that very reason, different criteria have to be used for judging the quality of a matching.

Intuitively, when thinking about matching students to seminars, it would be desirable to match as many students as possible, as well as matching as many students as possible to their first choice, but at the same time to remain fair and resistant to manipulation. To formalize these requirements, a few criteria have been discussed in the literature, which will be helpful for comparing different approaches.

\subsection{Maximum Cardinality}
The goal of the \emph{Maximum Cardinality Matching Problem} is finding a matching $M$ on a graph $G=(V=(X, Y), E)$, so that $|M|$ is maximal \cite{GraphTheoryIntro}. Consequently, maximum cardinality as an optimality criterion means that a matching is ideal if the number of students that are matched is maximized among all possible matchings. It should be noted that students' preferences are not considered when computing a maximum cardinality matching. As a matter of fact, it is possible that multiple matchings of the same cardinality exist, where one of the matchings could be better in the sense of a different optimality criterion. Therefore, maximum cardinality may be desirable, but should be used in conjunction with different criteria, as it does not consider student preferences. This criterion is also often referred to as \emph{efficiency} in the literature; however, we will use the term maximum-cardinality as it is more specific.

\subsection{Pareto Optimality}
\emph{Pareto-optimality} or \emph{Pareto-efficiency} is a commonly used term in economics to describe the state of resource allocations. Intuitively, an allocation, or in our case a matching, is Pareto-optimal iff no improvement can be made to a single individual without worsening the situation for other individuals. Additionally, a matching in which a subset of students $S'$ would be better off by swapping their seminars is not Pareto-optimal.
In order to more formally define Pareto-optimality, we must first define student preferences more formally:
given two matchings $M, M'$ and a student $s \in S$, the student $s$ prefers $M'$ over $M$ in the following cases:
\begin{enumerate}
    \item s is matched in $M'$ and unmatched in $M$, or
    \item s is matched in both $M'$ and $M$, however prefers $M'(a)$ over $M(a)$
\end{enumerate}
Using this definition, we now define Pareto optimality as follows: given an instance $I$ of a matching problem and its set of possible matchings $\mathcal{M}$, we define a relation $\succ_{pareto}$ on $\mathcal{M}$, where given two matchings $M, M' \in \mathcal{M}$ the following holds true: $M' \succ_{pareto} M$ if no student prefers $M$ to $M'$, but some student prefers $M'$ over $M$. Consequently, a matching $M' \in \mathcal{M}$ is called Pareto-optimal iff there exists no other matching $M \in \mathcal{M}$, such that $M' \succ_{pareto} M$ \cite{algorithmics}. A Pareto-optimal matching always exists for any instance of a matching problem and can be efficiently computed using one of various algorithms. A simple greedy algorithm uses the \emph{Random Serial-Dictatorship} mechanism to draw each agent in random order and lets them select their most-preferred, available item from their preference list (details in Section \ref{algo-rsd}) \cite{RothTwoSided, SerialDictatorship}. However, the greedy algorithm does not always produce a Pareto-optimal matching of maximum cardinality, which would be desirable for student-seminar matchings \cite{Abraham:Pacha}.

\subsection{Popularity}
In the context of matching with one-sided preference lists, using an optimality criterion like stability, which is based on the preferences of both parties, does not apply. Instead, a commonly used criterion is a matching $M$'s \emph{popularity}, which indicates that more students prefer that matching $M$ over any other possible matching \cite{ManlovePopularMatchings}. Given the definition of the student-seminar matching problem, we can formally define popularity as follows:
let $P(M', M)$ be the set of students who prefer $M'$ over $M$. A matching $M'$ is said to be more popular than $M$, denoted by $M' \succ_{pop} M$, iff $|P(M', M)| > |P(M, M')|$. That concludes that a matching $M'$ is popular, iff there is no other matching $M$ that is more popular than $M'$, i.e. $M' \succ_{pop} M$ \cite{Klaus, AbrahamPopular}. Because of that definition, this criterion is often also referred to as the majority assignment \cite{Gardenfors}.

Using this definition, we can see that every popular matching also is a Pareto-optimal matching. Given a popular matching $M'$ and any matching $M$ for an instance $I$ of the problem, $M'$ is Pareto-optimal if $|P(M, M')| = 0$ and $|P(M', M)| \geq 1$, which obviously implies that $M'$ is popular as well \cite{Klaus}.

It is important to note that a popular matching's cardinality could be smaller than the maximum cardinality, meaning that in the case of student-seminar matchings, a group of students could be left unassigned in favor of the majority of the students having a match that they prefer.
Additionally, a popular matching, unlike a Pareto-optimal matching, does not always exist. To illustrate this, let us consider the following instance: $S=\{s_1, s_2, s_3\}$, $T=\{t_1, t_2, t_3\}$, where each student has the same preference list, being $t_1 < t_2 < t_3$, and each seminar $t_i \in T$ has a capacity of 1. Given the following matchings, we can confirm that there exists no popular matching for the given instance:
\begin{enumerate}
    \item $M_1=\{(s_1, t_1), (s_2, t_2), (s_3, t_3)\}$
    \item $M_2=\{(s_1, t_3), (s_2, t_1), (s_3, t_2)\}$
    \item $M_3=\{(s_1, t_2), (s_2, t_3), (s_3, t_1)\}$
\end{enumerate}
It is clear that $M_2$ is more popular than $M_1$, $M_3$ is more popular than $M_2$ and $M_1$ is more popular than $M_3$ \cite{AbrahamPopular}.

\subsection{Profile-based Optimality}\label{sec:profile}
Contrary to popularity and Pareto-optimality, in which the students' satisfaction with a matching is compared, we should also examine the structure of a matching by defining the profile of a matching and comparing it. Intuitively, the profile of a matching $M$ is a vector whose $i$th component indicates the number of students obtaining their $i$th-choice seminar in $M$, according to their preference list.

Formally, let $I$ be an instance and $\mathcal{M}$ the set of its matchings. Given a matching $M \in \mathcal{M}$ with the set of students $S$ and seminars $T$, we define the regret $r(M)$ of $M$ as follows:
$r(M) = \max \{rank(s_i, t_j): (s_i, t_j)\in M\, s_i \in S, t_j \in T\}$, where for every match $(s_i, t_j) \in M$, $rank(s_i, t_j)$ is defined as the position of $t_j$ on $s_i$'s preference list. The profile of $M$ is now defined as a vector $\langle p_1,..., p_{r^*} \rangle$, with $r^* = r(M)$ and for each $k \in [1,r^*]$, the $k$th component is defined as: $p_k=|\{(s_i, t_j) \in M: rank(s_i, t_j) = k\}|$ \cite{algorithmics}.

Using the definition of a matching's profile, it is now possible to define a matching as rank-maximal as follows: a matching $M$ is rank-maximal, if its profile $p(M)$ is lexicographically maximal over all possible matchings in $\mathcal{M}$. That means that the number of students in $M$ who are matched to their first choice is maximal among all $M' \in \mathcal{M}$; taking that into consideration, the number of students who are matched to their 2nd choice is maximum among all matchings, and so on.

\subsection{Strategy-proofness}
% TODO: move this to a different section and potentially rewrite as truthful
The aforementioned criteria all primarily consider the structure of a matching to evaluate quality and not the properties of a matching mechanism. An important question to consider, however, is if the agents can manipulate the outcome of an algorithm by not truthfully disclosing their preferences - indeed, in the setting of matching residents to hospitals in the US, a previously used algorithm allowed students to improve their outcome of the algorithm by not supplying their real preferences \cite{Gusfield}. In the literature, the term for a mechanism, wherein no agent can benefit from misrepresenting their preferences, is called strategy-proof \cite{Klaus}. We would desire that a matching mechanism encourages students to truthfully disclose their preferences by giving the students no opportunity for strategically manipulating the outcome of the used matching algorithm.

Such strategy-proof mechanisms are of high interest for most matching problems, since preference-based optimality criteria, like the ones previously mentioned, would certainly lose some significance if the mechanism used to compute them is not strategy-proof. For instance, it has been shown \cite{ROTH1982127} that for the stable marriage problem with incomplete preferences, there exists no matching mechanism that both produces a popular matching and is strategy-proof.

\subsection{Application to Student-Seminar Matching}\label{criteria-application}
Given the problem description of student-seminar matching, it would be desirable to find a matching that has the following properties:
\begin{enumerate}
    \item \textbf{Maximum Cardinality:} As few students as possible should be left unmatched.
    \item \textbf{Pareto Optimality:} It should not be possible for any of the students to improve their situation without affecting other students negatively. This means that there exists no opportunity for anyone to perform a trade or get matched to a new seminar without negatively impacting other students.
    \item \textbf{Popularity:} The number of students who are satisfied with their match should be maximal among all possible matchings.
    \item \textbf{Rank Maximality:} As many students as possible should be matched to their first choice or if not possible, their second choice, and so on.
    \item \textbf{Strategy-proofness:} Students should not be able to benefit, i.e. increase their chances of being matched to their top-preference, by lying about their true preferences. A matching mechanism should also not encourage students to supply short preference lists in order to improve their chances of getting matched to their preferred seminar.
\end{enumerate}
As we have already seen, there does not always exist a popular matching or a Pareto-optimal matching that is also \emph{agent complete}, i.e. matches all students. Additionally, the fact that students can supply incomplete preference lists can very well lead to matchings that leave a few students unassigned, even if the sum of the capacity of all seminars is greater than the number of students. Given these constraints and observations, we will see that there is no such thing as an ideal matching for all instances based on the criteria we have presented. However, it will be possible to find matchings that will optimize for most of the optimality criteria.


%!TEX root = ../notes.tex
\section{April 4, 2022}
\subsection{Quantum Computing \emph{continued}}
\recall Deutch's Algorithm. Given black box that implements $f : \{0, 1\}\to \{0, 1\}$. We define $F : \{0, 1\}^2\to \left\{ 0, 1 \right\}^2$ via $F(x, y) = (x, f(x) + y\pmod{2})$.

The problem was to determine whether $f(0) \overset{?}{=}f(1)$. Our solution was to compute $F$ on a superposition of two states.
\[F\left(\frac{1}{\sqrt{2}}\Ket{0} + \frac{1}{\sqrt{2}}\Ket{0}, \frac{1}{\sqrt{2}}\Ket{0} - \frac{1}{\sqrt{2}}\Ket{0}\right) = \frac{1}{2}\left((-1)^{f(0)}\Ket{0} + (-1)^{f(1)}\Ket{1}\right)\otimes\left( \Ket{0} - \Ket{1} \right)\]
where we apply a rotation to the possible outcomes. Our key transformation is
\[(x, y)\mapsto \left( \frac{1}{\sqrt{2}}x + \frac{1}{\sqrt{2}}y,  \frac{1}{\sqrt{2}}x - \frac{1}{\sqrt{2}}y \right)\]
which is a \emph{rotation by $45^\circ$}.

We discuss a generalization of this which is the Discrete Fourier transformation.
\begin{definition}[Discrete Fourier Transform]
    Given $x_1, x_2, \dots, x_N$, the \ul{Discrete Fourier Transform} (DFT) is a new sequence $y_1, y_2, \dots, y_N$ defined via:
    \[y_k = \frac{1}{\sqrt{N}}\sum_{j} e^{-\frac{2\pi i}{N}\cdot jk}x_j\]
\end{definition}

Note that this is the discrete analog of the ordinary Fourier Transform:
\[f(x)\rightsquigarrow \hat{f}(y) = \int e^{2\pi ixy}f(x)\ dx\]

\begin{example}
    For $N = 2$, we have $x_1, x_2$ so
    \begin{align*}
        y_1 & = \frac{1}{\sqrt{2}}x_1 + \frac{1}{\sqrt{2}}x_2 \\
        y_2 & = \frac{1}{\sqrt{2}}x_1 - \frac{1}{\sqrt{2}}x_2
    \end{align*}
\end{example}
we note that these are efficiently computable in the quantum setting.

\subsection{Shor's Algorithm}
\begin{ques*}
    Given a black box that implements a function $f : \ZZ \to X$ (assumed to be periodic). What is the period of $f$?

    This is to say, we're promised that $f(x) = f(x + n)$ for some $n$, we are tasked to find $n$.
\end{ques*}

Classically, we take
\[f(1), f(2), \dots, f(n+1)\]
where $f(n+1) = f(1)$. The runtime is $\mathcal{O}(n)$.

We try to solve this using a quantum algorithm:
\begin{enumerate}
    \item Choose $N$ large power of $2$ (we want $N = \mathcal{O}(n^2)$).
    \item Prepare the state
          \[\frac{1}{\sqrt{N}}\sum_{j=1}^N \Ket{j}\otimes \Ket{f(j)}.\]
    \item Apply DFT to first register. That is,
          \begin{align*}
              \frac{1}{N}\sum_{k, x} \left( \sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}jk} \right)\Ket{k}\oplus\Ket{x}
          \end{align*}
    \item Measure first register.
\end{enumerate}

How big is this sum?
\[
    \sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}jk}
\]
We note that this sum
\[
    \sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}jk} \approx \sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}(j+n)k} = e^{-\frac{2\pi ink}{N}}\cdot \sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}jk}
\]
therefore
\[\sum_{j: f(j) = x} e^{-\frac{2\pi i}{N}jk} \approx 0\quad\text{unless}\quad e^{-\frac{2\pi ink}{N}}\approx 1 \iff nk\text{ is close to a multiple of $N$}\]
This is to say, $k$ is approximately a multiple of $\frac{N}{n}$. So we get $\frac{N}{n}$ out of this register.

\emph{Why might I get a multiple?}
\begin{example}
    Consider sequence
    \[\boxed{0, 1, 0, 1, 0, 1, 0, 0}, 0, 1, 0, 1, 0, 1, 0, 0, , 0, 1, 0, 1, 0, 1, 0, 0, \dots\]
    where the period is $8$. We'll usually get $\approx \frac{N}{\text{period}}$ unless there is a smaller ``almost-period'' where we might get $\frac{N}{\text{almost-period}}$ but the almost-period divides the period.
\end{example}

The runtime of this algorithm is $\approx \log n$.

\subsection{Breaking Encryption}
\subsubsection{Integer Factorization}
We're given $N = pq$. We pick $x\in \ZZ/N\ZZ$ and consider the function $f(j) = x^j\pmod{N}$. Applying Shor's Algorithm, we can determine the period of $f$, which is the order of $x$, which is a factor of $(p-1)(q-1)$. More precisely, $\frac{(p-1)(q-1)}{\text{something small}}$. We recover $(p-1)(q-1)$ and we can factor $N$ easily. This is bad news for RSA!

\subsubsection{Quantum Elgamal/DLP}
We have some group $G$ and $g\in G$ where we want to recover $k$ from $x=g^k$.

Consider function $f(a, b) = x^a \cdot g^{-b}$ (we do a 2-dimensional DFT). $f(a, b) = f(a+1, b+k)$. So $(1, k)$ is the period of $f$. This solves the discrete log problem \emph{in any group}!


% ******************************* Thesis Appendix B ********************************
\chapter{Appendix C: Additional Proofs, Figures, and Tables}

\ifpdf
    \graphicspath{{Appendix3/Figs/Raster/}{Appendix3/Figs/PDF/}{Appendix3/Figs/}}
\else
    \graphicspath{{Appendix3/Figs/Vector/}{Appendix3/Figs/}}
\fi

This appendix chapter contains additional proofs, figures, and tables omitted
from the body of this work for sake of clarity. It is intended for readers who
wish to examine our claims in greater detail.

\section{Sufficient conditions for vanishing gradients}\label{sec:vanishing-exploding-gradients}

Following \citet{Pascanu2012}, let $\| \cdot \|$ be any
submultiplicative matrix norm (\eg Frobenius, spectral, nuclear, Shatten
$p$-norms). Without loss of generality, we will use the \emph{operator norm}
defined as
\begin{equation}\label{eq:operator-norm}
    \| A \| = \sup_{x \in \RR^n; x \neq 0} \frac{|A x|}{|x|}
\end{equation}
where $|\cdot|$ is the standard Euclidian norm.

Applying the definition of submultiplicativity to the factors of
the product in \cref{eq:error-transfer}, we have that for any $k$
\begin{equation}
    \left\| \frac{\pd \h_k}{\pd \h_{k-1}} \right\|
    \leq \| \W_{hh}^\tp \| \| \diag\left( \sigma_{hh}'(\h_{k-1}) \right) \|
    \leq \gamma_{\W} \gamma_\sigma
\end{equation}
where we have defined $\gamma_{\W} = \| \W_{hh}^\tp \|$ and
\begin{align}
    \gamma_\sigma
    &\coloneqq \sup_{h \in \RR^n} \| \diag \left( \sigma_{hh}'(\h) \right) \|  &\\
    &= \sup_{h \in \RR^n} \max_i \sigma_{hh}'(\h)_i &\mbox{Operator norm of diag} \\
    &= \sup_{x \in \RR} \sigma_{hh}'(x) &\mbox{$\sigma_{hh}$ acts elementwise}
\end{align}

Substituting back into \cref{eq:error-transfer}, we find that
\begin{equation}
    \left\| \frac{\pd \h_t}{\pd \h_k} \right\|
    = \left\| \prod_{t \geq i > k} \frac{\pd \h_i}{\pd \h_{i-1}} \right\|
    \leq  \prod_{t \geq i > k} \left\| \frac{\pd \h_i}{\pd \h_{i-1}} \right\|
    \leq (\gamma_{\W} \gamma_\sigma)^{t-k}
\end{equation}

Hence, we see that a sufficient condition for vanishing gradients is
for $\gamma_{\W} \gamma_\sigma < 1$, in which case $\left\| \frac{\pd \h_t}{\pd \h_k} \right\| \to 0$
exponentially for long timespans $t \gg k$. $\qed$

If $\gamma_\sigma$ is bounded, sufficient
conditions for vanishing gradients to occur may be written as
\begin{equation}
    \gamma_{\W} < \frac{1}{\gamma_\sigma}
\end{equation}
This is true for commonly used activation functions (\eg $\gamma_\sigma = 1$
for $\sigma_{hh} = \tanh$, $\gamma_\sigma = 0.25$ for $\sigma_{hh} =
\sigmoid$).

The converse of the proof implies that $\| \W_{hh}^\tp \| \geq
\frac{1}{\gamma_\sigma}$ are necessary conditions for $\gamma_{\W}
\gamma_\sigma > 1$ and exploding gradients to occur.

\section{Quantifying the effects of preprocessing}\label{sec:quantify-effects-preprocessing}

Related discussion is in \vref{sec:preprocessing}.

\begin{landscape}
  \begin{figure}[p]
    \centering
    \begin{subfigure}[c]{1.0\textwidth}
        \centering
        \input{Appendix3/Figs/pitch-usage-original.pgf}
    \end{subfigure}
    \begin{subfigure}[c]{1.0\textwidth}
        \centering
        \input{Appendix3/Figs/pitch-usage-preproc.pgf}
    \end{subfigure}
    \caption{Distribution of pitches used over Bach chorales corpus.
      Transposition has resulted in an overall broader range of pitches and
    increased the counts of pitches which are in key.}
    \label{fig:pitch-key-standardization}
  \end{figure}
\end{landscape}

\begin{figure}[htbp]
    \centering
    \begin{subfigure}[t]{0.48\textwidth}
        \centering
        \input{Appendix3/Figs/pitch-class-usage-original.pgf}
    \end{subfigure}
    ~
    \begin{subfigure}[t]{0.48\textwidth}
        \centering
        \input{Appendix3/Figs/pitch-class-usage-preproc.pgf}
    \end{subfigure}
    \caption{Distribution of pitch classes over Bach chorales corpus. Transposition has increased the counts
    for pitch classes within the C-major / A-minor scales.}
    \label{fig:pc-key-standardization}
\end{figure}

\begin{figure}[htbp]
    \centering
    \begin{subfigure}[t]{0.48\textwidth}
        \centering
        \input{Appendix3/Figs/meter-usage-original.pgf}
    \end{subfigure}
    ~
    \begin{subfigure}[t]{0.48\textwidth}
        \centering
        \input{Appendix3/Figs/meter-usage-quantized.pgf}
    \end{subfigure}
    \caption{Meter is minimally affected by quantization due to the high resolution used for
    time quantization.}
    \label{fig:meter-time-quantization}
\end{figure}

\section{Discovering neurons specific to musical concepts}

Related discussion is in \vref{sec:music-concept-neurons}.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-0.pdf}
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-1.pdf}
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-2.pdf}
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-3.pdf}
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-4.pdf}
    \includegraphics[width=1.0\linewidth]{model-analysis-tokens-5.pdf}
    \caption{Neuron activations over time as the encoded stimulus is processed token-by-token}
    \label{fig:model-analysis-tokens}
\end{figure}

\section{Identifying and verifying local optimality of the overall best model}

Related discussion is in \vref{sec:overall-best-model}.

\begin{center}
  \captionof{figure}{Results of grid search (see \Cref{sec:lstm-grid-search}) over LSTM sequence model hyperparameters}
  \label{tab:torch-rnn-config-perfs}
  \input{Appendix3/Figs/torch-rnn-config-perfs.tex}
  \addtocounter{table}{-1}%
\end{center}

\begin{figure}[htbp]
    \centering
    \input{Appendix3/Figs/torch-rnn-network-params.pgf}
    \caption{\texttt{rnn\_size=256} and \texttt{num\_layers=3} yields lowest validation loss.}
    \label{fig:torch-rnn-network-params}
\end{figure}

\begin{figure}[htbp]
  \centering
  \input{Appendix3/Figs/torch-rnn-network-params-num-layers.pgf}
  \caption{Validation loss improves initially with increasing network depth but deteriorates after $>3$ layers.}
  \label{fig:torch-rnn-network-params-num-layers}
\end{figure}

\begin{figure}[htbp]
  \centering
  \input{Appendix3/Figs/torch-rnn-network-params-rnn-size.pgf}
  \caption{Validation loss improves initially with higher-dimensional hidden states
  but deteriorates after $>256$ dimensions.}
  \label{fig:torch-rnn-network-params-rnn-size}
\end{figure}

\begin{figure}[htbp]
    \centering
    \input{Appendix3/Figs/torch-rnn-input-params.pgf}
    \caption{\texttt{seq\_length=128} and \texttt{wordvec=32} yields lowest validation loss.}
    \label{fig:torch-rnn-input-params}
\end{figure}

\begin{figure}[htbp]
  \centering
  \input{Appendix3/Figs/torch-rnn-input-params-wordvec.pgf}
  \caption{Perturbations about \texttt{wordvec=32} do not yield significant improvements.}
  \label{fig:torch-rnn-input-params-wordvec}
\end{figure}

\section{Additional large-scale subjective evaluation results}

Related discussion is in \vref{sec:eval-results}.

\begin{figure}[htbp]
  \centering
  \input{Chapter7/Figs/responses-mask-agegroup.pgf}
  \caption{Proportion of correct responses for each question type and age group.}
  \label{fig:responses-mask-agegroup}
\end{figure}


\section{Self-organizing recurrent neural network (SORN)}
\label{sec:nn}

\subsection{Artificial neural networks}
\label{sec:ann}

Two different scientific approaches motivate an \acfi{ann}. Originally those networks were invented to simulate processes in the brain to get a better understanding of how the brain stores and processes information and how recognition and behavior is influenced by neural activity. Especially \textcite{rosenblatt1958perceptron} formulated a first idea to simulate a simple neural system, the so called \emph{perceptron}. On the other side, the idea came up to construct classification and prediction algorithms using \acs{ann}. Even though the approach was promising, for a long time artificial networks were not useful, because networks are computational costly and need big datasets to perform well. But when \textcite{hinton2006fast} published a fast way to train \acs{ann} and computers became more powerful at the same time, \acs{ann} became a widely used approach in machine learning. In machine learning \emph{supervised} \acs{ann}, where the algorithms learns with a given `truth', are still inspired by biology, but in most cases very different from real neural networks. On the other hand, computational neuroscience uses mostly \emph{unsupervised} networks, since there is no evidence that humans learn from an absolute truth. Those networks mostly try to be as close as possible to real neural processes or try to understand specific properties of biological networks. Nevertheless, in computational neuroscience the used networks highly depend on the level of research. For research on cell level, many details of neural conduction and synapses behaviors are modeled. Otherwise, if models on higher levels with hundreds or thousands of neurons are build, e.g. modeling memory and learning, many biological details on cell level are omitted in order to reduce complexity.

Independent of the approach, the basic principle for modeling neurons is the perceptron. Statistically the perceptron can be understood as a \ac{glm}, classifying the incoming signals either directly as $0$ or $1$ or giving a probability for those classes.

Beginning with a regression model, some inputs $\bm x \in \R^d$ are chosen to describe some output $y \in \R$, using a \emph{regression function} $f(\bm x)$. The regression is of the form

\begin{equation}
y = f(\bm x) + \epsilon.
\label{eq:gen-reg}
\end{equation}

Instead of the direct data $\bm x$ it is also possible to use a transformation $\phi(\bm x)$, which is called a \emph{feature}. For modeling a neural network, data $\bm x$ is used directly. $\epsilon \in \R$ is a stochastic error term, a random variable, and can be distributed in several ways. In most applications it is assumed to be normally i.i.d. distributed with $\epsilon \sim N(0,1)$. The regression function $f(\bm x)$ can be arbitrary. In a linear case it has the form

\begin{equation}
f(\bm x) = \bm x^T \bm \beta,
\end{equation}

where $\bm \beta \in \R^d$ are the parameters of dimension $d$. While the estimator of a linear model is relatively easy to obtain, an estimator for a non-linear $f(\bm x)$ is not easy to find. In order to have the opportunity to use non-linear models, in \ac{glm}s a so called link function $g^{-1}(f(\bm x)) = g^{-1}(\bm x^T \bm \beta)$ is introduced. It brings non-linearity to a linear model. If the expected value of $y$, modeled with the link function, is taken, it results in

\begin{equation}
\mu = \E[y] = \E[g^{-1}(\bm x^T \bm \beta) + \epsilon] = g^{-1}(\bm x^T \bm \beta),
\end{equation}

since $g^{-1}(\bm x^T \bm \beta)$ is deterministic and $\E[\epsilon] = 0$. At that point, the derived statistical model can be mapped to an \acs{ann} use case. $\bm x$ can be identified with the input signals from all pre-synaptic neurons $\bm z$ from equation \eqref{eq:activation}, where the first input is a dummy and is set to one. Furthermore, it can be seen that $\bm\beta$ is equal to the weights $\bm w_i$, where the first weight is $-b_i$. It means that $\sum_{j=1}^{n_i} w_{ij} z_j - b_i = \bm x_j^T \bm\beta_j$. Finally, the inverse of the link function $g^{-1} = \sigma$ is the \emph{activation function}, introduced in section \ref{sec:inhib-excit}, and $o = \mu$ denotes the output at the post-synaptic neuron. The output is given by

\begin{equation}
\label{eq:ann-model}
\mu = g^{-1}(\bm x^T \bm \beta) = g^{-1}\left(\sum_{j=1}^{n_i} w_{ij} z_j - b_i\right) \overset{\eqref{eq:activation}}{=} \sigma(a_i) = o.
\end{equation}

As already discussed in section \ref{sec:inhib-excit}, the choice of the activation function depends on the requirement. In machine learning, classically, a continuous function like a sigmoid or, more recent, piecewise rectified linear units (ReLUs) are used, since derivations are often necessary. In that case, there is always some activation, which rises strongly, if the input neurons increase in activity. Statistically, the perceptron with a sigmoid function is a logistic regression.

In case of an indicator function, an all-or-none law is realized, which is a closer approximation to real neurons. It results in zero activity, if the input neurons sum to a value below the threshold and an activity of one, if the sum of the activity hits the threshold. Such an indicator function was introduced in equation \eqref{eq:heavi}.

So far, the perceptron model is able to simulate some pre-synaptic neurons and one post-synaptic neuron, which depends on the activity of the pre-synaptic neurons and the activation function. If multiple perceptrons are modeled, the output of one neuron can be an input for another neuron and so forth. Mathematically, a neural network with multiple neurons is a recursive \acl{glm}. It can be modeled in one direction with a \emph{feed forward} structure or in arbitrary directions with a \emph{recurrent} structure.

\subsection{Classification of ANN}

Over time, a large number of network types were developed. Besides many different architectural types, two important types will be mentioned here:

\begin{itemize}
\item \emph{Feed-forward neural networks} are basically multilayer perceptrons, where synapses are just built in one direction. The flow of information is going from input neurons to output neurons with hidden layers inbetween.
\item \ACFI{rnn} have neurons which have synapses not only reaching into the next layer, but also to a neuron in next time step of processing. Therefore, time-coded information can be included.
\end{itemize}

Another possible classification refers to the number of connections. A \emph{fully connected} network has all connections established, and therefore weights of non-zero between all neurons. A \emph{sparsely connected} network has just some few connections, whereas most connections are fixed to zero. In some networks, like Echo State Networks (see below), the connectivity is in an order of about $1\%$.

\subsection{Reservoir Networks}
\label{sec:res-net}

A specific case of \acs{rnn} is \emph{Reservoir Computing} \parencite{lukovsevivcius2009reservoir}. Those networks can be called reservoir networks. Input signals are fed into a sparsely and randomly connected network, called \emph{reservoir}. The reservoir behaves like a complex nonlinear dynamic filter that transforms the incoming signals using a high-dimensional temporal mapping \parencite{schrauwen2007overview}. Schrauwen et al. described similarities between reservoir computing and kernel methods. Kernel methods are often used in machine learning to transform the input into a multi-dimensional or even infinite-dimensional feature space, where linear separation or linear fitting performs better than in the original input space. Similar to kernels, also a reservoir can be seen as a transformation of the input into higher dimensions, where the readout is kind of a feature space. If the reservoir contains $N$ neurons, the input is projected into a $N$-dimensional space. A state of the reservoir is mathematically just a point in this high dimensional space. Temporal signals entering the reservoir can be described as a trajectory in that space. \textcite{lazarphd2009self} hypothesized in her dissertation that, theoretically, the brain computes with trajectories in a similar way, which would justify the use of reservoir networks in neural computation approaches.

Two major types of Reservoir Networks are \ac{esn} \parencite{jaeger2004harnessing} and \ac{lsm} \parencite{maass2002real}. Both networks were independently developed \parencite{goodfellow2007deep} and are quite similar. \ac{lsm} uses neurons with binary outputs. They are designed to explain processes in the brain, whereas \ac{esn} uses continuous hidden units and focuses on machine learning tasks. Under the condition of the \emph{separation property} (different inputs result in separable outputs) and the \emph{fading memory property} (information about recent input can be maintained) the \ac{lsm} fulfills the Stone-Weierstrass theorem, namely that the \ac{lsm} can approximate any continuous function \parencite{maass2004computational}. Therefore \ac{lsm} is an interesting starting point for reservoir based neural simulation.

\subsection{Self-organizing recurrent neural network (SORN)}
\label{sec:sorn}

\ac{lsm} are developed for supervised learning and parameters are trained in a way to minimize the error. To simulate brain dynamics it is necessary to find a more unsupervised way of learning. Furthermore, those networks have normally a static reservoir, which means that the weights are randomly initialized and fixed in further processing. To tackle both problems, plasticity rules are a promising approach. They could be used as described in section \ref{sec:plasticity}. There were already some additions to \acl{lsm}, like adding hebbian learning \parencite{norton2006preparing}, but a first sophisticated reservoir network with plasticity was proposed by \textcite{lazar2009sorn}, called a \acfi{sorn}. It uses three plasticity rules in a reservoir network with binary threshold neurons, namely:

\begin{itemize}
\item \ACF{stdp}
\item \ACF{sn}
\item \ACF{ip}
\end{itemize}

\paragraph{Plasticity rules}

\begin{wrapfigure}{R}{0.45\textwidth}
	\centering
	\includegraphics[width=0.45\textwidth]{sorn_markov/stdp}
    \caption[Spike-timing-dependent plasticity rule used in \acs{sorn}.]{Spike-timing-dependent plasticity rule used in \acs{sorn}. The black circle corresponds to a silent neuron, the red circle represents a spiking neuron. The arrow indicates the direction of the weight. If the pre-synaptic neuron is firing, before the post-synaptic neuron fires, the weight between them is increased. Otherwise, the weight is decreased.}
    \label{fig:stdp-simple}
\end{wrapfigure}

The \acl{sn} and \acl{ip} are realized according to equation \eqref{eq:sn} and equation \eqref{eq:ip} respectively. Regarding the \acs{ip}, recall that it is the source for spontaneous activity. \acs{sorn} is build such that noise is optional and in most applications the network is using spontaneous activity from \acs{ip} only. In consequence the system can be described as deterministic chaos, if the initial weights are given. Only if noise is included, it would truly behave stochastically. The only remaining stochastic part is the random initialization of the weights.

The \acl{stdp} can be derived from equation \eqref{eq:stdp-orig}. In \acs{sorn} only the last step in time and thus only one spike is considered. If the pre-synaptic neuron fires directly before the post-synaptic neuron, the weight is increased. Otherwise, the weight is decreased, as shown in figure \ref{fig:stdp-simple}. Hence, there is no summing necessary and the rule is simplified to

\begin{equation}
\Delta w_{ij}(t) = f(\Delta t) = f(t_j - t_i),
\end{equation}

where $t_j$ is the time when the pre-synaptic neuron $j$ spikes and $t_i$ when the post-synaptic neuron $i$ spikes. The learning window is defined as

\begin{equation}
f(\Delta t)
= \eta_\STDP \cdot \begin{cases} x_j(t_i - 1) x_i(t_i) & t_j \le t_i\\ -x_j(t_j) x_i(t_j - 1) & t_j > t_i \end{cases},
\end{equation}

\nomenclature{$\eta_\STDP$}{Learning rate for \acl{stdp}}

where the learning rate $\eta_\STDP$ was introduced. Note that $f(\Delta t)$ is zero, if both neurons are not active in a row. If $t$ denotes the absolute time of the network, the weight change can be simplified to

\begin{equation}
\label{eq:stdp}
\Delta w_{ij}(t) = \eta_\STDP \cdot \left( x_j(t-1) x_i(t) - x_j(t) x_i(t-1) \right),
\end{equation}

since $x_i(t), x_j(t) \in \{0,1\}$. Equation \eqref{eq:stdp} corresponds to \textcite[equation 5]{lazar2009sorn}.

What kind of plasticity rule is used, depends on the phase of training and testing. All the rules, which are included in a phase are applied after every discrete calculation step.

\paragraph{Network setup}

\nomenclature{$N^E$}{Number of excitatory neurons}
\nomenclature{$N^I$}{Number of inhibitory neurons}

\nomenclature{$W$}{Weight matrix}

\begin{figure}[t]
	\centering
	\includegraphics[width=0.6\textwidth]{sorn_markov/sorn}
	\caption[Sketch of the \acl{sorn}]{Sketch of the \acl{sorn}. Red circles are excitatory neurons, blue circles represent inhibitory neurons. Connections between excitatory neurons are sparse, inhibitory and excitatory neurons are fully connected in both directions. Input neurons are represented by green circles. They are connected to excitatory neurons.}
	\label{fig:sorn}
\end{figure}

$N^U$ is the number of input neurons and $N^E$ the number of excitatory neurons. Excitatory and inhibitory neurons were separated. The number of inhibitory neurons is set as a fraction of excitatory neurons, for example $N^I = 0.2 \cdot N^E$. All neurons have a weight of $w_{ij} \in [0,1]$. Weights are established between excitatory and excitatory neurons, modeled in a matrix $W^{EE}$, between inhibitory and excitatory neurons $W^{EI}$, excitatory and inhibitory neurons $W^{IE}$ and input neurons and excitatory neurons $W^{EU}$. The matrices $W^{EI}$ and $W^{IE}$ are fully connected, $W^{EE}$ is sparsely connected.

There are no connections between inhibitory and inhibitory neurons in the model. While those connections exist in real biological networks, they occur sparsely and their function is highly specialized \parencite{chamberland2012inhibitory}. Important are connections between excitatory and inhibitory neurons and vice versa. Those connections provide feedback loops, which help to stabilize the system \parencite{turrigiano2004homeostatic}. Finally, the target rate for the \ac{ip} was chosen as $H_{\text{IP}} = 2 \cdot N^U/N^E$.

The structure of the network is sketched in figure \ref{fig:sorn}.

\paragraph{Updating the network}

The general model, defined in equation \eqref{eq:ann-model}, can be specified for the  case of a \acs{sorn} network. $\bm x(t) \in \{0,1\}^{N^E}$ denotes the spiking state of the excitatory neurons and $\bm v(t) \in \{0,1\}^{N^U}$ represents the input neurons. In the next step of time, the state of the excitatory neurons is given by

\begin{equation}
\label{eq:update-excitatory}
x_i(t+1) = \Theta\left( \sum_{j=1}^{N^E} W_{ij}^{EE}(t) x_j(t) - \sum_{k=1}^{N^I} W_{ik}^{EI} y_k(t) + v_i(t) - T_i^E(t)\right).
\end{equation}

Note that $v_i(t) = \sum_{l=1}^{N^U} W_{il}^{EU} v_l(t)$. But since every excitatory neuron has not more than one connection to an input neuron, the notation is simplified by $v_i(t) \in \{0,1\}$. The threshold $T_i^E(t)$ depends on \acl{ip}, which was defined in equation \eqref{eq:ip}.

\nomenclature{$\bm x(t)$}{Spiking vector of excitatory neurons}
\nomenclature{$\bm y(t)$}{Spiking vector of inhibitory neurons}
\nomenclature{$\bm v(t)$}{Spiking vector of input neurons}

$\bm y(t) \in \{0,1\}^{N^I}$ denotes the spiking state of the inhibitory neurons. They are updated in a more simple way, using

\begin{equation}
\label{eq:update-inhibitory}
y_i(t+1) = \Theta\left( \sum_{j=1}^{N^E} W_{ij}^{IE} x_j(t) - T_i^I\right).
\end{equation}

The threshold $T_i^I$ for the inhibitory neurons is not updated using \acs{ip}. Also note that \acs{stdp} is applied to $W_{ij}^{EE}(t)$, but not to the inhibitory-to-excitatory weights, neither to the excitatory-to-inhibitory weights. $W_{ik}^{EI}$ and $W_{ik}^{IE}$ are not time-dependent. Consequently, also \acl{sn} is only applied to $W_{ij}^{EE}(t)$.

If input is given to the network, the network is trained. Without input, meaning that $v_i(t) = 0$ for all $i \in \{1, ..., N^E\}$, the network is left alone with spontaneous activity only. The presented equations can also be found in \textcite{lazar2009sorn}, where most of the notation is very similar. Further details regarding the readout and the training are also given in section \ref{sec:methods}.

\subsection{Properties of SORN}
\label{sec:prop-sorn}

Since \acs{sorn} was invented in 2009, many studies were done with this network. Results from four papers will be introduced, which show the performance of the network on one hand and the implications regarding the general understanding of dynamic biological networks on the other hand.

In the original paper from \textcite{lazar2009sorn}, the \acs{sorn} network was able to outperform static reservoir networks in several tasks. The network was tested in an `counting' and an `occluder' task. They were able to show that in both cases the performance of the \acs{sorn} was better than in networks where the weights were just initially set. \textcite{lazar2009sorn} argues that especially \ac{stdp} has an important role. There is a lot evidence that \ac{stdp}-like mechanisms are essential for stimuli encoding in the brain. In several experiments it was possible to show that repetitive stimulation with temporal patterned inputs causes a rapid reorganization, which bases on \ac{stdp} mechanisms \parencite{yao2001stimulus, fu2002temporal, yao2007rapid}.

\textcite{zheng2013network} extended the \ac{sorn} by two more plasticity rules. First, \emph{structural plasticity} was added in order to allow new synaptic connections between excitatory neurons. With a probability of $p_c = 0.1$ a new connection was established between a random pair of neurons with a weight of $w_{ij} = 0.001$. Since \ac{stdp} tends to silence some specific connections to a weight close to zero, the structural plasticity enabled the network to balance this effect. Second, \acfi{istdp} added \ac{stdp} from inhibitory to excitatory neurons. With this adapted \ac{sorn} network, it was possible to reproduce experimental findings from \textcite{yasumatsu2008principles}. The distribution of synaptic weight strengths in the \ac{sorn} matched the lognormal distribution of EPSPs in the visual cortex of rats.

An extension to a \acs{sorn} network is a \acfi{rmsorn}, which modulates the weight-changes in \ac{stdp} \parencite{aswolinskiy2015rm}. Equation \eqref{eq:stdp} gets an additional \emph{modulation factor} $m$, therefore
%
\begin{equation}
\label{eq:stdp-modulated}
\Delta w_{ij}(t) = m \cdot \eta_\STDP \cdot \left( x_j(t-1) x_i(t) - x_j(t) x_i(t-1) \right).
\end{equation}

Different modulation strategies can be used. The weight-change can be applied as normal, suppressed or inversed, depending on a favored output or readout. Within eight tasks \textcite{aswolinskiy2015rm} could show that \acs{rmsorn} is close to the performance of the original \ac{sorn} from \textcite{lazar2009sorn}, even though the \acs{rmsorn} learns more self-organized than the \ac{sorn}. It shows the ability of the \ac{sorn} network to learn even more complex tasks very well in a highly self-organized way.

Another huge research effort was done by \textcite{hartmann2015s}. First, they were showing that \ac{sorn} has many characteristics which can be observed in experimental findings. The following list contains four such findings, where the experimental studies are cited in brackets.

\begin{itemize}
\item The inter-spike-interval (ISI) distribution of a neuron can be fitted by an exponential decay. Furthermore, the coefficient of variation (CV) follows a Poisson distribution with an event rate around $1$ \parencite{kenet2003spontaneously, goris2014partitioning}.
\item The fraction of excitatory-to-excitatory connections, where the weight stays $w_{ij} > 0$ during plasticity, converges to a stable fraction \parencite{yasumatsu2008principles}.
\item Sequences of four distinct activity patterns can be learned. In the original experiment with mice, gratings were used to train the visual cortex V1 \parencite{gavornik2014learned}.
\item Those sequences can be predicted, using the stored information of the network \parencite{gavornik2014learned}.
\end{itemize}

The sequence task, where specific patterns or `words' were learned, was also part of the inspiration for the present research. The words were represented surprisingly stable after a phase of plastic training, using \ac{stdp}.

In a very interesting approach \textcite{hartmann2015s} gave ambiguous input to the network, after it was trained. While the network was trained with several pre-defined input clusters, in the ambiguous case, some neurons from one cluster and some neurons from another cluster were activated. With a Bayesian approach, using the initial training information as a prior and the ambiguous input as data, they could predict the decision of the network by computing the posterior. They finally concluded, that \ac{sorn} is able to store information from earlier input and that this previous stored information, which is often seen as noise, could be information which systematically influences decisions of networks instead.


\documentclass{ximera}
\input{../preamble}
\title{Synthesis: Choose Your Method}
%%%%%\author{Philip T. Gressman}

\begin{document}
\begin{abstract}
We practice choosing a method for computing volume when none is specified.
\end{abstract}
\maketitle

\section*{Online Texts}
\begin{itemize}
\item \link[Ximera: Comparing Washers and Shells]{https://ximera.osu.edu/mooculus/calculus2/solidsOfRevolution/digInComparingWasherAndShellMethod}
\end{itemize}

\section*{Examples}

\begin{example}
(Spring 2016 Final Exam) Find the volume of the solid generated by revolving the region in the first quadrant bounded by $y = 1-x^2$, $y = 0$, and $x = 0$ about the line $x=2$.
\begin{itemize}
\item Using $x$ as the slicing variable, slices are \wordChoice{\choice[correct]{parallel}\choice{perpendicular}} to the axis $x=2$. This alignment corresponds to the \wordChoice{\choice{disk or washer}\choice[correct]{shell}} method.
(Expand to continue.)
\end{itemize}
\begin{expandable}
\begin{itemize}
\item The height of a shell is
\begin{multipleChoice}
\choice{$2$}
\choice{$2-x$}
\choice{$0$}
\choice{$x-2$}
\choice[correct]{$1-x^2$}
\choice{$1 - x^2 - 2$}
\end{multipleChoice}
and the radius is
\begin{multipleChoice}
\choice{$2$}
\choice[correct]{$2-x$}
\choice{$0$}
\choice{$x-2$}
\choice{$1-x^2$}
\choice{$1 - x^2 - 2$}
\end{multipleChoice}
\end{itemize}
Therefore
\[ V = \int_{\answer{0}}^{\answer{1}} \answer{2 \pi (2-x)(1-x^2)} dx = \answer{\frac{13 \pi}{6}}. \]
\end{expandable}
\end{example}

\begin{example}
(Fall 2012 Final Exam) The region in the $xy$-plane bounded by $y = (x-1)^{1/4}$ and the $x$-axis for $1 \leq x \leq 2$ is rotated about the $x$-axis. What is the volume of the resulting solid of revolution?
\begin{itemize}
\item Using $x$ as the slicing variable, slices are \wordChoice{\choice{parallel}\choice[correct]{perpendicular}} to the axis $x=2$. This alignment corresponds to the \wordChoice{\choice[correct]{disk or washer}\choice{shell}} method.
(Expand to continue.)
\end{itemize}
\begin{expandable}
\begin{itemize}
\item The outer radius of a disk is
\begin{multipleChoice}
\choice{$2$}
\choice{$y^4+1$}
\choice{$0$}
\choice[correct]{$(x-1)^{1/4}$}
\end{multipleChoice}
and the inner radius is
\begin{multipleChoice}
\choice{$2$}
\choice{$y^4+1$}
\choice[correct]{$0$}
\choice{$(x-1)^{1/4}$}
\end{multipleChoice}
\end{itemize}
Therefore
\[ V = \int_{\answer{1}}^{\answer{2}} \answer{\pi (x-1)^{1/2}} dx = \answer{\frac{2 \pi}{3}}. \]
\end{expandable}
\end{example}

\begin{example}
Find the volume of the solid generated by revolving the region in the first quadrant bounded on the left by $y = \sqrt{x}$,  on the right by $y = x-2$, and below by $y = 0$ about the axis $y = 0$.
\begin{itemize}
\item To carry out the calculation as efficiently as possible, the \wordChoice{\choice{$x$}\choice[correct]{$y$}}-variable should be used as the variable for slicing.
(Expand to continue.)
\end{itemize}
\begin{itemize}
\item Using $y$ as the slicing variable, slices are \wordChoice{\choice[correct]{parallel}\choice{perpendicular}} to the axis $y=0$. This alignment corresponds to the \wordChoice{\choice{disk or washer}\choice[correct]{shell}} method.
(Expand to continue.)
\end{itemize}
\begin{expandable}
\begin{itemize}
\item The height of a shell is
\begin{multipleChoice}
\choice{$y + 2$}
\choice{$y^2$}
\choice{$x-2$}
\choice{$\sqrt{x}$}
\choice{$y^2-y-2$}
\choice[correct]{$y+2-y^2$}
\end{multipleChoice}
and the radius is
\begin{multipleChoice}
\choice[correct]{$y$}
\choice{$y^2$}
\choice{$x-2$}
\choice{$\sqrt{x}$}
\choice{$y^2-y-2$}
\choice{$y+2-y^2$}
\end{multipleChoice}
\end{itemize}
Therefore
\[ V = \int_{\answer{0}}^{\answer{2}} \answer{2 \pi y(y+2-y^2)} dy = \answer{\frac{16 \pi}{3}}. \]
\end{expandable}
\end{example}

\end{document}