\newpage
\section{Classification Algorithms and Regression Trees}
This is from the book by Breiman et. al.

At the university of California, San Diego Medical Center, when a
heart attack patient is admitted, 19 variables are measured during the
first 24 hours. They include BP, age and 17 other binary covariates
summarizing the medical symptoms considered as important indicators of
the patient's condition.

The goal of a medical study can be to develop a method to identify
high risk patients on the basis of the initial 24-hour data.

The next figure shows  a picture of a tree structured classification
rule that was produced in the study. The letter F means no high and
the letter G means high risk.

\centerline{\epsfig{figure=Plots/plot-07-06.ps,width=\textwidth}}

How can we use data to construct trees that give us useful
answers. There is a large amount of work done in this type of problem.
We will give a brief description in this section.

%%%Karl's notes
\subsection{Classifiers as Partitions}
Suppose we have a categorical outcome
$y \in \mathcal{C} = \{1, 2, \ldots, J\}$. We call ${\cal C}$ the set
of classes. Denote  with ${\cal X}$ the space
of all possible covariates.

We can define a classification rule as a function $d(\bx)$ defined on
${\cal X}$ so that for every $\bx$, $d(\bx)$ is equal to one of the numbers
$1,\dots,J$.

This could be considered a systematic way of predicting class
membership from the covariates.

Another way to define classifiers is to partition  ${\cal X}$
into disjoint sets $A_1,\dots,A_j$ with $d(\bx)=j$ for all $\bx \in A_j$.

But how do we construct these classifiers from data?

\subsection{What is truth?}
We are now going to describe how to construct classification rules
from data. The
data we use to construct the tree is called the training set $\cal L$
which is simply $\{ (\bx_1,j_1),\dots,(\bx_n,j_n) \}$.

Once a classification rule $d(X)$ is constructed how do we define it's
accuracy?

In this section we will define the {\it true misclassification rate}
$R^*(d)$.

One way to estimate $R^*(d)$ is to draw another very large subset
(virtually infinite) from the same population as $\cal L$ and observe
the rate of correct classification in that set. The proportion
misclassified by $d$ is our estimate of $R^*(d)$.

To make this definition more precise, define the space ${\cal X} \times
{\cal C}$ as the set of all couples
$(\bx,j)$ where $\bx \in {\cal X}$ and $j \in {\cal C}$. Let
$\Pr(A,j)$ be a probability distribution on  ${\cal X} \times {\cal
  C}$. Assume each element of $\cal L$ is an iid outcome
from this distribution.

We define the misclassification rate as
\begin{equation}
\label{mr}
R^*(d) = \Pr[d(\bx) \neq j | {\cal L}]
\end{equation}
with $(\bx,j)$ an outcome independent of $\cal L$.

How do we obtain an estimate of this?

The {\it substitution estimate} simply counts how many times we are
right with the data we have, i.e.
\[
R(d) = \frac{1}{N} \sum_{n=1}^N 1_{d(\bx_n) \neq j_n}.
\]
The problem with this estimate is that most classification algorithms
construct $d$ trying to minimize the above equation. If we have enough
covariates we can define a rule that always has $d(\bx_n) = j_n$ and
randomly allocates any other $\bx$. This has an $R(d)=0$ but one can
see that, in general, $R^*(d)$ will be much bigger.

Another popular approach is the {\it test sample} estimate. Here we
divide the data $\cal L$ into two groups ${\cal L}_1$ and
${\cal L}_2$. We then use ${\cal L}_1$ to define $d$ and ${\cal L}_2$
to estimate $R^*(d)$ with
\[
R(d) =\frac{1}{N_2} \sum_{\bx_n \in {\cal L}_2} 1_{d(\bx_n) \neq j_n}
\]
with $N_2$ the size of ${\cal L}_2$. A popular choice for $N_2$ is
1/3 of $N$, the size of $\cal L$.

A problem with this procedure is that we don't use 1/3 of the data
when constructing $d$. In situations where $N$ is very large this may
not be such a big problem.

The third approach is cross validation. We divide the data into many
subsets of equal (or approximately equal) size ${\cal L}_1,\dots{\cal
  L}_V$, define a $d_v$ for each of these groups, and use the estimate
\[
R(d) = \frac{1}{V}\sum_{v=1}^V \frac{1}{N_v} \sum_{\bx_n \in {\cal L}_v}
 1_{d(\bx_n) \neq j_n}.
\]

\subsection{Bayes Rule}
The major guide that has been used in the construction of classifiers
is the concept of the Bayes rule. A Bayes rule is the $d_B$ for which
\[
\Pr[d_B(\bx) \neq j) \leq \Pr[d(\bx) \neq y].
\]
for all classification rules $d(\bx)$.

If we assume that $\Pr(A | j)$ has a probability density  $f_j(\bx)$
such that
\[
\Pr(A|j) = \int_A f_j(\bx) \, dx
\]
then we can show
that
\[
d_B(\bx) = j \mbox{ on } A_j = \{\bx; f_j(\bx)\Pr(j) = \max_i
f_i(\bx)\Pr(j) \}.
\]

Discriminant analysis, kernel density estimation, and $k$-th nearest
neighbor smoothing attempt to estimate $f_j(\bx)$ and $\Pr(j)$ in
order to estimate the Bayes rule. They make many assumptions so the
methods are not always useful.

\subsection{Constructing tree classifiers}
Notice how big the space of all possible classifiers is. In the simple
case where ${\cal X} = \{0,1\}^p$ this space has $2^p$ elements.

Binary trees are a special case of this partition. Binary trees are
constructed by repeated splits of the subsets of ${\cal X}$ into two
descendant subsets, beginning with ${\cal X}$ itself.

\setlength{\unitlength}{1.0cm}
\begin{center}
\begin{picture}(12,11)

% circles
\thicklines
\put(6,10){\circle{2}}
\put(3.5,7.5){\circle{2}}
\put(8.5,7.5){\circle{2}}
\put(10.5,4.5){\circle{2}}

% boxes
\put(1.25,3.5){\framebox(1.5,1.5)[]{${\cal X}_5$}}
\put(4.25,3.5){\framebox(1.5,1.5)[]{${\cal X}_6$}}
\put(6.25,3.5){\framebox(1.5,1.5)[]{${\cal X}_7$}}
\put(8.25,0.5){\framebox(1.5,1.5)[]{${\cal X}_8$}}
\put(11.25,0.5){\framebox(1.5,1.5)[]{${\cal X}_9$}}

% lines
\put(5.5,9.5){\line(-1,-1){1.5}}
\put(6.5,9.5){\line(1,-1){1.5}}
\put(3,7){\line(-1,-2){1}}
\put(4,7){\line(1,-2){1}}
\put(8,7){\line(-1,-2){1}}
\put(9,7){\line(1,-2){1}}
\put(10,4){\line(-1,-2){1}}
\put(11,4){\line(1,-2){1}}

% text
\put(6,10){\makebox(0,0)[]{${\cal X}$}}
\put(3.5,7.5){\makebox(0,0)[]{${\cal X}_2$}}
\put(8.5,7.5){\makebox(0,0)[]{${\cal X}_3$}}
\put(10.5,4.5){\makebox(0,0)[]{${\cal X}_4$}}

\put(6,8.75){\makebox(0,0)[]{Split 1}}
\put(3.45,6){\makebox(0,0)[]{Split 2}}
\put(8.45,6){\makebox(0,0)[]{Split 3}}

\put(10.45,3){\makebox(0,0)[]{Split 4}}
\end{picture} \end{center}

The subsets created by the splits are called {\it nodes}. The subsets
which are not split are called terminal nodes.

Each terminal nodes gets
assigned to one of the classes. So if we had 3 classes we could get $A_1
= {\cal X}_5 \cup {\cal X}_9$, $A_2 ={\cal X}_6$ and $A_3 ={\cal X}_7 \cup {\cal X}_8$.
If we are using  the data we assign the class most frequently found in
that subset of ${\cal X}$. We call these classification tress.

Various question still remain to be answered
\begin{itemize}
\item How do we define truth?
\item How do we construct the trees from data?
\item How do we assess trees, i.e. what makes a good tree?
\end{itemize}

The first problem in tree construction is how to use $\cal L$ to
determine the binary splits of $\cal X$ into smaller and smaller
pieces. The fundamental idea is to select each split of a subset so that
the data in each of the descendant subsets are ``purer'' than the data
in the parent subset.

This can be implemented in the following way

\begin{itemize}
\item Define the node proportions $p(j|t)$ to be the proportion of
  cases $\bx_n \in t$ belonging to class $j$ so that $\sum_j p(j|t) =
  1$.

\item Define a measure of impurity $i(t)$ as a nonnegative function
  $\phi$ such that it reaches its maximum at $\phi(1/n,\dots,1/n)$,
$\phi(1,0,\dots,0)=0$,  and is symmetric with respect to its entries.

A popular example is the entropy
\[
i(t) = - \sum_{j=1}^J p(j|t) \log p(j|t),
\]
but there are many other choices.

\item Define a set $\cal S$ of binary splits $s$ at each node. Then we
  chose the split that minimize the impurity of the new left and right
  nodes
\[
\Delta i(s,t) = i(t) - p_L i(t_l) + p_R i(t_R)
\]

\end{itemize}

There are many different possible splits. For continuous variables
there are an infinite amount. We need to define the set of splits
$\cal S$ that we consider.

Most implementations require that the the splits are defined by only
one covariate, but fancier versions permit the use of linear combinations.

If the covariate is continuous or ordered then the split must be
defined by $x < c$ and $x \geq c$.

If the covariate is categorical then we simply consider all splits
that divide original set into two.

\setlength{\unitlength}{1.0cm}
\begin{center}
\begin{picture}(12,11)

% circles
\thicklines
\put(6,10){\circle{2}}
\put(3.5,7.5){\circle{2}}
\put(8.5,7.5){\circle{2}}
\put(10.5,4.5){\circle{2}}

% boxes
\put(1.25,3.5){\framebox(1.5,1.5)[]{1}}
\put(4.25,3.5){\framebox(1.5,1.5)[]{2}}
\put(6.25,3.5){\framebox(1.5,1.5)[]{1}}
\put(8.25,0.5){\framebox(1.5,1.5)[]{2}}
\put(11.25,0.5){\framebox(1.5,1.5)[]{3}}

% lines
\put(5.5,9.5){\line(-1,-1){1.5}}
\put(6.5,9.5){\line(1,-1){1.5}}
\put(3,7){\line(-1,-2){1}}
\put(4,7){\line(1,-2){1}}
\put(8,7){\line(-1,-2){1}}
\put(9,7){\line(1,-2){1}}
\put(10,4){\line(-1,-2){1}}
\put(11,4){\line(1,-2){1}}

% text
\put(6,10){\makebox(0,0)[]{$x_1$}}
\put(3.5,7.5){\makebox(0,0)[]{$x_2$}}
\put(8.5,7.5){\makebox(0,0)[]{$x_3$}}
\put(10.5,4.5){\makebox(0,0)[]{$x_2$}}

\put(4.25,9.25){\makebox(0,0)[]{$< 5$}}
\put(7.75,9.25){\makebox(0,0)[]{$\ge 5$}}
\put(1.75,6){\makebox(0,0)[]{$> 3$}}
\put(5.25,6){\makebox(0,0)[]{$\le 3$}}
\put(6.75,6){\makebox(0,0)[]{$= 2$}}
\put(10.25,6){\makebox(0,0)[]{$\ne 2$}}
\put(8.75,3){\makebox(0,0)[]{$> 1$}}
\put(12.25,3){\makebox(0,0)[]{$\le 1$}}
\end{picture}
\end{center}

Now all we need is a stopping rule and we are ready to create trees. A
simple stopping rule is that $\Delta i(s,t) < \delta$, but this does
not work well in practice.

What is usually done is that we let the trees grow to a size that is
bigger than what we think makes sense and then prune. We remove node
by node and compare the trees using estimates of $R^*(d)$.

Sometimes to save time and/or choose smaller trees we define a
penalized criterion based on  $R^*(d)$.

The big issue here is \emph{model selection}.  The model
selection problem consists of four orthogonal components.

\begin{enumerate}

\item Select a space of models

\item Search through model space

\item Compare models
  \begin{itemize}
    \item of the same size
    \item of different sizes (penalize complexity)
  \end{itemize}

