\documentclass[10pt,aps,twocolumn,secnumarabic,balancelastpage,amsmath,amssymb,nofootinbib,floatfix]{revtex4}

%%\usepackage{setspace}
%%\setstretch{1.25}
\usepackage{graphicx}      % tools for importing graphics
%\usepackage{lgrind}        % convert program code listings to a form
                            % includable in a LaTeX document
%\usepackage{xcolor}        % produces boxes or entire pages with
                            % colored backgrounds
%\usepackage{longtable}     % helps with long table options
%\usepackage{epsf}          % old package handles encapsulated postscript issues
\usepackage{bm}            % special bold-math package. usge: \bm{mathsymbol}
%\usepackage{asymptote}     % For typesetting of mathematical illustrations
%\usepackage{thumbpdf}

\usepackage[colorlinks=true]{hyperref}  % this package should be added after
                                        % all others.
                                        % usage: \url{http://web.mit.edu/8.13}

\renewcommand{\baselinestretch}{1.0}

\begin{document}
\title{Measurement of the Free Fall Acceleration Using a Simple Pendulum}
\author{MIT Department of Physics}
\email{nodbody\@mit.edu}
\homepage{http://web.mit.edu/8.13/}
\date{\today}
%\affiliation{MIT Department of Physics}

%-------------------------------------------------------------------------------
\begin{abstract}
  The objective of this experiment is to measure the free fall acceleration using a pendulum. A pendulum will be constructed and measurements of its period will be performed. Using the small angle approximation the free fall acceleration will be determined. The simple experimental setup and execution will allow the experimenter to exercise all important ingredients for later experiments.
\end{abstract}

\maketitle

%-------------------------------------------------------------------------------
\section*{Preparatory questions}
There is not preparatory work required, except for remembering classical mechanics (8.01 or flavors of it).

%-------------------------------------------------------------------------------
\section{Introduction and Theory}

The pendulum is one of the classical examples used in the study of mechanics. When an object is suspended from a point and left to hang freely at a string, as shown in Figure~\ref{fig:pendulum} it will oscillate around the lowest position by a set frequency. Using Newton's Laws and realizing that only the force perpendicular to the string leads to a non-zero acceleration we find
%
\begin{equation}
  \vec{F} = m \vec{a} \ ; ~~ \to ~~ F_{\perp} = m g \sin\theta \ ,
\end{equation}
%
where $g$ denotes the free fall acceleration.
%
\begin{figure}[!h]
\centering\includegraphics[width=0.2\textwidth]{figs/pendulum.pdf}
\caption{\label{fig:pendulum} Schematic drawing of simple pendulum showing forces on mass.}
\label{spend}
\end{figure}
%
Assuming that the angle of the displacement is small $\sin\theta \approx \theta$. This is a version of Hook's law if we replace the angular displacement with the real displacement $x_\perp = \ell \theta$ resulting in the following equation
%
\begin{equation}
  F_{\perp} = m\frac{g}{\ell} \ell\theta = m\frac{g}{\ell} x_\perp
  ~~\to~~
  m a_\perp = m\frac{g}{\ell} x_\perp \ .
\end{equation}
%
The differential equation for the displacement is solved and we find the solution as a function of time $t$ to be
%
\begin{equation}
  x_\perp = A \sin \left( \sqrt{ \frac{g}{\ell} } t - \phi \right) \ .
\end{equation}
%
The two parameters, A, for amplitude and, $\phi$, for phase, have to be determined by the initial condition of the system. For example, the size of the displacement and velocity of the pendulum have to be given at a time $t$.
%
When dividing the displacement by the length of the string we obtain the angular displacement and the corresponding equation of motion
%
\begin{equation}\label{eq:theta_of_t}
  \theta = \theta_\text{max} \sin \left( \sqrt{ \frac{g}{\ell} } t - \phi \right) \ ,
\end{equation}
%
where $\theta_{max}$ is the maximal angular displacement.

Given the above derivation it is important to note that the frequency and period of the pendulum are {\it independent} of the initial angular displacement amplitude, the phase and more importantly of the mass. The period $T$ for a simple pendulum, while not dependent on the mass, does depend on the length $\ell$ of the string and the value of the gravitational field strength $g$, according to
\begin{equation}\label{eq:period}
  T = 2 \pi \sqrt{\frac{\ell}{g}} \ .
\end{equation}
We can rewrite to solve for $g$ as
\begin{equation}
  g = 4 \pi^2 \frac{\ell}{T^2} \ .
\end{equation}

%-------------------------------------------------------------------------------
\section{Procedure}

\begin{itemize}
\item[1.] Obtain a string of length $\ell$ and a weight of mass $m$.
\item[2.] Measure the length of the pendulum to the middle of the pendulum bob.  Record the length of the pendulum.
\item[3.] Set the pendulum in motion until it completes $N$ complete oscillations, taking care to record this time. Then the period $T$ for one oscillation is just the number recorded divided by $N$.
\item[4.] Make a total of eight measurements for $g$ using two different masses at four different values for the length $\ell$.
\item[5.] Determine a mean value for $g$ and determine the uncertainty $\Delta g$ in the measurement.
\item[6.] Report $g \pm \Delta g$ and conclude.
\end{itemize}

%-------------------------------------------------------------------------------
\section{Investigating the Small Angle Approximation}

In deriving Equation~\ref{eq:theta_of_t} above, the small angle approximation $\sin\theta \approx \theta$ had been used. Here we want to investigate how the period in Equation~\ref{eq':period} is modified for oscillations at larger angles. Following Reference~\cite{cite:kidd-fogg}, we can approximate
\begin{equation} \label{eq:period-larger}
  T \approx 2 \pi \sqrt{\frac{\ell}{g \cos (\theta_\text{max}/2)}} \ ,
\end{equation}
for maximum angle $\theta_\text{max}$. Then,
\begin{equation}
  T = T_0 \left[ 1 + \frac{\theta_\text{max}^2}{16} + \frac{7 \theta_\text{max}^4}{1536} + .... \right] \ ,
\end{equation}
where $T_0 = 2 \pi \sqrt{\frac{\ell}{g}}$.

\begin{figure}[!h]
  \centering\includegraphics[width=0.4\textwidth]{figs/large-angle.pdf}
  \caption{\label{fig:period-larger} Exact value of $M=T/T_0$ (dotted curve) and the cosine approximation (solid curve) plotted against the angular half-amplitude.}
\end{figure}

Fix $\ell$ and $m$ and measure the period $T$ as a function of the starting angle $\theta_\text{max}$.  Plot the ratio of the measured period to the small angle approximation as a function of $\theta_\text{max}$. Compare to Equation~\ref{eq:period-larger} and Figure~\ref{fig:period-larger}.

Choose at least four different initial displacements and repeat each setup several times to obtain more accurate results. Make sure to go back to the last section and see whether the angle you chose for the experiment are not affected by this discrepancy. Quote the systematic uncertainty on $g$ due to this effect.

%-------------------------------------------------------------------------------
\section{Other uncertainties to consider}

There are a number of auxiliary measurements that have to be perfromed to setup and perform the complete experiment. There is the length of the pendulum, and the time for the
oscillations. Both have to be done carefully and various options should be considered.

Please, carefully propagate the uncertainty on period measurements and length measurements into your final result.

To reduce the uncertainty it is useful for each setup to make several measurements and average those. It is important to make sure those setups are reproducible as to not average values of different setups.

\subsection{Length of the pendulum}

The length of the pendulum has to be carefully determined. There are two points that are of importance: the suspension point and the bob itself. Also you have to consider the accuracy of the meter you are using.

Please, make sure to think about the bob. Does the size of the bob matter? You might want to try different sizes. Also is the derivation of the period correct for large bobs?

\subsection{Measurement of the time}

To measure the period one has to decide on a start and stop time. It is critical to define those times well. Which point of the trajectory is more precise, the point of maximal displacement or the point of highest velocity? Should you measure one period or several at once? As experimentalists it is best to think and then try. Can you improve your strategy and what is the resulting uncertainty?

Also, everybody today has cell phones, a recording might be even more precise.

\bibliography{pendulum-intro-experiment}

\end{document}


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

%%% \documentclass{article}
%%% \usepackage{graphicx}
%%% \usepackage{color}

%%% \sloppy
%%% \definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

%%% \begin{document}

\subsection*{testM}

\begin{par}
Example for algorithm testM. Algorithm is usefull only for testing QWTB toolbox. It calculates maximal and minimal value of the record. MCM is calculated by wrapper.
\end{par} \vspace{1em}
\begin{par}
See also \lstinline{qwtb}
\end{par} \vspace{1em}

\subsubsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Generate sample data
   \item Call algorithm
   \item Plot results
\end{itemize}

\subsubsection*{Generate sample data}

\begin{par}
Two quantities are prepared: \lstinline{x} and \lstinline{y}.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
x = []; y = [];
x.v = [1:20];
y.v = [1:14 13:-1:8];
\end{lstlisting}
\begin{par}
All uncertainties are set to 1.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
x.u = x.v.*0 + 1;
y.u = y.v.*0 + 1;
\end{lstlisting}
\begin{par}
Quantities are put into data input structure \lstinline{DI}.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DI = [];
DI.x = x;
DI.y = y;
\end{lstlisting}
\begin{par}
Create calculation settings \lstinline{CS} and set uncertainty calculation method to Monte Carlo method. Allow randomization of uncertainties by the QWTB toolbox.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
CS = [];
CS.unc = 'mcm';
CS.mcm.randomize = 1;
\end{lstlisting}

\subsubsection*{Call algorithm}

\begin{par}
Use QWTB to apply algorithm \lstinline{testM} to data \lstinline{DI} with calculation settings \lstinline{CS}.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DO = qwtb('testM', DI, CS);
\end{lstlisting}

        \begin{lstlisting}[style=output]
QWTB: default correlation matrix generated for quantity `x`
QWTB: quantity x was randomized by QWTB
QWTB: default correlation matrix generated for quantity `y`
QWTB: quantity y was randomized by QWTB
QWTB: uncertainty calculation by means of wrapper or algorithm
\end{lstlisting} \color{black}

\subsubsection*{Plot results}

\begin{par}
Plot input data and calculated maximal and minimal values as a red and green lines with uncertainties represented by dashed lines.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
figure
hold on
errorbar(DI.x.v, DI.y.v, DI.y.u, 'xb')
plot([DI.x.v(1) DI.x.v(end)], [DO.max.v DO.max.v], '-r', 'linewidth', 3)
plot([DI.x.v(1) DI.x.v(end)], [DO.max.v - DO.max.u DO.max.v - DO.max.u], '--r', 'linewidth', 3)
plot([DI.x.v(1) DI.x.v(end)], [DO.min.v DO.min.v], '-g', 'linewidth', 3)
plot([DI.x.v(1) DI.x.v(end)], [DO.min.v - DO.min.u DO.min.v - DO.min.u], '--g', 'linewidth', 3)
plot([DI.x.v(1) DI.x.v(end)], [DO.max.v + DO.max.u DO.max.v + DO.max.u], '--r', 'linewidth', 3)
plot([DI.x.v(1) DI.x.v(end)], [DO.min.v + DO.min.u DO.min.v + DO.min.u], '--g', 'linewidth', 3)
legend('original data (DI.x.v, DI.y.v)', 'line at maximum value (DO.max.v)', 'uncertainty',  'line at minimum value (DO.min.v)', 'uncertainty', 'location', 'southoutside')
xlabel('quantity x')
ylabel('quantity y')
title('input data and results of testM algorithm')
hold off
\end{lstlisting}

\begin{center}
\includegraphics[width=0.7\textwidth]{algs_examples_published/testM_alg_example_01.pdf}
\end{center}
\begin{par}
Plot histogram of calculated maximal value, i.e. probability density function simulated by Monte Carlo method.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
figure
hist(DO.max.r, 50)
title('histogram of Monte Carlo method results of maximum value (DO.max.r)')
\end{lstlisting}

\begin{center}
\includegraphics[width=0.7\textwidth]{algs_examples_published/testM_alg_example_02.pdf}
\end{center}

%%% \end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
% \usepackage{graphicx}
%     \DeclareGraphicsExtensions{.png, .jpeg}
% \usepackage{caption}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

\title{STAT 775: Machine Learning \\ HW 07}
\author{Terence Henriod}
\date{\today}

\begin{document}

\clearpage            % All of
\maketitle            % this,
\thispagestyle{empty} % removes the page number from the title page

\begin{abstract}
In this assignment, we use random forrests to classify hand-written digits that are typically hard to discern from one another for classifiers.
\end{abstract}

\newpage
\section{Exercise 01}
\subsection{Problem Statement}
Using the zip.data data set from the ESL website, reduce the dimensionality to $40$ with PCA, and perform classification using ``a collection of random trees." Perform binary classification on $7$s vs. $9$s, $3$s vs. $5$s, and $0$s vs. $8$s.

\subsection{Difficulty In Implementation}
I had a lot of difficulty implementing a good random tree classifier. The runtime was awful (~10 minutes to train $3$ trees), the trees were large (greater tree depth than number of training examples), and classification performance was poor (~70-80\%). I am not sure what went wrong, so for a sanity check I made use of the R package \texttt{randomForest}. It operates much faster and with much better classification accuracy. Both sets of results are shown below. Unfortunately, at this time, I may not be able to spend the time to make a fully effective implementation, and I figure that my time is better spent moving on to final projects. If using a library is insufficient and I should redo this for full credit, please let me know.
%
%
%
\subsection{randomForest Package Results}
%
\subsubsection{The Tables}
The confusion matrices for classification are shown below.\\
\newline
$7$s vs. $9$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   7 &   9 &\% Correct \\
  \hline
  \hline
  7                 & 140 &   7 & 95.2 \\
  \hline
  9                 &   3 & 174 & 98.3 \\
  \hline
  \hline
  Overall           &     &     & 96.9 \\
  \hline
\end{tabular}\\
\newline
\newline
$3$s vs. $5$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   3 &   5 &\% Correct \\
  \hline
  \hline
  3                 & 148 &  18 & 89.1 \\
  \hline
  5                 &  10 & 150 & 93.7 \\
  \hline
  \hline
  Overall           &     &     & 91.4 \\
  \hline
\end{tabular}\\
\newline
\newline
$0$s vs. $8$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   0 &   8 &\% Correct \\
  \hline
  \hline
  0                 & 356 &   3 & 99.1 \\
  \hline
  8                 &   9 & 157 & 94.6 \\
  \hline
  \hline
  Overall           &     &     & 97.7 \\
  \hline
\end{tabular}\\
\newline
%
\subsubsection{The Code}
The following R code was used to perform classification:
\begin{verbatim}
library('randomForest')

tests <- list(
  list(7, 9),
  list(3, 5),
  list(0, 8)
)

for (test in tests) {
  data <- get.data(
    train.file = ZIP.TRAIN.FILE.NAME,
    test.file = ZIP.TEST.FILE.NAME,
    class.1 = 0,
    class.2 = 8,
    num.principle.components = 50
  )

  rf <- randomForest(x = data$train[, -(1:2)], y = data$train[, 1])
  data$test$prediction <- round(predict(rf, data$test[, -(1:2)]))

  print(compute.confusion.matrix(
    predictions = data$test$prediction,
    targets = data$test$target,
    num.classes = 2
  )
}
\end{verbatim}
%
%
%
\subsection{My Results}
%
\subsubsection{The Tables}
The confusion matrices for classification are shown below.\\
\newline
$7$s vs. $9$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   7 &   9 &\% Correct \\
  \hline
  \hline
  7                 & 109 &  38 & 74.1 \\
  \hline
  9                 &  74 & 103 & 58.1 \\
  \hline
  \hline
  Overall           &     &     & 65.4 \\
  \hline
\end{tabular}\\
\newline
\newline
%
$3$s vs. $5$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   3 &   5 &\% Correct \\
  \hline
  \hline
  3                 & 115 &  51 & 69.3 \\
  \hline
  5                 &  87 &  73 & 45.6 \\
  \hline
  \hline
  Overall           &     &     & 57.7 \\
  \hline
\end{tabular}\\
\newline
\newline
%
$0$s vs. $8$s:\\
\newline
\begin{tabular}{| c || c | c || c |}
  \hline
  Actual/Prediction &   0 &   8 &\% Correct \\
  \hline
  \hline
  0                 & 336 &  23 & 93.6 \\
  \hline
  8                 &  72 &  94 & 56.6 \\
  \hline
  \hline
  Overall           &     &     & 81.9 \\
  \hline
\end{tabular}
\newline
%
\subsubsection{The Code}
The following R code was used to perform classification:
\begin{verbatim}
##############
# STAT775: Machine Learning
#
# HW07
# Exercise 01
#
# Using the zip.data data set from the ESL website, reduce the dimensionality
# to $40$ with PCA, and perform classification using ``a collection of random
# trees." Perform binary classification on 7s vs. 9s, 3s vs. 5s, and 0s vs. 8s.
###############

#
# Initial Setup
#
setwd("C:/Users/Terence/Documents/GitHub/STAT775/HW07")

#
# Data Cleaning
#
DATA.PATH <- "../DataSets/zip.data/"
ZIP.TRAIN.FILE.NAME <- paste0(DATA.PATH, "zip.train")
ZIP.TEST.FILE.NAME <- paste0(DATA.PATH, "zip.test")

construct.classification.frame <- function(data, targets) {
  return(
    data.frame(
      'target' = targets,
      'prediction' = rep(0, length(targets)),
      data
    )
  )
}

get.data <- function(
  train.file, test.file, class.1, class.2, num.principle.components = 50) {
  # Args:
  #   train.file:
  #   test.file:
  #   class.1:
  #   class.2
  #   num.principle.components:
  #

  train <- read.table(train.file)
  train <- subset(train, V1 == class.1 | V1 == class.2)
  for (i in 1:nrow(train)) {
    train[i, 1] <- if (train[i, 1] == class.1) {1} else {2}
  }

  test <- read.table(test.file)
  test <- subset(test, V1 == class.1 | V1 == class.2)
  for (i in 1:nrow(test)) {
    test[i, 1] <- if (test[i, 1] == class.1) {1} else {2}
  }

  pca.model <- prcomp(train[, -1])

  train[, -1] <- predict(pca.model, train[, -1])
  train <- train[, 1:(num.principle.components + 1)]
  train <- construct.classification.frame(
    targets = train[, 1],
    data = train[, -1]
  )

  test[, -1] <- predict(pca.model, test[, -1])
  test <- test[, 1:(num.principle.components + 1)]
  test <- construct.classification.frame(
    targets = test[, 1],
    data = test[, -1]
  )

  return(list(
    train = train,
    test = test
  ))
}

#
# Data Presentation and Evaluation
#

compute.confusion.matrix <- function(predictions, targets, num.classes = 2) {
  confusion.matrix <- matrix(0, nrow = num.classes + 1, ncol = num.classes + 1)
  for (i in 1:length(predictions)) {
    confusion.matrix[targets[[i]], predictions[[i]]] <-
      confusion.matrix[targets[[i]], predictions[[i]]] + 1
  }
  confusion.matrix[num.classes + 1, num.classes + 1] <-
    sum(diag(confusion.matrix)) / sum(confusion.matrix)
  for (i in 1:num.classes) {
    confusion.matrix[i, num.classes + 1] <-
      confusion.matrix[i, i] / sum(confusion.matrix[i, 1:num.classes])
    confusion.matrix[num.classes + 1, i] <-
      confusion.matrix[i, i] / sum(confusion.matrix[1:num.classes, i])
  }
  return(confusion.matrix)
}

#
# Random Discriminant
#
require(ppls)

fit.gaussian <- function(x, n) {
  # Args:
  #   x: a list or 1-dimensional vector
  #   n: the total number of observations in the set the members of x come from

  gaussian.model <- list(
    mu = mean(x),
    sigma = var(x),
    prior = length(x) / n
  )

  if (gaussian.model$prior == 0) {
    gaussian.model$mu <- 1
    gaussian.model$sigma <- 1
  } else if (is.na(gaussian.model$sigma)) {
    gaussian.model$sigma <- 1  # arbitrary, hack for when fittint to 1 element
  }

  return(gaussian.model)
}

relative.gaussian.pdf <- function(model, x) {
  # Args:
  #   model:
  #   x:

  if (model$prior == 0) {
    return(0)
  }

  scaling.factor <- 1 / (sqrt(2 * pi) * model$sigma)
  exponent <- -(((x - model$mu) ^ 2) / (2 * model$sigma ^ 2))
  probability <- scaling.factor * exp(exponent)

if (any(is.na(probability))) {
  print(model)
  print(x)
}

  return(probability)
}

construct.random.discriminant <- function(classification.frame) {
  # Args:
  #   data: labeled data in rows

  dimensionality <- ncol(classification.frame) - 2

  projection.line <- normalize.vector(
    matrix(runif(n = dimensionality, min = -5, max = 5), ncol = 1)
  )

  data.c1 <- data.matrix(subset(classification.frame, target == 1)[, -(1:2)])
  data.c2 <- data.matrix(subset(classification.frame, target == 2)[, -(1:2)])

  projections.c1 <- data.c1 %*% projection.line
  projections.c2 <- data.c2 %*% projection.line

  model <- list(
    projection.line = projection.line,
    decision.boundary = NULL,
    class.1.model = NULL,
    class.2.model = NULL
  )

  if (nrow(data.c1) <= 1 || nrow(data.c1) <= 1) {
    model$decision.boundary <-
      0.5 * (mean(projections.c1) + mean(projections.c2))
  }

  model$class.1.model <- fit.gaussian(
    x = projections.c1,
    n = nrow(classification.frame)
  )
  model$class.2.model <- fit.gaussian(
    x = projections.c2,
    n = nrow(classification.frame)
  )

  return(model)
}

discriminant.predict <- function(model, c.frame) {
  # Args:
  #   model: an object that represents an LDA model
  #   x: the data to be classified via LDA

  if (nrow(c.frame) > 0) {
    if (is.null(model$decision.boundary)) {
      predictions <- list()
      for (i in 1:nrow(c.frame)) {
        projection <- data.matrix(c.frame[i, -(1:2)]) %*% model$projection.line
        p.class.1 <- relative.gaussian.pdf(
          model = model$class.1.model,
          x = projection
        )
        p.class.2 <- relative.gaussian.pdf(
          model = model$class.2.model,
          x = projection
        )

        predictions[[i]] <- if (p.class.1 >= p.class.2) {1} else {2}
      }
      c.frame$prediction <- predictions
    } else {
      for(i in 1:nrow(c.frame)) {
        projection <- data.matrix(c.frame[i, -(1:2)]) %*% model$projection.line
        if (projection <= model$decision.boundary) {
          c.frame$prediction[[i]] <- 1
        } else {
          c.frame$prediction[[i]] <- 2
        }
      }
    }
  }

  return(c.frame)
}

#
# Tree
#
compute.entropy <- function(c.frame) {
  # Args:
  #   c.frame:

  n <- nrow(c.frame)

  if (n == 0) {
    return(0)
  }

  p.1 <- (nrow(subset(c.frame, target == 1))) / n
  p.2 <- 1 - p.1

  if (p.1 == 0 || p.2 == 0) {
    return(0)
  }

  entropy <- -((p.1 * log2(p.1)) + (p.2 * log2(p.2)))
  return(entropy)
}

construct.random.tree <- function(c.frame, entropy.threshold = 0.1) {
  # Args:
  #   data: a frame of labeled data
  #   stopping.criterion: a function that will determine if the tree should
  #                       continue building. Could be entropy, gini index, etc.

  entropy <- compute.entropy(c.frame)

  if (nrow(c.frame) <= 1 || entropy <= entropy.threshold){
    return(NULL)
  }

  discriminant.model <- construct.random.discriminant(c.frame)

  c.frame <- discriminant.predict(
    model = discriminant.model,
    c.frame = c.frame
  )

  predicted.as.1 <- subset(c.frame, prediction == 1)
  predicted.as.1$prediction <- rep(0, nrow(predicted.as.1))
  class.1.branch <- construct.random.tree(c.frame = predicted.as.1)

  predicted.as.2 <- subset(c.frame, prediction == 2)
  predicted.as.2$prediction <- rep(0, nrow(predicted.as.2))
  class.2.branch <- construct.random.tree(c.frame = predicted.as.2)

  tree <- list(
    model = discriminant.model,
    entropy = entropy,
    class.1.branch = class.1.branch,
    class.2.branch = class.2.branch
  )

  return(tree)
}

tree.predict <- function(tree, c.frame) {
  if (!is.null(tree)) {
    c.frame <- discriminant.predict(tree$model, c.frame)

    predicted.as.1 <- subset(c.frame, prediction == 1)
    predicted.as.2 <- subset(c.frame, prediction == 2)

    c.frame <- rbind(
      tree.predict(tree$class.1.branch, predicted.as.1),
      tree.predict(tree$class.2.branch, predicted.as.2)
    )
  }
  return(c.frame)
}

#
# Main
#
library('randomForest')

tests <- list(
  list(7, 9),
  list(3, 5),
  list(0, 8)
)

for (test in tests) {
  data <- get.data(
    train.file = ZIP.TRAIN.FILE.NAME,
    test.file = ZIP.TEST.FILE.NAME,
    class.1 = 0,
    class.2 = 8,
    num.principle.components = 50
  )

  rf <- randomForest(x = data$train[, -(1:2)], y = data$train[, 1])
  data$test$prediction <- round(predict(rf, data$test[, -(1:2)]))

  print(compute.confusion.matrix(
    predictions = data$test$prediction,
    targets = data$test$target,
    num.classes = 2
  ))
}

run.trial <- function(
  class.1, class.2, num.features = 50, num.trees = 5, entropy.threshold = 0.1) {

  print('Fetching Data...')
  data <- get.data(
    train.file = ZIP.TRAIN.FILE.NAME,
    test.file = ZIP.TEST.FILE.NAME,
    class.1 = class.1,
    class.2 = class.2,
    num.principle.components = num.features
  )

  print('Constructing Forrest...')
  forrest <- list()
  for (i in 1:num.trees) {
    forrest[[i]] <- construct.random.tree(
      c.frame = data$train,
      entropy.threshold = entropy.threshold
    )
  }

  print('Making Predictions...')
  predictions <- list()
  for (i in 1:num.trees) {
    predictions[[i]] <- tree.predict(forrest[[i]], data$test)[, 1:2]
  }

  print('Tabulating Votes...')
  votes.1 <- rep(0, nrow(predictions[[1]]))
  votes.2 <- rep(0, nrow(predictions[[1]]))
  for (j in 1:nrow(predictions[[1]])) {
    for (i in 1:num.trees) {
      if (predictions[[i]]$prediction[[j]] == 1) {
        votes.1[[j]] <- votes.1[[j]] + 1
      } else {
        votes.2[[j]] <- votes.2[[j]] + 1
      }
    }
  }

  consensus <- list()
  for (j in 1:length(votes.1)) {
    if (votes.1[[j]] >= votes.2[[j]]) {
      consensus[[j]] <- 1
    } else {
      consensus[[j]] <- 2
    }
  }

  print(
    compute.confusion.matrix(
      predictions = consensus,
      targets = predictions[[1]]$target)
  )
}

for (test in tests) {
  run.trial(
    class.1 = test[[1]],
    class.2 = test[[2]],
    num.features = 50,
    entropy.threshold = 0.001
  )
}

\end{verbatim}
%
\end{document}


\chapter{Object Language}
\label{sec:source}

This chapter introduces λST, the calculus that will be the object of study for
the remainder of this thesis.

\section{Overview}
\label{sec:source:overview}

Before we dive into the technicalities of λST, I give an overview of the
calculus by example. Those who prefer their exposition top-down may skip to
\secref{source:sizes} and return later.

λST features two base types: sized natural numbers and sized streams of natural
numbers. Our first example is the function \icode{half} which divides a natural
number by two (rounding down if the input is odd). In non-sized Agda, this
function would be written as follows:
\begin{code}
  half : ℕ → ℕ
  half zero = zero
  half (suc zero) = zero
  half (suc (suc n)) = suc (half n)
\end{code}
The λST version is broadly similar, only with added size information and more
cumbersome syntax:
\begin{verbatim}
  half : ∀ n < ↑ ∞. Nat n → Nat n
  half ≔ fix[Nat ∙ → Nat ∙]
           (Λ n < ↑ ∞. λ rec ∶ (∀ m < n. Nat m → Nat m). λ i : Nat n.
             case i of
               (zero n)  → zero n
               (suc m j) →
                 case j of
                   (zero m)  → zero n
                   (suc o k) → suc n o (rec o k)
\end{verbatim}

The definition of \icode{half} already showcases most features of λST. The type
of natural numbers of size \icode{n} is called \icode{Nat~n}. If we think of
sizes as ordinals, \icode{Nat~n} is the type of naturals less than or equal to
\icode{n}. This intuition justifies the types of its constructors:
\begin{verbatim}
  zero : ∀ n < ↑ ∞. Nat n
  suc  : ∀ n < ↑ ∞. ∀ m < n. Nat m → Nat n
\end{verbatim}
Zero is less than or equal to any \icode{n}, so it is always in \icode{Nat~n}.
The successor of a natural number \icode{k} of size \icode{m} is less than or
equal to \icode{n} if \icode{m~<~n}. If we were to define sized natural numbers
in Agda, we would get constructors with very similar types. To eliminate sized
natural numbers, we use Haskell-style pattern matching. (The formal definition
of λST in \chapref{source} uses an eliminator instead.) The meaning of
\icode{↑~∞} is explained below.

λST's size grammar is similar to that of Agda, but for now we only need size
variables. These are introduced at the type level by a size quantification of
the form \icode{∀~x~<~m}, where \icode{x} is a size variable and \icode{m}, a
size, is the upper bound of \icode{x} (so \icode{x} can only be instantiated
with sizes below \icode{m}). At the term level, size variables are introduced by
binders of the form \icode{Λ~x~<~m} and terms with a size-quantified type can be
applied to sizes below the declared bound. All this makes size quantification
resemble the type quantification of System F: the bound of a size variable
corresponds to the kind of a type variable, size binders correspond to type
binders and size application corresponds to type application.

As mentioned in \secref{background:sized}, λST departs from Agda in its handling
of the \enquote*{infinite} size \icode{∞}. In Agda, \icode{∞} is less than
itself, so the type \icode{Size<~∞} contains \icode{∞}. This is convenient, but
also a likely culprit of size-related inconsistencies. In λST, we therefore omit
the rule \icode{∞~<~∞}. Where we want to quantify over all sizes up to and
including \icode{∞}, as in the type of \icode{half}, we use the successor of
\icode{∞}, \icode{↑~∞}, as a bound.

The last ingredient we need to define \icode{half} is the centrepiece of λST:
the size-based fixpoint operator \icode{fix} of type
\begin{verbatim}
  fix[T ∙] : (∀ n < ↑ ∞. (∀ m < n. T m) → T n) → ∀ n < ↑ ∞. T n
\end{verbatim}
Here, \icode{T} is an arbitrary type with one free size variable denoted by the
\enquote{hole} \icode{∙}. The type of \icode{fix} corresponds to induction over
sizes: if from an element of \icode{T} with size \icode{m} we can construct an
element of \icode{T} with a greater size \icode{n}, then we can construct an
element of \icode{T} with any size.

Besides natural numbers, the prototypical inductive type, λST also features the
prototypical coinductive type: sized streams (of natural numbers). They have one
constructor, \icode{cons}, which prepends an element to a stream, and two
destructors, \icode{head} and \icode{tail}, which respectively extract the first
element and the rest of a stream. These have the following types, again
analogous to those one would obtain in Agda:
\begin{verbatim}
  cons : ∀ n < ↑ ∞. Nat ∞ → (∀ m < n. Stream m) → Stream n
  head : ∀ n < ↑ ∞. Stream n → Nat ∞
  tail : ∀ n < ↑ ∞. Stream n → ∀ m < n. Stream m
\end{verbatim}
Using these primitives, we can define, for example, the stream of zeros:
\begin{verbatim}
  zeros : ∀ n < ↑ ∞. Stream n
  zeros ≔ fix[Stream ∙]
            (Λ n < ↑ ∞. λ rec : (∀ m < n. Stream m).
              cons (zero ∞) n rec)
\end{verbatim}
More interestingly, λST also admits the standard mapping function for streams:
\begin{verbatim}
  map : (Nat ∞ → Nat ∞) → ∀ n < ↑ ∞. Stream n → Stream n
  map ≔ λ f : Nat ∞ → Nat ∞. fix[Stream ∙ → Stream ∙]
          (Λ n < ↑ ∞. λ rec : (Λ m < n. Stream m → Stream m). λ xs : Stream n.
            cons (f (head n xs)) n (Λ m < n. rec m (tail n xs m)))
\end{verbatim}
Note that according to its type, \icode{map} is size-preserving: if we apply it
to a stream with at least \icode{n} defined elements, its result also has at
least \icode{n} defined elements. The same applies to \icode{half}, whose type
indicates that its output is no greater than its input. As we saw with the rose
tree example in \secref{background:sized}, such preservation properties are very
useful.

\section{Sizes}
\label{sec:source:sizes}

In the following sections we define λST. This section corresponds to the Agda
module \module{Source.\allowbreak Size}{Source.Size} in the formalisation
\cite{limperg2019code}.\footnote{In electronic versions of this document, module
  names such as \module{Source.\allowbreak Size}{Source.Size} link to a HTML
  rendering of the respective module.}

\Def{Sizes} and \Def{size contexts} are defined by the grammar in
\figref{syntax:sizes}. Size variables are de Bruijn indices; we write $v_x$ for
the $x$th variable. (The examples in the previous sections instead used named
variables for convenience.)

\begin{figure}
  \begin{displaymath}
    \begin{array}{llcll}
      \text{size} & n, m, o & \bnfdef & v_x & \text{(size variable)} \\
      & & | & 0 & \text{(zero)} \\
      & & | & \ssuc{n} & \text{(successor)} \\
      & & | & ∞ & \text{(infinity)} \\

      \text{size context} & Δ, Ω & \bnfdef & () & \text{(empty context)} \\
      & & | & \ctx{Δ}{n} & \text{(context extension)}
    \end{array}
  \end{displaymath}

  \caption{Syntax of sizes, size variables and size contexts}
  \label{fig:syntax:sizes}
\end{figure}

Our size grammar is essentially that of Agda (although Agda does not have a
separate grammatical category for sizes). We only add a zero size, which could
easily be added to Agda as well.\footnote{We need the zero size because we omit
  Agda's \icode{∞~<~∞} rule. Without this rule, certain Agda constructions, such
  as a natural number successor that works on \icode{Nat~∞}, do not transfer
  directly to λST. Adding the zero size allows us to approximate these
  constructions in λST.} Contexts are telescopes of size bounds: in the context
$\ctx{Δ}{n}$, the zeroth variable $v_0$ ranges over sizes less than $n$ and $n$
may involve variables in $Δ$.

The typing judgments for sizes are mutually inductively generated by the rules
in \figref{typing:sizes}. The judgment $Δ ⊢_x v_x < n$ holds if $v_x$ is a valid
index into $Δ$ and $n$ is the bound of $v_x$. The other two judgments, $Δ ⊢ n$
and $⊢ Δ$, ensure that sizes and contexts are scope-safe. In the definition of
$Δ ⊢_x v_x < n$, we use a weakened size $\wk(n)$, which is $n$ with all
variables $v_x$ replaced by $v_{x+1}$.
\begin{figure}
  \begin{mathpar}
    \fbox{$Δ ⊢_x v_x < n$} \\

    \inferrule{Δ ⊢ n}{\ctx{Δ}{n} ⊢_x v_0 < n}

    \inferrule{Δ ⊢_x v_x < n \\ Δ ⊢ m}{\ctx{Δ}{m} ⊢_x v_{x+1} < \wk(n)} \\

    \fbox{$Δ ⊢ n$} \\

    \inferrule{Δ ⊢_x v_x < n}{Δ ⊢ v_x}

    \inferrule{⊢ Δ}{Δ ⊢ 0 \\ Δ ⊢ ∞}

    \inferrule{Δ ⊢ n}{Δ ⊢ \ssuc{n}} \\

    \fbox{$⊢ Δ$} \\

    \inferrule{ }{⊢ ()}

    \inferrule{Δ ⊢ n}{⊢ \ctx{Δ}{n}}

  \end{mathpar}

  \caption{Typing rules for sizes and size contexts}
  \label{fig:typing:sizes}
\end{figure}

For convenience, the judgments are defined such that $Δ ⊢_x v_x < n$ implies $Δ ⊢
n$ and $Δ ⊢ n$ implies $⊢ Δ$, as is easily proved by induction on the respective
derivation. All other judgments we define below are structured similarly, so for
example $Δ ⊢ T$ (scope-safety of the type $T$) implies $⊢ Δ$ and $Δ; Γ ⊢ t ∶ T$
(well-typedness of a term $t$ of type $T$ in context $Γ$ and size context $Δ$)
implies $Δ ⊢ T$. I will henceforth use these lemmas implicitly.

Next, we define a syntactic comparison relation $Δ ⊢ n < m$ between sizes in the
same context, which is inductively generated by the rules in
\figref{typing:order}. The rules of $<$ are all supported by the intuition that
sizes of the form $\ssuc{\dots \ssuc{0}}$ are natural numbers and sizes of the
form $\ssuc{\dots \ssuc{∞}}$ are ordinals $ω + i$. Indeed, this will be the
interpretation of sizes in the model. As mentioned, we do not admit Agda's
dubious rule $∞ < ∞$ (see \secref{background:sized}).

\begin{figure}
  \begin{mathpar}
    \fbox{$Δ ⊢ n < m$} \\

    \inferrule{Δ ⊢_x v_x < n}{Δ ⊢ v_x < n}

    \inferrule{Δ ⊢ n}{Δ ⊢ 0 < \ssuc{n}}

    \inferrule{ }{Δ ⊢ 0 < ∞}

    \inferrule{Δ ⊢ n < m}{Δ ⊢ \ssuc{n} < \ssuc{m}}

    \inferrule{Δ ⊢ n < ∞}{Δ ⊢ \ssuc{n} < ∞}

    \inferrule{Δ ⊢ n < m \\ Δ ⊢ m < o}{Δ ⊢ n < o}

    \inferrule{Δ ⊢ n}{Δ ⊢ n < \ssuc{n}}
  \end{mathpar}

  \caption{Size comparison}
  \label{fig:typing:order}
\end{figure}

The following technical lemma will be needed later:
\begin{lemma}[Weakening preserves typing and $<$]
  \label{lem:wk-preservation}
  If $Δ ⊢ n$ and $Δ ⊢ m$, then $\ctx{Δ}{m} ⊢ \wk(n)$. If $Δ ⊢ o$ and $Δ ⊢ n <
  m$, then $\ctx{Δ}{o} ⊢ \wk(n) < \wk(m)$.
\end{lemma}
I do not give a proof of this lemma, and indeed most of the following lemmas,
because both statements are proved by an almost trivial induction. Instead, the
reader may refer to the formalisation, which closely follows the structure of
this thesis and provides all the details one could ever wish for.

\section{Size Substitutions}
\label{sec:source:sub}

Having size variables, we must say how to substitute for them. λST uses implicit
substitutions, so substitutions are not part of the calculus' grammar. We do,
however, define a universe of simultaneous substitutions between given size
contexts -- this will be helpful when we get to the model, where substitutions
will correspond to morphisms between (interpretations of) size contexts. Size
substitutions and their properties are formalised in \module{Source.\allowbreak
  Size.\allowbreak Substitution.\allowbreak
  Canonical}{Source.Size.Substitution.Canonical}.

A \Def{size substitution} is a snoc-list of sizes. We write $()$ for the empty
substitution and $\ssub{σ}{n}$ for the extension of $σ$ with a size $n$. The
application of a substitution to a size, $\sub{n}{σ}$, is defined by recursion
on $n$:
\begin{Align*}
  \sub{v_0}{\ssub{σ}{n}} &≔& n \\
  \sub{v_{x + 1}}{\ssub{σ}{n}} &≔& \sub{v_x}{σ} \\
  \sub{0}{σ} &≔& 0 \\
  \sub{(\ssuc{n})}{σ} & ≔ & \ssuc{\sub{n}{σ}} \\
  \sub{∞}{σ} &≔& ∞. \\
\end{Align*}
\figref{typing:ssub} gives the typing rules for size substitutions. In the
previous definition, there is no equation for the case $\sub{v_x}{()}$ because
this is impossible if $n$ and $σ$ are well-typed.

\begin{figure}
  \begin{mathpar}
    \fbox{$σ ∶ Δ ⇒ Ω$} \\

    \inferrule{⊢ Δ}{() ∶ Δ ⇒ ()} \\

    \inferrule{σ ∶ Δ ⇒ Ω \\ Δ ⊢ n \\ Ω ⊢ m \\ Δ ⊢ n < \sub{m}{σ}}{\ssub{σ}{n} ∶ Δ ⇒ \ctx{Ω}{m}}
  \end{mathpar}

  \caption{Typing rules for size substitutions}
  \label{fig:typing:ssub}
\end{figure}

The typing rules ensure that if we have $σ ∶ Δ ⇒ Ω$, then $σ$ maps every variable
$v_x$ in $Ω$ to a size $n$ such that $n$ contains only variables in $Δ$ and $n$
respects the bound declared for $v_x$ in $Ω$. Due to these properties,
well-typed substitutions are well-behaved with regards to typing and size
comparison:

\begin{lemma}[Size substitutions preserve typing and $<$]
  \label{lem:sub-preservation}
  If $σ ∶ Δ ⇒ Ω$ and $Ω ⊢ n$, then $Δ ⊢ \sub{n}{σ}$. If $σ ∶ Δ ⇒ Ω$ and $Ω ⊢ n <
  m$, then $Δ ⊢ \sub{n}{σ} < \sub{m}{σ}$.
\end{lemma}

\begin{proof}
  By induction on the derivation of $Ω ⊢ n$. In the proof of $Δ ⊢ \sub{n}{σ} <
  \sub{m}{σ}$ we use the lemma $\sub{\wk(m)}{\ssub{σ}{n}} = \sub{m}{σ}$, which
  is proved by a simple induction.
\end{proof}

Note that we write $σ ∶ Δ ⇒ Ω$ even though substitution with $σ$ transforms an
object in $Ω$ into an object in $Δ$. This notation is inspired by the model,
where $Δ$ and $Ω$ will be particular types and $σ$ a function between them.

Having constructed a universe of substitutions, we now define some particular
substitutions in this universe which we will use later. We could also have
axiomatised these substitutions directly but that would have made the
substitution operation and any proofs about it more complex, and we would also
have had to axiomatise some equations between substitutions instead of proving
them as a lemma. Our design leads to some subtle problems in the formalisation;
see \secref{formalisation:sub} for details and a workaround.

We define the following well-typed substitutions (assuming well-typed sizes $Ω ⊢
n$ and $Δ ⊢ m$ as well as substitutions $σ ∶ Δ ⇒ Ω$, $τ ∶ Ω ⇒ Ω′$):
\begin{itemize}
  \item The \Def{weakening} of $σ$ is
    \begin{Align*}
      \multicolumn{3}{l}{\Weaken(σ) ∶ \ctx{Δ}{m} ⇒ Ω} \\
      \Weaken(()) &≔& () \\
      \Weaken(\ssub{σ}{n}) &≔& \ssub{\Weaken(σ)}{\wk(n)}.
    \end{Align*}
  \item The \Def{lifting} of $σ$ is
    \begin{gather*}
      \Lift(σ) ∶ \ctx{Δ}{n[σ]} ⇒ \ctx{Ω}{n} \\
      \Lift(σ) ≔ \ssub{\Weaken(σ)}{v_0}.
    \end{gather*}
   \item The \Def{identity substitution} $\Id_Δ$ is
     \begin{Align*}
       \multicolumn{3}{l}{\Id_Δ ∶ Δ ⇒ Δ} \\
       \Id_{()} &≔& () \\
       \Id_{\ctx{Δ}{n}} &≔& \Lift(\Id_Δ).
     \end{Align*}
   \item The \Def{forward composition} of $σ$ and $τ$ is
     \begin{Align*}
       \multicolumn{3}{l}{σ \fcomp τ ∶ Δ ⇒ Ω′} \\
       σ \fcomp () &≔& () \\
       σ \fcomp (\ssub{τ}{n}) &≔& \ssub{(σ \fcomp τ)}{\sub{n}{σ}}.
     \end{Align*}
   \item The \Def{weakening substitution} is
     \begin{gather*}
       \Wk ∶ \ctx{Δ}{m} ⇒ Δ \\
       \Wk ≔ \Weaken(\Id).
     \end{gather*}
   \item Assuming that $Δ ⊢ n < m$, the \Def{singleton substitution} of $n$ is
     \begin{gather*}
       \Sing(n) ∶ Δ ⇒ \ctx{Δ}{m} \\
       \Sing(n) ≔ \ssub{\Id_Δ}{n}.
     \end{gather*}
   \item The \Def{skipping substitution} is
     \begin{gather*}
       \Skip ∶ \ctx{\ctx{Δ}{m}}{v_0} ⇒ \ctx{Δ}{m} \\
       \Skip ≔ \ssub{\Weaken(\Wk)}{v_0}.
     \end{gather*}
\end{itemize}

The first six of these substitutions are standard. The skipping substitution is
perhaps easier to understand if we switch to explicit variables for a moment:
given a size $n$ in the context $Δ,\, x < m$, $\sub{n}{\Skip}$ lives in the
context $\ctx{\ctx{Δ}{x < m}}{y < x}$ and all occurrences of $x$ in $n$ are
replaced by $y$. In this sense, $\sub{n}{\Skip}$ \enquote*{skips} $x$.

\section{Types}
\label{sec:source:types}

We move on to the type language of λST. This section is formalised in
\module{Source.\allowbreak Type}{Source.Type}.

The types and contexts of λST are generated by the grammar in
\figref{syntax:types}. Their typing rules appear in \figref{typing:types}.

\begin{figure}
  \begin{displaymath}
    \begin{array}{llcll}
      \text{type} & T, U, V & \bnfdef & \Nat{n} & \text{(sized naturals)} \\
      & & | & \Stream{n} & \text{(sized streams)} \\
      & & | & T → U & \text{(function space)} \\
      & & | & \All{n}{T} & \text{(size quantification)} \\

      \text{context} & Γ, Ψ & \bnfdef & () & \text{(empty context)} \\
      & & | & \Ctx{Γ}{T} & \text{(context extension)}\\
    \end{array}
  \end{displaymath}

  \caption{Syntax of types and contexts}
  \label{fig:syntax:types}
\end{figure}

\begin{figure}
  \begin{mathpar}
    \fbox{$Δ ⊢ T$} \\

    \inferrule{Δ ⊢ n}{Δ ⊢ \Nat{n} \\ Δ ⊢ \Stream{n}}

    \inferrule{Δ ⊢ T \\ Δ ⊢ U}{Δ ⊢ T → U}

    \inferrule{\ctx{Δ}{n} ⊢ T}{Δ ⊢ \All{n}{T}}
    \\

    \fbox{$Δ ⊢ Γ$} \\

    \inferrule{⊢ Δ}{Δ ⊢ ()}

    \inferrule{Δ ⊢ Γ \\ Δ ⊢ T}{Δ ⊢ \ctx{Γ}{T}}
  \end{mathpar}

  \caption{Typing rules for types and contexts}
  \label{fig:typing:types}
\end{figure}

λST features two base types: $\Nat{n}$, the type of sized natural numbers, and
$\Stream{n}$, the type of sized streams. We will discuss these in more detail
when we get to the terms of λST. As for composite types, λST has a non-dependent
function space $T → U$ and size quantification $\All{n}{T}$. Note that $n$ is
not a variable, as the notation may suggest, but the bound of the zeroth de
Bruijn index in $T$. Size quantification works essentially like quantification
in System F if we think of the bound $n$ as the \enquote*{type} of a size
variable. Contexts are lists of types. The typing rules for types and contexts
ensure scope-safety relative to some size context.

Since types can contain size variables, we define \Def{size substitution in
  types}, written $\sub{T}{σ}$, by recursion over the type:
\begin{Align*}
  \sub{(\Nat{n})}{σ} &≔& \Nat{\sub{n}{σ}} \\
  \sub{(\Stream{n})}{σ} &≔& \Stream{\sub{n}{σ}} \\
  \sub{(T → U)}{σ} &≔& \sub{T}{σ} → \sub{U}{σ} \\
  \sub{(\All{n}{T})}{σ} &≔& \All{\sub{n}{σ}}{\sub{T}{\Lift(σ)}}.
\end{Align*}
Substitution in contexts $\sub{Γ}{σ}$ is the pointwise extension of substitution
in types.

The only interesting case of the above definition is that for size
quantification. Here we must recurse with $\Lift(σ)$ rather than $σ$ because
$T$ is a type in $\ctx{Δ}{n}$ while $\All{n}{T}$ is a type in $Δ$. As usual,
size substitution preserves scope safety: if $Ω ⊢ T$ and $σ ∶ Δ ⇒ Ω$, then $Δ ⊢
\sub{T}{σ}$ (and similar for contexts).

\section{Terms}
\label{sec:source:terms}

In this section, we define the terms of λST and their typing rules. The
corresponding module in the formalisation is \module{Source.\allowbreak
  Term}{Source.Term}.

The terms of λST are generated by the grammar in \figref{syntax:terms}. Term
variables, like size variables, are de Bruijn indices.

\begin{figure}
  \begin{displaymath}
    \begin{array}{llcll}
      \text{term} & t, u, v & \bnfdef & v_x & \text{(variable)} \\
      & & | & \lam{T}{t} & \text{(abstraction)} \\
      & & | & \app{t}{u} & \text{(application)} \\
      & & | & \slam{n}{t} & \text{(size abstraction)} \\
      & & | & \sapp{t}{n} & \text{(size application)} \\
      & & | & \zero{n} & \text{(zero)} \\
      & & | & \suc{n}{m}{t} & \text{(successor)} \\
      & & | & \case{T}{n}{t}{u}{v} & \text{(case analysis for $\Nat{n}$)} \\
      & & | & \cons{t}{n}{u} & \text{(stream construction)} \\
      & & | & \head{n}{t} & \text{(stream head)} \\
      & & | & \tail{n}{t}{m} & \text{(stream tail)} \\
      & & | & \fix{T}{t}{n} & \text{(sized fixpoint)}
    \end{array}
  \end{displaymath}

  \caption{Syntax of terms}
  \label{fig:syntax:terms}
\end{figure}

The first three term constructions are those of the simply-typed lambda
calculus: variables, abstraction and application. The next two are size
abstraction and size application, which respectively introduce and eliminate a
size quantification. They are analogous to System F's type abstraction and type
application.

The other terms are conceptually constants, though we introduce them as term
formers to simplify the model (and particularly the formalisation). If they were
constants, they would have the following types (with explicit variables for
convenience):
\begin{Align*}
  \mathrm{zero} &∶& \AllE{x}{\Star}{\Nat{x}} \\
  \mathrm{suc} &∶& \AllE{x}{\Star}{\AllE{y}{x}{\Nat{y} → \Nat{x}}} \\
  \mathrm{caseNat}[T] &∶& \AllE{x}{\Star}{\Nat{x} → T → (\AllE{y}{x}{T}) → T} \\
  \mathrm{cons} &∶& \Nat{∞} → \AllE{x}{\Star}{(\AllE{y}{x}{\Stream{y}}) → \Stream{x}} \\
  \mathrm{head} &:& \AllE{x}{\Star}{\Stream{x} → \Nat{∞}} \\
  \mathrm{tail} &:& \AllE{x}{\Star}{\Stream{x} → \AllE{y}{x}{\Stream{y}}} \\
  \mathrm{fix}[T(∙)] &:& (\AllE{x}{\Star}{(\AllE{y}{x}{T(y)}) → T(x)}) → \AllE{x}{\Star}{T(x)}.
\end{Align*}
The terms $\mathrm{zero}$ and $\mathrm{suc}$ serve as constructors of $\Nat{n}$.
The type $\Nat{n}$ may be thought of as the set of natural numbers less than or
equal to $n$. 0 is therefore a member of $\Nat{n}$ for any $n$, as the type of
$\mathrm{zero}$ indicates. Similarly, if we have a natural number $≤ n$, its
successor is $≤ m$ for any $m > n$, which justifies the type of $\mathrm{suc}$.
The eliminator for $\Nat{n}$ into some arbitrary type $T$ is
$\mathrm{caseNat}[T]$. It performs case analysis on its first non-size argument:
in the zero case, it returns its second argument; in the successor case, it
returns the result of applying the function given as the third argument to the
predecessor of the first argument. That predecessor, of course, has some smaller
size $m < n$.

For $\Stream{n}$, the size $n$ has the opposite meaning: where $\Nat{n}$
contains naturals up to \emph{at most} $n$, $\Stream{n}$ is roughly speaking the
type of lists of naturals with \emph{at least} $n$ elements. This is because
$\Nat{n}$ is an inductive type while $\Stream{n}$ is coinductive. More
precisely, a value of $\Stream{n}$ is a function which maps any natural number
$m ≤ n$ to a natural number. $\Stream{∞}$ is then the type of functions $ℕ → ℕ$,
which is isomorphic to the coinductively defined type of infinite streams of
naturals.

The constructor, $\mathrm{cons}$, and the destructors, $\mathrm{head}$ and
$\mathrm{tail}$, reflect this understanding. The first destructor
$\mathrm{head}$ extracts the element at index 0 from a $\Stream{n}$ (which
always exists since $0 ≤ n$). The second destructor $\mathrm{tail}$, when given
a $\Stream{n}$, returns its tail, which has one less element and is therefore an
element of $\Stream{m}$ for any $m < n$. The constructor $\mathrm{cons}$ takes
a function which generates a $\Stream{m}$ for any $m < n$ and prepends an
element, returning a bigger $\Stream{n}$.\footnote{Another plausible type for
  $\mathrm{cons}$ would be $\Nat{∞} → \AllE{x}{\Star}{\AllE{y}{x}{\Stream{y} →
      \Stream{x}}}$, where the stream argument is a single stream instead of a
  family. I choose not to use this simpler type because if we define sized
  streams in Agda, $\mathrm{cons}$ has the type given above.}

The final term, $\mathrm{fix}[T]$, is a size-based fixpoint combinator that
allows us both to recurse over sized natural numbers and to build sized streams
corecursively. It is parameterised by a type $T$ with one free size variable or
\enquote*{hole}, as the notation $T(∙)$ suggests. The type of $\mathrm{fix}[T]$
is essentially the principle of induction over sizes: if from an object of size
$m$ we can construct an object of size $n > m$, then we can construct an object
of any size.

The typing rules of λST terms, which appear in \figref{typing:terms}, follow
directly from the previous discussion, only with constants replaced by
equivalent term formers and explicit variables replaced by de Bruijn indices and
size substitutions.

\begin{figure}
  \begin{mathpar}
    \fbox{$Δ; Γ ⊢ t ∶ T$} \\

    \inferrule{Δ ⊢ Γ \\ Δ ⊢ T}{Δ; \Ctx{Γ}{T} ⊢ v_0 ∶ T}

    \inferrule{Δ; Γ ⊢ v_x ∶ T \\ Δ ⊢ U}{Δ; \Ctx{Γ}{U} ⊢ v_{x + 1} ∶ T}

    \inferrule{Δ; \Ctx{Γ}{T} ⊢ t ∶ U}{Δ; Γ ⊢ \lam{T}{t} ∶ T → U}

    \inferrule{Δ; Γ ⊢ t ∶ T → U \\ Δ; Γ ⊢ u ∶ T}{Δ; Γ ⊢ \app{t}{u} ∶ U}

    \inferrule{\ctx{Δ}{n}; \sub{Γ}{\Wk} ⊢ t ∶ T \\ Δ ⊢ Γ}
      {Δ; Γ ⊢ \slam{n}{t} ∶ \All{n}{T}}

    \inferrule{Δ; Γ ⊢ t ∶ \All{n}{T} \\ Δ ⊢ m < n}
      {Δ; Γ ⊢ \sapp{t}{m} ∶ \sub{T}{\Sing(m)}}

    \inferrule{Δ ⊢ n < \Star \\ Δ ⊢ Γ}{Δ; Γ ⊢ \zero{n} ∶ \Nat{n}}

    \inferrule{Δ ⊢ n < \Star \\ Δ ⊢ m < n \\ Δ; Γ ⊢ i ∶ \Nat{m}}
      {Δ; Γ ⊢ \suc{n}{m}{i} ∶ \Nat{n}}

    \inferrule{Δ ⊢ T \\ Δ ⊢ n < \Star \\ Δ; Γ ⊢ i ∶ \Nat{n} \\ Δ; Γ ⊢ z ∶ T \\
      Δ; Γ ⊢ s ∶ \All{n}{\Nat{v_0} → \sub{T}{\Wk}}}
      {Δ; Γ ⊢ \case{T}{n}{i}{z}{s} ∶ T}

    \inferrule{Δ ⊢ n < \Star \\ Δ; Γ ⊢ i ∶ \Nat{∞} \\
      Δ; Γ ⊢ \V{is} ∶ \All{n}{\Stream{v_0}}}
      {Δ; Γ ⊢ \cons{n}{i}{\V{is}} ∶ \Stream{n}}

    \inferrule{Δ ⊢ n < \Star \\ Δ; Γ ⊢ \V{is} ∶ \Stream{n}}
      {Δ; Γ ⊢ \head{n}{\V{is}} ∶ \Nat{∞}}

    \inferrule{Δ ⊢ n < \Star \\ Δ; Γ ⊢ \V{is} ∶ \Stream{n} \\ Δ ⊢ m < n}
      {Δ; Γ ⊢ \tail{n}{\V{is}}{m} ∶ \Stream{m}}

    \inferrule{\ctx{Δ}{\Star} ⊢ T \\
      Δ; Γ ⊢ t ∶ \All{\Star}{(\All{v_0}{\sub{T}{\Skip}}) → T} \\ Δ ⊢ n < \Star}
      {Δ; Γ ⊢ \fix{T}{t}{n} ∶ \sub{T}{\Sing(n)}}
  \end{mathpar}

  \caption{Typing rules for terms}
  \label{fig:typing:terms}
\end{figure}


\section{Algorithmic considerations}


\section{\acrlong{twm}}%
\label{sec:twm}

The \gls{twm} is an optimization problem that has many real life applications.
As we have these last few years, giant forest fire are more and more vivid, frequent and dangerous.
This problem modelize such a fire and the teams of firefighters that must extinguished it.
We use the modelization presented by \citeauthor{comp_mcts_mo}\cite{comp_mcts_mo}.
As they say themselves, this problem is of practical use and is challenging but enough simple to test the \gls{snrpa}.

We will first present the modelization of the \gls{twm} and we will proceed to the codification of the actions we used for the \gls{snrpa}.

\subimport{./subs/}{definition.tex}
\subimport{./subs/}{code.tex}


\subsubsection{Influence of Threshold Determination} \label{influence_of_threshold_determination}
115 different simulations were tested that use $\eta$DTW with a Sakoe-Chiba band of size 36\% depending on the
input time series length and \textit{Mid} as window size determination. Three different official approaches were
tested for the threshold determination, \textit{HAveD}, \textit{HMidD} and \textit{HMinD}. Furthermore two unofficial
approaches to double check the performance of the official approaches, \textit{Peak} with a factor of 1.1 and 1.2. Every
approach has respectively 23 different simulations. Figure \ref{fig:threshold_result} illustrates the distribution of
the five subsets. The \textit{HAveD} and the \textit{HMidD} approaches reach the best $F_{1}score_{\mu}$ values,
\textit{HAveD} a bit better as \textit{HMidD}. Surprising is the poor performance of both \textit{Peak} approaches,
their low $Precision_{\mu}$ values are the reason for this. Remarkable are the top $Precision_{\mu}$ values of the
\textit{HMinD} threshold determination.

\begin{figure}[H]
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south west,
                            xmin=0.4,
                            xmax=1,
                            ymin=0.1,
                            ymax=0.7,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth,
                            xlabel=$Precision_{\mu}$,
                            ylabel=$Recall_{\mu}$,
                            samples=100]
                            \addplot[blue, only marks, mark size=1] table {../data/fig/threshold_result/haved.dat};
                            \addlegendentry{HAveD}
                            \addplot[red, only marks, mark size=1] table {../data/fig/threshold_result/hmidd.dat};
                            \addlegendentry{HMidD}
                            \addplot[green, only marks, mark size=1] table {../data/fig/threshold_result/hmind.dat};
                            \addlegendentry{HMinD}
                            \addplot[violet, only marks, mark size=1] table {../data/fig/threshold_result/peak110.dat};
                            \addlegendentry{Peak (1.1)}
                            \addplot[cyan, only marks, mark size=1] table {../data/fig/threshold_result/peak120.dat};
                            \addlegendentry{Peak (1.2)}
                            \addplot[gray, domain=0.11:1] {(0.2 * x) / (2 * x - 0.2)};
                            \addplot[gray, domain=0.16:1] {(0.3 * x) / (2 * x - 0.3)};
                            \addplot[gray, domain=0.21:1] {(0.4 * x) / (2 * x - 0.4)};
                            \addplot[gray, domain=0.26:1] {(0.5 * x) / (2 * x - 0.5)};
                            \addplot[gray, domain=0.31:1] {(0.6 * x) / (2 * x - 0.6)};
                            \addplot[gray, domain=0.36:1] {(0.7 * x) / (2 * x - 0.7)};
                            \addplot[gray, domain=0.41:1] {(0.8 * x) / (2 * x - 0.8)};
                        \end{axis}
                    \end{tikzpicture}
                } &
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            xmin=0,
                            xmax=1,
                            ymin=0,
                            ymax=1,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth,
                            xlabel=$Precision_{\mu}$,
                            ylabel=$Recall_{\mu}$,
                            samples=100]
                            \addplot[blue, only marks, mark size=1] table {../data/fig/threshold_result/haved.dat};
                            \addplot[red, only marks, mark size=1] table {../data/fig/threshold_result/hmidd.dat};
                            \addplot[green, only marks, mark size=1] table {../data/fig/threshold_result/hmind.dat};
                            \addplot[violet, only marks, mark size=1] table {../data/fig/threshold_result/peak110.dat};
                            \addplot[cyan, only marks, mark size=1] table {../data/fig/threshold_result/peak120.dat};
                            \addplot[gray, domain=0.051:1] {(0.1 * x) / (2 * x - 0.1)};
                            \addplot[gray, domain=0.11:1] {(0.2 * x) / (2 * x - 0.2)};
                            \addplot[gray, domain=0.16:1] {(0.3 * x) / (2 * x - 0.3)};
                            \addplot[gray, domain=0.21:1] {(0.4 * x) / (2 * x - 0.4)};
                            \addplot[gray, domain=0.26:1] {(0.5 * x) / (2 * x - 0.5)};
                            \addplot[gray, domain=0.31:1] {(0.6 * x) / (2 * x - 0.6)};
                            \addplot[gray, domain=0.36:1] {(0.7 * x) / (2 * x - 0.7)};
                            \addplot[gray, domain=0.41:1] {(0.8 * x) / (2 * x - 0.8)};
                            \addplot[gray, domain=0.46:1] {(0.9 * x) / (2 * x - 0.9)};
                        \end{axis}
                    \end{tikzpicture}
                }
            \end{tabular}
        }
    \end{center}
    \caption{$Precision_{\mu}$ and $Recall_{\mu}$ of all simulations that use $\eta$DTW with a Sakoe-Chiba band of size
    36\% depending on the input time series length and \textit{Mid} as window size determination, separated by the used
    threshold determination. The left plot is a zoomed version of the right plot. Gray lines are illustrating the
    distribution of $F_{1}score_{\mu}$ in $\frac{1}{10}$ steps.}
    \label{fig:threshold_result}
