\section*{Materials and Methods}
\label{sec:M&M}

\subsection*{Data Provenance}

\subsubsection*{Growth Data}
The first data set is an inventory of all trees over 10cm in Diameter at Breast Height (DBH), i.e. measured at 1.3m high, in nine 1-ha plots in French Guiana (see map~\autoref{fig:map}). In each plot, tree diameters were measured every two or five years depending on the plot.

We selected a common measured period between 2001 and 2013 comprising a total of 3549 trees; we estimated annual growth rate (AGR) in diameter by fitting a linear regression of DBH over years. The slope of the regression gave us an average AGR for each followed tree on the comprised

\subsubsection*{Trait Data}

The second data set was a collection of five functional traits (see~\autoref{tab:seltraits}) extracted from a bigger database \citep{baraloto_decoupled_2010} on the same trees. Selected traits can be seen in two orthogonal axes of variations: leaf and stem economics spectra, contrasting different ecological strategies~\citep{westoby_leaf-height-seed_1998, baraloto_decoupled_2010}.

\textbf{Leaf economics spectrum.} Specific Leaf Area (SLA) is the photo-sensitive area per unit of dry mass of the leaf; high SLA underlines investment on high light-capturing leaves that have a short payback time per gram of dry matter invested; while low SLA reflects strategies with less light-capturing leaves and longer payback time that may appear competitive in some conditions. Total leaf chlorophyll content reflects the global strategy of the plant of having resource-expansive leaves with high payback or resource-cheap leaves with lower payback~\citep{coste_assessing_2010}. Laminar toughness measures the resistance of a leaf to pinching, high toughness values correlates with low herbivory rate, it correlates with defense strategy~\citep{westoby_leaf-height-seed_1998}.

\textbf{Stem economics spectrum.} Wood density underlines different ecological strategy for trees, a low wood density makes wood less stable and less better protected against herbivory but cheap volumetric construction cost because of low resource requirements; while a high wood density makes the tree more stable but with higher construction cost, meaning a lower growth~\citep{chave_towards_2009}. Trunk bark thickness associate with defense strategies in neotropical forests, thicker bark provides higher resistance to pathogens and herbivores~\citep{paine_functional_2010}.

\subsection*{Statistical analyses}

\subsubsection*{How are intra-specific variabilities structured compared to inter-specific variabilities?}

To understand how inter-specific variability contrasted with intra-specific variability, we partitioned the variance of each trait and performance using ANalyses Of VAriances (ANOVAs) with a species term as follow:
\begin{subequations}
	\begin{align}
	\label{eq:anovatrait}
	\text{Tr}_{s, i} &= \mu_s + \epsilon_i\\
	\label{eq:anovaagr}
	\text{AGR}_{s, i} &= \overline{\text{AGR}_{s}} + \epsilon'_i,
	\end{align}
