\documentclass[../Notes/main.tex]{subfiles}

\begin{document}
\section{Graphs}

\subsection{Graph Traversal}

\subsubsection{Breadth First Search}
\lstinputlisting[firstline=2]{bfs/bfs.cpp}

\subsubsection{Recursive Depth First Search}
\lstinputlisting[firstline=2]{dfs/dfsRecursive.cpp}

\subsubsection{Iterative Depth First Search}
\lstinputlisting[firstline=2]{dfs/dfsIterative.cpp}

%\subsection{Topological Sort}
%\lstinputlisting[firstline=2]{topoSort/topoSort.cpp}

\subsection{Shortest Path Algorithms}

\subsubsection{Dijsktra}
All edges have non-negative values
\lstinputlisting[firstline=2]{dijsktra/dijsktra.cpp}

\subsubsection{Bellman Ford}
Edges can be negative, and it detects negative cycles
\lstinputlisting[firstline=2]{bellmanFord/bellmanFord.cpp}

\subsubsection{Floyd Warshall}
Shortest path from every node to every other node
\lstinputlisting[firstline=2]{floydWarshall/floydWarshall.cpp}

\subsection{Minimum Spanning Tree (MST)}

\subsubsection{Kruskal}
\lstinputlisting[firstline=2]{kruskal/kruskal.cpp}

% \subsubsection{Prim}
% \lstinputlisting[firstline=2]{prim/prim.cpp}

\subsection{Lowest Common Ancestor (LCA)}
Supports multiple trees
\lstinputlisting[firstline=2]{lca/lca.cpp}

% \subsection{Strongly Connected Components}

% \subsubsection{Tarjan}
% \lstinpuntlisting[firstline=2]{tarjan/tarjan.cpp}

% \subsubsection{Korasaju}
% \lstinpuntlisting[firstline=2]{korasaju/korasaju.cpp

\subsection{Max Flow}
\lstinputlisting[firstline=2]{dinic/dinic.cpp}

% \subsection{Heavy Light Decomposition}
% \lstinputlisting[firstline=2]{heavyLight/heavyLight.cpp}

\subsection{Others}
\subsubsection{Diameter of a tree}
\lstinputlisting[firstline=2]{treeDiameter/treeDiameter.cpp}
\end{document}


\documentclass[12pt]{article}

\usepackage{multicol}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, mathtools}  % mathtools required for box around equations
\usepackage{graphicx,float}
\usepackage[font={small}]{caption}

\title{Competitive Programming Team: AB IdeaLab\\ at Fall Activities Fair}
\author{Sanjit Bhat \and Alexander Sun}
\date{September 13, 2018}

\begin{document}

\maketitle

\section{Description}
Hello prospective IdeaLab \textbf{Competitive Programming Team} (CPT) member!

This year is AB's first year competing in the \textbf{American Computer Science League} (ACSL) competition!
As you will see with the three samples on the back page, the
problems are an interesting mix between Math and Computer Science and cover topics such as:

\begin{itemize}
\item Recursion (see Figure~\ref{fig:recursion} and Problems~\ref{prob:easy} and~\ref{prob:hard})
\item Graph Theory (see Figure~\ref{fig:graph_theory})
\item Computer Number Systems (see Problem~\ref{prob:medium})
\item Data Structures
\end{itemize}

If you would like to collaborate with others, learn how to solve these and similar problems,
and enter the world of Competitive Programming, come to IdeaLab's first meeting,
\textbf{September 21, 2018 in the SYSCO lab\footnote{Location: Upper West, near math department center} at 3:00 PM}.

The only pre-requisites are a \textbf{love for solving problems and an open mind}.
We value these \textbf{more} than prior experience coding
(as we'll be teaching basic coding skills on an ad-hoc basis).

\textbf{Represent AB, join an inclusive and caring team, and learn a ton in our first year competing in ACSL!}

\begin{multicols}{2}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.40\textwidth]{figures/fibonacci-recursion-tree.png}
  \caption{A tree representing the recursive calls made to the Fibonacci function.}
  \label{fig:recursion}
\end{figure}

%second column
\begin{figure}[H]
  \centering
  \includegraphics[width=0.20\textwidth]{figures/graph-theory.png}
  \caption{A graph theoretic representation of nodes, directed edges, and weights.}
  \label{fig:graph_theory}
\end{figure}
\end{multicols}

\section{Easy Problem}
\label{prob:easy}
Find $f(-1)$:
\begin{equation*}
f(x) =
\begin{cases}
x - f(x+1) & \text{if $x < 3$}\\
f(2x) & \text{if $3 \leq x < 5$}\\
x + 1 & otherwise
\end{cases}
\end{equation*}
\vspace{2cm}

\hfill
    \begin{tabular}[b]{|c|}
        \hline
        Your\\
        Answer:\\
        \\
        \hline
    \end{tabular}
\hfill

\section{Medium Problem}
\label{prob:medium}
Let $n$ be any positive base 10 integer from 1 to $2^{12}$ inclusive.
Let $S(n)$ be the number of 1's in the binary representation of $n$.
Find the number of possible $n$'s such that $S(n) - S(n + 1) = 3$.
\vspace{2cm}

\hfill
    \begin{tabular}[b]{|c|}
        \hline
        Your\\
        Answer:\\
        \\
        \hline
    \end{tabular}
\hfill

\section{Hard Problem}
\label{prob:hard}
Ackerman's function is defined as:

\begin{equation*}
A(x, y) =
\begin{cases}
y + 1 & \text{if $x = 0$}\\
A(x - 1, 1) & \text{if $x \neq 0$ and $y = 0$}\\
A(x - 1, A(x, y - 1)) & \text{if $x \neq 0$ and $y \neq 0$}
\end{cases}
\end{equation*}
Find $A(3, 4)$.
\vspace{2cm}

\hfill
    \begin{tabular}[b]{|c|}
        \hline
        Your\\
        Answer:\\
        \\
        \hline
    \end{tabular}
\hfill
\end{document}


\subsection{Major blocks}

The flow of control begins at the bottom left of \autoref{fig:blockdiag}, with a
mic providing audio input, which is captured by MATLAB (above) and converted to
an array of integers. In ideal conditions and availability of modules, this
would be done on the arduino, with little to no performance detriment. The time
between serial inputs (based on the baud rate) is utilised to process the
incoming data into the format used through the program, which is as in the
Doobit storage system.

\begin{figure}[ht]
    \centering
    \includegraphics[height=0.4\textheight]{fig/blockdiag.pdf}
    \caption{Block diagram and work distribution.}
    \label{fig:blockdiag}
\end{figure}

After the signal has been completely read, a Dirac comb (in frequency space) is
generated from a preset range of frequencies. Ideally, a Fourier transform would
be utilized for this, but that method carries far too much resolution for just
identifying certain frequencies. A limitation that still preserves a huge space
of applications. Identification of a combination of frequencies (a trivial
extension of the current algorithm, a simple check at the end, or even better if
specific, a smaller starting set) could allow for identifying more complicated
speech patterns and more processing on subsets of the input (would be stretching
the limits of the arduino) could help in preliminary speech recognition,
possibly as a low power first gate in an always-on speech recognition system.
For example, a lower level circuit that listening for \emph{"Alexa"} and
activates a more power hungry and performant circuit for speech recognition.

To avoid processing all the frequencies, we process the entire comb at once, and
if it passes a certain threshold, we infer that one or more of the frequencies
in the comb is present in the sample. Proceeding, the comb is split halfway in
frequency space, processed depth first, to identify the frequencies present,
rejecting the entire set at any point the global maxima of its cross correlation
drops below a given threshold.

A possible optimization, abandoned in the interest of time, is to compute the
cross correlation at only a few points, utilizing optimization techniques
instead of brute forcing to find the maximum.


\documentclass[10pt]{article}

\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{stmaryrd}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows.meta, positioning}

\tikzstyle{block} = [draw, fill=white, rectangle,
    minimum height=3em, minimum width=2em]
