\documentclass{article}
\usepackage[utf8]{inputenc}

\title{homework 4, task 1}
\author{Alexander Smirnov}
\date{March 2019}

\usepackage{natbib}
\usepackage{graphicx}

\begin{document}

\maketitle

\section{What to do}
Make $\beta$-reduction of $\lambda$-term:

$$((\lambda a.(\lambda b.b\:b)\:(\lambda b.b\:b))\:b)\:((\lambda c.(c\:b)) (\lambda a.a))$$

\section{Solution}

$$\textbf{(}\mathbf{\textbf{(} \lambda a.\textbf{(}\lambda b.b\:b\textbf{)}\:\textbf{(}\lambda b.b\:b\textbf{)}\textbf{)}\:b}\textbf{)}\:((\lambda c.(c\:b)) (\lambda a.a))$$

$$((\lambda b.b\:b)(\lambda b.b\:b))\mathbf{\:\textbf{(}\textbf{(}\lambda c.\textbf{(}c\:b\textbf{)}\textbf{)} \textbf{(}\lambda a.a\textbf{)}\textbf{)}}$$

$$((\lambda b.b\:b)(\lambda b.b\:b))\mathbf{\textbf{(}\textbf{(}\lambda a.a\textbf{)}b\textbf{)}}$$

$$((\lambda b.b\:b)(\lambda b.b\:b))b$$

\end{document}


\documentclass[manuscript,screen,review]{acmart}
%\AtBeginDocument{%
%	\providecommand\BibTeX{{%
%			\normalfont B\kern-0.5em{\scshape i\kern-0.25em b}\kern-0.8em\TeX}}}

\setcopyright{acmcopyright}
\copyrightyear{2021}
\acmYear{2021}
\acmDOI{10.1145/1122445.1122456}

\usepackage{amsthm, amsmath, amssymb}
\usepackage[linesnumbered, ruled, vlined]{algorithm2e}
%\usepackage{csvsimple}
%\usepackage{biblatex}
\usepackage{pgffor}
%\usepackage{graphicx}

