\documentclass[10pt]{article}

\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{booktabs}
\usepackage{mathrsfs}
\usepackage{graphicx}
\usepackage{cite}
\usepackage{times}
\usepackage{url}
\usepackage{hyperref}
\usepackage{lineno}
\usepackage{yhmath}
\usepackage{natbib}
\usepackage{../../definitions}
\hypersetup{
  bookmarksnumbered = true,
  bookmarksopen=false,
  pdfborder=0 0 0,         % make all links invisible, so the pdf looks good when printed
  pdffitwindow=true,      % window fit to page when opened
  pdfnewwindow=true, % links in new window
  colorlinks=true,           % false: boxed links; true: colored links
  linkcolor=blue,            % color of internal links
  citecolor=magenta,    % color of links to bibliography
  filecolor=magenta,     % color of file links
  urlcolor=cyan              % color of external links
}

\newcommand{\ee}[1]{{\color{blue} EE:~#1}}
\newcommand{\Lo}{\textsc{L}}
\newcommand{\Hi}{\textsc{H}}
\newcommand{\trans}{\textsc{T}}
\newcommand{\dx}{\Delta x}

\newtheorem{define}{Definition}
\newtheorem{lemma}{Lemma}
\newtheorem{prop}{Proposition}
\newtheorem{rem}{Remark}
\newtheorem{theorem}{Theorem}

\begin{document}

\title{Subcell Projection and Reconstruction}
\author{Eirik Endeve et al.}

\maketitle

\begin{abstract}
  We consider subcell projection and reconstruction to move between discontinuous Galerkin (DG) and finite volume (FV) representations.
  When the FV variables are represented on a subgrid with the same number of degrees of freedom as the DG representation, we can move between representations without loss of information.
  We basically follow Dumbser et al. (2014, {\it JCP}, {\bf 278}, 47).
\end{abstract}

\tableofcontents

\section{Preliminaries}

Consider an element $\vect{K}\subset\bbR^{d}$ where the DG representation is denoted $u_{h}(\vect{x},t)\in\bbV^{k}$, where $\bbV^{k}$ is constructed from the tensor product of one-dimensional polynomials of maximal degree $k$
\begin{equation}
  u_{h}(\vect{x},t)=\sum_{j=1}^{(k+1)^{d}}u_{j}(t)\,\Phi_{j}(\vect{x}).
  \label{eq:dgRepresentation}
\end{equation}
Next, consider dividing the element $\vect{K}$ into a subgrid $S$ of $(k+1)^{d}$ nonoverlapping FV cells $S_{j}$, $S=\cup_{j=1}^{(k+1)^{d}} S_{j}$, so that $K\setminus S=\emptyset$; see Figure~\ref{fig:ElementSubgrid} for an example in two spatial dimensions ($d=2$) and $k=1$.
On the subgrid, the representation is given by piecewise constants (cell averages)
\begin{equation}
  v_{h}(\vect{x},t)=\sum_{j=1}^{(k+1)^{d}}\chi(S_{j})\,v_{j}(t),
  \label{eq:piecewiseConstants}
\end{equation}
where $v_{j}(t)$ is the cell average in subgrid cell $S_{j}$ and the $\chi(S_{j})$ is the indicator function on $S_{j}$.

\begin{figure}
  \centering
  \includegraphics[width=0.75\textwidth]{./ElementSubgrid}
   \caption{Division of an element $\vect{K}$ into a subgrid of finite volume cells in two spatial dimensions ($d=2$) using DG polynomials of maximal degree $k=1$.}
  \label{fig:ElementSubgrid}
\end{figure}

\section{FV to DG Representation (Reconstruction)}

Knowing the cell averages $v_{j}$ (we suppress time dependence from here on), we reconstruct the DG representation $u_{h}$ by requiring that
\begin{equation}
  \f{1}{|S_{i}|}\int_{S_{i}}u_{h}(\vect{x})\,d\vect{x}
  =\f{1}{|S_{i}|}\int_{S_{i}}v_{h}(\vect{x})\,d\vect{x}=v_{i}, \forall~S_{i}\in S.
\end{equation}
With the definition in Eq.~\eqref{eq:dgRepresentation}, this gives
\begin{equation}
  R^{-1}\,\vect{u} = \vect{v},
\end{equation}
where $\vect{u}=(u_{1},\ldots,u_{(k+1)^{d}})^{T}$, $\vect{v}=(v_{1},\ldots,v_{(k+1)^{d}})^{T}$, and the components of the $(k+1)^{d}\times(k+1)^{d}$ inverse reconstruction matrix are
\begin{equation}
  R_{ij}^{-1}=\f{1}{|S_{i}|}\int_{S_{i}}\Phi_{j}(\vect{x})\,d\vect{x}.
\end{equation}
The reconstruction step amounts inverting to find
\begin{equation}
  \vect{u} = R\,\vect{v}.
\end{equation}

\section{DG to FV Representation (Projection)}

Knowing the DG representation $u_{h}(\vect{x})$, we can easily compute the cell averages for the finite volume representation
\begin{equation}
  v_{i} = \f{1}{|S_{i}|}\int_{S_{i}}\,u_{h}(\vect{x})\,d\vect{x} = \f{1}{|S_{i}|}\sum_{j=1}^{(k+1)^{d}}\int_{S_{i}}\Phi_{j}(\vect{x})\,d\vect{x}\,u_{j},
\end{equation}
which can be written as
\begin{equation}
  \vect{v} = P\,\vect{u},
\end{equation}
where the components of the $(k+1)^{d}\times(k+1)^{d}$ projection matrix $P$ are
\begin{equation}
  P_{ij} = \f{1}{|S_{i}|}\int_{S_{i}}\Phi_{j}(\vect{x})\,d\vect{x}.
\end{equation}
From the definitions above, it is obvious that $R\,P = I$; i.e., we do not lose any accuracy when switching between DG and FV representations.
The reconstruction and projection matrices are plotted in Figure~\ref{fig:matrices} for $d=2$, $k=1$.
\begin{figure}
  \centering
  \begin{tabular}{cc}
    \includegraphics[width=0.5\textwidth]{./ReconstructionMatrix} &
    \includegraphics[width=0.5\textwidth]{./ProjectionMatrix}
  \end{tabular}
   \caption{Reconstruction matrix $R$ (left) and projection matrix $P$ for two spatial dimensions ($d=2$) using DG polynomials of maximal degree $k=1$.}
  \label{fig:matrices}
\end{figure}

\end{document}


\subsection{TF-IDF}

% Ranked user retrieval can be implemented by only using the aforementioned
% \emph{PageRank} algorithm but by doing so, any query would return the same top
% listed users. While that might be interesting in some applications, that is not
% the case in our context. The words in the query should also be used to filter
% and rank the retrieved users.

In order to also consider terms in the search algorithm, \emph{TF-IDF} was used,
which is a well known solution to the problem of matching (in a ranked
way) documents modelled as \emph{bags-of-words}. Each document (including the
input query) is represented by a vector of scores, each of which related to one
of the possible terms in our dataset. The scores are calculated as follows:
${tf}_{w, d} * log_{10}(\frac{N}{{df}_{w}})$ where ${tf}_{w, d}$ is the number
of times term $w$ appears in document $d$, $N$ is the total number of documents
and ${df}_{w}$ is the number of documents term $w$ appears in. Then,
\emph{cosine-similarity} is used to compute how close the query is to each of
the documents. A link between a \emph{User} node and a \emph{Word} node maps
directly to a ${tf}_{w,d}$ score. The final procedure can be seen in Algorithm \ref{alg:tfidf}.

% In our implementation, \emph{User} nodes are documents containing each of the
% \emph{Word} nodes they are linked to. This link contains the number of times
% this \emph{Word} has been discussed by this \emph{User}, that is, a ${tf}_{w,
% d}$ score. The final procedure can be seen in Algorithm \ref{alg:tfidf}.

\begin{algorithm}[H]
\caption{TF-IDF in a Graph Database}\label{alg:tfidf}
\begin{algorithmic}
\Procedure{TF-IDF}{}
    \State $\textit{scores} \gets \emptyset$
    \State $\textit{sizes} \gets \emptyset$
    \For{token $\in$ query}
        \State $\textit{users} \gets \textit{query(users that discuss 'token')}$
        \State $\textit{df} \gets \textit{length(users)}$
        \State $\textit{count} \gets \textit{\# of occurences of 'token' in 'query'}$
        \State $\textit{wtq} \gets \textit{$count * log_{10}(\frac{length(documents)}{df})^2$}$
        \For{user $\in$ users}
        \State $\textit{tf} \gets \textit{query(\# of times 'user' discusses 'token')}$
          \State $\textit{scores[user]} \gets \textit{scores[user]} + wtq*tf$
          \State $\textit{sizes[user]} \gets \textit{query(\# of words discussed by 'user')}$
        \EndFor
    \EndFor
    \For{user $\in$ scores}
        \State $\textit{scores[user]} \gets \textit{$\frac{scores[user]}{sizes[user]}$}$
    \EndFor
    \State \Return {$sort(scores)$}
\EndProcedure
\end{algorithmic}
\end{algorithm}


\subsection{Kernel regression}

\subsubsection{Introduction}

For parametric regression we have:

\(y=f(X)\)

Where the form of \(f(X)\) is fixed, such as for linear regression.

For non-parametric regression we have:

\(y=m(X)\)

Where \(m(X)\) is not fixed.

We can estimate \(m(X)\) using kernel regression.

\(m(X)=\dfrac{\sum_{i=1}^nK_h(x-x_i)y_i}{\sum_{i=1}^nK_h(x-x_i)}\)

We know this because we have:

\(E(y|X)=\int yf(y|x)dy=\int y\dfrac{f(x,y)}{f(x)}dy\)

We then use kernel density estimation for both.


\documentclass[11pt]{article}

\usepackage{amsfonts}
\usepackage{fancyvrb}
\usepackage{url}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\topmargin}{0in}
\setlength{\headsep}{0.5in}
\setlength{\textheight}{8.5in}
\setcounter{page}{1}
%\pagestyle{empty}
%\hbadness=10000

\begin{document}
\huge
\noindent
{Discrete Optimization Assignment:}
\vspace{0.25cm}

\noindent
{\bf Graph Coloring}
\normalsize

\section{Problem Statement}

In this assignment you will design an algorithm to find the smallest {\em coloring} of a graph.\footnote{See \url{http://mathworld.wolfram.com/ChromaticNumber.html}}  You are provided with a graph and your task is to label the graph's nodes with as few colors as possible such that all pairs of nodes joined by an edge do not have the same color.  Figure \ref{fig:graph} illustrates a graph and a three coloring of that graph.  The nodes of the graph are labeled with black numbers while the coloring of the graph is labeled with white numbers.  You may notice that a three coloring is not a minimal coloring of this graph.  In fact, a two coloring is possible.

\begin{figure}[h]
        \centering
        \begin{subfigure}[b]{8.0cm}%{0.3\textwidth}
                \centering
                \includegraphics[width=8cm]{figures/coloring_1.pdf}
                \caption{A graph.}
                \label{fig:graph:input}
        \end{subfigure}%
        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
          %(or a blank line to force the subfigure onto a new line)
        \begin{subfigure}[b]{8.0cm}
                \centering
                \includegraphics[width=8cm]{figures/coloring_2.pdf}
                \caption{A three-coloring of the graph.}
                \label{fig:graph:coloring}
        \end{subfigure}
        \caption{A Graph Coloring Example}\label{fig:graph}
\end{figure}

\section{Assignment}

Write an algorithm to minimize the coloring of a graph.  The problem is mathematically formulated in the following way.  Given a graph $G = \langle N, E \rangle$ with nodes $N = 0 \ldots n-1$ and edges $E$, let $c_i \in \mathbb{N}$ be a variable denoting the color of node $i$.  Then the graph coloring problem is formalized as the following optimization problem,
$$
\begin{array}{ll}
\mbox{minimize:} & \displaystyle \max_{i \in 0 \ldots n-1} c_i \\
\mbox{subject to:} & \\
     & c_i \neq c_j  \;\;\; (\langle i,j \rangle \in E)
\end{array}
$$

\section{Data Format Specification}

The input consists of $|E| + 1$ lines.  The first line contains two numbers $|N|$ and $|E|$.
It is followed by $|E|$ lines, each line represents an edge $\langle u_i, v_j \rangle$ where $u_i, v_j \in 0 \ldots |N|-1$.

\vspace{0.2cm}
\noindent
Input Format
\vspace{-0.2cm}
\begin{Verbatim}[frame=single]
|N| |E|
u_0 v_0
u_1 v_1
...
u_|E|-1 v_|E|-1
\end{Verbatim}
%
The output has two lines.  The first line contains two values $obj$ and $opt$.  $obj$ is the numbers of colors used in the coloring (i.e. the objective value).  $opt$ should be $1$ if your algorithm proved optimality and $0$ otherwise.  The next line is a list of $n$ values in $\mathbb{N}$, one for each of the $c_i$ variables.  This line encodes the solution.

\vspace{0.2cm}
\noindent
Output Format
\vspace{-0.2cm}
\begin{Verbatim}[frame=single]
obj opt
c_0 c_1 c_2 ... c_n-1
\end{Verbatim}
%
%It is essential that the value order in the solution output matches the value order of the input.  Otherwise the grader will misinterpret the output.

\paragraph{Examples} \mbox{}
%\vspace{0.1cm}
\noindent
(based on Figure \ref{fig:graph})

\vspace{0.2cm}
\noindent
Input Example
\vspace{-0.2cm}
\begin{Verbatim}[frame=single]
4 3
0 1
1 2
1 3
\end{Verbatim}

\vspace{0.2cm}
\noindent
Output Example
\vspace{-0.2cm}
\begin{Verbatim}[frame=single]
3 0
0 1 2 2
\end{Verbatim}

\section{Instructions}

\input{instructions.tex}

%We use \texttt{stdout} for output.
%Output to other stream will be ignored (you may want to send runtime information to \texttt{stderr}). Your submission will be tested on a department linux machine. If your algorithm is a standalone program, please name it \texttt{nr},
%otherwise, please specify the compilation procedure,
%it is appreciated if you also provide a script that follows the above format to run the program.

\paragraph{Resources}
You will find several graph coloring instances in the \texttt{data} directory provided with the handout.

%An example output file, \texttt{blabla.out}, is also provided.

%\section{Remarks}

\input{handin.tex}

\input{grading.tex}

\input{collaboration.tex}

%\paragraph{Questions} Please contact the class GTA Carleton (cjc@cs.brown.edu).

\input{warnings.tex}

\paragraph{Hint}

The optimal value for  \texttt{data/gc\_1000\_5} is near $85$.

\input{techReqs.tex}

\end{document}


\subsection{Distributed Key Generation Protocol}
\label{ssec:dkg}

We desire a Byzantine Fault Tolerant consensus algorithm.
So, we let $\mathcal{P}$ be the total collection of participants with
$\abs{\mathcal{P}} = n$.
We set the threshold $t = \ceil{2n/3} - 1$ in our $\parens{t,n}$
secret sharing protocol.
Thus, it takes $t+1$ users to reconstruct a secret, which corresponds
to strictly greater than two-thirds of the participants.
We assume there is an open broadcast channel between all participants.
Encryption will be provided through Diffie-Hellman style shared
secret encryption; this will be discussed in Sec.~\ref{ssec:secret_enc}.
The group shared secret, henceforth called the \emph{master secret key},
will be the sum of the shared secrets of
each group member who correctly shared his secret.
Once there are $t+1$ valid partial signatures, these will be
combined to form a group signature.

As stated above, although the final master public key will
reside in $\G_{2}$, because the precompiled contracts
currently available in the Ethereum Virtual Machine only allow addition
and scalar multiplication in $\G_{1}$ (multiplication and exponentiation
in our multiplicative notation), we will primarily
use computations in $\G_{1}$ and anything required
in $\G_{2}$ will be confirmed via a \textsc{PairingCheck} call.

\subsubsection{Participant Setup}
Each participant $P_{i}\in\mathcal{P}$ begins by selecting
a secret key $\sk_{i}\in\Z_{q}$ with public key $\pk_{i} = g_{1}^{\sk_{i}}$.
The public-private key pair $\angles{\pk_{i},\sk_{i}}$ will be
used for secure communication over the insecure broadcast channel;
it will not be used for signing any messages.

\subsubsection{Verifiable Secret Sharing}

Participant $P_{i}$ chooses a secret
$s_{i}\in\Z_{q}$ to share with the other participants.
To do this, choose a secret polynomial $f_{i}:\Z_{q}\to\Z_{q}$
with

\begin{equation}
    f_{i}(x) = c_{i0} + c_{i1}x + c_{i2}x^{2} + \cdots + c_{it}x^{t},
\end{equation}

\noindent
where $c_{i0} = s_{i}, c_{i1}, \cdots, c_{it}$ are chosen uniformly
in $\Z_{q}$.
Setting

\begin{equation}
    C_{ik} = g_{1}^{c_{ik}}\quad k\in\braces{0,\cdots,t},
\end{equation}

\noindent
we have the corresponding public polynomial $F_{i}:\Z_{q}\to\G_{1}$:

\begin{equation}
    F_{i}(x) = C_{i0}C_{i1}^{x}\cdots C_{it}^{x^{t}}.
\end{equation}

\noindent
The shared secret from $P_{i}$ to $P_{j}$ is $s_{i\to j} = f_{i}(j)$
and

\begin{equation}
    \overline{\texttt{s}}_{i\to j} =
        \textsc{Encrypt}(\text{sk}_{i},\text{pk}_{j},j, s_{i\to j})
\end{equation}

\noindent
refers to a particular encryption scheme we discuss
in Sec.~\ref{ssec:secret_enc}.
Participant $P_{i}$ will broadcast the message

\begin{equation}
    \left\{
        \overline{\texttt{s}}_{i\to 1}, \overline{\texttt{s}}_{i\to 2},
            \cdots,
            \overline{\texttt{s}}_{i\to i-1},
            \overline{\texttt{s}}_{i\to i+1},
            \cdots,
            \overline{\texttt{s}}_{i\to n},
        C_{i0}, C_{i1}, \cdots, C_{it}
    \right\}
\end{equation}

\noindent
over the broadcast channel.
We note this message does not include the secret
$\overline{\texttt{s}}_{i\to i}$.

Once participant $P_{j}$ receives the message from $P_{i}$,
he sets

\begin{equation}
    \hat{s}_{i\to j} = \textsc{Decrypt}(\text{sk}_{j},\text{pk}_{i}, j,
        \overline{\texttt{s}}_{i\to j}).
\end{equation}

\noindent
$P_{j}$ then determines if

\begin{equation}
    g_{1}^{\hat{s}_{i\to j}} \overset{?}{=} F_{i}(j).
    \label{eq:secret_share_test}
\end{equation}

\noindent
If we have equality, then $\hat{s}_{i\to j} = s_{i\to j}$.
Otherwise, $P_{i}$ incorrectly shared his secret.

\subsubsection{Malicious shares}

We now suppose that $\overline{\texttt{s}}_{i\to j}$ is incorrect;
that is, we do not have equality in Eq.~\eqref{eq:secret_share_test}.
In order to prove this to be the case, everyone needs to be
able to prove that the encrypted secret
$\overline{\texttt{s}}_{i\to j}$ is incorrect.
To do this, $P_{j}$ must publish and prove the shared secret $k_{ij}$;
this is required in order to ensure bad actors
do not submit false proofs against honest actors.

Proving $k_{ij}$ is the shared secret is based on showing

\begin{equation}
    \pk_{j} = g_{1}^{\sk_{j}} \quad\text{and}\quad
    k_{ij} = \pk_{i}^{\sk_{j}}
\end{equation}

\noindent
\emph{without} sharing the secret key $\sk_{j}$;
that is, we wish to show
$\dlog_{g_{1}}(\pk_{j}) = \dlog_{\pk_{i}}(k_{ij})$
while keeping their common value ($P_{j}$'s secret key $\sk_{j}$) secret.
To do this, we use a zero-knowledge proof;
see Alg.~\ref{alg:zk_dleq_proof} for constructing the zk-proof
and Alg.~\ref{alg:zk_dleq_verify} for proof verification.
One reference for zk-proofs involving discrete logarithms
is~\cite{camenisch1997proof}.

Thus, $P_{j}$ would compute

\begin{equation}
    \pi(k_{ij}') = \textsc{DLEQ}(g_{1},\pk_{j},\pk_{i},k_{ij}',\sk_{j})
\end{equation}

\noindent
and publish $\angles{k_{ij}',\pi(k_{ij}')}$, where $k_{ij}'$
is claimed shared secret.
This allows anyone to use \textsc{DLEQ-verify} to determine
its validity.
If

\begin{equation}
    \textsc{DLEQ-verify}(g_{1},\pk_{j},\pk_{i},k_{ij}',\pi(k_{ij}'))
        = \texttt{true},
\end{equation}

\noindent
then $k_{ij}' = k_{ij}$, the shared secret.
Using this, everyone can decrypt $\overline{\texttt{s}}_{i\to j}$
by

\begin{equation}
    \hat{s}_{i\to j}
        = \textsc{DecryptSS}(k_{ij},j,\overline{\texttt{s}}_{i\to j}, b)
\end{equation}

\noindent
and determine if

\begin{equation}
    g_{1}^{\hat{s}_{i\to j}} \overset{?}{=} F_{i}(j).
    \label{eq:secret_share_test_2}
\end{equation}

\noindent
If the DLEQ proof $\pi(k_{ij}')$ shows $k_{ij}'$ is the shared
secret between $P_{j}$ and $P_{i}$ and we do not have equality in
Eq.~\eqref{eq:secret_share_test_2}, then $P_{i}$ is acted maliciously
and should be removed.
There are two other possibilities:
$k_{ij}'$ is not the shared secret, or $k_{ij}'$ is the shared secret
and we have equality in Eq.~\eqref{eq:secret_share_test_2}.
In both cases, $P_{j}$ acted maliciously and should be removed.
Thus, when $P_{j}$ submits a claim that $P_{i}$
failed to share a secret,
either $P_{j}$'s or $P_{i}$'s stake will be slashed.

In practice, $P_{j}$ will submit $P_{i}$'s broadcast message to an
Ethereum smart contract along with purported shared secret $k_{ij}'$
and proof $\pi(k_{ij}')$, and the smart contract would
determine its validity and burn stake as appropriate.

\input{algs/zk_dleq.tex}


\section{Hamilton's Equations}
Recall that the Lagrangian is defined by $L=L(\underline{q},\underline{\dot{q},t})=T-V$ where $\underline{q}$ is either the path of one particle or the path of many particles considered together in the configuration space.
\begin{definition}
    The Hamiltonian is the Legendre transform of $L$ wrt the velocity $\underline{\dot{q}}=\underline{v}$, so
    $$H(\underline{q},\underline{p},t)=\sup_{\underline{v}}(\underline{p}\cdot\underline{v}-\underline{L})$$
\end{definition}
The component $\underline{p}$ here is understood to be a generalised momentum.
So if $L$ is nice enough, we can write $H=\underline{p}\cdot\underline{v}-L(\underline{x},\underline{v},t)$ where $\underline{v}(\underline{p})$ is the solution to
$$p_i=\frac{\partial L}{\partial \dot{q_i}}$$
\begin{example}
    Consider $T=m|\underline{\dot{q}}|^2/2, V=V(\underline{q})$, so $\underline{v}=\underline{p}/m$, therefore
    $$H(\underline{q},\underline{p},t)=\frac{1}{2m}|\underline{p}|^2+V(\underline{q})$$
    So the Hamiltonian arises as the total energy.
\end{example}
What happened to the Euler-Lagrange equations?
We have $H=p_i\dot{q}^i-L(q^i,\dot{q}^i,t)$ where the summation in the first term is implied.
Suppose $L$ satisfies the Euler-Lagrange equations, we have
\begin{align*}
    \mathrm dH&=\frac{\partial H}{\partial q_i}\,\mathrm dq_i+\frac{\partial H}{\partial p_i}\,\mathrm dp_i+\frac{\partial H}{\partial t}\,\mathrm dt\\
    &=p_i\,\mathrm d\dot{q}^i+\dot{q}^i\,\mathrm dp_i-\frac{\partial L}{\partial q^i}\,\mathrm dq^i-\frac{\partial L}{\partial \dot{q}^i}\,\mathrm d\dot{q}^i-\frac{\partial L}{\partial t}\,\mathrm dt\\
    &=\dot{q}^i\,\mathrm dp_i-\dot{p}_i\,\mathrm dq^i-\frac{\partial L}{\partial t}\,\mathrm dt
\end{align*}
Since $\partial L/\partial \dot{q}^i=p_i$
So by comparing the differentials,
$$\dot{q}^i=\frac{\partial H}{\partial p_i},\dot{p}_i=-\frac{\partial H}{\partial q^i},\frac{\partial H}{\partial t}=-\frac{\partial L}{\partial t}$$
These equations are called the Hamilton's Equations.\\
Assume there is no explicit time dependence, then the system consists of $2n$ first-order ODEs, so we need to specify $q^i(0),p_i(0)$ for $i=1,\ldots,n$ for a typical system.
The solution curves to the Hamilton's equations is a trajectory in $2n$ dimensional ``phase space''.\\
Alternatively, Hamilton's equations also arise from the stationary points of a functional
$$S[\underline{q},\underline{p}]=\int_{t_1}^{t_2}f(\underline{q},\underline{p},\underline{\dot{q}},\underline{\dot{p}},t)\,\mathrm dt,f=\dot{q}^ip_i-H(\underline{q},\underline{p},t)$$
which variation wrt $p_i$ is then
$$\frac{\partial f}{\partial p_i}-\frac{\mathrm d}{\mathrm dt}\frac{\partial f}{\partial \dot{p}_i}=0\implies \dot{q}^i=\frac{\partial H}{\partial p_i}$$
and variation wrt $q_i$ is
$$\frac{\partial f}{\partial q^i}-\frac{\mathrm d}{\mathrm dt}\frac{\partial f}{\partial \dot{q}^i}=0\implies \dot{p}_i=-\frac{\partial H}{\partial q_i}$$
Which are just the Hamilton's equations.\\
Hamilton's equatios (or the Hamiltonian formulation of dynamics) actually allows one to extend the theory of classical dynamics to quantum settings, as observed by Paul Dirac in 1926.
So the Hamiltonian formalism can be seen as a bridge between classical and quantum physics.


\lab{Applications}{Balanced Trees}{Balanced Trees}
\label{lab:btrees}

Trees are very versatile structures.
Their worst case complexity of $O(\log n)$ for inserting, deleting, and searching make them an attractive option when working with lots of data.
In most cases, a simple binary search tree is sufficient.
However, there are cases when we lose all the benefits of a binary search tree.
One example is adding already sorted or nearly sorted data to a binary search tree.
This results in a degenerate BST that performs no better than a linked list!
Another problem is adding more levels to the tree than is necessary.
The more levels a tree has, the less performant it becomes.  If we could somehow fill a level or make it as close to full as possible, we can minimize the number of levels a a search tree has.

\begin{problem}
Write methods to determine the number of levels a tree has and how full each level is.  Look at breadth first search for inspiration on how to accomplish this.
\end{problem}

\section*{Balancing Trees}
There have been a variety of attempts at optimizing the structure of a binary tree.
The method that we will describe in depth is and AVL balanced tree.
AVL trees use the strictest definition of balance.
Another commonly used balanced binary tree is a red-black tree.  AVL trees optimize frequent lookups while red-black trees optimize insertion/deletion performance.
Red-black trees color nodes red or black and check for imbalance based on colors.  For example, a subtree is unbalanced if a red node has a red child.
AVL trees determine balance by comparing the height of right and  left subtrees.
The height of a node is the length of the path from a node to its deepest leaf node (the nodes at the base of the tree).
The height of a subtree is length of the path from a node to the deepest leaf node of that subtree.
For every node in an AVL tree, if the difference of height between the left and right subtrees is more than $\pm 1$ levels, then the tree is said to be \emph{out of balance}.  We can restore balance to the entire tree by balancing the nodes where the subtrees are out of balance.
There are four ways a tree can be unbalanced in an AVL tree.

\subsection*{Left-Left and Right-Right}
\begin{figure}[h]
\begin{subfigure}[b]{.49\textwidth}
    \includegraphics[width=.75\textwidth]{left_left.pdf}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
    \includegraphics[width=.75\textwidth]{balanced.pdf}
\end{subfigure}
\end{figure}

One of the primary cases, this imbalance can solved by a single rotation.
This will rotate the tree so that node B is the new root of the tree.
Node C becomes a child node of B and the right subtree of node becomes a left subtree of node C.
This preserves the ordered property of the tree.

\begin{figure}[h]
\includegraphics[width=.33\textwidth]{right_right.pdf}
\caption{The mirror image of the Left-Left case.}
\end{figure}

\subsection*{Left-Right and Right-Left}
\begin{figure}[h]
\begin{subfigure}[b]{.49\textwidth}
    \includegraphics[width=.75\textwidth]{left_right.pdf}
\end{subfigure}
\begin{subfigure}[b]{.49\textwidth}
    \includegraphics[width=.75\textwidth]{balanced.pdf}
\end{subfigure}
\end{figure}

These cases are a little more complex.
Two rotations must be performed to remedy the imbalance.
Node C is out of balance.
We first need rotate node B to be the parent of node A.
The left subtree of node B becomes the right subtree of node A.
Now we have to do a right rotate, moving node B to be the new root.
Node C becomes a child of node B and the the right subtree of B becomes the left subtree of node C.

\begin{figure}[h]
\includegraphics[width=.33\textwidth]{right_left.pdf}
\caption{The mirror image of the Left-Right case.}
\end{figure}

\begin{problem}
Subclass the BST from lab \ref{lab:Python_DataStructures} and add methods for balancing the BST.
You will also need to override the insert and remove methods from the BST because an AVL tree must be re-balanced on each insert or removal.
\end{problem}

\section*{Balanced Trees}
An AVL tree is an improvement over the ordinary BST because the tree is kept in an optimal shape.
There is, however, an even better improvement that can be made.
B-Trees are a very efficient form of balanced tree.
What makes them efficient?  B-Trees relax the requirement of only two children per node.
Balancing a B-Tree is also much simpler than balancing an AVL tree.
Balance is achieved when all leaf nodes are at the same depth.
Instead of using rotations to balance the tree, the B-Tree uses splitting and joining of nodes to maintain balance.

\subsection*{Structure of a B-Tree}
B-Trees are efficient due to their extreme branching.  It is common for B-Trees to hundreds, maybe even thousands of branches.
This extreme branching per node reduces the overall height of the tree considerably.
B-Trees of order $n$ satisfies the following properties:
\footnote{Knuth, Donald. \emph{The Art of Computer Programming: Sorting and Searching (Volume 3, $2^{nd}$ edition)}. New Jersey: Addison-Wesley, 1998. pp 483.}
\begin{enumerate}
\item Every node has at most $n$ children.
\item Every node, except for the root and the leaves, has at least $n/2$ children.
\item The root has at least 2 children (unless it is a leaf).
\item \label{enum:infoleaf} All leaves appear on the same level, and carry no information.
\item A nonleaf node with $n$ children contains $n-1$ keys.
\end{enumerate}
Some implementations modify requirement \ref{enum:infoleaf} so that all information is stored in only the leaf nodes.  In Knuth's original definition, the leaf nodes have references to the information we are storing rather than containing it directly.

The number of keys that a single B-Tree can hold grows exponentially with each new level of the tree.  A B-Tree of depth, $d$, and order $n$ (meaning each node can have at most $n$ children) can store a maximum of $n^{d} - 1$ keys.  For example, a B-Tree of order 100 and depth 3 can store 999999 keys.  Adding a new level increases the total capacity one hundred fold, or 99999999 keys!
In a B+Tree, the actual data of the tree is stored only in the bottom most nodes, or leaf nodes.  All other nodes in the tree are index nodes.

Unlike binary tree, B-Trees are built from the bottom up (starting with a single leaf node).  As more leaves are added, the root changes from a leaf node to an index node.

\begin{problem}
Verify that the maximum number of keys that a B-Tree of order $n$ and depth $d$ is $n^{d}-1$.
What is the maximum number of keys an AVL tree of depth $d$ can store?

Suppose a disk seek time is $.15$ms and we have an AVL tree and B-Tree stored on disk.  The B-Tree is order 50.
Predict the time required to access a leaf of the AVL tree.  The B-Tree?
It is for this reason that B-Trees are great for on-disk storage.  B-Trees are used in a variety of settings including databases and filesystems.
\end{problem}

\subsection*{Insertion and Deletion Methods of a B-Tree}
A B-Tree is balanced when all leaf nodes are on the same level.
The AVL rotations do work in balancing a B-Tree.
Instead balance is achieved and maintained by splitting and joining nodes during the insertion and removal of keys.
If the node is not full, inserting a new key is trivial.
We insert the key in sorted order into a leaf node.
Splitting is done when a node is full.  To make room for more keys, we have to split the full node into two nodes, each of which is only half full.
To split a node, we find a median key.
All keys less than the median are moved a node and all keys greater than the median are moved to the other node.
The median key is moved to the parent node and the node pointers in the parent node are updated with the newly created nodes.
However, moving the median to the parent node could cause the parent node to be split.  This process is repeated all the way to the root node.
If the root node needs to be split, a new root node is created and the old root node becomes a child node.  This is how the tree grows.
Doing splits like this can affect the insertion performance of the tree.
One alternative method is to split any node close to full as traverse the tree on insertion.  This causes many unnecessary splits, but avoids some costly re-balancing after insertion.

We can also remove a key from a leaf trivially as long as number of keys is more than then minimum allowed per node.
There are many different cases for removing a key.
They can all, however, be reduced to a single case (case \ref{enum:case1}).
The cases to consider are:
\begin{enumerate}
\item \label{enum:case1} Remove from a leaf node
\item \label{enum:case2} Remove from internal node
\begin{enumerate}
\item \label{enum:case2a} Remove with key re-ordering
\item \label{enum:case2b} Remove with node joining
\end{enumerate}
\end{enumerate}

Case \ref{enum:case2a} reduces to removing from an internal leaf node
when a key can be promoted from a leaf node to replace the key we are removing.
If we remove a key and the node underflows (has fewer than the minimum allowed keys), we have to do one of two things.  If possible, we borrow a key from an adjacent sibling node.
This is done by bringing down the median key and promoting the first key of the adjacent node to be the new median key.
We can do this only if the sibling node will not underflow as a result.  If underflow will happen, we have to join nodes.
To join nodes, we combine the keys of one node with that of another, adjacent, node and also demote the median key separating the two nodes in the parent.  The new node will contain the keys of child node $a$, the median key, and the keys from child node $b$.

\begin{problem}
Illustrate the insert, remove, and find methods for a B-Tree.  You do not have to implement them in Python.

\begin{figure}[H]
\includegraphics[width=.5\textwidth]{3.png} \\
Insert 12
\end{figure}

\begin{figure}[H]
\includegraphics[width=\textwidth]{8.png}
Insert 18
\end{figure}

\begin{figure}[H]
\includegraphics[width=\textwidth]{10.png}
Remove 19
\end{figure}

\begin{figure}[H]
\includegraphics[width=\textwidth]{11.png}
Remove 17
\end{figure}

\begin{figure}[H]
\includegraphics[width=\textwidth]{12.png}
Remove 4
\end{figure}

\end{problem}


\section{Verified Parallelization of Monoid Morphisms}\label{sec:parallelization}

A monoid morphism is a function between two monoids which
preserves the monoidal structure; \ie a function on the underlying
sets which preserves identity and associativity. We formally specify
this definition using a refinement type @Morphism@.
%
\begin{definition}[Monoid Morphism]\label{definition:morphism}
A function @f :: n -> m@ is a morphism
between the monoids
(@m@, @$\epsilon$@, @<>@)
and (@n@, @$\eta$@, @<+>@),
if @Morphism n m f@ has an inhabitant.
\begin{code}
  type Morphism n m F
    = x:n -> y:n -> {F eta = epsilon && F (x <+> y) = F x <> F y}
\end{code}
\end{definition}

A monoid morphism can be parallelized when its domain can be cut into
chunks and put back together again, a property we refer to as
chunkable and expand upon in \S~\ref{subsec:chunkable}. A
chunkable monoid morphism is then parallelized by:
\begin{itemize}
  \item chunking up the input,
  \item applying the morphism in parallel to all chunks, and
  \item recombining the chunks, also in parallel, back to a single value.
\end{itemize}
In the rest of this section we implement and verify to be correct the above
transformation.

\subsection{Chunkable Monoids}\label{subsec:chunkable}
\begin{definition}[Chunkable Monoids]\label{definition:chunkable}
A monoid (@m@, @epsilon@, @<>@) is chunkable
if the following four functions are defined on @m@.
\begin{code}
  length_m :: m -> Nat

  drop_m   :: i:Nat -> x:MGEq m i -> MEq m (length_m x - i)
  take_m   :: i:Nat -> x:MGEq m i -> MEq m i

  takeDropProp_m :: i:Nat -> x:m -> {x = take_m i x <> drop_m i x}
\end{code}

Where the type aliases @MLeq m I@ (and @MEq m I@)
constrain the monoid @m@ to have @length_m@
greater than (resp. equal) to @I@.
\begin{code}
  type MGEq m I = {x:m | I <= length_m x}
  type MEq  m I = {x:m | I =  length_m x}
\end{code}
\end{definition}

Note that the ``important'' methods of chunkable monoids
are the @take@ and @drop@, while the @length@ method is required
to give pre- and post-condition on the other operations.
%
Finally, @takeDropProp@ provides a proof that
for each @i@ and monoid @x@, appending
@take i x@ to @drop i x@ will reconstruct @x@.

Using @take_m@ and @drop_m@ we define for each chunkable monoid
(@m@, @epsilon@, @<>@) a function @chunk_m i x@ that
splits @x@ in chunks of size @i@.
\begin{code}
  chunk_m :: i:Pos -> x:m -> {v:L m | chunkRes_m i x v}
  chunk_m i x
    | length_m x <= i = C x N
    | otherwise      = take_m i x `C` chunk_m i (drop_m i x)

  chunkRes_m i x v
    | length_m x <= i = length_m v == 1
    | i == 1         = length_m v == length_m xs
    | otherwise      = length_m v <  length_m xs
\end{code}

%
The function @chunk_m@ provably terminates as
@drop_m i x@
will return a monoid smaller than @x@,
by the Definition of @drop_m@.
%
The definitions of both @take_m@ and @drop_m@
are also used from Liquid Haskell to verify the
@length_m@ constraints in the result of @chunk_m@.

\ignore{
As a concrete example, to define list chunking, we first define the @take@ and @drop@
methods on the list monoid of section~\ref{sec:haskell-proofs}.
%
\begin{code}
  take i N                    = N
  take i (C x xs) | i == 0    = N
                  | otherwise = C x (take (i-1) xs)

  drop i N                    = N
  drop i (C x xs) | i == 0    = C x xs
                  | otherwise = drop (i-1) xs
\end{code}
We can prove that the above definitions
combined with the @length@ of section~\ref{sec:haskell-proofs}
satisfy the specifications
of the Chunkable Monoid Definition~\ref{definition:chunkable}.
%
Thus, we can prove that the aforementioned list data type,
extended with the appropriate implementation for @takeDropProp@
is a chunkable monoid.
}

\subsection{Parallel Map}
We define a parallelized map function @pmap@
using Haskell's library @parallel@.
%
Concretely, we use the function
@Control.Parallel.Strategies.withStrategy@
that computes its argument in parallel given a parallel strategy.
\begin{code}
  pmap :: (a -> b) -> L a -> L b
  pmap f xs = withStrategy parStrategy (map f xs)
\end{code}
%
The strategy @parStrategy@ does not affect verification.
%
In our codebase we choose the traversable strategy.
\begin{code}
  parStrategy :: Strategy (L a)
  parStrategy = parTraversable rseq
\end{code}

\mypara{Parallelism in the Logic}
The function @withStrategy@ is an imported Haskell library function,
whose implementation is not available during verification.
%
To use it in our verified code, we make the \textit{assumption}
that it always returns its second argument.
\begin{code}
  assume withStrategy :: Strategy a -> x:a -> {v:a | v = x}
\end{code}
%
Moreover, we need to reflect the function @pmap@ and represent its
implementation in the logic.
%
Thus, we also need to represent the function @withStrategy@ in the logic.
%
LiquidHaskell represents @withStrategy@ in the logic as a logical
function that merely returns
its second argument, @withStrategy _ x = x@,
and does not reason about parallelism.

\subsection{Monoidal Concatenation}\label{subsec:mconcat}
The function @chunk_m@ allows chunking a monoidal value into several
pieces. Dually, for any monoid @m@, there is a
standard way of turning @L m@ back into a single @m@~\footnote{\texttt{mconcat} is usually defined as \texttt{foldr mappend mempty}}.
\begin{code}
  mconcat :: L m -> m
  mconcat N        = mempty
  mconcat (C x xs) = x <> mconcat xs
\end{code}
%
For any chunkable monoid @n@,
%
monoid morphism @f :: n -> m@,
%
and natural number @i > 0@
%
we can write a chunked version of @f@ as
\begin{code}
  mconcat . pmap f . chunk_n i :: n -> m.
\end{code}
Before parallelizing @mconcat@, we will prove that the previous function is equivalent to @f@.

\begin{theorem}[Morphism Distribution]\label{theorem:monoid:distribution}
Let (@m@, @$\epsilon$@, @<>@) be a monoid
and (@n@, @$\eta$@, @<+>@) be a chunkable monoid.
%
Then, for every morphism @f :: n -> m@,
every positive number @i@ and input @x@,
@f x = mconcat (pmap f (chunk_n i x))@ holds.
%
\begin{code}
  morphismDistribution
    :: f:(n -> m) -> Morphism n m f -> x:n -> i:Pos
    -> {f x = mconcat (pmap f (chunk_n i x))}
\end{code}
\end{theorem}

\begin{proof}
We prove the theorem by implementing
@morphismDistribution@ in a way that satisfies its type.
%
The proof proceeds by induction on the length of the input.
%
\begin{code}
  morphismDistribution f thm x i
    | length_n x <= i
    =  mconcat (pmap f (chunk_n i x))
    =. mconcat (map f (chunk_n i x))
    =. mconcat (map f (C x N))
    =. mconcat (f x `C` map f N)
    =. f is <> mconcat N
    =. f is <> epsilon
    =. f is ? idRight_m (f is)
    ** QED

  morphismDistribution f thm x i
    =   mconcat (pmap f (chunk_n i x))
    =. mconcat (map f (chunk_n i x))
    =. mconcat (map f (C takeX) (chunk_n i dropX)))
    =. mconcat (f takeX `C` map f (chunk_n n dropX))
    =. f takeX <> f dropX   ? morphismDistribution f thm dropX i
    =. f (takeX <+> dropX)   ? thm takeX dropX
    =. f x                 ? takeDropProp_n i x
    ** QED
    where
      dropX = drop_n i x
      takeX = take_n i x
\end{code}
%
In the base case we use rewriting and right identity on the monoid @f x@.
%
In the inductive case,
we use the inductive hypothesis on the input @dropX = drop_n i x@,
that is provably smaller than @x@ as @1 < i@.
%
Then, the fact that @f@ is a monoid morphism,
as encoded by our assumption argument @thm takeX dropX@
we get basic distribution of @f@, that is
@f takeX <> f dropX = f (takeX <+> dropX)@.
%
Finally, we merge @takeX <+> dropX@ to @x@
using the property @takeDropProp_n@ of the chunkable monoid @n@.
\cqed\end{proof}

\subsection{Parallel Monoidal Concatenation}\label{subsec:pmconcat}
%
We now parallelize the monoid concatenation by defining a
@pmconat i x@ function that chunks the input list of monoids and concatenates each
chunk in parallel.

We use the @chunk@ function of \S~\ref{subsec:chunkable} instantiated to @L m@ to define a parallelized version of
monoid concatenation @pmconcat@.
\begin{code}
  pmconcat :: Int -> L m -> m
  pmconcat i x | i <= 1 || length x <= i
    = mconcat x
  pmconcat i x
    = pmconcat i (pmap mconcat (chunk i x))
\end{code}
The function @pmconcat i x@ calls @mconcat x@ in the base case,
otherwise it
(1) chunks the list @x@ in lists of size @i@,
(2) runs in parallel @mconcat@ to each chunk,
(3) recursively runs itself with the resulting list.
%
Termination of @pmconcat@ holds, as the length of @chunk i x@
is smaller than the length of @x@, when @1 < i@.

Next, we prove equivalence of parallelized monoid concatenation.
%
\begin{theorem}[Correctness of Parallelization]\label{theorem:equivalence:concat}
Let (@m@, @$\epsilon$@, @<>@) be a monoid.
Then, the parallel and sequential concatenations are equivalent.
\begin{code}
  pmconcatEq :: i:Int -> x:L m -> {pmconcat i x = mconcat x}
\end{code}
\end{theorem}

\begin{proof}
We prove the theorem by providing a Haskell implementation of @pmconcatEq@
that satisfies its type.
%
The details of the proof can be found in~\cite{implementation},
here we provide the sketch of the proof.

First, we prove that @mconcat@ distributes over list splitting
\begin{code}
  mconcatSplit
    :: i:Nat -> xs:{L m | i <= length xs}
    -> {mconcat xs = mconcat (take i xs) <> mconcat (drop i xs)}
\end{code}
%
The proofs proceeds by structural induction, using monoid left identity in the base case
and monoid associativity associavity and unfolding of @take@ and @drop@
methods in the inductive step.

We generalize the above
to prove that @mconcat@ distributes over list chunking.
\begin{code}
  mconcatChunk
    :: i:Pos -> xs:L m
    -> {mconcat xs = mconcat (map mconcat (chunk i xs))}
\end{code}
%
The proofs proceeds by structural induction, using monoid left identity in the base case
and lemma @mconcatSplit@ in the inductive step.

Lemma @mconcatChunk@ is sufficient to prove @pmconcatEq@ by structural induction,
using monoid left identity in the base case.
\cqed\end{proof}

\subsection{Parallel Monoid Morphism}\label{subsec:both-levels}
We can now replace the @mconcat@ in our chunked monoid morphism in
\S~\ref{subsec:mconcat} with @pmconcat@ from
\S~\ref{subsec:pmconcat} to provide an implementation that uses
parallelism to both map the monoid morphism and concatenate the
results.

%\paragraph{Correctness} of our parallel monoid morphism follows from Theorems~\ref{theorem:monoid:distribution} and~\ref{theorem:equivalence:concat}.
%
\begin{theorem}[Correctness of Parallelization]\label{theorem:two-level}
Let (@m@, @$\epsilon$@, @<>@) be a monoid
and (@n@, @$\eta$@, @<+>@) be a chunkable monoid.
%
Then, for every morphism @f :: n -> m@,
every positive numbers @i@ and @j@, and input @x@,
@f x = pmconcat i (pmap f (chunk_n j x))@ holds.
%
\begin{code}
  parallelismEq
    :: f:(n -> m) -> Morphism n m f -> x:n -> i:Pos -> j:Pos
    -> {f x = pmconcat i (pmap f (chunk_n j x))}
\end{code}
\end{theorem}

\begin{proof}
We prove the theorem by providing an implementation of
@parallelismEq@ that satisfies its type.
%
\begin{code}
  parallelismEq f thm x i j
    =   pmconcat i (pmap f (chunk_n j x))
    =. mconcat (pmap f (chunk_n j x))
       ? pmconcatEq i (pmap f (chunk_n j x))
    =. f x
       ? morphismDistribution f thm x j
    ** QED
\end{code}
The proof follows merely by application of the
two previous Theorems~\ref{theorem:monoid:distribution} and~\ref{theorem:equivalence:concat}.
\cqed\end{proof}

\mypara{A Basic Time Complexity} analysis of the algorithm
reveals that parallelization of morphism  leads to runtime speedups
on monads with fast (constant time) appending operator.

We want to compare the complexities of the sequential @f i@
and the two-level parallel @pmconcat i (pmap f (chunk_n j x))@.
%
Let $n$ be the size on the input @x@.
Then, the sequential version runs in time
$T_f(n) = O(n)$, that is equal to the time complexity of the morphism @f@ on input @i@.
%

The parallel version runs @f@ on inputs of size $n' = \frac{n}{j}$.
%
Assuming the complexity of @x <> y@ to be $T_\mappend(\text{max}(|\tx|, |\ty|))$,
complexity of @mconcat xs@ is $O((\texttt{length \txs}-1) T_\mappend(\text{max}_{\tx_i \in \txs}(|\tx_i|)))$.
%
Now, parallel concatenation, @pmconcat i xs@ at each iteration runs @mappend@
on a list of size @i@. Moreover,
at each iteration, divides the input list in chunks of size @i@, leading to
$\frac{\log|xs|}{\log i}$ iterations, and time complexity
$(i-1)(\frac{\log|xs|}{\log i})(T_\mappend(m))$
for some $m$ that bounds the size of the monoids.

The time complexity of parallel algorithm consists on the base cost on running @f@
at each chunk and then parallel concatenating the $\frac{n}{j}$ chunks.
\begin{equation}
O((i-1)(\frac{\log n - \log j}{\log i})T_\mappend(m) + T_f(\frac{n}{j})) \label{eq:complexity}
\end{equation}
%
Since time complexity depends on the time complexity of @<>@
for the parallel algorithm to be efficient time complexity of @<>@ should be constant.
%
Otherwise, if it depends on the size of the input, the size of monoids can grow at each iteration of @mconcat@.
%

Moreover, from the complexity analysis we observe that time grows on bigger @i@ and smaller @j@.
%
Thus, chunking the input in small chunks while splitting the monoid list in half leads
to more parallelism, and thus (assuming infinite processors and no caching) greatest speedup.


\section*{Exercise 3.1}
\enum{
\item
We know that
\spl{
    &P[\text{a random sample of 50 pages contains at least one error}]\\
    =&1-P[\text{a random sample of 50 pages contains no error}]\\
    =&1-(\frac{150}{200})^5\\
    \approx&0.763.
}
\item
We denote that the probability of being sampled is $p$, then
\spl{
    &P[\text{a random sample of 50 pages contains at least three error}]\\
    =&\sum_{k=3}^5\binom{5}{k}p^k(1-p)^{5-k}\\
    \approx&1-\Phi(\frac{3-1/2-5p}{\sqrt{5p(1-p)}})\\
    \overset{!}{=}&90\%.
}

Thus
\spl{
    &\frac{3-1/2-5p}{\sqrt{5p(1-p)}}=-1.29,\\
    &p=0.750.\\
    &n=200p\approx150.
}
Hence, the random sample must contain 150 pages.
}

\section*{Exercise 3.2}
\enum{
\item
\spl{
R_1(t)&=1-\int_0^t 0.003x^{-0.5}e^{-0.006x^{0.5}}\dd x=1-(-e^{-0.006x^{0.5}})\bigg|_0^t=e^{-0.006t^{0.5}}.\\
R_2(t)&=1-\int_0^t\frac{1}{25000}e^{-\frac{x}{25000}}=1-(-e^{-\frac{x}{25000}}\bigg|_0^t)=e^{-t/25000}.\\
R_s(t)&=\prod_{i=1}^2R_1(t)R_2(t)=e^{-0.006t^{0.5}-t/25000}.
}

Hence, at 2500 hours,
\spl{
    R_s(2500)\approx0.67.
}

\item
\spl{
    F_s(t)=1-R_s(t)=1-e^{-0.006t^{0.5}-t/25000}.
}

When $t<2000$, the probability that the system fails is
\spl{
    F_s(2000)=0.29.
}

\item
\spl{
    &R_p(t)=1-\prod_{i=1}^2(1-R_1(t))(1-R_2(t))\\
    =&R_1(t)+R_2(t)-R_1(t)R_2(t)\\
    =&e^{-0.006t^{0.5}}+e^{-t/25000}-e^{-0.006t^{0.5}-t/25000}.
}
Hence, at 2500 hours,
\spl{
    R_p(t)\approx0.98.
}
}

\section*{Exercise 3.3}
\enum{
\item
\spl{
    \sum_{x=1}^n\sum_{y=x}^n f_{XY}(x,y)=\sum_{x=1}^n\sum_{y=x}^n\frac{2}{n(n+1)}=\frac{n(n+1)}{2}\frac{2}{n(n+1)}=1.
}
Also, $f_{XY}(x,y)>=0$.
Hence, $f_{XY}(x,y)$ is a density.

\item
To find the marginal density,
\spl{
    &f_X(x)=\sum_{y=x}^n\frac{2}{n(n+1)}=\frac{2(n-x+1)}{n(n+1)}.\\
    &f_Y(y)=\sum_{x=1}^y\frac{2}{n(n+1)}=\frac{2y}{n(n+1)}.\\
}

\item
\spl{
    f_X(x)f_Y(y)&=\frac{2(n-x+1)}{n(n+1)}\frac{2y}{n(n+1)}\\
    &=\frac{4y(n-x+1)}{n^2(n+1)^2}\neq\frac{2}{n(n+1)}.
}

Thus, $X$ and $Y$ are not independent.

\item
\spl{
    P[X\leq 3 \text{ and } Y\leq 2]=\sum_{x=1}^3\sum_{y=x}^2\frac{2}{5\cdot(5+1)}=\frac{1}{15}\times3=\frac{1}{5}.
}
}

\section*{Exercise 3.4}
We denote that $V=Y$. Considering $(x,y)\to(u,v)$, we apply
\spl{
    f_{U,V}(u,v)=f_{X,Y}\circ H^{-1}(u,v)\cdot|\text{det} DH^{-1}(u,v)|,
}
where $(x,y)=H^{-1}(u,v)=(u-v,v)$ and det $DH^{-1}(u,v)=1$.

Thus,
\spl{
    f_{U,V}(u,v)=f_{XY}(u-v,v).
}

Hence, the marginal density is
\spl{
    f_U(u)=\int_{-\infty}^{\infty}f_{XY}(u-v,v)dv.
}

\section*{Exercise 3.5}
We know that
\[
    f_X(x)=
    \left\{
    \begin{aligned}
        &\frac{1}{3}e^{-\frac{x}{3}}, \quad x>0\\
        &0, \quad x\leq0\\
    \end{aligned}
    \right.
    ,
    \quad
    f_Y(y)=
    \left\{
    \begin{aligned}
        &e^{-y}, \quad y>0\\
        &0, \quad y\leq0\\
    \end{aligned}
    \right.
\]

Since $U=X+Y$, then
\spl{
    f_U(u)&=\int_0^{u}\frac{1}{3}e^{-\frac{x}{3}}e^{-(u-x)}\dd x\\
    &=\frac{1}{3}e^{-u}\int_0^ue^{\frac{2}{3}x}\dd x\\
    &=\frac{1}{2}e^{-u}(e^{\frac{2}{3}u}-1)\\
    &=(e^{-u/3}-e^{-u})/2.
}

\section*{Exercise 3.6}
We assume that $X_1$ and $X_2$ are independent. Otherwise we cannot determine their covariance.
We know that
\spl{
    f_1(x)&=\frac{1}{\sqrt{2\pi}\sigma_1}e^{-\frac{(x-\mu_1)^2}{2\sigma_1^2}}\\
    m_1(t)&=e^{\mu_1 t+\sigma_1^2t^2/2}\\
    f_2(x)&=\frac{1}{\sqrt{2\pi}\sigma_2}e^{-\frac{(x-\mu_2)^2}{2\sigma_2^2}}\\
    m_2(t)&=e^{\mu_2 t+\sigma_2^2t^2/2}\\
}

First we prove that if $Z=\alpha X$, then $m_Z(t)=m_X(\alpha t)$.

\spl{
    &m_Z(t)=E[e^{tZ}]= E[e^{\alpha tX}]\\
    &=E[e^{(\alpha t)X}]\\
    &=m_X(\alpha t).
}

Also, we know that if $Z=X+Y$, then $m_Z(t)=m_X(t)m_Y(t)$.

Since $Y=\lambda_1X_1+\lambda_2X_2$, then
\spl{
    m_Y(t)=&m_{X_1}(\lambda_1 t)m_{X_2}(\lambda_2 t)\\
    &=e^{\mu_1 \lambda_1t+\sigma_1^2(\lambda_1t)^2/2+\mu_2 \lambda_2t+\sigma_2^2(\lambda_2t)^2/2}\\
    &=e^{(\mu_1\lambda_1+\mu_2\lambda_2)t+(\sigma_1^2\lambda_1^2+\sigma_2^2\lambda_2^2)t^2/2}.
}

Hence, $Y$ follows normal distribution, where
\spl{
    E[Y]&=\lambda_1\mu_2+\lambda_2\mu_2,\\
    \text{Var} Y&=\sigma_1^2\lambda_1^2+\sigma_2^2\lambda_2^2.\\
}

\section*{Exercise 3.7}
\enum{
\item
\spl{
    f_{X_1}(x_1)&=\int_{-\infty}^\infty f_{X_1X_2}(x_1,x_2)\dd x_2\\
    &=\frac{1}{2\pi\sigma_1\sigma_2\sqrt{1-\varrho^2}}\int_{-\infty}^\infty e^{-\frac{1}{2(1-\varrho^2)}[(\frac{x_1-\mu_1}{\sigma_1})^2-2\varrho(\frac{x_1-\mu_1}{\sigma_1})(\frac{x_2-\mu_2}{\sigma_2})+(\frac{x_2-\mu_2}{\sigma_2})^2]}\dd x_2\\
}

Since
\spl{
    -2\varrho(\frac{x_1-\mu_1}{\sigma_1})(\frac{x_2-\mu_2}{\sigma_2})+(\frac{x_2-\mu_2}{\sigma_2})^2=(\frac{x_2-\mu_2}{\sigma_2}-\varrho(\frac{x_1-\mu_1}{\sigma_1}))^2-\varrho^2(\frac{x_1-\mu_1}{\sigma_1})^2,
}
then
\spl{
    f_{X_1}(x_1)=\frac{1}{2\pi\sigma_1\sigma_2\sqrt{1-\varrho^2}}e^{-\frac{(x_1-\mu_1)^2}{2\sigma_1^2}}\int_{-\infty}^\infty e^{-\frac{1}{2(1-\varrho^2)}(\frac{x_2-\mu_2}{\sigma_2}-\varrho(\frac{x_1-\mu_1}{\sigma_1}))^2}\dd x_2.
}

We denote that
\spl{
    t=-\frac{1}{\sqrt{1-\varrho^2}}(\frac{x_2-\mu_2}{\sigma_2}-\varrho(\frac{x_1-\mu_1}{\sigma_1})).
}

Hence,
\spl{
    f_{X_1}(x_1)&=\frac{1}{2\pi\sigma_1}e^{-\frac{(x_1-\mu_1)^2}{2\sigma_1^2}}\int_{-\infty}^\infty e^{-\frac{t^2}{2}}\dd t\\
    &=\frac{1}{\sqrt{2\pi}\sigma_1}e^{-\frac{(x_1-\mu_1)^2}{2\sigma_1^2}}.
}

Hence $x_1\sim N(\mu_1,\sigma_1^2)$.
\item
We know that the coefficient of correlation of $X_1$ and $X_2$ is
\spl{
    \varrho_{X_1X_2}&=\frac{\text{Cov}(X_1,X_2)}{\sigma_1\sigma_2}=\frac{E[(X_1-\mu_1)(X_2-\mu_2)]}{\sigma_1\sigma_2}\\
    &=\frac{\iint_{\mathbb{R}^2}(x_1-\mu_1)(x_1-\mu_2)f_{X_1X_2}(x_1,x_2)\dd x_1\dd x_2}{\sigma_1\sigma_2}
}

We denote $u=\frac{x_1-\mu_1}{\sigma_1}$ and $v=\frac{x_1-\mu_2}{\sigma_2}$, then
\spl{
    &\iint_{\mathbb{R}^2}(x_1-\mu_1)(x_2-\mu_2)f_{X_1X_2}(x_1,x_2)\dd x_1\dd x_2\\
    =&\frac{\sigma_1\sigma_2}{2\pi\sqrt{1-\varrho^2}}\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}uv \exp\bigg[-\frac{1}{2(1-\varrho^2)}(u^2-2\varrho uv+v^2)\bigg]\dd u\dd v\\
    &=\frac{\sigma_1\sigma_2}{2\pi\sqrt{1-\varrho^2}}\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}uv\exp\bigg[-\frac{u^2}{2}-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}\bigg]\dd u\dd v\\
    &=\frac{\sigma_1\sigma_2}{2\pi\sqrt{1-\varrho^2}}\int_{-\infty}^{\infty}ue^{-\frac{u^2}{2}}\bigg[\int_{-\infty}^{\infty}v e^{-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}\dd v\bigg]\dd u\\
    &=\sigma_1\sigma_2\int_{-\infty}^{\infty}\frac{u}{\sqrt{2\pi}}e^{-\frac{u^2}{2}}\bigg[\int_{-\infty}^{\infty}\frac{v}{\sqrt{2\pi}\sqrt{1-\varrho^2}}e^{-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}\dd v\bigg]\dd u\\
    &=\sigma_1\sigma_2\int_{-\infty}^{\infty}\frac{u}{\sqrt{2\pi}}e^{-\frac{u^2}{2}}\cdot \varrho u\ \dd u\\
    &=\varrho\sigma_1\sigma_2\int_{-\infty}^{\infty}\frac{u^2}{\sqrt{2\pi}}e^{-\frac{u^2}{2}}\dd u\\
    &=\varrho\sigma_1\sigma_2.
}

Hence,
\spl{
    \varrho_{X_1X_2}=\frac{\varrho\sigma_1\sigma_2}{\sigma_1\sigma_2}=\varrho.
}

\item
Sufficiency:

if $\varrho=0$, then
\spl{
    f_{X_1X_2}(x_1,x_2)=\frac{1}{2\pi\sigma_1\sigma_2}e^{-\frac{1}{2}[(\frac{x_1-\mu_1}{\sigma_1})^2+(\frac{x_2-\mu_2}{\sigma_2})^2]}.
}
Also,
\spl{
    f_{X_1}(x_1)f_{X_2}(x_2)&=\frac{1}{2\pi\sigma_1\sigma_2}e^{[-(\frac{x_1-\mu_1}{2\sigma_1}^2+\frac{x_2-\mu_2}{2\sigma_2})^2]}\\
    &=f_{X_1X_2}(x_1,x_2),
}
which means $X_1$ and $X_2$ are independent.

Necessity:

if $X_1$ and $X_2$ are independent, then
\spl{
    f_{X_1X_2}(x_1,x_2)=f_{X_1}(x_1)f_{X_2}(x_2).
}
Specially, when $x_1=\mu_1$ and $x_2=\mu_2$,
\spl{
    \frac{1}{2\pi\sigma_1\sigma_2\sqrt{1-\varrho^2}}=\frac{1}{\sqrt{2\pi}\sigma_1\sqrt{2\pi}\sigma_2}.
}

Hence $\varrho=0$.

It's not true for a bivariate random variable with an arbitrary distribution.

For example,
\begin{table}[H]
    \centering
    \begin{tabular}{c|cccc}
        X,Y & 0 & $\frac{1}{4}$ & 1\\\hline
        -1 & 0 & 0 & $\frac{1}{5}$\\
        $\frac{1}{2}$ & 0 & $\frac{1}{5}$ & 0\\
        0 & $\frac{1}{5}$ & 0 & 0\\
        $\frac{1}{2}$ & 0 & $\frac{1}{5}$ & 0\\
        1 & 0 & 0 & $\frac{1}{5}$\\
    \end{tabular}
\end{table}

Here we know that $Y=X^2$. However,
\spl{
    &E[XY]=-\frac{1}{5}+\frac{1}{8}\frac{1}{5}+0\frac{1}{5}+\frac{1}{5}-\frac{1}{8}\frac{1}{5}=0,\\
    &E[X]E[Y]=0,\quad E[Y]=0.\\
    &\text{Cov}(X,Y)=E[XY]-E[X]E[Y]=0.
}

Here $\varrho_{XY}=0$, but $X$ and $Y$ are not independent.

\item
First, we derive the conditional density
\spl{
    f_{X_2|_{x_1}}=\frac{f_{X_1X_2}(x_1,x_2)}{f_{X_1}(x_1)}.
}
Then, we denote $u=\frac{x_1-\mu_1}{\sigma_1}$ and $v=\frac{x_1-\mu_2}{\sigma_2}$.

\spl{
    \mu_{X_2|_{x_1}}&=E[X_2|_{x_1}]=\int_{-\infty}^\infty x_2f_{X_2|_{x_1}}(x_2)\dd x_2\\
    &=\sigma_2\int_{-\infty}^\infty \frac{v\sigma_2+\mu_2}{\sqrt{2\pi}\sigma_2\sqrt{1-\varrho^2}}e^{-\frac{1}{2(1-\varrho^2)(u^2-2\varrho uv+v^2)}}e^{-\frac{u^2}{2}}\dd v\\
    &=\int_{-\infty}^\infty \frac{v\sigma_2+\mu_2}{\sqrt{2\pi}\sqrt{1-\varrho^2}}e^{-\frac{u^2}{2}-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}e^{\frac{u^2}{2}}\dd u\\
    &=\int_{-\infty}^\infty \frac{v\sigma_2+\mu_2}{\sqrt{2\pi}\sqrt{1-\varrho^2}}e^{-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}\dd u\\
    &=\sigma_2\int_{-\infty}^\infty \frac{v}{\sqrt{2\pi}\sqrt{1-\varrho^2}}e^{-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}\dd u+\mu_2\int_{-\infty}^\infty \frac{1}{\sqrt{2\pi}\sqrt{1-\varrho^2}}e^{-\frac{(v-\varrho u)^2}{2(1-\varrho^2)}}\dd u\\
    &=\sigma_2\varrho u+\mu_2\\
    &=\mu_2+\varrho\frac{\sigma_2}{\sigma_1}(x_1-\mu_1).
}
}

\section*{Exercise 3.8}
\enum{
\item
unit:10.

\begin{table}[H]
    \centering
    \begin{tabular}{c|l}
        Stem & Leaves\\\hline
        532 & 9 \\
        534 & 2 \\
        535 & 47 \\
        536 & 6 \\
        537 & 5678 \\
        538 & 12345778888 \\
        539 & 016999 \\
        540 & 11166677889 \\
        541 & 123666688 \\
        542 & 0011222357899 \\
        543 & 01111556 \\
        544 & 00012455678 \\
        545 & 233447899 \\
        546 & 23569 \\
        547 & 357 \\
        548 & 11257 \\
    \end{tabular}
\end{table}

We can approximately conclude that it follows a shape of normal distribution.

\item
We know that n=100, so that the number of categories is 8.

The data range is 5487-5329=158. The category length is $\lceil{158/8}\rceil=20$.

The lower bound is $5329-0.5=5328.5$.

{
\ttfamily Histogram[Data, {Min[cat], Max[cat], 20}, PlotRange -> {0, Automatic},\\
Frame -> {True, True, False, False}, \\
FrameTicks -> {{Automatic, None}, {cat, None}}, \\
FrameLabel -> {"Shear strength of spot welds", "Number of welds"}]
}

\begin{figure}[H]
    \centering
    \includegraphics[height=6.5cm]{images/382}
    \caption{Shear strength distribution of 100 sample welds}
\end{figure}

The histogram obtains a shape similar to normal distribution, conveying the same information as the stem-leaf display.

\item
\spl{
    &q_1=5399\quad q_2=5421.2\quad q_3=5445.5.\\
    &iqr=46.5,\quad f_1=5329.25,\quad f_3=5515.25.\\
    &a_1=5342,\quad a_3=5487.
}
\begin{figure}[H]
    \centering
    \includegraphics[height=6.5cm]{images/383}
    \caption{Boxplot of shear strength of 100 sample welds}
\end{figure}

Boxplot demonstrates the distribution more simply and clearly, stating the key points without specific data values. Stem and leaf diagram is more complicated but keeps all the details.
}

\section*{Exercise 3.9}
\enum{
\item
unit:1.
\begin{table}[h]
    \centering
    \begin{tabular}{c|l}
        Stem & Leaves\\\hline
        10&1\\
        22 & 588\\
        23 & 02234\\
        23 & 558\\
        24 & 023\\
    \end{tabular}
\end{table}
\item
Sample mean: 22.16.

Sample median: 23.35.

Sample standard deviation: 3.47.

\item
\begin{figure}[H]
    \centering
    \includegraphics[height=6.5cm]{images/393}
    \caption{Boxplot of temperature samples}
\end{figure}

\item
Sample mean: 23.39.

Sample median: 23.35.

Sample standard deviation: 0.54.

The sample mean is more accurate after we drop the outlier. The sample standard deviation is much less than before because the degree of dispersion is less.
}

\section*{Exercise 3.10}
\spl{
    L(\gamma)&=\prod_{i=1}^nf(X_i)=\prod_{i=1}^n(\gamma+1)x_i^\gamma\\
    &=(\gamma+1)^n(\prod_{i=1}^n x_i)^\gamma.
}

We take the logarithm,
\spl{
    \ln L(\gamma)&=n\ln (\gamma+1)+\gamma\ln (\prod_{i=1}^n x_i)\\
    &=n\ln (\gamma+1)+\gamma\sum_{i=1}^n \ln(x_i).
}

Thus,
\spl{
    \frac{\dd}{\dd \gamma}L(\gamma)&=\frac{n}{\gamma+1}+\sum_{i=1}^n \ln(x_i)=0.\\
    \gamma&=-\frac{1}{n\sum_{i=1}^n \ln(x_i)}-1.
}

\section*{Exercise 3.11}
\enum{
\item
We know that the density function is
\spl{
    f(x)=\binom{4}{x}p^x(1-p)^{4-x}, \quad x=0,1,2,3,4.
}

Thus,
\spl{
    L(p)=\bigg[\binom{4}{0}p^0(1-p)^4\bigg]^8\bigg[\binom{4}{1}p^1(1-p)^3\bigg]^6\bigg[\binom{4}{2}p^2(1-p)^2\bigg]=24p^8(1-p)^{52}.
}

We take the derivative,
\spl{
    \frac{\dd}{\dd p}L(p)&=24(8p^7(1-p)^{52}-p^8\cdot52(1-p)^51)=0,\\
    p&=\frac{2}{15}.
}

\item
The expectation of failed brake shoes is
\spl{
    E[X]=np=4\cdot\frac{2}{15}=\frac{8}{15}.
}

Hence the failure rate is
\spl{
    p=\frac{E[X]}{n}=\frac{2}{15}\approx0.133>0.1.
}

Hence, I will have some doubts concerning the use of this new material.
}


\subsection{Elementary matrices and {\rref}s}

Suppose an $m\times n$-matrix $A$ is row reduced to its {\rref}. By
tracking each row operation completed, this row reduction can be
performed through multiplication by elementary matrices. The following
theorem uses this fact.

\begin{theorem}{The form $R=UA$}{form-rua}
  Let $A$ be any $m\times n$-matrix and let $R$ be its {\rref}. Then
  there exists an invertible $m\times m$-matrix $U$ such that
  \begin{equation*}
    R=UA.
  \end{equation*}
  Specifically, $U$ can be computed as the product (from right to
  left) of the elementary matrices of all row operations used to
  convert $A$ to {\rref}.
\end{theorem}

\begin{example}{The form $R=UA$}{form-rua}
  Let
  \begin{equation*}
    A = \begin{mymatrix}{rr}
      0 & 1 \\
      1 & 0 \\
      2 & 0
    \end{mymatrix}.
  \end{equation*}
  Find the {\rref} of $A$ and write it in the form $R=UA$, where $U$
  is invertible.
\end{example}

\begin{solution}
  To find the {\rref} $R$, we row reduce $A$. For each step, we will
  record the appropriate elementary matrix. First, switch rows $1$
  and $2$.
  \begin{equation*}
    \begin{mymatrix}{rr}
      0 & 1 \\
      1 & 0 \\
      2 & 0
    \end{mymatrix}
    \quad\stackrel{R_1\rowswap R_2}{\roweq}\quad
    \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      2 & 0
    \end{mymatrix}.
  \end{equation*}
  The corresponding elementary matrix is
  $E_1 = \begin{mymatrix}{rrr}
    0 & 1 & 0 \\
    1 & 0 & 0 \\
    0 & 0 & 1
  \end{mymatrix}$, i.e.,
  \begin{equation*}
    \begin{mymatrix}{rrr}
      0 & 1 & 0 \\
      1 & 0 & 0 \\
      0 & 0 & 1
    \end{mymatrix}
    \begin{mymatrix}{rr}
      0 & 1 \\
      1 & 0 \\
      2 & 0
    \end{mymatrix}
    ~=~\begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      2 & 0
    \end{mymatrix}.
  \end{equation*}
  Next, subtract $2$ times the first row from the third row.
  \begin{equation*}
    \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      2 & 0
    \end{mymatrix}
    \quad\stackrel{R_3\rowop R_3-2R_1}{\roweq}\quad
    \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      0 & 0
    \end{mymatrix}.
  \end{equation*}
  The corresponding elementary matrix is
  $E_2 = \begin{mymatrix}{rrr}
    1  & 0 & 0 \\
    0  & 1 & 0 \\
    -2 & 0 & 1
  \end{mymatrix}$, i.e.,
  \begin{equation*}
    \begin{mymatrix}{rrr}
    1  & 0 & 0 \\
    0  & 1 & 0 \\
    -2 & 0 & 1
    \end{mymatrix}
    \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      2 & 0
    \end{mymatrix}
    ~=~\begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      0 & 0
    \end{mymatrix}.
  \end{equation*}
  Notice that the resulting matrix is $R$, the required {\rref} of
  $A$. We can then write
  \begin{eqnarray*}
    R &=& E_2E_1A \\
      &=& U A.
  \end{eqnarray*}
  It remains to compute $U$:
  \begin{equation*}
    U ~=~ E_2E_1 ~=~
    \begin{mymatrix}{rrr}
      1 & 0 & 0 \\
      0 & 1 & 0 \\
      -2 & 0 & 1
    \end{mymatrix}
    \begin{mymatrix}{rrr}
      0 & 1 & 0 \\
      1 & 0 & 0 \\
      0 & 0 & 1
    \end{mymatrix} \\
    ~=~ \begin{mymatrix}{rrr}
      0 & 1 & 0 \\
      1 & 0 & 0 \\
      0 & -2  & 1
    \end{mymatrix}.
  \end{equation*}
  We can verify that $R = UA$ holds for this matrix $U$:
  \begin{equation*}
    UA ~=~ \begin{mymatrix}{rrr}
      0 & 1 & 0 \\
      1 & 0 & 0 \\
      0 & -2  & 1
    \end{mymatrix}
    \begin{mymatrix}{rr}
      0 & 1 \\
      1 & 0 \\
      2 & 0
    \end{mymatrix} \\
    ~=~ \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
      0 & 0
    \end{mymatrix} \\
    ~=~ R.
  \end{equation*}
\end{solution}

While the process used in the above example is reliable and simple
when only a few row operations are used, it becomes cumbersome in a
case where many row operations are needed to carry $A$ to $R$. The
following theorem provides an alternate way to find the matrix $U$.

\begin{theorem}{Finding the matrix $U$}{finding-u}
  Let $A$ be an $m\times n$-matrix and let $R$ be its {\rref}. Then
  $R = UA$, where $U$ is an invertible $m \times m$-matrix found by
  forming the augmented matrix $\mat{A\mid I}$ and row reducing to
  $\mat{R\mid U}$.
\end{theorem}

Let's revisit the above example using the process outlined in
Theorem~\ref{thm:finding-u}.

\begin{example}{The form $R=UA$, revisited}{form-rua-revisited}
  Let $A = \begin{mymatrix}{rr}
    0 & 1 \\
    1 & 0 \\
    2 & 0
  \end{mymatrix}$. Use the process of Theorem~\ref{thm:finding-u} to
  find $U$ such that $R=UA$.
\end{example}

\begin{solution}
  First, we set up the augmented matrix $\mat{A\mid I}$:
  \begin{equation*}
    \begin{mymatrix}{rr|rrr}
      0 & 1 & 1 & 0 & 0 \\
      1 & 0 & 0 & 1 & 0 \\
      2 & 0 & 0 & 0 & 1
    \end{mymatrix}.
  \end{equation*}
  Now, we row reduce until the left-hand side is in {\rref}:
  \begin{eqnarray*}
    \begin{mymatrix}{rr|rrr}
      0 & 1 & 1 & 0 & 0 \\
      1 & 0 & 0 & 1 & 0 \\
      2 & 0 & 0 & 0 & 1
    \end{mymatrix}
        &\stackrel{R_1\rowswap R_2}{\roweq}
            &
              \begin{mymatrix}{rr|rrr}
                1 & 0 & 0 & 1 & 0 \\
                0 & 1 & 1 & 0 & 0 \\
                2 & 0 & 0 & 0 & 1
              \end{mymatrix} \\
        &\stackrel{R_3\rowop R_3-2R_1}{\roweq}
            &
              \begin{mymatrix}{rr|rrr}
                1 & 0 & 0 & 1 & 0 \\
                0 & 1 & 1 & 0 & 0 \\
                0 & 0 & 0 & -2 & 1
              \end{mymatrix}.
  \end{eqnarray*}
  The left-hand side of this augmented matrix is $R$, and the
  right-hand side is $U$. Comparing this to the matrices $R$ and $U$
  we found in Example~\ref{exa:form-rua}, we see that the same
  matrices are obtained regardless of which process is used.
\end{solution}


%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
\section{The bi-overturner class of problems}
The bi-overturner problems basically consist of two \textit{overturner} circulations models side-by-side, hence the name. However, although the overturner circulation was initially developed as an idealization of the meridional circulation in the Atlantic ocean, bi-overturner problems do not model any "real-life" problem. Therefore, the values of the different physical parameters are given without justification, although most of the quantities are inspired from the values proposed in \cite{timmermans2006masterthesis}. The bi-overturner problems are really used as a mathematical tool to test the method, and using the overturner circulation ensures that the velocity field that we consider satisfies the continuity equation and no-through boundary condition everywhere on $\partial \Omega$. The domain that we consider is $\Omega = [-L,\,L]\times[0,\,H]$. Let $\Omega^- = [-L,\,0[\times[0,\,H]$ and $\Omega^+ =\; ]0,\,L]\times[0,\,H]$. If $\psi(y,z;y_0,z_0)$ denote the streamfunction defined in~\eqref{eq:psi_overturner} with parameters $y_0 \in\; ]0,\,L[$ and $z_0 \in\; ]0,\,H[$, then the streamfunction $\varphi$ of the bi-overturner problems is defined as
\begin{equation} \label{eq:psi_2box}
	\varphi(y,z) = \left\{
		\begin{array}{lrr}
			\psi(L+y,z;y_0,z_0) & \mbox{if} & (y,z) \in \Omega^-,\\
			0 & \mbox{if} & (y,z) \in \{(0,z)\;|\;z \in [0,\,H]\},\\
			-\psi(y,z;L-y_0,z_0) & \mbox{if} & (y,z) \in \Omega^+,\\
		\end{array}
	\right.
\end{equation}
The streamfunction $\varphi$ has two extrema of equal strengths: a maximum at $(y_0^-,z_0)$ with $y_0^- := -L+y_0 = -y_0^-$ and a minimum at $(y_0^+,z_0)$ with $y_0^+ := L-y_0$. The overturner-like circulation is clockwise in $\Omega^-$ and counterclockwise in $\Omega^+$. The horizontal and vertical velocities $v$ and $w$ are given by
\begin{equation} \label{eq:u-psi_2box}
	v = -\frac{\partial \varphi}{\partial z}, \quad w = \frac{\partial  \varphi}{\partial y}.
\end{equation}
 Isolines are shown in figures~\ref{fig:v2box} and~\ref{fig:w2box} respectively. The key feature is that $v(0,z) = 0$, namely the horizontal velocity is zero on the whole segment $y = 0$. Hence, if there is no horizontal diffusion, a passive tracer's particle starting in $\Omega^-$ can never reach $\Omega^+$ and conversely. In that case, we can imagine that there is a vertical wall implying no-through boundary conditions at $y=0$ and the graph is not ergodic. But if the horizontal diffusivity is nonzero in some area near $y=0$, then exchange of particles between $\Omega^-$ and $\Omega^+$ can happen in that area. Now we suppose that the diffusivity tensor $\b K$ is diagonal:
 \begin{equation}
 	\b K(y,z) = \begin{pmatrix} K_{yy}(y,z) & 0\\ 0 & K_{zz} \end{pmatrix}.
 \end{equation}
We assume that the vertical diffusivity $K_{zz}$ is constant and equal to $10^{-3}$ [$\rm{m^2/s}$]. Now we introduce the parameter $\alpha \in [0,1]$ and define $z^* = \alpha H$. We choose an horizontal diffusivity $K_{yy}$ of the form
\begin{equation} \label{eq:Kh2box}
	K_{yy}(y,z) = \begin{cases}
			10^4\ \rm{[m^2/s]} & \mbox{if} \quad y_0^- \le y \le y_0^+ \quad \mbox{and} \quad z^* \le z \le H,\\
			10^3\ \rm{[m^2/s]} & \mbox{if} \quad -L \le y < y_0^- \quad \mbox{or} \quad y_0^+ < y \le L,\\
			0\ \rm{[m^2/s]}  & \mbox{otherwise}.
		\end{cases}
\end{equation}
Now, exchange between $\Omega^-$ and $\Omega^+$ is possible but only above $z^*$.\footnote{From a numerical, random-walk, point of view, we should also ensure that $y_0^+ = |y_0^-|$ is sufficiently large. If not, it would be numerically possible for particles lying below $z^*$ and before $y_0^-$ (resp. after $y_0^+$) to jump from $\Omega^-$ (resp. $\Omega^+$) to $\Omega^+$ (resp. $\Omega^-$).} Hence, we can imagine that there is a vertical, no-through wall of height $z^*$ at $y=0$. The situation is depicted on figure~\ref{fig:Kh2box}. For the next, we call \textit{exchange zone} the area where $K_{zz} = 10^4$ $\rm{[m^2/s]}$ (dark gray zone in figure~\ref{fig:Kh2box}). Making $\alpha$ vary between $0$ and $1$ defines a class of bi-overturner problem where the vertical wall's height $z^*$ at $y=0$ vary between $0$ and $H$. Two examples of trajectories with the same initial condition are shown for $\alpha = 0.75$ in figures~\ref{fig:withouttransfer} and~\ref{fig:withtransfer}.

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/v2box_timmermans.eps}
	\caption{Isolines of the horizontal velocity $v$ for bi-overturner problems.}
	\label{fig:v2box}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/w2box_timmermans.eps}
	\caption{Isolines of the vertical velocity $w$ for bi-overturner problems.}
	\label{fig:w2box}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/problem.eps}
	\caption{Illustration of the decomposition of the domain into grid cells corresponding to the nodes of the directed graph. The values of $K_{yy}$ are also shown for $\alpha = 0.6$, and the fictitious wall is represented by the black continuous line.}
	\label{fig:Kh2box}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/traj_without_transfer5.eps}
	\caption{Example of a particle trajectory in the bi-overturner model with $\alpha = 0.75$. The black cross represents the initial position whereas the black dot shows the final position. The simulation time is 200 years. Here the particle enters the exchange zone but finally stays in $\Omega^-$.}
	\label{fig:withouttransfer}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/traj_with_transfer2.eps}
	\caption{Example of a particle trajectory in the bi-overturner model with $\alpha = 0.75$. The black cross represents the initial position whereas the black dot shows the final position. The simulation time is 200 years. Here the particle enters the exchange zone and finally goes in $\Omega^+$.}
	\label{fig:withtransfer}
\end{figure}
When $\alpha = 1$, the obvious compartmental model is made of the two compartments $\Omega^-$ and $\Omega^+$ which do not communicate with each other. Suppose that different amounts of passive tracer are released into $\Omega^-$ and $\Omega^+$ at a given time; the concentration in each compartment tends to become uniform in time due to diffusion, but the concentration in $\Omega^-$ depends only on the initial quantity of tracer released in $\Omega^-$ and similarly for the concentration in $\Omega^+$. At the contrary, when $\alpha = 0$ the concentration tends to become uniform over the whole domain when time goes to infinity. Hence, we can expect three types of partitioning to be dominant at different time scales: a three-communities partitioning with left and right compartments and an intermediate zone in between; a two-communities partitioning corresponding to $\Omega^-$ and $\Omega^+$; and finally a trivial partitioning with one single community for very long time scales. For intermediate values of $\alpha$, we expect a behavior similar to the case $\alpha = 0$ when $\alpha$ is close to $0$. When $\alpha$ is close to $1$ but still different from $1$, exchange can still happen between $\Omega^-$ and $\Omega^+$. However, in terms of communities, we expect the two-community partitioning to be dominant for long time scales.

\section{Application of the stability method} \label{sec:applicationstability}
The partitioning results are presented here for $\alpha = 1$, $\alpha = 0.75$, $\alpha = 0.5$, $\alpha = 0.25$ and $\alpha = 0$. For every value of $\alpha$, the transition probability matrix is computed at $T = 1$ year on a discretization like the one presented in figure~\ref{fig:Kh2box}, namely with $\nby = 30$ and $\nbz = 10$. $P_0 = 10\,000$ particles are initially released in every grid cell. The stability software is run using a vector of Markov times taking values between $10$ and $10^3$. Since we have computed the transition probability matrix for $T = 1$ year, one unit of Markov time correspond here to one year. Notice that in the case where $\alpha = 1$, the graph is not ergodic (it is composed of two ergodic classes) and a random teleportation probability \mtlb{tau} $= 10^{-3}$ is used when running the stability software. When $\alpha < 1$, \mtlb{tau} $=0$ is used.
The stability, number of communities and variation of information curves are shown in figures~\ref{fig:staba1}, \ref{fig:staba75}, \ref{fig:staba5}, \ref{fig:staba25} and~\ref{fig:staba0} for the different values of $\alpha$. Most robust communities correspond to plateaux in the community curve with a corresponding low variation of information. Whatever the value of $\alpha$, the number of communities goes to $2$ after a given time and the corresponding variation of information is almost zero. This happens around $50$ years when $\alpha > 0.25$, and a bit later for $\alpha = 0.25$ and $\alpha = 0$. In every case, the two-communities partitioning corresponds as expected to $\Omega^-$ and $\Omega^+$. It is shown in figure~\ref{fig:cluster_a75_2} for the case $\alpha = 0.75$. Exactly the same two-communities clusterings are found in the cases $\alpha = 0.25$ and $\alpha = 1$. In those cases, the boundary is perfectly straight: this corresponds to the intuition and it is conform to the remark made on page \pageref{remark:straightboundaries}. When $\alpha = 0$ or $\alpha = 0.5$, the boundary is not exactly a straight line but almost. The situation is depicted in figure~\ref{fig:cluster_a5_2} for $\alpha = 0.5$. However, we have to remember that neither the transition probability matrix nor the stability partitioning is solved exactly. Hence, we can consider that the irregularity in the boundary of the communities is due to those numerical artifacts: if a box model has to be build from the partitioning shown in figure~\ref{fig:cluster_a5_2}, the compartments should of course be chosen with a vertical boundary. This illustrates the fact that when using a community-detection algorithm to build compartments for a box model, the communities should not be blindly interpreted as being the relevant compartments. In particular, if the boundaries of the communities are almost but not exactly vertical and horizontal, one should consider straight boundaries for the compartments. Community detection should thus be considered as a guide towards choosing relevant compartments, rather than as a method providing the exact perfect compartments.

When $\alpha = 0.75$, a small three-communities plateau starts appearing around 40 years, just before the two-communities plateau. This plateau grows as $\alpha$ decreases. The corresponding clusterings are shown in figure~\ref{fig:cluster_a25_3} for $\alpha = 0.25$ and in figure~\ref{fig:cluster_a0_3} for $\alpha = 0$. A central intermediate community appears, as expected.

In figure~\ref{fig:staba0} (stability, number of communities and $\VI$ for the case $\alpha = 0$), peaks corresponding to oscillations between two and three communities are observed around 300 and 400 years. As stated in chapter~\ref{chap:clustering}, the number of communities should decrease with time, and those oscillations are thus due to the fact that the stability partitioning is only solved approximately. However, this shows that the two- and three-communities clusterings have similar stabilities at those times. Finally, remember that we expected to find a single-community partitioning for very long Markov times in the case $\alpha = 0$, which does not appear here. Such a clustering would probably appear if we run the stability software for longer Markov times.

% Hopefully this introductory example shows how a community-detection algorithm could be use to build compartment models, and provide intuition about why it could work. The communities found depend on the time scale considered but this is not a problem since it could also be the case for the compartments. Notice that we do not to build compartmental models for the bi-overturner class of problems because the compartments are obvious in this case, and it is thus not the goal of this section. In the next section, the method is applied on the overturner problem and we should try to build a compartmental model for that problem.

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box/stab_a1.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 1$.}
	\label{fig:staba1}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box/stab_a75.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 0.75$.}
	\label{fig:staba75}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box/stab_a5.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 0.5$.}
	\label{fig:staba5}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box/stab_a25.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 0.25$.}
	\label{fig:staba25}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box/stab_a0.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 0$.}
	\label{fig:staba0}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth]{fig/problem2box/cluster_a5_2_.eps}
	\caption{Illustration of the two-communities partitioning for $\alpha = 0.5$.}
	\label{fig:cluster_a5_2}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth]{fig/problem2box/cluster_a75_2_.eps}
	\caption{Illustration of the two-communities partitioning for $\alpha = 0.75$. Exactly the same two-communities clustering is found for $\alpha = 0.25$, $\alpha = 0.9$ and $\alpha = 1$, but in this figure the wall is represented to have height $z^* = 0.75H$.}
	\label{fig:cluster_a75_2}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth]{fig/problem2box/cluster_a25_3_.eps}
	\caption{Illustration of the three-communities partitioning for $\alpha = 0.25$.}
	\label{fig:cluster_a25_3}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth]{fig/problem2box/cluster_a0_3_.eps}
	\caption{Illustration of the three-communities partitioning for $\alpha = 0$.}
	\label{fig:cluster_a0_3}
