\documentclass{article} % For LaTeX2e
\usepackage{iclr2017_conference,times}
\usepackage{hyperref}
\usepackage{url}

\usepackage{amsmath,graphicx}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

%\usepackage[cmex10]{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{hyperref}
% \usepackage[francais]{babel}
\usepackage[applemac]{inputenc}
\usepackage{color}

%\usepackage{graphicx}% Include figure files
%\usepackage{dcolumn}% Align table columns on decimal point
%\usepackage{bm}% bold math
%\usepackage{color}
%\usepackage[caption=false]{subfig}

\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=python,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\newcommand {\lb} {{\langle}}
\newcommand {\rb} {{\rangle}}
\newcommand {\R} {{\mathbb{R}}}
\newcommand {\X} {{\tilde X}}
\newcommand {\w} {{\tilde w}}
\newcommand {\hh} {{k}}
\newcommand {\p} {{\tilde p}}
\newcommand {\RN} {{{\mathbb{R}}^N}}
\newcommand {\Z} {{\mathbb{Z}}}
\newcommand {\E} {{\mathbb{E}}}
\newcommand {\W} {{\cal{W}}}
\newcommand {\Fem} {F_{e}}
\newcommand {\Forr} {F_{o}}
\newcommand{\Exp}[1]{\mathbb{E}\left(#1 \right)}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}

\newcommand{\figref}[1]{Fig. \ref{#1}}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

\title{Topology and Geometry of Deep Rectified Network Optimization Landscapes }

\author{C. Daniel Freeman \thanks{ See our github page at PAGEGOHERE} \\
Department of Physics\\
University of California at Berkeley\\
Berkeley, CA 94720, USA \\
\texttt{daniel.freeman@berkeley.edu} \\
\And
Joan Bruna \\
Courant Institute of Mathematical Sciences \\
New York University \\
New York, NY 10011, USA \\
\texttt{bruna@cims.nyu.edu} \\
}

% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to \LaTeX{} to determine where to break
% the lines. Using \AND forces a linebreak at that point. So, if \LaTeX{}
% puts 3 of 4 authors names on the first line, and the last on the second
% line, try using \AND instead of \And before the third author name.

\newcommand{\fix}{\marginpar{FIX}}
\newcommand{\new}{\marginpar{NEW}}

%\iclrfinalcopy % Uncomment for camera-ready version

\begin{document}

\maketitle

\begin{abstract}
The loss surface of deep neural networks has recently attracted interest
in the optimization and machine learning communities as a prime example of
high-dimensional non-convex problem. Some insights were recently gained using spin glass
models, but at the expense of strongly simplifying the nonlinear nature of the model.

In this work, we do not make any such assumption and study conditions
on the data distribution and model architecture that prevent the existence
of bad local minima. Our theoretical work quantifies and formalizes two
important qualitative facts: (i) the landscape of deep linear networks is radically different
from that of deep half-rectified ones, thus implying that mean-field approximations
are unable to capture essential nonlinear behavior, and (ii) that the energy landscape
in the non-linear case is fundamentally controlled by the interplay between the smoothness of the data distribution and model over-parametrization.
These results are in accordance with empirical practice and recent literature.
%Together with
%recent results that rigorously establish that no gradient descent can
%get stuck on saddle points, we conclude that gradient descent converges
%to a global optimum in deep rectified networks.

The conditioning of gradient descent is the next challenge we address.
We study this question by estimating the geometry of level sets, and we introduce
an algorithm to estimate the regularity of such sets on large-scale networks.
Our empirical results show that these level sets remain connected throughout
all the learning phase, but they become exponentially more curvy
as the energy level decays, in accordance to what is observed in practice with
very low curvature attractors.
\end{abstract}

%\tableofcontents

%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
\label{sec:Intro}
%%%%%%%%%%%%%%%%%%%%%%

\begin{itemize}
\item Context of the problem
\item Related work: Spin glass, recent results from Shamir. Gradient Descent converges to minimizers (Jordan Recht et al).
\item Topology of the level sets. Main result on connectedness of level sets.
\item Geometry of the level sets. Algorithm to estimate the geodesics along level sets. Measure of curvature of these sets.
\end{itemize}

\section{Topology of Level Sets}

Let $P$ be a probability measure on a product space $\mathcal{X} \times \mathcal{Y}$,
where we assume $\mathcal{X}$ and $\mathcal{Y}$ are Euclidean vector spaces for simplicity.
Let $\{ (x_i, y_i)\}_i$ be an iid sample of size $L$ drawn from $P$ defining the training set.
We consider the classic empirical risk minimization of the form
\begin{equation}
\label{emp_risk_min}
\Fem(\theta) = \frac{1}{L} \sum_{l=1}^L \| \Phi(x_i;\theta) - y_i \|^2~,
\end{equation}
where $\Phi(x ; \theta)$ encapsulates the feature representation
that uses parameters $\theta \in \R^S$. In a deep neural network, this parameter
contains the weights and biases used in all layers.
For convenience, in our analysis we will also use the oracle risk minimization:
\begin{equation}
\label{risk_min}
\Forr(\theta) = \E_{(X,Y) \sim P} \| \Phi(X;\theta) - Y \|^2~,
\end{equation}

We define the level set of $F(\theta)$ as
\begin{equation}
\Omega_F(\lambda) = \{ \theta \in \R^S~;~F(\theta) \leq \lambda \}~.
\end{equation}

The first question we study is the structure of critical points of $\Fem(\theta)$ and $\Forr(\theta)$
when $\Phi$ is a multilayer neural network. In particular, we are interested to know whether
$\Fem$ has local minima which are not global minima. This question is answered by
knowing whether $\Omega_F(\lambda)$ is connected at each energy level $\lambda$:

\begin{proposition}
If $\Omega_F(\lambda)$ is connected for all $\lambda$ then every local minima of $F(\theta)$ is a global minima.
\end{proposition}
{\it Proof:} Suppose that $\theta_1$ is a local minima and $\theta_2$ is a global minima,
but $F(\theta_1) > F(\theta_2)$. If $\lambda = F(\theta_1)$, then clearly
$\theta_1$ and $\theta_2$ both belong to $\Omega_F(\lambda)$. Suppose
now that $\Omega_F(\lambda)$
is connected. Then we could find a smooth (i.e. continuous and differentiable) path $\gamma(t)$
with $\gamma(0) = \theta_1$, $\gamma(1)= \theta_2$ and $F(\gamma(t)) \leq \lambda = F(\theta_1)$.
In particular, as $t \to 0$, we have
\begin{eqnarray*}
F(\gamma(t)) &=& F(\theta_1) + t \langle \nabla F(\theta_1) , \dot{\gamma}(0) \rangle + \frac{t^2}{2} \left(\dot{\gamma}(0)^T H F(\theta_1) \dot{\gamma}(0) + \langle \nabla F(\theta_1), \ddot{\gamma}(0) \rangle \right) + o(t^2) \\
&=& F(\theta_1) +   \frac{t^2}{2}  \dot{\gamma}(0)^T H F(\theta_1) \dot{\gamma}(0)  + o(t^2) ~,
\end{eqnarray*}
which shows that $F(\gamma(t)) \leq F(\theta_1)$ for all $t$ is incompatible with $H(\theta_1) \succeq 0$. $\square$

\subsection{The Linear Case}

A particularly simple but insightful case is
when $F$ is a multilayer network defined by
\begin{equation}
\label{linearcase}
\Phi(x;\theta) = W_K \dots W_1 x~,~\theta = (W_1, \dots, W_K)~.
\end{equation}
This model defines a non-convex (and non-concave) loss $\Fem(\theta)$.
It has been shown in \cite{ganguli} and \cite{linearcase} (concurrently with our work) that in this case,
every local minima is a global minima.
For completeness, we provide here an alternative proof of that result.

%For that purpose, let $W_1, W_2, \dots, W_K$ be weight matrices of sizes
%$n_k \times n_{k+1}$, $k < K$. Assume first that $n_j \geq \min(n_1, n_K)$ for $j=2 \dots K-1$.
%and let us define the following multilinear regression problem:
%\begin{equation}
%\label{multilinloss}
%L_0(W_1, \dots, W_K) = \sum_i \| W_K, \dots W_1 x_i - y_i \|^2~,
%\end{equation}
%where $\{ (x_i, y_i)\,; x_i \in \mathbb{R}^{n_1}, y_i \in \mathbb{R}^{n_K} \}_i$ is a given
%training set.

We have the following result.
\begin{proposition}
\label{proplinear}
Let $W_1, W_2, \dots, W_K$ be weight matrices of sizes
$n_k \times n_{k+1}$, $k < K$, and let $\Fem(\theta)$, $\Forr(\theta)$
denote the risk minimizations using $\Phi$ as in (\ref{linearcase}).
Assume that $n_j \geq \min(n_1, n_K)$ for $j=2 \dots K-1$ [TODO I think this is not necessary].
Then $\Omega_{\Fem}(\lambda)$ is connected for all $\lambda$, as well as $\Omega_{\Forr}$,
and therefore there are no poor local minima.
\end{proposition}
{\it Proof:} We proceed by induction over the number of layers $K$.
For $K=1$, the loss $F(\theta)$ is convex. Let  $\theta_1$, $\theta_2$ be two arbitrary points
in a level set $\Omega_\lambda$. Thus $L(\theta_1) \leq \lambda$ and $L(\theta_2) \leq \lambda$. We have
$$L( t \theta_1 + (1-t) \theta_2) \leq t L(\theta_1) + (1-t) L(\theta_2) \leq \lambda~,$$
and thus a linear path is sufficient in that case to connect $\theta_1$ and $\theta_2$.

Suppose the result is true for $K-1$. Let $\theta_1 = (W_1^1, \dots, W^1_K)$ and
 $\theta_2 = (W_1^2, \dots, W^2_K)$ with $L(\theta_1) \leq \lambda$, $L(\theta_2) \leq \lambda$.
For each $W_1, \dots, W_K$, we denote $\tilde{W}_j = W_j$ for $j < K-1$ and
$\tilde{W}_{K-1} = W_K W_{K-1}$. By induction hypothesis, the
loss expressed in terms of $\tilde{\theta} = (\tilde{W}_1, \dots, \tilde{W}_{K-1})$ is connected
between $\tilde{\theta}_1$ and $\tilde{\theta_2}$. Let $\tilde{W}_{K-1}(t)$ the corresponding
path projected in the last layer. We just need to produce a path in the variables $W_{K-1}(t)$, $W_K(t)$
such that (i) $W_{K-1}(0) = W_{K-1}^1$, $W_{K-1}(1) = W_{K-1}^2$,
(ii) $W_{K}(0) = W_{K}^1$, $W_{K}(1) = W_{K}^2$, and
(iii) $W_{K}(t) W_{K-1}(t) = \tilde{W}_{K-1}(t)$ for $t \in (0,1)$.
We construct it as follows. Let
$$W_{K}(t) = t W_{K}^2 + (1-t) W_{K}^1 + t (1-t) V~,$$
$$W_{K-1}(t) = W_{K}(t)^\dagger \tilde{W}_{K-1}(t)  ~,$$
where $W_{K}(t)^\dagger = ( W_{K}(t)^T W_{K}(t))^{-1} W_{K}(t)^T$ denotes the pseudoinverse
and $V$ is a $n_{K-1} \times n_{K}$ matrix drawn from a iid distribution.
Conditions (i) and (ii) are immediate from the definition, and condition (iii) results from the fact that
$$W_{K}(t) W_{K}(t)^\dagger  = {\bf I}_{N_K}~,$$
since $W_K(t)$ has full rank for all $t \in (0,1)$.
$\square$.

\subsection{Half-Rectified Nonlinear Case}

We now study the setting given by
\begin{equation}
\label{relucase}
\Phi(x;\theta) = W_K \rho W_{K-1} \rho \dots \rho W_1 x~,~\theta = (W_1, \dots, W_K)~,
\end{equation}
where $\rho(z) = \max(0 ,z)$.
The biases can be implemented by replacing the input vector $x$
with $\overline{x}=(x, 1)$ and by rebranding each parameter matrix as
$$\overline{W}_i = \left(
\begin{array}{c|c}
W_i & b_i \\
\hline
0 & 1
\end{array}
\right)~,$$
where $b_i$ contains the biases for each layer.
For simplicity, we continue to use $W_i$ and $x$ in the following.

One may wonder whether the same phenomena of global connectedness also holds
in the half-rectified case. A simple motivating counterexample shows that this is not the case in
general. Consider a simple setup with $X \in \R^2$ drawn from a mixture of two Gaussians $\mathcal{N}_{-1}$
and $\mathcal{N}_{1}$, and let $Y = (X-\mu_Z) \cdot Z $ , where $Z$ is the (hidden) mixture component taking $\{1,-1\}$ values.  Let
$\hat{Y} = \Phi(X; \{ W_1, W_2\} )$ be a single-hidden layer ReLU network, with two hidden units,
illustrated in Figure ??.
%Since the model is homogeneous, one can think about $W_1$ as encoding two unitary vectors without loss of generality.
Let $\theta^A$ be a configuration that bisects the two mixture components,
and let $\theta^B$ the same configuration, but swapping the bisectrices.
One can verify that they can both achieve arbitrarily small risk by letting the covariance of the mixture components go to $0$.
However, any path that connects $\theta^A$ to $\theta^B$
must necessarily pass through a point in which $W_1$ has rank $1$, which leads to an estimator with risk at least $1/2$.

In fact, it is easy to see that this counter-example can be extended to any generic half-recitifed architecture, if one is
allowed to adversarially design a data distribution. For any given $\Phi(X; \theta)$ with arbitrary architecture and current parameters
$\theta = (W_i)$, let $\mathcal{P}_\theta=\{ \mathcal{A}_1, \dots, \mathcal{A}_S\}$ be the underlying tesselation of the input space given by our current choice of parameters; that is, $\Phi(X; \theta)$ is piece-wise linear and $\mathcal{P}_\theta$ contains those pieces. Now let
$X$ be any arbitrary distribution with density $p(x) > 0$ for all $x \in \R^n$, for example a Gaussian, and let %$Y ~|~ \{X \in \mathcal{A}_s\} = s$.
%It is the indicator function corresponding to the tesselation $\mathcal{P}$.
$Y ~|~X = \Phi(X ; \theta)$~. Since $\Phi$ is invariant under permutations $\theta_\sigma$ of its hidden layers, it is easy to see that one can find two parameter values $\theta_A = \theta$ and $\theta_B = \theta_\sigma$ such that $\Forr(\theta_A) = \Forr(\theta_B) = 0$, but any continuous path $\gamma(t)$ from $\theta_A$ to $\theta_B$ will have a different tesselation and therefore won't satisfy $\Forr( \gamma(t) ) = 0$.

This illustrates an intrinsic difficulty in the optimization landscape if one is after \emph{universal}
guarantees that do not depend upon the data distribution. This difficulty is non-existent in the linear case
and not easy to exploit in mean-field approaches such as \cite{choromaska}, but
is easily detected as soon as one considers a non-linear model, and shows that in general
we should not expect to obtain connected level sets. However,
connectedness can be recovered if one is willing to accept a small increase
of energy. Our main result shows that the amount by which the energy is
allowed to increase is upper bounded by a quantity that trades-off model overparametrization
and smoothness in the data distribution.

For that purpose, we start with a characterization of the oracle loss, and for simplicity let us assume
$Y \in \R$.
Let us first describe the case with a single hidden layer of size $m$, and let
\begin{equation}
\label{bla2}
e(m) = \min_{W_1 \in \R^{m \times n}, W_2 \in \R^m} \E\{ | \Phi(X; \theta) - Y|^2 \}~.
\end{equation}
It is a well known result by Hornik and Cybenko that a single hidden layer
is a universal approximator under very mild assumptions, i.e. $\lim_{m \to \infty} e(m) = 0$.
This result merely states that our statistical setup is consistent. A more interesting
question is the rate at which $e(m)$ decays, which clearly depends
on the smoothness of the joint density $(X, Y) \sim P$.

Our main result considers now a non-asymptotic scenario given by some
size $M$ of the hidden layer. Given two parameter values $\theta^A = (W_1^A, W_2^A) \in \mathcal{W}$
and $\theta^B= (W_1^B, W_2^B)$ with $\Forr(\theta^{\{A,B\} } ) \leq \lambda$,
we show that there exists a continuous path
$\gamma: [0,1] \to \mathcal{W}$ connecting $\theta^A$ and $\theta^B$
such that its risk is uniformly bounded by $\lambda + \epsilon$, where $\epsilon$
decreases with model overparametrization and with the decay rate of $e(m)$.
\begin{theorem}

\end{theorem}

%\begin{theorem}
%Let $W_1, W_2, \dots, W_K$ be weight matrices of sizes
%$n_k \times n_{k+1}$, $k < K$, and let $\Fem(\theta)$, $\Forr(\theta)$
%denote the risk minimizations using $\Phi$ as in (\ref{relucase}).
%Assume that $n_j \geq \min(n_1, n_K)$ for $j=2 \dots K-1$ [TODO I think this is not necessary].
%Then $\Omega_{\Forr}(\lambda)$ is connected for all $\lambda$.
%\end{theorem}
%{\it Proof:}
%We will again prove the result by induction over the depth $K$.
%Suppose first that $K=2$. The oracle risk is
%$$\Forr( W_1, W_2) = \Exp{ \| W_2 \rho W_1 X - Y \|^2}~.$$
%If we denote $X_{W_1} = \rho W_1 X$, let us verify that $\Forr(W_1, W_2)$
%only depends upon the correlation operator of $X_{W_1}$
%and its cross-correlation to $Y$. Indeed, we have
%\begin{eqnarray*}
%\Exp{ \| W_2 \rho W_1 X - Y \|^2} &=& \Exp{ \| W_2 X_{W_1} - Y \|^2} \\
%&=& W_2 \Sigma_{W_1} W_2^T + \Sigma_Y - 2  Tr\left( W_2 \Sigma_{W_1,Y} \right)~,
%\end{eqnarray*}
%where $\Sigma_{W_1} = \Exp{ X_{W_1} X_{W_1}^T}$ and $\Sigma_{W_1,Y} = \Exp{ X_{W_1} Y^T}$.
%Let us see that when $\rho(z)$ is the half-rectification the covariance structure
%of $X_{W_1}$ can be easily related to the original distribution. Indeed, we have
%the following
%\begin{lemma}
%Let $Z = \rho W X$ with $\rho(z) = \max(0,z)$. Then
%\begin{equation}
%\Sigma_Z = \tilde{W}^T \Sigma_X \tilde{W}
%\end{equation}
%\end{lemma}
%
%$\square$

[ TODO  $\Fem$ case].

%%%%%%%%%%%%%%%%%%%%%%
\section{Geometry of Level Sets}
%\section{Quantifying Nonconvexity}
\label{sec:QuanNoncon}

\subsection{The Greedy Algorithm}
\label{sec:GreedyAlg}
%%%%%%%%%%%%%%%%%%%%%%

 For a pair of models with network parameters $\theta_i$, $\theta_j$, each with $F_e(\theta)$ below a threshold $L_0$, we aim to efficienly generate paths in the space of weights where the empirical loss along the path remains below the threshold.  These paths are continuous curves belonging to $\Omega_F(\lambda)$--that is, the level sets of the loss function of interest.

 We provide a greedy algorith, Dynamic String Sampling, which finds such a path below.

\begin{algorithm}
\caption{Greedy Dynamic String Sampling}\label{euclid}
\begin{algorithmic}[1]
\State $\text{$L_0$} \gets \text{Threshold below which path will be found}$
\State $\text{$\Phi_1$} \gets \text{randomly initialize } $$\theta_1$$ \text{, train } $$\Phi (x_i\;\theta_1)$$ \text{ to $L_0$}$
\State $\text{$\Phi_2$} \gets \text{randomly initialize } $$\theta_2$$ \text{, train } $$\Phi (x_i\;\theta_2)$$ \text{ to $L_0$}$

\State $\text{BeadList} \gets $$(\Phi_1,\Phi_2)$
\State $\text{Depth} \gets 0$

\Procedure{FindConnection}{$\Phi_1,\Phi_2$}
\State $\text{$t^*$} \gets \text{t such that } $$\frac{d \gamma(\theta_1, \theta_2, t)}{dt} \bigg|_{t} = 0$$  \text{ OR } $$t = 0.5$$ $
\State $\text{$\Phi_3$} \gets \text{train } $$\Phi(x_i; t^*\theta_1 + (1-t^*)\theta_2)$$ \text{ to $L_0$}$
\State $\text{BeadList} \gets \text{insert}$$(\Phi_3$$\text{, after } $$\Phi_1$$\text{, BeadList)}$
\State $\text{$MaxError_1$} \gets \text{$max_t$}$$(F_e(t\theta_3 + (1-t)\theta_1))$$ $
\State $\text{$MaxError_2$} \gets \text{$max_t$}$$(F_e(t\theta_2 + (1-t)\theta_3))$$ $
\If {$\text{$MaxError_1$} > \text{$L_0$ }} \text{ }\Return \text{ FindConnection}$$(\Phi_1,\Phi_3)$$ $
\EndIf
\If {$\text{$MaxError_2$} > \text{$L_0$ }} \text{ }\Return \text{ FindConnection}$$(\Phi_3,\Phi_2)$$ $
\EndIf
\State $\text{Depth} \gets \text{Depth$+1$}$
\EndProcedure
\end{algorithmic}
\end{algorithm}

  The algorithm recursively builds a string of models in the space of weights which continuously connect $\theta_i$ to $\theta_j$.  Models are added and trained until the pairwise linearly interpolated loss, i.e. $\rm{max}_t F_e(t\theta_i\ +\ (1-t)\theta_j)$ for $t\in(0,1)$, is below the threshold, $L_0$, for every pair of neighboring models on the string.  We provide a cartoon of the algorithm in \figref{fig:AlgorithmFigure}.

 \begin{figure}
\begin{center}
\scalebox{1}{\includegraphics[width=1.0\columnwidth]{AlgorithmFigure}}
\end{center}
\caption{A cartoon of the algorithm.  $a):$ The initial two models with approximately the same loss, $L_0$. $b):$ The interpolated loss curve, in red, and its global maximum, occuring at $t=t^*$. $c):$ The interpolated model $\Theta(\theta_i, \theta_j, t^*)$ is added and labeled $\theta_{i,j}$.  $d):$ Stochastic gradient descent is performed on the interpolated model until its loss is below $\alpha L_0$. $e):$ New interpolated loss curves are calculated between the models, pairwise on a chain.  $f):$ As in step $c)$, a new model is inserted at the maxima of the interpolated loss curve between $\theta_i$ and $\theta_{i,j}$.  $g):$  As in step $d)$, gradient descent is performed until the model has low enough loss.}
\label{fig:AlgorithmFigure}
\end{figure}

  \subsection{Failure Conditions and Practicalities}
  \label{sec:Fail}

  While the algorithm presented will faithfully certify two models are connected if the algorithm converges, it is worth emphasizing that the algorithm does not guarantee that two models are disconnected if the algorithm fails to converge.  In general, the problem of determining if two models are connected can be made arbitrarily difficult by choice of a particularly pathological geometry for the loss function, so we are constrained to heuristic arguments for determining when to stop running the algorithm.  Thankfully, in practice, loss function geometries for problems of interest are not intractably difficult to explore.  We comment more on diagnosing disconnections more carefully in section SYMMETRYDISCONNECT.

  Further, if the $\rm{\mathbf{MaxError}}$ exceeds $L_0$ for every new recursive branch as the algorithm progresses, the worst case runtime scales as $O(\rm{exp}(\rm{\mathbf{Depth}}))$.  Empirically, we find that the number of new models added at each depth does grow, but eventually saturates, and falls for a wide variety of models and architectures, so that the typical runtime is closer to $O(\rm{poly}(\rm{\mathbf{Depth}}))$---at least up until a critical value of $L_0$.  We comment more on this in section NUMERICALDISCUSSION.

  Finally, we find that training $\Phi_3$ to $\alpha L_0$ for $\alpha < 1$ in line $8$ of the algorithm tends to aid convergence without noticeably impacting our numerics.

 %%%%%%%%%%%%%%%%%%%%%%
