\documentclass[12pt,a4]{article}

\newcommand{\handoutdate}{Friday, 2020-04-17}
\newcommand{\firstduedate}{Friday, 2019-04-24}
\newcommand{\finalduedate}{Tuesday, 2020-05-01}

\input{preamble}

\setcounter{section}{3}

\section{Bottleneck Paths}

Let $G=(V,E)$ be a directed graph with an edge capacity function $c: E \rightarrow \R^+$. For a path
$p = u_0 u_1 \dots u_t$ define its {\em capacity} to be
\begin{align}
   c(p) := \min_{1 \leq i \leq t} c( \{u_{i-1}, u_i\}) \ .
\end{align}

\begin{quotation}
    \textbf{Maximum Capacity Path Problem (MCP).} Given a directed graph $G = (V,E)$, an edge capacity function
    $c: E \rightarrow \R^+$, and two vertices $s, t \in V$, compute the path $p^*$ maximizing $c(p)$. We
    denote by $p^*$ the optimal path and by $c^* := c(p^*)$ its cost.
\end{quotation}

\begin{exercise}
   Suppose the edges $e_1,\dots,e_m$ are sorted by their cost. Show how to solve MCP in time $O(n+m)$.
\end{exercise}

\begin{proof}
Design a algorithm following Pseudocode shows(Suppose the edges are sorted decreased, See in Algorithm 1):

And then we think about the correctness and complexity.

The algorithm means we can enum the answer. When we find a edge between the point visited and not visited, we can go through all the edges which's costs is higher than this edge. If now s is connected to t, means this edge is the largest edge while going through all edges higher than it from $s$ to $t$. That fits the answer we want.

