\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{listings}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\newtheorem{theorem}{Theorem}[section]

\DeclarePairedDelimiter \abs{\lvert}{\rvert} % short cut for absolute value

\setlength{\parindent}{0em}
\setlength{\parskip}{0.5em}

\title{Chapter 2: Algorithm Analysis}
\author{Yangtao Ge}
\date{\today}

\begin{document}
\maketitle
\begin{abstract}
This section discusses about:
\begin{itemize}
    \item how to estimate the time required for a program
    \item how to reduce running time of a program
    \item The result of careless use of recursion
    \item very efficient algorithms to raise a number to a power
    \item compute GCD
\end{itemize}
\end{abstract}
\section{Mathematical Background}
Four definitions of the framework:
\begin{definition}[Upper bound]
    $T(N) = O(f(N))$ if there are positive \emph{constants} $c$ and $n_0$ such that $T(N) \leq cf(N)$
    when $N \geq n_0$
\end{definition}

\begin{definition}[Lower bound]
    $T(N) = \Omega(g(N))$ if there are positive \emph{constants} $c$ and $n_0$ such that $T(N) \geq cg(N)$
    when $N \geq n_0$
\end{definition}

\begin{definition}[Envelope]
    $T(N) = \Theta(h(N))$ iff $T(N) - O(h(N))$ and $T(N) = \Omega(h(N))$
\end{definition}

\begin{definition}
    $T(N) = o(p(N))$ if for \textbf{\textit{all}} positive constants $c$ there exists an $n_0$ such that
    $T(N) < cp(N)$ when $N > n_0$.
\end{definition}
\emph{Ref: p.30 for detail theorem}

Typical growth rates:\newline
\begin{tabular}{|p{3cm}|p{5cm}|}
    \hline
    Function & Name\\
    \hline
    \hline
    $c$ & Constant \\
    $\log_{}N$ & Logarithmic \\
    $\log^2_{}N$ & Log-Squared \\
    $N$ & Linear \\
    $N\log_{}N$ & \ \\
    $N^2$ & Quadratic \\
    $N^3$ & Cubic \\
    $2^N$ & Exponential \\
    \hline
\end{tabular}

Some Theorem from the definition:
\begin{theorem}
    If $T_1(N) = O(f(N))$ and $T_2(N) = O(g(N))$, then:
    \begin{enumerate}
        \item $T_1(N) + T_2(N) = O(f(N) + g(N))$
        \item $T_1(N) * T_2(N) = O(f(N) * g(N))$
    \end{enumerate}
\end{theorem}

\begin{theorem}
    If $T(N$) is a polynomial of degree $k$, then $T(N) = \Theta(N^k)$
\end{theorem}

\begin{theorem}
    $\log^k_{}N = O(N)$ for any constant $k$, which tell us `Logarithms grow very slowly
\end{theorem}

For big-O notation answers: \underline{Lower-order terms can generally be ignored}
e.g. $f(N) = 2N^2 + N$ then its big-O notation is $T(N) = O(N^2)$

For \emph{relative growth rates} of two Function, we using `\emph{L'Hopitals's rule}' to determine it:
\begin{theorem}[L'Hopitals's rule]
    If $\lim_{N\to\infty}f(N) = \infty$ and $\lim_{N\to\infty}g(N) = \infty$,
    then $\lim_{N\to\infty} \frac{f(N)}{g(N)} = \lim_{N\to\infty} \frac{f'(N)}{g'(N)}$
\end{theorem}

Four possible results:
\begin{itemize}
    \item Limit is 0: $f(N) = 0(g(N))$
    \item Limit is $c\neq 0$: $f(N) = \Theta(g(N))$
    \item Limit is $\infty$: $g(N) = o(f(N))$
    \item Limit does not exist: No relations
\end{itemize}

\section{Model}
Basically a normal computer which execute instructions sequentially. And it has inifinite memory

\section{What to Analyze}
What we focus:
\begin{itemize}
    \item Input size
    \item Running time
\end{itemize}

What we use:
\begin{itemize}
    \item $T_{avg}(N)$ : average running time -- reflect typical behaviour.
    \item $T_{worst}(N)$: worst running time -- a guarantee for performance on any possible input
\end{itemize}
\emph{Ref: pp. 33 - 35} (Maximum subsequence Sum Problem)

\section{Running Time Calculations}
We only focus on \emph{big-O notation} answers.

\subsection{A simple Example}
Calculations of $\sum_{i=1}^N i^3$. The code is as follows:
\begin{lstlisting}[language=Java]
    public static int sum(int n){
        int partialSum = 0;           // Line 1
        for(int i = 1; i <= n; i++){  // Line 2
            partialSum += i * i * i;  // Line 3
        }
        return partialSum;            // Line 4
    }
\end{lstlisting}

\underline{Analysis:}
\begin{itemize}
    \item Line 1: 1 time for assignment
    \item Line 2: 2N + 2 times in total:
    \begin{itemize}
        \item 1 time for initialization
        \item N + 1 times for comparison
        \item N times for increment
    \end{itemize}
    \item Line 3: 4N times in total(2 multiplication, 1 addition, 1 assignment)
    \item Line 4: 1 time for return
\end{itemize}
In total $6N + 4$, so we say that it is a $O(N)$ method

\subsection{General Rules}
\begin{enumerate}
    \item `For' loops: $T = T_{statements} * iterations$ (at most)
    \item `Nested' loops: $T = T_{statements} * \prod_{}SizeOfLoop$ (Analysis them inside-out)
    \item Consecutive statements: Use `big-O' notation add \newline
    e.g. $O(N)$ followed by $O(N^2)$ is still $O(N^2)$
    \item `if-else': $T = \max(T_{Stat1}, T_{Stat2})$ (sometimes overestimate, but never underestimate)
\end{enumerate}

\subsection{Solution for the Problem of Maximum subsequence Sum}
Four algorithms are provided here (\emph{Ref: pp.39 - 49})

\subsection{Logarithms in the Running Time}
Some Logarithms algorithms:
\begin{itemize}
    \item \emph{divide-and-conquer} algorithms: $O(N\log_{}N)$ time
    \item General rules:
    \begin{itemize}
        \item $O(\log_{}N)$: takes constant $O(1)$ time to cut the problem size by a fraction (usually $1/2$)
        \item $O(N)$: constant time requred to merely reduce the problem by a constant amount
    \end{itemize}
\end{itemize}

Following subsection are some common Logarithmic algorithms:

\subsubsection{Binary Search}

\subsubsection{Euclid's Algorithm}

\subsubsection{Exponentiation}

\subsection{A Grain of Salt}
Sometimes, Worst case is better than average case:
\begin{itemize}
    \item average case is very complex
    \item Analysis needs to be tightened
    \item average running time is less significant than worst case running time
\end{itemize}

\end{document}


\section{Introduction}
\label{sec:introduction}

The trading pit of a stock exchange is often imagined by outsiders as a frenzy place, with telephones constantly ringing and traders shouting orders across the room at a frenetic rhythm. This was probably the reality thirty years ago, when open outcry was still the main communication system between pit traders. Since then the floors have become more and more quiet as the majority of the orders moved to electronic trading systems. Notwithstanding, investment decisions were still made by humans who could now execute their orders without passing through the pit traders. In the last decade, the markets have witnessed the widespread adoption of \emph{Automated Trading Systems} (ATS), that can make investment decisions in a fully automatized way at speeds with orders of magnitude greater than any human equivalent. In 2014, more than $75\%$ of the stock shares traded on United States exchanges were originated from ATS orders and this amount kept growing since then. Quantitative hedge funds, such as Renaissance Technologies, D.E. Shaw, Citadel and many others, are employing mathematicians, physicists and other scientists to develop algorithms able to extract trading signals from large amount of data and automatically trade. These algorithms are typically based on advanced statistics, signal processing, machine learning and other fields of mathematics. However, few of these hedge funds publish their profit-generating ``secret sauce'' and not much can be found in the literature. In this project we develop an automated trading algorithm based on \emph{Reinforcement Learning} (RL), a branch of \emph{Machine Learning} (ML) which has recently been in the spotlight for being at the core of the system who beat the Go world champion in a 5-match series \cite{silver2016mastering}.\\
This document is organized as follows. In Section \ref{sec:basics_reinforcement_learning} we introduce the basic concepts of RL and present two learning algorithms that allow two determine an approximation for the optimal policy of a sequential decision problem. In section \ref{sec:application_to_systematic_trading} we discuss the asset allocation problem from a mathematical point of view and show how these learning algorithms can be applied in this setting. In Section \ref{sec:python_prototype} we start discussing the implementation of the model in Python, which has been used during the prototyping phase. In Section \ref{sec:c++_implementation} we discuss a more efficient C++ implementation. In Section \ref{sec:execution_pipeline} we describe the execution pipeline used to run the learning experiment. In Section \ref{sec:numerical_results} we present the numerical results for a synthetic asset, whose price follows a particular stochastic process. In Section \ref{sec:conclusion} we conclude with some final remarks and we discuss some future research directions.


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumerate}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{March 26, 2014}
\maketitle
\section*{lesson 17}
this stuff is like the prototype solution setting up method of characteristic lines, we'll see more of this later.

Today we look at Alembert's solution
\begin{align*}
  &\text{PDE}&&\qquad u_{tt}=c^2u_{xx}&c&>0&\infty<&x<+\infty&0<&t<\infty\\
  &\text{IC}&&\left.
  \begin{aligned}
    u(x,0)&=f(x)\\
    u_t(x,0)&=g(x)
  \end{aligned}
  \right\}&&&\infty<&x<+\infty
\end{align*}
change independent variables
\begin{align*}
  \xi&=x+ct\\
  \eta&=x-ct
\end{align*}
point will get $u_{\xi\eta}$

See page 130(146) for more explanation
\end{document}


\chapter{Discussion}
\label{ch:7}
In this chapter, we seek to contextualize the results from the preceding chapter.
We apply theory from \Cref{ch:2} and compare the result to the expected behavior
% expect from what we know about their theoretical construction/base.

\section{Similarity Scores}

We have established that the measures define what makes trajectories similar.
Some of the measures have a similar base idea, and thus we expect to see this reflected in the results.
From the rankings of most similar pairs, there are a few things that stand out and we will go over them here.

%We reiterate this is not the case for all of them, and consequently, not all columns of TABLE can be used in this manner.

First of all, EDR with $\epsilon$ set to 0.203 was the column in \Cref{tab:top-10-sim-pairs} which had the least number of frequent “most similar pairs”.
When adjusting the parameter down to 0.101, our approximation of the recommend parameter value, all of its eight most pairs were found elsewhere in the table.
Recall that EDR’s parameter is the threshold distance that determines if points are “matching”.
The similarity ranking with the lower parameter value aligns more with the other measures, and this could indicate that the other threshold was set too high.
We would presume that EDR would give a similar ranking as the other noise-tolerant edit-distance inspired method, MSM.
From what can be seen in the table, this appears to hold, but it varies with its parameter value too.

%   that would have similar values to the

Next, we note that the Hausdorff distance generated similar pairs which were quite different from the other measures.
As the Hausdorff metric is parameter-free, we reckon that the reason its results stood out was that it defined trajectory similarity in a different manner than the rest.
We established that the Hausdorff distance does not take into account the direction of a trajectory and the only other measure that is invariant to direction is SSPD.
Indeed they share many of the top 10 ranked scores, however, SSPD distinguishes itself Hd as it reduces the similarity score to a single element-element pair making altering their internal rankings again.

% Next we observe that the Hausdorff distance contains the second least “frequent pairs” and as it parameter free, we reason that there must be something with how it defines trajectory similarity that makes it stand out. Recall that the Hausdorff distance stood out as the only measure that did not take into account the direction of a trajectory. Furthermore, it stands out as the only measure that reduces the similarity distance to an element-element pair.

The final observation from \Cref{tab:top-10-sim-pairs} we noted was that Euclidean distance, ERP, and one of the MSM measures gave the exact same top 10 pairs.
Upon closer examination, it turned out that their ranking remained the same until the 44th pair, and the similitude between the measures continued beyond that.
MSM with cost value 1 and the Euclidean distance's ranking match for 9456 of the 10 thousand most similar trajectory pairs.
We reason that this occurred as a consequence of the cost of Splits and Merges being set too high, making MSM  default to the Move operation whose cost is the $L_2$ norm between trajectory elements.
As for ERP, we reason that setting the reference point, $g$, to the origin, increased the cost for trajectory elements that were far away.
Again, making the method depend on the $L_2$ norm between trajectory elements as the cost of an edit.

\section{Davies-Bouldin Results}

The essence of this thesis is to examine how the definition of similarity varies with different measures.
Clustering is a manner of grouping the trajectories such that traits which are seen as the most defining characteristics under various definitions get highlighted.

As stated in \Cref{ch:2}, there are a number of cluster evaluation techniques that aim to numerically rank the quality of clustering results.
One of these is the DB-index however we will not an assumption regarding which type of type similarity was the most “correct” one.
The ranking created by this criterion is not invalid, but the insight it adds to is limited.
Nevertheless, we have chosen to leave in this stage of the experiment and in the report on the account of the efforts that were put into computing it as well as its natural role as a starting point for analyzing the clusters.

Our version of Davis-Bouldin favors clusterings where each trajectory element differs as little as possible from the mean of all trajectory elements when computed in a. A consequence of this is that the cluster can appear fuzzy since the placement of the surrounding points does not affect the index.
From \Cref{tab:cluster-dbidx} it appears as if the lock-step method and EDR with threshold parameter $\epsilon=0.203$ were the best performing methods, and this is as expected.

Moreover, methods that do not take into account order observations, or methods that would reorder the trajectory elements received low DB-index rankings. In \Cref{fig:cluster-best-worst-db-ap,fig:cluster-best-worst-db-h}  the clusters that were generated by the measures which received highest and lowest DB-indexes are drawn. These figures illustrate the bias of our DB-index implementation.

\begin{figure}[h!]
  \centering
  \hspace{1em}
   \begin{subfigure}[c]{0.37\linewidth}
     \includegraphics[width=\linewidth]{figs/clusters/CLU_AP_ALL[EDR;e=.203].png}
    \caption{EDR;$\epsilon=0.203$, highest rank}
  \end{subfigure}
  \hfill
  \begin{subfigure}[c]{0.37\linewidth}
    \includegraphics[width=\linewidth]{figs/clusters/CLU_AP_ALL[SSPD].png}
    \caption{SSPD, lowest rank}
  \end{subfigure}
  \caption{The AP-clusters of the measures the best and wort Davies-Bouldin indexes.}
  \label{fig:cluster-best-worst-db-ap}
\end{figure}

\begin{figure}[h!]
  \centering
  \hspace{1em}
   \begin{subfigure}[c]{0.4\linewidth}
     \includegraphics[width=\linewidth]{figs/clusters/CLU_H_ALL[ERP;g=0,0].png}
    \caption{ERP, $\epsilon=0.203$, highest rank}
  \end{subfigure}
  \hfill
  \begin{subfigure}[c]{0.4\linewidth}
    \includegraphics[width=\linewidth]{figs/clusters/CLU_H_ALL[SSPD].png}
    \caption{SSPD, lowest rank}
  \end{subfigure}
  \caption{The HCA-clusters of the measures the best and wort Davies-Bouldin indexes.}
  \label{fig:cluster-best-worst-db-h}
\end{figure}

As a consequence of the unreliability of the DB-index, the main tool for examining the clusters will be visual inspection.
This will allow us to account for the different aspects of trajectory similarity and formulate conclusions accordingly.
Re-examining \Cref{fig:cluster-best-worst-db-ap,fig:cluster-best-worst-db-h} with visual inspection it would appear that SSPD produced cleaner and fewer clusters than both EDR and ERP.
This is in stark contrast to the ranking created by the Davies-Bouldin criteria under both HCA and AP analysis.

This evaluation alternative comes with its own drawbacks, one of which is the matter of subjectivity.
At the same time, it has been stated that human assessment is an important component for determining the quality of clusters\cite{79-SilhouetteAnalysis}.
We will not be declaring any measure better than the others as our results do not provide support for an accurate ranking.

% We cannot declare one measure just better than all others from the evaluation that we executed here as it makes no sense. This is in large part due to the inherent bias of using a strict lockstep method to evaluate the final clusters.
% By visual inspection, SSPD produced cleaner and fewer clusters than EDR, $\epsilon=0.203$ under AP, despite having a much worse criterion. This pattern repeats itself under HCA when predetermined even with  the amount of final clusters. EDR  as well \Cref{fig:cluster-best-worst-h-db}.

% Based on those arguments, the main tool for examining the clusters will be visual inspection. This will allow us to account for the different definitions/aspects of similarity and formulate conclusions in an aggregate manner.
% This method comes with its own drawbacks, one of which is the matter of subjectivity. Yet as brought up in supported previous works[CITATIONS], human assessment key for determining the quality of clusters.

\section{Cluster Behavior}

First, we describe what we expected the clusters to look like based on the properties of the specific similarly distance function.
For the methods that operate with a parameter value, we discuss how changing that value would affect the cluster results.
Then we compare our expectations to the clusters themselves.
Affinity propagation is a was the parameter-free method, thus its results are weighted more.
On the other hand, the number of hierarchical clusters was arbitrarily set, leading us to use its results as a supplementary evaluation.

% https://www.wordhippo.com/what-is/another-word-for/foreseeable.html
\subsection{Expectations}
In general, there are three factors that will affect how we expect the final clusters to appear.
Measures that are context-aware should calculate fewer fuzzy clusters.
Next, measures that account for local time shifts should group trajectories that have similar sub-sections but with an offset creating a wider band of similarity.
Lastly, we expect the noise-sensitive trajectories to create fuzzier trajectories.

\textbf{Euclidean Distance} is the only lock-step measure, and trajectories that are in the same region might receive an artificially high similarly score by nature of being near each other.
A trajectory pair will be rewarded if they have elements at the same index which are near each.
This would create fuzzy clusters as each point evaluated isolation and taking the average of the pairwise element-distances will artificially smooth out the distances.
An extra amount of fuzziness is expected to come from the fact that this measure is sensitive to noise.

% WE COULD observe that there are trajectories that oscillate around the same region at a similar pace- giving them a lock-step similar score, but the cluster ends up looking fuzzy.

\clearpage
\textbf{Dynamic Time Warping}  optimizes for local shape similarity and warps trajectories to reflect similarity after shape-preserving transformations.
We expect the final clusters to appear fuzzy at first glance as the local similarities would be out of sync.
Yet upon closer examination, we should be able to spot bands of cohesive trajectory sections.
DTW is a noise-sensitive method, so we would expect some level of fuzziness to be present, however, the clusters should be crisper than that of the Ed.

From \textbf{Hausdorff Distance}, we would expect crisper clusters than both Ed and DTW if we could guarantee the data free of noise.
The maximizing over a minimum of all element-element pairings means that the overall shape similarity is weighted in a way that the other measures do not account for.
In other words, the global resemblance matters more under this definition.
However, the data set we used is not noise-free so while we expect some crispness, there will be fuzziness of the clusters due to its noise sensitivity.

% All of the trajectories' points have to be near each other for if the pair is to receive a high similarity score.
% stemming from the reduction of similarity distance to distance between two trajectory elements.

One of the key features of \textbf{Symmetrized Segment-Path Distance} is that it accounts for whole trajectory shape similarity.
This means that we expect it to generate the crispest looking clusters.
The expectation of crisp clusters is further substantiated by its tolerance to noise
This method aimed to be invariant to the physical locations, meaning that trajectories of similar shapes with different origins could be grouped together. This could result in some broader bands of similarity.

% All of the parametric measures adapt to local time shifts like in the same manner as DTW, and  therefore we expect the clusters they generate exhibit some of the same type of fuzziness. This, of course, is not the only factor that affects how fuzzy or crisp the clusters become. The choice of parameter, as well as the (internal workings) of the algorithms matter greatly.
%SOMETHING ABOUT HOW DTW ERP etc have re-s sim to dtw as noted in \cite{26}

Recall that \textbf{Edit Distance on Real Sequences} uses the parameter $\epsilon$ to as the matching threshold for how close how two trajectory elements are.
Decreasing the parameter value leads to an increase in strictness in how close elements have to be, and in turn, would lead to crisper clusters.
It goes without saying that a too restrictive $\epsilon$ would no longer be accurate; if no trajectory elements are matching, the only basis for similarity would be the trajectory lengths.
Clusters are expected to have some level of fuzziness as EDR considers the trajectory element in isolation.

\textbf{Edit Distance with Real Penalty} was computed with one parameter value.
We have established that this metric is sensitive to noise and that it accommodates local time shifts.
As EDR, this measure does isolates the trajectory elements when computing the similarity distance.
This means that there are three factors that contribute to fuzzy clusters, thus we expect the fuzziest clusters to be generated from this metric.
% As it is a measure that handles local time shifts and does not compare a trajectory as a whole, we expect the clusters to appear quite fuzzy. This expectation is further supported by its low tolerance to noise.

\textbf{Move-Split-Merge} handles local time shifts in the same manner as DTW and the other Edit Distance-based measures.
We expect to observe trajectory sections that match and create wider bands.
MSM distinguishes itself by accounting for the values that surround a given element when computing the similarity score.
This means that we expect the clusters are crisper than those of generated by EDR and ERP.
The parameter of MSM determines to which degree Splits and Merges are favored over Moves.
As the cost increases, they will increasingly be evaded in favor of directly substituting the element.
The cost of a Move is pairwise element-element distance, and in turn, the clusters would resemble those created by a Euclidean distance based measure.
On the other hand, if the parameter is set too low we expect more trajectories that do not resemble each other to get clustered together.

%I thin,, it is hard to tell

% Finally we study how we expect the change of the cost of MSM transformations to impact the final clusters. The parameter determines how much splits and merges should be favored over moves. At the extreme end, MSM will resemble DTW as element-element distance would dominate the cost matrix. That is, the higher the value of c, the more the cluster should resemble DTW. For lower values of c, we should see a preference for actively splitting and merging, bringing its cluster results closer to that of ERP.

% Setting the cost to 1, results in similarity scores that are

\subsection{Visual Inspection}

The visual inspection inspects the apparent fuzziness of the clusters as well as how scatted the trajectories within a cluster are.
We will refer to the latter of these characteristics as the \textit{band} of similarity which describes the general \textit{trend} of the cluster.
Where is it possible, we remark how larger trajectory sections were processed.
Unfortunately, spotting tendencies like that is not something visual inspection excels at.

In the case of affinity propagation, we take note of how many clusters it generated.
The corresponding evaluation for the hierarchical clusters is taking note of how balanced the final clusters are.
With how hierarchical clusters are created, some imbalance is expected as the most distinct trajectories will be connected last.

% contour
%  silhouette
%  hierarchical affinity

\textbf{Euclidean Distance:}
\begin{itemize}
\item AP:  In terms of overall shape, the clusters appear very fuzzy.
There are some clusters where that have a clearer contour and some where it is possible to spot a trend for the trajectories.
Nevertheless the general impression remains fuzzy; we observe trajectories that seem to oscillate freely around the apparent trajectory band.
While oscillations make the clusters appear fuzzier, the clusters do not come across as randomly grouped observations.

\medskip
\item HCA: There appears to be a bias and clusters are unbalanced.
Some of the clusters contained a few trajectories while some of them encompassed a large number of them.
In those clusters the bands were obvious, but the oscillations were even greater than those observed under AP.
This makes sense as the new clusters are created by merging similar sub-clusters.
We would expect a clearer band, but a fuzzier contour.
\end{itemize}

\textbf{Dynamic Time Warping:}
\begin{itemize}
\item AP: We observed two clusters that were very crisp and even more clusters that exhibited clear trends.
The trajectories do not seem to oscillate around the band in the clusters as much as they did under Ed.
We can spot some trajectory sections that appear as if they have been re-aligned– leading to a less messy expression.
Still, there are some oscillations and noisy clusters that make it hard to tell exactly why some trajectories were clustered together.
\medskip
\clearpage
\item HCA: The clusters are unbalanced, however less so than the ones created by Ed.
They illustrate how shape similarity is persevered under this measure by displaying a clear trend in the clusters.
Yet, the clusters are fuzzy in that their trajectories still deviate from the central band.
From these clusters it appears as if some of the trajectories are outliers;
they appear to be distinct from the rest of the trajectories and are placed in their own clusters.
\end{itemize}

