% !TEX program = xelatex

%This document had would had been used on Ways to Singularity, which is a website that supports MathJax. So some html elements may occur in this document. DELETE THIS when publishing. (If you are not very clear on the grammar I used here, read the academic publication called Time Traveller's Handbook of 1001 Tense Formations by Dr Dan Streetmentioner, which had would had been publish in year 220010 of Gregorian Calendar.)

\documentclass[12pt,a4paper]{book}

%%%%% Page settings  %%%%%
\addtolength{\textheight}{2.0cm}
\addtolength{\voffset}{-2cm}
\addtolength{\hoffset}{-1.0cm}
\addtolength{\textwidth}{2.0cm}

%\allowdisplaybreaks

%%%%%% Math %%%%%%%
\usepackage{amsthm,amsfonts,amssymb,bm}
\usepackage{mathrsfs}
\usepackage[fleqn]{amsmath}
\usepackage{subeqnarray}
\usepackage{tabulary}

%%%%% MISC  %%%%%%

%\usepackage{color}
\usepackage[usenames,dvipsnames]{color}
\usepackage{url}
\usepackage{ulem}
\usepackage{indentfirst}   % Indent first line of a paragraph
%\usepackage{textcomp}

\usepackage{enumerate}

%%%%%%   Here is the configuration for chinese. setmainfont is the default font of the text.
\usepackage[cm-default]{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}

%%%%%%% Figure, Diagram, Caption settings  %%%%%
%\usepackage{tikz}
%\usetikzlibrary{mindmap,trees}

\usepackage{graphicx}
%\usepackage{graphics}
%\usepackage[hang,small,bf]{caption}
%\setlength{\captionmargin}{50pt}

%\graphicspath{{Figures/}

%includeonly{}

\begin{document}
\title{TITLE}
\author{{\bf MA} Lei  \\
@ Interplanetary Immigration Agency \\
{\small\em \copyright \ Draft date \today}}
\date{}
%\begin{document}
%\maketitle

%%%%%% Redefine some math commands and environments. %%%%

\newcommand{\dd}{\mathrm d}

%\newcommand{\HH}{\mathcal H}

%\newcommand{\CN}{{\it Cosmologia Notebook}}

%\newenvironment{eqnset}
%{\begin{equation}\left \bracevert \begin{array}{l}}
%{\end{array} \right. \end{equation}}

%\newenvironment{eqn}
%{\begin{equation}\left \bracevert \begin{array}{l}}
%{\end{array} \right. \end{equation}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%    Let's Start Typing     %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%  CHAHCA   %%%%%%%%%%
%\setcounter{chapter}{10}
%\chapter{Tensors and Local Symmetries}

\pagestyle{empty}

\renewcommand{\arraystretch}{1.5}

\begin{center}
Calculus on One Page
\end{center}

\begin{tabular}{c|c}\hline
	Differential & Integral \\
	\hline
	\multicolumn{2}{c}{Fundamental Theorem of Calculus} \\ \hline
	 $\frac{\mathrm d}{\mathrm dx}\int_a^x f(t)\mathrm dt = f(x) $ &
	$\int_a^x \frac{\mathrm d}{\mathrm dt}f(t)\mathrm dt = f(x)-f(a)$  \\   \hline

	\multicolumn{2}{c}{Rules} \\  \hline
	$[c_1 u(x)+c_2 v(x)]'=c_1 u'(x)+c_2 v'(x)$ &  $\int\left[c_1f(x) + c_2 g(x)\right]\mathrm dx = c_1\int f(x)\mathrm dx + c_2 \int g(x)\mathrm dx$   \\
	$(u v)' = u'v + u v'$  & $\int u' v \mathrm dx = u v - \int uv'\mathrm dx$  \\
	$\frac{\mathrm df(u(x))}{\mathrm dx} = \frac{\mathrm df}{\mathrm du}\frac{\mathrm du}{\mathrm dx}$    &   $\int \frac{\mathrm d f}{\mathrm d u}\frac{\mathrm du}{\mathrm dx} \mathrm dx= f(u(x))+c $  \\   \hline
	\multicolumn{2}{c}{Mean Value Theorem} \\	  \hline
	$\frac{F(b) - F(a)}{b-a} = F'(\xi)$  &  $\int_a^b f(x) \mathrm dx = (b-a)f(\xi)$ \\ \hline
	\multicolumn{2}{c}{Useful Equations} \\   \hline
	$(x^n)'=nx^{n-1}$   &  $\int x^n \mathrm dx = \frac{x^{n+1}}{n+1}+c $  \\
	$(\sin x)'=\cos x$   &   $\int \cos x \mathrm dx = \sin x +c$    \\
	$(\ln x)' = \frac{1}{x}$   &   $\int \frac{1}{x} \mathrm dx = \ln\vert x \vert +c$  \\
	$(e^x)'=e^x$  &   $\int e^x \mathrm dx = e^x +c$  \\   \hline
	\multicolumn{2}{c}{Multivariable Calculus (Fundamental Theorem)} \\  \hline
	\multicolumn{2}{c}{$\int_{\partial\Omega} \bm \omega  = \int_{\Omega} \mathbf d\bm \omega$ }  \\  \hline
\end{tabular}

\vspace{5ex}
{\bf  Notes:}

\begin{itemize}
\item
One can derive other useful equation using just these in this table.
\item
The three equations of fundamental theorem for multivariable calculus can be derived using the exterior derivative.
\end{itemize}

\end{document}


\documentclass{article}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{hhline}
\usepackage[acronym]{glossaries}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{ctable}
\usepackage{float}
\usepackage{color}

\title{Experimental Results}
\begin{document}
   \maketitle

\section{Effect of simulation fixed step size on the accuracy, architecture  on neural network replacement of PID in dc motor speed control \label{step_size}}
\subsection{Performance with 10 hidden neurons}
Training Parameter: Tansig hidden layer, maximum epoch 150, stopping criteria epoch, minimum gradient, only the maximum epoch is changed everything else stays default. Such as input Delays = 1:2;
feedback Delays = 1:2; hidden Layer Size = 10;
With smaller step size, we have more data, and less error. Worst performance is shown in bold
\begin{table}[H]
\centering
\caption{\label{tab1}Effect of Fixed step size on Accuracy Testing Accuracy - Hidden Neurons 10}
\begin{tabular}{|l|l|l|}
\hline
Fixed Step Size & Training Error & Test Error \\ \hline
0.001           & 2.4116e-06     & 1.9052e-06 \\ \hline
0.01            & 2.3287e-06     & 1.8463e-06 \\ \hline
0.1             & 7.4570e-05     & 6.1457e-05 \\ \hline
0.2             & \textbf{2.8739e-04}     & \textbf{3.4440e-04} \\ \hline
\end{tabular}
\end{table}
\subsection{Performance with 3 hidden neurons}
\begin{table}[H]
\centering
\caption{\label{tab2}Effect of Fixed step size on Testing Accuracy - Hidden Neurons 3}
\begin{tabular}{|l|l|l|}
\hline
Fixed Step Size & Training Error & Test Error \\ \hline
0.001           & 2.5779e-06     & 2.0565e-06 \\ \hline
0.01            & 2.5016e-06     & 1.9960e-06 \\ \hline
0.1             & 9.5324e-05     & 7.7856e-05 \\ \hline
0.2             & \textbf{3.6994e-04 }     & \textbf{4.0580e-04} \\ \hline
\end{tabular}
\end{table}
From the experiment we can conclude that a fixed step size of 0.01 will give better performance and the minimum allowed number of neurons in the hidden layer is 3.

\section{Effect of Delay on the accuracy of neural network replacement of PID in DC motor speed control}
Using the fixed step size obtained in \ref{step_size}, and the minimum hidden layer of 3 neurons, the following are the results for varying the delay. All other parameters are kept constant.
\begin{table}[H]
\centering
\caption{\label{tab3}Effect of Delay on  Testing Accuracy}
\begin{tabular}{|l|l|l|}
\hline
Delay & Training Error & Test Error \\ \hline
2     & 2.5016e-06     & 1.9960e-06 \\ \hline
3     & 2.5152e-06     & 2.0026e-06 \\ \hline
6     & 2.4874e-06     & 1.9868e-06 \\ \hline
9     & 2.4872e-06     & 1.9871e-06 \\ \hline
12    & 2.4856e-06     & 1.9859e-06 \\ \hline
\end{tabular}
\end{table}
Observation: as the delay increases, the test accuracy gets better even though the difference is really small. However, the higher the value of the network delay, the more "complex" the architecture.

\section{Effect of Hidden Neurons on the Performance of Activation Functions}
\begin{itemize}
\item With each AF, does the number of hidden neurons have effect on their performance?
\item How low can we go before the error is unacceptable?
\item Which AF gives the minimum epoch,error with hidden neuron settings.
\end{itemize}
\begin{table}
\centering
\caption{\label{tab3}Effect of Activation Function on  Testing Accuracy}
\begin{tabular}{|l|l|l|l|l|l|l|}
\hline
\begin{tabular}[c]{@{}l@{}}Number of\\  Neurons\end{tabular} & \multicolumn{2}{l|}{3}                                        & \multicolumn{2}{l|}{4}                                        & \multicolumn{2}{l|}{10}                                      \\ \hline
Function                                                     & Epoch  & \begin{tabular}[c]{@{}l@{}}Test\\ Error\end{tabular} & Epoch  & \begin{tabular}[c]{@{}l@{}}Test\\ Error\end{tabular} & Epoch & \begin{tabular}[c]{@{}l@{}}Test\\ Error\end{tabular} \\ \hline
ElliotSig                                                    & 800    & 2.0010e-04                                           & 800    & 2.0004e-04                                           &       &                                                      \\ \hline
SQNL                                                         & 761.69 & 1.9967e-04                                           & 776.47 & 1.9951e-04                                           &       &                                                      \\ \hline
TanSig                                                       & 799.8  & 1.9968e-04                                           & 800    & 1.9921e-04                                           &       &                                                      \\ \hline
ReLu                                                         & 124.35 & 4.5861e-04                                           & 132.23 & 3.7159e-04                                           &       &                                                      \\ \hline
SQLU                                                         & 674.57 & 2.0031e-04                                           & 771.14 & 2.0023e-04                                           &       &                                                      \\ \hline
ELU                                                          & 681.81 & 2.0030e-04                                           & 776.21 & 2.0021e-04                                           &       &                                                      \\ \hline
Leaky ReLu                                                   & 132.06 & 3.2981e-04                                           & 128.06 & 3.7556e-04                                           &       &                                                      \\ \hline
\end{tabular}
\end{table}

\section{Write Up}
The use of PID as a controller in control system applications is ubiquitous. Although the theory behind PID is really simple but the design and implementation of PID controllers are known to be time consuming and difficult. Moreover, PID are linear systems and can only learn a particular plant based on their predefined settings. NN on the other hand are characterised with learning opportunities and adaptability in nature. This means that a NN based controller is robust to several input types and even maybe different plant architectures as will be proved in this work.

\section{Neural Network Controller}
We implemented a time series neural network controller for controlling the speed of a DC-motor. The following are the parameters of the DC motor ... The speed control of the motor was initially controlled using the popular PID controller and this controller is replaced by the NN controller. The best performance accuracy for the motor was obtained by training the NN with the following parameters input and feedback delays of 2 and hidden neuron size of 3. The block diagram of the experiment set-up of PID contoller replacement with Neural Network is illustrated in Fig. \ref{fig:pidNN}. Fig. \ref{fig:res_pidNN} shows the responses of a Matlab and the modelsim simulation. The ANNs use 8bit AFs, and the inferred speed in Matlab and VHDL are closely matched.
%We further demonstrated the ability of the proposed SQNL on control application. The use of PID for controlling control systems is a wide area of study however, in recent years, the disadvantages of PID controller have being solved by Neural Network Controllers. We therefore replaced the PID controller with 3 hidden neuron Neural Network architecture using the illustration in

\begin{figure}
\centering
\includegraphics[scale=0.65]{PID_NN.jpg}
\caption{\label{fig:pidNN} The Experimental setup of the NN replacement of PID controller}
\end{figure}

\begin{figure}
\centering
\includegraphics[scale=0.65]{NNpid.eps}
\caption{\label{fig:res_pidNN} The Experimental setup of the NN replacement of PID controller}
\end{figure}

\end{document}


%% 9
\section{ Defining a toy category in Haskell. }

Since we only have two oblects and three arrows, we can simply encode those as ADTs.
To encode objects, we need to choose between a standard \mintinline{haskell}{Either () ()} and a custom data type;
the latter seems to be more readable:

\inputminted[firstline=28,
             lastline=56,
             fontsize=\footnotesize, tabsize=4]{haskell}{ps1.hs}

It is easy to see that the code closely follows the answer to question 2.


% !TeX encoding = UTF-8
% !TeX program = pdflatex

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage{cite}
\usepackage{graphicx}
\graphicspath{{../Results/}}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage[labelformat=empty]{caption}
\usepackage{grffile}

\title{Reproducing results of \\
	"Enhancing adversarial defense by k-WTA"}
\author{Marco Costa, Andrea Luca Antonini}

\begin{document}

	\maketitle

	\section{Introduction}
	The goal of our project is to demonstrate the reproducibility of the paper ”Enhancing Adversarial Defense by k-Winners-Take-All” (Xiao et al.) \newline
	The use of k-WTA activation is motivated for defending against gradient-based adversarial attacks. In fact, provided a labeled data item (\textbf{x},y), the attacker finds a perturbation \textbf{x'} imperceptibly similar to \textbf{x} but misleading enough to cause the network to output a label different from y. The most effective way to find such a perturbation, i.e. adversarial example, is by exploiting the gradient information of the network w.r.t. its input \textbf{x}. \newline
	k-WTA activation function takes as input the entire output of a layer, retains its \textit{k} largest values and deactivates all others to zero. If we use $f(\textbf{x};\textbf{w})$ to denote a k-WTA network taking an input \textbf{x} and parameterized by weights \textbf{w}, then the gradient $\nabla_{\textbf{x}}f(\textbf{x};\textbf{w})$ at certain \textbf{x} is undefined. Therefore, $f(\textbf{x};\textbf{w})$ is C$^0$ discontinous. The discontinuities in the activation function also renders $f(\textbf{x};\textbf{w})$ discontinous w.r.t. \textbf{w}, but these discontinuities are rather sparse in the space of \textbf{w}, thus the network can be trained succesfully. \newline
	Formally, k-WTA retains the \textit{k} largest values of a $N$x$1$ input vector and sets all others to be zero before feeding the vector to the next network layer:

	\begin{equation}
	\phi_{k}(\textbf{y})_{j} =
	\begin{cases*}
	y_{j}\textrm{, if }y_{j} \in\{\textrm{\textit{k} largest elements of \textbf{y}}\} \\
	0 \textrm{, otherwise}
	\end{cases*}
	\end{equation}
	where $\phi_{k}: \mathbb{R}^{N} \rightarrow \mathbb{R}^{N}$ is the k-WTA function (parameterized by an integer \textit{k}), \textbf{y} $\in \mathbb{R}^{N}$ is the input to the activation, and $\phi_{k}(\textbf{y})_{j}$ denotes the j-th element of the output $\phi_{k}(\textbf{y})$.\\
	Instead of specifying \textit{k}, it's possible to introduce a parameter $\gamma \in \{0,1\}$ called \emph{sparsity ratio}. If a layer has an output dimension $N$, then its k-WTA activation has  $k = \lfloor\gamma \cdot N\rfloor$. Even though the sparsity ratio can be set differently for different layers, in practice there is no gain from introducing such a variation, so we use a fixed $\gamma$.\\
	In a Convolutional Neural Network (CNN), the output of a layer is a C x H x W tensor: in this case we will treat the tensor as a C $\cdot$ H $\cdot$ W x 1 vector input to the k-WTA activation function.\\
	The runtime of computing a k-WTA activation is asymptotically $O(N)$, because finding \textit{k} largest value in a list corresponds to finding the k-th largest value, which has $O(N)$ complexity.\\
	Concerning the training of k-WTA networks, when the sparsity ratio $\gamma$ is relatively small ($\le 0.2$), the network training converges slowly. In fact, a smaller $\gamma$ activates fewer neurons, effectively reducing more of the layer width and, therefore, the network size. Nevertheless, it is preferable to use a smaller $\gamma$ because it usually leads to better robustness against finding adversarial examples.\\
	Theoretically speaking, consider one layer outputting values \textbf{x}, passed through  a k-WTA activation, and followed by the next layer whose linear weight matrix is
	W. We define the k-WTA \emph{activation pattern} under the input \textbf{x} as:
	\begin{center}
		$\mathcal{A}(\textbf{x}) := \{i \in [l]$ | $x_{i}$ is one of the \textit{k} largest values in \textbf{x}$\} \subseteq [l]$
	\end{center}
	where we use $[l]$ to indicate integer set $\{1,...,l\}$.\\
	Even an infinitesimal perturbation of \textbf{x} can change $\mathcal{A}(\textbf{x})$: some element $i$ is removed from $\mathcal{A}(\textbf{x})$, while another element $j$ is added in. Corresponding to this change, in the evaluation of $W\phi_{k}(\textbf{x})$, the contribution of $W$'s column vector $W_{i}$ is useless, while another column vector $W_{j}$ suddenly takes effect. It's this change that makes the result of $W\phi_{k}(\textbf{x})$ $C^0$ discontinous.\\
	Once $W$ is determined, the discontinuity jump depends on the value of $x_i$ and $x_j$, that are respectively the input value that does no more affect the result and the new value that actually has effect. The smaller $\gamma$ is, the larger the discontinuity jump will be: for this reason, it is recommended to use a smaller $\gamma$, that will make the search of adversarial examples harder.\\
	If the activation patterns of all layers are fixed, then f(\textbf{x}) is a linear function, but when the activation pattern changes, f(\textbf{x}) switches from one linear function to another linear function. Over the entire space of \textbf{x}, f(\textbf{x}) is \emph{piecewise linear}. The specific activation patterns of all layers define a specific linear piece of the function, i.e. a \emph{linear region}: in k-WTA, these linear regions are disconnected.\\
	If the linear regions are densely distributed, a small perturbation $\Delta\textbf{x}$ from any data point will likely cross the boundary of the linear region where \textbf{x} is. Whenever boundary crossing occurs, the gradient becomes undefined.\\
	The rationale behind k-WTA activation is to destroy network gradients, that are information needed in \emph{white box attacks}.\\
	k-WTA is able to universally improve the white-box robustness, regardless of the training method. The k-WTA robustness under \emph{black box attacks} is not always significantly better than other activation function networks (e.g. ReLU).

	\section{Approach}
	The core of our approach is to substitute the most commonly used activation functions (e.g. ReLU) with k-WTA and measure their performance and robustness to adversarial attacks.
	The idea is to compare the performance of the models with k-WTA with those who have a most common ReLU.
	In order to do this, we used two different networks, i.e. \textbf{\textit{ResNet}} and \textbf{\textit{AlexNet}}, trained on different datasets, i.e. \textbf{\textit{CIFAR-10}} and \textbf{\textit{SVHN}}.\\
	By examining the resulting robustness of the trained models, we wanted to confirm the effectiveness of the method.

	\section{Experiments}
	\subsection{Frameworks and Data}
	All the experiments have been done with Pytorch deep learning framework, using GPU acceleration for the execution of the experiments.\\
	As said before, we considered ResNet and AlexNet models using the standard implementations of ResNet18 and AlexNet architecture from the Torchvision package. We used these networks in two ways. First, using the pre-trained models on ImageNet dataset, modifiying the final layer to output 10 features and fine-tuned the models on CIFAR-10 and SVHN dataset. Second, training the networks from scratch, directly on CIFAR-10 and SVHN datasets.

	\subsection{Training models}
	In order to train the models we proceeded as follows: \\
	For networks that use the \textit{ReLU} activation function, we have simply traied them with 80 epochs for models trained from scratch and 10 epochs for pre-trained models. For networks that use the \textit{k-WTA} activation function, instead, we replaced the \textit{ReLU} activation layer with \textit{k-WTA} using two sparsity ratio, first $\gamma = 0.2$ and then $\gamma = 0.1$, using the same training scheme seen before (80 epochs for models trained from scratch, 10 for pre-trained models). \\
	The authors of the paper, instead, used a different approach. For k-WTA networks with a sparsity ratio $\gamma = 0.1$, they trained them incrementally, starting with $\gamma = 0.2$ for 50 epochs with SGD and then decreasing $\gamma$ by $0.005$ every 2 epochs until it reached $0.1$. \\
	We noticed that, obvoiusly, training the models from scratch is much more expensive in terms of time than training pre-trained models.\\
	Our training dataset was preprocessed with random cropping and random horizontal flipping. Finally, we additionally scaled up images to 224x224 since the Torchvision pre-trained model architectures expect this input dimensionality.\\
	We used a batch size of 64 in the data loaders.\\
	All of our models used the Cross Entropy loss function and were trained with Stochastic Gradient Descent using	a learning rate of $0.01$ for pre-trained models, while for the others learning rate is set to $0.1$ for the first 50 epochs, while for the remaining 30 is manually changed to 0.01. \\ Moreover, we inserted a momentum of $0.9$, and in order to avoid exploding gradients we also used gradient clipping with a max norm of $0.5$

	\subsection{Robustness to Adversarial attacks}
	k-WTA activation function, as explained in the introduction, is wanted to be a defence against attacks that exploit the gradient of the network. In order to neutralize these attacks, k-WTA, thanks to its densely piecewise continous nature, destroys network gradients and, therefore, represents a counterpoison to the venom of attacks, specially white box attacks. Our goal is to test the effective robustness to adversarial attacks of the models using k-WTA activation function, comparing the results with models using ReLU activation function. To achieve this, we tested the various models ((Resnet | AlexNet) + (k-WTA | ReLU)) under two kind of attacks, namely Projected Gradient Descent (\textbf{\textit{PGD}}) and \textbf{\textit{Deepfool}}. Both the attacks are run using the Foolbox third-party toolkit.\\
	We used a perturbation $\epsilon = 0.031$ in the case of CIFAR-10 dataset, while in the case of SVHN dataset $\epsilon = 0.047$. PGD attack is carried on for 40 steps, with random starting and relative step size of $0.003$. Instead, Deepfool attack is run for 20 steps with a candidate sub-sampling parameter of 10. The attacks were executed for 20 mini-batches with batch size 64.

	\subsection{Results}
	In \textit{Table 1} and \textit{Table 2} we show the results for training an AlexNet network on SVHN and CIFAR-10 respectively, using ReLU, k-WTA with $\gamma = 0.2$ and k-WTA with $\gamma = 0.1$. The value $A_{std}$ indicates the standard test accuracy, while the other two columns show the robustness accuracy under PGD and Deepfool attacks. \textit{Table 3} and \textit{Table 4} illustrates the results with the networks with same characteristics, but with the difference that they are pre-trained.
	A series of graphics representing \emph{accuracy}, \emph{loss} and \emph{error} of the various experiments can be found in \emph{Appendix A}.
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$19.59\%$&$18.91\%$&$20.08\%$	\\
			\hline
			k-WTA-0.2	&$95.35\%$&$44.30\%$&$1.95\%$	\\
			\hline
			k-WTA-0.1	&$95.34\%$&$35.31\%$&$0.39\%$	\\
			\hline
		\end{tabular}
		\caption{Table 1: AlexNet experiment results with SVHN}\label{alexnetSVHN}
	\end{table}
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$89.34\%$&$62.19\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$88.84\%$&$63.52\%$&$0.00\%$	\\
			\hline
			k-WTA-0.1	&$89.58\%$&$61.64\%$&$0.08\%$	\\
			\hline
		\end{tabular}
		\caption{Table 2: AlexNet experiment results with CIFAR-10}\label{alexnetCIFAR10}
	\end{table}
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$90.62\%$&$10.62\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$90.64\%$&$8.98\%$&$0.00\%$	\\
			\hline
			k-WTA-0.1	&$87.66\%$&$35.62\%$&$2.50\%$	\\
			\hline
		\end{tabular}
		\caption{Table 3: Pre-trained AlexNet experiment results with SVHN}\label{pre-alexnetSVHN}
	\end{table}
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$87.32\%$&$3.83\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$88.08\%$&$8.20\%$&$0.00\%$	\\
			\hline
			k-WTA-0.1	&$83.64\%$&$23.52\%$&$11.02\%$	\\
			\hline
		\end{tabular}
		\caption{Table 4: Pre-trained AlexNet experiment results with CIFAR-10}\label{pre-alexnetCIFAR10}
	\end{table}\\
	Then, we show the experiment results for training using network ResNet18, with SVHN dataset (\textit{Table 5}) and CIFAR-10 dataset (\textit{Table 6}) on ReLU, k-WTA-0.2 and k-WTA-0.1.
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$96.19\%$&$57.58\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$95.37\%$&$68.52\%$&$13.91\%$	\\
			\hline
			k-WTA-0.1	&$94.17\%$&$69.45\%$&$24.69\%$	\\
			\hline
		\end{tabular}
		\caption{Table 5: ResNet18 experiment results with SVHN}\label{resnetSVHN}
	\end{table}
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$93.66\%$&$25.23\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$90.15\%$&$67.58\%$&$10.62\%$	\\
			\hline
			k-WTA-0.1	&$84.95\%$&$68.12\%$&$26.17\%$	\\
			\hline
		\end{tabular}
		\caption{Table 6: ResNet18 experiment results with CIFAR-10}\label{resnetCIFAR10}
	\end{table}\\
	Again, we show the results in \textit{Table 7} and \textit{Table 8} for networks with the same characteristics as before, but in this case they are pre-trained.
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$95.08\%$&$0.78\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$92.27\%$&$0.00\%$&$0.00\%$	\\
			\hline
			k-WTA-0.1	&$83.52\%$&$0.00\%$&$13.75\%$	\\
			\hline
		\end{tabular}
		\caption{Table 7: Pre-trained ResNet18 experiment results with SVHN}\label{pre-resnetSVHN}
	\end{table}
	\begin{table}[!htbp]
		\begin{tabular}{|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}|}
			\hline
			Activation	& $A_{std}$	&	PGD	&	Deepfool	\\
			\hline
			ReLU		&$95.12\%$&$3.83\%$&$0.00\%$	\\
			\hline
			k-WTA-0.2	&$90.79\%$&$0.16\%$&$10.62\%$	\\
			\hline
			k-WTA-0.1	&$76.92\%$&$3.05\%$&$25.16\%$	\\
			\hline
		\end{tabular}
		\caption{Table 8: Pre-trained ResNet18 experiment results with CIFAR-10}\label{pre-resnetCIFAR10}
	\end{table}

	\section{Conclusions}
	Analyzing the results of the various experiments, we can say that replacing the ReLU activation function with k-WTA activation function increases the robustness against white box attacks, minimally affecting the accuracy of the model.\\
	Differently from the conclusions reported in the paper \cite{xiao2019resisting}, our results show that using k-WTA with smaller sparsity ratio (0.1) not always implies better robustness. Another fact that we can infer from our experiments is that, using pre-trained networks, we cannot find substantial changes w.r.t. networks trained from scratch on CIFAR-10 and SVHN. On the contrary, we have an improvement in terms of time, given that pre-trained networks are trained only for 10 epochs.\\
	Analyzing the robustness w.r.t the two attacks, we can see that with Deepfool attack on ResNet networks, in general we have a marked improvement with the use of k-WTA w.r.t. ReLU. Moreover, experiments confirm that using k-WTA with lower sparsity ratio implies a better robustness against attacks.\\
	Concerning Deepfool applied on AlexNet, using k-WTA or ReLU is practically irrelevant, since robustness is quite always equal to 0.00\%.\\
	Talking about PGD, instead, we can observe surprisingly different and maybe unexpected results. As we can see in the tables, using k-WTA not always means improvement in robustness and, sometimes, k-WTA-0.2 gives better robustness w.r.t. k-WTA-0.1.\\
	Another fact to highline is that, training AlexNet on SVHN dataset, only using k-WTA with sparsity ratio 0.2 we can achieve an high accuracy, while in the case of k-WTA-0.1 and ReLU accuracy results to be very low.

	\section{Appendix A}
		\subsection{AlexNet on dataset SVHN}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_ReLU}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_k-WTA_0.2.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_k-WTA_0.1.png}
			\end{center}

		\subsection{Pre-trained AlexNet on dataset SVHN}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_ReLU_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_k-WTA_0.2_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_SVHN_k-WTA_0.1_Pretrained.png}
			\end{center}

		\subsection{AlexNet on dataset CIFAR-10}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_ReLU.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_k-WTA_0.2.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_k-WTA_0.1.png}
			\end{center}

		\subsection{Pre-trained AlexNet on dataset CIFAR-10}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_ReLU_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_k-WTA_0.2_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{AlexNet_CIFAR-10_k-WTA_0.1_Pretrained.png}
			\end{center}

		\subsection{ResNet18 on dataset SVHN}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_ReLU.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_k-WTA_0.2.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_k-WTA_0.1.png}
			\end{center}

		\subsection{Pre-trained ResNet18 on dataset SVHN}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_ReLU_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_k-WTA_0.2_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_SVHN_k-WTA_0.1_Pretrained.png}
			\end{center}

		\subsection{ResNet18 on dataset CIFAR-10}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_ReLU.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_k-WTA_0.2.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_k-WTA_0.1.png}
			\end{center}

		\subsection{Pre-trained ResNet18 on dataset CIFAR-10}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_ReLU_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_k-WTA_0.2_Pretrained.png}
			\end{center}
			\begin{center}
				\centering
				\includegraphics[width=400px,keepaspectratio]{ResNet18_CIFAR-10_k-WTA_0.1_Pretrained.png}
			\end{center}

		\bibliography{bibliography}
		\bibliographystyle{ieeetr}