\title{\bfseries A Simple Yet Effective and Efficient Collaborative Filtering Based Recommendation System}
\author{Masum Billal}
\authornote{This research is done independently by the author, no grant or fund was accepted by the author for this work.}
\email{billalmasum93@gmail.com}
\orcid{0000-0002-2321-5456}
%\addbibresource{ref.bib}
\renewcommand{\shortauthors}{Billal, Masum}
\citestyle{acmauthoryear}
\begin{document}
	\begin{abstract}
		In order to improve both prediction accuracy and the time required to recommend items to a user based on collaborative filtering, a simple algorithm is proposed. The algorithm is different than traditional algorithms only in some simple steps.
	\end{abstract}
	\keywords{Recommender system, collaborative filtering, similarity measure}
	\maketitle
	\section{Introduction}
	A common problem with typical filtering based recommendation systems they usually require a lot of users similar to a user for prediction (such users are called \textbf{neighbors}). This heavily affects performance since the complexity gets incredibly high as well. \textcite{herlocker_konstan_borchers_riedl_1999} uses a memory based collaborative filtering technique that uses similarity between users to predict recommendations using KNN algorithm. This algorithm suffers from some problems such as when too few items are common between two users, predicting accurately becomes harder. \textcite{bell_koren_volinsky_2007} uses a model based learning which is inherently very expensive. \textcite{shen_wei_yang_2013} introduces a way to improve calculating similarity more accurately but they do not offer all that much improvement for the cost of learning similarity, so in this paper such improvement has been ignored and more traditional approaches have been used. \textcite{jamali_ester_2009}, \textcite{pavlov_pennock_2002} uses hybrid collaborative and content filtering or memory and model based algorithm for recommendation. While these works have their merits, most of them are complicated and offer little value to be used in practice. In this paper, we have showed that it may be possible to gain significantly better results by simply modifying traditional approaches a little bit using some principles which are mathematically sound. We will show that using our proposed modifications, we get a flat line in accuracy metric graphs much earlier.
	\section{Algorithm}
	Let us first describe the assumption problem that we want to solve. Let $\mathcal{M}$ be a set of movies and $\mathcal{U}$ be a set of users. We are given a set of triplets $(u,m,r)\in\mathcal{S}$ such that $u\in\mathcal{U},m\in\mathcal{U}$ and $1\leq r\leq 5$ which denotes the rating $r$ of movie $m$ given by user $u$. We are also given a set of pairs $(u,m)\in\mathcal{T}$ such that $u\in\mathcal{U},m\in\mathcal{M}$ and we want to predict the rating $r$ given by user $m$ to the movie $m$. Let $r_{um}$ be the rating of movie $m$ given by user $u$ and $C_{uv}$ be the set of items that both $u$ and $v$ rated. We use the \textit{Pearson Correlation Coefficient} (see \textcite{freedman2007statistics}) as the measure of correlation between user $u$ and $v$. However, we will use a normalized version of it for practical consideration as follows:
	\begin{align*}
		S_{uv}
		& = \dfrac{\sum_{m\in C_{uv}}(r_{um}-\bar{r_{u}})(r_{vm}-\bar{r_{v}})}{\sqrt{\sum_{m\in C_{uv}}(r_{um}-\bar{r_{u}})^{2}}\sqrt{\sum_{m\in C_{uv}}(r_{vm}-\bar{r_{v}})^{2}}}\\
		S_{uv}
		& \leftarrow \dfrac{S_{uv}+1}{2}
	\end{align*}
	This normalization is done using the fact that Pearson correlation coefficient $p_{uv}$ is always in the range $[-1,1]$. We call two users $u$ and $v$ similar if $S_{uv}\geq s$ for some positive real number $s$ such that $0\leq s\leq 1$. Typically, we want $s$ in the range $[.5, 1]$. For this paper, we will consider $s\in\{.7,.8,.9\}$. For a movie $m$, let $\mathcal{U}_{m}$ be the set of users who rated $m$ and $M_{u}$ be the set of movies rated by user $u$. For a set or tuple $A$, let $\bar{A}$ denote the average of the numbers in $A$. For a tuple of weights $\mathbf{w}=(w_{1},\ldots,w_{n})$ such that $0\leq w_{i}\leq 1$ and $\sum_{i=1}^{n}w_{i}=1$ and a tuple of positive real numbers $\mathbf{a}=(a_{1},\ldots,a_{n})$, the \textit{weighted harmonic mean} of $\mathbf{a}$ is defined as
	\begin{align*}
		\mathfrak{H}(\mathbf{a},\mathbf{w})
		& = \dfrac{\sum_{i=1}^{n}w_{i}}{\sum_{i=1}^{n}\dfrac{w_{i}}{a_{i}}}
	\end{align*}
	Usually, \textit{weighted arithmetic mean} is used to predict the ratings in a recommendation system. But in this paper, we have investigated the results using harmonic mean. Next, we describe the rating prediction algorithm for a pair $(u,m)$.
	\begin{algorithm}[H]
		\SetAlgoLined
		\KwIn{Test data in the format $(u,m)$, Threshold $t$, similarity $s$, $T$ to take first $T$ neighbors for a user $u$}
		\KwOut{A single integer in the range $[1,5]$ denoting the predicted rating}
		\KwData{Train data in the format $(u,m,r)$}
		$W\leftarrow []$\\
		$X\leftarrow []$\\
		$tot\leftarrow0$\\
		$R\leftarrow U_{m}$\\
		res = 0\\
		\For{$v\in R$}{
			\If{$|C_{uv}|<t$}{continue}
			\If{$S_{uv}<s$}{continue}
			\If{$S_{uv}\geq s$}{
				$W\leftarrow [W,s]$\\
				$X\leftarrow [X,r_{vm}]$\\
				$tot\leftarrow tot+1$\\
				\If{$tot>T$}{break}
			}
		}
		\If{$tot>0$}{
			res = $\mathfrak{H}(X, W)$
		}
		\Else{
			\If{$|M_{u}|>0$}{
				res = $\bar{M_{u}}$
			}
			\Else{
				res = $\bar{R_{m}}$
			}
		}
		res = res+$.5$\\
		res = floor(res)\\
		return res
		\caption{Algorithm to predict rating}
	\end{algorithm}
	\subsection{Algorithm Principles}
	Our algorithm is based on the following principles.
	\begin{enumerate}
		\item[\bfseries First principle] \textit{Two users $u$ and $v$ are more relevant for each other if it is ensured that $|C_{uv}|\geq t$ for some large enough positive integer $t$}. It is obvious how this principle helps us establish better correlation between users since two users $u$ and $v$ can have very high correlation with very low number of common items between them.
		\item[\bfseries Second principle] \textit{If first principle is established, then it is possible to get an accurate estimation of predicted rating with a lower number of neighbors instead of using a very large number of neighbors}. This helps us predict a rating a lot more efficiently with better accuracy. It is also possible to get a mathematical sense of why this works in practice. Let $m$ and $n$ be positive integers such that $m>n$. Let $u$ be a user and $\mathcal{N}_{m}=\{v:|C_{uv}|\geq t\}$ and $\mathcal{N}_{n}=\{u:|C_{uv}|\geq t\}$ be two sets of neighbors of a user $u$ such that $|\mathcal{C}_{m}|=m$ and $|\mathcal{N}_{n}|=n$. Using the condition $|C_{uv}|\geq t$, it can be assumed that if $s<t$ for a positive integer $s$, then
		\begin{align}
			P(\sigma^{2}(A)
			& \leq \sigma^{2}(B))\label{eqn:secprin}
		\end{align}
		should be very high where $A=\{r_{vm}:|C_{uv}|\geq t\}$, $B=\{r_{vm}:|C_{uv}|\geq s\}$ and $P(x)$ denotes the probability of the random variable $x$. Since higher value of the threshold $t$ ensures better similarity between two users, we can say in a non-rigorous way that the \textit{Pigeonhole principle} ensures \eqref{eqn:secprin} is high enough in practice more often than not.
		\item[\bfseries Third principle] \textit{If two sets of ratings $A$ and $B$ have similar variance and consist ratings given by similar users of $u$ only, then $P(|\bar{A}-\bar{B}|<\epsilon)$ is very high for some positive real number $\epsilon$ which is considerably smaller than $1$}. We can show that a special case holds very often in practice. Since $A$ and $B$ has ratings from similar users only, assume that both $A$ and $B$ consist of $4$ and $5$ only (our data set rating range is $[1,5]$). If $|A|=m$ and $|B|=n$ and $a$ is the number of $4$ in $A$ whereas $b$ is the number of $4$ in $B$, then
		\begin{align*}
			\bar{A}
			& = \dfrac{4a+5(m-a)}{m}\\
			& = \dfrac{5m-a}{m}\\
			& = 5-\dfrac{a}{m}\\
			\bar{B}
			& = \dfrac{4b+5(n-b)}{n}\\
			& = \dfrac{5n-b}{n}\\
			& = 5-\dfrac{b}{n}
		\end{align*}
		As we can see, it does not matter if $m\gg n$ or $n\gg m$ since the difference $|\bar{A}-\bar{B}|$ or the ratio $\frac{\bar{A}}{\bar{B}}$ only depends on the ratio $\dfrac{a}{m}$ and $\dfrac{b}{n}$. So as long as these ratios are similar, $\bar{A}$ and $\bar{B}$ are similar as well.
	\end{enumerate}\clearpage
	\section{Results}
	We show the results of our algorithm on the Movielens 1 million data set and backup our claims with empirical evidence below. The quality of performance is measured using the $F1$ score. The $F1$ score is the harmonic mean of precision and recall.
	\begin{align*}
		P
		& = \dfrac{TP}{TP+FP}\\
		R
		& = \dfrac{TP}{TP+FN}\\
		F1
		& = \dfrac{2PR}{R+P}
	\end{align*}
	We have chosen $F1$ as an accuracy metric for couple of reasons.
	\begin{enumerate}
		\item It is well known that the harmonic mean of $n$ positive real numbers $a_{1},\ldots,a_{n}$ is less than $\min\{a_{1},\ldots,a_{n}\}$ for $n>1$. So an $F1$ value of $x$ indicates that the model has at least $x$ precision and $x$ recall.
		\item $F1$ score can penalize bad precision or bad recall scenarios properly where arithmetic or geometric means may fail. For example, if we consider a recommendation where we do not predict anything, technically, we do not have any errors. So the precision would be $100$ percent whereas recall would be $0$ percent. An arithmetic average would give us an accuracy of $50$ percent which is clearly wrong. $F1$ score penalizes all such scenarios accordingly. For this particular example, we would still have an $F1$ score of $0$ percent.
	\end{enumerate}
	For this paper, we call a test data point \textit{true positive} if a certain movie $m$ is to be recommended to user $u$ based on the rating. If the rating is over $k$ for some fixed $k$, then the movie is to be recommended, otherwise it is to be discarded. We have showed results for both $k\geq 3$ and $k\geq 4$ in this paper.

	We have also used \textit{mean absolute percentage error} as a metric in place of the more traditional \textit{mean absolute error}. Mean absolute error is calculated using the formula:
	\begin{align*}
		MAE
		& = \dfrac{1}{n}\sum_{i=1}^{n}|y_{i}-y_{i}'|
	\end{align*}
	whereas mean absolute percentage error is calculated using the formula
	\begin{align*}
		MAPE
		& = \dfrac{1}{n}\sum_{i=1}^{n}\dfrac{|y_{i}-y_{i}'}{y_{i}}
	\end{align*}
	where $y_{i}$ is the actual value for the data point at $i$ and $y_{i}'$ is the predicted value of this data point. It is easy to see that mean absolute error puts equal weights on all ratings whereas mean absolute percentage errors mitigates that problem to some extent. So we felt it important to also show the results in terms of mean absolute percentage errors.
	\foreach \x in {10,20,30,50,100}
	{
		\foreach\y in {7,8,9}{
			\begin{center}
				\begin{figure}
					\includegraphics[scale=.4]{\x\y.jpg}
				\end{figure}
				\label{fig:\x\y}
			\end{center}
		}\clearpage
	}
	\section{Conclusion}
	As we can see in the result section, all the graphs tend to give us a flat line very early on. usually, we do not get much improvement in accuracy after $30$ neighbors and we do not have to go beyond $.7$ similarity threshold for considering neighbors.
	%\bibliographystyle{ACM-Reference-Format}
	\bibliographystyle{ACM-Reference-Format}
	\bibliography{bibfile}

\end{document}


\chapter{Mathematics}
In this chapter we will see some examples of mathematics.

\lipsum[1]

\section{Very important formulas}
\lipsum[2]

\begin{equation}\label{eqn:rate_eqns}
\frac{\textrm{d}}{\textrm{d}t}\left[
\begin{array}{l}
P_{\textit{0}} \\
P_{\textit{1}} \\
P_{\textit{T}}
\end{array}
\right] =
\left[
\begin{array}{l}
\frac{P_{\textit{1}}}{\tau_{\textit{10}}} + \frac{P_{\textit{T}}}{\tau_{\textit{T}}} - \frac{P_{\textit{0}}}{\tau_{\textit{ex}}} \\
- \frac{P_{\textit{1}}}{\tau_{\textit{10}}} - \frac{P_{\textit{1}}}{\tau_{isc}} + \frac{P_{\textit{0}}}{\tau_{\textit{ex}}} \\
\frac{P_{\textit{1}}}{\tau_{isc}} -  \frac{P_{\textit{T}}}{\tau_{\textit{T}}}
\end{array}
\right]
\end{equation}

\lipsum[3]

\begin{equation}\label{eqn:avgfluorescence}
\bar{I_{f}}(\vec{r})
	= \gamma(\vec{r}) \left(1 - \frac{\tau_{\textit{T}} P_{\textit{T}}^{{eq}}\left(1-\exp \left(-\frac{(T_p - t_p)}{\tau_{\textit{T}}}\right)\right)}{1-\exp\left(-\frac{(T_p - t_p)}{\tau_{\textit{T}}} + k_{\textit{2}} t_p\right)} \times \frac{\left(\exp\left(k_{\textit{2}} t_p\right)-1\right)}{t_p} \right)
\end{equation}

\lipsum[3]


\textit{Deep Learning} is a class of \textit{Machine Learning} (ML) algorithms based on \textit{Neural Networks} (NNs) with multiple layers that progressively extract higher-level features from raw data (e.g., text, images, audio, and video) \cite{goodfellow2016deep}. As ML algorithms, deep neural networks are used to learn different tasks from examples without being explicitly programmed to do so, including \textit{supervised learning}, \textit{unsupervised learning}, and \textit{reinforcement learning} tasks. In a \textit{supervised learning} task, pairs $(X,Y)$ of inputs $X$ and target classes (also called labels) $Y$ are provided by a dataset as training examples. The NN then is \textit{trained} to learn a function that maps the input examples into the target classes. The learned function is typically used to perform predictions (e.g., classification) on examples that the NN has not seen during training. Supervised learning is typically divided into \textit{binary} and \textit{multiclass} problems. In binary problems, a given input $x$ can have one of two possible values $y \in [0, 1]$. In multiclass problems, the label $y \in [0, 1, \cdots, L]$ can take one of $L > 2$ values. Classic examples of binary and multiclass problems are email spam detection (spam or not) and handwritten digits classification \cite{lecun1998gradient}, respectively. In unsupervised learning tasks, only the inputs $X$ are given in the dataset, and the NN is trained to learn internal patterns in the data. These learned patterns can be used for different purposes such as clustering, transfer learning, and generative modeling \cite{bengio2012unsupervised}. In reinforcement learning, the NN is trained to learn an agent that can take optimal actions in an environment according to a \textit{reward function}.

Modern neural AMC systems are typically designed in an unsupervised learning setting, where a NN has to learn relationships between different music structures (e.g., notes, chords, and melodies) represented in symbolic format. Formally, these NNs are \textit{generative models}, i.e. a model that captures a probability distribution $P(X)$ from a given dataset $X$. Inspired by the great results that deep learning has achieved in NLP, generative models for AMC are typically designed as neural \textit{language models} (LM). In NLP, a LM is a conditional probability $L = P(x_t|x_1, x_2, \cdots, x_{t-1})$ of the next token $x_t$ given a prefix with the $t-1$ previous tokens $\{x_1, x_2, \cdots, x_{t-1}\}$ of a sentence. One can train a NN to learn $L$ by processing input sequences of tokens $\{x_1, x_2, \cdots, x_{t-1}\}$ to predict the next token $x_{t+1}$ from the current token $x_t$. A NN trained this way can generate new sentences by sampling tokens from $L$ or searching for sequences over the space defined by $L$.

Considering that music is a sequence of musical tokens (e.g., notes, chords, and sections), one can train a neural LM $L$ to compose music by (a) creating a dataset of symbolic music, (b) designing a NN to learn $L$, and (c) sampling or searching tokens with $L$. The remainder of this chapter discusses different approaches for these three steps.

% Representation and Datasets
\section{Symbolic Music Representation}

Symbolic music representation refers to using high-level symbols such as tokens, events, or matrices as a representation for music modeling. The advantage of symbolic music representation over audio music representation is that the former incorporates higher-level features (e.g., structure, harmony, and rhythm) directly within the representation itself, without the need for further preprocessing. There are many formats to represent symbolic music in computers, but the most common ones are MIDI and piano roll.

\subsection{MIDI}
MIDI is a standard protocol for interoperability between various electronic instruments, devices, and software \cite{briot2017deep}. A MIDI file represents a music piece as a series of messages that specify real-time note performance data and control data. The two most important MIDI messages for music LMs are the following:

\begin{itemize}
  \item NOTE\_ON: this message is sent when a note starts, and it has three parameters:
  \begin{itemize}
      \item \textit{Channel number}: indicates the instrument track with an integer $0 \leq i \leq 15$
      \item \textit{Note number}: indicates the note pitch with an integer $0 \leq p \leq 127$
      \item \textit{Note velocity}: indicates how loud the note is played with an integer
      $0 \leq v \leq 127$
  \end{itemize}

  \item NOTE\_OFF: this message is sent when a note ends, and it has the same three parameters as the
  NOTE\_ON message. In this case, the velocity parameter indicates how fast the note is released.
\end{itemize}

Note events are organized into a stream format called \textit{track chunk}, which specifies the timing information of each note event with a delta time value. A delta time value represents the time of the note event either in relative metrical time (number of ticks from the beginning) or absolute time. In the relative metrical format, a reference called \textit{division} is defined in the file header to set the number of ticks per quarter note. Table \ref{tab:midi} shows an example of a MIDI \textit{track chunk} encoded in a readable format, where the time division has been set to 384 ticks per quarter note.

\begin{table}[h]
    \centering
    % \setlength{\tabcolsep}{4pt}
    \begin{tabular}{llllll}
        \textbf{Delta time} & \textbf{Event Type} & \textbf{Channel} & \textbf{Pitch} & \textbf{Velocity} \\
        96  & NOTE\_ON  & 0 & 60 & 90 \\
        192 & NOTE\_OFF & 0 & 60 & 0  \\
        192 & NOTE\_ON  & 0 & 62 & 90 \\
        288 & NOTE\_OFF & 0 & 62 & 0  \\
        288 & NOTE\_ON  & 0 & 64 & 90 \\
        384 & NOTE\_OFF & 0 & 64 & 0
    \end{tabular}
    \caption{Example of MIDI file encoded in a readable format \cite{briot2017deep}.}
    \label{tab:midi}
\end{table}

% To use MIDI formats with LMs, one has to create a vocabulary of symbols that represent
% note events and their attributes. This vocabulary is then used to encode the stream of events
% in a given set of MIDI files as a sequence of symbols.
% There are many different ways to do that with smaller or bigger vocabularies.
% The pros and cons of some of the common approaches are discussed later in this chapter.

\subsection{Piano Roll}

\textit{Piano roll} is another common format of symbolic music. It is inspired by classic automated pianos that play pieces without a human performer by reading music from a continuous roll of paper with perforations punched into it. Each perforation automatically triggers a note, where the perforation location defines the note pitch, and the perforation length defines the note duration. In a modern piano roll, music is divided into discrete time steps forming a grid where the x axis represents time and the y axis represents pitch. The values $0 \leq v \leq 127$ in the grid represent the velocity of the notes. Figure \ref{fig:piano_roll} shows an example of a modern piano roll.

\begin{figure}[!h]
 \centering
 \includegraphics[width=\columnwidth]{imgs/background/piano_roll.png}
 \caption{Example of music represented in the piano roll format.}
 \label{fig:piano_roll}
\end{figure}

% Using a piano roll representation with LMs also requires creating a
% vocabulary of symbols to represent notes and their attributes
% (duration and velocity). However, the sequence of symbols is defined
% by the time steps in the piano roll, instead of the stream of events
% in the MIDI representation.

The MIDI representation can be mapped to a piano roll by sampling time steps with a given frequency (e.g., every 16th note) from the MIDI events. Because of this property, most datasets of symbolic music organize pieces in a collection of MIDI files. For example, the \textit{MAESTRO} dataset \cite{hawthorne2018enabling} is composed of about 200 hours of virtuosic piano performances of classical music pieces captured from the International Piano-e-Competition \cite{yamahaEPiano} in MIDI format aligned with audio waveforms. The \textit{Lakh} \cite{raffel2016learning} dataset is a collection of 176,581 unique MIDI files from various music genres (mostly pop music) scraped from publicly available sources on the internet, where 45,129 of them have been matched and aligned to entries in the \textit{Million Song Dataset} \cite{bertin2011million}. \textit{Piano midi.de} is a dataset of classical piano pieces from a wide variety of composers recorded in MIDI with a digital piano. JSB Chorales \cite{boulanger2012modeling} contains the entire corpus of 382 four-part harmonized chorales by J. S. Bach.

MIDI and piano roll are the most common formats used to represent symbolic music. However, other formats have also been used in the AMC literature. For example, the ABC notation \cite{walshaw1993abc2mtex} is a text-based music notation system popular for transcribing, publishing, and sharing folk music. MusicXML \cite{good2001musicxml} is a markup language that has been designed to facilitate the sharing, exchange, and storage of scores by musical software systems.

To use any of these symbolic music representations with LMs, one has to define a vocabulary that encodes music data into a sequence of music symbols. %These symbols will be used to create sequences that represent music pieces.
For example, in a MIDI representation, one has to map the note events into tokens and use the delta-time information from the \textit{track chunks} to define the order of the tokens. In a piano roll representation, one has to map the vertical axis (pitch) into tokens and process the piano roll grid either horizontally or vertically to define the order of the tokens. To be processed by NNs, each token in the vocabulary has to be mapped into a vector. Traditionally, these tokens are mapped using \textit{one-hot} encoding, where each token is given an index $i$ and is represented by a vector $v = [v_1, v_2, \cdots, v_n]$, where only $v_i = 1$ and all the other dimensions $v_{j \neq i} = 0$. In the one-hot encoding, $n$ is the number of tokens in the vocabulary.
For example, considering a vocabulary $V = \{a, b, c, d, e\}$, the one-hot encoding of the token $c$ is $c = [0,0,1,0,0]$.

\section{Neural Networks}

% With a dataset of music represented in a symbolic format (MIDI, piano roll, etc.),
% one can train a ML model to learn a LM that can generate music.
% In early examples, LMs were typically implement with Markov chains and
% recently LMs are more commonly modeled with artificial neural networks.
Artificial Neural Networks, or simply Neural Networks (NNs), interconnect a number of simple processing units called \textit{neurons} to learn a function from training examples. These neurons are typically organized into layers. Neurons might be connected to several other neurons in the layer before it, from which it receives data, and several neurons in the layer after it, to which it sends data. NNs can be defined with different \textit{architectures}, i.e. with a different number of layers (\textit{depth}) and different layouts of neuron connections. The first layer of the network is called the \textit{input layer}, and the last one is called the \textit{output layer}. All the intermediate layers are called \textit{hidden layers}. Each neuron in the hidden or output layers takes as input a vector $x$ of incoming connections from the previous layer and assigns a weight vector $w$ to these connections. In its most basic form, the neuron first applies a linear transformation $z = w \dot x + b$ to the inputs $x$, where $b$ is an extra weight called \textit{bias} that is not tied to any neuron of the previous layer. The neuron then uses a nonlinear function called \textit{activation function} $f$ to map the linearly transformed inputs $z$ into an output $y$. Figure \ref{fig:nn} shows a three-layer\footnote{Typically, the input layer is not considered when counting the depth of the NN.} NN called \textit{feedforward} network or \textit{multilayer perceptron} (MLP).

\begin{figure}[!h]
\centering
\includegraphics[width=0.6\columnwidth]{imgs/background/ann.png}
\caption{Diagram of a feedforward neural network \cite{lecun2015deep}. The computations performed by the neurons $j$ (hidden layer L2), $k$ (hidden layer L3), and $l$ (output layer L4) are highlighted beside them \cite{lecun2015deep}. }
\label{fig:nn}
\end{figure}

The number of neurons per layer (i.e., the \textit{layer size}) and the layers' activation functions depend on the task being learned by the NN. In traditional supervised problems, the input layer size is defined by how the input examples are represented and the output layer size by the number of classes in the problem. For example, consider a handwritten digits classification problem in which each handwritten digit is stored in a 28x28 grayscale image. The goal is to classify the images into one of the ten digits (0 to 9). In this example, the input layer size is 784 neurons, one for each pixel in the image. The output layer size is 10 neurons, one for each class. The sizes of the hidden layers are defined arbitrarily and should be controlled to optimize the performance (e.g., classification accuracy) of the network.

In \textit{multiclass} problems, such as the handwritten digit classification, the \textit{softmax} activation function is used in the output layer to create a probability distribution over the classes. Thus, the NN predicts the class with maximum probability. In \textit{binary} problems, the \textit{logistic} activation function is typically used to map the output layer into the probability that the label is one $P(y = 1)$. Thus, the NN predicts $1$ if $P(y = 1) > 0.5$ and $0$ otherwise. The activation functions in the hidden layers are decided arbitrarily, and they also affect the performance of the NNs. Three of the most common activation functions used in the hidden layers are: \textit{logistic}, \textit{tanh}, and \textit{ReLu}. Table \ref{tab:activation} defines each of these functions as well as the softmax function.

\begin{table}[h]
    \centering
    % \setlength{\tabcolsep}{4pt}
    \begin{tabular}{lll}
        \textbf{Name} & \textbf{Function} \\
        Logistic (sigmoid) & $\sigma(x) = \frac{1}{1 + e^{-x}}$ \\
        Hyperbolic tangent (tanh) & $tanh(x) = \frac{e^x - e^{-x}}{e^x + e^-x}$ \\
        Rectified linear unit (ReLu) & $relu(x) = max(0, x)$ \\
        Softmax & $softmax(x) = \frac{e^{x_i}}{\sum_{j=1}^{J} e^{x_j}}$ \\
    \end{tabular}
    \caption{A list of common activation functions used in neural networks.}
    \label{tab:activation}
\end{table}

NNs are typically trained with some variation of the \textit{gradient descent} (GD) algorithm, which optimizes all weights $W$ and $b$ of a network $N$ to minimize a given \textit{loss function} $J(W,b)$. The loss function depends on the task being modeled by $N$. However, in supervised learning, one of the most common losses is the \textit{cross-entropy}, which measures the difference between the training data distribution and the distribution modeled by $N$. Equation \ref{eq:cross_entropy} formally defines the cross-entropy loss for a single example, where $y_c$ is the target label for the class $c$, $\hat{y}_c$ is the output predicted by $N_{W,b}$ for class $c$, and $C$ is the number of classes in the prediction task.
\begin{equation} \label{eq:cross_entropy}
\begin{split}
    J(W,b) = -\sum_{c=1}^{C} y_clog(\hat{y}_c)
\end{split}
\end{equation}

% In its basic formulation, GD uses the average loss of the entire dataset to update the weights $W^i$ and $b$ of the NN $N_{W,b}$.
As shown in Algorithm \ref{alg:grad_desc}, GD works by iteratively taking steps in the opposite direction of the gradient of the loss function $J(W,b)$ with respect to all weights. For a given number of iterations called \textit{epochs}, GD (line \ref{line:grad}) computes the gradient of $J(W,b)$ for the entire training dataset and (line \ref{line:update}) updates all weights $W$ and $b$ in the opposite direction of the gradient. The \textit{learning rate} $\alpha$ is a parameter that controls the size of the training step. Computing the gradient (line \ref{line:grad}) requires calculating the partial derivatives of the loss function with respect to all weights in $N$. This calculation is typically performed by an algorithm called \textit{backpropagation}, which uses the chain rule to compute the gradient one layer at a time, iterating backwards from the output layer to avoid redundant calculations of intermediate terms in the chain rule.

\begin{algorithm}[t]
\caption{Gradient Descent}
\label{alg:grad_desc}
\begin{algorithmic}[1]
\REQUIRE Dataset $(X, Y)$, a loss function $J(W,b)$, a NN $N_{W,b}$ with
parameters $W$ and $b$, the number of epochs $e$ and the learning rate $\alpha$.
\ENSURE Updated parameters $W$ and $b$ that minimize the loss function $J(W,b)$.
\FOR{$i \gets 1$ \TO $e$ }
       \STATE $\partial W \gets \frac{\partial J}{\partial W}$, $\partial b \gets \frac{\partial J}{\partial b}$ \label{line:grad}
       \STATE $W \gets W - \alpha \, \partial W$, $b \gets b - \alpha \, \partial b$ \label{line:update}
\ENDFOR
\end{algorithmic}
\end{algorithm}

Calculating the gradients for the whole dataset to perform just one update can be very slow or intractable for datasets that do not fit in memory. \textit{Stochastic Gradient Descent} (SGD) is a variation of GD that solves this problem by splitting the training data into sets, called \textit{batches}, and performing a training step for each batch. Although SGD supports training with very large datasets, it introduces convergence issues due to the variance in the frequent updates that cause the value of the loss function to fluctuate. \textit{Adaptive Moment Estimation} (Adam) is a recent variation of SGD that mitigates this problem by having a learning rate per weight and separately adapting them during training \cite{adam14}. In practice, most practitioners use the Adam optimizer, given that successful NNs typically require large datasets that do not fit in memory\footnote{The term \textit{big data} is typically used to refer to these very large datasets.}.

\subsection{Recurrent Neural Networks}
\label{sec:rnns}

\textit{Recurrent Neural Networks} (RNNs) are an important architecture for AMC because they were specially designed to model sequential data. RNNs process sequences $\{x_1, x_2, \cdots, x_t\}$ step-by-step by keeping an internal state $h_t$ that is updated every step. Each element $x_i$ is a token (e.g., words in English or pitch classes in western music) traditionally encoded as a one-hot vector. Figure \ref{fig:rnn} shows an abstract diagram of a RNN. On the left-hand side, the RNN is shown with an input layer that passes a token $x_t$ to a hidden layer $A$ that updates $h_t$. A loop in the hidden layer allows information to be passed from one step of the network to the next. The right-hand side shows an unrolled version of the same RNN. The output layer of the network is omitted from the diagram because RNNs can produce one output per time step or one single output at the very last time step. This configuration depends on the learning task. However, the output layer typically maps the hidden state $h_t$ to an output vector $y_t$.

\begin{figure}[!h]
\centering
\vspace{0.5cm}
\includegraphics[width=0.8\columnwidth]{imgs/background/rnn.png}
\caption{Diagram of a RNN \cite{olah2015understanding}.}
\label{fig:rnn}
\end{figure}

Two of the most simple RNNs are the \textit{Elman} network \cite{elman1990finding} and the \textit{Jordan} network \cite{jordan1986}. They are composed of an input layer, a single hidden layer, and an output layer. As shown in Equation \ref{eq:elman}, these RNNs produce an output $y_t$ for each time step $t$. The matrices $W_{xh}$ and $W_{hh}$ represent the weights of the hidden layer, and the matrix $W_{hy}$ represents the weights of the output layer. The functions $f_h$ and $f_y$ are the activation functions of the hidden and output layers, respectively. The only difference between these two networks is that, in the Elman network, the weights $W_{hh}$ are fed from the hidden layer and, in the Jordan network, from the output layer.
\begin{align}
\label{eq:elman}
\begin{split}
    &\textbf{Elman network} \\
    h_t &= f_h(W_{xh}x_t + W_{hh}h_{t-1}) \\
    y_t &= f_y(W_{hy}h_t)
\end{split}
\begin{split}
    &\textbf{Jordan network} \\
    h_t &= f_h(W_{xh}x_t + W_{hh}y_{t-1}) \\
    y_t &= f_y(W_{hy}h_t)
\end{split}
\end{align}

\citet{todd1989connectionist} presented one of the first applications of RNNs for AMC: a \textit{Jordan} network designed to generate melodies. The input of this network is a melody encoded as a sequence of pitch classes (e.g., \textit{CDEGFEDF}), and the output is a single pitch. This network was trained to reconstruct given example melodies. Each melody contributed to $t$ training steps, where $t$ is the size (number of pitches) of the melody. At every training step $t$, the current example melody is given as input, and the network error is calculated by comparing the network output $y_t$ with the respective pitch $x_t$ of the input melody. After training, one can give new melodies as input to the network, which outputs new pitches by interpolating between the melodies seen during training. \citet{duff1989backpropagation} presented another early example of Jordan network for melody generation. However, instead of encoding melodies as a sequence of pitch classes, \citet{duff1989backpropagation} encoded them as a sequence of note intervals\footnote{The distance in pitch between two notes.}.

One of the major problems of RNNs consists of modeling long-term dependencies between symbols in a sequence. Modeling long-term dependency consists of creating a RNN capable of considering previous symbols that are distant from the one that is being predicted. In practice, simple RNNs are unable to connect the information between symbols that are very far from each other \cite{bengio1994}. In music, modeling long-term dependencies is critical to generate long complete pieces with coherent form.

\subsection{Long Short-Term Memory Networks}
\label{sec:lstm}
% LSTM and GRU

Long Short-Term Memory (LSTM) networks \cite{hochreiter1997long} are a special type of RNN explicitly designed to solve the long-term dependency problem. LSTMs also process sequences $x = \{x_1, x_2, \cdots, x_t\}$ step-by-step by keeping an internal state $h_t$ that is updated every step. However, as shown in Figure \ref{fig:lstm}, the hidden layers $A$ have a different structure allowing LSTMs to capture longer dependencies in the input sequences.
A single LSTM module $t$ is composed of an extra state $C_t$ called \textit{cell state}, which is responsible for carrying information through the entire LSTM network.

\begin{figure}[!h]
\centering
% \vspace{0.5cm}
\includegraphics[width=0.9\columnwidth]{imgs/background/lstm.png}
\caption{Diagram of an LSTM \cite{olah2015understanding}.}
\label{fig:lstm}
\end{figure}

The flow of information in the cell state is controlled by three \textit{gates}: an input gate $i_t$, an output gate $o_t$ and a forget gate $f_t$. These gates facilitate the cell to remember or forget information for an arbitrary amount of time. Equation \ref{eq:lstm} formally defines the computation performed in each LSTM module, where $W_f$, $W_i$, $W_c$, and $W_o$ are weight matrices, $[h_{t-1}, x_t]$ is the the hidden state vector $h_{t-1}$ concatenated with the input vector $x_t$, and $\tilde{C}_t$ is the candidate vector to be added to the cell state. Each gate $i_t$, $o_t$, and $f_t$ have the exact same equation, just with different weight matrices ($W_i$, $W_o$ and $W_f$, respectively). The cell state $C_t$ combines the input and forget gates to control the amount of information that will be included from the input versus the amount of information that will be forgotten from the current cell state, respectively. The output gate controls the parts of the cell state that will be included in the final hidden state $h_t$. Modern RNNs (including LSTMs) typically have an extra layer, called \textit{embedding} layer, that is added before any other hidden layer to transform the one-hot input vectors $x_t$ into a dense vector representation called \textit{embeddings}. In NLP, a word \textit{embedding} is a learned representation for text where words that have the same meaning have a similar representation.
% The \textit{embedding} layer learns a dense vector representation of arbitrary dimension from the sparse one-hot representation of the symbols in the vocabulary.
\begin{equation} \label{eq:lstm}
\begin{split}
    f_t &= \sigma(W_f[h_{t-1}, x_t]) \\
    i_t &= \sigma(W_i[h_{t-1}, x_t]) \\
    \tilde{C}_t &= tanh(W_c[h_{t-1},x_t]) \\
    C_t &= f_t * C_{t-1} + i_t * \tilde{C}_t \\
    o_t &= \sigma(W_o[h_{t-1}, x_t]) \\
    h_t &= o_t * tanh(C_t)
\end{split}
\end{equation}

% TODO: Add figure with embedding example
% TODO: bidirectional LSTMs

Modern LSTM-based AMC systems typically train an LSTM as a LM, i.e. to predict the next token $x_{t}$ given prefix tokens $x = \{x_1, x_2, \cdots, x_n\}$. Thus, the input token $x_t$ at each time step $t$ is mapped, with a \textit{softmax} activation function, to a probability distribution $\hat{y}_t$ over the symbols defined in the music vocabulary. The LSTM is then trained with the cross-entropy loss function, which compares the predicted probability distribution $\hat{y}_t$ with the true next token $x_t$. For example, BachBot \cite{liang2017automatic} uses an LSTM to generate polyphonic music in the style of Bach's chorales. BachBot was trained with the JSB Chorales dataset \cite{boulanger2012modeling}, where each chorale was encoded with a sequence of sixteenth-note frames. Each frame consists of four tuples (soprano, alto, tenor, and bass) in the form (\textit{pitch}, \textit{tie}), where \textit{pitch} represents a MIDI pitch number, and \textit{tie} is a boolean value that distinguishes whether a note is tied with another note at the same pitch from the previous frame or is articulated at the current time step. DeepBach \cite{hadjeres2017deepbach} is similar system that use LSTMs to geneate Bach chorales. The main difference between DeepBach and BachBot is that, in DeepBach, LSTMs consider both past and future contexts to predict the next token, while BachBot considers only the past.

% \citet{lyu2015modelling} combined an LSTM with another type of neural network called Restricted Boltzman Machine (RBM) to generate polyphonic music with different musical styles. They experimented with both the Musedata and the JSB Chorales \cite{boulanger2012modeling}. The pieces were encoded as a sequence of pitch numbers temporally aligned on an integer fraction of a quarter note. This encoding system only considered pitch and duration and hence did not considered dynamics information (e.g., note velocity).

\citet{Mao_2018} built upon a biaxial LSTM \cite{johnson2017generating} to create a system called DeepJ, which can compose music conditioned on a specific mixture of composer styles. DeepJ uses a piano roll representation augmented with dynamics information, where the style of the music piece is encoded as a one-hot representation over all artists in the training data. \citet{oore2017learning} proposed another LSTM that can generate music with dynamics. They trained an LSTM on the piano pieces from the International e-Piano Competition \cite{yamahaEPiano} with a new encoding method that extracts tempo and velocity information from MIDI messages.

\subsection{Transformers}

\textit{Transformers} \cite{vaswani2017attention} are modern architectures for sequence modeling based on \textit{attention} mechanisms. In neural NLP models, an attention mechanism is a part of a NN that dynamically highlights relevant tokens of the input sequence \cite{bahdanau2014neural}. Instead of keeping an internal hidden state that is updated at each time step like RNNs, transformers process entire sequences at once, associating an \textit{attention} score to each input token, which determines how much that token contributes to the output. Because transformers process tokens in parallel, they can take advantage of the parallel computing offered by GPUs, and hence transformers can be trained considerably faster than LSTMs \cite{vaswani2017attention}. One drawback of transformers is that they can only process sentences with a fixed size instead of LSTMs that can process sentences of any size.

Transformers were originally designed in the context of \textit{machine translation}, an NLP task that consists of translating a sequence from one language (e.g., English) to another (e.g., French). Machine translation is a \textit{sequence-to-sequence} problem, where a sequence input $x$ has to mapped into an output sequence $y$. NNs designed for machine translation normally have a \textit{encoder-decoder} structure. The first part of the network, called the \textit{encoder}, takes a sequence as input $x$ and outputs a vector representation $e$ (called \textit{encodings}) of the input $x$. The second part, called \textit{decoder}, takes the encodings $e$ as input and outputs a sequence $y$.

As shown in Figure \ref{fig:transformer}, the transformer has an encoder-decoder structure (the encoder is showed on the left side and the decoder on the right side). The transformer takes as input a sentence typically encoded with one-hot vectors and transforms it into two sequences: a sequence of \textit{input embeddings} and a sequence of \textit{positional encodings}. The former is a dense vector representation of words learned from the sparse one-hot input. The latter is a dense vector representation of the words' positions learned from the indices of the words in the sentence. The transformer adds the input embeddings and positional encodings together and passes the result through the encoder.

% TODO: Add figure with input + positional embedding example

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.55\columnwidth]{imgs/background/transformer.png}
 \caption{Diagram of a transformer \cite{vaswani2017attention}.}
 \label{fig:transformer}