\section{Numerical Experiments}
\label{sec:NumExp}

For our numerical experiments, we aimed to extract qualitative features of both small, toy networks, as well as of larger workhorse networks suitable for use on real world tasks (e.g. MNIST).  At its core, the maximum interpolated error (i.e., \eqref{eq:minmaxerror}) is a measure of problem nonconvexity---or, more precisely, of the nonconvexity of the loss surface of a given architecture on a particular learning problem.

\subsection{Polynomial Regression}
\label{sec:PolyFuncs}
%%%%%%%%%%%%%%%%%%%%%%

 Polynomial function regression is a task for which small neural networks can achieve extremely high accuracy.  For our numerical experiments, we studied a 1-4-4-1 fully connected multilayer perceptron style architecture with RELU activation and RMSProp optimization.  For ease-of-analysis, we restricted the family of polynomials to be strictly contained in the interval $x\in[0,1]$ and $f(x)\in[0,1]$.

 Discussion of different Loss functions

 etc.

%%%%%%%%%%%%%%%%%%%%%%
\subsection{Convolutional Neural Networks}
\label{sec:CNN}
%%%%%%%%%%%%%%%%%%%%%%

%Exploitation
\begin{figure}
\label{figurelength}
\centering
\includegraphics[width=.4\textwidth]{Plots/normlengthcubics}
\includegraphics[width=.4\textwidth]{Plots/normlengthMNIST}
\caption{Normalized geodesic length as a function of the energy level for two different models. Left: a ``low-dimensional" neural network
attempting to fit a cubic polynomial. Right: a convolutional neural network on MNIST. Whereas the cubic fitting displays a heavily non-convex structure
at mid-energy values, the MNIST example qualitatively behaves as a convex loss, in which geodesics approach straight lines.}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
\section{Discussion}
\label{sec:Discussion}
%%%%%%%%%%%%%%%%%%%%%%
\begin{itemize}
\item Future: Generalization Error Question.