\end{document}


\section{Preprocessing}
\subsection{3D to 2D conversion}
\nblink{brats/02\_preprocess.ipynb}
The scans in the BraTS datasets are 3D volumes. There is no general limitation of neural networks to work with 3D input, but 3D volumes are very big and would require many input neurons which in turn increases the model size and training time significantly.

We therefore decided to extract three horizontal slices from the 3D volume and save them as normal gray-scale images.
The extracted slice should show a part of the tumor in the brain, hence we can not just take random slices from a volume.

The used algorithm works like this (see Figure \ref{preprocessing_layers} for a graphical explanation):

\begin{itemize}
    \item From the top of the brain, search downwards on all modalities until the top of the tumor is found
    \item From the bottom of the brain, search upwards on all modalities until the bottom of the tumor is found
    \item Take the first slice 25\% into the tumor from the top to the bottom
    \item Take the second slice in the middle of the top and bottom of the tumor
    \item Take the last slice 25\% into the tumor from the bottom to the top
\end{itemize}

\begin{figure}[H]
    \centering
    \includegraphics[width=14cm]{chapters/04_segmentation/images/slicing.png}
    \caption{Extracting three 2D layers from the 3D volume. Search top and bottom of the tumor (white lines). Extract layer L1 at 25\% from the top to bottom, L2 at 50\% and L3 at 75\%.}
    \label{preprocessing_layers}
