\documentclass[9pt]{beamer}

%\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{algorithm2e}
\usepackage{color, colortbl}
\usepackage{enumerate}
\usepackage{arydshln}
\usepackage{multirow}

\usepackage{animate}
\usepackage{tabularx}

\renewcommand{\figurename}{Fig}
\usetheme{uha}

%\theoremstyle{plain}
%  \newtheorem{theorem}{Theorem}
%  \newtheorem{lemma}{Lemma}
\newtheorem{corrolary}{Corollary}
\newtheorem{claim}{Claim}
\newtheorem{proposition}{Proposition}
\newtheorem{property}{Property}
%  \newtheorem{fact}{Fact}
%\theoremstyle{definition}
%  \newtheorem{definition}{Definition}
%  \newtheorem{example}{Example}
%\theoremstyle{remark}
\newtheorem{remark}{Remark}
\newtheorem{proviso}{Proviso}

\newcommand{\ccr}[1]{{\color{red}#1}}
\newcommand{\ccb}[1]{{\color{blue}#1}}
\newcommand{\ccp}[1]{{\color{purple}#1}}
\newcommand{\ccm}[1]{{\color{magenta}#1}}
\newcommand{\cco}[1]{{\color{orange}#1}}
\newcommand{\ccy}[1]{{\color{yellow}#1}}
\newcommand{\ccl}[1]{{\color{lime}#1}}
\newcommand{\ccc}[1]{{\color{cyan}#1}}
\newcommand{\ccg}[1]{{\color{gray}#1}}
\newcommand{\ccpk}[1]{{\color{pink}#1}}
\newcommand{\ccov}[1]{{\color{olive}#1}}

\begin{document}

%%//////////////////////////////////////////////////////////////////////////////////////////////%%1

\title{Community Detection}
\subtitle{Algorithm Design}
\author{Zhemin Huang, Xunjie Wang}
\institute{School of Software, Shanghai Jiao Tong University}
\date{\hspace{2em}}
\frame{
	\titlepage
}

%%//////////////////////////////////////////////////////////////////////////////////////////////%%1

\section{Problem Description}

\frame{
	\frametitle{Network}
	\begin{definition}
		[Network]
		A \ccp{network} is represented as \ccb{$G=(V,E,W)$},\\
		where \ccb{$V,E$} denote the set of nodes and edges, respectively, and \ccb{$W$} denotes the corresponding weights of the connections.
	\end{definition}

	\bigskip

	\centerline{\includegraphics[width=0.4\textwidth]{figures/network/network.png}}
}

\frame{
	\frametitle{Community}
	\begin{definition}
		[Community]
		Communities are the sub-graphs in a network, where nodes share dense connections. Sparsely-connected nodes cripple communities. \\
		We use \ccb{$C=\{C_1,C_2,...,C_k\}$} to denote a set of \ccb{$k$} communities divided from a network \ccb{$G$}. A node \ccb{$v$} clustered into the community \ccb{$C_i$} satisfies the condition that the internal degree of each node inside the community exceeds its external degree. \\
		For \ccb{$\forall i,j, C_i\in C, C_j\in C$}, they satisfy the following conditions
		\ccb{
			\begin{equation*}
				C_i\cap C_j=\varnothing
			\end{equation*}
			\begin{equation*}
				\bigcup_{i=1}^{k}C_i=G
			\end{equation*}
		}
	\end{definition}
}

\frame{
	\frametitle{Community}
	A community is a type of sub-graph that represents some real social phenomenon. In order words, a community is a group of people or objects, which \ccp{share common characteristics}.

	\centerline{\includegraphics[width=0.5\textwidth]{figures/network/community.png}}
}

\frame{
	\frametitle{Modularity}
	\begin{definition}
		[Modularity]
		Consider a particular division of a network into \ccb{$k$} communities. Let us define a \ccb{$k \times k$} symmetric matrix \ccb{$e$} whose element \ccb{$e_{ij}$} is the fraction of all edges in the network that link vertices in community \ccb{$i$} to vertices in community \ccb{$j$}.\\
		We further define the row sums \ccb{$a_i=\sum_{j}e_{ij}$}, which represent the fraction of edges that connect to vertices in community \ccb{$i$}.\\
		In a network in which edges fall between vertices who belong to different communities, we would have \ccb{$e_{ij}=a_ia_j$}. Let \ccb{$||X||$} indicates the sum of the elements of the matrix \ccb{$X$}.
		Thus, we define modularity by
		\ccb{
			\begin{equation*}
				Q=\sum_i{e_{ii}-{a_i}^2}=\sum_i{e_{ii}}-\sum_i{{a_i}^2}=Trace-||e^2||
			\end{equation*}
		}
	\end{definition}
}

\frame{
	\frametitle{Modularity}
	Proposed by Mark Newman, UMich.

	\noindent He is known for his fundamental contributions to the fields of complex networks and complex systems, for which he was awarded the 2014 Lagrange Prize.
	\vspace{1cm}
	\href{http://www-personal.umich.edu/~mejn/}{
		\centerline{\includegraphics[width=0.45\textwidth]{figures/gn/newman.jpg}}
	}
}

\frame{
	\frametitle{Modularity}
	Modularity was designed to measure the strength of division of a network into communities.

	\noindent If \ccb{$Q$} is high, we get strong community structure.

	\noindent In practice, values for such networks typically fall in the range from about \ccb{0.3} to \ccb{0.7}.
	\centerline{\includegraphics[width=0.5\textwidth]{figures/network/modularity.png}}
}

\frame{
	\frametitle{Reformulation of The Modularity}
	In order to raise \ccp{a spectral algorithm for community detection}, Newman redefined the concept of modularity.
	\begin{definition}
		[Reformulation of The Modularity]
		The modularity of a community partition is a scalar ranging from \ccb{$-\frac{1}{2}$} to \ccb{$1$} that evaluates the density of links inside communities as compared to links between communities. \\
		For a given graph \ccb{$G$}. Let \ccb{$A_{ij}$} be the weight of the edge between \ccb{$i$} and \ccb{$j$}, \ccb{$k_{i}=\sum_{(i, j) \in E}A_{ij}$} is the sum of the weights of all the edges attached to vertex \ccb{$i$},  \ccb{$m = \frac{1}{2}\sum_{ij}A_{ij}$}, the quantity \ccb{$\frac{1}{2}(s_is_j+1)$} is \ccb{1} if \ccb{$i$} and \ccb{$j$} are in the same group and \ccb{0} otherwise, \\
		\ccb{
			\begin{equation*}
				Q = \frac{1}{4m}\sum_{i \in V, j \in V}(A_{ij} - \frac{k_{i}k_{j}}{2m})\frac{1}{2}(s_is_j+1)
			\end{equation*}}
	\end{definition}
	The leading factor of \ccb{$\frac{1}{4m}$} seems to be confusing. Actually, it is included for compatibility with the previous definition of modularity.
}

\frame{
	\frametitle{Community Detection}
	From the definitions above, we can lead to our goal -- \ccp{community detection}.
	\begin{definition}
		[Community detection]
		Community detection is a method to extract communities from large networks when the modularity is optimal.
	\end{definition}
	Not all methods are based on modularity, such as \ccp{Infomap}.

	\noindent Modularity has its limits, but it is still the most commonly used method for evaluating results.
}

\section{Key Properties}

\frame{
	\frametitle{Modularity}
	\begin{lemma}
		We denote the set of all possible communities of a graph \ccb{$G$} with \ccb{$A(G)$}, \ccb{$Q(P)$} refers to the modularity of \ccb{$P$}. \\
		Let \ccb{$G$} be an undirected and unweighted graph and \ccb{$P \in A(G)$}. \\
		Then \ccb{$-\frac{1}{2}\le Q(P) \le 1$} holds.
	\end{lemma}
}

\frame{
	\frametitle{Proof}
	\ccm{\em Proof.}
	Let \ccb{$m_i=|E(P)|$} be the number of edges inside community \ccb{$P$} and \ccb{$m_e=\sum_{P\neq P'\in P}|E(P, P')|$} be the number of edges having exactly one end-node in \ccb{$P$}. \\
	Then we can calculate the contribution of \ccb{$P$} to \ccb{$Q(P)$}
	\ccb{
		\begin{equation*}
			\frac{m_i}{m}-(\frac{m_i}{m}+\frac{m_e}{2m})^2=\frac{-4(m_i)^2+4m_i(m-m_e)-(m_e)^2}{4m^2}
		\end{equation*}
	}

	It is obvious that the only maximum point is at \ccb{$m_i=\frac{m-m_e}{2}$}. The contribution of a community is minimized when \ccb{$m_i$} is zero and \ccb{$m_e$} is as large as possible. \\
	Suppose now \ccb{$m_i=0$}, the upper bound can only be actually attained in the specific case of a graph with no edges, where coverage is defined to be \ccb{1}. \\
	Besides, any bipartite graph \ccb{$K_{a,b}$} with simple communities \ccb{$C=\{C_a, C_b\}$} yields the minimum modularity of \ccb{$\frac{1}{2}$}. This proof the lemma.
}

\frame{
	\frametitle{Modularity}
	\begin{theorem}
		Modularity is \ccb{\textbf{NP-complete}}.
	\end{theorem}
}

\frame{
	\frametitle{Proof(1)}
	We formalize the problem of finding modularity, and prove it by \ccb{\textbf{reduction}}.

	\begin{block}{Problem 1 (Modularity)}
		Given a graph \ccb{$G$} and a number \ccb{$K$}, is there a community    \ccb{$P\in G$}, for which \ccb{$Q(P)\ge K$}? (\ccb{$K\in [-\frac{1}{2},1]$}) ?
	\end{block}

	\begin{block}{Problem 2 (3-Partition)}
		Given \ccb{$3k$} positive integer numbers \ccb{$a_1,...,a_{3k}$} such that the sum \ccb{$\sum_{i=1}^{3k}a_i=kb$}, and \ccb{$\frac{b}{4} < a_i < \frac{b}{2}$}, for an integer \ccb{$b$} and for all \ccb{$i=1,...,3k$}, is there a partition of these numbers into \ccb{$k$} sets, such that the sum of the numbers in each set equals to \ccb{$b$}?
	\end{block}
}

\frame{
\frametitle{Proof(2)}
It has been proved that 3-partition problem is a NP-Complete problem.

Then we show that an instance \ccb{$A={a_1,...,a_{3k}}$} of 3-partition problem can be transformed into an instance \ccb{$G(A), K(A)$} of modularity problem.

Given an instance \ccb{$A$} of 3-partition, we can construct a graph \ccb{$G(A)$} with \ccb{$k$} cliques \ccb{$H_1,...,H_k$} of size \ccb{$a=\sum_{i=1}^{3k} a_i$} each. For each element \ccb{$a_i\in A$}, we introduce a single element node in graph, and
connect it to \ccb{$a_i$} nodes in each of the \ccb{$k$} cliques, where each clique member is connected to exactly one element node. Therefore, each clique node has degree \ccb{$a$}, and the element node corresponding to element \ccb{$a_i\in A$} has degree
\ccb{$ka_i$}. The number of edges in \ccb{$G(A)$} is \ccb{$m=\frac{k}{2}·a(a+1)$}.
}

\frame{
	\frametitle{Proof(3)}
	Then we construct \ccb{$K(A)$}. Since graph \ccb{$G(A)$} has exactly \ccb{$k$} cliques, it has exactly \ccb{$(k-1)a$} inter-community edges, so the edge contribution is given by
	\ccb{
		\begin{equation*}
			\begin{split}
				\sum_{C\in P}\frac{|E(C)|}{m}=\frac{m-(k-1)a}{m}
			\end{split}
			\begin{split}
				=1-\frac{2(k-1)a}{ka(a+1)}=1-\frac{2k+2}{k(a+1)}
			\end{split}
		\end{equation*}
	}
	Therefore, communities \ccb{$P=(C_1,...,C_k)$} with maximum modularity must minimize \ccb{$d(C_1)^2+d(C_2)^2+...+d(C_k)^2$}, where \ccb{$d(C_k)$} refers to the degree of \ccb{$C_k$}. Then the sum of degrees per community should be as small as possible.

	In the optimum case, we can assign to each community element nodes corresponding to elements that sum to \ccb{$b=\frac{1}{k}·a$}. In each clique, the sum equals to \ccb{$k·\frac{1}{k}·a=a$}.
	\ccb{
		\begin{equation*}
			d(C_1)^2+...+d(C_k)^2\ge k(a^2+a)^2 = ka^2(a+1)^2
		\end{equation*}
	}
}

\frame{
	\frametitle{Proof(4)}
	Hence, if there exist communities \ccb{$P$} with \ccb{$Q(P)$}, then
	\ccb{
		\begin{equation*}
			K(A)\ge 1-\frac{2k-2}{k(a+1)}-\frac{ka^2(a+1)^2}{k^2a^2(a+1)^2} = \frac{(k-1)(a-1)}{k(a+1)}
		\end{equation*}
	}

	As each element node is contained in exactly one community, this yields a solution for the instance of 3-partition. The instance of 3-partition is satisfiable if the instance of modularity is satisfiable.

	Otherwise, suppose the instance for 3-partition is satisfiable. Then there exists a partition into \ccb{$k$} sets, where the sum over each set is \ccb{$\frac{1}{k}·a$}. If we detect communities by joining the element nodes of each set with a different clique, we get communities of modularity \ccb{$K(A)$}. Therefore, the instance of modularity is satisfiable if the instance of 3-partition is satisfiable.

	Therefore, the theorem holds.
}

\section{Girvan-Newman Algorithm}

\frame{
	\frametitle{Edge Betweenness}
	\begin{definition}
		\ccp{Edge betweenness} of an edge \ccb{$(i,j)$} is the number of the shortest paths between pairs of vertices that pass through the edge \ccb{$(i,j)$}.
	\end{definition}\pause

	\cco{Intuition.}
	An edge with a high edge betweenness score represents a bridge-like connector between two parts of a network, and the removal of which may affect the communication between many pairs of nodes through the shortest paths between them.

	\bigskip

	\centerline{\includegraphics[width=0.65\textwidth]{figures/gn/eb.png}}
}

\frame{
	\frametitle{Edge Betweenness}

	\begin{itemize}
		\item \ccb{$d_{s, i}$} is the \ccp{length of the shortest path} between vertex \ccb{$s$} and vertex \ccb{$i$}.
		\item \ccb{$w_{s, i}$} is the \ccp{number of the shortest paths} from vertex \ccb{$s$} to vertex \ccb{$i$}.
		\item \ccb{$b_{s, i}$} is the \ccp{number of the shortest paths} between vertex \ccb{$s$} to any vertex in graph that pass through vertex \ccb{$i$}.
	\end{itemize}\pause

	Edge betweenness \ccb{$\sigma_{i,j}$} of edge \ccb{$(i,j)$}:
	\ccb{
		\begin{equation*}
			\sigma_{i,j} = \sum_{s \in V}\sigma_{s,i,j}
		\end{equation*}
	} where
	\ccb{
		\begin{equation*}
			\sigma_{s,i,j} = \begin{cases}
				\frac{w_{s,j}}{w_{s,i}}b_{s,i} & d_{s,i} > d_{s,j} \\
				\frac{w_{s,i}}{w_{s,j}}b_{s,j} & d_{s,i} < d_{s,j} \\
				0                              & d_{s,i} = d_{s,j}
			\end{cases}
		\end{equation*}
	}
}

\frame{
	\frametitle{Breath First Search}

	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Breath-First-Search}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E), s$}
			\BlankLine
			$d_{s,s} \leftarrow 0, w_{s,s} \leftarrow 1, b_{s,s} \leftarrow 0$,
			$Q \leftarrow \{s\}, L \leftarrow \{s\}$\;
			\For{each vertex $v \in V - \{s\}$}{
				$d_{s,v} \leftarrow \infty, w_{s,v} \leftarrow 0, b_{s,v} \leftarrow 1$\;
			}
			\While{$Q$ is not empty}{
			Dequeue $i \leftarrow Q$\;
			\For{each vertex $j$ where $(i,j) \in E$}{
			\If{$d_{s,j} \neq \infty$ and $d_{s,j} = d_{s,i}+1$}{
			$w_{s,j} = w_{s,j}+w_{s,i}$\;
			}
			\If{$d_{s,j}=\infty$}{
				$d_{s,j} = d_{s,i}+1, w_{s,j} = w_{s,i}$, Enqueue $j \rightarrow Q$, Push $j \rightarrow L$\;
			}
			}
			}
			\Return $(d,w,s,L)$\;
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Reverse Breath First Search}

	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Reverse-Breath-First-Search}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E),s,w,d,L$}
			\BlankLine
			\While{$L$ is not empty}{
			Pop $i \leftarrow L$\;
			\For{each vertex $j$ where $(i,j) \in E$}{
			\If{$d_{s,i} < d_{s,i}$}{
				$b_{s,i} = 1+\sum_{j}\sigma_{s,i,j}$\;
			}
			\If{$d_{s,i} > d_{s,j}$}{
			$\sigma_{s,i,j} = \frac{w_{s,j}}{w_{s,i}}b_{s,i}$\;
			}
			}
			}
			\Return $\sigma$\;
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Girvan-Newman Algorithm}

	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Girvan-Newman}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E)$}
			\BlankLine
			Calculate edge betweenness for each edge in the graph\;
			\While{$E$ is not empty}{
				Let $e$ be the edge with highest edge betweenness\;
				$E \leftarrow E - \{e\}$\;
				Calculate edge betweenness for remaining edges\;
			}
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Review of Modularity}
	\begin{definition}
		\ccp{Modularity} of a network is a scalar ranging from \ccb{$-\frac{1}{2}$} to \ccb{$1$} that evaluates the density of links inside communities as compared to links between communities.
		\ccb{
			\begin{equation*}
				Q = \frac{1}{4m}\sum_{i \in V, j \in V, c_i = c_j}[A_{ij} - \frac{k_{i}k_{j}}{2m}]
			\end{equation*}
		}
	\end{definition}

	\begin{itemize}
		\item \ccb{$A_{ij}$} is the weight of the edge between \ccb{$i$} and \ccb{$j$}.
		\item \ccb{$k_{i} = \sum_{(i, j) \in E}A_{ij}$} is the sum of the weights of all the edges attached to vertex \ccb{$i$}.
		\item \ccb{$m = \frac{1}{2}\sum_{ij}A_{ij}$},
		\item \ccb{$c_{i}$} is the community of vertex \ccb{$i$}.
	\end{itemize}

	\bigskip
}

\frame{
	\frametitle{Girvan-Newman Algorithm}

	\begin{itemize}
		\item Girvan-Newman algorithm computes the modularity of current community partition, and determined to terminate when the modularity of the resulting partition reaches a \ccp{maximum}.
		\item In complex networks it is often the case that more edges have the \ccp{same highest} edge betweenness. We can remove these edges \ccp{together} to effectively reduce the number of iteration.
	\end{itemize}

	\bigskip
}

\frame{
	\frametitle{Girvan-Newman Algorithm with Modularity}

	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Modularity-Girvan-Newman}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E)$}
			\BlankLine
			Calculate edge betweenness for each edge in the graph\;
			Calculate modularity $Q$\;
			\While{$E$ is not empty}{
				Let $E'$ be the set of all edges with the highest edge betweenness\;
				Calculate modularity $Q'$ of $G=(V, E - E')$\;
				\If{$Q' < Q$}{
					Break\;
				}
				$E \leftarrow E - E'$\;
				Calculate edge betweenness for remaining edges\;
			}
			\Return{(V, E)}\;
		\end{algorithm}
	\end{exampleblock}

	\bigskip
}

\frame{
	\frametitle{Example}
	Let's start with a simple network.
	\bigskip
	\centerline{\includegraphics[width=0.45\textwidth]{figures/gn/e0.jpg}}
}

\frame{
	\frametitle{Example}
	Start from vertex \ccb{$A$} and BFS the network to get the shortest paths.
	\bigskip
	\centerline{\includegraphics[width=1.1\textwidth]{figures/gn/e1_3.png}}
}

\frame{
	\frametitle{Example}
	Calculate the \ccp{number of the shortest paths} from vertex \ccb{$A$} that pass through vertex \ccb{$u$}.
	\bigskip
	\centerline{\includegraphics[width=0.35\textwidth]{figures/gn/e4.jpg}}
}

\frame{
	\frametitle{Example}
	Reverse BFS to calculate the \ccp{number of the shortest paths} from vertex \ccb{$A$} that pass through edge \ccb{$e$}.
	\bigskip
	\centerline{\includegraphics[width=0.85\textwidth]{figures/gn/e5.jpg}}
}

\frame{
	\frametitle{Example}
	Reverse BFS to calculate the \ccp{number of the shortest paths} from vertex \ccb{$A$} that pass through edge \ccb{$e$}.

	\bigskip

	\centerline{\includegraphics[width=0.55\textwidth]{figures/gn/e6.jpg}}
}

\frame{
	\frametitle{Example}
	Repeat for each vertex and sum them up.

	\bigskip

	\centerline{\includegraphics[width=0.55\textwidth]{figures/gn/e7.jpg}}
}

\frame{
	\frametitle{Example}
	Remove all edges with the highest edge betweenness.
	\bigskip
	\centerline{\includegraphics[width=1\textwidth]{figures/gn/e8.jpg}}

	And then we can get three communities.
}

\section{Louvain Algorithm}

\frame{
	\frametitle{Louvain Algorithm}
	\begin{itemize}
		\item Louvain is a multistep technique based on a \ccp{local optimization} of Newman-Girvan modularity in the neighborhood of each node.
		\item After a partition is identified in this way, communities are replaced by \ccp{supernodes}, yielding a smaller weighted network.
		\item The procedure is then iterated, until modularity does not increase any further.
	\end{itemize}\pause
	\centerline{\includegraphics[width=0.9\textwidth]{figures/louvain/v0.png}}
}

\frame{
	\frametitle{Modularity Increment}
	\begin{definition}
		\ccp{Modularity increment} \ccb{$\Delta{Q}$} of \ccb{$(i,j)$} is the \ccp{change of modularity} when removing vertex \ccb{$i$} from its community and then placing it to the community of its neighbor \ccb{$j$}.
	\end{definition}
	\pause
	This removing-placing action can be decomposed into two similar actions:
	\begin{itemize}
		\item [1)] making vertex \ccb{$u$} isolated
		\item [2)] moving the isolated vertex into a community
	\end{itemize}
	\ccb{$\Delta{Q}$} is the sum of \ccb{$\Delta{Q}'$} of these two actions. These two \ccb{$\Delta{Q}'$} can be computed in the same way, for 1) is just the inverse action of 2).
}

\frame{
	\frametitle{Modularity Increment}
	\ccb{
		\begin{equation*}
			\Delta{Q}' = \bigg[\frac{\sum_{in} + k_{i, in}}{2m} - \bigg(\frac{\sum_{tot} + k_{i}}{2m}\bigg)^{2}\bigg] - \bigg[\frac{\sum_{in}}{2m} - \bigg(\frac{\sum_{tot}}{2m}\bigg)^{2} - \bigg(\frac{k_{i}}{2m}\bigg)^{2}\bigg]
		\end{equation*}
	}
	where
	\ccb{
		\begin{itemize}
			\item $\sum_{in}$ is the sum of the weights of the internal edges of $c_{j}$
			\item $\sum_{tot}$ is the sum of the weights of the links incident to vertices in community $c_{j}$
			\item $k_{i, in}$ is the sum of the weights of the links from $i$ to vertices in community $c_{j}$
		\end{itemize}
	}
}

\frame{
	\frametitle{Louvain Algorithm}
	Each iteration, also named \ccp{pass} in Louvain, has two phases.
	\begin{itemize}
		\item [1)] Modularity Optimization
		\item [2)] Community Aggregation
	\end{itemize}

	\centerline{\includegraphics[width=0.65\textwidth]{figures/louvain/2phase.png}}
}

