% Define number set symbols.
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}

\chapter{Functions and Graphs}
\section{Composite Functions}
A function $f(x) = 3x+9$ will take in any value for $x$, and substitute it into the expression $3x+9$. If another expression was passed into $f(x)$, such as $x-2$, then the result would be $3(x-2)+9$. A composite function is one where two functions are combined, similar to above.

Suppose $f(x) = 2x$ and $g(x) = x+1$, then composite functions $f(g(x))$ and $g(f(x))$ can be found as follows:
\begin{align*}
	f(g(x)) &= 2(x+1) & g(f(x)) &= (2x)+1\\
	&=2x+2 & &=2x+1
\end{align*}

A composite function can be tiring to write out all the time, so it could be written as $h(x)=f(g(x))$.

\subsection{Example}
$f(x)=x^2+1$ and $g(x)=\frac{1}{x} \left(x\neq0\right)$. Find $h(x)=f(g(x))$ and $h(x)=g(f(x))$.
\begin{align*}
	h(x)&=f\left(\frac{1}{x}\right) & k(x)&=g\left(x^2+1\right)\\
	&=\left(\frac{1}{x}\right)^2-2 & &=\frac{1}{x^2+1}\\
	&=\frac{1}{x^2}-1
\end{align*}

\section{Domains and Set Notation}
Often, a function can't take all numbers as inputs. For example, a function of the area of a rectangle might be written as $f(x)=x^2+2x-8$. Since it's a function of real-life area, the output cannot be negative. After some thinking, it can be concluded that $x$ must be 2 or larger. Therefore the function's domain is any number larger or equal to 2.

Additionally, a decimal number could also be passed in to the function, which may be undesireable. You can define the domain to only include natural numbers (positive whole numbers).

Applying these two factors, the domain of the function is $\{x \in \N \mid x \geq 2\}$

Set (or rather set-builder) notation is a way of describing a set of numbers. For domains, it details what set of numbers can be an acceptable input of the function. The kinds of set number symbols that exist commonly are:
\begin{itemize}
	\item $\N$, natural numbers. Non-negative integers (starting with 0, counting up in steps of 1).
	\item $\Z$, integers. Any number that can be written without a fractional component (``whole" numbers).
	\item $\Q$, rational numbers. A number which can be written as a fraction with an integer numerator and a non-zero natural denominator\footnote{Negative denominators can exist, but are avoided, as they can be expressed as a negative numerator instead.}.
	\item $\R$, real numbers. Any number that can be represented on a number line, including irrational numbers such as $\pi$.
\end{itemize}

\subsection{Example}
Find a suitable domain for the function $\frac{1}{x^2-2}$.

\begin{equation*}
	\{x \in \R \mid x \neq \sqrt{2}\}
\end{equation*}

\section{Inverse Functions}
An inverse function ($f^{-1}(x)$) will do the opposite as what its normal counterpart ($f(x)$) does. For example,
\begin{align*}
	f(x) &= x^2\\
	f^{-1}(x)&=\sqrt{x}
\end{align*}

Functions are said to be inverse if $f\left(f^{-1}(x)\right) = f^{-1}\left(f(x)\right)=x$.

A trick for finding the inverse of a function is to do the following:
\begin{enumerate}
	\item replace $f(x)$ with $y$,
	\item change the subject of the formula for $x$,
	\item change $y$ to $x$, and $x$ to $f^{-1}(x)$.
\end{enumerate}

\subsection{Examples}
\begin{enumerate}
	\item
	\begin{align*}
		f(x)&=2x\\
		y&=2x\\
		x&=\frac{y}{2}\\
		f^{-1}(x)&=\frac{x}{2}
	\end{align*}
	\item
	\begin{align*}
		f(x)&=2x+1\\
		y&=2x+1\\
		2x&=y-1\\
		x&=\frac{1}{2}\left(y-1\right)\footnotemark\\
		f^{-1}(x)&=\frac{1}{2}\left(x-1\right)
	\end{align*}
	\footnotetext{Note that writing $x=\frac{y-1}{2}$ is correct too. That means that $f^{-1}(x)=\frac{x-1}{2}$ is a perfectly acceptable answer.}
\end{enumerate}

\section{Graph of $y=f(x)+a$ and \dots$-a$}
Given a graph $y=f(x)$, if asked to sketch $y=f(x)+a$, the graph would be moved $a$ places up; if asked to sketch $y=f(x)-a$, the graph would be moved $a$ places down.

In other words, the graph is moved $a$ places along the $y$-axis. A value of $-3$ would move the graph $-3$ places up (i.e. $3$ places down). Practically, add $a$ to all $y$-coordinates.

\subsection{Example}
In figure \ref{fig:verticalShiftExample}, the original graph $y=f(x)$ is drawn, along with $y=f(x)+3$.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
				\addplot [] {x^3-3*x-1};
				\coordinate
				[
					label=above left:{$(-1,1)$},
					black,
					cmark=*,
				] (a) at (-1,1);
				\coordinate
				[
					label=below right:{$(1,-3)$},
					black,
					cmark=*,
				] (a) at (1,-3);
			\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
				\addplot [mark=none] {x^3-3*x-1};
				\addplot [color=blue,mark=none] {x^3-3*x+2};
				\coordinate
				[
					label=left:{$(-1,4)$},
					black,
					cmark=*,
				] (a) at (-1,4);
				\coordinate
				[
					label=above:{$(1,0)$},
					black,
					cmark=*,
				] (a) at (1,0);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=f(x)+3$ in blue, along with the original graph.}
	\end{subfigure}
	\caption{An example of a typical question about $y=f(x)+a$ with the solution.}
	\label{fig:verticalShiftExample}
\end{figure}

To find out the transformed coordinates, add $a$ to the $y$-coordinates:
\begin{align*}
f(x)&=(-1,1) & f(x)&=(1,-3)\\
f(x)+3&=(-1,4) & f(x)+3&=(1,0)
\end{align*}

\section{Graph of $y=f(x+a)$ and \dots$-a)$}
When the $a$ is inside the bracket, it moves the graph from side to side. A graph of $y=f(x+a)$ will move the graph $a$ places \textit{left}, and $y=f(x-a)$ would move $a$ places \textit{right}.

In other words, the graph is moved $-a$ places along the $x$-axis. A value of $-3$ would move the graph 3 places to the right. Practically, add $-a$ to all $x$-coordinates. The thing to throw you off is that it is $-a$, and not $a$ that is being moved.

\subsection{Example}
The figure \ref{fig:horizontalShiftExample} is the graph $y=f(x)$ and $y=f(x+5)$.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
		\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [] {(x-3)^3-3*x+8};
			\coordinate
			[
				label=above:{$(2,1)$},
				black,
				cmark=*,
			] (a) at (2,1);
			\coordinate
			[
				label=below:{$(4,-3)$},
				black,
				cmark=*,
			] (a) at (4,-3);
		\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [mark=none] {(x-3)^3-3*x+8};
			\addplot [color=blue,mark=none] {(x+2)^3-3*(x+5)+8};
			\coordinate
			[
				label=above:{$(-3,1)$},
				black,
				cmark=*,
			] (a) at (-3,1);
			\coordinate
			[
				label=below left:{$(-1,-3)$},
				black,
				cmark=*,
			] (a) at (-1,-3);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=f(x+5)$ in blue, along with the original graph.}
	\end{subfigure}
	\caption{An example of a typical question about $y=f(x+a)$ with the solution.}
	\label{fig:horizontalShiftExample}
\end{figure}

To find out the transformed coordinates, add $-a$ to the $x$-coordinates:
\begin{align*}
f(x)&=(2,1) & f(x)&=(4,-3)\\
f(x+5)&=(-3,1) & f(x+5)&=(-1,-3)
\end{align*}

\section{Graph of $y=-f(x)$}
$y=-f(x)$ reflects the graph on the $x$-axis; all $y$-coordinates will change their sign.

An easy way to remember this is to think of the equation $y=x^2$ that was learnt in National 5. If the equation would be $y=-x^2$, then the parabola is ``flipped". This is the same thing that can be done to any function $y=f(x)$, $y=-f(x)$ will be ``flipped".

\subsection{Example}
An example of reflection on the $x$-axis with coordinates $(-1,3)$ and $(1,-1)$ is shown in figure \ref{fig:xAxisReflectionExample}.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-4,
				xmax=4,
				xtick={-4,-2,...,4},
				ymin=-4,
				ymax=4,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [] {x^3-3*x+1};
			\coordinate
			[
				label=above left:{$(-1,3)$},
				black,
				cmark=*,
			] (a) at (-1,3);
			\coordinate
			[
				label=below right:{$(1,-1)$},
				black,
				cmark=*,
			] (a) at (1,-1);
			\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-4,
				xmax=4,
				xtick={-4,-2,...,4},
				ymin=-4,
				ymax=4,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [mark=none] {x^3-3*x+1};
			\addplot [color=blue,mark=none] {-x^3+3*x-1};
			\coordinate
			[
				label=left:{$(-1,-3)$},
				black,
				cmark=*,
			] (a) at (-1,-3);
			\coordinate
			[
				label=above:{$(1,1)$},
				black,
				cmark=*,
			] (a) at (1,1);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=-f(x)$ in blue, along with the original graph.}
	\end{subfigure}
	\caption{An example of a typical question about $y=-f(x)$ with the solution.}
	\label{fig:xAxisReflectionExample}
\end{figure}

To find out the transformed coordinates, change the $y$-coordinates' sign:
\begin{align*}
	f(x)&=(-1,3) & f(x)&=(1,-1)\\
	-f(x)&=(-1,-3) & -f(x)&=(-1,-1)
\end{align*}

\section{Graph of $y=f(-x)$}
$y=f(-x)$ is reflection on the y-axis; all $x$-coordinates will change their signs.

\subsection{Example}
Here in figure \ref{fig:yAxisReflectionExample} an example of reflection on the $y$-axis is shown with the coordinates $(1,2)$ and $(3,-2)$.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-4,
				xmax=4,
				xtick={-4,-2,...,4},
				ymin=-4,
				ymax=4,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [] {(x-2)^3-3*x+6};
			\coordinate
			[
				label=above:{$(1,2)$},
				black,
				cmark=*,
			] (a) at (1,2);
			\coordinate
			[
				label=below:{$(3,-2)$},
				black,
				cmark=*,
			] (a) at (3,-2);
			\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-4,
				xmax=4,
				xtick={-4,-2,...,4},
				ymin=-4,
				ymax=4,
				ytick={-4,-2,...,3},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8
			]
			\addplot [mark=none] {(x-2)^3-3*x+6};
			\addplot [color=blue,mark=none] {(-x-2)^3+3*x+6};
			\coordinate
			[
				label=above left:{$(-1,2)$},
				black,
				cmark=*,
			] (a) at (-1,2);
			\coordinate
			[
				label=below:{$(-3,-2)$},
				black,
				cmark=*,
			] (a) at (-3,-2);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=f(-x)$ in blue, along with the original graph.}
	\end{subfigure}
	\caption{An example of a typical question about $y=f(-x)$ with the solution.}
	\label{fig:yAxisReflectionExample}
\end{figure}

To find out the transformed coordinates, change the $x$-coordinates' sign:
\begin{align*}
	f(x)&=(1,2) & f(x)&=(3,-2)\\
	f(-x)&=(-1,2) & f(-x)&=(-3,-2)
\end{align*}

\section{Graph of $y=kf(x)$}
A graph can be stretched or compressed on the $y$-axis by multiplying the graph by a constant, written as $y=kf(x)$. $k>1$ will stretch it, while $k<1$ will compress it.

To find out the new coordinates, multiply the $y$-coordinate by $k$.

\subsection{Example}
Figure \ref{fig:verticalCompressionExample} is the graph of $y=f(x)$, as well as $y=2f(x)$ and $y=\frac{1}{2}f(x)$.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,4},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8,
			]
			\addplot [] {x^3-3*x};
			\coordinate
			[
				label=above left:{$(-1,2)$},
				black,
				cmark=*,
			] (a) at (-1,2);
			\coordinate
			[
				label=below right:{$(1,-2)$},
				black,
				cmark=*,
			] (a) at (1,-2);
			\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,5},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,4},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8,
			]
			\addplot [color=blue,mark=none] {2*(x^3)-6*x};
			\addplot [color=red,mark=none] {0.5*(x^3)-1.5*x};
			\coordinate
			[
				label=left:{$(-1,4)$},
				black,
				cmark=*,
			] (a) at (-1,4);
			\coordinate
				[
				label=right:{$(1,-4)$},
				black,
				cmark=*,
			] (a) at (1,-4);
			\coordinate
			[
				label=above left:{$(-1,1)$},
				black,
				cmark=*,
			] (a) at (-1,1);
			\coordinate
			[
				label=below right:{$(1,-1)$},
				black,
				cmark=*,
			] (a) at (1,-1);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=2f(x)$ in blue and $y=\frac{1}{2}f(x)$, original graph omitted for clarity.}
	\end{subfigure}
	\caption{An example of a typical question about $y=kf(x)$ with the solution.}
	\label{fig:verticalCompressionExample}
\end{figure}

To find out the transformed coordinates, multiply the $y$-coordinates by $k$. For $k=2$:
\begin{align*}
f(x)&=(-1,2) & f(x)&=(1,-2)\\
f(2x)&=(-1,4) & f(2x)&=(1,-4)
\end{align*}
And for $k=\frac{1}{2}$:
\begin{align*}
f(x)&=(-1,2) & f(x)&=(1,-2)\\
f\left(\frac{1}{2}x\right)&=(-1,1) & f\left(\frac{1}{2}x\right)&=(1,-1)
\end{align*}