\textbf{Hausdorff Distance}
\begin{itemize}

\item AP: Compared to DTW and Ed, the final number of clusters increased and the clusters created were both crisper and fuzzier.
The clusters display both the most advantageous and most disadvantageous aspects of the Hausdorff metric.
% Reducing the similarity score to one element-element distance did indeed  amount
However, the number of crisp clusters outweighs the fuzzy ones, and the contours of the clusters are crisp enough to highlight more intricate trajectory details.
% This contrasts the other clusters clustered together, rather than a general direction across the grid

\medskip
\item HCA: Again, we see both crisp and intricate clusters as well as very fuzzy ones.
The observations from affinity propagation hold for these clusters as well;
both the advantages and disadvantages of setting the similarity score as the distance between two trajectory elements are highlighted.
There is one cluster that is so fussy that we were quite puzzled by how it was formed.
% It may be that trajectories with outliers and noise were put together in such a way that they were at some point considered so similar to each other that they were gathered.
\end{itemize}

\textbf{Symmetrized Segment-Path Distance:}
\begin{itemize}

\item AP: This measure created fewer clusters than Hd, yet the clusters it created appear to be at least equally as crisp.
We observed that the clusters had such tight bands that curves of the trajectories were accentuated.
In contrast, DTW clustered the trajectories by their general shape and direction.
Whereas Hd was sensitive to noise, it becomes clear that SSPD addressed that weakness.
Two clusters stand out as more fuzzy than the others, but this is likely due to noisy data.
\medskip
\item HCA: The clusters are about as balanced as those created by Hd.
As was demonstrated in the AP-clustering, there is a trend towards showing the intricacies of shape similarity at a larger scale.
The bands of similarity are thinner than both Ed end DTW and more detailed than Hd.
\end{itemize}

\textbf{Edit Distance with Real Penalty:}
\begin{itemize}

\item AP:  The clusters created by ERP are significantly more fuzzy than that of Hd and SSPD.
This is expected as it gives similarity scores based on edits before and then the distance between trajectory elements.
However, it is unexpected that the resulting clusters were as fuzzy as those created by DTW and Ed, especially as it created far more clusters than either of them did.
The increased number of clusters should indicate that we would have more distinguished clusters, but this does not appear to be the case.
Even still, we observe that clusters were not randomly put together.
Looking closer at specific clusters, it is possible to spot turns and segments that are just shifted from each other.
\medskip
\item HCA: These clusters are unbalanced, there is a cluster that only has one trajectory.
Yet, that trajectory does not appear significantly distinct from the ones existing clusters.
From visual inspection it challenging to get more insight.
The bands across the clusters are quite general and there is not a consistent show of shifted segments.

\end{itemize}

\textbf{Edit Distance on Real Sequence: }
In discussing this method, we examine the clusters obtained from both parameter values in parallel.
% $\epsilon = \{0.101, 0.201\}$

% contour
%  silhouette
%  hierarchical affinity propagation

\begin{itemize}
\item AP: This measure generated the two largest number of AP-clusters.
The most restrictive parameter led to 48 clusters while the other value resulted in 39 clusters.
In our subjective opinion, both of the parameters resulted in too many clusters for a data set of 300 observations.
However, with fewer trajectories in each cluster analysis through visual inspection becomes is easier.
We observed that the clusters contained trajectories whose segments were shifted from each other.
Both parameter values resulted in crisp clusters, and our approximation of the recommended value gave seemingly more defined contours.
While increasing the threshold value led to fuzzier clusters, these clusters were crisper than those of ERP.
This is an expected result as EDR is noise-tolerant whereas ERP is not.
\medskip
\item HCA: For both parameter values, there were bands that obscured the intricacies of shape similarity.
Once more we observed that increasing the parameter value resulted in fuzzier clusters.
However, the increase in fuzziness was less than expected.
The largest parameter value led to a more unbalanced distribution of trajectories, further signifying that the smaller one remains the most suited value for $\epsilon$.
\end{itemize}

\textbf{Move-Split-Merge:}

As we did for EDR, the clusters created by MSM and the effect of varying the parameter are discussed in parallel.
We first note that the clusters MSM created with the cost was set to 1 and were indistinguishable from those of Ed.
This was not an entirely unexpected result given what we know about MSM, and that the two methods had identical rankings of the most similar trajectory pairs.
By reason of that cost parameter being set too high and the accompanying clusters already being described, we will focus on the two remaining parameter values.

  % cost$= \{1, 0.1, 0.01\}$
\begin{itemize}

\item AP: We observed that the lowest parameter value resulted in fewer clusters, but both of the parameter values led to a larger number of clusters than expected.
As with EDR, the large number of clusters makes it easier to visually analyze them.
Given that this measure is both moderately noise-tolerant and context-aware we expect the clusters to be crisp.
Indeed, the majority of the clusters have crisp contours wherein the intricacies of the trajectories are preserved.
Some clusters contain what appear to be outlier trajectories.
This would suggest that the discrimination degree of this measure was high enough to isolate them in a way that no measures did.
The smaller parameter value resulted in both fuzzier clusters and an increase in clusters with few trajectories.
This may be an indication of that value being too low, suggesting that $0.1$ was the most appropriate cost for this data set.

\medskip
\item HCA: The clusters created by the lowest cost parameter were more unbalanced and fuzzier than those generated by the middle one.
This further supports the argument that the middle of the cost value is the most fitting one.
Interestingly, there were still trajectories that were clustered by themselves as if they were outliers.
It looks as if these trajectories are consistent across both parameter values and clustering techniques.

\end{itemize}

\section{Reflections}
% Other Applications write here, but i i think it might belong to "further work"

The measures created clusters whose qualities coincide with the anticipated results.
We observed fuzzy clusters where the underlying measure was less tolerant to noise and crisper clusters where the measure accounted for whole trajectory similarity.
Almost all of the measures were elastic, and we observed several clusters with trajectories that were time-shifted.

By visual inspection, it was hard to tell if metricity affected the clusters although this may have been a result of picking clustering techniques that did not require a metric distance function.
% metricity.

The clusters' appearance remained consistent between AP and HCA for a given measure, even as the number of clusters changed.
This observation was to be expected as the models that created the clusters used the same similarity distances scores.

Lastly, we comment on a few algorithm-specific observations.
The fuzziness of the clusters created by the Euclidean distance align with its lock-step design just as the crispness of SSPD’s clusters aligns with its design purposes.
It was initially unexpected that ERP would have the fuzziest looking clusters. However, upon closer examination, we reasoned that it made sense that a non-context aware, noise sensitive method that adapts to time shifts would lead to quite fuzzy clusters.

The way MSM could consistently filter out trajectories that did not behave like the others would imply that it would be well suited for outlier detection.

% For \textbf{MSM} the effects of its parameter, setting $c$ to $0.1$ resulted in AP generating a cluster consisting of just one trajectory and decreasing it $0.01$  lead to 5 of these single trajectory clusters. This could be an indication of poorly chosen parameter values. Looking at the HCA- generated clusters support this assumption. We observed less balanced clusters as the cost parameter decreased. Based on this, we have conclude that MSM with cost parameter $c=1$ best is the most representative for this measure and further analysis will be based on it.

\section{Inconsistencies from Simplifications}
As noted in \Cref{ch:5}, several simplifications were made for this experiment.
We close this chapter by reflecting on how these simplifications have affected the results.

% \clearpage

\subsection{Data format}
%  For real life data, it is not reasonable to assume that the trajectories will be of the same length and furthermore, if the trajectories are of the same length this still does not mean the time spent in each location matches up.

The first simplification that was made was truncating the trajectories to equal length.
In doing this we removed the option to study how measures would have handled cases like this.
There would be insight to be gained from examining measures at different trajectory lengths.
In particular, it is not reasonable to assume that real data would have this property.
The clusters could have ended up looking quite different, possibly highlighting the difference between trajectory section and whole trajectory similarity.

Of the algorithms in this thesis, only the Euclidean distance lacked a definition for trajectories of unequal length.
It is possible to design a comparative study where lock-step measures can give scores for these trajectories.
An option would be to artificially add more trajectories elements by interpolation.
However, we decided against it as we felt confident there would be enough properties to examine after the simplification.

The next data-format simplification we did was the re-scaling.
The manner in which it was done meant that the data would lose its connection to the real world.
To exemplify this we refer to \Cref{fig:sspd_loc_real}.
The clusters were created based on the scaled data, thus those clustered were quite crisp.
However, when displaying those same clusters with their raw coordinates it becomes clear how scattered the trajectories are.
It becomes clearer that trajectories were clustered together based on shape similarity.
We reiterate that the intent was to study measures themselves, thus the data set selection— and thereby the re-scaling was inconsequential.

\begin{figure}[h]
  \centering
  \includegraphics[width=.9\linewidth,height=.9\textheight,keepaspectratio]{figs/clusters/CLU_AP_ALL[SSPD]_REAL.png}
  \caption{Affinity propagation clusters created with SSPD, but showing the raw trajectory locations}
  \label{fig:sspd_loc_real}
\end{figure}

\subsection{Distance Computation and Evaluation}
% Tweaking the format and scale of the data is one thing, but the next line of simplifications mattered more for the evaluations of the measures.

We tested three measures that required a parameter and we did not do any proper parameter tuning.
ERP would likely have had more agreement with the other measures in \Cref{tab:top-10-sim-pairs} if different values for the reference point had been tested out.

% Next, while we did not  the measues

% The last simplification we would like to bring attention to is also something that potentially affected the final evaluation.

The application we chose for the similarity distance measures, clustering, is itself an area of active research.
The interconnectedness of clustering techniques and distance algorithms could have been studied in more detail before settling on affinity propagation and hierarchical clustering analysis as the evaluation basis.
We acknowledge that there may be traits of the selected measures that have been obscured or misrepresented.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                 %
%  GEANT manual in LaTeX form                              %
%                                                                 %
%  Michel Goossens (for translation into LaTeX)                   %
%  Version 1.00                                                   %
%  Last Mod. Jan 24 1991  1300   MG + IB                          %
%                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Origin{G.Tromba, P.Bregant}
\Submitted{10.10.89}\Revised{16.12.93}
\Version{Geant 3.16}\Routid{PHYS251}
\Makehead{Simulation of Rayleigh scattering}
\Shubr{GRAYL}{}

\Rind{GRAYL} generates Rayleigh scattering of a photon using
the random-number
composition and rejection technique to sample the momentum
of the scattered photon and the scattering angle, according to the
form-factor distribution. In this reaction no new particles are
generated and the kinematical quantities of the scattered photon
replace the original ones in the \FCind{/GCTRAK/} common block.

Activation of the Rayleigh scattering is done via the {\tt FFREAD} data
record {\tt RAYL}. If this process is activated, \Rind{GRAYL} is called
by \Rind{GTGAMA} when a Rayleigh scattering occurs.
\section{ Method }

The Rayleigh differential cross section as a function of $ q^2 $ is given
by \cite{bib-NELS}:
\begin{equation}
\frac{d \sigma_R \left( q^2 \right) }{ d \Omega } =
 \frac{ \pi r_0^2}{ k^2 } \left( \frac{1 + \mu^2 }{ 2 } \right)
 \left| F_T \left( q \right) \right| ^2
\end{equation}
where:\\

\[\begin{array}{LL}
r_0       & \mbox{electron radius} \\
k         & \mbox{incident wave vector} \\
q=2k sin \frac{ \theta }{2}  & \mbox{momentum of scattered
              photon ($\theta$ is the scattering angle)} \\
\mu = cos \theta  & = 1 - \frac{q^2 }{ 2 k^2} \\
F_T \left( q \right)     & \mbox{molecular form factor}
\end{array} \]

Under the assumption that the atoms of a molecule are completely independent,
$ \left| F_T \left( q \right) \right| ^2 $ is given by:
\begin{equation}
 \left| F_T \left( q \right) \right| ^2 = \sum_{i=1}^{N}
 \frac{W_i}{A_i}  \left| F_i \left( q_i , Z_i  \right) \right| ^2
 \sigma_{c_i} \left( Z_i , E \right)