\end{figure}

\subsection{Training and test data split}
\nblink{brats/03\_train\_test\_split.ipynb}

Slices taken from the same brain look very similar. It would be very easy for a neural network to correctly segment an image when an image from the same brain was part of the training data set.
We therefore split up the preprocessed dataset into a training and a test set while keeping the slices from the same brain in the same set. We used 80\% of the scans for training and 20\% of the scans for testing.


\documentclass[a4paper]{article}

\usepackage{fontspec,xltxtra,xunicode}
\usepackage{xeCJK}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{xcolor}
\usepackage{float}
\usepackage{pdfpages}

\title{\emph{Statistical Analysis with Missing Data}\ \ Problem 1.6\\Bootstrap,Jackknife}
\author{16212799,何浩勋}
\date{September 7, 2017}

\setCJKfamilyfont{yahei}{微软雅黑}
\newcommand{\yahei}{\CJKfamily{yahei}}

\begin{document}

\fontsize{12pt}{18pt}\selectfont\yahei
\maketitle

\section{Problem 1.6}
\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
100 trivariate normal observations
$\{(y_{i1},y_{i2},u_i),i=1,...,100\}$ on
$(Y_1, Y_2, U)$ as follows:
}

\begin{equation}
\begin{array}{lll}
y_{i1}=1+z_{i1} \\
y_{i2}=2*z_{i1}+z_{i2}\\
u_i=a*(y_{i1} - 1)+b*(y_{i2}-5)+z_{i3}
\end{array}
\end{equation}

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
where $\{(z_{i1},z_{i2}, z_{i3}), i=1,...,100\}$ are independent standard normal deviates.
The latent variable U determines missingness of $Y_2$ as follows:}

\begin{equation}
y_{i2}\ is\ missing\ if\ u_i\ <\ 0
\end{equation}

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
Label Description\\
origin\ : original generated Data\\
obs\ : $Y_1$ and $Y_2$ both observed\\
mis\ : $Y_1$ observed and $Y_2$ missing
}

\newpage

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
Case 1: a=0,b=0\\
Y1(origin) : mean =8.882104e-01, std=9.724232e-01\\
Y1(obs)    : mean =9.310380e-01, std=9.661219e-01\\
Y1(mis)    : mean =8.265806e-01, std=9.781347e-01\\
origin Vs mis : t =3.3875e-01, pvalue =7.3531e-01\\
obs Vs mis    : t =5.2375e-01, pvalue =6.0164e-01\\
Conclusion : There is no Significant differences.\\
$f(M|Y_1,Y_2,U)=F(M|Z_3)$,$Z_3$ is independent of $Y_2$,$Y_1$.\\
The machenism is MCAR.
}

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.5]{a0b0.png}
	\label{pic1}
\end{figure}

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
Case 2: a=2,b=0\\
Y1(origin) : mean =8.882104e-01, std=9.724232e-01\\
Y1(obs) : mean =1.635554e+00, std=6.775540e-01\\
Y1(mis) : mean =1.701749e-01, std=6.007170e-01\\
origin  Vs mis : t =4.7925e+00, pvalue =3.9468e-06\\
obs Vs mis : t =1.1339e+01, pvalue =1.5452e-19\\
Conclusion : There is a significant difference.\\
f(M|Y1,Y2,U) = F(M|Y1,Z3),The machenism is MAR.
}

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.5]{a2b0.png}
	\label{pic2}
\end{figure}

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
Case 3: a=0,b=2\\
Y1(origin) : mean =8.882104e-01, std=9.724232e-01\\
Y1(obs) : mean =1.701187e+00, std=6.813406e-01\\
Y1(mis) : mean =2.749121e-01, std=6.588037e-01\\
origin Vs mis : t =4.2122e+00, pvalue =4.2763e-05\\
obs Vs mis : t =1.0455e+01, pvalue =1.2476e-17\\
Conclusion : There is a significant difference.\\
f(M|Y1,Y2,U) = F(M|Y2,Z3),the machanism is NMAR.
}

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.5]{a0b2.png}
	\label{pic3}
\end{figure}

\newpage
\section{Bootstrap,Jackknife}

