\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric, arrows}
\usetheme{Hannover}
\begin{document}
	\author{Wang Xuanyu}
	\title{High Performance Parallel FDTD Computation by Using Vector Processor and CUDA}
	%\subtitle{}
	%\logo{}
	\institute{University of Electronic Science and Technology of China}
	\date{June 4 2016}
	\subject{Fundamental Science}
	%\setbeamercovered{transparent}
	%\setbeamertemplate{navigation symbols}{}
	\frame[plain]{\maketitle}
	\lstset{breaklines,tabsize=4,language=C,numbers=left,numberstyle=\tiny,backgroundcolor=\color{lightgray!40!white},extendedchars=false,keywordstyle=\color{blue!70}\bfseries,basicstyle=\tiny,commentstyle=\ttfamily\color{green!40!black}}

\section{Introduction}
	\begin{frame}
		\frametitle{FDTD}
		FDTD: Finite Difference Time Domain.
		\begin{figure}[hp]
			\centering
			\begin{tikzpicture}
			\def \len {3}
			\def \hlen {1.5}
			\def \coe {-0.75}

			%back rectangle wigh dashline
			\draw(0,0) rectangle +(\len,\len);
			\draw [dashed] ($(0,0)+0.5*(0,\len)$) -- +(\len,0);
			\draw [dashed] ($(0,0)+0.5*(\len,0)$) -- +(0,\len);

			%front rectangle wigh dashline
			\draw($(0,0)+\coe*(\hlen,\hlen)$) rectangle +(\len,\len);
			\draw [dashed] ($(0,0)+(0,\hlen)+\coe*(\hlen,\hlen)$) -- +(\len,0);
			\draw [dashed] ($(0,0)+(\hlen,0)+\coe*(\hlen,\hlen)$) -- +(0,\len);

			%connect two rectangle
			\draw (0,0) -- ($(0,0)+\coe*(\hlen,\hlen)$);
			\draw ($(0,0)+(0,\len)$) -- ($(0,0)+(0,\len)+\coe*(\hlen,\hlen)$);
			\draw ($(0,0)+(\len,0)$) -- ($(0,0)+(\len,0)+\coe*(\hlen,\hlen)$);
			\draw ($(0,0)+(\len,\len)$) -- ($(0,0)+(\len,\len)+\coe*(\hlen,\hlen)$);

			%all dashline
			\draw [dashed] ($(0,0)+(0,\hlen)$) -- ($(0,0)+(0,\hlen)+\coe*(\hlen,\hlen)$);
			\draw [dashed] ($(0,0)+(\hlen,0)$) -- ($(0,0)+(\hlen,0)+\coe*(\hlen,\hlen)$);
			\draw [dashed] ($(0,0)+(\len,\hlen)$) -- ($(0,0)+(\len,\hlen)+\coe*(\hlen,\hlen)$);
			\draw [dashed] ($(0,0)+(\hlen,\len)$) -- ($(0,0)+(\hlen,\len)+\coe*(\hlen,\hlen)$);

			\draw [dashed] ($(0,0)+0.5*\coe*(\hlen,\hlen)$) -- ++(\len,0) -- ++(0,\len) -- ++(-\len,0) -- cycle;

			%axis
			\draw [->] (0,0) -- +(1.1*\len,0) node[right]{$y$};
			\draw [->] (0,0) -- +(0,1.1*\len) node[above]{$z$};
			\draw [->] (0,0) -- +($(0,0)-(\hlen,\hlen)$) node[below,left]{$x$};

			%nodes
			\begin{tiny}
			%Hx
			\node[shape=circle,fill=cyan,inner sep=0pt] at (\hlen,\hlen) {$H_x$};
			\node[shape=circle,fill=cyan,inner sep=0pt] at ($(\hlen,\hlen)+\coe*(\hlen,\hlen)$) {$H_x$};
			%Hz
			\node[shape=circle,fill=cyan,inner sep=0pt] at ($(0,0)+(\hlen,0)+0.5*\coe*(\hlen,\hlen)$) {$H_z$};
			\node[shape=circle,fill=cyan,inner sep=0pt] at ($(0,0)+(\hlen,0)+0.5*\coe*(\hlen,\hlen)+(0,\len)$) {$H_z$};
			%Hy
			\node[shape=circle,fill=cyan,inner sep=0pt] at ($(0,0)+(0,\hlen)+0.5*\coe*(\hlen,\hlen)$) {$H_y$};
			\node[shape=circle,fill=cyan,inner sep=0pt] at ($(0,0)+(0,\hlen)+0.5*\coe*(\hlen,\hlen)+(\len,0)$) {$H_y$};
			%Ez
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)$) {$E_z$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+\coe*(\hlen,\hlen)$) {$E_z$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+(\len,0)$) {$E_z$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+\coe*(\hlen,\hlen)+(\len,0)$) {$E_z$};
			%Ex
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+0.5*\coe*(\hlen,\hlen)+(0,\hlen)$) {$E_x$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+0.5*\coe*(\hlen,\hlen)+(\len,0)+(0,\hlen)$) {$E_x$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+0.5*\coe*(\hlen,\hlen)+(0,-\hlen)$) {$E_x$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(0,\hlen)+0.5*\coe*(\hlen,\hlen)+(\len,0)+(0,-\hlen)$) {$E_x$};
			%Ey
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(\hlen,\hlen)+(0,\hlen)$) {$E_y$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(\hlen,\hlen)+\coe*(\hlen,\hlen)+(0,\hlen)$) {$E_y$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(\hlen,\hlen)+(0,-\hlen)$) {$E_y$};
			\node[shape=circle,fill=pink,inner sep=0pt] at ($(\hlen,\hlen)+\coe*(\hlen,\hlen)+(0,-\hlen)$) {$E_y$};
			\end{tiny}
			\end{tikzpicture}
			\caption{The spatial discrete structure of Yee cell}\label{yee cell}
		\end{figure}
	\end{frame}

	\begin{frame}{Vector Processor}
	\begin{figure}[hp]
		\centering
		\def \xl {2cm}
		\def \m {1.76cm}
		\def \s {0.8cm}
		\def \t {0.4cm}
		\def \fth {0.25}
		\begin{tikzpicture}
		\begin{tiny}

		\tikzstyle{lbox} = [rectangle, minimum size = \xl,text centered, draw=black,align=center,fill=lightgray!20]

		\tikzstyle{sbox} = [rectangle, minimum size = \s,text centered, draw=black,align=center,fill=orange!20]

		\tikzstyle{mbox} = [rectangle, minimum width = \m, minimum height=\s,text centered, draw=black,align=center,fill=orange!20]

		\tikzstyle{tbox} = [rectangle, minimum size = \t,text centered, draw=black,align=center,fill=orange!20]

		\tikzstyle{recbox} = [rectangle, minimum width = \xl, minimum height=\s,text centered, draw=black,align=center,fill=orange!20]
		%part1
		\node (cpu) at (0,0) [lbox, label=above:CPU] {};
		\node (core0) at (-\fth*\xl,\fth*\xl) [sbox] {core 0};
		\node (core1) at (\fth*\xl,\fth*\xl) [sbox,fill=orange!40] {core 1};
		\node (core2) at (-\fth*\xl,-\fth*\xl) [sbox] {core 2};
		\node (core3) at (\fth*\xl,-\fth*\xl) [sbox] {core 3};
		\draw (1.8*\fth*\xl,1.8*\fth*\xl) -- (4*\fth*\xl,2*\fth*\xl);
		\draw (1.8*\fth*\xl,0.2*\fth*\xl) -- (4*\fth*\xl,-2*\fth*\xl);

		%part2
		\node (core) [lbox, right of=cpu, xshift=\xl, label=above:Core] {};
		\node (sp) at (5.0*\fth*\xl,-\fth*\xl) [sbox] {SP};
		\node (vp) at (7.0*\fth*\xl,-\fth*\xl) [sbox] {VP};
		\node (cache) at (6*\fth*\xl,\fth*\xl) [mbox] {Cache};
		\draw [-stealth](7*\fth*\xl,-1.8*\fth*\xl) -- (7.5*\fth*\xl,-3*\fth*\xl);
		\draw [-stealth](4.5*\fth*\xl,-1.8*\fth*\xl) -- (1.5*\fth*\xl,-4.2*\fth*\xl);

		%part3
		\node (SP) [recbox, yshift=-\xl,below of=cpu, label=above:scalar processor] {};
		\node [tbox,fill=gray!50] at (0,-6*\fth*\xl) {};
		\node  at (-0.7*\fth*\xl,-6*\fth*\xl) {+};
		\node [tbox,fill=gray!30] at (-1.4*\fth*\xl,-6*\fth*\xl) {};
		\node  at (0.7*\fth*\xl,-6*\fth*\xl) {=};
		\node [tbox,fill=gray!80] at (1.4*\fth*\xl,-6*\fth*\xl) {};

		%part4
		\node (VP) [lbox, below of=core, yshift=-\xl, label=above:vector processor] {};
		\foreach \y in {1,2,3,4}{
			\node [tbox,fill=gray!30] at (4.6*\fth*\xl,-\y*\fth*\xl-3.5*\fth*\xl) {};
		}
		\foreach \y in {1,2,3,4}{
			\node at (5.3*\fth*\xl,-\y*\fth*\xl-3.5*\fth*\xl) {+};
		}
		\foreach \y in {1,2,3,4}{
			\node [tbox,fill=gray!50] at (6*\fth*\xl,-\y*\fth*\xl-3.5*\fth*\xl) {};
		}
		\foreach \y in {1,2,3,4}{
			\node at (6.7*\fth*\xl,-\y*\fth*\xl-3.5*\fth*\xl) {=};
		}
		\foreach \y in {1,2,3,4}{
			\node [tbox,fill=gray!80] at (7.4*\fth*\xl,-\y*\fth*\xl-3.5*\fth*\xl) {};
		}

		%arrows

		\end{tiny}
		\end{tikzpicture}
		\caption{The spatial discrete structure of Yee cell}\label{ch2 fig: yee cell}
	\end{figure}

	\end{frame}

	\begin{frame}{CUDA}
		CUDA: Compute Unified Device Architecture.

		Characteristics:
		\begin{itemize}
			\item Massive threads.
			\item Independent device.
		\end{itemize}
	\end{frame}

	\section{FDTD with VP}
	\begin{frame}{FDTD with VP}{New model}
		\begin{figure}
			\centering
			\begin{minipage}{0.45\textwidth}
				\centering
				\includegraphics[width=\textwidth]{old}
				\caption{The traditional computational model}
			\end{minipage}