\end{figure}

The encoder converts the (input + position) embeddings $b$ into encodings $e$ using a stack of $n$ identical layers called \textit{transformer blocks}. Each transformer block has two layers: a \textit{multi-head attention layer} and a fully connected \textit{feedforward layer}. A residual connection \cite{he2016deep} is applied around each of the two layers, followed by a layer normalization \cite{ba2016layer}. A residual connection is a connection between non-contiguous layers. Layer normalization normalizes the activations of the previous layer, i.e. it applies a transformation that maintains the mean activation within each example close to 0 and the activation standard deviation close to 1.

The key component of the transformer is the \textit{multi-head attention layer}, which computes a score matrix $Z$ from the embeddings $b$ or encodings $e$. The scores in $Z$ represent the relationship between different words in the input sentence. For example, consider the sentence ``The animal didn't cross the street because it was too tired.''. In this sentence, the word ``it'' is related to ``animal'', and so when the transformer is processing the word ``it'', self-attention allows it to associate ``animal'' with ``it''. Figure \ref{fig:attn} shows the encoder self-attention distribution for the word ``it'' in this example.

\begin{table}[!h]
    \centering
    \includegraphics[width=0.6\columnwidth]{imgs/background/attn.png}
    \caption{The encoder self-attention distribution for the word ``it'' in the sentence
    ``The animal didn't cross the street because it was too tired.'' \cite{vaswani2017attention}.}
    \label{fig:attn}
\end{table}

The score matrix $Z$ is computed similarly to a dictionary lookup: it takes a \textit{query} matrix $Q$, a \textit{key} matrix $K$, and a \textit{value} matrix $V$, and outputs a weighted sum of the values that correspond to the keys that are most similar to the query. One of the most common self-attention mechanisms in a transformer is the scaled dot-product attention, which is shown in Equation \ref{eq:attn}. The matrices $Q$, $K$, and $V$ are created by packing the embeddings $b$ (or encodings $e$) of all the words in the input sentence into a matrix $E$, and multiplying it by the weight matrices $W_q$, $W_k$ and $W_v$ that are learned during training. The size $d_k$ of the attention keys is a hyperparameter chosen according to the problem at hand.

\begin{equation}\label{eq:attn}
\begin{split}
    Z = Attention(Q, K, V) &= softmax(\frac{QK^T}{\sqrt{d_k}})V \\
    Q &= E * W_q \\
    K &= E * W_k \\
    V &= E * W_v
\end{split}
\end{equation}

The transformer decoder is similar to the encoder, but it has an extra \textit{masked multi-head attention} layer prepended to the transformer block to perform attention over the target output sentence $y$. This extra layer uses a mask to ensure that the predictions for position $i$ depend only on the known outputs at positions less than $i$. The output of the decoder is computed with a linear layer followed by a softmax activation.

One can use the transformer decoder (without the encoder) to train a LM capable of generating sequences (e.g., text or music) similarly to an LSTM LM (see Section \ref{sec:lstm}). Equation \ref{eq:transformer} formally defines a decoder-based transformer LM, where $x = \{x_1, x_2, \cdots, x_{t-1}\}$ is the input sequence, $n$ is the number of transformer blocks (hidden layers), $W_i$ is input embedding weight matrix, $W_p$ is the positional encoding weight matrix, and $\hat{y}$ is the predicted probability distribution of the next token $x_t$ given the input $\{x_1, x_2, \cdots, x_{t-1}\}$.
\begin{equation}\label{eq:transformer}
\begin{split}
    h_0 &= W_ix + W_p \\
    h_l &= transformer\_block(h_{l-1}) \forall i \in [1,n] \\
    \hat{y} &= softmax(h_nW_e^T)
\end{split}
\end{equation}

Transformers are currently the state-of-the-art of both natural and music language modeling. For example, Radford et al. \cite{Radford2018, radford2019language} proposed a series of models called GPT (General Pre-trained Transformer), GPT-2, and GPT-3 that used the transformer decoder to create a model of natural language. Besides generating long coherent sequences of text, pre-trained GPT models can be fine-tuned to perform specific NLP tasks (e.g., commonsense reasoning, question answering, and summarization) with state-of-the-art performance \cite{radford2019language}. Pre-training consists of training the GPT model as a (unsupervised) LM with a huge general dataset (e.g., Wikipedia). Fine-tuning is performed by stacking extra layers onto the pre-trained model and training these layers with a smaller (supervised) labeled dataset explicitly created for the task.

Music Transformer \cite{huang2018music} is one of the first transformer-based LMs designed for AMC. It uses a new relative attention mechanism that improves memory consumption of the original decoder, allowing it to process longer sequences. Music Transformer achieved state-of-the-art performance on the MAESTRO dataset \cite{hawthorne2018enabling}. \citet{donahue2019lakhnes} showed that a transformer can also compose multi-instrument scores by training it with the NES MDB \cite{donahue2018nesmdb} dataset. \citet{donahue2018nesmdb} used a transfer learning procedure similar to \citet{Radford2018}, where they first pre-trained the transformer with the Lakh dataset (multiple instruments) and then fine-tuned it with the NES-MDB (4 instruments). They manually defined a mapping between the instruments from the two datasets. Pop Music Transformer \cite{huang2020pop} is a transformer model with a specialized music representation to compose pop piano music. It was shown to generate a better rhythmic structure than previous transformer models.

% TODO: Convolutional Neural Networks

\subsection{Variational Autoencoders}

\textit{Variational autoencoders} (VAEs) \cite{kingma2013auto} are another modern architecture that can be used to generate music. VAEs are different from RNNs and Transformers because they were not specifically designed to model sequences. Instead, they are generative models that can potentially learn to represent data in any domain. VAEs have an architecture similar to a traditional \textit{autoencoder}, which is an encoder-decoder NN used to learn efficient encodings of unlabeled data (unsupervised learning). Figure \ref{fig:ae} shows a diagram of an autoencoder.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.7\columnwidth]{imgs/background/autoencoder.png}
 \caption{Diagram of an autoencoder \cite{rocca2019understanding}.}
 \label{fig:ae}