\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
$\{X_i, i=1,...,100\}$ is a random sample from a $N(7,4)$ population.
Three methods(Moment,Bootstrap,Jackknife) are applied to estimate the population mean $\mu$ and variance $\sigma^2$.
}
\paragraph{\fontsize{12pt}{16pt}\selectfont\yahei
It appears that the traditional moment method makes the best estimation of both $\mu$ and $\sigma^2$ while another two methods also make similar estimates.Overall,There is no significant difference among the estimates made by these three methods.}

\begin{table}[h]
\fontsize{12pt}{18pt}\selectfont\yahei
\centering
	\begin{tabular}{|l|c|c|c|c|}\hline
		Method & $\mu$(estimate) & variance \\
		\hline
		Moment & 7.11961603 & 0.04063306 \\
		Bootstrap & 7.11606507 & 0.04967192\\
		Jackknife & 7.11961603 & 0.04104350\\
		\hline
	\end{tabular}
	\caption{estimates of $\mu$}
\end{table}

\begin{table}[h]
\fontsize{12pt}{18pt}\selectfont\yahei
\centering
	\begin{tabular}{|l|c|c|c|c|}\hline
		Method & $\sigma^2$(estimate) & variance\\
		\hline
		Moment & 4.06330648 & 0.26774111\\
		Bootstrap & 4.06615074 & 0.32251543\\
		Jackknife & 4.10434998 & 0.27593671\\
		\hline
	\end{tabular}
	\caption{estimates of $\sigma^2$}
\end{table}

\end{document}


\documentclass[12pt]{article}
\usepackage{amsmath, amsthm}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\textwidth 12cm
\textheight 20cm

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}

\renewenvironment{abstract}
 {\small
  \begin{center}
  \bfseries \abstractname\vspace{-.5em}\vspace{0pt}
  \end{center}
  \list{}{
    \setlength{\leftmargin}{.5cm}%
    \setlength{\rightmargin}{\leftmargin}%
  }%
  \item\relax}
 {\endlist}

\begin{document}
\title{Existence and uniqueness of solutions for multidimensional nonlinear partial differential equations}
\author{Joel Reuben Lefkowitz \\
  \multicolumn{1}{p{.9\textwidth}}{\centering\emph{Undergraduate at The Univerity of Cambridge, Department of Engineering}}}
\maketitle

\begin{abstract}
  Abstract.
\end{abstract}
\section{Introduction}
Text.

\begin{theorem}
  Text.
\end{theorem}

\begin{theorem}[Pythagorean theorem]
  \label{pythagorean}
  \[ x^2 + y^2 = z^2 \]
\end{theorem}

A consequence of theorem \ref{pythagorean}:

\begin{corollary}
  Text.
\end{corollary}

\begin{lemma}
  Text.
\end{lemma}

\begin{thebibliography}{99}

  \bibitem{gasrah} G. Gasper, M. Rahman, {\it Basic Hypergeometric Series}, Cambridge University Press, Cambridge (1990).

\end{thebibliography}
\end{document}


\clearpage
\subsection{Machine Code} % (fold)
\label{sub:machine_code}

\emph{What instructions do Computers understand?}

Computers do not really \emph{understand} anything, computers are \textbf{unintelligent}. They are a machine that respond in a set way to a given number of instructions. The instructions that a computer uses is called its {\em instruction set} and contain instructions to perform basic mathematic operations, loading and storing data in memory, comparing numeric values, and moving to the new instruction elsewhere in the program. These very simple actions are performed very quickly, and can be use to create everything you have ever seen a computer do.

The computers instructions can be seen as binary numbers, numbers made from 0's and 1's. These values are like switches that are either off (0) or on (1). Setting these \emph{switches} to different sequences will cause the computer to perform different actions. For example, the \emph{switch} combination \texttt{0000 0011}, may cause the computer to add two numbers together. Any time you want the computer to perform this task you set the switches to that combination. These binary instructions are called \textbf{machine code}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.8\textwidth]{./topics/programs-and-compilers/diagrams/MachineCode}
   \caption{The computer responds to machine code instructions}
   \label{fig:machine-code}
\end{figure}

\mynote{
\begin{itemize}
  \item The \textbf{CPU}, Central Processing Unit, is the workhorse of the computer. It executes the program's instructions.
  \item The instructions a CPU uses is called its \textbf{Instruction Set}, and different CPUs have different instruction sets.
  \item Some common instruction sets: ARM (used in most mobiles and tablets), x86-64 (used in desktops and laptops).
\end{itemize}
}

\clearpage

\subsubsection{Programming in Machine Code} % (fold)
\label{ssub:programming_in_machine_code}

Listing \ref{lst:machine code} shows a chunk of the machine code for a small program. These 1s and 0s are the codes used to instruct the computer when this program is executed. Programs can be written directly in machine code, but this is a time consuming task. This is further complicated by the fact that machine code is unique to each kind of CPU. This means that programming at this level is entirely dependent on the kind of processor that you are targeting.

\begin{lstlisting}[caption={128 bits from the 106,752 bits of Machine Code from a small program.},label={lst:machine code}]
...
0110 0111 0111 0010 0000 0000 0110 0011 0100 1110 0101 1111 0100 0001 0101 1000
0110 0111 0111 0010 0000 0000 0111 0110 0101 1111 0101 1111 0110 1001 0101 1111
...
\end{lstlisting}

No one wants to have to work at this level of details, and fortunately you do not need to. Software developers have created tools to help them create programs without having to think about these low level details. These tools make it possible to work at a \textbf{higher level of abstraction}. They take the code you write, and do the hard work of converting that to the machine code of the computer you want to run it on.

\mynote{
\begin{itemize}
  \item You can look at the machine code of any program on your computer. You just need the right tools.
  \item If you open the program's executable file in a text editor it will look very strange, and not at all like a large list of binary values. This is because the text editor displays one character for every byte (or two bytes depending on the file) from the file.
  \item A \textbf{Hex Editor} is a program that is useful for examining binary data. It shows you one character for every four bits in the file.
\end{itemize}
}

\begin{table}[h]
  \ttfamily
  \centering
\begin{tabular}{|c|c||c|c||c|c||c|c|}
  \hline
  Binary & Hex & Binary & Hex & Binary & Hex & Binary & Hex  \\
  \hline
  0000 & \textbf{0} & 0001  & \textbf{1}  & 0010  &  \textbf{2} & 0011 & \textbf{3} \\
  0100 & \textbf{4} & 0101 & \textbf{5} & 0110 & \textbf{6} & 0111 & \textbf{7} \\
  1000 & \textbf{8} & 1001 & \textbf{9} & 1010 & \textbf{A} & 1011 & \textbf{B} \\
  1100 & \textbf{C} & 1101 & \textbf{D} & 1110 & \textbf{E} & 1111 & \textbf{F} \\
  \hline
\end{tabular}
  \caption{Binary to Hexadecimal}
\end{table}

\begin{figure}[h]
   \centering
   \includegraphics[width=0.55\textwidth]{./topics/programs-and-compilers/images/HexEditor}
   \caption{A HexEditor allows you to view the machine code of any program}
   \label{fig:hex-editor}
\end{figure}

% subsubsection programming_in_machine_code (end)

% subsection machine_code (end)


%% !TEX root = manual.tex

\section{Network Topologies and Routing}
\label{sec:tutorial:topology}
We here give a brief introduction to specifying different topologies and routing strategies.
We will only discuss one basic example (torus).
A more thorough introduction covering all topologies is planned for future releases.
Excellent resources are ``Principles and Practices of Interconnection Networks'' by Brian Towles and William Dally published by Morgan Kaufman and ``High Performance Datacenter Networks'' by Dennis Abts and John Kim published by Morgan and Claypool.

\subsection{Topology}
\label{subsec:tutorial:topology}

Topologies are determined by two mandatory parameters.

\begin{ViFile}
topology.name = torus
topology.geometry = 4 4
\end{ViFile}
Here we choose a 2D-torus topology with extent 4 in both the $X$ and $Y$ dimensions for a total of 16 nodes (Figure \ref{fig:torus:basic})
The topology is laid out in a regular grid with network links connecting nearest neighbors.
Additionally, wrap-around links connect the nodes on each boundary.
\begin{figure}[h]
\centering
\includegraphics[width=0.5\textwidth]{figures/tikz/torus/torus.png}
\caption{4 x 4 2D Torus}
\label{fig:torus:basic}
\end{figure}

The figure is actually an oversimplification.
The \inlinefile{topology_geometry} parameter actually specifies the topology of the \emph{network switches}, not the compute nodes.
A torus is an example of a direct network in which each switch has one or more nodes ``directly'' connected to it.
A more accurate picture of the network is given in Figure \ref{fig:torus:withnodes}.
\begin{figure}[h]
\centering
\includegraphics[width=0.4\textwidth]{figures/tikz/torus/withnodes.png}
\caption{4 x 4 2D Torus of Network Switches with Compute Nodes}
\label{fig:torus:withnodes}
\end{figure}
While in many previous architectures there was generally a one-to-one correspondence between compute nodes and switches, more recent architectures have multiple compute nodes per switch (e.g. Cray Gemini with two nodes).
Multinode switches can be specified via

\begin{ViFile}
topology {
 name = torus
 geometry = 4 4
 concentration = 2
}
\end{ViFile}
which would now generate a torus topology with 16 switches and 32 compute nodes.

Another subtle modification of torus (and other networks) can be controlled by giving the $X$, $Y$, and $Z$ directions different bandwidth.
The above network could be modified as

\begin{ViFile}
topology {
 name = torus
 geometry = 4 4
 redundant = 2 1
}
\end{ViFile}
giving the the $X$-dimension twice the bandwidth of the $Y$-dimension.
This pattern DOES exist in some interconnects as a load-balancing strategy.
A very subtle point arises here. Consider two different networks:

\begin{ViFile}
topology {
 name = torus
 geometry = 4 4
 redundant = 1 1
}
network_bandwidth = 2GB/s
\end{ViFile}
\begin{ViFile}
topology {
 name = torus
 geometry = 4 4
 redundant = 2 2
}
network_bandwidth = 1GB/s
\end{ViFile}
For some coarse-grained models, these two networks are exactly equivalent.
In more fine-grained models, however, these are actually two different networks.
The first network has ONE link carrying 2 GB/s. The second network has TWO links each carrying 1 GB/s.

\subsection{Routing}
\label{subsec:tutorial:routing}
By default, \sstmacro uses the simplest possible routing algorithm: dimension-order minimal routing (Figure \ref{fig:torus:basicrouting}).
\begin{figure}[h]
\centering
\includegraphics[width=0.4\textwidth]{figures/tikz/torus/minroutetorus.png}
\caption{Dimension-Order Minimal Routing on a 2D Torus}
\label{fig:torus:basicrouting}
\end{figure}
In going from source to destination, the message first travels along the $X$-dimension and then travels along the $Y$-dimension.
The above scheme is entirely static, making no adjustments to avoid congestion in the network.
\sstmacro supports a variety of adaptive routing algorithms.  This can be specified:

\begin{ViFile}
switch {
 router {
  name = min_ad
 }
}
\end{ViFile}
which specifies minimal adaptive routing.
There are now multiple valid paths between network endpoints, one of which is illustrated in Figure \ref{fig:torus:minadrouting}.
\begin{figure}[h!]
\centering
\includegraphics[width=0.4\textwidth]{figures/tikz/torus/minadroutetorus.png}
\caption{Adaptive Minimal Routing on a 2D Torus}
\label{fig:torus:minadrouting}
\end{figure}
At each network hop, the router chooses the \emph{productive} path with least congestion.
In some cases, however, there is only one minimal path (node $(0,0)$ sending to $(2,0)$ with only $X$ different).
For these messages, minimal adaptive is exactly equivalent to dimension-order routing.
Other supported routing schemes are valiant and UGAL.  More routing schemes are scheduled to be added in future versions.
A full description of more complicated routing schemes will be given in its own chapter in future versions.
For now, we direct users to existing resources such as ``High Performance Datacenter Networks'' by Dennis Abts and John Kim.


