\chapter{Neutral Diversity and Population Structure.}
%% this section was moved from the coalescent chapter

How does genetic differentiation build up between closely related
populations? How does migration act to reduce differentiation? These
questions are key to understand the conditions under which populations
(and species) can start to genetically diverge from each other. To answer these
questions, we'll first consider this in the context of neutral alleles, and then return
to think about selection and differentiation in later chapters.
We've considered neutral alleles drawn from a randomly-mating population, and divergence among alleles drawn from two distantly-related populations.
We'll now turn to consider divergence among more closely related
populations. In thinking about the coalescent within populations we
made the assumption that any pair of lineages is equally likely to
coalesce with each other. However, when there is population structure
this assumption is violated, as the parent for an allele is
likely to be found in the same population as it's child and so
lineages in different populations are less likely to coalesce. \\

To develop models of about population structure we'll use the
statistic $\fst$, which we introduced in Section \ref{section:F_stats}
of discussion of summarizing
population structure in allele frequencies. We have previously written the measure of population structure
$\fst$ as
\begin{equation}
\fst = \frac{H_T-H_S}{H_T}
\end{equation}
where $H_S$ is the probability that two alleles sampled at random from a
subpopulation differ, and $H_T$ is the probability that two alleles
sampled at random from the total population differ.

\section{A simple population split model}
Imagine a population of constant size of $N_e$ diploid individuals that
$T$ generations in the past split into two daughter populations (sub-populations)
each of size $N_e$ individuals, which do not subsequently exchange
migrants. In the current day we sample an equal number of alleles
from both subpopulations.

\begin{figure}
\begin{center}
\includegraphics[width= 0.8 \textwidth]{figures/drift_split.png}
\end{center}
\caption{Change in allele frequencies following a population split. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Rcode/Loss_of_heterozyg_varying_pop.R}} \label{fig:drift_split}
\end{figure}

Consider a pair of alleles sampled within one of our
sub-populations and think about their per site heterozygosity.
These alleles have experienced a population of size $N_e$
and so the probability that they differ is $H_S \approx 4N_e \mu$ (assuming that $N_e \mu \ll 1$, using our equation  \ref{eqn:hetero} for heterozygosity within a population ).

%Consider a pair of alleles sampled within one of our
%sub-populations, they have experienced a population of size $N_e$
%and so the probability that they differ is $H_S = \theta/(1+\theta)$
%(where $\theta=4N_e\mu$).

The heterozygosity in our total population is a little more tricky to
calculate. Assuming that we equally sample both sub-populations, when we draw two alleles from our total
sample, $50\%$ of the time they are drawn from the same
subpopulation and $50\%$ of the time they are drawn from different
subpopulations. Therefore, our total heterozygosity is given by
\begin{equation}
H_T = \half H_S + \half H_B
\end{equation}
where $H_B$ is the probability that a pair of alleles drawn from our
two different sub-populations differ from each other. A pair of
alleles from different sub-populations cannot find a common ancestor with each other for at least $T$
generations into the past as they are in distinct populations (not
connected by migration). Once our alleles find themselves back in the combined ancestral
population it takes them on average $2N$ generations to coalesce. So the total opportunity for mutation between our pair of alleles sampled from different populations is $2 (T + 2N )$ generations of meioses, such that the probability that our pairs of alleles is different is
\begin{equation}
H_B \approx 2\mu ( T + 2 N) %\left( 1-(1-\mu)^{2T} \right) + (1-\mu)^{2T}
  %\frac{\theta}{\theta+1}
\end{equation}

%The probability that one or other of them
%mutates in this time is $1-(1-\mu)^{2T}$. With probability
%$(1-\mu)^{2T} $ neither of our alleles mutate in the $T$ generations
%back in time before they find themselves back in the combined ancestral
%population. Conditional on failing to mutate before the combined ancestral
%population, the probability that they do manage to mutate before
%coalescing in that population of size $N_e$ is
%$\theta/(\theta+1)$. Putting these components together
%\begin{equation}
%H_B = \left( 1-(1-\mu)^{2T} \right) + (1-\mu)^{2T}
 % \frac{\theta}{\theta+1}
%\end{equation}
We can plug this into our expression for $H_T$, and then that in turn
into $\fst$. Doing so we find that
\begin{equation}
\fst \approx \frac{ \mu T}{\mu T +  4N_e\mu }  = \frac{ T}{ T +  4N_e
} \label{eqn:FST_split}
\end{equation}
Note that $\mu$ cancels out of this equation. In this simple toy model, $\fst$
is increasing because the amount of between-population diversity
increases with the divergence time of the two populations (initially
linearly with $T$). $\fst$ grows at a rate
give by $\nicefrac{T}{(4N_e)}$ so that differentiation will be higher
between populations separated by long divergence times or with small
effective population sizes.

\begin{marginfigure}
\begin{center}
\includegraphics[width= 0.8 \textwidth]{illustration_images/Genetic_drift/Orang/7971889392_d32b45668b_z.jpg}
\end{center}
\caption{Orangutan ({\it Pongo}). \BHLNC{Brehms thierleben, allgemeine kunde des thierreichs. Brehm, A. E.}{https://www.biodiversitylibrary.org/page/1008013\#page/114/mode/1up}{MBLWHOI Library}} \label{fig:Orang}
\end{marginfigure}

\begin{question}{}  %Pongo abelii (Sumatran) and Pongo pygmaeus
  The genome-wide $F_{ST}$ between Bornean and Sumatran orangutan species samples  ({\it Pongo pygmaeus} and {\it Pongo abelii}) is $\approx 0.37$ \citep{locke2011}, representing a deep population split between the species (potentially with little subsequent gene flow). Within the populations the genome-wide average Watterson's $\theta$ is $\theta_W=1.4$kb$^{-1}$, estimated from the number of segregating sites. Assume a generation time of 20 years, and a mutation rate of $2 \times 10^{-8}$ per base per generation. How far in the past did the two populations diverge?
\end{question}
%o understand this better we can make a simple
%approximation based on our mutation rate being very low, such that
%$N_e \mu \ll 1$ so $H_S \approx
%4N_e\mu$, and that $\mu \ll 1$ and $\mu T \ll 1$. Assuming this, then
%\begin{equation}
%H_B \approx 2 \mu T + 4N_e\mu.
%\end{equation}

%\begin{question}
%The gorilla lineage split from the human-chimp lineage $\sim$7 million years ago. Let’s assume that this speciation event occurred instantaneously in allopatry with no subsequent gene flow. \\
%{\bf A)}	What is the probability of that gorilla is not an outgroup to human and chimp at a single locus?\\
%{\bf B)}	It has been estimated that the gorilla lineage is not an outgroup at around ~30\% of autosomal loci. What effective population size would you need to assume to explain this observation? Is that only plausible explanation?\\
%{\bf C)}	The gorilla lineage is an outgroup for large portions of the X chromosome, what is a plausible explanation for this finding?
%\end{question}

\section{A simple model of migration between an island and the mainland.}
We can also use the coalescent to think about patterns of
differentiation under a simple model of migration-drift
equilibrium. Let's consider a small island population that is relatively isolated
from a large mainland population, where both of these populations
are constant in size. We'll assume that the expected heterozygosity
for a pair of alleles sampled on the mainland is $H_M$.

Our island has a population size
$N_{I}$ that is very small compared to our mainland population.
Each generation some low fraction $m$ of our individuals on the
island have migrant parents from the mainland the generation
before. Our island may also send migrants back to the mainland, but
these are a drop in the ocean compared to the large population size on
the mainland and their effect can be ignored.

If we sample an allele on the island and trace its ancestral
lineage backward in time, each generation our ancestral allele has a low
probability $m$ of being descended from the mainland in the preceding
generation (if we go back far enough the allele eventually has to be
descended from an allele on the mainland). The probability that a pair of alleles sampled on the
island are descended from a shared recent common ancestral allele on the island is the
probability that our pair of alleles coalesces before either lineage
migrates. Well our pair of lineages coalesce with probability
$\nicefrac{1}{2N_I} $ in a given generation and, assuming that the rate of migration is not
too high ($m \ll 1$), the probability that one or other lineage
migrates in a given generation is $2m$. So the probability that our
lineages coalesce before they migrate is
\begin{equation}
\frac{\nicefrac{1}{(2N_I)}}{\nicefrac{1}{(2N_I)} +   2m},
\end{equation}
which follows as an exactly analogous argument to our probability that a pair of
lineages coalesce before a mutation, \eqn \ref{eqn:coal_no_mut}, that we used in deriving the
expected heterozygosity.

%For example, the probability that our pair of alleles
%coalesces $t+1$ generations back on the island is
%\begin{equation}
%\frac{1}{2N_I}(1-m)^{2(t+1)} \left(1-\frac{1}{2N_I} \right)^{t} \approx
%\frac{1}{2N_I} \exp\left( -t\left (\frac{1}{2N_I} + 2m\right) \right),
%\end{equation}
%JRI: this is a long scary-looking equation. i think it's pretty straightforward but even I was momentarily surprised by it. i think many readers will see it and tune out and not try to work through it. i suggest you keep it but spend a few sentences working through it (1-m) not migrating times prob. of coalesceing t gens, etc.
%with the approximation following from assuming that $m \ll 1$ \& $\frac{1}{(2N_I)}
% \ll 1$ (note that this is very similar to our derivation of
% heterozygosity above). The probability that our alleles coalesce before either one
% of them migrates off the island, irrespective of the time, is
% \begin{equation}
% \int_0^{\infty} \frac{1}{2N_I} \exp\left( -t\left (\frac{1}{2N_I} +
% 2m\right) \right) dt = \frac{\nicefrac{1}{(2N_I)}}{\nicefrac{1}{(2N_I)} +
%     2m}.
% \end{equation}

% Let's assume that the mutation rate is very low such that it is very
% unlikely that the pair of alleles mutate before they coalesce on the
% island. Therefore, the only way that the alleles can be different from
% each other is if one or other of them migrates to the mainland, which
% happens with probability

Conditional on one or other of our alleles migrating to the mainland, both of our alleles represent independent draws from the mainland and so differ from each other with probability $H_M$. Therefore, the level of
heterozygosity on the island is given by
\begin{equation}
  H_I = \left(1 - \frac{\nicefrac{1}{(2N_I)}}{\nicefrac{1}{(2N_I)} + 2m} \right)H_M
\end{equation}
So the reduction of heterozygosity on the island compared to the
mainland is
\begin{equation}
  F_{IM} = 1- \frac{H_I}{H_M} = \frac{\nicefrac{
      1}{(2N_I)}}{\nicefrac{1}{(2N_I)} + 2m} = \frac{ 1 }{1 + 4N_Im}. \label{eqn:FIM}
\end{equation}
The level of inbreeding on the island compared to the mainland will be
high if the migration rate is low and the effective population size of
the island is low, as allele frequencies on the island are drifting
and diversity on the island is not being replenished by migration. The
key parameter here is the number individuals on the island replaced by
immigrants from the mainland each generation ($N_I m$), even a few
migrants arriving on the island a generation is enough to prevent much
allele frequency differentiation building up.

We have framed this problem as being about the reduction in genetic diversity on the island compared to the mainland. However, if we consider collecting individuals on the island and mainland in proportion to their population
sizes, the total level of heterozygosity would be $H_T=H_M$, as samples from our mainland would greatly outnumber those from our island. Therefore, considering the island as our sub-population, we have derived another simple model of $F_{ST}$.

\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.\\
What is your estimate of the migration rate from the marine
populations into the river?
\end{question}

\begin{marginfigure}
\begin{center}
\includegraphics[width=\textwidth]{illustration_images/Genetic_drift/stickleback/threespine.jpg}
\end{center}
\caption{Three-spined stickleback ({\it Gasterosteus aculeatus}).
  \newline \noindent \tiny{Jordan, David Starr (1907) Fishes, New York City, NY: Henry Holt and Company. Image from  \href{https://commons.wikimedia.org/wiki/File:FMIB_51889_Three-spined_Stickleback,_Gasterosteus_aculeatus_L_Woods_Hole,_Mass.jpeg}{Wikimedia Commons} Public domain.}} \label{fig:stickle}
\end{marginfigure}

\section{Incomplete Lineage Sorting}
%Finally we turn to the interaction of the coalescent and
%speciation and population splits. %%this is a guess
Often when we're studying multiple populations, e.g. species, we're interested in the
underlying order in which populations split off from each other, and
the timing of these events. In the case where populations split off
from each other with no subsequent gene flow, we can represent this
pattern of splitting by a population tree. Because it can take a long time for a polymorphism to drift up or down in frequency, multiple population splits may occur during the time an allele is still segregating. This can lead to incongruence between the overall population tree and the information about relationships present at
individual loci.
As we have seen in the previous chapters the
relationships between sampled alleles at a locus are represented by
coalescent tree, sometimes call gene trees in the context of
incomplete lineage and more generally in phylogenetics.
In Figure \ref{fig:NoILS_poly} and \ref{fig:ILS_poly}
we show a simulation of three populations where the bottom population splits off from the other two first, followed by the subsequent splitting of the the top and the middle populations. We start both simulations with a newly
introduced red allele being polymorphic in the combined ancestral population. The most likely fate of this allele is that it is
quickly lost from the population, but sometimes the allele can drift up in frequency and be polymorphic when the populations split, as the allele in our two figures has done. If the allele is lost/fixed in the descendant populations before the next population split, our allele
configuration will agree with the population tree, as it does in
Figure  \ref{fig:NoILS_poly}, and so too the gene tree will agree with population tree (as shown in the left side of Figure \ref{fig:ILS_cartoon}).
However, if the allele persists as a polymorphism in the ancestral population until the top and the middle
populations split, then the allele could fix in one of these populations
and not the other. Such an event leads to a substitution pattern
that disagrees with the population tree, as in Figure \ref{fig:ILS_poly}.  If
we were to construct a phylogeny using the variation at this site we would see a disagreement between the gene tree and population tree. In Figure \ref{fig:ILS_poly} an allele
drawn from the top and the bottom populations are
necessarily more closely related to each other than either is to an allele drawn from population 2;
tracing our allelic lineages from the top and bottom populations back through time, they must coalesce with each other before we reach the point where the red mutation arose; in contrast, a lineage from the middle population cannot have coalesced with either other lineage until past the time the red mutation arose. An example of this `incomplete lineage sorting' in terms of the underlying tree is shown on the right side of Figure \ref{fig:ILS_cartoon}.

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{figures/Genetic_drift/ILS/no_ILS.pdf}
\end{center}
\caption{An example of alleles assorting among three populations such that there is no incomplete lineage sorting. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Rcode/Loss_of_heterozyg_varying_pop.R}} \label{fig:NoILS_poly}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{figures/Genetic_drift/ILS/ILS.pdf}
\end{center}
\caption{An example of alleles assorting among three populations leading to incomplete lineage sorting. \gitcode{https://github.com/cooplab/popgen-notes/blob/master/Rcode/Loss_of_heterozyg_varying_pop.R} } \label{fig:ILS_poly}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{figures/Genetic_drift/ILS/ILS_coal_cartoon.pdf}
\end{center}
\caption{The population tree of three populations ((A, B), C), as the
  white space blocked out between the black shapes. Two different coalescent trees
relating a single allele drawn from A, B, and C are shown with thinner lines.} \label{fig:ILS_cartoon}
\end{figure}

A natural pedigree analogy to incomplete lineage sorting is the fact that while two
biological siblings are more closely related to each other genealogically than
either is to their cousin, at any given locus one of the siblings can
share an allele IBD with their cousin that they do not share with
their own sibling, due to the randomness of Mendelian segregation down their
pedigree. In these cases, the average relatedness of the individuals/populations disagrees
with the patterns of relatedness at a particular locus.

%\begin{question}
%Draw a pedigree of two full cousins, where one of the cousins has a full sibling. Label the four alleles in shared grandparents 1-4, and draw the situation where the cousins share 1 allele IBD, and the sibs share 0 alleles IBD.
%\end{question}
%This analogy isn't perfect as full cousins dont share one parents, while all three of our populations share one ancestral, parental population far enough back. If we want to make this analogy more perfect we'd need to posit a bunch of inbreeding, but honestly drawinf that would make my head hurt a lot.

%black-throated finch (Poephila cincta https://twitter.com/BioDivLibrary/status/1195096274048937984

\begin{marginfigure}
\begin{center}
\includegraphics[width=\textwidth]{illustration_images/Genetic_drift/Poephila_cincta_finch/Poephila_cincta_finch.png}
\end{center}
\caption{Banded grass Finch ({\it P. cincta}). Illustration by
  Elizabeth Gould.
  \newline \noindent \tiny{Birds of Australia Gould J. 1840. CC BY 4.0 uploaded to \href{https://www.flickr.com/photos/vintage_illustration/41073432105/in/photolist-WpHYmR-WBF2Uc-Uoq2kW-QAW9bN-UL33uh-RmyMxt-PKmBjN-8xpKmQ-8xmHg2-H6mfAS-dVREvC-7Qepuh-222xkPQ-7QepzS-25zw6ak-W28Nyk-Qa6d32-ijh2uH-6V8Srd-25zw5ov-8xpKB7-nUQ33T-6V8TLy-nV88oH-25RuYmq-21TcFz1-R2Gdy4-GdWRgi-GdMmry-GdWAwD-9mjwfA-25oHWeJ-HMbQre-6CQWTH-7TjKu-e6SYV-25RuXU3-pdKtxc-9hYCzE-voj7Sj-zqSdQb-6SNmDE-a1datx-a1dasZ-PFfPXY-rR7QYw-GJ7rey-6SJmuB-7TjFh}{Flickr} by \url{rawpixel.com}.}} \label{fig:Poephila_cincta}
\end{marginfigure}

As an empirical example of incomplete lineage sorting, let's consider the work of
\citet{jennings:05} who sequenced a single allele from three
different species of Australian grass finches ({\it Poephila}): two sister species
of long-tailed finches ({\it Poephila acuticauda} and {\it P. hecki})
and the black-throated finch ({\it Poephila cincta}, see Figure \ref{fig:Poephila_cincta}). They collected
sequence data for 30 genes and constructed phylogenetic gene trees
at each of these loci, resulting in 28 well-resolved gene trees.
Sixteen of the gene trees showed {\it
  P. acuticauda} and {\it P. hecki} as sisters with {\it P. cincta})
(the tree ((A,H),C)~), while for twelve genes the gene tree was
discordant with the population tree: for seven of their genes  {\it P. hecki}
fell as an outgroup to the other two and at five {\it P. acuticauda} fell as an outgroup (the trees ((A,C),H) and
((H,C),A) respectively).

Let's use the coalescent to understand this discordance between gene
trees and species trees. Let's assume that two sister populations
(A \& B) split $t_1$ generations in the past, with a
deeper split from a third outgroup population (C) $t_2$ generations in the
past. We'll assume that there's no gene flow among our populations
after each split. We can trace back the ancestral lineages of our three alleles. The
first opportunity for the A \& B lineages to coalesce is $t_1$
generations ago. If they coalesce with each other in their shared
ancestral population before $t_2$ in the past (left side of
Figure \ref{fig:ILS_cartoon}) their gene tree will
definitely agree with the population tree. So the only way for the gene
tree to disagree with the population tree is for the A \& B lineages
to fail to coalesce in their shared ancestral population between $t_1$ and  $t_2$; this happens
with probability $\left(1 - \nicefrac{1}{2N}\right)^{t_2-t_1}$. We'll
get a discordant gene tree if A
\& B make it back to the shared ancestral population with C without
coalescing, and then one or the other of them coalesces with the C lineage before they coalesce with each other. This happens with probability $2/3$, as at the first
pairwise-coalescent event there are are three possible pairs of lineages that could coalesce, two of
which (A \& C  and B \& C ) result in a discordant tree. So the
probability that we get a coalescent tree that is discordant with the population tree is
\begin{equation}
\frac{2}{3} \left(1 - \nicefrac{1}{2N}\right)^{t_2-t_1}. \label{eqn:ILS_coal}
\end{equation}
This equation allows us to relate the fraction of loci showing
incomplete lineage sorting to the population genetics parameters of
the ancestral population.
%ancestral population
\begin{question}{}
Let's return to \citeauthor{jennings:05}'s Australian grass finches
example. They estimated that the ancestral population size of our two
long-tailed finches was four hundred thousand. What is your best
estimate of the inter-speciation time, i.e. $t_2-t_1$?
\end{question}

The fraction of loci showing ILS, eqn \eqref{eqn:ILS_coal}, depends on the times between
population splits ($t_2-t_1$) Thus we should expect gene-tree population-tree discordance when populations split in rapid succession and/or population sizes are large.

\paragraph{Testing for gene flow.}

We often want to test whether gene flow has occurred between populations. For example, we might want to establish a case
that interbreeding between humans and Neanderthals occurred or demonstrate that
gene flow occurred after two populations began to speciate.
A broad range of methods have been designed to test for gene flow and
to estimate gene flow rates based on neutral expectations. Here we'll briefly just discuss one method based on
some simple coalescent ideas.  Above we assumed that gene-tree population-tree discordance was due to
incomplete lineage sorting due to populations rapidly
splitting. However, gene flow among populations can also lead to gene-tree discordance.
While both ILS and gene flow can lead to discordance, under
simplifying assumptions, ILS implies more symmetry in how these
discordances manifest themselves.\\

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{figures/Genetic_drift/ILS/ABBA_BABA_coal.pdf}
\end{center}
\caption{ Incomplete lineage sorting between our single lineages sampled from populations 1, 2, and 3. Population 4 is a distant outgroup
such that the lineages from 1 through 3 always coalesce with each other before any coalesce with 4. The small dash on the branch indicates the mutation A$\rightarrow$B occurring, giving rise to the
ABBA or BABA mutational pattern shown at the bottom. } \label{fig:ABBA_BABA}
\end{figure}
%JRI: here and in text you refer to populations ABCD but figure is labelled ``species'' and 1234. I think ABCD for populations and AB for alleles is confusing. I don't know if you need A/B for alleles. Why not use A1A2 or just 1/2 as you have previously? sure you miss the fun mnemonic, but it's clearer for a novice reader

Take a look at Figure \ref{fig:ABBA_BABA}. In both cases the lineages from 1 and 2 fail to coalesce in
their initial shared ancestral population, and one or the other of them
coalesces with the lineage from 3 before they coalesce with each other. Each option is equally
likely; therefore the mutational patterns ABBA and BABA are equally
likely to occur under ILS, but differential gene flow will break the symmetry.  \sidenote{Here we have to assume no structure in the ancestral population.}

To test for this effect of gene flow, we can sample a sequence from each of our 4 populations and count up the number of sites that show the two mutational patterns consistent with the gene-tree discordance $n_{ABBA}$ and
$n_{BABA}$ and calculate
\begin{equation}
  \frac{n_{ABBA}-n_{BABA}}{n_{ABBA}+n_{BABA}} \label{eqn:ABBA_BABA}
\end{equation}
This statistic will have expectation zero if the gene-tree discordance
is due to ILS. If there is gene flow between between 2 and 3, that
excludes 1, see Figure \ref{fig:ABBA_BABA_introgression}, there will be an excess ABBAs and so the ABBA-BABA
statistic will be skewed positively (and conversely it'll skew negatively if gene
flow occurred between
3 into 1). In practice, whether this is significantly different from
zero is judged by constructing a  Z statistic with a standard error
found by recalculating the statistic on computationally resampled
dataset of large genomic windows.

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{figures/Genetic_drift/ILS/ABBA_BABA_introgression.pdf}
\end{center}
\caption{A similar scenerio to Figure \ref{fig:ABBA_BABA} but now gene
  flow has occured populations 2 and 3, as depicited by the white gap
  having opened up between 2 and 3. Under
  this model there is an excess of ABBAs,  as they can arise both by
  incomplete lineage sorting (left) and by the lineages moving between
  2 and 3 by gene flow and coalescing before the ancestral 1-2-3
  population (right). BABAs can still occur but only by incomplete
  lineage sorting as in the right side of Figure \ref{fig:ABBA_BABA}.} \label{fig:ABBA_BABA_introgression}
\end{figure}

The big cats ({\it Panthera }) clade is a recent radiation, with a
considerable amount of shared genetic variation still
segregating across the group. \citet{figueiro2017genome} examined
patterns of genomic divergence, incomplete lineage sorting, and gene
flow across this clade using  ABBA-BABA tests with a Domestic cat sequence as the
outgroup. One example, for snow leopard, tiger, and  lion is shown
below. Snow leopards and tigers are known more closely related to each other than
either is to lions. \citeauthor{figueiro2017genome} counted SNPs where snow leopard and lion
sequences shared a derived allele to the exclusion of tiger (ABBA)
and those where where the tiger and lion
sequences shared a derived allele to the exclusion of snow leopard
(BABA) and found:\\
\begin{tabular}{ccccc}
Snow leopard &Tiger &  Lion & Domestic cat & Counts \\
A & B & B & A &   1,434,106 \\
B & A & B & A &   1,250,134\\
\end{tabular}\\
\begin{marginfigure}
\begin{center}
\includegraphics[width=\textwidth]{illustration_images/Genetic_drift/Big_cat_ILS/Big_cat_ILS.pdf}
\end{center}
\caption{A simple schematic of the population history of snow leopard
  ({\it Panthera uncia}), tiger ({\it Panthera tigris},
  and lion ({\it Panthera leo}) species. The arrow shows gene flow. \BHLNC{Images cropped from: The game animals of India, Burma, Malaya, and
    Tibet (1907). Lydekker, R}{https://www.biodiversitylibrary.org/ia/gameanimalsofind00lyde/\#page/311/mode/1up}{Smithsonian Libraries} } \label{fig:Big_cat_ILS}
\end{marginfigure}
The calculated ABBA-BABA
statistic, \eqn \eqref{eqn:ABBA_BABA}, is $0.07 \pm 0.0026~s.e.$,
which  is highly significantly different from zero.  The direction of
this statistics with a strong excess of derived SNPs where the tiger sequence is closer to
the lion sequence than snow leopard, is consistent with gene flow
between tigers and lions after tigers split off from snow
leopards (Figure \ref{fig:Big_cat_ILS}). Historically, lions had a large geographic range, and
so this interbreeding deep in the past is plausible.

%Tiger Snow leopard Lion 1,250,134 1,434,106 -0.07 -0.07 0.0026 -26.0

% Lion Jaguar Snow leopard 1,219,639 1,921,731 -0.22 -0.22 0.0043 -52.5 0

%https://twitter.com/arkivist/status/1187049284992143360

%% Isolation by distance
% https://en.wikipedia.org/wiki/Tobler%27s_first_law_of_geography
%  Waldo Tobler, is "everything is related to everything else, but near things are more related than distant things."

\begin{ChapterSummary}
\item We developed simple models of neutral population structure and
  developed expectations of allele frequency differentiation as
  measured by $\fst$ under these models.
\item Under a simple model of  population isolation, allele frequency
  differentiation builds up due to genetic drift in proportion to the
  split time divided by the population size.
  \item Only a small number of migrants between populations per generation is sufficient
    to prevent the build up of neutral allele frequency
    differentiation.
  \item Incomplete lineage sorting of ancestral variation is one source of disagreement
    between population/species-trees and gene trees. It occurs when the split times between
    populations are in quick enough succession that lineages do not
    have time coalese between more closely related populations.
    \item Gene flow can also lead to patterns similar to incomplete
      lineage sorting. We can test between a model of incomplete
      lineage sorting and gene flow using tests such as ABBA-BABA.
\end{ChapterSummary}

\begin{question} {}
You are studying a two species of fish (red fish \& blue fish), and
sequencing a set of pseudogenes.  Here are some facts you've collected:
 \begin{itemize}
 \item A third species of fish (black fish) diverged from
 the common ancestor of red/blue fish 3 million years ago.
 Assume 1 fish generation per year. Between red fish and black fish there is
 on average 1 substitution every 100 basepairs.
 \item In these pseudogenes, within red fish, you estimate that heterozygosity within
 red fish is $10^{-4}$ per basepair.
 \item $F_{ST}$ between red fish and blue fish is 0.1.
 \item There has been no gene flow among any of these species after
   they split.
 \end{itemize}
{\bf A)} What is the per base mutation rate?

{\bf B)} What is the effective population size of red fish?\\

{\bf C)} When did the red and blue fish populations split? Assume they
have equal population sizes.
\end{question}

  \begin{marginfigure}
  \begin{center}
    \includegraphics[width= \textwidth]{figures/Genetic_drift/ILS/ABBA_chimp_out.pdf}
  \end{center}
  \caption{ A simple population tree diagram, not to scale, of human
    populations and Neanderthals. Assume, for the sake of the question, that there is no gene flow
between populations after they split. Assume that the African and European populations split $\sim$100 thousand years
ago ($t_{OA}$). Neanderthals split from the modern human populations $\sim$700
thousand years ago ($t_N$). The population ancestral to humans and
chimps split 6.5 Million years ago ($t_C$).} \label{fig:ABBA_Neanderthal}
\end{marginfigure}

\begin{question}{}
With reference to the population tree shown in Figure \ref{fig:ABBA_Neanderthal}:
{\bf A)} On the population tree the dashed lines show an
incomplete gene phylogeny (for a single allele drawn from each population). At a locus, the Chimp lineage has the A allele. Complete
a gene genealogy in a way that would be consistent with Neanderthal and European lineages sharing a derived B allele, to the
exclusion of the African lineage (ABBA). Mark the branch that a mutation from $A
\rightarrow B$  must occur on in order to generate this pattern
(assuming a single mutation).\\
{\bf B)} What is the probability of observing a gene tree consistent
with the one you drew in part A under the coalescent model? Hint:
Remember that incomplete lineage sorting is due to failing to coalesce
within an ancestral population. \\
Assume a generation time of 30 years, and an
effective population size of 10,000 in all populations. Further, assume that lineages
sampled from the
Neanderthal and modern human populations will definitely coalesce with
each other before the common ancestral population with chimp.
\end{question}

%%D stats question in goose https://avianhybrids.wordpress.com/2019/11/09/d-statistics-for-dummies-a-simple-test-for-introgression/


\documentclass[twoside]{IEEEtran}

\usepackage{amsmath}
\usepackage{tikz}

\bibliographystyle{IEEEtran}

\newcommand{\mult}{\nonscript\:}
\newcommand{\opoff}{{\operatorname{off}}}
\newcommand{\opout}{{\operatorname{out}}}
\newcommand{\opth}{{\operatorname{th}}}
\newcommand{\optotal}{{\operatorname{total}}}
\newcommand{\opx}{{\operatorname{x}}}

\begin{document}

\title{An Extension of the Construction of\\Power Conservative Equivalent Circuits for\\DC Networks containing Current Sources}
\author{Julian Ahrens}
\maketitle

\section{Introduction}
\label{section_introduction}

By the Th\'{e}venin Theorem, any electrical network between two terminals, containing only voltage sources, current sources, and resistors, can be replaced by an equivalent circuit consisting of a voltage source $V_{\opth}$ and a series resistance $R_{\opth}$.
However, the power dissipated by the series resistance is not necessarily the same as the combined power dissipated by the resistors of the original network.
In a recent paper~\cite{Bar20}, it was shown that, in the case where the original network does not contain any current sources, a \emph{power conservative} equivalent circuit can be constructed.
The term `power conservative' refers to the property of the equivalent circuit, that it exhibits the same amount of Joule heating as the original network under all conditions, i.e., the total power dissipated by the resistors of the equivalent circuit is the same as the total power dissipated by the resistors of the original network.
In the following, an extension of this construction to the case of networks containing both voltage and current sources is developed.
The construction is divided into two parts: in Section~\ref{section_power} an analysis of the power dissipated in the original network is performed and in Section~\ref{section_circuit} the equivalent circuit is introduced and the values of its elements determined.

\section{Power dissipation in a general network}
\label{section_power}

\begin{figure}
  \centering
  \input{figures/loaded_network.tex}
  \caption{Electrical network with current load}
  \label{figure_loaded_network}
\end{figure}

For the analysis of the power dissipated by the resistors in an electrical network consisting only of voltage sources, current sources, and resistors, a current load $I_{\opout}$ is attached to the two terminals of the network as shown in \figurename~\ref{figure_loaded_network}.
In the following, the voltage across this load is denoted by $V_{\opout}$.
Furthermore, let $R_{k}$, $k = 0, \ldots, n - 1$, denote the values of the resistors in the network and let $V_{\opth}$ and $R_{\opth}$ denote the values of the voltage source and resistor of the Th\'{e}venin equivalent of the network, respectively.
The output voltage of the network is thus given by
\begin{equation}
    V_{\opout}
  = V_{\opth} - I_{\opout} \mult R_{\opth}
  \enskip .
  \label{eq_v_out}
\end{equation}

In order to simplify the analysis, only the case where $R_{\opth}$ is non-zero will be treated.
The case where $R_{\opth}$ is zero is fairly simple as in that case the power dissipated in the resistors of the network is independent of the load current $I_{\opout}$.

Now examine a modified version of the network where all the sources are switched off, i.e., all voltage sources are replaced by a short circuit and all current sources are replaced by an open circuit.
The current through each of the resistors $R_{k}$ in this modified network is proportional to the current flowing through the terminals of the network, i.e., for each $k$, there exists a unique constant $c_{k}$ such that the current through the resistor $R_{k}$ is equal to $I_{k, \opoff} = I_{\opout} \mult c_{k}$.
In particular, the power dissipated by $R_{k}$ is equal to
\begin{equation}
    P_{k, \opoff}
  = I_{k, \opoff}^{2} \mult R_{k}
  = (I_{\opout} \mult c_{k})^{2} \mult R_{k}
  \enskip .
  \label{eq_p_k_off}
\end{equation}
Furthermore, since the modified network consists purely of resistors and its equivalent resistance is, by the Th\'{e}venin theorem, equal to $R_{\opth}$, the total power dissipated by the entire network is given by
\begin{equation}
    P_{\optotal, \opoff}
  = I_{\opout}^{2} \mult R_{\opth}
  \enskip .
  \label{eq_p_total_off}
\end{equation}
Now since the total power dissipated by the entire network has to be equal to the sum of all the power dissipated in each individual resistor, combining \eqref{eq_p_k_off}~and~\eqref{eq_p_total_off}, we obtain the equation
\begin{align*}
         I_{\opout}^{2} \mult R_{\opth}
     & = P_{\optotal, \opoff}
  \\ & = \sum_{k} P_{k, \opoff}
       = \sum_{k} (I_{\opout} \mult c_{k})^{2} \mult R_{k}
       = I_{\opout}^{2} \mult \sum_{k} c_{k}^{2} \mult R_{k}
\end{align*}
and therefore
\begin{equation}
    \sum_{k} c_{k}^{2} \mult R_{k}
  = R_{\opth}
  \enskip .
  \label{eq_leading_coefficient}
\end{equation}

Returning to the original network, by the superposition principle, for each $k$, the current through the resistor $R_{k}$ is given by $I_{k} = I_{k, \opoff} + I_{k, 0} = I_{\opout} \mult c_{k} + I_{k, 0}$, where $I_{k, 0}$ denotes the current through the resistor at zero load current $I_{\opout}$.
Correspondingly, for each $k$, the power dissipated by the resistor $R_{k}$ is equal to
\begin{align*}
         P_{k}
       = I_{k}^{2} \mult R_{k}
     & = (I_{\opout} \mult c_{k} + I_{k, 0})^{2} \mult R_{k}
  \\ & = I_{\opout}^{2} \mult c_{k}^{2} \mult R_{k} + I_{\opout} \mult c_{k} \mult I_{k, 0} \cdot 2 \mult R_{k} + I_{k, 0}^{2} \mult R_{k}
  \enskip .
\end{align*}
Summing over all the resistors and applying~\eqref{eq_leading_coefficient} yields the total power dissipated by the network:
\begin{align}
         P_{\optotal}
     & = \sum_{k} P_{k}
  \nonumber \\ & = \sum_{k} \bigl( I_{\opout}^{2} \mult c_{k}^{2} \mult R_{k} + I_{\opout} \mult c_{k} \mult I_{k, 0} \cdot 2 \mult R_{k} + I_{k, 0}^{2} \mult R_{k} \bigr)
  \nonumber \\ & = I_{\opout}^{2} \mult \sum_{k} c_{k}^{2} \mult R_{k} + I_{\opout} \cdot 2 \mult \sum_{k} c_{k} \mult I_{k, 0} \mult R_{k} \nonumber \\ & \qquad + \sum_{k} I_{k, 0}^{2} \mult R_{k}
  \nonumber \\ & = I_{\opout}^{2} \mult R_{\opth} + I_{\opout} \cdot 2 \mult \sum_{k} c_{k} \mult I_{k, 0} \mult R_{k} + \sum_{k} I_{k, 0}^{2} \mult R_{k}
  \enskip .
  \label{eq_p_total}
\end{align}
The key observation to be made here is that the power dissipated in the network is a quadratic polynomial in $I_{\opout}$ with leading coefficient $R_{\opth}$.

\section{Power conservative equivalent circuit}
\label{section_circuit}

\begin{figure}
  \centering
  \input{figures/equivalent_circuit.tex}
  \caption{Power conservative equivalent circuit with current load}
  \label{figure_equivalent_circuit}
\end{figure}

In order to obtain a power conservative equivalent circuit, a circuit exhibiting both the same electrical properties as the original network and the same power dissipation in its resistors has to be found.
The Th\'{e}venin theorem reduces the problem of electrical equivalence to the problem of finding a circuit satisfying~\eqref{eq_v_out}.
As shown in Section~\ref{section_power}, the power dissipated by the resistors in the original network is given by~\eqref{eq_p_total} and hence the problem of finding a power conservative equivalent circuit reduces to the problem of finding an electrically equivalent circuit such that the total power dissipated by its resistors is equal to the right-hand side of~\eqref{eq_p_total}.

One possibility of obtaining a power conservative equivalent circuit is shown in \figurename~\ref{figure_equivalent_circuit}.
The resistor $R_{\opx}$ contained in this circuit does not contribute anything to the electrical properties of the circuit, its sole purpose being the dissipation of a fixed amount of power which in the following will be referred to as $P_{\opx}$.
The current flowing through the resistor $R_{\opth}$ is given by $I_{\opout} - I_{\opx}$, resulting in an output voltage of
\begin{equation}
    V_{\opout}
  = V_{\opx} - (I_{\opout} - I_{\opx}) \mult R_{\opth}
  = V_{\opx} - I_{\opout} \mult R_{\opth} + I_{\opx} \mult R_{\opth}
  \enskip .
  \label{eq_equivalent_v_out}
\end{equation}
Furthermore, the power dissipated by $R_{\opth}$ is given by $(I_{\opout} - I_{\opx})^{2} \mult R_{\opth}$ and therefore the total power dissipated by the resistors $R_{\opth}$ and $R_{\opx}$ amounts to
\begin{align}
                   P_{\optotal}
               & = (I_{\opout} - I_{\opx})^{2} \mult R_{\opth} + P_{\opx}
  \nonumber \\ & = I_{\opout}^{2} \mult R_{\opth} - I_{\opout} \mult I_{\opx} \cdot 2 \mult R_{\opth} + I_{\opx}^{2} \mult R_{\opth} + P_{\opx}
  \enskip .
  \label{eq_equivalent_p_total}
\end{align}

In order to achieve power conservative equivalence to the original network analyzed in Section~\ref{section_power}, the values of the elements $V_{\opx}$, $I_{\opx}$, and $R_{\opx}$ have to be chosen such that the right-hand sides of equations \eqref{eq_v_out}~and~\eqref{eq_equivalent_v_out} and the right-hand sides of equations \eqref{eq_p_total}~and~\eqref{eq_equivalent_p_total} become equal.
The right-hand sides of \eqref{eq_p_total}~and~\eqref{eq_equivalent_p_total} are both quadratic polynomials in $I_{\opout}$ with leading coefficient $R_{\opth}$ and achieving equality reduces to equating the remaining coefficients, yielding
\begin{displaymath}
    2 \mult \sum_{k} c_{k} \mult I_{k, 0} \mult R_{k}
  = -I_{\opx} \cdot 2 \mult R_{\opth}
\end{displaymath}
and hence
\begin{equation}
    I_{\opx}
  = -\sum_{k} c_{k} \mult I_{k, 0} \mult \frac{R_{k}}{R_{\opth}}
  \label{eq_i_x}
\end{equation}
and
\begin{displaymath}
    \sum_{k} I_{k, 0}^{2} \mult R_{k}
  = I_{\opx}^{2} \mult R_{\opth} + P_{\opx}
\end{displaymath}
and hence
\begin{equation}
    P_{\opx}
  = \sum_{k} I_{k, 0}^{2} \mult R_{k} - I_{\opx}^{2} \mult R_{\opth}
  \enskip .
  \label{eq_p_x}
\end{equation}
Finally, in view of \eqref{eq_v_out}~and~\eqref{eq_equivalent_v_out}, $V_{\opx}$ has to be chosen such that
\begin{displaymath}
    V_{\opth} - I_{\opout} \mult R_{\opth}
  = V_{\opx} - I_{\opout} \mult R_{\opth} + I_{\opx} \mult R_{\opth}
  \enskip ,
\end{displaymath}
i.e.,
\begin{equation}
    V_{\opx}
  = V_{\opth} - I_{\opx} \mult R_{\opth}
  \enskip .
  \label{eq_v_x}
\end{equation}

All that remains is to choose the resistor $R_{\opx}$ such that the power dissipated by it is equal to $P_{\opx}$.
However, this is only possible in the case where $P_{\opx}$ and $V_{\opx}$ are both non-zero, as then $R_{\opx}$ can be chosen to be $V_{\opx}^{2} / P_{\opx}$, leading to a power dissipation of $V_{\opx}^{2} / R_{\opx} = P_{\opx}$.
In the remaining cases, the circuit has to be modified slightly.
In the case where $P_{\opx}$ is zero, $R_{\opx}$ can simply be replaced by an open circuit.
In the case where $P_{\opx}$ is non-zero and $V_{\opx}$ is zero, $R_{\opx}$ and $V_{\opx}$ can be replaced by a short circuit and a separate source with a suitable resistor connected across its terminals added to the circuit so that the power dissipated in that resistor is equal to $P_{\opx}$.
Note that none of these modifications increase the number of elements in the circuit, i.e., a power conservative equivalent circuit can always be constructed using at most four elements.

\bibliography{main}

\end{document}


%-----------------------------------------------------------------------------------------------
\section{Reversible Stirling Engine Model}

    In     this     section,     Cheng     and     Yang's     reversible     Stirling     engine
    model~\cite{2012-ChengCH+YangHS-ApEnergy} is presented. Only a concise subset of  the  model
    that is useful for the goal of the present work is being presented.

    Moreover, the model nomenclature  is  kept  the  same  as  the  one  used  in  the  original
    reference~\cite{2012-ChengCH+YangHS-ApEnergy}, so as to allow readers to  easily  lookup  in
    the original work the parts herein abridged. There is only a minute change in  the  employed
    nomenclature for conciseness, which consists in annotating the Greek letter  of  the  engine
    configuration above a symbol, so as to specify that symbol's expression for that  particular
    engine configuration, starting on Eq.~(\ref{eq:Ve}).

    \begin{figure}[ht]
        \centering
        \includegraphics[width=0.7\columnwidth]{fig/stirling_engine_alpha_color.pdf}
        \caption{Schematic representation of an ideal Stirling engine of the $\alpha$-type.  The
            `eHX'  and  the  `cHX'  are  the  `expansion'  and  `compression'  heat  exchangers,
            respectively of high- and low- temperatures. The power pistons and  the  regenerator
            are indicated. Volumes are not to scale.}
        \label{fig:alpha}
    \end{figure}

    \begin{figure}[ht]
        \centering
        \includegraphics[width=0.7\columnwidth]{fig/stirling_engine_beta_color.pdf}
        \caption{Schematic representation of an ideal Stirling engine of the  $\beta$-type.  The
            `eHX'  and  the  `cHX'  are  the  `expansion'  and  `compression'  heat  exchangers,
            respectively of high- and low- temperatures. The power and displacement pistons  and
            the regenerator are indicated. Volumes are not to scale.}
        \label{fig:beta}
    \end{figure}

    Let $V_{cs}$ and $V_{es}$ be piston and displacer  sweep  volumes  in  the  operation  of  a
    reversible Stirling engine of either $\alpha$-,  $\beta$-,  or  $\gamma$-type---in  Stirling
    engine nomenclature, it is common to name high- and low-  temperature  spaces  and  adjacent
    moving   parts   as    `expansion',    and    `compression'    ones,    respectively,    see
    Figures~\ref{fig:alpha}--\ref{fig:gamma} that illustrate the `expansion', and  `compression'
    heat exchangers; hence the `$c$' and `$e$' subscripts.

    Let further $V_d$ be the so-called engine ``dead volume'', which is composed by  piston  and
    displacer minimum clearances and regenerator volumes, i.e., $V_d =  V_{emin}  +  V_{cmin}  +
    V_r$, respectively.

    \begin{figure}[ht]
        \centering
        \includegraphics[width=0.7\columnwidth]{fig/stirling_engine_gamma_color.pdf}
        \caption{Schematic representation of an ideal Stirling engine of the $\gamma$-type.  The
            `eHX'  and  the  `cHX'  are  the  `expansion'  and  `compression'  heat  exchangers,
            respectively of high- and low- temperatures. The power and displacement pistons  and
            the regenerator are indicated. Volumes are not to scale.}
        \label{fig:gamma}
    \end{figure}

    Let $\phi$ be the engine's crankshaft angle, and $\alpha$  be  the  expansion-to-compression
    piston-piston  (for  the  $\alpha$-type)  or  piston-displacer   (for   the   $\beta$-   and
    $\gamma$-types)  phase  angle.  For  all  engine  configurations   the   expansion   volume,
    $V_e(\phi)$, is:
    %
    \begin{align}
        \label{eq:Ve}
        V_e(\phi) &= V^{\alpha}_e(\phi) = V^{\beta}_e(\phi) = V^{\gamma}_e(\phi) \nonumber\\
                  &= V_{emin} + \frac{1}{2} V_{es}(1 + \sin(\phi + \alpha)).
    \end{align}

    The compression volume, $V_c(\phi)$, is configuration dependent:
    %
    \begin{align}
        \label{eq:Vca}
        V^{\alpha}_c(\phi) &= V_{cmin} + \frac{1}{2} V_{es} \kappa (1 + \sin(\phi)), \\
        \label{eq:Vcb}
        V^{\beta}_c(\phi)  &= V_{cmin} +
            \frac{1}{2} V_{es} (\sqrt{1 - 2\kappa\cos\alpha + \kappa^2} \nonumber\\
                           &+ \kappa\sin\phi - \sin(\phi + \alpha)), \\
        \label{eq:Vcg}
        V^{\gamma}_c(\phi) &= V_{cmin} + \frac{1}{2} V_{es}
            (1 + \kappa + \kappa\sin\phi \nonumber\\
                           &- \sin(\phi + \alpha)),
    \end{align}
    %
    \noindent where $\kappa \equiv V_{cs}/V_{es}$ is the engine sweep volume ratio.

    It is worth noting that each engine has its very own distinct volume dynamics,  making  them
    suitable to verifying Carnot's general proposition from the point  of  view  of  ``different
    internal details''.

    Let the total engine volume---the instantaneous volume  occupied  by  the  engine's  working
    fluid,
    %
    \begin{equation}
        V(\phi) = V_e(\phi) + V_c(\phi) + V_r,
    \end{equation}
    %
    \noindent be written in terms of  a  configuration  dependent  dimensionless  function  most
    generally written as $\Phi(\phi | \chi, \kappa,  \alpha)$,  i.e.,  in  terms  of  parameters
    $\chi$, $\kappa$ and $\alpha$, so that
    %
    \begin{equation}
        V(\phi) = V_{es}\Phi(\phi | \chi, \kappa, \alpha),
    \end{equation}
    %
    \noindent with:
    %
    \begin{align}
        \label{eq:Phia}
        \Phi^{\alpha}(\phi | \chi, \kappa, \alpha) &= \chi + \frac{1}{2}(1 + \kappa) \nonumber\\
            &+ \frac{1}{2}(\sin(\phi + \alpha) + \kappa\sin\phi),\\
        \label{eq:Phib}
        \Phi^{\beta}(\phi | \chi, \kappa, \alpha) &= \chi + \frac{1}{2}\kappa\sin\phi +
            \frac{1}{2}(1 \nonumber\\
            &+ \sqrt{1 - 2\kappa\cos\alpha + \kappa^2}),\\
        \label{eq:Phig}
        \Phi^{\gamma}(\phi | \chi, \kappa) &= \chi + \frac{1}{2}(2 + \kappa)
            + \frac{1}{2}\kappa\sin\phi.
    \end{align}
    %
    \noindent where $\chi \equiv V_{d}/V_{es}$ is the engine dead  volume  ratio.  It  is  worth
    noting that for the $\gamma$-type engine, parameter  $\alpha$  is  absent  from  the  $\Phi$
    function, thus showing that different  engine  configuration  may  also  lead  to  different
    mathematical ``signature'' of intermediate functions.

    The working fluid pressure, $p$, is assumed to be uniform throughout the engine  cavity---as
    pressure-drops introduce irreversibilities that would prevent one  from  verifying  Carnot's
    general proposition in such models. Assuming,  for  the  sake  of  simplicity,  while  still
    allowing for different fluids to be used, ideal gas  $p$-$V$-$T$  behavior  of  the  working
    fluid, one has:
    %
    \begin{equation}
        \label{eq:P}
        p = \frac{mR}{\frac{V_e}{T_e} + \frac{V_c}{T_c} + \frac{V_d}{T_d}}
          = \frac{mRT_e}{V_{es}}\Psi(\phi | \alpha, \kappa, \tau, \chi),
    \end{equation}
    %
    \noindent where $T_e$, $T_c$, and $T_d \equiv (T_e + T_c)/2$ are the expansion, compression,
    and dead space temperatures, respectively, with $T_e > T_c$ for heat engine  operation;  and
    $\Psi(\phi | \alpha, \kappa, \tau, \chi)$ is the dimensionless engine pressure  function  of
    $\phi$ with parameters $\alpha, \kappa, \tau, \chi$, in which $\tau \equiv T_c / T_e$ is the
    compression-to-expansion reservoir temperature ratio. It is worth noting  that  if  Carnot's
    general proposition is to hold, the thermal efficiency of the exemplified  Stirling  engines
    must be a function of $\tau$ only.

    The  dimensionless  pressure  function  is  a  multi-term,  engine   configuration dependent
    expression:
    %
    \begin{align}
        \label{eq:Psia}
        \frac{1}{\Psi^{\alpha}(\phi)} &=
                \frac{2\chi}{1 + \tau} +
                \frac{\kappa + \tau}{2\tau} +
                \frac{\kappa\sin\phi}{2\tau} \nonumber\\
            &+  \frac{\sin(\phi + \alpha)}{2},\\
        \label{eq:Psib}
        \frac{1}{\Psi^{\beta}(\phi)}  &=
                \frac{2\chi}{1 + \tau} +
                \frac{\tau + \sqrt{1 - 2\kappa\cos\alpha + \kappa^2}}{2\tau} \nonumber\\
            &+  \frac{\kappa\sin\phi}{2\tau} +
                \frac{(\tau - 1)\sin(\phi + \alpha)}{2\tau},\\
        \label{eq:Psig}
        \frac{1}{\Psi^{\gamma}(\phi)} &=
                \frac{2\chi}{1 + \tau} +
                \frac{1 + \tau + \kappa}{2\tau} +
                \frac{\kappa\sin\phi}{2\tau} \nonumber\\
            &+  \frac{(\tau - 1)\sin(\phi + \alpha)}{2\tau}.
    \end{align}

    The $RT_e$-normalized (i)~specific net cycle work, $\bar{W}$, and (ii)~specific inlet  cycle
    heat, $\bar{Q}_{in}$, are dimensionless quantities that can be expressed as:
    %
    \begin{align}
        \label{eq:W}
        \bar{W} &= \frac{\oint p\,\difff(V/m)}{RT_e} \nonumber\\
                &= \int_0^{2\pi} \Psi(\phi)
                   \frac{\partial\Phi(\phi | \chi, \kappa, \alpha)}{\partial\phi}\,\difff\phi,\\
        \label{eq:Q}
        \bar{Q}_{in} &= \frac{\oint p\,\difff(V_e/m)}{RT_e} \nonumber\\
                     &= \frac{1}{2} \int_0^{2\pi} \Psi(\phi)
                        \cos(\phi + \alpha) \,\difff\phi,
    \end{align}
    %
    \noindent recalling that $\Phi$ has a different  mathematical  signature  for  $\gamma$-type
    engines,      with      respect      to      the      $\alpha$-      and      $\beta$-types,
    Eq.~(\ref{eq:Phia})--(\ref{eq:Phig}).

    Equations~(\ref{eq:W}) and (\ref{eq:Q}) embody the argument made on the Introduction section
    of this work, meaning that both  cycle  net  work  and  cycle  inlet  heat  expressions  are
    \emph{complicated} and \emph{different} functions of the engine's internal details---whether
    in  terms  of  $\alpha$-,  $\beta$-,  or  of  $\gamma$-type  configurations,  but  also   of
    construction/tuning parameters such as $\alpha$, $\kappa$, and $\chi$---and of  the  thermal
    reservoir temperatures, $\tau$.

%-----------------------------------------------------------------------------------------------


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

      \subsection{lossQuad.m}

\begin{par}
\textbf{Summary:} Compute expectation and variance of a quadratic cost $(x-z)'*W*(x-z)$ and their derivatives, where $x \sim N(m,S)$
\end{par} \vspace{1em}

\begin{verbatim}function [L, dLdm, dLds, S, dSdm, dSds, C, dCdm, dCds] = lossQuad(cost, m, S)\end{verbatim}
    \begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}

\begin{verbatim}  cost
    .z:     target state                                              [D x 1]
    .W:     weight matrix                                             [D x D]
  m         mean of input distribution                                [D x 1]
  s         covariance matrix of input distribution                   [D x D]\end{verbatim}
    \begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}L               expected loss                                  [1   x    1 ]
dLdm            derivative of L wrt input mean                 [1   x    D ]
dLds            derivative of L wrt input covariance           [1   x   D^2]
S               variance of loss                               [1   x    1 ]
dSdm            derivative of S wrt input mean                 [1   x    D ]
dSds            derivative of S wrt input covariance           [1   x   D^2]
C               inv(S) times input-output covariance           [D   x    1 ]
dCdm            derivative of C wrt input mean                 [D   x    D ]
dCds            derivative of C wrt input covariance           [D   x   D^2]\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2013-05-30
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item Expected cost
   \item Variance of cost
   \item inv(s)* cov(x,L)
\end{enumerate}

\begin{lstlisting}
function [L, dLdm, dLds, S, dSdm, dSds, C, dCdm, dCds] = lossQuad(cost, m, S)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
D = length(m); % get state dimension

% set some defaults if necessary
if isfield(cost,'W'); W = cost.W; else W = eye(D); end
if isfield(cost,'z'); z = cost.z; else z = zeros(D,1); end

% 1. expected cost
L = S(:)'*W(:) + (z-m)'*W*(z-m);

% 1a. derivatives of expected cost
if nargout > 1
  dLdm = 2*(m-z)'*W; % wrt input mean
  dLds = W';         % wrt input covariance matrix
end

% 2. variance of cost
if nargout > 3
  S = trace(W*S*(W + W')*S) + (z-m)'*(W + W')*S*(W + W')*(z-m);
  if S < 1e-12; S = 0; end % for numerical reasons
end

% 2a. derivatives of variance of cost
if nargout > 4
  % wrt input mean
  dSdm = -(2*(W+W')*S*(W+W)*(z-m))';
  % wrt input covariance matrix
  dSds = W'*S'*(W + W')'+(W + W')'*S'*W' + (W + W')*(z-m)*((W + W')*(z-m))';
end

% 3. inv(s) times IO covariance with derivatives
if nargout > 6
    C = 2*W*(m-z);
    dCdm = 2*W;
    dCds = zeros(D,D^2);
end
\end{lstlisting}


\subsection{The Feasible Generalised Least Squares (FGLS) estimator}

\subsubsection{Introduction}

We do OLS to get a consistent estimate of \(\Omega \), \(\hat \Omega \).

We then plug this into the GLS estimator.


\documentclass[notitlepage]{simple}

\author{Matt McCarthy}
\title{Box Counting Dimension of the Middle-$\lambda$ Cantor Set}
\date{April 2016}

\def\card{\operatorname{card}}
\def\dim{\operatorname{dim}}
\def\diam{\operatorname{diam}}

\begin{document}
	\maketitle

	\begin{thm*}
		Let $C$ denote the middle-$\lambda$ Cantor set with $0 < \lambda < 1$.
		Then the box counting dimension of $C$ is
		\[
			\dim_B C = \frac{\ln 2}{\ln\paren{\frac{2}{1-\lambda}}}.
		\]
	\end{thm*}

	\section{Background}

	\subsection{Fractal Analysis}

	We begin by defining a $\delta$-cover of a set.
	\begin{definition}
		Let $X\subset\RR^n$ and $\delta>0$.
		Then a \textit{$\delta$-cover} of $X$ is a set $D=\set{D_i}_{i=1}^{n_D}$ such that $X\subseteq \cup_{D_i\in D} D_i$ and $\diam D_i \leq \delta$ for all $D_i\in D$.
		We denote the collection of all such covers as $\mathcal{D}_\delta(X)$.
	\end{definition}

	Now that we have $\delta$-covers, we can define the \textit{box-counting dimension} of a set.

	\begin{definition}
		Let $X$ be a subset of $\RR^n$.
		Then the \textit{box-counting dimension} of $X$, denoted $\dim_B(X)$, is defined as
		\[
			\lim\limits_{\delta\rightarrow0} \frac{\ln N_\delta(X)}{-\ln\delta}
		\]
		where
		\[
			N_\delta(X) = \min\limits_{D\in\mathcal{D}_\delta(X)} \card{D}.
		\]
	\end{definition}
	Note that the box-counting dimension of a set does not necessarily exist, however when it does exist it is usually easier to find.

	\begin{thm}[Squeeze Theorem]
		Let $(a_n),(b_n),(c_n)$ be real valued sequences such that $\lim a_n = \lim c_n = x$ and $a_n\leq b_n\leq c_n$ for all $n$ greater than some $n\in\NN$.
		Then $b_n$ converges to $x$.
	\end{thm}

	\subsection{The Cantor Set}

	We will now talk about the middle third Cantor set.
	Begin by defining $C_0=[0,1]$.
	We now remove the middle third from $C_0$ yielding, $C_1=[0,1/3]\cup[2/3,1]$.
	We then remove the middle third from \textit{each} subinterval of $C_1$, yielding $C_2 = [0,1/9]\cup[2/9,3/9]\cup [6/9,7/9]\cup[8/9,1]$.
	We iterate this process by removing the middle-third from each subinterval of $C_n$ and labeling the remaining set $C_{n+1}$.
	Finally, we define the Cantor set as $C=\cap_{n\in\NN} C_n$.
	We can similarly construct the middle-$\lambda$ Cantor set by performing the same construction but removing $\lambda$ instead of one third in each step.
	Furthermore, $C$ is an uncountable set that has no length left to it.

	\section{Solution}

	\begin{thm}
		Let $C$ denote the middle-$\lambda$ Cantor set with $0 < \lambda < 1$.
		Then the box counting dimension of $C$ is
		\[
			\dim_B C = \frac{\ln 2}{\ln\paren{\frac{2}{1-\lambda}}}.
		\]
	\end{thm}
	\begin{proof}
		Consider removing an interval of length $a\lambda$ from the middle of the interval $[0,a]$ for some $a>0$.
		Since we have removed $a\lambda$ from the interval, the length of this new set is exactly $a-a\lambda=a(1-\lambda)$.
		Moreover, since we removed it from the middle of the interval, this length is equally distributed among the two resultant subintervals.
		Thus the subintervals must have length $a((1-\lambda)/2)$.
		Furthermore, the leftmost interval must be $[0,a((1-\lambda)/2)]$.

		We now need to find the length of any subinterval of $C_n$.
		Since $C_0=[0,1]$, $l_0=1$.
		This tells us that $l_1=(1-\lambda)/2$ and that the leftmost interval in $C_1$ is $[0,(1-\lambda)/2]$.
		We know from the above logic that $l_{n+1}=l_n(1-\lambda)/2$ and its leftmost interval will be $[0,l_n(1-\lambda)/2]$.
		Solving this recursion yields that
		\[
			l_n = \paren{\frac{1-\lambda}{2}}^n
		\]
		and the leftmost interval of $C_n$ is $[0,((1-\lambda)/2)^n]$.
		Thus if we were to cover $C_n$ we would need $2^n$ sets of diameter $((1-\lambda)/2)^n$.

		Suppose $\delta >0$.
		Thus, we can find an $n$ such that $l_{n+1}\leq \delta < l_n$.
		Since $\delta < l_n$, we need no fewer than $2^n$ sets of diameter $\delta$ to cover $C_n\supset C$.
		Since $\delta \geq l_{n+1}$, we need no more than $2^{n+1}$ sets of diameter $\delta$ to cover $C_{n+1}\supset C$.
		Thus, we can glean the following inequality.
		\[
			2^{n} \leq N_\delta(C) \leq 2^{n+1}.
		\]
		Since $\ln$ is a monotone increasing function, we get
		\begin{equation}\label{ln-num}
			n\ln 2 \leq \ln N_\delta(C) \leq (n+1)\ln 2.
		\end{equation}
		Furthermore, since $l_{n+1}\leq \delta < l_n$,
		\begin{equation}\label{ln-den}
			\frac{1}{-\ln l_{n+1}} \leq \frac{1}{-\ln\delta} \leq \frac{1}{-\ln l_n}.
		\end{equation}

		Take \autoref{ln-num} and multiply it by $1/(-\ln\delta)$.
		\[
			\frac{n\ln 2}{-\ln\delta} \leq \frac{\ln N_\delta(C)}{-\ln\delta} \leq \frac{(n+1)\ln 2}{-\ln\delta}
		\]
		By \autoref{ln-den}, we have
		\[
			\frac{n\ln 2}{-\ln l_{n+1}} \leq \frac{\ln N_\delta(C)}{-\ln\delta} \leq \frac{(n+1)\ln 2}{-\ln l_n}.
		\]
		Consider $\ln l_n$.
		\[
			\ln l_n = \ln\paren{\paren{\frac{1-\lambda}{2}}^n} = -n\ln\paren{\frac{2}{1-\lambda}}
		\]
		Thus, our inequality becomes,
		\[
			\frac{n\ln 2}{(n+1)\ln\paren{\frac{2}{1-\lambda}}} \leq \frac{\ln N_\delta(C)}{-\ln\delta} \leq \frac{(n+1)\ln 2}{n\ln\paren{\frac{2}{1-\lambda}}}.
		\]
		If we want to find $\lim\limits_{\delta\rightarrow 0} \ln N_\delta(C)/(-\ln\delta)$, we need to find the limits of the far left and far right as $n\rightarrow\infty$.
		On the lefthand side we get
		\[
			\frac{n\ln 2}{(n+1)\ln\paren{\frac{2}{1-\lambda}}} = \frac{n\ln 2}{n\ln\paren{\frac{2}{1-\lambda}}+\ln\paren{\frac{2}{1-\lambda}}}\rightarrow\frac{\ln 2}{\ln\paren{\frac{2}{1-\lambda}}}
		\]
		by l'H\^opital's rule.
		On the righthand side we get
		\[
			\frac{(n+1)\ln 2}{n\ln\paren{\frac{2}{1-\lambda}}} = \frac{n\ln 2+\ln 2}{n\ln\paren{\frac{2}{1-\lambda}}}\rightarrow\frac{\ln 2}{\ln\paren{\frac{2}{1-\lambda}}}
		\]
		again by l'H\^opital's rule.
		Thus by squeeze theorem, $\dim_B (C)=\frac{\ln 2}{\ln\paren{\frac{2}{1-\lambda}}}$.
	\end{proof}
\end{document}


%!TEX root = ../notes.tex
\section{March 22, 2022}
\subsection{Discriminants of bases, Vandermonde determinants}
We have some review from last time:

Let $K = \QQ(\theta)$ be a number field of degree $n$, let $\{\alpha_1, \dots, \alpha_n\}$ be a $\QQ$-basis of $K$, and let $\sigma_i : K\hookrightarrow \CC$, $1\leq i\leq n$ be the embeddings of $K$ into $\CC$.

The discriminant of $\{\alpha, \dots, \alpha_n\}$ is
\[\Delta[\alpha_i, \dots, \alpha_n] = \det\begin{pmatrix}
        \sigma_1(\alpha_1) & \sigma_1(\alpha_2) & \cdots & \sigma_1(\alpha_n) \\
        \sigma_2(\alpha_1) & \sigma_2(\alpha_2) & \cdots & \sigma_2(\alpha_n) \\
        \vdots             & \vdots             & \ddots & \vdots             \\
        \sigma_n(\alpha_1) & \sigma_n(\alpha_2) & \cdots & \sigma_n(\alpha_n) \\
    \end{pmatrix}^2\]

If $\{\beta_1, \dots, \beta_n\}$ is another basis, then for all $1\leq k\leq n$,
\[\beta_k = \sum_{i=1}^n c_{ik}\alpha_i, \quad c_{ik}\in \QQ,\]
where $\det(c_{ik})\neq 0$. Fact from homework is that
\[\Delta[\beta_i, \dots, \beta_n] = \det(c_{ik})^2\cdot \Delta[\alpha_i, \dots, \alpha_n]\]

\begin{theorem*}[2.7, p.42 \cite{stewart2015algebraic}]
    The discriminant of any $\QQ$-basis for $K$ is rational and nonzero.
\end{theorem*}
\begin{proof}
    It suffices to prove this for $\{1, \theta, \theta^2, \dots, \theta^{n-1}\}$.
\end{proof}

We have the following observation:
\begin{definition*}[Vandermonde Matrix]
    A (square) \ul{Vandermonde matrix} is a matrix of the form
    \[V = \begin{pmatrix}
            1      & t_1    & t_1^2  & \dots  & t_1^{n-1} \\
            1      & t_2    & t_2^2  & \dots  & t_2^{n-1} \\
            \vdots & \vdots & \vdots & \ddots & \vdots    \\
            1      & t_n    & t_n^2  & \dots  & t_n^{n-1} \\
        \end{pmatrix}\]
\end{definition*}

We then claimed (without proof) that
\begin{claim*}
    The determinant of $V$ is
    \[\prod_{1\leq i < j\leq n}(t_j - t_i).\]
\end{claim*}
\begin{proof}
    We know that $\det(V) = 0$ when $t_i = t_j$ for some $i\neq j$. So, $\det(V)$ (as a polynomial in $t_1, \dots, t_n$) is divisible by $t_i - t_j$ for $i < j$. We have that the total degree of $\det(V)$ as a polynomial in $t_1, \dots, t_n$ is
    \[\sum_{i=1}^{n-1} i = \frac{n(n-1)}{2}.\]
    On the other hand, the total degree of $D$ is also $\binom{n}{2} = \frac{n(n-1)}{2}$. Hence, $\det(V)$ is a scalar multiple of $D$ (since ).

    But $\det(V)$ and $D$ are both monic as polynomials (\emph{kinda}) in $\QQ(t_2, \dots, t_n)[t_1]$. Thus $\det(V) = D$. \emph{Or something like that}.
\end{proof}

Going back to the proof of \cref{thm:2.7}, we take $t_i = \theta_i := \sigma_i(\theta)$ to get
\begin{align*}
    \Delta[1, \theta, \dots, \theta^{n-1}] & = \prod_{i < j}(theta_i - \theta_j)^2  \\
                                           & = \mathsf{disc}(\minpoly_\QQ(\theta)).
\end{align*}
which is clearly rational and nonzero in $\QQ^+$.

\begin{example}
    Let
    \[K = \QQ(\sqrt{5})\]
    with the obvious basis $\{1, \sqrt{5}\}$. We have
    \begin{align*}
        \Delta[1, \sqrt{5}] & = \left|\begin{array}{cc}
                                          1 & \sqrt{5}  \\
                                          1 & -\sqrt{5}
                                      \end{array}\right|^2 \\
                            & = (-2\sqrt{5})^2             \\
                            & = 20
    \end{align*}
    and another basis is $\left\{1, \frac{1+\sqrt{5}}{2}\right\}$ and
    \begin{align*}
        \Delta\left[1, \frac{1+\sqrt{5}}{2}\right] & = \left|\begin{array}{cc}
                                                                 1 & \frac{1+\sqrt{5}}{2}  \\
                                                                 1 & -\frac{1+\sqrt{5}}{2}
                                                             \end{array}\right|^2 \\
                                                   & = (-2\frac{\sqrt{5}}{2})^2                      \\
                                                   & = (\sqrt{5})^2 = 5
    \end{align*}
\end{example}

\begin{example}
    Let
    \[K = \QQ(\sqrt[3]{2})\]
    A basis is $\{1, \sqrt[3]{2}, (\sqrt[3]{2})^2\} =: B$
    \begin{align*}
        \Delta(B) = \left|\begin{array}{ccc}
                              1 & \sqrt[3]{2}          & (\sqrt[3]{2})^2          \\
                              1 & \zeta_3\sqrt[3]{2}   & (\zeta_3\sqrt[3]{2})^2   \\
                              1 & \zeta_3^2\sqrt[3]{2} & (\zeta_3^2\sqrt[3]{2})^2
                          \end{array}\right|
    \end{align*}
    \emph{computation left as exercise\dots}
\end{example}

\subsection{Algebraic Integers}
\begin{definition}[Algebraic Integer]
    A complex number is an \ul{algebraic integer} if it is a root of a \emph{monic} polynomial with integer coefficients.
\end{definition}

We denote the set of algebraic integers by $\overline{\ZZ}$. By definitions, $\overline{\ZZ}\subseteq\overline{\QQ}$.

\begin{example}
    The following are some algebraic integers:
    \begin{itemize}
        \item $\sqrt{2}\in\overline{\ZZ}$ since $\sqrt{2}$ is a root of $x^2 - 2$.
        \item $\tau = \frac{1}{2}(1 + \sqrt{5})$, since $\tau^2 - \tau - 1 = 0$.
    \end{itemize}
    Non-examples:
    \begin{itemize}
        \item $\frac{22}{7}$ is not an algebraic integer. \emph{Why?} We look at the $7$-adic valuation of the monic polynomial when we plug $\frac{22}{7}$ in. What are some other ways to reason about this?
    \end{itemize}
\end{example}
Key algebra fact: If $f(x)\in \ZZ[x]$ is a monic polynomial with $f(x) = g(x)\cdot h(x)$ where $g(x)$, $h(x)$ are monic polynomials in $\QQ[x]$, then $g(x), h(x)\in \ZZ[x]$. (Gauss's Lemma).

Hence, if $\frac{22}{7}$ is an algebraic integer, if has some $f(x)\in\ZZ[x]$ for which it is a root. But it is also a root of $g(x) = x - \frac{22}{7}$ where $f(x) = g(x)\cdot h(x)$ forcing $g(x)$ to be in $\ZZ[x]$. So the fact that $\minpoly_\QQ\left(\frac{22}{7}\right) = x - \frac{22}{7}$ implies that $\frac{22}{7}\not\in\overline{\ZZ}$.

\begin{definition*}[Algebraic Integer']
    An algebraic number $\theta$ is an algebraic integer iff $\minpoly_\QQ(\theta)\in\ZZ[x]$.
\end{definition*}


\section{Pattern Recognition Basics}

\subsection{Classification of Simple Patterns}

\begin{frame}
	\frametitle{Classification of Simple Patterns}

	The system for the classification of simple patterns has the following generic structure\\
	\vspace{2cm}
	\pause
%\begin{centering}
	$\hspace{1.03cm} \overset{\vec f}{\longrightarrow}\fbox{Preprocessing}\overset{\vec g}{\longrightarrow}\fbox{Feature Extraction} \overset{\vec c}{\longrightarrow}\fbox{Classification} \overset{y}{\longrightarrow}$\\
	$\hspace{1.03cm}\hspace{8.5cm} \uparrow $\\
	$\hspace{1.03cm}\hspace{4.3cm} \fbox{Training Samples}\longrightarrow \fbox{Learning}$
\end{frame}

\begin{frame}
	\frametitle{Classification of Simple Patterns \cont}

	\begin{itemize}
		\item {\em \structure{Supervised learning:}}

		      $m$ training samples include feature and associated class number
		      \begin{displaymath}
			      S = \{ (\vec x_1, y_1), (\vec x_2, y_2), (\vec x_3, y_3), \dots, (\vec x_m, y_m) \}
		      \end{displaymath}
              where $\vec x_i \in \mathcal{X}$ denotes the feature vector and $y_i\in Z$ denotes the class number of sample $i$.
	      If nothing special is mentioned ${\mathcal{X}}\subseteq \mathbb{R}^d$.
		      \pause
		      \vspace{0.5cm}
		\item {\em \structure{Unsupervised learning:}}

		      $m$ training samples just include features, no class assignments and even the number of classes is (not always) known
		      \begin{displaymath}
			      S = \{ \vec x_1, \vec x_2, \vec x_3, \dots, \vec x_m \}
		      \end{displaymath}
	\end{itemize}
\end{frame}

\subsection{Bayesian Classifier}

\begin{frame}
	\frametitle{Bayesian Classifier}

	\structure{Notation:}

	\begin{center}
		\begin{minipage}{0.7\textwidth}
			\begin{itemize}
				\item[ $ \vec x \in \mathbb{R}^d:$]  $d$-dimensional feature vector
				\item[ $y:$] class number \\
				      (usually $y\in\{0,1\}$ or $y\in\{-1,+1\}$)
				\item[$p(y):$] prior probability of pattern class $y$
				\item[$p(\vec x):$] evidence\\
				      (distribution of features in $d$-dimensional feature space)
				\item[$p(\vec x , y):$] joint probability density function (pdf)
				\item[$p(\vec x |y):$] class conditional density
				\item[$p(y| \vec x):$] posterior probability
			\end{itemize}
		\end{minipage}
	\end{center}
\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}
\vspace{-.52cm}
  \begin{figure}
    \resizebox{.85\linewidth}{!}{
      \alt<25->{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie25.\png}
      }{\alt<24>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie24.\png}
      }{\alt<23>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie23.\png}
      }{\alt<22>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie22.\png}
      }{\alt<21>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie21.\png}
      }{\alt<20>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie20.\png}
      }{\alt<19>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie19.\png}
      }{\alt<18>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie18.\png}
      }{\alt<17>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie17.\png}
      }{\alt<16>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie16.\png}
      }{\alt<15>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie15.\png}
      }{\alt<14>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie14.\png}
      }{\alt<13>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie13.\png}
      }{\alt<12>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie12.\png}
      }{\alt<11>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie11.\png}
      }{\alt<10>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie10.\png}
      }{\alt<9>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie9.\png}
      }{\alt<8>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie8.\png}
      }{\alt<7>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie7.\png}
      }{\alt<6>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie6.\png}
      }{\alt<5>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie5.\png}
      }{\alt<4>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie4.\png}
      }{\alt<3>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie3.\png}
      }{\alt<2>{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie2.\png}
      }{
        \includegraphics[width=.85\linewidth]{\pngdir/condProb/Folie1.\png}
      }}}}}}}}}}}}}}}}}}}}}}}}
    }
  \end{figure}

\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	\structure{Bayes rule:}\\[.5cm]

	\begin{eqnarray*}
		\underbrace{p(\vec x , y)}_{joint\ pdf}
		&=& \pause \underbrace{p(y)}_{prior} \cdot \underbrace{p(\vec x | y)}_{class\ conditional\ pdf} \\[.5cm] \pause
		&=& \underbrace{ p(\vec x)}_{evidence} \cdot \underbrace{ p(y| \vec x)}_{posterior}
	\end{eqnarray*}
\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	Now we get the posterior as follows:

	\begin{eqnarray*}
		p(y| \vec x)
		&=& \pause \frac{p(y) \cdot p(\vec x | y)}{p(\vec x)} \\ \pause
		&=& \frac{p(y) \cdot p(\vec x | y)}{\sum\limits_{y'}p(\vec x , y')} \\ \pause
		&=& \frac{p(y) \cdot p(\vec x | y)}{\sum\limits_{y'}p(y') \cdot p(\vec x | y')}\\
	\end{eqnarray*}
\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	\structure{Note:}
	\begin{displaymath}
		p(\vec x) = \sum\limits_{y}p(y) \cdot p(\vec x | y)
	\end{displaymath}
	is a \structure{marginal} of $p(\vec x , y)$.

	\begin{itemize}
		\item We get $p(\vec{x})$ by marginalizing $p(\vec{x}, y)$ over $y$.
		\item Accordingly we get $p(y)$ by marginalizing $p(\vec{x}, y)$ over $\vec{x}$, i.\,e.
		      \begin{eqnarray*}
			      p(y)&=& \int p(\vec{x}, y) \mathsf{d}\vec{x}
		      \end{eqnarray*}
	\end{itemize}

	\alert{Did you notice:} $y$ is a discrete random variable whereas $\vec{x}$ is a continuous random vector (summation vs.\ integration).
\end{frame}

\input{nextTime.tex}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	Now let us summarize the Bayesian decision rule:\\[.3cm]
	We decide for the class $y^*$ according to the decision rule
	\begin{eqnarray*}
		y^* &=& \pause \argmax\limits_{y} p(y | \vec x)\ \\[.3cm] \pause
		&=& \argmax\limits_{y} \frac{p(y) \cdot p(\vec x | y)}{p(\vec x)} \\[.3cm] \pause
		&=& \argmax\limits_{y} p(y) \cdot p(\vec x | y) \\[.3cm] \pause
		&=& \argmax\limits_{y} \{\log p(y)\ + \log p(\vec x | y)\}
	\end{eqnarray*}
\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	\structure{Notes:} \\[.3cm]

	\begin{itemize}
		\item The key aspect in designing a classifier is to find a good model \\
		      for the posterior $p(y|\vec x)$. \\[.3cm]
		\item Feature vectors $\vec x$ usually have fixed dimensions $d$ in simple classification schemes, \\[.3cm]
		\item but ${\mathcal{X}}$ is not necessarily a subset of $\mathbb{R}^d$: \\
		      features of varying dimension, sequences and sets of features
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Bayesian Classifier \cont}

	\begin{itemize}
		\item \structure{Generative modeling:} \\
		      modeling and estimation of $p(y)$ and $p(\vec x | y)$. \\[.5cm]
		\item \structure{Discriminative modeling:} \\
		      straight modeling and estimation of $p(y|\vec x)$.
	\end{itemize}
\end{frame}

\subsection{Optimality of the Bayesian Classifier}

\begin{frame}
	\frametitle{Optimality of the Bayesian Classifier}

	%  \begin{citeblock}{Definition}
	\begin{definition}
		$l(y_{1},y_{2})$ is the \structure{loss} if a feature vector belonging to class $y_{2}$
		is assigned to class $y_{1}$. The $(0,1)$-loss function is defined by
		\begin{eqnarray*}
			l(y_{1},y_{2}) &= &\left\{ \begin{array}{cc}
				0 & ,\ if\ y_{1}=y_{2} \\
				1 & ,\ otherwise
			\end{array} \right.
		\end{eqnarray*}
		%  \end{citeblock}
	\end{definition}
\end{frame}

\begin{frame}
	\frametitle{Optimality of the Bayesian Classifier \cont}

	The \structure{best (or optimal) decision rule} according to classification loss minimizes the average loss L:

	\begin{eqnarray*}
		\mathsf{AL}(\vec x , y) &=& \sum\limits_{y'}l(y,y')p(y'|\vec x)
	\end{eqnarray*}

\end{frame}

\begin{frame}
	\frametitle{Optimality of the Bayesian Classifier \cont}

	Using the $(0,1)$-loss function, the class decision is based on:

	\begin{eqnarray*}
		y^* &=& \argmin\limits_{y} \mathsf{AL}(\vec x, y)\\
		\pause  &=& \argmin\limits_{y} \sum\limits_{y'} l(y,y') \cdot p(y'|\vec x)\\
		%           \pause  &=& \argmin\limits_{y} (1-p(y|\vec x))\\
		\pause  &=& \argmax\limits_{y} p(y|\vec x)\\
	\end{eqnarray*}
\end{frame}

\begin{frame}
	\frametitle{Optimality of the Bayesian Classifier \cont}

	\structure{Conclusion:}

	\begin{itemize}
		\item The optimal classifier w.\,r.\,t.\ the (0,1)-loss function applies the Bayesian decision rule.
		\item This classifier is called \structure{Bayesian classifier}. \\[.75cm]
	\end{itemize}
	\spread

	\vorsicht The loss function is {\bf NOT} convex. \vfill
\end{frame}

\subsection{Lessons Learned}

\begin{frame}
	\frametitle{Lessons Learned}

	\begin{itemize}
		\item General structure of a classification system \\[.5cm] \pause
		\item Supervised and unsupervised learning \\[.5cm] \pause
		\item Basics on probabilities (probability, pdf, Bayes rule, etc.) \\[.5cm] \pause
		\item Optimality of Bayes classifier and the role of the loss function \\[.5cm] \pause
		\item Discriminative and generative approach to model a posteriori probability
	\end{itemize}
\end{frame}

\input{nextTime.tex}

\subsection{Further Readings}

\begin{frame}
	\frametitle{Further Readings}

	\begin{itemize}
		\item Heinrich Niemann: \\
		      \structure{Pattern Analysis}, \\
		      Springer Series in Information Sciences 4, Springer, Berlin, 1982. \\[.15cm]
		\item Heinrich Niemann: \\
		      \structure{Klassifikation von Mustern}, \\
		      Springer Verlag, Berlin, 1983. \\[.15cm]
		\item Richard O. Duda, Peter E. Hart, David G. Stork: \\
		      \structure{Pattern Classification}, 2nd Edition, \\
		      John Wiley \& Sons, New York, 2000.
	\end{itemize}
\end{frame}


\documentclass{amsart}

\usepackage[english]{babel}
\usepackage{enumerate}
\usepackage{graphicx}

\title{The game of Risk analysed}
\author{Hidde Wieringa}
\date{\today}

\begin{document}

	\begin{abstract}
		In the game of Risk, the fights between two countries dominate the game. After explaining the rules of the game, these fights are examined rather closely. To do that, we need an optimal defend strategy, which will be used to calculate the worst case scenarios for the attacker. The possibilities of losing a certain number of units for the two opposing forces will be calculated. Because of these possibilities, it is possible to calculate the expected \emph{gain} in a certain state of the fight, as well as the probability for the attacker to win the fight.
	\end{abstract}

	\maketitle

	\section{The game of Risk}

	The game of Risk is played by three to six players. Each player gets assigned a number of areas on a world map, and gets an objective. Once a player has fulfilled this objective, the game ends. These objectives are either defeating another player, or conquering certain areas, usually in the form of continents.

	To do that, a player will attack other players, in neighboring areas. A player can attack if he has more than one unit in the area. To attack, the attacker throws a minimum of one die, and a maximum of three dice. The defender then chooses whether he will defend with one or two dice. If the defender has only one unit, the player may only defend with one die.

	Once the dice rolls are known, the maximum of each set is compared. If the attacker has a strict greater roll, the defender loses one unit. Else, the attacker loses one unit. This is repeated, until either the attacker or defender has rolled no more dice.

	\section{The optimal defend strategy}

	The defender has the unique position be able to choose the amount of dice that will be used to defend the attacked area, after the attacker has rolled the attacking dice. This is a great advantage, because the defender can choose whether one or two armies must be put into the fight. Generally speaking, it is preferred to roll one die when the attacker rolled high, and two dice when the attacker rolled low. We will now make this strategy exact, to optimize the chances for the defender.

	To do that, we first define the \emph{gain} of a defending roll as
	$$
		\text{gain}_A := E(G_A),
	$$ where $G$ is the stochastic value of the difference between the amount of attacking units destroyed and the amount of defending units lost, given the attacking roll $A$.

	Two find the optimal defending strategy, we look at each possible die roll for the three attacking dice. For two attacking dice we get the same results, as only the best two dice of the three count. When the attacker only rolls one die, it is always better for the defender to roll two dice when possible.

	We calculate the \emph{gain} for both strategies by calculating the mean gain when throwing with one die and when throwing with two dice. The results are shown in figure \ref{fig:1}. Only the two maximum dice are shown, as the minimum die does not influence the outcome of the fight. The chances of rolling such a roll for the attacker are also noted.

	\begin{figure}

	\makebox[\textwidth]{
	\begin{tabular}{c||cc|cc|cc|cc|cc|cc}
	$A_1$ \,\textbackslash \, $A_2$ & \multicolumn{2}{c}{1} & \multicolumn{2}{c}{2} & \multicolumn{2}{c}{3} & \multicolumn{2}{c}{4} & \multicolumn{2}{c}{5} & \multicolumn{2}{c}{6} \\
	 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 \\

	\hline
1 & 1.00 & 2.00 & & & & & & & & & &  \\
2 & 0.67 & 1.94 & 0.67 & 1.33 & & & & & & & &  \\
3 & 0.33 & 1.78 & 0.33 & 1.17 & 0.33 & 0.67 & & & & & &  \\
4 & 0.00 & 1.50 & 0.00 & 0.89 & 0.00 & 0.39 & 0.00 & 0.00 & & & &  \\
5 & -0.33 & 1.11 & -0.33 & 0.50 & -0.33 & 0.00 & -0.33 & -0.39 & -0.33 & -0.67 & &  \\
6 & -0.67 & 0.61 & -0.67 & 0.00 & -0.67 & -0.50 & -0.67 & -0.89 & -0.67 & -1.17 & -0.67 & -1.33  \\
	\end{tabular}
	}

	\vspace{10px}

	\makebox[\textwidth]{
	\begin{tabular}{c||cc|cc|cc|cc|cc|cc}
	$A_1$ \,\textbackslash \, $A_2$ & \multicolumn{2}{c}{1} &  \multicolumn{2}{c}{2} & \multicolumn{2}{c}{3} & \multicolumn{2}{c}{4} & \multicolumn{2}{c}{5} & \multicolumn{2}{c}{6} \\

	\hline
1 & \textbullet\textbullet & 0.00  & & & & & & & & & &  \\
2 & \textbullet\textbullet & 0.01  & \textbullet\textbullet & 0.02  & & & & & & & &  \\
3 & \textbullet\textbullet & 0.01  & \textbullet\textbullet & 0.04  & \textbullet\textbullet & 0.03  & & & & & &  \\
4 & \textbullet\textbullet & 0.01  & \textbullet\textbullet & 0.04  & \textbullet\textbullet & 0.07  & \textbullet & 0.05  & & & &  \\
5 & \textbullet\textbullet & 0.01  & \textbullet\textbullet & 0.04  & \textbullet\textbullet & 0.07  & \textbullet & 0.10  & \textbullet & 0.06  & &  \\
6 & \textbullet\textbullet & 0.01  & \textbullet\textbullet & 0.04  & \textbullet\textbullet & 0.07  & \textbullet & 0.10  & \textbullet & 0.12  & \textbullet & 0.07   \\
	\end{tabular}
	}

	\caption{First table: the \emph{gain} when defending with either one or two dice. Second table: the optimal amount of dice to defend with, as well as the probability this situation occurs.} \label{fig:1}
	\end{figure}

	From this result we can conclude that the following simple rules (evaluated from the top) give us an optimal defending strategy, given attacking roll $A$ (sorted descending), attacking armies $X$ and defending armies $Y$:
	\begin{enumerate}
		\item If $Y = 1$, use one die,
		\item If $|A| = 1$, use two dice,
		\item If $A_2 > 3$, use one dice,
		\item Use two dice.
	\end{enumerate}

	We will use this set of rules to calculate the worst case scenario for the attacker concerning his chances of winning a fight.

	\section{Destroyed units}

	Now that we have found an optimal defending strategy, let us look at the attacking force. When a force attacks, it can either lose one or two units, and the defender can also lose either one or two units in one roll for both parties. Depending on the dice rolls and the choices made, some of these chances of losing a certain number of units for both sides can be $0$. These events will be our event space
	$$
	E = \{(2,0), (1,0), (1,1), (0,1), (0,2)\}.
	$$

	From now on we will assume the defender defends his area in an optimal way, to find a lower bound for the probabilities involved.

	Much like the \emph{gain} which was calculated in the previous section, we will now calculate the probabilities of the different events which can take place after one roll of the attacker and defender. To do this, for each attacking roll each (optimal) defending row was calculated and the probability of those rolls occurring added to the probability of the event which occurs when those rolls are rolled. The results are listed in figure \ref{fig:2}.

	\begin{figure}

	\makebox[\textwidth]{
	\begin{tabular}{c||ccccc}
		$X$ \,\textbackslash \, $Y$ & 1 & 2 & 3 & 4 \\
		\hline
		1 & \begin{tabular}{ll}
(0, 0) 1.00\end{tabular}& \begin{tabular}{ll}
(0, 0) 1.00\end{tabular}& \begin{tabular}{ll}
(0, 0) 1.00\end{tabular}& \begin{tabular}{ll}
(0, 0) 1.00\end{tabular} \\ \hline
2 & \begin{tabular}{ll}
(0, 1) 0.42\\(1, 0) 0.58\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.25\\(1, 0) 0.75\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.25\\(1, 0) 0.75\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.25\\(1, 0) 0.75\end{tabular} \\ \hline
3 & \begin{tabular}{ll}
(0, 1) 0.58\\(1, 0) 0.42\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.35\\(2, 0) 0.31\\(1, 0) 0.15\\(1, 1) 0.14\\(0, 2) 0.05\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.35\\(2, 0) 0.31\\(1, 0) 0.15\\(1, 1) 0.14\\(0, 2) 0.05\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.35\\(2, 0) 0.31\\(1, 0) 0.15\\(1, 1) 0.14\\(0, 2) 0.05\end{tabular} \\ \hline
4 & \begin{tabular}{ll}
(0, 1) 0.66\\(1, 0) 0.34\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.37\\(2, 0) 0.20\\(1, 0) 0.13\\(1, 1) 0.17\\(0, 2) 0.14\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.37\\(2, 0) 0.20\\(1, 0) 0.13\\(1, 1) 0.17\\(0, 2) 0.14\end{tabular}& \begin{tabular}{ll}
(0, 1) 0.37\\(2, 0) 0.20\\(1, 0) 0.13\\(1, 1) 0.17\\(0, 2) 0.14\end{tabular} \\
	\end{tabular}
	}

	\caption{For given $X$ and $Y$, the possible events $e \in E$ (the units destroyed for the attacker and defender) which can occur, with their respectible probabilities.} \label{fig:2}
	\end{figure}

	These probabilities are the same for any larger number of units on either side. Logical, since the attacking or defending strategy does not change when more units are added to either side.

	\section{Expected gain}

	As we have calculated the \emph{gain} for the defender in different scenarios, we will also calculate the gain for the attacker, given the attacking units $X$ and defending units $Y$. The plot of the gain is shown in figure \ref{fig:3}.

	\begin{figure}

	\makebox[\textwidth]{
	\begin{tabular}{c||ccccc}
		$X$ \,\textbackslash \, $Y$ & 1 & 2 & 3 & 4 & 5 \\
		\hline
		1 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00  \\
		2 & -0.17 & -0.49 & -0.49 & -0.49 & -0.49  \\
		3 & 0.16 & -0.30 & -0.30 & -0.30 & -0.30  \\
		4 & 0.32 & 0.11 & 0.11 & 0.11 & 0.11  \\
		5 & 0.32 & 0.11 & 0.11 & 0.11 & 0.11  \\
	\end{tabular}
	}

	\caption{The gain of the attacker, given $X$ and $Y$ with the defender defending optimally.} \label{fig:3}
	\end{figure}

	\section{Winning probabilities}

	Finally we calculate the winning probabilities for the attacker, given the attacking units $X$ and defending units $Y$. Let $S = (X, Y)$ be the state of the fight. This state can change in any of the ways in the event set $E$. For any $e \in E$, there is a probability $P_{e,X,Y}$ that this event occurs. Once such an event occurs, the state changes to a new state, for which the same things can be said.

	This chain of events can be viewed as a Discrete Time Markov Chain, as no previous state influences the future of the current state. The chain will always enter an absorbing state which is of the form $(X, 0)$ or $(1, Y)$. These states are absorbing, because the attacker has won when the defender has no more units, and the defender wins once the attacker is left with one unit.

	To calculate the probability that the attacker wins, we set up the recurrence relation
	$$
	P_\text{win}(X, Y) = \sum_{e \in E} P_\text{win}(X - e_\text{att}, Y-e_\text{def}) \cdot P_{e, X, Y},
	$$
	using
	$$
	P_\text{win}(X,0) = 1, P_\text{win}(1, Y) = 0
	$$
	for any $X$ and $Y$. The quantities $e_\text{att}$ and $e_\text{def}$ are the number of units that are destroyed of respectively the attacker and defender once event $e$ occurs.

	This recurrence relation can be solved recursively for any $X$ and $Y$, giving the results in figure \ref{fig:4}.

	\begin{figure}
	\makebox[\textwidth]{
	\begin{tabular}{c||cccccccccccccccc}
X \,\textbackslash \, Y &   0   &   1   &   2    &  3   &   4   &   5   &   6   &   7    &  8    &  9   &   10  &   11   &  12 &    13    & 14&     15  \\
\hline
1    &    - &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 \\
2    &    1.00 &   0.42 &   0.11 &   0.03 &   0.01 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 \\
3    &    1.00 &   0.75 &   0.39 &   0.20 &   0.09 &   0.04 &   0.02 &   0.01 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 &   0.00 \\
4    &    1.00 &   0.92 &   0.67 &   0.47 &   0.31 &   0.20 &   0.12 &   0.08 &   0.05 &   0.03 &   0.02 &   0.01 &   0.01 &   0.00 &   0.00 &   0.00 \\
5    &    1.00 &   0.97 &   0.81 &   0.64 &   0.48 &   0.35 &   0.25 &   0.17 &   0.12 &   0.08 &   0.05 &   0.03 &   0.02 &   0.01 &   0.01 &   0.01 \\
6    &    1.00 &   0.99 &   0.90 &   0.78 &   0.64 &   0.51 &   0.39 &   0.29 &   0.21 &   0.15 &   0.11 &   0.08 &   0.05 &   0.04 &   0.02 &   0.02 \\
7    &    1.00 &   1.00 &   0.95 &   0.86 &   0.76 &   0.64 &   0.52 &   0.41 &   0.32 &   0.25 &   0.18 &   0.14 &   0.10 &   0.07 &   0.05 &   0.04 \\
8    &    1.00 &   1.00 &   0.97 &   0.92 &   0.84 &   0.74 &   0.64 &   0.53 &   0.44 &   0.35 &   0.27 &   0.21 &   0.16 &   0.12 &   0.09 &   0.07 \\
9    &    1.00 &   1.00 &   0.99 &   0.95 &   0.90 &   0.82 &   0.74 &   0.64 &   0.55 &   0.45 &   0.37 &   0.30 &   0.24 &   0.18 &   0.14 &   0.11 \\
10   &    1.00 &   1.00 &   0.99 &   0.97 &   0.94 &   0.88 &   0.81 &   0.73 &   0.64 &   0.56 &   0.47 &   0.39 &   0.32 &   0.26 &   0.20 &   0.16 \\
11   &    1.00 &   1.00 &   1.00 &   0.98 &   0.96 &   0.92 &   0.87 &   0.80 &   0.73 &   0.65 &   0.56 &   0.48 &   0.41 &   0.34 &   0.28 &   0.22 \\
12   &    1.00 &   1.00 &   1.00 &   0.99 &   0.98 &   0.95 &   0.91 &   0.86 &   0.80 &   0.73 &   0.65 &   0.57 &   0.50 &   0.42 &   0.36 &   0.30 \\
13   &    1.00 &   1.00 &   1.00 &   0.99 &   0.99 &   0.97 &   0.94 &   0.90 &   0.85 &   0.79 &   0.72 &   0.65 &   0.58 &   0.51 &   0.44 &   0.37 \\
14   &    1.00 &   1.00 &   1.00 &   1.00 &   0.99 &   0.98 &   0.96 &   0.93 &   0.89 &   0.84 &   0.79 &   0.72 &   0.66 &   0.59 &   0.52 &   0.45 \\
15   &    1.00 &   1.00 &   1.00 &   1.00 &   0.99 &   0.99 &   0.97 &   0.95 &   0.92 &   0.89 &   0.84 &   0.78 &   0.72 &   0.66 &   0.59 &   0.53 \\
	\end{tabular}}

	\includegraphics[scale=0.6]{Plot}

	\caption{The probability of winning a fight with $X$ attacking and $Y$ defending units, or $P_\text{win}(X,Y)$. Below that is a plot of the same data.} \label{fig:4}
	\end{figure}

	\section{conclusion}

	From the results of the last sections we can deduce two conclusions:
	\begin{enumerate}
		\item It is always better to have more units than your opponent. The more units you have while the opponents units stays the same amount, the more your winning probabilities increase.

		\item It is better for the attacker to save up units, even if the defender is doing the same. For equal $X$ and $Y$, the winning probabilities increase for larger $X$ and $Y$.
	\end{enumerate}

	\section{Further ideas}

	This analysis is made solely for the game of Risk. Changing the rules of the game, any found results can be expanded easily. For example, one might change the number of states the dice can assume or the rules when and how many dice can be used to attack and defend. One can also create an more interesting problem by adding certain states in which units are added to the existing ones, instead of removed.

\end{document}