\documentclass{article}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{subcaption}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{placeins}
\usepackage[]{algorithm2e}
\usepackage{tabularx}
\usepackage{makecell}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{
        table/search path={data},
    }
\pgfplotsset{compat=newest}
\usepackage{tikz}

\usepackage{geometry}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 }

\begin{document}
	\title{Continual Learning Project}

	\author{Term 1 Update}

	\maketitle

\section{Latest Experiments}

From our most recent discussions, the experiments I ran focused on looking
at the way in which the generalisation error and rate of forgetting between
the two teachers varied with the amount of overlap between different layers.

There is a lot to unpack so I highlight one observation in the plots below.
I have added the raw tensorboard logs to the google drive here:\\

\href{https://drive.google.com/drive/folders/1SDVxsGVrHZesUncODQXMhKiUPnOCzW3p}{https://drive.google.com/drive/folders/1SDVxsGVrHZesUncODQXMhKiUPnOCzW3p}\\

\noindent if you want to look in more detail.

\subsection{Periodic - ReLU}

In this setup the teacher is changed with a fixed period of 5000 training steps.
I ran the experiment with five different random seeds. This section shows results with the ReLU non-linearity.

\subsubsection{Independent Teachers}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.6\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {independent_seed_5_full.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
    % 	\draw (-3.2,-0.5) -- (-3.2,0.3) -- (1.6,0.3) -- (1.6,-0.5) -- (-3.2,-0.5);
        \draw [blue, thick] (-9,-0.2) -- (-8.5,-0.2);
        \node at (-7.6, -0.2) {Teacher 1};
        \draw [orange, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {Teacher 2};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{At the start of training, the rate of forgetting is very large.
    Very quickly however this levels off and performance on the network not currently
    being used to train the student stays level.}\label{fig: results17}
\end{figure}
\FloatBarrier

\newpage

\subsubsection{Overlap 25-50\%}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.46\textwidth,
        height=0.38\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
        \begin{tikzpicture}
            \begin{axis}
            [
                scaled x ticks = true,
                scaled y ticks = true,
                xlabel={\small Training steps},
                % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
                ylabel={\small Generalisation Error (log)},
                xtick={0,0.5,1},
                xtick distance=0.5,
                xticklabels={0, 50000, 100000},
                ytick distance=0.25,
                % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
                yticklabels={-20, -16, -12, -8, -4, 0},
                legend pos=south east,
                % xmajorgrids=true,
                % ymajorgrids=true,
                % grid style=dashed,
                title={25\% Overlap}
            ]
            \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap25_seed_5_full.png};
            % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_learning_curve_lim.csv};\addlegendentry{PPO}
            % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_learning_curve_lim.csv};\addlegendentry{A2C}
            % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_lstm_learning_curve_lim.csv};\addlegendentry{PPO + LSTM}
            % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_lstm_learning_curve_lim.csv};\addlegendentry{A2C + LSTM}
            \end{axis}
            \end{tikzpicture}%
            \vspace{0.2cm}
            \begin{tikzpicture}
            \begin{axis}
            [
                scaled x ticks = true,
                scaled y ticks = true,
                xlabel={\small Training steps},
                ylabel={\small Generalisation Error (log)},
                xtick={0,0.5,1},
                xtick distance=0.5,
                xticklabels={0, 50000, 100000},
                ytick distance=0.25,
                % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
                yticklabels={-20, -16, -12, -8, -4, 0},
                legend pos=south east,
                % xmajorgrids=true,
                % ymajorgrids=true,
                % grid style=dashed,
                title={50\% Overlap}
            ]
            \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap50_seed_5_full.png};
            \end{axis}
            \end{tikzpicture}%
    \vspace{0.2em}
    % \begin{tikzpicture}
    % % 	\draw (-3.2,-0.5) -- (-3.2,0.3) -- (1.6,0.3) -- (1.6,-0.5) -- (-3.2,-0.5);
    %     \draw [blue, thick] (-9,-0.2) -- (-8.5,-0.2);
    %     \node at (-7.6, -0.2) {Teacher 1};
    %     \draw [orange, thick] (-4,-0.2) -- (-3.5,-0.2);
    %     \node at (-2.6, -0.2) {Teacher 2};
    % \end{tikzpicture}
    % \vspace{1em}
    \caption[]{With 25 and 50\% overlaps they are not clearly distinguishable
    from the independent case although the 50\% case seems to separate itself a little already.
    Oddly it takes longer to stop forgetting than in the independent case.}\label{fig: results16}
\end{figure}
\FloatBarrier

\subsubsection{Overlap 75\%}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.72\textwidth,
        height=0.5\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap75_seed_5_full.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
    % 	\draw (-3.2,-0.5) -- (-3.2,0.3) -- (1.6,0.3) -- (1.6,-0.5) -- (-3.2,-0.5);
        \draw [orange, thick] (-12,-0.2) -- (-11.5,-0.2);
        \node at (-9.8, -0.2) {T1 (Independent)};
        \draw [blue, thick] (-7.9,-0.2) -- (-7.4,-0.2);
        \node at (-5.8, -0.2) {T2 (Independent)};
        \draw [magenta, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {T1 (75\%)};
        \draw [cyan, thick] (-1.1,-0.2) -- (-0.6,-0.2);
        \node at (0.2, -0.2) {T2 (75\%)};
        % \draw [blue, thick] (-12,-0.2) -- (-11.5,-0.2);
        % \node at (-10.8, -0.2) {Vanilla};
        % \draw [red!45!blue, thick] (-9,-0.2) -- (-8.5,-0.2);
        % \node at (-7.8, -0.2) {Greedy};
        % \draw [orange, thick] (-6.7,-0.2) -- (-6.2,-0.2);
        % \node at (-5.4, -0.2) {Weighted};
        % \draw [green!45!black, thick] (-4,-0.2) -- (-3.5,-0.2);
        % \node at (-2.6, -0.2) {Importance};
        % \draw [red, thick] (-1.1,-0.2) -- (-0.6,-0.2);
        % \node at (-0.1, -0.2) {Delta};
    \end{tikzpicture}
    \vspace{1em}
    \caption[]{Once we reach 75\% there is a clear split from the independent case.
    There is some co-learning happening but again it starts to take longer to level out
    when teachers are swapped.}\label{fig: results15}
\end{figure}
\FloatBarrier

\newpage

\subsubsection{Overlap 100\%}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.45\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap100_seed_5_full2.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
        \draw [orange, thick] (-12,-0.2) -- (-11.5,-0.2);
        \node at (-9.8, -0.2) {T1 (Independent)};
        \draw [blue, thick] (-7.9,-0.2) -- (-7.4,-0.2);
        \node at (-5.8, -0.2) {T2 (Independent)};
        \draw [magenta, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {T1 (75\%)};
        \draw [cyan, thick] (-1.1,-0.2) -- (-0.6,-0.2);
        \node at (0.2, -0.2) {T2 (75\%)};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{There is a strange phenomenon when the overlap reaches the limit of 100\%, which
    is already hinted at above. While there is no discernible forgetting the overall learning
    is much slower and saturates.}\label{fig: results14}
\end{figure}
\FloatBarrier

This phenomenon of saturation with 100\% overlap of hidden weights holds for all the random seeds I tested:

\begin{figure}[h]
    \centering
    \pgfplotsset{
        width=0.4\textwidth,
        height=0.3\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        scaled y ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap100_seed_10_full.png};
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_learning_curve_lim.csv};\addlegendentry{PPO}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_learning_curve_lim.csv};\addlegendentry{A2C}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_lstm_learning_curve_lim.csv};\addlegendentry{PPO + LSTM}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_lstm_learning_curve_lim.csv};\addlegendentry{A2C + LSTM}
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2cm}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        scaled y ticks = true,
        xlabel={\small Training steps},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap100_seed_15_full.png};
    \end{axis}
    \end{tikzpicture}%
    \hspace{0.01cm}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap100_seed_20_full2.png};
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_learning_curve_lim.csv};\addlegendentry{PPO}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_learning_curve_lim.csv};\addlegendentry{A2C}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {ppo_lstm_learning_curve_lim.csv};\addlegendentry{PPO + LSTM}
    % \addplot + table [y=reward, x=step_count, col sep=comma, mark=none] {a2c_lstm_learning_curve_lim.csv};\addlegendentry{A2C + LSTM}
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2cm}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap100_seed_25_full.png};
    % \addplot + coordinates {
    % (0, 0)
    % (0, 1)};\addlegendentry{Vanilla}
    % \addplot + coordinates {
    % (0, 0)
    % (0, 1)};\addlegendentry{Weighted Sample}
    % \addplot + coordinates {
    % (0, 0)
    % (0, 1)};\addlegendentry{Importance Sample}
    % \addplot + coordinates {
    % (0, 0)
    % (0, 1)};\addlegendentry{Greedy Sample}
    % \addplot + coordinates {
    % (0, 0)
    % (0, 1)};\addlegendentry{Delta Sample}
    \end{axis}
    \end{tikzpicture}%
    % \vspace{1em}
    % \caption[]{}\label{fig: results}
    \end{figure}
    \FloatBarrier

\newpage

\subsection{Periodic - Sigmoid}

In this setup the teacher is changed with a fixed period of 5000 training steps.
I ran the experiment with five different random seeds. This section shows results with the Sigmoid non-linearity.
I've only shown the plots for independent and full overlap settings, i.e. the two extremes. Other plots like above
are on the google drive. With the sigmoid non-linearity, there is no proper convergence.

