\chapter{The Interaction of Selection, Mutation, and Migration.}
\label{Chapter:Sel_Mut_Mig}

Genetic variation is the raw fuel of evolution. Without variation,
natural selection would have nothing to act on to shape adaptive traits.
However, variation can be deleterious.

Mutation,
broadly defined, is the ultimate source of all genetic
variation and is constantly introducing new variation into all
populations. However, mutation is random and so mutations
that affect function are often damaging. Thus mutation will, in the absence of
sufficiently strong selection, degrade pre-existing adaptations and
undo the work of selection that has built up functional regions of DNA over time.

%They can sometimes spread between very diverged clades of species,
%indeed sometimes different domains of life, thanks to horizontal gene
%transfer.

Migration, the movement of individuals into a population,
can also increase variation to the population as the individuals
bring new alleles in from surrounding populations. Thus migration can
be an important source of adaptive alleles, aiding their spread
amongst populations within a species. Adaptive alleles can even spread between species if low levels of interbreeding occur.
However, again, just like mutation, migration can disrupt
adaptations. When populations are locally adapted migration amongst
populations can introduce maladaptive alleles into well adapted
populations. If this migration pressure is sufficiently strong, it can lead to the collapse of local
adaptations, or even the collapse of species.

In this chapter we'll study some of the interplay between selection,
migration, and mutation.

\subsection{Mutation--Selection Balance}
%</source-file>
Mutation is constantly introducing new alleles into the
population. Therefore, variation can be maintained within a
population not only if selection is balancing (e.g.\ through heterozygote advantage or fluctuating selection over time, as we have seen in the previous section), but also due to a balance between
mutation introducing deleterious alleles and selection acting to purge
these alleles from the population \citep{haldane1927mathematical,Haldane:37}. To study mutation-selection balance, we return to the model of directional selection, where allele $A_1$ is advantageous, i.e.
\begin{center}
\begin{tabular}{lccc}
genotype & $A_1A_1$ & $A_1A_2$ & $A_2A_2$ \\
absolute fitness & $W_{11}$ & $ \geq W_{12} \geq$ & $W_{22}$ \\
relative fitness & $w_{11}=1$ & $w_{12}=1-sh$ & $w_{22}=1-s$. \\
\end{tabular}\\
\end{center}
We'll begin by considering the case where allele $A_2$ is not
completely recessive ($h>0$), so that the heterozygotes suffer at least some disadvantage. We denote by $\mu = \mu_{1\rightarrow2}$ the  mutation rate per generation from $A_1$ to the deleterious allele $A_2$, and assume that there is no reverse mutation ($\mu_{2\rightarrow1} = 0$).  Let us assume that selection against $A_2$ is relatively strong compared to the mutation rate, so that it is justified to assume that $A_2$ is always rare, i.e.\ $q_t = 1-p_t \ll 1$. Compared to previous sections, for mathematical clarity, we also switch from following the frequency $p_t$ of $A_1$ to following the frequency $q_t$ of $A_2$. Of course, this is without loss of generality. The change in frequency of $A_2$ due to selection can be written as
\begin{equation}
	\Delta_S q_t = \frac{ \wbar_2 - \wbar_1}{\wbar} p_t q_t  \approx  -hs q_t.
	\label{eq:dirSelApprox}
\end{equation}
This approximation can be found by assuming that $q^2 \approx 0$, $p \approx 1$,
and that $\wbar \approx w_1$. \sa{All of these assumptions make sense if $q \ll 1$. From eqn.\ \eqref{eq:dirSelApprox} we see that selection acts to reduce the frequency of $A_2$ (as both $h$ and $s$ are positive), and it does so geometrically across the
generations. That is, if the initial frequency of $A_2$ is $q_0$, then its frequency at time $t$ is approximately
\begin{equation}
	q_t = q_0 (1 - hs)^t.
	\label{eq:dirSelExplApprox}
\end{equation}

We will now consider the change in frequency induced by mutation. Recalling that $\mu$ is
the mutation rate from $A_1$ to $A_2$ per generation, the frequency of $A_2$ after mutation is
\begin{equation}
	q^{\prime} =  \mu p_t + q_t = \mu(1 - q_t) + q_t.
\end{equation}
Assuming that $\mu \ll 1$ and that $q \ll 1$, the change in the
frequency of allele $A_2$ due to mutation ($\Delta_M q_t$) can be approximated by
\begin{equation}
	\Delta_M q_t = q^{\prime} - q_t =  \mu.
	\label{eq:mutApprox}
\end{equation}
Hence, when $A_2$ is rare and the mutation rate is low, mutation acts to linearly increase the frequency of the deleterious allele $A_2$.\\

If selection is to balance deleterious mutation, their combined effect over one generation has to be zero. Therefore, to find the mutation--selection equilibrium, we set
\begin{equation}
	\Delta_M q_t + \Delta_S q_t = 0,
\end{equation}
insert eqns. \eqref{eq:dirSelApprox} and \eqref{eq:mutApprox}, and solve for $q$ to obtain
\begin{equation}
	q_e = q_t = \frac{\mu}{hs}.
	\label{eqn:mut_sel_bal}
\end{equation}
We see that the frequency of the deleterious allele $A_2$ is balanced at a frequency equal to the mutation rate ($\mu$) divided by
the reduction in relative fitness in the heterozygote ($hs$).}\\

It is worth pointing out that the fitness of the $A_2A_2$ homozygote
has not entered this calculation, as $A_2$ is so rare that it is hardly ever found in the
homozygous state. Therefore, if $A_2$ has any deleterious effect
in a heterozygous state (i.e.\ if $h>0$), it is this effect that determines the
frequency at which $A_2$ is maintained in the population. Also, note that by
writing the total change in allele frequency as $\Delta_M q_t + \Delta_S q_t
$ we have implicitly assumed that we can ignore terms of order $\mu
\times s$. That is, we have assumed that mutation and selection are
both relatively weak. This assumption is valid under our prior assumption that both $\mu$ and $s$ are small.\\

If an allele is truly recessive (although few likely are), we have $h=0$, and
so eqn.\ \eqref{eqn:mut_sel_bal} is not valid. However, we can make an argument similar to the one above to show
that, for truly recessive alleles,
\begin{equation}
	q_e = \sqrt{\frac{\mu}{s}}. \label{eqn:recess_mut_sel_bal}
\end{equation}
\graham{Add figure illustrating the freq as a function of h and s}
\begin{marginfigure}
\begin{center}
  \includegraphics[width = \textwidth]{Journal_figs/single_locus_selection/mutation_selection_balance/Pink_Katydid.png}
\end{center}
\caption{Oblong-winged katydid. \BHLNC{Field book of insects (1918). Lutz,
  F.E. . Illustrations by Edna L. Beutenm\"uller. }{https://www.flickr.com/photos/biodivlibrary/6244366674/in/album-72157627768739853/}{MBLWHOI Library}} \label{fig:katydid}
\end{marginfigure}
\marginnote{The analysis of these pink katydids is
  a nice example of early Mendelian reasoning in a wonderfully
titled paper.\cite{Wheeler:07} }
\begin{question}{}
Oblong-winged katydids ({\it Amblycorypha oblongifolia}) are usually
% these are actually false katydid
green. However, some are bright pink, thanks to an erythrism
mutation .
This pink condition is thought to be due to
a dominant mutation (\href{https://blogs.scientificamerican.com/running-ponies/in-north-american-katydids-green-isne28099t-the-dominant-colour-pink-is/}{Crew, 2013}). Assume that roughly  %\citet{Crew:13}
one in ten thousand katydids is bright pink and that the mutation rate at the
gene underlying this condition is $10^{-5}$. What is the relative fitness of heterozygotes for the pink mutation?
\end{question}

\paragraph{The genetic load of deleterious alleles}
What effect do such deleterious mutations at mutation--selection balance have on the population? It is common to quantify the effect of deleterious alleles in terms of a reduction of the mean relative fitness of the population. For a single site at which a deleterious mutation is segregating at frequency $q_e = \mu/(hs)$, the population mean relative fitness is reduced to
\begin{equation}
	\wbar = 1- 2p_e q_e hs - q_e^2s \approx 1-2\mu.  \label{eqn:mut_load}
\end{equation}
Somewhat remarkably, the drop in mean fitness due to a site segregating
at mutation--selection balance is independent of the selection coefficient against the
heterozygote; it depends only on the mutation rate \citep{Haldane:37}. Intuitively this is because, given a fixed mutation rate, less deleterious alleles can rise to a higher equilibrium frequency, and thus contribute the same total load as more deleterious (rarer) alleles, but this load is spread across more individuals in the population. Note that this result applies only if the mutation is not totally recessive, i.e.\ if $h > 0$.

\begin{figure}
\begin{center}
  \includegraphics[width = \textwidth]{Journal_figs/single_locus_selection/LOF_variants/Neatherlands_LOF_variants.pdf}
\end{center}
\caption{{\bf Left)} The distribution of LOF alleles in 769 individuals from the
  Genome of the Netherlands project. Data from
  \citeauthor{francioli:14}. The average individual (red line) carries 144 LOF
  alleles. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/LOF_variants/Neatherlands_LOF_variants.R}} \label{fig:LOF}
\end{figure}

A fitness reduction of $2\mu$ is very small, given that the
mutation rate of a gene is usually on the order of $10^{-5}$. However, if there are many loci
segregating at mutation--selection balance, small fitness reductions can accumulate to a substantial so-called
genetic load, a major cause of variation in fitness-related traits
among individuals. To see how all of these loci contribute to variation in fitness
consider the fact that the human genome contains over twenty
thousand protein-coding genes, and many other functional regions, the vast majority
of which will be subject to purifying selection against mutations that
disrupt their function. In humans, most loss of function (LOF) variants, which severely
disrupt a protein-coding gene, are found at low frequencies but each human genome typically carries over
a hundred LOF variants \citep{macarthur:12,lek:16}. Not every LOF allele will be
deleterious; some could even be advantageous. However, the combined load of these
LOF alleles must on average lower our fitness, otherwise selection wouldn't be
removing them from the population. Each one of us
carries a unique set of these LOF alleles, usually in a heterozygous state. We differ slightly in how
many of these alleles we carry. For example, the left side of Figure \ref{fig:LOF} shows
the distribution of the number of LOF alleles carried by 769 individuals
of Dutch ancestry. The individuals who carry fewer of these LOF
alleles will on average likely have higher fitness than those individuals
with more. We don't yet know how much fitness variation this explains
across individuals, nor do we know how most of these LOF alleles manifest their fitness
consequences through disease and other mechanisms.  However, it's a
reasonable guess that this
variation in LOF alleles, presumably maintained by mutation-selection balance, is
a major source of variation in fitness.

%%% Deleted load calculation
%To see the effect on mean relative fitness of $L$
%unlinked  loci genome-wide, with mutation effects $\mu_1,\cdots,~\mu_L$, segregating at mutation-selection balance,
%we can assume a multiplicative model then
%\begin{equation}
%\wbar = \prod_{i=1}^L  (1-2\mu_i) \approx \exp \left( - \sum_{i=1}^L
%  2\mu_i \right) = \exp \left( - U\right)
 %\end{equation}
%where $U= \sum_{i=1}^L 2 \mu_i$, a genome-wide deleterious mutation
%rate.\sidenote{We move the two inside this rate to make it the
 % mutation rate per diploid genome.} Thus, under a multiplicative model, the
%mean fitness decrease depends only on the genome-wide mutation rate
%towards deleterious alleles.   \\

%How do these differences across individuals in number LOF mutations
%contribute to variation in fitness? If we are willing to assume that the fitness costs of deleterious alleles interact multiplicatively, we can make some
%progress. If an individual who carries one LOF mutation has a fitness
%$1-hs$,  then an individual who's heterozygote for two LOF mutations would have
%fitness $(1-hs)^2$, and an individual who is heterozygote for $L$ LOF
%alleles would have fitness  $(1-hs)^L$. The right-hand side of Figure
%\ref{fig:LOF} shows the predicted fitness of individuals carrying
%varying number of LOF alleles, relative to the mean
%fitness of the sample, using this multiplicative model.

%{\bf Right)}. The relative fitness of individuals carrying
 % these varying numbers  of LOF alleles, assuming multiplicative
 % selection and a selection coefficient of $sh=10^{-2}$ acting against
  %these alleles \citep{cassa2017}.

%\begin{question}

%\begin{tcolorbox}
%\begin{question}
%You are studying an outbred population of mice living in a farmer’s field. Mutations occur at a gene called nurseryrhyme that cause a totally recessive form of blindness. These blind mice do not survive to reproduce as the farmer’s wife cuts off their tail (and other bits) with a carving knife.
%Surveying the field for baby mice you find that 3 in ten thousand mice are blind.\\
%{\bf A} Assuming that the population mates at random, what is the mutation
% rate of blindness causing alleles?\\
%{\bf B} Following more careful study you now find that there is actually a $20
%\%$ reduction in the viability of heterozygotes for these
%mutations. What would you now estimate as the mutation rate for this
%gene?
%{\bf C)} Explain how and why your answers differ?
%\end{question}
%\end{tcolorbox}