\end{figure}


\section{Module 86: Register Spilling}
\begin{flushright}
\textit{(scribed by Jai Javeria)}
\end{flushright}
What do we do when we do not get a valid k colouring for a graph. In such a case we need to save some registers in the memory and update our code accordingly.\\
\textbf{Note:} Graph colouring is a NP hard problem and we would generally use some heuristic to find a colouring.  It might be the case that there exist a k colouring for the graph but our heuristic is not able to find it. In this case also we would spill registers to the memory.\\
\subsection{Register Allocation with Spilling}
\begin{itemize}
    \item Try to colour the RIG with k colours (k=number of machine registers).
    \item If this is not possible, pick up a node as a candidate for spilling.
    \item This register will now live in memory. Remove the picked node and all of its incident edges from the RIG.
    \item Repeat until the graph becomes k colourable.
\end{itemize}
The idea behind this algorithm is that when we remove one node from the RIG, it becomes sparser and hopefully it would become k colourable now. The algorithm is guaranteed to work because in the worst case the RIG would become k colurable when it has only k nodes left.
\subsection{Which node to pick}
\begin{itemize}
    \item The main idea to pick the node is to make the graph sparser and thus k-colourable. So one could pick the node with the maximum edges. This would make the RIG most sparser one at a time and we can get our solution faster.
    \item But it could happen that the node we picked up represents a register value that is used extensively in the code. This variable should not be sent to live in the memory, otherwise the runtime memory accesses would increase. Thus a better alternative is to select a register which has the minimum static live range i.e. all the program points where it it live.
    \item But a variable used just in a loop would have a less static live range but if the loop runs a million times during runtime, it would be not good to store it in memory. Thus we could select registers to spill based on 'cold' regions, the regions which are not going to be executed many time during runtime, e.g. the initialization part of the code.
    \item But just looking at the code statically may not give an accurate picture of which regions are cold and hot. Thus often a combination of 2nd and the 3rd strategy is used for selecting nodes. A JIT (Just In Time) compiler has an advantage of determining cold and hot regions more accurately.
