In this work, we present a data-driven IRL-based social navigation pipeline.\\
An overview of the pipeline:
\\ \textbf{An image showing the block diagram of the pipeline including the environment, the feature extractor and the other components}
We will describe each component of the pipeline in greater detail.
\section*{The IRL block:}
Inverse reinforcement learning(IRL) or inverse optimal control(IOC) has been in vogue in recent years when it comes to training robots to perform real-world tasks. This is understandable as assigning rewards to individual states to illicit out desired behaviors is challenging. IRL provides a better alternative of obtaining the underlying reward function as well as a trained agent on the obtained reward function using demonstrations from the expert.

To keep the chapter self-contained, we will briefly go over the definition of a Markov decision process, which is at the base of reinforcement and inverse reinforcement learning.
A Markov decision process or MDP can be defined as a tuple ($\mathcal{S}$,$\mathcal{A}$,T,$\gamma$, $\mathcal{R}$)  where,
\begin{itemize}
	\item $\mathcal{S}$ is the set of all possible states.
	\item $\mathcal{A}$ is the set of all possible actions.
	\item T is the state transition dynamics, i.e. the probability of moving to a state given its previous state and action, $P(s^{'}|s,a)$ .
	\item $\gamma$ is the discounting factor.
	\item $\mathcal{R}$ is the set of rewards $R:  \mathcal{S} \mapsto \mathbb{R} $ is the reward function. In practice, instead of using the raw states, hand engineered features are extracted from the states with are then used to calculate the reward of a particular state. This alleviates a lot of complexity when dealing with large continuous state-spaces.
	\end{itemize}
The goal of the IRL is to infer a reward function that best explains the behavior of the expert. The expert behavior is represented in terms of expert demonstrations or trajectories, $D = \{ \tau_1, \tau_2, \tau_3, \dots,
\tau_{M} \}$ in the context of navigation. Each of these trajectories, in turn, can be further broken down into a collection of states $\tau_{i} = \{ s_{0}, s_{1}, s_{2}, \dots, s_{T} \}$ as visited by the expert in the trajectory.

We base our work on Wulfmeier's paper, which in turn is a neural network adaptation of the work done by Ziebart 2008.
According to the max entropy formulation, the probability of the occurrence of a trajectory is directly proportional to the reward it receives.
\begin{align}
Entropy equation - Ziebart equation 2
\end{align}
and is equal to:
\begin{align}
equation with z
\end{align}
Given a set of expert demonstrations, an optimal reward structure should maximize the probability of the occurrence of the expert demonstrations and their associated states. Mathematically, this is given by
\begin{align}
the equation for the loglikelihood-Ziebart equation 6
\end{align}
The original paper by Ziebart used a linear combination of weights as reward functions. But linear representations have limited capabilities when it comes to expressing complex reward functions. This problem is addressed by Wulfmeier 2015 where they restructure the maxent IRL formulation using neural networks. Neural networks are universal function approximators, and this vastly improves on the amount of complexity the reward functions can encapsulate.

\begin{align}
derive the gradient expression in equation 3 of iros paper
\end{align}

The original formulation of MEDIRL was in the context of a model-based setting and the state transition matrix was used to calculate the agent SVF. While this produces an exact value of the agent SVF, assuming the availability of the state transition matrix is fairly optimistic for most real-world tasks including navigation. In an attempt to make things less constrained we take the model-free approach and focus on calculating the SVF using sampling-based methods.
The SVF calculation:
The main challenge going model free is the calculation of the Z value, which previously could be calculated using dynamic programming [citation of the paper].
Under the assumption of a model-free but deterministic environment, the SVF of a deterministic policy can be reasonably computed by taking trajectory samples of the policy from the context of all the existing pedestrians in the scene.
\begin{align}
equation 4 from iros2020
\end{align}
where the $\mathcal{P}$ represents state transitions obtained from sampling and not the state transition dynamics. \textbf{We argue that this assumption is reasonable in a navigation setting because the task is not inherently uncertain, and most transition dynamic uncertainty can be attributed to sensory noise and control error. We summarize our approach in algorithm 1. (Taken word-to-word from iros manuscript)}
\begin{algorithm}
	algorithm 1 from iros2020
\end{algorithm}

For solving the MDP, we employ actor-critic methods, which we will describe in detail in the next section.

%\subsection*{Overview of the algorithm used}
%The algorithm trains for two networks, the reward network that, given the features of a state returns the reward associated with it,\\
%\textbf{equation}\\ stating this.
%and the policy network, which given the same, returns the best possible action.\\
%\textbf{equation}\\
%The method starts with randomly initializing the weights of the reward network. This reward network is then used in the  RL block to train an agent which is optimal for the current reward structure. Once, an optimal policy is obtained, the policy is then sampled from, in the environment to obtain roll outs or trajectories in this case. A trajectory is given by the sequence of states visited by the agent {s1, s2, ... sn}.
%Once the trajectories are obtained, they are used to calculate the state visitation frequency. The difference between the expert and the agent SVF is used to calculate the loss
%\textbf{equation}
%This loss is then back propagated through the reward network to update the weights.
%Once the weights are updated, the new network is again fed into the RL block. This iterative process continues until completion.
%Explanation of the L1 regularization over l2 regularization

\section*{The RL block:}

Actor-critic methods are a class of reinforcement learning algorithms that are built upon policy gradient methods.
In policy-gradient methods, the goal is to iteratively improve the performance of a given policy which is achieved by maximizing the expected return of the policy. Mathematically, the objective of a policy gradient method can be expressed as:
\begin{align}
maximize \;\; J( \theta )  &\; = \; \mathbb{E} [ R | \pi_{\theta} ] \\
					   & \; = \; \mathbb{E}[ \sum^{T-1}_{t=0} r_{t+1}| \pi_{\theta}]
\end{align}
where, $r_{t}$ is the reward obtained at time $t$ and $\pi_{\theta}$ is the policy with parameters $\theta$.\\
This leads to an update function:
\begin{align}
\delta J ( \theta ) =
\end{align}
Vanilla policy gradient methods suffer from high biases because they do not have a normalizing factor for the second term. This is addressed by introducing a
baseline.
\begin{align}
Equation with the baseline to the vanilla policy gradient.
\end{align}
The baseline can be calculated in various ways. One of the resulting algorithms is the A2C or the Advantage actor-critic algorithm, which we use to solve our MDP.
The A2C method:
Two symbiotic agents at play here. The actor and the critic.
The critic estimates the value function of a given state.
The actor uses this information to update its policy distribution.
\begin{algorithm}
	The actor-critic algorithm.
\end{algorithm}

\section*{The feature extractor}
The feature extractor is a vital component in the navigation pipeline. It acts as a medium that enables the learning algorithm to interact with the environment. The performance of a learning agent is highly contingent on the design of the feature extractor. \cite{vasquez_et_al}\\

The feature vector generated by the feature extractor can be broken down into 2 broad components: local and global components.
\begin{itemize}
	\item The \textbf{local component}, as the name suggests, comprises of the information from nearby surroundings of the agent captured in the form of a binary feature vector. This provides an approximate idea of the obstacles in the vicinity.
	\item The \textbf{global component}, on the other hand, provides a rough direction of where the goal is with respect to the agent. This provides the agent with a purpose of navigation, preventing it from just rambling around the map.
\end{itemize}
 The environment, by design, publishes a detailed description of obstacles in the map, including their location, orientation, and velocity, and the goal. Having access to such a detailed picture of the surroundings on a mobile robot navigating any given scene in the real world is highly unlikely and difficult to obtain. This is additionally addressed by the feature extractor, which also acts as an information moderator, receiving raw information from the environment and packaging it in a feature vector that can be readily constructed by a mobile robot on the go using off-the-shelf sensors.\\
 Both the local and the global components along with their subcomponents are described in greater detail below.
Talk about the relative orientation calculation

\subsection*{The global component}
The global information is further comprised of 3 elements:

\subsubsection*{Relative goal orientation}
This acts as a compass, providing a rough estimate of the location of the goal based on the current position and orientation of the agent. This is denoted by a $9 \times 1$ one-hot vector, where the presence of the goal in any one of the bins is marked by a $1$ keeping the rest to $0$. The $360 \degree$ around the agent are divided into $8$ equal divisions forming the first 8 bins of $45 \degree$ each. The $9^{th}$ bin denotes the contact of the agent with the goal. The structuring of the bins is shown below.
\begin{figure}[!htbp]
	Figure showing the relative goal orientation
\end{figure}

\subsubsection*{Change in orientation}
Represented by a $5 \times 1$ one-hot vector, the change in orientation captures the magnitude of the change in the orientation of the agent in consecutive steps. The value ranges between $0 \degree$ -  $ 180 \degree$. This value is binned in one of 5 asymmetric bins. The rationale behind the uneven distribution is that empirically we have observed that human motion is smooth. So, the bins are constructed in a way to have a finer resolution in the lower range. This helps capture the nuances in the human motion in greater detail leading to better encapsulation of the essence of the navigational pattern. The division of the range is shown below.

\begin{table}[htbp]
	\caption{Bin thresholds orientation change features.}
	\label{orientation-change-bins}
	\begin{center}
		\renewcommand{\arraystretch}{1.3}
		\begin{tabular}{|c|c|}
			\hline
			Feature & Threshold \\
			\hline
			$\phi_{O1}$ & $\alpha_{OC} \in \left[ 0 , \frac{\pi}{9} \right)$ \\

			$\phi_{O2}$ & $\alpha_{OC} \in \left[ \frac{\pi}{9} , \frac{2\pi}{9} \right)$ \\

			$\phi_{O3}$ & $\alpha_{OC} \in \left[ \frac{2\pi}{9} , \frac{3\pi}{9} \right)$ \\

			$\phi_{O5}$ & $\alpha_{OC} \in \left[ \frac{3\pi}{9} , \frac{4\pi}{9} \right)$ \\

			$\phi_{O6}$ & $\alpha_{OC} \in \left[ \frac{4\pi}{9} , \pi \right)$ \\
			\hline
		\end{tabular}
	\end{center}
\end{table}
\subsection*{Deviation from the goal}
Represented by a $4 \times 1$ one-hot vector, the deviation from goal captures the magnitude of the angle between the vector to the goal from the current position of the agent and the current orientation vector of the agent. The value ranges from $0 \degree$ - $ 180 \degree$ which is again asymmetrically divided into 4 bins, with greater emphasis is laid on the lower degrees due to reasons mentioned earlier.

\begin{table}[htbp]
	\caption{Bin thresholds for deviation from the goal.}
	\label{deviation-from-goal-bins}
	\begin{center}
		\renewcommand{\arraystretch}{1.3}
		\begin{tabular}{|c|c|}
			\hline
			Feature & Threshold \\
			\hline
			$\phi_{GA1}$ & $\alpha_{GA} \in \left[ 0 , \frac{\pi}{8} \right)$ \\

			$\phi_{GA2}$ & $\alpha_{GA} \in \left[ \frac{\pi}{8} , \frac{\pi}{4} \right)$ \\

			$\phi_{GA3}$ & $\alpha_{GA} \in \left[ \frac{\pi}{4} , \frac{3\pi}{4} \right)$ \\

			$\phi_{GA4}$ & $\alpha_{GA} \in \left[ \frac{3\pi}{4} , \pi \right]$ \\
			\hline
		\end{tabular}
	\end{center}
\end{table}

\subsubsection*{Speed}
The entire range of all possible speed values are divided into 6 divisions and the current speed of the agent is represented by a $ 6 \times 1$ one-hot feature vector. The entire range of possible speeds are divided into 6 divisions with each division corresponding to an element of the one-hot vector.

%\begin{table}[htbp]
%	\caption{Thresholds for the qantization of the speed of the agent.}
%	\label{deviation-from-goal-bins}
%	\begin{center}
%		\renewcommand{\arraystretch}{1.3}
%		\begin{tabular}{|c|c|}
%			\hline
%			Raw speed & Speed bin \\
%			\hline
%			0 - 0.2 & 0 \\
%			0.2 - 0.4 & 1 \\
%			0.4 - 0.6 & 2 \\
%			\hline
%		\end{tabular}
%	\end{center}
%	\end{table}

\subsection*{The local information}
Taking inspiration from previous works in this field \cite{fahad-et-al} \cite{vasquez-et-al}, we use spatial bins to effectively break the region surrounding the agent into discrete segments and calculate a 'risk' metric for each of these bins.

\subsubsection*{Creation of the bins}
There are 16 spatial bins surrounding the agent. They are arranged in two concentric circles around the agent. The region between the agent and the inner circle is then broken into 8 equal divisions. These form bins from 1 - 8. Similarly, the region between the first and the second bin is again divided into 8 equal divisions which form bins 9 - 16.
\subsubsection*{Calculation of the risk}
'Risk' is a term we label the 'threat' the agent faces at a given time from any of these bins. Here 'threat' can loosely be seen as a measure of the possibility of hitting an obstacle. The higher the threat, the greater the chance that if the agent and the obstacles continue on their current course it will end in a collision.
The 'risk factor' is divided in 3 levels: high, low and something in between.
\begin{itemize}
	\item High risk:
When the relative motion of an obstacle is towards the agent and can lead to a collision if not intervened.
	\item Low risk:
When the relative motion of an obstacle is away from the agent.
	\item Med risk:
Anything in between
\end{itemize}
The calculation of the risk values are based on the following entities:
\begin{align}
	\vec{o}_{rel} = & \;\; \vec{o}_{obs} - \vec{o}_{agent}  \\
	\vec{d}_{rel} =  &\;\; \vec{d}_{agent} - \vec{d}_{obs} \\
	\theta_{risk} =  & \;\; \angle (\vec{o}_{rel}, \vec{o}_{rel}) \\
	\mathbf{s}_{obs} = & \;\; \tan(\theta_{risk}) \times |\vec{d_{rel}}| \\
	\mathbf{T} = & \;\; \text{agent witdh} + \text{obstacle witdh}
\end{align}
where $\vec{o}_{rel}$ is the relative orientation of the obstacle w.r.t the agent, $\vec{d}_{rel}$ is the relative position of the agent w.r.t to the obstacle, $\theta_{risk}$ is the angle between the vectors, $\vec{o}_{rel}$ and $\vec{d}_{rel}$,  $\mathbf{s}_{obs}$ is the estimated \textbf{safety margin} between the agent and the obstacle and $\mathbf{T}$ is a predefined value which if maintained between the agent and an obstacle should guarantee a collision-free trajectory.\\
An obstacle is marked as 'high risk' when $\theta$ is less than $90\degree$ and the safety margin is less than $\mathbf{T}$. If $\theta$ < $90 \degree$, this indicates that the obstacle is moving away from the agent and hence chances of collision are less and hence low risk. Anything that does not fall in the above two categories are considered as medium risk. The risk calculation conditions and values are summarized in Table \ref{risk-categorization-table}

\begin{table}[htbp]
	\caption{Categorization of the risk.}
	\label{risk-categorization-table}
	\begin{center}
		\renewcommand{\arraystretch}{1.3}
		\begin{tabular}{|c|c|}
			\hline
			Risk value & Risk condition \\
			\hline
			High & $\theta < 90\degree$ \&  $\mathbf{s}_{obs}$ < $\mathbf{T}$   \\

			Low & $\theta > 90\degree$\\

			Medium & otherwise \\
			\hline
		\end{tabular}
	\end{center}
\end{table}
The risk value of each bin is represented using a $3 \times 1$ one-hot vector. One thing to note is the risk is calculated for individual obstacles present in the bin separately. And it is not uncommon to have more than one obstacle falling in different risk divisions from the same spatial bin. In that case, the risk value assigned to that bin is the highest risk posed among all the obstacles that fall under that spatial bin.
\begin{figure}[!htbp]
	\text{Graphics that shows each of the risk conditions}
\end{figure}

\section*{The SVF calculation}
One of the main challenges of IRL is calculating the expected state distribution or the state visitation frequency (svf) of a given agent. (equation x). In model-based environments, this can be calculated using the state transition matrix and dynamic programming \cite{wulfmeier-deepirl}. Having access to the state dynamics of the environment might be difficult and not always readily available, especially in real-world applications like navigation. Instead, we relax this assumption for a model-free but deterministic environment. \textbf{Explanation as to why this is a reasonable assumption} This is a reasonable assumption in the context of a navigation problem because the movements of pedestrians in general are inherently deterministic. %and any observed uncertainty by the agent can be attributed to the error in measurement by the onboard sensors.\\

Due to the lack of state dynamics we use sampling techniques to get a \textbf{good} estimate of the svf. While this can be time consuming and computationally expensive, the problem is drastically simplified when using greedy policies. (greedy soft actor critic).\textbf{Why?}
Svf calculation for an agent following a greedy policy in a deterministic environment can be calculated by taking a single sample trajectory for each pedestrian by replacing them with the agent and letting it run till completion. The calculation of the expected state visitation frequency of the agent is shown in equation \ref{agent-approx}
\begin{align}
\label{agent-approx}
\text{Equation to calculate the state visitation frequency}
\end{align}
Additionally, we also introduce a \textbf{smoothing technique} for the calculated svf. \\
\textbf{What is smoothing?}\\
In the traditional way of calculating the SVF, the observation of a given state contributes to the increment of the visitation frequency of that state by 1.
Instead for a single observation, we opt for the increment in the visitation frequency of a set of neighboring states based on the closeness of the neighboring states to the state observed. Here smoothing is defined as the distribution of the visitation weight of a state over its set of neighboring states based on their spatial similarity.\\
\textbf{Why smoothing?}
Having a 1 to 1 mapping between the observation and the increment of the SVF misses out on the fact that not all states \textit{are equally different.} \textbf{differ from each other in equal magnitude} \\
\textbf{For example:} consider 3 different states, which differ only in their goal location component. Now, if two of the states indicated the goal to be in the $2^{nd}$ spatial bin and $3^{rd}$ spatial bin, then the difference between these two states are smaller than a $3^{rd}$ state where the goal is in the $6^{th}$ bin.
This inequality in the differences among different states are accounted for in the smoothing, by increasing the weighted increment of a set of neighboring states of the observed state (based on their similarity) rather than increasing the value of the observed state only. \\
\textbf{How smoothing?}
The state vector comprises of different components, and the 'smoothed' state is obtained by convolving a smoothing kernel to each of them separately. The values used in the kernel, and the type of convolution applied depends on the nature of the spatial division the feature represents and is summarised in the Table \ref{conv-table}.

\begin{table}
	\caption{Table showing the details of the convolution used for smoothing the state feature vector.}
	\label{conv-table}
	\begin{center}
		\renewcommand{\arraystretch}{1.3}
		\begin{tabular}{|c|c|c|}
			\hline
			Feature component & Convolution Kernel & Convolution type\\
			\hline
			Relative goal orientation & $ [0.1, \;0.8, \; 0.1 ]$ & Wrap  \\

			Change in orientation & $[ 0.1, \; 0.8; \;0.1 ]$ & Same \\

			Deviation from goal & $[0.9,\; 0.1]$, $[0.1,\; 0.9]$,
									      $[0.05, \; 0.9, \; 0.05]$, $[0.1,\; 0.9]$ & Same \\
			Local spatial bins & $[ 0.1, \; 0.8,\;0.1 ]$ & Wrap \\
			Speed info & $[ 0.1, \; 0.8,\;0.1 ]$  & Same \\
			\hline
		\end{tabular}
	\end{center}
\end{table}


\section{Expression types}\label{sec:dsdl_expression_types}

Expression types are a special category of data types whose instances can only exist and be operated upon
at the time of DSDL definition processing.
As such, expression types cannot be used to define attributes,
and their instances cannot be exchanged between nodes.

Expression types are used to represent values of constant expressions which are evaluated
when a DSDL definition is processed.
Results of such expressions can be used to define various constant properties,
such as array length boundaries or values of constant attributes.

Expression types are specified in this section.
Each expression type has a formal DSDL name for completeness;
even if such types can't be used to define attributes,
a well-defined formal name allows DSDL processing tools to emit well-formed
and understandable diagnostic messages.

\subsection{Rational number}\label{sec:dsdl_rational}

At the time of DSDL definition processing, integer and real numbers are represented internally as rational numbers
where the range of numerator and denominator is unlimited\footnote{%
Technically, the range may only be limited by the memory resources available to the DSDL processing tool.}.
DSDL processing tools are not permitted to introduce any implicit rational number transformations that
may result in a loss of information.

The DSDL name of the rational number type is ``\verb|rational|''.

Rational numbers are assumed to be stored in a normalized form, where the denominator is positive
and the greatest common divisor of the numerator and the denominator is one.

A rational number can be used in a context where an integer value is expected only if its denominator equals one.

Implicit conversions between boolean-valued entities and rational numbers are not allowed.

\begin{UAVCANSimpleTable}{Operators defined on instances of rational numbers}{|l l X X|}
    Op & Type & Constraints & Description
    \label{table:dsdl_operators_rational} \\

    \texttt{\textbf{+}}     & $(\texttt{rational}) \rightarrow \texttt{rational}$ & &
    No effect. \\
    \texttt{\textbf{-}}     & $(\texttt{rational}) \rightarrow \texttt{rational}$ & &
    Negation. \\

    \texttt{\textbf{**}}    & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Power denominator equals one &
    Exact exponentiation. \\

    \texttt{\textbf{**}}    & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Power denominator greater than one &
    Exponentiation with imp\-lem\-en\-ta\-ti\-on-de\-fin\-ed accuracy. \\

    \texttt{\textbf{*}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ & &
    Exact multiplication. \\

    \texttt{\textbf{/}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Non-zero divisor &
    Exact division. \\

    \texttt{\textbf{\%}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Non-zero divisor &
    Exact modulo. \\

    \texttt{\textbf{+}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ & &
    Exact addition. \\

    \texttt{\textbf{-}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ & &
    Exact subtraction. \\

    \texttt{\textbf{|}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Denominators equal one &
    Bitwise or. \\

    \texttt{\textbf{\textasciicircum{}}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Denominators equal one &
    Bitwise xor. \\

    \texttt{\textbf{\&}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{rational}$ &
    Denominators equal one &
    Bitwise and. \\

    \texttt{\textbf{!=}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Exact inequality. \\
    \texttt{\textbf{==}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Exact equality. \\
    \texttt{\textbf{<=}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Less or equal. \\
    \texttt{\textbf{>=}} & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Greater or equal. \\
    \texttt{\textbf{<}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Strictly less. \\
    \texttt{\textbf{>}}  & $(\texttt{rational}, \texttt{rational}) \rightarrow \texttt{bool}$ & & Strictly greater. \\

\end{UAVCANSimpleTable}

\subsection{Unicode string}\label{sec:dsdl_string}

This type contains a sequence of Unicode characters.
It is used to represent string literals internally.

The DSDL name of the Unicode string type is ``\verb|string|''.

A Unicode string containing one symbol whose code point is within $[0, 127]$
(i.e., an ASCII character) is implicitly convertible into a \verb|uint8|-typed constant attribute value,
where the value of the constant is to be equal the code point of the symbol.

\begin{UAVCANSimpleTable}{Operators defined on instances of Unicode strings}{|l l X|}
    Op & Type & Description
    \label{table:dsdl_operators_string} \\

    \texttt{\textbf{+}}  & $(\texttt{string}, \texttt{string}) \rightarrow \texttt{string}$ &
    Concatenation. \\

    \texttt{\textbf{!=}} & $(\texttt{string}, \texttt{string}) \rightarrow \texttt{bool}$ &
    Inequality of Unicode NFC normalized forms.
    NFC stands for \emph{Normalization Form Canonical Composition} --
    one of standard Unicode normalization forms where characters are recomposed by canonical equivalence. \\

    \texttt{\textbf{==}} & $(\texttt{string}, \texttt{string}) \rightarrow \texttt{bool}$ &
    Equality of Unicode NFC normalized forms. \\

\end{UAVCANSimpleTable}

The set of operations and conversions defined for Unicode strings is to be extended in future versions of
the specification.

\subsection{Set}\label{sec:dsdl_set}

A set type represents an unordered collection of unique objects.
All objects must be of the same type.
Uniqueness of elements is determined by application of the equality operator ``\verb|==|''.

The DSDL name of the set type is ``\verb|set|''.

A set can be constructed from a set literal, in which case such set must contain at least one element.

The attributes and operators defined on set instances are listed in the tables~\ref{table:dsdl_set_attributes}
and~\ref{table:dsdl_set_operators}, where $E$ represents the set element type.

\begin{UAVCANSimpleTable}{Attributes defined on instances of sets}{|l l X X|}
    Name & Type & Constraints & Description
    \label{table:dsdl_set_attributes} \\

    \texttt{min} & $E$ &
    Operator ``\texttt{<}'' is defined \mbox{$(E, E) \rightarrow \texttt{bool}$} &
    Smallest element in the set determined by sequential application of the operator ``\texttt{<}''. \\

    \texttt{max} & $E$ &
    Operator ``\texttt{>}'' is defined \mbox{$(E, E) \rightarrow \texttt{bool}$} &
    Greatest element in the set determined by sequential application of the operator ``\texttt{>}''. \\

    \texttt{count} & \texttt{rational} & &
    Cardinality. \\

\end{UAVCANSimpleTable}

\newcommand\SetElementwiseOperator[1]{%
    \texttt{\textbf{#1}} & $(\texttt{set}_\texttt{<E>}, E) \rightarrow \texttt{set}_\texttt{<R>}$ & $E$ is not a set &
    Elementwise $(E, E) \rightarrow R$.\\

    \texttt{\textbf{#1}} & $(E, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{set}_\texttt{<R>}$ & $E$ is not a set &
    Elementwise $(E, E) \rightarrow R$.\\
}

\begin{UAVCANSimpleTable}{Operators defined on instances of sets}{|l l l X|}%
    \label{table:dsdl_set_operators}%
    Op & Type & Constraints & Description \\

    \texttt{\textbf{==}} & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left equals right. \\

    \texttt{\textbf{!=}} & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left does not equal right. \\

    \texttt{\textbf{<=}} & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left is a subset of right. \\

    \texttt{\textbf{>=}} & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left is a superset of right. \\

    \texttt{\textbf{<}}  & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left is a proper subset of right. \\

    \texttt{\textbf{>}}  & $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{bool}$ & &
    Left is a proper superset of right. \\

    \texttt{\textbf{|}} &
    $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{set}_\texttt{<E>}$ & &
    Union. \\

    \texttt{\textbf{\textasciicircum{}}} &
    $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{set}_\texttt{<E>}$ & &
    Disjunctive union. \\

    \texttt{\textbf{\&}} &
    $(\texttt{set}_\texttt{<E>}, \texttt{set}_\texttt{<E>}) \rightarrow \texttt{set}_\texttt{<E>}$ & &
    Intersection. \\

    \SetElementwiseOperator{**}
    \SetElementwiseOperator{*}
    \SetElementwiseOperator{/}
    \SetElementwiseOperator{\%}
    \SetElementwiseOperator{+}
    \SetElementwiseOperator{-}

\end{UAVCANSimpleTable}

\subsection{Serializable metatype}\label{sec:dsdl_metaserializable}

Serializable types (which are reviewed in section \ref{sec:dsdl_serializable_types})
are instances of the serializable metatype.
This metatype is convenient for expression of various relations and attributes defined on serializable types.

The DSDL name of the serializable metatype is ``\verb|metaserializable|''.

Available attributes are defined on a per-instance basis.


\section{Elimination of nondeterminism}

\subsection{Elimination of $\epsilon$-moves}
For each $p \xrightarrow{\epsilon} q$:
\begin{itemize}
    \item For each $q \xrightarrow{a} r$ where $a$ can be anything (including $\epsilon$) add an arc $p \xrightarrow{a} r$
    \item If $q$ was final, $p$ becomes final
    \item Remove $p \xrightarrow{\epsilon} q$
\end{itemize}

If there is a cycle of $\epsilon$-moves all the involved nodes can be merged.

\subsection{Berry-Sethi Method}
\begin{itemize}
    \item Number non-$\epsilon$ arcs and add $\dashv$ on exiting final states
    \item Compute $Ini$ and $Fin$ with the same rules as r.e.
    \item Apply Berry-Sethi construction
\end{itemize}
The automaton obtained is deterministic but can be non-minimal.


\subsection{The singular value decomposition}

We begin this section with an important definition.

\begin{definition}{Singular values}{singular-values}
Let $A$ be an $m\times n$-matrix. The singular values of $A$ are the square roots of the positive
eigenvalues of $A^TA$\index{singular values}.
\end{definition}

Singular Value Decomposition (SVD) can be thought of as
a generalization of orthogonal diagonalization of a symmetric matrix
to an arbitrary $m\times n$-matrix. This decomposition is the focus of this section.

The following is a useful result that will help when computing the SVD of matrices.

\begin{proposition}{}{same-non-zero-eigenvalues}
Let $A$ be an $m \times n$-matrix. Then $A^TA$ and $AA^T$ have the same \bf{non-zero} eigenvalues.
\end{proposition}

\begin{proof}
Suppose $A$ is an $m\times n$-matrix, and suppose that  $\lambda$ is a non-zero eigenvalue of $A^TA$.
Then there exists a non-zero vector $X\in \R^n$ such that

\begin{equation}\label{non-zero}
(A^TA)X=\lambda X.
\end{equation}

Multiplying both sides of this equation by $A$ yields:
\begin{eqnarray*}
A(A^TA)X & = & A\lambda X\\
(AA^T)(AX) & = & \lambda (AX).
\end{eqnarray*}
Since $\lambda\neq 0$ and $X\neq 0_n$, $\lambda X\neq 0_n$,
and thus by equation~\eqref{non-zero},
$(A^TA)X\neq 0_m$; thus $A^T(AX)\neq 0_m$,
implying that $AX\neq 0_m$.

Therefore $AX$ is an eigenvector of $AA^T$ corresponding to eigenvalue
$\lambda$.  An analogous argument can be used to show that every
non-zero eigenvalue of $AA^T$ is an eigenvalue of $A^TA$, thus
completing the proof.
\end{proof}

Given an $m\times n$-matrix $A$, we will see how to express $A$ as a product
\[ A=U\Sigma V^T\]
where
\begin{itemize}
\item $U$ is an $m\times m$ orthogonal matrix whose columns are
eigenvectors of $AA^T$.
\item $V$ is an $n\times n$ orthogonal matrix whose columns are
eigenvectors of $A^TA$.
\item $\Sigma$ is an $m\times n$-matrix whose only non-zero values
lie on its main diagonal, and are the singular values of $A$.
\end{itemize}

How can we find such a decomposition? We are aiming to decompose $A$ in the following form:

\begin{equation*}
A=U\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix} V^T
\end{equation*}

where $\sigma $ is of the form
\[
\sigma =\begin{mymatrix}{ccc}
\sigma _{1} &  & 0 \\
& \ddots &  \\
0 &  & \sigma _{k}
\end{mymatrix}
\]

Thus $A^T=V\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix} U^T$ and it follows that
\begin{equation*}
A^TA=V\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix} U^TU\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix} V^T=V\begin{mymatrix}{cc}
\sigma ^{2} & 0 \\
0 & 0
\end{mymatrix} V^T
\end{equation*}
and so $A^TAV=V\begin{mymatrix}{cc}
\sigma ^{2} & 0 \\
0 & 0
\end{mymatrix}$. Similarly, $AA^TU=U\begin{mymatrix}{cc}
\sigma ^{2} & 0 \\
0 & 0
\end{mymatrix}$. Therefore, you would find an orthonormal basis of eigenvectors
for $AA^T$ make them the columns of a matrix such that the
corresponding eigenvalues are decreasing. This gives $U$. You could then do
the same for $A^TA$ to get $V$.

We formalize this discussion in the following theorem.

\begin{theorem}{Singular value decomposition}{singular-value-decomposition}
Let $A$ be an $m\times n$-matrix. Then there exist
orthogonal matrices $U$ and $V$ of the appropriate size such that $A= U \Sigma V^T$ where $\Sigma$ is of the form
\[
\Sigma =
\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix}
\]
and $\sigma $ is of the form
\[
\sigma =\begin{mymatrix}{ccc}
\sigma _{1} &  & 0 \\
& \ddots &  \\
0 &  & \sigma _{k}
\end{mymatrix}
\]
for the $\sigma _{i}$ the singular values of $A$.
\end{theorem}

\begin{proof}
There exists an orthonormal basis, $\set{\vect{v}_{i}} _{i=1}^{n}$ such that $
A^TA\vect{v}_{i}=\sigma _{i}^{2}\vect{v}_{i}$ where $\sigma
_{i}^{2}>0$ for $i=1,\ldots,k,(\sigma _{i}>0) $ and equals zero
if $i>k$. Thus for $i>k$, $A\vect{v}_{i}=\vect{0}$ because
\begin{equation*}
 A\vect{v}_{i}\dotprod A\vect{v}_{i} = A^TA\vect{v}_{i} \dotprod \vect{v}_{i}  = \vect{0} \dotprod \vect{v}_{i} =0.
\end{equation*}
For $i=1,\ldots,k$, define $\vect{u}_{i}\in \R^{m}$ by
\begin{equation*}
\vect{u}_{i}= \sigma _{i}^{-1}A\vect{v}_{i}.
\end{equation*}

Thus $A\vect{v}_{i}=\sigma _{i}\vect{u}_{i}$. Now
\begin{eqnarray*}
\vect{u}_{i} \dotprod \vect{u}_{j} &=&  \sigma _{i}^{-1}A
\vect{v}_{i} \dotprod \sigma _{j}^{-1}A\vect{v}_{j}  = \sigma_{i}^{-1}\vect{v}_{i} \dotprod \sigma _{j}^{-1}A^TA\vect{v}_{j} \\
&=& \sigma _{i}^{-1}\vect{v}_{i} \dotprod \sigma _{j}^{-1}\sigma _{j}^{2} \vect{v}_{j} =
\frac{\sigma _{j}}{\sigma _{i}}(\vect{v}_{i} \dotprod \vect{v}_{j})
=\delta _{ij}.
\end{eqnarray*}
Thus $\set{\vect{u}_{i}} _{i=1}^{k}$ is an orthonormal set of
vectors in $\R^{m}$. Also,
\begin{equation*}
AA^T\vect{u}_{i}=AA^T\sigma _{i}^{-1}A\vect{v}_{i}=\sigma
_{i}^{-1}AA^TA\vect{v}_{i}=\sigma _{i}^{-1}A\sigma _{i}^{2}\vect{v}
_{i}=\sigma _{i}^{2}\vect{u}_{i}.
\end{equation*}
Now extend $\set{\vect{u}_{i}} _{i=1}^{k}$ to an orthonormal
basis for all of $\R^{m}$, $\set{\vect{u}_{i}} _{i=1}^{m}$
and let
\begin{equation*}
U= \begin{mymatrix}{ccc}
\vect{u}_{1} & \cdots & \vect{u}_{m}
\end{mymatrix}
\end{equation*}
while $V= \mat{\vect{v}_{1},\ldots,\vect{v}_{n}}$. Thus $U$
is the matrix which has the $\vect{u}_{i}$ as columns and $V$ is defined
as the matrix which has the $\vect{v}_{i}$ as columns. Then
\begin{equation*}
U^TAV=\begin{mymatrix}{c}
\vect{u}_{1}^T \\
\vdots \\
\vect{u}_{k}^T \\
\vdots \\
\vect{u}_{m}^T
\end{mymatrix} A\mat{\vect{v}_{1},\ldots,\vect{v}_{n}}
\end{equation*}
\begin{equation*}
=\begin{mymatrix}{c}
\vect{u}_{1}^T \\
\vdots \\
\vect{u}_{k}^T \\
\vdots \\
\vect{u}_{m}^T
\end{mymatrix} \begin{mymatrix}{cccccc}
\sigma _{1}\vect{u}_{1} & \cdots & \sigma _{k}\vect{u}_{k} & \vect{0}
& \cdots & \vect{0}
\end{mymatrix} =\begin{mymatrix}{cc}
\sigma & 0 \\
0 & 0
\end{mymatrix}
\end{equation*}
where $\sigma $ is given in the statement of the theorem.
\end{proof}

The singular value decomposition has as an immediate corollary which is given in the following interesting result.

\begin{corollary}{Rank and singular values}{rank-singular-values}
Let $A$ be an $m\times n$-matrix. Then the rank of $A$ and $A^T$equals
the number of singular values.
\end{corollary}

%%\begin{proof}
%%Since $V$ and $U$ are unitary, it follows that
%%\begin{eqnarray*}
%%\rank(A) &=&\rank(U^TAV) =
%%\rank\mat{
%%\begin{array}{cc}
%%\sigma & 0 \\
%%0 & 0
%%\end{array}
%%} \\
%%&=&\text{number of singular values.}
%%\end{eqnarray*}
%%Also since $U,V$ are unitary,
%%\begin{equation*}
%%\rank(A^T) =\rank(V^TA^{\ast
%%}U) =\rank((U^TAV)^T)
%%\end{equation*}
%%\begin{equation*}
%%=\rank\paren{\mat{
%%\begin{array}{cc}
%%\sigma & 0 \\
%%0 & 0
%%\end{array}
%%} ^T} =\text{number of singular values.}
%%\end{equation*}
%%\end{proof}
%%
%%\medskip

Let's compute the Singular Value Decomposition of a simple matrix.

\begin{example}{Singular value decomposition}{SVD}
Let
$A=\begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}$.
Find the Singular Value Decomposition (SVD) of $A$.
\end{example}

\begin{solution}
To begin, we compute $AA^T$ and $A^TA$.
\[ AA^T = \begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}
\begin{mymatrix}{rr} 1 & 3 \\ -1 & 1 \\ 3 & 1  \end{mymatrix}
= \begin{mymatrix}{rr} 11 & 5 \\ 5 & 11  \end{mymatrix}.\]

\[ A^TA = \begin{mymatrix}{rr} 1 & 3 \\ -1 & 1 \\ 3 & 1  \end{mymatrix}
\begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}
= \begin{mymatrix}{rrr} 10 & 2 & 6 \\ 2 & 2 & -2\\
6 & -2 & 10 \end{mymatrix}.\]

Since $AA^T$ is $2\times 2$ while $A^T A$ is $3\times 3$, and $AA^T$
and $A^TA$ have the same {\em non-zero} eigenvalues (by Proposition~\ref{prop:same-non-zero-eigenvalues}), we compute the characteristic polynomial  $c_{AA^T}(x)$ (because it's
easier to compute than $c_{A^TA}(x)$).

\begin{eqnarray*}
c_{AA^T}(x)& = &\det(xI-AA^T)= \begin{absmatrix}{cc}
x-11 & -5 \\ -5 & x-11 \end{absmatrix}\\
& = &(x-11)^2 - 25 \\
& = & x^2-22x+121-25\\
& = & x^2-22x+96\\
& = & (x-16)(x-6)
\end{eqnarray*}

Therefore, the eigenvalues of $AA^T$ are $\lambda_1=16$ and $\lambda_2=6$.

The eigenvalues of $A^TA$ are $\lambda_1=16$, $\lambda_2=6$, and
$\lambda_3=0$, and the singular values of $A$ are $\sigma_1=\sqrt{16}=4$ and
$\sigma_2=\sqrt{6}$.
By convention, we list the eigenvalues (and corresponding singular values)
in non-increasing order (i.e., from largest to smallest).

{\bf To find the matrix $V$}:

To construct the matrix $V$ we need to find eigenvectors for $A^TA$.
Since the eigenvalues of $AA^T$ are distinct, the corresponding
eigenvectors are orthogonal, and we need only normalize them.

$\lambda_1=16$: solve $(16I-A^TA)Y= 0$.

\[ \begin{mymatrix}{rrr|r}
6 & -2 & -6 & 0 \\ -2 & 14 & 2 & 0 \\ -6 & 2 & 6 & 0
\end{mymatrix}
\rightarrow
\begin{mymatrix}{rrr|r}
1 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0
\end{mymatrix},
\mbox{ so }
Y=\begin{mymatrix}{r} t \\ 0 \\ t \end{mymatrix}
=t\begin{mymatrix}{r} 1 \\ 0 \\ 1 \end{mymatrix},
t\in \R. \]

$\lambda_2=6$: solve $(6I-A^TA)Y= 0$.

\[ \begin{mymatrix}{rrr|r}
-4 & -2 & -6 & 0 \\ -2 & 4 & 2 & 0 \\ -6 & 2 & -4 & 0
\end{mymatrix}
\rightarrow
\begin{mymatrix}{rrr|r}
1 & 0 & 1 & 0 \\ 0 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0
\end{mymatrix},
\mbox{ so }
Y=\begin{mymatrix}{r} -s \\ -s \\ s \end{mymatrix}
=s\begin{mymatrix}{r} -1 \\ -1 \\ 1 \end{mymatrix},
s\in \R. \]

$\lambda_3=0$: solve $(-A^TA)Y= 0$.
\[ \begin{mymatrix}{rrr|r}
-10 & -2 & -6 & 0 \\ -2 & -2 & 2 & 0 \\ -6 & 2 & -10 & 0
\end{mymatrix}
\rightarrow
\begin{mymatrix}{rrr|r}
1 & 0 & 1 & 0 \\ 0 & 1 & -2 & 0 \\ 0 & 0 & 0 & 0
\end{mymatrix},
\mbox{ so }
Y=\begin{mymatrix}{r} -r \\ 2r \\ r \end{mymatrix}
=r\begin{mymatrix}{r} -1 \\ 2 \\ 1 \end{mymatrix},
r\in \R. \]

Let
\[ V_1=\frac{1}{\sqrt{2}}\begin{mymatrix}{r} 1\\ 0\\ 1 \end{mymatrix},
V_2=\frac{1}{\sqrt{3}}\begin{mymatrix}{r} -1\\ -1\\ 1 \end{mymatrix},
V_3=\frac{1}{\sqrt{6}}\begin{mymatrix}{r} -1\\ 2\\ 1 \end{mymatrix}.\]

Then
\[ V=\frac{1}{\sqrt{6}}\begin{mymatrix}{rrr}
\sqrt 3 & -\sqrt 2 & -1  \\
0 & -\sqrt 2 & 2 \\
\sqrt 3 & \sqrt 2 & 1 \end{mymatrix}.\]

Also,
\[ \Sigma = \begin{mymatrix}{rrr} 4 & 0 & 0 \\
0 & \sqrt 6 & 0 \end{mymatrix},\]
and we use $A$, $V^T$, and $\Sigma$ to find $U$.

Since $V$ is orthogonal and $A=U\Sigma V^T$, it follows that $AV=U\Sigma$.
Let $V=\begin{mymatrix}{ccc} V_1 & V_2 & V_3 \end{mymatrix}$, and
let $U=\begin{mymatrix}{cc} U_1 & U_2 \end{mymatrix}$, where
$U_1$ and $U_2$ are the two columns of $U$.

Then we have
\begin{eqnarray*}
A\begin{mymatrix}{ccc} V_1 & V_2 & V_3 \end{mymatrix}
&=& \begin{mymatrix}{cc} U_1 & U_2 \end{mymatrix}\Sigma\\
\begin{mymatrix}{ccc} AV_1 & AV_2 & AV_3 \end{mymatrix}
&=& \begin{mymatrix}{ccc} \sigma_1U_1 + 0U_2 &
0U_1 + \sigma_2 U_2 & 0 U_1 + 0 U_2 \end{mymatrix} \\
&=& \begin{mymatrix}{ccc} \sigma_1U_1 & \sigma_2 U_2 &
0 \end{mymatrix}
\end{eqnarray*}
which implies that $AV_1=\sigma_1U_1 = 4U_1$ and
$AV_2=\sigma_2U_2 = \sqrt 6 U_2$.

Thus,
\[ U_1 = \frac{1}{4}AV_1
= \frac{1}{4}
\begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}
\frac{1}{\sqrt{2}}\begin{mymatrix}{r} 1\\ 0\\ 1 \end{mymatrix}
= \frac{1}{4\sqrt 2}\begin{mymatrix}{r} 4\\ 4 \end{mymatrix}
= \frac{1}{\sqrt 2}\begin{mymatrix}{r} 1\\ 1 \end{mymatrix},\]
and
\[ U_2 = \frac{1}{\sqrt 6}AV_2
= \frac{1}{\sqrt 6}
\begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}
\frac{1}{\sqrt{3}}\begin{mymatrix}{r} -1\\ -1\\ 1 \end{mymatrix}
=\frac{1}{3\sqrt 2}\begin{mymatrix}{r} 3\\ -3 \end{mymatrix}
=\frac{1}{\sqrt 2}\begin{mymatrix}{r} 1\\ -1 \end{mymatrix}.
\]
Therefore,
\[ U=\frac{1}{\sqrt{2}}\begin{mymatrix}{rr} 1 & 1 \\
1 & -1 \end{mymatrix},\]
and
\begin{eqnarray*}
A & = & \begin{mymatrix}{rrr} 1 & -1 & 3 \\ 3 & 1 & 1 \end{mymatrix}\\
& = & \paren{\frac{1}{\sqrt{2}}\begin{mymatrix}{rr} 1 & 1 \\
1 & -1 \end{mymatrix}}
\begin{mymatrix}{rrr} 4 & 0 & 0 \\
0 & \sqrt 6 & 0 \end{mymatrix}
\paren{\frac{1}{\sqrt{6}}\begin{mymatrix}{rrr}
\sqrt 3 & 0 & \sqrt 3  \\
-\sqrt 2 & -\sqrt 2 & \sqrt2 \\
-1 & 2 & 1 \end{mymatrix}}.
\end{eqnarray*}
\end{solution}

Here is another example.

\begin{example}{Finding the SVD}{SVD2}
Find an SVD for
$A=\begin{mymatrix}{r} -1 \\ 2\\ 2 \end{mymatrix}$.
\end{example}

\begin{solution}
Since $A$ is $3\times 1$, $A^T A$ is a $1\times 1$-matrix
whose eigenvalues are easier to find than the eigenvalues of
the $3\times 3$-matrix $AA^T$.

\[ A^TA=\begin{mymatrix}{ccc} -1 & 2 & 2 \end{mymatrix}
\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
=\begin{mymatrix}{r} 9 \end{mymatrix}.\]

Thus $A^TA$ has eigenvalue $\lambda_1=9$, and
the eigenvalues of $AA^T$ are $\lambda_1=9$, $\lambda_2=0$, and
$\lambda_3=0$.
Furthermore, $A$ has only one singular value, $\sigma_1=3$.

{\bf To find the matrix $V$}:
To do so we find an eigenvector for $A^TA$ and
normalize it.
In this case, finding a unit eigenvector is trivial:
$V_1=\begin{mymatrix}{r} 1 \end{mymatrix}$, and
\[ V=\begin{mymatrix}{r} 1 \end{mymatrix}.\]

Also,
$\Sigma =\begin{mymatrix}{r} 3 \\ 0\\ 0 \end{mymatrix}$,
and we use $A$, $V^T$, and $\Sigma$ to find $U$.

Now $AV=U\Sigma$, with
$V=\begin{mymatrix}{r} V_1 \end{mymatrix}$,
and $U=\begin{mymatrix}{rrr} U_1 & U_2 & U_3 \end{mymatrix}$,
where $U_1$, $U_2$, and $U_3$ are the columns of $U$.
Thus
\begin{eqnarray*}
A\begin{mymatrix}{r} V_1 \end{mymatrix}
&=& \begin{mymatrix}{rrr} U_1 & U_2 & U_3 \end{mymatrix}\Sigma\\
\begin{mymatrix}{r} AV_1 \end{mymatrix}
&=& \begin{mymatrix}{r} \sigma_1 U_1+0U_2+0U_3 \end{mymatrix}\\
&=& \begin{mymatrix}{r} \sigma_1 U_1 \end{mymatrix}
\end{eqnarray*}
This gives us $AV_1=\sigma_1 U_1= 3U_1$, so

\[ U_1 = \frac{1}{3}AV_1
= \frac{1}{3}
\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
\begin{mymatrix}{r} 1 \end{mymatrix}
= \frac{1}{3}
\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}.\]

The vectors $U_2$ and $U_3$ are eigenvectors of $AA^T$ corresponding
to the eigenvalue $\lambda_2=\lambda_3=0$.
Instead of solving the system $(0I-AA^T)X= 0$ and then using the
Gram-Schmidt process on the resulting set of
two basic eigenvectors, the following approach may be used.

Find vectors $U_2$ and $U_3$ by first extending $\set{U_1}$ to a basis of
$\R^3$, then using the Gram-Schmidt algorithm to orthogonalize the basis,
and finally normalizing the vectors.

Starting with $\set{3U_1}$ instead of $\set{U_1}$ makes the
arithmetic a bit easier.
It is easy to verify that

\[ \set{\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix},
\begin{mymatrix}{r} 1 \\ 0 \\ 0 \end{mymatrix},
\begin{mymatrix}{r} 0 \\ 1 \\ 0 \end{mymatrix}}\]
is a basis of $\R^3$.  Set

\[ E_1 = \begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix},
X_2 = \begin{mymatrix}{r} 1 \\ 0 \\ 0 \end{mymatrix},
X_3 =\begin{mymatrix}{r} 0 \\ 1 \\ 0 \end{mymatrix},\]

and apply the Gram-Schmidt algorithm to
$\set{E_1, X_2, X_3}$.

This gives us

\[ E_2 = \begin{mymatrix}{r} 4 \\ 1 \\ 1 \end{mymatrix}
\mbox{ and }
E_3 = \begin{mymatrix}{r} 0 \\ 1 \\ -1 \end{mymatrix}.\]

Therefore,
\[ U_2 = \frac{1}{\sqrt{18}}
 \begin{mymatrix}{r} 4 \\ 1 \\ 1 \end{mymatrix},
U_3 = \frac{1}{\sqrt 2}
\begin{mymatrix}{r} 0 \\ 1 \\ -1 \end{mymatrix},\]
and

\[ U = \begin{mymatrix}{rrr} -\frac{1}{3} & \frac{4}{\sqrt{18}} & 0 \\
\frac{2}{3} & \frac{1}{\sqrt{18}} & \frac{1}{\sqrt 2} \\
\frac{2}{3} & \frac{1}{\sqrt{18}} & -\frac{1}{\sqrt 2} \end{mymatrix}.\]

Finally,

\[ A =
\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
=
\begin{mymatrix}{rrr} -\frac{1}{3} & \frac{4}{\sqrt{18}} & 0 \\
\frac{2}{3} & \frac{1}{\sqrt{18}} & \frac{1}{\sqrt 2} \\
\frac{2}{3} & \frac{1}{\sqrt{18}} & -\frac{1}{\sqrt 2} \end{mymatrix}
\begin{mymatrix}{r} 3 \\ 0 \\ 0 \end{mymatrix}
\begin{mymatrix}{r} 1 \end{mymatrix}.\]

\end{solution}

Consider another example.

\begin{example}{Find the SVD}{SVD3}
Find a singular value decomposition for the matrix
\begin{equation*}
A= \begin{mymatrix}{ccc}
\frac{2}{5}\sqrt{2}\sqrt{5} & \frac{4}{5}\sqrt{2}\sqrt{5} & 0 \\
\frac{2}{5}\sqrt{2}\sqrt{5} & \frac{4}{5}\sqrt{2}\sqrt{5} & 0
\end{mymatrix}
\end{equation*}
\end{example}

First consider $A^TA$
\begin{equation*}
\begin{mymatrix}{ccc}
\frac{16}{5} & \frac{32}{5} & 0 \\
\frac{32}{5} & \frac{64}{5} & 0 \\
0 & 0 & 0
\end{mymatrix}
\end{equation*}
What are some eigenvalues and eigenvectors? Some computing shows these are

\begin{equation*}
\set{\begin{mymatrix}{c}
0 \\
0 \\
1
\end{mymatrix} ,\begin{mymatrix}{c}
-\frac{2}{5}\sqrt{5} \\
\frac{1}{5}\sqrt{5} \\
0
\end{mymatrix} } \leftrightarrow 0,\set{\begin{mymatrix}{c}
\frac{1}{5}\sqrt{5} \\
\frac{2}{5}\sqrt{5} \\
0
\end{mymatrix} } \leftrightarrow 16
\end{equation*}
Thus the matrix $V$ is given by
\begin{equation*}
V=\begin{mymatrix}{ccc}
\frac{1}{5}\sqrt{5} & -\frac{2}{5}\sqrt{5} & 0 \\
\frac{2}{5}\sqrt{5} & \frac{1}{5}\sqrt{5} & 0 \\
0 & 0 & 1
\end{mymatrix}
\end{equation*}
Next consider $AA^T$
\begin{equation*}
\begin{mymatrix}{cc}
8 & 8 \\
8 & 8
\end{mymatrix}
\end{equation*}
Eigenvectors and eigenvalues are

\begin{equation*}
\set{\begin{mymatrix}{c}
-\frac{1}{2}\sqrt{2} \\
\frac{1}{2}\sqrt{2}
\end{mymatrix} } \leftrightarrow 0,\set{\begin{mymatrix}{c}
\frac{1}{2}\sqrt{2} \\
\frac{1}{2}\sqrt{2}
\end{mymatrix} } \leftrightarrow 16
\end{equation*}
Thus you can let $U$ be given by
\begin{equation*}
U=\begin{mymatrix}{cc}
\frac{1}{2}\sqrt{2} & -\frac{1}{2}\sqrt{2} \\
\frac{1}{2}\sqrt{2} & \frac{1}{2}\sqrt{2}
\end{mymatrix}
\end{equation*}
Let us check this. $U^TAV=$
\begin{equation*}
\begin{mymatrix}{cc}
\frac{1}{2}\sqrt{2} & \frac{1}{2}\sqrt{2} \\
-\frac{1}{2}\sqrt{2} & \frac{1}{2}\sqrt{2}
\end{mymatrix} \begin{mymatrix}{ccc}
\frac{2}{5}\sqrt{2}\sqrt{5} & \frac{4}{5}\sqrt{2}\sqrt{5} & 0 \\
\frac{2}{5}\sqrt{2}\sqrt{5} & \frac{4}{5}\sqrt{2}\sqrt{5} & 0
\end{mymatrix} \begin{mymatrix}{ccc}
\frac{1}{5}\sqrt{5} & -\frac{2}{5}\sqrt{5} & 0 \\
\frac{2}{5}\sqrt{5} & \frac{1}{5}\sqrt{5} & 0 \\
0 & 0 & 1
\end{mymatrix}
\end{equation*}
\begin{equation*}
=\begin{mymatrix}{ccc}
4 & 0 & 0 \\
0 & 0 & 0
\end{mymatrix}
\end{equation*}

This illustrates that if you have a good way to find the eigenvectors and
eigenvalues for a Hermitian matrix which has non-negative eigenvalues, then
you also have a good way to find the singular value decomposition of an
arbitrary matrix.


\section{}
The network presented in \ref{sec:sgd} uses a sigmoid function as the activation function
of neurons. Using the same structure and initial weights given in \ref{sec:weights} we replace
the sigmoid function with a bipolar sigmoid function given in equation \ref{eq:bipolar_sigmoid}

\begin{equation}
\label{eq:bipolar_sigmoid}
 f(x)= \frac{2}{1 + e^{-x}} - 1
\end{equation}

\begin{equation}
\label{eq:bipolar_sigmoid_derivative}
 f'(x)= \frac{1}{2} (1 - f(x)^2)
\end{equation}

The input and output to the network are too converted into their bipolar representation. The
plot of training cost vs iterations is shown in figure \ref{fig:cost_bipolar}. Comparing
figures \ref{fig:cost_sgd} and \ref{fig:cost_bipolar} we can see that the network using bipolar
sigmoid with bipolar XOR representation converged in fewer number of iterations then original
network.

\section{}
In order to evaluate the impact of Nguyen-Widrow approach of weight initialization on convergence
a neural network similar to the one described in \ref{sec:sgd} was constructed. Then the
sigmoid function was replaced with hyperbolic tangent function and bipolar representation of XOR
gate was used as the training data. The training cost with respect to the iterations is given in figure
\ref{fig:cost_tanh}. \newline

Above experiment was repeated after initializing the weights and biases as per the Nguyen-Widrow
approach. The plot of the training cost with respect iterations is shown in figure \ref{fig:cost_tanh_nw}.
Comparing figure \ref{fig:cost_tanh_nw} with figure \ref{fig:cost_tanh} it can seen that using Nguyen-Widrow approach
to initialize the weights smoothens the decrease in cost per iteration. Figure \ref{fig:tanh_nw_first_2500_compare} shows
the training cost for the first 2500 iteration of both cases.

\section{}
Initially a neural network with 3 units in first hidden layer and 2 units in second hidden layer was constructed.
The weights of the network is randomly initialized in the range $[-0.5, 0.5]$. As for the activation function,
hyperbolic tangent function was used and SGD with momentum coefficient($\mu$)$=0.5$ was used to optimize the parameters.
The plot for training cost with respect to iteration is given in figure \ref{fig:cost_two_hidden_layer}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS240-2016S: Programming in C
% Copyright 2016 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS240-2016S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 3}

Write a program \texttt{sequence.c} that without using the multiply operator \texttt{*}, prints numbers $n = 2^{i}-1$ where $0 \leq i \leq 64$ separated by a single space.


\chapter{Short-rate models}
\label{chap:shortrate}

\added{This chapter introduces basics of short-rate modeling and short-rate models iwth affine term-structures.}

\section{Introduction to short-rate models}

\subsection{Term-structure equation}

\added{The derivation of the term-structure equation follows closely} \textcite[pp. 319--324]{bjork2004arbitrage}.

In this chapter we assume that the short rate process follows
\begin{align}
\label{short-ratedynamicsundersomemeasure}
\dx r(t) = \mu(t,r(t)) \dx t + \sigma(t,r(t)) \dx W(t) ,
\end{align}
where $W(t)$ is a Brownian motion under some measure $\Pm^*$ (this may be also physical measure) and $\mu$ and $\sigma$ are well-behaved functions. If $V(t,r(t))$ is a smooth function, then It\'{o}s lemma says that
\begin{align}
\dx V = \left( \frac{\partial V}{\partial t} + \mu \frac{\partial V}{\partial x} +\frac{1}{2} \sigma^2 \frac{\partial^2 V}{\partial x^2} \right) \dx t + \sigma \frac{\partial V}{\partial x} \dx W
\end{align}
Here we have dropped arguments for functions. Unlike in Black-Scholes model, we may not trade short rate $r(t)$ directly and it may not be used in hedging. But we may consider how to hedge a derivative using another contingent claim.

We consider two contingent claims with value processes $V_1(t, r(t))$ and $V_2(t,r(t))$. It\'{o}'s lemma gives us that
\begin{align}
\dx V_i = M_i \dx t + N_i \dx W,
\end{align}
where
\begin{align}
M_i &= \frac{\partial V_i}{\partial t} + \mu \frac{\partial V_i}{\partial r} + \frac{1}{2} \sigma^2 \frac{\partial^2 V_i}{\partial r^2}, \\
N_i &= \sigma \frac{\partial V_i}{\partial r} .
\end{align}
If $\Pi = V_1 + \delta V_2$ is a portfolio, then
\begin{align}
\dx \Pi = \left( M_1 + \delta M_2 \right) \dx t + \left( N_1 + \delta N_2 \right) \dx W .
\end{align}
By choosing $\delta = - \frac{N_1}{N_2}$, the Brownian motion disappears. In order to maintain absence of arbitrage, we have that
\begin{align}
\dx \Pi &= r \Pi \dx t \\
\left( M_1 - \frac{N_1}{N_2} M_2 \right) \dx t &= r \left( V_1 - \frac{N_1}{N_2} V_2 \right) \dx t .
\end{align}
Thus
\begin{align}
\frac{M_1 - r V_1}{N_1 } = \frac{M_2 - r V_2}{N_2}
\end{align}
Now $M_i, N_i$ and $V_i$ are functions of $t$ and $r(t)$. The left-hand side is independent of the portfolio $V_2$ and the right-hand size is independent of portfolio $V_1$. Hence there exist a function
\begin{align}
\lambda(t,r(t)) = \frac{M(t,r(t)) - r(t) V(t,r(t))}{N(t,r(t))}
\end{align}
called the market-price of risk, where $V$ is any interest-rate derivative with dynamics
\begin{align}
\dx V = M \dx t + N \dx W .
\end{align}
By combining this with earlier results, we get that the price process $V$ must satisfy partial differential equation
\begin{align}
\label{termstructureequation}
0 &= M - rV - \lambda N \\
&= \frac{\partial V}{\partial t} + \mu \frac{\partial V}{\partial r} + \frac{1}{2} \sigma^2 \frac{\partial^2 V}{\partial r^2} - rV - \lambda \sigma \frac{\partial V}{\partial r} \\
&= \frac{\partial V}{\partial t} + \left( \mu - \lambda \sigma \right) \frac{\partial V}{\partial r} + \frac{1}{2} \sigma^2 \frac{\partial^2 V}{\partial r^2} - rV
\end{align}
with the boundary condition given by the value $V(T, r(T))$.

\subsection{Fundamental models}

Suppose that $\Pm^* = \Pf$ is the physical measure. Models that are defined under the physical measure, are often called as fundamental models. If we denote
	\begin{align}
		\theta(t,r(t)) = \mu(t,r(t)) - \lambda(t,r(t)) \sigma(t,r(t))
	\end{align}
and assume that
	\begin{align}
		\lambda(t,r(t)) = \frac{\theta(t,r(t)) - \mu(t,r(t))}{\sigma(t,r(t))}
	\end{align}
may be used as a Girsanov kernel, then we get a new measure $\Pm_{\theta}$ under which
	\begin{align}
		r(t) = \theta(t,r(t)) \dx t + \sigma(t,r(t)) \dx W_{\theta} (t)
	\end{align}
and $W_{\theta} (t)$ is a Brownian motion. Now Feynman-Kac theorem implies that
	\begin{align}
		V(t) = \E_{\Pm_{\theta}} \left( \e^{-\int_t^T r(s) \dx s} V(T) \ | \ \F_t \right) .
	\end{align}
Unlike in the Black-Scholes model, the market price of risk $\lambda$ is not uniquely determined endogenously within the model. Here, the market price of risk is endogenously determined. The equivalent martingale measure (if it exists), it is not unique and prices depend on the choice of the function $\lambda$. However, the $\lambda$ is uniquely determined by the price of any interest rate derivative. Since the dynamic of Equation \ref{short-ratedynamicsundersomemeasure} are under the physical measure, we could use econometric time-series to estimate model parameters and market price of risk. This approach is problematic since neither the short-rate nor the market price of risk are not directly observable. According to \cite{chapmanlongpearson1999usingproxies}, using proxies to estimate parameters of short-rate models for single-factor affine models does not cause economically significant problems, but for more complex models, proxies cause significant errors. Also, for estimation, we usually have to assume a functional form for the market price of risk, which may be misspecified. Fundamental models have no guarantee that they will fit the observed term or volatility structures. If these obstacles are overcome, then the model may be used to price any instrument and forecast interest rates.

\subsection{Preference-free models}

Another approach is to assume that the Equation \ref{short-ratedynamicsundersomemeasure} holds under the risk-free measure $\Pm_0$. This means choosing $\lambda = 0$ in the Equation \ref{termstructureequation}, which will now read
\begin{align}
\label{termstructureequation2}
0 &= \frac{\partial V}{\partial t} + \mu \frac{\partial V}{\partial r} + \frac{1}{2} \sigma^2 \frac{\partial^2 V}{\partial r^2} - rV .
\end{align}
Therefore Feynman-Kac theorem implies that
	\begin{align}
		V(t) = \E_{\Pm_0} \left( \e^{-\int_t^T r(s) \dx s} V(T) \ | \ \F_t \right) .
	\end{align}
for all assets. Thus all discounted asset price process, where the num\'{e}raire is the bank account, are martingales under $\Pm_0$.

Under this methodology, we may use calibrate the model parameters using observed market prices. We may take liquid instruments and then use their prices to calibrate the model. We may not use historical data in calibration, since the assumed process is under the risk-free measure. The physical measure will be different from the risk-free measure, unless we explicitly make the strong assumption that the market price of risk will be zero. However, as the volatility term does not change under measure changes, diffusion term may be estimated with data that is collected under the risk-free measure. Models under this methodology may or may not be guaranteed to fit the observed term and volatility structures. They could be useful for pricing. They may not be used to forecast prices or interest rates without further assumptions.

\subsubsection{Preference-free +-models}

Preference-free models which have constant parameters $k, \theta, \sigma$ are notated with single plus sign. They do not probably fit the observed term and volatility structures. Most of the early fundamental models can be also interpreted as preference-free $+$-models.

\subsubsection{Preference-free ++-models}

Preference-free models which have constant parameters $k, \sigma$ but time-varying $\theta(t)$ are notated with double plus sign. They can be made to fit  the observed term structure but they probably do not match the volatility surface. \textcite{hull1990pricing} is often the prototypical example of a $++$-model.

When matched to term-structure and calibrated with cap or swaption prices, they can be useful in pricing of exotic interest-rate options.

\subsubsection{Preference-free +++-models}

Preference-free models which have time-varying $\theta(t), \sigma(t)$ (and sometimes $k$) are notated with triple plus sign. They can be made to fit both the observed term structure and some of the volatility surface. Variants of Hull-White models are sometimes $+++$-models.

Triple plus models can be useful in pricing but they could be prone to over-fitting.

\section{One-factor short-rate models}

One-factor short-rate model is model that has one underlying state variable that drives the evolution of the interest rates. This factor is often the short-rate itself. Often used form for one-factor short-rate process is
\begin{align}
\dx r (t) = k( \theta (t) - r (t) ) \dx t + \sigma (t) r (t)^{\gamma} \dx W (t)
\end{align}
The following table gives a quick overview of some of the models of this form.
\begin{center}
	\begin{tabular}{|l|l|l|l|l|}
		\hline
		Model & $k(t)$ & $\theta (t)$ & $\sigma (t)$ & $\gamma$ \\
		\hline
		\hline
		\cite{vasicek1977equilibrium} & $k$ & $\theta$ & $\sigma$ & $-$ \\
		\hline
		\cite{dothan1978term} & $-k(t)$ & $-$ & $\sigma$ & $-$ \\
		\hline
		\cite{coxingersollross1985theory} & $k$ & $\theta$ & $\sigma$ & $\frac{1}{2}$ \\
		\hline
		\cite{hull1990pricing} & $k$ & $\theta (t)$ & $\sigma (t)$ & $-$ \\
		\hline
	\end{tabular}
\end{center}

\subsection{Affine one-factor term-structures models}

\added{The derivation of the following theorem is based on } \textcite[pp. 329--331]{bjork2004arbitrage}.

A one-factor short-rate model has affine term-structure if the zero-coupon bond price is
\begin{align}
\label{affinitytermstructureequation}
\Bond(t,T) = \e^{A(t,T) - B(t,T)r(t)}
\end{align}
for all $0 \leq t \leq T$, where $A(t,T)$ and $B(t,T)$ deterministic and smooth functions. Since $\Bond(T,T) = 1$, we have that
\begin{align}
A(T,T) = B(T,T) = 0.
\end{align}
Under the assumption of affinity the instantaneous forward rate is
\begin{align}
f(t,T) &= - \frac{\partial \log \Bond(t,T)}{\partial T} \\
&= \frac{\partial (B(t,T)r(t) - A(t,T))}{\partial T} \\
&= \frac{\partial B(t,T)}{\partial T}r(t) - \frac{\partial A(t,T)}{\partial T}
\end{align}
for all $0 \leq t \leq T$.

If we denote that $p(t,T) = F(t,T,r)$ to make clear that $\Bond(t,T)$ is also a function of $r$, then we have that
\begin{align}
\frac{\partial F}{\partial t} &= \left( \frac{\partial A}{\partial t} - \frac{\partial B}{\partial t} r \right) F \\
\frac{\partial F}{\partial r} &= - B F \\
\frac{\partial^2 F}{\partial r^2} &= B^2 F
\end{align}
as the derivative of $r$ with respect to $t$ vanishes. Since the price of a zero-coupon price must satisfy \ref{fundamentaltermstructure_equationinaffine}, we have that
\begin{align}
\frac{\partial F}{\partial t} + \mu^* \frac{\partial F}{\partial r} + \frac{1}{2} \sigma^2 \frac{\partial^2 F}{\partial r^2} - r F &= 0 \\
F(T,T,r) = 1.
\end{align}
By combining these, we have that
\begin{align}
0 &= \left( \frac{\partial A}{\partial t} - \frac{\partial B}{\partial t} r \right) F - \mu^* B F + \frac{1}{2} \sigma^2 B^2 F - r F, \\
0 &= \frac{\partial A}{\partial t} - \frac{\partial B}{\partial t} r - \mu^* B + \frac{1}{2} \sigma^2 B^2 - r \\
&= \frac{\partial A}{\partial t} - ( 1 + \frac{\partial B}{\partial t} ) r - \mu^* B + \frac{1}{2} \sigma^2 B^2
\end{align}

If we suppose that$\mu^*(t)$ and $\sigma^2(t)$ are affine in short-rate $r(t)$, meaning that
\begin{align}
\mu^*(t) &= a(t)r(t) + b(t) \\
\sigma^2(t) &= c(t)r(t) + d(t),
\end{align}
where $a,b,c$ and $d$ are deterministic functions. Thus
\begin{align}
0 	&= \frac{\partial A}{\partial t} - ( 1 + \frac{\partial B}{\partial t} ) r - \mu^* B + \frac{1}{2} \sigma^2 B^2 \\
&= \frac{\partial A}{\partial t} - ( 1 + \frac{\partial B}{\partial t} ) r - \left( ar+b \right) B + \frac{1}{2} \left( cr+d \right) B^2 \\
&= \left( \frac{\partial A}{\partial t} - bB + \frac{1}{2} d B^2 \right) +
\left( \frac{1}{2} cB^2 - \frac{\partial B}{\partial t} -aB - 1 \right) r
\end{align}
and this must hold for all $t, T$ and $r(t)$. Thus the coefficients must equal zero and we have that
\begin{align}
\frac{\partial A}{\partial t} - bB + \frac{1}{2} d B^2 &= 0, \\
A(T,T) &= 0, \\
\frac{1}{2} cB^2 - \frac{\partial B}{\partial t} -aB - 1 &= 0, \\
B(T,T) &= 0 .
\end{align}

Thus we have proved the following.

\begin{thm}
	\label{affineconfition}
	If
	\begin{align}
	\mu^*(t) &= a(t) r + b(t) \\
	\sigma^2(t) &= c(t) r + d(t),
	\end{align}
	then the short-rate model has an affine term-structure model and the following equations holds:
	\begin{align}
	&\begin{cases}
	\frac{\partial B}{\partial t} = \frac{1}{2} c B^2  -a B - 1 \\
	B(T,T) = 0
	\end{cases} \\
	&\begin{cases}
	\frac{\partial A}{\partial t} - bB + \frac{1}{2} d B^2 &= 0 \\
	A(T,T) = 0 .
	\end{cases}
	\end{align}
\end{thm}

Equations of this type are Riccati Equations and they are easy to solved efficiently.

\subsection{Va\v{s}\'{i}\v{c}ek--model}

The material from this section is from \textcite[pp. 58--62]{brigo2007interest}.

\textcite{vasicek1977equilibrium} showed that under certain economic assumptions, the short-rate process is a Ornstein-Uhlenbeck process. This particular form was earlier suggest by \textcite{merton1971optimum}. Under the Va\v{s}\'{i}\v{c}ek--model, the short-rate process $r(t)$ is given by
\begin{align}
\dx r(t) = k ( \theta - r(t) ) \dx t + \sigma \dx W(t) ,
\end{align}
where $k, \theta, \sigma > 0$ and $r(0) = r_0$ are constants. Thus
	\begin{align}
		\mu(t,r(t)) &= k ( \theta - r(t) ) \\
		\sigma(r,t(t)) &= \sigma .
	\end{align}
Equivalent parametrization is
\begin{align}
\dx r(t) = ( \theta^* - k r(t) ) \dx t + \sigma \dx W(t) ,
\end{align}
where $\theta^* = \theta k$. We could also write
	\begin{align}
		r(t) &= \theta + Y(t) \\
		\dx Y(t) &= - k Y(t) \dx t + \sigma \dx W(t).
	\end{align}
Now $r(t) = g(t,Y(t))$, where $g(t,y) = m + y$ actually and It\^{o}'s lemma gives
	\begin{align}
		\dx r(t, Y(t)) &= - k Y(t) \dx t + \sigma \dx W(t) \\
			&= k ( \theta - r(t) ) \dx t + \sigma \dx W(t) .
	\end{align}

We consider a process $X(t) = \int_0^t \e^{k s} \dx W(s)$ so that $\dx X(t) = \e^{k t} \dx W(t)$. Now we define the function
\begin{align}
g(x,t) &= r_0 \e^{-k t} + \theta (1 - \e^{-k t}) + \sigma \e^{-k t} x \\
	&= \theta + \e^{-k t} \left( r_0 - \theta + \sigma x \right)
\end{align}
and
\begin{align}
\frac{ \partial g(x,t)}{ \partial x} &= \sigma \e^{-k t}, \\
\frac{ \partial^2 g(x,t)}{ \partial x^2} &= 0, \\
\frac{ \partial g(x,t)}{ \partial t} &= - k \e^{- k t} (r_0 - \theta +\sigma x ) \\
&= k (\theta -g(x,t) ).
\end{align}
Since the drift term for $X$ is zero and diffusion factor is $\e^{k t}$, It\^{o}'s lemma for the process $X(t)$ yields
\begin{align}
\dx g (X(t),t) &= \frac{ \partial g(X(t),t) }{ \partial t} \dx t + \e^{k t} \frac{ \partial g(X(t),t)}{ \partial x} \dx W \\
&= k (\theta -g(X(t),t) ) \dx t + \sigma W(t) .
\end{align}
As $X(0)=0$, we have that $g(X(0),0) = r_0$. Thus
\begin{align}
\label{vasiceksolution}
r(t) = g(X(t),t) = r_0 \e^{-k t} + \theta (1 - \e^{-k t}) + \sigma \e^{-k t} \int_0^t \e^{k s} \dx W(s) .
\end{align}
By Theorem \ref{gaussiancalculation}, the expected value of the integral in the equation \ref{vasiceksolution} is zero and it has variance of
\begin{align}
\int_0^t \e^{2 k s} \dx s = \frac{1}{2 k} (\e^{2 k t} -1).
\end{align}
Hence
\begin{align}
r(t) \sim N \left( r_0 \e^{-k t} + \theta (1 - \e^{-k t}), \frac{\sigma^2}{2k } (1-\e^{2k t}) \right) .
\end{align}
Since $r(t)$ is normally distributed in the Va\v{s}\'{i}\v{c}ek--model, there is a positive change that short-rate will be negative in a given time frame. If $t \rightarrow \infty$, then
\begin{align}
\E (r(t)) &\rightarrow \theta , \\
\Var ( r(t) ) &\rightarrow \frac{\sigma^2}{2k} .
\end{align}
We see that the parameter $\theta$ can be seen as the long-term mean and the short-rate has a tendency to move toward it. The parameter $k$ signifies the speed of this mean-reversion while $\sigma$ controls the volatility.

One of the features of the Va\v{s}\'{i}\v{c}ek--model is that there is a non-zero probability for negative rates. Earlier this was seen as a major drawback of the model.

\subsubsection{Bond pricing in the Va\v{s}\'{i}\v{c}ek--model}

If we assume that the short-rate process $r(t)$ is given by
\begin{align}
\dx r(t) = k ( \theta - r(t) ) \dx t + \sigma \dx W(t)^*
\end{align}
under the risk neutral measure. Now $\mu(t) = k \theta - k r(t)$ and $\sigma(t) = \sigma$ are affine in $r(t)$. By Theorem \ref{affineconfition}, Va\v{s}\'{i}\v{c}ek-model has affine term-structure and
\begin{align}
\label{vasicekaffine}
\begin{cases}
\frac{\partial B}{\partial t} = k B - 1 \\
\frac{\partial A}{\partial t} = k \theta B - \frac{1}{2} \sigma^2 B^2 \\
A(T,T) = B(T,T) = 0,
\end{cases}
\end{align}
Now
\begin{align}
B(t,T) = \frac{1}{k} \left( 1 - \e^{-k (T-t)} \right)
\end{align}
satisfies \ref{vasicekaffine} and therefore we might solve $A(t,T)$ by calculation the integral
\begin{align}
A(t,T) = A(t,T) - A(T,T) = - \int_t^T A(s,T) \dx s.
\end{align}
We note that $B^2 = \frac{B}{k} \left( \frac{\partial B}{\partial t} + 1 \right)$ and hence
\begin{align}
k \theta B - \frac{1}{2} \sigma^2 B^2 &= k \theta B - \frac{\sigma^2}{2k} B(1+\frac{\partial B}{\partial t}) \\
&= \frac{k^2 \theta - \frac{1}{2}\sigma^2}{k} B  - \frac{\sigma^2}{2k} \frac{\partial B}{\partial t} B \\
&= \frac{k^2 \theta - \frac{1}{2}\sigma^2}{k^2} (\frac{\partial B}{\partial t} + 1) - \frac{\sigma^2}{2k} B\frac{\partial B}{\partial t} .
\end{align}
Now the conditions in Equation \ref{vasicekaffine} will be satisfied by
\begin{align}
A(t,T) = \frac{k^2 \theta - \frac{1}{2}\sigma^2}{k^2} (B(t,T) - (T - t)) - \frac{\sigma^2}{4 k} B^2(t,T) .
\end{align}
Therefore Va\v{s}\'{i}\v{c}ek-model has term-structure defined by
\begin{align}
P(t,T) = \e^{A(t,T) - B(t,T)r(t)},
\end{align}
where
\begin{align}
B(t,T) &= \frac{1}{ k} \left( 1 - \e^{-k (T-t)} \right), \\
A(t,T) &= \frac{k^2 \theta - \frac{1}{2}\sigma^2}{k^2} (B(t,T) - (T - t)) - \frac{\sigma^2}{4k} B^2(t,T).
\end{align}

\subsubsection{Option pricing in the Va\v{s}\'{i}\v{c}ek--model}

Since the short-rate follows a Gaussian distribution, the price of a option on a zero-coupon bond can be calculated explicitly. We shall not do that. A European call option with maturity $S$ on a $T$-bond and exercise price $K$ has a price
	\begin{align}
		\ZBC(t, S, T, K) = \Bond(t,T) N(d_1) - K \Bond(t,S) N(d_2),
	\end{align}
at the time $t$, where
	\begin{align}
		d_1 &= \frac{ \log \frac{\Bond(t,T)}{K \Bond(t,S)} + \frac{V}{2} }{ \sqrt{V} } \\
		d_2 &= \frac{ \log \frac{\Bond(t,T)}{K \Bond(t,S)} - \frac{V}{2} }{ \sqrt{V} } \\
		V &= \sigma^2 \left( \frac{1 - \e^{-2k(T-S)}}{k} \right)^2 \frac{1 - \e^{-2k(S-t)}}{2k} .
	\end{align}
A European put option with maturity $S$ on a $T$-bond and exercise price $K$ has a price
\begin{align}
\ZBP(t, S, T, K) = K \Bond(t,S) N( - d_2) - \Bond(t,T) N(-d_1)
\end{align}
at the time $t$

\subsection{Cox-Ingersol-Ross--model (CIR)}
\label{sec:cir}

The material from this section is mainly from \textcite[pp. 64--68]{brigo2007interest}.

\textcite{coxingersollross1985theory} introduced the Cox-Ingersol-Ross--model (CIR) where the short-rate process $r_t$ is given by
\begin{align}
\dx r(t) = k( \theta - r(t) ) \dx t + \sigma \sqrt{r(t)} \dx W(t) ,
\end{align}
where $r_0, k, \theta$ and $\sigma$ are positive constants. Another widely used parametrization is
\begin{align}
\dx r(t) = ( \alpha - k r(t) ) \dx t + \sigma \sqrt{r(t)} \dx W(t) ,
\end{align}
where $\alpha = k \theta$. Like Va\v{s}\'{i}\v{c}ek--model, CIR features reversion toward the mean $\theta$ with $k$ as the strength of the reversion. But it also has non-constant volatility as the diffusion term is $\sigma \sqrt{r(t)}$. CIR model also has affine term-structure and therefore the bond prices can be efficiently solved. Unlike Va\v{s}\'{i}\v{c}ek--model, CIR--model can be specified so that the short-rate will be always positive.

\subsubsection{Bond pricing in the CIR--model}

By solving the Riccati equation, which we shall not do, we get that the bond price in CIR model is
	\begin{align}
		\Bond(t,T) = \e^{A(t,T) - B(t,T)r(t)} ,
	\end{align}
where
	\begin{align}
		A(t,T) &= \frac{2k\theta}{\sigma^2} \log \left( \frac{2 \beta \e^{ \frac{(\beta + k)(T-t)}{2} } }{(\beta + k)(\e^{\beta(T-t)} - 1) + 2\beta}  \right) \\
		B(t,T) &= \frac{ 2(\e^{\beta(T-t)} - 1) }{ (\beta + k)(\e^{\beta(T-t)} - 1) + 2\beta } \\
		\beta &= \sqrt{k^2+ 2\sigma^2}
	\end{align}

\subsubsection{Option pricing in the CIR--model}

A European call option with maturity $S$ on a $T$-bond and exercise price $K$ has a price
\begin{align}
\ZBC(t, S, T, K) = \Bond(t,T) \chi_1^2 - K \Bond(t,S) \chi_2^2,
\end{align}
at the time $t$, where
\begin{align}
	\chi_1^2 &= \chi^2 \left( v_1, \frac{4k\theta}{\sigma^2}, \frac{2\beta_3^2 r(t) \e^{\beta (S-t)} }{\beta_2 + \beta_3 + B(S,T)} \right) \\
	\chi_2^2 &= \chi^2 \left( v_2, \frac{4k\theta}{\sigma^2}, \frac{2\beta_3^2 r(t) \e^{\beta (S-t)} }{\beta_2 + \beta_3} \right) \\
	v_1 &= 2( \beta_2 + \beta_3 + B(S,T) ) \frac{A(S,T)-\log(K)}{B(S,T)} \\
	v_2 &= 2( \beta_2 + \beta_3 ) \frac{A(S,T)-\log(K)}{B(S,T)} \\
	\beta_2 &= \frac{k+\beta}{\sigma^2}\\
	\beta_3 &= \frac{2\beta}{\sigma^2 (\e^{\beta(S-t)} - 1) }
\end{align}
and $\chi^2(v,a,b)$ is the cumulative non-central chi-squared distribution with $A$ degrees of freedom and non-centrality parameter $b$.

\iffalse

\subsubsection{Glasserman algorithm}

Glasserman gives the following algorithm to generate a sampling of a path in a CIR-model on time grid $0 = t_0 < t_1 < \ldots < t_n$. We denote $d = \frac{k \theta}{\sigma^2}$.

\begin{description}
	\item[Case $d>1$]: For $i=0,1, \ldots, n$:
	\begin{enumerate}
		\item Set
		\begin{align}
		c &= \sigma^2 \frac{ 1 - \exp^{ - k ( t_{i+1} - t_i ) } }{4 k}, \\
		\lambda &= r(t_i) \frac{ \exp^{ - k ( t_{i+1} - t_i ) } }{ c }.
		\end{align}
		\item Generate
		\begin{align}
		Z &\sim N(0,1), \\
		X &\sim \chi_{d-1}^2.
		\end{align}
		\item Set
		\begin{align}
		r(t_{i+1}) = \left( (Z + \sqrt{\lambda})^2 + X \right) .
		\end{align}
	\end{enumerate}
	\item[Case $d \leq 1$]: For $i=0,1, \ldots, n$:
	\begin{enumerate}
		\item Set
		\begin{align}
		c &= \sigma^2 \frac{ 1 - \exp^{ - k ( t_{i+1} - t_i ) } }{4 k}, \\
		\lambda &= r(t_i) \frac{ \exp^{ - k ( t_{i+1} - t_i ) } }{ c }.
		\end{align}
		\item Generate
		\begin{align}
		Z &\sim \text{Poisson} ( \frac{\lambda}{2} ), \\
		X &\sim \chi_{d+2Z}^2.
		\end{align}
		\item Set
		\begin{align}
		r(t_{i+1}) = cX .
		\end{align}
	\end{enumerate}
\end{description}

\fi

\section{Multi-factor short-rate models}

Multi-factor short-rate models have more than one state variables that drive the evolution of the short-rate. \textcite{litterman1991common} demonstrated that while the majority of the yield curve movements can be explained by a single factor, it can not explain it all. Usually it is considered that at least 3 factors are needed.

One problem with one-factor affine one-factor term-structures models is that rates of different maturities are perfectly correlated. By Equation \ref{affinitytermstructureequation},
\begin{align}
\Bond(t,T) = \e^{A(t,T) - B(t,T)r(t)}
\end{align}
and the continuously compounded rate satisfies $\e^{R(t,T)(T-t)} \Bond(t,T) = 1$, we see that
\begin{align}
	R(t,T) = \frac{B(t,T)}{T-t} r(t) - \frac{A(t,T)}{T-t} .
\end{align}
This implies that rates are perfectly correlated. Thus multiple factors are needed to induce realistic correlations among the rates of different maturities.

In this section we shall follow the presentation based on \textcite[pp. 425--435]{nawalkabeliaevasoto2007dynamic}.

\subsection{Simple $A(M,N)$--models}
\label{subsec-AMN-interestrate}

We now define a class of models $A(M,N)$ with $N-M$ correlated Gaussian processes and $M$ uncorrelated square-root processes. The correlated gaussian processes are
\begin{align}
\dx Y_i(t) &= - k_i Y_i(t) \dx t + \nu_i \dx W_i (t),
\end{align}
where $W_i$ is a Wiener process and
\begin{align}
\dx W_i (t) \dx W_j (t) &= \rho_{ij} \dx t
\end{align}
for all $i,j = 1,2, \ldots, N-M$. Here $-1 < \rho_{ij} = \rho_{ji} < 1$ and $\rho_{ii} = 1$. The $M$ square-root processes are
\begin{align}
\dx X_m(t) = \alpha_m ( \theta_m - X_m(t) ) \dx t + \sigma_m \sqrt{ X_m(t) } \dx Z_m (t)
\end{align}
where $Z_m$ are independent Wiener process and
\begin{align}
\dx W_i (t) \dx Z_m (t) &= 0
\end{align}
for all $i = 1,2, \ldots, N-M$ and $m = 1,2, \ldots, M$. The short-rate is defined by
\begin{align}
r(t) = \delta + \sum_{m=1}^{M} X_m(t) + \sum_{i=1}^{N-M} Y_i(t),
\end{align}
where $\delta$ is a constant. Thus
\begin{align}
\dx r(t) &= \left( \sum_{m=1}^{M} \alpha_m ( \theta_m - X_m(t) ) - \sum_{i=1}^{N-M} k_i Y_i(t) \right) \dx t \\
&+ \sum_{m=1}^{M} \sigma_m \sqrt{ X_m(t) } \dx Z_m (t) + \sum_{i=1}^{N-M} \nu_i \dx W_i (t) .
\end{align}

If we define
\begin{align}
H(t,T) &= \int_t^T \delta \dx x = (T-t) \delta, \\
\beta_m &= \sqrt{\alpha_m^2 + 2 \sigma_m^2} , \\
C_i(x) &= \frac{1 - \e^{-k_i x}}{k_i}, \\
B_m(x) &= \frac{ 2 ( \e^{\beta_m x} - 1 ) }{ (\beta_m + \alpha_m)( \e^{\beta_m x} - 1) + 2 \beta_m }, \\
A(x) &= \sum_{m=1}^M \frac{2 \alpha_m \theta_m }{\sigma_m^2} \log \left( \frac{ 2 \beta_m \e^{ \frac{1}{2} (\beta_m + \alpha_m) x } }{ (\beta_m + \alpha_m)( \e^{\beta_m x -1} ) + 2 \beta_m } \right) \\
&+ \frac{1}{2} \sum_{i=1}^{N-M} \sum_{j=1}^{N-M} \frac{\nu_i \nu_j \rho_{ij}}{k_i k_j} \left( x - C_i(x) - C_j(x) + \frac{1-\e^{(k_i+k_j)x}}{k_i + k_j} \right)
\end{align}
for all $i = 1,2, \ldots, N-M$ and $m=1,2, \ldots, M$, then the price of a zero coupon bond is given by
\begin{align}
\Bond (t,T) = \exp{ \left( A(\tau) - \sum_{m=1}^M B_m(\tau) X_m(t) - \sum_{i=1}^{N-M} C_i(\tau) Y_i(t) - H(t,T) \right) },
\end{align}
where $\tau = T-t$. This model also has a semi-explicit formula for options on zero-coupon bonds. The method to calculate this will introduced in Section \ref{chap:fourier}.

We note that  Va\v{s}\'{i}\v{c}ek--model is $A(0,1)$ and CIR--model is $A(1,1)$ in this notation.

These $A(M,N)$--models can be made into $A(M,N)++$ models by using the following dynamic extension.

\section{Dynamic extension to match the given term-structure}
\label{sec:dynamicextension}

This section follows the paper by \textcite{brigomercurio2001deterministic}.

Let $(\Omega^x, \Pm^x, \F^x)$ be a probability space. We first assume the process $(x_{\alpha}(t))$ follows
	\begin{align}
		\dx x_{\alpha}(t) &= \mu(x_{\alpha}(t); \alpha) \dx t + \sigma(x_{\alpha}(t)); \alpha) \dx W_x(t) , \\
		x_{\alpha}(0) &= x_0
	\end{align}
under the measure $\Pm^x$, where $\alpha$ is a parameter vector. Let $\F_t^x$ be the member of a filtration generated by $x_{\alpha}$ up to time $t$. Suppose that the process $(x_{\alpha}(t))$ is the short-rate process under the risk-free measure $\Pm_x$ and the price of a zero-coupon bond is
	\begin{align}
		\Bond^x(t,T) &= \E_{\Pm_x} \left( \e^{ - \int_t^T x_{\alpha}(s) \ \dx s} \ | \ \F_t^{x_{\alpha}} \right) ,
	\end{align}
which is a function of $(t,T,x_{\alpha}, \alpha)$. \textcite{brigomercurio2001deterministic} calls this as a reference model. It is not guaranteed that the implied zero-curve structure by this model will match the observed market data.

Let $\varphi(t; \alpha, x_0) = \varphi(t; \alpha^*)$ be a deterministic real-valued function that it is at least integrable under any closed interval. Suppose that the short-rate follows
	\begin{align}
		r(t) = x(t) + \varphi(t; \alpha^*)
	\end{align}
and $(x(t))$ follows that same process under the risk-free measure $\Pm_0$ as $(x_{\alpha}(t))$ does under the measure $\Pm^x$. This model is the shifted model. This implies that
	\begin{align}
		\Bond(t,T) &= \E_{\Pm_0} \left( \e^{ - \int_t^T r(s) \ \dx s} \ | \ \F_t \right) \\
			&= \E_{\Pm_0} \left( \e^{ - \int_t^T (x(s) + \varphi(s; \alpha^*)) \ \dx s} \ | \ \F_t \right) \\
			&=  \E_{\Pm_0} \left( \e^{ - \int_t^T x(s)  \ \dx s} \ | \ \F_t \right) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s} \\
			&=  \E_{\Pm^x} \left( \e^{ - \int_t^T x_{\alpha}(s)  \ \dx s} \ | \ \F_t^{x_{\alpha}} \right) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s} \\
			&= \Bond^x(t,T) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s} .
	\end{align}
If we have a method to calculate the bond price under the reference model, then bond prices under the shifted can be calculated by a making a deterministic discounting based on the function $\varphi(t, \alpha^*)$.

In order to shorten the notations, we denote
	\begin{align}
		I(t,T,f) = \e^{ \int_t^T f(s) \ \dx s } ,
	\end{align}
where $f$ is a real valued function. If there is also a function
\begin{align}
\ZBC^x (t,S,T,K) = \E_{\Pm_x} \left( \e^{ - \int_t^S x_{\alpha}(s) \ \dx s} \left( \Bond^x(t,T) - K \right)^+ \ | \ \F_t^{x_{\alpha}} \right)
\end{align}
for the price at time $t$ of a call option maturing at $S$ for a $T$-bond under the reference model. Now the price under the shifted model is
	\begin{align}
	&\ZBC(t,S,T,K) \\ = &\E_{\Pm_0} \left( \e^{ - \int_t^S r(s) \ \dx s} \left( \Bond(S,T) - K \right)^+ \ | \ \F_t \right) \\
		= &\e^{ - \int_t^S \varphi(s; \alpha^*) \ \dx s} \E_{\Pm_0} \left( \e^{ - \int_t^S x(s) \ \dx s} \left( \Bond(S,T) - K \right)^+ \ | \ \F_t \right) \\
		= &I(t,S, -\varphi) \E_{\Pm_0} \left( I(t,S,-x) \left( \Bond(S,T) - K \right)^+ \ | \ \F_t \right)
	\end{align}
As
	\begin{align}
		&\E_{\Pm_0} \left( I(t,S,-x) \left( \Bond(S,T) - K \right)^+ \ | \ \F_t \right) \\
		= &\E_{\Pm_0} \left( I(t,S,-x) \left( \Bond^x(S,T) I(S,T,-\varphi) - K \right)^+ \ | \ \F_t \right) \\
		= &\E_{\Pm_0} \left( I(t,S,-x) \left( \Bond^x(S,T) - I(S,T,\varphi) K \right)^+ \ | \ \F_t \right) I(S,T,-\varphi) \\
		= &\ZBC^x(t,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_S^T \varphi(s; \alpha^*) \ \dx s} .
	\end{align}
Here we have used again the equivalence on processes $x(t)$ and $x_{\alpha(t)}$. Hence
	\begin{align}
		&\ZBC(t,S,T,K) \\ = &\e^{ - \int_t^S \varphi(s; \alpha^*) \ \dx s} \ZBC^x(t,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_S^T \varphi(s; \alpha) \ \dx s} \\
		= & \ZBC^x(t,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s}
	\end{align}
We see that the options prices for the shifted model can be computed easily, if the options prices can be calculated efficiently under the reference model. But not pure discounting is not enough, as we also have to shift probabilities by shifting the target strike.

Thus the following holds.

\begin{thm}
\label{bondandoptionpricesinextension}
	Under the earlier assumptions, the $T$-bond has a price
	\begin{align}
	\Bond(t,T) =  \Bond^x(t,T) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s}
	\end{align}
	and a call option with maturity $S$ on this bond has price
	\begin{align}
	\ZBC(t,S,T,K) =  \ZBC^x(t,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_t^S \varphi(s; \alpha^*) \ \dx s}
	\end{align}
	at the time $t$.
\end{thm}

This model can achieve a perfect fit to the initial interest-rate term structure.

\begin{thm}
\label{extensionequivalentandfactor}
	Under the earlier assumptions, the following are equivalent:
\begin{itemize}
	\item The model
		\begin{align}
			r(t) = x(t) + \varphi(t; \alpha^*)
		\end{align}
	has a perfect fit to the given interest-rate term structure,
	\item for all $t \geq 0$
		\begin{align}
			\e^{-\int_t^T \varphi(s; \alpha^*) \dx s} = \frac{\Bond^M(0,T) \Bond^x(0,t)}{\Bond^M(0,t) \Bond^x(0,T)},
		\end{align}
	\item for all $t \geq 0$
	\begin{align}
		\varphi( t, \alpha^*) = \Forwardrate^M(0,t) - \Forwardrate^x(0,t)
	\end{align}
\end{itemize}
where $\Bond^M$ are observed market prices of zero-coupon bonds, $\Forwardrate^M(0,t)$ is the market implied forward-rate and $\Forwardrate^x(0,t)$ is the forward-rate implied by the reference model.
\end{thm}

\begin{proof}
We note that the perfect fit to market rates is equivalent to
	\begin{align}
		\label{perfectfitequationindynamicextension}
		\Bond^M(0,t) = \Bond(0,t) = \e^{ - \int_0^t \varphi(s; \alpha^*) \ \dx s} \Bond^x(0,t) .
	\end{align}
That is equivalent to
	\begin{align}
\e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s} &= \e^{ - \int_0^T \varphi(s; \alpha^*) \ \dx s} \e^{ \int_0^t \varphi(s; \alpha^*) \ \dx s} \\
	&= \frac{\Bond^M(0,T) }{ \Bond^x(0,T)} \frac{ \Bond^x(0,t)}{\Bond^M(0,t) }
\end{align}
Equation \ref{perfectfitequationindynamicextension} is also equivalent to
	\begin{align}
		\log \Bond^M(0,t) = \log \Bond^x(0,t) - \int_0^t \varphi(s; \alpha^*) \ \dx s
	\end{align}
for all $t \geq 0$ and this implies that the $-f^M(0,t) = - f^x(0,t) -  \varphi(t; \alpha^*)$.
\end{proof}

Theorem \ref{extensionequivalentandfactor} guarantees that no matter how the term-structure is shaped, we can fit it exactly with a suitable function. If we want to price bonds and options on bonds in the shifted model, then the calculation of the whole function $\varphi$ is unnecessary as we need only the values
		\begin{align}
\e^{-\int_t^T \varphi(s; \alpha^*) \dx s} = \frac{\Bond^M(0,T) \Bond^x(0,t)}{\Bond^M(0,t) \Bond^x(0,T)} .
\end{align}
Using the market values is preferable to estimating the forward curve, as the curve fitting may cause errors.

We note that if $\varphi(t) = \varphi(t; \alpha^*)$ is differentiable, then
	\begin{align}
		\dx r(t) &= \dx x(t) + \frac{\partial}{\partial t} \varphi(t; \alpha, x_0) \dx t \\
			&= \mu(x_{\alpha}(t); \alpha) \dx t + \sigma(x_{\alpha}(t)); \alpha) \dx W_x(t) + \frac{\partial}{\partial t} \varphi(t) \dx t \\
			&= \left( \mu(r(t) - \varphi(t); \alpha) + \frac{\partial}{\partial t} \varphi(t) \right) \dx t + \sigma(r(t) - \varphi(t)); \alpha) \dx W_x(t) .
	\end{align}

