% chapter included in vclmanual.tex
\documentclass[vcl_manual.tex]{subfiles}
\begin{document}

\section{Floating point behavior details}
\label{FloatingPointBehavior}

The Vector Class Library is generally conforming to the new IEEE 754-2019 Standard for Floating-Point Arithmetic, but some compromises have been necessary for the purpose of vector processing and for better performance. The deviations from the standard are discussed below.
\vspacesmall

\begin{description}

\item[Subnormal numbers.]
Subnormal numbers (also called denormal numbers) are numerically extremely small floating point numbers where the exponent is below the normal range. Some microprocessors are handling subnormal numbers in a very inefficient way that is more than a hundred times slower than for normal floating point numbers. You may call the function \codei{no\_subnormals()} to prevent this and treat subnormal numbers as zero. Some of the mathematical functions in VCL always treat subnormal numbers as zero for reasons of performance. This includes logarithm, exponential, and power functions.

\item[Signed zero.]
Signed zero is a controversial issue. The floating point standard defines two different zeroes: +0.0 and -0.0.
The two zeroes are equal, but still distinguishable. Some of the functions may return +0.0 where the standard requires -0.0.\\
You may {} \codei{\#define SIGNED\_ZERO} {} if you want the sign of zero to conform to the
IEEE 754-2019 standard, though this may slow down performance a little.
\codei{SIGNED\_ZERO} may affect several functions, including
\codei{round}, \codei{truncate}, \codei{floor}, \codei{ceil},
\codei{maximum}, \codei{minimum}, \codei{cbrt}, \codei{pow\_ratio}, \codei{expm1}, \codei{log1p}. \\
This feature is not supported in version 1.xx of VCL.

\item[No exception trapping.] \label{NoExceptionTrapping}
Floating point errors are traditionally detected by trapping errors or relying on an \codei{errno} variable. These methods are not well suited for vector processing and out-of-order processing. This is explained in the document \href{https://www.agner.org/optimize/nan_propagation.pdf}{"NAN propagation versus fault trapping in floating point code", Agner Fog, 2019}.
\vspacesmall

The Vector Class Library does not support fault trapping, and it does not indicate exceptions in a variable such as the traditional \codei{errno}. It is not recommended to turn on floating point exceptions because this can cause inconsistent behavior, such as traps for exceptions in not-taken branches. Do not attempt to trap numerical errors in \codei{try/catch} blocks.
\vspacesmall

Instead, the vector class library indicates floating point exceptions by producing INF or NAN codes in the individual vector element that produced the fault.
The INF and NAN codes will propagate to the end result of a series of calculations when certain conditions are satisfied. The most efficient way of detecting floating point errors is to look for INF and NAN codes in the result.
\vspacesmall

Conditions where INF and NAN codes are not propagated are discussed at page  \pageref{FloatingPointErrors}
\vspacesmall

Do not use the compiler options -ffast-math, -ffinite-math-only, or /fp:fast because this may disable the detection of INF and NAN.
\vspacesmall

\item[No signaling NANs.]
Signaling NANs are special codes that will raise an exception when they are loaded from memory. Signaling NANs are rarely used in modern software. Signaling NANs should not be used in VCL because exception trapping is not supported.

\item[NAN payload operations.]
A NAN may contain additional information called a payload. This payload can propagate through a series of calculations to the end result. Some of the mathematical functions in VCL can put a payload into the NAN result in case of an error. This makes it possible to identify which function generated the NAN.
\vspacesmall

The \codei{nan..} and \codei{nan\_code} functions make it possible to set and get NAN payloads. The IEEE 754 standard does not specify what happens to the payload when converting between single and double precision, but experiments show that all microprocessors that use the binary floating point format will left-justify the payload. The \codei{nan..} and \codei{nan\_code} functions treat the NAN payload as a 22-bit left-justified unsigned integer in order to allow conversions between single and double precision. These functions deviate from the IEEE 754-2019 standard.

\item[NAN propagation in maximum and minimum functions.]
The \codei{max} and \codei{min} functions do not propagate NANs according to the 2008 version of the standard. This unfortunate situation is redressed in the  2019 revision of the standard. VCL offers two different versions of these functions:
The \codei{max} and \codei{min} functions are equivalent to
\codei{a > b ? a : b} and
\codei{a < b ? a : b}, respectively. These functions return \codei{b} if \codei{a} is NAN. The slightly less efficient functions \codei{maximum} and \codei{minimum} are sure to propagate NANs, in accordance with the 2019 revision of the standard.

\item[NAN propagation in pow function.]
The standard specifies that pow(NAN,0) and pow(1,NAN) will give the result 1.0. The VCL implementation deviates from this and produces a NAN output in all cases where an input is NAN, in order to support reliable NAN propagation.

\item[Function parameter range.]
Some of the mathematical functions have internal overflow for extreme values of the input parameters. These functions have a limited input range because an extra branch to handle the extreme cases would reduce the overall performance. Limitations of the input range are mentioned in the listing of the individual functions.

\end{description}

\vspacesmall

\end{document}


The horseshoe effect is a phenomenon that has long intrigued ecologists.  Commonly thought to be an artifact of dimensionality reduction, multiple techniques were developed to unravel this phenomenon and simplify interpretation.  Here, we provide evidence that horseshoes arise as a consequence of distance metrics that saturate - a familiar concept in other fields but new to microbial ecology.   This saturation property loses information about community dissimilarity, simply because it cannot discriminate between samples that do not share any common features. The phenomenon illuminates niche differentiation in microbial communities and indicates species turnover along environmental gradients.  Here we propose a rationale to the observed horseshoe effect from multiple dimensionality reduction techniques applied to simulations, soil samples, and samples from postmortem mice.   An intuitive-depth understanding of this phenomenon allows for the targeting of niche differentiation patterns from high-level ordination plots.
\section{Introduction}
Ecological datasets, particularly those observed in microbiome studies, are typically sparse and high-dimensional, frustrating most conventional statistical techniques.  Many numerical ecology software packages make use of distance-based statistics by calculating the distance between ecological communities, to compare various ecosystems to each other over space and time.  One of the most common exploratory analysis techniques is ordination, where the distances between the communities are embedded into a Euclidean space, and then visualized via Principal Components Analysis (\gls{pca}) \cite{numerical_ecology}.  A widely used extension of this technique, where the distance metric can be varied, is called Principal Coordinates Analysis (\gls{pcoa}) \cite{numerical_ecology}.  \par
One phenomenon that commonly occurs in datasets containing ecological gradients is the horseshoe effect or Guttman effect \cite{global_patterns}.  This phenomenon is typified by a linear gradient that appears as a curve in ordination space. The horseshoe effect, or its relative the arch effect \cite{detrended_correspondence_analysis} (where the ends of the gradient do not attract each other along the first principal coordinate as they do in the horseshoe effect), is observed using multiple types of ordinations, including Principal Components Analysis, Principal Coordinates Analysis, Non-Metric Multidimensional Scaling, Correspondence Analysis, and many others \cite{numerical_ecology}.  In 1982, the prevailing view of the horseshoe effect arose, when it was described by Gauch as a mathematical artifact that obscures the underlying ecological gradient.  Soon thereafter, Detrending Correspondence Analysis \cite{detrended_correspondence_analysis} was invented to unbend the horseshoe using reciprocal averaging. Since then, detrending has become a commonly applied practice to ordinations in ecological datasets.   Although these detrending techniques appear to provide a more intuitive visualization, they have been criticized as providing a distorted perspective of the underlying data, relying on many parameter settings that cannot be chosen in a principled way, and obscuring true underlying patterns in the data \cite{trust_dca}.\par
\begin{figure}[H]
        \centering
        \includegraphics[width=1.1\textwidth]{ch2/Figure1.pdf}
        \caption[An explanation of the horseshoe effect arising from distance saturation.]
        {An explanation of the horseshoe effect arising from distance saturation. (a) A band table where the y axis encodes for individual \gls{otu}s and the x axis encodes for samples.  Blocks that are colored black have a value of 1/10 while blocks that are colored white have a value of 0.  (b) The first 2 components from a \gls{pca} of the band table, yielding the typical horseshoe shape.  (c) The Euclidean distance from the point 0 to all of the other points.  (d) An illustration of distance saturation property.\index{SanDiego6}}
        \label{figb1}
\end{figure}
From previous studies, it was shown that horseshoes can arise from band tables \cite{horseshoe_kernel, guttman_effect}.  These tables consist of highly dense, non-zero values along the diagonal of the table, and sparse values everywhere else.  This pattern can be apparent when the rows and columns are sorted in the proper order. Although the idea that band tables lead to horseshoes is not a new idea, it is commonly misunderstood how this concept applies to microbial analyses.  Here we provide some intuition behind the mathematical structure of horseshoes.\par
In Figure \ref{figb1}a, we show a simulated band table, where each vertical band is represented by a sample, and contains 10 non-zero values.  In typical microbiome datasets, these values could reflect \gls{otu} or species counts; for simplicity, here we will to refer to them as species counts, although this concept can also be generalized to multiple data types, such as gene counts, metabolite abundances.  Each sample in the table is shifted by 1 row, creating the band effect.  When \gls{pca} is applied directly to this table, the first 2 eigenvectors yield a horseshoe pattern (Figure \ref{figb1}b).  Here, the band table is parameterized with a band size of 10, since each sample has exactly 10 non-zero values.  \par
For close local points, the Euclidean distance grows linearly along the gradient (Figure \ref{figb1}c).  However, after a certain point, the distance completely saturates. This property has been previously noted with Euclidean distance \cite{detrended_correspondence_analysis}.  The overlap between the first sample in the band table, and sample 10 and beyond disappears, and the distance between these samples is maximized. This can yield unintuitive properties, sample 10 could be less dissimilar than sample 1 compared to sample 20.  For instance, sample 10 could represent a medium pH environment, sample 1 could represent an acidic low pH environment and sample 20 could represent a high pHbasic environment. Sample 1 is expected to be a substantially more different microbial community to Sample 20 than Sample 10.  The acidophiles found in Sample 1 are typically not found in basic environments. Sample 20 is expected to be more different to sample 1 than sample 10, since it contains very different microbes that thrive in high pH environments.  But as far as Euclidean distance is concerned, sample 10 is just as dissimilar to sample 1 as sample 20, just because there are no common bacteria shared between these samples.  It is apparent that the saturation property of Euclidean distance does not capture all of the information about community dissimilarity along a gradient, simply because it cannot discriminate between samples that do not share any common features.  Once the distance is saturated, all samples that do not overlap lie within a ball of radius B where B is the band size lie within a ball of radius where B is the band size and the first point is the center of the ball as shown in Figure \ref{figb1}d.\par
This saturation property has been suggested to give rise to horseshoes in previous studies in other fields \cite{horseshoe_kernel}, and is an unintuitive property that can confound ecological interpretations if not understood properly.  This property also restricts the possible trajectories of samples in the feature space, and gradients cannot be represented by linear trajectories in the real space (Supplemental proof 1). This means that communities in the original high dimensional space do not arrange into linear trajectories in the first place, and when projected to lower dimensions do not fall into linear trajectories.  These trajectories are what we refer to as horseshoes.  The horseshoe phenomenon is analogous to the familiar concept of saturation in molecular evolution, where two randomly evolving sequences saturate at 75\% DNA sequence identity (assuming equal nucleotide frequencies), even if infinite time has elapsed \cite{dna_saturation}. Consequently, distances that reflect a higher degree of molecular change need to be corrected for multiple substitutions in order to recover the molecular clock-like behavior obtained when comparing more similar sequences. This is why corrections according to models such as Jukes-Cantor or the Kimura 2-parameter model are required to obtain distances for reconstructing better phylogenetic trees. Analogous distance corrections are needed in microbial ecology for reconstructing better relationships among microbial communities \cite{evolutionary_distances}.\par
It is important to note that horseshoes do not only arise from \gls{pca}, but also arise in \gls{pcoa} with a variety of distance metrics.  Arch effects have plagued every multidimensional reduction technique we have applied to a wide range of microbial ecology datasets \cite{microbial_patterns}. In the following case studies, we'll show that these distance metrics also have the saturation property.  In addition, if a distance doesn't have this saturation property, there won't be an observed horseshoe artifact (Figure S1).\par
\section*{Case Study 1 - 88 Soils}
In this study, 88 soil samples were obtained from multiple locations across the United States having varying levels of pH \cite{soil_pyro}.  The V4 region of the 16S rRNA gene (16S) within each organism was amplified and sequenced using 454 pyrosequencing to obtain relative abundances of microbial taxa.  A matrix representing abundance values for each taxonomic unit per soil sample was used as input in correspondence analysis (\gls{ca}) \cite{correspondence_analysis}. The resulting ordination showed clear separation of the communities based on pH (Figure \ref{figb2}a), which led to the same conclusion that pH is a major driving factor in soil biogeography, i.e. pH has major impacts on the distribution of bacterial taxonomic units in soil \cite{soil_pyro}.  The \gls{ca} analysis in Figure \ref{figb2}a also shows the classic horseshoe shape.   Here we revisited this study, to better understand the horseshoe shape behind this dataset.\par
To test the effect of another commonly used distance metric on the sample distribution, we analyzed the same soil dataset applying Chi Squared distance (Figure \ref{figb2}b). Similar to what was observed with Euclidean distance, which was applied in the simulation, the Chi Squared distance increased sharply at pH 3 and 4, but began to saturate at pH of 5.  Also the band table similar to what we have observed in Figure \ref{figb2}a can be obtained when sorting.  Also, when the the \gls{otu} table was sorted by sample pH and the mean pH of the samples that the \gls{otu}s were observed in mean pH of the \gls{otu}s (Equation 12), the same band table pattern appeared as we show in Figure \ref{figb2}a.  While the diagonal isn't completely dense, there are more non-zero values compared to the corners of the heatmap. In line with the findings from the original study, this pattern is likely representative of niche differentiation of \gls{otu}s with respect to pH. The organisms that thrive in low pH environments tend not to exist in high pH environments and vice versa.  Low pH and high pH samples are shown in Figure \ref{figb2}c to have few overlapping species, a pattern not observed in the original study as membership was evaluated at coarser levels of taxonomic resolution\cite{soil_pyro}.\par
\begin{figure}[H]
        \centering
        \includegraphics[width=1\textwidth]{ch2/Figure2.pdf}
        \caption[Two case studies show casing how horseshoes can appear in the context of
          soil microbial communities and post-mortem microbial communities.]
        {Two case studies show casing how horseshoes can appear in the context of soil microbial communities and post-mortem microbial communities. (a) Correspondence analysis of 88 soils.  (b) Distance saturation of chi-squared metric, plotting the chi squared distance of the first sample versus all of the other samples. (c) Heatmap of log transformed \gls{otu} counts from the 88 soils with the samples sorted by pH and the \gls{otu}s sorted by mean pH.  (d) Principal Coordinates Analysis of unweighted UniFrac distance.  (e) UniFrac distance of a samples from the last time point versus all of the samples. (f) Heatmap of centred log ratio transformed (Equation 2) \gls{otu} counts sorted by harvest days.\index{SanDiego8}}
        \label{figb2}
\end{figure}
\section*{Case Study 2 - Post Mortem Mice Study}
In this study, 120 mice were sacrificed and allowed to decompose on soil. Mice were destructively sampled over approximately 8 weeks\cite{mammalian_corpse}. 16S sequencing libraries were generated from total DNA extracted from swabs of the skin on the head, and relative abundance values were calculated for each bacterial \gls{otu}. A relative abundance matrix was generated for each library and used as input in \gls{pca}. This analysis generated a clear horseshoe (Figure \ref{figb2}d) using unweighted UniFrac distance \cite{soil_pyro}, with a gradient with respect to the time since death, possibly reflecting a changing skin microbiome during decomposition of the mouse carcass. When the samples were sorted by time since death using a similar strategy as noted above, a band table emerges (Figure \ref{figb2}f). Also, the unweighted UniFrac distance analysis appears to have the same saturation property as observed previously with Euclidean distance and Chi-squared distance.  It is important to note that highest possible UniFrac distance is 1, suggesting that this distance metric can also be saturated.  In Figure \ref{figb2}e, while the distance hasn't completely saturated, these distances are quickly approaching the theoretical maximal UniFrac distance. \par
The striking changes in microbial communities during decomposition are associated with dramatic environmental biochemical changes, including increased pH, ammonia, and total nitrogen, all measured in soil beneath the mice. Correspondingly, microbial communities are predicted to increase in gene abundance of important nitrogen cycling pathways such as amino acid degradation (e.g. glutamate dehydrogenase, lysine decarboxylase, ornithine decarboxylase) and nitrate reduction (e.g. nitrate and nitrite reductase). Bacterial taxa in the families Chromatiaceae (\gls{otu} 46026, 4482362) and Rhizobiaceae (\gls{otu} 4301099) are involved in nitrogen metabolism and become abundant as mouse bodies progress through the stages of decomposition (e.g. Fresh, Active Decay, Advanced Decay).  As shown in Figure S1, all of these \gls{otu}s peak at specific timepoints. The two Chromatiaceae \gls{otu}s peak during Active Decay (bloating and purge of fluids) at 15 days of decomposition. The Rhizobiaceae \gls{otu} peaks during Advanced Decay (sinking and sagging flesh) at 30 days of decomposition and when pH, ammonia, and total nitrogen were measured at their highest levels \cite{mammalian_corpse}.\par
To further validate if saturation leads to horseshoes, a new distance metric \gls{embad} (Earth Mover Band Aware Distance) was engineered to be non-saturating as a proof of concept (Supplemental Methods).  This distance metric uses prior knowledge about the ordering of the band table, and is determined by calculating the flow between two samples. As shown in Figure S1a, sample 1 and sample 2 each have 4 species proportions.  To calculate the distance between sample 1 and sample 2, the probability mass of species 1 and species 2 needs to be shuffled over to species 3 and species 4.  This concept is analogous to computing maximum flow along a pipe, and can be calculated using Earth Mover's distance \cite{emd, unifrac_emd, unifrac}.\par
For the 88 soils (Figure S1b), the \gls{embad} was applied to the pH sorted table. Therefore, even if two samples are not overlapping, samples closer together will have a smaller distance than samples farther apart in the gradient.  This is because the distance is defined to be not saturating and explicitly accounts for the pH gradient.  The same strategy was employed for the postmortem interval mice (Figure S1c), sorting the table by decomposition days. The \gls{pcoa} plots resulting from these applications of \gls{embad} suggest that a non-saturating distance metric could remove the horseshoe effect from lower dimensional projections of these abundances.  This provides further evidence that this saturation property could explain the the horseshoe phenomenon.\par
For the 88 soils study a \gls{permanova} test investigating the difference between soils with a pH less than 3, and soils with a pH greater than 8.  With the \gls{embad} distance metric the \gls{permanova} gave a pseudo F-statistic of 650.5 and a p-value of 0.0003, which has a much larger effect size compared to the original Chi-squared distance metric with a pseudo F-statistic of 3.8 and a p-value of 0.0004 with 9999 permutations.  A similar trend was observed in the post mortem interval mice study when testing the first decomposition day to the last decomposition day using \gls{permanova}.  The \gls{embad} distance metric had a pseudo F-statistic of 439.8 and a p-value of 0.0001 with 9999 permutations, which has a larger effect size than the Unifrac distance metric, which had a pseudo F-statistic of 25.5 and a p-value of 0.0001. This method is relieved from misinterpretations of data due to horseshoes and arches and facilitates the interpretation of taxonomic units along biologically significant gradients that reflect the selective pressure of these factors on the distribution of microbes.  \par
In light of the benefits of engineering a non-saturating distance metric, the \gls{embad} distance metric requires the gradient to be known a priori.  Generalizing this approach in the absence of known gradients is a difficult problem would require an exhaustive using known algorithms.  Specifically, this problem falls under the category of NP-hard problems (Supplemental Proof 2).  In the 88 soils study and the post mortem mice study, we were fortunate to be able to infer the underlying band table with known metadata.  \par
The band patterns we observe here are probably very common in ecology studies investigating species distribution patterns across spatial or temporal gradients. The pattern confirms microbial ecological fundamentals, i.e. bacteria have acquired unique adaptations to the environment and occupy either a broad range or very specific niches. In our case studies of the 88 soils - and the postmortem mice we confirmed that by using a band table pattern analysis approach, bacterial species show different adaptations to pH and bacterial diversity changes over time during decomposition of mice carcasses. The band pattern approach we apply here represents an additional method to visualize differences between microbial communities.\par
Based on our observations here, the horseshoe effect appears in dimensionality reduction techniques due to the saturation property of distance metrics.  While we have only tested a few distance metrics, it is suspected that a vast majority of these distance metrics exhibit the same property, which would also explain why horseshoes are encountered so frequently across many different fields. The saturation property has also been observed in multiple other fields, and other studies from different disciplines have come to similar conclusions \cite{horseshoe_kernel}. In addition, multiple techniques such as self organizing maps \cite{self_organizing_maps} and local linear embedding \cite{local_linear_embedding} attempt to avoid this saturation phenomenon by focusing on distances of nearby points.\par
In spite of the saturation property of distance metrics, identifying horseshoes is still highly useful for identifying patterns concerning niche differentiation.   Properly understanding this phenomenon will motivate the use and development of algorithms such as biclustering \cite{biclustering} to uncover underlying band patterns.  These insights will in effect allow us to identify microbial niches along different environmental conditions.\par
\section{Materials and Methods}
All analyses can be found below on github \\
\url{https://github.com/knightlab-analyses/horseshoe-analyses}.
The mean gradient used for the 2 case studies was calculated as follows.
\begin{equation}
        \overline{g}_{x}=\sum_{i=1}^{N}g_{i}\frac{x_{i}}{\sum_{j=1}^{D}x_{j}}
\end{equation}
Where $x_{i}$ is the proportion of \gls{otu} \textit{x} in sample \textit{i} , $\overline{g}_{x}$ is the mean gradient of \gls{otu} \textit{x}, and $g_{i}$  is the sample gradient at sample i. This calculation can be found in the gneiss package under the function \textbf{mean\_niche\_estimator}. The function used to sort the tables in Figure \ref{figaS1}c used \textbf{niche\_sort}. In the 88 soils study, the table was sorted by sample pH and the mean pH of the samples that the organisms were observed in. In the post mortem mice study, the table was sorted by the days of decomposition and the mean day of the samples of that the organisms were observed in.\par
The heatmap in Figure \ref{figaS2}f and the abundances in Figure S2 were normalized using the centre log ratio \gls{clr} transformation given by the following equation.
\begin{equation}
        clr(x)=\left [ \log \frac{x_{1}}{g(x)},...,\log \frac{x_{D}}{g(x)} \right ]=\log x-\overline{\log x}
\end{equation}
Where $g(x)=\sqrt[n]{\prod_{i=0}^{n}x_{i}}$ is the geometric mean and $\overline{\log x}=\log g(x)=\frac{1}{n}\sum_{i=0}^{n}\log x$ is the average of the log transformed values.  A pseudocount of 1 is added to all of the counts to prevent logarithms of zero occurring. \par
Analyses were performed using Scipy, Numpy, Matplotlib, Seaborn, Scikit-bio and Gneiss.
\section{Acknowledgements}
We thank Noah Fierer for his input on the analysis of the 88 soil samples and Dan Knights for discussion on the \gls{embad} metric. We also acknowledge Amnon Amir and Tomasz Kosciolek for their insights into the horseshoe effect. Finally, we thank Susan Holmes for her insights on previous work understanding the horseshoe effect.

J.T.M. was funded by NSF GRFP DGE-1144086. J.L.M. and R.K. were funded by the Office of Justice Programs National Institute of Justice (award NIJ-2011-DN-BX-K533).

Chapter 2, in full, is a reprint of the material as it appears in
``Uncovering the Horseshoe Effect in Microbial Analyses''
James T. Morton, Liam Toran, Anna Edlund, Jessica L. Metcalf,
Christian Lauber, Rob Knight \emph{mSystems}, 2, 2017.  The dissertation author was the primary investigator and first author of this paper.


%%
%% Template abstract.tex
%%

\chapter*{Abstract}
\label{cha:abstract}
\addcontentsline{toc}{chapter}{Abstract}

Recent sky surveys such as the Sloan Digital Sky Survey and the VST ATLAS Survey have given us a large
amount of data to work with. Spectroscopic labelling, however, is quite expensive, so we only
want to manually label an object if doing so allows us to gain new information. This thesis
explores pool-based active learning and the novel application of prominent active learning heuristics to
the domain of photometric classification.

We begin by applying standard supervised machine learning techniques to two astronomical datasets.
The best-performing classifiers with reliable probability estimates, logistic regression and support
vector machines, are then used to conduct the active learning experiment. Our key original
contribution is the application of Thompson sampling, a Bayesian solution to the exploration vs
exploitation problem, to the selection of six active learning heuristics. To address the problem of
class imbalance, we derive an extension of the posterior balanced accuracy to the
multi-class setting. This is used to evaluate the performance of our algorithms.

The results are very promising. Even under simplistic assumptions like a normally distributed
reward, Thompson sampling manages to automatically identify the optimal heuristic after only 50
examples. In particular, the margin minimisation technique is a clear winner, outperforming random
sampling by as much as 9\% in the VST ATLAS dataset after 300 examples. Being very
computationally efficient, we recommend that astronomers use the margin heuristic with logistic
regression to decide which objects to label next in future sky surveys. To help them with this task,
we have created an open-source and extendable Python package that allows others to easily apply
active learning routines to any dataset and make quick visualisation of photometric data.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "thesis"
%%% End:


\documentclass{article}
\usepackage{amsmath,mathtools,amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{blkarray}
\usepackage{gensymb}
\usepackage{verbatim}
\usepackage{mathrsfs}
\usepackage{bbm}
\usepackage{braket}
\usepackage{hyperref}
\usepackage{verbatim}
\usepackage{cancel}
\usepackage[margin=1.0in]{geometry}
\newcommand{\ol}{\overline}
\newcommand{\lp}{\left(}
\newcommand{\rp}{\right)}
\newcommand{\eps}{\varepsilon}
\newcommand{\lam}{\lambda}
\newcommand{\h}{\circ}
\newcommand{\p}{\bullet}

\newcommand{\Ezero}{E^{(0)}}
\newcommand{\Phizero}{\Phi^{(0)}}
\newcommand{\Eone}{E^{(1)}}
\newcommand{\Phione}{\Phi^{(1)}}

\newcommand{\Ecorr}{E_{\mathrm{corr}}}
\newcommand{\Hc}{H_{\mathrm{c}}}
\newcommand{\dg}{\ensuremath{^\dagger} }
\def\*#1{\mathbf{#1}}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\title{Lecture 5: Perturbation Theory I}
\date{April 1, 2020}
\begin{document}
\maketitle
\noindent
To remind you of the current task at hand, we are trying to find good quality approximations
    to electronic wavefunctions in order to obtain the electronic energy of a molecular system.
That is, we seek to solve the Schr{\"o}dinger equation in the best way possible.
\[H \ket{\Psi} = E \ket{\Psi} \]
We already learned that HF theory is a decent and cheap approximation, and full-CI is exact but very expensive.
Perturbation theory is one of many approaches to finding wavefunctions and energies which are of
    reasonable cost and reasonable accuracy.
Recall, however, that the HF wavefunction $\Phi$ gives us a very good starting point, so we are really just interested in solving for the correlation energy,
which is obtained by shifting our Hamiltonian by the HF energy.
\[\Hc \ket{\Psi} = \Ecorr \ket{\Psi} \]
where
\[\Hc = H - \braket{\Phi| H | \Phi} \]
\[\Ecorr = E_{\mathrm{exact}} - E_{\mathrm{HF}}\]

We will start by discussing Rayleigh-Schr{\"o}dinger perturbation theory (RSPT), which applies the ideas of PT to the time-independent
    Schr{\"o}dinger equation.
Next, we will apply the results of RSPT to derive M{\o}ller-Plesset methods (MP$n$) for approximating the electronic correlation energy of molecular systems.

\section{Rayleigh-Schr{\"o}dinger Perturbation Theory}
Perturbation theory (PT), in the most general sense, is applicable in nearly every domain of physics.
If a problem can be separated into some easily solvable piece A and not-so-easily solvable piece B
    (``the perturbation''), one can approach the exact solution of the problem in terms of the solution to A
    plus a series of corrections to account for the influence of perturbation.
Rayleigh-Schr{\"o}dinger Perturbation Theory (RSPT) applies the ideas of PT to the time-independent
    Schr{\"o}dinger equation.
The Hamiltonian is decomposed into a sum of a simple (easy to solve) problem $H_0$ and a perturbation $V$:
\[H = H_0 + V \]
If the perturbation is ``small'' (i.e., does not wildly shift the solutions of the problem) the
    energy levels/eigenstates associated with the \textit{perturbed system} described by $H$ can be expressed
    as sum of the energy levels/eigenstates of  the \textit{simple system} (described by $H_0$) and a series
    of corrections obtained by considering the influence of the perturbation $V$ on the
    energy levels/eigenstates of the simple system.
Each of these corrections contain eigenvalues of $H_0$ and matrix elements of the perturbation
    between the eigenfunctions of $H_0$, as we will see.
In summary, RSPT is applying age-old ideas of general perturbation theory to the
    time independent Schr{\"o}dinger equation.

The eigenvalue problem we wish to solve is the following:
\[H \ket{\Psi_i} = (H_0 + V) \ket{\Psi_i} = E_i \ket{\Psi_i}\]
We are assuming $H_0$ is a simple, easy-to-solve system, for which we have access to all the eigenstates
    and eigenvalues.
\[H_0 \ket{\Phi_i^{(0)}} = E_i^{(0)} \ket{\Phi_i^{(0)}} \]
These eigenstates $\ket{\Phi_i^{(0)}}$ and eigenvalues $E_i$ for the simple system are hopefully very close
    to the exact results  $\ket{\Psi_i}$ and  $E_i$, that is, the perturbation $V$ is small.
Now, we do something a bit weird.
We introduce a parameter $\lam$ in front of $V$, which allows us to toggle the influence of the
    perturbation on the energy levels and eigenstates.
Before saying much else about $\lam$, let's write it down and see what it allows us to do.
\[(H_0 +  \lam V) \ket{\Psi_i} = E_i \ket{\Psi_i}\]
When $\lam = 0$, our Hamiltonian is just $H_0$, and our eigenstates and energy levels become that
    of the simple system $\ket{\Phi_i^{(0)}}$ and $ E_i^{(0)}$.
When $\lam = 1$, our Hamiltonian is the full Hamiltonian $H$, and our eigenstates and energy levels
    are the perturbed system $\ket{\Psi_i^{(0)}}$ and $E_i$.
By doing this, \textit{the energy levels and eigenstates of the perturbed system are a function
    of }$\lam$.
To put it another way, our energy levels and eigenstates depend on lambda;
    we might express this mathematically as  $E_i(\lam)$ and $\Psi_i(\lam)$.
Since these quantities are \textit{functions of} $\lam$, we can write down a Taylor series
expansion of the energy levels and eigenstates of $H$ in terms of $\lam$:
\[E_i =  E_i^{(0)} +  \lam E_i^{(1)} + \lam^2 E_i^{(2)}  + \cdots \]
\[\ket{\Psi_i} =  \Phi_i^{(0)} +  \lam \Phi_i^{(1)} + \lam^2 \Phi_i^{(2)} + \cdots \]
where each term is,
\[ E_i^{(k)} = \left. \frac{1}{k!} \frac{d^k E_i}{d \lam^k} \right\rvert_{\lam = 0} \]
\[ \Phi_i^{(k)} = \left. \frac{1}{k!} \frac{d^k \ket{\Psi_i}}{d \lam^k} \right\rvert_{\lam = 0} \]

We call $E_i^{(0)}$ the 0$^{th}$ order energy, which is the energy of the simple system.
Every additional term in the energy expansion is the $n^{th}$ order energy correction,
    and these are what we must solve for.
Likewise terminology is used for the eigenstate corrections $\ket{\Phi_i^{(k)}}$

Many textbooks and other resources simply say $\lam$ is a
    ``fictitious dummy parameter'' or ``book-keeping device'' that is
    used to keep track of the  ``orders'' of the terms when working with the perturbation expansion.
This is a bit of an oversimplification. The parameter $\lam$ has the utility of turning
the perturbation on/off, and motivates the use of a Taylor expansion in the first place.
Without $\lam$, you have no Taylor expansion, and no perturbation theory.
While it does serve as a useful label for the orders of each term, it's not just a ``dummy'' parameter.
It's the key to all of perturbation theory.

We have a form for the wavefunction and the energy. Plugging these
into the time-independent Schr{\"o}dinger equation, we get
\[
(H_0 + \lam V) (\Phi_i^{(0)} +  \lam \Phi_i^{(1)} + \lam^2 \Phi_i^{(2)} + \cdots)
=  (E_i^{(0)} +  \lam E_i^{(1)} + \lam^2 E_i^{(2)}  + \cdots ) (\Phi_i^{(0)} +  \lam \Phi_i^{(1)} + \lam^2 \Phi_i^{(2)} + \cdots)
 \]

Subtracting from each side so that the left is 0 and grouping like powers of $\lam$ gives
\begin{align*}
&  \left(H_0 \ket{\Phi_i^{(0)}} - E_i^{(0)} \ket{\Phi_i^{(0)}}   \right) \\
&+ \lam \left( H_0\ket{\Phi_i^{(1)}} + V\ket{\Phi_i^{(0)}} - E_i^{(0)}\ket{\Phi_i^{(1)}} - E_i^{(1)}\ket{\Phi_i^{(0)}} \right)  \\
&+ \lam^2 \left( H_0 \ket{\Phi_i^{(2)}} + V \ket{\Phi_i^{(1)}} - E_i^{(0)} \ket{\Phi_i^{(2)}} - E_i^{(1)} \ket{\Phi_i^{(1)}} - E_i^{(2)} \ket{\Phi_i^{(0)}}  \right)  \\
&+ \cdots \\
&= 0
\end{align*}

The only way the above equation can be satisfied for any arbitrary value of $\lam$ is
    if the coefficient in parentheses for each power of $\lam$ is equal to 0.
Therefore, since each term for each power of $\lam$ is equal to 0,
    this equation is fully separable into an infinite series of simultaneous equations
\begin{align*}
&  H_0 \ket{\Phi_i^{(0)}} = E_i^{(0)} \ket{\Phi_i^{(0)}}   \\
&  H_0\ket{\Phi_i^{(1)}} + V\ket{\Phi_i^{(0)}} = E_i^{(0)}\ket{\Phi_i^{(1)}} + E_i^{(1)}\ket{\Phi_i^{(0)}}  \\
&  H_0 \ket{\Phi_i^{(2)}} + V \ket{\Phi_i^{(1)}} = E_i^{(0)} \ket{\Phi_i^{(2)}} + E_i^{(1)} \ket{\Phi_i^{(1)}} + E_i^{(2)} \ket{\Phi_i^{(0)}}  \\
& \cdots \\
\end{align*}
Notice we have now dropped the $\lam$'s since each of the above equations hold whether or not $\lam^n$ is applied to each term.
You could also interpret this as us setting $\lam=1$, and fully ``turning on'' the perturbation.

We will assume henceforth that our reference state is normalized  $\braket{\Phizero_i | \Phizero_i} = 1$
and our exact wavefunction is intermediately normalized $\braket{\Phizero_i | \Psi_i} = 1$.
This yields,
\[\braket{\Phizero_i | \Psi_i} = 1 = \braket{\Phizero_i | \Phizero_i} + \lam \braket{\Phizero_i | \Phione_i} + \lam^2 \braket{\Phizero_i|\Phi_i^{(2)}} + \cdots \]
In order for the above expression to be true for arbitrary $\lam$,
it must be the case that $\braket{\Phizero_i | \Phi_i^{(k)}} = 0 \space \mathrm{for} \space k = 1,2,3,...$

We can now project each equation in our system of equations on the left by $\bra{\Phi_i^{(0)}}$.
We can simplify quite a bit by noting $\braket{\Phizero_i | \Phi_i^{(k)}} = 0 \space \mathrm{for} \space k = 1,2,3,...$
and that since $H_0$ is Hermitian, we can act it act on the left to give
$ \braket{\Phizero_i | H_0 | \Phi_n^{(k)}   } = \Ezero_i \braket{\Phizero_i | \Phi_i^{(k)}}  $.

The final result is:
\begin{align*}
E_i^{(0)} = \braket{\Phi_i^{(0)} | H_0 | \Phi_i^{(0)}} \\
E_i^{(1)} = \braket{\Phi_i^{(0)} | V | \Phi_i^{(0)}} \\
E_i^{(2)} = \braket{\Phi_i^{(0)} | V | \Phi_i^{(1)}} \\
\end{align*}

Notice that $E_i^{(1)}$ depends on $\Phi_i^{(0)}$, and $E_i^{(2)}$ depends on $\Phi_i^{(1)}$.
This trend continues on for higher order energy corrections:
    the $n^{th}$ order correction to the wavefunction is required for finding the $(n+1)^{th}$ order energy correction.

% Note, algebra in Szabo/Ostlund was archaic, instead used this https://www.lct.jussieu.fr/pagesperso/toulouse/enseignement/introduction_pt_cc.pdf
% combined with some other sources
\subsection{The first-order wavefunction and second-order energy corrections}
The third equation above, the second-order energy ($E_i^{(2)}$) correction, requires you to know the 1st order correction to the wavefunction $\Phione_i$.
To obtain it, we take the equation containing our 1st-order terms and rearrange:

\[ H_0\ket{\Phione_i} + V \ket{\Phizero_i} = \Ezero_i \ket{\Phione_i} + \Eone_i \ket{\Phizero_i} \]
Projecting by $\bra{\Phizero_n}$ for some $n \neq i$ gives

\[ \braket{\Phizero_n | H_0 | \Phione_i} + \braket{ \Phizero_n | V | \Phizero_i} = \Ezero_i \braket{\Phizero_n|\Phione_i} + \Eone_i \braket{\Phizero_n | \Phizero_i} \]

To simplify, note that $\bra{\Phizero_n} H_0 = \Ezero_n \bra{\Phizero_n}$, so the first term on the left becomes $\Ezero_n \braket{\Phizero_n | \Phione_i} $
The second term on the right goes to 0 since $\braket{\Phizero_n|\Phizero_i} = 0 $ when $n \neq i$.
The reason is these are both eigenfunctions of $H_0$, which are orthonormal.
We are left with
\[ \Ezero_n \braket{ \Phizero_n | \Phione_i} + \braket{ \Phizero_n | V | \Phizero_i} = \Ezero_i \braket{ \Phizero_n | \Phione_i }    \]
Rearranging we get
\[ \braket{\Phizero_n | \Phione_i } = -\frac{ \braket{ \Phizero_n | V | \Phizero_i} }{ \Ezero_n - \Ezero_i} \]

We want to get $\ket{\Phione_i}$ by itself on the left side.
The left side above is the expansion coefficients of $ \Phione_i$ in the basis of $\Phizero_n$.
To see this, project each side by $ \ket{\Phizero_n} $ and sum over all $n \neq i$
\[ \sum_{n \neq i} \ket{ \Phizero_n} \braket{\Phizero_n | \Phione_i } = \sum_{n \neq i} -\frac{ \braket{ \Phizero_n | V | \Phizero_i} }{ \Ezero_n - \Ezero_i} \ket{\Phizero_n} \]

The left side above would be equal to $\ket{\Phione_i}$ if the sum was over all $n$ (resolution of the identity),
    but if we sum over all $n$, the energy denominator on the right will be 0 for $n = i$.
Fortunately, the expansion coefficient $\braket{\Phizero_n | \Phione_i } $ for $n = i$ is necessarily 0.
Why? Because of intermediate normalization.
\[\braket{\Phizero_i | \Psi_i} = 1 = \braket{\Phizero_i | \Phizero_i} + \lam \braket{\Phizero_i | \Phione_i} + \lam^2 \braket{\Phizero_i|\Phi_i^{(2)}} + \cdots \]

Since our reference wavefunction is normalized $\braket{\Phizero_i | \Phizero_i} = 1$, in order for this expression to be true for arbitrary $\lam$,
it must be the case that $\braket{\Phizero_i | \Phi_i^{(k)} = 0 \mathrm{for k} = 1,2,3,...}   $
This implies that when $n = i$, the expansion coefficient is 0.
Therefore, we can sum over all $n$ (rather than $n \neq i$) on the left side of our expression for $\ket{\Phione_i}$ without penalty; it is the same as adding zero to both sides:
\[ \sum_{n} \ket{ \Phizero_n} \braket{\Phizero_n | \Phione_i } = \sum_{n \neq i} -\frac{ \braket{ \Phizero_n | V | \Phizero_i} }{ \Ezero_n - \Ezero_i} \ket{\Phizero_n} \]
Noting the left side contains the resolution of the identity, we can just remove it:
\[ \ket{\Phione_i} = \sum_{n \neq i} -\frac{ \braket{ \Phizero_n | V | \Phizero_i} }{ \Ezero_n - \Ezero_i} \ket{\Phizero_n} \]

This now enables us to derive an evaluable expression for the second-order energy
\[ E_i^{(2)} = \braket{\Phi_i^{(0)} | V | \Phi_i^{(1)}} \]
Plugging in the definition for $\Phi_i^{(1)}$ above we obtain
\[ E_i^{(2)} = \sum_{n \neq i} -\frac{ \braket{ \Phizero_n | V | \Phizero_i} \braket{ \Phizero_i | V | \Phizero_n} }{ \Ezero_n - \Ezero_i} \ket{\Phizero_n} \]
\[ E_i^{(2)} = \sum_{n \neq i} \frac{ |\braket{ \Phizero_i | V | \Phizero_n} |^2  }{ \Ezero_i - \Ezero_n} \]

One could keep working through the tedious algebra and derive the expressions for the second order wavefunction, third order wavefunction, etc, and
in turn obtain energy corrections $E_i^{(3)}$, $E_i^{(4)}$, etc, but we will stop here.
The above results are sufficient for deriving second-order M{\o}ller--Plesset perturbation theory.

There does exist a more advanced scheme which generalizes the results of all orders of perturbation theory using a mysterious new thing called ``resolvents''.
We will touch on this at the end of these notes and continue down that path after learning diagrams.

RSPT, as described above, is a completely general scheme, applicable to any application of the time-independent Schrodinger equation.
We will use these ideas to come up with a way to obtain electronic energies with RSPT.

\section{M{\o}ller--Plesset Perturbation Theory}
M{\o}ller--Plesset Perturbation Theory (MP$n$) is the direct application of time-independent RSPT to the problem of finding electronic wavefunctions and energies.
In this section, we will derive MP2 by applying the RSPT equations derived above for a specific partition of our electronic Hamiltonian into $H_0$ and $V$.
The MP2 energy which is correct up to second-order.

We first must divide our Hamiltonian into two parts, one which has a simple solution $H_0$ and the perturbation $V$.
\[H = H_0 + V \]
In MP$n$ methods, the $H_0$ part of the Hamiltonian is taken to be the Hartree-Fock Hamiltonian,
\[H_0 = F = (h_p^q + \bar{g}_{pi}^{qi}) a_q^p = f_p^q a_q^p  \]
Note here we are \textbf{not} using the $\Phi$-normal ordered Fock operator $f_p^q \tilde{a}_q^p$.
The perturbation $V$ will just be everything not included in $H_0$.
\[V = H - F \]
\[V =  \frac{1}{4} \bar{g}_{pq}^{rs} a_{rs}^{pq} - \bar{g}_{pi}^{qi} a_{q}^{p}  \]
One could interpret $V$, the perturbation, as being all of the two-electron interactions, except for the mean-field picture already given by the HF Hamiltonian.

Suppose we seek the energy of the ground state of a system (which we will denote by a subscript 0).
The ground state 0th order wavefunction $\Phi_0^{(0)}$ is the Hartree-Fock Slater determinant wavefunction.
The ground state 0th order energy is given by $E_0^{(0)} = \braket{\Phi_0^{(0)} | H_0 | \Phi_0^{(0)}}$
\begin{align*}
E_0^{(0)} &= \braket{\Phi_0^{(0)} | H_0 | \Phi_0^{(0)}} \\
          &= \sum_i h_{ii} + \sum_{ij} \braket{ij||ij}
\end{align*}
We note the above expression, from Hartree-Fock theory, is equal to the sum of the occupied orbital energies $\sum_i \eps_i$
\[E_0^{(0)} = \sum_i h_{ii} + \sum_{ij} \braket{ij||ij} = \sum_i \eps_i  \]

The 1st order energy correction is given by $E_0^{(1)} = \braket{\Phi_0^{(0)} | V | \Phi_0^{(0)}}$
\begin{align*}
E_0^{(1)} &= \braket{\Phi_0^{(0)} | V | \Phi_0^{(0)}} \\
          &= -\frac{1}{2} \sum_{ij} \braket{ij||ij}
\end{align*}

We notice immediately that the sum of the 0th and 1st order energies is the Hartree-Fock energy.
\[ E_0^{(0)} + E_0^{(1)} = \sum_i h_{ii} + \frac{1}{2}\sum_{ij} \braket{ij||ij}   \]

Thus, the first two terms of our energy perturbation expansion is the HF energy.
\[E_0 =  E_0^{(0)} +  E_0^{(1)} + E_0^{(2)} + \cdots \]
\[E_0 =  E_{\mathrm{HF}} + E_0^{(2)} + \cdots \]
It must be that all higher-order terms constitute \textit{correlation energies}, since in principle the expansion should approach the exact
    energy so long as the perturbation is small.

The second-order energy correction to the ground state energy is given by
\[ E_0^{(2)} = \sum_{n \neq 0} \frac{ |\braket{ \Phizero_0 | V | \Phizero_n} |^2  }{ \Ezero_0 - \Ezero_n}  \]
What sort of states are $\Phizero_n$? They can be any excitation from our ground state reference HF determinant $\Phi_0$ (any determinant in our CI expansion).
However, we find that single excitations do not survive:
\[ \braket{\Phi_0 | V | \Phi_i^a } =  \braket{\Phi_0 | H - H_0 | \Phi_i^a } = \braket{\Phi_0 | H | \Phi_i^a } - \braket{\Phi_0 | H_0 | \Phi_i^a }  = 0 \]
Triple and higher excitations do not survive because you cannot form complete contractions, so $\Phizero_n$ can only be the set of unique doubly excited determinants.
What about the $\Ezero_0$ and $\Ezero_n $?
Recall $\Ezero_0$ is the eigenvalue of $H_0$ acting on our Hartree-Fock determinant,
\[H_0 \ket{\Phi_0^{(0)}} = E_0^{(0)} \ket{\Phi_0^{(0)}} \]
 which is just the sum of the occupied orbital energies $E_0^{(0)} = \sum_k \eps_k$,.
The same is true for $\Ezero_n$, but it has a sum over occupied orbital energies which differs by two spin orbital occupations. Thus,
\[\Ezero_0 - \Ezero_n =  \sum_k \eps_k - ( \sum_k \eps_k - (\eps_i + \eps_j - \eps_a - \eps_b)) \]
\[\Ezero_0 - \Ezero_n =  \eps_i + \eps_j - \eps_a - \eps_b \]

So we finally obtain for the second order energy expression,
\[ E_0^{(2)} = \sum_{n \neq 0} \frac{ |\braket{ \Phizero_0 | V | \Phizero_n} |^2  }{ \Ezero_0 - \Ezero_n}  \]
\[ E_0^{(2)} = \sum_{\substack{i < j \\ a < b}} \frac{ |\braket{ \Phi_0 | V | \Phi_{ij}^{ab}} |^2}{ \eps_i + \eps_j - \eps_a - \eps_b  }  \]
Noting that only the first term of $V$ yields a nonzero result,
\[ E_0^{(2)} = \sum_{\substack{i < j \\ a < b}} \frac{ |\braket{ \Phi_0 | \frac{1}{4} \bar{g}_{pq}^{rs} a_{rs}^{pq} | \Phi_{ij}^{ab}} |^2}{ \eps_i + \eps_j - \eps_a - \eps_b  }  \]
\[ E_0^{(2)} = \frac{1}{16} \sum_{\substack{i < j \\ a < b}}  \frac{ |\braket{ ij || ab}|^2}{ \eps_i + \eps_j - \eps_a - \eps_b}\]
\[ E_0^{(2)} = \frac{1}{4} \sum_{ijab} \frac{ |\braket{ ij || ab}|^2}{ \eps_i + \eps_j - \eps_a - \eps_b}\]

\end{document}


\section{Background}
\label{sec:background}

To give context for DSP-PBE, we first explain the traditional concept of programming by example~\cite{cypher93,lieberman01,synasc12}.
Programming by example (PBE) is a synthesis technique that automatically generates programs that coincide with given examples.
An example is specified as a tuple of input and output values.
Given a set $S= \{(i_1, o_1),\ldots, (i_n, o_n)\}$ of input/output examples, the goal is to automatically derive a program $P$ such that for every $j$, $P(i_j) = o_j$.

PBE is in line with one of the often repeated high level goals of functional programming -- to describe \textit{what} a program should do, and not \textit{how} the program should do it.
Instead of writing code, the user provides a list of relevant examples and the synthesis tool automatically generates a program.
In this way, the examples can be seen as an easily readable and understandable specification.
However, even if the synthesized program satisfies all the provided examples, it still might not correspond to the user's intentions.
Examples are, by nature, an incomplete specification.

PBE is a promising research direction that enables easy manipulation of data even for non-programmers~\cite{GulwaniHS12}.
Recent work in this area has focused on manipulating fundamental data types such as strings~\cite{vldb12,icml13} and lists~\cite{FeserCD15,poseraZ15}.
The success and impact of this line of work can be estimated from the fact that PBE ships as part of the popular Flash Fill feature in Excel 2013~\cite{flashfill}.

The core difference between traditional PBE and DSP-PBE is in the application domain of Digital Signal Processing.
Digital Signal Processing (DSP) programming languages provide users with an interface to build signal processing programs in domain specific languages.
Some of these languages provide their own implementations of signal processing primatives, such as SuperCollider~\cite{supercollider}, CSound~\cite{csound}, and PureData~\cite{puredata}.
Other DSP languages provide alternative front-ends to these languages, such as Vivid~\cite{vivid}, which provides Haskell bindings to Supercollider.

Although many DSP languages are full featured enough to write general purpose programs, in this work we focus on the construction of DSP filters.
A DSP filter is, broadly speaking, any program that transforms a digital signal from one form to another.
An example of a DSP filter is a low-pass filter, which takes an input signal and generates an output signal that keeps frequencies below some frequency threshold, but removes frequencies above that threshold.

The most closely related work in audio signal processing is a technique called resynthesis~\cite{masri1996improved}.
Resynthesis is the process of decomposing a sound into its spectrogram, and then building a synthesizer to recreate a similar sound.
The limitation here is that resynthesis builds a generative synthesizer, which does not take into account any information about the components used to create the original sound.
This limitation means that resynthesis cannot be applied in a new context, whereas DSP-PBE allows us to construct a DSP program that can be used with various new input samples to create novel sounds.
For example, DSP-PBE could be given a sample of a trumpet and a trombone, and the generated DSP program could be applied to a violin to hear what a violin sounds like if it was a trumpet that had been turned into a trombone.
In this case we can discover the analogy \textit{trumpet:trombone :: violin:?}.

From a machine learning perspective, the above example use case is closely related to work on learning analogies~\cite{mikolov2013distributed}, where the goal is to discover relations such as \textit{man:king :: woman:queen}.
To do this, words are embedded in a vector space, so that the transformation from \textit{man} to \textit{king}, can be directly applied to \textit{woman}.
There are two keys differences between this approach and DSP-PBE.
The first is that DSP-PBE should produce a human readable transformation.
We would like to generate DSP programs that can be used verbatim, but also inspected and modified by the user.
While program code provides this readability, vector transformations are not comprehensible in the same way.
Second, word embeddings require that the semantics of an object can be embedded into a vector space.
As we will see in Sec~\ref{sec:distance}, a semantic representation of an audio file (what a human perceives) is not immediately recoverable from its direct representation.


\documentclass[main.tex]{subfiles}
\begin{document}

\marginpar{Friday\\ 2020-4-24, \\ compiled \\ \today}

\section{The Zel'dovich approximation}

So far we have discussed perturbation in two ways: initially, we had a contribution on the RHS of the EFE given by the perturbations.

During radiation dominance there is not much need to go beyond linear theory.

Now we want to go full nonlinear.

We need to use a mixture of techniques: both the study of the 2-body problem and a continuous distribution of matter.
This is what we do if we use numerical techniques.

On the other hand, we can make some smart approximations.

Last semester we mentioned the ``spherical top-hat solution'': we assume spherical symmetry, and work from there.

The Zel'dovich approximation gives an exact solution in the case of planar symmetry.
We can also get exact solutions with cylindrical symmetry.

We come back to the fluid equations: continuity, Euler, Poisson on a FRLW background.

When you do linear perturbation theory, you perturb around the background value.

However, in doing this we are assuming that all the perturbations have the same weight: this is not necessarily true. We should define an internal hierarchy.

What we are doing is formal in the context of Lagrangian perturbation theory.

We define
%
\begin{subequations}
\begin{align}
\eta &= \frac{\rho }{ \rho_{b}}  = 1 + \delta  \\
\vec{u} &= \dv{\vec{x}}{a} = \frac{\vec{v}}{a \dot{a} }  \\
\varphi &= \frac{3 t_{*}^2}{2 a_{*}^3} \phi
\,.
\end{align}
\end{subequations}

This is because he wanted dependence on the growth factor. In EDS this is the scale factor, in general this could be a different thing.

We are referring to a more general background: there can be a growth suppression factor, such that the density perturbation \(\delta \) does not scale with \(a\) but instead based on some function of \(a\).
We should use this function and not the scale factor in general.
In the matter-filled universe we are considering right now this is the same.

The new equations are
%
\begin{subequations}
\begin{align}
\frac{ \DD \vec{u}}{\DD a} + \frac{3}{2a} \vec{u} &= - \frac{3}{2a} \nabla \varphi  \\
\frac{ \DD \eta }{\DD a} + \eta \nabla \cdot \vec{u} &= 0  \\
\nabla^2 \varphi &= \frac{\delta}{a}
\,,
\end{align}
\end{subequations}
%
where
%
\begin{align}
\frac{\DD }{\DD a} = \pdv{}{a} + \vec{u} \cdot \nabla
\,
\end{align}
%
is the convective derivative.

We have chosen this variable \(\vec{u}\) precisely because we want something which is almost constant in linear theory: we have \(\delta \sim t^{2/3}\) and \(v \sim t^{1/3}\) and \(\phi \sim \const\).

So, we get
%
\begin{align}
\pdv{\vec{u}}{a} = 0
\,,
\end{align}
%
so then we can approximate:
%
\begin{align}
\bigd{\vec{u}}{a} = 0
\,.
\end{align}

At the linear level, \(\vec{u} = - \nabla \varphi \).
The argument is that we can \emph{extend} the linear result beyond the linear level. This is because the velocity goes like \(k\), while the potential goes like \(\delta / k^2\).

The system becomes more and more linear as we increase the scale.

Why don't we try to solve the equation by neglecting the nonlinearity in the velocity?

Then the equation system reads:
%
\begin{subequations}
\begin{align}
\bigd{\vec{u}}{a} &= 0  \\
\bigd{\eta }{a} + \eta \nabla \cdot \vec{u} &= 0
\,.
\end{align}
\end{subequations}

This can be solved exactly, as we will now see.

This is describing the inertial motion of particles with no external force acting on them.

It is important to say that this is a total derivative: the variation is zero along the trajectories of the particles.

The velocity of a particle which has a velocity \(\vec{u}_{0}\) at a position \(\vec{q}\) is preserevd. We can integrate the position straightforwardly, since the relation we get is linear.

This, however, is in our weird variables: the real motion is more complicated, but we can map the new variables to the new ones.

We can solve the continuity equation in different ways: we can divide by \(\eta \) to get logarithmic derivatives, so that
%
\begin{align}
\bigd{\log \eta }{a} =  -\nabla \cdot \vec{u}
\,.
\end{align}

when we integrate, though, we should follow the trajectory:
%
\begin{align}
\eta (\vec{x}, a) = \eta_0 (\vec{q}) \exp{ - \int_{a_0 }^{a} \dd{a'} \nabla \cdot \vec{u} \qty[\vec{x}(\vec{q}, a',), a']}
\,.
\end{align}

This calculation is done in `The large scale structure of the universe' by Peebles \cite[]{peeblesLargescaleStructureUniverse1980}.

Also, we can use an approach in which we use the Jacobian of the change of coordinates between Lagrangian and Eulerian.

The tensor
%
\begin{align}
D_{ij} = \pdv[2]{\varphi_0}{q_{i}}{q^{j}}
\,
\end{align}
%
is called the deformation tensor.
The matrix
%
\begin{align}
\pdv{x^{i}}{q_{j}} = \delta^{i}_{j} - a \pdv[2]{\varphi_0}{q_{i}}{q^{j}}
\,
\end{align}
%
defines the transformation between the old and new coordinates.
The eigenvalues of the transformation tensor are local.

It can be shown (Doroshevick 1970) that there is a \SI{92}{\percent} probability that at least one eigenvalue is positive.

This is computed using the assumption that \(\varphi_0 \) has a gaussian distribution.

This is important since we can write
%
\begin{align}
\eta (\vec{x}, a) = \frac{1}{\prod_{i=1}^{3} (1 - \lambda_{i}(\vec{q}) a)}
\,.
\end{align}

Pancakes in terms of shapes, of course, not in terms of ingredients.

The jacobian of the change of coordinates is ill defined if two particles collide.

At that point, we get nonlinearities, which \emph{are} structure formation.
This is a sort of non-gaussianity we get from this approximation.

\end{document}