\end{subequations}
with $\text{Tr}_{s, i}$, the trait of individual $i$ of species $s$; $\mu_s$ the mean trait of species $s$; $\epsilon_i$ the individual trait error term with a Gaussian distribution; $\text{AGR}_{s, i}$ the AGR of individual $i$ of species $s$; $\overline{\text{AGR}_{s}}$ the average AGR of species $s$; $\epsilon'_i$ the individual AGR error term and $\epsilon_i \sim \mathcal{N}(0, \sigma^2), \epsilon'_i \sim \mathcal{N}(0, \sigma'^2)$. The explained variance by the species effect can then be expressed by the proportion of group sum of squares over the total sum of squares. We considered the residual sum of squares as being the individual variance plus white noise. We partitioned the variance similarly for AGR.

\subsubsection*{How does trait intra-specific variability influence radial growth?}

In order to understand the influence of trait intra-specific variability on radial growth, we modeled AGR with a mixed linear model of traits and other factors. Based on~\citet{herault_functional_2011} ontogenical model, we used fixed terms $DBH$ and $\log(DBH)$, because they capture well the hump shape of growth during ontogeny for tropical trees. As they did, we modeled $\log(AGR + 1)$ because of data high heteroscedasticity. We added a random intercept for species effect to take inter-specific variability into account, this effect also influenced the slopes of $DBH$ and $\log(DBH)$ terms. We also added a random intercept plot effect to take inter-plot variability into account. Then depending on the hypothesis tested various fixed terms were added to the model:

\begin{align}
	\label{eq:growth_mod}
	\log(\text{AGR}_{p, s, i} + 1) =& \prescript{0}{}\theta + \prescript{0}{}\gamma_s + \gamma_p \nonumber \\
		&+ (\prescript{1}{}\theta + \prescript{1}{}\gamma_s) \times \text{DBH} \nonumber \\
		&+ (\prescript{2}{}\theta + \prescript{2}{}\gamma_s) \times \log(\text{DBH}) \nonumber \\
		&+ \delta + \epsilon_i,
\end{align}
with $\epsilon_i \sim \mathcal{N}(0, \prescript{3}{}\theta)$ the individual residual,
where $\text{AGR}_{p, s, i}$ is the AGR of tree $i$ of species $s$ in plot $p$; $\prescript{0}{}\theta \dots \prescript{3}{}\theta$ are parameters to be estimated; $\prescript{0}{}\gamma_{s} \dots \prescript{2}{}\gamma_s$ and $\gamma_p$ follow a zero-centered Gaussian distribution with unknown variances $\prescript{0}{}\sigma^2_s \dots \prescript{2}{}\sigma^2_s$ and $\sigma^2_p$. $\text{Tr}_s$ is the average trait value for species $s$.

We tested different hypotheses through the $\delta$ term in~\autoref{eq:growth_mod}. If we position an individual relative to its species mean, we can compare them on a hierarchy of traits, and the relevant variable is the hierarchical distance between individual trait and species average trait. We may also hypothesized as~\citet{kunstler_competitive_2012} did for competition, that the relevant relation for AGR would be the absolute distance. We obtain two models that have to be compared with models having only the specific trait or the individual trait.
\begin{equation}
\label{eq:delta}
\delta = \left\{
	\begin{array}{lll}
		\prescript{4}{}\theta\times \text{Tr}_s & \text{Species Average} &  \\
		\prescript{4}{}\theta \times \text{Tr}_s & + \prescript{5}{}\theta \times(\text{Tr}_i - \text{Tr}_s) & \text{Hierarchical Dist.} \\
		\prescript{4}{}\theta \times \text{Tr}_s & + \prescript{5}{}\theta' \times \vert \text{Tr}_i - \text{Tr}_s \vert & \text{Absolute Dist.} \\
		\prescript{4}{}\theta' \times \text{Tr}_i & \text{Individual Trait} & \\
	\end{array}
\right.
\end{equation}

 $\text{Tr}_s$ the species average value, with a parameter $\prescript{4}{}\theta$; $\text{Tr}_i - \text{Tr}_s$ the hierarchical distance of individual trait value $\text{Tr}_i$ to species average trait value $\text{Tr}_s$; or $\vert \text{Tr}_i - \text{Tr}_s \vert$ the absolute distance to species average trait; and $\text{Tr}_i$ the individual trait value.

\subsubsection*{Intra-specific variability vs. inter-specific variability effects on performance for each trait}

Intra-specific variability and inter-specific variaibilty may have different impacts on performance. For each trait we selected the growth model from~\autoref{eq:delta} that had the highest adapted R-squared for mixed models~\citep{nakagawa_general_2013}, i.e. the model that best described our data. Using them we predicted AGR based on a 5\%-95\% range of species average and hierarchical distance.

\subsection*{Data analysis}

All data analyses were made using \texttt{R} \citep{R_language} version 3.2.0 (2015-04-16), plots were made with \texttt{ggplot2} \citep{ggplot2_pkg}. We fit mixed-models with \texttt{lme4} \texttt{R} package \citep{lme4_pkg} 1.1-7 and computed adapted R-squared for mixed-models \citep{nakagawa_general_2013} implemented in \texttt{MuMIn} \texttt{R} package \citep{mumin_pkg} version 1.13.4.

\begin{table*}
	\begin{center}
		\begin{tabular}{lccc}
		\hline
		Trait Name & Units & Role \\
		\hline
		Trunk bark thickness & mm & Stem economics \\
		Xylem density (wood density) & $\text{g}.\text{cm}^{-3}$ & Stem economics \\
		Specific Leaf Area (SLA) & $\text{cm}^2.\text{g}^{-1}$ & Leaf economics \\
		Laminar total chlorophyll & $\text{µm}.\text{mm}^{-2}$  & Leaf economics \\
		Laminar toughness & N & Leaf economics \\
		\hline
		\end{tabular}
		\caption{\textbf{Selected functional traits.} Stem and Leaf Economics Spectrum are defined as in~\citep{baraloto_decoupled_2010}, the two axes unravel distinct ecological strategies. Leaf economics spectrum contrasts}
		\label{tab:seltraits}
	\end{center}
\end{table*}


%\begin{center}SECTION\end{center}

\chapter{CONCLUSIONS}

\section{CHARACTERIZATION OF TRANSPORT REGIME TRANSITIONS IN NON-CONSERVATIVE RANDOM MEDIA}

The purpose of the first portion of this dissertation was to characterize the effect of absorption and gain in ballistic, diffusive, and localized transport regimes. Particle and wave-based transport models were studied in one dimension (1D) and quasi-1D geometries.

An investigation of the ratio of transmission to energy in the system, $T/{\cal E}$, was performed in 1D using theoretical and numerical methods~\cite{2010_Payne_TE}. %~(c.f.~Section \ref{sec:TE2009}).
The numerical model uses the transfer matrix method~\cite{1981_MacKinnon_scaling} with self-embedding~\cite{1999_yamilov_selfembed} to simulate layers of dielectric material with random widths. Since diffusion cannot occur in 1D, the response of the parameter $T/{\cal E}$ in the regime of Anderson localization when gain is present was found. A decrease of $T/{\cal E}$ from the value given by the classical un-renormalized diffusion coefficient may be attributed to wave-interference localization effects. Although $T/{\cal E}$ does not diverge as the random lasing threshold is approached, there is a dependence on the position of the center of localization. This position dependence is closely related to the existance of necklace states~\cite{1987_Pendry}.

To investigate the transition from diffusion to Anderson localization, the previous numerical model was extended to quasi-1D geometry. To guide our efforts we developed a phase space diagram with 15 different transport regimes~\cite{2010_Yamilov_Regimes}. A way of characterizing which regime a given system is in was clearly needed. In the process of using the numerical model in this geometry, we showed that evanescant channels do not need to be included in simulations of passive media~\cite{2010_Payne_closed}. The effect of evanescant channels is renormalize the transport mean free path while conforming to single parameter scaling.

A parameter related to $T/{\cal E}$, the position dependent diffusion coefficient $D(z)$, was investigated for use in characterizing the multiple transport regimes in quasi-1D non-conservative random media~\cite{2010_Payne_PRL}. Our results indicate that $D(z)$ may serve as a useful criterion for the enumerated transport regimes.

\section{ANALYSIS OF DETERMINISTIC APERIODIC STRUCTURES}

Although random media produces novel features, these systems lack easy reproducibility. For applications such as photonic integrated circuits we are interested in novel features unavailable to periodic media while maintaining reproducibility. Thus media correlated disorder is a natural avenue of pursuit. The numerical model used in this dissertation can simulate any arrangement of scatters, making it amenable to aperiodic media. %The same simulation tool used for random media was applied to aperiodic media.

In the studies of deterministic aperiodic structures (DAS) we focused on the Thue-Morse pattern. This generation algorithm yeilds a singular continuous Fourier transform spectrum with self-similar features. We demonstrated the possibility of mapping the array of micro-cavities in the two dimensional (2D) Thue Morse DAS onto a periodic square lattice~\cite{2012_Payne_Mapping_2D_TM}. Such mapping allowed us to uniquely identify and enumerate the configurations of nearest and next-nearest neighbors. Thus the original aperiodic structure is reduced to the periodic structure with aperiodic arrangement of the limited set of pairings.

Once this step was completed, we demonstrated the applicability of the tight binding approach in a deterministic aperiodic array of photonic micro-cavities. Under realistic conditions, we observed hybridization of the modes of individual micro-cavities into the eigenstates of the entire array. Our work adds the tight binding approach to the arsenal of theoretical tools for studying of 2D~Thue-Morse structures as well as for design and analysis of experiments.

The tight binding model allows us to investigate the size scaling of the density of the optical states in large arrays of optical micro-cavities; monitor the evolution of the spectra; and to study spatial properties of the eigenstates via e.g. the inverse participation ratio. The inverse participation ratio shows coexistence of localized and extended states in the same spectral regions. Some of the extended states have nearly constant intensity across the entire sample. This property makes the considered system extremely promising for practical applications in optical control of light propagation via e.g. wave-front shaping.

%\section{TEST OF BORDERS}

%Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum. Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis par turient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum. Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis par turient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum. Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis par turient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum. Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis par turient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.


\documentclass{article}

\setlength{\topmargin}{-0.75in}
\setlength{\textheight}{9in}
\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}

\begin{document}

\title{Energy Prediction and Adaptation in flux-e}
\author{Jacob Sorber\\Matt Brennan}
\maketitle

\section{Goal and Premise}
Given an energy buffer and source, our obvious goal is to never deplete our resources. Additionally, we want our energy state to remain as stable as possible, as this will give the user the most predictable system response. \\
We assume that there is periodicity in both the system load and the energy harvesting.  We divide this period in to epochs, and make our state adjustments on these boundaries.

\section{Definitions and Data Structures}
\begin{eqnarray*}
{paths\_trig} & \equiv & \mbox{set of triggered paths}\\
{paths\_timed} & \equiv & \mbox{set of timed paths}\\
{period} & \equiv & \mbox{length of time over which load and supply repeats}\\
{epoch} & \equiv & \mbox{division of a period for which statistics are separately}\\
{states} & \equiv & \mbox{set of possible energy states} \\
\delta_c & \equiv & \mbox{cost prediction adjustment dampening factor} \in(0,1)\\
\delta_r & \equiv & \mbox{revenue prediction adjustment dampening factor} \in(0,1)\\
\delta_b & \equiv & \mbox{buffer adjustment dampening factor} \in(0,1)\\
{cost\_actual} & \equiv  & \mbox{actual energy spent during previous epoch}\\
{cost\_pred}   & \equiv  & \mbox{predicted energy cost for previous epoch}\\
{rev\_actual} & \equiv  & \mbox{actual energy acquisition during previous epoch}\\
{rev\_pred}   & \equiv  & \mbox{energy acquisition predicted for previous epoch}\\
{timer\_level} & \equiv & \mbox{percentage of timer range selected} \\
{freq}[{path}][{state}] & \equiv  &\mbox{probability of taking a given path in a given state}\\
{load}[{epoch}] & \equiv & \mbox{average of loads during given epoch}\\
{energy\_in}[{epoch}] & \equiv & \mbox{average energy revenue during given epoch}\\
{cost\_static} & \equiv & \mbox{minimum energy cost of an epoch (ie, when idle)}\\
{cost}[{path}] & \equiv & \mbox{average energy cost of a given path, in addition to }{cost\_static}\\
{buffer\_level} & \equiv & \mbox{the percent of the buffer's capacity currently in use}\\
{buffer\_goal} & \equiv & \mbox{the percent of the buffer's capacity to aim for}\\
{scale\_cost} & \equiv & \mbox{a scaling factor to the stochastic energy consumption}\\
{scale\_rev} & \equiv & \mbox{a scaling factor to the energy revenue}\\
\end{eqnarray*}

\section{State Selection}

At the beginning of each ${epoch}$, we predict the base energy cost and revenue for the next ${period}$ (that is to say, the next ${NUM\_EPOCHS}$).  Base energy cost is calculated based on the probability of taking a triggered path, and assuming that timed paths are taken at the minimum frequency allowed for the state. A goal is set based on the expected revenue and the ${buffer\_level}$.  We then select the highest state which will have a cost lower than our goal.

Timer periods remain unselected.  To determine them, we calculate the cost for the same state with maximum frequency for all the active timers.  We find where our goal lies between the minimum and maximum costs, and scale the timers proportionally.

More precisely,

\begin{eqnarray*}
{scale\_rev} & = & 1 + \left(\delta_r\frac{{rev\_actual}-{rev\_pred}}{{rev\_pred}}\right) \\
{scale\_cost} & =&  1 + \left(\delta_c\frac{{cost\_actual}-{cost\_pred}}{{cost\_pred}}\right) \\
{predicted\_cost}_{min}[{state}]&  =&  \sum_{e\in{epoch}}\sum_{p\in{paths\_trig}}\left[ {freq}[p][{state}] \cdot {load}[e] \cdot {cost}[p] \right] \cdot {scale\_cost} +\\
                          &  &  \sum_{e\in{epoch}}\sum_{p\in{paths\_timed}}{cost}[p]\cdot1/{max\_period}_p \\\\
{predicted\_cost}_{max}[{state}]&  =&  \sum_{e\in{epoch}}\sum_{p\in{paths\_trig}}\left[ {freq}[p][{state}] \cdot {load}[e] \cdot {cost}[p] \right] \cdot {scale\_cost} +\\
                          &  &   \sum_{e\in{epoch}}\sum_{p\in{paths\_timed}}{cost}[p]\cdot1/{min\_period}_p \\\\
{predicted\_revenue}&  =&  \sum_{e\in{epoch}} {energy\_in}[e] \cdot {scale\_rev}\\
{goal} & = & {predicted\_revenue} \cdot \delta_b\left(({buffer\_level} - {buffer\_goal}\cdot{buffer\_capacity}\right)
\\
\end{eqnarray*}
\ttfamily
${state}$ = ${MAX\_STATE}$ ;\\
while (${predicted\_cost}_{min} < {goal}$) \\
\hspace{1cm}${state}$--;\\
\({timer\_level} = \frac{{goal} - {predicted\_cost}_{min}}{{predicted\_cost}_{min} - {goal}}\)

\rmfamily

\section{Path Cost Measurement}

Given the energy flow through the buffer (implemented as a hardware fuel gauge), and the total consumption of the system (implemented as a software fuel gauge), we are able to keep track of the power being consumed during execution of paths.  We allow for the possibility of parallel path execution by attributing equal parts of the total power consumption to each active path.  Acknowledging that this method may result in skewed values of ${cost}[{path}]$, we argue probabilistically that this will have nominal effect on our overall energy prediction.  If two paths with large energy consumption differences have a high enough probability of running in parallel enough to skew each other's results, that same high probability infers that it is unnecessary to distinguish between their values.

\end{document}


\chapter{Simulated Annealing}
\section{Introduction}
The Simulated Annealing search algorithm is examined in this chapter, looking  initially at the strategy options and configuration choices. The results section shows the performance of the algorithm in preliminary trials with a range of annealing schedules before presenting the results of the main trials.

\section{Search Strategy Options}
To be consistent with efforts of Random Search and Hill Climbing we again limit each trial to 25000 evaluations of the objective function. The operators used for Simulated Annealing are those that were used with Hill Climbing, however we have reduced the probability of occurrence of the shrink and grow operators based on our observations regarding duplication of solutions as discussed in Section~\ref{duplicate_solutions}.

The approach in general is similar to Hill Climbing, as the operators have been selected to allow fine-grained exploration of the search space while exploiting the capability of Simulated Annealing to avoid the pitfalls of local optima. The principle strategy choice for Simulated Annealing will be the selection of a suitable annealing schedule (see Section~\ref{schedule}), this is covered in detail in Section~\ref{schedule_selection}.

\section{Experimental Conditions}
Table~\ref{sa_param_table} shows the parameters used to configure Simulated Annealing. Symbolic Integration, Santa Fe and Blocks use initial Genome lengths of 100 while Symbolic Regression uses an initial length of 200. A temperature change rate of 0.95 is used to reduce the temperature on each iteration. A minimum temperature of \emph{0} is used to halt the annealing schedule. For a more complete explanation Section~\ref{sa_algorithm} provides a step-by-step guide to the algorithm.

\begin{table}[!hbp]
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
Parameter &\multicolumn{4}{l|}{Problems}\\
\cline{2-1} \cline{3-1} \cline{4-1} \cline{5-1}
 & Sym Int & Santa Fe & Blocks & Sym Reg  \\
\hline
Number of Trials & 1000 & 1000 & 1000 & 1000  \\
Number of Objective & & & & \\
Function Evaluations  & 25000 & 25000 & 25000 & 25000 \\
Initial Genome Length & 100 & 100 & 100 & 200  \\
Variation Range  & 10\% & 10\% & 10\% & 10\%  \\
Zero Improvement Accept  & off & off & off & off  \\
Probability of Selecting & & & & \\
Shrink Operator  & .05 & .05 & .05 & .05  \\
Probability of Selecting & & & & \\
Grow Operator  & .05 & .05 & .05 & .05  \\
Probability of Selecting & & & & \\
Force-up Operator  & .45 & .45 & .45 & .45  \\
Probability of Selecting & & & & \\
Force-down Operator  & .45 & .45 & .45 & .45 \\
Temperature Change Rate & 0.95 & 0.95 & 0.95 & 0.95  \\
Minimum Temperature & 0 & 0 & 0 & 0  \\
\hline
\end{tabular}
\caption{\label{sa_param_table} Parameters used for the Simulated Annealing Search Algorithm.}
\end{center}
\end{table}

\section{Establishing an Annealing Schedule}
\label{schedule_selection}The establishment of an appropriate annealing schedule for each of the problems centres on selecting the desired level of probability for acceptance of a dis-improving solution.
At a temperature $t$, the probability $P$ for a change  in energy of magnitude $\delta E$ is given by

\large
\begin{displaymath}
P(\delta E) = e ^{ \frac{-\delta E}{kt}}
\end{displaymath}
\normalsize

Figure~\ref{santafe_accept_prob_score_delta} shows the change in the probability of accepting a dis-improving solution for a range of score deltas. Three different temperatures have been shown. If it was possible to predict the type of score deltas we were likely to see in the course of a trial then we could usefully employ this information to select a suitable starting temperature. We know from the analysis of the Hill Climbing results that solutions often emerge from areas of the solution landscape where scores are low score. This might suggest that it is reasonable to accept large dis-improving deltas late in a trial. However this policy would also have the consequence of having us accept even larger score deltas early in a trial, this could force the search to remain trapped in sub optimal regions of the search space.

This is better illustrated in Figure~\ref{santafe_accept_prob_score_temp} which again shows the change in the probability of accepting a dis-improving solution but this time it has been plotted against increasing temperature. Three plots for score deltas of 1, 5 and 10 have been included. Again we see some of the trade offs in this graph a high starting temperature exposes the search to higher probabilities of moving into low scoring regions of the search space while starting with a very low temperatures might leave the search unable to effectively explore the search space because of a overly captious approach.

\begin{figure}[]
\centerline{\hbox{
\psfig{file=Chapter7/graphs/accept_prob_score_delta.ps,width=4in}}}
\caption[Probability of accepting a dis-improving Solution for a Range of Score Deltas]{Probability of accepting a dis-improving Solution for a Range of Score Deltas at three different Temperatures on the Santafe Trail Problem.}
\label{santafe_accept_prob_score_delta}
\end{figure}

\begin{figure}[]
\centerline{\hbox{
\psfig{file=Chapter7/graphs/accept_prob_temp_change.ps,width=4in}}}
\caption[Probability of accepting a dis-improving Solution for a Range of Temperatures]{Probability of accepting a dis-improving Solution for a Range of Temperatures at three different Score Deltas on the Santafe Trail Problem.}
\label{santafe_accept_prob_score_temp}
\end{figure}

In order to get some indication of suitable  annealing schedules for the problems a limited number of runs were first performed for a range of starting temperatures. The number of iterations at each temperature accordingly  varied in order to maintain our limit of 25000 evaluations of the objective function in a single trial.
The results of these preliminary runs plus the results of the extended runs are presented in the next section.

\section{Results}

Tables~\ref{sa_symint_sched_table},~\ref{sa_santafe_sched_table} and~\ref{sa_blocks_sched_table} show the results from the preliminary trials used to determine the appropriate annealing schedule for Symbolic Integration, Santa Fe and Blocks respectively. Similar runs on the Symbolic Regression problem failed to find any correct solutions.

These preliminary trials consisted of 500 runs for each start temperature. The table shows the starting temperature and  the number of successes achieved.

For the case of Symbolic Integration the success rate appears to peak at 11\% for starting temperatures of 1 with the success rate trailing off either side of this. On the Santa Fe problem we see a plateau of similar scores for temperatures of 1 or less with a significant fall off in success rates for higher starting temperatures. The schedules for Blocks follows a  similar pattern to that of Santa Fe showing higher success rates at starting temperatures of one or less. The Symbolic Regression problem was not solved in any of the preliminary trials.

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|}
\hline
Starting Temperature & Successful Runs \\
\hline
0.3 & 10\% \\
0.1 & 6\% \\
1   &  11\% \\
5   &  5\% \\
10  & 2\% \\
\hline
\end{tabular}
\caption{\label{sa_symint_sched_table} Results from 500 trials of Simulated Annealing on the Symbolic Integration Problem for a Range of Starting Temperatures.}
\end{center}
\end{table}

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|}
\hline
Starting Temperature &  Successful Runs \\
\hline
0.3   &  17\%\\
0.1   &  20\%\\
1    &  19\% \\
5    &  4\% \\
10   &  5\% \\
\hline
\end{tabular}
\caption{\label{sa_santafe_sched_table} Results from 500 trials of Simulated Annealing on the Santa Fe Trail Problem for a Range of Starting Temperatures.}
\end{center}
\end{table}

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|}
\hline
Starting Temperature &  Successful Runs \\
\hline
0.1   &   11\%\\
0.3    &   13\%\\
1     &   12\%\\
5     &   6\%\\
10    &   9\%\\
\hline
\end{tabular}
\caption{\label{sa_blocks_sched_table} Results from 500 trials of Simulated Annealing on the Blocks Problem for a Range of Starting Temperatures.}
\end{center}
\end{table}

Table~\ref{sa_results_table} provides a summary of the final results for Simulated Annealing. 1000 runs were performed using most successful starting temperature of 1 from the preliminary trials. Symbolic Regression was not solved in any of the attempts, however Symbolic Integration and Blocks have similar success rates scoring 11\% and 13\% respectively, while Santa Fe scores 21\%.
While the algorithm is more successful on the Symbolic Integration and Santa Fe problems than Hill Climbing, the overall results are still poor relative to those of Random Search.

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|}
\hline
Problem & Successful Runs \\
\hline
Symbolic Integration & 11\% \\
Santa Fe Trail & 21\% \\
Blocks & 13\% \\
Symbolic Regression & 0\% \\
Spirals & 0\% \\
\hline
\end{tabular}
\caption{ \label{sa_results_table} Results from the Simulated Annealing Trials.}
\end{center}
\end{table}

