\documentclass{article}

\input{preamble-articles}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% We define a command \@ifnextcharamong accepting an arbitrary number of
%%%% arguments. The first is what it should do if a match is found, the second
%%%% contains what it should do when no match is found; all the other arguments
%%%% are the things it tries to find as the next character.
%%%%
%%%% For example \@ifnextcharamong{#1}{#2}{*}{\bgroup} expands #1 if the next
%%%% character is a * or a \bgroup and it expands #2 otherwise.

\makeatletter
\newcommand{\@ifnextcharamong}[2]
  {\@ifnextchar\bgroup{\@@ifnextchar{#1}{\@@ifnextcharamong{#1}{#2}}}{#2}}
\newcommand{\@@ifnextchar}[3]{\@ifnextchar{#3}{#1}{#2}}
\newcommand{\@@ifnextcharamong}[3]{\@ifnextcharamong{#1}{#2}}
\makeatother

\newcommand{\ucomp}[1]{\hat{#1}}
\newcommand{\finset}[1]{{[#1]}}

\makeatletter
\newcommand{\higherequifibsf}{\mathcal}
\newcommand{\higherequifib}[2]{\higherequifibsf{#1}(#2)}
\newcommand{\underlyinggraph}[1]{U(#1)}
\newcommand{\theequifib}[1]{{\def\higherequifibsf{}#1}}
\makeatother

\newcommand{\loopspace}[2][]{\typefont{\Omega}^{#1}(#2)}
\newcommand{\join}[2]{{#1}*{#2}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{Notes on algebraic topology}
\date{\today}

\begin{document}

\maketitle

\tableofcontents

\part{Spectral sequences}
\section{Background}
\begin{defn}
A graded $R$-module $M$ is an $R$-module which decomposes as a direct
sum
\begin{equation*}
\bigoplus_{p\in\Z} F_p M
\end{equation*}
of $R$-modules. A graded $R$-homomorphism $h:M\to N$ is an $R$-homomorphism which
decomposes into $h_p:F_pM\to F_pN$.
\end{defn}

\begin{lem}
Suppose $M$ and $N$ are graded $R$-modules. Then $M\otimes N$ is a graded
$R$-module by
\begin{equation*}
(M\otimes_R N)_i\defeq \bigoplus_{p+q=i} F_pM\otimes_R F_qN.
\end{equation*}
\end{lem}

\begin{defn}
A graded algebra is a graded $R$-module $M$ for which there are linear mappings
$\varphi_{p,q}:F_pM\otimes_R F_qM\to F_{p+q}M$, i.e.~a graded $R$-homomorphism
$\varphi:M\otimes M\to M$, which is associative in the sense
that the diagram
\begin{equation*}
\begin{tikzcd}
M\otimes M\otimes M \arrow[r,"\varphi\otimes 1"] \arrow[d,swap,"1\otimes\varphi"] &
M\otimes M \arrow[d,"\varphi"] \\ M\otimes M \arrow[r,swap,"\varphi"] & M
\end{tikzcd}
\end{equation*}
commutes.
\end{defn}

\begin{eg}
Polynomials with coefficients in $R$ forms a graded algebra. Moreover, in the
polynomial ring $R[X]$, we find that $G_pR[X]\defeq F_pR[X]/F_{p-1}R[X]\cong R$.
Since those are free modules, we have that $R[X]\cong \bigoplus_p G_pR[X]$.
\end{eg}

\section{Spectral sequences}

\subsection{Motivation from the long exact sequence of a pair}

Recall that a pair of spaces $(X,A)$ induces a long exact sequence of homology
groups
\begin{equation*}
\begin{tikzcd}
\cdots \arrow[r,"\partial_{n+1}"]
& H_n(A) \arrow[r,"i_n"]
& H_n(X) \arrow[r,"j_n"]
& H_n(X,A) \arrow[r,"\partial_n"]
& H_{n-1}(A) \arrow[r,"i_{n-1}"]
& \cdots
\end{tikzcd}
\end{equation*}
from the short exact sequence
\begin{equation*}
\begin{tikzcd}
0 \arrow[r] & C_\ast(A) \arrow[r] & C_\ast(X) \arrow[r] & C_\ast(X,A) \arrow[r] & 0
\end{tikzcd}
\end{equation*}
of chain complexes, by means of the snake lemma. This long exact sequence helps
us to compute $H_n(X)$ in terms of $H_n(A)$ and $H_n(X,A)$, which may be easier
to determine. For instance, from the long exact sequence we obtain the short
exact sequence
\begin{equation*}
\begin{tikzcd}
0 \arrow[r] & \mathrm{coker}(\partial_{n+1}) \arrow[r] & H_n(X) \arrow[r] & \mathrm{ker}(\partial_n) \arrow[r] & 0
\end{tikzcd}
\end{equation*}
and hence we have determined that $H_n(X)$ can be obtained as some element of the
group $\mathrm{Ext}(\mathrm{coker}(\partial_{n+1}),\mathrm{ker}(\partial_n))$.
In other words, $H_n(X)$ is a particular solution to an extension problem.

Note also that the long exact sequence of relative homology groups can be
presented as an exact triangle of graded $R$-homomorphisms:
\begin{equation*}
\begin{tikzcd}[column sep=0em]
\bigoplus_n H_n(C_\ast(A))
\arrow[rr,"i"] & & \bigoplus_n H_n(C_\ast(X)) \arrow[dl,"j"] \\
& \bigoplus_n H_n(C_\ast(X,A)) \arrow[ul,"\partial"]
\end{tikzcd}
\end{equation*}

The first idea of spectral sequences is to generalize the long exact sequence
of homology obtained from a pair of spaces, to an algebraic gadget obtained from
a filtration on a space, and mimic the derivation of determining the homology
group as a solution to an extension problem.

\begin{defn}
A filtration of a space X consists of a sequence
\begin{equation*}
\cdots\subseteq X_p\subseteq X_{p+1}\subseteq\cdots
\end{equation*}
such that $X=\bigcup_p X_p$ and $\bigcap_p X_p=\varnothing$. A filtration of $X$ is said to be bounded, if
$X_p=\varnothing$ for $p$ sufficiently small, and $X_p=X$ for $X$ sufficiently
large.
\end{defn}

An important class of filtered spaces is that of CW-complexes, where the filtration
$X_p$ of $X$ is given by the $p$-skeleton of $X$. Another case is where
$X_p\defeq\varnothing$ for $p<0$, $X_0\defeq A$ and $X_p\defeq X$ for $p>0$; here
we recover the old theory of the topological pair.

\begin{defn}
Given a space $X$ with a filtration, we can form the staircase diagram
\begin{footnotesize}
\begin{equation*}
\begin{tikzcd}
& \vdots \arrow[d] & & \vdots \arrow[d] \\
\cdots \arrow[r]
& H_{n+1}(X_p) \arrow[r] \arrow[d]
& H_{n+1}(X_p,X_{p-1}) \arrow[r]
& H_n(X_{p-1}) \arrow[r] \arrow[d]
& H_n(X_{p-1},X_{p-2}) \arrow[r]
& \cdots \\
\cdots \arrow[r]
& H_{n+1}(X_{p+1}) \arrow[r] \arrow[d]
& H_{n+1}(X_{p+1},X_{p}) \arrow[r]
& H_n(X_{p}) \arrow[r] \arrow[d]
& H_n(X_{p},X_{p-1}) \arrow[r]
& \cdots \\
& \vdots & & \vdots
\end{tikzcd}
\end{equation*}%
\end{footnotesize}%
in which the familiar long exact sequence of the pairs $(X_p,X_{p-1})$ run
down like a staircase.
\end{defn}

\begin{defn}
Let $X$ be a space with a filtration. Then we obtain the exact couple
\begin{equation*}
\begin{tikzcd}
A \arrow[rr,"i"] & & A \arrow[dl,"j"] \\
& E \arrow[ul,"\partial"]
\end{tikzcd}
\end{equation*}
in which $A\defeq\bigoplus_{p,n} H_n(X_p)$, and $E\defeq\bigoplus_{p,n}H_n(X_p,X_{p-1})$.
\end{defn}

We can come to such an exact couple from any filtered chain complex, which is
what we turn our attention to before continuing.

\subsection{The spectral sequence of a filtered complex}

\begin{defn}
A filtration of an $R$-module $M$ consists of a sequence
\begin{equation*}
\cdots\subseteq F_pM\subseteq F_{p+1}M\subseteq\cdots
\end{equation*}
of $R$-submodules of $M$, such that $M=\bigcup_p F_pM$ and $\bigcap_p F_pM=0$.
A filtration of $R$ is said to be bounded if $F_pM=0$ for $p$ sufficiently
small and $F_pM=M$ for $p$ sufficiently large.
\end{defn}

\begin{defn}
Let $\{M,F_pM\}$ be a graded $R$-module. The associated graded module is defined
by $G_p M\defeq F_pM/F_{p-1}M$. We obtain a short exact sequence
\begin{equation*}
\begin{tikzcd}
0 \arrow[r] & F_{p-1}M \arrow[r] & F_pM \arrow[r] & G_pM \arrow[r] & 0.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{rmk}
It would be nice if $F_pM\cong F_{p-1}M\oplus G_pM$, so that we can write
$M\cong\bigoplus_p G_pM$. Under what condition does this hold? This holds if
each $G_pM$ is a projective $R$-module, so under what conditions is this true?
\end{rmk}

\begin{defn}
A filtered chain complex is a chain complex $(C_\ast,\partial)$ together with a
filtration $\{F_pC_i\}$ of each $C_i$, such that the differential preserves the
filtration, i.e.~$\partial(F_pC_i)\subseteq F_p C_{i-1}$.

A filtration of a chain complex is said to be bounded if it is bounded in each
dimension.
\end{defn}

Let $(F_pC_\ast,\partial)$ be a filtered chain complex. We have again our
short exact sequence
\begin{equation*}
\begin{tikzcd}
0 \arrow[r] & F_{p-1} C_\ast \arrow[r] & F_p C_\ast \arrow[r] & G_p C_\ast \arrow[r] & 0
\end{tikzcd}
\end{equation*}
of chain complexes. This also gives us the long exact sequence on homology,
which we may express conveniently as the exact couple
\begin{equation*}
\begin{tikzcd}[column sep=0em]
\bigoplus_{p,q} H_{p+q}(F_pC_\ast) \arrow[rr,"i"] & & \bigoplus_{p,q} H_{p+q}(F_pC_\ast) \arrow[dl,"j"] \\
& \bigoplus_{p,q} H_{p+q}(G_p C_\ast) \arrow[ul,"k"]
\end{tikzcd}
\end{equation*}
consisting of graded $R$-homomorphisms (of which $k$ shifts in degree).

\begin{defn}
Consider an exact couple, i.e.~a commutative triangle
\begin{equation*}
\begin{tikzcd}
A \arrow[rr,"i"] & & A \arrow[dl,"j"] \\ & E \arrow[ul,"k"]
\end{tikzcd}
\end{equation*}
of $R$-modules, which is exact at every vertex. Taking $\partial^0\defeq j\circ k$,
we see that $(\partial^0)^2=0$ by exactness. We may now form the derived exact couple
\begin{equation*}
\begin{tikzcd}[column sep=0]
\mathrm{im}(i) \arrow[rr,"i'"] & & \mathrm{im}(i) \arrow[dl,"j'"] \\
& \frac{\mathrm{ker}(\partial)}{\mathrm{im}(\partial)} \arrow[ul,"k'"]
\end{tikzcd}
\end{equation*}
where
\begin{align*}
i'(i(a)) & \defeq i(i(a)) \\
j'(i(a)) & \defeq [j(a)] \\
k'([e]) & \defeq k(e)
\end{align*}
\end{defn}

\begin{rmk}
Since quotients commute with direct sums (both are colimits), it follows that
\begin{equation*}
E'\defeq \frac{\mathrm{ker}(\partial)}{\mathrm{im}(\partial)}
  \cong
\bigoplus_{p,q} \frac{\mathrm{ker}(\partial^0_{p,q})}{\mathrm{im}(\partial^0_{p,q+1})}
\end{equation*}
is a graded $R$-module. In other words, $E'$ is a direct sum of the homology
groups of the $p$-indexed family of chain complexes
\begin{equation*}
\begin{tikzcd}
\cdots \arrow[r] & E_{p,q}^0 \arrow[r,"{\partial^0_{p,q}}"] & E_{p,q-1}^0 \arrow[r] & \cdots
\end{tikzcd}
\end{equation*}
It follows that $i'$, $j'$ and $k'$ are graded
whenever $i$, $j$ and $k$ are, where $k'$ shifts down in dimension the same way
$k$ does.
\end{rmk}

\begin{comment}
\begin{defn}
We define
\begin{equation*}
E_{p,q}^0\defeq G_pC_{p+q}\defeq F_pC_{p+1}/F_{p-1}C_{p+q},
\end{equation*}

Since the differential preserves the filtration, we obtain from the differentials
well-defined $R$-homomorphisms functioning as the boundary maps in the chain complex

\end{defn}

\begin{defn}
The homology groups
\begin{equation*}
E^1_{p,q}\defeq \mathrm{ker}(\partial^0_{p,q})/\mathrm{im}(\partial^0_{p,q+1})
\end{equation*}
form again a chain complex, with boundary maps $\partial^1_{p,q}:E^1_{p,q}\to
E^1_{p,q-1}$. Thus, this process may be repeated indefinitely.
\end{defn}
\end{comment}

\begin{comment}
\begin{lem}
Let $(C_\ast,\partial)$ be a filtered chain complex. Then there is a filtration
on the homology of $C_\ast$, given by
\begin{equation*}
F_pH_i(C_\ast)\defeq\{\alpha\in H_i(C_\ast)\mid \exists_{(x\in F_p C_i)}\,\alpha=[x]\}.
\end{equation*}
\end{lem}
\end{comment}

\subsection{Convergent spectral sequences}

\begin{defn}
A spectral sequence consists of
\begin{enumerate}
\item An $R$-module $E^r_{p,q}$ for each $p,q\in\Z$ and each $r\geq 0$.
\item Differentials $\partial_r:E^r_{p,q}\to E^r_{p-r,q+r-1}$ such that
$\partial_r^2=0$ and $E^{r+1}$ is the homology of $(E^r,\partial_r)$
\end{enumerate}
\end{defn}

\begin{defn}
A spectral sequence $\{E^r,\partial_r\}$ of $R$-modules is said to converge
if for every $p,q\in\Z$, one has $\partial_r=0:E^r_{p,q}\to E^r_{p-r,q+r-1}$
for $r$ sufficiently large.
\end{defn}

\begin{rmk}
If a spectral sequence $\{E^r,\partial_r\}$ converges, then the $R$-module
$E^r_{p,q}$ is independent of $r$ for sufficiently large $r$.
\end{rmk}

\begin{thm}
Let $(F_pC_\ast,\partial)$ be a filtered complex. Then we obtain a spectral
sequence $(E^r_{p,q},\partial^r)$ defined for $r\geq 0$, with
\begin{equation*}
E^1_{p,q}\defeq H_{p+q}(G_pC_\ast).
\end{equation*}
This is the spectral sequence of filtered complexes.
\end{thm}

\begin{thm}
If $(F_pC_\ast,\partial)$ is a bounded filtered complex, then the spectral
sequence converges to
\begin{equation*}
E^\infty_{p,q}\defeq G_pH_{p+q}(C_\ast).
\end{equation*}
\end{thm}

Let $X$ be a filtered space, and let our goal be to compute the $n$-th (co)homology
group $H_n(X)$. In general, this might be a complicated task. However, it might
be easier to compute the homologies of the subcomplex $C_\ast(X_p)$, and the quotient
complex $C_\ast(X)/C_\ast(X_p)$. From this, we obtain a short exact sequence
\begin{equation*}
\begin{tikzcd}
0 \arrow[r]
& \mathrm{coker}(\delta) \arrow[r]
& H_\ast(X) \arrow[r]
& \mathrm{ker}(\delta) \arrow[r]
& 0
\end{tikzcd}
\end{equation*}

\subsection{The Serre spectral sequence}

The Serre spectral sequence relates the homology of a Serre fibration to the
homology of the fibers and the base. Thus, in some cases one can compute the
homology of the fibration in terms of the homology of the fibers and the base.

Let $\pi : X\to B$ be a fibration, with $B$ a path-connected CW-complex, and we
filter $X$ by the subspaces $X_p\defeq \pi^{-1}(B_p)$, in which $B_p$ is the
$p$-skeleton of $B$.

\begin{lem}
The spectral sequence for homology with coefficients in $G$ associated to this
filtration of $X$ converges to $H_\ast(X;G)$.
\end{lem}

\begin{thm}
Let $F\to X\to B$ be a fibration with $B$ path-connected. If $\pi_1(B)$ acts
trivially on $H_\ast(F;G)$, then there is a spectral sequence $\{E^r_{p,q},\partial_r\}$
with:
\begin{enumerate}
%\item $\partial_r : E^r_{p,q}\to E^r_{p-r,q+r-1}$ and $E^{r+1}_{p,q}=\mathrm{ker}\,d_r/\mathrm{im}\,dr$.
\item the stable terms $E^\infty_{p,n-p}$ are isomorphic to $F^p_n/F^{p-1}_n$ in
a filtration $0\subseteq F^0_n\subseteq\cdots\subseteq F^n_n=H_n(X;G)$ of ...
\item $E^2_{p,q}\cong H_p(B;H_q(F;G))$.
\end{enumerate}
\end{thm}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%     This file is included from the file   Segmentation.tex
%
%     Section tag and label are placed in this top file.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Introduction}
\label{sec:HybridSegmentationIntroduction}
This section introduces the use of hybrid methods for segmentation of image
data. The hybrid segmentation approach integrates boundary-based and
region-based segmentation methods that amplify the strength but reduce the
weakness of both techniques. The advantage of this approach comes from
combining region-based segmentation methods like the fuzzy connectedness and
Voronoi diagram classification with boundary-based deformable model
segmentation. The synergy between fundamentally different methodologies tends
to result in robustness and higher segmentation quality.  A hybrid
segmentation engine can be built, as illustrated in
Figure~\ref{fig:ComponentsofaHybridSegmentationApproach}. It consists of
modules representing segmentation methods and implemented as ITK filters. We
can derive a variety of hybrid segmentation methods by exchanging the filter
used in each module. It should be noted that under the fuzzy connectedness
and deformable models modules, there are several different filters that can
be used as components. Below, we describe two examples of hybrid segmentation
methods, derived from the hybrid segmentation engine: integration of fuzzy
connectedness and Voronoi diagram classification (hybrid method 1), and
integration of Gibbs prior and deformable models (hybrid method 2).  Details
regarding the concepts behind these methods have been discussed in the
literature
\cite{Angelini2002,Udupa2002,Jin2002,Imielinska2001,Imielinska2000a,Imielinska2000b}

\subsection{Fuzzy Connectedness and Confidence Connectedness }

Probably the simplest combination of hybrid filters is the pair formed by the
\doxygen{itk}{ConfidenceConnectednessImageFilter} and
\doxygen{itk}{SimpleFuzzyConnectednessScalarImageFilter}. In this combination the
confidence connectedness filter is used to produce a rough segmentation of an
object and to compute and estimation of the mean and variance of
gray values in such structure. The values of mean and variance are then passed
to the Simple Fuzzy Connectedness image filter in order to compute an affinity map.

\ifitkFullVersion
\input{FuzzyConnectednessImageFilter.tex}
\fi

\subsection{Fuzzy Connectedness and Voronoi Classification}
\label{sec:HybridMethod1}
In this section we present a hybrid segmentation method that requires
minimal manual initialization by integrating the fuzzy connectedness and
Voronoi diagram classification segmentation algorithms. We start with a fuzzy
connectedness filter to generate a sample of tissue from a region to be
segmented. From the sample, we automatically derive image statistics that
constitute the homogeneity operator to be used in the next stage of the
method. The output of the fuzzy connectedness filter is used as a prior to
the Voronoi diagram classification filter. This filter performs iterative
subdivision and classification of the segmented image resulting in an
estimation of the boundary. The output of this filter is a 3D binary image
that can be used to display the 3D result of the segmentation, or passed to
another filter (e.g. deformable model) for further improvement of the final
segmentation. Details describing the concepts behind these methods have been
published in
\cite{Angelini2002,Udupa2002,Jin2002,Imielinska2001,Imielinska2000a,Imielinska2000b}

In Figure~\ref{fig:UMLClassDiagramoftherFuzzyConnectednessFilter}, we
describe the base class for simple fuzzy connectedness segmentation. This
method is non-scale based and non-iterative, and requires only one seed to
initialize it. We define affinity between two nearby elements in a image
(e.g. pixels, voxels) via a degree of adjacency, similarity in their
intensity values, and their similarity to the estimated object.  The closer
the elements and the more similar their intensities, the greater the affinity
between them. We compute the strength of a path and fuzzy connectedness
between each two pixels (voxels) in the segmented image from the fuzzy
affinity.  Computation of the fuzzy connectedness value of each pixel (voxel)
is implemented by selecting a seed point and using dynamic programming. The
result constitutes the fuzzy map. Thresholding of the fuzzy map gives a
segmented object that is strongly connected to the seed point (for more
details, see \cite{Udupa1996}). Two fuzzy connectedness filters are available
in the toolkit:

\begin{itemize}
\item The \doxygen{itk}{SimpleFuzzyConnectednessScalarImageFilter}, an implementation of the
fuzzy connectedness segmentation of single-channel (grayscale) image.
\item The \doxygen{itk}{SimpleFuzzyConnectednessRGBImageFilter}, an implementation
of fuzzy connectedness segmentation of a three-channel (RGB) image.
\end{itemize}

New classes can be derived from the base class by defining other affinity
functions and targeting multi-channel images with an arbitrary number of
channels. Note that the simple fuzzy connectedness filter can be used as a
stand-alone segmentation method and does not necessarily need to be combined
with other methods as indicated by
Figure~\ref{fig:UMLCollaborationDiagramoftheFuzzyConnectednessFilter}.

In Figure~\ref{fig:UMLVoronoiSegmentationClassFilter} we present the base
class for Voronoi diagram classification. We initialize the method with a
number of random seed points and compute the Voronoi diagram over the
segmented 2D image. Each Voronoi region in the subdivision is classified as
internal or external, based on the homogeneity operator derived from the
fuzzy connectedness algorithm.  We define boundary regions as the external
regions that are adjacent to the internal regions.  We further subdivide the
boundary regions by adding seed points to them. We converge to the final
segmentation using simple stopping criteria (for details, see
\cite{Imielinska2001}). Two Voronoi-based segmentation methods are available in ITK: the \doxygen{itk}{VoronoiSegmentationImageFilter} for processing single-channel
(grayscale) images, and the \doxygen{itk}{VoronoiSegmentationRGBImageFilter}, for
segmenting three-channel (RGB) images. New classes can be derived from the
base class by defining other homogeneity measurements and targeting
multichannel images with an arbitrary number of channels.  The other classes
that are used for computing a 2D Voronoi diagram are shown in
Figure~\ref{fig:UMLClassesforImplementationofVoronoiDiagramFilter}. Note that
the Voronoi diagram filter can be used as a stand-alone segmentation method,
as depicted in
Figure~\ref{fig:UMLCollaborationDiagramoftheVoronoiSegmentationFilter}.

Figures~\ref{fig:UMLHybridMethodDiagram1} and
\ref{fig:UMLHybridMethodDiagram2} illustrate hybrid segmentation methods
that integrate fuzzy connectedness with Voronoi diagrams, and fuzzy
connectedness, Voronoi diagrams and deformable models, respectively.

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{HybridSegmentationEngine1.eps}
\itkcaption[Hybrid Segmentation Engine]{The hybrid segmentation engine.}
\label{fig:ComponentsofaHybridSegmentationApproach}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{FuzzyConnectednessClassDiagram1.eps}
\itkcaption[FuzzyConectedness Filter Diagram]{Inheritance diagram for the fuzzy connectedness filter.}
\label{fig:UMLClassDiagramoftherFuzzyConnectednessFilter}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{FuzzyConnectednessCollaborationDiagram1.eps}
\itkcaption[Fuzzy Connectedness Segmentation Diagram]{Inputs and outputs to
FuzzyConnectednessImageFilter segmentation algorithm.}
\label{fig:UMLCollaborationDiagramoftheFuzzyConnectednessFilter}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{VoronoiSegmentationClassDiagram1.eps}
\itkcaption[Voronoi Filter class diagram]{Inheritance diagram for the Voronoi
segmentation filters.}
\label{fig:UMLVoronoiSegmentationClassFilter}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{VoronoiSegmentationCollaborationDiagram1.eps}
\itkcaption[Voronoi Diagram Filter classes]{Classes used by the Voronoi
segmentation filters.}
\label{fig:UMLClassesforImplementationofVoronoiDiagramFilter}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{VoronoiSegmentationCollaborationDiagram2.eps}
\itkcaption[Voronoi Diagram Segmentation]{Input and output to the
VoronoiSegmentationImageFilter.}
\label{fig:UMLCollaborationDiagramoftheVoronoiSegmentationFilter}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{FuzzyVoronoiCollaborationDiagram1.eps}
\itkcaption[Fuzzy Connectedness and Voronoi Diagram Classification]{Integration
 of the fuzzy connectedness and Voronoi segmentation filters.}
\label{fig:UMLHybridMethodDiagram1}
\end{figure}

\begin{figure}
\center
\includegraphics[width=0.8\textwidth]{FuzzyVoronoiDeformableCollaborationDiagram1.eps}
\itkcaption[Fuzzy Connectedness, Voronoi diagram, and Deformable
Models]{Integration of the fuzzy connectedness, Voronoi, and
deformable model segmentation methods.}
\label{fig:UMLHybridMethodDiagram2}
\end{figure}

\subsubsection{Example of a Hybrid Segmentation Method}
\label{sec:HybridMethod1:Example}

\ifitkFullVersion
\input{HybridSegmentationFuzzyVoronoi.tex}
\fi

%% \subsection{Deformable Models and Gibbs Prior}

%% Another combination that can be used in a hybrid segmentation method is the
%% set of Gibbs prior filters with deformable models.

%% \subsubsection{Deformable Model}
%% \ifitkFullVersion
%% \input{DeformableModel1.tex}
%% \fi

%% \subsubsection{Gibbs Prior Image Filter}
%% \ifitkFullVersion
%% \input{GibbsPriorImageFilter1.tex}
%% \fi


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{The First Steps}\label{FirstSteps}
In this chapter we give an introduction how to use \escript to solve
a partial differential equation\index{partial differential equation} (PDE\index{partial differential equation!PDE}).
We assume you are at least a little familiar with \PYTHON.
The knowledge presented in the \PYTHON tutorial at \url{https://docs.python.org/2/tutorial/} is more than sufficient.

The PDE\index{partial differential equation} we wish to solve is the Poisson equation\index{Poisson equation}
\begin{equation}
    -\Delta u=f
    \label{eq:FirstSteps.1}
\end{equation}
for the solution $u$. The function $f$ is the given right hand side. The domain of interest, denoted by $\Omega$,
is the unit square
\begin{equation}
\Omega=[0,1]^2=\{ (x_0;x_1) | 0\le x_{0} \le 1 \mbox{ and } 0\le x_{1} \le 1 \}
\label{eq:FirstSteps.1b}
\end{equation}
The domain is shown in \fig{fig:FirstSteps.1}.
\begin{figure}[ht]
    \centerline{\includegraphics{FirstStepDomain}}
    \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}
    \label{fig:FirstSteps.1}
\end{figure}

$\Delta$ denotes the Laplace operator\index{Laplace operator}, which is defined by
\begin{equation}
\Delta u = (u_{,0})_{,0}+(u_{,1})_{,1}
\label{eq:FirstSteps.1.1}
\end{equation}
where, for any function $u$ and any direction $i$, $u_{,i}$
denotes the partial derivative \index{partial derivative} of $u$ with respect
to $i$.\footnote{You may be more familiar with the Laplace
operator\index{Laplace operator} being written as $\nabla^2$, and written in
the form
\begin{equation*}
    \nabla^2 u = \nabla^t \cdot \nabla u =  \frac{\partial^2 u}{\partial x_0^2}
    + \frac{\partial^2 u}{\partial  x_1^2}
\end{equation*}
and \eqn{eq:FirstSteps.1} as
\begin{equation*}
    -\nabla^2 u = f
\end{equation*}
}
Basically, in the subindex of a function, any index to the right of the comma denotes a spatial derivative with respect
to the index. To get a more compact form we will write $u_{,ij}=(u_{,i})_{,j}$
which leads to
\begin{equation}
\Delta u = u_{,00}+u_{,11}=\sum_{i=0}^2 u_{,ii}
\label{eq:FirstSteps.1.1b}
\end{equation}
We often find that use
of nested $\sum$ symbols makes formulas cumbersome, and we use the more
compact Einstein summation convention\index{summation convention}. This
drops the $\sum$ sign and assumes that a summation is performed over any repeated index.
For instance we write
\begin{eqnarray}
x_{i}y_{i}=\sum_{i=0}^2 x_{i}y_{i}   \\
x_{i}u_{,i}=\sum_{i=0}^2 x_{i}u_{,i}   \\
u_{,ii}=\sum_{i=0}^2 u_{,ii} \\
x_{ij}u_{i,j}=\sum_{j=0}^2\sum_{i=0}^2 x_{ij}u_{i,j}   \\
\label{eq:FirstSteps.1.1c}
\end{eqnarray}
With the summation convention we can write the Poisson equation \index{Poisson equation} as
\begin{equation}
- u_{,ii} =1
\label{eq:FirstSteps.1.sum}
\end{equation}
where $f=1$ in this example.

On the boundary of the domain $\Omega$ the normal derivative $n_{i} u_{,i}$
of the solution $u$ shall be zero, i.e. $u$ shall fulfill
the homogeneous Neumann boundary condition\index{Neumann
boundary condition!homogeneous}
\begin{equation}
n_{i} u_{,i}= 0 \;.
\label{eq:FirstSteps.2}
\end{equation}
$n=(n_{i})$ denotes the outer normal field
of the domain, see \fig{fig:FirstSteps.1}. Remember that we
are applying the Einstein summation convention \index{summation convention}, i.e. $n_{i} u_{,i}= n_{0} u_{,0} +%
n_{1} u_{,1}$.\footnote{Some readers may familiar with the
notation $\frac{\partial u}{\partial n} = n_{i} u_{,i}$
for the normal derivative.}
The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the
set $\Gamma^N$ which is the top and right edge of the domain:
\begin{equation}
    \Gamma^N=\{(x_0;x_1) \in \Omega | x_{0}=1 \mbox{ or } x_{1}=1  \}
    \label{eq:FirstSteps.2b}
\end{equation}
On the bottom and the left edge of the domain which is defined
as
\begin{equation}
    \Gamma^D=\{(x_0;x_1) \in \Omega | x_{0}=0 \mbox{ or } x_{1}=0  \}
    \label{eq:FirstSteps.2c}
\end{equation}
the solution shall be identical to zero:
\begin{equation}
    u=0 \; .
    \label{eq:FirstSteps.2d}
\end{equation}
This kind of boundary condition is called a homogeneous Dirichlet boundary
condition\index{Dirichlet boundary condition!homogeneous}.
The partial differential equation in \eqn{eq:FirstSteps.1.sum} together
with the Neumann boundary condition \eqn{eq:FirstSteps.2} and
Dirichlet boundary condition in \eqn{eq:FirstSteps.2d} form a so-called
boundary value
problem\index{boundary value problem} (BVP\index{boundary value problem!BVP})
for the unknown function~$u$.

\begin{figure}[ht]
    \centerline{\includegraphics{FirstStepMesh}}
    \caption{Mesh of $4 \times 4$ elements on a rectangular domain. Here
    each element is a quadrilateral and described by four nodes, namely
    the corner points. The solution is interpolated by a bi-linear
    polynomial.}
    \label{fig:FirstSteps.2}
\end{figure}

In general the BVP\index{boundary value problem!BVP} cannot be solved
analytically and numerical methods have to be used to construct an
approximation of the solution $u$.
Here we will use the finite element method\index{finite element method}
(FEM\index{finite element method!FEM}).
The basic idea is to fill the domain with a set of points called nodes.
The solution is approximated by its values on the nodes\index{finite element method!nodes}.
Moreover, the domain is subdivided into smaller sub-domains called
elements\index{finite element method!element}.
On each element the solution is represented by a polynomial of a certain
degree through its values at the nodes located in the element.
The nodes and their connection through elements is called a
mesh\index{finite element method!mesh}. \fig{fig:FirstSteps.2} shows an
example of a FEM mesh with four elements in the $x_0$ and four elements
in the $x_1$ direction over the unit square.
For more details we refer the reader to the literature, for instance \Ref{Zienc,NumHand}.

The \escript solver we want to use to solve this problem is embedded into the \PYTHON interpreter language.
So you can solve the problem interactively but you will learn quickly that it
is more efficient to use scripts which you can edit with your favorite editor.
To enter the escript environment, use the \program{run-escript}
command\footnote{\program{run-escript} is not available under Windows.
If you run under Windows you can just use the \program{python} command and the
\env{OMP_NUM_THREADS} environment variable to control the number of threads.}:
\begin{verbatim}
run-escript
\end{verbatim}
which will pass you on to the \PYTHON prompt
\begin{verbatim}
Python 2.7.6 (default, Mar 22 2014, 15:40:47)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
\end{verbatim}
Here you can use all available \PYTHON commands and language features\footnote{Throughout our examples, we use the python 3 form of
print. That is, print(1) instead of print 1.}, for instance
\begin{python}
  >>> x=2+3
  >>> print("2+3=",x)
  2+3= 5
\end{python}
We refer to the \PYTHON user's guide if you are not familiar with \PYTHON.

\escript provides the class \Poisson to define a Poisson equation\index{Poisson equation}.
(We will discuss a more general form of a PDE\index{partial differential equation!PDE}
that can be defined through the \LinearPDE class later.)
The instantiation of a \Poisson class object requires the specification of the domain $\Omega$.
In \escript the \Domain class objects are used to describe the geometry of a
domain but it also contains information about the discretization methods and
the actual solver which is used to solve the PDE.
Here we are using the FEM\index{finite element method} library \finley.
The following statements create the \Domain object \var{mydomain} from the
\finley function \method{Rectangle}:
\begin{python}
  from esys.finley import Rectangle
  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
\end{python}
In this case the domain is a rectangle with the lower left corner at point $(0,0)$
and the upper right corner at $(\var{l0},\var{l1})=(1,1)$.
The arguments \var{n0} and \var{n1} define the number of elements in $x_{0}$ and
$x_{1}$-direction respectively. For more details on \method{Rectangle} and
other \Domain generators see \Chap{chap:finley}, \Chap{chap:ripley}, and
\Chap{chap:speckley}.

The following statements define the \Poisson class object \var{mypde} with domain \var{mydomain} and
the right hand side $f$ of the PDE to constant $1$:
\begin{python}
  from esys.escript.linearPDEs import Poisson
  mypde = Poisson(mydomain)
  mypde.setValue(f=1)
\end{python}
We have not specified any boundary condition but the \Poisson class implicitly
assumes homogeneous Neuman boundary conditions\index{Neumann boundary condition!homogeneous} defined by \eqn{eq:FirstSteps.2}.
With this boundary condition the BVP\index{boundary value problem!BVP} we have
defined has no unique solution.
In fact, with any solution $u$ and any constant $C$ the function $u+C$ becomes
a solution as well.
We have to add a Dirichlet boundary condition\index{Dirichlet boundary condition}.
This is done by defining a characteristic function\index{characteristic function}
which has positive values at locations $x=(x_{0},x_{1})$
where Dirichlet boundary condition is set and $0$ elsewhere.
In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c}, we need to
construct a function \var{gammaD} which is positive for the cases $x_{0}=0$ or $x_{1}=0$.
To get an object \var{x} which contains the coordinates of the nodes in the domain use
\begin{python}
  x=mydomain.getX()
\end{python}
The method \method{getX} of the \Domain \var{mydomain} gives access to locations
in the domain defined by \var{mydomain}.
The object \var{x} is actually a \Data object which will be discussed in
\Chap{ESCRIPT CHAP} in more detail.
What we need to know here is that \var{x} has \Rank (number of dimensions) and
a \Shape (list of dimensions) which can be viewed by calling the \method{getRank} and \method{getShape} methods:
\begin{python}
  print("rank ",x.getRank(),", shape ",x.getShape())
\end{python}
This will print something like
\begin{python}
  rank 1, shape (2,)
\end{python}
The \Data object also maintains type information which is represented by the
\FunctionSpace of the object. For instance
\begin{python}
  print(x.getFunctionSpace())
\end{python}
will print
\begin{python}
  Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
\end{python}
which tells us that the coordinates are stored on the nodes of (rather than on
points in the interior of) a Finley mesh.
To get the  $x_{0}$ coordinates of the locations we use the statement
\begin{python}
  x0=x[0]
\end{python}
Object \var{x0} is again a \Data object now with \Rank $0$ and \Shape $()$.
It inherits the \FunctionSpace from \var{x}:
\begin{python}
  print(x0.getRank(), x0.getShape(), x0.getFunctionSpace())
\end{python}
will print
\begin{python}
  0 () Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
\end{python}
We can now construct a function \var{gammaD} which is only non-zero on the
bottom and left edges of the domain with
\begin{python}
  from esys.escript import whereZero
  gammaD=whereZero(x[0])+whereZero(x[1])
\end{python}

\code{whereZero(x[0])} creates a function which equals $1$ where \code{x[0]} is (almost) equal to zero and $0$ elsewhere.
Similarly, \code{whereZero(x[1])} creates a function which equals $1$ where \code{x[1]} is equal to zero and $0$ elsewhere.
The sum of the results of \code{whereZero(x[0])} and \code{whereZero(x[1])}
gives a function on the domain \var{mydomain} which is strictly positive where $x_{0}$ or $x_{1}$ is equal to zero.
Note that \var{gammaD} has the same \Rank, \Shape and \FunctionSpace like \var{x0} used to define it.
So from
\begin{python}
  print(gammaD.getRank(), gammaD.getShape(), gammaD.getFunctionSpace())
\end{python}
one gets
\begin{python}
  0 () Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
\end{python}
An additional parameter \var{q} of the \code{setValue} method of the \Poisson
class defines the characteristic function\index{characteristic function} of
the locations of the domain where the homogeneous Dirichlet boundary condition\index{Dirichlet boundary condition!homogeneous} is set.
The complete definition of our example is now:
\begin{python}
  from esys.escript.linearPDEs import Poisson
  x = mydomain.getX()
  gammaD = whereZero(x[0])+whereZero(x[1])
  mypde = Poisson(domain=mydomain)
  mypde.setValue(f=1,q=gammaD)
\end{python}
The first statement imports the \Poisson class definition from the \linearPDEs module.
To get the solution of the Poisson equation defined by \var{mypde} we just have to call its \method{getSolution} method.

Now we can write the script to solve our Poisson problem
\begin{python}
  from esys.escript import *
  from esys.escript.linearPDEs import Poisson
  from esys.finley import Rectangle
  # generate domain:
  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
  # define characteristic function of Gamma^D
  x = mydomain.getX()
  gammaD = whereZero(x[0])+whereZero(x[1])
  # define PDE and get its solution u
  mypde = Poisson(domain=mydomain)
  mypde.setValue(f=1, q=gammaD)
  u = mypde.getSolution()
\end{python}
The question is what we do with the calculated solution \var{u}.
Besides postprocessing, e.g. calculating the gradient or the average value,
which will be discussed later, plotting the solution is one of the things you
might want to do.
\escript offers two ways to do this, both based on external modules or packages.
The first option is using the \MATPLOTLIB module which allows plotting 2D
results relatively quickly from within the \PYTHON script, see~\cite{matplotlib}.
However, there are limitations when using this tool, especially for large
problems and when solving three-dimensional problems.
Therefore, \escript provides functionality to export data as files which can
subsequently be read by third-party software packages such as
\mayavi\cite{mayavi} or \VisIt~\cite{VisIt}.

\subsection{Plotting Using \MATPLOTLIB}
The \MATPLOTLIB module provides a simple and easy-to-use way to visualize PDE
solutions (or other \Data objects).
To hand over data from \escript to \MATPLOTLIB the values need to be mapped onto
a rectangular grid. We will make use of the \numpy module for this.

First we need to create a rectangular grid which is accomplished by the following statements:
\begin{python}
  import numpy
  x_grid = numpy.linspace(0., 1., 50)
  y_grid = numpy.linspace(0., 1., 50)
\end{python}
\var{x_grid} is an array defining the x coordinates of the grid while
\var{y_grid} defines the y coordinates of the grid.
In this case we use $50$ points over the interval $[0,1]$ in both directions.

Now the values created by \escript need to be interpolated to this grid.
We will use the \MATPLOTLIB \function{mlab.griddata} function to do this.
Spatial coordinates are easily extracted as a \var{list} by
\begin{python}
  x=mydomain.getX()[0].toListOfTuples()
  y=mydomain.getX()[1].toListOfTuples()
\end{python}
In principle we can apply the same \member{toListOfTuples} method to extract the values from the PDE solution \var{u}.
However, we have to make sure that the \Data object we extract the values from
uses the same \FunctionSpace as we have used when extracting \var{x} and \var{y}.
We apply the \function{interpolation} to \var{u} before extraction to achieve this:
\begin{python}
  z=interpolate(u, mydomain.getX().getFunctionSpace())
\end{python}
The values in \var{z} are the values at the points with the coordinates given by \var{x} and \var{y}.
These values are interpolated to the grid defined by \var{x_grid} and \var{y_grid} by using
\begin{python}
  import matplotlib
  z_grid = matplotlib.mlab.griddata(x, y, z, xi=x_grid, yi=y_grid)
\end{python}
Now \var{z_grid} gives the values of the PDE solution \var{u} at the grid which can be plotted using \function{contourf}:
\begin{python}
  matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
  matplotlib.pyplot.savefig("u.png")
\end{python}
Here we use 5 contours. The last statement writes the plot to the file \file{u.png} in the PNG format.
Alternatively, one can use
\begin{python}
  matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
  matplotlib.pyplot.show()
\end{python}
which gives an interactive browser window.

\begin{figure}
\centerline{\includegraphics[width=\figwidth]{FirstStepResultMATPLOTLIB}}
\caption{Visualization of the Poisson Equation Solution for $f=1$ using \MATPLOTLIB}
\label{fig:FirstSteps.3b}
\end{figure}

Now we can write the script to solve our Poisson problem
\begin{python}
  from esys.escript import *
  from esys.escript.linearPDEs import Poisson
  from esys.finley import Rectangle
  import numpy
  import matplotlib
  import pylab
  # generate domain:
  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
  # define characteristic function of Gamma^D
  x = mydomain.getX()
  gammaD = whereZero(x[0])+whereZero(x[1])
  # define PDE and get its solution u
  mypde = Poisson(domain=mydomain)
  mypde.setValue(f=1,q=gammaD)
  u = mypde.getSolution()
  # interpolate u to a matplotlib grid:
  x_grid = numpy.linspace(0.,1.,50)
  y_grid = numpy.linspace(0.,1.,50)
  x=mydomain.getX()[0].toListOfTuples()
  y=mydomain.getX()[1].toListOfTuples()
  z=interpolate(u,mydomain.getX().getFunctionSpace()).toListOfTuples()
  z_grid = matplotlib.mlab.griddata(x,y,z,xi=x_grid,yi=y_grid )
  # interpolate u to a rectangular grid:
  matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
  matplotlib.pyplot.savefig("u.png")
\end{python}
The entire code is available as \file{poisson_matplotlib.py} in the \ExampleDirectory.
You can run the script using the {\it escript} environment
\begin{verbatim}
run-escript poisson_matplotlib.py
\end{verbatim}
This will create a file called \file{u.png}, see \fig{fig:FirstSteps.3b}.
For details on the usage of the \MATPLOTLIB module we refer to the documentation~\cite{matplotlib}.

As pointed out, \MATPLOTLIB is restricted to the two-dimensional case and
should be used for small problems only.
It can not be used under \MPI as the \member{toListOfTuples} method is not
safe under \MPI\footnote{The phrase 'safe under \MPI' means that a program
will produce correct results when run on more than one processor under \MPI.}.

\begin{figure}
\centerline{\includegraphics[width=\figwidth]{FirstStepResult}}
\caption{Visualization of the Poisson Equation Solution for $f=1$}
\label{fig:FirstSteps.3}
\end{figure}

\subsection{Visualization using export files}

As an alternative to \MATPLOTLIB, {\it escript} supports exporting data to
\VTK and \SILO files which can be read by visualization tools such as
\mayavi\cite{mayavi} and \VisIt~\cite{VisIt}. This method is \MPI safe and
works with large 2D and 3D problems.

To write the solution \var{u} of the Poisson problem in the \VTK file format
to the file \file{u.vtu} one needs to add:
\begin{python}
  from esys.weipa import saveVTK
  saveVTK("u.vtu", sol=u)
\end{python}
This file can then be opened in a \VTK compatible visualization tool where the
solution is accessible by the name {\it sol}. Similarly,
\begin{python}
  from esys.weipa import saveSilo
  saveSilo("u.silo", sol=u)
\end{python}
will write \var{u} to a \SILO file if escript was compiled with support for
LLNL's \SILO library.

The Poisson problem script is now
\begin{python}
  from esys.escript import *
  from esys.escript.linearPDEs import Poisson
  from esys.finley import Rectangle
  from esys.weipa import saveVTK
  # generate domain:
  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
  # define characteristic function of Gamma^D
  x = mydomain.getX()
  gammaD = whereZero(x[0])+whereZero(x[1])
  # define PDE and get its solution u
  mypde = Poisson(domain=mydomain)
  mypde.setValue(f=1,q=gammaD)
  u = mypde.getSolution()
  # write u to an external file
  saveVTK("u.vtu",sol=u)
\end{python}
The entire code is available as \file{poisson_vtk.py} in the \ExampleDirectory.

You can run the script using the {\it escript} environment and visualize the
solution using \mayavi:
\begin{verbatim}
run-escript poisson_vtk.py
mayavi2 -d u.vtu -m Surface
\end{verbatim}
The result is shown in \fig{fig:FirstSteps.3}.


\section{Introduction}

We consider the problem of performing SQL-style join operations on tables that are secret shared among three parties, in the presence of an honest majority. Our proposed protocol takes two or more arbitrarily secret shared database tables and constructs another secret shared table containing a \texttt{join} of the two tables, without revealing \emph{any} information beyond the secret shares themselves. Our protocol is constant-round and  has computation and communication overhead that is linear in the size of the tables. Simulation-based security is achieved in the semi-honest setting with an honest-majority. Our protocol can perform inner, left and full joins along with union and arbitrary computation on the resulting table, with the best performance and security guarantees when joins operate on unique primary keys.

New techniques \cite{usenix:PSZ14,USENIX:PSSZ15,PSZ16,CCS:KKRT16,PSWW18,CLR17,CHLR18,cryptoeprint:2017:738,RA17,KLSAP17,OOS17,KMPRT17} for performing set intersection, inner join and related functionalities have shown great promise for practical deployment.
The vast majority of these works perform {\em private set intersection} (PSI), which is analogous to revealing the entire result of an inner join.
Computing a join {\em without revealing it} (\ie, performing further joins, filtering, or computing only aggregate information) is significantly harder, and optimizations for such a setting do not automatically translate to our composable setting.
We highlight a few notable results that hide the contents of a join (revealing only some function of it):

Ion et al.\ recently deployed a private set intersection sum protocol\cite{cryptoeprint:2017:738} to allow customers of Google Adwords to correlate online advertising with offline sales, while preserving user privacy. Pinkas et al. \cite{PSWW18} also introduced a practical protocol that can compute any (symmetric) function of the intersection and associated data. These protocols can be framed in terms of SQL queries consisting of an inner join followed by an aggregation on the resulting table, e.g. summing a column.
Neither of these protocols (and almost no prior related results) support secret-shared inputs, but rather require the source tables to be held in the clear by each party.

The majority of these protocols consider the two party setting and are based on various cryptographic primitives, e.g. exponentiation~\cite{cryptoeprint:2017:738}, oblivious transfer\cite{PSWW18}, or fully homomorphic encryption\cite{CLR17}. However, in this work we alter the security model to consider three parties with an honest majority. The motivation is that typical protocols in this setting (e.g.\cite{highthroughput}) require less computation and communication than similar two party protocols, by a factor of at least the security parameter $\kappa=128$. Moreover, we will see that the honest majority enables various algorithms which are orders of magnitude more efficient, e.g. oblivious permutations require $O(n)$ work instead of $O(n\log n \kappa)$ \cite{MS13}.

Given this observation we investigate how to leverage the efficiency gains in the three party setting to construct practical protocols for performing set intersection and other SQL-like operations where both the inputs and outputs are secret shared. One critical aspect of this input/output requirement is that join operations can then be \emph{composed} together, where the output of a join can be the input to another. Allowing this composability greatly increases the ability to perform highly complex queries and enables external parties to contribute data simply by secret sharing it between the primary parties which participate in the protocol.

The fact that we support secret-shared inputs also leads to \emph{outsourced secure computation.} Three non-coluding servers can be established, and inputs can be provided by the servers themselves or by external parties simply by secret sharing the input among the servers. This model has been gaining traction in industry. Mozilla recently deployed a service to collect telemetry data about Firefox\cite{mozilla} using two non-colluding servers running the Prio protocol\cite{DBLP:conf/nsdi/Corrigan-GibbsB17}. Other examples include privacy preserving machine learning frameworks which support secret shared inputs such as Facebook's Crypten built for PyTorch\cite{Crypten} and Cape Privacy's FTEncrypted built for TensorFlow\cite{TFEncrypted}.

Complex joins on secret-shared inputs/outputs are valuable in all of these examples.
Most privacy preserving machine learning publications\cite{aby3,Crypten,TFEncrypted,SecureNN,XONN} assume that the data being trained on has already been joined together. However, without a framework like ours it is unclear how this would be accomplished while preserving privacy and efficiency. The generality of our approach allows us to solve these problems and many others, e.g. the two applications presented in \sectionref{sec:app}.

\subsection{Functionality}

Our protocol offers a wide variety of functionality including set intersection, set union, set difference and a variety of SQL-like joins with complex boolean queries. Generally speaking, our protocol works on tables of secret shared data which are functionally similar to SQL tables. This is in contrast to traditional PSI and PSU protocols\cite{usenix:PSZ14,USENIX:PSSZ15,PSZ16,CCS:KKRT16} in that each record is now a tuple of values as opposed to a single key.

We define our database tables in the natural way. Each table can be viewed as a collection of rows or as a vector of columns. For a table $X$, we denote the $i$th row as $X[i]$ and the $j$th column as $X_j$.
\iffullversion
Note that each column of a table has the same length but can contain different data types, e.g. $X_1$ is a column of 32 bit fixed point decimal values and $X_2$ is a column of 1024 bit strings.

\fi
Our core protocol requires each table to contain unique values in the column defining the join  (i.e., we can only join on ``unique primary keys''). For example, if we consider the following SQL styled join/intersection query
\vspace{-0.1cm}
$$
\texttt{select } X_2 \texttt{ from } X \texttt{ inner join } Y \texttt{ on } X_1 = Y_1
\vspace{-0.1cm}
$$
then the join-keys are $X_1$ and $Y_1$. This uniqueness condition can be extended to the setting where multiple columns are being compared for equality.
\iffullversion
In this case the tuple of these columns must be unique.
\fi
Later on we will discuss the case when such a uniqueness property does not hold. Our protocols also support a \texttt{where} clause that filters the selection using an arbitrary predicate of the $X$ and $Y$ rows. Furthermore, the \texttt{select} clause can also return a function of the two rows. For example,
\vspace{-0.1cm}
\begin{align*}
\texttt{select }&  X_1,max(X_2, Y_2)  \texttt{ from } X \texttt{ inner join } Y \\
\texttt{on }& X_1 = Y_1 \texttt{ where } Y_2 > 23.3
\vspace{-0.1cm}
\end{align*}
In general, the supported join operations can be characterized in three parts: 1) The select function $S(\cdot)$ that defines how the rows of $X,Y$ are used to construct each output row, e.g. $S(X,Y)=(X_1, max(X_2,Y_2))$
\iffullversion
in the example above
\fi. 2) The predicate $P(\cdot)$ that defines the \texttt{where} clause, and 3) which columns are being joined on.
\iffullversion
 We require that both $S$ and $P$ must be expressible in the framework of \cite{aby3} which we provide a custom implementation of.
\fi

\iffullversion
So far we have discussed inner join (intersection) between two tables.
\fi
Several other types of joins are also supported including left and right joins, set union and set minus (difference) and full joins. A left join takes the inner join and includes all of the missing records from the left table. For the records solely from the left table, the resulting table contains \texttt{NULL} for the columns from the right table. Right join is defined symmetrically. A full join is a natural extension where all the missing rows from $X$ and $Y$ are added to the output table.

We define the union of two tables to contain all records from the left table, along with all the records from the right table which are not in the intersection with respect to the join-keys. Note that this definition is not strictly symmetric with respect to the left and right tables due to rows in the intersection coming from the left table. %\footnote{We define union in this way to resolve the ambiguity around which table the records in the intersection should come from. For example, consider two tables each with one row $X[0]=(k, x_2,x_3,...), Y[0]=(k, y_2,y_3,...)$. The union of $X,Y$ with $X_1,Y_1$ being the join-keys should only contain one row $(k,...)$ but which values $x_2,x_3,...$ or $y_2,y_3,...$ that should be in the output table is ambiguous. We therefore define it to be $(k,x_2,x_3,...)$. A full join allows both sets of values to be in the output.}.
Table minus is similarly defined as all of the left table whose join-column value is not present in the right table.

Beyond these various join operations, our framework supports two broad classes of operations which are a function of a single table. The first is a general SQL select statement which can perform computation on each row (e.g. compute the max of two columns) and filter the results using a \texttt{where} clause predicate. The second class is referred to as an aggregation which performs an operation across all of the rows of a table --- for example, computing the sum, counts, or the max of a given column.
\subsection{Our Results}

We present the first practical secure multi-party computation protocol for performing SQL-style database joins with linear overhead and constant rounds. Our protocol is fully composable in that the input and output tables are generically secret shared between the parties.
\iffullversion
	No partial information is revealed at any point.
\fi
	We achieve this result by combining various techniques from private set intersection and secure computation more
\iffullversion
	broadly while requiring the joined on keys be unique.
\else
	broadly.
\fi
 We build on the the binary secret sharing technique of \cite{highthroughput} with enhancements described by \cite{aby3}. We then combine this secret sharing scheme with cuckoo hashing\cite{usenix:PSZ14}, an MPC friendly PRF\cite{lowmc} and a custom protocol for evaluating an oblivious switching network\cite{MS13}. Using these building blocks our protocol is capable of computing the intersection of two tables of $n=2^{20}$ rows in 4.9 seconds.
\iffullversion
	Alternatively, the cardinality of the intersection can be computed in just 3.1 seconds.
\fi
  Beyond these two specific functionalities, our protocol allows arbitrary computation applied to a shared table. Compared to existing three party protocols with similar functionality (composable), our implementation is roughly $1000\times$ faster. When compared with \emph{non-composable} two party protocol, we observe a larger difference ranging from our protocol being $1.25\times$ slower to $4000\times$ faster depending on the functionality.

Building on our proposed protocol we demonstrate its utility by showcasing two potential applications. The first prototype would involve running our protocol between and within the states of the United States to validate the accuracy of the voter registration data in a privacy preserving way. The Pew Charitable Trust\cite{pew} reported 1 in 8 voter registration records in the United States contains a serious error while 1 in 4 eligible citizens remain unregistered. Our privacy preserving protocol identifies when an individual's address is out of date or more seriously if someone is registered to vote in more than one state which could allow them to cast two votes.
\iffullversion
Additionally, our protocol can help register eligible citizens. Our protocol ensures that only the minimum amount of information is revealed, namely the identities of individuals with serious registration errors.
\fi
Due to how the data is distributed between different governmental agencies, it will be critical that our protocol allows for composable operations. We implement this application and demonstrate that it is practical to run at a national scale (250M records) and low cost.

The second application that we consider allows multiple organizations to compare computer security incidents and logs to more accurately identify unwanted activities, e.g. a bot net. Several companies already offer this service including Facebook's ThreatExchange\cite{threat} and an open source alternative\cite{alt_threat}. One of the primary limitations of these existing solutions is the requirement that each organization send their security logs to a central party, e.g. Facebook. We propose using our protocol to distribute the trust of this central party between three parties such that privacy is guaranteed so long as there is an honest majority.

\input{related}


\chapter{Conclusions} \label{Conclusion}
The detection of outliers in indicates something scientifically interesting or significant. There are various algorithms for outlier detection in datasets. Two of them are Local Outlier Factor and One Class Support Vector Machine. Both algorithms have been used to detect outliers in a credit card fraud dataset. These algorithms require labeled normal data, which is essential in semi-supervised learning algorithms. The algorithms assigned a binary label to the observations, classifying themes either normal or fraudulent. Local Outlier Factor and One Class Support Vector Machine have different ways to identify outliers. They use \em{density} and \em{distance} of the observations respectively.

This research proposes a comparison of these two algorithms for the outlier detection process. Both algorithms reach different percentages of performance, and the One Class Support Vector Machine performs better than Local Outlier Factor in the given dataset. Comparison of these algorithms has been done by using the results of a confusion matrix, precisely, the True Positive Rate and False Positive Rate. The algorithm that has a higher True Positive Rate and a False Positive Rate is the better one.

To test the algorithm, a range of parameter values have been used. The Local Outlier Factor True Positive Rate is around 90$\%$, while the False Positive Rate changes from 88.7$\%$ to 79.7$\%$ and the Area Under the Curve is from 52.2$\%$ to 44.7$\%$. The One Class Support Vector Machine algorithm shows different behavior, with True Positive Rates between 98.8$\%$ and 89.4$\%$, and False Positive Rates is between 21.6$\%$ and, while its 10.4$\%$ and the Area Under the Curve is from 93$\%$ to 94$\%$. To conclude, the algorithms have shown different True Positive Rates, False Positive Rates and areas of Receiver Operator Characteristic. One Class Support Vector Machine displays a better performance on detecting outliers.

The current results are obtained by applying the Local Outlier Factor algorithm and One Class Support Vector Machine in some transactions that selected randomly from the dataset. Therefore the behavior of the algorithms may change with different transactions and parameters. Further work could be done by applying the algorithms on the whole dataset which will give more precise results. Applying these algorithms in different datasets to see if the results convergence or divergence will give a better understanding of algorithms behavior. Adding more Machine Learning outlier detection algorithms will help to know which one is better to apply in Big Data.


\chapter{Implementation in Coq}
\label{chap:implementation}

\lstset{style=coq}

We outline some pearls of the implementation of the framework of this thesis in the theorem prover Coq.  We do not assume additional axioms.  The
implementation compiles with the Coq versions 8.7 and 8.8.  We use proof scripts to derive proof terms, using Coq's standard tactic language
\textit{ltac}.  The complete the source code can be downloaded from the homepage of this thesis:
\begin{center}
  \url\homepage
\end{center}

\section*{Used Libraries}
\label{sec:coq-libraries}

We make use of the \textit{typeclass} mechanism built into Coq.  Typeclasses are first-citizen objects in Coq, that may be parametrised over types and
other typeclasses.  Coq uses proof-search to infer instances of typeclasses.  For that, the user has to declare definitions or lemmas as typeclass
instances.  We refer to Cast{\'e}ran and Sozeau~\cite{casteran2012gentle}, for a more thorough description of this feature.

\paragraph{Base Library and Finite Types}

We use a modified version of the library for the lecture \textit{Introduction to Computational Logic} at Saarland University.  This library extends
the standard library of Coq with additional functions and automation for lists and decidable predicates.  On top of this library, we use the library
of finite types that was developed by J.~Menz in his bachelor's thesis \cite{JanMenz}.  The following listing outlines the definition of decidable
predicates and finite types:

\begin{lstlisting}
Definition dec (P:Prop) := {P} + {~ P}.
Definition eq_dec (X:Type) := forall x l, dec (x=l).
Structure eqType := EqType {
  eqtype :> Type;
  decide_eq : eq_dec eqtype
}.
Canonical Structure eqType_CS X (A: eq_dec X) := EqType X.

Class finTypeC (type: eqType) : Type := FinTypeC {
  enum : list type;
  enum_ok : forall x : type, count enum x = 1
}.
Structure finType : Type := FinType {
  type :> eqType;
  class : finTypeC type
}.
Canonical Structure finType_CS (X:Type) {p : eq_dec X} {class : finTypeC (EqType X)} : finType := FinType (EqType X).
\end{lstlisting}%

The coercions (\lstinline!:>!) make it possible to use finite types \lstinline{L:finType} as types, i.e.\ we can write \lstinline!x:L!.  The canonical
structures enable automatic inference of the structure, for example there is a function \lstinline!index : forall L:finType, L -> nat!, and we can
write \lstinline!index true!.

\paragraph{Retraction Library}

We implemented a library for retractions.  We use a typeclass for the existence of retractions between two types.
\begin{lstlisting}
Section Retract.
  Variable X Y : Type.
  Definition retract (f : X -> Y) (g : Y -> option X) :=
    forall x l, g l = Some x <-> l = f x.
  Class Retract := {
    Retr_f : X -> Y;
    Retr_g : Y -> option X;
    Retr_retr : retract Retr_f Retr_g;
  }.
End Retract.
Arguments Retr_f { _ _ _ }.
Arguments Retr_g { _ _ _ }.
\end{lstlisting}
The \textit{Vernacular} command \lstinline!Arguments! makes $X$, $Y$, and the instance contextual implicit.  This means that
\lstinline!Retr_f : X -> Y!, where \lstinline!X! and \lstinline!Y! are inferred from the context.  We define instances for the retractions in
Section~\ref{sec:retracts}.

The retraction composition operator is not defined as an instance, to avoid diverging proof search during the typeclass inference, because it can be
applied arbitrary many times.  Although it is not declared as instance, the retraction composition function \lstinline!ComposeRetract! can be manually
applied:
\begin{lstlisting}
ComposeRetract : forall A B C : Type, Retract B C -> Retract A B -> Retract A C
\end{lstlisting}
We embed composition of retractions into retraction operators.  For example, instead of defining a retraction $RetrLft : X \hookrightarrow X+Y$, we
define (and declare as an instance) an operator on retractions:
\begin{lstlisting}
Retract_inl : forall A B C : Type, Retract A B -> Retract A (B + C)
\end{lstlisting}

\paragraph{Inhabited Types}
We also have a library for inhabited types.  Whenever we need a semantically irrelevant value, we can just write \lstinline!default! and Coq infers
and inserts a value with the following typeclass:
\begin{lstlisting}
Class inhabitedC (X : Type) := {
  default : X;
}.
\end{lstlisting}

\paragraph{Vectors and $\Fin_k$}

We use the type constructors \lstinline!Vector.t! and \lstinline!Fin.t! from Coq's standard library.  However, many basic lemmas and functions are
missing for this type.  We have a small library that adds the missing functions and lemmas.  It also provides some tactics, for example to make case
distinctions over \lstinline!Fin.t (S n)!.  We also introduce notations for elements of $\Fin_k$.

\paragraph{smpl}

We use Sigurd~Schneider's smpl plugin\footnote{\url{https://github.com/sigurdschneider/smpl}}.  It lets the user add tactics to a database (similar to
\lstinline!HintDb!) and provides a tactic that applies the first applicable tactic from the database.  We use this plugin for proof automation, more
on that below.

\section*{Mechanisation of encodable types}
\label{sec:coq-values}

The Coq implementation of the definition of multi-tape Turing machines is straight-forward.  Therefore, we omit code-listings here.  The interested
reader of the PDF version of this thesis may click on the definitions or lemmas, to get to the corresponding Coq code.

We also use typeclasses to implement the notion of encodable types:\footnote{Note that for technical reasons we do not parametrise \lstinline!codable!
  over \lstinline!sig:finType!.}

\begin{lstlisting}
Class codable (sig: Type) (X: Type) := {
  encode : X -> list sig;
}.
Arguments encode {sig} {X} {_}.
Coercion encode : codable >-> Funclass.
\end{lstlisting}

When \lstinline!cX : codable sig X!, the above \lstinline!Coercion! vernacular makes it possible to write \lstinline!cX x!.  This applies the concrete
encoding function given by the instance \lstinline!cX!.  We prefer this notation over \lstinline!encode x!, due to the fact that encodability is
ambiguous, as noted in Section~\ref{sec:value-containment}.  We define the alphabets in Definition~\ref{lem:retracts-basic} inductively, and also
declare their proofs of finiteness.

We realise the type of the extended alphabet $\Sigma^+ := boundary + \Sigma$, where
\[ boundary ::= \MS{START} ~|~ \MS{STOP} ~|~ \MS{UNKNOWN} \]%
is defined as an inductive type.

\section*{Automation}
\label{sec:coq-automation}

Our proof scripts make use of the feature of \textit{existential variables}.  An existential variable $?X$ is a type (that may contain variables
referring to more existential variables) and the environment in which it should be typed.  Existential variables are refined during unification (e.g.\
using the tactic ``\lstinline!apply!'') and created with tactics like ``\lstinline!eapply!''.  Before the proof script can be finished and saved with
the \lstinline!Qed! command, all existential variables must be bound to values.

Our tactic \lstinline!TMSimp! destructs conjunctive assumptions in all hypotheses (i.e.\ logical conjunctions and logical existentials).  It also
introduces and names tapes to \lstinline!tmid!, \lstinline!tmid0!, etc.  Furthermore, it instantiates and rewrites with hypotheses of the form
$H: \forall j.~ j \notin I \rightarrow tmid0[j] = tmid[j]$, that come from the correctness Lemma~\ref{lem:LiftTapes_Realise} of the tapes-lift
operator.

The tactic \lstinline!modpon H! instantiates assumptions of the form $H: \forall x~\cdots.~P \rightarrow \cdots \rightarrow Q$.  For each premise that
the tactic could not solve automatically, it creates a new subgoal with existential variables for the quantified variables.

The tactic \lstinline!TM_Correct! instantiates the existential variable for the relation $?R'$ and proves $M \Realise ?R'$.  For that, it applies all
correctness lemmas of the primitive machines, control-flow operators, lifts, constructors and deconstructors, and some other auxiliary machines.  The
user can declare more correctness and running time lemmas to be used by \lstinline!TM_Correct!.  For that, we use the Coq plugin \textit{smpl}.  For
example, the following code declares the correctness and running time lemmas of \lstinline!LiftTapes!:

\begin{lstlisting}
Ltac smpl_TM_LiftN :=
  lazymatch goal with
  | [ |- LiftTapes _ _ $\Realise$ _] =>
    apply LiftTapes_Realise; [ smpl_dupfree | ]
  | [ |- LiftTapes _ _ $\Realise$c(_) _] =>
    apply LiftTapes_RealiseIn; [ smpl_dupfree | ]
  | [ |- projT1 (LiftTapes _ _) $\TerminatesIn$ _] =>
    apply LiftTapes_Terminates; [ smpl_dupfree | ]
  end.
Smpl Add smpl_TM_LiftN : TM_Correct.
\end{lstlisting}
The tactic \lstinline!smpl_dupfree! automatically proves that a vector is duplicate-free.

Note that we do not register correctness lemmas to \lstinline!TM_Correct! that require semantical information.  For example, the correctness
Lemmas~\ref{lem:Reset_Realise} (\lstinline!Reset_Realise!) and~\ref{lem:CopyValue_Realise} (\lstinline!CopyValue!) are parametrised over the encoding
of $X$.  The user has to applying the lemmas with the correct encoding manually.

\section*{Example Correctness Proof in Coq}
\label{sec:coq-correctness}

We use the general approach how to prove correctness (or running time) of a Turing machine, as described in Chapter~\ref{chap:compound}.  For example,
consider the following goal (we use the user-defined Coq notation \lstinline!$\Realise$c(k)! to mean $\RealiseIn{k}$).
\begin{lstlisting}
============================
Add_Step $\Realise$c(9) Add_Step_Rel
\end{lstlisting}
The outline of the proof script is:
\begin{lstlisting}
Proof.
  eapply RealiseIn_monotone.
  { unfold Add_Step. TM_Correct. }
  { cbn. reflexivity. }
  { (* ... *) }
Qed.
\end{lstlisting}

The tactic \lstinline!eapply RealiseIn_monoton! applies Lemma~\ref{lem:Realise_monotone}, and creates existential variables $?R'$ and $?k'$ and three
subgoals:
\begin{lstlisting}
3 focused subgoals
(shelved: 2)

============================
Add_Step $\Realise$c(?k') ?R'

subgoal 2 is:
 ?k' <= 9
subgoal 3 is:
 ?R' <<=2 Add_Step_Rel
\end{lstlisting}

After focusing the first subgoal, we unfold the definition of \lstinline!Add_Step!:
\begin{lstlisting}
============================
If (LiftTapes CaseNat [|Fin1|])
   (Return (LiftTapes Constr_S [|Fin0|]) None)
   (Return Nop (Some tt)) $\Realise$(?k') ?R'
\end{lstlisting}

The tactic \lstinline!TM_Correct! automatically applies the correctness lemmas of the conditional, tape-lifting, etc.  It instantiates
\lstinline!?R'! and \lstinline!k'!:
\begin{lstlisting}
?R' := (LiftTapes_Rel [|Fin1|] CaseNat_Rel|_true $\circ$
        ($\bigcup_{f:\Unit}$ LiftTapes_Rel [|Fin0|] S_Rel|_f)||_None)
       $\cup$
       (LiftTapes_Rel [|Fin1|] CaseNat_Rel|_false $\circ$
        ($\bigcup_{f:\Unit}$ Nop_Rel|_f)||_(Some tt))
?k' := 1 + CaseNat_steps + Nat.max Constr_S_steps 0
\end{lstlisting}
where \lstinline!CaseNat_steps! is the constant number of steps required for $\MS{CaseNat}$ (i.e.\ $5$) and \lstinline!Constr_S_steps! is $3$.  By
simplification, the second goal reduces to \lstinline!9<=9!, which is solved by reflexivity of $\leq$.

The main part of the proof is the third goal (with \lstinline!R'! substituted).  We prove it with the following proof script:
\begin{lstlisting}
{
  intros tin (yout, tout) H. cbn. intros a b HEncA HEncB.
  cbn in *. destruct H; TMSimp.
  - modpon H. destruct b; auto.
  - modpon H. destruct b; auto.
}
\end{lstlisting}

After the introduction of the variables and hypotheses \lstinline!HEncA: tin[@Fin0] $\simeq$ a!, \lstinline!HEncB: tin[@Fin1] $\simeq$ b!, and
\lstinline!H: R' tin (yout, tout)!, we make a case-distinction over \lstinline!H! (note that the head symbol in the definition of \lstinline!R'! is
$\cup$, so \lstinline!H! reduces to a disjunction).  This gives two sub-goals.  In both subgoals, we use the automation tactic \lstinline!TMSimp!.
The first subgoal is:
\begin{lstlisting}
tin, tout, tmid : tapes (boundary + sigNat) 2
H : forall n : nat, tin[@Fin1] $\simeq$ n -> match n with
                               | 0 => False
                               | S n' => tmid[@Fin1] $\simeq$ n'
                               end
H0 : forall n : nat, tin[@Fin0] $\simeq$ n -> tout[@Fin0] $\simeq$ S n
a, b : nat
HEncA : tin[@Fin0] $\simeq$ a
HEncB : tin[@Fin1] $\simeq$ b
H0_0 : tmid[@Fin0] = tin[@Fin0]
H1_0 : tout[@Fin1] = tmid[@Fin1]
============================
match b with
| 0 => False
| S b' => tout[@Fin0] $\simeq$ S a /\ tmid[@Fin1] $\simeq$ b'
end
\end{lstlisting}

In this case, we know that \lstinline!a! must be equal to \lstinline!S a'! for some \lstinline!a'!.  The assumption \lstinline!H! is automatically
instantiated with \lstinline!a! and the proof \lstinline!HEncA!.  We finish the goal by case-distinction over \lstinline!a!.  The second goal is
analogous.

Even for complex machines, the correctness proofs in Coq follow this pattern.  It is important to note that the structure of the proof always follows
the structure of the machine.  Running time proofs are analogous.

\section*{Lines of Code}
\label{sec:coq-lines}

In Table~\ref{tab:coq-lines}, we summarise the numbers of Coq code.  We used the tool \texttt{coqwc} to count the lines.  The case-studies are under
the horizontal line in the middle.
\begin{table}[h]
  \centering
  \begin{tabular}{p{6.5cm}|r|r}
    Module                                    & Spec & Proof\\ \hline
    Preliminary (incl. $\Loop$ and relations) &  176 &   84 \\
    Definition of Turing machines             &  430 &  194 \\
    Primitive Machines                        &  122 &   34 \\
    Control-flow operators                    &  425 &  383 \\
    Lifting                                   &  362 &  193 \\
    Simple Machines                           &  380 &  278 \\
    Value containment                         &  394 &  119 \\
    Copying and writing values                &  411 &  288 \\
    Alphabet-Lift with values                 &  133 &  147 \\
    Deconstructors and constructors           &  486 &  482 \\
    Notations and tactics for compound
    or programmed machines                    &  165 &   15 \\  \hline
    $\MS{MapSum}$                             &   47 &  110 \\
    Addition and Multiplication machines      &  181 &  298 \\
    List functions machines                   &  326 &  456 \\
    Heap machine simulator                    &  981 & 1040 \\ \hline\hline
    Total                                     & 5019 & 4121 \\
  \end{tabular}
  \caption{Lines of specification and proof code for the ``modules'' (with several source files)}
  \label{tab:coq-lines}
\end{table}

The total number of library lines is $153$ spec and $2638$ proof.  The total compilation time is circa 4:30~minutes.\footnote{Measured on the
  following hardware: \texttt{Intel(R) Core(TM) i7-4710MQ CPU}; 8 cores @ 2.50GHz; compiled on GNU/Linux with \texttt{make -j8} and Coq 8.8.1.}

Using Coq`s \texttt{Extract} mechanism, we compiled the Coq implementation to Haskell, and we were able to count the total number of states of the
heap machine simulator: The alphabet of $\MS{Loop}$ consists of $30$ symbols and it has $11537$ states.

%%% Local Variables:
%%% TeX-master: "thesis"
%%% End:


% \documentclass[a4paper, 10px]{ctexart}
\documentclass{ctexart}
\usepackage[left=1in, right=1in, top=1.2in, bottom=1.2in]{geometry}
\usepackage{ctex}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{a4wide}
% \setmainfont[Scale = 1]{Palatino}
% \setCJKmainfont{Songti SC}
\usepackage{fancyhdr}
\usepackage{amsmath}
\pagestyle{fancy}
\fancypagestyle{plain}{
    \fancyhead[L]{East China Normal University}
    \fancyhead[R]{}
    \fancyfoot[C]{\thepage}
}
\def\premise{\mathrm{premise}}
\def\assumption{\mathrm{assumption}}
\def\MT{\mathrm{MT\ }}
\def\LEM{\mathrm{LEM}}
\def\PBC{\mathrm{PBC\ }}
\def\intro{\mathrm{i\ }}
\def\elim{\mathrm{e\ }}
\def\introa{\mathrm{i_1\ }}
\def\elima{\mathrm{e_1\ }}
\def\introb{\mathrm{i_2\ }}
\def\elimb{\mathrm{e_2\ }}

\def\n{\neg}
\def\d{\vee}
\def\c{\wedge}
\def\a{\forall}
\def\e{\exists}

\def\SAT{\mathrm{SAT\ }}
\def\AF{\mathrm{AF\ }}
\def\AG{\mathrm{AG\ }}
\def\EF{\mathrm{EF\ }}
\def\EG{\mathrm{EG\ }}
\def\U{\mathrm{\ U\ }}

\def\st{\texttt{Start}}
\def\cl{\texttt{Close}}
\def\he{\texttt{Heat}}
\def\er{\texttt{Error}}

\def\bs{\ \backslash\ }

\newcommand{\set}[1]{\{#1\}}

\title{Logic in Computer Science Assignment 5}
\author{10185101210 陈俊潼}
\date{December 2020}

\begin{document}

\maketitle

\section{Check CTL}

\subsection{Check whether the following models satisfies the CTL formulas.}

\subsubsection{$\AG (\st \to \AF \he)$}

Applying SAT algorithm:
\begin{equation*}
\begin{split}
    \SAT& (\AG (\st \to \AF \he))
    = \SAT(\n \EF \n (\st \to \AF \he)) \\
    &= S \bs \SAT(\EF \n (\st \to \AF \he)) \\
    &= S \bs \SAT(E [\top \U \n (\st \to \AF \he)]) \\
    &= S \bs \SAT_{EU}(\top, \n (\st \to \AF \he)) \\
    & \qquad \qquad
    \begin{split}
        & \SAT(\top) = S \\
        & \SAT(\n (\st \to \AF \he)) = S \bs \SAT (\st \to \AF \he) \\
        & = S \bs \SAT (\n \st \vee \AF \he) \\
        & = S \bs \SAT(\n \st) \cup \SAT(\AF \he) \\
        & = S \bs \{1, 3, 4\} \cup \SAT_{AF}(\he) \\
        & \qquad \qquad
        \begin{split}
            & \SAT_{AF} (\he) = \{4, 7\} \cup \{1,2,3,4,5,6,7\} \\
            & = \{1,2,3,4,5,6,7\}
        \end{split}
        \\ & = S \bs \{\1,3,4\} \cup \{1,2,3,4,5,6,7\} \\
        & = S \bs \{1,2,3,4,5,6,7\} \\
        & = \emptyset\\
    \end{split}
    \\ & \Rightarrow \SAT_{EU}(\top, \n (\st \to \AF \he)) = \emptyset \\
    \Rightarrow \SAT & (\AG (\st \to \AF \he)) = S
\end{split}
\end{equation*}

So the model satisfies the CTL formula.

\newpage

\subsubsection{$\AG (A[\n \er \U (\AF \cl)])$}

\begin{equation*}
    \begin{split}
        \SAT &(\AG (A \n \er \U \AF \cl)) \\
        &= \SAT(\n \EF \n (A[\n \er \U (\AF \cl)])) \\
        &= S \bs \SAT (\EF \n (A [\n \er \U (\AF \cl)])) \\
        &= S \bs \SAT (E (\top \U ()\n A [\n \er \U (AF \cl)])) \\
        &= S \bs \SAT_{EU} (\top, \n A[\n \er \U (\AF \cl)]) \\
        & \qquad \qquad
        \begin{split}
             & \SAT(\top) = S \\
             & \SAT(\n A[\n \er \U (\AF \cl)]) \\
             &= S \bs \SAT (A[\n \er \U (\AF \cl)]) \\
             &= S \bs \SAT (\n (E [\er \U (\er \c \n (\AF \cl))]  \d \EG \n (\AF \cl))) \\
             &= \SAT(E [\er \U (\er \c \n (\AF \cl))] \d \EG (\n (\AF \cl))) \\
             &= \SAT(E [\er \U (\er \c \n (\AF \cl))]) \cup \SAT (\EG (\n (\AF \cl))) \\
             &= \SAT_{EU}(\er, \er \c \n (\AF \cl)) \cup \SAT (\n \AF (\AF \cl)) \\
             &= \SAT_{EU}(\er, \er \c \n (\AF \cl)) \cup (S \bs \SAT (\AF (\AF \cl)))\\
             &= \SAT_{EU}(\er, \er \c \n (\AF \cl)) \cup (S \bs \SAT_{AF}(\AF \cl))\\
             & \qquad \qquad
             \begin{split}
                 & \SAT(\er) = \set{2, 5} \\
                 & \SAT(\er \c \n (\AF \cl)) \\
                 & = \SAT(\er) \cap \SAT(\n \AF (\cl)) \\
                 & = \set{2, 5} \cap (S \bs \SAT {\AF \cl)}) \\
                 & = \set {2, 5} \cap (S \bs \set{1,2,3,4,5,6,7}) \\
                 & = \emptyset
             \end{split}
             \\ & \Rightarrow \SAT_{EU}(\er, \er \c \n (\AF \cl)) = \emptyset \\
             & \qquad \qquad
             \begin{split}
                 & \SAT(\AF \cl) = \SAT_{AF} (\cl)\\
                 & = \set{3,4,5,6,7} \\
                 & = \set{1,2,3,4,5,6,7}
             \end{split}
             \\ & = \emptyset \cup (S \bs \set{1,2,3,4,5,6,7}) \\
             & = \emptyset
        \end{split}
        \\ & = S \bs \emptyset \\
        & = S
    \end{split}
\end{equation*}

So the model satisfies the CTL formula.

\end{document}


\documentclass[12pt]{article}

\usepackage{amssymb,amsmath,amsfonts,eurosym,geometry,ulem,graphicx,caption,color,setspace,sectsty,comment,footmisc,caption,natbib,pdflscape,subfigure,array,hyperref,natbib}

\normalem
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\usepackage{float}
\usepackage{indentfirst}
\usepackage{etoolbox}

\usepackage{lscape}
\usepackage{setspace}
\usepackage{stackrel}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{assumption}{Assumption}
\newenvironment{proof}[1][Proof]{\noindent\textbf{#1.} }{\ \rule{0.5em}{0.5em}}

\newtheorem{hyp}{Hypothesis}
\newtheorem{subhyp}{Hypothesis}[hyp]
\renewcommand{\thesubhyp}{\thehyp\alph{subhyp}}

\newcommand{\red}[1]{{\color{red} #1}}
\newcommand{\blue}[1]{{\color{blue} #1}}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\arraybackslash\hspace{0pt}}m{#1}}

\newcommand\blfootnote[1]{%
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}%
  \addtocounter{footnote}{-1}%
  \endgroup
}
\allowdisplaybreaks
\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in}
\linespread{1.25}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}

\begin{document}

\title{Interactive Projection Tool for COVID-19 Interventions \\ \emph{Technical Documentation}}
\author{COVID-19 Statistics, Policy modeling and Epidemiology Collective (C-SPEC)}
\date{}
\maketitle

\section{Model}

\subsection{Model Structure}

The model uses the following structure, with susceptible individuals (\(S\)) progressing to an exposed state (\(E\)), and then moving to either an undetected symptomatic infected state (\(UI\)) or an undetected asymptomatic state (\(UA\)). From there, symptomatic infected individuals can either die or recover, or move to a detected symptomatic state (\(DI\), and asymptomatic individuals can only recover or move to a detected asymptomatic state (\(DA\)). Detected individuals can then recover or die (if they are symptomatic). (Note that \(\lambda=\beta S I/N\), discussed below.)

\begin{figure}[H]
\centering
\scalebox{1.1}{
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3.7cm,
                    semithick]
  \tikzstyle{every state}=[fill=white,draw=black,text=black]

  \node[state]         (S)                    {$S$};
  \node[state]         (E) [right of=S] {$E$};
  \node[state]         (J) [above right of=E] {$UI$};
  \node[state]         (B) [below right of=E] {$UA$};
  \node[state]         (I) [right of=J] {$DI$};
  \node[state]         (A) [right of=B] {$DA$};
	\node[state]         (R) [above right of=A] {$R$};

  \path (S)  edge  node {\(\lambda\)} (E)
        (E)  edge  node {\((1-\alpha)\delta\)} (J)
             edge  [below left] node {\(\alpha\delta\)} (B)
        (J) edge  node {\(d_I\)} (I)
             edge [below left] node {\((1-m)\gamma\)} (R)
        (B) edge  node {\(d_A\)} (A)
             edge  node {\(\gamma\)} (R)
        (I) edge  node {\((1-m)\gamma\)} (R)
        (A) edge [below right] node {\(\gamma\)} (R);
  \path [draw, ->] (I.north) -- ++(0,1cm) node[above] {\(m\gamma\)};
  \path [draw, ->] (J.north) -- ++(0,1cm) node[above] {\(m\gamma\)};
\end{tikzpicture}}
    \caption{Model Structure}
    \label{fig:model}
\end{figure}

\subsection{Baseline Specification}

The population is stratified by age into three groups: $i = \{< 20, 20-64, 65+\}$. Then, let \(\beta_{ij}=v_{ij} p k_{Si}k_{Ij}\) be the effective contact rate between susceptible and symptomatic individuals, and let \(\beta_{ij_A}=v_{ij}p\kappa k_{Si}k_{Ij}\) be the effective contact rate between susceptible and asymptomatic individuals. Here, \(v_{ij}\) is the number of daily contacts with individuals in group \(j\) per person in group \(i\), \(p\) is the probability of infection per contact between a susceptible and infected individual, \(\kappa\) is the relative reduction in the effective contact rate when the contact is with an asymptomatic individual, compared to a symptomatic individual, \(k_{Si}\) is the relative susceptibility of individuals in age group \(i\), and \(k_{Ij}\) is the relative infectiousness of individuals in age group \(j\). Then, the model is defined by the following system of differential equations (where \(N_i = S_i + E_i + UA_i + DA_i + UI_i + DI_i + R_i\)):
\begin{align*}
\frac{\partial S_i}{\partial t} &= -\sum_{j=1}^3 \beta_{ij} S_i \frac{UI_j+k_{Det}DI_j}{N_j} - \sum_{j=1}^3 \beta_{ij_A} S_i \frac{UA_j+k_{Det}DA_j}{N_j} \\
\frac{\partial E_i}{\partial t} &= \sum_{j=1}^3 \beta_{ij} S_i \frac{UI_j+k_{Det}DI_j}{N_j} + \sum_{j=1}^3 \beta_{ij_A} S_i \frac{UA_j+k_{Det}DA_j}{N_j}- \delta E_i \\
\frac{\partial UA_i}{\partial t} &= \alpha_i\delta E_i  - (k_{Rep,i}r_A+\gamma) UA_i \\
\frac{\partial DA_i}{\partial t} &= k_{Rep,i}r_A UA_i - \gamma DA_i \\
\frac{\partial UI_i}{\partial t} &= (1-\alpha_i)\delta E_i - (k_{Rep,i}r_I + \gamma)  UI_i\\
\frac{\partial DI_i}{\partial t} &= k_{Rep,i}r_A UI_i - \gamma DA_i\\
\frac{\partial R_i}{\partial t} &= (1-m_i)\gamma (UI_i + DI_i) + \gamma (UA_i + DA_i)
\end{align*}

In addition to \(\beta_{ij}\) defined above, \(k_{Det}\) is the relative reduction in social contacts for individuals with a detected infection, \(\delta\) is the rate of progression out of the exposed state, \(\alpha_i\) is the proportion of infections that are asymptomatic for age group \(i\), \(k_{Rep,i}\) is the relative detection (i.e., ``reporting'') rate for individuals in age group \(i\), \(r_A\) is the detection rate for asymptomatic infections, \(r_I\) is the detection rate for symptomatic infections, \(\gamma\) is the rate of progression from infected to recovered or death, and \(m_i\) is the proportion of individuals in age group \(i\) who die once they exit the infected state.

\subsection{Social Distancing Intervention}

In order to model a social distancing intervention, an additional set of ``social distancing'' compartments are added to the model -- a constant fraction \(s\) of each age stratum are split between the social distancing and baseline contact compartments. The effective contact rates between individuals in the social distancing compartments, and between individuals in the baseline contact and social distancing compartments, are reduced by a constant fraction -- the effective contact rates for these interactions become \(\beta_{ij}^{SD}=e v_{ij} p k_{Si}k_{Ij}\) and \(\beta_{ij_A}^{SD}=e v_{ij}p\kappa k_{Si}k_{Ij}\), where \(e\in[0,1]\). Then, the model is defined by the following system of differential equations, where the social distancing compartments are notated with the \(SD\) superscript:
\begin{align*}
		\frac{\partial S_i}{\partial t} &= -\sum_{j=1}^3 \beta_{ij} S_i \frac{UI_j+k_{Det}DI_j}{N_j} - \sum_{j=1}^3 \beta_{ij_A} S_i \frac{UA_j+k_{Det}DA_j}{N_j} \\
		&-\sum_{j=1}^3 \beta_{ij}^{SD} S_{i} \frac{UI_{j}^{SD}+k_{Det}DI_{j}^{SD}}{N_{j}^{SD}} - \sum_{j=1}^3 \beta_{ij_A}^{SD} S_i \frac{UA_{j}^{SD}+k_{Det}DA_{j}^{SD}}{N_{j}^{SD}} \\
		\frac{\partial E_i}{\partial t} &= \sum_{j=1}^3 \beta_{ij} S_i \frac{UI_j+k_{Det}DI_j}{N_j} + \sum_{j=1}^3 \beta_{ij_A} S_i \frac{UA_j+k_{Det}DA_j}{N_j} \\
		&+\sum_{j=1}^3 \beta_{ij}^{SD} S_{i} \frac{UI_{j}^{SD}+k_{Det}DI_{j}^{SD}}{N_{j}^{SD}} + \sum_{j=1}^3 \beta_{ij_A}^{SD} S_i \frac{UA_{j}^{SD}+k_{Det}DA_{j}^{SD}}{N_{j}^{SD}}- \delta E_i \\
		\frac{\partial UA_i}{\partial t} &= \alpha_i\delta E_i  - (k_{Rep,i}r_A+\gamma) UA_i \\
		\frac{\partial DA_i}{\partial t} &= k_{Rep,i}r_A UA_i - \gamma DA_i \\
		\frac{\partial UI_i}{\partial t} &= (1-\alpha_i)\delta E_i - (k_{Rep,i}r_I + \gamma)  UI_i\\
		\frac{\partial DI_i}{\partial t} &= k_{Rep,i}r_A UI_i - \gamma DA_i\\
		\frac{\partial R_{i}}{\partial t} &= (1-m_i)\gamma (UI_i + DI_i) + \gamma (UA_i + DA_i)\\
		\frac{\partial S_i^{SD}}{\partial t} &= -\sum_{j=1}^3 \beta_{ij}^{SD} S_{i}^{SD} \frac{UI_j+k_{Det}DI_j}{N_j} - \sum_{j=1}^3 \beta_{ij_A}^{SD} S_{i}^{SD} \frac{UA_j+k_{Det}DA_j}{N_j} \\
		&-\sum_{j=1}^3 \beta_{ij}^{SD} S_{i}^{SD} \frac{UI_{j}^{SD}+k_{Det}DI_{j}^{SD}}{N_{j}^{SD}} - \sum_{j=1}^3 \beta_{ij_A}^{SD} S_{i}^{SD} \frac{UA_{j}^{SD}+k_{Det}DA_{j}^{SD}}{N_{j}^{SD}}\\
		\frac{\partial E_{i}^{SD}}{\partial t} &= \sum_{j=1}^3 \beta_{ij}^{SD} S_{i}^{SD} \frac{UI_j+k_{Det}DI_j}{N_j} + \sum_{j=1}^3 \beta_{ij_A}^{SD} S_{i}^{SD} \frac{UA_j+k_{Det}DA_j}{N_j} \\
		&+\sum_{j=1}^3 \beta_{ij}^{SD} S_{i}^{SD} \frac{UI_{j}^{SD}+k_{Det}DI_{j}^{SD}}{N_{j}^{SD}} + \sum_{j=1}^3 \beta_{ij_A}^{SD} S_{i}^{SD} \frac{UA_{j}^{SD}+k_{Det}DA_{j}^{SD}}{N_{j}^{SD}} - \delta E_{i}^{SD} \\
		\frac{\partial UA_{i}^{SD}}{\partial t} &= \alpha_i\delta E_{i}^{SD}  - (k_{Rep,i}r_A+\gamma) UA_{i}^{SD} \\
		\frac{\partial DA_{i}^{SD}}{\partial t} &= k_{Rep,i}r_A UA_{i}^{SD} - \gamma DA_{i}^{SD} \\
		\frac{\partial UI_{i}^{SD}}{\partial t} &= (1-\alpha_i)\delta E_{i}{SD} - (k_{Rep,i}r_I + \gamma)  UI_{i}^{SD}\\
		\frac{\partial DI_{i}^{SD}}{\partial t} &= k_{Rep,i}r_A UI_{i}^{SD} - \gamma DA_{i}^{SD}\\
		\frac{\partial R_{i}^{SD}}{\partial t} &= (1-m_i)\gamma (UI_{i}^{SD} + DI_{i}^{SD}) + \gamma (UA_{i}^{SD} + DA_{i}^{SD})
\end{align*}

\section{Baseline Parameter Values}

The interactive projection tool uses the baseline parameter values in the table below as the default settings. Some of these values were set based on available data or similar models -- these sources are also described below.

\begin{table}[H]
\begin{tabular}{p{0.15\textwidth}  p{0.65\textwidth} p{0.2\textwidth}}
\textbf{Parameter}             & \textbf{Description} & \textbf{Value}  \\\hline
\(n_i(0)+n_{i}^{SD}\) & Age Distribution at \(t=0\) & \((0.25, 0.59, 0.16)\)\\
\(v_{ij}\) & Average number of contacts with individuals in group \(j\) per person in group \(i\) &  (See Below)\\
\(p\) & Probability of transmission given contact with an infectious person & 0.05 \\
\(\kappa\)& Relative infectiousness of asymptomatic individuals & 0.375 \\
\(k_{Si}\) & Relative susceptibility of individuals in age group \(i\) & \(1,1,1\) \\
\(k_{Ii}\) & Relative infectiousness of individuals in age group \(i\) & \(1,1,1\) \\
\(k_{Det}\) & Relative contact reduction for infected individuals & 0.5 \\
\(\frac{1}{\delta}\) & Average length of the incubation period    & 5 days \\
\(\alpha_i\) & Proportion of cases in age group \(i\) that do not go on to experience symptoms & \((0.75, 0.3, 0.3)\) \\
\(k_{Rep,i}\) & Relative detection rate for individuals in age group \(i\) & \((1,1,1)\)\\
\(r_A\) & Detection rate for asymptomatic infections & 0.01 detections per day\\
\(r_I\) & Detection rate for symptomatic infections & 0.1 detections per day\\
\(\frac{1}{\gamma}\) & Average duration of infection to recovery or death &  5 days\\
\(m_i\) & Mortality risk for group \(i\) & \((0, 0.01, 0.1)\) \\
\(s\) & Fraction of population in social distancing compartments & 0.01 \\
\(e\) & Relative reduction in contact rates for individuals in socially distancing compartments & 0 \\\hline
\end{tabular}
\caption{Baseline Parameters Values}
\end{table}

\subsection{Age distribution}

We assume an age distribution representative of the United States with 25\% of the population under 20, 59\% between 20 and 64, and 16\% at least 65 \citep{censusbureau}.  While individual municipalities may not reflect these averages, we find that these values are similar across major metropolitan areas (Figure \ref{fig:age.dist}), and we do not formally model uncertainty around these parameters.

\begin{figure}[H]
    \centering
    \includegraphics{dem.pdf}
    \caption{Age distribution by location.  Due to rounding errors, total may not sum to 100\%.  (Source: American Community Survey 2018 via \citet{noauthor_population_2019} and \citet{noauthor_census_2020}).}
    \label{fig:age.dist}
\end{figure}

\subsection{Contact rates between strata}

Very limited primary data exists on age-stratified contacts in the US. Therefore, we rely on methods that project contact rates derived from surveys in other contexts and US demographics. We use the age-structured contact matrix for the US estimated by \cite{prem2017projecting}, which was derived by projecting the POLYMOD \cite{mossong2008social,mossong2017polymod} survey data to US demographics \cite{prem2017projecting}. Since we are interested in 3 broad age-classes, we bin the projected contacts according to US Census Bureau age-distribution estimates for 2018 (\cite{censusbureau}). This provided the following baseline contact matrix, where \(v_{ij}\) is the average number of daily contacts with individuals in group \(j\) per person in group \(i\) (i.e., \(v_{12}\) represents the number of daily contacts where an infectious individual in group 2 could infect a susceptible individual in group 1):

\begin{equation*}
    \begin{bmatrix}
9.7619 & 2.2706 & 0.2923\\
6.6876 & 10.3334 & 0.8853\\
0.9157 & 1.0141 & 1.2495
\end{bmatrix}
\end{equation*}

\subsection{Probability of Transmission}
The probability of transmission is determined by calibrating the model to an empirical estimate of the basic reproduction number \(R_0\). Both \citet{li_early_2020} and \citet{Riou_R0} estimate \(R_0\) at 2.2; following \citet{peak_modeling_2020} we assume a 95\% confidence interval of (1.46, 3.31) and calibrate the transmission probability to the \(R_0\) estimate of 2.2. The calibrated value for the transmission probability per contact is 0.05.

Additionally, we assume that the rate of transmission from asymptomatic individuals is systematically lower than the transmission probability from symptomatic individuals. There is no empirical estimate for this reduction in transmission, however, so we assume that the transmissibility of asymptomatic infection is 37.5\% the rate of symptomatic infection. This assumption reflects a middle ground between the 50\% value used by \citet{Zhao2020} (which is similar to the estimate for influenza) and the 25\% value used by \citet{Prem2020} (which they derived from \citet{Liu2020}).

\subsection{Age-specific mortality rates}

We assume that the mortality risks among symptomatic individuals are 0\%, 1\%, and 10\% for the 0-19, 20-64, and 65+ age groups, respectively. These assumptions are consistent with estimates from \citet{riou_adjusted_2020}, which adjusts crude fatality rates from Hubei province, China to account for delayed mortality and unidentified cases. They estimate that the case fatality risk is less than 0.05\% among individuals 19 years of age and younger, between 0.19\% and 2.7\% for individuals between 20-59 years old, and is at least 9.5\% for individuals older than 60. (For comparison, the \citet{ChinaEpiWG} reports the crude case fatality risk from all reported Chinese cases through February 11, 2020, as 0.10\% for individuals aged 0-19 (1 deaths out of 965 cases), 0.98\% for ages 20-59 (193 deaths out of 19,790 cases), and 5.96\% for ages 60 and older (829 deaths out of 13,909) -- the high proportion of unreported mild cases leads to a lower adjusted CFR for the younger age groups, and the delayed mortality leads to a higher adjusted CFR for the older age group.)

\subsection{Proportion of asymptomatic infections}

We assume that, for individuals 20 years and older, 30\% of infections are asymptomatic. This assumption is supported by findings from \citet{nishiura2020}, which found that 30.8\% (95\% CI: 7.7, 53.8) of infected individuals among Japanese citizens evacuated from Wuhan, China were asymptomatic. Similarly, \citet{mizumoto_estimating_2020} estimate that, among individuals on the Diamond Princess Cruise ship, 17.9\% (95\% CI: 15.5, 20.2) were asymptomatic.

There is less available data on the proportion of asymptomatic infections among individuals younger than 20 years, but we assume that 75\% of such infections are asymptomatic. This assumption is also supported by the data from the Diamond Princess Cruise ship -- among the 6 cases detected in individuals under 20 years old as of Februrary 20, 2020, 4 of the cases were asymptomatic \citep{Russell2020}.

\subsection{Incubation and Latent period}

We assume an incubation period of 5 days, consistent with the existing literature. \citet{li_early_2020} reported a mean incubation period of 5.2 days in the first 425 confirmed patients from Wuhan, China; \citet{linton_2020} estimate a mean period of 5.6 days for patients in China (both inside and outside Wuhan); \citet{lauer_2020} estimates the median incubation period at 5.1 days for patients outside Hubei province, China; while \citet{xu_2020} reports a slightly lower median period of 4 days for patients in Zhejiang province, China, and \citet{backer_incubation_2020} reports a slightly higher mean period of 6.4 days for travelers from Wuhan, China.

Additionally, we assume that the incubation and latent periods coincide and that there is no pre-symptomatic infectious period. This is likely not an accurate assumption, given reports of pre-symptomatic transmission, but unfortunately no data is available on the length of the latent period itself. This assumption will be revisited and updated in the model as more data becomes available.

\subsection{Duration of Infectious Period}

There is limited evidence available on the duration of the infectious period. We assume the duration of the infectious period is 5 days, and that it coincides with the entire duration of disease. This assumption aligns with \citet{Prem2020}, which modeled a length of both 3 and  7 days.

\section{Modeling Interventions}

The interactive projection tool allows users to model different types of interventions by changing the baseline parameters listed above, and evaluate the impact on different outcome measures. For example, in order to model social distancing interventions, the fraction of the population in the social distancing compartments (\(s\)) can be increased and the relative reduction in contacts by individuals in those compartments (\(e\)) can be decreased. Similarly, in order to model treatments that reduce the mortality risk, the values for \(m_i\) can be changed across the population.

\setlength\bibsep{0pt}
\bibliographystyle{aer}
\bibliography{lib}

\end{document}

Documentation/C-SPEC Epidemiological Model Documentation.tex