\documentclass[letterpaper,final,12pt,reqno]{amsart}

\usepackage[total={6.0in,8.8in},top=1.3in,left=1.25in]{geometry}

\usepackage{times,bm,bbm,empheq,verbatim,fancyvrb,graphicx}
\usepackage[dvipsnames]{xcolor}

\usepackage[kw]{pseudo}

\pseudoset{left-margin=15mm,topsep=5mm,idfont=\texttt}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

% hyperref should be the last package we load
\usepackage[pdftex,
colorlinks=true,
plainpages=false, % only if colorlinks=true
linkcolor=blue,   % ...
citecolor=Red,    % ...
urlcolor=black    % ...
]{hyperref}

\DefineVerbatimEnvironment{cline}{Verbatim}{fontsize=\small,xleftmargin=5mm}

\renewcommand{\baselinestretch}{1.05}

\newtheorem{lemma}{Lemma}
\newtheorem*{example}{Example}

\newcommand{\Matlab}{\textsc{Matlab}\xspace}
\newcommand{\eps}{\epsilon}
\newcommand{\lam}{\lambda}
\newcommand{\RR}{\mathbb{R}}

\newcommand{\grad}{\nabla}
\newcommand{\Div}{\nabla\cdot}
\newcommand{\trace}{\operatorname{tr}}

\newcommand{\hbn}{\hat{\mathbf{n}}}

\newcommand{\bb}{\mathbf{b}}
\newcommand{\be}{\mathbf{e}}
\newcommand{\bbf}{\mathbf{f}}
\newcommand{\bg}{\mathbf{g}}
\newcommand{\bn}{\mathbf{n}}
\newcommand{\bq}{\mathbf{q}}
\newcommand{\br}{\mathbf{r}}
\newcommand{\bs}{\mathbf{s}}
\newcommand{\bt}{\mathbf{t}}
\newcommand{\bu}{\mathbf{u}}
\newcommand{\bv}{\mathbf{v}}
\newcommand{\bw}{\mathbf{w}}
\newcommand{\bx}{\mathbf{x}}
\newcommand{\by}{\mathbf{y}}
\newcommand{\bz}{\mathbf{z}}

\newcommand{\bF}{\mathbf{F}}
\newcommand{\bV}{\mathbf{V}}
\newcommand{\bX}{\mathbf{X}}

\newcommand{\bxi}{\bm{\xi}}

\newcommand{\blambda}{\bm{\lambda}}
\newcommand{\bzero}{\bm{0}}

