\section{Results}
\label{chp:study:sec:results}
In this chapter we present the results of the executed study which we base the later discussion upon.
Specifically, we state the calculated metrics of the two runs.

First we look at the metrics of the first run in which we use the crowdsourced dataset $D_{crowd}$.
Here all three models achieve mostly similar metrics when classifying $D_{crowd_{test}}$.
All metrics are listed in \cref{tab:study:results:first_run} for each model.
\begin{table}[htpb]
    \centering
    \begin{tabular}{l | l l l l }
        \toprule
         Model & Precision & Recall & $F_1$ Score & Average Precision \\
        \midrule
        \ac{BERT} & $0.36$ & $0.82$ & $0.51$ & $0.50$\\
        \ac{DistilBERT} & $0.31$ & $0.79$ & $0.45$ & $0.47$\\
        \ac{ERNIE2.0} & $0.48$ & $0.52$ & $0.50$ & $0.47$\\
        \bottomrule
    \end{tabular}
    \caption[Study Results on Crowdsourced Dataset]{The results for the crowdsourced dataset $D_{crowd_{test}}$.}\label{tab:study:results:first_run}
\end{table}

As we describe in \cref{chp:study:sec:execution}, we performed a second run similar to the first one.
The only difference is that the more extensive dataset $D_{all_{train}}$ was used for the training and $D_{all_{test}}$ to calculate the resulting metrics correspondingly.
The results of this second run are shown in \cref{tab:study:results:second_run}.
\begin{table}[htpb]
    \centering
    \begin{tabular}{l | l l l l }
        \toprule
         Model & Precision & Recall & $F_1$ Score & Average Precision \\
        \midrule
        \ac{BERT} & $0.45$ & $0.53$ & $0.48$ & $0.46$\\
        \ac{DistilBERT} & $0.33$ & $0.71$ & $0.48$ & $0.46$\\
        \ac{ERNIE2.0} & $0.35$ & $0.85$ & $0.50$ & $0.43$\\
        \bottomrule
    \end{tabular}
    \caption[Study Results on Complete Dataset]{The results for the dataset $D_{all_{test}}$.}\label{tab:study:results:second_run}
\end{table}


\section{Delegation}
\label{sec:delegation}

An agent owning a key that can sign new blocks can delegate its signing rights
to another key by means of \textit{delegation certificates}. These certificates
are included in the ledger, and therefore also included in the body of the
blocks in the blockchain.

There are several restrictions on a certificate posted on the blockchain:
\begin{enumerate}
\item Only genesis keys can delegate.
\item Certificates must be properly signed by the delegator.
\item Any given key can delegate at most once per-epoch.
\item Any given key can issue at most one certificate in a given slot.
\item The epochs in the certificates must refer to the current or to the next
  epoch. We do not want to allow certificates from past epochs so that a
  delegation certificate cannot be replayed. On the other hand if we allow
  certificates with arbitrary future epochs, then a malicious key can issue a
  delegation certificate per-slot, setting the epoch to a sufficiently large
  value. This will cause a blow up in the size of the ledger state since we
  will not be able to clean $\var{eks}$ (we only clean past epochs). Also note
  that we do not check the relation between the certificate epoch and the slot
  in which the certificate becomes active. This would bring additional
  complexity without any obvious benefit.
\item Certificates do not become active immediately, but they require a certain
  number of slots till they become stable in all the nodes.
\end{enumerate}
These conditions are formalized in \cref{fig:rules:delegation-scheduling}.
Rule~\ref{eq:rule:delegation-scheduling} determines when a certificate can
become ``scheduled''. The definitions used in these rules are presented in
\cref{fig:defs:delegation-scheduling}, and the types of the system induced by
$\trans{sdeleg}{\wcard}$ are presented in
\cref{fig:ts-types:delegation-scheduling}. Here and in the remaining rules we
will be using $k$ as an abstract constant that gives us the chain stability
parameter.

\begin{figure}[htb]
  \emph{Abstract types}
  \begin{equation*}
    \begin{array}{r@{~\in~}lr}
      c & \DCert & \text{delegation certificate}\\
      \var{vk_g} & \VKeyGen & \text{genesis verification key}\\
    \end{array}
  \end{equation*}

  \emph{Derived types}
  \begin{equation*}
    \begin{array}{r@{~\in~}l@{\qquad=\qquad}r@{~\in~}lr}
      \var{e} & \Epoch & n & \mathbb{N} & \text{epoch}\\
      \var{s} & \Slot & s & \mathbb{N} & \text{slot}\\
      \var{d} & \SlotCount & s & \mathbb{N} & \text{slot}
    \end{array}
  \end{equation*}

  \emph{Constraints}
  \begin{align*}
    \VKeyGen \subseteq \VKey
  \end{align*}

  \emph{Abstract functions}
  \begin{equation*}
    \begin{array}{r@{~\in~}lr}
      \fun{dbody} & \DCert \to (\VKey \times \Epoch)
      & \text{body of the delegation certificate}\\
      \fun{dwit} & \DCert \to (\VKeyGen \times \Sig)
      & \text{witness for the delegation certificate}\\
      \fun{dwho} & \DCert \mapsto (\VKeyGen \times \VKey)
      & \text{who delegates to whom in the certificate}\\
      \fun{depoch} & \DCert \mapsto \Epoch
      & \text{certificate epoch}\\
      \var{k} & \mathbb{N} & \text{chain stability parameter}
    \end{array}
  \end{equation*}
  \caption{Delegation scheduling definitions}
  \label{fig:defs:delegation-scheduling}
\end{figure}

\begin{figure}[htb]
  \emph{Delegation scheduling environments}
  \begin{equation*}
    \DSEnv =
    \left(
      \begin{array}{r@{~\in~}lr}
        \mathcal{K} & \powerset{\VKeyGen} & \text{allowed delegators}\\
        \var{e} & \Epoch & \text{epoch}\\
        \var{s} & \Slot & \text{slot}\\
      \end{array}
    \right)
  \end{equation*}

  \emph{Delegation scheduling states}
  \begin{equation*}
    \DSState
    = \left(
      \begin{array}{r@{~\in~}lr}
        \var{sds} & \seqof{(\Slot \times (\VKeyGen \times \VKey))} & \text{scheduled delegations}\\
        \var{eks} & \powerset{(\Epoch \times \VKeyGen)} & \text{key-epoch delegations}
      \end{array}
    \right)
  \end{equation*}

  \emph{Delegation scheduling transitions}
  \begin{equation*}
    \var{\_} \vdash
    \var{\_} \trans{sdeleg}{\_} \var{\_}
    \subseteq \powerset (\DSEnv \times \DSState \times \DCert \times \DSState)
  \end{equation*}
  \caption{Delegation scheduling transition-system types}
  \label{fig:ts-types:delegation-scheduling}
\end{figure}