\item Assess the performance of a procedure

\end{enumerate}

%\vsphalf

\textbf{Important points}:
\begin{itemize}
\item Components 2 and 3 are often confused (e.g., in
  stepwise regression).  That's bad.

\item People often forget component 1.

\item People almost always ignore component 4; it can be the hardest.

\end{itemize}

Better trees may be found by doing a one-step ``look ahead,'' but this
comes with the cost of a great increase in computation.

\subsection{Regression Trees}
If instead of classification we are interested in predicting we can
assign a predictive value to each of the terminal nodes. Notice that
this defines an estimate for the regression function
$\E(Y|X_1,\dots,X_n)$ that is like a multidimensional bin smoother.
We call these regression trees.

Regression trees are constructed in a similar way to classification
trees. They are used for the case where $Y$ is a
continuous random variable.

A regression tree partitions $x$-space into disjoint regions $A_k$
and provides a fitted value $\E(y | x \in A_k)$ within each region.

\setlength{\unitlength}{1.0cm}
\begin{center}
\begin{picture}(12,11)

% circles
\thicklines
\put(6,10){\circle{2}}
\put(3.5,7.5){\circle{2}}
\put(8.5,7.5){\circle{2}}
\put(10.5,4.5){\circle{2}}

% boxes
\put(1.25,3.5){\framebox(1.5,1.5)[]{13}}
\put(4.25,3.5){\framebox(1.5,1.5)[]{34}}
\put(6.25,3.5){\framebox(1.5,1.5)[]{77}}
\put(8.25,0.5){\framebox(1.5,1.5)[]{51}}
\put(11.25,0.5){\framebox(1.5,1.5)[]{26}}

% lines
\put(5.5,9.5){\line(-1,-1){1.5}}
\put(6.5,9.5){\line(1,-1){1.5}}
\put(3,7){\line(-1,-2){1}}
\put(4,7){\line(1,-2){1}}
\put(8,7){\line(-1,-2){1}}
\put(9,7){\line(1,-2){1}}
\put(10,4){\line(-1,-2){1}}
\put(11,4){\line(1,-2){1}}

% text
\put(6,10){\makebox(0,0)[]{$x_1$}}
\put(3.5,7.5){\makebox(0,0)[]{$x_2$}}
\put(8.5,7.5){\makebox(0,0)[]{$x_3$}}
\put(10.5,4.5){\makebox(0,0)[]{$x_2$}}

\put(4.25,9.25){\makebox(0,0)[]{$< 5$}}
\put(7.75,9.25){\makebox(0,0)[]{$\ge 5$}}
\put(1.75,6){\makebox(0,0)[]{$> 3$}}
\put(5.25,6){\makebox(0,0)[]{$\le 3$}}
\put(6.75,6){\makebox(0,0)[]{$= 2$}}
\put(10.25,6){\makebox(0,0)[]{$\ne 2$}}
\put(8.75,3){\makebox(0,0)[]{$> 1$}}
\put(12.25,3){\makebox(0,0)[]{$\le 1$}}
\end{picture} \end{center}

In other words, this is a decision tree where the outcome is a
fitted value for $y$.

We need a new definition $d(\bx)$ and $R^*(d)$.

Now $d(\bx_j)$ will simply be the average of the terminal node where
$\bx_j$ lies. So $d(\bx)$ defines a step-function ${\mathbb R}^p
\rightarrow {\mathbb R}$.

Instead of misclassification rate, we can define mean squared error
\[
R^*(d) = \E[ Y - d(\bx) ]^2
\]

The rest is pretty much the same.
\subsection{General points}

From Karl Broman's notes.

\begin{itemize}

\item This is most natural when the explanatory variables are
  categorical (and it is especially nice when they are \emph{binary}).

\item There is nothing special about the tree structure...the tree
  just partitions $x$-space, with a fitted value in each region.

\item \textbf{Advantage}: These models go after
  \emph{interactions\/} immediately, rather than as an afterthought.

\item \textbf{Advantage}: Trees can be easy to explain to
  non-statisticians.

\item \textbf{Disadvantage}: Tree-space is huge, so we may need
  \emph{a lot\/} of data.

\item \textbf{Disadvantage}: It can be hard to assess uncertainty in
  inference about trees.

\item \textbf{Disadvantage}: The results can be quite variable.  (Tree
  selection is not very \emph{stable}.)

\item \textbf{Disadvantage}: Actual \emph{additivity\/} becomes a mess
  in a binary tree.  This problem is somewhat alleviated by allowing
  splits of the form $x_1 + b x_2 < (\ge) \; d$.

\end{itemize}

\centerline{\textbf{Computing with trees}}

%\HRule

%\vsphalf

\textbf{R}: \verb|library(tree)|; \verb|library(rpart)|
[MASS, ch 10]

%\vspone

\textbf{An important issue}: Storing trees

%\vsphalf

Binary trees are composed of nodes (root node, internal nodes and
terminal nodes).

%\vsphalf

\emph{Root and internal nodes}:
\begin{itemize}
\item Splitting rule (variable + what goes to right)
\item Link to left and right daughter nodes
\item Possibly a link to the parent node (null if this is the root
  node)
\end{itemize}

\emph{Terminal nodes}:
\begin{itemize}
\item Fitted value
\item Possibly a link to the parent node
\end{itemize}

%\vspone

\textbf{C}: Use pointers and structures (\verb|struct|)

%\vsphalf

\textbf{R}: It beats me.  Take a look.

\textbf{Ref}: Breiman et al (1984) Classification and regression
trees.  Wadsworth.


\subsection{Well-balanced Property with Uncertain Topography}

In this section it is shown that the stochastic Galerkin model satisfies the well-balanced property for a lake-at-rest with uncertain topography.
A lake at rest is an idealised scenario where the free-surface is entirely flat and motionless, much like still water in a lake.
Assuming a lake-at-rest for the stochastic model, the mean free-surface elevation is constant and the mean discharge is zero, and there is no uncertainty in the free-surface elevation or discharge.
The bed elevation can have any spatial profile and stochastic profile.

Since the free-surface elevation is constant and the averaged bed elevation is continuous at interfaces then $\eta_{i+1/2,p}^- - z^\star_{i+1/2,p} = \eta_{i+1/2,p}^+ - z^\star_{i+1/2,p}$, hence $h^{-,\star}_{i+1/2,p} = h^{+,\star}_{i+1/2,p}$ for all $i = 0, \ldots, M$ and $p = 0, \ldots, P$.
Shorthand notation is introduced such that
$\eta_p$ is the $p$\textsuperscript{th} expansion coefficient of the spatially uniform free-surface elevation, and
$h^\star_{i+1/2,p} = h^{-,\star}_{i+1/2,p} = h^{+,\star}_{i+1/2,p}$.
Since $h^\star_{i+1/2,p}$ is continuous at interfaces and the discharge is zero then the numerical flux is equal to the physical flux.

In order to preserve a lake-at-rest solution in element $i$ then the ensemble average of the flux gradient must balance the ensemble average of the source term vector in equation~\eqref{eqn:swe-pc}.
The balance of mass and momentum components can be considered separately.
The mass continuity equation balances because the discharge is zero.
For the momentum equation, the following equality must hold for all $l = 0, \ldots, P$:
\begin{align}
\frac{\Ensemble{F_{i+1/2}^{(n)} \pcbasis_l} - \Ensemble{F_{i-1/2}^{(n)} \pcbasis_l}}{\Delta x}
-
\Ensemble{S_i^{(n)} \pcbasis_l}
= 0
\label{eqn:momentum-balance-separate}
\end{align}
where the ensemble average of the bed slope source term $\Ensemble{S_i^{(n)} \pcbasis_l}$ is given by equation~\eqref{eqn:pc-source} and the momentum flux $F_{i+1/2}^{(n)}$ is
\begin{align}
F_{i+1/2}^{(n)} = \frac{g}{2}
\left\{\sum_{p=0}^P h^\star_{i+1/2,p} \pcbasis_p\right\}
\left\{\sum_{s=0}^P h^\star_{i+1/2,s} \pcbasis_s\right\}
\end{align}
and similarly for $F_{i-1/2}^{(n)}$.
Due to the distributivity property of the basis (equation~\ref{eqn:distributive}), equation~\eqref{eqn:momentum-balance-separate} can be rewritten as
\begin{align}
\Ensemble{\frac{F_{i+1/2}^{(n)}
-
F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
=
\Ensemble{ S_i^{(n)} \pcbasis_l}
\label{eqn:momentum-balance}
\end{align}
The ensemble average of the momentum flux gradient is
\begin{align}
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    \frac{g}{2 \Delta x}
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left( h^\star_{i+1/2,p} \, h^\star_{i+1/2,\palt} -
    h^\star_{i-1/2,p} \, h^\star_{i-1/2,\palt} \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l}
    \label{eqn:momentum-flux-unfactorised}
%
\intertext{To be able to factorise equation~\eqref{eqn:momentum-flux-unfactorised}, the first step is to add then subtract the term $h^\star_{i-1/2,p} h^\star_{i+1/2,s}$ to yield}
%
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    \frac{g}{2 \Delta x}
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left( h^\star_{i+1/2,p} \, h^\star_{i+1/2,\palt} -
    h^\star_{i-1/2,p} \, h^\star_{i-1/2,\palt} + \right. \nonumber \\
    &\left.
    \hspace{6em}
    h^\star_{i-1/2,p} \, h^\star_{i+1/2,s} -
    h^\star_{i-1/2,p} \, h^\star_{i+1/2,s} \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l}
    \label{eqn:momentum-flux-4term}
%
\intertext{Using the distributivity property of multiplication, and associativity of summation, equation~\eqref{eqn:momentum-flux-4term} can be rewritten as}
%
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    \frac{g}{2 \Delta x}
    \left[
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left( h^\star_{i+1/2,p} \, h^\star_{i+1/2,\palt} -
    h^\star_{i-1/2,p} \, h^\star_{i-1/2,\palt} +
    h^\star_{i-1/2,p} \, h^\star_{i+1/2,s}
    \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l}
    \right.
    \nonumber \\
    &\left.
    \hspace{2.5em}
    -
    \sum_{p=0}^P \sum_{\palt=0}^P
    h^\star_{i-1/2,p} \, h^\star_{i+1/2,s}
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l} \right]
    \label{eqn:momentum-flux-split}
%
\intertext{
The final term in equation~\eqref{eqn:momentum-flux-split} can be rewritten as $\sum_{p=0}^P \sum_{s=0}^P h^\star_{i-1/2,s} \, h^\star_{i+1/2,p} \Ensemble{\pcbasis_p \pcbasis_s \pcbasis_l}$ due to commutativity of summation and product operators as well as the basis function (equation~\ref{eqn:commutative}).
Then, by associativity of summation:
}
%
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    \frac{g}{2 \Delta x}
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left( h^\star_{i+1/2,p} \, h^\star_{i+1/2,\palt} -
    h^\star_{i-1/2,p} \, h^\star_{i-1/2,\palt} + \right. \nonumber \\
    &\left.
    \hspace{6em}
    h^\star_{i-1/2,p} \, h^\star_{i+1/2,s} -
    h^\star_{i-1/2,s} \, h^\star_{i+1/2,p} \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l}
    \label{eqn:momentum-flux-4term-reordered}
%
\intertext{Finally, equation~\eqref{eqn:momentum-flux-4term-reordered} can be factorised as}
%
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    \frac{g}{2 \Delta x}
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left( h^\star_{i-1/2,p} + h^\star_{i+1/2,p} \right)
    \left( h^\star_{i+1/2,s} - h^\star_{i-1/2,s} \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l} \\
%
\intertext{and, given that $h^\star_{i+1/2,s} = \eta_s - \zmodified_{i+1/2,s}$ and $h^\star_{i-1/2,s} = \eta_s - \zmodified_{i-1/2,s}$, then}
    \Ensemble{\frac{F_{i+1/2}^{(n)}
    -
    F_{i-1/2}^{(n)}}{\Delta x} \pcbasis_l}
    &=
    -\frac{g}{\Delta x}
    \sum_{p=0}^P \sum_{\palt=0}^P
    \left(
    \frac{h^{+,\star}_{i-1/2,p} + h^{-,\star}_{i+1/2,p}}{2}
    \right)
    \left( \zmodified_{i+1/2,\palt} - \zmodified_{i-1/2,\palt} \right)
    \Ensemble{\pcbasis_p \pcbasis_\palt \pcbasis_l}
    \label{eqn:momentum-flux}
\end{align}
Equation~\eqref{eqn:momentum-flux} is equal to the ensemble average of the bed slope source term $\Ensemble{S_i^{(n)} \pcbasis_l}$ given in equation~\eqref{eqn:pc-source}, hence discrete balance is preserved.


\section{Rule Derivation}

Rules derived are stored in \textit{rule\_base.txt}, together with the definitions of fuzzy sets.

\subsection{Manual Rule Derivation}

After observing a couple of patterns from data in the prototyping set and reasoning about their validities, we come up with several heuristics.

First we notice that $Extent$ is a very good feature to start with: triangles have a $Extent$ ratio close to 0.5, rectangles and squares have a $Extent$ ratio close to 1, while circles and ellipses have a $Extent$ ratio of around 0.75 $\sim$ 0.85.

Then we can use $Thinness$ ratio to distinguish circles and ellipses, because circle has the smallest thinness ($=4\pi$) among all 2D figures,.

Similarily, when rectangles have $Thinness$ ratio close to 16, they become squares.

For the feature $Corners$, as mentioned in Section 3.3, it's a very shaky feature for our task. Therefore we decide not to use it.

Finally, we obtain 5 simple rules:

\begin{enumerate}

\item \textit{IF Thinness IS LIKE Circle AND Extent IS LIKE ellipse THEN Shape IS Circle}
\item \textit{IF Thinness IS NOT LIKE Circle AND Extent IS LIKE ellipse THEN Shape IS Ellipse}
\item \textit{IF Extent IS LIKE Triangle THEN Shape IS Triangle}
\item \textit{IF Thinness IS LIKE Square AND Extent IS LIKE rectangle THEN Shape IS Square}
\item \textit{IF Thinness IS NOT LIKE Square AND Extent IS LIKE rectangle THEN Shape IS Rectangle}

\end{enumerate}

\subsection{Autonomous Rule Derivation}

We will discuss the possibility of autonomous rule derivation in Section 7.


\chapter{Feature Selection}

\newthought{Sometimes, measuring features is expensive} or training a model needs to be fast yet accurate. It can also happen that a large number of features make the model difficult to interpret. Hence it is handy to know which features are actually important for the model and which are redundant. And in order to determine which are those features, we use a process called feature scoring.

\begin{wrapfigure}{o}{0.6\textwidth}
    \vspace{-0.5cm}
    \includegraphics[scale=0.4]{workflow.png}
\end{wrapfigure}

There are several scoring methods, some of which are univariate (they look at the relationship between a single feature and the target variable), some are multivariate (they can consider multiple features), and some are model-based (they use models to estimate feature importance).

One such scoring technique is called \textit{information gain}. Let us consider feature X and the target variable Y. Information gain is a univariate method measuring the amount of information gained Y knowing the value of X.

%box plot
A similar method is \textit{Gini decrease}, which also measures decrease in impurity after observing the variable (X). They both favor multilabel features, because they tend to split the data more resulting in purer subsets. This was the reason behind conception of \textit{Relief} (later improved to ReliefF), which handles multilabel and numeric data better. It also has one important advantage - it is a multivariate method, which means it can handle the so-called XOR problems\marginnote{An XOR problem is a problem where the final class would be a result of }

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.4]{test_and_score.png}
    \caption{Cross validation splits the data sets into, say, 10 different non-overlapping subsets we call folds. In each iteration, one fold will be used for testing, while the data from all other folds will be used for training. In this way, each data instance will be used for testing exactly once.}