We also note that if $t=0$, then
	\begin{align}
		\Bond(0,T) &= \Bond^M(0,T) \\ &=  \Bond^x(0,T) \e^{ - \int_0^T \varphi(s; \alpha^*) \ \dx s}
	\end{align}
and
	\begin{align}
		\ZBC(0,S,T,K) &=  \ZBC^x(0,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_0^S \varphi(s; \alpha^*) \ \dx s}
	\end{align}

\subsection{Va\v{s}\'{i}\v{c}ek++--model}
\label{sex:vasicek++}

By dynamically extending Va\v{s}\'{i}\v{c}ek--model, we get a model that is equivalent to a variant of Hull-White--model (\cite{hull1990pricing}, \cite{brigomercurio2001deterministic}). \added{This section follows } \textcite[pp. 100--102]{brigo2007interest}.

Suppose that we bootstrap the prices of $T_i$-bonds from the market for some $i=1,2, \ldots, n$. Let them be $\Bond^M(0,T_1), \Bond^M(0,T_2), \ldots , \Bond^M(0,T_n)$.

We assume that the evolution of $x(t)$ is given by
\begin{align}
\dx x(t) = k ( \theta - x(t) ) \dx t + \sigma \dx W(t) ,
\end{align}
where $k, \theta, \sigma > 0$ and $x(0) = x_0$ are constants and $W(t)$ is a brownian motion under the risk-free measure. We make the explicit assumption that $\theta = 0$, hence
\begin{align}
\dx x(t) = - k x(t) ) \dx t + \sigma \dx W(t) .
\end{align}
This assumption does not actually restrict the model at all. It only changes the center of the distribution, which does not matter as dynamic shift will be shifted similarly.