\newcommand{\rhoi}{\rho_{\text{i}}}
\newcommand{\ip}[2]{\left<#1,#2\right>}

\newcommand{\Rpr}{R_{\text{pr}}}
\newcommand{\Rin}{R_{\text{in}}}
\newcommand{\Rfw}{R_{\text{fw}}}

\begin{document}
\title[Two balls, rigidly-connected]{Two balls, rigidly-connected: \\ a DAE case study}

\author{Ed Bueler}

\begin{abstract}
The problem of two equal-mass balls, rigidly connected by a massless rod, is described by an index-3 differential-algebraic equations (DAE) system.  An index-reduction procedure rewrites it as a stabilized index-2 DAE system.  Numerical solutions using implicit PETSc TS solvers are evaluated.
\end{abstract}

\maketitle

%\tableofcontents

\thispagestyle{empty}
\bigskip

\section{What are the cartesian equations of motion?}

Consider the problem of two equal masses $m$, labeled ``$a$'' and ``$b$'', moving in the $(x,y)$ plane, with $x$ horizontal and $y$ vertical.  We may form a column vector from their cartesian coordinates,
\begin{equation}
\bq(t) = \begin{bmatrix} q_1(t) \\ q_2(t) \\ q_3(t) \\ q_4(t) \end{bmatrix} = \begin{bmatrix} x_a(t) \\ y_a(t) \\ x_b(t) \\ y_b(t) \end{bmatrix}. \label{position}
\end{equation}

Now suppose the two masses move according to two kinds of forces.  First, gravity acts vertically downwards.  Second, suppose the masses are connected by a rigid, massless rod with length $\ell$.  The positions of the two masses are therefore constrained to satisfy $(x_a - x_b)^2 + (y_a - y_b)^2 = \ell^2$ at all times.  Equivalently, a certain scalar function is identically zero:
\begin{equation}
0 = g(\bq) = \frac{1}{2} \Big((q_1 - q_3)^2 + (q_2 - q_4)^2 - \ell^2\Big). \label{constraint}
\end{equation}
(The overall constant $\frac{1}{2}$ is chosen for later convenience.)

Physically speaking, the rod exerts a tension or expansion force along the line between the masses, which varies during the motion.  We denote this scalar force by $\lambda$, positive when the rod is pulling the two masses together.  Newton's second law says, of course, that the \emph{total} force $\bm{\Phi}(\bq,\lambda)$ determines the accelerations $\ddot \bq$, namely
\begin{equation}
m \ddot \bq = \bm{\Phi}(\bq,\lambda) \label{newtonssecond}
\end{equation}
describes the motion.  This is all fine and good, except how are we to determine the total (vector) forces including those enforcing the constraint?  In particular, how does $\bm{\Phi}$ depend on the tension $\lambda$, and how does the constraint \eqref{constraint} determine $\lambda$?

\section{As a rigid body: easy}

Classical mechanics does not regard this as a difficult problem.  It can be solved by a simple, planar application of the theory of \emph{rigid bodies} \cite[Vol.~1, Chapter 18]{Feynman2011i}.

Let $\xi(t),\eta(t)$ be the coordinates of the center of mass; this is the center of the rod.   Let $\theta(t)$ be the angle in radians between the rod and the horizontal axis, specifically the angle between the positive $x$-axis and the vector from the center of mass $(\xi,\eta)$ to the first mass $(x_a,y_a) = (q_1,q_2)$.  In terms of the three new variables $\xi,\eta,\theta$, we can write
\begin{equation}
\bq = \begin{bmatrix} \xi + (\ell/2) \cos\theta \\ \eta + (\ell/2) \sin\theta \\ \xi - (\ell/2) \cos\theta \\ \eta - (\ell/2) \sin\theta  \end{bmatrix}.  \label{cartesianrigid}
\end{equation}
Immediately constraint \eqref{constraint} is satisfied.

In the theory of rigid bodies the motion of the center of mass is determined by the external forces, here just gravity.  On the other hand the moment of inertial ($m\ell$) times the angular acceleration equals the torques applied to the rigid body.  Here there are no torques at all, so the angular acceleration is zero.  Thus, now that we are using the ``correct'' coordinates, the equations of motion are
\begin{subequations}
\label{rigidequations}
\begin{align}
      2 m \ddot\xi &= 0 \\
     2 m \ddot\eta &= -2 m g_r \\
m \ell \ddot\theta &= 0
\end{align}
\end{subequations}
As a first-order system this has dimension 6.

This is a delightfully trivial ODE system, with exact solution
\begin{subequations}
\label{rigidsolution}
\begin{align}
   \xi(t) &= \xi(0) + \dot\xi(0) t \\
  \eta(t) &= \eta(0) + \dot\eta(0) t - \frac{1}{2} g_r t^2 \\
\theta(t) &= \theta(0) + \dot\theta(0) t
\end{align}
\end{subequations}

The problem is thus easy in the correct coordinates.  However, we are interested in the solution of the DAE system in the original cartesian coordinates, primarily because other more-complicated problems do not have a cute theory like rigid bodies.  The rigid body exact solution \eqref{rigidsolution} will be used to verify the numerical solution of the ``wrong coordinates'' DAE solution.

In such verification usage we will need to convert the cartesian initial conditions to the initial conditions for the rigid body solution \eqref{rigidsolution}.  The key equations say that the cartesian velocity of each mass is the sum of the center-of-mass velocity plus the cross product of the angular velocity and the position vector of the mass relative to the center of mass, for instance $\bv = \bV + \bm{\omega}\times \br$ in obvious notation.  Thus, to do the initial-value conversion, we use \eqref{cartesianrigid} plus these equations for initial velocity:
\begin{subequations}
\label{velocitycartesianrigid}
\begin{align}
\dot q_1 &= \dot\xi - \dot\theta (q_2 - \eta) \\
\dot q_2 &= \dot\eta + \dot\theta (q_1 - \xi) \\
\dot q_3 &= \dot\xi - \dot\theta (q_4 - \eta) \\
\dot q_4 &= \dot\eta + \dot\theta (q_3 - \xi)
\end{align}
\end{subequations}

\section{Construction of a DAE system using Lagrangian dynamics}

Returning to our naive, cartesian problem, it is well-known that Newton's laws are poorly-suited to describing the forces in such a constrained situation, but that we can find the motion via \emph{Lagrangian dynamics}.  One can derive the Lagrangian approach from the calculus of variations, via Hamilton's principle of least action or the principle of virtual work \cite{Lanczos1970}, but here we will simply use the derived Euler-Lagrange equations (below) as the source of our DAE system.  This Lagrangian analytical approach applies broadly, across all of classical dynamics \cite{Layton1998}.

First, for unconstrained, non-dissipative motion described by the position variables $\bq(t) \in \RR^n$ and velocities $\dot\bq(t)$, one defines the \emph{Lagrangian} $\mathcal{L}_0(\bq,\dot\bq) = T(\bq,\dot\bq) - U(\bq)$ as the difference of kinetic and potential energy.  The motion $\bq(t)$ then solves the \emph{Euler-Lagrange differential equations} ($i=1,\dots,n$):
\begin{equation}
\frac{d}{dt} \frac{\partial \mathcal{L}}{\partial \dot q_i} = \frac{\partial \mathcal{L}}{\partial q_i}. \label{eulerlagrange}
\end{equation}
For constrained motion we must, however, modify the Lagrangian.  Suppose $\bg(\bq) \in \RR^k$ is a column vector of the constraints, i.e.
\begin{equation}
\bg(\bq)=\bzero. \label{generalconstraints}
\end{equation}
Now we define a vector of Lagrange multipliers $\blambda(t) \in \RR^k$ of the same length and a modified Lagrangian \cite[equation (58.2)]{Lanczos1970}
\begin{align}
\mathcal{L}(\bq,\dot\bq,\blambda) &= \mathcal{L}_0(\bq,\dot\bq) - \blambda^\top \bg(\bq)  \notag \\
  &= T(\bq,\dot\bq) - U(\bq) - \blambda^\top \bg(\bq). \label{extendedlagrangian}
\end{align}
Then the motion $\bq(t),\blambda(t)$ satisfies the system \eqref{eulerlagrange} for Lagrangian $\mathcal{L}$, along with the constraint equations \eqref{constraint}.  Physically, $\blambda$ is the (vector) force which enforces the constraints.

Because $\mathcal{L}$ does not depend on $\dot\blambda$, one may recover the constraints by the notional Euler-Lagrange equation $\bzero = d/dt(\partial \mathcal{L}/\partial \dot\blambda) = \partial \mathcal{L}/\partial\blambda = \bg(\bq)$.  In this sense the multipliers are treated as coordinates and the Euler-Lagrange equations include the constraint equations.

In our two-masses problem with $n=4$ there is $k=1$ (scalar) constraint $g(\bq)$ and thus one Lagrange multiplier $\lambda$, and it is the rod tension which we seek.  Based on the usual cartesian formula for kinetic energy and on gravitational potential energy, we have
\begin{equation}
T(\bv) = \frac{m}{2} \left(v_1^2+v_2^2+v_3^2+v_4^2\right), \qquad U(\bq) = m g_r \left(q_2+q_4\right), \label{energies}
\end{equation}
where $\bv(t) = \dot\bq(t)$ and $g_r>0$ is the acceleration of gravity.  The modified Lagrangian is
\begin{align}
\mathcal{L}(\bq,\bv,\lambda) &= T(\bv) - U(\bq) - \lambda g(\bq) \label{lagrangian} \\
  &= \frac{m}{2} \left(v_1^2+v_2^2+v_3^2+v_4^2\right) - m g_r \left(q_2+q_4\right) - \frac{\lambda}{2} \Big((q_1 - q_3)^2 + (q_2 - q_4)^2 - \ell^2\Big). \notag
\end{align}

From the Euler-Lagrange equations \eqref{eulerlagrange} and the constraint equation \eqref{constraint}, our two-masses problem is a system of 9 equations with (at most) first-order derivatives once we include the definition of the velocities:
\begin{subequations}
\label{rawsystem}
\begin{align}
  \dot q_1 &= v_1 \\
  \dot q_2 &= v_2 \\
  \dot q_3 &= v_3 \\
  \dot q_4 &= v_4 \\
m \dot v_1 &= - \lambda (q_1 - q_3) \\
m \dot v_2 &= - m g_r - \lambda (q_2 - q_4) \\
m \dot v_3 &= \lambda (q_1 - q_3) \\
m \dot v_4 &= - m g_r + \lambda (q_2 - q_4) \\
         0 &= \frac{1}{2} \Big((q_1 - q_3)^2 + (q_2 - q_4)^2 - \ell^2\Big) \label{rawsystem:constraint}
\end{align}
\end{subequations}

Next we rewrite system \eqref{rawsystem} using the vector notation from \cite[equation (9.30)]{AscherPetzold1998}:
\begin{subequations}
\label{system}
\begin{align}
\dot \bq &= \bv  \label{system:dotq} \\
m \dot \bv &= \bbf - \lambda\, G(\bq)^\top  \label{system:dotv} \\
0 &= g(\bq)  \label{system:constraint}
\end{align}
\end{subequations}
The column vector $\bbf = [0,-mg_r,0,-mg_r]^\top$ is the (constant) external force and the Jacobian matrix $G$ is a row vector
\begin{equation}
G(\bq) = \begin{bmatrix} {\displaystyle \frac{\partial g}{\partial q_i}} \end{bmatrix} = \begin{bmatrix} q_1-q_3, & q_2-q_4, & -(q_1-q_3), & -(q_2-q_4) \end{bmatrix}. \label{constraintjacobian}
\end{equation}
(In general $G$ is a $k\times n$ matrix.  Reference \cite{AscherPetzold1998} allows a nontrivial mass matrix $M(\bq)$, but here $M(\bq) = mI$.)  For future reference we calculate that $G(\bq) \bbf = 0$ and that
\begin{equation}
G(\bq) G(\bq)^\top = 2 (q_1-q_3)^2 + 2 (q_2 - q_4)^2 = 2 \ell^2 > 0.  \label{ggpd}
\end{equation}

System \eqref{system} would be suitable for numerical solution by a black-box ODE solver, e.g.~any adaptive explicit Runge-Kutta method \cite{AscherPetzold1998}, except that the constraint $0=g(\bq)$ is not, in fact, a differential equation at all.  Formulated as we have done it in cartesian coordinates, our problem is a \emph{differential-algebraic equation} (DAE) system.  Equations \eqref{system:dotq}, \eqref{system:dotv}, equivalently the first eight equations in system \eqref{rawsystem}, are differential equations, but \eqref{system:constraint} is algebraic.  Application of an implicit solver, one which consistently solves the discretized equations at each time step, will be necessary.

\section{DAE index}

Some DAE systems are more difficult to solve than others, but the various possibilities are neither simple to describe nor particularly easy to categorize.  One quantification which has proven value is the (\emph{differential}) \emph{index} of the DAE system.  This nonnegative integer is the minimum number of times that the algebraic equations, the constraints, must be differentiated in time before substitutions reveal an ODE system.  (An ODE system is thus an index-0 DAE system.)  While this is not obviously a rigorous definition, the linear case is, at least, precise \cite[Chapter IV.5]{HairerWanner1996}.

Our DAE problem \eqref{system} is a well-known type of mechanical system with equality constraints on the position variables (``holonomic constraints'' \cite{Lanczos1970}), a class of DAE problems with index 3.  Note that indices higher than 2 are traditionally called ``high index,'' because much less is known about reliable numerical solvers beyond index 2.

Let us show, by differentiating $g(\bq)=0$ with respect to time, why system \eqref{system} has index 3.  (The argument here will not exclude generating an ODE in fewer differentiations; this shows that the differential index is at most 3.)  Differentiating \eqref{system:constraint} once with respect to $t$ gives
\begin{align}
0 &= \frac{d}{dt} \bigg(\frac{1}{2} \Big((q_1 - q_3)^2 + (q_2 - q_4)^2 - \ell^2\Big)\bigg) \notag \\
  &= (q_1 - q_3)(v_1 - v_3) + (q_2 - q_4) (v_2 - v_4). \label{rawvelocityconstraint}
\end{align}
By substituting \eqref{system:dotq} and \eqref{constraintjacobian}, we may write this as a matrix-vector product:
\begin{equation}
0 = G(\bq) \bv. \label{velocityconstraint}
\end{equation}
This equation is called the \emph{velocity constraint}.

Differentiating again, and then substitution of \eqref{system:dotv} and \eqref{system:constraint}, yields the \emph{acceleration constraint} \cite{Layton1998}
\begin{equation}
0 = \frac{d}{dt} \Big((q_1 - q_3)(v_1 - v_3) + (q_2 - q_4) (v_2 - v_4)\Big) = (v_1 - v_3)^2 + (v_2 - v_4)^2 - \lambda \frac{2 \ell^2}{m} \label{rawddconstraint}
\end{equation}
or equivalently
\begin{equation}
\lambda = \frac{m}{2\ell^2} \left((v_1 - v_3)^2 + (v_2 - v_4)^2\right). \label{rawlambda}
\end{equation}
Writing this using matrix-vector notation is not so obvious.  However, since $G(\bq)\bv = \sum_i G_{1i}(\bq) v_i$, substituting \eqref{system:dotv} yields
\begin{align}
0 &= \sum_{i,j=1}^4 \frac{\partial G_{1i}(\bq)}{\partial q_j} \dot q_j v_i + \sum_{i=1}^4 G_{1i}(\bq) \dot v_i = \bv^\top \frac{\partial G(\bq)}{\partial \bq} \bv + G(\bq) \frac{1}{m} \left(\bbf - \lambda\, G(\bq)^\top\right) \notag \\
  &= \frac{\partial (G(\bq)\bv)}{\partial \bq} \bv +  \frac{G(\bq)}{m} \bbf - \lambda \frac{G(\bq) G(\bq)^\top}{m} = \frac{\partial (G(\bq)\bv)}{\partial \bq} \bv - \lambda \frac{G(\bq) G(\bq)^\top}{m}.  \label{ddconstraint}
\end{align}
Here
\begin{equation}
\left(\frac{\partial G(\bq)}{\partial \bq}\right)_{ij} = \frac{\partial G_{1i}(\bq)}{\partial q_j} \qquad \text{and} \qquad
\left(\frac{\partial (G(\bq)\bv)}{\partial \bq}\right)_{j} = \frac{\partial G_{1i}(\bq)}{\partial q_j} v_j,
\end{equation}
with the latter regarded as a row vector.  In fact, let
\begin{equation}
H(\bv) = \frac{\partial (G(\bq)\bv)}{\partial \bq} = \begin{bmatrix} v_1-v_3, & v_2-v_4, & -(v_1-v_3), & -(v_2-v_4) \end{bmatrix}. \label{velocityconstraintderiv}
\end{equation}
Applying \eqref{ggpd} in \eqref{ddconstraint} and solving for $\lambda$ gives \eqref{rawlambda} again:
\begin{equation}
\lambda = \frac{m}{2\ell^2} H(\bv) \bv = \frac{m}{2\ell^2} \left((v_1 - v_3)^2 + (v_2 - v_4)^2\right). \label{lambda}
\end{equation}

Differentiating \eqref{lambda}, and using this formula for $\dot\lambda$ to replace \eqref{system:constraint}, finally converts system \eqref{system} into an ODE system.  This \emph{unstabilized index reduction} shows that \eqref{system} has at most index 3.  However, we do not actually need or want the final ODE system, or even the differential equation for $\dot \lambda$.  Instead, we will use a stabilized index 2 DAE formulation as described in the next section.

\section{Stabilized index-2 formulation}

\begin{quote}
\emph{For a DAE of index greater than 2 it is usually best to use one of the index-reduction techniques \dots to rewrite the problem in lower-index form.} \, \cite[p 262]{AscherPetzold1998}
\end{quote}

The approach of Gear and others \cite{Gearetal1985} is to replace the original index-3 DAE system \eqref{system} with one which is more constrained, and has lower index, but which remains a DAE.  This involves two changes to system \eqref{system}.  First we append the velocity constraint \eqref{velocityconstraint} to \eqref{system}.  Then, to compensate, we add a corresponding Lagrange multiplier $\mu$, and use it to add a restoring constraint force to equation \eqref{system:dotq}:
\begin{subequations}
\label{stab}
\begin{align}
\dot \bq &= \bv - \mu\, G(\bq)^\top \label{stab:dotq} \\
m \dot \bv &= \bbf - \lambda\, G(\bq)^\top  \label{stab:dotv} \\
0 &= g(\bq)  \label{stab:qconstraint} \\
0 &= G(\bq) \bv  \label{stab:vconstraint}
\end{align}
\end{subequations}

As shown momentarily, system \eqref{stab} has index 2.  It is called the \emph{stabilized index-2 formulation} \cite[Exercise 9.10]{AscherPetzold1998} of our index-3 constrained mechanical system.  When such mechanical systems initially have $n$ position variables and $k$ constraints, thus total dimension $2n+k$, the dimension of the stabilized index-2 formulation becomes $2(n+k)$.  In our case $n=4$ and $k=1$.

It is easy to see that the exact solution is unchanged.  In fact, the solution of \eqref{stab} is a quadruple $\bq(t),\bv(t),\lambda(t),\mu(t)$ in which the triple $\bq(t),\bv(t),\lambda(t)$ solves \eqref{system} and $\mu(t)=0$ identically.  To show this, differentiate \eqref{stab:qconstraint} with respect to time and apply \eqref{ggpd}, \eqref{stab:dotq}, \eqref{stab:vconstraint}:
\begin{equation}
0 = G(\bq) \dot \bq = G(\bq) \left(\bv - \mu G(\bq)^\top\right) = - 2 \ell^2 \mu.
\end{equation}
When system \eqref{stab} is solved exactly it follows that $\mu(t)=0$.  However, the modified velocity equation \eqref{stab:dotq} will assist the numerical solver in staying on the constraint, making the numerical solution to \eqref{stab} superior the unstabilized formulation \eqref{system}.

To further expose the structure of system \eqref{stab}, let
\begin{equation}
\bx = \begin{bmatrix} \bq \\ \bv \end{bmatrix} \qquad \text{and} \qquad \bz = \begin{bmatrix} \mu \\ \lambda \end{bmatrix}.
\end{equation}
In this notation, system \eqref{stab} has the form
\begin{subequations}
\label{hessen}
\begin{align}
\dot \bx &= \br(\bx,\bz) \label{hessen:differential} \\
  \bzero &= \bs(\bx) \label{hessen:algebraic}
\end{align}
\end{subequations}
The $\bx$ variables are \emph{differential}, as they have time derivatives, while the $\bz$ variables are \emph{algebraic}.  For such a DAE system one would convert to an ODE system by computing by differentiating \eqref{hessen:algebraic} with respect to $t$ and then substituting \eqref{hessen:differential},
\begin{equation}
\bzero = \frac{\partial \bs}{\partial \bx} \dot \bx = \frac{\partial \bs}{\partial \bx} \br.
\end{equation}
This equation which is algebraic; note $\partial \bs/\partial \bx$ is a $2k\times 2n = 2\times 8$ matrix.  A second time-differentiation is now needed, yielding
\begin{equation}
\bzero = \frac{\partial^2 \bs}{\partial \bx^2}\left[\br, \br\right] + \frac{\partial \bs}{\partial \bx} \frac{\partial \br}{\partial \bx}\, \br + \frac{\partial \bs}{\partial \bx} \frac{\partial \br}{\partial \bz}\, \dot \bz  \label{hessen:indextwo}
\end{equation}
wherein the second derivative $\partial^2 \bs/\partial \bx^2$ is tensor-valued.  This gets us to the main point: equation \eqref{hessen:indextwo} can be solved for the needed time-derivative $\dot \bz$ if the matrix sitting beside it is invertible.  That is, system \eqref{hessen} has index 2 as long as
\begin{equation}
B = \frac{\partial \bs}{\partial \bx} \frac{\partial \br}{\partial \bz} \in \RR^{2k\times 2k} \quad \text{ is invertible}. \label{hessen:criterion}
\end{equation}
In general, when \eqref{hessen:criterion} holds we say system \eqref{hessen} is a \emph{Hessenberg} (or \emph{pure}) index-2 system.

In our two-balls problem one differentiates system \eqref{stab} to find $B$, a $2\times 2$ matrix which is in fact diagonal:
\begin{equation}
B = \begin{bmatrix}  G(\bq) G(\bq)^\top & 0 \\ 2 G(\bq) \bv & m^{-1} G(\bq) G(\bq)^\top \end{bmatrix} = 2\ell^2 \begin{bmatrix}  1 & 0 \\ 0 & m^{-1} \end{bmatrix}.
\end{equation}
As this matrix is invertible, we have shown that system \eqref{stab} has index 2.  Thus our index-reduction and stabilization procedure has converted the original index-3 problem into a Hessenberg index-2 system.

\section{Numerical solutions}

\begin{quote}
\emph{[For] Hessenberg index-2 DAEs, the coefficients of the multistep methods must satisfy a set of order conditions which is in addition to the order conditions for ODEs, to attain order greater than 2.  It turns out that these additional order conditions are satisfied by BDF methods.} \, \cite[p 267]{AscherPetzold1998}
\end{quote}

Our numerical implementation in this section will solve initial-value problems for a first-order, index-2 DAE system with 10 scalar variables.  We will apply the TS (time-steppers) component of the PETSc library \cite{Balayetal2021,Bueler2021} to solve the DAE system.

To use the fully-implicit solvers in PETSc TS, specifically BDF, the problem is best put in the most general DAE form
\begin{equation}
\bF(t,\bu,\bu')=0. \label{fullyimplicit}
\end{equation}
We choose to order the components of $\bu$ as follows:
\begin{equation}
\bu = \begin{bmatrix} \bq \\ \bv \\ \mu \\ \lambda \end{bmatrix}
= \begin{bmatrix} q_1 & q_2 & q_3 & q_4 & v_1 & v_2 & v_3 & v_4 & \mu & \lambda \end{bmatrix}^\top
\end{equation}
The function $\bF$ is defined following system \eqref{stab}:
\begin{equation}
\bF(t,\bu,\dot\bu)
 = \begin{bmatrix}
\dot \bq - \bv + \mu\, G(\bq)^\top \\
m \dot \bv - \bbf + \lambda\, G(\bq)^\top \\
g(\bq) \\
G(\bq) \bv
 \end{bmatrix}
 = \begin{bmatrix}
  \dot q_1 - v_1 + \mu (q_1 - q_3) \\
  \dot q_2 - v_2 + \mu (q_2 - q_4) \\
  \dot q_3 - v_3 - \mu (q_1 - q_3) \\
  \dot q_4 - v_4 - \mu (q_2 - q_4) \\
m \dot v_1 + \lambda (q_1 - q_3) \\
m \dot v_2 + m g_r + \lambda (q_2 - q_4) \\
m \dot v_3 - \lambda (q_1 - q_3) \\
m \dot v_4 + m g_r - \lambda (q_2 - q_4) \\
\frac{1}{2} \Big((q_1 - q_3)^2 + (q_2 - q_4)^2 - \ell^2\Big) \\
(q_1 - q_3) (v_1 - v_3) + (q_2 - q_4) (v_2 - v_4)
\end{bmatrix}
\end{equation}
Note that $\bF$ does not explicitly depend on $t$.

At each step of an implicit method, a system of 10 nonlinear equations will be solved.  While this can be done using a finite-difference Jacobian (see the next section), it is most efficient using an exact, analytical Jacobian.  Effectively this means we must calculate two $10 \times 10$ matrices, next, the derivatives of $\bF$ with respect to $\bu$ and $\dot\bu$.

The matrix $\partial\bF/\partial\bu$ has nested block structure with a larger upper-left $8\times 8$ block associated to the $\bq,\bv$ variables and a smaller lower-right $2\times 2$ block which is identically zero:
\begin{equation}
\frac{\partial\bF}{\partial\bu} =
\left[\begin{array}{cc|cc}
 \mu C & -I     & G(\bq)^\top & \\
\lam C &        &             & G(\bq)^\top \\ \hline
G(\bq) &        &             & \\
H(\bv) & G(\bq) &             &
\end{array}\right].
\end{equation}
(Blank blocks/entries are zero.)  Here $I$ is the $4\times 4$ identity matrix and
\begin{equation}
C = \begin{bmatrix}
 1 &    & -1 & \\
   &  1 &    & -1 \\
-1 &    &  1 & \\
   & -1 &    &  1
\end{bmatrix}.
\end{equation}
Recall that $H(\bv)$ is the $1\times 4$ row matrix defined in \eqref{velocityconstraintderiv}.

Because our problem is a nontrivial DAE, the matrix $\partial\bF/\partial\dot\bu$ is singular (nullity $=2$), but also conveniently diagonal:
\begin{equation}
\frac{\partial\bF}{\partial\dot\bu} = \left[\begin{array}{cc|cc}
I &    &   & \\
  & mI &   & \\ \hline
  &    & 0 & \\
  &    &   & 0
\end{array}\right]
\end{equation}

The PETSc implicit TS solvers use a callback for the function $\bF(t,\bu,\dot\bu)$  and, because implicit discrete methods generate a linear system based on a combined Jacobian \cite[section 2.5]{Balayetal2021}
\begin{equation}
J_\sigma = \sigma \frac{\partial\bF}{\partial\dot\bu} + \frac{\partial\bF}{\partial\bu} = \left[\begin{array}{cc|cc}
\sigma I + \mu C & -I     & G(\bq)^\top & \\
\lam C & \sigma m I &             & G(\bq)^\top \\ \hline
G(\bq) &        & 0           & \\
H(\bv) & G(\bq) &             & 0
\end{array}\right].
\end{equation}
for some scalar $\sigma$.  A key idea is that the DAE itself, and many implicit methods, will be uniquely solvable because $J_\sigma$ is nonsingular for $\sigma > 0$.

Next we need specific examples for testing purposes.

\subsection*{Example A}  Suppose the masses are tennis balls ($m=58\,\text{g}$), the rod has length $\ell=50\,\text{cm}$, and we are on Earth ($g_r=9.81\,\text{m}\,\text{s}^{-2}$).  For Example A we take the initial positions to be $(q_1,q_2)=(0,1)\,\text{m}$ and $(q_3,q_4)=(0,1.5)\,\text{m}$, thus the two balls start one above the other, with the lower one meter off the ground.  The initial velocities are $(v_1,v_2)=(10,10)\,\text{m}\,\text{s}^{-1}$ and $(v_3,v_4)=(15,10)\,\text{m}\,\text{s}^{-1}$ so that the second, higher mass starts at a higher velocity.  Observe that both constraints, $0=g(\bq)$ and $0=G(\bq)\bv$, are satisfied for these initial conditions.  That is, the initial positions are distance $\ell$ apart and the velocity constraint \eqref{stab:vconstraint} is satisfied.

\subsection*{Example B}  FIXME suppose balls falling straight down, side-by-side $\ell$ apart; $\partial\bF/\partial\bu$ clearly not invertible because $\mu=\lambda=0$; of course $\partial\bF/\partial\dot\bu$ also not invertible; but $J_\sigma$ invertible

FIXME start with BDF

\small

\bigskip
\bibliography{twoballs}
\bibliographystyle{siam}

\end{document}


\documentclass[]{article}
\begin{document}

\title{Homework 2 1.3: 1, 3, 7, 8, 11(c), 21(e)}
\author{Alex Gordon}
\date{\today}
\maketitle

\section*{Homework}
\subsection*{1. A)}

\begin{tabular}{ l | c || r || r ||}
 p & q & p $\wedge$ q are lying &$\neg$p (A is lying) \\
  \hline
  T & T & T & F\\
  T & F & F & F\\
  F & T & F & T\\
  F & F & F & T\\
  \hline
\end{tabular}
\\ The only answer that can explain this is that A is lying, because Row 3 is the only row that is consistent with all the demands.
\subsection*{1. B)}
\begin{tabular}{ | c | c | c || r || r || r || }
 p & q & r & $\neg$ p $\vee$ $\neg$ q &$\neg$ r & p $\wedge$ q \\
  \hline
  T & T & T & F & F & T\\
  T & T & F & T & T & F\\
  T & F & T & T & F & T\\
  T & F & F & T & T & F\\
  F & T & T & F & F & F\\
  F & T & F & T & T & F\\
  F & F & T & T & F & F\\
  F & F & F & T & T & F\\
  \hline