\subsubsection{Independent Teachers}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.45\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.2,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -3.6, -3, -2.4, -1.8, -1.2, -0.6},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {independent_seed_5_full_sigmoid.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
    % 	\draw (-3.2,-0.5) -- (-3.2,0.3) -- (1.6,0.3) -- (1.6,-0.5) -- (-3.2,-0.5);
        \draw [magenta, thick] (-9,-0.2) -- (-8.5,-0.2);
        \node at (-7.6, -0.2) {Teacher 1};
        \draw [green!35!black, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {Teacher 2};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{}\label{fig: results13}
\end{figure}
\FloatBarrier

\subsubsection{Overlap 100\%}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.45\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.1428,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap_100_seed_5_full_sigmoid.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
        \draw [magenta, thick] (-9,-0.2) -- (-8.5,-0.2);
        \node at (-7.6, -0.2) {Teacher 1};
        \draw [green!35!black, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {Teacher 2};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{}\label{fig: results1}
\end{figure}
\FloatBarrier

\newpage

\subsection{Periodic - Linear}

In this setup the teacher is changed with a fixed period of 5000 training steps.
I ran the experiment with five different random seeds. This section shows results without a non-linearity.
Again only independent and full overlap shown here.

\subsubsection{Independent Teachers}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.45\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0, -0.6},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {independent_seed_5_full_linear.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
    % 	\draw (-3.2,-0.5) -- (-3.2,0.3) -- (1.6,0.3) -- (1.6,-0.5) -- (-3.2,-0.5);
        \draw [blue, thick] (-9,-0.2) -- (-8.5,-0.2);
        \node at (-7.6, -0.2) {Teacher 1};
        \draw [orange, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {Teacher 2};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{}\label{fig: results17}
\end{figure}
\FloatBarrier

\subsubsection{Overlap 100\%}

\begin{figure}[htpb]
    \centering
    \pgfplotsset{
        width=0.8\textwidth,
        height=0.45\textwidth,
        legend style={at={(axis cs:-0.5,-1)},anchor=south west},
    % 	legend style={at={(0.97,0.03)},
    % 		anchor=south east},
        label style={font=\large},
        legend cell align = {left},
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        cycle list={
    % 	{red, densely dotted, ultra thick},
    % 	{blue, solid, thick},
    % 	{black!45!green, loosely dashed, ultra thick},
    % 	{yellow, loosely dotted, ultra thick},
        {green!45!black, thick},
        {red, thick},
        {orange, thick},
        {blue, thick},
        {red!45!blue, thick}}}
    \begin{tikzpicture}
    \begin{axis}
    [
        scaled x ticks = true,
        xlabel={\small Training steps},
        % ylabel={\small Meta validation loss, $\Delta_{\text{Test}}$},
        ylabel={\small Generalisation Error (log)},
        xtick={0,0.5,1},
        xtick distance=0.5,
        xticklabels={0, 50000, 100000},
        ytick distance=0.25,
        % ytick={0,0.12,0.24, 0.36, 0.48,0.6,0.72,0.84, 0.96},
        yticklabels={-20, -16, -12, -8, -4, 0, -0.6},
        legend pos=south east,
        % xmajorgrids=true,
        % ymajorgrids=true,
        % grid style=dashed,
    ]
    \addplot graphics [xmin=0,xmax=1,ymin=0,ymax=1] {overlap_100_seed_5_full_linear.png};
    \end{axis}
    \end{tikzpicture}%
    \vspace{0.2em}
    \begin{tikzpicture}
        \draw [red, thick] (-9,-0.2) -- (-8.5,-0.2);
        \node at (-7.6, -0.2) {Teacher 1};
        \draw [blue, thick] (-4,-0.2) -- (-3.5,-0.2);
        \node at (-2.6, -0.2) {Teacher 2};
    \end{tikzpicture}
    % \vspace{1em}
    \caption[]{}\label{fig: results19}
\end{figure}
\FloatBarrier

% \subsection{Fully Interleaved}

% In this setup the teacher is changed after each training step i.e. with period 1.
% I ran this for five different random seeds.

% In this setting there is not really any discernable forgetting and the two tasks
% are just learned together.

\newpage

\section{Experimental Details}

\begin{table}[ht]
    \centering\small
    \caption{Hyperparameters}
        \begin{tabularx}{\linewidth}{lX}
            \toprule
            Hyperparameter & Value\\
            \midrule
            Optimiser& SGD  \\
            Loss& MSE \\
            Non-linearity& ReLU\\
            Teacher Architecture& Input: 500 $\to$ 1 $\to$ 1\\
            Student Architecture& Input: 500 $\to$ 2 $\to$ 1\\
            Learning Rate& 0.2 \\
            Test Batch Size&50000\\
            Train Batch Size& 1   \\
            Teacher Initialisation STD& 1   \\
            Student Initialisation STD& 0.001   \\
            Noise& None\\
            \bottomrule
    \end{tabularx}
\end{table}

Currently an overlap of X\% is defined in the following way:
Teacher 1 is randomly initialised. Teacher 2 is initialised such that X\%
of it's hidden weights are exact copies of the corresponding weights in teacher
1, and the rest are also randomly initialised.

\end{document}


\section{Classification}
\label{sec:modelSelection}
After the text data set, i.e., tweets are converted into numerical representation, remaining is the application of classification algorithms.
For this purpose, we employed several algorithms such as Logistic Regression (LR), Naive Bayes (NB), Random Forest (RF), Support Vector Machines (SVMs), Neural Networks (NNs) and Convolutional Neural Networks (CNNs).
After preliminary performance investigation of classification algorithms, we decided to continue our experiments with the three promising ones, which are explained below.
%On the other hand, RF, which is really fast to compute, enabled us to evaluate the classification performance quickly for the first experiments.

\paragraph{\textbf{Support Vector Machines}}
SVMs are very strong classification algorithms, even in high dimensional spaces, and the aim is to separate samples from different classes with a clear margin.
They can be applied as both linear binary classifiers and non-linear complements after kernel trick.
In our framework, we used the linear SVM classifier implementation provided by \textit{scikit}\footnote{\url{http://scikit-learn.org/}}, which is a well-known, open-source machine learning library in Python.
After some experimentation, penalty parameter is used as $1$ for 10K iterations for all training.

\paragraph{\textbf{Neural Networks}}
NNs, as nonlinear function approximators trained via backpropagation, are configurable structures with number of layers, number of neurons per layers and nonlinearity options.
For our binary classification problem, we experimented with several network structures, some of which are presented in Section \ref{sec:results}.
Training is achieved using lbfgs algorithm, which is more memory friendly compared to SGD counterparts, with a constant learning rate of $10^{-5}$ for at most 1000 iteration, during which early stopping was possible when the cost function is noticed not to improve any more than $10^{-6}$.

\paragraph{\textbf{Convolutional Neural Networks}}
\begin{figure}[t]
	\centering
	\includegraphics[width=0.8\columnwidth]{CNN_text.pdf}
	\caption{Convolutional Neural Networks for tweets}
	\label{fig:cnn}
\end{figure}

As well known for its success in computer vision area in the last years, CNNs are also applied to lots of NLP tasks with success \cite{kim14conv}.
In order for CNN to be applied to text data, get trained by batches and understand the contextual relation between numerical representations of words, each tweet is assumed to be consisting of maximum number of words $n_{max}$.
Hence, each tweet is represented by a matrix with a size of $n_{max} \times D$, where $D$ represents the dimension of representation (\textit{GloVe} vectors in our case).
Notice that all these matrices are zero-padded in the absence of words.
As we may see in Figure~\ref{fig:cnn}, the main idea behind is to apply a bunch of filters with different window sizes (depicted by different colors). Afterwards we convolve, decrease the number of data by \textit{max-pooling} and apply \textit{soft max} layer as the classifier.
Although we experimented with different training strategies, the results to be presented in Section \ref{sec:results} are trained with minibatches of size 1024, Adam optimizer with a exponentially decaying learning rate every 1000 epoch, which is initialized as $5 \times 10^{-4}$.
$n_{max}$ is chosen to be 40 for all the experiments.
A dropout layer with a keep probability of $50\%$ is used for training in order to avoid overfitting the statistics of training dataset..
Our CNN implementation relies on \textit{Tensorflow}\footnote{\url{https://www.tensorflow.org/}}, which is developed by Google and an open source software library for numerical computation using data flow graphs.


\documentclass{article}
\usepackage[a4paper, total={6in, 9in}]{geometry}
\usepackage{fancyhdr}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proof}{Proof}[section]

\pagestyle{fancy}
\fancyhf{}
\rhead{DanDoge}
\lhead{Notes on convex sets}
\rfoot{Page \thepage}
\cfoot{latest version: 2018/02/08}

\title{Notes on convex sets}
\date{2018-01-25}
\author{DanDoge}

\begin{document}

\section{Affine and convex sets}
  \subsection{lines and line segments}
    \paragraph{} \textit{line} is points of the form $y = \theta x_1 + (1 - \theta) x_2$ where $\theta \in R$, and $x_1 \neq x_2$.
    \paragraph{} \textit{line segement} corresponds to points where parameter $\theta$ is between 0 and 1.
  \subsection{affine sets}
    \paragraph{} a set is \textit{affine} iff for any $x_1, x_2 \in C$ and $\theta \in R$, we have $\theta x_1 + (1 - \theta) x_2 \in C$. This idea can be generalized to more than two points. That is to say, a affine set contains every affine combination of its points: $C$ is an affine set, $x_1, x_2,...,x_k \in C$, and $\theta_1 +...+ \theta_k = 1$, then the point $\theta_1 x_1 + ... + \theta_k x_k$ also belongs to $C$.
    \paragraph{} if $C$ is an affine set and $x_0 \in C$, then the set $V = C - x_0 = \{x\ -\ x_0\ |\ x\ \in C \}$ is a subspace. thus any affine set could be expressed as a subspace plus an offset, and the subspace doesnot depend on the choice of $x_0$.
    \paragraph{} the set of all affine combination of points in some set $C$ is called the \textit{affine hull} of $C$:
    \begin{equation}
      \mathbf{affC} = \{\theta_1 x_1 + ... + \theta_k x_k\ |\  x_1, x_2,...,x_k \in C,\ \theta_1 +...+ \theta_k = 1\}.
    \end{equation}
  \subsection{affine dimension and relative interior}
    \paragraph{} we define the $\textit{affine dimension}$ of a set $C$ as the dimension of its affine hull. if the affine dimension of $C$ is less than n, then the set lies in a affine set $\mathbf{aff C} \neq \mathbf{R}^n$. we define $\textit{relative interior}$ if the set $C$, denoted $\mathbf{relintC}$, as its interior relative to $\mathbf{affC}$:
    \begin{equation}
      \mathbf{relintC} = \{ x \in C\  |\  B(x, r) \cap \textit{aff}C \subset C \ for\ some\ r > 0\}
    \end{equation}
    where $B(x, r) = \{y | \left \| y - x \right \| \leq r\}$. we can then define the \textit{ralative boundary} of a set $C$ as $\mathbf{cl}C$ \\ $\mathbf{relint}C$, where $\mathbf{cl}C$ is the closure of $C$.
  \subsection{convex sets}
    \paragraph{} a seet $C$ is \textit{convex} if the line segment between any two points in $C$ lies in $C$. roughly speaking, a set is convex if every point in the set can be seen by every other point.
    \paragraph{} we call a point of the form $\theta_1 x_1 + ... + \theta_k x_k$, where $\theta_1 +...+ \theta_k = 1$, and $\theta_i \ge 0$, a \textit{convex combination} of the points $x_1,..., x_k$. a set is convex iff it contains every convex combination of its points. the \textit{convex hull} of a set $C$, denoted $\mathbf{conv} C$, is the set of all convex combinations of the points in $C$. the idea of a convex combination can be generalized to include infinite sums, integrals, and probability distributions. suppose $\theta_i$ satisfy
    \begin{equation}
      \theta_i \ge 0, i = 1,2,...,\quad \sum_{i = 1}^{\infty} \theta_i = 1,
    \end{equation}
    and $x_1, x_2,... \in C$, where $C \subset \mathbf{R}^n$ is convex, then
    \begin{equation}
      \sum_{i = 1}^{\infty} \theta_i x_i \in C,
    \end{equation}
    if the series converges. more generally, suppose $C \subset \mathbf{R}^n$ is convex and $x$ is a random vector with $x \in C$ with probability one, then $\mathbf{E}x \in C$.
  \subsection{cones}
    \paragraph{} a set is called a \textit{cone}, or \textit{nonnegative homogeneous}, if for every $x \in C$ and $\theta \ge 0$ we have $\theta x \in C$. a point of the form $\theta_1 x_1 + ... + \theta_k x_k$ with $\theta_i \ge 0$ is called \textit{conic combination} or a \textit{nonnegative linear combination} of $x_i$. a set $C$ is a convex cone iff it contains all conic combinations of its elements. this idea could also be generalized to infinite sums and integrals. the \textit{conic hull} of a set $C$ is the set of all conic combinations of points in $C$.

\section{some important examples}
  \subsection{hyperplanes and halfspaces}
    \paragraph{} a \textit{hyperplane} is a set of the form
    \begin{equation}
      \{x\ |\ a^Tx = b\},
    \end{equation}
    where $a \in \mathbf{R}^n,\ a \neq 0$, and $b \in \mathbf{R}$. this repersentation can inturn be expressed as
    \begin{equation}
      \{x\ |\ a^T(x - x_0) = 0\} = x_0 + a^{\perp},
    \end{equation}
    where $a^{\perp}$ denotes the orthogonal complement of $a$, $i.e.$, the set of all vectors orthogonal to it.
    \paragraph{} a hyperplane divides $\mathbf{R}^n$ into two \textit{halfspaces}, which a set of the form
    \begin{equation}
      \{x\ |\ a^Tx \leq b\},
    \end{equation}
    where $a \neq 0$
  \subsection{euclidean balls and ellipsoids}
    \paragraph{} a \textit{(euclidean) ball} in $\mathbf{R}^n$ has the form
    \begin{equation}
      B(x_c, r) = \{x\ |\ \| x \ - \ x_c \| \leq \ r\} = \{x\ |\ (x\ -\ x_c)^T(x\ -\ x_c) \leq \ r^2\},
    \end{equation}
    where $r\ \ge 0$, and the vector $x_c$ is the \textit{center} of the ball and the saclar $r$ is its $radius$. another common repersentation for the euclidean ball is
    \begin{equation}
      B(x_c, r) = \{x_c\ +\ ru\ |\ \|u\|\ \leq\ 1\},
    \end{equation}
    a euclidean ball is a convex set (use the homogeneity and triangle inequality for $\|\cdot\|_2$)
    \paragraph{} a related family of convex sets is the \textit{ellipsoids}, which have the form
    \begin{equation}
      \mathcal{E}\ =\ \{x\ |\ (x - x_c)^TP^{-1}(x - x_c)\leq 1\},
    \end{equation}
    where $P$ is symmetric and positive definite. the vector $x_c \in \mathbf{R}^n$ is the \textit{center} of the ellipsoid. the matrix $P$ determines how far the ellipsoid extends in every direction from $x_c$ by the square root of its eigenvalues. another common repersentation of an ellipsoid is
    \begin{equation}
      \mathcal{E}\ =\ \{x_c + Au\ |\ \|u\|_2\ \leq\ 1\},
    \end{equation}
    where $A$ is the square and nonsingular, in fact, $A\ =\ P^{1/2}$. when the matrix $A$ is symmetric positive semidefinite but not singular, the set is called a \textit{degenerate ellipsoid}, its affine dimension is equal to the rank of $A$, and it is also convex.
  \subsection{norm balls and norm cones}
    \paragraph{} a \textit{norm ball} of radius $r$ and center $x_c$ given by $\{x\ |\ \|x - x_c\|\ \leq\ r\}$, is convex. the \textit{norm cone} associated with the norm $\|\cdot\|$ is the set
    \begin{equation}
      C\ =\ \{(x,t)\ |\ \|x\|\ \leq\ t\}\ \subset\ \mathbf{R}^{n + 1}.
    \end{equation}
    it is also a convex set, as the name suggests.
  \subsection{polyhera}
    \paragraph{} a \textit{polyheron} is defined as the solution set of a finite number of linear equalities and inequalities:
    \begin{equation}
      \mathcal{P} = \{x\ |\ a_j^Tx \leq b_j,\ j = 1,...,m,\ c_j^Tx = d_j,\ j = 1,...,p\}.
    \end{equation}
    it will be convenient to use the compact notation
    \begin{equation}
      \mathcal{P} = \{x\ |\ Ax\preceq b,\ Cx = d\},
    \end{equation}
    where
    \begin{equation}
      A =
      \begin{bmatrix}
        a_1^T \\
        \vdots \\
        a_m^T
      \end{bmatrix}
      ,\quad
      C =
      \begin{bmatrix}
        c_1^T \\
        \vdots \\
        c_p^T
      \end{bmatrix}
      ,
    \end{equation}
    and the symbol $\preceq$ denotes \textit{vector inequality} or \textit{componentwise inequality} in $\mathbf{R}^m$.
    \subsubsection{simplexes}
      \paragraph{} suppose the $k + 1$ points $v+0, ..., v_k\ \in\ \mathbf{R}^n$ are \textit{affinely independent}, then the simplex determined by then is given by
      \begin{equation}
        C = \mathbf{conv}\{v_0,...,v_k\} = \{\theta_0v_0 +\dots+\theta_kv_k\ |\ \theta \succeq 0,\ \mathbf{1}^T\theta = 1\},
      \end{equation}
      where $\mathbf{1}$ denotes the vector with all entries one. to show the simplex is a polyheron, we define $y = (\theta_1,...,\theta_k)$ and
      \begin{equation}
        B = [v_1 - v_0\ \dots\ v_k - v_0]\ \in\ \mathbf{R}^{n\*k},
      \end{equation}
      we can say that $x \in C$ iff
      \begin{equation}
        x = v_0 + By
      \end{equation}
      we note that affine independence of the points implies that the matrix $B$ has rank $k$, therefore there exists a nonsingular matrix $A = (A_1,A_2)\in\mathbf{R}^{n\*n}$ s.t.
      \begin{equation}
        AB =
        \begin{bmatrix}
          A_1 \\
          A_2
        \end{bmatrix}
        B =
        \begin{bmatrix}
          I \\
          0
        \end{bmatrix}.
      \end{equation}
      in other words, we have $x \in C$ iff
      \begin{equation}
        A_2x = A_2v_0,\quad A_1x \succeq A_1v_0,\quad \mathbf{1}^TA_1x \leq 1 + \mathbf{1}^TA_1v_0.
      \end{equation}
    \subsubsection{convex hull description of polyhedra}
      \paragraph{} the convex hull of a finite set $\{v_1,...,v_k\}$ is
      \begin{equation}
        \mathbf{conv}\{v_1,...,v_k\} = \{\theta_1v_1+...,\theta_kv_k\ |\ \theta \succeq 0,\ \mathbf{1}^T\theta = 1\}.
      \end{equation}
      a generalization of this convex hull description is
      \begin{equation}
        \{\theta_1v_1+...,\theta_kv_k\ |\ \theta_1+\dots+\theta_m\ = 1,\ \theta_i \ge 0,\ i = 1,...,k\},
      \end{equation}
      this defines a polyheron, and conversely, every polyheron can be repersented in this form.
  \subsection{the positive semidefinite cone}
    \paragraph{} we use the notation $\mathbf{S}^n\ \mathbf{S}_+^n\ \mathbf{S}_{++}^n$ to be analogous to $\mathbf{R}_+\ \mathbf{R}_{++}$, then the set $\mathbf{S}_+^n$ is a convex cone.
\section{operations that preserve convexity}
  \subsection{intersection}
    \paragraph{} convexity is preserved under intersection: if $S_\alpha$ is convex for every $\alpha \in \mathcal{A}$, then $\cap_{\alpha \in \mathcal{A}} S_\alpha$ is convex.
  \subsection{affine functions}
    \paragraph{} suppose $S \subset \mathbf{R}^n$ is convex and $f : \mathbf{R}^n \to \mathbf{R}^m$ is an affine function. then the image of $S$ under $f$,
    \begin{equation}
      f(S) = \{f(x)\ |\ x \in S\},
    \end{equation}
    is convex. similarly, the \textit{inverse image} if $S$ under $f$ in convex. two simple examples aer \textit{scaling} and \textit{translation}, \textit{i.e.} $\alpha S$ and $S + \alpha$ are convex. the \textit{projection} of a convex set onto some of its coordinates is convex. the \textit{sum} of two sets $S_1 + S_2$ is convex, if $S_1$ and $S_2$ are convex. we can also define the \textit{partial sum} of $S_1, S_2 \in \mathbf{R}^n \* \mathbf{R}^m$ as
    \begin{equation}
      S = \{(x, y_1 + y_2)\ |\ (x, y_1) \in S_1,\ (x, y_2) \in S_2\},
    \end{equation}
    where $x \in \mathbf{R}^n$ and $y_i \in \mathbf{R}^m$. partial sums of convex sets are convex.
  \subsection{linear-fractional and perspective functions}
    \subsubsection{the perspective function}
      \paragraph{} we define \textit{perspective function} $P : \mathbf{R}^{n + 1} \to \mathbf{R}^{n}$, as $P(z, t) = z / t$, which normalizes vectors so the last component is one, and drop the last component. if $C \subset \mathbf{dom} P$ is convex then its image
      \begin{equation}
        P(C) = \{P(x)\ |\ x \in C\}
      \end{equation}
      is convex. the imverse image of a convex set under the perspective function is also convex.
    \subsubsection{linear-fractional functions}
      \paragraph{} a \textit{linear-fractional function} is formed by composing the perspective function with an affine function. suppose $g : \mathbf{R}^n \to \mathbf{R}^{m + 1}$ is affine $i.e.$,
      \begin{equation}
        g(x) =
        \begin{bmatrix}
          A \\
          c^T
        \end{bmatrix}
        x +
        \begin{bmatrix}
          b \\
          d
        \end{bmatrix},
      \end{equation}
      the function $f: \mathbf{R}^n \to \mathbf{R}^m$ given by $f = P(g)$, $i.e.$,
      \begin{equation}
        f(x) = (Ax + b) / (c^Tx + d),\quad \mathbf{dom} f = {x\ |\ c^Tx + d > 0},
      \end{equation}
      is called \textit{linear-fractional} (or \textit{projective}) function. like the perspective function, linear-fractional functions preserve convexity. if we define $mathcal{P}(z) = \{t(z, 1)\ |\ t > 0\}$ in $\mathbf{R}^{n + 1}$, and
      \begin{equation}
        Q =
        \begin{bmatrix}
          A   & b  \\
          c^T & d
        \end{bmatrix}
        \in \mathbf{R}^{(m + 1) * (n + 1)}
      \end{equation}
      then the linear-fractional function can be expressed as
      \begin{equation}
        f(x) = \mathcal{P}^{-1}(Q\mathcal{P}(x)).
      \end{equation}
\section{generalized inequalities}
  \subsection{proper cones and generalized inequalities}
    \paragraph{} a cone $K \subseteq \mathbf{R}^n$ is called a \textit{proper cone} if it satisfies the following:
    \begin{itemize}
      \item $K$ is convex.
      \item $K$ is closed.
      \item $K$ is \textit{solid}, which means it has nonempty interior.
      \item $K$ is \textit{pointed}, which means that it contains no line.
    \end{itemize}
    we associate with the proper cone $K$ the partial ordering on $\mathbf{R}^n$ defined by
    \begin{equation}
      x \preceq_K y \iff y - x \in K.
    \end{equation}
    similarly, wedefine an associated strict partial ordering by
    \begin{equation}
      x \prec_K y \iff y - x \in \mathbf{int} K.
    \end{equation}
    when $K = \mathbf{R}_+$, the partial ordering $\preceq_K$ is the usual ordering $\leq$ on $\mathbf{R}$.
    \subsubsection{properties of generalized inequalities}
      \paragraph{} transitive, reflexive, antisymmetric, preserved under addition, nonnegative scaling, limits.
  \subsection{minimum and minimal elements}
    \paragraph{} this part is totally the same as \textit{set and graph theory} in PKU.
\section{separaing and supporting hyperplanes}
  \subsection{separaing hyperplane theorem}
    \paragraph{} suppose $C$ and $D$ are nonempty disjoint convex sets, then there exists $a \neq 0$ and $b$ such that $a^Tx \leq b$ for all $x \in C$ and $a^Tx \ge b$ for all $x \in D$.
    \subsubsection{proof of separaing hyperplane theorem}
      we assume the (euclidean) \textit{distance} between $C$ and $D$, defined as
      \begin{equation}
        \mathbf{dist}(C, D) = inf\{\|u - v\|_2 \ |\ u \in C,\ v \in D\},
      \end{equation}
      and there exists points $c \in C$ and $d \in D$ that achieve the minimum distance, define $a = d - c,\quad b = \frac{\|d\|_2^2 - \|c\|_2^2}{2}$, we will show that the affine function
      \begin{equation}
        f(x) = a^T x - b = (d - c)^T(x - (1/2)(c + d))
      \end{equation}
      is nonpositive on $C$ and nonnegative on $D$, $i.e.$, this hyperplane separates $C$ and $D$.
    \subsubsection{strict separation}
      \paragraph{} the separating hyperplane we constructed above satisfies the stronger condition that $a^Tx \le b$ for all $x \in C$ and $a^Tx \ge b$ for all $x \in D$, this is called \textit{strict separation}.
    \subsubsection{converse separaing hyperplane theorem}
      \paragraph{} is not true, that existence of a separaing hyperplane implies that $C$ and $D$ do not intersect, however, if we suppose at least one of these two sets is open, then this is true.
  \subsection{supporting hyperplanes}
    \paragraph{} suppose $C \in \mathbf{R}^n$, adn $x_0$ is a point in its boundary $\mathbf{bd}C$, if $a \neq 0$ satisfies $a^Tx \leq a^Tx_0$ for all $x \in C$, then the hyperplane $\{x\ |\ a^Tx = a^Tx_0\}$ is called a \textit{supporting hyperplane} to $C$ at the point $x_0$. a basic result, called the \textit{supporting hyperplane theorem}, states that for any nonempty convex sets $C$, and any $x_0 \in \mathbf{bd} C $, there exists a supporting hyperplane to $C$ at $x_0$.
\section{dual cones and generalized inequalities}
  \subsection{dual cones}
    \paragraph{} let $K$ be a cone, the sec
    \begin{equation}
      K^* = \{y\ |\ x^Ty \geq 0\ for\ all\ x \in K\}
    \end{equation}
    is called the \textit{dual cone} of $K$. $K^*$ is a cone, and is always convex, even the orginal cone $K$ is not.
    \paragraph{} dual cone satisfy several properties, such as:
    \begin{itemize}
      \item $K^*$ is closed adn convex
      \item $K_1 \subseteq K_2$ implies $K_2^* \subseteq K_1^*$
      \item if $K$ has nonempty interior, then $K^*$ is pointed
      \item $K^{**}$ is the closure of the convex hull of $K$
    \end{itemize}
  \subsection{dual generalized inequalities}
    \paragraph{} suppose that the convex cone $K$ is proper, then it induces a generalized inequality $\preceq_K$, and the \textit{dual} of the generalized inequality $\preceq_{K^*}$
    \paragraph{} some important properties relating a generalized inequality and its dual are:
    \begin{itemize}
      \item $x \preceq_K y$ iff $\lambda^Tx \leq \lambda^Ty$ for all $\lambda \succeq_{K^*} 0$
      \item $x \prec_K y$ iff $\lambda^Tx \le \lambda^Ty$ for all $\lambda \succeq_{K^*} 0$, $\lambda \neq 0$.
    \end{itemize}
  \subsection{minimum adn minimal elements via dual inequalities}
    \subsubsection{dual characterization of minimum element}
      \paragraph{} the \textit{minimum} element: $x$ is the minimum element of $S$, with respect to the generalized inequality $\preceq_K$, iff for all $\lambda \succ_{K^*}$ 0, $x$ is the unique minmizer of $\lambda^Tz$ over $z \in S$. this means that for any $\lambda \succ_{K^*}$ 0, the hyperplane
      \begin{equation}
        \{z\ |\ \lambda^T(z - x) = 0\}
      \end{equation}
      is a strict supporting hyperplane to $S$ at $x$
    \subsubsection{dual characterization of minimal elements}
      \paragraph{} if $\lambda \succ_{K^*}$ 0 and $x$ minimizes $\lambda^Tz$ over $z \in S$, then $x$ is minimal, the converse is in general false, except that $S$ is convex.
\end{document}


\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{graphicx,color}
\usepackage{gensymb}

\title{\bf{Final Project Report: C++ implementation of the Metropolis-Hasting Algorithm}}
\author{Nicholas Malaya \\ University of Texas at Austin} \date{}

\begin{document}
\maketitle

\section{Introduction}

Bayesian Inference is a powerful statistical method that has found a
variety of applications in uncertainty quantification, decision theory,
model selection and many others. These methods have been applied to
diverse fields such as epidemiology, spam filters, computer vision, as
well as assessments of the reliability of the readiness of the United
States nuclear weapon stockpiles.

However, Bayes' Theorem, despite existing since the 18th century, has
only recently become more prevalent in scientific applications. The
recent dramatic growth in Bayesian methods coincides with the
exponential expansion of computational power. This is because for all
but a small set of models, the posterior distribution is unable to be
analytically determined, and must be statistically sampled, often
requiring far more samples than are humanly possible to generate.

For this project, we have developed a C++ implementation of the
Metropolis-Hastings algorithm. This is a Markov Chain Monte Carlo (MCMC)
method to sample from a probability distribution.  These samples will be
generated in order to estimate the posterior of a distribution for a
Bayesian Inference problem. We present results for two distributions, a Gaussian and Beta,
although it would be very simple to extend this software to other distributions.

The code is written in C++, and parallelized with openMP.
\section{Methodology}
%
% 3-4 page report on the progess of the project
%

Suppose we have a posterior that we want to sample from, but:
\begin{itemize}
 \item the posterior doesn't look like any distribution we know (no
       conjugate model exists)
 \item some (or all) of the full conditionals do not look like any
      distributions we know (no Gibbs sampling)
\end{itemize}
In these cases, it is necessary to sample the underlying (and often
quite complicated) distribution. The Metropolis-Hastings algorithm
accomplishes this.

Alternatively, we could sample from the distribution only using ``Vanilla''
MC (Monte-Carlo) methods. However, this method unnecessarily discards otherwise useful
information. As an example, consider figure one below. In this instance, the red dot shows a location
that when sampled, would be rejected because it falls outside of the function's mass.

\begin{center}
 \includegraphics[width=3.5in]{figs/norm}
\end{center}

Next, in figure two, we have another sample that is colored green, and would be accepted in this instance.
However, instead of again completely randomly sampling a point in the designated interval,
we should take advantage of the fact that we know this latest point is now within the region we are interested in sampling.
Thus, instead of simply randomly generating a new sample, we desire to sample a point that is ``nearby'' the latest one. In this way,
each point we sample is correlated with the last step we made. When each step only depends on the last step,
this is known as a ``Markov Process''.

\begin{center}
 \includegraphics[width=3.5in]{figs/norm-acc}
\end{center}

Figure three shows such a guess. Here, we have colored blue around a region adjacent to the latest guess.
By restricting our guess to a local region near the latest accepted chain, we hope to more efficiently (but still stochastically!)
sample the integral. There are many different ways we might ``propose'' a location near the last accepted sample. This decision, as well as
a more detailed discussion of the particular MCMC algorithm we are implementing, are discussed in the next section.

\begin{center}
 \includegraphics[width=3.5in]{figs/norm-corr}
\end{center}

\subsection{Model Document: The Metropolis Hastings Algorithm}

The Metropolis-Hastings Algorithm is an MCMC algorithm for generating samples. There are many ways we might
``guess'' out next point around the last one. Metropolis-Hastings ``proposes'' a location nearby randomly, by drawing random samples
from a specified probability density function.

For each Metropolis step, we begin with a guess for the starting point,
$\phi_1$. Afterward, we begin sampling. Each step of this so-called,
``rejection sampling'' involves ``proposing'' a new value, which if it
meets a specified criterion, is accepted. The next step then begins.

Thus, we begin with a proposal step (in this case, for step two),
\begin{equation}
 q(\tilde \phi_2 | \phi_1) = N(\tilde \phi_2 | \phi_1, \nu^2).
\end{equation}
In other words, our proposal step is correlated with the previous step,
in that it is drawn from a Gaussian (Normal) distribution with mean of
the previous step, and standard deviation, $\nu$. We will discuss our
choice of $\nu$ more, later. In order to draw this proposal, we generate a
proposal from a standard normal distribution, Z, and then transform it according to,
\begin{equation}
 X = \phi_1 + \nu*Z.
\end{equation}

Thus, with our new proposal $\tilde \phi_2$ drawn, we must evaluate
whether to accept this or reject it. In order to accomplish this, we
next draw from a uniform distribution, $u \sim U(0,1)$.

Now, we accept the new $\tilde \phi_2$ if it meets the following
criterion,
\begin{equation}
u < \frac{\pi_n(\tilde \phi_2)}{\pi_n(\phi_1)}.
\end{equation}
If we accept, then $\tilde \phi_2$ becomes $\phi_2$, and we begin the
next step. If we reject, then we keep our present value, e.g. $\phi_2 = \phi_1$.

Now as we proceed, you might ask, what dictates whether we accept or
reject? Well, after the ``burn-in'' period, where we move around looking
for the region where the distribution has non-zero probability, we
eventually start sampling the posterior. Here, we want to ``jump''
around in each guess just enough that we will capture the tails of the
distribution, but simultaneously, we don't want to jump so far that we
move away from the areas of the distribution that has most of the
probability associated with it. This is where our $\nu$ comes in. This
is the variance of the proposal. If it is very large, then we will tend
to make large jumps away from the mean for our proposal. These are great
for finding the distribution in the first place, and they will tend to
capture more of the tails, but simultaneously, too many large jumps will
result in more rejections by our sampling algorithm. Therefore, we need
to balance $\nu$ so that is jumps around, but not too far.

It turns out that mathematicians have shown that for various situations,
an, ``acceptance ratio'' of right around 40\% is just about right. This
acceptance ratio is simply the ratio of accepted proposals to total MCMC
steps we have taken. Therefore, we have tried to tune our $\nu$
parameter for each situation to have an acceptance ratio around 40\%.

Note that while we chose for this project a Gaussian distribution, this is not
a required feature. One could use nearly any other sort of proposal distribution. A gaussian
has several appealing features, namely, it is easily generalized to multi-dimensions, it is inexpensive to
compute, and it is a smooth function.  One other very useful feature of a Gaussian is that the
proposal distribution is symmetric, that is to say,
\begin{equation}
  f(x) = f(-x).
\end{equation}
This might not seem particularly important, but it actually significantly simplifies the sampling algorithm above.
Furthermore, note that just because the proposal distribution is not symmetric does not mean that this particular
algorithm cannot sample non-symmetric distributions. In the results section later in this report
we will successfully sample the Beta distribution (which is non-symmetric), despite using a proposal distribution
that is symmetric. However, a non-symmetric proposal distribution might be more effective at sampling such a
distribution.

\section{Using the Library}

The metropolis algorithm as detailed above has now been implemented in a few hundred lines of C++.
The codebase for this project has been developed on bitbucket, with logs
 for each commit. A fully functioning build system (a makefile) alongside a 'make
 check' regression suite has been developed. At present, three
 regression tests are run, that perform simple checks on the distributions and sampling algorithm.
The code has been tested on a local i7 quad core laptop, as well as TACC's lonestar supercomputer.

Finally, a directory, ``postprocessing'' contains scripts used to
generate all plots. These are routines using python with numpy+scipy and
matplotlib.

The library should be run as follows:

\begin{itemize}
\item make
\item make check (this runs a series of tests
\item make install (installs your routines to a local bin/)
\item make run (executes the c++ metropolis-hastings routines)
\item cd ../postproc
\item python hist-gauss.py (to generate a histogram from the gaussian sampling)
\item python hist-beta.py (to generate a histogram from the beta sampling)
\item inspect hist-gauss.png and hist-beta.png
\end{itemize}

\section{Results}

Figure four shows a histogram of the samples from a Gaussian distribution.
Shown are $1e5$ steps which are used to verify that our distribution matches a known posterior.
\begin{center}
 \includegraphics[width=3.5in]{figs/hist-gauss}
\end{center}

Likewise, Figure five shows a histogram of the samples from a $\beta$ distribution.
Shown are $1e5$ steps which are used to verify that our distribution matches this posterior.

\begin{center}
 \includegraphics[width=3.5in]{figs/hist-beta}
\end{center}

Now you might say, this doesn't fit particularly well! And you are somewhat correct.
The mean seems to be over-predicted (higher than the green line) and the tail (off near $x=1$) is under-resolved.
This is not too surprising, as it is harder to resolve the tails, and generally takes more samples. This image looks very much like
a not-converged set of samples! So let's bump up the number of samples to $1e7$ to see if things match more closely. This is show in figure six, below.

\begin{center}
  \includegraphics[width=3.5in]{figs/hist-beta_10e7}
\end{center}

Much better. We have now resolved the tail and the mean matches the result much more closely! So why am I showing you this, instead of
just starting with $1e7$ and showing you those results? Because it is an example of how MCMC can be tricky: without that exact result
(which we typically do not have, or we would have no reason to use MCMC!) we would not have known that we had too few samples. So one must be careful to ensure that
the system is ``converged'', in particular, that the mean, standard deviation, shape of the histogram, etc. are all not changing as a function of the number of steps.

\section{OpenMP Multithreading}

In MCMC, one can also instantiate multiple chains, which advance for each
Markov-chain step based on the same algorithm as outlined above. In other words,

Each chain is completely independent, and the parallel problem is ``embarrassingly parallel'', e.g. we
do not require any communication between chains during the runs. This means we anticipate near-perfect scalability.

In order to accomplish this, we start each chain at a slightly different location, by perturbing each chain
slightly from our initial guess. Then, each thread can run completely independently, and at the end of the run,
we will combine the chains together.

Preparing this in the code is relatively straight-forward. We have created a third driver routine, entitled, ``metropolis-omp'' that is created at compile time when using the command, ``make''. This routine is compiled with the C++ compiler, adding only,

\begin{itemize}
\item -fopenmp -lpthread
\end{itemize}
for GCC, or for intel:

\begin{itemize}
\item -openmp
\end{itemize}

to the \$LDFLAGS variable. A standard C++ compiler (such as intel's icpc or GNU's G++) will then enable OpenMP.
At this point, we create as many chains as there are threads, using the omp\_get\_max\_threads() method. We can also
vary the number of threads by calling the omp\_set\_num\_threads(n) method, where n here is the desired number of threads.
Each chain is given a number of steps to run, which are appended to a file. This is all parallelized
using a PRAGMA, in particular, ``\#pragma omp for'' before the loop that runs each chain.
At this point, each chain can run indepdendently.

The plot of the strong scaling is shown below. Regrettably, very little speedup is seen. This is because the computation is relatively modest,
and the vast majority of the computation appears to be in File I/O as well as spawning the threads. Therefore,
the advantage of multiple threads is completely mitigated.

\begin{center}
  \includegraphics[width=3.5in]{figs/data}
\end{center}

We could likely fix this by changing how the code runs: presently, at each step of the sampler, the present
MCMC chain location is written to disk. It would be far more efficient to capture all the samples
in an array, and then after each thread has finished sampling, and then writing that entire array to disk.

\section{Conclusions}

We have shown how to formulate an MCMC rejection sampler, the Metropolis-Hastings algorithm. For this we use a gaussian distribution for the proposal. We have instantiated a C++ implementation
of this algorithm, which we have tested for the Gaussian and Beta distributions. Finally, we developed an OpenMP parallelization around this algorithm.

All the code is available in project/src. The postprocessing used to generate the plots are stored in project/postproc. Finally, this entire report, the figures and the LaTeX used to generate this PDF
are all stored in project/final.

I had fun writing a Metropolis-Hastings sampler, which is a very fundamental algorithm in computational
statistics. Thank you for reading this document and examining the project. I'm sure this is an involved and time-consuming process. Thank you for the class, and have a happy Holiday Season!

--Nicholas Malaya

\end{document}


\documentclass[12pt,english]{article}
\usepackage[a4paper,bindingoffset=0.2in,%
            left=1in,right=1in,top=1in,bottom=1in,%
                        footskip=.25in]{geometry}

\usepackage{graphicx} % Required to insert images
\usepackage{amsmath}

\newcommand{\hmwkTitle}{PCA Assignment} % Assignment title
\newcommand{\hmwkClass}{MATH-578} % Course/class
\newcommand{\hmwkClassTime}{} % Class/lecture time
\newcommand{\hmwkAuthorName}{Saket Choudhary} % Your name
\newcommand{\hmwkAuthorID}{2170058637} % Teacher/lecturer
%----------------------------------------------------------------------------------------
%       TITLE PAGE
%----------------------------------------------------------------------------------------

\title{
\vspace{2in}
\textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
}

\author{\textbf{\hmwkAuthorName} \\
        \textbf{\hmwkAuthorID}
        }
\date{} % Insert date here if you want it to appear below your name

\begin{document}

\maketitle
\newpage

\section{Part a}

\subsection{Photo Image}
\begin{figure}
%\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{Mean-Patch-Photo-Images}
    \caption{Mean Patch for Photo Images}
\end{figure}
General feature of Photo Image: Band of colors with gradual descent(colors merge into one another), indicating image has less features(less variance)
overall.
%\end{minipage}
%\begin{minipage}{0.5\linewidth}
\subsection{VTEC Image}
\begin{figure}
    \includegraphics[width=\linewidth]{Mean-Patch-VTEC-Images}
    \caption{Mean Patch for VTEC Images}
%\end{minipage}
\end{figure}

General features of VTEC Image: Band of colors which form a steep gradient(different colors do not merge into one another), indicating
that the image has more features (the total variance is high)

\pagebreak
\newpage
\clearpage

\section{Part b}

\subsection{Photo Image}
\begin{figure}
    \includegraphics[width=\linewidth]{first25-Photo-Images.png}
    \caption{First 25 eigen vectors of Photo Image. The images exhibit high variance}
\end{figure}

\begin{figure}
    \includegraphics[width=\linewidth]{last25-Photo-Images.png}
    \caption{Last 25 eigen vectors of Photo Image. Images exhibit low variance and hence are indistinguishable}
\end{figure}

\subsection{VTEC Image}
\begin{figure}
    \includegraphics[width=\linewidth]{first25-VTEC-Images.png}
    \caption{First 25 eigen vectors of VTEC Image. Images exhibit high variance.}
\end{figure}

\begin{figure}
    \includegraphics[width=\linewidth]{last25-VTEC-Images.png}
    \caption{Last 25 eigen vectors of VTEC Image. Images exhibit low variance and are indisinguishable}
\end{figure}

\newpage
\section{Part c,d,e}

Given that we are compressing a $600\times 600$ to a $400\times r$ matrix of eigen vectors(assume the eigen values vetor of length $r$ occupies egligible
space),
we can simply store this eigen vector matrix, we attain a $\frac{400}{r}\%$ reduction.
For example with $r=1$ we are attaining a $400\%$ reduction, with $r=80$ it is $5\%$.
With $r=10$, it is $40\%$.

\subsection{ Photo Images }
\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-original.png}
    \caption{\footnotesize{Original 170th Photo Image}}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-reconstructed-r=1.png}
    \caption{\footnotesize{r=1. \% variance = $78.7749$}}
\end{minipage}
\end{figure}

\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-original.png}
    \caption{Original 170th Photo Image}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-reconstructed-r=10.png}
    \caption{r=10. \% variance = $92.1408$}
\end{minipage}
\end{figure}

\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-original.png}
    \caption{Original 170th Photo Image}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-Photo_Images-reconstructed-r=80.png}
    \caption{r=80. \% variance = $99.3984$}
\end{minipage}
\end{figure}

\subsection{ VTEC Images }
\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-original.png}
    \caption{\footnotesize{Original 170th VTEC Image}}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-reconstructed-r=1.png}
    \caption{\footnotesize{r=1. \% variance = $95.3856$}}