\begin{figure}[htb]
  \begin{equation}
    \label{eq:sdeleg-bootstrap}
    \inference
    {
      \var{sds_0} \leteq \epsilon
      &
      \var{eks_0} \leteq \emptyset
    }
    {
      {\left(\begin{array}{l}
       \mathcal{K}\\
        e\\
        s
      \end{array}\right)}
      \vdash
      \trans{sdeleg}{}
      \left(
        \begin{array}{l}
          \var{sds_0}\\
          \var{eks_0}
        \end{array}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:delegation-scheduling}
    \inference
    {
      (\var{vk_s},~ \sigma) \leteq \dwit{c}
      & \verify{vk_s}{\serialised{\dbody{c}}}{\sigma} & vk_s \in \mathcal{K}\\ ~ \\
      (\var{vk_s},~ \var{vk_d}) \leteq \dwho{c} & e_d \leteq \depoch{c}
      & (e_d,~ \var{vk_s}) \notin \var{eks} & 0 \leq e_d - e \leq 1 \\ ~ \\
      d \leteq 2 \cdot k & (s + d,~ (\var{vk_s},~ \wcard)) \notin \var{sds}\\
    }
    {
      {\left(\begin{array}{l}
       \mathcal{K}\\
        e\\
        s
      \end{array}\right)}
      \vdash
      {
        \left(
          \begin{array}{l}
            \var{sds}\\
            \var{eks}
          \end{array}
        \right)
      }
      \trans{sdeleg}{c}
      {
        \left(
          \begin{array}{l}
            \var{sds}; (s + d,~ (\var{vk_s},~ \var{vk_d}))\\
            \var{eks} \cup \{(e_d,~ \var{vk_s})\}
          \end{array}
        \right)
      }
    }
  \end{equation}
  \caption{Delegation scheduling rules}
  \label{fig:rules:delegation-scheduling}
\end{figure}

\clearpage

The rules in Figure~\ref{fig:rules:delegation} model the activation of
delegation certificates. Once a scheduled certificate becomes active
(see~\cref{sec:delegation-interface-rules}), the delegation map is changed by
it only if:
\begin{itemize}
\item The delegating key ($\var{vk_s}$) did not activate a delegation
  certificate in a slot greater or equal than the certificate slot ($s$). This
  check is performed to avoid having the constraint that the delegation
  certificates have to be activated in slot order.
\item The key being delegated to ($\var{vk_d}$) has not been delegated by
  another key (injectivity constraint).
\end{itemize}
The reason why we check that the delegation map is injective is to avoid a
potential risk (during the OBFT era) in which a malicious node gets control of
a genesis key $\var{vk_m}$ that issued the maximum number of blocks in a given
window. By delegating to another key $\var{vk_d}$, which was already delegated to
by some other key $\var{vk_g}$, the malicious node could prevent $\var{vk_g}$
from issuing blocks. Even though the delegation certificates take several slots
to become effective, the malicious node could calculate when the certificate
would become active, and issue a delegation certificate at the right time.

As an additional advantage, by having an injective delegation map, we are able
to simplify our specification when it comes to counting the blocks issued by
(delegates of) genesis keys.

Note also, that we could not impose the injectivity constraint in
Rule~\ref{eq:rule:delegation-scheduling} since we do not have information about
the delegations that will become effective. We could of course detect a
violation in the injectivity constraint when scheduling a delegation
certificate, but this will lead to a complex computation and larger state in
said rule.

Finally, note that we do not want to reject a scheduled delegation that would
violate the injectivity constraint (since delegation might not have been
scheduled by the node issuing the block). Instead, we simply ignore the
delegation certificate (Rule~\ref{eq:rule:delegation-nop}).

\begin{figure}[htb]
  \begin{align*}
    & \unionoverrideRight \in (A \mapsto B) \to (A \mapsto B) \to (A \mapsto B)
    & \text{union override}\\
    & d_0 \unionoverrideRight d_1 = d_1 \cup (\dom d_1 \subtractdom d_0)
  \end{align*}
  \caption{Functions used in delegation rules}
  \label{fig:funcs:delegation}
\end{figure}

\begin{figure}[htb]
  \emph{Delegation environments}
  \begin{equation*}
    \DEnv =
    \left(
      \begin{array}{r@{~\in~}lr}
        \mathcal{K} & \powerset{\VKeyGen} & \text{allowed delegators}
      \end{array}
    \right)
  \end{equation*}

  \emph{Delegation states}
  \begin{align*}
    & \DState
      = \left(
        \begin{array}{r@{~\in~}lr}
          \var{dms} & \VKeyGen \mapsto \VKey & \text{delegation map}\\
          \var{dws} & \VKeyGen \mapsto \Slot & \text{when last delegation occurred}\\
        \end{array}\right)
  \end{align*}
  \emph{Delegation transitions}
  \begin{equation*}
    \_ \vdash \_ \trans{adeleg}{\_} \_ \in
    \powerset (\DEnv \times \DState \times (\Slot \times (\VKeyGen \times \VKey)) \times \DState)
    \end{equation*}
  \caption{Delegation transition-system types}
  \label{fig:ts-types:delegation}
\end{figure}

\begin{figure}[htb]
  \begin{equation}
    \label{eq:adeleg-bootstrap}
    \inference
    {
      \var{dms_0} \leteq \Set{k \mapsto k}{k \in \mathcal{K}} &
      \var{dws_0} \leteq \Set{k \mapsto 0}{k \in \mathcal{K}}
    }
    {
      \left(
        \mathcal{K}
      \right)
      \vdash
      \trans{adeleg}{}
      \left(
        \begin{array}{l}
          \var{dms_0}\\
          \var{dws_0}
        \end{array}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}\label{eq:rule:delegation-change}
    \inference
    {
      \var{vk_d} \notin \range~\var{dms} & (\var{vk_s} \mapsto s_p \in \var{dws} \Rightarrow s_p < s)
    }
    {
      \left(\mathcal{K}\right)
      \vdash
      \left(
      \begin{array}{r}
        \var{dms}\\
        \var{dws}
      \end{array}
      \right)
      \trans{adeleg}{(s,~ (vk_s,~ vk_d))}
      \left(
      \begin{array}{lcl}
        \var{dms} & \unionoverrideRight & \{\var{vk_s} \mapsto \var{vk_d}\}\\
        \var{dws} & \unionoverrideRight & \{\var{vk_s} \mapsto s \}
      \end{array}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}\label{eq:rule:delegation-nop}
    \inference
    {\var{vk_d} \in \range~\var{dms} \vee (\var{vk_s} \mapsto s_p  \in \var{dws}  \wedge s \leq s_p)
    }
    {
      \left(\mathcal{K}\right)
      \vdash
      \left(
      \begin{array}{r}
        \var{dms}\\
        \var{dws}
      \end{array}
      \right)
      \trans{adeleg}{(s,~ (\var{vk_s},~ \var{vk_d}))}
      \left(
      \begin{array}{lcl}
        \var{dms}\\
        \var{dws}
      \end{array}
      \right)
    }
  \end{equation}
  \caption{Delegation inference rules}
  \label{fig:rules:delegation}
\end{figure}

\clearpage

\subsection{Delegation sequences}
\label{sec:delegation-sequences}

This section presents the rules that model the effect that sequences of
delegations have on the ledger.

\begin{figure}[htb]
  \begin{equation}
    \inference
    {
      {\begin{array}{l}
         \var{delegEnv}
      \end{array}}
      \vdash
      \trans{\hyperref[eq:sdeleg-bootstrap]{sdeleg}}{}
      \var{delegSt}
    }
    {
      {\begin{array}{l}
         \var{delegEnv}
      \end{array}}
      \vdash
      \trans{sdelegs}{}
      \var{delegSt}
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:delegation-scheduling-seq-base}
    \inference
    {}
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{sdelegs}{\epsilon}
      \var{delegSt}
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:delegation-scheduling-seq-ind}
    \inference
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{sdelegs}{\Gamma}
      \var{delegSt'}
      &
      \var{delegEnv}
      \vdash
      \var{delegSt'}
      \trans{\hyperref[fig:rules:delegation-scheduling]{sdeleg}}{c}
      \var{delegSt''}
    }
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{sdelegs}{\Gamma; c}
      \var{delegSt''}
    }
  \end{equation}
  \caption{Delegation scheduling sequence rules}
  \label{fig:rules:delegation-scheduling-seq}
\end{figure}

\begin{figure}
  \begin{equation}
    \inference
    {
      {\begin{array}{l}
         \var{delegEnv}
      \end{array}}
      \vdash
      \trans{\hyperref[eq:adeleg-bootstrap]{adeleg}}{}
      \var{delegSt}
    }
    {
      {\begin{array}{l}
         \var{delegEnv}
      \end{array}}
      \vdash
      \trans{adelegs}{}
      \var{delegSt}
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:delegation-seq-base}
    \inference
    {}
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{adelegs}{\epsilon}
      \var{delegSt}
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:delegation-seq-ind}
    \inference
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{adelegs}{\Gamma}
      \var{delegSt'}
      &
      \var{delegEnv}
      \vdash
      \var{delegSt'}
      \trans{\hyperref[fig:rules:delegation]{adeleg}}{c}
      \var{delegSt''}
    }
    {
      \var{delegEnv}
      \vdash
      \var{delegSt}
      \trans{adelegs}{\Gamma; c}
      \var{delegSt''}
    }
  \end{equation}
  \caption{Delegations sequence rules}
  \label{fig:rules:delegation-seq}
\end{figure}

\subsection{Deviation from the \texttt{cardano-sl} implementation}
\label{sec:delegation:deviation-cardano-sl-imp}

In the \texttt{cardano-sl} implementation, the block issuer needs to include a
delegation certificate in the block, which witness the fact that a genesis key
gave the issuer the rights of issuing blocks on behalf of this genesis key. The
reasons why this was implemented in this way in \texttt{cardano-sl} are not
clear, since the delegation certificates are posted on the chain, so the ledger
state contains the information about who delegates to whom. Hence in the
current specification we use a heavyweight delegation scheme, i.e. where the
certificates are posted on the chain, but an implementation of this rules that
aims at being compatible with the implementation in \texttt{cardano-sl} has to
take the fact that delegation certificates are also present in a block into
account.


\documentclass[prb,preprint]{revtex4-1}
% The line above defines the type of LaTeX document.
% Note that AJP uses the same style as Phys. Rev. B (prb).

% The % character begins a comment, which continues to the end of the line.

\usepackage{amsmath}  % needed for \tfrac, \bmatrix, etc.
\usepackage{amsfonts} % needed for bold Greek, Fraktur, and blackboard bold
\usepackage{graphicx}
\usepackage{amssymb} % needed for figures

\DeclareMathOperator{\dd}{d\!}
\DeclareMathOperator{\ddd}{\mathrm{d}}

\begin{document}

% Be sure to use the \title, \author, \affiliation, and \abstract macros
% to format your title page.  Don't use lower-level macros to  manually
% adjust the fonts and centering.

\title{Demystifying the Lagrangian of classical mechanics} %Derivation and Motivation of the Lagrangian of Classical Mechanics
% In a long title you can use \\ to force a line break at a certain location.

\author{Gerd Wagner}
\email{gerdhwagner@t-online.de} % optional
\affiliation{Mayener Str. 131, 56070 Koblenz, Germany} % optional second address
% If there were a second author at the same address, we would put another
% \author{} statement here.  Don't combine multiple authors in a single
% \author statement.
%\affiliation{mailing address}
% Please provide a full mailing address here.

\author{Matthew W. Guthrie}
\email{matthew.guthrie@ucf.edu}
\affiliation{Department of Physics, University of Central Florida, Orlando, FL 32816}

% See the REVTeX documentation for more examples of author and affiliation lists.

\date{\today}

\begin{abstract}
The Lagrangian formulation of classical mechanics is extremely useful for a vast array of physics problems encountered in the undergraduate and graduate physics curriculum. Unfortunately, many treatments of this topic lack explanations of the most basic details that make Lagrangian mechanics so practical. In this paper, we detail the steps taken to arrive at the principle of stationary action, the Euler-Lagrange equations, and the Lagrangian of classical mechanics. These steps are: 1) the calculation of the minimal distance between two points in a plane, to introduce the variational principle and to derive the Euler-Lagrange equation; 2) a straightforward reformulation of Newton's second law in the form of Euler-Lagrange equations and formulation of the principle of stationary action; and 3) proving that Euler-Lagrange equations are independent of arbitrary coordinate transformations and motivating that this independence is desirable for classical mechanics. This paper is targeted toward the advanced undergraduate student who, like our own experiences, struggles with details which are not seen as crucial to the utilization of the tools developed by Lagrangian mechanics, and is especially frustrated by the question ``\textit{why} is the Lagrangian always kinetic minus potential energy?'' We answer this question in a simple and approachable manner.
\end{abstract}

