\title{\bf Stellar Dynamics}

% REALLY should also cover:
% - Axisymmetric potentials
% - epicyclic frequencies

\section{Basics \& Nomenclature}

Stellar dynamics is almost entirely collisionless, due to the low
number density of stars relative to their radii. It is therefore
governed by the {\it collisionless Boltzmann equation} (sometimes
called the {\it Vlasov equation}) acting under gravity.

In the continuum limit, we can express the {\it distribution function}
of stars in phase space as $f(\vec{x}, \vec{v}, t)$, in units of per
length-cubed per unit velocity-cubed. If we define the phase space
vector $\vec{w} = \{\vec{x}, \vec{v}\}$ then we can write $f(\vec{w},
t)$. The distribution in phase space can be arbitrarily
complicated. It will not be thermalized as in a gas or fluid, or obey
any particular equation of state.

The continuum limit will be violated most rapidly by two-body
interactions. We find in the exercises that the time for an $N$-body
system to relax due to this effect, the {\it two-body relaxation
time}, is:
\begin{equation}
\label{eq:relax}
t_{\rm relax} \sim \frac{0.1 N}{\ln N} t_{\rm cross}
\end{equation}
where $t_{\rm cross}$ is the crossing time of the system. Globular
clusters have relaxation times short compared to their ages.
Galaxies, over most of their extent, have relaxation times high
compared to their ages.

In the continuum limit, the system obeys the collisionless Boltzmann
equation under just gravity:
\begin{equation}
 \frac{\partial f}{\partial t} + \vec{v}\cdot\vec{\nabla} f -