\tikzstyle{sum} = [draw, fill=white, circle, node distance=1cm]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\B}{\mathbb{B}}
\newcommand{\stream}[1]{\ensuremath{\mathcal{S}_{#1}}}
\newcommand{\zm}{\ensuremath{z^{-1}}}
\newtheorem{theorem}{Theorem}[section]
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\I}{\mathcal{I}}
\newcommand{\D}{\mathcal{D}}
\newcommand{\distinct}{\mathit{distinct}}

\newcommand{\multiset}[1]{\mathit{multiset}_{#1}}
\newcommand{\set}[1]{\mathit{set}_{#1}}

\title{A model of DDlog}

\begin{document}
\maketitle

\section{Basic notation}

\subsection{Types}

$\N$ is the set of natural numbers, while $\Z$ is the set of integers.

Our language will deal with a set of base types $B$ that includes
$\N$, $\Z$, \code{string}, $\B$ (Booleans), $\mathbf{unit}$.

Derived types include: tuples, vectors (lists), functions.

$$T = B \;|\; T \times T \;|\; T^* \;|\; T \rightarrow T$$

%% We also highlight two special derived types: $\multiset{T}$: finite
%% multisets of elements of type $T$, and $\set{T}$: sets of elements of
%% type $T$.  We call these two particular types \emph{collections}.

A typing context is a map that assigns types to variable names.
$\Gamma = x_1 : T_1, x_2 : T_2, \ldots, x_m : T_m$, where $T_i$ is the
type of variable $x_i$.

\subsection{Multisets}

A monoid $(M, 0_M, +_M)$ is a set $M$ with a distinguished element
$0_M \in M$ and a binary associative operation $+_M : M \times M
\rightarrow M$, such that $a +_M 0 = 0 +_M a = a$ for any $a \in M$.
When clear from the context we drop the index $_M$.  A commutative
monoid has the property that $a + b = b + a$ for all $a, b \in M$.  A
group is a monoid where every element $a \in M$ has a complement $-a
\in M$ such that $a + (-a) = 0$.

Given a monoid (group) $B$, the set of functions between $A$ and $B$
form a monoid (group): M = $A \rightarrow B$, defined by $0_M = (x
\mapsto 0_B)$ and $f +_M g = x \mapsto f(x) +_B g(x)$.  We denote the
\emph{finite} functions between $A$ and $B$ as $B[A]$; these are
functions where $f(x) \not= 0_B$ for at most a finite number of values
of $x \in A$.  Such a function can be denoted by enumerating the
inputs that map to non-zero values: $\{ x_1 \mapsto a_1, \dots, x_n
\mapsto a_n \}$.  The values in $B[A]$ can also be thought as being
key-value maps with keys in $A$ and values in $B$.

In particular, \emph{multisets} with elements of type $A$ are the
functions $\Z[A]$.  Multiset union is just addition in the $\Z[A]$
monoid.  Note that multisets elements can have negative weights.
Multisets form a commutative group, since $\Z$ with addition is a
group.  The zero is the empty multiset $\{\} = \phi$.  Given a
multiset $m \in \Z[A]$ and a value $v \in A$, we overload the array
index notation $m[v]$ to denote the \emph{weight} of the element $v$.

The ``distinct'' function $\distinct: \Z[D] \rightarrow \Z[D]$ projects
a multiset into an underlying set, but \emph{represents the result
  still as a multiset}.  The definition of distinct is given by
$\distinct(m) = \{ x \mapsto 1, m[x] > 0 \} \in \Z[D]$.

\subsection{Streams}

Given a monoid $T$, streams $\stream{T}$ of values of type $T$ are
functions $\N \rightarrow T$.  If $S \in \stream{T}$ is a stream of
values of type $T$, we use the notation $S[t] = S(t) \in T$ is a is
the value of the element of the stream at time $t \in \N$.

The $\zm: \stream{T} \rightarrow \stream{T}$ \emph{delay operator} is
a function from streams to streams, defined as $(\zm(s))[0] = 0_T$,
and $(\zm(S))[t] = S[t - 1]$ for all $t > 0$.

Given a function $f: A \times B \rightarrow C$ one can naturally
extend it to operate on streams $f: \stream{A} \times \stream{B}
\rightarrow \stream{C}$ operating pointwise on time: $f(X, Y)[t] =
f(X[t], Y[t])$ for $t \in \N$.  (This applies to unary functions as
well, which can be extended to operate over a stream pointwise.)

In the rest of this text we only consider streams over multisets: at
each time moment the value of the stream is a multiset.  Since
multisets form a group, addition and subtraction on streams over
multisets of the same type are well-defined.

Given a monoid $M$, integration is an operation from streams to
streams, $\I : \stream{M} \rightarrow \stream{M}$.  The integral of a
stream $S$ is defined using an equation: $\I(S) = \zm(\I(S)) + S$.
Using the notation used for digital filters this equation can be also
written as $\I(S) = S / (1 - \zm)$.  The element of $\I(S)$ at time
$t$ is the (prefix) sum of all elements of $S$ at times up to $t$:
$\I(S)[t] = \sum_{i \leq t} S[t]$, where the sum uses the addition of
the underlying monoid $M$.

Given a group $M$, differentiation is an operation from streams to
streams $\D : \stream{M} \rightarrow \stream{M}$, defined as $\D(S) =
S - \zm(S)$ (we need $M$ to be group in order to have subtraction).
An element of $\D(S)$ (at time $t$) is the difference between the
previous element (time $t-1$) of $S$ and the current (time $t$)
element of $S$.

\begin{theorem}
For a stream $S$ over a commutative group we have $D(I(S)) = S$.
\end{theorem}

\begin{proof}
$\D(\I(S)) = \I(S) - \zm(\I(S)) = (\zm(\I(S)) + S) - \zm(\I(S)) = S$, by
  substituting the definitions of $\D$ and $\I$ and using the
  commutativity of the group.
\end{proof}

\section{DDlog}

\subsection{DDlog types}

\[\frac{\code{A}: T, \code{B}: S}{\code{(A,B)}: T \times S}\]
\[\frac{\code{A}: T, \code{B}: S}{\code{function(x:A):B}: A \rightarrow B}\]
\[\frac{\code{T1}: T_1, \code{T2}: T_2}{\code{T1 | T2} : T_1 \oplus T_2}\]
\[\frac{\code{T1}: T_1, \code{T2}: T_2}{\code{Constructor\{f1: T1, f2:
    T2\}} : \string \rightarrow T_1 \oplus T_2}\]

\subsection{DDlog collections}

DDlog programs consume and produce streams of data.  The outside world
inputs/outputs streams feed to/from DDlog collections.  A DDlog
transaction defines a time step for all the input streams.  On each
transaction commit DDlog updates all output streams.  DDlog programs
only operate on streams.  Some input and output streams are fed
directly to DDlog programs, while others are processed, as described
in this section.

\subsubsection{\code{input stream IS[D]}}

\begin{tikzpicture}[auto, node distance=4cm,>=latex]
    \node [input, name=input] {};
    \node [block, right of=input] (block) {};
    \node [block, shape=ellipse, right of=block] (ddlog) {DDlog};
    \draw [draw,->] (input) -- node {$IS$} (block) node [below,pos=0.5] {input stream};
    \draw [->] (block) -- node {$\code{IS}$}(ddlog) node [below,pos=0.5] {internal stream};
\end{tikzpicture}

A DDlog declaration of \code{input stream IS[D]} declares a (DDlog
internal) stream $\code{IS} \in \stream{\Z[\code{D}]}$, where the
stream's elements are multisets with elements of type \code{D}.  Given
a stream of input values $IS \in \stream{\Z[D]}$, the contents of the
DDlog stream \code{IS} is exactly $IS$.

\subsubsection{\code{output stream OS[D]}}

\begin{tikzpicture}[auto, node distance=4cm,>=latex]
    \node [block, shape=ellipse] (ddlog) {DDlog};
    \node [block, right of=ddlog] (block) {};
    \node [output, right of=block] (output) {};
    \draw [draw,->] (ddlog) -- node {\code{OS}} (block) node [below,pos=0.5] {internal stream};
    \draw [->] (block) -- node {$OS$}(output) node [below,pos=0.5] {output stream};
\end{tikzpicture}

As shown in the previous diagram, a DDlog declaration of \code{output
  stream OS[D]} declares an (output) stream $OS \in
\stream{\Z[\code{D}]}$.  The contents of the output stream $OS$ is
exactly the contents of the stream \code{OS}.

\subsubsection{\code{input multiset IM[D]}}

\begin{tikzpicture}[auto, node distance=4cm,>=latex]
    \node [input, name=input] {};
    \node [block, right of=input] (block) {$\I(IM)$};
    \node [block, shape=ellipse, right of=block] (ddlog) {DDlog};
    \draw [draw,->] (input) -- node {$IM$} (block) node [below,pos=0.5] {input stream};
    \draw [->] (block) -- node {$\code{IM}$}(ddlog) node [below,pos=0.5] {internal stream};
\end{tikzpicture}

A DDlog declaration of \code{input multiset IM[D]} declares a stream
$\code{IM} \in \stream{\Z[\code{D}]}$.  Given a stream of input values
$IM \in \stream{\Z[\code{D}]}$, the contents of \code{IM} is given by
$\code{IM} = \I(MI)$, the integral of the stream of inputs.

\subsubsection{\code{output multiset OM[D]}}

\begin{tikzpicture}[auto, node distance=4cm,>=latex]
    \node [block, shape=ellipse] (ddlog) {DDlog};
    \node [block, right of=ddlog] (block) {$\D(\code{OM})$};
    \node [output, right of=block] (output) {};
    \draw [draw,->] (ddlog) -- node {\code{OM}} (block) node [below,pos=0.5] {internal stream};
    \draw [->] (block) -- node {$OM$}(output) node [below,pos=0.5] {output stream};
\end{tikzpicture}

A DDlog declaration of \code{output multiset OM[D]} declares a stream
$OM \in \stream{\Z[\code{D}]}$.  The contents of the (output) stream
$OM$ is given by $OM = \D(\code{OM})$, the derivative of the stream
\code{OM}.

\subsubsection{\code{input relation IR[D]}}

\begin{tikzpicture}[auto, node distance=4.5cm,>=latex]
    \node [input, name=input] {};
    \node [block, right of=input] (block) {$\distinct(\I(IR))$};
    \node [block, shape=ellipse, right of=block] (ddlog) {DDlog};
    \draw [draw,->] (input) -- node {$IR$} (block) node [below,pos=0.5] {input stream};
    \draw [->] (block) -- node {$\code{IR}$}(ddlog) node [below,pos=0.5] {internal stream};
\end{tikzpicture}

A DDlog declaration of \code{input relation IR[D]} declares a stream
$\code{IR} \in \stream{\Z[\code{D}]}$.  The elements of the stream
\code{IR} are defined by $\code{IR} = \distinct(\I(IR))$.

\subsubsection{\code{output relation OR[D]}}

\begin{tikzpicture}[auto, node distance=4.5cm,>=latex]
    \node [block, shape=ellipse] (ddlog) {DDlog};
    \node [block, right of=ddlog] (block) {$\D(\distinct(\code{OR}))$};
    \node [output, right of=block] (output) {};
    \draw [draw,->] (ddlog) -- node {\code{OR}} (block) node [below,pos=0.5] {internal stream};
    \draw [->] (block) -- node {$OR$}(output) node [below,pos=0.5] {output stream};
\end{tikzpicture}

A DDlog declaration of \code{output relation OR[D]} declares a stream
$OR \in \stream{\Z[\code{D}]}$.  The elements of the stream are $OR$ are defined
by $OR = \D(\distinct(\code{OR}))$.

\subsection{DDlog operators}

All DDlog operators: join, group-by, aggregate, projection, etc., are
just particular instances of functions over multisets.  When computing
over streams DDlog just applies these functions to the streams
pointwise.  In this section we describe the semantics of these
operators over a multi-set; the semantics over streams is just lifted
pointwise.

\subsection{DDlog syntax}

A Datalog rule is a term of the form:

\begin{eqnarray*}
  rule &::=& atom \code{ :- } \mathit{RHSClauses}. \\
  atom &::=& C[X] \mbox{ where $C$ is a collection and $X$ is a tuple
    of names} \\
  \mathit{RHSClauses} &::=& \mathit{RHSClause} \\
  &|& \mathit{RHSClause}, \mathit{RHSClauses} \\
  \mathit{RHSClause} &::=& \code{not } atom \\
  &|& expr   \\
  &|& \code{var } name = expr \\
  &|& \code{var } name = \code{FlatMap}( expr ) \\
  &|& \code{var } name = expr \code{.groupby}( expr ) \\
\end{eqnarray*}

\subsection{Typing rules}

\code{X}: $T$

\subsubsection{Multiset semantics}

We use $\llbracket t \rrbracket$ to denote the semantics of a term
$t$.  The semantics of a $\mathit{RHSClauses}$ term is a mapping of
all live variables to a multisets of tuples values.  In a typing
environment $\Gamma = x_1 : T_1, x_2 : T_2, \ldots, x_m : T_m$, the
semantics of a \code{RHSClauses}

\end{document}


\documentclass[12pt]{article}
\usepackage{pmmeta}
\pmcanonicalname{LeastCommonMultiple}
\pmcreated{2015-05-06 19:07:25}
\pmmodified{2020-02-09}
\pmowner{pahio}{2872}
\pmmodifier{pahio}{2872}
\pmformalizer{ngonga}{}
\pmtitle{least common multiple}
\pmrecord{32}{35723}
\pmprivacy{1}
\pmauthor{pahio}{2872}
\pmtype{Definition}
\pmcomment{trigger rebuild}
\pmclassification{msc}{11-00}
\pmsynonym{least common dividend}{LeastCommonMultiple}
\pmsynonym{lcm}{LeastCommonMultiple}
\pmrelated{Divisibility}
\pmrelated{PruferRing}
\pmrelated{SumOfIdeals}
\pmrelated{IdealOfElementsWithFiniteOrder}

\pmdefines{divides}
\pmdefines{multiple}
\pmdefines{common multiple}
\pmdefines{least common multiple}

\endmetadata

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{cnl}
\usepackage{xcolor}

\DeclareMathOperator{\lcm}{lcm}

% TITLE AUTHOR DATE
\title{Number Theory,\\ LeastCommonMultiple}
\date{February 09, 2020}
\author{Ngo Nga (ngongatlu)}

%- DOCUMENT

\begin{document}
\parskip=\baselineskip

% Local Defs
\def\natdiv#1#2{{#1}\mathrel{|}{#2}}
\parindent=0pt

\begin{cnl}

\Cnlinput{../TeX2CNL/package/cnlinit}

\bigskip

%-% BEGIN

\lsection{LeastCommonMultiple}
In this section, let $m,\ d$ be integers.

\deflabel{divides}
Assume that $m, \d$ are integers. Then we say that $d$ \df{divides} $m$ iff $d\ne 0$ and there
exists an integer $r$ such that $m= d\*r$.
\end{definition}

\dfn{
We write $\natdiv{d}{m}$ iff $d$ divides $m$.
}

\dfn{
We say that $m$ is a \df{mutiple of} $d$ iff $d$ divides $m$.
}

\dfn{
We say that $d$ is \df{positive} iff $d > 0$.
}

\dfn{
Assume that $f,\ p,\ q$ are integers. Then we say $f$ is a \df{common\~multiple} of $p$ and $q$
iff $p$ divides $f$ and $q$ divides $f$.}

\begin{remark}
We now introduce the least common multiple:
\end{remark}

\dfn{Assume that $p,\ q, \ f$ are positive integers. Then we say that $f$ is a
\df{least\~common\~multiple} of $p$ and $q$  iff $f$ is a positive common\~multiple of $p$ and
 $q$ such that for every common\~multiple $r$ of $p$ and $q$ we have $\natdiv{f}{r}$.}

\dfn{ Assume that  $p,\ q$ are positive integers. Let $\h{\df{lcm}}\ p\ q$ denote the
least\~common\~multiple of $p$ and $q$. This exists and is unique.}

\begin{demark}
\textbf{Note:} \, The definition can be generalized for several
numbers. \,The positive {lcm of positive integers is
uniquely determined. (Its negative satisfies the same two
conditions.)

\subsection*{Properties}

\begin{enumerate}
  \item If\, $a = \prod_{i=1}^{m}p_i^{\alpha_i}$\, and\,
  $b = \prod_{i=1}^{m}p_i^{\beta_i}$\, are the prime factor
  \PMlinkescapetext{presentations} of the positive integers $a$ and $b$ ($\alpha_{i} \geqq 0$, \,$\beta_{i} \geqq 0$ \,$\forall i$), then
        $$\mathrm{lcm}\!(a,\,b)=
        \prod_{i=1}^{m}p_i^{\max\{\alpha_i,\,\beta_i\}}.$$
This can be generalized for lcm of several numbers.
  \item  Because the greatest common divisor has the expression\,
  $\gcd(a,\,b) = \prod_{i=1}^{m}p_i^{\min\{\alpha_i,\,\beta_i\}}$, we see that
  $$\gcd(a,\,b)\cdot \mathrm{lcm}\!(a,\,b) = ab.$$
This formula is sensible only for two integers; it can not be
generalized for several numbers, i.e., for example,
       $$\gcd(a,\,b,\,c)\cdot \mathrm{lcm}(a,\,b,\,c) \neq abc.$$
  \item The preceding formula may be presented in
  \PMlinkescapetext{terms} of ideals of $\mathbb{Z}$; we may
  replace the integers with the corresponding principal ideals.\,
  The formula acquires the form
      $$((a)+(b))((a)\cap(b)) = (a)(b).$$
  \item The recent formula is valid also for other than principal ideals and even in so general systems as the Pr\"ufer rings; in fact, it could be taken as defining property of these rings: \, Let $R$ be a commutative ring with non-zero unity. \,$R$ is a Pr\"ufer ring iff {\em Jensen's formula}
 $$(\mathfrak{a}+\mathfrak{b})(\mathfrak{a}\cap\mathfrak{b}) = \mathfrak{ab}$$
is true for all ideals $\mathfrak{a}$ and $\mathfrak{b}$ of $R$, with at least one of them having \PMlinkname{non-zero-divisors}{ZeroDivisor}.
\end{enumerate}

\end{demark}

\begin{thebibliography}{9}
\bibitem{Larsen & McCarthy} M. Larsen and P. McCarthy: {\em Multiplicative theory of ideals}. Academic Press. New York (1971).
\end{thebibliography}
%%%%%
%%%%%

\end{cnl}
\end{document}


\section{Introduction}

\begin{frame}{Some Physics}

    \begin{block}{Classical Mechanics}
        \begin{itemize}
            \item In classical mechanics, the space of all possible states of a system is given by \emph{phase space}, $M$.
            \item ``\emph{State}'' describes the \emph{position} and the \emph{momentum}.
        \end{itemize}
    \end{block}

    \begin{block}{Quantum Mechanics}
        \begin{itemize}
            \item In quantum mechanics, still have $M$ but states are replaced by \emph{wavefunctions}.
            \item In my research, wavefunctions are just \emph{homogeneous polynomials}, for example:
            $$ \phi(\vb{z}) = z_{1}^{k_{1}}z_{2}^{k_{2}}z_{3}^{k_{3}}, \qquad \text{where } k_{1} + k_{2} + k_{3} = k. $$
        \end{itemize}
    \end{block}

\end{frame}


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

      \subsection{reward.m}

\begin{par}
\textbf{Summary:} Compute expectation, variance, and their derivatives of an exponentiated negative quadratic cost $\exp(-(x-z)'W(x-z)/2)$, where $x\sim\mathcal N(m,S)$
\end{par} \vspace{1em}
\begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}m:          D-by-1 mean of the state distribution
S:          D-by-D covariance matrix of the state distribution
z:          D-by-1 target state
W:          D-by-D weight matrix\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}muR:        1-by-1 expected reward
dmuRdm:     1-by-D derivative of expected reward wrt input mean
dmuRdS:     D-by-D derivative of expected reward wrt input covariance matrix
sR:         1-by-1 variance of reward
dsRdm:      1-by-D derivative of variance of reward wrt input mean
dsRdS:      D-by-D derivative reward variance wrt input covariance matrix\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modification: 2013-01-20
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item Compute expected reward
   \item Compute the derivatives of the expected reward with respect to the input   distribution (optional)
   \item Compute variance of reward
   \item Compute the derivatives of the variance of the reward with respect to the input distribution (optional)
\end{enumerate}

\begin{lstlisting}
function [muR, dmuRdm, dmuRdS, sR, dsRdm, dsRdS] = reward(m, S, z, W)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
% some precomputations
D = length(m); % get state dimension
SW = S*W;
iSpW = W/(eye(D)+SW);

% 1. expected reward
muR = exp(-(m-z)'*iSpW*(m-z)/2)/sqrt(det(eye(D)+SW));

% 2. derivatives of expected reward
if nargout > 1
  dmuRdm = -muR*(m-z)'*iSpW;  % wrt input mean
  dmuRdS = muR*(iSpW*(m-z)*(m-z)'-eye(D))*iSpW/2;  % wrt input covariance matrix
end

% 3. reward variance
if nargout > 3
  i2SpW = W/(eye(D)+2*SW);
  r2 = exp(-(m-z)'*i2SpW*(m-z))/sqrt(det(eye(D)+2*SW));
  sR = r2 - muR^2;
  if sR < 1e-12; sR=0; end % for numerical reasons
end

% 4. derivatives of reward variance
if nargout > 4
  % wrt input mean
  dsRdm = -2*r2*(m-z)'*i2SpW-2*muR*dmuRdm;
  % wrt input covariance matrix
  dsRdS = r2*(2*i2SpW*(m-z)*(m-z)'-eye(D))*i2SpW-2*muR*dmuRdS;
end
\end{lstlisting}


\section{Classes} % (fold)
\label{sec:classes}
\begin{questions}

    \titledquestion{Rational numbers} % (fold)
    \label{sub:rational_numbers}

        In this problem, we will write a class that can represent rational numbers,
        i.e. fractions $\frac{p}{q}$.

        \begin{parts}
            \part Create a class \texttt{Rational} which is initialized by two
            integers, $p$ and $q$, the nominator and denominator
            \part Add a method to print the rational number as $p/q$
            (the \verb|__str__| or \verb|__repr__| method is useful).
            \part We would like to represent $\frac{10}{20}$ by $\frac{1}{2}$
            instead, hence write a function that computes the greatest common
            divisor, and ensure that every rational number is simplified
            \part Add a method so that we can add two rational numbers with
            \texttt{r1 + r2}, here the \verb|__add__()| method is useful.
            \part Add a method to subtract two rational numbers. (\verb|__sub__|)
            \part Add a method to multiply two rational numbers. (\verb|__mul__|)
            \part Add a method to divide two rational numbers. (\verb|__div__|)
            \part Add a method that compares whether two rational numbers are equal.
            \part Add a method to convert the rational number to a floating point
            (the \verb|__float__()| method may be handy).
            \part Add any more functionality that you think is useful but I failed
            to mention.
        \end{parts}

    % titledquestion rational_numbers (end)

    \titledquestion{Rock Paper Scissors}
    \label{sub:rock_paper_scissors}

        In this problem, we will finish an implementation for Rock-Paper-Scissors.
        We have written some code to get you started, now it's up to you to finish the
        implementation.

        The code consists of 2 files: game.py and agent.py.
        The code that implements the actual game is coded in game.py.
        agent.py defines several agents that can play the game.
        Download the starter code here using\\
        \texttt{\$ git clone https://github.com/schmit/Rock-paper-scissors-startercode.git} (if you have git / use Cloud9)
        or dowload the code directly:\\ \url{https://github.com/schmit/Rock-paper-scissors-startercode/archive/master.zip}
        \begin{parts}
            \part Finish the implementation of game.py by implementing the compare function,
                updating the scores (where a win is 1 point, and a tie or loss 0 points),
                and finally the summary function that gives some information after the game.
            \part Implement the HumanAgent in agent.py, this agent should query the user
                for the next move, and ensure that the user gives valid input.
            \part Implement MyAgent, where you can implement your own strategy,
                try to beat the InstructorAgent consistently over 100 rounds.
        \end{parts}

        Hint: have a look at the Hangman code.

    \titledquestion{Hangman agent}
    \label{sub:hangman_agent}

    Implement your own Hangman computer agent (see exercise~\ref{sec:control_flow}.\ref{sub:hangman_1})
    that is much more effective than the Agent that guesses random characters.

    Make sure you create a new class rather than overwriting the existing \texttt{Agent}
    class. You can of course inherit from the \texttt{Agent} class

    You can update the \texttt{simulate.py} script to test your implementation.

    \titledquestion{Sparse and dense vectors}
    \label{sub:vector_class}
    In exercise~\ref{sec:dictionaries}.\ref{sub:vector_multiply} you implemented
    functions for sparse and dense vector multiplications using lists and dictionaries.
    However, this is a bit clumsy to use in practice.
    Really, we would like to represent sparse and dense vectors as classes,
    this way we can overload operators such as \texttt{+} (\texttt{\_\_add\_\_})
    and get sensible output.
    For example, using \texttt{+} on two dense vectors implemented as lists would
    append the second vector to the first, instead of adding the two together.

    Implement sparse and dense vectors.
    Both classes should have the following capabilities:
    \begin{parts}
        \item Print vector
        \item Add two vectors (both if other is dense and sparse)
        \item Multiply two vectors (both if other is dense and sparse)
    \end{parts}
    Do re-use your code from the previous exercise.

    Hint: \texttt{isinstance()} might be useful.

    \titledquestion{Implementing the set class}
    \label{sub:set_as_dict}

    Write a class \texttt{mySet} that has the same basic functionality as
    the Python \texttt{set} data structure.
    Base your implementation on a dictionary.

    \titledquestion{Binary search tree} % (fold)
    \label{sub:binary_search_tree}

        In this exercise, we will implement a binary search tree.
        See \url{http://en.wikipedia.org/wiki/Binary_search_tree} for
        an explanation.

        \begin{parts}
            \part Define a class \texttt{Node}, and write the constructor, which
            takes one argument, \texttt{value}, and initializes the left and right
            children to None.
            \part Write a function to print the tree.
            \part Write a function that inserts a new value in the tree at the
            right location.
            \part Write a function that looks up a value in the tree.
            \part Write a function that removes a value from the tree.
        \end{parts}

    % titledquestion binary_search_tree (end)

    \titledquestion{Ordinary least squares} % (fold)
    \label{sub:binary_search_tree}

        Our goal in this exercise is to write our own least-squares solver to solve regression problems:
        \[
            \arg\min_{\beta} \|y - X\beta\|_2
        \]
        See for example statsmodels \texttt{ols} or \texttt{LinearRegression}.
        While one can, and should, use written solvers, it's a good practice exercise.

        \begin{parts}
            \part Setup an OLS class with fit and predict methods, to be coded later
            \part Write the fit method using numpy's or scipy's linear algebra module.
            \part Now write the predict function, that predicts $y_n$ given new $X_n$.
            \part Add a function that summarizes the model
            \part (Optional) Use Patsy and Pandas to support DataFrames and formulas, similar to \texttt{R}.
        \end{parts}

    % titledquestion binary_search_tree (end)
\end{questions}
% section classes (end)


%
% Chapter 8
%

\chapter{Statistical methods and systematic uncertainties}
\label{syst_unc}

\section{Introduction}
In the search for LFV Higgs decays, a discovery will be the observation of events with the Higgs boson decaying either into \mutau or \etau. As the LFV is forbidden in SM, the SM can be taken as the background model, and discovery can be claimed if the observation is not compatible with this background model. The uncertainties resulting from theoretical, experimental, and statistical sources can give rise to an excess in the observation even when there is no signal at all. When an excess is observed, a p-value is computed. The p-value represents the probability that an observed excess is due to statistical fluctuations. The p-value has to be very low to indicate that the excess observed is due to a signal's presence and not merely a statistical fluctuation. However, if no excess is observed, upper exclusion limits can be set on the branching fraction. This chapter describes the statistical methods used for extracting the signal strength, followed by the systematic uncertainties involved in the analysis.

\section{Statistical methods}
\label{stat_meth}

The results on the branching fraction of the LFV Higgs boson decays to \mutau, and \etau are estimated using a profile likelihood method. The SM Higgs boson production cross-sections are used for the signal model, while the branching fraction of the Higgs boson to \mutau and \etau remain free parameters. The branching fraction is the parameter of interest. In addition to it, the signal and background model contain nuisance parameters whose values must be derived from collision data. The profile likelihood method is implemented, assuming the asymptotic approximation. Distributions of the BDT discriminator and the collinear mass for signal and various background processes are fitted to collision data. Systematic uncertainties are represented as nuisance parameters, and they can affect the normalization or the shape of the distribution.

Poisson distribution can model the expected number of events and the observed events for the situation at hand. The expected number of events is $\mu\cdot s + b$, where $s$ is the expected signal event yields, and $b$ is the expected background event yields. The parameter $\mu$ is the signal strength modifier, which changes the signal production cross-sections of all the production mechanisms by the same scale of $\mu$. The likelihood function measures the goodness of fit of a statistical model to a sample of data for given values of the unknown parameters. For our situation, the likelihood function $\mathcal{L}(data|\mu)$ is then given by:
\begin{equation}
  \mathcal{L}(data|\mu)=\prod_{i=1}^{bins}\frac{(\mu\cdot s_i + b_i)^{n_i}}{n_{i}!}e^{-\mu\cdot s_i - b_i}
\end{equation}
where $n_i$ is the number of events observed in the bin $i$ of the distribution, and $s_i$ and $b_i$ are the expected number of signal and background events in that bin, respectively.

The nuisance parameters which represent the systematic uncertainties are embedded into the likelihood function. The uncertainties considered are taken to be 100\%-correlated or uncorrelated, thus ensuring that the likelihood function has a clean factorized form~\cite{ATLAS:2011tau}. However, certain uncertainties have partial correlations across the years. There are also partial correlations among uncertainties between the embedded and MC samples. A partially correlated uncertainty is separated into 100\%-correlated or uncorrelated components.
The magnitude of the correlated component will be $\rho$, and for the uncorrelated part, it will be $\sqrt{1-\rho^{2}}$, where $\rho$ is the magnitude of the correlation. For example, a 50\% correlation will have a correlated component with a magnitude of 0.5 and an uncorrelated part with a magnitude of 0.866.

The expected signal and background yields are effected by nuisance parameters and we parametrize them as $s(\theta)$ and $b(\theta)$. There is a default value $\tilde{\theta}$ for each component of $\theta$. The default value reflects our degree of belief on the real value of $\theta$. The probability distribution function $\rho(\theta|\tilde{\theta})$ can then be interpreted as a posterior distribution from measurements of $\tilde{\theta}$. Using Bayes' theorem:
\begin{equation}
  \rho(\theta|\tilde{\theta})=\rho(\tilde{\theta}|\theta)\cdot\pi_\theta(\theta),
\end{equation}

The priors $\pi_\theta(\theta)$ are taken as flat distributions representing no prior knowledge of $\theta$. The likelihood of the measurement can be constrained by using the probability distribution function of $\tilde{\theta}$. After incorporating the nuisance parameters, the likelihood function now becomes:
\begin{equation}
  \mathcal{L}(data|\mu,\theta)=\prod_{i=1}^{bins}\frac{(\mu\cdot s_i(\theta) + b_i(\theta))^{n_i}}{n_{i}!}e^{-\mu\cdot s_i(\theta) - b_i(\theta)}\cdot\rho(\tilde{\theta}|\theta)
\end{equation}

If no excess is observed then upper exclusion limits can be set on the branching fraction using the CL$_\text{s}$ method~\cite{Read:2002hq, Read:2000ru, Junk:1999kv}. According to Neyman-Pearson lemma, the likelihood ratio is the most powerful discriminator. The likelihood ratio is used generally in the searches at the LHC for hypothesis testing, and it uses profiling of nuisances. The test statistic is denoted by $\tilde{q_\mu}$ and is given by:
\begin{equation}
\label{eq:proflik}
  \tilde{q_\mu}=-2\text{ ln}\frac{\mathcal{L}(\text{data}|\mu,\hat{\theta_\mu})}{\mathcal{L}(\text{data}|\hat{\mu},\hat{\theta})},\text{   with  } 0 \leq \hat{\mu} \leq \mu
\end{equation}
where $\hat{\theta_\mu}$ refers to the conditional maximum likelihood estimators of $\theta$, while $\hat\mu$ and $\hat\theta$ refer to the global maximum likelihood estimators for $\mu$ and $\theta$. The constraint $0 \leq \hat{\mu}$ ensures that the signal rate cannot be negative. In contrast, the upper constraint $\hat{\mu} \leq \mu$ is imposed to guarantee that upward fluctuations of data such that $\hat{\mu} > \mu$ are not considered as evidence against the signal hypothesis.

The observed value of the test statistic, $\tilde{q_\mu}^{obs}$, is calculated for the signal strength $\mu$, using equation~\ref{eq:proflik}. Maximum likelihood estimators for the nuisance parameters, under the background-only and signal-plus-background hypotheses, are calculated and are denoted by $\hat{\theta_{0}}^{obs}$ and $\hat{\theta_\mu}^{obs}$ respectively. They are used to generate toy MC pseudo-datasets. These pseudo-datasets are used to construct probability distribution functions of test statistics $f(\tilde{q_\mu}|0,\hat{\theta_{0}}^{obs})$ and $f(\tilde{q_\mu}|\mu,\hat{\theta_\mu}^{obs})$ by treating them as if they were real data. An illustration of these distributions can be seen in Figure~\ref{fig:test_stat_dist}.
\begin{figure*}[!htpb]\centering
 \captionsetup{width=.87\textwidth,justification=centering}
 \includegraphics[width=0.85\textwidth]{plots/chapter8/test_statistic_distri.png}
 \caption{Test statistic distributions for ensembles of pseudo-data generated for background-only (blue) and signal-plus-background (red) hypotheses~\cite{ATLAS:2011tau}.}
 \label{fig:test_stat_dist}
\end{figure*}

CL$_\text{s+b}$ and CL$_\text{b}$ correspond to the probabilities of the observations under both hypotheses. CL$_\text{s+b}$ measures the incompatibility of data under the signal-plus-background hypothesis. CL$_\text{b}$ measures the incompatibility of data under the background hypothesis. These quantities alone are not adequate for hypothesis testing. For example, when the signal is so small that both hypotheses are compatible with the observation and a downward fluctuation of the background can lead to an inference of signal. Also, the data's incompatibility with the background-only hypothesis alone doesn't tell us that it is compatible with the signal. The ratio of the two quantities referred to as CL$_\text{s}$ helps deal with both the situations described above well. The 95\% CL is then arrived at by iterating over $\mu$ until we have CL$_\text{s}=0.05$. The $\mu$ thus obtained is denoted as $\mu^{95\% \, \text{CL}}$ and is said to be excluded at 95\% CL.

\begin{gather}
  p_\mu=P(\tilde{q_\mu}\geq \tilde{q_\mu}^{obs}|\text{signal-plus-background})=\int_{\tilde{q_\mu}^{obs}}^{\inf}f(\tilde{q_\mu}|\mu,\hat{\theta_\mu}^{obs})d\tilde{q_\mu} \\
  1-p_b=P(\tilde{q_\mu}\geq \tilde{q_\mu}^{obs}|\text{background-only})=\int_{\tilde{q_\mu}^{obs}}^{\inf}f(\tilde{q_\mu}|0,\hat{\theta_0}^{obs})d\tilde{q_\mu} \\
  \text{CL}_\text{s}=\frac{p_\mu}{1-p_b}
\end{gather}

The discussion until now pertains to calculating the observed limits when the data is unblinded. However, when the analysis is performed in a blinded manner, we first calculate the expected limits, which are upper exclusion limits calculated using toy datasets of background-only expectation. An extensive set of pseudo-data is generated using the background-only hypothesis, and CL$_\text{s}$ and $\mu^{95\%\text{CL}}$ is calculated for each of them. A distribution is built as a function of the $\mu^{95\%\text{CL}}$ calculated for each pseudo-data. We then calculate the median expected limit from the 50\% quantile of the cumulative distribution function. The $\pm 1\sigma$ and $\pm 2\sigma$ bands are calculated similarly by integrating the distribution until the appropriate quantiles are reached. The expected limits can be used to maximize the sensitivity of the search. A more stringent median limit corresponds to a more sensitive search.

\begin{figure*}[!htpb]\centering
  \captionsetup{width=.87\textwidth,justification=centering}
  \includegraphics[width=0.85\textwidth]{plots/chapter8/Median.png}
  \caption{(left) An example of differential distribution of possible limits on $\mu$ for the background-only hypothesis. (right) The cumulative probability distribution of the plot on the left with 2.5\%, 16\%, 50\%, 84\%, and 97.5\% quantiles defines the median expected limit as well as the 68\% and 95\% bands for the expected value of $\mu$ for the background-only hypothesis.}
  \label{fig:median}
\end{figure*}

\section{Systematic uncertainties}

Several sources of experimental and theoretical systematic uncertainties are taken into account as input to the maximum likelihood fit. These uncertainties affect both the normalization and the shape of the distributions of the different processes. The normalization effect is described by log-normal probability distribution functions and corresponds to a multiplicative factor in the signal or background yields. These probability distribution functions are characterized by the parameter $\kappa$ and are well-suited for positively valued observables. The log-normal distribution looks like:
\begin{equation}
\rho(\theta|\tilde{\theta})=\frac{1}{\sqrt{2\pi}\text{ ln}(\kappa)}\text{exp }(\frac{\text{ln}(\theta/\tilde{\theta})^2}{2(\text{ln }\kappa)^2}) \frac{1}{\theta}
\end{equation}

The distributions' shape is altered when the systematic uncertainties affect the distribution's scale differently in each bin. Such uncertainties are called shape uncertainties~\cite{Conway:2011in} and are modeled using a linear interpolation method~\cite{Read:1999kh}. Two other distributions obtained by varying the nuisance parameter by $\pm 1$ standard deviation are used for implementing the shape uncertainties. A parameter is added to the likelihood that smoothly interpolates between these two other distributions.

As the analysis is categorized into different final states, partial and complete correlations between the uncertainties in different categories are treated appropriately. They are summarized in Tables~\ref{tab:systematics_mt} and~\ref{tab:systematics_et}.

\input{tables/tab_sysUnc_mt.tex}
\input{tables/tab_sysUnc_et.tex}

The uncertainties to reconstruct a \tauh and estimation of its identification efficiency for different \pt ranges are measured using a tag-and-probe method and found to be in the range of 2-3\%. The uncertainties for different ranges of \pt are treated in an uncorrelated way. These uncertainties are also considered for the embedded \Pgt{}\Pgt background, where they are treated 50\% correlated with the simulation uncertainties. For the embedded samples, triggering on muons before being replaced by tau leptons have an uncertainty of about 4\%. This uncertainty is treated as uncorrelated between the three years due to different triggering criteria. Uncertainty due to tracking measurement is also considered in correlated way between $\text{h}^{\pm}$ and $\text{h}^{\pm}\text{h}^{\mp}\text{h}^{\pm}$ decay modes and uncorrelated way between $\text{h}^{\pm}\pi^{0}$ and $\text{h}^{\pm}\text{h}^{\mp}\text{h}^{\pm}\pi^{0}$ decay modes.

Uncertainties due to electrons and muons misidentified as \tauh candidates correspond to 40\% and between 10-70\%, respectively, for different bins of \pt, $\eta$, and \tauh decay modes. The uncertainty due to \tauh energy scale uncertainty is treated as uncorrelated for different decay modes and 50\% correlated between embedded and simulated backgrounds and ranges from 0.7 to 1.2\%. The uncertainty due to the electron (muon) energy scale for misidentified leptons is independent of the \tauh energy scale and amounts to 7\% (1\%). The effect of lepton energy resolution is found to be negligible for the study under consideration.

The jet energy scale (JES) is affected by several sources, and uncertainty is evaluated as a function of \pt and pseudorapidity. JES's effect is propagated to the BDT discriminator by varying each source of uncertainty by one standard deviation and disseminating them to the fit template for each process and are of the order of 3-20\%. The jet energy resolution uncertainties are also taken into account, and they mostly impact the \mjj-defined categories. In the PF candidate list, jets with $\pt < 10 \GeV$ are not considered. They fall under unclustered energy. The unclustered energy scale is considered independently for charged particles, neutral hadrons, photons, and very forward particles, affecting both shape and yield, and are treated as uncorrelated. The efficiency to classify a jet as b-tagged is different in data, and simulation and scale factors depend on jet \pt and $\eta$ are used to correct simulation. The uncertainties in measuring these scale factors are treated as systematic uncertainty.

The uncertainty to track leptons (\Pe, \Pgm), reconstruct them, and identify and isolate is measured using the tag-and-probe method in data in \Zee and \Zmm events and sums up to about 2\%~\cite{Chatrchyan:2012xi, Khachatryan:2015hwa, Khachatryan:2015dfa, CMS:2016gvn}. The uncertainty in the measurement of the muon momentum scale is in the range $0.4-2.7\%$ for different $\eta$, while for the electron momentum scale, it is less than 1\%. The selection of events using electron and muon based triggers results in additional 2\% uncertainty in the yield of simulated processes.

The misidentification rates in the  \ehad and \muhad final states are parameterized using a linear function dependent on transverse momentum of \tauh, where a couple of uncertainties are ascribed per fit function. Discriminators with different signal to background efficiency are used to single out \tauh against electrons and muons, which entails an additional 3\% uncertainty for the \ehad channel. The normalization uncertainties in the data-driven estimation of misidentified lepton backgrounds ($\text{jet}\to\tauh, \Pgm, \Pe$), is taken from the orthogonal control region as described in Chapter~\ref{bkg_est}.

An additional shape uncertainty is estimated for the misidentified lepton background in the \PW boson enriched control region. Hence, the signal region is defined orthogonally to the definition of the \PW boson enriched control region in hadronic channels. The variables $\Delta\phi(\ell, MET)$ are chosen because a clear discrepancy can be seen for them in the \PW boson enriched control region. The discrepancy is obtained by subtracting the other background's contribution from data and dividing it by the estimated misidentified lepton background. It is parameterized as a function of \dphimmet for \muhad channel and as a function of \dphiemet for \ehad channel. This is not applied as a correction to the signal region because no such discrepancy is observed in the signal region, and hence only a shape uncertainty is considered. The discrepancy in the \PW boson enriched control region, parametrization for shape uncertainty, and the distribution observed in the signal region is shown in Figure~\ref{fig:mutauh_systematics} for \Hmuhad channel and Figure~\ref{fig:etauh_systematics} for \Hehad channel.

\begin{figure}[htbp]
  \centering
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2016.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2017.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2018.pdf}
  }
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/2016.png}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/2017.png}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/2018.png}
  }
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2016SR.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2017SR.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/mutau/dPhiMuMET2018SR.pdf}
  }
  \caption{Shape uncertainty estimated for the \Hmuhad channel. (a) Distribution of $\Delta\phi(\Pgm, MET)$ from the \PW boson enriched control region. (b) The discrepancy seen above is parametrized with a linear function. (c) Distribution of $\Delta\phi(\Pgm, MET)$ from the signal region.}
  \label{fig:mutauh_systematics}
\end{figure}

\begin{figure}[htbp]
  \centering
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2016.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2017.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2018.pdf}
  }
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/2016.png}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/2017.png}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/2018.png}
  }
  \subfigure[]{
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2016SR.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2017SR.pdf}
    \includegraphics[width=0.3\textwidth]{plots/chapter8/systematic/etau/dPhiEMET2018SR.pdf}
  }
  \caption{Shape uncertainty estimated for the \Hehad channel. (a) Distribution of $\Delta\phi(\Pe, MET)$ from the \PW boson enriched control region. (b) The discrepancy seen above is parametrized with a linear function. (c) Distribution of $\Delta\phi(\Pe, MET)$ from the signal region.}
  \label{fig:etauh_systematics}