%			\hspace*{10pt}
			\begin{minipage}{0.4\textwidth}
				\centering
				\includegraphics[width=\textwidth]{new}
				\caption{The modified computational model}
			\end{minipage}
			\caption{The traditional and new computational model}
		\end{figure}
	\end{frame}

	\begin{frame}{FDTD with VP}{Comparasions}{Number of discrete field points}
		\begin{table}
			\includegraphics[width=0.8\textwidth]{oldnumber}
			\caption{The number of traditional scheme}
		\end{table}
		\begin{table}
			\includegraphics[width=0.8\textwidth]{newnumber}
			\caption{The number of modified scheme}
		\end{table}
	\end{frame}

	\begin{frame}{FDTD with VP}{Comparasions}{Time elapsed}
		\begin{table}
			\includegraphics[width=0.8\textwidth]{vprlst}
			\caption{The comparison between traditional and new computational model}
		\end{table}
	\end{frame}

	\begin{frame}{FDTD with VP}{In different conditions}
		The relation between elapsed time and simulation size:
		\begin{description}
			\item[Space size] The size of space scale is as
			n times as before, the time-consuming will be about $0.92n + 0.08$ times than before.
			\item[Time size] The size of time scale is as
			$n$ times as before, the time-consuming will be about $0.99n$ times than before.
		\end{description}
	\end{frame}

	\section{FDTD with CUDA}
	\begin{frame}[containsverbatim]{FDTD with CUDA}{Implementation}
	\begin{lstlisting}
	int x, y, tid, number;
	float dif_Hy, dif_Hx;
	tid = threadIdx.x + blockIdx.x*blockDim.x;
	while (tid < ele_ex*size_Ez_y)
	{
		number = tid + 1;
		y = number % ele_ex;//row
		x = number - (y*ele_ex);//column
		//Hy(i,j)	-	Hy(i-1,j)
		dif_Hy = Hy[y*ele_hy + x] - Hy[(y - 1)* ele_hy + x];
		//Hx(i,j-1)	-	Hx(i,j)
		dif_Hx = Hx[y*ele_hx + (x - 1)] - Hx[y*ele_hx + x];
		Ez[y*ele_ex + x] += coe_Ez * (dif_Hx + dif_Hy);
		tid += blockDim.x*gridDim.x;
	}
	\end{lstlisting}
	\end{frame}

	\begin{frame}{FDTD with CUDA}{Comparison}
		\begin{table}
			\includegraphics[width=0.8\textwidth]{cuda}
			\caption{The comparison between the modified data parallelism and using CUDA}
		\end{table}
	\end{frame}

	\begin{frame}{FDTD with CUDA}{In different conditions}
		In all conditions, time elapsed in a single running time is less that 0.01.
	\end{frame}

	\section{Conclusion}
	\begin{frame}{Conclusion}
		In this, we did following contributions:
		\begin{description}
			\item[FDTD with VP] Proposed a new computational model, which can save about 3.45\% time. The result had been sent to a journal.
			\item[FDTD with CUDA] Implemented the Mur ABC with CUDA. In the profiling result we can see how powerful the GPU is in parallel computation.
		\end{description}
	\end{frame}

	\section{Thanks}
	\begin{frame}{Acknowledgements}
		Thanks for your patience and attention.
	\end{frame}
\end{document}


\title{\bf Images}

\section{Basics \& Nomenclature}

Two-dimensional array-based detectors at the focal planes of
telescopes produce images --- two-dimension rasters of values.  Here
we consider the basic properties such images with an emphasis on the
sorts of issues affecting ultraviolet, optical, and infrared imaging
instruments. Many of the principles hold for other sorts of data sets.

These images can be thought of as noisy samplings of an {\it image
function} which is the convolution of the image coming from space with
a response function known as the {\it point spread function}
(PSF). The PSF can have contributions from the Earth's atmosphere, the
telescope's physical and geometric optics, and the detector.

The width of the PSF is usually characterized by its full-width
half-maximum (FWHM). An important property of an imaging system is its
sampling density relative to the FWHM. If this density is high enough,
typically greater than 2 pixels per FWHM, the PSF will be close to
{\it Nyquist sampled}, meaning that if the image were noiseless it
would preserve all of the information in the image function. The
exercises describe the origin of this criterion, which is related to
the true Nyquist sampling criterion of a particular band-limited
function (one with constant Fourier amplitudes out to some
wavenumber). Typically instruments do not truly Nyquist sample the
image function, but in certain circumstances it can be possible.

Images taken outside the Galactic Plane which are not extraordinarily
deep usually allow one to separate the flux into individual objects,
which are generally classifiable as {\it point sources} or {\it
extended sources}. The point sources are those whose images are
consistent with the PSF. With ordinary imaging capability, stars are
point sources. Galaxies are often extended sources, with FWHM larger
than the PSF FWHM. High redshift, luminous quasars usually appear as
point sources, though with high contrast imaging from space their
extended host galaxies can be detected.

The image locations can be quantified by their $x$ and $y$ pixel
values. The center of each object is typically determined by the mode
of the light distribution, or sometimes by a weighted centroid of the
flux distribution (which is less precise but for extended sources may
be more desirable at times). The relationship between these $x$ and
$y$ pixel values and the RA and Dec locations they correspond to is
called the {\it astrometric solution}. Usually the astrometric
solution is expressed in terms of a World Coordinate System
(\citealt{greisen02a}) description of the image, which provides the
metadata encoding an approximate projection of the image and
distortions in that projection.

The raw image is usually obtained in some uncalibrated form. In terms
of the specific intensity of the image $I_\nu(\alpha, \delta)$ coming
from outside the Earth's atmosphere convolved with the instrumental
resolution, the raw image in DN$(x,y)$ in pixel coordinates may be
written:
\begin{equation}
\mathrm{DN}(x, y, t) = \mathrm{DN}_{\mathrm{back}}(x, y, t) +
\int \frac{\dd{\nu}}{\nu} Q(\nu, {\rm alt}, {\rm az}, x, y, t)
\left[I_\nu(\nu, \alpha, \delta) + S_{\nu}(\nu, {\rm alt}, {\rm az},
t)\right]
\end{equation}
$Q$ is a general response function written to depend on the pixels
detecting the objects, the local sky coordinates (though it might have
a more general form in some cases), frequency, and time. $S_{\nu}$ is
background, generally due to sky emission, and written in terms of
altitude and azimuth, to suggest the important axes, but also in terms
of time (e.g. where the Moon is). DN$_{\mathrm{back}}$ is the
instrumental background.

Often the response $Q$ is at least roughly separable as follows
\begin{equation}
Q(\nu) = R(\nu) A({\rm alt}, {\rm az}, \nu) F(x, y)
\end{equation}
where $R(\nu)$ is the instrumental bandpass, $A$ is the atmospheric
throughput, and $F$ is the flat-field. $R$ is often defined to
incorporate the dependence of the atmosphere on wavelength at some
nominal altitude, so that $A$ would express only the differences from
that. $F$ is often defined to include the determinant of the Jacobian
between $(\alpha, \delta)$ and $(x, y)$ which would otherwise need to
appear and which primarily depends on the instrument. $F$ is usually
defined to have a mean around unity.

A final common approximation is that the functions are sufficiently
separable or the dependence on angle is sufficient small over the
field of view of the image that we can write:
\begin{equation}
\mathrm{DN}(x, y, t) = \mathrm{DN}_{\mathrm{back}}(x, y, t) +
\bar{A} F(x, y) \left[\mu(\alpha, \delta) + \mu_S({\rm alt}, {\rm az},
t)\right]
\end{equation}
Here, $\bar{A}$ is the throughput of the instrument and atmosphere
averaged over wavelength.  We separate DN$_{\rm back}$ and $\mu_S$
because they are from different physical sources. For example, in a
CCD, DN$_{\rm back}$ is due to dark current and and bias in the
device, whereas $\mu_S$ is emission from the sky itself.

The process of {\it photometric calibration} is the conversion of the
observed DN to $\mu$. In its most basic form, calibration requires
subtracting the instrumental background, scaling the remaining flux by
the overall factor $1/\bar{A}$, dividing by the flat field, and then
subtracting the sky. Typically, the instrumental background is
determined through bias frames taken with the shutter shut.  The flat
field is determined by observing either the sky in twilight or by
observing a screen (sometimes just the dome wall) illuminated such
that it mimics light uniformly entering the telescope aperture.

It is somewhat less standard how $\mu_S$ and $\bar{A}$ are
determined. In most cases, determining either one requires some
detection and measurement of objects in the image to occur, and their
determination is usually somewhat iterative.

For $\mu_s$, usually one takes an initial stab at the background level
(e.g. just a median), detecting the bright objects, and then
redetermining the background and its variation across the image when
excluding ``detected'' pixels; one can then iterate this
procedure. Typically this procedure subtracts not just the atmospheric
sky emission, but also other sources such as zodiacal light, and also
some fraction of the light from detected stars and galaxies. Whether
these other sources should or should not be subtracted depends on what
is wanted out of the image.

For $\bar{A}$, the concept is to use detected objects in the field
whose $\mu$ is known from a catalog of standards to calibrate the
entire image. Sometimes the objects used for calibration are not in
the same field but are objects observed close in time to the field of
interest with the same instrument; in such cases the difference in
airmass of the field and the standard field needs to be accounted
for. Often in these cases the standards are not known in exactly the
same filters as the observations, and these color-dependent effects
must be taken into account. Sometimes there are no standards as such,
but instead a suite of overlapping observations can be calibrated onto
a self-consistent scale (e.g., \citealt{finkbeiner15a}).