Now, let's think about the complexity. For every node, it may be in queue at least once. And for every edge, it may be in $G'$ and used in bfs at least once. So the time complexity is $O(n+m)$.
\begin{algorithm}[H]
\caption{Solve MCP in time $\Theta(n+m)$ with all the edges sorted by their cost}
\begin{algorithmic}
\Procedure{MCP}{$G, s, t$}
	\State Initiate $visited \gets s$ and $G' \leftarrow \emptyset$
	\For {$e \in G$}
		\If{$e.from\in visited$ and $e.to\notin visited$}
			\State $bfs\_graph(e.to, G')$
		\Else
			\State $G'\gets G' \cup {e}$
		\EndIf
		\If{$t \in visited$}
			\State \Return $e.weight$
		\EndIf
	\EndFor
\EndProcedure
\Procedure{bfs\_graph}{$s, G$}
	\State $visited \gets visited \cup {s}$
	\State push s into the queue
	\While {queue is not empty}
		\State Let top be the head of the queue and pop the head.
		\For{$e \in {edges\ in\ G\ from\ top}$}
			\If {$e.to \notin visited$}
				\State $visited \gets visitied \cup{e.to}$
				\State push e.to into the queue
			\EndIf
		\EndFor
	\EndWhile
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{proof}

\begin{exercise}
   Give an algorithm for MCP of running time $O(m \log \log m)$. \textbf{Hint:} Using the median-of-medians algorithm,
   you can determine an edge $e$ such that at most $m/2$ edges are cheaper than $e$ and at most $m/2$ edges are
   more expensive than $e$. Can you determine, in time $O(n+m)$, whether $c^* < c(e)$, $c^* = c(e)$, or $c^* > c(e)$?
   Iterate to shrink the set of possible
    values for $c^*$ to $m/4$, $m/8$, and so on.
\end{exercise}
\begin{proof}
	As is shown by the hint, during each iteration, we can shrink the
	set of possible values of $c^{*}$, i.e.
	\[
E_1= \{e \mid c\left( e \right) \le M,e\in E'\},
		E_2 = \{e \mid c\left( e \right) > M, e\in E'\}
		\]
	We we can find a path in  $E_2$, then the lower bound $L$ of $c^{*}$ can
	be updated to $M$. Since if $c^{*}>L$, we must have a
	path $e$ with all the edges larger then $L$.

	Otherwise, the upper bound $U$ will be $M$ as there is no such path with
	all edges larger than $M$. However, if we take iterations until
	$L = U$, we will have $O\left( m\log m  \right)  $ running time. So we only do
	$ \log(s\left( m \right) )$
	iterations. Here $s$ is a place holder to decide later

	Consider the following algorithm
	\begin{algorithm}[H]
		\caption{Solve MCP in time $n\log\log n$ }
		\begin{algorithmic}
				\State $i=0, U = \infty, L = 0$
				\While {$i < \log s\left( m \right) $ }
				\State Determine the median of $\{e \mid e\in E',c\left( e \right) \le U\} $.
				\If{ $\left( V,E_2 \right) $ is $s$- $t$ connected }
				\State $E' \leftarrow E_2, L = M$,
				\Else
				\State $U = M$
				\EndIf
				\State $i=i+1$
				\EndWhile
				\State Number $t$ edges in set $\{e \in E' \mid c\left( e \right) \le U\} $ according to increasing order $e_1,e_2,\ldots.$
				\State Solve instance by \textbf{Algorithm 1} with the following
				ordering, i.e. the place of $e$ in the sorted array
				\[
						l\left( e \right) = \begin{cases}
								1, \quad c\left( e \right) \le L \\
								i, \quad  \exists i,e =e_{i} \\
								t, \quad c \left( e \right) > U
						\end{cases}
				.\]
		\end{algorithmic}
\end{algorithm}
Now we prove this algotihm is $n\log \log n$, just notice that
\[
		t = O\left( \frac{m}{s\left( m \right) } \right)
.\]
Since every iteration we make the set $\{ e  \mid  e\in E', c\left( e \right)
< U\} $ shrinks to $\frac{1}{2}$. That is
the size $t = \lvert  E'\rvert \le \frac{m}{2^{\log s\left( m \right) }}$.

Thus the running time is
\[
\log\left( s\left( m \right) \right)\cdot m + t \log t + t
\]
considering the sorting of the
$t$ edges plus the running time of \textbf{Algorithm 1} ,
now we choose $s\left( m \right) = \log m $ to minimize the time which is
\[
		m\left( \frac{\log m}{s} - \frac{\log s}{s} \right)
.\]
Hence
\[
		T =O\left( m \log\log m \right) + O \left( \frac{m}{\log m} \log \left( \frac{m}{\log m } \right) \right) +
		O \left( \log m \right)
		= O\left( m\log\log m  \right)
.\]
\end{proof}

\begin{exercise}
   Give an algorithm for MCP that runs in time $O(m \log \log \log m)$? How about $O(m \log \log \log \log m)$? How far can you get?
\end{exercise}
\begin{proof}
		We can have a better algorithm runs in $O\left( m \log\ldots\log m \right) $ (arbitrary number of log),

		Consider a set of edge $E$ with $\frac{m}{k}$ elements. We apply  \textbf{median of median} algorithm
		$k$ rounds. Each round we break one block into 2 parts. For example, in the first round, we find the median
		 $M$ of $E$, and break $E$ into
		\[
				E_1=\{e \mid c\left( e \right) < M, e\in E\} , E_2 = \{e  \mid c \left( e \right) \ge M, e\in E\}
		.\]
		And in the second round, we can obtain 4 blocks by apply MOM to $E_1,E_2$ respectively. Finally we have $2^{k}$ blocks $A_1,\ldots,A_{2^{k}}$ by using the above method
		recursively, while
		each block has $\frac{m}{k 2^{k}}$ elements. Plus by the property of median
		\[
				\forall i<j, \forall e_1 \in A_i, e_2\in A_j, c\left( e_1 \right) < c\left( e_2 \right)
		.\]
		Hence we can apply the idea we used in \textbf{Exercise 2}. That is,

	\begin{algorithm}[H]
		\caption{The key idea to solve MCP in time $n\log\ldots\log n$ }
		\begin{algorithmic}
				\State $E' = \bigcup_{i\le 2^{k}} A_{i}, L = 0, U = \max\left( E \right) $
				\For{$i$ in range$(2^{k})$}
				\If{ $\left( V,A_i \right) $ is $s$- $t$ connected}
				\State $E' \leftarrow A_i$, $L = \min \left( A_i \right) $
				\Else
				\State $U = \min \left( A_i \right) $
				\EndIf
				\EndFor
		\end{algorithmic}
\end{algorithm}
Hence our problem is reduced to a edge set $E'$ with whose size is $O\left( \frac{m}{k 2^{k}} \right) $, we
call this one  iteration. And this
costs
 \[
		 T = \sum_{i=1}^{k} \sum_{j=1}^{i} O\left( \frac{m}{k 2^{i}} \right) = k O\left( \frac{m}{k} \right) = O\left( m \right)
.\]
And let $f\left( k \right)  = k 2^{k}$, assume that after $r$ iterations, we can decide $c^{*}$ i.e. there is only one integer in the interval $E'$, we have
\[
		f^{r-1}\left( 1 \right) <  m \le  f^{r} \left( 1 \right)
.\]
Which leads that $m \ge g^{r-1}\left( 1 \right)  $, in which $g\left( x \right) = 2^{x}$. Hence $r$ is the
smallest integer such that
$1 \le  \log \log\ldots\log m$ with $r$ log, that means we only need
$r$ iterations to reduce the problem,  hence the overall running time is
$O\left( r m  \right)  $. Actually we can prove $\forall s$, $\exists m$
\[
		r < \log\log\ldots\log m \left( \text{with } s \log \right)
.\]
Otherwise $1 < \log\ldots\log r $ with $r-s$ log. Hence  $r >f^{r-s} \left( 1 \right) $, which
leads to a contradiction since $m$ and  $r$ can be arbitrarily large but $s$ is fixed.
Hence the running time could be written as $O\left( m \log \log \ldots\log m \right) $ (arbitrary number of log).
\end{proof}

\end{document}


\documentclass[11pt]{article}
\setlength{\textwidth}{7in} \setlength{\textheight}{9.8in}
\setlength{\topmargin}{-0.8in} \setlength{\oddsidemargin}{-0.25in}
\setlength{\evensidemargin}{-0.25in}

\usepackage{graphicx}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}
\usepackage{color}
\usepackage{hyperref}
\usepackage{amssymb}
\usepackage{amsmath}
%\usepackage{algpseudocode}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{Homework2  Page \thepage}
\lhead{Shen Qu}
\chead{STAT 671}

\DeclareMathOperator{\trace}{trace}

\begin{document}

%\includegraphics{../../../psulogo_horiz_bw.eps}\hfill\includegraphics{../../../deptlogo}
\noindent
This homework is an exan that was given during a previous iteration of this course. I have voluntarily kept the presentation as it was for the exam.
\noindent
\noindent

\section{Multivariate Fisher Kernel}
The Fisher kernel is a kernel for data in $\mathbb{R}^d$ which is based on a probabilistic model. Specifically, let $x \in \mathbb{R}^d$, and let $p_{\theta}$ be a probability density function indexed by a parameter vector $\theta \in \mathbb{R}^k$.  First, fix some $\theta_0 \in \mathbb{R}^k$ and define the Fisher score of a data point $x$, by $\phi(x)$, the gradient of the logarithm of $p_{\theta}$ evaluated at $\theta=\theta_0$. That is,
\begin{equation}
\phi(x)=\nabla_\theta \ln p_{\theta}(x) |_{\theta=\theta_0}
\end{equation}
Thus $\phi: \mathbb{R}^d \to \mathbb{R}^k$.

http://yongyuan.name/blog/fim-fisher-kernel.html

https://wiseodd.github.io/techblog/2018/03/11/fisher-information/

Next, define the Fisher information matrix as the following expected value:
\begin{equation}
I = E_{p_{\theta_0}}[\phi(X)\phi^T(X)]
\end{equation}
Note that $I$ is a $(k,k)$ matrix. You can use the fact that this matrix is symmetric and positive definite.
Finally, define the Fisher kernel as
\begin{equation}
K(x,y)=\phi(x)^T I^{-1} \phi(y)
\end{equation}
for any couple of points $(x,y)$, both in $\mathbb{R}^d$
\begin{enumerate}
\item Verify that $K$ is symmetric

\begin{align*}
E_{p_{\theta_0}}[\phi(X)] &= E_{p_{\theta_0}}\left[\nabla_\theta \ln p_{\theta}(x) |_{\theta=\theta_0} \right]= \int \nabla \ln p_{\theta}(x) \, p_{\theta}(x) \, \text{d}x = \int \frac{\nabla p_{\theta}(x)}{p_{\theta}(x)} p_{\theta}(x) \, \text{d}x \\
&= \int \nabla p_{\theta}(x) \, \text{d}x = \nabla \int p_{\theta}(x) \, \text{d}x = \nabla 1 = 0
\end{align*}

$I = E_{p_{\theta_0}}[\phi(X)\phi^T(X)]=E[\phi(X)^2]=V[\phi(X)^2]-E[\phi(X)]^2=V[\phi(X)^2]-0\ge 0$

For $\underset{(k,k)}{I}$ is symmetric and positive definite, $I^{-1}=L^TL$ by Cholesky decomposition, where $L$ is a lower triangular matrix with real and positive diagonal entries.

\begin{align*}
K(x,y)&=\phi(x)^T I^{-1} \phi(y)=\phi(x)^T L^T L \phi(y)=(L\phi(x))^T (L \phi(y))\\
      &=\langle L\phi(x),L\phi(y)\rangle_{\mathcal{H}}=\langle L\phi(y),L\phi(x)\rangle_{\mathcal{H}}=K(y,x)
\end{align*}

where $\mathcal{H}$ is a Hilber Space. $L\phi(\cdot)$ is a function $\mathcal{X}\to \mathbb{R}$

\item Verify that $K$ is positive definite

Choose $x_1,..x_n\in\mathcal{X}$; $\alpha_1,..\alpha_n\in\mathbb{R}$

$$\sum_{i,j=1}^n\alpha_i\alpha_j k(x_i,x_j)=\sum_{i,j=1}^n\alpha_i\alpha_j(L\phi(x_i))^T (L \phi(x_j))=(\sum_{i=1}^n\alpha_i L\phi(x_i))^T (\sum_{j=1}^n\alpha_j L \phi(x_j))=\|\alpha L\phi(x)\|^2_{\mathcal{H}}\ge 0$$

\item
Consider the following multivariate Normal model with given invertible covariance matrix $\Lambda^{-1}$:
$p_\theta(x)=(2\pi)^{-d/2}(\det{\Lambda})^{1/2}\exp\left(-\frac{1}{2}(x-\theta)^T\Lambda (x-\theta)\right)$
Show that
$\phi(x)=\Lambda (x - \theta_0)$

\begin{align*}
\phi(x)&=\nabla_\theta \ln p_{\theta}(x) |_{\theta=\theta_0}=\nabla_\theta \ln(2\pi)^{-d/2}(\det{\Lambda})^{1/2}\exp\left(-\frac{1}{2}(x-\theta_0)^T\Lambda (x-\theta_0)\right)\\
&=\nabla_\theta -\frac{d}2\ln(2\pi)+\frac{1}2(\det{\Lambda})-\frac{1}2(x-\theta_0)^T\Lambda (x-\theta_0)\\
&=-\frac{2}2\Lambda (x-\theta_0)(-1)=\Lambda (x - \theta_0)
\end{align*}

\item Compute the Fisher information matrix $I$ for this model
\begin{align*}
I &= E_{p_{\theta_0}}[\phi(X)\phi^T(X)]=E_{p_{\theta}}[(\Lambda (x - \theta))^T\Lambda (x - \theta)]\\
  &=\Lambda E_{p_{\theta}}[(x - \theta)^T (x - \theta)]\Lambda=\Lambda^3
\end{align*}

\item Compute the Fisher kernel for this model

\begin{align*}
K(x,y)&=\phi(x)^T I^{-1} \phi(y)=(\Lambda (x - \theta_0))^T(\Lambda^3)^{-1}\Lambda (x - \theta_0)\\
  &=(x - \theta_0)^T\Lambda(\Lambda\Lambda\Lambda)^{-1}\Lambda (y - \theta_0)=(x-\theta_0)^T\Lambda^{-1}(y - \theta_0)
\end{align*}

\end{enumerate}

\section{Optimal ordering}
We consider a binary classification problem where the training set is $\mathcal{D}=\{(x_i,y_i), 1\leq i \leq n\}$, with $x_i \in \mathbb{R}^d$ and $y_i \in \{-1,+1\}$ is the class. For convenience, we consider the following subset of indices
\begin{eqnarray}
I_-&=&\{i, 1\leq i \leq n, y_i=-1\}\\
I_+&=& \{i, 1\leq i \leq n, y_i=+1\}
\end{eqnarray}
Moreover, notate $n_-$ the number of elements in $I_-$, and $n_+$ the number of elements in $I_+$. Thus
\begin{equation}
n_-+n_+=n
\end{equation}
 Our objective is to use $\mathcal{D}$ to construct a function $f:\mathbb{R}^d \to \mathbb{R}$ that assign larger values to the data points in the positive class than to the data points in the negative class while being smooth in some sense.

Thus, we propose to choose $f$ in a RKHS with a kernel K that minimizes the following functional:
\begin{equation}
J_0(f) = \frac{1}{n_- n_+}\sum_{i \in I_-}\sum_{j \in I_+} \mathbb{I}_{f(x_i)>f(x_j)} + \lambda ||f||_H^2
\end{equation}
 where $\mathbb{I}_u$ is the indicator function of the event $u$. It takes the value 1 if the event u occurs and 0 otherwise and $\lambda>0$.

Since $J_0$ is not convex, we propose instead to minimize
\begin{equation}
J(f) = \frac{1}{n_- n_+}\sum_{i \in I_-}\sum_{j \in I_+}\left(1-\left(f(x_j)-f(x_i)\right)\right) + \lambda ||f||_H^2
\end{equation}

\begin{enumerate}
\item Show that the minimum is achieved for a function $f$ index by a parameter $\alpha \in \mathbb{R}^d$ and of the form
\begin{equation}
f(x)=\sum_{i=1}^n \alpha_i K(x_i,x)
\end{equation}
Note: do not invoke the representer theorem. Instead, use the key elements in the proof of this theorem applied to this particular case.

Let $v=\text{span} [k(\cdot,x_i),..,k(\cdot,x_n)]$ $\mathcal{V}$ is closed linear subspace of $\mathcal{H}$. Then all minimizers of $J$ belong to  $\mathcal{V}$.
Thus, there is an unique decomposition $f=f_v+f_{\perp}$ with $f_v\in\mathcal{V}$

$\forall f\in\mathcal{V}$, $\langle f_{\perp},g\rangle=0$

$$\|f\|^2_{\mathcal{H}}=\|f_v+f_{\perp}\|^2_{\mathcal{H}}=\langle f_v+f_{\perp},f_v+f_{\perp}\rangle=\langle f_v,f_v\rangle+\langle f_{\perp},f_{\perp}\rangle+\underbrace{2\langle f_v,f_{\perp}\rangle}_{0}=\|f_v\|^2_{\mathcal{H}}+\|f_{\perp}\|^2_{\mathcal{H}}$$

$$f(x_i)=\langle f,k(\cdot,x_i)\rangle=\langle f_{v}+f_{\perp},k(\cdot,x_i)\rangle=\langle f_{v},k(\cdot,x_i)\rangle+\underbrace{\langle f_{\perp},k(\cdot,x_i)\rangle}_{0}=f_v(x_i)$$

For $f$ is strictly increasing

\begin{align*}
J(f)-J(f_v) &= \frac{1}{n_- n_+}\sum_{i \in I_-}\sum_{j \in I_+} [1-\left(f(x_j)-f(x_i)\right)]+ \lambda\|f\|_{\mathcal{H}}^2
              -\frac{1}{n_- n_+}\sum_{i \in I_-}\sum_{j \in I_+} [1-\left(f_v(x_j)-f_v(x_i)\right)]- \lambda\|f_v\|_{\mathcal{H}}^2\\
&=\lambda \|f\|_{\mathcal{H}}^2-\lambda \|f_v\|_{\mathcal{H}}^2=\lambda \|f_{\perp}\|_{\mathcal{H}}^2\ge 0
\end{align*}

The representer theorem allows us to reduce the optimization problem to a finite dimensional optimization problem.

Let $\alpha=(\alpha_1,..,\alpha_n)^T\in\mathbb{R}^n$ is the solution of $\min J(f)$, the function $f(x)=\sum_{i=1}^n \alpha_i k(x_i,x)$,$f \in\mathcal{H}$ that minimizes $J(f)$

\item Rewrite then $J(f)$ as a functional $J(\alpha)$ using the notation  $K$ for the $(n,n)$ matrix $K_{ij}=K(x_i,x_j)$ and $K_i$ for the $i^{th}$ column of $K$.

$$\|f\|^2_{\mathcal{H}}=\langle f,f\rangle_{\mathcal{H}}=\langle \sum_{i=1}^n\alpha_i k(\cdot,x_i),\sum_{j=1}^n\alpha_j k(\cdot,x_j)\rangle_{\mathcal{H}}=\sum_{i,j=1}^n\alpha_i\alpha_j\underbrace{\langle k(\cdot,x_i),k(\cdot,x_j)\rangle_{\mathcal{H}}}_{k(x_i,x_j)}=\underset{(1,n)}{\alpha^T}\underset{(n,n)}{K}\underset{(n,1)}{\alpha}$$

$$f(x_i)=\sum_{j=1}^n\alpha_j k(x_i,x_j)=\sum_{j=1}^n\alpha_j[\underset{(n,n)}{K}]_{i,j}=[K\alpha]_i$$

$$J(\sum_{i=1}^n\alpha_i k(x_i,\cdot))=\frac{1}{n_- n_+}\sum_{i \in I_-}\sum_{j \in I_+} [1-([K\alpha]_j-[K\alpha]_i)]+ \lambda \alpha^T{K}\alpha$$

\item Simplify further the expression $J(\alpha)$ using the notations:

$K_-=\frac{1}{n_-}\sum_{i \in I_-}K_i$;
$K_+=\frac{1}{n_+}\sum_{i \in I_+}K_i$;

$$J(\alpha)=1-[\frac{1}{n_+}\sum_{j \in I_+}[K\alpha]_j-\frac{1}{n_-}\sum_{i \in I_-}[K\alpha]_i)]+ \lambda \alpha^T{K}\alpha=1-[K_+-K_-]\alpha+ \lambda \alpha^T{K}\alpha$$

\item Compute $\nabla_\alpha J(\alpha)$, the gradient in $\alpha$ of $J(\alpha)$. Solve for $\alpha$, assuming that $K$
is invertible.

p.d. $K,X$ is symmetric, $K=K^T$, $X=X^T$; $K=P\Lambda P^T$; $I=PP^T$; $\Lambda$ is diagonal matrix with $\gamma_1,..,\gamma_n$.

$\lambda>0$,$\gamma_i>0$, $K+\lambda I=P(\Lambda+\lambda I) P^T$ is inversible.

\begin{align*}
\nabla_\alpha J&=\frac{\partial}{\partial\alpha}(1-[K_+-K_-]\alpha+ \lambda \alpha^T{K}\alpha)=-[K_+-K_-]+ \lambda \frac{\partial}{\partial\alpha}\langle\alpha,K\alpha\rangle\\
&=-[K_+-K_-]+ \lambda (IK\alpha+K^T\alpha)=-[K_+-K_-]+2\lambda K\alpha\overset{set}{=}0\\
\end{align*}

$$\alpha^\star=(2\lambda K)^{-1}[K_+-K_-]$$

\item Bonus question: Compute $f^*(x)$, where $f^*$ is the minimizer of $J(f)$ for the linear kernel $K(x,y)=x^Ty$. Use the notations
${x}_-=\frac{1}{n_-}\sum_{i \in I_-}x_i$;
${x}_+=\frac{1}{n_+}\sum_{i \in I_+}x_i$

\end{enumerate}

$$\frac{1}{n_+}\sum_{j \in I_+}f(x_l)=\frac{1}{n_+}\sum_{j \in I_+}\sum_{l=1}^n\alpha_l k(x_l,x_j)= \sum_{l=1}^n\alpha_l x_l\frac{1}{n_+}\sum_{j \in I_+}x_j=\alpha^T K x_+=f(.) x_+$$

$$\frac{1}{n_-}\sum_{i \in I_-}f(x_l)=\frac{1}{n_-}\sum_{i \in I_-}\sum_{l=1}^n\alpha_l k(x_l,x_i)= \sum_{l=1}^n\alpha_l x_l\frac{1}{n_-}\sum_{i \in I_-}x_j=\alpha^T K x_-=f(.) x_-$$

$$J(f)=1-(f(.) x_+-f(.) x_-)+ \lambda f(.)^T f(.)$$

\begin{align*}
\nabla_{f(.)} J&=\frac{\partial}{\partial f(.)}[1-(f(.) x_+-f(.) x_-+ \lambda f(.)^T f(.)]=-[x^T_+-x^T_-]+ \lambda \frac{\partial}{\partial\alpha}\langle f(.),f(.)\rangle\\
&=-[x^T_+-x^T_-]+ 2\lambda f(.)\overset{set}{=}0\\
\end{align*}

$$f^\star(x)=(2\lambda)^{-1} [x^T_+-x^T_-]$$

\end{document}


\chapter{M/EEG modelling and statistics}
\label{ch:eeg_stats}
After projection to 2D- or a 3D-space (source reconstruction), the
data is in voxel-space and ready to be analysed. There are several
ways how one can proceed. In this chapter, we will focus on analyzing
epoched time-series data. These can be event-related responses (ERPs),
event-related fields (ERFs) or single trials (M/EEG).

In the following, we will go through the various stages of modelling
using typical examples to illustrate the procedures.

\section{Preliminary remarks}
All analyses can be done using either the graphical user interface
(GUI) or a batch system (i.e. using scripts in the SPM2-fashion,
s.~below). The GUI has the advantage that one doesn't need
matlab-knowledge to analyse data. The batch system has the
advantage that it is a fast and efficient way of entering model and
data. Its disadvantage is that some Matlab-Knowledge is
required. However, with this distribution, we provide some template
scripts to analyse (typical) data in batch mode. We assume that with slight
modifications these scripts can be used for most analyses.

\section{How epoched time-series are analysed in SPM}
After preprocessing the data (i.e.~epoching, filtering, etc...) and
projection to voxel-space, we have discretely sampled versions of
continuous fields \cite{kiebel_spm_eeg1}. These data can be analysed
with a mass-univariate approach using results from Random Field theory
(RFT) to adjust p-values for multiple comparisons \cite{kjw_hbf2}. The
model used at each voxel is a general linear model
\cite{kiebel_spm_eeg2}. Typically one wants to analyse
multiple subjects' data acquired under multiple conditions. Given that
each evoked-response has up to hundreds of time points, this is an
awful lot of data at each voxel. The ideal way to analyse these data
would be to specify a single hierarchical model (1st level: within-subject,
2nd level: over subjects) and estimate its parameters. However, this
is computationally not feasible because of the length of the data
vector at each voxel. Fortunately, such a 2-level model can usually be
split up into two models: The 1st level and the 2nd level model. The
input data to the 2nd model are contrasts of the 1st level model
\cite{kiebel_spm_eeg2}. In all cases considered in this chapter, this
2-stage procedure
gives exactly the same results as the 2-level model. The reason for
this is that we are not really \textit{modelling} the data at the 1st
level, but simply forming weighted sums of the data, over time. For
example, if we are interested in the N170 component, one could average
the data from 150 to 190 milliseconds. This is exactly the approach
used in conventional ERP analysis. This approach is not a model,
because simply taking sums corresponds to using an identity matrix as
design matrix. This procedure leaves no degrees of freedom for error
estimation.\\

In summary, the SPM-approach is to form, at each voxel, weighted sums of
the data, over time, at the 1st level. We refer to these weighted sums
as contrast images. These form the input to the 2nd level, where
one usually tests for differences betwen conditions or between groups
(s.~below). The second level models are usually the same as the ones
one would use for functional magnetic resonance imaging (fMRI). Importantly,
these 2nd level models have enough degrees of freedom to estimate the
error, i.e.~statistics can be computed.\\

The output of such a 2nd level analysis is a
voxel-volume (or map), where each voxel contains one statistical
value. The associated p-value is adjusted for multiple
comparisons \cite{kjw_hbf2}. This adjustment is important, because there are many
other voxels or channels. One (disadvantageous) alternative to
adjustment is to consider only pre-selected channels or averages over
channels. This is why the adjustment is especially important for
high-density measurements, because there are many channels to
select from. We believe that it is generally too subjective
to select channels for analysis a-priori. We see the
GFT-adjustment as a good way of looking at the whole data without any
prior selection. This has been already demonstrated for EEG data (in
another context) by \cite{james_rft}.

\section{1st level}
At the 1st level, we select periods or time points in
peri-stimulus time that we would like to analyse. Critically, this
choice must be made a-priori by you. The alternative would be to not
treat peri-stimulus time as a factor, but as a dimension of a Random
Field approach. This alternative approach is often used in
time-frequency analysis of induced and evoked oscillations, where it
seems sometimes difficult to specifiy areas of interest on the
time-frequency plane a-priori \cite{james_rft}.

In the present approach, time is a factor, and
you have to form weighted-sums over peri-stimulus time to provide
input to the 2nd level. Of course, you don't need to constrain
yourself to a single contrast around a specific peri-stimulus time,
but you can compute as many as you like. For example, to analyse
multiple aspects of an ERP, it is not uncommon to form averages around
several time-points of an ERP. At the 2nd level, these can be either
analysed independently or within one model to make inferences about
interactions between conditions and peri-stimulus time.

In the follwing, we will go through model specification and
computation of contrast images. This guide is not written as a
tutorial (i.e.~detailed instructions for a given data set), but
describes each design option and hopefully provides deeper background
knowledge.

\subsection{The aim}
The aim of the 1st level is to compute contrast images that provide
the input to the 2nd level. We will describe this using the example of
2D-data, i.e.~data that has not been source reconstructed but, for
each peri-stimulus time point, has been projected to a 2D-plane
(s.~chapter \ref{ch:eeg_source}).

\subsection{Start}
Start SPM by the command \textit{'spm eeg'} from the matlab command
line. Press the \textit{EEG/MEG} button. Your first choice is to
either specify the model design or the input data. One always starts
with the design. Currently, there are two design options: (i)
\textit{all options} and (ii) \textit{ERP/ERF}. The latter option is a
shortcut to quickly input an evoked responses study. We will first
describe \textit{all options} and then treat the \textit{ERP/ERF}
option as a special case.

\subsection{All options}
You first have to answer the question whether
this is a 1st level design. This determines whether SPM expects to
model peri-stimulus time as a factor. Also, if one models first-level
data, SPM will ask next for \textbf{one} M/EEG-matfile before the
data was projected to voxel-space. The reason for this is that the voxel-images
lost important information during the conversion. For example, all timing
information were lost. With the nifti-images only, SPM doesn't
know the peri-stimulus time of each data point. However, this
information is critical as soon as you try to specify (later on)
linear weights in terms of peri-stimulus time. So, when you select an
M/EEG file, SPM will read timing information from this file. For an
ERP-study, the M/EEG-file of the average (ERP) is a good choice.

\subsection{How many factors?}
This question starts off the design specification proper. SPM needs to
know the number of factors which you want to model. At the 1st level,
there are typically only factors \textit{peri-stimulus time} and
\textit{condition}. If you like, you can further subdivide the
condition-factor in its components. For instance, if you have a 2x2
factorial design, you may want to specify 3 factors: \textit{factor1},
\textit{factor2} and  \textit{peri-stimulus time}.

\subsection{Factor names and \# of levels for factors}
For each factor, you now input its name, e.g.~condition, and enter the
number of levels. For instance, if you have 2 conditions, you enter
2. For peri-stimulus time, you enter the number of time points in your
evoked responses. Important: You should call the peri-stimulus time
factor  'time'. For the number of levels for this special factor, SPM
defaults to the correct number of peri-stimulus time points. (Note
that it is currently not possible to model only a subset of time points.)

\subsection{Select design component}
You have the choice between \textit{Identity} and
\textit{Constant}. Your selected design components are combined (by
Kronecker tensor product) to form the 1st level design matrix. This
has also been described in \cite{kiebel_spm_eeg2}. For the 1st level,
you simply choose for all factors \textit{identity}. This completes
model specification.

\subsection{Data}
For selecting data, press the \textit{EEG/MEG} button again. After
selecting the \textit{SPM.mat} file, you are asked to select data for
each factor. The order in which you input data  depends on the order
of how you named the individual factors. We recommend that you make
the \textit{peri-stimulus time} factor the last factor. After
projection to voxel-space, the data are stored as 4-dimensional files
with the third dimension $z = 1$. If you want to input all
peri-stimulus time points for a given file, you have to select all
volumes along the 4th dimension. This is done by setting the number
'1' in the SPM-file selector (below the 'Filt' line) to '1:101', where
'101' is the total number of peri-stimulus time points. Of course, you
have to replace '101' by the number of time points of your
data (or by any natural number bigger than that). This choice will make
all time points selectable. Then right-click over the file
names and \textit{Select all}. Press \textit{done} to confirm your
choice. This completes data selection.

\subsection{'Estimation'}
Although there is actually nothing to estimate, clicking the
\textit{Estimation} button will prepare some internal structure for
the results section. We kept this (otherwise redundant) estimation step to
provide for greater similiarity with other analyses using SPM.

\subsection{Results}
After clicking on \textit{Results}, choose the appropriate
\textit{SPM} and the contrast manager will pop up. In contrast to a usual
SPM study, we don't use the contrast manager to compute
statistics, but contrasts only!

Click \textit{Define new contrast...} and enter a name for your
contrast. Then note a (new) button called \textit{components} which is
only visible for M/EEG models. Clicking this button opens the contrast
components manager. This is simply a tool that exploits the knowledge
about the factors which you have specified earlier. Knowing the
factors and their levels makes it easy to split up a (long) contrast
weight vector into a few components. For each contrast weight vector,
each factor contributes one component. By using the Kronecker tensor
product, these components can be combined into the resulting contrast
weight vector. This is not only time-saving, but many people tend to
find this approach more intuitive than the usual approach of figuring
our the contrasts yourself. For instance, if you have specified
two conditions, you might be interested in their difference. Enter a
$[-1 \; \; 1]$ as contrast component. For the \textit{time} factor,
instead of entering one number for each time point, better click on
the \textit{Generate} button. Click on the 'Time' button and specify
a rectangular averaging window by providing the start and end of this
window (in milliseconds). Press \textit{Compute}. You can see now in
the contrast manager window that your contrast weights have been
computed and are displayed above the identity (design)
matrix. You can also specify the contrast weights as usual in the
contrast box, but this would require to enter several hundreds to
thousands of numbers. Press \textit{ok} to proceed and compute the
contrast.

\subsection{Display}
You can display the resulting contrast image by using the
\textit{Display} button.

\subsection{ERP/ERF}
You can shortcut some of the question and especially the data
selection by choosing the \textit{ERP/ERF} option (instead of
\textit{all options} when specifying a design. This options assumes
that you have two factors, \textit{condition} and \textit{time}. There
are less questions during design specification. When selecting data,
you don't need to select all time point, but only the first! SPM will
assume that you want to select all time points of the selected
file. Using this option will otherwise result in the same model
as described above.

\subsection{Multiple subjects}
For each of your subjects, you perform these operations in a separate
1st-level analysis. For each subject, you want to compute the same
contrasts and use them as input to a model, where subjects is the
repetition factor.

\section{2nd level models}
For 2nd level modelling, you can use different ways to specify a
model. There is \textit{Basic models} which was primarily developed for
PET/fMRI but is equally appropriate for EEG/MEG data. These are
suited best when the model is simple (like a 1-sample or 2-sample
t-test). In our experience, most EEG/MEG models fall into this
category of simple models. If models are more complicated, like, e.g.,
two groups with multiple subjects/conditions, we recommend using the
\textit{EEG/MEG} models.

\subsection{All options}
As above, go for \textit{All options}. This time, press 'no' for the
question 'Is this a first-level design'.

\subsection{Factors}
This includes all factors, even repetition factors. For example, at
the 2nd level a 2x2 factorial design has 3 factors: \textit{subject},
\textit{factor1} and \textit{factor2}.

\subsection{Design partitions and design components}
The way this modelling device constructs a design matrix is by using the
Kronecker tensor product on the hierarchy of specified design
components. However, some/many designs can't be constructed in this
way. For example, the design matrix of a paired two sample-test
consists of two merged partitions, each of which is a Kronecker tensor
product of design components. For each partition, the factors and the
levels are the same. The difference is in the choice of the design
components for each factor under each partition. For example, for a
paired two-sample-test, one has 2 factors (subjects and conditions)
and 2 design partitions. For the 1st partition, choose
\textit{Constant} for subjects and \textit{Identity} for
conditions. For the 2nd partition, it's the other way around,
i.e.~\textit{Identity} for subjects and \textit{Constant} for
conditions.

\subsection{Covariance components}
Specification of the covariance components determines the error
model \cite{daniel_hbf2}. For each factor, there are two questions:
(i) Identical variance for
factor $xxx$, and (ii) Independence for factor $xxx$. SPM constructs all the
variance components from your answers. The first question pertains to
the assumption whether each level of this factor has identical
variance. The second questions asks whether the different levels for a
given factor are correlated. Some examples: For a repetition factor
like subjects, you should always answer both questions with yes. For a
group factor, one would assume that the levels of this factor (the
groups) have unequal variance structures, but are uncorrelated (i.e.,
(i) no, (ii) yes). For a condition factor, the choice is up to
you. A very restrained model would follow from using (i) yes (ii)
yes, whereas the most liberal model is given by (i) no (ii) no.

\subsection{Data}
For each combination of factors, SPM asks you for the filenames of the
data. Sometimes, this process can be more convient for you, when you
have specified the factors in a specific order. For example, if you
have two factors \textit{subjects} and \textit{condition}, the order
(i) subjects, (ii) condition will ask for all images for each
subject. This is convienient if you have stored the contrast images
in their individual subject folder. This is the case, if you
have computed 1st level contrasts following the approach described
above. However, if, in an intermediate step, you have saved contrasts
in condition-specific folders, the alternative order ((i) condition, (ii)
subjects) is more appropriate.

\subsection{Estimation and Results}
The estimation follows the usual scheme, i.e.~for a classical
estimation procedure we use exactly the same routine as for PET/fMRI
data (i.e.~maximum-likelihood estimators for the parameters and Restricted
Maximum Likelihood for estimation of the variance parameters).

For specification of contrasts, you have the option to specify
contrasts component-wise. This can be useful for complex designs, when
it's no longer easy to work out the interaction contrasts.

For 2D data the statistical map is displayed instead of the usual
glass brain. You can invoke all the usual functions that are also
available for fMRI/PET data. An additional option is \textit{channels}
which let you visualise to which voxel each channel maps. You can
select this option by right-clicking the button on the statistical
map background. SPM asks you then for one of the original M/EEG-mat
files to read the channel mapping.


\input{Ch3/intro}

\section{Sets}
\input{Ch3/sets1}
\input{Ch3/sets2}
\subsection{Exercises}
\input{Ch3/sets}

\section{Functions and Relations}
\input{Ch3/fr}

\subsection{The Graph of a Relation}
\input{Ch3/rgraph}

\subsection{Equivalence Relations}
\input{Ch3/eqr}


\label{sec:Res}
\section*{Results}

We partitioned variance due to the species effect, i.e. the part of the variance explained by the species of individuals (see~\autoref{fig:aov}). Depending on the trait, the species effect could explain between 27\% up to 75\% of the variability of our data. While species effect can explain over 75\% of the variability in wood density, it only explains less than 30\% of the variability in AGR.

To test how growth was affected by individual traits we made growth mixed-model for each one of them (see~\autoref{tab:seltraits} for traits and~\autoref{tab:growth_mod} for models) as predictors of the AGR. Based on the adapted R-squared for mixed-models~\citep{nakagawa_general_2013}, we selected the best model for each trait (see~\autoref{tab:growth_mod}).

For all traits models with distance measures were better than model with only species average term. But for wood density and SLA, hierarchical distances model performed as well as indvidual's trait models. By adding a distance term to the species average term model, we gained in predictive power, shown by the increase of condition R-squared between models.

For bark thickness the absolute distance model improved the conditional R-squared by 0.025 compared to the model with only species average trait. For wood density hierarchical distance model improved the R-squared by 0.056 (10\% of total $\text{R}^2$) compared to species average model. While for SLA hierarchical distance model only increased $\text{R}^2$ by 0.004. For laminar chlorophyll and toughness the hierarchical distance models gained 0.001 of R-squared compared to the species average term models.

Using the best models (in bold in~\autoref{tab:growth_mod}) that included a distance term we predicted AGR for a range of individual trait values and species average values. To underline the interplay between intra- and inter-specific variabilities we plotted the centered species average trait value vs. the hierarchical distance of indivdual's (see~\autoref{fig:simul}). We obtained AGR "landscapes", that show the pattern of variations of variation as a function of individual and species trait variation. Depending on the trait we had different patterns, for SLA and wood density we had similar patterns: the larger the species average trait value, the lower the AGR, and the higher the individual hierarchical distance the lower the AGR. For example, an increase of $10\text{cm}^2.\text{g}^{-1}$ in SLA species average decreases the growth by $0.035\text{mm}.\text{yr}^{-1}$ while the same increase in hierarchical distance decreases the AGR by $0.02\text{mm}.\text{yr}^{-1}$. For laminar chlorophyll content we observe that increasing species average as well as increasing hierarchical distance lead to increasing AGR. While for leaf toughness, species average and hierarchical AGR gradients are opposed: an increase in species average decreases AGR, and an increase individual hierarchical distance increases AGR. Because the best model for bark thickness was an absolute distance model, we observe the symmetrical patterns of AGR variations for change in hierarchical distance: globally, the higher the hierarchical distance the lower the AGR, while the higher the species average, the lower the growth.
\begin{table*}[!pt]
	\begin{center}
		\begin{tabular}{llcccc}
		  \hline
		 Trait Name & Model Type & Marginal $\text{R}^2$ & Cond. $\text{R}^2$ & AIC & logLikelihood \\
		  \hline
		Bark Thickness & Species Avg. & 0.104 & 0.472 & -2790 & 1407 \\
		            & Hierarchical Distance & 0.106 & 0.471 & -2794 & 1410 \\
		            & \textbf{Absolute Distance} & 0.100 & \textbf{0.497} & -2860 & \textbf{1443} \\
		            & Individual Trait & 0.102 & 0.470 & -2792 & 1408 \\
		  \cline{2-6}
		  Wood Density & Species Avg. & 0.142 & 0.474 & -1838 & 931 \\
		               & \textbf{Hierarchical Distance} & 0.139 & \textbf{0.530} & -1910 & \textbf{968} \\
		               & Absolute Distance & 0.140 & 0.529 & -1906 & 966 \\
		               & \textbf{Individual Trait} & 0.137 & \textbf{0.530} & -1911 & \textbf{968} \\
		  \cline{2-6}
		  SLA & Species Avg. & 0.094 & 0.485 & -3081 & 1553 \\
		      & \textbf{Hierarchical Distance} & 0.095 & \textbf{0.489} & -3092 & \textbf{1559} \\
		      & Absolute Distance & 0.096 & 0.459 & -3000 & 1513 \\
		      & \textbf{Individual Trait} & 0.096 & \textbf{0.490} & -3093 & \textbf{1559} \\
		  \cline{2-6}
		  Chloro. Content & Species Avg. & 0.092 & 0.487 & -3143 & 1583 \\
		                  & \textbf{Hierarchical Distance} & 0.093 & \textbf{0.488} & -3145 & \textbf{1586} \\
		                  & Absolute Distance & 0.092 & 0.486 & -3143 & 1584 \\
		                  & Individual Trait & 0.100 & 0.469 & -3075 & 1550 \\
		  \cline{2-6}
		  Toughness & Species Avg. & 0.088 & 0.478 & -3136 & 1580 \\
		            & \textbf{Hierarchical Distance} & 0.088 & \textbf{0.479} & -3135 & \textbf{1581} \\
		            & Absolute Distance & 0.088 & 0.478 & -3134 & 1580 \\
		            & Individual Trait & 0.088 & 0.479 & -3134 & 1579 \\
		  \cline{2-6}
		  All Traits & Species Avg. & 0.133 & 0.524 & -1739 & 885 \\
		             & \textbf{Hierarchical Distance} & 0.143 & \textbf{0.532} & -1761 & \textbf{901} \\
		             & Absolute Distance & 0.138 & 0.524 & -1743 & 892 \\
		             & Individual Trait & 0.138 & 0.528 & -1766 & 899 \\
		   \hline
		\end{tabular}
		\caption{\textbf{Summary table of tested trait-specific growth models.} We modeled radial growth using linear-mixed model, all models contained plot and species random effects, as well as DBH and $\log\text{DBH}$ terms to take growth curve shape into account~\citep{herault_functional_2011}. Then, for each trait, we added a fixed effect that contained different terms: \textbf{Species Avg.}, only the species average trait value; \textbf{Hierarchical Distance}, the species average plus the difference between individual trait and species average; \textbf{Absolute Distance}, the species average plus the absolute difference between individual trait and species average; \textbf{Individual Trait}, only the trait value of individuals. Models indicated in \textbf{bold} are those with the highest logarithmic likelihood per trait. Not all traits had been measured on each tree, giving a different number of comprised tree for each model, thus the likelihood of models for each trait are not comparable.}
		\label{tab:growth_mod}
	\end{center}
\end{table*}
\begin{figure*}[!tb]
	\centering
	\begin{subfigure}[c]{0.45\textwidth}
		\includegraphics[scale=0.75]{figures/Plots_Map_2015-05-26.pdf}
		\caption{}
		\label{fig:map}
	\end{subfigure}
	\begin{subfigure}[c]{0.5\textwidth}
		\includegraphics[scale=0.7]{figures/Aov_Var_Traits_2015-05-25.pdf}
		\caption{}
		\label{fig:aov}
	\end{subfigure}
	\caption{\textbf{(a) Plots map.} 9 1-ha plots were used, spread in French Guiana, two plots were surveyed both in Nouragues and in Paracou (see~\citealp{baraloto_decoupled_2010}) \textbf{(b) Explained variance by species effect in ANOVAs.} Dot-plot of explained variance in ANOVA by the species effect for traits and AGR. \textbf{Chloro. Content}: Laminar Chlorophyll Content, \textbf{AGR}: Annual Growth Rate (in diameter).}
	\label{fig:gen}
\end{figure*}

\begin{figure*}[!ptb]
	\centering
	\includegraphics{figures/Sel_Traits_Simul_AGR_2015-05-30.pdf}
	\caption{\textbf{Predictions of AGR depending on intra-specific and inter-specific variabilities in traits.} Surface plots of predicted AGR of simulated range of data: X-axis, centered species average trait (species average trait minus mean of all species average trait); Y-axis, individual distance to species average trait. Black lines are equal-AGR lines over the surface, i.e. on those line each point has the same AGR value, each line mark a $5e^{-3}\text{mm}.\text{yr}^{-1}$ break. For details on traits see~\autoref{tab:seltraits}.}
	\label{fig:simul}
\end{figure*}


\section{Evaluation}

In order to evaluate this technique, 1 hour of location data was captured for 4 busses in the Edmonton Public Transit System. This data consists of 120 sensor updates per bus, or 1 every 30 seconds. All results were captured on a machine with 32 GB of RAM, and a Core i5 2700K processor.

Unless otherwise specified, all probability meshes are 256*256*9*9, and are mapped to the Edmonton City Limits, with velocities in the range (-100km/h,100km/h).

\subsection{Optimal Grid Size}
Using a finer spatial grid ($n$ and $m$) allows for smaller changes in position to be represented, and for finer points to be selected as most-likely positions.
However, in order to be useful for real-time refinement, it must be possible to update meshes at least as quickly as new sensor updates come in. This means it must be possible to update all meshes in under 1 hour.

By calculating recoverability between the raw GPS positions and the refined positions for a series of grid sizes, we can determine at what grid size the fidelity no longer contributes a substantial error. Because in this case refinement itself will appear to contribute error, we are looking not for a low absolute error, but a "levelling off" in which increasing size does not further reduce error.
Error is presented here as the mean distance between points at the same timestamp, in meters.

\input{figures/fig-size-err}

Shown in \figref{fig:size:err}, substantial error is contributed by the 64x64 and 128x128 grids, but beyond that there appears to be negligible gain from further size increases. It may still be desirable to use larger grids, as the error continues to reduce, but execution time may prevent it. \figref{fig:size:time} shows the time required to compute all 120 sensor updates for all 4 busses. While sizes up to 256x256 are relatively efficient to compute, it takes nearly the full hour to process the 1024x1024 grid.
Execution times remain approximately constant per-cell, meaning doubling the spatial fidelity in two dimensions roughly quadruples the processing time required. From the combination of the above, 256*256*9*9 was selected as the optimal probability mesh size.

\input{figures/fig-size-time}

\subsection{Route Recoverability}

Because GPS sensors are assumed in this work to be unreliable, and the resulting techniques operate on that assumption, it makes little sense to compare the refined paths to the original sensor data. Instead, we perform multiple runs, randomly permuting GPS positions within specified radii. By comparing the error from raw sensor input to the error in the refined path, we can evaluate the effectiveness of these techniques in accomodating for errors. \figref{fig:gps:err} shows the resulting information.

\input{figures/fig-gps-err}

Clearly visible in the figure, path refinement fails at all error levels to produce a more recoverable path than just using the original sensor readings. However, as GPS error increases, the gap begins to close in relative terms. At 10cm of GPS error, the refined error is 8.4x greater, while at 2m of GPS error, the refined error is only 3x greater. At further inspection, this error occurs for two main reasons:
\begin{enumerate}
    \item \textbf{Error Magnification:} The probability meshes can express maximum likelihood points only at exact grid cells. Thus, a small change in raw position can cause the refined path to move the position an entire cell. Since cells correspond to positions multiple meters apart, this magnifies small changes into large changes. Though most cells are unchanged, a few errors skew the result.

    \item \textbf{Off-the-grid Error:} One bus being tracked actually leaves the City of Edmonton geographic limits for a portion of its route. During this portion, the probability mesh is forced to approximate its location as at the nearest edge.
\end{enumerate}

These issues, along with the reducing gap in recoverability as the error grows more dramatic, seem to indicate that a more precise probability mesh would solve much of the issue in refined recoverability.

%\input{figures/fig-algm-ex}
%\input{figures/fig-code-ex}
%\input{figures/fig-tbl-ex}


%\documentstyle[11pt,epsfig,psfig,times]{article}

%\textheight 9in
%\topmargin 1in
%\footheight 0.5in
%\textwidth 6.8in
%\oddsidemargin -0.2in

%\usepackage{latex8}
%\usepackage{psfig}
%\usepackage{epsfig}
%\usepackage{latexsym}
%\usepackage{amssymb}

%\begin{document}

Let ${\tt preset}(t)$ and ${\tt postset}(t)$ denote the preset and postset of
a transition $t$.  ${\tt preset}(p)$ and ${\tt postset}(p)$ are similarly
defined for a place $p$.

\subsection{Transform 0: Merge Parallel Places}
\label{reduce0}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform0-a,width=27.5mm} \hspace{5mm} &
\psfig{figure=xform0-b,width=22.5mm} \\
(a) \hspace{6mm} & (b)
\end{tabular}
{\caption{\label{xform0}Transform 0. Merge parallel places.}}
\end{center}
\end{figure}

Figure~\ref{xform0}(a) shows two places in parallel.  They can be
merged into a single place as shown in Figure~\ref{xform0}(b) if the following requirements hold:
\begin{itemize}
\item {\tt preset(p1) = preset(p2)}.
\item {\tt postset(p1) = postset(p2)}.
\item {\tt marking(p1) = marking(p2)}
\end{itemize}

%\clearpage

\subsection{Transform 1: Remove a Place in a Self Loop}
\label{reduce}

\begin{figure}[!tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform1-a,width=31.25mm} \hspace{5mm} &
\psfig{figure=xform1-b,width=35mm} \\
(a) \hspace{6mm} & (b)
\end{tabular}
{\caption{\label{xform1}Transform 1. Remove a self loop on a
    transition.}}
\end{center}
\end{figure}

Figure~\ref{xform1}(a) shows a transition $t$ with a self loop, where a
place $p$ is in ${\tt preset}(t)$ and ${\tt postset}(t)$, and is marked.
The transformation removes $p$ and its incoming and outgoing flow relations.
Then, the upper bound delay of the flow relations entering $t$ are
modified such that the upper bound is the maximum of the current max
and that of the flow relation between $p$ and $t$.
The new TPN is shown in Figure~\ref{xform1}(b).  If there is only the
same one place in the preset and postset of $t$, this place is not
removed as shown in Figure~\ref{bad}(c).

%\clearpage

\subsection{Transform 2: (BAD TRANSFORM) Remove a Transition in a Loop}
\label{reduce1}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform2-a,width=27.5mm} \hspace{5mm} &
\psfig{figure=xform2-b,width=22.5mm} \\
(a) \hspace{6mm} & (b)
\end{tabular}
{\caption{\label{xform2}Transform 2. Remove a transition in a loop.}}
\end{center}
\end{figure}

Figure~\ref{xform2}(a) shows a loop formed by a transition $t_3$, where the size
of the preset and postset of $t_3$ is 1. The semantics
of this TPN can be interpreted as transition $t_4$ can fire after the
infinite number of times of firings of transition $t_2$ and $t_3$.
If ${\tt u}(p_1) + {\tt u}(p_2) > 0$, by changing ${\tt u}(p2)$ to $\infty$,
the timing of $t_4$ is preserved.  The new TPN after removing transition
$t_3$ and its incoming and outgoing flow relations is shown in
Figure~\ref{xform2}(b).

The code implemented the above transformation is in the function call
{\em xform\_1} in postproc.cc.

%\clearpage

\subsection{Transform 3: Remove a Transition With a Single Place in the Postset}
\label{reduce2}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform3a-a,width=35mm} \hspace{10mm} &
\psfig{figure=xform3a-b,width=50mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform3a}Transform 3a. Remove the transition with
    the size of postset of 1.}}
\end{center}
\end{figure}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform3b-a,width=37.5mm} \hspace{10mm} &
\psfig{figure=xform3b-b,width=62.5mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform3b}Transform 3b. The transformation in
    Figure~\ref{xform3a} without the restriction that places in preset(t)
    have postset sizes of 1.}}
\end{center}
\end{figure}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform3c-a,width=35mm} \hspace{10mm} &
\psfig{figure=xform3c-b,width=57.5mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform3c}Transform 3c.  The transformation in
    Figure~\ref{xform3a} without the restriction that the place in
    postset(t) have preset size of 1.}}
\end{center}
\end{figure}

Figure~\ref{xform3a}(a) shows a transition $t$ with only one place in its
postset.  Suppose $t$ is removed.  The incoming and outgoing flow relations
of $t$ are also removed.  Then the place in ${\tt postset}(t)$ is
merged with those in ${\tt preset}(t)$.  The delay bounds of the place in
${\tt postset}(t)$ are added to the delay bounds of the places in
${\tt preset}(t)$, as shown in Figure~\ref{xform3a}(b).
The transformation in Figure~\ref{xform3a}  has the restrictions that
for each place $p$ in
${\tt preset}(t)$, the size of ${\tt postset}(p)$ equals 1 and  for the
place $p$ in ${\tt postset}(t)$, the size of ${\tt preset}(p)$ equals
1.  Figures \ref{xform3b} and \ref{xform3c} show the same
transformations without each restriction respectively.

Figure~\ref{bad}(a) and (b) show where these above transformations
cannot be applied.

All places in preset(t) must have same marking (all marked or all
unmarked) and place in postset(t) can be marked.  If either preset(t)
or postset(t) is marked, new combined places are marked.

%\clearpage

\subsection{Transform 4: Remove A Transition With A Single Place in the Preset}
\label{xform-3}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform4a-a,width=40mm} \hspace{10mm} &
\psfig{figure=xform4a-b,width=41.25mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform4a}Transform 4a. Remove a transition with
    preset of size 1.}}
\end{center}
\end{figure}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform4b-a,width=37.5mm} \hspace{10mm} &
\psfig{figure=xform4b-b,width=40mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform4b}Transform 4b. Transformation in Figure~\ref{xform4a}
    without restriction that the places in preset(t) have a preset of
    size 0.}}
\end{center}
\end{figure}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform4c-a,width=40mm} \hspace{10mm} &
\psfig{figure=xform4c-b,width=45mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform4c}Transform 4c. Transformation in Figure~\ref{xform4a}
    without restriction that the places in postset(t) each have a preset of
    size 1.}}
\end{center}
\end{figure}

Figure~\ref{xform4a}(a) shows a transition $t$ with only one place in its
preset.  Suppose $t$ is removed.  The incoming and outgoing flow relations
of $t$ are also removed.  The the place in ${\tt preset}(t)$ is
merged with those in ${\tt postset}(t)$.  The delay bounds of the place in
${\tt preset}(t)$ are added to the delay bounds of the places in
${\tt postset}(t)$, as shown in Figure~\ref{xform4a}(b).
The transformation in Figure~\ref{xform4a} as the restrictions that
for the place $p$ in
${\tt preset}(t)$, the size of ${\tt postset}(p)$ equals 1 and for each
place $p$ in ${\tt postset}(t)$, the size of ${\tt preset}(p)$ equals
1, too.  Figures~\ref{xform4b} and \ref{xform4c} demonstrate the same
transform where these restrictions are loosened.