\section{Characteristics of Solutions found by Simulated Annealing}

Table~\ref{sa_results_analysis_table} shows details of the solutions found by Simulated Annealing, wrapping features prominently in Santa Fe and Blocks. The average solution length for Santa Fe is 58 codons of which 48 are expressed. Symbolic Integration has solutions with an average genome length of 76 of which only 19 are expressed. An aspect of the results from the Blocks problem is that the number of expressed codons was 91, which is considerably greater than the average number of codons in a solution at 54. This aspect of the Block's results is caused by multiple wrap events where the codons are repeatedly re-used until a successful solution is found.

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|l|l|l|}
\hline
Feature & Sym Int & Santa Fe & Blocks & Sym Reg  \\
\hline
Avg Number of Codons & & & &  \\
in Solution & 76 & 58 & 54 & n/a  \\
Avg Number of expressed & & & &  \\
Codons in Solution & 19 & 48 & 91 & n/a  \\
Avg Number of Solutions & & & &  \\
featuring Wrapping & 0\% & 50\% & 66\% & n/a  \\
\hline
\end{tabular}
\caption{\label{sa_results_analysis_table} Analysis of Characteristics from Solutions found by Simulated Annealing.}
\end{center}
\end{table}

\subsection{Analysis of search trajectory}

A typical search trajectory for a Simulated Annealing trial is shown in ~\ref{sa_search1}. In the early stages the algorithm explores areas of both low and high fitness but as the trials progresses we see a steady climb to areas of high fitness as the decreasing temperature reduces the probability of selecting dis-improving solutions.