\end{figure}

The misidentified lepton background in the \emu and \mue final states is affected by different shape uncertainties. The statistical uncertainties arising from both fits of the extrapolation factors as a function of the spatial separation between electron and muon, and lepton \pt are taken into account. The uncertainties from extrapolation factors are estimated in the anti-isolated muon control region, which results in an additional uncertainty due to the inversion of muon isolation, with a combined effect of about of 20\% on the normalization.  The predominant source of uncertainties in the simulated background processes, \Zee, \Zmm, \Ztt, \PW{}\PW, \PZ{}\PZ, \PW{}\Pgg, \ttbar, and single top production is the measurement of the cross-section for these processes.

The theoretical uncertainties affecting the measurement of Higgs boson production cross-section are the factorization and the renormalization scales, choice of the PDFs, and the strong coupling constant (\as). These uncertainties affect the normalization of the signal shape and are taken from Ref.~\cite{deFlorian:2016spz}. The variations of the QCD scales results in a 3.9\%, 0.5\%, 0.9\%, and 0.8\% uncertainty for the ggF, VBF, ZH, WH cross-sections, respectively, while variations of PDF+\as results in 3.2\%, 2.1\%, 1.3\%, 1.9\% uncertainty. The acceptance effects are also taken into account while varying the renormalization and factorization scales and the PDF choice and PDF+\as.