\end{figure}

\newpage
\section{Building a compartment model}
Let us now focus on the case $\alpha = 0.75$. The community detection method applied in the previous section suggests a two compartments model with $\Omega_1 = \Omega^-$ and $\Omega_2 = \Omega^+$, see figure~\ref{fig:cluster_a75_2}. This clustering appears around 50 years. In order to ensure that this clustering is indeed relevant at that time scale, we apply the second approach proposed in section~\ref{sec:timescales} for dealing with the time scales: the stability is computed on the transition probability matrix $\b M(50)$, numerically approximated under the same conditions as $\b M(1)$ in section~\ref{sec:applicationstability}. The stability method is applied for Markov times in the interval $[1,\,10]$: this corresponds to physical times between 50 and 500 years. The stability, community and variation of information curves are shown in figure~\ref{fig:staba75_T50}. A two-communities partitioning is indeed found in the range of Markov times $[1.028,\, 4.074]$. This corresponds to the range of physical times $[51.4,\, 203.7]$ years. The two communities are exactly the same as the ones obtained with $\b M(1)$ and shown in figure~\ref{fig:cluster_a75_2}, indicating a robust community structure.

\begin{figure}[!htp]
	\centering
	\includegraphics[width = .7\textwidth, height = .4\textheight]{fig/problem2box50/stab_a75.eps}
	\caption{Stability, number of communities and variation of information as a function of the Markov time for $\alpha = 0.75$. Those results are obtained from the transition probability matrix evaluated a $T = 50$ years.}
	\label{fig:staba75_T50}