\end{itemize}

\subsubsection*{Acknowledgments}

Use unnumbered third level headings for the acknowledgments. All
acknowledgments, including those to funding agencies, go at the end of the paper.

\appendix

\section{Constrained Dynamic String Sampling}
  \label{sec:ConstrainedAlg}

  While the algorithm presented in Sec. \ref{sec:GreedyAlg} is fast for sufficiently smooth families of loss surfaces with few saddle points, here we present a slightly modified version which, while slower, provides more control over the convergence of the string.  Instead of training intermediate models via full SGD to a desired accuracy, intermediate models will be subject to a constraint that ensures they are ``close'' to the neighboring models on the string.  Specifically, intermediate models will be constrained to the unique hyperplane in weightspace equidistant from its two neighbors.  This is similar to a sort of ``$L_1$ regularization'' where the loss function for a given model on the string, $\theta_i$, has an additional term $\tilde{L}(\theta) = L(\theta)+\zeta(\|\theta_{i-1} - \theta_i\|+\|\theta_{i+1} + \theta_i\|)$.  The strength of the $\zeta$ regularization term controls the ``springy-ness'' of the weightstring. note: make this more precise, the hyperplane constraint is stronger than the $L_1$ constraint...$L_1$ only keeps the model in a ball close to the midpoint between the models.

  Because adapting DSS to use this constraint is straightforward, here we will describe an alternative ``breadth-first'' approach wherein models are trained in parallel until convergence.  This alternative approach has the advantage that it will indicate a disconnection between two models ``sooner'' insofar as it will be clear two models cannot be connected once the loss on either of the two initial models, $\theta_1$ or $\theta_2$, is less than $\Gamma(\theta_1, \theta_2)$.  The precise geometry of the loss surface will dictate which approach to use in practice.

  Given two random models $\sigma_i$ and $\sigma_j$ where $|\sigma_i - \sigma_j| < \kappa$, we aim to follow the evolution of the family of models connecting $\sigma_i$ to $\sigma_j$.  Intuitively, almost every continuous path in the space of random models connecting $\sigma_i$ to $\sigma_j$ has, on average, the same (high) loss.  For simplicity, we choose to initialize the string to the linear segment interpolating between these two models.  If this entire segment is evolved via gradient descent, the segment will either evolve into a string which is entirely contained in a basin of the loss surface, or some number of points will become fixed at a higher loss.  These fixed points are difficult to detect directly, but will be indirectly detected by the persistence of a large interpolated loss between two adjacent models on the string.

  The algorithm proceeds as follows:

  (0.) Initialize model string to have two models, $\sigma_i$ and $\sigma_j$.

  1. Begin training all models to the desired loss, keeping the instantaneous loss of all models being trained approximately constant..

  2. If the pairwise interpolated loss $\gamma(\sigma_n,\sigma_{n+1})$ exceeds a tolerance $\alpha_1$, insert a new model at the maximum of the interpolated loss between these two models.  For simplicity, this tolerance is chosen to be $(1 + \alpha_1^*)$ times the instantaneous loss of all other models on the string.

  3. Repeat steps (1) and (2) until all models (and interpolated errors) are below a threshold loss $L_0$, or until a chosen failure condition (see \ref{sec:Fail}).

\bibliography{iclr2017_conference}
\bibliographystyle{iclr2017_conference}

\end{document}


\section{Setting the target domain} \label{sec:domain}
%=======================================================================

In this section, the number of grids, and the target domain and its relationship with the MPI process are explained.
The calculation domain is determined by the horizontal grid spacing and the number of grid points.
The parallelization is implemented by a 2D domain decomposition in the horizontal directions.

Figure \ref{fig:domain} shows an example of this relationship.
The total numbers of grids are specified by \nmitem{IMAXG, JMAXG} in \namelist{PARAM_ATMOS_GRID_CARTESC_INDEX}.
The entire domain is divided into \nmitem{PRC_NUM_X} along the X direction and \nmitem{PRC_NUM_Y} in the Y direction; the \nmitem{PRC_NUM_X} and \nmitem{PRC_NUM_Y} are specified in \namelist{PARAM_PRC_CARTESC}.
The process number starts zero and is numbered in order from the bottom left to the top right (Arrow in Fig. \ref{fig:domain}).
Each sub-domain is managed by an MPI process, each of which takes charge of a grid block of \nmitem{IMAX} $\times$ \nmitem{JMAX} $\times$ \nmitem{KMAX},
where \nmitem{KMAX} is the number of grids along the vertical direction specified in \namelist{PARAM_ATMOS_GRID_CARTESC_INDEX}.
Care is taken to ensure that \nmitem{IMAX} and \nmitem{JMAX} must be identical for all MPI processes.
Therefore, \nmitem{PRC_NUM_X} and \nmitem{PRC_NUM_Y} must be a divisor of \nmitem{IMAXG} and \nmitem{JMAXG}, respectively.

The total number of grids along each horizontal direction and that in the entire domain are summarized as
\begin{eqnarray}
  && \nmitemeq{IMAXG} = \nmitemeq{IMAX} \times \nmitemeq{PRC_NUM_X}
  \label{eq:xgridnum}\\
  && \nmitemeq{JMAXG} = \nmitemeq{JMAX} \times \nmitemeq{PRC_NUM_Y}.
  \label{eq:ygridnum}\\
&& \verb|Total number of grids in the domain| \nonumber\\
&&\quad = \nmitemeq{IMAXG} \times \nmitemeq{JMAXG} \times \nmitemeq{KMAX}, \nonumber\\
&&\quad = \left(\nmitemeq{IMAX} \times \nmitemeq{PRC_NUM_X}\right)
          \times (\nmitemeq{JMAX} \times \nmitemeq{PRC_NUM_Y})
          \times (\nmitemeq{KMAX} ).
\end{eqnarray}
When \nmitem{IMAXG} and \nmitem{JMAXG} are specified, \nmitem{IMAX} and \nmitem{JMAX} are internally calculated.
Alternatively, user can specify \nmitem{IMAX} and \nmitem{JMAX} instead of \nmitem{IMAXG} and \nmitem{JMAXG}.
In this case, \nmitem{IMAXG} and \nmitem{JMAXG} are internally calculated by using Eqs. (\ref{eq:xgridnum}) and (\ref{eq:ygridnum}), respectively.

The size of the entire domain is determined as follows:
\begin{eqnarray}
 \verb|Domain length in the X direction| &=& \nmitemeq{IMAXG} \times \nmitemeq{DX}\\
 \verb|Domain length in the Y direction| &=& \nmitemeq{IMAXG} \times \nmitemeq{DY},
\end{eqnarray}
where \nmitem{DX, DY} is grid spacings specified in \namelist{PARAM_ATMOS_GRID_CARTESC} as described in subsection \ref{subsec:gridinterv}.

In order to perform a horizontal-vertical two-dimensional experiment, set both the \nmitem{IMAXG} and \nmitem{PRC_NUM_X} to 1.
In this case, the motion in the Y-Z section is calculated.
The value of \texttt{DX} does not affect the simulation result.

In the next subsections, the number of grids, the grid interval, and the configuration of the MPI processes are described in more detail.
\textcolor{blue}{Note that it is necessary that these settings must be identical among the configuration files for \texttt{scale-rm\_pp},  \texttt{scale-rm\_init}, and \texttt{scale-rm}}.

\begin{figure}[h]
\begin{center}
  \includegraphics[width=0.8\hsize]{./../../figure/domain_decomposition.pdf}\\
  \caption{Relation between horizontal grid interval (\texttt{DX}, \texttt{DY}),
   the number of grids (\nmitem{IMAX}, \nmitem{JMAX}) per MPI process,
   the total number of grids (\nmitem{IMAXG}, \nmitem{JMAXG}) in the entire domain,
   and the number of MPI processes (\nmitem{PRC_NUM_X}, \nmitem{PRC_NUM_Y}).
   The blue part corresponds to a region managed by an MPI process.
   The six digit number following ``pe'' indicates the process number.}
  \label{fig:domain}
\end{center}
\end{figure}

%-----------------------------------------------------------------------
\subsection{Setting the number of horizontal and vertical grids} \label{subsec:relation_dom_reso3}
%-----------------------------------------------------------------------

The number of grids is specified in \namelist{PARAM_ATMOS_GRID_CARTESC_INDEX} in the configuration files.
\editboxtwo{
\verb|&PARAM_ATMOS_GRID_CARTESC_INDEX| & \\
\verb| KMAX  = 97,|  & ; Number of vertical layers \\
\verb| IMAXG = 40,|  & ; Total number of grids along the {\XDIR} \\
\verb| JMAXG = 25,|  & ; Total number of grids along the {\YDIR} \\
\verb|/|\\
}

%-----------------------------------------------------------------------
\subsection{Setting grid intervals along the horizontal and vertical directions} \label{subsec:gridinterv}
%-----------------------------------------------------------------------

Excluding the buffer region explained in Section \ref{subsec:buffer}, the horizontal grid intervals are configured only equidistantly, whereas the vertical grid intervals are configured freely.
When the grid intervals are configured uniformly along all directions, specify the grid intervals for the x, y, and z directions with \nmitem{DX, DY, DZ} in \namelist{PARAM_ATMOS_GRID_CARTESC}, respectively.
The unit is [m].
\editboxtwo{
\verb|&PARAM_ATMOS_GRID_CARTESC  | & \\
\verb| DX = 500.D0,| & ; Grid interval along the {\XDIR} \\
\verb| DY = 500.D0,| & ; Grid interval along the {\YDIR} \\
\verb| DZ = 500.D0,| & ; Grid interval along the vertical (Z) direction\\
\verb|/|\\
}

An arbitrary non-uniform grid can be specify in the vertical direction.
Since the model employs the C-grid system, the points of definition for the velocity vector and other scalars are staggered, deviating by a half grid.
In this document, the scalar location is called the center point and the half-grid-deviated location the face point.
Refer to Figure \ref{fig:scale_grid} for the details.
The face point of the vertical grids can be specified with \nmitem{FZ(:)} in \namelist{PARAM_ATMOS_GRID_CARTESC} as an array \footnote{In this case, the same precision as used in the simulation is recommended to be specified. By default, the model is compiled as a double-precision floating point model.}.
Note that the number of elements specified in \nmitem{FZ(:)} should correspond to the number of vertical layers (\nmitem{KMAX} in \namelist{PARAM_ATMOS_GRID_CARTESC_INDEX}).
The following file for the ideal experiment is shown as an example:
\editboxtwo{
\verb|&PARAM_ATMOS_GRID_CARTESC|     & \\
\verb| DX = 500.D0,|   & Grid interval along the X direction (equidistant) [m]\\
\verb| DY = 500.D0,|   & Grid interval along the Y direction (equidistant) [m]\\
\verb| FZ(:) = |       & Location at face point along the Z direction [m] \\
\verb|     80.000000000000000D0      ,| & \\
\verb|     168.00000190734863D0      ,| & \\
\verb|     264.80000610351567D0      ,| & \\
\verb|           ........           | & \\
\verb|     14910.428862936289D0      ,| & \\
\verb|     15517.262523292475D0      ,| & \\
\verb|     16215.121232702089D0      ,| & \\
\verb|     17017.658748523147D0      ,| & \\
\verb|     17940.576891717363D0      ,| & \\
\verb|     19001.932756390710D0      ,| & \\
\verb|     20222.492000765058D0      ,| & \\
\verb| BUFFER_DZ = 5000.D0,|          & Refer to Section \ref{subsec:buffer}\\
\verb| BUFFFACT  =   1.0D0,|          & Refer to Section \ref{subsec:buffer}\\
\verb|/|\\
}

\begin{figure}[tb]
\begin{center}
  \includegraphics[width=0.4\hsize]{./../../figure/verticalface.pdf}\\
  \caption{The definition of the face point in \scalerm. If \nmitem{FZ} is given in \namelist{PARAM_ATMOS_GRID_CARTESC}, the top height at the first layer is given for the value at $k=1$. Note that $k=1$ is not the ground surface height.}
  \label{fig:scale_grid}
\end{center}
\end{figure}

The above setting is processed at a topographical height of 0 m.
The location of the vertical grids at the non-zero topography is appropriately treated by the terrain-following coordinate.

The locations of the vertical grids are configured arbitrarily.
However, an unusual configuration sometimes leads to numerical instability. To avoid it, the tool for the generation of vertical grids is supported as a FORTRAN program \verb|make_vgrid.f90| in the directory\\ \texttt{scale-\version/scale-rm/util/makevgrid/} with several samples of the namelist. If needed, use them as references. The tool generates the values of \nmitem{FZ(:)} directly. Copy and paste them in the configuration file.

%-----------------------------------------------------------------------
\subsection{Setting the number of MPI processes} \label{subsec:relation_dom_reso2}
%-----------------------------------------------------------------------

The number of MPI processes is specified in \namelist{PARAM_PRC_CARTESC} in the configuration file.
\editboxtwo{
\verb|&PARAM_PRC_CARTESC| & \\
\verb| PRC_NUM_X       = 2,| & ; number of divisions by MPI parallelization in the {\XDIR} (zonal direction) \\
\verb| PRC_NUM_Y       = 1,| & ; number of divisions by MPI parallelization in the {\YDIR} (meridional direction) \\
\verb|/|\\
}
It should be noted that the number of MPI process must be a divisor of the total grid number \nmitem{IMAXG} or \nmitem{JMAXG} in the x- and y-directions, respectively.
Otherwise, the program is terminated immediately with the following message:
\msgbox{
  \verb|number of IMAXG should be divisible by PRC_NUM_X| \\
}
or
\msgbox{
  \verb|number of JMAXG should be divisible by PRC_NUM_Y| \\
}.

The total number of MPI processes is given by \verb|PRC_NUM_X| $\times$ \verb|PRC_NUM_Y|.
The above example expresses a two-MPI parallel by dividing the domain into two sub-domains along the X direction, but not dividing along the Y direction. The total number of processes must be given as the number of MPI processes in the MPI command at submitting job.
If this condition is not satisfied,  the program is terminated immediately without calculation and the following message is output to the standard output.
\msgbox{
\verb|xxx total number of node does not match that requested. Check!| \\
}

Since the input and output files of \scalerm are divided process by process according to the MPI, the total number of files is changed according to the number of MPI processes.
For example, the initial and boundary files made by two-MPI processes cannot be used for model execution by four-MPI processes.
If the number of MPI processes is changed, it is necessary
to edit \namelist{PARAM_PRC_CARTESC} in \verb|pp.conf|, \verb|init.conf|, and \verb|run.conf|,
and then conduct once again the processes of \verb|pp| and \verb|init|.
As another way, the postprocess \sno is also available for redistribution (Please refer Sec.\ref{sec:sno}).

%-----------------------------------------------------------------------
\subsection{Setting sponge layer} \label{subsec:raydamp}
%-----------------------------------------------------------------------

\scalerm adopts height coordinate system in vertical. The uppermost boundary condition is a rigid lid, and the sound and gravity waves often reflect at the model top. To reduce worse effects of these reflecting waves, the damping layer named ``sponge layer'' is placed in the upper part of the model domain. In the sponge layer, a vertical velocity is dumped by Rayleigh friction. The relaxation time scale (= e-folding time) of damping is minimum at the model top and it increases with decreasing the height. Below the bottom boundary of sponge layer, the relaxation time scale is set to infinity.
There are two methods to set the thickness of the sponge layer in \namelist{PARAM_ATMOS_DYN}.

\begin{enumerate}
\item specify number of layer of the sponge layer \\
  The number of layer appointed in \nmitem{ATMOS_DYN_wdamp_layer} is set as the sponge layer. The number is counted from the model top.
\item specify bottom boundary height [m] of the sponge layer \\
  The layer that is higher than altitude appointed in \nmitem{ATMOS_DYN_wdamp_height} is set as the sponge layer.
\end{enumerate}

Both parameters above are not set by default, and the sponge layer is not applied. If both are set, \nmitem{ATMOS_DYN_wdamp_layer} is given priority.

The relaxation time at the uppermost boundary is specified by \nmitem{ATMOS_DYN_wdamp_tau}. The unit is [second]. This parameter is not allowed to set the value smaller than \nmitem{TIME_DT_ATMOS_DYN}. When \nmitem{ATMOS_DYN_wdamp_tau} is not specified explicitly, the value ten times as large as \\
\nmitem{TIME_DT_ATMOS_DYN} is automatically set. Please refer to section \ref{sec:timeintiv} for \nmitem{TIME_DT_ATMOS_DYN}.
The example of concrete setting is shown in section \ref{subsec:atmos_dyn_scheme}.

%-----------------------------------------------------------------------
\subsection{Setting Buffer Region and Boundary Nudging Method} \label{subsec:buffer}
%-----------------------------------------------------------------------

In general, disagreement in values between input data as boundary condition and actual calculation output occurs at the lateral boundaries.
They generate several problems, such as nonphysical mode, in calculation.
To avoid these problems, the ``buffer region'' is placed in the domain.

As shown in Fig.\ref{fig:buff_xz}, \scalerm places the buffer region just inside the calculation domain.
In the buffer region, prognostic variables are updated to be close to the specified values of boundary data and/or the parent model data with a certain relaxation time.
Hereinafter, this relaxation is called nudging.

\subsubsection{Buffer Region}

The width of the buffer region is specified in \namelist{PARAM_ATMOS_GRID_CARTESC} in the configuration file. Note again that the configuration in all procedures must be identical. There are two methods to configure the width of the buffer region.

\begin{enumerate}
\item specify number of grid of the buffer region with \nmitem{BUFFER_NX, BUFFER_NY, BUFFER_NZ}
\item specify width [m] of the buffer region with \nmitem{BUFFER_DX, BUFFER_DY, BUFFER_DZ}
\end{enumerate}
Both parameters above are not set by default, and no buffer regions are set. If both are set, \nmitem{BUFFER_NX, BUFFER_NY, BUFFER_NZ} is given priority.
The buffer regions along the horizontal directions are placed at the four domain boundaries,
whereas those along the vertical direction are placed just at the top of the domain.
Nothing is affected in the bottom region.
Note that the actual target region unaffected by the nudging (the region excluding the buffer regions) narrows compared to the calculation domain because the buffer region is placed on the inside of calculation domain.

Two examples are as below.
%
\editboxtwo{
\verb|&PARAM_ATMOS_GRID_CARTESC| & \\
 \verb|BUFFER_NX = 30, | & ; The number of grid for the buffer region along the \XDIR \\
 \verb|BUFFER_NY = 30, | & ; The number of grid for the buffer region along the \YDIR \\
 \verb|BUFFFACT  = 1.D0, | & ; Stretched factor for grid intervals in the buffer region \\
\verb|/|\\
}
\editboxtwo{
\verb|&PARAM_ATMOS_GRID_CARTESC| & \\
 \verb|BUFFER_DZ  = 5000.D0,   | & ; The width of the buffer region along the Z direction from the top of the model (a reference) [m]\\
 \verb|BUFFER_DX  = 300000.D0, | & ; The width of the buffer region along the X (zonal) direction ( a reference ) [m]\\
 \verb|BUFFER_DY  = 300000.D0, | & ; The width of the buffer region along the Y (meridional) direction ( a reference ) [m]\\
 \verb|BUFFFACT_Z = 1.20D0,    | & ; Stretched factor for grid intervals along the Z direction\\
 \verb|BUFFFACT_X = 1.05D0,    | & ; Stretched factor for grid intervals along the X (zonal) direction\\
 \verb|BUFFFACT_Y = 1.05D0,    | & ; Stretched factor for grid intervals along the Y (meridional) direction\\
 \verb|/|\\
}

The setting procedure of buffer region for the X direction is described as follows.
The number of grids \verb|ibuff| in the buffer region is equal to \nmitemeq{BUFFER_NX}.
If \nmitemeq{BUFFER_DX} is configured instead of \nmitemeq{BUFFER_NX}, \verb|ibuff| is automatically calculated as the minimum integer satisfying the following condition:
%
\begin{eqnarray}
   && \sum_{n=1}^{\verb|ibuff|} \verb|BDX|(n) \ge \nmitemeq{BUFFER_DX}. \nonumber
\end{eqnarray}
%
Thus, it should be noted that the width of the buffer region $\verb|BUFFER|_{\verb|X|}$ ($= \sum_{n=1}^{\verb|ibuff|} \verb|BDX|(n)$) does not always correspond to \nmitem{BUFFER_DX}. At the end, the actual target region excluded by the buffer region is expressed as
%
\begin{eqnarray}
   && \nmitemeq{DX} \times ( \nmitemeq{IMAXG} - 2 \times \verb|ibuff| ).
\end{eqnarray}
%
Although the situations along the Y and Z directions are similar to this, note that the actual target region along the Z direction is expressed as
%
\begin{eqnarray}
   && \nmitemeq{DZ} \times ( \nmitemeq{KMAX} - \verb|kbuff| ),
\end{eqnarray}
%
using the number of grids \verb|kbuff| in the upper buffer region.

\begin{figure}[t]
\begin{center}
  \includegraphics[width=0.8\hsize]{./../../figure/buffer_xz.pdf}\\
  \caption{Location of the buffer region in the entire calculation domain: the shaded area indicates the buffer region. This figure shows the XZ cross-section. It is the same as the YZ cross-section.}
  \label{fig:buff_xz}
\end{center}
\end{figure}

In general, there is no clear criterion for setting the width and locating grids in the buffer region.
This depends on a problem to be solved.
In \scalerm, the followings are recommended: the number of grids in the vertical buffer region at the top of the model is greater than 5, whereas that in the lateral boundaries is approximately 20$\sim$40.
Depending on the experiment, it may be necessary to increase the number of grids in the buffer region, to increase the buffer region itself by using the appropriate stretch factor, to tune relaxation time, and so on.
The relaxation time is explained below.

The grid intervals in the buffer region are the same as \nmitem{DX, DY, DZ} in \namelist{PARAM_ATMOS_GRID_CARTESC} by default.
But, it is possible for them to be stretched by setting \nmitem{BUFFFACT} $>$ 1. This specification of \nmitem{BUFFFACT} is applied in all directions if the grid intervals are uniformly specified. When the stretched factor is configured separately in every direction, specify \nmitem{BUFFFACT_X, BUFFFACT_Y, BUFFFACT_Z}. Note that in case of the configuration of vertical levels by giving \nmitem{FZ(:)} (refer to \ref{subsec:gridinterv}), the above stretched settings have no effect along the vertical direction.

The grid interval \verb|BDX| in the buffer region is determined as follows:
\begin{eqnarray}
 \verb|BDX(|n\verb|)| &=& \verb|DX| \times \verb|BUFFFACT|^n, \nonumber
\end{eqnarray}
where $n$ denotes the index of grids in the buffer region, in the order directed from the inner to the outer region in the domain. The grid interval is the same as the inner domain at \nmitem{BUFFFACT=1.0}, whereas it increases from the inner to the outer region by a factor of 1.2 at \nmitem{BUFFFACT=1.2}.  Although any value of \nmitem{BUFFFACT} can be configured, the value from 1.0 to 1.2 is recommended to avoid numerical instability.

Finally, the width of the buffer region $\verb|BUFFER|_{\verb|X|}$ is as follows:
\begin{eqnarray}
  \verb|BUFFER|_{\verb|X|} = \nmitemeq{DX} \times \frac{ \nmitemeq{BUFFFACT}^{\texttt{\detokenize{ibuff}}}-1}{ \nmitemeq{BUFFFACT}-1 }
\end{eqnarray}
Even if the same width of buffer region \nmitem{BUFFER_DX} is specified, the number of grids in the buffer region decreases with increasing \nmitem{BUFFFACT}.
When given by \nmitem{BUFFER_NX}, only the width of buffer region is changed.

\subsubsection{Nudging Methods in Buffer Region}

\namelist{PARAM_ATMOS_BOUNDARY} has parameters to configure the nudging in the buffer region.
The boundary data type is configurable by \nmitem{ATMOS_BOUNDARY_TYPE} in \namelist{PARAM_ATMOS_BOUNDARY} (Table \ref{tab:nml_atmos_boundary_type}.)

\begin{table}[h]
\begin{center}
\caption{Choices of the boundary data type}
\label{tab:nml_atmos_boundary_type}
\begin{tabularx}{150mm}{lXX} \hline
  \rowcolor[gray]{0.9} Value & Description of type \\ \hline
  \verb|NONE|    & Do not nudge \\
  \verb|CONST|   & Nudge to a prescribed constant value \\
  \verb|INIT|    & Nudge to the initial value \\
  \verb|OFFLINE| & Nudge to value read from a file (temporally unchanged) \\
  \verb|REAL|    & Nudge to time-dependent value of the parent model or domain \\
  \hline
\end{tabularx}
\end{center}
\end{table}

The following is the parameters in \namelist{PARAM_ATMOS_BOUNDARY}.
\editboxtwo{
  \verb|&PARAM_ATMOS_BOUNDARY | & \\
  \verb| ATMOS_BOUNDARY_TYPE = 'NONE',         | & ; The boundary data type. See Table \ref{tab:nml_atmos_boundary_type}. \\
  \verb| ATMOS_BOUNDARY_IN_BASENAME = '',      | & ; File name of the boundary data for \verb|OFFLINE| or \verb|REAL| type \\
  \verb| ATMOS_BOUNDARY_IN_CHECK_COORDINATES | \textbackslash \\
  ~~\verb|                   = .true.,| & ; Flag to check coordinate variables in the boundary data file. \\
  \verb| ATMOS_BOUNDARY_START_DATE | \textbackslash \\
  ~~\verb|        = (/ -9999, 0, 0, 0, 0, 0 /),| & ; Start time of the boundary data. Only for \verb|REAL| type. \\
  \verb| ATMOS_BOUNDARY_UPDATE_DT = 0.0D0,     | & ; Time interval of the boundary data. Only for \verb|REAL| type. \\
  \\ ({\small\slshape continued on next page})
}
\editboxtwo{
  ({\small\slshape continued from previous page}) \\ \\
  \verb| ATMOS_BOUNDARY_INTERP_TYPE | \textbackslash \\
  ~~\verb|                  = 'lerp_initpoint',| & ; Temporal interpolation type. \\
                                                 &  ~\verb|same_parent|: use the latest step value (no interpolation), \\
                                                 &  ~\verb|nearest_neighbor|: use the value of the nearest time step, \\
                                                 &  ~\verb|lerp_initpoint|: linear interpolation between values at two time steps as the values are snapshot, \\
                                                 &  ~\verb|lerp_midpoint|: same as \verb|lerp_initpoint| but the values are temporal average during the time step for boundary data. \\
  \verb| ATMOS_BOUNDARY_OUT_BASENAME = '',     | & ; File name to output the initial boundary data. \\
  \verb| ATMOS_BOUNDARY_OUT_TITLE | \textbackslash \\
  ~~\verb|     = 'SCALE-RM BOUNDARY CONDITION',| & ; Title for the output file. \\
  \verb| ATMOS_BOUNDARY_OUT_DTYPE = 'DEFAULT', | & ; Data type (\verb|REAL4| or \verb|REAL8|) for the output. \\
  \verb| ATMOS_BOUNDARY_USE_DENS = .false.,    | & ; Switch of the nudging for the density. \\
  \verb| ATMOS_BOUNDARY_USE_VELZ = .false.,    | & ; Switch for the w. \\
  \verb| ATMOS_BOUNDARY_USE_VELX = .false.,    | & ; Switch for the u. \\
  \verb| ATMOS_BOUNDARY_USE_VELY = .false.,    | & ; Switch for the v. \\
  \verb| ATMOS_BOUNDARY_USE_PT = .false.,      | & ; Switch for the $\theta$. \\
  \verb| ATMOS_BOUNDARY_USE_QV = .false.,      | & ; Switch for the vapor. \\
  \verb| ATMOS_BOUNDARY_USE_QHYD = .false.,    | & ; Switch for the hydrometeors. \\
  \verb| ATMOS_BOUNDARY_VALUE_VELZ = 0.0D0,    | & ; Value of the w. Only for \verb|CONST| type. \\
  \verb| ATMOS_BOUNDARY_VALUE_VELX = 0.0D0,    | & ; Value of the u. Only for \verb|CONST| type. \\
  \verb| ATMOS_BOUNDARY_VALUE_VELY = 0.0D0,    | & ; Value of the v. Only for \verb|CONST| type. \\
  \verb| ATMOS_BOUNDARY_VALUE_PT = 300.0D0,    | & ; Value of the $\theta$. Only for \verb|CONST| type. \\
  \verb| ATMOS_BOUNDARY_VALUE_QTRC =   0.0D0,  | & ; Value of the vapor. Only for \verb|CONST| type. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_DENS = 1.0D0,| & ; Factor of the $1/\tau$ for the density. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_VELZ = 1.0D0,| & ; Factor for the w. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_VELX = 1.0D0,| & ; Factor for the u. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_VELZ = 1.0D0,| & ; Factor for the v. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_PT = 1.0D0,  | & ; Factor for the $\theta$. \\
  \verb| ATMOS_BOUNDARY_ALPHAFACT_QTRC = 1.0D0,| & ; Factor for the vapor. \\
  \verb| ATMOS_BOUNDARY_SMOOTHER_FACT = 0.2D0, | & ; Factor of the horizontal smoother against the pointwise difference. \\
  \verb| ATMOS_BOUNDARY_FRACZ = 1.0D0,         | & ; Fraction for the nudging region to the buffer region in the z-direction. \\
  \verb| ATMOS_BOUNDARY_FRACX = 1.0D0,         | & ; Fraction in the x-direction. \\
  \verb| ATMOS_BOUNDARY_FRACY = 1.0D0,         | & ; Fraction in the y-direction. \\
  \verb| ATMOS_BOUNDARY_TAUZ = DT * 10.0D0,    | & ; Time scale of the nudging at the top boundary (in second). \\
  \verb| ATMOS_BOUNDARY_TAUX = DT * 10.0D0,    | & ; Time scale at the western and eastern boundaries. \\
  \verb| ATMOS_BOUNDARY_TAUY = DT * 10.0D0,    | & ; Time scale at the southern and northern boundaries. \\
  \verb| ATMOS_BOUNDARY_LINEAR_V = .false.,    | & ; Profile type of the time scale in the z-direction. If \verb|.true.|, it is a linear profile, otherwise a sinusoidal profile. \\
  \verb| ATMOS_BOUNDARY_LINEAR_H = .false.,    | & ; Profile type in the x- and y-direction. If \verb|.true.|, it is a linear profile, otherwise a exponential profile. \\
  \verb| ATMOS_BOUNDARY_EXP_H = 2.0D0,         | & ; Factor of the exponent of the exponential profile. \\
  \verb| ATMOS_BOUNDARY_DENS_ADJUST = .false., | & ; Switch of the mass flux adjustment. \\
  \verb| ATMOS_BOUNDARY_DENS_ADJUST_TAU | \textbackslash \\
  ~~\verb|     = -1.0D0,                       | & ; Time scale of the density nudging when the mass flux adjustment is enabled (in second). \\
}

The tendency due to the nudging is written as
\begin{eqnarray}
  \left.\frac{\partial \phi_{k,i,j}}{\partial t}\right|_\mathrm{nudging}
  & = & - \alpha \Delta\phi_{k,i,j} \\ \nonumber
  && + \alpha_s \left( \frac{\Delta\phi_{k,i-1,j} + \Delta\phi_{k,i+1,j} + \Delta\phi_{k,i,j-1} + \Delta\phi_{k,i,j+1}}{8} - \frac{\Delta\phi_{k,i,j}}{2} \right),
\label{eq:nudging}
\end{eqnarray}
where $\Delta\phi$ is difference from the boundary data and $\alpha_s = \alpha \times \nmitemeq{ATMOS_BOUNDARY_SMOOTHER_FACT}$.
The $\alpha$ is the maximum of those in the tree directions $\alpha_x, \alpha_y$ and $\alpha_z$.
The $\alpha$s depend on a length scale $e$ as
\begin{equation}
  e = \max\left( 1 - \frac{d}{\texttt{BUFFER} \times \nmitemeq{ATMOS_BOUNDARY_FRAC}}, 0 \right),
\end{equation}
where $d$ is distance from the boundary.
If \nmitem{ATMOS_BOUNDARY_LINEAR_V} = \verb|.true.|,
\begin{equation}
  \alpha_z = e_z / \tau_z,
\end{equation}
otherwise
\begin{equation}
  \alpha_z =  \sin^2(\pi e_z/2) / \tau_z,
\end{equation}
where $\tau_z$ is \nmitem{ATMOS_BOUNDARY_TAUZ}.
For the horizontal direction, if \nmitem{ATMOS_BOUNDARY_LINEAR_H} = \verb|.true.|,
\begin{equation}
  \alpha_x = e_x / \tau_x,
\end{equation}
otherwise
\begin{equation}
  \alpha_x = e_x \exp\{ - (1-e_x) \times \nmitemeq{ATMOS_BOUNDARY_EXP_H} \} / \tau_x.
\end{equation}
$\alpha_y$ is derived by the same way as $\alpha_x$.

The $\tau$ is the relaxation time at the boundary ($d=0$) with which the difference between the simulated value and the boundary value becomes $1/e$.
On the other hand, two grid scale component of $\Delta \phi$ becomes $1/e$ with the time of $\tau/\nmitemeq{ATMOS_BOUNDARY_SMOOTHER_FACT}$ by the second term of the right-hand side of Eq. \ref{eq:nudging}.
The default value of the $\tau$ is ten times of \nmitem{TIME_DT}.
Please refer to Section \ref{sec:timeintiv} for \nmitem{TIME_DT}.

If \nmitem{ATMOS_BOUNDARY_TYPE} = ``\verb|REAL|'' in \namelist{PARAM_ATMOS_BOUNDARY},
the nudging at the top and lateral boundaries is applied for the horizontal velocities, potential temperature, density, and vapor,
regardless of the settings of \nmitem{ATMOS_BOUNDARY_USE_{VELX,VELY,PT,DESN,QV}}.
The nudging for the vertical velocity and hydrometeors is applied
when \nmitem{ATMOS_BOUNDARY_USE_VELZ}=\verb|.true.| and \nmitem{ATMOS_BOUNDARY_USE_QHYD}=\verb|.true.|, respectively.
In the case of an online nesting simulation (See Section \ref{subsec:nest_online}),
the same settings with the `` \verb|REAL|'' boundary type is applied to the child domain,
except that \nmitem{ONLINE_USE_VELZ} and \nmitem{ONLINE_BOUNDARY_USE_QHYD} in \namelist{PARAM_COMM_CARTESC_NEST}
are used instead of \nmitem{ATMOS_BOUNDARY_USE_VELZ} and \nmitem{ATMOS_BOUNDARY_USE_QHYD}, respectively.

The density nudging is effective to reduce the bias of total mass in the simulation. However, it also decreases the pressure gradient in the nudging region; the pressure gradient plays a role to transfer the information of boundary data into the inner domain especially in outflow region.
As a way to reduce the mass bias keeping the pressure gradient, the adjustment of the mass flux is available.
By using the adjustment of the mass flux, the density nudging could be weakened.
%
If \nmitem{ATMOS_BOUNDARY_DENS_ADJUSTMENT} = \verb|.true.|,
the mass flux at the lateral boundaries is adjusted so that the difference in the total mass between the parent model and the simulation becomes small.
For example, if the total mass in the simulation is less than that in the parent model, the $\rho u$ at the western boundary and the $\rho v$ at the southern boundary are increased, and the $\rho u$ at the eastern boundary and the $\rho v$ at the northern boundary are decreased to enlarge the total mass convergence.
The mass flux adjustment is available only for the \verb|REAL| boundary type and child domain of the online nesting.
When the mass flux adjustment is enabled, the time scale of the density nudging is specified by \nmitem{ATMOS_BOUNDARY_DENS_ADJUSTMENT_TAU}.
If the value is negative, the time scale is internally determined as $\nmitemeq{ATMOS_BOUNDARY_UPDATE_DT} / 6.0$.
To weaken the density nudging, set the time scale larger than \nmitem{ATMOS_BOUNDARY_TAUX} and \nmitem{ATMOS_BOUNDARY_TAUY}.
The adjustment method in \scalerm is constructed under the assumption that the total mass change due to convergence of the mass flux at the lateral boundaries is much larger than that due to the physical processes, such as precipitation and surface latent heat flux.
The validity of the assumption depends on target domain and situation.

There exists a similar dumping method at near the top boundary, i.e., Rayleigh dumping. See Section \ref{subsec:raydamp}.

%-----------------------------------------------------------------------
\subsection{Setting vertical grids for ocean, land, and urban models} \label{subsec:gridolu}
%-----------------------------------------------------------------------

Settings for horizontal grids of ocean, land, and urban models are same as those for the atmospheric model.
The numbers of the grids are specified by \nmitem{IMAXG, JMAXG} in \namelist{PARAM_ATMOS_GRID_CARTESC_INDEX}.
The horizontal grid intervals are specified by \nmitem{DX, DY} in \namelist{PARAM_ATMOS_GRID_CARTESC}.
On the other hand, settings for vertical grids should be specified for each model separately.

Please see Sec. \ref{sec:basic_usel_ocean}, Sec. \ref{sec:basic_usel_land} and Sec. \ref{sec:basic_usel_urban}
for vertical grid settings for ocean, land and urban models, respectively.


\section{Discussion}
We developed a computational tool, \comale, that can detect regions
and variants under selection E\&R experiments. Using extensive simulations,
we show that \comale\
outperforms existing methods in detecting selection, locating the
favored allele, and estimating model parameters.  Also, while
being computationally efficient, \comale\ provide means for estimating
populations size and hypothesis testing.

Many factors such as small population size, finite coverage, linkage
disequilibrium, finite sampling for sequencing, duration of the
experiment and the small number of replicates can limit the power of
tools for analyzing E\&R.  Here, by an discrete modeling, \comale\
estimates population size, and provides unbiased estimates of
$s,h$. It adjusts for heterogeneous coverage of pool-seq data, and
exploits presence of linkage within a region to compute composite
likelihood ratio statistic.

It should be noted that, even though we described \comale\ for small
fixed-size populations, the statistic can be adjusted for other
scenarios, including changing population sizes when the demography is
known. For large populations, transitions can be computed on sparse
data structures, as for large $N$ the transition matrices become
increasingly sparse. Alternatively, frequencies can be binned to
reduce dimensionality.

The comparison of hard and soft sweep scenarios showed that initial
frequency of the favored allele can have an nontrivial effect on the
statistical power for identifying selection. Interestingly, while it
is easier to detect a region undergoing strong selection, it is harder
to locate the favored allele in that region.

There are many directions to improve the analyses presented here.  In
particular, we plan to focus our attention on other organisms with
more complex life cycles, experiments with variable population size
and longer sampling-time-spans. As evolve and resequencing experiments
continue to grow, deeper insights into adaptation will go hand in hand
with improved computational analysis.


\section{Big Data}
\begin{definition}[ACID]\label{def:acid}
    \begin{description}
        \item[Atomicity:] An action will either completely fail or completely succeed, nothing  in between.
        \item[Consistency:] An action will either completely fail or completely succeed, nothing  in between.
        \item[Isolation:] Executing multiple actions simultaneously yields the same result as if all the actions
            were executed serially (that is, no messing up because of pararell execution).
        \item[Durability:] If an action is committed, it will remain in the system even if there are
            power losses, crashes or errors.
    \end{description}
\end{definition}

\begin{definition}[At-least-once ingestion]\label{def:atleastonce}
    If any message is lost, retransmit it. Thus, we can guarantee that a message will be received.
    There is a possibility of duplicates.
\end{definition}

\begin{definition}[At-most-once ingestion]\label{def:atmostonce}
    Every message will be sent, but at most one time.
    There is a possibility of duplicates.
\end{definition}

\begin{definition}[BASE]\label{def:base}
    An alternative way of analyzing DBMS.\@ It is less strict than \nameref{def:acid}.
    \begin{description}
        \item[Basic Availibility] database should work most of the time
        \item[Soft-state] replicas don't always have to be consistent
        \item[Eventual consistency] An update doesn't have to be seen by all peers right away
    \end{description}
\end{definition}

\begin{definition}[CAP theorem]\label{def:captheorem}
    Working with Big Data involves dealing with inputs so large that conventional methods do not work.
    This also involves that we have to compromise, not opting for ``optimal'' solutions like RDMS.\
    We have
    \begin{description}
        \item[Consistency:] Whether or not copies of data are the same across all nodes.
        For example, having a server in London with data X and another server in USA with data X', where X' is intended to be equal to X, but it is not.
        \item[Availibility:] Whether or not we can guarantee success or failure (alternatively: every request returns a non-error response).
        \item[Partition tolerance]:  If a node goes down, will the system continue to operate?
    \end{description}

    The CAP theorem states that any big-data system can only acheive two out of tree letters (CA, CP or AP).
\end{definition}