\end{minipage}
\end{figure}

\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-original.png}
    \caption{Original 170th VTEC Image}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-reconstructed-r=10.png}
    \caption{r=10. \% variance = $99.9930$}
\end{minipage}
\end{figure}

\begin{figure}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-original.png}
    \caption{Original 170th VTEC Image}
\end{minipage}
\begin{minipage}{0.5\linewidth}
    \includegraphics[width=\linewidth]{pca-part-c-VTEC_Images-reconstructed-r=80.png}
    \caption{r=80. \% variance = $99.9991$}
\end{minipage}
\end{figure}

\end{document}


\documentclass[12pt, a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath, amsthm, amssymb, textcomp}
\usepackage{setspace}
\usepackage{paralist}
\usepackage{graphicx}
\usepackage{caption}
\graphicspath{{WSK_im/}} %Graphic is in a folder named WSK_im in the currend directory
\usepackage{float, hyperref}
\usepackage{authblk}
\renewcommand\Authfont{\fontsize{12}{14.4}\selectfont}
\title{Bayesian probability theory - Lesson 5:\\
Odyssey and stochastic processes}

\author{Wolfgang von der Linden, Gerhard Dorn, Johanna Moser}
\date{Transcript}

\begin{document}
\setlength{\parindent}{0pt}
\maketitle
\onehalfspacing

