\documentclass[t,usenames,dvipsnames]{beamer}
\usetheme{Copenhagen}
\usepackage{amsmath, tikz, xcolor, array}
\setbeamertemplate{headline}{} % remove toc from headers

\title{Polar Form of Complex Numbers}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Table of Contents}
    \tableofcontents[currentsection]
  \end{frame}
}
\DeclareMathOperator\cis{cis}

\begin{document}

\begin{frame}
    \titlepage
\end{frame}

\section{Plot Numbers in the Complex Plane}

\begin{frame}{The Complex Plane}
    The complex plane is very similar to the Cartesian $(x,y)$ plane from algebra. \newline\\

    The point $(x,y)$ in the Cartesian plane is represented by \alert{\[z = x + yi\]} in the complex plane.  \newline\\

    $x$ is called the \emph{real part} and $y$ is called the \emph{imaginary part}.
\end{frame}

\begin{frame}{The Complex Plane}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (3,0) node [right] {Real axis};
    \draw [->, >=stealth] (0,-0.5) -- (0,2) node [above] {Imaginary axis};
    \draw (2,0.1) -- (2,-0.1) node [below] {$x$};
    \draw (0.1,1) -- (-0.1,1) node [left] {$y$};
    \coordinate (Z) at (2,1);
    \draw [fill=black] (Z) circle (1pt) node [above right] {\small $z=x+yi$};
    \end{tikzpicture}
\end{frame}

\section{Find the Modulus and Argument of a Complex Number}

\begin{frame}{Modulus of a Complex Number}
    The \alert{modulus} of a complex number is the absolute value of it:
    \[
    |z| = |x + yi| = \sqrt{x^2+y^2}
    \]
    and it denotes the \emph{distance} the point is to the origin.
\end{frame}

\begin{frame}{Argument of a Complex Number}
    If we connect out point to the origin, it models an angle drawn in standard position. \newline\\

    \begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (3,0) node [right] {Real axis};
    \draw [->, >=stealth] (0,-0.5) -- (0,2) node [above] {Imaginary axis};
    \draw (2,0.1) -- (2,-0.1) node [below] {$x$};
    \draw (0.1,1) -- (-0.1,1) node [left] {$y$};
    \coordinate (Z) at (2,1);
    \draw [fill=black] (Z) circle (1pt) node [above right] {\small $z=x+yi$};
    \draw [thick, dashed, Plum] (0,0)  -- (Z);
    \node at (0,0) [xshift=0.75cm, yshift=0.2cm] {\small $\theta$};
    \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Argument of a Complex Number}
    If $z$ is in polar form, the total angle rotated $\theta$ is the \alert{argument} of $z$. \newline\\

    The set of all arguments of $z$ is denoted \alert{arg($z$)}. (These can be found by using co-terminal angles).    \newline\\

    If $z \neq 0$ and $-\pi < \theta \leq \pi$, then $\theta$ is the \alert{principal argument} of $z$, and is written \alert{$\theta = \text{Arg}(z)$}    \newline\\

    We can get the angle rotated via reference angles using
    \[
    \theta ' = \tan^{-1} \left( \frac{y}{x} \right)
    \]
\end{frame}

\begin{frame}{Example 1a.}
    Plot each complex number, find its modulus, real and imaginary parts, arg($z$) and Arg($z$). \newline\\

    (a) \quad   $z = \sqrt{3} - i$  \newline\\
    \pause
    The real part of $z$ is $\sqrt{3}$ and the imaginary part of $z$ is $-1$.
\end{frame}

\begin{frame}{Example 1a.}
    \begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (2,0) node [right] {\small $x$};
    \draw [->, >=stealth] (0,0.5) -- (0,-1.25) node [below] {\small $-yi$};
    \draw [fill=black] (1.5,-0.75) circle (1pt) node [below right] {\small $z=\sqrt{3}-i$};
    \draw (1.5,0.1) -- (1.5,-0.1) node [above, yshift=0.2cm] {\tiny $\sqrt{3}$};
    \draw (0.1,-0.75) -- (-0.1,-0.75) node [left] {\tiny $-1$};
    \end{tikzpicture}
    \end{center}
\pause
The modulus is
\[
|z| = \sqrt{(\sqrt{3})^2 + (-1)^2} = \sqrt{4} = 2
\]
\end{frame}

\begin{frame}{Example 1a.}
\emph{Tip:} Find the principal argument of $z$, denoted Arg($z$), before finding the general solution, arg($z$).
\pause
    \begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (2,0) node [right] {\small $x$};
    \draw [->, >=stealth] (0,0.5) -- (0,-1.25) node [below] {\small $-yi$};
    \draw [fill=black] (1.5,-0.75) circle (1pt) node [below right] {\small $z=\sqrt{3}-i$};
    \draw (1.5,0.1) -- (1.5,-0.1) node [above, yshift=0.2cm] {\tiny $\sqrt{3}$};
    \draw (0.1,-0.75) -- (-0.1,-0.75) node [left] {\tiny $-1$};
    \draw [thick, dashed, Plum] (0,0) -- (1.5,-0.75);
    \node at (0,0) [xshift=0.5cm, yshift=-0.1cm] {\scriptsize $\theta$};
    \end{tikzpicture}
    \end{center}
    \pause
    \[
    \text{Arg}(z) = \tan^{-1}\left( \frac{-1}{\sqrt{3}}\right) = -30^\circ = -\frac{\pi}{6}
    \]
\end{frame}

\begin{frame}{Example 1a.}
    To find arg($z$), use co-terminal angles with the answer you got for Arg($z$) to get
    \[
    \text{arg}(z) = -\frac{\pi}{6} + 2\pi k
    \]
    where $k$ is an integer.
\end{frame}

\begin{frame}{Example 1b.}
    (b) \quad   $z = -2+4i$  \newline\\
    \pause
    The real part of $z$ is $-2$ and the imaginary part of $z$ is $4$.
\end{frame}

\begin{frame}{Example 1b.}
    \begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (0.5,0) -- (-2,0);
    \draw [->, >=stealth] (0,-0.5) -- (0,2.5);
    \coordinate (Z) at (-1,2);
    \draw [fill=black] (Z) circle (1pt) node [above left] {$z = -2+4i$};
    \draw (-1,0.1) -- (-1,-0.1) node [below] {\scriptsize $-2$};
    \draw (-0.1,2) -- (0.1,2) node [right] {\scriptsize $4$};
    \end{tikzpicture}
    \end{center}
\pause
The modulus is
\[
|z| = \sqrt{(-2)^2 + 4^2} = \sqrt{20} = 2\sqrt{5}
\]
\end{frame}

\begin{frame}{Example 1b.}
    Arg($z$) is
    \[
    \tan^{-1} \left(\frac{4}{-2}\right) = \tan^{-1}(-2) \approx -63.4^\circ
    \]
\pause
Since $z$ is in the 2nd quadrant, we can put a $63.4^\circ$ reference angle (\emph{Reminder}: $63.4^\circ \approx \tan^{-1}(2)$) in quadrant II, so the total angle rotated is about $116.6^\circ$, or $\pi - \tan^{-1}(2)$ to be exact.   \newline\\      \pause
Thus, Arg($z$) = $\pi - \tan^{-1}(2)$, \newline\\
\pause  and \newline\\
arg($z$) = $\left(\pi - \tan^{-1}(2)\right) + 2\pi k$
\end{frame}

\begin{frame}{Example 1c.}
    (c) \quad   $z = 3i$  \newline\\
    \pause
    The real part of $z$ is 0 and the imaginary part of $z$ is $3$.
\end{frame}

\begin{frame}{Example 1c.}
\begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (2,0);
    \draw [->, >=stealth] (0,-0.5) -- (0,2);
    \draw (0.1,1.5) -- (-0.1,1.5) node [left] {\scriptsize 3};
    \draw [fill=black] (0,1.5) circle (1.5pt) node [right] {\scriptsize $z = 3i$};
    \end{tikzpicture}
\newline\\
\pause
The modulus is 3.   \newline\\
\pause
Arg($z$) = $\frac{\pi}{2}$  \newline\\
\pause
arg($z$) = $\frac{\pi}{2} + 2\pi k$
\end{center}
\end{frame}

\begin{frame}{Example 1d.}
   (d) \quad   $z = -117$  \newline\\
    \pause
    The real part of $z$ is $-117$ and the imaginary part of $z$ is 0.
\end{frame}

\begin{frame}{Example 1d.}
\begin{center}
    \begin{tikzpicture}
    \draw [<->, >=stealth] (-2,0) -- (2,0);
    \draw [->, >=stealth] (0,0) -- (0,1);
    \draw (-1.5,0.1) -- (-1.5,-0.1) node [below] {\scriptsize $-117$};
    \draw [fill=black] (-1.5,0) circle (1.5pt) node [above] {\scriptsize $z=-117$};
    \end{tikzpicture}
\newline\\  \pause
The modulus of $z$ is 117.  \newline\\  \pause
Arg($z$) = $\pi$    \newline\\      \pause
arg($z$) = $\pi + 2\pi k$
\end{center}
\end{frame}

\section{Write Complex Numbers in Polar Form and Vice Versa}

\begin{frame}{Write Complex Numbers in Polar Form}
    Writing polar form of complex numbers is a lot like writing rectangular coordinates as polar coordinates.   \newline\\
    \begin{center}
    \begin{tikzpicture}
    \draw [->, >=stealth] (-0.25,0) -- (2.5,0);
    \draw [->, >=stealth] (0,-0.25) -- (0,2);
    \draw (1.5,0) rectangle +(-0.2,0.2);
    \draw [thick, color=blue] (0,0) -- (1.5,0) -- (1.5,1) -- cycle;
    \node at (0.75,0) [below] {\scriptsize $x$};
    \node at (1.5,0.5) [right] {\scriptsize $y$};
    \node at (0.75,0.5) [above] {\scriptsize $r$};
    \draw [color=blue, fill=blue] (1.5,1) circle (1.5pt) node [above right] {$z = x+yi$};
    \node at (0.55,0.15) {\scriptsize $\theta$};
    \end{tikzpicture}
    \end{center}
\pause
Using the fact that $x = r\cos\theta$ and $y = r\sin\theta$, we get
\begin{align*}
    \onslide<2->{z &= x + yi\\[8pt]}
    \onslide<3->{&= r\cos\theta + (r\sin\theta)i \\[8pt]}
    \onslide<4->{&=r(\cos\theta + i\sin\theta)}
\end{align*}
\end{frame}

\begin{frame}{Write Complex Numbers in Polar Form}
    $r(\cos\theta + i\sin\theta)$ is often abbreviated $r\cis\theta$.
\end{frame}

\begin{frame}{Example 2a.}
    Write each in polar form.   \newline\\
    (a) \quad $z = \sqrt{3} - i$    \newline\\  \pause
    From Example 1a, $r = 2$, and $\theta = -\frac{\pi}{6}$.    \newline\\
    Thus, $z = 2\cis\left(-\frac{\pi}{6}\right)$
\end{frame}

\begin{frame}{Example 2b.}
    Write each in polar form.   \newline\\
    (b) \quad $z = -2 + 4i$    \newline\\  \pause
    From Example 1b, $r = 2\sqrt{5}$, and $\theta = \pi - \tan^{-1}(2)$.    \newline\\
    Thus, $z = \left(2\sqrt{5}\right)\cis\left(\pi - \tan^{-1}(2)\right)$
\end{frame}

\begin{frame}{Example 2c.}
    Write each in polar form.   \newline\\
    (c) \quad $z = 3i$    \newline\\  \pause
    From Example 1c, $r = 3$, and $\theta = \frac{\pi}{2}$.    \newline\\
    Thus, $z = 3\cis\left(\frac{\pi}{2}\right)$
\end{frame}

\begin{frame}{Example 2d.}
    Write each in polar form.   \newline\\
    (d) \quad $z = -117$    \newline\\  \pause
    From Example 1d, $r = 117$, and $\theta = \pi$.    \newline\\
    Thus, $z = 117\cis\left(\pi\right)$
\end{frame}

\begin{frame}{Write Complex Polar Numbers in Rectangular Form}
    Going backwards, expand and evaluate $\cis$ and distribute the modulus $r$.
\end{frame}

\begin{frame}{Example 3a.}
    Write each of the following in rectangular form. \newline\\
    (a) \quad   $4\cis\left(\frac{2\pi}{3}\right)$  \newline\\
    \begin{align*}
        \onslide<2->{4\cis\left(\frac{2\pi}{3}\right) &= 4\left(\cos\left(\frac{2\pi}{3}\right) + i\sin\left(\frac{2\pi}{3}\right)\right)}    \\[8pt]
        \onslide<3->{&=4\left(-\frac{1}{2} + \frac{\sqrt{3}}{2}i\right)}  \\[8pt]
        \onslide<4->{&= -2+2i\sqrt{3}}
    \end{align*}