\maketitle

\section{Introduction}\label{introduction}

Lagrangian formulation of mechanics is a powerful description of classical mechanics. To some students, Lagrangian mechanics can be seemingly separate from the more familiar Newtonian formulation. Many undergraduate students who are taking a classical mechanics course have difficulties understanding how and why the Lagrangian formulation is an equivalent description of motion. Significant learning difficulty arises from the presentation of the material when students first encounter Lagrangian mechanics. Lagrangian mechanics presents an entirely new way for students to think about physics and this shift in thinking can be difficult. Often through a lack of class time to cover material, many of the treatments of the subject in university courses and popular textbooks relies on incomplete arguments, especially when considering the Lagrangian ($L$) itself. It is difficult to understand why the Lagrangian takes the intriguing form $L=T-V$, the difference between a particle's kinetic ($T$) and potential ($V$) energies (which are functions of position \boldmath$r$\unboldmath, velocity \boldmath$\dot{r}$\unboldmath, and time $t$).

The most popular undergraduate level treatments of the Lagrangian simply accept the definition as it is given and move on to using it to solve problems. Taylor~\cite[p.~238]{taylor2005classical} includes the explicit statement that the reader is ``certainly entitled to ask why the quantity $T-U$ should be of any interest''\footnote{Taylor uses $U$ for the potential energy function.} and continues to say that ``there seems to be no simple answer to this question except that it is.'' Marion ~\cite[p.~198-199]{marion1970classical} and Folwes and Cassiday~\cite[p.~393]{fowles1999analytical} merely define the Lagrangian and move to its utility. Gregory~\cite[p.~348]{gregory2006classical} explains the Lagrangian in further detail, including a detailed derivation, although the information is presented over multiple sections and chapters with numerous digressions.

In our treatment presented in this paper, the Lagrangian formalism arises naturally through an introductory example from geometry. We then utilize the formalism to reformulate Newton's second law in the form of the Euler-Lagrange equation. To motivate the new formulation of Newton's second law, we prove the invariance of the Euler-Lagrange equation under arbitrary coordinate transformations. To make clear why this property is important for physics, we remind the reader that the laws of nature do not depend on the coordinates we use to describe them. On the other hand, physicists cannot formulate laws without coordinates and physical equations usually look different in different coordinates\footnote{Coordinate-free laws of physics do exist (see Maxwell's equations written in the language of differential forms or Einstein's equations). However, these formulations are not very useful until a coordinate system is chosen.}. This is why the relation of physical theories to coordinates should be as well defined and restricted as possible. The Lagrangian formalism fulfills this demand through the Euler-Lagrange equations being independent of coordinate transformations. Lagrangian functionals do depend on coordinates but in the simplest way physicists can think of: they transform like scalars.

Thinking about the relation of the laws of physics to coordinates proved fruitful in the past.
Probably the most famous example is Einstein's first paper on special relativity~\cite{EinsteinSpecialRelativity}.
In this paper we follow this tradition, reformulating Newton's second law in the form of Euler-Lagrange equations. As a result, the Lagrangian of classical mechanics appears naturally without requiring further motivation or derivation.

\section{The Lagrangian formalism and the minimal distance between two points in a plane}\label{distance}

This section is adapted from many sources, chiefly Goldstein's Classical Mechanics textbook~\cite{goldstein2002classical}. Because it is a simple introduction to both the functional that we will derive to be the Lagrangian, and to techniques in the calculus of variations (a pre-requisite topic to understanding the Lagrangian formalism), we aim to show that the minimal distance between two points in a plane is a straight line.
%This same derivation is often used to motivate and introduce the calculus of variations which is a pre-requisite topic to understand the Lagrangian formalism.
\footnote{Some limitations are: we limit ourselves to lines that can be written as one dimensional functions $y=f(x)$ while curves in two dimensions would be more general. We assume the shortest distance must be a differentiable function (although continuous would be sufficient). Foremost, we satisfy ourselves with finding a condition that makes the curve only stationary instead of minimal.}

The arc length $S$ of a function $y(x)$ between two points $(x_1,y_1)$ and $(x_2,y_2)$ is given by

\begin{equation}
S=\int\limits_{x_1}^{x_2}\sqrt{\dd x^2 + \dd y^2}.
\end{equation}
Factoring a $\dd x$ from the radical results in the following equation
\begin{equation}
S= \int\limits_{x_1}^{x_2}\sqrt{1 + \left(\frac{\dd y}{\dd x}\right)^2} \dd x,
\end{equation}
and defining $y' := \frac{\dd y}{\dd x}$ gives a convenient representation for the equation representing the arc length of $y(x)$ as a function of how $y$ changes over its length, %``changes'' is not the best terminology.