The above transformations are also
restricted by the conditions shown in Figure~\ref{bad}(a) and (b).

All places in postset(t) must have same marking (all marked or all
unmarked) and place in preset(t) can be marked.  If anything is
marked, new combined places are marked.

%\clearpage

\subsection{Transform 5: Merge Transitions With the same preset and postset}
\label{merge-1}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform5a-a,width=58.75mm} \hspace{10mm} &
\psfig{figure=xform5a-b,width=48.75mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform5a}Transform 5a. Merge transitions with the same
    preset and postset}}
\end{center}
\end{figure}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform5b-a,width=48.75mm} \hspace{10mm} &
\psfig{figure=xform5b-b,width=46.25mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform5b}Transform 5b. Merge transitions where the
    places in their preset and postset have the same preset and
    postset.}}
\end{center}
\end{figure}

Figure~\ref{xform5a}(a) shows two transitions $t_6$ and $t_7$ with
the same preset and postset.  Those two transitions can be merged together
without affecting the behavior on transitions $t_{10}$, $t_{11}$, and
$t_{12}$ as shown in Figure~\ref{xform5a}(b).  Since no places are
being removed, no marking restrictions apply.

%Note: Transform 5b is not turned on by default.  It was found to have
%minimal effect except for in a few examples and was computationaly
%intensive.  Additionally, the correctness of this transformation in
%its current form is in question.

Another case is where the two transitions do not have the same preset
and postset, but the places in their preset and postset have the same
preset and postset as shown in Figure~\ref{xform5b}(a).  The two
transitions can still be merged along with the places in their preset
and postset. This transformation is depicted in Figure~\ref{xform5b}.
In order for the transformation to be applied certain timing
constraints must be satisfied.  Specifically, arcs between the places
in the postset to transitions in the postsets of the postset must have
the same timing bounds.  Furthermore, the place to transition arcs
feeding the transitions to be merged must have the same timing
constraints.

%There is also requirement on this transformation that the places in
%the preset of the merged two transitions must have same postset
%excluding the merging transitions, and the places in the postset of
%the merged two transitions must have same preset excluding the merging
%transitions.  For example, in Figure~\ref{xform5b}(a), the places in
%the preset of $t_3$ and $t_4$ are $p_1$ and $p_2$.  The postset of
%$p_1$ and $p_2$ contains only $t_2$, excluding $t_3$ and $t_4$.  The
%places in the postset of $t_3$ and $t_4$ are $p_3$ and $p_4$.  The
%preset of $p_3$ and $p_4$ contains only $t_5$, excluding $t_3$ and
%$t_4$.

%\clearpage

\subsection{Transform 6: Merge Transitions With the same preset}
\label{merge-2}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform6-a,width=57.5mm} \hspace{5mm} &
\psfig{figure=xform6-b,width=50mm} \\
(a) \hspace{5mm} & (b)
\end{tabular}
{\caption{\label{xform6}Transform 6. Merge transitions with the same
    preset.}}
\end{center}
\end{figure}

Figure~\ref{xform6}(a) shows two transitions $t_1$ and $t_2$ with
the same preset.  It is also required that the places in the postset of
$t_1$ and $t_2$ must have the same preset aside from the two
transitions being merged.  Additionally, there can only be one place
in the postsets of t1 and t2.  Those two
transitions can be merged together without affecting the behavior on
transitions $t_3$, $t_4$, and $t_5$ as shown in Figure~\ref{xform6}(b).

Since the places in the postset of the merging transitions will be
merged, they must have a consistent marking.  The newly merged place
will have the same marking as the places being merged.
%\clearpage

\subsection{Transform 7: Merge Transitions With the same postset}
\label{merge-3}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=xform7-a,width=51.25mm} \hspace{10mm} &
\psfig{figure=xform7-b,width=51.25mm} \\
(a) \hspace{10mm} & (b)
\end{tabular}
{\caption{\label{xform7}Transform 7. Merge transitions with the same
    postset.}}
\end{center}
\end{figure}

Figure~\ref{xform7}(a) shows two transitions $t_1$ and $t_2$ with
the same postset.  It is also required that the places in the preset of
$t_1$ and $t_2$ must have the same postset aside from $t_1$ and $t_2$.
Those two
transitions can be merged together without affecting the behavior on
transitions $t_3$, $t_4$, and $t_5$ as shown in
Figure~\ref{xform7}(b).  Additionally, the transitions being merged
may each only have one place in their presets.  The places in the
preset of the transitions
being merged should have consistent markings.  The newly merged place
in the transformed net should have the same marking as the places that
are merged.

%\clearpage

\subsection{Illegal Transformations}

\begin{figure}[tbh]
\begin{center}
\begin{tabular}{cc}
\psfig{figure=bad-1,width=25mm} \hspace{5mm} &
\psfig{figure=bad-2,width=32.5mm} \\
(a) \hspace{6mm} & (b) \\
\psfig{figure=bad-3,width=20mm} \hspace{5mm} &
\psfig{figure=bad-4,width=12.5mm} \\
(c) \hspace{6mm} & (d)
\end{tabular}
{\caption{\label{bad}Illegal transformations.}}
\end{center}
\end{figure}

Figure~\ref{bad} shows several illegal transformations. In Figure~\ref{bad}(a),
neither $t_1$ nor $t_2$ can be removed using transformations in
section~\ref{reduce1} and \ref{reduce2}.
They may be merged by transformations insection~\ref{merge-1},
\ref{merge-2}, or \ref{merge-3}.  In Figure~\ref{bad}(b), $t_3$ cannot be
removed in that it is impossible to have two places connected without a
transition in between. Figure~\ref{bad}(c) shows a stand-alone self loop which
is not removed for some reason.  I think it can be removed if the wire of
that transition is not used in any levels.  Figure~\ref{bad}(d) shows a
dangling transition $t_2$.  It cannot be removed because safety failure
maybe hidden; otherwise.