\documentclass{article}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\DeclarePairedDelimiter\abs{\lvert}{\rvert}%
\DeclarePairedDelimiter\norm{\lVert}{\rVert}%

% Swap the definition of \abs* and \norm*, so that \abs
% and \norm resizes the size of the brackets, and the
% starred version does not.
\makeatletter
\let\oldabs\abs
\def\abs{\@ifstar{\oldabs}{\oldabs*}}
%
\let\oldnorm\norm
\def\norm{\@ifstar{\oldnorm}{\oldnorm*}}
\makeatother
%

\providecommand\given{} % so it exists
\newcommand\SetSymbol[1][]{
	\nonscript\,#1\vert \allowbreak \nonscript\,\mathopen{}}

\DeclarePairedDelimiterX\Set[1]{\lbrace}{\rbrace}%
{ \renewcommand\given{\SetSymbol[\delimsize]} #1 }

\newcommand{\pd}[2]{
	\frac{\partial{#1}}{\partial{#2}}
}

\newcommand{\ppd}[1]{
	\frac{\partial}{\partial{#1}}
}

\newcommand{\mean}[3]{
	\frac{1}{#2} \sum_{#1 = 1}^{#2}{#3}
}

\newcommand{\score}[1]{
	\textrm{score}(#1)
}

\newcommand{\pa}[1]{Pa(#1)}

\newcommand{\affnodes}[1]{
	\textrm{Aff}(#1)
}

\newcommand{\addedge}[1]{
	\textrm{Add}(#1)
}

\newcommand{\rmedge}[1]{
	\textrm{Del}(#1)
}

\newcommand{\revedge}[1]{
	\textrm{Rev}(#1)
}

\newcommand{\bigO}[1]{
	\mathcal{O}{(#1)}
}

\newcommand{\graph}[1]{
	\mathcal{#1}
}

\newcommand{\var}[1]{{\ttfamily#1}}% variable

\newcommand{\ops}[1]{
	\textrm{ops}(#1)
}

\newcommand{\argmin}{\operatornamewithlimits{argmin}}
\newcommand{\argmax}{\operatornamewithlimits{argmax}}

\author{Dmitry Lunin}
\title{EM + RVR for structure learning}
\begin{document}
\maketitle

\section{RVR for entropy estimation}
\paragraph{Graph scores and mutual information} Often computing graph scores for structure learning problem requires mutual information estimation. For example, BIC score. The reason behind this is that in discrete case, likelihood of a Bayesian network structure can be computed using mutual information:
\begin{equation}
	\log p(\graph{G}|X) = N \sum_{i=1}^M{I(X_i; Pa(X_i))} - N \sum_{i=1}^M{H(X_i)}
\end{equation}
where $N$ is the number of data points, $M$ is the number of variables.
\paragraph{Entropy}
Mutual information, on the other hand, can be computed via entropy:
\begin{equation}
	I(X; Y) = H(X) + H(Y) - H(X, Y)
\end{equation}

\paragraph{RVR} It is possible to learn to predict entropy of a subset of variables using Relevance Vector Machine for regression (RVR). It can predict entropy with decent accuracy (however SVR performs better). Here we need to estimate the probability disribution on entropies, so we use RVR instead of SVR.

\section{EM for structure learning}
\paragraph{Idea} The Bayesian network graph ($\graph{G}$) is the parameter we want to estimate, all the entropies between variables ($H$) are hidden variables in the EM algorithm.
$X$ is the training dataset of the structure learning algorithm.
\paragraph{E-step}
In the expectation step, we have to estimate $p(H|X, \graph{G})$.
\begin{enumerate}
	\item Fit the parameters of the Bayesian network with structure $\graph{G}_{old}$.
	\item Build the training data by running inference on random subsets of variables, then calculating entropy of the resulting distribution.
	\item Train the RVR on the training data from step 2.
\end{enumerate}
Now we have a RVR model which can predict $q(H) = p(H|X, \graph{G}_{old})$.
\paragraph{M-step}
The goal of the M-step is to maximize
\begin{equation}
E_{q(H)}[\log p(X, H| \graph{G})] \to \max_{\graph{G}}
\end{equation}
\begin{equation}
E_{q(H)}[\log p(X | H, \graph{G})] + E_{q(H)}[\log p(H|\graph{G})] \to \max_{\graph{G}}
\end{equation}
$\log p(X| H, \graph{G})$ can be easily computed (see eq. (1)).

\begin{thebibliography}{9}

	\bibitem{KollerFriedman}
	Daphne Koller, Nir Friedman
	\emph{Probabilistic Graphical Models: Principles and Techniques},
	The MIT Press, Cambridge, Massachusetts,
	2009.

\end{thebibliography}

\end{document}


\section{Constrained Dynamics}

Sometimes we are interested in more than simply the number of degrees of freedom a particular configuration has. To explore the configuration space, find similarities between different configurations, analyze the geometric configuration space's topology, or compute other relevant statistics, it can be useful to compute dynamic processes on the geometric configuration space. There are clear computational challenges to computing such dynamics due to computational handling of constraints, the lack of an explicit parameterizations, and the fact that geometric configuration space is often of much smaller dimension than the ambient space it sits in.

%Constraint algorithms have been widely studied for use in molecular dynamic simulations. Many  methods use a two stage scheme in which the unconstrained dynamics are first used to evolve the system to an intermediary configuration $\hat{z}_{n+1}$ based on the previous configuration $z_n$. In the second stage, Lagrange multipliers are used to correct $\hat{z}_{n+1}$ to a new configuration $z_{n+1}$ which satisfies the constraints. Previously, we used the SHAKE algorithm--which employs this two stage philosophy--to examine a constraint space used to model the configurations of cyclohexane~\cite{Pandey2014}.

%Constraint algorithms can be used to compute various statistics of the configuration space. For example, the relative volume of different regions of configuration space can be empirically estimated. Additionally, if we have soft constraints, such as preferred angles between faces, we can institute cost functions on the configuration space with configurations having more favorable angles having a lower cost. Whether physically or artificially motivated, such cost functions can be treated as potential energy functions which can imply specific dynamics. Under this set of dynamics, quantities such as the average or minimum energies may be of interest.

%We may also be interested in the behavior of a small part of the configuration as it undergoes constrained dynamics. For instance, suppose we are interested in the tendency for two edges on distinct faces to come together and form a new connection. By looking at the distance between these edges, we can measure how frequently this event happens and the probability that it will occur before another event of interest. This is similar to the exit time problem for diffusion and may provide a physically motivated evaluation tool for the appropriateness of our transition rules we adopt in our models.

%\subsection{Cyclohexane Application}

%??include??

%\label{introduction}
%Cyclohexane is a molecule composed of six carbon atoms and twelve hydrogen atoms. The carbon atoms are connected in a ring with two hydrogen atoms attaching to each carbon. Since each carbon has four bonds, the energetically preferred bond spacing is at tetrahedral angles. While this preference dictates much of the cyclohexane structure, there are several structurally distinct conformations the molecule can take.
%
%Of the many forces acting on the cyclohexane molecule, we focus on three. \textbf{Eclipsing strain} refers to the force between the carbon atoms that prevents them from getting too close to each other. This imposes a preferred distance between each pair of bonded atoms. Second, \textbf{angle strain} corresponds to the carbon atom's four bonds trying to spread apart from each other. As mentioned before, tetrahedral angles are preferred. Finally, \textbf{steric crowding} is similar to eclipsing strain, but the eclipsing in this case is between the hydrogen atoms bonded to the carbons.
%
%\subsubsection{Configurations}
%
%Due to the aforementioned forces, there are a variety of frequently observed configurations with geometries that alleviate these strains to varying amounts.
%
%The lowest energy configuration is the \textbf{chair}. With the absence of both angle and eclipsing strain, the chair only has a small amount of steric strain.
%
%Another well studied configuration is the \textbf{boat}. With two sets of parallel carbons arranged in a planar rectangular structure, the remaining two carbon atoms sit at opposite ends slightly above the plain. The structure resembles the bow and stern of a boat. Despite having little angle strain, it does have some steric crowding between the hydrogen atoms at the ends of the boat as well as some eclipsing stain.
%
%
%
%\begin{figure}
%        %\centering
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_chair_1.png}
%%                %\caption{Chair}
%%                %\label{fig:Chair}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_boat_1.png}
%%                %\caption{Boat}
%%                %\label{fig:Boat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_boat_2.png}
%%                %\caption{Twist Boat}
%%                %\label{fig:TwistBoat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_chair_2.png}
%%                %\caption{Twist Chair}
%%                %\label{fig:TwistChair}
%%        \end{subfigure}%
%%
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_chair_2.png}
%%                %\caption{Chair}
%%                %\label{fig:Chair}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_boat_2.png}
%%                %\caption{Boat}
%%                %\label{fig:Boat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_boat_1.png}
%%                %\caption{Twist Boat}
%%                %\label{fig:TwistBoat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_chair_1.png}
%%                %\caption{Twist Chair}
%%                %\label{fig:TwistChair}
%%        \end{subfigure}%
%%
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_chair_3.png}
%%                \caption{Chair}
%%                \label{fig:Chair}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_boat_3.png}
%%                \caption{Boat}
%%                \label{fig:Boat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_boat_3.png}
%%                \caption{Twist Boat}
%%                \label{fig:TwistBoat}
%%        \end{subfigure}%
%%        ~ %add desired spacing between images, e. g. ~, \quad, \qquad etc.
%%          %(or a blank line to force the subfigure onto a new line)
%%        \begin{subfigure}[b]{0.24\textwidth}
%%                \includegraphics[width=\textwidth]{pv_twist_chair_3.png}
%%                \caption{Twist Chair}
%%                \label{fig:TwistChair}
%%        \end{subfigure}%
%%
%%
%%        \caption{Cyclohexane Configurations}\label{fig:animals}
%\end{figure}
%
%
%Besides the chair and boat, there are a few intermediate configurations that cyclohexane assumes as when it transitions between the chair and boat. The aptly named \textbf{twist boat} is similar to the boat, but some of the carbons are rotated from the rest of the molecule. Due to this twisting, the twist boat actually has a lower energy than the boat as the hydrogen atoms at the bow and stern are no longer aligned. Interestingly, the boat can transition via the twist boat to a second boat configuration in which the bow and stern atoms are different, but with an otherwise indistinguishable geometry. As part of the boat's transition to the chair, the \textbf{twist chair} is another distinguished configuration. Having large angle and eclipsing strains, the twist boat represents somewhat of an energy barrier between the chair and boat.
%
%
%\subsubsection{Finding Intermediate Coordinates}
%
%Since any configuration has an infinitude of equivalent configurations given by rotations and translations, we fix the first three atoms $v_1, v_2,v_3$, at positions $c_1, c_2, c_3$ that satisfy the length and angle constraints. These account for nine of the twenty-one total constraint equations.
%
%We wish to examine each of the four cyclohexane configurations to verify if they satisfy these constraints, but to do so, an explicit representation of the coordinates of each configuration is required. Since we know from Sachse's model that the boat and chair have a special polyhedral representation, geometry enables us to find such coordinates. We pick the $c_1 = \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right)$, $c_2 = \left(0,0,0\right)$, $c_3  = \left(\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right)$, and solve for the following coordinates.
%
%\begin{align*}
%v_1^{(chair)} &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) &= v_1^{(boat)} &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) \\
%v_2^{(chair)}  &= \left(0,0,0\right) &= v_2^{(boat)}  &= \left(0,0,0\right) \\
%v_3^{(chair)}  &= \left(\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) &= v_3^{(boat)}  &= \left(\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) \\
%v_4^{(chair)}  &= \left(\sqrt{\frac{2}{3}},\sqrt{\frac{2}{3}},2\sqrt{\frac{1}{3}}\right) &= v_4^{(boat)}  &= \left(\sqrt{\frac{2}{3}},\sqrt{\frac{2}{3}},2\sqrt{\frac{1}{3}}\right) \\
%v_5^{(chair)}  &= \left(0,\sqrt{\frac{2}{3}},\sqrt{3}\right) & v_5^{(boat)}  &= \left(0,\frac{5}{3}\sqrt{\frac{2}{3}},\frac{5}{3}\sqrt{3}\right) \\
%v_6^{(chair)}  &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) &= v_6^{(boat)}  &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right)
%\end{align*}
%%\begin{align*}
%%v_1^{(chair)} &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) & v_4^{(chair)}  &= \left(\sqrt{\frac{2}{3}},\sqrt{\frac{2}{3}},2\sqrt{\frac{1}{3}}\right) \\
%%v_2^{(chair)}  &= \left(0,0,0\right) & v_5^{(chair)}  &= \left(0,\sqrt{\frac{2}{3}},\sqrt{3}\right) \\
%%v_3^{(chair)}  &= \left(\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) & v_6^{(chair)}  &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) \\
%%v_1^{(boat)} &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) & v_4^{(boat)}  &= \left(\sqrt{\frac{2}{3}},\sqrt{\frac{2}{3}},2\sqrt{\frac{1}{3}}\right) \\
%%v_2^{(boat)}  &= \left(0,0,0\right) & v_5^{(boat)}  &= \left(0,\sqrt{\frac{2}{3}},\sqrt{3}\right) \\
%%v_3^{(boat)}  &= \left(\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right) & v_6^{(boat)}  &= \left(-\sqrt{\frac{2}{3}},0,\sqrt{\frac{1}{3}}\right)
%%\end{align*}
%
%It is easily verified that both the boat and chair coordinates satisfy all of the constraint equations exactly. As for the twist boat and twist chair, we do not have a precise definition of their coordinates other than they exist somewhere in transition between the boat and chair.
%
%\subsubsection{Dynamics}
%
%As we are interested in transitions between the four configurations, we can use our constraint model to examine such movements. Is it possible to start with the chair coordinates and continuously deform them to the boat coordinates without ever breaking any of the constraints? Is there a similar deformation between different boat configurations that satisfies the constraints? If so, how hard is it to find such a path?
%
%
%\subsubsection{Transitioning Between Boat Configurations}
%
%Under the conjecture that it is indeed possible to transition between two boat configurations, some numerical and visual experimentation was used to find coordinates to a second boat configuration, $\textbf{boat2}$. Even though it was easily verified that boat2 satisfies all of the constraints and was equivalent to the first boat by translation and rotation up to a permutation of the carbon atoms, it was not clear how to find a path between the two boats. Common in molecular dynamics simulations, a constrained dynamics SHAKE-type scheme was used to explore the admissible paths away from the boat configuration.
%
%The method is a two stage scheme in which we first step to a new configuration in the direction of boat2 and second enforce the constraints with Lagrange multipliers to get the updated configuration. For mathematical simplicity, we represent the coordinates $v_1,\dots,v_6$ as a single point $x \in \mathbbm{R}^{18}$ where $\left(v_j\right)_k = x_{3j+k}$.
%
%It is very important to make the estimate $\hat{x}^{n+1}$ of the next configuration satisfy the constraints reasonably well, as it will make the correction step easier. Rather than just defining the naive update of
%$$\hat{x}^{n+1} = x^n + \frac{1}{2}\left(\Delta t\right)^2\left(x^{boat2}-x^n\right),$$
%we seek a smarter method for making the estimate $\hat{x}^{n+1}$. Since we know the that the boat has one degree of freedom, its null space has one dimension. If we step in the direction of null-space $\nu^n \in \mathbbm{R}^{18}$, the constraints should still be close to being satisfied. Thus, we use the following estimate.
%$$\hat{x}^{n+1} = x^n + \frac{1}{2}\left(\Delta t\right)^2\left[\nu^n\cdot\left(x^{boat2}-x^n\right)\frac{\nu^n}{\|\nu^n\|}\right]$$
%To enforce our constraints on this prediction, we use Lagrange multipliers. We define the update to be
%$$x^{n+1} = \hat{x}^{n+1} + \left(\Delta t\right)^2\sum^{21}_{k=1}\lambda_k^{n+1}\nabla\varphi_k\left(x^n\right)$$
%and find $\lambda^{n+1}$ such that $x^{n+1}$ satisfies the constraint equations. To do this, we plug $x^{n+1}$ into the constraint equations $\varphi$ and use Newton iteration to find $\lambda^{n+1}$.
%
%
%\begin{figure}
%	%\includegraphics[width=\textwidth]{coords_v3.png}
%	\caption{Boat to Boat2 Coordinate Transition}
%	\label{fig:Coords}
%\end{figure}
%
%Using this scheme, we were able to simulate the transition between the boat and boat2 configurations. While maintaining the constraints up to arbitrary precision, the carbon coordinates $x$ were updated along a path that lead from the boat to boat2. This serves as confirmation that it is possible to maintain the constraints given by our model and transition between boat and twist boat configurations. The continuous change in carbon coordinates during this transition can be seen in figure~\ref{fig:Coords}.
%
%This algorithm also enabled us to approximate coordinates for the twist boat as they were taken to be midway between the boat and boat2. Also, by relaxing the constraints to only enforce fixed lengths between carbons and allowing some angle strain, a path from the Chair to the twist boat was found. Similarly, this path allowed for the approximation of the twist chair coordinates.
%
%\subsubsection{Analogy to Folding Model}
%Transition between the different configurations of cyclohexane can be compared to the transitioning within the folding model configuration space. Just as each folding intermediate is represented by a node in a graph, the Cyclohexane intermediates can similarly be organized. In both graphs an edge would mean that the intermediates at either end could transition into each other. As seen in figure~\ref{fig:cyclofold}, to transition between the chair and the boat, the molecule must first become a twist-chair and then a twist boat before it can finally become a boat. Similarly, to transition between the boat and boat2, the twist boat intermediate must first be visited. This relationship is mirrored by the boat and octahedron folding intermediates.
%
%\begin{figure}[h]
%	%\includegraphics[width=\textwidth]{cyclohexane_folding.png}
%	\caption{Configuration Graphs for Cyclohexane and Folding}
%	\label{fig:cyclofold}
%\end{figure}
\section{Brownian Motion on a Manifold}

To explore various properties of the Building Game geometric configuration spaces, we simulate a Brownian motion on the corresponding constraint space. Assuming that the constraint space has no singularities, it is a manifold. Brownian motion on a Riemannian manifold is defined as a diffusion process generated by $\Delta /2$ where $\Delta$ is the Laplace-Beltrami operator%~\cite{Hsu1988}
\begin{align}
\Delta &= g_{ij}\frac{\partial^2}{\partial x_i \partial x_j} - g_{ij}\Gamma^k_{ij}\frac{\partial}{\partial x_k}~\cite{Hsu1988}.
\end{align}
Since the geometric configuration space is defined implicitly by constrain functions, an analytic solution of the Brownian density function is not possible. Using a two step scheme, we use a random walk to approximate the Brownian motion. To compute each new random walk step, we first take a step in the tangent space and then project this point back to the manifold.

\subsection{Computational Scheme}

Suppose we have a $m$ dimensional manifold $\mathcal{M} \subset \mathbbm{R}^n$ defined implicitly by $\mathcal{M} \doteq \left\{z \in \mathbbm{R}^n : c\left(z\right) = 0\right\}$ for some function $c: \mathbbm{R}^n \to \mathbbm{R}^{n-m}$ with $c_1,\dots,c_{n-m}$ independent constraint functions. We seek to approximate a Brownian motion on $\mathcal{M}$ by constructing a random walk. As mentioned before, this is done in two stages; starting at a point $z \in \mathcal{M}$ we take a step of size $\sqrt{\Delta t}$ in a tangent direction $w$ and then we project the point $z +\sqrt{\Delta t} w$ back onto $\mathcal{M}$.

\subsubsection{Sampling in Tangent Space}

At each point $z \in \mathcal{M}$ the tangent space is $\mathcal{T}_z\mathcal{M} \doteq \left\{w \in \mathbbm{R}^n : C\left(z\right)w = 0\right\}$ where $C: \mathbbm{R}^n \to \mathbbm{R}^{(n-m)\times n}$ is the Jacobian of $c$. Thus, sampling from the tangent space reduces to sampling from the null space of $C(z)$.

First we must construct a basis for $\mathcal{T}_z\mathcal{M}$. Let $A = [C^T(z) B]$ be the concatenation of $C^T(z)$ and a randomly drawn matrix $B \in \mathbbm{R}^{n\times m}$. We assume each entry of $B$ is independent with $B_{jk} \sim  \text{unif}(0,1)$.
\begin{mylem}
The matrix $A$ is of full rank with probability 1.
\end{mylem}
\begin{proof}
Since the constraints of $c$ are independent, $C$ is of full rank. Additionally, since $B$ is drawn randomly, each column of $B$ will be independent of the columns of $C^T$  and also the other columns of $B$ almost surely. Thus, the columns of $A$ are independent and $A$ is of full rank.
\end{proof}

Now, we compute the QR decomposition
\begin{align}
        A = \left[C^T B\right] = QR = \left[Q^{(1)} Q^{(2)}\right] R
\end{align}
where $Q^{(1)} \in \mathbbm{R}^{(n-m)\times n}$ and $ Q^{(2)} \in \mathbbm{R}^{m\times n}$.
\begin{mythm}
\label{thm:Qbases}
The columns of $Q^{(2)}$ are an orthonormal basis for $\mathcal{T}_z\mathcal{M} = N(C)$ and the columns of $Q^{(1)}$ are an orthonormal basis for $(\mathcal{T}_z\mathcal{M})^\perp = (N(C))^\perp$.
\end{mythm}
\begin{proof}
The second claim is true by simple linear algebra arguments and the properties of the QR decomposition.
\begin{align}
        \text{col} (Q^{(1)}) &= \text{col}(C^T)\\
        &= ((\text{col}(C^T))^\perp)^\perp\\
        &= (N((C^T)^T)^\perp \\
        &= (N(C))^\perp
\end{align}
Here we use the vector space identities $X = (X^\perp)^\perp$ and $(\text{col} X)^\perp = N(X^T)$. This shows that columns of $Q^{(1)}$ are an orthonormal basis for $(\mathcal{T}_z\mathcal{M})^\perp$. Now, since
\begin{align}
        \text{col} (Q^{(1)}) \oplus \text{col} (Q^{(2)}) &= \text{col}(A) \\
        &= \mathbbm{R}^n \\
        & = (N(C)) \oplus (N(C))^\perp \\
        & = (N(C)) \oplus \text{col} (Q^{(1)})
\end{align}
 we must have that $\text{col}(Q^{(2)}) = N(C)$ and that the columns of $Q^{(2)}$ provide an orthonormal basis for $\mathcal{T}_z\mathcal{M}$.
\end{proof}
Now, with our orthonormal basis for $\mathcal{T}_z\mathcal{M}$, we can define any $w \in \mathcal{T}_z\mathcal{M}$ by a vector $\alpha \in\mathbbm{R}^m$ as follows.
$$w = \sum_{i=1}^m \alpha_iQ^{(2)}_i = Q^{(2)}\alpha$$
So, for any two $w,\tilde{w} \in \mathcal{T}_z\mathcal{M}$ we can define the manifold metric $g$ as
\begin{align}
        g(w,\tilde{w}) &= g\left(\sum_{i=1}^m \alpha_iQ^{(2)}_i, \sum_{j=1}^m \tilde{\alpha}_jQ^{(2)}_j\right) \\
        &= \sum_{i=1}^m\sum_{j=1}^m  \alpha_i \tilde{\alpha}_j g\left(Q^{(2)}_i, Q^{(2)}_j\right) \\
        &= \sum_{i=1}^m\sum_{j=1}^m  \alpha_i \tilde{\alpha}_j G_{ij} \\
        &= \alpha^T  G\tilde{\alpha}
\end{align}
Then, our goal is to sample uniformly from $\Omega_G = \left\{w = Q^{(2)}\alpha : \alpha^T G\alpha = 1\right\}$. Since this set corresponds to a level set of the Multivariate normal distribution with mean zero and covariance matrix $G^{-1}$, our sampling problem reduces to sampling $u \sim \mathcal{N}(0,G^{-1})$. Given $u$, $w \doteq \frac{u}{|u|}$ is sampled from $\Omega_G$ as desired. We generally choose $G = I$, but this is not required.

\subsubsection{Projection onto $\mathcal{M}$}
After making a step away from $z$ of size $\sqrt{\Delta t}$ in the direction $w \in N(C)$, our new point $z + \sqrt{\Delta t}w$ is not likely to be in $\mathcal{M}$. To return our point to $\mathcal{M}$, we make a projection step $w^\perp \in (N(C))^\perp$. Since $Q^{(1)}$ provides a basis for $(N(C))^\perp$, we can write any such step as
$$w^\perp = \sum_{i=1}^{n-m} \gamma_iQ^{(1)}_i = Q^{(1)}\gamma.$$
with $\gamma \in \mathbbm{R}^{n-m}$. Thus, we want to identify a $\gamma$ such that $c(z + \sqrt{\Delta t} w + Q^{(1)}\gamma) = 0$. Since $c$ is nonlinear, we use Newton-Raphson iteration to solve for $\gamma$. By defining an objective function $F$ and its Jacobian $\mathcal{J}$ as
\begin{align}
        F(\gamma) &= c(z + \sqrt{\Delta t} w + Q^{(1)}\gamma) \\
        \mathcal{J}_{jk}(\gamma) &= \frac{\partial F_j}{\partial \gamma_k}(\gamma) \\
        &= \sum_{i=1}^{n-m} \frac{\partial c_j}{\partial z_i}(z + \sqrt{\Delta t} w + Q^{(1)}\gamma)Q^{(1)}_{ik} \\
        \mathcal{J}(\gamma) &= C(z + \sqrt{\Delta t} w + Q^{(1)}\gamma)Q^{(1)}
\end{align}
we arrive at the iteration routine below, which typically commences with the initial guess $\gamma_0 = 0$.
\begin{align}
        C(z + \sqrt{\Delta t} w + Q^{(1)}\gamma)Q^{(1)}\left(\gamma_{k+1} - \gamma_{k}\right) &= \mathcal{J}(\gamma_k)\left(\gamma_{k+1} - \gamma_{k}\right)\\
         &= -F(\gamma_k) \\
         &= -c(z + \sqrt{\Delta t} w + Q^{(1)}\gamma)
\end{align}

\subsubsection{Sampling Algorithm}

\begin{figure}[ht]
\centering
\begin{algorithmic}
\For{$k=1,\dots,N$}
        \State $B \gets \text{unif}(0,1)^{n\times m}$
        \State $A \gets \left[C^T B\right]$
        \State $[Q^{(1)}, Q^{(2)}], R \gets$QRdecomposition$(A)$
        \State $\alpha \gets \mathcal{N}(0,G^{-1})$
        \State $w \gets \frac{Q^{(2)}\alpha}{|Q^{(2)}\alpha|}$
        \State $\gamma \gets 0^{n-m}$
        \While{$|c(z + \sqrt{\Delta t}w + Q^{(1)}\gamma)| > \epsilon$}
                     \State $\Delta\gamma \gets (C(z + \sqrt{\Delta t} w + Q^{(1)}\gamma)Q^{(1)})^{-1}(-c(z + \sqrt{\Delta t} w + Q^{(1)}\gamma))$
                     \State $\gamma \gets \gamma + \Delta\gamma$
        \EndWhile
        \State $z \gets z + \sqrt{\Delta t}w + Q^{(1)}\gamma$
\EndFor
\end{algorithmic}
\caption{Random Walk algorithm on $\mathcal{M}$.}
\label{alg:MBMRW}
\end{figure}

%\begin{figure}[ht]
%\centering
%\begin{algorithmic}
%\end{algorithmic}
%\caption{Algorithm for generating new configuration sample.}
%\label{alg:MBM}
%\end{figure}

\subsection{Validation and Test Cases}

An interesting test of our sampling scheme is unitary matrices. Since unitary matrices are complex matrices that satisfy $U^*U = UU^* = I$, we can use a system constraints to represent the space of unitary $N \times N$ matrices as an algebraic variety sitting in a $n = 2N^2$ dimensional ambient space. If we use the coordinates $U = A + iB$ for real, $N \times N$ matrices $A$ and $B$. We use the constraints given by the following.
\begin{align}
I &= U^*U \\
&= (A^T - iB^T)(A + iB) \\
&= (A^TA + B^TB) + i(A^TB + B^TA) \\
\mathbbm{1}_{j=k} &= A_j\cdot A_k + B_j\cdot B_k \\
0 &= A_j\cdot B_k + A_k \cdot B_j
\end{align}
Considering each $1 \leq j \leq N$ gives us $2*(N + \frac{1}{2}N(N-1)) = N + N^2$ independent polynomial constraint equations.

Using our random walk scheme to simulate a Brownian motion of the constraint space. Considering first the case of $N = 3$, the ambient space is $\mathbbm{R}^{18}$. The resulting constraint manifold has dimension $6$ with the co-dimension $12$ corresponding to the $12 = 3 + 3^2$ independent constraints imposed.

\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6, angle=0]{images/unitary_3_hist.eps}
\caption{Empirical distribution of $3\times 3$ Unitary matrices is uniform.}
\label{fig:Uny3}
\end{figure}
%Mezzadri outlines properties of random unitary matrices~\cite{Mezzadri2007}. In particular,

A uniform distribution according to the Haar measure corresponds to a uniform sampling of of the arguments $\theta$ of the matrices eigenvalues~\cite{Mezzadri2007}. Using our scheme, $3,000,000$ samples were drawn for both $N = 3$ and $N = 10$. Figures~\ref{fig:Uny3} and~\ref{fig:Uny10} show the eigenvalue distribution of our sampled matrices to be uniform.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6, angle=0]{images/unitary_10_hist.eps}
\caption{Empirical distribution of $10\times 10$ Unitary matrices is uniform.}
\label{fig:Uny10}
\end{figure}

%--ellipsoid examples.?

\subsection{Brownian Motion on Geometric Configuration Spaces}
\label{ssc:MBMGCS}
As an initial test case of our Brownian Motion scheme on Building Game geometric configuration spaces, we first consider two simple triangle linkages. The first is a dimer composed of two triangles linked together along a hinged edge. As seen in figure~\ref{fig:T2_diagram}, the linkage can be specified by the $3$-dimensional locations of four vertices, giving an ambient space of $\mathbbm{R}^3$. In addition to the six trivial degrees of freedom, this configuration also carries an internal degree of freedom corresponding to the dihedral angle formed at the hinged edge.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.1]{images/T2_diagram.eps}
\caption{Two triangle linkage.}
\label{fig:T2_diagram}
\end{figure}
Figure~\ref{fig:T2_1} shows a histogram of this dihedral angle as sampled using out random walk scheme. Interestingly, the distribution is not uniform and appears to be of the form $\alpha_0 + \alpha_1\cos(\theta) + \alpha_2\cos(2\theta)$.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T2_1.eps}
\caption{Histogram of the dihedral angle in sampled configurations of a two triangle linkage. The red curve has the form $\alpha_0 + \alpha_1\cos(\theta) + \alpha_2\cos(2\theta)$.}
\label{fig:T2_1}
\end{figure}

A second test case we consider is a linkage with three triangles. As depicted in figure~\ref{fig:T3_diagram}, the configuration is composed of an interior triangle connected along two of its edges to an additional two triangles and is parameterized by five vertices.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.1]{images/T3_diagram.eps}
\caption{Three triangle linkage.}
\label{fig:T3_diagram}
\end{figure}
This configuration has two internal degrees of freedom, which are represented my the two dihedral angles. Figure~\ref{fig:T3_1} is a $2$-dimensional histogram of the dihedral angles sampled by our scheme.
%As expected, the two dihedral angles are independent in the sense that individually their distributions look like that of the two triangle linkage.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T3_1_2D.eps}
\caption{Histogram of the dihedral angle in sampled configurations of a three triangle linkage.}
\label{fig:T3_1}
\end{figure}
As a test of convergence for our scheme, figure~\ref{fig:KSD1} show the Kolmogorov-Smirnov distance of the empirical distribution on the first dihedral angle of the three triangle linkage as a function of the number of samples. The line of best fit has a slope of $-0.461$. This is nearly $-0.5$ indicating that the expected square root rate of convergence is close to being achieved.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/KSD1.eps}
\caption{Convergence of the sampling scheme for the three-triangle linkage.}
\label{fig:KSD1}
\end{figure}

\subsubsection{Fixing Trivial Degrees of Freedom}
%From the two and three triangle linkage examples, we see that the sampling scheme does not sample the dihedral angles of the configuration uniformly. This was unexpected, but possibly explained by the choice to not explicitly mod out the trivial rotational and translational degrees of freedom.

Since we are only interested in internal degrees of freedom and statistics invariant to rigid body movement, the inclusion of the six trivial degrees of freedom in our simulations may not be necessary. In fact, if the geometric configuration space is reduced by six dimensions, sampling efficiency will improve.

%The first apparent way of fixing the trivial degrees of freedom is to fix an individual face at a specific location. Without letting this face translate or rotate, we are also preventing these trivial degrees of freedom from entering the entire configuration. As seen in figure~\ref{fig:T2_3}, fixing of one of the faces in a two triangle linkage will give the dihedral angle a uniform distribution as hoped.
%\begin{figure}[ht]
%\centering
%  \includegraphics[scale=0.6]{images/T2_3.eps}
%\caption{Two triangle linkage with one face fixed.}
%\label{fig:T2_3}
%\end{figure}
%However, when we consider the three triangle linkage, there are two choices we have; we can either fix on of the outside triangles or we can fix the interior face. As seen in figure~\ref{fig:T3_3}, if we fix the interior triangle, the distribution we observe will be uniform. However, if we choose to fix an exterior triangle, the dihedral angle between the fixed triangle and the interior triangle  will be uniformly sampled, but the other dihedral will not have a uniform distribution. Seen in figure~\ref{fig:T3_4}, the distribution of this second dihedral angle appears to be the sum of trigonometric functions.
%\begin{figure}[ht]
%\centering
%  \includegraphics[scale=0.6]{images/T3_3_2D.eps}
%\caption{Three triangle linkage with interior triangle fixed.}
%\label{fig:T3_3}
%  \includegraphics[scale=0.6]{images/T3_4_2D.eps}
%\caption{Three triangle linkage with exterior triangle fixed.}
%\label{fig:T3_4}
%\end{figure}
%This poses a bit of a problem. If the choice of face to fix affects the resulting distribution, then we must have justification for selecting one face over another. Since a clear reasoning for this choice is not apparent--especially in the case of an arbitrary linkage--we look to other methods for fixing the trivial degrees of freedom that the system has.

As mentioned in section~\ref{ssc:RemTrivDoF}, one way of fixing the translational degrees of freedom is to fix the center of mass of the configuration. While this method does not address the rotational degrees of freedom, it does appear to maintain the same distribution on the internal modes. In fact, the Kolmogorov-Smirnov distance between the sampled points for the first dihedral angle in figures~\ref{fig:T3_1} and~\ref{fig:T3_5} is just $0.00296$ indicating an excellent agreement. Thus, it remains to account for the rotational degrees of freedom separately.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T2_4.eps}
\caption{Sampling of two triangle linkage with fixed center of mass. The red curve has the form $\alpha_0 + \alpha_1\cos(\theta) + \alpha_2\cos(2\theta)$.}
\label{fig:T2_4}
\end{figure}