The uncertainty in the \Htt branching fraction includes a 1.7\% uncertainty due to missing higher-order corrections, a 0.99\% parametric uncertainty in the quark masses, and a 0.62\% parametric uncertainty in \as. In addition, the latest CMS measurement of the \Htt signal strength $\mu = 0.85^{+0.12}_{-0.11}$ has been used~\cite{CMS:2020dvp}. The uncertainty in the \HWW branching fraction includes a 0.99\% uncertainty due to missing higher-order corrections, a 0.99\% parametric uncertainty in the quark masses, and a 0.66\% parametric uncertainty in \as.

The bin-by-bin uncertainties account for the statistical uncertainties in each bin of the template distributions of every process. The Barlow-Beeston Lite~\cite{Conway:2011in, Barlow:1993dm} approach is used, assigning a single nuisance parameter to scale the sum of the process yields in each bin, constrained by the total uncertainty, instead of requiring separate parameters, one per process. This is useful to minimize the number of parameters needed in the maximum-likelihood fit. They are treated uncorrelated between bins, processes, and categories.

The integrated luminosities of 2016, 2017, and 2018 data-taking periods are individually known with uncertainties in the 2.3--2.5\% range~\cite{CMS:2017sdi, CMS:2018elu, CMS:2019jhq}, while the total Run~2 (2016--2018) integrated luminosity has an uncertainty of 1.8\%, the improvement in precision reflecting the (uncorrelated) time evolution of some systematic effects. The uncertainty in the integrated luminosity affects all processes with the normalization taken directly from the simulation. Shape uncertainties related to the pileup have been considered by varying the weights applied to simulation. The weight variation is obtained by a 5\% change of the total inelastic cross-section used to estimate the number of pileup events in data. Other minimum bias event modeling and simulation uncertainties are estimated to be much smaller than those on the rate and are therefore neglected.