Welcome to the fifth unit of the course on Bayesian probability theory.
My name is Wolfgang von der Linden and I will enable you to help Captain Bayes and her crew to prevail on their odyssey across the ocean.
This unit is dedicated to \textbf{stochastic processes} and \textbf{random walks}. We will determine the average distance from the origin in a random walk. We will investigate the relation to diffusion. We will discuss \textbf{Markov processes} to simulate the moving pattern of the rat on the ship and we will learn how to solve probabilistic problems by computer simulations.\\

\section*{The random walk}
Let us begin this lesson with a proper description of Captain Bayes' random odyssey across the ocean as a so-called \textbf{random walk}.
We consider a regular infinite square lattice in two dimensions which represents the possible positions of the ship on the map. The spacing is 1 in arbitrary units. The ship shall start at the origin. Each possible ship position has 4 nearest neighbors that are a distance one away. Every day - which we call a step - the ship - now also called a walker -  randomly jumps to one of these nearest neighbor positions. %5_1
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_1.png}
\end{figure}
\fbox{\parbox{\linewidth}{\textbf{Question 1.} How many possibilities are there to reach position (2$|$1) in 3 days?\\
a) 1\\
b) 4\\
c) 3\\
d) 2
}}\\[0.2cm]
The journey is therefore characterized by a sequence of directions the ship sails.
We map the cardinal directions to random variables and count the frequencies $K_i$ how often a direction occurs.
There are several paths that lead to the same final position, which is given by the $x$ and $y$ coordinates that can be calculated from the \textit{frequencies of the directions}, i.e. $x = K_1-K_3$, $y=K_2-K_4$\\
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_2.png}
\end{figure}
\fbox{\parbox{\linewidth}{\textbf{Question 2.} Which of the following frequencies lead to the position (3$|$2)?\\
a) $K_1=3,K_2=0,K_3=2,K_4=0$\\
b) $K_1=1,K_2=3,K_3=2,K_4=0$\\
c) $K_1=3,K_2=8,K_3=0,K_4=6$\\
d) $K_1=5,K_2=6,K_3=2,K_4=4$\\
}}\\