\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T3_5_2D.eps}
\caption{Sampling of three triangle linkage with fixed center of mass.}
\label{fig:T3_5}
\end{figure}

Now, to address rotations, we seek to find the three dimensional subspace of the tangent space and restrict our random walk to the space orthogonal to these rotational directions. Since such a step in the tangent space is then projected to the manifold, some rotation may occur in the projection step. To account for this, we must add a correction step that rotates the configuration back to its original frame of reference.

We treat our constraint space as the product of the special orthogonal group (rotations) and an algebraic variety corresponding to the remaining degrees of freedom $\mathcal{M} = SO(3) \times \tilde{\mathcal{M}}$. For every non-singular $z \in \mathcal{M}$ we notice that there is a local diffeomorphism from $\mathcal{M}$ to $SO(3) \times \tilde{\mathcal{M}}$. Using this knowledge, we aim to find local coordinates $Q \in SO(3)$ and $\tilde{z} \in \tilde{\mathcal{M}}$ such that the tangent space $\mathcal{T}_z\mathcal{M} = \mathcal{T}_QSO(3) \times \mathcal{T}_{\tilde{z}}\tilde{\mathcal{M}}$. Since we already have a method for deriving $\mathcal{T}_z\mathcal{M}$, the goal now is to decompose this tangent space into the two orthogonal subspaces corresponding to the rotational and internal degrees of freedom.