\end{tabular} \\
\\ We can only know that A is telling the truth. We know that either B or C is truthful, but with this truth table we can not tell which. This is shown in the fact that row 2 and 3 are consistent within the table.

\subsection*{1. C)}
\begin{tabular}{ | c | c | c || r || r || r || }
 p & q & r & $\neg$ p $\vee$ $\neg$ q &$\neg$ r & p $\wedge$ q \\
  \hline
  T & T & T & F & F & F\\
  T & T & F & F & T & T\\
  T & F & T & F & F & T\\
  T & F & F & T & T & T\\
  F & T & T & F & T & T\\
  F & T & F & F & T & T\\
  F & F & T & F & T & T\\
  F & F & F & T & T & T\\
  \hline
\end{tabular} \\
\\ Because of row 5, we can see that A is lying and both B and C are telling the truth. This is because that row is the only consistent row within the truth table.

\subsection*{3. A)}
$\neg$ p $\vee$ q
\subsection*{3. B)}
p $\wedge$ q
\subsection*{3. C)}
p $\wedge$ $\neg$ q
\subsection*{7. A)}
 t $\wedge$ d $\wedge$ h
\subsection*{7. B)}
t $\wedge$ d $\wedge$ $\neg$ h
\subsection*{7. C)}
(t $\vee$ h)$\wedge$ $\neg$ (t $\wedge$ h)
\subsection*{7. D)}
$\neg$ t $\wedge$ $\neg$ h