The probability for such a set of direction frequencies after steps is given by the \textbf{multinomial distribution}.\\
\begin{equation*}\boxed{P(K_1,K_2,K_3,K_4|N)=\frac{N!}{\prod_iK_i!}\prod_iQ_i^{K_i}
}\end{equation*}\\
\fbox{\parbox{\linewidth}{\textbf{Question 3.} Given the following frequencies: $K_1=3,K_2=1,K_3=4,K_4=5$, what is the final position of the ship (it always starts at (0$|$0)?\\
a) (3$|$1)\\
b) (2$|$-1)\\
c) (-1$|$-4)\\
}}\\
\fbox{\parbox{\linewidth}{\textbf{Question 4.} Given the same frequencies: $K_1=3,K_2=1,K_3=4,K_4=5$, how many different paths can be realized with those frequencies?\\
a) 360360\\
b) 210\\
c) 3422\\
}}\\

The compass is perfectly symmetric so by the \textbf{principle of indifference} all four directions have the same probability of $P=0.25$\\
\textit{Note: You can become creative and change the probabilities of the compass - maybe making them also dependent on the position of the ship - to study for instance the effect of an ocean shift or ocean streams}.\\
%%%%%%

What we have derived just now is a so-called \textbf{random walk} which is a special case of a \textbf{stochastic} or \textbf{random process}. Such processes are in most cases \textit{time or space dependent random variables}. In our case it is the time-dependent sequence of random positions of the ship on the map. If it depends on space, it is also called a \textbf{random field} in modern literature.
Stochastic processes are widely used to model and analyse complicated systems such as stock market prices, the motion of particles in a liquid or the spacial random variation of geological material to study the origin of earth quakes.
The properties of stochastic processes can be studied numerically by using many repeated realisations of one walk or by using many walkers in parallel.
Furthermore, stochastic processes are also used to solve high dimensional problems that originally have nothing to do with chance.  Like the evaluation of high-dimensional integrals or the solution of partial differential equations.\\