\vec{\nabla}\Phi\cdot\frac{\partial f}{\partial \vec{v}} = 0
\end{equation}
It can be shown that this equation obeys a special case of {\it
Liouville's Theorem}:
\begin{equation}
\frac{{\rm d}f}{{\rm d}t} = 0
\end{equation}
where in this case the substantive derivative is:
\begin{equation}
\frac{\rm d}{{\rm d}t} = \frac{\partial}{\partial t}
+ \sum_\alpha \frac{\partial}{\partial w_\alpha}
\end{equation}
The meaning of Liouville's Theorem is that as a particle travels
through phase space, the phase space density of particles around it
remains constant.

\section{Jeans Equations}

The first few moments of the collisionless Boltzmann equation are
instructive and can be useful. These equations are known as the {\it
Jeans Equations}.

The zeroth moment integrated over velocity yields the equation of
continuity:
\begin{equation}
\frac{\partial n}{\partial t}
+ \vec{\nabla} \cdot\left(n \left\langle\vec{v}\right\rangle\right) =
0
\end{equation}
where $n(\vec{x})$ is the mean density per unit volume and
$\langle\rangle$ indicates a density weighted-mean over all
velocities.

The first moment integrated over velocity yields something akin to
Euler's equations:
\begin{equation}
\label{eq:euler}
n \frac{\partial \langle\vec{v}\rangle}{\partial t}
 + n \langle \vec{v} \rangle \cdot
 \vec{\nabla} \langle\vec{v}\rangle
  = -n \vec{\nabla}\Phi(\vec{x}, t) - \vec{\nabla} \cdot(n
 {\mathbf{\sigma^2}})
\end{equation}
where ${\mathbf{\sigma^2}}$ is the tensor second moment of the velocity
field, analogous to pressure:
\begin{equation}
\sigma_{ij} = \left\langle \left(v_i - \langle v_i \rangle\right)
\left(v_j - \langle v_j \rangle\right) \right\rangle
\end{equation}
Each successively higher moment of the collisional Boltzman equation
involves terms of higher order in this fashion; whereas in a
collisional fluid the system would close with an equation of state, in
a collisionless system the equations never close.

In steady state, where the density is not changing with time anywhere
and therefore the mean velocity is zero everywhere, we find a relation
analogous to the hydrostatic equation:
\begin{equation}
 \label{eq:steady}
 \vec{\nabla} \cdot(n
 {\mathbf{\sigma^2}}) = -n \vec{\nabla}\Phi(\vec{x}, t)
\end{equation}
Under spherical symmetry in configuration space, this can be
rewritten:
\begin{equation}
 \label{eq:spherical}
\frac{\partial(n \sigma_{rr}^2)}{\partial r}
+ \frac{n}{r} \left[2 \sigma_{rr}^2
- \left(\sigma_{\theta\theta}^2 + \sigma_{\phi\phi}^2\right)\right] =
- n \frac{\partial \Phi}{\partial r}
\end{equation}
and in addition $\sigma_{\theta\theta}^2 = \sigma_{\phi\phi}^2$. In
this equation, $\sigma_{rr}$ and $\sigma_{\theta\theta}$ may both be
functions of $r$.

Although the spherical symmetry in configuration space means that $f$
does not depend on $\theta$ or $\phi$, it can clearly depend on
$v_\theta$ and $v_\phi$. Thus, $\sigma_{rr}^2$ does not have to equal
$\sigma_{\theta\theta}^2$. The orbit distribution can be anisotropic,
and the degree anisotropy affects the radial distribution $n(r)$.
This anisotropy is usually quantified by
\begin{equation}
\beta(r)  = 1 - \frac{\sigma_{\theta\theta}^2}{\sigma_{rr}^2}
\end{equation}
We can then write:
\begin{equation}
 \label{eq:sphericalmass}
M(<r) = \frac{rv_c^2}{G} = - \frac{r \sigma_{rr}^2}{G}
\left[\frac{\dd{\ln n}}{\dd{\ln r}}
+ \frac{\dd{\ln \sigma_{rr}^2}}{\dd{\ln r}} + 2 \beta\right],
\end{equation}
where we have defined $v_c$ as the circular velocity of a stable
circular orbit. In this equation, the right hand side consists of
in-principle observables. Particularly, $n(r)$ is the tracer density;
the potential could be set by other unobserved masses. However, in
practice $\beta$ proves hard to constrain for most systems, for which
only line-of-sight velocities and projected densities are known.

If we take $\sigma_{rr}$ to be constant and $\beta=0$, and we assume
that the particles are {\it self-gravitating} --- meaning that they
are the source of the potential --- then a solution to the equations
is given by the singular isothermal sphere:
\begin{eqnarray}
n &\propto& r^{-2} \cr
M(<r) = \frac{rv_c^2}{G} &=& \frac{2 r \sigma_{rr}^2}{G}
\end{eqnarray}
yielding the relation $v_c^2 = 2\sigma_{rr}^2$, which is a useful
order-of-magnitude relationship between the circular velocity and the
one-dimensional (for example, line-of-sight) velocity dispersion in
gravitating systems.

\section{Virial Theorem}

The virial equations establish the relationship between kinetic
and potential energy in collisionless gravitating systems. They are
obtained as a further moment of the Jeans Equation. Specifically, one
takes the first moment of position over the analog of Euler's
equation. For a time-independent system, in the center-of-mass frame,
we can establish the {\it tensor virial theorem}:
\begin{equation}
2 K_{jk} + W_{jk} = 0
\end{equation}
where the internal {\it kinetic energy tensor} is:
\begin{equation}
K_{jk} = \frac{1}{2} \int \dd{}^3\vec{x}^3 \rho \sigma_{jk}^2
\end{equation}
(where $\rho$ is the mass density, so for particles of equal mass $m$,
$\rho = nm$). The {\it potential energy tensor} is:
\begin{equation}
W_{jk} = - \frac{G}{2} \int \dd{}^3\vec{x}'
\dd{}^3\vec{x} \rho(\vec{x}') \rho(\vec{x}) \frac{(x_j' - x_j)(x_k' -
x_k)}{\left| \vec{x}' - \vec{x}\right|^3}
\end{equation}

The trace of the tensor virial theorem yields the {\it scalar virial
theorem}:
\begin{equation}
2K + W = 0
\end{equation}
where $K$ is the total kinetic energy in the center of mass frame, and
$W$ is the total potential energy.

\section{Jeans Theorem}

Jeans Theorem yields an important tool for modeling equilibrium
self-gravitating systems. These systems can be described by the set of
orbits of the particles comprising them. The density field resulting
from this distribution of orbits generates a potential. To remain in
equilibrium, the orbit distribution needs to be stable in that
potential. Jeans Theorem yields a way of generating orbit
distributions that are self-consistent in this sense that they are in
equilibrium.

We showed earlier that $f$ is conserved along orbits in phase space:
\begin{equation}
\frac{\dd{f}}{\dd{t}} = 0
\end{equation}
Further, if $\Phi(\vec{x})$ is time-independent, there are six {\it
constants of motion} $C(\vec{x}, \vec{v}, t)$ conserved along the
orbit (there must be six because the orbit is fully defined by
$\vec{w}(t=0)$).

The {\it integrals of motion} are related. These are functions only of
phase space position that are conserved along orbits:
\begin{equation}
\frac{\dd{I(\vec{x}, \vec{v})}}{\dd{t}} = 0
\end{equation}
Each is also a constant of the motion; so there are at most six of
them. The existence of these integrals of motion implies:
\begin{equation}
f(\vec{x}, \vec{v}) = f\left(I_1(\vec{x}, \vec{v}),
I_2(\vec{x}, \vec{v}), \ldots, I_6(\vec{x}, \vec{v})\right)
\end{equation}
If this were not the case, then $f$ would be an independent integral
of the motion itself!

An integral of motion that always exists is total energy. It is
conserved for each particle along its orbit. Under specific
symmetries, other useful integrals of motion exist. For example, in
spherical symmetry, the angular momentum $\vec{J}$ is conserved; note
that only its amplitude is physically significant in spherical
symmetry however. Therefore under spherical symmetry all equilibrium
distribution functions can be written as $f(E, J)$.

A specific case of interest is the {\it isothermal sphere}. This
distribution results from the choice $f\propto \exp(-E/\sigma^2)$. The
resulting $f$ can be shown to have a velocity distribution that has a
Gaussian width $\sigma$ in each dimension. Generically, at large
radius $\rho \propto r^{-2}$ for an isothermal sphere. At these radii
the circular velocity $v_c = \sqrt{2}\sigma$. The case in which
$r^{-2}$ at all radii is known as the {\it singular isothermal
sphere}, because of the infinite value of the density at the center.

\section{Chandrasekhar Dynamical Friction}

Collisionless, gravitating, dynamical systems exhibit an effect known
as {\it dynamical friction} that converts ``bulk'' kinetic energy into
``internal'' kinetic energy, even in systems with long two-body
relaxation times. This effect is calculated in the exercises below,
where it is shown that for a mass $M$ moving through a system with
density $\rho$ with an isothermal distribution function of velocity
distribution $\sigma$ there is a drag force:
\begin{equation}
\frac{\dd{\vec{v}_M}}{\dd{t}} = - \frac{4\pi \ln\Lambda
G^2M\rho}{v_M^3} \left[\erf X
-  \frac{2X}{\sqrt{\pi}} \exp\left(-X^2\right) \right] \vec{v}_M
\end{equation}
where $X = v_M /\sqrt{2} \sigma$ and $\Lambda \sim M_{\rm total} / M$.
For an initial circular orbit of radius $r_i$, this drag leads to a
dynamical friction time scale:
\begin{equation}
t_{f} = \frac{2.6 \times
10^{11} \mathrm{~yr}}{\ln \Lambda} \left[\frac{r_i}{2\mathrm{~kpc}}\right]^2
\left[\frac{v_c}{250\mathrm{~km~s}^{-1}} \right]
\left[\frac{10^6 M_\odot}{M}\right]
\end{equation}

\section{Tidal radius}

Two point masses $M$ and $m$ separated by distance $D$ will create a
potential with a saddle point that separates zones dominated by one of
the two potential wells or the other.  If these two point masses are
orbiting each other with frequency $\Omega$, a fixed potential can
only be found in the frame rotating about the center of mass at
$\Omega$ (and this potential is only relevant for telling you how
stationary objects in that frame will start to move in that
frame).

The co-rotating frame by design has the relative $1/r^2$ forces from
the two masses roughly balancing each other, meaning the position of
the saddle point is defined by the derivatives of the gravitational
forces, that is, the tidal forces. The dimensional scaling for the
tidal forces is $1/r^3$, so:
\begin{equation}
r = \left(\frac{m}{3M}\right)^{1/3} D
\end{equation}

This tidal radius limits the size of any system of mass $m$ orbiting a
body of mass $M$. Particles or gas more distant than the tidal radius
become unbound from mass $m$. The tend to form {\it tidal
tails}. Particles closer to mass $M$ enter orbits that lead mass $m$,
and particles farther from mass $M$ enter orbits that trail mass $m$.
These tidal features are observable for numerous systems near the
Milky Way.

\section{Commentary}

The fact that collisionless systems have a non-trivial phase space is
of enormous significance. It provides another way that each objects'
history may be encoded in its dynamics. It also means that the
properties of a system have a full six-dimensional structure to their
description. This complexifies accurate predictions of $N$-body
gravitating systems when $N$ cannot be achieved computationally.

The virial theorem is often spoken of in the casual terms that
$v^2 \sim GM/r$ for the characteristic, $v$, $M$ and $r$ for the
system. While this relation follows from dimensional analysis alone,
the virial theorem goes further and is a precise relationship. It is
clearest to think of the virial theorem establishing the relationship
between $K$ and $U$ for a bound, equilibrium system.  However, as
described in the problems, one can create definitions of
``characteristic'' for $v$, $M$, and $r$ for which the equation $v^2 =
GM/r$ holds strictly, and will scale with a constant coefficient in
homologous systems.

\section{Important numbers}

\section{Key References}

\begin{itemize}
  \item
    {\it Galactic
    Dynamics, \href{https://ui.adsabs.harvard.edu/abs/2009PhT....62e..56B/abstract}{\citet{binney09a}}}
\end{itemize}

\section{Order-of-magnitude Exercises}

\begin{enumerate}
\item Assuming the Milky Way halo is isothermal and spherical, given
    the circular velocity at the radius of the Sun is 220 km s$^{-1}$,
    what do you expect the one-dimensional velocity dispersion of dark
    matter particles is? What is the mass interior to the Sun?
\item What is the typical relaxation time for globular clusters?
    Galaxies? Clusters of galaxies?
\item What are the tidal radii for:
\begin{enumerate}
\item A Milky Way mass galaxy ($10^{12}$ $M_\odot$ total) inside a
Coma-mass cluster of galaxies ($10^{15}$ $M_\odot$),
at 500 kpc from the cluster center.
\item An LMC-mass galaxy ($10^{11}$ $M_\odot$ total) inside a
Milky Way-mass galaxy, at 50 kpc from the galactic center.
\item A relatively massive globular cluster ($10^5$ $M\odot$) inside a
Milky Way mass galaxy, at 10 kpc from the galactic center.
\end{enumerate}
\item What is the dynamical friction time scale for:
\begin{enumerate}
\item A Milky Way mass galaxy ($10^{12}$ $M_\odot$ total) inside a
Coma-mass cluster of galaxies ($\sigma \sim 1000$ km s$^{-1}$),
at 500 kpc from the cluster center.
\item An LMC-mass galaxy ($10^{11}$ $M_\odot$ total) inside a
Milky Way-mass galaxy ($\sigma\sim 150$ km s$^{-1}$), at 50 kpc from
the galactic center.
\item A relatively massive globular cluster ($10^5$ $M\odot$) inside a
Milky Way mass galaxy, at 10 kpc from the galactic center.
\end{enumerate}
\end{enumerate}

\section{Analytic Exercises}

\begin{enumerate}
\item In this exercise, we derive the equation for the relaxation time
given in Equation \ref{eq:relax}. Relaxation refers to the effect of
granularity in the potential due to the fact that there are a finite
number of particles in the system, due primarily to two-body
interactions. A distribution function that forms an equilibrium
system, if sampled by a finite number of particles, will slowly (or
quickly) wander away from that equilibrium, on a time scale associated
with the relaxation time. We will calculate this for a system of mass
$M$, consisting of $N$ particles with mass $m$, within some system
size $R$. For this exercise we define the crossing time $t_c=R/v$,
where $v$ is the typical velocity of a particle.
\begin{enumerate}
\item What does the virial theorem tell us about the crossing time
$t_c$?
\begin{answer}
The typical velocity is defined by the virial relation $v^2\sim GM/R$,
so the crossing time is $R^{3/2}/M^{1/2}$ (related, as it needs to be,
by the square root of the density).
\end{answer}
\item \label{q:deflection}
Imagine two particles of mass $m$ passing by each other with
speed $v$ with an impact parameter $b$, defined as their separation at
infinity normal to their relative velocity. Argue from a heuristic
point of view why the velocity perturbation normal to the original
velocity will scale $\propto Gm/bv$ (in detail it is $\Delta
v_\perp \approx 2Gm/bv$).
\begin{answer}
At closest approach the relative acceleration perpendicular to the
original velocity is $Gm/b^2$, and this closest approach lasts a time
of order $b/v$. Multiplying these together yields the $Gm/bv$ scaling.
\end{answer}
\item Consider interactions in some range of impact parameters,
between $b$ and $b+\dd{b}$. During one crossing time of a particle,
what is the mean $\langle \delta v_\perp\rangle$ and mean-squared
$\langle \delta v_\perp^2\rangle$ perturbation that these
interactions cause on the velocity of the particle perpendicular to
its motion?
\item Below $b_{\rm min} = Gm/v^2$ our assumptions break down. We will
take the perhaps questionable route of ignoring these close
encounters. Considering only interactions with impact parameters
between $b_{\rm min}$ and $R$ to express the total
$langle \delta v_\perp\rangle$ over all encounters in a crossing
time. Express the result in terms of $\Lambda = R/b_{\rm min}$.
\item Define the relaxation time as the time it takes for the total
fractional perturbation in velocity to reach unity. How many crossing
times does it take?
\item Approximate the answer one step further, using the
virial theorem to show $\Lambda \sim N$, and thus expressing the
number of crossings just in terms of $N$.
\end{enumerate}
\item Show that Liouville's Theorem follows from the collisionless
Boltzmann equation.
\item Verify the expressions for the first-order Jeans Equation.
\item Show that Equation \ref{eq:spherical} follows from
Equation \ref{eq:steady} under spherical symmetric in configuration
space.
\begin{answer}
First we need to recognize that if there is spherical symmetry in
configuration space, then in spherical coordinates $\sigma_{ij}$ will
be diagonal. Furthermore, the derivatives of $\sigma_{ij}$ with
respect to angular coordinates will be zero. We can write
$\vec{\nabla}$ in spherical coordinates as:
\begin{equation}
\vec{\nabla} = {\hat e}_e \partial_r + {\hat
e}_\theta \frac{1}{r} \partial_\theta + {\hat
e}_\phi \frac{1}{r\sin\theta} \partial_\phi
\end{equation}
We are applying this operator to the tensor $\mathbf{\sigma^2}$ so we
need to work out how it acts on basis vectors:
\begin{equation}
\begin{array}{ccc}
\partial_r {\hat e}_r = 0 &
\partial_r {\hat e}_\theta = 0 &
\partial_r {\hat e}_\phi = 0 \cr
\partial_\theta {\hat e}_r = {\hat e}_\theta &
\partial_\theta {\hat e}_\theta = - {\hat e}_r &
\partial_\theta {\hat e}_\phi = 0 \cr
\partial_\phi {\hat e}_r = \sin\theta {\hat e}_\phi &
\partial_\phi {\hat e}_\theta = \cos\theta {\hat e}_\phi &
\partial_\phi {\hat e}_\phi = - \cos\theta {\hat e}_\theta - \sin\theta {\hat e}_r \cr
\end{array}
\end{equation}
Then we can write the right-hand side of Equation \ref{eq:steady} as:
\begin{equation}
\left[{\hat e}_r \partial_r +
{\hat e}_\theta \frac{1}{r} \partial_\theta +
{\hat e}_\phi \frac{1}{r\sin\theta} \partial_\phi\right] \cdot
\left[ \langle n\rangle \sigma_{ij}^2 {\hat e}_i {\hat e}_j \right]
\end{equation}
where we use the Einstein summation convention. The first term yields:
\begin{equation}
{\hat e}_r \partial_r \left(\langle n\rangle \sigma_{rr}^2\right).
\end{equation}
The second term yields:
\begin{eqnarray}
{\hat e}_\theta \cdot \frac{1}{r} \partial_\theta \left[ \langle
n\rangle \sigma_{ij}^2 {\hat e}_i {\hat e}_j \right] &=&
{\hat e}_\theta \cdot \frac{1}{r} \partial_\theta
\left[\langle n \rangle \left(\sigma_{rr}^2 {\hat e}_r {\hat e}_r +
\sigma_{\theta\theta}^2 {\hat e}_\theta {\hat e}_\theta +
\sigma_{\phi\phi}^2 {\hat e}_\phi {\hat e}_\phi\right) \right]
\cr
&=& {\hat e}_\theta \cdot \frac{\langle n \rangle}{r}
\left[
\sigma_{rr}^2 {\hat e}_\theta {\hat e}_r +
\sigma_{rr}^2 {\hat e}_r {\hat e}_\theta -
\sigma_{\theta\theta}^2 {\hat e}_r {\hat e}_\theta -
\sigma_{\theta\theta}^2 {\hat e}_\theta {\hat e}_r\right]\cr
&=& \frac{\langle n \rangle}{r}
\left[
\sigma_{rr}^2 {\hat e}_r -
\sigma_{\theta\theta}^2 {\hat e}_r\right]\cr
&=& \frac{\langle n \rangle}{r}
\left[
\sigma_{rr}^2 -
\sigma_{\theta\theta}^2 \right] {\hat e}_r
\end{eqnarray}
The third term yields:
\begin{eqnarray}
{\hat e}_\phi \frac{1}{r\sin\theta} \cdot \partial_\phi
\left[ \langle n\rangle \sigma_{ij}^2 {\hat e}_i {\hat e}_j \right]
&=&
{\hat e}_\phi \frac{1}{r\sin\theta} \cdot \partial_\phi
\left[\langle n \rangle \left(\sigma_{rr}^2 {\hat e}_r {\hat e}_r +
\sigma_{\theta\theta}^2 {\hat e}_\theta {\hat e}_\theta +
\sigma_{\phi\phi}^2 {\hat e}_\phi {\hat e}_\phi\right) \right]
\cr
&=&
{\hat e}_\phi \frac{\langle n \rangle}{r\sin\theta} \cdot
\left(\sigma_{rr}^2 \sin\theta {\hat e}_\phi {\hat e}_r +
\sigma_{rr}^2 \sin\theta {\hat e}_r {\hat e}_\phi +
\sigma_{\theta\theta}^2 \cos\theta {\hat e}_\phi {\hat e}_\theta +
\right. \cr
&&
\left.
\sigma_{\theta\theta}^2 \cos\theta {\hat e}_\theta {\hat e}_\phi -
\sigma_{\phi\phi}^2 \sin\theta {\hat e}_r {\hat e}_\phi -
\sigma_{\phi\phi}^2 \sin\theta {\hat e}_\phi {\hat e}_r - \right. \cr
& &
\left.
\sigma_{\phi\phi}^2 \cos\theta {\hat e}_\theta {\hat e}_\phi -
\sigma_{\phi\phi}^2 \cos\theta {\hat e}_\phi {\hat e}_\theta
\right) \cr
&=&
 \frac{\langle n \rangle}{r} \left(
 \sigma_{rr}^2 - \sigma_{\phi\phi}^2 \right) {\hat e}_r +
 \frac{\langle
 n \rangle \cos\theta}{r \sin\theta} \left(\sigma_{\theta\theta}^2
 - \sigma_{\phi\phi}^2\right) {\hat e}_\theta
\end{eqnarray}
Since under spherical symmetric the gradient of the potential has no
components in the angular directions, the second term must also be
zero:
\begin{equation}
\sigma_{\theta\theta} = \sigma_{\phi\phi}
\end{equation}
and the radial terms then add together and yield Equation
(\ref{eq:spherical}):
\begin{equation}
\frac{\partial(n \sigma_{rr}^2)}{\partial r}
+ \frac{n}{r} \left[2 \sigma_{rr}^2
- \left(\sigma_{\theta\theta}^2 + \sigma_{\phi\phi}^2\right)\right] =
- n \frac{\partial \Phi}{\partial r}
\end{equation}
\end{answer}
\item Using the spherically symmetric first-order Jeans Equation,
Equation (\ref{eq:spherical}), shown that
Equation \ref{eq:sphericalmass} holds.
\item For a singular isothermal sphere with $n\propto r^{-2}$ and
$\sigma_{rr}^2$ a constant, but $\beta > 0$, what choice of $\beta$
will make the radial velocity dispersion equal to the
velocity of a stable circular orbit in the potential?
\item Starting with the first-order Jeans Equation,
Equation (\ref{eq:euler}), take another moment with respect to
position. Rearrange in terms of the kinetic and potential energy
tensors to obtain the tensor virial theorem.
% \item Define the characteristic v, M, R for virial theorem
\item We will derive the Plummer model for a spherical equilibrium set
of orbits. Under Jeans Theorem, all equilibrium models will have
$f(E,J)$.  We can take:
\begin{equation}
f = \left\{ \begin{array}{ll}
k_1 (-E)^p & E<0 \cr
0 & E>0 \end{array} \right.
\end{equation}
Under this form, we can find self-consistent combinations of $\rho$
and the gravitational potential $\phi$.
\begin{enumerate}
\item Show that under this form, the density becomes:
\begin{equation}
\rho = k_2 \left(-\phi\right)^n
\end{equation}
for $n=p+3/2$. You may find the following integral useful:
\begin{equation}
\int_0^a {\dd x} x^m \left(a^n - x^n\right)^p =
\frac{a^{m+1+np} \Gamma\left((m+1)/n\right) \Gamma\left(p+1\right)}
{n \Gamma\left[(m+1)/n +p + 1\right]}
\end{equation}

\begin{answer}[Author: Jiarong Zhu]
The distribution function f :
\begin{equation}
f =
\begin{cases}
k_1 (-E)^p & {E < 0} \\
0 & {E > 0}
\end{cases}
\end{equation}
$f$ is non-zero only when $E<0$, which fact will be used in the following calculation. Energy E takes the form:
\begin{equation}
    E = \frac{1}{2}m v^2 + m\phi
\end{equation}
Thus $E<0$ corresponds to $v<v_{max}=\sqrt{2(-\phi)}$.
Density $\rho(\vec x) = m n(\vec x)$, where n is number density and can be calculated by integrating $f(\vec x,\vec v)$ over the velocity space:
\begin{align*}
n &= \int f \,d^3 \vec v  \\
&= \int_{0}^{\infty} 4\pi v^2 f \,dv  \\
&= \int_{0}^{v_{max}} 4 \pi v^2 k_1 \left(-\frac{1}{2}m v^2 -  m \phi\right)^p \,dv + \int_{v_{max}}^{\infty}4
\pi v^2 * 0 \,dv\\
&= 4 \pi
k_1 \left(\frac{m}{2}\right)^p \int_{0}^{v_{max}=\sqrt{2(-\phi)}} v^2
(2(-\phi) - v^2)^p\,dv \\
&= 4 \pi k_1 \left(\frac{m}{2}\right)^p (-2\phi)^{\frac{3+2p}{2}}\frac{\Gamma (3/2) \Gamma (p+1)}{2\Gamma (3/2 +p+1)}\\
&\propto (-\phi)^{p+3/2}
\end{align*}
\end{answer}

\item Demonstrate that the potential:
\begin{equation}
\phi = - \frac{GM}{R} \frac{1}{\left(1 + r^2/R^2\right)^{1/2}}
\end{equation}
satisfies this relation for $n=5$.

\begin{answer}[Author: Jiarong Zhu]
We want to show that potential below satisfies the relation  $\rho = k_2 (-\phi)^5$.
\begin{equation}
    \phi = - \frac{GM}{R}\frac{1}{(1+r^2/R^2)^{1/2}}
\end{equation}

We know that the potential satisfies Poisson's equation:
\begin{equation}
   \nabla ^2 \phi = 4 \pi G \rho
\end{equation}
For spherical equilibrium, potential only depend on radius $r$. Calculate LHS of Eqn.(4) with Eqn.(3) plugged in:

\begin{align*}
    \nabla ^2 \phi &= \frac{1}{r^2}\frac{\partial}{\partial r}(r^2 \frac{\partial \phi}{\partial r})\\
    & = \frac{1}{r^2}\frac{\partial}{\partial r}(r^2 \frac{GM}{2R}\frac{2r/R^2}{(1+r^2/R^2)^{3/2}})\\
    &= \frac{GM}{R^3}\frac{1}{r^2}\frac{\partial}{\partial r}(\frac{r^3}{(1+r^2/R^2)^{3/2}}) \\
    &= \frac{GM}{R^3}(\frac{3(1+r^2/R^2)^{3/2}-r^2 \frac{3}{2} (1+r^2/R^2)^{1/2}\frac{2}{R^2}}{(1+r^2/R^2)^3})\\
    &= \frac{3GM}{R^3(1+r^2/R^2)^{5/2}} \\
    & \propto (-\phi)^5
\end{align*}
$\therefore \rho \propto \nabla ^2 \phi \propto (-\phi)^5 $
\\
The proportionality can be easily fixed by assign $k_1$ properly.

The Plummer model density profile, sometimes used for modeling
spherical systems like globular clusters, is therefore:
\begin{equation}
\rho \propto \frac{1}{\left(1 + r^2/R^2\right)^{5/2}}.
\end{equation}
\end{answer}

\end{enumerate}
\item We will derive the isothermal sphere model for a spherical
equilibrium set of orbits. Under Jeans Theorem, all equilibrium models
will have $f(E,J)$.  We can take:
\begin{equation}
f =
k \exp\left(-E/2\sigma^2\right)
\end{equation}
There is something very unusual about this assumption, because it
includes positive as well as negative energies. As it turns out, this
feature has to do with the result we find later than the total size
and mass of the system is infinite.
\begin{enumerate}
\item Show that:
\begin{equation}
\rho = m K \left(4\pi\sigma^2\right)^{3/2} \exp\left(-\phi/2 \sigma^2\right)
\end{equation}
\item Show that the density must satisfy the equation:
\begin{equation}
\label{eq:lane-emden}
\frac{\partial}{\partial
r} \left(r^2 \frac{\partial \ln \rho}{\partial r} \right) =
- \frac{2\pi G}{\sigma^2} r^2 \rho
\end{equation}
\item Using the hydrostatic equation and the ideal gas law, and
assuming isothermal gas, show that $\sigma^2$ is the equivalent of
$kT/2m$ --- i.e. $m\sigma^2$ is like the energy per degree of freedom.
\item Find the singular solution with $\rho\propto r^{-2}$ everwhere,
and express $\rho(r)$, $M(<r)$, and $v_c(r)$ as functions of
$\sigma^2$ and $r$.
\end{enumerate}
We will show in a  numerical exercise that we can numerically integrate
the equations to a better solution by imposing a zero slope at $r=0$
(but it is still infinite mass).
\item Lowered isothermal sphere model
\item King model
\item Tidal radius
\item We will here derive Equation (\ref{eq:chandra}) for the
Chandrasekhar Dynamical Friction. We will calculate the drag on a
particle of mass $M$ moving at velocity $\vec{v}_M$ through a system
of density $\rho$ with a Gaussian velocity dispersion $\sigma$ in each
Cartesian dimension.
\begin{enumerate}
\item We use the same approach for calculating the relaxation
time. Consider a two-body interaction in the frame of the particle
with mass $M$, with a particle in the field with velocity
$\vec{v}$. Using the results of Exercise \ref{q:deflection}, estimate
the change of the velocity of the second particle along the
original direction ${\hat v}$.
\begin{answer}
The perpendicular velocity deflection is $2GM/bv$. However, the
velocity amplitude should not change. This means that the deflection
angle should satisfy:
\begin{equation}
\sin \theta_D = \frac{2GM/ bv}{v} = \frac{2GM}{bv^2}
\end{equation}
If we assume that the velocities are typicall not much perturbed on
average, we can write the perpendicular deflection as:
\begin{eqnarray}
\left|\Delta \vec{v}_{||}\right| &=&
v\left(1- \cos\theta_D\right) \cr
&\approx&
v\left(\frac{\theta_D^2}{2}\right) \cr
&\approx&
\frac{2G^2M^2}{b^2 v^3}
\end{eqnarray}
\end{answer}
\item What is the velocity change of the particle of mass $M$ due to
this interaction?
\begin{answer}
Conservation of momentum says it must be:
\begin{equation}
\left|\Delta \vec{v}_{M,||}\right| = \frac{m}{M}
\left|\Delta \vec{v}_{||}\right| \approx
\frac{2G^2Mm }{b^2 v^3}
\end{equation}
\end{answer}

\item If the particle of mass $M$ travels at velocity $\vec{v}_M$
through a uniform sea of particles of mass $m$ with a velocity
distribution $f(\vec{v}_m$, what is its rate of encounters with
particles within some differential $\dd{}^3\vec{v}_m$ around a
particular velocity $\vec{v}_m$, and within a differential $\dd{b}$
around impact parameter $b$?
\begin{answer}
Define relative velocity as $\vec{v}_0 = \vec{v}_m - \vec{v}_M$. Then
the particle of mass $M$ sweeps out particles of mass $m$ in the
specified annulus around it with the rate:
\begin{equation}
2\pi b \dd{b} v_0
\end{equation}
Using the distribution function then, the rate is:
\begin{equation}
r(v_0) = 2\pi b \dd{b} v_0 f(\vec{v}_m)
\dd{}^3\vec{v}_m
\end{equation}
\end{answer}
\item In the case of the perpendicular deflection, the mean velocity
difference averages to zero. However, the mean velocity difference
along the parallel direction is always in the same direction. Assume
some $b_{\rm max}$ is set by the size of the system in question, and
define:
\begin{equation}
\Lambda = \frac{b_{\rm max}v_0^2}{GM} \sim \frac{M_{\rm total}}{M}
\end{equation}
where the approximation is from the virial relation. Derive the change
in $\vec{v}_M$ per unit time from particles around velocity
$\vec{v}_m$.
\begin{answer}
\begin{eqnarray}
\left.\frac{\dd\vec{v}_M}{\dd{t}} \right|_m  &=&
\vec{v}_0 f(\vec{v}_m) \dd{}^3 \vec{v}_m
\int_0^{b_{\rm max}} \dd{b} (2\pi b) \frac{2G^2Mm}{b^2 v_0^3} \cr
&=&
\end{eqnarray}
\end{answer}
\end{enumerate}
\item Imagine two masses, $M_1$ and $M_2$, orbiting in a circular
orbit around each other with a constant separation $D$. Transform into
a frame rotating with the orbit, and find the effective potential
$\Phi_{\rm eff}$ such that in the rotating frame:
\begin{equation}
{\rm d}\vec{v}/{\rm d}t = - \vec{\nabla}\Phi_{\rm eff}.
\end{equation}
Plot contours of this effective potential in the orbital plane; you
should see a number of stationary points (the Lagrange points), one of
which is between the two masses. Then, for $M_2\ll M_1$, show that the
stationary point between the two masses is a distance from the small
mass of:
\begin{equation}
r = \left(\frac{M_2}{3M_1}\right)^{1/3} D
\end{equation}
That distance is approximately the tidal radius.
\end{enumerate}

\section{Numerics and Data Exercises}

\begin{enumerate}
\item Using the inner boundary condition that $\partial\rho/\partial
r=0$, integrate Equation \ref{eq:lane-emden} outwards to derive the
shape of an isothermal sphere with a core.
\item Lowered isothermal sphere.
\item $\Phi$, $n$ and $\beta$
\item Dynamics estimates from Gaia
\item Globular cluster radial profiles.
\item Tidal potential
\end{enumerate}

\bibliographystyle{apj}
\bibliography{exex}


% Sequential IMPLEMENTATION
\chapter{Sequential Implementation}
\label{chapter:sequential}
In order to better understand the algorithm, we have started with a basic sequential implementation of it in C++. While this step could be done in any language, we have chosen to work with C++, as it would allow us to re-use pieces of code for the parallel CUDA implementations, described further in this report. Running this version with a large number of options will likely result in a significant amount of computation time. However, the purpose of this implementation is rather a proof of concept that the algorithm produces correct approximations, as well as to provide a set of results, which can be used to test against with the other implementations.

The algorithm described in the book is used to price one option at a time and the natural way to start a sequential implementation would be to create a single function that prices one option. Looping through all options in the data set and calling this function for each of them will then produce the end results. Pseudo-code in Algorithm~\ref{alg:sequential} describes the approach we took based on the book and articles by Hull and White. Note that real is a data type that can be either single or double precision floating point number based on the required accuracy.

The implementation iterates through all given options, constructs a trinomial tree for each of them and propagates prices back through the tree, obtaining the price approximations for each option and returning them in the end. The algorithm follows the intuition provided in the previous chapter \ref{chapter:hullwhitemodel}. The focus of this implementation is on correctness and simplicity.

\pagebreak
\section{Algorithm Description}
\paragraph{Precomputation}
Pricing of one option starts with computing its constants such as tree width and rate step, tree height and time step, and other values needed to solve the formulas in Hull and White model. Afterwards, for each width step j, rate and probabilities (up, middle, down) are precomputed for use during both forward and backward propagations.

\begin{algorithm}[H]
    \DontPrintSemicolon
    \caption{Sequential implementation\label{alg:sequential}}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}

    \underline{function ComputeOptionPrice}\;
    \Input{option : \{ StrikePrice, Maturity, Length, TermUnit, TermStepCount, ReversionRate, Volatility, Type \}\\yields : \{ [Prices], [Timesteps] \}}
    \Output{Price approximation for the option}
    \;
    Pre-compute probabilities for all width steps j\;
    c : OptionConstants = Compute constants for the option\;
    \tcc{Option constants include:}
    \tcc{c.t : int - option length}
    \tcc{c.X : real - option strike price}
    \tcc{c.dt : real - time step (height)}
    \tcc{c.dr : real - rate step (width)}
    \tcc{c.jmax : int - max. j of the tree}
    \tcc{c.width : int - tree width ($2 * \text{c.jmax} + 1$)}
    \tcc{c.height : int - tree height}
    \tcc{c.type : CALL | PUT - option type}
    \;
    \tcc{Create an array of alphas and set the first alpha to the initial dt-period interest rate}
    alphas : real[$\text{c.height} + 1$]\;
    alphas[0] = Compute yield at c.dt\;
\end{algorithm}

\pagebreak
\paragraph{Forward propagation}
The purpose of forward propagation is to compute an array of alphas of size tree height + 1 that will be used during backward propagation. The first alpha is set to the interest rate at time of one time step. To capture the tree values at any given time step, only the current and previous tree levels of size tree width are needed, these two arrays are named $\mathit{Qs}$ and $\mathit{QsCopy}$. The single starting value in the middle of the tree (the root of the tree) in $\mathit{Qs}$ array is initalized to 1\$.

After the arrays are initialized, the program iterates through time steps along the tree height. At each time step, it goes through the values Q computed in the previous step. Every value contributes to three values in the next time step ($\mathit{QsCopy}$) as illustrated in figure~\ref{fig:seqforward}, according to the precomputed rates and probabilities. Note that this is an example of standard branching and there are also a bottom and a top branching, see fig.~\ref{fig:background:allbranchings}. After all $\mathit{Qs}$ in the next step are computed, their values are aggregated to compute the next alpha. Lastly, arrays $\mathit{Qs}$ and $\mathit{QsCopy}$ are swapped and $\mathit{QsCopy}$ is reset to zeros for the next iteration. Note that this approach combines stages 1 and 2 described in chapter~\ref{section:hullwhite:forwardpropagation} in a single iteration of the forward propagation loop.

\begin{figure}[H]
    \centering
    \def\svgwidth{0.5\textwidth}
	\caption{Forward propagation - computing the next step}
    \input{img/seqforward.pdf_tex}
	\source{Compiled by the authors}
	\label{fig:seqforward}
\end{figure}

\begin{algorithm}[H]
    \DontPrintSemicolon
    \setcounter{AlgoLine}{17}
    \caption{Sequential implementation - forward propagation\label{alg:sequential-forward}}

    \tcc{Forward propagation}
    Qs = real[c.width]\;
    QsCopy = real[c.width]\;
    Qs[c.jmax] = 1\tcc*{Set initial node to 1\textdollar}
    \;
    \tcc{Iterate through nodes along tree height}
    \For{$i = 0$ \KwTo $\mathit{c.height} - 1$}{
        \tcc{Compute the highest allowed j index on step i}
        jhigh : int = min(i, c.jmax)\;
        alpha : real = alphas[i]\;
        \;
        \tcc{Iterate along width between j indexes on step i}
        \For{$j = -jhigh$ \KwTo jhigh} {
            Compute and add to QsCopy on $\text{j} + 1$, j, $\text{j}-1$
        }
        \;
        \tcc{Iterate along width between j indexes on step $i + 1$}
        jhigh1 : int = min($\text{i}+1$, c.jmax)\;
        alpha\_p1 : real = 0\;
        \For{$j = -\mathit{jhigh1}$ \KwTo jhigh1}{
            Aggregate alpha\_p1 based on QsCopy[j]
        }
        \;
        Compute alphas[$\text{i}+1$] based on alpha\_p1\;
        Qs = QsCopy\;
        Fill QsCopy with 0\;
    }
\end{algorithm}

\pagebreak
\paragraph{Backward propagation}
After all alphas are computed, they are carried over to backward propagation along with two arrays of size tree width. These arrays called Prices and $PricesCopy$ are used to store the current and previous tree levels similarly to forward propagation. Prices are initialized to 100\$ which represents the payoff at bond maturity.

Afterwards, the program iterates through time steps along the tree height starting from the end of the tree. At each time step, the values at step $i-1$ in $PricesCopy$ are computed from three values in $Prices$ at step $i$ using alpha at $i$ and the precomputed probabilities as illustrated in figure~\ref{fig:seqbackward}. If the current time step is the option maturity, every computed price is discounted by the option strike price, taking care of the option type being call or put as well. Lastly, arrays Prices and $PricesCopy$ are swapped and $PricesCopy$ is reset to zeros for the next iteration.

\begin{figure}[H]
    \centering
    \def\svgwidth{0.5\textwidth}
	\caption{Backward propagation - computing the previous step}
    \input{img/seqbackward.pdf_tex}
	\label{fig:seqbackward}
\end{figure}

\begin{algorithm}[H]
    \DontPrintSemicolon
    \setcounter{AlgoLine}{44}
    \caption{Sequential implementation - backward propagation\label{alg:sequential-backward}}

    \tcc{Backward propagation}
    Prices : real[c.width]\;
    PricesCopy : real[c.width]\;
    Fill Prices with 100 \tcc*{Initialize prices to 100\textdollar}
    \;
    \For{$i = \mathit{c.height} - 1$ \KwTo 0} {
        jhigh : int = min(i, c.jmax)\;
        alpha : real = alphas[i]\;
        \;
        \For{$j = -\mathit{jhigh}$ \KwTo jhigh} {
            jind : int = j + c.jmax\;
            Compute res based on Prices at $\text{j}+1$, j, $\text{j}-1$\;
            \;
            \eIf{Step i is the option maturity}{
                \eIf(\tcc*[f]{Call option}){c.type is CALL}{
                    PricesCopy[jind] = max($\text{res} - \text{c.X}, 0$)
                }(\tcc*[f]{Put option}){
                    PricesCopy[jind] = max($\text{c.X} - \text{res}, 0$)
                }
            }{
                PricesCopy[jind] = res
            }
        }

        Prices = PricesCopy\;
        Fill PricesCopy with 0\;
    }
    \;
    \tcc{Return the calculated current option price}
    \Return Prices[c.jmax]
\end{algorithm}

\section{Validation}\label{section:sequential:validation}

Results obtained by running this implementation will be used for validation of the parallel algorithms, so it is important that they are fully correct. We compared our intermediate array values of $\mathit{alphas}$, $\mathit{Qs}$ and $Prices$ along with the final results with values provided by our supervisor and made sure they are the same within a margin of error.

Table~\ref{table:book-results} compares the value of a three-year put option on a nine-year zero-coupon bond with a strike price of 63: mean-reversion rate $a = 0.1$ and volatility $\sigma = 0.01$, which is an example option in Hull \& White~\cite[pg. 706]{ofod}. The left table shows book results~\cite[pg. 707]{ofod} and the right table shows our results for the same option with different time steps. Our approach is fully numerical, while their tree results are semi-analytic, since they do not build a tree for the whole nine-year bond, but only for the three-year option and then compute the rest using analytic formulas. Despite this fact, our result for daily time steps, i.e. $365 \times 9$ steps for the full tree, are within $0.02\%$ difference of their analytic result.

\begin{table}[h]
\centering
\caption{Sequential results compared on a book example}
\source{Compiled by the authors, based on~\cite[pg. 707]{ofod}.}
\label{table:book-results}
\begin{tabular}{|lll|}
\hline
Steps & Tree   & Analytic \\ \hline
10    & 1.8468 & 1.8093   \\
30    & 1.8172 & 1.8093   \\
50    & 1.8057 & 1.8093   \\
100   & 1.8128 & 1.8093   \\
200   & 1.8090 & 1.8093   \\
500   & 1.8091 & 1.8093   \\ \hline
\end{tabular}
\begin{tabular}{|ll|}
\hline
Steps per year & Results \\ \hline
1              & 1.87996 \\
5              & 1.83827 \\
10             & 1.81851 \\
25             & 1.81120 \\
100            & 1.81053 \\
365            & 1.80968 \\ \hline
\end{tabular}
\end{table}

\newpage
\section*{Summary}
This chapter provided an overview of our sequential implementation with focus on explaining  the computations in forward and backward propagations and how the final results are obtained. Finally, it described how the computations and results were validated with external sources.
The following chapter will describe how this implementation was adapted for a parallel one option per thread version in CUDA.