During the 2016 and 2017 data-taking periods, pre-firing has impacted the ECAL Level-1 trigger. A gradual shift in the timing of the inputs of the ECAL Level-1 trigger in the region at $\aeta > 2.0$ caused a specific trigger inefficiency. For events containing an electron (a jet) with \pt larger than $\approx$50~\GeV ($\approx$100~\GeV), in the region $2.5 < \aeta < 3.0$ the efficiency loss is $\approx$10-20\%, depending on \pt, $\eta$, and time. Correction factors were computed from data and applied to the acceptance evaluated by simulation. Uncertainty due to this correction factor is of the order of 1\%.


\subsection{Defining spheres}

\(x^2+y^2+z^2 = r^2\)

\subsection{Volume of a sphere}

\(V=\)

\subsection{Surface area of a sphere}

\(A=\)


\chapter{Discussion and Outlook}

\textbf{by Bastian Boll} \\

Unfortunately, the alignments obtained by our original approach are fairly underwhelming. They do improve upon the initial guess, but the required computation time is relatively large and the alignments are not precise enough to generate subtitles without additional human help. Upon closer examination of the model predictions one can especially see that predicted probabilities of single words over the time of a TED talk remain almost constant. Our hypothesis of why the models seem to be unable to capture the structure of the data is twofold:
\begin{enumerate}
	\item As previously alluded to, the (acoustic) structure of the labels is continuous, but the categorical labels we provided for training are discrete. This makes classification much more challenging because mistaking a word in the audio for an acoustically similar one is treated by cross entropy loss as if the predicted word and the label were completely dissimilar. This is addressed via a word embedding approach described in section \ref{seq:word_embedding}.
	\item It is challenging to construct a good loss function which allows for the inherent imprecision of our data. We describe an alternative approach to mitigate this problem in section \ref{seq:interval_seq}.