\section{Graph of $y=f(kx)$}
The last transformation is stretching and compressing on the $x$-axis. Again, the opposite is true here, $k>1$ will \textit{compress} the graph, $k<1$ will stretch the graph.

To get the new coordinates, divide all $x$-coordinates by $k$.

\subsection{Examples}
Figure \ref{fig:horizontalCompressionExample} shows $y=f(x)$, and the graphs of $y=f(2x)$ and $y=f(\frac{1}{2})$.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,4},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,4},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8,
			]
			\addplot [] {x^3-3*x};
			\coordinate
			[
				label=above left:{$(-1,2)$},
				black,
				cmark=*,
			] (a) at (-1,2);
			\coordinate
			[
				label=below right:{$(1,-2)$},
				black,
				cmark=*,
			] (a) at (1,-2);
			\end{axis}
		\end{tikzpicture}
		\caption{Some function $y=f(x)$ with a couple of given coordinates.}
	\end{subfigure}
	\begin{subfigure}{0.475\linewidth}
		\begin{tikzpicture}[cmark/.style={label={[anchor=center]:\pgfuseplotmark{#1}}}]
			\begin{axis}
			[
				restrict y to domain=-10:10,
				restrict x to domain=-5:5,
				xlabel=$x$,
				ylabel=$y$,
				xmin=-5,
				xmax=5,
				xtick={-4,-2,...,5},
				ymin=-5,
				ymax=5,
				ytick={-4,-2,...,4},
				axis lines=center,
				axis equal,
				smooth,
				scale=0.8,
			]
			\addplot [color=blue,mark=none] {(x/2)^3-1.5*x};
			\addplot [color=red,mark=none] {(2*x)^3-6*x};
			\coordinate
			[
				label=left:{$(-2,2)$},
				black,
				cmark=*,
				] (a) at (-2,2);
			\coordinate
			[
				label=right:{$(2,-2)$},
				black,
				cmark=*,
			] (a) at (2,-2);
			\end{axis}
		\end{tikzpicture}
		\caption{$y=f(\frac{1}{2}x)$ in blue and $y=f(2x)$ in red, original graph and coordinates of $y=f(\frac{1}{2}x)$ omitted for clarity.}
	\end{subfigure}
	\caption{An example of a typical question about $y=f(kx)$ with the solution.}
	\label{fig:horizontalCompressionExample}
\end{figure}

To find out the transformed coordinates, divide the $x$-coordinates by $k$. For $k=\frac{1}{2}$:
\begin{align*}
f(x)&=(-1,2) & f(x)&=(1,-2)\\
f(2x)&=(-2,2) & f(2x)&=(2,-2)
\end{align*}
And for $k=2$:
\begin{align*}
f(x)&=(-1,2) & f(x)&=(1,-2)\\
f\left(\frac{1}{2}x\right)&=\left(-\frac{1}{2},2\right) & f\left(\frac{1}{2}x\right)&=\left(\frac{1}{2},-1\right)
\end{align*}


\section{Methods}
\label{mmf-sec:methods}

With the change from a variant focused approach to a read based method, this new method will call ``mismatches`` of a read from the reference genome, rather than a variant. This has the advantage of not requiring a matched normal and its use for virtually any sequencing data source, be it TAS, WES, WGS or even nanopore sequencing\footnote{however nanopore is not really usefull due to the short fragments naturally occurring in cfDNA}. However it also means, that the error suppression method, which are usually used by variant calling methods like read position ranks sum (RPRS) or strand bias are not usable, which leads to a higher degree of background noise. In the following sections I will describe how we filter and curate the found mismatches to retain as much signal as possible.

\subsection{Mathematical concept}
\label{mmf-sec:concept}
With the change from site based method, the concept of a mismatch from the reference needs to be introduced. A mismatch in the following is any position in an aligned read, which does not show the same base as the reference at the aligned position. The mismatch will inherit all the metrics of the read such as mapping quality, base quality and read position.

This then means, there are three sources of mismatches in a read, which are somatic variants, germline variants and sequencing errors (\autoref{mmf-eq:1}).
\begin{equation}
n(mismatches) = n(somatic~var.) + n(germline~var.)  + n(seq.~ error)
\label{mmf-eq:1}
\end{equation}
\myequation[\ref{mmf-eq:1}]{MisMatchFinder: number of mismatches}

With the sequencing error being a function of the sequencing machine and chemistry, the error rate should be a stable almost constant, when using the same sequencing machine and chemistry \cite{Schirmer2016,Stoler2021}. We can therefore reduce \autoref{mmf-eq:1} to
\begin{equation}
n(mismatches) = n(som.~var.) + n(germ.~var.)  + c_{seq.~err.}
\label{mmf-eq:2}
\end{equation}
\myequation[\ref{mmf-eq:2}]{MisMatchFinder: sequencing error}

Secondly, the number of germline variants is approximately the same between two people \cite{Auton2015}, which again simplifies \autoref{mmf-eq:2} by replacing $n(germline~var.)$.

\begin{equation}
n(mismatches) = n(som.~var.) + c_{germ.~var.} + c_{seq. err.}
\label{mmf-eq:3}
\end{equation}
\myequation[\ref{mmf-eq:3}]{MisMatchFinder: germline variants}

Of course, \autoref{mmf-eq:3} is a crude approximation and instead the constants are not real constants, but instead are better approximated with Gaussian distributions which leads to the following equation

\begin{equation}
n(mismatches) = n(som.~var.) + \mathcal{N}(\mu_{germ.~var.}, \sigma_{germ.~var.}^{2}) + \mathcal{N}(\mu_{seq.~err.}, \sigma_{seq.~err.}^{2})
\label{mmf-eq:4}
\end{equation}
\myequation[\ref{mmf-eq:4}]{MisMatchFinder: number of mismatches with distributions}

However, both \autoref{mmf-eq:3} and \ref{mmf-eq:4} allow to make the conclusion, that with small enough values for either $c_{germ.~var}/c_{seq.~err.}$ or $\mu_{germ.~var}/\mu_{seq.~err.}$ and $\sigma_{germ.~var}/\sigma_{seq.~err.}$ respectively, there is a linear correlation between the amount of mismatches on a read and the somatic variants it contains:

\begin{equation}
n(mismatches) \sim n(som.~var.)
\label{mmf-eq:final}
\end{equation}
\myequation[\ref{mmf-eq:final}]{MisMatchFinder: number of mismatches correlation with somatic variants}

With the help of \autoref{mmf-eq:final} we can approximate tumour mutational burden and signatures from individual reads. This method is therefore independent from read depth and requires no matched normal sample for somatic variant calling.

\subsection{Data preprocessing}
As this new method has sophisticated internal measures to filter and process sequencing data, the steps for preprocessing are minimal: The reads only need to be aligned to a reference genome (\autoref{intro-sec:mapping}). For optimal mapping and additional noise reduction, paired end sequencing of at least 75 bp is suggested. This ensures a few bases overlap on the standard fragment length of less than 150bp of ctDNA (\autoref{intro-sec:ctDNA}). Another optional suggested step is the duplication marking of the BAM file.

\subsection{Mismatch detection}
In contrast to conventional variant calling approaches, which find regions of interest through pileups (position wise) and then realign reads in the surrounding area, to accurately estimate the most likely event that lead to the observed haplotype (\autoref{intro-sec:variantcalling}), with this new method, we take every individual read as a separate entity to fully span the heterogeneity of all cells and their genetic background. A sequencing reads ``MD``- and ``CIGAR``- tag from the preprocessed BAM file are used to reconstruct the sequence of the read and the positions, where the read shows a different base than the reference. These potential mismatch sites will then filtered in multiple steps to reduce the impact of both germline variants as well as sequencing errors

\subsection{Filtering steps}
Apart from the filters, which most variant callers will employ, like mapping quality (MQ) and base quality (BQ), which are used to ignore reads as well as positions respectively, the method also internally filters out common sequencing errors next to homopolymer regions \cite{Heydari2019}. While these cutoffs were preselected by me for optimal performance on our data (MQ=20, BQ=55, homopolyLength=5), the program allows the user to adjust them to their liking.
This is also possible for both the region of interest (ROI) bed-file which was used to restrict the analysis to only highly mappable regions of the genome (\autoref{ch-mmfAppendix:bedfiles}), as well as for multiple other parameters which are unique to our method, like minimum average base quality, minimum and maximum number of mismatches per read and/or fragment, and the minimum and maximum length of a fragment \cite{Hudecova2021}. If any of these values are not within the specified range a read will be discarded in the analysis. This is also the default for reads which have a secondary alignment position or are considered duplicates of any kind.

\subsection[Consensus reads]{Consensus reads - what happens when the sequencer isn't sure}
\label{mmf-sec:consensus}

When paired end sequencing of ctDNA is analysed, the fraction of fragments where reads overlap is higher, than with ``normal`` tissue based sequencing, due to the shorter fragment length of ctDNA (\autoref{intro-sec:ctDNA}). This allows an fragment internal consensus generation, by adjusting for differences between forward and reverse read. In many variant calling methods, these differences are used by measuring the ``strand bias`` \cite{Guo2012, Saunders2012, GATKTeam2019} or ``strand balance probability`` \cite{Garrison2012} by looking at a specific locus and evaluating the discrepancy of all forward and all reverse reads. As our method evaluates each read/fragment independently, the bias cannot be calculated, however in the overlapping region of both reads, a consensus can be generated. If both reads agree on the mismatch, the BQ of both reads will be added together to emphasise the increased evidence for this variants. However, if they disagree the base of the higher quality will be used and its quality will be decreased by half of the BQ of the lower quality base (\autoref{fig:mmf-consensus}~bottom). To increase the stringency of the method, the user can also enable the \lq\emph{--strictOverlap}\rq~ option, which will only consider a mismatch, if both reads agree with each other and decrease the BQ to zero otherwise. As we are only interested in mismatches from the reference, all positions where both agree with the reference are irrelevant for the analysis and will be discarded (\autoref{fig:mmf-consensus}~top). For the most stringent analysis, MisMatchFinder can additionally be configured to only use mismatches in the overlap part of a fragment (\lq\emph{--onlyOverlap}\rq), which significantly reduces the number of sequencing errors which end up in the final analysis (\autoref{mmf-sec:cleanSim}).

\begin{figure}[!ht]
\centering
\includegraphics[width=.99\linewidth]{Figures/ConsensusMethodMisMatchFinder.pdf}
\caption[Schematic of consensus computation method for overlapping reads]{Schematic of consensus computation method for overlapping reads in MisMatchFinder; Read 1 and Read 2 depict two overlapping paired end reads aligned to the reference sequence; Positions in the overlap are numbered for later referral; Read positions agreeing with the reference are coloured black, positions differing from the reference but agreeing in both reads are coloured purple (position 3) and differences between reads are coloured in the respective read colours (blue and red, position 6); Calculation for the resulting base quality (BQ$_{cons}$ for each possibility is shown as formulas)}\label{fig:mmf-consensus}
\end{figure}

\subsection[Germline filtering]{Germline filtering - exclusion of normal variation}
\label{mmf-sec:germline}

To further enable the \autoref{mmf-sec:concept} claim that the germline is a very small constant, we need to remove as many mismatches as possible, which stem from germline variants. For this purpose, I built a zarr \cite{Miles2021} based storage system from the gnomAD database (v.3.1) \cite{Karczewski2020} using scikit-alel \cite{Miles2021a}.
A in-depth explanation of the generation as well as a script for for an end user can be found in \autoref{ch-mmfAppendix:germlineFilter}.

This then allows very precise filtering of known germline variants sites from the analysis. The method allows the specification of an allele frequency to consider a variant to be filtered, however as baseline, it will filter all sites, which were detected in any sample in gnomAD. This even includes sites with low quality variants, as these are signs for sequencing or mapping complications, which will most likely interfere with our method as well.

\subsection[Count normalisation]{Count normalisation - not everyone has the same chances}
\label{mmf-sec:countNorm}
Finally when having filtered all ``noise`` mismatches from the dataset, we can aggregate all mismatches to oligo-nucleotide counts. With this step also comes the classification of
directly neighbouring mismatches as DBS, which are counted as separate entities. SBS and DBS both can be used to identify underlying biological mutational processes, but they have very different signatures associated with them \cite{Alexandrov2020}. The counts formed this way are influenced by the background frequency of their reference oligo-nucleotides in the analysed genomic region. As the frequencies of di- and tri-nucleotides are not uniform in the genome, the chance for a mismatch found in an ``AAA`` reference contex is almost seven times higher than a mismatch in ``CGC`` (\autoref{A:mmf:tab:tricounts}, \autoref{A:mmf:tab:dicounts}). To reduce this bias towards high frequency oligo-nucleotides, I implemented a count normalisation step.

First the di- and tri-nucleotides in the analysed regions are counted using the supplied reference without any black-listed and/or only in white-listed regions. These counts are then either used to directly weight the observed mismatch counts, which leads to a more uniform distribution of mismatches, or by building a fraction of observed oligo-nucleotides and the total counts in the genome (\autoref{A:mmf:tab:tricounts}, \autoref{A:mmf:tab:dicounts}), the weighting achieves an approximation of how the counts would be distributed over the whole genome. These two options are available with \lq\emph{--normaliseCounts}\rq~ for the approximation to full genome. By also adding \lq\emph{--flatNormalisation}\rq~ only the observed counts are used for normalisation.

\subsection[Signature deconvolution]{Signature deconvolution - find the original signal}
\label{mmf-sec:sigDeconv}
The deconvolution of the involved signatures from known set of signatures is equivalent to finding the minimal distance between $m$ as the observed number of mismatches in each oligo-nucleotide context (a vector of length 96) and $\textbf{S}w$, where $\textbf{S}$ is the matrix of oligo-nucleotide defined contributions for each signature, resulting in a matrix of $96 \times k$ with $k$ being the number of known signatures. Lastly, $w$ is the weights of each signature, which we want to estimate.

\begin{align}
 \text{minimise:} & \quad (m - \textbf{S}w)^T(m - \textbf{S}w)
 = m^Tm - w^T\textbf{S}^Tm - m^T\textbf{S}w + w^T\textbf{S}^T\textbf{S}w \label{mmf-eq:optim1}\\
 \text{with:} & \quad \sum_j w_j = 1 \quad \text{and} \quad \forall_j w_j \geq 0 \label{mmf-eq:requirements}
\end{align}
\myequation[\ref{mmf-eq:optim1}]{MisMatchFinder: optimisation for signature weights}
\myequation[\ref{mmf-eq:requirements}]{MisMatchFinder: optimisation function restrictions}

\autoref{mmf-eq:optim1} can then be written as

\begin{equation}
\text{minimise:} \quad - m^T\textbf{S}w + \frac{1}{2}w^T\textbf{S}^T\textbf{S}w
\label{mmf-eq:optim2}
\end{equation}
\myequation[\ref{mmf-eq:optim2}]{MisMatchFinder: quadratic programming formula}

With the same restrictions as shown in \autoref{mmf-eq:requirements}. These equations and the idea to solve it with quadratic programming (QP) have been taken from \textcite{Lynch2016}, the iterative linear models (ILM) solving approach was adapted from deconstructSigs \cite{Rosenthal2016}. Both methods are implemented in python, using the quadprog package \cite{McGibbon2021} for QP and a translation of the R code of deconstrutSigs.

MisMatchFinder allows the use of either QP or ILM, as they in many cases produce very similar results \cite{Lynch2016}. The default method is QP however, even though ILM is the more interpretable method and is the more parsimonious method, because with the increased number of signatures, in the latest work by \textcite{Alexandrov2020}, ILM does not lead to the right signatures if the signal is not strong enough but QP seems to be more stable (\autoref{fig:mmf-ILMerror}).

\begin{figure}[!ht]
\centering
\includegraphics[width=.99\linewidth]{Figures/lowInputSignalDeconv.pdf}
\caption[Distance of deconvolution methods from truth]{Distances of the estimated weights generated with ILM and QP from the true weight used as input; Truth is a synthetic count sample with (SBS1: 0.25; SBS3: 0.05; SBS5: 0.46; SBS7a: 0.1; SBS19: 0.03; SBS21: 0.01; SBS31: 0.08; SBS57: 0.02;)}\label{fig:mmf-ILMerror}
\end{figure}

The combinatorial problem in ILM, already shown by \textcite{Lynch2016}, seems to be especially strong with ``wide`` signatures like SBS3 (\autoref{fig:sig7a}) and low signature contribution. That makes it less useful for our approach, as we expect low tumour purity and therefore low somatic signature signals in cfDNA, but even with ILM as deconvolution method impacts the detection of SBS7a less than the detection of SBS3 (\autoref{fig:mmf-qpVSilm})\todo{make that image}.

The deconvolution method might be a spot for further optimisation by creating a custom deconvolution system adjusted for ctDNA detection.

\todo[color=green,inline]{maybe move the simulation description here}


% !TeX spellcheck = en_US
\chapter{Backtracking}
\section{Problem's specification}

Pattern:

For a given problem we search all the sequences  $\bm{x_{1}x_{2} ... x_{n}}$ for which some property holds
 $\bm{P_n(x_{1},x_{2}, ..., x_{n})}$

\bigskip where: $\bm{x_k \in D_k}$ (some given domain of integers )
\\
The backtrack method consists in designing "cutoff"/"bounding" properties $\bm{P_l(x_{1},x_{2}, ..., x_{l})}$ for $\bm{1\leq l < n}$ such that:

\begin{itemize}
 \item $\bm{P_l(x_{1},x_{2}, ..., x_{l})}$ is true whenever $\bm{P_{l+1}(x_{1},x_{2}, ..., x_{l+1})}$ is true;
 \item $\bm{P_l(x_{1},x_{2}, ..., x_{l})}$ is simple to test, if $\bm{P_{l-1}(x_{1},x_{2}, ..., x_{l-1})}$ holds.

\end{itemize}

\section{References}

Bactracking from \cite{KnuthArtOfCompProg4-5b}

Doc/Notes on Data Structures and Algorithms - With Implementation/TeX_files/chapter_backtracking.tex


\subsection{Intro}

\begin{frame}
  \frametitle{Eager and Lazy}

  \scriptsize

  SMT can be reduced to SAT, but requires discovering and
  adding incompatibilities between \tatoms
  \vfill
  Eager and Lazy refers to the time in which these incompatibilities
  are added to the Boolean structure of the problem
  \begin{itemize}
    \item eager: immediately, before \satsolver is called as black-box
    \item {\bf lazy}: on demand, during \satsolver's search
  \end{itemize}
  \vfill
    \begin{tabular}{ccc}
      Eager & & Lazy \\
      \begin{minipage}{.4\textwidth}
          \scalebox{.3}{\input{eager.pdf_t}}
      \end{minipage}
      & ~~~~~ &
      \begin{minipage}{.4\textwidth}
          \scalebox{.3}{\input{lazy.pdf_t}}
      \end{minipage}
    \end{tabular}

\end{frame}

\begin{frame}
  \frametitle{Lazy Approach}

  \scriptsize

  The Lazy Approach builds on top of SAT and of available and well known
  {\bf decision procedures}, which we call {\bf theory solvers} (\tsolvers)
  \vfill
  Examples of these \tsolvers are the Union-Find procedure for equality,
  and the Simplex Algorithm for Linear Rational Arithmetic
  \vfill
  These procedures are very efficient in handling {\bf conjunctions} of \tatoms,
  but they don't know how to handle arbitrary Boolean operators
  \vfill
  \pause
  Lazy SMT can be seen as an efficient mechanism to extend these procedures
  to handle generic Boolean combinations of \tatoms
  \vfill
  This is achieved with a tight integration between a SAT-solver and the
  \tsolver
  \vfill
  In the following we assume that
  \begin{enumerate}[$(i)$]
    \item \T is decidable, and that
    \item a \tsolver for conjunctions of \tatoms exists
  \end{enumerate}

\end{frame}

\begin{frame}
  \frametitle{A bit of notation}

  \scriptsize

  We will use the following notation
  \vfill
  \begin{center}
  \ra{1.8}
  \begin{tabular}{cl}
  \hline
  Symbol            & Meaning \\
  \hline
  $\varphi$         & original formula, in some background theory \T \\
  $\babst{\varphi}$ & the Boolean abstraction of $\varphi$ \\
  $\mu$             & an assignment for $\varphi$ \\
  $\babst{\mu}$     & the assignment for $\babst{\varphi}$ induced by $\mu$ \\
  \hline
  \end{tabular}
  \end{center}
  \vfill
  \pause
  E.g., where \T is \Lia (Linear Integer Arithmetic)
  $$
  \ra{1.8}
  \begin{array}{rccccccc}
    \varphi \equiv         & (x + y \leq 0) & \wedge & (x = 0) & \wedge & (\neg (y = 1) & \vee & (x = 1) ) \\
    \babst{\varphi} \equiv & a_1            & \wedge & a_2     & \wedge & (\neg a_3 & \vee & a_4     ) \\
    \mu     \equiv         & \multicolumn{7}{l}{\{ x \mapsto 0, y \mapsto 0 \}}       \\
    \babst{\mu} \equiv     & \multicolumn{7}{l}{\{ a_1 \mapsto \top, a_2 \mapsto \top, a_3 \mapsto \bot, a_4 \mapsto \bot \}}
  \end{array}
  $$

\end{frame}

\begin{frame}
  \frametitle{A bit of notation}

  \scriptsize
  $$
  \ra{1.8}
  \begin{array}{rccccccc}
    \varphi \equiv         & (x + y \leq 0) & \wedge & (x = 0) & \wedge & (\neg (y=1) & \vee & (x = 1) ) \\
    \babst{\varphi} \equiv & a_1            & \wedge & a_2     & \wedge & (\neg a_3 & \vee & a_4     ) \\
    \mu     \equiv         & \multicolumn{7}{l}{\{ x \mapsto 0, y \mapsto 0 \}}       \\
    \babst{\mu} \equiv     & \multicolumn{7}{l}{\{ a_1 \mapsto \top, a_2 \mapsto \top, a_3 \mapsto \bot, a_4 \mapsto \bot \}}
  \end{array}
  $$
  \vfill
  Notice that
  $$\babst{\mu} \equiv \{ a_1 \mapsto \top, a_2 \mapsto \top, a_3 \mapsto \bot, a_4 \mapsto \bot \} \equiv \{ a_1, a_2, \neg a_3, \neg a_4 \}$$
  is nothing but
  $$\{\ (x+y \leq 0),\ (x=0),\ \neg (y=1),\ \neg (x=1)\ \}$$
  i.e., it is a {\bf conjunction} of constraints, whose satisfiability can be checked with a \tsolver
  \vfill\pause
  In other words, the \tsolver can tell if $\babst{\mu}$ is \T-satisfiable
  \vfill
  If so, then there is a model $\mu$, that induces $\babst{\mu}$, and if
  $\babst{\mu}$ is a model for $\babst{\varphi}$ then $\mu$ is also a
  model for $\varphi$ (take some time to think about it at home)

\end{frame}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{ragged2e}
\usepackage{changepage}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin=1in]{geometry}
\title{Triangulation Algorithm}
\author{Chloe Yugawa}
\date{February 2018}

\begin{document}

\maketitle
\paragraph{}
This algorithm is designed to calculate the location of a signal received by four hydrophones, all of which are equally spaced from each other in a square that is parallel to the surface of the water. To calculate the location of the signal origin, we will use the time stamps from each hydrophone (the time at which the signal was received), the speed of sound in the given environment, and some basic algebra and trigonometry. First, we write equations for the time it takes the signal to reach each hydrophone (labelled a,b,c, and d) using the formula for the distance between two points times the speed of sound in water ($s$). The origin is defined as the center of the hydrophone square, and $l$ is defined as both the $x$ and $y$ distances from the origin to each hydrophone.\\
% One of your diagrams might be nice here to illustrate L, x, and y. Also, I thought that you said L was the distance between adjacent hydrophones, not their distance from the origin. Am I miss-remembering? -Noah 4/28
% Perhaps add a brief note that 's' is a function of the conditions in the water? -Noah 4/28
$s$ = speed of sound through water \\
$l$ = $x$ and $y$ distance from origin to each hydrophone\\
$a$ = hydrophone at location $(l,l)$\\
$b$ = hydrophone at location $(-l,l)$\\
$c$ = hydrophone at location $(-l,-l)$\\
$d$ = hydrophone at location $(l,-l)$\\
$t_a$ = time stamp of signal for hydrophone a\\
$t_b$ = time stamp of signal for hydrophone b\\
$t_c$ = time stamp of signal for hydrophone c\\
$t_d$ = time stamp of signal for hydrophone d\\
\begin{gather*}
t_a = \frac{1}{s}*\sqrt{(x-l)^2+(y-l)^2+z^2}\\
t_b = \frac{1}{s}*\sqrt{(x+l)^2+(y-l)^2+z^2}\\
t_c = \frac{1}{s}*\sqrt{(x+l)^2+(y+l)^2+z^2}\\
t_d = \frac{1}{s}*\sqrt{(x-l)^2+(y+l)^2+z^2}
\end{gather*}
Now, we define the deltas, or time differences. For this algorithm, hydrophone $a$ has been chosen to be the reference. Calculating in terms of a different hydrophone would work equivalently.
\begin{gather*}
    delta_1 = t_b-t_a = \frac{1}{s}*\sqrt{(x+l)^2+(y-l)^2+z^2} - s*\sqrt{(x-l)^2+(y-l)^2+z^2}\\
    delta_2 = t_c-t_a = \frac{1}{s}*\sqrt{(x+l)^2+(y+l)^2+z^2} - s*\sqrt{(x-l)^2+(y-l)^2+z^2}\\
    delta_3 = t_d-t_a = \frac{1}{s}*\sqrt{(x-l)^2+(y+l)^2+z^2} - s*\sqrt{(x-l)^2+(y-l)^2+z^2}
\end{gather*}
These equations rewritten for clarity and for future steps:
\begin{gather*}
    delta_1 = \frac{1}{s}(\sqrt{(x+l)^2+(y-l)^2+z^2} - \sqrt{(x-l)^2+(y-l)^2+z^2})\\
    delta_2 = \frac{1}{s}(\sqrt{(x+l)^2+(y+l)^2+z^2} - \sqrt{(x-l)^2+(y-l)^2+z^2})\\
    delta_3 = \frac{1}{s}(\sqrt{(x-l)^2+(y+l)^2+z^2} - \sqrt{(x-l)^2+(y-l)^2+z^2})
\end{gather*}
With $2$s factored out and the distance to hydrophone $a$ moved to the other side of the equations, we get:
\begin{gather*}
    delta_1*s +\sqrt{(x-l)^2+(y-l)^2+z^2} = \sqrt{(x+l)^2+(y-l)^2+z^2}\\
    delta_2*s + \sqrt{(x-l)^2+(y-l)^2+z^2} = \sqrt{(x+l)^2+(y+l)^2+z^2}\\
    delta_3*s + \sqrt{(x-l)^2+(y-l)^2+z^2} = \sqrt{(x-l)^2+(y+l)^2+z^2}
\end{gather*}
Squaring both sides and simplifying yields
\begin{gather*}
    (delta_1*s)^2 +(x-l)^2+(y-l)^2+z^2 + delta_1*s*\sqrt{(x-l)^2+(y-l)^2+z^2}= (x+l)^2+(y-l)^2+z^2\\
    (delta_2*s)^2 + (x-l)^2+(y-l)^2+z^2 + delta_2*s*\sqrt{(x-l)^2+(y-l)^2+z^2} = (x+l)^2+(y+l)^2+z^2\\
    (delta_3*s)^2 + (x-l)^2+(y-l)^2+z^2 + delta_3*s*\sqrt{(x-l)^2+(y-l)^2+z^2} = (x-l)^2+(y+l)^2+z^2
\end{gather*}

Note that the only known values are the $s$ and time stamps and thus the value of the deltas. Further simplification:
\begin{equation}
\label{equ:d1}
    (delta_1*s)^2  + delta_1*s*\sqrt{(x-l)^2+(y-l)^2+z^2}= 4lx
\end{equation}
\begin{equation}
\label{equ:d2}
     (delta_2*s)^2 + delta_2*s*\sqrt{(x-l)^2+(y-l)^2+z^2} = 4lx+4ly
\end{equation}
\begin{equation}
\label{equ:d3}
     (delta_3*s)^2 + delta_3*s*\sqrt{(x-l)^2+(y-l)^2+z^2} = 4ly
\end{equation}

Here, we see \eqref{equ:d2} is a linear combination of \eqref{equ:d1} and \eqref{equ:d3}, and that the term $\sqrt{(x-l)^2+(y-l)^2+z^2}$ shows up in all three equations. To solve this equation, set $\sqrt{(x-l)^2+(y-l)^2+z^2} = N$.
\begin{equation}
\label{equ:n1}
    (delta_1*s)^2  + delta_1*s*N= 4lx
\end{equation}
\begin{equation}
\label{equ:n2}
     (delta_2*s)^2 + delta_2*s*N = 4lx+4ly
\end{equation}
\begin{equation}
\label{equ:n3}
     (delta_3*s)^2 + delta_3*s*N = 4ly
\end{equation}

Now, using the fact that the equations are linear combinations, we get \eqref{equ:n1} + \eqref{equ:n3} = \eqref{equ:n2}:
\begin{equation*}
    (delta_1*s)^2  + delta_1*s*N + (delta_3*s)^2 + delta_3*s*N = (delta_2*s)^2 + delta_2*s*N
\end{equation*}
Solving for $N$:
\[N = \frac{s(delta_2^2-delta_1^2-delta_3^2)}{2(delta_1+delta_3-delta_2)}\]

Solving \eqref{equ:n1} for $x$ and \eqref{equ:n3} for $y$ and using $N$ which is now in terms of known values, we get
\begin{equation*}
    x = \frac{(s*delta_1)^2 + 2s*delta_1*N}{4l}
\end{equation*}
\begin{equation*}
    y = \frac{(s*delta_3)^2 + 2s*delta_3*N}{4l}
\end{equation*}
To find $z$, we go back to $N = \sqrt{(x-l)^2+(y-l)^2+z^2}$. We now have equations for $N$, $x$, and $y$, so we just need to solve this for $z$:
\[ z = \sqrt{N^2-(x-l)^2-(y-l)^2}\]
The last calculations to do are translating the Cartesian coordinates into polar coordinates. Per the software requirements, angle measurements will be in degrees. $z$ is the same in both coordinate systems, so the only values to find are radius $r$ and angle $\theta$.
\begin{gather*}
    r = \sqrt{x^2+y^2}\\
    \theta = \tanh{(\frac{y}{x})}*\frac{180}{\pi}
\end{gather*}

\end{document}


\chapter{Data imputation}
\label{ch:data-imputation}

\section{Model estimation}

Whenever some variables are measured, there is a chance of failing on collecting the data and missing some samples. It could be related to different reasons. % people not answering some questions in surveys or incomplete medical records or even public entities failing to report or just choosing not to disclose them.
Also, sampling data from sensors and reporting it via wireless communication channels is not an exception.

Therefore, handling missing data is a problem that has been under research for a long time. Being Multiple Imputation\cite{imputationRubin}, Expectation-Maximization\cite{imputationEM}, Nearest Neighbours\cite{imputationNN} and hot-deck\cite{imputationHotDeck} the most popular techniques to deal with them.

%As in the previous step, and based on time constraints, it has been decided not to implement algorithms from scratch but rely on existing known packages to avoid investing in developing time.

Moritz et al. have analysed several univariate time series imputation implementations in R\cite{MoritzComparison}, which results later have been compiled in the \texttt{imputeTS} R package\cite{imputeTS}. Therefore it is reasonable to rely on their work and use their results to decide which imputing strategy should be taken.
Thus, for the current work, the \texttt{imputeTS} package will estimate a structural time series model from the data and then perform Kalman smoothing to fill the gaps.

It should be noted that before performing a Kalman smoothing, it is needed to have a state representation of the model, for which \texttt{imputeTS} supports Auto-ARIMA state space estimation and structural time series model fitted by maximum likelihood. Moreover, it should be mentioned that the development has been mainly done in Python. Nonetheless, for this phase, an R package will be called from Python using the \texttt{rpy2} module as an interface to bounce between both languages.

\pagebreak
\section{Auto-ARIMA}

\subsection{ARIMA models}

It is defined that a time series $\{X_t\}$ can be modelled by an \emph{integrated autoregressive moving average} (ARIMA) model if there exist a $d$-th difference $\{Y_t\} = \nabla^d \{X_t\}$ that can be modelled as an ARMA model\cite{brockwell2016introduction}.

Let $\nabla$ and $B$ be the difference and backshift operators, respectively. Then:

\begin{align}\label{eq1}
	\begin{split}
		\{Y_t\} &= \nabla^d \{X_t\}, \quad d>0 \\
				&= (1-B)\nabla^{d-1}\{X_t\} \\
				& \quad\quad \vdots \\
				&= (1-B)^{d-1}\nabla\{X_t\} \\
				&= (1-B)^d \{X_t\} \longrightarrow \text{ARMA}(p,q) \\
	\end{split}
\end{align}

Therefore, $\{Y_t - \mu\}$ will be $\text{ARMA}(p,q)$ with mean $\mu$ if satisfies the following difference equation:

\begin{equation*}
	Y_t - \phi_1Y_{t-1} - \cdots - \phi_pY_{t-p} = Z_t + \theta_1Z_{t-1} + \cdots + \theta_qZ_{t-q}
\end{equation*}

Which written using backshift operator:

\begin{align}\label{eq2}
	\phi(B)\{Y_t\} &= \theta(B) Z_t \quad,\quad Z_t \sim \mathcal{N}(0,\sigma^2)
\end{align}

Replacing \ref{eq1} in \ref{eq2} the ARIMA process can be defined as:

\begin{align}\label{eq:arima}
	\begin{split}
		\phi(B)(1-B)^d \{X_t\} &= \theta(B) Z_t \\
		\{X_t\} &\sim \text{ARIMA}(p,d,q)
	\end{split}
\end{align}

\subsection{SARIMA}

 It is said that a time series has a seasonality of period $s$ if $Y_t = Y_{t-s}$. Written using the backshift operator this is $Y_t = B^sY_t$.

 Using the same logical development than for ARIMA derivation, it can be said that $\{Y_t\}$ follows a Seasonal ARIMA (SARIMA) process with period $s$ \cite{brockwell2016introduction} :

\begin{equation*}
 	\{Y_t\} \sim \text{ARIMA}(p,d,q)(P,D,Q)_s
\end{equation*}

 If and only if

\begin{equation}\label{eq3}
 	\{X_t\} = (1-B)^d (1-B^s)^D \{Y_t\}
\end{equation}

Is a causal ARMA process defined by

\begin{equation}\label{eq4}
	\phi(B)\Phi(B^s)X_t = \theta(B)\Theta(B^s)Z_t , \quad\{Z_t\} \sim \mathcal{N}(0,\sigma^2)
\end{equation}

Therefore, replacing (\ref{eq3}) in (\ref{eq4}), the SARIMA definition can be rewritten as

\begin{equation*}\label{eq5}
	\phi(B)\Phi(B^s)(1-B)^d (1-B^s)^D \{Y_t\} = \theta(B)\Theta(B^s)Z_t , \quad\{Z_t\} \sim \mathcal{N}(0,\sigma^2)
\end{equation*}

Most of the literature obviate a constant $c$ as part of the model since it does not add more insight about the concepts around these derivations. Nonetheless, as it will be seen in the following section it is considered as part of the auto-ARIMA estimation, therefore, the final ARIMA definition in current work is given by:

\begin{equation}\label{eq:sarima}
	\phi(B)\Phi(B^s)(1-B)^d (1-B^s)^D \{Y_t\} = c + \theta(B)\Theta(B^s)Z_t , \quad\{Z_t\} \sim \mathcal{N}(0,\sigma^2)
\end{equation}

\subsection{Auto-ARIMA algorithm}

Despite the fact that in the library the procedure is called \emph{auto-ARIMA}, it does also support SARIMA models.

The main goal of auto-tuning these models is to choose the appropriates $p, q, d, P, Q$ and $D$ values so the model can make a good approximation of the process. If $d$ and $D$ are known, $p, q, P, Q$ can be selected by using an information criterion such as the \ac{aic}\cite{Akaike1998}:

\begin{equation}
	\text{AIC} = -2\log(L) + 2(p+q+P+Q+k)
\end{equation}

Where $k=1$ if $c\neq0$ in equation (\ref{eq:sarima}) and $0$ otherwise, and $L$ is the maximised likelihood of the model fitted to $(1-B)^d (1-B^s)^D \{Y_t\}$\cite{autoarimaLib}

\texttt{ImputeTS} library does not implement the (S)ARIMA estimation on its code. Instead, it uses the \texttt{forecast} package to do for it\cite{imputeTS}.

The \texttt{forecast} library implements the Hyndman-Khandakar algorithm for \ref{eq:sarima}, which uses an heuristic that combines unit root tests, \ac{aic} minimisation and MLE maximisation as shown in Figure \ref{alg:autoarima}\cite{autoarimaLib}.

\begin{figure}[H]
	\noindent\fbox{
	{\small\parbox{\textwidth}
		{
			\subsubsection*{Step 1: Find $D$ and $d$}

			For non-seasonal data run KPSS unit-root tests\cite{kpss}. If the results are significant difference the data and repeat $d$ times until the test results are insignificant.

			For seasonal data it is considered ARIMA$(p,d,q)(P,D,Q)_s$ models where $s$ is the seasonal frequency and $D=0$ or $D=1$ depending on a extended Canova-Hansen test\cite{canovahansen} (for $s$ > 13 the library estimates critical values $C_s = 0.269s^{0.928}$).

			After the seasonal pattern has passed its stability test and $D$ has been selected, $d$ is chosen by successive KPSS tests for seasonal differenced data if $D=1$, or the original data if $D=0$

			\subsubsection*{Step 2: Initial model}

			Fit the following models

			\begin{itemize}
				\item ARIMA$(2,d,2)$ if $s=1$ or ARIMA$(2,d,2)(1,D,1)$ if $s \geq 1$
				\item ARIMA$(0,d,0)$ if $s=1$ or ARIMA$(0,d,0)(0,D,0)$ if $s \geq 1$
				\item ARIMA$(1,d,0)$ if $s=1$ or ARIMA$(1,d,0)(1,D,0)$ if $s \geq 1$
				\item ARIMA$(0,d,1)$ if $s=1$ or ARIMA$(0,d,1)(0,D,1)$ if $s \geq 1$
			\end{itemize}

			From these ones it is selected the one with lowest AIC score as initial model, it will be called \emph{current} model and denoted as ARIMA$(p,d,q)$ if $s=1$ or ARIMA$(p,d,q)(P,D,Q)_s$ if $s \geq 1$

			\subsubsection*{Step 3: Explore variations}

			13 variations of current model are explored. They are summarised as:

			\begin{itemize}
				\item One of $p, q, P$ and $Q$ is allowed to vary by $\pm1$ from the current model
				\item $p$ and $q$ vary both $\pm1$ from the current model
				\item $P$ and $Q$ both vary by $\pm1$ from the current model
				\item $c$ is included if the current model has $c=0$ or excluded if the current model has $c\neq0$.
			\end{itemize}

			Once it is found a model with better AIC score than the current model, then this one is updated. The algorithm stops when no better AIC than the current is found.

			\emph{Note: The model updating is also subjected to stability constraints that can be found in the original publication for further details.}

		}
	}}
	\caption{Hyndman-Khandakar algorithm for Auto-ARIMA estimation}
	\label{alg:autoarima}
\end{figure}

\section{Structural times series estimation}

\subsection{State space representation}

A state-space representation of a time series is suitable for cases in which the underlying nature of a process is hidden and cannot be determined. Nonetheless,  indirect observations can be measured.

Thus, state-space representations are given by two equations. The \emph{observation equation} (\ref{eq:obs}) relates $w$-dimensional observable measures as a linear function of the $v$-dimensional noisy hidden state, and the \emph{state equation} (\ref{eq:state}) determines how the hidden state will transition from time $t$ to $t+1$.

\begin{align}
	\bm{Y}_t		&=	G\bm{X}_t + \bm{W}_t  \label{eq:obs} \\
	\bm{X}_{t+1}	&=	F\bm{X}_t + \bm{V}_t \label{eq:state}
\end{align}

Where $F$ is a $v \times v$ matrix, $G$ a $w \times v$ matrix, $\{\bm{W}_t\} \sim \mathcal{N}(0,R)$, $\{\bm{V}_t\} \sim \mathcal{N}(0,Q)$ and $E(\bm{V}_s\bm{W}^T_t) = 0$ for all $t, s$.

\subsection{Structural models}

The classical structural models are defined in terms of trend $(m)$, seasonal $(s)$ and noise $(\varepsilon)$ components (\ref{eq:struct}). Although useful for some applications, they can be too deterministic for some others.

\begin{equation}\label{eq:struct}
	X_t = m_t + s_t + \varepsilon_t
\end{equation}

State-space representations allow bringing more flexibility to these components. Therefore, it is natural to extend these concepts to a state-space domain.

\subsubsection*{Local level model}

To show how the model is built up, it will be derived by adding component by component from the most straightforward model: the random walk. Let $\{Y_t\}$ be the observable variable from a state-space (\ref{eq:loclvl_obs}) in which the hidden state corresponds is a random variable (\ref{eq:loclvl_st}), which in fact will determine the \emph{local level model}\cite{brockwell2016introduction}.

\begin{align}
	Y_t		&= M_t + W_t, \quad W_t \sim \mathcal{N}(0,\sigma^2_w) \label{eq:loclvl_obs} \\
	M_{t+1}	&= M_t + Vt, \quad V_t \sim \mathcal{N}(0,\sigma^2_v) \label{eq:loclvl_st}
\end{align}

\subsubsection*{Local linear trend model}

It is not difficult to extend this local level model to a \emph{local linear trend model} by adding a slope state $B_t$.

\begin{equation}
	M_t = M_{t-1} + B_{t-1} + V_{t-1} \label{eq:loclintrend}
\end{equation}

Introducing randomness into the slope also

\begin{equation}
	B_t = B_{t-1} + U_t , \quad  U_t \sim \mathcal{N}(0,\sigma^2_u) \label{eq:randslope}
\end{equation}

As now this model contains multiple state, in order to write the model in state-space form, it can be defined the state vector:

\begin{equation}
	\bm{X}_t = (M_t, B_t)^T \label{eq:statevect}
\end{equation}

Thus, using (\ref{eq:statevect}), (\ref{eq:loclintrend}) and (\ref{eq:randslope}) can be rewritten as

\begin{equation}
	\bm{X}_{t+1} =
	\begin{bmatrix}
		1 & 1 \\
		0 & 1
	\end{bmatrix} \bm{X}_{t+1} + \bm{V}_{t}, \quad t = 1, 2, \ldots
	\label{eq:lintrendstates}
\end{equation}

Such that
\begin{equation}\label{eq:V_trend}
	\bm{V}_{t} = (V_t, U_t)^T
\end{equation}

The observation equation for the process $\{Y_t\}$ is then by

\begin{equation}\label{eq:lintrendobs}
	Y_t = \begin{bmatrix}1 & 0\end{bmatrix}\bm{X}_{t} + W_t
\end{equation}

Thus, from (\ref{eq:lintrendstates}) and (\ref{eq:lintrendobs}) the missing pieces to define the state-space equations are:

\begin{equation}\label{eq:F_trend}
	F = \begin{bmatrix}
		1 & 1 \\
		0 & 1
	\end{bmatrix}
\end{equation}

\begin{equation}
  G = \begin{bmatrix}1 & 0\end{bmatrix}
\end{equation}

\begin{equation}
  Q = \begin{bmatrix}
  		\sigma^2_v 	& 0 \\
  		0 			& \sigma^2_u
  \end{bmatrix}
\end{equation}

\begin{equation}
  R = \sigma^2_w
\end{equation}

\subsubsection*{Noisy seasonal model}

Same as the classical structural models, the seasonal component $s_t$ with period $d$ has the properties\cite{maravall1985structural}

\begin{align*}
	s_{t+d} &= s_t \\
	\sum_{t=1}^{d}{s_t} &= 0
\end{align*}

Expanding it as in \cite{brockwell2016introduction} it is obtained that

\begin{equation}\label{eq:noiseasonal}
	s_{t+1} = -s_t - \cdots - s_{t-d+2}, \quad t=1,2,\ldots
\end{equation}

From (\ref{eq:noiseasonal}) it can be constructed a generalised sequence $\{Y_t\}$ by adding a random variable $S_t \sim \mathcal{N}(0, \sigma^2_s)$

\begin{equation}
	Y_{t+1} = -Y_t - \cdots - Y_{t-d+2} + S_t, \quad t=1,2,\ldots
\end{equation}

Now, to put it into a state-space form, it is defined the state vector

\begin{equation}
	\bm{X} = (Y_t, Y_{t-1}, \ldots, Y_{t-d+2})^T
\end{equation}

Therefore the observation equation for $\{Y_t\}$

\begin{equation}
	Y_t = \begin{bmatrix}1 & 0 & 0 & \cdots & 0\end{bmatrix} \bm{X}, \quad t=1,2,\ldots
\end{equation}

$\{\bm{X}\}$ in the state equation

\begin{align}
	\bm{X}_{t+1} &= F\bm{X}_t + \bm{V}_t , \quad t=1,2,\dots\\
	\bm{V}_t &= (S_t, 0, \ldots, 0)^T \label{eq:V_noise} \\
	F &=
	\begin{bmatrix}
		-1		& -1		& \cdots	& -1		& -1 		\\
		 1		&  0 		& \cdots 	&  0		& 0  		\\
		 0		&  1 		& \cdots 	&  0		& 0  		\\
		\vdots	& \vdots	& \ddots	& \vdots	& \vdots	\\
		0		&  0		& \cdots 	&  1		& 0
	\end{bmatrix}\label{eq:F_noise}
\end{align}

\subsubsection*{Structural time series general model}

As in (\ref{eq:struct}), to construct a general (additive) structural time series model, it is needed to sum each of the components, i.e., the general model is built as result of merging the linear trend and the noisy seasonal models.

The state vector is then

\begin{equation}
	\bm{X}_t =
	\begin{bmatrix}
		\bm{X}_t^1 \\
		\bm{X}_t^2
	\end{bmatrix} =
	\begin{bmatrix}
		(M_t, B_t)^T \\
		(Y_t, Y_{t-1}, \ldots, Y_{t-d+2})^T
	\end{bmatrix} =
	\begin{bmatrix}
		\begin{bmatrix}
			M_t \\
			B_t
		\end{bmatrix} \\
		\begin{bmatrix}
			Y_t \\
			Y_{t-1} \\
			\vdots \\
			Y_{t-d+2}
		\end{bmatrix}
	\end{bmatrix}
\end{equation}

The state equation

\begin{align}
	\bm{X}_{t+1}	&=
	\begin{bmatrix}
		F_1	& 0 \\
		0	& F2
	\end{bmatrix} \bm{X}_{t} +
	\begin{bmatrix}
		\bm{V}_t^1 \\
		\bm{V}_t^2
	\end{bmatrix}
\end{align}

Where $F_1$ and $F_2$ are the matrices defined in (\ref{eq:F_trend}) and (\ref{eq:F_noise}) respectively. $\bm{V}_t^1$ and $\bm{V}_t^2$ are (\ref{eq:V_trend}) and (\ref{eq:V_noise}).

And the observations equation

\begin{equation}
	Y_t = 	\begin{bmatrix}1 & 0 & 1 & 0 & \cdots & 0\end{bmatrix}\bm{X}_{t} + W_t
\end{equation}

\subsection{Kalman prediction}

Let $\bm{X} = (X_1, \ldots, X_v)^T$ be a random vector. Then it can be defined

\begin{equation}
	P_t(\bm{X}) \coloneqq (P_t(X_1), \ldots, P_t(X_v))^T
\end{equation}

Such that

\begin{equation}
	P_t(X_i) \coloneqq P(X_i | Y_0, \ldots, Y_t)
\end{equation}

Is the best linear predictor of $X_i$ in terms of all components of $Y_0, Y_1, \ldots, Y_t$\cite{brockwell2016introduction}

Then the Kalman one-step predictor for a state-space model given by (\ref{eq:obs}) and (\ref{eq:state}) is defined as

\begin{equation}
	\hat{\bm{X}} \coloneqq P_{t-1}(\bm{X}_t)
\end{equation}

And their error covariance matrices

\begin{equation}
	\Omega_t = E[(\bm{X}_t - \hat{\bm{X}}_t)(\bm{X}_t - \hat{\bm{X}}_t)^T]
\end{equation}

The Kalman predictive recursions are then defined by

Initial conditions:
\begin{align}\label{eq:kalm_pred_init}
\begin{split}
		\hat{\bm{X}}_1 &= P(\bm{X}_1 | \bm{Y}_0) \\
		\Omega_1 &= E[(\bm{X}_1 - \hat{\bm{X}}_1)(\bm{X}_1 - \hat{\bm{X}}_1)^T]
\end{split}
\end{align}

Recursions:
\begin{align}\label{eq:kalm_pred_1}
\begin{split}
	\hat{\bm{X}}_{t+1} &= F_t\hat{\bm{X}}_t + \Theta_t\Delta_t^{-1}(\hat{\bm{Y}}_t - G_t\hat{\bm{X}}_t) \\
	\Omega_{t+1} &= F_t \Omega_t F_t^T + Q_t - \Theta_t \Delta_t^{-1} \Theta_t^T
\end{split}
\end{align}

Where,
\begin{align}\label{eq:kalm_pred_2}
\begin{split}
	\Delta_t &= G_t \Omega_t G_t^T + R_t \\
	\Theta_t &= F_t \Omega_t G_t^T
\end{split}
\end{align}

\subsection{Structural models estimation}

Consider a vector $\bm{\theta}$ whose components can fully parametrise the state space given by (\ref{eq:obs}) and (\ref{eq:state}).

Therefore, it is possible to find $\bm{\hat{\bm{\theta}}}_{MLE}$ by maximising the observations in $\{Y_t\}$ with respect to the parameters in $\bm{\theta}$.

If the conditional probability density of $\bm{Y}_t | \bm{Y}_{t-1}, \ldots, \bm{Y}_0$ is $f(\cdot | \bm{Y}_{t-1}, \ldots, \bm{Y}_0)$, then the likelihood can be expressed as

\begin{equation}\label{eq:structts_lik_1}
	\mathcal{L}(\bm{\theta} ; \bm{Y}_1, \ldots, \bm{Y}_n) = \prod_{t=1}^{n}{f(\bm{Y}_t | \bm{Y}_{t-1}, \ldots, \bm{Y}_0)}
\end{equation}

In general (\ref{eq:structts_lik_1}) is hard to solve. But, if it is assumed that $\bm{Y}_0$ , $\bm{X}_1$ and $\bm{W}_t$, $\bm{V}_t, t=1,2,\ldots$ are \emph{jointly Gaussian}, then the resulting conditional densities will have the form\cite{brockwell2016introduction}

\begin{equation}
	f(\cdot | \bm{Y}_{t-1}, \ldots, \bm{Y}_0) =  \left(2 \pi\right)^{-w/2} \left(\det \Delta_t\right)^{-1/2} \exp \left\{-\frac{1}{2} \bm{I}_t^T \Delta_t^{-1}\bm{I}_t\right\}
\end{equation}

Where

\begin{equation}
	\bm{I}_t = \bm{Y}_t - P_{t-1} \bm{Y}_t = \bm{Y}_t - G \hat{\bm{X}}_t
\end{equation}

And $P_{t-1} \bm{Y}_t$ and $\Delta_t, t \geq 1$ are obtained from the Kalman prediction recursions.

Finally, under the Gaussianity assumptions, the likelihood can be rewritten as\cite{brockwell2016introduction}

\begin{equation}\label{eq:structts_lik_2}
	\mathcal{L}(\bm{\theta} ; \bm{Y}_1, \ldots, \bm{Y}_n) =
	\left(2 \pi\right)^{-\frac{nw}{2}}
	\left( \prod_{j=1}^{n}{\det \Delta_j} \right)^{-\frac{1}{2}}
	\exp\left\{ - \frac{1}{2} \sum_{j=1}^{n}{\bm{I}_t^T \Delta_t^{-1}\bm{I}_t} \right\}
\end{equation}

Now, for any value of $\bm{\theta}$, the likelihood values $\mathcal{L}(\bm{\theta} ; \bm{Y}_1, \ldots, \bm{Y}_n)$ can be calculated using the help of Kalman recursions. Thus, in order to find $\hat{\bm{\theta}}_{MLE}$ it is needed to run some nonlinear optimisation algorithm to find the best $\bm{\theta}$ by maximising $\mathcal{L}$\footnote{Constrained to the optimisation algorithm behaviour}. In the current work it is used the L-BFGS-B algorithm via \texttt{optim} calls in R.

\pagebreak
\section{Kalman smoothing}

A time series smoothing consists in the estimation of the hidden states $\bm{X}_t$ given the complete time series $\bm{Y}_1, \ldots, \bm{Y}_n$ such that $n > n$\cite{timeseries_statespace_models}. Which is especially suitable when there is any missing data point at a time $t < n$, this is:

\begin{equation}
	\bm{X}_{t|n} = P_n \bm{X}_t
\end{equation}

An the error covariance matrices\cite{brockwell2016introduction}

\begin{equation}
	\Omega_{t|n} = E \left[(\bm{X}_{t} - \bm{X}_{t|n})(\bm{X}_{t} - \bm{X}_{t|n})^T \right]
\end{equation}

Then, the Kalman iterations for the smoothing problem are \cite{brockwell2016introduction}

\begin{align}
	P_n \bm{X}_t
	&=
	P_{n-1} \bm{X}_t + \Omega_{t,n} G_n^T \Delta_n^{-1} (\bm{Y}_n - G_n \hat{\bm{X}}_n)
	\\
	\Omega_{t,n+1}
	&=
	\Omega_{t,n} \left[ F_n `\Theta_n \Delta_n^{-1} G_n \right]^T
	\\
	\Omega_{t|n}
	&=
	\Omega_{t|n-1} - \Omega_{t,n} G_n^T \Delta_n^{-1} G_n \Omega_{t,n}^T
\end{align}

Given the initial conditions

\begin{align}
	\hat{\bm{X}}_t &= P_{t-1} \bm{X}_t \\
	\Omega_{t,t} &= \Omega_{t|t-1} = \Omega_t
\end{align}

Which are found from the Kalman prediction.

\section{Comparative experiments}

A set of thorough experiments have been designed to compare both model estimation techniques and empirically determine which one performs best for the current dataset.

The starting point is a database containing 500 \acp{rbs} and approximately two months of measurements. Then, the database is mined to find $n=50$ signals per feature with no missing data. After that, it has been artificially removed a given ratio of data using a uniform distribution so that it can be simulated a \ac{mcar} scenario\cite{rantou2017missing}.

The two algorithms are run to estimate the model and use them to run Kalman smoothing. It is used the coefficient of determination $R^2$ to compare the performance of the models.

\begin{equation}\label{eq:R2}
	R^2 = 1 - \frac{\sum_{i}{(y_i - \hat{y}_i)^2}}{\sum_{i}{(y_i - \bar{y}_i)^2}}
\end{equation}

Some of the good results are presented in Figure \ref{fig:imp_exp_good}

\begin{figure}[hptb]
	\begin{subfigure}{.47\textwidth}
		\includegraphics[width=\textwidth]{imp_traffic}
		\caption{Radio traffic load imputation}
		\label{fig:imp_radio_traffic}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.47\textwidth}
		\includegraphics[width=\textwidth]{imp_temp}
		\caption{Cabinet temperature}
		\label{fig:imp_temp}
	\end{subfigure}
	\caption{Imputation experiments good results}
	\label{fig:imp_exp_good}
\end{figure}

Although the plots above show promising results, there are other cases where the experiment did not perform as expected.

\pagebreak

\subsubsection*{Unintuitive $R^2$ values}

There are cases in which Auto-ARIMA estimation show poor and even negative $R^2$ values as shown in Figure \ref{fig:imp_exp_issue}. The latter are unintuitive results, as $R^2$ values are expected to be limited to the $[0,1]$ interval.

Nonetheless, this is meant only for linear models, where the worst fitted model is assumed to be the observations mean[add citation]. Thus, having negative $R^2$ values implies that the observations mean explains more variance than the fitted model.

\begin{figure}[hptb]
	\begin{subfigure}{.47\textwidth}
		\includegraphics[width=\textwidth]{imp_sys_voltage}
		\caption{\ac{pdu} system voltage imputation}
		\label{fig:imp_pdu_sys_voltage}
	\end{subfigure}%
	\hfill
	\begin{subfigure}{.47\textwidth}
		\includegraphics[width=\textwidth]{imp_power_load}
		\caption{Average \ac{psu} utilization imputation}
		\label{fig:imp_psu_load}
	\end{subfigure}
	\caption{Imputation experiments good results}
	\label{fig:imp_exp_issue}
\end{figure}

\subsubsection*{Optim convergence failures}

The model estimation threw runtime exceptions for some features due to the R code calls to the \texttt{optim} library not converging. These exceptions were found to be an actual bug in the library that occurs when the function being optimised tends to a constant value.

A dedicated routine was written to catch whenever this exception was thrown and run a simple interpolation instead of estimating the model.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.55\linewidth]{imp_conv_issue}
	\caption{Problematic signals for imputeTS}
	\label{fig:imp_conv_issue}
\end{figure}

\section{Database construction algorithm}

It has been implemented a pipeline to build a unified and non-corrupted database from the sparsed raw files so that the forecasting section could learn from reliable data.

In the Figure, it is shown how the implemented blocks interact with each other to accomplish this task.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.65\linewidth]{db_construction_algorithm}
	\caption{Database construction algorithm}
	\label{fig:db_construction_algorithm}
\end{figure}


The basic rules of logic are very simple. Logica is entirely defined as a way to manipulate symbols. These symbols are usually common signs such as numbers, letter, Greek letters, etc., but it is not forbidden to use any symbol we find useful or interesting. In this section, precisely with the goal of illustrating the \textit{independence of logic from the symbols chosen}, we shall manipulate smileys and other icons. \textbf{The first step is thus choosing the symbols.} Logic is really the description of most (complex) dynamic processes that search for an answer in a complex space. This search may be a purely mathematical search, or it could be a program running. The dynamic process is defined in terms of a series of rules which are activated whenever we find a matching input. Thus, \textbf{the second step is choosing the rules that define our dynamic processes}.

After defining symbols and rules, things become interesting and we can try to use our system to find answers and process information. We usually start with a given proposition, which is the input of the whole process. A \textit{proposition} is just a series of symbols, such as for example: \texttt{3 + 2 = 5 of \dSmiley\dSmiley\Candle\Candle}. We apply the rules to the proposition until we reach the desired answer, and we have also answered all of the intermediate questions that came to existance during the dynamic process.

\paragraph{A concrete example}
Let us consider a full example. Consider the symbols of our language to be:
\begin{itemize}
\item A smiley \dSmiley
\item A candle \Candle
\item A tree \Springtree
\item A coffee cup \Coffeecup
\end{itemize}

We consider a proposition to be true if and only if we can process it until we reach a \Coffeecup. \footnote{Given the extreme importance of coffee in the diet of mathematicians and computer scientists, equating coffee with truth does not really seem that illogical a step} Our rules are:

\begin{itemize}
\item \textbf{(G0)} A \Coffeecup means we are done
\item \textbf{(R1)} Two \dSmiley followed by a \Springtree, and then further followed by \texttt{r}, means that we will have to process \texttt{r} to find the answer
\item \textbf{(R2)} Two \Candle followed by a \Springtree, and then further followed by \texttt{r}, means that we will have to process \texttt{r} to find the answer
\end{itemize}

Consider now the input proposition of \\
\dSmiley\dSmiley\Springtree\Candle\Candle\Springtree\dSmiley\dSmiley\Springtree\Coffeecup \\

We begin by using \textbf{R2}, therefore obtaining: \\
\Candle\Candle\Springtree\dSmiley\dSmiley\Springtree\Coffeecup \\

We then use rule \textbf{R1}, therefore obtaining: \\
\dSmiley\dSmiley\Springtree\Coffeecup \\

We then use rule \textbf{R2}, therefore obtaining: \\
\Coffeecup \\

Now according to \textbf{G0} we are done. The proof is successfull, therefore we can conclude that \dSmiley\dSmiley\Springtree\Candle\Candle\Springtree\dSmiley\dSmiley\Springtree\Coffeecup was \textbf{true within our system of rules}.

Consider now the new input proposition of \\
\dSmiley\dSmiley\Springtree\dSmiley\dSmiley\Springtree\Candle\Springtree\Coffeecup \\

We begin by using \textbf{R2}, therefore obtaining: \\
\dSmiley\dSmiley\Springtree\Candle\Springtree\Coffeecup \\

We then use rule \textbf{R1}, therefore obtaining: \\
\Candle\Springtree\Coffeecup \\

Unfortunately now we cannot apply rule \textbf{R1} again, because we have no \dSmiley at the head of our proposition; we cannot apply rule \textbf{R2} because we have no \Candle at the head of our proposition; and we can certainly not apply rule \textbf{G0} because there is no lonely \Coffeecup. If we cannot apply any of our rules, the process is \textit{stuck}. This means that we cannot prove \dSmiley\dSmiley\Springtree\dSmiley\dSmiley\Springtree\Candle\Springtree\Coffeecup with our rules, thus \dSmiley\dSmiley\Springtree\dSmiley\dSmiley\Springtree\Candle\Springtree\Coffeecup was \textbf{not true within our system of rules}.


\section{Logistic Regression}
\smallskip \hrule height 2pt \smallskip

\begin{itemize}
	\item Another probabilistic approach to classification (categorical predictions).  \hfill \\
	\item directly optimizing the conditional log likelihood % TA 3/15/2016
	\item Discriminative: learn $P(Y|X)$ directly then discriminate between classes.    % Week 5 audio.
		We solved similar problems using Bayes before (a generative approach).
	But if we don't want to bother with modeling all those joint or conditional probabilities, we can do this discriminative approach instead. \hfill \\  %Wed 2/3 audio transcription.
	\item Parameters from gradient ascent % End of slide set 7
	\item Linear, uses a probabilistic model, and is discriminative.% End of slide set 7
	\item produces weights, $w^*$ of dim n + 1 (n = number of features in each training point).
		These parameters are tied together, unlike in Naive Bayes where there is independence for parameters for different classes.
	\item Use the dot product of those weights against the feature vectors as an input to the sigmoid function.
\end{itemize}

Logistic Regression is the discriminative counterpart to a Naive Bayes generative classifier over Boolean features.  % https://www.cs.cmu.edu/~tom/mlbook/NBayesLogReg.pdf
The difference between logistic and Naive Bayes is just one word: "conditional".   % wk 5 audio
We are maximizing conditional log likelihood, that is conditional on X.
	We are not going to spend our time encoding the distribution over X.   % wk 5 audio
\hfill \\

Parameters are tied together, unlike in Naive Bayes.  % audio week 6
\hfill \\

Can use discrete or continuous outputs. \hfill \\ % https://www.youtube.com/watch?v=zAULhNrnuL4
It's a linear classifier; the decision rule is a hyperplane. \hfill \\  % Slide 6 summary
You optimize it (find weights) by gradient ascent, which works because it is concave.    \hfill \\  % Slide 6 summary
You can use "maximum conditional a posteriori" for regularization.    \hfill \\  % Slide 6 summary

\hfill \\
Note: $ \displaystyle \frac{e^x}{1 + e^x} = \frac{1}{1 + e^-x}$ \hfill \\  % student/TA question/ans Feb 8th about gradient ascent/descent.

\hfill \\

\underline{Summary from non-class sources:} \hfill \\
% https://www.youtube.com/watch?v=-Z2a_mzl9LM
We are still using linear regression in the inputs, but putting the result into a sigmoid function. \hfill \\
Recall $w_0 + w_1 x_1 + w_2 x_2 + w_3 x_3 = w^Tx$ and $x = (1, x_1, x_2, x_3)$.  \hfill \\
$P(death|x) = \sigma(w^Tx)$  %https://www.youtube.com/watch?v=-Z2a_mzl9LM
where $\sigma$, the sigmoid function,  converts your regression output into a sigmoid curve. \hfill \\
$\displaystyle \sigma(a) = \frac{1}{1+ e^{-a}} = \frac{1}{1+ e^{-(w_0 + w_1 x_1 + w_2 x_2 + w_3 x_3)}} = \frac{1}{1+ e^{-(w^Tx)}}$   \hfill \\ % https://www.youtube.com/watch?v=-Z2a_mzl9LM
\hfill \\

% https://www.youtube.com/watch?v=_Po-xZJflPM  :
We can convert this to a linear relationship by "taking the logit". \hfill \\
The logit (log odds) is the inverse of the logistic.  \hfill \\ %  https://en.wikipedia.org/wiki/Logistic_regression
$F(x) = \sigma(a)$ above.  It is the probability that the dependent variable equals a case, given some linear combination of the predictors.  It can range from $- \infty$ to $\infty$   % https://en.wikipedia.org/wiki/Logistic_regression
The logit is $\ln \frac{F(x)}{1-F(x)}$, or equivalently, after exponentiating both sides: \hfill \\
$\frac{F(x)}{1-F(x)} = e^{w^Tx}$  \hfill \\
The logit (i.e., log-odds or natural logarithm of the odds) is equivalent to the linear regression expression.

Note used odds ratio: $\frac{p}{1-p}$  \hfill \\
$\logit(\frac{1}{1+ e^{-(w^Tx)}}) = \log(\frac{\frac{1}{1+ e^{-(w^Tx)}}}{1-\frac{1}{1+ e^{-(w^Tx)}}}) $  \hfill \\ % https://www.youtube.com/watch?v=_Po-xZJflPM
We can now proceed with linear regression.  \hfill \\
Note that our predictions are now on the log scale; this impacts interpretation of the coefficients.  \hfill \\  %https://www.youtube.com/watch?v=_Po-xZJflPM

\hfill \\ \hfill \\

\underline{Lecture's presentation:} \hfill \\

Notation:  \hfill \\
\begin{itemize}
	\item $x_i^j$: the $i^{th}$ attribute of data point $j$
	\item $y^j$: the $j^{th}$ class  %(?)
	\item $x^j$: the $j^{th}$ training example
\end{itemize}

Once again we don't want to try to estimate $P(X,Y)$; that is challenging due to the size of the distribution. \hfill \\
We could make the Naive Bayes assumption and only need to calculate $P(X_i | Y)$,
but if we want $P(Y|X)$, why not learn that directly?  You can use logistic regression.
\includegraphics[width=1.5in]{figures/expo.pdf}     \hfill \\
\hfill \\

Reuse ideas from regression, but let the y-intercept define the probability.  \hfill \\
$P(Y=1|\bm{X, w}) \propto exp(w_0 + \sum_i w_i X_i)$  \hfill \\
With normalization constants:  \hfill \\
$\displaystyle  P(Y=0|\bm{X, w}) \frac{1}{1+ exp(w_0 + \sum_i w_i X_i)} $ \hfill \\
$\displaystyle  P(Y=1|\bm{X, w}) \frac{exp(w_0 + \sum_i w_i X_i)}{1+ exp(w_0 + \sum_i w_i X_i)} $ \hfill \\
Logistic function: \includegraphics[width=1in]{figures/logistic.pdf}     \hfill \\
 \hfill \\

Making a decision boundary out of logistic equations:  \hfill \\
Output the $Y$ with the highest $P(Y|X)$.   \hfill \\
If binary Y, output Y=1 if $\displaystyle 1 < \frac{P(Y=1|X)}{P(Y=0|X)}$  \hfill \\
That simplifies to just $1 <exp(w_0 + \sum_i w_i X_i)$ or \hfill \\
$0 <w_0 + \sum_i w_i X_i$   \hfill \\
\includegraphics[width=.8in]{figures/logistic_boundary_linear.pdf}     \hfill \\
\textbf{The decision boundary is a line (or hyperplane), hence we have a linear classifier!} \hfill \\  \hfill \\

For $ \displaystyle P(Y=0 | \bm{X,w}) = \frac{1}{1 + exp(w_o + w_1 x_1)}$:  \hfill \\

\includegraphics[width=3.4in]{figures/decision_boundary_examples.pdf}   \hfill \\

\includegraphics[width=2in]{figures/decision_boundary_example.pdf}   \hfill \\

(See notes for more $w_0, w_1$ values plotted.)  \hfill \\
In these plots, Y is the probability that the class is 1.    \hfill \\
The red curve is the sigmoid.  The blue line is the decision boundary.  \hfill \\
The decision boundary is from the equation $0 = w_1X + w_0$.  \hfill \\
% Erick advice: ignore the blue lines entirely.  don't need them to find probability distribution.

\hfill \\
Larger weights result in a sharper curve.  The bias $w_0$ shifts there the middle of the curve is.   \hfill \\
The red sigmoid defines a probability distribution over $Y$ in \{0,1\} for every possible input X. \hfill \\
\hfill \\
The decision boundary leads to $P(Y=0|X, w) = 0.5$ when you are at the $y=0$ point on the line.   \hfill \\
(E/J words:  when the blue line crosses the x axis, that's when the sigmoid curve is above 1/2, which corresponds to classifying it as a no/0.)  \hfill \\
\hfill \\

% Forum 3/15/2016:
If $w_0 = 0$, $P(Y=0|X, w) = 0.5$ when at the $y=0$ point on the line: the exponent just becomes 0 and the whole equation evaluates to 0.5.
If $w_0 \neq 0 $, this statement is no longer true.
And similar argument for the 2-d case.
The slope of the line defines how quickly the probabilities go to 0 or 1 around the decision boundary.
\hfill \\
\hfill \\

2D inputs: \hfill \\
For $ \displaystyle P(Y=0 | \bm{X,w}) = \frac{1}{1 + exp(w_o + w_1 x_1 + w_2 x_2)}$:  \hfill \\
\includegraphics[width=2in]{figures/decision_boundary_example-2D.pdf}   \hfill \\

$P(Y=0 | X, w)$ decreases as $w_0 + \sum_i w_i x_i$ increases.
Again, if you set the stuff inside the exponential to zero, you get the decision boundary hyperplane.

\subsubsection{Finding the w coefficients: Loss Function}
\underline{Simple Intro}: \hfill \\  % TA e-mail 3/15/2016
You can use the chain rule in the opposite direction to decompose the loss function into the log likelihood of $X$ as well as the conditional log likelihood of the labels $y$ given $X$.

\underline{Class Version}: \hfill \\
Generative (Naive Bayes) loss function:
Now $j$ is a data point with observations indexed over $i$.

\begin{align*}
	\ln P(D | \bm{w}) = \sum_{j=1}^N  &  \ln P(x^j, y^j | \bm{w}) \mbox{   } \mbox{    (the full log-likelihood)}\\
					& \mbox{use Bayes' rule to rewrite conditionally}  \\  % J added this line.
					= \sum_{j=1}^N  &  \ln [P(y^j | x^j, \bm{w}) P(x^j | \bm{w})] \\ % J added this line.
				=  \sum_{j=1}^N  & \ln P(y^j | x^j , \bm{w}) + \sum_{j=1}^N \ln P(x^j | \bm{w})
\end{align*}

We decide to ignore the 2nd term because it won't help you get better predictions for that data anyway.
Or, "From a machine learning perspective, "God gave us the data" and we don't care about the 2nd sum."  % Erick 2/6/2016

Professor Farhadi is calling this first time a discriminative (logistic regression) loss function:  \hfill \\
It is helping you discriminate between different classes.  It's not going to help you model the data.
This is unlike regression; we don't care about the value it puts out.  We only care about what the resulting class is.  \hfill \\

% Erick.
This is the difference between statistics and machine learning.  We only care about getting the best $\bm{w}$ for discriminating between classes.

\textbf{Conditional Data Likelihood:}
"Conditional" because you are conditioning on what $\bm{X}$ is.
\begin{align*}
	\ln P(D_Y | D_{\bm{X}}, \bm{w}) = \sum_{j=1}^N \ln P(y^j | \bm{x}^j, \bm{w})
\end{align*}
$D_Y$ = ???  \hfill \\
$D_{\bm{X}}$ = ???   \hfill \\
Doesn't waste effort learning $P(X)$.  Focuses on $P(Y| \bf{X})$, which is all that matters for classification. \hfill \\
Discriminative models cann't compute $P(\bm{x}^j | \bm{w})$!  ???
\hfill \\

\subsubsection{Conditional Log Likelihood}
Just need to figure out how to go up and find the maximum likelihood.

(the binary case only).  \hfill \\
$P(Y=0 | \bm{X}, \bm{w}) = \frac{1}{1 + \exp(w_0 + \sum_i w_i X_i)}$  \hfill \\
$P(Y=1 | \bm{X}, \bm{w}) = \frac{\exp(w_0 + \sum_i w_i X_i)}{1 + \exp(w_0 + \sum_i w_i X_i)}$  \hfill \\

($ l( \bm{w} )$ is conditional data log-likelihood.)
\begin{align*}
	l( \bm{w})  \equiv  &  \sum_j \ln P(y^j | x^j, \bm{w})  \\
	& \mbox{Since $y^j$ is in \{0, 1\}, sum over the two cases: }   \\
	& \mbox{(the $y^j$ and $(1-y^j)$ act like delta functions)}   \\
	l(\bm{w})  =  & \sum_j y^j \ln P(y^j = 1 | x^j, \bm{w}) +(1 - y^j) \ln P(y^j = 0 | x^j, \bm{w})  \\
	& \mbox{plug in the definition of the likelihoods and do algebra to get:}  \\
	=& \sum_j  y^j (w_0 + \sum_i^n w_i x_i^j)  - \ln(1 + \exp(w_0 + \sum_i^n w_i x_i^j))
\end{align*}

While we can't find a closed-form solution to optimize $l(\bm{w})$,  $l(\bm{w})$ is concave so we can to gradient \underline{as}cent.
Jsut need to figure out how to go up and find the maximum likelihood.   % Wk 5 audio

\subsubsection{Gradent ascent to optimize w}
To maximize, we can't take derivative w.r.t $\bm{w}$ and optimize b/c no closed form solution.  % Wk 5 audio
Instead we form the gradient vector and move along the gradient. % Wk 5 audio
Conditional likelihood for Logistic Regression is convex.  (see above)  \hfill \\
In this case it doesn't matter b/c we have a concave function.  % Wk 5 audio
Iterate to find $\bm{w}$: start somewhere, find gradient direction, make step in that direction, repeat, repeat.
\hfill \\

\textbf{Gradient}:  gives ascent or descent direction. \hfill \\
\begin{align*}
	\nabla_w l(\bm{w}) = [\frac{\partial l(\bm{w})}{\partial w_0}, \dots, \frac{\partial l(\bm{w})}{\partial w_n}]'  \hfill \\
\end{align*}
	(The $'$ at the end is for transpose b/c usually a column vector.)  \hfill \\
\textbf{Update Rule:} \hfill \\
\begin{align*}
	\Delta \bm{w} &= \eta \nabla_{\bm{w}} l(\bm{w})
\end{align*}
$\eta$ is the learning rate.  $\eta > 0$.
	It can't be too big or you can get lost.
	It can't be too small or you take forever to converge.

Your next weights $(t+1)$ become:
$w_i^{(t+1)} \leftarrow w_i^{(t)} + \eta \frac{\partial l(\bm{w})}{\partial w_i}$   \hfill \\
\hfill \\
Gradient ascent is the simplest  of optimization approaches.
Note that conjugate gradient ascent is much better (see reading).(?)  \hfill \\

See slides for derivation of: \hfill \\
$\displaystyle \frac{\partial l(w)}{\partial w_i} = \sum_j x_i^j (y^j - P(Y^j = 1 | x^j, w))$

\subsubsection{Example of gradient ascent to maximize conditional log likelihood}
(M(C)LE; C for conditional) \hfill
Learning an approximation of the step function.  % Wk 5 audio.

Use the equations: \hfill \\
\begin{itemize}
	\item \textbf{eq 1:} $w_i^{(t+1)} \leftarrow w_i^{(t)} + \eta \frac{\partial l(\bm{w})}{\partial w_i}$
	\item \textbf{eq 2:} $\displaystyle \frac{\partial l(w)}{\partial w_i} = \sum_j x_i^j (y^j - P(Y^j = 1 | x^j, w))$
			 The loss function is conditional on log likelihood.  % Wk 5 audio
	\item \textbf{eq 3:} $P(Y=1 | X, W) = \frac{\exp(w_0 + \sum_i w_i X_i)}{1 + \exp(w_0 + \sum_i w_i X_i)}$
	\item \textit{note:} superscript is for the $j^{th}$ data point and
			subscripts are for the $i^{th}$ value of the input ($x$) array.
			Don't forget that there is an $i=0$ $(x_0 = 1)$ bias term.
\end{itemize}
\includegraphics[width=1.5in]{figures/gradient_ascent_logistic_regression.pdf}   \hfill \\
\begin{itemize}
	\item begin with t=0 and $w = [w_0 + w_1 + w_2] = [0, 0, 0]$
	\item calculate \textbf{eq 3} for each Y.
		\begin{itemize}
			\item For j = 0:
				\begin{align*}
					P(Y^0 = 1 | x^0, w) &= \frac{\exp(0 + 0*3 + 0*(-3))}{(1 +  \exp(0 + 0*3 + 0*(-3)))} \\
						&= 1/(1+1) = 1/2
				\end{align*}
			\item For j = 1:
				\begin{align*}
					P(Y^1 = 1 | x^1, w) &= \frac{\exp(0 + 0*(-2) + 0^(2))}{(1 +  \exp(0 +  0*(-2) + 0^(2)))} \\
						&= 1/(1+1) = 1/2
				\end{align*}
		\end{itemize}
	\item calculate the terms that go into \textbf{eq 2} by looping over the 3 values of $i$ (bias and two other coefficients) and 2 values of $j$ (2 data points).
		\begin{itemize}
			\item for $i=0, j=0$: ($j=$0th (1st) training example, bias term) \hfill \\
				 $x_0^0(y^0 - P(Y^0=1|x^0, w)) = 1(1-0.5) = 0.5$
			\item for $i=0, j=1$: ($j=$1st (2nd) training example, bias term) \hfill \\
				 $x_0^0(y^0 - P(Y^0=1|x^0, w)) = 1(0-0.5) = -0.5$

			\item for $1=1, j=0$: ($j=$0th (1st) training example, $x_1$ term) \hfill \\
				 $x_1^0(y^0 - P(Y^0=1|x^0, w)) = -2(1-0.5) = 1.5$
			\item for $i=1, j=1$: ($j=$1st (2nd) training example, $x_1$ term) \hfill \\
				 $x_1^1(y^1 - P(Y^1=1|x^1, w)) = -2(0-0.5) = 1.0$

			\item for $1=2, j=0$: ($j=$0th (1st) training example, $x_2$ term) \hfill \\
				 $x_2^0(y^0 - P(Y^0=1|x^0, w)) = -3(1-0.5) = -1.5$
			\item for $i=2, j=1$: ($j=$1st (2nd) training example, $x_2$ term) \hfill \\
				 $x_2^1(y^1 - P(Y^1=1|x^1, w)) = 2(0-0.5) = -1.0$
		\end{itemize}
	\item Now we can compute the gradient (\textbf{eq 2}):
		\begin{itemize}
			\item $\nabla_w l(\bm{w}) = [\frac{\partial l(w)}{\partial w_1}, \frac{\partial l(w)}{\partial w_2}, \frac{\partial l(w)}{\partial w_3}]$
				\begin{align*}
					%\frac{\partial l(w)}{\partial w_i} &= \sum_j x_i^j (y^j - P(Y^j = 1 | x^j, w)) \\
						&= [0.5 - 0.5, 1.5 + 1.0, -1.5 - 1] = [0, 2.5, -2.5]
				\end{align*}
		\end{itemize}

	\item Use $\eta = 0.1$ to scale the gradient:  (\textbf{eq 1}) \hfill \\
			$w = [0,0,0] + 0.1*[0, 2.5, 2.5] = [0, 0.25, -0.25]$
	\item Start over with \textbf{eq 3} and updated $w$.
		\includegraphics[width=2in]{figures/logistic_regression_ex_loop_2.pdf}
\end{itemize}

How to do more algorithmically:  \hfill \\
You don't have to get all the info to update all the weights at once.
Do them one at a time.  \hfill \\
\includegraphics[width=2.5in]{figures/gradient_descent_algorthim.pdf}

\underline{How do you decide when to stop?} \hfill \\
(Not in notes!) \hfill \\

You need a criteria that tells you when to stop updating your gradient.
	Use a threshold for magnitude of the gradient.
	When the amount of change is small it is not changing a lot.  If concave, you are close to global optima.

Why don't we wait until it gets to zero?
	The chances of us hitting zero is low, and we might spend a lot of time wasting CPU cycles on negligible changes.

When we are really far from the global optimum we can afford to take bigger steps.
	Think about the objective function.  We can afford to make big steps if we are far.
	If we make big steps when close, we can get farther away .

You could use this policy: let $\nu$ be big at the beginning of a loop counter.
	Increase the value until you see a drop.
	"Line search" asks "what is maximum value of this parameter that gives you no drop?"

\subsubsection{Overfitting in Logistic Regression}

 \underline{Intro \#1:}  \hfill \\
Be weary of large parameters.  % Audio wk 5
	Large parameters (large $a$ in $\frac{1}{1 + \exp(-a)}$) lead to a step function.
	That's ok if you truly want a step, but you should be concerned about overfitting the minute you see a large parameter.
	We could regularize again: apply a penalty for large parameters.
    	Could do something like maximizing $w^* = \argmax_w l(w) - \lambda*(||w||_2^2)$ instead of $w* =  \argmax_w l(w)$.
	But that's not ideal.  Might have a hard time finding a suitable value for $\lambda$.  \hfill \\

Instead, use a probabilistic way of regularization.
	We've done this before using a prior.
	Instead of MLE, we did MAP. The prior regularizes.
	$P(w | Y, X) \propto P(Y | X,w)P(w)$.
	What would be the form of our prior ($P(w)$) be?
	We want the posterior to be differentiable.
	The likelihood was a sigmoid shape, so the log likelihood had the form of was of the form $e^{something}$.
	We already  know how to get the conditional likelihood in the form of $e^{blah}$.
	Well behaved summation of terms.   \hfill \\

We pick a gaussian distribution for the prior for mathematical convenience.
	We wanted to form a function we can optimize.
	If it doesn't come from gaussian and does come from multinomial, what happens?
	We wouldn't be too wrong.  And it is just a regularization.
	We are just trying to constrain $\bm{w}$ with something that is mathematically well behaved.
	Usually the prior is off. But it is only there to keep w from getting too big.  It gives a low score when w is too big.   \hfill \\

Q from student: why aren't we considering $P(X,W)$ as the prior? Why $P(Y|X, w)$?
	P(X,W) is discriminative.
	We aren't reformulating our problem.  Just looking to estimate the parameters.
	We want to regularize the parameter not the data (?).    \hfill \\

 \underline{Intro \#2:}   \hfill \\

Like in linear regression, the maximum likelihood solution prefers higher weights.
Higher weights can give higher likelihood of properly classifying examples close to the decision boundary.
This over-fitting causes features to have larger influences on the decision:  \textbf{beware of overfitting!!!}.
Again, you can use regularization to penalize high weights.
This will be covered more later.
\hfill \\

\subsubsection{MAP for Logistic Regression}
Above was M(Conditional)LE.     \hfill \\
Recall that the MAP/MLE difference is \_\_\_\_\_\_\_\_\_.  \hfill \\
Priors on $\bm{w}$ are commonly added for regularization.    \hfill \\
Helps avoid very large weights and overfitting.    \hfill \\
$P(\bm{w} | Y, \bm{X}) \propto P(Y | \bm{X}, \bm{w} ) P(\bm{w} )$  \hfill \\
Use a normal distribution with zero mean and "identity covariance".  (\_\_\_\_):  \hfill \\
$ \displaystyle  P(\bm{w} ) = \prod_i \frac{1}{\kappa \sqrt{2 \pi}} e^{\frac{-w_i^2}{2 \kappa^2}}$ \hfill \\
$\kappa^2$ is variance.  \hfill \\
\hfill \\

Now we form our MAP estimate:
\begin{align*}
	w* &= \argmax_w \ln[P(\bm{w}) \prod_{j=1}^N P(y^j | x^j, \bm{w})]  \\
		&= \argmax_w \ln[ \prod_i \frac{1}{\kappa \sqrt{2 \pi}} e^{\frac{-w_i^2}{2 \kappa^2}}   \prod_{j=1}^N P(y^j | x^j, \bm{w})]
\end{align*}

 Instead of focusing on the stuff after the $\prod$ in the bracket we also have $P(w)$.
 Our prior, $P(w)$ is $\prod$ fo gaussians.
 The $\prod(y^j|x^, x_j)$ is called likelihood (???)

Add $\log P(\bm{w})$ to the objective:
\begin{align*}
	\ln P(\bm{w}) & \propto - \frac{\lambda}{2} \sum_i w_i^2 \\
	\frac{\partial \ln P(\bm{w})}{\partial w_i} &= \lambda w_i
\end{align*}

We now have a new way of optimizing w.
This is a quadratic penalty (???) that drives the weights toward zero. \hfill \\
It adds a negative linear term to the gradients (???).  \hfill \\
Also applicable in linear regression!

\subsubsection{Review: MLE vs. MAP for Logistic Regression}
\underline{Maximum conditional likelihood estimate:}
\begin{align*}
	w* &= \argmax_w \ln[\prod_{j=1}^N P(y^j | x^j, \bm{w})] \\
	w_i^{(t+1)} & \leftarrow w_i^{(t)} + \eta \sum_j x_i^j [y^j - \widehat{P}(Y^j = 1 | x^j, \bm{w})]\\
\end{align*}

\underline{Maximum conditional a posteriori estimate:}
\begin{align*}
	w* &= \argmax_w \ln[P(\bm{w}) \prod_{j=1}^N P(y^j | x^j, \bm{w})] \\
	w_i^{(t+1)} & \leftarrow w_i^{(t)} + \eta \{ -\lambda w_i^{(t)} +  \sum_j x_i^j [y^j - \widehat{P}(Y^j = 1 | x^j, \bm{w})]\\
\end{align*}
The $\lambda$ term complains when the value is big.
This $-\lambda$ if from the derivative of the gaussian.
The whole point: if you add a prior and take a derivative, it is a form of regularization and should act as a penalty on big $w$.

\hfill \\  \hfill \\

Vocab \hfill \\
\begin{itemize}
	\item \textbf{discriminative}:  estimates joint probabilities.  E.g. $p(Data, Zebra)$, $p(Data, No Zebra)$.
	\item \textbf{generative}:  E.g. $p(Zebra | Data)$, $p(No Zebra | Data)$.
\end{itemize}

\subsubsection{Multiclass Logistic Regression (discrete labels)}
Define a weight vector $w_i$ for each $y_i$ where i ranges from $1$ to $R-1$ for $R$ classes.
You don't need a weight vector for the $R^{th}$ class b/c its probability is 1 - the sum of the rest.  \hfill \\
\includegraphics[width=1in]{figures/multiclass_logistic.pdf} \hfill \\
Each category will have its own w values.  \hfill \\ % Wk 5 audio.
Don't need to train k w values.  k-1.  b/c they have to sum to 1.    % Wk 5 audio.

\begin {align*}
	P(Y=1 | X ) & \propto \exp(w_{10} + \sum_i w_{1i}X_i)  \\
		& \mbox{note: $\bm{w}$ is now a matrix, not an array.}  \\
	P(Y=2 | X ) & \propto \exp(w_{20} + \sum_i w_{2i}X_i)  \\
	 & \dots \\
	 & \mbox{the last probability is defined relative to the rest.}  \\
	P(Y=r | X ) &= 1- \sum_{j=1}^{r-1} P(Y=j | X)
\end{align*}
After normalizing, your probabilities become:
\begin {align*}
	P(Y=y_k | X ) &=  \frac{\exp(w_{k0} + \sum_{i=1}^n w_{ki} X_i)}{1 + \sum_{j=1}^{R-1} \exp(w_{j0} + \sum_{i=1}^n w_{ji}X_i)} \\
	& \mbox{the last class is 1 - the sum of the other probabilities:} \\
	P(Y=y_R | X ) &=  \frac{1}{1 + \sum_{j=1}^{R-1} \exp(w_{j0} + \sum_{i=1}^n w_{ji}X_i)}
\end{align*}
Note: features can be discrete or discontinuous.
\hfill \\  \hfill \\

\subsubsection{Gaussian Naive Bayes vs Logistic Regression.}
Gaussian Naive Bayes with class-independent variances is representationally equivalent to Linear Regression.
("If you do have an infinite number of training examples, the GNB and LR produce identical classifiers.")  % Erick 2/7
The solutions different because of the objective (loss) function.  \hfill \\
\hfill \\

If you do have class-independent variances and the underlying model is gaussian, GNB does better than LR.
You get an incorrect model for GNB if you don't have class-independent variances.  %Erick
LR is less biased because it does not assume conditional independence.  % Erick.
\hfill \\

If you have enough categories, GNB w/ class independence will produce identical results to LR.  % week 6 audio
Given enough points \& the assumption features independence of class is held true, GNB will do better.
If these assumptions are *not* true then LR will do better.

 \hfill \\  \hfill \\

You could use either to learn $ f: X \rightarrow Y$ for $X$ that is a vector of real-valued features ($< X_1, \dots, X_n >$) and boolean Y. \hfill \\ \hfill \\

The two approaches make different assumptions:   \hfill \\
\begin{itemize}
	\item NB assumes features are independent given the class.  This is an assumption on $P(\bm{X} | Y)$.
	\item LR assumes functional form of $P(Y|\bm{X})$, and makes no assumption on $P(\bm{X} | Y)$.
\end{itemize}

Gaussian Naive Bayes classifier would assume: \hfill \\
\begin{itemize}
	\item all $X_i$ are conditionally independent given Y.
		If you fix what hump you are looking at, you don't need to worry about the rest of the parameters.
	\item model $P(X_i | Y = y_k)$ as Gaussian with $N(\mu_{ik}, \sigma_i)$.  (Your inputs are each produced by a gaussian.)
	\item model $P(Y)$ as Bernoulli$(\theta, 1-\theta)$
\end{itemize}
This implies the form of $P(Y|X)$ is:
$\displaystyle P(Y=1 | X= < X_1, \dots, X_n >) = \frac{1}{1 + \exp(w_0 + \sum_i w_i X_i)}$. \hfill \\
In the slides, he derives the form.   \hfill \\
(??)  You see that if you assume Gaussian in Logistic Regression, you get: \hfill \\
 $w_0 = \ln \frac{1 - \theta}{\theta} + \frac{\mu_{i0}^2 + \mu_{i1}^2}{2 \sigma_i^2}$ and $w_i = \frac{\mu_{i0} + \mu_{i1}}{\sigma_i^2}$

 His confusing summary: \hfill \\
 \includegraphics[width=2.5in]{figures/GNB_vs_LR.pdf} \hfill \\
 Attempted translation:

 Note: you need more parameters to train Naive Bayes. \hfill \\
 $4n + 1$ parameters for GNB,  $n + 1$ parameters for Logistic Regression. \hfill \\
 The NB parameter estimates are uncoupled, so there are more of them.   \hfill \\
 (The Logistic Regression parameter estimates \underline{are} coupled.)

If you don't have infinite data (? "non-asymptotic analysis"): \hfill \\
For $n$ attributes in $X$, NB needs $O(\log n)$ samples to converge and Logistic Regression needs $O(n)$.
So GNB converges more quickly to its (perhaps less helpful) "asymptotic estimates."

You have to do all of the optimization at once, all together if you are doing logistic regression.  ???
Easier in a certain sense to fit a Gaussian model.
Typically we have a lot of data, and that data is not gaussian so Logistic Regression is probably better.
\hfill \\

\subsubsection{More notes from class}  % week 6 audio
Logistic regression makes no assumption about $P(X|Y)$.
Naive Bayes assumed independence of parameters, conditioned on label.
Optimizing different loss functions.  \hfill \\
\hfill \\

Which is better?
Need to know how many parameters each classifier would need.
\# of parameters, \# of features?
How many parameters does NB need?
?? $2cC$ classes. C, d, n.  C classes, d is \# of training examples, n is number of features.
$4n + 1$ ??.
For logistic regression, the output was weights, w*.  Dim was n + 1.
NB parameters are independent (uncoupled).
Logistic regression: the parameters are tied together.  \hfill \\
\hfill \\

\underline{Gen vs Disc. for NB/LR} \hfill \\
If you have enough categories, GNB w/ class independence will produce identical results:
Given enough points and the assumption features independence of class is held true
If this is *not* true then LR will do better (when GNB is false).
\textbf{With limiting data, use GNB. }
GNB may converge faster but to something less true.  \hfill \\
\hfill \\

Do we do optimization with NB?  No.
We start w/ joint distribution,  $P(x|y)*prior$, $\dots$, take the max.
 We are not ML optimizing.  Not required to optimize every time we learn something.
\textbf{Both LR and NB are trying to do the same task, and one doesn't require training}.
\hfill \\  \hfill \\

NB requires less training examples.
\hfill \\  \hfill \\

Use logistic regression by default.  \hfill \\  \hfill \\

Is there a global optimum for LR with regularization?
No.  But still should be close.
If you add a regularization that breaks the concavity of the function, you can't prove anything.
\hfill \\

\subsection{Logistic Regression Protocol}
Steps:
\begin{itemize}
	\item start with given data: x and labels.
	\item form conditional log likelihood function.
	\item either optimize that or add a prior
	\item do hill climbing until you stop seeing big changes in the weights
	\item then you claim victory
\end{itemize}

\textbf{For binary classification:} \hfill \\
The final output of trained regression is the weight vector, $w*$.
It is d+1 dimensional for a d-dimensional feature vector.  The +1 is for bias (that coefficient is 1).  \hfill \\

\underline{How do you use it?}  \hfill \\
You found the form of decision boundary was $P(Y=1|X,w) = exp()/(1+ exp())$.
Put in the $x$ with the $w$.
Compute the probability of Y=0 and Y=1, pick the biggest result.
How do you know if your weights are good?
Test on held-out data (NOT the test data).

\subsubsection{Andrew Ng}
* gives numbers between 0 and 1 (good for classification)   \hfill \\
* called "logistic regression" but it is really for classification.  (don't be confused by "regression") \hfill \\
* "sigmoid function" and "logistic function" are essentially synonymous.  \hfill \\