%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ML Presentation Paper
% Jeremy Lao/John Reynolds
% JJL359, JR4716
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[11pt]{article}

% AMS packages:
\usepackage[fleqn]{amsmath}
\usepackage{amsmath, amsthm, amsfonts}

% Document Formatting Packages:
\usepackage{hyperref}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\newcommand{\vertSpace}[1]{\vspace{3mm}}

%----------------------------------------------------------------
\title{Predicting FOMC Actions using NLP; Assesing the impact of matrix sparsity and regularization}
\author{
        Jeremy Lao - jjl359 \\
        NYU Computer Science \\
            \and
        John Reynolds - jr4716 \\
        NYU Computer Science \\
}

\begin{document}
{\setlength{\mathindent}{0cm}
\maketitle

\abstract{\textit{The Federal Open Market Committee (FOMC) meets throughout the year to set the target Federal Funds rate.  The Federal Funds rate is one of the primary monetary policy tools, and it impacts the cost of borrowing money globally.  This paper explores ML and NLP methods, examines the impact of matrix sparsity from NLP methods and regularization from ML models, to predict the potential outcome of an upcoming FOMC using past FOMC statements and meeting minutes to train ML and NLP models.}}

\section{Introduction}
In this paper we use methods in Natural Language Processing (NLP) and Machine Learning (ML) to predict Federal Open Market Committee (FOMC) rate actions (hold or change) using text from FOMC meeting minutes, Board of Governors speeches, and FOMC post-meeting statements.  \vertSpace

For this analysis we used 713 separate documents (rougly 1.6 Million Words) and 148 Fed Decisions for our data set.  Employing the bag-of-words models have a high dimensionality, meaning the space is extremely sparse with a large amount of features leading to a sparse feature matrix.
In the analysis we conducted some of the feature matricies generated contained over 2 Billion elements with fractions as small as 0.0025 of the elements containing a non-zero value.
Our analysis and techniques used were driven by the issue of feature sizes orders of magnitued larger than the label size and the extreme degrees of sparsness in the feature matrix.
To make sure the analysis was at least plausible we created a simulator to detect positive or negative sentiment from randomly generated text and found as the number of sentiment words increased the prediction accuracy approached close to 1.0.
For the study the analysis hones in on the mix of distributions containing hawkish or dovish(positive) words.

\subsection{git repository for project}
\url{https://www.github.com/jrrpanix/ML9}

\section{Objective}
\begin{enumerate}
\item Since its pretty easy to just blindly apply tools to a set of data we wanted to understand the
characteristics of the output from the text processing functions (in our case CountVectorizer) in terms of number of features, matrix sparsity, matrix norm
that are generated from calls to this function.
\item Assess the effeiveness of Multinomial Niave Bayes and Logistic Lasso applied to problmes where the feature size is orders of magnitues larger than the label size.
\item Determine whether ML and NLP methods can be used to predict FOMC action (no change or a change in the base rate).
\item Because 70$\%$ of the actions are ``no action'', make sure the prediction is not always just ``no action'' (which would give a 70$\%$ accuracy).
\end{enumerate}

\section{Methodology}

\subsection{Data}

The time frame of our study covered 2000 to 2018.
We primarily utilized three sources for our data, FOMC statements, FOMC meeting minutes and Boarde of Governors speeches.
Details on datasources, extraction and scrubbing are left to the appendix to keep the focus on the methods and results.
Preparing the data was a large amount of work and we are not aware of any data sets that combine the three types of documents
and then map the documents to a Fed Action.

\subsection{Feature Modeling and Data Label Mapping}
\noindent Documents are mapped to the first rate decision on or before the document date at time $ti$
where $j=\text{Statement}$ or $j=\text{Speech}$ or $j=\text{Minutes}$
\begin{equation*}
\mbox{Document}_{j,ti} \rightarrow \mbox{Decision}_{ti+k}
 = \begin{cases}
  1, & \text{if  Decision = change}, \\
  0, & \text{if  Decision = no change}
\end{cases}
\end{equation*}

\noindent
\textbf{Model 1: Unstacked}\\
\noindent
The \textbf{Unstacked} model maps each document to a feature vector generated by CountVector given an n-gram($n_1$, $n_2$)
where $n_1 <= n_2$ and $n_i$ are integers $>0$. Given there are approximately 713 documents, this will generate
a feature matrix with 713 rows and $K$ columns where each column will be and instance of the word sequence
generated by the n-gram.

\begin{equation*}
E[D_ti] = f(ngram(n_1, n_2)[\mbox{Document}_{j,ti-k}])
\end{equation*}

\noindent
\textbf{Model 2: Stacked}\\
The \textbf{Stacked}  model maps the set of documents which occurr after the previous decision date but on or before the
next decision date to one rate decision.  This has the effect of contatenating the documents between decisions into
one large document, but cuts down the number from 713 to 148.
where $n_1 <= n_2$ and $n_i$ are integers $>0$. Given there are approximately 713 documents, this will generate
a feature matrix with 713 rows and $K$ columns where each column will be and instance of the word sequence
generated by the n-gram.

\begin{equation*}
E[D_{ti}] = f(ngram(n_1, n_2)[\mbox{Document}_{j1,ti-k1},{Document}_{j2,ti-k2},...]) \text{, } ti-1 < ti-ki < ti+1
\end{equation*}

\subsection{Matrix Sizes and Sparsity from CountVector}

\noindent For this study we used \textbf{CountVectorizer} (\textbf{TfiDf} can also be used, but it did not alter the conclustions of this study).
When an n-gram is applied to corpus combinatorics diticates a large number of features will be generated and for a given feature, most of the column entries will
be zero, resulting in extremly sparse matricies.  The table below gives a measure of $\mbox{sparsity} = \frac{non-zero elements}{total elements}$ and
the resulting number of elements in each n-gram generated matrix in the billions. In general stacking reduces the sparsity by a factor of 4 and
the matrix size by a factor of 5. The L1 matrix norm for all of the matricies ranged between 1200 to 1600.\\

\noindent \begin{tabular}{ |p{2cm}||p{2cm}|p{2cm}|p{2cm}|p{2cm}|  }
 \hline
 \multicolumn{5}{|c|}{CountVector Matrix Size} \\
 \hline
 \multicolumn{1}{|c|}{} &
 \multicolumn{2}{|c|}{Unstacked} &
 \multicolumn{2}{|c|}{Stacked}\\
 \hline
 N-gram & sparsity & size(billions) & sparsity & size(billions)\\
 \hline
       1:1& 0.03628801& 0.008561&  0.10635940&   0.001757 \\
       1:2& 0.00544312& 0.170058&  0.02081416&    0.036107\\
       1:3& 0.00368457& 0.439898&  0.01508941&     0.092869\\
       3:5& 0.00238128& 0.900721&  0.01059293&     0.173040\\
       4:7& 0.00224699& 1.332560&  0.00996789&     0.271117\\
      5:10& 0.00218455& 1.995502&  0.00972276&     0.402041\\
      8:10& 0.00213633& 1.019811&  0.00955894&     0.198627\\
     10:15& 0.00210643& 2.097187&  0.00945586&     0.398312\\
     20:20& 0.00205055& 0.347043&  0.00931157&     0.074799\\
 \hline
\end{tabular}

\subsection{Apply ML to Sparse Matricies generated from CountVectorizer}
\noindent
For this problem the large feature size relative to the the number of labels dicitates looking
at Bayesian approach or some from of L1 regularization (HW1 problem 2) to push a large number
of coefficients to zero.
\begin{itemize}
\item Naive Bayes
\begin{equation*}
P(A|x_1,x_2,...,x_n) = p(A)\prod_{i=1}^n p(x_i|A)
\end{equation*}

\item Logistic Lasso
\begin{equation*}
A = \prod_{i=1}^n p(x_i)^{y_i}(1-p(x_i))^{1-y_i}+ L1
\end{equation*}
\end{itemize}

\subsubsection{Training, Testing and Determining the efficacy of the Models}
For this study we randomly sample 75$\%$ of the corpus for training and then
predict Fed action using the ramining 25$\%$ of the data and calculate
\begin{itemize}
\item Accuracy
\item Precision
\item Recall
\item F1 Score
\end{itemize}
Approximately 70$\%$ of the time there is no Action so a model that does nothing will
have an accuracy of 70$\%$, but a recall of 0.  The results are summarized using F1
score.

\subsubsection{The impact of regularization coefficient $\alpha$ on logistic regression}
\noindent
For \textbf{Logistic Lasso} we are interested in examining the impact of the regularization parameter $\alpha$ on
the fraction of  non-zero coefficients remaining after regularization is applied and on the impact of the predictive
score on test data. The results show the regularization parameter at the extremes produces the worst results
and the stacked model has a higher fraction of coefficients eliminated from logistic lasso. The expected result
is the larger regularization parameter reduces the fraction of non-zero coefficients.\\

\noindent \begin{tabular}{ |p{2cm}||p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|  }
 \hline
 \multicolumn{6}{|c|}{Comparision of Logistic Lasso varying regularization coefficient $\alpha$} \\
 \hline
 \multicolumn{1}{|c|}{} &
 \multicolumn{1}{|c|}{} &
 \multicolumn{2}{|c|}{Unstacked} &
 \multicolumn{2}{|c|}{Stacked}\\
 \hline
 N-gram & $\alpha$ & F1 & frac remain & F1 & frac remain\\
 \hline

10:15 &  3.333 & 0.454 & 0.000040 & 0.720 & 0.000016 \\
10:15 &  2.000 & 0.575 & 0.000066 & 0.897 & 0.000029 \\
10:15 &  1.000 & 0.647 & 0.000148 & 0.893 & 0.000055 \\
10:15 &  0.500 & 0.679 & 0.000291 & 0.913 & 0.000094 \\
10:15 &  0.200 & 0.708 & 0.000339 & 0.913 & 0.000134 \\
10:15 &  0.100 & 0.710 & 0.000463 & 0.923 & 0.000161 \\
10:15 &  0.050 & 0.711 & 0.000619 & 0.925 & 0.000206 \\
10:15 &  0.020 & 0.715 & 0.000764 & 0.923 & 0.000243 \\
10:15 &  0.010 & 0.713 & 0.001045 & 0.913 & 0.000315 \\
10:15 &  0.002 & 0.730 & 0.001659 & 0.906 & 0.000517 \\
10:15 &  0.000 & 0.669 & 0.978049 & 0.669 & 1.000000 \\
 \hline
\end{tabular}

\subsubsection{A comparision of Multinomial Naive Bayes to Logistic Regression with extrem sparse Matricies}
We noticed that under extreme levels of sparseness (sparseness measure of less than $0.003$) from the Matricies
generated by CountVectorizer , Logistic Regression and Logistic Lasso with larger regularization parameters would break down,
while Multinomial Naive Bayes would produce usable results. Logistic Lasso with $\alpha=0.002$ seemed to hold up
well even under extreme sparsness.

\section{Conclusion}
Reducing the sparsity of the matricies generated from CountVectorizer by stacking the documents greatly improved the results.
We were able to attain maximum test accuracy of 95.9$\%$ using a 10:15 n-gram with a recall of 93.4$\%$ and an F1 score of 92.5$\%$.
Because of the number of features generated can be in the millions, Logistic Lasso with small penatly proved to be the most effective
method for reducing the number of coefficients.
The fraction of non-zero coefficients remaining after applying logistic lasso
ranged between 3e-05 to 1.6e-03.  (Compared to .97 with no regularization using logistic regression).
Logistic regression ($\alpha > 0.05$) and Logistic Lasso both broke down at extreme levels of sparsity
(2.5e-03).
In general the Regularization parameter at the extremes produced inferior results.
MultiNomial Naive Bayes with Laplace smoothing at zero proved to be the most stable method for producing useful results at the
most extreme measures of matrix sparcity.  In general no smoothing or large smoothing proved to give the best results with
MultiNomial Naive Bayes. Overall Logistic Lasso with $\alpha = 0.002$ produced the most stable results.

\appendix
\section{Appendix}

\subsection{What is the FOMC}

The FOMC sets the Federal Funds target range (target rate prior to 2009).  The permanent members are the Board of Governors of the Federal Reserve System, President of the Federal Reserve Bank of New York, and the rest of the seats are rotated through the Presidents of the other Federal Reserve Banks.  There are twelve Reserve Banks and the Board of Governors oversees the activity, operations, and policies of the Federal Reserve System.

\subsection{Why Does the Federal Funds Target Range Matter?}

The Federal Funds Target Range is set by the FOMC.  The Federal Funds Target Range is the range where the Federal Funds Effective rate (calculated as the volumn weighted median of eligible transactions reported on FR 2420) is exepected to fix on a daily basis.  The Federal Funds rate is the primary monetary policy instrument of the Federal Reserve System, and it influces the level of interest rates domestically and globally.  For example, if the Federal Funds effective rate were 5 percent, then the interest rate on a 30 year mortgage would be 5 percent or greater.

\subsection{Data}

\subsubsection{Data Sources}
The time frame of our study covered 2000 to 2018.
The primary source of our textual data were FOMC statements, FOMC meeting minutes, and Board of Governors speeches from \url{https://www.federalreserve.gov}.
For FOMC statements between 2000 and 2008 we utilized Stanford's FOMC corpus.
The Stanford FOMC corpus only has text prior to 2008 and it does not have Board of Governor speeches.
The data for the Federal Funds target rate and target range (post 2009) is from FRED St. Louis.
FRED offers a wealth of economic data and information to promote economic education and enhance economic research.
FRED is updated regularly and allows 24/7 access to regional and national financial and economic data.

\subsubsection{Scraping and Text Pre-Processing}

We collected data from the Federal Reserve website and pre-processed the data for the document-term matrix. \vertSpace

We utilized Python packages \textit{beautifulsoup4}, \textit{re}, and \textit{urllib} to scrape contextual data from the Federal Reserve website.  Our scraping algorithm used regular expressions to handle and remove extraneous \textit{html} and \textit{javascript} text collected by the page scraper.  We had to handle non UTF-8 characters upfront and remove them at this stage altogether.  We collected over six hundred documents from the Federal Reserve website. \vertSpace

After scraping the data, we pre-processed the text by removing all punctuation, ensuring proper spacing between words, setting all words to lower case, and making all numbers \textit{d} to reduce the dimensionality document-term matrix.  We also used Regex to detect direct references to the Federal Funds target rate/range and transformed those references (a mixture of numbers, punctuation, and the word 'percent') to \textit{ddpercentrate}.  Since we were modeling the sentiment of FOMC rate action, this was one of our strategies to directly capture the target rate in the document-term matrix. \vertSpace

This formed the bulk of our text pre-processing and it allowed us to essentially utilize the stored text in the document-term matrix for NLP and ML models.

\subsubsection{Meta-Data}
\noindent Below is a table of the FOMC document metadata:

\vertSpace

\noindent \begin{tabular}{ |p{2cm}||p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|  }
 \hline
 \multicolumn{6}{|c|}{FOMC Documents} \\
 \hline
 Document Type& Years & Num Documents & Total Words & Avg Words Per Document & StDev\\
 \hline
 Minutes   & 2000-2018    & 149 & 738,656 & 4,957 & 1,284\\
 Statements &   2000-2018  & 160   & 62,350 & 389 & 185\\
 Speeches & 2011-2018 & 403 & 915,359 & 2,271 & 1,539\\
 \hline
\end{tabular}

\vertSpace

Statements are post-meeting communiqu\'e and are generally short, but contains a summary of the committee's reasoning for the decision.  Minutes are longer and contain the Staff and Committee member current economic assessments and outlooks.  The speeches in the corpus are from the Board of Governors and may contain clues of their thoughts on their current economic outlook.  \vertSpace

\subsection{Latent Dirichlet Allocation}

We explored latent topic modelling in our research to determine how a machine learning algorithm would classify $k=3$ topics, a proxy for the Federal Reserve's three objectives. \vertSpace

Given the Federal Reserve's mandates of stable prices, maximum employment, and financial stability, we explored Latent Dirichlet Distribution of the speeches and minutes' word distribution over three latent topics.  We were interested in exploring how well a topic model would distribute words over the three objectives and if the meaning of the distributions were relatively clear.

\begin{figure}[h!]
  \includegraphics[width=\linewidth]{../output/data_for_graphs/Topic-Model-3-topics.PNG}
  \caption{Top 15 Words Distributed Over Three Latent Topics}
  \label{fig:LDA}
\end{figure}

We found that the topic model generally distributed the speeches and minutes into the three categories fairly well.  It was interesting that "Topic 1" referred solely to financial stability, "Topic 0" primarily referred to inflation and spending, and "Topic 2" returned a word distribution that included labor, growth, and lower for longer monetary policy. \vertSpace

% Bibliography
%-----------------------------------------------------------------
\begin{thebibliography}{99}

\bibitem{Cd94} Hansen,Stephen, McMachon, Michael  \emph{Transparency and Deliberation with the FOMC:a Computational Linquistics Approach}, CEPR Discussion Paper, (2014)

\bibitem{stanford} Puri, Indira \emph{Using Machine Learning to Predict Interest Rate Changes from Federal Reserve Proceedings}

\bibitem{nyu} Ranganath, Rajesh  \emph{NYU - CSCI-GA.2565 ML Lecture Slides Spring 2019}
\end{thebibliography}

\end{document}


\section{Single-Point Search}\label{sec:single_point}
The single-point search selection perturbative hyper-heuristic applies a low-level heuristic - from the heuristic set in table \ref{tab:low-level_heuristics} - \emph{n} times, where \emph{n} has been arbitrarily chosen as the number of exams that need to be scheduled for the problem instance.

To select the low-level heuristic to apply a greedy selection technique is used. So, for each of the \emph{n} iterations, all low-level heuristics are applied to the existing solution to produce 5 new solutions - one for each low-level heuristic. Then, out of these 5 solutions, the solution which violates the least hard constraints is chosen to replace the current solution. Algorithm \ref{alg:single_point} details this single-point search approach in its entirety.

\begin{algorithm}[H]\label{alg:single_point}
\SetAlgoLined
 currentSolution = initialSolution\;
 \BlankLine
 \For{$i\gets1$ \KwTo $n$}{
 \BlankLine
   \ForEach{$heuristic$ in $H$}{
       newSolution = heuristic.applyTo(currentSolution)\;
       \uIf{newSolution.violations() $<$ bestSolution.violations()}{
            bestSolution = newSolution\;
       }
     }
     currentSolution = bestSolution\;
     \BlankLine
 }
 \BlankLine
 return currentSolution\;
 \caption{Single-Point Search Selection Perturbative Hyper-Heuristic}
\end{algorithm}


\appendix
\chapter{Implementation} \label{app:Implementation}
The code for the study, including all scripts and plotted results, is available in a \href{https://github.com/anmolkabra/avicaching-summ17}{repository}.

Both the Identification and the Pricing Problem were programmed in Python 2.7 using NumPy 1.12.1, SciPy 0.19.0 and PyTorch 0.1.12 modules \cite{NPDocs,SCPOptimizeDocs,PTDocs} [results plotted with Matplotlib 2.0.2]. With some code optimizations, the input dataset $\tens{F}$ was built using NumPy's \texttt{ndarray} and PyTorch's \texttt{tensor} functions. Since PyTorch offers NumPy-like code base but with dedicated neural network functions and submodules, PyTorch's \texttt{relu} and \texttt{softmax} functions were used along with other matrix operations.

\section{Specific Implementation Details} \label{app:Specific Implementation Details}
Among all the code optimizations in both models, some in that for the Pricing Problem are worth discussing, as they drastically differ from \Cref{alg:Solving the Pricing Problem} or are intricate. Most optimizations relevant to the Identification Problem are trivial and relate directly to those for the Pricing Problem.

\subsection{Building the Dataset $\tens{F}$} \label{app:Building the Dataset F}
Notice that we build the dataset $\tens{F}$ and batch-multiply it with $\matr{w_1}$ on each iteration/epoch (lines 2-3 of \Cref{alg:Solving the Pricing Problem}). Doing these steps is repetitive as most elements of $\tens{F}$ --- distances $\matr{D}$ and environmental feature vector $\vect{f}$ --- do not change unlike rewards $\vect{r}$. Moreover since $\matr{w_1}$ is fixed, \Cref{alg:Solving the Pricing Problem} would repetitively multiply the $\vect{f}$ and $\matr{D}$ components of $\tens{F}$ with $\matr{w_1}$. To avoid these unnecessary computations, we preprocessed most of $\tens{F}$ by batch-multiplying with $\matr{w_1}$ and only multiplied $\vect{r}$ with the corresponding elements of $\matr{w_1}$. \Cref{fig:Splitting and Batch Multiplying F and w1} describes the process graphically.
\begin{figure}[!htbp]
    \centering
    \includegraphics[width=.9\textwidth]{split_and_batch_multiply}
    \caption[Splitting and Batch Multiplying $\tens{F}$ and $\matr{w_1}$]{Splitting and Batch Multiplying $\tens{F}$ and $\matr{w_1}$: This splitting is possible if one thinks of these 3-D tensors as batches of slices 2-D matrices depicted by the dotted lines. For example, when the top figure is split into horizontal batches, we multiply a $J \times n_F$ with a $n_F \times n_F$ matrix.}
    \label{fig:Splitting and Batch Multiplying F and w1}
\end{figure}

This preprocessing partly applies the Identification Problem's model too. Since the weights $\matr{w_1}$ are updated on each iteration/epoch, we cannot multiply them with parts of $\tens{F}$ beforehand (\Cref{alg:Algorithm for the Identification Problem}). However, we can combine $\matr{D}$ and $\vect{f}$ in the preprocessing stage and simply append $\vect{r}[t]$ on each iteration, reducing execution time.

\subsection{Modeling the Linear Programming Problem in the Standard Format} \label{app:Modeling the Linear Programming Problem in the Standard Format}
The \texttt{scipy.optimize} module's \texttt{linprog} function requires that the arguments are in standard LP format. As discussed in \Cref{sec:Constraining Rewards}, \Cref{eqn:lp_code_constrain_rewards} resembles the standard format more closely than \Cref{eqn:lp_math_constrain_rewards}, but it may not be clear how so.

Considering $\vect{u}$ and $\vect{r'}$ as variables $\vect{x}$, \Cref{eqn:lp_code_constrain_rewards} translates into \Cref{eqn:lp_matrix_rewards} ($J$ is the number of locations).
\begin{equation} \label{eqn:lp_matrix_rewards}
\begin{aligned}
& \text{minimize}
& & \begin{bmatrix}
\vect{0_J}\\
\vect{1_J}\\
\end{bmatrix}^T
\begin{bmatrix}
\vect{r'}\\
\vect{u}
\end{bmatrix}\\ \\
& \text{subject to}
& & \begin{bmatrix}
I_J & -I_J\\
-I_J & -I_J\\
\vect{1}^T_J & \vect{0}^T_J\\
\end{bmatrix}
\begin{bmatrix}
\vect{r'}\\
\vect{u}\\
\end{bmatrix} \leq
\begin{bmatrix}
\vect{r}\\
-\vect{r}\\
\mathcal{R}\\
\end{bmatrix}\\
&&& r'_i, u_i \geq 0
\end{aligned}
\end{equation}

\chapter{GPU Speedup in LP Computation} \label{app:GPU Speedup in LP Computation}
Even though we intentionally transferred the rewards vector to the CPU and constrained it using \texttt{scipy.optimize} module's \texttt{linprog} function, we obtained an unexpected GPU Speedup in the LP's execution (see \Cref{sec:PriProbRes - GPU} and \Cref{fig:Finding Rewards - Time taken by the LP}). Confounded by this weird behavior, we wanted to pinpoint the reason(s). It was clear that \texttt{scipy.optimize.linprog()} could not have differentiated between the configurations and delivered different results. However, since this was not our study's prime motive, we did not take a very rigorous quantitative approach in determining the cause(s).

\section{Possible Reasons for GPU Speedup} \label{app:Possible Reasons for GPU Speedup}
There could have been many reasons for this bizarre behavior, including but not limited to:
\begin{enumerate}
    \item SciPy's Optimize module differentiating between configurations. This can be ruled out because the module could not have known the configuration during which it was called. This is because only PyTorch's tensors are executable on the GPU, whereas NumPy tensors are operable on the CPU only\footnote{We didn't mind SciPy.Optimize using MKL, BLAS or any other linear algebra libraries. Even if it had, there wouldn't be a distinction between CPU and GPU ``set'' configurations!} \cite{PTDocs,NPDocs,SCPOptimizeDocs}. SciPy's Optimize module identifying the configurations is just supernatural.
    \item CPU ``set'' exploiting more main memory than GPU ``set''. We suspected that since CPU ``set'' configuration's operations were executed solely on the CPU, the residing datasets could have used more main memory than when GPU ``set'' was running. This could have hampered the performance of LP with CPU ``set'', as the LP had lesser space to operate in. Unlike the 1\textsuperscript{st} possibility, this would have meant that CPU ``set'' was slowing down the LP, and not that GPU ``set'' was speeding up the LP.
    \item Neural network in CPU ``set'' using more CPU threads than that in GPU ``set''. The Intel i7-7700K processor is quad-core with 8 threads. Since PyTorch uses OpenMP \cite{PTDocs,OpenMP}, a parallel processing API for CPUs, we fancied the neural network to run on simultaneous processors/threads, thus either allowing less available threads for the LP to run, or taking up CPU's resources, or causing latency due to synchronization locks\footnote{\Cref{alg:Solving the Pricing Problem} for the Pricing Problem is sequential in that calculated tensors have to be fully available before LP starts --- a `Fork and Join' synchronization task \cite[Section~2.2]{IssuesMP}.}.
\end{enumerate}

\section{LP Slowing Down or Speeding Up?} \label{app:LP Slowing Down or Speeding Up?}
First we determined whether the LP runtime was being sped up in GPU ``set'' or slowed down in CPU ``set''. To test this, we created a copy of our Pricing Problem's model, which focused only on logging LP runtimes at each epoch. We used 100 locations, 20 time units, and 10 total features including distances and excluding rewards (all randomly initialized with a single seed). For a baseline comparison, we scripted the same LP without the neural network, which gave us the \underline{true} runtimes for the LP (`Only LP' setting), without any involvement of PyTorch modules or functions.

Comparing the former runtimes (CPU and GPU ``set'') with `Only LP' runtime, we observed that the LP in the CPU ``set'' configuration took longer to execute than that in `Only LP' setting during each epoch (\Cref{fig:LP Runtime Example for Different Configurations}). We also noticed little to no interaction between the neural network in GPU ``set'' with the LP, as the runtimes of LP in GPU ``set'' were similar to those of LP in `Only LP' setting. This confirmed that CPU ``set'' was slowing down the LP, and that GPU ``set'' was not speeding it up. But why?
\begin{figure}[!htbp]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
        width=.9\textwidth,
        height=8cm,
        xlabel=Epochs,
        ylabel=Time/s,
        scaled y ticks = false,
        grid=both,
        every axis plot/.append style={very thick},
    ]
    \addplot[red] table [col sep=comma,x=epoch, y=cpuset]{datafiles/lp_time_logs.csv};
    \addlegendentry{CPU ``set''}

    \addplot[blue] table [col sep=comma,x=epoch, y=gpuset]{datafiles/lp_time_logs.csv};
    \addlegendentry{GPU ``set''}

    \addplot[brown] table [col sep=comma,x=epoch, y=onlylp]{datafiles/lp_time_logs.csv};
    \addlegendentry{`Only LP'}

    \end{axis}
    \end{tikzpicture}
    \caption[LP Runtime Example for Different Configurations]{LP Runtime Example for Different Configurations: LPs in both CPU and GPU ``set'' start running slowly, but pick up speed after $\approx 20$ epochs. A possible reason for spikes in CPU ``set'' is given in \Cref{app:Parallelism Doesn't Always Help}. The test was done on a random dataset for 200 epochs.}
    \label{fig:LP Runtime Example for Different Configurations}
\end{figure}

\section{CPU and Main Memory Usage} \label{app:CPU and Main Memory Usage}
While logging the LP runtimes, we also recorded an estimate of the amount of computer resources both configurations were using. Using the \texttt{top} command in Ubuntu, we polled the resource monitor every 0.1 seconds while the python script was running\footnote{\label{foo:logs not epochs} Running processes were polled every 0.1 seconds --- contributing to a `log'. The longer the script ran, the more number of logs collected.}. \Cref{fig:CPU Usage by Different Configurations,fig:Main Memory Usage by Different Configurations} shows how much main memory and CPU resource each setting was using.

\subsubsection{CPU Usage}
We see that CPU ``set'' constantly used more than 4 out of 8 available threads during execution, i.e., $>400\%$ CPU usage, while GPU ``set'' only used a single thread. Also, since we polled at every 0.1 second, and the LP took a minimum of 0.14 seconds (\Cref{fig:LP Runtime Example for Different Configurations}), the data plotted in \Cref{fig:CPU Usage by Different Configurations} must show resource use \textit{while} the LP was running. Considering that the LP in `Only LP' setting only used a single thread ($100\%$), it makes sense that GPU ``set'' would use 1 thread for execution --- the neural network operations were performed on the GPU, leaving the CPU empty for management and LP.
\begin{figure}[!htbp]
    \centering
    \begin{minipage}{.49\textwidth}
        \centering
        \begin{tikzpicture}
        \begin{axis}[
        name=cpuusage-cpuset,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        ymax=800,
        ymin=0,
        ]
        \addplot+[fill=red, opacity=.4] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_cpu_cpuset.csv} \closedcycle;

        \addplot+[very thick, red!50!black] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_cpu_cpuset.csv};

        \end{axis}

        \begin{axis}[
        name=cpuusage-gpuset,
        at=(cpuusage-cpuset.below south west), anchor=above north west,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        ylabel={CPU Usage/\%},
        ymax=200,
        ymin=0,
        ]
        \addplot+[fill=blue, opacity=.4] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_cpu_gpuset.csv} \closedcycle;

        \addplot+[very thick, blue!50!black] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_cpu_gpuset.csv};
        \end{axis}

        \begin{axis}[
        name=cpuusage-onlylp,
        at=(cpuusage-gpuset.below south west), anchor=above north west,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        xlabel={Logs},
        ymax=200,
        ymin=0,
        ]
        \addplot+[fill=green, opacity=.4] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_onlylp.csv} \closedcycle;

        \addplot+[very thick, green!50!black] table [col sep=comma,x=epoch, y=cpu]{datafiles/ext_onlylp.csv};
        \end{axis}
        \end{tikzpicture}
        \caption[CPU Usage by Different Configurations]{CPU Usage by Different Configurations\textsuperscript{\ref{foo:logs not epochs}}: From top --- CPU ``set'', GPU ``set'', `Only LP'. CPU Usage for GPU ``set'' and `Only LP' are very similar as operations other than the LP run on the GPU.}
        \label{fig:CPU Usage by Different Configurations}
    \end{minipage}\hfill
    \begin{minipage}{.49\textwidth}
        \centering
        \begin{tikzpicture}
        \begin{axis}[
        name=memusage-cpuset,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        ymax=1,
        ymin=0,
        ]
        \addplot+[fill=red, opacity=.4] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_cpu_cpuset.csv} \closedcycle;

        \addplot+[very thick, red!50!black] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_cpu_cpuset.csv};

        \end{axis}

        \begin{axis}[
        name=memusage-gpuset,
        at=(memusage-cpuset.below south west), anchor=above north west,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        ylabel={Main Memory Usage/\%},
        ymax=10,
        ymin=0,
        ]
        \addplot+[fill=blue, opacity=.4] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_cpu_gpuset.csv} \closedcycle;

        \addplot+[very thick, blue!50!black] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_cpu_gpuset.csv};
        \end{axis}

        \begin{axis}[
        name=memusage-onlylp,
        at=(memusage-gpuset.below south west), anchor=above north west,
        width=\textwidth,
        enlarge x limits=0.15,
        no markers,
        xlabel={Logs},
        ymax=1,
        ymin=0,
        ]
        \addplot+[fill=green, opacity=.4] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_onlylp.csv} \closedcycle;

        \addplot+[very thick, green!50!black] table [col sep=comma,x=epoch, y=mem]{datafiles/ext_onlylp.csv};
        \end{axis}
        \end{tikzpicture}
        \caption[Main Memory Usage by Different Configurations]{Main Memory Usage by Different Configurations\textsuperscript{\ref{foo:logs not epochs}}: From top --- CPU ``set'', GPU ``set'', `Only LP'. The neural network doesn't occupy much main memory in CPU ``set'' --- could be due to Python/PyTorch's garbage collection.}
        \label{fig:Main Memory Usage by Different Configurations}
    \end{minipage}
\end{figure}

On the other hand, it is apparent that CPU ``set'' had multi-threaded operations running simultaneously (\#3 in \Cref{app:Possible Reasons for GPU Speedup}). Since we know from the `Only LP' setting that the LP only used a single thread, the other threads in CPU ``set'' must have been the neural network. Although this counters our reasoning that the neural network threads should have fully synchronized and terminated before the LP started, it seems that those threads were still active. Therefore, PyTorch and OpenMP continued calculating for the neural network even after the LP had started. Nevertheless, this activity does not impact correctness, as found from optimization tests on CPU ``set''\footnote{CPU ``set'' tests were done for optimization on original datasets to check this. Since we got the same results as obtained for GPU ``set'' optimization tests in \Cref{sec:PriProbRes - Optimization}, the results are not shown in the report.}.

\subsubsection{Main Memory Usage}
GPU ``set'' was using 10 times as much main memory as CPU ``set'' or `Only LP', in addition to storing and operating on tensors in the GPU's internal memory. Not only this is weird, but it is also opposite of what we expected to happen: CPU ``set'' using more main memory and hampering LP performance. It is contradictory that the LP performs better (even as good as `Only LP') on GPU ``set'' if we consider RAM usage as the reason --- GPU ``set'' uses a lot more main memory than CPU ``set''. Clearly, main memory usage cannot be a criterion for assessing LP performance on different configurations.

\section{Parallelism Doesn't Always Help} \label{app:Parallelism Doesn't Always Help}
Now that we had determined that multi-processing/threading might be the root of the strange GPU Speedup, we asked why. Were the simultaneous threads gobbling up valuable CPU resources or were the neural network threads causing latency due to prolonged synchronization delays?

Testing the first possibility was trivial enough --- manually run LPs simultaneously on different threads (in 4 available hyper-threaded cores) and see if they impact each other's performance. If they didn't, multi-threading would prove independent in terms of CPU resources, and there would be no reason to believe that simultaneous threads would eat up each other's CPU resources. After running several instances of the LP on multiple threads and cores, we found no impact of multi-processed scripts on each other when it came to CPU resources. The scripts were unaffected by any other processes on other cores of CPU. One might very well argue that the independence of the LPs in these tests does not highlight the dependence of the LP to neural network in the Pricing Problem, and that these tests are wrongly based. Well, we contend that the argument pertains to the second possibility in the previous paragraph and not the first one.

CPU ``set'' was utilizing more than half of the available 8 threads, and we were concerned if there was latency due to thread synchronization. Since we used \texttt{scipy.optimize.linprog()}'s Simplex solver, which was distinct from PyTorch, this led to a `Fork and Join' task-synchronization requirement \cite[Section~2.2]{IssuesMP} after the neural network was complete. We suspected this causing latency as the LP waited for the neural network to finish. To test this hunch, we ran the scripts with restricted access to CPU threads. Using the \texttt{taskset} command in Ubuntu, we manually forced the script to use specific CPU threads. If CPU ``set'' slowed down even after restriction, then there would not be synchronization delays. Whereas, a gradual increase in execution time with the number of available threads would show that more threads for the neural network create delays in LP's execution.
\begin{figure}[!htbp]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    width=.9\textwidth,
    height=8cm,
    xlabel=Epochs,
    ylabel=Time/s,
    scaled y ticks = false,
    grid=both,
    every axis plot/.append style={very thick},
    ]
    \addplot[red] table [col sep=comma,x=epoch, y=t1]{datafiles/lp_time_logs_threads.csv};
    \addlegendentry{1 Thread}

    \addplot[blue] table [col sep=comma,x=epoch, y=t3]{datafiles/lp_time_logs_threads.csv};
    \addlegendentry{3 Threads}

    \addplot[brown] table [col sep=comma,x=epoch, y=t5]{datafiles/lp_time_logs_threads.csv};
    \addlegendentry{5 Threads}

    \addplot[black] table [col sep=comma,x=epoch, y=t7]{datafiles/lp_time_logs_threads.csv};
    \addlegendentry{7 Threads}

    \end{axis}
    \end{tikzpicture}
    \caption[LP Runtime Example with Restricted Thread Access]{LP Runtime Example with Restricted Thread Access: With only 1 thread accessible to the whole script (neural network and LP), the LP performs as good as GPU ``set'' and `Only LP' setting (\Cref{app:LP Slowing Down or Speeding Up?}). We start seeing erratic synchronization delays for 3 threads, and constant delays with more threads.}
    \label{fig:LP Runtime Example with Restricted Thread Access}
\end{figure}

\Cref{fig:LP Runtime Example with Restricted Thread Access} shows how the access to CPU's threads affected the LP's runtime. Clearly, restricting access to a single thread avoids synchronization delays in the Fork and Join task, allowing the LP to run faster on all epochs. Based on this finding, we restricted the Pricing Problem model's access to the all CPU threads, giving us better CPU ``set'' performance (\Cref{sec:PriProbRes - GPU}) and removing the unexpected GPU Speedup (\Cref{fig:Restricted Finding Rewards - Time taken by the LP,fig:Restricted Finding Rewards - Speedup for LP}).
\begin{figure}[!htbp]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    width=.9\textwidth,
    height=8cm,
    xtick=data,
    xlabel={Batch-Size $J$},
    ylabel={Time/s},
    enlarge y limits=0.15,
    enlarge x limits=0.05,
    y tick label style={/pgf/number format/1000 sep=},
    extra y tick style={grid=major, tick label style={xshift=-1cm}},
    legend style={at={(0.02,.98)},
        anchor=north west},
    nodes near coords,
    every node near coord/.append style={yshift=-0.7cm,/pgf/number format/.cd,fixed zerofill,precision=2}
    ]
    \addplot+ [mark=*,
    nodes near coords=\raisebox{0.8cm}{\pgfmathprintnumber\pgfplotspointmeta},error bars/.cd, y dir=both,y explicit
    ] coordinates {
        (11,0.82) +- (0.08,0.08)
        (37,1.55) +- (0.01,0.01)
        (63,2.66) +- (0.05,0.05)
        (90,4.19) +- (0.03,0.03)
        (116,6.22) +- (0.05,0.05)
        (145,9.93) +- (0.17,0.17)
        (174,11.68) +- (0.08,0.08)
        (203,15.54) +- (0.14,0.14)
        (232,20.56) +- (0.12,0.12)
    };  % no restriction
    \addplot+ [mark=*,error bars/.cd, y dir=both,y explicit] coordinates {
        (11,0.61) +- (0.01,0.01)
        (37,1.09) +- (0.003,0.003)
        (63,1.84) +- (0.01,0.01)
        (90,3.16) +- (0.04,0.04)
        (116,4.69) +- (0.004,0.004)
        (145,7.28) +- (0.02,0.02)
        (174,9.45) +- (0.02,0.02)
        (203,12.51) +- (0.06,0.06)
        (232,16.09) +- (0.04,0.04)
    };  % restricted to 1thread
    \legend{Access to all threads,Restricted to 1 thread}
    \end{axis}
    \end{tikzpicture}
    \caption[Neural Network's Performance (CPU ``set'') with and without Thread Access Restrictions]{Neural Network's Performance (CPU ``set'') with and without Thread Access Restrictions: Data from the Pricing Problem Tests in \Cref{sec:PriProbRes - GPU}. Contrary to one's expectation, using fewer threads increases performance.}
    \label{fig:Neural Network's Performance (CPU ``set'') with and without Thread Access Restrictions}
\end{figure}

Well, does it reduce the neural network's performance (higher runtime due to lower parallelism)? Shockingly, it does the opposite: increase the performance. Explaining this phenomena requires in-depth understanding of PyTorch's implementation with OpenMP, but there can be some possibilities:
\begin{itemize}
    \item More parallelism could result in competition between threads for RAM access \cite[Section~2.1]{IssuesMP}.
    \item There could still be inconspicuous synchronization requirements in the network, caused due to our algorithm's structure.
\end{itemize}
We cannot ensure one of them nor deny any other possibility. This area requires further research and we are open to suggestions. As for the results, we use the restricted access results for CPU ``set'' to calculate GPU Speedups in \Cref{sec:PriProbRes - Optimization}.


\documentclass[preprint,12pt]{elsarticle}

\usepackage{amssymb}

\usepackage{lineno}

\usepackage{url}

\newcommand{\filenamebase}{drones}

\lefthyphenmin=3
\righthyphenmin=2

\usepackage{graphicx,epsfig,verbatim,enumerate}
\usepackage{amssymb,amsmath}
\usepackage{ifthen}

\usepackage{longtable}

\usepackage{mathtools}

\newboolean{twocolswitch}

\newcommand{\avg}[1]{\left\langle#1\right\rangle}
\newcommand{\tavg}[1]{\langle#1\rangle}

\newcommand{\LavgHa}[2]{\tavg{L_{#1,#2}}}
\newcommand{\LavgHaN}[2]{\tavg{L_{#1,#2}}}
\newcommand{\Lavg}{\tavg{L}}

\newcommand{\sindex}[1]{}
\newcommand{\nindex}[1]{}

\newcommand{\etal}{\textit{et al.}}
\newcommand{\www}[1]{\url{#1}}
\newcommand{\req}[1]{(\ref{#1})}
\newcommand{\Req}[1]{Eq.~(\ref{#1})}

% Lettrines
\usepackage{lettrine}

\newcommand{\yes}{}
\newcommand{\no}{}
\newcommand{\tbf}{\textbf}
\newcommand{\tit}{\textit}

\newcommand{\dee}[1]{\mbox{d}#1}
\newcommand{\pdiff}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pdiffsq}[2]{\frac{\partial^2 #1}{{\partial #2}^2}}
\newcommand{\diff}[2]{\frac{{\rm d}#1}{{\rm d}#2}}
\newcommand{\diffsq}[2]{\frac{{\rm d}^{2}#1}{{\rm d} {#2}^2}}
\newcommand{\tdiff}[2]{\mbox{d} #1/\mbox{d} #2}
\newcommand{\tdiffsq}[2]{\mbox{d}^{2} #1/\mbox{d} {#2}^2}
\newcommand{\tpdiff}[2]{\partial #1/\partial #2}
\newcommand{\tpdiffsq}[2]{\partial^2 #1/\partial {#2}^2}
\newcommand{\postdee}[1]{\,\mbox{d}#1}

\usepackage{color}
\newcommand{\todo}[1]{\noindent\textcolor{blue}{{$\Box$ #1}}}

\newcommand{\Prob}[1]{{\rm Pr}\left(#1\right)}

% \newcommand{\kstar}{d^{\ast}}
% \newcommand{\kstari}{k_i^\ast}
\newcommand{\kstar}{d^\ast}
\newcommand{\kstari}{d_i^\ast}

\newcommand{\dstar}{d^\ast}
\newcommand{\dstari}{d_i^\ast}

\newcommand{\phifix}{{\phi^{\ast}}}
\newcommand{\phifixc}{{\phi_c^{\ast}}}
\newcommand{\phifixb}{{\phi_b^{\ast}}}

\newcommand{\phiiactive}{\phi_{i,t}}

\newcommand{\phiiup}{{\phi_{i,\rm on}}}
\newcommand{\phiidown}{{\phi_{i,\rm off}}}

\newcommand{\phiup}{{\phi_{\rm on}}}
\newcommand{\phidown}{{\phi_{\rm off}}}

\newcommand{\Gfun}{G}

\newcommand{\dstardist}{g}
\newcommand{\dosedist}{f}
\newcommand{\dosedistk}{f^{k\star}}

% chaotic contagion
\newcommand{\edgeinfprob}{\rho}
\newcommand{\nodeinfprob}{\phi}

\newcommand{\avgdegree}{k_{\rm avg}}

\newcommand{\stateA}{S_{0}}
\newcommand{\stateB}{S_{1}}

\newcommand{\lcce}{H}

\newcommand{\effectivecount}{f_{q,\textnormal{exp}}}
\newcommand{\effectivecountq}[1]{f_{#1,\textnormal{exp}}}

\newcommand{\simonrho}{\rho}
\newcommand{\zipfexponent}{\alpha}

\newcommand{\partitionprob}{q}
\newcommand{\partitiontype}[1]{$\partitionprob$$=$$#1$}

\newcommand{\onehalf}{\frac{1}{2}}
\newcommand{\onequarter}{\frac{1}{4}}

\newcommand{\clgamma}{\hat{\zipfexponent}}
\newcommand{\xmin}{r_{\textnormal{max}}}
\newcommand{\kstat}{D}
\newcommand{\clp}{\mbox{$p$-value}}
\newcommand{\sigamma}{1-\simonrho}

\journal{Physica A}

\begin{document}

\begin{frontmatter}

\title{Quantitative patterns in drone wars}

\author{
Javier Garcia-Bernardo
}

\ead{javier.garcia-bernado@uvm.edu}

\address{Department of Computer Science,
  The University of Vermont,
  Burlington, VT 05401.}

\author{
Peter Sheridan Dodds
}

\ead{peter.dodds@uvm.edu}

\address{Department of Mathematics \& Statistics,
  Vermont Complex Systems Center,
  Computational Story Lab,
  \& the Vermont Advanced Computing Core,
  The University of Vermont,
  Burlington, VT 05401.}

\author{
Neil F. Johnson
}

\ead{njohnson@physics.miami.edu}

\address{Department of Physics,
University of Miami,
Coral Gables, FL 33124.
}

\begin{abstract}
  Attacks by drones (i.e., unmanned combat air vehicles) continue to generate heated political and ethical debates. Here we examine instead the quantitative nature of drone attacks, focusing on how their intensity and frequency compares to other forms of human conflict. Instead of the power-law distribution found recently for insurgent and terrorist attacks, the severity of attacks is more akin to lognormal and exponential distributions, suggesting that the dynamics underlying drone attacks lie beyond these other forms of human conflict. Meanwhile the pattern in the timing of attacks is consistent with one side having almost complete control. We show that these novel features can be reproduced and understood using a generative mathematical model in which resource allocation to the dominant side is regulated through a feedback loop.

\end{abstract}

\begin{keyword}

warfare \sep drones \sep terrorism \sep scaling

\PACS 89.65.-s \sep 89.75.Da \sep 89.75.Fb \sep 89.75.-k

\end{keyword}

\end{frontmatter}

\linenumbers

\section*{Introduction}
Dating back to physicist L.~F.~Richardson's pioneering
work nearly 100 years ago~\cite{Richardson194}, the quantitative
analysis of human conflict has attracted research interest from across
the social, biological, economic, mathematical and physical
sciences~\cite{Morgenstern2013,Cederman2003,Clauset2009,Clauset,Bohorquez2009a,Dedeo2011}.
As in other human activities~\cite{Barabasi2005,Gabaix2003}, power
laws have been identified in the severity distribution of individual
attacks in insurgencies and
terrorism~\cite{Johnson2013b,Clauset2009,Clauset,Bohorquez2009a}, and
in the temporal trend in
events~\cite{Johnson2013b,Johnson2011a,Clauset}. These studies found
that across a wide range of insurgent wars in which a relatively small
opponent such as an insurgency (Red Queen~\cite{Johnson2011a}) fights
a larger one such as a state (Blue King~\cite{Johnson2011a}), the
probability distribution for the severity $s$---the number of fatalities---of an event (i.e., clash
or attack) is given by $P(s) \propto s^{-\alpha}$ where $\alpha \sim
2.5$, while the trend in the timing of attacks is given by $\tau_{n}
= \tau_1 n^{-b}$, where $\tau_n$ is the time interval
between events $n$ and $n+1$, $n=1,2,\dots$ and $b$ is the escalation
parameter. When $b=0$, the Blue King and Red Queen are evenly matched,
with both effectively running on the same spot---hence the terminology
surrounding the Red Queen~\cite{Johnson2011a}. When $b\neq 0$, there
is an escalation in the frequency of attacks which can be interpreted
as a relative advantage between the Red Queen and the Blue King~\cite{Johnson2011a}. The power-law finding for the distribution
of event severities is consistent with the Red Queen (i.e., insurgent
force) evolving dynamically as a self-organizing system composed of
cells (i.e., clusters) that sporadically either fragment under the
pressure of the Blue King (e.g., state) or coalesce to create larger
cells, and taking the severity of attacks as proportional to the sizes
of the resulting cells~\cite{Bohorquez2009a}.

This paper examines, for the first time, event patterns in the new
form of human conflict offered by unmanned combat air vehicles
(drones)~\cite{wars}. We focus on Pakistan and Yemen because of their
association with drone strike campaigns, using data from the New
America Foundation and the South Asia Terrorism Portal
databases. The situation of drone wars differs from the typical
situation for insurgencies and terrorism in that the attacks are now
carried out by the Blue King on the Red Queen. Moreover, the
sophistication of the action-at-a-distance technology means that any
delay in the Blue King's next attack is likely to have come from a
constraint within Blue itself (e.g., political opposition) as opposed
to any direct counter-adaptation by the Red Queen. Our findings show that drone attacks tend to deviate from the universal patterns observed in the
severity and timing for insurgencies and terrorism, and instead suggest a new regime in which the Blue
King has almost complete control over the conflict. We develop a
generative model in which the timing of attacks is
determined solely by the resources of the Blue King, but are regulated
by a positive feedback loop due to the Blue King's internal
sociopolitical and economic constraints. We show that this simple
model reproduces the main features of the original data and hence
yields novel insight into the unique nature of drone warfare.

\begin{figure*}[tp!]
  \centering
  \includegraphics[width=\textwidth]{drawing-2.pdf}
  \caption{
    \textbf{The severity of drone attacks approximately follows a lognormal distribution.}
    Complementary Cumulative Distribution Function
    (CCDF) of the severity of attacks (blue dots and solid line) and
    best fits to power-law (solid black) and lognormal (dashed green) distributions for drone attacks in Pakistan (A) and Yemen
    (B). The optimal parameters for each distribution are (A)
    Power-law: $\alpha = 4.82$, Log-normal: $\mu = 1.60$ and $\sigma = 0.64$, (B) Power-law:
    $\alpha = 2.21$, Log-normal: $\mu = 1.65$ and $\sigma = 0.77$.
    (C-F)  CCDFs of the severity of attacks and best fits to log-normal distributions. (C-D) The attack size is drawn from a
    normal distribution ($N(\mu,\sigma$) with $\mu$ and $\sigma$ corresponding to (C) the largest value in 100 random numbers drawn from a power-law ($\alpha = 4$) and (D) a random value from a exponential distribution ($\lambda = 5$). (E-F) The attack size is drawn from a normal distribution with $\mu$ and $\sigma^2$ corresponding to (E) the largest value in 100 random numbers drawn from a power-law ($\alpha = 4$) and (F) a random value from a exponential distribution ($\lambda = 5$). The maximum likelihood parameters for the lognormal fits are (C) $\mu = 1.48$ and $\sigma = 0.73$, (D) $\mu = 1.51$ and $\sigma = 0.93$, (E) $\mu = 1.33$ and $\sigma = 0.63$, (F) $\mu = 1.44$ and $\sigma = 0.86$. }
  \label{fig:drones.data}
\end{figure*}

\begin{figure*}[tp!]
  \centering
  \includegraphics[width=\textwidth]{drawing-3.pdf}
  \caption{
    \textbf{The severity of drone attacks approximately follows an exponential distribution.}
    Complementary Cumulative Distribution Function
    (CCDF) of the severity of attacks (blue dots and solid line) and
    best fits to power-law (solid black) and exponential (solid red) distributions for drone attacks in Pakistan (A) and Yemen
    (B). The optimal parameters for each distribution are (A)
    $\lambda = -0.21$ (B) $\lambda = -0.11$.
    (C-F)  CCDFs of the severity of attacks and best fits to exponential distributions. (C-D) The attack size is drawn from a
    normal distribution with $\mu$ and $\sigma$ ($\sigma^2$ for D) corresponding to  a random value from a exponential distribution ($\lambda = 5$). (E-F) The attack size is drawn from a normal distribution with $\mu$ and $\sigma$ ($\sigma$ for F) corresponding to the largest value in 100 random numbers drawn from a power-law ($\alpha = 4$). $\lambda$ is measured from the slope of the least squared fit in semi-log scale and corresponds to (C) $\lambda = -0.13$, (D) $\lambda = -0.17$, (E) $\lambda = -0.15$,(F) $\lambda = -0.17$. }
  \label{fig:drones.dataExp}
\end{figure*}

\begin{figure*}[tp!]
  \centering
  \includegraphics[width=\textwidth]{drawing-1.pdf}
  \caption{
    \textbf{The timing between attacks reveals power-law relationships.}
    (A-B) The severity of the attacks (vertical lines,
    left axis) and their escalation parameter $b$ (right axis) are plotted for a moving window of 50 attacks in (A) Pakistan and (B) Yemen. (C) A simple model of the process. The Blue King's
    resources (funding, units, experience, etc.) influence the
    frequency of attacks. Resources are invested and create a
    positive feedback loop. The civilian population and other
    variables influence the strength
    of this positive feedback. The amount of resources
    available corresponds to \textit{A}, the advantage of the
    Blue side. (D) Symmetric plot to (A--B) using data generated by the model. For the severity of attacks, the size was assumed to be drawn from the largest known group, where the group size is distributed as a power-law with $\alpha = 4$. }
  \label{fig:drones.model}
\end{figure*}

\section*{Results and Discussion}
Figs.~\ref{fig:drones.data}A--B shows the complementary cumulative distribution function
(CCDF) of the severity of drone attacks using the New America Foundation
database. We fit power-law and lognormal
distributions (dashed green and solid black lines respectively; see methods)
for attacks in Pakistan (Figs.~\ref{fig:drones.data}A) and
Yemen (Figs.~\ref{fig:drones.data}B).
We find that the severity of the strikes is approximately described by lognormal
distributions, particularly in the case of Pakistan. In the case of Yemen, for which we have far less data, the lognormal is more tentative with the larger events deviating most. This finding of approximate log-normality is
consistent with the notion that a drone has a specific design and targets (predominantly houses and vehicles) and hence a pre-determined order of magnitude of the range of
destruction and likely severity. This contrasts with attacks by terrorist or insurgent clusters whose size and hence lethality crosses multiple scales, yielding scale-free (power-law) severity distributions. In drone attacks, an approximate lognormal distribution can arise through at least two mechanisms: First, the fact that the severity of the attack is the result of many independent processes (e.g., successful reporting, good visibility, compact target group, etc.) will itself produce a lognormal distribution in the attack size. Second, if we take the uncertainty in the casualty number to scale with the target size, this also produce an approximate lognormal distribution for many underlying distributions of target sizes. For example, suppose attacks target the largest known or available Red group, drawn from a power-law distribution. Setting the mean and standard deviation of a zero-truncated normal distribution to this value, then reproduces the approximate lognormal distribution (Figs.~\ref{fig:drones.data}C). Similarly, we can imagine that most attacks target small groups, where the chances of civilian casualties is lower, and that the probability of targeting larger groups decreases exponentially. Setting the mean and standard deviation to a random value from a exponential distribution again yields an approximate lognormal distribution (Figs.~\ref{fig:drones.data}D). The same pattern is recovered if the standard deviation is set to scale with the square root of the mean (Figs.~\ref{fig:drones.data}E--F). We note that further reduction in the uncertainty of group size increases the weight of the underlying distribution: For the case where the largest known group is targeted, this can explain the fat tail observed for the Yemen data.

Although we have choses to focus on fitting lognormal distributions as the alternative to power laws, other distributions can also provide good fits. For example, the data agrees well with an exponential distribution (see Fig.~\ref{fig:drones.dataExp}A--B). Scenarios where the size of the Red groups is exponentially distributed, as is the case if the probability of joining a group is constant and independent of the number of members, would naturally yield exponential distributions (Fig.~\ref{fig:drones.dataExp}C--D). Approximate exponential distributions can also be achieved if the groups are power-law distributed Fig.~\ref{fig:drones.dataExp}E--F). Our purpose is not to identify the best alternative to a power-law distribution, but to show that in contrast with conventional warfare and terrorism, the data does not follow a power-law distribution and hence feedback processes are not present across all scales.

We now turn to the timing of attacks in order to gain insight into the temporal
dynamics of the Blue King-versus-Red Queen activity. Following
previous work~\cite{Johnson2013b}, we plot the time interval between
consecutive attacks $\tau_n$ as a function of the cardinal number of
the attack $n=1,2,3,\dots$. The escalation parameter $b$ is the
exponent of the power-law fit $\tau_n = \tau_1 n^{-b}$, which will be
the slope of the best-fit line on a double logarithmic plot. In the
organizational development literature in which subsequent events are related to production, this is referred to as a development curve
while in the psychology literature, where subsequent events correspond
to completing a certain task, it is referred to as a learning
curve~\cite{Johnson2011a}. In this sense, the `production' or `completion'
of drone attacks has a natural connection to human activity in these
wider fields. For both Pakistan and Yemen, we find that the
parameter $b$ fails to stabilize around zero
(Figs.~\ref{fig:drones.model}A--B), which is the expected value in a steady state where both sides are adapting well to the opponent's advances.
Instead, the
drone attacks exhibit a large initial escalation (i.e., large positive
$b$) which then transitions to a de-escalation (i.e., large negative
$b$). Given the difficulty for a Red Queen without air defenses to thwart drone attacks
directly, Figs.~\ref{fig:drones.model}A--B suggest that one side (Blue King) effectively
holds complete control for an extended period of time, and that some
internal constraints then arise within the Blue King entity that
eventually de-escalate drone attacks. This is consistent with the decrease in the escalation rate following the closure of a main drone base in 2011 ~\cite{benjamin2013drone}. Interestingly, there is some evidence of Red adaptation to Blue attacks in a recent report by ``The Bureau of Investigative Journalism", which shows a decrease in Red vehicle usage after 2011 corresponding to a peak in attacks on vehicles \url{http://wherethedronesstrike.com/report/76}. This suggests that the Red Queen may be able to limit the severity of attacks.

Fig.~\ref{fig:drones.model}C shows our simple model for explaining these drone attack patterns. This model is of course over-simplified given the wealth of unknowns, yet we believe it is a plausible first step in explaining the empirical observations. We regard the Blue King as possessing
certain resources, for example experience, units, and funding. These
resources degrade over time if no investment is made in the Blue
King's activity, i.e. if the government does not invest in its own
drone development or information research. We assume that if there exists
investment (i.e., funding, time, etc.) then the available resources increase due to a
positive feedback loop, according to the escalation observed $A \propto n^b$,
where $A$ corresponds to the advantage of the Blue King over the Red Queen. Similar feedback loops has been proposed
in models of conventional terrorism~\cite{Clauset2012}, and can be
affected by external agents, for example public opinion or budget changes. For simplicity we take the
frequency of attacks as directly proportional to the resource level,
while the severity of the attack is independent of resources.

These minimal features are able to replicate the drone strike data
(Figs.~\ref{fig:drones.data}C--F,Figs.~\ref{fig:drones.data}D, is achieved if the
resources increase as a power-law --- hence this is only sustainable for
short periods of time. A constant $b<0$, corresponding to the
frequency of attacks decreasing continuously, is achieved if the
resources decrease continuously, i.e. when there is little or no
investment. Assuming that each drone acts individually, that the attack
severity varies slowly with the available resources (which is in turn
consistent with some form of adaptation by the Red side) and that an increase in precision
requires significant amounts
of development effort, we are able to recreate approximate
lognormal and exponential distributions for the severity of attacks.

In summary, our analysis reveals and helps explain patterns in the severity and
timing of attacks in drone wars, which themselves represent a new form
of action-at-a-distance human conflict. We have purposely stepped
aside from issues of ethics or technology, choosing instead to focus
on the event data since they represent a quantitative measure of drone
war impact. We have shown that a simple model, in which the production of
drones evolves from a shared pool of resources controlled by a
feedback loop, is able to recreate the original data and therefore
explain the overall dynamics of the Blue King's drone campaign.
Going forward, our model could be also used to
explore how wars might unfold when drones are used
by two or more sides in conflicts.

\section*{Methods}
We obtained all data from the New America database:
\url{http://securitydata.newamerica.net/.newamerica.net/} and crosschecked with the South Asia Terrorism Portal
database:
\url{http://www.satp.org/satporgtp/countries/pakistan/}.

We obtained the best fit to power-law distributions following Clauset et
al.~\cite{Clauset2009}. We fitted lognormal distributions using the
maximum likelihood estimators. For the escalation rate analysis, $\tau_n
= \tau_1 n^{-b}$, we plotted the number of attack vs. the time between
attacks on a log-log scale. We used a rolling window of 50 attacks and accepted every value of $b$ that
allowed for a correlation greater than 20\%, which allows us to
measure fast transitions.

We simulated 200 attacks with our
model. The initial advantage was set to $1$. The time to the
next attack is equal to $323 \cdot A^{-1}$, where the 323 mimics $t_0$ for the Pakistan conflict. At every step (attack) the advantage
of the Blue King changed by the factor
$\left((n+1)/n\right)^b$, where $n$ is the attack number.
For the first 50 attacks
$b = 0.5$;
for the last 50 attacks
$b = -0.5$.

\section*{Acknowledgments}
PSD was supported by NSF CAREER Grant No. 0846668.

\bibliographystyle{elsarticle-num}
\begin{thebibliography}{10}
\expandafter\ifx\csname url\endcsname\relax
  \def\url#1{\texttt{#1}}\fi
\expandafter\ifx\csname urlprefix\endcsname\relax\def\urlprefix{URL }\fi
\expandafter\ifx\csname href\endcsname\relax
  \def\href#1#2{#2} \def\path#1{#1}\fi

\bibitem{Richardson194}
L.~F. Richardson, {Variation of the frequency of fatal quarrels with
  magnitude}, Journal of the American Statistical Association 43~(224) (1948)
  523--546.

\bibitem{Morgenstern2013}
A.~P. Morgenstern, N.~Velasquez, P.~Manrique, Q.~Hong, N.~Johnson, N.~Johnson,
  {Resource Letter MPCVW-1: Modeling Political Conflict, Violence, and Wars: A
  Survey}, American Journal of Physics 81~(11) (2013) 805.

\bibitem{Cederman2003}
L.-E. Cederman, {Modeling the Size of Wars: From Billiard Balls to Sandpiles},
  American Political Science Review 97~(01) (2003) 135.

\bibitem{Clauset2009}
A.~Clauset, C.~R. Shalizi, M.~E.~J. Newman, {Power-law distributions in
  empirical data}, SIAM Review 51~(4) (2009) 661--703.

\bibitem{Clauset}
A.~Clauset, M.~Young, K.~Gleditsch, {On the frequency of severe terrorist
  events}, Journal of Conflict Resolution 51~(1) (2007) 58--88.

\bibitem{Bohorquez2009a}
J.~C. Bohorquez, S.~Gourley, A.~R. Dixon, M.~Spagat, N.~F. Johnson, Common
  ecology quantifies human insurgency, Nature 462 (2009) 911--914.

\bibitem{Dedeo2011}
S.~Dedeo, D.~Krakauer, J.~Flack, {Evidence of strategic periodicities in
  collective conflict dynamics.}, Journal of the Royal Society Interface 8~(62)
  (2011) 1260--73.

\bibitem{Barabasi2005}
A.-L. Barab\'{a}si, {The origin of bursts and heavy tails in human dynamics.},
  Nature 435~(7039) (2005) 207--11.

\bibitem{Gabaix2003}
X.~Gabaix, P.~Gopikrishnan, V.~Plerou, H.~E. Stanley, {A theory of power-law
  distributions in financial market fluctuations.}, Nature 423~(6937) (2003)
  267--70.

\bibitem{Johnson2013b}
N.~F. Johnson, P.~Medina, G.~Zhao, D.~S. Messinger, J.~Horgan, P.~Gill, J.~C.
  Bohorquez, W.~Mattson, D.~Gangi, H.~Qi, P.~Manrique, N.~Velasquez,
  A.~Morgenstern, E.~Restrepo, N.~Johnson, M.~Spagat, R.~Zarama, {Simple
  mathematical law benchmarks human confrontations.}, Scientific Reports 3
  (2013) 3463.

\bibitem{Johnson2011a}
N.~Johnson, S.~Carran, J.~Botner, K.~Fontaine, N.~Laxague, P.~Nuetzel,
  J.~Turnley, B.~Tivnan, {Pattern in escalations in insurgent and terrorist
  activity.}, Science Magazine 333~(6038) (2011) 81--4.

\bibitem{wars}
A.~R. Hall, C.~J. Coyne, {The political economy of drones}, Defence and Peace
  Economics 25~(5) (2013) 445--460.

\bibitem{benjamin2013drone}
M.~Benjamin, Drone warfare: Killing by remote control, Verso Books, 2013.

\bibitem{Clauset2012}
A.~Clauset, K.~S. Gleditsch, {The developmental dynamics of terrorist
  organizations.}, PloS ONE 7~(11) (2012) e48633.

\end{thebibliography}

\end{document}


\section{How fast will Raft elect a leader with no split votes?}
\label{leaderelection:nosplit}

\begin{figure}
\centering
\includegraphics[scale=.55]{leaderelection/timeline}
\vcaption[leader election timeline with no split votes]{
Timeline of a typical election when no split vote occurs.
The first candidate to time out successfully collects votes and
completes the election (other elections may not be so fortunate). The
figure is drawn to scale assuming the election timeouts are chosen from
a range between 10--20 times the cluster's one-way network latency.
\\
The ``old leader heartbeats'' row shows the final heartbeat that the
old leader completes, and when it would have sent its next heartbeats
were it not to crash.
\\
The ``old leader crash'' row shows the interval during which the old
leader crashes. This time is assumed to follow a uniform random
distribution within its heartbeat interval. The vertical line halfway
through the interval is its expected (average) value.
\\
The ``base election timeout'' row shows the interval during which all
the followers await additional heartbeats from the old leader.
\\
The ``election timeout range'' row shows the interval during which the
servers would time out and start elections to replace the old leader.
The vertical lines show expected earliest timeout values for different
numbers of remaining servers (eight, four, and two, respectively).
\\
The ``requests for votes'' row shows when the candidate sends its
RequestVote RPCs to the other servers and receives their votes.
\\
The ``new leader heartbeats'' row shows the new leader sending out
heartbeat RPCs right away after becoming leader, then periodically
thereafter.
}
\label{fig:leaderelection:nosplit:timeline}
\end{figure}

The most common case for leader election in Raft is when no split vote
occurs, and this section analyzes how long it takes to elect a leader
under that assumption. This is expected to be the normal case for Raft
clusters; if the cluster is configured correctly, most normal elections
will not encounter a split vote. The first server to time out will be
able to collect votes from a majority of the cluster and become leader.
The timeline of events is shown in
Figure~\ref{fig:leaderelection:nosplit:timeline}.

\begin{table}
\centering
\begin{tabular}{ccl}
variable & type & meaning \\
\hline
\noalign{\vskip .75ex}
$s$     & natural & number of available servers \\
$n$     & natural & size of full cluster (including unavailable servers) \\
$c$     & natural & number of servers to time out near each other \\
$l$     & time & constant half round trip network latency (special case of $L$)\\
$L$     & random variable of time & half round trip network latency \\
$W$     & random variable of time & time to write term and vote durably to disk \\
$T_i$   & random variable of time & timeout of server $i$ \\
$M_s$   & random variable of time & earliest timeout of $s$ servers \\
$D_{c,s}$ & random variable of time & difference in timeouts of earliest $c$ of $s$ servers \\
$E_s$   & random variable of time & time to complete an election \\
\end{tabular}
\vcaption[summary of variables]{
Summary of the variables used throughout this chapter to
analyze leader election performance.
Times are normalized to the election timeout range (ranging from 0 to
1).
}
\label{tab:leaderelection:variables}
\end{table}

\begin{figure}
\centering
\includegraphics[scale=.5]{leaderelection/earliesttimeoutdiagram}
\vcaption[earliest timeout example]{
What is the smallest random election timeout value chosen by $s$
servers? The diagram shows random election timeouts a five-server
cluster where one server has failed ($s=4$). $t_{(1)}$ is the smallest
timeout value chosen.
}
\label{fig:leaderelection:theory:earliesttimeoutdiagram}
\end{figure}

With no split votes, the time it takes to elect a leader is determined
by how long it takes the first server to time out. The question of when
it will time out is illustrated in
Figure~\ref{fig:leaderelection:theory:earliesttimeoutdiagram}. Each server
waits for a uniform random timeout after the last time it received a
heartbeat. Intuitively, any individual server is expected to time out
halfway through the election timeout range, but with more servers it
becomes more likely that the first server will time out sooner.

We now define the problem more precisely and derive when the first
server times out analytically. The variables defined in this chapter are
summarized in Table~\ref{tab:leaderelection:variables}. Suppose each
server chooses its timeouts randomly from the standard
uniform distribution (in the range $[0,1]$). Let $T_1 \ldots T_s$ be
random variables representing when each of $s$ servers times out. Let $M_s$ be the minimum of
$T_1 \ldots T_s$, a random variable representing the time the first
server times out. Its cumulative distribution function (CDF) defines the
probability that $M_s$ is no greater than a particular time, $t$. This
is equivalent to one minus the probability that all servers times out
after $t$:
\begin{align*}
\Pr(M_s \leq t)
        &= 1 - \Pr(M_s > t) \\
        &= 1 - \prod_{i=1}^s \Pr(T_i > t) \\
        &= 1 - \prod_{i=1}^s (1 - t) \\
        &= 1 - (1-t)^s
\end{align*}
%
For example, consider a cluster with five servers where the prior
leader has failed.
The probability that the earliest of the remaining four servers times out
sometime in the first quarter of the election timeout range is
$\Pr(M_4 \leq \frac{1}{4}) = 1 - (1 - \frac{1}{4})^4 \approx 0.68$.
The CDF is graphed in
Figure~\ref{fig:leaderelection:theory:model:earliesttimeout}
for various values of $s$.

\begin{figure}
\centering
\includegraphics{leaderelection/earliesttimeout}
\vspace{-2ex}
\vcaption[earliest timeout CDF]{
The graph shows the probability that the earliest server times out
before $t$ when different numbers of servers are available. The point on
each line shows the time when the first server is expected to time out
($\Ex[M_s]$).
}
\label{fig:leaderelection:theory:model:earliesttimeout}
\end{figure}

The probability density function (PDF) of $M_s$ is the derivative of the
CDF:
\begin{align*}
 f_{M_s}(t) &= \frac{d}{dt} \Pr(M_s \leq t) \\
        &= \frac{d}{dt} (1-(1-t)^s) \\
        &= -\frac{d}{dt} (1-t)^s \\
        &= s (1-t)^{s-1}
\end{align*}

The expected value (mean) of $M_s$ is calculated from the PDF:
\begin{align*}
 \Ex[M_s] &= \int_0^1 \! t \, f_{M_s}(t) \, dt \\
          &= \int_0^1 \! t (s (1-t)^{s-1}) \, dt \\
          &= \left. \! -\frac{(1-t)^s (s\,t+1)}{s+1} \right|_{t=0}^1 \\
          &= \frac{1}{s+1}
\end{align*}

\noindent
For example, with four available servers, the first timeout is expected
to occur $\dfrac{1}{5}^\textrm{th}$ of the way through the election
timeout range. Fortunately, this very simple expression is a good
estimate of Raft's overall election performance, since elections
complete soon after the first candidate times out when no split vote
occurs.

More precisely, if there is no split vote, the full election requires
a candidate to time out and request votes, once the leader crashes:
\begin{align*}
E_s &= \text{baseline election timeout} + M_s + \text{time to request
votes} - \text{heartbeat adjustment} \\
E_s &= 1 + M_s + 2L + W - U(0,\dfrac{1}{2}) \\
\Ex[E_s] &= 1 + \frac{1}{s+1} + 2\Ex[L] + \Ex[W] - \dfrac{1}{4}
\end{align*}
where election timeouts are chosen from the range $[1,2]$,
$L$ is the network latency, and $W$ is the time to write the votes
persistently to disk.
A uniform random time value from the range $[0, \dfrac{1}{2}]$ is
subtracted, since leaders are expected to crash randomly within their
heartbeat intervals rather than immediately after sending heartbeats.


\chapter{Legendre expansions of energy-angle probability densities
in the center-of-mass frame}
\label{Ch:Legendre-cm}

Energy-angle probability density data in \xendl\ may also be given
as Legendre coefficients for the expansion Eq.~(\ref{piLegendre})
with outgoing energy $E$ and direction cosine~$\mu$ in the center-of-mass
frame.  In this case,
the data consists of tables of coefficients $\pi_\ell( \Ecm' \mid E)$
for the sum
\begin{equation}
  \pi( \Ecm', \mucm \mid E) = \sum_\ell
  \left(
    \ell + \frac{1}{2}
  \right)
  \pi_\ell( \Ecm' \mid E) P_\ell( \mucm )
 \label{pi-Legendre-cm}
\end{equation}
for a set of outgoing energies~$\Ecm'$ at incident energies~$E$.
The number of terms in the sum in Eq.~(\ref{pi-Legendre-cm})
is determined by the data.

The analysis given in this section is also applicable to the case
of isotropic energy probability densities given
in the center-of-mass frame.  The data then consist only of
values of the $\pi_0( \Ecm' \mid E)$ term in Eq.~(\ref{pi-Legendre-cm}).

For incident energies $E$ between the tabulated values,
the coefficients $\pi_\ell( \Ecm' \mid E)$ are obtained by one of
the interpolation methods discussed in Section~\ref{Sec:2d-interp}.

For the probability density $\pi( \Ecm', \mucm \mid E) $ in
Eq.~(\ref{pi-Legendre-cm}), the integral Eq.~(\ref{Inum})
for computing the number-preserving transfer matrix becomes
\begin{equation}
  \Inum_{gh,\ell} =
         \int_{\calE_g} dE \, \sigma ( E ) M(E) w(E) \widetilde \phi_\ell(E)
       \, \int_{ \calD_{h, \text{cm} }} d \Ecm' \, d\mucm \,
             P_\ell( \mulab ) \pi( \Ecm', \mucm \mid E) ,
 \label{Inum-Legendre-cm}
\end{equation}
where $\calD_{h, \text{cm}}$ is the set of outgoing energies $\Ecm'$
and direction cosines $\mucm$ which are mapped into $\calE_h'$
under the boost to the laboratory frame for incident particles with energy~$E$.

Figure~\ref{Fig:boost-regions} illustrates the portion of the region $\calD_{h, \text{cm} }$
for one incident energy generated by a range of outgoing energies
corresponding to the data
\begin{equation}
  E'_{\text{cm}, j-1} \le \Ecm' \le  E'_{\text{cm}, j}.
 \label{calDh-energy-range}
\end{equation}
In this figure the outgoing energy bin $\calE_h'$ in the laboratory frame
 is a half annulus
centered at the origin with radii corresponding to the upper and
lower boundaries of the energy bin.
The vector $\Vtrans$ is the velocity of the center of mass
with magnitude $\vtrans$ as in Eq.~(\ref{Vtrans-length}).
The range of outgoing center-of-mass energies in Eq.~(\ref{calDh-energy-range})
produces the second half annulus in Figure~8-1, and its contribution to
the set $\calD_{h, \text{cm} }$
is the intersection of these two half annuli and is shaded dark gray.
This dark gray set displays the outgoing energies $\Ecm'$ in the center-of-mass frame
which satisfy Eq.~(\ref{calDh-energy-range}) and the direction cosines
$\mucm$ such that the energy $E$ of the outgoing particle in the laboratory
frame is in the bin~$\calE_h'$.  In this figure, the upper limit of $\calE_h'$ is
indicated by the arc $\Elab' = \Ebin'$.

\begin{figure}
\input{fig8-1}
\end{figure}

\section{Geometrical considerations}
\label{Sec:boost-geometry}
The first question in the analysis of the integral in Eq.~(\ref{Inum-Legendre-cm})
is the determination of the set~$\calD_{h, \text{cm} }$.  This requires
knowing whether or not the arc for a bin boundary $\Elab' = \Ebin'$
intersects an arc $\Ecm' = E'_{\text{cm},j}$ derived from a data point.  For a boost
to the laboratory frame using Newtonian mechanics as in Eq.~(\ref{E_lab}),
this identification is achieved by the function
\begin{equation}
  G_0( \Ebin',  \Ecm', E ) = 2 \Ebin'( \Etrans' + \Ecm' ) -
    ( \Etrans' - \Ecm' )^2 - {\Ebin'}^2.
  \label{def-G0}
\end{equation}
Note that in $G_0$ the dependence on the energy $E$ of the
incident particle typically enters in two ways.  For one thing,
$\Etrans'$ depends on $E$ as
in Eq.~(\ref{E_trans}).  On the other hand, if the interpolation with
respect to incident energy is unit-base or by cumulative
points, then the inversion of the unit-base map Eq.~(\ref{range-inv})
takes the form
\begin{equation}
  \Ecm' = E'_{\text{cm}, \text{min}} +
     ( E'_{\text{cm}, \text{max}} - E'_{\text{cm}, \text{min}} )
    \widehat \Ecm'.
 \label{Kalb-unit-base}
\end{equation}
In linear-linear unit-base interpolation, $\widehat \Ecm'$ is fixed in
the interval $0 \le \widehat \Ecm' \le 1$, while
$E'_{\text{cm}, \text{min}}$
and $E'_{\text{cm}, \text{max}}$ depend on $E$ according to
Eq.~(\ref{EoutRange}) with $q$ given by Eq.~(\ref{def-q}).

The utility of the function $G_0$ in Eq.~(\ref{Kalb-unit-base})
depends on the following result.

\subsection{Assertion}\label{Sec:assertion8}
\textit{In Figure~\ref{Fig:boost-regions} under a Newtonian boost at fixed incident energy $E$,
an arc $\Elab' = \Ebin'$ representing an edge of an energy bin in
the laboratory frame intersects an arc
$\Ecm' = {\rm const}$ generated by data
in the center-of-mass frame  if and only if}
\begin{equation}
   G_0( \Ebin', \Ecm', E ) \ge 0.
  \label{G0-pos}
\end{equation}

This assertion is proved in Appendix~\ref{Sec:Appendix-B}.

One application of Assertion~\ref{Sec:assertion8} is that of finding the incident energies
$E$ in $\calE_g$ in the integral Eq.~(\ref{Inum-Legendre-cm}) such that
the set $\calD_{h, \text{cm} }$ is non-empty.  This may be done by
locating the zeros of $G_0( \Ebin',  \Ecm', E )$ as a function of $E$
with the edges of the bin $\calE_h'$ as values of $\Ebin'$ and with
$\Ecm'$ as in Eq.~(\ref{Kalb-unit-base}) for
$$
  \widehat \Ecm' = \widehat E'_{\text{cm}, j-1}
  \quad \text{and} \quad
  \widehat \Ecm' = \widehat E'_{\text{cm}, j},
$$
according to the data.

\section{Input of Legendre coefficients of energy-angle probability
densities in the center-of-mass frame}
The format for input of the coefficients $\pi_\ell( \Ecm' \mid E)$
in Eq.~(\ref{pi-Legendre-cm}) is that of Section~\ref{Sec:ENDF-I4-data}
with some obvious modifications.  For one thing, the data are in
the center-of-mass frame\\
 \Input{Product Frame: CenterOfMass}{}

The other difference is that information on particle masses is
required by the boost to the laboratory frame\\
  \Input{Projectile's mass:}{$\myi$} \\
 \Input{Target's mass:}{$\mtarg$} \\
 \Input{Product's mass:}{$\myo$} \\
 \Input{Reaction's Q value:}{$Q$} \\
The values of these quantities must be in the same units as
the energy bin boundaries.

The code computes the mass of the residual from the $Q$ value and
the masses of the other particles.  If the input file also contains the line\\
 \Input{Residual's mass:}{$\mres$}\\
the code compares this value with the mass it computed, printing
a warning message if they are significantly different.

Currently, the boost for this type of data is only implemented using Newtonian mechanics.

\section{Input of isotropic energy probability
densities in the center-of-mass frame}
\label{Sec:isotropic-table-cm}

The format for isotropic energy probability
density data given in the center-of-mass frame is the
same as that for laboratory-frame data in
Section~\ref{Sec:isotropic-table-lab}, except that
the line\\
  \Input{Product Frame: lab}{}\\
is replaced by\\
 \Input{Product Frame: CenterOfMass}{}