\end{equation}
where the index $i$ runs on the $N$ atoms in the molecule and:

\[\begin{array}{LL}
 W_i        & \mbox{proportion by weight} \\
 Z_i , A_i  & \mbox{atomic number and weight} \\
 F_i        & \mbox{form factor} \\
\sigma_{c_i}& \mbox{total atomic cross section for coherent scattering}\\
\end{array} \]

Using the combined composition and rejection sampling method described in
\Rind{GPAIRG} ({\tt [PHYS211]}) we may set:
\begin{equation}
f \left( q \right) = \sum_{i=1}^{N} \alpha_i f_i \left( q \right)
g_i \left( q \right) = \sum_{i=1}^{N} A \left( q_i^2 \right)
\frac{ \left| F_T \left( q \right) \right| ^2 }{ A \left( q_n^2 \right) }
\left( \frac{1+\mu^2}{2} \right)
\end{equation}

where:

\[\begin{array}{LL}
 n          & \mbox{number of energy bins} \\
 q_i        & \mbox{momentum of the photon with energy $ E_i $ of the
                 $i^{th}$  bin} \\
 q_n        & \mbox{upper limit for the momentum of the scattered photon} \\
 \alpha_i   & A \left( q_i^2 \right)  \\
 f_i \left( q \right )  &
 \frac{\left| F_T \left( q \right) \right| ^2}{A \left( q_n^2 \right)}  \\
 g_i \left( q \right )  & = \frac{1+\mu^2}{2} \mbox{\hspace{0.5cm}
rejection function.}
\end{array} \]

Therefore, for given values of the random numbers $r_1$ and $r_2$, \Rind{GRAYL}
samples the momentum of the scattered photon and the scattering angle
$\theta$ via the following steps:

\begin{enumerate}
\item sample $ A \left( q^2 \right) = r_1 A \left( q_n^2 \right) $
\item find the $ \left (  q_{i-1} , q_i  \right] $ interval
which gives $ A \left( q_{i-1}^2 \right) \leq A \left( q^2 \right)
\leq  A \left( q_i^2 \right) $
\item calculate the linear extrapolation:
\begin{equation}
q = q_{i-1} + \left( A \left( q^2 \right) -  A \left( q_{i-1}^2 \right) \right)
\frac{q_i - q_{i-1} }
{ A \left( q_i^2 \right) -  A \left( q_{i-1}^2 \right)} \nonumber
\end{equation}
\item calculate $\mu = cos \theta  = 1 - q^2/(2 k^2) $
\item calculate $g_i \left( q \right) = (1 + \mu^2 )/2 $
\item if $g_i \left( q \right) > r_2 $ the event is accepted, otherwise
go back to 1.
\end{enumerate}


\subsection{\(L_2\) boosting}


\section{Optical Tracking}
\subsection{Overview}
The Optical Tracking System is implemented with three subsystems:
\begin{enumerate}
  \item Video Extraction
  \item Pinhole Camera Model
  \item Epipolar Geometry
\end{enumerate}
\subsection{Video Extraction}
The Video Extraction system's function is to extract the location of a patricular object within the video footage. The objects, in this case, are differently colored small balls. The small balls represent sever key points of the Quadcopter. By determining the location of the balls, we will be able to calculate the position and attitude of the Quadcopter.
The procedure of video extraction is described as following:
\begin{enumerate}
  \item Shotting: Get images from camera,
  \item Transforming: transform the image to HSV colorspace,
  \item Extracting: use tresholding to extract pixels in patricular color range, then use histogram and backprojection to determin the probablity that the pixels belong to the model,
  \item Denoising: use opening and closing to denoise,
  \item Determining: use \emph{Lucas-Kanade} and \emph{CamShift} algorithm to determin the centroid of the extracted pixels.
\end{enumerate}
\subsubsection{Shooting}
In this project, v4l\footnote{a.k.a. Video For Linux} is the middleware of OpenCV and camera. With \emph{v4l} and \emph{OpenCV}, reading an image from camera is as easy as following:
\lstset{language=python}
\begin{python}
cap = cv2.VideoCapture(source)
img = cap.read()
\end{python}
\subsubsection{Transforming}
Different from the RGB colorspace, HSV colorspace has a unique character: its V channel represents the brightness. If we remove V channel from tresholding, the same color profile will be able to work in different lighting conditions.
The transforming process is described below\cite{cite4}:
\begin{align}
  M &= \operatorname{max}(R, G, B) \\
  m &= \operatorname{min}(R, G, B) \\
  C &= M - m\\
  H^\prime &=
    \begin{cases}
      \mathrm{undefined},        &\mbox{if } C = 0 \\
      \frac{G - B}{C} \;\bmod 6, &\mbox{if } M = R \\
      \frac{B - R}{C} + 2,       &\mbox{if } M = G \\
      \frac{R - G}{C} + 4,       &\mbox{if } M = B
    \end{cases} \\
  H        &= 60^\circ \times H^\prime\\
  V &= M\\
   S &=
    \begin{cases}
      0,           &\mbox{if } C = 0 \\
      \frac{C}{V}, &\mbox{otherwise}
    \end{cases}
\end{align}
The code used to transform the image is:
\begin{python}
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
\end{python}
Note that by default, OpenCV uses BGR color space.
\subsubsection{Extracting}
OpenCV's built in treshold function is used to utilize multithreading.
The process of tresholding is printed below:
\begin{python}
mask = cv2.inRange( hsv,
                    np.array((0., 60., 32.)),
                    np.array((180., 255., 255.))
                    )
\end{python}
However, simply tresholding is often not enough. The object tracked can have a complex color feature. In order to track the entire object, it is necessary to consider all of the features. Histogram is used to model the color distribution of an object, and me can then map the probability of pixels using backprojection.\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.2\textwidth]{../Pictures/histogram.JPG}
    \caption{A sample histogram\cite{cite5}}\\
\end{figure}
Backprojection uses the modeled histogram to find the probability that certain pixel belongs to the model. Consider an image matrix $M$, each pixel can be described as pair $(H_{i,j},S_{i,j})$. We can use the two values to create a 2D histogram that represents the distribution of certain color range in $H$ and $S$. Below is a sample 2D histogram generated by \emph{OpenCV Python Sample}.\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/2dhist.png}
    \caption{A sample 2D histogram}\\
\end{figure}
After generating the histogram, the probability that a pixel belongs to the model can be expressed by the following steps:
\begin{enumerate}
  \item Find the pair $(H_{i,j},S_{i,j})$ of a pixel,
  \item find the correspondent bin in the histogram,
  \item get the relative frequency of the bin.
\end{enumerate}
Then we can use the frequency to map a binary image. In this image, each pixel represents the probability that the correspondent pixel in the original image belongs to the model. Below is an example backproject image:\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.5\textwidth]{../Pictures/backproject.png}
    \caption{A sample backprojrction}\\
\end{figure}
\subsubsection{Denoising}
After calculating the backproject image, we will be able to use morphology transformation to remove noises. In this projcet, we used Opening and Closing to remove small noise pixels and fill black pixel holes in the backprojection.\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/Opening.png}
    \caption{Sample Opening operation\cite{cite7}}\\
\end{figure}
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/Closing.png}
    \caption{Sample Closing operation\cite{cite6}}\\
\end{figure}

The result is significant. A great deal of noises is removed in this process:\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/before.png}
    \caption{Befor morphology transformation}\\
\end{figure}
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/after.png}
    \caption{After morphology transformation}\\
\end{figure}
\newpage

\subsubsection{Extracting}
There are two stages of extracting. They are:
\begin{enumerate}
  \item Detecting
  \item Tracking
\end{enumerate}
The difference between them is that \emph{Tracking} requires the original location of the object to be known. That information should be fed with \emph{Detecting} in the \emph{first} frame of video source.

By \emph{Detecting}, we find locations where the image of \emph{source} is similar to \emph{template}.
The algotighm we used in \emph{Detecting} is rather simple. It "shifts" the \emph{template} on the \emph{source}, and find the similarities bewteen the \emph{template} and the image below.

The method of template matching is printed below:
\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{python}
  import numpy as np
  import cv2
  import math

  def match(template, image):
    '''
    Match template on image and return the centroid's coord.
    '''

    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

    result = cv2.matchTemplate(gray, patch, cv2.TM_CCOEFF_NORMED)
    result = np.abs(result) ** 3
    val, result = cv2.threshold(result, 0.01, 0, cv2.THRESH_TOZERO)

    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)

    height, width, depth = template.shape
    x, y = maxLoc
    x += width/2
    y += height/2

    return (x,y)
\end{minted}

In order to make things clearer, we used a marker from the TV series \emph{Person of Interest} to mark the point of interest.
Here's the code we used to mark the object:
\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{python}
  import numpy as np
  import cv2
  import math

  def draw_machine_mark(size, location, image):
      '''
      This function draws a machine mark on the image
      '''
      original = cv2.imread("assets/machine.png", -1)

      osize = math.sqrt(original.size)/2
      ratio = size / osize
      height, width, depth = image.shape
      timg = cv2.resize(original, (0,0), fx=ratio, fy=ratio)
      x, y = location

      image[:] *= 0.8

      x_start = max(0, x - size/2)
      x_end = min(x_start + size, width)
      x_start = min(x_end - size, x_start)
      y_start = max(0, y - size/2)
      y_end = min(y_start + size, height)
      y_start = min(y_end - size, y_start)

      roi = image[y_start : y_end, x_start : x_end]
      for c in range(0,3):
          roi[:,:,c] = timg[:,:,c] * (timg[:,:,3] / 255.0) + roi[:,:,c] * (1 - timg[:,:,3] / 255.0)
      image[y_start : y_end, x_start : x_end] = roi
\end{minted}
Below is an image of the effect:\\
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.5\textwidth]{../Pictures/after.png}
    \caption{Example of tracking effect}\\
\end{figure}


\section{Distributions}
\subsection{Distribution (Base Class)}
\subsection{Arcsine}
\subsection{Asymmetric Laplace}
\subsection{Bernoulli}
\subsection{Beta}
\subsection{Binomial}
\subsection{Categorical}
\subsection{Cauchy}
\subsection{Chi Square}
\subsection{Conditional Model}
\subsection{Convolution}
\subsection{Data}
\subsection{Dirac Delta}
\subsection{Dirichlet}
\subsection{Exponential}
\subsection{Fisher-Snedcor (F-Distribution)}
\subsection{Gamma}
\subsection{Generator}
\subsection{Gumbel}
\subsection{Gumbel Softmax}
\subsection{Gumbel Mixture Model}
\subsection{Half Cauchy}
\subsection{Half Normal}
\subsection{Hyperbolic Secant}
\subsection{Infinite Mixture Model}
\subsection{Kumaraswamy}
\subsection{Langevin}
\subsection{Laplace}
\subsection{Log Cauchy}
\subsection{Log Laplace}
\subsection{Log Normal}
\subsection{Logistic}
\subsection{Logit Normal}
\subsection{Mixture Model}
\subsection{Negative Binomial}
\subsection{Normal (Independent)}
\subsection{Normal (Multivariate)}
\subsection{Pareto}
\subsection{Poisson}
\subsection{Rayleigh}
\subsection{Relaxed Bernoulli}
\subsection{Student T}
\subsection{Transformed Distribution}
\subsection{Uniform}
\subsection{Weibull}


% !TEX encoding = UTF-8 Unicode
% !TEX spellcheck = en_US
\documentclass{svproc}