\end{itemize}
\subsection{Optimistic Colouring}
\begin{itemize}
    \item Pick a node to spill, say n, and remove it and its edges from the RIG.
    \item Colour the remaining graph with k colours ( by potentially spilling more nodes).
    \item After we have got a colouring, try to put n back into the graph such that we can find a colour for n too.
\end{itemize}
\subsection{Optimistic Colouring Example}
Lets take the RIG example from the previous module
\begin{center}
    \includegraphics[scale=0.30]{images/85_RIGeg2.png}
\end{center}
And lets say we only have 3 registers for allocation and thus we need to find a 3 colouring. The current graph is not 3 colourable and thus we pick a node for spilling, say a.
\begin{center}
    \includegraphics[scale=0.30]{images/86_remove_a.png}
\end{center}
We see that again the graph cannot be coloured in 3 colours and thus we select another node for spilling, say f. Now we can colour the graph with 3 colours.
\begin{center}
    \includegraphics[scale=0.30]{images/86_color_RIG.png}
\end{center}
Now we try to add f back to the graph, but we cannot find a colour for f.
\begin{center}
    \includegraphics[scale=0.30]{images/86_add_f.png}
\end{center}
We move to adding a to the RIG and we are successful in colouring it.
\begin{center}
    \includegraphics[scale=0.30]{images/86_add_a.png}