\begin{figure}[hbp]
\centerline{\hbox{
\psfig{file=Chapter7/graphs/search_trajectory_sa.ps,width=5in}}}
\caption{\label{sa_search1}Search Trajectory for a Successful Simulated Annealing Trial}
\end{figure}

\section{Summary}
In this chapter we have looked at Simulated Annealing, a metaheuristic, which models itself on the way crystals form in solids during the cooling process. The principle strategy choice for the method was the selection of a suitable annealing schedule, to assist this selection a number of preliminary trials were performed using a range of starting temperatures. The final results showed unsuccessful attempts on two of the problem, Symbolic Regression and Spirals and low success rates on the other three.

The additional capability of Simulated Annealing to avoid sub-optima would appear to have added little or no improvement on the scores achieved by Hill Climbing, leaving it a considerable way behind Random Search.


\chapter{Analysis}
\label{Analysis}

Like any algorithm, we can not define "the best cryptography method;" rather, the cryptography algorithm that you use is
very case-dependant. In order to find a suitable candidate(s), one must examine the merits of a set of different algorithms.

When it comes to cryptography, security is at the root of the question. But how much do you \textit{really} value security?
For some, it may be a matter of "life or death." For others, security is a secondary feature of their program. Maybe you don't
even care about security? This can surely be the case, consider a puzzle game, one could employ the use of a substitution or
transposition cipher to create an interesting cryptography puzzle.