\frame{
	\frametitle{Modularity Optimization}
	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Modularity-Optimization}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E)$}
			\BlankLine
			\ForEach{$u \in V$}{
				Let $v$ be the neighbor who has the largest modularity increment\;
				\If{this increment is positive}{
					$c_{u} \leftarrow c_{}$
				}
			}
			\Return{c}
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Example}
	Here is the visualization of phase 1.
	\bigskip
	\centerline{\includegraphics[width=1\textwidth]{figures/louvain/phase1.png}}
}

\frame{
	\frametitle{Community Aggregation}
	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Community-Aggregation}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E),c$}
			\BlankLine
			\ForEach{unmerged vertex $u$}{
				\ForEach{unmerged vertex $v \in V - \{u\}$ and $c_{u} = c_{v}$}{
					Merge vertex $v$ to $u$\;
				}
				Update internal and external weights of edges\;
			}
			\Return{G,c}
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Example}
	Here is the visualization of phase 2.
	\begin{itemize}
		\item The weights of edges between two new vertices are determined by sum of the weights of the edges between vertices in the corresponding two communities.
		\item The internal edges of a community leads to a self-loop for the corresponding vertex in the new network.
	\end{itemize}
	\centerline{\includegraphics[width=1\textwidth]{figures/louvain/phase2.png}}
}

 \frame{
	\frametitle{Visualization of Louvain Algorithm}

	% \centerline{\includemovie{1cm}{1cm}{figures/karoake_louvain.gif}}
	% \animategraphics[width=10cm,height=10cm, autoplay, loop, controls]{24}{figures/louvain/louvain-}
 	\centerline{\animategraphics[width=12cm,height=6cm,autoplay,loop,controls]{36}{figures/louvain_gif/louvain-}{0}{620}}
 }