%B) You look at family of outbred mice. You find that one of the mice in the family is blind, what is the probability that its 1st cousin is also blind?

%C) In another isolated field on the farm, there is a high rate of inbreeding among the mice. The farmer’s wife also carries out her cruel carving knife policy in this field as well. Do you expect to the underlying blindness mutations to be at a higher or low rate in this second field than the first field? Briefly explain your answer.

\subsection{Inbreeding depression}
All else being equal, eqn.\ \eqref{eqn:mut_sel_bal} suggests that mutations that have a smaller effect in the
heterozygote can segregate at higher frequency under mutation--selection balance. As a consequence, alleles that have
strongly deleterious effects in the homozygous state can still segregate at
low frequencies in the population, as long as they do not have too
strong a deleterious effect in heterozygotes. Thus, outbred populations may have many
alleles with recessive deleterious effects segregating within them.
\begin{question}{}
Assume that a deleterious allele has a relative fitness $0.99$ in
heterozygotes and a relative fitness $0.2$ when present in the
homozygote state. Assume that the
deleterious allele is at a frequency $10^{-3}$ at birth and the
genotype frequencies follow from HWE. Only considering the fitness effects of this locus, and measuring fitness relative to the most fit genotype, answer the following questions: \\
{\bf A)} What is the average fitness of an individual in the population? \\
 {\bf B)} What is the average fitness of the child of a full-sib
 mating? \\
\end{question}
\begin{figure}
\begin{center}
  \includegraphics[width = 0.75 \textwidth]{Journal_figs/single_locus_selection/Silene_inbreeding_Richards/Silene_inbreeding_depression.pdf}
\end{center}
\caption{Data showing inbreeding depression over different degrees of
  inbreeding in {\it S. latifolia}. Each point is the mean seed germination rates for different
  family crosses.  Data from \citeauthor{richards:00}. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/Silene_inbreeding_Richards/Silene_inbreeding_depression.R} } \label{fig:Silene_inbreeding}
