\documentclass[twoside, 11pt]{article}

\usepackage{jmlr2e}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}

% ensure sufficient marginspace for todos
\setlength {\marginparwidth }{2cm}
\usepackage[obeyFinal]{todonotes}
% \setuptodonotes{inline}

% define notation for norm and abs that scale nicely.
% ref: https://tex.stackexchange.com/a/297263
\let\oldnorm\norm
\let\norm\undefined
\DeclarePairedDelimiter\norm{\lVert}{\rVert}

\let\oldabs\abs
\let\abs\undefined
\DeclarePairedDelimiter\abs{\lvert}{\rvert}

\DeclarePairedDelimiter\card{\lvert}{\rvert}

\newcommand{\xx}[0] {\mathbb{X}} % decision variable space
\newcommand{\hh}[0] {\mathbb{H}} % stochastic process for state
\newcommand{\zz}[0] {\mathbb{Z}} % hidden event space?
\newcommand{\mm}[0] {\mathbb{M}} % HMM model type space
\newcommand{\TT}[0] {\mathbb{T}} % time indices
\newcommand{\II}[0] {\mathbb{I}} % factorial model indices
\newcommand{\traj}[1] {H^{(#1)}}
\newcommand{\state}[2] {H_{#2}^{(#1)}}
\newcommand{\event}[2] {Z_{#2}^{(#1)}}
\newcommand{\eventseq}[1] {Z^{(#1)}}
\newcommand{\reals}[0] {\mathbb{R}}
\newcommand{\naturals}[0] {\mathbb{N}}
\newcommand{\events}[0] {\mathbb{Y}}

\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}

\author{\name Reuben Fletcher-Costin}

\editor{}

\title{Approximate MAP inference of sparse factorial hidden Markov models through set cover decomposition}

\maketitle

\begin{abstract}%
Our goal is to infer a sparse subset of hidden Markov models (HMMs) that collectively explain a sequence of observations. We define a probabilistic model for a simple form of factorial HMMs and pose a maximum a posteriori (MAP) estimation problem to infer a sparse subset of component HMM, their hidden states, and separate the observations into component signals associated with each component HMM. We show that the MAP estimation problem is equivalent to an exact cover problem, where each component HMM is regarded as a set that covers a portion of the observed signal. This permits a relaxed approximation of the problem to be decomposed as a master set cover problem and auxiliary problem using column generation. Each auxiliary problem can be solved efficiently using dynamic programming by a "prize-collecting" modified Viterbi algorithm that recovers a HMM trajectory that incorporates prizes from the master problem for explaining portions of the observed signal.
\end{abstract}

% keywords could go here

\section{Introduction}

\todo[inline]{why care about HMMs}
\todo[inline]{why care about separation problems}
\todo[inline]{discuss factorial HMMs}
\todo[inline]{discuss alternatives to factorial HMMs}
\todo[inline]{intro linear programming decompositions, convex optimisation decompositions}

\section{Probabilistic model}

\subsection{Hidden Markov models}
We start by assuming a family of hidden Markov models, indexed by $m \in \mm$. Each model $m \in \mm$ in the family has a finite state space $S_m = \{s_1, \ldots, s_{K_m}\}$ consisting of $K_m \in \naturals$ states. The state of the model $m$ at time $t \in \TT = \{ 1, \ldots, T \}$ is denoted by the random variable $H_{m, t}$ which takes values in the state space $S_m$. We regard the states as not directly observable and refer to them as hidden (aka latent) states. The hidden states of model $m$ evolve in time independently from other models according to a discrete time first-order Markov process
\begin{equation}
P(H_{m,t+1} \mid \{ H_{m^{\prime},t^{\prime}} \}_{m^{\prime}, t^{\prime} \in \mm \times \TT \setminus \{m, t\}} )
=
P(H_{m,t+1} \mid H_{m,t} )
\end{equation}
We assume that the transition model $P(H_{m,t+1} \mid H_{m,t} )$ for each $m \in \mm$ is stationary, that is, there exists some $K_m$ by $K_m$ stochastic matrix $A^{m}$ with elements $A^{m}_{s^{\prime}, s}$ such that for all $t \in \TT$, $A^{m}_{s^{\prime}, s} = P(H_{m, t+1}=s^{\prime} \mid H_{m,t}=s)$. We write $\pi_m$ to denote a prior distribution over $H_{m,1}$ at $t=1$. In the simple case where we believe a-priori that the $m$th HMM is present \footnote{Once we consider a sparse factorial model composed of multiple component HMMs from the family $\mm$, we may no longer believe a-priori that all or any HMM $m \in \mm$ necessarily participates in the factorial model.}, we could write $P(H_{m,1}) = \pi_m$.

Each hidden Markov model $m$ emits a sequence of signals $Z_{m,1}, \ldots, Z_{m,T}$, where each $Z_{m,t} \in \events$. The space of signals $\events$ is assumed to be a vector space over $\reals$. Each signal $Z_{m,t}$ is assumed to be caused solely by the corresponding hidden state $H_{m,t}$:
\begin{equation}
P\left(Z_{m,t}
\mid
\{ H_{m^{\prime},t^{\prime}} \}_{m^{\prime}, t^{\prime} \in \mm \times \TT} \;
\{ Z_{m^{\prime},t^{\prime}} \}_{m^{\prime}, t^{\prime} \in \mm \times \TT \setminus \{m, t\}}
\right)
=
P(Z_{m,t} \mid H_{m,t} ) .
\end{equation}
We refer to $P(Z_{m, t} \mid H_{m,t} )$ as the observation model for the $m$th model. We assume the observation model for each $m \in \mm$ is stationary with respect to $t$.

\todo{add a clear graphical model of a single HMM for some $m \in \mm$}

\subsection{Factorial hidden Markov models}

We consider factorial hidden Markov models consisting of a multiset of component hidden Markov models, where the possible component HMMs are indexed by $i$ over some abstract index set $\II$. Note that the cardinality of $\II$ may not be finite. We defer explictly constructing $\II$ until section \ref{section:inference}. Let $m(i) \in \mm$ denote the component HMM associated with the $i$th index. We represent a given factorial model as the pair $X, M$, where
\begin{align}
X & := (X_i)_{i \in \II} \quad X_i \in \{0, 1\} , \\
M & := (m(i))_{i \in II} \quad m(i) \in \mm .
\end{align}
We regard $X$ as a collection of random variables over binary values that address a subset of indices from $\II$, and $M$ as a collection of random variables over $\mm$ that associate a particular type of HMM $m(i) \in \mm$ to each selected index. Note this representation of factorial models is not unique to permutations of the indices $\II$.

We restrict our focus to \emph{sparse} factorial models where $\norm{X}_1$ is bounded, that is, where there exists $n \in \naturals$ such that $\sum_{i \in \II} \abs{X_i} \leq n$. We can further express a preference for sparsity through a choice of prior over $X$.

We are interested in estimating a factorial hidden Markov model with components $(X_i)_{i \in \II}$ that best "explains" some given observed data $Y_t \in \events$ for $t \in \TT$. The standard probabilistic model for a single HMM $m \in \mm$ assumes that at each time $t in \TT$ the output signal $Z_{m,t}$ is directly observable. In contrast, with a factorial HMM the output $Z_{m,t}$ of each component HMM $m$ is not directly observable. Instead, we observe a signal $Y_t$ each $t \in \TT$ that aggregates the hidden output signals of all component HMMs:
\begin{equation}
Y_t = \sum_{i \in \II} X_i \event i t ,
\end{equation}
where $\event i t := Z_{m(i), t}$. Note that unlike the definition of the more general factorial hidden Markov model considered by {Ghahramani and Jordan 1997}\todo{CITE} we assume there is no top-level error term and assume a different dependency structure between $Y$ and $Z$. Error terms can be expressed by $\event i t$ through the observation models of the component HMMs.

\todo{add graphical model of this factorial HMM construction using Y X Z M H}

\section{Inference} \label{section:inference}

\todo{rework in terms of index set $\II$. that will allow multiple copies of a single class of markov model to appear in solution. that's fine, as long as they each pay their way by justifying another copy of the log prior in the objective function}

Our goal is to infer a sparse factorial HMM, represented by $X$ and $M$, that explains a sequence of outputs $Y = (Y_t)_{t \in \TT}$. Given the framework of Bayesian inference, this amounts to computing the posterior distribution $P(X, M | Y=y)$ given observed data $y \in \events^{\TT}$. Computing this posterior distribution exactly is computationally challenging as it requires integrating over all the hidden states $\state i t$ and all outputs $\event i t$ for all $t \in \TT$ and all potential component models $i \in \II$. A less useful but more computationally feasible task is to instead compute a maximum a posteriori parameter estimate of the quad $(X, M, H, Z) = (X_i, m(i), \traj i, Z^{(i)})_{i \in \II}$ given the observed data $Y=y$. The elements of the quad define which component models participate in the factorial model ($X$), the type of each component from the family $\mm$ ($M$), the hidden states of each component ($H$), and the hidden outputs emitted by each component ($Z$).

This form of MAP estimate is computationally tractable because the probabilities of each component HMM state $(X_i, Z_i)$ are conditionally independent from $(X_j, Z_j)$ $j \neq i$ given $Z$, allowing conditional probabilities to be calculated independently component by component once some value of $Z$ is fixed. This leads to a tractable decomposition. \todo{explain in terms of causal graph diagram, markov blankets, d separation}

We demonstrate the decomposition more formally. Consider the following factorisation of the conditional probability of the variables $X, H, Z$ given the data $Y$:
\begin{align}
P(X, M, H, Z \mid Y)
& = P(Y \mid X, M, H, Z) P(X, M, H, Z) / P(Y) \label{map1} \\
& \propto P(Y \mid X, M, H, Z) P(X, M, H, Z) \label{map2} \\
& = P(Y \mid X, Z) P(X, M, H, Z) \label{map3} \\
& = P(Y \mid X, Z) P(Z | X, M, H) P(X, M, H) \label{map4} \\
& = P\left(Y \mid X, Z\right) \prod_{i \in \II} P\left(\eventseq i | M_i, \traj i\right)^{X_i} P(X, M, H) \label{map5} \\
& = P\left(Y = \sum_{i \in \II} X_i \eventseq i \mid X, Z \right)
\prod_{i \in \II} P\left(\eventseq i | M_i, \traj i\right)^{X_i} P(X, M, H) \label{map6}
\end{align}
\todo{fixup p a given b bar scaling}
where \ref{map1} uses Bayes' theorem, \ref{map2} drops the factor $P(Y)$ as it is invariant during maximisation over $(X, H, Z)$, \ref{map3} applies the conditional independence of $Y$ from $H$ and $M$ given $X$ and $Z$, \ref{map5} is due to the conditional independence of the component observation models given $Z$, and \ref{map6} applies the definition of the aggregated observation $Y$ from the component hidden outputs.

To complete the decomposition of the conditional probability $P(X, M, H, Z \mid Y)$ in terms the distributions defined by the family of component Markov processes, we need to decide on $P(X, M, H)$, a prior joint distribution over $X$, $M$ and $H$. We assume the prior probabilities of $\traj m$ and $\traj {m^{\prime}}$ are conditionally independent given $X$ for each distinct $m, m^{\prime} \in \mm$. Therefore we have
\begin{align}
P(X, M, H)
& = \prod_{i \in \II} P(\traj i \mid m(i)) P(m(i) \mid X_i ) P(X) \\
& = \prod_{i \in \II} \left( \frac{1}{\card{\mm}} P(\traj i \mid m(i)) \right)^{X_i} P(X) \\
\end{align}
where $P(\traj i \mid m(i))$ is the prior probability of the trajectory $\traj {m(i)} = \pi_{m(i)}$ as supplied by the component HMM. The $\frac{1}{\card{\mm}}$ factor is due to assuming a uniform prior over $m(i)$ that does not prefer any particular model $m$ of the family $\mm$. We tentatively define a prior $P(X)$ over the binary sequence $X = \{X_i\}_{i \in \II} \in \{0, 1\}^{\card{I}}$ in terms of a prior $P(n)$ over the number of active processes $n := \norm{X}_1$ that assigns higher prior probability to explanations involving fewer active Markov processes:
\begin{align}
P(X)
& = P(X \mid n) P(n) \\
& \propto \frac{1}{|\mm|^n \; 2^{n+1}}
\end{align}
where we have omitted a normalisation constant required so that $\sum_{X} P(X) = 1$
\todo{rework, support nonuniform prior on $\mm$ as it is easy and probably quite useful in applications}

\todo{add $M$ into the argmax}

Consider the MAP parameter estimation problem:
\begin{equation}
(X^{\star}, M^{\star}, H^{\star}, Z^{\star}) = \argmax_{X, M, H, Z} P\left(X, M, H, Z \mid Y\right)
\end{equation}
Substituting our decomposition of $P(X, M, H, Z | Y)$ and the definition of our prior $P(X, M, H)$ gives
\begin{align*}
& \argmax_{X, M, H, Z} P\left(X, H, Z \mid Y\right) \\
= & \argmax_{X, M, H, Z}
P\left(Y = \sum_{i \in \II} X_i \eventseq i \mid X, Z \right)
\prod_{i \in \mm} P\left(\eventseq i | m(i), \traj i\right)^{X_i} P(X, M, H) \\
= & \argmax_{X, M, H, Z} \prod_{i \in \II} P\left(\eventseq i | m(i), \traj i\right)^{X_i}
\prod_{i \in \II} P(\traj i \mid m(i))^{X_i}
\frac{1}{|\mm|^n \; 2^{n+1}} \\
 & \mathrm{s.t.} \sum_{i \in \II} X_i \eventseq i = Y
\end{align*}
where $n = \norm{X}_1$ and the factor for the conditional probability of the observation vector $Y$ is equivalently expressed by the constraining the max to consider only those quads $(X, M, H, Z)$ such that $\sum_{i \in \II} X_i \eventseq i = Y$.

Since $\argmax$ is invariant under transformation of the objective function by a monotonic function, by the monotonicity of the logarithm we have
\begin{align}
(X^{\star}, M^{\star}, H^{\star}, Z^{\star})
= & \argmax_{X, M, H, Z} \sum_{i \in \II} X_i C(m(i), \eventseq i, \traj i) \\
 & \mathrm{s.t.} \sum_{i \in \II} X_i \eventseq i = Y ,
\end{align}
where $C(m(i), \eventseq i, \traj i)$ is defined by
\begin{equation*}
C(m(i), \eventseq i, \traj i) =
\log P\left(\eventseq i | m(i), \traj i\right) +
\log P(\traj i \mid m(i)) -
\left ( \log |\mm| + \log 2 \right) ,
\end{equation*}
and we have dropped terms from the $\argmax$ that are constant
with respect to $(X, M, H, Z)$.

\todo{explicitly construct the index and show this is a linear program}

\section{Practical considerations}

{bootstrapping initial feasible solution so there is a dual solution that can be used to set initial prizes}\todo{elabourate}

{decomposition of MAP estimation problem into master relaxed exact cover problem and auxiliary prize-collecting Viterbi dynamic programming problem allows many complex problem specific details to be handled as part of the dynamic programming}\todo{elabourate}

{degeneracy / slow convergence of restricted relaxed master linear program}\todo{investigate and mitigate}

{exactly when is the dual solution used to set prizes well defined -- over which range?}\todo{investigate}

{efficient search over particular families of HMMs}\todo{investigate}

{embed into full branch and price regime}\todo{investigate}

{reducing wasted effort by reusing LP bookkeeping and prior soln to warm-start over successive LP solves}\todo{investigate}

\section{Variations}

{maybe hard equality constraint linking Y to Z is not pragmatic. if soften constraint and allow nonzero error $r$ one naive implementation might add factor $k \exp(-\frac{\norm{r}^2}{{\sigma^2}})$ to unlogged objective function. equivalently when objective is logged, this adds some $- \norm{r}^2$ term. seems to give some kind of quadratic optimisation problem. can decomposition still work?}\todo{investigate}

\section{bibliography}
\todo{setup bibtex etc}

\end{document}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}

\begin{document}
\logo
\rulename{Exponential Moving Average Trading Rule} %Argument is name of rule
\tblofcontents

\ruledescription{Exponential Moving Average (EMA) is a type of moving average similar to simple moving average, but it reduce the lag by applying more weight to recent prices. The weighting applied varies depending on the number of periods use in the moving average.
                Traders used this more to get a clearer view of the most recent price change of an asset.}

\section{Equation}
\begin{equation}
    EMA_{\currenttime} = \Big(V_{\currenttime} * \Big(\frac{S}{1 + \lookbacklength}\Big)\Big) + EMA_{\currenttime-1} * \Big(1-\frac{S}{1 + \lookbacklength}\Big)
\end{equation}
\\

where: \\

$EMA$ is exponentially weighted moving average. \\

$V_{\currenttime}$ is current stock value. \\

$\lookbacklength$ \ is look back length. \\

$S$ is the smoothing factor.

\ruleparameters
{Window size}{50}{This is the number of time steps over which exponential contributions are sourced.}{$\lookbacklength$}
{Smoothing Factor}{2}{Smoothing factor represents the weighting applied to the most recent period’s value.}{$S$}
\stoptable

\keyterms
\furtherlinks

\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{physics}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\relpenalty=10000
\binoppenalty=10000

\begin{document}
\section{Geometry revision}
\begin{enumerate}
  \item % http://www.pdmi.ras.ru/EIMI/2018/Baltic_way/bw18coord.pdf
  The points $A$, $B$, $C$, $D$ lie, in this order, on a circle $\omega$, where $AD$ is a diameter of $\omega$. Furthermore, $AB = BC = a$ and $CD = c$ for some relatively prime integers $a$ and $c$. Show that if the diameter $d$ of $\omega$ is also an integer, then either $d$ or $2d$ is a perfect square.

  \item %IMO SL2018
  Let $ABCDE$ be a convex pentagon such that $AB = BC = CD$, $\angle EAB =  \angle BCD$, and $\angle EDC = \angle CBA$. Prove that the perpendiular line from $E$ to $BC$ and the line segments $AC$ and $BD$ are concurrent.

  \item % http://www.math.olympiaadid.ut.ee/arhiiv/varia/bwtr/bw18tr/bw18tren.pdf
  The heights of triangle $ABC$ for triangle $A_1B_1C_1$. The heights of triangle $A_1B_1C_1$ form triangle $A_2B_2C_2$. Prove that $ABC \sim A_2B_2C_2$

  \item % http://www.math.olympiaadid.ut.ee/arhiiv/varia/bwtr/bw18tr/bw18tren.pdf
  Vertex $A$ of square $ABCD$ is symmetric to the midpoint of side $CD$ with respect to line $l$. Find the ratio of the areas of the two quadrilaterals on either side of line $l$ which make up the square.

  \item % http://www.math.olympiaadid.ut.ee/arhiiv/varia/bwtr/bw18tr/bw18tren.pdf
  Acute triangle $ABC$ has circumcircle $\omega$. The tangents of $\omega$ at points $B$ and $C$ intesect at $P$. $D$ and $E$ are the projections of $P$ to lines $AB$ and $AC$ respectively. Prove that the orthocentre of triangle $ADE$ coincides with the midpoint of line $BC$.

  \item % http://www.pdmi.ras.ru/EIMI/2018/Baltic_way/bw18coord.pdf
  The bisector of the $\angle A$ of a triangle $ABC$ intersects $BC$ in a point $D$ and intersects the circumcircle of the triangle $ABC$ in a point $E$. Let $K, L, M$ and $N$ be the midpoints of the segments $AB, BD, CD$ and $AC$, respectively. Let $P$ be the circumcenter of the triangle $EKL$, and $Q$ be the circumcenter of the triangle $EMN$. Prove that $\angle PEQ = \angle BAC$.

\end{enumerate}

\section{Auxilliary constructions} % http://www.math.olympiaadid.ut.ee/arhiiv/oppemat/eesti/lisakon.pdf
\begin{enumerate}[resume]
  \item
  Vertices $A,B,C,D$ of parallelogram are connected to the midpoints of sides $BC,CD,DA,AB$ respectively. The segments intersect at points $K,L,M,N$. Find the ratio of the areas of $KLMN$ and $ABCD$.

  \item
  In triangle $ABC$ $\angle A = 90 ^\circ$. Let $M$ be the midpoint of $AB$. Perpendicular line to $CM$ through $A$ intersects $AB$ at $P$. Prove that $\angle AMC = \angle BMP$.

  \item
    Let $ABC$ be an acute triangle and $D$ the projection of $A$ to side $BC$. Point $E$ lies on segment $AD$ and satisfies
    $$\frac{AE}{ED}=\frac{CD}{DB}$$
    Let $F$ be the projection of point $D$ to $BE$. Prove that $\angle AFC = 90 ^\circ$.
  \item
  Points $E$ and $F$ are chosen on the sides $CD$ and $BC$ of square $ABCD$, such that $\angle AEB = \angle AEF$. Find $\angle EAF$.
  \item
  The angle bisectors from points $A$ and $B$ of triangle $ABC$ intersect sides $BC$ and $AC$ at points $D$ and $E$ respectively. Find $\angle C$, if $AE + BD = AB$

  \item
  Let us have a parallelogram $ABCD$. A circle which goes through $A$ intersects segments $AB$, $AC$ and $AD$ at points $M$, $K$ and $N$. Prove that $|AB|\cdot|AM| + |AD|\cdot |AN| = |AK|\cdot|AC|$

\end{enumerate}

\end{document}


%!TEX root = ../notes.tex
\section{February 15, 2022}

\subsection{Cyclicity of Groups}
\subsubsection{mod odd \texorpdfstring{$p$}{p}}
\recall from last class, we had \cref{prop:d-roots}:
\begin{proposition*}
    If $p$ is a prime and if $d\mid p-1$, then the polynomial
    \[x^{d-1}\in (\ZZ/p\ZZ)[x]\] has exactly $d$ roots in the base field $\FF_p = \ZZ/p\ZZ$.
\end{proposition*}
\begin{corollary}\label{cor:generators-in-p}
    $G:=(\ZZ/p\ZZ)^\times$ is cyclic.
\end{corollary}
\begin{proof}
    For $d\mid (p-1)$, we write $\psi(d)$ for the number of elements of $G$ having order $d$.

    Proposition 2 implies that\footnote{We throw in Lagrange's theorem, and essentially count the number of solutions to $x^d\equiv 1$.}
    \[\sum_{c\mid d}\psi(c) = d\qquad(\psi* i = \id, \psi = \id * \mu)\]
    M\"obius inversion gives
    \[\psi(d) = \sum_{c\mid d}\mu(c)\frac{d}{c}.\]
    On the other hand, we have $\id = \phi * i\Rightarrow \phi = \mu * \id$. Thus $\psi(d) = \phi(d)$ for all $d\mid (p-1)$.
    So in particular, $\psi(p-1) = \phi(p-1)\geq 1$ for any prime $p$.
\end{proof}

\subsubsection{mod odd power \texorpdfstring{$p^e$}{p\^e}}
\begin{theorem}\label{thm:generators-in-powers-of-p}
    Let $p\in \ZZ_+$ be an odd prime, and let $e\geq 1$. Then $U(p^e)$ is cyclic.
\end{theorem}
Proof overview:
\begin{enumerate}[1.]
    \item Pick a primitive root mod $p$. We call it $g$ (for generator).
    \item Show that either $g$ or $g + p$ is a primitive root mod $p^2$.
    \item Show that if $h$ is any primitive root mod $p^2$, then $h$ is a primitive root mod $p^e$ $\forall e\geq 2$.
\end{enumerate}
\begin{proof}[Proof of \cref{thm:generators-in-powers-of-p}]
    ~\begin{description}
        \item[Step 1.] Let $g$ be a primitive root modulo $p$ given by \cref{cor:generators-in-p}.
        \item[Step 2.]  Let $d$ be the order of $g$ mod $p^2$. Since $\phi(p^2) = p(p-1)$, we have that
            \[d\mid p(p-1)\qquad\text{by Lagange}.\]
            By definition of $d$,
            \begin{align*}
                g^d\equiv 1\mod{p^2}
                \intertext{so we also have}
                g^d\equiv 1\mod{p}
            \end{align*}
            Thus $(p-1)\mid d$ since $g$ has order $p-1$ mod $p$. Altogether, $d$ is either $p-1$ or $p(p-1)$. If $d = p(p-1)$, then we are done with step 2. So we assume the former that $d = p-1$.

            Let $h = g + p$. We know that $h$ is a primitive root mod $p$, so we do the same [yoga] as above and conclude that the order of $h$ mod $p^2$ is either $p-1$ or $p(p-1)$.

            By our new hypothesis,
            \[g^{p-1}\equiv 1\pmod{p^2}\]
            so modulo $p^2$, we have
            \begin{align*}
                h^{p-1} = (g+p)^{p-1} & = g^{p-1} + (p-1)g^{p-2}p + \cdots + p^{p-1}
                \intertext{Modulo $p^2$, the only terms that survive are (expand and all $p^2$ terms die):}
                                      & \equiv 1 - pg^{p-2}\pmod{p^2}
            \end{align*}
            But $p\nmid g$, so $pg^{p-2}\not\equiv 0\mod{p}$, and hence $h^{p-1}\not\equiv 1\mod{p^2}$. Thus the order of $h$ mod $p^2$ is $p(p-1)$, so $h$ generates $U(p^2)$.

            So we are done with step $2$. If $g$ is a primitive root mod $p$, then either $g$ or $g + p$ is a primitive root mod $p^2$.
        \item[Step 3.] We wish to show that a primitive root mod $p^2$ is also a primitive root mod $p^e$ $\forall e\geq 2$. We induct on $e$.

            Let $h$ be a primitive root mod $p^e$ for some fixed $e\geq 2$. Let $d$ be the order of $h$ mod $p^{e+1}$. By Lagange, we have that $d\mid \phi(p^{e+1}) = p^e(p-1)$, and from step $2$,
            \[\phi(p^e) = p^{e-1}(p-1)\mid d\]
            Hence $d = p^e(p-1)$ or $p^{e-1}(p-1)$. If it's the former then we are done, so we assume latter.

            We want to show that
            \[h^{p^{e-1}(p-1)}\not\equiv 1\mod{p^{e+1}}\]
            implying that $d = p^e(p-1)$ after all.

            Since $h$ has order $\phi(p^e) = p^{e-1}(p-1)$ in $U(p^e)$, we have \begin{equation}
                h^{p^{e-2}(p-1)}\not\equiv 1\mod{p^e} \label{eqn:5.2-1}\tag{$\star$}
            \end{equation} However,
            \begin{equation}
                h^{p^{e-2}(p-1)}\equiv 1\mod{p^{e-1}} \label{eqn:5.2-2}\tag{$\star\star$}
            \end{equation}
            Combining \cref{eqn:5.2-1} and \cref{eqn:5.2-2} yields
            \[h^{p^{e-2}(p-1)}=1+kp^{e-1}\]
            where $p\nmid k$. Therefore, we have
            \begin{align*}
                h^{p^{e-1}(p-1)} & = (1+kp^{e-1})^p                                    \\
                                 & = 1 + pkp^{e-1} + \binom{p}{2}k^2p^{2e-2} + \cdots
                \intertext{Subsequent terms are all divisible by $p^{3e-3}=(p^{e-1})^3$, and hence divisible by $p^{e+1}$ as $e(e-1)\geq 2+1\ \forall e\geq 2$. Thus
                }
                h^{p^{e-1}(p-1)} & = 1 + kp^e + \frac{1}{2}k^2p^{2e-1}(p-1)\mod{p^e+1}
            \end{align*}
            $p$ is odd, so
            \[\frac{1}{2}k^2p^{2e-1}(p-1)\]
            is divisible by $p^{e+1}$, since $2e-1\geq e+1$. Thus
            \[h^{p^{e-1}(p-1)} \equiv 1 + kp^e \mod{p^e+1}\]
            Since $p\nmid k$, we get that $kp^e\not\equiv 0$ so
            \[h^{p^{e-1}(p-1)} \not\equiv 1 \mod{p^e+1}\]
            This proves that $d = p^e(p-1)$, which is to say that $h$ is a primitive root mod $p^{e+1}$.
    \end{description}
    Altogether, we have that $U(p^e)$ is cyclic.
\end{proof}

\subsubsection{mod powers of 2}
\begin{theorem}
    $U(2^e)$ is cyclic iff $e = 1$ or $e = 2$.
\end{theorem}
\begin{proof}
    Clearly $U(2)$ and $U(4)$ are cyclic\footnote{We don't have much choice since there is only one trivial group and one group of order $2$, both cyclic.}.

    We show that $U(2^e)$ is \emph{not} cyclic for all $e\geq 3$. Notice: it suffices to show that $U(8)$ is not cyclic, since we can find group homomorphisms down powers of $2$.
    \[U(8) = \{\overline{1}, \overline{3}, \overline{5}, \overline{7}\}\]
    and $\overline{1}^2 = \overline{3}^2 = \overline{5}^2 = \overline{7}^2\mod{8}$.
\end{proof}

\subsection{Classification of all cyclic unit groups}
\begin{corollary}\label{cor:cyclicity-of-unit-groups}
    $U(m)$ is cyclic if and only if $m = 1, 2, 4, p^e$ or $2p^e$ for some odd prime $p$.
\end{corollary}
\begin{proof}
    Recall that a product $G$ of finite cyclic groups $G_1$ and $G_2$ is cyclic iff $(|G_1|, |G_2|) = 1$.\footnote{Secretly, Chinese Remainder Theorem.} On the other hand, $\phi(m)$ is even $\forall m\geq 3$. So only one of $G_1$ and $G_2$ needs odd power.

    Combined with our structure theorems on $U(p^e)$ for primes $p$, this proves the corollary since these are the only possibilities.
\end{proof}


\section{Using Python to Model the Earth's Atmosphere}
This stream Simon was not feeling that well and it felt like his brain was not working, so be wary of errors! You have been warned. also the resolutin (size of an individual cell on the latitude
longitude grid) has been decreased to 5 degrees per cell instead of 3 degrees.

\subsection{Interpolating the Air Density}
In order to interpolate (see \autoref{sec:interpolation}) the air density, we need data. However currently we are just guessing the air density at higher levels, instead of taking real values.
So let us change that. For that we are going to use the U.S. Standard Atmosphere, an industry standard measure of the atmosphere on Earth \cite{usatmosp}. This data was provided in a text
(\texttt{TXT}) file which of course needs to be read in order for the data to be used in the model. Here we only care for the density and the temperature at a specific height. So the text file
only contains those two columns of the data (and the height in km of course as that is the index of the row, the property that uniquely identifies a row).

With that in mind, let's get coding and importing the data. We do this in \autoref{alg:usatmosp}. As one can see we do not specify how to open the file or how to split the read line, as this
is language specific and not interesting to describe in detail. I refer you to the internet to search for how to open a text file in the language you are working in. Keep in mind in which
magnitude you are working and in which magnitude the data is. If you work with $km$ for height and the data is in $m$, you need to account for that somewhere by either transforming the imported
data or work in the other magnitude.

\begin{algorithm}
    $data \leftarrow \text{open text file containing the us standard atmosphere data}$ \;
    \ForEach{$line \in data$}{
        Split $line$ into three components, $sh, st$ and $sd$, representing the height, temperature and density respectively \;
        $standardHeight.add(sh)$ \;
        $standardTemperature.add(st)$ \;
        $standardDensity.add(sd)$ \;
    }

    $densityProfile \leftarrow \texttt{interpolate}(heights, standardHeight, standardDensity)$ \;
    $temperatureProfile \leftarrow \texttt{interpolate}(heights, standardHeight, standardTemperature)$ \;

    \For{$alt \in [0, nlevels]$}{
        $\rho[:, :, alt] \leftarrow densityProfile[alt]$ \;
        $T_a[:, :, alt] \leftarrow temperatureProfile[alt]$ \;
    }
    \caption{Loading in the U.S. Standard Atmosphere}
    \label{alg:usatmosp}
\end{algorithm}

Note that the function \texttt{interpolate} takes three arguments, the first one being the data points that we want to have values for, the second one is the data points that we know and the
third one is the values for the data points that we know. This function may or may not exist in your programming language of choice, which might mean that you have to write it yourself.
The formula that we use for interpolation can be found in \autoref{eq:interpolation}, though you still need to figure out what value you need for $\lambda$ (see \autoref{sec:interpolation}).
This is left as an exercise for the reader.

\subsection{Fixing Vertical Motion}
Another attempt was made at fixing the vertical motion. The changes are incorporated in \autoref{alg:advection layer}. Do keep in mind that the low air density in the upper layers messes a lot
with the vertical motion. In other words, it kinda works but not really. Another idea to help fix it, is to introduce a variable called $top$ which indicates the highest point that the
atmosphere may have. This value is initialised as $8 \cdot 10^3$ in meters (so 8 $km$). We then change the definition of $heights$ to: An array of uniform thickness of $\frac{top}{nlevels} m$.
We also added the $\delta z$ to \autoref{alg:temperature layer} as that was something that was still missing.

The current theory why the vertical velocity is not right is that the vertical thermodynamics may be wrong. This will be investigated further and we will report on this in future sections.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{import}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{balance}
\usepackage{geometry}
\geometry{legalpaper,
portrait,
margin=2in,
lmargin = 2cm,
rmargin = 2cm,
}

\usepackage{multirow} % for tables with complicated header

\usepackage{booktabs,makecell,tabularx}
\renewcommand\theadfont{\small}
\renewcommand\theadgape{}

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
\begin{document}

\newcommand{\diag}{\operatorname{diag}}
\newcommand{\E}{\operatorname{E}} % expectation
\newcommand{\tr}{\operatorname{tr}} % trace
\newcommand{\iDFT}{\operatorname{IDFT}} %
\newcommand{\DFT}{\operatorname{DFT}} %
\newcommand{\iFFT}{\operatorname{IFFT}} %
\newcommand{\iSFFT}{\operatorname{ISFFT}} %
\newcommand{\SFFT}{\operatorname{SFFT}} %
\newcommand{\FFT}{\operatorname{FFT}} %
\newcommand{\suma}{\operatorname{sum}} %

\title{Full-rate STLC for Four Receive Antennas - Notes }
\author{radim.zedka@vut.cz }
\date{March 2022}
\maketitle

% =================================================
\section{Problem Description}

Formula (6) in \cite{b_FullRate_STLC} is given by
\begin{equation} \label{eq_xi_orig}
    \xi = \rho \frac{\Big( \sum_{p=0}^{M-1} \gamma_{p} \pm 2\mathcal{R}\big\{ \epsilon_p\big\} \Big)^2}{4\sum_{p'=0}^{M-1}\gamma_{p'}},
\end{equation}
% \begin{equation} \label{eq_xi_orig}
%     \xi = \rho \frac{\frac{M^2}{M^2}\Big( \sum_{p=0}^{M-1} \gamma_{p} \pm 2\mathcal{R}\big\{ \epsilon_p\big\} \Big)^2}{4\sum_{p'=0}^{M-1}\gamma_{p'}} = \rho \frac{M^2\Big( \frac{1}{M}\sum_{p=0}^{M-1} \gamma_{p} \pm 2\frac{1}{M} \mathcal{R}\big\{ \epsilon_p\big\} \Big)^2}{4\sum_{p'=0}^{M-1}\gamma_{p'}}\ \overrightarrow{\text{for high }M}\ = \frac{\rho}{4} \sum_{p=0}^{M-1} \gamma_{p},
% \end{equation}
% \begin{equation} \label{eq_xi_orig}
%     \lim_{M\to\infty} \xi = \frac{\rho}{4} \sum_{p=0}^{M-1} \gamma_{p}
% \end{equation}
% \begin{equation} \label{eq_xi_orig}
%     \lim_{M\to\infty} \frac{1}{M} \sum_{p=0}^{M-1} \mathcal{R}\big\{ \epsilon_p\big\} = 0
% \end{equation}
where
\begin{equation} \label{eq_gamma_p}
    \gamma_p = \sum_{q=0}^{3} |h_{p,q}|^2,
\end{equation}
and
\begin{equation} \label{eq_epsilon_p}
    \epsilon_p = h_{p,0}h_{p,2}^* + h_{p,1}h_{p,3}^*.
\end{equation}
Each complex channel gain $h_{p,q}$ is composed of two i.i.d. normal variables $a_{p,q}, b_{p,q} \sim \mathcal{N}(0,1/2)$ which relate to channel gain by $h_{p,q} = a_{p,q} + jb_{p,q}$, where $j = \sqrt{-1}$.
Formula \eqref{eq_gamma_p} then evolves into
\begin{equation} \label{eq_gamma_p_2}
    \gamma_p = \sum_{q=0}^{3} |a_{p,q}|^2 + |b_{p,q}|^2,
\end{equation}
and $\mathcal{R}\big\{ \epsilon_p\big\}$ is expressed as
\begin{equation} \label{eq_epsilon_p_2}
    \mathcal{R}\big\{ \epsilon_p\big\} = a_{p,0}a_{p,2} + b_{p,0}b_{p,2} + a_{p,1}a_{p,3} + b_{p,1}b_{p,3}.
\end{equation}
Formula \eqref{eq_xi_orig} may be expanded into purely real-valued form
\begin{equation} \label{eq_xi_2}
    \xi = \frac{\rho}{4} \frac{\Big( \sum_{p=0}^{M-1} \sum_{q=0}^{3} |a_{p,q}|^2 + |b_{p,q}|^2 \pm 2\mathcal{R}\big\{ \epsilon_p\big\} \Big)^2}{ \sum_{p'=0}^{M-1} \sum_{q=0}^{3} |a_{p',q}|^2 + |b_{p',q}|^2}.
\end{equation}
After Lemma 1 in \cite{b_FullRate_STLC} the receiver SNR is calculated via
\begin{equation} \label{eq_xi_Lemma}
    \xi =  \frac{\rho}{4} \sum_{p=0}^{M-1} \sum_{q=0}^{3} |a_{p,q}|^2 + |b_{p,q}|^2 .
\end{equation}

% =================================================
\section{MATLAB Simulation}

In MATLAB I created a script which generates the normal-distributed variables $a_{p,q}$ and $b_{p,q}$ in vectors of $10^7$ samples each. This way I calculate the histogram of formula \eqref{eq_xi_2} and I attempt to approximate it with Gamma distribution with PDF given by
\begin{equation}\label{eq_Gamma_PDF}
 f_{\Xi}(\xi) =  \frac{\xi^{nM - 1} e^{-\frac{\xi m}{2}}}{ (2/m)^{nM} \cdot \Gamma(nM)} \quad \forall \quad \xi \in \langle 0, \infty).
\end{equation}
where $n$ is related to the shape parameter and $m$ to the Gamma scale parameter. Please note that for PDF approximations I set $\rho = 1$.
As the error metric of the PDF approximation I used mean-squared-error defined as
\begin{equation}\label{eq_PDF_error_metric}
 J = \int \big( f_{\Xi}(\xi)' - f_{\Xi}(\xi) \big)^2 d\xi,
\end{equation}
where $f_{\Xi}(\xi)'$ is the simulated data histogram and $f_{\Xi}(\xi)$ is the approximation.

Diversity gain of systems approximated by Gamma distribution is given by
\begin{equation}\label{eq_DivGain_limit}
\kappa = -\lim_{\rho \to \infty}\frac{d \log_{10}(\bar{\varepsilon}(\rho))}{d \log_{10}(\rho)} = nM,
\end{equation}
where $\bar{\varepsilon}(\rho)$ is the average bit error rate at given $\rho$ and $n$ is the diversity gain coefficient. Table~\ref{table:1} then summarizes PDF approximation parameters $n$, $m$ for several values of $M$.

\begin{table}[t]
\caption{PDF fitting parameters of \eqref{eq_xi_2} at given $M$}
\label{table:1}
\centering
\small
\begin{tabular} {|c|c|c|c|}
\toprule
\thead {$M$} & \thead {$m$ } & \thead {$n$ } & \thead {$J$ [dB] }  \\
    \midrule
$1$ & $1.530445$ & $0.949676$  & $-41.6985$\\
$2$ & $1.574221$ & $0.886598$  & $-46.4452$\\
$4$ & $1.596783$ & $0.847111$  & $-55.8000$\\
$8$ & $1.600477$ & $0.824861$  & $-58.9625$\\
$16$ & $1.606456$ & $0.815408$  & $-60.4560$\\
$32$ & $1.610120$ & $0.811129$  & $-61.2368$\\
$64$ & $1.608200$ & $0.807196$  & $-63.6193$\\
$128$ & $1.613510$ & $0.808271$  & $-62.2914$\\
$256$ & $1.612725$ & $0.807231$  & $-64.0791$\\
$512$ & $1.610609$ & $0.805685$  & $-66.8909$\\
    \bottomrule
    \end{tabular}
\end{table}

Figures \ref{fig_PDF_M1} to \ref{fig_PDF_M512} I present the results for $M = \{1,32,64,256,512\}$ where formula \eqref{eq_xi_2} is fitted by \eqref{eq_Gamma_PDF} with $m$ and $n$ according to Table~\ref{table:1}.

\begin{figure}[!h] \centering
\includegraphics[scale=0.8]{images/PDF_M_1.eps}
\caption{PDF of \eqref{eq_xi_2} approximated by \eqref{eq_Gamma_PDF} for $M=1$. }
\label{fig_PDF_M1}
\end{figure}

\begin{figure}[!h] \centering
\includegraphics[scale=0.8]{images/PDF_M_32.eps}
\caption{PDF of \eqref{eq_xi_2} approximated by \eqref{eq_Gamma_PDF} for $M=32$ . }
\label{fig_PDF_M32}
\end{figure}

\begin{figure}[!h] \centering
\includegraphics[scale=0.8]{images/PDF_M_64.eps}
\caption{PDF of \eqref{eq_xi_2} approximated by \eqref{eq_Gamma_PDF} for $M=64$ . }
\label{fig_PDF_M64}
\end{figure}

\begin{figure}[!h] \centering
\includegraphics[scale=0.8]{images/PDF_M_256.eps}
\caption{PDF of \eqref{eq_xi_2} approximated by \eqref{eq_Gamma_PDF} for $M=256$ . }
\label{fig_PDF_M256}
\end{figure}

\begin{figure}[!h] \centering
\includegraphics[scale=0.8]{images/PDF_M_512.eps}
\caption{PDF of \eqref{eq_xi_2} approximated by \eqref{eq_Gamma_PDF} for $M=512$ . }
\label{fig_PDF_M512}
\end{figure}

% \begin{figure}[!h]
% \begin{tabular}{ll}
% \includegraphics[scale=0.53]{images/PDF_M_32.eps}
% &
% \includegraphics[scale=0.53]{images/PDF_M_64.eps}
% \end{tabular}
% \caption{PDF of \eqref{eq_xi_2} and \eqref{eq_xi_Lemma} with scaled chi-squared PDFs \eqref{eq_ChiSquared_PDF_scaled} for $M=32$ and $M=64$. }
% \label{fig_PDF_1}
% \end{figure}

% \begin{figure}[!h]
% \begin{tabular}{ll}
% \includegraphics[scale=0.53]{images/PDF_M_256.eps}
% &
% \includegraphics[scale=0.53]{images/PDF_M_512.eps}
% \end{tabular}
% \caption{PDF of \eqref{eq_xi_2} and \eqref{eq_xi_Lemma} with scaled chi-squared PDFs \eqref{eq_ChiSquared_PDF_scaled} for $M=256$ and $M=512$. }
% \label{fig_PDF_2}
% \end{figure}

% =================================================
\section{Discussion}

My MATLAB simulation attepts show that \eqref{eq_xi_2} is close to Gamma distribution with $n = 0.8$ and $m = 1.6$. The diversity gain is for Gamma distribution directly given by $nM$. Parameter $n$ is decreasing with increasing value of $M$ and it is approaching $n \sim 0.8$.
I would be very grateful for your advice.

% ======================== References ====================
\begin{thebibliography}{00}

\bibitem{b_FullRate_STLC} S. -chan Lim and J. Joung, “Full-Rate Space–Time Line Code for Four Receive Antennas”, IEEE wireless communications letters, pp. 1-1, 2021.

\end{thebibliography}

\end{document}


% Chapter Template

\chapter{Background Information and Theory} % Main chapter title

\label{Chapter2} % Change X to a consecutive number; for referencing this chapter elsewhere, use \ref{ChapterX}

%----------------------------------------------------------------------------------------
%	SECTION 1
%----------------------------------------------------------------------------------------

\section{Machine Learning}
% \begin{note}
%   \begin{itemize}
%     \item Una definición rápida
%     \item Clasificación y regresión
%     \item Cross-validation
%     \item Qué son los datos de train y test, y por qué se hace esa partición
%     \item Qué es el sobre-ajuste
%   \end{itemize}
% \end{note}

\begin{pre-delivery}
  % Machine Learning uses statistical and mathematical models to give answers to
  % problems when there is no known formula of procedure to compute the answer.

  Machine Learning uses statistical and mathematical models to give
  computational answers based on data to problems when there is no known
  formula of procedure.

  In the subfield of Supervised Learning, the objective is to predict a numerical
  or categorical variable in response to some input data, and the way of doing
  it is to feed the model with lots of different examples for which we already
  know the correct answer, and we expect the models to be able to predict
  the correct answer to instances that it hasn't seen before. When it does,
  we say that the model is able to generalize.

  When a model is trained with some data, there is always a risk of overfitting
  \cite{hawkins2004problem}.
  For a model to overfit means that it adjusts very well to the data that is
  has seen, but can't predict the correct answer to new, unseen data.
  % This
  % happens because it has not only from the relevant information, but also
  % from the random noise that the data had, and so it can only memorise, but
  % not generalize.
  This happens because it has not only fitted the relevant information,
  but also random noise present in the data sample, and thus it generalizes
  poorly. In the extreme case of ovefitting, the model tends to memorise the
  data sample.

  For this reason, when a Machine Learning algorithm is trained the data
  is split in two subsets, a \textit{Training  dataset} and a \textit{Testing
  dataset}. The training dataset will be used to train the model, while the
  Testing datasets will be used only to check it. If a model has generalized
  well, it will achieve a good accuracy score on both the training and the
  testing dataset, but if it has overfitted it will show good results in the
  training dataset and bad ones in the testing dataset.

  Many models need some parameters to tune the behaviour of the algorithm. For
  example, some of them are used to adjust how much a model will fit to the data.
  We usually call these ``hyperparameters''. The correct value for them is not
  straightforward, and it is normally chosen with a resampling process called
  ``cross-validation''\cite{geisser2017predictive}. This process consists of
  splitting the training dataset
  in many subsets and check many possible values for the hyperparameters in order
  to see which one gets a higher accuracy with unseen data.
\end{pre-delivery}

\section{Some currently used Machine Learning models}

% \begin{note}
  % \section{Review de los principales modelos que existen}
% \end{note}
  \subsection{Decision Tree}
  \label{sec:dec-tree}
  % \begin{note}
  %   \begin{itemize}
  %     \item No se basa en productos escalares
  %     \item Es extremadamente rápido
  %     \item Es más fácil de interpretar que otros modelos
  %     \item Es extremadamente inestable
  %     \item Cuando se hace un Random Forest, se randommiza un poco, de modo que
  %     árboles distintos entrenados con los mismos datos pueden ser destintos
  %     \item Es un modelo no lineal
  %   \end{itemize}
  % \end{note}
  \begin{pre-delivery}
    Decision Tree\cite{breiman2017classification}\cite{lewis2000introduction}
    is a predictive model which uses
    the training data to build
    a tree where each node splits the data in two sets according to some
    feature, and the leafs contain the set of instances that belong to some class
    (in classification problems) or that has a similar numerical response variable
    (for regression problems).

    To predict the answer to a new instance, it uses the features to ``decide''
    the nodes to cross until it reaches a leaf. The response given is the
    most prevailing class in the leaf for classification problems, or the mean
    of the values of the rest of the instances in the leaf.

    To decide what feature to use to split a node in two subsets, it uses
    the Gini impurity: it will pick the feature that minimises the sum of
    the Gini impurity of the two child nodes. Given a node with instances
    belonging to $k$ classes, if $p_i$ is the proportion of instances that
    belong to class $i$, the Gini impurity of the node is
    $1 - \sum_{i = 1}^k p_i^2$.

    Decision Trees have the advantages that it is easy to interpret the
    tree produced and that it is very fast to build the tree. The way to avoid
    overfitting is to limit its growth.

    These models are very unstable. This means that small differences in the
    training data can produce very different Decision Trees. This property
    is very useful to build an ensemble of estimators to produce better answers.
    Random Forest is an algorithm that trains many Decision Trees with some sort
    of randomization.
  \end{pre-delivery}
  \subsection{Logistic Regression}
  \label{ssec:log-reg}
  \begin{pre-delivery}
    Logistic Regression\cite{cox1958regression} models the probability that an instance belongs to
    a class, and predicts the class with a higher probability. To do so it
    uses the \textit{logistic sigmoid function}\cite{han1995influence}, defined by:

    \begin{equation}
      \sigma(a) = \frac{1}{1 + exp(-a)}
    \end{equation}

     Once the vector $w \in \reals^d$ has been found, the predicted probability
     that an instance $\vx \in \reals^d$ belongs to a class is
     $y(\vx) = \sigma(w^\transp\vx)$.

     % To find a suitable $w$ it solves an optimization problem of finding $w$
     % that maximizes the likelihood that each of the instances belongs to the
     % specified class

     Given
     $D = \{\bm{\chi}, \bm{t}\}$
     , where
     $\bm{\chi} = \{\bm{x}_1, \ldots \bm{x}_n\}$, $\bm{x}_i \in \reals^d$, $\bm{t} = \{0, 1\}^n$
     it tries to maximize a likelihood function that can be written

     \begin{equation}
       p(\bm{t} | w) = \prod_{i = 1}^n y_i^{\bm{t}_i} (1 - y_i)^{1 - \bm{t}_i}
     \end{equation}

     where $y_i = \sigma(w^\transp\vx_i + w_0)$.
     % , the problem is to find $w \in \reals^d$ and $c \in \reals$ that minimizes

     % \begin{equation}
     %   \frac{1}{2}w^\transp w + \sum_{i = 1}^n log\left( exp\left( -y_i(\vx_i^\transp w + c) \right) + 1\right)
     % \end{equation}
  \end{pre-delivery}
  \subsection{Support Vector Machines}
  \label{ssec:svm}
  % \begin{note}
  %   \begin{itemize}
  %     \item Inicialmente pensadas para clasificación en 2 clases
  %     \item Pero se puede más clases con \eng{one-vs-rest} y también hay
  %     formas de hacer regresión
  %     \item Se basa únicamente en el producto escalar de sus entradas
  %     \item Intenta separar los datos con un híper-plano
  %     \item Actualmente es poco eficiente usarlas porque su coste s cúbico
  %     con la cantidad de entradas.
  %     \item Las fórmulas que quiere optimizar
  %   \end{itemize}
  % \end{note}

  \begin{pre-delivery}
    Support Vector Machine\cite{Cortes1995} (SVM) is a model that finds in hyperplane that
    divides the data in two sets. In two-class classification problems, each
    side of the hyperplane contains the instances of each of the classes.
    It does so by converting the problem to an optimization one.

    Given some data
    $D = \{\bm{\chi}, \bm{y}\}$
    , where
    $\bm{\chi} = \{\bm{x}_1, \ldots \bm{x}_n\}$, $\bm{x}_i \in \reals^d$, $\bm{y} = \{-1, +1\}^n$
    , the optimization problem consists on finding $\bm{\alpha} \in \reals^n$
    the maximises

\begin{equation}
  L = \sum_{i = 1}^n\alpha_i -\frac{1}{2}\sum_{i = 1}^n\sum_{j = 1}^n\alpha_i\alpha_jy_iy_j\vx_i^\transp\vx_j
\end{equation}

subject to

\begin{align}
  0 \leq \alpha_i \leq C; \forall i\\
  \sum_{i = 1}^n \alpha_iy_i = 0
\end{align}

% $C$ is an hyperparameter to tune the amount of penalization for miss-classified
% instances.

$C$ is an hyper-parameter to tune the amount of penalization for missclassified
instances or instances located within the margin zone.

If we compute
\begin{equation}
  w = \sum_{i = 1}^n\alpha_iy_i\vx_i
\end{equation}
and

\begin{equation}
  b = y_i - w\cdot\vx_i
\end{equation}
for any $i$ so that $\alpha_i \neq 0$, we can compute the class of $\vx_0$ with

\begin{equation}
sign(w\cdot\vx_0 + b)
\end{equation}

Note that this algorithm just uses the dot product of the input data, not the
data itself. This property allows us to use the Kernel Trick with them.
See \ref{sec:kern-trick}

\end{pre-delivery}

\section{Ensemble Methods}
\label{sec:ens-meth}
  % \subsection{Bagging}
    % \begin{note}
    %   \begin{itemize}
    %     \item Bagging
    %     \begin{itemize}
    %       \item Inventado por Leo Breiman (referencia)
    %       \item Pretende reducir el sesgo
    %       \item Wikipedia dice que pretende reducir la varianza
    %       \item Es el boosting el que pretende reducir el sesgo
    %       \item Entrenamiento de los estimadores es independiente, se podría
    %       hacer en paralelo
    %       \item Actualmente casi solo se usa con DT, debido a su inestabilidad
    %     \end{itemize}
    %     \item Bootstrap
    %     \begin{itemize}
    %       \item Intenta solucionar el problema de que para bagging es bueno
    %       que los estimadores sean distintos
    %       \item Idealmente usaríamos un dataset distinto para cada estimador
    %       \item Consiste en hacer un resalmpling con repetición
    %       \item Si la cantidad de instancias del original es la misma que la de cada uno
    %       de los subconjuntos, se espera que la proporción de elementos úncos sea de
    %       $1 - \frac{1}{e} \approx 0.632$.
    %       \item Si el conjunto original tiene $n$ elementos, y tu haces un subconjunto
    %       de tamaño $r$, puedes esperar que la proporción de elementos del original que
    %       sí tienen presencia en el nuevo sea de $1 - e^{-\frac{r}{n}}$
    %     \end{itemize}
    %     \item Random Forest
    %   \end{itemize}
    % \end{note}

  \begin{pre-delivery}
    Ensemble methods\cite{polikar2006ensemble} are a technique used in Machine
    Learning to reduce the
    overall accuracy error of a basic classification or regression model. The
    idea is that a commimtee of models is expected to learn better than a single
    one.

    Some ensemble methods are focused on decreasing the error caused by the
    variance of the model. One example is \textit{Bagging}\cite{breiman1996bagging}. Others are focused
    on decreasing the bias error, like \textit{Boosting}\cite{freund1997decision}.

    In Bagging, every model in the ensemble vote with equal weight. Thus, it is
    important to promote the variance among each of the models, since not doing
    it would be equivalent to training just one model. Ideally, one would train
    each of the models with totally different datasets, with no correlation
    among them. But in practice this is not always possible, because of a
    limited number of instances to train. One alternative is to use a
    technique called \textit{Bootstrap}\cite{efron1994introduction}. Bootstrap allows to generate
    many different instances of a dataset by performing a resampling.

    Given a dataset $D$ of size $n$, Bootstrap generates $m$ new datasets
    $D_i$ of size $n$ by sampling instances from $D$ uniformly and with
    replacement. This means that some of the instances in $D$ may be repeated
    in $D_i$, and others may not appear at all. With a large $n$, it is expected
    that each dataset $D_i$ will contain $63.2 \% $ of the instances in $D$.

    Theoretically Bagging could be used with any kind of method. However, for
    most of them Bootstrap is not enough to decorrelate the estimators.
    In practice, Bagging is mostly used with Decision Tree, given that this
    method produces very different trees with a small variation in the data.
    Random Forest\cite{Breiman2001} is an algorithm that trains many Decision Trees with a
    Bagging. Instead of building the tree in a deterministic way, in each
    split it chooses a random subset of features on which to perform the
    separation. Besides, it lets the estimators overfit, since it has a positive
    impact in reducing the overall variance of the Forest.
  \end{pre-delivery}

\section{The kernel trick}
\label{sec:kern-trick}

% \begin{note}
%   \begin{itemize}
%     \item Teorema de Bochner
%     \item El kernel RBF
%     \begin{itemize}
%       \item Su fórmula es \ldots
%       \item Equivalencia entre $\gamma$ y $\sigma$
%       \item La noción de similitud que tiene
%       \item \Hspace\ es de dimensionalidad infinita
%       \item Permite ajustarse infinitamente a los datos, tuneando el
%       híper-parámetro
%       \item $\sigma$ más pequeño, más sobreajuste
%       \item $\gamma$ más grande, más sobreajuste
%     \end{itemize}
%   \end{itemize}
% \end{note}

\begin{pre-delivery}
  A Kernel\cite{bergman1970kernel} is a function that equals to the inner product of inputs mapped into
  some Hilbert Space
  \footnote{A Hilbert space is a generalization of the Euclidean Space which contains
  the structure of an inner product that allows length and angle to be
  measured.}
  , i.e:
  \begin{equation}
  \kernel(x,y) = \phi(x)\cdot\phi(y)
\end{equation}
% A Hilbert space is just a generalization of the Euclidean Space which contains
% the structure of an inner product that allows length and angle to be measured.

They are interesting in Machine Learning because we don't need to know the
explicit function $\phi(\cdot)$. In fact, $\phi(\cdot)$ could map the data to
a Hilbert Space with infinite dimensions, and we could still compute
$\phi(\vx)\cdot\phi(\vy)$ through the kernel $\kernel$

Support Vector Machines (explained in \ref{ssec:svm}) can benefit a lot of
Kernel Functions. SVMs solve an
optimization problem to maximise

\begin{equation}
  L = \sum_{i = 1}^n\alpha_i -\frac{1}{2}\sum_{i = 1}^n\sum_{j = 1}^n\alpha_i\alpha_jy_iy_j\vx_i^\transp\vx_j
\end{equation}

in order to find an hyperplane that separates the data points in two classes.
But with some problems there may not exist such hyperplane, and so it would
be needed to map the data to a different feature space. If we did that, then
the function to maximise would be

\begin{equation}
  L = \sum_{i = 1}^n\alpha_i -\frac{1}{2}\sum_{i = 1}^n\sum_{j = 1}^n\alpha_i\alpha_jy_iy_j\phi(\vx_i)^\transp\phi(\vx_j)
\end{equation}

As we said previously, SVMs don't work with the data points alone, but just with
their inner products. Thus, a Kernel could be used to define the optimization
problem as

\begin{equation}
  L = \sum_{i = 1}^n\alpha_i -\frac{1}{2}\sum_{i = 1}^n\sum_{j = 1}^n\alpha_i\alpha_jy_iy_j\kernel(\vx_i, \vx_j)
\end{equation}

This approach has one big advantage:
% first, we don't need to explicitly
% compute $\phi(\vx)^\transp\phi(\vy)$, which could have a high cost if the
% new dimensionality was too big.
as long as the learning technique relies
only on the inner product of the input, the underlying mapping $\phi(\cdot)$
does not need to be explicitly calculated and can, in fact, be unknown\cite{burges1998tutorial}.

Kernel functions can be characterised with the Mercer's condition
\cite{mercer1909functions}. It says that given a function $\kernel(\vx, \vy)$,
there exists a mapping $\phi(\cdot)$ so that
$\kernel(\vx, \vy) = \phi(\vx)\cdot\phi(\vy)$
if and only if for any $g(\vx)$ such that $\int g(\vx)^2 d\vx$ is finite then
$\int \kernel(\vx, \vy)g(\vx)g(\vy) \geq 0$.

There are many known Kernels. One that is very popular is the Radial Basis
Function Kernel\cite{vert2004primer}, RBF. This kernel is defined as:
\begin{equation}
\kernel(\vx,\vy) = \semiRbf
\end{equation}
where $\gamma > 0$ is a free parameter. The value of this Kernel decreases with the
euclidean distance of the parameters, so it can be interpreted as a measure
of similarity. The feature space of this kernel has infinite number of
dimensions.

When a kernel is used with an SVM, the answer can be computed with

\begin{equation}
sign\left(\sum_{i = 1}^n \alpha_iy_i\kernel(\vx_i, \vx)\right)
\end{equation}
SVMs using the RBF kernel have a huge ability to fit to the data, and is able
to separate classes for very difficult problems. The problem is that the
optimization of the function

\end{pre-delivery}
  % \subsection{The RBF kernel}

\section{Random Fourier Features}

% \begin{note}
%   \begin{itemize}
%     \item Teorema de bochner
%     \item Tiene que ser un shift invariant kernel
%     \item Es más, tiene que ser un positive definite shift-invariant kernel
%     \item Converge bounds for ability to approximate
%     \item Instead, we propose to factor the kernel function itself
%     \item La factorización no depende de los datos
%     \item we propose explicitly mapping
% the data to a low-dimensional Euclidean inner product space using a randomized feature map z :
% Rd --> RD so that the inner product between a pair of transformed points approximates their kernel
% evaluation
% \item Puesto que los valores están entre -1 y 1, hay un teorema que asegura
% la convergencia exponencial hacia el kernel real
%   \end{itemize}
% \end{note}

\begin{pre-delivery}
  A kernel function
  $\kernel(\vx, \vy)$ with $\vx, \vy \in \reals^d$
  equals the inner product of inputs mapped with some function $\phi(\cdot)$,
  so that
  $\phi(\vx)^\transp\cdot\phi(\vy) = \kernel(\vx, \vy)$.
  But $\phi(\cdot)$ could be a mapping to an infinitely-dimensional space, so
  calculating $\phi(\vx)$ is not possible for some kernels.

  Random Fourier Features\cite{rahimi2008random} provide a way to, given a
  kernel $\kernel(\vx, \vy)$,
  explicitly map the data to a
  low-dimensional Euclidean inner product space using a randomized feature
  map $z: \reals^d \mapsto \reals^D$ so that the inner product between a pair
  of transformed points approximates their kernel evaluation, i.e:

  \begin{equation}
    \kernel(\vx, \vy) = \phi(\vx)^\transp\cdot\phi(\vy) \approx z(\vx)^\transp\cdot z(\vy)
  \end{equation}

  To approximate the RBF kernel, it uses the Bochner's Theorem, which says:

\newtheorem{theorem}{Theorem}
  \begin{theorem}
    \cite{rudin1962fourier}
    A continuous kernel $\kernel(x, y) = \kernel(x - y)$ on $\reals^D$  is
    positive definite if and only if $k(\delta)$ is the
    Fourier Transform of a non-negative measure.
  \end{theorem}

  % Since RBF is defined as $\kernel(\vx, \vy) = e^{-\gamma\norm{\vx - \vy}^2}$

  Since it is known that RBF is shift-invariant and positive definite, then
  its Fourier transform is a proper probability distribution, and so

  \begin{equation}
    \kernel(\vx - \vy) = \int_{\reals^D} p(w)e^{iw^\transp (\vx-\vy)}
    = \int_{\reals^D} p(w)cos\left(w^\transp (\vx-\vy)\right)
  \end{equation}

  A random feature can be obtained by picking $w \sim \{\mathcal{N}(0, 2\gamma)\}^d$
  and $b \sim \mathcal{U}(0, 2\pi)$
  and computing $\sqrt{2}cos(w^\transp\vx + b)$. To generate a lower variance
  approximation of $\phi(\vx)$ with $D$ features we can concatenate $D$ randomly
  chosen features $(f_1, \ldots, f_D)$ into a column vector and normalize each
  component by $\sqrt{D}$.

  It is guaranteed an exponentially fast convergence in $D$ between
  $z(\vx)^\transp z(\vy)$ and $\kernel(\vx, \vy)$.

\end{pre-delivery}

\section{\Nys}

\begin{pre-delivery}
  The \Nys\cite{NIPS2000_1866} method is a general method for low-rank approximations of
  kernels. It achieves this by subsampling the data on which the kernel
  is evaluated.

  In kernel methods the data can be represented in a kernel matrix $K$, where
  $K_{i,j} = \kernel(\vx_i, \vx_j)$. The problem of these methods is their
  high computational cost associated with the kernel matrix: with non-linear
  kernels, the cost of training the model is cubic with the number of
  instances, something unacceptable for large-scale problems.

  The \Nys\ method consists on generating an approximation of the kernel matrix of
  ranq $q$, where $q$ can be a lot smaller than the number of instances, without
  any significant decrease in the accuracy of the solution. This way, if there
  are $n$ instances in a dataset, the complexity can be reduced from
  $\mathcal{O}(n^3)$ to $\mathcal{O}(nq^2)$.

  With \Nys, given a kernel $\kernel(\vx, \vy) = \phi(\vx)\cdot\phi(\vy)$, one can
  construct a mapping $z: \reals^d \mapsto \reals^q$ so that
  $z(\vx) \approx \phi(\vx)$. This function defines each component $j$ as
  $z_j(\vy) = \frac{1}{q}\sum_{i = 1}^q \kernel(\vy, \vx_i)g_i(\vx_i)$,
  where $\vx_1, \ldots, \vx_q$ are some chosen instances and
  $g_i(\cdot)$ comes from a column from the Singular Value Decomposition
  of the approximated kernel matrix.
\end{pre-delivery}


\documentclass[11pt, a4wide]{article}   	% use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{a4paper}                   		% ... or a4paper or a5paper or ...

%Custom commands
\newcommand{\data}{\text{data}}
\newcommand{\sessions}{s}
\newcommand{\purchases}{p}
\newcommand{\sepu}{\sessions, \purchases}
\newcommand{\convr}{R}
\newcommand{\ushi}{\text{UH}}

%Packages
\usepackage{graphicx}
\usepackage{amssymb, amsmath}

\usepackage{a4wide}
\usepackage{listings,url}
\usepackage[retainorgcmds]{IEEEtrantools}

\title{A Bayesian Approach to AB Testing [Draft]\\{\small Introducing a Python Engine for Testing}}
\author{Hugo Pibernat and Nuria Duran}
%\date{}

\begin{document}
\maketitle
\section{Introduction}
The main goal of our AB-Test is to decide which of two different versions of the same product performs better on a specific set of metrics. In this report we present a Bayesian approach to tackle this objective.

A Bayesian model includes a prior distribution which enables us to provide information about the parameters $KPI_A$ and $KPI_B$ governing our data \emph{before} observing the data. The information included in the data is taken into account by means of the likelihood function. Combining both the prior and the likelihood function results in a posteriori distribution, a probability distribution of the parameters (the metric of each group, in this case) which will help us to decide which of the version performs better.

The (joint) posterior probability distribution is defined as follows:

\begin{equation}
P(\text{KPI}_A,\text{KPI}_B|data)
\end{equation}

In order to compute this distribution, we will need the Bayes theorem, which states that

\begin{equation}
P(X|Y) = \frac{P(Y|X)\cdot P(X)}{P(Y)}
\end{equation}

%\section{Bayesian models}
\section{Binomial Variables: Modelling Conversion Rate}

The history of a user (UH) looks as follows:
\begin{equation}
<0,0,1,0,0,0,1,0,0,1,0,1,1,0,1>
\end{equation}
where 0 represents a non-converted visit and 1 represents a converted visit. Hence,
\begin{equation}
\ushi\sim Bin(n,\theta)
\end{equation}
where $n\in \mathbb{Z}^{+}$ and $\theta\in[0,1]$ is the parameter we want to model with the posteriori distribution once given the priori distribution $P(\theta)$ and the likelihood function. We usually call this parameter Conversion Rate (CR).

\subsection{Choosing the prior distribution}
The prior distribution $P(\theta)$ enables us to include some knowledge in the final distribution of the KPI that we are modelling. There are several considerations when choosing the prior. However, for the sake of simplicity, in this case we will use the Beta distribution, which has a support of $[0,1]$, exactly the range of values of CR:

\begin{equation}
Beta_{\alpha,\beta}(\theta) = \frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)}
\end{equation}

where $\alpha$ and $\beta$ are the parameters of the distribution and B is the beta function, defined as follows:

\begin{equation}
B(\alpha,\beta) = \int_0^1t^{\alpha-1}(1-t)^{\beta-1}{B(\alpha,\beta)}
\end{equation}

 A particularly useful set of parameters is $\alpha=\beta=1$ which results in the uniform distribution. That is, a priori it is considered that all the values a parameter can take are equally likely to be taken.

\subsection{Obtaining the posteriori distribution}

To compute the posteriori distribution, apart from the priori distribution, we need the likelihood function, the probability mass function of the Binomial distribution in our case:
\begin{equation}
P(X=k)=\binom{n}{k}\theta^k(1-\theta)^{n-k}
\end{equation}

In order to compute the posteriori distribution, we will use the Bayes theorem:

\begin{equation}
\label{eq:bayesconvr}
P(\theta|n,k) = \frac{P(n,k|\theta)\cdot P(\theta)}{P(n,k)}
\end{equation}

We therefore need to obtain $P(n,k|\theta)$ and $P(\theta)$. As it will be seen later, the value of P(n,k) does not actually need to be computed.

First, we have
\begin{equation}
P(n,k|\theta) = \binom{n}{k}\theta^k(1-\theta)^{n-k}
\end{equation}

Back to Equation~\ref{eq:bayesconvr} we now have that

\begin{equation}
\begin{array}{c}
P(\theta|n,k) = \\
\\
=  \frac{P(n,k|\theta)\cdot P(\theta)}{P(n,k)}  = \\
\\
=  \frac{\binom{n}{k}\theta^k(1-\theta)^{n-k} \cdot \frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)}}{P(n,k)} = \\
\\
=  \frac{\binom{n}{k}}{P(n,k)B(\alpha,\beta)} \cdot \theta^{k + \alpha-1}(1-\theta)^{n-k+\beta-1}
\end{array}
\end{equation}

We can rewrite the previous equation as:
\begin{equation}
P(\theta|n,k) = \lambda \cdot \theta^{k + \alpha-1}(1-\theta)^{n-k+\beta-1}
\end{equation}

where
\begin{equation}
\lambda = \binom{n}{k}\frac{1}{P(n,k)B(\alpha,\beta)}
\end{equation}

Hence, our posteriori distribution for the $\theta$ is:

\begin{equation}
P(\theta|n,k) = Beta_{n+\alpha,n-k+\beta}(\theta)
\end{equation}

Or, in terms of sessions $\sessions$, purchases $\purchases$, and conversion rate $\convr$:

\begin{equation}
P(\convr|\sepu) = Beta_{\sessions+\alpha,\sessions-\purchases+\beta}(\theta)
\end{equation}

\subsection{Running an AB-Test on Conversion Rate}

Let's focus now on obtaining the probability that one conversion rate group is higher than the other (although the result is easily generalisable to $n$ test groups). That is:

\begin{equation}
\label{eq:integral-binomial}
P(\convr_A > \convr_B|\data) = \iint\limits_{\convr_A>\convr_B} P(\convr_A,\convr_B|\data)\; d\convr_A\; d\convr_B
\end{equation}

where $P(\convr_A,\convr_B|\data)$ is the joint distribution of both conversion rates. If we can assume independence of our tests groups\footnote{Although it is quite difficult that with our sparse social graph any two events are really independent, this is still quite a general assumption in most of our tests.}, then the following holds:

\begin{equation}
\begin{array}{c}
P(\convr_A,\convr_B|\data) = \\
\\
= P(\convr_A|\data_A)\cdot P(\convr_B|\data_B) = \\
\\
= \frac{P(\data_A|\convr_A)P(\convr_A)}{P(\data_A)} \cdot \frac{P(\data_B|\convr_B)P(\convr_B)}{P(\data_B)} = \\
\\
= \frac{P(\data_A|\convr_A)P(\convr_A)P(\data_B|\convr_B)P(\convr_B)}{P(\data_A)P(\data_B)}
\end{array}
\end{equation}

where $P(\data_A|\convr_A)$, $P(\convr_A)$, on the one hand, and $P(\data_B|\convr_B)$, $P(\convr_B)$, on the other hand, stand for the \emph{likelihood function} and the \emph{prior distribution}, and they can be obtained as explained above. Look in the \emph{BayesianABTest.py} file for an implementation of this and the other methods explained in this paper. Or run some of the examples in \emph{examples\_ABTests.py} to see  the method in action.

\section{Gaussian Variables: Modelling Attempts per Level}

When our data follow a Gaussian Distribution

$$X\sim N(\mu,\sigma^2)$$

the prior distribution becomes slightly more complicated. In this case we need to obtain a joint distribution of the form:

\begin{equation}
P(\mu,\sigma^2)
\end{equation}

From Gelman et al.~[4], we know that it can be described as follows:

\begin{equation}
P(\mu,\sigma^2) = P(\sigma^2)P(\mu|\sigma^2)
\end{equation}

where $P(\sigma^2)$ is an Inverse Gamma Distribution and $P(\mu|\sigma^2)$ is also a Gaussian Distribution.

Following Gelman's indications we can sample the posteriori distribution and then run the AB-Test using the code from \emph{BayesianABTest.py}.

\section{Log-Normal Variables: Modelling Spend}

When, for instance, we are modelling user spend, data are positive. Therefore, a Gaussian (or Normal) distribution is not appropriate due to its $(-\infty,+\infty)$ support.

In those situations, a Log-Normal distribution might fit our data more accurately. A Log-Normal Distribution is such that if $X$ is distributed log-normally, then $ln(X)$ is distributed normally:

\begin{equation}
\text{if}\;\; X\sim Log-N(\mu,\sigma^2) \text{   then   } ln(X) \sim N(\mu,\sigma^2)
\end{equation}

In a Log-Normal distribution the $\mu$ and $\sigma^2$ are not the mean and the variance. The mean, variance, median, and mode from a Log-Normal distribution are computed as follows:

\begin{itemize}
\item Mean: $e^{\mu+\frac{\sigma^2}{2}}$
\item Variance: $(e^{\sigma^2}-1)e^{2\mu+\sigma^2}$
\item Median: $e^{\mu}$
\item Mode: $e^{\mu-\sigma^2}$
\end{itemize}

An examplary code to sample the posteriori distribution can be found in \emph{BayesianABTest.py}.

\section{Modelling Spend per Session with a Two-Part Model Approach}
When our data contain a substantial amount of zeroes, a log-normal distribution is not appropriate, as its support is strictly positive. In this situation, we can first model the zeros vs. not zeros with a Binomial distribution, then the non-zeroes with a Log normal distribution, and obtain our final posteriori distribution as the product of these two.

The following code runs an AB-Test on such a combination of variables: each set of variable values is randomly sampled, and then each random value from the first set is multiplied by one random value from the second set. Look at the file \emph{BayesianABTest.py} for a possible implementation of this method.

\section{References}
\begin{enumerate}
\item Evan Miller. How Not To Run An A/B Test.
Available at: \url{http://www.evanmiller.org/how-not-to-run-an-ab-test.html}
\item Chris Stucchio. \emph{Analyzing conversion rates with Bayes Rule (Bayesian statistics tutorial)}. Available at: \url{http://www.chrisstucchio.com/blog/2013/bayesian_analysis_conversion_rates.html}
\item Sergey Feldman. \emph{Rich Relevance, Collection on Bayesian Testing}. Available at: \url{http://engineering.richrelevance.com/category/bayesian/}
\item Gelman,~A. et al. \emph{Bayesian Data Analysis}, 3rd Edition. CRC Press, 2013.
\end{enumerate}

\end{document}


\section{Introduction}

% This project is an assignment by Prof. Fanucci to Mancini and Origlia.
% The purpose of the project is to practice electronics design using
% the hardware description language VHDL and Vivado, after attending the courses
% of \textit{Electronics and communications systems} at the Computer Engineering
% Master Degree Courses (for Mancini) and \textit{Microelecronics for telecommunications}
% at the Telecommunications Engineering Master Degree (for Origlia).

The assignment consists of the design of a Voice Activity Detector (VAD), a
system that takes an audio stream as input and it outputs a logical value that
indicates whether the voice is present or absent.

\subsection{Specifications}
\begin{itemize}
  \item The input stream is divided in frames of duration
  $T_{frame} = 16\si{\milli\second}$
  \item The sampling rate is $f_s = 16 \si{\kilo\hertz}$
  \item The mean frame energy is $ E_{frame} = \frac{1}{N}\sum_{n=1}^N x^2[n] $
  \item The samples range from -1 to 1: $x[n] \in [-1, 1)$
  \item The mean frame energy is compared to a given threshold:
  $ E_{threshold} = 0.05 $
  \item The input data are represented in 2 complement using 16 bits
  \item The ports of the VAD are:
  \begin{itemize}
    \item \texttt{x}: 16 bits representing the samples
    \item \texttt{FRAME\_START}: it signals the start of the frame by
    a pulse of 1 clock cycle
    \item \texttt{rst\_n}: the system active low reset
    \item \texttt{clk}: the system clock
    \item \texttt{VAD}: the output. High for \textit{voice detected},
    low otherwise.
  \end{itemize}
\end{itemize}

\begin{figure}[]
  \centering
  \includegraphics[width=0.75\textwidth]{figs/vad_schematic.png}
  \caption{Schematic for the VAD component}
  \label{fig:schematic}
\end{figure}

\subsection{Source code availability}
The source code is freely available on Github: \url{https://github.com/moriglia/VAD}.


\documentclass[rnaas]{aastex62}

\pdfoutput=1

\usepackage{lmodern}
\usepackage{microtype}
\usepackage{url}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
\usepackage{multirow}
\usepackage{graphicx}
\bibliographystyle{aasjournal}

% ------------------ %
% end of AASTeX mods %
% ------------------ %

% Projects:
\newcommand{\project}[1]{\textsf{#1}}
\newcommand{\kepler}{\project{Kepler}}
\newcommand{\ktwo}{\project{K2}}

% references to text content
\newcommand{\documentname}{\textsl{Note}}
\newcommand{\figureref}[1]{\ref{fig:#1}}
\newcommand{\Figure}[1]{Figure~\figureref{#1}}
\newcommand{\figurelabel}[1]{\label{fig:#1}}
\renewcommand{\eqref}[1]{\ref{eq:#1}}
\newcommand{\Eq}[1]{Equation~(\eqref{#1})}
\newcommand{\eq}[1]{\Eq{#1}}
\newcommand{\eqalt}[1]{Equation~\eqref{#1}}
\newcommand{\eqlabel}[1]{\label{eq:#1}}

% TODOs
\newcommand{\todo}[3]{{\color{#2}\emph{#1}: #3}}
\newcommand{\dfmtodo}[1]{\todo{DFM}{red}{#1}}
\newcommand{\alltodo}[1]{\todo{TEAM}{red}{#1}}
\newcommand{\citeme}{{\color{red}(citation needed)}}

% math
\newcommand{\T}{\ensuremath{\mathrm{T}}}
\newcommand{\dd}{\ensuremath{ \mathrm{d}}}
\newcommand{\unit}[1]{{\ensuremath{ \mathrm{#1}}}}
\newcommand{\bvec}[1]{{\ensuremath{\boldsymbol{#1}}}}
\newcommand{\Gaussian}[3]{\ensuremath{\frac{1}{|2\pi #2|^\frac{1}{2}}
            \exp\left[ -\frac{1}{2}#1^\top #2^{-1} #1 \right]}}

% VECTORS AND MATRICES USED IN THIS PAPER
\newcommand{\Normal}{\ensuremath{\mathcal{N}}}
\newcommand{\mA}{\ensuremath{\bvec{A}}}
\newcommand{\mC}{\ensuremath{\bvec{C}}}
\newcommand{\mS}{\ensuremath{\bvec{\Sigma}}}
\newcommand{\mL}{\ensuremath{\bvec{\Lambda}}}
\newcommand{\vw}{\ensuremath{\bvec{w}}}
\newcommand{\vy}{\ensuremath{\bvec{y}}}
\newcommand{\vt}{\ensuremath{\bvec{\theta}}}
\newcommand{\vm}{\ensuremath{\bvec{\mu}(\bvec{\theta})}}
\newcommand{\vre}{\ensuremath{\bvec{r}}}
\newcommand{\vh}{\ensuremath{\bvec{h}}}
\newcommand{\vk}{\ensuremath{\bvec{k}}}

% typography obsessions
\setlength{\parindent}{3.0ex}

\begin{document}\raggedbottom\sloppy\sloppypar\frenchspacing

\title{%
    Scalable backpropagation for Gaussian Processes using celerite
}

\author[0000-0002-9328-5652]{Daniel Foreman-Mackey}
\affil{Center for Computational Astrophysics, Flatiron Institute, New York, NY}

\keywords{%
methods: data analysis ---
methods: statistical
}

\section{Introduction}

This research note presents a derivation and implementation of efficient and
scalable gradient computations using the \emph{celerite} algorithm for Gaussian
Process (GP) modeling.
The algorithms are derived in a ``reverse accumulation'' or
``backpropagation'' framework and they can be easily integrated into existing
automatic differentiation frameworks to provide a scalable method for
evaluating the gradients of the GP likelihood with respect to all input
parameters.
The algorithm derived in this note uses less memory and is more efficient than
versions using automatic differentiation and the computational cost scales
linearly with the number of data points.

GPs \citep{Rasmussen:2006} are a class of models used
extensively in the astrophysical literature to model stochastic processes.
The applications are broad-ranging and some examples include the time domain
variability of astronomical sources \citep{Brewer:2009, Kelly:2014,
Haywood:2014, Rajpaul:2015, Foreman-Mackey:2017, Angus:2018}, data-driven
models of light curves or stellar spectra \citep{Wang:2012, Luger:2016,
Czekala:2017}, and the cosmic microwave background
\citep{Bond:1987,Wandelt:2003}.
In all of these applications, the calculation and optimization of the GP
marginalized likelihood function (here we follow the notation of
\citealt{Foreman-Mackey:2017})
\begin{eqnarray}\eqlabel{loglike}
\log \mathcal{L}(\bvec{\theta},\,\bvec{\alpha}) &=&
    -\frac{1}{2}\,\left[\bvec{y} - \bvec{\mu}_\bvec{\theta}\right]^\T\,
        {K_\bvec{\alpha}}^{-1}\,\left[\bvec{y}-\bvec{\mu}_\bvec{\theta}\right]
    -\frac{1}{2}\,\log\det K_\bvec{\alpha} + \mathrm{constant}
\end{eqnarray}
is generally the computational bottleneck.
The details of these models are omitted here (see \citealt{Rasmussen:2006} and
\citealt{Foreman-Mackey:2017} for details), but the key point is that, for a
dataset with $N$ data points, every evaluation of a GP model requires
computing the log-determinant and multiplying a vector by the inverse of
%\footnote{Or, more specifically, a linear operator
%that can left multiply a vector or a matrix by the inverse of this matrix.}
the $N \times N$ covariance matrix $K_\bvec{\alpha}$.
The computational cost of these operations scales as $\mathcal{O}(N^3)$ in the
general case, but the \emph{celerite} method was recently introduced in the
astronomical literature to compute the GP likelihood for a class of
one-dimensional models with $\mathcal{O}(N)$ scaling
\citep{Ambikasaran:2015, Foreman-Mackey:2017}.

The details of the \emph{celerite} method can be found in
\citet{Foreman-Mackey:2017} and I will not repeat them here.
The only difference in notation is that all the matrices in what follows are
the ``pre-conditioned'' matrices that are indicated with a tilde by
\citet{Foreman-Mackey:2017}.
The tilde is not included here for simplicity and to improve the readability
of the algorithms.
I will also use the symbol $P$ for the $(N-1) \times J$ pre-conditioning matrix
that was called $\phi$ by \citet{Foreman-Mackey:2017}.
The Cholesky factorization algorithm derived by \citet[][their
Equation~46]{Foreman-Mackey:2017} is as follows:

\medskip
\begin{minipage}{\linewidth}
\textbf{function} \texttt{celerite\_factor}($U$, $P$, $\bvec{d}$, $W$) \\
\hspace*{2em}\textsf{\# At input $\bvec{d} = \bvec{a}$ and $W = V$} \\
\hspace*{2em}$S \gets$ \texttt{zeros}($J$, $J$) \\
    \hspace*{2em}$\bvec{w}_1 \gets \bvec{w}_1 / d_{1}$ \\
\hspace*{2em}\textbf{for} $n = 2,\ldots,N$:\\
\hspace*{2em}\hspace*{2em}$S \gets \texttt{diag}(\bvec{p}_{n-1})\,[
    S + d_{n-1}\,{\bvec{w}_{n-1}}^\T\,{\bvec{w}_{n-1}}
]\,\texttt{diag}(\bvec{p}_{n-1})$ \\
\hspace*{2em}\hspace*{2em}$d_{n} \gets d_{n} - \bvec{u}_n\,S\,{\bvec{u}_n}^\T$\\
\hspace*{2em}\hspace*{2em}$\bvec{w}_n \gets \left[\bvec{w}_n -
    \bvec{u}_n\,S \right] / d_{n}$\\
    \hspace*{2em}\textbf{return} $\bvec{d}$, $W$, $S$
\end{minipage}
\medskip

\noindent In this algorithm, the \texttt{zeros}$(J,\,K)$ function creates a $J
\times K$ matrix of zeros, the \texttt{diag} function creates a diagonal
matrix from a vector, and $\bvec{x}_n$ indicates a row vector made from the
$n$-th row of the matrix $X$.
The computational cost of this algorithm scales as $\mathcal{O}(N\,J^2)$.
Using this factorization, the log-determinant of $K$ is
\begin{eqnarray}
\log \det K &=& \sum_{n=1}^N \log d_{n} \quad.
\end{eqnarray}
Similarly, \citet{Foreman-Mackey:2017} derived a $\mathcal{O}(N\,J)$ algorithm
to apply the inverse of $K$ (i.e.\ compute $Z = K^{-1}\,Y$) as follows
(Equations~47 and~48 in \citealt{Foreman-Mackey:2017}):

\medskip
\begin{minipage}{\linewidth}
\textbf{function} \texttt{celerite\_solve}($U$, $P$, $\bvec{d}$, $W$, $Z$) \\
\hspace*{2em}\textsf{\# At input $Z = Y$} \\
\hspace*{2em}$F \gets$ \texttt{zeros}($J$, $N_\mathrm{rhs}$) \\
\hspace*{2em}\textbf{for} $n = 2,\ldots,N$:\\
\hspace*{2em}\hspace*{2em}$F \gets \texttt{diag}(\bvec{p}_{n-1})\,[F +
    {\bvec{w}_{n-1}}^\T\,\bvec{z}_{n-1}]$\\
\hspace*{2em}\hspace*{2em}$\bvec{z}_n \gets \bvec{z}_n - \bvec{u}_n\,F$\\
\hspace*{2em}\textbf{for} $n = 1,\ldots,N$:\\
\hspace*{2em}\hspace*{2em}$\bvec{z}_n \gets \bvec{z}_n / d_{n}$\\
\hspace*{2em}$G \gets$ \texttt{zeros}($J$, $N_\mathrm{rhs}$) \\
\hspace*{2em}\textbf{for} $n = N-1,\ldots,1$:\\
\hspace*{2em}\hspace*{2em}$G \gets \texttt{diag}(\bvec{p}_{n})\,[G +
    {\bvec{u}_{n+1}}^\T\,\bvec{z}_{n+1}]$\\
    \hspace*{2em}\hspace*{2em}$\bvec{z}_n \gets \bvec{z}_n - \bvec{w}_n\,G$\\
\hspace*{2em}\textbf{return} $Z$, $F$, $G$
\end{minipage}
\medskip

\noindent The empirical scaling of these algorithms is shown in
\Figure{figure}.

\section{Gradients of GP models using celerite}

It is standard practice to make inferences using \Eq{loglike} by optimizing or
choosing a prior and sampling with respect to $\bvec{\theta}$ and
$\bvec{\alpha}$.
Many numerical inference methods (like non-linear optimization or Hamiltonian
Monte Carlo) can benefit from efficient calculation of the gradient of
\Eq{loglike} with respect to the parameters.
The standard method of computing these gradients uses the identity
\citep{Rasmussen:2006}
\begin{eqnarray}\eqlabel{naive-grad}
\frac{\dd \log \mathcal{L}}{\dd \alpha_k} &=&
    \frac{1}{2}\,\mathrm{Tr}\left[
        \left[
        \bvec{\tilde{r}}\,\bvec{\tilde{r}}^\T - {K_\bvec{\alpha}^{-1}}
        \right]
        \,\frac{\dd K}{\dd \alpha_k}
    \right]
\end{eqnarray}
where
\begin{eqnarray}
    \bvec{\tilde{r}} &=&
        {K_\bvec{\alpha}}^{-1}\,\left[\bvec{y}-\bvec{\mu}_\bvec{\theta}\right]
    \quad.
\end{eqnarray}
Similar equations exist for the parameters $\bvec{\theta}$.
Even with a scalable method of applying ${K_\bvec{\alpha}}^{-1}$, the
computational cost of \Eq{naive-grad} scales as $\mathcal{O}(N^2)$.
This scaling is prohibitive when applying the \emph{celerite} method to large
datasets and I have not found a simple analytic method of improving this
scaling for semi-separable matrices.
However, it was recently demonstrated that substantial computational gains can
be made by directly differentiating Cholesky factorization algorithms even in
the general case \citep{Murray:2016}.

Following this reasoning and using the notation from an excellent review of
matrix gradients \citep{Giles:2008}, I present the reverse-mode gradients of
the \emph{celerite} method.
While not yet popular within astrophysics, ``reverse accumulation'' of
gradients (also known as ``backpropagation'') has recently revolutionized the
field of machine learning \citep[see][for example]{LeCun:2015} by enabling the
non-linear optimization of models with large numbers of parameters.
The review \citep{Giles:2008} provides a thorough overview of these methods
and the interested reader is directed to that discussion for details and for
an explanation of the notation.

Using the notation from \citet{Giles:2008}, after some tedious
algebra, the reverse accumulation function corresponding to
\texttt{celerite\_factor} is found to be:

\medskip
\begin{minipage}{\linewidth}
\textbf{function} \texttt{celerite\_factor\_rev}($U$, $P$, $\bvec{d}$, $W$,
    $S$, $\bar{S}$, $\bar{\bvec{a}}$, $\bar{V}$) \\
\hspace*{2em}\textsf{\# At input $\bar{\bvec{a}} = \bar{\bvec{d}}$ and
    $\bar{V} = \bar{W}$}\\
\hspace*{2em}$\bar{U} \gets \mathrm{zeros}(N,\,J)$\\
\hspace*{2em}$\bar{P} \gets \mathrm{zeros}(N-1,\,J)$\\
\hspace*{2em}$\bar{\bvec{v}}_N \gets \bar{\bvec{v}}_N / d_N$\\
\hspace*{2em}\textbf{for} $n = N,\ldots,2$:\\
\hspace*{2em}\hspace*{2em}$\bar{a}_n \gets \bar{a}_n -
    \bvec{w}_n\,{\bar{\bvec{v}}_n}^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{u}}_n \gets - [\bar{\bvec{v}}_n +
    2\,\bar{a}_n\,\bvec{u}_n]\,S$\\
\hspace*{2em}\hspace*{2em}$\bar{S} \gets \bar{S} -
    {\bvec{u}_n}^\T\,[\bar{\bvec{v}}_n + \bar{a}_n\,\bvec{u}_n]$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{p}}_{n-1} \gets \mathrm{diag}(\bar{S}\,S
    \,\mathrm{diag}(\bvec{p}_{n-1})^{-1} + \mathrm{diag}(\bvec{p}_{n-1})^{-1}\,
    S\,\bar{S})$\\
\hspace*{2em}\hspace*{2em}$\bar{S} \gets \mathrm{diag}(\bvec{p}_{n-1})\,
    \bar{S}\,\mathrm{diag}(\bvec{p}_{n-1})$\\
\hspace*{2em}\hspace*{2em}$\bar{d}_{n-1} \gets \bar{d}_{n-1} +
    \bvec{w}_{n-1}\,\bar{S}\,{\bvec{w}_{n-1}}^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{v}}_{n-1} \gets \bar{\bvec{v}}_{n-1}
    / d_{n-1} + \bvec{w}_{n-1}\,[\bar{S} + \bar{S}^\T]$\\
\hspace*{2em}\hspace*{2em}$S \gets \mathrm{diag}(\bvec{p}_{n-1})^{-1}\,S\,
    \mathrm{diag}(\bvec{p}_{n-1})^{-1}
    - d_{n-1}\,{\bvec{w}_{n-1}}^\T\,\bvec{w}_{n-1}$\\
\hspace*{2em}$\bar{a}_1 \gets \bar{a}_1 -
    \bar{\bvec{v}}_1\,{\bvec{w}_1}^\T$\\
\hspace*{2em}\textbf{return} $\bar{U}$, $\bar{P}$, $\bar{\bvec{a}}$, $\bar{V}$
\end{minipage}
\medskip

\noindent Similarly, the reverse accumulation function for to
\texttt{celerite\_solve} is:

\medskip
\begin{minipage}{\linewidth}
\textbf{function} \texttt{celerite\_solve\_rev}($U$, $P$, $\bvec{d}$, $W$,
    $Z$, $F$, $G$, $\bar{F}$, $\bar{G}$, $\bar{Y}$) \\
\hspace*{2em}\textsf{\# At input $\bar{Y} = \bar{Z}$}\\
\hspace*{2em}$\bar{U} \gets \mathrm{zeros}(N,\,J)$\\
\hspace*{2em}$\bar{P} \gets \mathrm{zeros}(N-1,\,J)$\\
\hspace*{2em}$\bar{\bvec{d}} \gets \mathrm{zeros}(N)$\\
\hspace*{2em}$\bar{W} \gets \mathrm{zeros}(N,\,J)$\\
\hspace*{2em}\textbf{for} $n = 1,\ldots,N-1$:\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{w}}_n \gets
    - \bar{\bvec{y}}_n\,G^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{G} \gets \bar{G} - {\bvec{w}_n}^\T \,
    \bar{\bvec{y}}_n$\\
\hspace*{2em}\hspace*{2em}$\bvec{z}_n \gets \bvec{z}_n + \bvec{w}_n\,G$\\
\hspace*{2em}\hspace*{2em}$G \gets \mathrm{diag}(\bvec{p}_n)^{-1}\,G$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{p}}_n \gets \mathrm{diag}(
    \bar{G}\,G^\T)$\\
\hspace*{2em}\hspace*{2em}$\bar{G} \gets
    \mathrm{diag}(\bvec{p}_n)\,\bar{G}$\\
\hspace*{2em}\hspace*{2em}$G \gets G - {\bvec{u}_{n+1}}^\T\,\bvec{z}_{n+1}$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{u}}_{n+1} \gets
    \bvec{z}_{n+1}\,\bar{G}^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{y}}_{n+1} \gets
    \bvec{u}_{n+1}\,\bar{G}$\\
\hspace*{2em}\textbf{for} $n = 1,\ldots,N$:\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{y}}_n \gets \bar{\bvec{y}}_n/d_n$\\
\hspace*{2em}\hspace*{2em}$\bar{d}_n \gets -
    \bvec{z}_n\,{\bar{\bvec{y}}_n}^\T$\\
\hspace*{2em}\textbf{for} $n = N,\ldots,2$:\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{u}}_n \gets \bar{\bvec{u}}_n
    - \bar{\bvec{y}}_n\,F^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{F} \gets \bar{F} - {\bvec{u}_n}^\T \,
    \bar{\bvec{y}}_n$\\
\hspace*{2em}\hspace*{2em}$F \gets \mathrm{diag}(\bvec{p}_{n-1})^{-1}\,F$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{p}}_{n-1} \gets \bar{\bvec{p}}_{n-1} +
    \mathrm{diag}(\bar{F}\,F^\T)$\\
\hspace*{2em}\hspace*{2em}$\bar{F} \gets
    \mathrm{diag}(\bvec{p}_{n-1})\,\bar{F}$\\
\hspace*{2em}\hspace*{2em}$F \gets F - {\bvec{w}_{n-1}}^\T\,\bvec{z}_{n-1}$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{w}}_{n-1} \gets \bar{\bvec{w}}_{n-1} +
    \bvec{z}_{n-1}\,\bar{F}^\T$\\
\hspace*{2em}\hspace*{2em}$\bar{\bvec{y}}_{n-1} \gets \bar{\bvec{y}}_{n-1} +
    \bvec{w}_{n-1}\,\bar{F}$\\
\hspace*{2em}\textbf{return} $\bar{U}$, $\bar{P}$, $\bar{\bvec{d}}$,
    $\bar{W}$, $\bar{Y}$
\end{minipage}
\medskip

\noindent A reference C++ implementation of this algorithm can be found online
\citep{Foreman-Mackey:2018} and \Figure{figure} shows the performance of this
implementation.

\newpage
\section{Discussion}

This research note presents the algorithms needed to efficiently compute
gradients of GP models applied to large datasets using the \emph{celerite}
method.
These developments increase the performance of inference methods based on
\emph{celerite} and improve the convergence properties of non-linear
optimization routines.
Furthermore, the derivation of reverse accumulation algorithms for
\emph{celerite} allow its integration into popular model building and
automatic differentiation libraries like Stan \citep{Carpenter:2015},
TensorFlow \citep{Abadi:2016}, and others.

\acknowledgments
It is a pleasure to thank
Eric Agol,
Sivaram Ambikasaran, and
Victor Minden
for conversations that inspired this work.
A reference implementation of these algorithms, benchmarks, and tests can be
found at \url{https://github.com/dfm/celerite-grad} and
\citet{Foreman-Mackey:2018}.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{figure.pdf}
\caption{%
    The empirical computational scaling of the algorithms presented in this
    note.
    \emph{(top row)}: The cost of computing \eq{loglike} as a
    function of the number of data points ($N$; left) and the model complexity
    ($J$; right).
    \emph{(bottom row)}: The cost of computing the gradient of \eq{loglike}
    with respect to the vector $\bvec{a}$ and the matrices $U$, $V$, and $P$.
    In the left panels, each line corresponds to a different value of $J$ as
    indicated in the legend (with $J$ increasing from bottom to top).
    Similarly, in the right panels, the lines correspond to different values
    of $N$ increasing from bottom to top.
    In both cases, the theoretical scaling is $\mathcal{O}(N,J^2)$.
\figurelabel{figure}}
\end{center}
\end{figure}

\bibliography{celerite}

\end{document}


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

\chapter{Free field theories}

\section{Lagrangian and Hamiltonian formalisms}

\marginpar{Saturday\\ 2020-5-2, \\ compiled \\ \today}

The Lagrangian and Hamiltonian formalism can aid in the description of systems with either a finite of infinite number of degree of freedom.

\subsection{Classical system with finite DoF: Lagrangian formalism}

The usual example is a system of particles labelled by the index \(i\) with masses \(m_i\), positions \(q_i (t)\) and velocities \(\dot{q}_{i}(t)\).

If the forces acting on the particles are \textbf{conservative}, we can express them in terms of a potential, which we assume not to depend on the velocities nor on time: \(V(q_{i})\).
From now on when we write \(q\) or \(\dot{q}\) we will mean the full vector of the positions or velocities.

If this is the case, the motion of the particles is described by Newton's equation:
%
\begin{align}
m_i \ddot{q}_{i} = - \pdv{V}{q_{i}}
\,.
\end{align}

This can alternatively be described in terms of a function called the Lagrangian:
%
\begin{align}
L(q, \dot{q}, t)
\,,
\end{align}
%
which could depend on time, but we usually assume it to be independent of time explicitly, that is, \(\partial_{t} L = 0\) (although the \emph{total} derivative of the Lagrangian may be nonzero!).

The condition of the Lagrangian being explicitly time-independent is equivalent to the system of forces being conservative.

The Lagrangian can be a generic function of the positions and velocities of the particles, but in order to reproduce Newton's law we need it to be in the form
%
\begin{align}
L = T-V = \frac{1}{2} m_i \dot{q}_i^2 - V(q_i)
\,,
\end{align}
%
where a sum over the particles is implied in the kinetic energy term.

Typically problems in Lagrangian mechanics are given by fixing the boundary conditions as the initial and final position, as opposed to writing the initial values of position and velocity.

We want to find the physical trajectory the particle(s) will take corresponding to those initial and final conditions.\footnote{There can be issues arising from this approach: for instance, the process for finding trajectories may find many equivalent ones, for example in situations with some symmetry or nontrivial topology.
In the gravitational two-body problem we can ask what is the stationary action path needed in order to reach the antipodal point to the current one: there are infinite ones, corresponding to the choice of azimuthal angle of the initial velocity.
However, this is not really a problem, since the approach we are about to introduce --- Hamilton's principle of stationary action --- just provides a formulation which eventually yields the same differential equations as the initial value problem; physically the velocity as well as the position of the particles is well determined in the initial moment.}

This can be accomplished using Hamilton's principle of stationary action. We start by introducing the action:
%
\begin{align}
S[q(t), t _{\text{in}}, t _{\text{fin}}] = \int_{t _{\text{in}}}^{t _{\text{fin}}} L(q(t), \dot{q}(t)) \dd{t}
\,,
\end{align}
%
which depends on the full \emph{path} \(q(t)\): the integral is computed by evaluating the Lagrangian along it.
Because of this the action \(S\) is called a \emph{functional}, since it is a function of a function. The Lagrangian, on the other hand, is  a regular function.

We then state the following:
\begin{claim}
The path taken physically by the system satisfies the stationary action principle:
%
\begin{align}
\delta S = 0
\,.
\end{align}

The variation is the functional derivative of \(S\), meant to be a variation in the infinite-dimensional space of possible curves.
\end{claim}

This is not a proven principle, but rather an axiom to be taken, analyzed and confronted with experiment.

The action principle, as stated, only guarantees that the action should be stationary: it could be a minimum or a maximum.
There are considerations to be made about stability: a maximum-action path will solve the equations of motion and thus be physical, but it will be unstable. This is why the principle is often referred to as the minimum-action principle.

Let us calculate what the variation of the action means physically. We start by considering two nearby paths, \(\gamma \) and \(\gamma '\), such that
%
\begin{align}
\gamma ' = \gamma + \delta_0 \gamma
\,,
\end{align}
%
so that the coordinates \(q\) at a time \(t\) are given by
%
\begin{align}
q_{\gamma } (t)
\qquad \text{and} \qquad
q_{\gamma '} (t) = q_{\gamma }(t) + \delta_0 q_{\gamma }(t)
\,,
\end{align}
%
and we impose that the variation of the path, \(\delta_0 q_{\gamma } (t)\), is zero at the initial and final time, so that the boundary conditions are satisfied: \(\delta_0 q_{\gamma }(t _{\text{in}}) = 0 = \delta_0 q_{\gamma }(t _{\text{fin}})\).

The reason for the subscript \(0\) for the variation \(\delta \) is that we consider \emph{synchronous} variations: the difference between the perturbed and unperturbed trajectory, at a \emph{fixed time}.
Also, we take the synchronous variation of the action:
%
\begin{subequations}
\begin{align}
\delta_0 S [q_{\gamma }] &= S [q_{\gamma } + \delta_0 q_{\gamma }] - S[q_{\gamma }]  \\
&= \int_{t _{\text{in}}}^{t _{\text{fin}}} \delta_0 L (q_{\gamma }, \dot{q}_{\gamma }) \dd{t}  \\
&= \int_{t _{\text{in}}}^{t _{\text{fin}}} \qty[ \pdv{L}{q} \cdot \delta_0 q + \pdv{L}{\dot{q}} \cdot \dot{q}] \dd{t}  \\
&= \int_{t _{\text{in}}}^{t _{\text{fin}}} \qty[\pdv{L}{q}  - \dv{}{t} \pdv{L}{\dot{q}}] \cdot \delta_0 q \dd{t}
\,,
\end{align}
\end{subequations}
%
since, as we are considering synchronous variations, \(\delta_0 \) and \(\partial_{t}\) commute; also we neglected boundary terms in the integration by parts as they are set to zero by the fact that the variation of the path,  \(\delta_0 q_{\gamma }\), is zero at the boundaries.

Since this must hold for any variation of the path, by the fundamental lemma of the calculus of variation the integrand must vanish: this yields the Euler-Lagrange equations
%
\begin{align}
\pdv{L}{q} - \dv[]{}{t} \pdv{L}{\dot{q}} = 0
\,.
\end{align}

Note that there is \textbf{gauge freedom} in the choice of Lagrangian: an easy symmetry to see is the scaling one; the Lagrange equations of \(L\) and \(cL\) for \(c \in \mathbb{R}\) are the same.
Also, the equations for \(L\) and \(L + \dv*{F}{t}\) are the same: the action is the integral in time of the Lagrangian, therefore adding a total derivative to it shifts the action by a constant \(\eval{\Delta F}_{\text{in}}^{\text{fin}}\), which vanishes when taking the variation.

\subsection{Classical system with finite DoF: Hamiltonian formalism}

We start off by defining the momenta: in component form, they are
%
\begin{align}
p_{i} = \pdv{L}{\dot{q}^{i}}
\,.
\end{align}

As long as the Lagrangian is well behaved\footnote{The formal requirement is that the matrix
%
\begin{align}
\pdv[2]{L}{\dot{q}}{\dot{q}}
\,
\end{align}
%
should be invertible.}
we can move between momenta and velocities, by using the inverse relation \(\dot{q} = \dot{q} (q, p)\).

The Hamiltonian is then defined as
%
\begin{align}
H(q, p) = \dot{q}(q, p) \cdot p - L (q, \dot{q}(q, p))
\,,
\end{align}
%
and this procedure is called a \emph{Legendre transform}.
Note that since we assumed the Lagrangian to have no explicit dependence of time the Hamiltonian will not depend on time either. This is tied to the conservation of energy: if the Lagrangian is given by \(T - V\) the Hamiltonian is given by \(T + V\), and thus represents the total energy of the system. A theorem in Hamiltonian mechanics is
%
\begin{align}
\dv{H}{t} = \pdv{H}{t}
\,,
\end{align}
%
so the variation of the energy in the evolution of the system (total derivative) is equal to the partial derivative of the Hamiltonian with respect to time.

Let us take the functional derivative of the Hamiltonian: using indices for the clarity of the tensorial structure, we find
%
\begin{subequations}
\begin{align}
\delta H &= \pdv{H}{p_{i}}  \delta p_{i} + \pdv{H}{q^{i}} \delta q^{i}  \\
&= \qty[\dot{q}^{i} + \pdv{\dot{q}^{j}}{p_{i}} p_{j} - \pdv{L}{\dot{q}^{j}}  \pdv{\dot{q}^{j}}{p_{i}}] \delta p_{i}
+ \qty[\pdv{\dot{q}^{j}}{q^{i}} p_{j}
- \pdv{L}{q^{i}} - \pdv{L}{\dot{q}^{j}} \pdv{\dot{q}^{j}}{q^{i}}] \delta q^{i}  \\
&= \dot{q}^{i} \delta p_{i} - \pdv{L}{q^{i}} \delta q^{i}
\marginnote{Used the fact that \(p_{i} = \pdv*{L}{\dot{q}^{i}}\).}  \\
&= \dot{q}^{i} \delta p_{i} - \dot{p}_{i} \delta q^{i}
\marginnote{Used the Euler-Lagrange equations.}
\,,
\end{align}
\end{subequations}
%
so we can equate the first and last equations to find the coupled equations
%
\boxalign{
\begin{align}
\dot{q}^{i} = \pdv{H}{p_{i}} \qquad \text{and} \qquad
\dot{p}_{i} = -\pdv{H}{q^{i}}
\,.
\end{align}}
%

These are known as \textbf{Hamilton's equations of motion}, they are an equivalent formulation of Lagrange's ones but they may be more useful in certain contexts such as when performing numerical integration.

In the Hamiltonian contexts it is useful to define the Poisson bracket.
If we define the operator \(\nabla\) as the derivative operator on the \(2N\)-dimensional \(q, p\) phase space, then the Poisson bracket is defined as
%
\begin{subequations}
\begin{align}
\qty{f, g} &= \qty(\nabla_{i} f) \mathbb{J}_{ij} \qty(\nabla_{j} g)  \\
&= \pdv{f}{q^{i}} \pdv{g}{p_{i}}
- \pdv{f}{p_{i}} \pdv{g}{q^{i}}
\,,
\end{align}
\end{subequations}
%
where we defined the \emph{symplectic unity} tensor
%
\begin{subequations}
\begin{align}
\mathbb{J} = \left[\begin{array}{cc}
0 & \mathbb{1}_{N} \\
-\mathbb{1}_{N} & 0
\end{array}\right]
\,.
\end{align}
\end{subequations}

With the aid of these, and defining the vector \(X = \qty[q, p]^{\top}\) we can write Hamilton's equations as
%
\begin{align}
\dv{X}{t} = \qty{X, H}
\,,
\end{align}
%
or, more explicitly,
%
\begin{align}
\dot{q} = \qty{q, H}
\qquad \text{and} \qquad
\dot{p} = \qty{p, H}
\,.
\end{align}

The Poisson brackets between the coordinates in phase space are given by
%
\begin{align}
\qty{q^{i}, q^{j}} = 0
 \qquad
\qty{p_{i}, p_{j}} = 0
\qquad
\qty{q^{i}, p_{j}} = \delta^{i}_{j}
\,.
\end{align}

A generic function's variation in time is given by
%
\begin{align}
\dv{f}{t} = \pdv{f}{t} + \qty{f, H}
\,.
\end{align}

There is a clear analogy between the classical Poisson bracket and the commutator in quantum mechanics.

\end{document}