\section{Parallelization of Louvain Algorithm}

\frame{
	\frametitle{Parallelization of Louvain Algorithm}
	\begin{itemize}
		\item In the original \ccp{serial} algorithm, each vertex examines the communities of its neighbors and makes a choice to chooses a new community based on a function to maximize the calculated change in modularity.
		\item In the \ccp{distributed} and \ccp{parallel} version, all vertices make this choice simultaneously rather than in serial order, updating the graph state after each change.
	\end{itemize}
	\vspace{5mm}
	\centerline{\includegraphics[width=0.4\textwidth]{figures/louvain/spark.jpeg}}

}

\frame{
	\frametitle{Parallelization of Louvain Algorithm}
	\begin{exampleblock}{}
		\begin{algorithm}[H]
			\SetKwData{x}{x}\SetKwData{y}{y}\SetKwData{z}{z}
			\SetKwFunction{CS}{\sc Parallel-Louvain}\SetKwFunction{Return}{\sc Return}\SetKwFunction{Init}{\sc Initialize}
			\SetKwFunction{Up}{\sc Update}\SetKwFunction{Au}{\sc Augment}
			\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
			\CS{$G=(V,E)$}
			\BlankLine
			\While{True}{
				\ForEach{vertex $u \in V$}{
					Modularity Optimization on vertex $u$ in parallel to get modularity increment $\Delta{Q}_{u}$\;
				}
				$\Delta{Q} \leftarrow \sum_{u \in V}\Delta{Q}_{u}$\;
				\If{$\Delta{Q} \le 0$}{
					Break\;
				}
				Community-Aggregation(G,c)\;
			}
		\end{algorithm}
	\end{exampleblock}
}

\frame{
	\frametitle{Parallelization of Louvain Algorithm}
	The flowchart of the parallel Louvain algorithm:
	\centerline{\includegraphics[width=1\textwidth]{figures/louvain/p_flowchart.jpg}}
}

\frame{
	\frametitle{Parallelization of Louvain Algorithm}
	Practically, It is a good choice to implement a parallel algorithm on \ccp{Spark}, which is a cluster computing framework supporting reusing a working set of data across multiple parallel operations while retaining the scalability and fault tolerance of \ccp{MapReduce}.

	To describe the algorithm using MapReduce framework:
	\begin{itemize}
		\item [1)] Get information of adjacent vertices.
		      \begin{itemize}
			      \item [a)] Map: produce information of adjacent vertices \ccb{$\text{VertexData}$};
			      \item [b)] Reduce: get information of adjacent vertices \ccb{$(\text{Id}, \text{Array}[\text{VertexData}])$}.
		      \end{itemize}
		\item [2)] Get the new community \ccb{$(\text{Id}, \text{getBestCommunity}(\text{Array}[\text{VertexData}]))$}
		\item [3)] Update the information in the network and merge vertices to go to next iteration.
	\end{itemize}
}

\section{Improvement of Louvain Algorithm}

\frame{
	\frametitle{Drawbacks of Louvain Algorithm}
	In the Louvain algorithm, a node may be moved to a different community, while it may have acted as a "bridge" between different communities. Removing such a node disconnects the nodes in the old community.

	\vspace{5mm}

	\centerline{\includegraphics[width=0.8\textwidth]{figures/louvain/louvain_drawback.png}}
}

\frame{
	\frametitle{Leiden Algorithm}
	The Leiden algorithm is more complex than the Louvain algorithm. It consists of three phases:
	\begin{itemize}
		\item [1)] Local moving of nodes.
		\item [2)] Refinement of the partition.
		\item [3)] Aggregation of the network based on the refined partition, use the non-refined partition to create an initial partition for the aggregate network.
	\end{itemize}
}

\frame{
	\frametitle{Leiden Algorithm}

	\centerline{\includegraphics[width=0.5\textwidth]{figures/louvain/leiden.png}}

	\begin{itemize}
		\item The Leiden algorithm starts from a singleton partition (\ccb{$a$}).
		\item The algorithm moves individual nodes from one community to another to find a partition (\ccb{$b$}), which is then refined (\ccb{$c$}).
		\item An aggregate network (\ccb{$d$}) is created based on the refined partition, using the non-refined partition to create an initial partition for the aggregate network.
	\end{itemize}
}

\frame{
	\frametitle{Leiden Algorithm}

	\centerline{\includegraphics[width=0.5\textwidth]{figures/louvain/leiden.png}}

	\begin{itemize}
		\item The algorithm then moves individual nodes in the aggregate network (\ccb{$e$}).
		\item In this case, refinement does not change the partition (\ccb{$f$}).
		\item These steps are repeated until no further improvements can be made.
	\end{itemize}
}

\frame{
	\frametitle{Comparison Between Louvain and Leiden}

	\centerline{\includegraphics[width=1.05\textwidth]{figures/louvain/compare.jpg}}
}

\section{Applications of Community Detection}

\frame{
	\frametitle{Applications in social networks}
	An social network is the interaction of people with each other through the web. Community detection has been widely used in this area.

	Various studies have utilized different community detection methods to analyze public emotional reaction and visualize relationships and characteristics.
	\centerline{\includegraphics[width=1\textwidth]{figures/app/combine.png}}
}

\frame{
	\frametitle{Applications in biological networks}
	Researchers have proposed an algorithm called \ccp{disease-gene network detecting algorithm}, based on Principal Component Analysis (PCA), which can extract the communities in a diseasome bipartite network.

	This algorithm is aimed at disease prevention and medical diagnosis.
	\centerline{\includegraphics[width=0.9\textwidth]{figures/app/bio.png}}
}

\frame{
	\frametitle{Applications in economics}
	In the \ccp{stock market}, each stock can be represented by a vertex and edge represents the correlations of stock values in the market. Researchers have stated the way to construct the network of stock market and detect communities in it.
	They revealed community structure by using modularity \ccb{$Q$}, which helps to the analysis and decision-making of the stock market.

	\begin{figure}
	\centerline{\includegraphics[width=0.75\textwidth]{figures/app/stock.png}}
	\caption{Several pictures of stock networks. Different node colors represent different communities and node sizes reflect its degree.}
	\end{figure}

}

\section{Conclusion}

\frame{
	\frametitle{Future Direction}
	\ccp{Deep learning} --- a promising direction of community detection. Beyond simply examining network topologies for detecting communities, some strategies also explore semantic descriptions as node features in the data.

	\centerline{\includegraphics[width=0.65\textwidth]{figures/conc/dl.jpg}}
}

\frame{
	\frametitle{Open problems}
	There are still so many broad challenges in community detection. For example:
	\begin{itemize}
		\item Network \ccp{dynamics}
		\item \ccp{Large-scale} networks
		\item \ccp{Inaccurate} number of communities
	\end{itemize}
}

\frame{
	\frametitle{Network dynamics}
	Changing dynamics can affect either the network topology or the node attributes. Topological changes not only cause changes in a local community, but also leads to devastating changes across an entire network.

	Actually, a lot of methods have been used to deal with sequential data in machine learning, such as \ccp{long short-term memory} (\ccp{LSTM}). Therefore, deep learning methods for detecting communities with dynamic spatial and temporal properties are very likely to be developed.

	\centerline{\includegraphics[width=0.6\textwidth]{figures/conc/LSTM.png}}
}

\frame{
	\frametitle{Large-scale networks}
	Nowadays, large-scale networks can contain millions of nodes, edges, and structural patterns, as networks like Twitter and Weibo, which has also brought a lot of problems.

	For instance, large-scale networks may have their inherent characteristics, such as \ccp{scale-free}. There exists lots of mega hubs in the network, which can influence the performance of algorithms in community detection.

	\vspace{5mm}

	\centerline{\includegraphics[width=0.75\textwidth]{figures/app/scalefree.jpg}}

}

\frame{
	\frametitle{Inaccurate number of communities}
	In fact, most algorithms of the community detection require the \ccp{number of communities} beforehand as a hyperparameter, including deep learning.

	Two common solutions:
	\begin{itemize}
		\item Using \ccp{modularity-based} algorithm, like Girvan-Newman or Louvain
		\item Using \ccp{statistical} interference
	\end{itemize}

	Unfortunately, both methods have poor performance under large-scale networks.
}

\end{document}


% --- [ Distance ] -------------------------------------------------------------

\subsection{Distance}

The distance metric measures the distance in height between the recovered type and the source type in the primitive type lattice (see figure \ref{fig:primitive_type_lattice}). The calculation of distance is meaningful only for subtypes (e.g. \texttt{int32} is a subtype of \texttt{reg32} at distance 2), otherwise the maximum lattice height is used.


\documentclass[a4paper, fontsize=9pt, twocolumn]{scrreprt}

\setlength{\paperheight}{297mm}
\setlength{\paperwidth}{210mm}
\setlength{\textheight}{252mm}
\setlength{\textwidth}{172mm}
\setlength{\columnsep}{8mm}
\setlength{\headheight}{0mm}
\setlength{\voffset}{-12mm}
\setlength{\hoffset}{0mm}
\setlength{\marginparwidth}{0mm}
\setlength{\parindent}{1pc}
\setlength{\topmargin}{-5mm}
\setlength{\oddsidemargin}{-6mm}
\setlength{\evensidemargin}{-6mm}

\usepackage[utf8]{inputenc}
\usepackage[sc]{mathpazo}
\linespread{1.05}
\usepackage{helvet}
\usepackage{fullpage}

\usepackage{amsmath,amsthm,amssymb}
\usepackage[shortlabel]{enumitem}
\usepackage{nicefrac}
\usepackage{upgreek}
\usepackage{graphicx}
\graphicspath{{figs/figS02/}}