\subsection*{8. A)}
Bill is tall OR dark OR handsome AND he is NOT tall AND dark AND handsome
\subsection*{8. B)}
Bill is NOT tall and NOT dark
\subsection*{8. C)}
Bill is dark AND NOT tall AND NOT dark
\subsection*{8. D)}
Bill is tall AND dark OR NOT tall AND dark
\subsection*{11. C)}
\begin{tabular}{ | c | c || r || r || r || }
 p & q & p $\vee$ q &$\neg$p $\vee$ q & (p $\vee$ q) $\wedge$ ($\neg$p $\vee$ q)  \\
  \hline
  T & T & T & T & T\\
  T & F & T & F & F\\
  F & T & T & T & T\\
  F & F & F & T & F\\
  \hline
\end{tabular}
\subsection*{21. E)}
\begin{tabular}{| c | c | c || r || r ||}
 p & q & r & (p $\vee$ q) $\wedge$ (q $\vee$ r) &(p  $\wedge$ r)  $\vee$ q  \\
  \hline
  T & T & T & T & T \\
  T & T & F & T & T \\
  T & F & T & T & T \\
  T & F & F & F & F \\
  F & T & T & T & T \\
  F & T & F & T & T \\
  F & F & T & F & F \\
  F & F & F & F & F \\

  \hline
\end{tabular}
\end{document}


\section{Types of Rules}

\begin{table}[H]
    \centering
    \begin{tabularx}{\textwidth}{| l | c |}
        Terminal & $\rarr a | \epsilon$ \\
        Empty & $\rarr \epsilon$ \\
        Initial & $S \rarr$ \\
        Recursive & $A \rarr \alpha A \beta$ \\
        Left-Recursive & $A \rarr A \beta$ \\
        Right-Recursive & $A \rarr \alpha A$ \\
        Left-Right-Recursive & $A \rarr A \alpha A$ \\
        Copy/Categorization & $A \rarr B$ \\
        Linear & $\rarr u A v | w$ \\
        Left-Linear & $\rarr A v | w$ \\
        Right-Linear & $\rarr v A | w$ \\
        Homogeneous Normal & $\rarr A_1\ldots A_n | a$ \\
        Chomsky Normal & $\rarr AB | a$ \\
        Real Time Normal & $\rarr a \alpha | a$ \\
        Greibach Normal & $\rarr a A_1\ldots A_n | b$ \\
        Operator Normal & $\rarr A a B$ \\
    \end{tabularx}
\end{table}


\chapter{Introduction to Data Visualization}

It is difficult for the human mind to look at a list of numbers and
identify the patterns in them, so we often make pictures with
numbers. These pictures are called \textit{graphs}, or
\textit{charts}, or \textit{plots}. Often the right picture can make
the meaning in the data obvious. \textit{Data visualization} is the
process of making pictures from numbers.

\section{Common Types of Data Visualizations}

Depending on the type of data and what you are trying to demonstrate
about it, you will use different types of data visualizations.  How
many types of data visualizations are there? Hundreds, but we will
concentrate on just four: The bar chart, the line graph, the pie
chart, and the scatter plot.

\subsection{Bar Chart}

Here is an example of a bar chart.\index{bar chart}

\includegraphics[width=0.7\textwidth]{CookieChart.png}

Each bar represents the cookie sales of one person.  For example,
Charlie has sold 6 boxes of cookies, so the bar goes over Charlie's
name and reaches to the number 6.

Looking at this chart, you probably think, ``Wow, Debra has sold a lot
more cookies than anyone else, and Francis has sold a lot fewer.''

The same data could be in a table like this:

\begin{tabular}{c | c}
  Salesperson & Boxes Sold \\
  \hline
  Allison & 4 \\
  Becky & 5 \\
  Charlie & 6\\
  Debra & 12\\
  Elias & 5\\
  Francis & 1\\
  Glenda & 7
\end{tabular}

The table (especially a large table) is often just a bunch of
numbers. A chart helps our brains understand what the numbers mean.

Bar charts can also go horizontally.

\includegraphics[width=0.7\textwidth]{HorizontalBarCookies.png}

Sometimes we use colors to explain what contributed to the number.

\includegraphics[width=0.7\textwidth]{TypesCookieBar.png}

This tells us that Becky sold more boxes of chocolate chip cookies
than boxes of oatmeal cookies.

\subsection{Line Graph}

Here is a line graph.\index{line graph}

\includegraphics[width=0.7\textwidth]{SharksLine1.png}

These are often used to show trends over time. Here, for example, you
can see that the number of shark attacks has been increasing over
time.

You can have more than one line on a graph.

\includegraphics[width=0.7\textwidth]{SharksVsMosquitoes.png}

\subsection{Pie Chart}

You use a pie chart when you are looking at the comparative size of numbers.\index{pie chart}

\includegraphics[width=0.4\textwidth]{AirPie.png}

\subsection{Scatter Plot}

Sometimes you have a bunch of data points with two values and you are
looking for a relationship between them.  For example, maybe you write
down the average temperature and the total sales for your lemonade
stand on the 15th of every month:\index{scatter plot}

\begin{tabular}{c | c | c}
  Date &	Avg. Temp. &	Total Sales \\
  \hline
15 January 2022 & 2.6º C & \$183.85 \\
15 February 2022 & -4.2º C & \$173.56\\
15 March 2022 & 13.3º C & \$195.22\\
15 April 2022 & 26.2º C & \$207.61\\
15 May 2022 & 27.5º C & \$210.88\\
15 June 2022 & 31.3º C & \$214.18\\
15 July 2022 & 33.5º C & \$215.23\\
15 Aug 2022 & 41.7º C & \$224.07\\
15 September 2022 & 20.7º C & \$198.94\\
15 October 2022 & 17.2º C & \$196.10\\
15 November 2022 & 1.7º C & \$185.10\\
15 December 2022 & 0.2º C & \$188.70 \\
\end{tabular}

And you think ``I wonder if I sell more lemonade on hotter days?''

You might create a scatter plot.  For each day, you put a mark that
represents that temperature and the sales that day:

\includegraphics[width=\textwidth]{LemonadeScatter.png}

From this scatter plat, you can easily see that you do sell more
lemonade as the temperature goes up.

\section{Make Bar Graph}

Go back to your compound interest spreadsheet and make a bar graph
that shows both balances over time:

\includegraphics[width=0.9\textwidth]{InterestGraph.png}

The year column should be used as the x-axis. There are two series of
data that come from C4:C16 and E4:E16.  Tidy up the titles and legend
as much as you like.\index{spreadsheet!graphing multiple series}

Looking at the graph, you can see the balances start the same, but
balance of the account with the larger interest rate quickly pulls
away from the account with the smaller interest rate.


\subsection{Voronoi path planning}

Find paths as far as way from obstacles as possible

Divide plane in cells, with a cell around each obstacle

Within a cell, all points are closest to that obstacle

We move along the lines between cells

Overly conservative

Hard to compute in 3d

Small environmental changes can significantly change the graph.


\subsection{Logistic Regression}

A key subroutine in logistic regression \cite{Hastie2009} is improving the
estimates of the
coefficients, \(\beta\), using the Newton-Raphson algorithm.
In the course of this, we need to compute
\(\frac{1}{1 + e^{-x}}\) and
\(\frac{1}{(1 + e^{-x})^2}\).
We use this to motivate the ``conjoin'' feature of \Q.
Performing common sub-expression elimination as in
Figure~\ref{sub_expr} yields a 2x speedup.

\begin{figure}
\centering
\fbox{
\begin{minipage}{8 cm}
\centering
\verbatiminput{sub_expr.lua}
\caption{Common sub-expression elimination}
\label{sub_expr}
\end{minipage}
}
\end{figure}

\subsubsection{Lock step evaluation}
However, the code in Figure~\ref{sub_expr} has a subtle but
critical bug when the number of elements in \(x\) exceeds the chunk
size.  If we were to call {\tt eval()} on \(y\), then we would
end up consuming sucessive chunks of \(t_3\). Now, if we were to call
{\tt eval()} on \(z\), we would fail when requesting the first
chunk of \(t_3\), since it has {\bf not} been memo-ized. One solution
is to ensure that \(y\) and \(z\) are evaluated in lock-step, after they have
been created, as in Figure~\ref{lock_step}.
\begin{figure}
\centering
\fbox{
\begin{minipage}{8 cm}
\centering
\verbatiminput{lock_step.lua}
\caption{Lock step evaluation}
\label{lock_step}
\end{minipage}
}
\end{figure}

The problem with this solution is that the burden of lock-step evaluation falls
on the \Q\ programmer. This is remedied by the {\tt conjoin} function
(Figure~\ref{conjoin})

\begin{figure}
\centering
\fbox{
\begin{minipage}{8 cm}
\centering
\verbatiminput{conjoin.lua}
\caption{Conjoined Vectors}
\label{conjoin}
\end{minipage}
}
\end{figure}


\section{Canonical Transformations}
In Hamiltonian formalism,
$p$ and $q$ are on an equal footing.

What that means is we can make coordinate transformations that mix up the $p$
and $q$.
\begin{align}
    q_i &\to Q_i = Q_i\left( q, p, t  \right)\\
    p_i &\to P_i = P_i\left( q, p, t  \right)
\end{align}
Now in the Hamiltonian formalism,
$p$ and $q$ are on an equal footing.
So in general you can think of a transformation that mixes the $p$ and $q$.
But there's a catch.
Not all such transformations are allowed.

Only those transformations that are allowed are those that leave Hamiltonian's
equations invariant,
so that in general
\begin{align}
    H(q, p) \to K(Q, P, t)
\end{align}
and what we need is for
\begin{align}
    \frac{\partial K}{\partial P_i} &= \dot{Q}_i
\end{align}
and
\begin{align}
    \frac{\partial K}{\partial Q_i} &= -\dot{P}_i
\end{align}
so you're allowed a much bigger set of transformations,
but the condition is that they still have to respect Hamilton's equations.

\begin{question}
    How are $q$ and $\dot{q}$ not on an equal footing in the Lagrangian
    formalism?
\end{question}
If you know $q(t)$,
you know $\dot{q}$ from the derivative.
You have second order differential equation for $q(t)$.
Here,
you get two first-order equations for $p$ and $q$ separately.
You can vary $p$ and $q$ independently to get the equations.
But you can't do that in the Lagrangian formalism.

So anyway,
they shouldn't change the form of Hamilton's equations.

Transformations that satisfy this condition are called canonical
transformations.
This is the set of allowed transformations that leave the form of Hamilton's
equations invariant.

Unfortunately,
this is a huge topic,
quite a complicated one,
and we don't have enough time to go into great detail.
So I will look at a subset of these transformations.
I'm going to look at descriptive canonical transformations.
These are the most important subset,
but if you look in the book,
you'll see a more extended discussion.
The real difference is mostly the ones we're looking at are not going to
involve time.
So if you have transformations that mix $p$ and $q$ that don't mix time.
For those,
there's a simplification that $H=K$.
That's not very clear,
but it's part of the bigger formalism.

\section{Restricted Canonical Transformations}
So they are
\begin{align}
   q_i &\to Q_i(q, p)\\
   p_i &\to P_i(q, p)
\end{align}
and the $H$ and $K$ are the same
\begin{align}
    H(q, p) = K(Q, P)
\end{align}
This is what defines the restricted set of canonical transformations.
What we do is,
we begin by writing Hamilton's equations in a symmetric form.
So you put the $p$'s and $q$'s into a single vector.
\begin{align}
    \vec{z} &=
    \begin{pmatrix}
        q_1\\
        q_2\\
        \vdots\\
        q_n\\
        p_1\\
        p_2\\
        \vdots\\
        p_n
    \end{pmatrix}
\end{align}
So we just defined this giant vector $2n$ components
with all the $p$'s and the $q$'s together.

And then there's a standard matrix.
We introduce a $2n\times 2n$ matrix called $\hat{J}$,
which is of this form
\begin{align}
    \hat{J} &=
    \begin{pmatrix}
        0_{n\times n} & I_{n\times n}\\
        - I_{n\times n} & 0_{n\times n}\\
    \end{pmatrix}
\end{align}
Where the $0_{n\times n}$ denotes an $n\times n$ matrix of zeros,
and $1_{n\times n}$ is the $n\times n$ identity matrix.
It has the funny property that if you take the transpose you get $-1$ of itself.

In this notation,
Hamilton's equations are
\begin{align}
    \dot{\vec{z}} &=
    \hat{J}
    \frac{\partial H}{\partial \vec{z}}
\end{align}
or alternatively
\begin{align}
    \dot{z}_i
    &=
    \sum_{j}
    \hat{J}_{ij}
    \frac{\partial H}{\partial z_j}
\end{align}
This is just a rewrite.

So now we make our transformation
from $z=(q_i, p_i)$ to $w=(Q_i, P_i)$.
In other word's we're going from
\begin{align}
    z_i &\to w_i = w_i(z)
\end{align}
Let's keep going.

So now these $w$'s must satisfy an equation just like the equation for the
$\dot{z}$.
That is,
\begin{align}
    \dot{w}_i &=
    \sum_{j}
    \frac{\partial w_i}{\partial z_j}
    \dot{z_j}
\end{align}
but this $\dot{z}_j$ satisfies
\begin{align}
    \dot{z}_j &=
    \sum_{k}
    \hat{J}_{jk}
    \frac{\partial H}{\partial z_k}
\end{align}
so then the equation becomes
\begin{align}
    \dot{w} &=
    \sum_{j,j}
    \frac{\partial w_i}{\partial z_j}
    \hat{J}_{jk}
    \frac{\partial H}{\partial z_k}
\end{align}
but this last derivative can be written
\begin{align}
    \frac{\partial H}{\partial z_k}
    &=
    \sum_{l}
    \frac{\partial H}{\partial w_l}
    \frac{\partial w_l}{\partial z_k}
\end{align}
so putting it together
\begin{align}
    \dot{w}_i &=
    \sum_{l}
    \left(
    \sum_{jk}
    \frac{\partial w_i}{\partial z_j}
    \hat{J}_{jk}
    \frac{\partial w_l}{\partial z_k}
    \right)
    \frac{\partial H}{\partial w_l}
\end{align}
and you'll see why I put the brackets like that in a minute.
So this expression in the brackets is
\begin{align}
    \sum_{jk}
    \frac{\partial w_i}{\partial z_j}
    \hat{J}_{jk}
    \frac{\partial w_l}{\partial z_k}
    &=
    \left( J \hat{J} J \right)_{il}
\end{align}
where $J$ here is the Jacobian matrix of the transformation.
So when we transform from the $z$'s to the $w$'s,
there's a Jacobian matrix associated with that transformation.
So the Jacobian matrix is the matrix whose elements are
\begin{align}
    J_{ij} &=
    \frac{\partial w_i}{\partial z_j}
\end{align}
Those are the elements of the Jacobian matrix.
And you can see that with this definition,
this thing in the brackets is indeed just $\left( J \hat{J} J \right)_{il}$.

So then this equation can then be written as
\begin{align}
    \dot{\vec{w}}
    &=
    \left(
    J \hat{J} J^T
    \right)
    \frac{\partial H}{\partial \vec{w}}
\end{align}
So this equation comes once
once you recognize that expression can be written in terms of the Jacobian.
The condition for the transformation to be canonicial is tht this equation
should have the same form as this equation
\begin{align}
    \dot{\vec{z}} &=
    \hat{J}
    \frac{\partial H}{\partial \vec{z}}
\end{align}
That is,
the equation for $\dot{w}$ should have the same form as the equation for
$\dot{z}$.

So the condition for Hamilton's equations to take the same form is that
\begin{align}
    \boxed{J \hat{J} J^T = \hat{J}}
\end{align}
If this condition is satisfied then we are done.
This is the condition you have to remember.
It's easy in the exam to get a question that asks to show that a transformation
is canonical.
Then you just calculate the Jacobian matrix,
then compute this to check that you get $\hat{J}$ back.

If this condition holds,
then we say that the Jacobian is \emph{symplectic}.
It's a technical term.

If you study group theory,
you'll find that matrices that satisfy this equation forms a group,
called the symplectic group.
If you study classical mechanics in detail,
a large part of it is understanding properties of the symplectic group.
This is one of the fundamental classical continuous groups.

\begin{question}
    Is this the same as the canonical group?
\end{question}
No. Symplectic is not the same as canonical.
This symplectic group is the real symplectic group.
It's called $SP_2(i)$.
But in quantum theory,
the transformation also has to be unitary,
and it's complex.
For example, in QM,
you have an extra condition of unitary,
and you're allowed to have a complex map.
So things can be symplectic without being canonical.

This $\hat{J}$ matrix shows up everywhere beyond classical mechanics
in the mathematics literature.
Unfortunately,
this is what everyone calls it $J$,
without the hat,
but unfortunately,
the Jacobian is also called $J$,
so I have to choose which one has the hat.

So we found this equation.
We now show that this condition is equivalent to the requirement that the new
coordinates satisfy
\begin{align}
    \left\{
    Q_i, Q_j
    \right\}
    &=
    \left\{
    P_i, P_j
    \right\}
    = 0\\
    \left\{
    Q_i, P_i
    \right\}
    &=
    \delta_{ij}
\end{align}
The claim is,
that if the new coordinates satsify the Poisson brackets,
then you are guaranteed that symplectic condition.

So let's show that.
This is just an exercise in linear algebra.
The Jacobian matrix by definition is
\begin{align}
    \frac{\partial w_i}{\partial z_j} &=
    J_{ij}\\
    &=
    \begin{pmatrix}
        \partial Q_i/ \partial q_j & \partial Q_i / \partial p_j\\
        \partial P_i/ \partial q_j & \partial P_i / \partial p_j\\
    \end{pmatrix}
\end{align}
What we're doing is evaluating this condition,
but in terms of the $Q$'s and $P$'s rather than in $z$-space.
Just blindly plugging it in.
Then what we have is
\begin{align}
    \left( J \hat{J} J^T \right)_{ik} &=
    \sum_{j} \sum_{k}
    \frac{\partial w_i}{\partial z_j}
    \hat{J}_{jk}
    \frac{\partial w_i}{\partial z_k}\\
    &=
    \begin{pmatrix}
        \frac{\partial Q_i}{\partial q_j} &
        \frac{\partial Q_i}{\partial p_j}\\
        \frac{\partial P_i}{\partial q_j} &
        \frac{\partial P_i}{\partial p_j}\\
    \end{pmatrix}
    \begin{pmatrix}
        0 & \delta_{ij}\\
        - \delta_{jk} & 0
    \end{pmatrix}
    \begin{pmatrix}
        \frac{\partial Q_i}{\partial q_k} &
        \frac{\partial P_l}{\partial q_k}\\
        \frac{\partial Q_l}{\partial p_k} &
        \frac{\partial P_l}{\partial p_k}
    \end{pmatrix}
\end{align}
and as an exercise in linear algebra,
if you multiply this out,
you get
\begin{align}
    \begin{pmatrix}
        0 & \delta_{ij}\\
        - \delta_{jk} & 0
    \end{pmatrix}
    \begin{pmatrix}
        \frac{\partial Q_i}{\partial q_k} &
        \frac{\partial P_l}{\partial q_k}\\
        \frac{\partial Q_l}{\partial p_k} &
        \frac{\partial P_l}{\partial p_k}
    \end{pmatrix}
    &=
    \begin{pmatrix}
        \frac{\partial Q_i}{\partial p_j} &
        \frac{\partial P_l}{\partial p_j}\\
        -\frac{\partial Q_l}{\partial q_j} &
        -\frac{\partial P_l}{\partial q_j}
    \end{pmatrix}
