% 202010??
\documentclass[env.tex]{subfiles}
\begin{document}

\subsection{The model}
The system is a set of ordinary differential equations (ODE).  It is a temperature-static system with two biological components (\phy\ and \bac) and three density state variables (organic carbon $C$, \phy\ biomass $P$ and \bac l biomass $B$; Fig.\ref{f:model}). \Phy\ and \bac\ receive carbon from different sources (\phy\ photosynthesize carbon dioxide from an external unlimited source; \bac\ consume organic carbon from the internal finite $C$ pool) but allocate carbon in the same way (respiration, leakage and biomass incorporation).  Some biomass from \phy/\bac\ die and become part of the $C$ pool.  Organic carbon in the environment can either be consumed by \bac, harvested out of the system or remained in the $C$ pool only.  There are four major assumptions:  \Rn{1}) homogeneous environmental conditions and light intensity (i.e. a system shaped as a thin panel); \Rn{2}) unlimited growth nutrient for \phy\ and \bac; \Rn{3}) constant system temperature; \Rn{4}) \bac\ have no carbon type preferences on consumption.  In short, living space for \phy\ is the only limiting factor.

The rate of change of state variables (carbon density per day, \dxdt) are the currencies in our equations.  The equations are composed of three state variables of carbon densities, four life history traits of \phy, four life history traits of \bac\ and one harvest rate parameter (Eq.\ref{eq:PBH}).

\begin{equation}\left\{\begin{array}{rl}
    C'(t) &= \ePR(1-\eP)\cdot\gP\cdot P +\aP\cdot P^2 +(\eBR(1-\eB)-1)\cdot\gB\cdot C\cdot B +\mB\cdot B -xC\\
    P'(t) &= \ePR\cdot\eP\cdot\gP\cdot P -\aP\cdot P^2\\
    B'(t) &= \eBR\cdot\eB\cdot\gB\cdot C\cdot B -\mB\cdot B
\end{array}\right.\label{eq:PBH}\end{equation}

In Eq.\ref{eq:PBH}, $C'(t)$, $P'(t)$ and $B'(t)$ are the rates of density change (\dxdt).  $\ePR$ is the fraction of non-respired carbon for $P$, $\eP$ is the fraction of carbon incorporated as $P$ biomass, $\eBR$ is the fraction of non-respired carbon for $B$ and $\eB$ is the fraction of carbon incorporated as $B$ biomass.  These four are ``fraction parameters”.  $\gP$ is the growth rate of $P$ (\dayU), $\aP$ is the intraspecific interference of $P$ (\denI), $\gB$ is the resource clearance rate of $B$  (\denI) and $\mB$ is the death rate of $B$ (\dayU).  These four are ``rate parameters”.  $x$ is the harvest rate (\dayU), which is the speed of carbon harvest from the $C$ pool in a continuous harvest system.  $x$ is set to zero in destructive harvest systems and the harvest interval $T$ (day) is the alternative parameter for the yield measurement.  The interval is defined as

\begin{equation*}
    T = x-1
    \label{eq:TvsX}
\end{equation*}

Values of equivalent $T$ and $x$ differ by 1 because the day of system establishment is day 0.  Continuous harvest starts on day 0 but destructive harvest starts on day 1.  Carbon yield from continuous harvest on the first day is equivalent to that by destructive harvest on the second day.

Daily yield in continuous harvest systems is defined as

\begin{equation}
    \text{daily yield} = x\cdot C
    \label{eq:yield}
\end{equation}

and the equivalent for destructive harvest is ``average yield", which is defined as

\begin{equation*}
    \text{average yield} = \dfrac{[\text{total carbon}]|_{T}-[\text{total carbon}]|_{0}}{T}
    \label{eq:avgYd}
\end{equation*}

\begin{figure}[H]
    \centering
    \includegraphics[width=.8\linewidth]{sec/model.png}
    \caption[Model visualization]{The theoretical coexistence open system of \phy\ and \bac\ allows carbon exchange with external carbon dioxide source.  The dashed box is the defined boundary allowed matter exchange.  Blue arrows are the directions of carbon flow.  Pink boxes are the carbon pools (i.e. state variables).  White boxes are biochemical processes in indicated organisms.  These processes divert carbon from source to different carbon pools.}
    \label{f:model}
\end{figure}

Eq.\ref{eq:PBH} is a \pbs\ of $P$ and $B$ under continuous harvest (\PBH).  Three systems on $P$-only continuous harvest (\PoH), $P$ and $B$ destructive harvest (\PBN) and $P$-only destructive harvest (\PoN) were branched.

Continuous harvest \phy-only system (\PoH);
\begin{equation*}\left\{\begin{array}{rl}
    C'(t) &= \ePR(1-\eP)\cdot\gP\cdot P +\aP\cdot P^2 -xC\\
    P'(t) &= \ePR\cdot\eP\cdot\gP\cdot P -\aP\cdot P^2
\end{array}\right.\label{eq:PoH}\end{equation*}

Destructive harvest \pbs\ of $P$ and $B$ (\PBN); and
\begin{equation*}\left\{\begin{array}{rl}
    C'(t) &= \ePR(1-\eP)\cdot\gP\cdot P +\aP\cdot P^2 +(\eBR(1-\eB)-1)\cdot\gB\cdot C\cdot B +\mB\cdot B\\
    P'(t) &= \ePR\cdot\eP\cdot\gP\cdot P -\aP\cdot P^2\\
    B'(t) &= \eBR\cdot\eB\cdot\gB\cdot C\cdot B -\mB\cdot B
\end{array}\right.\label{eq:PBN}\end{equation*}

Destructive harvest \phy-only system (\PoN).
\begin{equation*}\left\{\begin{array}{rl}
    C'(t) &= \ePR(1-\eP)\cdot\gP\cdot P +\aP\cdot P^2\\
    P'(t) &= \ePR\cdot\eP\cdot\gP\cdot P -\aP\cdot P^2
\end{array}\right.\label{eq:PoN}\end{equation*}

Three biologically meaningful system stable states for continuous harvest systems are deduced using SymPy (v1.5.1) in python3 (v3.7.3) (Table \ref{t:eqm}).  Equilibrium 2 \& 3 are \PoH\ \& \PBH\ respectively. Equilibrium 1 was a default alternative stable state with no carbon in the system.  Destructive systems can only be investigated through integration because the analytical solutions are invalid; these solutions consisted with state variables only but not biological parameters.

\begin{table}[H]
    \centering
    \caption[Model equilibria]{Equilibria from the four variations of the proposed model (Eq.\ref{eq:PBH})}
    \begin{tabular}{cl|ccc}\hline
        equilibrium & scenario & $C$ & $P$ & $B$ (\PBH\ only) \\\hline
        1 & \PBH, \PoH & 0 & 0 & 0 \\
        2 & \PBH, \PoH & $\dfrac{\eP(\ePR\gP)^2}{\aP x}$ & $\dfrac{\ePR\eP\gP}{\aP}$ & 0 \\
        3 & \PBH & $\dfrac{\mB}{\eBR\eB\gB}$ & $\dfrac{\ePR\eP\gP}{\aP}$ & $\dfrac{(\ePR\gP)^2\eBR\eB\gB-\aP\mB x}{(1-\eBR)\aP\gB\mB}$ \\\hline
    \end{tabular}
    \label{t:eqm}
\end{table}

\subsection{Parameter space}
Data of the defined parameters in Eq. \ref{eq:PBH} was collected from literature (Supplementary sections 3-9).  Data was temperature-standardized to \temp\ to align with most data collected (Supplementary section 2).  Parameters less than five data points were called ``data deficient"; half of the parameters in Table \ref{t:ranges} ($\aP$, $\eBR$, $\eB$, $\mB$) were data deficient.  The widest percentage range in respective parameter groups (fraction or rate parameters) was inferred as the logical range for these data-deficient parameters.  The selection was based on the Metabolic Theory of Ecology \autocite{brown2004toward}, which suggested that similar body size organisms had similar metabolic rates.  \Phy\ and \bac\ had similar cell sizes as unicellular organisms.  Similar life history traits for \phy\ and \bac\ therefore would potentially share parameter ranges.  Ranges for each parameter was rooted around the mean of collected data bounded by the percentage range.  In each parameter range, 11 evenly-spaced sample values were selected.  The values formed a defined space of parameter combinations for model simulations.

\begin{table}[H]
    \centering
    \caption[Algebra variables]{Biological variables and corresponding ranges framing the parameter space}
    \begin{tabular}{cclll}\hline
        variable & unit & description & min & max \\\hline
        $N'(t)$ & \dxdt & rate of change of respective carbon pool {\tiny($N=C,P,B$)} & - & - \\
        $N$ & \den & carbon density for respective pool {\tiny($N=C,P,B$)} & - & - \\
        $\ePR$ & - & non-respired carbon fraction for $P$ & 0.08 & 0.87 \\
        $\eP$ & - & assimilated carbon fraction for $P$ & 0.40 & 1.00 \\
        $\gP$ & \dayU & growth rate of $P$ & 0.03 & 3.17 \\
        $\aP$ & \denI & intraspecific interference of $P$ & 0.02 & 1.52 \\
        $\eBR$ & - & non-respired carbon fraction for $B$ & 0.13 & 1.00 \\
        $\eB$ & - & assimilated carbon fraction for $B$ & 0.07 & 0.82 \\
        $\gB$ & \denI & clearance rate of $B$ & 0.10 & 3.50 \\
        $\mB$ & \dayU & death rate of $B$ & 0.01 & 0.63 \\
    \hline\end{tabular}
    \label{t:ranges}
\end{table}

\subsection{Scenario sampling}
A set of 5500 (11 sample values $\times$ 500 expected sample frequency) unique biological parameter combinations were randomly selected via a uniform prior with seed number ``20192020" in R (v4.0.2).  This Latin Hypercube Sampling (LHS) method was to ensure the widest parameter space covered with a limited sample size.  200 evenly-spaced harvest rates ($x$) ranged 1-19901 \dayU were applied on each set of biological parameter combinations on continuous harvest systems.  An equivalent harvest interval samples ($T$ = 0-19900 days, 200 evenly-spaced samples) were applied for each set of biological parameter combinations on destructive harvest systems.

\subsection{Model experiment}
Carbon densities of continuous harvest systems were analytically calculated by C-lang (compiler gcc v7.5.0) using equilibrium positions from Table \ref{t:eqm}. Yield in \PoH\ was independent from harvest rates as defined in Eq.\ref{eq:yield}.  Carbon densities of destructive harvest systems were numerically integrated by python3 (v3.7.3) SciPy (v1.2.3) ``odeint" function using Eq. \ref{eq:PBH}.  Initial densities for \PBN\ and \PoN\ were $C=P=B=1$ and $C=P=1$, $B=0$ \den\ respectively.  I also integrated destructive systems in a log-spaced time (range of $T$ = 0-100 days) to observe the system behaviour before reaching stability.

\subsection{Carbon yield analysis}
Feasible solutions were defined as non-negative abundances in all state variables for a scenario.  Unfeasible solutions were set as invalid data (NA) for observing yield distributions within feasibility limits.  All four systems had right-skewed carbon yield distributions with extremely high-valued outliers.  Pairwise Wilcox signed rank tests in R (v4.0.2) with bonferroni adjustment and no paired situations were therefore chosen for median comparisons between systems.  Destructive harvest system development was observed on a log-spaced time; \bac l effect on each harvest mode was examined through selected harvest parameters; and life history influences were deduced by observing the log carbon yield distribution across the parameter range of each trait.

\end{document}


\chapter{Theoretical Background}
\section{Basics in Modelling Light in Computer Graphics}
\subsection{Radiometry}
One purpose of Computer Graphics is to simulate the interaction of light with a surface and how a real-world observer, such as a human eye, will perceive this. These visual sensations of an eye are modelled relying on a virtual camera which captures the emitted light from the surface. The physical basis to measure such reflected light is studied under radiometry which deals with measures on the electromagnetic radiation transferred from a source to a receiver. \\

Fundamentally, light is a form of energy propagation, consisting of a large collection of photons, whereat each photon can be considered as a quantum of light that has a position, a direction of propagation and a wavelength $\lambda$. A photon travels at a certain speed $v = c/n$, that depends only the speed of light $c$ and the refractive index $n$ through which it propagates. Its frequency is defined by $f = v/\lambda$ and its carried amount of energy $q$, measured in the SI unit Joule, is given by $q = hf= hv/\lambda n$ where $h$ is the Plank's constant. The total energy of a large collection of photons is hence $Q = \sum_i q_i$.

\subsection{Spectral Energy}

It is important to understand that the human eye is not equally sensitive to all wavelength of the spectrum of light and therefore responds differently to specific wavelengths. Remember that our goal is to model the human visual perception. This is why we consider the energy distribution of a light spectrum rather than considering the total energy of a photon collection since then we could weight the distribution according to the human visual system. So the question we want to answer is: How is the energy distributed across wavelengths of light? \\

One idea is to make an energy histogram from a given photon collection. For this we have to order all photons by their associated wavelength, discretize the wavelength spectrum, count all photons which then will fall in same wavelength-interval, and then, finally, normalize each interval by the total energy $Q$. This will give us a histogram which tells us the spectral energy $Q_{\lambda}$ for a given discrete $\lambda$ interval and thus models the so called spectral energy distribution $\footnote{Intensive quantities can be thought of as density functions that tell the density of an extensive quantity at an infinitesimal by a small interval or a point.}$.

\subsection{Spectral Power}
Rendering an image in Computer Graphics corresponds to capturing the color sensation of an illuminated, target scene at a certain point in time. Each color is associated with either a particular wavelength or is composed of a wavelength spectrum$\footnote{A wavelength spectrum is a collection of certain wavelengths. For example brown color is a composition of many wavelengths in the region of yellow, orange or read color in combination with low luminance.}$. Thus a color is directly related to a certain amount of energy. In order to determine the color of a to-be-rendered pixel of an image, we have to first get a sense of how much light (in terms of energy) passes through the area which the pixel corresponds to. We begin by considering the flow of energy $\Phi = \frac{\Delta Q}{\Delta t}$ transferred through this area over a unit period of time. This allows us to measure the energy flow through a pixel during a certain amount of time. In general, power is the estimated rate of energy production for light sources and corresponds to the flux. It is measured in the unit Watts, denoted by Q. Since power is a rate over time, it is well defined even when energy production is varying over time. As with Spectral Energy for rendering, we are really interested in the spectral power $\Phi_\lambda = \frac{\Delta \Phi}{\Delta \lambda}$, measured in Watts per nanometer.

\subsection{Spectral Irradiance}
Before we can tell how much light is reflected from a given point on a surface towards the viewing direction of an observer, we first have to know how much light arrives at this point. Since in general a point has no length, area or even volume associated, let us instead consider an infinitesimal area $\Delta A$ around a such a point. Then, we can ask ourself how much light falls in such a small area. Further while observing this process over a short period of time, the measured quantity gives us the the spectral irradiance $E$ as illustrated in figure $\ref{fig:irradiance}$. Summarized, this quantity tells us how much spectral power is incident on a surface per unit area and mathematically is equal:

\begin{equation}
 E_{\lambda} = \frac{\Phi_{\lambda}}{\Delta A}
\end{equation}

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.5]{background/irradiance.png}
  \caption[Irradiance]{Irradiance is the summed up radiance over all directions. The black border is representing a surface element.}
  \label{fig:irradiance}
\end{figure}

\subsection{Spectral Radiance}
When rendering an image we have to determine the color of each pixel of the image. Although irradiance tells us how much light is arriving at a point and gets reflected, it tells us nothing about the power distribution across different directions. The direction of the element is important because the human eye may perceive the brightness of an illuminated objects differently when looking from direction.

\begin{figure}[H]
  \centering
  \subfigure[Radiances is the density of photons per area per solid angle]{
    \includegraphics[scale=0.4]{background/radiancehemisphere.png}
    \label{fig:radiance}
  }
~
  \subfigure[Solid angle is the area of a surface patch on a sphere with radius R which is spanned by a set of directions]{
    \includegraphics[scale=0.6]{background/solidangle.png}
    \label{fig:solidangle}
  }
\caption[Concept of Radiance]{Illustration of the concepts of radiance and of solid angle and how they are related.}
\label{fig:radianceBasics}
\end{figure}
\noindent
This concept is described by the radiometric quantity radiance. Basically, radiance is the measure of light energy passing through or emitted from a small area around a point on a surface towards a given direction during a short period in time. More formally this is the spectral power emerging from an arbitrary point (an infinitesimal area around this point) and falls within a given solid angle (see figure$\footnote{Modified from a figure in Computer Graphics class 2012 in chapter \emph{Colors}}$ $\ref{fig:solidangle}$) in a specific direction (usually towards the observer) as shown in figure $\ref{fig:radiance}$. Formally, this leads us to the following mathematical formalism:

\begin{equation}
 L_{\lambda}(\omega) = \frac{d^2 \Phi_{\lambda}}{dA d\Omega} \approx \frac{\Phi_{\lambda}}{\Omega A}
\end{equation}

where $L$ is the observed spectral radiance in $Wm^-2 sr^-1$ in direction $\omega$$\footnote{The direction $\omega$ is determined by two angles, $\phi$ and $\theta$ like illustrated in figure $\ref{fig:radianceBasics}$}$, $\Theta_{\lambda}$ is the total power emitted, $\theta$ is the angle between the surface normal and the specified direction, $A$ is the area of the surface and $\Omega$ is the solid angle subtended by the observation or measurement. \\

It is useful to distinguish between radiance incident at a point on a surface and excitant from that point. Terms for these concepts sometimes used in the graphics literature are surface radiance $L_r$ for the radiance \textit{reflected} from a surface and field radiance $L_i$ for the radiance \textit{incident} at a surface.

\subsection{BRDF}
In order to render the colorization of an observed object, a natural question in Computer Graphics is what portion of the incident light a viewer will receive after reflection, after when he looks at an illuminated object. For any given surface which is illuminated from a certain direction $\omega_i$, we can ask ourself how much light is reflected from any point on this surface towards a viewing direction $\omega_r$. This is where the Bidirectional Reflectance Distribution Function (BRDF) comes into play, which is a radiometric quantity telling us how much light is reflected at an opaque surface. Mathematically speaking, the BRDF is the ratio of the reflected radiance pointing in the direction $\omega_r$ to the incident irradiance coming from the inverse direction of $\omega_i$ as illustrated in figure $\ref{fig:brdfillustration}$. Hence the BRDF is a four dimensional function defined by four angles $\theta_i$, $\phi_i$, $\theta_r$ and $\phi_r$.

\begin{figure}[ht]
  \centering
  \includegraphics[scale=0.5]{background/mybrdfmodel.png}
  \caption[BRDF Model]{Illustration of the BRDF model, where $\omega_i$ is pointing to the light source and the viewing direction is denoted by $\omega_r$. Both unit direction vectors are defined w.r.t to a surface normal $\mathbf{n}$ for every point on the surface.}
  \label{fig:brdfillustration}
\end{figure}

Formally, a BRDF for any given wavelength $\lambda$ is defined as:

\begin{align}
  BRDF_{\lambda}(\omega_i, \omega_r)
  & = \frac{dL_r(\omega_r)}{dE_i(\omega_i)} \nonumber \\
  & = \frac{dL_r(\omega_r)}{L_i(\omega_i)cos(\theta_i)d\omega_i}
  \label{eq:defbrdf}
\end{align}

Where $L_{r}$ is the reflected spectral radiance, $E_i$ is the incident spectral irradiance and $\theta_{\text{i}}$ is the angle between $\omega_{\text{i}}$ and the surface normal $\mathbf n$. Also, $L_i$ is the incident spectral radiance.

\subsection{Wavespectrum and Colors}
In order to see how crucial the role of human vision plays, let us consider the following definition of color by \textit{Wyszechkiu and Siles} mentioned in the Fundamentals of Computergraphics Book$\cite{fundcg}$, stating that \textit{"Color is the aspect of visual perception by which an observer may distinguish differences between two structure-free fields of view of the same size and shape such as may be caused by differences in the spectral composition of the radiant energy concerned in the observation"}. Therefore, similarly like the humans' perceived sensation of smell and taste, color vision is just another individual sense of perception giving us the ability to distinguish between different frequency distributions of light which we experienced as different colors.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.35]{background/lightspec.png}
  \caption[Visible Lightspectrum]{Frequency (top) and wavelength (bottom) of colors of the visible light spectrum$\footnotemark$.}
  \label{fig:colorspectrum}
\end{figure}
\footnotetext{Similar figure like used in computer graphics class 2012 in chapter colors}

In general, an eye consists of photoreceptor cells which are responsible for providing ability of color perception. A schematic of an eye is illustrated in figure $\ref{fig:humaneye}$. Basically, there are two specialized types of photoreceptor cells, cone cells which are responsible for color vision and rod cells, which allow an eye to perceive different brightness levels.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.35]{background/humaneye.png}
  \caption[humanayeschematic]{Schematic$\footnotemark$ of photoreceptor cells, cones and rods, in a human eye }
  \label{fig:humaneye}
\end{figure}
\footnotetext{image of illustration has been taken from \\ \texttt{http://en.wikipedia.org/wiki/Bidirectional\textunderscore reflectance\textunderscore distribution\textunderscore function}}

A human eye is made of three different types of cone cells, having their peak sensitivity in different wavelength ranges. More precisely, there are cone cells most sensitive to short wavelengths between $420 nm$ and $440 nm$, those which are most sensitive in the middle range between $530 nm$ and $550 nm$ and those which have their peak in the long range, from $560 nm$ to $580 nm$. Therefore, any color sensation in human color perception can therefore be described by just three parameters, corresponding to the levels of stimulus of these three types of cone cells.

\subsection{Colorspace}
\label{sec:colorspace}
In order to render accurate images of how a human observer sees its world, a mathematical model of the human color perception is required. Remember that color sensation is due to a visual stimulus processed by cone cells in an eye. A human eye contains three different types of cone cells. Therefore, one possible approach is to describe each kind of these cone cells with a function mapping each wavelength to a certain sensitivity. In the early 1920 from a series of experiments, the so called CIE RGB color space was derived (see figure $\ref{fig:ciergb}$). This space describes the response of cone cells of an average human individual, the so called standard observer. Basically, a statistically sufficiently large number of probands were exposed to different target light colors expressed by their wavelength. The task of each proband was to reproduce these target colors by mixing three given primary colors, red, green and blue light. The strength  of each primary color could be manually adjusted by setting their relative sensitivity. Those adjustment weights have been measured, aggregated and averaged among all probands for each primary color. This model describes each color as a triple of three real valued numbers$\footnote{note that there are  negative color weights possible in the CIE RGB colors space. This is why some human perceived color sensations could not be reconstructed using just an additive color model (adding three positively weighted primary values). Therefore, a proband was also allowed to move one of the primary colors to the target color and instead was supposed to reproduce this new color mix using the two remaining primaries (subtractive model). The value of the selected, moved primary was then interpreted as being negative weighted in an additive color model.}$, the so called tristimulus values. Summarized, these experiments provided certain weights of primary colors in order to match a color at a certain wavelength according to the average human color perception. However, some of these weights could have a negative value. \\

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.3]{introduction/ciergb.png}
  \caption[CIE RGB Color Matching Functions]{Plots$\footnotemark$ of CIE 1931 RGB Color matching functions showing the amounts of primaries needed to match a certain wavelength.}
\label{fig:ciergb}
\end{figure}
\footnotetext{These plots have been taken from \texttt{http://en.wikipedia.org/wiki/CIE\textunderscore 1931\textunderscore color\textunderscore space}}

The disadvantage of the CIE RGB colorspace is that some of its color weights are negative. Thus, scientist derived the CIE XYZ colorspace which as no negative color matching functions but is still additive$\footnote{Remember, the property of an additive colorspace is that any color can be represented as a weighted sum of matching functions of that color space.}$. Figure $\ref{fig:matchingfunction}$ visualizes the matching functions of the CIE XYZ space. Another property of the CIE XYZ space is that its Y component is representing the luminance of the corresponding color. Usually, the CIE XYZ space is used as a reference colorspace to define colorspace transformations. \\

Pragmatically speaking, color spaces describe the range of colors a camera can see, a printer can print or a monitor can display. Thus, formally we can define it as a mapping from a range of physically produced colors from mixed light to a standard objective description of color sensations registered in the eye of an observer in terms of tristimulus values.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.7]{background/somatchingfunctions.png}
  \caption[Color Matching Functions]{Plots of our CIE XYZ color matching functions we used for rendering}
  \label{fig:matchingfunction}
\end{figure}

Interpolating all measured tristimulus values gives us three basis functions, the CIE color matching functions $\overline{x}(\lambda)$, $\overline{y}(\lambda)$, $\overline{z}(\lambda)$. In figure $\ref{fig:matchingfunction}$ are the numerical description of the chromatic response of the observer. They can be thought of as the spectral sensitivity curves of three linear light detectors yielding the CIE tristimulus values X, Y and Z. \\

The tristimulus values for a color with a spectral power distribution $I(\lambda)$, are given in terms of the standard observer by:

\begin{align}
    X= \int_{\Lambda} I(\lambda)\,\overline{x}(\lambda)\,d\lambda \nonumber \\
    Y= \int_{\Lambda} I(\lambda)\,\overline{y}(\lambda)\,d\lambda \nonumber \\
    Z= \int_{\Lambda} I(\lambda)\,\overline{z}(\lambda)\,d\lambda
\label{eq:tristimulusvalues}
\end{align}
\noindent
where $\lambda$ is the wavelength of the equivalent monochromatic light spectrum $\Lambda = [380nm, 780nm]$. Note that it is not possible to build a display that corresponds to the CIE XYZ colorspace. For this reason it is necessary to design other color spaces, which are physically realizable, efficiently encoded, perceptually uniform and have an intuitive color specification. There are simple conversions between XYZ color spaces to other color space - such as the RGB colorspace -  described as linear transformations.

\subsection{Spectral Rendering}
When rendering an image, most of the time we are using colors described in a certain RGB color space. However, a RGB colorspace results from a colorspace transformation of the tristimulus values, which themselves are inherent to the human visual system. Therefore, many physical phenomenon are poorly modelled when they rely on RGB colors for rendering. Using only RGB colors for rendering is like assuming that a given light source emits light of only three particular wavelengths. But in reality this is rarely the case. Spectral rendering refers to using certain wavelength spectrum, for e.g. the human visible light spectrum, instead of simply using only the range of RGB values in order to render an illuminated scene. This captures the physical reality of specific light sources way more accurately. Keep in mind that even when we make use of a spectral rendering approach, we have to convert the final spectra to RGB color values when we want to display an image on an actual display.

\subsection{Rendering Equation}
\label{sec:renderingequation}
In Computer Graphics we are interested in rendering images realistically of a given scene. One common choice is to use the rendering equation when knowing the BRDF of the involved scene materials. This equation models the amount of emitted radiance from a point on a surface material along a particular viewing direction. Now, let us assume we are given an incoming light source directional at a solid angle $\omega_i$ and $\theta_i$ is its angle of incidence and that $\omega_r$ is the solid angle for the viewing direction. Further let $\lambda$ denote the wavelength$\footnote{Notice that, to keep our terms simple, we have dropped all $\lambda$ subscripts for spectral radiance quantities.}$ and $\Omega$ is the hemisphere of integration for the incoming light. Then, we are able to formulate a $BRDF_\lambda$ by using its definition $\ref{eq:defbrdf}$:

\begin{alignat}{4}
& f_r(\omega_i, \omega_r) &&= \frac{dL_r(\omega_r)}{L_i(\omega_i)cos(\theta_i)d\omega_i} \nonumber \\
\Rightarrow{} & f_r(\omega_i, \omega_r) L_i(\omega_i)cos(\theta_i)d\omega_i &&= dL_r(\omega_r) \nonumber \\
\Rightarrow{} & \int_{\Omega}f_r(\omega_i, \omega_r) L_i(\omega_i)cos(\theta_i)d\omega_i &&= \int_{\Omega}dL_r(\omega_r) \nonumber\\
L_r(\omega_r) &&= \Rightarrow{} & \int_{\Omega}f_r(\omega_i, \omega_r) L_i(\omega_i)cos(\theta_i)d\omega_i
\label{eq:initialbrdf}
\end{alignat}

The last equation is the so called rendering equation $\label{sec:dirlighsourceassumption}$. We assume that our incident light is a directional, unpolarized light source like sunlight and therefore its radiance is given as:

\begin{equation}
 L_{\lambda}(\omega)=I(\lambda)\delta(\omega-\omega_i)
\label{eq:radiancedirlightsource}
\end{equation}

where $I(\lambda)$ is the intensity of the relative spectral power for the wavelength $\lambda$. By plugging the identity in equation $\ref{eq:radiancedirlightsource}$ into our current rendering equation $\ref{eq:initialbrdf}$, we get:

\begin{align}
L_{\lambda}(w_r)
& = \int_{\Omega} BRDF_{\lambda}(\omega_i, \omega_r) L_{\lambda}(\omega_i) cos(\theta_i) d\omega_i \nonumber \\
& = BRDF_{\lambda}(\omega_i, \omega_r) I(\lambda) cos(\theta_i)
\label{eq:deribrdfwithdirsource}
\end{align}

where $L_{\lambda}(\omega_i)$ is the incident radiance and $L_{\lambda}(\omega_r)$ is the radiance reflected by the given surface. Note that the integral in equation $\ref{eq:deribrdfwithdirsource}$ vanishes since $\delta(\omega-\omega_i)$ is only equal one if and only if $\omega = \omega_i$.

\section{Wave Theory for Light and Diffraction}
\subsection{Basics in Wave Theory}
In order to prepare the reader for relevant concepts in physics which are used later for derivations and reasonings within this thesis, I am going to provide a quick introduction to the basics of wave theory and related concepts. In physics, a wave describes a disturbance that travels from one location to another through a certain medium. The disturbance temporarily displaces the particles in the medium from their rest position which results in an energy transport along the medium during wave propagation. Usually, when talking about waves we are actually referring to a complex valued function which is a solution to the so called \emph{wave equation} which is modelling how the wave disturbance proceeds in space during time. \\

There are two types of waves, (a) mechanical waves which deform their mediums during propagation like sound waves and (b) electromagnetic waves consisting of periodic oscillations of an electromagnetic field, such as light. As illustrated in figure $\ref{fig:wavebasics}$, there are several properties someone can use and apply in order to compare and distinguish different waves:

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.65]{background/waveschematicimpr.png}
  \caption[Sinewave]{Simplified, one dimensionally real valued wave function$\footnotemark$, giving an idea about some important wave properties. We denote the crest of a wave as the highest point relative to the equilibrium line (zero height along time axis) and similarly the trough as the lowest point.}
  \label{fig:wavebasics}
\end{figure}
\footnotetext{Image source: http://neutrino.ethz.ch/Vorlesung/FS2013/index.php/vorlesungsskript}

\begin{description}
  \item[Wavelength:] It is usually denoted by $\lambda$ and is a measure for the spatial distance from one point to another until the shape of a wave repeats
  \item[Amplitude:] It is denoted by $A$ and there are two possible interpretations: Firstly, it is a measure of the height from the equilibrium point to the highest point of a crest on the wave or the lowest point of a trough. This means that the amplitude can be positive or negative. However, usually, someone is just interested in the absolute value of an amplitude, i.e. the magnitude of a wave. For light waves it is a relative measure of intensity or brightness to other light waves of the same wavelength. And secondly, it can be interpreted as a measure how much energy a wave carries whereat the greater the absolute amplitude value, the bigger the amount of energy being carried.
  \item[Frequency:] Is a measure of the number of waves which are passing through a particular point in the propagation medium during one unit of time and is denoted by $f$.
  \item[Phase:] It is denoted by $\varphi$. It describes either the offset of initial position of a wave or the relative displacement between or among waves having the same frequency. Two waves with same frequency are said to be \emph{in phase} if they have the same phase. This means that they line up everywhere. On the other hand, two waves are said to be \emph{out of phase} if they have the same frequency but a different phases. As a remark, we denote by $\omega$ the angular frequency which is equal $2\pi f$.
\end{description}

A geometrical property of waves is their wavefront. This is either a surface or line along the path of wave propagation on which the disturbance at every point has the same phase. Basically, a wavefront can have any kind of shape although three prominent types of wavefronts are: spherical-, cylindrical- and plane wavefronts. If a point in an isotropic medium is sending out waves in three dimensions, then the corresponding wavefronts are spheres, centered on the source point. Hence spherical wavefront is the result of a spherical wave, also denoted as a wavelet. Note that for electromagnetic waves, the phase is a position of a point in time on a wavefront cycle (motion of wave over a whole wavelength) whereat a complete cycle is defined as being equal to $360\degree$.

\subsection{Wave Interference}
Next, after having seen that a wave is simply a traveling disturbance along a medium, having some special properties, someone could ask what happens when there are several waves traveling on the same medium. Especially, we are interested in how these waves will interact with each other. In physics the term interference denotes the interaction of waves when they encounter each other at a point along their propagation medium. At each point where two waves superpose, their total displacement is the sum of the displacements of each individual wave at that point. Then, the resulting wave is having a greater or lower amplitude than each separate wave and we can interpret the interference as the addition operates on waves. Two extreme scenarios are illustrated in figure $\ref{fig:interferenceconcept}$ for waves with same frequency and equal amplitude. There are basically three variants of interferences which can occur, depending on how crest and troughs of the waves are matched up:

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.65]{background/interferenceconcept.png}
  \caption[interference]{Interference scenarios$\footnotemark$ when two waves waves meet: On the left hand-side, there is constructive interference and on the right hand-side there is destructive interference illustrated.}
  \label{fig:interferenceconcept}
\end{figure}
\footnotetext{Image source: \texttt{http://en.wikipedia.org/wiki/Interference\textunderscore(wave\textunderscore propagation)} }

\begin{itemize}
  \item A crest of a wave meets a crest of another wave and similarly a trough meets a trough of another wave. This scenario is denoted as constructive interference and occurs at any location along the medium where the two interfering waves have a displacement in the same direction. This is like saying that the phase difference between the waves is a multiple of $2\pi$. Then the resulting amplitude at that point is being much larger than the amplitude of an individual wave. For two waves with an equal amplitude interfering constructively, the resulting amplitude is twice as large as the amplitude of an individual wave.
  \item A crest of a wave meets a trough of another wave and vice versa. This scenario is denoted as destructive interference and occurs at any location along the medium where the two interfering waves have a displacement in the opposite direction. This is like saying that the phase difference between the waves is an odd multiple of $\pi$. Then the waves completely cancel each other out at any point they superimpose.
  \item If the phase difference between two waves is intermediate between the first two scenarios, then the magnitude of the displacement lies between the minimal and maximal values which we could get from constructive interference.
\end{itemize}

Keep in mind that when two or more waves interfere which each other, the resulting wave may have a different frequency. This means that interfering waves, coming from two light sources with a certain color, may produce a light of another color than they have.

\subsection{Wave Coherence}
\label{sec:wavecoherence}
When considering waves which are traveling on a shared medium along the same direction, we could examine how their phase difference is changing over time. Formulating the change in their relative phase as a function of time will provide us a quantitative measure of the synchronization between those two waves, the so called wave coherence. In order to better understand this concept, let us consider a perfectly mathematical sine wave and a second wave which is a phase-shifted replica of the first one. A property of these mathematical waves is that they keep their shape over an infinity amount of time (i.e. propagated wavelengths). In our scenario, both waves are traveling along the same direction on the same medium, as illustrated in figure $\ref{fig:coherencesinsignal}$.
\begin{figure}[H]
  \centering
  \includegraphics[scale=0.32]{background/coherencesinsignal.png}
  \caption[Wave Coherence]{Two mathematical sine waves which are perfectly coherent which means that their phase difference is constant for every point in time.}
  \label{fig:coherencesinsignal}
\end{figure}
\noindent
Taking the difference between these two sine waves always yields a constant number. Therefore, those two waves are said to be coherent and hence perfectly synchronous over time. Notice that this scenario is completely artificial since in nature there are no mathematical sine waves. Rather, the phase difference is then a function of time $p(t)$. The more coherent two waves are, the slower this function will change over time.
In fact, two waves are said to be coherent if they are of the same frequency, are temporally in phase or have the same amplitude at every point in time. Thus two waves are coherent if they are generated at the same time, having the same frequency, amplitude, and phase. Conversely, waves are considered incoherent or also asynchronous if they have no stable phase difference. This means $p(t)$ is heavily varying over time. Coherence describes the effect of whether waves will tend to interfere with each other constructively or destructively at a certain point in time and space. Thus this is a property of waves that enables stationary interference. The more correlated two waves are, the higher their degree of coherence is. In physics coherence between waves is quantified by the cross-correlation function, which basically predicts the value of a second wave using the value of the first one. There are two basic coherence classifications:

\begin{itemize}
  \item Spatial coherence is dealing with the question of what is the range of distance between two points in space in the span of a wave for which there is occurring a significant effect of stationary interference when averaged over time. This is formally answered by considering the correlation between waves at different point in space. The range of distance with significant coherence is also denoted as the coherence area.
  \item Temporal coherence examines how well two waves which are observed at two different moments in time correlate with each other. Thus it may be used for predicting how well a wave interferes temporally with itself. Mathematically, this kind of coherence is computed by measuring the correlation between the value of the wave and the delayed version of itself. The coherence time denotes the maximum time delay for which the waves are coherent. The distance a wave has traveled during the coherence time is denoted as their temporal coherence length.
\end{itemize}

\subsection{Huygen's Principle}
\label{sec:huygensprincipledef}
Besides the phases and the amplitudes of waves, their propagation directly affects the interaction between different waves and how they could interfere with each other. This is why it makes sense to formulate a model which allows us to predict the position of a moving wavefront and how it moves in space. This is where $\emph{Huygen's Principle}$ comes into play. It states that any point of a wavefront may be regarded as a point source that emits spherical wavelets in every direction. Within the same propagation medium, these wavelets travel at the same speed as their source wavefront. The position of the new wavefront results by superimposing all of these emitted wavelets. Geometrically, the surface that is tangential to the secondary waves can be used in order to determine the future position of the wavefront. Therefore, the new wavefront encloses all emitted wavelets. Figure $\ref{fig:huygensprinciple}$ visualizes Huygen's principle for a wavefront reflected off from a plane surface.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.6]{background/huygensprinciple.png}
  \caption[Huygen's Principle]{A moving wavefront (blue) encounters an obstacle (a surface shown in brown colors) and produces a new wavefront (green) as a result of superposition of all secondary wavelets.}
  \label{fig:huygensprinciple}
\end{figure}

\subsection{Waves Diffraction}
Revisiting Huygen's principle we know that each point on a wavefront can be considered as a source of a spherical wavelet which propagates in every direction. But what exactly happens when a wave's propagation direction is only partially occluded by an object? What will be the outcome on applying Huygen's principle to this case? An example scenario for this case is shown in figure $\ref{fig:wavediffraction}$.

\begin{figure}[H]
  \centering
  \subfigure[transmissive grating]{
    \includegraphics[scale=0.45]{background/diffractiontransmissive2.png}
    \label{fig:wavediffractiontransm}
  }
~
  \subfigure[reflective grating]{
    \includegraphics[scale=0.6]{background/reflectivegrating.png}
    \label{fig:wavediffractionrefl}
  }

\caption[Diffracted Wave]{Illustration$\footnotemark$ of a diffraction scenario in which a plane wavefront passes through a surface with a certain width and how the wave will be bent, also showing the intensity of the resulting wave along a straight line in its path.}
\label{fig:wavediffraction}
\end{figure}
\footnotetext{Image source:\texttt{http://cronodon.com/images/Single\textunderscore slit\textunderscore diffraction\textunderscore 2b.jpg} }

Whenever a propagating wavefront is partially occluded by an obstacle, the wave is not only moving in the direction along its propagation, but is also bent around the edges of the obstacle. In physics, this phenomenon is called diffraction. Waves are diffracted due to interference which occurs among all wavelets when applying Huygen's Principle for the case when a wavefront hits an obstacle. Generally, the effect of diffraction is most pronounced for the waves whose wavelength is roughly similar in size to the dimension of the occluding object. Conversely, if the wavelength is much smaller in size, then there is almost no wave diffraction perceivable at a far off distance. This relationship between the strength of wave diffraction and the wavelength is conceptually illustrated in figure $\ref{fig:diffractionrelationshipdimension}$ when a wave is transmitted through an opening in a surface. A reflective example for diffraction provided in figure $\ref{fig:huygensprinciple}$.

\begin{figure}[H]
  \centering
  \subfigure[W $\ll$ $\lambda$]{
  \includegraphics[scale=1.0]{background/Aa2l.png}
    \label{fig:a1}
  }
~
  \subfigure[W $\approx$ $2 \lambda$]{
    \includegraphics[scale=1.0]{background/Aastl.png}
    \label{fig:a2}
  }
~
  \subfigure[W $\approx$ $6 \lambda$]{
    \includegraphics[scale=1.0]{background/Aa6l.png}
    \label{fig:a3}
  }
  \caption[Diffraction for different $\texttt{Wavelength/Slit-Width}$ ratio]{Illustration$\footnotemark$ of how diffraction changes when a wave with wavelength $\lambda$ propagates through a slit of width equal $W$.}
  \label{fig:diffractionrelationshipdimension}
\end{figure}
\footnotetext{Image taken from:\texttt{http://neutrino.ethz.ch/Vorlesung/FS2013/index.php/vorlesungsskript}, chapter 9, figure 9.14 }

In everyday's life, we can see the direct outcome of the effect of wave diffraction in form of structural colors. There are examples from nature such as the iridescent colors on various snake skins as well as artificial examples such as the colorful patterns notable when having a close look at an illuminated compact disc. All these examples comprise a surface made of highly regular nanostructures which diffract an incident light significantly. Such a nanostructure which exhibits a certain degree of regularity is also denoted as a diffraction grating. Further information about diffraction gratings can be found in section $\ref{sec:diffractiongrating}$.

\section{Stam's BRDF formulation}
\label{sec:sumstam}
The theoretical foundation of this thesis is based on the pioneering work of J.Stam$\cite{diffstam}$. In this model Stam derived a BRDF formulation to model the effect of far field diffraction for various analytical anisotropic surfaces. His model is relying on the so called scalar wave theory of diffraction for which a wave$\footnote{In general, a wave is a complex valued vector satisfying Maxwell's equations. A scalar values wave only satisfies the Helmoltz equation. For further information please visit \texttt{http://en.wikipedia.org/wiki/Maxwells\textunderscore equations} and \texttt{http://en.wikipedia.org/wiki/Helmholtz\textunderscore equation}.}$ is assumed to be a complex valued scalar. Thus, Stam's BRDF formulation does not take into account the polarization of the incident light. Fortunately, light sources like sunlight and light bulbs are unpolarized. The principal behind J. Stam's approach is illustrated in figure $\ref{fig:meaningofstamsapproach}$.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.6]{background/stamsapproachsmeaning2.png}
  \caption[Idea behind Stam's approach]{Illustration of secondary wavelets reflected off a surface. An integration over all secondary sources resulting from an incident wave according to Huygen's principle will give us an identity for the total contribution at a certain point in space.}
  \label{fig:meaningofstamsapproach}
\end{figure}

An incident wave $p_i$ from a a light source encounters a surface representing a diffraction grating. According to Huygen's Principle, at any point $i$ on the grating at which the incident wave meets the grating a secondary, spherical wavelet $p_{r,i}$ will be emitted. A viewer, indicated by a gray circle in the figure, will perceive the superimposed contribution of all wavelets along the surface $S$ (in the figure indicated by a integration symbol), which will directly follow the laws of wave interference. Therefore the resulting color which an observer sees is the final radiance at that point which reflects from stationary interference of all emitted secondary wavelets and per due to Huygen's principle. \\

A further assumption in Stam's Paper is, that the emanated waves from the source are stationary, which implies the wave is a superposition of independent monochromatic waves. This further implies that each wave is associated with a definite wavelength $\lambda$. Directional light sources such as sunlight fulfills this fact and since we are using these kinds of light sources for our simulations, Stam's model can be used for our modelling purposes. \\

The main idea of his model is to formulate a BRDF as a function of the Fourier transformation applied on a certain correlation function relating to the given height field. His model assumes homogeneity of the height field structure and depicts an approximation of far-field diffraction effects. The geometrical setup of his model is illustrated in figure $\ref{fig:geometricsetup}$. The classes of surfaces his model is able to support either exhibit a very regular structure or may be considered as a superposition of bumps forming a periodic like structure. Therefore, the surfaces he is dealing with can either be modelled by probabilistic distributions or have a direct analytical representation. Both cases allow him to derive an analytical solution for his BRDF model.

\begin{figure}[H]
  \centering
  \includegraphics[scale=0.8]{background/stamsinputp.png}
  \caption[Stam's geometrical setup]{Illustration$\footnotemark$ of the geometrical setup of Stam's approach where $\omega_i$ is a direction, pointing towards the light source, $\omega_r$ points towards the viewer, $n$ is the surface normal and$ (u,v,w)$ are the components of the vector $-\omega_i - \omega_r$.}
  \label{fig:geometricsetup}
\end{figure}
\footnotetext{Modified image which originally has been taken from D.S. Dhillon et. al. poster$\cite{diffourp}$.}

Figure $\ref{fig:geometricsetup}$ illustrated schematically the geometrical setup used for Stam's BRDF formulation. An incident light with a direction $\omega_i$ hits the surface of a given height field at the position $p$. The direction vector $\omega_r$ points towards the viewer. After the incident light has hit the surface, a spherical wavelet is reflected off from this hit position. The direction vector of the wavelet can be computed by taking the difference between the incident and viewing direction as shown in equation $\ref{eq:uvw}$:

\begin{equation}
  (u,v,w) = -\omega_i - \omega_r
\label{eq:uvw}
\end{equation}

These coordinates will later be used in order to compute the total contribution of all secondary sources used in Stam's BRDF in equation $\ref{eq:mainstam}$. In Stam's Derivation, the phase difference between the incident and emitted wave from the given height field is denoted by the auxiliary function $\Phi$ defined as in equation $\ref{eq:heightfieldphase}$.

\begin{equation}
  \Phi(x,y) = \frac{2 \pi}{\lambda} w h(x,y)
\label{eq:heightfieldphase}
\end{equation}

Then, any secondary wavelet $p$ which is emitted off from the given surface is equal to:
\begin{equation}
  p(x,y) = e^{i\Phi(x,y)}
\label{eq:px}
\end{equation}

using the idea presented for figure $\ref{fig:meaningofstamsapproach}$ and performing some further mathematical steps shown as described in Stams paper, will lead us to the final BRDF representation. This BRDF models the total contribution of all secondary sources reflected off the the provided surface $h$ in the direction $\omega_r$:

\begin{equation}
  BRDF_{\lambda}(\omega_i, \omega_r) = \frac{k^2 F^2 G}{4\pi^2 A w^2} \langle \left|P(ku, kv)\right|^2\rangle
\label{eq:mainstam}
\end{equation}

where $F$ denotes the Fresnel coefficient and $G$ is the so called geometry term$\footnote{The geometric terms expresses the correction factor to perform an integration over an area instead over a surface. For further information, please have a look at \texttt{http://en.wikipedia.org/wiki/Surface\textunderscore integral}, and read the definition about \emph{surface element}}$ which is equal to:
\begin{equation}
  G =\frac{(1 + \omega_i \cdot \omega_r)^2}{cos(\theta_i)cos(\theta_r)}
\label{eq:geometricterm}
\end{equation}

\myparagraph{Fourier Transformation Sign Convention}
\label{sec:electricalengeneeringftconvention}
\noindent
One last word about the Fourier transform terms that Stam uses in his derivation. Conventionally, following the definitions of the Fourier transformation, we are dealing with is commonly denoted as the inverse Fourier Transformation. However, especially in electrical engineering (EE), it is common to define the inverse Fourier transformation by the Fourier Transformation and vice versa. To be more precisely there are two definitions of the Fourier transformation. One commonly used by physicist where the exponential function is w.r.t. $-i$ and the one which is used in EE, performing an integration over an exponential is w.r.t. $i$. Further information about the sign convention in Fourier transformations can be looked up in the book Quantum Mechanics for Electrical Engineers$\cite{signconvention}$. Note that by substituting the minus sign of the physicist definition of the Fourier transformation gives us the definition used in EE shown in in equation $\ref{eq:signchangementconvention}$:

\begin{align}
\mathcal{F}_{FT}\{f\}(w)
& = \int_{\mathds{R}^n} f(x)e^{-iwt} dt \nonumber\\
& = \int_{\mathds{R}^n} f(x)e^{i\hat{w}t} dt \nonumber\\
& = \mathcal{F}^{-1}_{FT}\{f\}(\hat{w})
\label{eq:signchangementconvention}
\end{align}
where $\hat{w}$ is equal $-w$. \\

The height fields we are dealing with in this work are, however, natural gratings containing a complex shaped nano-structure and hence far from being very regularly aligned. The reason why Stam's approach in its current form is not suitable for our purpose is twofold: First his approach does not capture the complexity of natural gratings accurately well enough when relying on his statistical approaches and secondly it is way too slow in order to be usable for interactive rendering since his BRDF needs an evaluation of a Fourier Transform for every directional changing. \\

In the next chapter we are going to adapt Stam's BRDF model such that it will be able to handle the kind of surfaces we are dealing with and even will have a runtime complexity which permits interactive rendering.


\documentclass[11pt]{article}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{booktabs}

\begin{document}
\title{MAT1830 --- Assignment 10}
\author{Dylan Pinn --- 24160547}
\maketitle

\section*{Question 1}
Rewrite the following expressions without using $\Sigma$ or $\Pi$.

\begin{enumerate}[label= (\alph*)]
  \item $\sum_{i=2}^{6} \frac{2}{6i - 7}$

  \begin{align*}
    &= \frac{2}{6 \times 2 - 7} + \frac{2}{6 \times 3 - 7} + \frac{2}{6 \times
    4 - 7} \\
    &+ \frac{2}{6 \times 5 - 7} + \frac{2}{6 \times 6 - 7} + \frac{2}{6 \times
    7 - 7} \\
    &= \frac{2}{5} + \frac{2}{11} + \frac{2}{17} + \frac{2}{23} + \frac{2}{29} +
    \frac{2}{35}
  \end{align*}

  \item $\prod_{i=5}^{8} ({(z + 2i)}^{i} - i - 1)$

    \begin{align*}
      &= ({(z + 2 \times 5)}^{5} - 5 - 1) \times ({(z + 2 \times 6)}^{6} - 6 -
      1) \\
      &\times ({(z + 2 \times 7)}^{7} - 7 - 1) \times ({(z + 2 \times 8)}^{8} - 8
      - 1) \\
      &\times ({(z + 2 \times 9)}^{9} - 9 - 1) \times ({(z + 2 \times 10)}^{10}
      - 10 - 1) \\
      &\times ({(z + 2 \times 11)}^{11} - 11 - 1) \times ({(z + 2 \times
      12)}^{12} - 12 - 1) \\
      &= ({(z + 10)}^{5} - 6) \times ({(z + 12)}^{6} - 7) \times ({(z + 14)}^{7}
      - 8) \\
      &\times ({(z + 16)}^{8} - 9) \times ({(z + 18)}^{9} - 10) \times ({(z +
      20)}^{10} - 11) \\
      &\times ({(z + 22)}^{11} - 12) \times ({(z + 24)}^{12} - 13) \\
    \end{align*}

\end{enumerate}

\section*{Question 2}
Rewrite the following expressions using $\Sigma$ or $\Pi$ notation.

\begin{enumerate}[label= (\alph*)]
  \item $x(x+1)(x+4)(x+9)(x+16) \dots (x+400)$

    \[ \prod_{i=0}^{21} (x + i^2) \]

  \item $\frac{1}{6^4} + \frac{1}{9^5} + \frac{1}{12^6} + \frac{1}{15^7} + \dots
    + \frac{1}{33^{13}}$

    \[ \sum_{i=4}^{10} \frac{1}{{(3i - 6)}^{i}} \]

\end{enumerate}

\section*{Question 3}
Call a string of letters ``legal'' if it can be produced by concatenating
(running together) copies of the strings `a', `bb' and `cc'. For example,
`abba' is legal because it can be produced by concatenating `a', `bb' and `a',
but `ccca' is not legal.

For each integer $n \geq 1$, let $t_n$ be the number of legal strings with $n$
letters. For example, $t_1 = 1$ (`$a$' is the only legal string) and $t_2 = 3$
(`aa', `bb' and `cc' are the legal strings).

\begin{enumerate}[label= (\alph*)]
  \item Write down $t_3$ and a list of all the legal strings of length 3.

    \begin{itemize}
      \item $aaa$
      \item $abb$
      \item $acc$
      \item $bba$
      \item $cca$
    \end{itemize}

    \[ t_3 = 5 \]

  \item Write down $t_4$ and a list of all the legal strings of length 4.

    \begin{itemize}
      \item $aaaa$
      \item $abba$
      \item $aabb$
      \item $aacc$
      \item $acca$
      \item $bbaa$
      \item $bbbb$
      \item $bbcc$
      \item $ccaa$
      \item $cccc$
      \item $ccbb$
    \end{itemize}

    \[ t_4 = 11 \]

  \item Find a recurrence for $t_n$ that holds for all $n \geq 3$. Explain why
    your recurrence gives $t_n$.

\end{enumerate}

\section*{Question 4}
Draw simple graphs with the following properties or explain why they do not
exist.

\begin{enumerate}[label= (\alph*)]
  \item The list of vertices is: $P$, $Q$, $R$, $S$, $T$ and the list of edges
    is $PQ$, $PS$, $QR$, $RS$, $RT$.

  SEE ATTACHED

  \item The graph has 10 vertices and 47 edges.

  Does not exist. Maximum number of edges possible for a simple graph of $n$
    verticies is $\binom{n}{2}$.

  \[ \binom{10}{2} = 45 \]

  So a simple graph of 10 vertices and 47 edges is not possible.

  \item The graph has 7 vertices and 6 edges and is connected.

    Does not exist. To traverse between 7 verticies you need at least 7 edges.

  \item The graph has 7 vertices and 11 edges and its vertices can be divided
  into two sets in such a way that no edge joins two vertices in the same set.

\end{enumerate}
\end{document}


\chapter{Implementation}
\label{chp:implementation}

% %
% HEADER
% %
The smart elasticity is the ability of a system to dynamically auto-scale resources according to scaling policies determined with machine learning techniques.
%
The smart elasticity service hence provides a resource manager, e.g. a container orchestrator, with the ability to learn the best replication degree for its resources, e.g. deployed containers, with respect to the current cluster state.
%
In this work we propose to implement smart elasticity for Kubernetes leveraging reinforcement learning, that is the most famous technique of unsupervised machine learning.
%
In particular, we focus on $\mathcal{Q}$-Learning, which is the most widely studied reinforcement learning algorithm.
%
In this Chapter we show the adopted reinforcement learning model and how the smart elasticity service has been implemented in the Kubernetes environment.

% %
% Q-LEARNING MODEL
% %
\section{$\mathcal{Q}$-Learning model}
\label{sec:implementation-q-learning-model}

A reinforcement learning algorithm can be used to learn the best scaling policy to adopt in response to cluster state changes without any past knowledge.
%
In this work, we propose an approach that makes use of the $\mathcal{Q}$-Learning algorithm, described in Chapter \ref{chp:reinforcement-learning}.
%
A general $\mathcal{Q}$-Learning algorithm relies on a reinforcement learning model that requires the definition of the parameters:

\begin{itemize}

	\item \textbf{State Space} $\mathcal{X}$ where each state $x\in\mathcal{X}$ is a cluster state.

	\item \textbf{Action Space} $\mathcal{A}$ where each action $a\in\mathcal{A}$ is a scaling action.

	\item \textbf{Quality Function} $\mathcal{Q}:\mathcal{X}\times\mathcal{A}\rightarrow\Re$ where the value $q_{x_{n},a_{n}}=\mathcal{Q}(x_{n},a_{n})$ is the quality of the choice of performing the action $a_{n}$ in the state $x_{n}$ at epoch $n$.
	%
	Notice that, since $\mathcal{Q}$-Learning is an iterative algorithm that updates $\mathcal{Q}$, this function requires an initialization setting $\mathcal{Q}_{0}$.

	\item \textbf{Rewarding Function} $\mathcal{R}:\mathcal{X}\rightarrow\Re$ where the value $r_{n}=\mathcal{R}(x_{n})$ is the reward achieved for the state $x_{n}$ at epoch $n$.

	\item \textbf{Learning Rate} $\alpha\in (0,1]$ that trades off the importance of sooner versus later quality values. The closer $\alpha$ is to $1$ the more important are future quality evaluations with respect to past ones.

	\item \textbf{Discount Factor} $\gamma\in [0,1]$ that trades off the importance of sooner versus later rewards. The closer $\gamma$ is to $1$ the more important are future rewards with respect to past ones.

\end{itemize}

These parameters encapsulate the peculiarities of the domain where the algorithm is applied to and are used by the reinforcement learning agent to determine the optimality of a specific action in a given state, according to the following update:

\begin{equation}
\label{eqn:implementation-q-learning-update}
\mathcal{Q}_{n}(x,a) =
\begin{cases}
(1-\alpha_{n})\mathcal{Q}_{n-1}(x,a) + \alpha_{n}(r_{n} + \gamma\max_{b} \mathcal{Q}_{n-1}(x_{n+1},b)) & \text{if } x=x_{n},a=a_{n} \\
\mathcal{Q}_{n-1}(x,a)       & \text{otherwise}           \\
\end{cases}
\end{equation}

In the following, we show and motivate our choice for the aforementioned parameters in the context of smart elasticity.

\subsection{State Space}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-state-space}
INSERT HERE DESCRIPTION OF THE STATE SPACE (ITERATE WITH PROF).

\subsection{Action Space}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-action-space}
INSERT HERE THE DESCRIPTION AND MOTIVATIONS OF THE CHOSEN ACTION SPACE (ITERATE WITH PROF).

\subsection{Quality Function}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-quality-function}
INSERT HERE THE DESCRIPTION AND MOTIVATIONS OF THE CHOSEN QUALITY FUNCTION (ITERATE WITH PROF).

\subsection{Rewarding Function}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-rewarding-function}
INSERT HERE THE DESCRIPTION AND MOTIVATIONS OF THE CHOSEN REWARDING FUNCTION (ITERATE WITH PROF).

\subsection{Learning Rate}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-learning-rate}
INSERT HERE THE DESCRIPTION AND MOTIVATIONS OF THE CHOSEN LEARNING RATE (ITERATE WITH PROF).

\subsection{Discount Factor}
\label{sec:smart-elasticity-elasticity-leveraging-q-learning-discount-factor}
INSERT HERE THE DESCRIPTION AND MOTIVATIONS OF THE CHOSEN DISCOUNT FACTOR (ITERATE WITH PROF).

% %
% ARCHITECTURE
% %
\section{Architecture}
\label{sec:implementation-architecture}

The smart elasticity service is realized by the Smart Scaler Engine, that is a Kubernetes custom controller implemented as a UAS following the micro-services design pattern.
%
In Figure \ref{fig:implementation-architecture} we show the high-level architecture for the smart elasticity service, represented as a UML component diagram.

\begin{itemize}

	\item \texttt{Heapster}: collects cluster performance metrics focused on CPU, Memory and Network utilization in a per-Node and per-Pod basis.
	%
	Metrics can be queried via the \texttt{K8S REST} interface, which proxies the \texttt{Metric Server}, that is responsible for gathering metrics from \texttt{Heapster Master}.

	\item \texttt{Kubernetes}: responsible for containers orchestration.
	%
	In particular, it deployes containers in a per-Pod fashion and dynamically scales them in/out accordingly with the scaling actions submitted by the \texttt{Smart Scale Engine}.
	%
	It exposes its functionalities with a REST API served by \texttt{Kubernetes Master}.
	%
	The most important functionalities are
	(i) listing of Smart Scalers,
	(ii) listing of Deployments,
	(iii) watching for Smart Scalers changes,
	(iv) watching for Deployments changes,
	(v) horizontal scaling of Deployments
	(vi) querying of cluster metrics.

	\item \texttt{Smart Scale Engine}: realizes the smart elasticity service. It follows a micro-services architecture, where services are:

	\begin{itemize}

		\item \texttt{Agents Manager}: responsible for the allocation/deallocation of \texttt{RL Agents} accordingly to \texttt{Smart Scalers} instantiated within \texttt{Kubernetes}.

		\item \texttt{RL Agent}: this is a reinforcement learning agent, thus it executes the $\mathcal{Q}$-learning loop to determine the scaling actions. Each agent is uniquely associated to a Deployment. In particular it determines the best scaling action with respect to the current cluster state accordingly to the adopted reinforcement learning technique. It submits scaling action to \texttt{Kubernetes} via its REST interface. When this component is asked to produce a new scaling action, the \texttt{StateManager} collects metrics from the \texttt{ClusterMonitor} and aggregates them into a new \texttt{ClusterState}, representing the current cluster state. The latter are then used by the \texttt{RLAgent} to execute the reinforcement learning algorithm to produce the new \texttt{ScalingAction}. The \texttt{RLAgent} reads/writes all data required by the RL algorithm from/to the \texttt{RL Repo}.

		\item \texttt{RL Repo}: a data store responsible for storing data required by \texttt{RLAgent} for the execution of the reinforcement learning algorithm, e.g. parameters and reinforcement learning matrices.

		\item \texttt{API Gateway}: a REST API server exposing to the outside the services provided by the overall \texttt{Smart Scaler Engine} infrastructure.

	\end{itemize}

\end{itemize}

From a technological point of view, the \texttt{ScalerAI} has been implemented as a Spring-based REST service, that is a de-facto standard solution for Java enterprise web applications; while the \texttt{RLRepository} has been implemented as a MongoDB datastore, which is a de-facto standard technology for NoSQL document-based datastore.

Let us now focus on the main interactions flows that realizes smart elasticity.
%
First, we have the loop executed by \texttt{Agents Manager}, that is made of the following steps:
%
\begin{enumerate}
	\item
\end{enumerate}
%
In Figure \ref{fig:implementation-agents-manager-loop-sequence-diagram} we show the flow of interactions during the loop executed by \texttt{Agents Manager}, represented as a UML sequence diagram.

Then, we have the loop executed by each \texttt{RL Agent}, that is made of the following steps:
%
\begin{enumerate}

	\item the \texttt{Orchestrator} asks \texttt{ScalerAI} to compute a new scaling action. The request is submitted to the \texttt{ScalerAIController}, which is the \texttt{ScalerAI} entry-point.

	\item the \texttt{ScalerAIController} retrieves the current cluster state from the \texttt{StateManager}. The custer state encapsulates the cluster performance metrics of interest, gathered from the \texttt{ClusterMonitor}.

	\item the \texttt{ScalerAIController} asks the \texttt{RLAgent} to compute a new scaling action with respect to the current cluster state.

	\item the \texttt{RLAgent} (i) computes the current state $s\in\mathcal{S}$ and, concurrently, (ii) retrieves reinforcement learning data from the \texttt{RLDataManager}, which collects it from the \texttt{RLRepository}.

	\item the \texttt{RLAgent} uses he gathered state and data to compute sequentially (i) the rewarding function, (ii) the quality function and (iii) the action.

	\item the \texttt{RLAgent} concurrently (i) saves new function values to the \texttt{RLRepository} via the \texttt{RLRepositoryManager} and (ii) encapsulates the computed action into a new \texttt{ScalingAction} returned to the \texttt{ScalerAIController}.

	\item the \texttt{ScalerAIController} returns the new scaling action to the \texttt{Orchestrator}.

	\item the \texttt{Orchestrator} apply the scaling action.

\end{enumerate}
%
In Figure \ref{fig:implementation-rl-agent-loop-sequence-diagram} we show the flow of interactions during the loop executed by each \texttt{RL Agent}, represented as a UML sequence diagram.

\clearpage
\vfill
\begin{landscape}
	\begin{figure}
		\label{fig:implementation-architecture}
		\centering
		\includegraphics[height=.6\columnwidth, width=.95\columnwidth]{design/implementation-architecture}
		\caption{The architecture of Smart Scale Engine.}
	\end{figure}
\end{landscape}
\vfill
\clearpage
\vfill
\begin{landscape}
	\begin{figure}
		\label{fig:implementation-agents-manager-loop-sequence-diagram}
		\centering
		\includegraphics[height=.6\columnwidth, width=.95\columnwidth]{design/implementation-agents-manager-loop-sequence-diagram}
		\caption{The sequence diagram representing the loop executed by the \texttt{AgentsManager}.}
	\end{figure}
\end{landscape}
\vfill
\clearpage
\vfill
\begin{landscape}
	\begin{figure}
		\label{fig:implementation-rl-agent-loop-sequence-diagram}
		\centering
		\includegraphics[height=.6\columnwidth, width=.95\columnwidth]{design/implementation-rl-agent-loop-sequence-diagram}
		\caption{The sequence diagram representing the loop executed by a single \texttt{RLAgent}.}
	\end{figure}
\end{landscape}
\vfill
\clearpage

% %
% SMART SCALER
% %
\section{Smart Scaler}
\label{sec:implementation-smart-scaler}
%
A Smart Scaler is a Kubernetes Custom Resource that is associated to an existing Deployment to provide it with smart elasticity.
%
As any other Kubernetes object, a Smart Scaler instance points out a desired system state.
%
In particular, a Smart Scaler instance points out that a given Deployment should be provided with smart elasticity.
%
Smart Scalers are in a $0...1:1$ relation with Deployments, that is each existing Deployment may have zero or one Smart Scaler associated to it.
%
If a Deployment has no Smart Scaler, the smart elasticity service is not provided to it.
%
If a Deployment has a Smart Scaler, the smart elasticity service is provided to it.
%
In particular, since the learning process involved in each Smart Scaler is relative to a any given Deployment, each Deployment should have at most one unique Smart Scaler.

If the Administrator wants to provide an existing Deployment with smart elasticity, it is enough to create a Smart Scaler \textit{MyAppSmartScaler} associated to it.
%
This is the same way the built-in auto-scaling mechanism works, as described in Section \ref{sec:kubernetes-elasticity}.

As Smart Scalers are not built-in objects, a Custom Resource Definition (CRD) should be submitted to Kubernetes to define them as custom objects.
%
%Figure \ref{fig:implementation-smart-scaler-crd} shows the CRD used to define Smart Scalers.
%
The CRD it is pretty self-explanatory.
%
It states that a Smart Scaler is an object provided by the API \textit{gmarciani.com/v1} scoped in namespaces following the common plural, singular and short naming convention.
%
Each Smart Scaler has the following attribute:
%
\begin{itemize}

	\item \textbf{deployment:} the name of an existing Deployment to provide with smart elasticity.
	%
	This is a required field.

	\item \textbf{algorithm:} the name of the reinforcement learning algorithm to power smart elasticity.
	%
	This is a required field with default value \textit{qlearning}, meaning that $\mathcal{Q}$-learning is the default reinforcement learning algorithm.

	\item \textbf{parameters:} a collection of parameters specific to each algorithm. For example, $\mathcal{Q}$-learning requires to specify parameters $rewardFunction$, $\alpha$ and $\gamma$ to customize its behavior.

	\item \textbf{replicationMin:} the minimum number of replicas.
	%
	This is an optional field with default value $1$, meaning that at least one replica should be present.

	\item \textbf{replicationMax:} the maximum number of replicas.
	%
	This is an optional field, meaning that the replication degree is unbounded.

\end{itemize}
%
Once CRD is submitted, Smart Scalers can be managed as native objects.

For example, to provide an existing Deployment named \textit{MyApp} with a Smart Scaler it is enough to run the command
%
\begin{verbatim}
kubectl create -f MyAppSmartScaler.yaml
\end{verbatim}
%
where \textit{MyAppSmartScaler.yaml} is an instance of a Smart Scaler defined following the schema specified in the Smart Scaler CRD.
%
%\begin{figure}
%	\label{fig:implementation-smart-scaler-crd}
%	\lstinputlisting{code/implementation-smart-scaler-crd.yaml}
%	\caption{Custom Resource Definition for the smart scaler object.}
%\end{figure}

For example, Figure \ref{fig:implementation-smart-scaler-cr} shows a Smart Scaler that realizes smart elasticity for the Deployment named \textit{MyApp} using the Q-learning algorithm with reward function \textit{rfunc1}, alpha equals to 0.7, gamma equals to 0.8 and a replication degree between $1$ and $100$.
%
\begin{figure}
	\label{fig:implementation-smart-scaler-cr}
	\lstinputlisting{code/implementation-smart-scaler-cr.yaml}
	\caption{A Smart Scaler instance.}
\end{figure}

% %
% REST INTERFACES
% %
\subsection{REST Interfaces}
\label{sec:smart-elasticity-implementation-rest-interfaces}

In this section we detail the REST API exposed by the API Gateway provided by Smart Scaler Engine to manage the learning processes.
%
Furthermore we detail the REST calls executed by Smart Scaler Engine against the Kubernetes Web Server to gather deployment information, performance metrics and to submit scaling actions.

\begin{table}
	\label{tbl:implementation-rest-smart-scaler-engine}
	\centering
	\begin{tabular}{| m{1.5cm} | m{5cm} | m{7.5cm} | }\hline

		\textbf{Met.} & \textbf{Resource} & \textbf{Description} \\\hline

		\texttt{GET}	& /status         & Retrieves the status of the overall Smart Scale Engine service \\\hline

	\end{tabular}
	\caption{The REST interface exposed by Smart Scaler Engine's Web Server. Each resource path shown here is relative to the server root.}
\end{table}

\begin{table}
	\label{tbl:implementation-rest-kubernetes}
	\centering
	\begin{tabular}{| m{1.5cm} | m{5cm} | m{7.5cm} | }\hline

		\textbf{Met.} & \textbf{Resource} & \textbf{Description} \\\hline

		\multicolumn{3}{| c |}{/api/gmarciani.com/v1/namespaces/\{namespace\}/} \\\hline

		\texttt{GET}  & /smart-scalers & List all deployed smart scalers \\\hline

		\texttt{GET}  & /smart-scalers/\{scaler\} & Describe the specified smart scaler \\\hline

		\texttt{PATCH} & /smart-scalers/\{scaler\} & Update the specified smart scaler \\\hline

		\texttt{DELETE}  & /smart-scalers/\{scaler\} & Delete the specified smart scaler \\\hline

		\multicolumn{3}{| c |}{/apis/metrics/v1alpha1/} \\\hline

		\textbf{Met.} & \textbf{Resource} & \textbf{Description} \\\hline

		\texttt{GET}  & /nodes            & List all nodes metrics \\\hline

		\texttt{GET}  & /nodes/\{node\}   & List metrics for the specified node \\\hline

		\multicolumn{3}{| c |}{/apis/metrics/v1alpha1/namespaces/\{namespace\}} \\\hline

		\texttt{GET}  & /pods & List metrics for all pods in the specified namespace \\\hline

		\texttt{GET}  & /pods/\{pod\} & List metrics for the specified pod \\\hline

	\end{tabular}
	\caption{The REST interface of interest exposed by Kubernetes's Web Server to manage Smart Scalers and gather metrics.}
\end{table}


\chapter{Similarity Distance Algorithms}
\label{ch:3}

In this chapter, we present seven different similarity distance measures.
First, we describe four that are parameter-free, then we describe three algorithms that require a parameter.
By chance, all of the parametric methods are based on Edit Distance, so we briefly explain how its construction as well.

The methods have largely been chosen due to their prominence in the existing literature\cite{58-UCRTime,70-SimilarityDistances,8-EffectivenessStudy,53-TimeSeries,52-OnlineEfficient}.
However, we present two newer algorithms, one parameter-free and one that requires a parameter.
They are presented at the end of their respective groupings.

As was the case in the previous chapter, large portions here are based on the report created for \textit{TDT4501 Computer Science, Specialization Project}

\section{Parameter-Free Measures}
% The advantage of these parameter free methods is, as their names tell us, that they do not have parameters and they are “out of the box” ready.

\subsection{Euclidean Distance (Ed)}
As noted in \Cref{ch:2}, the Euclidean distance (Ed) is a way to quantify the distance between two points in space.
There is a naive extension of that principle that lays the foundation for a Euclidean Distance measure for trajectories\cite{44-FastSubsequence, 8-EffectivenessStudy, 24-ReviewTrajectory,86-ComputingMinimum}. This method would be to compute the mean point-point distances in a lock-step manner as seen in \Cref{eq:ed}.

\begin{equation}\label{eq:ed}
Ed(A, B) = \frac{1}{n}\cdot \sum_{i=1}^{n}d(a_i, b_i)
\end{equation}
where the distance between corresponding points is the $L_2$-norm as defined in \Cref{eq:l2norm}

Its simplicity comes with a couple of disadvantages, one notable one is that it requires the trajectories to be of equal length.
In the cases where the number of points does not match one would have to adapt the data, potentially altering the original observations.
Furthermore, computing distance in a lock-step manner means that it is sensitive to local time shifts and noise\cite{12-RobustFast}.

Even with these limitations, this measure is included for its simplicity.  Previous work has shown that it holds remarkably well up to more advanced methods\cite{26-QueryingMining}, further encouraging us to examine this metric.

\subsection{Dynamic Time Warping (DTW)}

As stated in \Cref{sec:elasticity} an elastic measure is needed in order to handle local time shifts and Dynamic Time Warping (DTW) is precisely that.
DTW was originally designed for speech recognition which means that it handles relative lag seamlessly\cite{45-CrosswordsReference, 27-DynamicTime}.
This makes it a prevalent choice for examining similarity under temporal drift.
The idea behind this algorithm is to stretch and contract time such that a favorable alignment of the input trajectories can be found.

\Cref{fig:dtw_cost_path} illustrates the implementation of DTW.
The computation begins by taking input trajectories $A$ and $B$ and constructing a full cost matrix $C$. This matrix keeps all pairwise distances  between the trajectories' elements.
We use the $L_2$ norm for point-to-point distance computation as is common for DTW\cite{5-ComputingVisualizing}.
After the matrix has been constructed, the next step is to iteratively search through  $C$ and find the optimal warping path, $p$.
The optimal warping path is the set of point-point pairings, entries of $C$, that  minimizes the accumulated cost.
The accumulated cost along the warping path is the DTW distance between the trajectories.
\Cref{fig:ed_dtw_diff} exemplifies how the Euclidean distance and DTW differ with respect to time-shift.

% how this approach stands out from
% for an illustration of how the points are aliened under a lock-step measure like the Euclidean Distance and DTW.

\begin{figure}[h]
\centering
\includegraphics[width=.7\textwidth]{figs/algos/dtw_C_p.png}
\caption{Cost matrix $C$ (left) and accumulated cost matrix $C'$ which the optimal warping path $p$ (right) . Figure copied from \cite{27-DynamicTime}}
\label{fig:dtw_cost_path}
\end{figure}

\begin{figure}
\centering
\includegraphics[width=.7\textwidth]{figs/algos/ed_dtw_diff.png}
\caption{An illustration of how the Euclidean forces a lock-step computation whereas DTW realigns the trajectories. Figure copied from \cite{58-UCRTime}}
\label{fig:ed_dtw_diff}
\end{figure}

The main drawback of DTW is that it weights all points of both trajectories equally and thus it is not robust to noise.
A naive implementation of DTW would forcefully align the start- and endpoints of the two trajectories which can disproportionately punish trajectories that are overall similar\cite{4-ElasticPartial}.
Generating the cost matrix quickly becomes expensive due to pairwise comparing all points of the input trajectories which makes it less suited for large data sets \cite{27-DynamicTime}.

Its shortcomings have inspired iterations of DTW that seek to address them. Some remark that constructing a full cost matrix may not be needed, and some seek to speed up how to iterate through $C$\cite{5-ComputingVisualizing}. We note that a popular choice is \textbf{FastDTW}, which is a parameterized version of DTW.
As the name indicates, it is much faster than the naive implementation yet gives comparably accurate results\cite{9-FastDTWAccurate}.
Nevertheless, this thesis shall only focus on the parameter-free version.

\subsection{Hausdorff Distance (Hd)}
% This distance measure is a metric that weights shape similarity, meaning the temporal aspect or order of trajectory elements does not matter
This technique is often discussed in relation to polygons and sets\cite{49-HausdorffDistance} however it is applicable for time-series data as well.
A consequence of its geometric origin is that the trajectory direction no longer affects the final result, the notion of a first or last trajectory element is not kept\cite{86-ComputingMinimum}.
The Hausdorff distance from  $A$ to $B$ is the maximum of the minimum of all pairwise distances, see \Cref{eq:Hd-directed}\cite{48-ComputationalGeometrya}

\begin{equation}
\label{eq:Hd-directed}
\widetilde{Hd}(A, B) = max_{a\in A}\big\{ \, min_{b\in B } \enspace d(a, b)\,\big\}
\end{equation}

where $d(a, b)$ is any metric distance function, here the $L_2$-norm is used.
The distance function described in \Cref{eq:Hd-directed} is not symmetric and thereby not a metric\cite{14-EfficientAlgorithm}. The function is referred to as the directed Hausdorff distance.
To make the Hausdorff metric, it is defined as the maximum  of the two directed results.
This makes the function symmetric, so that it fulfills the requirements for a metric, see \Cref{eq:Hd-metric}.

\begin{equation}
\label{eq:Hd-metric}
Hd(A, B) = max\big\{ \,\widetilde{Hd}(A, B), \;\widetilde{Hd}(B, A)\big\}
\end{equation}

Since we can compare any point in A to any point in B this measure is elastic.
All points of the trajectories are weighted equally which makes it is sensitive to noisy data.
Additionally, the effect of reducing the similarity score to the distance between two points from each trajectory is that information about the trajectories' overall shape is lost.
The metric is highly sensitive to noise\cite{51-HausdorffDistance}.
Lastly, we need to work out both of the directed distances to get the similarly score. This means more work per trajectory pair than naturally symmetric measures such as Ed and DTW.

\subsection{Symmetrized Segment-Path Distance (SSPD)}
As the  Hausdorff distance, Symmetrized Segment-Path Distance (SSPD) method is a spatial only algorithm that largely disregards the direction of the trajectories.
It is was developed from the same principles as the Hausdorff distance, but with the addition of being able to account for  the trajectories as a whole.\cite{43-TrajectoryDistance, 50-ReviewPerspective}.
The algorithms explained so far have used the trajectory elements directly as the basis for the computations. For SSPD, this is no longer the case, and thus we define notation and terms to describe this algorithm.

 In line with  \Cref{tb:notation}  $a, b$ are elements of Trajectories $A, B$ respectively .
 A \textit{segment} is the line between two consecutive points, and we use $\breve{a}$ to denote a segment of $A$. Using indexes,  $\breve{a}_i$ is the line between $a_i$ and $a_{i+1}$.

Next we define the \textit{point-to-segment distance} as shown in \Cref{eq:sspd-ps}. This distance function requires us to find the point’s orthogonal projection onto the segment.
If it is within the segment, the distance will be the  $L_2$-norm between the original point $a$ and its projection $\dot{a}$.
Otherwise, the  $L_2$- norm to segment's end points is computed nearest those distances becomes the point-to-segment distance.

\begin{equation}
\label{eq:sspd-ps}
d_{ps}(a_i, \breve{b}_j) =  \begin{cases}
d(a, \dot{a})  &\text{if} \enspace \dot{a} \in \breve{b}_j\\
min\{ d(a_i, b_j), d(a_i, b_{j+1})\}  &\text{otherwise}
\end{cases}
\end{equation}
Where $\dot{a}$ is the orthogonal projection of point  $a$ onto segment  $\breve{b_j} $ and $d$ is the $L_2$-norm.

From point-to-segment distance, \textit{point-to-trajectory distance} is defined as shown in \Cref{eq:sspd-pt}.
The point-to-segment distance is computed for all the segments of the trajectory and the lowest one becomes the point-to-trajectory distance.

\begin{equation}
\label{eq:sspd-pt}
D_{pT}(a_i, B) = min_{\breve{b} \in B} \big\{ \, d_{ps}(a_i, \breve{b})\, \big\}
\end{equation}
The \textit{Segment-Path-Distance} (SPD) is directed.
It is defined to be the mean of the all point-to-trajectory distances from points of the first trajectory to the second trajectory, \Cref{eq:sspd-spd}.

\begin{equation}
\label{eq:sspd-spd}
SPD(A, B) = \frac{1}{n_A} \sum_{i=1}^{n_A} D_{pT} (a_i, B)
\end{equation}
As with the Hd, this distance measure is made symmetric by computing both directed versions first. However unlike Hd, the final result is the mean of the directed results. The name comes from this last step and the final formula is shown in \Cref{eq:sspd-main}.

 \begin{equation}
\label{eq:sspd-main}
SSPD(A, B) = \frac{\big( \, SPD(A, B) + SPD(B, A)\, \big)}{2}
\end{equation}

The creators of SSPD note that if one were to use the max function rather than computing the mean upon symmetrification this algorithm would be identical to the Hausdorff function\cite{50-ReviewPerspective}.
They note that with their method, this distance function becomes more robust to noise when calculating the mean, as opposed to maximizing or minimizing.
Moreover, they commend SSPD for being parameter-free as well as not relying on interpolation between two observed point, preferring to strengthen the importance of observed data.
As a closing remark, we note that SSPD does not fulfill the requirements of a metric.

\section{Parameterized Measures}

 In this section, we have described measures that require a parameter.
 All of them are based on String Edit Distance (SED) which is a similarity measure designed for strings.
 The idea is to count the number of \textit{edits} needed to convert one string into the other using the edit operations are insert, delete and replace.
 Due to the strings' natural discretization, it is trivial to determine whether or not two symbols are matching.
There are variations of implementation but a common choice is to let the cost of an \textit{edit} be 1, creating a formula as seen in \Cref{eq:ed_string}:

\begin{equation}\label{eq:ed_string}
    \text{SED}(S1, S2) = \begin{dcases}
        \quad |S1| & \text{if }  |S2| = 0\\
     	\quad |S2| & \text{if }  |S1| = 0\\
        \quad \text{SED}(S1,\; S2) & \text{if }  |S1| =|S2| \\
        \begin{aligned}
        1 + min\big\{   & \text{SED}(Rest(S1), \; Rest(S2)) \\
              	     & \text{SED}(Rest(S1), \; S2) \\
           		    & \text{SED}(S1, \; Rest(S2)) \\
        \end{aligned} & \text{otherwise}
  \end{dcases}
\end{equation}

Real data does not let itself be discretized as fortuitously as strings, thereby leading to the creation of the algorithms below.
The measures differ in how they have adapted SED for time series data, and the role of the parameter value changes as well.

% \subsection*{FastDTW}
% As its name indicates, this algorithm is a modification of DTW. We remarked that DTW is expensive in both time and space complexity. FastDTW archives linear complexity on both fronts, but does introduce a parameter to do so \cite{9}.

% The idea behind this is to first run the algorithm at coarse resolution, compute a projected warp-path and then rerun the algorithm at a finer resolution to tune the path. This is repeated until we have reached the original resolution. Two of the most important ways this algorithm is sped up are the following.
% Firstly, fewer cells of the cost matrix need to be calculated or evaluated. Secondly, data abstraction means that the cost of DTW itself is greatly reduced. This means a great improvement in cost, however at the expense of no longer being parameter free. The parameter in question is a radius parameter which determines how much the projected optimal path can deviate from the one found at the previous iteration. See FIGURE for an illustration of how the iterative search plays out with the radius parameter. WILL NOT INCLUDE

% One way we could speed up DTW is by introducing a \textit{radius parameter}, and this algorithm is called FastDTW\cite{9}. This approach first runs the algorithm at a coarser resolution, and then reruns itself in order to tune the result. This means a great improvement in cost, however at the expense of no longer being parameter free. Nevertheless we have chosen to include both DTW and FastDTW as part of our survey to examine the impact of this alteration.

\subsection{Edit Distance on Real Sequence (EDR)}

Edit Distance on Real Sequence (EDR)\cite{12-RobustFast} introduces a threshold parameter $\epsilon$ that determines if two trajectory elements are “matching”.
Crucially, there can be no partial matches so the point-to-point distance is either 1 or a 0 as shown in \Cref{eq:edr_subcost}.

\begin{equation}\label{eq:edr_subcost}
d_{edr} (a, b) = \begin{cases}
0 \quad &\text{if } \quad | a_{LNG} -b_{LNG}|\leqslant  \epsilon  \text{ and } | a_{LAT} -b_{LAT}|\leqslant  \epsilon    \\
1 \quad &\text{otherwise}\\
\end{cases}
\end{equation}
where $\epsilon$ is the threshold parameter. After determining whether or not two points match with the subcost function we get the full EDR is shown  algorithm as seen in \Cref{eq:edr_main}

\begin{equation}\label{eq:edr_main}
    EDR(A, B) = \begin{cases}
        n_A \qquad &\text{if } n_B = 0\\
        n_B \qquad &\text{if } n_A = 0\\
        \begin{aligned}
        min\big\{ & EDR(\text{ Rest}(A), \text{ Rest}(B)) + d_{edr}(a, b),\\
              & EDR(\text{ Rest}(A), B) + 1,\\
              & EDR(A, \text{ Rest}(B)) + 1 \big\}
        \end{aligned} & \text{otherwise}
  \end{cases}
\end{equation}where $a, b$ are the leading elements of $A, B$.

The main advantages of EDR are its resistance to noise and the ability to handle local time shifts.
It is resistant to noise because of how it maps the distance between elements to a binary 1 or 0.
Toohey and Duckham asserted that EDR performed well in spite of variance in the sampling rate\cite{17-TrajectorySimilarity}.

EDR is not a metric measure as it does not fulfill triangle inequality, but it meets other requirements of metrics.
Furthermore, it evaluates similarity as trajectory elements, not taking into account the trajectories' overall shape.
We have chosen to include this algorithm as it is a well-studied algorithm.
Its prevalence in the literature has led it to be studied alongside DTW and Euclidean Distance.

\subsection{Edit Distance with Real Penalty (ERP) }
Edit Distance with Real Penalty (ERP) was designed to bridge the gap between metrics distance functions and local time shifting tolerant ones. \cite{13-MarriageLpnorms}. The design of EDR began with the $L_1$-norm \Cref{eq:manhatten}

\begin{equation}\label{eq:manhatten}
Dist_{L1} (A, B) = \sum_{i=1}^{n} d(a_i, b_i) \qquad \text{where} \qquad d_{L_1}(a_i, b_i) = \sum_{j=1}^{p}|a_{i,j}- b_{i,j}|
\end{equation}

From SED, the notion of a \textit{gap element} is introduced.
A gap element is a symbol that could have been deleted from string $S1$ but instead is inserted into string $S2$.
The point-to-point distance function would then become what is shown in \Cref{eq:erp_ed}.

\begin{equation}\label{eq:erp_ed}
    d_{ed}(a_i, b_i) = \begin{cases}
        0 &\qquad \text{if } a_i = b_i \\
        1 &\qquad \text{if } a_i \text{ or } b_i \text{ is a gap } \\
        1 &\qquad \text{otherwise } \\
    \end{cases}
\end{equation}

% SED, just as the $L_p$ norms, is a metric distance measure, but to further develop this algorithm it needs to accommodate real values.
Rather than using a constant value to penalize all edit operations uniformly like EDR, ERP differentiates between gap elements and non-gap elements.
This distinction is important so that it will be tolerant to local time sifting.
Gap elements are penalized with a constant value, while non-gap elements have a real-value cost based on their value.
The parameter of ERP,  $g$, is the reference value for cost computation, and the point-to-point distance function is seen in \Cref{eq:erp_sub}

\begin{equation}\label{eq:erp_sub}
     d_{epr}(a_i, b_i) = \begin{cases}
        |a_i-b_i| & \qquad \text{if  neither is a gap} \\
        |a_i-g|   & \qquad \text{if } b_i \text{ is a gap} \\
        |b_i-g|   & \qquad \text{if } a_i \text{ is a gap} \\
     \end{cases}
 \end{equation}

 Again, the trajectory distance function is an adaption of SED, and the full algorithm for ERP is shown in \Cref{eq:erp_main}
%  but was no longer a metric after it.
\begin{equation}\label{eq:erp_main}
    ERP(A, B) = \begin{dcases}
        \quad \sum_{i=1}^{n_A}|a_i - g| & \text{if }  n_B = 0\\
        \quad \sum_{i=1}^{n_B}|b_i - g| & \text{if }  n_A = 0\\
        \begin{aligned}
        min\big\{   & {ERP}(Rest(A), \; Rest(B)) + d_{erp}(a_1,\;  b_1),\\
              	    & {ERP}(Rest(A), \; B) + d_{erp}(a_1, \; g),\\
           		    & {ERP}(A, \; Rest(B)) + d_{erp}( b_1,\; g) \big\}
        \end{aligned} & \text{otherwise}
  \end{dcases}
\end{equation}

The main drawback of this method stems from the characteristic that makes it a metric, using differences of real values as costs.
This method is a metric as long as $g$ is constant, making the cost of an edit vary the location of the trajectory element.
However, this makes ERP sensitive to noise\cite{12-RobustFast}.

\clearpage
\subsection{Move-Split-Merge (MSM)}
Move-Split-Merge (MSM) \cite{40-MoveSplitMergeMetric} is similar to the aforementioned to the other SED-based approaches in that it calculated the similarity score from how many operations are needed to transform one trajectory into another one.
This algorithm is tolerant to temporal misalignments and translation invariant\cite{53-TimeSeries} as are EDR and ERP.
What distinguishes this algorithm from them is how insertions and deletions are handled.
The MSM cost model uses both the value of the element being modified as well the adjacent one whereas ERP only uses the element being modified and EDR uses a constant cost for all operations.

As the name of this algorithm indicates, the possible operations are Move, Split, and Merge, which are then used to emulate the established Edit Distance operations Insert, Delete, and Substitute.
The Move operation is a renaming of does the same as Substitute.
The Split operation inserts a copy of a given value directly after itself, and the Merge operation deletes a value if it is immediately followed by a matching value\cite{53-TimeSeries}. In other words, Split and Merge are each others’ inverse.
The Insert operation becomes a Split followed by a Move, while Delete becomes a Move followed by a Merge.
The MSM parameter, $c$, is  a non-negative value that determines the cost of every Split and Merge operation. See \Crefrange{eq:msm-start}{eq:msm-end} for details.

\begin{align}
 A  &= [a_1, \cdots , a_{i-1},\, a_i,\, a_{i+1}, \cdots  , a_{n_A}] \nonumber \\
 \nonumber\\
\text{Move}_{a_i \rightarrow b_j}(A)  &= [a_1, \cdots , a_{i-1},\, b_j,\, a_{i+1}, \cdots  , a_{n_A}] \label{eq:msm-start} \\
\text{Cost} \big\{ \text{Move}_{a_i \rightarrow b_j}(A) \big\} &= d(a_i, b_j) \\
\nonumber \\
\text{Split}_{a_i}(A) &= [a_1, \cdots , a_{i-1},\,a_i,\, a_i,\, a_{i+1}, \cdots  , a_{n_A}]\\
\text{Cost} \big\{ \text{Split}_{a_i}(A) \big\} &= c \\
\nonumber \\
\text{Merge}_{a_i}(A) &= [a_1, \cdots , a_{i-1},\, a_{i+1}, \cdots , a_{n_A}] \\
\text{Cost} \big\{ \text{Merge}_{a_i}(A) \big\} &= c \label{eq:msm-end}
\end{align}

Recall that the $\text{Merge}_{a_i}(A)$ operation is only permitted if $a_i = a_{i+1} $.
This algorithm fulfills the requirements of a metric and thus it can be used for indexing and clustering techniques that are designed to function in metric spaces.
In terms of computational cost and accuracy, we refer to work done by Bagnall et. al. which alludes to MSM being comparably efficient to ERP and more tolerant to noise DTW\cite{56-GreatTime}.

 \clearpage
\section{Summary of Algorithms}

\Cref{tab:meas_comp} summarizes some trajectory similarity features.
Remark that none of the measures have matching rows, further indicating that the meaning of trajectory similarly varies with technique implementation.

We reiterate that large portions of this chapter stem from the report that was mentioned at the beginning.
Still, there is not a complete overlap of the measures discussed in that report and this thesis.

% Euclidean distance is the only measure that does not account for local time shifts, and
% From all aspects apart from "overall shape" similarity, there has been about half and half if they measures fulfilled the requirements.

% distance functions satisfy the requirements of metricity

% https://www.tablesgenerator.com/latex_tables

\begin{table}[t]
\centering
\caption{Quick view of some aspects the similarity distance measures}
\label{tab:meas_comp}
\resizebox{\textwidth}{!}{%
\renewcommand{\arraystretch}{1.5}
\begin{tabular}[h]{*{6}{|p{1.8cm}}|}
 \hline
\textbf{Name}   & \textbf{Metric } & \textbf{Single \newline Element } & \textbf{Time Shifts} & \textbf{Parameter} & \textbf{Noise \newline Tolerant}      \\
\hline
\textbf{ED} 		 & Yes    & Yes     & No  & No    & No      \\
\hline
\textbf{DTW}      & No:    &Yes    &Yes    & No   & No    \\
\hline
\textbf{Hd}       & Yes    & Yes        &Yes     & No     & No      \\
\hline
\textbf{SSPD}      & No    & No          &Yes   & No   & Yes  \\
\hline
\textbf{EDR}       & No   & Yes    & Yes       & Yes     & Yes   \\
\hline
\textbf{ERP}       & Yes   & Yes    & Yes      & Yes    & No    \\
\hline
\textbf{MSM}      & Yes   & No    & Yes   & Yes   & Yes \\
\hline
\end{tabular}
}
\end{table}

% double check the noise tolerances, use checkmarks?