\end{frame}

\begin{frame}{Example 3b.}
    (b) \quad   $2\cis\left(-\frac{3\pi}{4}\right)$ \newline\\
    \begin{align*}
        \onslide<2->{2\cis\left(-\frac{3\pi}{4}\right) &= 2\left(\cos\left(-\frac{3\pi}{4}\right) + i\sin\left(-\frac{3\pi}{4}\right)\right)}   \\[8pt]
        \onslide<3->{&= 2\left(-\frac{\sqrt{2}}{2} - \frac{\sqrt{2}}{2}i \right)}    \\[8pt]
        \onslide<4->{&= -\sqrt{2}-i\sqrt{2}}
    \end{align*}
\end{frame}

\begin{frame}{Example 3c.}
    (c) \quad   $3\cis(0)$  \newline\\
    \begin{align*}
        \onslide<2->{3\cis(0) &= 3(\cos(0) + i\sin(0))} \\[8pt]
        \onslide<3->{&= 3(1 + 0) = 3}
    \end{align*}
\end{frame}

\begin{frame}{Example 3d.}
    (d) \quad $\cis\left(\frac{\pi}{2}\right)$  \newline\\
    \begin{align*}
        \onslide<2->{\cis\left(\frac{\pi}{2}\right) &= \cos\left(\frac{\pi}{2}\right) + i\sin\left(\frac{\pi}{2}\right)}    \\[8pt]
        \onslide<3->{&= 0 + 1i = i}
    \end{align*}
\end{frame}

\section{Perform Arithmetic Operations on Complex Numbers in Polar Form}

\begin{frame}{Arithmetic Operations on Complex Numbers in Polar Form}
The rules for multiplying, dividing, and finding powers of complex numbers are the same as those for exponents.    \newline\\
\pause

\setlength{\extrarowheight}{10pt}
\begin{tabular}{ccc}
    \textbf{Rule} & \textbf{Exponents} & \textbf{Complex Numbers}    \\ \hline
    Product & $3x^{15}\cdot 4x^7 = 12x^{22}$ & $3\cis15^\circ \cdot 4 \cis 7^\circ = 12 \cis 22^\circ$ \\[10pt]
    Quotient & $\dfrac{3x^{15}}{4x^7} = \dfrac{3}{4}x^8$ &
    $\dfrac{3\cis15^\circ}{4\cis7^\circ} = \dfrac{3}{4}\cis 8^\circ$ \\[10pt]
    Power & $(3x^{15})^2 = 9x^{30}$ & $(3\cis15^\circ)^2 = 9\cis30^\circ$ \\[15pt]
\end{tabular}

The Power Rule is known as DeMoivre's Theorem
\end{frame}

\begin{frame}{Example 4a}
    Let $z=2\sqrt{3}+2i$ and $w=-1+i\sqrt{3}$. Find each of the following and write your answers in rectangular form.    \newline\\
    (a) \quad $zw$  \newline\\
    \pause
    In polar form, $z=4\cis\dfrac{\pi}{6}$ and $w=2\cis\dfrac{2\pi}{3}$.   \\[15pt]  \pause
    Thus, $zw = \left(4\cis\dfrac{\pi}{6}\right)\left(2\cis\dfrac{2\pi}{3}\right)=8\cis\dfrac{5\pi}{6}$   \\[15pt]  \pause
    In rectangular form, $8\cis\dfrac{5\pi}{6} = -4\sqrt{3} + 4i$
\end{frame}

\begin{frame}{Example 4b}
    (b) \quad $w^5$ \newline\\
    \pause
    $w^5 = \left(2\cis\dfrac{2\pi}{3}\right)^5 = 32\cis\dfrac{10\pi}{3}$  \\[15pt]   \pause
    $32\cis\dfrac{10\pi}{3} = 32\cis\dfrac{4\pi}{3}=-16-16i\sqrt{3}$
\end{frame}

\begin{frame}{Example 4c}
(c) \quad $\dfrac{z}{w}$    \newline\\  \pause
\begin{align*}
    \onslide<2->{\dfrac{z}{w} &= \dfrac{4\cis\frac{\pi}{6}}{2\cis\frac{2\pi}{3}}} \\[15pt]
    \onslide<3->{&= 2\cis\left(\frac{\pi}{6}-\frac{2\pi}{3}\right)} \\[15pt]
    \onslide<4->{&=2\cis\left(-\frac{\pi}{2}\right)=-2i}
\end{align*}
\end{frame}

\section{Find Roots of Complex Numbers}

\begin{frame}{Find Roots of Complex Numbers}
    Recall that $\sqrt[n]{x} = x^{1/n}$.   \newline\\
    We can reverse DeMoivre's Theorem to get roots of complex numbers.    \newline\\  \pause
    Given $w$ and $z$ are complex numbers, if there is a natural number $n$ such that $w^n = z$, then $w$ is an $n$th root of $z$.
\end{frame}

\begin{frame}{Find Roots of Complex Numbers}
    Let $z \neq 0$ be a complex numbrer with polar form $z=r\cis\theta$. For each natural number $n$, $z$ has $n$ distinct $n$th roots (denoted by $w_0, w_1, \dots, w_{n-1}$, given by
    \[
    w_k = \sqrt[n]{r}\cis\left(\dfrac{\theta + 2\pi k}{n}\right)
    \]
    \pause
    \emph{Tip:} You can still work in degrees and convert your answers to radians.
\end{frame}

\begin{frame}{Example 5a}
    Find each of the following. \newline\\
    (a) \quad both square roots of $z = -2 + 2i\sqrt{3}$    \pause
    \begin{align*}
        \onslide<2->{z &= 4\cis\dfrac{2\pi}{3} \left(\text{or } 4\cis120^\circ\right)} \\
        \onslide<3->{&=4^{1/2}\left(\cos\frac{120^\circ+360k}{2}+i\sin\frac{120^\circ+360k}{2}\right)}  \\
        \onslide<4->{&=2\left(\cos\left(60^\circ+180k\right) + i\sin\left(60^\circ+180k\right)\right)} \\
        \onslide<5->{&=2\cis60^\circ, 2\cis240^\circ} \\
        \onslide<6->{&=1+i\sqrt{3}, \quad -2+2i\sqrt{3}}
    \end{align*}
\end{frame}

\begin{frame}{Example 5b}
    (b) \quad the four fourth roots of $z=-16$ \pause
    \begin{align*}
        \onslide<2->{z &= 16\cis180^\circ} \\
        \onslide<3->{&=16^{1/4}\cis\left(\frac{180^\circ+360k}{4}\right)} \\
        \onslide<4->{&=2\cis\left(45^\circ+90k\right)}   \\
    \end{align*}
    \onslide<5->{$2\cis45^\circ, 2\cis135^\circ, 2\cis225^\circ, 2\cis315^\circ$} \newline\\
    \onslide<6->{$\sqrt{2}+i\sqrt{2}, -\sqrt{2}+i\sqrt{2}, -\sqrt{2}-i\sqrt{2}, \sqrt{2}-i\sqrt{2}$}
\end{frame}

\begin{frame}{Visualization of Solutions}
    \begin{center}
    \begin{tikzpicture}
    \draw [<->, >=stealth] (-3.5,0) -- (3.5,0) node [right] {$x$};
    \draw [<->, >=stealth] (0,-3.5) -- (0,3.5) node [right] {$yi$};
    \foreach \x in {-3,-2,-1,,,1,2,3}
    \draw (\x,0.1) -- (\x,-0.1) node [below] {\scriptsize $\x$};
    \foreach \y in {-3,-2,-1,,,1,2,3}
    \draw (0.1,\y) -- (-0.1,\y) node [left] {\scriptsize $\y i$};
    \coordinate (A) at (1.414,1.414);
    \coordinate (B) at (-1.414,1.414);
    \coordinate (C) at (-1.414,-1.414);
    \coordinate (D) at (1.414,-1.414);
    \draw [color=red, fill = red] (A) circle (1.5pt);
    \draw [color=red, fill = red] (B) circle (1.5pt);
    \draw [color=red, fill = red] (C) circle (1.5pt);
    \draw [color=red, fill = red] (D) circle (1.5pt);
    \draw [color=red, dashed] (A) -- (B) -- (C) -- (D) -- cycle;
    \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Example 5c}
    (c) \quad the three cube roots of $z = \sqrt{2} + i\sqrt{2}$ \pause
    \begin{align*}
        \onslide<2->{z &= 2\cis\left(45^\circ\right)} \\
        \onslide<3->{&= 2^{1/3}\left(\cis\left(\frac{45^\circ + 360k}{3}\right)\right)} \\
        \onslide<4->{&= \sqrt[3]{2}\cis\left(15^\circ + 120k\right)} \\
    \end{align*}
    \onslide<5->{$\sqrt[3]{2}\cis15^\circ, \sqrt[3]{2}\cis135^\circ, \sqrt[3]{2}\cis255^\circ$}  \newline\\
\end{frame}

\begin{frame}{Example 5d}
    (d) \quad the five fifth roots of $z = 1$   \pause
    \begin{align*}
        \onslide<2->{z &= 1\cis0} \\
        \onslide<3->{&= \cis\left(\frac{0+360k}{5}\right)} \\
        \onslide<4->{&=\cis\left(0+72k\right)} \\
    \end{align*}
    \onslide<5->{$1$, $\cis72^\circ$, $\cis144^\circ$, $\cis216^\circ$, $\cis288^\circ$}
\end{frame}
\end{document}


\section{Algorithm}
\label{sec:algorithm}

In this section we describe our HINGCN algorithm. Figure~\ref{figure:flow_graph}(c) shows a flow diagram of HINGCN.
\dan{TODO:signposting}

\subsection{Edge features as supplementary information}
\label{sec:edge}
According to research findings of Xu et al. \cite{XuHLJ19}, discriminative power of a graph neural network determines its representation capability.
We claim that information is loss if we only consider adjacency of target type vertices.
Let's consider again the example shown in Fig.~\ref{figure:example}. In long tailed meta-path $APCPA$, a shrinking $A$-$A$ adjacency loses information from intermediate $PCP$ nodes. And a graph neural network using only adjacency information cannot distinguish between two instances $p_1 = (a_1 p_1 c_1 p_2 a_4)$ and $p_2 = (a_1 p_2 c_2 p_3 a_4)$. Instead, both instances are represented as edge $\langle a_1 a_4\rangle$.
Therefore we propose to improve expressivity of our network by adding additional edge feature to homogeneous $A$-$A$ edges. These edge features are designed to effectly distinguish different intermediate nodes between meta-path instances.
We propose edge features that is composed of three different components, namely path count, intermediate node embeddings and PathSim\citep{SunHYYW11}.

\noindent{\small$\bullet$}\textbf{Path Count}. Different object pairs have different numbers of path instance between them and these numbers may reflect object similarity under a given meta-path. We propose to record count of path between objects $x_u$ and $x_v$ under meta-path $\Phi$ by:
\begin{equation*}
c(x_u,x_v) = \vert\{ p_{x_u \rightsquigarrow x_v}:p_{x_u \rightsquigarrow x_v} \vdash \Phi \}\vert
\end{equation*}

\noindent{\small$\bullet$}\textbf{Intermediate object embedding}.
To discriminate between different intermediate objects, we propose to use a sum of pre-trained node embedding as a component of edge feature. This node embedding can be trained from heterogeneous embedding algorithms such as \cite{GroverL16,DongCS17}. We favor sum over other aggregators since this operation possibly preserves maximum discriminant power on graphs \citep{XuHLJ19}.
Given a meta-path instance $p = (x_1x_2\ldots x_{l+1})$, we calculate
\begin{equation*}
o(x_1,x_{l+1}) = \dfrac{1}{(l+1) \cdot c(x_1,x_{l+1})} \sum\limits_{i=1}^{l+1} e_i
\end{equation*}
where $e_i$ is embedding of object $x_i$. Division of number of vertices $l+1$ and path count $c(x_u,x_v)$ is proposed to normalize intermediate object embedding to the scale of input object embeddings. Since path count is extracted and recorded as $c(x_u,x_v)$, this does no result in extra loss of information.

\noindent{\small$\bullet$}\textbf{PathSim}.
PathSim is proposed in \citep{SunHYYW11} to measure similarity between two objects of a same type on a heterogeneous graph. Given a symmetric meta-path $\Phi$, PathSim between $x_u$ and $x_v$ is:
\begin{equation*}
s(x_u,x_v) = \frac{2\times\vert\{ p_{x_u \rightsquigarrow x_v}:p_{x_u \rightsquigarrow x_v} \vdash \Phi \}\vert}{\vert\{ p_{x_u \rightsquigarrow x_u}:p_{x_u \rightsquigarrow x_u} \vdash \Phi \}\vert +\vert\{ p_{x_v \rightsquigarrow x_v}:p_{x_v \rightsquigarrow x_v} \vdash \Phi \}\vert }
\end{equation*}

The edge feature between objects $x_u$ and $x_v$ under meta-path $\Phi$ is thereby concatenated as:
\begin{equation}
\label{eq:edge}
r^{(0)}_\Phi(x_u,x_v) = c(x_u,x_v)\oplus o(x_u,x_v)\oplus s(x_u,x_v)
\end{equation}

\subsection{Aggregating node embedding}
We propose a modified version of scaled dot product attention mechanism to aggregate node embedding from its neighbor. Given a meta-path $\Phi$, we update vertex embedding as follow. First we apply 2 layer MLPs on node embedding and its neighbor respectively for scaling purpose. The query vector of node $i$ is
\begin{equation}
\label{eq:query}
q_i= W_{a1}^{(t)}(\text{tanh}(W_{a2}^{(t)}h^{(t)}_i ))
\end{equation}
And key vector $k_{i,j}$ is calculated as:
\begin{equation}
\label{eq:key}
k_{i,j} = W_{a3}^{(t)}(\text{tanh}(W_{a4}^{(t)}(h^{(t)}_j \oplus r^{(t)}_{i,j}) ))
\end{equation}
And the value vector of neighbor $j$ is calculated as:
\begin{equation}
\label{eq:value}
v^{(t)}_{i,j} = W^{(t)}( h^{(t)}_j \oplus r^{(t)}_{i,j} )
\end{equation}
where edge feature $r^{(t)}_{i,j}$ is concatenated to vertex feature of the neighbors, in order to provide additional information.

It is worth noticing that due to unique feature of each edge, both key and value vectors are no longer universal for different query nodes. We believe this uniqueness improves expressivity of self-attention mechanism for HIN embedding tasks.

\noindent Afterwards, the importance of a neighbor $j$ to object $i$ is calculated as:
\begin{equation}
\label{eq:dot}
\tilde{a}^{(t)}_{i,j} = q_i^T \cdot k_{i,j}
\end{equation}
Then we normalize this importance coefficient by softmax function and calculate attention coefficient as:
\begin{equation}
\label{eq:softmax}
a^{(t)}_{i,j} = \text{softmax}(\tilde{a}^{(t)}_{i,j}) = \dfrac{\text{exp}(\tilde{a}^{(t)}_{i,j})}{\sum_{k\in N^\Phi_i}\text{exp}(\tilde{a}^{(t)}_{i,k})}
\end{equation}
Finally the output of the node aggregation layer is a weighted sum of value vectors.
\begin{equation}
\label{eq:upd_node}
h^{(t+1)}_i = \text{ReLU}( W_{h1}\cdot \sum_{j\in N^\Phi_i} a^{(t)}_{i,j} \cdot v^{(t)}_{i,j} + W_{h2}\cdot h^{(t)}_i)
\end{equation}
Although a vertex $i$ is always in its own meta-path neighbor, we still explicitly add term $h^{(t)}_i$ in equation \ref{eq:upd_node} following GraphSAGE\cite{HamiltonYL17}.

\subsection{Aggregating edge embedding}
It is a natural idea to treat edge embeddings as parameters and update on pre-trained edge embeddings by gradient descend. However, this is not feasible in our experiment condition. Unlike language models where only a small portion of pre-trained embeddings are involved, in transductive settings, all edge embeddings we processed contribute to training process.
Due to the fact that the shrinking homograph is much denser than original graph, treating these edge embeddings as parameters would drastically increase number of parameters involved the model. A dense homogeneous adjacency could result in an update of billions of parameters in single epoch, especially in long tail meta-paths like $APCPA$ in DBLP dataset, where density of adjacency matrix could reach \dan{30\%?} or more.

Therefore, we propose another way to fine-tune edge embeddings by allowing convolution on edge embeddings. Pre-trained embeddings are served as initial feature, and the edge convolution layer is allowed to update edge embeddings by aggregating information from its endpoint nodes.
\begin{equation}
\label{eq:upd_edge}
r^{(t+1)}_{i,j} = \text{ReLU}(W_{r1}\cdot (h^{(t)}_i \oplus h^{(t)}_j \oplus r^{(t)}_{i,j}) + W_{r2}\cdot r^{(t)}_{i,j})
\end{equation}

Term $W_{r2}\cdot r^{(t)}_{i,j}$ at the end of the equation serve as a shortcut residual, in order to guarantee a feasible convergence.

In multi-layer HINGCN, we propose an alternate update scheme of node embedding and edge embedding.
\subsection{Dealing with over-smoothing via adaptive depth}
Plain GCNs often degrades rapidly as number of layers increases \cite{LiHW18,abs-1904-03751}. Stacking four or more layers into GCNs cause over-smoothing problem and features of vertices tends to converge to the same value. In heterogeneous graphs, this problem is even more severe. Let's consider again meta-path $APCPA$ in Fig.~\ref{figure:example}. A two layer $a_1 a_2 a_3$ propagation on the shrinking $A-A$ homo-graph actually passes across \textbf{8} edges on the underlying heterogeneous graph, which is a terrible number of layers for plain graph convolution networks.
In previous sections, our model already address this problem by including edge features in node update and updating edge feature. To further alleviate the over-smoothing problem, in equation \ref{eq:upd_node} and \ref{eq:upd_edge}, we have already included residual connections \cite{HeZRS16} in updating rule. And we propose to further improve the problem by creating jumping connections \cite{XuLTSKJ18}.
\begin{equation}
\label{eq:jump}
h^{(final)}_i = h^{(0)}_i \oplus h^{(1)}_i \cdots \oplus h^{(t)}_i
\end{equation}

\subsection{Aggregation across different meta-path semantics}
\label{sec:mp_aggr}
\dan{need some intuition/justification shit}
Given a node $i$, and its set of embeddings under different semantics, a metapath aggregator is a function $\gamma$ in the form of $y_i = \gamma_\Theta(\{x_{i,1},x_{i,2},..x_{i,P}\})$, where $x_{i,p}$ is the embedding of node $i$ under metapath $p$, and $y_i$ is the aggregated embedding of node $i$ in the HIN. $\Theta$ is the learnable paremeters of the aggregator.
Here we propose several metapath aggregators.

$\bullet$ \textbf{Attention}
\dan{cite} The first option we consider is scaled dot-product attention introduced in Transformer.
\begin{equation}
\label{eq:mp_mlp}
\tilde{h}^{\Phi_p}_i = W_{m1} \cdot \text{tanh}(W_{m2} \cdot h^{\Phi_p}_i)
\end{equation}

\begin{equation}
\label{eq:mp_attn}
\tilde{w}_i^{\Phi_p} = W_w \cdot \sigma(\tilde{h}^{\Phi_p}_i)
\end{equation}

\begin{equation}
\label{eq:mp_soft}
w_i^{\Phi_p} = \text{softmax}(\tilde{w}_i^{\Phi_p}) = \dfrac{\text{exp}(\tilde{w}_i^{\Phi_p})}{\sum_q \text{exp}(\tilde{w}_i^{\Phi_q})}
\end{equation}

After the fully-connected self-attention, we propose to reduce across different meta-path semantics by \textbf{Sum} operation.
\begin{equation}
\label{eq:mp_asum}
z_i = \text{ReLU}(\sum_p w_i^{\Phi_p} \cdot h^{\Phi_p}_i)
\end{equation}

$\bullet$ \textbf{Gated linear unit}
Inspired by forget gate mechanisms in recurrent networks (RNN) \dan{TODO:cite}, we compute a soft gate between 0 (low importance) and 1 (high importance) to represent different importance to each embedding entry.
We make the element-wise gated sum layer as follows:
\begin{multicols}{2}
\begin{equation}
o^{\Phi_p}_i=\sigma(W_o \cdot h^{\Phi_p}_i)
\end{equation}\break
\begin{equation}
\tilde{h}^{\Phi_p}_i=\text{tanh}(W_z \cdot h^{\Phi_p}_i)
\end{equation}
\end{multicols}
Finally, we have
\begin{equation}
\label{eq:mp_sum}
{z}_i= \sum_p o^{\Phi_p}_i\odot \tilde{h}^{\Phi_p}_i
\end{equation}

The gated unit $o_{\Phi_p}$ with sigmoid output (ranged (0,1)) is used to control which elements are to be aggregated in the output.

$\bullet$ \textbf{Other aggregators}
In preliminary experiments, \textbf{Concat}, \textbf{Max-Pooling} and \textbf{Mean} aggregators did not generate a better result, and we omit results of these aggregators in experiment section.

\subsection{HINGCN: Semi-supervised classification on Heterogeneous Information Network via Graph Convolution Networks}
After meta-path aggregation layer, the embeddings are fed to a 2-layer MLP for final output. In semi-supervised classification tasks, we minimize the Cross-Entropy loss and the model generates final prediction based on the largest output value.
\begin{equation}
\label{eq:loss}
\mathcal{L}(W)=   - \sum_{l\in \mathcal{Y}_L}Y^l ln(z^l)   + \lambda || W ||_2
\end{equation}
$ \mathcal{Y}_L$ is the set of labeled nodes in training set. $W$ represents the weight matrices used in the layers of our HINGCN. We use $L_2$ normalization for parameters involved.

To this end, we give a summary of overall process of HINGCN.
The part of embedding generation is summarized in Algorithm~\ref{alg}.
HINGCN first computes initial edge feature $r^{(0)}_{\Phi_p}$ for each edge under each meta-path. Then it applies an alternative update of node embedding and edge embedding.
After that each node fuses a meta-path specific embedding by jumping connections. These embeddings are further aggregated using either meta-path level attention or gated linear units. Finally, embeddings are fed to a two layer MLP and a softmax layer for classification output.

\begin{algorithm}
%\begin{small}
\caption{HINGCN Embedding Generation}
\label{alg}
\begin{algorithmic}[1]
\Require HIN $G=(V,E)$;
labeled type $T$;
meta-path set ${\Phi_0,\Phi_1,\ldots ,\Phi_P}$;
an initial node representation $\{ f_i,\forall x_i \in X_T \}$;
number of aggregation layers $L$;
\Ensure Final node embeddings $Z = \{z_1, ..., z_i\}$
\State $h^{(0)}_i \leftarrow f_i$, $\forall x_i \in X_T $
\For {$p \leftarrow 0$ to $P$}
\State Compose edge features $r^{(0)}_{\Phi_p}$ according to Eq.\ref{eq:edge}.
\EndFor
\For {$p \leftarrow 0$ to $P$}
 \For {$t \leftarrow 0$ to $L-1$}
 %Update node embeddings:
  \For {$x_i \in X_T$}
  \State $q_i= W_{a1}^{(t)}(\text{tanh}(W_{a2}^{(t)}h^{(t)}_i ))$
 \For {$x_j \in N^{\Phi_p}_i$}
  \State $k_{i,j} = W_{a3}^{(t)}(\text{tanh}(W_{a4}^{(t)}(h^{(t)}_j \oplus r^{(t)}_{i,j}) ))$
  \State $v^{(t)}_{i,j} = W^{(t)}( h^{(t)}_j \oplus r^{(t)}_{i,j} )$
  %Calculate importance of j to i
  \State $\tilde{a}^{(t)}_{i,j} = q_i^T \cdot k_{i,j}$
  %Calculate the attention coefficient
  \State $a^{(t)}_{i,j} = \text{softmax}(\tilde{a}^{(t)}_{i,j})$
 \EndFor
  \State $h^{(t+1)}_i = \text{ReLU}(W_{h1}\cdot \sum_{j\in N^{\Phi_p}_i} a^{(t)}_{i,j} \cdot v^{(t)}_{i,j}+ W_{h2}\cdot h^{(t)}_i) $
  \EndFor

  %\State Update edge embeddings:
  \For {$e \in E$}
  \State $(x_i,x_j) \leftarrow \text{Endpoints}(e)$
  \State $r^{(t+1)}_{i,j} = \text{ReLU}(W_{r1}\cdot (h^{(t)}_i \oplus h^{(t)}_j \oplus r^{(t)}_{i,j}) + W_{r2}\cdot r^{(t)}_{i,j})$
  \EndFor
 \EndFor
 \State $h^{\Phi_p}_i = h^{(0)}_i \oplus h^{(1)}_i \cdots \oplus h^{(t)}_i$