\end{figure}
\begin{marginfigure}
\begin{center}
\includegraphics[width = \textwidth]{illustration_images/single_locus_selection/Silene_inbreeding_dep/Silene_latifolia_Sturm18.jpg}
\end{center}
\caption{  White
campion  ({\it S. latifolia}). \newline \noindent \tiny{ Deutschlands Flora in Abbildungen
(1796). Johann Georg Sturm (Painter: Jacob Sturm).  Public Domain,
    \href{https://zh.wikipedia.org/wiki/File:Silene_latifolia_Sturm18.jpg}{wikimedia}.} } \label{fig:Silene}
\end{marginfigure}
One consequence of segregating for low-frequency recessive deleterious alleles is that inbreeding can reduce fitness.  In typically outbred populations, the mean fitness of individuals
decreases with the inbreeding coefficient, i.e. so-called 'inbreeding depression'
is a common observation. This wide-spread observation dates back to systematic
surveys of inbreeding depression by \citet{darwin:1876}.
Inbreeding depression is likely primarily a consequence of being homozygous at many loci for
alleles with recessive deleterious effects.

One example of inbreeding
depression is shown in Figure \ref{fig:Silene_inbreeding}.  White
campion ({\it Silene latifolia}) is a dioecious flowering plant; dioecious means that the males and females are
separate individuals. \citet{richards:00} performed crosses to
create offspring who were outbred, the offspring of half-sibs,
full-sibs, and of two generations of full-sib mating. He measured
their germination success, which is plotted in Figure
\ref{fig:Silene_inbreeding}. Note how the fitness of individuals
declines with increased inbreeding.

\begin{figure}
\begin{center}
  \includegraphics[width = 0.8 \textwidth]{Journal_figs/single_locus_selection/Decode_human_inbreeding/decode_inbreeding.pdf}
\end{center}
\caption{Inbreeding depression for offspring lifespan in humans in Iceland. Data from \citep{helgason2008association}. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/Decode_human_inbreeding/decode_inbreeding.R} } \label{fig:Decode_inbreeding}
\end{figure}

We also see evidence for inbreeding depression in various human
populations. For example, \citet{helgason2008association} used the
remarkable genealogical records in Iceland to look at the effects of
inbreeding on various fitness components in humans. They saw that
parents who were closer than 2nd cousins had children with reduced
lifespans. However, these patterns were more complex for other fitness
components with parents with immediate levels of relatedness having
more descendants overall. More generally, studying inbreeding
depression is challenging in humans because it can be difficult to
differentiate the cultural and socio-economic effects frombiological
effects on reproduction. Finally, it is important to note that
societal views of inbreeding risks can be overblown compared to the
actual risks and that these fears have often been used to stigmatize
immigrant and rural poor communities \citep{paul2008s}.

\graham{``I do not think anything in my scientific life has given me so much satisfaction as making out the meaning of the structure of heterostylous flowers''.-Darwin}

\paragraph{Purging the inbreeding load.}
Populations that regularly inbreed over sustained periods of time
are expected to partially purge this load of deleterious
alleles. This is because such populations have exposed many of these alleles
in a homozygous state, and so selection can more readily remove these alleles
from the population.

If the population has sustained inbreeding, such that individuals in the population have an inbreeding
coefficient $F$, deleterious alleles at each locus will find a
new equilibrium frequency. Assuming the mutation-selection
model, now with inbreeding, the equilibrium frequency is
\begin{equation}
	q_e = \frac{\mu}{\big( h(1-F) + F \big) s}
\end{equation}
The frequency of the deleterious allele is decreased due to
the allele now being expressed in homozygotes, and therefore exposed
to selection, more often due to inbreeding.  Thus, all else being equal,
populations that have had a long-term history of close inbreeding will purge their load.
\graham{Add plant eg of purging}
\subsection{Migration--selection balance}
The influx of alleles carried by migrants from other populations can
be an important source of genetic and phenotypic variation.
Thus, one reason for the persistence of deleterious alleles in a
population is that there is a constant influx of maladaptive alleles
from other populations where these alleles are locally adaptive.
Migration--selection balance seems unlikely to be as broad an explanation for the
persistence of deleterious alleles genome-wide as mutation-selection
balance. However, a brief discussion of such alleles is worthwhile, as
it helps to inform our ideas about local adaptation, hybrid zones, and speciation.\\

\begin{marginfigure}
\begin{center}
  \includegraphics[width = \textwidth]{illustration_images/single_locus_selection/Anthoxanthum_odoratum/19750531053_28f0b3b36e_z.jpg}
\end{center}
\caption{Sweet vernal grass ({\it Anthoxanthum odoratum}). \BHLNC{Billeder af
  nordens flora  (1917).  Mentz, A \& Ostenfeld, C H.}{https://www.flickr.com/photos/internetarchivebookimages/19750531053/in/photolist-wNFPcF-oeKFoD-oeqVrq-xjVkoi-xi5Y8c-whqJ7q-wEuf4k-wJ17N5-tomD7h-wJMXfh-xjM43c-u5erAM-xz2zWL-wH6Dsj-xhwUay-tB85tk-owe1TX-xzu6yj-x4Z1QU-xKkAg9-wR4LKK-xuCehm-xrBNQo-wHELGB-xmTvij-xkqKLC-xzu78f-wCuhJH-x4qfEM-x4p98v-x4n2fg-x3ZDt3-xkAEUP-womZdZ-woh4Wg-x3eR9U-wnUSax-xj2vfv-xf1qFH-x5g4HL-x6c1k4-x4pCYH-w6hCMt-wYM1uh-wZSoKj-w4AuK3-tn7aso-tyRsGC-tiuGr3-ow8wYz}{New York Botanical Garden}} \label{fig:Zinc_plant}
\end{marginfigure}

Local adaptation \erin{do you need to define local adaptation here?} can occur over a range of geographic scales. Local
adaptation is relatively unimpeded by migration at broad
geographically scales, where selection pressures change more slowly than distances
over which individuals typically migrate over a number of
generations. Adaptation can, however, potentially occur on much finer geographic scales,
from kilometers down to meters in some species. On such small scales,
dispersal is surely rapidly moving alleles between environments, but
local adaptation is maintained by the continued action of selection. An
example of adaptation at fine-scales is shown in Figure
\ref{fig:Zinc_mine} \graham{Fix axis on graph}. \citet{jain1966evolutionary}
studied the patterns of heavy-metal resistance in plants
on mine tailings and in nearby meadows, a set of classic studies of population differences maintained by local adaptation to different soils.
\begin{figure}
\begin{center}
  \includegraphics[width = \textwidth]{Journal_figs/single_locus_selection/Anthoxanthum_mines_Jain_Bradshaw/Anthoxanthum_zinc.pdf}
\end{center}
\caption[][2cm]{Data showing the zinc tolerance of {\it Anthoxanthum
odoratum} on and off of the Trelogan
Mine, Flintshire, North Wales. The numbers along the top give the soil
contamination of zinc in
parts per million.  Data from \citet{jain1966evolutionary}. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/Anthoxanthum_mines_Jain_Bradshaw/Anthoxanthum_zinc_Jain_Bradshaw.R}} \label{fig:Zinc_mine}
\end{figure}
Even at these very short geographically scales, over which seed and
pollen will definitely move, we see strong local
adaptation. Zinc-intolerant alleles are nearly absent from the mine tailings because they prevent plants from growing on these
zinc-heavy soils; conversely, zinc-tolerant alleles do not spread into the meadow populations,  likely due to some trade-off or fitness cost of zinc-tolerance.

As a first pass at developing a model of local adaptation, let's consider a haploid two-allele model with
two different populations, see Figure \ref{fig:mig_sel_bal}, where the relative fitnesses of our alleles
are as follows
\begin{center}
\begin{tabular}{c|cc}
allele & $1$ & $2$ \\
\hline
population 1 & 1 & 1-s \\
population 2 & 1-s & 1 \\
\end{tabular}
\end{center}
As a simple model of migration, let's suppose within a population a
fraction of $m$ individuals are migrants from the other population,
and $1-m$ individuals are from the same population.\\
\begin{marginfigure}
\begin{center}
  \includegraphics[width = \textwidth]{figures/mig_sel_balance_haploid.pdf}
\end{center}
\caption{Setup of a two-population haploid model of local adaptation.} \label{fig:mig_sel_bal}
\end{marginfigure}

To quickly sketch an equilibrium solution to this scenario, we'll take an approach analogous
to our mutation-selection balance model. To do this, let's assume that selection is strong compared to migration ($s \gg m$), such that allele
$1$ will be almost fixed in population $1$ and allele $2$ will be
almost fixed in population $2$. If that is the case,
migration changes the frequency of allele $2$ in population $1$ ($q_1$) by
\begin{equation}
\Delta_{Mig.} q_1 \approx m
\end{equation}
while as noted above $\Delta_{S} q_1= -sq_1$, so that migration and
selection are at an equilibrium when $0 = \Delta_{S} q_1+
\Delta_{Mig.}q_1$, i.e. an equilibrium frequency of allele $2$ in
population $1$ of
\begin{equation}
q_{e,1} = \frac{m}{s}
\end{equation}
Here, migration is playing the role of mutation and so
migration--selection balance (at least under strong selection) is
analogous to mutation--selection balance.\\

We can use this same model by analogy for the case of
migration--selection balance in a diploid model. For the diploid case, we replace
our haploid $s$ by the cost to heterozygotes $hs$ from our directional
selection model, resulting in a diploid
migration--selection balance equilibrium frequency of
\begin{equation}
q_{e,1} = \frac{m}{hs} \label{eqn:mig_sel_eq}
\end{equation}
If selection is weaker and only of the order of migration $s \approx
m$ our migration-selection polymorphism collapses, as selection can
not maintain the difference in the face of gene flow. Under this
situation, both populations are expected to have roughly the same
frequency of the alleles. Migration has swamped out local adaptation.
\begin{figure}
\begin{center}
  \includegraphics[width = 0.8 \textwidth]{Journal_figs/single_locus_selection/Rock_Pocket_mice/Rock_pocket_mice.pdf}
\end{center}
\caption{Frequency of melanic mice on the lava flow, and at nearby
  locations (diamonds). Frequency of {\it MC1R} melanic allele at same
  locations. Data from \citet{hoekstra2004ecological}. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/Rock_Pocket_mice/Rock_pocket_mice.R}} \label{fig:mig_sel_bal_mice}
\end{figure}

\begin{marginfigure}
\begin{center}
  \includegraphics[width =  \textwidth]{illustration_images/single_locus_selection/chaetodipus_pocket_mice/chaetodipus.png}
\end{center}
\caption{Two species from the genus {\it Chaetodipus}, pocket mice,
  formally known as {\it Perognathus}. \BHLNC{Wild animals of North America,
  intimate studies of big and little creatures of the mammal
  kingdom (1918), Nelson, E. W.}{https://www.flickr.com/photos/biodivlibrary/6217227436/in/photolist-x1RbyL-atoV7u-wLxcK5-otRwPb-ovD312-otT7XT-xo6AL1-xXACVH-xWJdrj-x4aerR-w7hgf4-wLxwW3-x5GDbx-x1RehS-w79qy1-x6XU6C-tAuQHM-w7hbgB-wLxXg3-x49K8g-w7hf9g-x71usx-wrkzJ9-oddZ1D-xXADKt-xYfW4F-w8hLZD}{American Museum of Natural History Library}} \label{fig:chaetodipus}
\end{marginfigure}
\begin{question}{}
\citet{hoekstra2004ecological} found that the dark $D$ allele was at
$3\%$ frequency at the Tule Mountains study site. Using $F_{ST}$-based
approaches, for unlinked markers, they estimated that the per individual migration rate was $m=7.0 \times
10^{-4}$ per generation between this site and the  Pinacate lava flow. What is the selection coefficient acting against the dark $D$ allele at the Tule Mountains site?
\end{question}
As an example of fine-scale local adaptation due to a single locus,
consider the case of the rock pocket mice adapting to lava flows. Throughout the deserts of the American
Southwest there are old lava flows, where the rocks and soils are much
dark than the surrounding desert.  Many populations of small animals
that live on these flows have evolved darker pigmentation to be
cryptic against this dark substrate and better avoid visual predators.
One example of such a locally adapted population are the rock pocket
mice ({\it Chaetodipus intermedius}) who live on the Pinacate
lava flow on the Arizona-Mexico border, studied by
\citet{hoekstra2004ecological}. These mice have much darker, more melanic pelts  than the mice who live on nearby rocky outcrops (see Figure \ref{fig:mig_sel_bal_mice}).   %https://hoekstra.oeb.harvard.edu/files/hoekstra/files/hoekstra2005vol.pdf
\citet{nachman2003genetic} determined that a dominant allele ($D$) at
{\it MC1R}
is the primary determinant of this melanic phenotype. The frequency of
this allele across study sites is shown in Figure
\ref{fig:mig_sel_bal_mice}. \citet{hoekstra2004ecological} found that
other, unlinked markers showed little differentiation over these populations, suggesting that the migration rate is high.

%% Selection overrides gene flow to break down maladaptive mimicry https://www.nature.com/articles/nature06532

\paragraph{The width of a genetic cline.}
We can also extend these ideas beyond our discrete model to a model of a population spread out on a landscape where individuals migrate in a more continuous fashion. For simplicity, let's assume a one dimensional
habitat, where the habitat makes a sharp transition in the middle of
our region. You could imagine this to be a set of populations sampled along a
transect through some environmental transition. Our individuals
disperse to live on average $\sigma$ miles away from where they were
born (we can think of this as our individuals migrating a random
displacement drawn from a normal distribution, with mean zero, and $\sigma$ being the standard
deviation of this distribution). \marginnote{``Upon an island hard to reach,
the East Beast sits upon his beach.
Upon the west beach sits the West Beast.
Each beach beast thinks he's the best beast.'' --
Theodor Seuss Geisel} We'll think of a bi-allelic model where
the homozygotes for allele 1 have an additive selective advantage $s$
over allele $2$ homozygotes to the east of our habitat transition (left of
zero in Figure \ref{fig:cline_main}). This flips to allele $2$ having the same advantage $s$ west of the transition (right of zero).  If you've read this send Prof Coop a picture of
the East and West Beast.

\begin{figure}
\begin{center}
\includegraphics[width=0.7\textwidth]{figures/equilib_cline.pdf}
\end{center}
\caption{An equilibrium cline in allele frequency (the frequency of
  allele 2, $q(~)$ is shown). Our individuals
  dispersal an average distance of $\sigma=1$miles per generation, and our
allele $2$ has a relative fitness of $1+s$ and $1-s$ on either side of
the environmental change at $x=0$. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Rcode/cline.R}} \label{fig:cline_main}
\end{figure}

With this setup, we get an equilibrium
distribution of our two alleles, where to the left of zero our allele $2$ is at
higher frequency, while to the right of zero allele $1$ predominates. As we
cross from the left to the right side of our range, the frequency of our allele
$2$ decreases in a smooth cline. The frequency of allele $2$, $q(x)$,
is shown as a function of location, $x$, along the cline for a variety of
selection coefficients ($s$) in Figure \ref{fig:cline_main}. The width of this cline, i.e. the
geographic distance over which the allele frequency changes, depends on
the relative strengths of dispersal and selection. If selection is
strong compared to dispersal, then selection acts to remove maladaptive
alleles much faster than migration acts to move alleles across the
environmental transition. Thus the allele frequency transition would
be very rapid, and the cline narrow, as we move across the environmental transition. In contrast, if individuals
disperse long distances and selection is weak, many alleles are being moved back and forth over the
environmental transition much faster than selection can act against
these alleles and so the cline would be very wide.

\begin{marginfigure}
\begin{center}
\includegraphics[width=\textwidth]{figures/equilib_width_cline.pdf}
\end{center}
\caption{An equilibrium cline in allele frequency from Figure
  \ref{fig:cline_main}, $s=0.01$. Vertical lines show the cline width. The
  diagonal line show the tangent to the cline at its midpoint. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Rcode/cline.R}} \label{fig:cline_tangent}
\end{marginfigure}
The width of our cline, i.e. the distance over which we make this shift
from allele $2$ to allele $1$ predominating,
can be defined in a number of different ways. One way to define
the cline width, which is simple to define but perhaps hard to measure
accurately, is via the slope (i.e. the
tangent) of $q(x)$ at $x=0$. See Figure \ref{fig:cline_tangent}. Under this definition, the cline width is
approximately
\begin{equation}
  0.6 \sigma/\sqrt{s} ~\textrm{miles}, \label{eqn:cline_width}
\end{equation}
note that the units are miles here just because we defined the average
dispersal distance ($\sigma$) in miles above. Thus the cline will be wider if individuals dispersal further, higher
$\sigma$, and if selection is weaker, smaller $s$. The appendix at the
end of this chapter, talks through the math underlying these
ideas in more detail.

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{Journal_figs/single_locus_selection/Culex_resistance/culex_resistance.pdf}
\end{center}
\caption[][2cm]{Allele frequency clines of two pesticide resistance
  alleles, at the {\it Ace 1} and {\it Ester} genes, in the
  mosquito {\it Culex pipiens}. The dotted line shows where we move from pesticide-treated to untreated
 areas as we move away from the French coast. The dots show observed allele frequencies, the solid lines clines fit under a migration-selection
  balance model of a cline. These allele frequencies represent
 collections over two summers, the frequencies of the alleles are
  substantially reduced in the winter due to the reduced use of
  pesticides. Data from
  \citet{lenormand1999tracking}. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/Culex_resistance/culex_resistance.R}} \label{fig:cline_culex}
\end{figure}

\citet{lenormand1999tracking} collected mosquitoes ({\it Culex pipiens})
in a north–south transect moving away from the Southern French
coast. Areas near the coast were are treated with pesticides, and the mosquitos have
evolved resistance, but areas just a few tens of kilometers from the coast were untreated. \citeauthor{lenormand1999tracking} estimated the frequency
of two unlinked, pesticide-resistance alleles, and found them at high
frequency near the coast but found that their frequencies declined rapidly
moving inland. \citeauthor{lenormand1999tracking} fit migration-selection
cline models to their data, similar to those in Figure \ref{fig:cline_main}, with the pesticide-resistance alleles having an
selection advantage ($s$) in treated areas an a cost ($c$) in untreated areas (they didn't
force the selective advantage and cost to be symmetric).
\begin{marginfigure}[0cm]
\begin{center}
\includegraphics[width=0.7\textwidth]{illustration_images/single_locus_selection/Culex_pipiens/Culex_pipiens.png}
\end{center}
\caption{Mosquito ({\it Culex pipiens}). \BHLNC{Domestic mosquitoes (1939).  Bishopp, F. C.}{https://archive.org/stream/domesticmosquito186bish/\#page/3/mode/1up}{U.S. Department of Agriculture, National Agricultural Library}} \label{fig:culex}
\end{marginfigure}
They
estimated that a higher selective advantage for the {\it Ace 1} allele
than{\it Ester} allele ($s=0.33$ and $s=0.19$  respectively) and a
higher cost to the {\it Ace 1} allele than
{\it Ester} allele in untreated areas ($c=0.11$ and $c=07$
respectively) potentially explaining the less extreme cline for {\it Ester}
allele than the  {\it Ace 1} allele. Despite these strong selection pressures, we still see a cline
over tens of kilometers because dispersal is relatively high ($\sigma=
6.6$km per generation).

\paragraph{Hybrid zones}
%https://sci-hub.tw/https://www.nature.com/articles/341497a0
%http://evolution-textbook.org/content/free/figures/18_EVOW_Art/16_EVOW_CH18.jpg
Local adaptation isn't the only way that selection can generate strong
spatial patterns. We can also see strong selection-driven clines when partially-reproductively isolated species spread back in to
secondary contact they can hybridize bringing alleles together
that may not work well with each other.
One simple model of
is to think about an under-dominant polymorphism, i.e. where the
heterozygote has lower fitness. The two ancestral populations
are alternatively fixed for the two fitter homozygote states,
e.g. ancestral population 1 fixed $A_1A_1$ and ancestral population two the $A_2 A_2$. The
hybrid population forming at the mating edge between the two ancestral populations has a
high frequency of the less fit heterozygotes. Thus hybrids are at a
disadvantage, potentially acting to keep the two populations from
collapsing into each other.

\begin{figure}
\begin{center}
\includegraphics[width=0.65 \textwidth]{Journal_figs/single_locus_selection/grasshopper_cline_barton_hewitt/grasshopper_cline_barton_hewitt.pdf}
\end{center}
\caption{The frequency of the southern neo-X chromosome moving
  along a valley transect (more southern locations to the right of the
  graph). This represents data from four different
  valleys in the French Alps over less then a kilometer, each point
  represents a sample of 20 males. The red curve is the fitted cline
  under a model of heterozygote disadvantage
  \citep{bazykin1969hypothetical}. Data from
  \citet{barton1981chromosomal},
  \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Journal_figs/single_locus_selection/grasshopper_cline_barton_hewitt/grasshopper_cline_barton_hewitt.R} } \label{fig:grasshopper_cline}
\end{figure}

%% https://commons.wikimedia.org/wiki/File:Brockhaus_and_Efron_Encyclopedic_Dictionary_b56_402-0.jpg

%Brockhaus and Efron Encyclopedic Dictionary
Two previously isolated populations of the short-horned grasshopper {\it Podisma pedestris}
have spread into secondary contact in the French Alps, probably after
the last ice age. The population that has spread into the Alps from the south has a large section of novel X chromosome,
due to a chromosomal fusion. This `neo-X' is absent in the populations
that spread from the North into the Alps. The two populations meet in
many valleys running through the Alps, and repeatedly form a narrow
hybrid zone, with the frequency of the neo-X chromosome forming a very
steep cline transitioning in frequency over a few hundred meters \citep{barton1981chromosomal}.
One potential reason for this steep cline is that females who are
heterozygous for the neo-X (neo-X/old-X) may have reduced fitness, consistent with
an underdominant polymorphism. The neo-X allele cannot spread into the northern
population as it cannot increase in frequency when rare. Conversely
the northern population cannot displace the neo-X, as the old-X is at
a disadvantage.  This spatial distribution at this locus is a tension zone between the
two populations, where neither allele can push the other out due to the low fitness of the hybrid.
\begin{marginfigure}[1cm]
\begin{center}
\includegraphics[width= \textwidth]{illustration_images/single_locus_selection/Grasshoppers/634px-Brockhaus_and_Efron_Encyclopedic_Dictionary_b56_402-0.jpg}
\end{center}
\caption{
7. {\it Podisma pedestris}, a species
of short-horned grasshoppers;  from a
page illustrating {\it Orthoptera}.
\newline \noindent \tiny{Illustration from Brockhaus and Efron
  Encyclopedic Dictionary (1890) Image \href{https://commons.wikimedia.org/wiki/File:Brockhaus_and_Efron_Encyclopedic_Dictionary_b56_402-0.jpg}{wikimedia},  public domain.}
} \label{fig:grasshoppers}
\end{marginfigure}
We can use our same continuous model of migration and selection to
study this setup. Assuming that the homozygotes are equally fit, and
that the heterozygotes relative fitness is reduced by a selection
coefficent $s_h$, the width of the cline is
\begin{equation}
\frac{\sigma}{\sqrt{s_h}}
 \end{equation}
The stronger the selection the more abrupt the transition between the
populations. These wingless grasshoppers move $\sigma \sim 20$ meters
a generation. Thus a reduction in the relative fitness of the hybrid
would be needed to explain this hybrid zone with a width of $\sim
800$m.

More generally we can see tension zones arise when hybrids have reduced
fitness compared to either species. For example, this can occur due to
be due to bad epistatic interactions between alleles from each
species. If selection is strong enough on hybrids, often because many
loci are involved in incompatibilities between the species, the entire
genome can be tied up in a tension zone between the two species.

%Throughout
%most of its range {\it Podisma pedestris} has an X chromosome-based sex
%determination system. But the population

% \begin{question}
% You are investigating a small river population of sticklebacks, which receives infrequent migrants from a very large marine population. At a set of (putatively) neutral biallelic markers the freshwater population has frequencies:\\
% 0.2, 0.7, 0.8\\
% at the same markers the marine population has frequencies:\\
% 0.4, 0.5 and 0.7.\\
%  From studying patterns of heterozygosity at a large collection of markers, you have estimated the long term effective size of your freshwater population is 2000 individuals.\\
% {\bf A)}	What is $F_{ST}$ across these neutral markers in the freshwater population, with respect to the large marine population (i.e. treat the marine population as the total)?\\
% {\bf B)} You are also studying an unlinked locus involved in the
% regulation of salt uptake. In the marine population the ancestral
% allele is at close to fixation, but in your river population the
% derived allele is at 0.99 frequency. Estimate the selective
% disadvantage of the ancestral allele in your river population. [Hint
% how can you use neutral differentiation to estimate the migration rate?]
% \end{question}

\subsection*{Appendix: Some theory of the spatial distribution of allele
frequencies under deterministic models of selection}

Imagine a continuous haploid population spread out along a line. Each individual
disperses a random displacement $\Delta x$ from its birthplace to the location where
it reproduces, where $\Delta x$ is drawn from the probability density $g(\Delta x)$.
To make life simple, we will assume that $g(\Delta x)$ is normally distributed
with mean zero and standard deviation $\sigma$, i.e. migration is unbiased and
individuals migrate an average displacement of $\sigma$. \\

The frequency of allele $2$ at time $t$ in the population at spatial location
$x$ is $q(x,t)$. Assuming that only dispersal occurs, how does our allele
frequency change in the next generation? Our allele frequency in the next
generation at location $x$ reflects the migration from different locations in
the proceeding generation. Our population at location $x$ receives a
contribution $g(\Delta x)q(x+\Delta x,t)$ of allele $2$ from the population at
location $x+\Delta x$, such that the frequency of our allele at $x$ in the next
generation is \begin{equation} q(x,t+1) = \int_{-\infty}^{\infty} g(\Delta
x)q(x+\Delta x,t) d \Delta x.  \end{equation}

To obtain $q(x+\Delta x,t)$, let's take a Taylor series expansion of $q(x, t)$:

\begin{equation}
q(x+\Delta x,t) = q(x,t) + \Delta x \frac{dq(x,t)}{dx}+ \tfrac{1}{2}(\Delta x)^2 \frac{d^2q(x,t)}{dx^2}+\cdots
\end{equation}

then

\begin{equation}
q(x,t+1) = q(x,t) +\left( \int_{-\infty}^{\infty} \Delta x g(\Delta x)
  d \Delta x \right) \frac{dq(x,t)}{dx} + \tfrac{1}{2}\left( \int_{-\infty}^{\infty}(\Delta x)^2 g(\Delta x)
  d \Delta x \right)  \frac{d^2q(x,t)}{dx^2}+\cdots
\end{equation}

Because $g(~)$ has a mean of zero, $ \int_{-\infty}^{\infty} \Delta x g(\Delta x) d
\Delta x =0$, and has because  $g(~)$ has variance $\sigma^2$,  $\int_{-\infty}^{\infty}(\Delta
x)^2 g(\Delta x) d \Delta x = \sigma^2 $. All higher order terms in
our Taylor series expansion cancel out (as all higher central moments of the normal distribution are zero). Looking at the change in allele frequency,
$\Delta q(x,t) = q(x,t+1)-q(x,t)$, so

\begin{equation}
\Delta q(x,t) = \frac{\sigma^2}{2} \frac{d^2q(x,t)}{dx^2}
\end{equation}
This is a diffusion equation, so that migration is acting to smooth out allele frequency differences with a diffusion constant of $\tfrac{\sigma^2}{2}$. This is exactly analogous to the equation
describing how a gas diffuses out to equal density, as both particles in a gas and our individuals of type $2$ are performing Brownian motion (blurring our eyes and seeing time as continuous). \\

We will now introduce fitness differences into our model and set the relative
fitnesses of allele $1$ and $2$ at location $x $ to be $1$ and $1+s\gamma(x)$.
To make progress in this model, we'll have to assume that selection isn't too
strong, i.e. $ s \gamma(x) \ll 1$ for all $x$. The change in
frequency of allele $2$ obtained within a generation due to selection is

\begin{equation}
q^{\prime}(x,t) - q(x,t) \approx s\gamma(x) q(x,t) \big( 1 - q(x,t) \big)
\end{equation}
i.e. logistic growth of our favoured allele at location $x$.  Putting our
selection and migration terms together, we find the total change in allele frequency at location x in one generation is
\begin{equation} q(x,t+1) -
q(x,t) = s\gamma(x) q(x,t) \big( 1 - q(x,t) \big)+\frac{\sigma^2}{2}
\frac{d^2q(x,t)}{dx^2} \label{eqn:fisherKPP}
\end{equation}
In deriving this result we have ignored terms of the order of $\sigma s$.
%, we have essentially assumed that migration acted upon our original
%allele frequencies before selection, and in doing so

\begin{figure}
\begin{center}
\includegraphics[width=0.5\textwidth]{figures/equilib_cline.pdf}
\end{center}
\caption{An equilibrium cline in allele frequency. Our individuals
  dispersal an average distance of $\sigma=1$km per generation, and our
allele $2$ has a relative fitness of $1+s$ and $1-s$ on either side of
the environmental change at $x=0$.} \label{fig:cline}
\end{figure}

\paragraph{The cline in allele frequency associated with a sharp
  environmental transition.}
To make progress, let's consider a simple model of local adaptation where the
environment abruptly changes. Specifically, we assume that $\gamma(x)= 1$ for
$x<0$ and $\gamma(x)= -1$ for $x \geq 0$, i.e. our allele $2$ has a selective
advantage at locations to the left of zero, while this allele is at a
disadvantage to the right of zero. In this case we can get an equilibrium
distribution of our two alleles, where to the left of zero our allele $2$ is at
higher frequency, while to the right of zero allele $1$ predominates. As we
cross from the left to the right side of our range, the frequency of our allele
$2$ decreases in a smooth cline. \erin{figure 6.25 is repeating figure 6.24.. }\\

%Under such a model the change in frequency of allele $2$ at location $x$ at time $t$,
%$q(x,t)$, over time follows the differential equation
%\begin{equation}
%\frac{dq(x,t)}{dt} = s\gamma(x) q(x,t) \left( 1 - q(x,t) \right) + \frac{\sigma^2}{2} \frac{d^2q(x,t)}{dx^2}
%\end{equation}
%the first term on the right-hand side is the logistic growth of our
%favoured allele at location $x$, while the second term on the
%right-hand side is the diffusion of allele frequencies due to
%dispersal (i.e. dispersal is acting to spread out the effect of
%allele frequency change across populations). See below for how we
%derive this \gc{to be added}.\\

Our equilibrium spatial distribution of allele frequencies can be found by
setting the left-hand side of eqn. \eqref{eqn:fisherKPP} to zero to arrive at
\begin{equation}
s\gamma(x) q(x) \left( 1 - q(x) \right) = - \frac{\sigma^2}{2} \frac{d^2q(x)}{dx^2}
\end{equation}
We then could solve this differential equation with appropriate boundary
conditions ($q(-\infty)=1$ and $q(\infty) = 0$) to arrive at the
appropriate functional form for our cline. While we won't go into the
solution of this equation here, we can note that by dividing our
distance $x$ by $\ell=\sigma/\sqrt{s}$, we can remove the effect of our
parameters from the above equation. This compound parameter $\ell$ is the characteristic
length of our cline, and it is this parameter which determines over
what geographic scale we change from allele $2$ predominating to
allele $1$ predominating as we move across our environmental shift. \\
\newpage
\begin{ChapterSummary}
\item Deleterious variation can be maintained in the population by a balance of selection and mutation. If the mutations are not completely recessive, the equilibrium frequency of deleterious
  alleles is given by the ratio of mutation to the selection coefficient against heterozygotes ($q_{eq}= \nicefrac{\mu}{hs}$). The more recessive an allele the higher frequency it segregates
  under mutation-selection balance, all else being equal, as they better avoid selection in the heterozygote state.
\item While the equilibrium frequency of alleles under mutation-selection balance at any one locus is low, there are many such loci in the genome such that every individual carries many deleterious alleles.
\item As more recessive deleterious alleles segregate at higher frequency, inbred individuals are expected to have lower fitness than typical outbred individuals in the population as they are on average homozygous for recessive deleterious alleles.
 \item Divergent selection between populations can maintain allele frequency differences between populations in the face of migration. The constant influx of alleles by migration can maintain maladaptive alleles at low frequency in the face of selection leading to a migration selection balance, an analog to mutation selection balance.
  \item When strong selection pressures change over short geographical scales, we expect abrupt allele frequency clines at the selected loci. We also expect strong allele frequency clines in hybrid zones at loci underpinning hybrid fitness disadvantage.

\end{ChapterSummary}

\begin{question}{}
  You are studying a gene causing partial infertility, due to errors
during meiosis, in an outcrossing plant population. You estimate that $5\%$ of heterozygotes for knockout
mutation in this gene are completely sterile, but $95\%$ of
heterozygote individuals have normal fertility. Homozygotes for the
knockout are often embryonic lethal due to errors in mitosis. The frequency at birth
of knockouts for the gene is $\nicefrac{1}{5000}$.\\
{\bf A)} What is the knockout mutation rate at this gene?\\

{\bf B)} You find a sister species which has had a high degree of
inbreeding for many generations due to selfing. Do you expect to find
the knockout allele at higher or lower frequency? Explain your
answer.
\end{question}

\begin{question}{}
There's an outbred population of mice living in a farmer’s field. Mutations occur at a gene called nurseryrhyme that cause a totally recessive form of blindness. These blind mice do not survive to reproduce as the farmer’s wife cuts off their tail (and other bits) with a carving knife.
Surveying the field for baby mice you find that 3 in ten thousand mice are blind.\\
{\bf A} Assuming that the population mates at random, what is the mutation
rate of blindness causing alleles?\\
{\bf B} Following more careful study you now find that there is actually a $20
\%$ reduction in the viability of heterozygotes for these
mutations. What would you now estimate as the mutation rate for this
gene?
{\bf C)} Explain how and why your answers differ?
\end{question}

%\paragraph{Cline arising from an underdominant polymorphism}

%The width of our cline, i.e. over what distance do we make this shift
%from allele $2$ predominating to allele $1$,
%can be defined in a number of different ways. One simple way to define
%the cline width, which is easy to define but perhaps hard to measure accurately, is the slope (i.e. the
%tangent) of $q(x)$ at $x=0$. Under this definition the cline width is approximately $0.6
%\sigma/\sqrt{s}$.\\

%\paragraph{The rate of spatial spread of a beneficial allele.}
%Consider a beneficial mutation that has arisen in a specific spatial
%location and has begun to spread geographically.
%\gc{FINISH THIS.}


% Acronyms ====================================================================
\addcontentsline{toc}{chapter}{\tocEntry{Acronyms}}
\chapter*{Acronyms}

\begin{tabularx}{\textwidth}{ l X }
$CS$ 		& Cost-sensitive classification \\
$CI$    & Cost-insensitive classification \\
$CPS$    & Cost-proportionate sampling \\
$CST$    & Cost-sensitive training \\
$LR$  & Logistic regression \\
$DT$  & Decision tree  \\
$RF$ &  Random forest  \\
$BMR$ & Bayes minimum risk \\
$CSLR$ & Cost-sensitive logistic regression  \\
$CSDT$ & Cost-sensitive decision tree  \\
$ECSDT$ & Ensemble of cost-sensitive decision tree \\
$CSB$ & Cost-sensitive bagging\\
$CSP$ & Cost-sensitive pasting\\
$CSRF$ & Cost-sensitive random forest  \\
$CSRP$ & Cost-sensitive random patches  \\
$t$ & Training \\
$u$ & Under-sampling \\
$r$ & Cost-proportionate rejection-sampling \\
$o$ & Cost-proportionate over-sampling \\
$mv$ & Majority voting \\
$wv$ & Cost-sensitive weighted voting \\
$s$ & Cost-sensitive stacking \\
\end{tabularx}


\section{Feature Invariance}

Rotation is added to the testing images to see how tolerant the different models are. This section will describe the implementation of the rotation method used to test feature invariance. The implementation of the rotation can be seen in listing \ref{lst:rotation}.

\begin{lstlisting}[language=Python, label=lst:rotation, caption=Rotate the images and calculate the accuracy]
def RotateAndCalc(sess, images, labels):
    #List with all accuracies
    accList = []
    prcList = np.zeros(len(labels))

    rotAngle = 10
    curAngle = 10

    #First run for original image
    fAcc,fPrc = CalcTestAccuracy(sess, ConvertImages(images),
	 	labels, False)
    accList.append(fAcc)
    prcList = [x+y for x, y in zip(prcList, fPrc)]

    #Rotate 10 degree
    while curAngle < 360:
        print "Rotating degree " + str(curAngle)
        rotImages = tf.contrib.image.rotate(ConvertImages(images),
			np.radians(curAngle))

        #Calc accuracy and failures for the rotated images
        sAcc, sPrc = CalcTestAccuracy(sess, rotImages, labels, True)
        prcList = [x+y for x, y in zip(prcList, sPrc)]
        accList.append(sAcc)

        #Increment our curAngle with +rotAngle
        curAngle += rotAngle

    #Get failures in percentage
    prcSum = sum(prcList)
    nPrcList = []
    for numb in prcList:
        nPrcList.append((numb / prcSum) * 100)

    return accList, nPrcList
\end{lstlisting}

First the accuracy for the original images is calculated, this is the same as having a rotation of 0. Thereafter the images is rotated by 10 degree and the accuracy is calculated for the rotated images. The images will get rotated until they reach a rotation of 350 degree, and a total of 35 accuracies are calculated (from 0 to 350 degree). 360 degree is not included, as this will be the same as a 0 degree rotation. The accuracies is added to a list, which will be used to plot the results. For each of the 35 runs the CalcTestAccuracy function returns a list with the amounts of failure for each label. This could be label 0 (Airplane) failed to classify a specific amount of times for example. the prcList is after each run updated with these amounts. At last the the amount of failure for each label is calculated in percentage. This gives a rotation, from 0 to 350 degree, mean for each label. The mean value for each label can be used to see which label is most rotational invariant.
\newline

\mySubFigure{org_image}{rotate_40}{Comparison of the original image and a image which is rotated 40 degree}{Original image}{Image rotated 40 degree}{fig:rot}{fig:orgimg}{fig:rotimg}

Figure \ref{fig:orgimg} shows the first original image extracted from the CIFAR-10 test set. By rotating the original image with 40 degree the image shown in figure \ref{fig:rotimg} is given. The images shown in figure \ref{fig:rot} is blurry, this is because the images in the CIFAR10 set is small, and when plotting them they get stretched. The printed images have been reshaped to a 32x32x3 image to be able to plot them. Three is the depth of the colorspace for the image, which in this case is the RGB colorspace. The images on figure \ref{fig:rot} clearly shows an airplane.


\chapter{The Thouless theorem}
\label{appendix:thouless}

\begin{ntt}
\label{ntt:orbital-transformation}
Let $\bm{\y}=[\y_p]$ be a row vector of orthonormal spin-orbitals, composed of occupied and virtual blocks, $\bm{\y}=[\bm{\y}_{\text{o}}\ \bm{\y}_{\text{v}}]$, with respect to a reference determinant, $\F$.
Other spin-orbital bases relate to this one via
$
  \kt{\bm{\y}'}
=
  \kt{\bm{\y}}\,
  \bo{U}
$,
which is a unitary transformation if the primed orbitals are orthonormal.
Let $\F'$ be the \textit{transformed reference determinant}, constructed from the first $n$ orbitals in $\bm{\y}'$.
Then the occupied and virtual orbitals of the transformed space are given by
\begin{align}
\label{eq:block-transformation}
  \kt{\bm{\y}'_{\text{o}}}
=
  \kt{\bm{\y}_{\text{o}}}\,
  \bo{U}_{\text{oo}}
+
  \kt{\bm{\y}_{\text{v}}}\,
  \bo{U}_{\text{vo}}
&&
  \kt{\bm{\y}'_{\text{v}}}
=
  \kt{\bm{\y}_{\text{o}}}\,
  \bo{U}_{\text{ov}}
+
  \kt{\bm{\y}_{\text{v}}}\,
  \bo{U}_{\text{vv}}
\end{align}
in terms of the occupied and virtual blocks of $\bo{U}$.
This kind of transformation is sometimes called an \textit{orbital rotation}.
\end{ntt}

\begin{thm}
\label{thm:thouless}
\thmtitle{The Thouless theorem}
\begin{enumerate}
\item
\label{item:thouless-part-1}
\thmstatement{
  The function
  $e^{T_1}\F$
  is an intermediately normalized determinant
  $
    \tfr{1}{\sqrt{n!}}
    \text{det}(\widetilde{\y}_1\cd\widetilde{\y}_n)
  $
  with orbitals
  $
    \widetilde{\y}_i
  =
    \y_i
  +
    \sum_a
    \y_a
    t_a^i
  $.
}
\thmproof{
  Intermediate normalization follows from
  $
    \ip{\F|e^{T_1}\F}
  =
    1
  $.
  This function has the form of a determinant
\begin{align*}
  e^{T_1}
  \kt{\F}
=
  e^{\sum_{a}t_a^1 a_1^a + \cd + \sum_a t_a^n a_n^a}
  a_1\dg
  \cd
  a_n\dg
  \kt{\vac}
=
  \widetilde{a}_1\dg
  \cd
  \widetilde{a}_n\dg
  \kt{\vac}
=
  \kt{\widetilde{\F}}
&&
  \widetilde{a}_i\dg
\equiv
  \text{exp}(\ts{\sum_{a}t_a^i a_i^a})\,
  a_i\dg
\end{align*}
  since $\sum_a t_a^ia_i^a$ commutes with all creation operators except $a_i\dg$.
  The transformed orbitals are given by
\begin{align*}
  \kt{\widetilde{\y}_i}
=
  \widetilde{a}_i\dg
  \kt{\vac}
=
  \text{exp}(\ts{\sum_{a}t_a^i a_a\dg a_i})\,
  a_i\dg
  \kt{\vac}
=
  (\ts{
    1
  +
    \sum_a
    t_a^i
    a_a\dg
    a_i
  })\,
    a_i\dg
  \kt{\vac}
=
  \kt{\y_i}
+
  \ts{\sum_a}
  t_a^i
  \kt{\y_a}
\end{align*}
 using $a_i^2=0$ and $a_ia_i\dg\kt{\vac}=\kt{\vac}$.
}

\item
\thmstatement{
  Any intermediately normalized determinant
  $
    \widetilde{\F}
  =
    \tfr{1}{\sqrt{n!}}
    \text{det}(\widetilde{\y}_1\cd\widetilde{\y}_n)
  $
  can be written as $e^{T_1}\,\F$.
}
\thmproof{
  Intermediate normalization implies that $\widetilde{\F}$ has non-zero overlap with the reference determinant.
  Therefore, $\widetilde{\F}$ can be written as
  $\F'/\ip{\F|\F'}$
  where $\F'$ is a Slater determinant.
  The normalization factor is given by
\begin{align*}
\ts{
  \ip{\F|\F'}
=
  \tfr{1}{n!}
  \sum_{\pi,\si}^{\text{S}_n}
  \e_\pi
  \e_\si
  \ip{\y_{\pi(1)}|\y'_{\si(1)}}
  \cd
  \ip{\y_{\pi(n)}|\y'_{\si(n)}}
=
  \sum_{\si}^{\text{S}_n}
  \e_\si
  \ip{\y_{1}|\y'_{\si(1)}}
  \cd
  \ip{\y_{n}|\y'_{\si(n)}}
=
  \text{det}(\bo{U}_{\text{oo}})\,\,.
}
\end{align*}
  Therefore,
  $
    \widetilde{\F}
  =
    \F'/
    \text{det}(\bo{U}_{\text{oo}})
  =
    \F'\,
    \text{det}(\bo{U}_{\text{oo}}^{-1})
  $
  and the rows of $\widetilde{\F}$ are given by the following vector\footnote{
    The second equality follows from expanding $\kt{\bm{\y}_{\text{o}}'}$ according to eq~\ref{eq:block-transformation}.
  }
\begin{align*}
  \kt{\bm{\widetilde{\y}}_{\text{o}}}
=
  \kt{\bm{\y}'_{\text{o}}}\,
  \bo{U}_{\text{oo}}^{-1}
=
  \kt{\bm{\y}_{\text{o}}}\,
+
  \kt{\bm{\y}_{\text{v}}}\,
  \bo{U}_{\text{vo}}
  \bo{U}_{\text{oo}}^{-1}
\end{align*}
  with elements
  $
    \widetilde{\y}_i
  =
    \y_i
  +
    \sum_a
    \y_a\,
    (\bo{U}_{\text{vo}}\bo{U}_{\text{oo}}^{-1})_{ai}
  $.
  Referring back to part one,
  $
    \widetilde{\F}
  =
    e^{T_1}\F
  $
  with
  $
    t_a^i
  =
    (\bo{U}_{\text{vo}}\bo{U}_{\text{oo}}^{-1})_{ai}
  $.
}
\end{enumerate}
\end{thm}


\section{Implementation choices}
\label{sec:03-implementation}

As stated in \cref{sec:motion-models}, we decided to use an indirect method with affine model for motion estimation as in Bergen et al.\cite{Bergen92}; also, we decided to obtain a robust estimation by using a strategy similar to \cite{Dufeaux2000,Ayer94,He01}, where they adopt a hierarchical approach to obtain a robust estimation using coarse resolution to guide the search for the best parameters at a finer resolution.

In the following sections, we will provide an in-depth explaination of our solution and present the pseudocode of the algorithm (see \cref{alg:gme}). A schematic representation of the procedure is presented in \cref{fig:implementation-flow}.

\begin{figure}
    \centering
    \includegraphics[width=.95\linewidth]{../assets/images/implementation-flow.png}
    \caption{Schema of the flow of implementation of the global motion estimation pipeline.}
    \label{fig:implementation-flow}
\end{figure}

\subsection{Block-Based Motion Estimation}
\label{sec:BBME}
The first piece of the architecture is the algorithm to compute the motion field to be used as ground truth.
In our case, we decided to implement a block-based motion estimation (BBME) pipeline, since computing the motion field on blocks rather than single pixels is much less computationally expensive.

The main idea of BBME is to divide the frame in blocks and search for each block in the previous frame the corresponding block in the next frame.
This gives us the displacement of the block between the two frames, which is accounted as \emph{motion vector} of the block.
From this fundational principle a number of implementations have been proposed, differing mainly in two aspects:
\begin{enumerate}
    \item the size and shape of the block;
    \item the path that the searching procedure follows.
\end{enumerate}

During the project we implemented a number of these techniques:
\begin{itemize}
    \item exhaustive search;
    \item two-dimensional logarithmic search;
    \item new three-step search, from \cite{Li94};
    \item diamond search, from \cite{Zhu2000};
\end{itemize}

In the final implementation, the block matching method used is the one presented in \cite{Zhu2000}, known as diamond search (DS),  with a mean square error measure to compute (dis-)similarity between blocks.
Nonetheless, all the other methods are still present and by slightly changing the code they can be tested as well.

Here we briefly discuss the algorithm that we choose to implement and propose a small example for clarification.
The DS algorithm uses a peculiar diamond-shaped search pattern, which defines which positions will be used as target points where to center the block in the next frame to see if it corresponds to the block in the previous frame. In particular, the algorithm uses two slighlty different shapes depending on the current iteration (see \cref*{fig:ds-search-patterns}):
\begin{itemize}
    \item large diamond search pattern;
    \item small diamond search pattern.
\end{itemize}

\begin{figure}
    \centering
    \includegraphics[width=.95\linewidth]{../assets/images/ds-search-patterns.png}
    \caption{Representation of the two DS algorithm search patterns.}
    \label{fig:ds-search-patterns}
\end{figure}

The procedure is the following:
\begin{enumerate}
    \item the search starts with LDSP, meaning that we check the blocks centered in each one of the 9 positions; if the best match is the central position then we pass to the second step, otherwise we move to the best match position and re-use the LDSP;
    \item in the second step we use SDSP and look again for the best matching position among the 5 available, then return the best match position.
\end{enumerate}

\begin{figure}
    \centering
    \includegraphics[width=.95\linewidth]{../assets/images/ds-exe.png}
    \caption{Example of run of diamond search algorithm for block matching motion estimation.}
    \label{fig:diamond-search-example}
\end{figure}

In \cref{fig:diamond-search-example} we can see an example of run. We can spot the three large-shape diamonds (the contour position are marked with circles) and the small shaped diamond (contour positions marked with triangles).
In this example the block matching algorithm started from $(0,0)$ then moved with the large diamond to $(-2,0)$, because this was the position that minimized the error between the block in the previous and the next frame. Then again with the large diamond the algorithm reached $(-3,-1)$ and then $(-4,-2)$. Here the best matching position for the large diamond shape was $(-4,-2)$, therefore the algorithm stopped using the large diamond and started with the small diamond. Finally, the small diamond returned once again as best match position $(-4,-2)$, which was then returned as final matching position for the block in the previous frame.

\subsection{Affine Model Parameter Estimation}
\begin{figure*}[!h]
    \removelatexerror
    \input{../assets/code/algorithm.tex}
\end{figure*}
In \cref{sec:motion-models} we presented the affine motion model, which is the one we decided to use in our implementation.

To get the motion vector of a certain position $p=(x,y)$ in the previous frame, we can solve the following formulation of the affine model:
\input{../assets/math/displacement-matrix-formulation.tex}
where $d(p, a)$ is the displacement for the position $p = (x,y)$, given the parameter vector $a$; $A[p]$ is an intermediate matrix that is computed as
\input{../assets/math/affine-model-parameter-matrix.tex}

By encoding the estimation of the motion field in this way, the $p$-norm error we are trying to minimize (see \cref{eq:indirect-estimation}), gets rewritten in this way:

\input{../assets/math/pnorm-error.tex}
Here we introduce $d(p)$ as the ground truth motion vector (the one we get from the BBME as in \cref{sec:BBME}) and $P$ which specifies the grade of the $p$-norm used. In this case we decided to set $P=2$, following the procedure explained in \cite{WangBook}. Pay attention to the difference, in the formula, between $p$ (the position) and $P$ (the grade of the $p$-norm).

The next step is to minimize the error in \cref{eq:pnorm-error} with respect to the parameter vector $a$.
The procedure in \cite{WangBook} states that we need to compute the gradient of $E$ w.r.t $p$, and then get the value of $a$ that sets the gradient to 0.

By using the matrix formulation explained in \cref{eq:displacement-matrix-formulation} and by setting the gradient $\nabla_a E = 0$, we obtain the following result:
\input{../assets/math/optimal-parameter-affine.tex}
There are three interesting notes about \cref{eq:optimal-parameter-affine}:
\begin{enumerate}
    \item we are iterating over a set of positions $p$: it's interesting to notice that these points do not need to cover the complete scene, i.e. we can use only a subset of the image to optimize the parameters;
    \item each one of the points can be weighted with a weight $w(p)$ as shown in \cref{eq:optimal-parameter-affine-weighted};
    \item we can actually split the computation of the parameter vector $a = [a_1, a_2, a_3, b_1, b_2, b_3]$ in two different slices, $a_x = [a_1, a_2, a_3]$ and $a_y = [b_1, b_2, b_3]$, which can be computed in a specular way as shown in \cref{eq:optimal-parameter-affine-weighted-halved}\footnote{Notice the shape of the intermediate matrix $A_x(p) = [1,x,y]$ in \cref{eq:optimal-parameter-affine-weighted-halved}.}; this is useful to further reduce the complexity of the procedure.
\end{enumerate}

\input{../assets/math/optimal-parameter-affine-weighted.tex}
\input{../assets/math/optimal-parameter-affine-weighted-halved.tex}
The form of the solution that we used in the implementation is the one reported in \cref{eq:optimal-parameter-affine-weighted-halved}.

\subsection{Hierarchical Robust Estimation}

The last part of the project is a strategy used to provide a robust estimation of the parameters for the affine motion model.
The proposed implementation is inspired by \cite{Dufeaux2000}, where the computation of the motion field takes place at different levels of resolution subsequently.

The main idea is to compute the parameter vector $a$ at a coarser resolution, and then use this coarser estimation to extract information useful for regularizing the estimation at finer resolutions.
In practice, this translates in our code being structured as shown in \cref{fig:implementation-flow}: first of all, the two frames we use as previous and next frames are transformed in multi-resolution pyramids, and then used iteratively for the process of parameter estimation.

Here we report a summary of the procedure used to implement the hierarchical robust estimation:
\begin{enumerate}
    \item for each iteration we get as input the frames at the corresponding level $l$ of the pyramid (at a finer resolution w.r.t the previous iteration at level $l-1$) and the parameter vector $a_{l-1}$ computed in the previous iteration;
    \item we use the scaled parameters $a_{l-1}$ to compute the error for each position $p$ in the frames at level $l$; the error is computed as difference between the ground truth motion vector in $p$ and the motion vector obtained with the motion model in $p$ with parameters $a_{l-1}$;
    \item to detect the outliers we order all positions $p$ by the magnitude of the error, we set a fixed percentage \texttt{outlier\_percentage} and all the positions that fall in that percentage of higer-error-positions are marked as outliers;
    \item once we have marked all the outliers, we can proceed and compute the new estimate of the parameter vector $a_{l}$ ignoring the outliers;
    \item the new estimate will be returned to the next level $l+1$ of the hierarchical procedure, or simply returned to the caller of the function when we reach the last level.
\end{enumerate}

In \cref{alg:gme} we present the pseudocode summarizing the procedure.


\documentclass[11pt]{article}

\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{pseudocode}
\usepackage{url}
\usepackage[backref, colorlinks=true, citecolor=red, urlcolor=blue, pdfauthor={Jyh-Ming Lien}]{hyperref}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{#1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}
\graphicspath{ {images/} }

\begin{document}

\lecture{Programming Assignment 2 report}{Fall 2017}{Faez Dabestani Ziabari}{Computational Geometry}

\section{Summary of the two methods}

\subsection{hedcuter method}
Abstract: The hedcuter algorithm generates a number of points (this number can be given by the user) with a bias towards the darker areas; then treating these points as Voronoi sites, creates a Voronoi graph (either with or with out the GPU) and tessellates the image. Then, using a density function, the method needs to find the weighted center of each cell and replace that point as the new site of the cell. The algorithm then Recomputes the Voronoi graph, moving the center each time, over and over until the new weighted centers stay the same as the old ones (with in a margin of error), or until the code runs out of a certain predetermined number of iterations.

In the following paragraphs, I will recount some  the key details of this implementation. First off, the way hedcuter initializes the given number of random points with a bias towards darker areas, is by getting random points repeatedly, and discarding them with a probability. If the generated point is a repeat (has already been visited), we discard the point immediately. Otherwise, the point is discarded randomly with a weighted probability, increasing the likelihood of discarding a point if it is brighter. This is to reduce the number of iterations, and start the process off in an approximation of the final image.

To define a Voronoi cell, the program uses a structure that holds the \underline{site} of the cell, and a list of \underline{every single point} in that cell. The creation of the Voronoi diagram can be done using or not using the GPU. When using the GPU, the graph is discretely approximated using the cones generating method that we covered in class, then find the density center of each cell and relocating it. If the user decides against using the GPU, the method generates a Voronoi diagram, considering the color intensities of each pixel while doing so. First stipples are added to a vector, paired up with their color intensity, then the vector is sorted into a priority queue (which is very time consuming), and then propagates every pixel, spreading from every site to it's neighbors, to find out which site each pixel belongs to.

Finally, the method moves the sites of each cell by computing the centroid of each cell. To find that point, we find the weighted average of the color intensities of every pixel in each cell. The whole process of the hedcuter method is terminated either based on the average displacement of the center of each cell, or just the maximum displacement. Depending on user input, when one of these desired numbers is optimally small, the stipples are considered converged. and the process is over.

\subsection{voronoi method}

Abstract: first create an initial distribution of stipples, again with bias towards darker regions, then find the Voronoi diagram treating the stipples as sites of the diagram, then finding the center of each cell using a density function, and moving each stipple to that center, repeating these steps until the stipples converge to the center of each cell permanently.

Similar to the first method, the first step is to generate n points, with biased towards the darker regions of the picture. The only difference of this method compared to the first method, is that this algorithm uses the generator function from Boost to generate a random point and randomly discard that point. Similar to the hedcut algorithm, It is much more likely for a point to get discarded, if the point is chosen from the lighter parts of the picture.

Generating the Voronoi diagram in this method is very straight forward, using the Fortune's algorithm (or plain sweep) to find the cells. The fundamental difference being that in this method, we define each Voronoi cell as by the \underline{site} and the \underline{edges of the cell}. After that, same as the last method, we add all the pixels falling with in the bounds of a group of edges, to be considered inside of the correctly corresponding cell.

 Calculating the new centroid of each cell in this algorithm is much more complex. Using a function called 'create clipLine', the algorithm finds all the line formulas of the edges of each cell. Then, finding an imaginary bounding box for each cell, we select a number of points (Based on the default of inputted subpixel number), and only consider these points with of the cells. We approximate the intensity of the cell based on the intensity of these points (as opposed to what we did in the previous method where we calculated the intensity of every pixel), and find the average of those pixels to be the new centroid of the cell.

\newpage\section{Comparison of the two methods}
On average, the hedcut method has a clear disadvantage on speed. Since there is no definite number of iterations before the distance converges to smaller numbers, the calculations can continue for random amounts of time. Yet, there is little competition in terms of which methods would finish first. Since I am a Windows user, I was forced to compile the code on a virtual Linux Ubuntu machine; which made the running time even longer. So I excuse my self from measuring actual time.

We will answer multiple questions regarding the quality of these algorithms, but first lets have a side by side view of some of the outputs of these methods:
\newline\includegraphics[width=5cm, height=8cm]{klaymen.png}
\includegraphics[width=5cm, height=8cm]{klaymen-hedcut-20000.png}
\includegraphics[width=5cm, height=8cm]{klaymen-voronoi-20000.png}
\newline Image 1: Klaymen from left to right Original - Hedcut - Voronoi 20,000 stipples
\newline
\newline\includegraphics[width=5cm, height=4cm]{phoenix.png}
\includegraphics[width=5cm, height=4cm]{phoenix-hedcut-2000.png}
\includegraphics[width=5cm, height=4cm]{phoenix-voronoi-2000.png}
\newline Image 2: Phoenix from left to right Original - Hedcut - Voronoi 2,000 stipples
\newline
\newline\includegraphics[width=6cm, height=5cm]{libertyhall.png}
\includegraphics[width=6cm, height=5cm]{libertyhall-hedcut-20000.png}
\includegraphics[width=6cm, height=5cm]{libertyhall-voronoi-20000.png}
\newline Image 3: Liberty hall from left to right Original - Hedcut - Voronoi 20,000 stipples
\newline
\newline\includegraphics[width=6cm, height=5cm]{landscape.png}
\includegraphics[width=6cm, height=5cm]{landscape-hedcut-20000.png}
\includegraphics[width=6cm, height=5cm]{landscape-voronoi-20000.png}
\newline Image 4: Random landscape from left to right Original - Hedcut - Voronoi 20,000 stipples
\newline
\newline\includegraphics[width=6cm, height=4.5cm]{sanders.png}
\includegraphics[width=6cm, height=4.5cm]{sanders-hedcut-10000.png}
\includegraphics[width=6cm, height=4.5cm]{sanders_voronoi_10000.png}
\newline Image 5: Bernie Sanders from left to right Original - Hedcut - Voronoi 10,000 stipples

\textbf{Question 1.} Do you get the same results by running the same program on the same image multiple times?

\textbf{Answer:} No. Every time the images look slightly different. This is due to the randomized nature of the initial stipple points.

\textbf{Question 2.} If you vary the number of the disks in the output images, do these implementations produce the same distribution in the final image? If not, why?

\textbf{Answer:} Well points always gravitate towards darker areas. So the image never looks completely different or the same  given different number of disks. The Voronoi method actually by default adapts, by changing the size of these disks to fill out more area. Which makes the images more similar to the images of the print outlets publishing hedcuts.

\textbf{Question 3.} If you vary the number of the disks in the output images, is a method faster than the other?

\textbf{Answer:} This depends on how much we reduce the number of disks in comparison to the other method. As mentioned, the second method usually concludes much faster than the first method. So to reduce the number of stipples on the first method to the point that it becomes faster than the second method, would also reduce the quality of the outputted image to the point where it is basically unrecognizable. So technically yes... it just would look like a few points on a blank page.

\textbf{Question 4.} Does the size (number of pixels), image brightness or contrast of image increase or decrease their difference?

\textbf{Answer:} The size does not, because you need to find the magic number of disks that covers all of the screen anyways. But the brightness does make a fairly important difference. An image with a dark background yields results surprisingly faster using the first algorithm (Faster than it does on a brighter image, but still not as fast as the second algorithm). In this example, image 5 converged in 28 iterations on hedcut, where image 2 did not converge until it ran out of maximum iterations. So unless the code has a political bias hard coded, I think the darker the image, the faster the hedcut code converges.

\textbf{Question 5.} Does the type of image (human vs. machine, natural vs. urban landscapes, photo vs. painting,etc) increase or decrease their difference?

\textbf{Answer:} As can be seen, I have made an attempt to address this question with my choice of output images. Though I honestly doubt that the humanity of the subject of a picture is going to make any kind of a significant impact, I have images that illustrate the difference in quality between drawn vs. photographed, indoor vs. outdoor, single subject vs. multiple objects, and predominantly light vs predominantly dark. As was mentioned before, the darker the image, the faster the computation time for the hedcut and even Voronoi. The scenery images seem to perform worse than single subject images, due to the variations of the colors (contrasts), and the usual mandate of them to be lighter overall.

As can be seen, in all of these cases, with the same amount of stipple points, we more often than not get better results with the Voronoi method. The dynamic sizing of the disks seems to suggest a higher level of sophistication, which allows the user to not need to struggle to find the best number of stipples, regardless of the type and size of the image.

\textbf{Question 6.} Are the outputs of these stippling methods different from the hedcut images created by artists (e.g.those from the Wall Street Journal)?

\textbf{Answer:} To address this question it is only fair to consider the single subject images, and not the scenery. And even then... I would say no. Neither of these methods perform as well as a real artist. Mostly, because an artist makes good design decisions, deciding whether to include some background elements or not, and to emphasize certain parts of the picture more, even though they are not in reality darker than the others. The first method is particularly bad at this, since when too many points are used, the image completely breaks the structure of standalone points, turns into patches of connected sections.

Again the fact that the Voronoi method dynamically sizes the points by default comes at a major advantage here, and makes these images look like the real inspirations for the hedcut artist. The images output by the second algorithm, look like actual old timely printed images, rather than an artists imitation of those prints.

\section{Improvement of hedcuter method}

\textbf{Improvement 1}- Better Initial Stippling:

As was mentioned in "Weighted Voronoi stippling" paper, if we start out with a better approximation of the final answer, the faster will we conclude our stippling process. Which is why in both methods there are mechanisms to initialize the stipples with a major bias towards darker regions of the images.

However, when the hedcut method runs in debug mode, it is immediately obvious that this is hardly the case, since the initial distribution still is allowing for too many points to be picked out of the lighter parts of the images, as you can see from this screen grab:
\newline

\includegraphics[width=12cm, height=8cm]{Pheonix-Debug-Normal.png}
\newline Image 6: Phoenix using 2,000 stipples, no changes

As can be seen, even though the points are much more likely to be in the colored areas, there are still way too many points in the blank areas of the picture. It is in our interest to reduce this likely hood as much as possible. The hedcuter code uses a Gaussian distribution to bias the points being picked. I had the idea to use an exponential distribution to bias the points in favor of being chosen from the darker sides. That way, the lighter points are still likely to be chosen, but the lighter the points, the exponentially less likely they would be to be picked as a stipple point.

So as my first improvement, I have re-written the "sample\_initial\_points" function to bias the stippling with exponential likely hoods. The "std::exponential\_distribution" function demands a standard deviation, and experimenting with that, I found the best number to be 7 (which is the default of my code now). But I have also allowed the user to be able to pick this number.

\includegraphics[width=12cm, height=8cm]{3point5Phoenix.png}
\newline Image 7: Phoenix using 2,000 stipples, exponential distribution at 3.5

\includegraphics[width=12cm, height=8cm]{5Phoenix.png}
\newline Image 7: Phoenix using 2,000 stipples, exponential distribution at 5

\includegraphics[width=12cm, height=8cm]{7phoenix.png}
\newline Image 7: Phoenix using 2,000 stipples, exponential distribution at 7

As can be seen, this method proved to be very effective. This reduces the computation time reasonably, since the initial estimation is already close to the final image. As was mentioned, I have made it so the user can change this deviation variable by typing -deviation followed by a floating point number in the command line while running the code. Though \underline{the number must be smaller than 10, and using a larger number will case the program to fall in an infinite loop.}

\textbf{Improvement 2}- Dynamic sizing and Collision avoidance: (Feel free to count this as 2 improvements!)

A fairly big advantage of the Voronoi method, was the fact that it could dynamically decide the size of every Voronoi cell. The way that method was able to do this, was by finding the distance of the centroid from each edge of the cell, and picking the longest or the shortest distance depending if overlapping was allowed or not. In the hedcut method, our data structure does not hold the edges of each cell, so we need to find another way of deciding the radius of the disks.

To find the actual edges of each cell, we would need a convex haul, which would take O(n.log(n)) time. We would use plain sweep, and find the edge, and be able to find each of the radiuses. Even though that is an efficient strategy, this algorithm already takes far too long to compute, and we already have a lot of memory occupied. So I came up with an O(n) time algorithm to give an educated guess as to what the radius of the cell should be.

So, in my improvement, I sweep over the coverage list (the list of all the pixels in a cell), and find the points on the edges directly to the top, bottom, left and right of the centroid. Using these points we have 2 guesses as to what the radius can be, which are the distance of the points across from each other. The larger of these points can be for when we don't mind collisions, and the smaller one, for when we do want to avoid collisions.

Now I am under no illusions; this method is not guaranteed or optimal. It is very much possible to pick the smallest of these distances and still end up colliding with other cells. But it is an educated guess, with less time spent trying to find. As with the last improvement, I have added a command "-noCollisions", that would allow the user to control which one of these radiuses should be used.

Here are a couple of new outputs. To underscore the helpfulness of these changes, I am comparing 2 images that would have been done terribly if it hadn't been for dynamic sizing. Before the dynamic size, I made the disk size of these images too small. Observe how the dynamic sizing overrides the disk size, when the uniform-disk-size variable is false, to give a better approximation of the images:

\includegraphics[width=5cm, height=8cm]{klaymen-old-2000.png}
\includegraphics[width=5cm, height=8cm]{klaymen-new-2000.png}
\newline Image 8: Klaymen statically, vs Dynamically sized, with 2000 stipples

\includegraphics[width=8cm, height=6.5cm]{sanders-old-10000.png}
\includegraphics[width=8cm, height=6.5cm]{sanders-new-10000.png}
\newline Image 9: Bernie Sanders statically, vs Dynamically sized, with 10000 stipples

This improvement still does not give results as good as the Voronoi method, but the results after this improvement look more similar to the Voronoi method than they used to.

\bibliographystyle{plain}
\bibliography{report}

\end{document}


\chapter{C Syntax}
\label{app:c-syntax}

This appendix contains a definition of the relevant parts of C Syntax
for this report. This is based on the C11 Specification, but written
with mathematical symbols rather than ASCII text.

\begin{align*}
  \text{LValue Expressions} \\*
  e_l \gis {} & x  \tag*{Variables} \\
  \gor {} & \deref e_v \tag*{Dereference} \\
  \gor {} & e_v[e_v] \tag*{Index} \\
  \gor {} & e_l \member m \tag*{Struct Member Access} \\
  \gor {} & e_v \arrow m \tag*{Pointer Member Access} \\[1em]
%
\text{Value Expressions} \\*
  e_v \gis {} & \addrof e_l \tag*{Address-of} \\
  \gor {} & e_l \assign e_v \tag*{Assignment} \\
  \gor {} & e_l \compassign e_v \tag*{Compound Assignment} \\
  \gor {} & e_l \inc \mathrel{\mid} \inc e_l \tag*{Increment} \\
  \gor {} & e_l \dec \mathrel{\mid} \dec e_l \tag*{Decrement} \\
  \gor {} & e_v \member m \tag*{Struct Member Access} \\
  \gor {} & e_v , e_v \tag*{Comma Expression} \\
  \gor {} & e_v \binop e_v \tag*{Binary Operation} \\
  \gor {} & e_v \mathrel{R} e_v \tag*{Binary Relational Operation} \\
  \gor {} & \unop e_v \tag*{Unary Operation} \\
  \gor {} & e_v(\overline{e_v}) \tag*{Function Call} \\
  \gor {} & (t)e_v \tag*{Cast} \\
  \gor {} & e_v \mathrel{?} e_v : e_v \tag*{Conditional Operator} \\
  \gor {} & l \tag*{Literal Value} \\
  \gor {} & e_l \tag*{Lvalue \& Array Conversion} \displaybreak\\[1em]
  %
  \text{Binary Operators} \\*
  \binop \gis {} & + \tag*{Addition} \\
  \gor {} & - \tag*{Subtraction} \\
  \gor {} & * \tag*{Multiplication} \\
  \gor {} & / \tag*{Division} \\
  \gor {} & \% \tag*{Modulus} \\
  \gor {} & \& \tag*{Bitwise AND} \\
  \gor {} & \mid \tag*{Bitwise OR} \\
  \gor {} & \wedge \tag*{Bitwise XOR} \\
  \gor {} & \gg \tag*{Bitwise Shift Right} \\
  \gor {} & \ll \tag*{Bitwise Shift Left} \\[1em]
  %
  \text{Relational Operators} \\*
  R \gis {} & \&\& \tag*{Logical AND} \\
  \gor {} & \mid\mid \tag*{Logical OR} \\
  \gor {} & \mathord{==} \tag*{Equality} \\
  \gor {} & \mathord{!=} \tag*{Negated Equality} \\
  \gor {} & \mathord{>} \mathrel{\mid}
             \mathord{\ge} \mathrel{\mid}
             \mathord{<} \mathrel{\mid}
             \mathord{\le} \tag*{Comparison} \\[1em]
  %
  \text{Unary Operators} \\*
  \unop \gis {} & + \tag*{Unary Plus} \\
  \gor {} & - \tag*{Numerical Negation} \\
  \gor {} & \sim \tag*{Bitwise Negation} \\
  \gor {} & ! \tag*{Logical Negation} \\[1em]
  %
  \text{Literals} \\*
  l \gis {} & \mathtt{NULL} \tag*{Null Pointer Literal} \\
  \gor {} & \mathrm{integers} \tag*{Integer Literals} \\
  \gor {} & \mathrm{floats} \tag*{Float Literals} \\
  \gor {} & \{ l , \dots \} \tag*{Array \& Struct Literals} \\[1em]
  %
  \text{Types} \\*
  t \gis {} & \dots \\[1em]
  %
  \text{Struct Members} \\*
  m \gis {} & \dots \\
\end{align*}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "tr02"
%%% End: