\documentclass[12pt, a4paper]{article}
\usepackage[a4paper, bindingoffset=0.2in, %
left=0.5in,right=0.5in,top=0.5in,bottom=0.5in,%
footskip=.25in]{geometry}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{physics}

\title{PSet9 Report}
\author{Ali Abolhassanzadeh Mahani}

\begin{document}
	\maketitle
	\section{Theory}
	The problem is to simulate 100 argon atoms in a space. The potential for the interaction of
	the atoms is the leonard-jones potential as below:
	\begin{equation}
		U_{ij} = 4\epsilon \left(\left(\frac{\sigma}{r_{ij}}\right)^{12} -
		\left(\frac{\sigma}{r_{ij}}\right)^6\right), \;
		r_{ij} \equiv \norm{\vec{r}_i - \vec{r}_j}
		\label{eq:leonard_jones}
	\end{equation}

	First, there are some matters we need to clarify before jumping into the code.
	Trying to simulate the Argon atoms using the SI units is impossible. It takes a lot of time and
	space that is unnecessary. In order to solve this problem, we come up with characteristic
	units for the system. Our parameters are as follows:
	\begin{equation}
		m_{arg} \sim 10^{-26} (kg), \; \sigma = 3.47 \times 10^{-10} (m), \; \epsilon \simeq
	\end{equation}
	If we make the following changes:
	\begin{equation}
		m \rightarrow \frac{m}{m_{arg}}, \; U \rightarrow \frac{U}{\epsilon}, \; r \rightarrow
		\frac{r}{\sigma}
	\end{equation}
	then Eq.~\ref{eq:leonard_jones} becomes:
	\begin{equation}
		u_{ij} = 4 \left(\frac{1}{r_{ij}^{12}} - \frac{1}{r_{ij}^6}\right)
	\end{equation}

	Using this transformation, the equations for kinetic and potential energy of the system
	are:
	\begin{equation}
		\begin{aligned}
			K &= \frac{1}{2}\sum_{i=1}^{n} v_i^2,\\
			U &= \sum_{i < j} u_{ij} = \frac{1}{2} \sum_{i, j = 1}^{n} u_{ij}
		\end{aligned}
	\end{equation}

	\section{The MDSystem Class}
	I start by writing a class called \texttt{MDSystem} that takes in the size of the box, the
	number of atoms, and the initial position of the atoms in the box. Then, it assigns a speed
	according to the temperature of the system and speads the velocities in random directions.
	Then, we make sure that the velocity of the center of mass (CoM)  of the system is zero,
	by calling \texttt{stabilize\_system()}. This is because the notions of temperature is defined
	in a stationary system in the LAB frame. This is all the work of \texttt{\_\_init\_\_()}

	The algorithm for the evolution of the system is the "verlet" method, since it intrinsicly conserves the energy of the system as discussed in the previous homeworks.

	Then, we have the periodic boundary conditions that need to be applied both when a
	particle is passing through one side of the box, and when we are calculating the interaction
	of the particles. For the interactions, since the Leonard-Jones interaction is short-range, we
	set a "cutoff radius" from each particle and assume that only the particles within that range
	can interact with the subjected atom. The value chosen for the cutoff radius is $2.5$ in the
	characteristic units.

	\section{The Simulation}
	\subsection{Analysis}
	\subsubsection{Conservation of Energy and number of atoms}
	First, we check the conservation of energy and the number of particles in the left side of the
	box. Since our initial setup was to place the atoms in a grid in the left half of the box, we
	should expect the value to start from total number of atom (in our case 100) and move towards half of that value (50 for our case), and then to oscillate about that value. As for
	energy, we expect it to be conserved since our system is micro-canonical (NVE) as seen in
	Fig~\ref{fig:energy_and_left}

	\begin{figure}[h!]
		\centering
		\includegraphics[width=.45\linewidth]{../results/particles_on_left100_50000.jpg}
		\includegraphics[width=.45\linewidth]{../results/energy_conservation100_50000.jpg}
		\caption{As expected, on the left, we can see that the number of particles on the
		left side of the box reaches, and oscillates about half of the atoms. And on the right, we
		can clearly witness the conservation of energy}
		\label{fig:energy_and_left}
	\end{figure}

	\subsubsection{Temperature, Pressure, and equilibrium}
	Now, we turn our heads to a thermodynamic observable, temperature. The thermodynamic
	equation for temperature is:
	\begin{equation}
		\frac{1}{2} \sum_{i=1}^{N}\sum_{e=1}^{d} v_{ie}^2 = Nd \frac{1}{2}k_B T
	\end{equation}
	where $N$ is the number of particles, $d$ is the dimension of the system (in our case 2) and
	T, the temperature. In the system that $k_B$ is $1$, we can rewrite the equation as follows:
	\begin{equation}
		\sum_{i=1}^{N} \sum_{e=1}^{d} v_{ie}^2 = N d T
	\end{equation}
	one should take note that in our case, we used a convention, namely that the system is
	stabilized (i.e., the CoM velocity is zero). In Thermodynamics this doesn't matter due to the
	size of our ensemble, but in MD it matters. So we have:
	\begin{equation*}
		N d T \rightarrow (N-1) d T
	\end{equation*}

	Now we use the "Virial Theorem" to find and equation for the pressure of the system.
	\begin{equation}
		P V = N k_B T - \frac{1}{2 N d} \sum_{i,j = 1}^{N} \vec{F}_{ij}.\vec{r}_{ij}
	\end{equation}
	 Now we have an equation to calculate pressure in-time.

	 The values for temp. and pressure are available in Fig~\ref{fig:temp_pressure}

	 \begin{figure}[h!]
		\centering
		\includegraphics[width=.45\linewidth]{../results/temp100_5000.jpg}
		\includegraphics[width=.45\linewidth]{../results/pressure100_5000.jpg}
		\caption{On the left, you can see the plot for temperature over time. the unit for temp. is Kelvin. On the right, you can see the values for
		reduced pressure. The unit for time on both plots is $10^{-2} \tau$ where $\tau$ is time in reduced units.
		the inital value for velocity is $1.5$}
		\label{fig:temp_pressure}
	 \end{figure}

 	We find the temperature and pressure of the system in equilibrium, by taking the mean value over time. The values are found below:
 	\begin{equation*}
 		T_{eq} = 87 \pm 5 K, \; P_{eq} = 0.080 \pm 0.005 \text{(reduced unit)}
 	\end{equation*}

	\subsubsection{Velocity Correlation and Relaxation Time}
	I chose the initial conditions to be the same as in the previous section.
	I ran the simulation for 2000 time steps (waiting for the system to reach equilibrium), and then I saved the
	data for the velocity of particles for every 2 time steps in a file in the \texttt{data/} directory.

	Then using the file \texttt{velocity\_correlation.py}, I found the correlation and plotted in
	Fig~\ref{fig:velocity_correlation}

	\begin{figure}[h!]
		\centering
		\includegraphics[width=.9\linewidth]{../results/velocity_correlation.jpg}
		\caption{Velocity correlation for different values of seperation $\tau$. The initial velocity in the simulation was
			$0.5$.}
		\label{fig:velocity_correlation}
	\end{figure}

	And so by crossing the auto-correlation curve with the $y = e^{-1}$ line, we find the relation time of the
	system, $T$, to be:
	\begin{equation}
		T = 2564 \times 10^{-3} \tau = 2.564 \tau, \; \tau \simeq 10^{-12} s \Rightarrow T \simeq 2.564 \times
		10^{-12} s
	\end{equation}

	\subsubsection{State Equation check with the Leonard-Jones gas}
	In order to check this system with the Leonard-Jones gas, I have shown the P-T plot of the system, available in Fig~\ref{fig:P-T}
	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.9\linewidth]{../results/pressure_temp.jpg}
		\caption{The P-T plot of the system to check with the Leonard-Jones system.}
		\label{fig:P-T}
	\end{figure}

	\subsection{Phase transition of the system for different Temperatures}
	In order to see the phase transition of the system we plot the energy E of the system vs. the temperature. And by the change in
	$C_v = \derivative{E}{T}$ we can see the phase transitions. I varied the energy and temperature of the system by changing the initial velocity
	of the particles. For each initial value, I ran the simulation 2000 time steps for the system to reach equilibrium, then, I ran 5000 times and took
	the temperature of the system and returned the mean and the standard deviation as values and error. The plot is available in
	Fig~\ref{fig:phase_transition}

	\begin{figure}[h!]
		\centering
		\includegraphics[width=0.9\linewidth]{../results/phase_transition.jpg}
		\caption{Phase Transition of the MD system. As one can see, from about 80 to 90 Kelvin, the system is in the liquid phase, gas for over 90,
		and crystal for lower than 80.}
		\label{fig:phase_transition}
	\end{figure}

	\section{Animating the system}
	Again, I wait 2000 time steps for the system to reach equilibrium and then, I start animating the system.
	The initial velocity for the crystal phase is $0.5$, for liquid, $1.5$, and for gas $3$. The animations are accessible in the \texttt{animations/}
	subdirectory.
\end{document}


\documentclass{article}
% This section of the document is the preamble

\usepackage{hyperref,lipsum,amsmath,amssymb,graphicx,youngtab,ytableau,tikz,subcaption}

% TikZ uses its own libraries of commands to make drawing easier. We add TikZ libraries just like including new packages.
\usetikzlibrary{arrows,automata,positioning}

% \graphicspath{{../Images/}} % the folder in which images are stored for this project

\hypersetup{colorlinks=true} % make sure our hyperlinks are coloured for visibility
% We define an Author, Title and Date
\author{\emph{Your Name Here}}
\title{A Short \LaTeX{} Worksheet}
\date{March 15\textsuperscript{th}, 2019}

\begin{document}
% Create a title from our Author, Title and Date
\maketitle
% We put our content into section environments
\section{Introduction}
\label{sec:introduction}
% I didn't have time to discuss labels today, but adding labels to your document allows you to refer back to the relevant place later in your document using the \ref{label} command. For example, I could refer to the introduction later in my document by saying `see section \ref{sec:introduction}'. This would produce the output `see section 1'. The advantage os using labels, is that if I add a reference to section 2, and then later add a new section before the old section 2, the reference will automatically be updated to say section 3. Try adding some references to the various sections and subsections of this document.
This worksheet has been created using the \LaTeX{} typesetting system. By reproducing this document you should be able to develop your skills with \LaTeX{}. Start by copying the basic example from the talk into a new tex file and then \emph{typesetting} your file to produce a PDF. To make a document with a title and some \emph{sections}, see the second example from the talk. You should \emph{typeset} your file regularly to see whether it is looking correct. Try it now if you haven't already. If everything has worked properly you should now have a PDF file of the worksheet up to this point.

\section{Content}
\label{sec:content}

\emph{Sections} allow us to add structure our documents. For a short document such as this, we will use \emph{sections} and \emph{subsections}. A longer document such as a thesis would also use \emph{chapters} (and the \emph{report} document class). The first thing we will discuss is how to typeset mathematics. To start with, ignore the `verbatim' code and hyperlinks in the following subsection, these will be explained later. A good reference for some of the symbols and commands in the following subsections is \href{https://wch.github.io/latexsheet/latexsheet.pdf}{the \LaTeXe{} cheat sheet}.

\subsection{Typesetting Mathematics}
\label{sub:typesetting_mathematics}

One of the reasons to use \LaTeX{} is that it can produce very good looking mathematical formulae. We have already seen some examples in the talk. \LaTeX{} only recognises mathematical commands while in `maths mode'. There are a few ways to enter maths mode -- which one we use depends on how we want the output to be formatted. If I want the maths to be `inline', part of the current line, then I place it between dollar symbols. Therefore \$ x = 4 \$ produces $x=4$. If I want to produce an equation which is placed on its own line then I can use the \emph{\textbf{equation} environment}.
\begin{verbatim}
	\begin{equation}
	    x^3 + 2 \ge 6.
	\end{equation}
\end{verbatim}
This produces
\begin{equation}
	x^3 + 2 \ge 6.
\end{equation}
See \href{https://www.overleaf.com/learn/latex/Mathematical_expressions}{this webpage} for more information on mathematics modes. Try to reproduce the rest of this subsection as closely as you can.

A \emph{bijection} is a map between two sets which is both \emph{injective} and \emph{surjective}. Let us now define these terms. Given two sets $A$ and $B$, a map $\phi:A \to B$ is said to be injective if
\begin{equation}\label{eq:injective} % I can also add referenecs to my equations using labels.
	\phi(a_1) = \phi(a_2) \iff a_1 = a_2,\ \forall\ a_1,a_2 \in A.
\end{equation}
Furthermore, the map is surjective if
\begin{equation}\label{eq:surjective}
	\forall\ b \in B,\ \exists\ a \in A\ |\ \phi(a)=b.
\end{equation}
% Try uncommenting the following line.
% If both equation \ref{eq:injective} and equation \ref{eq:surjective} are satisfied, then the map is said to be \emph{bijective}.

The talk also covered the import result of the \emph{Gaussian integral}
\begin{equation}
	\int_{-\infty}^\infty e^{-\frac{1}{2}\left( \frac{x - \mu}{\sigma} \right)^2} dx = \sigma\sqrt{2 \pi}.
\end{equation}

In the partitions project, we saw how the geometric progression formula
\begin{equation}
	\frac{1}{1-x} = 1 + x + x^2 + \ldots + x^n + \ldots = \sum_{n=0}^\infty x^n,
\end{equation}
could be used to deduce the generating function for the partition number $p(n)$,
\begin{equation}
	\sum_{n=0}^\infty p(n) x^n = \prod_{n=1}^\infty \frac{1}{1-x^n}.
\end{equation}

\subsection{Verbatim} % (fold)
\label{sub:verbatim}

In order to explain some of the commands that you will need to add to your file, we will need to show the names of the commands on the worksheet. In order for \LaTeX{} to display the command rather than process it we use an \emph{environment} called \textbf{Verbatim}.
\begin{verbatim}
	\begin{verbatim}
	    \LaTeX{} is now printed rather than processed.
	\end{verbatim }
\end{verbatim}
% Note the space after in the inner \end{verbatim }. This is so \LaTeX{} understand the difference between the one I want to print verbatim and the actual end of the verbatim environment.
We can also produce verbatim output in the middle of a line using \verb!\verb|\LaTeX{}|!.
% We can use both the exclamation mark or the pipe mark to indicate what content is to be inline verbatim. As previously, I have to use one of each to ensure that \LaTeX{} understands which one ends the actual verb environment, and which is to be printed verbatim.
\subsection{Packages}
\label{sub:packages}

One of the advantages of \LaTeX{} (or \TeX{}) is that it can be extended using \emph{packages}. A package is \emph{loaded} by adding the the line
\begin{verbatim}
	\usepackage{package_name}
\end{verbatim}
into the \emph{preamble} of your tex file. The preamble is the part of the file before the line \verb|\begin{document}|. Everything between \verb|\begin{document}| and \verb|\end{document}| is called the \emph{body} of the document. We can use a package called \href{https://ctan.org/pkg/lipsum?lang=en}{lipsum} to produce dummy text. This hyperlink was in turn created with a package called \href{latex hyperref}{hyperref} and the command used was
\begin{verbatim}
	\href{https://ctan.org/pkg/lipsum?lang=en}{lipsum}.
\end{verbatim}
The following paragraph is produce using the lipsum package, you can read about how to use this package from the package documentation on the previously linked webpage for lipsum -- the following is the lipsum paragraph 66.

\lipsum[66]

Two packages that we will often want to use are the \href{https://michael-prokop.at/latextug/amsldoc.pdf}{amsmath and amssymb} packages, which are documented at the previously linked page. They add more mathematical commands that you can then use in you documents, such as matrices, unnumbered environments and mathematical fonts. Once I have loaded amsmath and amssymb, I can prodcue the output
\begin{equation*}
	A = \begin{pmatrix}
		4 - 2i & i \\
		-i & a
	\end{pmatrix} \in M_2(\mathbb{C}).
\end{equation*}
These packages also allow us to align equations using the \textbf{align} environment. We can use this to define two block matrices $B$ and $C$. We can then give the multiplication of these matrices in a new equation environment. If
\begin{align}
	B &= \left(\begin{array}{c | c} % The c here is to centre align the array columns.
		P & Q \\
		\hline
		R & S
	\end{array}\right),\nonumber \\
	C &= \left(\begin{array}{c | c}
		W & X \\
		\hline
		Y & Z
	\end{array}\right),
\end{align}
then
\begin{equation}
	BC = \left(\begin{array}{c | c}
		PW + QY & PX + QZ \\
		\hline
		RW + SY & RX + SZ
	\end{array}\right).
\end{equation}
% There's quite a lot going on in the previous few lines, this was supposed to be the hardest part to reproduce for anyone who already had some experience with latex. The \left( and \right) commands are used to produce parentheses which scale automatically based on what is inside them. The array environment lets me format content in columns and specify a separator. I use this to produce the vertical line of the block matrix, the horizontal line is produced with the \hline command. The ampersand character & is used by \LaTeX{} to align elements. It is used between the B and the equals and between the C and the equals to align these two lines. The double slash \\ after the first right) is used to end the first line and stat a new line. The ampersand is also used inside the array environment to tell \LaTeX{} how to align the elements of the columns. Finally, the \nonumber command suppresses the equation number for the first line of the align environment.

\section{Images}
\label{sec:images}
You may want to include images in a \LaTeX{} document. If you already have the image file saved on your computer, you can do this using the command
\begin{verbatim}
	\includegraphics[scale=1]{imagename}
\end{verbatim}

For this to work, the image must be in the same folder as your tex file and we must include the \emph{graphicx} package. This adds an image like so, \includegraphics[scale=0.1]{DULogo}.
%In fact, we can put our pictures in a separate location to our tex file by setting a 'graphics path'. We do this by putting the command '\graphicspath{{location}}' in our document preamble. I've done this in the preamble to this document. Note that the location can either be given as an absolute folder path such as 'C:/...' on Windows, or '/...' on Unix based systems (Mac, Linux, etc.), or as a relative folder path such as '../folder'.

The easiest way to add an image to a \LaTeX{} document is therefore to create an image file (using a drawing tool, or by hand and then taking a picture of your drawing) and then to include the image using the \verb|\includegraphics| command.

Images often look better when included in a \emph{figure} environment as shown in figure \ref{fig:DULogo}. This also allows us to label the image and add a caption. See the documentation at \href{https://www.overleaf.com/learn/latex/Inserting_Images}{Overleaf} for more details.

\begin{figure}[htbp]
	\centering
		\includegraphics[scale=0.1]{DU-logo}
	\caption{The Durham University Logo}
	\label{fig:DULogo}
\end{figure}
% The options to the figure environment specify the places where LaTeX can place the figure. In this example, [htbp] means 'here', 'top', 'bottom', 'page'. This means that LaTeX will first try to place the figure in the location that it appears in the source tex file. If it can't place the figure there (what can't means is complicated, see https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat/39020#39020 for a detailed explanation), then it next tries to place it at the top of either the current page, or the next page. If it can't do this, it tries to place it at the bottom of the current or next page. Finally, if none of the previous options work, it creates a new page for the figure.

There also exist many packages for creating images. Those on the partitions project may want to know about \href{http://www.ctex.org/documents/packages/math/youngtab.pdf}{youngtab} or \href{http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/ytableau/ytableau.pdf}{ytableau}. The image below was created with the command \verb|\ydiagram{6,6,1,1}| from the ytableau package.

\begin{equation*}
	\ydiagram{6,6,1,1}
\end{equation*}

Another more advanced package is called \href{https://www.overleaf.com/learn/latex/TikZ_package}{Ti\emph{k}Z}. Using Ti\emph{k}Z we can create much more complicated diagrams such as the following diagrams for finite state automata and graphs (or trees, networks etc.). These commands are very complicated, have a look at the source tex file to see how they were produced.

\begin{figure}[htbp]
	\begin{subfigure}[b]{.5\linewidth}
		\centering
			\begin{tikzpicture}
				% We can specify options for the tikz picture using the tikzset command
				\tikzset{
				        ->,  % makes the edges directed
				        >=stealth', % makes the arrow heads bold
				        node distance=3cm, % specifies the minimum distance between two nodes. Change if needed
				        every state/.style={thick, fill=gray!10}, % sets the properties for each ’state
				        initial text=start, % sets the text that appears on the start arrow
				}
				% We define the states to be initial or accepting as required. They are positioned relative to the other nodes.
			    \node[state, initial] (0p) {$0$p};
				\node[state, below right of=0p] (20p) {$20$p};
				\node[state, above right of=0p] (10p) {$10$p};
			    \node[state, accepting, below right of=10p] (30p) {$\ge 30$p};
		    	% The sloped command tells tikz to turn the label sideways. I used this to help all the labels fit into a relatively compact space.
			    \draw   (0p) edge[above, sloped] node{$10$p} (10p)
						(0p) edge[below, sloped] node{$20$p} (20p)
			            (10p) edge[left] node{$10$p} (20p)
						(10p) edge[bend right, above, sloped] node{$20$p} (30p)
						(30p) edge[bend right, above, sloped] node{$10$p} (10p)
						(20p) edge[above, sloped] node{$20$p} (30p)
						(20p) edge[bend left, above, sloped] node{$10$p} (30p)
						(30p) edge[bend left, above, sloped] node{$20$p} (20p);
			\end{tikzpicture}
		\caption{The transition diagram for a vending machine}
		\label{fig:fsa}
	\end{subfigure}
	\begin{subfigure}[b]{.5\linewidth}
		 \centering
		 	\begin{tikzpicture}
				% A graph probably wants different options to a FSA
				\tikzset{node distance=2cm, % specifies the minimum distance between two nodes. Change if needed
				}
				\node[draw,circle] (a) {$a$};
				% For the map/graph colouring examples, you may want to colour your nodes as shown here.
				\node[draw,circle,right of=a,fill=red] (b) {};
				% The exclamation mark after orange sets some transparency, try changing it.
				\node[draw,circle,above right of=a,fill=orange!40] (c) {};
				\node[draw,circle,below of=b,fill=none] (d) {};
				% This node shows how you can label the nodes in your graphs.
				\node[draw,circle,above of=a,fill=green] (e) {e};
				% The next node is never going to appear in the picture, it's used to make one of the curving paths in the diagram.
				\node[above right of=c,draw=none,minimum size=0cm,inner sep=0] (f) {};
				% This edge has a label, so could be used to represent capacity or edge weight.
				\draw (a) edge[above,->] node{10} (b);
				% I can change the line ends to either include arrows or not as the next few edges show.
				\draw (a) edge[->] (c);
				\draw (b) edge[-] (c);
				\draw (b) edge[<->,right] node{4} (d);
				\draw (a) edge[-] (e);
				\draw (a) edge[bend right] (d);
				\draw (b) edge[bend right] (f);
				\draw (f) edge[bend right,->] (e);
				% Can you tell what the .north part of the next line does? Try changing it to '.south west'. The looseness command has been added to stop the line from cutting through node c.
				\draw (b.north) edge[bend right,looseness=2.1] (e.east);
			\end{tikzpicture}
		\caption{A simple graph}
		\label{fig:graph}
	\end{subfigure}
\end{figure}

\section{Conclusion}
\label{sec:conclusion}
Regardless of how much of this sheet you have been able to reproduce, I hope you have learnt something about \LaTeX{}. In my opinion, it is best to learn \LaTeX{} simply by starting to use it, and answering any questions you have as you go by using the many excellent resources online. The \href{https://www.overleaf.com/learn}{Overleaf} website contains a lot of useful help, as does \href{https://tobi.oetiker.ch/lshort/lshort.pdf}{The Not So Short Introduction To \LaTeXe{}}. The tex file used to produce this talk will be available shortly so that you can compare it with your tex file. I have also added some comments to my file to explain some additional things, so I encourage you to read through if you are interested.

\end{document}


<source-file filename="assignment_methods.tex" project="Assignment_methods">

\documentclass[12pt]{article}
\begin{document}
\title{PBGG Notes 2011: The use of assignment methods in population genetics.}
\author{Graham Coop$^{1}$ \\
\small $^1$ Department of Evolution and Ecology \& Center for Population Biology,\\
}
\date{}
\maketitle

Here I describe a simple probabilistic assignment to find the probability that an individual of unknown population comes from one of $K$ predefined populations. I then briefly explain how to extend this to cluster individuals into $K$ initially unknown populations. This method is a simplified version of what Bayesian population genetics clustering algorithms such as STRUCTURE (Pritchard et al. Genetics 2000).

\paragraph{A simple assignment method}

We have genotype data from unlinked S bi-allelic loci for $K$ populations. The allele frequency of allele $1$ at locus $l$ in population $k$ is denoted by $p_{k,l}$, so that the allele frequencies in population 1 are $p_{1,1},\cdots p_{1,L}$ and population 2 are $p_{2,1},\cdots p_{2,L}$ and so on.

You type a new individual from an unknown population at these $L$ loci. This individual's genotype at locus $l$ is $g_l$, where $g_l$ denotes the number of copies of allele 1 this individual carries at this locus $g_l=0,1,2$).

The probability of this individual's genotype at locus $l$ conditional on coming from population $k$ (i.e. their alleles being a random HW draw from population $k$) is
\begin{equation}
P(g_l | \textrm{pop k}) = I(g_l=0) (1-p_{k,l})^2 +  I(g_l=1) 2 p_{k,l} (1-p_{k,l}) + I(g_l=2) p_{k,l}^2
\end{equation}
where $I(g_l=0)$ is an indicator function which is $1$ if $g_l=0$ and zero otherwise, and likewise for the other indicator functions.

Therefore, assuming that the loci are independent, the probability of individual's genotypes conditional on them coming from population $k$ is
\begin{equation}
P(\textrm{new ind.} | \textrm{pop k})  = \prod_{l=1}^S P(g_l | \textrm{pop k}) \label{eqn_assignment}
\end{equation}

We wish to know the probability that this new individual comes from population $k$, i.e. $P(\textrm{pop k} | \textrm{new ind.})$. We can obtain this through Bayes rule
\begin{equation}
 P(\textrm{pop k} | \textrm{new ind.})  = \frac{P(\textrm{new ind.} | \textrm{pop k}) P(\textrm{pop k})}{P(\textrm{new ind.})}
\end{equation}
where
\begin{equation}
P(\textrm{new ind.}) = \sum_{k=1}^K  \frac{P(\textrm{new ind.} | \textrm{pop k}) P(\textrm{pop k})}{P(\textrm{new ind.})}
\end{equation}
is the normalizing constant. We interpret $P(\textrm{pop k})$ as the prior probability of the individual coming from population $k$, unless we have some prior knowledge we will assume that the new individual has a equal probability of coming from each population $P(\textrm{pop k})=1/K$.

We intepret
\begin{equation}
 P(\textrm{pop k} | \textrm{new ind.})
\end{equation}
as the posterior probability that our new individual comes from each of our $1,\cdots, K$ populations.

More sophisticated versions of this are now used to allow for hybrids, e.g, we can have a proportion $q_k$ of our individual's genome come from population $k$ and estimate the set of $q_k$'s.
\paragraph{clustering based on assignment methods}
We wish to cluster our individuals into $K$ unknown populations. We begin by assigning our individuals at random to these $K$ populations.
\begin{itemize}
\item Given these assignments we estimate the allele frequencies at all of our loci in each population.
\item Given these allele frequencies we chose to reassign each individual to a population $k$ with a probability given by eqn. ($\ref{eqn_assignment}$).
\end{itemize}
We iterate steps 1 and 2 for many iterations. If the data is sufficiently informative the assignments and allele frequencies will quickly converge.

Technically this is the joint posterior of our allele frequencies and assignments.

\end{document}

</source-file>


\subsection{Geometric proof (using Compactness)}

After the exhaustive proof of the previous section, we wanted to
refresh the reader with a totally different type of proof for SVD; one
that brings a new perspective for the way in which the special basis
for \R{n} is chosen. \\

Thinking about modular theorem proving, that is, factorizing common
results for the sake of clarity; one could consider that the
\cref{thm:SVD1} and \cref{thm:SVD2} are some kind  of common step
for many possible SVD proofs. They deal with the task of proving that,
given certain condition over the bases in \R{n} and \R{m} ($A\vec{v_i}
= \sigma_i\vec{u_i}, \ds{\forall} i=1\dots\func{rank}(A)$); the SVD
factorization holds. Those auxiliary
theorems then, reduce the task of proving the SVD
\cref{thm:SVD} to the much more specific (but hard) subproblem of
finding the bases. Per discussion in previous section, we know
that such problem can be reduced even further, to the one of finding
the orthonormal basis for \R{n} $\{\vec{v_1},\vec{v_2},\dots,\vec{v_n}\}$, such
that its orthogonality is preserved through $A$
(\cite{kalman96}). This last remark can be considered the true essence of a
whole family of proofs for SVD Theorem, where each one brings a
particular way of finding a basis with such an special
property. \\

Intuitively, this property of preserving orthogonality could be
thought as a generalization of the eigenvectors behavior, which
are the vectors not ``moved'' by transformation $A$ but just scaled; this in
particular implies that if the eigenvectors formed an orthogonal basis
of the space prior application, their images under $A$ will still form a
basis (eigenvectors are defined when $A$ has signature $\R{n} \fromto
\R{n}$, which means $A$ is an square matrix). Something similar occurs
for the \vec{v}\apos{s} in SVD, but extended to a couple of spaces
instead of just one: we can not expect these vectors are not moved by
$A$, as they migrate of space ($\R{n} \fromto \R{m}$); but we request
that whatever landing they do on \R{m}, they still form an orthogonal
basis there. \\

The proof we are about to present now is thanks to Blank et al
\cite{blank89}; which presents a quite interesting approach: using
pure geometric arguments \footnote{With an implicit use of
  compactness} he finds the basis whose orthogonality under $A$ gets
preserved. \\

The whole reasoning occurs on the unit sphere and its image over an
square matrix $A$; and here comes a great connection with our previous
comments about the true essence of an arbitrary matrix $A$ of $m \times
n$ with rank $r$: the real information is on the mapping from the row
space to the column space ($\C{\trans{A}} \fromto \C{A}$), and
restricted to those subspaces $A$ is a bijection $\R{r} \fromto
\R{r}$; working with a
bijective linear transformation means that $\inv{A}$ does
exist. Without loosing generality then, we will assume that matrix $A$ is
square and non-singular (invertible); because if it was not, we can do
a zoom and focus on its embedded bijection $\R{r} \fromto \R{r}$, and
calculate the basis there (and later extend to whole basis of host
spaces \R{n} and \R{m}). \\

The unit sphere is picked as the source of the \vec{v}\apos{s}, simply
because we want them to be an orthonormal basis (which in particular
requires them to be unitary). For starting to form this basis, we
could start picking an arbitrary unit vector; picking a second vector
in the sphere such that is perpendicular to the former is no issue
either; but how pick the second one such that the property the
property $\vec{v_1} \perp \vec{v_2}$ is preserved through $A$? Every
vector $\vec{v}$ in \R{r} defines a hyperplane $P$,
which actually happens to be a subspace on its own. But such hyperplane
is actually the orthogonal complement of the subspace generated by
the vector alone; which implies that $P \perp \vec{v}$. So what? $P$
merely becomes an infinite source of orthogonal vectors to the second
choice \vec{v}; but the question of how to pick one such that the
orthogonality property gets preserved, is still unanswered. \\

The main point of the proof in \cite{blank89}, is that if we choose
properly the first vector \vec{v},
meaning if we choose the one which gets the maximum expansion through
$A$, then the orthogonality relation of \vec{v} with the hyperplane it
defines gets preserved. Here comes then a very powerful and beautiful
idea at the same time: having found a whole subspace that is
orthogonal to the first choice vector, allows one to forget about the
original host space \R{r} and focus on that subspace only; the new
hyperplane would essentially be \R{r-1} embedded in \R{r}, and the
intersection with the sphere in \R{r} would the unit sphere in
\R{r-1}. Thus, we can apply recursively the same procedure in that
subspace, for finding the next unit vector such that the
orthogonality of its hyperplane gets preserved through $A$! \\

Therefore, the theoretical recursive algorithm would be to find one vector
\vec{v_i} at a time, by working only on a subspace of dimension
$r-i+1$ (\vec{v_1} is found in whole \R{r}, \vec{v_2} is found in an
embedded \R{r-1}, \vec{v_3} in the nested embedded \R{r-2}, etc). If
we wanted to think in a proof rather than a constructive algorithm, we
could use induction and claim that we know how to find the first $r-1$
vectors \R{r-1}, and proceed to find the remaining vector in \R{r}. \\

We have reduced then, the problem of finding the right basis of
\vec{v}\apos{s} to the following theorem: \\

\begin{theorem}

\label{thm:svdgeo}
Let $A$ a non-singular matrix of $r \times r$; let be vectors
$\vec{v},\vec{w} \in \R{r}
\suchthat A\vec{v} = \vec{w} \ds{\land}
\norm{\vec{w}}_2 = \max\left\{\norm{\vec{x}}_2 \suchthat \norm{\inv{A}\vec{x}}_2= 1 \right\}$
and let $S$; $T$ be hyperplanes in
\R{r} $\suchthat$ $S$ is the orthogonal hyperplane of \vec{v}; and $T$
is image of $S$ under $A$ $\implies$ $T \perp \vec{w}$ ($T$ is also the
orthogonal hyperplane of \vec{w}).
\end{theorem}

To visualize the artifacts mentioned in the theorem, let us pay
attention then to the following picture, which is the unit
sphere in $\R{r}$, mapped to an ellipsoid in $\R{r}$ \footnote{A
  formal argument is actually required to prove that the image of the
  unit sphere is an ellipsoid, and even telling that is the surface of
a quadratic form requires a little development. But we will omit those
details, aiming to keep the spirit of this short proof.} In order to
prove that $T \perp \vec{w}$, we will use the auxiliary hyperplanes
$S_1$ and $T_1$ (where the second is the image under $A$ of the
former). Of course the picture aims to represent \R{3}, and
the hyperplanes would be embeddings of \R{2}; but let us just consider
them a visual representation of arbitrary dimension objects (the only
representation we can imagine, indeed). \\

\begin{figure}[h]
  \centering
  \includegraphics[width=13cm]{svd-geo-proof}
  \caption{Geometrical proof of SVD Theorem: Transformation $A$ preserves the
    relation $S \perp \vec{v}$}
  \label{fig:svdgeop}
\end{figure}
\hfill

\begin{proof}
The geometric proof goes like this: \\

\begin{enumerate}
\item The hyperplane $S_1$ touches the unit sphere only at point
  \vec{v}. It is a geometrical result that such hyperplane is unique
  and that $S_1 \perp \vec{v}$. \\

\item By definition, the hyperplane $T_1$ is the image of $S_1$ under
  $A$; also, the whole unit sphere is mapped by $A$ into an ellipsoid. Since
  $A$ is a bijective function, then $T_1$ must touch the ellipsoid only in
  point \vec{w} (just like $S_1$ touches the unit sphere only at point
  \vec{v}). Actually, $T_1$ must be the only hyperplane with such
  property (otherwise, we could apply \inv{A} to that other hyperplane,
  and it would produce a different hyperplane that also touches
  \vec{v} in the unit sphere; contradicting previous point about the
  uniqueness of $S_1$). \\

\item Now take another sphere, big enough to cover the deformed image of
  the unit sphere under $A$ (the ellipsoid). Start to shrink
  such sphere until it touches the ellipsoid for the first time; per
  definition, \vec{w} must be part of those points of first
  contact. \\

\item Now consider the hyperplane $T_2$ that touches this shrunk sphere,
  precisely at \vec{w}. Using the same geometrical theorem of first
  argument about $S_1$, we can tell such hyperplane is unique and is
  orthogonal to \vec{w}. \\

\item Since this adjusted sphere covers entirely the ellipsoid (per
  definition of \vec{w}), then $T_2$ also touches the ellipsoid at
  point \vec{w}. But we argued that $T_1$ was the only hyperplane
  touching the ellipsoid at \vec{w} $\implies$ $T_1 = T_2 \ds{\land} T_1
  \perp \vec{w}$. \\

\item Both hyperplanes $S$ and $S_1$ are orthogonal to \vec{v}; by
  geometrical arguments they must be parallel then. \\

\item Linear transformations, in particular $A$, preserve parallelism;
  since, $S \parallel S_1$ $\implies$ their respective images under $A$
  must be parallel as well. \\

\item The image of $S_1$ under $A$ is $T_1$, then, whatever becomes
  the image of $S$ under $A$; it must be parallel to $T_1$. Let us
  call this image $T$. \\

\item $\therefore$ $T \parallel T_1 \ds{\land} T_1 \perp \vec{w}$
  $\implies$ $T \perp \vec{w}$.
\end{enumerate}
\end{proof}
\hfill

The key choice in the proof was \vec{w}, as being the biggest axis of
the ellipsoid makes it coincide with the sphere of radius
$\norm{w}_2$; and that in turns allows us to transfer the properties
of the hyperplane that touches the sphere at \vec{w} to the one that
touches the ellipsoid at same point (as they become same hyperplane
indeed). It is no coincidence then, that the spectral norm $\norm{A}_2$
is actually defined as $\norm{\vec{w}}_2$; that is, is defined
as the maximum expansion $\norm{A\vec{x}}_2$ that transformation $A$ causes
on the vectors belonging to the unit sphere. This norm actually, is
used in the algebraic proof of Golub in \cite{golub13}, of the SVD
Theorem. \\

The last question the reader may have now is: where was the
compactness property used? It may not be explicitly stated, but it
lies behind the definition of \vec{w}: $\norm{\vec{w}}_2 =
\max\left\{\norm{\vec{x}}_2 \suchthat \norm{\inv{A}\vec{x}}_2 = 1
\right\}$. The reason why \vec{w} exists
on the first place, is because the ellipsoid is a compact set (it
inherits that property from its pre-image, the unit sphere, thanks to
the continuity of function $A$\footnote{Though we did not find the
  name for such theorem, it must exists and state that continuous
  functions preserve compactness.}). Since the norm function $\norm{.}_2$
is also continuous, then by a generalization of the Extreme Value
Theorem from Calculus, it must reach its maximum on a point of the
ellipsoid (we named that particular point as \vec{w} in the proof). \\