\EndFor
\State Aggregate meta-path specific embeddings and compute ${z}_i$ according to \textbf{Attention} (Eq.\ref{eq:mp_asum}) or \textbf{Gated Linear Units} (Eq.\ref{eq:mp_sum})
\State \Return $Z = \{z_1, ..., z_i\}$
\end{algorithmic}
%\end{small}
\end{algorithm}


\section{Implemented Algorithms}

We shortly describe the algorithms that are implemented in \pgsolver. The aim is
not to give a complete description of these algorithms.
For details and an account of the theory behind them please follow the literature.
We just give an idea of how these algorithms work, in order to be able to name
implementation details and the optimisations that are carried out.
%  Note that
% they are all used as a backend to the universal solver only, i.e.\ they always
% get given a parity game consisting of a single strongly connected component with
% at least two different priorities and proper choices by both of the players.

For the terms describing the asymptotic time and space complexities of these
algorithms we introduce the convention that $n$ denotes the number of nodes
in a game, $e$ denotes the number of edges, and $d$ denotes the number of
priorities.

\subsection{The Recursive Algorithm}

This algorithm falls out of the constructive determinacy proof for parity games
due to Zielonka. It decomposes the game at hand to smaller ones recursively by
simultaneous induction on the number of priorities and the number of nodes in the
game. In the base cases, if the game only has one node or one priority, the winner
and corresponding strategy can easily be obtained, in the latter case as a random
strategy for example. In the other cases a winning strategy can be assembled out of
strategies for smaller subgames and an attractor strategy for one of the players
reaching the set of nodes with maximal priority in the game.
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Recursive Algorithm)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & W.~Zielonka\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{TCS::Zielonka1998} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Decomposition into subgames with recursion on number
                              of nodes and priorities \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot n^d)$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(e \cdot n)$ \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

% \subsection{The Recursive Preservation Algorithm}
% This algorithm is inspired by the recursive algorithm due to Zielonka. First it determines the set of nodes
% with greatest priority in the game. It removes the attractor of this set and determines recursively the
% winning sets of the remaining game. But instead of throwing away the winning set of one of the players we
% rescue as many dominions as possible by determining on which nodes the player still wins. This does not affect
% the worst-case complexities compared to the original recursive algorithm.
% \begin{center}
  % \begin{tabular}{|l|p{8cm}|}
    % \hline
    % \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Recursive Preservation Algorithm)} \\ \hline\hline
    % \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & O.~Friedmann\\ \hline
% %    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{TCS::Zielonka1998} \\ \hline
    % \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Decomposition into subgames with recursion on number
                              % of nodes and priorities by trying to keep as much computed information as possible\\ \hline
    % \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot n^d)$ \\ \hline
    % \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(e \cdot n)$ \\ \hline
% %    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  % \end{tabular}
% \end{center}

\subsection{The Local Model Checking Algorithm}

The only algorithm solving parity games locally in our collection is the $\mu$-calculus model checker
due to Stevens and Stirling. Since a parity game can be regarded as the product of an unknown transition
system and an unknown $\mu$-calculus formula (even though there may not exists such factors), this algorithm
can also be used to solve parity games. It basically explores a game depth-first and whenever it reaches a
cycle it stops, storing the node starting the cycle along with a cycle progress measure as an \emph{assumption}
for the cycle-winning player. Then, the exploration is backtracked in the sense that if the losing player could
have made other moves they are again explored depth-first. If this leads to a cycle-win for the other player, the
whole process starts again, now with respect to the other player. Whenever the backtracking finally leads to the
starting node of a cycle the node is registered as a \emph{decision} for the player which basically can be seen
as being a preliminary winning node for the respective player. Additionally, if there are assumptions of the other
player for the respective node, these assumptions are dropped, and all depending assumptions and decisions are
invalidated.
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Model Checking Algorithm)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & P.~Stevens and C.~Stirling\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{StevensStirling98} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Exploring the game depth-first, detecting cycles and backtracking subsequently for other possible moves \\ \hline
%     \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & not given in the literature  \\ \hline
%     \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & not given in the literature  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}
There are no sensible estimations on the worst-case time and space complexities of this algorithm in the
literature.

\subsection{The Strategy Improvement Algorithm}

The strategy improvement algorithm due to Jurdzi{\'n}ski and V\"oge picks a strategy for one of the two players, say for player 0, and computes a valuation of the strategy-induced subgame. This valuation is used to select a new strategy for player 0 by choosing transitions from player 0 choice points maximizing the valuation for the respective target node.

The process of valuating the current strategy and subsequently picking a new one is iterated until all transitions of the new strategy are not assigned better valuations than the transitions of the former strategy. The final valuation is then used to infer winning sets and winning strategies for both players.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Strategy Improvement Algorithm)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & M.~Jurdzi{\'n}ski and J.~V\"oge\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{conf/cav/VogeJ00,SchVog00} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an initialization strategy until it satisfies a winning-strategy-predicate \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(2^e \cdot n \cdot e)$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2 + n \cdot \log d + e)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{The Optimal Strategy Improvement Method}
This strategy improvement algorithm due to Schewe guarantees to select, in each improvement step, an optimal combination of local strategy modifications. The estimation produced in each iteration step are used to infer winning regions along with winning strategies for both players.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Optimal Strategy Improvement Method)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & S. Schewe\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{conf/csl/Schewe08} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an estimation until a fixed point is reached \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot (\frac{n+d}{d})^d \cdot \log(\frac{n+d}{d}))$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{The Strategy Improvement by Reduction to Discounted Payoff Games}
This algorithm translates the given parity game to a corresponding discounted payoff games and solves the latter
by Puri's algorithm. The winning regions as well as winning strategies for the discounted payoff games directly
correspond to those of the original parity game.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Strategy Improvement for DPGs)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & A.~Puri\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{purithesis} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an estimation until a fixed point is reached \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot (\frac{n+d}{d})^d \cdot \log(\frac{n+d}{d}))$ \\ \hline
    %\rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{Probabilistic Strategy Improvement}
A probabilistic strategy iteration method that has an subexponential upper bound on the number of expected iterations
that are required to solve the game.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Probabilistic Strategy Improvement)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & H.~Bj{\"o}rklund and S.~Vorobyov \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{BjoerklundVorobyov/2007} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an estimation until a fixed point is reached \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot (\frac{n+d}{d})^d \cdot \log(\frac{n+d}{d}))$ \\ \hline
    %\rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{Probabilistic Strategy Improvement 2}
Another probabilistic strategy iteration method that has an subexponential upper bound on the number of expected iterations
that are required to solve the game.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Probabilistic Strategy Improvement 2)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & H.~Bj{\"o}rklund, S.~Sandberg and S.~Vorobyov \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{DBLP:conf/stacs/BjorklundSV03} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an estimation until a fixed point is reached \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot (\frac{n+d}{d})^d \cdot \log(\frac{n+d}{d}))$ \\ \hline
    %\rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{Local Strategy Improvement}
A local version of the strategy iteration paradigm.

\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Local Strategy Improvement)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & O.~Friedmann and M.~Lange \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{fl-gandalf10,FriedmannL:JFCS:2011} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively improves an estimation until a fixed point is reached \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e \cdot (\frac{n+d}{d})^d \cdot \log(\frac{n+d}{d}))$ \\ \hline
    %\rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^2)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{The Small Progress Measures Algorithm}

The existence of a winning strategy for either of the players can be characterised be a condition
that is local to the nodes of the parity game. Each node carries a tuple of values, and those values
have to be larger than one or all of the values in successor nodes depending on the own of the node
and its priority. It can be shown that the values in a node can be bounded by a number depending on the
nodes reachable from the one at hand.

Jurdzi{\'n}ski suggest to find these values iteratively starting with 0 everywhere and increasing
them whereever necessary to respect their relation. Non-existence of a winning strategy on certain parts
of the game is found when the iteration tries to increase values beyond their pre-computed maxima.
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Small Progress Measures Algorithm)}
                    \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & M.~Jurdzi{\'n}ski \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{Jurdzinski/00} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Iteratively increase lexicographically ordered tuples
                              \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(d\cdot e \cdot (\frac{n}{d})^{d/2})$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(d\cdot n \cdot \log n)$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & tight computation of maximal values \\ \hline
  \end{tabular}
\end{center}
The performance of the algorithm depends very much on a good approximation of the maximal values in
these tuples since this directly affects the maximal running time. Suppose this algorithm is used to
find a winning strategy for player $0$. Then each tuple contains a field for every odd priority that
occurs in the game. The maximal value in the field $p$ of the tuple at node $v$ for some odd priority $p$
is 1 plus the number of nodes of priority $p$ in the same SCC as $v$ that are reachable from $v$ without
passing through a node with a priority higher than $p$.

Our implementation performs two iterations -- one for each player. Note that the original
exposition of the small progress measures algorithm allows to compute the winning region and a strategy
for one of the players. Clearly, the winning region for the other player can easily be inferred from this
but his/her strategy cannot. One possibility would be to rerun the algorithm on the winning region for the
other player measuring the progress for him/her. Another possibility is to iterate the
progress measures for both players in all nodes right away from the beginning.

\subsection{The Small Progress Measures Reduction to SAT}

Since solving parity games is known to be in NP there must be a polynomial reduction to SAT, the
satisfiability problem for propositional logic. Such a reduction is basically given by the small
progress measures algorithm. Instead of iteratively computing values one lets a SAT solver find
them. Since these values can be bounded, there is a finite number of bits representing these numbers,
and these bits can be seen as propositional variables for the SAT solver. The necessary relations
between the numbers are only less-than and less-than-or-equals, and it is not difficult to write down
propositional formulas which describe such relations between natural numbers in terms of constraints
on their bits.

Again, this is supposed to be a \emph{global} parity game solver. Hence, it has to report winning
regions and strategies for both players. As said above, Jurdzi{\'n}ski's original characterisation of these
in terms of small progress measures caters for one of the players only, and therefore needs space linear in
$\lceil\frac{d}{2}\rceil$. In order to obtain strategies for both players one has to use the algorithm
twice. The same holds for the symbolic execution by a reduction to SAT. The obvious choice here is to
simply create constraints for two sets of progress measures. But then the reduction technically does not
map into the satisfiability problem for propositional logic because the resulting formula is always
satisfiable, and a satisfying variable assignment encodes both the partition into winning regions and the
positional strategies.
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Small Progress Measures Reduction to SAT)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & M.~Lange \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{lange-gdv05} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Symbolic encoding in propositional logic of the
                               small progress measure algorithm \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(e\cdot d)$ + running time of the SAT solver \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(e\cdot d)$ + space needed by the SAT solver  \\ \hline
  \end{tabular}
\end{center}
% The SAT solver that is currently used is the library version of \textsc{zChaff} \cite{Moskewicz2001a},
% a high-performance and state-of-the-art program for that purpose.

\subsection{The Direct Reduction to SAT}

This formalises in propositional logics the existing of strategies and requires them to be winning
by checking that every cycle which is reached by following the strategy for one of the players sees
has a greatest priority which is good for that player. As with the previous reduction, the resulting
formula is always satisfiable.
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Direct Reduction to SAT)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & O.~Friedmann \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{lange-gdv05} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Symbolic encoding in propositional logic of the
                               a direct predicate \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $\mathcal{O}(n^3)$ + running time of the SAT solver \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(n^3)$ + space needed by the SAT solver  \\ \hline
  \end{tabular}
\end{center}

\subsection{The Dominion Decomposition Algorithm}
This algorithm is the first deterministic subexponential algorithm for parity games. It basically refines
the recursive algorithm due to Zielonka. First, one searches for small ($\leq \sqrt{2 \cdot n}$) dominions
by simply building each small subset, checking whether it is closed w.r.t.\ player 0 or player 1 and if so
checking whether the closed small subset is an $i$-dominion by using the original recursive algorithm. If
it actually is a dominion the attractor is built and the complement subgame recursively solved. If there is
no small dominion the algorithm switches to the original recursive algorithm (and searches for smaller
dominions in subsequent recursive calls).
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Dominion Decomposition Algorithm)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & M.~Jurdzi{\'n}ski, M. Paterson, and U. Zwick\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{JPZ06} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Brute-force search for small dominions with subsequent decomposition into subgames with recursion on number of nodes and priorities \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $n^{O(\sqrt{n})}$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}(e \cdot n)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

\subsection{The Big-Step Algorithm}
This algorithm refines the dominion decomposition algorithm by replacing the brute-force search for small
dominions by a restricted run of the small progress measures algorithm. Basically, the sum of the entries
in a small progress measure is limited by the size of the largest dominion that is searched for
($\sqrt[3]{n^2 \cdot d}$). Hence, the small progress measures algorithm identifies all dominions less or
equal to that limit. If there is no small dominion the algorithm switches to the original recursive algorithm
(and searches for smaller dominions in subsequent recursive calls).
\begin{center}
  \begin{tabular}{|l|p{8cm}|}
    \hline
    \multicolumn{2}{l}{\rule[-3mm]{0mm}{8mm}\quad \bfseries Algorithm \nextalg\ (Big-Step Algorithm)} \\ \hline\hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Author(s)} & S. Schewe\\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Literature} & \cite{Schewe/07/Parity} \\ \hline
    \rule[-8mm]{0mm}{13mm}{\bfseries Short description} & Small progress measures-based search for small dominions with subsequent decomposition into subgames with recursion on number of nodes and priorities \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Time complexity} & $O(e \cdot n^{\frac{1}{3}d})$ \\ \hline
    \rule[-3mm]{0mm}{8mm}{\bfseries Space complexity} & $\mathcal{O}((e + d \cdot \log n) \cdot n)$  \\ \hline
%    \rule[-3mm]{0mm}{8mm}{\bfseries Optimisations} & none \\ \hline
  \end{tabular}
\end{center}

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PROBLEM 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Problem 1}

Compute the atomic densities of $^{235}$U, $^{238}$U, and O in UO$_2$ when its density is 10.41 g/cm$^3$ and the uranium is enriched to 5 wt\% in $^{235}$U.


\section{Moving average models}


%!TEX root = ../thesis.tex
\chapter{Stabilising Training}\label{stabilising-training}

\paragraph{Experience Replay}