\end{figure}

An autoencoder builds a latent space of a dataset $X$ with an encoder network $e$ by learning to compress each example $x$ into a vector $z$ and then reproducing $x$ from $z$ with a decoder network $d$. A key component of an autoencoder is the bottleneck introduced by making the vector $z$ have fewer dimensions than the input $x$, which forces the model to learn a compression scheme. During training, the autoencoder ideally distills the qualities that are common throughout the dataset. As shown in Figure \ref{fig:ae_sampling}, one can use an autoencoder as a generative model by sampling random vectors from the latent space learned by the encoder and using the trained decoder to build the output from the sampled vector.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.8\columnwidth]{imgs/background/ae_sampling.png}
 \caption{Sampling from the latent space built by an autoencoder \cite{rocca2019understanding}.}
 \label{fig:ae_sampling}
\end{figure}

One limitation of the autoencoder is that it often learns a latent space that is not continuous\footnote{Two close points in the latent space should yield similar outputs when decoded.} nor complete\footnote{Any point sampled from the latent space should yield a ``meaningful'' output when decoded} \cite{roberts2018hierarchical}. This means that if one decodes a random vector sampled from the learned latent space, it might not result in a realistic output. VAEs solve this problem by encoding the dataset $X$ as a probability distribution over the latent space instead of a single vector $z$. Typically, this distribution is assumed to be a multivariate normal distribution $N(\mu_x, \sigma_x)$. Figure \ref{fig:vae} shows a diagram of a VAE. The difference between an autoencoder and a VAE is that the VAE encoder outputs two vectors $\mu_x$ and $\sigma_x$, instead of a single vector $z$. These two vectors represent the mean and standard deviation of a normal distribution $N$, respectively. The decoder $d$ samples a vector $z \sim N(\mu_x, \sigma_x)$ from the distribution $N$ and reconstructs the input $x$ from $z$.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.8\columnwidth]{imgs/background/vae.png}
 \caption{Diagram of a variational autoencoder \cite{rocca2019understanding}.}
 \label{fig:vae}