\end{align}
If you do the multiplication,
I claim that this is what you're going to find.
\begin{align}
    \left(J \hat{J} J\right)_{ik}
    &=
    \begin{pmatrix}
        \left\{ Q_i, Q_l \right\} &
        \left\{ Q_i, P_l \right\}\\
        \left\{ P_i, Q_l \right\} &
        \left\{ P_i, p_l \right\}
    \end{pmatrix}
\end{align}
and so if this is really equal to $\hat{J}_{il}$,
then we get the conditions
\begin{align}
    \left\{ Q_i, Q_l \right\} &=
    \left\{ P_i, P_l \right\} = 0
\end{align}
and
\begin{align}
    \left\{ Q_i, P_l \right\} &= \delta_{il}
\end{align}

I just want you to convince yourself that the $ij$-th element,
corresponds to that block.
What it is depends on which block you're in.

\begin{question}
    If we actually set out to compute the Jacobian,
    we could just do it element by element right?
\end{question}
I just wanted to show that the condition
$J \hat{J} J^T = \hat{J}$
is equivalent to
$\left\{ Q_i, Q_l \right\} = \left\{ P_i, P_l \right\} = 0$
and
$\left\{ Q_i, P_l \right\} = \delta_{il}$.
The two are completely equivalent mathematically.
In the exam you could show either.

\section{Poisson Bracket Invariance}
There's one more thing I want to show.
The Poisson bracket is invariant under canonical transformation.
Let me explain what I mean by this.
Suppose you have a transformation
\begin{align}
    q &\to Q(q, p)\\
    p &\to P(q, p)
\end{align}
And suppose you have a Poisson bracket that is by definition,
what I mean is that they are the same in the old coordinates and the new
coordinates.
\begin{align}
    \left\{ f, g \right\}
    &=
    \sum_{i}
    \left(
    \frac{\partial f}{\partial q_i}
    \frac{\partial g}{\partial p_i}
    -
    \frac{\partial g}{\partial q_i}
    \frac{\partial f}{\partial p_i}
    \right)
    \sum_{i}
    \left(
    \frac{\partial f}{\partial Q_i}
    \frac{\partial g}{\partial P_i}
    -
    \frac{\partial g}{\partial Q_i}
    \frac{\partial f}{\partial P_i}
    \right)
\end{align}
That means,
if you have a Poisson bracket,
you can evalaute it in any coordinates you like,
provided they are related by canonical transformation.

Let's prove this.
This is one of those things where it proves useful to be good at linear algebra.
To prove this note that
\begin{align}
    \left\{ f, g \right\}
    &=
    \sum_i
    \left(
    \frac{\partial f}{\partial q_i}
    \frac{\partial g}{\partial p_i}
    -
    \frac{\partial f}{\partial p_i}
    \frac{\partial g}{\partial q_i}
    \right)\\
    &=
    \sum_{i}
    \sum_{j}
    \left(
    \frac{\partial f}{\partial z_i}
    \hat{J}_{ij}
    \frac{\partial g}{\partial z_j}
    \right)
\end{align}
So we're going to expand it out and show the claim that they are equal.

Under $z\to w(z)$,
we have
\begin{align}
    \frac{\partial f}{\partial z_i}
    &=
    \sum_{j}
    \left( \frac{\partial f}{\partial w_j} \right)
    \underbrace{\left( \frac{\partial w_j}{\partial z_j} \right)}_{J_{ji}}
\end{align}
where we notice the second factor is just the Jacobian matrix element.

Then we have
\begin{align}
    \left\{ f, g \right\}
    &=
    \sum_i \sum_j \sum_k \sum_l
    \frac{\partial f}{\partial w_k}
    J_{ki}
    \hat{J}_{ij}
    J_{lj}
    \frac{\partial g}{\partial w_l}\\
    &=
    \sum_k \sum_l
    \frac{\partial f}{\partial w_k}
    \left( J \hat{J} J^T \right)_{kl}
    \frac{\partial g}{\partial w_l}
\end{align}
But because the transformation is canonical,
\begin{align}
    J \hat{J} J^T &= \hat{J}_{kl}
\end{align}
we find
\begin{align}
    \left\{ f, g \right\}
    &=
    \sum_l
    \frac{\partial f}{\partial w_k}
    \hat{J}_{kl}
    \frac{\partial g}{\partial w_l}
\end{align}
and we can immediately go back to the definition of $\hat{J}$ and see
\begin{align}
    \left\{ f, g \right\}
    &=
    \sum_i
    \left(
    \frac{\partial f}{\partial Q_i}
    \frac{\partial g}{\partial P_i}
    -
    \frac{\partial g}{\partial Q_i}
    \frac{\partial f}{\partial P_i}
    \right)
\end{align}
and we are done.

So the Poisson bracket can be evaluated in any coordinate system,
provided they are related by canonical transformation.
It's a huge subject.
There is a chapter in Goldstein dedicated to canonical transformation.
The current edition is quite a bit better than the earlier ones.
They are mostly taken from David Tong's lectures.
But,
you can find this in Goldstein as well.
Not in the first edition,
but it is in the second edition.

Now I'm going to start a new topic.

\section{Action-Angle Variables}
Consider a one-dimensional system $H(q, p)$
without explicit time-dependence.
So then the total energy is conserved.
We assume that the motion is bounded,
so there exists some $q_1$, $q_2$ such that
\begin{align}
    q_1 \le q \le q_2
\end{align}
So then imagine an arbitrary potential.
The system undergoes periodic motion,
with turning points $q_1$ and $q_2$.
Transform from
$(p, q)$
to new variables
$(I, \theta)$
which have the property that $H$ is independent of $\theta$,
so the Hamiltonian is a function of $I$
\begin{align}
    H = H(I).
\end{align}
In these variables,
from Hamilton's equations
\begin{align}
    -\frac{dI}{dt} &= \frac{\partial H}{\partial \theta} = 0
\end{align}
which means that $I$ is a constant of the motion.
Then what about the other Hamilton's equation?
\begin{align}
    \frac{d\theta}{dt} &=
    \frac{\partial H}{\partial I}
\end{align}
which is some function of $I$.
Remember $H$ does not contain $t$ and it does not contain $\theta$,
and $I$ we just showed to be a constant.
So this is also a constant.

So the equations of motion are very simple.
So this equation can be integrated to find that $\theta$ is just a constant
times time,
and the first equation just tells you $I$ is a constant.

And so by convention,
$I$ and $\theta$ are normalized such that
$\dot{\theta} = \omega$,
where $\omega$ is the angular frequency of this oscillation.

$I$ is called the \emph{action variable}
and $\theta$ is called the \emph{angle variable}.

So $I$ is like the momentum and $\theta$ is like the position.

The point is that in terms of the action-angle variables,
the problem is very simple.
This equation just tells you $I$ is a constant,
and this equation tells you this can be integrated to $\theta=\omega t$.
That's easy.

The hard work is figuring out what is the relation between $I$ and $\theta$.
If you can find the action-angle variables,
you can trivially solve the problem.
The challenge is how do you find the action-angle variables.
If you can find those variables,
you basically solve the problem.

I'm going to show you how to do his for a special case and then we'll talk
about how potentially generalize it.

Almost all the solve problems there are,
you can reduce it down to action-angle variables,
the Kepler problem,
the harmonic oscillator,
etc.
But there are chaotic systems for which there is no such variable.s
But in more complicated systems,
you usually start from a limit that does have action-angle variables,
and then you perturb.
So you might not have exact action-angle variables,
but then you start from a point where you do.
In this case,
they exist because there is a constant of motion,
the conserved energy,
and you will see it is related in a trivial way to the action variable.
Once you have an action variable,
that's all you need.
If you have an $n$-dimensional system with conserved energy,
there is a theorem which says you do have action-angle variables.
But if energy is not conserved,
you may not have action-angle variables in general.

Consider
\begin{align}
    H &=
    \frac{p^2}{2m} + V(q)
\end{align}
If $I$ is a constant of the motion,
it must be some function of the energy.
\begin{align}
    H &= H(I) =  E
\end{align}
Then
\begin{align}
    \dot{\theta}
    &=
    \frac{\partial H}{\partial I}
    =
    \frac{d E}{dI}
    = \omega
\end{align}
This is because $H$ does not depend on $\theta$.
Remember that we normalized $\dot{\theta}$
and this $\omega$ is the angular frequency of oscillation.
That's because we normalized the action-angle variables to get this.
So this is the setting.
For this Hamiltonian,
we have to find the action variable which is a constant of the motion,
some function of the energy,
and it has this property that $\frac{dE}{dI} = \omega$.

The claims is the following.
The correct choice of $I$ is
\begin{align}
    I &=
    \frac{1}{2\pi}
    \oint p\, dq
\end{align}
where this is the area in phase space enclsoed by once orbit.

In other words,
in physical space,
this thing is just bouncing back and forth between $q_1$ and $q_2$.
Let's thing about what this means in phase space.

There is a turning point $q_2$ and there's a turning point $q_1$.
And the horizontal axis is $q$ and the vertical axis is $p$.
The system traces out an orbit in phase space.
This has a reflection symmetry about the $q$ axis.
This is because therei s a positive value of $p$ and it comes back with a
negative value of $p$,
since the root of the equation gives two solutions.
So this thing is the enclosed area,
which is obviously a function of the energy.
With more energy you can go further apart.
So this is a function of the energy and this gives a relation between the energy
and $I$.
This is just a claim we haven't proved it yet.

Okay,
so let's now try to prove this claim.
To prove this,
we need to show that
\begin{align}
    \frac{d}{dE}\left(
    \oint p\, dq
    \right)
    =
    \frac{2\pi}{\omega}
\end{align}
where $\omega$ is the frequency of oscillation.
Alright, so this is what we're going to show.
So now we have a bit of work.
How do we evalaute the area of this curve for a given $E$?
\begin{align}
    p &=
    \sqrt{2m\left( E - V(q) \right)}
\end{align}
As $E$ is changed, two things happen.
Firstly,
obviously the value of $p$ at every point $q$ is altered.
So
\begin{align}
    p &=
    p
    +
    \left( \frac{\partial p}{\partial E}\right)_q
    \Delta E
\end{align}
By convention the square root is always positive.
So if we change the energy,
at every point inside this integral,
the valueof $E$ is going to change for a given $q$.

Secondly,
the end points $q_1$ and $q_2$ are shifted.
That's because if you change $E$,
your $q_1$ and $q_2$ are not the same now.
We're going to see how much this changes if I change $E$.
That's what we're planning to do.

