\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{float}
\usepackage{color}
\usepackage{url}
\usepackage{amsmath}
\definecolor{backcolour}{rgb}{0.96,0.96,0.96}
\definecolor{commentgreen}{rgb}{0.3,0.6,0.75}
\definecolor{keywordgreen}{rgb}{0.15,0.5,0.15}
\definecolor{stringmaroon}{rgb}{0.5,0,0}
\usepackage{amsmath}
\newcommand\norm[1]{\left\lVert#1\right\rVert}
\usepackage{geometry}
\geometry{left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{commentgreen},
    keywordstyle=\color{keywordgreen},
    stringstyle=\color{stringmaroon},
    basicstyle=\footnotesize\ttfamily,
    breaklines=true,
    captionpos=b,
    keepspaces=false,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=4
}
\lstset{style=mystyle}
\title{Assignment2}
\begin{document}
\maketitle
\section{Question(a)}
We know that:
\begin{equation}
y_w=\left\{\begin{aligned} & 1 &w\text{ shown in context of } o\\
& 0 & w\text{ not shown in context of } o\\
\end{aligned} \right.
\end{equation}
And since y is a one-hot vector, so only for $y_o$, it is 1.
\begin{equation}
\text{cross\_entropy}(y, \hat{y})=\sum_{w \in {V}}y_wlog(\hat{y_w}) = y_olog(\hat{y_o})
\end{equation}
\section{Question(b)}
$U$ is a matrix where each column is $u_i$ for context word $i$. $c$ is the center word's index, $o$ is the outside word's index.
\begin{equation}
\begin{aligned}
J(o, c, U) &= -log\frac{exp(u_o^Tv_c)}{\sum_{w \in {V}}{exp(u_w^Tv_c)}}\\
&=-u_o^Tv_c + log(\sum_{w \in {V}}{exp(u_w^Tv_c}))
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
\frac{\partial J(o, c, U)}{\partial v_c} &= -u_o + \frac{\partial log\sum_{w \in V} exp(u_w^Tv_c)}{\partial v_c} \\
&= -u_o + \frac{1}{\sum_{w \in V} exp(u_w^Tv_c)}\frac{\partial{\sum_{w \in V} exp(u_w^Tv_c)}}{\partial v_c}\\
&= -u_o + \frac{\sum_{w \in V} u_wexp(u_w^Tv_c)}{\sum_{w \in V} exp(u_w^Tv_c)}\\
&= -Uy + \sum_{w \in V} u_w\hat y\\
&= U(\hat y - y)
\end{aligned}
\end{equation}
\section{Question(c)}
If $w$ = $o$(this word is the outside word), then:
\begin{equation}
\begin{aligned}
\frac{\partial J(o, c, U)}{\partial u_w} &= -v_c + \frac{\partial log\sum_{w \in V} exp(u_w^Tv_c)}{\partial u_o} \\
&= -v_c + \frac{1}{\sum_{w \in V} exp(u_w^Tv_c)}\frac{\partial{\sum_{w \in V} exp(u_w^Tv_c)}}{\partial u_o}\\
&= -v_c + \frac{v_cexp(u_w^Tv_c)}{\sum_{w \in V} exp(u_w^Tv_c)}\\
&= -v_c + v_c\hat y\\
&= v_c(\hat y - y)
\end{aligned}
\end{equation}
Otheriwise $v_c\hat y$
\section{Question(d)}
\begin{equation}
\begin{aligned}
\frac{d\sigma(x)}{d x} &= \frac{d(1+e^{-x})^{-1}}{dx} \\
&= -(1+e^{-x})^{-2} e^{-x} \\
&= -\frac{1}{(1+e^{-x})(1+e^x)} \\
&= \sigma(x)\sigma(-x)
\end{aligned}
\end{equation}
\section{Question(e)}
\begin{equation}
\begin{aligned}
J_{\text{neg-sample}}(v_o, c, U) &= -log(\sigma(u_o^Tv_c)) - \sum_{u_k \in \text{neg}}log(\sigma(-u_k^Tv_c))\\
\frac{\partial J_{\text{neg-sample}}(v_o, c, U)}{\partial v_c} &= -\frac{\partial(log(\sigma(u_o^Tv_c))}{\partial v_c} - \sum_{v_k \in \text{neg}}\frac{\partial log(\sigma(-u_k^Tv_c))}{\partial v_c}\\
&= -u_o\sigma(-u_o^Tv_c) + \sum_{v_k \in \text{neg}}u_k\sigma(u_k^Tv_c)\\
&= u_o[\sigma(u_o^Tv_c)-1] + \sum_{v_k \in \text{neg}}u_k\sigma(u_k^Tv_c)
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
\frac{\partial J_{\text{neg-sample}}(v_o, c, U)}{\partial u_k} &= -\frac{\partial(log(\sigma(u_o^Tv_c))}{\partial u_k} - \sum_{v_k \in \text{neg}}\frac{\partial log(\sigma(-u_k^Tv_c))}{\partial u_k}\\
&= v_c\sigma(u_k^Tv_c)
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
\frac{\partial J_{\text{neg-sample}}(v_o, c, U)}{\partial u_o} = -v_c\sigma(-u_o^Tv_c)
\end{aligned}
\end{equation}
This negative-sampling loss function is more efficient to compute than naive softmax loss function because here only one vector $u_k$ or $v_c$ or $u_o$ involved, but in naive softmax, we need the whole matrix $U$ to be involved.
\section{Question(f)}
(i) \begin{equation}
\begin{aligned}
\frac{\partial J_{\text{skip-gram}}(v_o, w_{t-m}, ... , w_{t+m}, U)}{\partial U} &=
\sum_{-m \le j \le m, j \ne 0}\frac{\partial J(v_c, w_{t+j}, U)}{\partial U} \\
&= \sum_{-m \le j \le m, j \ne 0}v_{c}(\hat y_j - y_j)
\end{aligned}
\end{equation}
(ii) \begin{equation}
\begin{aligned}
\frac{\partial J_{\text{skip-gram}}(v_o, w_{t-m}, ... , w_{t+m}, U)}{\partial v_c} &= \sum_{-m \le j \le m, j \ne 0} \frac{\partial J(v_c, w_j, U)}{\partial v_c} \\ &= \sum_{-m \le j \le m, j \ne 0}U(\hat y_j - y_j)
\end{aligned}
\end{equation}
(iii) \begin{equation}
\begin{aligned}
\frac{\partial J_{\text{skip-gram}}(v_o, w_{t-m}, ... , w_{t+m}, U)}{\partial v_w} (\text{ when } w \ne c &)= 0
\end{aligned}
\end{equation}
\end{document}


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

%\makeindex

\begin{document}

% \maketitle

% Notes taken on

\subsection{Alternating Group}
\label{subsec:alternating_group}

Our initial description of even and odd permutations was left intentionally vague. A careful reader might question the robustness of our definition, and the correspondence between the combinatiorial definition of even/odd permutations and the group definition.\\

This section will answer these questions in better detail.\\

Recall that every element of \(S_n\) can be written uniquely as a product of disjoint cycles. However, if we remove the requirement that the cycles are disjoint, we are launched into disarray, as we find multitudes of various ways to write the same product of cycles. We will find that one thing remains constant between these variations-- if we write our element as a product of 2-cycles, then the quantity of 2-cycles maintains a parity.
\begin{defn}[Transposition]
	A \(2\)-cycle is called a \textbf{transposition}.
\end{defn}

\begin{hw}
	Prove that every permutation of \(\left\{ 1,2,\ldots,n \right\} \) can be written by a succession of transpositions.\\

	Conclude from this that every element of \(S_n\) can be written as a product of transpositions.
\end{hw}

While there are many different ways to represent the product of transpositions, one will notice that the number of terms remains the same. We will now show this more formally.\\

Let \(\Delta \) be the polynomial on variables \(\left\{ x_i \right\}_i^{n}\) given by
\begin{align*}
	\Delta (x_1,x_2,\ldots,x_n) = \prod_{i<j} (x_i - x_j).
\end{align*}
Now let \(\sigma  \in S_n\) be a permutation. We say that \(\sigma \) acts on \(\Delta \) by
\begin{align*}
	\sigma (\Delta ) = \prod_{i<j} (x_{\sigma (i)} - x_{\sigma (j)})
\end{align*}
\begin{prop}[Even and odd permutations]
	Let \(\sigma,\tau  \) be two permutations with \(\rho =\sigma \tau \). Then
	\begin{itemize}
		\item \(\sigma (\Delta ) = \pm \Delta \)
		\item \(\rho (\Delta ) = \sigma (\tau (\Delta ))\)
		\item \(\sigma (\Delta ) = -\Delta \) whenever \(\sigma \) is a transposition.
	\end{itemize}
\end{prop}
This allows us to formally define even and odd permutations.
\begin{defn}
	Let \(\sigma \) be a permutation so that \(\sigma (\Delta ) = (-1)^{k}\Delta \) for some \( \in \N\). If \(k\) is even, we say that \(\sigma \) is an \textbf{even permutation}, and if \(k\) is odd, we say that \(\sigma \) is an \textbf{odd permutation}.\\

	In general, \((-1)^{k}\) is the \textbf{sign} of a permutation.
\end{defn}

\begin{prop}
There is a surjective homomorphism given by
\begin{align*}
	\varphi :S_n \to \Z_2\\
	\varphi (\sigma ) = \textrm{sign}(\sigma )
\end{align*}
The kernel of the surjective homomorphism is the \textbf{alternating group \(A_n\)} and consists of the even transpositions.
\end{prop}

The alternating group has a fascinating property that it only has non-trivial normal subgroups for \(n=4\). Otherwise, any proper normal subgroups are trivial.

\begin{defn}[Simple]
	A group \(G\) is \textbf{simple} if the only normal subgroups are the trivial ones.
\end{defn}

\begin{thm}
	Suppose that \(n\neq 4\). Then \(A_n\) is a simple group.
\end{thm}

% \printindex
\end{document}


\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage{xcolor}
\usepackage{caption}
\usepackage{bashful}
\usepackage{empheq}
\usepackage{graphicx}
\usepackage{cancel}
\usepackage[shortlabels]{enumitem}
\usepackage[symbol]{footmisc}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}
\newcommand*{\vertbar}{\rule[-1ex]{0.5pt}{2.5ex}}
\usepackage{titlesec}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection.}{1em}{}

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height

\title{
\horrule{1pt} \\[0.4cm] % Thick top horizontal rule
 \Large Linear analysis of sub-shelf channel and keel formation
\horrule{1pt} \\[0.5cm] % Thick bottom horizontal rule
}

\author{Aaron Stubblefield} % Your name

\date{\small\today} % Today's date or a custom date

\begin{document}

\maketitle % Print the title

\setcounter{footnote}{1}

\section{Model}
Here, we analyze a small-perturbation model for ice shelves undergoing localized basal melting and/or freezing.
The model is derived in the appendix.
The variables in the small-perturbation model are
\begin{align*}
&h = \text{upper-surface elevation perturbation}\\
&s = \text{lower-surface elevation perturbation}\\
&m = \text{melting (or freezing) rate perturbation}.
\end{align*}
These perturbations are relative to a background state corresponding to uniform
flow in the $x$ direction and perfect flotation (Figure \ref{fig:sketch}). We will also be interested in velocity perturbations (`secondary flow')
$\pmb{u}=[u,v,w]^T$ relative to this background state.

Throughout these notes, we will refer to the condition $$h=-\delta s$$ as the ``perfect
flotation condition'' where $$\delta = \rho_\mathrm{w}/\rho_\mathrm{i}-1$$
is the flotation factor. One of our goals is to analyze how well the solutions satisfy this condition.
We will also be interested in the depth-dependence of the horizontal velocity perturbations $u$ and $v$---
the assumption that the depth-averaged ice velocity (i.e. ice flux) matches
its surface value underlies traditional melt-rate estimation methods.

\begin{figure}
  \centering
\includegraphics[width=0.95\textwidth]{figs/fig1.png}
\caption{Sketch of model variables.}
\label{fig:sketch}
\end{figure}

We use the Fourier transform (denoted with $\;\widehat{}\;$) with respect to the horizontal coordinates $(x,y)$
to solve the problem. To this end, we define
\begin{align*}
[k_x,k_y]^T = \text{wavevector},\quad
k = \sqrt{k_x^2 + k_y^2},\quad
k' = Hk,
\end{align*}
where $H$ is the ice thickness.
The Fourier-transformed elevations evolve according to
the coupled system
\begin{eqnarray}
&&\frac{\partial \widehat{h}}{\partial t}+ \left[ik_x \bar{u}  + \mathsf{R}\right]\widehat{h} = -\mathsf{B}\delta\widehat{s}\label{hf}\\
&&\frac{\partial \widehat{s}}{\partial t}+ [ik_x \bar{u} + \delta\mathsf{R}]\widehat{s} = \widehat{m} - \mathsf{B} \widehat{h}, \label{sf}
\end{eqnarray}
where $\bar{u}$ is the background ice flow speed (Appendix). The function
\begin{eqnarray}
\mathsf{R} = \left(\frac{\rho_\mathrm{i} g}{2\eta k}\right) \frac{e^{4{k'}} +4{k'} e^{2{k'}} -1 }{e^{4{k'}} -2(1+2{k'}^2)e^{2{k'}} +1}, \label{Rf}
\end{eqnarray}
describes viscous relaxation of surface topography and influences both the upper and basal surfaces.

Buoyancy forcing between the upper and basal surfaces is described by the function
\begin{eqnarray}
\mathsf{B} = \left(\frac{\rho_\mathrm{i} g}{2\eta k}\right) \frac{ 2({k'}+1)e^{3{k'}}+2({k'}-1)e^{{k'}} }{e^{4{k'}} -2(1+2{k'}^2)e^{2{k'}} +1}. \label{B}
\end{eqnarray}
The upper-surface evolution equation (\ref{hf}) shows that a change in $s$ forces changes in $h$ proportional to the
buoyancy function $\mathsf{B} $ (eq. \ref{B}), and vice versa for the basal-surface evolution equation (\ref{sf}).
On the other hand, $\mathsf{R}$ causes these surface perturbations to decay. The balance between
these relaxation and buoyancy functions is intimately linked to the stability of sub-shelf channels and keels.
Figure (\ref{fig:RB}) shows plots of the buoyancy and relaxation functions, and some of their
limiting properties.

The relaxation and buoyancy functions are singular in the limit
$k\to 0$ (Figure 2). This means that
relaxation and buoyancy adjustments occur \emph{instantaneously} at long wavelengths under the assumption of
an infinitely expansive ice shelf. In reality, the ice shelf is partially supported by the grounded
portion of the ice sheet (well, until it breaks off...), which ameliorates this behavior.
After scaling the problem, we introduce regularizations to avoid these singularities when computing the solutions.

\section{Scaling}
Now, we introduce scalings for the model equations (\ref{hf}) and (\ref{sf}).
We scale the variables according to
\begin{eqnarray}
  && x = Hx',\quad y=Hy',\quad z=Hz', \quad t = t_r t', \nonumber \\
&&k = H^{-1}k' , \quad k_x = H^{-1}k_x', \quad k_y  = H^{-1}k_y', \nonumber\\
&&u = \frac{H}{t_r}u',\quad v = \frac{H}{t_r}v',\quad w = \frac{H}{t_r}w',\nonumber\\
&& h = H h', \quad s = H s' ,\quad m = \frac{H}{t_r} m' \label{scaling}
\end{eqnarray}
where primes denote dimensionless quantites and
\begin{eqnarray}
t_r \equiv \frac{2\eta}{\rho_i gH} \label{t_relax}
\end{eqnarray}
is the characteristic timescale for viscous relaxation of surface topography.

We scale the relaxation frequency function (eq. \ref{Rf}) according to
\begin{eqnarray}
   &&\mathsf{R} = t_r^{-1} \mathsf{R}', \quad
 \mathsf{R}'(k') =  \frac{1}{k'}\frac{e^{4k'} +4k' e^{2k'} -1 }{e^{4k'} -2(1+2(k')^2)e^{2k'} +1}. \label{Rfsc}
\end{eqnarray}
Similarly, we scale the buoyancy transfer function according to
\begin{eqnarray}
   &&\mathsf{B} =  t_r^{-1} \mathsf{B}', \quad
 \mathsf{B}'(k') =   \frac{1}{k'}\frac{ 2(k'+1)e^{3k'}+2(k'-1)e^{k'} }{e^{4k'} -2(1+2(k')^2)e^{2k'} +1}. \label{Bsc}
\end{eqnarray}
Omitting primes on the variables, the evolution equations (\ref{hf}) and (\ref{sf}) scale to
\begin{eqnarray}
&&\frac{\partial \widehat{h}}{\partial t}+ \left[ik_x {\alpha}  +  \mathsf{R}\right]\widehat{h} = - \mathsf{B}\delta\widehat{s}\label{hfsc}\\
&&\frac{\partial \widehat{s}}{\partial t}+ [ik_x{\alpha}  + \delta \mathsf{R}]\widehat{s} = \widehat{m} -  \mathsf{B} \widehat{h},\label{sfsc}
\end{eqnarray}
where
\begin{equation}
\alpha = \frac{\bar{u} t_r}{H}  \label{uh_eq}
\end{equation}
is the background flow speed relative the the characteristic velocity scale
$H/t_r$.

\begin{figure}
  \centering
\includegraphics[width=0.95\textwidth]{figs/fig2.png}
\caption{Graphs of (scaled) $\mathsf{R}$ (eq. \ref{Rfsc}) and $\mathsf{B}$ (eq. \ref{Bsc}), as well as their difference and quotient. The limits $\mathsf{R}-\mathsf{B}\to 1/4$ and $\mathsf{R}/\mathsf{B}\to 1$
as $k\to 0$ can be proven using L'Hospital's rule.}
\label{fig:RB}
\end{figure}

\section{Regularization}
To remove the singularities in $\mathsf{R}$ and $\mathsf{B}$, we define the regularized
relaxation and buoyancy functions
\begin{eqnarray}
\mathsf{R}_\varepsilon = (\varepsilon + \mathsf{R}^{-1})^{-1}\\
\mathsf{B}_\varepsilon = (\varepsilon + \mathsf{B}^{-1})^{-1},
\end{eqnarray}
where $\varepsilon$ is a small regularization parameter. We choose
$\varepsilon$ such that the $k\to 0$ limit $\mathsf{R}_\varepsilon-\mathsf{B}_\varepsilon\to
\frac{1}{4}$ (Figure \ref{fig:RB}) is attained at $k = 10^{-3}$, resulting in
$\varepsilon \approx 2.5\times 10^{-14}$. This results in other analytical
limits in the problem being respected at small $k$ (see
below), so we think that this is a good idea and we don't feel bad about it.
These regularizations are only used to facilitate computation of the solutions
so there are no $\varepsilon$'s appearing in the analysis below.

\section{Analysis}

\subsection{Solution formulas}
We rewrite the system (\ref{hfsc})-(\ref{sfsc}) as
\begin{eqnarray}
\frac{\partial\pmb{y}}{\partial t} = \mathsf{A}\pmb{y} + \pmb{b}, \label{ydot}
\end{eqnarray}
where
\begin{eqnarray}
\pmb{y} = \left[\begin{array}{ll}
\widehat{h} \\
\widehat{s}
\end{array}\right], \quad
\pmb{b} =
\left[\begin{array}{ll}
0\\
\widehat{m}
\end{array}\right],\quad
\mathsf{A} =-\left[\begin{array}{ll}
ik_x{\alpha}+ \mathsf{R} & \delta\mathsf{B} \\
\mathsf{B} & ik_x{\alpha}+\delta \mathsf{R}
\end{array}\right],
\label{Amat}
\end{eqnarray}
with the matrix $\mathsf{A}$ not depending on time $t$.
We diagonalize $\mathsf{A}$ according to
\begin{eqnarray}
&& \mathsf{A} = \mathsf{P}\mathsf{D}\mathsf{P}^{-1},\quad
 \mathsf{P} = \left[\begin{array}{cc}
\vertbar & \vertbar \\
\pmb{\varphi}_- & \pmb{\varphi}_+ \\
\vertbar & \vertbar
\end{array}\right],\quad
 \mathsf{D} = \left[\begin{array}{ll}
\lambda_-  & 0 \\
0 & \lambda_+
\end{array}\right].
\end{eqnarray}
The eigenvalues ($\lambda_\pm$) and eigenvectors ($\pmb{\varphi}_\pm$)
of $\mathsf{A}$ are
\begin{eqnarray}
&&\lambda_\pm = -ik_x{\alpha}-\frac{\delta+1}{2} \mathsf{R} \pm \frac{\mu}{2} \label{Lambda} \\
&&\pmb{\varphi}_\pm = \left[\begin{array}{c}
\frac{\mathsf{R}(1-\delta)\mp\mu}{2\mathsf{B}} \\
 1
\end{array}\right]
\end{eqnarray}
where we have defined
\begin{equation}
\mu = \sqrt{4\delta\mathsf{B}^2 +  \mathsf{R}^2(\delta-1)^2}.
\end{equation}

See Figure \ref{fig:eigvals} for graphs of the real parts of the eigenvalues and Figure \ref{fig:eigvecs}
for graphs of the (normalized) eigenvector components over a range of $k$.
The main points are that
\begin{itemize}
\item the real parts of the eigenvalues are always negative---meaning that the
steady solutions are stable. In particular, $\lambda_+$ remains bounded as $k\to 0$
so that $\pmb{\varphi}_+$ becomes the dominant mode in the long-wavelength limit.
\item the eigenvector corresponding to the dominant (larger)
eigenvalue $\lambda_+$ has the limit $\pmb{\varphi}_+/\|\pmb{\varphi}_+\|\to [-\delta,1]^T$,
which drives the solution towards the perfect flotation condition. The other
eigenpair encourages deviations from perfect flotation, but this effect
decays as $k\to 0$ since $\lambda_-$ becomes very negative. However, it is still
important at shorter wavelengths.
\item The normalized eigenvectors approach $\pmb{\varphi}_+/\|\pmb{\varphi}_+\| \to [0,1]^T$ and $\pmb{\varphi}_-/\|\pmb{\varphi}_-\|\to [1,0]^T$
as $k\to \infty$, meaning that the surface and basal response becomes decoupled
at short wavelengths.
This just means that short-wavelength perturbations are not transmitted through the ice.
\end{itemize}

\begin{figure}
  \centering
\includegraphics[width=0.95\textwidth]{figs/fig3.png}
\caption{Real parts of eigenvalues of $\mathsf{A}$.}
\label{fig:eigvals}
\end{figure}

\begin{figure}
\centering
\includegraphics[width=0.95\textwidth]{figs/fig4.png}
\caption{Normalized eigenvectors $\pmb{\varphi}_\pm$ of $\mathsf{A}$. `$h$ comp.' is the first
vector component and `$s$ comp.' is the second component. Note that $\pmb{\varphi}_+\to[-\delta,1]^T$
as $k\to 0$, corresponding to the perfect flotation condition. }
\label{fig:eigvecs}
\end{figure}

\subsubsection{General solutions}
Assuming no initial perturbation ($\pmb{y}(0)=\pmb{0}$), the solution to (\ref{ydot}) is
\begin{eqnarray}
\pmb{y}(t) = \int_0^t e^{\mathsf{A}(t-\tau)} \pmb{b}(\tau)\;\mathrm{d}\tau, \label{ysol}
\end{eqnarray}
where $e^{\mathsf{A}t} = \mathsf{P}e^{\mathsf{D}t}\mathsf{P}^{-1}$ is the matrix exponential.
Using (\ref{ysol}), we find that
the solution for the ice-surface elevation anomaly is
\begin{eqnarray}
&&\widehat{h} = \widehat{m} * {\mathsf{K}}_h \nonumber, \\
&&{\mathsf{K}}_h  \equiv  -\frac{\delta\mathsf{B}}{\mu}(e^{\lambda_+ t}-e^{\lambda_-t}), \label{hfsol}
\end{eqnarray}
where $*$ denotes convolution over time. Similarly, the solution for the lower-surface elevation anomaly is
\begin{eqnarray}
  &&\widehat{s} = \widehat{m} * {\mathsf{K}}_s\nonumber, \\
 &&{\mathsf{K}}_s  \equiv  \frac{1}{2\mu}[(\mu-\chi)e^{\lambda_- t}+(\mu+\chi)e^{\lambda_+t}],\nonumber \\
  &&\chi \equiv (1-\delta)\mathsf{R}. \label{sfsol}
\end{eqnarray}
From these solution formulas we can see that $h$ will be close to $-\delta s$ provided that
$\mathsf{K}_h$ is close to $-\delta \mathsf{K}_s$ and $|m|$ is not too large\footnote{
More precisely, over some time frame $[0,T]$, we can use Young's convolution inequality,
H\"{o}lder's inequality, and the Plancherel theorem to estimate the deviation from
perfect flotation ($h=-\delta s$) via (skipping the details)
\begin{equation*}
\|h+\delta s\|_{2,2} \leq \|\mathsf{K}_h + \delta \mathsf{K}_s\|_{1,\infty} \times \|m\|_{2,2}
\end{equation*}
where we have defined the mixed Lebesgue norm of a function $f(x,y,t)$---or $f(k_x,k_y,t)$---by
\begin{equation*}
\|f\|_{p,q} = \left(\int_0^T \|f\|_{L^q(\mathbb{R}^2)}^p \; \mathrm{d}t \right)^{\frac{1}{p}}.
\end{equation*}
}.
We show the kernels $\mathsf{K}_s$ and $-\mathsf{K}_h/\delta$ in Figure \ref{fig:kernels},
highlighting that $\mathsf{K}_h(k,t)\approx-\delta\mathsf{K}_s(k,t)$ when
$(k,t)\in(0,1)\times(10^{-1},\infty)$. Therefore, and unsuprisingly,
significant deviations from perfect flotation only occur on fast timescales (relative to the relaxation time)
and short wavelengths (relative to ice thickness). We show an example of this short-wavelength
deviation in Figure \ref{fig:keel2}.

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig5.png}
\caption{Real parts of the kernels $\mathsf{K}_s$ and $-\mathsf{K}_h/\delta$ of the solution operator.
The dashed boxes denote the region in $(k,t)$ space where the flotation thickness is approximately satisfied
since $\mathsf{K}_h\approx -\delta \mathsf{K}_s$ .}
\label{fig:kernels}
\end{figure}

\subsubsection{Steady solutions}
We compute steady solutions via $[\widehat{h}_e,\widehat{s}_e]^T=\pmb{y}_e = -\mathsf{A}^{-1}\pmb{b}$ to find
\begin{align}
&\widehat{h}_e = -c^{-1}\delta\mathsf{B}\widehat{m}\\
&\widehat{s}_e = c^{-1}(\mathsf{R}+ik_x\alpha)\widehat{m}\\
&c = \delta^2(\mathsf{R}^2-\mathsf{B}^2)+ik_x\alpha(\delta+1)\mathsf{R}-k_x^2\alpha^2.
\end{align}
Similar to above, we see that $h_e$ will be close to $-\delta s_e$ when
$\mathsf{B}$ is close to $\mathsf{R} + ik_x\alpha$, which occurs at long wavelengths.

\subsection{Stability}
Next, we establish that the steady solutions are stable by showing that the
eigenvalues of $\mathsf{A}$ have negative real part.
First, note that $\mathrm{Re}(\lambda_-)<0$ follows immediately
from  (\ref{Lambda}).
For the other eigenvalue, we have
\begin{align}
\mathrm{Re}(\lambda_+) = -\frac{\delta+1}{2}\mathsf{R} + \frac{\mu}{2}
= \frac{1}{2}\left(-(\delta+1)\mathsf{R}+\sqrt{4\delta\mathsf{B}^2 + (\delta-1)^2\mathsf{R}^2 } \,\right). \label{RLp}
\end{align}
It follows from (\ref{RLp}) that $\mathrm{Re}(\lambda_+)<0$ if and only if
\begin{align}
&\sqrt{4\delta\mathsf{B}^2 + (\delta-1)^2\mathsf{R}^2 } < (\delta+1)\mathsf{R} \\
\iff &4\delta\mathsf{B}^2 + (\delta-1)^2\mathsf{R}^2 < (\delta+1)^2 \mathsf{R}^2 \\
\iff &4\delta\mathsf{B}^2 < 4\delta \mathsf{R}^2
\end{align}
Therefore, the stability criterion for a given wavevector is
\begin{align}
\mathrm{Re}(\lambda_+)<0  \iff &\mathsf{B}(k) < \mathsf{R}(k), \label{stability}
\end{align}
which states that viscous relaxation must exceed the bouyancy forcing
in order for the equilibrium to be stable (i.e., no runaway growth). \\ \\
{From (\ref{Rfsc}) and (\ref{Bsc}) (or Figure 2),
the condition (\ref{stability}) holds for all $k> 0$!}
\\ \\
Caveat: Of course, nonlinear
effects or other physics like an extensional background state (see Bassis and Ma, 2015)
not considered here could lead to instability.
\\ \\
Finally, it is worth noting that $\mathrm{Re}(\lambda_+)$
remains bounded in the limit $k\to 0$:
\begin{align}
\lim_{k\to 0 }\mathrm{Re}(\lambda_+) = - \frac{1}{2}\left(\frac{\delta}{\delta+1}\right) \approx -0.041 \text{ for } \delta=0.09,
\end{align}
according to SymPy (in my experience, proving that limit by hand is basically unreasonable).
On the other hand, clearly $\mathrm{Re}(\lambda_-)\to-\infty$. As stated before,
this means that $\pmb{\varphi}_+$ (the flotation-y one) is the dominant mode at long wavelengths.

\subsection{The role of advection}
Advection from the background flow---described by the $\alpha$ parameter---has two effects:
(1) it causes asymmetry and (2) it leads to smaller surface perturbations.
In the fast-flow limit ($\alpha\to\infty$), the perturbations decay to zero mathematically because
the kernels become highly oscillatory (cf. Riemann-Lebesgue lemma). Physically, this corresponds
to the case where the lower surface is ``re-paved'' by the background flow
faster than it can be disrupted by melting or freezing. We illustrate these effects in the
examples below.

\section{Velocity solutions}
So far, we have focused on the elevation perturbations $h$ and $s$. Once these are determined,
the (scaled) horizontal velocity solutions can be computed via
\begin{eqnarray}
\widehat{u} =  \frac{ik_x}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right), \label{uHf}\\
\widehat{v} = \frac{ik_y}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right),\label{vHf}
\end{eqnarray}
where $\mathsf{U}_h(k,z)$ and $\mathsf{U}_s(k,z)$ are (depth-dependent) response functions (Appendix).
We provide plots of the velocity response functions in Figure \ref{fig:response}.
In particular, we want to know when $u$ and $v$ are approximately depth-independent.
When $h\approx-\delta s$, $\widehat{u}$ and $\widehat{v}$ are proportional
to the difference $\mathsf{U}_h-\mathsf{U}_s$. We see
from Figure \ref{fig:response}c that this difference is depth-independent at longer wavelengths.
At shorter wavelengths, this is not necessarily the case but the horizontal velocity perturbations
will eventually vanish (since $\mathsf{U}_h$, $\mathsf{U}_s\to 0$).

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig6.png}
\caption{Horizontal velocity response functions $\mathsf{U}_h(k,z)$ and $\mathsf{U}_s(k,z)$,
as well as their difference, at the surface ($z=1$), base ($z=0$), and depth-averaged
over a range of $k$.}
\label{fig:response}
\end{figure}

\section{Some examples}
Finally, we will now look at some steady-state channels and keels. In all of these examples,
we choose a Gaussian-shaped melt rate anomaly that does not vary with time.
Therefore, we set
\begin{align}
m(x,y) = \pm m_0\exp\left(-\frac{1}{2}(x^2+y^2)/\sigma^2\right)
\end{align}
and choose an amplitude of $m_0=3.52\times 10^{-4}$, which corresponds to a melt
rate of 5 m/yr when $H=1000$ m and $\eta = 10^{13}$ Pa s.
In Figures \ref{fig:map}-\ref{fig:keel2}, we look at the effect of
varying the advection parameter $\alpha$, the width of the anomaly via $\sigma$,
and the sign of the anomaly (i.e. melting or freezing). Below, we use the notation $\|\cdot\|_\infty$
to denote the maximum absolute value of a scalar field (i.e. $h$, $s$, $m$) or
maximum flow speed. There is not much else to say right now, so I will
leave the rest to the figures.

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig7.png}
\caption{Map-plane plots of steady $h$ and $s$ for two different parameter sets.
Left panels: melting with no background advection. Right panels: freezing with
background advection. Panel (a) shows the $y=0$ centerline where the profiles in Figures \ref{fig:channel}-\ref{fig:keel2} are taken.}
\label{fig:map}
\end{figure}

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig8.png}
\caption{Steady ice-shelf profile highlighting $h$ and $s$, vertically exaggerated by
a factor of $\gamma=50$. The (exaggerated) perfect flotation elevation is shown
as a dashed line. The profiles are taken along the $y=0$ centerline.
Here, $m$ is positive with standard deviation $\sigma = 10/3$.
The in-plane velocity perturbation $[u,w]^T$ streamlines are
plotted on the deformed grid defined by $z\mapsto (1-z)\gamma s + z\gamma h$. }
\label{fig:channel}
\end{figure}

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig9.png}
\caption{Same as Figure \ref{fig:channel} except with $m<0$. }
\label{fig:keel}
\end{figure}

\begin{figure}
  \centering
\includegraphics[width=0.99\textwidth]{figs/fig10.png}
\caption{Same as Figure \ref{fig:keel} except with $\sigma = 1/3$
and a smaller vertical exaggeration of $\gamma=30$.}
\label{fig:keel2}
\end{figure}

\clearpage

\section{Appendix: Model derivation}
Here, we derive the model that is introduced in the main text.
We assume that the domain is an ice slab of finite thickness $H$ and infinite horizontal extent.
In $(x,y,z)$-space we assume this ice slab is defined by $|x|<\infty$, $|y|<\infty$, and $s\leq z\leq h$, where $h$ and $s$
are the upper and basal surfaces of the ice sheet, respectively.
We assume that $h$ and $s$ are uniform in the background state.

Assuming Newtonian and incompressible Stokes flow, ice deforms according to
\begin{eqnarray}
&&-p_x + \eta ( u_{xx} +u_{yy} + u_{zz}) = 0 \label{stokesbg1} \\
&&-p_y + \eta ( v_{xx}+v_{yy} + v_{zz}) = 0 \\
&&-p_z + \eta ( w_{xx} +w_{yy} + w_{zz}) = \rho_i g \\
&&u_x + v_y + w_z = 0, \label{stokesbg4}
\end{eqnarray}
where $[u,v,w]^T$ is the velocity, $p$ is the pressure, $\rho_i$ is ice density,
and $g$ is gravitational acceleration.
We assume a stress-free condition at the upper surface ($z=h$), which is equivalent to
\begin{eqnarray}
&&2\eta w_z - p = 0 \label{nostress1}\\
&&\eta(u_z +w_x) = 0 \\
&&\eta(v_z +w_y) = 0.\label{nostress3}
\end{eqnarray}

We consider zero shear stress at the ice-water interface
\begin{eqnarray}
&&u_z + w_x = 0 \label{sl1} \\
&&v_z + w_y = 0 \label{sl2}
\end{eqnarray}
along with a hydrostatic normal stress condition
\begin{eqnarray}
&&p-2\eta w_z  = \rho_w g(\ell-s) \label{hydstress}
\end{eqnarray}
where $\ell$ is sea level.
The upper and basal surfaces of the ice sheet evolve according to kinematic
equations that are coupled to the Stokes flow equations.
The upper surface evolves according to
\begin{eqnarray}
h_t + uh_x + vh_y = w+a,
\end{eqnarray}
where $a$ denotes accumulation or ablation at the ice-sheet surface.
Similarly, the basal surface evolves according to
\begin{eqnarray}
s_t + us_x + vs_y  = w + m \label{stbg}
\end{eqnarray}
where $m$ is the melt rate.

\subsection{Background states}
Perturbations will be taken with respect to a background state (denoted by bars)
corresponding to uniform flow in the $x$ direction and perfect flotation:
\begin{eqnarray}
  &&\bar{u} = \text{constant},\quad \bar{v}=0, \quad \bar{w}=0 \nonumber \\
  &&\bar{h}=H, \quad
  \bar{s}=0,\quad \bar{\ell}  = (\rho_i/\rho_w)H,\quad
  \bar{a} = 0, \quad
\bar{m} = 0.\quad
\end{eqnarray}

\subsection{Perturbation equations}
We introduce perturbations (denoted by 1's) to the background states via
\begin{eqnarray}
&&u= \bar{u} +  u^1, \quad v= \bar{v} +  v^1,\quad w =  \bar{w} + w^1 \nonumber \\
&&p = \bar{p} +  p^1,\quad s =  \bar{s} + s^1, \quad h = \bar{h} +  h^1, \quad m = \bar{m} + m^1, \label{perturbations}
\end{eqnarray}
where the perturbations are assumed to be small.
Note that we aren't considering perturbations to the accumulation/ablation rate.
We obtain equations for the perturbed fields by inserting the perturbations (\ref{perturbations}) into (\ref{stokesbg1})-(\ref{stbg})
and discarding the product terms (i.e., $f^1g^1$).

In this way, we obtain a homogeneous Stokes system for the perturbed fields
\begin{eqnarray}
&&-p_x^1 + \eta (u_{xx}^1 +u_{yy}^1+ u_{zz}^1) = 0 \label{per1} \\
&&-p_y^1 + \eta (v_{xx}^1 +v_{yy}^1+ v_{zz}^1) = 0 \\
&&-p_z^1 + \eta (w_{xx}^1 +w_{yy}^1+ w_{zz}^1) = 0\\
&&u_x^1 + v_y^1 + w_z^1 = 0, \label{per4}
\end{eqnarray}
and the surface kinematic equations become
\begin{eqnarray}
&&h_t^1 + \bar{u} h_x^1  = w^1  \label{ht} \\
&&s_t^1  + \bar{u} s_x^1 = w^1 + m^1. \label{st}
\end{eqnarray}

To account for changes in ice geometry,
we linearize the upper and lower surface boundary conditions at $z=H +  h^1$
and $z= s^1$ onto $z=H$ and $z=0$, respectively.
To do this, we
use a $1^\mathrm{st}$-order Taylor expansion in depth for a function $f(z)$:
$ f(z^0 + z^1) \approx f(z^0) + f_z(z^0)z^1. $
The stress-free condition at $z=H+h^1$ is approximated at $z=H$ by
\begin{eqnarray}
&&2\eta w_z^1 - p^1 = -\rho_i g h^1 \label{pnorm} \\
&&u_z^1 +w_x^1 = 0\label{pshear1}\\
&&v_z^1 +w_y^1 = 0 \label{pshear2}
\end{eqnarray}
Equation (\ref{pnorm}) states that the perturbed normal stress is balanced
by the perturbed cryostatic stress from the elevation anomaly.
Similarly, boundary conditions at the base become
\begin{eqnarray}
&&u_z^1 + w_x^1 = 0  \label{pslaw1}   \\
&&v_z^1 +w_y^1 = 0 \label{pslaw2}\\
&&2\eta w_z^1 - p^1 = \Delta\rho g s^1
\end{eqnarray}
where $\Delta\rho = \rho_w-\rho_i$.
We drop the ``1" superscripts below.

\subsection{Fourier transform approach}
We use the Fourier transform to solve the system (\ref{per1})-(\ref{per4}).
The Stokes flow equation become
\begin{eqnarray}
&&-ik_x\widehat{p} + \eta ( -k^2\widehat{u} + \widehat{u}_{zz}) = 0 \label{pft1} \\
&&-ik_y\widehat{p} + \eta ( -k^2\widehat{v} + \widehat{v}_{zz}) = 0 \\
&&-\widehat{p}_z + \eta (-k^2\widehat{w} + \widehat{w}_{zz}) = 0 \\
&&ik_x\widehat{u} + ik_y\widehat{v} + \widehat{w}_z = 0 \label{pft4}
\end{eqnarray}
Equation (\ref{pft1})-(\ref{pft4}) can be reduced to a fourth-order equation for the transformed vertical velocity
\begin{eqnarray}
&&\widehat{w}_{zzzz} - 2k^2 \widehat{w}_{zz} + k^4 \widehat{w}=0. \label{ode}
\end{eqnarray}
The general solution to (\ref{ode}) is
\begin{eqnarray}
\widehat{w} = \frac{A}{k}e^{k z} + \frac{B}{k}e^{-k z} + {C}ze^{k z}+ {D}ze^{-k z},\label{wformula}
\end{eqnarray}
where the constants $A,B,C,$ and $D$ depend on $k$.
To determine these coefficients, we will rewrite all of the boundary
conditions in terms of $\widehat{w}$ and its $z$ derivatives.
The $z$-derivatives of $\widehat{w}$ are
\begin{eqnarray}
&&\widehat{w}_{z} = {A}e^{k z} - {B}e^{-k z} + {C}e^{k z} + {C}kze^{k z} - {D}kze^{-k z} + {D}e^{-k z} \nonumber \\
&&\widehat{w}_{zz} = {Ak}e^{k z} + {Bk}e^{-k z} + {2Ck}e^{k z} + {C}k^2 ze^{k z} + {D}k^2ze^{-k z} - {2Dk}e^{-k z} \nonumber\\
&&\widehat{w}_{zzz} = {Ak^2}e^{k z} - {Bk^2}e^{-k z} + {3Ck^2}e^{k z} + {C}k^3 ze^{k z} - {D}k^3ze^{-k z} + {3Dk^2}e^{-k z}\nonumber \\ \label{wzzz}
\end{eqnarray}
For later reference,
the surface kinematic equations (\ref{ht})-(\ref{st}) transform to
\begin{eqnarray}
&&\widehat{h}_t + ik_x \bar{u}  \widehat{h} = \widehat{w},\label{hthat}\\
&&\widehat{s}_t +  ik_x \bar{u} \widehat{s} = \widehat{w}+\widehat{m}.\label{sthat}
\end{eqnarray}

The basal shear stress conditions (\ref{pslaw1})-(\ref{pslaw2}) become
\begin{eqnarray}
&&\widehat{u}_z + ik_x \widehat{w} = 0 \label{fslaw1} \\
&&\widehat{v}_z + ik_y \widehat{w} = 0. \label{fslaw2}
\end{eqnarray}
Multiplying (\ref{fslaw1})-(\ref{fslaw2}) by $-ik_x$ and $-ik_y$, respectively, summing the equations, and using the transformed incompressibility condition (\ref{pft4}), we obtain
\begin{eqnarray}
\widehat{w}_{zz} + k^2 \widehat{w} = 0.\label{ftslaw}
\end{eqnarray}
Similarly, the shear stress condition (\ref{pshear1})-(\ref{pshear2}) at the upper surface becomes
\begin{eqnarray}
\widehat{w}_{zz} + k^2 \widehat{w} = 0. \label{ftsstress}
\end{eqnarray}
The normal-stress condition at the upper surface (\ref{pnorm}) transforms to
$
2\eta \widehat{w}_{z} - \widehat{p} = -\rho_i g\widehat{h}.
$
The transformed Stokes equations (\ref{pft1})-(\ref{pft4}) imply that
$-k^2 \widehat{p} = \eta(k^2\widehat{w}_z - \widehat{w}_{zzz}) $, which reduces this expression for
the normal-stress condition to
\begin{eqnarray}
\eta (3k^2 \widehat{w}_{z}-\widehat{w}_{zzz})  = -k^2 \rho_i g \widehat{h}.\label{ftnstress}
\end{eqnarray}
Similar, the normal stress condition at the base becomes
\begin{eqnarray}
\eta (3k^2 \widehat{w}_{z}-\widehat{w}_{zzz})  = k^2 \Delta\rho g \widehat{s}. \label{b4alt}
\end{eqnarray}

To simplify notation, we define the scaled wavevector length $${k'} \equiv kH.$$
Using the formulas (\ref{wformula})-(\ref{wzzz}) evaluated at $z=H$,
the normal stress condition (\ref{ftnstress}) reduces to
\begin{eqnarray}
{A} e^{{k'}} - {B} e^{-{k'}} + {C}{k'} e^{{k'}} - {D}{k'} e^{-{k'}}
=- \frac{\rho_i g }{2\eta }\widehat{h} \equiv b_1, \label{b1}
\end{eqnarray}
and the upper-surface shear-stress condition (\ref{ftsstress}) becomes
\begin{eqnarray}
&&A e^{{k'}} + B e^{-{k'}} + C({k'}+1) e^{{k'}} +D({k'}-1) e^{-{k'}} =  0.
\end{eqnarray}
Using the formulas (\ref{wformula})-(\ref{wzzz}) evaluated at $z=0$,
the lower surface shear stress condition (\ref{ftslaw}) reduces to
\begin{eqnarray}
{A} + {B} + {C}  - {D} = 0.
\end{eqnarray}
Finally the normal stress condition at the base reduces to
\begin{eqnarray}
A-B = \frac{\Delta \rho g}{2\eta}\widehat{s}\equiv b_2. \label{b2}
\end{eqnarray}
The conditions (\ref{b1})-(\ref{b2}) form a linear system for the coefficients $\{A,B,C,D\}$.

Solving for $\{A,B,C,D\}$ symbolically, the vertical velocity at the upper surface is then given by
\begin{eqnarray}
w|_{z=H} =  -\mathsf{R}_f\widehat{h} - \mathsf{B}\delta\widehat{s} \label{wfloat}
\end{eqnarray}
where the surface relaxation function is given by
\begin{eqnarray}
\mathsf{R} = \left(\frac{\rho_i g}{2\eta k}\right) \frac{e^{4{k'}} +4{k'} e^{2{k'}} -1 }{e^{4{k'}} -2(1+2{k'}^2)e^{2{k'}} +1},
\end{eqnarray}
the buoyancy transfer function is given by
\begin{eqnarray}
&&\mathsf{B} = \left(\frac{\rho_i g}{2\eta k}\right) \frac{ 2({k'}+1)e^{3{k'}}+2({k'}-1)e^{{k'}} }{e^{4{k'}} -2(1+2{k'}^2)e^{2{k'}} +1},
\end{eqnarray}
and we have defined
\begin{eqnarray}
&&\delta = \rho_w/\rho_i -1.
\end{eqnarray}
Combining (\ref{hthat}) and (\ref{wfloat}), the upper surface evolves according to
\begin{eqnarray}
\frac{\partial \widehat{h}}{\partial t}+ \left[ik_x \bar{u}  + \mathsf{R}\right]\widehat{h} = -\mathsf{B}\delta\widehat{s}.
\end{eqnarray}
We also have to determine the evolution of the lower surface.
Using $\widehat{w}|_{z=0}= \frac{1}{k}(A+B)$, we obtain
\begin{eqnarray}
\widehat{w}|_{z=0} = -\mathsf{R}\delta\widehat{s} - \mathsf{B} \widehat{h}, \label{wbfloat}
\end{eqnarray}
which, along with (\ref{sthat}), implies that the basal surface evolves according to
\begin{eqnarray}
\frac{\partial \widehat{s}}{\partial t}+ [ik_x\bar{u} + \delta\mathsf{R}]\widehat{s} = \widehat{m} - \mathsf{B} \widehat{h}.
\end{eqnarray}
%
\subsection{Horizontal velocity solutions}
Here we derive the horizontal velocity solutions.
Rearranging the transformed Stokes equations:
\begin{eqnarray}
  &&  \widehat{u}_{zz}-k^2\widehat{u} = \frac{ik_x}{\eta}\widehat{p} \\
  &&  \widehat{v}_{zz}-k^2\widehat{v} = \frac{ik_y}{\eta}\widehat{p}
\end{eqnarray}
These have the general solutions
\begin{eqnarray}
\widehat{u}(z) = \frac{ik_x }{2\eta k} \left(e^{kz}\int_0^z \widehat{p}(z')e^{-kz'}\;\mathrm{d}z' -
e^{-kz}\int_0^z \widehat{p}(z')e^{kz'}\;\mathrm{d}z'\right)
+ E e^{kz} + F e^{-kz} \\
\widehat{v}(z) = \frac{ik_y }{2\eta k} \left(e^{kz}\int_0^z \widehat{p}(z')e^{-kz'}\;\mathrm{d}z' -
e^{-kz}\int_0^z \widehat{p}(z')e^{kz'}\;\mathrm{d}z'\right)
+ G e^{kz} + I e^{-kz}
\end{eqnarray}
where $\{E,F,G,I\}$ depend on the boundary conditions.
As noted before, the body equations (\ref{pft1})-(\ref{pft4}) imply that
\begin{eqnarray}
\widehat{p} = \eta\left(\frac{1}{k^2}\widehat{w}_{zzz}-\widehat{w}_z \right). \label{pexpr}
\end{eqnarray}
We substitute (\ref{pexpr}) into the integrals above
and integrate the $\widehat{w}_{zzz}$ term by parts twice.
To this end, we use the identity
\begin{eqnarray}
\int_0^z \left( \frac{1}{k^2} \widehat{w}_{zzz} - \widehat{w}_{z}\right) e^{\pm kz'}\;\mathrm{d}z'
= \frac{1}{k^2} \left[e^{\pm kz'}\widehat{w}_{zz}\right]_0^z - \frac{\pm k}{k^2}\left[\widehat{w}_ze^{\pm kz'} \right]_0^z
\end{eqnarray}
and find that the pressure integrals reduce to
\begin{eqnarray}
&&e^{kz}\int_0^z \widehat{p}(z')e^{-kz'}\;\mathrm{d}z' -
  e^{-kz}\int_0^z \widehat{p}(z')e^{kz'}\;\mathrm{d}z' \nonumber
  \\ &&= \frac{2\eta}{k} \left( \widehat{w}_z-\widehat{w}_z|_{z=0}\cosh(kz) - \frac{1}{k} \widehat{w}_{zz}|_{z=0}\sinh(kz)  \right).
\end{eqnarray}
Therefore, we obtain
\begin{eqnarray}
&&\widehat{u}(z) = \frac{ik_x }{k^2} P(z)
+ E e^{kz} + F e^{-kz} \label{uexpr}\\
&&\widehat{v}(z) = \frac{ik_y }{k^2} P(z) + G e^{kz} + I e^{-kz} \label{vexpr} \\
&&P(z) = \widehat{w}_z-\widehat{w}_z|_{z=0}\cosh(kz) - \frac{1}{k} \widehat{w}_{zz}|_{z=0}\sinh(kz)
\end{eqnarray}
We can determine the constants $\{E,F,G,I\}$ from the stress boundary conditions.
First, we note that
\begin{eqnarray}
&&\widehat{u}_z(z) = \frac{ik_x }{k^2} P_z(z)
+ E k e^{kz} - F k e^{-kz} \\
&&\widehat{v}_z(z) = \frac{ik_y }{k^2} P_z(z) + G k  e^{kz} - I k e^{-kz} \\
&&P_z(z) = \widehat{w}_{zz}-\widehat{w}_z|_{z=0}k\sinh(kz) - \widehat{w}_{zz}|_{z=0}\cosh(kz)\\
&&P_z(0) = 0 = P(0).
\end{eqnarray}
The stress-free conditions at $z=H$ (i.e., $\widehat{u}_z = -ik_x \widehat{w}$ and
$\widehat{v}_z = -ik_y \widehat{w}$) imply
\begin{eqnarray}
 &&E  e^{{k'}} - F  e^{-{k'}} = -\frac{ik_x}{k^2} \left(k\widehat{w}|_{z=H}+\frac{1}{k}P_{z}(H) \right)\equiv b_3 \label{b4eq}\\
  && G  e^{{k'}} - I  e^{-{k'}} = -\frac{ik_y}{k^2} \left(k\widehat{w}|_{z=H}+\frac{1}{k}P_{z}(H)\right) \equiv b_3'. \label{b4peq}
\end{eqnarray}
For later convenience in deriving the response functions,
we note that $b_3'$ is analogous to $b_3$.
Similarly, the shear stress conditions at the lower surface imply
\begin{eqnarray}
&&E - F  = -\frac{ik_x}{k^2}\left(k\widehat{w}|_{z=0} \right)  \equiv b_5,\\ \label{b5eq}
&&G  -I = -\frac{ik_y}{k^2}(k\widehat{w}|_{z=0}) \equiv b_5'. \label{b5peq}
\end{eqnarray}
As before, we note that $b_4'$ is analogous to $b_4$.
At this point, we can solve symbolically for $\{E,F,G,I\}$.
Then, we can solve for the velocity solutions via (\ref{uexpr})-(\ref{vexpr}).

Using SymPy, the velocities can be written in terms of $\{\widehat{h},\widehat{s}\}$ as
\begin{eqnarray}
\widehat{u} =  \frac{\rho_i g}{2\eta }\frac{ik_x}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right) \label{uHf}\\
\widehat{v} = \frac{\rho_i g}{2\eta}\frac{ik_y}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right)\label{vHf}
\end{eqnarray}
where the response functions are given by
\begin{align}
&\mathsf{U}_h =  \left[ z(2k'(e^{2k'} + e^{2k'z'}) -(1-z')(e^{2k'(z'+1)} + e^{2k'} - e^{2k'z'} -1 )  )      \right]\mathsf{Q}^{-1}e^{k'}\\
&\mathsf{U}_s =  \left[ z(e^{4k'} +e^{2k'(z'+1)}-e^{2k'}-e^{2k'z'}  ) -(1-z')2k'e^{2k'}(e^{2k'z'}+1)     \right]\mathsf{Q}^{-1}\\
&\mathsf{Q} = e^{{k'z'}}\left(e^{4{k'}} -2(1+2{k'}^2)e^{2{k'}} +1 \right)/k'
\end{align}
where we have defined the scaled height $$z'=H^{-1}z\in[0,1].$$

With the scaling in the main text, the above equations
take the nondimensional form
\begin{eqnarray}
\widehat{u} =  \frac{ik_x}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right), \label{uHf}\\
\widehat{v} = \frac{ik_y}{k^2}\left(\mathsf{U}_h\widehat{h} +  \mathsf{U}_s\delta\widehat{s}\right),\label{vHf}
\end{eqnarray}
noting that the response functions are already nondimensional.

\end{document}


\section{Financing, Part II}

\subsection*{MM with taxes}

Notation: $U$: Unlevered, $L$: Levered,
 $X$: Terminal value, $\tau$: taxes. \\
Firm $U$: $(1-\tau)X$ \\
Firm $L$: $(1-\tau)(X-r_D D) + r_D D = (1-\tau)X + \tau r_D D $ \\
$V_U =\frac{(1-\tau)X}{1+r_A} $ \\
$V_L =\frac{(1-\tau)X}{1+r_A} + \frac{\tau r_D D}{1+r_D} = V_U + \frac{\tau r_D D}{1+r_D}  $ \\

The value of a levered firm equals the value of the unlevered
firm (with the same assets) plus the present value of the {\bf tax shield}

$V_L = V_U + PV(\text{debt tax shield}) = V_U + PVTS$ where

$PVTS = PV(Interest \cdot \tau)$ use $r_A$ to discount.

\subsection*{Costs of financial distress }

$V_L = V_U + PV(\text{debt tax shield}) - PV (\text{cost of financial distress}) = APV$. Where $APV$: Adjusted present value.

\subsection*{MM with personal tax}

Notation: Debt level at $D$ with interest rate $r_D$.
Corporate tax rate $\tau$. Investors pay additional personal taxes:
\begin{itemize}
	\item Tax rate on equity (dividend and capital gain) $\pi$.
	\item Tax rate on debt (interest) $\delta$.
\end{itemize}

total after-tax cashflow:

$\underbrace{(1-\pi)(1-\tau)X}_{\text{all equity firm}} + \underbrace{[(1-\delta)-(1-\pi)(1-\tau)]r_DD}_{\text{tax impact of debt}}$

$V_L = V_U + [(1-\delta)-(1-\pi)(1-\tau)]PV(r_DD) $


Over the course of this project, we employed a broad spectrum of data
analysis and machine learning techniques to develop fast and high-quality
surrogates for a~MC TBR simulation in use at~UKAEA. Having implemented a sampling
software to efficiently evaluate this expensive MC~model, we deployed it on a
high performance cluster to generate over~\num{900000}
datapoints for training and test purposes. We
investigated possibilities for simplification of the parameter space, and
concluded that no straightforward reduction was possible. After reviewing
9~surrogate model families, examining their behaviour on constrained and
unrestricted feature space, and studying their scaling properties, we retrained
the best-performing instances to produce properties desirable for
practical use. The fastest surrogate, an artificial neural network trained
on~\num{500000} datapoints, featured~$R^2=\num{0.985}$ with mean prediction time
of~$\SI{0.898}{\micro\second}$, representing a relative
speedup of $8\cdot 10^6$ with respect to the MC model. Alternatively, we
also demonstrated the possibility of achieving comparable results using only a
training set of size~\num{10000}.

After a thorough review of the literature, we developed a novel adaptive
sampling algorithm, QASS, capable of interfacing with any of the studied
surrogates. Preliminary testing on a toy theory, qualitatively comparable to
the MC TBR model, demonstrated the effectiveness of QASS and behavioural trends
consistent with the design of the algorithm. With~\num{100000} initial samples and 100 incremental samples per iteration, QASS achieved a ${\sim}40\%$ decrease in surrogate error as compared with a baseline random sampling scheme. Equivalently, the same surrogate error as the baseline could be achieved with ${\sim}6\%$ fewer total samples of the expensive theory. Further optimisation over the hyperparameter space has strong
potential to increase this performance, in particular by decreasing the required quantity of initial samples. This will allow for future deployment of QASS
in coalition with any of the most effective identified surrogates to facilitate
learning during evaluation of the MC TBR model.

\section*{Acknowledgements}

The authors would like to thank Simeon Bash, Vignesh Gopakumar, Prof.~Nikolaos
Konstantinidis, Nikolaos Nikolaou, Prof.~Emily Nurse, Jonathan Shimwell and Ingo
Waldmann for their supervision and valuable suggestions related to this work.


\subsubsection{Pade form}
\label{sec:onebodyjastrowpade}

Although the spline Jastrow factor is the most flexible and most commonly used form implemented in \qmcpack,
there are times where its flexibility can make it difficult to optimize.  As an example, a spline Jastrow
with a very large cutoff can be difficult to optimize for isolated systems such as molecules because of the small
number of samples present in the tail of the function.  In such cases, a simpler functional
form might be advantageous.  The second-order Pade Jastrow factor, given in Equation~\ref{padeeqn}, is a good choice
in such cases.
\begin{equation}
\label{padeeqn}
u_{ab}(r) = \frac{a*r+c*r^2}{1+b*r} .
\end{equation}
Unlike the spline Jastrow factor, which includes a cutoff, this form has an infinite range and will be applied to every particle
pair (subject to the minimum image convention).  It also is a cuspless Jastrow factor,
so it should be used either in combination with a single particle basis set that contains the proper cusp or
with a smooth pseudopotential.

\paragraph{Input specification}
\begin{table}[h]
\begin{center}
\begin{tabular}{l c c c l }
\hline
\multicolumn{5}{l}{Correlation element} \\
\hline
\bfseries Name & \bfseries Datatype & \bfseries Values & \bfseries Defaults & \bfseries Description \\
\hline
ElementType & Text & Name & See below & Classical particle target  \\
\hline
\multicolumn{5}{l}{Elements}\\ \hline
& Coefficients & & & \\ \hline
\multicolumn{5}{l}{Contents}\\ \hline
& (None)  & & &  \\ \hline
\end{tabular}
%\end{tabular*}
\end{center}
\end{table}

\begin{table}[h]
\begin{center}
\begin{tabular}{l c c c l }
\hline
\multicolumn{5}{l}{Parameter element} \\
\hline
\bfseries Name & \bfseries Datatype & \bfseries Values & \bfseries Defaults & \bfseries Description \\
\hline
Id & String & Name & (Required) & Name for variable \\
Name & String & A or B or C & (Required) & See Equation~\ref{padeeqn}\\
Optimize & Text & Yes or no & Yes & If no, values are fixed in optimizations \\
\hline
\multicolumn{5}{l}{Elements}\\ \hline
(None) & & & \\ \hline
\multicolumn{5}{l}{Contents}\\ \hline
 (No name) & Real & Parameter value & (Required) & Jastrow coefficients \\ \hline
\end{tabular}
%\end{tabular*}
\end{center}
\end{table}

\paragraph{Example use case}
\label{sec:1bjpadeexamples}

Specify a spin-independent function with independent Jastrow factors for two different species (Li and H).
The name of the particleset holding the ionic positions is ``i."
\begin{lstlisting}[style=QMCPXML]
<jastrow name="J1" function="pade2" type="One-Body" print="yes" source="i">
  <correlation elementType="Li">
    <var id="LiA" name="A">  0.34 </var>
    <var id="LiB" name="B"> 12.78 </var>
    <var id="LiC" name="C">  1.62 </var>
  </correlation>
  <correlation elementType="H"">
    <var id="HA" name="A">  0.14 </var>
    <var id="HB" name="B"> 6.88 </var>
    <var id="HC" name="C"> 0.237 </var>
  </correlation>
</jastrow>
\end{lstlisting}


\chapter{Additional Code Blocks for Differential Evolution} % Main appendix title
\label{diff_evo_app} % Change X to a consecutive letter; for referencing this appendix elsewhere, use \ref{AppendixX}
This appendix includes additional Code Blocks relevant to the introduction of the DE optimisation method discussed in Section~\ref{sec:de}.
Code Block~\ref{cb:mut} describes the mutation step of the DE algorithm, as described by Bj\"{o}rck.\sidecite<-1\baselineskip>{bjorck_fitting_2011}
%
\begin{listing}
    \centering
    \caption{The mutation step method for a DE algorithm, as described in \cite{bjorck_fitting_20112}. The input variables are \texttt{p} which is an array of floats representing the parent population, \texttt{b} which is an array of floats representing the best vector of the parent population, and \texttt{km} the mutant constant. This function returns an array of floats representing the mutant vector.}
    \lstinputlisting{reports/code_blocks/mutation.py}
    \label{cb:mut}
\end{listing}
%
Code Block~\ref{cb:recomb} describes the recombination step of the DE algorithm, as described by Bj\"{o}rck.
%
\begin{listing}
    \centering
    \caption{The recombination step method for a DE algorithm, as described in \cite{bjorck_fitting_20112}. The input variables are \texttt{p} which is an array of floats representing the parent population, \texttt{m} which is an array of floats representing the mutant vector, and \texttt{kr} the recombination constant. This function returns an array of floats representing the offspring population.}
    \lstinputlisting{reports/code_blocks/recombination.py}
    \label{cb:recomb}
\end{listing}
%
Code Block~\ref{cb:sel} describes the classical step of the DE algorithm, as described by Bj\"{o}rck.
%
\begin{listing}
    \centering
    \caption{The classical selection step used in a DE algorithm, as described in \cite{bjorck_fitting_20112}. The input variables are \texttt{p} which is an array of floats representing the parent population, \texttt{m} which is an array of floats representing the offspring vector, and \texttt{f} is the function to be minimised. This function returns an array of floats representing the new parent population.}
    \lstinputlisting{reports/code_blocks/selection.py}
    \label{cb:sel}
\end{listing}
%


\documentclass[11pt]{article}
\usepackage[a4paper,margin= 2cm]{geometry}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\title{\LARGE{\bf{Task 5 - QR Decomposition and Least Squares }}}
\author{\Large{\bf{Kirtan Patel - AE19B038}}}
\date{}

\begin{document}

\maketitle

\section{Introduction}
Often times the data that we get is not linear and using higher order polynomials gives a better fit than using Linear Regression.\\

In Task 2,  we performed Linear Regression on each dataset, taking the first 50,100 and 200 datapoints one after the other.

Plotting the Regression line as
\[Y = \beta_1 X + \beta_0\]
we record the values obtained for $\beta_0$ and $\beta_1$.\\

We did that by converting it into matrix form as :
\[A.x = B\]
where x = [$\beta_0~~\beta_1$]\textsuperscript{T}. We further solved this by premultiplying A$^T$, getting a square matrix on the LHS (A$^T$.A gives a square matrix) and then finding the inverse of this square matrix. We pre-mulitply the inverse matrix and get the solution for vector x, ans in-turn get the coefficients of Linear Regression.\\

Finding the inverse of a matrix is computationally heavy and hence there are other optimized methods (eg. LU Decomposition) which help us reduce the complexity of the code. One such method is the QR decomposition of matrix A.

\section{QR Decomposition}
The QR decomposition (also called the QR factorization) of a matrix is a decomposition
of the matrix into an orthogonal matrix and a triangular matrix. A QR decomposition of
a real square matrix A is a decomposition of A as:
\[A = QR\]
where Q is an orthogonal matrix (i.e. Q$^T$Q = I) and R is an upper triangular matrix. If A is nonsingular, then this factorization is unique.\\

There are several methods for actually computing the QR decomposition. One of such
method is the Gram-Schmidt process.\\

After seperating A into Q and R, we pre-multiply Q$^T$ and since Q is orthogonal, we get :
\[ R.x = Q^T B\]

And since R is an upper triangular matrix, we can solve for the components of x using back substitution form the last term to the first term (bottom to the up). This process is computationally faster than finding the inverse of a matrix and hence is used. There are other faster processes as well which are computationally more efficient than the processes mentioned and discussed here.
\newpage
\subsection{Gram-Schmidt Process}
\begin{figure}[h!]
	\centering
	\centering
	\includegraphics[width=\linewidth]{GS_Process}
	\label{GS_orthogonalization}
\end{figure}

\section{Task 5}
Keeping the above mentioned points in mind, in Task 5, we are given a data set. Using QR Decomposition with Gram-Schmidt Orthogonalization and back-substitution(without using canned routines), we need to find the Regression Coefficients for a quadratic regression. i.e.
\[ Y = \beta_2 x^2 ~+~ \beta_1 x ~+~ \beta_0\]

Furthermore, we have to compare this method of solving the Matrix Equation to the method of matrix inversion used in Task 2. The comparison is to be made on the  accuracy and the time taken for the methods to execute and return the coefficients.\\

We were even asked to justify if the bi-quadratic regression line is a better fit.
\[Y = \beta_4 x^4 ~+~ \beta_3 x^3 ~+~\beta_2 x^2 ~+~ \beta_1 x ~+~ \beta_0\]

To take measurement for the time parameter, the built-in time module has been imported, whereas to measure the accuracy/fit of the model, instead of simple square-error, the R-sqaure is used. R-squared evaluates the scatter of the data points around the fitted regression line. It is also called the coefficient of determination, or the coefficient of multiple determination for multiple regression. For the same data set, higher R-squared values represent smaller differences between the observed data and the fitted value. Usually, the larger the R2, the better the regression model fits your observations.

\section{Results}
\begin{figure}[h!]
	\centering
	\centering
	\includegraphics[width=0.7\linewidth]{Task5}
	\label{Plot}
\end{figure}

As we can see the plot is highly intervined, and the regression lines from all the methods are extremely similar.

\subsection{Regression Coefficients}
Tabulating the results obtained for the Regression Coefficients.
\begin{table} [h!]
	\centering
	\begin{tabular}{| l | l | l | l | l | l |}
		\hline
		Model - Solving Method & $\beta_0$ & $\beta_1$ & $\beta_2$  & $\beta_3$ & $\beta_4$\\
		\hline \hline
		Quadratic Model - QR Decomposition & -0.30539967 & 1.00663479 & 1.0066348  & na & na \\
		\hline
		Quadratic Model - Matrix Inversion & -0.30539967 & 1.00663479  & 0.0996548  & na & na \\
		\hline
		Biquadratic Model - QR Decomposition & -0.31756812 & 1.00848424  & 0.1056871  & -0.0017 & 0.00011314\\
		\hline
	\end{tabular}
\end{table}

\subsection{Time Taken}
The computational times were not coming out to be constant but were varying around a range. Thus a range of runtimes has been tabulated.
\begin{table} [h!]
	\centering
	\begin{tabular}{| l | l |}
		\hline
		Model - Solving Method & time (ms)\\
		\hline \hline
		Quadratic Model - QR Decomposition & 0.30~-~0.35 \\
		\hline
		Quadratic Model - Matrix Inversion & 0.40~-~0.50 \\
		\hline
	\end{tabular}
\end{table}

\subsection{Accuracy}
\begin{table} [h!]
	\centering
	\begin{tabular}{| l | l |}
		\hline
		Model - Solving Method & R-squared\\
		\hline \hline
		Quadratic Model - QR Decomposition & 0.9998552213770414 \\
		\hline
		Quadratic Model - Matrix Inversion & 0.9998552213770575 \\
		\hline
		Biquadratic Model - QR Decomposition & 0.9998596029906132 \\
		\hline
	\end{tabular}
\end{table}
We can see that while both the Quadratic Models have almost same accuracy, the Biquadratic Model is slightly more accurate.
\pagebreak

\section{References}
\begin{enumerate}
	\item https://www.math.ucla.edu/~yanovsky/Teaching/Math151B/handouts/GramSchmidt.pdf
	\item
	https://www.r-bloggers.com/2017/03/qr-decomposition-with-the-gram-schmidt-algorithm/
	\item
	https://en.wikipedia.org/wiki/Gram$\%$E2$\%$80$\%$93Schmidt$\_$process
	\item
	http://math.iit.edu/~fass/477577$\_$Chapter$\_$4.pdf
	\item
	https://www.youtube.com/watch?v=w2FKXOa0HGA
\end{enumerate}

\end{document}


\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{amsmath, enumerate, multicol}

\newcommand\sumone{\sum_{n=1}^{\infty}}
\newcommand\sumzero{\sum_{n=0}^{\infty}}
\newcommand\sumtwo{\sum_{n=2}^{\infty}}

\renewcommand{\baselinestretch}{1.3}
\usepackage[width=16.00cm, height=24.00cm]{geometry}
\author{Nith Kosal}
\title{Problem Sets with R Programming}
\date{June 14, 2021}
\begin{document}
	\maketitle

\subsection*{Problem 1: Vectors}

\begin{enumerate}
	\item Create the vectors:
	\begin{enumerate}
		\item (1, 2, 3, ..., 29, 30)
		\item (30, 29, 28, ..., 2, 1)
		\item (1, 2, 3, ..., 19, 20, 19, 18, ..., 2, 1)
		\item (44, 66, 33) and assign it to the name \texttt{futureforum}.
		For parts (e), (f) and (g) look at the help for the function \texttt{rep}.

		\item (44, 66, 33, 44, 66, 33, ..., 44, 66, 33) where there are 20 occurrences of 44.
		\item (44, 66, 33, 44, 66, 33, ..., 44, 66, 33, 44) where there are 11 occurrences of 44, 10 occurrences of 66 and 10 occurrences of 33.
		\item (44, 44, ..., 44, 66, 66, ..., 66, 33, 33, ..., 33) where there are 10 occurrences of 44, 20 occurrences of 44, 20 occurrences of 66, 30 occurrences of 33.

	\end{enumerate}
    \item Create a vector of the value of $e^{x}cos(x)$ at $x$ = 3, 3.1, 3.2, ..., 7.

    \item Create the following vectors:
	\setlength\columnsep{0cm}

	\begin{multicols}{2}
		\begin{enumerate}
			\everymath{\displaystyle}
			\item $(0.1^{3}0.2^{1}, 0.1^{6}0.2^{3}, ..., 0.1^{37}0.2^{32})$
			\item $(2, \dfrac{2^{2}}{4}, \dfrac{2^{3}}{4}, \dfrac{2^{4}}{5}, ..., \dfrac{2^{26}}{26})$
		\end{enumerate}
	\end{multicols}

	\item Calculate the following:
	\setlength\columnsep{0cm}

	\begin{multicols}{2}
		\begin{enumerate}
			\everymath{\displaystyle}
			\item $\sum_{n=1}^{200}(n^{3}+4n^{2})$
			\item $\sum_{n=1}^{25}(\dfrac{2^{n}}{n} + \dfrac{3^{n}}{n^{2}})$
		\end{enumerate}
	\end{multicols}

	\item Use the function \texttt{paste} to create the following character vectors.
	\begin{enumerate}
		\item \texttt{("Cambodia 1", "Cambodia 2", ..., "Cambodia 34")}. Note that there is a single space between \texttt{Cambodia} and the number following.
		\item \texttt{("ffteam1", "ffteam2", ..., "ffteam29")}. Note that there are is no space between \texttt{ttteam} and the number following.
	\end{enumerate}

	\item Execute the following lines which create two vectors of random integers which are chosen with replacement from the integers 0, 1, ..., 999. Both vector have length 250.

	\texttt{set.seed(50) \\
	xVec <- sample(0:999, 250, replace=T)\\
	yVec <- sample(0:999, 250, replace=T)}

	Suppose $x = (x_{1}, x_{2}, ..., x_{n})$ denotes the vector \texttt{xVec} and $y = (y_{1}, y_{2}, ..., y_{n})$ denotes the vector \texttt{yVec}.

	\begin{enumerate}
		\item Create the vector $(y_{2}-x_{1}, ..., y_{n}-x_{n-1})$
		\item Create the vector $\left ( \dfrac{sin(y_{1})}{cos(x_{2})}, \dfrac{sin(y_{2})}{cos(x_{3})}, ..., \dfrac{sin(y_{n-1})}{cos(x_{n})}\right )$
		\item Create the vector $(x_{1} + 2x_{2} - x_{3}, x_{2} + 2x_{3} - x{4}, ..., x_{n-2} +2x_{n-1} - x_{n})$
		\everymath{\displaystyle}
		\item Calculate $\sum_{i=1}^{n-1} \dfrac{e^{-x_{i+1}}}{x_{i} + 10}$
	\end{enumerate}

	\item Use the vectors \texttt{xVec} and \texttt{yVec} created in the previous question and the functions \texttt{sort}, \texttt{order}, \texttt{mean}, \texttt{sqrt}, \texttt{sum} and \texttt{abs}.

	\begin{enumerate}
		\item Pick out the values in \texttt{yVec} which are >400.
		\item What are the index positions in \texttt{yVec} of the values which are >400?
		\item What are the values in \texttt{xVec} which correspond to the values in \texttt{yVec} which are >400? (By correspond, we mean at the same index positions.)
		\item Create the vector $(|x_{1} - \bar{X}|^{1/2}, |x_{2} - \bar{X}|^{1/2}, ..., |x_{n} - \bar{X}|^{1/2})$ where $\bar{X}$ denotes the mean of the vector $X = (x_{1}, x_{2}, ..., x_{n})$.

		\item How many values in \texttt{yVec} are within 200 of the maximum value of the terms in \texttt{yVec}?

		\item How many numbers in xVec are divisible by 2? (Note that the modulo operator is denoted \texttt{\%\%}.)

		\item Sort the numbers in the vector \texttt{xVec} in the order of increasing values in \texttt{yVec}.

		\item Pick out the elements in \texttt{yVec} at index positions 1, 4, 7, 10, 13, ...

	\end{enumerate}

	\item By using the function \texttt{cumprod} or otherwise, calculate

	$1 + \dfrac{2}{3} + \dfrac{24}{35} + \dfrac{246}{357} + ... + (\dfrac{2}{3} \dfrac{4}{5} ... \dfrac{38}{39})$
\end{enumerate}
%------------------------------------------------------------------

\subsection*{Problem 2: Matrices}

	\begin{enumerate}
		\item Suppose
		$A = \begin{bmatrix}
			1 & 2 & 3\\
			5 & 2 & 6\\
			-2 & -4 & 8
		\end{bmatrix}$
		\begin{enumerate}
			\item Check that $A^{3}=0$ where $0$ is a $3\times3$ matrix with entry equal to 0.
			\item Replace the third column of $A$ by the sum of the second and third columns.
		\end{enumerate}

		\item Create the following matrix $B$ with 15 rows:
		$B = \begin{bmatrix}
			20 & -20 & 20\\
			20 & -20 & 20\\
			\cdots & \cdots & \cdots \\
			20 & -20 & 20
		\end{bmatrix}$

	And than, Calculate the $3\times3$ matrix $B^{T}B$. Note: Look at the help for \texttt{crossprod}.

		\item Create a $6\times6$ matrix \texttt{matA} with every equal to 0. Check what the functions \texttt{row} and \texttt{col} return when applied to \texttt{matA}. Hence create the $6\times6$ matrix:
		$$\begin{bmatrix}
			0 & 1 & 0 & 0 & 0 & 0 \\
			1 & 0 & 1 & 0 & 0 & 0 \\
			0 & 1 & 0 & 1 & 0 & 0 \\
			0 & 0 & 1 & 0 & 1 & 0 \\
			0 & 0 & 0 & 1 & 0 & 1 \\
			0 & 0 & 0 & 0 & 1 & 0
		\end{bmatrix}$$

		\item Look at the help for the function \texttt{outer}. Hence create the following patterned matrix:
		$$\begin{pmatrix}
			0 & 1 & 2 & 3 & 4\\
			1 & 2 & 3 & 4 & 5\\
			2 & 3 & 4 & 5 & 6\\
			3 & 4 & 5 & 6 & 7\\
			4 & 5 & 6 & 7 & 8
		\end{pmatrix}$$

	\item Create the following patterned matrices. In each case, your solution should make use of the special form of the matrix --- this means that the solution should easily generalize to creating a larger matrix with the same structure and should not involve typing in all the entries in the matrix.
	\setlength\columnsep{0cm}

	\begin{multicols}{2}
		\begin{enumerate}
			\everymath{\displaystyle}
			\item $\begin{pmatrix}
				0 & 1 & 2 & 3 & 4 \\
				1 & 2 & 3 & 4 & 0 \\
				2 & 3 & 4 & 0 & 1 \\
				3 & 4 & 0 & 1 & 2 \\
				4 & 0 & 1 & 2 & 3
			\end{pmatrix}$

			\item $\begin{pmatrix}
				0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\
				1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 0\\
				\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
				8 & 9 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7\\
				9 & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8
			\end{pmatrix}$

			\item $\begin{pmatrix}
				0 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 \\
				1 & 0 & 8 & 7 & 6 & 5 & 4 & 3 & 2 \\
				2 & 1 & 0 & 8 & 7 & 6 & 5 & 4 & 3 \\
				3 & 2 & 1 & 0 & 8 & 7 & 6 & 5 & 4 \\
				4 & 3 & 2 & 1 & 0 & 8 & 7 & 6 & 5 \\
				5 & 4 & 3 & 2 & 1 & 0 & 8 & 7 & 6 \\
				6 & 5 & 4 & 3 & 2 & 1 & 0 & 8 & 7 \\
				7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 & 8 \\
				8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0
			\end{pmatrix}$
				\end{enumerate}
	\end{multicols}

	\item Solve the following system of linear equations in five unknowns
	$$x_{1} + 2x_{2} + 3x_{3} + 4x_{4} + 5x_{5} = 7$$
	$$2x_{1} + x_{2} + 2x_{3} + 3x_{4} + 4x_{5} = -1$$
	$$3x_{1} + 2x_{2} + x_{3} + 2x_{4} + 3x_{5} = -3$$
	$$4x_{1} + 3x_{2} + 2x_{3} + x_{4} + 2x_{5} = 5$$
	$$5x_{1} + 4x_{2} + 3x_{3} + 2x_{4} + x_{5} = 17$$

	by considering an appropriate matrix equation $AX = y$.
	Make use of the special form of the matrix $A$. The method used for the solution should easily generalize to a larger set of equations where the matrix $A$ has the same structure; hence the solution should not involve typing in every number of $A$.
	\end{enumerate}
%------------------------------------------------------------------

\subsection*{Problem 3}

\begin{enumerate}
	\item The following table gives the size of the floor area (ha) and the price (\$000), for 15 houses sold in the Canberra (Australia) suburb of Aranda in 1999.
	$$\begin{tabular}{cols}
		& area & sale.price \\
		1 & 694 & 192.0 \\
		2 & 905 & 215.0 \\
		3 & 802 & 215.0 \\
		4 & 1366 & 274.0 \\
		5 & 716 & 112.7 \\
		6 & 963 & 185.0 \\
		7 & 821 & 212.0 \\
		8 & 714 & 220.0 \\
		9 & 1018 & 276.0 \\
		10 & 887 & 260.0 \\
		11 & 790 & 221.5 \\
		12 & 696 & 255.0 \\
		13 & 771 & 260.0 \\
		14 & 1006 & 293.0 \\
		15 & 1191 & 375.0
	\end{tabular}$$
	Type these data into a data frame with column names \texttt{area} and \texttt{sale.price}.

	\begin{enumerate}
		\item Plot \texttt{sale.price} versus \texttt{area}.
		\item Use the \texttt{hist()} command to plot a histogram of the sale prices.
		\item Repeat (a) and (b) after taking logarithms of sale prices.
	\end{enumerate}

	\item The \texttt{orings} data frame (DAAG package) gives data on the damage that had occurred in US space shuttle launches prior to the disastrous Challenger launch of 28 January 1986. The observations in rows 1, 2, 4,
	11, 13, and 18 were included in the pre-launch charts used in deciding whether to proceed with the launch, while remaining rows were omitted.
	Create a new data frame by extracting these rows from \texttt{orings}, and plot \texttt{Total} incidents against \texttt{Temperature} for this new data frame. Obtain a similar plot for the full data set.

	\item For the data frame \texttt{possum} (DAAG package)
	\begin{enumerate}
		\item Use the function \texttt{str()} to get information on each of the columns.
		\item Using the function \texttt{complete.cases()}, determine the rows in which one or more values is missing. Print those rows. In which columns do the missing values appear?
	\end{enumerate}

	 \item For the data frame \texttt{ais} (DAAG package)
	 \begin{enumerate}
	 	\item Use the function \texttt{str()} to get information on each of the columns. Determine whether any of the columns hold missing values.
	 	\item Make a table that shows the numbers of males and females for each different sport. In which sports is there a large imbalance (e.g., by a factor of more than 2:1) in the numbers of the two sexes?
	 \end{enumerate}

	 \item Create a table that gives, for each species represented in the data frame \texttt{rainforest} (DAAG package), the number of values of branch that are NAs, and the total number of cases.
	 [Hint: Use either \texttt{!is.na()} or \texttt{complete.cases()} to identify NAs.]

\end{enumerate}

%------------------------------------------------------------------

%------------------------------------------------------------------

\subsection*{Problem 4: Data transformations}
\textbf{Filter rows with \texttt{filter()}}

\begin{enumerate}
	\item Use \texttt{flights} data frame in the \texttt{nycflights13} library and use \texttt{tidyverse} package. Find all flights that:

	\begin{enumerate}
		\item Had an arrival delay of two or more hours. To do so, you should find the variable that denote as an arrival delay.
		\item Show the flights (\texttt{dest} variable) that flew to Houston where the destination is either \texttt{``IAH''} or \texttt{``HOU''}.
		\item Were operated by United, American, or Delta.
		\item Departed in summer (July, August, and September).
		\item Arrived more than two hours late, but didn't leave late.
		\item Were delayed by at least an hour, but made up over 30 minutes in flight.
		\item Departed between midnight and 6am (inclusive).
	\end{enumerate}

	\item In the \texttt{month} variable of the \texttt{flights} data frame, please show departed in summer (month $>= 7$ \& month $<= 10$) using the \texttt{between()} function.

	\item How many flights have a missing \texttt{dep\_time} of the \texttt{flights} data frame? What other variables are missing? What might these rows represent?

	\item Why is \texttt{NA\textasciicircum 0} not missing? Why is \texttt{NA | TRUE} not missing? Why is \texttt{FALSE \& NA} not missing? Can you figure out the general rule? (\texttt{NA * 0} is a tricky counterexample!)

\end{enumerate}
%------------------------------------------------------------------
\noindent \textbf{Arrange rows with \texttt{arrange()}}

\begin{enumerate}
	\item Continuing the \texttt{flights} data frame, how could you use \texttt{arrange()} to sort all missing values to the start? (Hint: use \texttt{is.na()}).

	\item Sort \texttt{flights} to find the most delayed flights. Find the flights that left earliest.

	\item Sort \texttt{flights} to find the fastest (the highest speed) flights.

	\item Which flights traveled the farthest? Which traveled the shortest?

\end{enumerate}
%------------------------------------------------------------------

\noindent \textbf{Select columns with \texttt{select()}}

\begin{enumerate}
	\item Please select \texttt{dep\_time}, \texttt{dep\_delay}, \texttt{arr\_time}, and \texttt{arr\_delay} from \texttt{flights} data frame.

	\item What happens if you include the name of a variable multiple times in a \texttt{select()} call?

	\item What does the \texttt{one\_of()} function do? Why might it be helpful in conjunction with this vector?

	\texttt{vars <- c(``year'', ``month'', ``day'', ``dep\_delay'', ``arr\_delay'')}

	\item Does the result of running the following code surprise you? How do the select helpers deal with case by default? How can you change that default?

	\texttt{select(flights, contains(``TIME''))}

\end{enumerate}
%------------------------------------------------------------------

\noindent \textbf{Add new variables with \texttt{mutate()}}

\begin{enumerate}
	\item Currently \texttt{dep\_time} and \texttt{sched\_dep\_time} are convenient to look at, but hard to compute with because they're not really continuous numbers. Convert them to a more convenient representation of number of minutes since midnight.

	\item Compare \texttt{air\_time} with \texttt{arr\_time - dep\_time}. What do you expect to see? What do you see? What do you need to do to fix it?

	\item Compare \texttt{dep\_time}, \texttt{sched\_dep\_time}, and \texttt{dep\_delay}. How would you expect those three numbers to be related?

	\item Find the 10 most delayed flights using a ranking function. How do you want to handle ties? Carefully read the documentation for \texttt{min\_rank()}.

	\item What does \texttt{1:3 + 1:10} return? Why?

	\item What trigonometric functions does R provide?

\end{enumerate}
%------------------------------------------------------------------

\noindent \textbf{Grouped summaries with \texttt{summarise()}}

\begin{enumerate}
	\item Come up with another approach that will give you the same output as \texttt{not\_cancelled \%>\% count(dest)} and \texttt{not\_cancelled \%>\% count(tailnum, wt = distance)} (without using \texttt{count()}).

	\item Our definition of canceled flights (\texttt{is.na(dep\_delay) | is.na(arr\_delay)}) is slightly sub-optimal. Why? Which is the most important column?

	\item Look at the number of canceled flights per day. Is there a pattern? Is the proportion of canceled flights related to the average delay?

	\item Which carrier has the worst delays? Challenge: can you disentangle the effects of bad airports vs. bad carriers? Why/why not? (Hint: think about flights \texttt{\%>\% group\_by(carrier, dest) \%>\% summarise(n())})

	\item What does the sort argument to \texttt{count()} do? When might you use it?

\end{enumerate}
%------------------------------------------------------------------

\noindent \textbf{Grouped mutates (and filters)}

\begin{enumerate}
	\item Refer back to the lists of useful mutate and filtering functions. Describe how each operation changes when you combine it with grouping.

	\item What time of day should you fly if you want to avoid delays as much as possible?

	\item For each destination, compute the total minutes of delay. For each flight, compute the proportion of the total delay for its destination.

	\item Delays are typically temporally correlated: even once the problem that caused the initial delay has been resolved, later flights are delayed to allow earlier flights to leave. Using \texttt{lag()} explore how the delay of a flight is related to the delay of the immediately preceding flight.

	\item Look at each destination. Can you find flights that are suspiciously fast? (i.e. flights that represent a potential data entry error). Compute the air time of a flight relative to the shortest flight to that destination. Which flights were most delayed in the air?

	\item Find all destinations that are flown by at least two carriers. Use that information to rank the carriers.

	\item For each plane, count the number of flights before the first delay of greater than 1 hour.

\end{enumerate}
%------------------------------------------------------------------
\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{bbold}
\usepackage{graphicx}

\title{Modularity and random walks}
\author{Tom Burns @tfburns}
\date{20 May 2020}

\begin{document}

\maketitle

In response to Thomas Varley @ThosVarley, who tweeted on 20 May 2020: ``Has any mathematical work been done relating the modularity of a network to the distribution of the number of times a random walker will visit every node?  My intuition says that if the distribution is narrow, the network shouldn't be modular. I'm not sure, though."

These (informal) notes show why this intuition is false.

\paragraph{Fact 1: Complete graphs have a one-point probability distribution of vertex visitations in a random walk.}
Let $K_n$ be a complete graph, where $n \in \mathbb{N}$ is the number of vertices. Let us say that a random walk starts on an arbitrary vertex and may `visit' another vertex in the graph iff those vertices share an edge, and let all possible visits be equally likely. In a random walk on $K_n$, at a timestep $t$ in the random walk the probability of being located at a vertex $k$ at timestep $t+1$ is $1/n$, since in the complete graph all vertices are connected and in a random walk all edge transitions are equally likely. The distribution of vertex visitation probabilities is therefore a one-point distribution and infinitesimally narrow. It is a stationary stochastic process.

\paragraph{Corollary 1: A vertex's visitation probability in a random walk on a graph is proportional to its degree.}

\paragraph{Fact 2: It is possible to construct an infinitely modular graph with a two-point probability distribution of vertex visitations in a random walk.}

Let $K_n$ be a complete graph, where $n \in \mathbb{N}$ is the number of vertices. By fact 1, at a timestep $t$ in the random walk on $K_n$ the probability of being located at a vertex $k$ at timestep $t+1$ is $1/n$. Let $G$ be a graph which we construct by connecting two complete graphs of the same size with a single edge between an arbitrary vertex in each graph. This will maintain the degree of all but one of the original vertices in each of the complete sub-graphs, whereas the degree of the vertices which connect the complete sub-graphs increase their degree by 1. By induction, we may continue connecting additional complete graphs of the same size while guaranteeing all vertices in $G$ have a degree of $n-1$ (if they do not connect two complete sub-graphs) or $n$ (if they connect two complete sub-graphs). By corollary 1, we have a two-point probability distribution of vertex visitations in a random walk.

Let a metric for graph modularity be the number of connected components, where we define a component to be any subgraph which can become disconnected by cutting a single edge. Let us say a graph has $N$-components if it has $N-1$ such edges. By the method above, we may construct $G$ to have as many connected components as we wish, and thus be infinitely modular while maintaining a two-point probability distribution of vertex visitations in a random walk. Note: Although these arguments hold for $n=1$ and $n=2$, it is more natural to see this in the case of $n\geq3$. The figure below shows the example of $n=5$ and N=3.

\begin{center}
    \includegraphics[width=6.5cm]{K5_K5_K5.png}
    \\
    \includegraphics[width=6.5cm]{K5_K5_K5_dist.png}
    \\
    Simulated in Python. Code available at: https://github.com/tfburns/Graph-Gang/tree/master/notes/modularity-and-random-walks
\end{center}

\end{document}


\documentclass{scrartcl}
\usepackage[a4paper,left=1in,right=1in,top=1.2in,bottom=1in]{geometry}
\usepackage{siunitx}
\usepackage{graphicx}
\setkomafont{disposition}{\normalfont\bfseries}
\title{Exercise 01:\\Reversal and Resting Potential}
\subtitle{Theoretical Neuroscience I}
\author{Maria del Cerro \and Johannes G\"atjen \and Lorena Morton}
\usepackage{amsmath}
\newcommand\Answer{%
  \textbf{\\Answer:}%
}
\newcommand\Question{%
  \textbf{Question:}%
}

\begin{document}
\maketitle

\section{Nernst Equation}
\subsection{Reversal potentials}

\Question\\
Compute the reversal potential for each ion species, assuming the following ionic concentrations:\\
\\
\begin{tabular}{c | c c}
Ion & $[\cdot]_{out}$ & $[\cdot]_{in}$ \\ \hline
$\text{K}^+$ & 5 mM & 140 mM \\
$\text{Na}^+$ & 145 mM & 15 mM \\
$\text{Cl}^-$ & 110 mM & 13 mM
\end{tabular}\\
Can the membrane potential equal the reversal potential of more than one of these ions?

\Answer\\
We use the Nernst equation to calculate the reversal potential:
\begin{equation*}
E_{\text{rev}}=\frac{RT}{zF}\ln \frac{[X]_{\text{out}}}{[X]_{\text{in}}} \approx \frac{1}{z} \cdot 26.70 \si{mV} \cdot \ln \frac{[X]_{\text{out}}}{[X]_{\text{in}}}
\end{equation*}

The $z$ values for potassium, sodium and chloride are 1, 1 and -1 respectively. Inserting the given values in the Nernst equation we obtain:
\begin{equation*}
\begin{split}
E_{\text{K}^+}=\frac{1}{1} \cdot 26.70 \si{mV} \cdot \ln \frac{5 \si{mM}}{140 \si{mM}} \approx 26.70 \si{mV} \cdot -3.332 \approx -88.97 \si{mV}\\
E_{\text{Na}^+}=\frac{1}{1} \cdot 26.70 \si{mV} \cdot \ln \frac{145 \si{mM}}{15 \si{mM}} \approx 26.70 \si{mV} \cdot 2.269 \approx 60.57 \si{mV}\\
E_{\text{Cl}^-}=\frac{1}{-1} \cdot 26.70 \si{mV} \cdot \ln \frac{110 \si{mM}}{13 \si{mM}} \approx {-26.70} \si{mV} \cdot 2.136 \approx -57.02 \si{mV}
\end{split}
\end{equation*}
The membrane potential can only equal the reversal potential of more than one ion at a time, if either two ions of the same charge have the same concentration ratio or if two ions of opposite charge have inverse concentration ratios. At different times, if the membrane potential changes over time, it can be equal to the reversal potential of different ions.
\pagebreak
\subsection{Driving forces}

\Question\\
For the ions listed above, sketch the driving force $\Delta \si{V}$ (in units of \si{mV}) as a function of the membrane potential $\si{V}_{\text{mem}}$ in the range of $-100 \si{mV} \leq \si{V}_{\text{mem}} \leq +100\si{mV}$!

\Answer\\
\begin{figure}[h]
\centering
\includegraphics[scale=0.7]{../pics/drivingforce}
\caption{The driving force $\Delta \si{V}$ as a function of the membrane potential $\si{V}_{\text{mem}}$ for the potassium, sodium and chloride ions.}
\end{figure}

\section{Goldman equation}
\Question
\begin{enumerate}
\item
Assuming the concentrations you used for solving the Nernst equation, calculate the resting potential \textbf{for equal permeabilities} ($b=c=1$)!
\item
Calculate the resting potential for $b = 0.03$ and $c = 0.1$!
\end{enumerate}

\Answer\\
We insert the given values into the Goldman equation:
\begin{equation*}
E_{\text{rest}} = 27 \si{mV} \cdot \ln \frac{[\text{K}^+]_{\text{out}} + b [\text{Na}^+]_{\text{out}} + c [\text{Cl}^-]_{\text{in}}}{[\text{K}^+]_{\text{in}} + b [\text{Na}^+]_{\text{in}} + c [\text{Cl}^-]_{\text{out}}}
\end{equation*}

\begin{enumerate}
\item
\begin{equation*}
E_{\text{rest}} = 27 \si{mV} \cdot \ln \frac{5 \si{mM} + 145 \si{mM} + 13 \si{mM}}{140 \si{mM} + 15 \si{mM} + 110 \si{mM}}  \approx -13.12 \si{mV}
\end{equation*}
\item
\begin{equation*}
E_{\text{rest}} = 27 \si{mV} \cdot \ln \frac{5 \si{mM} + 0.03 \cdot 145 \si{mM} + 0.1 \cdot 13 \si{mM}}{140 \si{mM} + 0.03 \cdot 15 \si{mM} + 0.1 \cdot 110 \si{mM}} \approx -71.68 \si{mV}
\end{equation*}

\end{enumerate}

\end{document}


\section{\Luscher's Formulae}\label{sec:luescher}

In subsequent sections we will extract scattering data from numerical calculations for particular box sizes and discretizations.
We will show that when tuned and analyzed using the traditional \Luscher method, we induce a momentum-dependent scattering amplitude at any finite lattice spacing and explain how to achieve a momentum-independent scattering amplitude, even at finite lattice spacing, by constructing a lattice-aware \Luscher-like method.

For concreteness of our discussion we here provide a derivation of \Luscher's S-wave formula roughly following \Ref{Beane:2003da}, although the technology and sophistication of the finite-volume formalism has grown substantially \cite{Beane:2010ny,Ozaki:2012ce,Hansen:2012tf,Briceno:2013hya,Briceno:2013lba,Li:2014wga,Zhu:2019dho}. What differentiates our derivation from others is our ensuing lattice spacing-corrected procedure.

\input{section/luescher/continuum}
\input{section/luescher/dispersion}


\section{Bootstrap Standard Errors and Confidence Sets }
Statistical science is the science of learning from experience.
Efron and Tibshirani (1993) say ``Most people are not natural-born
statisticians. Left to our own devices we are not very good at picking
out patterns from a sea of noisy data. To put it another way, we are
all too good at picking out non existing patterns that happen to suit
our purposes.''

%%%from efron and tibshi
Suppose we find ourselves in the following common data-analytic
situation: a random sample $\bx = (x_1,\dots,x_n)$ from an unknown
probability distribution $F$ has been observed and we wish to estimate
a parameter of interest $\theta = t(F)$ on the basis of $\bx$. For
this purpose, we calculate an estimate $\hat{\theta} = s(\bx)$ from
$\bx$.

A common estimate is the {\it plug-in} estimate $t(\hat{F})$
where $\hat{F}$ is the empirical distribution defined by
\[
F(x) = \frac{\mbox{ number of values in } \bx \mbox{ equal
    to }x}{n}
\]
Can you think of a plug-in estimate that is commonly used?

The bootstrap was introduced by Efron (1979) as a computer based
method to estimate the standard deviation of $\hat{\theta}$.

What are the advantages:
\begin{itemize}
\item It is completely automatic
\item Requires no theoretical calculations
\item Not based on asymptotic results
\item Available no matter how complicated the estimator $\hat{\theta}$
  is.
\end{itemize}

A bootstrap sample is defined to be a random sample of size $n$ drawn
from $\hat{F}$, say $\bx^* = (x_1^*,\dots,x_n^*)$.

For each bootstrap sample $\bx^*$ there is a bootstrap replicate of
$\hat{\theta}$,
\[
\hat{\theta}^* = s(\bx^*).
\]
The bootstrap estimate of $\se_F(\hat{\theta})$ is defined by
\begin{equation}
\label{ibe}
\se_{\hat{F}}(\hat{\theta}^*).
\end{equation}
This is called the {\it ideal bootstrap estimate} of the standard
error of $s(\bx)$.

Notice that for the case where $\theta$ is the
expected value or mean of $\bx_1$ we have
\[
\se_{\hat{F}}(\bar{x}^*) = \se_{\hat{F}}(x^*_1)/\sqrt{n} =
\sqrt{n^{-1} \sum_{i=1}^n (x_i - \hat{x})^2}/\sqrt{n}
\]
and the ideal bootstrap estimate is the estimate we are used
to. However, for any other estimator other than the mean obtaining
(\ref{ibe}) there is no neat formula that enables us to compute a
numerical value in practice.

The bootstrap algorithm is a computational way of obtaining a good
approximation to the numerical value of (\ref{ibe}).

\subsection{The bootstrap algorithm}
The bootstrap algorithm works by drawing many independent bootstrap
samples, evaluating the corresponding bootstrap replications, and
estimating the standard error of $\hat{\theta}$ by the empirical standard
error, denoted by $\hat{\se}_B$, where $B$ is the number of bootstrap
samples used.

\begin{enumerate}
\item Select $B$ independent bootstrap samples
  $\bx_1^*,\dots,\bx^*_B$, each consisting of $n$ data values drawing
  with replacement from $\bx$.
\item Evaluate the bootstrap replication corresponding to each
  bootstrap sample
\[
\hat{\theta}^*(b) = s(\bx_b^*), b=1,\dots,B
\]
\item Estimate the standard error $\se_{F}(\hat{\theta})$ by the
  sample standard error of the $B$ replicates
\[
\hat{\se}_B = \left[ \frac{1}{B-1}\sum_{b=1}^B \{ \hat{\theta}^*(b) -
  \hat{\theta}^*(\cdot) \}^2 \right]
\]
with
\[
\hat{\theta}^*(\cdot) = B^{-1} \sum_{b=1}^B  \hat{\theta}^*(b)
\]
\end{enumerate}
The limit of $\hat{\se}_B$ as $B$ goes to infinity is the ideal
bootstrap estimate of (\ref{ibe}). But how close is (\ref{ibe}) to
$\se_F(\hat{\theta})$? See Efron and Tibshirani (1993) for more details.

\subsection{Example: Curve fitting}
In this example we will be estimating regression functions in two
ways, by a standard least-squares line and by loess.
\begin{figure}
\begin{center}
\epsfig{figure=Plots/plot-05-06.ps,angle=270,width=.8\textwidth}
\end{center}
\caption{\label{f5.3.1} Estimated regression curves of Improvement on
  Compliance.}
\end{figure}

A total of 164 mean took part in an experiment to see if the drug
cholostyramine lowered blood cholesterol levels. The men were supposed
to take six packets of cholostyramine per day, but many of them
actually took much less. Figure \ref{f5.3.1} shows compliance plotted
against percentage of the intended dose actually taken. We also show a
fitted line and a loess fit (using span=2/3). Notice the curves
similar from 0 to 60, a little different from 60 to 80 and quite
different from 80 to 100.

Assume the points a regression model
\[
y_i = f(x_i) + \varepsilon_i, i=1,\dots,n
\]
with the $\varepsilon_i$ IID.

Say we are interested in the difference in rate of change of $f(x)$ in
the 60--80 and 80--100
sections. We could define as the parameter to describe this.
How can we do this?

Notice that finding a standard error for this estimate is not
straight-forward. We can use the bootstrap.

\begin{table}[htb]
\caption{Estimates and bootstrap standard errors of $f(60), f(80),$
  and $f(100)$.}
\begin{tabular}{lcccccc}
 &$\hat{f}_{\mbox{line}}(60)$&$\hat{f}_{\mbox{line}}(80)$&$
 \hat{f}_{\mbox{line}}(100)$&$\hat{f}_{\mbox{loess}}(60)$&$
   \hat{f}_{\mbox{loess}}(80)$&$\hat{f}_{\mbox{loess}}(100)$\\
\hline
value:&33&44&56&28&35&66\\
$\hat{\se}_{50}$:&2&2&3&5&4&4
\end{tabular}
\end{table}

As seen in Figure \ref{f5.3.2}. Even when there is no parameter of interest, the bootstrap estimates
of $f$ give us an idea of what a confidence set is for the
nonparametric estimates. We will see more of this in Chapter 7 and 8.

\begin{figure}
\begin{center}
\epsfig{figure=Plots/plot-05-07.ps,angle=270,width=.8\textwidth}
\end{center}
\caption{\label{f5.3.2} 50 boostrap curves for each estimation techinique.}
\end{figure}

\subsection{Confidence ``intervals'' for linear smoothers}
It is easy to show that the variance-covariance matrix of the vector
of fitted values $\hat{\f} = \bS \by$ is
\[
\cov(\hat{\f}) = \bS \bS' \sigma^2
\]
and given an estimate of $\sigma^2$ this can be used to give
point-wise standard errors, mainly by looking at
$\mbox{diag}(\bS\bS')\sigma^2$.

Can we construct confidence intervals? What do we need?

First of all we need to know the distribution (at least approximately)
of $\hat{\f}$. If the errors are normal we know that $\bf{\f}$ is normally
distributed. Why?

In the normal case, what are the confidence intervals for?

Remember that our estimates are usually biased, $\E(\hat{\f}) = \bS \f
\neq \f$. If our null hypothesis is $\bS \f = \f$ (in the case of splines
this is equivalent to assuming $f \in \cal G$) then our confidence
intervals are for $\f$ otherwise it is much more convinient to compute
them for $\bS \f$. We will start
using the notation $\tilde{\f} = \bS \f$. We can think of $\tilde{\f}$
as the best possilble approximation to ``the truth'' $\f$ when using
the $\bS$ as a smoother.

To see how point-wise estimates can be useful, notice that we can get
an idea of how variable $\hat{\f}(x_0)$ is. However, it isn't very
helpful when we want to see how variable $\hat{\f}$ is as a whole.

What if we want to know if a certain function, say a line, is
in our ``confidence interval''? Point-wise intervals don't really help
us with this.

\subsection{Global confidence bands}
Remember that $\hat{\f} \in {\mathbb R}^n$. This means that
talking about confidence intervals doesn't make much sense. We need to
consider confidence sets.

For example if the errors are normal we know that
\[
\chi(\tilde{\f}) = (\hat{\f} - \tilde{\f})'(\bS \bS'
\sigma^2)^{-1}(\hat{\f} - \tilde{\f})
\]
is $\chi^2_n$ distributed. This permits us to construct confidence
sets (which you can think of as random $n$-dimensional balls)
for $\tilde{\f}$ of probability $\alpha$
\[
C_{\alpha} = \{ \g \in {\mathbb R}^b ; \chi(\g) \leq \chi_{1-\alpha}
\}=\{ \g  \in {\mathbb R}^b; (\hat{\f}
- \g)'(\bS \bS' \sigma^2)^{-1}(\hat{\f} - \g) \leq \chi_{1-\alpha} \}.
\]
Notice that the probability that the random ball doesn't fall on the
approximate truth $\tilde{\f}$ is $\alpha$:
\[
\Pr(\tilde{\f} \not\in C_{\alpha}) = \Pr\left[(\hat{\f} - \tilde{\f})'(\bS \bS'
\sigma^2)^{-1}(\hat{\f} - \tilde{\f}) > \chi_{1-\alpha}\right]= \alpha.
\]
This is only the case if we know $\sigma^2$.

Usually we construct an estimate
\[
\hat{\sigma}^2 = (\by - \hat{\f})' (\by - \hat{\f})/ \{n - \tr(2\bS -
\bS\bS')\}
\]
and define confidence sets
\[
C(\tilde{\f}) = \{ \g  \in {\mathbb R}^b; \nu(\g) \leq G_{1-\alpha} \}
\]
based on
\[
\nu(\tilde{\f}) =  (\hat{\f} - \tilde{\f})'(\bS \bS'
\hat{\sigma}^2)^{-1}(\hat{\f} - \tilde{\f}).
\]
Here $G_{1-\alpha}$ is the $(1-\alpha)$th quantile of the distribution
of $\nu(\tilde{\f})$.

Do we know $G$? Not necessarily.

In the case of linear regression, where the gaussian model is correct
and $\bS$ is a
p-dimensional projection, $\nu(\tilde{\f})=\nu(\f)$ has distribution
$(n-p) + p F_{p,n-p}$.

When this is not the case we can argue that the
distribution is approximately
\[
\{n-\tr(2\bS - \bS\bS')\} + \tr(\bS\bS') F_{\tr(\bS\bS'),n-\tr(2\bS -
  \bS\bS')}
\]

If we are not sure of the normality assumption or that $\tilde{\f}
\approx \f$ we can use the bootstrap to construct an approximate
distribution $\hat{G}$ of $G$.

How do we do it?

\begin{figure}[htb]
\caption{The regression curve and an outcome with $n=100$ and
  $\sigma^2=1$.}
\begin{center}
\epsfig{figure=Plots/plot-05-08.ps,angle=270,width=.8\textwidth}
\end{center}
\end{figure}

\subsection{Bootstrap estimate of $G_{1-\alpha}$}
A bootstrap sample is generated in the following way
\begin{itemize}
\item For some data $\by$ use some procedure (a linear smoother for
  example) to obtain an estimate $\hat{\f}$ of some estimand (in this
  case the regression function $\f$).
\item Obtain residuals $\hat{\bg{\varepsilon}} = \by - \hat{\f}$.
\item Take a simple random sample of size $B$ from the residuals
  $\hat{\varepsilon}_1,\dots,\hat{\varepsilon}_n$. Notice that this makes
  them IID just like the $\varepsilon$s.
\item Construct a ``new'' data set
\[\by^* = \hat{\f} +  \hat{\bg{\varepsilon}}^*
\]
with $\hat{\bg{\varepsilon}}^*$ the vector of resampled residuals.
\item From the new data form a new estimate $\hat{\f}^*$.
\item Finally we obtain the value of
\[
\nu^* = (\hat{\f}^* - \hat{\f})'(\bS \bS'
\hat{\sigma}^{*2})^{-1}(\hat{\f}^* - \hat{\f})
\]
\item We repeat this procedure many times and form an approximate
  distribution $\hat{G}$ with the values of $\nu^*$. We may use the $(1-\alpha)$th
  quantile of $\hat{G}$ as an estimate of $G_{1-\alpha}$.
\end{itemize}

Let's consider the model $y_i = f(x_i) + \varepsilon_i, i=1,\dots,n$
with $\varepsilon_i$ IID normal.  In Figure \ref{f5.8} we see
qqplots of the true $G$, the bootstrap $G$ and the F-distribution
approximation.

\subsection{Displaying the confidence sets}
Displaying an $n-dimensional$ ball is not easy.

Global confidence
bands usually show the projections of the confidence set onto each of
the component sub-spaces. Notice that a
function (now I'm using function and $n$-dimensional vector
interchangeably) in this set would actually be in a confidence cube as opposed
to a ball! So a vector within the confidence bands isn't necessarily in the confidence
ball. However its true that being in the ball implies being within the
band.

Another popular approach is selecting a few functions at random from
$N(\hat{\f},\bS\bS'\hat{\sigma}^2)$ and checking to see if they are in
the confidence set. If they are, we plot them. This enables us to see
what kind of ``shape'' functions in the confidence set have. Maybe
they all have a bump, maybe a large amount of them are close to being
constant lines, etc...

\begin{figure}[htb]
\caption{\label{f5.8} QQ-plot of bootstrap vs. true $G$ and the
  F-distribution approximation. We also see point-wise confidence
  intervals and curves in (blue) and out (green) of the bootstrap
  confidence set.}
\begin{center}
\epsfig{figure=Plots/plot-05-09.ps,angle=270,width=.8\textwidth}
\end{center}
\end{figure}

\subsection{Approximate F-test}
Using the F-distribution approximations we may construct F-tests for
testing various hypotheses.

The p-value given by the S-Plus function {\tt gam()} is usually
testing for linearity and using an F-distribution approximation.

Suppose we wish to compare 2 smoothers $\hat{\f}_1 = \bS_1 \by$ and
$\hat{\f}_2 = \bS_2 \by$. For example, $\hat{\f}_1$ may be linear
regression and $\hat{\f}_2$ may be a ``rougher'' smoother.

Let $RSS_1$ and $RSS_2$ be the residual sum of squares obtained for
each smoother. Which one do you expect to be bigger?

and $\gamma_1$ and $\gamma_2$ be the degrees of freedom of each
smoother, $\tr(2\bS_j - \bS_j\bS_j'), j=1,2$. An approximation that
may be useful for this comparison is
\[
\frac{(RSS_1 - RSS_2)/(\gamma_2 - \gamma_1)}{RSS2/(n-\gamma_2)} \sim
F_{\gamma_2-\gamma_1,n-\gamma_2}
\]

There are moment corrections that can make this a better
approximation (see H\&T).
\begin{figure}[htb]
\caption{Same as previos figure but with t-distributed errors}
\begin{center}
\epsfig{figure=Plots/plot-05-10.ps,angle=270,width=.8\textwidth}
\end{center}
\end{figure}