Let us consider a \textit{live chat application}. This application will send messages from one user to another (over the network).
We will assume that the messages sent are sensitive. In this case, security does seem to play a vital role in the function
of the application. Though we must also consider the \textit{comprising party}---the party which intends to infiltrate the
communication network.

Say we are designing a chat application for an intranet,\footnote{An intranet is a private network that is only accessible to
members of the network. An organization may use an intranet; only staff of the organization may access the network.} there may
no comprising party or the risk of a comprising party is low. Therefore we may not require any complex cryptography systems,
instead opting for a simple symmetric-key algorithm (such as Caesar Cipher or the slightly more complex DES scheme). On the
other hand, if we are developing a chat application for the internet where anyone can access the communication network. We may
place more importance on security as the network is public.

What if do we require a complex asymmetric-key system? We could use the discussed RSA scheme. All around, the RSA
scheme is an excellent cryptography method---it is extremely reliable---it is purely a mathematical solution---and it is secure.
But as always, there exists a caveat. The RSA scheme generates insanely large numbers. This is due to the fact that our plaintext
must be smaller than our modulus (the shared key). As discussed in
\hyperref[Asymmetric-key cryptography]{Asymmetric-key cryptography}, if our plaintext is
larger than our modulus, we must split our plaintext into appropriately sized blocks. We then encrypt each block in some mode
of operation (such as Feistel cipher). Unfortunately, this type of encryption will reveal redundancies in the ciphertext which
makes the cipher easier to break. Alternatively, we can expand the size of our modulus (and private and public keys)
to suit the size of our plaintext. This technique will work in theory but in practice there exists a hardware limitation---the
size of a data type; RSA will quickly exceed the max value of a $64$-bit integer. Instead we can use a \textit{Big Integer}, a
data structure which allows us to store \textit{very very} large numbers. This does however use a lot of memory. Nonetheless,
despite the existing alternatives, there exists the fact that RSA is a memory-hog. And despite advancements in computer
technology, there always exists an instantial upper-bound---whether it be storage or speed.


\section{2021-09-22 Lecture}

This is a class that cannot fail,
because if it crashes and burns,
the qualifier fails.

This is my favourite lecture.
Sorry it's online.
It's conceptual.
This used to not be covered in standard classes,
but it because so popular there's no way out.
I'm going to start from something everyone knows.
The double slit experiment.