Once an image is calibrated, the stars, galaxies, or other objects may
be measured for their fluxes and other properties. The fluxes may be
measured through fixed apertures, or one may fit models to the data
and infer fluxes from those models.

\section{Commentary}

The description above of calibration procedures is roughly accurate,
but is more illustrative than anything. There are many different
techniques in use, driven by the nature of the instrument and
observations as well as individual tastes. In part because each
individual experiment is so different, astronomers (including myself)
tend to learn about this subject in the context of a particular set of
observations rather than from a generalized perspective. Keep that in
mind when people (including me!) are telling you how this process
works!

At the highest precision, the calibration effects described above are
not truly separable. For example, the astrometric solution depends on
the definition of the PSF, because that determines what exactly you
consider the location of each object, and the flat-fielding, which can
also affect the centroiding.

A more subtle example is that the flux definitions cannot truly be
separated from the calibration. The calibration must assume some
effective aperture for the fluxes of the calibrating sources, but some
flux will leak outside the aperture, in ways that can vary across the
images used in the calibration. These aperture effects need to be
accounted for for percent level calibration accuracy.

\section{Key References}

\begin{itemize}
  \item
    {\it Design and Construction of Large Telescopes},
      \citet{bely03a}
  \item
    {\it Astrophysical Techniques}, \citet{kitchin09a}
\end{itemize}

%\section{Order-of-magnitude Exercises}
%%
%\begin{enumerate}
%\item Proper motion of galaxies
%\end{enumerate}

\section{Analytic Exercises}

\begin{enumerate}
\item {\it Origin of the Nyquist criterion}. The Nyquist
    sampling criterion is based on the concept of a ``band
    limit.''
    \begin{enumerate}
    \item Show that if the 2D Fourier
    transform $f(\vec{k})$ of the point spread function has zero power
    higher than the band limit $k_{\rm max}$, then it can be perfectly
    described using a discrete Fourier transform that extends only up
    to that $k_{\rm max}$. What is the necessary configuration-space
    sampling for the discrete Fourier transform?
    \item Assume $f(\vec{k}) = {\rm constant}$
    below the band limit, and zero above it. What is the resulting PSF
    $f(\vec{x})$?
    \item Under what circumstances might the criterion in part (b)
    hold, given what you know about how telescopes work?
    \item What is the FWHM of $f(\vec{x})$ in units of the sampling?
    This sets the Nyquist criterion for sampling.
    \end{enumerate}
\item {\it Interpolation}. Imagine starting with an image sampled on a
    rectangular grid. Interpolation is the process of inferring
    the value of an image in between the given sampled points of the
    image. The case of Nyquist sampled, band limited images motivates
    a particular method of interpolation.
    \begin{enumerate}
    \item Explain how linear interpolation can be recast as
    constructing a model of the image using a set of basis functions,
    or ``kernels,'' centered on the original grid points.
    \item Explain why a (noiseless) Nyquist sampled, band limited
    image contains all the information necessary for perfect
    interpolation.
    \item If you interpolate perfectly in that situation, what is the
    effective kernel you are using? Note that this is called {\it
    sinc interpolation}.
    \item Explain why using exactly that interpolation kernel might be
    problematic.
    \end{enumerate}
\item Following the methods of Section 2 of \citet{vakili16a}, show
    how the best possible centroiding accuracy depends on the FWHM and
    on the total signal-to-noise ratio ($S/N$) of a Gaussian point
    source.
\end{enumerate}

\section{Numerics and Data Exercises}

\begin{enumerate}
\item {\it Interpolation}. Create an image of a critically sampled
    Gaussian PSF centered at the middle of a pixel. Create another
    image of the same PSF, but with its center offset some fraction of
    a pixel from the first case. We will test how well different types
    of interpolation work by trying to shift the first image using
    interpolation and comparing it to the second image.
\begin{enumerate}
\item Use linear interpolation to try to shift the first image
    so the PSF has the same center as in the second image. Compare the
    absolute and fractional differences (pixel-by-pixel) between first
    image shifted and the second image.
\item
    Perform the same test with sinc interpolation.
\item Perform the same test with a ``damped'' version of sinc
    interpolation, which multiplies the kernel by a broad Gaussian
    (say, a few FWHM broad).
\end{enumerate}
\item CCD images from SDSS are available
as \href{https://www.sdss.org/dr14/imaging/images/#corr}{corrected
frames}. Those images have valid WCS headers associated with
them. Using the tools from {\tt astropy}, take RA and Dec values from
objects in a randomly chosen field (get these from the {\tt photoObj}
table in CAS) and overplot their locations on the CCD $r$-band image.
\item This problem tests the measurement of image centroids.
\begin{enumerate}
\item Write a piece of code to generate a fake, critically-sampled
image of a double Gaussian, with a center that isn't necessarily at
the center of a pixel. For the second Gaussian, use $A_2 = 0.1 A_1$
and $\sigma_2 = 2.47\sigma_1$, where $A$ indicates the value at the
center of the Gaussian. This choice is an approximate description of
the atmospheric PSF (Jim Gunn, private communication).
\item Write a routine to find the light-weighted centroid of the
image. Start by using the maximum pixel value, and use your knowledge
of the PSF FWHM to calculate the center based on the light within 3
FWHM, and iterate to convergence.
\item Write a routine to find the mode of the image. Start by using
the maximum pixel value, but then use the 3$\times$3 grid of pixels in
the center to perform a quadratic interpolation to find the peak.
\item Now add noise to the images, and use a Monte Carlo test to
evaluate how the precision of each estimate depends on the total $S/N$
within 3 FWHM.
\end{enumerate}
\item It is common to use the measured signal from a Poisson process
to estimate the noise. Use a Monte Carlo technique to estimate the
bias that this causes as a function of the expectation value $\bar N$
for the Poisson process.
\end{enumerate}

\bibliographystyle{apj}
\bibliography{exex}


%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Thesis template by Youssif Al-Nashif
%
%   May 2020
%
%%%%%%%%%%%%%%%%%%%%%%%%

\section{Processing Text into Graphs}

\hspace*{0.3cm} For both datasets, the reddit comments and the SCI papers, the text data is collected and stored in its raw form for reproducibility. The text is then cleaned and prepared for analysis using a suite of text processing tools from the \texttt{\{tidytext\}} package for R \cite{silge2016tidytext}. Using this package, the text undergoes tokenization into skip-grams, stop word removal, and filtering to remove punctuation, numbers, short words, etc. \\

Skip-grams are produced for $k=2,3,4$, where $k$ is the window width of the skip-gram for which bigrams are formed. Stop words are gathered from popular lexicons: ``snowball", ``SMART", and ``onix" \cite{silge2016tidytext}. As stated above, all numeric values, and lingering punctuation were also removed through use of the \texttt{\{stringR\}} package \cite{wickham2010stringr}. This processing is an essential preprocessing step for using graph kernels to gauge similarity of documents, because words and strings (like punctuation or numbers) that get repeated frequently across many documents in the set do not actually mean the documents are similar\textemdash they just have common reoccurring words. Removing these types of words forces the text data to be more unique across the document set.  \\

The result of the skip-gram tokenizer is a data frame where each observation is a bigram, a pair of words, that the skip-gram window captured. The data frame is then cleaned of stop words through using anti-joins on each bigram; any row with the appearance of a stop word in either position, the first or second word, was removed. The same method was applied for punctuation and numbers. The result was a data frame of bigrams which appeared in a fixed window width, $k$, of one another and where both words are of a length greater than 3 letters, not a stop word, and do not contain numbers or punctuation. This cleaned dataset is now ready to be converted into a graph object. \\

Now that the skip-grams are cleaned, the data frame can be converted to a graph object, through use of the \texttt{\{igraph\}} package \cite{csardi2013package}. To do this, each word pair in the data frame is converted to an edge and vertex pair in the graph object. For example, the word pair ``data frame" will become two vertices, labeled ``data" and ``frame", with an undirected edge connecting them. In this study, directed graph edges are not used, but could be considered in future iterations of this work. When this completed for all the skip-gram pairs that were generated, it produces a singular connected graph, however a great deal of filtering occurred and there may be disconnected portions. When words are removed from the graph, a vertex will disappear and can potentially split the graph. This is not too common in the NHTSA dataset for two reasons. First, the text data is quite long, and so if a word is reused at a later time in the text there will be an additional connection to keep it included in the main graph. Secondly, the benefit of the skip-gram, as opposed to plain bigrams, is that the larger window width means words get connected and can ``skip" over words that will get removed through the data cleaning process. So at this point, if there is a group of words that are isolated and not connected to the main graph, they are often quite small and are only several words. To keep computation simple, these lingering small isolated graphs are removed. The vertices that are not members of the main graph are removed from the graph object and that leaves a single connected graph for each text document. These graphs can be compared with graph kernels easily now.


\paragraph{}
The SBFEM developed by Song and Wolf \citep{Wolf1996} provides a promising semi-analytical method to analyze problem in fracture mechanics and unbounded domain.
As a method developed based on the FEM and the BEM, the SBFEM is a fundamental-solution-less boundary element method which keeps the benefits of the both as well as provides some effective solutions to the limitations to the FEM and the BEM \citep{Wol1999}.
In contrast to the FEM, only the boundary is discretized using the conventional FEM interpolating function which leads to a decline in the number of unknowns.
It also allows solving the problem involving bimaterial interfaces and crack faces without the discretization of them.
Compared to the BEM, the fundamental solution is no longer required.
The infinite boundary can be achieved naturally as the radiation condition at infinity is satisfied in the SBFEM \citep{Wol2003}..