The value of that integral changes because $E$ is changing at every point.
And the end points are shifting.
We're going to account for both these effects.


\subsection{Visibility graphs}

Visibility graph generates nodes from points on obstacles, current position and goal. generate graph.

Use A* to solve

That algorithm is called VGRAPH

\subsection{Robots with volume}

What if robot takes up space?

Expand each obstacle by size of robot

How to grow each obstacle? draw shape of robot around each obstacle. shape is relative to point, so the growth is asymmetric if you choose a point for the robot which is not in the middle. eg a vertex.

\subsection{Rotation of robots}

What about rotation? grow robot by shape of all rotations. but this is overly conservative. will miss paths

Can do vgraph for each rotation, and switch between rotations. can choose 2 or 3 rotations


Assuming that data is appropriately treated to eliminate redundant
features, we proceed to propose surrogate models and criteria
used for their evaluation. The task all presented surrogates strive to solve can be
formulated using the language of conventional regression problems. In this section,
we focus on interpretation in the scheme of supervised learning with decoupled
and adaptive sampling.

Labeling the expensive MC TBR model $f(x)$, a surrogate is a mapping
$\hat{f}(x)$ that yields similar images as $f(x)$. In other words, $f(x)$ and
$\hat{f}(x)$ minimise a selected dissimilarity metric. Furthermore, in order to
be considered \textit{viable}, surrogates are required to achieve expected evaluation time
lower than that of $f(x)$.

In the decoupled sampling approach that is further described
in~\cref{sec:supervised}, we first gather a sufficiently large
training set of samples $\mathcal{T}=\left\{\left( x^{(i)},f\left(x^{(i)}\right) \right)\right\}_{i=1}^N$
to describe the behaviour of $f(x)$ across its domain.
Depending on a specific model family and appropriate choice of its
hyperparameters, surrogate models $\hat{f}(x)$ are trained to minimise
the empirical risk~$R_{\text{emp.}}$ with respect to $\mathcal{T}$ and a model-specific
loss function $\mathcal{L}$, where the empirical risk is defined as
$R_{\text{emp.}}(\hat{f}\mid\mathcal{T},\mathcal{L})
	=\frac{1}{N}\sum_{i=1}^N
	\mathcal{L}\left(\hat{f}(x^{(i)}),f(x^{(i)})\right)$.

The adaptive sampling approach that we characterise in~\cref{sec:adaptive} can be viewed as a more general problem.
Rather than fixing the training set $\mathcal{T}$ for the entire duration of
training, multiple sets $\{\mathcal{T}_k\}_{k=0}^K$ are used, such that
$\mathcal{T}_{k-1}\subset\mathcal{T}_k$ for all $k>1$. The first set
$\mathcal{T}_0$ is initialised randomly to provide a \textit{burn-in}, and is
repeatedly extended in epochs, whereby each epoch trains a new surrogate~$\hat{f}_k(x)$ on
$\mathcal{T}_k$ using the supervised learning procedure, evaluates its
performance, and forms a new set $\mathcal{T}_{k+1}$ by adding more samples to
$\mathcal{T}_k$. This permits the learning algorithm to condition the selection
of new samples in~$\mathcal{T}_{k+1}$ on the evaluation results
of~$\hat{f}_k(x)$ in order to maximise improvement of
surrogate performance over complex regions within the feature space.

\subsection{Metrics}
\label{sec:metrics}

Aiming to provide objective comparison of a diverse set of surrogate model
families, we define a multitude of metrics to be tracked during experiments.
Following the motivation of this work, two desirable properties of surrogates
arise: (a) their capability to approximate the TBR MC model well and (b) their
prediction time. An ideal surrogate would maximise
the former while minimising the latter.

\Cref{tbl:metrics} provides an exhaustive listing and description of metrics recorded
in our experiments. For regression performance analysis, we include a selection
of absolute metrics to assess the approximation capability of surrogates, and set
practical bounds on the expected uncertainty of their predictions. In addition, we also track
relative measures that are better-suited for comparison between this work and others, as
they maintain invariance with respect to the selected domain and image space.
For complexity analysis, surrogates are assessed in terms of wall
time\footnote{Real time elapsed during computation measured by a
chronometer, here by means of the Python~\texttt{time} package.}
elapsed during training and prediction. This is motivated by common practical use
cases of our work, where models are trained and used as drop-in replacements for the
expensive MC TBR model. Since training set sizes remain to be determined, all times are
reported per a single datapoint. Even though some surrogates support acceleration
by means of parallelisation, sequential processing of samples was ensured to
achieve comparability between considered
models.\footnote{The only exception to this are artificial neural networks,
	which require considerable amount of processing power for training on conventional CPU architectures.}

\begin{table}[h]
	\centering
	{\footnotesize
		\begin{tabular}{llrl}
		\toprule
		Regression performance metrics	& Mathematical formulation / description &
		\multicolumn{2}{c}{Ideal value [units]} \\
		\midrule
		Mean absolute error (MAE)	& $\sum_{i=1}^N |y^{(i)}-\hat{y}^{(i)}|/N$ & 0
									& [TBR] \\
		Standard error of regression $S$	& $\text{StdDev}_{i=1}^N\left\{ |y^{(i)} -
		\hat{y}^{(i)}| \right\} $	 & 0 & [TBR] \\
		Coefficient of determination $R^2$	& $1-\sum_{i=1}^N \left(y^{(i)}-\hat{y}^{(i)} \right)^2 /
		\sum_{i=1}^N \left( y^{(i)}-\overline{y} \right)^2 $ & 1 & [rel.] \\
		Adjusted $R^2$	& $1-(1-R^2)(N-1)/(N-P-1)$	& 1 & [rel.] \\
		\midrule
		Complexity metrics	& {} & {} & {} \\
		\midrule
		Mean training time $\overline{t}_{\text{trn.}}$	& $(\text{wall training time of
		$\hat{f}(x)$})/N_0$ 	& 0 & [ms] \\
		Mean prediction time $\overline{t}_{\text{pred.}}$	& $(\text{wall prediction time of
		$\hat{f}(x)$})/N$	& 0 & [ms] \\
		Relative speedup $\omega$	& $(\text{wall evaluation time of $f(x)$}) /
		(N\overline{t}_{\text{pred.}})$	&
		$\to\infty$ & [rel.] \\
		\bottomrule
		\end{tabular}
	}
	\caption{Metrics recorded in supervised learning experiments. In
	formulations, we work with a training set of size $N_0$ and a test set of
size $N$, TBR values $y^{(i)}=f(x^{(i)})$ and $\hat{y}^{(i)}=\hat{f}(x^{(i)})$
denote images of the $i$th testing sample in the expensive model and the surrogate
respectively. Furthermore, the mean $\overline{y}=\sum_{i=1}^N y^{(i)}/N$ and $P$ is the
number of input features.}
	\label{tbl:metrics}
\end{table}

To prevent undesirable bias in results due to training set selection, all metrics
are collected in the scheme of $k$-fold cross-validation with a conventional choice of
$k=5$. In this setting, a sample set is uniformly divided into 5 disjoint folds, each of which
is used as a test set for models trained on the remaining 4.\footnote{Unless explicitly stated otherwise, we use 1
fold for testing and the 4 remaining folds for training. This gives 80\% to
20\% train-test ratio.} Having repeated the same experiment for each such run,
the overall value of individual metrics is reported in terms of their mean and
standard deviation over all folds.

\subsection{Decoupled Sampling}
\label{sec:supervised}

In our experiments, we evaluate and compare surrogates in effort to
optimise against metrics described in~\cref{sec:metrics}. To attain meaningful
and practically usable results, we require a sufficiently large and diverse pool
of surrogate families to review. This is described by the listing in~\cref{tbl:surrogates}. The presented selection of models includes basic
techniques suitable for linear regression enhanced by the kernel trick or dimension
lifting, methods driven by decision trees, instance-based learning models,
ensemble regressors, randomised algorithms, artificial neural networks and mathematical approaches
developed specifically for the purposes of surrogate modelling. For each of
these families, a state-of-the-art implementation was selected and adapted to
operate with TBR samples.

\begin{table}[h]
	\centering
	{\footnotesize
		\begin{tabular}{llll}
		\toprule
		Surrogate & Acronym & Implementation & Hyperparameters \\
		\midrule
		Support vector machines~\cite{fan2008liblinear}	& SVM & SciKit Learn~\cite{scikit-learn} & 3 \\
		Gradient boosted trees~\cite{friedman2001greedy,friedman1999stochastic,hastie2009elements}	& GBT & SciKit Learn & 11 \\
		Extremely randomised trees~\cite{geurts2006extremely}	& ERT & SciKit Learn & 7 \\
		AdaBoosted decision trees~\cite{drucker1997improving}	& ABT & SciKit Learn & 3 \\
		Gaussian process regression~\cite{williams2006gaussian}	& GPR & SciKit Learn & 2 \\
		$k$ nearest neighbours	& KNN & SciKit Learn & 3 \\
		Artificial neural networks	& ANN & Keras (TensorFlow)~\cite{chollet2015keras} & 2 \\
		Inverse distance weighing~\cite{shepard1968two} & IDW & SMT~\cite{SMT2019} & 1 \\
		Radial basis functions & RBF & SMT & 3 \\
		\bottomrule
		\end{tabular}
	}
	\caption{Considered surrogate model families.}
	\label{tbl:surrogates}
\end{table}

While some of the presented model families are clearly determined by an explicit
choice of a learning algorithm, others may vary considerably depending on
hyperparameter configuration. A good example of the latter group are artificial neural
networks, which in addition to conventional hyperparameters enable to control network
architecture through selection from various parametric graph templates
(illustrated in~\cref{fig:nn-archs}). This allows us to realise a simplistic
network architecture search during the hyperparameter tuning procedure.

\begin{figure}[h]
	\centering
	\begin{subfigure}[b]{0.25\textwidth}
		\centering
		{\scriptsize \incfigscale{0.6}{1h3f}}
		\caption{$\text{1h3f}(W)$}
	\end{subfigure}\hfill%
	\begin{subfigure}[b]{0.25\textwidth}
		\centering
		{\scriptsize \incfigscale{0.6}{kf}}
		\caption{$\text{df}(D,W)$}
	\end{subfigure}\hfill%
	\begin{subfigure}[b]{0.25\textwidth}
		\centering
		{\scriptsize \incfigscale{0.6}{3pyramid}}
		\caption{$\text{3pyramid}(W)$}
	\end{subfigure}\hfill%
	\begin{subfigure}[b]{0.25\textwidth}
		\centering
		{\scriptsize \incfigscale{0.6}{5diam}}
		\caption{$\text{5diamond}(W)$}
	\end{subfigure}

	\caption{Selected parametric neural network architectures. All layers except
		the last use ReLU activation. Prediction information flow is indicated by
		arrows.}
	\label{fig:nn-archs}