\begin{equation}
S = \int\limits_{x_1}^{x_2}\sqrt{1 + y'^2} \dd x.
\end{equation}
We generalize this formula by writing
\begin{equation}
S=\int\limits_{x_1}^{x_2} G(y,y',x) \dd x .
\end{equation}
Although in our example $G = \sqrt{1 + y'^2}$ only depends on $y'$ and not explicitly on $y$ or $x$, we can also assume dependence on $y$ and $x$. %This is to say that later back replacements of $G$ by $\sqrt{1 + y'^2}$ will still be possible.
If formulas we derive for $G$ contain derivatives of $G$ with respect to $y$ or $x$, we just replace these terms with zero as the derivative of a function with respect to a variable it does not depend on is always zero.

% Former version:
% and making the assumption that $G = \sqrt{1 + y'^2}$ only depends on $y'$ and not on $y$ or $x$. We further do as if $G$ would depend on more variables than it actually does. This does no harm to later back replacements of $G$ either. Should formulas we derive for $G$ contain derivatives of $G$ with respect to these new variables, we just replace these terms with zero as the derivative of a function with respect to a variable it does not depend on is always zero. %I'm struggling to find a situation where this would happen (but it is of course mathematically true)

To find the function $y(x)$ that minimizes $S$, we make the simplifying assumption that it is sufficient to find the $y(x)$ that makes $S$ stationary. In other words, we assume that S has exactly one minimum.
To do this, we consider small but arbitrary variations $\delta y$ of $y$ and try to find a condition that causes $\delta S$ to vanish.
During this, the endpoints $(x_1,y_1)$ and $(x_2,y_2)$ are kept fixed; therefore the variations $\delta y$ have the property $\delta y(x_1) = \delta y(x_2) = 0$.
As a result,

\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left(\frac{\partial G}{\partial y} \delta y
+ \frac{\partial G}{\partial y'} \delta y' \right) \dd x.
\end{equation}
Using $\delta y' = y_2' - y_1' = \frac{\ddd }{\dd x}(y_2 - y_1) = \frac{\ddd}{\dd x} \delta y$ and integration by parts for the second term, we find
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial y} \delta y
- \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} \delta y \right) \dd x
+ \left[\frac{\partial G}{\partial y'} \delta y \right]_{x_1}^{x_2}.
\end{equation}
The last term vanishes because $\delta y(x_1) = \delta y(x_2) = 0$, and we are left with
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial y}
- \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} \right) \delta y \; \dd x.
\end{equation}
For $S$ to be stationary, $\delta S$ must vanish. Since $\delta y$ is arbitrary the condition must be
\begin{equation}\label{e-l}
\frac{\partial G}{\partial y} - \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} = 0.
\end{equation}
This equation is called an Euler-Lagrange equation. The procedure of looking for a condition to make $S$ stationary under a function $G(y,y',x)$ is called the Lagrangian formalism. If we substitute $G = \sqrt{1+y'^2}$ into equation \eqref{e-l}, we see that $y'$ must be constant and thus $y(x)$ is a straight line connecting $(x_1,y_1)$ and $(x_2,y_2)$.

\section{Application to Newtonian mechanics} \label{application}
As we discussed in section \ref{introduction}% and will motivate through section \ref{invariance}
, it is desirable to write Newton's law $F=ma$ in the form of an Euler-Lagrange equation. Doing so will provide us with a function like $G$ from the previous section, only now the function will have important physical implications. This function is called the Lagrangian of classical mechanics.

To proceed, we rearrange Newton's second law

\begin{equation}
0 = ma - F.
\end{equation}
The first term can be rewritten as follows:
\begin{equation}
ma = m \ddot{r} = \frac{\ddd}{\dd t} (m \dot{r})
= \frac{\ddd}{\dd t} \frac{\partial}{\partial \boldmath\dot{r}\unboldmath} \left(\frac{1}{2} m \dot{r}^2 \right)
= \frac{\ddd}{\dd t} \frac{\partial T}{\partial \dot{r}},
\end{equation}
where $T:=\frac{1}{2} m \dot{r}^2$ is the classical kinetic energy of the system. For the second term, we assume the force $F$ to be conservative. Consequently, there exists a potential $V$ such that
\begin{equation}
F = - \frac{\partial V}{\partial r} = \frac{\partial (-V)}{\partial r}.
\end{equation}
Using both rewritten terms, Newton's law becomes
\begin{equation}
0 = \frac{\ddd}{\dd t} \frac{\partial T}{\partial \dot{r}} - \frac{\partial (-V)}{\partial r}.
\end{equation}
If we assume that $\partial T/ \partial r = 0$ and $\partial V / \partial \dot{r} = 0$, which is nearly always true in Newtonian mechanics, we can convert the equation to
\begin{equation}\label{e-lwithtv}
0 = \frac{\ddd}{\dd t} \frac{\partial (T-V)}{\partial \dot{r}} - \frac{\partial (T-V)}{\partial r}.
\end{equation}

\section{Invariance of the Euler-Lagrange equation under coordinate transformations} \label{invariance}

This section was inspired by the first chapter of Hagen (2009)~\cite{hagen2009path}.
Let $y=f(Y,x)$ be an invertible and differentiable coordinate transformation.
\footnote{The most effective tool in a physicist's toolbox when solving physics problems is picking an appropriate coordinate system.
A pendulum in Euclidean $(x,y)$ coordinates makes analyzing the problem cumbersome, but in circular $(r,\theta)$ coordinates, analysis becomes simple.
Translating from the coordinate system of the problem statement to the coordinate system that best simplifies the system usually provides great insight, but translating back to the coordinate system of the problem statement is still necessary to solve the problem.}
%This means for any $x$ the transformation $f$ is supposed to be an invertible and differentiable function of the new coordinate $Y$.
%The dependence on $x$ is optional, which is to say that we also well allow transformations $f$ that are only functions of $Y$.
%
We define the transformed function $\widetilde{G}$ through $G$ by
\begin{equation} \label{lagrangian-transform}
\widetilde{G}(Y,Y',x) := G(f,f',x).
\end{equation}
Using the derivation from section \ref{distance}, we find that making $S = \int\limits_{x_1}^{x_2} \widetilde{G}(Y,Y',x) \dd x$ stationary requires the Euler-Lagrange equation

\begin{equation}
\frac{\partial \widetilde{G}}{\partial Y}
- \frac{\ddd}{\dd x}\frac{\partial \widetilde{G}}{\partial Y'} = 0
\end{equation}
be satisfied. This result is again reached by considering a small but arbitrary variation $\delta Y$ which again vanishes at its endpoints.

Likewise, the variation of the same $S$ can be expressed by
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial f} \delta f
+ \frac{\partial G}{\partial f'} \delta f' \right) \dd x
\end{equation}
where $\delta f$ is given by $\delta f = \frac{\partial f}{\partial Y} \delta Y$.

Using $\delta f' = f_2' - f_1' = \frac{\ddd}{\dd x}(f_2 - f_1) = \frac{\dd}{\dd x} \delta f$ and integration by parts for the second term we find
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial f}
- \frac{\ddd}{\dd x} \frac{\partial G}{\partial f'} \right) \delta f \, \dd x \;
+ \; \left[\frac{\partial G}{\partial f'} \delta f \right]_{x_1}^{x_2}.
\end{equation}
As $\delta Y$ goes to $0$ at the endpoints, so does $\delta f$ which causes the last term to vanish. From the arbitrariness of $\delta Y$ follows the arbitrariness of $\delta f$. The only mechanism for $\delta S$ to vanish is

\begin{equation}
0 = \frac{\partial G}{\partial f} - \frac{\ddd}{\dd x} \frac{\partial G}{\partial f'}
= \frac{\partial G}{\partial y} - \frac{\ddd}{\dd x} \frac{\partial G}{\partial y'}.
\end{equation}
This shows that the Euler-Lagrange equation takes the same form under any coordinate transformation as long as the transformation of the function $G$ is given by $\widetilde{G}(Y,Y',x) := G(f,f',x)$. Functions that behave this way under coordinate transformations as $G$ are called scalar functions, especially in physical contexts.
%Can we give an example of a scalar function in undergraduate level mechanics? "Such as the gravitational potential or temperature over an object"
\footnote{An example for a scalar is air temperature.
To analyze this example we consider two coordinate systems with coordinates $x$ and $X$ and their transformation $x=f(X)$.
If $T=T(x)$ denotes temperature in the first coordinate system then as an analogue to definition \eqref{lagrangian-transform} we define the temperature in the second coordinate system by
\begin{equation} \label{definitionOfTemperatureTransform}
  \tilde{T}(X) := T(f(x))
\end{equation}

Now the physical fact that temperature is the same in both coordinate systems leads to the condition $\tilde{T}(X) = T(x)$ which by using definition \eqref{definitionOfTemperatureTransform} can be turned into $T(f(x)) = T(x)$.
Scalars that fulfill this condition are called invariant with respect to the transformation $f$.

An interesting point of the temperature example is that this condition becomes invalid if one of the two coordinate systems moves with a velocity that is not negligible compared to the average motion of the air molecules while the other stays relative to the air at rest.
}
In section \ref{lagrangian-def}, $G$ will be interpreted as the Lagrangian we mentioned in section \ref{introduction}. As we now see, $G$ does indeed have the transformation properties we claimed in section \ref{introduction}.

\section{Definition of the Lagrangian}\label{lagrangian-def} %We can easily remove this section header if a reviewer suggests it, though for now I think defining the Lagrangian is important enough to warrant it

Looking back at the original Euler-Lagrange equation \eqref{e-l} and the transformed version of equation \eqref{e-lwithtv}, let time $t$ take the place of a general independent variable $x$, position $r$ take the place of a general coordinate $y$, and $T-V$ take the place of $G$, we arrive at the following results:

Newton's second law takes the form
\begin{equation}
0 = \frac{\ddd}{\dd t} \frac{\partial L}{\partial \dot{r}} - \frac{\partial L}{\partial r},
\end{equation}
where
\begin{equation}
 L := T-V.
\end{equation}
This $L$ is precisely the Lagrangian from classical mechanics. It is not some divinely sanctioned quantity; it is just Newtonian mechanics under a simple change of variable.

Using this representation of $L$ allows the Lagrangian formulation of mechanics to be derived by requiring that the trajectory $r(t)$ which a particle takes between two endpoints $(t_1,r_1)$ and $(t_2,r_2)$ makes the integral
\begin{equation}\label{eqref:action}
S=\int\limits_{t_1}^{t_2} L \; \dd t
\end{equation}
stationary. From this, the equations of motion for the particle follow, being the particle's Euler-Lagrange equation. This is called the principle of stationary action in physics\footnote{The principle of stationary action is also sometimes called the principle of \emph{least} action. This can be confusing because the Euler-Lagrange equation finds instances where action is stationary (e.g. a saddle point or even maximized)~\cite{gray2007action}.}.

It is unnecessary to speciously explain the form of the classical Lagrangian as having an innate property of interest. We have shown that the Lagrangian takes its classical form
\begin{equation}
  L = T-V
\end{equation}
out of convenience.

Since $F=ma$ is formulated in Cartesian coordinates, the Lagrangian $L$ we derived is formulated in Cartesian coordinates, as well. The transformation law \eqref{lagrangian-transform} we found for $G$ provides us with a well defined method for the transformation of $L$ to other coordinate systems. The equations of motion which are now Euler-Lagrange equations are likewise the same in any coordinate system.  This way the relation of Newton's law to coordinates is restricted and well defined in the sense we mentioned in section \ref{introduction}. %$L$ has the dimension of energy, thus $S$ has dimensions of energy times time, a quantity also called action.

\section{Conclusion and Outlook}

Although the principle of stationary action is a new interpretation of classical mechanics, it nonetheless leads to the same equations of motion as Newtonian physics. In sections \ref{distance} and \ref{invariance} we explored the mathematical structure of the Lagrangian formalism and, because of its transformation properties, found it desirable for classical mechanics. This motivated and enabled us to give comprehensive derivations of the Lagrangian of classical mechanics $L=T-V$ and the principle of stationary action. We hope that this has been useful for clarifying at least one aspect of the Lagrangian formulation of classical mechanics for students who are curious about the subject.

Although the transformation properties of the Lagrangian and the Euler-Lagrange equations are already reason enough to formulate physical laws using the principle of stationary action, these are by far not the only reasons. The principle of stationary action has enormous analytical capabilities which lie far beyond those of Newtonian mechanics. As an outlook we mention some of these analytical capabilities.

\subsection{Conservation laws~\cite{KleinertConservation}}
The principle of stationary action allows derivation of conservation laws from transformation properties of the Lagrangian. From this, energy, momentum, angular momentum, and other quantities that may be conserved, are given formulations which depend only on the Lagrangian, coordinates, and time. Thus, concepts like energy, momentum, and angular momentum gain well-defined meaning for any physical system that has a Lagrangian. Since every physical theory can be described by the principle of stationary action (for examples see section \ref{field.theory}), the concepts of energy, momentum, angular momentum, and other conserved quantities are consistently defined for all of these theories.

\subsection{Quantum mechanics~\cite{Schwabl}}
The rules of quantum mechanics are mostly based on Hamiltonian physics and Poisson brackets which are both continuations of the principle of stationary action. A recent and in-depth discussion of an Lagrangian underlying the Schr{\"o}dinger equation can be found in Deriglazov's paper~\cite{Deriglazov}.

\subsection{Field theory~\cite{Sterman,Dirac}} \label{field.theory}
The principle of stationary action can be and has been extensively continued to field theories. For example, Maxwell's equations can be derived from a principle of stationary action in electrodynamics. There, Maxwell's equations play a similar role as Newton's laws did in this paper.

Most physical theories are field theories, and as a result the widest variety of Lagrangians are field Lagrangians. The most popular field theories apart from electrodynamics are
\begin{itemize}
  \item The Dirac Lagrangian for the relativistic field of fermions.
  \item The Klein-Gordon Lagrangian for the relativistic field of bosons.
  \item The Schr{\"o}dinger Lagrangian for non-relativistic quantum mechanics.
  \item The Lagrangian of the Standard Model of particle physics.
  \item The general relativity Lagrangian for Einstein's theory of general relativity.
\end{itemize}

\subsection{Constrained motion~\cite{Kuypers}}
The simplest example of a problem of constrained motion is that of particle on an inclined plane in a uniform gravitational field. There are, of course, situations with more complex constraints than an inclined plane. In these cases it can be difficult to find the equations of motion for the particle through using Newton's laws. At this point, the transformation law we derived in section \ref{invariance} becomes very helpful: assume there is a transformation of coordinates that well suits the constraints to the coordinates in which the Lagrangian is formulated. Once that is done, formula \eqref{lagrangian-transform} can be used to rewrite the Lagrangian in the coordinates that suit the constraints. The equations of the constrained motion are then the Euler-Lagrange equations of the transformed Lagrangian.

\acknowledgments{We would like to thank the many friends and teachers who helped us clarify numerous points in this manuscript and encouraged the curiosity that sparked this writing. Namely, John Jaszczak, Zhongzhou Chen, Tony Szedlak, David McGhan, and Chris Riley.}

\bibliographystyle{aipauth4-1}
\bibliography{lagrange}

% \begin{thebibliography}{9}

% \bibitem{taylor} John R. Taylor, Classical Mechanics, 2005, page 238

% \bibitem{marion} Jerry B. Marion, Classical Dynamics of Particles and Systems, 1970, page 198-199

% \bibitem{fowles} Grant R. Fowles, Analytical Mechanics, 1999, page 393

% \bibitem{gregory} R. Douglas Gregory, Classical Mechanics, 2006, page 348

% %\bibitem{Klopper} Juan Klopper, Understanding the Euler Lagrange Equation, \url{https://www.youtube.com/watch?v=08vJyA-XD3Q}

% \bibitem{Goldstein} Goldstein et.al., Classical mechanics 3 ed, Chapter 2.2.

% \bibitem{Kleinert} Hagen Kleinert, Path Integrals in Quantum Mechanics, Statistics, Polymer Physics, and Financial Markets, 2004, page 5, \url{https://books.google.com/books?id=dJ3FCgAAQBAJ}

% \bibitem{action-not-least} Edwin Taylor and C.G. Gray, When Action is Not Least, doi: 10.1119/1.2710480

% %\bibitem{Guthrie} Matt Guthrie, The Origin of the Lagrangian, \url{https://web2.ph.utexas.edu/~mwguthrie/t.lagrangian.pdf}

% Citations for conclusion subsections:

% \bibitem{Kleinert-conservation} Hagen Kleinert, Particles and Quantum Fields, 2015, Section 8 , \url{https://books.google.de/books?id=d1-2DAAAQBAJ}

% \bibitem{Schwabl} Franz Schwabl, Quantum Mechanics, 2007, 4th edition, Sections 2.5 and 2.6, \url{https://books.google.de/books?id=pTHb4NK2eZcC&printsec}

% \bibitem{Sterman} George Sterman, An Introduction to Quantum Field Theory, 1993, Sections 1.1 - 1.4, 5.3, 5.4, \url{https://books.google.de/books?id=rB_wdTnfTmoC}

% \bibitem{Dirac} P. A. M. Dirac, General theroy of relativitiy, 1975/1996, Sections 26 - 30, \url{https://books.google.de/books?id=qkWPDAAAQBAJ}

% \bibitem{Kuypers} Friedhelm Kuypers, Klassische Mechanik, 2016, Section 3, \url{https://books.google.de/books?id=dJ4CDAAAQBAJ}

% \end{thebibliography}

\end{document}


\subsubsection{Domain specific features}
\label{sec:methods_features_domain}

The objective of including domain specific features is to add into the
secondary model data that is intrinsically representative of the underlying
technology and market behavior. This subsection shows and explains the
transformations incurred to the listed features in \ref{sec:material_data_bitcoin_features}.

\paragraph{Addresses:} this group is composed of: \emph{total-addresses},
\emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses}, \emph{receiving-addresses}. We can split these
features into two groups, in figure \ref{fig:total_addresses} the evolution of
total registered addresses in the network is plotted. A logarithm transformation
is show as well in green to stabilize its variance and range. Then, in figure
\ref{fig:addresses} we see the evolution of the other features. The reader may
observe the strong correlation between these four indexes and validate the
observation by inspecting figure \ref{fig:addresses_correlation} which shows the
correlation matrix between these four attributes. Because of that, only
\emph{active-addresses} will be used to train the model. We computed the
fractional differentiation optimum $d$ value for the latter index and turned out
to be 0.1.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/total_addresses.png}
    \caption{Evolution of total registered addresses over time.}
    \label{fig:total_addresses}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/addresses.png}
    \caption{Evolution of \emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses} and \emph{receiving-addresses} over time.}
    \label{fig:addresses}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/addresses_correlation_mat.png}
    \caption{Correlation matrix of \emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses} and \emph{receiving-addresses}.}
    \label{fig:addresses_correlation}
\end{figure}

\paragraph{Blocks:} in this group There are: \emph{blocks-mined},
\emph{block-interval-mean}, \emph{block-interval-median}, \emph{block-size-mean}
and \emph{block-size-total}. The first three exhibit a quite similar tendency
(see figure \ref{fig:blocks_produced_blocks_mined}) which is aligned with what
they mean: \emph{blocks-mined} describes the number of blocks added to the
blockchain and the other two are centrality measures of the production rate. The
correlation matrix confirms it, see figure \ref{fig:blocks_production_blocks_mined_corr}.
Only \emph{blocks-mined} will be used to avoid adding redundant information.
On the other hand, we have the \emph{block-size-mean} and
\emph{block-size-total} which are expressed in bytes. Both indexes are expected
to be correlated and \ref{fig:block_size} shows their evolution over time. A
fractional differentiation transformation is not enough to stabilize the series
and a logarithmic transformation is applied which results in
\ref{fig:block_size_log}. Only the logarithm of \emph{block-size-total} will be
used to train the model between these two.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_production_blocks_mined.png}
    \caption{Evolution of \emph{block-interval-mean} and \emph{block-interval-median} are tracked on the left y-axis. \emph{blocks-mined} is tracked on the right y-axis.}
    \label{fig:blocks_produced_blocks_mined}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/block_production_blocks_mined_corr.png}
    \caption{Correlation matrix of \emph{blocks-mined}, \emph{block-interval-mean} and \emph{block-interval-median}.}
    \label{fig:blocks_production_blocks_mined_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_size.png}
    \caption{Evolution of \emph{block-size-mean} (in blue and tracked on the left y axis) and \emph{block-size-total} (in red and tracked on the right y axis) over time.}
    \label{fig:block_size}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_size_log.png}
    \caption{Same features as in \ref{fig:block_size} but with a logarithmic transformation. Due to the change of scale, both features are tracked on the same left y axis.}
    \label{fig:block_size_log}
\end{figure}

\paragraph{Fees:} there are two features related to fees: \emph{fees-total} and
\emph{fees-mean}. They both remain relatively stable with very low values but
there are high valued outliers that get out of range rapidly. Fractional
differentiation was tried but ended up in very low values of $d$ that made no
significant change.  A logarithmic transformation is applied to account the explosive change in local variance when
these outliers occur. Note also that because some values in the series are zero,
a $1$ is added to the logarithm argument to avoid having minus infinity in the
series after the transformation. For illustration purposes, figure
\ref{fig:total_fees} is shown.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/total_fees.png}
    \caption{Evolution of \emph{fees-total} (in blue) and its the logarithmic transformation (in red) over time. The left y axis tracks the linear scale and the right y axis tracks the logarithmic scale.}
    \label{fig:total_fees}
\end{figure}

\paragraph{General indicators:} in this group we have \emph{sopr}, \emph{ratio},
\emph{daysTillHalving}, \emph{price-drawdown-from-ath}, \emph{market-cap} and
\emph{circulating-supply}. The relationship between \emph{market-cap} and
\emph{ratio} was already explained in section \ref{sec:intro_domain} when
referring to the stock to flow model so it will be omitted in this case. We will
just mention that \emph{market-cap} will suffer a logarithmic transformation to
stabilize its range. \emph{sopr} and \emph{price-drawdown-from-ath} are indexes
so no further transformation is required (see figures \ref{fig:sopr} and
\ref{fig:price-drawdown-from-ath}). Moreover, the \emph{circulating-supply}
values are in the order of millions and evolves asymptotically to 18 millions as
the issuance model expects it to be. In particular, we will create a derivative
index from the supply computed as the differentiated series of circulating
supply logarithm which shows the speed of issuance (see figure \ref{fig:circulating_supply_rate}).

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/sopr.png}
    \caption{Evolution of \emph{sopr} over time.}
    \label{fig:sopr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/price-drawdown-from-ath.png}
    \caption{Evolution of \emph{price-drawdown-from-ath} over time. In vertical
    dashed red lines the halving dates are displayed}
    \label{fig:price-drawdown-from-ath}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/circulating_supply_rate.png}
    \caption{Evolution of the logarithm of issuance over time. In vertical
    dashed red lines the halving dates are displayed.}
    \label{fig:circulating_supply_rate}
\end{figure}

\paragraph{Transactions:} in this group we have \emph{transaction-size-total},
\emph{transaction-rate}, \emph{transaction-count}, \emph{transaction-size-mean},
\emph{transfer-volume-mean} and \emph{transfer-volume-median}. As it can be seen
in figure \ref{fig:transaction_corr}, the three indexes
\emph{transaction-size-total}, \emph{transaction-rate}, and
\emph{transaction-count} are highly correlated so only one will be taken as
input to the model, in particular the \emph{transaction-rate}. A note about this
index can be done when looking at the histogram which seems to be bimodal. A
derivative index is created from it which groups the values into their deciles
and aims to reduce the high frequency changes in the transaction rate that
affect the signal on a daily basis. Figure \ref{fig:transaction_rate} shows it
in detail. Moving to \emph{transaction-size-mean} the index exhibits some
frequent spikes but those are within range and there is no clear trend so the
feature remains as is (\ref{fig:transaction_size_mean}). Central transfer volume
features (\emph{transfer-volume-mean} and \emph{transfer-volume-median}) exhibit
a high volatility in the first issuance period and then it is more and more
stable. A logarithmic transformation is applied to both and even though they
try to predict the same, their tendencies are different in values and capture
differently the local volatility so both will be preserved (
\ref{fig:transfer_volume_central}). Similarly to the processing done to the
others, the \emph{transfer-volume-total} is transformed with a logarithm (
\ref{fig:transfer-volume-total}).

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/transaction_corr.png}
    \caption{Correlation matrix between the transaction related features}
    \label{fig:transaction_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transaction_rate.png}
    \caption{Left: \emph{transaction-rate} (in blue) and decile index of the same feature (in red) over time. Right: histogram of \emph{transaction-rate}.}
    \label{fig:transaction_rate}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transaction_mean_size.png}
    \caption{Evolution of \emph{transaction-size-mean} over time.}
    \label{fig:transaction_size_mean}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transfer_volume_central.png}
    \caption{Evolution of the logarithm of \emph{transfer-volume-mean} and \emph{transfer-volume-median}.}
    \label{fig:transfer_volume_central}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transfer_volume_total.png}
    \caption{Evolution of the logarithm of \emph{transfer-volume-total} over time.}
    \label{fig:transfer-volume-total}