If you're not comfortable,
it means two things:
you don't know QM and you should laern straight away.

The best way to learn is to open the old Feynman lectures on physics.
Of course you can go to Youtube anddo the same thing,
with beautiful computer graphics,
but unfortanaltely many of them are wrong.
Feynman knows physics.

If there's only one hole nothing exciting happens.
The particle hits the screen,
mostly behind the hole.
Here I plot the number of particles that hit the screen.
If the hole is left,
you get particles on the lfet.
If the hole is right,
you get particles on the right.

Buty what happens when you shoot two holes?
They interferer with each other.
The result is surpricingly,
in fact the most likely place to get a particle is in the middle,
which is neither the single hole scneairos.
It's more than just the sum of the two.
You get an interference pattern.

You seen t his before.
It's pretty.
There are regions where the waves interfere desctructively and you don't get a
particle behin the screen.
that's how all waves behave.
If you make waves no thel ake,you get he same thing,
so it's not surprising.
What is surprising is tat the aprticles sometimes behave like particles,
and sometimes waves.
In fact,
what you get on the screen is this.

If you send particles one by one and you start counting,
every time you send a particle,
you get one spot on the screen,
however the probability fo getting apricles obeys the interference pattern.
That's this other way we see this particle-wave duality.

I'm going to switch to my laptop.
That's a drawing of the situation here.
I want to remind you how to descrbie this quanutm mechancially.

Let's say the left sitautions is $\ket{L}$
and the situation for the right hole open is $\ket{R}$.
When you have both holes open,
the state is going to be a linear combination
\begin{align}
    \ket{\psi} = \alpha\ket{L} + \beta\ket{R}
\end{align}
How do you get intference?
Let's measure $A_y$ that counts the number of particles,
or the probablity of the particle arriving at coordinate $y$
on hte screen.
Of course,
I want to take the expectation value.
\begin{align}
    \bra{\psi} A_y \ket{\psi} &=
    \left( \alpha^* \bra{L} + \beta^*\bra{R} \right)
    A_y
    \left( \alpha\ket{L} + \beta\ket{R} \right)\\
    &=
    |\alpha|^2 \bra{L}A_y\ket{L}
    + |\beta|^2 \bra{R}A_y\ket{R}
    + \alpha\beta^* \bra{L}A_y\ket{R}
    + \alpha^*\beta \bra{R}A_y\ket{L}
\end{align}
The first two terms are familiar.
They are just the probabilities for if the left hole is open only
and the rigt hole is open only respectively.
THe novelty of course in QM is that you have inerference terms,
the last two terms.
While the $|\alpha|^2$ and $|\beta^2|$ terms are positive,
those $\alpha\beta^*$ and $\beta\alpha^*$ terms are not necessarily positive,
so it's possible you cna have cancelation.
And that's why you get zeros in the distribution,
it's because of intereference and cancellation.

So this is well known.
The fact you have uqanutm intereference out of linear combinations,
you get this pretty well.

Let's compare this with a different situation.
Let's say I have a different siuaiton like this.
Except the two slits are never open at the same time.

I'm going to trow a dice.
If the number is odd,
I open the left hole.
If the number is even,
I open the right hole.
What am I going to get?

hal the time I'm going to get the L distributino,
and half the time I'm going to ge the R distribution.
And I get a double-bump distirbution.

So then this quantity,
The previous situation would be left AND right.
But now I will call this dice roll situation left OR right.
\begin{align}
    \bar{A}_y &=
    p_L \bra{L}A_y\ket{L}
    + p_R \bra{R}A_y\ket{R}
\end{align}
where $p_L$ and $p_R$ are the probabilities of having the left or right holes
open respectively.
You notice that this matces the first two terms of the previous formula.
What you do not find is the second two terms,
so there is no interference.
This is a different physical sitaution.

Those two things are not the same.
If you plot $A_y$ vs $y$,
in the QM superposition,
you get this
[picture]

But if you have the classical probability,
you get two bumps.
[picture]

These two sitautions are very common,
and you need to give these names.
having L and R in superpsotiion quantum mechanically,
we have a \emph{coherent} sum.
But the classical probability situation,
is the \emph{incoherent sum}.
The incorhernet sitaution happens all the time
in classical physics.

But the coherent sum is something very special to QM,
there is no classical analogue for that.

This situation where you have a combination of this,
where some of it is quanutm mehaonical,
but I also don't know what the wave function is,
appears commonly in physics too.

So ther eare two kinds of uncertainty.
There is the quantum uncertainty of the wave function,
butther'es the classical uncertainty of not knowing what the wave function is in
the first place.
There is a formalism to deal with this.

Suppose you have some observablve $A$.
Then
\begin{align}
    \bar{A} &=
    \underbrace{
    \sum_n \underbrace{p_n}_{\text{prob. in $\ket{n}$}} \underbrace{\bra{n} A \ket{n}}_{\text{quantum average}}
    }_{\text{classical average}}
\end{align}
So we have a quantum and a classical average

There's a cute way to write this as a trace.
\begin{align}
    \bar{A} = \Tr\left[
    \left( \sum_n p_n \ket{n}\bra{n} \right) A
    \right]
\end{align}
Is this the same?
It's easy to see,
because to compute the trace,
one way of computing the trace is to take a basis,
for example the same basis $\ket{n}$,
then compute the sandwich of whatever is in the trace and then sum over elements
of the basis,
but you see this is easy to compute.
\begin{align}
    \Tr\left[
    \left( \sum_n p_n \ket{n}\bra{n} \right) A
    \right]
    &=
    \sum_{m} \bra{m}
    \sum_n p_n \ket{n}\bra{n} A\ket{m}\\
    &= \sum_{m,n} \cdots
\end{align}
Then observe the following things.
Then we call that part the density matrix.
\begin{align}
    \rho = \sum_n p_n \ket{n}\bra{n}
\end{align}
This contains all the information about the system.
It doesn't tell me what the wave function is,
because there's uncertainty about what the wave function is.
But the probabliies are al defined by this $\rho$.
States whree therare different clasical probailitesi of different wave fucntions
is called a \emph{mixed state}.
In situations wher we know what hte wave function is,
they are called \emph{pure dstates}.

To be clear,
\begin{align}
    \bar{A} = \Tr(\rho A)
\end{align}
Remember $\rho$ contains informaiton about all possible wave functions the
system may have.

Are we good up to now?
By the way,
you can raise a question by raising a hand,
but it's better to shout,
just unmute yourself and shout.
You can even go and ask questions in the chat.

\begin{question}
    Can you elaborate on mixed state?
\end{question}
If you know thw ave function of the sytem,
you call it a pure state,
that's what you learnt in QM so far.
A pure state is just a real state.
But then suppose you have a harmonic oscillator
with 30\% chance in the ground state and 70\% chance in the first excited state,
that is a mixed state.
You should not be confused though.
Take hte harmonic oscilator for exmaple.
\begin{align}
    \ket{\varphi} &=
    \frac{3}{5}\ket{0}
    + \frac{4}{5}\ket{1}
    \ne
    \rho
    =
    \left( \frac{3}{5} \right)^2 \ket{0}\bra{0}
    \left( \frac{4}{5} \right)^2 \ket{1}\bra{1}
\end{align}
These states here have the same probabiliteis,
but they are completely different states.
I can even add a phase.
\begin{align}
    \ket{\varphi} &=
    \frac{3}{5}\ket{0}
    + i\frac{4}{5}\ket{1}
    \ne
    \rho
    =
    \left( \frac{3}{5} \right)^2 \ket{0}\bra{0}
    \left( \frac{4}{5} \right)^2 \ket{1}\bra{1}
\end{align}
It's either one or the other.
No matter how smart I am,
I will know a way to measure.

here's an example.

How do we describe th sitaution where both holes are open.
We already have that,
a quantum superpsoition of left and right.
When I compuet the probablity of getting a particle on the sreen,
I get interference tersm.
If I have a phsae,
the probabilities $|\alpha|^2$ and $|\beta|^2$ don't change,
but I get a different interefernece pattern.

That's usual quantum mechanics,
but I can describe the sitaution with desity matrices too
which account for probabilities that arise because I am too dumb to know.

So I can describe the double slit experiment with density matrices too.
For example,
the incoehreent sum is
\begin{align}
    \rho &= \frac{1}{2} \ket{L}\bra{L}
    + \frac{1}{2}\ket{R}\bra{R}
\end{align}
and you can computet the probabilities
\begin{align}
    \bar{A} &=
    \Tr(\rho A_y)\\
    &=
    \Tr\left( \frac{1}{2}\ket{L}\bra{L} + \frac{1}{2}\ket{R}\bra{R} \right) A\\
    &=
    \bra{L}\left(
    \frac{1}{2} \ket{L}\bra{L}
    + \frac{1}{2} \ket{R}\bra{R}
    \right)A_y
    \ket{R}
    + \bra{L}\left(
    \frac{1}{2} \ket{L}\bra{L}
    + \frac{1}{2} \ket{R}\bra{R}
    \right)A_y
    \ket{R}\\
    &= \frac{1}{2}\bra{L} A_y \ket{L}
    + \frac{1}{2}\bra{R} A_{y} \ket{R}
\end{align}
Now look at what we thoguht it should be
\begin{align}
    \bar{A}_
    &= p_L\bra{L} A_y \ket{L}
    + p_R\bra{R} A_{y} \ket{R}
\end{align}
so the formalism works!
Let me tell you a couple of probabilities it's to satisfy.

Firstly, $\rho$ is a Hermitian operator.
It's weird,
because it's not an observable,
but it's the state of the system.
\begin{align}
    \rho^{\dagger} = \rho
\end{align}
Can you see this is true?
Sure
\begin{align}
    \left( \sum_n p_n \ket{n}\bra{n} \right)^{\dagger}
    &= \sum_n p_n^* \ket{n}\bra{n}\\
    &= \sum_n p_n \ket{n}\bra{n}
\end{align}
because $p_n$ are real probabilities,
so very straighforward.

Anotherp robability isthat it has unit trace.
\begin{align}
    \Tr \rho = 1
\end{align}
To see this,
\begin{align}
    \Tr \rho &=
    \Tr \sum_n p_n\ket{n}\bra{n}\\
    &= \sum_m \bra{m} \sum_n p_n\ket{n}\bra{n} \ket{m}\\
    &= \sum_{n,m} p_n \braket{m}{n} \braket{n}{m}\\
    &= \sum_n p_n\\
    &= 1
\end{align}
because the total probability is equal to 1.

Finally, $\rho$ is positive definite.
It follows from the fact that $p_n$ has to be positive or zero.
Positive definite means that
\begin{align}
    \bra{\phi}\rho\ket{\phi} &=
    \bra{\phi} \sum_n p_n\ket{n}\braket{n}{\phi}\\
    &=
    \sum_n p_n \braket{\phi}{n} \braket{n}{\phi}\\
    &= \sum_n p_n |\braket{n}{\phi}|^2\\
    &\ge 0
\end{align}

There's one final property.
For some special states, we have
\begin{align}
    \rho^2 = \rho
\end{align}
if and only if $\rho=\ket{\psi}\bra{\psi}$ is a pure state.
You can see this clearly because if
$\rho = \sum_n p_n \ket{n}\bra{n}$
and if $p_n=0$ except $p_{\bar{n}}=1$,
then obviously $\ket{\bar{n}}=\ket{n}$.
There are two things to prove,
because it's if and only if.
It's a simple test.
Suppose $\rho$ is pure,
then
\begin{align}
    \rho^2 &=
    \ket{\psi}\braket{\psi}{\psi}\bra{\psi} = \rho
\end{align}
It's a bit of extra work to go the other way around.

Suppose $\rho^2 = \rho$.
Then
\begin{align}
    \sum_n p_n \ket{n}\bra{n}
    \sum_m p_m \ket{m}\bra{m}
    &= \sum_{n,m} p_n p_m \ket{n}
    \underbrace{\braket{n}{m}}_{\delta_{nm}} \bra{m}\\
    &= \sum_n p_n^2 \ket{n}\bra{n}\\
    &= \sum_n p_n \ket{n}\bra{n}
\end{align}
so we know that $p_n^2=p_n$ for all $n$.
There are only two ways this can be true.
Either $p_n=0$ or $p_n=1$.
But probabilities sum to 1,
so only one of these can be $p_{\bar{n}}=1$,
and all the other $p_n=0$.
What does that prove?
In other words,
\begin{align}
    \rho = \sum_n p_n\ket{n}\bra{n} = \ket{\bar{n}}\bra{\bar{n}}
\end{align}

Oh there's a question.
It's just one word: magical.

You're just in awe of the power of density matrices and bras and kets?

By the way,
Landau invented density matrices.
He was two years too young to invent QM,
but he invented density matrices.
Bras and kets,
I'm tired of syaing it's a beautiful thing,
but I hope you can appreciate how the algebra just works without thinking.
It's importnat to understand why it works,
but once you do,
it's easy.