%\clearpage

\subsection{To Do and Notes}

\begin{itemize}
\item Transforms 3d and 4d.  Same As 3a and 4a but with both
  restrictions removed.
\end{itemize}

%\end{document}


% !TEX root = ../thesis.tex

\chapter{Theoretical Background}
% \label{cha:eva_theory}
In this chapter, I will present the theory behind evaporative cooling and the simulation of gas dynamics in a rarefied atomic gas.

I will first explain the concept of evaporative cooling in an arbitrary power law potential, including the conditions that need to be reached to achieve efficient evaporation. In the second section, I will describe the general problem of a molecular dynamics simulation and the simulation method used in this work.

\section{Theory of Evaporative Cooling}
% \label{sec:evaporative_cooling_theory}
Evaporative cooling is a method to reach temperatures below the recoil limit as well as high phase space densities, which is a requirement for the onset of Bose-Einstein condensation~\cite{pethick_smith_2008}. The concept of evaporative cooling is illustrated in \cref{fig:evaporative_cooling_sketch}. A gas of atoms with temperature $T_0$ is trapped in potential with depth $U_0$. In equilibrium, the speeds in the gas are distributed according to a Maxwell-Boltzmann distribution. If the potential depth is then lowered to $U'$, a portion of atoms with energies above $U'$ can leave the trap. The remaining atoms then thermalise to a lower temperature $T'$.

\vfill
\begin{figure}[htbp]
    \centering
    \includegraphics[clip, trim=0 4px 0 0]{Evap/EvaporationSketch1}
    % \input{TexContents/Figures/Evap/EvaporationSketch.pgf}
    \caption[Concept of evaporative cooling]{By selectively removing the hottest atoms from a gas, the remaining portion will have a lower temperature than before, after equilibrium is reached.}
    \label{fig:evaporative_cooling_sketch}
\end{figure}
\vfill

% Many textbooks constrain themselves to a harmonic potential for a model of evaporative cooling\insertcite{}. However, as we want to simulate a box potential, I will use a generalised power-law potential to derive basic scaling laws for the evaporation process.

\subsection{The Ideal Gas in a Power-Law-Potential}
We look at an ideal gas in a general power-law-potential
%
\begin{equation*}
    U(\vec{r}) = U_x \left| \frac{x}{x_0} \right|^{\alpha_x} + U_y \left| \frac{y}{y_0} \right|^{\alpha_y} + U_z \left| \frac{z}{z_0} \right|^{\alpha_z}
\end{equation*}
where $\vec{r}^\mathsf{T} = (x,y,z)$ is the spatial coordinate.
%
The density of states in such a potential has been derived in \cite{PhysRevA.35.4354}, but because there is a slight error in their derivation, making it incorrect for any odd $\alpha_x$, $\alpha_y$ or $\alpha_z$, the complete and correct derivation is presented in \cref{sec:appendix_dos}. The result is
%
\begin{equation*} %\label{eq:general_dos}
    D(E) = \left(\frac{2m}{\pi\hbar^2}\right)^\frac{3}{2} \frac{x_0 y_0 z_0}{U_x^{1/\alpha_x} U_y^{1/\alpha_y} U_z^{1/\alpha_z}} \frac{F(\alpha_x,\alpha_y,\alpha_z)}{\Gamma(\xi)} \times E^{\xi - 1}
\end{equation*}
with the energy $E$,
\begin{equation*}
    \xi = \frac{3}{2} + \frac{1}{\alpha_x} + \frac{1}{\alpha_y} + \frac{1}{\alpha_z} \quad \text{and} \quad F(\alpha_x, \alpha_y, \alpha_z) = \prod_{i \in \{x,y,z\}} \Gamma\!\left(1 + \frac{1}{\alpha_i}\right).
\end{equation*}
%
From the density of states, we can calculate the partition function $Z$ as
\begin{align*}
    Z &= \int_0^\infty D(E)\exp{\!\left(-\frac{E}{\kB T}\right)} \diff{E} \nonumber \\
      &= \left(\frac{2m}{\pi\hbar^2}\right)^\frac{3}{2} \frac{x_0 y_0 z_0}{U_x^{1/\alpha_x} U_y^{1/\alpha_y} U_z^{1/\alpha_z}} F(\alpha_x, \alpha_y, \alpha_z) \times (\kB T)^\xi
\end{align*}
and using the relation
\begin{equation}
    U = -N \frac{\partial \ln Z}{\partial \beta} \quad \text{with} \quad \beta = \frac{1}{\kB T} \nonumber
\end{equation}
from \cite{laurendeau_2005}, we can write down the internal energy $U$:
\begin{equation*}
    U = N \xi \kB T.
\end{equation*}
This is consistent with the well known results of $U = 3N\kB T$ for the harmonic oscillator potential and $U = \frac{3}{2} N \kB T$ for the 3D box potential.

By integrating out parts of the phase space distribution function
\begin{equation*}
    f(\vec{r},\vec{p}) = Z^{-1}\exp\!{\left(-\frac{\frac{\vec{p}^2}{2m} + U(\vec{r})}{\kB T}\right)}
\end{equation*}
we can calculate both the spatial density $n(\vec{r})$ and the momentum distribution $n(\vec{p})$.

Integrating out the momenta and multiplying with the atom number $N$ gives us the spatial density
\begin{align}
    n(\vec{r}) &= N\int_{-\infty}^\infty \frac{\multidiff{3}{p}}{(2\pi\hbar)^3} f(\vec{r},\vec{p}) \nonumber \\
    %
    &= 4\pi N Z^{-1} (2\pi\hbar)^{-3} \int_0^\infty \diff{p}\, p^2 \exp\!\left(-\frac{\frac{\vec{p}^2}{2m} + U(\vec{r})}{\kB T}\right) \nonumber \\
    %
    &= N \left(\frac{m\kB T}{2\pi\hbar^2}\right)^{\frac{3}{2}} Z^{-1} \exp\!\left(-\frac{U(\vec{r})}{\kB T}\right) \nonumber \\
    %
    &= \underbrace{\underbrace{\frac{N}{8x_0y_0z_0}}_{=n_\text{Box}}\frac{1}{F(\alpha_x\alpha_y\alpha_z)}\prod_{i\in\{x,y,z\}}\left(\frac{U_i}{\kB T}\right)^{\frac{1}{\alpha_i}}}_{\equiv\, n_0} \exp\!\left(-\frac{U(\vec{r})}{\kB T}\right) \label{eq:spatial_density}
\end{align}
where we have defined the peak density $n_0$. In the case of a 3D box potential where $\alpha_x = \alpha_y = \alpha_z \rightarrow \infty$, the density is constant and given by $n_\text{Box} = \frac{N}{8x_0y_0z_0}$.

For future reference, we also want to calculate the average density $\langle n\rangle$ here. The average of any quantity $Q$ over a given density distribution $n(\vec{r})$ is given by
\[\langle Q\rangle = \frac{\int_{-\infty}^\infty \multidiff{3}{r}\; Q n(\vec{r})}{\int_{-\infty}^\infty \multidiff{3}{r}\; n(\vec{r})} = \frac{1}{N} \int_{-\infty}^\infty \multidiff{3}{r}\; Q n(\vec{r})\]
so for the average density we put
\begin{align}
    \langle n\rangle &= \frac{1}{N} \int_{-\infty}^{\infty} \multidiff{3}{r} \left(n(\vec{r})\right)^2 \nonumber \\
    &= \frac{N}{(8x_0y_0z_0)^2} \frac{1}{\left(F(\alpha_x,\alpha_y,\alpha_z)\right)^2} \left(\prod_{i\in\{x,y,z\}} \left(\frac{U_i}{\kB T}\right)^{\frac{1}{\alpha_i}} \right)^2 \int_{-\infty}^{\infty} \multidiff{3}{r} \exp\!\left(-\frac{2U(\vec{r})}{\kB T}\right) \nonumber \\
    &= \frac{N}{(8x_0y_0z_0)^2} \frac{1}{\left(F(\alpha_x,\alpha_y,\alpha_z)\right)^2} \left(\prod_{i\in\{x,y,z\}} \left(\frac{U_i}{\kB T}\right)^{\frac{1}{\alpha_i}} \right)^2 \nonumber \\
    &\phantom{=} \times 8x_0y_0z_0 \prod_{i\in\{x,y,z\}} \left(\frac{\kB T}{2 U_i}\right)^{\frac{1}{\alpha_i}} F(\alpha_x,\alpha_y,\alpha_z) \nonumber \\
    &= \frac{N}{8x_0y_0z_0} \frac{2^{\frac{3}{2} - \xi}}{F(\alpha_x,\alpha_y,\alpha_z)} \prod_{i\in\{x,y,z\}} \left(\frac{U_i}{\kB T}\right)^{\frac{1}{\alpha_i}} \nonumber \\
    &= \frac{1}{2^{\xi - \frac{3}{2}}} n_0. \label{eq:mean_density}
\end{align}
For example, in the harmonic oscillator potential, this is $\langle n\rangle = \frac{n_0}{\sqrt{8}}$.

Similarly, if we integrate out the spatial coordinates, we get the momentum distribution
\begin{align}
    n(\vec{p}) &= Z^{-1} \left[\int_{-\infty}^\infty \frac{\multidiff{3}{r}}{(2\pi\hbar)^3} \exp\!\left(-\frac{U(\vec{r})}{\kB T}\right) \right]\exp\!\left(-\frac{\vec{p}^2}{2m\kB T}\right)\nonumber \\
    %
    &= \frac{8x_0y_0z_0}{Z(2\pi\hbar)^3} \prod_{i\in\{x,y,z\}} \left(\frac{\kB T}{U_i}\right)^{\frac{1}{\alpha_i}} F(\alpha_x,\alpha_y,\alpha_z) \exp\!\left(-\frac{\vec{p}^2}{2m\kB T}\right) \nonumber \\
    %
    &= \frac{1}{(2\pi m \kB T)^\frac{3}{2}} \exp\!\left(-\frac{\vec{p}^2}{2m\kB T}\right), \nonumber
\end{align}
recovering the Maxwell-Boltzmann distribution.
From this, the mean speed $\langle c\rangle$ follows as
\begin{equation}
    \langle c\rangle = \frac{\langle |\vec{p}| \rangle}{m} = \frac{1}{m}\int \multidiff{3}{p} |\vec{p}|n(\vec{p}) = \sqrt{\frac{8\kB T}{\pi m}}.
\end{equation}

\subsection{Model for Evaporative Cooling}
Following the simple model explained above, we describe evaporative cooling as a sequence of infinitesimal steps in which a number \diff{N} of atoms is removed from the cloud by reducing the trap depth to a value $\eta \kB T$. Evaporative cooling is described for example in \cite{KETTERLE1996181}. Assuming $\eta \gg 1$, the loss in internal energy can be approximated as
\begin{equation}
    \diff{U} \simeq \eta \kB T \diff{N}
\end{equation}
which reduces the temperature after thermalisation by \diff{T} and the internal energy to the final value
\begin{equation}
    U_\text{f} = \xi (N - \diff{N}) \kB (T - \diff{T}).
\end{equation}
By subtracting the change in internal energy from the initial value $U_\text{i}$, we can get a relation between the change in atom number and the change in temperature:
\begin{align}
    U_\text{i} - \diff{U} &= U_\text{f} \nonumber \\
    \xi N \kB T - \eta \kB T \diff{N} &= \xi (N - \diff{N}) \kB (T - \diff{T}) \nonumber \\
    &\simeq \xi (TN - N\diff{T} - T\diff{N})\kB \nonumber
\end{align}
omitting second order terms in \diff{T} and \diff{N}.
This can be reduced to
\begin{equation*}
    \frac{\frac{\diff{T}}{T}}{\frac{\diff{N}}{N}} = \frac{\diff{\ln T}}{\diff{\ln N}} = \frac{\eta}{\xi} -1 \equiv \nu
\end{equation*}
where we define the parameter $\nu$. Trivially, for certain starting conditions $N_0$ and $T_0$ we get
\begin{equation*}
    \frac{T}{T_0} = \left(\frac{N}{N_0}\right) ^\nu.
\end{equation*}
Because the goal of evaporative cooling is to reach quantum degeneracy, cooling alone is not enough. It is also necessary to increase the phase space density \PSD which is given by
\begin{equation*}
    \PSD = n_0(N,T)\cdot \left(\lambdadB(T)\right)^3
\end{equation*}
with the de Broglie wavelength
\begin{equation*}
    \lambdadB = \sqrt{\frac{2\pi\hbar^2}{m\kB T}}.