\section*{The final position}
We will now discuss the general statements we can make about Captain Bayes' random odyssey.\\
\textit{Which final position do we expect on average after N days steps if many ships walkers would start at the same time and same initial position?}
The \textbf{mean value} in $x$-direction is given by the \textit{mean of the difference of the direction frequencies} \[\langle K_1\rangle-\langle K_3\rangle.\] By using linearity we can conclude that the mean position is 0.
\textit{That does not mean that on average a single walker does not move!}
It rather means that if you repeat the random walks many times, the mean of the final positions will be zero.
Here is an extreme example in 2 dimensions: one ship walker may end at ($N$,0) and another one at ($-N$,0). Their mean value is zero, although they have traveled individually to the furthest possible points.\\
Bernoulli wanted to know the \textit{average distance they reach from their origin after $N$ days}. Obviously, the mean position is not the right object to answer this question. Instead we’d rather use the \textbf{mean of the quadratic distance} \[\langle x^2\rangle = \langle K_1^2\rangle + \langle K_3^2\rangle-2\langle K_1K_3\rangle\] and take the square root, since this provides a suitable distance measure.
We can split $K$ into the mean plus a fluctuation $K_i=\langle K_i\rangle +\Delta K_i$
So (after a somewhat lengthy calculation) the mean squared distance is equal to the number of steps $N$.
Therefore, the answer to Bernoulli's question is:
\textit{The mean distance is given by the square root of the number of steps.}\\

