\section{Abstract Convergence}
\label{sect.abstract.convergence}

Strong eventual consistency (SEC) requires \emph{convergence} of all copies of the shared state: whenever two nodes have received the same set of updates, they must be in the same state.
This definition constrains the values that read operations may return at any time, making SEC a stronger property than eventual consistency.
By accessing only their local copy of the shared state, nodes can execute read and write operations without waiting for network communication.
Nodes exchange updates asynchronously when a network connection is available.

We now use Isabelle to formalise the notion of strong eventual consistency.
In this section we do not make any assumptions about networks or data structures; instead, we use an abstract model of operations that may be reordered, and we reason about the properties that those operations must satisfy.
We then provide concrete implementations of that abstract model in later sections.

\subsection{The Happens-before Relation and Causality}\label{sect.happens.before}

The simplest way of achieving convergence is to require all operations to be commutative, but this definition is too strong to be useful for many datatypes.
For example, in a set, an element may first be added and then subsequently removed again.
Although it is possible to make such additions and removals unconditionally commutative, doing so yields counter-intuitive semantics \cite{Bieniusa:2012wu,Bieniusa:2012gt}.
Instead, a better approach is to require only \emph{concurrent} operations to commute with each other.
Two operations are concurrent if neither ``knew about'' the other at the time when they were generated.
If one operation happened before another---for example, if the removal of an element from a set knew about the prior addition of that element from the set---then it is reasonable to assume that all nodes will apply the operations in that order (first the addition, then the removal).

The \emph{happens-before} relation, as introduced by \citet{Lamport:1978jq}, captures such causal dependencies between operations.
It can be defined in terms of sending and receiving messages on a network, and we give such a definition in Section~\ref{sect.network}.
However, for now, we keep it abstract, writing $\isa{x} \prec \isa{y}$ to indicate that operation $\isa{x}$ happened before $\isa{y}$, where $\prec$ is a predicate of type $\isacharprime\isa{oper} \mathbin{\isasymRightarrow} \isacharprime\isa{oper} \mathbin{\isasymRightarrow} \isa{bool}$.
In words, $\prec$ can be applied to two operations of some abstract type $\isacharprime\isa{oper}$, returning either $\isa{True}$ or $\isa{False}$.%
\footnote{Note that in the distributed systems literature it is conventional to write the happens-before relation as $\isa{x} \rightarrow \isa{y}$, but we reserve the arrow operator to denote logical implication.}
Our only restriction on the happens-before relation $\prec$ is that it must be a \emph{strict partial order}, that is, it must be irreflexive and transitive, which implies that it is also antisymmetric.
We say that two operations $\isa{x}$ and $\isa{y}$ are \emph{concurrent}, written $\isa{x} \mathbin{\isasymparallel} \isa{y}$, whenever one does not happen before the other:
$\neg (\isa{x} \prec \isa{y})$ and $\neg (\isa{y} \prec \isa{x})$.
Thus, given any two operations $\isa{x}$ and $\isa{y}$, there are three mutually exclusive ways in which they can be related: either $\isa{x} \prec \isa{y}$, or $\isa{y} \prec \isa{x}$, or $\isa{x} \mathbin{\isasymparallel} \isa{y}$.

As discussed above, the purpose of the happens-before relation is to require that some operations must be applied in a particular order, while allowing concurrent operations to be reordered with respect to each other.
We assume that each node applies operations in some sequential order (a standard assumption for distributed algorithms), and so we can model the execution history of a node as a list of operations.
We can then inductively define a list of operations as being \emph{consistent with the happens-before relation}, or simply \emph{hb-consistent}, as follows:
\begin{isabelle}
\isacommand{inductive} hb{\isacharunderscore}consistent\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ list\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\\
~~~~{\isachardoublequoteopen}hb{\isacharunderscore}consistent\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ {\isacharbar}\\
~~~~{\isachardoublequoteopen}{\isasymlbrakk}\ hb{\isacharunderscore}consistent\ xs{\isacharsemicolon}\ {\isasymforall}x\ {\isasymin}\ set\ xs{\isachardot}\ {\isasymnot}\ y\ {\isasymprec}\ x\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ hb{\isacharunderscore}consistent\ {\isacharparenleft}xs\ {\isacharat}\ {\isacharbrackleft}y{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}
\end{isabelle}
In words: the empty list is hb-consistent; furthermore, given an hb-consistent list $\isa{xs}$, we can append an operation $\isa{y}$ to the end of the list to obtain another hb-consistent list, provided that $\isa{y}$ does not happen-before any existing operation $\isa{x}$ in $\isa{xs}$. As a result, whenever two operations $\isa{x}$ and $\isa{y}$ appear in a hb-consistent list, and $\isa{x}\prec\isa{y}$, then $\isa{x}$ must appear before $\isa{y}$ in the list. However, if $\isa{x}\mathbin{\isasymparallel}\isa{y}$, the operations can appear in the list in either order.

\subsection{Interpretation of Operations}\label{sect.ops.interpretation}

We describe the state of a node using an abstract type variable $\isacharprime\isa{state}$.
To model state changes, we assume the existence of an \emph{interpretation} function of type $\isa{interp} \mathbin{\isacharcolon\isacharcolon} \isacharprime\isa{oper} \mathbin{\isasymRightarrow} \isacharprime\isa{state} \mathbin{\isasymRightarrow} \isacharprime\isa{state}\ \isa{option}$, which lifts an operation into a \emph{state transformer}---a function that either maps an old state to a new state, or fails by returning $\isa{None}$.
If $\isa{x}$ is an operation, we also write $\langle\isa{x}\rangle$ for the state transformer obtained by applying $\isa{x}$ to the interpretation function.