\end{enumerate}

\section{Acoustic word embedding}
\label{seq:word_embedding}

\textbf{by Bastian Boll} \\

With regard to acoustical structure, we need to use full words from the transcript as labels instead of word stems. A statistical survey of the dataset analogous to the one described in section \ref{sec:reduction_labels} leads to the decision to use at least about $7000$ distinct words.\\
One can leverage available dictionary resources such as \href{https://github.com/cmusphinx/cmudict}{cmudict} through \href{https://www.nltk.org/}{nltk} in order to transcribe each word into a sequence of phonemes. To quantify the acoustic distance between two words, we can construct the sets of 2-shingles of the respective phoneme sequences and compute their Jaccard distance. This defines a distance metric on the set of words. To be able to leverage this distance metric computationally, we compute an embedding of the words into a Euclidean space such that the Euclidean distance corresponds to the above acoustical distance. This embedding process is a non-trivial optimization problem in a very high dimensional space. To make it computationally feasible, we construct a stochastic variant of the objective and iterate by stochastic gradient descent. The idea of  embedding structure into Euclidean spaces is well-known in the research community and has been used in adjacent problem settings \cite{silfverberg2018sound,goldberg2014word2vec,tang2014learning}.

\section{Interval sequence prediction}
\label{seq:interval_seq}

\textbf{by Bastian Boll} \\
We assume the error between the computed time $t_w$ for $w$ in the audio and the exact time $w$ to be normally distributed with zero mean. Empirically, the standard deviation of the respective error distribution may be as high as $0.8$ seconds. In order to select an interval which contains a given word in the transcript with high probability, we need to consider larger intervals.\\
The model is now set up to predict a sequence of eight words for the MFCC features extracted from a $2.0$ second interval. From the middle of the interval, we take two consecutive words as label which should be part of the eight predicted words with high probability. As a loss function, we compute the minimal acoustic distance between two consecutive predicted words and the two label words.

\section{Conclusion}

\textbf{by Bastian Boll and Paul Warkentin} \\

Our optimization setting has proven to be well-suited to tackle the alignment problem at hand. One can clearly see the positive effect of being able to leverage prior knowledge and prevent unwanted classification side effects such as possible confusion of word order.\\
We have also seen speciallized loss function constructions being better geared towards this specific learning problem, yielding superior results on real-world data. However, the computed alignments still are not sufficient for generating e.g. movie subtitles because local deviations may be on the order of seconds.\\

While we already outlined ways to improve prediction results in sections \ref{seq:word_embedding} and \ref{seq:interval_seq}, the gained structural improvements do not seem to fully overcome the difficulty presented by the coarsely labeled dataset.\\
In order to leverage both the availability of coarsely labeled data and the ability to learn lower level features from data with greater time resolution, one could try to devise a weakly supervised approach. Similar endeavors have already shown to be effective for adjacent problem domains \cite{wei2017personalized, synnaeve2014weakly}. In fact, \cite{serriere2016weakly} even describes proceedings to address very similar alignment problems.

% In summary, we could successfully tackle the problem of automatic subtitle alignment. The results depend on the class of neural network and the loss function that was used during the training process. In the end, the most difficult parts to implement were the nearly exact extraction of the timings of each spoken word as well as the optimization process and loss function considerations. \\

% The latter still presents  opportunity for improvement, as the time required is very inconsistent and overall fairly large. The problem of the optimizer converging towards wrong local minima could also be addressed by a globalization strategy or by introducing a regularization term to make the computed distribution of words more uniform.\\

% Mastering this type of problems with neural networks may also enable application of similar methods to adjacent problem domains such as indicating the progress of an orchestra playing a classical piece of music.


\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{xspace}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\setcounter{MaxMatrixCols}{20}
\usepackage[capitalise]{cleveref}
\usepackage[per-mode=symbol]{siunitx}
\usepackage{url}

\newcommand{\xk}{\ensuremath{\bm{x}_k}\xspace}
\newcommand{\yk}{\ensuremath{\bm{y}_k}\xspace}
\newcommand{\xkk}{\ensuremath{\bm{x}_{k+1}}\xspace}
\newcommand{\xx}[1]{\ensuremath{\bm{x}_{#1}}\xspace}
\newcommand{\uk}{\ensuremath{\bm{u}_k}\xspace}
\newcommand{\ukk}{\ensuremath{\bm{u}_{k+1}}\xspace}
\newcommand{\uu}[1]{\ensuremath{\bm{u}_{#1}}\xspace}
\newcommand{\duk}{\ensuremath{\bm{\dot{u}}_k}\xspace}
\newcommand{\dukk}{\ensuremath{\bm{\dot{u}}_{k+1}}\xspace}
\newcommand{\duu}[1]{\ensuremath{\bm{\dot{u}}_{#1}}\xspace}
\newcommand{\ek}{\ensuremath{\bm{e}_k}\xspace}
\newcommand{\ekk}{\ensuremath{\bm{e}_{k+1}}\xspace}
\newcommand{\ee}[1]{\ensuremath{\bm{e}_{#1}}\xspace}
\newcommand{\rk}{\ensuremath{\bm{r}_k}\xspace}
\newcommand{\ymin}{\ensuremath{\bm{y}_\text{min}}\xspace}
\newcommand{\ymax}{\ensuremath{\bm{y}_\text{max}}\xspace}
\newcommand{\umin}{\ensuremath{\bm{u}_\text{min}}\xspace}
\newcommand{\umax}{\ensuremath{\bm{u}_\text{max}}\xspace}
\newcommand{\dumin}{\ensuremath{\bm{\dot{u}}_\text{min}}\xspace}
\newcommand{\dumax}{\ensuremath{\bm{\dot{u}}_\text{max}}\xspace}
\newcommand{\epsy}{\ensuremath{\epsilon_{y}}\xspace}
\newcommand{\epsu}{\ensuremath{\epsilon_{u}}\xspace}
\newcommand{\epsdu}{\ensuremath{\epsilon_{\dot{u}}}\xspace}
\newcommand{\epst}{\ensuremath{\epsilon_T}\xspace}
\newcommand{\code}[1]{\texttt{#1}\xspace}
\newcommand{\R}{\ensuremath{\mathbb{R}\xspace}}
\newcommand{\ts}{\ensuremath{t_s}}
\newcommand{\Co}{\ensuremath{C_1}}
\newcommand{\Cc}{\ensuremath{C_2}}
\def\pwidth{0.49}

\begin{document}

\section{Problem formulation}

The \code{MPCProblem} class permits to solve Model Predictive Control (MPC) problems.
The class considers the following state space representation of the system
\begin{align}
    \xkk &= A\xk + B\uk\\
    \yk &= \Co\xk
\end{align}
where $\xk \in \R^n$, $\yk \in \R^q$, and $\uk \in \R^p$ represent the state of the system, the output, and the control at time step $k$, respectively.
$A \in \R^{n \times n}$, $B \in \R^{n \times p}$, and $\Co \in \R^{q \times n}$ are the state, the control, and the output matrices.
The matrices given to the class are the discrete time matrices, so if you have the continuous time state space representation of your system, you should first discretize it for a particular sampling time.

In general, the \code{MPCProblem} class solves the following quadratic optimization problem over the time horizon $T$:
\begin{equation}
    \label{eq:min}
    \min_s s^\intercal Q s
\end{equation}
with
\begin{equation}
    \label{eq:s}
    s = [\bm{e}, \bm{u}, \bm{\dot{u}}, \epsy, \epsy, \epsdu, \epst]
\end{equation}
subject to
\begin{align}
    \uu{0} &= \bm{u}^* \label{eq:u0}\\
    \ek &= \Co\xk - \rk,\quad k = 0,\dots,T \label{eq:error}\\
    -\epst &\leq \ee{T} \leq \epst \label{eq:terminal}\\
    \ukk &= \uk + \duk \cdot \ts,\quad k = 0,\dots,T-1 \label{eq:duk}\\
    \Cc\xkk &\leq \ymax + \epsy,\quad k = 0,\dots,T-1 \label{eq:ymax}\\
    \Cc\xkk &\geq \ymin - \epsy,\quad k = 0,\dots,T-1 \label{eq:ymin}\\
    \ukk &\leq \umax + \epsu,\quad k = 0,\dots,T-1 \label{eq:umax}\\
    \ukk &\geq \umin - \epsu,\quad k = 0,\dots,T-1 \label{eq:umin}\\
    \ukk - \uk &\leq \dumax\cdot\ts + \epsdu,\quad k = 0,\dots,T-1 \label{eq:dumax}\\
    \ukk - \uk &\geq \dumin\cdot\ts - \epsdu,\quad k = 0,\dots,T-1 \label{eq:dumin}\\
    \epsy &\geq 0 \label{eq:epsy}\\
    \epsu &\geq 0 \label{eq:epsu}\\
    \epsdu &\geq 0 \label{eq:epsdu}\\
    \epst &\geq 0 \label{eq:epst}\\
\end{align}
where
\begin{equation}
    \label{eq:xk}
    \xk = A^{k} \xx{0} + \sum_{i=0}^{k-1} A^{k-i-1}B \uu{i}
\end{equation}

In \cref{eq:min}, $s$ is the minimization variable and $Q$ a diagonal matrix, which can be modified by the user to weight the minimization terms.
The $Q$ matrix must be positive definite, so all entries on the diagonal must be strictly positive.
This is required because of the resolution method used by the solver, which requires problems to be strictly convex.
This kind of problem is simpler to solve, so the solution can be computed very efficiently.
In turn, we cannot set diagonal entries to 0, which requires the problem to be expressed in a non-intuitive way.
For example, in standard MPC problems \xk is part of the state so, for example, the error constraint is formulated as
\begin{align}
    \xkk &= A\xk + B\uk\\
    \ek &= \Co\xk - \rk,
\end{align}
which is much easier to read than the combination of \cref{eq:error,eq:xk}.
Clearly, \xk should not be part of the minimization: you want to minimize the error, not the state.
With a solver accepting semi-definite programs, this is not a problem, as we can simply set the diagonal entries of the $Q$ matrix regarding the \xk terms to 0.
With the QuadProg++\footnote{\url{https://github.com/liuq/QuadProgpp}} solver used in here this is not possible, and this is the reason why the problem is formulated in a more ``complex'' way.

The $s$ variable is composed by a set of subvariables:
\begin{itemize}
    \item $\bm{e}$, output error: this is the set of output errors for each time step $k$ in the considered time horizon. $\ek \in \R^q$ is the difference between the output of the system ($\Co\xk$) and the target reference value ($\rk$) (constraint in \cref{eq:error}) for $k = 0,\dots,T$.
    \item $\bm{u}$, control: this is the set of control actions for each time step $k$ in the considered time horizon, with $\uk \in \R^p$ for $k = 0,\dots,T-1$. The first value (\uu{0}) is fixed and it is a parameter of the problem (\cref{eq:u0}).
    \item $\bm{\dot{u}}$, control derivative: this is the set of control derivatives, i.e., $\duk \in \R^q$ is the difference between \ukk and \uk (constraint in \cref{eq:duk}) for $k = 0,\dots,T-1$. This term is optional can can be disabled when instantiating the problem, disregarding the control derivative when minimizing.
    \item \epsy: slack variable for the violation of output constraints. This is optional and can be disabled when instantiating the problem. This removes the slack variable from the optimization problem and allows no constraint violation.
    \item \epsu: slack variable for the violation of control constraints. This is optional and can be disabled when instantiating the problem. This removes the slack variable from the optimization problem and allows no constraint violation.
    \item \epsdu: slack variable for the violation of control derivative constraints. This is optional and can be disabled when instantiating the problem. This removes the slack variable from the optimization problem and allows no constraint violation.
	\item \epst: slack variable for the terminal constraint. This is optional and can be disabled when instantiating the problem. This removes the slack variable from the optimization problem and transforms the terminal constraint into $\ee{T} = 0$.
\end{itemize}

With respect to the constraints, the interpretation is the following:
\begin{itemize}
    \item \cref{eq:terminal}: this is called the terminal constraint and indicates that, at the end of the time horizon, we want our output error to be zero, or very close to it. Notice that this might make the problem unfeasible, e.g., when setting a very small time horizon. For this reason, the constraint can be disabled. Alternatively to disabling the constraint, it is possible to enable a slack variable for it, so that the constraint can be violated but at a certain cost.
    \item \cref{eq:duk}: this constraint simply defines that two successive control actions differ by the control derivative. When the minimization on the control derivative is disabled, this constraint is not used.
    \item \cref{eq:ymax,eq:ymin}: constraints on output limiting the maximum and the minimum output value. Notice that we use a different output matrix than the one of the error constraint (\cref{eq:error}), which we call \Cc. The reason is simple: imagine that our state is composed by the acceleration and the speed of a vehicle and that we want to reach a target speed while limiting the acceleration. If we use the same output matrix this is not possible (if not with some ugly hack), but by using two different output matrices we increase the flexibility while lowering the computational effort. In the document and in the code, $\Cc \in \R^{q_2 \times n}$. These constraints can be violated if the slack variable for the output is enabled. If the slack variable on output is not enabled, then the \epsy variable is not considered.
    \item \cref{eq:umax,eq:umin}: constraints on control limiting the maximum and the minimum control value. These constraints can be violated if the slack variable for the control is enabled. If the slack variable on control is not enabled, then the \epsu variable is not considered.
    \item \cref{eq:dumax,eq:dumin}: constraints on control derivative limiting the maximum and the minimum control derivative value. The limit is multiplied by the sampling time \ts, so changing the sampling time doesn't require the user to change the \dumax and the \dumin values. These constraints can be violated if the slack variable for the control derivative is enabled. If the slack variable on control derivative is not enabled, then the \epsu variable is not considered.
    \item \cref{eq:epsy,eq:epsu,eq:epsdu,eq:epst}: these constraints simply force the slack variables to be positive.
\end{itemize}

Finally, \cref{eq:xk} defines \xk in terms of the variables of the problem.
\xx{0} and \uu{0} are the initial state and control action, respectively, while \uk for $k = 1,\dots,k-1$ are the control actions computed by the algorithm.

\section{Sample problem}

The library comes with a sample program (\texttt{test-mpc.cc}) that solves an MPC problem where the state is described by the following system of differential equations:
\begin{equation}
    \dot{\bm{x}} = \begin{cases}
        \dot{a} = -\frac{1}{\tau} a + \frac{1}{\tau} u\\
        \dot{v} = a
    \end{cases}
\end{equation}
The system state represents the acceleration and the speed of a vehicle, where the acceleration is subject to an actuation lag modeled as a first order lag with a time constant $\tau$.
By writing the system in the standard state-space representation we obtain
\begin{align}
    \dot{\bm{x}} &= G\bm{x} + H\bm{u}\\
    \bm{y} &= \Co\bm{x}
\end{align}
where
\begin{equation}
    G = \begin{bmatrix}-\frac{1}{\tau} & 0\\1 & 0\end{bmatrix},\quad H = \begin{bmatrix}\frac{1}{\tau}\\0\end{bmatrix},\quad \Co = \begin{bmatrix}0 & 1\end{bmatrix}
\end{equation}
We discretize the continuous time representation by transforming $H$ and $G$ into $A$ and $B$, respectively, so that the state-space representation now becomes
\begin{align}
    \xkk &= A\xk + B\uk\\
    \yk &= \Co\xk
\end{align}
where
\begin{equation}
    A = e^{G \cdot \ts},\quad B = \int_0^{\ts} e^{G \lambda} d\lambda H
\end{equation}
obtaining
\begin{equation}
    A = \begin{bmatrix}
        e^{-\frac{\ts}{\tau}} & 0\\
        \tau\left(1 - e^{-\frac{\ts}{\tau}}\right) & 1\\
    \end{bmatrix},\quad
    B = \begin{bmatrix}
        1 - e^{-\frac{\ts}{\tau}}\\
        \ts + \tau\left(e^{-\frac{\ts}{\tau}} - 1\right)
    \end{bmatrix}
\end{equation}
$A$, $B$, and $C$ are thus the matrices we pass to the \texttt{MPCProblem} class given a particular choice of the time constant $\tau$ and the sampling time \ts.

In the provided example, we set $\ts = \SI{0.1}{\second}$ and $\tau = \SI{0.5}{\second}$, obtaining
\begin{equation}
    A = \begin{bmatrix}
        0.8187 & 0\\
        0.09063 & 1\\
    \end{bmatrix},\quad
    B = \begin{bmatrix}
        0.1812\\
        0.009365\\
    \end{bmatrix}
\end{equation}
The initial state $\xx{0}$ and the initial control $\uu{0}$ are set to $[0, 0]^\intercal$ and $[0]$, respectively, the reference $\rk$ to $[1]$, the time horizon $T$ to 60 steps (\SI{6}{\second}), the terminal constraint is enabled, and there is no bound for output, control, and control derivative variables.
Finally all the weights in the $Q$ matrix are set to 1.

In the root folder you find a bash script (\texttt{test-mpc.sh}) that runs the test application under different output parameters.
If the scripts finds an \texttt{R} installation, it will also plot the results to some PDF files.

The first example (\cref{fig:test01}) shows the results for the default parameters, i.e., the ones previously described.
The graph shows the different quantities considered in the optimization problem, i.e., the actual speed and the target speed, the acceleration, the control input, and the control derivative.
As expected, the solution brings the system to the target speed minimizing also the effort on control and control derivative.

\begin{figure}[h]
    \centering
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test01}
        \caption{Results for default parameters.}
        \label{fig:test01}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test02}
        \caption{Results for lower weights on control and control derivative.}
        \label{fig:test02}
    \end{minipage}
\end{figure}

In the second example (\cref{fig:test02}) we lower the weights for the control and the control derivative terms in the minimization, setting them to $0.01$ instead of $1$.
Control and control derivative are now much larger, as we told our solver that we do not care too much about minimizing them.
This results in a faster settling time.

In the third example (\cref{fig:test03}) we use the parameters of the second but we add upper and lower bounds on control actions, i.e., \SI{1}{\meter\per\second\squared} and \SI{-1}{\meter\per\second\squared}, respectively.
The result is very similar to the second, with the only difference that the control action is ``truncated'' at \SI{1}{\meter\per\second\squared} as per constraint.
This causes a slightly larger settling time with respect to the second example.

\begin{figure}[h]
    \centering
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test03}
        \caption{Results for lower weights on control and control derivative, plus bounded control action.}
        \label{fig:test03}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test04}
        \caption{Results for lower weights on control and control derivative, plus bounded control and control derivative action.}
        \label{fig:test04}
    \end{minipage}
\end{figure}

In the fourth example (\cref{fig:test04}), w.r.t. the third, we add a constraint on the control derivative as well, i.e., $\SI{-0.5}{\meter\per\second\cubed} \leq \duk \leq \SI{0.5}{\meter\per\second\cubed}$.
The result is pretty evident, both in terms of control derivative bounds and in terms of control input.
The bound on control derivative causes a ``linear'' increase and decrease of the control action.
The additional bound causes the settling time to increase.

The fifth example (\cref{fig:test05}) is the same as the second (i.e., we lower the weights for control and control derivative) but, in addition, we add a bound to the maximum acceleration (\SI{0.6}{\meter\per\second\squared}).
The plot shows that the system reaches the target speed, but the acceleration never exceeds the bound set by the constraint.

\begin{figure}[h]
    \centering
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test05}
        \caption{Results for lower weights on control and control derivative, plus bound on acceleration.}
        \label{fig:test05}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{\pwidth\textwidth}
        \includegraphics[width=\textwidth]{./fig/test06}
        \caption{Results for lower weights on control and control derivative, plus bound on acceleration and slack variable for output bounds enabled.}
        \label{fig:test06}
    \end{minipage}
\end{figure}

The final example (\cref{fig:test06}) enables the slack variable for the output constraint set in the fifth example, setting the weight penalization for the slack variable in the $Q$ matrix to 10.
In this case the solver is allowed to violate the constraint on acceleration, as it can bee seen in the plot.
The constraint bound is highlighted by the gray dashed line, and the solver exceeds this limit reaching roughly \SI{0.75}{\meter\per\second\squared}.

\end{document}