As usual, Captain Bayes wanted to delve deeper and asked about the \textit{probability for the position}. The answer is particularly transparent in 1 dimensions where we use a compass that has only the two directions ``West" and ``East".
Then we can express the $x$-position after $N$ steps by the difference of the number of steps $K_i$ taken in these directions.
To reach position $x$ in $N$ steps, the number of times the compass points to the ``West" must be $K_{\text{West}}$.
\begin{equation*}\boxed{x = K_{\text{East}} - K_{\text{West}} = N - 2K_{West}\Rightarrow K_{\text{West}}=\frac{N-x}{2}
}\end{equation*}\\
As the frequency to dial $K$ times "West" on the random compass is given by the \textbf{binomial distribution}, the same holds true for the position.
\begin{equation*}\boxed{P(x|N)={N\choose \frac{N-x}{2}} 2^{-N}
}\end{equation*}\\
Note that in an even or odd number of steps, only an even or odd site can be reached.
For the 2D case we will use \textbf{Markov processes} and matrices to determine the probability for the position.\\

\section*{Diffusion as a random walk}
Captain Bayes also speculated that \textit{diffusion is a random walk} too. That is indeed the case.
Diffusion is actually a motion in continuous space and time, but we will introduce finite time steps d$t$. Within such a time step two things can happen. Either the particle \textit{hops a distance d$x$} in one of the Cartesian directions or it \textit{stays where it is}. We make the obvious assumption that the hopping probability is proportional to the time interval d$t$.
Then, the probability that \textbf{no hopping} occurs in dt is (1 - $P$(hopping)). %5_3
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_3.png}
\end{figure}
That defines the random walk. The only difference to the previous case is the possibility that the walker does \text{not} move in each time step, which would correspond to the free day Bernoulli is longing for.
The trick now is to express the probability to reach position $x$ at time $t$ via the \textit{marginalization rule} as a sum over probabilities for the position $x’$ at time $t-\text{d}t$ times the hopping probability from $x’$ to $x$ in one time step d$t$. That leads to a recursion relation.
\begin{equation*}\boxed{P(x,t)=\sum_{x'}P(x,t|x',t-\text{d}t)P(x',t-\text{d}t)=(1-\alpha\Delta t)P(x,t-\text{d}t)+\sum_{|x-x'|=\Delta x}\frac{\alpha\Delta t}{2D}P(x',t-\text{d}t)
}\end{equation*}\\
The same trick will be discussed in greater detail in case of the Markov process. Here we are more interested in the final result.
Rewriting the recursion relation and taking the appropriate limit d$t\rightarrow 0$ and d$x \rightarrow 0$ leads to the (to physicists) well known diffusion equation.
\begin{equation*}\boxed{\frac{\partial}{\partial t}\rho(x,t)=\alpha \frac{\partial^2}{\partial x^2}\rho(x,t)
}\end{equation*}\\
Here, $\alpha$ is the \textit{diffusion constant} and $\rho$ stands for the \textit{probability to find the particle at time $t$ at position $x$}. But as we discussed in the second unit it can also be interpreted as relative frequency. So if there is a huge number of particles in the system described by their density, the diffusion equation is equally valid for the density.
Let us think of a drop of ink that is put on the water surface. Initially the size is almost zero.
For this initial value problem the solution of the diffusion equation on the water surface is a 2 dimensional Gaussian.
It is centered at the origin and the variance grows proportional to time. %5_4
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_4.png}
\end{figure}
So Captain Bayes correctly concluded that diffusion can be described by a random walk.\\

\section*{Returning to the starting point}
Pascal raised the question, whether they will ever see the bottle that she put in the ocean again.
Assuming the ship is on a daily random course the question concerns the\textit{ probability that a random walk will return to its starting point}.
To answer this question we consider the probability that an isotropic random walk will return to its starting point after a certain number of steps.
We introduce the following proposition $a_N$ and its probability $P(a_N)$ of having an arbitrary return after $N$ steps, which includes the possibility of having already seen the bottle in less than $N$ steps as well.
The return always requires an \textit{even number of steps}, $N=2M$, so we introduce the variable $M$, because the return implies that for each Cartesian axis the walker moves forward as often as backward.
In 1D the frequency going East is simply half the number of total steps and the probability is given by the \textbf{Binomial distribution}.
But how does one answer the question whether the walker will ever return home? One could come up with the idea of \textit{summing over $M$ from 1 to infinity}, since this covers all possibilities for a return - after an even number of steps. %5_5
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_5.png}
\end{figure}
If the sum is less than one, then the probability that the walker will never return is not-zero.
But, unfortunately, the events of arbitrary return are not exclusive since we may \textit{double count paths that} reach the origin and the sum over $M$ does not have the desired meaning.\\

What we need instead is the probability for the \textbf{first return after N steps}.
There is a clever way to compute it. The first part of an arbitrary return is always a first return $f_K$. If the arbitrary return occurs after $N = 2M$ steps, the first return may happen at $K$ steps, and the remaining steps again describe an arbitrary return.
So we can \textit{decompose} any arbitrary return into first return and remaining arbitrary returns.%
\begin{equation*}\boxed{a_{2M}=\bigvee_{L=1}^{M}(f_{2L}\wedge a_{2(M-L)})
}\end{equation*}\\
This equation can be solved for the desired probability of first return by what is called the \textbf{generating functions} or a \textbf{Fourier transform}. After a somewhat lengthy calculation summing all probabilities of first return, it yields the probability to ever return - which in \textbf{1D and 2D} is equal to 1.%5_6
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_6.png}
\end{figure}
Pascal will definitely see her bottle again, but it may take a very long time.
But what about Ernesto, will he ever return home if he follows a \textbf{3D random walk}, which may happen if he ate too many overripe grapes?
In that case the probability to ever return is less than one (34\% to be precise).
Poor Ernesto, if he is drunk the chances are 66\% that he will get lost.
\textit{So never get drunk in three dimensions}!\\

\section*{Reaching turtle island}
Bernoulli wanted to know the \textit{chance to reach turtle island that is 5 steps away within 5, 6, up to 10 days}.
Similarly to the ideas of the First return we can at least say the following immediately: It takes an odd number of days as the distance is odd - just think of a checkerboard with the colors corresponding to the even or odd days.%5_7
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_7.png}
\end{figure}
To reach the island in 5 days if it is 5 steps away means that the ship has to go straight.
So there is only one favourable path out of $4^5$. Thus the probability is
$4^{-5} = 0.001$ which is surprisingly small.
For the other cases one has to examine \textit{all combinations of direction frequencies $K_i$ that lead to the final position after $N$ steps}.
As mentioned before the probability for such a combination is given by the \textbf{multinomial distribution}. Although the tricky thing is again that we are considering paths with arbitrary arrivals and not those with first arrival, but Bernoulli will definitely disembark as soon as he reaches the island.
So we have to subtract those paths which already have reached turtle island or use the same convolution trick we saw before.
\begin{equation*}\boxed{P(f_N)=\left(\frac{N!}{\prod_i K_i!}-\#\text{paths through turtle island}\right)\prod_i Q_i^{K_i}
}\end{equation*}\\
Besides the analytical version there are also other ways to solve this problem, one by using a \textit{computer simulation} using many walkers to count the relative frequencies of arrival, and one based on \textit{Markov matrices} and working with probability distributions which we will discuss just now.\\ \textit{Have a look at the Pluto notebooks that show you these different solutions and other simulation techniques!}\\

\section*{Markov processes}
There are many different important types of stochastic processes. Those discussed so far have one thing in common: The state or position of the next time step depends only on the previous position, so there is \textit{no memory}. A journey with memory could be that Captain Bayes choses the next direction with a probability that she favours directions that occurred less frequently in the last 10 days.
A stochastic process whose \textit{next state only depends on the present state} is called \textbf{Markov process}.
We can exploit the Markovian property to derive a recurrence relation, as we did for the diffusion problem.
Let $x_t$ be the proposition describing for instance the position of a particle at time $t$. Time shall be discretised in units of d$t=1$. The probability for the position at time $t+1$  can then be related to the probability for the position at time $t$ via the marginalization rule.
In the case of the random walk the \textit{sum is restricted} to positions that are nearest neighbours of the final position.
\begin{equation*}\boxed{P(x^{(t+1)})=\sum_iP(x^{t+1)}|x_i^{(t)})P(x_i^{(t)})=\sum_{|x^{(t+1)}-x_i^{(t)}|=\Delta x}P(x^{t+1)}|x_i^{(t)})P(x_i^{(t)})=
}\end{equation*}\\
Since in the present example the hopping probabilities are not time dependent, we have a so-called \textbf{homogeneous} Markov process.  An example for an \textbf{inhomogeneous} Markov process would be given if there is a storm or an ocean current that influences the hopping probabilities.\\