\end{equation*}
Plugging in our result for $n_0$ from \cref{eq:spatial_density} and the scaling of temperature with atom number, we find
\begin{equation*}
    \PSD \propto NT^{-\left(\xi - \frac{3}{2}\right)} \cdot T^{-\frac{3}{2}} \propto N^{1 - \xi\nu} = N^{1 + \xi - \eta}.
\end{equation*}

The decrease in temperature is achieved by thermalisation, which relies on elastic collisions. In an ideal gas, the elastic collision rate \Rcoll is given by \cite[8]{bird1994}
\begin{equation*}
   \Rcoll = n \meanProb
\end{equation*}
where $n$ is the density and \meanProb the ensemble average of the product of cross section and relative velocity. As we are dealing with low temperatures, only s-wave scattering needs to be considered \cite{joachain}. For this simple model, we also set the cross section to be constant. Furthermore, the mean relative speed is proportional to the mean speed. Inserting our result for the mean density from above, we find
% \begin{align}
%     \langle \Rcoll\rangle &= \langle n\rangle\cdot \meanProb = \langle n\rangle \cdot \sigma \cdot \langle\crel\rangle \nonumber \\
%     &\propto NT^{-\left(\xi - \frac{3}{2}\right)} \cdot \text{const.} \cdot T^{\frac{1}{2}} \nonumber = NT^{1-\xi} \nonumber \\
%     &\propto N^{\eta \left(\frac{2}{\xi} - 1\right) + \xi - 1}. \nonumber
% \end{align}
\begin{equation*}
    \langle \Rcoll\rangle = \langle n\rangle \cdot \sigma \cdot \langle\crel\rangle \propto N^{\eta \left(\frac{2}{\xi} - 1\right) + \xi - 1}.
\end{equation*}

\subsubsection*{Conditions for Efficient Evaporation}
Necessary conditions to achieve evaporation are that the temperature decreases and the phase space density increases with decreasing atom number. The collision rate should at least stay constant, if it increases with atom number, \emph{runaway evaporation} occurs. As we know the scaling behaviour of these quantities with $N$, we can give the requirements for $\eta$ that have to be fulfilled to achieve each of these conditions. These are calculated in \cref{tab:eva_scalings} for a linear trap (e.g.\ quadrupole trap, $\xi = 4.5$), a harmonic trap (e.g.\ Ioffe-Pritchard trap, $\xi = 3$) and a box trap ($\xi = 1.5$).
\begin{table}[htbp]
    \centering
    \caption{Scaling laws and requirements for the parameter $\eta$ to achieve (runaway) evaporation}
    \begin{tabular}{lcccl}
        \toprule
        Quantity & Scaling $N^{\{\ \}}$ & \multicolumn{3}{c}{Requirement for $\eta$} \\
        \cmidrule{3-5}
        & & \multicolumn{1}{c}{Linear} & \multicolumn{1}{c}{Harmonic} & \multicolumn{1}{c}{Box} \\
        \midrule
        Temperature $T$ & $-1 + \frac{\eta}{\xi}$ & $\eta > \num{4.5}$ & $\eta > \num{3}$ & $\eta > \num{1.5}$ \\
        Phase space density $\PSD$ & $1 + \xi - \eta$ & $\eta > \num{5.5}$ & $\eta > \num{4}$ & $\eta > \num{2.5}$ \\
        Collision rate \Rcoll & $\xi - 1 + \eta\left(\frac{2}{\xi} - 1\right)$ & $\eta > \num{6.3}$ & $\eta > \num{6}$ & $\eta < \num{-1.5}$ \\
        \bottomrule
    \end{tabular}
    \label{tab:eva_scalings}
\end{table}
As we can see, efficient evaporation in a stationary box potential cannot be achieved. This result is very intuitive as the local density is independent of the position in this case, which means it can only decrease with a decrease in atom number. Therefore, thermalisation times increase exponentially.
This adverse interplay between density and the thermalisation rate also plays a role for other trapping configurations and several papers have been published about methods to circumvent this problem in optical dipole traps (dimple trap \cite{Jacob_2011}, tilted trap \cite{PhysRevA.78.011604, PhysRevA.79.061406}, crossed beam trap \cite{ARNOLD20113288}, zoom lenses \cite{PhysRevA.71.011602}).

% Our concept, as explained in \cref{char:eva_motivation} is a very similar ansatz, but for a box potential instead.
% It should be immediately obvious that evaporative cooling in a box potential is not something that could be meaningfully pursued. However, all the previous considerations assumed that the parameters $U_{\{x,y,z\}}$ and $x_0,y_0,z_0$ stay constant during the evaporation trajectory. If the volume of a hypothetical box potential could shrink while evaporation is performed, the adverse effect of the decreasing density could be conquered. Similar\insertcite{}, concepts have been developed and are frequently implemented with optical evaporation, where two traps with different confinement overlap or where zoom lenses are used to compress the atomic cloud during the cooling stage.

\subsection{Inelastic Atom Loss}
\label{sec:inelastic_losses}
Up until now we only considered elastic collisions between atoms from the cold cloud, because these are the driving mechanism behind evaporative cooling. Elastic collisions with the background gas as well as inelastic collisions between the atoms however are detrimental to the cooling effect. Atoms removed through these are not exclusively from the high-temperature end of the Maxwell-Boltzmann distribution. Since inelastic losses occur through density dependent effects, atoms are more likely to be lost from high-density regions. In non-uniform potentials, this is where cold atoms are usually accumulated.

\subsubsection*{Background Collisions}
Collisions with the background gas in the vacuum chamber lead to atom loss because the background atoms (being in equilibrium with the walls of the chamber) are usually at room temperature (\SI{300}{K}), which means that a collision often imparts more kinetic energy onto the trapped atom than the trap depth. The collision rate with the background gas atoms is calculated via
\begin{equation*}
    \Gamma_\text{BG} = \sum_i n_i \langle \sigma v \rangle_{X,i}
\end{equation*}
where the indices $i$ represent the different atomic species in the background gas and $X$ is the trapped atomic species. The densities $n_i$ are related to the partial pressures $P_i$ by
\begin{equation}
    n_i = \frac{P_i}{\kB T}. \nonumber
\end{equation}
For ultracold atoms, the background loss rate is simply the trap lifetime at low density~\cite{PhysRevLett.91.123201}.

\subsubsection*{Two-Body Collisions -- Spin Relaxation}
Collisions of two trapped atoms can only be inelastic if the spin projections of the involved atoms change during the collisions.
This process is called spin relaxation and in magnetic traps, one of the departing states can be untrapped. However, these kinds of processes can usually be suppressed by preparing the atomic cloud in a stretched spin state \cite{PhysRevLett.99.223201}.

\subsubsection*{Three-Body Collisions -- Molecule Formation}
The final process we include is the three-body collision, where two atoms form a bound state and transfer the binding energy and momentum difference to a third atom \cite{PhysRevLett.91.123201}. Usually, this leads to all three atoms being lost from the trap.

The loss rate for three-body recombination depends roughly on the square of the local density as three atoms need to be present. This also means that it can often be neglected at low densities but becomes dominant at very high densities, limiting the maximum achievable density \cite{PhysRevA.85.053647}.

\subsubsection*{Loss Rate Equation}
If the rates for background ($K_1$), 2-body ($K_2$) and 3-body ($K_3$) losses are known, the local\footnote{If we wanted to describe losses for the whole sample, we would need to take the averages $\langle n\rangle$ and $\langle n^2 \rangle$ instead.} loss rate can be expressed as
\begin{align} \label{eq:lossrate}
    \frac{1}{N}\frac{\diff{N}}{\diff{t}} = - K_1 - K_2 n - K_3 n^2.
\end{align}
where $\frac{\diff{N}}{N}$ represents the probability of an individual atom to be lost in an infinitesimal timestep $\diff{t}$.

% \section{Scattering Theory}
% \todo[inline]{derive the cross section formula used later and the mean collision rate}

\section{Numerical Simulation of Gas Dynamics}
% \label{sec:dsmc_theory}

Simulating evaporative cooling is, in a bigger picture, the same as simulating any gas flow. A large number $N$ of particles, atoms or molecules, move according to a set of equations, can collide with each other and may or may not have interactions with a confining potential. A parameter that decides which model is best used to describe a gas flow is the Knudsen number \Kn
\begin{equation*}
    \Kn = \frac{\lambda}{L}
\end{equation*}
where $\lambda$ is the mean free path and $L$ is the length scale of the surrounding environment, in our case the side length of the volume in which the atoms are confined. Based only on this ratio, gas flows can be separated roughly into four different regimes \cite{schaaf:1958}:
\begin{itemize}
    \item $\Kn < \num{.01}$: Continuum regime
    \item $\num{.01} < \Kn < \num{.1}$: Slip regime
    \item $\num{.1} < \Kn < \num{10}$: Transition regime
    \item $\Kn > 10$: Free molecular flow
\end{itemize}

Ultracold atomic gases, as they are present during evaporative cooling, usually exhibit a high Knudsen number. For example, a gas of $^{87}$Rb (scattering length $a\sim 100\, a_0$ \cite{PhysRevA.87.053614}) atoms confined in a box with a side length of \SI{2}{mm} with a density of \SI{e11}{\per\centi\meter\cubed} would have a Knudsen number of $\Kn = \num{7.1}$. For this calculation, a constant cross section $\sigma = 8\pi a^2$ was assumed, which is justified at low temperatures. The more correct s-wave scattering cross section for identical bosons is given by \cite{dalibardCollision}
\begin{equation}  \label{eq:crosssection}
    \sigma(k) = \frac{8\pi a^2}{1 + (ak)^2}
\end{equation}
where $k=\frac{m\crel}{2\hbar}$ is the absolute value of the collision wave vector. This cross section will be used in the full simulation. For low collision energies, it approaches the constant value $8\pi a^2$.

We can therefore safely assume a rarefied or highly rarefied gas flow that fulfills the conditions of the transition or even free molecular regimes. The simulation method most appropriate for these regimes is the Direct Simulation Monte Carlo (DSMC) method originally developed by G.~A.~Bird \cite{bird1994}.
% In a very simple, but near complete model, interparticle dynamics are described as forces acting between the particles. This gives the complete force $\vec{F}$ acting on one particle denoted with the index $i$:
% %
% \begin{equation}
%     \vec{F}_i = -\nabla U(\vec{r}_i) + \sum_{j = 0,\, i\neq j}^N \vec{F}_{ij}
% \end{equation}
% %
% Here, $U$ is the external potential and $\vec{F}_{ij}$ is the interparticle force between particles $i$ and $j$. Naively, one could program a simulation that would calculate each of these forces at discretised timesteps and integrate the system with any differential equation solver. It is however immediately obvious why this is not feasible, at least as soon the system is a many-particle system. With ultracold gases, atom numbers $\sim$\num{E8} are not uncommon\insertcite{}. This means, there are $\sim$\num{E16} calculations necessary to get all interparticle forces, and this has to be repeated in each step. Current computer hardware is simply not powerful enough to handle extensive calculations like this. However, evaporative cooling is an example of a rarefied gas flow. The classical mean free path is given by
% \begin{equation}
%     \lambda = \frac{1}{n\sigma}
% \end{equation}
% and from, the Knudsen number $\text{Kn}$ is derived as
% \begin{equation}
%     \text{Kn} = \frac{\lambda}{L}
% \end{equation}
% where $L$ is the characteristic length scale of the medium. For an ultracold gas at typical conditions during evaporative cooling, i.e.\ a temperature of \SI{1}{\micro\kelvin}, a density of \SI{e11}{\per\cubic\centi\metre}, and a scattering length on the order of $200a_0$ that has a diameter of \SI{1}{mm}, the Knudsen number would be
% \[\text{Kn} > 1 \]
% which puts us into the transition regime or almost into the regime of free molecular flow. When a gas is rarefied like this, numerical methods can be used to limit the computational expense necessary. Specifically, we will use the DSMC (Direct Simulation Monte Carlo) method, which is valid for Knudsen numbers $\text{Kn} > \num{0.1}$.\insertcite{MULTIPLES}

\section{The DSMC Method}
Simulating the flow of a gas with upwards of \num{e8} particles is a computational expensive task. Over a certain time $\Dt$, the probability of two particles colliding with each other is proportional to the cross section and the relative velocity. This can be imagined as the cylindrical volume that a circle with an area equal to the cross section traces out while moving with the relative velocity \cite[7]{bird1994}. As the number of possible particle pairs scales quadratically with the number of particles, calculating the probabilities for all pairs becomes a virtually impossible task.

In DSMC, several measures are taken that reduce the computational expense significantly, making it possible to run a simulation on a home desktop or a laptop.