Now we know that
\begin{align}
\Bond^x(t,T) = \e^{- B(t,T)x(t)} ,
\end{align}
where
\begin{align}
B(t,T) &= \frac{ 2(\e^{\beta(T-t)} - 1) }{ (\beta + k)(\e^{\beta(T-t)} - 1) + 2\beta }, \\
\beta &= \sqrt{k^2+ 2\sigma^2}	.
\end{align}
We now define the short-rate by
	\begin{align}
		r(t) = x(t) + \varphi(t),
	\end{align}
where the function $\varphi$ is taken as in the Theorem \ref{extensionequivalentandfactor}. Now
	\begin{align}
		\dx r(t) &= \left( -kx(t) + \frac{\partial}{\partial t} \varphi(t) \right) \dx t + \sigma \dx W(t) \\
			&= \left( k( \varphi(t) -r(t) ) + \frac{\partial}{\partial t} \varphi(t) \right) \dx t + \sigma \dx W(t) .
	\end{align}

By Theorems \ref{bondandoptionpricesinextension} and \ref{extensionequivalentandfactor}, we have that
	\begin{align}
		\Bond(t,T) &= \Bond^x(t,T) \e^{ - \int_t^T \varphi(s; \alpha^*) \ \dx s} \\
			&= \Bond^x(t,T) \frac{\Bond^M(0,T) \Bond^x(0,t)}{\Bond^M(0,t \Bond^x(0,T)} \\
			&= \frac{\Bond^M(0,T) }{\Bond^M(0,t)} \e^{ -B(t,T)x(t) -B(0,t)x(0) + B(0,T)x(0) }
	\end{align}
if $t = T_i$ and $T = T_j$ for some $i < j$. If $t=0$, then $\Bond(0,T) = \Bond^M(0,T)$, as was expected.

A European call option with maturity $S$ on a $T$-bond and exercise price $K$ has a price
\begin{align}
\ZBC^x(t, S, T, K) = \Bond^x(t,T) N(d_1) - K \Bond^x(t,S) N(d_2),
\end{align}
at the time $t$ under the dynamics of $x(t)$, where
\begin{align}
d_1 &= \frac{ \log \frac{\Bond^x(t,T)}{K \Bond^x(t,S)} + \frac{V}{2} }{ \sqrt{V} } \\
d_2 &= \frac{ \log \frac{\Bond^x(t,T)}{K \Bond^x(t,S)} - \frac{V}{2} }{ \sqrt{V} } \\
V &= \sigma^2 \left( \frac{1 - \e^{-2k(T-S)}}{k} \right)^2 \frac{1 - \e^{-2k(S-t)}}{2k} .
\end{align}
Now, by Theorems \ref{bondandoptionpricesinextension} and \ref{extensionequivalentandfactor}, we have that
	\begin{align}
	\ZBC(t,S,T,K) &=  \ZBC^x(t,S,T,K\e^{ \int_S^T \varphi(s; \alpha^*) \ \dx s}) \e^{ - \int_t^S \varphi(s; \alpha^*) \ \dx s} \\
		&= \ZBC^x(t,S,T,K/I(S,T)) I(t,S),
	\end{align}
where
	\begin{align}
		I(t,S) &= \frac{\Bond^M(0,S) \Bond^x(0,t)}{\Bond^M(0,t) \Bond^x(0,S)} \\
				&= \frac{\Bond^M(0,S) }{\Bond^M(0,t)} \e^{ -B(0,t)x(0) + B(0,S)x(0) } \end{align}
and
	\begin{align}
		I(S,T) &= \frac{\Bond^M(0,T) \Bond^x(0,S)}{\Bond^M(0,S) \Bond^x(0,T)} \\
			&= \frac{\Bond^M(0,T) }{\Bond^M(0,S)} \e^{ -B(0,S)x(0) + B(0,T)x(0) } .
	\end{align}

\subsection{CIR++--model}

Extended CIR--model, or CIR++--model, can be constructed by combining the formulas in section \ref{sec:cir} with the results from section \ref{sec:dynamicextension} as was done in section \ref{sex:vasicek++}. As this is trivial, we shall writing the formulas again.

\subsection{G2++--model}

$A(2,2)++$--model is achieved by extending $A(2,2)$--model as above. Thus model is equivalent to model by \textcite{hull1994numerical} and it is called as  $G2++$--model by \textcite{brigo2007interest}.

\textcite[pp. 153--156, 172--173]{brigo2007interest} contains the proof for the following.

\begin{thm}
A European call option with maturity $S$ on a $T$-bond and exercise price $K$ has a price
\begin{align}
\ZBC^x(t, S, T, K) &= \Bond(t, T) \Phi \left( K^* + \frac{1}{2} S(t,S,T) \right) \\
&- \Bond(t, S) K \Phi \left( K^* - \frac{1}{2} S(t,S,T) \right)
\end{align}
where
\begin{align}
K^* & = \frac{\log \frac{\Bond(t,T)}{K\Bond(t,S)}}{S(t,S,T)} \\
S(t,S,T)^2 &= \sum_{i=1}^2 \frac{\nu_i^2}{2 k_i^3} \left( 1 - \e^{ -k_i(T-S) } \right)^2 \left( 1 - \e^{-2k_i(S-t)} \right) \\
&+ 2 \rho \frac{\nu_1 \nu_2}{ k_1 k_2 (k_1 + k_2) }  \left( 1 - \e^{ -k_1(T-S) } \right)  \left( 1 - \e^{ -k_2(T-S) } \right)  \left( 1 - \e^{ -(k_1 + k_2)(S-t) } \right)
\end{align}
\end{thm}


\subsection{Horizontal Asymptotes}\label{subsec:HorizontalAsymptotes}
The line $y=L$ is a \dfont{horizontal asymptote} of $f(x)$ if either
$$\lim_{x\to\infty}f(x)=L\qquad\mbox{or}\qquad\lim_{x\to-\infty}f(x)=L.$$

\begin{example}{Horizontal Asymptotes}{HorizontalAsymptotes}
Find the horizontal asymptotes of $\ds f(x)=\frac{|x|}{x}$.
\end{example}

\begin{solution}
We must compute two infinite limits.
First,
$$\lim_{x\to\infty}\frac{|x|}{x}.$$
Notice that for $x$ arbitrarily large that $x>0$, so that $|x|=x$.
In particular, for $x$ in the interval $(0,\infty)$ we have
$$\lim_{x\to\infty}\frac{|x|}{x}=\lim_{x\to\infty}\frac{x}{x}=1.$$
Second, we must compute
$$\lim_{x\to-\infty}\frac{|x|}{x}.$$
Notice that for $x$ arbitrarily large negative that $x<0$, so that $|x|=-x$.
In particular, for $x$ in the interval $(-\infty,0)$ we have
$$\lim_{x\to -\infty}\frac{|x|}{x}=\lim_{x\to -\infty}\frac{-x}{x}=-1.$$
Therefore there are two horizontal asymptotes, namely, $y=1$ and $y=-1$.
\end{solution}


\section{Pi Digits Computation}
\paragraph{Goals and algorithms.}
The goal of these program implementations were to compute the number pi using
Riemann sums. In other words combining the area under the curve split into small
rectangular intervals. For
parallelization, the strategy used was to evenly distribute the
number interval areas to be calculated among the threads created by the
program, and then join the results of each thread at the end to form the final
answer for pi. To allow for a substantial enough runtime to measure, the
programs were run using an absurdly high number of intervals.

\paragraph{Pi Implemented in C++.}
The C++ implementation used the POSIX pthread library, and creates $n$
threads, as the user specifies, to compute the area under the curve. In order
to isolate the performance of the thread creation, deletion, and parallelization
of pthreads, the timer used for measuring the program's runtime is started after
all initialization of variables occurs, and just before creation of the child
threads.

Once all
threads are created, the main thread waits idle while trying to join all child
threads. As each child thread terminates, the final sum of pi is added to. Once
all threads are finished, the final result is calculated and the timer stops.

\paragraph{Pi Implemented in Rust.}
The Rust implementation, like the C++ implementation, takes $n$ threads, as the
user specifies, to compute the area under the curve. Another similarity the Rust
version shares with its C++ counterpart is that it's timer begins after the all
initialization of variables, and just before creation of the child threads.

Once each thread finishes its work, it sends it's solution to a channel located
on the main thread where it is added to a global sum. Once all threads are
finished, the final sum is calculated and the timer stops.

\paragraph{User Experience writing pi in C++.}
Overall this program was trivial to implement in C++, as the problem is not very
complex, and room for memory errors to appear is narrow. There were no recorded
memory errors encountered during the writing of the C++ program, and no major
usability faults regarding pthreads, with one exception: Passing arguments to
a child thread is certainly type unsafe, and memory unsafe, and could be
a potential culprit for many runtime errors if not carefully managed. This is
due to C++ pthreads requiring a single {\cf void *} for all arguments, which
guarantees almost nothing about the data being passed into the thread, and
cannot allow for the compiler to check for safety. In the case of Pi, this was
not a major issue. Only a single integer value was to be passed into each
thread upon its creation, and so no risk for accessing out-of-bounds memory was
present.

\paragraph{User Experience writing pi in Rust.}
Writing this program felt much simpler and more inutitve than C++ with pthreads.
Since channels make asynchronous sums very trivial, the hard part was pretty
much already solved from the begining. The implementation looks nearly identical
to what a single threaded pi solver implementation would look like, except for
the addition of a thread spawn occuring in the main loop and a channel being
used to collect the sums. Even an individual with little knowledge of the
language could easily figure out whats going on, a feature that isn't shared
with the C++ implementation.