\begin{proof}
Assume a system has two nodes: A and B. A and B cannot communicate.
We write data X to node A and B. Then, we write $X'$ to node A. Following that we want to read $X$ from node B.
If node A and B do not talk together, we will not achieve consistency (node B doesn't know X is updated in A).
This also means we struggle with availibility: $X'$ has not been written to both nodes.
If we do let node A and B talk together, then node B depends on A, so we are not parition tolerant.
Hence, achieving all three letters is not possible in this case.
\end{proof}

\begin{definition}[Consistent hashing]\label{def:consistenthashing}
    A hashing scheme that scales well. When you add keys (expand domain), you don't need to
    remap the majority of the keys as you would have to in normal hashing.
    This can be thought of as extending a simple hashing algorithm $h(x) = \mod k$.
    When we expand the domain of the function, it will simply result in a lower hashvalues that can
    be used directly without changing other indices.
\end{definition}

\begin{definition}[Equi-depth histogram]\label{def:equidepthhistogram}
Take query:
\begin{verbatim} SELECT * FROM Person WHERE AGE > 24 \end{verbatim}.
Here, we can optimize the query by using an equidepth histogram:
instead of preparing to return each row, we first process the $AGE$ column and count
how many occurences we get. Then, we estimate parallelism, return size, etc, and return queries.
If $AGE$ is indexed, we could make estimates like  this in $O(1)$.
\end{definition}

\begin{definition}[Equi-width histogram]
    Similar to \nameref{def:equidepthhistogram}, except we fix bucket sizes.
    For example, for an age query we don't return the number of people above a threshold age,
    instead we bucketize, e.g. everyone under 24 in one bucket, everyone over 24 in another,
    or splitting per 5 years, etc.
\end{definition}

\begin{definition}[Exactly once ingestion]\label{def:exactlyonce}
    Every message is delivered once. No duplicates. Hard to implement (you need to retain data for a longer amount of time and have synchronization protocols).
\end{definition}

\begin{definition}[Fault Tolerance]
    In Big Data, there are two types of failure that can occur
    \begin{description}
        \item[Soft:] unexpected errors, such as null values or code error
        \item[Hard:] loss of nodes, etc.
    \end{description}
\end{definition}

\begin{definition}[Five V's of Big Data]\label{def:fiveV}
    \begin{description}
        \item[Volume:] How much data do we have?
        \item[Veracity:] Can we trust the data we have?
        \item[Variety:] What types of data do we read?
        \item[Value:] Is it really worth while to do big data solutions?
        \item[Velocity:] How fast is data coming in?
    \end{description}
\end{definition}

\begin{definition}[Log-structured Merge Tree]\label{def:LSM}
    (LSM) A data-structure optimized for indexed access with a high insert volume.
    It typically stores a lot of data in key-value form in-memory, and flushes out
    when exceeding a memory threshold.
\end{definition}

\begin{definition}[Map-side join]\label{def:mapjoin}
    Similar to \nameref{def:reducejoin}, but we avoid the second pass of sorting.
    A few conditions are necessary to do the join in the first stage of our job.
    \begin{enumerate}
        \item Data is already sorted by join key inside partitions
        \item Input datasets have same number of partitions (difficult if doing joins
            for $> 2$ tables).
    \end{enumerate}
\end{definition}

\begin{definition}[Reduce-side join]\label{def:reducejoin}
    On the map-side of things, we simply sort the data by join-key. E.g. a mapper A
    will give reducer A' all entries with key K. Then, reducer A' performs another sort,
    joining entries from two or more tables on K.
\end{definition}

\begin{definition}[Side-Data]\label{def:sidedata}
    Read-only data needed by a job to process a dataset.
\end{definition}

\begin{definition}[Uber task]\label{def:ubertask}
    A term used in MapReduce to denote a job so small that pararellizing it
    will not cause a benefit, thus leaving the job to be resolved in the same JVM
    as the master.
\end{definition}

\begin{definition}[Write-ahead log]\label{def:WAL}
    WAL:\@ write action before doing them. Important for \nameref{def:acid} properties.
\end{definition}


\documentclass[12pt]{amsart}
\usepackage[margin=0.5in]{geometry}
  % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{bsymb}
\usepackage{calculational}
\usepackage{ulem}
\usepackage{hyperref}
\usepackage{unitb}

\newcommand{\REQ}{\text{REQ}}

\begin{document}

\section{Strategy}

\begin{itemize}
  \item
\end{itemize}
\section{Model m0 --- Requests and non-deterministic handling}
  % \input{lock-free-deque/machine_m0.tex}
\begin{machine}{m0}
  \with{sets}
  \newset{\REQ}
  \newevent{add}{add}
  \newevent{handle}{handle}
  \[ \variable{ req : \set [\REQ] } \]
  \begin{description}
    \comment{req}{set of pending requests}
  \end{description}
  \[ \param{add}{ r : \REQ } \]
  \[ \param{handle}{ r : \REQ } \]
  \begin{align*}
      \initialization{m0:init0}{ req = \emptyset } \\
      \evguard{add}{m0:guard}{ \neg r \in req } \\
      \evbcmeq{add}{m0:act0}{req}{ req \bunion \{ r \} } \\
      \cschedule{handle}{m0:sch0}{ \neg req = \emptyset } \\
      \evguard{handle}{m0:grd0}{ r \in req } \\
      \evbcmeq{handle}{m0:act0}{req}{ req \setminus \{ r \} }
  \end{align*}

\noindent
\end{machine}
\section{Model m1 --- Version numbers and individual fairness}
  % \input{lock-free-deque/machine_m1.tex}
\begin{machine}{m1}
    \refines{m0}
  \[ \variable{ ver : \Int } \]
  \begin{description}
    \comment{ver}{ serial number of the current data structure state }
  \end{description}
  \[ \indices{handle}{ v : \Int } \]
  \promote{handle}{r}
  \removecoarse{handle}{m0:sch0}
  \removeguard{handle}{m0:grd0}
  % \removeact{handle}{m0:act0}
  \[\witness{handle}{r}{r \in req}\]
  \[\witness{handle}{v}{v = ver}\]
  \begin{align}
      \initialization{m1:init0}{ ver = 0 } \\
      \cschedule{handle}{m1:sch0}{ r \in req } \\
      \cschedule{handle}{m1:sch1}{ v = ver } \\
      % \evguard{handle}{m1:grd0}{ r = r0 } \\
      \evbcmeq{handle}{m1:act0}{ver}{ ver + 1 }
  \end{align}
\end{machine}
\section{Model m2 --- Specialized events}
  % \input{lock-free-deque/machine_m2}
\begin{machine}{m2}
  \refines{m1}
  \[ \variable{pshL,pshR,popR,popL : \set [\REQ]} \]
  \begin{description}
    \comment{pshL}{replaces $req$. Set of push\_left requests }
    \comment{pshR}{replaces $req$. Set of push\_right requests }
    \comment{popL}{replaces $req$. Set of pop\_left requests }
    \comment{popR}{replaces $req$. Set of pop\_right requests }
  \end{description}
  \begin{align}
    \initialization{m2:init0}{pshL = \emptyset} \\
    \initialization{m2:init1}{popL = \emptyset} \\
    \initialization{m2:init2}{pshR = \emptyset} \\
    \initialization{m2:init3}{popR = \emptyset}
  \end{align}
  \subsection{Specialize \emph{handle}}
  % \begin{align*}
    \refiningevent{handle}{handle:popL}{handle\_popL}
    \refiningevent{handle}{handle:popR}{handle\_popR}
    \refiningevent{handle}{handle:pushL}{handle\_pushL}
    \refiningevent{handle}{handle:pushR}{handle\_pushR}
  % \end{align*}
  \splitevent{handle}{handle:popL,handle:popR,handle:pushR,handle:pushL}
  \begin{align}
    \invariant{m2:inv0}{ pshL \bunion
        pshR \bunion
        popL \bunion
        popR
        = req } \\
    \invariant{m2:inv1}{ pshL \binter pshR = \emptyset } \\
    \invariant{m2:inv2}{ pshL \binter popL = \emptyset } \\
    \invariant{m2:inv3}{ pshL \binter popR = \emptyset } \\
    \invariant{m2:inv4}{ pshR \binter popL = \emptyset } \\
    \invariant{m2:inv5}{ pshR \binter popR = \emptyset } \\
    \invariant{m2:inv6}{ popL \binter popR = \emptyset } \\
    \cschedule{handle:pushL}{m2:sch0}{ r \in pshL } \\
    \evbcmeq{handle:pushL}{m2:act0}{pshL}{pshL \setminus \{ r \}} \\
    \cschedule{handle:pushR}{m2:sch0}{ r \in pshR } \\
    \evbcmeq{handle:pushR}{m2:act0}{pshR}{pshR \setminus \{ r \}} \\
    \cschedule{handle:popL}{m2:sch0}{ r \in popL } \\
    \evbcmeq{handle:popL}{m2:act0}{popL}{popL \setminus \{ r \}} \\
    \cschedule{handle:popR}{m2:sch0}{ r \in popR } \\
    \evbcmeq{handle:popR}{m2:act0}{popR}{popR \setminus \{ r \}}
    % \invariant{m2:inv1}{ popL \subseteq req } \\
    % \invariant{m2:inv2}{ pshR \subseteq req } \\
    % \invariant{m2:inv3}{ popR \subseteq req }
  \end{align}
  \[ \variable{ ppd : \set [\REQ] } \]
  \begin{align}
    \initialization{m2:init4}{ ppd = \emptyset } \\
    \evbcmeq{handle:popR}{m2:act1}{ppd}{ ppd \bunion \{r\} } \\
    \evbcmeq{handle:popL}{m2:act1}{ppd}{ ppd \bunion \{r\} }
  \end{align}
  \newevent{return}{return}
  \[ \indices{return}{ r : \REQ } \]
  \begin{align}
    \cschedule{return}{m2:sch0}{ r \in ppd } \\
    \evbcmeq{return}{m2:act0}{ppd}{ ppd \setminus \{ r \} }
  \end{align}
\subsection{Specialize \emph{add}}
    \refiningevent{add}{add:popL}{add\_popL}
    \refiningevent{add}{add:popR}{add\_popR}
    \refiningevent{add}{add:pushL}{add\_pushL}
    \refiningevent{add}{add:pushR}{add\_pushR}
  \splitevent{add}{add:popL,add:popR,add:pushR,add:pushL}
  \begin{align}
    \evbcmeq{add:popL}{m2:act0}{popL}{popL \bunion \{r\}} \\
    \evbcmeq{add:pushL}{m2:act1}{pshL}{pshL \bunion \{r\}} \\
    \evbcmeq{add:popR}{m2:act2}{popR}{popR \bunion \{r\}} \\
    \evbcmeq{add:pushR}{m2:act3}{pshR}{pshR \bunion \{r\}}
  \end{align}
\subsection{Data refinement}
  \removevar{req}
  \removeinit{m0:init0}
  \[\initwitness{req}{req = \emptyset}\]
  \removeact{handle:popR}{m0:act0}
  \removeact{handle:pushR}{m0:act0}
  \removeact{handle:popL}{m0:act0}
  \removeact{handle:pushL}{m0:act0}
  \removecoarse{handle:popR}{m1:sch0}
  \removecoarse{handle:pushR}{m1:sch0}
  \removecoarse{handle:popL}{m1:sch0}
  \removecoarse{handle:pushL}{m1:sch0}
  \removeguard{add:popR}{m0:guard}
  \removeguard{add:pushR}{m0:guard}
  \removeguard{add:popL}{m0:guard}
  \removeguard{add:pushL}{m0:guard}
  \begin{align*}
  \evguard{add:popR}{m2:grd0}
    { \neg r \in pshL \bunion pshR \bunion popL \bunion popR } \\
  \evguard{add:pushR}{m2:grd0}
    { \neg r \in pshL \bunion pshR \bunion popL \bunion popR } \\
  \evguard{add:popL}{m2:grd0}
    { \neg r \in pshL \bunion pshR \bunion popL \bunion popR } \\
  \evguard{add:pushL}{m2:grd0}
    { \neg r \in pshL \bunion pshR \bunion popL \bunion popR }
  \end{align*}
  \removeact{add:popR}{m0:act0}
  \removeact{add:pushR}{m0:act0}
  \removeact{add:popL}{m0:act0}
  \removeact{add:pushL}{m0:act0}
\end{machine}
  \newcommand{\OBJ}{\text{OBJ}}
\section{Model m3 --- The Contents}
  % \input{lock-free-deque/machine_m3}
\begin{machine}{m3}
  \refines{m2}
  \with{functions}
  \with{intervals}

  \newset{\OBJ}
  \[ \variable{ p,q : \Int } \]
  \[ \variable{ qe : \Int \pfun \OBJ } \]
  \begin{align}
    \invariant{m3:inv0}{ qe \in \intervalR{p}{q} \tfun \OBJ } \\
    \invariant{m3:inv1}{ p \le q } \\
    \initialization{m3:init0}{ p = 0 \land q = 0} \\
    \initialization{m3:init1}{ qe = \emptyfun }
  \end{align}
  \subsection{Push}
    \[\variable{insL : \REQ \pfun \OBJ} \]
    \[\variable{insR : \REQ \pfun \OBJ} \]
  \begin{description}
    \comment{insL}{parameter for the \emph{push left} operation}
    \comment{insR}{parameter for the \emph{push right} operation}
  \end{description}
  \begin{align}
    \invariant{m3:inv2}{ insL \in pshL \tfun \OBJ } \\
    \invariant{m3:inv3}{ insR \in pshR \tfun \OBJ } \\
    \initialization{m3:init2}{ insL = \emptyfun } \\
    \initialization{m3:init4}{ insR = \emptyfun }
  \end{align}
  \begin{align}
    \evbcmeq{handle:pushL}{m3:act0}{p}{p - 1} \\
    \evbcmeq{handle:pushL}{m3:act1}{qe}{ qe \2| (p \0- 1 \fun insL.r)} \\
    \evbcmeq{handle:pushR}{m3:act0}{q}{q + 1} \\
    \evbcmeq{handle:pushR}{m3:act1}{qe}{ qe \2| (q \fun insR.r)}
  \end{align}

  \[ \param{add:pushL}{ obj : \OBJ } \]

  \begin{align}
    \evbcmeq{add:pushL}{m3:act0}
      {insL}{ insL \1| r \fun obj } \\
    \evbcmeq{handle:pushL}{m3:act2}
      {insL}{ \{ r \} \domsub insL }
  \end{align}

  \[ \param{add:pushR}{ obj : \OBJ } \]

  \begin{align}
    \evbcmeq{add:pushR}{m3:act0}
      {insR}{ insR \1| r \fun obj } \\
    \evbcmeq{handle:pushR}{m3:act2}
      {insR}{ \{ r \} \domsub insR }
  \end{align}
  \[ \variable{ res : \REQ \pfun \OBJ } \]
  \[ \variable{ result : \OBJ } \]
  \[ \variable{ emp : \Bool } \]
  \subsection{Pop}
  \begin{align}
    \initialization{m3:init3}{ res = \emptyfun } \\
    \invariant{m3:inv4}{ res \in ppd \pfun \OBJ } \\
    \evbcmeq{return}{m3:act0}{res}{ \{r\} \domsub res} \\
    \evbcmsuch{return}{m3:act1}{result}{ r \in \dom.res \implies result' = res.r } \\
    \evbcmeq{return}{m3:act2}{emp}{ (r \in \dom.res) }
  \end{align}
  \splitevent{handle:popL}{handle:popL:empty,handle:popL:non:empty}
  \splitevent{handle:popR}{handle:popR:empty,handle:popR:non:empty}
  \refiningevent{handle:popL}{handle:popL:empty}{handle\_popL\_empty}
  \refiningevent{handle:popL}{handle:popL:non:empty}{handle\_popL\_non\_empty}
  \refiningevent{handle:popR}{handle:popR:empty}{handle\_popR\_empty}
  \refiningevent{handle:popR}{handle:popR:non:empty}{handle\_popR\_non\_empty}
  \[ \dummy{v : \Int} \]
  \replace{handle:popL}{m3:sch0}{m3:prog0}
  \begin{align*}
    & \progress{m3:prog0}{v = ver}{p = q \lor p < q}
    \refine{m3:prog0}{implication}{}{}
  \end{align*}
  \begin{align}
    \cschedule{handle:popL:non:empty}{m3:sch0}{ p < q } \\
    \cschedule{handle:popL:empty}{m3:sch0}{ p = q } \\
    \evbcmeq{handle:popL:non:empty}{m3:act0}{res}{ res \1| (r \fun qe.p) } \\
    \evbcmeq{handle:popL:non:empty}{m3:act1}{p}{p+1} \\
    \evbcmeq{handle:popL:non:empty}{m3:act2}{qe}{ \{p\} \domsub qe }
  \end{align}
  \replace{handle:popR}{m3:sch0}{m3:prog0}
  \begin{align}
    \cschedule{handle:popR:non:empty}{m3:sch0}{ p < q } \\
    \cschedule{handle:popR:empty}{m3:sch0}{ p = q } \\
    \evbcmeq{handle:popR:non:empty}{m3:act0}{res}{ res \1| (r \fun qe.(q\0-1)) } \\
    \evbcmeq{handle:popR:non:empty}{m3:act1}{q}{q-1} \\
    \evbcmeq{handle:popR:non:empty}{m3:act2}{qe}{ \{q-1\} \domsub qe }
  \end{align}
\end{machine}
\section{Model m4 --- Memory nodes}
\newcommand{\Node}{\text{Node}}
  % \input{lock-free-deque/machine_m4}
\begin{machine}{m4}
  \refines{m3}
\subsection{Data structure}
  \newset{\Node}
  \[ \variable{ rep : \Int \pfun \Node } \]
  \[ \variable{ item : \Node \pfun \OBJ } \]
  \[ \variable{ node : \set [\Node] } \]
  \[ \constant{ dummy : \Node } \]
  \begin{align}
    \invariant{m4:inv0}
      { rep \in \intervalR{p}{q} \tfun node } \\
    \invariant{m4:inv1}
      { item \in node \tfun \OBJ } \\
    % \invariant{m4:inv2}{ rep is injective }
    \invariant{m4:inv3}
      { \qforall{i}{\betweenR{p}{i}{q}}{ qe.i = item.(rep.i) } } \\
    \initialization{m4:init0}{ rep  = \emptyfun } \\
    \initialization{m4:init1}
      { \qexists{val}{}{ item \1= dummy \fun val } } \\
    \initialization{m4:init2}{ node = \{ dummy \} } \\
    \invariant{m4:inv9}
      { dummy \in node } \\
    \invariant{m4:inv10}
      { \neg dummy \in \ran.rep }
  \end{align}
\subsection{Pop}
  \begin{align}
    \evbcmeq{handle:popL:non:empty}{m4:act0}{rep}
      { \{ p \} \domsub rep } \\
    \evbcmeq{handle:popR:non:empty}{m4:act0}{rep}
      { \{ q-1 \} \domsub rep }
  \end{align}
\subsection{Push}
  \[ \variable{ nL : \REQ \pfun \Node } \]
  \[ \variable{ nR : \REQ \pfun \Node } \]
  \[ \variable{ new : \set [\Node] } \]
\paragraph{\eqref{m4:inv0}}
  \begin{align}
    \invariant{m4:inv4}{ nL \in pshL \pfun node } \\
    \invariant{m4:inv5}{ nR \in pshR \pfun node } \\
    \initialization{m4:init3}{ nL = \emptyfun } \\
    \initialization{m4:init4}{ nR = \emptyfun } \\
    \evbcmeq{handle:pushL}{m4:act0}
      {rep}{ rep \2| (p\0-1 \fun nL.r) } \\
    \cschedule{handle:pushL}{m4:sch0}
      { r \in \dom.nL } \\
    \evbcmeq{handle:pushR}{m4:act0}
      {rep}{ rep \2| (q \fun nR.r) } \\
    \cschedule{handle:pushR}{m4:sch0}
      { r \in \dom.nR }
  \end{align}
  \replace{handle:pushL}{m4:sch0}{m4:prog0}
  \replace{handle:pushR}{m4:sch0}{m4:prog1}
  \[ \dummy{ r : \REQ } \]
  \begin{align}
    \progress{m4:prog0}{ r \in pshL }{ r \in \dom.nL } \\
    \progress{m4:prog1}{ r \in pshR }{ r \in \dom.nR }
  \end{align}
\paragraph{\eqref{m4:inv3}}
  \begin{align}
    \invariant{m4:inv6}
      { \qforall{r}{ r \in \dom.nL }{item.(nL.r) = insL.r} }\\
    \evbcmeq{handle:pushL}{m4:act1}{nL}{ \{ r \} \domsub nL }
  \end{align}
  \begin{align}
    \invariant{m4:inv7}
      { \qforall{r}{ r \in \dom.nR }{item.(nR.r) = insR.r} }\\
    \evbcmeq{handle:pushR}{m4:act1}{nR}{ \{ r \} \domsub nR }
  \end{align}
\subsection{ \eqref{m4:inv10} }
  \begin{align}
    \invariant{m4:inv11}
      { \neg dummy \in \ran.nL } \\
    \invariant{m4:inv12}
      { \neg dummy \in \ran.nR }
  \end{align}
\subsection{New Progress Properties}
  \newevent{allocateL}{allocate\_left}
  \begin{align*}
    \refine{m4:prog0}{ensure}{allocateL}{ \index{r}{r' = r} }
  \end{align*}
  \[ \indices{allocateL}{ r : \REQ } \]
  \[ \param{allocateL}{ n : \Node } \]
  \begin{align}
    \evguard{allocateL}{m4:grd0}{ \neg n \in node } \\
    \assumption{m4:asm0}{ \neg \finite{\Node} } \\
    \invariant{m4:inv8}{ \finite{node} } \\
    \cschedule{allocateL}{m4:sch0}{ r \in pshL } \\
    \evbcmeq{allocateL}{m4:act0}{nL}{nL \1| (r \fun n)} \\
    \evbcmeq{allocateL}{m4:act1}{item}{item \1| (n \fun insL.r)} \\
    \evbcmeq{allocateL}{m4:act2}{node}{ node \bunion \{n\}}
  \end{align}
  \newevent{allocateR}{allocate\_right}
  \begin{align*}
    \refine{m4:prog1}{ensure}{allocateR}{ \index{r}{r' = r} }
  \end{align*}
  \[ \indices{allocateR}{ r : \REQ } \]
  \[ \param{allocateR}{ n : \Node } \]
  \begin{align}
    \evguard{allocateR}{m4:grd0}{ \neg n \in node } \\
    \cschedule{allocateR}{m4:sch0}{ r \in pshR } \\
    \evbcmeq{allocateR}{m4:act0}{nR}{nR \1| (r \fun n)} \\
    \evbcmeq{allocateR}{m4:act1}{item}{item \1| (n \fun insR.r)} \\
    \evbcmeq{allocateR}{m4:act2}{node}{ node \bunion \{n\}}
  \end{align}

\end{machine}
\begin{machine}{m5}
  \refines{m4}
  \[ \variable{ left, right : \Node \pfun \Node } \]
  \[ \variable{ LH, RH : \Node } \]
  \begin{align}
    \invariant{m5:inv0}{ left \in node \tfun node } \\
    \invariant{m5:inv1}{ right \in node \tfun node }  \\
    \invariant{m5:inv2}
      { p < q \1\implies LH = rep.p \land RH = rep.(q\0-1) }  \\
    \invariant{m5:inv3}
      { p = q \1\implies LH = dummy \land RH = dummy }  \\
    \invariant{m5:inv4}{ left.dummy = dummy }  \\
    \invariant{m5:inv5}{ right.dummy = dummy }  \\
    \initialization{m5:init0}{ left \1= (dummy \fun dummy) } \\
    \initialization{m5:init1}{ right \1= (dummy \fun dummy) } \\
    \initialization{m5:init2}{ LH = dummy \land RH = dummy }
  \end{align}

%   \begin{align}
    \invariant{m5:inv6}
      { \qforall{i}{\betweenR{p}{i}{q\0-1}}{ right.(rep.i) = rep.(i+1) } } \\
    \invariant{m5:inv7}
      { \qforall{i}{\betweenR{p}{i}{q\0-1}}{ left.(rep.(i+1)) = rep.i } }
%   \end{align}
% \subsection{Pop}
%   \begin{align}
    \invariant{m5:inv8}
      { left.LH = dummy } \\
    \invariant{m5:inv9}
      { right.RH = dummy } \\

%   \end{align}
\subsection{Push}
\subsection{Allocate}
\end{machine}
\end{document}


\subsection{16bit Timer}

This timer is more complex than SysTimer, but is only 16bit wide. It
can also generate interrupt on compare match and have pre-scaler. Clock
is hard-wired to CPU clock.

\subsubsection{Function}

Timer are clocked at same frequency as CPU and this cannot be changed. It
is counting from zero to up, until compare match with specified top occur. Then
interrupt request is generated, counter is set to zero and counting are restarted.

Timer have two registers, one for controlling and one with actual counter value. Reading
from registers are possible, writing too but when write is performed at counter
value register, counting is restarted from zero.

With control register you can set top value, enable and disable timer or interrupt.

\subsubsection{Registers}

All registers are listed in table \ref{tab:timer_reg_map}. Register TVRx is 16
bits wide and  contains  actual value of counter.

\begin{table}[h]
    \centering
    \begin{tabular}{|l|l|l|}
        \hline
        \textbf{Offset} & \textbf{Name} & \textbf{Purpose}            \\ \hline
        $+0$            & TCRx          & Timer X control register.    \\ \hline
        $+1$            & TVRx          & Timer X value register.      \\ \hline
    \end{tabular}
    \caption{16bit timer register map}
    \label{tab:timer_reg_map}
\end{table}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \bitrect{16}{32-\bit}
        \robits{0}{11}{}
        \rwbits{11}{3}{DIV}
        \rwbits{14}{1}{ien}
        \rwbits{15}{1}{ten}
    \end{tikzpicture}
    \begin{tikzpicture}
        \bitrect{16}{16-\bit}
        \rwbits{0}{16}{CMPM}
    \end{tikzpicture}
    \caption{TCRx register}
    \label{fig:tcr_reg}
\end{figure}

Register TCRx is control register of Timer X. You can set CMPM value,
to set top value of counting, you can enable whole timer here, enable
interrupts and set pre-scaler. In TCRx bits have following meaning:

\begin{itemize}
	\item \textbf{CMPM} 16bit TOP value
	\item \textbf{ten} enable timer
	\item \textbf{ien} enable interrupt
	\item \textbf{DIV} 3bit wide prescaler select
\end{itemize}

All possible configurations for pre-scaler are listed in table \ref{tab:tim_pres}.

\begin{table}[H]
    \centering
    \begin{tabular}{|l|l|l|l|}
        \hline
        \textbf{$DIV_2$} & \textbf{$DIV_1$} & \textbf{$DIV_0$} & \textbf{pre-scaler} \\ \hline
        0 & 0 & 0 & $F_{clk}$ \\ \hline
		0 & 0 & 1 & $\frac{1}{2} * F_{CLK}$  \\ \hline
		0 & 1 & 0 & $\frac{1}{4} * F_{CLK}$  \\ \hline
		0 & 1 & 1 & $\frac{1}{8} * F_{CLK}$  \\ \hline
		1 & 0 & 0 & $\frac{1}{16} * F_{CLK}$ \\ \hline
		1 & 0 & 1 & \textit{reserved}    \\ \hline
		1 & 1 & 0 & \textit{reserved}    \\ \hline
		1 & 1 & 1 & \textit{reserved}    \\ \hline
    \end{tabular}
    \caption{16bit timer pre-scaler}
    \label{tab:tim_pres}
\end{table}

\subsubsection{Hacking}

One can simply add another instances of 16bit timer if needed.
Interface consist from bus interface and one interrupt request signal.
When you wand add another timer, just remember to connect intrq to the
interrupt controller.

\begin{lstlisting}[language=VHDL, frame=single]
entity timer is
    generic(
        BASE_ADDRESS: unsigned(23 downto 0) := x"000000"
    );
    port(
        clk: in std_logic;
        res: in std_logic;
        address: in std_logic_vector(23 downto 0);
        data_mosi: in std_logic_vector(31 downto 0);
        data_miso: out std_logic_vector(31 downto 0);
        WR: in std_logic;
        RD: in std_logic;
        ack: out std_logic;
        intrq: out std_logic
    );
end entity timer;
\end{lstlisting}


\chapter{Unobserved Confounding}

05 - Causal Networks/chapters/08_unobserved_confounding.tex


\section{Methodology}
\label{sec: Methodology}
% In this section. First, we describe the dataset. Second we show how we collecting data, preprocessing data for the model and the last we describe our algorithm.

A dataset is created using twitter (a popular micro-blogging service where users create status messages called tweets) posts of electronic products. These tweets sometimes express opinions about different topics. Therefore, they are short messages full of slang words and misspellings (Fig.\ref{fig1}).  So we propose a method to automatically classifying sentiment (positive or negative) from a tweet. This is done in three phases. In the first phase preprocessing is done. Then a feature vector is created using relevant features. Finally using SVM classifiers, tweets are classified into positive and negative classes.

\begin{figure*}[!htb]
    \center{\includegraphics[width=1.4\columnwidth]{original_dataset.png}}
    \caption{\label{fig1} Original dataset}
\end{figure*}

\begin{figure*}[!htb]
    \center{\includegraphics[width=1.4\columnwidth]{formatted_2classes.png}}
    \caption{\label{fig3} Our dataset after preprocessing}
\end{figure*}

\subsection{Dataset}
\label{sec: Dataset}

The data used in this project for training and testing is SemEval-2014 Twitter dataset Task 9 Corpus \cite{nakov2016evaluation} which was collected within the SemEval-2014 Task 9 competition that was a part of the International Workshop on Semantic Evaluation. We used 80$\%$ of the dataset for training and the remaining 20$\%$ for testing.

\subsection{Preprocessing Data}
\label{sec: Preprocessing Data}

A tweet contains a lot of opinions about the data which are expressed in different ways by different users. So keyword extraction is difficult on twitter due to misspellings and slang words. To avoid this, a preprocessing step is performed before feature extraction. Preprocessing steps include several techniques following to select features from the raw data:

\begin{itemize}
    \item Remove all URLs (e.g. $www.xyz.com$), hash tags (e.g. $\#topic$), targets ($@$username), hyperlinks, numbers, repeated letters (more than 2 repeated letters) and punctuation marks.
    \item Removing stop words that add no sentiment value (articles, some prepositions, etc.).
    \item Stemming: reducing distinct words to their root form (stem).
    \item Removing infrequent words (words that appear less than $n_{min}$ number of times).
\end{itemize}

\subsection{Creation of Feature Vector}
\label{sec: Creation of Feature Vector}

\begin{figure}[!ht]
    \center{\includegraphics[width=1.0\columnwidth]{features.png}}
    \caption{\label{fig_feature_vector} Our features}
\end{figure}

For the unigram feature, there are usually a larger 500,000 different features. This is a very large number. It makes a model a higher variance. (Since the more complicated model has higher variance). It will need much more training data to avoid overfitting. Our dataset set contains almost 10 hundreds of sentences. This is a small number of examples. So we need to discard some useless features.

In this project, we use frequency-based feature selection which is the simplest way to do the feature selection. We just pick features (unigram words in our case) for each class with high-frequency occurrence in this class. In practice, if the number of occurrences of a feature is larger than some threshold (100 or 1000 in our experiments), this feature is a good one for that class (Fig.\ref{fig_feature_vector}). As we have seen in the result table, this simple algorithm increases about 0.03 of accuracy.

\subsection{Classification Technique}
\label{sec: Classification Technique}

There are different types of classifiers that are generally used for text classification which can be also used for twitter sentiment classification. But in this project, we focus on using  Support Vector Machine to do classify twitter sentiment.

SVM Classifier uses a large margin for classification. It separates the tweets using a hyperplane. SVM uses the discriminative function defined as

\begin{equation} \label{eq: svm}
g(X) = w^T \phi(X) + b
\end{equation}

$X$ is the feature vector, $w$ is the weights vector and $b$ is
the bias vector. $\phi()$ is the non-linear mapping from input space to high dimensional feature space. $w$ and $b$ are learned
automatically on the training set. Here we used different kernels for classification such as linear and radial basis functions. It maintains a wide gap between two classes.

\subsection{Automated Sentiment Classification System}
\label{sec: system}

After the training and testing process, we selected and saved the best model for later usage. After that, we built a web interface tool to aid in the manual user-defined query and then automated sentiment classification.

After typing the user-query (query will be directly done by Twitter API).  The saved model is loaded and used to classify the tweets (return from twitter api). The statistics of the number of positive and negative tweets also are also calculated. The classification labels is compared between our model and TextBlob (\cite{loria2014textblob}) model (Fig.\ref{automated_system}).

\begin{figure*}[!t]
    \center{\includegraphics[width=1.8\columnwidth]{searching_and_comparing.png}}
    \caption{\label{automated_system} Automated sentiment classification system}
\end{figure*}