\end{figure}

\subsubsection{Experiments}
\label{sec:experiment-methodology}

The presented surrogate candidates are evaluated in four experimental cases:%
\begin{enumerate}
	\item Hyperparameter tuning in a simplified domain.

	\item Hyperparameter tuning in full domain.

	\item Scaling benchmark.

	\item Model comparison.
\end{enumerate}

The aim of the initial experiments is to use a relatively small subset of
collected TBR samples to determine hyperparameters of considered surrogates.
Since this process requires learning the behaviour of an unknown, possibly
expensive mapping -- here a function that assigns cross-validated metrics to a
point in the hyperparameter domain -- it in many aspects mirrors
the primary task of this work with the notable extension of added utility
to optimise. In order to avoid undesirable exponential slowdown in exhaustive
searches of a possibly high-dimensional parameter space, Bayesian
optimisation~\cite{movckus1975bayesian} is employed as a standard hyperparameter tuning algorithm. We set
its objective to maximise $R^2$ and perform
1000~iterations.\footnote{Hyperparameter tuning of each surrogate family was
	terminated after 2~days. Instances that reached this limit may be identified
	in~\cref{tbl:exp1-detailed-results,tbl:exp2-detailed-results} in the
	Appendix.}

In the first experiment, efforts are made to maximise the possibility of success
in surrogates that are prone to suboptimal performance in discontinuous spaces.
This follows the notion that, if desired, performance of such models may be
replicated by training separate instances to model each continuous subregion of
the domain independently.
To this end, data are limited to a single slice from run~2, and discrete
features are completely withheld from evaluated
surrogates. This is repeated for each of the four available slices to
investigate variance in behaviour under different discrete feature assignments.
The second experiment conventionally measures surrogate performance on the full
feature space. Here, in extension of the previous case, surrogates work with
samples comprised of discrete as well as continuous features.

The objective of the last two experiments is to exploit the information gathered by
hyperparameter tuning. In the third experiment, the 20~best-performing
hyperparameter configurations of each family (with respect to~$R^2$) are used to
perform training on progressively larger sets to investigate their scaling
properties. Following that, the fourth experiment aims to produce surrogates
suitable for practical use by retraining selected well-scaling instances on large
training sets to satisfy the goals of this work.

\subsection{Adaptive Sampling}
\label{sec:adaptive}

\begin{wrapfigure}[25]{r}{0.55\textwidth}
	\centering
	\vspace{-3ex}
	\includegraphics[width=0.7\textwidth]{fig4_qassplan.png}
	\caption{Schematic of QASS algorithm}
	\label{fig:qassplan}
\end{wrapfigure}

All of the surrogate modelling techniques studied in this project face a common
challenge: their accuracy is limited by the quantity of training samples which
are available from the expensive MC TBR model. Adaptive sampling procedures can
improve upon this limitation by taking advantage of statistical information
which is accumulated during the training of any surrogate model. Rather than
training the surrogate on a single sample set generated according to a fixed
strategy, sample locations are chosen periodically during training so as to best suit the model
under consideration.

Adaptive sampling techniques appear frequently in the literature and have been
specialised for surrogate modelling. Garud's~\cite{Garud2016} ``Smart Sampling
Algorithm'' achieved notable success by incorporating surrogate quality and
crowding distance scoring to identify optimal new samples, but was only tested
on a single-parameter domain. We theorised that a nondeterministic sample
generation approach, built around Markov Chain Monte Carlo methods (MCMC), would
fare better for high-dimensional models by more thoroughly exploring all local
optima in the feature space. MCMC produces a progressive chain of sample points,
each drawn according to the same symmetric proposal distribution\footnote{An
adaptive MCMC procedure~\cite{Zhang2012}, which adjusts an ellipsoidal proposal
distribution to fit the posterior, was also implemented but not fully tested.}
from the prior point. These sample points will converge to a desired posterior
distribution, so long as the acceptance probability for these draws has a
particular functional dependence on that posterior value (see~\cite{Zhou2018}
for a review).

Many researchers have embedded surrogate methods into MCMC strategies for
parameter optimisation~\cite{Zhang2020,Gong2017}, in particular the ASMO-PODE
algorithm~\cite{Ginting2011} which makes use of MCMC-based adaptive sampling to
attain greater surrogate precision around prospective optima. Our novel approach
draws inspiration from ASMO-PODE, but instead uses MCMC to generate samples
which increase surrogate precision throughout the entire parameter space.

We designed the Quality-Adaptive Surrogate Sampling algorithm (QASS,
\cref{fig:qassplan}) to iteratively increment the training/test set with sample
points which maximise surrogate error and minimise a crowding distance metric
(CDM)~\cite{Solonen2012} in feature space. On each iteration following an initial training of the surrogate on $N$ uniformly random samples, the surrogate was trained and absolute error calculated. MCMC was then performed on the error function generated by performing nearest-neighbor interpolation on these test error points. The resultant samples were culled by $50\%$ according to the CDM, and then the $n$ highest-error candidates were selected for reintegration with the training/test set, beginning another training epoch. Validation was also performed during each iteration on independent, uniformly-random sample sets.

% Adaptive multi-chain MCMC:~\cite{Zhang2012}


\chapter{Spiking Neural Networks}
\label{section:snn}

For an more in depth introduction to spiking neural networks see \cite{Vreeken2003}

Notes:

Spiking neural networks are the third generation of neural network models. They are more realistic and Wolfgang Maass showed that they are in theory computationally more powerful than threshold and sigmoidal gates\cite{Maass1997}.

Generations of neural networks:
1. generation consisted of McCulloch-Pitts threshold neurons
simple model. neuron sends a binary 'high' signal if the sum of its weighted inputs rises above a threshold value
used successfully in multi-layer perceptrons and Hopfield nets. Any function with boolean output can be computed by a multilayer perception with single hidden layer
2. generation neuronal networks
successfully used in deep nn and others

Neurons of the first two generations do not employ individual pulses but their output signals typically lie between 0 and 1. These signals can be seen as normalized firing rates of the neuron within a certain period of time. rate coding. there ,ist ne am averaging window and the outputs of all neurons can be calculated by using the firing rates instead of working with single spikes. Neurons of the second generation are biologically more realisic and powerful than neurons of the first generation. They use a continuous activation function withc allows us to apply a gradient descet learning algorithm like back propagation.

The third generation of neural network once again raises teh level of biological realism by using individual spikes. This allows incorporating spatial-temporal information in communication and computation. These neurons use pulse coding instead of rate coding. Humans can classyfy visual input in under 100ms it takes at leas 10 synapic steps from the retina to the temporal lobe this leaves about 10 ms of processing time per neuron. such a time window is much to little to allow an averagin mechanism like rate coding

Why use snn over other models?
SNN raise the biological plausibility in comparison to ann since they simulate individual spikes instead of using averaged firing frequencys.
On robotic platforms like a snake like robot the energy and computing resources are limited. The human brain needs only 20 W of power  (Drubach, 2000). Yet is able to perform visual pattern analysis and classification in just 100 ms, despite it taking at least 10 synaptic stages (Thorpe et al., 2001).
SNN can use the temporal information, precise timing of events can contain information
SNN allow to invorporate spatial-temporal information that would be lost by averaging over pulse frequencies. This ability is essencial in a environment that is rich with temporal information.

\section{Leaky Integrate and Fire Neuron Model}
% TODO rewrite
% Izhikevich (2004). comparison of different models
The neuron model used in this work is the Leaky integrate and fire neuron model(LIF)\cite{(Stein, 1965)}, a variant of the Integrate-and-Fire model from Burkitt\cite{(Burkitt, 2006)}. It is a widely used model because it is a good trade off between biological plausibility and complexity. It is compared to other models simple since it can be explained using principles form electronics. The model is based on the assumption that timing of spikes rather than the specific shape carries neural information. The sequences of firing times are called spike trains and can be described as

\begin{equation}\label{eq:spikeTrain}
S\left(t\right) = \sum_{f}\delta\left(t - t^f\right)
\end{equation}

where $ f = 1, 2, \dots $ is the label of a spike and $\delta\left(\cdot\right) $ is a Dirac function % TODO explain dirac ? alpha shaped function??

The incoming spike train will trigger the following synaptic electric current

\begin{equation}\label{eq:current}
i\left(t\right) = \int_{0}^{\infty} S_j\left(s - t\right)\exp\left(\frac{-s}{\tau_s}\right)
\end{equation}

The post synaptic current then charges the LIF neuron model increasing the potential u according to

\begin{equation}\label{eq:potential}
\tau_m\frac{du}{dt}\left(t\right)=u_{rest} - u\left(t\right) + R\left(i_0\left(t\right) + \sum w_ji_j\left(t\right) \right)
\end{equation}

where $\tau_m=RC$ is the time constant of the neuron membrane, modeling the voltage leakage depending on the resistance R. The potential after a reset is $u_{rest}$. The external current $i_0\left(t\right)$ drives the neuron state, the input current $i_j\left(t\right)$ from the j-th synaptic input while $w_j$ represents the strength of the connection. Once the membrane potential $u$ reaches a certain firing threshold $\vartheta$ the neuron fires a single spike and its membrane potential is set back to $u_{rest}$. This event is followed by a refracory period in wich the neuron is inactive and can't be charged.

% iaf_psc_alpha - Leaky integrate-and-fire neuron model.
% http://www.nest-simulator.org/helpindex/cc/iaf_psc_alpha.html

% E. M. Izhikevich et. al. "Simple Model of spiking Neurons" 2003
% Hodgking, Huxley 1952 A quantitive description of membrane current and its application to conduction and excition in nerves
%	TODO look up on which paper nest implementation is based

% TODO choose wich one i want to use
\begin{equation}
	\tau_m \frac{dv \left( t \right)}{dt} = - \left( v \left( t \right) - v_{rest} \right) + RI \left( t \right)
\end{equation}

\section{Spike-Timing-Dependent-Plasticity}

Synaptic plasticity is a change of the pre-processing, so it is a different way of saying 'learning'. Hebbian plasticity is a local form of long term potentiation (LTP) and depression (TLD) of synapses based on the correlation of firing activity between pre and post synaptic neurons. Spike Timing Dependent synaptic plasticity (STDP) is a form of hebbian learning that uses the exact spike timing information.

\begin{equation}
	\Delta t = t_{post} - t_{pre}
\end{equation}

\begin{equation}
	\Delta w_+ = A_+ e^{- \frac{\Delta t}{\tau_+}} if \Delta t > 0
\end{equation}

\begin{equation}
	\Delta w_- = A_- e^{- \frac{\Delta t}{\tau_-}} if \Delta t < 0
\end{equation}

\section{Reward-Modulated Spike-Timing-Dependent-Plasticity}