\fbox{\parbox{\linewidth}{\textbf{Question 5.} Which realizations of an ocean current or defect on the ship are still Markovian?\\
a) The rudder for steering the ship is damaged, so the probability for turning left is always smaller than for turning right.\\
b) When hitting the coral reef on position (-2$|$2), the rudder gets damaged and the ship loses its ability to tack against the wind coming from north, so the probability for north decreases whereas all other directions become more likely.\\
c) After 10 days a storm came up, changing the probabilities for going north (decreased) and south (increased).\\
d) The probability for sailing west increases and east decreases as soon as the ship enters a $y$ coordinate larger than 3.
}}\\

\section*{The rat problem}
The possible positions of the rat can be enumerated by an index $i$. %5_8
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_8.png}
\end{figure}
Then the recursion relation can be expressed by matrix-vector multiplications.
\begin{equation*}\boxed{\vec{P}^{(t+1)}=M\vec{P}^{(t)} \qquad \text{where} \qquad P_i^{(t)}=P(x^{(t)}=x_i)
}\end{equation*}\\
Here $M$ is the so-called \textbf{Markov matrix}.
\begin{equation*}\boxed{ M_{ij}=P(x^{(t+1)}=x_i|x^{(t)}=x_j)
}\end{equation*}\\

As an example, let us consider the rat as a creature without memory that decides the next move randomly \textit{based only on the current position and nearby positions that are in range}. Each such move may have its \textit{individual probability}, perhaps depending on the odor gradient. \\
%5_9
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_9.png}
\end{figure}

We cannot avoid introducing at least one more definition: \textbf{Ergodicity}.\\
A Markov process is \textbf{ergodic} if there is a \textit{nonzero probability} for reaching every state $\xi_{j}$ from every other state $\xi_{i}$ in a \textit{finite} number of steps. \\%box
In the case of the rat, there could be certain disjoint areas on the ship between which the rat cannot switch.
The key object we want to determine with Markov processes is the \textit{probability distribution of the position of the rat after $t$ time steps when we know its starting point}.\\
At time $t=0$ the probability vector has one non-zero entry only at the position of the origin. At any later time $t$ the probability vector follows from the recursion relation.%5_10
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_10.png}
\end{figure}
So this illustrates how Markov processes can be used. You can also treat the question of first return or first arrival by the Markov matrices or one can generalise the concept to more general hopping probabilities.\\
\textit{In the interactive notebooks you can examine how to derive these Markov matrices and how to apply them.}\\

 \section*{Poisson processes}
Another stochastic process is the \textbf{Poisson process}, which in contrast to the random walk does not choose a random direction but a \textit{random length of the next step} at which an event happens or a \textit{random number of events that happen at the next time step of equal length}.%

Figuratively speaking, it’s a special staircase, where each step height is one but the step length is a random variable with an exponential distribution. Alternatively, one could always use the same step length but the step height is taken from a poisson distribution. With each realisation we obtain a new staircase.%5_11
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_11.png}
\end{figure}
Poisson processes play an important role in \textit{queuing theory} or in \textit{insurance} to describe the distribution of special events such as accidents.\\

\section*{Gaussian processes}
Now we turn to \textbf{Gaussian processes}. They are a special type of stochastic processes that are very \textit{versatile} and also allow for non-Markovian properties where one can \textit{choose parameters that describe the correlation} of the random variables.
For easier understanding we will give a \textit{specialised and simplified definition} of a Gaussian process that nevertheless covers most applications.
The index of the random variable $X_t$ shall be time and we only consider a finite set of discrete times (not necessarily equi-distant), the generalization to space or other index sets is obvious.
The vector formed by the corresponding random variables is \textbf{multivariate Gaussian}.
A multivariate Gaussian is given by %5_12
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_12.png}
\end{figure}
 and it is characterized by its \textit{mean $\vec{\mu}$ and its covariance $\Sigma^{-1}$},
For the case of a one-dimensional random walk the elements of the vector are the positions at the discrete times.
The power of Gaussian processes is due to the \textit{wide variety of forms} that can be achieved by different choices of the covariance or kernel function defining the elements of covariance matrix.%5_13
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_13.png}
\end{figure}
I guess it will surprise you to hear that straight lines can be the result of a Gaussian process as well as the random walk, just due to the proper choice of the kernel function.\\
\textit{In the Pluto notebook you have ample opportunity to get acquainted with various kernels and types of results you can describe by them}.\\

\section*{Epidemics as a random walk}
Yes indeed, the spread of an epidemic can also be simulated as a random walk.
Initially there is one person that has the virus.
On each site the walker reaches, there is either a person or not, depending on the population density and it can either be susceptible for the virus, or immune. If this person also gets infected there is a second contagious walker. After 7 steps / days an infected walker becomes immune.
The epidemic spread can also be treated by probabilistic means in terms of the so-called \textbf{SIR equation} that describes the time-evolution of the probabilities for Susceptible, Infected, and Recovered (immune) people.  \\
As it would be beyond the scope of this imoox course to discuss the derivation and solution of the SIR equation in more detail, we refer to a nice youtube video for more details:
\url{https://www.youtube.com/watch?v=gxAaO2rsdIs (3b1b)}\\

This brings us to the end of Lesson 5.
We have gained some insights into how to use stochastic processes to model real life applications. Now it's your turn to deepen your understanding of these processes by going through the interactive notebooks, revisiting some of the derivations, and reviewing the additional resources.
Please feel free to ask questions in the forum and feel encouraged to test your knowledge in the quiz!
 \begin{figure}[H]
	\centering
	\includegraphics[width=0.75\textwidth]{5_14.png}
\end{figure}

\vspace{2cm}
\begin{minipage}[t]{1\textwidth}
	\raggedleft
	\centering
	\includegraphics[width = 0.20\textwidth]{CC-BY_icon}
	\vspace{0.2cm}

	\centering
	{\large ITPCP, TU Graz} \\
	https://creativecommons.org/licenses/by/4.0/legalcode
\end{minipage}
\end{document}


\documentclass{memoir}
\usepackage{notestemplate}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}

\subsection{Quotient Groups}
\label{subsec:quotient_groups}

\begin{defn}[Group Homomorphism]
	We call a map \(\varphi:G_1\to G_2\) a \textbf{group homomorphism} if it preserves the operation; \(\varphi(gh) = \varphi(g)\varphi(h)\) for every \(g,h \in G_1\).
\end{defn}
If the homomorphism is bijective, then it is an \textbf{isomorphism}. A homomorphism \(\varphi\) is an isomorphism if and only if \( \textrm{Ker}\varphi = e_1\) and \( \textrm{Im}\varphi = G_2\).

\begin{prop}
	Let \(G\) and \(H\) be groups and let \(\varphi :G\to H\) be a homomorphism.
	\begin{itemize}
		\item \(\varphi (1_G) = 1_H\)
		\item \(\varphi (g^{-1}) = \varphi (g)^{-1}\)
		\item \(\varphi (g^{n}) = \varphi (g)^{n}\)
		\item \(\textrm{Ker}\varphi \triangleleft G\)
		\item \(\textrm{Im}\varphi \leq H\)
	\end{itemize}
\end{prop}
In fact, a subgroup \(N \triangleleft G\) is normal if and only if it is the kernel of a group homomorphism.

\begin{defn}[Quotient Group]
	We denote by \(G / N\) the \textbf{quotient group}, whose elements are the cosets of the normal subgroup \(N\), with operations defined by \((aN)(bN) = (abN)\)
\end{defn}
\begin{thm}[First Isomorphism Theorem]
If \(\varphi:G_1\to G_2\) is a homomorphism, then
\begin{align*}
	\textrm{Im}\varphi \cong G_1 / \textrm{Ker}\varphi.
\end{align*}
\end{thm}
This tells us that \(\varphi \) is injective if and only if the kernel is trivial, and we can also see that \(\left| G : \textrm{Ker}\varphi  \right|  = \left| \varphi (G) \right| \).\\

This key theorem allows us to fully connect the notion of normal subgroups partitioning a group.

\begin{defn}[Natural Homomorphism]
	If \(N \triangleleft G\), then \(\psi:G \to G / N\) defined by \(\psi(g) = gN\) is the natural homomorphism with \( \textrm{Ker}\psi = N\) and \( \textrm{Im}\psi = G / N\).
\end{defn}

\begin{thm}[Third Isomorphism Theorem]
	Let \(G\) be a group, and let \(H,K \triangleleft G\) with \(H\leq K\). Then \(K / H \triangleleft G / H\) and
	\begin{align*}
		(G / H) / (K / H) \cong G /K
	\end{align*}
\end{thm}

The point of this theorem is that quotients of quotient groups provide little additional information.

\begin{thm}[Fourth Isomorphism Theorem]
	Let \(N \triangleleft G\) be a normal subgroup of \(G\). There is a bijection from the set of subgroups A satisfying \(N \leq A\leq G\) onto the set of subgroups \(A/N \leq G / N\).\\

	That is, every subgroup of \(G / N\) can be viewed as some \(A / N\) for some subgroup \(A\) containing \(N\). Furthermore, for all \(A,B \leq G\) with \(N\leq A,B\),
	\begin{itemize}[(i).]
		\item \(A\leq B \iff A / N \leq B / N\)
		\item \(A\leq B \implies \left| B : A \right| = \left| B / N : A / N \right| \)
		\item \(\langle A,B \rangle / N = \langle A / N, B / N \rangle \)
		\item \(A \triangleleft G \iff A / N \triangleleft G / N\)
	\end{itemize}
\end{thm}
This theorem really just tells us that we can get isomorphisms beween structures via lattices-- if two group structures have a certain lattice structure, there is a natural isomorphism between each other.

% Include some example of lattices here

\end{document}

Abstract Algebra - Introductory/Algebra I/Notes/source/2020-04-01-QuotientGroup+Homo.tex