\end{figure}

Now we are ready to build a two-compartment model based on the clustering from figure~\ref{fig:cluster_a75_2}. First, in section~\ref{subsec:limitations}, the limitations of such a two-compartment model are discussed \textit{a priori}. Then, two approaches are proposed in order to build a compartment model: in section~\ref{sec:ctcm}, a continuous-time compartment model is build which depends on one parameter, and the analytical solution to that compartment model is used together with a numerical simulation to compute a relevant value for that parameter; in section~\ref{sec:dtcm}, a discrete-time compartment model is build which also depends on one parameter, and a numerical simulation is used to estimate that parameter.

In this section, $\b c(t) = (C_1(t),C_2(t))$ denotes the vector of the average concentrations over the compartments:
\begin{equation}
	C_i(t) = \frac{1}{|\Omega_i|} \int_{\Omega_i} C(\b x,t) \rm d\Omega_i \quad \mbox{for } i = 1,2,
\end{equation}
where $C(\b x,t)$ denotes the concentration function. Notice that since we consider a passive tracer in an isolated domain, the mean concentration $\bar C(t)$ is constant and
\begin{equation}
	\bar C(t) = \bar C = \frac{|\Omega_1| C_1 + |\Omega_2| C_2}{|\Omega|} = \frac{C_1 + C_2}{2},
