\chapter{ The Problem of Pattern Prediction and the Proposed Approach }

\label{chapter:system}

\par In this chapter, we introduce our proposed approach for pattern prediction over multiple input event streams. We start by defining the problem that we address in this thesis, which is divided into two parts: the formal definition of the pattern prediction over an individual event stream, then it is extended to the case of multiple streams. We present the used base model for event pattern prediction, and our proposed approach to leverage the distributed online protocol to enable knowledge sharing between prediction models over multiple event streams. Furthermore, we provide an analysis of the theoretical aspects of the proposed approach.

\par We follow the general notation and terminology of \cite{agrawal2008efficient,schultz2009distributed,luckham2008power,alevizos2015complex,zhou_pattern_2015,kamp2014communication} to formalize the problem we tackle and our solution approach.

\section{Pattern Prediction over a Single Event Stream}

\par In this section, we define the first part of the problem that we tackle in this thesis, by defining the problem of pattern prediction over a single event stream. We also give an introduction of the base prediction model, which is built based on the event forecasting with pattern Markov chains method \cite{alevizos2017event}.

\subsection{Basic Problem Formulation}

We first define the input event and the stream of events as follows:
\begin{definition}
	Each event is defined as a tuple of attributes denoted by $e_i$, where
	$e_i = (id,type,\tau,a_1,a_2,\ldots,a_n)$, $type\in\Sigma$ is the event type attribute that takes a value from a set of finite event types/symbols $\Sigma$, $\tau\in\R$ represents the time when the event tuple was created,  the  $a_1,a_2,...,a_n$ are spatial or other contextual features (e.g., speed); these features vary from one application domain to another. The attribute $id\in\N$ is a unique identifier that connects the event tuple to an associated domain object.
\end{definition}

\begin{definition}
A stream $s=\langle e_1,e_3,...,e_t,...\rangle$  is a time-ordered infinite sequence of events.
\end{definition}

\par A user-defined pattern $\mathcal{P}$ is given in the form of a regular expression over a set of event types $\Sigma$ (i.e., alphabet). Let a word over $\Sigma$ be a sequence of event types, and the set of words over $\Sigma$ be a language $L$. Then $L(\mathcal{P})$ is the regular language defined by the regular expression ($\mathcal{P}$) \cite{hopcroft2006automata,nuel_pattern_2008,alevizos2017event},  where a regular expression is an event type $\in$ $\Sigma$, or defined using the following operators over regular expressions:

\begin{itemize}[noitemsep]
	\item \textit{sequence} that represents a concatenation of two regular expressions languages
	\item \textit{disjunction} i.e., union, which is the language that its words belong to one of the languages of the two regular expressions
	\item \textit{iteration} operation to define the set of all possible concatenation over a regular expression
\end{itemize}

More formally, a pattern is given through the following grammar:
\begin{definition}
$\mathcal{P} := E\ |\ \mathcal{P}_{1} ; \mathcal{P}_{2}\ | \mathcal{P}_{1} \vee \mathcal{P}_{2}\ |\ \mathcal{P}_{1}^{*}  $, where $E \in \Sigma$ is a constant event type. $;$ stands for sequence, $\vee$ for disjunction and $*$ for $\mathit{Kleene}-*$.
The pattern $\mathcal{P} := E$ is matched by reading an event $e_i$ iff $e_{i}.type = E$.
The other cases are matched as in standard automata theory.
\end{definition}

The $\mathcal{P}$ (regular expression) is encoded by a deterministic finite automaton (DFA). We define the DFA as following:

\begin{definition}[\cite{hopcroft2006automata}]
	$(\Sigma,Q,s,F,\delta)$ is a deterministic finite automaton (DFA)  where  $\Sigma$ a finite alphabet of event types, and $Q$ is a finite set of states, $s \in Q$ a start state and $F \subset Q$ represents all final states. $\delta: Q \times \Sigma \rightarrow Q$ is a transition function from a state to another state given an input event type, which is defined as recursive function $\delta(q,e_{1}e_{2}\ldots e_{d})=\delta(\delta(q,e_{1}e_{2}...e_{d-1}),e_{d})$. If $\delta(s,w) \in F$ of a word $w=e_{1}e_{2}\ldots e_{d} \in \Sigma^{*}$, then the word $w$ is said to be accepted by the DFA. In addition,  $\delta(q)^{-m}$ is the set of words of size $m$ defined as $\{w \in \Sigma^{m} \mid \exists q \in Q,\ \delta(p,w)=q \}$
\end{definition}

\par The problem setting can be summarized as follows: given a stream $s$ of low-level events and a pattern $\mathcal{P}$,
the goal is to estimate at each new event arrival the number of future events
that we will need to wait for until the pattern is completed (full match).

\subsection{Base Prediction Model}
\label{sec:Event-Forecasting-PMC}

\par In this thesis, we use the event forecasting with pattern Markov chains method \cite{alevizos2017event} to construct a pattern prediction model over an event stream. Next, we describe the details of this approach. We first provide an overview of the Patten Markov Chain framework \cite{nuel_pattern_2008}. Then, we describe how this framework is used to build a pattern prediction model \cite{alevizos2017event}.

\subsubsection*{Pattern Markov Chain (PMC)}

~\citet{alevizos2017event} proposed to employ the Pattern Markov Chain (PMC) \cite{nuel_pattern_2008} to build an online prediction model associated with a pattern over an event stream. The algorithm of constructing a PMC associated with a pattern $\mathcal{P}$ over a stream of events $s$ consists of the following steps:

\begin{itemize}[noitemsep]
	\item Build a DFA that accepts the regular expression $\Sigma^{*};\mathcal{P}$. Firstly, the regular expression $\Sigma^{*};\mathcal{P}$ is converted to non-deterministic finite automaton (NFA), then an equivalent DFA of the constructed NFA is computed using the subset construction \cite{hopcroft2006automata,alevizos2017event}. And the events stream $s$ is considered as the input word of the DFA.

\par Furthermore, if $\forall q \in Q$ of a DFA the all $\delta(q)^{-m}$ are sets of cardinality 1, then the DFA is called \textit{m-unambiguous}. ~\citet{nuel_pattern_2008} proposed a procedure to transform any DFA to an \textit{m-unambiguous} DFA, which is achieved by incrementally duplicate all states have m-ambiguity (i.e., $\vert\delta(q)^{-m}\vert > 1$).   Figure ~\ref{fig:dfa_adc} shows an associated DFA of a sequential pattern $\mathcal{P}=a ; d ; c$ over an alphabet $\Sigma=\{a,b,c,d\}$ of order 1 ($m=1$).

\input{dfa_figure}

%(note that the DFA has no dead states since we need to handle streams and not strings).

\item  Given a constructed \textit{m-unambiguous} DFA for the pattern $\mathcal{P}$, and  assuming that the input events stream $s=\langle e_1,e_3,...,e_t,...\rangle$ is a $m$-order homogeneous Markov sequence, where $m \geq 1$.  ~\citet{nuel_pattern_2008} showed that the sequence of states of the DFA that generated by consuming the input events stream $s$ is a first-order homogeneous Markov chain, which is represented by $\langle q_{0},q_{1},...,q_{t},...\rangle$, where $q_{0}=s$ and $q_{t}=\delta(q_{t-1},e_{t})$. We denote by \pmcmr the derived Markov chain associated with a pattern $\mathcal{P}$ that is called a Pattern Markov Chain (PMC) of order $m$ \cite{nuel_pattern_2008}. In other words, we perform a direct mapping of the states of the DFA to states of a homogeneous Markov chain and the transitions of the DFA to transitions of the Markov chain. Thus, the terms \textit{m-unambiguous} DFA of a pattern and the corresponding \pmcmr  will be used interchangeably.

\par Furthermore, the \pmcmr model is characterized by a $\vert Q \vert \times \vert Q \vert$ transition probability matrix $\Pi$ where $Q$ is the set of states of the \textit{m-unambiguous} DFA. Figure~\ref{fig:pmc} depicts the corresponding \pmcmr of order 1 for the generated DFA of Figure~\ref{fig:dfa_adc}.

%\par The next step is to derive a Markov chain that will be able to provide a probabilistic description of the DFA's run-time behavior.
%\par Towards this goal, we use Pattern Markov Chains, as was proposed in \cite{nuel_pattern_2008}.
%, it can be shown that there is a direct mapping of the states of the DFA to states of a Markov chain and the transitions of the DFA to transitions of the Markov chain.
%\par The transition probabilities of the Markov chain are the occurrence probabilities of the various event types.
%On the other hand, if the occurrence probabilities of the events are dependent on some of the previous events  seen in the stream (i.e., the stream is generated by an $m^{th}$ order Markov process), we might need to perform a more complex transformation
%(see \cite{nuel_pattern_2008} for details)
%in order to obtain a ``proper'' Markov chain.
%The transition probabilities are then conditional probabilities on the event types.
%In any case,
%we call such a derived Markov chain a Pattern Markov Chain (PMC) of order $m$
%and denote by \pmcmr , where $\mathcal{P}$ is the initial pattern and $m$ the assumed order.

\end{itemize}

\input{dfa_mc_figure}
%\end{comment}

\subsubsection*{Constructing the Pattern Prediction Model}
\label{sec:pmc_prediction}

~\citet{alevizos2017event} proposed to use the constructed \pmcmr to build a probabilistic prediction model that describes the DFA's run-time behavior. The method is based on calculating the \textit{waiting-time} distributions. Given a specific state of the \pmcmr, a \textit{waiting-time} distribution provides the probability of reaching an absorbing state in $n$ transitions from the current state. So by mapping the final states of the DFA to absorbing states of the \pmcmr by adding self-loops with probabilities equal to $1.0$. Therefore, we can calculate the probability of reaching a final state in $n$ transitions, which means predicting a full match of the defined pattern $\mathcal{P}$.

\par We denote by $W_{\mathcal{P}}(q)$ the waiting-time random variable that represents the number of transitions from a current state $q$ of DFA to reach a final state \cite{alevizos2017event}, where it also represents the expected number of future events from the current time event to a full match of the pattern $\mathcal{P}$, which is given by

\begin{equation*}
W_{\mathcal{P}}(q)=inf\{n: q_{0},q_{1},...,q_{n}, q_{0}=q, q \in Q \backslash F, q_{n} \in F\}
\end{equation*}

where $Q$ is the set of states of the DFA, and $F$ the set of the  all final states. However, the \textit{waiting-time} distribution of the $W_{\mathcal{P}}(q)$ random variable can be computed based on the transition probability matrix $\Pi$ of the \pmcmr, where it has $h$ non-final states and $d$ final states (absorbing states) \cite{alevizos2017event}, then the distribution is calculated by the following equation

\begin{equation*}
P(W_{\mathcal{P}}(q)=n)=\boldsymbol{\xi_{q}}\boldsymbol{N}^{n-1}(\boldsymbol{I}-\boldsymbol{N})\boldsymbol{1}
\end{equation*}
where $\boldsymbol{N}$ is $h \times h$ matrix that obtained by re-arranging the transition matrix $\Pi$ to include transitions between the non-final states of the DFA, $\boldsymbol{I}$ is an identity matrix of size $h \times h$, and  $\boldsymbol{1}$ is a $h$-dimensional vector of ones. The $\boldsymbol{\xi_{q}}$ is $1 \times h$ row of elements that contains zeros except $1.0$ in the cell corresponding to $q$.
\par Finally, the model provides the prediction reports in the form of intervals i.e.,  $I=(\mathit{start},\mathit{end})$. Which means that the DFA is expected to reach a final state  after future transitions (number of events $n$) between $\mathit{start}$ and $\mathit{end}$ with probability $P(I)$ at least some constant threshold $\theta_{p}$ that defined by the user, given by:
\begin{equation*}
P(I)=\sum_{n \in I}{P(W_{\mathcal{P}}(q)=n)}\ where\  P(I) \geq \theta_{p}
\end{equation*}
where $P(I)$ equals the sum of probabilities of all number of future events $n$ that fall within $I$ ($start\leq n\leq end$). Furthermore, the length of the intervals ($spread(I) = end - start$)  can be restricted to not be greater than a maximum spread threshold $\theta_{s}$ ($spread(I)\leq \theta_{s}$)

\begin{figure}[H]
	\begin{centering}
		\center
		\includegraphics[width=\textwidth,keepaspectratio]{chapters/figures/new_wt.png}

		\caption{Waiting-time distribution for
			$\mathcal{P}=a ; d ; c$, $\Sigma=\{a,b,c,d\}$, and $m=1$.}
		\label{fig:wt1}
	\end{centering}
\end{figure}

\par These intervals are estimated by a single-pass algorithm that scans a waiting-time distribution and finds the smallest (in terms of length) interval that exceeds the $\theta_{p}$ and has a spread  not greater that $\theta_{s}$. For example, Figure~\ref{fig:wt1} shows the \textit{waiting-time} distributions for all non-final states of the DFA presented in Figure~\ref{fig:wt1}, and the generated intervals are depicted in Figure~\ref{fig:predictionsIntervals}.

\begin{figure}[H]
	\begin{centering}
		\center
			\includegraphics[width=\textwidth,keepaspectratio]{chapters/figures/new_prediction_intervals.png}

		\caption{Example of the computed prediction intervals for
			$\mathcal{P}=a ; d ; c$, $\Sigma=\{a,b,c,d\}$, $m=1$, $\theta_{p}=0.5,\ and\ \theta_{s}=20$.}
		\label{fig:predictionsIntervals}
	\end{centering}
\end{figure}

\par The proposed method assumes that the transition probability matrix $\Pi$ is available to build the prediction intervals. However, this is not true in the real-world applications.
Therefore, it is essential to learn the values of the \pmcmr's transition probability matrix in order apply this method. One common way, is to use the maximum-likelihood estimator to learn the transition probabilities as illustrated in Section ~\ref{sec:theoretical}.

\par This model is performing the learning over an event stream $s$, which is usually has a slow convergence to a sufficiently good model. Where in the case of multiple event streams, a unique model associated with each stream is needed. Accordingly, we present in this work, a technique to share information among the \pmcmr predictors over multiple input event streams (i.e., distributed learning of the transition probability matrix).

\section{Pattern Prediction over Multiple Event Streams}

We extend the pattern prediction problem over a single event stream to the case of multiple distributed input event streams, where there is an associated prediction model for each event stream. We further present our proposed cooperative learning based approach by using a distributed online prediction protocol \cite{kamp2014communication} to synchronize the distributed prediction models in a communication-efficient manner.

\subsection{Extended Problem Formulation}

\par In this section, we extend the problem of pattern prediction over a single event stream to consider the setting where there are multiple input event streams. Let $O = \{ o_1, ..., o_k\}$ be a set of \emph{$K$} objects/entities (e.g., moving objects), each of which is generating an event stream $s_i$, hence, we have a set of real-time input event streams $S = \{ s_1, ..., s_k\}$, where these streams are produced independently from the same distribution.

 \par $\mathcal{P}$ is a user-defined pattern given in the form of regular expression, which is monitored in every stream $s_i \in S$. Then, we have a system consists of \emph{$K$} local predictor (learner) nodes $n_1,n_2\dots,n_k$, each of which receives an input event stream $s_i\in S$ that is associated with a single object $o_i \in O$.

\par  The goal is to provide online predictions about the completion of the pattern $\mathcal{P}$ within each stream $s_i$. Each node maintains a local prediction model $f_i$. Then for each new arriving event tuple  $e_t \in s_i$, the $f_i$ model provides an online prediction interval about the future full match of the pattern $\mathcal{P}$.

\par In other words, we have separated running instances of an online prediction algorithm on distributed nodes for \emph{$K$} input event streams. For example, massive streams of events that describe trajectories of group of moving vessels in the context of maritime monitoring, where there is one predictor node for each vessel's event stream.

  \par In this work, we employ the \pmcmr models on the distributed predictor nodes, each model is associated with a particular event stream. While these predictors can work in isolation with no communication with other local predictors, such non-cooperative way leads to inefficiency in terms of learning convergence and predictive performance among all predictors. Thus, in this work, we propose to use the distributed online prediction protocol \cite{kamp2014communication} to enable the communication among the local predictors to synchronize their models.
%
%The defined pattern $\mathcal{P}$ is monitored over each event stream $s_i$  by a  predictor nodes  $n_i$  that maintains a local prediction model $f_i$, where there is one node for each vessel's event stream.  The prediction model $f_i$ gives the ability to provide an online predictions about when the pattern will be completed in the form of an expected number of future events before a full match does occur.

\subsection{The Proposed Method}
\label{sec:proposed_approach}

\par This work proposes a scalable and distributed prediction system for user-defined patterns over multiple massive input event streams. The system  uses the PMC forecasting method \cite{alevizos2017event} as the base prediction model, where there is a \pmcmr model associated with each input event stream $s_i$ on a predictor node $n_i$. And it employs the distributed online learning protocol \cite{kamp2014communication} to exchange information among the distributed predictors of the input event streams, which allows to synchronize the parameters of their prediction models, which are represented by the transition matrices.

%
%Thus, the prediction model $f_i$ on a predictor node is represented by \pmcmr and its associated DFA.

\subsection*{Distributed Online Learning for Pattern Prediction}

\par In what follows, we describe the details of adapting the distributed online learning protocol \cite{kamp2014communication} with the pattern prediction (\pmcmr) models. This protocol provides a communication-efficient dynamic synchronization scheme based on a periodic static scheme.

\par Algorithm~\ref{algonline:dol} presents the distributed online prediction protocol by dynamic model synchronization on both the predictor nodes and the coordinator. We refer to the PMC's transition matrix $\boldsymbol{\Pi}_i$ on predictor node $n_i$ by $w_i$, which represents the parameters of the local prediction model $f_i$.

\par That is, when a predictor $n_i:\ i \in[k]$ observes an event $e_j$ it revises its internal model state ($w_i$) and provides a prediction report. Then it checks the local conditions, by checking the local model divergence from a reference model $w_r$ after consuming a predefined number of events (batch size $b$), to decide whether there is a need to synchronize its local model with the coordinator [or not]. $w_r$ is maintained in the predictor node as a copy of the last computed aggregated model $\hat{w}$ from the previous full synchronization step, which is shared among all distributed predictors.
%define delta%
 \par Let $\Delta$ be a predefined divergence threshold parameter. Then, by monitoring the local condition $(\|w_i - w_r\|^2 > \Delta)$ on all local predictors, we have a guarantee that if none of the local conditions is violated, the divergence (i.e., variance of local models $\delta(w)=\frac{1}{k} \sum_{j=1}^{k}\|w_i - \hat{w}\|^2$) does not exceed the threshold $\Delta$ \cite{kamp2014communication}. This is the key point of this protocol that reduces the communication overhead in comparison to the static based communication scheme, where the predictors always communicate periodically after observing a fixed number ($b$) of events ~\cite{dekel2012optimal}.

\par On the other hand, the coordinator receives the parameters of prediction models from the predictor nodes that requested for model synchronization (violation). Then it tries to keep incrementally querying other nodes for their local prediction models until reaching out all nodes have been queried, or the variance of the aggregated parameters $\hat{w}$ that is computed from the already received models is less or equal than the divergence threshold $\Delta$. The coordinator uses a synchronization operation to compute the parameters of a global model  $\hat{w}$. Finally, $\hat{w}$ is sent back to the predictor nodes that sent their models after the violation or have been queried by the coordinator.

\begin{algorithm}[h]
	\caption{Communication-efficient Distributed Online Learning~\cite{kamp2014communication} .}
	\begin{algorithmic}[1]

		\Statex \Indm  \textbf{Predictors:}
		\Statex \Indp node $n_i$: at observing event $e_j$

		\Statex \Indp update the parameters of the local prediction model $w_i$ and provide a prediction interval $I$ \;

		\Statex \If {$j\mod b = 0\ and\ \|w_i - w_r\|^2 > \Delta$}
		\Statex send  $w_i$ to the Coordinator (violation) \;
		\Statex \Indm \textbf{Coordinator}:
		\Statex \Indp receive parameters of local models with violation
		 $B=\{w_i\}_{i=1}^m$ \;

		\Statex \While{$|B| \neq k $ and $\frac{1}{|B|} \ \sum_{w_i\in  \Pi}\|w_i - \hat{w}\|^2 > \Delta$}{

			 \Statex  \hspace{\algorithmicindent} add other nodes that have not reported violation for \Statex \hspace{\algorithmicindent} their models $ B \gets \{w_l : w_l \notin B\ and\ l \in [k]\}$    \;
			\Statex  \hspace{\algorithmicindent} receive models from nodes in $B$\;
	}
        \Statex
		\Statex compute a new global model $\hat{w}$ \;
		\Statex send $\hat{w}$ to all the predictors in $B$ and set $w_{1}\dots w_{m}=\hat{w} $\;
		\Statex \If {$|B| = k$}{
		\Statex  \hspace{\algorithmicindent} set a new reference model $w_r	\gets \hat{w}$ \; }

	\end{algorithmic}
	\label{algonline:dol}
\end{algorithm}

\par  We use this protocol for the pattern prediction models, which are internally based on the \pmcmr. This allows the distributed \pmcmr predictors for multiple event streams to synchronize their models (i.e., the transition probability matrix of each predictor), and learn a shared model in a communication-efficient way.

\subsection*{Learning a Global Transition Probability Matrix}

\par We propose a \textit{synchronization operation} for the transition probability matrices ($w_i=\boldsymbol{\Pi}_i :i \in[k]$) of the $k$ distributed \pmcmr predictors. The operation is based on distributing the maximum-likelihood estimation \cite{anderson1957statistical} for the transition probabilities of the \pmcmr models.
\par Let $\pi_{i,j}$ be the transition probability from state $i$ to state $j$
and $n_{k,i,j}$ represents the number of transitions from state $i$ to state $j$ on node $k$. Then, the global estimation $\hat{\pi}_{i,j}$ for $p_{i,j}$ is described by:

\begin{equation}
\label{eq:dis_pi_estim}
\hat{\pi}_{i,j} = \frac{\sum_{k \in K} n_{k,i,j}}{\sum_{k \in K} \sum_{l \in L} n_{k,i,l}}
\end{equation}

That is,  $\hat{\Pi}$ is the global transition probability matrix that will be shared among all \pmcmr predictors.
\par Moreover, we measure the divergence of the local models from the reference model $\|w_i - w_r\|^2$ by calculating the sum of square difference between the local transition probability matrix $\Pi_i$ and the reference transition matrix $\Pi_r$:
\begin{equation*}
\label{eq:dis_pi_varinace}
\|w_i - w_r\|^2=|\Pi_i - \Pi_r\|^2=\sum_{l,j} (\pi{i,l,j} -\pi{r,l,j})^2
\end{equation*}

Where $\pi{i,l,j}$ is the transition probability from state $l$ to state $j$ in the $\Pi_i$, and the $\pi{r,l,j}$ the reference transition probability.
\input{chapters/theoretical}

\section{Summary}

\par In this chapter, we addressed the problem of pattern prediction over \emph{$K$} event streams by extending the case of predicting  event patterns over a single stream, where there are \emph{$K$} distributed predictors.  We also described the used prediction model on each predictor, which is based on the event forecasting with pattern Markov chains method \cite{alevizos2017event}. We presented our approach to adapt the distributed online learning protocol \cite{kamp2014communication} that allows the predictors to synchronize their models in a communication-efficient way.
\par Additionally, we provided the theoretical analysis of our approach by deriving a probabilistic guarantee on the learning efficiency.

 \par In next chapter, we will introduce the high-level architecture of our pattern prediction system, which is composed of distributed \pmcmr predictors \cite{alevizos2017event} that communicate their models to a coordinator node based a dynamic synchronization scheme \cite{kamp2014communication}. The coordinator computes a global model using the proposed synchronization operation (Equation~\ref{eq:dis_pi_estim}) and shares among the predictors.

 \par We will also present the implementation details of the system based on Apache Flink and Apache Kafka. We will describe the different modules  that we implemented to provide the functionalities of the system in a modular way.


\chapter{Testing Framework Implementation}
\label{chap:implementation}

This chapter describes the modelisation of the problem, the block building
strategies that have been implemented in order to validate the correctness of
the proposed model, and the methodology that has been employed to test the
framework.

\section{Modelisation}
\subsection{Metrics}
\FloatBarrier
A way to rationally compare strategies is needed in order to discuss their
relative strengths and weaknesses. Three main characteristics will be used for
that:
\begin{itemize}
        \item latency;
        \item number of nodes;
        \item overhead.
\end{itemize}

\subsubsection{Latency}
The latency is the number of messages required to finalize a block and is a
measure of a blockchain's liveness. Latency should be as close to \(2\)
 so as to reach finality as soon as possible and the closer the latency is to the ideal
value, the more alive the system is as transactions take less time to be
confirmed. The minimal value for latency is \(2\) because it is the minimal
number of steps to finalize a block, as seen in \ssec{ssec:finality}:Finality.

\subsubsection{Number of nodes}
The number of nodes in the validator set and should be as high as possible to
guarantee decentralization and safety.

\subsubsection{Overhead}
The overhead is the number of messages that are sent over the network between
one step of the consensus and the next. It should be as low as possible to keep
the costs in bandwidth low, the minimum being 1 as at least one message is
needed to move consensus forward.

\subsubsection{Example}

\begin{figure}[h]
	\centering
	\includegraphics[width=\columnwidth]{metrics-example}
  %\captionsetup{justification=centering}
    \caption{Metrics computation example. At time \(t_1\), \(m_1\) is finalized
    (left). At time \(t_2\), \(m_2\) is finalized. The latency \(l_2\) for \(m_2\) is the difference
    of height between \(t_1\) and \(t_2\), worth \(4\) in this case, and its
    overhead \(o_2\) is worth \(3\) and is the number of messages sent between the
    finalization of the previous message (\(m_1\)) and \(m_2\).}
	\label{fig:metricsSchema}
\end{figure}

\fig{fig:metricsSchema} portrays how the metrics are computed: the left half of
the Figure is the initial states of the system at time \(t_1\) where message
\(m_1\) is finalized. The other half of the Figure is the same system at time
\(t_2\) when \(m_2\) is finalized. The latency of \(m_2\) is \(l_2\)
-represented by the difference in heights between \(m_3\) and \(m_2\)- which is
worth 4 in this case. The overhead is measured by counting the number of
messages that have been sent between \(t_1\) and \(t_2\), worth 3.

\FloatBarrier

\subsection{Basic model}
\label{ssec:model}
The proposed variables can be considered as forming a trade-off triangle or trilemma.
As \gls{cbc} Casper does not make assumptions on timing, sources, contents,
destinations of the messages that are exchanged, the whole trade-off space can
be explored by varying any of those parameters.

The first strategy evaluation model which represents such a trilemma that was
chosen is:

\[1 = s_n \cdot \frac{1}{n} + s_l\cdot l + s_o\cdot o\]

This model binds each 3 variable \(n\) (number of nodes), \(l\) (latency) and
\(o\) (overhead) to their score \(s_{variable}\).

The higher the score \(s_x\) is, the more its related variable is dominant in
the strategy and therefore the closer to a corner of the triangle the strategy
is.  \(\frac{1}{n}\) is used because a large number of nodes is wanted.  The
basic model has linear dependencies between scores and variables and is probably
highly inaccurate but serves as a decent starting point to analyze the
simulation dataset.

\subsection{Model Evaluation}
After running the strategies in the simulation environment, metrics for \(n\),
\(l\) and \(o\) have been recorded. Then, for multiple runs, a linear regression
has been performed in order to find the scores \(s_x\).

\section{Strategies}
\label{sec:strategies}

The following strategies were proposed in order to visit known parts of the
trade-off triangle:
\begin{itemize}
        \item round-robin;
        \item double round-robin;
        \item triple round-robin;
        \item maximal overhead;
        \item arbitrary.
\end{itemize}
These strategies should allow one to visit the whole triangle and to discuss
their respective strength and weaknesses. The following sections describe the
strategies as well as their expected locations in the triangle.

\begin{figure}[h]
	\centering
	\includegraphics[width=0.8\columnwidth]{all_strats}
  %\captionsetup{justification=centering}
    \caption{Strategies examples with 4 validators. Top-left: Round-Robin.
    Top-right: Double Round-Robin. Bottom-left: Maximal Overhead. Bottom-right:
    Arbitrary. Arrows here show justifications for all messages.}
	\label{fig:allStrats}
\end{figure}

\subsection{Round-Robin}
Nodes send messages one after the other in a fixed order (\fig{fig:allStrats},
top-left). The expected overhead for this strategy should be the lowest of all
the strategies as only one message is sent at a time and each sent message
should finalize a new block.  The latency, on the other hand, should be quite
high because as seen in \ssec{ssec:cbc} \(50\%\) of the validators have to
acknowledge the messages to finalize them. Since the Round-Robin has a fixed
rotation order for the message production, finalization happens only a rotation
and a half later, where the first rotation acknowledges the message's existence
and the subsequent half rotation confirms acknowledgment.

\subsection{Double Round-Robin}
In this setting, two nodes send messages at the same time, in a fixed order
(\fig{fig:allStrats}, top-right). If the two nodes that send messages at the
same step are as far as possible from each other in the set of validators, the
latency to finality is supposedly half as much as the simple Round-Robin
strategy, as the whole validator set sends a message in half the number of
steps. The overhead is however doubled because two messages are sent at each
step.

\subsection{Triple Round-Robin}
This strategy is similar to the Double Round-Robin, except that three nodes send
a message at each step. It was decided to add this strategy at a later stage of
the project in order to have intermediate comparison points between the Double
Round-Robin and the Maximal Overhead strategy. As for the Double Round-Robin
strategy, the latency should be divided by three compared to the simple
Round-Robin and the overhead should be three times higher.
Note that a \(N\)-parallelized round-robin could be implemented in order to change the
position in the trade-off triangle but it would lose meaning for a number of
nodes smaller than \(N\) so there is no quadruple (or bigger) round-robin
implementation.

\subsection{Maximal Overhead}
This strategy is the most expensive in terms of bandwidth; at each step, each
validator sends a message to all others(\fig{fig:allStrats}, bottom-left). This
example strategy should give a baseline value for the maximum overhead that is
reachable in the tradeoff triangle. It also minimizes the latency as only two
steps are needed in order to finalize a block. This strategy is similar to the
pBFT\cite{pBFT} protocol because each node sends a message to every other one
over two steps.

\subsection{Arbitrary}
The Arbitrary strategy is the simplest to think of: pick a sender at
random(\fig{fig:allStrats}, bottom-right).  Using fixed probability density
functions, one node is arbitrarily selected at each step to create a message.
This strategy could be modified in the same way the simple Round-Robin has
evolved into the Double and Triple Round-Robin strategies to show the evolution
of the metrics with the number of messages sent at each step.

\subsection{Bottom-up strategies}
\label{ssec:bottomUpStrats}
All the previous strategies except Arbitrary require an overarching view
(``top-down'' view) of the system and the nodes to be synchronized.
Bottom-up strategies are more likely to be implemented from a real-life point of
view. Each node has its own view of the messages and acts in its own interest
based on that view.
For example a node could keep track of the whole state of the finalization of
messages, and only publish a message itself when it facilitates the
finalization. That kind of strategy has not been implemented but the testing and
scoring framework allows one to easily add them.
Such strategies should implement incentives for the nodes not to spam the
network and include mechanisms to slash nodes not following some rules the
strategies dictate.

\section{Methodology}
Over the duration of this thesis, the \texttt{core-cbc} library included a
test framework called \texttt{proptest}. The testing framework that has been
implemented includes ways to simulate the behavior of the Casper protocol over
multiple nodes and thousands of blocks. At the time of
writing, the simulations do not include network latencies.

\subsection{\texttt{proptest}}
\label{ssec:proptest}
The \texttt{proptest} implementation is able to run blockchain simulations off
the following parameters:
\begin{itemize}
    \item Number of validators;
    \item Terminating condition;
    \item Sender strategy;
    \item Receiver strategy.
\end{itemize}

\subsubsection{Number of validators}
The number of nodes that can validate blocks.

\subsubsection{Terminating condition}
The terminating condition is a predicate that tells whether or not the simulation has
reached an end. In this case, the end of the simulation is reached when at least
one node finds a safety oracle for a blockchain that has a height of 4. This
height was chosen because it offered a balance between the computation speed and
the variance in output values. If the chosen value was smaller the generated
blockchains would all look alike and some side effects would have been observed
near the genesis block whereas a bigger value would imply more computation time.

\subsubsection{Sender strategy}
The sender strategy selects one or more nodes that will create new messages and
forward them to the rest of the network. All the basic strategies that have been
presented in \sect{sec:strategies} are implemented as Sender strategies.
New strategies (including bottom-up ones) can be easily implemented as well.

\subsubsection{Receiver strategy}
Receiver strategies select a set of validators that receive messages created by
Sender strategies. Three strategies have been implemented for now:
\begin{itemize}
        \item All receivers;
        \item Half receivers;
        \item Some receivers.
\end{itemize}

The \textit{all receivers} strategy broadcasts messages to each other validator.
The \textit{some receivers} strategy sends a message to \(1\) or more validator,
using a uniform probability density function.  As of now, none of the
implemented strategies are a good modelisation of a typical Ethereum network and
this will be fixed at a later iteration.  Nonetheless, these strategies offer
two extreme points on the spectrum of the network topology: a fully connected
one without latency (\textit{all receivers}), and a random one (\textit{some
receivers}) and will be both taken into account to compare the sender
strategies.
The \textit{half receivers} strategy sends a message to half the validator set,
chosen at random which has been implemented at a later stage of the project
because the \textit{some receivers} strategy ended up modifying multiple
parameters at once rendering the analysis of the output too complex for the
proposed model and tools.

\subsection{Metrics measurements}
\label{ssec:metricsMeasurements}
Metrics are not computed as-is in the \texttt{proptest} implementation. Simpler
data are logged into \texttt{csv} files, and are processed by a \texttt{Python}
script. The recorded data are:
\begin{itemize}
    \item the number of nodes;
    \item the identity of the current node;
    \item the height of the chain;
    \item the height of the highest finalized block;
    \item and the total number of messages that have been sent.
\end{itemize}
This two step processing enabled the generation of
data before having a precise definition of the metrics. It also permits to vary
the way metrics are recorded. For example, from the same log file, you could
extract metrics only when the last step of the consensus is reached, or have an
average of the metrics at each step of the consensus.


\chapter{Background}
\label{chap:background}

In this chapter the needed background for this work will be shown.
Therefore, first the AutoML concept in general will be explained.
Then the optimization techniques that are used in AutoML systems will be shown.
These optimization techniques require metrics as input.
So, the metrics that are available for clustering will be presented.
Afterwards the concept of Meta-learning will be described.

\section{AutoML}
The problem of \gls{AutoML} is to produce automatically, so without human input, test set predictions for a given test set within a resource budget $b$.
This can be formally defined as shown in \Cref{def:automl}.

\begin{definition}[\gls{AutoML} problem]
\label{def:automl}
For $i = 1, \dots, n+m$ let $x_i \in \mathbb{R}^d$ denote a feature vector and $y_i \in Y $ the corresponding target value.
Given training data $D_{train} = \lbrace (x_1, y_1), \dots, (x_n, y_n) \rbrace $ and the feature vectors $x_{n+1, \dots, n+m}$ of a test dataset $D_{test} = \lbrace (x_{n+1}, y_{n+1}), \dots, (x_{n+m}, y_{n+m})  \rbrace$ drawn from same underlying distribution, as well as a ressource budget $b$ and a loss metric $\mathbb{L}(\cdot, \cdot)$, the \gls{AutoML} problem is to automatically produce test set predictions $\hat{y}_{n+1}, \dots, \hat{y}_{n+m}$ such that the loss
\begin{equation*}
    \frac{1}{m} \sum_{j=1}^m \Lcurv(\hat{y}_j, y_j)
\end{equation*}
is minimized.
\end{definition}

Many \gls{AutoML} systems do not directly address the problem described in \Cref{def:automl}, but they see the \gls{AutoML} problem as \gls{CASH} problem \cite{Feurer2015EfficientLearning,  Kotthoff2017Auto-WEKAWEKA}.
This is because the main problems of \gls{AutoML} are that first there is no single algorithm that performs best on every dataset and the choice of hyperparameter is crucial for the performance of an algorithm.
The \gls{CASH} problem is formally defined in \Cref{def:cash}.

\begin{definition} [\gls{CASH} problem]
\label{def:cash}
Let $A^(1), \dots, A^(R)$ be a set of algorithms, where the hyperparameters of each algorithm have the domain $\Lambda^(i)$.
Alos, let $ D_{train} =  \lbrace (x_1, y_1), \dots, (x_n, y_n) \rbrace$ be a training set which is split into k  disjunct cross-validation folds $\lbrace D^{(1)}_{valid}, \dots, D^{(k)}_{valid} \rbrace$ and $\lbrace D^{(1)}_{train}, \cdots, D^{(k)}_{train} \rbrace$  such that $D^{(i)}_{train} = D_{train} \textbackslash D^{(i)}_{valid}$ for $i=1, \dots, k$.
Finally, let $L( A^{(i)}_{\lambda}, D^{(i)}_{train}, D^{(i)}_{valid})$ denote the loss that algorithm $A^{(i)}$ has on $D^{(i)}_{valid}$ when trained on $D^{(j)_{train}}$ with hyperparameters $\lambda$.
Then, the \gls{CASH} problem is to find the joint algorithm and hyperparameter setting that minimizes the loss:
\begin{equation*}
    A^*, \lambda^* \in \underset{A^{(j)}\in A, \lambda \in \Lambda^{(j)}}{argmin} \frac{1}{k} \sum_{i=1}^k \Lcurv(A^{(j)}_{\lambda}, D^{(i)}_{train}, D^{(i)}_{valid})
\end{equation*}

\end{definition}

\section{Hyperparameter Optimization Techniques for Classification}

\begin{itemize}
    \item Hyperparameter optimization in general, describe the problem of finding parameter configuration that maximizes the validation performance.
    \item
\end{itemize}
In this section methods for the optimization of hyperparameters that are used in AutoML systems will be described.

Most of the methods have three steps in common, which are:
\begin{enumerate}
    \item Run the current model.
    \item Evaluate the results of the current model according to a metric.
    \item Select the next model that should be run.
\end{enumerate}
The difference in the methods is how they select the next model that will be run, so step 3 above.

The methods that will be presented are shown in \cref{fig:hpo} and can generally be divided into three categories \cite{Li2017Hyperband:Optimization,BergstraAlgorithmsOptimization, Falkner2018BOHB:Scale, Feurer2015EfficientLearning}:
\begin{figure}
    \centering
    \includegraphics[width=0.5\textwidth]{data/HPO_methods.png}
    \caption{Overview of the hyperparameter optimization methods that will be described.}
    \label{fig:hpo}
\end{figure}
\begin{itemize}
    \item \textbf{Base}: Methods that are basically used like Random-search.
    Like the name suggests, Random-search is running random configurations and evaluates them.
    \item \textbf{Sequential model-based}: Methods that create a model of the hyperparameters to predict the best hyperparameters.
    One representative of this class is the \gls{BHO}
    \item \textbf{Multi-armed Bandit}: These methods try to sample a lot of configurations in parallel with a very low budget for each configuration.
    Examples for this class are Hyperband and \gls{BOHB}, which is a combination of \gls{BHO} and Hyperband.
\end{itemize}

In the following these methods will be described in more detail.
Of course, there are also other methods like gradient-based methods, but in the following only those methods will be presented that have proven to work well in the AutoML systems.

\subsection{Random}

\begin{itemize}
    \item One of the most simple Hyperparameter optimization methods is Random \cite{Bergstra:2012:RSH:2503308.2188395}.
    \item Though can be very effective, especially if the search space is not very large.
    \item Performs in general better than Gridsearch.
    \item Fails for large and high-dimensional parameter spaces.

\end{itemize}

\subsection{Bayes Optimization}

\begin{itemize}
    \item Model-based method that relies on a statistical model and an acquisition function \cite{ShahriariTakingOptimization, Frazier2018AOptimization, SnoekPracticalAlgorithms}.
    Goal of acquisition function is to find a sample that should be evaluated next.
    Should make trade-off between exploration and exploitation.
    \item Only a framework, several implementations with different models and acquisition functions.
    \item Surrogate model about how the objective function (parameter configuration to metric value).
    \item An example for an often used statistical model are Gaussian Processes (GP) \cite{ShahriariTakingOptimization}.
    \item Goal is to approximate this model by samples.
    \item The steps are:
    \begin{itemize}
        \item select next parameter configuration that maximizes the acquisition function.
        \item Run model with these parameter configuration.
        \item Evaluate the configuration, so calculate metric(configuration).
        \item Update the model.
    \end{itemize}
    \item An example for this procedure is shown in \cref{fig:BOExample}.

    \begin{figure}
        \centering
        \includegraphics{graphics/bayes_optimization_example.png}
        \caption{Example of the Bayesian Optimization procedure \cite{ShahriariTakingOptimization}.}
        \label{fig:BOExample}
    \end{figure}
    \item Example for an acquisition function is the \gls{EI}:
    \begin{equation}
        a(x) = \int max(0, \alpha -f(x)) dp(f|D)
    \end{equation}
    Here $\alpha$ is the currently best observed value.
    \item One implementation of the Bayes optimization is the Tree Parzen Estimator (TPE) \cite{BergstraAlgorithmsOptimization}, which uses a density kernel estimator.

    \item Should only be used if f has no closed form or is expensive to evaluate.
    \item Sequential, so no parallelization.
    \item Only good after some samples, for less samples random could be better.
\end{itemize}

\subsection{Hyperband}

Hyperband \cite{Li2017Hyperband:Optimization} is a multi-armed bandit method that relies on \gls{SH} \cite{Jamieson2015Non-stochasticOptimization}.
The general algorithm is shown in \cref{fig:HBAlgorithm}.
\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{graphics/hyperband_algorithm.png}
    \caption{Overview of the Hyperband algorithm \cite{Li2017Hyperband:Optimization}.}
    \label{fig:HBAlgorithm}
\end{figure}

\begin{itemize}
    \item Gets as input the maximum budget for one configuration and $\eta$, which controls the amount of configurations that are discarded after each round of \gls{SH}.
    \item The general idea is to run many \gls{SH} runs.
    \item One SH run consists of many iterations. First random configurations are sampled.
    The number of configurations should be chosen in such a way that the minimum budget can be distributed equally over all configurations.
\end{itemize}

\subsection{Bayesian Optimization and Hyperband}

\gls{BOHB} is a combination of the Bayesian Optimization and the Hyperband method.
Instead of random sampling of configurations in the different \gls{SH} iterations of Hyperband, \gls{BOHB} uses Bayesian Optimization to fit a model and to select the configurations.

\section{Clustering Algorithms}

\begin{itemize}
    \item What is clustering
    \item what is partitional clustering
    \item which partitional clustering algorithms exist
    \item shortly explain the algorithms that are used in the implementation (GMM, KMeans, KMedoids)
\end{itemize}

\section{Clustering Metrics}
\begin{itemize}

    \item Metric Types: internal, external, relative (?)
    \item explain how they work and use show metrics that are used in the concept/implementation.
\end{itemize}

\section{Meta-learning}

Meta-learning \cite{Brazdil2010MetalearningMining.} is the task of learning to learn.
In the area of machine learning it can be used to learn algorithms and their configurations from previous tasks.
For this, it typically includes an offline and an online phase.
The basic workflow is shown in \cref{fig:metalearningArch}.
\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{graphics/metalearning_general_architecture.png}
    \caption{General architecture of meta-learning, divided into offline and online phase.}
    \label{fig:metalearningArch}
\end{figure}

\begin{itemize}
    \item In an offline phase, training data is needed (which represents previous tasks).
    \item From this training data, \textit{meta-features} are extracted.
    Describe characteristics of the dataset.
    Example can be number of instances or number of attributes.
    \item meta-features of a dataset are saved in a database (meta-db in the figure).
    \item Then for the dataset, different algorithms with different configurations are run and evaluated.
    The configurations as well as the result of the evaluations are also stored in the meta-db.
    From meta-db information meta-learner can be derived.
    \item Online Phase: If new dataset, then first have to extract meta-features.
    \item Meta-features are input for meta-learner.
    \item Meta-learner suggests promising algorithm with configuration based on the meta-db knowledge.
    \item One possibility is to find the most similar dataset according to meta-features and then take best known algorithm and configuration from meta-db.
    \item This would correspond to a k-Nearest-Neighbor classifier with $k=1$.
    \item Question is which meta-features should be used.
    \item Meta-features can be categorized into five groups \cite{Rivolli2018TowardsMeta-learning}:
    \begin{itemize}
        \item \textbf{Simple}: Can be directly extracted from the data and include measure like the number of instances or the number of attributes.
        \item \textbf{Statistical}: Measure statistical information about the data.
        They include measure like mean, median, standard deviation etc.
        \item \textbf{Information-theoretic}: Based on entropy and capture the information and the complexity in the data.
        An example is the attribute entropy.
        \item \textbf{Model-based}: This group of meta-features use structural properties of machine learning models.
        For example they build a decision tree from the data and the number of leaves of that tree is one meta-feature.
        \item \textbf{Landmarking}: The landmarking meta-features use the performance of simple classifiers like Naive Bayes as meta-features.
    \end{itemize}

    \item Landmarking and model-based cannot be used for clustering since we are assuming in the online phase of meta-learning that there are no class labels.
    \item Other groups also contain meta-features with class labels, e.g., the number of classes. For this work only those without class labels are interesting.
\end{itemize}


\section{Implementation and Limitations}
\label{SEC:implementation}

Our reference implementation for PolyPasswordHasher is available with an MIT
license at \showurlx.  It utilizes a 16 byte salt, with SHA256, to compute password
hashes. The Shamir Secret Sharing routines utilize GF256 as the underlying
field (by encoding each byte as a separate share).  The code base for
PolyPasswordHasher is 120 lines of Python code.  This code handles the
functionality described in Section~\ref{SEC:design}, including support for protector,
bootstrapping, and \thresholdlessaccounts; reading/writing a database to disk;
changing passwords; and detecting \partialverification of incorrect passwords.
PolyPasswordHasher relies on mathematical libraries such as GF256 operations,
Lagrange interpolation, and polynomial math code -- 391 lines of Python and 83 lines
of C code.  Additionally, the implementation uses standard python
libraries for functionalities such as SHA256 and AES.

Several outside developers, unbeknownst to us (until they shared their work
with us) implemented versions of PolyPasswordHasher in Ruby and PHP.  They
created their implementations based on our Python reference implementation and
specification that is available in our GitHub repository.
Table~\ref{TABLE:source-code} describes these outside PPH implementations.
Other external developers have uploaded fixes and improvements to our code when
they wanted it to be compatible with their environment.

\begin{table}[t]
    \centering
    \renewcommand{\arraystretch}{1.3}

    \begin{tabular}{| c | c | c | }
    \hline
    {\bf Language } & {\bf Lines of Code} & {\bf Author}\\
    \hline
    C & 598 & Local1\\
    \hline
    Python & 391 (+83) & Local2\\
    \hline
    Django & 786 & Local1 + External1\\
    \hline
    Ruby & 437 & External2\\
    \hline
    PHP* & N/A  & External3\\
    \hline
    \end{tabular}
    \caption{Different implementations of \PPH for different Languages and Frameworks, the
    PHP implementation has not been publicly released}
    \label{TABLE:source-code}
\end{table}

\subsection{Handling Alternate Authentication Mechanisms}
\label{SUBSEC:alternate-authentication-mechanisms}

Passwords are not the only mechanism for logging into modern systems. For
example, it is common for users to have a private key to log in over ssh,
biometric-based authentication holds substantial promise, smart cards are often
used to hide user credentials, and single sign-in systems like OAuth and OpenID
are commonplace and provided by many major websites. Any practical protection
mechanism needs to operate in conjunction with such techniques.

Handling non-password logins for shielded user accounts is trivial because
it requires no changes to the system. These non-password logins can be handled
using existing login mechanisms, without any modification.

For \thresholdaccounts, it is possible to view the authentication process as
decrypting information using a secret that has been stored by a remote party
(as in fact many such systems are implemented). Essentially, we can encrypt the
Shamir Secret Share with the user’s key and check that the resulting share is
correct.  For example, suppose that an administrator has a public/private
keypair that is used to log in (as is common with SSH private key or smart card
based authentications).  Instead of XORing the Shamir Share with the salted
hash of the password, the share can be encrypted with the user’s private key.
When the administrator presents her public key to login, it decrypts the
original share.

Prior work on deriving a private key from (noisy) data [34] is relevant when
authenticating \thresholdaccounts using other techniques, such as biometrics.
For example, once a key has been derived from biometric data,
PolyPasswordHasher will function in a way that is identical to the private key
authentication scheme discussed in the previous paragraph.

\subsection{Deployment}
\label{SUBSEC:deployment}

One key issue with deploying PolyPasswordHasher is deciding which accounts should be protector
accounts and which should be shielded.  General guidelines include that
\thresholdaccounts should be assigned to users who choose strong passwords.
Ideally these are users who will log in soon after a reboot, which will
minimize the amount of time needed to bootstrap. Although different servers may
have different use patterns, in our experience, system administrators usually
meet the above requirements.  We examined logs from servers at our institution
and found that system administrators typically login a few minutes after a
system is restarted (likely because they will check the system to make sure it
is operating correctly).  Based on these practices, system administrators tend
to be ideal candidates for \thresholdaccounts.

The threshold setting is another important value for deployment.  In our
experience, a threshold value of 2 - 5 is sufficient, even for systems that
process thousands of password requests.  As we demonstrate in the evaluation
section, even a threshold of 2 increases password strength immensely.

Using PolyPasswordHasher results in very minor changes to existing password
authentication systems. The client tools for password authentication do not
change in any way. In fact, because PolyPasswordHasher only impacts password
storage, it is invisible to clients. For administrators, the only change is
that when an account is created, the administrator can specify whether or not
an account counts toward the threshold.

The file format for password storage is similar to existing systems. In systems
that use a database, the share number and isolated-check bits can simply be
inserted as new columns in the table.  Many servers (such as Linux, Mac, and
BSD) use the /etc/shadow or /etc/master.password colon delimited formats.  As
it is used today, the ‘password’ field contains both the salt and hash but
these different portions of the field are not delineated.  With
PolyPasswordHasher, additional data (such as the share number and
isolated-check bits) can also be added at a known position within the
‘password’ field (e.g., the beginning or end); doing so will lengthen this
variable length, opaque field.


\documentclass[rnaas]{aastex62}

\pdfoutput=1

\usepackage{lmodern}
\usepackage{microtype}
\usepackage{url}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
\usepackage{multirow}
\usepackage{graphicx}
\bibliographystyle{aasjournal}

% ------------------ %
% end of AASTeX mods %
% ------------------ %

% Projects:
\newcommand{\project}[1]{\textsf{#1}}
\newcommand{\kepler}{\project{Kepler}}
\newcommand{\ktwo}{\project{K2}}

% references to text content
\newcommand{\documentname}{\textsl{Note}}
\newcommand{\figureref}[1]{\ref{fig:#1}}
\newcommand{\Figure}[1]{Figure~\figureref{#1}}
\newcommand{\figurelabel}[1]{\label{fig:#1}}
\renewcommand{\eqref}[1]{\ref{eq:#1}}
\newcommand{\Eq}[1]{Equation~(\eqref{#1})}
\newcommand{\eq}[1]{\Eq{#1}}
\newcommand{\eqalt}[1]{Equation~\eqref{#1}}
\newcommand{\eqlabel}[1]{\label{eq:#1}}

% TODOs
\newcommand{\todo}[3]{{\color{#2}\emph{#1}: #3}}
\newcommand{\dfmtodo}[1]{\todo{DFM}{red}{#1}}
\newcommand{\alltodo}[1]{\todo{TEAM}{red}{#1}}
\newcommand{\citeme}{{\color{red}(citation needed)}}

% math
\newcommand{\T}{\ensuremath{\mathrm{T}}}
\newcommand{\dd}{\ensuremath{ \mathrm{d}}}
\newcommand{\unit}[1]{{\ensuremath{ \mathrm{#1}}}}
\newcommand{\bvec}[1]{{\ensuremath{\boldsymbol{#1}}}}
\newcommand{\Gaussian}[3]{\ensuremath{\frac{1}{|2\pi #2|^\frac{1}{2}}
            \exp\left[ -\frac{1}{2}#1^\top #2^{-1} #1 \right]}}

% VECTORS AND MATRICES USED IN THIS PAPER
\newcommand{\Normal}{\ensuremath{\mathcal{N}}}
\newcommand{\mA}{\ensuremath{\bvec{A}}}
\newcommand{\mC}{\ensuremath{\bvec{C}}}
\newcommand{\mS}{\ensuremath{\bvec{\Sigma}}}
\newcommand{\mL}{\ensuremath{\bvec{\Lambda}}}
\newcommand{\vw}{\ensuremath{\bvec{w}}}
\newcommand{\vy}{\ensuremath{\bvec{y}}}
\newcommand{\vt}{\ensuremath{\bvec{\theta}}}
\newcommand{\vm}{\ensuremath{\bvec{\mu}(\bvec{\theta})}}
\newcommand{\vre}{\ensuremath{\bvec{r}}}
\newcommand{\vh}{\ensuremath{\bvec{h}}}
\newcommand{\vk}{\ensuremath{\bvec{k}}}

% typography obsessions
\setlength{\parindent}{3.0ex}

\begin{document}\raggedbottom\sloppy\sloppypar\frenchspacing

\title{%
A robust method to measure centroids of spectral lines
}

\author[0000-0003-1534-5186]{Richard Teague}
\affil{University of Michigan, Ann Arbor, MI}

\author[0000-0002-9328-5652]{Daniel Foreman-Mackey}
\affil{Flatiron Institute, New York, NY}

%\keywords{%
%methods: data analysis ---
%methods: statistical
%}

\section{Background}

When analyzing data cubes produced by radio telescopes or integral field units,
astronomers frequently aim to produce a map of velocity as a function of pixel
coordinate or time.
In some cases, the spectral dimension is dominated by a single line and the
operation of inferring a velocity is simplified to a question of determining
the centroid of the line.
For this problem, there exist multiple popular methods \citep[see, for
example][]{deBlok:2008}, however the most common are the ``intensity weighted
average velocity'' (also known as the ``first moment'' method) and the
velocity of the maximum intensity, called the ``ninth moment''\footnote{We
emphasize that this is a misnomer as it does not relate to any statistical
moment.}, as they are implemented by \texttt{CASA} \citep{McMullin:2007}.
Even when the emission is dominated by a single line component, these methods
do not offer the accuracy or precision required to extend studies to small
scale structures.

The ``first moment'' approach is strongly affected by the noise in the spectrum
and any asymmetry in the line profile.
Sigma clipping is typically used to circumvent these issues, however this can
result in the addition of spurious features or the masking of astrophysically
interesting features in the resulting velocity map.
``Ninth moment'' maps are more robust to these issues, however the precision of
this approach is limited by the velocity resolution of the data.

Other approaches involve fitting a model line profile (e.g.~a Gaussian) to the
data, but these methods are both more computationally expensive and more
sensitive to the details of the line shape.
In the case of a double-Gaussian profile, due to radial infall, for example, the
line center for a single Gaussian component (or a higher order expansion) will
therefore be offset to account for this double component.

In this note we present a new method for inferring line centroids that is
simple, computationally efficient, and robust to noise and errors in line
shape.

\section{Technical details}

It has been demonstrated that the centroid of photometric sources can be
determined with near-optimal precision by fitting a quadratic surface to the
brightest central pixels of the point spread function \citep{Vakili:2016}.
Here we apply this method in one dimension to estimate the centroid of
spectral lines.
The fundamental idea is that we can fit a quadratic model to the brightest
pixel and one pixel on either side to estimate the curvature of the line
profile near maximum.

The procedure is as follows:
\begin{enumerate}

{\item We find the pixel of maximum intensity in the spectrum and extract that
pixel value $I_0$ and the one to the left $I_-$ and right $I_+$.}

{\item We model these three intensities as a quadratic function
\begin{eqnarray}
I(x) &=& a_0 + a_1\,(x-x_0) + a_2\,{(x-x_0)}^2
\eqlabel{quad}
\end{eqnarray}
where $x_0$ is the pixel coordinate of maximum intensity and $x$ is in units
of pixels.
Given the intensities in the three target pixels, we find
\begin{eqnarray}
a_0 &=& I_0 \\
a_1 &=& \frac{1}{2}(I_+ - I_-) \\
a_2 &=& \frac{1}{2}(I_+ + I_- - 2\,I_0) \quad.
\end{eqnarray}
}

{\item The maximum of \Eq{quad} is at
\begin{eqnarray}
x = x_\mathrm{max} &=& x_0 - \frac{a_1}{2\,a_2} \\
    &=& x_0 - \frac{f_+ - f_-}{2\,(f_+ + f_- - 2\,f_0)} \quad,
\end{eqnarray}
and this is our estimate for the centroid of the line in pixel coordinates.
}

\end{enumerate}

We can also estimate the statistical uncertainty on $x_\mathrm{max}$ by
linearizing and propagating the uncertainty from the fluxes to the centroid
estimate.
If we assume that the intensity uncertainties are normal, independent, and
homoskedastic with standard deviation $\sigma$, we find the
following approximation for the statistical uncertainty on $x_\mathrm{max}$
\begin{eqnarray}
{\sigma_{x_\mathrm{max}}}^2 = \frac{\sigma^2}{8}\,\left(
    \frac{3}{{a_2}^2} + \frac{{a_1}^2}{{a_2}^4}
\right)\quad.
\end{eqnarray}
We emphasize that this only includes the statistical uncertainty and any
application of this method to real data will also require a treatment of
systematic uncertainties\footnote{The code released
alongside this note includes an implementation and proof of more general noise
models at \url{https://github.com/richteague/bettermoments}.}.

\section{Demonstration of Method}

\begin{figure}[htbp]
\centering
\includegraphics[width=\textwidth]{figure.pdf}
\caption{%
Top row: Comparisons of different line centroid methods applied to simulated
data.
Left: A Gaussian fit to the data is shown in green and the quadratic fit to
the peak pixels is in blue.
Vertical ticks show the velocity inferred using the other approaches with the
same colors as in the right panel.
Right: The difference between the inferred centroid and the truth for the
methods listed along the x-axis.
The error bars indicate the statistical uncertainty of each method.
Bottom row: Application of the three methods to observations of HD~135344B.
\label{figure}}
\end{figure}

The top row of Figure~\ref{figure} compares different methods for estimating
a line centroid the case of an asymmetric line profile, modeled as the sum of
two Gaussian components.
Both the first moment and Gaussian fit results are biased by the asymmetry of
the line.
Although the ``ninth moment'' approach avoids this bias, its precision is
limited by the velocity resolution of the data.

The bottom row shows velocity maps of $^{13}$CO from the protoplanetary disk,
HD~135344B \citep[ALMA Project 2012.1.00158.S]{vanderMarel:2016}, imaged at a
velocity resolution of $330~{\rm m\,s^{-1}}$.
With a $2\sigma$ clip applied to the data, a ``first moment'' approach results
in a noisy map, making it hard to distinguish real structures from noise.
As with the asymmetric spectrum, the ``ninth moment'' is less affected by the
noise but the resulting map loses fidelity due to the coarse velocity
resolution of the data.
The quadratic approach does not suffer from noise features while maintaining a
high precision on the inferred velocity and achieving a statistical uncertainty
of $47~{\rm m\,s^{-1}}$, or 15\% of the velocity resolution.

The code used to generate this Figure and an open source implementation of
this method is available at
\url{https://github.com/richteague/bettermoments}.

\bibliography{bettermoments}

\end{document}


% Boris Jeremic (@ucdavis.edu)

\section{Large Deformation Hyperelastic Material Model Commands}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Compressible Neo-Hookean Material Commands}
\label{CompNH}

\begin{verbatim}
nDMaterial NeoHookean3D matTag? K? G? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial NeoHookeanCompressible3D matTag? K? G? <rho?>
\end{verbatim}

This model is the traditional compressible Neo-Hookean material model.
A compressible Neo-Hookean material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object.
The parameter \texttt{K} defines the material reference bulk modulus.
The parameter \texttt{G} defines the material reference shear modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

Although this is named compressible Neo-Hookean material, it can also be used for
near-compressible or incompressible material. In this case,
make \texttt{K} a value far bigger than \texttt{G}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Neo-Hookean Material Commands}
\label{DNH}

\begin{verbatim}
nDMaterial DecoupledNH3D matTag? K? G? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledNeoHookean3D matTag? K? G? <rho?>
\end{verbatim}

This model is a decoupled model for its volumetric and isochoric parts.
A decoupled Neo-Hookean material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object.
The parameter \texttt{K} defines the material reference bulk modulus.
The parameter \texttt{G} defines the material reference shear modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Logarithmic Material Commands}
\label{DLog}

\begin{verbatim}
nDMaterial DecoupledLog3D matTag? K? G? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledLogarithmic3D matTag? K? G? <rho?>
\end{verbatim}

This model is a decoupled model for its volumetric and isochoric parts.
A decoupled logarithmic material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object.
The parameter \texttt{K} defines the material reference bulk modulus.
The parameter \texttt{G} defines the material reference shear modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Mooney-Rivlin-Simo Material Commands}
\label{DMRS}

\begin{verbatim}
nDMaterial DecoupledMRS3D matTag? c1? c2? K? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledMooneyRivlinSimo3D matTag? c1? c2? K? <rho?>
\end{verbatim}

This model is a decoupled model for its volumetric and isochoric parts.
Its isochoric part is Mooney-Rivlin model and its volumetric part is
Simo-Pister model.
A decoupled Mooney-Rivlin-Simo material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object. The parameter \texttt{c1} and \texttt{c2}
define the material constants.
The parameter \texttt{K} defines the material reference bulk modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Ogden-Simo Material Commands}
\label{DOS}

\begin{verbatim}
nDMaterial DecoupledOS3D matTag? N? c1? ... cN? m1? ... mN? K? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledOgdenSimo3D matTag? N? c1? ... cN? m1? ... mN? K? <rho?>
\end{verbatim}

This model is a decoupled model for its volumetric and isochoric parts.
Its isochoric part is Ogden model and its volumetric part is Simo-Pister model.
A decoupled Ogden-Simo material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object. The parameter \texttt{N},
\texttt{c1}, ..., \texttt{cN}, and \texttt{m1}, ..., \texttt{mN} define the material constants.
The parameter \texttt{K} defines the material reference bulk modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Mooney-Rivlin Material Commands}
\label{DMR}

\begin{verbatim}
nDMaterial DecoupledMR3D matTag? c1? c2? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledMooneyRivlin3D matTag? c1? c2? <rho?>
\end{verbatim}

This model is a decoupled isochoric model.
A decoupled Mooney-Rivlin-Simo material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object. The parameter \texttt{c1} and \texttt{c2}
define the material constants.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Ogden Material Commands}
\label{DO}

\begin{verbatim}
nDMaterial DecoupledOgden3D matTag? N? c1? ... cN? m1? ... mN? <rho?>
\end{verbatim}

This model is a decoupled isochoric model.
A decoupled Ogden-Simo material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object. The parameter \texttt{N},
\texttt{c1}, ..., \texttt{cN}, and \texttt{m1}, ..., \texttt{mN} define the material constants.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Decoupled Simo-Pister Material Commands}
\label{DSP}

\begin{verbatim}
nDMaterial DecoupledSP3D matTag? K? <rho?>
\end{verbatim}
or
\begin{verbatim}
nDMaterial DecoupledSimoPister3D matTag? K? <rho?>
\end{verbatim}

This model is a decoupled volumetric model.
A decoupled Simo-Pister material is constructed using \texttt{nDMaterial} command.
The argument \texttt{matTag} is used to uniquely identify this nDMaterial object among
nDMaterial objects in the BasicBuilder object.
The parameter \texttt{K} defines the material reference bulk modulus.
The optional parameter \texttt{rho} defines the material reference density,
the default value of rho is zero.