The name of this class is quantum and statistical physics.
Forgoet about the deep stuff.y
You're goign to use distributions of positions and momentum classical,
but the density analogue is density matrices,
where you have distributions over wave functions.

\begin{question}
    So $\rho$ is diagonal?
\end{question}
Well $\bra{n}\rho\ket{n'}$ is a diagonal matrix in this basis,
but in a different basis,
it will not necessarily be a diagonal matrix,
despite describing the same physics.
If the operator was diagonal,
I wouldn't talk about htis operator,
it would be overkill.
In this basiss to define $\rho$,
but may be diagonal,
but in another basis,
it's not diagonal
and it's not obvoius at all it's not a pure state.


\section{Types}

\subsection{Primary types}
Types are composed of several key expressions:

\begin{grammar}
<type> ::= <name>
      \alt <type application>
      \alt <forall>
      \alt <constraint>
      \alt <type function>
      \alt <type tuple>
      \alt `(' <type> `)'
      \alt `_'
      \alt `[' <type> `]'
      \alt `[|' <type> `|]'
      \alt <record>
      \alt <row>
      \alt <union>
      \alt <unifies>

<var> ::= <ident>
     \alt `(' <ident> : <type> `)'

<type application> ::= <type> <type>
                  \alt <type> <name> <type>
                  \alt <type> \lit{\`{}} <name> \lit{\`{}} <type>

<forall>        ::= `forall' \{ <var> \} `.' <type>

<constraint>    ::= <type> `=>' <type>

<function type> ::= <type> `->' <type>

<tuple type>    ::= <type> \{ `*' <type> \}

<unifies>       ::= <type> \lit{$\sim$} <type>
\end{grammar}
The $type\ application$, $forall$, $constraint$ and $type\ function$ rules are right associative, whilst $unifies$ is non-associative.

\todo[inline]{What is the precedence of these?}

\subsection{Type annotations}
\begin{itemize}
\item \texttt{:} is used to annotate a variable or expression having a type.
\item \texttt{\_} can be used to mark a type wildcard (not a type variable, just something you don’t want to explicitly write). So you could have \texttt{[\_]} to constrain it to be a list of something.
\end{itemize}

\subsubsection{Rows and Unions}
Rows are composed of a series of key-type pairs or a union of two rows:

\begin{grammar}
<row body> ::= \{<ident> `:' <type> `,'\}
          \alt <ident>

<row union> ::= `'
           \alt <row_body> \{ `|' <row_body> \}

<row>      ::= `(' <row union> `)'
\end{grammar}

\todo[inline]{What syntax are we using for union types? Braces are taken for record syntax.}

\subsubsection{Records}
Record definitions are composed of a series of identifier keys to type pairs separated by commas within braces.

\begin{grammar}
<record>      ::= `\{' <row union> `\}'
\end{grammar}

\subsection{Type declarations}
Types can only be declared in a module. There are two forms of type declarations:

\begin{itemize}
\item Aliases: allowing referencing a longer type name as a shorter one (or renaming it if it conflicts or any other reason)
\item Type definitions: used for creating sum and product types.
\end{itemize}

\subsubsection{Type names}
Type names are defined as an identifier followed by a series of free type variables and/or concrete types.
The parameters that are specified within the RHS of the definition \textit{must} exist on the LHS.

\subsubsection{Type aliases}
The \texttt{type} keyword is used to define a type alias:

\begin{grammar}
<type name def> ::= <ident> \{ <var> \}
               \alt `op' (`left' | `right') [<digit>] <var> <ident> <var>
               \alt <var> \lit{\`{}} <ident> \lit{\`{}} <var>

<type name>     ::= <type name>  [ `:' <type> ]

<type def> ::= `type' <type name> `=' <type>
          \alt `type' `foreign' <type name>
\end{grammar}

\subsubsection{Sum type definitions}
Sum types are composed of one or more type-constructors, each followed by the types the product type is composed of.

\begin{grammar}
<product name> ::= <ident> \{<type>\}
              \alt <type> `(' <ident> `)' <type>
              \alt <type> \lit{\`{}} <ident> \lit{\`{}} <type>

<product type> ::= <product name>  [ `:' <type> ]

<sum type>     ::= `data' <type name> `=' \{`|' <product type> \}
              \alt `data' `foreign' <sum name>
\end{grammar}

If the declaration starts on a new line it should have a leading `|', otherwise not.

\begin{minted}{haskell}
data X = A | B

data Y =
    | C Int
    | D
\end{minted}

\paragraph{GADTs}
GADTs are defined as so:

\begin{minted}{haskell}
data Foo a = Foo1 Int : Foo Int
           | Foo2     : Foo a
\end{minted}

Type variables defined on the RHS are accessible on the left:

\begin{minted}{haskell}
data Bar a = Bar1 a b : forall b . Foo a
           | Bar2     : Foo a
\end{minted}

\subsubsection{Type classes}
Type classes are marked with the \texttt{class} keyword. You can then specify a series of constraints on various parameters before specifying the actual type. All constraints’ parameters must appear in the type class’s definition.

\begin{grammar}
<class constraint> ::= <type> `=>'

<functional dep>   ::= \{ <ident> \} `->' \{ <ident> \}

<binding>  ::= <lhs> [`:' <type> ] [`=' <expr>]

<bindings> ::= <binding>
          \alt <binding> \{ `and' <binding> \}

<class body> ::= `type' <type name> [`=' <type> ]
            \alt `data' <type name> [`=' \{ '|' <product type> \} ]
            \alt `let' [`rec'] <bindings>

<class>            ::= `class' \{ <class constraint> \} <type> [ `|' <functional dep> \{ `,` <functional dep> \} ] `where' <class body>

<instance>         ::= `impl' <ident> \{ <var> \} `=' <type> `where' <class body>
\end{grammar}

\begin{minted}{haskell}
class Show a where -- or class Show a =
    let show : a -> String

impl X = Show String where
    let show x = "!" ++ x ++ "!"
\end{minted}

The beginning of the type class’s body is marked with the \texttt{where} keyword and an indent. The body contains a series of variables with type annotations with optional definitions. It is possible for all definitions to be filled and depend on one another: when creating an implementation Amulet will determine if you have provided sufficient information for a complete definition.

\paragraph{Type class implementations}
Implementations share a similar syntax to type class definitions, using \texttt{impl} instead of \texttt{class}. However, instead of following the type class with type parameters you can use any type expression.

The body of the implementation uses an identical syntax to that of type classes, though empty definitions are not allowed.