%! program = pdflatex
\documentclass[12pt]{article}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{booktabs}
\usepackage{natbib}
\usepackage{amsmath,bm}
\usepackage{cancel}
\usepackage[colorlinks=true,
            linkcolor=red,
            urlcolor=blue,
            citecolor=black]{hyper ref}
%\usepackage{url, multicol}
%\usepackage{MnSymbol}
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent

% style for partial derivative
\newcommand{\pd}[2]{ \frac{\partial #1}{\partial #2} }
%\newcommand{\pd}[2]{ \partial_{#2}#1 }
\newcommand{\od}[2]{\ensuremath{\frac{d #1}{d #2}}}
\newcommand{\td}[2]{\ensuremath{\frac{D #1}{D #2}}}
\newcommand{\ab}[1]{\ensuremath{\langle #1 \rangle}}
%\newcommand{\dab}[1]{\ensuremath{\llangle #1 \rrangle}}
\newcommand{\bss}[1]{\textsf{\textbf{#1}}}
\newcommand{\ol}{\ensuremath{\overline}}
\newcommand{\olx}[1]{\ensuremath{\overline{#1}^x}}
\newcommand{\nms}{\ensuremath{\mbox{ N m}^{-2}}}
\newcommand{\wms}{\ensuremath{\mbox{ W m}^{-2}}}
\newcommand{\mms}{\ensuremath{\mbox{ m}^2 \mbox{ s}^{-1}}}
\newcommand{\mss}{\ensuremath{\mbox{ m s}^{-2}}}
\newcommand{\ihat}{\hat{\textbf{\i}}}
\newcommand{\jhat}{\hat{\textbf{\j}}}
\newcommand{\orho}{\frac{1}{\rho_0}}

\geometry{letterpaper}

\title{ Southern Ocean Circulation }
\author{Ryan Abernathey}
\date{} % delete this line to display the current date

\begin{document} \maketitle \tableofcontents

\section{Equations and Problem Setup}

We will use the Boussinesq momentum equations to describe a simple model for the southern ocean circulation. For simplicity, we will use Cartesian coordinates. We will be focused particularly on the zonal momentum balance, which reads

\begin{equation}
\td{u}{t}  - f v = - \pd{\phi}{x} + \frac{1}{\rho_0}\pd{\tau^x}{z}
\end{equation}
where $\tau_x$ is the zonal component of the frictional stress. For completeness, we also note the incompressibility equation
\begin{equation}
\pd{u}{x} + \pd{v}{y} + \pd{w}{z} = 0
\end{equation}
hydrostatic balance
\begin{equation}
\pd{\phi}{z} = b
\end{equation}
and the buoyancy equation
\begin{equation}
\td{b}{t} = \pd{B}{z}
\label{eq:b}
\end{equation}
where $B$ represents a vertical non-advective buoyancy flux due to air-sea forcing and turbulent mixing.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=5in]{cartoon/cartoon.pdf}
\caption{Cartoon showing the domain of the idealized ACC problem.}
\label{fig:cartoon}
\end{center}
\end{figure}

\section{Zonal Momentum Balance in Barotropic Model}
If we assume the density is constant ($b=0$), then the hydrostatic balance tells us that pressure doesn't vary with depth. This situation is called {\em barotropic}: ``baro'' means pressure and ``tropic'' means constant. Pressure gradients can come only from the free surface $\eta$: $\phi = g \eta$.

Now consider an integral over the full depth of the water column and across a closed circumpolar path in the $x$ direction. (For simplicity, we assume this path is a latitude circle, but the results generalize to any circumpolar contour.) Assuming a steady state, we find
\begin{equation}
- f \oint \int_{-H}^0 v dz dx = -\oint \int_{-H}^\eta \pd{}{y}(uv) dx dz - \oint \int_{-H}^\eta \pd{\phi}{x} dx dz + \frac{1}{\rho_0}\oint \tau^x_s - \tau^x_b dx
\end{equation}
where $\tau^x_s$ is the frictional stress at the surface and $\tau^x_b$ is the frictional stress at the bottom.
Mass conservations requires that the term on the left-hand side vanish; otherwise mass would be flowing into or out of the Southern Ocean. The first term on the right-hand side is a Reynolds stress; this is generally negligible on the planetary scale, and we will neglect it from here on. However, it may play an important role on smaller scales, such as within the multiple jets of the ACC.

A key question is whether the bottom is flat ($H = const$) or rather depends on $x$ ($H = H(x)$). Application of Leibnitz rule allows us to simplify the pressure term into
\begin{equation}
-\oint \int_{-H}^\eta \pd{\phi}{x} dx dz = -\cancelto{0}{\oint \pd{}{x} \left ( \int_{-H}^\eta \pd{\phi}{x} dz \right ) dx} - \cancelto{0}{\oint \phi_s \pd{\eta}{x} dx} + \oint \phi_b \pd{}{x}(-H)dx
\end{equation}
where $\phi_s$ is the surface pressure and $\phi_b$ is the bottom pressure. The first term cancels exactly via the fundamental theorem of calculus, since the integral is around a closed path. The second term cancels approximately because the surface pressure (the atmospheric load) is negligible. We are left with only the final term, the {\em topographic form stress}.

We can simplify our notation a lot by defining a {\em zonal average}, i.e.
\begin{equation}
\ab{ \ } = \frac{1}{L_x} \oint ( \ ) dx
\end{equation}
which allows us to write the vertically and zonally integrated momentum balance as
\begin{equation}
\ab{\tau^x_s} =  \rho_0 \ab{\phi_b \pd{H}{x}} + \ab{\tau^x_b}
\label{eq:mom}
\end{equation}

The balance in \eqref{eq:mom} was first considered by \citet{MunkPalmen1951}. They wondered whether friction played a strong role in the momentum balance of the ACC. If the bottom were flat, then the bottom frictional stress would have to balance the surface wind stress exactly. To see the consequences of this, we need an explicit formula for the bottom stress. We can use the same quadratic drag law that we used for surface stress, namely
\begin{equation}
\tau_b = \rho_0 C_D u_b^2
\end{equation}
where $u_b$ is the flow in the bottom boundary layer. We can use this to make an estimate for the zonal transport of the ACC. Since the flow was assumed to be barotropic, it cannot vary with depth; $u(z) = u_b$. The zonal transport of the ACC can then be approximated as
\begin{equation}
T_{ACC} = H L_y \sqrt{ \frac{ \ab{\tau^x_s} }{ \rho_0 C_D } }
\end{equation}
where $L_y$ is the meridional extent of the current. Plugging in some rough numbers $H = 4000$ m, $L_y = 800$ km, $\tau_0 = 0.2 \nms$, $\rho_0 = 1030$ kg m$^{-3}$, $C_D = 1.2 \time 10^{-3}$, we find $T_{ACC} \simeq 1200 $ Sv. This is about ten times larger than the observed ACC transport! Although just a rough estimate, this calculation tells us that bottom friction can't be the main sink of momentum in the ACC; it has to be topographic form stress.

Now assuming that $\tau^x_b$ is negligible, what does \eqref{eq:mom} tell us about the relationship between bottom pressure and topography? Since the wind stress is westerly ($\tau^x_s > 0$) over the ACC, we must have a positive correlation between $\phi_b$ and $\partial H / \partial x$. In other words, we need high pressure where the bottom is sloping up towards the east, and low pressure where the bottom is sloping down. A configuration that fulfills this requirement is shown in Fig.~\ref{fig:cartoon}. This configuration generates a form stress term that transfers eastward momentum from the ocean to the solid earth below.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=5in]{cartoon/side.pdf}
\caption{View of the barotropic ACC model from the south, looking towards the equator.}
\label{fig:cartoon_side}
\end{center}
\end{figure}

\section{The Wind-Driven Overturning}

Although we eliminated the coriolis force on the meridional flow ($-fv$) from our budget by integrating over the full depth of the water column, \eqref{eq:mom} also tells us a lot about the meridional overturning circulation. Let's now decompose the vertical integral into three regions: the surface Ekman layer, the interior below the Ekman layer and above the topography, and the abyss below the topographic level.

\begin{alignat}{3}
-f \int_{-h_{Ek}}^\eta \ab{v} dz&& = -f \ab{V_{Ek}} =&& \frac{1}{\rho_0} \ab{\tau^x_s} \\
-f \int_{-H_{min}}^{-h_{Ek}} \ab{v} dz&& =&& 0  \\
-f \int_{-H_{max}}^{-H_{min}} \ab{v} dz&& = -f \ab{V_g} =&& \ab{\phi_b \pd{H}{x}}
\end{alignat}
where $\ab{V_{Ek}}$ is the zonally averaged Ekman transport and $\ab{V_g}$ is the zonally averaged geostrophic transport below topography. Mass conservation can be expressed as $\ab{V_{Ek}} + \ab{V_g} = 0$; the Ekman transport near the surface must be returned by an equal and opposite geostrophic flow in the abyss.

The zonal wind stress is not constant with latitude, but rather decreases southward over the ACC latitudes from its maximum around 50S. This means that $\ab{V_{Ek}}$ also decreases southward, and Ekman pumping is required to supply mass from below. This implies a zonal-mean overturning circulation as shown in Fig.~\ref{fig:cartoon_side}. Mass conservation of the zonally-averaged flow means that $\partial \ab{v} / \partial y + \partial \ab{w} / \partial z$. Consequently, we can express the overturning circulation using a single scalar {\em streamfunction}:
\begin{equation}
\ab{v} = - \pd{}{z}\psi_{Ek} \ , \ \ \ab{w} = \pd{}{y}\psi_{Ek} \ .
\end{equation}
In the ocean interior (i.e.~$-H_{max} < z < h_{Ek}$),
\begin{equation}
\psi_{Ek} = -\frac{\tau_0}{f \rho_0} \ .
\end{equation}
The subscript $_{Ek}$ reminds us that this overturning circulation is fundamentally {\em wind driven} and proportional to the Ekman transport.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=5in]{I8_gam_with_winds.pdf}
\caption{Neutral density surfaces in the Southern Indian Ocean (WOCE line I8), superimposed with arrows showing the sense of the wind-driven Ekman circulation.}
\label{fig:gamma}
\end{center}
\end{figure}

\section{Stratification and Eddy Flux}
This wind-driven overturning circulation $\psi_{Ek}$ describes very strong upwelling in the ACC latitudes. Using typical values of wind stress and other parameters, we can estimate $w_{Ek} \simeq 2.5 \times 10^{-6}$ m/s or about 75 m / year. This upwelling takes place over a strong stratified pycnocline. The question therefore arises: how is this strong advective flux of buoyancy balanced?

In our discussion of advection and diffusion, we considered a one-dimensional balance between vertical advection and diffusion. The zonally-averaged version of this balance is
\begin{equation}
\ab{w} \pd{}{z} \ab{b} = \kappa_v \pd{^2}{z^2} \ab{b}
\label{eq:b1d}
\end{equation}
We found that such a system can be satisfied by a profile of the form
\begin{equation}
\ab{b} = b_0 e^{z/z^\ast}
\end{equation}
where $z^\ast = \kappa_v / \ab{w}$ is a {\em scale depth} parameter, describing how rapidly the stratification changes with depth.

One important characteristic of the Southern Ocean is the relatively deep scale depth of both its stratification and associated thermal wind currents. A section of density in the Southern Indian Ocean (Fig.~\ref{fig:gamma}) illustrates this well. An exponential profile is actually not a bad fit; the ``scale depth'' can be estimated to be approx.~800 m. Plugging in our estimate of $w_{Ek} \simeq 2.5 \times 10^{-6}$ m/s implies that a value of $\kappa_v \simeq 2 \times 10^{-3} \mms$ would be required to satisfy the balance in \eqref{eq:b1d}. Clearly such a value is far beyond the typical level of turbulent mixing in the thermocline ($\kappa_v \simeq 1 \times 10^{-5} \mms$). Therefore another mechanism is required to balance the wind-driven vertical advection of buoyancy.

That other mechanism is widely believed to be mesoscale eddy transport. It turns out that \eqref{eq:b1d} is an inadequate description of the buoyancy budget in the Southern Ocean; it neglects strong correlations between transient and small-scale motions which can contribute substantial buoyancy fluxes. To understand how these correlations arise, we must use a buoyancy budget that properly accounts for eddy fluxes.

We start by taking a time and zonal average of \eqref{eq:b}. After some algebra, we can reduce the result to
\begin{equation}
\ab{\ol{v}} \pd{}{y} \ab{\ol{b}} + \ab{\ol{w}} \pd{}{z} \ab{\ol{b}} = - \pd{F^y_b}{y}  - \pd{F^z_b}{z} + \pd{}{z}\ab{\ol{B}}
\label{eq:b1}
\end{equation}
where
\begin{align}
F^y_b = &\ab{\ol{v'b'}} + \ab{v^\dagger b^\dagger} \\
F^z_b = & \ab{\ol{w'b'}} + \ab{w^\dagger b^\dagger}
\end{align}
are the eddy fluxes of buoyancy in the meridional and zonal directions. We have introduced the symbol $\ol{( \ )}$ to indicate a time average. The deviations from the time average are indicated by a prime, and the deviations from the time and zonal average are indicated by a dagger, i.e.
\begin{equation}
a' = a - \ol{a} \ , \ \ \ a^\dagger = \ab{\ol{a}} - \ol{a} \ .
\end{equation}
The prime terms are associated with {\em transient} eddy fluctuations (turbulent flow), while the dagger terms are associated with {\em stationary} eddies, a.k.a.~steady meanders of the ACC. Both types of motion can transport buoyancy meridionally and vertically. If the zonal average is reinterpreted as a ``streamwise'' average around a circumpolar contour which follows the ACC path, the dagger terms will vanish. In practice, the buoyancy budget is generally dominated by the vertical flux terms, i.e.
\begin{equation}
\ab{\ol{w}} \ab{\ol{b}} \simeq -\ab{\ol{w'b'}} \ .
\end{equation}

In our discussion of advection and diffusion, we learned that mesoscale eddies tend to transport properties {\em along} rather than across isopycnals. This results from fundamental dynamical and energetic constrains on the eddy motion. As a result, the eddy buoyancy flux is oriented {\em perpendicular} to the mean buoyancy gradient, i.e.
\begin{equation}
\frac{F^z_b}{F^y_b} \simeq - \frac{\pd{}{y} \ab{\ol{b}}}{\pd{}{z} \ab{\ol{b}} } \ .
\label{eq:adiabatic}
\end{equation}
When \eqref{eq:adiabatic} is satisfied, the eddy fluxes are usually termed ``adiabatic.'' Given this fact, an ingenious way to simplify \eqref{eq:b1} emerges if we make the following definitions:
\begin{equation}
\psi_{eddy} = - \frac{F^z_b}{\pd{}{y} \ab{\ol{b}}}
\label{eq:psi_eddy}
\end{equation}
and
\begin{equation}
\psi_{res} = \psi_{Ek} + \psi_{eddy} \ , \ \ v_{res} = - \pd{}{z}\psi_{res} \ , \ \ w_{res} = \pd{}{y}\psi_{res}
\end{equation}
Using \eqref{eq:adiabatic}, we can now write \eqref{eq:b1} as
\begin{equation}
v_{res} \pd{}{y} \ab{\ol{b}} + w_{res} \pd{}{z} \ab{\ol{b}} =  \pd{}{z}\ab{\ol{B}} \ .
\label{eq:bres}
\end{equation}

In \eqref{eq:psi_eddy}, we defined an {\em eddy-induced} overturning streamfunction $\psi_{eddy}$. We then defined a {\em residual} overturning streamfunction $\psi_{res}$ as the sum of this and the wind-driven overturning. What this means is that, when the eddy fluxes are adiabatic and \eqref{eq:adiabatic} is satisfied, we can represent their contribution to the buoyancy budget as {\em advection}, not diffusion! The eddy-induced overturning has the opposite sense as the wind-driven component; hence the net overturning is a small residual ($\psi_{res}$ between the two opposing terms.

It was clear that the wind stress determines $\psi_{Ek}$, but it is not so clear what determines $\psi_{res}$. In general this is a difficult problem, since in involves mesoscale eddy turbulence. However, there is one powerful constraint we can use: the air-sea buoyancy flux.

Assume there is a surface mixed layer of depth $h_m > h_{Ek}$, within which $\partial b / \partial z = 0$. At the surface ($z=0$), both $\psi_{Ek}$ and $\psi_{eddy}$ go to zero. We will further assume that $B$, the vertical buoyancy flux due to mixing, is vanishingly small in the interior---this is consistent with weak values of $\kappa_v$ that have been measured in the upper Southern Ocean. We can then integrate \eqref{eq:bres} in the vertical over the surface mixed layer to find
\begin{equation}
\psi_{res}(z=h_m) \pd{}{y} \ab{\ol{b_s}} = \ab{\ol{B_s}}
\label{eq:psi}
\end{equation}
where $b_s$ is the surface buoyancy and $B_s$ is the surface air-sea buoyancy flux.

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=5in]{moc.png}
\caption{Idealized view of Southern Ocean residual circulation and mixing.}
\label{fig:cartoon_side}
\end{center}
\end{figure}

This equation is very powerful: it tells us that {\em the strength of the residual circulation as the base of the mixed layer is proportional to the air-sea buoyancy flux}. This actually expresses something quite intuitive: that the residual overturning can only cross isopycnals where forcing / mixing processes can modify its density. To see this more clearly, first consider \eqref{eq:bres} in the ocean interior, where $B \simeq 0$. In that case we have
\begin{equation}
v_{res} \pd{}{y} \ab{\ol{b}} + w_{res} \pd{}{z} \ab{\ol{b}} = 0 \ .
\end{equation}
The residual flow vector in the $(y, z)$ plane is {\em parallel} to the buoyancy surfaces; the flow cannot cross isopycnals without mixing. Instead, in the surface layer, the presence of air-sea forcing allows water to become lighter or denser as it moves through the MOC.

As depicted in Fig.~\ref{fig:cartoon_side}, there are generally three different regions of buoyancy flux in the Southern Ocean.
\begin{itemize}
\item Closest to Antarctica, there is net buoyancy loss due to strong cooling as well as ice-driven freshwater export (brine rejection). Much of this buoyancy flux occurs in winter polynyas and is consequently extremely hard to observe directly. Nevertheless, we know it must exist, because of the existence of the Antarctic Bottom Water (AABW) cell, which requires such a buoyancy flux in order for lighter waters to be transformed to dense abyssal water.
\item Over the ACC, there is a net buoyancy gain, largely due to latent and sensible warming of the ocean. One way of explaining this heat flux is that the Ekman upwelling brings cold deep water into contact with warm surface water; this explanation is only partially satisfying, since we have seen that the residual overturning also includes an eddy contribution. Precipitation also freshens the surface. In any case, water indisputably gains buoyancy in the ACC latitudes, which allows the residual MOC to flow north, across the surface buoyancy gradient.
\item North of the ACC, there is another region of buoyancy loss. This buoyancy loss is associated with the formation and subduction of mode and intermediate waters.
\end{itemize}

%\section{Observations}
%
%The model we have considered here is extremely simple. This simplicity allowed us to reach a physical description of how the Southern Ocean overturning circulation works.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bibliographystyle{ametsoc}
\bibliography{references}
\end{document}


\section{Growth stages through late times} \slabel{late}

\begin{figure*}
\begin{subfigure}[b]{\columnwidth}
\includegraphics[width=\columnwidth]{figs/Cascade-short-4-1}
\caption{Early times}
\end{subfigure}
\begin{subfigure}[b]{\columnwidth}
\includegraphics[width=\columnwidth]{figs/Cascade-4-1}
\caption{Late times}
\end{subfigure}
\caption{ \flabel{high_Ra_traj}
Bubble Froude number vs non-dimensional bubble height for $\text{Ra} = 10^{5.75}, \text{Sc} = 4$, simulation vs model with successive terms enabled: first $C_4, C_6$ and $C_8$, then $C_3$, then $C_1$, then $C_2$, and finally $C_5$ and $C_7$.
The dashed vertical lines divide the trajectory into three regimes: linear growth for $H/\lambda < .05$, saturation until $H / \lambda \approx 8$, and viscosity beyond that.
The stagnation and re-acceleration transients are seen begining at $H/\lambda = 0.5$ and ending by $H / \lambda = 1.5$.
}
\end{figure*}

\begin{figure*}
\begin{subfigure}[b]{\columnwidth}
\includegraphics[width=\columnwidth]{figs/Cascade-short-32-4}
\caption{Early times}
\end{subfigure}
\begin{subfigure}[b]{\columnwidth}
\includegraphics[width=\columnwidth]{figs/Cascade-32-4}
\caption{Late times}
\end{subfigure}
\caption{ \flabel{low_Ra_traj}
Bubble Froude number vs non-dimensional bubble height for $\text{Ra} = 10^{4.5}, \text{Sc} = 8$, simulation vs model with successive terms enabled: first $C_4, C_6$ and $C_8$, then $C_3$, then $C_1$, then $C_2$, and finally $C_5$ and $C_7$.
Dashed vertical lines divide the trajectory into four regimes: linear growth for $H/\lambda < 0.05$, saturation until $H / \lambda \approx 0.1$, viscosity until $H / \lambda \approx 1.3$, and diffusion beyond that.
The stagnation and re-acceleration transients, seen in \fref{high_Ra_traj}, are suppressed by the viscous regime, which onsets before the transient begins at $H / \lambda = 0.5$.
}
\end{figure*}

Single mode experiments have been limited to bubble heights of $1.8\lambda$~\cite{Wilkinson2007}.
Simulations have reached in $4\lambda$ in 3D~\cite{Ramaprabhu2012} and $9\lambda$ in 2D~\cite{Wei2012}.
Here, we present trajectories that continue up to $17\lambda$, for example \fref{high_Ra_traj}.
However, the more dissipative bubbles stop rising at lower aspect rations, for example \fref{low_Ra_traj}.
Plotted with the non-dimensional bubble height, $h / \lambda$, and nondimensional mixed volume, $M / \lambda^3$ is the non-dimensional velocity, or Froude number, which is defined as:
\begin{equation}
  \text{Fr} = \frac{v}{\sqrt{A g \lambda}}
\end{equation}
The dissipation in the bubble is characterized by the Rayleigh number:
\begin{equation}
  \text{Ra} = \text{Gr} \text{Sc} = \frac{A_0 g \lambda^3}{\nu D}
\end{equation}

In the spirit of recent analyses~\cite{Ramaprabhu2012, Wei2012}, we try to identify distinct growth regimes.
To do so, we consider the behavior of the simple model with coefficients set to zero.
Initially, only $C_4$, $C_6$, and $C_8$ affect bubble dynamics.
The growth is exponential with a rate in agreement with the linear theory, so we term this the linear regime.
As the bubble grows, the $C_3$ term reduces the growth rate to a limiting value of $A g / C_3$.
Because this represents the transition from exponential growth to free-fall, we term it the saturation regime.
The $C_1$ term has the same qualitative effect as the $C_3$ term, so it doesn't distinguish a unique regime.
The $C_2$ term does impose a limiting velocity scale, so it departs significantly from the $C_3$ dynamics in the viscous regime.
Finally, the $C_5$ term, balanced by the $C_7$ term, mix the fluid and reduce the effective Atwood number in the diffusive regime.
Because the relative onset of the viscous and diffusive regimes depends on the Schmidt number, they are grouped together into the dissipative regime.
Ultimately, the bubble stops rising.
The bubble height at this point, which is also the maximum bubble height, is called the penetration depth.

\subsection{Exponential growth}
When the amplitude is small and the interface is thin, the linear theory and numerical results identify exponential growth: $\ddot{h} = \gamma^2 h$.
Similarly, in the limit $h, \delta \rightarrow 0$, the simple model yields a growth rate:
\begin{equation}
\gamma = \sqrt{\frac{A_0 g k}{2 \pi C_4(1 + C_8 / C_6 \pi^{1/2} k \delta)}},
\end{equation}
which differs from the linear theory by the absence of a $-D k^2$ term.
This is the only term that can cause unstable interfaces to decay, i.e. $\gamma < 0$ for positive Atwood numbers.
In the simple model, all unmixed bubbles grow while in the linear theory highly diffusive bubbles decay.

The other terms, \ie those scaled by $C_1, C_2, C_3, C_5$ and $C_7$, can be omitted while the description of the exponential growth is unaffected.

\subsection{Saturation regime}
The exponential growth saturates as the bubble height increases.
In the simple model, this captured by the $C_3$ term:
\begin{equation}
\ddot{h} = \frac{A g}{(C_3 h + C_4 \lambda) (1 + C_8 / C_6 \pi^{1/2} k \delta)},
\end{equation}
which becomes significant when $h \approx C_4 \lambda / C_3$.
We will find in the following section that $C_3$ takes a value of about 1, so, omitting viscous corrections, saturation halves the growth rate at $h \approx \lambda / (2\pi)$.

The definition of the start of the saturation regime is somewhat arbitrary.
Here, we propose the definition $h/\lambda < 0.05$ as it is where the exponential and saturated plots of the Froude number versus height visually deviate.
The important thing is that this threshold is independent of $Ag$, the Grashof number, and the Rayleigh number.
Within the saturation regime, the acceleration takes a limiting value of $Ag$, defining a saturation velocity:
\begin{equation} \elabel{vel_sat}
v_s \sim \sqrt{A g h}
\end{equation}

\subsection{Viscous regime}
As the bubble grows, so does its surface area.
The viscous drag, which scales linearly with the bubble height and bubble velocity, ultimately balances the buoyancy to limit the velocity, as in \eref{visc_vel}.
The scaling of the onset of this regime can be found by equating the saturation velocity and the viscous velocity:
\begin{equation}
\sqrt{A g h} \sim \frac{A g \lambda^2}{\nu}.
\end{equation}
Solving for $h/\lambda$ yields:
\begin{equation}
\frac{h_\nu}{\lambda} \sim \frac{A g \lambda^3}{\nu^2} = \text{Gr},
\end{equation}
where $h_\nu$ is the onset of the viscous regime.

As in the saturation case, the particular definition of the onset is arbitrary.
Here, we will define $h_\nu$ such that when $h_\nu/\lambda = 0.5$, the viscous velocity has the potential flow value: $v_\nu = \pi^{-1/2} \sqrt{A g \lambda}$.
This works out to be:
\begin{equation} \elabel{visc_height}
\frac{h_\nu}{\lambda} = \frac{\pi}{4 C_2^2} \text{Gr} \approx 4.7 \times 10^{-5} \text{Gr},
\end{equation}
where we've let $C_2 = 128$, the nominal value from Poiseuille flow.

\subsection{Diffusive regime}
\begin{figure}
\includegraphics[width=\columnwidth]{figs/PenetrationDepth-vs-Rayleigh-Schmidt}
\caption{ \flabel{depth_scatter}
  Penetration depth, non-dimensionalized, vs the Rayleigh and Schmidt numbers.
  The dashed line separates completed from incomplete trajectories, which are clipped at $h/\lambda \approx 23$.
}
\end{figure}

\begin{figure}
\includegraphics[width=\columnwidth]{figs/Depth-vs-Rayleigh}
\caption{ \flabel{depth_line}
  Penetration depth, non-dimensionalized, vs the Rayleigh.
  The dashed line is a best fit line $h/\lambda = 2.41 \times 10^{-4} \text{Ra} - 0.36$.
}
\end{figure}

The rate of diffusion across the surface of the bubble also scales with the bubble height.
When the viscous drag limits the bubble's velocity, the flux of pure fluid into the bubble, which goes as the velocity, is unable to match the flux of mixed fluid through the interface.
Mixing dilutes the buoyant fluid, reducing the Atwood number and therefore the bubble velocity.
Ultimately, the Atwood number reaches zero and eventually the bubble stops rising.

The penetration depth is the maximum height of the bubble, which is the height of the bubble at the stopping condition of this analysis.
We can estimate the scaling of the penetration depth as the product of a characteristic velocity with a characteristic time-scale.
The late-time velocity is the viscous velocity, $v_\nu$, while time-scale is given by diffusion:
\begin{equation}
\tau_D = \frac{\lambda^2}{D}.
\end{equation}
Combining and non-dimensionalizing yields:
\begin{equation}
\frac{h(\infty)}{\lambda} = \frac{A g \lambda^3}{\nu D} = \text{Ra},
\end{equation}
so the penetration depth should go linearly with the Rayleigh number.

The penetration depth is plotted as a function of Rayleigh and Schmidt number in \fref{depth_scatter}, and, indeed, depends strongly on the Rayleigh number before being clipped by the top walls.
Furthermore, the relationship to the Rayleigh number is linear over the cases shown here, as shown in \fref{depth_line}.

We can use a similar analysis to define the onset of the diffusive regime when the interface width is a quarter wavelength, $\delta = \lambda / 8$, which is a quarter of the nominal bubble diameter.
This results in a bubble height:
\begin{equation} \elabel{diff_height}
\frac{h_D}{\lambda} = \frac{1}{128 C_2} \text{Ra} \approx 6.1 \times 10^{-5} \text{Ra},
\end{equation}
where we've again let $C_2 = 128$.

The ratio of the diffusive to viscous heights, \eref{diff_height} and \eref{visc_height}, respectively, is linear with the Schmidt number, or about $1.3 \text{Sc}$.
For $\text{Sc} > 1$, the portion of the trajectory that is governed by viscosity alone increases with the Schmidt number.
However, for $\text{Sc} < 1$ the diffusive regime dominates the viscous regime entirely.

\subsection{Stagnation and re-acceleration}

Absent from the previous discussion is the stagnation and re-acceleration of the bubble around $h / \lambda = 1$, as seen in \fref{high_Ra_traj}.
There are no terms in the buoyancy-drag model capable of producing an inflection point in the Froude number vs bubble height, so stagnation and re-acceleration cannot be controlled by turning a model coefficient on or off.
This suggests that the buoyancy-drag model is missing a term.

However, the buoyancy-drag model does have a limiting velocity, the viscous velocity $v_\nu$.
When the viscous velocity is near or below the stagnation velocity, $\text{Fr} \approx \pi^{-1/2}$, saturation and re-acceleration are suppressed.
Otherwise, stagnation and re-acceleration temporarily interrupt the saturation regime.
The stagnation and re-acceleration is a transient regime that only occurs at sufficiently high Grashof numbers.


% !TEX root = ../main.tex
\section{Introduction}
\label{sec:pvi-introduction}
\Gls{VI} is a powerful method for probabilistic modeling.  \gls{VI} uses optimization to approximate difficult-to-compute conditional distributions~\citep{jordan1999introduction}.  In its modern incarnation, it has scaled Bayesian computation to large data sets~\citep{hoffman2013stochastic}, generalized to large classes of models~\citep{kingma2014autoencoding,ranganath2014black,Rezende:2015}, and has been deployed as a computational engine in probabilistic programming systems~\citep{DBLP:journals/corr/MansinghkaSP14,Kucukelbir:2015,tran2016edward}.

Despite these significant advances, however, \gls{VI} has drawbacks. For one, it tries to iteratively solve a difficult nonconvex optimization problem and its objective contains many local optima. Consequently, \gls{VI} is sensitive to initialization and easily gets stuck in a poor solution.  We develop a new optimization method for \gls{VI} and show that it finds better optima.

Consider a probability model $p(\mbz,\mbx)$ and the goal of calculating the posterior $p(\mbz \mid \mbx)$. The idea behind \gls{VI} is to posit a family of distributions over the hidden variables $q(\mbz ; \mblambda)$ and then fit the variational parameters $\mblambda$ to minimize the \gls{KL} divergence between the approximating family and the exact posterior, $\textsc{kl}(q(\mbz ; \mblambda) || p(\mbz \mid \mbx))$. The \gls{KL} is not tractable so \gls{VI} optimizes a proxy.  That proxy is the
\gls{ELBO},
\begin{align}
\cL(\mblambda) = \E[\log p(\mbz,\mbx)] - \E[\log q(\mbz ; \mblambda)],
\label{eq:ELBO}
\end{align}
where expectations are taken with respect to $q(\mbz ; \mblambda)$. Maximizing the \gls{ELBO} with respect to $\mblambda$ is equivalent to minimizing the \gls{KL} divergence.
\input{ch-pvi/fig_arrows}
The issues around \gls{VI} stem from the \gls{ELBO} and the iterative algorithms used to optimize it. When the algorithm zeroes (or nearly zeroes) some of the support of $q(\mbz ; \mblambda)$, it becomes hard to later ``escape,'' i.e., to add support for the configurations of the latent variables that have been assigned zero probability~\citep{mackay2003information, Burda2016}. This leads to poor local optima and to sensitivity to the starting point, where a misguided initialization will lead to such optima. These problems happen in both gradient-based and coordinate ascent methods. We address these issues with \glsreset{PVI} \gls{PVI}, a variational inference algorithm that is specifically designed to avoid poor local optima and to be robust to different initializations.

\gls{PVI} builds on the proximity perspective of gradient ascent.  The proximity perspective views each step of gradient ascent as a constrained minimization of a Taylor expansion of the objective around the previous step's parameter~\citep{Spall:2003,boyd2004convex}.  The constraint, a \textit{proximity constraint}, enforces that the next point should be inside a Euclidean ball of the previous.  The step size relates to the size of that ball. We construct \gls{PVI} by questioning whether such a Euclidan distance-based constraint is appropriate, and whether other notions of proximity may be useful in constraining gradient ascent steps.

In \gls{VI}, a constraint on the Euclidean distance means that all dimensions of the variational parameters are equally constrained. We posit that this leads to problems; some dimensions need more regularization than others. For example, consider a variational distribution that is Gaussian.  A good optimization will change the variance parameter more slowly than the mean parameter to prevent rapid changes to the support.  The Euclidean constraint cannot enforce this. Furthermore, the constraints enforced by gradient descent are transient; the constraints are relative to the previous iterate---one poor move during the optimization can lead to permanent optimization problems.

To this end, \gls{PVI} uses proximity constraints that are more meaningful to variational inference and to optimization of probability parameters.  A constraint is defined using a proximity statistic and distance function. As one example, we consider a constraint based on the entropy proximity statistic. This limits the change in entropy of the variational approximation from one step to the next.  Consider again a Gaussian approximation. The entropy is a function of the variance alone and thus the entropy constraint counters the pathologies induced by the Euclidean proximity constraint. We also study constraints built from other proximity statistics, such as those that penalize the rapid changes in the mean and variance of the approximate posterior.

\Cref{fig:bernoulli_arrows} provides an illustration of the advantages of \gls{PVI}.  Our goal is to estimate the parameters of a factor analysis model with variational inference, i.e., using the posterior expectation under a fitted variational distribution.  We run variational inference $100$ times, each time initializing the estimates (the model parameters) to a different position on a ring around the truth.

In the figure, red points indicate the true value. The start locations of the green arrows indicate the initialized estimates. Green points indicate the final estimates, after optimizing from the initial points. Panel (a) shows that optimizing the standard \gls{ELBO} with gradients leads to poor local optima and misplaced estimates.  Panel (b) illustrates that regardless of the initialization, \gls{PVI} with an entropy proximity statistic finds estimates that are close to the true value.

The rest of this chapter is organized as follows. \Cref{sec:variational_inference} reviews variational inference and the proximity perspective of gradient optimization.  \Cref{sec:pvi} derives \gls{PVI}; we develop four proximity constraints and two algorithms for optimizing the \gls{ELBO}. We study four models in \Cref{sec:pvi-experiments}: a Bernoulli factor model, a sigmoid belief network \citep{Mnih:2016:VIM:3045390.3045621}, a variational autoencoder~\citep{kingma2014autoencoding,rezende2014stochastic}, and a deep exponential family model of text~\citep{ranganath2015deep}. \gls{PVI} outperforms classical methods for variational inference.

\input{ch-pvi/sec_related}
\section{Variational Inference}
\label{sec:variational_inference}

Consider a model $p(\mbx, \mbz)$, where $\mbx$ is the observed data and $\mbz$ are the latent variables.  As described in \Cref{sec:pvi-introduction}, \gls{VI} posits an approximating family $q(\mbz; \mblambda)$ and maximizes the \gls{ELBO} in \Cref{eq:ELBO}. Solving this optimization is equivalent to finding the variational approximation that minimizes \gls{KL} divergence to the exact posterior \citep{jordan1999introduction,wainwright2008graphical}.

\subsection{Gradient Ascent has Euclidean Proximity}
\label{sec:euclidean_proximity}
Gradient ascent maximizes the \gls{ELBO} by repeatedly following its gradient.  One view of this algorithm is that it repeatedly maximizes the linearized \gls{ELBO} subject to a proximity constraint on the current variational parameter~\citep{Spall:2003}. The name `proximity' comes from constraining subsequent parameters to remain close in the proximity statistic. In gradient ascent, the proximity statistic for the variational parameters is the identity function $f(\mblambda) = \mblambda $, and the distance function is the square difference.

Let $\mblambda_t$ be the variational parameters at iteration $t$ and $\rho$ be a constant. To obtain the next iterate $\mblambda_{t+1}$, gradient ascent maximizes the linearized \gls{ELBO},
\begin{align}
  \label{eq:linearized-elbo}
  % \begin{split}
    U(\mblambda_{t+1}) = %&
    \cL(\mblambda_t) +
    \nabla\cL(\mblambda_t)^\top(\mblambda_{t+1}-\mblambda_t) %\\
     - %&
    \frac{1}{2\rho} (\mblambda_{t+1}-\mblambda_t)^\top
    (\mblambda_{t+1}-\mblambda_t).
  % \end{split}
\end{align}
Specifically, this is the linearized \gls{ELBO} around $\mblambda_t$, subject to $\mblambda_{t+1}$ being close to $\mblambda_t$ in squared Euclidean distance.

Finding the $\mblambda_{t+1}$ which maximizes \Cref{eq:linearized-elbo} yields
\begin{align}
  \mblambda_{t+1} = \mblambda_t + \rho
  \nabla\cL(\mblambda_t). \label{eq:standard_update}
\end{align}
This is the familiar gradient ascent update with a step size of $\rho$. The step size $\rho$ controls the radius of the Euclidean ball which demarcates valid next steps for the parameters.  Note that the Euclidean constraint between subsequent iterates is implicit in all gradient ascent algorithms.

\subsection{An Example where Variational Inference Fails}
\label{sec:bernoulli_factor_model}
We study a setting where variational inference suffers from poor local optima. Consider a factor model, with Bernoulli latent variables and Gaussian likelihood:
\begin{align}
z_{ik}~&\sim~\textrm{Bernoulli}(\pi) \\
x_i~&\sim~\textrm{Gaussian}\left(\mu = \textstyle \sum_k z_{ik}
        \mu_k, \sigma^2=1\right).
\end{align}
This is a ``feature'' model of real-valued data $x$; when one of the features is on (i.e., $z_{ik}=1$), the $i$th mean shifts according the that feature's mean parameter (i.e., $\mu_{k}$).  Thus the binary latent variables $z_{ik}$ control which cluster means $\mu_k$ contribute to the distribution of $x_i$.

The Bernoulli prior is parametrized by $\pi$; we choose a Bernoulli approximate posterior $q(z_k;\lambda_k)~=~\textrm{Bernoulli}(\lambda_{k})$.  A common approach to \gls{VI} is coordinate ascent~\citep{bishop2006pattern}, where we iteratively optimize each variational parameter.  The optimal variational parameter for $z_{ik}$ is
\begin{align}
  \label{eq:z_update} \lambda_{ik} \propto
   \exp \left\{\E_{-z_{ik}} \left[- \frac{1}{2\sigma^2}(x_i -
    \sum_{j} z_{ij} \mu_j)^2\right]\right\}.
\end{align}
We can use this update in a variational expectation-maximization setting.  The
corresponding gradient for $\mu_k$ is
\begin{align}
  \label{eq:mu-gradient}
  \frac{\partial \cL}{\partial\mu_k}
  &= -
    \frac{1}{\sigma^2}\sum_i
    \left(-x_i \lambda_{ik} + \lambda_{ik} \mu_k
    + \lambda_{ik} \sum_{j \neq k}\lambda_{ij}\mu_j\right).
\end{align}
Meditating on these two equations reveals a deficiency in mean field variational inference.  First, if the mean parameters $\mu$ are initialized far from the data then $q^*(z_{ik} = 1)$ will be very small.  The reason is in \Cref{eq:z_update}, where the squared difference between the data $x_i$ and the expected cluster mean will be large and negative.  Second, when the probability of cluster assignment is close to zero, $\lambda_{ik}$ is small.  This means that the norm of the gradient in \Cref{eq:mu-gradient} will be small. Consequently, learning will be slow. We see this phenomenon in \Cref{fig:bernoulli_arrows} (a). Variational inference arrives at poor local optima and does not recover the correct cluster means.


\documentclass[class=report, float=false, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}

\input{preamble}

\graphicspath{{figures/images/}}

% \begin{cbunit}

\begin{document}

\chapter{Quaternions}
\label{chap:quaternions}

Our goal here is to present quaternions and how they can be implemented in numerical simulations to solve rigid body rotation equations.

\section{Interest of quaternions}

\subsection{Quaternions}

Please find in \hyperref[appendix:quaternions]{appendix \ref{appendix:quaternions}} a complete presentation of ellipsoids.\\

Quaternions are an extension of complex numbers which can be written
\begin{equation}
q = q_0 + \sum_{i=1}^{3}q_i e_i\text{, with } \begin{cases} e_m^2 = -1& \\ e_me_n = \varepsilon_{mnk} e_k&\text{, } m \neq n \end{cases} \text{ and } q_0,q_1,q_2,q_3 \in \mathbb{R}
\end{equation}
and can therefore be associated to $\mathbb{R}^4$ vectors.\\

We can show that any rotation in the 3-dimensional space can be expressed as the action of some unit quaternion, \textit{i.e.} a quaternion associated to a vectos of the unitary hypersphere in $\mathbb{R}^4$.

\subsection{Theoretical interest}

The intrinsic geometry of the rotation group is that of a sphere, therefore it is more natural to use quaternions to represent rotations like it is more natural rectangular coordinates to represent translations \cite{shoemake1985animating}.\\

Furthermore, quaternions allow us to represent the orientation with a single vector and are safe from gimbal lock which results from the alignment of two of the three axes of rotation, causing the loss of one degree of rotational freedom, while Euler angles and rotation matrix are not \cite{munjiza2009application}.

\subsection{Numerical interest}
\label{quaternions_interest}

Computationally speaking, resorting to quaternions is more efficient than resorting to rotation matrix or axis-angle representations \cite{eberly2002rotation}.\\

A rotation can be expressed with 4 floats when using a quaternion while it would need 6 floats with the axis-angle representation and 9 floats with the rotation matrix representation. Moreover, the composition of two rotations can be done with 12 additions and 16 multiplications when using quaternions while it would need 18 additions and 27 multiplications when using rotation matrix. Composing rotations when using axis-angle representation is way harder since it would need to convert this representation to a rotation matrix or a quaternion, do the composition, and then extract the axis angle-pair.\\

However, when using quaternions, the rotation of $n$ vectors needs -- at least \cite{eberly2002rotation} -- $12 + 6n$ additions and $12 + 9n$ multiplications and this result is achieved by converting the quaternion to a rotation matrix.\\

Quaternions are also more resistant to rounding errors. Indeed, while a slightly off quaternion still represents a rotation after being normalised, one may have a hard time to convert back a slightly off orthogonal matrix to a proper orthogonal matrix \cite{wiki:quaternions}.\\

Furthermore, quaternions are used in many fields where it is essential to interpolate rotations smoothly -- in video games for example, \textit{Tomb Raider} (1996) being the first mass-market computer game to have used quaternions to achieve smooth 3D rotation \cite{bobick1998rotating}, or aircraft kinematics \cite{phillips2001review} -- something which is difficultly achievable with the means of rotation matrix or Euler angles.

\section{Rotation of a rigid body}

\subsection{Equation of conservation of the angular momentum}

Please find in \hyperref[appendix:euler]{appendix \ref{appendix:euler}} the extensive demonstrations of the equations presented in this part.\\

Consider a body $\mathcal{O}$ of angular momentum $\vec{A} = \bar{\bar{I}}\vec{\omega}$, with $\bar{\bar{I}}$ its tensor of moments of inertia and $\vec{\omega}$ its rotation vector. If the total torque $\vec{M}$ is exerted on $\mathcal{O}$, then its angular momentum varies as
\begin{equation}
\boxed{\frac{d}{dt}\vec{A} = \frac{d}{dt}\bar{\bar{I}}\vec{\omega} = \vec{M}}
\label{TMC_}
\end{equation}
where both $\bar{\bar{I}}$ and $\vec{\omega}$ vary as a function of the time.\\

In the base $(\vec{u_i})_{i=1:3}$ formed by the axis of inertia of the body, the tensor of inertia is constant and diagonal, therefore
\begin{align*}
\vec{A} = \sum_{k=1}^3 \underbrace{\bar{\bar{I}}\vec{u_k}}_{I_k} \underbrace{\vec{\omega}\cdot\vec{u_k}}_{\omega_k} \vec{u_k}
\end{align*}
which allows us to write
\begin{equation}
\boxed{\forall k \in \llbracket1,3\rrbracket,~ I_k \dot{\omega_k} + \varepsilon_{k,l,m}\omega_lI_m\omega_m = M_k}
\label{euler_rot_}
\end{equation}

\subsection{Expression with quaternions}

If $\vec{r}(t)$ denotes the orientation of the body in the world frame as a function of time then it can be obtained through a 3D rotation from $\vec{r}(0)$ \cite{rapaport1985molecular}. This rotation can be expressed with an unit quaternion $q(t)$ such that
\begin{equation}
\boxed{\vec{r}(t) = q(t)\vec{r}(0)q^*(t)}
\label{orientation}
\end{equation}
We have that $\vec{\omega}(t)$ satisfies $\dot{\vec{r}}(t) = \vec{\omega}(t)\times\vec{r}(t)$ and we can infer from equation \ref{orientation} that
\begin{align*}
\dot{\vec{r}}(t) &= \dot{q}(t)\vec{r}(0)q^*(t) + q(t)\vec{r}(0)\dot{q}^*(t)\\
&= \dot{q}(t)q^*(t)\vec{r}(t)\cancel{q(t)q^*(t)} + \cancel{q(t)q^*(t)}\vec{r}(t)q(t)\dot{q}^*(t)
\end{align*}
We know that $\frac{d}{dt} qq^* = \frac{d}{dt} \underbrace{N^2(q)}_{=1} = 0 = \dot{q}q^* + q\dot{q}^*$ so $q\dot{q}^* = - \dot{q}q^*$ and then
\begin{align*}
\dot{\vec{r}}(t) = \underbrace{\dot{q}(t)q^*(t)}_{g(t)}\vec{r}(t) - \vec{r}(t)\underbrace{\dot{q}(t)q^*(t)}_{g(t)}
\end{align*}
Moreover, the scalar part in the above equation equals to 0. We can then identify $g(t)$ to an $\mathbb{R}^3$ vector $\vec{g}(t)$ and write
\begin{align*}
\dot{\vec{r}}(t) &= \vec{g}(t)\times\vec{r}(t) - \vec{r}(t)\times\vec{g}(t)\\
&= 2\vec{g}(t)\times\vec{r}(t)
\end{align*}
By identification, it appears that $\vec{\omega}(t) = 2\vec{g}(t) = 2\dot{q}(t)q^*(t)$. We know that $\vec{\omega'}(t) = q^*(t)\vec{\omega}(t)q(t)$, therefore
\begin{equation}
\boxed{\vec{\omega'}(t) = 2q^*(t)\dot{q}(t)}
\label{omega}
\end{equation}

We now have an equation which links the rotation vector $\vec{\omega'}(t)$ and the rotation quaternion $q$ \cite{mandre}. Equation \ref{euler_rot_} showed that the first derivative of $\vec{\omega'}(t)$ is linked to the sum $\sum \vec{M'}$ of the moments exerted on the body and expressed in the body frame. We should then be able to have such a link between these moments and the quaternion $q$
\begin{align*}
\dot{\vec{\omega'}}(t) &= 2(\dot{q}^*(t)\dot{q}(t) + q^*(t)\ddot{q}(t))\\
\Leftrightarrow \ddot{q}(t) &= \frac{1}{2}(q(t)\dot{\vec{\omega'}}(t) - 2q(t)\underbrace{\dot{q}^*(t)\dot{q}(t)}_{N^2(\dot{q}(t))})\\
&= q(t)\left(\frac{1}{2}\dot{\vec{\omega'}} - N^2(\dot{q}(t))\right)
\end{align*}
Since that, according to equation \ref{euler_rot_}, $\dot{\vec{\omega'}} = \bar{\bar{I}}^{-1}\left(\sum\vec{M'} - \vec{\omega'}\times(\bar{\bar{I}}\vec{\omega'})\right) = \bar{\bar{I}}^{-1}\left(\sum\vec{M'} - 2q^*\dot{q}\times(\bar{\bar{I}}2q^*\dot{q})\right)$ we can finally write
\begin{equation}
\boxed{\ddot{q}(t) = q(t)\left(\bar{\bar{I}}^{-1}\left(\frac{1}{2}\sum\vec{M'} - 2q^*(t)\dot{q}(t)\times\left(\bar{\bar{I}}q^*(t)\dot{q}(t)\right)\right) - N^2(\dot{q}(t))\right)}
\label{conservation__}
\end{equation}
For spherically symmetric bodies, a simpler equation can be derived from equation \ref{TMC_}:
\begin{equation}
\boxed{\ddot{q}(t) = q(t)\left(\frac{1}{2I}\sum\vec{M'} - N^2(\dot{q}(t))\right)}
\end{equation}
These equations are the quaternion expressions of the conservation of the angular momentum.

\section{Numerical integration}

\subsection{Issue with the integration of quaternions}
\label{Kleppmann}

What we have to keep in mind is that the quaternion from equations \ref{omega} and \ref{conservation__} is an unit vector of $\mathbb{R}^4$. Therefore, this vector does not move in the whole $\mathbb{R}^4$ space but is \textbf{constrained to the 4D hypersphere of radius 1}.\\

The basic Euler method would guess from $q_0 \equiv q(t = t_0)$ and $\dot{q_0} \equiv \dot{q}(t = t_0)$ the value of the quaternion after an increment $h$ of time, $q(t = t_0 + h) \equiv q_1 = q_0 + h \dot{q_0}$.\\

However, this new quaternion lies outside of the unit quaternion hypersphere and renormalisation is not enough. Indeed, if you consider the limit case of a body rotating infinitely fast, the Euler method followed by a renormalisation of the incremented quaternion would give a move of a quarter of the sphere instead of an infinite number of revolutions.\\

A better increment can be found through geometric considerations \cite{kleppmann2007simulation}.

\begin{figure}[h!]
\centering
\includestandalone{figures/tikz/quaternion_numerical_integration}
\caption{Scheme of the numerical integration}
\label{num_int_quat}
\end{figure}

We can notice that $\forall t$, $\dot{q}(t)\cdot q(t) = \left(\frac{1}{2}q(t)\vec{\omega'}(t)\right)\cdot q(t)$ according to equation \ref{omega}, then
\begin{align*}
\dot{q}(t)\cdot q(t) &= \frac{1}{2}[\vec{q}(t)\times \vec{\omega'}(t) + q_0(t)\vec{\omega'}(t),-\vec{q(t)}\cdot\vec{\omega'}(t)]\cdot[\vec{q}(t),q_0(t)]\\
&=\underbrace{\cancel{\left(\vec{q}(t)\times\vec{\omega'}(t)\right)\cdot\vec{q}(t)}}_{=0} + \cancel{q_0(t) \vec{\omega'}(t)\cdot \vec{q}(t)} - \cancel{q_0(t)\vec{q}(t)\cdot\vec{\omega'}(t)}\\
&=0
\end{align*}
Therefore, since $q(t)$ is a radius of the hypersphere, $\dot{q}(t)$ is always tangent to the hypersphere.\\

In our integration, we want $q_1$ to have moved an arc of length $h\dot{q_0}$ on the hypersphere in the direction $\dot{q_0}$.

From figure \ref{num_int_quat}, we understand that we have to take $q_1$ in the direction of $q'_1 \equiv q_0 + \tan(|h\dot{q_0}|)\frac{\dot{q_0}}{|\dot{q_0}|}$, so:
\begin{equation}
\boxed{q_1 = \frac{q_0 + \tan(|h\dot{q_0}|)\frac{\dot{q_0}}{|\dot{q_0}|}}{|q_0 + \tan(|h\dot{q_0}|)\frac{\dot{q_0}}{|\dot{q_0}|}|}}
\label{int_kleppmann}
\end{equation}

\subsection{Modified Verlet's method}
\label{Verlet_princ}

A modified Verlet integration algorithm is proposed in \cite{vaagberg2017shear}.\\

Consider a quantity $\zeta(t)$ which is a function of time and whose derivatives are $\dot{\zeta}(t)$ and $\ddot{\zeta}(\zeta(t),\dot{\zeta}(t))$. Note $\zeta_0 \equiv \zeta(t = t_0)$ and $\dot{\zeta}_0 \equiv \dot{\zeta}(t = t_0)$, then with
\begin{align*}
\dot{\tilde{\zeta}}_1 &= \dot{\zeta}_0 + h\ddot{\zeta}(\zeta_0,\dot{\zeta}_0)\\
\tilde{\zeta}_1 &= \zeta_0 + h\dot{\zeta}_0
\end{align*}
where $h$ is the increment of time, we have
\begin{align*}
\dot{\zeta}_1 &= \dot{\zeta}_0 + \frac{h}{2}(\ddot{\zeta}(\zeta_0,\dot{\zeta}_0) + \ddot{\zeta}(\tilde{\zeta}_1,\dot{\tilde{\zeta}}_1))
\\
\zeta_1 &= \zeta_0 + \frac{h}{2}(\dot{\zeta}_0 + \dot{\zeta}_1)
\end{align*}
where $\zeta_1 \equiv \zeta(t = t_0 + h)$ and $\dot{\zeta}_1 \equiv \dot{\zeta}(t = t_0 + h)$.

\subsection{Quaternion direct integration}
\label{quaternion_d_int}

\subsubsection{Principle}

We have seen that the Euler's equation for the body's rotation vector $\vec{\omega}$ is equivalent to equation \ref{conservation__} expressed for the body's quaternion $q$, we can then solve this equation with the mean of the modified Verlet's method.

\subsubsection{Initial conditions}
\label{quat_init}

The equation \ref{conservation__} is a second order differential equation, we then need initial conditions for both $q(t)$ and $\dot{q}(t)$.\\

% According to the equation \ref{orientation} at $t = 0$ we have $\vec{r}(0) = q(0)\vec{r}(0)q^*(0)$. We must then have $\boxed{q(0) = 1}$.\\
There are 2 ways to initialise the quaternion:
\begin{itemize}
\item If we have already chosen the initial orientation $\vec{r}(0)$ of the body, then we must have $\boxed{q(0) = 1}$ according to equation \ref{orientation} evaluated at $t = 0$.
\item If we do not get to choose $\vec{r}(0)$, then we can obtain the initial orientation of the body through a rotation of angle $2\theta$ around an axis $\vec{v}$ and we then have $\boxed{q(0) = [\vec{v}\sin\theta,\cos\theta]}$.\\
\end{itemize}

According to the equation \ref{omega} at $t = 0$ we have $\vec{\omega'}(0) = 2q^*(0)\dot{q}(0) \Rightarrow \boxed{\dot{q}(0) = \frac{1}{2} q(0)\vec{\omega'}(0)}$.
%\begin{align*}
% = 2[-\vec{q}(0),q_0(0)][\dot{\vec{q}}(0),\dot{q_0}(0)]\\
%&= 2[\underbrace{\dot{\vec{q}}(0)\times\vec{q}(0)}_{A\dot{\vec{q}}(0)} + \underbrace{q_0\dot{\vec{q}}(0)}_{B\dot{\vec{q}}(0)}~\underbrace{-\dot{q_0}(0)\vec{q}(0)}_{C\dot{\vec{q}}(0)},\underbrace{q_0(0)\dot{q_0}(0)}_{D\dot{\vec{q}}(0)} + \underbrace{\vec{q}(0)\cdot\dot{\vec{q}}(0)}_{E\dot{\vec{q}}(0)}]
%\end{align*}

\subsubsection{Integration scheme}

We use the same notations as in part \ref{Verlet_princ}.\\

We have that $\dot{q}$ is not constrained to any particular space, therefore there is no particular precaution to take when integrating it with the modified Verlet's method. However, $q$ is constrained to the 4D hypersphere. In order to avoid errors, we have to use the technique developed in part \ref{Kleppmann} and calculate
\begin{align*}
\tilde{q}_1 &= \frac{q_0 + \tan(|h\dot{q_0}|)\frac{\dot{q_0}}{|\dot{q_0}|}}{|q_0 + \tan(|h\dot{q_0}|)\frac{\dot{q_0}}{|\dot{q_0}|}|}\\
\dot{\tilde{q}}_1' &= \frac{1}{2}(\dot{q}_0 + \dot{q}_1)\\
q_1 &= \frac{q_0 + \tan(|h\dot{\tilde{q}}_1'|)\frac{\dot{\tilde{q}}_1'}{|\dot{\tilde{q}}_1'|}}{|q_0 + \tan(|h\dot{\tilde{q}}_1'|)\frac{\dot{\tilde{q}}_1'}{|\dot{\tilde{q}}_1'|}|}
\end{align*}

Higher order methods will then rely on the same trick.

\subsection{Quaternion - rotation vector integration}

\subsubsection{Principle}

Experimentally, we observe that the method described in part \ref{quaternion_d_int} is not numerically stable, and that the errors diverge exponentially over time in the case of torque-free precession.\\

A better method is to combine the solving of equation \ref{euler_rot_}, which gives $\dot{\vec{\omega'}}(\vec{\omega'}(t),\vec{M'}(t))$, and equation \ref{omega}, which can be rewritten as
\begin{align*}
\dot{q}(t) = \frac{1}{2}q(t)\vec{\omega'}(t)
\end{align*}
and gives $\dot{q}(\vec{\omega'}(t),q(t))$.

\subsubsection{Initial conditions}

The equation \ref{euler_rot_} is a first order differential equation in $\vec{\omega'}$, therefore we only need an initial condition for the rotation vector of the body $\boxed{\vec{\omega'}(0) = \vec{\omega'}_0}$.\\

Equation \ref{omega} is a first order differential equation in $q$, therefore we only need an initial condition for the quaternion, for whcih we can refer to part \ref{quat_init}.

\subsubsection{Integration scheme}

We use the same notations as in part \ref{Verlet_princ}. We will consider a body described by the position of its centre $\vec{r}$, its velocity $\vec{v}$, its quaternion $q$ and its rotation vector $\vec{\omega'}$. We will denote $\vec{f}$ and $\vec{M'}$ the sum of the forces and the moments exerted on the body, in the reference and in the body frame respectively.\\

The first step is an Euler integration over the time step $h$
\begin{align*}
\tilde{\vec{\omega'_1}} &= \vec{\omega'_0} + h\dot{\vec{\omega'_0}}(\vec{M'}(\vec{r_0}^{(i)},\vec{v_0}^{(i)},q_0^{(i)},\vec{\omega'_0}^{(i)}),\vec{\omega'_0})\\
\tilde{q_1} &= \left\langle q_0 + \tan\left(|h\dot{q}_0(q_0,\vec{\omega'_0})|\right) \frac{\dot{q}_0(q_0,\vec{\omega'_0})}{|\dot{q}_0(q_0,\vec{\omega'_0})|}\right\rangle\\
\tilde{\vec{v_1}} &= \vec{v_0} + \frac{h}{m} \vec{f}(\vec{r_0}^{(i)},\vec{v_0}^{(i)},q_0^{(i)},\vec{\omega'_0}^{(i)})\\
\tilde{\vec{r_1}} &= \vec{r_0} + h\vec{v_0}
\end{align*}
where $\langle q \rangle = q/|q|$, from which we can calculate
\begin{align*}
\Delta q = \frac{h}{2} \left( \dot{q_0}(q_0,\vec{\omega'_0}) + \dot{q_1}(\tilde{q_1},\tilde{\vec{\omega'_1}}) \right)
\end{align*}
and finally do the integration
\begin{align*}
\vec{\omega'_1} &= \vec{\omega'_0} + \frac{h}{2}\left(\dot{\vec{\omega'_0}}(\vec{M'}(\vec{r_0}^{(i)},\vec{v_0}^{(i)},q_0^{(i)},\vec{\omega'_0}^{(i)}),\vec{\omega'_0}) + \dot{\vec{\omega'_1}}(\vec{M'}(\tilde{\vec{r_1}}\hspace{1pt}^{(i)},\tilde{\vec{v_1}}\hspace{1pt}^{(i)},\tilde{q_1}^{(i)},\tilde{\vec{\omega\prime_1}}^{(i)}),\tilde{\vec{\omega'_1}}) \right)\\
q_1 &= \left\langle q_0 + \tan\left(|\Delta q|\right) \frac{\Delta q}{|\Delta q|} \right \rangle\\
\vec{v_1} &= \vec{v_0} + \frac{h}{2m} \Big( \underbrace{\vec{f}(\vec{r_0}^{(i)},\vec{v_0}^{(i)},q_0^{(i)},\vec{\omega'_0}^{(i)})}_{\textstyle m\frac{\tilde{\vec{v_1}} - \vec{v_0}}{h}} + \vec{f}(\tilde{\vec{r_1}}\hspace{1pt}^{(i)},\tilde{\vec{v_1}}\hspace{1pt}^{(i)},\tilde{q_1}^{(i)},\tilde{\vec{\omega\prime_1}}^{(i)}) \Big)\\
&= \frac{1}{2} \left(\tilde{\vec{v_1}} + \vec{v_0} + \frac{h}{m} \vec{f}(\tilde{\vec{r_1}}\hspace{1pt}^{(i)},\tilde{\vec{v_1}}\hspace{1pt}^{(i)},\tilde{q_1}^{(i)},\tilde{\vec{\omega\prime_1}}^{(i)}) \right)\\
\vec{r_1} &= \vec{r_0} + \frac{h}{2}\left(\vec{v_0} + \vec{v_1}\right)
\end{align*}

% \input{references/biblio}

\end{document}

% \end{cbunit}


\section{Definition of Community Detection}
In the beginning, it is necessary to introduce the terminology used in this dissertation. A graph, in its intuitive understanding, is a group of nodes connected with each other to form either one connected or several disjoint components. In this thesis, a graph is also called a network, and a node or vertex (plural: vertices) is a single object appearing in the graph. The terms edge or link refers to the connections between two nodes. Graph mining, also known as complex network analysis, is the practice of exploring graphs to analyze their properties and, often, to extract them automatically. These terms are often used interchangeably, and there is no fundamental difference between them. Community detection is a subtopic within graph mining that aims to detect node closeness relationships given the graph topological structure. Also known as clustering, it entails grouping nodes into communities (also called groups or clusters) where the within-community nodes should have as many connections as possible and between-community nodes should have as few connections as possible.

Mathematically, given a graph $G(V,E)$ where $V$ is the node set and $E$ is the edge set, community detection approaches aim to find a partition $C = \{c_1,c_2,...,c_k\}$ to group all nodes into $k$ communities, where $c_i$ refers to the $i_{th}$ community with $N_i$ nodes. For the simplest scenario in which a node can only belong to one community ($v_i \in c_j$ , $N = \sum_{i=1}^k N_i$) is the number of total nodes  $|V|$ in the graph.

A simple visualization of graph community structures is shown in Figure \ref{fig:c1_community} where nodes and edges constitute the graph and different colors refer to different communities. It is easy to see that there are three communities, which are marked in different colors (blue, green, red). Within each community, the nodes are densely connected with each other, whereas there are few edges between nodes in different communities.

The goal of community detection is to assign each node to a community (disjoint community detection) or multiple communities (overlapping community detection) in a manner that satisfies the abovementioned criteria. Apart from the simple concept definitions introduced above, a Table \ref{tab:c2_glossary} provides a comprehensive glossary of terminology used in this thesis, introducing representative symbols and brief definitions.
\begin{figure}
	% \advance\leftskip-1cm
	% \hspace*{0.25cm}
	\centering
	\includegraphics[width=0.5\columnwidth]{img/chapter1/community.png}
	%  \vspace{-1em}
	\caption{Example graph showing community structure. The nodes of this graph are divided into three communities, with most edges falling within communities and only a few between communities. The figure is contributed from \cite{newman2012communities}.}
	\label{fig:c1_community}
	%   \vspace{-1em}
\end{figure}

\begin{table}
	% \scriptsize
	\centering
	%   \vspace{-3em}
	% \renewcommand{\tabcolsep}{2pt}
	\begin{tabular}{|p{3cm}|p{11cm}|} \hline
		\textbf{Term} &  \textbf{Definition} \\ \hline
		$G(V,E)$ & Graph $G$ with node set $V$ and edge set $E$.  \\ \hline
		$\Omega$ & $\Omega = \{ \omega_1,\omega_2,..., \omega_k\}$ is the ground truth community partition by models  where $\omega_k$ is the $k_{th}$ predicted community. \\ \hline
		$C$ & $C = \{ c_1,c_2,..., c_j\}$ is the detected community partition where $c_j$ is the $j_{th}$ community.\\ \hline
		$N, M$& $N = |V|$ denotes the node number and $M = |E|$ denotes the edge number in graph $G$\\ \hline
		$N_c$& $N_c = |c|$ is the number of nodes in community $c$\\ \hline
		$N_{c,\omega}$& $N_{c,\omega} = |c \cap \omega |$ is the common nodes in the detected community $c$ and ground truth community $\omega$ \\ \hline
		$E_{c}^{in}$& The number of edges within the community $c$. $E_{c}^{in} = \{(u,v) \in E : u \in c, v \in c \}$\\ \hline
		$E_{c}^{out}$& The number of edges on the boundary of the community $c$. $E_{c}^{out} = \{(u,v) \in E : u \in c, v \notin c$ \textit{or} $ u \notin c, v \in c \} $\\ \hline
		$D(v)$& Degree of node $v \in V$ \\ \hline
		$D_c$& The sum of node degrees in community $c$ \\ \hline
		$\mathcal{N}(v)$ & The neighbour node set of node $v$ \\ \hline
		$A$ & Graph adjacent matrix \\ \hline
	\end{tabular}
	\caption{Glossary of technical terms.}
	\label{tab:c2_glossary}

\end{table}


\chapter{Cross-Validation}

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

Estimating the accuracy may depend on a particular split of the data set. To increase robustness, we can repeat the measurement several times, each time choosing a different subset of the data for training. One such method is cross-validation. It is available in Orange in the \widget{Test and Score} widget.

Note that in each iteration, \widget{Test and Score} will pick a part of the data for training, learn the predictive model on this data using some machine learning method, and then test the accuracy of the resulting model on the remaining, test data set. For this, the widget will need on its input a data set from which it will sample the data for training and testing, and a learning method which it will use on the training data set to construct a predictive model. In Orange, the learning method is simply called a learner. Hence, \widget{Test and Score} needs a learner on its input. \marginnote{For geeks: a learner is an object that, given the data, outputs a classifier. Just what \widget{Test and Score} needs.}

This is another way to use the \widget{Tree} widget. In the workflows from the previous lessons we have used another of its outputs, called \textit{Model}; its construction required data. This time, no data is needed for \widget{Tree}, because all that we need from it is a \textit{Learner}.

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

In the \widget{Test and Score} widget, the second column, CA, stands for classification accuracy, and this is what we really care for for now.


\subsection{MLE of the Poisson distribution}


\paragraph{Breeding values}

\gc{Here be dragons, this section has not be worked through}
Before we turn to our discussion of the response to selection it will
help to define one more quantity, the breeding value.
The breeding value of an individual is the mean phenotype of the
offspring of this individual averaged over all possible matings in the
population. This is $X_{Ai}$.

For an individual $i$ we can write the the breeding value of an individual $G_X$ as
\begin{equation}
G_i = \alpha_G + \beta_{GP} X_i   +\epsilon_G %equation 10.4 Falconer
\end{equation}
where $\beta_{GX}  Cov(X,G)/Var(X) =h^2$

The breeder's equation is in fact a statement of the change in the mean
breeding value of the population. To see this consider the fact that
we can write the change in mean breeding value within a generation due
to selection as
\begin{equation}
R=\E[G_i] = \beta_{GP} \bar{X_{}}-\bar{P_X}
\end{equation}

We can also write the breeder's equation as
\begin{equation}
R =\delta \bar{G} = Cov(G,W)
\end{equation}
i.e. the mean change in phenotypes over the generations is due to the
fact that our breeding values covaries with fitness
this is the Price equation.

If  $\epsilon_G$ and G are uncorrelated then
\begin{equation}
\delta \bar{G} = Cov(G,W) = \beta_{GP} Cov(P,W) = h^2 S
\end{equation}
we can also write
\begin{equation}
W = \alpha_W + \beta_{WP} P +\epsilon_W
\end{equation}
where $\beta_{WP} = Cov(W,P)/Var(P)$. If $\epsilon_W$ and $G$ are uncorrelated

\begin{equation}
\delta \bar{G} = Cov(G,W) = \beta_{WP} Cov(G,P)
\end{equation}

\subsubsection{Hamiliton's Rule and the evolution of altruistic and
  selfish behaviours}

\gc{Here be dragons, this section has not be worked through}
We can use our quantitative genetics framework to gain some simple
intuition for when altruistic behaviours should evolve through kin selection. To do this we
can follow Queller's (1992) treatment, to rederive Hamiliton's rule in
a quantitative genetics framework (Hamilition original work did this in a
population genetics framework).

So lets imagine that individuals interact in pairs, with our focal
individual $i$ being paired with an individual $j$.
Imagine that individuals have two possible phenotypes $X=1$ or $0$,
corresponding to providing or withholding some small act of `Altruism'
(we could just as easily flip these labels and call them a unselfish
act and a selfish act respectively). Providing the altruistic act has a cost $C$ to the fitness of our
individual, with failing to provide this act has no cost. Receiving this
altruistic act confers a fitness benefit $B$ (not receiving it has no benefit).
Hamiliton's Rule is that such a trait will spread through the
population if the average coefficient of relatedness between two
individuals is $r$, i.e. the probability that a gene picked randomly
from each at the same locus is identical by descent, such that $C<rB$.
The intuition here is that while our focal individual $i$ is losing
out of some reproductive output, the alleles underlying the behavior
are still spreading as this cost is outweighed by the transmission of these
alleles through a related individual (note that this means that the
allele is not acting in an self sacrificing manner, even though
individuals may as a result).

We will assume that our focal $i$ individual's relative fitness can be written as
\begin{equation}
W(X_i,X_j)= W_0 + W_i +W_j
\end{equation}
where $W_i$ is the contribution of the fitness of the individual $i$ due
to this phenotype, and $W_j$ is the contribution to our
individual $i$'s fitness due to the $j$'s behaviour (i.e. phenotype).
With the benefit $B$ and cost $C$ our $W(X_i,X_j)$ are depicted in
Figure XXX. We can write our fitness component for our individual's fitness
as
\begin{equation}
W_i = \alpha_i + \beta_{W_i P_i} X_i + \epsilon_{W_i}
\end{equation}
and
\begin{equation}
W_j = \alpha_j + \beta_{W_j P_j} P_j + \epsilon_{W_j}
\end{equation}
our slopes $\beta_{W_i P_i} = Cov(W_i,P_i)/Var(P_i)$ and $\beta_{W_j
  P_j} = Cov(W_j,P_j)/Var(P_j)$ i.e. the regression our our individual
$i$s fitness components on his and his neighbour's phenotype. From
Figure XXX these slopes are just our cost and benefit.

Using the price equation, assuming that the residuals are not
correlated with our genotype we arrive at
\begin{equation}
\delta \bar{G}_X = \beta_{W_i P_i} Cov(G_i,P_i) +  \beta_{W_j P_j} Cov(G_i,P_j)
\end{equation}
so our  altruistic trait will increase in frequency in the
population only if $\delta \bar{G}_X >0$, i.e. that
\begin{equation}
0<\beta_{W_i P_i}  + \beta_{W_j P_j} \frac{Cov(G_i,P_j)}{Cov(G_i,P_i)}
\end{equation}
substituting in our slopes we arrive at
\begin{equation}
C<  B \frac{Cov(G_i,P_j)}{Cov(G_i,P_i)}
\end{equation}
i.e. our altruistic trait will increase if the cost is outweighed by
the benefit weighted by a factor $\frac{Cov(G_i,P_j)}{Cov(G_i,P_i)}$. The term $\frac{Cov(G_i,P_j)}{Cov(G_i,P_i)}$
is the ratio of the ratio of covariance between our focal
individual's genotype and their phenotype and that of their
interacting neighbour.


\input{../../assignment-header}

%========================================================================
\title{Assignment 07:  Ballistic Trajectory Calculation}
\date{Assigned:  February 27  ---  Due:  March 7 at 11:55pm}
\author{Optimal Control for Robotics}
%========================================================================
\begin{document}
\maketitle
%=================================================

\section*{Introduction}

In this assignment you will solve a simple boundary value problem using single shooting.

The example problem will be that of aiming a canon:
compute the launch velocity vector with the minimum launch speed
such that the projectile reaches the specified target.

\section*{Deliverables}

Implement the function \texttt{computeBallisticTrajectory} using the template provided.
Also submit any additional Matlab code that you write for the assignment.

\section*{Write-Up:}

In addition to the Matlab code described above,
upload a short write-up for this assignment: \\
\texttt{hw\_07\_studentName\_writeup.txt}
\vspace{-0.0em} \begin{itemize}  \setlength\itemsep{0em} \setlength\itemindent{18pt}
  \item Header: full name, date, assignment name and number
  \item List any other students that you worked with.
  \item How long did this assignment take you?
  \item Briefly describe your approach to testing your code.
  \item Answers to the questions below.
\end{itemize}

One of the best ways to learn about trajectory optimization is to experiment with
the inputs and outputs of an optimization.
Here are a few things to investigate.
\vspace{-0.0em} \begin{itemize}  \setlength\itemsep{0em} \setlength\itemindent{18pt}
\item How does varying the number of grid points and simulation method affect the
      number of iterations, solve time, and final accuracy of the NLP solver?
\item Adjust the various problem parameters to see how the solution changes.
      What happens if the drag is large and the distance is large? Why?
\item How many function calls does \texttt{fmincon} make on each iteration?
      What is going on here?
\end{itemize}

\pagebreak
\section*{\texttt{computeBallisticTrajectory.m}}

For this assignment you will implement one Matlab function: \texttt{computeBallisticTrajectory}.
This function takes a set of paramters that describe a specific boundary value problem
and the computes the optimal ballistic trajectory.
In general there are many ways to solve this problem.
For this assignment I will restrict a few of the details to keep things consistent across the class.
\vspace{-0.6em} \begin{itemize}  \setlength\itemsep{0em}
  \item Use the Matlab function \texttt{fmincon} to solve the underlying non-linear program.
        Create a \texttt{problem} data structure to define the NLP and
        then pass it as a single argument to \texttt{fmincon}.
  \item Use the function \texttt{runSimulation} from the code library to run the simulation.
  \item Use a uniform time grid.
  \item Use all of the inputs and return all of the outputs that are specified in the template code.
  \item The decision variables in the optiization should be the initial velocity vector (three elements)
        and the total duration of the simulation.
  \item The objective function should be the initial speed squared.
\end{itemize}

\subsection*{Inputs:}

There are four inputs to this function, which are described in detail in the source code.
The first input, \texttt{param}, describes the boundary value problem specification:
boundary positions and physics model parameters.
The function \texttt{getBallisticParameters} in the assignment source code can be used
to provide a candidate set of parameters.
The second input, \texttt{nGrid}, gives the number of gridpoints
that should be used for the underlying simulation.
Next is \texttt{method} which is passed directly to \texttt{runSimulation}.
The final input, \texttt{nlpOpt}, is a set of options that are passed to \texttt{fmincon}.
See the template code for how to get the default parameters.

\subsection*{Outputs:}

There are two outputs, which describe the optimal trajectory that the ballistic projectile takes.
The first should be the uniform time grid that was used by the simulation for the dynamics.
The second should be the full state (position and velocity) at each point in time.

\pagebreak
\subsection*{Template Code:}

\lstinputlisting{computeBallisticTrajectory.m}

\pagebreak

\section*{Dynamics Model}

We will use a simple model of projectile motion:
the projectile is acted on by only gravity and a quadratic drag force.
The dynamics are implemented in the code library: \\
\texttt{ME149/codeLibrary/modelSystems/projectile/projectileDynamics.m} \\
The equations of motion are given below for the curious.
The constant parameters are:
$d$ = coefficient of quadratic drag,
$g$ = acceleration due to gravity,
$m$ = projectile mass,
$\bm{\vec{w}}$ = wind velocity.
There are three time-varying scalar functions:
$x = x(t)$, $y = y(t)$, and $z = z(t)$ that describe the position of the projectile.
Finally, there are three constant unit vectors:
$ \hat{\bm{i}}$, $\bm{\hat{j}}$, $\hat{\bm{k}}$.

\begin{equation}
  \bm{\vec{p}} = x  \,  \bm{\hat{i}} + y  \,  \bm{\hat{j}} + z  \,  \bm{\hat{k}}
\end{equation}
\vspace{-0.8em}
\begin{equation}
  \bm{\vec{v}} = \bm{\dot{\vec{p}}} - \bm{\vec{w}}
\end{equation}
\vspace{-0.5em}
\begin{equation}
  \bm{\vec{F}_D} = -d   \;  || \bm{\vec{v}} ||  \;  \bm{\vec{v}}
\end{equation}
\vspace{-0.5em}
\begin{equation}
  \bm{\vec{F}_G} = -m  \;  g  \;  \bm{\hat{k}}
\end{equation}
\vspace{-0.5em}
\begin{equation}
  \bm{\ddot{\vec{p}}} = ( \bm{\vec{F}_D} + \bm{\vec{F}_G} ) / m
\end{equation}

%=================================================
\end{document}


\section{Maximum Interval Query on Incremental Data} \label{sec:QIUD}

This section describes our approach to address the challenges in the
first stage of Algorithm~\ref{alg:parallel-VGLCS}, where we compute
the suffix maximum on every {\em column} of $V$ while new data are
added.  Here we generalize our technique so that we can also answer
incremental {\em range} maximum queries on incrementally added data,
so that our technique can be applied to other cases that require
queries on an interval.

% not here, somewhere else
%\input{./tables/tlb-cmp-complexity.tex}

\subsection{Build Least Common Ancestor Table}

Recall from the discussion of Cartesian tree in
Section~\ref{sec:parallelRMQ} that finding the {\em least common
  ancestor} is important for answering range maximum queries.  Here we
need to address two issues -- how to label a binary tree with a {\em
  Catalan index} and how to find the least common ancestor of two
nodes in a given Cartesian tree.

\subsubsection{Cartesian Tree Labeling}

Our Cartesian tree labeling will enumerate all binary search trees in
{\em lexicographical order} from $0$ to the $n$-th Catalan number minus
1.  This lexicographical order among binary search trees of the {\em
same} number of nodes is defined {\em recursively} as follows.  A
binary tree $x$ appears {\em before} another binary tree $y$ if any
of the following condition is true.  Figure~\ref{fig:labelingBST}
shows our lexicographical labeling of binary search trees of sizes
1, 2 and 3.

\begin{itemize}
\item $x$ has more nodes than $y$ in the left subtree.
\item $x$ and $y$ have the same number of nodes in the left subtree,
  and $x$'s left subtree appears before $y$'s left subtree in
  lexicographical order.
\item $x$ and $y$ has the same left subtree, and $x$'s right subtree
  appears before $y$'s right subtree in lexicographical order.
\end{itemize}

\begin{figure}[!thb]
  \centering
  \includegraphics[width=0.8\linewidth]{\GraphicPath/fig-bst-encoding.pdf}
  \caption{The labeling of binary search trees of sizes 1, 2, and 3.}
  \label{fig:labelingBST}
\end{figure}

\subsubsection{Least Common Ancestor}

We also need to determine the {\em least common ancestor} efficiently
for answering range maximum queries when data are incrementally added.
Let $t$ be the Catalan index of the binary search tree from our
lexicographical labeling, so $t$ is between $0$ and ${\cal C}_s-1$, where
$s$ is the number of search trees.  Let ${\cal A}(s, t, p, q)$ denote
the least common ancestor of the node $p$ and $q$ within a binary search
tree of size $s$ that has a Catalan index $t$.  For example, ${\cal
A}(3, 2, 0, 2) = 1$ from Figure~\ref{fig:labelingBST}.  Now let $s_l$
denote the size of the left subtree, $s_r$ denote the size of the right
subtree, $t_l$ be the Catalan index of its left subtree, and $t_r$ be
the Catalan index of its right subtree.  With these notations we can
define the least common ancestor $\cal A$ {\em recursively} as in
Equation~\ref{fun:LCA}.  A Pseudo code for computing the LCA table $\cal
A$ is given in Algorithm~\ref{alg:parallel-LCA}.

\input{\FormulaPath/fun-LCA.tex}

\input{\AlgoPath/alg-parallel-LCA-2e}

We first analyze the space complexity of
Algorithm~\ref{alg:parallel-LCA}.  The table $\cal A$ keeps all LCA
information for binary trees of sizes from 1 to $s$.  When tree size is
$m$, the number of different binary trees is the $m$-th Catalan number
${\cal C}_m$, which is
$\frac{1}{m+1}\binom{2m}{m}=O(\frac{4^m}{m^{\nicefrac{3}{2}}})$. For
each binary tree of size $m$, we store the least common ancestor of {\em
every} pair of nodes into the table, so the size of the table is
$O(m^2)$. Therefore, the space complexity is $O(s \;
\frac{1}{s+1}\binom{2s}{s} \; s^2)$, where $s$ is the number of elements
in a block.  When we set $s$ to $\frac{\log n}{4}$, the space complexity
is $O(\sqrt{n} \;
\log^{\nicefrac{3}{2}} n)$.  Note that the size of range query is up to
tree size $s$.  However, we do need the space for tables of {\em
smaller} tree sizes as intermediate data to compute the table of tree
size $s$.

We now analyze the time complexity of the parallel version of
Algorithm~\ref{alg:parallel-LCA}.  The time complexity of a sequential
version of Algorithm~\ref{alg:parallel-LCA} is $O(\frac{s^3}{s+1}
\binom{2s}{s})$ because the number of operations in
Equation~\ref{fun:LCA} is $O(1)$.  The time complexity hence becomes
$O(\sqrt{n} \; \log^{\nicefrac{3}{2}} n)$ when we set $s$ to $\frac{\log
n}{4}$.

Now for the parallel version, we observe that the computation of the
${\cal C}_m$ trees of size $m$ are {\em independent}, hence can be done
in parallel.  However, the time to find the sizes and Catalan indices of
subtrees (in line \ref{alg:parallel-LCA-compute}) is $O(m)$ for a tree
of size $m$.  Since both line \ref{alg:parallel-LCA-compute} and
\ref{alg:parallel-LCA-loop} are in the same loop body, it is not
necessary to parallelize line~\ref{alg:parallel-LCA-compute} because the
loop starting at line~\ref{alg:parallel-LCA-loop} will dominate the
time.  As a result, we only parallelize the double loops in
line~\ref{alg:parallel-LCA-for-2} and \ref{alg:parallel-LCA-for-3} in
Algorithm~\ref{alg:parallel-LCA}, and the time complexity of our
parallel algorithm is $O(\frac{s^3}{s+1} \binom{2s}{s} / p + s^2) =
O(\sqrt{n} \; (\log^{\nicefrac{3}{2}} n) / p + \log^2 n )$, where $p$ is
the number of processors.

%% % how to compute subtree information from t

%% Note that in line 4 of Algorithm~\ref{alg:parallel-LCA}, when given
%% the tree id $t$, we need to compute the sizes and ids of the left and
%% right subtrees in our encoding.  We can do this in $O(n)$ time, where
%% $n$ is the number of tree nodes.

\subsection{Catalan Index Computation}

Note that Algorithm~\ref{alg:parallel-LCA} requires Catalan index $t$,
so we need to determine $t$ efficiently when given a block of data.
There are two possible approaches -- build the tree or keep only the
rightmost path.

% how to compute t from tree data structure

\subsubsection{Build the Tree}

In order to find the Catalan index of a block of data, we can build a
Cartesian tree corresponding to the data of the block, and then find the
index of the Cartesian tree.  That is, we build the tree and compute it
from the the sizes and indices of the left and right subtrees.  This
requires a recursive traversal on the tree and has a $O(n)$ time
complexity, where $n$ is the number of tree nodes.  Let ${\cal T}$
denote the Catalan index.  After knowing these information, we can
compute the Catalan index ${\cal T}$ by Equation~\ref{fun:tid}.  Recall
that $s_l$ denotes the size of the left subtree, $s_r$ denotes the size
of the right subtree, $t_l$ is the Catalan index of the left subtree,
and $t_r$ is the Catalan index of the right subtree.  Recall that ${\cal
C}_i$ denotes the Catalan number of tree of size $i$.

% \input{algorithms/alg-encode-tid}

\input{\FormulaPath/fun-tid}

We can further optimize Equation~\ref{fun:tid} by precomputing the
{\em prefix sum} of the Catalan number products.  Then we store these
sums in memory, so that we can use them directly, instead of
recomputing them as in Equation~\ref{fun:tid}.

% how to compute t with rightmost path of the tree

\subsubsection{Keep the Rightmost Path}

The previous computation of Catalan index requires building the tree to
obtain subtree information, and may not be efficient.  We propose a more
efficient method that determines the Catalan index by keeps only the
{\em rightmost path} in a {\em stack}, without building the entire tree.
This technique is similar to the {\em rightmost-pops encoding} in
Section~\ref{sec:cct}.  After knowing the Catalan index $t$, we can
compute LCA and answer queries with Algorithm~\ref{alg:parallel-LCA} and
Equation~\ref{fun:tid}.

We compute the Catalan index $t$ efficiently by the maintaining its {\em
rightmost path} in a {\em stack}.  That is, we will {\em not} build
these left subtrees along the rightmost path.  Instead, we keep their
Catalan indices and sizes in the stack (denoted by $D$).  That is, the
stack $D$ has the Catalan {\em indices} and {\em sizes} of every {\em
left subtree} along the rightmost path.  Please refer to
Algorithm~\ref{alg:cartesian-encode-offline} for the details on the
stack.  In Algorithm~\ref{alg:cartesian-encode-offline} each node of the
stack $D$ has three members -- $v$ as the data, $s$ as the size of its
subtree, and $t$ as the index of its left subtree.  We also use a
pointer $p$ to point to the top of the stack.

\input{\AlgoPath/alg-cartesian-encode-offline-2e}

\begin{figure}[!thb]
  \centering
  \includegraphics[width=0.4\linewidth]{\GraphicPath/fig-cartesian-encoding-static.pdf}
  \caption{Compute Catalan index for a tree.  $A_l$ and $B_l$ denote
    the left subtrees of $A$ and $B$ respectively.}
  \label{fig:fig-cartesian-encoding-static}
\end{figure}

Algorithm~\ref{alg:cartesian-encode-offline} computes the Catalan index
for a given block of data with a stack $D$.  In the first double loop,
the outer loop goes through every input and the inner loop inserts a
data at the {\em end} of the rightmost path, which is at the top of the
stack $D[p]$, and traverse towards the root by popping any {\em smaller}
data out of the stack $D$.  When we rotate nodes along the rightmost
path to update the Cartesian tree, we compute the new {\em index} $t$
and size $s$ of the new left subtree whenever the newly inserted data
replaces a node on the rightmost path.  As a result, the new Catalan
index $t$ can be recomputed with Equation~\ref{fun:tid} by the indices
and sizes of the left and right subtrees in the stack.  Please refer to
the first while loop of Algorithm~\ref{alg:cartesian-encode-offline} and
Figure~\ref{fig:fig-cartesian-encoding-static} for an illustration. Note
that $A_l$ and $B_l$ denote the left subtrees of $A$ and $B$
respectively.  After popping all smaller data in the stack the while
loop stops and the size, index, and input are pushed into the new top of
stack.  Finally, we pop all data out of the stack and compute the
Catalan index for the entire block in the last loop of
Algorithm~\ref{alg:cartesian-encode-offline}.

Algorithm~\ref{alg:cartesian-encode-offline} can compute any Catalan
index for Cartesian trees with the entire block of data and the block
size.  The algorithm runs in $O(s)$ time since an element is
pushed/popped at most {\em once}.

\subsection{Dynamic Catalan Index Computation} \label{sec:dynamic}

Several encoding methods were proposed for indexing Cartesian trees.
Fischer~\cite{Fischer2006TheoreticalAP} introduced the first encoding
method and Masud~\cite{Hasan2010CacheOA} presents a new encoding method
that reduces the number of instructions. Unfortunately all these
algorithms are {\em off-line}, i.e., they assume all data are given in
advance, as a result, they cannot cope with incrementally added data.
In addition, they require a preprocessing of time $O(n)$, where $n$ is
the number of data.  The preprocessing requires extra memory to process
the input data block, or reads external files from disk.

We generalize our Cartesian tree labeling technique for incrementally
added data by a {\em normalization}, which keeps the size of tree as a
constant $n$.  That is, suppose we want to encode a series of Cartesian
trees of {\em increasing} sizes up to $n$, we can append a path of $n-i$
{\em right-child-only} nodes to the rightmost path of the existing tree
of size $i$, so that the total number of node is {\em always} $n$.  That
is, when the computation starts without any existing tree nodes, we will
start with a path of $n$ right-child-only nodes. Please refer to
Figure~\ref{fig:cartesianEncoding-init} for an illustration.  After we
have added $i$ nodes, we will have a path of $n
- i$ right-child-only nodes attached to the rightmost path, as
illustrated by Figure~\ref{fig:cartesianEncoding-before}.  For ease of
explanation we will refer to this added path as the {\em virtual path}.

\begin{figure*}[!thb]
  \centering \subfigure[Initiation $t_{{\it root}_0}= {\cal C}_n - 1$]{
    \includegraphics[width=0.26\linewidth]{\GraphicPath/fig-cartesian-encoding.pdf}
    \label{fig:cartesianEncoding-init}
  } \subfigure[$t_{{\it root}_i}$]{
    \includegraphics[width=0.3\linewidth]{\GraphicPath/fig-cartesian-encoding-before.pdf}
    \label{fig:cartesianEncoding-before}
  } \subfigure[$t_{{\it root}_{i+1}} = t_{{\it root}_i} + t_x - t_A$]{
    \includegraphics[width=0.3\linewidth]{\GraphicPath/fig-cartesian-encoding-after.pdf}
    \label{fig:cartesianEncoding-after}
  }
  \caption{Normalization of Cartesian trees of increasing sizes by
    adding a virtual path.}
  \label{fig:cartesianEncoding}
\end{figure*}

There are two advantages of this tree normalization.  First, this
normalization simplifies the computation of Catalan index.  That is,
we can update the Catalan index with ease whenever we insert a new
data. Second, this normalization simply adds increasingly {\em
  larger} data to the end of a data block, so that the answers to the
interval maximum queries on actual data are {\em not} affected.  As a
result we can {\em dynamically} maintain a lookup table to obtain the
maximum value within a range, so as to answer a range query from this
dynamic Cartesian tree encoding.

%% Now, we provide the dynamic encoding method so that each operation is
%% amortized $O(1)$ time.

The dynamic Catalan index computation with tree normalization works as
follows.  We will maintain the Catalan index of the current Cartesian
tree (as $t^*$), and a stack of the rightmost path of the current tree
(as $D$).  Then we will {\em update} these information every time we add
a new data $x$ into the tree, by calling
Algorithm~\ref{alg:cartesian-encode-online}, which is based on
Algorithm~\ref{alg:parallel-LCA} and Equation~\ref{fun:tid}.  At any
given time $i$, where $i$ is the index of the data, we can compute the
Catalan index of the current tree, which has $i$ nodes, and use it to
answer a range maximum query.

For ease of notation, we use $t$ and $s$ to denote the Catalan index and
the size of the Cartesian tree {\em without} the virtual path. These
notations are similar to those in
Algorithm~\ref{alg:cartesian-encode-offline}.  We also use $t'$ and $s'$
to maintain the index and size of the tree {\em with} the virtual path.
Please refer to Figure~\ref{fig:cartesianEncoding} for an illustration,
where the tree enclosed by the red dotted line does include the virtual
path.

Algorithm~\ref{alg:cartesian-encode-online} first initializes $s$ and
$t$ as Algorithm~\ref{alg:cartesian-encode-offline} does, since it will
also work from the end of the rightmost path and start with an empty
tree.  Then Algorithm~\ref{alg:cartesian-encode-online} initializes $s'$
to $n - i$ since we will append the a virtual path of length $n - i$ to
the end of the rightmost path.  Consequently, $t'$ is ${\cal C}_{s'} -
1$ since it has the largest Catalan index of that size.

Algorithm~\ref{alg:cartesian-encode-online} then inserts the $i$-th
element $x$ in two stages.  The
line~\ref{alg:cartesian-encode-online-pop-c1} and
\ref{alg:cartesian-encode-online-pop-c2} of
Algorithm~\ref{alg:cartesian-encode-online} pops the elements smaller
than $x$ from the stack $D$, using $p$ as the stack pointer of $D$. Note
that $t$ and $s$ are the index and size of the last subtree {\em
without} the virtual path.  Consequently, we popped smaller elements
from the stack and update $s$ and $t$ just as in
Algorithm~\ref{alg:cartesian-encode-online}.  In
line~\ref{alg:cartesian-encode-online-pop-virtual-c1} and
\ref{alg:cartesian-encode-online-pop-virtual-c2} of the loop
Algorithm~\ref{alg:cartesian-encode-online} updates $s'$ and $t'$
similarly.  However it must maintain a virtual path of $n-s'$
right-child-only nodes to the right of the rightmost path.  Please refer
to the Algorithm~\ref{alg:cartesian-encode-online} and
Figure~\ref{fig:cartesianEncoding} for details.

\input{\AlgoPath/alg-cartesian-encode-online-2e}

Now we are ready to update the stack $D$ and return the overall Catalan
index $t^*$.  The new top of stack is now $x$, with left subtree of size
$s$ and index $t$, so we push them into the stack. Please refer to
Figure~\ref{fig:cartesianEncoding-after} for an illustration.  Next we
update the overall Catalan index $t^*$.  After we popped all elements
smaller than $x$, the original subtree of these popped nodes on the
rightmost path, should be inserted as the left subtree of $x$.  Please
compare Figure~\ref{fig:cartesianEncoding-before} (before) and
Figure~\ref{fig:cartesianEncoding-after} (after).

We observe that the only difference between
Figure~\ref{fig:cartesianEncoding-before} and
Figure~\ref{fig:cartesianEncoding-after} is the area enclosed by the red
dotted line.  If we compute the {\em difference} between the Catalan
indices of these two red dotted line area, we can compute the final
$t^*$ (Figure~\ref{fig:cartesianEncoding-after}) by {\em patching} the
Catalan index in Figure~\ref{fig:cartesianEncoding-before} with this
difference. Fortunately our tree lexicographical ordering considers left
subtree {\em before} right subtree, so the difference of the Catalan
indices between Figure~\ref{fig:cartesianEncoding-before} and
Figure~\ref{fig:cartesianEncoding-after} is $t' - {\cal T}(s, t, s - i,
{\cal C}_{s-i} - 1)$.  As a result, the new $t^*$ is the old value of
$t^*$, plus $t' - {\cal T}(s, t, s - i, {\cal C}_{s-i} - 1)$.

Note that Algorithm~\ref{alg:cartesian-encode-online} does {\em not}
increase the amortized time complexity to compute the Catalan index,
even when we dynamically add data into the tree.  As described earlier,
the time complexity of the static index computation
(Algorithm~\ref{alg:cartesian-encode-offline}) is $O(n)$ because each
element is popped exactly once.  Similarly, the total cost of calling
Algorithm~\ref{alg:cartesian-encode-online} $n$ times is also $O(n)$,
since each operation within the loop is $O(1)$.  As a result, the
amortized cost for an in-block query, including the computation of
Catalan index, is also $O(1)$.