\end{figure}


\documentclass{article}
%include polycode.fmt

\begin{document}

Not so long ago, an older post of mine about \href{2018-07-12-llc-in-agda.html}{LLC in Agda} circulated on Twitter.
One comment was: \emph{it's typing with leftovers!}. What does that mean? you may ask; let me show.

This is the post with fancy \LaTeX\ formulas :)

\subsection{An example with times}

In \href{2019-06-26-linear-church-encodings.html}{Church encoding of linear types} post,
we wrote a right rule for times as (slightly adjusted)

\begin{code}
 Γ ==> x : X    Γ ==> y : Y
---------------------------- ⊗R
 Γ₁,Γ₂ ==> pair x y : X ⊗ Y
\end{code}

And the fancy \LaTeX\ version of it looks like:

\begin{equation*}
\prftree[r]{${\otimes}\text{R}$}{
\Gamma_1 \Longrightarrow x:X
}{
\Gamma_2 \Longrightarrow y:Y
}{
\Gamma_1,\Gamma_2 \Longrightarrow \mathtt{pair}\;x\;y : X \otimes Y
}
\end{equation*}

In this version, we somehow know how to split the context into two parts,
to construct halves of times-pair.

Another way to formulate rules, is using leftover contexts:

\begin{equation*}
\prftree[r]{${\otimes}\text{R}$}{
\Gamma_1 \xRightarrow{\displaystyle\;x:X\;\;} \Gamma_2
}{
\Gamma_2 \xRightarrow{\displaystyle\;y:Y\;\;} \Gamma_3
}{
\Gamma_1 \xRightarrow{\displaystyle\;\mathtt{pair}\;x\;y : X \otimes Y\;\;} \Gamma_3
}
\end{equation*}

We start with $\Gamma_1$ context, try to construct $x:X$. Its construction
uses as much stuff from context as it needs, leaving over some context $\Gamma_2$.
Then we continue with $\Gamma_2$ context trying to construct $y:Y$, and get
another residual context $\Gamma_3$. So starting with $\Gamma_1$ context,
we can construct a pair $X \otimes Y$, with a leftover $\Gamma_3$.

\subsection{A few more rules}

We'll need few more rules to make large enough fragment of logic to show
meaningful example: Identity rule, left rule for times an
right and left rules for linear implication and one.
This fragment is called \emph{Multiplicative Intuitionistic Linear Logic},
or MILL for short.

\begin{equation*}
\prfbyaxiom{\sc Id}
{
\Gamma, x : X \xRightarrow{\displaystyle\; x : X\;\;} \Gamma
}
\end{equation*}

\begin{equation*}
\frac{
\Gamma_1, x : X, y : Y \xRightarrow{\displaystyle\;z : Z\;\;} \Gamma_2
}{
\Gamma_1, p : X \otimes Y \xRightarrow{\displaystyle\mathtt{case}\;p\;\mathtt{of}\;\mathtt{pair}\;x\;y \mathrel{\mapsto} z : Z\;\;} \Gamma_2
}\,{\otimes}\text{L}
\end{equation*}

\begin{equation*}
\frac {
\Gamma_1, x : X \xRightarrow{\displaystyle\;y:Y\;\;} \Gamma_2
}{
\Gamma_1 \xRightarrow{\displaystyle\;\lambda\,x \mapsto y : X \multimap Y} \Gamma_2
}\,{\multimap}\text{R}
\qquad\qquad
\frac{
\Gamma_1 \xRightarrow{\displaystyle\;x : X\;\;} \Gamma_2
\qquad
\Gamma_2, f\,x : Y \xRightarrow{\displaystyle\; z : Z \;\;}\Gamma_3
}{
\Gamma_1, f : X \multimap Y \xRightarrow{\displaystyle\; z : Z\;\;} \Gamma_3
}\,{\multimap}\text{L}
\end{equation*}

\begin{equation*}
\prftree[r]{$1\text{R}$}
{\
}
{
\Gamma \RightarrowLabel{\mathtt{one} : 1} \Gamma
}
\qquad\qquad
\prftree[r]{$1\text{L}$}
{
\Gamma_1 \RightarrowLabel{z : Z} \Gamma_2
}
{
\Gamma_1, x : 1 \RightarrowLabel{z : Z} \Gamma_2
}
\end{equation*}

\subsection{Linear curry}

Let us see how this work in practice. Let's write
$\mathtt{curry} : ((X \otimes Y) \multimap Z) \multimap X \multimap Y \multimap Z$.

First we'll write in "a traditional style". I'll omit proof terms for brevity.

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\multimap}\text{R}^3$}{
  \prftree[r]{${\multimap}\text{L}$}{
    \prftree[r]{${\otimes}R$}{
      \prfbyaxiom{\sc Id}{X \Longrightarrow X}
    }{
      \prfbyaxiom{\sc Id}{Y \Longrightarrow Y}
    }{
      X,Y \Longrightarrow X \otimes Y
    }
  }{
    \prfbyaxiom{\sc Id}{Z \Longrightarrow Z}
  }{
  X \otimes Y \multimap Z, X, Y  \Longrightarrow Z
  }
}{
\bullet \Longrightarrow (X \otimes Y \multimap Z) \multimap X \multimap Y \multimap Z
}
\end{equation*}

The proof term is

\begin{code}
curry = \f x y -> f (pair x y)
\end{code}

Let's see the with leftovers derivation tree.

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\multimap}\text{R}^3$}{
  \prftree[r]{${\multimap}\text{L}$}{
    \prftree[r]{${\otimes}R$}{
      X,Y \RightarrowLabel{X} Y
    }{
      Y \RightarrowLabel{Y} \bullet
    }{
      X,Y \RightarrowLabel{X \otimes Y} \bullet
    }
  }{
    \prfbyaxiom{\sc Id}{Z \RightarrowLabel{Z} \bullet}
  }{
    X \otimes Y \multimap Z, X, Y \RightarrowLabel{Z} \bullet
  }
}{
  \bullet \RightarrowLabel{ (X \otimes Y \multimap Z) \multimap X \multimap Y \multimap Z} \bullet
}
\end{equation*}

Not so surpsingly, the proof term is the same

\begin{code}
curry = \f x y -> f (pair x y)
\end{code}