\paragraph{}
Fig.~\ref{lr_fig:sbfem_intro} illustrates a basic concept of the SBFEM.
A scaling center $O$ is selected at a point from which the whole boundary of the domain is visible (scaling requirement).
This condition is automatically satisfied for all convex polygons and many concave polygons.
The scaling requirement is equivalent to the notion of ``star convexity'' \citep{Bishop2014}.
For the domain that does not meet the scaling requirement, the requirement can always be satisfied by sub-structuring, i.e. dividing the structure into smaller subdomains, for example, scaled boundary polygon formulation \citep{NATARAJAN2014101}.
The problem domain can be covered by scaling the boundary in the radial direction respect to the scaling center with a ratio $\xi \in [0,1]$.
In the domain, only the boundary at $\xi=1$ is discretized with the conventional shape functions.

\begin{figure}[!ht]
    \centering
    \scalebox{0.5}{
        \includegraphics{literature/images/sbfem_intro.eps}
    }
    \caption[Two dimensional scaled boundary coordinates]{Two dimensional scaled boundary coordinates, where O is the scaling center and $\xi$ is the radial coordinate with $\xi=0$ at the scaling center and $\xi=1$ on the boundary.}
    \label{lr_fig:sbfem_intro}
\end{figure}

\paragraph{}
The method proved to be far more versatile and was applied to static problems in bounded domains and extended to take into account prescribed displacements \citep{DEEKS20041153} and concentrated loads \citep{Vu2014}.
A simple derivation of the necessary equations based on the virtual work principle is also presented \citep{Dee2002}.
This spurred the interest among researchers, as the similarity with the virtual work-based FEM derivation was highlighted.
The method is further developed by deriving a stress recovery technique that was later adopted in adaptive refinement techniques, such as h- and p-adaptive SBFEM \citep{NME:NME439, doi:10.1002/nme.440, Vu2008441, YANG20111417}.
The wave interaction with a cylindrical structure is investigated \citep{TAO2007232} and the method is also extended to structural dynamics \citep{Song2009} where the dynamic stiffness matrix was obtained as a continued fraction solution.
The main advantage of this approach is that the inertial effect at high frequencies can be modeled by high-order terms of the continued fraction without introducing an internal mesh.
A higher order spectral element was used in computation of the dispersion curves of the elastic wave using the SBFEM \citep{GRAVENKAMP201446} and a superior accuracy compared to the conventional approaches is shown.

\paragraph{}
The conventional FEM is known to be inefficient to deal with internal discontinuities such as material interfaces or singularities.
In an effort to overcome the limitations of the FEM, mesh-free methods and enrichment techniques such as the extended finite element method (XFEM) were introduced.
Treatment of evolving discontinuities in mesh-free methods \citep{doi:10.1002/nme.1151,Rabczuk2007} and enrichment techniques \citep{Babuška20031,doi:10.1002/nme.1966,CHAUDINH2012242,AREIAS2013113} is more straightforward because it does not require conforming mesh or frequent mesh adaptation as the discontinuities evolve.
On the other front, by exploiting the unique feature of the scaling center, the method allows the computation of stress intensity factors directly from their definitions \citep{Dee2005,SONG2002183}.
This has emerged to be an attractive alternate to the already established methods such as the XFEM and the meshless methods to model crack propagation.

\paragraph{}
Chidgzey et al. \citep{CHIDGZEY20081198,BIRD2010599} coupled the SBFEM with the BEM for computations in fracture mechanics.
This framework combines the semi-analytical solution accuracy of the SBFEM with the geometric flexibility provided by the BEM.
Natarajan and Song \citep{doi:10.1002/nme.4557} combined the extended FEM and the SBFEM, thus, circumventing the need to know a priori the enrichment functions, required by the former.
Recently, Ooi et al. \citep{doi:10.1002/nme.4284} and Natarajan et al.\citep{NATARAJAN2014101} employed scaled boundary formulation in polygonal elements to study crack propagation and compared the performance with other displacement based formulations, respectively.
Li et al.\citep{LI201352} applied SBFEM to analyze two-dimensional fracture problems in piezoelectric materials.
Ooi et al.\citep{doi:10.1002/nme.4284,OOI20101178,OOI20131} developed an efficient methodology for automatic crack propagation simulation using the SBFEM.

\paragraph{}
It can be seen that, since the inception of the method, the SBFEM has been applied to various problems in engineering and science.
It should be noted that most of the above studies employed Lagrange interpolants to approximate the unknown fields in the circumferential direction. He et al.
\citep{HE201228,HE2014152} employed moving least square (MLS) shape functions and Fourier series expansion to approximate the displacement fields in the circumferential direction.
It should be noted that the MLS and Fourier basis functions do not satisfy Kronecker δ property and that special care must be employed to enforce the boundary conditions.
It was shown that the SBFEM with MLS and Fourier shape functions yielded more accurate results than the MLS.
Very recently, Lin et al.\citep{Lin2014} employed non-uniform rational B-splines to approximate the unknown field in the circumferential direction.
However, their study was limited to 2D elastostatics.


\documentclass{article}

% math stuff
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{xcolor}

\usepackage{mathtools}

\usepackage{float}
\usepackage{subcaption}

% to insert images
\usepackage{graphicx}

% to correctly insert stressed characters
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{multirow}

% Bibliography
% \usepackage[style=alphabetic]{biblatex}
% \usepackage[nottoc]{tocbibind}
% \usepackage{bibentry}
% \setcounter{biburllcpenalty}{9000}
% \usepackage{nameref}

% to put links in table of contents
\usepackage{hyperref}
\hypersetup{colorlinks=false, %set true if you want colored links
	linktoc=all,     %set to all if you
}

% Add symbols
% \usepackage{textcomp}

% Add command for Real and Z sets
% \usepackage{dsfont}
% \newcommand{\Rset}{$\mathds{R}$}
% \newcommand{\Zset}{$\mathds{Z}$}

% Code highlighting
% \usepackage{minted}
% \usemintedstyle{perldoc}
% \setminted{
%     frame=single,
%     breaklines,
% }

% tikz figures
\usepackage{tikz}
\usepackage{tikzit}
\input{style.tikzstyles}
\usetikzlibrary{positioning}

\newtheorem{theorem}{Theorem}
\newtheorem{claim}[theorem]{Claim}

\begin{document}

\section{Approximability of Echo Chamber Problem}%
\label{sec:approximability_of_echo_chamber_problem}

\begin{theorem}
	\label{th:approximability}
	Echo Chamber Problem (ECP) has no $n^{1-\epsilon} $-approximation algorithm for
	any $\epsilon$ unless $\mathcal{P} = \mathcal{NP}  $
\end{theorem}

\begin{proof}
	We show this by presenting a direct reduction from \textsc{Maximum
		Independent Set} (MIS), which is known having the mentioned hardness factor.

	\bigskip
	Let $G_{1}  = (V_{1} ,E_{1} )$ be an undirected and unweighted graph and
	$\lambda \geq \frac{\alpha }{1 - \alpha }$, $\lambda \in \mathbb{N}$ and
	$n_{1} \coloneqq |V_{1}| $.
	We construct the \emph{interaction} graph ${G}_{2}  = (V_{2} , E^{+}_{2} , E
			^{-}_{2} ) $ as follows

	\begin{itemize}
		\item for each vertex $v_{i}  \in V_{1} $ we add a vertex in $G_{2} $
		\item for each edge $e_{ij}  \in
			      E_{1} $ we add $\lambda n_{1} $ negative edges between $v_{i} $ and $v_{j} $
		\item add a vertex $v_r$ and a positive edge between it and any other
		      vertex that we already inserted in $G_2$
		\item add a vertex $v_x$ and $\lambda n_{1} $ negative edges between $v_x$
		      and $v_{r} $
	\end{itemize}

	Furthermore, all the edges in $G_{2} $ are associated to the same content
	$C$ and the same thread $T \in \mathcal{T}_{C}  $.
	An illustration of the conversion can be found in \autoref{fig:construction}.

	\begin{figure}[hbt]
		\begin{center}
			\begin{subfigure}[b]{0.4\textwidth}
				\centering
				\caption{$G_{1}$, undirected graph}
				\tikzfig{tikz/approximability1}
				\label{fig:g1_example}
			\end{subfigure}
			\begin{subfigure}[b]{0.4\textwidth}
				\centering
				\tikzfig{tikz/approximability2}
				\caption{$G_{2}$, directed signed graph, for $\lambda = 1$}
				\label{fig:g2_example}
			\end{subfigure}
		\end{center}
		\caption{Example construction of the interaction graph $G_{2} $ from
			$G_{1} $, for $\alpha = \frac{1}{2} $}
		\label{fig:construction}
	\end{figure}

	\begin{claim}
		\label{th:claim-controversial}
		Content $C$ is controversial.
	\end{claim}
	\begin{proof}
		Let $m_{2}^{-} $ and $m_{2}^{+} $ be the number of negative and
		positive edges in $G_2$, respectively.

		By construction $m_{2}^{+} = n_{1} $ and $m_{2}^{-} \geq \lambda n_{1}
		$. Also, for $a, b, c \in \mathbb{R}^{+}$ it holds that $\frac{a +
				b}{a + b + c} \geq \frac{a}{a + c} $. Consequently

		\begin{align}
			\eta(C) = \frac{m_{2}^{-} }{m_{2}^{-} +
				m_{2}^{+} } \geq \frac{\lambda n_{1}}{\lambda n_{1}
				+ n_{1} } = \frac{\lambda }{\lambda + 1} =
			\frac{ \frac{\alpha }{1 - \alpha }  }{ \frac{\alpha }{1 - \alpha }
				+ 1 } \geq \alpha
		\end{align}
	\end{proof}

	So content C is controversial. This reduces the Echo Chamber Problem on $G_2$ to the maximization of

	\begin{equation}
		\label{eq:score}
		\xi(U) = \sum^{}_{T \in S_{C}(U) } | T[U] |
	\end{equation}

	\begin{claim}
		\label{th:opt-equality}
		\begin{equation}
			OPT(ECP) = OPT(MIS)
		\end{equation}
	\end{claim}

	\begin{proof}
		Let $I \subseteq V_{1} $ be an independent set of $G_1$ of size $|I| >
			1$. Consider the associated solution in $G_2$ in which $U = I
			\cup \{v_{r} \}$. By construction it will contain $|I|$ positive
		edges, so $T$ will not be controversial and also

		\begin{equation}
			OPT(ECP) \geq \xi(U) = |T[U]| = |I| \implies OPT(ECP) \geq OPT(MIS)
		\end{equation}

		Now let $S \subseteq V_2$ be a solution of the Echo Chamber problem on
		$G_2$, and suppose $\xi(S) > 0$. It is easy to see that $v_{r} \in S$
		and that $v_{x} \not\in S $. Let $J \coloneqq S \setminus \{v_r\}$.

		Suppose that $2$ vertices $v_{i} $, $v_{j} \in J$ are linked in
		$G_1$. By construction there are at least $\lambda n_1$ negative edges
		in $T[S]$, thus

		\begin{equation}
			\eta(T[S]) \geq \frac{\lambda n_1}{\lambda n_1 + |S-1|} \geq \frac{\lambda n_1}{\lambda n_1 + n_1} = \frac{\lambda
			}{\lambda + 1} \geq \alpha
		\end{equation}

		This means that $T[S]$ is controversial $\implies \xi(S) = 0
			\implies contradiction$. Consequently $J$
		contains vertices which are independent in $G_1$. Therefore $T[S]$ contains
		only positive edges; more specifically

		\begin{equation}
			\xi(S) = |T[S]| = |S| - 1 = |S \setminus \{v_r\}| = |J|
		\end{equation}

		Thus

		\begin{equation}
			OPT(MIS) \geq |J| \implies OPT(MIS) \geq OPT(ECP)
		\end{equation}

		So the optimal value of the constructed instance of Echo Chamber Problem
		exactly equals that of the \textsc{Maximum Independent Set} instance, so it
		has an hardness factor at least as large as that of MIS.
	\end{proof}

	This concludes the proof of \autoref{th:approximability}.