\end{center}
Thus finally we need to spill f to the memory.
\subsection{How to Spill}
\begin{itemize}
    \item Allocate a memory location for f on the stack frame. Let its address be $f_a$
    \item Before each operation that reads f, insert
        \begin{center}
            f = load $f_a$
        \end{center}
    \item After each write operation on f, insert
        \begin{center}
            store f, $f_a$
        \end{center}
    \item Also rename every use of f to f1,f2... . This would be beneficial for some further analysis that needs to be done. Since each different instance of f would be getting different values, it is possible to rename them to different variables.
\end{itemize}
\subsection{Effect of Spilling on RIG}
Since we have broken down f into different variables with short live ranges, the graph becomes sparser and easier to colour.


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

      \subsection{gp2.m}

\begin{par}
\textbf{Summary:} Compute joint predictions and derivatives for multiple GPs with uncertain inputs. Does not consider the uncertainty about the underlying function (in prediction), hence, only the GP mean function is considered. Therefore, this representation is equivalent to a regularized RBF network. If gpmodel.nigp exists, individial noise contributions are added.
\end{par} \vspace{1em}
\begin{verbatim}function [M, S, V] = gp2(gpmodel, m, s)\end{verbatim}
\begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}gpmodel    GP model struct
  hyp      log-hyper-parameters                                  [D+2 x  E ]
  inputs   training inputs                                       [ n  x  D ]
  targets  training targets                                      [ n  x  E ]
  nigp     (optional) individual noise variance terms            [ n  x  E ]
m          mean of the test distribution                         [ D  x  1 ]
s          covariance matrix of the test distribution            [ D  x  D ]\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}M          mean of pred. distribution                            [ E  x  1 ]
S          covariance of the pred. distribution                  [ E  x  E ]
V          inv(s) times covariance between input and output      [ D  x  E ]\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2013-03-05
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item If necessary, re-compute cached variables
   \item Compute predicted mean and inv(s) times input-output covariance
   \item Compute predictive covariance matrix, non-central moments
   \item Centralize moments
\end{enumerate}

\begin{lstlisting}
function [M, S, V] = gp2(gpmodel, m, s)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
persistent iK oldX oldIn oldOut beta oldn;
D = size(gpmodel.inputs,2);    % number of examples and dimension of inputs
[n, E] = size(gpmodel.targets);      % number of examples and number of outputs

input = gpmodel.inputs;  target = gpmodel.targets; X = gpmodel.hyp;

% 1) if necessary: re-compute cached variables
if numel(X) ~= numel(oldX) || isempty(iK) ||  n ~= oldn || ...
    sum(any(X ~= oldX)) || sum(any(oldIn ~= input)) || ...
    sum(any(oldOut ~= target))
  oldX = X; oldIn = input; oldOut = target; oldn = n;
  K = zeros(n,n,E); iK = K; beta = zeros(n,E);

  for i=1:E                                              % compute K and inv(K)
    inp = bsxfun(@rdivide,gpmodel.inputs,exp(X(1:D,i)'));
    K(:,:,i) = exp(2*X(D+1,i)-maha(inp,inp)/2);
    if isfield(gpmodel,'nigp')
      L = chol(K(:,:,i) + exp(2*X(D+2,i))*eye(n) + diag(gpmodel.nigp(:,i)))';
    else
      L = chol(K(:,:,i) + exp(2*X(D+2,i))*eye(n))';
    end
    iK(:,:,i) = L'\(L\eye(n));
    beta(:,i) = L'\(L\gpmodel.targets(:,i));
  end
end

k = zeros(n,E); M = zeros(E,1); V = zeros(D,E); S = zeros(E);

inp = bsxfun(@minus,gpmodel.inputs,m');                    % centralize inputs

% 2) Compute predicted mean and inv(s) times input-output covariance
for i=1:E
  iL = diag(exp(-X(1:D,i))); % inverse length-scales
  in = inp*iL;
  B = iL*s*iL+eye(D);

  t = in/B;
  l = exp(-sum(in.*t,2)/2); lb = l.*beta(:,i);
  tL = t*iL;
  c = exp(2*X(D+1,i))/sqrt(det(B));

  M(i) = sum(lb)*c;                                            % predicted mean
  V(:,i) = tL'*lb*c;                   % inv(s) times input-output covariance
  k(:,i) = 2*X(D+1,i)-sum(in.*in,2)/2;
end

% 3) Compute predictive covariance, non-central moments
for i=1:E
  ii = bsxfun(@rdivide,inp,exp(2*X(1:D,i)'));

  for j=1:i
    R = s*diag(exp(-2*X(1:D,i))+exp(-2*X(1:D,j)))+eye(D);
    t = 1/sqrt(det(R));
    ij = bsxfun(@rdivide,inp,exp(2*X(1:D,j)'));
    L = exp(bsxfun(@plus,k(:,i),k(:,j)')+maha(ii,-ij,R\s/2));
    S(i,j) = t*beta(:,i)'*L*beta(:,j); S(j,i) = S(i,j);
  end

  S(i,i) = S(i,i) + 1e-6;          % add small jitter for numerical reasons

end

% 4) Centralize moments
S = S - M*M';
\end{lstlisting}


%% Maybe mention the difference between regression and classification problems
\glsreset{GAN}

\chapter{Machine Learning and Neural networks} \label{cha:neuralNets}

Machine learning is the discipline of computer science where the goal, instead of laying down the steps for a machine to produce a result given some inputs, is in fact to make the machine find by itself (``learn'') the correct course of action by showing it relevant data. A more rigorous definition is given by \textcite{machineLearning1997}.
\begin{citacao}
    A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at task T, as measured by P, improves with experience E. \cite[p. 2]{machineLearning1997}
\end{citacao}

In this definition the experience E can be viewed as the data that is used to teach the machine \textbf{--} this could be for example a dataset of images that the computer is expected to classify with the correct label (see \autoref{sub:supervised_learning}), or also repeated self play to become better at games like chess or Go as seen in reinforcement learning (see \autoref{sub:semi-supervised_and_RL}), where AlphaZero is a notable example \cite{alphaZero2017}.
The task T is what the machine is ultimately trying to achieve (e.g. classify images, play chess) and P is the measure of success in the task (e.g. percentage of accurately classified images, proportion of wins against an opponent in chess).

There are multiple approaches that fall unto the category of machine learning, popular techniques include \gls{KNN}, decision trees, random forest, \gls{SVM}, linear and logistic regression, and others\footnote{
    The paper from \textcite{fashionMNIST2017} gives many performance results for different methods applied on the Fashion MNIST dataset
}. Among the existing methods, neural networks have shown very good results in the last years, specially after the resurgence of deep learning thanks to the increased computational power, better use of parallelization, and the development of frameworks like Pytorch and Tensorflow.

Currently, neural networks are at the forefront of many areas like image classification\footnote{
    Big breakthrough in 2012 with AlexNet and the resurgence of Convolutional Neural Networks \cite{alexnet2012}
}, reinforcement learning\footnote{
    AlphaGo was the first ever computer to be able to defeat a human professional player at the game of Go \cite{alphaGo2016} and its successor AlphaZero was able to surpass it by learning entirely through self-play \cite{alphaZero2017}
}, and generative modeling; where \acp{GAN}, which are the main focus of this document, are showing very good results.

This chapter will explain what are neural networks and how they can learn by themselves, this learning process is also often called training. First however, it is worth to have a brief description of the different types of learning.

\input{chapters/NeuralNets/typesML}
\input{chapters/NeuralNets/ANN}
\input{chapters/NeuralNets/gradDesc}
\input{chapters/NeuralNets/backprop}
\input{chapters/NeuralNets/others}


\chapter{Exist-Random Quantified SSAT}
\label{chap:exist-random-ssat}

In this chapter, we propose a new algorithm motivated by a recent QBF-solving technique for exist-random quantified SSAT formulas, which are also known as \textit{E-MAJSAT} formulas~\cite{Littman1998}.
Most content in this chapter is based on our conference paper~\cite{LeeIJCAI18ERSSAT} published at IJCAI\,'18.

\input{exist-random-ssat/preliminaries.tex}
\input{exist-random-ssat/technique.tex}
\input{exist-random-ssat/evaluation.tex}