\subsection{What's the point?}

The leftover approach seems more complicated, why to use it?
There is at least two reasons. First is that proof search is a lot more direct.
Let us examine the $\mathtt{curry}$ derivation more closely.

This fragment of linear logic is so well behaving, that a "try whatever fits"
strategy will find a solution. Let us however make it a bit more precise:

\begin{enumerate}
  \item Use {\sc Id}-rule if it applies.
  \item Try right rules.
  \item Try left rules.
  \item Go to 1.
\end{enumerate}

Let's see how that strategy works on $\mathtt{curry}$. First we try right rules
while they match. We arrive at \

\begin{equation*}
 X \otimes Y \multimap Z, X, Y \RightarrowLabel{Z} \bullet
\end{equation*}

situation. $Z$ is atomic formula, so no right rule match. {\sc Id} doesn't apply either.
So we have to use some left rule. In this situation we don't have a choice,
as only $\otimes$L applies. The goal state has two obligations, note how
we don't yet know what $\Gamma_1$ will become.

\begin{equation*}
X,Y \RightarrowLabel{X \otimes Y} \Gamma_1 \qquad\qquad \Gamma_1,Z \RightarrowLabel{Z} \bullet
\end{equation*}

We continue with the leftmost derivation. $\otimes_R$ rule applies. Note,
how rule output and input context form kind of a chain:

\begin{equation*}
X,Y \RightarrowLabel{X} \Gamma_2 \qquad\qquad \Gamma_2 \RightarrowLabel{Y} \Gamma_1 \qquad\qquad \Gamma_1,Z \RightarrowLabel{Z} \bullet
\end{equation*}

Now, {\sc Id}-rule applies, and we infer that $\Gamma_2 = Y$.

\begin{equation*}
Y \RightarrowLabel{Y} \Gamma_1 \qquad\qquad \Gamma_1,Z \RightarrowLabel{Z} \bullet
\end{equation*}

{\sc Id}-rule applies again, $\Gamma_1 = \bullet$

\begin{equation*}
Z \RightarrowLabel{Z} \bullet
\end{equation*}

and again. Finally we are left with no obligations and an empty context, as was required.
Proof search completes successfully. Note how we didn't need to an arbitrary guess
how to split the context. With "traditional" sequents, we'd need to guess how to split
context when using $\multimap$L and $\otimes$R rules, for example in a situation like:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{\Gamma_1 \Longrightarrow X}
{\Gamma_2 \Longrightarrow Y}
{X, Y \Longrightarrow X \otimes Y}
\end{equation*}

What should $\Gamma_1$ and $\Gamma_2$ be? With leftovers approach,
we just continue with full context, and see what's left.

It's worth pointing out, that in ordinary lambda calculus, there are no such
problem. If we would need to find a term for $\Gamma \vdash X \times Y$, we'd
split the search into to sub-searches: $\Gamma \vdash X$ and $\Gamma \vdash Y$,
as we can reuse assumptions as many times as needed.

\subsubsection{DILL and annotations}

In a \href{2019-07-03-from-modal-necessity-to-dill.html}{recent post} I described DILL.
It works with leftover typing as well. For example a $\otimes$R-rule would look like:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{\Gamma; \Delta_1 \RightarrowLabel{x : X} \Delta_2}
{\Gamma; \Delta_2 \RightarrowLabel{y : Y} \Delta_3}
{\Gamma; \Delta_1 \RightarrowLabel{\mathtt{pair}\;x\;y : X \otimes Y} \Delta_3}
\end{equation*}

Yet, it's more interesting to use annotations. The assumptions
stay always the same, but their usage changes:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{\Gamma; M_1 \RightarrowLabel{x : X} M_2}
{\Gamma; M_2 \RightarrowLabel{y : Y} M_3}
{\Gamma; M_1 \RightarrowLabel{\mathtt{pair}\;x\;y : X \otimes Y} M_3}
\end{equation*}

where, type type of $M_1$, $M_2$ and $M_3$ depend on $\Gamma$ (they are "lists" of the same length).
Not a big change, but that's how \href{2018-07-12-llc-in-agda.html}{LLC in Agda}
is encoded. And I think that encoding is quite simple.

For example, in a following simple derivation the variables stay always the same: $x : X, y : Y$,
only their multiplicities change during the course. I'll write multiplicities reusing variable names:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{x : X, y : Y; x^1, y^1 \RightarrowLabel{x : X} x^0, y^1}
{x : X, y : Y; x^0, y^1 \RightarrowLabel{y : Y} x^0, y^0}
{x : X, y : Y; x^1, y^1 \RightarrowLabel{\mathtt{pair}\;x\;y : X \otimes Y} x^0, y^0}
\end{equation*}

This approach is also how you could represent and type-check
linear logic using \href{https://hackage.haskell.org/package/bound}{\texttt{bound}}-library.
See an \href{https://github.com/phadej/write-yourself-a-typed-functional-language/blob/master/src/LLC.hs}{example} from my \href{https://skillsmatter.com/skillscasts/12389-write-yourself-a-typed-functional-language}{HaskellX 2018 talk}.

So the implementation concerns are second reason to use leftover typing.
This idea is described in \emph{Typing with Leftovers - A mechanization of Intuitionistic Multiplicative-Additive Linear Logic} by Guillaume Allais (TYPES 2017).

\subsubsection{Scoping...}

We must be careful, as using leftovers scoping is different.
Look closely at left over rules for $\otimes$.
Consider the re-associating term, from $A \otimes (B \otimes C)$ to
$(A \otimes B) \otimes C$, first omitting the proof terms:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{L}$}{
  \prftree[r]{${\otimes}\text{R}$}{
    \prftree[r]{${\otimes}\text{R}$}{
      \prfbyaxiom{\sc Id}{A, B \otimes A \RightarrowLabel{A} B \otimes C}
    }{
      \prftree[r]{${\otimes}\text{L}$}{
        \prfbyaxiom{\sc Id}{B,C \RightarrowLabel{B} C}
      }{
        B \otimes C \RightarrowLabel{B} C
      }
    }{
      A, B \otimes C \RightarrowLabel{A \otimes B} C
    }
  }{
    \prfbyaxiom{\sc Id}{C \RightarrowLabel{C} \bullet}
  }{
    A, B \otimes C \RightarrowLabel{(A \otimes B) \otimes C} \bullet
  }
}{
  A \otimes (B \otimes C) \RightarrowLabel{(A \otimes B) \otimes C} \bullet
}
\end{equation*}

If I haven't made any mistake during type-setting, that tree uses
the specified $\otimes$ leftover rules. But look at the resulting proof
term:

\begin{code}
case abc of
  pair a bc -> pair
    (pair a (case bc of
      pair b c -> b))
    c
\end{code}

Looks like |c| is out-of-scope, but it isn't, given the rules.
This \emph{might} affect what we can do with $\&$ and $\oplus$ rules.
There are also might be some consequences for substitution. I'm not sure.

Note however, that this unconvential behaviour isn't possible in "LLC in Agda"
like encoding: there the scoping is normal, only usages are tracked with
leftovers. So the above example would be rejected by a type-checked with
"Variable not in scope: |c|" error.

\subsection{You don't expect this, but: regular expressions}

Linear logic is a substructural logic, where so called structural rules
of contraction (duplication) and weakening (removing) are removed.
There's third structural rule: exchange (using out-of-order).

\begin{itemize}
  \item all rules: intuitionistic logic
  \item without contraction: affine logic
  \item without weakening: relevant logic
  \item without contraction and weakening: linear logic
  \item without contraction, weakening, and exchange: ordered logic
\end{itemize}

Ordered logic (also known as non-commutative linear logic, I use NCILL for intuitionistic variant)
feels very restrictive, and it is. In ordered logic,
"traditional" ${\otimes}\text{R}$ rule must be read literally,
assumptions have to be used in order, first $\Gamma_1$, then $\Gamma_2$.
In intuitionistic logic context is like a set, in linear logic
like a multiset, and in ordered logic like a list.

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{\Gamma_1, \Gamma_2, \Gamma_3 \RightarrowLabel{X} \Gamma_2, \Gamma_3}
{\Gamma_2, \Gamma_3 \RightarrowLabel{Y} \Gamma_3}
{\Gamma_1, \Gamma_2, \Gamma_3 \RightarrowLabel{X \otimes Y} \Gamma_3}
\end{equation*}

This makes proof search even more simpler, you'll ever need to look at the
front of the context (when applying right rules).

And it turns out, that NCILL describes how regular expressions match:
the match string forms a context (of atomic "propositions"), the regular expression
is a type of expression we want, and for complete match we expect
leftover context to be empty. For complete regular expressions, we'll
need to add lists to the language, but it works out.

Let's match a string \texttt{XY} against \texttt{X(Y$|$Z)} regular expression,
in other words is

\begin{equation*}
  X, Y \RightarrowLabel{X \otimes (Y \oplus Z)} \bullet
\end{equation*}

derivable? It is:

\begin{equation*}
\prfinterspace=2em
\prftree[r]{${\otimes}\text{R}$}
{\prfbyaxiom{\sc Id}{X,Y \RightarrowLabel{X} Y}}
{\prftree[r]{${\oplus}\text{R}_1$}
{\prfbyaxiom{\sc Id}{Y \RightarrowLabel{Y} \bullet}}
{Y \RightarrowLabel{Y \oplus Z} \bullet}
}
{X, Y \RightarrowLabel{X \otimes (Y \oplus Z)} \bullet}
\end{equation*}

The proof search algorithm grew out of regular expression matching
algorithm using \href{https://en.wikipedia.org/wiki/Brzozowski_derivative}{Brzozowski derivatives}.
The result is in implementation of \href{2018-09-13-regular-expressions-of-types.html}{Regular expressions of types}
i.e. \href{https://github.com/phadej/kleene-type}{\texttt{kleene-type}} (\href{http://oleg.fi/haddocks/kleene-type/}{haddocks}).

See [Certified Proof Search for Intuitionistic Linear Logic](https://gallais.github.io/proof-search-ILLWiL/).
I'm rediscovering things Guillaume Allais alrady did.

\subsection{Conclusion}

What could I say. Everything seems to be connected in theoretical computer science.
Linear logic and regular expressions. Lambda calculi and automata.

More seriously. We can formulate logics differently, so their expressive
power stays the same, but the resulting "programming language"
would look different. Maybe more convinient to use, or better in some
other way.

\end{document}


\section{Link-level Transmission Success Probability}
\label{sec:trans_succ_p_pair}
We first study the transmission success probability $p_s(r)$ for a given uplink between a BS at the origin and a device $x_0$ at distance $r$. Using Slivnyak's theorem~\cite{vaze2015random}[Theorem 2.3.3], combining $(\ref{eq:path-loss})$ and $(\ref{eq: sinr-definition})$, we have:
\begin{align}
p_{s} \left( r\right)
& =\mathbb{P}\left\lbrace \frac{H \exp(\chi) r^{-\gamma}}{\sum_{x_j \in \Phi_{m}} H_{x_j} \exp(\chi_{x_j}) r_{x_j}^{-\gamma}}  \geq \theta_T \right\rbrace. \nonumber
\end{align}
Let $I=\sum_{x_j \in \Phi_{m}} H_{x_j} \exp(\chi_{x_j}) r_{x_j}^{-\gamma}$, which is the cumulative interference suffered by $x_0$. Conditioned on log-normal shadowing component $\exp(\chi_{x_j}) $, as shown in~\cite{haenggi2009interference}, $p_{s}\left( r \right)$ can be expressed in terms of Laplace transform of cumulative interference $\mathcal{L}_{I}(s)$ at point $\theta_{T} e^{-\chi} r^{\gamma}$:
\begin{align}
\label{eq:def_ps}
p_{s}\left( r \right)  &= Pr \left\lbrace H  \geq I \theta_{T} e^{-\chi}r ^{\gamma}  \right\rbrace \nonumber\\
% The follwing line can be hidden, not necessary in FINAL_VERSION
%&=\mathbb{E}_{\chi}\left\lbrace  \mathbb{E}_{I} \left[ \exp(-\theta_{T} \exp(-g) r^{\gamma}  I ) \vert \chi = g\right]\right\rbrace   \nonumber\\
&= \int_{-\infty}^{+\infty} \left[ \int_{0}^{+\infty} \exp(-y \theta_{T} e^{-x} r^{\gamma} ) f_{I}(y)dy\right] f_{\chi}(x) dx  \nonumber\\
&= \mathbb{E}_{\chi} \left[ \mathcal{L}_{I}\left\lbrace \theta_{T} e^{-\chi} r^{\gamma}\right\rbrace \right],
\end{align}
where $f_{X} (x)$ is probability density function (PDF) of r.v. $X$, $\mathbb{E}_{X} \left[ \cdot \right] $ is the expectation operator with respect to $X$.
\subsection{Slotted ALOHA}
In slotted ALOHA, the cumulative interference is constant during each slot. The geometry-aware interference analysis for slotted ALOHA is well investigated. Reference~\cite{haenggi2009interference} gives a closed-form expression about the Laplace transform of cumulative interference. With independent Rayleigh fading and log-normal shadowing, we have:
\begin{align}
\label{eq:lp_tr_slotted}
%\mathcal{L}_{I}\left( s \right) &=\exp\left\lbrace -p\lambda_m K_{\text{slotted}} \mathbb{E}\left[ H^{\frac{2}{\gamma}}\exp(\frac{2}{\gamma}\chi)\right]s^{\frac{2}{\gamma}}  \right\rbrace
\mathcal{L}_{I}\left( s \right)  &=\exp \left\lbrace
-p\lambda_m \pi A  \exp \left( 2\sigma^2/\gamma^2\right) s^{2/\gamma}
\right\rbrace,
\end{align}
where $A = \Gamma(1-\frac{2}{\gamma})\Gamma(1+\frac{2}{\gamma})$ and $\Gamma(z)=\int_{0}^{+\infty} x^{z-1} e^{-x} dx$.
%is called the spatial contention factor~\cite{haenggi2009outage}, $\Gamma(z)$ is the gamma function defined as $\Gamma(z)=\int_{0}^{+\infty} x^{z-1} e^{-x} dx$.
%Since $H$ and $\chi$ are independent, $\mathbb{E}\left[ H^{\frac{2}{\gamma}}\exp(\frac{2}{\gamma}\chi)\right] = \Gamma(1+\frac{2}{\gamma}) \exp \left( \frac{\sqrt{2}\sigma}{\gamma}\right) ^2$.
\subsection{Pure ALOHA}
\label{subsec: pure-aloha}
In pure ALOHA, the cumulative interference $I(t)$ suffered by a given packet transmitted in interval $\left[ T, T+B\right]$ is variable during the packet transmission. When advanced transmission techniques (e.g., interleaving, robust channel coding, etc.) are used, $p_{s}(r)$ is a function of the average interference $I^{\text{mean}} = \frac{1}{B}\int_{T}^{T+B} I(t)dt$, which replaces $I$ in $(\ref{eq: sinr-definition})$.

B{\l}aszczyszyn et al. propose a Poisson-rain model~\cite[Sec.2.4]{blaszczyszyn2015interference} that approximates well this case and allows to compute the cumulative interference. They prove that formula $(\ref{eq:lp_tr_slotted})$ can be reused by letting $A=\frac{2\gamma}{\gamma + 2} \Gamma(1-\frac{2}{\gamma})\Gamma(1+\frac{2}{\gamma})$.
%\subsection{Pure ALOHA, maximum interference}

Another case of pure ALOHA is assumed to have no error correction neither interleaving techniques for the purpose of reducing the device-side cost. In this case, the packet is delivered if and only if each bit is correctly received. Hence, the SINR should be larger than or equal to $\theta_{T}$ during $B$. Hence, $p_{s}$ is a function of maximum interference $I^{\text{max}} = \max_{t \in \left[ T, T+B\right]} I(t)$. According to~\cite[Sec.2.4]{blaszczyszyn2015interference}, there is no closed-form for $I^{\text{max}} $, and the authors use a simulation approach to study. Next, we propose a simple upper bound to estimate $I^{\text{max}}$. Section~\ref{sec:simulation} shows that the bias is at an acceptable level.

Consider a packet transmitted in interval $\left[ T, T+B\right]$. Any device generating its interference at $T$ terminates packet transmission before $T+B$. The interfering packets at $T+B$ do not exist at $T$, because the packet transmission duration is $B$.
Hence, cumulative interference $I(T)$ and $I(T+B)$ are two independent and identically distributed random variables. Furthermore, any device generating interference on the packet is either active at $T$  or at $T+B$. The maximum interference is thus upper bounded by $I(T)+I(T+B)$. Therefore, the Laplace transform of maximum cumulative interference upper bound $\mathcal{L}_{I^{\text{upper}}}\left( s \right)$ during interval $\left[ T, T+B\right]$ is equal to $ \mathcal{L}_{I(T)+I(T+B)}\left( s\right)= \left[ \mathcal{L}_{I(T)}\left( s\right) \right]^2$.

We can reuse $(\ref{eq:lp_tr_slotted})$ to calculate $\mathcal{L}_{I(T)}\left( s\right)$. Thus, Laplace transform of $I^{\text{upper}}$ can be unified into $(\ref{eq:lp_tr_slotted})$ by letting $A=2 \Gamma(1-\frac{2}{\gamma}) \Gamma(1+\frac{2}{\gamma})$.

Combining $(\ref{eq:def_ps})$ and $(\ref{eq:lp_tr_slotted})$, we can express $p_s(r)$ with a formula common to all ALOHA cases:
\begin{align}
\label{eq:def_ps_2}
p_{s}(r) & = \mathbb{E}_{\chi}\left[ \exp(-p \lambda_{m} \pi A \theta_{T}^{\frac{2}{\gamma}} e^{\frac{2\sigma^2}{\gamma^2}}  r^2 e^{-\frac{2}{\gamma}\chi}) \right],
\end{align}
%\begin{align}
%\label{eq:conditional_lp_trans}
%\mathcal{L}_{I}\left\lbrace \theta_{T} \exp(-\chi) r^{\gamma}\right\rbrace = \exp\left\lbrace -A K r^2\exp(-\frac{2}{\gamma}\chi)\right\rbrace,
%\end{align}
where $\sigma = \frac{\ln(10)}{10}\sigma_{dB}$ and:
\[ \!\!\!\!A =
\begin{cases}
\Gamma\!\!\left( 1-\frac{2}{\gamma}\right)\Gamma\!\!\left( 1+\frac{2}{\gamma}\right) ,  & \text{for slotted ALOHA }\\
\frac{2\gamma}{\gamma+2}\Gamma\!\!\left( 1-\frac{2}{\gamma}\right)\Gamma\!\!\left( 1+\frac{2}{\gamma}\right),  & \parbox[t]{.41\columnwidth}{for pure ALOHA, average interference }\\
2\Gamma\!\!\left( 1-\frac{2}{\gamma}\right)\Gamma\!\!\left( 1+\frac{2}{\gamma}\right),  & \parbox[t]{.4\columnwidth}{for pure ALOHA, maximum interference }
\end{cases}
\]
%With substitution of $(\ref{eq:conditional_lp_trans})$ into $(\ref{eq:def_ps})$,
%Although expression $(\ref{eq:def_ps_2})$ is not in closed-form, it simplifies the analysis in Sec.~\ref{sec:op_over_infinite_plane}.

%The authors of paper: Interference and SINR coverage in spatial non-slotted ALOHA networks, state that: We have not been able to derive closed formulas when the maximal interference constraint is used; this case is studied by simulations in Section 5.

%\[ \!\!\!\!K\!\!\!=\!\!\!
%\begin{cases}
%\pi \Gamma\left( 1-\frac{2}{\gamma}\right),  & \text{for slotted ALOHA }\\
%\frac{2\gamma\pi}{\gamma+2}\Gamma\!\!\left( 1-\frac{2}{\gamma}\right),  & \parbox[t]{.41\columnwidth}{for pure ALOHA, average interference }\\
%2\pi \Gamma\left( 1-\frac{2}{\gamma}\right),  & \parbox[t]{.4\columnwidth}{for pure ALOHA, maximum interference }
%\end{cases}
%\]
%
%\[ K\!\!\!=\!\!\!
%\begin{cases}
%\pi \Gamma\left( 1-\frac{2}{\gamma}\right),  & \text{for slotted ALOHA }\\
%\!\!\frac{2\gamma\pi}{\gamma+2}\Gamma\!\!\left( 1-\frac{2}{\gamma}\right),  & \!\!\!\text{for pure ALOHA, average interference}\\
%\!\!2\pi \Gamma\!\!\left( 1-\frac{2}{\gamma}\right),  & \!\!\!\!\!\!\!\text{for pure ALOHA, maximum interference }
%\end{cases}
%\]


\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}
\usepackage[utf8]{inputenc} % this enables utf-8, and is currently commented out
\usepackage{cprotect}
% \usepackage{tikz}
% \usepackage[bottom]{footmisc}
% \usepackage[preprint]{spconfa4}
\usepackage{hyperref}

\cprotect\title{The inner workings of \verb|Function::from_points|}
\author{Liam Bloom}
\date{January 14, 2021}

\begin{document}

\maketitle

\section*{Introduction}

This library takes, as input, a series of $n$ points
\((x_0, y_0), (x_1, y_1)\cdots \\ (x_{n-2}, y_{n-2}),
(x_{n-1}, y_{n-1})\), and returns {\textit a} \verb|Function| that
produces these points. Note that this will not be the only such
\verb|Function|, only the one that my code feels makes the most sense.
Here are, in order, the types of functions that may be returned, and
how they are calculated.

\section{Polynomial}

{\textbf Form:} \(f(x)=ax^{n-1}+bx^{n-2}\cdots cx^1+d\) \vspace{2ex}

A polynomial of the $n$-th degree, the standard form of which is shown
above, can be generated from a series of $\geq n+1$ points using linear
algebra. We begin by creating a system of equations:

\[y_0=ax_0^{n-1}+bx_0^{n-2}\cdots cx_0^1+d\]
\[y_1=ax_1^{n-1}+bx_1^{n-2}\cdots cx_1^1+d\]
\[\vdots\]
\[y_{n-2}=ax_{n-2}^{n-1}+bx_{n-2}^{n-2}\cdots cx_{n-2}^1+d\]
\[y_{n-1}=ax_{n-1}^{n-1}+bx_{n-1}^{n-2}\cdots cx_{n-1}^1+d\]

This can be rewritten as a linear algebra equation:

% NOTE: The final row is $1$, NOT \(x^0\), because $d$ is multiplied by 1 even when \(x=0\) (\(0^0\neq 0\))

\[
    \begin{bmatrix}
    x_0^{n-1} & x_0^{n-2} & \cdots & x_0^1 & 1 \\
    x_1^{n-1} & x_1^{n-2} & \cdots & x_1^1 & 1 \\
    \vdots & \vdots & \ddots & \vdots & \vdots \\
    x_{n-2}^{n-1} & x_{n-2}^{n-2} & \cdots & x_{n-2}^1 & 1 \\
    x_{n-1}^{n-1} & x_{n-1}^{n-2} & \cdots & x_{n-1}^1 & 1
    \end{bmatrix}
    \begin{bmatrix}
    a \\
    b \\
    \vdots \\
    c \\
    d
    \end{bmatrix}
    =
    \begin{bmatrix}
    y_0 \\
    y_1 \\
    \vdots \\
    y_{n-2} \\
    y_{n-1}
    \end{bmatrix}
\]

and solved by multiplying both sides by the inverse:

\[
    \begin{bmatrix}
    a \\
    b \\
    \vdots \\
    c \\
    d
    \end{bmatrix}
    =
    \begin{bmatrix}
    x_0^{n-1} & x_0^{n-2} & \cdots & x_0^1 & 1 \\
    x_1^{n-1} & x_1^{n-2} & \cdots & x_1^1 & 1 \\
    \vdots & \vdots & \ddots & \vdots & \vdots \\
    x_{n-2}^{n-1} & x_{n-2}^{n-2} & \cdots & x_{n-2}^1 & 1 \\
    x_{n-1}^{n-1} & x_{n-1}^{n-2} & \cdots & x_{n-1}^1 & 1
    \end{bmatrix}^{-1}
    \begin{bmatrix}
    y_0 \\
    y_1 \\
    \vdots \\
    y_{n-2} \\
    y_{n-1}
    \end{bmatrix}
\]

We then plug \(a, b \cdots c, d\) into the standard form to generate
our function.

\subsection{Old Polynomial Method}

The above linear algebra method was not used for determining polynomial
functions prior to commit \cprotect{\href{
https://github.com/liambloom/pattern-finder/commit/1f190e5aca4f2f4dda9d342b1e721a6458b44415
}}{\verb|1f190e5|}. Instead, the following method was used:

\section{Exponential}
{\textbf Form:} \(f(x)=ab^x+c\) \vspace{2ex}

An exponential equation, as shown above, can be generated from
$\geq 3$ points

% TODO

\section{The Future}

This section lists what types of functions may or may not be added
to this library in the future, and if not, why.

\subsection{Likely to be added}
\begin{itemize}
    \item Rational
    \item Fibonacci-like
\end{itemize}

\subsection{May be added}
\begin{itemize}
    \item Trig
    \item Absolute Value
\end{itemize}

\subsection{Unlikely to be added}
\begin{itemize}
    \item Radical functions would most likely require storage of
        irrational numbers, which is impossible
    \item Logarithmic functions for the same reason as radicals
    \item Conic Sections because they cannot be represented as functions
    \item Modulus because it would be quite difficult to implement
    \item \(\frac{|x|}{x}\) because it is very niche
\end{itemize}

\end{document}


\subsection{Bias of static models and spurious correlations}
\subsubsection{Static models}

Static models are of the form:

\(y_{t}=\alpha+\beta x_{t}+\epsilon_{t}\)

These have no lagged variables or difference operators.

\subsubsection{Bias of static models}


\chapter{PID}
\label{chp:pid}
The Proportional-, Integral- and Derivative(PID)-controller can be used for motion and error correction. An example of a feedback loop with error correction is shown in figure \ref{fig:PID_Circuit}.

\myFigure{Theory/PID/PID_Circuit}{PID using error feedback. From the book \citep{FBS}}{fig:PID_Circuit}{0.7}

PID can be used for motion correction such as correcting the steering of a car. For example if the error is defined as the crosstrack error which means how far the car is off the intended course. The PID controller can then be feed with the error, and the different parts of the PID controller can then adjust the control signal which affects the steering of the car.

\section{Proportional-, Integral- and Derivative(PID)-controller}
There are three parts in the PID controller. First there is the Proportional part.
The proportional part is calculated by: Kp*CurrentError. The result of this is shown in figure \ref{fig:prop}.
\myFigure{Theory/PID/proportional}{Proportional part of PID.}{fig:prop}{0.5}

As figure \ref{fig:prop} shows by only having the proportional part the curve will be marginally stable. This result in an oscillation.

Too compensate for oscillation the derivative part is added. The derivative part can also be helpful against overshooting. This is seen in figure \ref{fig:derivative}.

\myFigure{Theory/PID/derivative}{Derivative part of PID.}{fig:derivative}{0.5}

In figure \ref{fig:derivative} the "PD controller" curve gets to the correct course faster than the "P controller" curve does. The derivative part is calculated by: Kd*(CurrentError - PreviousError).

But is the PD controller enough ? No. An example would be if there is a systematic bias. This can result in an constant offset. The PD controller alone cannot compensate for this offset.

That is why the integral part is needed.

Figure \ref{fig:integral} shows that the integral will overtime compensate for this offset.

\myFigure{Theory/PID/integral}{Integral part of PID.}{fig:integral}{0.5}

The integral part is calculated by: (Summation of errors)*Ki.

In figure \ref{fig:pid_calc} the calculation for the output u can be seen. This calculation are with all three part proportional, integral and derivative.

\myFigure{Theory/PID/pid_calc}{PID with all parts.}{fig:pid_calc}{0.5}

In figure \ref{fig:pid_calc} all three gains are shown {Kp, Ki, Kd} and the error and time are also used to calculate the control signal u.

\section{PID Tuning}

Their are many ways of tuning a PID controller. Two of them are Twiddle and Ziegler-Nichols.
Twiddle is hill climbing algorithm. it works by trial and error. The problem with this is that it typically finds the local minima, and not the global minima.

Ziegler-Nichols works by initially Ki and Kd are set to zero. Then Kp is raised until oscillation appears. Then Kc is set to value of Kp and Tc is the period of the oscillation. When Kc and Tc are found the table in figure \ref{fig:zig} can be used to get Kp, Ti and Td.

\myFigure{Theory/PID/zig}{Ziegler-Nichols frequency response method. From the book \citep{FBS}}{fig:zig}{0.5}

The problem with Ziegler-Nichols is that it gives an aggressive gain and overshoot.

Tuning a PID controller will always depend on the application. Which makes it difficult to generalize the solution of tuning a PID controller.


\section{Introduction}

The planar incompressible Stokes equations describe
creeping flows in two dimensions.
%
Let $\Omega \subset \R^2$
be a bounded domain with $C^2$ boundary denoted by $\Gamma$.
%
The Stokes eigenvalue problem is to find
values $k^2$ such that

\begin{equation}
\begin{aligned}
  -\Delta \uu + \nabla p &= k^2 \uu \quad \textrm{in} \quad
  \Omega \label{eq:ostokes} \; , \\
  \nabla \cdot \uu &= 0 \; ,
\end{aligned}
\end{equation}
subject to boundary conditions, has a non-trivial solution $(\uu,p)$.
%
In this work, we consider the eigenvalue problem subject to
the Dirichlet boundary condition,
\begin{equation}
  \uu = \bz \quad \textrm{on} \quad \Gamma \label{eq:ostokes_dir} \; .
\end{equation}
It is well known that the values $k^2$ are necessarily
real and positive and that there is a countable collection of such
values $0 < k_{1}^{2} \leq k_{2}^2 \leq \ldots \uparrow \infty$,
counting multiplicities.

\begin{remark}
  When $k = i\alpha$, the differential equation
  \cref{eq:ostokes} is known as the modified Stokes
  equation. As there appears to be no preferred
  name for the equation with real-valued $k$,
  we will refer to \cref{eq:ostokes} as the
  oscillatory Stokes equation.
\end{remark}

The eigenvalues (and eigenfunctions)
of the Stokes operator have applications in the
stability analysis of stationary solutions of the
Navier--Stokes equations \cite{osborn1976approximation},
in the study of decaying two dimensional turbulence
\cite{schneider2008final}, and as a trial basis for
numerical simulations of the Navier--Stokes
equations~\cite{batcho1994generalized}.
%
The eigenvalues and eigenfunctions of the Stokes
operator are also the subject of intense analytical
investigation
\cite{taylor1933buckling,szego1950membranes,
  polya1951isoperimetric,bramble1963pointwise,
  ashbaugh1996fundamental,leriche2004stokes,
  kelliher2009eigenvalues,antunes2011buckling},
especially as they relate to the eigenvalues and
eigenfunctions of the Laplacian.

Historically, the Stokes eigenvalue problem serves as a
common model problem for numerical eigenvalue analysis
with a fourth order operator (here, the bi-Laplacian).
%
Further, numerical simulation has long played an
important role in the analyses cited above --- both for
computing the eigenvalues and eigenfunctions
on domains of practical interest and in forming
new conjectures.

Borrowing the language of~\cite{zhao2015robust},
which concerns the eigenvalues of the Laplacian
(also known as the membrane or ``drum'' problem),
the numerical treatment of the Stokes eigenvalue
problem can be divided into two basic approaches.
%
The first class of methods
directly discretize the
differential operator, typically with a
finite element basis, and the eigenvalues are found
as the eigenvalues of the discrete system.
%
The second class of methods reformulate the
oscillatory Stokes equations as a boundary integral
equation (BIE) which is discretized.
The eigenvalues are then found
by a nonlinear search for the values of
$k$ where the BIE is not invertible.
%

There is a large body of research on the first class of methods
for the Stokes eigenvalue problem.
%
We do not seek to review this literature here,
but point to \cite{johnson1974beam,
  rannacher1979nonconforming,
  mercier1981eigenvalue,bjorstad1999high,
  jia2009approximation,chen2006approximation,
  lovadina2009posteriori,huang2011numerical,
  carstensen2014guaranteed}
for some representative examples.

As noted in~\cite{zhao2015robust}, integral
equation based methods provide several advantages.
%
Because the BIE is defined on the
boundary alone, there is a reduction in the
dimension of the domain to be discretized.
%
This approach reduces the number of unknowns over finite
element discretizations, and does not suffer
from high-frequency ``pollution'' due to the
large number of unknowns required to resolve
the computation when $k$ is large
\cite{babuska1997pollution} (which typically affects
finite element approaches to determining the eigenvalues).

Further, Zhao and Barnett~\cite{zhao2015robust}
show how to alleviate some of the costliness of the
nonlinear optimization introduced by formulating the
problem as an integral equation.
%
The standard approach searches for ``V''-shaped minima
of the singular values of the BIE; see, for
instance, \cite{trefethen2006computed}.
%
Instead, Zhao and Barnett utilize the Fredholm
determinant (see \cref{sec:dets}) which, for certain
BIEs, is an analytic function of $k$ with roots
precisely when $k^2$ is an eigenvalue.
%
The Fredholm determinant can be estimated using
a Nystr\"{o}m discretization of the BIE
\cite{bornemann2010numerical,zhao2015robust}.
%
Then, the eigenvalues can be estimated efficiently
by using high order root finding methods applied
to the discretized determinant.

With the efficiency of the approach of
\cite{zhao2015robust} for the drum problem in mind,
we develop an integral equation based method for the
Stokes eigenvalue problem.
%
This requires that a layer
potential representation of the solution
of \cref{eq:ostokes} be given and that the resulting BIE
is not invertible precisely when $k^2$ is an eigenvalue.
%
The first requirement is straightforward to
satisfy because
well-known layer potential representations for the
modified Stokes equation~\cite{Pozrikidis1992,biros2002embedded,
  jiang2013second,ladyzhenskaya1969mathematical}
are directly applicable.
%
Proving the invertibility of the associated operators
away from the eigenvalues is a more involved task
and forms the bulk of the theoretical component
of this paper.

\subsection{Relation to other work}

While integral equation based methods for the
related ``buckling'' eigenvalue problem
(which is equivalent  on simply connected domains
\cite{kelliher2009eigenvalues})
have been considered previously,
these typically relied on first-kind integral
equation formulations of the underlying PDE,
i.e. formulations in which the BIE operator is
compact \cite{kitahara2014boundary,antunes2011buckling}.
%
This is unsatisfying from a numerical
perspective, because the spectrum of a
compact operator either contains zero
or has zero as a limit point
(by design, the representations in
\cite{kitahara2014boundary,antunes2011buckling}
are not injective precisely when
$k^2$ is an eigenvalue).
%
This obscures the relation between
the non-invertibility of discrete approximations
of the operator and the eigenvalues;
in particular, common measures of the
``non-invertibility'' of a matrix, like the
smallest singular value or the determinant,
converge rapidly to zero for all values
of $k^2$ as the boundary is refined.
The measure of whether $k^2$ is an
approximate eigenvalue is then {\em relative to the
  current grid} for first kind formulations.

The classical single and double layer representations
for oscillatory Stokes considered in this paper
result in second kind equations, i.e. integral equations
of the form $\mathcal{I} - \mathcal{K}_k$ where
$\mathcal{K}_k$ is compact.
%
Such equations have a more satisfying theory
\cite{reed1972methods,colton1983integral,kress1989linear},
which translates well to numerical implementation
\cite{atkinson2009numerical,bornemann2010numerical,
  hackbusch2012integral,zhao2015robust}.
%
The use of a second kind representation is standard
for the drum problem \cite{backer2003numerical,zhao2015robust}
and was used recently to compute the vibrating
modes of thin, clamped plates~\cite{lindsay2018boundary}.

\subsection{Paper outline and contributions}

The rest of this paper proceeds as follows.
%
In \cref{sec:prelim}, we set the notation, provide some
mathematical preliminaries, and review
properties of single and double layer potentials
for the oscillatory Stokes equations.
%
Then, in \cref{sec:analysis}, we develop the necessary
theory for proving the main results of this work
(\cref{thm:dlmain,thm:cfmain}),
which show that the BIEs resulting from these
layer potential representations are not invertible
precisely when $k^2$ is an eigenvalue.
%
These theoretical developments include a detailed
discussion of the uniqueness of oscillatory Stokes
boundary value problems in exterior domains.
%
To the best of our knowledge, the invertibility
and uniqueness results are new to the literature.
%
\Cref{sec:dets} then outlines how the Fredholm determinant
can be used in the oscillatory Stokes context.
%
In \cref{sec:numerical}, we describe the numerical
methods we use to discretize the BIEs and to perform
determinant calculations.
%
While the underlying methods are well-established,
the combination of a high-order singular quadrature
rule and a fast-direct method for determinant evaluations
in a BIE framework is novel.
%
At the moderate frequencies considered in this
paper, we find that standard fast-direct
solvers provide a reasonably efficient determinant
evaluation.
%
We also present numerical experiments
which demonstrate some of the paper's analytical
claims as well as the effectiveness of the overall
framework.
%
Finally, we provide some concluding thoughts,
describe plans for future research,
and outline some open questions in
\cref{sec:conclusion}.


\subsection{Karush-Kuhn-Tucker conditions}

If our problem is non-convex, or if Slater's condition does not hold, how else can be find a solution?

A solution, \(p^*\) can satisify KKT conditions.


\documentclass[../PHYS306Notes.tex]{subfiles}

\begin{document}
\section{Lecture 18}
\subsection{Lecture Notes - Moment of Inertia Tensor}
\subsubsection{Review of Last Day's Results}
\[\v{L} = \v{L}_{CM} + \v{L}_{rel}\]
\[T = T_{CM} + T_{rel}\]
\[U = U_{CM} + U_{rel}\]
We can decompose the angular momentum and energies into the center of mass term and the relative to the COM term. We note that for rigid bodies, $U_{rel}$ is constant.
\newline Last time (see 17.1.5) we observed that the angular momentum vector and the rotation vector are, in general, not parallel. Let us solve a question with a similar idea. Suppose we have a rotating dumbbell of two masses $m$ which move in circles (radius $a$) at a z displacement $l$ and $-l$, joined by a massless rod. The Angular velocity vector is given by $\bm{\omega} = \omega\zhat$. What is the direction of $\v{L}$?
\begin{center}
    \includegraphics[scale=0.5]{Lecture-18/l18-img1.png}
    \includegraphics[scale=0.5]{Lecture-18/l18-img2.png}
\end{center}
\begin{s}
Direction E. Use the right hand rule with $\v{L} = \v{r} \times \v{v}$ and the add the angular momenta of the two terms.
\end{s}
Follow-up question; consider the body frame where the position of the masses are $(0, a, l)$, $(0, -a, -l)$. What is the $I_{zz}$ component of the inertia tensor?
\begin{s}
Recall that $I_{zz} = \sum_\alpha m_\alpha(x_\alpha^2 + y_\alpha^2)$. Applying the formula, we have that $I_zz = ma^2 + m(-a)^2 = 2ma^2$.
\end{s}
\noindent Next, what is the $I_{xz}$ component of the inertia tensor?
\begin{s}
Recall that $I_{xz} = -\sum_\alpha x_\alpha z_\alpha$. We see that $x = 0$ for both masses so hence $I_{xz} = 0$.
\end{s}
\noindent What's $I_{yz}$?
\begin{s}
Recall that $I_{yz} = -\sum_\alpha m_\alpha y_\alpha z_\alpha$. We therefore have that $I_{yz} = -mal - m(-a)(-l) = -2mal$.
\end{s}
\noindent What is the kinetic energy of the system?
\begin{s}
We use that $T = \frac{1}{2}I_{zz}\omega^2 = \frac{1}{2}(2ma^2)\omega^2 = ma^2\omega^2$.
\end{s}
\noindent \textit{Remark:} We can genrealize this to be $T = \frac{1}{2}\bm{\omega} \cdot \v{L}$.

\subsubsection{Angular momentum for rigid body with angular velocity along arbitrary direction}
We have that:
\[\bm{\omega} = \m{\omega_x \\ \omega_y \\ \omega_z}\]
As well as that:
\[\v{L} = \sum_\alpha m_\alpha \left(\v{r}_\alpha \times (\bm{\omega} \times \v{v}_\alpha)\right)\]
We apply the BAC-CAB rule, that is:
\[\v{A} \times (\v{B} \times \v{C}) = \v{B}(\v{A} \cdot \v{C}) - \v{C}(\v{A} \cdot \v{B})\]
Using this, the above expression for the angular momentum becomes:
\[\v{L} = \m{L_x \\ L_y \\ L_z} = \sum_\alpha m_\alpha \m{(y_\alpha^2 + z_\alpha^2)\omega x & - x_\alpha y_\alpha \omega_y & -x_\alpha z_\alpha \omega_z
\\ -y_\alpha x_\alpha \omega_x & (z_\alpha^2 + x_\alpha^2)\omega_y & -y_\alpha z_\alpha \omega_z
\\ -z_\alpha x_\alpha \omega_x & -z_\alpha y_\alpha \omega_y & (x_\alpha^2 + y_\alpha^2)\omega_z}\]
We may pull out these coefficients and define a moment of inertia matrix/tensor:
\[\II = \m{I_{xx} & I_{xy} & I_{xz} \\ I_{yx} & I_{yy} & I_{yz} \\ I_{zx} & I_{zy} & I_{zz}}\]
Where $\v{L} = \II\bm{\omega}$. Note that this matrix is both real symmetric, as $I_{ij} = I_{ji}$, and hence contains 6 independent elements. We also note that this means $\II^T = \II$ (equal to its transpose). For example, using the definition, we can say that:
\[I_{xx} = \sum_\alpha m_\alpha(y_\alpha^2 + z_\alpha^2)\]
\[I_{xy} = -\sum_\alpha m_\alpha x_\alpha y_\alpha = -\sum_\alpha m_\alpha y_\alpha x_\alpha = I_{yx}\]
We can extend this notion to continuous mass distributions:
\[\II = \int dV\rho(x, y, z)\m{y^2 + z^2 & -xy & -xz \\ -xy & x^2 + z^2 & -yz \\ -xz & -yz & x^2 + y^2
}\]
\subsubsection{Index notation}
Note that when we write $\v{L} = \II\bm{\omega}$, this is equivalent to $L_i = \sum_j I_{ij}\omega_j$. We can also write this compactly by introducing the Kronecker Delta notation:
\[\delta_{ij} = \begin{cases}
1 & \text{if $i = j$}
\\ 0 & \text{otherwise}
\end{cases}\]
Hence we could write the above expression for the inertia tensor more compactly as:
\[I_{ij} = \int dV\rho(x,y,z)\left(\v{r}^2\delta_{ij} - r_ir_j\right)\]

\subsubsection{Example: Components of Inertia Tensor for rotation of cube about corner}
\begin{center}
    \includegraphics[scale=1]{Lecture-18/l18-img3.png}
\end{center}
We have a uniform solid cube of mass $M$ and side $b$, rotating about a corner (the origin). We assume a constant $\rho$ of $\rho = \frac{M}{b^3}$. Calculating $I_{xx}$, we have:
\[I_xx = \rho\int_0^bdx \int_0^bdy \int_0^bdz (y^2 + z^2) = \ldots = \frac{2}{3}Mb^2\]
Note we have pulled out $\rho$ from the integration as these are constant. By symmetry of the object, $I_{xx} = I_{yy} = I_{zz}$. What about the off diagonal terms? Calculating $I_{xy}$ we have:
\[I_{xy} = \rho\int_0^bdx\int_0^bdy\int_0^bdz(-xy) = -\frac{M}{4}b^4\]
And we would expect the other off diagonal elements to again be identical by symmetry. Writing the total inertia tensor, we then have:
\[\II = Mb^2\m{2/3 & -1/4 & -1/4 \\ -1/4 & 2/3 & -1/4 \\ -1/4 & -1/4 & 2/3}\]

\subsubsection{Example: Components of Inertia Tensor for rotation of cube about COM}
\begin{center}
    \includegraphics[scale=1]{Lecture-18/l18-img4.png}
\end{center}
Our bounds of integration will change compared to the last case. Calculating $I_{xx}$, we have:
\[I_{xx} = \rho\int_{-b/2}^{b/2}dz\int_{-b/2}^{b/2}dy\int_{-b/2}^{b/2}dz(y^2 + z^2) = \frac{Mb^2}{6}\]
Again by symmetry, $I_{xx} = I_{yy} = I_{zz}$. We note that this is different from before! Calculating $I_{xy}$, we have:
\[I_{xy} = \rho\int_{-b/2}^{b/2}dz\int_{-b/2}^{b/2}dy\int_{-b/2}^{b/2}dz(-xy) = 0\]
The integral is immediately zero by the fact that the integrand is odd. The same goes for the other off diagonal elements, which yields the final inertia tensor:
\[\II = \frac{Mb^2}{6}\m{1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1}\]
Which is diagonal! This happens to be the case because this is a principal axis of rotation, where the inertia tensor has a particularly simple (diagonal) form.

\subsubsection{Parallel Axis Theorem}
\begin{center}
    \includegraphics[scale=0.5]{Lecture-18/l18-img5.png}
\end{center}
If $I_{ij}$ is the inertia tensor calculated in the CM coordinates, and $J_{ij}$ is the tensor element in the displaced coordinates (where $\v{R} = \v{r} + \v{a}$), then:
\[J_{ij} = I_{ij} + M(a^2\delta_{ij} - a_ia_j)\]

\subsubsection{Example: Applying the Parallel Axis Theorem to the Cube}
For the displacement of coordinates from the center of the mass of the cube to the corner of the cube, we have that:
\[\v{a} = \m{-b/2 \\ -b/2 \\ -b/2}, \quad a^2 = \abs{\v{a}}^2 = \frac{3}{4}b^2\]
Then, for the diagonal elements we have:
\[M(a^2\delta_{ii} - a_ia_i) = M\left[\frac{3}{4}b^2 - \frac{-b}{2}\cdot\frac{-b}{2}\right] = \frac{Mb^2}{2}\]
And for the off diagonals:
\[M(a^2\delta_{ij} - a_ia_j) = M\left[-\frac{-b}{2}\frac{-b}{2}\right] = -\frac{Mb^2}{4}\]
Hence calculating the inertia tensor about the corner of the cube (in the displaced coordinates) we get:
\[\mathbb{J} = Mb^2\m{1/6 & 0 & 0 \\ 0 & 1/6 & 0 \\ 0 & 0 & 1/6} + Mb^2\m{1/2 & -1/4 & -1/4 \\ -1/4 & 1/2 & -1/4 \\ -1/4 & -1/4 & 1/2} = Mb^2\m{2/3 & -1/4 & -1/4 \\ -1/4 & 2/3 & -1/4 \\ -1/4 & -1/4 & 2/3}\]
Which agrees with the result obtained from the direct calculation.

\subsubsection{Principal axes}
If $\v{L} = \lambda\bm{\omega}$ for a scalar $\lambda$, the body rotates around one of its principal axes. $\lambda$ is the "Moment of inertia" about that axis. So if:
\[\II = \m{\lambda_1 & 0 & 0 \\ 0 & \lambda_2 & 0 \\ 0 & 0 & \lambda_3}\]
Then the chosen axes are the principal axes and $\lambda_i$ are the principal moments. For any rigid body and any point $O$, there are three perpendicular axes with respect to which the inertia tensor is diagonal! This is a consquence of the fact that the moment of inertia tensor has real entries and is symmetric.
\newline Which of the following statements are a consequence of the fact that the inertia tensor is a 3x3 matrix with real positive eigenvalues and orthogonal eigenvectors?
\begin{enumerate}
    \item The matrix can be diagonalized
    \item The matrix of eigenvectors is an orthogonal matrix
    \item The matrix of eigenvectors is a rotation matrix (if properly normalized)
    \item In the coordinate system aligned with the eigenvectors, the tensor is diagonal.
\end{enumerate}
\begin{s}
    All 4 are correct.
\end{s}
\end{document}


\documentclass[12pt]{article}

\usepackage[left=1cm,right=1cm,
    top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage[T2A]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\parindent=0.5cm

\usepackage{hyperref}
\usepackage{indentfirst}

\numberwithin{equation}{section}
%\usepackage{showkeys}

\begin{document}
\section*{MSAI Statistics \& Probability – Week 1 Seminar \& HW}\\

\textbf{Problem 1:} Two dice, each having $M$ faces, are thrown. Each die has numbers from $1$ to $M$ written on its faces. Find the probability that the sum of two numbers equals $i$. Provide the answer for $M=10,$ and $i=15.$
\\

\textbf{Problem 2:} A random subset is chosen from the set $\{1,\dots,N\}.$ Find the probability that the cardinality (number of elements) of thus subset is an even number.
\\

\textbf{Problem 3:} Two subsets, $A_1$ and $A_2,$ of $\{1,\dots,n\}$ are chosen randomly (they may coincide). Find the probability that $A_1 \cap A_2 = \emptyset$
\\

\textbf{Problem 4:}
An algebra is a set of subsets of $\Omega$ that 1) contains $\Omega$, 2) is closed under complement (if $A$ is in the algebra – $\bar{A},$ its complement, is also) and union (if $A$ and $B$ are in the algebra – $A\cup B$ is as well). Can these two operations from point 2) be replaced with
\begin{enumerate}
    \item $\Delta$ (symmetric difference) and $\textbackslash$ (set difference, also called relative complement)?
    \item $\overline{A\cap B}$ (just one operation, complement of the intersection)?
\end{enumerate}
\\

\textbf{Problem 5:} Let $\mathcal{F}_1,~\mathcal{F}_2$ be $\sigma$-algebras. Is it true that 1) $\mathcal{F}_1\cap\mathcal{F}_2$ and 2)$\mathcal{F}_1\cup\mathcal{F}_2$ are $\sigma$-algebras?

\end{document}


\chapter{n-step bootstrapping}
\section{Summary}

\subsection{n-step TD prediction}

Monte Carlo updates the estimate $V(S)$ using the complete return (equation~\ref{eq:complete return of an episode}. TD(0) only watches one step ahead, a compromised is too take an n-step prediction window as illustrated by equation~\ref{eq:return estimate for n step method}. The state learning algorithm them becomes equation~\ref{eq:state learning algorithm n-step return}.

\begin{equation}
G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ... + \gamma^{T-t-1}R_T
\label{eq:complete return of an episode}
\end{equation}

\begin{equation}
G_{t:t+n} = R_{t+1} + \gamma R_{t+2} + ... \gamma^{n--1} R_{t+n} + \gamma^nV_{t+n-1}(S_{t+n})
\label{eq:return estimate for n step method}
\end{equation}

\begin{equation}
V_{t+n}(S_t) = V_{t+n-1}(S_t) + \alpha [G_{t:t+n} - V_{t+n-1}(S_t)]
\label{eq:state learning algorithm n-step return}
\end{equation}

\subsection{n-step Sarsa}

The previous Sarsa is often called Sarsa(0), the generalized version is call n-step Sarsa. The return value can be estimated by equation~\ref{eq:return estimate for n step sarsa}. The update rule for Q then becomes equation~\ref{eq:n step sarsa learning algorithm}.

\begin{equation}
G_{t:t+n} = R_{t+1} + \gamma R_{t+2} ... \gamma^{n-1} R_{t+n-1} \gamma^n Q_{t+n-1}(S_{t+n}, A_{t+n})
\label{eq:return estimate for n step sarsa}
\end{equation}

\begin{equation}
Q_{t+n}(S_{t}, A_{t}) := Q_{t+n-1}(S_t, A_t) + \alpha \left[G_{t:t+n} - Q_{t+n-1}(S_t, A_t)\right]
\label{eq:n step sarsa learning algorithm}
\end{equation}

The same logic can be applied to expected SARSA. The value function can be defined as equation~\ref{eq:expected n step sarsa value function}. Using equation~\ref{eq:expected n step sarsa value function} the n-step expected sarsa return value is defined by equation~\ref{eq:n step expected sarsa return value}. The update is still equation~\ref{eq:n step sarsa learning algorithm} but using the new $G_{t:t+n}$ from equation~\ref{eq:n step expected sarsa return value}.

\begin{equation}
V_t(S_t) = \sum_a P(a) Q(S_t, a)
\label{eq:expected n step sarsa value function}
\end{equation}

\begin{equation}
G_{t:t+n} = R_{t} + \gamma R_{t+1} + ... \gamma^{n-1} R_{t+n-1} + V_t(S_{t+n}, A_{t+n})
\label{eq:n step expected sarsa return value}
\end{equation}

\subsection{n-step Off-policy Learning}

\begin{equation}
Q_{t+n}(S_{t}, A_{t}) := Q_{t+n-1}(S_t, A_t) + \rho_{t:t+n} \alpha \left[G_{t:t+n} - Q_{t+n-1}(S_t, A_t)\right]
\label{eq:n step off policy sarsa learning algorithm}
\end{equation}

\begin{equation}
V_{t+n}(S_t) = V_{t+n-1}(S_t) + \rho_{t:t+n-1} \alpha [G_{t:t+n} - V_{t+n-1}(S_t)]
\label{eq:state learning algorithm n-step off policy return}
\end{equation}

\begin{equation}
\rho_{t:h} = \prod_{k=t}^{min(h, T-1)} \frac{\pi(A_k|S_k)}{b(A_k|S_k)}
\label{eq:n step off policy sarsa importance sampling ratio}
\end{equation}

\subsection{Per Decision  Methods with Control Variates}

The previous sections are inefficient implementations of the n-step algorithm. The n-step return can be written recursively as $G_{t:h} = R_{t+1} + \gamma G_{t+1:h}$, with $G_{h:h}=V_{n-1}(S_h)$. The importance sampling weighting is still $\rho_t = \frac{\pi(S_t, A_t)}{b(s_t, A_t)}$. Using this recursive definition we can define the return as equation~\ref{eq:off policy definition of return estimate with control variate}.

\begin{equation}
G_{t:h} = \rho_t(R_{t+1} + \gamma G_{t+1:h}) + (1-\rho) V_{h-1}(S_t)
\label{eq:off policy definition of return estimate with control variate}
\end{equation}

The term $(1-\rho) V_{h-1}(S_t)$ in equation~\ref{eq:off policy definition of return estimate with control variate} is called the control variate. It has an expected value of one because $\EX[\rho]=1$ so $\EX[1-\rho]=0$.

The return state of a n-step off policy with control variate is defined by equation~\ref{eq:n step off policy form with control variate}. The recursion ends with $G_{h:h}=Q_{h-1}(S_h, A_h)$

\begin{equation}
G_{t:h} = R_{t+1} + \gamma [ \rho_{t+1} G_{t+1:h} + V_{h-1}(S_{t+1})-\rho_{t+1}Q_{h-1}(S_{t+1}, A_{t+1})]
\label{eq:n step off policy form with control variate}
\end{equation}

\subsection{Off-Policy Learning Without Importance Sampling: The n-step Tree Backup Algorithm}

Instead of only using the value estimations of the actual path taken in n-steps. We can generalize the expected SARSA algorithm, from equation~\ref{eq:one step return expected sarsa} to equation~\ref{eq:tree backup rule n-step return}.

\begin{equation}
G_{t:t+1} = R_{t+1} + \gamma \sum_{a} \pi (a | S_{t+1})Q_t(S_{t+1}, a)
\label{eq:one step return expected sarsa}
\end{equation}

\begin{equation}
\begin{split}
G_{t:t+n} & = R_{t+1} + \gamma \sum_{a \neq A_{t+1}} \pi (a|S_{t+1})Q_{t+n-1}(S_{t+1},a) + \gamma \pi(A_{t+1}|S_{t+1})G_{t+1:t+n} \\
& = R_{t+1} + \gamma \pi (A_{t+1}| S_{t+1}) [G_{t+1:h}-Q_{h-1}(S_{t+1}, A_{t+1})] + \gamma V_{h-1} (S_{t+1})
\label{eq:tree backup rule n-step return}
\end{split}
\end{equation}

Adding the control variate to Equation~\ref{eq:tree backup rule n-step return} gives equation~\ref{eq:n-step sigma}.

\begin{equation}
G_{t:t+n}  = R_{t+1} + \gamma \pi (\sigma_{t+1}\rho_{t+1} + (1-\sigma_{t+1})\pi(A_{t+1}|S_{t+1})) [G_{t+1:h}-Q_{h-1}(S_{t+1}, A_{t+1})] + \gamma V_{h-1} (S_{t+1})
\label{eq:n-step sigma}
\end{equation}

\subsection{*A Unifying Algorithm: n-step Q(Sigma)}

\section{Exercises}

\subsection{Exercise 7.1}

The Monte carlo error can be written as a sum of TD errors, with TD(0) this becomes:

$
G_t = R_{t+1} + \gamma G_{t+1}
$

$
\delta{t} = R_{t+1} + \gamma V(S_{t+1}) - V(S_t)
$

$
G_t - V(S) = R_{t+1} + \gamma G_{t+1} - V(S_{t}) = \sum^{T-1}_{k=t} \gamma^{k-1}\delta_k
$\\

With an n step we get:

$
G_t = R_{t+1} + \gamma G_{t+1}
$

$
\delta{t} =  \sum_{k=1}^{n} \gamma^{k-1} R_{t+k} + \gamma^{n}V(S_{t+n}) - V(S_{t})
$\\

By putting them together we get:

$
G_t - V(S_t)\\
= R_{t+1} + \gamma G_{t+1} - V(S_t)\\
= R_{t+1} + \gamma G_{t+1} - V(S_t)\\
+  \sum_{k=2}^{n} \gamma^{k-1} R_{t+k} - \sum_{k=2}^{n}\gamma^{k-1} R_{t+k}\\
+ \gamma^{n}V(S_{t+n}) - \gamma^{n}V(S_{t+n})\\
= \delta_t + \gamma (G_{t+1} - \gamma^{n-1}V(S_{t+n}))  - \sum_{k=2}^{n}\gamma^{k-1} R_{t+k}
$

I don't see how to continue from here.

\subsection{Excercise 7.3 page 145}
\textbf{question: Why do you think a larger random walk task (19 states instead of 5) was in the examples of this chapter? Would a smaller walk have shifted the advantage to a different value of n? How about the change in left-side outcome form 0 to -1 made in the larger walk? Do you think that made any difference in the best value of n?}

If a random walk with length of 5 was used the results would not be best around an n of 4 or 8. As a you need a large enough episode to learn from a step of 4 or 8 samples.

When the walk is smaller, smaller n's will give better results then current results.

If the left side is negative, the first time the walk will go into it. That negative result will propagate n-steps, instead of 1 with TD(0).

\subsection{Excercise 7.4 page 148}
Similar to excercise 7.1, still TODO, first finish 7.1.


\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{inputenc}
\begin{document}
\section{Pre 9.1}
\textbf{Pages} 184-185:
On page 184,:
\textit{If $a_2$ is deleted, we maintain .....}
Error: c=A-CGC
Correction: c=A-TGC
Why?: The statement says $a_2$ is deleted, which is correctly depicted by $c$
but then it also makes an erroneous substitution at $a_3$

\it
On page 185:
\textit{Define $S=S'-5 log s$ ....}
Error: $\delta=log(\sqrt(s)/r)$
Correction: $\delta=log(s/r)$
WHY?:
Let's start from scratch:\\
Given: $S=S' -5\ log\ s$ and $log(p/s)=1$
\begin{subequations}
\label{eq:185}
S'= 3\ log\ p + log\ q + 2\ log\ r \\
S = S'-5 log\ s = 3(log\ p - log\ s) + (log\ q - log\ s) + 2(log\ r - log\ s)  \\
S = 3 - $\mu$ - 2$\delta$
\end{subequations}

Comparing \ref{eq:185}b and \ref{eq:185}c we see: \\
$\delta = log(s/r)$
\end{document}


\subsection{Classification}
\label{sec:impl_classification}

The first naive idea, which assigns a cluster, i.e., writing style group, to a blog post, is to calculate the \textit{Euclidean} distance from the blog post to the central points of each cluster.
The resulting cluster would then be the one with the lowest distance.
While this method is computationally simple and fast, it might not provide the best possible results.
Therefore we looked at more sophisticated approaches that were used for similar problems before.
We applied two of those methods, a \textit{k-nearest neighbor} algorithm~\cite{peterson2009k} and a \textit{support vector machine}~\cite{kolari2006svms}.
The training data for these algorithms is based on the clustering results, as described in Section~\ref{sec:clustering}.

\subsubsection{K-Nearest Neighbor}
\label{sec:k_nearest_neighbor}

The \textit{k-nearest neighbor} algorithm selects the $k$ vectors from the training data, which are closest to the feature vector of the new blog post.
Then it returns the cluster that is prevalent amongst these neighbors as a result.
For example consider the scenario depicted in Figure~\ref{fig:naive}.
The naive method of calculating the \textit{Euclidean} distance between the cluster center and the feature vector of the new blog post would have cluster 2 as a result.
However the \textit{k-nearest neighbor} algorithm with, for example $k=5$, returns cluster 1, because four out of the five closest neighbors belong to cluster 1.
Intuitively, this appears as the better solution, because the feature vector of the new blog post seems to be naturally belonging to cluster 1.

\begin{figure}[ht!]
    \centering
    \includegraphics[width=0.6\textwidth]{images/naive.pdf}
    \caption{The naive approach, which calculates the \textit{Euclidean} distance to the center of the cluster, would put the new blog post into cluster 2. The \textit{k-nearest neighbor} algorithm would return cluster 1 as a result.}
    \label{fig:naive}
\end{figure}

\subsubsection{Support Vector Machine}
\label{sec:support_vector_machine}

A \textit{support vector machine} creates a model to distinguish between the different clusters of the training data by calculating borders between them.
These borders can be thought of as functions in the same vector space as the feature vectors.
If the \textit{support vector machine} uses a linear model, this might look such as shown in Figure~\ref{fig:svm}, represented in a two-dimensional graph.
Both, the dotted and the dashed line serve as examples for a possible border, but the \textit{support vector machine} should usually tend to use the model with more distance to all feature vectors.
In this case, the dashed line would more accurately divide cluster 1 and 2.

We used the \textit{support vector machine} implementation LIBSVM\footnote{\url{http://www.csie.ntu.edu.tw/~cjlin/libsvm/}}, as described by Fan et al.~\cite{fan2005working}.
We followed the configuration procedure proposed by Hsu et al.~\cite{hsu2003practical}.
In the end we used the default configuration of type C-SVC and a radial basis function kernel for the \textit{support vector machine}.
The only parameter we adapted was $c=1000$, which was done using cross-validation, as recommended by Hsu et al. to prevent overfitting on our data set.

\begin{figure}[ht!]
    \centering
    \includegraphics[width=0.6\textwidth]{images/svm.pdf}
    \caption{Two differently configured \textit{support vector machines} might provide the two different lines as borders between the different clusters. The dotted line would result in the blog post belonging to cluster 1, while the dashed line would return cluster 2 as a result.}
    \label{fig:svm}
\end{figure}