\end{figure}

MusicVAE \cite{Roberts2017} is one of the first examples of VAE applied to AMC. It splits the input sequence $x$ into $U$ non-overlapping subsequences $y_u$, such that $x = \{y_1, y_2, \cdots, y_U\}$. The encoder processes the segmented input $x$ with a bidirectional LSTM, whose hidden states are used to produce the latent distribution parameters $\mu_x$ and $\sigma_x$. The decoder is a novel hierarchical LSTM that takes the latent vector $z \sim N(\mu_x, \sigma_x)$ as input and first produces $U$ embedding vectors $e = \{e_1, e_2, \cdots, e_U\}$ (one for each subsequence $y_u$) with an LSTM called \textit{conductor}. Each embedding vector is then passed through a  fully connected layer to produce the initial states for a final decoder LSTM. The final LSTM then autoregressively produces a sequence of distributions over output tokens for each subsequence $y_u$ via a softmax activation.

MIDI-VAE \cite{brunner2018midi} uses a VAE to perform style transfer in polyphonic symbolic music. It works by separating a portion $z_s$ of the latent vector $z$ for style classification and another portion $z_t$ to encode music structure (pitch, timber, and velocity). During generation, one can change the style $s_i$ of a given piece $x$ to another style $s_j$ by passing $x$ through the encoder to get $z$ and swapping the values $z_s^i$ and $z_s^j$. The modified latent vector is then passed through the decoder to get $x$ with the target style $s_j$. Style labels can be music genres such as Jazz, Pop, and Classic; or composer names such as Bach or Mozart.
VirtuosoNet \cite{jeong2019virtuosonet} is a VAE designed to generate piano performances with expressive control of tempo, dynamics, and articulations. The encoder is a hierarchical LSTM that encodes music on different levels: note, beat, and measure. The decoder renders musical expressions by first predicting the tempo and dynamics in measure level and then refining the result in note level.

\subsection{Generative Adversarial Networks}

\textit{Generative adversarial networks} (GANs) \cite{goodfellow2014generative} are another recent class of generative models that, in theory, can generate synthetic data in different domains. GANs are composed of two independent NNs: a \textit{generator} and a \textit{discriminator}. In its most basic form, the generator takes random noise as input and transforms it into a fake example. The discriminator is a binary classifier that discriminates examples as \textit{fake} (0) or \textit{real} (1). The generator and discriminator architectures depend on the generative task that one wants to perform with the GAN. For example, Figure \ref{fig:gan} illustrates a GAN for handwritten digit generation. The random noise is a matrix $M$ representing a grayscale image, which can be generated by sampling values between 0 (black) and 1 (white) from a uniform distribution. Typically, the generator uses convolutional layers to transform $M$ into a fake handwritten digit. The discriminator then combines real and fake images to learn how to separate images between fake and real.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.9\columnwidth]{imgs/background/gan.png}
 \caption{Diagram of a generative adversarial network \cite{silva2018intuitive}.}
 \label{fig:gan}
\end{figure}

Training a GAN consists of training the generator and the discriminator together
iteratively in alternating periods:

\begin{enumerate}
    \item \textbf{The discriminator trains for one or more epochs.}

    The discriminator is trained with a loss function that penalizes it for misclassifying a real instance as fake or a fake instance as real. The discriminator updates its weights through backpropagation from the discriminator loss through the discriminator network.

    \item \textbf{The generator trains for one or more epochs.}

    The generator is trained with a loss function that penalizes it for producing a sample that the discriminator  classifies as fake. In other words, the generator loss is computed via the discriminator. Thus, the generator updates its weights through backpropagation from the discriminator to the generator.
\end{enumerate}

The original GAN uses a single loss function called \textit{minimax loss} to train both the generator and the discriminator \cite{goodfellow2014generative}. Minimax loss is derived from the cross-entropy between the real and generated distributions. The generator tries to minimize this loss while the discriminator tries to maximize it.
% Equation \ref{eq:minimax_loss} formally defines the minimax loss, where $D(x)$ is the discriminator's estimate of the probability that real data instance x is real, $E_x$ is the expected value over all real data instances, $G(z)$ is the generator's output when given noise z, $D(G(z))$ is the discriminator's estimate of the probability that a fake instance is real, and $E_z$ is the expected value over all generated fake instances $G(z)$.
%
% \begin{equation} \label{eq:minimax_loss}
%     E_x[log(D(x))] + E_z[log(1 - D(G(z)))]
% \end{equation}
During training, $G$ learns to generate fake data that resembles the original data, and $D$ learns to distinguish the generator's fake data from real data. The training process aims to have a generator $G$ that produces output that can fool the discriminator $D$. After training, one can generate new data points by sampling from different distribution points learned by $G$.

GANs work well to generate continuous data such as images \cite{brock2018large}, but it has limitations in generating categorical data, especially in a sequential form such as text or music. Images can be represented by a continuous matrix $M$ and so applying transformations (e.g., $M' = M + 0.05$) to $M$ still results in defined images $M'$ that can be classified as real or fake. In sequential domains such as text and music, tokens are encoded using embedding vectors. Applying transformation to an embedding vector does not necessarily generate a valid token. For example, assume that the word ``university'' is represented by the vector $v = [0.44, 0.37, -0.28]$. If one applies the transformation $v' = v + 0.05$ to the original $v$, the new vector $v'$ not necessarily represent some word in the vocabulary. Therefore, updating the weights of $G$ with the gradients of the minimax loss might lead $G$ to generate invalid data. Another problem is that the discriminator can only provide feedback on entire sequences.

% TODO: Better summarize and correlate the different approaches
Different approaches have been proposed to solve these problems in the domain of symbolic music generation. For example, C-RNN-GAN \cite{mogren2016c} encodes MIDI files as a sequence of continuous note events. Each note is a tuple $(l, p, i, dt)$, where $l$ is the note length, $p$ is the pitch frequency, $i$ is note intensity, and $dt$ is the time elapsed since the last note. With this encoding scheme, C-RNN-GAN used RNNs for both the generator and the discriminator. SeqGAN \cite{yu2017seqgan} combined adversarial training and reinforcement learning to generate monophonic music with a RNN generator and a convolutional discriminator. MidiNet \cite{yang2017midinet} encodes MIDI files as a sequence of fixed-size piano rolls $M \in \{0, 1\}^{128*w}$, where $w$ is the number of time steps in each piano roll. Since each piano roll can be seen as a grayscale image, SeqGAN uses convolutional layers in the generator and the discriminator. MuseGAN \cite{dong2018musegan} also uses a piano roll encoding with a convolutional generator and discriminator to generate polyphonic music. \citet{muhamed2021symbolic} proposed a GAN where both the generator and the discriminator are transformers. The music pieces are encoded with the Music Transformer encoding scheme \cite{huang2018music}, and the Gumbel-Softmax trick \cite{jang2016categorical} is used to address the gradient problem of categorical generators.

\section{Decoding}

As discussed in the previous section, most neural generative models for AMC are based on RNNs, LSTMs, or Transformers\footnote{Including VAEs and GANs, which typically use RNNs, LSTMs, or Transformers as building blocks.}. These sequential models use a softmax activation function in the output layer to create a LM $L = P(x_t|x_1, \cdots, x_{t-2}, x_{t-1})$, where $\{x_1, \cdots, x_{t-2}, x_{t-1}\}$ is an input sequence and $x_t$ is the next token in that sequence. Typically, an \textit{autoregressive} strategy is used to generate music with $L$, i.e. to \textit{decode} the softmax output into a sequence of music tokens. One starts with a prior sequence of tokens $x = \{x_1, x_2, \cdots, x_{t 1}\}$, which is fed into $L$ to generate $L(x) = x_{t}$. Next, $x_{t}$ is concatenated with $x$ and the process repeats until a special end-of-piece token is found or a given number of tokens is generated. As defined in Equation \ref{eq:autoregressive}, autoregressive generation assumes that the probability distribution of a sequence of tokens can be decomposed into the product of conditional next token distributions.

\begin{equation} \label{eq:autoregressive}
    P(x_t|x_1, \cdots, x_{t-2}, x_{t-1}) = \prod_{t=1}^{T} P(x_t|x_{1:t-1})
\end{equation}

Currently, most prominent autoregressive strategies for music (and text) decoding are based either on sampling or searching. While sampling is well suited for creative tasks such as music composition, searching better fits generative problems where specific solutions are expected, such as machine translation.

\subsection{Top-k Sampling}

In its most basic form, sampling consists of randomly picking the next token according to the conditional probability distribution given by the LM: $x_t \sim P(x_t|x_{1:t-1})$. Figure \ref{fig:topk} shows and example of text generation with the prior sequence $x = \{\textit{The}\}$. In the first step, the word \textit{ant} is sampled from the condition probability distribution $P(x_2 | \textit{The})$ and, in the second step, the word \textit{sleeps} is sampled from $P(x_3 | \textit{The}, \textit{ant})$.

\begin{figure}[!h]
 \centering
 \includegraphics[width=\columnwidth]{imgs/background/sampling.png}
 \caption{Example of sampling with prior sequence $x = \{\textit{The}\}$ \cite{platen2020}.}
 \label{fig:topk}
\end{figure}

A simple trick called \textit{temperature} can be applied to control the confidence of the LM. It consists of dividing the LM output before the softmax activation by a parameter $t > 0$. Lower temperatures $t < 1$ make the model increasingly confident in its top choices, while $t > 1$ decreases confidence. Figure \ref{fig:temp} shows the previous example with temperature $t = 0.7$. The conditional probability $P(x_2 | \textit{The})$ of the first step becomes more confident, leaving almost no chance for the word \textit{ant} to be selected. Thus, the word \textit{big} is sampled first, followed by the word \textit{dog}.

\begin{figure}[!h]
 \centering
 \includegraphics[width=\columnwidth]{imgs/background/temperature.png}
 \caption{Example of sampling with prior sequence $x = \{\textit{The}\}$ and temperature $t = 0.7$ \cite{platen2020}.}
 \label{fig:temp}
\end{figure}

\textit{Top-k sampling} \cite{fan2018hierarchical} is another way to control the probability distribution of the LM. It consists of using only the $k$ most likely tokens in the distribution, redistributing the probability mass among only those \textit{top-k} tokens. With this approach, the LM is filtered at each generation step and the token is picked randomly according to the resuling probability distribution. Figure \ref{fig:topk} illustrates the previous example with top-k sampling ($k=6$).

\begin{figure}[!h]
 \centering
 \includegraphics[width=\columnwidth]{imgs/background/topk.png}
 \caption{Example of top-k sampling with $k=6$.}
 \label{fig:topk}
\end{figure}

In the first step, the top-k sampling keeps the six words $\{\textit{big}, \textit{small}, \textit{cat}, \textit{dog}, \textit{boy}, \textit{man}\}$ in the sampling pool, which encompass only two thirds of the probability mass. The words $\{\textit{people}, \textit{tall}, \textit{house}, \textit{ant}\}$ are eliminated, even though they seem like reasonable candidates. In the second step, the top six words represent almost all of the probability mass. Three of the selected words $\{\textit{stops}, \textit{down}, \textit{a}\}$ are arguably bad candidates. Nevertheless, the eliminated words $\{\textit{not}, \textit{the}, \textit{small}, \textit{told}\}$ are rather bad candidates and hence successfully eliminated. This example highlights that top-k sampling can jeopardize the LM, making it produce wrong sentences for sharp distributions and limiting the model's creativity for flat distributions.

\subsection{Top-p (Nucleus) Sampling}

Holtzman et. al. \cite{holtzman2019curious} proposed \textit{top-p (nucleus) sampling} to address the degeneration problems faced by top-k sampling (and other decoding strategies). Instead of limiting the sample pool to a fixed size $k$, top-p samples from a dynamic \textit{nucleus}, i.e. the smallest set of tokens whose cumulative probability exceeds a given probability $p$. Thus, the size of the sample pool is dynamically adjusted for each step depending on the LM distribution. Figure \ref{fig:topp} shows the previous example with top-p sampling ($p=0.92$).

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.9\columnwidth]{imgs/background/topp.png}
 \caption{Example of top-p sampling with $p=0.92$ \cite{platen2020}.}
 \label{fig:topp}
\end{figure}

The nucleus for $p=92\%$ includes the nine most likely words in the first step and only three words in the second step. This example highlights that top-p sampling keeps a wide range of tokens in less predictable situations (e.g., in the first step) and a few tokens in more predictable situations (e.g., in the second step). Although top-p is theoretically more interesting than top-k, both methods work well in practice. Top-p can also be combined with top-k to avoid very low ranked tokens while allowing for some dynamic selection.

\subsection{Greedy Search}

Greedy search selects the token with the highest probability at each generation step $t$: $x_t = argmax \, P(x_t|x_{1:t-1})$. Figure \ref{fig:greedy} shows a text generation example with greedy decoding and prior sequence $x = \{\textit{The}\}$.
In the first step, greedy search selects the word $\textit{nice}$, which has the highest probability among the three options $\{ \textit{dog}, \textit{nice}, \textit{car} \}$. In the second step, the options are $\{ \textit{woman}, \textit{house}, \textit{guy} \}$ and hence the greedy search selects the word \textit{woman}. The final generated sentence is \textit{The nice woman} with a joint probability of $0.5*0.4=0.2$.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.5\columnwidth]{imgs/background/greedy.png}
 \caption{Example of greedy search with prior sequence $x = \{\textit{The}\}$ \cite{platen2020}.}
 \label{fig:greedy}
\end{figure}

The problem with greedy search is that it misses high probability tokens ``hidden behind'' low probability ones. In this example, the global optimal solution is the sentence \textit{The dog has} (with joint probability $0.36$), but the word \textit{nice} has higher probability than \textit{dog} in the first step. Thus, greedy search selects \textit{nice} and completely disregards the branch with the word \textit{dog} in the second step.

\subsection{Beam Search}

\textit{Beam search} reduces the risk of missing hidden high probability tokens by keeping the most likely $b$ solutions (called \textit{beams}) at each time step, where $b$ is a parameter called \textit{beam width}. At the final generation step, the solution (or beam) with the highest joint probability is selected. Figure \ref{fig:beam} shows how beam search is capable of finding the best solution of the previous example with beam size $b=2$.

\begin{figure}[!h]
 \centering
 \includegraphics[width=0.5\columnwidth]{imgs/background/beam.png}
 \caption{Example of beam search with prior sequence $x = \{\textit{The}\}$ and
 beam size $b=2$ \cite{platen2020}.}
 \label{fig:beam}
\end{figure}