Concretely, these definitions are captured in Isabelle with the following locale declaration:
\begin{isabelle}
~~~~\isakeyword{fixes}\ \=hb{\isacharunderscore}weak\ \=\kill
\isacommand{locale} happens{\isacharunderscore}before\ {\isacharequal}\ preorder\ hb{\isacharunderscore}weak\ hb\\
~~~~\isakeyword{for}\>hb{\isacharunderscore}weak\>{\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ {\isasymRightarrow}\ {\isacharprime}oper\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\\
~~~~\isakeyword{and}\>hb\>{\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ {\isasymRightarrow}\ {\isacharprime}oper\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ {\isacharplus}\\
~~~~\isakeyword{fixes}\>interp\>{\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ {\isasymRightarrow}\ {\isacharprime}state\ {\isasymRightarrow}\ {\isacharprime}state\ option{\isachardoublequoteclose}
\end{isabelle}
The $\isa{happens-before}$ locale extends the $\isa{preorder}$ locale, which is part of Isabelle's standard library and includes various useful lemmas.
It fixes two constants: a preorder that we call $\isa{hb-weak}$ or $\preceq$, and a strict partial order that we call $\isa{hb}$ or $\prec$.
We are only interested in the strict partial order and define $\isa{x}\preceq\isa{y}$ to be $\isa{x}\prec\isa{y} \vee \isa{x}=\isa{y}$.
Moreover, the locale fixes the interpretation function $\isa{interp}$ as described above, which means that we assume the existence of a function with the given type signature without specifying an implementation.

Given two operations $\isa{x}$ and $\isa{y}$, we can now define the composition of state transformers: we write $\langle\isa{x}\rangle \mathbin{\isasymrhd} \langle\isa{y}\rangle$ to denote the state transformer that first applies the effect of $\isa{x}$ to some state, and then applies the effect of $\isa{y}$ to the result.
If either $\langle\isa{x}\rangle$ or $\langle\isa{y}\rangle$ fails, the combined state transformer also fails.
The operator $\isasymrhd$ is a specialised form of the \emph{Kleisli arrow composition}, which we define as:
\begin{isabelle}
\isacommand{definition} kleisli\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isacharparenright}{\isachardoublequoteclose}\ \isakeyword{where}\\
~~~~{\isachardoublequoteopen}f\ {\isasymrhd}\ g\ {\isasymequiv}\ {\isasymlambda}x{\isachardot}\ f\ x\ {\isasymbind}\ {\isacharparenleft}{\isasymlambda}y{\isachardot}\ g\ y{\isacharparenright}{\isachardoublequoteclose}
\end{isabelle}
\noindent Here, $\isasymbind$ is the \emph{monadic bind} operation, defined on the option type that we are using to implement partial functions.
We can now define a function $\isa{apply-operations}$ that composes an arbitrary list of operations into a state transformer.
We first map $\isa{interp}$ across the list to obtain a state transformer for each operation, and then collectively compose them using the Kleisli arrow composition combinator:
\begin{isabelle}
\isacommand{definition} apply{\isacharunderscore}operations\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ list\ {\isasymRightarrow}\ {\isacharprime}state\ {\isasymRightarrow}\ {\isacharprime}state\ option{\isachardoublequoteclose}\ \isakeyword{where}\\
~~~~{\isachardoublequoteopen}apply{\isacharunderscore}operations\ ops\ {\isasymequiv}\ foldl\ {\isacharparenleft}op\ {\isasymrhd}{\isacharparenright}\ Some\ {\isacharparenleft}map\ interp\ ops{\isacharparenright}{\isachardoublequoteclose}
\end{isabelle}
\noindent The result is a state transformer that applies the interpretation of each of the operations in the list, in left-to-right order, to some initial state.
If any of the operations fails, the entire composition returns $\isa{None}$.

\subsection{Commutativity and Convergence}\label{sect.ops.commute}

We say that two operations $\isa{x}$ and $\isa{y}$ \emph{commute} whenever $\langle\isa{x}\rangle \mathbin{\isasymrhd} \langle\isa{y}\rangle = \langle\isa{y}\rangle \mathbin{\isasymrhd} \langle\isa{x}\rangle$, i.e. when we can swap the order of the composition of their interpretations without changing the resulting state transformer.
For our purposes, requiring that this property holds for \emph{all} pairs of operations is too strong.
Rather, the commutation property is only required to hold for operations that are concurrent, as captured in the next definition:
\begin{isabelle}
\isacommand{definition} concurrent{\isacharunderscore}ops{\isacharunderscore}commute\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ list\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\\
~~~~{\isachardoublequoteopen}concurrent{\isacharunderscore}ops{\isacharunderscore}commute\ xs\ {\isasymequiv} {\isasymforall}x\ y{\isachardot}\ {\isacharbraceleft}x{\isacharcomma}\ y{\isacharbraceright}\ {\isasymsubseteq}\ set\ xs\ {\isasymlongrightarrow}\ x\ {\isasymparallel}\ y\ {\isasymlongrightarrow}\ {\isasymlangle}x{\isasymrangle}{\isasymrhd}{\isasymlangle}y{\isasymrangle}\ {\isacharequal}\ {\isasymlangle}y{\isasymrangle}{\isasymrhd}{\isasymlangle}x{\isasymrangle}{\isachardoublequoteclose}
\end{isabelle}
Given this definition, we can now state and prove our main theorem, $\isa{convergence}$.
This theorem states that two hb-consistent lists of distinct operations, which are permutations of each other and in which concurrent operations commute, have the same interpretation:
\begin{isabelle}
\isacommand{theorem} convergence{\isacharcolon}\\
~~~~\isakeyword{assumes}\ {\isachardoublequoteopen}set\ xs\ {\isacharequal}\ set\ ys{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}concurrent{\isacharunderscore}ops{\isacharunderscore}commute\ xs{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}concurrent{\isacharunderscore}ops{\isacharunderscore}commute\ ys{\isachardoublequoteclose}\\
~~~~~~~~\isakeyword{and}\ {\isachardoublequoteopen}distinct\ xs{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}distinct\ ys{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}hb{\isacharunderscore}consistent\ xs{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}hb{\isacharunderscore}consistent\ ys{\isachardoublequoteclose}\\
~~~~\isakeyword{shows}\ {\isachardoublequoteopen}apply{\isacharunderscore}operations\ xs\ {\isacharequal}\ apply{\isacharunderscore}operations\ ys{\isachardoublequoteclose}
\end{isabelle}
\noindent
A fully mechanised proof of this theorem can be found in our submission to the Archive of Formal Proofs \cite{CRDT-AFP}.
Although this theorem may seem ``obvious'' at first glance---commutativity allows the operation order to be permuted---it is more subtle than it seems.
The difficulty arises because operations may succeed when applied to some state, but fail when applied to another state (for example, attempting to delete an element that does not exist in the state).
We find it interesting that it is nevertheless sufficient for the definition of $\isa{concurrent-ops-commute}$ to be expressed only in terms of the Kleisli arrow composition, and without explicitly referring to the state.

\subsection{Formalising Strong Eventual Consistency}\label{sect.abstract.sec.spec}

Besides convergence, another required property of SEC is \emph{progress}: if one node issues a valid operation, and another node applies that operation, then it must not become stuck in an error state.
Although the type signature of the interpretation function allows operations to fail, we need to prove that such a failure never occurs in any $\isa{hb-consistent}$ network behaviour.
We capture this requirement in the $\isa{strong-eventual-consistency}$ locale:
\begin{isabelle}
~~~~\isakeyword{assumes}\ \=commutativity{\isacharcolon}\ \={\isasymlbrakk}\ \=op{\isacharunderscore}history{\isacharparenleft}xs{\isacharat}{\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ \={\isasymrbrakk}\ \=\kill
\isacommand{locale}\ strong{\isacharunderscore}eventual{\isacharunderscore}consistency\ {\isacharequal}\ happens{\isacharunderscore}before\ {\isacharplus}\\
~~~~\isakeyword{fixes}\ op{\isacharunderscore}history\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}oper\ list\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \ \isakeyword{and}\ initial{\isacharunderscore}state\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}state{\isachardoublequoteclose}\\
~~~~\isakeyword{assumes}\>causality{\isacharcolon}\ \>{\isasymlbrakk}\>{\isachardoublequoteopen}op{\isacharunderscore}history\ xs\ \>{\isasymrbrakk}\ {\isasymLongrightarrow}\ hb{\isacharunderscore}consistent\ xs{\isachardoublequoteclose}\\
~~~~~~~~\isakeyword{and}\>distinctness{\isacharcolon}\ \>{\isasymlbrakk}\>{\isachardoublequoteopen}op{\isacharunderscore}history\ xs\ \>{\isasymrbrakk}\ {\isasymLongrightarrow}\ distinct\ xs{\isachardoublequoteclose}\\
~~~~~~~~\isakeyword{and}\>trunc{\isacharunderscore}history{\isacharcolon}\ \>{\isasymlbrakk}\>{\isachardoublequoteopen}op{\isacharunderscore}history{\isacharparenleft}xs{\isacharat}{\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ \>{\isasymrbrakk}\ {\isasymLongrightarrow}\ op{\isacharunderscore}history\ xs{\isachardoublequoteclose}\\
~~~~~~~~\isakeyword{and}\>commutativity{\isacharcolon}\ \>{\isasymlbrakk}\>{\isachardoublequoteopen}op{\isacharunderscore}history\ xs\ \>{\isasymrbrakk}\ {\isasymLongrightarrow}\ concurrent{\isacharunderscore}ops{\isacharunderscore}commute\ xs{\isachardoublequoteclose}\\
~~~~~~~~\isakeyword{and}\>no{\isacharunderscore}failure{\isacharcolon}\ \>{\isasymlbrakk}\>{\isachardoublequoteopen}op{\isacharunderscore}history{\isacharparenleft}xs{\isacharat}{\isacharbrackleft}x{\isacharbrackright}{\isacharparenright};\\
\>\>\>apply{\isacharunderscore}operations\ xs\ initial{\isacharunderscore}state\ {\isacharequal}\ Some\ state\\
\>\>\>\>{\isasymrbrakk}\>{\isasymLongrightarrow}\ {\isasymlangle}x{\isasymrangle}\ state\ {\isasymnoteq}\ None{\isachardoublequoteclose}
\end{isabelle}
\noindent Here, $\isa{op-history}$ is an abstract predicate describing any valid operation history of some replication algorithm, encapsulating the assumptions of the $\isa{convergence}$ theorem ($\isa{concurrent-ops-commute}$, $\isa{distinct}$, and $\isa{hb-consistent}$).
This locale serves as a concise summary of the properties that we require in order to achieve SEC, and from these assumptions and the theorem above we easily obtain the two safety properties of SEC as theorems:
\begin{isabelle}
~~~~\isakeyword{assumes}\ \=\kill
\isacommand{theorem}\ sec{\isacharunderscore}convergence{\isacharcolon}\\
~~~~\isakeyword{assumes}\ \>{\isachardoublequoteopen}set\ xs\ {\isacharequal}\ set\ ys{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}op{\isacharunderscore}history\ xs{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}op{\isacharunderscore}history\ ys{\isachardoublequoteclose}\\
~~~~\isakeyword{shows}\ \>{\isachardoublequoteopen}apply{\isacharunderscore}operations\ xs\ {\isacharequal}\ apply{\isacharunderscore}operations\ ys{\isachardoublequoteclose}\\[4pt]
\isacommand{theorem}\ sec{\isacharunderscore}progress{\isacharcolon}\\
~~~~\isakeyword{assumes} \>{\isachardoublequoteopen}op{\isacharunderscore}history\ xs{\isachardoublequoteclose}\\
~~~~\isakeyword{shows} \>{\isachardoublequoteopen}apply{\isacharunderscore}operations\ xs\ initial{\isacharunderscore}state\ {\isasymnoteq}\ None{\isachardoublequoteclose}
\end{isabelle}

Thus, in order to prove SEC for some replication algorithm, we only need to show that the five assumptions of the $\isa{strong-eventual-consistency}$ locale are satisfied.
As we shall see in Section~\ref{sect.network}, the first three assumptions are satisfied by our network model, and do not require any algorithm-specific proofs.
For individual algorithms we only need to prove the $\isa{commutativity}$ and $\isa{no-failure}$ properties, and we show how to do this in Sections~\ref{sect.rga} and~\ref{sect.simple.crdts}.

Note that the $\isa{trunc-history}$ assumption requires that every prefix of a valid operation history is also valid.
This means that the convergence theorem holds at every step of the execution, not only at some unspecified time in the future (``eventually''), making SEC stronger than eventual consistency.


\chapter{Quantum Key Distribution}
\label{chap:bb84}

% Talk about quantum key distribution
Quantum computers have been shown to solve the underlying mathematical problems, such as the DLP and prime factorization that keep our encryption secure.
Although symmetric encryption is much less effected by this, it is not commonly used due to the key distribution problem \cite{cryptography}.
One potential solution is Quantum Key Distribution (QKD), which applies quantum mechanics and quantum computing properties to a key distribution protocol that is provably secure \cite{MikeAndIke}.
It allows for two parties to securely generate a symmetric key.
In this chapter, we look at the BB84 quantum key exchange protocol to exchange encryption keys between two parties, Alice and Bob.
The encryption key is exchanged through a quantum channel in the form of qubits.
An eavesdropper listening in on this communication cannot obtain any information without disturbing the qubits and measuring them, which introduces noise to the signal since the basis used to encode information is unknown.
We show that such an eavesdropper can be detected by Alice and Bob due to a spike in the error rate of transmitted qubits.

% Introduce protocol
\section{The BB84 Protocol}

\begin{figure}[htp]
	\centering
	\fbox{\begin{minipage}{41em}
	\begin{center}
		\textbf{BB84}
	\end{center}
	\begin{enumerate}
	\item  Alice and Bob connect via a quantum communication channel.
    \item  Alice generates $N$ random bits, where $N \geq 4l$, $l$ = desired key length.
    \item  Alice encodes the bits into qubits, randomly choosing to encode using one of two orthonormal bases -- the standard and the Hadamard basis.
    \item  Alice sends the qubits to Bob.
    \item  Bob measures each qubit, choosing to use one of the two selected bases at random.
    \item  Bob sends a bitvector of his chosen bases to Alice over a classical channel.
    \item  Alice responds with a bitvector showing which of Bob's bases were correct.
    \item  Alice and Bob discard all qubits that were measured in the wrong basis.
    \item  Bob sends Alice some number of the measured values to ensure the key was received without any interference.
    \item Alice responds with whether or not the exchanged values were correct.
    \item If there were no errors in the compared bits, the bits that were exchanged are discarded by both parties, and the rest of the bits are used as the key.
    \end{enumerate}
	\end{minipage}}
\caption{AThe BB84 Quantum Key Distribution Protocol.}
	\label{fig:BB84}
\end{figure}

In 1984 Charles Bennett and Gilles Brassard proposed the first quantum key distribution protocol, the BB84 \cite{qc:agi}.
The protocol allows two parties communicating over a public classical channel, such as the internet, and a public quantum channel to securely generate a shared encryption key for symmetric encryption.

To start the quantum key exchange, Alice randomly generates two bit sequences, $a$ and $b$, of length at least $N = 4l$ bits each, where $l$ is the intended key length.
While $N$ does not necessarily need to be $4l$, it allows for $l$ bits to be used to verify the keys integrity, since half of the bits are discarded during the key exchange, on average \cite{MikeAndIke}.
Alice then encodes $a$ into a block of $N$ qubits, $\ket{\psi}$.
This is done using two bases; in our case, the standard basis and Hadamard basis.
The basis chosen for encoding each bit in $a$ is determined by the corresponding bit in $b$, with $b_i = \{0 \textrm{ or } 1\}$ where $0$ refers to the standard basis and $1$  refers to the Hadamard basis.
Thus, each qubit is in either the standard basis or the Hadamard basis, and is in one of four states shown in Figure ~\ref{fig:possible_states}.
\begin{figure}[htp]
\centering
\begin{tabular}{|c|c|c|}
\hline
$a_i$ & $b_i$ & $\ket{\psi_i}$ \\ \hline
0 & 0 & $\ket{0}$ \\ \hline
1 & 0 & $\ket{1}$ \\ \hline
0 & 1 & $\ket{+}$ \\ \hline
1 & 1 & $\ket{-}$ \\ \hline
\end{tabular}
\caption{The four possible states of a qubit in a BB84 encoded string.}
\label{fig:possible_states}
\end{figure}
Next, Alice sends each qubit to Bob using a public quantum channel.
When Bob has received each qubit he can assemble the full qubit block $\ket{\psi}^\prime$.
Assuming a perfect quantum channel and no eavesdropping, there should not be any disturbance or noise in the communication; therefore $\ket{\psi}^\prime = \ket{\psi}$.
Once Bob has received all qubits, he measures each qubit in $\ket{\psi}^\prime$ into a bit sequence $a^\prime$ by randomly choosing a basis of measurement for each bit.
The bases chosen are stored in a bit sequence $b^\prime$.
Bob then informs Alice that he has measured all the received qubits.
Because there is a 50\% chance that Bob will choose an incorrect basis for measurement for each qubit, and a 50\% probability of measuring the correct value using the wrong basis, Bob has measured 75\% of the qubits correctly, on average (see Figure ~\ref{fig:possible_measurements_no_eve}).
\begin{figure}[htp]
	\centering
	\begin{tabular}{|r|c|c|c|c|c|c|c|c|}
		\hline
		Encoded value  & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
		Encoded basis  & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 \\ \hline
		Measured basis  & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 \\ \hline
		Measured value & 0 & $\nicefrac{0}{1}$& $\nicefrac{0}{1}$ & 0 & 1 & $\nicefrac{0}{1}$ & $\nicefrac{0}{1}$ & 1\\
		\hline
	\end{tabular}
	\caption{Exhaustive list of encoding and measurement of a single qubit between Alice and Bob.}
	\label{fig:possible_measurements_no_eve}
\end{figure}
While 75\% of the bits are measured correct on average, an average of 50\% are measured in the correct basis; those qubits are  guaranteed to be measured correctly.
Note that Alice has ($a$, $b$) and Bob has($a^\prime$ $b^\prime$), but they each do not know the others $a$ and $a^\prime$.
Alice and Bob now exchange the bases they each used, $b$ and $b^\prime$, respectively.
Alice and Bob both discard every bit that was encoded and measured using different bases: bit $i$ is discarded from $a$ and $a^\prime$ if $b_i \neq b^\prime_i$.
They store the remaining bits from $a$ and $a^\prime$ into a new bit sequence, $k$ and $k^\prime$, respectively.
Alice and Bob now each have the same key, $k = k^\prime$.
They each exchange some number of bits from $k$ to verify there was no error in their key generation.
If the exchanged bits are identical, then Alice and Bob can confirm with high probability that there was no eavesdropper, and that their key is secure \cite{MikeAndIke}.
They can now use the key for symmetrically encrypted communication, or even as a OTP on a classical channel.

% Discuss eavesdropping
If an eavesdropper, Eve, were to attempt to listen in on the conversation, she would not be able to gain any useful information from the qubits, since she does not know the basis in which the qubits are encoded, nor could she duplicate the qubits.
Therefore the only strategy she has is to perform a ``man-in-the-middle" attack, in which Eve impersonates Bob to Alice and Alice to Bob \cite{qc:agi}.

To eavesdrop on the communication, Eve listens on the quantum channel and waits for Alice to transmit qubits.
As Alice sends Bob the qubits over the quantum channel Eve intercepts each qubit, forming her own qubit block $\ket{\psi}^\prime$.
With the qubits now in Eve's possession, she attempt to measure the qubits or clone them on either of the two bases randomly.
She then re-encodes them using the same bases, into $\ket{\psi}^{\prime\prime}$, and forwards the qubits to Bob.
However, as previously shown, this would introduce noise to the signal, reducing Bob's correct average measurement.
Since the average percentage of correctness is $\frac{2 * 100\% + 6 * 50\%}{8} = 62.5\%$, the average percentage that a bit is measured correctly drops from 75\% to 62.5\%, leaving only 25\% of the qubits measured in the same basis used during encoding.
\begin{figure}[htp]
	\centering
	\begin{tabular}{|c|c|c|c|c|}
		% Table headers
		\hline
		$\textrm{Basis}_{Alice}$ & $\textrm{Basis}_{Eve}$ &$\textrm{Basis}_{Bob}$ & Percent Correct & Bit Kept in Key\\ \hline
	%	Ab  Eb  Bb  c       a
    	0 & 0 & 0 & 100\% & Kept \\ \hline
    	0 & 0 & 1 &  50\% & Discarded \\ \hline
    	0 & 1 & 0 &  50\% & Kept \\ \hline
    	0 & 1 & 1 &  50\% & Discarded \\ \hline
    	1 & 0 & 0 &  50\% & Discarded \\ \hline
    	1 & 0 & 1 &  50\% & Kept \\ \hline
    	1 & 1 & 0 &  50\% & Discarded \\ \hline
    	1 & 1 & 1 & 100\% & Kept \\ \hline
	\end{tabular}
	\caption{Exhaustive list of encoding and measurement of a single qubit between Alice and Bob with an eavesdropper}
	\label{fig:possible_measurements_eve}
\end{figure}
As can be seen in figure~\ref{fig:possible_measurements_eve}, when there is an eavesdropper, half of the bits kept in the generated key are measured in an incorrect basis by at least one party.
Once the bits that Bob measured in a different basis than Alice are discarded, there is still only a $\frac{2 * 100\% + 2 * 50\%}{4} = 75\%$ chance of any qubit being measured by Bob as the same value encoded by Alice.
This means, on average, 25\% of the bits in $k^\prime$ are incorrect.
When Alice and Bob exchange some bits to verify their correctness, even if only four bits are compared they both will, on average, detect that the qubits were maliciously measured during transmission, at which point Alice and Bob can abort communication \cite{MikeAndIke}.

In practice this protocol can be implemented using polarized photons as qubits, which can be sent between Alice and Bob using fiber optics.
The data is encoded into the photons using the angle of the polarization since photons can act as a qubit \cite{qc:agi}.
In this case the bases for encoding data are the standard basis, $(\ket{0}, \ket{1}) = (\ket{\rightarrow}, \ket{\uparrow})$, and the Hadamard basis, $(\ket{+}, \ket{-}) = (\ket{\nearrow}, \ket{\nwarrow})$.
However all that is required to perform any QKD protocol is the ability to communicate qubits over a public channel with a very low error rate \cite{MikeAndIke}.


\section{PageRank Algorithm}

Here we use the Wikipedia Vote Network dataset \cite{wikipedia} as our processing source. The requested PageRank algorithm is similar to the \emph{Connected Component} one, and after checking the spark source code, we're sure that the \texttt{GraphLoader} can also be used to process the wiki votes.

PageRank algorithm is named after both the term "web page" and Google co-founder Larry Page. The key to the ranking is a probabilistic balance between nodes. At the beginning of the computational process the pagerank for each node is randomized, and for each iteration, the rank is computed as:

\begin{equation}
PR(p_i) =\frac{1-d}{N} + d\sum_{p_j\in M(p_i)}\frac{PR(p_j)}{L(p_j)}
\end{equation}

An approximation of real page rank value can be calculated with several iterations.

After researching on GraphX Programming Guide \cite{pagerank}, the graph build with GraphLoader has implemented PageRank algorithm. In our implementation, it is called by

\begin{verbatim}
val ranksGraph = graph.pageRank(0.0001)
\end{verbatim}

The original source file does not contain name information, but we want to form a more intuitive result as \emph{Connected Component}, which adds an additional user--node linkage. We used \texttt{sed} scripts to pre-process the original vote data from \cite{wiki-elec}, and generate the user--node list as in \texttt{users.txt}.

{\small
\begin{verbatim}
sed '/^[\s]*#/d;
/^\s*$/d;
/^[ET].*/d;
/^N\t-1\tUNKNOWN.*/d;
s/^[UN]\t\(.*\)\t\(.*\)$/\1,\2/g;
s/^V\t.*\t\(.*\)\t.*\t\(.*\)$/\1,\2/g' \
original.txt | sort -g | uniq > users.txt
\end{verbatim}
}

The user--node list generated would be like:

{\small
\begin{verbatim}
3,ludraman
4,gzornenplatz
5,orthogonal
6,andrevan
7,texture
8,lst27
9,mirv
...
\end{verbatim}
}

After compiling and submitting like we've done in the \emph{Connected Component} part, the results are shown below and as figure \ref{fig:PR-result}:

{\small
\begin{verbatim}
PR: 32.78, ID:4037, Name:elonka
PR: 26.18, ID:  15, Name:danny
PR: 25.52, ID:6634, Name:tenpoundhammer
PR: 23.36, ID:2625, Name:_clown_will_eat_me
PR: 18.56, ID:2398, Name:werdna
PR: 17.96, ID:2470, Name:alex_bakharev
PR: 17.76, ID:2237, Name:khoikhoi
PR: 16.14, ID:4191, Name:ryulong
PR: 15.44, ID:7553, Name:dihydrogen_monoxide
PR: 15.30, ID:5254, Name:gracenotes
PR: 14.51, ID:2328, Name:phaedriel
PR: 14.48, ID:1186, Name:william_m._connolley
PR: 13.84, ID:1297, Name:robchurch
PR: 13.78, ID:4335, Name:mer-c
PR: 13.75, ID:7620, Name:cobi
PR: 13.65, ID:5412, Name:protectionbot
PR: 13.57, ID:7632, Name:redirectcleanupbot
PR: 13.33, ID:4875, Name:earle_martin
PR: 12.87, ID:6946, Name:useight
PR: 12.69, ID:3352, Name:crzrussian
\end{verbatim}
}

Although we do not know how Wikipedia administrators are selected, after checking their usernames, those we checked were all engaged in Wikipedia administration during 2008. This shows that distributed computing is not just an airy idea in papers or academics, but can also work well in solving practical problems.

All necessary files and source code are put in the \texttt{PageRank} folder.


% \chapauthor{J. P. Balthasar Mueller}
\chapter{Combinatorics}

\begin{multicols}{2}[\subsubsection*{Contents of this chapter}]
   \printcontents{}{1}{\setcounter{tocdepth}{2}}
\end{multicols}

\section{Combinatorial Identities and Expansions}

% binomial coefficients
\subsection{Binomial Coefficients and Binomial Expansions}

For two positive integers $n$ and $k$, the binomial coefficient "$n$ choose $k$" is:

\begin{equation}
{n \choose k} = \left\{ \begin{array}{c}
\frac{n!}{k!(n-k)!}\ \ \mathrm{for\ }n\geq k\\
\\
0 \ \ \mathrm{otherwise}
\end{array}\right.
\end{equation}

The term can be defined for negative arguments, which is comes up often when working with generating functions.

\begin{equation}
{-n \choose k} = \left\{\begin{array}{c}
(-1)^k { n+k-1 \choose k}\ \ \mathrm{for\ }n\geq k\\
\\
0 \ \ \mathrm{otherwise}
\end{array}\right.
\end{equation}

\begin{equation}
{-n \choose -k} = \left\{\begin{array}{c}
(-1)^{k-n} {k-1 \choose k-n}\ \ \mathrm{for\ }n\geq k\\
\\
0 \ \ \mathrm{otherwise}
\end{array}\right.
\end{equation}

The generalizations can, for example, be derived using symmetry arguments and the Gamma function, which is the generalization of the factorial to non-integers (cf.  \citeasnoun{kronenburg2011binomial}).

% Binomial Expansion
The binomial expansion can be proven either by expanding the polynomial or by creating the Taylor series for the polynomial.

\begin{equation}
(x + y)^n = \sum_{k=0}^n {n \choose k} x^k y^{n-k}
\end{equation}

This holds also for negative integer exponents $n$, in which case:

\begin{equation}
\frac{1}{(y+x)^n} = \sum_{k=0}^\infty {-n \choose k} x^k y^{-n-k} = (-1)^k { n+k-1 \choose k} x^k y^{-(n+k)}
\end{equation}

\subsubsection{Derivation of the Binomial Theorem for a Negative Exponent}

Let $f(x) = \frac{1}{(y+x)^n} = (y+x)^{-n}$. The Taylor expansion about the point $x=0$ is:

\begin{equation}
f(x) = \sum_{k=0}^\infty \frac{f^{(k)}(x=0)}{k!} x^k
\label{eq:taylor}
\end{equation}

The derivatives of $f$ are:

\begin{equation}
\begin{array}{ll}
f^{(0)}(x) &= (y+x)^{-n}\\
f^{(1)}(x) &= (y+x)^{-(n+1)}(-1)n\\
f^{(2)}(x) &= (y+x)^{-(n+2)}(-1)^2 n(n+1)\\
&\vdots\\
f^{(k)}(x) &= (y+x)^{-(n+k)}(-1)^k n(n+1)\hdots	(n+k-1)\\
&=  (y+x)^{-(n+k)}(-1)^k \frac{(n+k-1)!}{(n-1)!}
\end{array}
\label{eq:derivatives}
\end{equation}

Combining Eqns. \ref{eq:taylor} and \ref{eq:derivatives} gives:

\begin{equation}
f(x) = \sum_{k=0}^\infty (-1)^k {n+k-1 \choose k} x^k y^{-(n+k)}
\end{equation}

\subsubsection{Derivation of the Binomial Theorem for a Fractional Exponent}

Following much the same logic as for a negative exponent, for a fractional exponent the Taylor series is also infinite.

Let $f(x) = (y+x)^{m}$ with $m\notin \mathbb{Z}$.

The derivatives of $f$ are:

\begin{equation}
\begin{array}{ll}
f^{(0)}(x) &= (y+x)^{m}\\
f^{(1)}(x) &= (y+x)^{m-1}m\\
f^{(2)}(x) &= (y+x)^{m-2}m(m-1)\\
&\vdots\\
f^{(k)}(x) &= (y+x)^{n-k}m(m-1)\hdots	(m-k+1)\\
\end{array}
\label{eq:derivatives2}
\end{equation}

Combining Eqns. \ref{eq:taylor} and \ref{eq:derivatives2} gives:

\begin{equation}
f(x) = \sum_{k=0}^\infty { m \choose k } x^k y^{m-k}
\end{equation}

Where, boldly, I defined ${ m \choose k}$ to mean:

\begin{equation}
{m \choose k} = \frac{m(m-1)(m-2)\hdots(m-k+2)(m-k+1)}{k!}
\end{equation}

% Multinomial Expansion
\subsection{Multinomial Expansion}

\begin{equation}
(x_1 + x_2 + x_3 + \hdots + x_k)^n = \sum_{\begin{array}{c} i_1,i_2,i_3,\hdots,i_k \\ \sum_j i_j = n\end{array}} {n \choose i_1,i_2,\hdots i_k}x^{i_1}x^{i_2}x^{i_3}\hdots x^{i_k}
\end{equation}

with:

\begin{equation}
{n \choose i_1,i_2,\hdots i_k} = \frac{n!}{i_1!i_2!,i_3!\hdots i_k!}
\end{equation}

Where the sum over all possible exponents $i_j$ so that $\sum_j i_j = n$ has ${n+k-1 \choose n}$ terms.

\subsection{Unnamed Polynomial Identity}
I don't know what this is called, but it's useful.

\begin{equation}
\prod_i^n (1-x_i) = \sum_{s=0}^{n} (-1)^s \sum_{0\leq \underbrace{i_1,i_2,...,i_s }_{\{i\}_s} \leq n} \prod_{i \in \{i\}_s} x_i
\end{equation}

Where $\{i\}_s$ is a set of $s$ indices that range between $0$ and $n$, and the sum is over all possible such sets, of which there are ${n \choose s}$.

% Factorial Expansion
\subsection{Factorial Expansion}

\begin{equation}
x^{\underline{n}} = \frac{x!}{(x-n)!} = \sum_{k=0}^n s(n,k)x^k
\end{equation}

where

\begin{equation}
s(n,k) = (-1)^{n-k}\left[\begin{array}{c} n\\k \end{array}\right]
\end{equation}

are the stirling numbers of the first kind.

% Stirling Numbers of the second kinds
\subsection{Stirling Numbers of the Second Kind}
\label{sec:stirling2}

Stirling numbers of the second kind $S(k,n)$ measure the amount of ways in which $k$ objects can be divided into $n$ non-empty groups. They give the number of onto functions from a set of $k$ distinct objects to $n$ indistinct recipients. For example: how many ways can a set of $k$ pool balls be put into $n$ bags, so that there is at least one ball in each bag. All the pool balls have numbers on them and have different colors, so that $n=2$ bags containing $[(1,2,3), (4)] $ and $[(1,2,4),(3)]$ count as different. This sort of problem is discussed at length in section \ref{twentyfoldway}

They are given by an explicit formula:

\begin{equation}
S(k,n) = \frac{1}{n!}\sum_{i=1}^n (-1)^{n-j} {n \choose j}j^k
\end{equation}

They can also be generated via the recurrence relation:

\begin{equation}
\left\{\begin{array}{c}k+1\\n\end{array}\right\} = n\left\{\begin{array}{c}k\\n\end{array}\right\} + \left\{\begin{array}{c}k\\n-1\end{array}\right\}
\end{equation}

The recurrence relation is explained by adding the combinations corresponding to two cases. If the $k+1$st object is added to one of the $n$ existing subsets with $k$ objects, then that corresponds to:

\begin{equation}
n\left\{\begin{array}{c}k\\n\end{array}\right\} = 1
\end{equation}

Possbilities. If the $k+1$st object is in a set by itself (a singleton), then the remaining objects are distributed over $n-1$ set. The combinations arising from this are:

\begin{equation}
\left\{\begin{array}{c}k\\n-1\end{array}\right\} = 1
\end{equation}

Furthermore, the following holds:

\begin{equation}
\left\{\begin{array}{c}0\\0\end{array}\right\} = 1
\end{equation}

\begin{equation}
\left\{\begin{array}{c}k\\0\end{array}\right\} = \left\{\begin{array}{c}0\\n\end{array}\right\} = 0
\end{equation}

And $S(k,n) = 0$ if $n>k$.

\input{./chapters/sections/comb_twentyfoldway.tex}
\input{./chapters/sections/comb_generatingfuncs.tex}


\chapter{Python Classes}

The built-in types, like strings have functions associated with
them. So, for example, if you needed a string converted to uppercase,
you would call it's \pyfunction{upper()} function:
-
\begin{Verbatim}
my_string = "houston, we have a problem!"
louder_string = my_string.upper()
\end{Verbatim}
This would set \pyvar{louder\_string} to "HOUSTON, WE HAVE A PROBLEM!"
When a function is associated with a datatype like this, it called a
\emph{method}. A datatype with methods is known as a \emph{class}. The
data of that type is known as \emph{instance} of that class. For
example, in the example, we would say ``\pyvar{my\_string} is an instance of
the class \pytype{str}. \pytype{str} has a method called \pyfunction{upper}''

The function \pyfunction{type} will tell you the type of any data:
\begin{Verbatim}
  print(type(my_string))
\end{Verbatim}
This will output
\begin{Verbatim}
<class 'str'>
\end{Verbatim}

A class can also define operators.  \pyfunction{+}, for example, is
redefined by \pytype{str} to concatenate strings together:
\begin{Verbatim}
long_string = "I saw " + "15 people"
\end{Verbatim}

\section{Making a Polynomial class}

You have created a bunch of useful python functions for dealing with
polynomials. Notice how each one has the word ``polynomial'' in the
function name like \pyfunction{derivative\_of\_polynomial}.  Wouldn't it
be more elegant if you had a Polynomial class with a
\pyfunction{derivative} method? Then you could use your polynomial
like this:\index{class in python}
\begin{Verbatim}
a = Polynomial([9.0, 0.0, 2.3])
b = Polynomial([-2.0, 4.5, 0.0, 2.1])

print(a, "plus", b , "is", a+b)
print(a, "times", b , "is", a*b)
print(a, "times", 3 , "is", a*3)
print(a, "minus", b , "is", a-b)

c = b.derivative()

print("Derivative of", b ,"is", c)
\end{Verbatim}

And it would output:
\begin{Verbatim}
2.30x^2 + 9.00 plus 2.10x^3 + 4.50x + -2.00 is 2.10x^3 + 2.30x^2 + 4.50x + 7.00
2.30x^2 + 9.00 times 2.10x^3 + 4.50x + -2.00 is 4.83x^5 + 29.25x^3 + -4.60x^2 + 40.50x + -18.00
2.30x^2 + 9.00 times 3 is 6.90x^2 + 27.00
2.30x^2 + 9.00 minus 2.10x^3 + 4.50x + -2.00 is -2.10x^3 + 2.30x^2 + -4.50x + 11.00
Derivative of 2.10x^3 + 4.50x + -2.00 is 6.30x^2 + 4.50
\end{Verbatim}

Create a file for your class definition called \filename{Polynomial.py}. Enter the following:
\begin{Verbatim}
class Polynomial:
    def __init__(self, coeffs):
        self.coefficients = coeffs.copy()

    def __repr__(self):
        # Make a list of the monomial strings
        monomial_strings = []

        # For standard form we start at the largest degree
        degree = len(self.coefficients) - 1

        # Go through the list backwards
        while degree >= 0:
            coefficient = self.coefficients[degree]

            if coefficient != 0.0:
                # Describe the monomial
                if degree == 0:
                    monomial_string = "{:.2f}".format(coefficient)
                elif degree == 1:
                    monomial_string = "{:.2f}x".format(coefficient)
                else:
                    monomial_string = "{:.2f}x^{}".format(coefficient, degree)

                # Add it to the list
                monomial_strings.append(monomial_string)

            # Move to the previous term
            degree = degree - 1

        # Deal with the zero polynomial
        if len(monomial_strings) == 0:
            monomial_strings.append("0.0")

        # Separate the terms with a plus sign
        return " + ".join(monomial_strings)

    def __call__(self, x):
        sum = 0.0
        for degree, coefficient in enumerate(self.coefficients):
            sum = sum + coefficient * x ** degree
        return sum

    def __add__(self, b):
        result_length = max(len(self.coefficients), len(b.coefficients))
        result = []
        for i in range(result_length):
            if i < len(self.coefficients):
                coefficient_a = self.coefficients[i]
            else:
                coefficient_a = 0.0

            if i < len(b.coefficients):
                coefficient_b = b.coefficients[i]
            else:
                coefficient_b = 0.0
            result.append(coefficient_a + coefficient_b)

        return Polynomial(result)

    def __mul__(self, other):

        # Not a polynomial?
        if not isinstance(other, Polynomial):
            # Try to make it a constant polynomial
            other = Polynomial([other])

        # What is the degree of the resulting polynomial?
        result_degree = (len(self.coefficients) - 1) + (len(other.coefficients) - 1)

        # Make a list of zeros to hold the coefficents
        result = [0.0] * (result_degree + 1)

        # Iterate over the indices and values of a
        for a_degree, a_coefficient in enumerate(self.coefficients):

            # Iterate over the indices and values of b
            for b_degree, b_coefficient in enumerate(other.coefficients):

                # Calculate the resulting monomial
                coefficient = a_coefficient * b_coefficient
                degree = a_degree + b_degree

                # Add it to the right bucket
                result[degree] = result[degree] + coefficient

        return Polynomial(result)

    __rmul__ = __mul__

    def __sub__(self, other):
        return self + other * -1.0

    def derivative(self):

        # What is the degree of the resulting polynomial?
        original_degree = len(self.coefficients) - 1
        if original_degree > 0:
            degree_of_derivative = original_degree - 1
        else:
            degree_of_derivative = 0

        # We can ignore the constant term (skip the first coefficient)
        current_degree = 1
        result = []

        # Differentiate each monomial
        while current_degree < len(self.coefficients):
            coefficient = self.coefficients[current_degree]
            result.append(coefficient * current_degree)
            current_degree = current_degree + 1

        # No terms? Make it the zero polynomial
        if len(result) == 0:
            result.append(0.0)

        return Polynomial(result)
\end{Verbatim}

Create a second file called \filename{test\_polynomial.py} to test it:
\begin{Verbatim}[commandchars=\\\{\}]
from Polynomial import Polynomial

a = Polynomial([9.0, 0.0, 2.3])
b = Polynomial([-2.0, 4.5, 0.0, 2.1])

print(a, "plus", b , "is", a+b)
print(a, "times", b , "is", a*b)
print(a, "times", 3 , "is", a*3)
print(a, "minus", b , "is", a-b)

c = b.derivative()

print("Derivative of", b ,"is", c)

slope = c(3)
print("Value of the derivative at 3 is", slope)

\end{Verbatim}

Run the test code:
\begin{Verbatim}
python3 test_polynomial.py
\end{Verbatim}


\section{Supervised classification}

As discussed in section \vref{uns-class}, text classification is the
problem of associating texts to classes, that are identified by labels.

\paragraph{Training data set}
A set of data examples are provided to the classifier to learn
the mapping between data and labels. The goal is then to
predict unlabeled data.

\paragraph{Unsupervised vs Supervised}
In the first case we don't have any information about
classification criterions, while in the second we have classes
associated to features, represented by the data points associated with
labels.

\subsection{Models}

\paragraph{Decision trees}
The application of this model to text classification is rather
easy.
One could compute a vectorization of the text, in one of the many ways
seen and apply the tree learning algorithm.
This means imposing constraints on the presence and relevance
of a certain word in a document.

One of the pros of having a decision tree is quick and easy visualization.
This means being able to see how the model computes a classification,
to potentially avoid unethical decisions.

\paragraph{Rule-based classifiers}
These classifiers are based on rules, they are similar to
decision trees, basically the antecedent of
the rule is a subset of words and the consequent is a label.

\paragraph{Naive Bayes Classifiers}
The idea is that data has been generated by a mixture of $k$ components,
where $k$ is the number of classes.
The goal is to estimate the probability
\begin{equation}
    \begin{aligned}
        P(\mathit{class} \;|\;\mathit{features} ) &=
        \frac{P(\mathit{class})
        P(\mathit{features}\;|\;\mathit{class})}{P(\mathit{features})}
        \\&= P(\mathit{class})
        P(\mathit{features}\;|\;\mathit{class})
    \end{aligned}
\end{equation}
where the probability of a class is the size of the class over the size
of the corpus.
Also, the probability of features given the class, is simply
the product of the count of a given words over the total count of the words in
the class.

\paragraph{K-NN classifiers}
The application of the model to text is straight forward after
text vectorization.

\paragraph{Linear classifiers}
The idea is to learn an hyperplane separating data, again, this is
not different to any linear classifier task.

The main idea is to minimize a loss function plus
a regularizer term.

\paragraph{Support Vector Machines}
A linear classifier is the Support Vector Machine,
the goal is to find the optimal hyperplane separating data,
i.e. the one with greater margin between the two classes.

To accommodate non linearly separable data one could use the kernel
trick. Basically we perform a transformation of the dataset
to map it in a high dimensional space where data point could be
linearly separable.  There are many possible kernels.

\subsection{Classification strategies}
When the system is efficient in performing binary classification
we can apply it to multi-class classification by transforming the
problem in a binary task.

\paragraph{One vs Rest}
The goal here is to learn a binary classifier between a class and all
the others.

\paragraph{One vs One}
The idea here is to train a classifier between every pair of classes.

\subsection{Multi-label classification}
To assign multiple labels to data point we need to exploit
something in the previous classifiers.

For instance, we could use the distance of the many one vs one classifiers
as confidence measures for labels.

Another way is to take the power set of classes and train many classifiers
as before. We are left with a multi-class classification
where each class is a combination of labels.

\subsection{Hyperparameters tuning  and evaluation}

\paragraph{Model selection}
The activity of tuning hyperparameters to find the
best model instance to solve a task.

\paragraph{Cross Validation}
The idea is to split the dataset in $k$ folds and consider
$k-1$ folds as training and the last one as test set.
We iterate over the possible training sets and average the error.

\paragraph{Confusion matrix}
We can arrange the errors in a confusion matrix to see
how many elements of class $i$ are classified as class $j$.
Obviously the goal is to have positive values only on the
diagonal.


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

TEAL (Tool for Economic AnaLysis) is a RAVEN plug-in that leverages Uncertainty Quantification, Probabilistic Risk Assessment, Parameter Optimization
and Data Analysis framework in RAVEN to perform basic cash flow analyses.

TEAL enables computation of the \textbf{NPV (Net Present Value)}, \textbf{IRR (Internal Rate of Return)}, and the \textbf{PI (Profitability Index)} with RAVEN.
Furthermore, it allows NPV, IRR, or PI search (i.e. TEAL will compute a multiplicative value such as the production cost) so that the NPV, IRR or PI has a desired value.
The plugin allows for a generic definition of cash flows, which are driven by variables provided by RAVEN. Furthermore, TEAL includes flexible options to deal with taxes, inflation, and discounting, and
offers capabilities to compute a combined cash flow for components with different component lifetimes.

\textbf{NPV}: computes the Net Present Value according to Eq. \ref{eq:NPV}.
\begin{equation}\label{eq:NPV}
NPV=\sum_{y=0}^{N}\frac{CF_{y}}{(1+DiscountRate)^{y}}
\end{equation}

The sum runs over the years $y=0$ to $N$. The net cash flows $CF_{y}$ are the sum of all cash flows defined in the indicator block (discussed later).
$N$ is the least common multiple (LCM) of all component lifetimes involved. This guarantees that the NPV is computed for a time span so that all components reach their end of life in the same year.
The individual component cash flows are repeated until the LCM is reached.

For example, let's assume the calculation involves two components, \textit{Component1} and \textit{Component2},
 with life times of 60 years and 40 years respectively. $N$ will be 120 years where two successive \textit{Component1} and three successive \textit{Component2} will be built. For every "building year,"
the cash flow will be summed for the last year (of the old component) and the year zero (for the newly built component). Table \ref{tbl:cashflows} shows an example.
TEAL computes the the cash flows and the NPV, and sends the variable `NPV' back to RAVEN to be added to the output data object.

\begin{table}[]
\centering
\caption{Example cash flows for NPV calculation.}
\label{tbl:cashflows}
\begin{tabular}{ll|l|l|l|l|l|ll}
\cline{3-4} \cline{6-7}
                           &  & \multicolumn{2}{l|}{Compo. 1}                                                                                &  & \multicolumn{2}{l|}{Compo. 2}                                                                                &                       &                                                                                \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{Year} &  & \begin{tabular}[c]{@{}l@{}}Comp. \\ lifetime\end{tabular} & \begin{tabular}[c]{@{}l@{}}Cash Flow\\ (year)\end{tabular}         &  & \begin{tabular}[c]{@{}l@{}}Compo. \\ Lifetime\end{tabular} & \begin{tabular}[c]{@{}l@{}}  Cash Flow \\ (year) \end{tabular}      & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Total Net Cash flow \\ ($CF_{y}$)         \end{tabular}}   \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{0}    &  & 0                                                         & $CF^{comp1}_{0}$                                                          &  & 0                                                          & $CF^{comp2}_{0}$                                                           & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{0} + CF^{comp2}_{0}$ }                                     \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{1}    &  & 1                                                         & $CF^{comp1}_{1}$                                                          &  & 1                                                          & $CF^{comp2}_{1}$                                                           & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{1} + CF^{comp2}_{1}$ }                                     \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{…}    &  &                                                           &                                                                    &  &                                                            &                                                                     & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{}                                                                                \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{39}   &  & 39                                                        & $CF^{comp1}_{39}$                                                         &  & 39                                                         & $CF^{comp2}_{39}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{39} + CF^{comp2}_{39}$ }                                   \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{40}   &  & 40                                                        & $CF^{comp1}_{40}$                                                         &  & 40 and 0                                                   & \begin{tabular}[c]{@{}l@{}}$CF^{comp2}_{40}$  \\ $+ CF^{comp2}_{0}$ \end{tabular} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}$CF^{comp1}_{40} + CF^{comp2}_{40}$ \\  $+ CF^{comp2}_{0}$ \end{tabular}} \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{41}   &  & 41                                                        & $CF^{comp1}_{41}$                                                         &  & 1                                                          & $CF^{comp2}_{1}$                                                           & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{41} + CF^{comp2}_{1}$ }                                  \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{…}    &  &                                                           &                                                                    &  &                                                            &                                                                     & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{}                                                                                \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{59}   &  & 59                                                        & $CF^{comp1}_{59}$                                                         &  & 19                                                         & $CF^{comp2}_{19}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{59} + CF^{comp2}_{19}$ }                                 \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{60}   &  & 60 and 0                                                  & \begin{tabular}[c]{@{}l@{}}$CF^{comp1}_{60}$ \\ $+ CF^{comp1}_{0}$ \end{tabular} &  & 20                                                         & $CF^{comp2}_{20}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}$CF^{comp1}_{60} + CF^{comp1}_{0}$ \\ $+ CF^{comp2}_{20}$ \end{tabular}} \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{61}   &  & 1                                                         & $CF^{comp1}_{1}$                                                          &  & 21                                                         & $CF^{comp2}_{21}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{1} + CF^{comp2}_{21}$ }                                                          \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{…}    &  &                                                           &                                                                    &  &                                                            &                                                                     & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{}                                                                                \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{79}   &  & 19                                                        & $CF^{comp1}_{19}$                                                         &  & 39                                                         & $CF^{comp2}_{39}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{19} + CF^{comp2}_{39}$ }                                                         \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{80}   &  & 20                                                        & $CF^{comp1}_{20}$                                                         &  & 40 and 0                                                   & \begin{tabular}[c]{@{}l@{}}$CF^{comp2}_{40}$ \\  $+ CF^{comp2}_{0}$ \end{tabular} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}$CF^{comp1}_{20} + CF^{comp2}_{40}$ \\ $+ CF^{comp2}_{0}$ \end{tabular}}  \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{81}   &  & 21                                                        & $CF^{comp1}_{21}$                                                         &  & 1                                                          & $CF^{comp2}_{1}$                                                           & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{21} + CF^{comp2}_{1}$ }                                                          \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{…}    &  &                                                           &                                                                    &  &                                                            &                                                                     & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{}                                                                                \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{119}  &  & 59                                                        & $CF^{comp1}_{59}$                                                         &  & 39                                                         & $CF^{comp2}_{39}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{59} + CF^{comp2}_{39}$ }                                                         \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\multicolumn{1}{|l|}{120}  &  & 60                                                        & $CF^{comp1}_{60}$                                                         &  & 40                                                         & $CF^{comp2}_{40}$                                                          & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{$CF^{comp1}_{60} + CF^{comp2}_{40}$}                                                         \\ \cline{1-1} \cline{3-4} \cline{6-7} \cline{9-9}
\end{tabular}
\end{table}

\textbf{PI}: computes the Profitability Index according to Eq. \ref{eq:PI}.
\begin{equation}\label{eq:PI}
PI=\frac{NPV}{Initial\_investment}
\end{equation}
NPV is calculated as explained above and the $Initial\_investment$ is the Total Net Cash flow at year zero ($CF_{0}$ in the example above).
TEAL computes the the cash flows and the PI, and sends the variable`'PI' back to RAVEN to be added to the output data object.

\textbf{IRR}: computes the Internal Rate of Return according to Eq. \ref{eq:IRR}.
\begin{equation}\label{eq:IRR}
0=\sum_{y=0}^{N}\frac{CF_{y}}{(1+IRR)^{y}}
\end{equation}
Similar to the NPV, the sum runs over the years $y=0$ to $N$. The net cash flows $CF_{y}$ are the sum of all cash flows defined in the indicator block
(see explanation of NPV above for details). $N$ is the LCM of all component life times involved.
TEAL computes the cash flows and the IRR, and sends the variable `IRR' back to RAVEN to be added to the output data object.

\textbf{NPV\_search}: The NPV search finds a multiplier `$x$' that multiplies some of the cash flows, so that the NPV has a desired value (defined by the \xmlAttr{target} attribute). The equation solved is shown in Eq. \ref{eq:NPV_search}.
\label{subsec:NPV_search}
\begin{equation}\label{eq:NPV_search}
'target'=\sum_{y=0}^{N}\frac{CF^{dep\_on\_x}_{y}}{(1+DiscountRate)^{y}}x + \sum_{y=0}^{N}\frac{CF^{not\_dep\_on\_x}_{y}}{(1+DiscountRate)^{y}}
\end{equation}


% This is part of the TFTB Tutorial.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file tutorial.tex for copying conditions.

\section{The reassignment method}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\index{reassignment}
\subsection{Introduction}
%''''''''''''''''''''''''

  Bilinear time-frequency distributions, presented in the previous two
sections, offer a wide range of methods designed for the analyze of non
stationary signals. Nevertheless, a critical point of these methods is
their readability, which means both a good concentration of the signal
components and no misleading interference terms. Some efforts have been
made recently in that direction, and in particular a general methodology
referred to as {\it reassignment}. The purpose of this section is to
present this methodology, to illustrate it on different examples, and to
make the link with connected approaches (see \cite{AUG94}, \cite{KOD76} and
\cite{AUG95} for more details on reassignment).

\subsection{The reassignment of the spectrogram}
%'''''''''''''''''''''''''''''''''''''''''''''''

  The original idea of reassignment was introduced in an attempt to
improve the spectrogram. Indeed, as any other bilinear energy
distribution, the spectrogram is faced with an unavoidable trade-off
between the reduction of misleading interference terms and a sharp
localization of the signal components.

  Let us recall the expression of the spectrogram as a 2D-convolution
of the Wigner-Ville distribution of the signal by the WVD of the
analysis window :
\begin{eqnarray}
\label{spectro2}
S_x(t,\nu;h)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} W_x(s,\xi)\
W_h(t-s,\nu-\xi)\ ds\ d\xi.
\end{eqnarray}
Therefore, this distribution reduces the interference terms of the signal's
WVD, but at the expense of opposed time and frequency resolutions, and of
biased marginals and first order moments. However, a closer look at
expression (\ref{spectro2}) shows that $W_h(t-s,\nu-\xi)$ delimits a
time-frequency domain at the vicinity of the $(t,\nu)$ point, inside which
a weighted average of the signal's WVD values is performed. The key point
of the reassignment principle is that these values have no reason to be
symmetrically distributed around $(t,\nu)$, which is the geometrical center
of this domain. Therefore, their average should not be assigned at this
point, but rather at the center of gravity of this domain, which is much
more representative of the local energetic distribution of the
signal. Reasoning with a mechanical analogy, the local energy distribution
$W_h(t-s,\nu-\xi) W_x(s,\xi)$ (as a function of $s$ and $\xi$) can be
considered as a mass distribution, and it is much more accurate to assign
the total mass (i.e. the spectrogram value) to the center of
gravity of the domain rather than to its geometrical center.

  This is exactly how the reassignment method proceeds : it moves each
value of the spectrogram computed at any point $(t,\nu)$ to another point
$(\hat{t},\hat{\nu})$ which is the center of gravity of the signal energy
distribution around $(t,\nu)$ :
\begin{eqnarray}
\label{hatt}
\hat{t}(x;t,\nu)={
\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} s\
W_h(t-s,\nu-\xi)\ W_x(s,\xi)\ ds\ d\xi
\over
\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} W_h(t-s,\nu-\xi)\
W_x(s,\xi)\ ds\ d\xi}\\
\label{hatnu}
\hat{\nu}(x;t,\nu)=\frac{\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
\xi\ W_h(t-s,\nu-\xi)\ W_x(s,\xi)\ ds\ d\xi}
 {\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} W_h(t-s,\nu-\xi)\
W_x(s,\xi)\ ds\ d\xi}
\end{eqnarray}
and thus leads to a reassigned spectrogram, whose value at any point
$(t',\nu')$ is the sum of all the spectrogram values reassigned to this
point :
\begin{eqnarray}
\label{reasspectro}
 S_x^{(r)}(t',\nu';h)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
S_x(t,\nu;h)\ \delta(t'-\hat{t}(x;t,\nu))\ \delta(\nu'-\hat{\nu}(x;t,\nu))\
dt\ d\nu \ \
\end{eqnarray}
One of the mostly interesting properties of this new
distribution is that it also uses the phase information of the
short-time Fourier transform, and not only its squared modulus as in
the spectrogram. This can be seen from the following expressions of
the reassignment operators :
\begin{eqnarray*}
	\hat{t}(x;t,\nu)  = -\frac{d\Phi_x(t,\nu;h)}{d\nu}\\
	\hat{\nu}(x;t,\nu) = \nu+\frac{d\Phi_x(t,\nu;h)}{dt}
\end{eqnarray*}
where $\Phi_x(t,\nu;h)$ is the phase of the STFT of $x$ :
$\Phi_x(t,\nu;h)=\arg(F_x(t,\nu;h))$. However, these expressions do not
lead to an efficient implementation, and have to be replaced by the
following ones :
\begin{eqnarray*}
\hat{t}(x;t,\nu)=t-\Re\left\{\frac{F_x(t,\nu;T_h)\
 F_x^*(t,\nu;h)}{|F_x(t,\nu;h)|^2}\right\}\\
\hat{\nu}(x;t,\nu)=\nu-\Im\left\{\frac{F_x(t,\nu;D_h)\
 F_x^*(t,\nu;h)}{|F_x(t,\nu;h)|^2}\right\}
\end{eqnarray*}
where $T_h(t)=t\times h(t)$ and $D_h(t)=\frac{dh}{dt}(t)$. Reassigned
spectrograms are therefore very easy to implement, and do not require a
drastic increase in computational complexity.

  Finally, it should also be underlined that the reassigned spectrogram,
though no longer bilinear, satisfies the time and frequency shifts
covariance, the energy conservation (provided that $h(t)$ is of unit
energy), and the non-negativity property. It cans also be shown that, since
the WVD is perfectly localized on linear chirp signals and impulses, any
reassigned spectrogram also satisfies this property :
\begin{eqnarray*}
x(t)=A\ \exp\left\{j\{\nu_0 t+\alpha
t^2/2\}\right\}&\Rightarrow&\hat{\nu}=\nu_0+\alpha\hat{t}\\
x(t)=A\ \delta(t-t_0)&\Rightarrow&\hat{t}=t_0.
\end{eqnarray*}
  Before presenting the generalization of this method to the Cohen's class
and to the affine class, let us have a look at the readability improvement
obtained by the reassigned spectrogram on an example of multi-component
signal. The reassigned spectrogram is available thanks to the M-file
\index{\ttfamily tfrrsp}{\ttfamily tfrrsp.m}. The result is compared to the
spectrogram and to the "ideal" representation \index{\ttfamily
tfrideal}({\ttfamily tfrideal.m}) based on the knowledge of the
instantaneous frequency law of each component :
\begin{verbatim}
     >> N=128; [sig1 ifl1]=fmsin(N,0.15,0.45,100,1,0.4,-1);
     >> [sig2 ifl2]=fmhyp(N,[1 .5],[32 0.05]);
     >> sig=sig1+sig2;
     >> tfrideal([ifl1 ifl2]);
     >> figure; tfrrsp(sig);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=12cm
\epsfysize=8cm
\centerline{\epsfbox{figure/re1fig1.eps}}
\caption{\label{Re1fig1}Reassignment of the spectrogram on a synthetic
signal composed of a sinusoidal frequency modulation simultaneously with a
hyperbolic frequency modulation : comparison with the ``ideal''
time-frequency representation and with the spectrogram}
\end{figure}

The file \index{\ttfamily tfrrsp}{\ttfamily tfrrsp.m} allows you to display
the spectrogram itself or its reassigned version. The improvement given by
the reassignment method is obvious : the two components are much better
localized and almost perfectly concentrated, and there are very few
cross-terms.

\subsection{Reassignment of the Cohen's class representations}
%'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  The presentation of the reassignment principle done above allows a
straightforward extension of its use to other distributions. Indeed,
if we consider the general expression of a distribution of the Cohen's
class as a 2D-convolution of the WVD,
\begin{eqnarray*}
C_x(t,\nu;\Pi)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
\Pi(t-s,\nu-\xi)\ W_x(s,\xi)\ ds\ d\xi,
\end{eqnarray*}
replacing the particular smoothing kernel $W_h(u,\xi)$ in expressions
(\ref{hatt}), (\ref{hatnu}) and (\ref{reasspectro}) by an arbitrary kernel
$\Pi(s,\xi)$ simply defines the reassignment of any member of the Cohen's
class :
\begin{eqnarray*}
\hat{t}(x;t,\nu) &=&
\frac{\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} s\
\Pi(t-s,\nu-\xi)\ W_x(s,\xi)\ ds\ d\xi}
 {\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \Pi(t-s,\nu-\xi)\
W_x(s,\xi)\ ds\ d\xi}\\
\hat{\nu}(x;t,\nu)&=&
\frac{\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \xi\
\Pi(t-s,\nu-\xi)\ W_x(s,\xi)\ ds\ d\xi}
 {\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \Pi(t-s,\nu-\xi)\
W_x(s,\xi)\ ds\ d\xi}\\
C_x^{(r)}(t',\nu';\Pi)&=&\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
C_x(t,\nu;\Pi)\ \delta(t'-\hat{t}(x;t,\nu))\
\delta(\nu'-\hat{\nu}(x;t,\nu))\ dt\ d\nu.
\end{eqnarray*}
The resulting reassigned distributions efficiently combine a reduction of
the interference terms provided by a well adapted smoothing kernel and an
increased concentration of the signal components achieved by the
reassignment. From a theoretical point of view, these distributions are
covariant by time and frequency shifts, and are perfectly localized for
linear chirp signals and impulses. Finally, for the most common cases, such
as the SPWVD and the Reduced Interference Distributions (see section
\ref{SPWVD} and \ref{RID}), the reassignment operators $\hat{t}(x;t,\nu)$ and
$\hat{\nu}(x;t,\nu)$ are almost as easy to compute as for the spectrogram.

\subsection{Reassignment of the affine class representations}
%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Similarly, the reassignment method can also be applied to the
time-scale energy distributions. Starting from the general
expression :
\begin{eqnarray*}
\Omega_x(t,a;\Pi)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
\Pi(s/a,\nu_0-a \xi)\ W_x(t-s,\xi)\ ds\ d\xi
\end{eqnarray*}
we can see that the representation value at any point $(t,a=\nu_0/\nu)$ is
the average of the weighted WVD values on the points $(t-s,\xi)$ located in
a domain centered on $(t,\nu)$ and bounded by the essential support of
$\Pi$. In order to avoid the resultant signal components broadening while
preserving the cross-terms attenuation, it seems once again appropriate to
assign this average to the center of gravity of these energy measures,
whose coordinates are :
\begin{eqnarray*}
\hat{t}(x;t,\nu) &=&
t-\frac{\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} s\
\Pi(s/a,\nu_0-a \xi)\ W_x(t-s,\xi)\ ds\ d\xi}
 {\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \Pi(s/a,\nu_0-a \xi)\
W_x(t-s,\xi)\ ds\ d\xi}	\\
\hat{\nu}(x;t,\nu) &=& \frac{\nu_0}{\hat{a}(x;t,\nu)}\\
 &=& \frac{\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \xi\
\Pi(s/a,\nu_0-a \xi)\ W_x(t-s,\xi)\ ds\ d\xi}
   {\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \Pi(s/a,\nu_0-a \xi)\
W_x(t-s,\xi)\ ds\ d\xi}
\end{eqnarray*}
rather than to the point $(t,a=\nu_0/\nu)$ where it is computed. The value
of the resulting modified time-scale representation on any point
$(t',a')$ is then the sum of all the representation values moved to this
point :
\begin{eqnarray*}
\Omega_x^{(r)}(t',a';\Pi)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}
{a'}^2\ \Omega_x(t,a;\Pi)\
 \delta(t'-\hat{t}(x;t,a))\ \delta(a'-\hat{a}(x;t,a))\ dt\ \frac{da}{a^2}.
\end{eqnarray*}
As for the Cohen's class, it can be shown that these modified
distributions are no longer bilinear, but are covariant by time shifts
and time scalings, distribute the energy of the signal on the whole
time-scale plane, and are also perfectly localized for chirps and
impulses.

\subsection{Numerical examples}
%''''''''''''''''''''''''''''''
  In order to evaluate the benefits of the reassignment method in practical
applications, a comparison of the experimental results provided by some
time-frequency representations and their modified versions is shown in this
section. The analyzed signal is a 128-points signal made up of a sinusoidal
frequency modulation followed by a pure tone simultaneously with a chirp
component :
\begin{verbatim}
     >> [sig1 ifl1]=fmsin(60,0.15,0.35,50,1,0.35,1);
     >> [sig2 ifl2]=fmlin(60,0.3,0.1);
     >> [sig3 ifl3]=fmconst(60,0.4);
     >> sig=[sig1;zeros(8,1);sig2+sig3];
     >> iflaw=zeros(128,2);
     >> iflaw(:,1)=[ifl1;NaN*ones(8,1);ifl2];
     >> iflaw(:,2)=[NaN*ones(68,1);ifl3];
\end{verbatim}
We first plot the instantaneous frequency laws (obtained by {\ttfamily
tfrideal}), to which the proposed solutions should be as near as possible,
and the WVD of this signal (see the first two plots of figure
\ref{Re1fig2})\,:
\begin{verbatim}
     >> tfrideal(iflaw);
     >> figure; tfrwv(sig);
\end{verbatim}
With the WVD, the signal components are well localized, but the numerous
cross-terms make the figure hardly readable. If we now consider the
smoothed pseudo-WVD and its reassigned version (see the third and fourth
plots of fig. \ref{Re1fig2}),
\begin{verbatim}
     >> tfrrspwv(sig);
\end{verbatim}
we can see that the smoothing done by the SPWVD almost completely suppress
the cross terms, but the signal components localization becomes
coarser. The improvement given by the reassignment method is obvious : all
components are much better localized, leading to a nearly ideal
representation.
\begin{figure}[htb]
\epsfxsize=12cm
\epsfysize=12cm
\centerline{\epsfbox{figure/re1fig2.eps}}
\caption{\label{Re1fig2}Comparison of different time-frequency
distributions and their reassigned version (1/3) : the analyzed signal is
composed of three components, as can be clearly seen on the first plot
representing the instantaneous frequency laws of the components. The other
plots are the Wigner-Ville distribution, the smoothed pseudo Wigner-Ville
distribution and its reassigned version}
\end{figure}
The next distributions we consider are the spectrogram (see the first two
plots of fig. \ref{Re1fig3}) and the Morlet scalogram (see the third and
fourth plots of fig. \ref{Re1fig3})\,:
\begin{verbatim}
     >> figure(1); tfrrsp(sig);
     >> figure(2); tfrrmsc(sig);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=12cm
\epsfysize=12cm
\centerline{\epsfbox{figure/re1fig3.eps}}
\caption{\label{Re1fig3}Comparison of different time-frequency
distributions and their reassigned version (2/3) : the spectrogram and the
Morlet scalogram}
\end{figure}
These two distributions present nearly no cross terms, except at
the bottom of the sinusoid and around time $t=64$. But the time and
frequency resolutions are not good, especially at low frequencies in the
case of the scalogram. The reassignment method improves considerably these
localizations, and the reassigned spectrogram is even perfectly
concentrated for the chirp components. The result obtained with the
modified scalogram is less good, especially at low frequencies where the
time-resolution is really inadequate.

Finally, we represent the pseudo-Page and the pseudo Margenau-Hill
distributions with their reassigned version (see fig. \ref{Re1fig4})\,:
\begin{verbatim}
     >> figure(1); tfrrppag(sig);
     >> figure(2); tfrrpmh(sig);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=12cm
\epsfysize=12cm
\centerline{\epsfbox{figure/re1fig4.eps}}
\caption{\label{Re1fig4}Comparison of different time-frequency
distributions and their reassigned version (3/3) : the pseudo Page
distribution and the pseudo Margenau-Hill distribution}
\end{figure}
These representations (before reassignment) are hardly readable since some
cross-terms are superimposed on the signal components. Their modified
versions give much better localized signal components, but less
concentrated than in the case of the spectrogram or the SPWVD.

\subsection{Connected approaches}
%''''''''''''''''''''''''''''''''
  Connections of the reassignment method has been found with other
techniques which extract relevant information from the time-frequency
plane.

\subsubsection{Friedman's instantaneous frequency density}
\index{instantaneous frequency density}
A first example is the instantaneous frequency density : so as to take
advantage of the phase structure of the short-time Fourier transform
(STFT), Friedman simply computed at each time $t$ the histogram of the
frequency displacements $\hat{\nu}(x;t,\nu)$ of the spectrogram. The
resulting time-frequency representation is no more an energy
distribution, and could be derived as well from any other reassigned
distribution.

Here is an example of this instantaneous frequency density, obtained with
the M-file \index{\ttfamily friedman}{\ttfamily friedman.m} on the
pseudo-WVD of the previous signal (see fig. \ref{Re1fig5})\,:
\begin{verbatim}
     >> t=1:2:127; [tfr,rtfr,hat]=tfrrpwv(sig,t);
     >> friedman(tfr,hat,t,'tfrrsp',1);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=10cm
\centerline{\epsfbox{figure/re1fig5.eps}}
\caption{\label{Re1fig5}Instantaneous frequency density defined by
Friedman, computed from the frequency displacements $\hat{\nu}(x;t,\nu)$ of
the pseudo-WVD}
\end{figure}
Although some cross terms are still present, the localization of the
components is quite good, especially for the chirp components.

\subsubsection{Extraction of ridges and skeleton}
\index{ridges}\index{skeleton} Another related approach is the extraction
of {\it ridges} and {\it skeleton}. This method extracts from either the
STFT or the continuous wavelet transform (CWT) some particular sets of
curves deduced from the stationary points of their phase (see \cite{FLA93}
for more information about the stationary phase principle). Indeed,
applying the stationary phase theorem to the signal reconstruction formula
of the CWT $T_x(t,a;\Psi)$ expressed in the frequency domain :
\[X(\nu)=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \sqrt{a}\ H(a
\nu)\ T_x(t,a;\Psi)\ e^{-j2\pi \nu t}\ dt\
\frac{da}{a^2}\]
leads to particular points such that
\begin{eqnarray}
\label{hatt1}
\hat{t}(x;t,a)=t-\Phi'_h(\nu_0)\ \ \mbox{ and }\ \ \hat{a}(x;t,a)=a,
\end{eqnarray}
with $\Phi_h(\nu)=\arg\{H(\nu)\}$, and which constitute a set of curves
called the {\it horizontal ridges} of the representation.

Similarly, applying the stationary phase principle to the signal
reconstruction formula of the CWT expressed in the time domain leads to
particular points such that
\begin{eqnarray}
\label{hatt2}
\hat{t}(x;t,a)=t\ \ \mbox{ and }\ \ \hat{a}(x;t,a)=a\
\frac{\nu_0}{\phi'_h(0)},
\end{eqnarray}
with $\phi_h(t)=\arg\{h(t)\}$, and which constitute a set of curves called
the {\it vertical ridges} of the representation. These relations between
the ridges and the reassignment operators suggest to extract the ridges of
any reassigned distribution by a straightforward generalization of
expressions (\ref{hatt1}), (\ref{hatt2}).

For example, let us extract the ridges from the spectrogram of the previous
signal (see fig. \ref{Re1fig6})\,:\index{\ttfamily ridges}
\begin{verbatim}
     >> [tfr,rtfr,hat]=tfrrsp(sig);
     >> ridges(tfr,hat);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=10cm
\centerline{\epsfbox{figure/re1fig6.eps}}
\caption{\label{Re1fig6}Extraction of ridges from the spectrogram}
\end{figure}
The result is interesting : apart from some ``gaps'' present in particular
on the sinusoidal frequency modulation, this method concentrates and
localizes nearly ideally the signal in the time-frequency plane, even when
there are two components present at the same time (or at the same
frequency).

\subsection{Conclusion}
%''''''''''''''''''''''

  The reassignment method creates a modified version of a
time-frequency representation by moving the representation values away
from where they are computed. These displacements depend on the signal
and on the representation, forcing the bilinearity to be lost, but
they are still consistent with many of the representation
properties. The principle of reassignment exploits the local
structures of a distribution in both time and frequency
directions. The experimental results show that this method provides a
higher concentration in the time-frequency plane, but of course does
not remove all the cross terms.


\subsection{Connected spaces}\label{subsec:connected_sets}

\begin{definition}\label{def:connected_space}\mcite[thm. 6.1.1]{Engelking1989}
  We say that the topological space \( X \) is \term{connected} if it satisfies any of the following equivalent conditions:
  \begin{thmenum}
    \thmitem{def:connected_space/open_union} If \( X = X_1 \cup X_2 \) and \( X_1, X_2 \) are disjoint open sets, either \( X_1 \) or \( X_2 \) is empty.

    \thmitem{def:connected_space/closed_union} If \( X = X_1 \cup X_2 \) and \( X_1, X_2 \) are disjoint closed sets, either \( X_1 \) or \( X_2 \) is empty.

    \thmitem{def:connected_space/separated_union} If \( X = X_1 \cup X_2 \) and \( X_1, X_2 \) are \hyperref[def:topological_space_separation]{separated}, either \( X_1 \) or \( X_2 \) is empty.

    \thmitem{def:connected_space/clopen} The only subsets of \( X \) that are both open and closed are \( \varnothing \) and \( X \).

    \thmitem{def:connected_space/discrete_mapping} Every continuous mapping \( f: X \to \{ 0, 1 \} \) into the two-point discrete space is constant.
  \end{thmenum}
\end{definition}

\begin{definition}\label{def:locally_connected}\mcite[exer. 6.3.3]{Engelking1989}
  We say that \( X \) is \term{locally connected} if for every point \( x \in X \) and every neighborhood \( U \) of \( x \) there exists a connected set \( C \subseteq U \) such that \( x \in \Int(C) \).
\end{definition}

\begin{definition}\label{def:path_connected}\mcite[exer. 6.3.9]{Engelking1989}
  We say that a topological space is \term{path connected} if every two points can be connected via a \hyperref[def:parametric_curve]{path}.
\end{definition}

\begin{definition}\label{def:locally_path_connected}\mcite[exer. 6.3.10]{Engelking1989}
  We say that \( X \) is \term{locally path connected} if for every point \( x \in X \) and every neighborhood \( U \) of \( x \) there exists a neighborhood \( V \) of \( x \) such that for any \( y \in V \) there exists a path \( \gamma: [0, 1] \to U \) connecting \( x \) with \( y \).
\end{definition}

\begin{proposition}\label{thm:homomorphism_preserves_connectedness}
  If \( X \) is connected and \( f: X \to Y \) is a homeomorphism, then \( Y \) is also connected.
\end{proposition}
\begin{proof}
  Let \( Y = Y_1 \cup Y_2 \), where \( Y_1 \) and \( Y_2 \) are disjoint and open.

  Note that the preimages \( \gamma^{-1}(Y_1) \) and \( \gamma^{-1}(Y_2) \) are open and disjoint, hence \( X = \gamma^{-1}(Y_1) \cup \gamma^{-1}(Y_2) \). But \( X \) is connected and by \fullref{def:connected_space/open_union}, either \( \gamma^{-1}(Y_1) \) or \( \gamma^{-1}(Y_2) \) is the null set. Thus, either \( Y_1 \) and \( Y_2 \) is the null set and, again, by \fullref{def:connected_space/open_union}, \( Y \) is connected.
\end{proof}

\begin{proposition}\label{thm:path_connected_implies_connected}
  Any path connected space is connected.
\end{proposition}
\begin{proof}
  Let \( X = X_1 \cup X_2 \), where \( X_1 \) and \( X_2 \) are disjoint and open.

  Assume that both are nonzero and take \( x_1 \in X_1, x_2 \in X_2 \). Then there exists a path \( \gamma: I \to X \) with endpoints \( x_1 \) and \( x_2 \). Note that the preimages \( \gamma^{-1}(X_1) \) and \( \gamma^{-1}(X_2) \) are nonempty and open, hence cannot be separated by \fullref{def:connected_space/separated_union}. But this contradicts the disjointedness of \( X_1 \) and \( X_2 \).

  The obtained contradiction proves that \( X \) is connected.
\end{proof}


\newpage
\section{Regular Expression $\rarr$ Automaton}

\subsection{Thompson method}
Result nondeterministic with $\epsilon$-moves. Map every portion of the r.e. to a piece of the automaton. Every piece of the automaton must have a unique and final state.

\begin{figure}[H]
    \centering
    \includegraphics[width=\linewidth]{automata/thompson.png}
\end{figure}

\subsection{Berry-Sethi method}

Result deterministic but can be non-minimal.

\subsubsection{Locally Testable Languages (LOC)}

Proper subfamily of REG. Assuming $a,b \in \Sigma$, $x, y \in \Sigma^*$, define $Ini(L) = \{a | ax \in L\}$, $Fin(L) = \{b | xb \in L\}$, $Dig(L) = \{ab | xaby \in L\}$.

\begin{align*}
    L \in \text{LOC} &\iff \\
    L \setminus \{\epsilon\} = \{&x | Ini(x) \in Ini(L) \land \\
    &Dig(x) \subseteq Dig(L) \land \\
    &Fin(x) \in Fin(L) \}
\end{align*}

To prove that a language is not local provide a string $x\notin L$ s.t. $Ini(x) \in Ini(L) \land Dig(x) \subseteq Dig(L) \land Fin(x) \in Fin(L)$.

\subsubsection{Recognizer for Local Languages}

A unique initial state $q_0$, a state for each terminal, the finals are the $Fin$. If $\epsilon \in L$ then also $q_0$ is final. $q_0$ is connected to the $Ini$, and the states are connected if the pair is in $Dig$.

\subsubsection{Local Automaton Condition}
A deterministic automaton is \emph{local} if it satisfies
\begin{equation*}
    \forall a \in \Sigma, | \left\{ \delta(q, a) | q \in Q \right\} | \le 1
\end{equation*}
Informally, each arc with the same terminal character ends in the same state.

If a language is recognized by a local automaton, the language is LOC.

\subsubsection{Berry-Sethi method}

Given $e$ the initial r.e., $e'$ is the numbered version over alphabet $\Sigma_N$. Conside expression $e' \dashv$. Define for each symbol $a$ of $e'$ the set $Fol(a) = \{b | ab \in Dig(e'\dashv)\}$.

Every state is a subset of $\Sigma_N \cup \{\dashv\}$, containing all the symbols that may follow in the input. The initial state contains $Ini(e'\dashv)$. The states are generated adding transitions until a fixed point is reached. The final states are the ones containing $\dashv$.

\begin{algorithm*}[H]
    \caption{Berry-Sethi}
    \SetAlgoLined
    $q_0 = Ini(e'\dashv)$;
    $Q = \{q_0\}$;
    $\delta=\emptyset$\;
    \While{$\exists q \in Q$ not visited}{
        mark $q$ as visited\;
        \For{$b \in \Sigma$}{
            $q' = \cup_{\forall b_i \in q} Fol(b_i)$\;
            \If{$q' \notin Q$}{
                mark $q'$ as not visited\;
                $Q = Q \cup \{q'\}$
            }
            $\delta = \delta \cup \{q \xrightarrow{b} q'\}$
        }
    }
\end{algorithm*}