\usepackage{paralist}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{calrsfs} % Für Kalligraphie-F
\newcommand{\bm}[1]{\boldsymbol{#1}}
\DeclareMathOperator{\arctantwo}{arctan2}
% Latex-Makros für häufig verwendete Formelzeichen
\newcommand{\ortvek}[4]{{ }_{(#1)}{\boldsymbol{#2}}^{#3}_{#4} }
\newcommand{\vek}[3]{\boldsymbol{#1}^{#2}_{#3}}
\newcommand{\rotmat}[2]{{{ }^{#1}\boldsymbol{R}}_{#2}}
\newcommand{\rotmato}[2]{{{ }^{#1}\boldsymbol{\overline{R}}}_{#2}}
\newcommand{\transp}[0]{{\mathrm{T}}}
\newcommand{\ks}[1]{{\mathcal{F}}_{#1}}

% Für Deutsche Umlaute
%\usepackage{ngerman}
\usepackage[utf8]{inputenc}

% Für Bilder
\usepackage{graphicx}
\usepackage{color}
\graphicspath{{./figures/}}

% to typeset URLs, URIs, and DOIs
\usepackage{url}
\def\UrlFont{\rmfamily}

\begin{document}

\mainmatter              % start of a contribution
%
\title{Resolution of Functional Redundancy for 3T2R Robot Tasks using Two Sets of \\Reciprocal Euler Angles}
%
\titlerunning{Reciprocal Euler Angles}  % abbreviated title (for running head)
%                                     also used for the TOC unless
%                                     \toctitle is used
%
\author{Moritz Schappler \and Svenja Tappe \and Tobias Ortmaier}
%
\authorrunning{Schappler et al.} % abbreviated author list (for running head)
%
%%%% list of authors for the TOC (use if author list has to be modified)
%\tocauthor{Moritz Schappler, Svenja Tappe, and Tobias Ortmaier}
%
\institute{Institute for Mechatronic Systems, Leibniz University Hannover, Germany,\\
    \email{moritz.schappler@imes.uni-hannover.de}}

\maketitle              % typeset the title of the contribution

%ABSTRACT
\begin{abstract}
Robotic tasks like welding or drilling with three translational and only two rotational degrees of freedom (``3T2R'') are of high industrial relevance but are ra\-ther scarcely addressed in scientific publications.
Existing solutions for the resolution of the functional redundancy of robotic manipulators with more than five axes performing these tasks either expand the full kinematic formulation or reduce it in intermediate steps.
This paper presents an approach to reduce the kinematic formulation from the start to solve the problem in a simpler way.
This is done by using a set of reciprocal Euler angles to describe the end-effector orientation and the orientation error in inverse kinematics.
\end{abstract}

%KEYWORDS
\begin{keywords}
functional redundancy, reciprocal Euler angles, \\ inverse kinematics, robot manipulators, five-DoF task, 3T2R task
\end{keywords}

\section{Introduction}
\label{sec:Intro}
Since the first papers in the 1980s, the fields of inverse kinematics \cite{GoldenbergBenFen1985} and the resolution of \emph{intrinsic} redundancy \cite{Yoshikawa1984} in robotics have been extensively elaborated upon.
Inverse kinematics for robot manipulators can be solved analytically for some structures and can be approached in general with gradient-based numeric methods at  joint position or joint velocity level.
Due to the nonlinearity of rotation at position level \cite{GoldenbergBenFen1985}, the latter is usually preferred.

Intrinsic redundancy is defined as a robot joint space dimension higher than the operational space dimension and functional redundancy is defined as an operational space dimension higher than the task space dimension, implying that there are more independent joint coordinates than task coordinates.

The kinematics of \emph{functionally} redundant robots performing tasks with five instead of six degrees of freedom (DoF) has drawn much less attention, even if many industrial relevant tasks only require 5-DoF such as the class of 3T2R-tasks where the tool for the task is axis-symmetric.
These tasks comprise amongst others arc welding \cite{HuoBar2005}, drilling \cite{ZhuQuCaoYan2013,GuoDonKe2015}, spray-painting \cite{FromGra2010}, milling \cite{MousaviGagBouRay2018}, laser-cutting and glueing and are transfered to a greater extend to robotic manipulators in the automotive or aviation industry.

Since standard industrial robots have six DoF, one degree of \emph{functional} redundancy exists which allows to improve performance characteristics of the robots as a secondary task while reaching a valid pose to perform one of the aforementioned primary tasks.
To be able to incorporate performance optimization into inverse kinematics via the well-established gradient projection method \cite{Yoshikawa1984}, either the joint space can be augmented or the task space can be reduced. % \cite{Huo2009}.
Augmenting the joint space is possible by virtually inserting an additional joint for the rotation around the task's axis of symmetry (``tool axis'') \cite{Baron2000}.
Reducing the task space can be performed by orthogonal decomposition of the end-effector twist \cite{HuoBar2005}, removing the row of the ``task frame Jacobian'' corresponding to the tool axis \cite{Zlajpah2017}, the construction of the Jacobian Nullspace based upon geometric properties of the task \cite{LegerAng2016} or constructing a cone or pyramid resulting from the tool axis with an additional range of tolerance for tilt angles \cite{FromGra2010}.

Another possibility for the resolution of the functional redundancy is a cascaded optimization where the inner loop calculates the inverse kinematics with standard methods and the outer loop optimizes the performance index.
The optimization can be performed by evaluating the index on a range of rotation angles around the tool axis \cite{ZhuQuCaoYan2013} or by using the incremental change of the rotation around the tool axis directly to adapt the joint angles  \cite{GuoDonKe2015}.

The choice of performance indices aims to improve the task execution with measures such as the joint positions quadratic \cite{HuoBar2005} or hyperbolic \cite{ZhuQuCaoYan2013} distances from their limits, singularity avoidance via Frobenius-norm condition number \cite{ZhuQuCaoYan2013}, squared condition number \cite{LegerAng2016} or a combination of manipulability and condition number of the Jacobian \cite{HuoBar2008}, stiffness \cite{GuoDonKe2015} or milling chatter stability margins \cite{MousaviGagBouRay2018}.

The characteristic length required to normalize the Jacobian for the performance indices can be either chosen constant regarding the robot geometry \cite{ZhuQuCaoYan2013} or as an additional optimization parameter \cite{LegerAng2016}.

The existing methods each have drawbacks that are avoided by using the new method presented in this paper:
\begin{compactitem}
    \item Augmenting the joint space \cite{Baron2000} increases the computational cost and can lead to an ill-conditioned Jacobian \cite{HuoBar2008}.
    \item Using a nested optimization \cite{ZhuQuCaoYan2013,GuoDonKe2015} does not allow using the well-proven gradient projection method.
    \item In \cite{Zlajpah2017} the tool-axis rotation of the end-effector has to be calculated and canceled out in the nullspace.
    \item Describing the tool axis with two points requires to define a distance between these points, which has to be chosen e.\,g. as the normalized Jacobian's characteristic length to gain a good conditioning of the optimization \cite{LegerAng2016}.
    \item All methods have a high mathematical level of abstraction e.\,g. using orthogonal decomposition \cite{HuoBar2008}, linear matrix inequalities and convex optimization \cite{FromGra2010}, rotation and component-selection of the vector part of the quaternion orientation error \cite{Zlajpah2017} or sequential quadratic programming \cite{LegerAng2016}.
\end{compactitem}

This paper transfers the velocity level approach from \cite{Zlajpah2017} to the position level and the orientation error is expressed in Euler angles instead of quaternions:

The absolute orientation of the end-effector and the corresponding orientation error between desired and actual end-effector orientation are expressed with two sets of Euler angles which are mutually reciprocal.
Reciprocity for sets of Euler angles is defined in this paper as successive rotations around intrinsic elementary axes of switched order.
By this definition, the rotation component belonging to the tool axis in 3T2R tasks is always the same for the absolute orientation and for the orientation error, respectively.
The effect of the tool axis can then be eliminated from the kinematic equations.
This allows to express the kinematics equations in minimal form which makes it easy to use the gradient projection method to incorporate performance indices in inverse kinematics.

The contributions of this paper are
\begin{compactitem}
    \item a new formulation for the kinematics problem for robot manipulators performing 3T2R-tasks,
    \item an application of the formulation leading to an efficient solution for inverse kinematics with functional redundancy,
    \item remarks on the implementation of gradients of and w.r.t. rotation matrices and its performance.
\end{compactitem}

The remainder of the paper is structured as follows: The idea of a kinematic description using sets of reciprocal Euler angles is elaborated upon in Sec.\,\ref{sec:RecEulAng}.
Their application in inverse kinematics of 3T2R tasks and in resolving the functional redundancy is shown in Sec.\,\ref{sec:RecEulAng_3T2R_app} and \ref{sec:ResFuncRed}.
Sec.\,\ref{sec:Conclusion} concludes the paper.

\section{Using Reciprocal Euler Angles for Robot Kinematics}
\label{sec:RecEulAng}

The core of solving the inverse kinematics problem of robot manipulators for tasks with only two rotational degrees of freedom is the nonlinearity of rotation.
Rotation can be described using rotation matrices, Euler angles, rotation angle/axis and its derivations like quaternions or the Rodrigues vector.
In contrast to all other notations, consecutive elementary rotations like Euler angles can be reduced to adapt for tasks only requiring two rotatory DoF.

\subsection{Kinematics Description}
\label{sec:RecEulAng_KinDesc}

A serial kinematic chain is described with the joint positions $\bm{q}$ and the forward kinematics
\vspace{-0.2em}
%
\begin{equation}
\bm{x} (\bm{q})
=
\bm{f} (\bm{q})
\label{equ:dirkin}
\end{equation}
%
giving the configuration-dependent position and orientation $\bm{x} (\bm{q})$ of the actual end-effector (``E'') frame $\ks{E}$.
In the following, the general, joint-independent, pose $\bm{x}$ of the robot end-effector is defined as the \emph{desired} (``D'') end-effector pose in the inverse kinematics problem and will be termed with ``$\bm{x}$'' without further supplements for the sake of compactness of the equations.
This general pose
%
\begin{equation}
\bm{x}
=
\begin{bmatrix}
\bm{x}_{\mathrm{t}}^\transp & \bm{x}_{\mathrm{r}}^\transp
\end{bmatrix}^\transp
\in {\mathbb{R}}^{6}
\label{equ:x_def}
\end{equation}
%
describes the desired robot end-effector frames $\ks{D}$ position
%
\begin{equation}
\bm{x}_{\mathrm{t}}
=
\ortvek{0}{r}{}{D}
\in {\mathbb{R}}^{3}
\label{equ:xt_def}
\end{equation}
%
and orientation
%\vspace{-0.5em}
%
\begin{equation}
\rotmat{0}{D} (\bm{x}_{\mathrm{r}})
=
\begin{bmatrix}\vek{n}{}{D} & \vek{o}{}{D} & \vek{a}{}{D}\end{bmatrix} \in \mathrm{SO(3)}
\label{equ:xr_def_rotmat}
\end{equation}
%
with respect to the base frame $\ks{0}$, which is marked with left subscript ``$(0)$'' for vectors and left superscript ``$0$'' for rotation matrices.
The rotation (\ref{equ:xr_def_rotmat}) is expressed without loss of generality with a set $\bm{\beta}$ of $X$-$Y$-$Z$-Euler angles
%\footnote{Other sets of Euler angles may be used as well.}
%
\begin{equation}
\bm{x}_{\mathrm{r}}
=
\begin{bmatrix}
\beta_1  & \beta_2 & \beta_3
\end{bmatrix}^{\mathrm{T}}
\in {\mathbb{R}}^{3},
\label{equ:xr_def}
\end{equation}
%
\begin{equation}
\bm{R}(\bm{\beta}) = \bm{R}_x(\beta_1) \bm{R}_y(\beta_2) \bm{R}_z(\beta_3) \in \mathrm{SO(3)}.
\label{equ:def_rmat_xyz}
\end{equation}
%
The deviation $\bm{\Phi}$ between the desired end-effector frame $\ks{D}$ expressed with $\bm{x}$ of (\ref{equ:x_def}) and the actual robots end-effector frame $\ks{E}$ expressed with $\bm{f}(\bm{q})$ of (\ref{equ:dirkin}) is defined as
%\vspace{-0.5em}
%
\begin{equation}
\bm{\Phi}=\begin{bmatrix}
\bm{\Phi}_{\mathrm{t}}^\transp & \bm{\Phi}_{\mathrm{r}}^\transp
\end{bmatrix}^\transp \in {\mathbb{R}}^{6},
\label{equ:Phi_def}
\end{equation}
%
which is the residual vector of the inverse kinematics problem.
The vector
%
\begin{equation}
\bm{\Phi}_{\mathrm{t}}(\bm{q},\bm{x})
=
- \ortvek{0}{r}{}{D} + \ortvek{0}{r}{}{E}(\bm{q})
=
- \bm{x}_{\mathrm{t}} + \ortvek{0}{r}{}{E}(\bm{q})
 \in {\mathbb{R}}^{3}
\label{equ:Phit_def}
\end{equation}
%
from the origins of $\ks{D}$ to $\ks{E}$ is the translational part and the rotational part
%
\begin{align}
\bm{\Phi}_{\mathrm{r}}(\bm{q},\bm{x})
= \begin{bmatrix}
\alpha_1  & \alpha_2 & \alpha_3
\end{bmatrix}^\transp
&=\bm{\alpha}\left(\rotmat{D}{E}(\bm{x}_{\mathrm{r}},\bm{q})\right)\nonumber \\
&=\bm{\alpha}\left(\rotmat{0}{D}^\transp (\bm{x}_{\mathrm{r}})\rotmat{0}{E}(\bm{q})\right) \in {\mathbb{R}}^{3}
\label{equ:Phir_def}
\end{align}
%
is also chosen as a set of Euler angles $\bm{\alpha}$ \cite{GoldenbergBenFen1985}, that is calculated from the rotation matrix in (\ref{equ:Phir_def}).
In the following, ``$\bm{\alpha}$'' will always refer to the rotation error/residual and ``$\bm{\beta}$'' to an orientation relative to the base frame.
The Euler angle convention of $\bm{\alpha}$ can be chosen independently of the choice for the orientation representation in  $\bm{\beta}$.
The intuitive approach of choosing
%
\begin{equation}
\bm{R}(\bm{\alpha}^*) := \bm{R}_x(\alpha_1^*) \bm{R}_y(\alpha_2^*) \bm{R}_z(\alpha_3^*) \in \mathrm{SO(3)}
\label{equ:alpha_convention_xyz}
\end{equation}
%
the same way as $\bm{\beta}$ leads to a set of transformations depicted in Fig.\,\ref{fig:frames_5dof_6dof}\,(a) where the intermediate steps of the single elementary rotations are omitted since they have no technical meaning.
The upperscript in $\bm{\alpha}^*$ in (\ref{equ:alpha_convention_xyz}) demarcates this specific example and following elaborations on the calculation of $\bm{\alpha}$.
%

\subsection{Effect of the Reciprocal Euler Angles}
\label{sec:RecEulAng_effect}

\begin{figure}[htb]
    \input{./figures/frame_comparison_combined_5dof_6dof.pdf_tex}
    \caption{Overview of the different frames (a) for 6-dof tasks with standard Euler angle notation and (b) for 5-dof tasks with reciprocal Euler angle notation.}
    \label{fig:frames_5dof_6dof}
\end{figure}

Using $\bm{\Phi}_{\mathrm{r}}{=}\bm{\alpha}^*$ as defined in (\ref{equ:alpha_convention_xyz}), all three components of $\bm{x}_{\mathrm{r}}{=}\bm{\beta}$ affect the rotation matrix $\rotmat{D}{E}$, which makes it impossible to remove one rotational coordinate from the kinematic description, even if it is not required in the task.
%
To encounter this issue, the Euler angle convention $\bm{\alpha}$ for the orientation error $\bm{\Phi}_{\mathrm{r}}$ is now chosen to be
\vspace{-0.1em}
%
\begin{equation}
\bm{R}(\bm{\alpha}) := \bm{R}_z(\alpha_1) \bm{R}_y(\alpha_2) \bm{R}_x(\alpha_3) \in \mathrm{SO(3)}
\label{equ:def_rmat_zyxr}
\end{equation}
%
instead of the definition from (\ref{equ:alpha_convention_xyz}).
This set of $Z$-$Y$-$X$-Euler angles $\bm{\alpha}$ is defined in this paper as being \emph{reciprocal} to the set of $X$-$Y$-$Z$-Euler angles of $\bm{\beta}$ for the absolute orientation.

The reciprocity refers to the switched order of the elementary axes $X$, $Y$ and $Z$ in the combination of the sets $\bm{\alpha}$ \emph{and} $\bm{\beta}$.
One set of Euler angles alone can not be declared as reciprocal without reference to another set of angles.
%
Similar to the six end-effector operational space coordinates $\bm{x}$, the task space of 3T2R tasks is defined to have five coordinates
%
\begin{equation}
\bm{\eta}
=
\begin{bmatrix}
\bm{\eta}_{\mathrm{t}}^\transp &
\bm{\eta}_{\mathrm{r}}^\transp
\end{bmatrix}^\transp
\in {\mathbb{R}}^{5}.
\end{equation}
%
The translational part
%\vspace{-0.5em}
%
\begin{equation}
\bm{\eta}_{\mathrm{t}}
=
\bm{x}_{\mathrm{t}}
=
\ortvek{0}{r}{}{D}
\in {\mathbb{R}}^{3}
\end{equation}
%
remains unchanged and the rotational part
%
\begin{equation}
\bm{\eta}_{\mathrm{r}}
=
\begin{bmatrix}
\beta_1  & \beta_2
\end{bmatrix}^\transp
=
\underbrace{\begin{bmatrix}
1 & 0 & 0  \\
0 & 1 & 0
\end{bmatrix}}_{=\bm{P}_{\eta}}
\bm{x}_{\mathrm{r}}
\in {\mathbb{R}}^{2}
\label{equ:etar_def}
\end{equation}
%
is modified compared to $\bm{x}$.
The last rotation $\beta_3$ around the $z$-axis $\bm{a}_{D}$ of $\rotmat{0}{D}$ is excluded from the task space by the selection matrix $\bm{P}_{\eta}$, since it corresponds to a rotation around the tool axis in 3T2R tasks and is a DoF of the operational space which can be set arbitrarily (from the kinematic point of view).

The frames $\ks{A1}$ and $\ks{A2}$ result from intermediate elementary rotations, as sketched in Fig.\,\ref{fig:frames_5dof_6dof}\,(b).
These intermediate frames are the partial frame rotation to the former 3T3R desired frame $\ks{D}$
%
\begin{equation}
\rotmat{0}{A1}
=
\bm{R}_x(\beta_1) \bm{R}_y(\beta_2)
=
\begin{bmatrix}
\vek{n}{}{A1} & \vek{o}{}{A1} & \vek{a}{}{A1}
\end{bmatrix}
\end{equation}
%
and the partial frame rotation
%
\begin{equation}
\rotmat{0}{A2}
=
\rotmat{0}{E}(\bm{q})
\left(\bm{R}_y(\alpha_2) \bm{R}_x(\alpha_3)\right)^\transp
=
\begin{bmatrix}
\vek{n}{}{A2} & \vek{o}{}{A2} & \vek{a}{}{A2}
\end{bmatrix}
\end{equation}
%
from the actual frame and the $x$- and $y$-axis error components.
%
The frames $\ks{A1}$, $\ks{A2}$ and $\ks{D}$ all share the same $z$-axis
%
\begin{equation}
\vek{a}{}{D}
=
\vek{a}{}{A1}
=
\vek{a}{}{A2}
\label{equ:z_axis_equal}
\end{equation}
%
which is also the tool axis, since transformations between these frames are only rotations $\bm{R}_z$ around the $z$-axes from (\ref{equ:z_axis_equal}).
\newpage
Since the tool axis rotation $\beta_3$ and the orientation error component $\alpha_1$ are defined around the same axis, $\beta_3$ only influences $\alpha_1$ and not $\alpha_2$ and $\alpha_3$, which can be expressed by
%
\begin{align}
\begin{bmatrix}
\alpha_1 \\
\alpha_2 \\
\alpha_3
\end{bmatrix}
=
\begin{bmatrix}
\alpha_1(\bm{q},\beta_1,\beta_2,\beta_3) \\
\alpha_2(\bm{q},\beta_1,\beta_2) \\
\alpha_3(\bm{q},\beta_1,\beta_2)
\end{bmatrix}
=
\begin{bmatrix}
\alpha_1(\bm{q},\bm{x}) \\
\alpha_2(\bm{q},\bm{\eta}) \\
\alpha_3(\bm{q},\bm{\eta})
\end{bmatrix}
\label{equ:alpha_dep_beta}.
\end{align}
%
This property of the reciprocal sets of Euler angles allows a kinematic description of robots in 3T2R tasks, as elaborated in the next sections.
It can be derived by symbolically comparing $\bm{\alpha}$ of (\ref{equ:alpha_def_rotmat_zyx}) for two different desired rotations $\bm{\beta}$ which only differ regarding a rotation $\beta_3$ around the tool axis.

\section{Application on the Inverse Kinematics of 3T2R Tasks}
\label{sec:RecEulAng_3T2R_app}

The standard methods, introduced in Sec.\,\ref{sec:Intro} for solving the inverse kinematics for 3T2R tasks and exploiting the functional redundancy, struggle with the definition of a Jacobian matrix with appropriate dimensions.

\subsection{Jacobian for Gradient-based Inverse Kinematics}

To obtain a Jacobian with minimal row dimension, the kinematic condition for the 3T2R problem in the coordinates $\bm{\eta}$ is now defined as
%
\begin{equation}
\bm{\Psi}=\begin{bmatrix}
\bm{\Psi}_{\mathrm{t}}^\transp & \bm{\Psi}_{\mathrm{r}}^\transp
\end{bmatrix}^\transp \in {\mathbb{R}}^{5}
\end{equation}
%
following the definition for $\bm{\Phi}$ from (\ref{equ:Phi_def}).
The translational part
%
\begin{equation}
\bm{\Psi}_{\mathrm{t}}(\bm{q},\bm{\eta})
=
\bm{\Phi}_{\mathrm{t}}(\bm{q},\bm{x})
=
- \bm{\eta}_{\mathrm{t}} + \ortvek{0}{r}{}{E}(\bm{q}) \in {\mathbb{R}}^{3}
\end{equation}
%
remains unchanged to (\ref{equ:Phit_def}).
The first component of the rotational part from (\ref{equ:Phir_def}) is omitted by the selection matrix $\bm{P}_{\Psi}$, since it corresponds to the orientation error $\alpha_1$ around the tool axis, leaving
%\vspace{-0.5em}
%
\begin{equation}
\bm{\Psi}_{\mathrm{r}}(\bm{q},\bm{\eta}) =
\begin{bmatrix}
\alpha_2  & \alpha_3
\end{bmatrix}^\transp
=
\overbrace{\begin{bmatrix}
    0 & 1 & 0  \\
    0 & 0 & 1
    \end{bmatrix}}^{=\bm{P}_{\Psi}}
 \bm{\Phi}_{\mathrm{r}} (\bm{q},\bm{x})
 \in {\mathbb{R}}^{2}.
\label{equ:Psir_def}
\end{equation}
%
The dependence on $\bm{\eta}$ and not on $\bm{x}$ can be explained by using (\ref{equ:alpha_dep_beta}) and (\ref{equ:etar_def}) together with $\rotmat{A1}{A2}=\bm{R}_z(\beta_3+\alpha_1)$ from Fig.\,\ref{fig:frames_5dof_6dof}\,(b) which results to
%
\begin{align}
\bm{\Psi}_{\mathrm{r}}(\bm{q},\bm{\eta})
&=
\bm{P}_{\Psi} \bm{\alpha}\left(\rotmat{A2}{E}(\bm{q},\bm{\eta}_{\mathrm{r}},\alpha_1,\beta_3)\right) \nonumber\\
&=
\bm{P}_{\Psi} \bm{\alpha}\left(\rotmat{0}{A1}^\transp (\bm{\eta}_{\mathrm{r}})\rotmat{0}{E}(\bm{q})\right).
\end{align}

The condition $\bm{\Phi}=\bm{0}$ or $\bm{\Psi}=\bm{0}$ leads to a valid configuration of the end-effector position and the complete orientation of the end-effector (using $\bm{\Phi}$) or the orientation only of the tool-axis (using $\bm{\Psi}$).

Following \cite{GoldenbergBenFen1985}, the inverse kinematics problem for serial link robots in 6-DoF tasks at the iterative step $k+1$ can be derived with the linear approximation of the Taylor series expansion of $\bm{\Phi}(\bm{q},\bm{x})$ to
%
\begin{equation}
\bm{\Phi}(\bm{q}^{k+1},\bm{x}) =
\bm{\Phi}(\bm{q}^{k},\bm{x})
+
\frac{\partial}{\partial \bm{q}} \bm{\Phi}(\bm{q},\bm{x}) \biggr\rvert_{\bm{q}^k} (\bm{q}^{k+1} - \bm{q}^k)
\label{equ:taylor_phi}
\end{equation}
%
where $\bm{\Phi}_{\partial\bm{q}}=(\partial \bm{\Phi} / \partial \bm{q})$ is called the ``Jacobian matrix corresponding to the residual vector'' ($\bm{\Phi}$) in \cite{GoldenbergBenFen1985} and $\bm{q}^0$ is assumed as given.
%
For 3T2R tasks
%
\begin{equation}
\bm{\Psi}(\bm{q}^{k+1},\bm{\eta}) =
\bm{\Psi}(\bm{q}^{k},\bm{\eta})
+
\frac{\partial}{\partial \bm{q}} \bm{\Psi}(\bm{q},\bm{\eta}) \biggr\rvert_{\bm{q}^k} (\bm{q}^{k+1} - \bm{q}^k)
\label{equ:taylor_psi}
\end{equation}
%
can be defined in the same way with the condition
%
\begin{equation}
\bm{\Psi}(\bm{q}^{k+1},\bm{\eta})=\bm{0}
\end{equation}
%
for solution of the inverse kinematics in the next step $k+1$. The increment
%
\begin{equation}
\Delta \bm{q}^k
=
(\bm{q}^{k+1} - \bm{q}^k)
=
\left(\frac{\partial \bm{\Psi}(\bm{q},\bm{\eta})}{\partial \bm{q}}\biggr\rvert_{\bm{q}^k}\right)^{\dagger}
(\bm{0} - \bm{\Psi}(\bm{q}^{k},\bm{\eta}))
\label{equ:deltaq_psi}
\end{equation}
%
of the joint angles towards this solution can be used in iterative algorithms like Newton-Raphson together with methods to adapt the step sizes to ensure convergence.
Depending on the dimension of the matrix, $(\cdot)^\dagger$ denotes the matrix inverse or the pseudo-inverse.

\subsection{Discussion of Singularities}

In \cite{Zlajpah2017}, quaternions were used in favor of Euler angles for the orientation error with the reasons that a singularity-free representation of $\bm{\Phi}=\bm{0}$ is needed.
This is the case for Tait-Bryan angles\footnote{Tait-Bryan angles are referred to as $A$-$B$-$C$-Euler angles with axes $A \ne C$ as opposed to ``proper Euler angles'' with $A = C$. The elementary axes are $A,B,C \in \{ X,Y,Z\}$.} like e.\,g. the $Z$-$Y$-$X$ notation used here for $\bm{\alpha}$.
For control purposes it can be assumed that the components of the orientation error $\bm{\alpha}$ always stay below $\pm$90$^\circ$, which avoids the ``gimbal lock'' representation singularity of Euler angles.
This assumption can be justified by the consideration that active and effective position and orientation tracking will only produce small errors.
A singularity-free representation of the absolute orientation $\bm{\beta}$ has to be ensured at the phase of motion planning, as well as avoiding discontinuities of the trajectory.

\subsection{Remarks on Differentiation and Rotation Matrices}
\label{sec:RecEulAng_implement}

The Jacobians $\bm{\Phi}_{\partial\bm{q}}$ of (\ref{equ:taylor_phi}) or $\bm{\Psi}_{\partial\bm{q}}$ of (\ref{equ:taylor_psi}) consist of nested non-linear functions and do not use the geometric Jacobian of the serial link manipulator for the rotational part, which is easy to calculate.

However, $\bm{\Phi}_{\partial\bm{q}}$ can be implemented efficiently by exploiting the chain rule and sparsity of the matrices with partial derivatives as shown in the following.

The column operator $\overline{\bm{R}}$ for rotation matrices $\bm{R}$ to stack the coordinate systems unit vectors $\bm{n},\bm{o},\bm{a} \in {\mathbb{R}}^{3}$ vertically instead of horizontally is defined as
%
\begin{equation}
\overline{\bm{R}}(\bm{R})=\begin{bmatrix}
\bm{n} \\ \bm{o} \\ \bm{a}
\end{bmatrix} \in {\mathbb{R}}^{9}
\quad
\mathrm{with}
\quad
\bm{R}=\begin{bmatrix}
\bm{n} & \bm{o} & \bm{a}
\end{bmatrix}
=
\begin{bmatrix}
{n_x}&{o_x}&{a_x} \\
{n_y}&{o_y}&{a_y} \\
{n_z}&{o_z}&{a_z} \\
\end{bmatrix}
 \in \mathrm{SO}(3)
\label{equ:def_rotmat}
\end{equation}
%
to avoid differentiating matrices or w.r.t. matrices.
Matrix multiplication is then expressed with the matrix product operator $\overline{\Pi}$
%
such that
%
\begin{equation}
\rotmato{1}{3}
=
\overline{\prod}\left( \rotmato{1}{2}, \rotmato{2}{3}\right)
=
\overline{\bm{R}}(\rotmat{1}{3})
\quad
\mathrm{with}
\quad
\rotmat{1}{3}
=
\rotmat{1}{2}
\rotmat{2}{3}.
\label{equ:matprod}
\end{equation}
%
The transposition operator $\bm{P}_\transp$ is a $9 \times 9$ permutation matrix such that
%
\begin{equation}
\rotmato{2}{1}
=
\bm{P}_\transp \rotmato{1}{2}
\in {\mathbb{R}}^{9}
\enspace
\mathrm{with}
\enspace
\rotmat{2}{1}
=
\rotmat{1}{2}^\transp
\in \mathrm{SO}(3)
\enspace
\mathrm{and}
\enspace
\rotmato{1}{2}=\overline{\bm{R}}(\rotmat{1}{2})
.
\end{equation}
%
The Euler angles can be calculated from the general rotation matrix $\bm{R}$ of (\ref{equ:def_rotmat}) in the same way as before\footnote{Utilizing the sign-aware operator $\arctantwo(y,x)$ instead of $\arctan(y/x)$ allows angles to be in $(-\pi,+\pi]$, removes ambiguities and provides global differentiability.} using these operators with the notation
%
\begin{equation}
\bm{\alpha}(\overline{\bm{R}})
=
\bm{\alpha}(\bm{R})
=
\begin{bmatrix}
\arctantwo \left( {n_y} , { n_x} \right) \\
\arctantwo \left( -{n_z} , \sqrt {{{a_z}}^{2}+{{ o_z}}^{2}} \right) \\
\arctantwo \left( {o_z} , {a_z} \right)
\end{bmatrix}
\label{equ:alpha_def_rotmat_zyx}
\end{equation}
%
at the $Z$-$Y$-$X$ example.
Finally, applying this to the rotational part of the residual vector Jacobian and using the chain rule for differentiation yields
%
\begin{align}
\frac{\partial}{\partial \bm{q}}\bm{\Phi}_{\mathrm{r}}
&=
\frac{\partial}{\partial \bm{q}} \bm{\alpha}\left(\rotmat{0}{D}^\transp(\bm{x}) \rotmat{0}{E}(\bm{q})\right) \label{equ:grad_Phi_q}\\
&=
\frac{\partial}{\partial \bm{q}} \bm{\alpha}\left(\overline{\prod}\left( \rotmato{0}{D}^\transp(\bm{x}), \rotmato{0}{E}(\bm{q})\right)\right) \nonumber \\
&=
%underbrace für Geschweifte Klammern drunter, vphantom nur für vertikale Größe des mittleren Terms
\underbrace{\vphantom{\left(\frac{\partial \overline{\prod}\left(\rotmato{0}{D}^\transp, \rotmato{0}{E}\right)}{\partial \rotmato{0}{E}}\right)}\left(\frac{\partial \bm{\alpha}}{\partial \overline{\bm{R}}}\right)}_{\mathrm{I} \in {\mathbb{R}}^{3 \times 9}}
\underbrace{\left(\frac{\partial \overline{\prod}\left(\rotmato{0}{D}^\transp, \rotmato{0}{E}\right)}{\partial \rotmato{0}{E}}\right)}_{\mathrm{II} \in {\mathbb{R}}^{9 \times 9}}
\underbrace{\vphantom{\left(\frac{\partial \overline{\prod}\left(\rotmato{0}{D}^\transp, \rotmato{0}{E}\right)}{\partial \rotmato{0}{E}}\right)}\left(\frac{\partial \rotmato{0}{E}(\bm{q})}{\partial \bm{q}} \right)}_{\mathrm{III} \in {\mathbb{R}}^{9 \times \mathrm{dim}(\bm{q})}}.  \nonumber
\end{align}
%
The first two partial derivatives ``I'' and ``II'' from (\ref{equ:grad_Phi_q}) are sparse matrices of low complexity where the elements of $\rotmat{D}{E}$ and $\rotmat{0}{D}$ have to be inserted.
The last partial derivative ``III'' can be derived efficiently with computer algebra systems.

The translational part $\bm{\Phi}_{\mathrm{t},\partial\bm{q}}=\partial \bm{\Phi}_{\mathrm{t}} / \partial \bm{q}=\bm{\Psi}_{\mathrm{t},\partial\bm{q}}$ is enclosed in the geometric Jacobian of the manipulator and is not considered at this point to focus on the rotational aspects.
The gradient
$\bm{\Psi}_{\mathrm{r}, \partial\bm{q}}=\partial \bm{\Psi}_{\mathrm{r}} / \partial \bm{q}=\bm{P}_{\Psi}\bm{\Phi}_{\mathrm{r},\partial\bm{q}}$
is obtained from the results of (\ref{equ:grad_Phi_q}) with the selection matrix $\bm{P}_{\Psi}$ from (\ref{equ:Psir_def}).

To account for exchangeable end-effectors or tools, a distinguished frame rotation $\rotmat{0}{N}(\bm{q})$ to the last robot link frame and a constant frame rotation $\rotmat{N}{E}$ to the end-effector frame can be used.
The properties of the column-operator allow then to substitute the last term ``III'' in (\ref{equ:grad_Phi_q}) with
%
\begin{align}
\frac{\partial \rotmato{0}{E}(\bm{q})}{\partial \bm{q}}
&=
\frac{\partial}{\partial \bm{q}} \overline{\prod}\left( \rotmato{0}{N}(\bm{q}), \rotmato{N}{E}\right) \label{equ:ee_rotation_gradq}\\
&=
\left(\frac{\partial}{\partial \rotmato{0}{N}} \overline{\prod}\left( \rotmato{0}{N}, \rotmato{N}{E}\right)\right)
\left(\frac{\partial}{\partial \bm{q}} \rotmato{0}{N}(\bm{q})\right). \nonumber
\end{align}
%
%The gradient ``I'' in (\ref{equ:ee_rotation_gradq}) matrix with linear entries where $\rotmat{N}{E}$ can be inserted directly similar to ``II'' in (\ref{equ:grad_Phi_q}).

\section{Resolving Functional Redundancy for 3T2R Tasks}
\label{sec:ResFuncRed}

%with $\mathrm{dim}(\bm{\Psi})=5$

The inverse kinematics formalism (\ref{equ:deltaq_psi}), described in the previous section \ref{sec:RecEulAng_3T2R_app}, does not take into account the functional redundancy yet.

The typical scenario for 3T2R tasks in industry is a serial link robot with $\mathrm{dim}(\bm{q})>5$.
Most commonly a classical industrial robot with $\mathrm{dim}(\bm{q})=6$ will be used.
Since $\mathrm{dim}(\bm{\Psi})=5$, at least one DoF is free for optimization of additional criteria.

For the sake of simplicity, as an additional criterion the summed $\bm{W}$-weighted quadratic distances
\vspace{-0.1em}
%
\begin{equation}
h(\bm{q})
=
\frac{1}{2} (\bm{q}-\bar{\bm{q}})^\transp\bm{W}(\bm{q}-\bar{\bm{q}})
\end{equation}
%
of the joint positions $\bm{q}$ from their respective reference position $\bar{\bm{q}}$ will be used.
Minimizing $h(\bm{q})$ avoids the risk of joints reaching their technical limits.
The gradient
\vspace{-0.1em}
%
\begin{equation}
h_{\partial\bm{q}}
=
\frac{\partial h}{\partial \bm{q}}
=
\bm{W}(\bm{q}-\bar{\bm{q}})
\end{equation}
%
can be used to include this additional minimization into the solution of the inverse kinematics.
The gradient $h_{\partial\bm{q}}$ is projected into the nullspace of $\bm{\Psi}_{\partial\bm{q}}$ with
\vspace{-0.1em}
%
\begin{align}
{\Delta}\bm{q}
&=
{\Delta}\bm{q}_{\mathrm{T}} + {\Delta}\bm{q}_{\mathrm{N}} \nonumber \\
&=
\bm{\Psi}_{\partial\bm{q}}^{\dagger} (-\bm{\Psi}) +  (\bm{1}-\bm{\Psi}_{\partial\bm{q}}^{\dagger}\bm{\Psi}_{\partial\bm{q}}) h_{\partial\bm{q}}
\label{equ:nullspace}
\end{align}
%
where the nullspace incremental motion ${\Delta}\bm{q}_{\mathrm{N}}$ does not affect the task achievement ensured via ${\Delta}\bm{q}_{\mathrm{T}}$ \cite{Yoshikawa1984}.

It is reported in \cite{GuoDonKe2015} that their optimization does not work for industrial robots where the tool axis is aligned parallel to the last robot joint axis.
This ``pointing configuration'' can also not be addressed by the nullspace projection from (\ref{equ:nullspace}), since the nullspace corresponds to the last robot axis and (in the case of a six-DoF robot)
\vspace{-0.1em}
%
\begin{equation}
\bm{1}-\bm{\Psi}_{\partial\bm{q}}^{\dagger}\bm{\Psi}_{\partial\bm{q}}
=
\begin{bmatrix}
\bm{0}_{5 \times 5} & \bm{0}_{5 \times 1} \\
\bm{0}_{1 \times 5} & 1_{1 \times 1}
\end{bmatrix}
\end{equation}
%
only projects the gradient $h_{\partial\bm{q}}$ of additional criteria onto the last joint.
Therefore, the method does only work if the tool is mounted in a different configuration (``side'' or ``hanging'' in \cite{GuoDonKe2015}), which might be unfavorable for some end-effectors or tasks.

\section{Conclusions}
\label{sec:Conclusion}
\vspace{-0.2em}
This paper presented a novel concept to formulate the inverse kinematics problem of serial kinematic chains using reciprocal sets of Euler angles.
This exploits the properties of Euler angles to reduce the number of coordinates required for 3T2R tasks which are of high industrial relevance.
Applications to the inverse kinematics of serial robots are given.
%The detailed view on the practical implementation aims at facilitating the understanding of the approach and at reducing the barrier for potential users.
%
Future works will include comparative simulative evaluation against state of the art methods, inquiries on singularities and the relation to the geometric Jacobian and the application to parallel robots.

\section*{Acknowledgements}
\vspace{-0.2em}
The financial support from the Deutsche Forschungsgemeinschaft (DFG) under grant number OR 196/33-1 is gracefully acknowledged.
\vspace{-0.2em}

% BIBLIOGRAPHY
\bibliographystyle{spmpsci_unsrt}
\bibliography{ikfr_ref}

\end{document}


\chapter{Conclusion}
\label{chap:conclusion}

% What we did and what others did
We have formalised multi-tape Turing machines in Coq.  We developed a framework for programming and proving correctness and time complexity of
multi-tape Turing machines.  We have demonstrated the power of this framework by implementing and verifying a multi-tape Turing machine that simulates
an abstract machine.  This machine is a variation of the heap machine in Kunze et~al.~\cite{KunzeEtAl:2018:Formal}.  The two variants differ in that
programs in our version are linearised lists of commands.  In~\cite{KunzeEtAl:2018:Formal}, the authors show that their heap machines can simulate
terms of the programming language $L$, which is a subset of the call-by-value $\lambda$-calculus.  It should be easy to formalise the reduction from
the heap machine in~\cite{KunzeEtAl:2018:Formal} to our version.  By that, we would formalise the reduction from the halting problem of $L$ to the
halting problem of multi-tape Turing machines.  This is, however, beyond the scope of this thesis.  It is an ongoing research
project~\cite{ForsterLOLA2018}, to implement a Coq library of undecidability reductions, and this thesis provides one step towards this goal.  The
reduction from the halting problem of single-tape Turing machines to the Post correspondence problem (PCP) has already been mechanised in Coq
in~\cite{forster2018verification}.

\paragraph{Differences to other mechanisations of Turing machines}
We build on Asperti and Ricciotti`s framework from~\cite{asperti2015} inside the theorem prover Matita, and initially ported their definitions of
tapes and Turing machines to Coq.  We find their inductive definition of tapes appealing, because of its symmetric and finite nature.  Because of the
symmetric nature of their definition of tapes, it was easy to define an operator $\MS{Mirror}$ that mirrors the transition function of a machine.
This is in contrast to the implementation of tapes in~\cite{xu2013}, where tapes are split into two halves, and the right half contains the current
symbol.  The finite nature made it possible to define an always terminating machine that moves the head to the right (or using $\MS{Mirror}$ to the
left) end of the tape.  This is in contrast to~\cite{ciaffaglione2016}, where tapes are implemented as infinite streams of symbols.  Our framework
implements five major improvements on~\cite{asperti2015}.  (1) By introducing labelled machines we make it unnecessary to reason about concrete states
of machines.  The authors in~\cite{asperti2015} already note that reasoning about internal states is tedious and therefore do not include the
terminating state in their definition of realisation.  However, they also need a separate definition of realisation that includes the terminating
state.  (2) We have introduced a notion of time complexity that relates the inputs to the number of steps needed for the computation.  (3) By
introducing an operator $\Switch$ that generalises sequential composition and conditional, we simplified the verification of both operators and also
introduced a useful operator that was used throughout the thesis.  (4) We implemented general lifting operators that make it possible to compose small
machines (w.r.t.\ the alphabet and number of tapes) to fairly complex machines.  At this point, composing compound machines is reasonably easy, but we
(5) have introduced another layer of abstraction.  We have made it possible to directly manipulate values of encodable types.

% Norrish`s comment about the "daunting prospect".
In~\cite{norrish2011mechanised}, Norrish concludes that:
\begin{quote}
  If register machines are unappealing because of their general fiddliness, Turing machines are an even more daunting prospect.
\end{quote}
Certainly, Turing machines are not appealing.  We have spent considerable efforts (ca.\ one year) to make programming and verifying Turing machines
feasible.  Interestingly, we ended up at a point, where programming and verifying Turing machines can be (in some sense) \textit{easier} than for
register machines, because register machines are restricted to natural numbers.  The on-paper design, implementation and verification of the simulator
was finished in three weeks.

\paragraph{Duality of realisation and termination}
We noted that our concepts for correctness and time complexity are dual in a sense.  The (weak) notion of realisation says that \textit{if} the
machine terminates, then the output is correct w.r.t.\ a correctness relation $R$.  On the other side, a machine terminates in a termination relation
$T$, if for all pairs of input tapes $t$ and step numbers $k$ that are in $T$, the machine terminates in $k$ steps given the input $t$.  Realisation
is monotone (cf.\ Lemma~\ref{lem:Realise_monotone}), and termination is anti-monotone (cf.\ Lemma~\ref{lem:TerminatesIn_monotone}).  We find it
remarkable that we use an inductive correctness relation for $\MS{While}$ (cf.~Lemma~\ref{lem:While_Realise}) and a co-inductive running time relation
(cf.\ Lemma~\ref{lem:While_TerminatesIn}).

\paragraph{Similarity of realisation and Hoare logic}
As already noted in~\cite{ciaffaglione2016}, the notion of realisation is similar to Hoare logic, that is widely used for program verification.  For
example, consider the Hoare proof rule for sequential composition and the corresponding relational rule (for unlabelled machines
$M_1,M_2 : \TM_\Sigma^n$):
\[
  \inferrule{\{A\}~P_1~\{B\} \and \{B\}~P_2~\{C\}}{\{A\}~P_1 \Seq P_2~\{C\}}
  \qquad
  \inferrule{M_1 \Realise R_1 \and M_2 \Realise R_2}{M_1 \Seq M_2 \Realise R_1 \circ R_2}
\]
Sequential composition of machines amounts to relational composition of correctness relations (cf.\ Lemma~\ref{lem:Seq_RealiseIn}).  We encode
preconditions and postconditions inside correctness relations.  This means that if the precondition does not hold for input tapes $t$, this implies
$R~t~(l,t')$.  We are not aware of a Hoare-style calculus for reasoning about termination in a concrete number of steps related to the input, that is
dual to Hoare logic, like in our duality between realisation and termination.

\paragraph{Problems of the framework}
The biggest problem of this framework is that encodability of types can be ambiguous.  For example, there are more than three ways how to encode
natural numbers on the alphabet of the heap machine simulator (cf.~Section~\ref{sec:Lookup}).  We had to mentally keep track of in which encoding a
value is encoded on a tape, and to translate values from one encoding to another.  The greatest part of the total compilation time (which is less then
5~minutes) consists of rewriting tapes.  This could probably be further optimised.

\paragraph{Comparison of proof assistants}
Asperti and Ricciotti~\cite{asperti2015} propose the formalisation of Turing machines as a benchmark for comparing proof assistants.  We think that
the formalisation and usability of finite sets could be a benchmark for itself.  However, the task ``formalise Turing machines in proof assistant
$X$'' is rather broad.  There are many mathematical formalisations of Turing machines, and some might be easier to implement in one or the other proof
assistant.  For example, Isabelle does not support dependent types, so this concrete formalisation of Turing machines in this thesis would not be
possible in Isabelle.  Dependent types are quite central in our formalisation of Turing machines.  For example, defining $\Switch$ without them would
probably be considerable harder.

\paragraph{Future work}

\enlargethispage{0.5cm}

When we defined the notion of value-containment (cf.~Section~\ref{sec:value-containment}), we had future work in mind where we formalise space-usage
of machines.  We were careful to avoid memory-leaks in the machines, but have not yet formalised this aspect of correctness.  We can strengthen the
correctness relations with commitments about the space-usage of each tape.  Asperti and Ricciotti`s inductive definition of tapes is very helpful in
this regard, because their tapes never decrease the number of symbols.  This means that the total space usage of a tape is just the number of symbols
on the tape.  Our idea is that we parametrise the definition of value-containment over the length $l$ of the ``rest list'' on the left, and write
$t \simeq_{l} x$.  Note that, by definition, there are no symbols beyond the stop symbol on the right side of the tape, so the total size of the tape
only depends on the length of the encoding and the length of the left rest.  For example, $\MS{CaseNat}$ does not change the amount of totally
allocated symbols, but decreases the length of the encoding and increases the length of the rest by one.  On the other side, $\MS{ConstrS}$
``consumes'' one rest symbol, i.e.\ it decreases the length of the rest by one and increases the length of the encoding by one.  Thus, if the rest is
empty, $\MS{ConstrS}$ allocates one new symbol.

%\newpage

Further future work could be to show that the running time function of the simulator is polynomial w.r.t.\ the number of steps and the length of the
encoding of the initial heap machine state, see~\cite{ForsterLOLA2017}.  We could also formalise the reduction from multi-tape Turing machines to
single-tape Turing machines, and from single-tape Turing machines to single-tape Turing machines with a binary alphabet.  The framework can be used to
program other simulator machines, for example, for the ``naive'' substitution-based machine in~\cite{KunzeEtAl:2018:Formal}.  We could implement a
universal Turing machines as in~\cite{asperti2015}, and formalise results of computationally and complexity theory, for example the undecidability of
the halting problem and Rice`s theorem.  The opposite reduction from multi-tape Turing machines to $L$, i.e.\ programming an $L$ expression that
simulates multi-tape Turing machines, is also open for future work.  This should be a ``less daunting prospect'', because there is a framework for
verified extraction of Coq terms to expressions of $L$, see~\cite{forster2016verified}.

%%% Local Variables:
%%% TeX-master: "thesis"
%%% End: