%%% exercise 02 %%%
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath, amssymb, amsfonts, enumitem, fancyhdr, tikz}
% get rid of paragraph indent
\setlength{\parindent}{0 pt}
% allow section.equation numbering
\numberwithin{equation}{section}
% allows you to copy-paste code segments. requires pygments to be installed
% natively, i.e. not just in a Python venv! for example, on WSL Ubuntu you
% need to sudo apt-get install python3-pygments (unfortunately, the version
% will be a bit dated) and cannot rely on your venv Pygments version.
\usepackage{minted}
% makes clickable links to sections
\usepackage{hyperref}
% make the link colors blue, as well as cite colors. urls are magenta
\hypersetup{
    colorlinks, linkcolor = blue, citecolor = blue, urlcolor = magenta
}
% fancy pagestyle so we can use fancyhdr for fancy headers/footers
\pagestyle{fancy}
% add logo in right of header. note that you will have to adjust logo path!
\fancyhead[R]{\includegraphics[scale = 0.15]{../bac_logo1.png}}
% don't show anything in the left and center header
\fancyhead[L, C]{}
% give enough space for logo by reducing top margin height, head separator,
% increasing headerheight. see Figure 1 in the fancyhdr documentation. if
% \topmargin + \headheight + \headsep = 0, original text margins unchanged.
\setlength{\topmargin}{-60 pt}
\setlength{\headheight}{50 pt}
\setlength{\headsep}{10 pt}
% remove decorative line in the fancy header
\renewcommand{\headrulewidth}{0 pt}

% title, author, date
\title{Exercise 2}
\author{Derek Huang\thanks{NYU Stern 2021, BAC Advanced Team.}}
\date{May 20, 2021\thanks{Original version released on February 25, 2021.}}

\begin{document}

\maketitle
% need to include this after making title to undo the automatic
% \thispagestyle{plain} command that is issued.
\thispagestyle{fancy}

\section{Linear algebra}

\subsection{The centering matrix}

Let $ \mathbf{X} \in \mathbb{R}^{N \times d} $ be a matrix where each $ k $th
row is a transposed data example $ \mathbf{x}_k^\top $. Some matrix analysis
methods require that the data be \textit{centered}, i.e. that the sample mean
$ \bar{\mathbf{x}} \triangleq \frac{1}{N}\mathbf{X}^\top\mathbf{1} =
\mathbf{0} \in \mathbb{R}^d $. Let $ \tilde{\mathbf{X}} \in
\mathbb{R}^{N \times d} $ be $ \mathbf{X} $ centered, where each $ k $th row
is $ \tilde{\mathbf{x}}_k^\top \triangleq \mathbf{x}_k^\top -
\bar{\mathbf{x}}^\top $ and $ \frac{1}{N}\tilde{\mathbf{X}}^\top\mathbf{1}
= \mathbf{0} $. Show that
\begin{equation*}
    \tilde{\mathbf{X}} = \left(
        \mathbf{I} - \frac{1}{N}\mathbf{11}^\top
    \right)\mathbf{X}
\end{equation*}

The matrix $ \mathbf{I} - \frac{1}{N}\mathbf{11}^\top \in
\mathbb{R}^{N \times N} $ is called the \textit{centering matrix}.

\subsection{Symmetric rank-1 matrices}

The \textit{outer product} of $ \mathbf{x} \in \mathbb{R}^m $,
$ \mathbf{y} \in \mathbb{R}^n $, is $ \mathbf{xy}^\top \in
\mathbb{R}^{m \times n} $, where
\begin{equation*}
    \mathbf{xy}^\top = \begin{bmatrix}
        \ x_1y_1 & \ldots & x_1y_n \ \\
        \ \vdots & \ddots & \vdots \ \\
        \ x_my_1 & \ldots & x_my_n \
    \end{bmatrix}
\end{equation*}
Prove that $ \mathbf{xx}^\top $ is symmetric and that if $ \mathbf{x} \ne
\mathbf{0} $, $ \operatorname{rank}\big(\mathbf{xx}^\top\big) = 1 $.

\subsection{The $ \ell^0 $ ``norm''}

The \textit{counting}, or $ \ell^0 $, ``norm'' of a vector $ \mathbf{x} \in
\mathbb{R}^n $, denoted by $ \Vert\mathbf{x}\Vert_0 $, gives the number of
nonzero elements of $ \mathbf{x} $. The $ \ell^0 $ ``norm'' is called a norm
in quotations since it does not satisfy the requisite properties to be a norm,
although it does satisfy the triangle inequality. Prove that the $ \ell^0 $
``norm'' is not a norm.

\subsection{Vector orthogonality}

Recall that for $ \mathbf{u}, \mathbf{v} \in \mathbb{R}^n \setminus
\{\mathbf{0}\} $, $ \mathbf{u}^\top\mathbf{v} = \Vert\mathbf{u}\Vert_2
\Vert\mathbf{v}\Vert_2\cos\theta $, $ \theta \in [0, \pi] $. Prove
\begin{enumerate}[label = \alph*.]
    \item
    $ \forall k \in (0, \infty) $, $ \mathbf{u} = k\mathbf{v} \Rightarrow
    \theta = 0 $.

    \item
    $ \mathbf{u}^\top\mathbf{v} = 0 \Leftrightarrow \theta = \frac{\pi}{2} $.
\end{enumerate}

% pushes footnote down a bit
\medskip

\subsection{QR decomposition linear least squares}

The QR decomposition of a matrix $ \mathbf{A} \in \mathbb{R}^{m \times n} $
where $ \operatorname{rank}(\mathbf{A}) = n $ factors $ \mathbf{A} $ such that
\cite{bv_convex_opt}
\begin{equation*}
    \mathbf{A} = \begin{bmatrix} \ \mathbf{Q}_1 & \mathbf{Q}_2 \ \end{bmatrix}
    \begin{bmatrix} \ \mathbf{R} \ \\ \ \mathbf{0} \ \end{bmatrix}
     = \mathbf{Q}_1\mathbf{R}
\end{equation*}

Here $ \mathbf{Q}_1 \in \mathbb{R}^{m \times n} $, $ \mathbf{R} \in
\mathbb{R}^{n \times n} $. $ \mathbf{Q}_1 $ has orthonormal columns, i.e.
$ \mathbf{Q}_1^\top\mathbf{Q}_1 = \mathbf{I}_n $, and $ \mathbf{R} $ is
\textit{upper triangular}, i.e. all nonzero elements are on the main diagonal
and above, with nonzero diagonal elements. Given centered input data
$ \mathbf{X} \in \mathbb{R}^{N \times d} $,
$ \operatorname{rank}(\mathbf{A}) = d $, each row a transposed data example
$ \mathbf{x}_k^\top $, and centered\footnotemark\footnotetext{
    If $ \mathbf{X}, \mathbf{y} $ are already centered, i.e.
    $ \mathbf{X}^\top\mathbf{1} = \mathbf{0} $,
    $ \mathbf{y}^\top\mathbf{1} = 0 $, then we don't need to fit the bias
    term.
} response vector $ \mathbf{y} \in \mathbb{R}^N $, the linear least squares
problem can be written in matrix-vector form as
\begin{equation*}
    \begin{array}{ll}
        \displaystyle\min_\mathbf{w} &
        (\mathbf{y} - \mathbf{Xw})^\top(\mathbf{y} - \mathbf{Xw})
    \end{array}
\end{equation*}

Suppose $ \mathbf{X} = \mathbf{Q}_1\mathbf{R} $, $ \mathbf{Q}_1 \in
\mathbb{R}^{N \times d} $, $ \mathbf{R} \in \mathbb{R}^{d \times d} $. Write
an analytical expression for $ \hat{\mathbf{w}} $, the \textit{ordinary least
squares estimator}, that solves this problem. The expression must not contain
$ \mathbf{X} $.

\medskip

\textit{Hints.} The objective is convex, so its gradient is $ \mathbf{0} $ at
$ \hat{\mathbf{w}} $. $ \mathbf{R} $ and $ \mathbf{X}^\top\mathbf{X} $ are
invertible. Use properties of the matrix transpose and the fact that
$ \mathbf{Q}_1^\top\mathbf{Q}_1 = \mathbf{I}_d $. It may help to find an
expression for $ \hat{\mathbf{w}} $ first without factoring $ \mathbf{X} $
and to then substitute $ \mathbf{Q}_1\mathbf{R} $ for $ \mathbf{X} $ and
perform any additional calculations at the end.

\section{Vector calculus}

\subsection{Quadratic functions}

Given $ \mathbf{Q} \succeq \mathbf{0} \in \mathbb{R}^{n \times n} $,
$ \mathbf{a} \in \mathbb{R}^n $, $ b \in \mathbb{R} $, define function
$ f : \mathbb{R}^n \rightarrow \mathbb{R} $ such that $ \forall \mathbf{x} \in
\mathbb{R}^n $,
\begin{equation*}
    f(\mathbf{x}) \triangleq \frac{1}{2}\mathbf{x}^\top\mathbf{Qx} +
    \mathbf{a}^\top\mathbf{x} + b
\end{equation*}

\begin{enumerate}[label = \alph*.]
    \item
    Determine the form of $ \nabla f : \mathbb{R}^n \rightarrow
    \mathbb{R}^n $, the gradient of $ f $.

    \item
    Determine the form of $ \nabla^2f : \mathbb{R}^n \rightarrow
    \mathbb{R}^{n \times n} $, the Hessian of $ f $. What does it tell you
    about $ f $?
\end{enumerate}

\subsection{Ridge regression}

Given centered input data $ \mathbf{X} \in \mathbb{R}^{N \times d} $,
$ \operatorname{rank}(\mathbf{X}) = d $, each row a transposed data example
$ \mathbf{x}_k^\top $, centered response vector
$ \mathbf{y} \in \mathbb{R}^N $, $ \lambda > 0 $, the ridge regression problem
can be written in matrix-vector form as
\begin{equation*}
    \begin{array}{ll}
        \displaystyle\min_\mathbf{w} &
        (\mathbf{y} - \mathbf{Xw})^\top(\mathbf{y} - \mathbf{Xw}) +
        \lambda\Vert\mathbf{w}\Vert_2^2
    \end{array}
\end{equation*}

Find an analytical expression for $ \tilde{\mathbf{w}} \in \mathbb{R}^d $, the
\textit{ridge estimator}, that solves this problem.

\medskip

\textit{Hints.} The objective is convex, so its gradient is $ \mathbf{0} $ at
$ \tilde{\mathbf{w}} $. $ \mathbf{X}^\top\mathbf{X} + \lambda\mathbf{I} $ is
invertible.

\bibliographystyle{acm}
\bibliography{../master_bib}

\end{document}


% Filename: Lambda.tex
% Author: Alan Jeffrey
% Last modified: 11 May 1990
%
% Copyright (c) 1990 Alan Jeffrey.  Permission is granted to Tugboat
% to publish any or all of this.
%
% A keyboard check:
%
%     @ # $ % ^ & * ~   at hash dollar percent caret ampersand asterisk tilde
%     : ; , .           colon semicolon comma period
%     ? !               question-mark exclamation-mark
%     " ' `             double-quote apostrophe back-quote
%     ( ) { } [ ]       parentheses braces square-brackets
%     - + = / \         minus plus equals forward-slash backslash
%     _ | < >           underscore vertical-bar less-than greater-than
%

\documentstyle[ltugboat,Lambda]{article}

% This document defines a whole load of extra commands, some of which
% over-ride how LaTeX normally lays things out.  For example, ~ is
% redefined to give a hairspace in math mode.  This whole document
% should probably be put in a group to stop it getting in the way
% of other articles' macros.

\title{Lists in \TeX's Mouth}

\author{Alan Jeffrey}

\address{Programming Research Group\\
         Oxford University\\
         11 Keble Road\\
         Oxford OX1 3QD}

\netaddress{Alan.Jeffrey@uk.ac.oxford.prg}

\makeatletter

% The mathcodes for the letters A, ..., Z, a, ..., z are changed to
% generate text italic rather than math italic by default. This makes
% multi-letter names look neater. The mathcode for character 'c'
% is set to "7000 (variable family) + "400 (text italic) + c.
%
% This neat bit of code is due to Mike Spivey.

\def\@setmcodes#1#2#3{{\count0=#1 \count1=#3
        \loop \global\mathcode\count0=\count1 \ifnum \count0<#2
        \advance\count0 by1 \advance\count1 by1 \repeat}}

\@setmcodes{`A}{`Z}{"7441}
\@setmcodes{`a}{`z}{"7461}

\def\Number#1{\csname Number-#1\endcsname}
\def\Label#1{\csname Label-#1\endcsname}

\newcount\Lastnum

\def\Forward#1%
   {\global\advance\Lastnum by 1
    \csnameafter\xdef{Number-#1}%
       {\the\Lastnum}%
    \csnameafter\xdef{Label-\the\Lastnum}%
       {\@currentlabel}}

\def\csnameafter#1#2%
   {\expandafter#1\csname#2\endcsname}

\def\Bylist#1%
   {\Map\Label
      {\Insertsort\Lessthan
         {\Map\Number{#1}}}}

\def\By{\Show\Bylist}

\let\bindspace=~
\def~{\ifmmode \, \else \bindspace \fi}

\def\start#1{\lefteqn{#1}\quad\\}

\def\nil{[\,\,]}

\newtheorem{fact}{Fact}
\def\thefact{\@roman\c@fact}

\def\cstok#1{\leavevmode\thinspace\hbox{\vrule\vtop{\vbox{\hrule\kern1pt
        \hbox{\vphantom{\tt/}\thinspace{\tt#1}\thinspace}}%
      \kern1pt\hrule}\vrule}\thinspace}

\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xTeXcode#1\end{TeXcode}[#1|end[TeXcode]]
|endgroup

\def\TeXcode
   {\@verbatim \smallskip\hrule\medskip \frenchspacing\@vobeyspaces \@xTeXcode}
\def\endTeXcode
   {\medskip\hrule\smallskip\endtrivlist}

\makeatother

\begin{document}

\maketitle

\section{Why lists?}

Originally, I wanted lists in \TeX\ for
a paper I was writing which contained a lot of facts.
\begin{fact}
\Forward{Fac-cows}
   Cows have four legs.
\end{fact}
\begin{fact}
\Forward{Fac-people}
   People have two legs.
\end{fact}
\begin{fact}
\Forward{Fac-yawn}
   Lots of facts in a row can be dull.
\end{fact}
These are generated with commands like
\begin{verbatim}
\begin{fact}
\Forward{Fac-yawn}
   Lots of facts in a row can be dull.
\end{fact}
\end{verbatim}
I can then refer to these facts by saying
\begin{verbatim}
\By[Fac-yawn,Fac-cows,Fac-people]
\end{verbatim}
to get
\By[Fac-yawn,Fac-cows,Fac-people].
And as if by magic, the facts come out sorted, rather than in
the jumbled order I typed them.  This is very useful, as I can
reorganize my document to my heart's content, and not have to worry
about getting my facts straight.

Originally I tried programming this sorting routine in \TeX's
list macros, from Appendix~D of \TB, but I soon ran into trouble.
The problem is that all the Appendix~D macros work by assigning
values to macros.  For example:
\begin{verbatim}
\concatenate\foo=\bar\baz
\end{verbatim}
expands out to
\begin{verbatim}
\ta=\expandafter{\bar}
\tb=\expandafter{\baz}
\edef\foo{\the\ta\the\tb}
\end{verbatim}
which assigns the macro \verb|\foo| the contents of \verb|\bar|
followed by the contents of \verb|\baz|.  Programming sorting routines
(which are usually recursive) in terms of these lists became rather
painful, as I was constantly having to watch out for local variables,
worrying about what happened if a local variable had the same name
as a global one, and generally having a hard time.

Then I had one of those ``flash of light'' experiences ---
``You can do lambda-calculus in \TeX,'' I thought,
and since you can do lists directly in lambda calculus,
you should be able to do lists straightforwardly in \TeX.  And so you
can.  Well, fairly straightforwardly anyway.

So I went and did a bit of mathematics, and derived the \TeX\ macros
you see here.  They were formally verified, and worked first time
(modulo typing errors, of which there were two).

\section{\TeX's mouth and \TeX's stomach}

\TeX's programming facilities come in two forms --- there are \TeX's
{\em macros\/} which are expanded in its mouth, and some additional
{\em assignment\/} operations like \verb|\def| which take place in the
stomach.  \TeX\ can often spring surprises on you as exactly what
gets evaluated where.
For example, in \LaTeX\ I can put down a
label by saying \verb|\label{Here}|.
\label{Here}
Then I can refer back to that label by saying
\verb|Section~\ref{Here}|, which
produces Section~\ref{Here}.  Unfortunately, \verb|\ref{Here}| does
{\em not\/} expand out to {\tt\ref{Here}}!  Instead, it expands out to:
\begin{verbatim}
\edef\@tempa{\@nameuse{r@Here}}
\expandafter\@car\@tempa\@nil\null
\end{verbatim}
This means that I can't say
\begin{verbatim}
\ifnum\ref{Here}<4 Hello\fi
\end{verbatim}
and hope that this will expand out to Hello.  Instead I
get an error message.  Which is rather a pity, as \TeX's mouth is
quite a powerful programming language (as powerful as a Turing Machine in
fact).

\section{Functions}

A {\em function\/} is a mathematical object that takes in an argument
(which could well be another function) and returns some other mathematical
object.  For example the function $Not$ takes in a boolean and returns
its complement.  I'll write function application without brackets,
so $Not~b$ is the boolean complement of $b$.

Function application
binds to the left, so $f~a~b$ is $(f~a)~b$ rather than $f~(a~b)$.
For example, $Or~a~b$ is the boolean or of $a$ and $b$, and
$Or~True$ is a perfectly good function that takes in a boolean
and returns $True$.

The obvious equivalents of functions in \TeX\ are macros ---
if I define a function $Foo$ to be:
\begin{eqnarray*}
   Foo~x  &  =  &  True
\end{eqnarray*}
then it can be translated into \TeX\ as:
\begin{verbatim}
\def\Foo#1{\True}
\end{verbatim}
So where $Foo$ is a function that takes in one argument, \verb|\Foo|
is a macro that takes in one parameter.  Nothing has changed except
the jargon and the font.  \TeX\ macros can even be partially applied,
for example if we defined:
\begin{eqnarray*}
   Baz  &  =  &  Or~True
\end{eqnarray*}
then the \TeX\ equivalent would be
\begin{verbatim}
\def\Baz{\Or\True}
\end{verbatim}
Once \verb|\Baz| is expanded, it will expect to be given a parameter,
but when we are defining things, we can go around partially applying
them all we like.

Here, I'm using $=$ without formally defining it, which is rather
naughty.  If I say $x = y$, this means
``given enough parameters, $x$ and $y$ will eventually
expand out to the same thing.''  For example $Foo = Baz$, because
for any $x$,
\begin{eqnarray*}
   \start{Foo~x}
   &  =  &  True  \\
   &  =  &  Or~True~x  \\
   &  =  &  Baz~x
\end{eqnarray*}
Normally, functions have to {respect equality\/} which means that:
\begin{itemize}
\item if $x = y$ then $f~x = f~y$, and
\item if $x$ respects equality, then $f~x$ respects equality.
\end{itemize}
However, some \TeX\ control sequences don't obey this.  For example,
\verb|\string\Foo| and \verb|\string\Baz| are different, even though
$Foo = Baz$.  Hence $string$ doesn't respect equality.
Unless otherwise stated, we won't assume functions respect equality,
although all the functions defined here do.

All of our functions have capital letters, so that their \TeX\ equivalents
(\verb|\Not|, \verb|\Or| and so on) don't clash with standard \TeX\ or
\LaTeX\ macros.

\subsection{Identity}

The simplest function is the {\em identity\/} function, called
$Identity$ funnily enough, which is defined:
\begin{eqnarray*}
   Identity~x  &  =  &  \Identity{x}
\end{eqnarray*}
This, it must be admitted, is a pretty dull function, but
it's a useful basic combinator.  It can be implemented
in \TeX\ quite simply.
\begin{TeXcode}
\def\Identity#1{#1}
\end{TeXcode}
The rules around this definition mean that it is actually part of
\verb|Lambda.sty| and not just another example.

\subsection{Error}

Whereas $Identity$ does nothing in a fairly pleasant sort of way,
$Error$ does nothing in a particularly brutal and harsh fashion.
Mathematically, $Error$ is the function that destroys everything
else in front of it.  It is often written as $\perp$.
\begin{eqnarray*}
   Error~x  &  =  &  Error
\end{eqnarray*}
In practice, destroying the entire document when we hit one error
is a bit much, so we'll just print out an error message.
The user can carry on past an error at their own risk, as the code
will no longer be formally verified.
\begin{TeXcode}
\def\Error
   {\errmessage{Abandon verification all
                ye who enter here}}
\end{TeXcode}
Maybe this function ought to return a more useful error message \ldots

\subsection{First and Second}

Two other basic functions are $First$ and $Second$, both of which
take in two arguments, and do the obvious thing.  They are defined:
\begin{eqnarray*}
    First~x~y  &  =  &  x  \\
   Second~x~y  &  =  &  y
\end{eqnarray*}
We could, in
fact, define $Second$ in terms of $Identity$ and $First$.
For any $x$ and $y$,
\begin{eqnarray*}
   \start{First~Identity~x~y}
   &  =  &  Identity~y  \\
   &  =  &  y  \\
   &  =  &  Second~x~y
\end{eqnarray*}
So $First~Identity = Second$.  This means that anywhere in our \TeX\ code
we have \verb|\First\Identity| we could replace it by \verb|\Second|.
This is perhaps not the most astonishing \TeX\ fact known to humanity,
but this sort of proof did enable more complex bits of \TeX\ to be
verified before they were run.

The \TeX\ definitions of \verb|\First| and \verb|\Second| are pretty
obvious.
\begin{TeXcode}
\def\First#1#2{#1}
\def\Second#1#2{#2}
\end{TeXcode}
Note that in \TeX\, \verb|\First\foo\bar| expands out to
\verb|\foo| {\em without\/} expanding out \verb|\bar|.
This is very useful, as we can write macros that would take
forever and a day to run if they expanded all their arguments,
but which actually terminate quite quickly.  This is called
{\em lazy evaluation\/} by the functional programming community.

\subsection{Compose}

Given two functions $f$ and $g$ we would like to be able to {\em compose\/}
them to produce a function that first applies $g$ then applies $f$.
Normally, this is written as $f \circ g$, but unfortunately \TeX\ doesn't
have infix functions, so we'll have to write it $Compose~f~g$.
\begin{eqnarray*}
   Compose~f~g~x  &  =  &  f~(g~x)
\end{eqnarray*}
>From this definition, we can deduce that $Compose$ is associative:
\begin{eqnarray*}
   \start{Compose~(Compose~f~g)~h}
   &  =  &  Compose~f~(Compose~g~h)
\end{eqnarray*}
and $Identity$ is the left unit of $Compose$:
\begin{eqnarray*}
        Compose~Identity~f  &  =  &  f
\end{eqnarray*}
The reader may wonder why $Identity$ is called a {\em left\/} unit
even though it occurs on the right of the $Compose$ --- this is a side-effect
of using prefix notations where infix is more normal.  The infix version
of this equation is:
\begin{eqnarray*}
   Identity \circ f  &  =  &  f
\end{eqnarray*}
so $Identity$ is indeed on the left of the composition.

$Compose$ can be implemented in \TeX\ as
\begin{TeXcode}
\def\Compose#1#2#3{#1{#2{#3}}}
\end{TeXcode}

\subsection{Twiddle}

Yet another useful little function is $Twiddle$, which takes in
a function and reverses the order that function takes its (first two)
arguments.
\begin{eqnarray*}
   Twiddle~f~x~y  &  =  &  f~y~x
\end{eqnarray*}
Again, there aren't many immediate uses for such a function, but it'll
come in handy later on.  It satisfies the properties
\begin{eqnarray*}
              Twiddle~First  &  =  &  Second  \\
             Twiddle~Second  &  =  &  First  \\
    Compose~Twiddle~Twiddle  &  =  &  Identity
\end{eqnarray*}
Its \TeX\ equivalent is
\begin{TeXcode}
\def\Twiddle#1#2#3{#1{#3}{#2}}
\end{TeXcode}
This function is called ``twiddle'' because it is sometimes written
$\widetilde f$ (and $\sim$ is pronounced ``twiddle'').
It also twiddles its arguments around,
which is quite nice if your sense of humour runs to appalling puns.

\section{Booleans}

As we're trying to program a sorting routine, it would be nice to
be able to define orderings on things, and to do this we need some
representation of boolean variables.  Unfortunately \TeX\ doesn't have a type
for booleans, so we'll have to invent our own.  We'll
implement a boolean as a function $b$ of the form
\begin{eqnarray*}
   b~x~y  &
   =  &
   \left\{
      \begin{array}{ll}
         x  &  \mbox{if $b$ is true}  \\
         y  &  \mbox{otherwise}
      \end{array}
   \right.
\end{eqnarray*}
More formally, a
boolean $b$ is a function which respects equality,
such that for all $f$, $g$ and $z$:
\begin{eqnarray*}
   b~f~g~z  &  =  &  b~(f~z)~(g~z)
\end{eqnarray*}
and for all $f$ and $g$ which respect equality,
\begin{eqnarray*}
   b~(f~b)~(g~b)  &  =  &  b~(f~First)~(g~Second)
\end{eqnarray*}
All the functions in this section satisfy these properties.  Surprisingly
enough, so does $Error$, which is quite useful, as it allows us to
reason about booleans which ``go wrong''.

\subsection{True, False and Not}

Since we are implementing booleans as functions, we already have the
definitions of $True$, $False$ and $Not$.
\begin{eqnarray*}
    True  &  =  &  First  \\
   False  &  =  &  Second  \\
     Not  &  =  &  Twiddle
\end{eqnarray*}
So for free we get the following results:
\begin{eqnarray*}
          Not~True  &  =  &  False  \\
         Not~False  &  =  &  True  \\
   Compose~Not~Not  &  =  &  Identity
\end{eqnarray*}
The \TeX\ implementation is not exactly difficult:
\begin{TeXcode}
\let\True=\First
\let\False=\Second
\let\Not=\Twiddle
\end{TeXcode}

\subsection{And and Or}

The definitions of $And$ and $Or$ are:
\begin{eqnarray*}
   And~a~b  &
   =  &
   \left\{
      \begin{array}{ll}
         b      &  \mbox{if $a$ is true}  \\
         False  &  \mbox{otherwise}
      \end{array}
   \right.
   \\
   Or~a~b  &
   =  &
   \left\{
      \begin{array}{ll}
         True   &  \mbox{if $a$ is true}  \\
         b      &  \mbox{otherwise}
      \end{array}
   \right.
\end{eqnarray*}
With our definition of what a boolean is, this is just the same as
\begin{eqnarray*}
   And~a~b  &  =  &  a~b~False  \\
    Or~a~b  &  =  &  a~True~b
\end{eqnarray*}
>From these conditions, we can show that $And$ is associative, and
has left unit $True$ and left zeros $False$ and $Error$:
\begin{eqnarray*}
   And~(And~a~b)~c  &  =  &  And~a~(And~b~c)  \\
        And~True~b  &  =  &  b  \\
       And~False~b  &  =  &  False  \\
       And~Error~b  &  =  &  Error
\end{eqnarray*}
$Or$ is associative, has left unit $False$ and left zeros $True$ and $Error$:
\begin{eqnarray*}
   Or~(Or~a~b)~c  &  =  &  Or~a~(Or~b~c)  \\
      Or~False~b  &  =  &  b  \\
       Or~True~b  &  =  &  True  \\
      Or~Error~b  &  =  &  Error
\end{eqnarray*}
De~Morgan's laws hold:
\begin{eqnarray*}
   Not~(And~a~b)  &  =  &  Or~(Not~a)~(Not~b)  \\
    Not~(Or~a~b)  &  =  &  And~(Not~a)~(Not~b)
\end{eqnarray*}
and $And$ and $Or$ left-distribute through one another:
\begin{eqnarray*}
   Or~a~(And~b~c)  &  =  &  And~(Or~a~b)~(Or~a~c)  \\
   And~a~(Or~b~c)  &  =  &  Or~(And~a~b)~(And~a~c)
\end{eqnarray*}
$And$ and $Or$ are {\em not\/} commutative, though.  For example,
\begin{eqnarray*}
   \start{Or~True~Error}
   &  =  &  True~True~Error  \\
   &  =  &  True
\end{eqnarray*}
but
\begin{eqnarray*}
   \start{Or~Error~True}
   &  =  &  Error~True~True \\
   &  =  &  Error
\end{eqnarray*}
This is actually quite useful since there are some booleans that
need to return an error occasionally.  If $a$ is $True$ when $b$
is safe (i.e.\ doesn't become $Error$) and is $False$ otherwise, we can
say $Or~a~b$ and know we're not going to get an error.  This is handy
for things like checking for division by zero, or trying to get the
first element of an empty list.

Similarly, because of the possibility of $Error$,
$And$ and $Or$ don't right-distribute through each other,
as
\begin{eqnarray*}
   \start{Or~(And~False~Error)~True}
   &  \ne  &  And~(Or~False~True)~(Or~Error~True)
\end{eqnarray*}
As errors shouldn't crop up, this needn't worry us too much.
\begin{TeXcode}
\def\And#1#2{#1{#2}\False}
\def\Or#1#2{#1\True{#2}}
\end{TeXcode}

\subsection{Lift}

Quite a lot of the time we won't be dealing with booleans, but with
{\em predicates}, which are just functions that return a boolean.
For example, the predicate $Lessthan$ is defined below so that
$Lessthan~i~j$ is true whenever $i<j$.
Given a predicate $p$ we would like to be able to
{\em lift\/} it to $Lift~p$, defined:
\begin{eqnarray*}
   Lift~p~f~g~x  &  =  &  p~x~f~g~x
\end{eqnarray*}
For example, $Lift~(Lessthan~0)~f~g$ takes in a number and applies
$f$ to it if it is positive and $g$ to it otherwise.  This is quite
useful for defining functions.
\begin{TeXcode}
\def\Lift#1#2#3#4{#1{#4}{#2}{#3}{#4}}
\end{TeXcode}

\subsection{Lessthan and \TeX if}

Finally, we would like to be able to use \TeX's built-in booleans
as well as our own.  For example, we would like a predicate
$Lessthan$ such that:
\begin{eqnarray*}
   Lessthan~i~j  &
   =  &
   \left\{
      \begin{array}{ll}
         True   &  \mbox{if } i < j    \\
         False  &  \mbox{if } i \ge j  \\
         Error  &  \mbox{otherwise}
      \end{array}
   \right.
\end{eqnarray*}
The $Error$ condition happens if we try applying $Lessthan$ to something
that isn't a number --- $Lessthan~True~False$ is $Error$%
\footnote
   {Actually, that's a little white lie --- trying to persuade \TeX\ to
    do run-time type checking isn't much fun.  So the \TeX\ implementation
    of this is actually a {\em refinement\/} where the $Error$ condition
    has been replaced by whatever it is \TeX\ does if you try doing
    {\tt\string\ifnum $x$ < $y$} when $x$ and $y$ aren't numbers}.
This is fine as a mathematical definition, but how will
we implement it?  If we assume we have a macro \verb|\TeXif|,
which converts \TeX\ if-statements into booleans, we could just
define:
\begin{TeXcode}
\def\Lessthan#1#2{\TeXif{\ifnum#1<#2 }}
\end{TeXcode}
So the question is just how to define \verb|\TeXif|.
Unfortunately, the ``obvious'' code does not work:
\begin{verbatim}
\def\TeXif#1#2#3{#1#2\else#3\fi}
\end{verbatim}
For example, \verb|\TeXif\iftrue\True\True| doesn't expand out to
\verb|\True|.  Instead, it expands as:
\begin{verbatim}
\TeXif\iftrue\True\True
   = \iftrue\True\else\True\fi
   = \True\else\True\fi
   = \else\fi
   =
\end{verbatim}
Another common \TeX nique is to use a macro \verb|\next| to
be the expansion text:
\begin{verbatim}
\def\TeXif#1#2#3%
   {#1\def\next{#2}\else\def\next{#3}\fi
    \next}
\end{verbatim}
However, this uses \TeX's stomach to do the \verb|\def|, and we are
trying to do this using only the mouth.  One (slightly tricky) solution
is to use pattern-matching to gobble up the offending \verb|\else| and/or
\verb|\fi|.
\begin{TeXcode}
\def\gobblefalse\else\gobbletrue\fi#1#2%
   {\fi#1}
\def\gobbletrue\fi#1#2%
   {\fi#2}
\def\TeXif#1%
   {#1\gobblefalse\else\gobbletrue\fi}
\end{TeXcode}
So if the \TeX\ if-statement is true, \verb|\gobblefalse| gobbles
up the false-text, otherwise \verb|\gobbletrue| gobbles up the
true-text.  For example,
\begin{verbatim}
\TeXif\iftrue\True\True
   = \iftrue\gobblefalse\else
         \gobbletrue\fi\True\True
   = \gobblefalse\else
         \gobbletrue\fi\True\True
   = \fi\True
   = \True
\end{verbatim}
Phew.  And so we have booleans.

\section{Lists}

A list is a (possibly infinite) sequence of values.  For example,
the list $[1,2,3]$ contains three numbers, the list $\nil$ contains
none, and the list $[1,2,3,\ldots]$ contains infinitely many.
A list is either {\em empty\/} (written $\nil$) or is comprised
of a {\em head\/}
$x$ and a {\em tail\/} $xs$ (in which case it's written $x:xs$).
For example, $1:2:3:\nil$ is $[1,2,3]$.

In a similar fashion to the implementation of booleans,
a list $xs$ is implemented as a function of the form
\begin{eqnarray*}
   xs~f~e  &
   =  &
   \left\{
      \begin{array}{ll}
         e       &  \mbox{if $xs$ is empty}  \\
         f~y~ys  &  \mbox{if $xs$ has head $y$ and tail $ys$}
      \end{array}
   \right.
\end{eqnarray*}
Again, we are implementing a datatype as a function, a quite powerful
trick, just not one usually seen in \TeX.  We will assume that
whenever a list $x:xs$ is applied to $f$ and $e$, $f~x$ respects equality.
This allows us to assume that if $xs = ys$ then $x:xs = x:ys$,
which is handy.

\subsection{Nil, Cons, Stream and Singleton}

The simplest list is $Nil$, the empty list which we have been writing
$\nil$.
\begin{eqnarray*}
   Nil  &  =  &  Second
\end{eqnarray*}
The other possible list is $Cons~x~xs$, which has head $x$ and tail $xs$.
\begin{eqnarray*}
   Cons~x~xs~f~e  &  =  &  f~x~xs
\end{eqnarray*}
Every list can be constructed using these functions.
The list $[1,2,3]$ is $Cons~1~(Cons~2~(Cons~3~Nil))$, and the
list $[a,a,a,\ldots]$ is $Stream~a$ where $Stream$ is defined:
\begin{eqnarray*}
   Stream~a  &  =  &  Cons~a~(Stream~a)
\end{eqnarray*}
There's even at least one application for infinite lists,
as we'll see in Section~\ref{outputroutines}.

The singleton list $[a]$ is $Singleton~a$, defined as:
\begin{eqnarray*}
   Singleton~a  &  =  &  Cons~a~Nil
\end{eqnarray*}
These all have straightforward \TeX\ definitions.
\begin{TeXcode}
\let\Nil=\Second
\def\Cons#1#2#3#4{#3{#1}{#2}}
\def\Stream#1{\Cons{#1}{\Stream{#1}}}
\def\Singleton#1{\Cons{#1}\Nil}
\end{TeXcode}

\subsection{Head and Tail}

So, we can construct any list we like, but we still can't get any information
out of it.  To begin with, we'd like to be able to get the head
and tail of a list.
\begin{eqnarray*}
   Head~xs  &  =  &  xs~First~Error  \\
   Tail~xs  &  =  &  xs~Second~Error
\end{eqnarray*}
For example, the tail of $x:xs$ is
\begin{eqnarray*}
   \start{Tail~(Cons~x~xs)}
   &  =  &  Cons~x~xs~Second~Error  \\
   &  =  &  Second~x~xs  \\
   &  =  &  \Tail{\Cons{x}{xs}}
\end{eqnarray*}
The tail of $\nil$ is, as one would expect,
\begin{eqnarray*}
   \start{Tail~Nil}
   &  =  &  Nil~Second~Error  \\
   &  =  &  Error
\end{eqnarray*}
And the head of $Stream~a$ is
\begin{eqnarray*}
   \start{Head~(Stream~a)}
   &  =  &  Stream~a~First~Error  \\
   &  =  &  Cons~a~(Stream~a)~First~Error  \\
   &  =  &  First~a~(Stream~a)  \\
   &  =  &  \Head{\Stream{a}}
\end{eqnarray*}
So we can get the head of an infinite list in finite time.  This is
fortunate, as otherwise there wouldn't be much point in allowing
infinite objects.
\begin{TeXcode}
\def\Head#1{#1\First\Error}
\def\Tail#1{#1\Second\Error}
\end{TeXcode}

\subsection{Foldl and Foldr}

Using $Head$ and $Tail$ we can get at the beginning of any non-empty list,
but in general we need more information than that.  Rather than write
a whole bunch of recursive functions on lists, I'll implement two
fairly general functions, with which we can implement (almost) everything
else.

$Foldl$ and $Foldr$ both take in functions and apply them recursively
to a list.  $Foldl$ starts at the left of the list, and $Foldr$
starts at the right.  For example,
\begin{eqnarray*}
   Foldl~f~e~[1,2,3]  &  =  &  f~(f~(f~e~1)~2)~3  \\
   Foldr~f~e~[1,2,3]  &  =  &  f~1~(f~2~(f~3~e))
\end{eqnarray*}
These functions will be used a lot later on.  $Foldl$ can be defined:
\begin{eqnarray*}
      Foldl~f~e~xs  &  =  &  xs~(Foldl'~f~e)~e  \\
   Foldl'~f~e~x~xs  &  =  &  Foldl~f~(f~e~x)~xs
\end{eqnarray*}
So $Foldl~f~e~\nil$ is
\begin{eqnarray*}
   \start{Foldl~f~e~Nil}
   &  =  &  Nil~(Foldl'~f~e)~e  \\
   &  =  &  \Foldl{f}{e}\Nil
\end{eqnarray*}
And $Foldl~f~e~(x:xs)$ is
\begin{eqnarray*}
   \start{Foldl~f~e~(Cons~x~xs)}
   &  =  &  Cons~x~xs~(Foldl'~f~e)~e  \\
   &  =  &  Foldl'~f~e~x~xs  \\
   &  =  &  Foldl~f~(f~e~x)~xs
\end{eqnarray*}
For example, $Foldl~f~e~[1,2,3]$ is
\begin{eqnarray*}
   \start{Foldl~f~e~[1,2,3]}
   &  =  &  Foldl~f~(f~e~1)~[2,3]  \\
   &  =  &  Foldl~f~(f~(f~e~1)~2)~[3]  \\
   &  =  &  Foldl~f~(f~(f~(f~e~1)~2)~3)~\nil  \\
   &  =  &  f~(f~(f~e~1)~2)~3
\end{eqnarray*}
as promised.  Similarly, we can define $Foldr$ as
\begin{eqnarray*}
      Foldr~f~e~xs  &  =  &  xs~(Foldr'~f~e)~e  \\
   Foldr'~f~e~x~xs  &  =  &  f~x~(Foldr~f~e~xs)
\end{eqnarray*}
For $Foldr~f$ to respect equality, $f~x$ should respect equality.

When we do the unfolding, we discover that
\begin{eqnarray*}
       Foldr~f~e~\nil  &  =  &  e  \\
   Foldr~f~e~(x:xs)  &  =  &  f~e~(Foldr~f~e~xs)
\end{eqnarray*}
$Foldr$ tends to be more efficient than $Foldl$, because $Foldl$
has to run along the entire list before it can start applying $f$,
whereas $Foldr$ can apply $f$ straight away.  If $f$ is a lazy function,
this can make quite a difference.  $Foldl$ on infinite lists, anyone?
\begin{TeXcode}
\def\Foldl#1#2#3%
   {#3{\Foldl@{#1}{#2}}{#2}}
\def\Foldl@#1#2#3#4%
   {\Foldl{#1}{#1{#2}{#3}}{#4}}
\def\Foldr#1#2#3%
   {#3{\Foldr@{#1}{#2}}{#2}}
\def\Foldr@#1#2#3#4%
   {#1{#3}{\Foldr{#1}{#2}{#4}}}
\end{TeXcode}

\subsection{Cat}

Given two lists, we would like to be able to stick them together,
which is what $Cat$ (short for ``concatenate'')
does.  For example, $Cat~[1,2]~[3,4]$ is
$[1,2,3,4]$.  It can be defined using $Foldr$:
\begin{eqnarray*}
   Cat~xs~ys  &  =  &  Foldr~Cons~ys~xs
\end{eqnarray*}
So
\begin{eqnarray*}
   \start{Cat~[1,2]~[3,4]}
   &  =  &  Foldr~Cons~[3,4]~[1,2]  \\
   &  =  &  Cons~1~(Foldr~Cons~[3,4]~[2])  \\
   &  =  &  Cons~1~(Cons~2~(Foldr~Cons~[3,4]~\nil))  \\
   &  =  &  Cons~1~(Cons~2~[3,4])  \\
   &  =  &  \Unlistize{\Cat{\Listize[1,2]}{\Listize[3,4]}}
\end{eqnarray*}
The \TeX\ code for \verb|\Cat| is suspiciously similar to its mathematical
definition.
\begin{TeXcode}
\def\Cat#1#2{\Foldr\Cons{#2}{#1}}
\end{TeXcode}

\subsection{Reverse}

We can reverse any list with the function $Reverse$, defined using
$Foldl$:
\begin{eqnarray*}
   Reverse  &  =  &  Foldl~(Twiddle~Cons)~Nil
\end{eqnarray*}
For example, $Reverse~[1,2,3]$ can be calculated:
\begin{eqnarray*}
   \start{Reverse~[1,2,3]}
   &  =  &  Foldl~(Twiddle~Cons)~Nil~[1,2,3]  \\
   &  =  &  Twiddle~Cons  \\
   &     &  \quad (Twiddle~Cons~(Twiddle~Cons~Nil~1)~2)~3  \\
   &  =  &  Cons~3~(Cons~2~(Cons~1~Nil))  \\
   &  =  &  \Show\Reverse[1,2,3]
\end{eqnarray*}
The \TeX\ code for \verb|\Reverse| doesn't even take in any parameters.
\begin{TeXcode}
\def\Reverse{\Foldl{\Twiddle\Cons}\Nil}
\end{TeXcode}

\subsection{All, Some and Isempty}

Given a predicate $p$, we can find out if all the elements of
a list satisfy $p$ with $All~p$.  Similarly we can find if something
in the list satisfies $p$ with $Some~p$.  For example,
\begin{eqnarray*}
   All~(Lessthan~1)~[1,2,3]
   &  =  &  \All{\Lessthan 1}{\Listize[1,2,3]}{True}{False}  \\
   Some~(Lessthan~1)~[1,2,3]
   &  =  &  \Some{\Lessthan 1}{\Listize[1,2,3]}{True}{False}
\end{eqnarray*}
These can be defined
\begin{eqnarray*}
    All~p  &  =  &  Foldr~(Compose~And~p)~True  \\
   Some~p  &  =  &  Foldr~(Compose~Or~p)~False
\end{eqnarray*}
For example, $Isempty$ can be defined
\begin{eqnarray*}
   Isempty  &  =  &  All~(First~False)
\end{eqnarray*}
This is probably not the most efficient check in the world, but we
hardly ever need it --- $Foldl$ or $Foldr$ will normally do the job.
\begin{TeXcode}
\def\All#1{\Foldr{\Compose\And{#1}}\True}
\def\Some#1{\Foldr{\Compose\Or{#1}}\False}
\def\Isempty{\All{\First\False}}
\end{TeXcode}

\subsection{Filter}

$Filter$ takes a predicate $p$ and a list $xs$, and returns a list
containing only those elements of $xs$ that satisfy $p$.  For example,
\begin{eqnarray*}
   Filter~(Lessthan~1)~[1,2,3]  &  =  &  \Show\Filter{\Lessthan 1}[1,2,3]
\end{eqnarray*}
$Filter$ can be defined as a $Foldr$:
\begin{eqnarray*}
   Filter~p  &  =  &  Foldr~(Lift~p~Cons~Second)~Nil
\end{eqnarray*}
Another easy bit of \TeX:
\begin{TeXcode}
\def\Filter#1%
   {\Foldr{\Lift{#1}\Cons\Second}\Nil}
\end{TeXcode}

\subsection{Map}

$Map$ takes a function $f$ and a list $xs$ and applies $f$ to every
element of $xs$.  For example,
\begin{eqnarray*}
   Map~f~[1,2,3]  &  =  &  \Show\Map{f~}[1,2,3]
\end{eqnarray*}
This is another job for $Foldr$.
\begin{eqnarray*}
   Map~f  &  =  &  Foldr~(Compose~Cons~f)~Nil
\end{eqnarray*}
We shall see $Map$ used later on, to convert from a list of
names such as \Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows], to a list of
labels such as \By[Fac-yawn,Fac-cows].
\begin{TeXcode}
\def\Map#1{\Foldr{\Compose\Cons{#1}}\Nil}
\end{TeXcode}

\subsection{Insert}

The only function we need which isn't easily defined as a reduction
is $Insert$, which inserts an element into a sorted list.  For example,
\begin{eqnarray*}
   Insert~Lessthan~3~[1,2,4,5]  &  =  &  \Show\Insert\Lessthan3[1,2,4,5]
\end{eqnarray*}
$Insert$ takes in an ordering as its first parameter, so we're not stuck
with one particular order.  It is defined directly in terms of the
definition of lists.
\begin{eqnarray*}
      Insert~o~x~xs  &  =  &  xs~(Insert'~o~x)~(Singleton~x)  \\
   Insert'~o~x~y~ys  &  =  &  o~x~y  \\
                     &     &  \quad  (Cons~x~(Cons~y~ys))  \\
                     &     &  \quad  (Cons~y~(Insert~o~x~ys))
\end{eqnarray*}
We can then define the function all this has been leading up to,
$Insertsort$ which takes an ordering and a list, and insert-sorts the
list according to the ordering.  For example,
\begin{eqnarray*}
   Insertsort~Lessthan~[2,3,1,2]  &  =  &  \Show\Insertsort\Lessthan[2,3,1,2]
\end{eqnarray*}
We can implement this as a fold:
\begin{eqnarray*}
   Insertsort~o  &  =  &  Foldr~(Insert~o)~Nil
\end{eqnarray*}
And so we've got sorted lists.
\begin{TeXcode}
\def\Insert#1#2#3%
   {#3{\Insert@{#1}{#2}}{\Singleton{#2}}}
\def\Insert@#1#2#3#4%
   {#1{#2}{#3}%
      {\Cons{#2}{\Cons{#3}{#4}}}%
      {\Cons{#3}{\Insert{#1}{#2}{#4}}}}
\def\Insertsort#1{\Foldr{\Insert{#1}}\Nil}
\end{TeXcode}
Interestingly, as we have implemented unbounded lists in \TeX's mouth,
this means we can implement a Turing Machine.  So, if you believe
the Church-Turing thesis, \TeX's mouth is as powerful as any
computer anywhere.  Isn't that good to know?

\section{Sorting reference lists}
\label{thissection}

So, these are the macros I've got to play with --- how do we apply them to
sorting lists of references?  Well, I'm using \LaTeX, which keeps the
current reference in a macro called \verb|\@currentlabel|, which
is~\ref{thissection} at the moment, as this is Section~\ref{thissection}.
So I just need to store the value of \verb|\@currentlabel| somehow.

Fortunately, I'm only ever going to be making references to facts
earlier on in the document, in order to make sure I'm not proving
any results in terms of themselves.  So I don't need to play around
with auxiliary files, and can just do everything in terms of
macros.

\subsection{Number and Label}

Each label in the document is given a unique number, in the order
the labels were put down.  So the number of \verb|Fac-cows|
is \verb|\Number{Fac-cows}|, which expands out to~\Number{Fac-cows},
the number of \verb|Fac-people| is~\Number{Fac-people}, and so on.

Each number has an associated label with it.  For example,
the first label is \verb|\Label{1}|, which is~\Label{1},
the second label is~\Label{2} and so on.  So to find the label for
\verb|Fac-cows|, we say \verb|\Label{\Number{Fac-cows}}| which expands
out to~\Label{\Number{Fac-cows}}.

These numbers and labels are kept track of in macros.  For example,
the number of \verb|Fac-cows| is kept in \cstok{Number-Fac-cows}.
Similarly, the first label is kept in \cstok{Label-1}.
As these macros have dashes in their names, they aren't likely to
be used already.

So the \TeX\ code for \verb|\Number| and \verb|\Label| is pretty
simple.
\begin{verbatim}
\def\Number#1{\csname Number-#1\endcsname}
\def\Label#1{\csname Label-#1\endcsname}
\end{verbatim}

\subsection{Lastnum and Forward}

The number of the most recent label is kept in \verb|\Lastnum|.
\begin{verbatim}
\newcount\Lastnum
\end{verbatim}
To put down a label \verb|Foo|, I type \verb|\Forward{Foo}|.
\Forward{Foo}
This increments the counter
\verb|\Lastnum|, and \verb|\xdef|s \cstok{Number-Foo}
to be the value of
\verb|\Lastnum|, which is now~\the\Lastnum.  So
\verb|\Number{Foo}| now expands to~\Number{Foo}.
Similarly, it \verb|\xdef|s \cstok{Label-\Number{Foo}} to be
\verb|\@currentlabel|, which is currently~\Label{\Number{Foo}}.
So \verb|\Label{\Number{Foo}}| now expands to~\Label{\Number{Foo}}.
\begin{verbatim}
\def\Forward#1%
   {\global\advance\Lastnum by 1
    \csnameafter\xdef{Number-#1}%
       {\the\Lastnum}%
    \csnameafter\xdef{Label-\the\Lastnum}%
       {\@currentlabel}}
\end{verbatim}
This uses \verb|\csnameafter\foo{bar}|, which expands out to
\verb|\foo\bar|.
\begin{verbatim}
\def\csnameafter#1#2%
   {\expandafter#1\csname#2\endcsname}
\end{verbatim}

\subsection{Listize, Unlistize and Show}

At the moment, lists have to be built up using \verb|\Cons| and
\verb|\Nil|, which is rather annoying.  Similarly, we can't actually
do anything with a list once we've built it.  We'd like some way
of converting lists in the form \verb|[a,b,c]| to and from the
form $[a,b,c]$.  This is done with \verb|\Listize| and \verb|\Unlistize|.
So \verb|\Listize[a,b,c]| expands to
\begin{verbatim}
\Cons{a}{\Cons{b}{\Cons{c}{\Nil}}}
\end{verbatim}
Similarly, \verb|\Unlistize| takes the list $[a,b,c]$ and expands out
to \verb|[a, b, c]|.  \verb|\Unlistize| is done with a $Foldr$.
\begin{TeXcode}
\def\Unlistize#1{[#1\Unlistize@{}]}
\def\Unlistize@#1{#1\Foldr\Commaize{}}
\def\Commaize#1#2{, #1#2}
\end{TeXcode}
The macro \verb|\Listize| is just a \TeX\ hack with pattern matching.
It would have been nice to use \verb|\@ifnextchar| for this, but
that uses \verb|\futurelet|, which doesn't expand in the mouth.  Oh well.
\begin{TeXcode}
\def\Listize[#1]%
   {\Listize@[#1,\relax]}
\def\Listize@#1,#2]%
   {\TeXif{\ifx\relax#2}%
        {\Singleton{#1}}%
        {\Cons{#1}{\Listize@#2]}}
\end{TeXcode}
This only works for nonempty lists --- \verb|\Listize[]| produces the
singleton list \verb|\Singleton{}|.  It also uses \verb|\relax| as its
end-of-list character, so lists with \verb|\relax| in them have to
be done by hand.  You can't win them all.  So
\begin{verbatim}
$\Unlistize{\Listize[a,b,c]}$
\end{verbatim}
produces
$\Unlistize{\Listize[a,b,c]}$.  This is such a common construction
that I've defined a macro \verb|\Show| such that
\verb|\Show\foo[a,b,c]| expands out to
\begin{verbatim}
\Unlistize{\foo{\Listize[a,b,c]}}
\end{verbatim}
For example, the equation
\begin{eqnarray*}
   Filter\,(Lessthan\,1)\,[1,2,3]
      &=& \Show\Filter{\Lessthan 1}[1,2,3]
\end{eqnarray*}
was generated with
\begin{verbatim}
\begin{eqnarray*}
   Filter\,(Lessthan\,1)\,[1,2,3]
      &=& \Show\Filter{\Lessthan 1}[1,2,3]
\end{eqnarray*}
\end{verbatim}
Many of the examples in this article were typeset this way.
\begin{TeXcode}
\def\Show#1[#2]%
   {\Unlistize{#1{\Listize[#2]}}}
\end{TeXcode}

\subsection{By}

Given these macros, we can now sort any list of references with $Bylist$,
defined
\begin{eqnarray*}
   Bylist~xs  &  =  &  Map~Label  \\
              &     &  \quad (Insertsort~Lessthan \\
              &     &  \quad\quad (Map~Number~xs))
\end{eqnarray*}
This takes in a list of label names like \verb|Fac-yawn|,
converts it into a list of numbers with $Map~Number$,
sorts the resulting list with $Insertsort~Lessthan$,
and finally converts all the numbers into labels like
\Label{\Number{Fac-yawn}} with $Map~Label$.  For example,
\begin{eqnarray*}
   \start{Bylist~\Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows]}
   &  =  &  Map~Label~(Insertsort~Lessthan \\
   &     &  \quad (Map~Number~
               \Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows]))  \\
   &  =  &  Map~Label~(Insertsort~Lessthan~
               \Show\Map\Number[Fac-yawn,Fac-cows]) \\
   &  =  &  Map~Label~\Show\Compose{\Insertsort\Lessthan}{\Map\Number}
               [Fac-yawn,Fac-cows]  \\
   &  =  &  \Show\Bylist[Fac-yawn,Fac-cows]
\end{eqnarray*}
The \TeX\ code for this is
\begin{verbatim}
\def\Bylist#1%
   {\Map\Label
      {\Insertsort\Lessthan
         {\Map\Number{#1}}}}
\end{verbatim}
So we can now stick all this together, and define the macro \verb|\By|
that prints out lists of references.  It is
\begin{verbatim}
\def\By{\Show\Bylist}
\end{verbatim}
So \verb|\By[Fac-yawn,Fac-cows]| is~\By[Fac-yawn,Fac-cows].
Which is quite nice.

\section{Other applications}
\label{outputroutines}

Is all this worth it?  Well, I've managed to get my lists of facts in
order, but that's not the world's most astonishing application.
There are other things that these lists are useful for, though.

For example, Damian Cugley has a macro package under development for
laying out magazines.  {\sc Mag}\TeX's output routine needs to be quite
smart, as magazines often have gaps where illustrations or photographs
are going to live.  In general, each block of text needs to be output
in a different fashion from every other block of text.
This will be handled by keeping an infinite list of output
routines.  Each time a box is cut off the scroll to be output,
the head of the list is chopped off and is used as the output routine
for that box.  That way, quite complex page shapes can be built up.

Mainly, though, these macros were written just as a challenge.
I learned quite a lot about \TeX\ and needed some \TeX niques I'd
never seen before.  It was also quite pleasing to see that \TeX\ code
can be formally verified, albeit in a rather noddy way.  Without
some sort of abstract view of lists, these \TeX\ macros could
not have been written.

\section{Acknowledgements}

Thanks to Jeremy Gibbons for letting me bounce ideas off him
and spotting the duff ones,
to Damian Cugley for saying ``Do you really think \TeX\ is meant
to do this?'', and to the Problem Solving Club for hearing me out.
This work was sponsored by the Science and Engineering
Research Council and Hewlett Packard.

\makesignature

\end{document}


\documentstyle[12pt, epsf]{article}
\setlength{\topmargin}{0.25cm}
\setlength{\topskip}{0.0cm}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{8in}
\setlength{\oddsidemargin}{1pt}
\setlength{\evensidemargin}{1pt}
\setlength{\parskip}{1ex plus0.5ex minus0.2ex}
\sloppy
\setlength{\parindent}{1cm}

\begin{document}
\begin{center}
	{\bf Laboratory Session 1}
\end{center}
Speech is produced by the excitation of time varying
vocal tract system by a time varying source (vibrations of vocal
cords). The excitation is generated by air flow from lungs carried by trachea
through vocal cords. As the acoustic wave passes through the vocal
tract, its frequency content (spectrum) is altered by the
resonances of the vocal tract. Vocal tract resonances are called
formants. Thus, the vocal tract shape can be estimated from the
spectral shape (e.g. formant location and spectral tilt) of the speech
signal. The speech produced is an acoustic wave which is recorded,
sampled, quantized and stored on the computer as sequence of numbers
(signal). The speech signal can't be used directly, as the information
is in the sequence of the numbers. So the speech signal has to be
processed and then features relevant to the task have to be
extracted. The features extracted may be related to voice source
i.e. vocal cords, like pitch frequency, pitch frequency contour
etc. or vocal tract system like
linear prediction parameters, cepstral etc. In this
laboratory session, we are going to study about speech signal processing
and extraction of features related to voice source and vocal-tract
system.\\[2ex]
The first experiment is to observe a 2 sec speech signal, you will
observe that the the energy in speech regions is more than the
nonspeech regions. Speech signal is nonstationary in nature so it is
processed as short-time signal. In the second experiment, we will
select a short-time speech signal and estimate the pitch frequency
manually. In the
third experiment, we will observe the autocorrelation of the
short-time signal and will compute the pitch frequency from it. In the
fourth experiment, we will estimate the Fourier spectrum of the
short-time signal and will also study the effect of windowing. In the
fifth experient, we will
study the spectrogram of the speech signal observed in the first
experiment. The sixth experiment is about
linear prediction analysis and studying the vocal-tract response, like
formants and voice source feature like pitch frequency. We will perform linear
prediction analysis on different speech sound signals and observe that
they have distinct features, in seventh experiment. Though, the features are distinctive in
nature they can vary, which makes the tasks such as speech recognition
or speaker recognition difficult. We will study about variability
introduced by speaker in the eighth experiment. In the last
experiment, we will study the pitch contour and the informations
embedded in it.\\[2ex]

The sampling frequency {\it sf} of the speech signal is 16000 Hz. Every
file name contains information about the
gender, speaker, trial number and the sound for e.g. the speech signal
file {\it f\_s1\_t1\_a} means it is the utterance {\it /a/} spoken by female
({\it f}), (for male its {\it m} and child {\it c}) speaker 1 ({\it s1})
and trial number 1 ({\it t1}). If you have problems in using any
of the routines, use {\it help} to know the usage, for e.g.\\[1ex]
\noindent
$\gg$ help speech\_signal\_observation\\[1ex]
\noindent
It will give the usage of the routine {\it
speech\_signal\_observation}.\\[2ex]

Note: The speech files are stored in ascii format so kindly don't edit
or tamper it. In all the experiments, the usage of the routine is
explained and then an example usage is given. Follow the example usage
for now.
\section{Speech Signal Observation}
\label{exp1}
Plot the 2 sec speech utterance using the {\it
speech\_signal\_observation} routine and observe the envelope of the
signal. The usage of the routine is as given below\\[2ex]
\noindent
$\gg$ data = speech\_signal\_observation('filename', fignu,
'plottitle'); \\[1ex]
\noindent
this routine plots the speech signal passed to the routine through the
file {\it filename} in a figure numbered {\it fignu} with the string
{\it plottitle} as the title and returns the speech signal array,
which is assigned to the variable {\it data}. for e.g.\\[2ex]
\noindent
$\gg$ fdata = speech\_signal\_observation('f\_s1\_t1\_a', 1, 'Utterance
/a/ of female speaker 1'); \\[2ex]
\noindent
the figure shows the speech utterance plotted in the upper part of the
figure and the short-time energy plotted below which is the envelope
of the speech signal.\\[1ex]

\noindent
Note: the speech signal array returned will be used for the
Experiments \ref{exp2} and \ref{exp5}.

\section{Observation of Short-Time Speech Signal and Manual Pitch
Computation}
\label{exp2}
Speech signal is nonstationary in nature but it can be assumed to be
quasistationary for one to three pitch periods (short-time signal). In
this experiment, we
are going to observe a short-time speech signal. Select 30 msec speech
signal from the 2 sec speech signal observed in experiment \ref{exp1}
by using routine {\it
select\_speech}. The usage of the routine is as given below\\[2ex]
\noindent
$\gg$ stdata = select\_speech(data, beginSampleNumber, endSampleNumber,
fignu, 'plottitle');\\[1ex]
\noindent
this routine plots a region of the speech signal {\it data} between the
samples {\it beginSampleNumber} and {\it endSampleNumber} in figure
number {\it fignu} with the string {\it plottitle} as the title. For
e.g.\\[2ex]
\noindent
$\gg$ fstdata = select\_speech(fdata, 15001, 15480, 2, '30 msec Speech
Signal of Utterance /a/ Spoken by Female Speaker 1');\\

\noindent
Observe the damped sinusoids repeated periodically. Find the period of
each sinusoid (neglect the sinusiods which are not complete in the
plot) in the following way:
\begin{itemize}
	\item [Step 1] Zoom (click) on the largest peak of each sinusoid and
note down the sample number (in the x-axis).
	\item [Step 2] Find the number of samples between each of the
consecutive peaks. It gives the period of each sinusoid.
\end{itemize}

Average the periods by the number of sinusoids, this is the
pitch period, $p_{t}$. Calculate the fundamental frequency or pitch
frequency $F_{0}$ using the following equation ({\it sf is the
sampling frequency})
\begin{equation}
\label{peq}
	F_{0} = \frac{sf}{p_{t}}
\end{equation}

\noindent
Note: The short-time speech signal region selected in this experiment
will be used for the Experiments \ref{exp3}, \ref{exp4} and \ref{exp6}.

\section{Autocorrelation Analysis}
\label{exp3}
In this experiment, we compute the autocorrelation of the short-time
speech signal obtained from the Experiment \ref{exp2} using routine
{\it autocorrelation}. The usage of the routine is as given
below\\[2ex]
\noindent
$\gg$ corrdata =  autocorrelation(stdata, order, fignu,
'plottitle');\\[2ex]
\noindent
The routine computes the autocorrelation of the short-time signal {\it
stdata} of order {\it order} and plots it in the figure number {\it
fignu} with string {\it plottitle} as the title. This routine returns
the autocorrelation value to the array {\it corrdata}. For e.g.\\[2ex]
\noindent
$\gg$ fcorrdata = autocorrelation(fstdata, 256, 3, 'Autocorrelation of
order 256 of the 30 msec speech signal of utterance /a/ of female
speaker 1');\\[1ex]
\noindent
The length of the autocorrleation array is {\it order + order + 1} which is
symmetric to the point {\it order + 1} (for the above example it is 257),
the value at this point is the energy of the short-time signal for
which the autocorrelation was computed. The upper plot shows the
actual autocorrelation (observe the symmetricity) and the plot
below shows the right half symmetry (i.e. from order + 1 to order +
order + 1). Zoom the second peak in this plot and find
the sequence number, it is the pitch period
$p_{t}$. Use equation \ref{peq} to find the fundamental frequency
$F_{0}$. Compare it with the $F_{0}$ obtained in the previous
experiment.

\section{Fourier Spectrum}
\label{exp4}
In this experiment, we compute the Fourier spectrum of the short-time
signal {\it stdata} obtained in the Experiment \ref{exp2} using
routine {\it FourierSpectrum}. The usage of the routine is as
follows\\[1ex]
\noindent
$\gg$ fourierSpectrum(stdata, order, fignu, 'plottitle');\\[1ex]
\noindent
this routine computes DFT of order {\it order} of the short-time signal
{\it stdata}. The order of the DFT is generally chosen such that it is
a $2^{n}$ value to use the FFT routine. Depending upon the number of
samples select the order of FFT which is near to it, for e.g. 30 msec
of the signal we are using has 480 samples so we select order as
512. The Fourier spectrum is plotted in figure {\it fignu} with string
{\it plottitle} as the title. See the usage e.g. below\\[1ex]
\noindent
$\gg$ fourierSpectrum(fstdata, 512, 4, 'Fourier spectrum of 30 msec
speech signal of utterance /a/ of female speaker 1');\\[1ex]
\noindent
the upper plot shows the 512 point DFT spectrum (observe the
symmetricity) and the plot below shows the left symmetry of the
plot (from point 1 to 256). Observe the spectral peaks they are the
formants (resonances in the vocal tract). The 512 point range covers
the entire sampling frequency range i.e. 16000 Hz which has redudant
information where as the plot below covers half of the sampling
frequency i.e. 8000Hz, which is the region of interest (recall the sampling
theorem).

%\subsection{Windowed Speech Analysis}
%Window the short-time speech signal {\it stdata} by Hamming
%window, as shown below\\[2ex]
%\noindent
%$\gg$ stdataw = stdata .* hamming(length(stdata));\\[2ex]
%e.g usage\\[1ex]
%$\gg$ fstdataw = fstdata .* hamming(length(fstdata));\\[1ex]

%Compute the Fourier spectrum for the windowed short-time signal {\it
%stdataw} and observe the difference in the Fourier spectrum of the
%signal windowed by a rectangular window {\it stdata} (which is
%implicit when you selected a short-time signal in Experiment 2) and the Hamming
%window {\it stdataw}.\\[2ex]
%\noindent
%$\gg$ fourierSpectrum(fstdataw, 512, 5, 'Fourier spectrum of 30 msec
%Hamming windowed speech signal of utterance /a/ of female speaker
%1');

\section{Spectrogram}
\label{exp5}
In this experiment we are going to compute the narrow-band and
wide-band spectrogram of the entire utterance i.e. the signal {\it
data} obtained from the Experiment \ref{exp1}. Recall that in the
wide-band we get time
resolution and in the narrow-band we get frequency resolution. The
spectorgam is computed using {\it plotSpectrogram} routine. See the
usage below\\[1ex]
\noindent
$\gg$ plotSpectrogram(data, order, hamming(order), sf, fignu,
'plottitle');\\[1ex]
\noindent
this routine computes the spectrogram of the given signal {\it data},
the type of spectrogram depends upon the order {\it order}, for wide-band
spectrogram we need small window we choose order 256 or 128 which is a
short duration, whereas, for narrow-band we choose order as 1024 or
2048, which is a long duration so we loose the time resolution. {\it
hamming} is the window and {\it sf} sampling frequency. The
spectrogram is plotted in the figure {\it fignu} with the string
plottitle as the title. See the usage examples below\\[1ex]
\noindent
Wide Band Spectrogram\\ $\gg$ plotSpectrogram(fdata, 256, hamming(256),
16000, 6, 'wide band spectrogram of the utterance /a/ of female
speaker 1');\\[1ex]
\noindent
Narrow Band Spectrogram\\ $\gg$ plotSpectrogram(fdata, 1024,
hamming(1024), 16000, 7, 'narrow band spectrogram of the utterance /a/
of female speaker 1');\\[1ex]
\noindent
The upper plot shows the time domain speech signal and the lower plot
shows the spectrogram of the time domain speech signal in the upper
plot. The high energy regions are in red color, the more dark it is
the more energy is in that region.

\section{Linear Prediction (LP) Analysis}
\label{exp6}
Linear prediction is the most common technique to estimate the
shape of the vocal tract. A $pth$ order linear prediction expresses
every sample as the linear weighted sum of the past $p$ samples. The
resulting difference equation expressed in $z-domain$ is
\begin{equation}
H(z) = \frac{1}{1 - \sum_{j = 1}^{p} a_{j}z^{-j}}
\end{equation}
The idea behind
linear prediction analysis is to estimate the $p$ $a_{k}$s'
which minimize the prediction error in mean-square sense. The linear
prediction error is also called LP residual. The $a_{k}$s'
determine the solution of the equation. The solution of the equation
in denominator is called pole. A real pole determines the spectral
roll off and a complex pole (which always exist with a conjugate)
determines the location of the formant in the LP spectrum. The LP
spectrum is the Fourier transform of the $a_{k}$s'.
\noindent
\subsection{LP Spectrum}
\label{lp}
In this experiment, we will observe the LP spectrum of the short-time
speech signal obtained from Experiment \ref{exp2} using
routine {\it lpSpectrum}. The usage of the routine is as given below\\[2ex]
$\gg$ lpSpectrum(stdata, lporder, win, order, sf, fignu,
'plottitle')\\[2ex]
{\it stdata} is the short-time signal obtained from {\it
select\_speech} routine, {\it lporder} is the linear prediction order
$p$, to window the signal by a hamming window set {\it win} 1 else set
it 0 (rectangular window), {\it order} is the FFT order needed to
compute the linear prediction spectrum from $a_{k}$s', {\it sf} is the
sampling frequency (which is 16000). The resulting linear prediction
and Fourier spectrum are plotted in figure number {\it fignu} with
{\it plottitle} as the title of the plot. e.g.\\[2ex]
$\gg$ lpSpectrum(fstdata, 14, 1, 512, 16000, 8, 'Linear Prediction
Spectrum of the short-time signal fstdata')\\[2ex]
In the figure, you will observe two plots. The upper plot is the Fourier
spectrum and the lower plot is the linear prediction spectrum. Observe
the spectral peaks (formants) in the linear prediction spectrum (which
is very clear) and Fourier spectrum. Zoom in the spectral peaks and
note down the frequency displayed on the x-axis then zoom in the wideband
spectrogram (observed in the earlier experiment) near that spectral
frequency and observe that the energy is indeed high in that
region. Now change the linear prediction
order i.e. {\it lporder} to, say 1, 3, 16, 20, 30, 50 and observe the
changes in the LP spectrum. Try to reason it.

\subsection{LP Residual}
\label{lpres}
In this experiment we will perform linear
prediction analysis and compute the LP residual of short-time speech signal
obtained from Experiment \ref{exp2}. The usage of the
routine is as given below.\\[2ex]
$\gg$ residual = lpResidual(stdata, nsample, lporder, fignu,
'plottitle')\\[1ex]
\noindent
{\it stdata} is the short-time signal obtained from {\it
select\_speech} routine, {\it nsample} is the number of samples in the
short-time signal, {\it lporder} is the linear prediction order
$p$. The routine plots the short-time signal and the LP Residual of it
in the figure {\it fignu} with title {\it plottitle}. An example of
the usage is as given below\\[2ex]
$\gg$ residual = lpResidual(fstdata, 480, 10, 9, 'LP Residual
Signal');
\begin{itemize}
	\item [Step 1] Zoom (click) on the largest peak of each
sinusoid in the upper plot and note down the sample number (in the
x-axis).
	\item [Step 2] Zoom (click) on the corresponding peaks in the
LP residual signal in the upper plot and note down the sample number
(in the x-axis).
	\item [Step 3] Compare the observations of step 1 and 2. Are
they same?
\end{itemize}
Perform an autocorrelation analysis on the residual signal using
routine {\it autocorrelation} and find the pitch period as it was done
in the Experiment \ref{exp3}. Usage of the routine is as
given below.\\[2ex]
$\gg$ autocorrelation(residual, 256, 10, 'Autocorrelation of LP
Residual signal');

\section{LP Spectrum of Different Speech Sounds}
\label{exp7}
In the Experiment \ref{lp}, we studied the LP spectrum of short-time
signal. In this experiment, we are going to study the LP spectrum of
different speech sounds using routine {\it lpSpectrum\_Sounds}. The
usage of the routine is as given below\\[2ex]
$\gg$ lpSpectrum\_Sounds(fignu)\\[2ex]
\noindent
This routine computes the LP spectrum of sounds {\it /a/, /e/, /i/,
/o/, /u/} and plots them in different figures. The input to this
routine is figure number {\it fignu}. An example usage is given below\\[2ex]
$\gg$ lpSpectrum\_Sounds(1)\\[2ex]
\noindent
Note down your observation.

\section{Intra and Inter Speaker Variability}
\label{exp8}
In the Experiments \ref{exp6} and \ref{exp7}, we studied the effect of
order on linear prediction and also we observed that for different
sounds the formants are different. In this experiment, we are going to
study about
variability caused by speakers. There are two kinds of speaker
variability that are of interest, they are intra-speaker variability
and inter-speaker variability. Intra-speaker variability is the
variability introduced by the same speaker while producing the same
sound. Inter-speaker variability is the variability introduced by
different speakers when producing the same sound. This can be useful
depending upon type of application, such as in speech recognition it is
good if there is no speaker variability, where as, for speaker
recognition inter-speaker variability is very important. Intra-speaker
variability is neither useful for speech recognition nor for
speaker recognition applications.
\begin{enumerate}

	\item The first experiment is to study the intra-speaker
variability. Three
utterances of the same sound $/a/$ spoken by the same speaker at three
different instants is used for this study. For this study, we will use
the routine {\it speakerVariation}. This routine takes in the
utterances file names, their corresponding begin and end points defining
the short-time signal and figure number as input. See the usage
below.\\[2ex]
$\gg$ speakerVariation('f\_s2\_t1\_a', 'f\_s2\_t2\_a', 'f\_s2\_t3\_a',
14001, 14480, 10001, 10480, 12481, 12960, 1);\\[2ex]
The linear prediction spectrum of the short-time signal of all the
three utterances is computed and plotted in the same figure. Observe
that the first two formants regions for all the three utterances are
almost same but it is not the case with higher formants.
	\item The second experiment is to study the inter-speaker
variability. Three utterances of sound $/a/$ spoken by a female, male
and child is used for this study. The same routine {\it
speakerVariation} is used for this study as shown below.\\[2ex]
$\gg$ speakerVariation('f\_s1\_t1\_a', 'm\_s2\_t1\_a', 'c\_s1\_t1\_a', 15001,
15480, 9001, 9480, 12481, 12960, 1);\\[2ex]
The linear prediction of the short-time signal of all the three
utterances is plotted in the same figure. Again observe that the first
two formant regions for the male and female speaker are almost same,
in case of child speech the second formant is very much shifted than
the first formant. Like in the previous experiment we observe that the
higher formant regions are different for different speakers even
though the same sound $/a/$ is being spoken.
\end{enumerate}

\section{SIFT Algorithm and Pitch Contour}
\label{exp9}
In this experiment, we extend the idea of pitch estimation using LP
residual (refer to Experiment \ref{lpres}) into a pitch estimation
algorithm.
The pitch frequency can be estimated through Simple Inverse Filter
Tracking (SIFT) algorithm. The SIFT
algorithm computes the pitch frequency for a given short-time speech
signal in the following way.
\begin{itemize}
	\item [Step 1] Low pass filter the short-time signal.
	\item [Step 2] Perform linear prediction analysis and obtain
	the linear prediction residual.
	\item [Step 3] Perform autocorrelation on the linear
	prediction residual.
	\item [Step 4] Find the location of second peak, make a decision on
	voicing. If it is voiced compute the pitch frequency else set
	pitch frequency to zero.
\end{itemize}
The pitch frequency contour for a spoken sentence can be computed by
taking a short-time window of size say 30 msec.
\begin{enumerate}

	\item Place this window at the begining of the speech signal
compute the pitch frequency using the SIFT algorithm.
	\item Shift the window by 10 msec on the speech signal and
compute the pitch frequency using SIFT algorithm.
	\item Repeat Step 2 until the end of the speech signal is
reached.
\end{enumerate}
The 10 msec shift is called as frame. So we obtain a pitch
frequency for every 10 msec or frame. The pitch contour is
nothing but the array of pitch frequencies obtained for the sequence of
frames. For applications like speech or speaker recognition for every
frame a feature parameter vector (e.g. linear prediction coefficients
$a_{k}$s') is obtained. In other words, the feature extraction stage
yields a sequence of feature parameter vectors $x_{1}, x_{2} \cdots
x_{N-1}, x_{N}$, where $N$ is the number of frames.\\[2ex]
\noindent
In this experiment, first, we are going to observe the pitch contour
for two different types of sentences, interrogative and declarative,
using routine {\it sift}. For this study, we will use 30 msec
frame size (480 samples), a shift of 10 msec (160 samples) and
linear prediction order of 10. The usage is as given below.\\[1ex]
The sentence spoken is an interrogative sentence, {\it Where are you
from?}\\[1ex]
$\gg$ sift('m\_s1\_i\_sen1', 480, 160, 10, 16000, 1, 'Pitch contour
of interogative sentence spoken by male speaker 1');\\[1ex]
\noindent
In the figure 1, you can observe the window moving across the signal
in the upper plot and in the lower plot the corresponding LP residual
signal. Once the estimation of the pitch contour is over, the speech
signal (upper plot) and the pitch contour (lower plot) are plotted in
figure 2. Observe the rise and fall of pitch contour across the
sentence. This rise and fall of pitch contour carries information like
speaking style, type of sentence, emotional status of speaker
etc. Observe the rise of pitch contour for the word {\it where} at the
begining of the sentence (in the
context of interogation). If a line is drawn interpolating
the peaks and valleys in the pitch contour, it
will have a positive slope. Observe at the end
again a fall and then rise of pitch contour.\\[1ex]
\noindent
The sentence spoken is a declarative sentence, {\it I am from
India.}\\[1ex]
$\gg$ sift('m\_s1\_d\_sen1', 480, 160, 10, 16000, 3, 'Pitch contour
of declarative sentence spoken by male speaker 1');\\[1ex]
\noindent
Again in the figure 3 you can observe the window moving across the signal
in the upper plot and in the lower plot the corresponding LP residual
signal. Once the estimation of the pitch contour is over, the speech
signal (upper plot) and the pitch contour (lower plot) are plotted in
figure 8. Observe the rise and fall of pitch contour across the
sentence. If a line is drawn interpolating the peaks and valleys in
the pitch contour, it will have a negative slope.\\[1ex]
\noindent
Note that the pitch frequency for a single frame is just an
information about the speaker. It doesnot convey any information
regarding the sentence being spoken or message or emotional status of
speaker. But when a large duration (say 100-300 msec) i.e. a sequence
of frames are considered then we can observe the rise and fall of pitch
contour and get the
informations related to it. Still pitch contour does not conveys any
information regarding the message being spoken. Now we will perform
the pitch contour analysis on the same sentences spoken by a different
speaker\\[1ex]
$\gg$ sift('m\_s2\_i\_sen1', 480, 160, 10, 16000, 5, 'Pitch contour
of interogative sentence spoken by male speaker 2');\\[1ex]
$\gg$ sift('m\_s2\_d\_sen1', 480, 160, 10, 16000, 7, 'Pitch contour
of declarative sentence spoken by male speaker 2');\\[1ex]
Compare the pitch contour in plots 2 and 6. They are
the pitch contour of the same interogative sentence spoken by two
different speakers. Are they different? Similary compare the plots 4
and 8 and put down your observations. Human use efficiently the
speaking style information which is embedded in the pitch contour to
recognize another person.\\[2ex]

\end{document}


\section{Unconstrained optimisation}


\section{Reading Rust}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
\hi{fn gcd}(mut n: u64, mut m: u64) \hi{->} u64 \{
    assert!(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(\hi{mut} n: u64, \hi{mut} m: u64) -> u64 \{
    assert!(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut \hi{n: u64}, mut \hi{m: u64}) -> u64 \{
    assert!(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut n: u64, mut m: u64) -> u64 \{
    \hi{assert!}(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut n: u64, mut m: u64) -> u64 \{
    assert!(n != \hi{0} && m != \hi{0});
    while m != \hi{0} \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut n: u64, mut m: u64) -> u64 \{
    assert!(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            \hi{let t = m};
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut n: u64, mut m: u64) -> u64 \{
    assert!(n != 0 && m != 0);
    \hi{while m != 0 \{}
        \hi{if m < n \{}
            let t = m;
            m = n;
            n = t;
        \hi{\}}
        m = m % n;
    \hi{\}}
    n
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Basic Syntax}
\begin{alltt}
fn gcd(mut n: u64, mut m: u64) -> u64 \{
    assert!(n != 0 && m != 0);
    while m != 0 \{
        if m < n \{
            let t = m;
            m = n;
            n = t;
        \}
        m = m % n;
    \}
    \hi{n}
\}
\end{alltt}
\end{frame}

%%%

\begin{frame}[fragile]{Structs}
\begin{minted}{rust}
pub struct Point {
  x: f64,
  y: f64,
}
\end{minted}
\pause
\begin{minted}{rust}
impl Point {
  /// static method ("constructor" per convention)
  pub fn new() -> Point {
    Point { x: 0.0, y: 0.0 }
  }
  pub fn distance_from_zero(&self) -> f64 {
    (self.x * self.x + self.y * self.y).sqrt()
  }
}
\end{minted}
\end{frame}

\begin{frame}[fragile]{Structs: Usage}
\begin{minted}{rust}
let p = Point::new();
assert_eq!(p.x, 0.0);
assert_eq!(p.y, 0.0);
\end{minted}
\begin{minted}{rust}
let p = Point { x: 3.0, y: 4.0 };
assert_eq!(p.x, 3.0);
assert_eq!(p.y, 4.0);
assert_eq!(p.distance_from_zero(), 5.0);
\end{minted}
\end{frame}

\begin{frame}[fragile]{Enumerations}
\begin{columns}[t,onlytextwidth]
\column{.35\textwidth}
\begin{minted}{rust}
pub enum Color {
  Red,
  Green,
  Blue,
}
\end{minted}
\column{.65\textwidth}
\begin{minted}{rust}
let color = Color::Red;
match color {
  Color::Red => println!("Got red"),
  Color::Green => println!("Got green"),
  Color::Blue => println!("Got blue"),
}
\end{minted}
\end{columns}
  \begin{itemize}
    \item<1-> In its simplest form similar to C/C++ (but type safe)
    \item<1-> Data that is one of several possible variants (sum type, discriminated union)
    \item<2> We can match (switch) on enums
    \item<2> We \emph{must} match all variants
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Enums with data}
\begin{columns}[t,onlytextwidth]
\column{.42\textwidth}
\begin{minted}{rust}
pub struct MyCircle {
  radius: f64,
}
pub struct MySquare {
  side: f64,
}
pub enum Shape {
  Circle(MyCircle),
  Square(MySquare),
}
\end{minted}
\column{.58\textwidth}
\pause\begin{minted}{rust}
let shape = Shape::Circle(MyCircle {
  radius: 1.0,
});
match shape {
  Shape::Circle(c) => {
    println!("Circle (r={})", c.radius);
  }
  _ => panic!("Must be a circle!"),
}
\end{minted}
\end{columns}
\begin{itemize}
  \item<1-> A \texttt{Shape} is Either a \texttt{Circle} containing
    \texttt{MyCircle} or a
    \texttt{Square} containing \texttt{MySquare}
  \item<2-> We can use match to "extract" data
\end{itemize}
\end{frame}

%%%

\begin{frame}[fragile]{Traits}
\begin{minted}{rust}
pub trait HasArea {
  fn area(&self) -> f64;
}
\end{minted}
\begin{itemize}
	\item Like interfaces
	\item Defines functionality a type must provide
\end{itemize}
\end{frame}

%%%

\begin{frame}[fragile]{Trait Implementation}
\begin{minted}{rust}
pub struct Circle {
  x: f64,
  y: f64,
  radius: f64,
}

impl HasArea for Circle {
  fn area(&self) -> f64 {
    std::f64::consts::PI * (self.radius * self.radius)
  }
}
\end{minted}
\end{frame}

\begin{frame}[fragile]{Trait Implementation}
\begin{minted}{rust}
pub struct Square {
  x: f64,
  y: f64,
  side: f64,
}

impl HasArea for Square {
  fn area(&self) -> f64 {
    self.side * self.side
  }
}
\end{minted}
\end{frame}

%%%

\begin{frame}[fragile]{Trait Based Generics}

\begin{minted}{rust}
pub fn print_area<S: HasArea>(shape: &S) {
  println!("{}", shape.area());
}
\end{minted}
\pause
\begin{minted}{rust}
...

// Shape is Circle
print_area(&Circle{x: 1.0, y: 2.0, radius: 3.0});
// Shape is Square
print_area(&Square{x: 4.0, y: 5.0, side: 5.0});
\end{minted}

\end{frame}

%%%

\begin{frame}[fragile]{Generic Types}
\begin{minted}{rust}
struct Coords<T> {
  x: T,
  y: T,
}
\end{minted}
\pause
\begin{minted}{rust}
...

Coords { x: 200, y: 800 }  // OK
Coords { x: 1.3, y: 4.7 }  // Also OK
Coords { x: 1.3, y: 100 }  // Not OK
\end{minted}
\end{frame}

%%%

\begin{frame}[fragile]{ Generic Enumerations}
\begin{minted}{rust}
enum Option<T> {
  Some(T),
  None
}
\end{minted}
\end{frame}

%%%

\begin{frame}[fragile]{Application of Option<T>}
\begin{minted}{rust}
fn safe_div(n: i32, d: i32) -> Option<i32> {
  if d == 0 {
    None
  } else {
    Some(n / d)
  }
}
\end{minted}
\end{frame}

%%%

\begin{frame}[fragile]{Matching an Option}
\begin{minted}{rust}
match safe_div(num, denom) {
  None => println!("No quotient."),
  Some(v) => println!("Quotient is {}.", v)
}

// ...or

if let Some(q) = safe_div(num, denom) {
  println!("Quotient is {}", q);
} else {
  println!("No quotient.");
}
\end{minted}
\end{frame}

%%%


If the resonant data sample can be divided based on the type of
parton that initiated the jets, the sensitivity of the search for some
resonances could be increased.
The fraction of events as a function of
the dijet mass from QCD  simulated with a  \textsc{Pythia~8.186}~\cite{pythia8} and the leading-order NNPDF2.3~\cite{Ball:2012cx}
parton distribution functions (PDFs) is shown in Fig.~\ref{fig:quarkgluonfraction}.
This suggests that tagging quark and gluon jets should be able to improve the sensitivity of searches for new particles.
ATLAS has published a study \cite{ATL-PHYS-PUB-2017-009} showing that jets can be tagged as quark or gluon jets
based on the number of charged particles with transverse momentum (\pt ) above 500\,MeV.
In this note we present the analysis for the whole combined 2015 and
2016 $\sqrt{s} = 13$~\TeV\xspace dataset using quark
and gluon tagging  based on charged-particle constituent multiplicity.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/Fractions_QCD.pdf}
\caption{The fraction of dijet events that are initiated by quark-quark events (blue), quark-gluon
events (green) and gluon-gluon events (red) in simulated data.  \label{fig:quarkgluonfraction}}
\end{figure}

It has been shown~\cite{ATL-PHYS-PUB-2017-009}  that the number of charged particles with transverse
momentum (\pT ) above 500\,MeV, \ntrk, associated with a jet can be used
to select samples which have an increased fraction of jets produced by
quarks (or gluons)
%Samples with enhanced fractions of quark or gluon
%initiated jets can be created by using a selection based on the \ntrk\
as shown in Fig.~\ref{fig:jet_pt_quark_gluon}.
Ref.~\cite{ATL-PHYS-PUB-2017-009} also shows that the \Pythia8
generator~\cite{pythia8} using the A14 tune~\cite{A14tune} is in a good
agreement with the distribution of \ntrk\ found in data.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/fig_01_ATL-PHYS-PUB-2017-009.pdf}
\caption{Distribution of the jet reconstructed track multiplicity (\ntrk ) in
 different pT ranges with the \Pythia~8 generator~\cite{pythia8} using
 the A14 tune~\cite{A14tune}, the NNPDF2.3 PDF
 set~\cite{Carrazza:2013axa}, and processes with a full simulation of the
 ATLAS detector. Jets must be fully within the tracking acceptance
 ($|\eta|<2.1$) and tracks are required to have $\pT > 500$\,MeV and pass
  quality criteria described in Ref.~\cite{ATL-PHYS-PUB-2017-009}. Figure
 from Ref.~\cite{ATL-PHYS-PUB-2017-009}. \label{fig:jet_pt_quark_gluon}}
\end{figure}

\subsection{Expected Signal Significance}
\label{sec:ExpectedSig}

The dijet mass spectrum has a complex background with rapidly changing  fractions of events that originate
from quark-quark, quark-gluon and gluon-gluon scattering. The expected signal significance has been investigated using
MC simulated  signals and background. The background is represented using the \QCD samples described
in Section~\ref{qcdsamps}.

\subsubsection{Signals that decay to quark-quark.}

The significance for signals that decay to a quark anti-quark pair are estimated using \Zprime\ models (Section~\ref{subsec:MCZprimeSamples}) using
\begin{equation}
S = N_S \sum_i{ \dfrac{ f_{{qq}_i}\epsilon_{qQ}^2 + f_{{qg}_i}\epsilon_{qQ}\epsilon_{gQ} + f_{{gg}_i}\epsilon_{gQ}^2  } {\sqrt{ B_{{qq}_i}\epsilon_{qQ}^2 + B_{{qg}_i}\epsilon_{qQ}\epsilon_{gQ} + B_{{gg}_i}\epsilon_{gQ}^2  }}}
\end{equation}
where $N_S$ is the number of signal events, $f_{{qq}_i}$ is fraction of signal events that result in the two
highest \pT\ jets that where initiated by quarks in bin $i$ ( $f_{{qg}_i}$ are quark-gluon jets, and $f_{{gg}_i}$ is two gluon jets),
$\epsilon_{qQ}$ is the efficiency of a quark initiated jet passing the quark selection criteria,
$\epsilon_{gQ}$ is the efficiency of a gluon initiated jet passing the quark selection criteria,
and $B_{{xx}_i}$ is the expected number of background events with quark-quark, quark-gluon or gluon-gluon initiated jets.

The significance is calculated for \Zprime's with masses ranging from 1500 to 4000\,\GeV and quark-jet selection
efficiencies ranging from 30 to 90\%. The resulting significances are  in Fig.~\ref{fig:QuarkSignalSignificance}
and show that the significance decreases if any quark-selection is applied to the data. This is because
the data is dominated by quark-quark events and the selection reduces background and signal in similar amounts.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/QuarkSignalSignificance.png}
\caption{ The significance for observing a \Zprime\ with masses from 1500 to 4000\,\GeV
for $\epsilon_{qQ}$ ranging from 90 to 30\% compared to the significance calculated with no quark selection applied. The key gives pairs of efficiencies ($\epsilon_{qQ}$, $\epsilon_{gQ}$).
  \label{fig:QuarkSignalSignificance}}
\end{figure}

\subsubsection{Signals that decay to gluon-gluon.}

The significance for signals that decay to a gluon-gluon pair are estimated using \Hprime\ models  using
\begin{equation}
S = N_S \sum_i{ \dfrac{ f_{{qq}_i}\epsilon_{qG}^2 + f_{{qg}_i}\epsilon_{qG}\epsilon_{gG} + f_{{gg}_i}\epsilon_{gG}^2  } {\sqrt{ B_{{qq}_i}\epsilon_{qG}^2 + B_{{qg}_i}\epsilon_{qG}\epsilon_{gG} + B_{{gg}_i}\epsilon_{gG}^2  }}}
\end{equation}
where
$\epsilon_{qG}$ is the efficiency of a quark initiated jet passing the gluon selection criteria,
$\epsilon_{gG}$ is the efficiency of a gluon initiated jet passing the gluon selection criteria.
The significance is calculated for \Hprime's with masses ranging from 2000 to 7000\,\GeV\ and quark-jet selection
efficiencies ranging from 60 to 90\%. The resulting significances are  in Fig.~\ref{fig:GluonSignalSignificance}
and show that the significance increases from approximately 1.2 at 2\,TeV\ to 1.6 at 7\,\TeV\ with the greatest
increases  occurring for gluon selection efficiency of 75\%.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/GluonSignalSignificance.png}
\caption{ The significance for observing a \Hprime\ with masses from 2000 to 7000\,\GeV\
for $\epsilon_{gG}$ ranging from 90 to 60\% compared to the significance calculated with no gluon selection applied. The key gives pairs of efficiencies ($\epsilon_{gG}$, $\epsilon_{qG}$).
  \label{fig:GluonSignalSignificance}}
\end{figure}

\subsubsection{Signals that decay to quark-gluon.}

The calculation of the significance for a quark-gluon signal such as an excited quark decay is much more complicated as
both jets can satisfy both the quark and the gluon selection.  In this case we need to define three efficiencies that are exclusive for quark and gluon jets. They are
\begin{itemize}
\item $\epsilon_{qQ}$ The probability that a quark initiated jet is identified only as a quark jet.
\item $\epsilon_{qQG}$ The probability that a quark initiated jet is identified  as a quark and a gluon jet.
\item $\epsilon_{qG}$ The probability that a quark initiated jet is identified only as a gluon jet.
\end{itemize}
where by construction $\epsilon_{qQ} + \epsilon_{qQG} + \epsilon_{qG} = 1$. A similar set of efficiencies are measured
for gluon initiated jets.
\begin{itemize}
\item $\epsilon_{gQ}$ The probability that a gluon initiated jet is identified only as a quark jet.
\item $\epsilon_{gQG}$ The probability that a gluon initiated jet is identified  as a quark and a gluon jet.
\item $\epsilon_{gG}$ The probability that a gluon initiated jet is identified only as a gluon jet.
\end{itemize}

The probability of truth quark-quark ($p_{qq}$), quark-gluon ($p_{qg}$) and gluon-gluon ($p_{gg}$) truth
events of passing the selection criteria are given by
\begin{align}
p_{qq} & = 2  \epsilon_{qQ}\epsilon_{qG} + \epsilon_{qQG}\left( \epsilon_{qQ} + \epsilon_{qG} \right)  + \epsilon_{qQG}\epsilon_{qQG} \\
p_{gg} & = 2  \epsilon_{gQ}\epsilon_{gG} + \epsilon_{gQG}\left( \epsilon_{gQ} + \epsilon_{gG} \right)  + \epsilon_{gQG}\epsilon_{gQG} \\
p_{qg} & = \epsilon_{qQ}\epsilon_{gG} + \epsilon_{gQ}\epsilon_{qG} + \epsilon_{qQG}\left( \epsilon_{gQ} + \epsilon_{gG} \right)
+ \epsilon_{gQG}\left( \epsilon_{qQ} + \epsilon_{qG} \right)
+ \epsilon_{gQG}\epsilon_{gQG}
\end{align}
and the significance is then given by
\begin{equation}
S = N_S \sum_i{ \dfrac{ f_{{qq}_i} p_{qq} + f_{{qg}_i}p_{qg} + f_{{gg}_i}p_{gg}  } {\sqrt{ B_{{qq}_i}p_{qq} + B_{{qg}_i}p_{qg} + B_{{gg}_i}p_{gg}  }}}.
\end{equation}

Exploring selection efficiencies from 30 to 100\% for both quarks and gluons shows that no benefit
is obtained by applying a quark selection. A small but significant improvement in significance is obtained
if one of the two jets is required to pass a gluon selection. The resulting significances are  in Fig.~\ref{fig:QuarkGluonSignalSignificance}
and show that the significance increases by about 25\% at high masses (above 5\,\TeV ) with the greatest
increases  occurring for gluon selection efficiency over 70\%.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/QuarkGluonSignalSignificance3.png}
\caption{ The significance for observing a \qstar\ with masses from 2000 to 7000\,\GeV\
for $\epsilon_{gG}$ ranging from 30 to 90\% compared to the significance calculated with no gluon selection applied. The key gives pairs of efficiencies ($\epsilon_{gG}$, $\epsilon_{qG}$).
  \label{fig:QuarkGluonSignalSignificance}}
\end{figure}

\clearpage

\subsection{Selection Criteria}

In Ref.~\cite{ATL-PHYS-PUB-2017-009}  the selection criteria for  an
enriched quark-initiated jet sample was chosen so that each \pT\ bin had
60\% quark-initiated purity. Applying this criteria to the high mass
dijet sample would lead to discontinuities in the mass spectrum that
would present difficulties to a resonance search.

Using a selection criteria that is a linear function of the \( \ln(\pT) \)
results in a smooth mass distribution and can be chosen to produce a
selection efficiency that is approximately uniform.
A jet is classed as being more likely to be quark-initiated if \ntrk is less than
the threshold \nq and a more likely to be gluon-initiated if \ntrk is
greater than the threshold \ngluon
\begin{align}
\ntrk & \le \nq \; \mbox{quark-initiated sample} %\label{eq:QGselect}  % uncomment if label used.
\\
\ntrk	  & \ge \ngluon \; \mbox{gluon-initiated sample} \nonumber
\end{align}
where
\begin{equation}
n_{\mathrm{q(g)}} = {c + m \ln(\pT)}  \label{eq:nqg2}
\end{equation}
where $m$ and $c$ are constants chosen to provide suitable subsamples.

The constants $m$ and $c$ are found by finding the value of \ntrk\
that corresponds to a given efficiency for truth quark and gluon jets in
\pT\ bins and fitting the results. For each \pT\ bin the number of tracks
closest to the chosen selection efficiency is found. Since this is an integer
number of tracks and does and does not correspond exactly to the selection efficiency
a correction is applied by estimating the fractional number of tracks that corresponds
to the selection  efficiency  by carrying out a linear interpolation between the efficiencies
for the selected bin and its nearest neighbour. The uncertainty on this value is then estimated using
binomial uncertainties.

The jet \pT\ bin edges are chosen to be
480, 500, 520, 540, 560, 580, 600, 625, 650, 700, 750, 800, 900, 1000, 1400,
1600, 1800, 2000, 2500, 3000, 3500, 4000, 5000, 6000\,\GeV. An example of the \ntrk cumulative
distribution for truth quark and gluon jets satisfying $800 < \pT < 900\,\GeV$ is shown in
Fig.~\ref{fig:ntrk_cumulative}.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/tagging/Cumulative_ntrk_distribution_12_800_900GeV.pdf}
\caption{The cumulative distribution of \ntrk\ for truth quark (blue) and gluon (red) initiated jets
satisfying $800 < \pT < 900\,\GeV$.  \label{fig:ntrk_cumulative}}
\end{figure}

The constants for Eq.~\ref{eq:nqg2} are found for quark and gluon selection efficiencies from
65\% to 95\% in 5\% steps. The plot of the value of \ntrk\ that satisfies the selection efficiencies
of 70, 75 and 80\% are shown in Fig.~\ref{fig:qg_selection_curves} along with the best fit using Eq.~\ref{eq:nqg2}.
The values of the constants for both quark and gluon selections are summarised in
Tables~\ref{table:truthQuarkSelectionEfficiencies} and \ref{table:truthGluonSelectionEfficiencies}.
The fit for a selection efficiency of 75\% has a $\chi^2$ of $33.5$ (quark-selection) and $2.6$
(gluon-selection) for 21 degrees of freedom.

The value of \ntrk\ that satisfies the selection efficiency plateaus above 4000\,\GeV\ indicating a possible saturation effect. As a cross check the data is fitted to an alternative fit function
\begin{equation}
n_{\mathrm{q(g)}} = {c + m \ln(\pT) + n \sqrt{\ln(\pT)}}. \label{eq:nqg3}
\end{equation}
This improves the $\chi^2$ of the fit for a selection efficiency of 75\% for quark-selection from
$33.5$ to $25.1$ and for gluon-selection from $2.6$ to $1.6$.  The improved fit is shown in Fig.~\ref{fig:qg_selection_curves2} and the values of the constants for both quark and gluon selections
are summarised in
Tables~\ref{table:truthQuarkSelectionEfficiencies2} and \ref{table:truthGluonSelectionEfficiencies2}.

\clearpage

\begin{table}[h]
	\centering
		\caption{ Values of constants $m$ and $c$ from Eq.~\ref{eq:nqg2} such that $ \ntrk  \le \nq $
		for truth quark jets for a range of efficiencies  from 65 to 95\%.
		\label{table:truthQuarkSelectionEfficiencies}
		}
	\begin{tabular}{SSSS}
	\toprule
\multicolumn{1}{c}{Truth-$q$ selection efficiency}   & \multicolumn{1}{c}{Truth-$g$ selection efficiency} &  \multicolumn{1}{c}{$c$}  &  \multicolumn{1}{c}{$m$} \\
\midrule
0.95 & 0.732 & -27.568 & 8.789 \\
0.90 & 0.563 & -21.518 & 7.269 \\
0.85 & 0.447 & -17.646 & 6.304 \\
0.80 & 0.350 & -14.956 & 5.610 \\
0.75 & 0.278 & -12.600 & 5.022 \\
0.70 & 0.221 & -10.691 & 4.536 \\
0.65 & 0.174 & -8.990 & 4.105 \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[h]
	\centering
		\caption{ Values of constants $m$ and $c$ from Eq.~\ref{eq:nqg2} such that $ \ntrk  \ge \ngluon $
		for truth quark jets for a range of efficiencies  from 65 to 95\%.
		\label{table:truthGluonSelectionEfficiencies}
		}
	\begin{tabular}{SSSS}
	\toprule
\multicolumn{1}{c}{Truth-$g$ selection efficiency}   & \multicolumn{1}{c}{Truth-$q$ selection efficiency} &  \multicolumn{1}{c}{$c$}  &  \multicolumn{1}{c}{$m$} \\
\midrule
 0.95 & 0.586 & -7.541 & 3.233 \\
0.90 & 0.456 & -8.980 & 3.779 \\
0.85 & 0.377 & -10.419 & 4.230 \\
0.80 & 0.320 & -11.964 & 4.659 \\
0.75 & 0.274 & -13.376 & 5.047 \\
0.70 & 0.234 & -14.937 & 5.446 \\
0.65 & 0.202 & -16.466 & 5.834 \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[h]
	\centering
		\caption{ Values of constants $m$ and $c$ from Eq.~\ref{eq:nqg3} such that $ \ntrk  \le \nq $
		for truth quark jets for a range of efficiencies  from 70 to 80\%.
		\label{table:truthQuarkSelectionEfficiencies2}
		}
	\begin{tabular}{SSSSS}
	\toprule
\multicolumn{1}{c}{Truth-$q$ selection efficiency}   & \multicolumn{1}{c}{Truth-$g$ selection efficiency} &  \multicolumn{1}{c}{$c$}  &  \multicolumn{1}{c}{$m$} &  \multicolumn{1}{c}{$n$} \\
\midrule
0.80 & 0.350 & -139.822 & -11.714 & 93.100 \\
0.75 & 0.278 & -128.174 & -11.001 & 86.141 \\
0.70 & 0.221 & -128.255 & -11.755 & 87.604 \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[h]
	\centering
		\caption{ Values of constants $m$ and $c$ from Eq.~\ref{eq:nqg3} such that $ \ntrk  \ge \ngluon $
		for truth quark jets for a range of efficiencies  from 70 to 80\%.
		\label{table:truthGluonSelectionEfficiencies2}
		}
	\begin{tabular}{SSSSS}
	\toprule
\multicolumn{1}{c}{Truth-$g$ selection efficiency}   & \multicolumn{1}{c}{Truth-$q$ selection efficiency} &  \multicolumn{1}{c}{$c$}  &  \multicolumn{1}{c}{$m$} &  \multicolumn{1}{c}{$n$}  \\
\midrule
0.80 & 0.320 & -99.796 & -7.839 & 66.301 \\
0.75 & 0.274 & -99.949 & -7.271 & 65.347 \\
0.70 & 0.234 & -99.774 & -6.640 & 64.077 \\
\bottomrule
\end{tabular}
\end{table}

\begin{figure}[p]
 \centering
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_selection_errors_6}}
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_selection_errors_5}}
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_selection_errors_4}}

\caption{ The values of \ntrk\ for (a) 70\%,  (b) 75\%  and (c) 80\% quark (blue) and gluon (red)
selection efficiencies in each \pT\ bin along with the best fit to Eq.~\ref{eq:nqg2}.
 \label{fig:qg_selection_curves2}}
\end{figure}

\begin{figure}[p]
 \centering
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_newFit_selection_errors_6}}
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_newFit_selection_errors_5}}
  \subfigure[] {\includegraphics[width=0.60\textwidth]{figures/tagging/quark_frac_newFit_selection_errors_4}}

\caption{ The values of \ntrk\ for  (a) 70\%,  (b) 75\%  and (c) 80\% quark  quark (blue) and gluon (red)
selection efficiencies in each \pT\ bin along with the best fit to Eq.~\ref{eq:nqg3}.
 \label{fig:qg_selection_curves}}
\end{figure}

\subsubsection{Signal Selection Efficiencies}

The selection criteria for a single jet gluon selection efficiency of 75\% is applied to the \Hprime\
sample described in in section~\ref{sec:hprime} are given in Table~\ref{table:HprimeselctionEfficiency}.
If the selection works perfectly the expected selection efficiency for the \Hprime\ sample would be 56.3\% ($0.75^2$).
The actual selection efficiencies range between 51.9\% for a 2\,\TeV\ signal to 57.4\% for a 7\,\TeV\ signal.

The actual fraction of \Hprime\ events that decay to two gluons is less than 100\% due to gluon splitting and other showering
effects and ranges from 91.3 to 95.4\%. The variation between the expected efficiency (56.3\% of the truth efficiency)
is plotted in Fig.~\ref{fig:HPrime_efficiency_difference}. The average difference for is approximately 3.3\% for both selection criteria.

There is negligible difference between the two selection criteria so the simpler choice as given in Eq.~\ref{eq:nqg2} will be used.

\begin{table}[h]
	\centering
		\caption{ The signal selection efficiency for a fully simulated \Hprime\ decaying to two gluons with requiring two jets to
		pass the 75\% single jet criteria given in Eq.~\ref{eq:nqg2} with constants from
		Table~\ref{table:truthGluonSelectionEfficiencies} and the criteria given in Eq.~\ref{eq:nqg3} with constants from
		Table~\ref{table:truthGluonSelectionEfficiencies2}.
		The expected double tagged gluon efficiency is 56.3\%.
		\label{table:HprimeselctionEfficiency}
		}
	\begin{tabular}{SSSS}
	\toprule
\multicolumn{1}{c}{\Hprime\ Mass (\GeV)}   & \multicolumn{3}{c}{Selection efficiency(\%)} \\
\multicolumn{1}{c}{} & \multicolumn{1}{c}{Eq.~\ref{eq:nqg2}} & \multicolumn{1}{c}{Eq.~\ref{eq:nqg3} ($\sqrt{}$ term)}
& \multicolumn{1}{c}{Truth} \\
\midrule
2000	&	51.9 & 51.8& 91.3 \\
2500	&	53.2 & 53.0& 91.7 \\
3000 	&	54.9 & 54.6& 92.3 \\
3500	&	55.3 & 55.1& 93.4 \\
4000	&	56.4 & 56.2& 93.4 \\
4500	&	56.7 & 56.7& 94.1 \\
5000	&	56.2 & 56.4& 94.3 \\
5500	&	57.2 & 57.5& 94.9 \\
6000	&	57.4 & 57.8& 95.1 \\
6500	&	57.4 & 58.3& 95.5 \\
7000	&	57.4 & 58.1& 95.4 \\\bottomrule
\end{tabular}
\end{table}

\begin{figure}[p]
 \centering
 {\includegraphics[width=0.60\textwidth]{figures/tagging/HPrime_Efficiency_Difference}}

\caption{ The difference between the expected signal selection efficiency of 56.3\%
for a single ject selection efficiency of 75\% for \Hprime\ using  Eq.~\ref{eq:nqg2} (Blue) and
Eq.~\ref{eq:nqg3} (Green)
 \label{fig:HPrime_efficiency_difference}}
\end{figure}

\clearpage