Since $z = (v^1, v^2, \dots, v^N)$ for $v^i \in \mathbbm{R}^3$, the rotation by a $Q \in SO(3)$ is really the vertex-wise rotation $\bar{Q}z = (Qv^1, Qv^2, \dots, Qv^N)$. In the case of $Q = I$ the tangent directions are generated by the skew-symmetric matrices. A basis for the skew-symmetric matrices is given by
\begin{align}
\textswab{e}_1 &= \begin{pmatrix} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 0  \end{pmatrix} \\
\textswab{e}_2 &= \begin{pmatrix} 0 & 0 & 1 \\ 0 & 0 & 0 \\ -1 & 0 & 0  \end{pmatrix} \\
\textswab{e}_3 &= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 1 \\ 0 & -1 & 0  \end{pmatrix}.
\end{align}
Thus, in the case of $Q=I$ we can represent the rotational directions of the tangent space as $\mathcal{T}_ISO(3) = \text{span}(w_1, w_2, w_3)$ where the bases $w_1, w_2, w_3$ are defined by
\begin{align}
w_1 &= (\textswab{e}_1v^1, \textswab{e}_1v^2, \dots, \textswab{e}_1v^N) \\
w_2 &= (\textswab{e}_2v^1, \textswab{e}_2v^2, \dots, \textswab{e}_2v^N) \\
w_3 &= (\textswab{e}_3v^1, \textswab{e}_3v^2, \dots, \textswab{e}_3v^N).
\end{align}

With this formulation, we can alter our method for finding the bases of the tangent space from theorem~\ref{thm:Qbases} to get the decomposition we desire. Specifically, setting
$$\tilde{A} = \left[C^T(z) (w_1, w_2, w_3) \tilde{B} \right]$$
where $B \in \mathbbm{R}^{n\times (m-3)}$ is again random and taking the QR decomposition will give us
$$\tilde{A} = \left[Q^{(1)} Q^{(2)}_{rot} Q^{(2)}_{int}\right]R.$$
As before, $Q^{(2)} = \left[Q^{(2)}_{rot} Q^{(2)}_{int} \right]$ is a basis for $\mathcal{T}_z\mathcal{M}$, but we will also have $Q^{(2)}_{rot}$ as a basis for the rotational directions $\mathcal{T}_ISO(3)$ and $Q^{(2)}_{int}$ as a basis for the remaining internal tangent directions $\mathcal{T}_{\tilde{z}}\tilde{\mathcal{M}}$. Thus by modifying our
random walk routine to sample from the directions of $Q_{int}^{(2)}$, we will fix the rotation of the proposed step in the tangent space.

Two issues must still be addressed. First, we have assumed that that the local rotation coordinate $Q$ is the identity. Additionally, even though our step in tangent space does not contain any of the rotational directions, when it is projected back to the constraint space, some rotation may occur. We address these two issues simultaneously in the form of a correction step after the projection has been made. To do this, the configuration is simply rotated back to its original frame of reference using the Kabsch algorithm which finds the optimal rotation by which to match two sets of points in three dimensions~\cite{Kabsch}. The implicit assumption here is that if we have bad a sufficiently small random walk step, the resulting configuration will be close to the one at the previous step. Moreover, if $Q=I$ at the previous step, this rotation will bring the configuration back to the same frame of reference.

Figure~\ref{fig:T2_5} shows the results for a sampling of configurations for the two triangle linkage. The distribution of dihedral angle takes the form of a cosine function which differs from the method which does not fix rotations.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T2_5.eps}
\caption{Sampling of two triangle linkage with fixed center of mass and rotations. The green curve has the form $\alpha_0 + \alpha_1\cos(\theta)$.}
\label{fig:T2_5}
\end{figure}
Similarly, the three triangle linkage also produces a different distribution on dihedral angles. As seen in figure~\ref{fig:T3_6}, the distribution is not the simple product of one-dimensional distributions. There are local maxima at $(0,0), (\frac{\pi}{3}, \frac{5\pi}{3})$, and $(\frac{5\pi}{3}, \frac{\pi}{3})$ With a local minima at $(\pi,\pi)$.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T3_6_2D.eps}
\caption{Sampling of three triangle linkage with fixed center of mass and rotations.}
\label{fig:T3_6}
\end{figure}

Since the fixing of rotation alters our sampling distribution, this method is not optimal. An approach that may be considered in the future is to use the quotient metric for $g$ as was similarly used in~\cite{Holmes-Cerfon2013}.

\section{Reflected Brownian Motion on a Manifold}
%In our sampling of Building Game geometric configurations, we see that many of the sampled configurations or Brownian motion paths do not make physical sense. For example, we do not prevent the triangles of the configuration from intersecting each other. Additionally, two faces attached by a hinge may pass through each other as the dihedral angle passes through zero. We introduce implicit boundaries to turn the Brownian motion into a Reflected Brownian motion on. With these added boundaries, the process will be prevented from entering nonphysical configurations, and represents a better sampling on configurations that coincide with the actual self-assembly processes we are attempting to model.

In our sampling of Building Game geometric configurations, we see that many of the sampled configurations of Brownian motion paths are self-intersecting. These configuration are nonphysical and can be avoided by using reflected Brownian motion.

\subsection{Computational Scheme}

To sample from a reflected Brownian motion on the manifold, we use a rejection scheme that first generates a proposal configuration according to the previous random walk scheme. Using our implicit boundary function that simply returns true if a proposed configuration is valid (not self-intersecting), and false otherwise, we accept or reject the proposed configuration. This process repeats until a proposal is accepted.

%\begin{figure}[ht]
%\centering
%\begin{algorithmic}
%\Do
%\State $\hat{z} \gets$ proposal configuration using algorithm~\ref{alg:MBMRW}.
%\doWhile{$\hat{z}$ is self-intersecting}
%\State $z \gets \hat{z}$
%\end{algorithmic}
%\caption{Reflected Random Walk on $\mathcal{M}$}
%\end{figure}

\subsubsection{Testing for Self-Intersection}

In order to compute our implicit self-intersection function at each step, we must ensure that no two triangles intersect. Thus, for an intermediate $[x]$, each function call requires that $\frac{1}{2}|x|(|x| - 1)$ pairs of triangle be tested for intersection. For the scheme to remain computationally feasible, it is imperative that these comparisons are carried out as efficiently as possible. We use an algorithm presented by M\"oller, which has been shown to be effective ~\cite{Möller97afast}.

%\begin{figure}[ht]
%\centering
%\begin{algorithmic}
%\State HI
%\end{algorithmic}
%\caption{Self Intersection Boundary}
%\end{figure}

\subsection{Validation and Test Cases}

One way to test whether our rejection scheme has the properties that coincide with a reflected Brownian motion is to look at its distribution after a finite amount of time rather than only looking at long time statistics. In figure~\ref{fig:HCFin} we consider the case of a $d$-dimensional hyper-cube for $d = 2, 3, 4, 5$  and compare the distribution after a finite simulation time with the exact solution.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.4, angle=0]{images/rejection_finite.eps}
\caption{Finite time distribution of first dimension of rejection sampled points in hyper-cubes of increasing dimension across different choices of timestep.}
\label{fig:HCFin}
\end{figure}
The results match almost perfectly with the biggest errors occurring at the boundaries. This is to be expected since the rejection method biases the sampling slightly to points away from the boundary.

\subsection{Reflected Brownian Motion on Geometric Configuration Spaces}
Now, we again consider the two and three triangle linkage examples from section~\ref{ssc:MBMGCS}. Figure~\ref{fig:T2_2} depicts the same conditions (no fixed trivial degrees of freedom) as figure~\ref{fig:T2_1} previously, but with the addition of boundaries that prevent the two triangles from rotating through each other. The agreement in the two histograms is strong with the greatest differences being at the boundary as expected.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T2_2.eps}
\caption{Sampling of two triangle linkage with boundaries. The red curve has the form $\alpha_0 + \alpha_1\cos(\theta) + \alpha_2\cos(2\theta)$.}
\label{fig:T2_2}
\end{figure}

Similarly, we consider the three triangle linkage with no fixed degrees of freedom and impose boundaries. This test is less trivial than the two triangle linkage since the exterior triangles of the linkage may interact more easily. Comparing the original in figure~\ref{fig:T3_1}, there is nice agreement except in the regions in the lower-right and upper-left corners of the plot which correspond to the aforementioned triangle intersection. Also, a noticeable layer of lower probability is seen around the periphery of the plot which reflects the boundary preventing the triangles from rotating through each other.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T3_2_2D.eps}
\caption{Sampling of three triangle linkage with boundaries.}
\label{fig:T3_2}
\end{figure}

Again, we test the convergence for our scheme, using show the Kolmogorov-Smirnov distance of the empirical distribution on the first dihedral angle of the three triangle linkage with boundaries as a function of the number of samples. As seen in figure~\ref{fig:KSD2} the line of best fit has a slope of $-0.449$ which is again close to square root convergence.
\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/KSD2.eps}
\caption{Convergence of the sampling scheme for the three-triangle linkage with self-intersection boundaries.}
\label{fig:KSD2}
\end{figure}

\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T2_6.eps}
\caption{Sampling of two triangle linkage with boundaries and fixed center of mass and rotations. The green curve has the form $\alpha_0 + \alpha_1\cos(\theta)$.}
\label{fig:T2_6}
\end{figure}

\begin{figure}[ht]
\centering
  \includegraphics[scale=0.6]{images/T3_7_2D.eps}
\caption{Sampling of three triangle linkage with boundaries and fixed center of mass and rotations.}
\label{fig:T3_7}
\end{figure}

Now we compare the analogous cases in which we fixed all of the trivial degrees of freedom with and without a boundary. Figure~\ref{fig:T2_6} is the boundaried version of figure~\ref{fig:T2_5} and figure~\ref{fig:T3_7} is the boundaried version of figure~\ref{fig:T3_6}. Both cases exhibit a close comparison to their counterparts with the regions near the boundary possessing the largest deviations.

\section{Computational Implementation}

As with our enumeration work, simulations were carried out on a desktop computer running 64-bit Ubuntu 14.04 LTS with 15.6 GiB memory and a quad-core 3.20GHz Intel processor. The module for simulating the Brownian motion on a manifold was implemented in Python and was able to sample at a rate on the order of $3,000,000$ samples per hour. This rate varies with use of a boundary and the dimension of the manifold and ambient spaces as well.