\newcommand{\vect}[1]{\mathrm{\mathbf{#1}}}
\newcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}

\newcommand{\vx}{\vect x}
\newcommand{\vr}{\vect r}
\newcommand{\vu}{\vect u}
\newcommand{\vv}{\vect v}
\newcommand{\vS}{\vect S}
\newcommand{\vX}{\vect X}
\newcommand{\vC}{\vect C}
\newcommand{\vR}{\vect R}
\newcommand{\vW}{\vect W}
\newcommand{\vomega}{\boldsymbol{\upomega}}
\newcommand{\Real}{\text{Re}}

\newcommand{\hvx}{\hat \vx}
\newcommand{\hvX}{\hat \vX}

\DeclareMathOperator{\proj}{proj}
\DeclareMathOperator{\Cov}{Cov}

\usepackage[]{xcolor}
\newcommand{\todo}[1]{{\color{red}\textbf{To do:} #1}}

\usepackage[backend=biber,style=apa,isbn=false,url=false]{biblatex}
\addbibresource{bibliography.bib}
\renewcommand*{\bibfont}{\footnotesize}
\setlength\bibitemsep{0em}

\renewcommand*{\thechapter}{S\arabic{chapter}}

\title{%
    Cosine Contours\\%
    A multipurpose representation \\ for melodies\\[2em]%
    \textsc{supplementary materials}
}
\author{Bas Cornelissen, Willem Zuidema \& John Ashley Burgoyne}
\date{}

\begin{document}

\maketitle
\newpage

\pagebreak

%=============================
\chapter{Random walk baseline}
%=============================

\hspace{-2em}
\includegraphics[width=.5\textwidth]{figs/figS01a.pdf}

\noindent
We compared the principal components of phrases to a random walk baseline that was intended to be fairly similar to actual phrase contours.
First, we draw the length (number of notes) $K$ of the random walk from a Poisson distribution with mean $\lambda=12$ (truncated below 3). The value $12$ was chosen so as to approximate the length distribution of phrases.
Then we draw an initial pitch $x_0$ uniformly between 60 and 85 (in MIDI pitch space).
Next, at every step $k$ we draw the size of a step $r_k$ (the interval) from a Binomial distribution with parameters $n=10$ and $p=0.5$, shifted to have mean 0, and let the next pitch be $x_k = x_{k-1}+r_k$.
We constrain the step sizes to lie between $-12$ and $+12$, meaning that jumps cannot exceed an octave.
This results in small, approximately normally distributed step sizes.
This process yields a sequence of pitches $x_0, \dots, x_{K-1}$.
As usual, we interpolate a step function through these pitches and sample $N=100$ equally spaced pitches to obtain a random contour.
In the figure above we use $N=50$ for readability.
\vfill\pagebreak

\hspace{-2em}
\includegraphics[width=.5\textwidth]{figs/figS01b.pdf}

\noindent
Here we vary the average length $\lambda$ of the random walk baseline.
This affects the number of notes $K$, but we still have $N=100$ throughout.
We generate 10,000 random contours, and compute the covariance matrices (A).
The longer the melodies (larger $K$), the more the covariance matrix starts to resemble a Toeplitz matrix, which has constant values along each of its diagonals.
As an ad-hoc measure of \emph{Toeplitzness}, we measure how much every entry of the covariance matrix differs from the mean value on that diagonal.
For a Toeplitz matrix, that should be zero everywhere: all diagonals are constant, so every entry also equals the mean of that diagonal.
Column (B) makes clear that the covariance matrix differs from a Toeplitz matrix mostly in the upper left corner, which contains the covariance in the first timesteps.
All this is also reflected in the principal components (C).

%===================================
\chapter{Analyses of other datasets}
%===================================

In this section we visualize the principal components of melodic material from motifs to songs in different traditions.
For every dataset we show:
\begin{enumerate}[label={\textbf{\textsc{\alph*.}}}]
    \item The first four principal components. The first one is usually a flat line (gray), the second a descending shape (blue), the third a convex shape (orange), and the fourth one undulating (green).
    The corresponding cosines are shown as thin dashed lines in the same colors.
    \item The length distribution of the melodic material, where length is measured in quarter notes. For Gregorian chant we assume all notes are quarter notes.
    \item The covariance matrix.
    \item A scatterplot showing the representations of 2000 contours in 2d cosine contour space.
    \item The reconstruction error using the discrete cosine transform compared to a principal component analysis.
\end{enumerate}
It is clear that the cosine approximation is most accurate at the phrase level.
For very short melodic fragments (such as neumes or syllables), you see clear effects of the typical number of notes.
For example, neumes often have only 2 notes, meaning there is a jump in the middle of the contour. You can see this in the principal components, but also in the covariance matrix.
Such effects are weaker, but sometimes still visible at the phrase level: German folksongs apparently often have durations of 8 quarter notes, with jumps in the middle, or after 2 of 6 quarter notes.
For complete songs, finally, the principal components are often difficult to interpret.
Only for a very large number of songs (such as when combining all chants in GregoBase) does a pattern reminiscent of the cosines emerge.
But for very small datasets, such as those in the Densmore collection, the principal components are very irregular.

\vfill
\pagebreak

\newcommand{\showdataset}[2]{%
    \subsubsection*{#2}
    \includegraphics{#1}
    \par
}

%———————————————
\section{Motifs}
%———————————————

All motifs come from Gregorian chant (responsories from  CantusCorpus).
\showdataset{motif-responsory-subset-neumes.pdf}{Neumes}
\showdataset{{motif-responsory-subset-syllables.pdf}}{Syllables}
\showdataset{{motif-responsory-subset-words.pdf}}{Words}
\pagebreak

%————————————————
\section{Phrases}
%————————————————

\showdataset{{phrase-erk-phrase-contours.pdf}}{German: Erk}
\showdataset{{phrase-han-phrase-contours.pdf}}{Chinese: Han}
\showdataset{{phrase-liber-antiphons-phrase-contours.pdf}}{Antiphons}
\pagebreak

%————————————————————————
\section{Random segments}
%————————————————————————

\showdataset{{phrase-erk-random-contours.pdf}}{German: Erk}
\showdataset{{phrase-han-random-contours.pdf}}{German: Han}
\showdataset{{phrase-liber-antiphons-random-contours.pdf}}{Antiphons}
\pagebreak

%————————————————————————————
\section{Phrases (continued)}
%————————————————————————————

\showdataset{{phrase-boehme-phrase-contours.pdf}}{German: Boehme}
\showdataset{{phrase-shanxi-phrase-contours.pdf}}{Chinese: Shanxi}
\showdataset{{phrase-liber-responsories-phrase-contours.pdf}}{Responsories}
\pagebreak

%————————————————————————————————————
\section{Random segments (continued)}
%————————————————————————————————————

\showdataset{{phrase-boehme-random-contours.pdf}}{German: Boehme}
\showdataset{{phrase-shanxi-random-contours.pdf}}{Chinese: Shanxi}
\showdataset{{phrase-liber-responsories-random-contours.pdf}}{Responsories}
\pagebreak

%——————————————
\section{Songs}
%——————————————

\showdataset{song-erk.pdf}{German: Erk}
\showdataset{song-han.pdf}{Chinese: Han}
\showdataset{song-gregobase.pdf}{All chants in GregoBase}
\vfill\pagebreak

\section*{~}
\showdataset{song-boehme-altdeutsches-liederbuch.pdf}{German: Boehme}
\showdataset{song-shanxi.pdf}{Chinese: Shanxi}
\vfill\pagebreak

%——————————————————————————
\section{Songs (continued)}
%——————————————————————————

\showdataset{song-densmore-teton-sioux.pdf}{Teton Sioux (Densmore)}
\showdataset{song-densmore-nootka.pdf}{Nootka (Densmore)}
\showdataset{song-densmore-papago.pdf}{Papago (Densmore)}
\showdataset{song-densmore-menominee.pdf}{Menominee (Densmore)}

%================================
\chapter{Mathematical background}
%================================

In this section we provide some more mathematical background to illustrate why we observe cosine-shaped principal components.
The aim is to make some of the key points a bit more accessible; we refer to \textcite{Jolliffe2002} for a detailed discussion of principal component analysis, to \textcite{Gray2006} for a rigorous treatment of Toeplitz matrices and their limiting behaviour, and to \textcite{Rao1990} for the discrete cosine transform.

\paragraph{Notation}
%-------------------

We write $N$ for the length of a contour, or the number of steps in a random walk, and $M$ denotes the number of contours.
Consider a dataset $\{\vx_1, \dots, \vx_M\}$ of points $\vx_m = (x_{m1}, \dots, x_{MN})$ in $\R^N$.
We denote the sample mean by $\bar \vx$ and the centered data points by $\hat \vx_m$:
\begin{align}
    \bar \vx = \frac{1}{M} \sum_{m=1}^M \vx_m
    \qquad \text{and} \qquad
    \hat \vx_m= \vx_m - \bar \vx,
\end{align}
and both of course live in $\R^N$.
An $M \times N$ matrix $\vX$ has entries $(\vX)_{m, n} = x_{mn}$, and for $N\times N$ matrices we generally index rows by $n$ and columns by $k$.

%—————————————————————————————
\section{Principal components}
%—————————————————————————————

\paragraph{Maximize projected variance}
%--------------------------------------

The goal of a principal component analysis is to find a subspace of lower dimensionality $D < N$ that maximizes the variance of the data when it is projected on this subspace.
First, we project the data on a one-dimensional subspace spanned by the unit vector $\vu_1 \in \R^N$.
You can think of the projection of $\vx_n$ as a point in the $N$-dimensional ambient space, but we rather treat it as the scalar $\vu_1^T \vx_n$: the coordinate in the one-dimensional subspace.
The projected data then has mean $\vu_1^T\bar \vx$ and variance
\begin{align}
    \frac{1}{M} \sum_{m=1}^M \bigl( \vu_1^T \vx_m - \vu_1^T \bar\vx \bigr)^2
    =\vu_1^T \vS \vu_1,
\end{align}
where $\vS$ is the $N\times n$ covariance matrix given by
\begin{align}
    \vS
        = \frac{1}{M} \sum_{m=1}^M \vx_m - \bar\vx)(\vx_m - \bar\vx)^T
\end{align}
We want to choose $\vu_1$ in such a way that it maximizes the projected variance $\vu_1^T\vS\vu_1$.
It can be shown, using a Lagrange multiplier, that under the constraint $\|\vu_1\| = 1$, the projected variance is maximized when
\begin{align}
    \label{eq:pca-eigen-vector}
    \vS \vu_1 = \lambda_1 \vu_1
\end{align}
\parencite[see e.g.~]{Jolliffe2002}.
Left-multiplying by $\vu_1^T$, and using that $\vu_1^T \vu_1 = 1$, this is the case when
\begin{align}
    \label{eq:pca-variance}
    \vu_1^T \vS \vu_1 = \lambda_1.
\end{align}
Equation \eqref{eq:pca-eigen-vector} shows that $\vu_1$ must be an eigenvector of the covariance matrix $\vS$ corresponding to eigenvalue $\lambda_1$, which is exactly the projected variance according to \eqref{eq:pca-variance}.
 The first principal component, in short, is the eigenvector of the covariance matrix corresponding to the largest eigenvalue.
The argument can be extended inductively to identify all principal components as eigenvectors of the covariance matrix, ordered according to their eigenvalues.

\paragraph{Minimize reconstruction error}
%----------------------------------------

It should be noted that one can also motivate principal components in another way.
Consider a dataset $\{x_m\in \R^N\}_m$ as before, and a set of basis vectors $\{\vu_1, \dots, v_N\}$ for $\R^N$ with norm 1.
As before, the projection of $\vx$ on the $\vu_n$ is $c_n = \vu_n^T\vx$, and so we can represent $\vx$ as a coordinate vector $(c_0, \dots, c_N)$.
Now suppose we only use the first $D$ coordinates to represent $\vx$, so we get the truncated representation:
\begin{align}
    \tilde\vx = \sum_{i=1}^D c_i \vu_i.
\end{align}
Now measure the \emph{reconstruction error} as
\begin{align}
    \textsc{mse} = \frac{1}{M}\sum_{m=1}^M (\vx - \tilde \vx)^2
\end{align}
We ask: how should we choose the basis vectors so that the reconstruction error \textsc{mse} is minimized?
The answer is the same: as the eigenvectors, ranked in descending order \parencite{Rao1990}.

%————————————————————————————————————————
\section{Toeplitz and circulant matrices}
%————————————————————————————————————————

\emph{Toeplitz matrices} are matrices were every diagonal has the same value.
They are usually indexed as follows:
\begin{align}
    \vect T =
    \begin{bmatrix}
    t_0     & t_{-1}    & t_{-2}    &\dots  & t_{-(N-1)}\\
    t_1     & t_{0}     & t_{-1}    &       & \\
    t_2     & t_1       &t_0        &       & \vdots\\
    \vdots  &           &           &\ddots & \\
    t_{N-1} &           &           &\dots  &t_0
    \end{bmatrix}
\end{align}
That means that $T_{i, j} = t_{j-i}$.
Before we discuss Toeplitz matrices further, let's focus on the special subset of circulant matrices.
A \emph{circulant matrix} is a Toeplitz matrix where every row equals the previous row, rotated one step to the right:
\begin{align}
    \vC =
    \begin{bmatrix}
        c_0     & c_1   & c_2   & \dots & c_{N-1} \\
        c_{N-1} & c_0   & c_1   &       & c_{N-2} \\
        c_{N-2} & c_{N-1} & c_0 & \\
        \vdots  &       & \ddots      &&\vdots\\
        &&&c_0&c_1\\
        c_1 &&\dots &c_{N-1}&c_0
    \end{bmatrix}
\end{align}
It is convenient to start indexing at 0 rather than 1, so that we have $\vC_{n, k} = c_{k-n \mod N}$.
We read the subscripts periodically, so that e.g. $c_{N+3} = c_3$.
For circulant matrices, matrix multiplication takes the form of a \emph{circular convolution}: if $\vect y = \vC \vx$, we have
\begin{align}
    \label{eq:circular-matrix-multiplication}
    y_n = \sum_{k=0}^{N-1} c_{k-n} x_k.
\end{align}

\begin{figure}
    \centering
    \includegraphics[width=.5\textwidth]{figs/suppl-fig-roots-of-unity.pdf}
    \caption{The $N$-th roots of unity for $N=5$ are points on the complex unit circle.}
    \label{fig:roots-of-unity}
\end{figure}

\paragraph{Eigenvectors of circulant matrices}
%---------------------------------------------

Suprisingly, all circulant matrices have the same eigenvectors.
These eigenvectors consist of \emph{($N$-th) roots of unity}: the complex numbers $z$ satisfying $z^N = 1$.
The first complex root of unity is
\begin{align}
    \omega = e^{\frac{2\pi i}{N}},
\end{align}
and its powers $\omega^k$ are other roots of unity, since $(\omega^k)^N = (\omega^N)^k = 1$.
The numbers $\omega^0, \dots, \omega^{N-1}$ can be visualized as evenly spaced points on the unit circle in the complex plane (see figure \ref{fig:roots-of-unity}).
Importantly, these numbers (like the coefficients $c_k$) are periodical: $\omega^{N+k} = \omega^N \cdot \omega^k = \omega^k$.

This property allows us to show that the $N$ eigenvectors of a circulant matrix are
\begin{align}
    \vomega_n = (\omega^{n \cdot 0}, \dots, \omega^{n\cdot(N-1)}),
 ,\end{align}
for $n=0, \dots, N-1$.
You can verify this directly when $n=0$, since $\vomega_0$ is then an an all-ones vector, but let's consider the general case.
We have to show that $\vC \vomega_n = \lambda_n \vomega_n$ for some constant $\lambda_n$.
Using \eqref{eq:circular-matrix-multiplication}, we can show that $k$'the entry of the left hand side indeed equals $\lambda_n \omega^{nk}$:
\begin{align}
    (\vC \vomega_n)_k
        &= \sum_{j=0}^{N-1} c_{j-k} \cdot \omega^{n \cdot j} \\
        &= \omega^{nk} \cdot \sum_{j=0}^{N-1} c_{j-k}  \cdot \omega^{n(j-k)} \\
        &= \omega^{nk} \cdot \underbrace{
            \sum_{j'=0}^{N-1} c_{j'}  \cdot \omega^{n\cdot j'}
        }_{\lambda_n}.
\end{align}
Here we first multiplied by $\omega^{-nk}/\omega^{-nk}$ to align the indices of the coefficients and the powers.
Then we used the periodicity of the roots of unity to reorder the sum,
so it no longer depends on $k$ and must equal the eigenvalue $\lambda_n$.
The general case is similar.

Summarizing, every $N\times N$ circulant matrix $\vC$ has the same $N$ eigenvectors $\vomega_0, \dots, \vomega_n$, with (different) corresponding eigenvalues:
\begin{align}
    \label{eq:eigen-pair-circulant-matrix}
    \lambda_n
        &= c_0 \omega^0 + c_1 \omega^{n} \dots c_{N-1} \omega^{n(N-1)} \\
        &=
        \sum_{j=0}^{N-1} c_j e^{\frac{2\pi i \cdot nj}{N}},
\end{align}
for $n=0, \dots, N-1$.
From the second expression one sees that the eigenvalues $(\lambda_0, \dots, \lambda_{N-1})$ are the discrete Fourier transform of $(c_0, \dots, c_{N-1})$.

\paragraph{Real circulant matrices}
%----------------------------------

In the scenario we are interested in, the matrix $\vC$ is real and symmetric, and such matrices have real eigenvalues and eigenvectors.
To see that the eigenvalues are real, first note that a symmetric circulant matrix satisfies the additional constraint $c_k = c_{N-k}$.
Also observe that $\omega^k$ and $\omega^{N-k} = \omega^{-k}$ are each others mirror image in the real axis (see figure \ref{fig:roots-of-unity}).
They have the same real part,
\begin{equation}
    \label{eq:real-part-root-of-unity}
    \Real(\omega^k) = \cos\Bigl( \frac{2\pi k}{N}\Bigr),
\end{equation}
and when adding them, the complex part cancels out: $\omega^k + \omega^{-k}$ lies on the real axis, at the point $2 \Real(\omega^k)$.
This means that
\begin{align}
    \label{eq:sum-symmetric-circulant}
    c_k \omega^k + c_{N-k} \omega^{N-k}
        = 2c_k \Real(\omega^k)
\end{align}
is a real number.
From \eqref{eq:eigen-pair-circulant-matrix} we see that the eigenvalues $\lambda_n$ consist of many such sums: all complex parts cancel out and the eigenvalues are real\footnote{The expression for the eigenvalues is slightly different depending on whether $N$ is odd or even.}

Now we can also choose real eigenvectors: the real part of $\vomega_n$.
After all, if $\vomega_n$ is an eigenvector for the real eigenvalue $\lambda_n$, so are $\vomega_{-n}$ and $\vv_n = \nicefrac{1}{2}( \vomega_n + \vomega_{-n})$.
By the same argument as before, equations \eqref{eq:sum-symmetric-circulant} and \eqref{eq:real-part-root-of-unity} show that this is a real eigenvector:
\begin{align}
    \vv_n = \Bigl(1, \; \cos \theta, \; \dots, \; \cos N\theta\Bigr),
    \quad \theta = \frac{2\pi n}{N}.
\end{align}
This is a discrete cosine function consisting of $N$ points, where higher $n$ implies in higher frequencies.
This is illustrated in figure \ref{fig:eigenvectors-symmetric-circulant}.

\begin{figure}
    \centering
    \includegraphics[width=.5\textwidth]{figs/suppl-fig-cosines.pdf}
    \caption{The eigenvectors of a symmetric, circulant matrix are discrete cosine functions with different periods.}
    \label{fig:eigenvectors-symmetric-circulant}
\end{figure}

\paragraph{Toeplitz is asymptotically circulant}
%-----------------------------------------------

The reason circulant matrices are interesting here, is that
Toeplitz matrices can be shown to be asymptotically equivalent to circulant matrices, and that eigenvalues are preserved.
We refer to \textcite{Gray2006} for a detailed discussion of that result.
What this implies is that the eigenvectors of large Toeplitz matrices are well approximated by those of circulant matrices: sinusoidal functions.
That in turn means that approximately Toeplitz covariance matrices (which are real and symmetric) will have cosine-shaped eigenvectors.

%————————————————————————————————
\section{PCs of random processes}
%————————————————————————————————

We want to end by discussing two examples where Toeplitz covariance structures arise, and we thus would expect cosine eigenvectors, at least asymptotically.

\begin{figure}
    \centering
    \includegraphics[width=.5\textwidth]{figs/suppl-fig-ar1.pdf}
    \caption{The autocovariance matrix for an autoregressive process \textsc{ar}(1) for two values of $\rho$. When $\rho\to 1$ it approximates the discrete cosine transform.}
    \label{fig:ar1}
\end{figure}

\paragraph{Weakly stationary process}
%------------------------------------

Toeplitz matrices arise naturally in the study of weakly stationary processes.
These are random processes where the mean is constant over time, and where the covariance does not change by shifts in time: it only depends on the distance between two time steps.
That is, when $\Cov(x_i, x_j) = K(j-i)$ is some function of $j-i$, and thus results in a Toeplitz covariance matrix.

One example of such a process is a first order autoregressive process \textsc{ar}(1), where
\begin{align}
    x_n = \rho x_{n-1} + r_n,
\end{align}
where $r_n$ is a random step with mean zero and variance $\sigma^2$, and we assume $x_0 = 0$.
It can be shown that this process has mean $E[x_n] = 0$ and variance $\text{Var}[x_t] = \nicefrac{1}{1-\rho^2}$ if $|\rho| < 1$.
In that case, the covariance is
\begin{align}
    \Cov(x_i, x_j) = \frac{\sigma^2}{1-\rho^2} \cdot \rho^{|j-i|}.
\end{align}
This is actually one of the few cases where an analytic expression for the eigenvectors is known, although it is rather complex \parencite{Ray1970,Rao1990}.
Interestingly, one can use this to show that for \textsc{ar}(1) processes, the discrete cosine transform \textsc{dct-ii} becomes equivalent to the `principal component transform' (Karhunen-Loève transform) as $\rho\to 1$ \parencite[section~3.3.2]{Rao1990}.

\paragraph{High-dimensional random walk}
%---------------------------------------

In the limit $\rho \to 1$ one obtains a random walk.
\textcite{Antognini2018} analyse the principal components of high-dimensional random walks.
We briefly summarise their results.
Consider a random walk in $\R^M$ with $N$ steps given by
\begin{equation}
    \vx_n = \vx_{n-1} + \vr_n
    \label{eq:random-walk}
\end{equation}
where $\vr_n$ is a random step drawn from a probability distribution with zero mean and a finite, normalized covariance matrix.
We start from $\vx_0 = \mathbf{0}$ in $\R^M$.

We can express all this as matrix multiplications.
Collect the points $\vx_n$ and steps $\vr_n$ as the rows of the $N \times M$ matrices $\vX$ and $\vR$ respectively.
Let $\vW$ be a $N\times N$ matrix with $1$'s on the diagonal, $-1$'s on the subdiagonal and zeros elsewhere.
This implements the walking mechanism in the sense that $\vW \vX = \vR$, hence
\begin{align}
    \label{eq:random-walk-matrix}
    \vX = \vW^{-1} \vR.
\end{align}
To compute the covariance matrix $\vS$ we need the centered datapoints $\hat\vx_n = \vx_n - \bar \vx_n$.
The centering operation be conveniently expressed as multiplication by the $N \times N$ \emph{centering matrix} $\vect C = \vect I - \frac{1}{M} \vect J$, where  $\mathbf{J}$ is the all-ones matrix.
This gives
\begin{align}
    \label{eq:hat-X}
    \hat \vX = \vC \vX = \vC \vW^{-1} \vR
\end{align}
and allows us to express the covariance matrix as $\vS = \frac{1}{N} \hat \vX^T \hat \vX$.
Instead of finding the eigenvectors of $\hat \vX^T\hat \vX$, we can look for those of $\hat \vX\hat \vX^T$.
After all, if $\vu$ is an eigenvector for $\hat \vX^T \hat \vX$ with nonzero eigenvalue $\lambda$, then $\vect v = \hat\vX \vu$ is the corresponding eigenvector for $\hat \vX \hat\vX^T$.

Putting all this together, \textcite{Antognini2018} look for the eigenvalues and eigenvectors of
\begin{align}
    \label{eq:target}
    \hat \vX \hat \vX^T
        = \vC \vW^{-1} \vR \vR^{T} \vW^{-T} \vC
\end{align}
where we used symmetry of $\vC$.
Note that this matrix contains the covariance between timesteps, rather than dimensions.
They observe that in the limit of infinte dimensionality $M \to \infty$, we have that $\vR\vR^T$ tends to the $N\times N$ identity matrix.
This allows us to simplify \eqref{eq:target} to
\begin{align}
    \label{eq:target-simple}
    \hat \vX \hat \vX^T
        = \vC \vW^{-1} \vW^{-T} \vC.
\end{align}
Since $\vW$ is a so called banded Toeplitz matrix, and $\vC$ is circulant, the whole expression can be shown to be asymptotically equivalent to a circulant matrix, meaning that the eigenvectors are cosines.
This analysis can be related to melodic contours, when we consider a collection of $M$ contours of length $N$ as one high-dimensional walk through $\R^M$.

%—————————————————
\printbibliography
%—————————————————

\end{document}


\documentclass[serif,xcolor=pdftex,dvipsnames,table,hyperref={bookmarks=false,breaklinks}]{beamer}

\input{../config.tex}

\settitlecard{9}{Sparse Matrices and Probability 1}

\begin{document}

\maketitlepage

\section{Sparse Matrices}
\subsection{Foo}

\begin{frame}[t]{Sparsity}
	\begin{itemize}[<+->]
		\item A \textbf{sparse matrix} is an matrix in which most of the entries are zero.
		\item This is in contrast to a \textbf{dense matrix}.
		\item Sparse matrices naturally appear in many applications:
		\begin{itemize}[<+->]
			\item Network adjacency matrices are typically very sparse. For example, you are only Facebook friends with a small percentage of the total Facebook population.
			\item In recommender systems, ratings are often arranged as a sparse matrix.
			\item In NLP, the matrix of word counts in a set of documents is typically sparse.
			\item Multiple parallel event sequences can be arranged as a matrix. If the events are uncommon, then the matrix is sparse.
		\end{itemize}
		\item If we know an matrix is sparse, we can take advantage of this structure to speed up computations on the matrix.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Sparsity}
	\begin{itemize}[<+->]
		\item Consider taking the inner product of two length $n$ vectors, $x$ and $y$.
		\item In general, how many multiplications must we perform?
		\item What if we know that only 10\% of the entries in $x$ are non-zero and we know where the are, how many multiplications do we need to perform?
	\end{itemize}
\end{frame}

\begin{frame}[t]{Sparse Representations}
	There are two main strategies for storing sparse matrices:
	\begin{enumerate}[<+->]
		\item Formats that support \textbf{efficient modifications} include Dictionary of Keys (DOK), List of Lists (LIL), and Coordinate list (COO) formats.
		\item Formats that support \textbf{efficient access and operations} include Compressed Sparse Row (CSR) and Compressed Sparse Column (CSC) formats.
	\end{enumerate}
\end{frame}

\begin{frame}[t]{Dictionary of Keys Format}
	\begin{itemize}[<+->]
		\item The DOK format is perhaps the simplest of the formats for efficient modification.
		\item The DOK format stores the matrix as a dictionary with row/column tuples as keys and one key/value pair per non-zero entry.
		\item Adding or removing an entry can be done in constant time.
		\item What is the complexity of row or column slicing?
	\end{itemize}
\end{frame}

\begin{frame}[t,fragile]{Compressed Sparse Row Format}
	\begin{itemize}[<+->]
		\item The CSR format stores an $m \times n$ matrix as three one dimensional arrays \verb|indices|, \verb|indptr|, and \verb|data|.
		\begin{enumerate}[<+->]
			\item The \verb|data| array stores the non-zero entries of the matrix in a left-to-right top-to-bottom order (row major order).
			\item The \verb|indices| array has the same length as the \verb|data| array and stores the column of each entry.
			\item The \verb|indptr| array is a length $m$ array. \verb|indptr[i]| stores the index in \verb|data| and \verb|indices| of the first non-zero entry in the $i$th row.
		\end{enumerate}
		\item The CSC format is the same as CSR, but \verb|data| is stored in column major order and \verb|indices| stores the row of each entry.
	\end{itemize}

\end{frame}

\begin{frame}[t]{Compressed Sparse Row Format: Example}
	\centering
	\includegraphics[width=\textwidth]{{../Figures/array_slicing/Slide32}.png}
\end{frame}

\begin{frame}[t]{Compressed Sparse Row Format: Example}
	\centering
	\includegraphics[width=\textwidth]{{../Figures/array_slicing/Slide33}.png}
\end{frame}

\begin{frame}[t]{Compressed Sparse Row Format: Example}
	\centering
	\includegraphics[width=\textwidth]{{../Figures/array_slicing/Slide34}.png}
\end{frame}

\begin{frame}[t]{Compressed Sparse Row Format: Example}
	\centering
	\includegraphics[width=\textwidth]{{../Figures/array_slicing/Slide35}.png}
\end{frame}

\begin{frame}[t]{Compressed Sparse Row Format: Example}
	\centering
	\includegraphics[width=\textwidth]{{../Figures/array_slicing/Slide35}.png}
\end{frame}

\begin{frame}[t,fragile]{Scipy Sparse Matrices}
	The module \verb|scipy.sparse| implements each of these sparse formats.

	\begin{lstlisting}
		>>> import scipy.sparse as sps
		>>> import numpy as np

		>>> A = np.eye(5)
		>>> identity = np.eye(5)
		>>> sparse_identity = sps.csr_matrix(identity)

		>>> sparse_identity.indptr
		array([0, 1, 2, 3, 4, 5], dtype=int32)
		>>> sparse_identity.indices
		array([0, 1, 2, 3, 4], dtype=int32)
		>>> sparse_identity.data
		array([ 1.,  1.,  1.,  1.,  1.])
	\end{lstlisting}
\end{frame}

\begin{frame}[t,fragile]{Interactive Demo}
	\begin{itemize}
		\item What linear algebra operations are most sped up by using sparse matrices?
	\end{itemize}
\end{frame}

\section{Probability in Python}
\subsection{Foo}

\begin{frame}[t]{Random Variables}
	% PDF, PMF, CDF
	\begin{itemize}[<+->]
		\item Probability and statistics play a central role in data analysis, modeling, and numerical algorithms.
		\item But first, a quick review.
	\end{itemize}

	\pause
	\begin{block}{Random Variables}
		A random variable, $X$, is a quantity that can take any value from a set of possible values, $\Omega$, according to a set of probabilities.
	\end{block}
\end{frame}

\begin{frame}[t]{Random Variables}
	For example: Imagine we are flipping a coin.
	\pause
	\begin{itemize}[<+->]
		\item What is the random variable?
		\begin{itemize}[<+->]
			\item The random variable is the outcome of the coin flip.
		\end{itemize}
		\item What is the set of possible outcomes?
		\begin{itemize}[<+->]
			\item $\Omega = \{H,T\}$
		\end{itemize}
	\end{itemize}

\end{frame}

\begin{frame}[t]{Discrete Random Variables}
	% PDF, PMF, CDF
	\begin{itemize}[<+->]
		\item A discrete random variable may take its value from a finite or countably infinite set.
		\item Examples include:
		% Support as a question
		\begin{itemize}[<+->]
			\item The outcome of a coin flip can take one of two possible values.
			\item A randomly dealt five card poker hand can take one of $\approx$2.6 possible million values.
			\item The number of people who log in to Netflix between 1pm and 2pm can be any non-negative integer.
			\item The end of season goal differential for a soccer team can be any integer (positive or negative).
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}[t]{Probability Mass Functions}
	% PDF, PMF, CDF
	The probability of each possible outcome is defined by a Probability Mass Function.

	\begin{block}{Probability Mass Function}
		For a discrete random variable $X$ with support $\Omega$, a Probability Mass Function (PMF) $P:\Omega\to [0,1]$ maps possible outcomes to probabilities. A PMF must satisfy two conditions:
		\begin{enumerate}[<+->]
			\item Probability of any single outcome must be between zero and one (i.e. $P(x) \in [0,1]$ for all $x \in \Omega$).
			\item The probabilities of all possible outcomes must sum to one (i.e. $\sum_x P(x) = 1$).
		\end{enumerate}
	\end{block}
\end{frame}

\begin{frame}[t]{Probability Mass Functions: Examples}
	% PDF, PMF, CDF
	Let $X$ represent the outcome of a six sided dice roll.
	\begin{itemize}[<+->]
		\item The set of possible outcomes is $\Omega = \{1,2,3,4,5,6\}$.
		\item Assuming the dice is fair, then the PMF may look like:
	\end{itemize}
	\pause
	% Dice PMF
	\centering
	\includegraphics[height=2in]{../Figures/uniform_multinomial.pdf}
\end{frame}

\begin{frame}[t]{Probability Mass Functions: Examples}
	% PDF, PMF, CDF
	Let $X$ represent the the number of people entering a certain bank between 1pm and 2pm.
	\begin{itemize}[<+->]
		\item The set of possible outcomes is the set of all non-negative integers $\Omega = \mathbb{Z}_{\geq 0}$.
		\item This random variable is a canonical example of a Poisson distributed random variable which has the following PMF:
	\end{itemize}
	\pause
	% Poisson PfMF
	\centering
	\includegraphics[height=1.5in]{../Figures/Poisson_pmf.png}
\end{frame}

\begin{frame}[t]{Parametric Distributions}
	The Poisson distribution is an example of a \textbf{parametric distribution}, that is, it requires a set of parameter values to fully specify the distribution.

	\pause
	\begin{align*}
		f(X=x; \lambda) = \frac{\lambda^x e^{-\lambda}}{x!},\, \lambda > 0
	\end{align*}

	\pause
	\begin{itemize}[<+->]
		\item In this case the, the distribution has a single parameter $\lambda$ that must be a positive real number.
		\item Much of statistics is concerned with inferring these parameters from data.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Continuous Random Variables}
	\begin{itemize}[<+->]
		\item A continuous random variable takes its value from an uncountably infinite set such as the set of real numbers.
		\item Examples include:
		\begin{itemize}[<+->]
			\item The height of a randomly selected person.
			\item Income of a randomly selected household.
			\item The amount of time between hard drive failures in a server.
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}[t]{Probability Density Functions}
	The distribution over possible values of a continuous random variable is given by a \textbf{probability density function}.
	\begin{block}{Probability Density Function}
		For a continuous random variable $X$, the probability density function (PDF) $P(x)$ describes the relative likelihood of a continuous random variable taking a given value. A PDF must satisfy the following two conditions:
		\begin{enumerate}[<+->]
			\item All values must be non-negative. That is, $P(x) \geq 0$ for all $x \in \Omega$.
			\item The area under the PDF must equal one. That is, $\int_x P(x) dx = 1$.
		\end{enumerate}
		\pause
		The probability of $X$ falling between $a$ and $b$ is given by the integral:
		$$P(a < x < b) = \int_x p(x) dx$$
	\end{block}
\end{frame}

\begin{frame}[t]{Probability Density Functions: Examples}
	\begin{itemize}
		\item Given some range $[a,b]$, the \textbf{uniform distribution} places equal likelihood on all values in the range.
	\end{itemize}
	\pause
	\begin{align*}
		p(x;a,b) = \frac{1}{b - a}
	\end{align*}
	% uniform plot
	\centering
	\includegraphics[height=1.5in]{../Figures/Uniform_Distribution_PDF_SVG.png}
\end{frame}

\begin{frame}[t]{Probability Density Functions: Examples}
	\begin{itemize}[<+->]
		\item Perhaps the most common distribution in statistics is the \textbf{Normal} distribution.
		\item The Normal distribution takes two parameters: a mean parameter $\mu$ and a variance parameter $\sigma^2$.
	\end{itemize}
	\pause
	\begin{align*}
		p(x;\mu,\sigma) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)
	\end{align*}
	% Normal plot
	\centering
	\includegraphics[height=1.5in]{../Figures/Normal_Distribution_PDF.png}
\end{frame}

% \begin{frame}[t]{Cumulative Mass Function}
% 	\begin{block}{Cumulative Mass Function}
% 		A Cumulative Mass Function (CMF) is
% 	\end{block}
% \end{frame}

\begin{frame}[t]{Evaluating PMFs, PDFs, and CMFs}
	The most fundamental computation necessary when working with probability distributions is evaluating the distribution at different values. This computation can be difficult or costly for a number of reasons:
	\pause
	\begin{itemize}[<+->]
		\item The PDF, PMF, or CMF involves a difficult to compute special function.
		\item Normalizing the distribution (ensuring the PDF/PMF integrates to 1) requires a difficult to compute sum or integral.
		\item Probabilities near zero or near one can cause numerical errors.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Calculating PDFs: The Gamma Function}
	Evaluating PDFs and PMFs, even common ones, often requires evaluating difficult to compute special functions. A particularly common function for continuous distributions is the Gamma function.

	\pause
	\begin{block}{Gamma Function}
		The gamma function is defined as
		$$\Gamma(t) = \int_0^\infty x^{t-1}e^{-x}dx$$
	\end{block}

	\pause
	The Gamma function and related Incomplete Gamma and Incomplete Beta functions are necessary/useful for working with the following common distributions (among others).
	\begin{itemize}[<+->]
		\item Gamma, $Z$, $t$, $\chi^2$, $F$, binomial, Poisson
	\end{itemize}
\end{frame}

\begin{frame}[t]{Calculating PDFs: The Gamma Function}
	Algorithms for computing the Gamma function have been heavily studied. The most commonly used algorithm is uses the Lanczos approximation.

	\pause
	\begin{align*}
		\Gamma(z+1) &= \sqrt{2\pi}\left(z + g + \frac{1}{2}\right)^{z+1/2}e^{-(z+g+1/2)}A_g(z)\\
		A_g(z) &= c_0 + \frac{c_1}{z+1} + \frac{c_2}{z+2} + \frac{c_3}{z+3} + ... \\
	\end{align*}

	\pause
	Importantly, the user can choose $g$ and pre-calculate the constants $c_i$.
\end{frame}

\begin{frame}[t]{Calculating PMFs: Using Recursion}
	\begin{itemize}[<+->]
		\item Often, when multiple values of a PMF are desired, we can take advantage of recurrence relations to avoid calculating costly special functions.
		\item The binomial distribution is the distribution for the number of successes in a sequence of $n$ yes/no experiments (e.g. coin flips) with success probability $p$. The binomial PMF is,
	\end{itemize}
	\pause
	\begin{align*}
		P(X = x; n, p) &= {{n}\choose{x}}p^x(1-p)^{n-x}
	\end{align*}
\end{frame}

\begin{frame}[t]{Calculating PMFs: Using Recursion}
	Evaluating the binomial PMF involves evaluating factorials (a special case of the Gamma function); however, if multiple values are desired, we can take advantage of the following recurrence relation:

	\begin{align*}
		P(X=x) &= {{n}\choose{x}}p^x(1-p)^{n-x}\\
		&= \frac{n-x}{x+1}\frac{p}{1-p}\left[{{n}\choose{x-1}}p^{x-1}(1-p)^{n-(x-1)}\right]\\
		&= P(X=x-1)\frac{n-x}{x+1}\frac{p}{1-p}
	\end{align*}
\end{frame}

\begin{frame}[t]{Calculating PMFs: Using Recursion}
	\begin{itemize}[<+->]
		\item Does utilizing this recursion improve the complexity or the constant?
		\item The complexity of calculating the first $k$ values of the binomial PMF is $\mathcal{O}(k)$ in both cases; however, we are replacing a special function with arithmetic operations only.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Working in log-space and the Log-Sum-Exp trick}
	\begin{itemize}[<+->]
		\item As we discussed in a previous lecture, exponentiating even moderate values can result in numerical overflow or underflow.
		\item Many distributions require exponentiation of intermediate values.
		\item One common solution is to work in \textbf{log-space}.
	\end{itemize}

\end{frame}

\begin{frame}[t]{Working in log-space and the Log-Sum-Exp trick}
	The multinomial distribution is the standard distribution for finite sets. Consider the following multinomial distribution over $K$ discrete values parameterized by a length $K$ vector of weights $\alpha$.

	\begin{align*}
		P(X=i; \alpha) &= \frac{e^{\alpha_i}}{\sum_i e^{\alpha_i}},\, i = 1,...,K
	\end{align*}

	\pause
	Rather than evaluate this function directly, risking over/underflow, we can evaluate the log PMF,

	$$\log P(X=i;\alpha) = \alpha_i - \log \sum_i e^{\alpha_i}$$

	\pause
	Why does this not completely solve our problem?

\end{frame}

\begin{frame}[t]{Working in log-space and the Log-Sum-Exp trick}
	Calculating the second term $\log \sum_i e^{\alpha_i}$ (also known as the cumulant function) still requires exponentiating $\alpha$. Instead, let $\alpha^* = \max_i \alpha_i$. Then, we can use the following trick,

	\begin{align*}
		\log \sum_i e^{\alpha_i} &= \log \frac{e^{\alpha^*}}{e^{\alpha^*}}\sum_i e^{\alpha_i}\\
		&= \log \sum_i e^{\alpha_i-\alpha^*} + \log e^{\alpha^*}\\
		&= \log \sum_i e^{\alpha_i-\alpha^*} + \alpha^*\\
	\end{align*}
\end{frame}

\begin{frame}[t,fragile]{Working in log-space and the Log-Sum-Exp trick}
	\begin{itemize}[<+->]
		\item Now we are guaranteed that the largest term in the sum equals one.
		\item There can be no overflow and, while individual terms in the sum may underflow, this results only in round-off error in the final sum.
		\item Log-sum-exp is implemented in \verb|scipy.misc.logsumexp|.
	\end{itemize}
\end{frame}

% \begin{frame}[t]{Estimating and Visualizing Distributions}
% 	% PDF, PMF, CDF
% \end{frame}
%
% \begin{frame}[t]{Building Histograms}
% 	% PDF, PMF, CDF
% \end{frame}
%
% \begin{frame}[t]{Seaborn}
% \end{frame}
%
% \begin{frame}[t]{Seaborn}
% 	% Interactive Demo
% \end{frame}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Thesis template by Youssif Al-Nashif
%
%   May 2020
%
%%%%%%%%%%%%%%%%%%%%%%%%

\section{NHTSA Special Crash Investigations}
\hspace*{0.3cm} For the NHTSA Special Crash Investigation dataset, a hierarchical clustering was performed on the resulting graph kernel computed from the skip-gram graphs. Since this dataset was small at $N=48$, it served as a good subject to study the interactions between modifying the skip window width, kernel hyper parameters, and number of clusters. This study on these interactions informs decisions made on which hyper parameters to use in final analysis of the NHTSA data as well as the reddit thread data. \\

For the study on these interactions, hyper-parameters were varied and the value of cluster within sum of squares was tracked. Specifically, the values in Figure 4.1 were tested. In Figure 4.1, we see some promising values occurring at more prominent ``elbow" points in the plots. These points will be further analyzed and compared. The hyper parameters for these points are:\\

 \begin{table}
\centering
\begin{tabular}{c|c|c|c}
Point&Skip-gram k& Graph Kernel sigma&No. of Clusters \\
\hline
A&3&1000&5\\
B&2&1100&3\\
C&3&1100&5\\
D&3&800&6\\
E&3&900&7
\end{tabular}
\caption{Hyper-parameter for Variation Study in Figure 4.1}
\end{table}

\begin{figure}
\includegraphics[width=6in]{Content/Images/hclust_variation.png}
\caption{Hierarchical Clustering Variation (NHTSA) for differing hyper-parameters.}
\end{figure}

These hyper-parameter sets, and their corresponding graph kernel matrix, are used in a hierarchical clustering analysis. The results from the analysis perform well, and display prominent clusters, often of comparable sizes. In Figure 4.2, we see the 5 dendrograms produced from hyper-parameter sets A, B, C, D, and E. Upon some examination, one will notice that documents often appear in the same clusters together, regardless of the hyper-parameter sets. This is most encouraging, as it indicates that these clusters are not a result of user-chosen parameters, but that the documents are indeed similar. \\

\begin{figure}
\includegraphics[width=6in]{Content/Images/5cluster.png}
\caption{Five hyper-parameter sets and their corresponding dendrograms.}
\end{figure}

To compare how often this co-membership in groups was appearing, a matrix of co-membership was created. In Figure 4.3, we see that each document has a set of other documents with which it is always clustered with, regardless of hyper-parameter configurations. These small document sets are the document sets which we can expect to have the most in common with one another.\\

\begin{figure}
\includegraphics[width=6in]{Content/Images/comembers5.png}
\caption{Co-membership of documents across hyper-parameter sets A, B, C, D, and E.}
\end{figure}

We can inspect the results by checking some of the co-members' text. For example, document two (which was an ambulance crash in Angola, Delaware), has only 3 other co-members. Document two's co-members are: document 11, document 30, and document 38. Document 11 was an ambulance crash in Sheridan, Indiana. Document 30 was an ambulance crash in Pasadena, California. Document 38 was an ambulance crash in Ocilla, Georgia.\\

Now, we can examine some information about these four incidents that may explain their clustering. For example, all of these incidents had fatalities, three of them had roll over events, and these four were all front end collisions to the ambulance where they struck another vehicle, or object, head on. While this is just one example of the clustering methods identifying similarities in the text, we can examine similarities across the dataset through use of term-frequency/inverse document frequency.\\

\begin{equation}
TF-IDF = \frac{\frac{\text{Term Frequency in Document}}{\text{Total Words in Document}}}{\log_2(\frac{\text{Total Documents in Corpus}}{Documents with Term})}
\end{equation}

\newpage

\begin{figure}
\includegraphics[width=6in]{Content/Images/nhtsa_tf_idf.png}
\caption{Term-Frequency/Inverse Document-Frequency for NHTSA reports, using hyper parameter set A.}
\end{figure}

In Figure 4.4, we see that words that appear frequently, and are unique to that cluster appear to be vehicle manufacturers, medical terms, and words that describe a crash situation (e.g. guardrail, tree, interstate). We can use these to get an idea of the crash situation. Alternatively, we can also examine portions of the skip-gram graph produced, which was used for the graph kernel calculation. \\

We can put together some of the ideas and topics discussed in the cluster. For example, in Figure 4.5 we see that ``Front Left Passenger", ``Rear Facing Seat", and ``Patient Compartment" are all skip-grams which appeared frequently in this cluster.\\

\begin{figure}
\includegraphics[width=6in]{Content/Images/graph_k5_2.png}
\caption{Skip-grams which occurred more than 50 times in hierarchical clustering with hyper-parameter set A, in cluster 3.}
\end{figure}

\section{reddit Threads}

\hspace*{0.3cm} For the reddit threads dataset, analysis was focused on the ability of the clusterings to organize threads based on their query words. The words used to query the \texttt{r/MentalHealth} subreddit (\texttt{https://www.reddit.com/r/mentalhealth}) all returned threads which were then clustered into new groups. Some of these clusters expressed strong preference for posts that corresponded with specific key words. \\

This data was collected with the \texttt{\{redditExtractoR\}} package \cite{rivera2015package}, and then we augmented and cleaned with \texttt{\{tidytext\}} \cite{silge2016tidytext} and other \texttt{\{tidyverse\}} tools \cite{wickham2019welcome}. These tools made it easy to extract the data from reddit, tidy the data, and perform text processing tasks such as tokenization and parsing for punctuation or numbers. Once the data was in a clean format, it followed the script map to get to a format where it could be analyzed in the same fashion as the NHTSA data was. Since this data was a large set, it utilized the scripts set up with \texttt{\{furrr\}} to speed up computation to under 20 minutes \cite{bengtsson2020unifying}.\\

To assess how many clusters to use, an ``elbow plot" was formed similar to those in Figure 4.1, see Figure 4.6. For the reddit threads, the hyper parameters from the NHTSA analysis were used, and the consistently lowest metric parameter set was chosen; skip-grams of $k=3$ and kernel parameter $\sigma = 1200$ were used in the reddit clustering analysis. In Figure 4.6, we see that 4 clusters is the optimal value, where additional clusters provides diminishing results. \\

\newpage

\begin{figure}
\includegraphics[width=6in]{Content/Images/reddit_wss.png}
\caption{Within sum of squares as a function of number of clusters}
\end{figure}

After computing the kernel and using these parameters, four clusters are produced. Examining the dendrogram, in Figure 4.7, we see that we have 2 very distinct groups, which then break into two smaller groups. These groups are very clear and provide confidence in the clustering results. Along side the dendrogram, there is a heat map of what proportion of documents (or threads) in the cluster came from a query word. In Figure 4.7, we see that clusters 3 and 4 collected posts from the ``angry" query word. Additionally cluster 1 displays a strong amount of its posts coming from the ``sad" query word. \\

\begin{figure}
\includegraphics[width=6in]{Content/Images/k4_reddit_clusters.png}
\caption{Left - Proportion of cluster's threads coming from each query word. Right - Dendrogram for reddit thread analysis, with 4 clusters.}
\end{figure}

The prominence of some of these query words in the clusters indicates that the tone or language used by the thread authors was picked out by the clustering methods. This is of particular interest, as the goal of these methods was to perform unsupervised text clustering which utilized more of the rich intricacies of language.\\

\begin{figure}
\includegraphics[width=6in]{Content/Images/reddit_network.png}
\caption{10 Reddit posts and their skip-grams which occurred 8 or more times.}
\end{figure}

\section{Comparative Analysis}

\hspace*{0.5cm} Looking at the results from both hierarchical clusterings on the graph kernels it is apparent that these methods work much better on larger documents. The performance of the graph kernels being applied to the skip-gram graph and then clustering was much better in the NHTSA dataset than the reddit threads dataset. By measuring within sum of squares, the NHTSA data set performed much better, as its WSS to number of observations ratio was 2.08 at its lowest, whereas the reddit dataset had a WSS to number of observation ratio of 9.\\

As for computational time, the NHTSA dataset took less than 2 minutes to compute, whereas the reddit dataset took just under 20 minutes to compute with the parallel processing. However, the reddit dataset displayed more prominent clusters, which was also indicative of success.\\

Comparing the elbow plots for both the NHTSA and the reddit threads datasets, it is clear that the optimal number of clusters for these methods will be dependent upon the dataset being analyzed. The results from the NHTSA study that indicated that skip-grams with $k=3$ did seem to apply to the reddit dataset as well. These hyper parameters will need tuning for any new application.\\

Overall, the largest difference between the two analyses was that although the reddit threads displayed strong, prominent clusters, the NHTSA dataset was being computed in reasonable amount of time. Since the application of these methods are scaling better for increasing document size instead of increasing document lists, there is a case to be made that aggregating reddit threads into larger documents may be more effective, but to keep the observational unit tied to a reddit thread, this was not completed.\\

These methods could be applied in any text mining application. Since the methods were able to be parallelized during their most computationally expensive portions of code, the methods could scale with sufficient hardware. In a high performance computing environment, these methods could either be applied in batch/scheduled computations or in a stream/real time application (with some modification). The readiness for HPC environments is important as it allows the code to scale and be used in much larger analytic applications.

\section{Contributions to the Field}

\hspace*{0.5cm} Through this work, several new contributions have been made to the field. First, a specific study on the use of graph kernels as preprocessing for hierarchical clustering; the combination of these ideas has not been well documented within the literature reviewed here. Second, the analysis of such differing datasets will bring new perspective to the topic as it relates to the performance of such methods on different types of text data. Thirdly, the workflow developed here was made open for others to use, edit, and analyze for future applications. All of the scripts, notebooks, supplementary materials, and data being made open encourages further development by the community. Lastly, the results of the analysis will go on to inform their respective subfields about the exciting applications of these methods. In the case of NHTSA, edge cases that can be summarized through these methods can lead to more representative simulations being made that can lead to better trained autonomous vehicles. For the reddit mental health data, the work completed here can be used for semi-supervised applications to classify discussion in these type of subreddits. The classification can inform mental health practitioners about the state of mental health within such communities at scale. They can then use those results to drive mental health awareness campaigns, better target what the community is feeling about treatment, or perhaps identify new language or a new subgroup of people dealing with a specific mental health issue. These contributions will support furthering research in this field, as well as the fields from which the data is of interest to.


\input{permve-ntnu-latex-assignment.tex}

\usepackage{float}
\usepackage{tabularx}

\title{
\normalfont \normalsize
\textsc{Norwegian University of Science and Technology\\IT3105 -- Artificial Intelligence Programming}
\horrule{0.5pt} \\[0.4cm]
\huge Module 6:\\ Deep Learning for Game Playing\\
\horrule{2pt} \\[0.5cm]
}

\author{Per Magnus Veierland\\permve@stud.ntnu.no}

\date{\normalsize\today}

\newacro{ANN}{Artificial Neural Network}
\newacro{SGD}{Stochastic Gradient Descent}

\begin{document}

\fancyfoot[C]{}
\maketitle

\newpage
\fancyfoot[C]{\thepage~of~\pageref{LastPage}} % Page numbering for right footer
\setcounter{page}{1}

\section*{Knowledge Representation}

The goal of the module is to find a way to train an \ac{ANN} with supervised learning such that it can beat a random player at the game \textsc{2048}. The assignment states the difficulty of this task clearly, and heavily suggests preprocessing the board state to expose features in a form which is easier to learn by the network.

Based on this, two preprocessing schemes has been developed to transform a board state into features which are used as inputs to the \ac{ANN}. To construct the training data for the two schemes, an evaluation function is needed to decide which move to choose for each network input when generating training data.

During prototyping, a small reference algorithm was written in Python to find a set of working features upon which to base a heuristic for satisfactory play. An important factor when doing supervised learning is that the answer in training examples must correspond well to the inputs presented in the training example. It was found when building the reference implementation that greedily selecting the move which maximizes the number of open cells will result in a mean score of $\approx 230$, which significantly beats the mean random play score of $\approx 110$ (see Table~\ref{table:results}). This same heuristic is used to produce the correct move for both approaches.

The first knowledge engineering approach is the most basic of the two and involves feeding the number of possible merges for each row and each column in the current state as inputs to the network; resulting in 8 network inputs. Intuitively this should work well, as the information of how many merges are possible in each direction should be sufficient to correctly chose the optimal move according to the heuristic of maximizing the number of free cells.

The second knowledge engineering approach is more complex. An obvious solution would be to feed the board state directly into the network. However, this representation is complex and involves a range of different values for each cell. In an effort to simplify the board input, while allowing the network to learn when cells can be merged, a reduction is performed on the input values. For a given board state, the number of distinct non-zero values in the state is first counted. If a row in the board state has the values \texttt{[0 2 4 2]}, then the row has two distinct non-zero values. After counting the number of non-zero values, each non-zero value on the board is reassigned to be its equal to its index in the sorted list of unique non-zero values. This reduction is meant to make the network blind to the magnitude of values in a board state and only treat values according to mergeability. The number of network inputs for the second approach is 16, corresponding to the reduced board cell values.

An important detail when producing training examples was to leave out examples where the heuristic was not able to distinguish one move as better than any other move. Unless the heuristic clearly knows that one move is the right move for the given state, it is assumed to not be beneficial to insist that the training examples follows the same ``random'' move that the example generator ends up making.

\begin{table}
\centering
{\small
\begin{tabular}{ccccccc}
\toprule
Player    & Dimensions                          & Hidden $f$    & Output $f$       & $\varepsilon$ [\%] & Mean score & $\sigma~\text{score}$ \\
\midrule
Random    & N/A                                 & N/A           & N/A              & N/A                & 107.26     &  54.4479              \\
Reference & N/A                                 & N/A           & N/A              & N/A                & 231.87     & 125.5831              \\
Network A & $8 \times 4$                        & \textsc{ReLu} & \textsc{Softmax} & 0.0000             & 250.56     & 129.0481              \\
Network B & $16 \times 512 \times 512 \times 4$ & \textsc{ReLu} & \textsc{Softmax} & 0.2563             & 221.31     & 117.8580              \\
\bottomrule
\end{tabular}
}
\caption{Statistics comparing the two chosen networks and the reference implementation. All networks use cross-entropy cost functions and are trained with learning~rate~0.08. Network~A is trained with minibatch size 40 and network~B with minibatch size 50. All scores are based on highest tile present at end of game; averaged over 1000 games. $\varepsilon~=~\text{Training set error}$. $\sigma~=~\text{standard deviation}$.}
\label{table:results}
\end{table}

\section*{Network Design}

The network inputs chosen for both representations has the same range in each representation. By using the \textit{rectifier} activation function, scaling the inputs is not required as the activation function cannot be saturated, and because all values will be close to zero already. The \textit{softmax} function is used for the output nodes to rank the most probable move.

The first network using the basic knowledge representation scheme does not require any hidden layer. It is able to train to 0\% error within the first couple of epochs. It is however necessary to provide a large enough number of training examples. With training examples based on 100 games played a training error of 0\% was not achieved. However, increasing the number of training examples to 1000 games was sufficient to complete optimal training. It is clear that the first knowledge representation scheme requires little ``intelligence'' from the network. This is intuitive, as it should be straightforward to select an optimal move to maximize the number of free cells based on the number of possible merges for each row and column.

The second network requires a much larger topology. Achieving good training errors required large hidden layers, and the final network uses two hidden layers of 512 nodes each. It is possible to train a network to achieve a training error of 0 since the preprocessing used for the first network is a simple algorithm. However this will likely require more training data and possibly even larger topology.

Both networks use \textit{softmax} output nodes and the cross-entropy loss function as these yielded good results for the MNIST dataset in module~5.

Running each trained network for 1000 games and running a \textit{Welch}-test produces a $p$-value of 0.0. It is clear that network~A with the much simpler input and topology beats network~B with the more complex input and topology. It is also clear that the choices and reduction algorithm used to produce feature input to the more complex network produces good results which could be improved further with the same input.

\section*{Play Analysis}

Both network configurations and their associated preprocessing stages are able to approximate the heuristic function well by achieving a low training error. When the training error is 0, bad moves will either occur because the training set is not large enough for the \ac{ANN} to capture the information necessary to model the heuristic function -- or because the heuristic function simply made an evaluation with poor results.

During observed play bad moves were only seen which were caused by the simplistic heuristic function chosen.

\begin{figure}[!h]
\centering
\begin{tabularx}{\textwidth}{cXc}
\includegraphics[scale=0.35]{bad_1} & ~ & \includegraphics[scale=0.35]{bad_2} \\
\end{tabularx}
\caption{\textit{Example of poor gameplay:} Instead of moving up to gather the 4-tile, the 8-tile and the 16-tile to the right -- while keeping the 2-tiles to the upper left gathered -- the \ac{ANN} instead choses to greedily merge two 2-tiles while risking worsening the board state by permitting tiles to spawn at the top of the board.}
\label{fig:N1}
\end{figure}
\begin{figure}[!h]
\centering
\begin{tabularx}{\textwidth}{cXc}
\includegraphics[scale=0.35]{good_1} & ~ & \includegraphics[scale=0.35]{good_2} \\
\end{tabularx}
\caption{\textit{Example of good gameplay:} In this scenario the board state offers the possibility to merge four tile pairs. The greedy heuristic which the \ac{ANN} models recognizes this possibility and performs four simultaneous merge while at the same time lining up two new merges.}
\label{fig:N1}
\end{figure}

\end{document}