\end{proof}

\newpage

\section{Approximability of Densest Echo Chamber Problem}%
\label{sec:approximability_of_densest_echo_chamber_problem}

\begin{theorem}
	\label{th:approximability-densest}
	Densest Echo Chamber Problem (D-ECP) has no $n^{1-\epsilon} $-approximation algorithm for
	any $\epsilon$ unless $\mathcal{P} = \mathcal{NP}  $
\end{theorem}

\begin{proof}
	We again show this by presenting a direct reduction from \textsc{Maximum
		Independent Set}.

	\bigskip
	Let $G_{1}  = (V_{1} ,E_{1} )$ be an undirected and unweighted graph and
	$\lambda \geq \frac{\alpha }{1 - \alpha }$, $\lambda \in \mathbb{N}$ and
	$n_{1} \coloneqq |V_{1}| $.
	We construct the \emph{interaction} graph ${G}_{2}  = (V_{2} , E^{+}_{2} , E
			^{-}_{2} ) $ as follows

	\begin{itemize}
		\item for each vertex $v_{i}  \in V_{1} $ we add a vertex in $G_{2} $
		\item for each edge $e_{ij}  \in
			      E_{1} $ we add $\lambda (n_{1}+1)^{2}  $ negative edges between $v_{i} $ and $v_{j} $
		\item for each edge $e_{ij} \in V_1 \times V_1, e_{ij} \not\in
			      E_{1} $ we add $2$ positive edges between $v_{i} $ and $v_{j} $
		\item add a vertex $v_r$ and $2$ positive edges between it and any other
		      vertex that we already inserted in $G_2$
		\item add a vertex $v_x$ and $\lambda n_{1}^{2}  $ negative edges between $v_x$
		      and $v_{r} $
	\end{itemize}

	Furthermore, all the edges in $G_{2} $ are associated to the same content
	$C$ and the same thread $T \in \mathcal{T}_{C}  $.
	An illustration of the conversion can be found in
	\autoref{fig:construction-densest}.

	\begin{figure}[hbt]
		\begin{center}
			\begin{subfigure}[b]{0.4\textwidth}
				\centering
				\caption{$G_{1}$, undirected graph}
				\tikzfig{tikz/approximability1-densest}
				\label{fig:g1_example}
			\end{subfigure}
			\begin{subfigure}[b]{0.4\textwidth}
				\centering
				\tikzfig{tikz/approximability2-densest}
				\caption{$G_{2}$, directed signed graph}
				\label{fig:g2_example}
			\end{subfigure}
		\end{center}
		\caption{Example construction of the interaction graph $G_{2} $ from
			$G_{1} $}
		\label{fig:construction-densest}
	\end{figure}

	\begin{claim}
		\label{th:claim-controversial-densest}
		Content $C$ is controversial.
	\end{claim}
	\begin{proof}
		% Let $m_{2}^{-} $ and $m_{2}^{+} $ be the number of negative and
		% positive edges in $G_2$, respectively.
		%
		By construction $m_{2}^{+} \leq n_{1}^{2}  $ and $m_{2}^{-} \geq
			\lambda n_{1}^{2} $.
		Thus

		\begin{align}
			\eta(C) = \frac{m_{2}^{-} }{m_{2}^{-} +
				m_{2}^{+} } \geq \frac{\lambda n_{1} ^{2} }{\lambda n_{1}^{2}
				+ n_{1}^{2}  } = \frac{\lambda }{\lambda + 1} =
			\frac{ \frac{\alpha }{1 - \alpha }  }{ \frac{\alpha }{1 - \alpha }
				+ 1 } \geq \alpha
		\end{align}
	\end{proof}

	So content C is controversial. This reduces the Densest Echo Chamber Problem on $G_2$ to the maximization of

	\begin{equation}
		\label{eq:score-densest}
		\psi(U) = \sum^{}_{T \in S_{C}(U) } \frac{| T[U] |}{|U|}
	\end{equation}

	\begin{claim}
		\label{th:opt-equality-densest}
		\begin{equation}
			OPT(D-ECP) = OPT(MIS)
		\end{equation}
	\end{claim}

	\begin{proof}
		Let $I \subseteq V_{1} $ be an independent set of $G_1$ of size $n_{I}
			\coloneqq |I| > 1$. Consider the associated solution in $G_2$ in
		which $U = I \cup \{v_{r} \}$.

		By construction it will contain
		\begin{itemize}
			\item $2 \cdot n_{I} $ positive edges between $v_{r} $ and $v_{i} \in I$
			\item $n_{I}(n_{I}  -1)$
			      positive edges between vertices $v_{i} \in I$

		\end{itemize}
		$|I|$ positive
		thus $T$ will not be controversial and also

		\begin{equation}
			\label{eq:score-densest-mip}
			\psi(U) = \frac{|T[U]|}{|U|}  = \frac{2n_{I}  +
				\cdot n_{I}(n_{I}  -1) }{n_{I} + 1} = \frac{n_{I}^{2} +
				n_{I}}{n_{I} + 1} = n_{I}
		\end{equation}

		Consequently

		\begin{equation}
			OPT(D-ECP) \geq \psi(U) = |I| \implies OPT(D-ECP) \geq OPT(MIS)
		\end{equation}

		Now let $S \subseteq V_2$ be a solution of the Densest Echo Chamber
		problem on $G_2$, and suppose $\psi(S) > 0$. It is easy to see that
		$v_{r} \in S$ and that $v_{x} \not\in S $. Let $J \coloneqq S \setminus
			\{v_r\}$.

		Suppose that $2$ vertices $v_{i} $, $v_{j} \in J$ are linked in $G_1$.
		By construction there are at least $\lambda (n_1 + 1)^{2} $ negative edges in
		$T[S]$, thus

		\begin{equation}
			\eta(T[S]) \geq \frac{\lambda (n_1+1)^2}{\lambda (n_1+1)^2 + n(n+1)} \geq
			\frac{\lambda (n_1+1)^{2} }{\lambda (n_1+1)^2 + (n_1+1)^2} = \frac{\lambda }{\lambda +
				1} \geq \alpha
		\end{equation}

		This means that $T[S]$ is controversial $\implies \psi(S) = 0
			\implies contradiction$. Consequently $J$
		contains vertices which are independent in $G_1$. Therefore $T[S]$ contains
		only positive edges. As shown previously in
		\autoref{eq:score-densest-mip}

		\begin{equation}
			\psi(S) = \frac{|T[S]|}{|S|} = |J|
		\end{equation}

		Thus

		\begin{equation}
			OPT(MIS) \geq |J| \implies OPT(MIS) \geq OPT(D-ECP)
		\end{equation}

		So the optimal value of the constructed instance of Densest Echo Chamber Problem
		exactly equals that of the \textsc{Maximum Independent Set} instance, so it
		has an hardness factor at least as large as that of MIS.
	\end{proof}

	This concludes the proof of \autoref{th:approximability-densest}.
\end{proof}

\end{document}


\chapter{Introduction}
Fitting is the process of optimising the parameters of a 3DMM when a 2D image is given. There are different approaches on how to do that. Some use a mask with labels for each pixel of the image that determine whether or not the pixel represents a part of the imaged face. An additional difficulty is partial occlusion of the facial region, which must be excluded from the mask too. To achieve satisfying fits, the segmentation has to cut out the face as accurately as possible. There are different approaches to algorithmically generate such a mask. Due to the variety and diversity of such occlusions, this is not a simple problem. In this thesis, the quality of such segmentations by machine-learning-algorithms is measured. We compare the neural network of Nirkin er al \cite{nirkin2018_faceswap} and the existing model-based top-down approach by Egger et al \cite{egger_paper}.

\section{Artificial Neural Networks}
The idea of artificial neural networks was somehow influenced by biology. These networks consist of a variety of neurons which are grouped in layers. The way each neuron works is very simple. It takes multiple inputs of varying strength from other neurons, sums them up, puts the sum into a non-linear function (e.g. Maxout, Sigmoid, ReLu) [\Cref{fig:chap1:ActivFunctions}], and decides depending on the output if it should send a stimulus itself and if so, in which strength. Each layer is somehow connected to the next layer. Some layers are fully connected (each neuron of a layer is connected to every other neuron in the next layer) while others are convolutional. Convolutional means that a neuron only gets an input of spatially close neighbours in the previous layer. There are many different architectures which mainly differ in the number of layers, number of neurons per layer, and the interconnectivity of the neurons. A classical convolutional neural network (CNN) is depicted in [Figure \ref{fig:chap1:classicalCNN}].

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/chap1/ActivFunctions.png}
	\caption[Caption for LOF]{Six often used activation functions\footnotemark. The most commonly used activation function is ReLU because of its simplicity. The disadvantage of this function is if one input has a negative sign, then all following neurons output a signal of 0. An improvement of ReLU is Leaky ReLU.}
	\label{fig:chap1:ActivFunctions}
\end{figure}

\footnotetext{Jadon S. (2015 March). Introduction to Different Activation Functions for Deep Learning. Retrieved July 19, 2018 from \textit{https://medium.com/@shrutijadon10104776/survey-on-activation-functions-for-deep-learning-9689331ba092}}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/chap1/classicalCNN.JPG}
	\caption{An example of a classical convolutional neural network (CNN). After a certain number of convolutions, a pooling layer extracts the most important information of the image and writes it into the next layer. That is why the picture is getting smaller and smaller until just a vector is left.}
	\label{fig:chap1:classicalCNN}
\end{figure}

Already in 1943, Warren McCulloch and Walter Pitts \cite{mcculloch} showed that even simple networks of this kind can simulate every possible logical formula. For this, they used a neuron model which consisted of simple logic gates and could only process binary input and output signals.

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/chap1/bio_vs_arti_neuron.png}
	\caption[Caption for LOF]{The left-hand side of the image\footnotemark\ shows a biological neuron. It is a nerve cell which occurs in almost every animal. On the right-hand side, an artificial neuron is depicted. It sums up the stimuli of the previous neurons, applies an activation function to the sum, and forwards the output.}
	\label{fig:test1}
\end{figure}

\footnotetext{Willems K. (2017 May, 2nd). Keras Tutorial: Deep Learning in Python. Retrieved July 10, 2018 from \textit{https://www.datacamp.com/community/tutorials/deep-learning-python}}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/chap1/JLongEtAl.png}
	\caption{The transformation from a CNN into an FCN. This is a schematic diagram of Long et al \cite{jlong}, which fine-tuned the network used in this work. They call this process "convolutionalization". They turn the fully connected layers into convolution layers which produces an efficient machine for end-to-end dense learning. }
	\label{fig:chap1:JLongEtAl}
\end{figure}

\section{The Fully Convolutional Network Used}
\label{sec:theFCN}
For this thesis, a pretrained fully convolutional network from \cite{nirkin2018_faceswap} is used. A fully convolutional network (often called: FCN) is basically a CNN but with a modified architecture. An FCN does not have the fully connected layers which are usually found at the end of an CNN [compare \Cref{fig:chap1:classicalCNN} and \Cref{fig:chap1:JLongEtAl}]. These layers would enable the network to make decisions based on global information. A CNN for example can be used for classification. Nevertheless, for image analysis we want local information of the input image (we do not want to know if there is a face in the image, but where the face is in the image). Therefore, a FCN uses only convolutional and pooling layers. In the whole fully convolutional network only the following structure is repeated: One or more convolution layers and a pooling layer which downsamples the picture. This constellation is recurring several times.\\
\\
The assembly of the network used for this thesis follows the FCN-8s-VGG architecture with extensions of Long et al \cite{jlong} [\Cref{fig:chap1:JLongEtAl}]. The first part 'FCN' stands for 'fully convolutional network', '8s' means that the result gets upsampled eight times (because of the pooling layers), and 'VGG' means that the popular 16-layer network by Oxford's visual geometry group \cite{ksimonyan} is used [\Cref{fig:chap1:VGGnet}]. The original task of the network was to find the name of an object in an input image. The network could distinguish between 1000 different objects. Each cell in the final vector (1*1000 in size) was a boolean variable for one specific item. A schematic representation of this architecture can be found in [\Cref{fig:chap1:classicalCNN}].\\
\\
For our experiments a pretrained fully convolutional network (FCN) of \cite{nirkin2018_faceswap} is used. It was shown that even with a widespread network, good segmentation can be made and that the network does not have to be specially tailored for the future purpose. However, the network must have been trained with a large enough data set. Nirkin et al used the FCN for intra- and inter-subject face swapping on the Labeled Faces in the Wild (LFW) dataset and showed that intra-subject swapped faces remain as recognisable as before the swap and that in the inter-subject version better face swapping  leads to less perceptibility.\\
\\
Nirkin et al used a semi-supervised approach to produce training data in order to train the FCN. To make large quantities of them, 2'043 face videos of the IARPA Janus CS2 dataset of Klare et al \cite{IARPAJanus} were used. To avoid searching for the face in every frame of the video, they used motion queues which tracked the face given an initial segmentation based on the approach of \cite{grundmann} which enriched their training set to 9'818 images. To enlarge the collection of images, they rendered 3D Shapes of various objects (e.g. sunglasses, hands) into existing images. Each occlusion adds 9'500 images to their training set.\\

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/fcn_1.png}
	\caption{In the lower-left corner, the 16 layers of the well known VGGnet are shown. In the top-right, you can see the meaning of the '8s' term of the FCN-name. It means that the resulting image has to be 8x upsampled, to get a replica of it which is equal in size to the input image.}
	\label{fig:chap1:VGGnet}
\end{figure}

\section{Occlusion aware 3DMM}

Egger et al propose a fully automated, probabilistic and occlusion-aware 3D morphable face model adaptation framework \cite{egger_paper}. These methods use an iterative approach to label each pixel whether it belongs to the face or to the background. This approach can handle multiple labels and differentiate between multiple occlusion types, for example, face specific ones (eg. beards) and background. For updating the z-labels they use an algorithm which classifies a pixel based on the probabilities for each possible label, but for our experiments, we limited ourselves to two. We only need to distinguish between the face (including skin and beard) and the background.\\

\pagebreak

The algorithm does two things at the same time. In addition to creating a face mask (segmentation), it estimates the parameters of the popular Basel face Model to reconstruct the given face. It is an EM-algorithm-like method to solve two problems simultaneously. In the E-steps Egger et al update the z-labels and in the M-steps they update the face model  parameters based on the current estimate of the z-labels. For face model adaptation they apply a stochastic sampling strategy based on the Metropolis–Hastings algorithm (Markov Chain Monte Carlo). The likelihood of each pixel is split up into a background model ($X_{BG}$) and a foreground model ($X_{FG}$).
\[
\underbrace{p(\Theta |I)=p(I| \Theta)*p(\Theta)}_{\mathclap{\text{\parbox{4.5cm}{posterior probability of face model parameters $\Theta$ given an image $I$}}}}\quad\quad\text{ with: }p(I|\Theta)=\prod_{X \in \text{pixels}}p(X_{BG}|\Theta)^{z-1}*p(X_{FG}|\Theta)^z
\]
Conventional approaches for occlusion-segmentation often fail on important parts of the face such as the eyes, eyebrows or the oral region due to their strong variability in colour and shape. The segmentation of Egger et al has difficulties with these aspects too as Figure \ref{fig:iterations} shows. The algorithm starts with an initial guess and then alternating updates the parameters $\Theta$ and the z-labels. From the updated parameter set (M-Step) the algorithm renews the z-labels (E-Step) and vice versa (see Figure \ref{fig:EGGER's_method}).\\

\begin{figure}[H]
	\begin{center}
		\newcolumntype{C}{>{\centering\arraybackslash} m{2.4cm} }  %# New column type
		\begin{tabular}{SC|SC|SC|SC|SC}
			target image & 0 iterations & 10 iterations & 20 iterations & FCN\\ \hline
			\subfloat{\includegraphics[width=0.16\textwidth]{Figures/chap1/angie_original.png}} &
			\subfloat{\includegraphics[width=0.16\textwidth]{Figures/chap1/EGGER_Segmentation_Nr_0.png}} &
			\subfloat{\includegraphics[width=0.16\textwidth]{Figures/chap1/EGGER_Segmentation_Nr_9.png}} &
			\subfloat{\includegraphics[width=0.16\textwidth]{Figures/chap1/EGGER_Segmentation_Nr_19.png}}&
			\subfloat{\includegraphics[width=0.16\textwidth]{Figures/chap1/angie_FCN.png}} \\
		\end{tabular}
	\end{center}
	\caption{This picture shows the development of the labels after 0, 10 and 20 iterations. Noticeable in this sample image are not only the eyes as mentioned before but also the shadow of the nose, which is first segmented as a background. Only after a certain number of iterations these errors are partially recognised and provided with the correct label.}
	\label{fig:iterations}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\linewidth]{Figures/chap1/EGGER's_method.png}
	\caption[Caption for LOF]{\footnotemark \  Algorithm overview: As input, the algorithm takes a target image and fiducial points. The external Clandmark library for automated fiducial point detection from still images [U\v ri\v c\' a\v r et al (2015) \cite{Uricar}] is used. The algorithm starts with an initial face model fit of our average face with a pose estimation. Then, a robust illumination estimation for initialisation of the segmentation labels z and the illumination settings is performed. For this task, a random sample consensus (RANSAC) algorithm is used. RANSAC methods estimate parameters of a mathematical model from a set of observed data that contains outliers. Then the face model and the segmentation are simultaneously adapted to the target image I. The result is a set of face model parameters $\Theta$ and a segmentation into face and non-face regions. The presented target image is from the LFW database (Huang et al (2007))}
	\label{fig:EGGER's_method}
\end{figure}

\footnotetext{Figure \ref{fig:EGGER's_method} is copied from Fig.4 of the "Occlusion-aware  3D  Morphable  Models and Illumination Prior for Face Image Analysis" paper of Egger et al. \cite{egger_paper}}

% Soll ich auch die 'stable illumination estimation' in ein chapter machen? Wenn ja, was ist das 'consensus set' ?

% The source for this table was this post: https://stackoverflow.com/questions/2771856/centering-text-horizontally-and-vertically-in-latex
% To add padding for the cell contents: https://tex.stackexchange.com/questions/31672/column-and-row-padding-in-tables
An approach using convolutional neural networks to segment occluded faces has already been described by \cite{SaitoEtAl}. The big difference to our approach is that multiple frames are needed for the final segmentation. Another approach of Morel-Forster et al \cite{MorelForster} uses random forests to detect facial-occlusions by hair. These are integrated into the fitting process in order to model uncertainty. Unlike us, Morel-Forster et al integrate the occlusion in the face likelihood of the evaluator.


\section{A Worklist Algorithm for Polymorphic Subtyping}\label{algorithmic_subtyping}

This section presents our algorithm for polymorphic
subtyping. A novel aspect of our algorithm is the use of worklist
judgments: a form of judgment that facilitates the propagation
of information.

%-------------------------------------------------------------------------------
\subsection{Syntax and Well-Formedness of the Algorithmic System}
Figure~\ref{fig:ITP:alg:syntax} shows the
syntax and the well-formedness judgment.

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\paragraph{Existential Variables}
In order to solve the unknown types $\tau$, the algorithmic system extends the
declarative syntax of types with \emph{existential variables} $\al$.  They
behave like unification variables, but are not globally defined. Instead, the
ordered \emph{algorithmic context}, inspired by \citet{dunfield2013complete},
defines their scope. Thus
the type $\tau$ represented by the corresponding existential variable is
always bound in the corresponding declarative context $\Psi$.

%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\paragraph{Worklist judgments} The form of our algorithmic judgments is
non-standard.
%tracks the (partial) solutions of existential variables
%in the algorithmic context; they denote a delayed substitution that is
%incrementally applied to outstanding work as it is encoutered.
%Instead of reifying the substitution,
Our algorithm keeps track of an explicit list of
outstanding work: the list $\Omega$ of (reified) \emph{algorithmic judgments}
of the form $A \leq B$,
to which a substitution can be applied once and for all to propagate the solution
of an existential variable.

\begin{figure}[t]
\[
\begin{array}{l@{\qquad}lcl}
\text{Type variables} & a, b\\
\text{Existential variables} & \al, \bt\\[3mm]
\text{Algorithmic types} &A, B, C &::=&\quad 1 \mid a \mid \al \mid \forall a. A \mid A\to B\\
\text{Algorithmic context}&\Gamma &::=&\quad \cdot \mid \Gamma, a \mid \Gamma, \al\\
\text{Algorithmic judgments}&\exps &::=&\quad \cdot \mid \jcons{A \le B}{\exps}
\end{array}
\]
\centering \framebox{$\Gamma \vdash A$}
\begin{gather*}
\inferrule*[right=$\mathtt{{wf_a}unit}$]
  {~}
  {\Gamma \vdash 1}
\qquad
\inferrule*[right=$\mathtt{{wf_a}var}$]
  {a\in\Gamma}
  {\Gamma\vdash a}
\qquad
\inferrule*[right=$\mathtt{{wf_a}exvar}$]
  {\al\in\Gamma}
  {\Gamma\vdash \al} \\
\inferrule*[right=$\mathtt{{wf_a}{\to}}$]
  {\Gamma\vdash A \\ \Gamma\vdash B}
  {\Gamma\vdash A\to B}
\qquad
\inferrule*[right=$\mathtt{{wf_a}\forall}$]
  {\Gamma, a\vdash A}
  {\Gamma\vdash \forall a. A}
\end{gather*}
\caption{Syntax and Well-Formedness judgment for the Algorithmic System.}\label{fig:ITP:alg:syntax}
\end{figure}

\begin{comment}
\begin{figure}[t]
\centering \framebox{$\Gamma \vdash A$}
\begin{gather*}
\inferrule*[right=$\mathtt{{wf_a}unit}$]
  {~}
  {\Gamma \vdash 1}
\qquad
\inferrule*[right=$\mathtt{{wf_a}var}$]
  {a\in\Gamma}
  {\Gamma\vdash a}
\qquad
\inferrule*[right=$\mathtt{{wf_a}exvar}$]
  {\al\in\Gamma}
  {\Gamma\vdash \al} \\
\inferrule*[right=$\mathtt{{wf_a}{\to}}$]
  {\Gamma\vdash A \\ \Gamma\vdash B}
  {\Gamma\vdash A\to B}
\qquad
\inferrule*[right=$\mathtt{{wf_a}\forall}$]
  {\Gamma, a\vdash A}
  {\Gamma\vdash \forall a. A}
\end{gather*}
\caption{Well-Formedness judgment of the Algorithmic System}\label{fig:alg:wf}
\end{figure}
\end{comment}

\paragraph{Hole Notation}
To facilitate context manipulation, we use the syntax $\Gamma[\Gamma_M]$ to
denote a context of the form $\Gamma_L, \Gamma_M, \Gamma_R$ where $\Gamma$ is
the context $\Gamma_L, \bullet, \Gamma_R$ with a hole ($\bullet$).
Hole notations with the same name implicitly share the same $\Gamma_L$ and $\Gamma_R$. A multi-hole notation like $\Gamma[\al][\bt]$ means $\Gamma_1,\al,\Gamma_2,\bt,\Gamma_3$.

%-------------------------------------------------------------------------------
\subsection{Algorithmic Subtyping}

The algorithmic subtyping judgment, defined in Figure~\ref{fig:ITP:alg}, has the form $\Gamma\vdash\exps$, where
$\exps$ collects multiple subtyping judgments $A\le B$.
% \bruno{Text comparing to Dunfield. Maybe mention in RW instead?:
% In contrast to the
% original formulation---which features 3 interdepenent judgments---our
% algorithmic rules are all part of the same judgment. This is better for
% formalization in proof assistants and avoids mutual dependencies.
% }
The algorithm treats $\exps$ as a worklist. In every step
it takes one task from the worklist for processing, possibly
pushes some new tasks on the worklist, and repeats this
process until the list is empty. This last and single base case
is handled by Rule~$\mathtt{a\_nil}$.
The remaining rules all deal with the first task in the worklist.
Logically we can discern 3 groups of rules.

\begin{figure}[t]
\centering \framebox{$\Gamma \vdash \exps$}
\begin{gather*}
\inferrule*[right=$\mathtt{{\le_a}nil}$]
  {~}
  {\Gamma \vdash \cdot}
\\ \\
\inferrule*[right=$\mathtt{{\le_a}unit}$]
  {\Gamma \vdash \exps}
  {\Gamma \vdash \jcons{1\le 1}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}var}$]
  {a\in\Gamma \\ \Gamma \vdash \exps}
  {\Gamma \vdash \jcons{a\le a}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}exvar}$]
  {\al\in\Gamma \\ \Gamma \vdash \exps}
  {\Gamma \vdash \jcons{\al\le \al}{\exps}}
\\
\inferrule*[right=$\mathtt{{\le_a}{\to}}$]
  {\Gamma \vdash \jcons{B_1\le A_1}{\jcons{A_2\le B_2}{\exps}}}
  {\Gamma \vdash \jcons{A_1\to A_2\le B_1\to B_2}{\exps}}
\\
\inferrule*[right=$\mathtt{{\le_a}\forall L}$]
  {\al \text{ fresh} \\ \Gamma,\al \vdash \jcons{[\al/a]A\le B}{\exps}}
  {\Gamma\vdash \jcons{\forall a. A\le B}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}\forall R}$]
  {b \text{ fresh} \\ \Gamma,b \vdash \jcons{A\le B}{\exps}}
  {\Gamma\vdash \jcons{A\le \forall b. B}{\exps}}
\\
\\
\inferrule*[right=$\mathtt{{\le_a}instL}$]
  {\al\notin \mathit{FV}(A)\cup FV(B)\quad
  	\Gamma[\al[1], \al[2]]\vdash \jcons{\al[1]\to \al[2]\le A\to B}{ [\al[1]\to \al[2]/\al]\exps}}
  {\Gamma[\al] \vdash \jcons{\al\le A\to B}{\exps}}
\\
\inferrule*[right=$\mathtt{{\le_a}instR}$]
  {\al\notin FV(A)\cup FV(B)\quad
	\Gamma[\al[1], \al[2]]\vdash \jcons{A\to B\le \al[1]\to \al[2]}{ [\al[1]\to \al[2]/\al]\exps}}
  {\Gamma[\al] \vdash \jcons{A\to B\le \al}{\exps}}
\\
\\
\inferrule*[right=$\mathtt{{\le_a}solve\_ex}$]
  {\Gamma[\al][]\vdash [\al/\bt]\exps}
  {\Gamma[\al][\bt]\vdash \jcons{\al\le \bt}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}solve\_ex'}$]
  {\Gamma[\al][]\vdash [\al/\bt]\exps}
  {\Gamma[\al][\bt]\vdash \jcons{\bt\le \al}{\exps}}
\\
\inferrule*[right=$\mathtt{{\le_a}solve\_var}$]
  {\Gamma[a][]\vdash [a/\bt]\exps}
  {\Gamma[a][\bt]\vdash \jcons{a\le \bt}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}solve\_var'}$]
  {\Gamma[a][]\vdash [a/\bt]\exps}
  {\Gamma[a][\bt]\vdash \jcons{\bt\le a}{\exps}}
\\
\inferrule*[right=$\mathtt{{\le_a}solve\_unit}$]
  {\Gamma[]\vdash [1/\al]\exps}
  {\Gamma[\al]\vdash \jcons{\al\le 1}{\exps}}
\qquad
\inferrule*[right=$\mathtt{{\le_a}solve\_unit'}$]
  {\Gamma[]\vdash [1/\al]\exps}
  {\Gamma[\al]\vdash \jcons{1\le \al}{\exps}}
\end{gather*}
\caption{Algorithmic Subtyping}
\label{fig:ITP:alg}
\end{figure}

Firstly, we have five rules that are similar to those in the declarative
system, mostly just adapted to the worklist style. For instance, Rule
$\mathtt{{\le_a}{\to}}$ consumes one judgment and pushes two to the
worklist.  A notable difference with the declarative Rule $\mathtt{{\le}\forall
L}$ is that Rule $\mathtt{{\le_a}\forall L}$ requires no guessing of a type $\tau$ to instantiate
the polymorphic type $\forall a. A$, but instead
introduces an existential variable $\al$ to the context and to $A$. In
accordance with the declarative system, where
the monotype $\tau$ should be bound in the context $\Psi$, here $\al$ should only
be solved to a monotype bound in $\Gamma$. More generally, for any algorithmic context $\Gamma[\al]$, the algorithmic variable $\al$
can only be solved to a monotype that is well-formed with respect to $\Gamma_L$.

Secondly, Rules $\mathtt{{\le_a}instL}$ and $\mathtt{{\le_a}instR}$ partially
instantiate existential types $\al$, to function types. The domain and range
of the new function type are undetermined: they are set to two
fresh existential variables $\al[1]$ and $\al[2]$. To make sure that
$\al[1] \to \al[2]$ has the same scope as $\al$, the new variables
$\al[1]$ and $\al[2]$ are inserted in the same position in the context
where the old variable $\al$ was. To propagate the instantiation to the remainder
of the worklist, $\al$ is substituted for $\al[1] \to \al[2]$ in $\Omega$.
The \emph{occurs-check} side-condition is necessary to prevent a diverging
infinite instantiation. For example
$1 \to \al \le \al$ would diverge with no such check.
Note that the algorithm does not choose to instantiate $\al$ directly with
$A \to B$, since the type is not guaranteed to be a monotype,
and such instantiation will be inconsistent with our predicative declarative system.

Thirdly, in the remaining six rules an existential variable can be immediately
solved. Each of the six similar rules removes an existential variable from the
context,  performs a substitution on the remainder of the worklist and
continues.

The algorithm on judgment list is designed to share the context across all judgments.
However, the declarative system does not share a single context in its derivation.
This gap is filled by strengthening and weakening lemmas of both systems,
where most of them are straightforward to prove,
except for the strengthening lemma of the declarative system, which is a little trickier.

\begin{figure}[t]
$$
	\inferrule*[Right=$\mathtt{{\le_a}\forall L}$]
	{\inferrule*[Right=$\mathtt{{\le_a}{\to}}$]
		{\inferrule*[Right=$\mathtt{{\le_a}\forall L}$]
			{\inferrule*[Right=$\mathtt{{\le_a}instR}$]
				{\inferrule*[Right=$\mathtt{{\le_a}{\to}}$]
					{\inferrule*[Right=$\mathtt{{\le_a}solve\_ex}$]
						{\inferrule*[Right=$\mathtt{{\le_a}solve\_ex}$]
							{\inferrule*[Right=$\mathtt{{\le_a}unit}$]
								{\inferrule*[Right=$\mathtt{a\_nil}$]
									{~}
									{\al[1] \vdash \cdot}
								}
								{\al[1] \vdash \jcons{1 \le 1}{\cdot}}
							}
							{\al[1], \al[2] \vdash \jcons{\al[1] \le \al[2]}{\jcons{1 \le 1}{\cdot}}}
						}
						{\al[1], \al[2], \bt \vdash \jcons{\al[1] \le \bt}{\jcons{\bt \le \al[2]}{\jcons{1 \le 1}{\cdot}}}}
					}
					{\al[1], \al[2], \bt \vdash \jcons{\bt \to \bt \le \al[1] \to \al[2]}{\jcons{1 \le 1}{\cdot}}}
				}
				{\al, \bt \vdash \jcons{\bt \to \bt \le \al}{\jcons{1 \le 1}{\cdot}}}
			}
			{\al \vdash \jcons{\forall a.\ a \to a \le \al}{\jcons{1 \le 1}{\cdot}}}
		}
		{\al \vdash \jcons{\al \to 1 \le (\forall a.\ a \to a) \to 1}{\cdot}}
	}
	{\cdot \vdash \jcons{\forall a.\ a\to 1\le (\forall a.\ a\to a)\to 1}{\cdot }}
$$
  %\label{fig:alg_sample}
\caption{A Success Derivation for the Algorithmic Subtyping Relation}
\label{fig:alg_sample_success}
\end{figure}

\begin{figure}[t]
	$$
	\inferrule*[Right=$\mathtt{{\le_a}\forall L}$]
	{\inferrule*[Right=$\mathtt{{\le_a}{\to}}$]
		{\inferrule*[Right=$\mathtt{{\le_a}unit}$]
			{\inferrule*[Right=$\mathtt{{\le_a}\forall R}$]
				{\inferrule*[Right=$\mathtt{?}$]
					{stuck
					}
					{\al, b \vdash \jcons{\al \le b}{\cdot}}
				}
				{\al \vdash {\jcons{\al \le \forall b.\ b}{\cdot}}}
			}
			{\al \vdash \jcons{1\le 1}{\jcons{\al \le \forall b.\ b}{\cdot}}}
		}
		{\al \vdash \jcons{1 \to \al \le 1\to \forall b.\ b}{\cdot}}
	}
	{\cdot\vdash \jcons{\forall a.\ 1\to a \le 1\to \forall b.\ b}{\cdot }}
	$$
\caption{A Failing Derivation for the Algorithmic Subtyping Relation}
\label{fig:alg_sample_fail}
\end{figure}

\paragraph{Example}
We illustrate the subtyping rules through a sample derivation in
Figure~\ref{fig:alg_sample_success},
which shows that that $\forall a.\ a\to 1\le (\forall a.\ a\to
a)\to 1$. Thus the derivation starts with an empty context and a
judgment list with only one element.

% \begin{center}
% 	\begin{tabular}{|c|c|c|c|}\hline
% 		\# & Context & Worklist & Rule\\\hline
% 		1&$\cdot$ & $\forall x.\ x\to 1\le (\forall x.\ x\to x)\to 1$ & $\mathtt{{\le_a}\forall L}$\\\hline
% 		2&$\al$ & $\al\to 1\le (\forall x.\ x\to x)\to 1$ & $\mathtt{{\le_a}{\to}}$\\\hline
% 		3&$\al$ & $\forall x.\ x\to x\le \al : 1\le 1$ & $\mathtt{{\le_a}\forall L}$\\\hline
% 		4&$\al,\bt$ & $\bt\to \bt\le \al : 1\le 1$ & $\mathtt{instR}$\\\hline
% 		5&$\al[1],\al[2], \bt$ & $\bt\to \bt\le \al[1]\to \al[2] : 1\le 1$ & $\mathtt{{\le_a}{\to}}$\\\hline
% 		6&$\al[1],\al[2], \bt$ & $\al[1]\le \bt : \bt\le \al[2] : 1\le 1$ & $\mathtt{{\le_a}solve\_ex (\bt\leftarrow \al[1])}$\\\hline
% 		7&$\al[1],\al[2]$ & $\al[1]\le \al[2] : 1\le 1$ & $\mathtt{{\le_a}solve\_ex (\al[2]\leftarrow \al[1])}$\\\hline
% 		8&$\al[1]$ & $1\le 1$ & $\mathtt{{\le_a}unit}$\\\hline
% 	\end{tabular}
% \end{center}

In step 1, we have only one judgment, and that one has a top-level $\forall$ on
the left hand side. So the only choice is Rule $\mathtt{{\le_a}\forall L}$, which
opens the universally quantified type with an unknown existential variable
$\al$. Variable $\al$ will be solved later to some monotype that is well-formed
within the context before $\al$. That is, the empty context $\cdot$ in this
case.
In step 2, Rule $\mathtt{{\le_a}{\to}}$ is applied to the worklist,
splitting the first judgment into two.
Step 3 is similar to step 1, where the left-hand-side $\forall$ of the first
judgment is opened according to Rule $\mathtt{{\le_a}\forall L}$ with a fresh
existential variable.
In step 4, the first judgment has an arrow on the left hand side, but the
right-hand-side type is an existential variable. It is obvious
that $\al$ should be solved to a monotype of the form
$\sigma \to \tau$. Rule $\mathtt{instR}$ implements this, but avoids
guessing $\sigma$ and $\tau$ by ``splitting'' $\al$ into two existential
variables, $\al[1]$ and $\al[2]$, which will be solved to some $\sigma$ and
$\tau$ later.
Step 5 applies Rule $\mathtt{{\le_a}{\to}}$ again. Notice that after the
split, $\bt$ appears in two judgments. When the first $\bt$ is solved
during any step of the derivation, the next $\bt$ will be substituted by that
solution.  This propagation mechanism ensures the consistent solution of the
variables, while keeping the context as simple as possible.
Steps 6 and 7 solve existential variables. The existential
variable that is right-most in the context is always solved in terms of the other. Therefore in step 6,
$\bt$ is solved in terms of $\al[1]$, and in step 7, $\al[2]$ is solved in terms of $\al[1]$.
Additionally, in step 6, when $\bt$ is solved, the substitution $[\al[1] /
\bt]$ is propagated to the rest of the judgment list, and thus the second
judgment becomes $\al[1]\le\al[2]$.
Steps 8 and 9 trivially finish the derivation. Notice that $\al[1]$ is not
instantiated at the end. This means that any well-scoped instantiation is fine.

\paragraph{A Failing Derivation} We illustrate the role of ordered contexts through another example: $\forall a.\ 1\to a \le 1\to \forall b.\ b$. From the declarative perspective, $a$ should be instantiated to some $\tau$ first, then $b$ is introduced to the context, so that $b\notin FV(\tau)$. As a result, we cannot find $\tau$ such that $\tau \le b$. Figure~\ref{fig:alg_sample_fail} shows the algorithmic derivation, which also fails due to the scoping---$\al$ is introduced earlier than $b$, thus it cannot be solved to $b$.