Experience replay~\cite{lin1992experiencereplay} aims to address the problem that in vanilla \DQL, subsequent update steps are correlated but theoretical guarantees hold only for i.i.d. training samples -- e.g.\  in our case, after we update the weights of the DQN around a load vector with $x$ balls, we next update it around a load vector with $x+1$ balls. This correlation stems from updating in the same order as executing the actions in the game. With experience replay, instead of calling the update rule~\eqref{eq:deep-q-learning-update-with-semi-gradient} on the current $(s, a, s', r)$ tuple, we store this tuple in an experience replay buffer. Then, after every fixed number of steps, a batch of tuples are sampled uniformly at random from the buffer, and the DQN is updated according to those tuples. Another benefit is the reuse of samples, leading to a more efficient learning. Nevertheless, there should be a size limit on the buffer to get rid of outdated samples, which I implemented using a deque data structure with a limited memory capacity. Finally, note that sampling a batch of tuples rather than a single sample when updating has been shown to have a stabilising effect~\cite{qian2020batchingsgd}.

\paragraph{Target Network}

The main difference between Q-Learning and \DQL is that in the former, during one step we only update a single value in the Q-table, while in the latter, due to the function approximation, many neighbouring values are also affected. This becomes problematic, since looking at the update rule~\eqref{eq:deep-q-learning-update-with-semi-gradient} for \DQL, to update around a Q-value $Q_{\mathbf{w_t}}(s_t, a_t)$, a neighbouring Q-value $Q_{\mathbf{w_t}}(s_{t+1}, a')$ is used. This can lead to a blow-up in the Q-values, intuitively, as a chain reaction. Target networks~\cite{fan2020target} have two copies of the same network architecture: one whose weights are updated by the update rule, and one that provides $Q_{\mathbf{w_t}}(s_{t+1}, a')$ of the ``target'' part $r_{t+1}+ \max_{a'} Q_{\mathbf{w'_t}}(s_{t+1}, a')$ of the rule. This weights $w'$ of the target network are updated to that of the main network periodically, every fixed number of training episodes. Another way to think about this is as a way to achieve the stability of~\eqref{eq:deep-q-learning-update-with-semi-gradient}, while still using the full gradient descent update rule.

\paragraph{Using a Different Optimiser}

Expanding on the update rule~\eqref{eq:deep-q-learning-update-with-semi-gradient}, which updates the weights (based on the gradient) using simple Stochastic Gradient Descent (SGD), SGD could be replaced by more sophisticated optimisers taking into moving averages and variances of the gradient (e.g.\ ADAM~\cite{kingma2015adamoptimiser}. I also added the widely used gradient clipping \cite{mikolov2012gradientclippingoriginal} technique which does not allow absolute gradient values to exceed $1$, hence prevents exploding gradients and speeding up training~\cite{zhang2020gradientclippingaccelerate}.


\clearpage
\subsection{C For Loop} % (fold)
\label{sub:c_for_loop}

The \nameref{sub:for_loop} in C can do much more than just counting, but that is its primary purpose. You can use this to implement the logic to process each element of an array.

The for loop itself is controlled by three aspects. The first is an initialiser, it sets the first value for the control variable (usually \texttt{i} if you are  using it to index an array). The second part is the condition, the body will run \emph{while} this is true just like a \nameref{sub:c_while_loop}. The third part is a post loop increment, you use this to move the index to the next value.

The standard for loop is: \csnipet{for(i = 0; i < size; i++)\{...\}}. This can be read as `for \texttt{i} starts at 0, while \texttt{i} is less than \texttt{size}, do the following then increment \texttt{i}'. If \texttt{size} is three then this counts 0, 1, 2. Repeating the body of the loop three times.

\csyntax{csynt:looping-for-loop}{a For Loop}{looping/for-loop}

\csection{\ccode{clst:test-for}{Code illustrating the for loop in C}{code/c/array/test-for.c}}

\mynote{
\begin{itemize}
  \item This is the C syntax for implementing a \nameref{sub:for_loop}.
  \item The \emph{initialiser} of the for loop is used to set the initial values before the loop body is first executed.
  \item The \emph{control} is a \emph{condition}, the loop executes \emph{while}\footnote{In the same way a while loop does, checking the condition to determine if the body should execute or be skipped when the condition is checked.} this is true.
  \item The \emph{increment} is a post loop action, used to move to the next index.
\end{itemize}
}

% subsection c_while_loop (end)


% -*- mode:LaTex; mode:visual-line; mode:flyspell; fill-column:75-*-
\chapter{Background} \label{chap:background}

As eluded to in Chapter~\ref{chap:introduction}, SLAM systems that run on real robots can be crudely divided into \emph{front-end} and \emph{back-end}. This chapter is a primer on algorithms and methods used in the \emph{front-end} and \emph{back-end}.

\section{The Front End}

The robotic paradigm describes the relationship of an agent interacting with its environment through the \emph{sense-plan-act} control loop. SLAM being in the sense paradigm, deals with processing the raw environment sensor readings to a structured representation that can be used in downstream tasks.

The front end deals with data preparation, modeling and data association such that it is amenable to the underlying optimization. More often than not, the choice of front-end is sensor dependent. For instance, many visual SLAM systems that utilize a monocular or stereo camera resort to \emph{feature based}~\cite{qinVINSMonoRobustVersatile2018, loiannoVisualInertialOdometry2016} sparse representations, while RGBD sensor based systems resort to \emph{direct methods}~\cite{newcombeKinectFusionRealtimeDense2011, whelanElasticFusionDenseSLAM2015} that utilize all the pixels in the input frame.

This distinction in the choice of front-end often influences the choice of map representation in a SLAM system. A popular choice with visual SLAM system is to use an \emph{explicit} map representation as a set of 3D points~\cite{mur-artalORBSLAM2OpenSourceSLAM2017} that is \emph{triangulated} from multiple 2D image feature correspondences. On the other hand dense front-end systems rely on \emph{surfel} representations or on volumetric representations such as occupancy grids. More recently, after the seminal work \cite{newcombeKinectFusionRealtimeDense2011}, \emph{implicit} map representations have gained popularity as a candidate representation for RGBD SLAM systems.

In this work, since the focus is on semantic high level object landmarks each represented with implicit representations such as Truncated Signed Distance Function (TSDF) grids, the next section provides a short introduction of this representation.

\subsection{Implicit map representation through TSDFs}

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.5\linewidth]{figs/metric-space-grid.png}
    \caption{An example of metric space in 3D discretized on a computer as a voxel grid.}%
    \label{fig:voxel-grid}
\end{figure}

\emph{Implicit} map representations are fundamentally different from \emph{explicit} map representations. As opposed to point clouds or landmark collections, an implicit map refers to one where the environment is defined through a metric space $\Xv$ (see Figure~\ref{fig:voxel-grid}) and structures in this space are described through an \emph{implicit} function $f : \Xv \rightarrow \Rb$. One example of an implicit function is the log-odds occupancy, resulting directly in occupancy grids \cite{thrunProbabilisticRoboticsIntelligent2005} and \emph{octree} \cite{hornungOctoMapEfficientProbabilistic2013} representations. Another is the signed distance function (SDF) which is relevant for this thesis. Formally, it is defined as follows:

In a metric space $\Xv$ defined with distance $d$, if the surface defined by $\partial \Omega$ demarcates this space into subsets  $\Omegav$ and  $\Omegav^c$, the signed distance function $f$ is defined by
\begin{align}
   f = \begin{cases}
       \phantom{-}d(\xv, \partial \Omegav), &\text{ if } \xv \in \Omegav\\
       -d(\xv, \partial \Omegav), &\text{ if } \xv \in \Omegav^c
   \end{cases}
\end{align}
In particular, for the 3D environment, we have that $\Xv = \Rb^3$ with $d = \|\cdot, \cdot \|_2$, and $\partial \Omega$ corresponds to the zero level set of the signed distance function and represents the surface of objects as a topological space. In simple words, the signed distance function returns the shortest distance of a query point in Euclidean space to its closest point on the surface boundary (see Figure~\ref{fig:2d-sdf}).

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.5\linewidth]{figs/2d-sdf.png}
    \caption{An illustration of Signed Distance Function for a 2D metric space discretized into pixels (query coordinates '+') (Courtesy: Wikipedia).}%
    \label{fig:2d-sdf}
\end{figure}

\subsubsection{TSDF integration}

A TSDF representation requires a metric volume by design, which is implemented as a volumetric grid $V$, arbitrarily initialised in the world frame $\Tv_W = \Iv \in SE(3)$. Consider a single RGBD frame measurement $\langle \Ic, \Dc \rangle$ that consists of a color ($\Ic$) and depth ($\Dc$) image, at a relative camera pose $\Tv_C^W  = \begin{bmatrix}
    \Rv_C^W & \tv_C^W \\ 0 & 1
\end{bmatrix}\in SE(3)$ with camera intrinsics $\Kv$. To integrate the measurement into the TSDF volume, we project  all the query points $q \in \Rb^3$ of the volume $V$ into the current image as follows:
\begin{align}
    \dot{q}^\prime = (\Tv_C^W)^{-1} \dot{q} \\
    \dot{p} = \lfloor \Kv q^\prime\rfloor
\end{align}
where $\dot{q} = \begin{bmatrix}
    q^\top & 1
\end{bmatrix}^\top$  and similarly $\dot{p}$ denote the homogeneous coordinates in $\Rb^2$ and $\Rb^3$ respectively. $p \in \Zb^2$ is the associated 2D pixel coordinate in the image and $\lfloor \cdot \rfloor$ is the floor function and finds the greatest integer pixel coordinate less than the pixel value in the image. For every matched pixel coordinate, we backproject the associated measurement i.e., the depth value of the pixel $\Dc(p) \in \Rb$ into the volume to obtain a \emph{noisy} 3D surface in the metric space (see Figure~\ref{fig:tsdf-single-image}). This noisy surface is used as the proxy surface against which all query coordinates ($q \in \Zb^3$) in the volumetric grid are evaluated against to obtain their corresponding SDF values as follows:

\begin{align}
    \text{SDF}(q) =  \norm{ \Dc(p) - \frac{1}{\lambda} \|\tv_C^W - q\|_2 }_2
\end{align}

 where $\| \tv_C^W - q \|_2$ is the distance between the query point and the camera center along the camera optical axis, and $\lambda = K^{-1} p$ defines the ray direction for the pixel $p$. This effectively calculates the shortest distance of the query coordinate to the surface.

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.6\linewidth]{figs/tsdf-integration-single}
    \caption{Back-projection of associated pixels into the 3D volumetric grid to obtain a noisy surface.}%
    \label{fig:tsdf-single-image}
\end{figure}

This SDF value is truncated such that only a band of SDF values ($+\mu$ to $-\mu$) around the measured surface are computed, to avoid computation of all the query coordinates in the volume.
Similarly, colors from the image can be associated to the query coordinates by unprojection uniquely since a depth image is available.

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.5\linewidth]{figs/tsdf-multiple-images.png}
    \caption{On weighted averaging of TSDF values obtained from multiple RGBD measurements, in the limit we converge to TSDF values of the underlying surface.}%
    \label{fig:tsdf-multiple-images}
\end{figure}

Now, when we have multiple RGBD frame measurements $\{\langle \Ic^{(i)}, \Dc^{(i)} \rangle\}_{i=1}^{N}$ with their respective poses $\{ T_{C^{(i)}}^W \}_{i=1}^N$, we can compute TSDF measurements from each individual frame. The global fusion of all these TSDF measurements is then done through weighted average of the TSDF measurements for each query coordinate $q \in \Rb^3$ over the camera frame sequence as follows:
\begin{align}
    d &= \phi(\text{SDF}(q)) \\
    \text{TSDF}_k(q) &= \frac{\text{TSDF}_{k-1}(q) + d}{W(q) + 1} \\
    W(q) &= W(q) + 1
\end{align}
where $\phi$ truncates the $\text{SDF}$ computed for the frame being integrated, and $W(q)$ corresponds to the maintained weight for every voxel coordinate in the volume grid. This integration process intuitively results in evaluation of the SDF values for the query coordinates with the expected surface (mean of the TSDFs) from all the incorporated RGBD measurements (see Figure~\ref{fig:tsdf-multiple-images}).

In general, the integration process can also be applied to the color values, where the colors are averages elementwise, with the incoming stream of images.

\subsubsection{TSDF Rendering}

TSDF Rendering is the process of generating a virtual depth and color image given a camera pose $\Tv_C^W$ and TSDF volume grid $V$. The previous subsection assumed access to camera poses for global TSDF fusion, but in an actual SLAM setting camera poses are computed in the loop typically through iterative closest point (ICP)~\cite{rusinkiewiczEfficientVariantsICP2001} registration. TSDF Rendering is the intermediate step that generates a virtual image that the incoming camera frame is registered against (also called \emph{frame to model} registration~\cite{newcombeKinectFusionRealtimeDense2011}). (details in \S\ref{subsec: tracking})

Rendering is accomplished by marching a ray per pixel of the virtual frame into the global TSDF volume which encodes surfaces as the zero level set (zero crossing). For a given pixel $p \in \Zb^2$, once again the camera ray can be computed as
\begin{align*}
    \rv = \Tv_C^W \Kv^{-1} \dot{p}.
\end{align*}
\noindent We then march along the ray from the minimum depth supported by the camera (usually assumed to be $0.1$m) to a max depth value defined by the supported camera range (around 5m to 8m for Microsoft Kinect), or until we find a zero crossing. Typically this process is sped up by skipping marching along the ray, and using a step size $< \mu$, the truncation threshold. Once a zero crossing has been found, the depth value can be further refined by trilinear interpolation. (details in~\cite{newcombeKinectFusionRealtimeDense2011})

\subsection{TSDF Limitations}

Based on the discussion of TSDF for map representation in SLAM, the following limitations might be apparent to the keen reader.
\begin{enumerate}
    \item \textbf{TSDF representation is memory intensive:} A naive TSDF representation, utilizes a 3D volumetric grid to represent the scene -- typically  a voxel grid of resolution $512^3$ covering a bounded volume of about $4m^3$. This makes the entire system very memory intensive. Later works such as Voxel Hashing~\cite{niessnerRealtime3DReconstruction2013} and OctoMap \cite{hornungOctoMapEfficientProbabilistic2013} have tried to ameliorate this issue. In our implementation, we utilize this spatial hashing based TSDF implementation based on \cite{dongGPUAcceleratedRobust2019} to achieve memory efficiency.
    \item \textbf{TSDFs are computationally intensive:} During integration of an incoming frame, all pixels that are projectively associated to the 3D query coordinates are updated requiring about $640\times 480 = 307200$ operations for a VGA resolution image. Similarly, during TSDF rendering, each pixel to be rendered requires at most $\frac{\|d_{\max} - d_{\min}\|_2}{\mu}$ evaluations, where $\|d_{\max} - d_{\min}\|_2$ is the maximum ray length, and $\mu$ is the TSDF truncation width. Therefore, ray-casting is typically the most computationally intensive step in any dense RGBD SLAM system. Observing that each pixel is updated independent of others, a GPU can (and is) used to parallelize all TSDF operations, to obtain realtime operation.

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.4\linewidth]{figs/chair-landmark.png}
    \caption{An example of dense high level landmark in our SLAM system.}%
    \label{fig:chair-landmark}
\end{figure}

    \item \textbf{Cannot easily smooth noisy pose estimates:} It is not easy to correct noisy pose estimates with implicit models, and therefore most early systems employed only a filtering based integration \cite{newcombeKinectFusionRealtimeDense2011}. This is because to correct for a noisy pose estimate in retrospect requires the expensive operation of subtracting the weighted TSDF values in the global volume from the appropriate coordinates and re-integrating the volume with corrected TSDF values \cite{daiBundleFusionRealtimeGlobally2017}. \cite{whelanKintinuousSpatiallyExtended} used submaps to subvert this problem, however with large TSDF volumes as submaps there are still locally noisy measurements that are not corrected for. In our method, since we use a TSDF representation only for landmark objects, we reap the benefits of explicit representation, where the whole landmark pose can be optimized, but each landmark provides a dense and appealing representation. (see Figure~\ref{fig:chair-landmark})
\end{enumerate}

\section{The Back End}

The back end is responsible for generating a reasonable explanation in the state variables given the abstract sensor measurements and data associations from the front end. As opposed to filtering based methods such as Kalman Filter, more recently smoothing formulations based on matrix factorization have provided exact solutions in the linear case, and approximate but good solutions in non-linear case by adding the entire trajectory into the optimization problem while simplifying the solution~\cite{kaessIncrementalSmoothingMapping}. In this section, I review the batch smoothing formulation that is quite common in graph based SLAM back-ends today~\cite{grisettiTutorialGraphBasedSLAM, dellaertFactorGraphsRobot2017}.

\subsection{SLAM and Non-Linear Least Squares}

\begin{figure}[htpb]
    \centering
    \includegraphics[width=\linewidth]{figs/factor-graph-background}
    \caption{(a) A factor graph typical for SLAM and (b) the explicit Bayes net for the factor graph in (a). Note the Markovian conditional independences between measurements, given the state variables.}%
    \label{fig:factor-graph}
\end{figure}

As introduced in Chapter \ref{chap:introduction}, by treating the robot states and landmarks as random variables given measurements, the SLAM problem can be treated as the maximum a posteriori (MAP) estimation of robot and landmark states. Now, consider a collection of robot state variables $\Xc \triangleq \{ \xv_t \}_{t=1}^{T}$, and a collection of landmarks $\Lc \triangleq \{ \ell_m \}_{m=1}^{M}$, and set of measurements $\Zc \triangleq \{\{\zv_n \}_{n=1}^{N}, \{\zv_t\}_{t=1}^{T}\}$ including both landmark and odometry measurements respectively, then the MAP estimation is given as
\begin{align}
    \Xc^*, \Lc^* = \argmax_{\Xc, \Lc} p(\Xc, \Lc | \Zc) = \argmax_{\Xc, \Lc} p(\Zc | \Xc, \Lc) p(\Xc, \Lc) \label{eq:map}
\end{align}
While MAP estimation is known to be NP-Hard in general, by assuming reasonable conditional independences between the associated variables (see Figure~\ref{fig:factor-graph}), i.e., by making the markovian assumption on the state variables, and observing that measurements are local, we can write the joint probability in \ref{eq:map} as follows:
\begin{align}
    p(\Xc, \Lc, \Zc) &= p(\xv_0) \prod_{n=1}^{N} p(\zv_n | \xv_{\alpha_n}, \ell_{\beta_n}) \prod_{t=1}^{T} p(\zv_t | \xv_{t-1}, \xv_{t})\nonumber\\
    \log p(\Xc, \Lc, \Zc) &= \log p(\xv_0) + \sum_{n=1}^{N} \log p(\zv_n | \xv_{\alpha_n}, \ell_{\beta_n}) + \sum_{t=1}^{T} \log p(\zv_t | \xv_{t-1}, \xv_{t})
\end{align}
Note, that the above problem assumes known \emph{data association} between measurements $\zv_n$ of landmark $\ell_{\beta_n}$ at a robot pose $\xv_{\alpha_n}$as $\Dc := \{(\alpha_n, \beta_n)\}_{n=1}^{N}$ \cite{bowmanProbabilisticDataAssociation2017}.
Now as is typical in SLAM, we assume Gaussian measurement noise in each of the measurements i.e.,
\begin{align}
    \zv_0 &= \xv_0 + \nuv_0\\
    \zv_n &= h_n(\xv_{\alpha_n}, \ell_{\beta_n}) + \nuv_n\\
    \zv_t &= h_t(\xv_{t-1}, \xv_t) ~+ \nuv_t
\end{align}
Where $\nuv_n$ and $\nuv_t$ are the intrinsic noise in the respective measurements, each distributed as zero mean noise with respective covariances $\Sigma_n$ and $\Sigma_t$. Also note that we added a phantom measurement $\zv_0$ to constrain the first pose with an empirically chosen small covariance $\Sigma_0$. On writing down the probability distributions for each of the Gaussian noise terms we have the following:
\begin{align}
    p(\xv_0) &\propto \exp\{ \|\zv_0 - \xv_0\|^2_{\Sigma_0}\}\\
    p(\zv_n | \xv_{\alpha_n}, \ell_{\beta_n}) &\propto \exp\{ \|\zv_n - h_n(\xv_{\alpha_n}, \ell_{\beta_n})\|^2_{\Sigma_n}\}\\
    p(\zv_t | \xv_{t-1}, \ell_{t}) &\propto \exp\{ \|\zv_t - h_n(\xv_{t-1}, \ell_{t})\|^2_{\Sigma_t}\}
\end{align}
Observe that
\begin{align}
    \Xc^*, \Lc^* &= \argmax_{\Xc, \Lc} p(\Xc, \Lc, \Zc) = \argmin_{\Xc, \Lc} - \log p(\Xc, \Lc, \Zc)
\end{align}
Now writing the joint probability using the factorized measurement distributions we have
\begin{align*}
    \Xc^*, \Lc^* = \argmin_{\Xc, \Lc} \Bigg \{ \| \zv_0 - \xv_0 \|_{\Sigma_0}^2 + &\sum_{n=1}^{N} \| \zv_n - h_n(\xv_{\alpha_n}, \ell_{\beta_n}) \|_{\Sigma_t}^2 + \\&\sum_{t=1}^{T}\| \zv_t - h_t(\xv_{t-1}, \xv_{t}) \|_{\Sigma_n}^2 \Bigg \}
\end{align*}
If we generalize the different types of measurements in the system as $h_i(\Xv_i)$, and subsume the landmarks and state variables into a single state vector $\Xc$ then we may write the above as:
\begin{align}
    \Xc^* = \argmin_{\Xc} \Bigg \{ &\sum_{i=1}^{N} \| \zv_i - h_i(\Xv_i) \|_{\Sigma_i}^2 \Bigg \} \label{eq:least-squares}
\end{align}
The above minimization is a non-linear least squares optimization which is typically solved by iteratively linearizing the measurement models and solving a series of linear approximations to the problem to approach a local minima solution for landmark and robot latent state variables.

\subsection{Iterative Linearization and Gauss Newton}

Consider a single non-linear measurement $h_i( \cdot )$, then by Taylor's expansion, we can linearize the measurement at a linearization point $\Xv_i^{(0)}$ as follows (see Figure~\ref{fig:taylor}):
\begin{align}
    h_i(\Xv_i) \approx h_i(\Xv_i^{(0)}) + \frac{\partial h_i}{\partial \Xv_i} \Bigg |_{\Xv_i^{(0)}} (\Xv_i - \Xv_i^{(0)})
\end{align}
If we define the jacobian as $\Jv_i \triangleq \frac{\partial h_i}{\partial \Xv_i} \Big|_{\Xv_i^{(0)}}$, and $\Delta_i \triangleq (\Xv_i - \Xv_i^{(0)})$ and substitute in the general non-linear least squares objective \ref{eq:least-squares}:

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.5\linewidth]{figs/linearization}
    \caption{Illustration of linearization using Taylor's expansion.}
    \label{fig:taylor}
    \vspace{-1em}
\end{figure}
\begin{align}
    \Delta^* = \argmin_{\Delta}\sum_{i} \| \{\zv_i - h_i(X_i^{(0)})\} - \Jv_i \Delta_i \|_{\Sigma_i}^2
\end{align}
where $\qv_i \triangleq \zv_i - h_i(\Xv_i^{(0)})$ is the prediction error at the linearization point, and $\Delta^*$ is the solution to the locally linearized problem. Now we may write the least squares objective as:
\begin{align}
    \Delta^* &= \argmin_{\Delta}\sum_{i} \| \qv_i - \Jv_i \Delta_i \|_{\Sigma_i}^2 = \argmin_{\Delta}\sum_{i} (\qv_i - \Jv_i \Delta_i)^\top \Sigma_i^{-1} (\qv_i - \Jv_i \Delta_i)\\
             &=  \argmin_{\Delta}\sum_{i} \left\{\Sigma_i^{-1/2}(\qv_i - \Jv_i \Delta_i)\right\}^\top \left\{ \Sigma_i^{-1/2} (\qv_i - \Jv_i \Delta_i)\right\}\\
             &= \argmin_{\Delta}\sum_{i} \| \bv_i - \Av_i \Delta_i \|_2^2 = \argmin_{\Delta} \| \bv - \Av \Delta\|_2^2
\end{align}
By writing $\bv_i = \Sigma_i^{-1/2} \qv_i$ and $\Av_i = \Sigma_i^{-1/2} \Jv_i$ and then collecting them in the vector $\bv$ and matrix $\Av$ respectively, we obtain the standard least squares problem, that can then be solved using \emph{normal equations}.

The above solution gives us the local update $\Delta^*$ for a linearization point $\Xv^{(0)}$. To solve a non-linear problem, we solve for local updates iteratively as in Algorithm~\ref{gauss-newton}.

\input{algorithms/gauss-newton.tex}

A more detailed treatment of sophisticated non-linear optimization algorithms in the context of SLAM is provided in \cite{dellaertFactorGraphsRobot2017}.

\subsection{Discussion and Limitations}

We noted earlier that MAP inference on the factor graph for the SLAM setting is tractable due to the inherent local structure of the factor graph. This structure appears in the sparsity of the measurement Jacobian $\Av = \Sigma^{-1/2} \Jv$ with number of rows equal to the number of measurements in the graph (number of factors) and the number of columns equal to the number of state variables to be solved. The non-linear least squares optimization is solved efficiently using matrix factorization methods such as QR factorization and Cholesky factorization (equivalent to variable elimination on the factor graph~\cite{dellaertSquareRootSAM2006}). Further improvement in performance can be gained by reordering the measurements using methods such as COLAMD \cite{davisAlgorithm8xxCOLAMD} which consequently reduces fill in in the matrix during computation.

Nevertheless, for an online system with this formulation matrix factorization needs to be performed with a new graph when new measurements are added, resulting in wasted computation to resolve all the entries in the matrix. Later work has made progress on incremental solvers for SLAM, that are able to update the solution iteratively for non-linear systems and provide linearization point management, as well as careful updation of older variables based on the current estimate. As this thesis does not directly use incremental solvers, the reader is directed to \citet{kaessISAM2IncrementalSmoothing2012}

For implicit mapping representations, however, it is unclear how they are to be represented as landmark variables, and how updates to the landmark state variables are to be propagated in the map. In this thesis, we show that representing the map as a collection of high level objects, each represented with implicit representation and corresponding to a landmark variable in the optimization is a good strategy, and benefits the SLAM in multiple facets. Each object is represented with a spatially hashed TSDF volumetric grid \cite{prisacariuInfiniTAMV3Framework2017} \cite{niessnerRealtime3DReconstruction2013} \cite{dongGPUAcceleratedRobust2019} that is not updated in the back end optimization process. Instead, since the object is rigid, optimizing the pose of the base frame attached to it suffices.
\clearpage
% In theory implicit map representations provide a method for water tight  reconstructions that are visually appealing, and in general more detailed when compared to feature based methods (see Figure Figure~\ref{fig:}), however these methods typically do not employ smoothing based back ends, and therefore are unable to correct for noisy pose estimates in retrospect. This is because, it is not easy to make a correction to an integrated measurement in the TSDF representation. In particular, to correct a single pose estimate, the measurement that had been integrated into the global TSDF has to be removed by subtractingthe corresponding TSDF value from the affected 3D query coordinates, and then re-integrating the new measurement appropriately.


%	This is written by Zhiyang Ong as a template for writing algorithms in LaTeX.

%	The MIT License (MIT)

%	Copyright (c) <2014> <Zhiyang Ong>

%	Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

%	The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

%	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

%	Email address: echo "cukj -wb- 23wU4X5M589 TROJANS cqkH wiuz2y 0f Mw Stanford" | awk '{ sub("23wU4X5M589","F.d_c_b. ") sub("Stanford","d0mA1n"); print $5, $2, $8; for (i=1; i<=1; i++) print "6\b"; print $9, $7, $6 }' | sed y/kqcbuHwM62z/gnotrzadqmC/ | tr 'q' ' ' | tr -d [:cntrl:] | tr -d 'ir' | tr y "\n"

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Algorithms}
\label{chp:Algorithms}

A template for typesetting algorithms is shown in {\sc Procedure} \ref{lst:MyAlgorithm}.

%	\begin{codebox}
%	\Procname{$\proc{NAME OF THE ALGORITHM}({\it ARGUMENTS})$}
%	\label{lst:MyAlgorithm}
%	\zi \Comment {\it Input ARGUMENT \#1: Definition1}
%	\zi \Comment {\it Input ARGUMENT \#2: Definition2}
%	\li BODY OF THE PROCEDURE
%	\zi \Comment {\it A while loop.}
%	\li \While [condition]
%		\Do
%	\li	[Something]
%		\End
%	\zi \Comment {\it A for loop.}
%	\li \For \id{Var} $\gets$ [initial value] \To [final value]
%		\Do
%	\li	[Something]
%		\End
%	\zi \Comment {\it An if-elseif-else block.}
%	\li	\If $[$Condition1$]$
%		\Then
%	\li		Blah\dots
%	\li	\ElseIf $[$Condition2$]$
%		\Then
%	\li		Blah\dots
%	\li	\ElseIf $[$Condition3$]$
%		\Then
%	\li		Blah\dots
%	%	\li	\ElseNoIf $[$Condition$]$
%	%		\Then
%	%	\li		Blah\dots
%	\li	\Else
%	\li		Blah\dots
%		\End
%	\zi \Comment {\it A variable assignment.}
%	\li $\id{blah} \gets A[j]$
%	\zi	\>	\Comment {\it This is indented with a tab.}
%	\zi	\Comment {\it What is the output of this procedure?}
%	\li	\Return
%	\end{codebox}

%	\begin{algorithm}
%	\caption{Euclid?s algorithm}\label{euclid}
%	\begin{algorithmic}[1]
%	\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
%	\State $r\gets a\bmod b$
%	\While{$r\not=0$}\Comment{We have the answer if r is 0}
%	\State $a\gets b$
%	\State $b\gets r$
%	\State $r\gets a\bmod b$
%	\EndWhile\label{euclidendwhile}
%	\State \textbf{return} $b$\Comment{The gcd is b}
%	\EndProcedure
%	\end{algorithmic}
%	\end{algorithm}

\begin{algorithm}
\caption{The Bellman-Kalaba algorithm}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l?(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l?$
\State $l \leftarrow l?$
\Until{$\neg changed$}
\EndProcedure
\Statex
\Procedure {FindPathBK}{$v$, $u$, $p$}
\If {$v = u$}
\State \textbf{Write} $v$
\Else
\State $w \leftarrow v$
\While {$w \not= u$}
\State \textbf{Write} $w$
\State $w \leftarrow p(w)$
\EndWhile
\EndIf
\EndProcedure
\end{algorithmic}
\end{algorithm}


\documentclass{article}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, latexsym}

\usepackage{pgfplots}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{nicefrac}
\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}
\usetikzlibrary{shapes,positioning,intersections,quotes}
\usetikzlibrary{arrows.meta,
                bending,
                intersections,
                quotes,
                shapes.geometric}

\definecolor{darkgreen}{rgb}{0.0, 0.6, 0.0}
\definecolor{darkred}{rgb}{0.7, 0.0, 0.0}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\title{Week 6}
\begin{document}
\pagenumbering{gobble}
\maketitle
\newpage
\pagenumbering{arabic}

\section*{Classification}
\begin{itemize}
  \item Y can only have discrete values.
  \item For example: 0 = negative class (absence of something) and 1 = positive class (presence of something).
  \item Email $->$ spam/not spam?
  \item Online transactions $->$ fraudulent?
  \item Tumor $->$ Malignant/benign?
\end{itemize}

Let's go back to the cancer example from the Week 1 and try to apply linear regression:

~\\
\begin{tikzpicture}
  \begin{axis}[
      axis x line=middle,
      axis y line=middle,
      width=12cm, height=5cm,     % size of the image
      grid = none,
      grid style={dashed, gray!0},
      %xmode=log,log basis x=10,
      %ymode=log,log basis y=10,
      xmin=0,     % start the diagram at this x-coordinate
      xmax= 1,    % end   the diagram at this x-coordinate
      ymin=-0.1,     % start the diagram at this y-coordinate
      ymax= 2.5,   % end   the diagram at this y-coordinate
      %/pgfplots/xtick={0,1,...,60}, % make steps of length 5
      %extra x ticks={23},
      extra y ticks={0, 1},
      axis background/.style={fill=white},
      ylabel=Maligant?,
      xlabel=Tumor size,
      xticklabels={,,},
      yticklabels={,,},
      tick align=outside,
      tension=0.08]
    % plot the stirling-formulae
    \fill[blue] (5, 5) circle (5pt);
    \fill[blue] (20, 5) circle (5pt);
    \fill[blue] (30, 5) circle (5pt);
    \fill[blue] (50, 5) circle (5pt);
    \fill[blue] (60, 5) circle (5pt);
    \fill[red] (50, 20) circle (5pt);
    \fill[red] (45, 20) circle (5pt);
    \fill[red] (65, 20) circle (5pt);
    \fill[red] (75, 20) circle (5pt);
    \fill[red] (89, 20) circle (5pt);
    \addplot[domain=0:10, gray, ultra thick,samples=500] {2.5*x - 0.3};

  \end{axis}
  \node[above,font=\large\bfseries] at (current bounding box.north) {Define breast cancer as malignant or benign based on tumour size
  };
\end{tikzpicture}

~\\
We see that it wasn't the best idea. Of course, we could attempt another approach to find a straight line that would better separate the points, but a straight line isn't our sole choice. There are more appropriate functions for that job.

\section*{Hypothesis representation}
\begin{itemize}
  \item We want our classifier to output values between 0 and 1.
  \item For classification hypothesis representation we have: $h_{\theta}(x) = g((\theta^Tx))$.
  \item $g(z)$ is called the sigmoid function, or the logistic function.
        $$g(z) = \frac{1}{1 + e^{-z}}$$
  \item If we combine these equations we can write out the hypothesis as:
\end{itemize}

$$h_{\theta}(x) = \frac{1}{1+e^{-\theta Tx}}$$

~\\
\begin{tikzpicture}
  \begin{axis}[
      axis x line=middle,
      axis y line=middle,
      width=12cm, height=5cm,     % size of the image
      grid = none,
      grid style={dashed, gray!0},
      %xmode=log,log basis x=10,
      %ymode=log,log basis y=10,
      xmin= -8,     % start the diagram at this x-coordinate
      xmax= 8,    % end   the diagram at this x-coordinate
      ymin=-0.1,     % start the diagram at this y-coordinate
      ymax= 1.2,   % end   the diagram at this y-coordinate
      %/pgfplots/xtick={0,1,...,60}, % make steps of length 5
      %extra x ticks={23},
      extra y ticks={0, 1},
      axis background/.style={fill=white},
      ylabel=y,
      xlabel=x,
      xticklabels={,,},
      yticklabels={,,},
      tick align=outside,
      tension=0.08]
    % plot the stirling-formulae
    \addplot[domain=-8:8, gray, ultra thick,samples=500] {1/(1+exp(-x)};

  \end{axis}
  \node[above,font=\large\bfseries] at (current bounding box.north) {Sigmoid function
  };
\end{tikzpicture}

~\\
When our hypothesis $(h_{\theta}(x))$ outputs a number, we treat that value as the estimated probability that $y=1$ on input $x$.

$$h_{\theta}(x) = P(y=1|x\ ;\ \theta)$$

~\\
Example:\\

$h_{\theta}(x) = 0.7$ and

\begin{align*}
  X & = \begin{bmatrix}
    1          \\
    tumourSize
  \end{bmatrix}
\end{align*}

~\\
Informs a patient that a tumor has a $70\%$ likelihood of being malignant.

\section*{Decision boundary}

One way of using the sigmoid function is:

\begin{itemize}
  \item When the probability of y being 1 is greater than 0.5 then we can predict y = 1.
  \item Else we predict y = 0.
\end{itemize}

\includegraphics[width=0.5\textwidth]{resources/decision_boundary}

\begin{itemize}
  \item The hypothesis predicts $y = 1$ when $\theta^T  x >= 0$.
  \item When $\theta^T x <= 0$ then the hypothesis predicts y = 0.
\end{itemize}

~\\
Example

$$h_{\theta}(x) = g(\theta_0 + \theta_1x_1 + \theta_2x_2)$$

\begin{align*}
  \theta & = \begin{bmatrix}
    -3 \\
    1  \\
    1
  \end{bmatrix}
\end{align*}

~\\
We predict $y = 1$ if:

$$-3x_0 + 1x_1 + 1x_2 \geq 0$$
$$-3 + x_1 + x_2 \geq 0$$

~\\
As a result, the straight line equation is as follows:
$$x_2 = -x_1 + 3$$

~\\\\
\includegraphics[width=0.5\textwidth]{resources/linear_decision_boundary}

\begin{itemize}
  \item Blue = false
  \item Magenta = true
  \item Line = decision boundary
\end{itemize}

\section*{Non-linear decision boundaries}

Get logistic regression to fit a complex non-linear data set.

~\\
Example
$$h_{\theta}(x) = g(\theta_0 + \theta_1x_1 + \theta_3x_1^2 + \theta_4x_2^2)$$

\begin{align*}
  \theta & = \begin{bmatrix}
    -1 \\
    0  \\
    0  \\
    1  \\
    1
  \end{bmatrix}
\end{align*}

~\\
We predict $y = 1$ if:

$$-1 + x_1^2 + x_2^2 \geq 0$$
$$x_1^2 + x_2^2 \geq 1$$

~\\
As a result, the circle equation is as follows:
$$x_1^2 + x_2^2 = 1$$

~\\
This gives us a circle with a radius of 1 around 0.

~\\\\
\includegraphics[width=0.5\textwidth]{resources/non_linear_decision_boundary}

\section*{Cost function for logistic regression}

\begin{itemize}
  \item Fit $\theta$ parameters/
  \item Define the optimization object for the cost function we use the fit the parameters.
\end{itemize}

~\\
Training set of m training examples:
$$\{(x^{(1)}, y^{(1)}), (x^{(1)}, y^{(1)}), ..., (x^{(m)}, y^{(m)})\}$$

\begin{align*}
  x & = \begin{bmatrix}
    x_0 \\
    x_1 \\
    ... \\
    x_n
  \end{bmatrix}
\end{align*}

$$x_0 =1,\quad y \in \{0,1\}$$

~\\
Linear regression uses the following function to determine $\theta$:

$$J(\theta) = \frac{1}{2m} \sum_{m}^{i=1}(h_{\theta}(x^{(i)}) - y^{(i)})^2$$

~\\
We define "cost()" as:

$$cost(h_{\theta}(x^{(i)}), y^{(i)}) = \frac{1}{2} (h_{\theta}(x^{(i)}) - y^{(i)})^2$$

~\\
We can now redefine $J(\theta)$ as:

$$J(\theta) = \frac{1}{2} \sum_{m}^{i=1}cost(h_{\theta}(x^{(i)}), y^{(i)})$$

\begin{itemize}
  \item This is the cost you want the learning algorithm to pay if the outcome is $h_{\theta}(x)$ but the actual outcome is y.
  \item This function is a non-convex function for parameter optimization when used for logistic regression.
  \item If you take $h_{\theta}(x)$ and plug it into the Cost() function, and them plug the Cost() function into $J(\theta)$ and plot $J(\theta)$ we find many local optimum.
\end{itemize}

\[ cost(h_{\theta}(x), y) = \begin{cases}
    -log(h_{\theta}(x))     & if\ y=1 \\
    -log(1 - h_{\theta}(x)) & if\ y=0
  \end{cases}
\]

\section*{Multiclass classification problems}
Getting logistic regression for multiclass classification using one vs. all.

~\\
\includegraphics[width=0.8\textwidth]{resources/multiclass_classification}

~\\
Split the training set into three separate binary classification problems.
\begin{itemize}
  \item Triangle (1) vs crosses and squares (0) $h_{\theta}^{(1)}(x)$.
  \item Crosses (1) vs triangle and square (0) $h_{\theta}^{(2)}(x)$.
  \item Square (1) vs crosses and square (0) $h_{\theta}^{(3)}(x)$.
\end{itemize}

~\\
\includegraphics[width=0.5\textwidth]{resources/one_vs_all}

\end{document}


\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}

\begin{document}
\section{P-PCA}
In this section, we'll learn PCA from perspective of probability which is called P-PCA.

\subsection{Given}
$$
\begin{aligned}
x \in R^p, \ z \in R^q, \ q < p\\
\begin{cases}
z \backsim N(0_q, I_q)\\
x = wz + \mu + \epsilon\\
\epsilon \backsim N(0_p, \sigma^2 I_p)
\end{cases}
\end{aligned}
$$
\subsection{To solve}
$$
x|z \quad x \quad z|x
$$
\subsection{Derivation}
\subsubsection{derive $x$}
\begin{equation}
\begin{aligned}
E[x]
&= w E[z] + E[\mu] + E[\epsilon]\\
&=\mu
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
Var[x] &= w Var[z] w^T + Var[\mu] + Var[\epsilon]\\
&=ww^T + 0 + \sigma^2 I_p\\
&=ww^T + \sigma^2 I_p
\end{aligned}
\end{equation}
$$
\therefore x \backsim N(\mu, ww^T + \sigma^2 I_p)
$$
\subsubsection{derive $x|z$}
\paragraph{construct dist $t$\\}
$$
t =
\left (
\begin{matrix}
x\\
z
\end{matrix}
\right )
$$
\begin{equation}
\begin{aligned}
E[t] &=
\left (
\begin{matrix}
E[x]\\
E[z]
\end{matrix}
\right )\\
&=
\left (
\begin{matrix}
\mu l_p\\
0_q
\end{matrix}
\right )
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
cov(x,z)
&= E[(x - E[x])(z - E[z])^T]\\
&= E[(x - \mu)z^T]\\
&= E[(wz + \epsilon)z^T]\\
&= E[wzz^T] + E[\epsilon z^T]\\
&= w E[(z - E[z])(z - E[z])^T] + E[\epsilon z^T]\\
&= w Var[z] + 0\\
&= w I_q\\
&= w
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
\therefore
Var[t] &=
\left (
\begin{matrix}
ww^T + \sigma^2 I_p & w\\
w^T & I_q
\end{matrix}
\right )
\end{aligned}
\end{equation}
$$
\therefore
t \backsim
N(
\left [
\begin{matrix}
\mu l_p\\
0_q
\end{matrix}
\right ],
\left [
\begin{matrix}
ww^T + \sigma^2 I_p & w\\
w^T & I_q
\end{matrix}
\right ]
)
$$
\paragraph{construct $x.z$\\}
$$set\ x.z = x - \Sigma_{xz} \Sigma_{zz}^{-1}z$$
\begin{equation}
\begin{aligned}
\therefore x.z &= x - wz\\
&=
\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right )
\left (
\begin{matrix}
x \\
z
\end{matrix}
\right )\\
&=\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right )
t
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
\therefore
E[x.z]&=
\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right ) E[t]\\
&=
\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right )
\left (
\begin{matrix}
\mu l_p\\
0_q
\end{matrix}
\right )\\
&=\mu l_p
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
Var[x.z]
&=
\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right )
Var[t]
 \left (
\begin{matrix}
I_p \\ -w^T
\end{matrix}
\right )\\
&=
\left (
\begin{matrix}
I_p & -w
\end{matrix}
\right )
\left (
\begin{matrix}
ww^T + \sigma^2 I_p & w\\
w^T & I_q
\end{matrix}
\right )
\left (
\begin{matrix}
I_p \\ -w^T
\end{matrix}
\right )\\
&=
\left (
\begin{matrix}
\sigma^2 I_p & 0
\end{matrix}
\right )
\left (
\begin{matrix}
I_p \\ -w^T
\end{matrix}
\right )\\
&=\sigma^2 I_p
\end{aligned}
\end{equation}
\\
\begin{equation}
\therefore
x.z \backsim N(\mu l_p, \sigma^2 I_p)
\end{equation}
\subsubsection{derive $x|z$}
\begin{equation}
\begin{aligned}
\because
x.z &= x - \Sigma_{xz} \Sigma_{zz}^{-1} z\\
\therefore
x|z &= x.z + \Sigma_{xz} \Sigma_{zz}^{-1}z\\
&= x.z + wz
\end{aligned}
\end{equation}
Here $z$ is known, so we can treat $wz$ as constant.
\begin{equation}
\begin{aligned}
E[x|z] &= E[x.z] + wz\\
&= \mu l_p + wz
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
Var[x|z] &= Var[x.z] + 0\\
&= \sigma^2 I_p
\end{aligned}
\end{equation}\\
\begin{equation}
\therefore
x|z \backsim N(\mu l_p + wz, \sigma^2 I_p)
\end{equation}

\subsubsection{derive $z|x$}
\begin{equation}
\begin{aligned}
set\ z.x &= z - \Sigma_{zx} \Sigma_{xx}^{-1} x\\
&= z - w^T \Sigma_{xx}^{-1} x\\
set\ \Sigma &= \Sigma_{xx} \\
\therefore
z.x &= z - w^T \Sigma^{-1}x
\end{aligned}
\end{equation}
let's slightly deform the formula above:
\begin{equation}
\begin{aligned}
z.x &= z - w^T \Sigma^{-1}x\\
&=
\left (
\begin{matrix}
-w^T \Sigma^{-1} & I
\end{matrix}
\right )
\left (
\begin{matrix}
x\\z
\end{matrix}
\right )\\
&= \left (
\begin{matrix}
-w^T \Sigma^{-1} & I
\end{matrix}
\right )
t
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
E[z.x] &=
\left (
\begin{matrix}
-w^T \Sigma^{-1} & I
\end{matrix}
\right )
\left (
\begin{matrix}
\mu l_p \\ 0_q
\end{matrix}
\right )\\
&= -\mu w^T \Sigma^{-1}
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
Var[z.x] &=
\left (
\begin{matrix}
-w^T \Sigma^{-1} & I
\end{matrix}
\right )
\left (
\begin{matrix}
\Sigma & w\\
w^T & I
\end{matrix}
\right )
\left (
\begin{matrix}
-(\Sigma^{-1})^T w \\ I
\end{matrix}
\right )\\
&=
\left (
\begin{matrix}
0 & -w^T \Sigma^{-1}w + I
\end{matrix}
\right )
\left (
\begin{matrix}
- \Sigma^{-1} w\\ I
\end{matrix}
\right )\\
&= -w^T \Sigma^{-1}w + I
\end{aligned}
\end{equation}\\
\begin{equation}
\therefore z.x \backsim N(-\mu w^T \Sigma^{-1}, -w^T \Sigma^{-1} w + I)
\end{equation}\\
\begin{equation}
\begin{aligned}
\because
z.x &= z - w^T \Sigma^{-1} x\\
\therefore
z|x &= z.x + w^T \Sigma^{-1} x\\
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
E[z|x]
&= E[z.x] + w^T \Sigma^{-1} x\\
&= -\mu w^T \Sigma^{-1} + w^T \Sigma^{-1} x\\
&= w^T \Sigma^{-1} (x - \mu l_p)\\
&= w^T (ww^T + \sigma^2 I)^{-1}(x - \mu l_p)
\end{aligned}
\end{equation}
\begin{equation}
\begin{aligned}
Var[z|x]
&= Var[z.x] + 0\\
&= -w^T \Sigma^{-1} w + I\\
&= -w^T (ww^T + \sigma^2 I)^{-1} w + I\\
\end{aligned}
\end{equation}\\
\begin{equation}
\therefore
z|x \backsim N(w^T (ww^T + \sigma^2 I)^{-1}(x - \mu l_p), -w^T (ww^T + \sigma^2 I)^{-1} w + I)
\end{equation}
\subsection{Conclusion}
the parameters we are to seek are:
$$
\{ w, \ \sigma , \ \mu \}
$$
We can obtain them by maximizing $p(x|z)$ via EM algorithm which means:
\begin{itemize}
	\item we calculate $p(x|z)$ in the process of training.
	\item we calculate $p(z|x)$ in the process of inference.
\end{itemize}
\end{document}