Time is discretised into small timesteps that are much smaller than the mean collision time. The simulation volume is divided into cells, each of which is treated separately for the collision handling. The number of particles in the simulation is far lower than the number of simulated real particles. For the sake of better readability and because our simulation only deals with monatomic gases, a particle in the real world will be described as an \emph{atom} from now on, while particles present in the simulation will be described as \emph{particles}. A particle in the simulation represents a certain number of atoms in the real world. This number is called the statistic weight $W$ and plays an important role in the collision handling. Over one timestep, the particle movement is uncoupled from the collisions. The process of a DSMC simulation is sketched in \cref{fig:dsmc_flowchart}. The following subsections will go into more detail on each of the steps.
\vfill
\begin{figure}[htbp]
    \centering
    \input{TexContents/Figures/Evap/DSMCFlowchart/DSMCFlowchart.tikz}
    \caption[The process of a DSMC simulation]{The steps outlined in the flowchart are further described in the following sections of this and the following chapter.}
    \label{fig:dsmc_flowchart}
\end{figure}
\vfill

\subsection{Particle Motion}
The motion of particles of mass $m$ in a potential $U(\vec{r})$ is a well known problem with equally well known solutions. If every particle is treated separately, the problem reduces to the single-particle equation of motion
\begin{equation*}
    \ddot{\vec{r}} = -\frac{1}{m}\nabla U(\vec{r})
\end{equation*}
with known starting conditions for the velocity $\dot{\vec{r}}$ and position $\vec{r}$. In many cases, a very simple method like leapfrog etc.\ is sufficient. However, with a surrounding potential that is highly dependent on the position of the particles, more advanced methods are required to preserve energy as well as possible.

\subsection{Boundary Conditions}
In evaporative cooling, particle loss is an essential step. After all particles have been moved, it is checked if they have moved outside a certain boundary. If they have, they get removed from the simulation.

Because the total number of atoms usually decreases by a few orders of magnitude during evaporation, after some time a large number of particles would be lost from the simulation. However, the number of particles in a collisional cell should stay above a certain threshold for accurate collision numbers \cite{SUN20111}. To circumvent this problem, we impose a threshold on the number of simulated particles $N$. Once this threshold is reached, all particles still present are copied and the statistic weight is halved. Of course, this means that we have to start with a certain power of 2 as the statistic weight. To copy the particles, the $x$ and $y$ values of their position and velocity are mirrored across the origin and the $z$ value is kept the same. This imposes the restriction on the surrounding potential that it has to be conforming to this symmetry. The $z$ values are explicitly not mirrored to retain the possibility of having gravity as a potential in the simulation.

\subsection{Collisions}
\label{sec:eva_theory_collisions}
In each collision step, a number of particle pairs is calculated in every cell, given by
\begin{equation} \label{eq:ntc_npairs}
    N_\text{P} = \frac{1}{\VCell} \NCell \overline{\NCell} \maxProb \Dt
\end{equation}
where $\VCell$ is the volume of one cell, $\NCell$ is the instantaneous number of particles in this cell, $\overline{\NCell}$ is the corresponding time averaged value and \maxProb is the maximum value of the product of cross section and relative velocity over all particle pairs.
Then, $N_\text{P}$ particle pairs are chosen randomly from the cell and a collision between them is performed if
\begin{equation*} %\label{eq:ntc_fraction}
    \frac{\sigma \crel}{\maxProb} > R
\end{equation*}
where $R$ is a random number between 0 and 1.
If \maxProb was to be calculated in every step, this would still be very inefficient as it would again require iterating over all possible particle pairs. However, because the number of pairs is proportional to \maxProb and the probability is antiproportional to it, it suffices to calculate (or even guess) \maxProb only once at the beginning. If a higher value is encountered subsequently, it can be updated. Even more, the true value of the maximum for $\sigma \crel$ is only a lower bound for the \maxProb parameter. In our simulation, we will prefer better statistics by artificially increasing the number of pairs chosen through a higher than necessary value of \maxProb.

\subsubsection*{Transient Adaptive Sub-Cell Technique}
To enforce near neighbor collisions, every cell is even further subdivided into sub-cells during every step. This technique has been described in \cite{SU20101136}. When a particle pair is chosen for a collision attempt, the first particle is chosen from the whole cell. If the sub-cell with this particle contains other particles, the collision partner is chosen from the same sub-cell. Otherwise, sub-cells are gradually searched outwards to select a viable collision partner. We also perform collision tracking meaning that a particle $j$ is not viable for a collision with particle $i$ if $i$ and $j$ have been each other's last collision partner.

This technique is called transient, because the sub-cell division happens locally in each step and adaptive, because the number of sub-cells is chosen such that each sub-cell contains a certain number of particles on average. In practice, we aim for maximum 2 particles per sub-cell. This means that the total number of sub-cells in $x$, $y$ and $z$ direction is given by
\begin{equation} \label{eq:tas_ncells}
    N_i = \lfloor\sqrt[3]{\NCell / 2}\rfloor + 1.
\end{equation}
In almost all cases, this means there are fewer than 2 particles in each sub-cell.

\subsubsection*{Calculation of the Post-Collision Velocities}
It has been mentioned before that only s-wave scattering is considered. Therefore, the collisions are isotropic and the post-collision velocities can be calculated trivially. The absolute value of the relative velocity of the two particles does not change, only its direction.

The centre-of-mass $\vec{c}_\text{COM}$ and relative velocity $\vec{c}_\text{r}$ before the collision are given by
\begin{equation*}
    \vec{c}_\text{COM} = \frac{1}{2}(\vec{c}_1 + \vec{c}_2), \quad \vec{c}_\text{r} = \vec{c}_1 - \vec{c}_2.
\end{equation*}
We calculate a unit vector of random direction as
\begin{equation*}
    \hat{\vec{R}} = \begin{pmatrix}
        \cos(\theta) \\ \sin(\theta) \cos(\phi) \\ \sin(\theta) \sin(\phi)
    \end{pmatrix}
\end{equation*}
where $\phi$ is randomly chosen between $0$ and $2\pi$ and $\cos(\theta)$ is randomly chosen between $-1$ and $1$.
Then the post collision velocities $\vec{c}_1'$ and $\vec{c}_2'$ are simply
\begin{equation*}
    \vec{c}_1' = \vec{c}_\text{COM} + \frac{1}{2} |\vec{c}_\text{r}| \cdot \hat{\vec{R}}, \quad \vec{c}_2' = \vec{c}_\text{COM} - \frac{1}{2} |\vec{c}_\text{r}| \cdot \hat{\vec{R}}.
\end{equation*}

% One possible approach in order to reduce the computational complexity of the task at hand is to decouple the particle motion from the collisions over a small timestep, i.e.\ calculating and applying the motion first and then handling the collisions in a separate step. To get accurate collision numbers, the domain of the simulaion is separated into smaller cells such that in each cell, a constant density can be assumed. The size of the cells also needs to be smaller than the local mean free path\insertcite{}. With a sufficiently small timestep only near neighbor collisions, i.e.\ collisions inside the same cell, are possible. Then, the probability of a collision between two particles is given by
% %
% \begin{equation}
%     P = \sigma \crel \Dt / \Vcell.
% \end{equation}
% %
% Here, $\sigma$ is the collisional cross-section, \crel the relative speed of the particles and \Vcell the volume of the cell. One could then compute the expected number of collisions in one timestep from the collision rate, choose particle pairs at random and evaluate collisions between them based on an acceptance-rejection method. This involves choosing a random number and accepting a collision if the probability $P$ exceeds this random number and rejecting it if it does not. This approach brings the problem that $P$ is usually a very small quantity, meaning a very large number of pairs would have to be tested for collisions in each step to reach the expected collision numbers.

% G.~A.~Bird first proposed and developed many of the algorithms that this work is based on\insertcite{}. In his \enquote{No-Time-Counter} (NTC) method, the problem mentioned above is solved the following way: A number of particle pairs is selected from each cell and the collisions are then sampled among them. Specifically,
% %
% \begin{equation} \label{eq:ntc_npairs_incomplete}
%     \frac{1}{2\Vcell} N (N - 1) \maxProb \Dt
% \end{equation}
% %
% pairs are chosen. Here, $N$ is the number of particles in the cell and \maxProb represents the maximum value of the product of cross-section and relative speed over all particle pairs in the cell. A collision of a pair $(ij)$ is accepted if
% %
% \begin{equation} \label{eq:ntc_fraction}
%     \frac{(\sigma\crel)_{ij}}{\maxProb} > R
% \end{equation}
% %
% with a random number $0 \leq R \leq 1$. Now the value of the deciding fraction in \cref{eq:ntc_fraction} is much closer to 1 than if the probability $P$ had been calculated for each pair separately. This means that a lower number of pairs tested get's rejected, reducing the time necessary to complete one collision time step in one cell. The value of \maxProb also does not have to be calculated in each step which would take the benefits away again. Instead, it is calculated once in the beginning of the simulation and then subsequently updated if a larger value is encountered.

% Up until now we are still dealing with an enormous amount of calculations if both the motion and collision processing have to be done for $\sim$\num{1e8} particles. Arguably the most important step to achieving lower computation times is to let each particle in the simulation be representative of a number of actual physical particles. The simulated particles are called \emph{macro}-particles from now on to avoid confusion. The number of particles represented by one macro-particle is called the statistic weight $W$. In simulations where the average number of particles is approximately constant over the simulated duration, this weight can be chosen almost arbitrarily\footnote{of cource, lower values for $W$ result in better statistic accuracy.}. However, in evaporative cooling, particle loss is inherent to the process. This means that we have to choose the weight as a power of $2$. If the number of macro-particles drops below a threshold during the simulation, the weight is halved and each macro-particle is duplicated. During this duplication procedure, the velocity and position are mirrored along at least one axis to make it impossible for the duplicated particles to collide with the \enquote{old} ones immediately after the duplication process. [\textbf{NOTE:} This requires the external potential to have spatial symmetry in at least one direction, otherwise potential energy would not be conserved.]
% With this technique, the behaviour of \num{E8} particles can be simulated using only e.g.\ $\sim$\num{12000} macro-particles, each with a statistic weight of $W=2^{13} = \num{8192}$.
% With the inclusion of the statistic weight, equation \cref{eq:ntc_npairs_incomplete} becomes
% %
% \begin{equation} \label{eq:ntc_npairs}
%     N_\text{pairs} = \frac{1}{2\Vcell} W N (N-1) \maxProb \Dt.
% \end{equation}
% %

% \paragraph{Transient Adaptive Sub-Cell Technique} ~\\
% Because the macroscopic parameters of an atomic cloud change rapidly during evaporative cooling, a fixed number of cells might not always guarantee the conditions that the cell size should be about $1/2$ - $1/3$ the local mean free path. However, the accuracy of the simulation can be maintained by introducing sub-cells \insertcite{}. In each step, every cell gets subdivided into sub-cells, such that each sub-cell contains a previously specified number of macro-particles. During the collision procedure, first collision partner is chosen from the whole cell. If the sub-cell of this contains other macro-particles, the second collision partner is chosen from the same sub-cell. Otherwise, the adjacent sub-cells are searched for a collision partner.

% The technique is called \enquote{transient} because the sub-cells are created in every step and \enquote{adaptive} because the number of sub-cells can change between steps, depending on the number of macro-particles present.

% \subsection{Collisions}
% \label{sec:eva_theory_collisions}
% In ultracold gases, only s-wave collisions are possible due to the very low temperatures. This simplifies the calculation of the velocities in a particle pair after a collision significantly, as a hard-sphere model can be applied. This means that the magnitude of the relative velocity between the particles does not change during the collision, only its direction is reassigned at random. In a gas with identical atoms, the centre-of-mass velocity is simply given by
% %
% \begin{equation}
%     \cCOM = \frac{1}{2}(\vec{c}_1 + \vec{c}_2)
% \end{equation}
% %
% and the relative velocity is
% %
% \begin{equation}
%     \vec{c}_\text{rel} = \vec{c}_1 - \vec{c}_2.
% \end{equation}
% %
% The velocities after the collision are then calculated as
% %
% \begin{align}
%     \vec{c}_1^* &= \cCOM + \frac12 |\vec{c}_\text{rel}|\cdot\vec{\hat{R}} \\
%     \vec{c}_2^* &= \cCOM - \frac12 |\vec{c}_\text{rel}|\cdot\vec{\hat{R}}
% \end{align}
% %
% where $\vec{\hat{R}}$ is a unit vector of random direction.

% \iffalse
% \subsection{Summary}
% A number of particles is represented as a smaller number of macro-particles, each with a the number of particles per macro-particle as a statistic weight. The domain is divided into smaller cells for near neighbour collisions.
% In one timestep, the following steps are executed:
% \begin{enumerate}
%     \item move all particles according to the external forces.
%     \item apply boundary conditions (e.g.\ eject particles that have moved outside of the domain).
%     \item perform collisions in each cell seperately according to the scheme described above.
% \end{enumerate}
% \fi