\end{figure}

\paragraph{Unspent / spent transactions:} under this final group we can find:
\emph{utx-os-created} \emph{utx-os-spent}, \emph{utxo-value-spent-mean},
\emph{utxo-value-spent-median} and \emph{utxo-value-created-mean}. Following the
same procedure as with the others, a correlation matrix between these features
is created and shown in figure \ref{fig:utxo_corr}. We can find two pairs of
highly correlated features: \emph{utx-os-created} with \emph{utx-os-spent}, and
\emph{utxo-value-created-mean} with \emph{utxo-value-spent-mean}. Note that
\emph{utxo-value-spent-median} is mildly correlated with the others (0.48 and
0.49 respectively) which makes this feature to be kept. Provided that
\emph{utx-os-created} is a super set of \emph{utx-os-spent}, the former will be
preferred (see figure \ref{fig:utxo_created}). The same reasoning applies to
 \emph{utxo-value-created-mean} (see figure \ref{fig:mean_utxo_created}).

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/utxo_corr.png}
    \caption{Correlation matrix between unspent / spent transaction features.}
    \label{fig:utxo_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/utxo_created.png}
    \caption{Evolution \emph{utx-os-created} (in blue) and its logarithmic transform (in green) over time.}
    \label{fig:utxo_created}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/utxo_value_spent.png}
    \caption{Left: evolution of \emph{utxo-value-created-mean} (blue) and its logarithmic transformation (in green) over time. Right: evolution of \emph{utxo-value-created-median} (blue) and its logarithmic transformation (in green) over time.}
    \label{fig:utxo_value_spent}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/mean_utxo_created.png}
    \caption{Evolution of \emph{utxo-value-created-mean} (in blue) and its logarithmic transformation (in green) over time.}
    \label{fig:mean_utxo_created}
\end{figure}


\documentclass[pdf]{beamer}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{tcolorbox}
\usepackage[all]{xy}

\mode<presentation>{}

% ------------------------------------------------------------------------------
% Theme

\usetheme[usetitleprogressbar, nosmallcapitals, protectframetitle, usetotalslideindicator]{m}

\lstloadlanguages{Haskell}
\lstnewenvironment{code}
    {\lstset{}%
      \csname lst@SetFirstLabel\endcsname}
    {\csname lst@SaveFirstLabel\endcsname}
    \lstset{
      basicstyle=\ttfamily\footnotesize,
      flexiblecolumns=false,
      basewidth={0.5em,0.45em},
      literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1
               {\\\\}{{\char`\\\char`\\}}1
               {=>}{{$\Rightarrow$}}2
               {forall}{{$\forall$}}2
               {->}{{$\rightarrow$}}2
               {<-}{{$\leftarrow$}}2
               {>>}{{>>}}3 {>>=}{{>>=}}3,
      commentstyle={\ttfamily\color{gray}},
      language=haskell
    }

% ------------------------------------------------------------------------------
% Presentation

\title{Finally \texttt{mtl}!}
\date{\today}
\author{Joseph Tel Abrahamson / @sdbo / \texttt{github.com/tel} }

\renewcommand{\to}{\ensuremath{\rightarrow}}
\DeclareMathOperator{\ty}{\texttt{ :: }}

\begin{document}

\maketitle

\begin{frame}
  \frametitle{Synopsis}
  \begin{itemize}
  \item What is ``Finally Tagless''? What's important about it?
  \item What is the ``Monad Transformer Library'', \texttt{mtl}?
  \item \texttt{mtl} is a ``finally tagless'' effect library---and that's quite
    nice!
  \end{itemize}
\end{frame}

\section{A tale of three DSLs}