In the first step, the two most likely sub-sequences are $\{ \textit{The}, \textit{dog} \}$ and $\{ \textit{The}, \textit{nice} \}$. In the second step, beam search expands the beam from the previous step with the two most likely sub-sequences $\{ \textit{The}, \textit{dog}, \textit{has} \}$ and $\{ \textit{The}, \textit{nice}, \textit{woman} \}$. At the end of the second step, beam search returns the beam with highest probability, which is \textit{The dog has} with joint probability $0.36$. The solutions generated with beam search are always as good as or better (more likely according to the LM) than the sequences generated with greedy search. However, beam search is not guaranteed to find the optimal solution.

A common problem with beam search (and other search-based approaches) is that the decoded sequences tend to become repetitive in a few generation steps \cite{holtzman2019curious}. This problem can be alleviated by combining beam search with sampling (including top-k and top-p) to create a \textit{stochastic beam search} \cite{poole2010artificial}. In this case, the sub-sequences are sampled at each time step according to their joint probability, instead of selected greedly.

Different \textit{decoding} strategies (e.g., top-k sampling and beam search) can be used to generate music with different NN \textit{architectures} (e.g., LSTM, Transformer, VAEs, and GANs) trained with different \textit{symbolic music} datasets (e.g. MAESTRO, Lakh, and JSB Chorales). This chapter presented an overview of these fundamental ideas of deep learning for music generation. This dissertation builds upon these ideas for controlling the emotion of music composed with neural LMs. The next chapter reviews previous works that are related to this dissertation, including methods to control neural LMs and other AMC systems that generate music with a target emotion.


\chapter{Linear Algebra Operations}
\label{chapter:blas}

We provide the parallel implementations of the BLAS
(Basic Linear Algebra Subprograms).  The BLAS routines
are categorized into vector-vector, matrix-vector, and
matrix-matrix operations called
level-1, level-2, and level-3 BLAS.

\section{Vector-Vector Operations}

\section{Matrix-Vector Operations}

\section{Matrix-Matrix Operations}

\section{Local BLAS Routines}

We provide localized implementations of the BLAS routines.
These routines can only be called if the data for each object
all reside within one processor (or duplicated).
Their interfaces are the same as the general BLAS routines,
except that the invocation has the form {\tt PLA\_Local\_}
followed by the name of the BLAS routine in lowercase
(i.e., {\tt PLA\_Local\_gemm}).


\documentclass[11pt,oneside]{article}

\usepackage{amsmath,amsfonts,amssymb}
\usepackage{mathtools}
\usepackage{bm}
\usepackage[shortlabels]{enumitem}
\usepackage[hidelinks]{hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usepackage{booktabs}
\usepackage{color,xcolor}
\usepackage[paperheight=11in,paperwidth=8.5in,margin=1.25in]{geometry}
\usepackage{float}
\usepackage{subcaption}

\usepackage{cancel}

\usepackage{rotating}
\usepackage{listings}
\usepackage{tagging}
\usepackage{hyphenat}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{multirow}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}% \theoremstyle{plain}
\newtheorem{exercise}{Exercise}[section]
\newtheorem*{exercise*}{Exercise}
\theoremstyle{remark}
\newtheorem*{solution}{Solution}
\newtheorem*{notation}{Notation}
\newtheorem*{terminology}{Terminology}
\newtheorem*{remark}{Remark}

\usepackage{mathtools}
\renewcommand{\doteq}{\vcentcolon=}

\usepackage{chngcntr}
\numberwithin{equation}{section}
% \numberwithin{equation}{subsection}

\linespread{1.25}

\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator{\sech}{sech}
\DeclareMathOperator{\csch}{csch}
\DeclareMathOperator{\prox}{prox}
\DeclareMathOperator{\dom}{dom}

% \pagecolor[RGB]{48,54,66}
% \color[RGB]{245,246,247}

\definecolor{myorange}{rgb}{0.70,0.56,0.42}
\definecolor{mygreen}{rgb}{0.10,0.45,0.25}
\definecolor{myblue}{rgb}{0.22,0.50,0.50}
\definecolor{mypurple}{rgb}{0.46,0.37,0.60}
\definecolor{myred}{rgb}{0.60,0.32,0.32}

% -------------------------- %
\newcommand{\misha}[1]{{\color{myred}{#1}}}
\newcommand{\colin}[1]{{\color{mypurple}{#1}}}
\newcommand{\craig}[1]{{\color{mygreen}{#1}}}
%----------------------------%

\setcounter{tocdepth}{1}

\title{ Project list for Integration Workshop}
\author{Colin Clark \\  Graduate Program in Applied Mathematics,\\ University of Arizona, Tucson}
\date{August 2021}

\begin{document}

\maketitle

\subsection*{Objectives:}
\begin{enumerate}
\item Making sure each student has a basic working knowledge of Matlab, Python, Julia, or some other high-level programming language for scientific computing.
\item Making sure each student has a basic working knowledge of \LaTeX.
\item Making sure students can use computational approaches to solve math problems (turning mathematics into computer code).
\item Planning, writing, sharing, and discussing algorithms in a group setting.
\item Having fun!
\end{enumerate}

\newpage

\input{fredholm-alternative_least-squares}
\newpage

\input{lagrange-interpolants}
\newpage

\input{complex-contour-integrals}
\newpage

\input{circulant-matrices}
\newpage

\input{ode-solver-first-order}
\newpage

\input{random-walk}
\newpage

\input{bias-vs-variance}
\newpage

\input{random-triangles-high-dimensions}
\newpage

\end{document}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header2.tex}

\title{Phys 221A -- Quantum Mechanics -- HW03}
\author{UCLA, Fall 2014}
\date{\formatdate{5}{11}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\textit{
Problems 2.17 (p. 152), 2.18 (p. 152), 2.19 (p. 152), 2.20 (p. 152), and 2.22 (p. 153) from the textbook (Sakurai and Napolitano, Modern Quantum Mechanics, 2nd edition).
}

\section*{Problem 2.17}
\textit{
Consider again a one-dimensional simple harmonic oscillator. Do the following algebraically---that is, without using wave functions.
}

\begin{enumproblem}

% part A
\item \textit{
Construct a linear combination of $\ket{0}$ and $\ket{1}$ such that $\avg{x}$ is as large as possible.
}

% part B
\item \textit{
Suppose the oscillator is in the state constructed in (a) at $t=0$. What is the state vector for $t>0$ in the Schroedinger picture? Evaluate the expectation value $\avg{x}$ as a function of time for $t>0$, using (i) the Schroedinger picture and (ii) the Heisenberg picture.
}

% part C
\item \textit{
Evaluate $\avg{(\Delta x)^2}$ as a function of time using either picture.
}

\end{enumproblem}

\section*{Problem 2.18}
\textit{
Show that for the one-dimensional simple harmonic oscillator,
\begin{eqn}
\matrixel{0}{e^{ikx}}{0} = \exp \left[ -\frac{k^2}{2} \matrixel{0}{x^2}{0} \right],
\end{eqn}
where $x$ is the position \textbf{operator}.
}

\section*{Problem 2.19}
\textit{
A coherent state of a one-dimensional simple harmonic oscillator is defined to be an eigenstate of the (non-Hermitian) annihilation operator $a$:
\begin{eqn}
a \ket{\lambda} = \lambda \ket{\lambda},
\end{eqn}
where $\lambda$ is, in general, a complex number.
}

\begin{enumproblem}

% part A
\item \textit{
Prove that
\begin{eqn}
\ket{\lambda} = e^{-\abs{\lambda}^2 / 2} e^{\lambda a^\dagger} \ket{0}
\end{eqn}
is a normalized coherent state.
}

% part B
\item \textit{
Prove the minimum uncertainty relation for such a state.
}

% part C
\item \textit{
Write $\ket{\lambda}$ as
\begin{eqn}
\ket{\lambda} = \sum_{n=0}^\infty f(n) \ket{n}.
\end{eqn}
Show that the distribution of $\abs{f(n)}^2$ with respect to $n$ is of the Poisson form. Find the most probable value of $n$, and hence of $E$.
}

% part D
\item \textit{
Show that a coherent state can also be obtained by applying the translation (finite-displacement) operator $e^{-ipl/\hbar}$ (where $p$ is the momentum operator and $l$ is the displacement distance) to the ground state. (See also Gottfried 1966, 262-64.)
}

\end{enumproblem}

\section*{Problem 2.20}
\textit{
Let
\begin{eqn}
J_\pm = \hbar a_\pm^\dagger a_\mp, \qquad
J_z = \frac{\hbar}{2} (a_+^\dagger a_+ - a_-^\dagger a_-), \qquad
N = a_+^\dagger a_+ + a_-^\dagger a_-,
\end{eqn}
where $a_\pm$ and $a_\pm^\dagger$ are the annihilation and creation operators of two \textbf{independent} simple harmonic oscillators satisfying the usual simple harmonic oscillator commutation relations. Prove
\begin{eqn}
[J_z, J_\pm] = \pm \hbar J_\pm, \qquad
[\v{J}^2, J_z] =0, \qquad
\v{J}^2 = \left( \frac{\hbar^2}{2} \right) N \left[ \left( \frac{N}{2} \right) + 1 \right].
\end{eqn}
}

\section*{Problem 2.22}
\textit{
Consider a particle of mass $m$ subject to a one-dimensional potential of the following form:
\begin{eqn}
V =
\begin{cases}
\frac{1}{2} k x^2 & \text{for $x > 0$} \\
\infty & \text{for $x < 0$.}
\end{cases}
\end{eqn}
}

\begin{enumproblem}

% part A
\item \textit{
What is the ground-state energy?
}

% part B
\item \textit{
What is the expectation value $\avg{x^2}$ for the ground state?
}

\end{enumproblem}

\end{document}


\section{Utils}

\lst{Fast IO}{}{utils/fastio.cc}

%TODO(Alex): Make output routine better
\lst{128 Bit Integer}{}{utils/128BitInt.cc}

\lst{Buildin}{}{utils/buildin.cpp}

\lst{Bit Operations}{}{utils/bits.cc}

\lst{Ternary Search}{$\mathcal{O}(\log (r - l))$}{utils/ternarySearch.cc}


% !TEX root = cs1textbook.tex

\chapter{Simple Selection}
\label{chapter:if}

\minitoc

Now that we know how to store numbers in a Java program, it's important to learn how to ask a program to make decisions for us.

\section{True and False Values}
Numbers are not the only things we need to store in memory.  It is important to be able to represent the values \mintinline{java}{true} and \mintinline{java}{false} also, and the \mintinline{java}{boolean} data type does this for us.

A \mintinline{java}{boolean}\index{Primitive Data Types!boolean@\mintinline{java}{boolean}} variable can store either true or false, as seen here:

\begin{minted}{java}
boolean b1 = true;
boolean b2 = false;
\end{minted}

A \mintinline{java}{boolean} variable can also store the results of a \textit{boolean expression}, using \textit{relational operators}, as seen in this example:

\begin{minted}{java}
int i1 = 15;
int i2 = 12;
boolean b1 = i1 > i2;   // b1 stores true
boolean b2 = i1 < i2;   // b2 stores false
\end{minted}

\begin{defn}{Boolean Expression}
A \textbf{boolean expression} is an expression that can be either true or false.
\end{defn}

Your computer keyboard likely does not have a key with $\ge$ or $\le$ symbols on it, and so these operators are made up of two symbols each: \mintinline{java}{>=} is the ``greater than or equal'' symbol in Java, and \mintinline{java}{<=} is the ``less than or equal'' symbol.  Such a symbol, made up of two keystrokes, is called a \textit{digraph}, from the Greek roots for ``two'' and ``writing''.

Notice that the single equal sign \mintinline{java}{=}, which is used in assignment statements in Java, is not a relational operator.  The operator that tests for equality is a digraph composed of two equal signs: \mintinline{java}{==}.

\begin{minted}{java}
int i1 = 6;
int i2 = 6;
int i3 = 11;
boolean b1 = i1 == i3;   // b1 stores false
boolean b2 = i1 == i2;   // b2 stores true;
\end{minted}

You might find those last two expressions difficult to read; some programmers choose to add parentheses \textit{just for readability} in a case like this:

\begin{minted}[firstnumber=5]{java}
boolean b2 = ( i1 == i2 );
\end{minted}

\section{Simple Selection Statements}
\label{section:simple-selection}
As stated before, we can have our Java programs make decisions for us, by writing what some textbook authors call a \textit{selection statement}, and what other textbook authors call a \textit{conditional statement}, but what most programmers call an \textit{if statement} due to the required keyword.

\begin{javaformat}{Simple Selection,label=format:selection}
\begin{minted}{text}
if( <boolean-expression> )
    <statement>
\end{minted}
\end{javaformat}

The statement on the second line is \textit{indented} so that it's obvious that it's part of a larger structure.  That statement will only be performed if the boolean expression right before it is true.  At the end of this example, \mintinline{java}{x} will have the value 15, and \mintinline{java}{y} will have the value 6:

\begin{minted}{java}
int x = 12;
int y = 6;

if( x < y )
    y = 10;
if( x > y )
    x = 15;
\end{minted}

Note that line 6 of that example will run even though \mintinline{java}{x < y} is false, because the first selection statement ends at the end of line 5.  The \mintinline{java}{<statement>} in Java Format \ref{format:selection} is just that: a single statement.

Watch out for mistakes like this:
\begin{trap}{Indentation}\label{trap:selection}
\begin{minted}{java}
int x = 100;
int y = 1;
int z = 1;

if( x < 10 )
    y = 5;
    z = 5;      // Uh oh!
\end{minted}
\end{trap}

In this trap, the value of \mintinline{java}{y} will not be changed to 5 because the condition is false, but then the value of \mintinline{java}{z} \textit{will} be changed to 5, because the conditional statement already ended.  We use indentation to make it easy to recognize the structure of our code, but when the computer turns our source code into a program, it \textit{completely ignores} our indentation.  Whether the line that contains \mintinline{java}{z = 5} in Trap \ref{trap:selection} is indented or not \textit{does not change} how the computer interprets the code.

In order to run multiple statements when a condition is true, we must write a \textit{compound statement}, also called a \textit{block}.

\begin{defn}{Compound Statement}
A \textbf{compound statement}, also called a \textbf{block}, is a group of statements surrounded by curly braces.  The statements within a block should be indented for readability.
\end{defn}

The following code will change neither \mintinline{java}{y} nor \mintinline{java}{z} to 5, because both of those assignment statements are part of the compound statement that follows the condition:

\begin{minted}{java}
int x = 100;
int y = 1;
int z = 1;

if( x < 10 ) {
    // None of the code in this block will be executed
    y = 5;
    z = 5;
}
\end{minted}

The following trap also doesn't do what one might think after a quick glance:

\begin{trap}{Semicolon}
\label{trap:semicolon}
\begin{minted}{java}
int x = 100;
int y = 1;

if( x < 10 );
    y = 5;
\end{minted}
\end{trap}

Even though the condition in this trap is false, the value of \mintinline{java}{y} \textit{will be changed} to 5.  The trap here is that the semicolon after the condition counts as a statement, and so the statement \mintinline{java}{y = 5;}, while indented, exists \textit{after} the selection statement, and therefore will always be executed regardless of the condition.  It's easy to get accustomed to typing a semicolon every time you're about to hit the Enter key, but this is one scenario in which you should definitely not do so.

\begin{tip}{Automatic Indentation Tip}
Use a text editor that can automatically indent your code for you.  In emacs, you just have to enable the Java editing mode; you can use Control-I in Eclipse (Command-I on a Mac) to correct indentation.  If the code from Trap \ref{trap:semicolon} were automatically indented, it would look like this, which ought to help alert you to the bug:

\begin{minted}{java}
int x = 100;
int y = 1;

if( x < 10 );
y = 5;          // Not indented any more, because the if statement ended!
\end{minted}
\end{tip}

\section{Simple If-Else Statements}
\label{section:ifelse}
In Section \ref{section:simple-selection}, we saw code that examined a condition and ran some code only if the condition was true.  Sometimes, however, we need to react to a condition being false also.  In this case, we can write an \textit{if-else statement}:

\begin{javaformat}{Simple If-Else Statement}
\begin{minted}[escapeinside=||,mathescape=true]{text}
if( <boolean-expression> )
    <statement|$_\mathrm{T}$|>
else
    <statement|$_\mathrm{F}$|>
\end{minted}
\end{javaformat}

In this conditional, \texttt{<statement$_\mathrm{T}$>} will only run if the \texttt{<boolean-expression>} is true, and \texttt{<statement$_\mathrm{F}$>} will only run if the \texttt{<boolean-expression>} is false.  One or the other of those statements will definitely be executed, but which one depends on the condition.

Going back to our party guests scenario, perhaps our party will be hosted in the sort of location that charges \$3.99 per guest, but also assesses a minimum fee if the number of attendees doesn't meet a certain threshold:

\begin{minted}{java}
int numberOfGuests = 15;
double fee;

if( numberOfGuests <= 10 )
    fee = 39.99;
else
    fee = 39.99 + ( ( numberOfGuests - 10 ) * 3.99 );
\end{minted}

More interesting uses of conditional statements will be explored in Chapter \ref{chapter:complex-selection}.


\subsection{AVL-Trees}

\begin{frame}{Balanced Trees}{AVL-Tree}
  \textbf{AVL-Tree:}
  \begin{itemize}
    \item<2->
      Gregory Maximovich \textbf{A}delson-\textbf{V}elskii, Yevgeniy Mikhailovlovich \textbf{L}andis (1963)
    \item<3->
      Search tree with modified \texttt{\color{Mittel-Blau}insert} and
      \texttt{\color{Mittel-Blau}remove} operations while satisfying
      a {\color{Mittel-Blau}depth} condition
    \item<4->
      Prevents degeneration of the search tree
    \item<5->
      Height difference of left and right subtree is at maximum one
    \item<6->
      With that the height of the search tree is always
      ${\color{Mittel-Blau}O(\log n)}$
    \item<7->
      We can perform all basic operations in
      ${\color{Mittel-Blau}O(\log n)}$
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree}
  \begin{figure}
    \begin{adjustbox}{width=0.75\linewidth}
      \input{Images/AVL-Tree/AVL-Tree_Example1.tikz}
    \end{adjustbox}
    \caption{Example of an AVL-Tree}
    \label{fig:balanced_trees:avl_tree_example1}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree}
  \begin{figure}
    \begin{adjustbox}{width=0.75\linewidth}
      \input{Images/AVL-Tree/AVL-Tree_Example2.tikz}
    \end{adjustbox}
    \caption{\textbf{Not} an AVL-Tree}
    \label{fig:balanced_trees:avl_tree_example2}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree}
  \begin{figure}
    \begin{adjustbox}{width=0.75\linewidth}
      \input{Images/AVL-Tree/AVL-Tree_Example3.tikz}
    \end{adjustbox}
    \caption{Another example of an AVL-Tree}
    \label{fig:balanced_trees:avl_tree_example3}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree - Rebalancing}
  \textbf{Rotation:}
  \vspace{-2.5em}
  \begin{columns}
    \begin{column}{0.35\linewidth}
      \begin{figure}
        \begin{adjustbox}{width=0.9\linewidth}
          \input{Images/AVL-Tree/AVL-Tree_RotateA.tikz}
        \end{adjustbox}
        \vspace{-0.5em}
        \caption{Before rotating}
        \label{fig:balanced_trees:avl_tree_rotate_1}
      \end{figure}
    \end{column}
    \begin{column}{0.1\linewidth}
      \begin{center}
        $\Rightarrow$
      \end{center}
    \end{column}
    \begin{column}{0.35\linewidth}
      \begin{figure}
        \begin{adjustbox}{width=0.9\linewidth}
          \input{Images/AVL-Tree/AVL-Tree_RotateB.tikz}
        \end{adjustbox}
        \vspace{-0.5em}
        \caption{After rotating}
        \label{fig:balanced_trees:avl_tree_rotate_2}
      \end{figure}
    \end{column}
   \end{columns}
  \begin{itemize}
    \item<2->
      Central operation of {\color{Mittel-Blau}rebalancing}
    \item<3->
      After rotation to the right:
      \begin{itemize}
        \item<4->
          Subtree {\color{Mittel-Blau}$A$} is a layer higher and subtree
          {\color{Mittel-Blau}$C$} a layer lower
        \item<5->
          The parent child relations between nodes {\color{Mittel-Blau}$x$} and
          {\color{Mittel-Blau}$y$} have been swapped
      \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree - Rebalancing}
  \textbf{AVL-Tree:}
  \begin{itemize}
    \item<2->
      If a height difference of $\pm 2$ occurs on an
      \texttt{\color{Mittel-Blau}insert} or
      \texttt{\color{Mittel-Blau}remove} operation the tree is rebalanced
    \item<3->
      Many different cases of rebalancing
    \item<4->
      \textbf{Example:}
      \texttt{\color{Mittel-Blau}insert} of $1,2,3,\dotsc$
  \end{itemize}
  \onslide<6->
  \begin{figure}
    \includegraphics[width=0.5\linewidth]
      {Images/AVL-Tree/AVL-Tree_Insert1To10.png}
    \caption{Inserting $1,\dots,10$ into an AVL-tree~\cite{gnarley_trees}}
    \label{fig:balanced_trees:avl_tree_example4}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Balanced Trees}{AVL-Tree - Summary}
  \textbf{Summary:}
  \begin{itemize}
    \item<2->
      Historical the first search tree providing guaranteed
      \texttt{\color{Mittel-Blau}insert}, \texttt{\color{Mittel-Blau}remove}
      and \texttt{\color{Mittel-Blau}lookup} in
      ${\color{Mittel-Blau}O(\log n)}$
    \item<3->
      However not amortized update costs of {\color{Mittel-Blau}$O(1)$}
    \item<4->
      Additional memory costs:
      We have to save a height difference for every node
    \item<5->
      Better (and easier) to implement are {\color{Mittel-Blau}(a,b)}-trees
  \end{itemize}
\end{frame}