\end{equation}
where we have used the fact that $|\Omega_1| = |\Omega_2| = |\Omega|/2$. Hence, we can express $C_2$ as a linear function of $C_1$:
\begin{equation} \label{eq:C2-C1}
	C_2 = 2\bar C - C_1.
\end{equation}
In the next, it will thus be sufficient to analyze only the quantity $C_1$, since the error on $C_2$ is exactly the opposite of the error on $C_1$.

\subsection{Limitations of the compartment model} \label{subsec:limitations}
The main limitation of a compartment model is that it only "sees" the average concentration over compartments. In particular, for a same initial condition $C_1(0)$ to the compartment model, an infinity of tracer repartitions within $\Omega_1$ are possible. In this section, we illustrate three different initial repartitions of the particles leading to the same initial condition $C_1(0)$ and thus to the same function $C_1(t)$ in the compartment model.

A first possible initial repartition of the particles is when the tracer mass is uniformly distributed over $\Omega_1$. We denote that initial condition $C^1(0)$. The second case that we consider is when the particles are uniformly distributed over $[-L,-L/2]\times[0,H]$ and there is no particle in $]-L/2,0[\times[0,H]$; it is denoted $C^2(0)$. The last case is when all the tracer mass is concentrated on a single point, chosen here to be in the lower left corner of the domain (the precise location is $(-\frac{14}{15}L,\frac{1}{10}H)$); it is denoted $C^3(0)$. Those three possible initial repartitions of the particles, all leading to the same compartment initial condition $C_1(0)$ (and thus to the same $C_1(t)$ for all $t \ge 0$), are represented in figure~\ref{fig:CI_init}.

Let $C_1^i(t)$ denote the aggregated concentration over compartment 1 at time $t$ corresponding to the initial repartition of the particles $C^i(0)$ for $i = 1,2,3$. The evolutions of $C^1_1(t)$, $C^2_1(t)$ and $C^3_1(t)$ over 1000 years are shown in figure~\ref{fig:CI_evol}. The compartment model assumption is that the concentration is approximately uniform over the compartments. Hence, we shall expect the compartment model solution $C_1(t)$ corresponding to the initial condition $C_1(0) = C^1_1(0) = C^2_1(0) = C^3_1(0) = 2\bar C$ to be close to $C^1_1(t)$. As expected, $C_1^1(t)$ starts decreasing immediately, since there are already many particles in the exchange zone at $t=0$. In the two other cases, plateaux are observed. Their lengths correspond to the time for the particles to reach the exchange zone and then possibly enter $\Omega_2$. Obviously, this time is larger in case 3 than in case 2. In both cases, once particles have entered the exchange zone, their concentration in the exchange zone is larger than in case 1, allowing for a larger flux of particles towards $\Omega_2$. Hence, at the end of the plateau, the concentration decreases faster in case 2 than it does initially in case 1 and it decreases still faster in case 3 than in case 2. After 100 years, $C^1_1(t)$ and $C^2_1(t)$ are almost confounded but $C^3_1(t)$ stays clearly different until it reaches equilibrium after approximately 700 years.

The point is that those three cases could never be rendered exactly by a compartment model with two compartments since they correspond exactly to the same compartment solution. Hence, we shall not expect too much from our compartment model: a good compartment model should produce a curve that corresponds approximately to $C_1^1(t)$.
\begin{figure}[!htp]
	\centering
	\includegraphics[width = \textwidth]{fig/problem2box/CI_init.eps}
	\caption{Illustration of the three different initial repartitions of the tracer's particles.}
	\label{fig:CI_init}
\end{figure}

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/CI_1000years.eps}
	\caption{Comparison of the compartment model solution with the numerical solution for the initial condition $C_1(0) = 2\bar C$.}
	\label{fig:CI_evol}
\end{figure}

\subsection{Continuous-time compartment model} \label{sec:ctcm}
Let $\b A$ be the $2 \times 2$ interaction matrix, and let $\b c$ and $\bs \Omega$ be defined as in~\eqref{eq:def_compartment_vars}. The evolution of the concentration in the two compartments is given by
\begin{equation} \label{eq:generalEDOp2b}
 	\bs \Omega \b{\dot{c}} = \b A \b c.
\end{equation}
It remains to propose an expression for $\b A$. In the general case, a $2 \times 2$ matrix such as $\b A$ has four independent entries. However, properties~\ref{prop1bis_comp}, \ref{prop2bis_comp} and~\ref{prop3bis_comp} shown in chapter~\ref{chap:compartment} imply that $\b A$ has only one degree of freedom. Besides, for $a > 0$, $\b A$ must have the following form:
\begin{equation} \label{eq:A}
	\b A = \begin{pmatrix}
		-a & a\\
		a & -a
	\end{pmatrix}.
\end{equation}
This is nothing but a straightforward implication of the combination of the three properties. Another way of seeing this is by looking at the general form of $\b A$ shown in~\eqref{eq:generalA}. In this case, that expression reduces to (remember that $U_{i,i} = 0 = V_{i,i}$):
\begin{equation} \label{eq:Atemp}
	\b A = \begin{pmatrix}
		-\frac{1}{2} U_{1,2} - V_{1,2} & -\frac{1}{2}U_{1,2} + V_{1,2}\\
		-\frac{1}{2}U_{2,1} + V_{2,1} & -\frac{1}{2}U_{2,1} - V_{2,1}
	\end{pmatrix}.
\end{equation}
By the continuity equation for the compartment model~\eqref{eq:continuitycompartment}, $U_{1,2} = U_{2,1} = 0$, and by the property~\eqref{eq:Vprop} of $V_{i,j}$, $V_{1,2} = V_{2,1} > 0$. With those considerations,~\eqref{eq:Atemp} simplifies to
\begin{equation}
	\b A = \begin{pmatrix}
		- V_{1,2} & V_{1,2}\\
		V_{1,2} & -V_{1,2}
	\end{pmatrix},
\end{equation}
which is exactly~\eqref{eq:A} with $V_{1,2} = a$.

The compartment model considered here has in fact an analytic solution that is relatively easy to compute. Indeed, by~\eqref{eq:C2-C1}, we can express $C_2$ as a linear function of $C_1$ and~\eqref{eq:generalEDOp2b} reduces to a simple ODE in $C_1$ with one parameter $a$:
\begin{equation} \label{eq:edoC1_p2b_a75}
	\frac{dC_1}{dt} = -2aC_1 + 2a\bar C,
\end{equation}
where we have redefined $a$ as $V_{1,2}/|\Omega_1|$ in order to simplify the notations. Let $C_{1,0}$ be the initial condition on $C_1$, namely $C_1(0) = C_{1,0}$. The solution to~\eqref{eq:edoC1_p2b_a75} is easily computed as:
\begin{equation}
	C_1(t) = (C_{1,0} - \bar C) \Exp^{-2at} + \bar C.
\end{equation}
Let us consider the scaled form of the concentration $\tilde C_1 = C_1/\bar C$. The solution is expressed in terms of $\tilde C_1$ as:
\begin{equation}
	\tilde C_1(t) = (\tilde C_{1,0} - 1) \Exp^{-2at} + 1.
\end{equation}

The goal now is to estimate the parameter $a$. To this end, we propose the following approach:
\begin{enumerate}
	\item Run a simulation on a particular instance of the bi-overturner problem (i.e. a particular initial condition).
	\item Using that simulation, compute the average concentration in $\Omega_1$ at $m$ different times $t_0 < t_1 < \dots < t_m$. For the next, let $\bar C_1(t)$ denote the average concentration over $\Omega_1$ at time $t$ (whereas $C_1(t)$ stands for the compartment model concentration in the compartment corresponding to the subdomain $\Omega_1$).
	\item Apply the method of least squares to compute $a$:
	\begin{equation} \label{eq:a_minimization}
		a = \argmin_a \sum_{i=1}^m \left(C_1(t_i)-\bar C_1(t_i)\right)^2.
	\end{equation}
\end{enumerate}
This method leads to a value of $a$ based on one single particular instance of the problem (i.e. a particular initial condition). Of course, we will need to check that the results obtained for other initial conditions are sufficiently close to the simulations.

The method is applied for the initial condition $\tilde C_{1,0} = 2$ (and hence $\tilde C_2(0) = 0$), and with the tracer particles initially uniformly distributed over $\Omega_1$. The simulation is run for 1000 years, and $\bar C_1(t)$ is evaluated every year. The minimization of~\eqref{eq:a_minimization} is performed in \matlab using the Gauss-Newton algorithm, which is well-suited to solve non-linear least squares problems.\footnote{See for example the wikipedia page of the algorithm : \url{https://en.wikipedia.org/wiki/Gauss-Newton_algorithm}.} The value
\begin{equation}
	a = 0.003125
\end{equation}
is found. Figure~\ref{fig:comparison_comp-real1} compares the evolution of $\tilde C_1(t) = C_1(t)/\bar C$, the analytical solution of the compartment model, with the evolution of $\tilde C_1(t)/\bar C$, the scaled mean value of the concentration over $\Omega_1$ computed numerically, which we may consider as an approximation of the exact solution.

\textcolor{red}{todo : autres conditions initiales}

\subsection{Discrete-time compartment model} \label{sec:dtcm}
When there are more than two compartments, finding an analytic solution is not always possible and the methodology presented in the previous section cannot be applied. For this reason, another approach is proposed in this section.
\subsubsection{The method}
Here a \textit{discrete} interaction matrix $\b A_{\Delta t}$ is build based on a numerical simulation over a period $\Delta t$. This methodology is thus far more general than the one presented in the previous section. It goes as follows: to compute the entry $[\b A_{\Delta t}]_{i,j}$, run a simulation over a period $\Delta t$, with all the particles initially uniformly distributed over compartment $j$ (i.e. subdomain $\Omega_j$). Let $P_0$ be the total number of particles, and $P_{j \rightarrow i}(\Delta t)$ the number of particles amongst those initially in compartment $j$ that end up in compartment $i$ after a time $\Delta t$. The factor $\frac{P_{j \rightarrow i}(\Delta t)}{P_0}$ is an approximation of the probability to go from compartment $j$ to compartment $i$ within a time period $\Delta t$. But this probability is precisely given by $\frac{|\Omega_i|}{|\Omega_j|}[\b A_{\Delta t}]_{i,j}$, as explained page \pageref{page:probability_interpretation}. We have thus the approximation
\begin{equation} \label{eq:A_approx_discr}
	[\b A_{\Delta t}]_{i,j} \approx \frac{P_{j \rightarrow i}(\Delta t)}{P_0}\frac{|\Omega_j|}{|\Omega_i|}.
\end{equation}
Using that formula to compute an approximation of $\b A_{\Delta t}$, it is seen that properties~\ref{prop1_discr_comp}, \ref{prop2_discr_comp} and~\ref{prop5_discr_comp} are always respected \textit{a priori}. Properties~\ref{prop3_discr_comp} and~\ref{prop4_discr_comp} should by verified a posteriori.
\subsubsection{Results}
In a first instance, we can take advantage of the properties developed in section~\ref{sec:dtcm(chapcomp)} to deduce the general form of $\b A_{\Delta t}$ for this problem. In the present case, $\Omega_1$ and $\Omega_2$ have the same size; this implies that property~\ref{prop2_discr_comp} reduces to corollary~\ref{corollary2}, and that property~\ref{prop5_discr_comp} becomes equivalent to property~\ref{prop4_discr_comp}. Putting everything together, the matrix $\b A_{\Delta t}$ that we search is a $2 \times 2$ matrix whose entries are comprised between 0 and 1 and which satisfies $\b 1^\t \b A_{\Delta t} = \b 1^\t$ and $\b A_{\Delta t} \b 1= \b 1$. This implies that $\b A_{\Delta t}$ must have the following form:
\begin{equation} \label{eq:generalformAdeltat}
	\b A_{\Delta t} = \begin{pmatrix}
		a & 1-a\\ 1-a & a
	\end{pmatrix},
\end{equation}
for some $a \in [0,\,1]$.

The discrete interaction matrix is approximated for $\Delta t = 1$ year with $P_0 = 10\ 000$. \textcolor{red}{Mentionner l'étude de convergence, mais la rendre plus rigoureuse.} Using formula~\eqref{eq:A_approx_discr}, we get
\begin{equation}
	\b A_1 = \begin{pmatrix}
		0.9839 & 0.0159 \\
		0.0161 & 0.9841
	\end{pmatrix}.
\end{equation}
This approximation of $\b A_1$ does not exactly match the general form~\eqref{eq:generalformAdeltat}. Hence, we improve that approximation by choosing $a$ as an average:
\begin{equation}
	a = \frac{[\b A_1]_{1,1} + (1-[\b A_1]_{1,2}) + (1 - [\b A_1]_{2,1}) + [\b A_1]_{2,2}}{4} = 0.9840,
\end{equation}
leading to
\begin{equation}
	\b A_1^{discr,1} = \begin{pmatrix}
		0.9840 & 0.0160 \\
		0.0160 & 0.9840
	\end{pmatrix},
\end{equation}
which match the general form~\eqref{eq:generalformAdeltat}. For any initial condition, we can now approximate the concentration in both compartments after $T$ years as
\begin{equation}
	\b c^{discr,1}(T) = \left(\b A_1^{discr,1}\right)^T \b c(0).
\end{equation}
The resulting evolution of the concentration in compartment 1 is shown in figure~\ref{fig:comparison_comp-real1} for the initial condition $\b c(0) = (2\bar C,0)^\t$. Clearly, this result is not satisfying as the concentration decreases much too fast in compartment 1. Therefore, we propose another approach: recall from figure~\ref{fig:staba75} that the two-communities clustering is returned by the stability method for times larger than (approximately) 50 years. This suggests that computing the discrete interaction matrix $\b A_{50}$ could provide more interesting results. We get numerically
\begin{equation}
	\b A_{50} = \begin{pmatrix}
		0.8753 &   0.1206\\
	    0.1248  &  0.8795
	\end{pmatrix},
\end{equation}
which is corrected as
\begin{equation}
	\b A_{50} = \begin{pmatrix}
		0.87735 &   0.12265\\
	    0.12265  &  0.87735
	\end{pmatrix}.
\end{equation}
This discrete interaction matrix only allows to compute the concentration at times that are multiples of 50 years. We have the approximation
\begin{equation} \label{eq:cdiscr2}
	\b c^{discr,2}(T) = \left(\b A_{50}\right)^{\frac{T}{50}} \b c(0) \qquad \mbox{for } T = 0,50,100,\dots
\end{equation}
The resulting evolution of the concentration in compartment 1 is also shown in figure~\ref{fig:comparison_comp-real1}. The result is far better than with the first method. Notice that in this case, approximating $\b A_1$ as
\begin{equation}
	\b A_1^{discr,2} = \left(\b A_{50}\right)^{\frac{1}{50}},
\end{equation}
yields
\begin{equation}
	\b A_1^{discr,2} = \begin{pmatrix}
		0.9972 & 0.0028 \\
		0.0028 & 0.9972
	\end{pmatrix}.
\end{equation}
The resulting approximation of the concentration is the same as in equation~\eqref{eq:cdiscr2} but now we are able to compute the concentration in the compartments every year. Notice that this does not provide a general method to compute $\b A_1$ since we have no a priori guarantee that the entries of $(\b A_{\Delta t})^{\frac{1}{\Delta t}}$ are real.

This section presents a procedure to numerically estimate the discrete transition matrix $\b A_{\Delta t}$ for any time step $\Delta t$. However, the bad results obtained with our first estimation of $\b A_1$ show that the choice of $\Delta t$ is crucial. Let $[t_a,\, t_b]$ denote the time range at which the clustering chosen to delineate the subdomains is found by the stability method. The results of this section suggest that $\Delta t = t_a$ could be a satisfying choice. Unfortunately, no conclusion can be drawn based only on this simple example, and the method would need to be applied on more complicated problems.

\begin{figure}[!htp]
	\centering
	\includegraphics[width=\textwidth]{fig/problem2box/C1vsC1tilde1_1000years2.eps}
	\caption{Comparison of the different compartment model solutions with the numerical solution for the initial condition $C_1(0) = 2\bar C$.}
	\label{fig:comparison_comp-real1}
\end{figure}