\begin{frame}[fragile]
  \frametitle{Hutton's Razor}
\begin{lstlisting}
data AddLang
  = AddLangIntLit Integer
  | Add AddLang AddLang
  deriving ( Show, Eq )

interpAddLang :: AddLang -> Integer
interpAddLang = \case
  AddLangIntLit i -> i
  Add l r  -> interpAddLang l + interpAddLang r

addLangExp :: AddLang
addLangExp = Add (AddLangIntLit 1) (AddLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Hutton's Backup Razor}
\begin{lstlisting}
data MultLang
  = MultLangIntLit Integer
  | Mult MultLang MultLang
  deriving ( Show, Eq )

interpMultLang :: MultLang -> Integer
interpMultLang = \case
  MultLangIntLit i -> i
  Mult l r -> interpMultLang l * interpMultLang r

multLangExp :: MultLang
multLangExp = Mult (MultLangIntLit 1) (MultLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Hutton's Travel Kit}
\begin{lstlisting}
data RingLang
  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang
  deriving ( Show, Eq )

interpRingLang :: RingLang -> Integer
interpRingLang = \case
  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r

ringLangExp :: RingLang
ringLangExp = RingMult (RingAdd (RingLangIntLit 3)
                                (RingLangIntLit 2))
                       (RingLangIntLit 3)
\end{lstlisting}
\end{frame}

\section{What's wrong with this picture?}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}
data RingLang
  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang
  deriving ( Show, Eq )

interpRingLang :: RingLang -> Integer
interpRingLang = \case
  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}

  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang

  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}

  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang

  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\plain{Nooooooooooooooooooooo- oooooooooooooooooo!}

\begin{frame}
  \frametitle{Let's use category theory!}
  \begin{displaymath}
    \xymatrix{
      \mathtt{AddLang} & \bullet \ar[rdd]_{???} & & \ar[ldd]^{???} \bullet & \mathtt{MultLang} \\
      & & & & \\
      & & \bullet & & \\
      & & \mathtt{RingLang} & & \\
    }
  \end{displaymath}
\end{frame}

\section{Once more, with feeling!}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}[p]
newtype Fix f = Fix { unFix :: f (Fix f) }

fixFold :: Functor f => (f a -> a) -> (Fix f -> a)
fixFold phi = go where go = phi . fmap go . unFix
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE UndecidableInstances #-}

deriving instance Show (f (Fix f)) => Show (Fix f)
deriving instance Eq   (f (Fix f)) => Eq   (Fix f)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE DeriveFunctor #-}

data AddF  x = AddI  Integer | AddF  x x deriving ( Show, Eq, Functor )
data MultF x = MultI Integer | MultF x x deriving ( Show, Eq, Functor )

type AddLang'  = Fix AddF
type MultLang' = Fix MultF
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
addLangExp' :: AddLang'
addLangExp' = Fix (AddF (Fix (AddI 1)) (Fix (AddI 3)))

multLangExp' :: MultLang'
multLangExp' = Fix (MultF (Fix (MultI 1)) (Fix (MultI 3)))
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
-- Smart constructors! Yessssssss!
addI :: Integer -> AddLang'
addI = Fix . AddI

multI :: Integer -> MultLang'
multI = Fix . MultI

addAdd :: AddLang' -> AddLang' -> AddLang'
addAdd l r = Fix (AddF l r)

multMult :: MultLang' -> MultLang' -> MultLang'
multMult l r = Fix (MultF l r)

addLangExp' :: AddLang'
addLangExp' = addAdd (addI 1) (addI 3)

multLangExp' :: MultLang'
multLangExp' = multMult (multI 1) (multI 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
interpAddF :: AddF Integer -> Integer
interpAddF = \case
  AddI i   -> i
  AddF l r -> l + r

interpMultF :: MultF Integer -> Integer
interpMultF = \case
  MultI i   -> i
  MultF l r -> l * r

interpAddLang' :: AddLang' -> Integer
interpAddLang' = fixFold interpAddF

interpMultLang' :: MultLang' -> Integer
interpMultLang' = fixFold interpMultF
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE TypeOperators #-}

data (f :+: g) x
  = Inl (f x)
  | Inr (g x)
  deriving ( Eq, Show, Functor )

-- Natural :+: eliminator
foldSum :: (f a -> a) -> (g a -> a) -> ((f :+: g) a -> a)
foldSum falg galg = \case
  Inl fa -> falg fa
  Inr ga -> galg ga
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The prize!}
\begin{lstlisting}
type RingLang' = Fix (AddF :+: MultF)

-- More smart constructors!
ringI :: Integer -> AddLang'
ringI = Fix . Inl . AddI  -- why not via MultI?

ringAdd :: AddLang' -> AddLang' -> AddLang'
ringAdd l r = Fix (Inl (AddF l r))

ringMult :: MultLang' -> MultLang' -> MultLang'
ringMult l r = Fix (Inr (MultF l r))

ringLangExp' :: RingLang'
ringLangExp' = ringMult (ringAdd (ringI 3) (ringI 2)) (ringI 3)

interpRingLang' :: RingLang' -> Integer
interpRingLang' = fixFold (foldSum interpAddF interpMultF)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> interpRingLang' ringLangExp'

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> interpRingLang' ringLangExp'
15
\end{lstlisting}
\end{frame}

\plain{Yessssssss! HASKELL! \\ \textbackslash{}textbackslash o/}

\begin{frame}
  \frametitle{D. R. Y. Stats}
  \begin{center}
    40 additions, 3 new PRAGMAs

    \pause

    Used \texttt{Fix} in anger
  \end{center}
\end{frame}

\plain{Yessssssss! HASKELL! \\ (+1 Functional Programming)}

\begin{frame}
  \frametitle{In all seriousness...}
  Generalizing \lstinline{:+:}

  Wouter Swierstra, \textit{Data types \`a la carte}.

  Really cool.
\end{frame}

\section{Data types \`a la Carette, Kiselyov, and Shan}

\begin{frame}
  Let's try this again...
\end{frame}

\begin{frame}[fragile]
  \frametitle{Design by wishful thinking}
\begin{lstlisting}
class Adds v where
  add :: v -> v -> v

class Multiplies v where
  mult :: v -> v -> v
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
instance Adds Integer where add = (+)
instance Multiplies Integer where mult = (*)

addsExp :: Integer
addsExp = add 1 3

multsExp :: Integer
multsExp = mult 1 3
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
class FromInteger v where
  i :: Integer -> v

instance FromInteger Integer where
  i = id
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
addsExp :: (FromInteger v, Adds v) => v
addsExp = add (i 1) (i 3)

multsExp :: (FromInteger v, Multiplies v) => v
multsExp = mult (i 1) (i 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
{-# LANGUAGE ConstraintKinds #-}

type Rings v = (FromInteger v, Adds v, Multiplies v)

ringsExp :: Rings v => v
ringsExp = mult (add (i 3) (i 2)) (i 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp
15
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: Integer
15
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{But then!}
\begin{lstlisting}
instance FromInteger RingLang where i    = RingLangIntLit
instance Adds        RingLang where add  = RingAdd
instance Multiplies  RingLang where mult = RingMult
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: RingLang

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: RingLang
RingMult (RingAdd (RingLangIntLit 3)
                  (RingLangIntLit 2))
         (RingLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Recap}
  \begin{itemize}
  \pause
  \item One-line composabiity
  \pause
  \item Needn't mention concrete data types or \lstinline{Inl}/\lstinline{Inr}
    \pause
    \begin{itemize}
    \item ``tagless''
    \end{itemize}
  \pause
  \item But if we have them, we can still recover ``raw'' ASTs
  \end{itemize}
\end{frame}

\section{"Does is scale?"}

\begin{frame}
  Yes.
\end{frame}

\section{The "Monad Transformer Library"}

\begin{frame}
  Or: ``Haskell2010 Composable Effects Through Prolog Technology''
\end{frame}

\begin{frame}
  Or: ``Haskell2010 Composable Effects Through Typeclass Technology''
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
import Control.Monad.State

inc :: MonadState Int m => m Int
inc = do
  count <- get
  set (count + 1)
  return count
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
-- let's get a concrete stack from `transformers`
import Control.Monad.Trans.State (runState)

Prelude> flip runState 0 inc
(1, 1)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
-- let's get a concrete stack from `transformers`
import Control.Monad.Trans.State (runState)

Prelude> flip runState 0 (inc :: StateT Int Identity Int)
(1, 1)
\end{lstlisting}
\end{frame}

\begin{frame}
  Solve your monad stacks at compile time! \pause Not runtime\pause, nor write-time.
\end{frame}

\section{Complaints}

\begin{frame}[fragile]
  \frametitle{Just a few points in the lattice}
\begin{lstlisting}
MonadTrans (ContT r) Source
Monad (ContT r m) Source
Functor (ContT r m) Source
Applicative (ContT r m) Source
MonadIO m => MonadIO (ContT r m) Source
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{All points in the lattice}
\begin{lstlisting}
MonadCont (ContT r m) Source
\end{lstlisting}
  \pause
\begin{lstlisting}
MonadCont m => MonadCont (MaybeT m) Source
MonadCont m => MonadCont (ListT m) Source
MonadCont m => MonadCont (IdentityT m) Source
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source
(Error e, MonadCont m) => MonadCont (ErrorT e m) Source
MonadCont m => MonadCont (ExceptT e m) Source
MonadCont m => MonadCont (StateT s m) Source
MonadCont m => MonadCont (StateT s m) Source
MonadCont m => MonadCont (ReaderT r m) Source
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source
...
-- This gets quite complex
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{All points in the lattice}
  But this is just the name of the game! Effects don't commute.
\end{frame}

\begin{frame}[fragile,fragile]
  \frametitle{Lose control of ordering}
\begin{lstlisting}
op :: (MonadError e m, MonadState s m) => m ()
\end{lstlisting}
  \pause
\begin{lstlisting}
op ==> StateT s (Either e) ()
op ==> EitherT e (State s) ()
\end{lstlisting}
  \pause
No right answer!
\end{frame}

\begin{frame}
  \frametitle{Loose control of ordering}
  This is why we have laws.
  \pause
  Combining laws ``correctly'' \textit{is} hard.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Loose control of ordering}
\begin{lstlisting}
class (MonadState s m, MonadError e m) => MonadParser s e m where {}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Tight denotation of semantics}
\begin{lstlisting}
-- Tighten to your domain
class MonadParser m where
  type family Char m :: *
  type family Error m :: *

  peekChar :: m (Char m)
  getChar  :: m (Char m)

  failParse :: Error m -> m a
\end{lstlisting}
\end{frame}

\section{Thanks!}

\begin{frame}
  \frametitle{Tweet at me!}
  \begin{center}
    @sdbo
  \end{center}
\end{frame}

\end{document}

%%% Local Variables:
%%% coding: utf-8
%%% mode: latex
%%% TeX-engine: xetex
%%% End:


% !TEX root = ../thesis.texSecondly
%
\chapter{Conclusion}
\label{sec:conclusion}
In this thesis we present an zero-knowledge image retrieval system employing a \acrlong{fcn} for efficient object detection. With a set of transformed samples derived from a query image we fine-tune the underlying \gls{resnet}. Training is done without a training progress dependent abort condition. The linear classifier is converted to a convolution to predict sparse class probabilities on images of varying size. Using these probability maps we predict the existence of alike objects in images. We yield these patches and thereby we retrieve object instances from image set related to the training samples.

With our results on \textsc{Pascal}-Part and the application on art historical images we provide evidence that this approach can carry out image retrieval in unlabeled, context-less images using a small number of examples, ultimately only a single one. On the one hand, the tests on \textsc{Pascal}-Part verify the supposition, that providing the network with more positives image samples drastically improves its detection results, on the other hand we showed that under our test settings, results quickly converged over an increasing number of examples.\\
Although we prove the methods effectiveness we recognize multiple drawbacks. Firstly the time complexity \tref{sec:results:time} restricts its use on small datasets of images and makes instance retrieval somewhat cumbersome. Secondly our region scoring method throughout under-scales object boxes thus serving more as a translation prediction.\\
The approach could be continued and improved on multiple different ways. Of most importance is the unanswered question, how to use the image representation provided by the deep filter of a \gls{cnn} to bootstrap an approximate object detector without having to actually iteratively train the network. We could not test any approaches in this domain and as such can not predict a possible solution.\\
To fasten the approach it may be possible to formulate the complete pipeline inside the neural network, reducing time costs. Taking densities of regions is nothing else than average pooling and thus could be done within the network after convolutional upsampling.

All code is made publicly available under \url{https://github.com/mrtukkin/bachelor-thesis}.


\documentclass{article}

\usepackage{xstring}
\usepackage{calc}
\usepackage{xintexpr}
\usepackage{arrayjobx}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}

\title{Advent of Code - Day 8}
\author{Sansero}
\date{July 2020}

\newcommand\imgwidth{25}
\newcommand\imgheight{6}
\newcommand\pixelcount{0}

\renewcommand\pixelcount{\the\numexpr \imgwidth * \imgheight}

\typeout{moin}

\newread\file
\openin\file=input.txt
\read\file to \input % Reads a line of the file into \input
\closein\file

\typeout{Loaded file}

\newarray\bitmap
\expandarrayelementtrue

\newcommand\updatelayer[1]{
    \foreach \i in {1,...,\pixelcount}{
        \StrMid{#1}{\i}{\i}[\char]
        \IfEq{\char}{0}{
            \updatearray{\i}{0}
        }{
            \IfEq{\char}{1}{
                \updatearray{\i}{1}
            }{
                \IfEq{\char}{2}{
                    \updatearray{\i}{2}
                }{}
            }
        }
    }
}

\newcommand\updatearray[2]{
    \checkbitmap(#1)
    \ifemptydata
        \bitmap(#1)={#2}
    \else
        \if 2\cachedata \bitmap(#1)={#2} \fi
    \fi
}

\newcount\minzero
\newcount\minlayer
\minzero=\pixelcount
\newcount\onetimestwo
\newcount\ones
\newcount\twos
\newcount\layernr

\newcount\counter
\counter=0
\loop
    \typeout{\the\counter}
    \StrLeft{\input}{\pixelcount}[\layer]
    \StrGobbleLeft{\input}{\pixelcount}[\input]
    \StrLen{\layer}[\layerlength]
    \ifnum \layerlength=\pixelcount
        \updatelayer{\layer}
        \StrCount{\layer}{0}[\zerocount]
        \ifnum \zerocount<\minzero
            \StrCount{\layer}{1}[\onecount]
            \StrCount{\layer}{2}[\twocount]
            \minzero=\zerocount
            \minlayer=\layernr
            \ones=\onecount
            \twos=\twocount
            \onetimestwo=\the\numexpr \onecount * \twocount \relax
        \fi
    \fi
    \advance \counter \pixelcount
    \advance \layernr 1
\ifx\input\empty
\else
\repeat

\begin{document}

\maketitle

\section{Part 1}

Error detection. Get the layer with the fewest zeros

Layer: \the\minlayer

Zeros: \the\minzero

$1*2$: \the\onetimestwo

Ones: \the\ones

Twos: \the\twos

\section{Part 2}

The final image

% 0: black
% 1: white
% 2: transparent

\begin{tikzpicture}[]
  \tikzstyle{every node}=[minimum size=3mm]
  \tikzset{pre/.style={draw,fill=black, text=white}}
  \let\mymatrixcontent\empty
  \foreach \i in {0,...,\the\numexpr \imgheight -1} {%
      \foreach \y in {1,...,\imgwidth} {%
        \checkbitmap(\the\numexpr \i*\imgwidth + \y)%
        \if 0\cachedata
            \edef\x{
            \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=black, text=black]{0}; \&}}\x
        \else
            \if 1\cachedata
                \edef\x{
                \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=white]{1}; \&}}\x
            \else
                \if 2\cachedata
                    \edef\x{
                    \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=blue]{2}; \&}}\x
                \else
                    \edef\x{
                    \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=red]{e}; \&}}\x
                \fi
            \fi
        \fi
      }
      \gappto\mymatrixcontent{\\}%
  }

  \matrix[matrix of math nodes,%
      nodes = {pre},%
      ampersand replacement=\&] {%
    \mymatrixcontent
  };
\end{tikzpicture}

\end{document}


\subsection{The mixed effects estimator}

\subsubsection{Introduction}


% \newcommand{\nconfirmed}{500}
\newcommand{\dantodo}[1]{{\color{blue}#1}}

\section{Method}
\label{sec:method}

\subsection{Rotation period measurement}

Since the \nconfirmed\ comoving stars identified in TGAS and confirmed with RV
follow-up we consider here are all \Kepler\ targets with precise light curves
covering around 4 years, we measured photometric rotation periods for these
stars.
We applied three different methods to the light curves: a Lomb-Scargle
periodogram method, an autocorrelation method and a Gaussian process method.
The exact implementations of the Lomb-Scargle and Gaussian process methods
were also used in the \citet{Angus2017} rotation period study.
In that work we found that the Gaussian process method produces slightly more
accurate rotation periods than the periodogram method and substantially more
accurate periods than the autocorrelation method for \kepler\ data.
However, we also found that each rotation period measurement method is very
sensitive to the exact implementations, for example any choices of heuristics
or priors affected the resulting rotation periods for an ensemble of light
curves.
For this reason we use all three methods here to ensure that our results are
not significantly altered by method choice.
We describe the implementation of each method below.

To measure periods using a Lomb-Scargle periodogram we first applied a
high-pass filter to the light curves to remove long-term trends.
We used the {\tt scipy} 3rd order Butterworth filter with a 35 day cut-off,
attenuating signals with periods greater than this threshold.
For each simulated light curve, we computed a LS periodogram\footnote{LS
periodograms were calculated using the {\tt scipy} Lomb-Scargle algorithm}
over a grid of 10,000 periods, evenly spaced in frequency, between 1 and 100
days.
We adopted the period of the highest peak in the periodogram as the measured
rotation period.
The uncertainties on the rotation periods were calculated using the
following equation for the standard deviation of the frequency
\citep{Horne1986, Kovacs1981}:
\begin{equation}
    \sigma_{\nu} = \frac{3\pi\sigma_N}{2N^{1/2}TA},
\end{equation}
where $A$ is the amplitude of the signal of highest power, $\sigma_N$ is the
variance of the time series, with the signal of highest power removed, $N$
is the number of observations and $T$ is the timespan of the data.
These formal uncertainties are only valid in the case that the noise is white,
the data are evenly sampled and there is only one signal present.
Since there are multiple signals present in these light curves, this formal
uncertainty is an underestimate of the true uncertainty.

\racomment{Add section on ACF method.}

The implementation of the Gaussian process method we use here is described and
discussed in detail in \citet{Angus2017}.
Given its success in \citet{Angus2017}, we chose to use the same kernel
function:
\begin{equation}
\label{eq:QP}
k_{i,j} = A \exp \left[-\frac{(x_i - x_j)^2}{2l^2} -
    \Gamma^2 \sin^2\left(\frac{\pi(x_i - x_j)}{P}\right) \right] + \sigma^2
    \delta_{ij},
\end{equation}
where $A$ is the covariance amplitude, $l$ is the length-scale of overall
covariance decay, $\Gamma$ is the parameter controlling
the intra-period variation (large $\Gamma$ permits more zero-crossings), $P$
is the rotation period and $\sigma$ is the additional white noise needed to
account for errorbar uncertainties.
The priors on each of these parameters takes a relatively simple functional
form, except for the period prior which is built from an initial
autocorrelation function guess.
These priors are fully described in \citet{Angus2017} and we will not go into
further detail here.

The rotation periods measured using these three methods are in good agreement:
N\% of periods agree to within 10\%.

\subsection{Age prediction}

Since we are assuming that the comoving stars are coeval we expect their
rotation periods to reflect this.
To quantify the rate of age-matches,

\subsection{Inferring intrinsic dispersion in the gyrochronology
relations}