% !TEX root = ABMCMC.tex
\section{Formulation of the problem}
%##########################################

Suppose we have a timestepping ABM where agents have a finite number of possible internal states and a finite number of ways of acting on their world. Given this, we can define an ABM as:
\begin{itemize}
\item A domain of agent actions $\mathcal{A} =\{ a_0 ... a_n \}$

\item A domain of agent states $\mathcal{S} = \{\psi_0 ... \psi_m\}$

\item An \textit{agent timestep}, which is a computer program, $\pi(\psi,\Psi)$, where $\psi \in \mathcal{S}$ is the agent's state and $\Psi \in \mathbb{Z}^m$ is a (sparse) vector whose $i^{th}$ element is the number of agents in state $\psi_i$ at the start of the timestep. The program returns an action $a \in \mathcal{A}$ which defines the behaviour of the agent in a timestep. The program may make calls to a random number generator that returns a value $0 \le r < 1$ with uniform probability, so its returned value may be non-deterministic and we write $P(\pi(\psi,\Psi)=a)$ to be the probability that the program will return action $a$, given inputs $\psi$ and $\Psi$.

\item An \textit{action function}, $F: \mathcal{S} \times \mathcal{A} \to \mathbb{Z}^m$, which defines the effect of an action on the world, where $F(\psi, a)$ gives a (sparse) vector, $\Phi$, whose $i^{th}$ element gives the number of agents in state $i$ that result from an agent in state $\psi$ performing act $a$ (this includes the final state of the acting agent).
\end{itemize}

As a simple demonstration model, we'll take a spatial predator-prey model where agents are either predator or prey moving about a 2-dimensional grid. In this case, the domain of actions are $\mathcal{A} = \{ a_0=\textrm{move-left}, a_1=\textrm{move-right}, a_2=\textrm{move-up}, a_3=\textrm{move-down}, a_4=\textrm{give-birth}, a_5=\textrm{die}\}$, and the domain of agent states, $\mathcal{S}$, can be thought of as the class
\begin{lstlisting}
class Agent {
	Boolean	isaPredator
	Integer	yPosition
}	Integer	xPosition

\end{lstlisting}

It will be useful in the following to define an ordering of agent states. The ordering we choose isn't important but we choose to place agent \texttt{a} in the (\texttt{a.isaPredator*G$^2$ + a.yPositition*G + a.xPosition})$^{th}$ position, where \texttt{G} is the number of positions along one side of the grid. So, for example, in the $2\times 2$ grid of Figure~\ref{fig:AB-MCMC-1} there are a total of 8 \texttt{Agent} states:
\begin{align*}
\psi_0 &= [0, 0, 0] \\
\psi_1 &= [0, 0, 1] \\
\psi_2 &= [0, 1, 0] \\
\psi_3 &= [0, 1, 1] \\
\psi_4 &= [1, 0, 0] \\
\psi_5 &= [1, 0, 1] \\
\psi_6 &= [1, 1, 0] \\
\psi_7 &= [1, 1, 1]  \\
\end{align*}
where $[p,y,x]$ denotes an \texttt{Agent} in state (\texttt{isaPredator=p, yPosition=y, xPosition=x}).

\begin{figure}
	\centering
	\resizebox{0.5\textwidth}{!}{
		\includegraphics[scale=0.5]{figs/AB-MCMC-1}
	}
	\caption{A simple predator-prey example.\label{fig:AB-MCMC-1}}
\end{figure}

Let a model timestep consist of a matrix $E$ whose elements $e_{\psi a}$ are the number of agents in state $\psi$ that perform act $a$ in this timestep. Note that this matrix will generally be sparse. For example, the timestep shown in Figure~\ref{fig:AB-MCMC-1} would be
\[
E = \kbordermatrix{
	& a_0 & a_1 & a_2 & a_3 & a_4 & a_5 \\
	\psi_0 & 0 & 0 & 0 & 0 & 0 & 0 \\
	\psi_1 & 0 & 0 & 1 & 0 & 0 & 0 \\
	\psi_2 & 0  & 0 & 0 & 0 & 0 & 0 \\
	\psi_3 & 0 & 0 & 0 & 0 & 0 & 0 \\
	\psi_4 & 0  & 0 & 0 & 0 & 0 & 0 \\
	\psi_5 & 0 & 0 & 0 & 0 & 0 & 0 \\
	\psi_6 & 0 & 0 & 0 & 0 & 0 & 0 \\
	\psi_7 & 1 & 0 & 0 & 0 & 0 & 0 \\
}
\]
where all elements are zero except those representing agent $\psi_1$ performing action $a_2$ and agent $\psi_7$ performing action $a_0$.

Finally, let a model trajectory be a tensor, $T$, consisting of a number of model timesteps. ie. a slice of $T$, $T^t$, is the timestep matrix for time $t$, and an element of $T$, $T^t_{\psi a}$, is the number of agents in state $\psi$ that perform act $a$ in timestep $t$.

The timestep function for a predator/prey agent is shown in algorithm \ref{agentTimestep} (where we assume the grid has periodic boundary conditions).

\begin{algorithm}
\caption{Timestep of a predator/prey agent}
\label{agentTimestep}
\begin{algorithmic}
\Function{Timestep}{agent, otherAgents}
\If{agent.isaPredator}
  \If{\Call{random}{ } < $\beta_0$ and number of prey on neighbouring gridsquares of otherAgents > 0}
    \State\Return give-birth \Comment{more likely to reproduce when food is available}
  \EndIf
  \If{\Call{random}{ } < $\gamma_0$}
    \State\Return die
  \EndIf
\Else
  \If{\Call{random}{ } < $\beta_1$}
    \State\Return give-birth
  \EndIf
  \If{\Call{random}{ } < $\gamma_1$}
    \State\Return die
  \EndIf
  \If{\Call{random}{ } < $\delta$ and number of predators on this or neighbouring gridsquares of otherAgents > 0}
    \State\Return die \Comment{been eaten by predator}
  \EndIf

\EndIf
\State \Return move-left, move-right, move-up or move-down based on a call to \Call{Random}{ }
\EndFunction
\end{algorithmic}
\end{algorithm}

\subsection{The posterior}
A trajectory must satisfy a number of constraints in order to be a possible trajectory of an ABM. Since the elements of a trajectory are counts, they must all be non-negative integers, we'll call this the \textit{non-negative integer constraint}
\begin{equation}
\forall t,\psi, a: T^t_{\psi a} \ge 0, T^t_{\psi a} \in \mathbb{Z}
\label{nonNegativeInt}
\end{equation}

A trajectory, $T^t_{\psi a}$, must also be \textit{continuous} which means that the number of agents in each state at the end of timestep $t-1$ must be the number of agents in each state at the beginning of timestep $t$. This can be expressed in terms of the \textit{continuity constraints}:
\begin{equation}
\forall t \in 1 ... n:\forall \phi: \sum_{\psi, a} F(\psi, a)_\phi T^{t-1}_{\psi a} - \sum_a T^t_{\phi a} = 0
\label{continuous}
\end{equation}
where the trajectory runs from $t=0$ to $t=n$. If a trajectory satisfies constraints \ref{nonNegativeInt} and \ref{continuous} we say that it is \textit{valid}.

If we let $\Psi^t_\psi$ be the number of agents in state $\psi$ at the beginning of timestep $t$ then the prior probability of a trajectory is
\[
P(T) =
\begin{cases}
P\left(\Psi^0_{*} = \sum_a T^0_{* a}\right) \prod_{\psi, t} P\left(T^t_{\psi *} \mid \Psi^t_* = \sum_a T^t_{* a}\right) & \text{if } T \text{ is valid} \\
0 & \text{otherwise}
\end{cases}
\]
where $P(\Psi^0_*)$ is our prior beliefs about the state at time $t=0$, and we use $*$ as an index when we want to emphasise that the index remains unbound.

For example, in the predator/prey model, we may know the probability of finding a predator or prey at each gridpoint at $t=0$, may not know the exact positions of each agent so the prior would be a product of Poisson distributions
\[
P(\Psi^0_*) = \prod_\psi \frac{\lambda_\psi^{\Psi^0_\psi} e^{\lambda_\psi}}{\Psi^0_\psi!}.
\]

However, we know the probability that a single agent in a given state will perform an action given the state of the other agents, this is defined by the agent timestep function, so the joint probability that $\Psi^t_\psi$ agents will perform actions $T^t_{\psi *}$ is the multinomial distribution
\[
P\left(T^t_{\psi *} \mid \Psi^t_*\right) = \Psi^t_\psi!\prod_a \frac{P(\pi(\psi,\Psi^t_*)=a)^{T^{t}_{\psi a}}}{T^{t}_{\psi a}!}.
\]
So the prior probability of a trajectory is
\[
P(T) =
\begin{cases}
P(\Psi^0_* = \sum_aT^0_{* a}) \prod_{t, \psi}\left(\sum_a T^t_{\psi a}\right)!\prod_a \frac{P(\pi(\psi,\sum_bT^{t}_{\phi b})=a)^{T^{t}_{\psi a}}}{T^{t}_{\psi a}!} & \text{if } T^t_{\psi a} \text{ is valid} \\
0 & \text{otherwise}\\
\end{cases}
\]

Suppose we also have a set of noisy, aggregate observations, $\Omega$, that have a likelihood function $P(\Omega|T^t_{\psi a})$. In our running example, let's suppose we periodically note down when and where we see footprints of predator and prey. Our aim is to generate a number of samples from the posterior distribution
\[
P(T^t_{\psi a}|\Omega) \propto P\left(\Omega \middle| T^{t}_{\psi a}\right)P(T^t_{\psi a})
\]

We assume that observations are independent given the trajectory and that each individual observation in $\Omega$ can be simulated by a computer program $\omega(T^t_{\psi a})$ which may make calls to a random number generator. The probability, $P(\omega(T^t_{\psi a}) = v)$, that the program returns a value $v$ defines the likelihood of observing value $v$ upon making observation $\omega$ on trajectory $T^t_{\psi a}$. i.e. $\omega(T^t_{\psi a})$ is just a simulation of the observation, which is usually quite easy to express as a computer program and to compute.

In the case of the predator prey model, let's suppose that at the end of every day we go out to a fixed set of inspection sites and check for footprints. Suppose that if predators were present in the grid-square containing an inspection site on that day, there's a 50\% chance we'll see a footprint, and if prey were present, there's a 25\% chance. The observation function for a site is shown in algorithm \ref{observationFunc}

\begin{algorithm}
\caption{Function for observing predator/prey footprints at a site}
\label{observationFunc}
\begin{algorithmic}
\Function{ObserveFootprints}{T} \Comment{T is the ABM trajectory}
\State $t \leftarrow$ time of observation
\State $x \leftarrow$ x-position of observation
\State $y \leftarrow$ y-position of observation
\State $G \leftarrow$ size of grid
\State $\psi \leftarrow G^2 + Gy + x$   \Comment{state of predator in this gridsquare}
\State $\phi \leftarrow Gy + x$     \Comment{state of prey in this gridsquare}
\State $F_{pred} \leftarrow$ \Call{False}{}  \Comment{did we observe predator footprints?}
\State $F_{prey} \leftarrow$ \Call{False}{}  \Comment{did we observe prey footprints?}

\If{$\sum_a T^t_{\psi a} > 0$ and \Call{Random}{ } < 0.5}
\State $F_{pred} \leftarrow$ \Call{True}{}
\EndIf
\If{$\sum_a T^t_{\phi a} > 0$ and \Call{Random}{ } < 0.25}
\State $F_{prey} \leftarrow$ \Call{True}{}
\EndIf
\State\Return $F_{pred}$, $F_{prey}$
\EndFunction
\end{algorithmic}
\end{algorithm}

Given this
\[
P(\Omega|T^t_{\psi a}) = \prod_{(\omega,v) \in \Omega} P(\omega(T^t_{\psi a})=v)
\]

Also, for notational convenience, and without loss of generality, we express our prior beliefs in terms of a set of observations, $\Omega_0$, at $t=0$, so that $P_0(\sum_a T^0_{\psi a}) \propto \prod_{(\omega,v) \in \Omega_0} P(\omega(T^t_{\psi a})=v)$ (this also allows any priors that aren't at $t=0$). The posterior can now be written as
\begin{equation}
P(T^t_{\psi a}|\Omega) \propto
\begin{cases}
\prod_{(\omega,v) \in \Omega} P\left(\omega(T^{t}_{\psi a})=v\right) \prod_{t, \psi}\left(\sum_a T^t_{\psi a}\right)!\prod_{a}\frac{P(\pi(\psi,\sum_bT^{t}_{\phi b})=a)^{T^{t}_{\psi a}}}{T^t_{\psi a}!} & \text{if } T^t_{\psi a} \text{ is valid} \\
0 & \text{otherwise}\\
\end{cases}
\label{posterior}
\end{equation}
where $\Omega$ now includes any priors.

In many practical applications, sampling from this posterior is difficult because it has zero probability for the vast majority of trajectories (i.e. most trajectories are invalid, contain an impossible action or are refuted by the observations). This is true of our predator/prey model, for example. Even though we can generate valid trajectories that fit the prior by simply performing a forward execution of the model from an initial state drawn from the prior, if the trajectory has no predator or prey at a time and place that we observed footprints, then the observations refute the trajectory and the probability falls to zero. It doesn't take many observations until the probability of randomly choosing a trajectory that fits the observations becomes very small indeed. So simple techniques such as rejection sampling are not practical.

In this paper we'll use the Metropolis-Hastings algorithm to generate samples. However, this method requires a proposal function which randomly generates the next sample given the current one. However, given the current sample, we still have the problem of finding a trajectory with non-zero probability. For example if we generate a new trajectory by perturbing one or more elements of the last sample at random (a common strategy with Metropolis-Hastings), it's very unlikely that we'll end up with a trajectory that is valid, contains only possible actions and satisfies the observations. So, the proposed next sample would almost certainly be rejected and we'd probably end up stuck on the first sample until we grew old.

\section{Approximating the support of the posterior}
%##########################################

To solve this problem we'd like to constrain our proposal function to the support of the posterior, $\supp(P(T^t_{\psi a}|\Omega))$ (i.e. the set of trajectories that have non-zero probability).

If we let
\[
\pi'(s, \xi, T^t_{\psi a}) = \pi(\xi,\sum_bT^{s}_{\phi b})
\]
Then from equation \ref{posterior}
\begin{equation}
\supp (P( \,.\, |\Omega)) =
\bigcap_{(\omega,v) \in \Omega}  \supp\left(P\left(\omega(.)=v\right)\right) \cap
\bigcap_{t, \psi, a} \left(\supp\left(P\left( \pi'(t,\psi,.) = a \right)\right) \cup \left\{T^s_{\xi b}: T^t_{\psi a} = 0\right\}\right) \cap
\left\{T^t_{\psi a} \mid T^t_{\psi a} \text{is valid}\right\}
\label{support}
\end{equation}
i.e. in order for the posterior to be non-zero, all the observation likelihoods must be non-zero, the probability of each agent's action at every timestep must be non-zero and the trajectory must be valid (note that we've dropped the exponent in the agent action term as it doesn't affect the support).

The first two terms in equation \ref{support} consists of the supports of computer programs whose inputs are ABM trajectories and whose outputs are given, i.e. the set of trajectories that, when passed to a computer program, would produce a given output.

Calculating the support of a computer program for a given output is, in full generality, NP-complete\footnote{Consider, for example, a program that accepts an assignment of variables to truth values, and returns true if that assignment satisfies a Boolean formula. Deciding whether the support of this program, given that it returns true, is empty or not is equivalent to solving the Boolean satisfiability problem, which is known to be NP-complete\cite{cook1971complexity}} but it is possible to use a technique known as \textit{abstract interpretation}\cite{cousot1977abstract} to efficiently calculate a superset of the support. So, given a computer program $\rho$, we can calculate a set $\mathcal{P}(\rho, v)$ such that
\[
\supp(P(\rho(.)=y)) \subset \mathcal{P}(\rho, v)
\]
Tools to perform abstract interpretation already exist (e.g. PAGAI\cite{henry2012pagai}) and are used widely in applications such as the verification of safety critical systems\cite{blanchet2003static} and in practice $\mathcal{P}(\rho, v)$ is often reasonably tight (i.e. most members of $\mathcal{P}(\rho, v)$ are in $\supp(P(\rho(.)=y))$). For our application we choose to express $\mathcal{P}(\rho, v)$ in terms of a set of linear inequalities on $\rho$'s inputs, this corresponds to the abstract domain of convex polyhedra\cite{cousot1978automatic}\cite{becchi2018efficient}. Calls to the random number generator can be dealt with in the abstract domain by generating a new variable, $r$, that satisfies $0 \le r < 1$ for each call to \texttt{Random()}. These can either be left in as ``auxiliary'' variables in the same way as slack variables, or removed as soon as the variable goes out of scope by finding the convex hull of the projection into a lower dimensional space (this can be done using the double description method\cite{motzkin1953double}).

If we replace the supports in equation \ref{support} with their equivalent supersets we end up with a superset of the posterior
\begin{equation}
\supp(P(.|\Omega)) \subset
\bigcap_{(\omega,v) \in \Omega}  \mathcal{P}(\omega, v) \cap
\bigcap_{t, \psi, a} \left( \mathcal{P}(\pi'(t,\psi,.), a) \cup \left\{T^s_{\xi b}: T^t_{\psi a} = 0\right\} \right)\cap
\left\{T^t_{\psi a} \mid T^t_{\psi a} \text{is valid}\right\}
\label{linearSupport}
\end{equation}

Constraining our proposal function to members of the superset in equation \ref{linearSupport} instead of the true support won't affect the stationary distribution of the Markov Chain. If the proposal function happens to return a trajectory that isn't in $\supp(P(\rho(.)=y))$ then it will just be rejected. This is fine as long as we generate acceptable proposals at a reasonable rate.

The final term in equation \ref{linearSupport}, which requires the trajectories in the support to be valid, consists of the continuity constraints of equation \ref{continuous}, which are also linear constraints so can be treated along with the other constraints, and the non-negative integer constraints of equation \ref{nonNegativeInt} which we leave as separate constraints.

The union term in equation \ref{linearSupport} can also be transformed to a set of linear constraints, but the way we do this depends on the type of ABM we're dealing with, so we defer the explanation until sections \ref{Fermionic} and ...

Without loss of generality, we express inequalities as equalities by introducing slack variables so that
\[
\sum_i c_i x_i \le y
\]
is equivalent to
\[
\begin{split}
 \sum_i c_i x_i + \lambda & = y \\
\text{subject to}\ \lambda & \ge 0 \\
\end{split}
\]

All the constraints can now be gathered into a standard matrix form
\begin{equation}
\begin{split}
AX &= B \\
\text{subject to}\ x_i &\ge 0\\
x_i &\in \mathbb{Z}
\end{split}
\label{Axy}
\end{equation}
where the vector $X$ contains the elements of the trajectory tensor $T^t_{\psi a}$ and the slack variables $\lambda$.

\section{From convex polyhedron to Markov process}
%#####################################################

Given a set of inequalities that contain the support of the posterior, we next need to define a stochastic proposal function $f:\mathbb{S} \to \mathbb{S}$ on a set of Markov states, $\mathbb{S}$, where each state, $S$, is associated with a trajectory $T(S)$. The proposal function should have the following properties:
\begin{itemize}
\item There should be a set of transitions with non-zero probability between any two Markov states. This ensures proper mixing as time tends to infinity.

\item At the stationary point, the probability of being in a state associated with trajectory $T$ should be our target posterior probability $P(T \mid \Omega)$.

\item For any transition from state $S_a \to S_b$ the probability of transitioning from $S_b \to S_a$ should be non-zero. This allows us to attain detailed balance by using the Metropolis Hastings algorithm.

\item The proposal function should be computationally efficient.
\end{itemize}

\subsection{A proposal function for a Fermionic ABM}
\label{Fermionic}
%#############################################

We define a Fermionic ABM to be one where no two agents can share the same state at the same time. i.e. in addition to the validity constraints of equations \ref{nonNegativeInt} and \ref{continuous} the trajectory of a Fermionic ABM also satisfies the \textit{Fermionic constraint}
\begin{equation}
\begin{split}
\forall t,\psi: \sum_a T^t_{\psi a} \le 1 \\
\forall \phi: \sum_{\psi, a} F(\psi, a)_\phi T^n_{\psi a} \le 1
\end{split}
\label{fermionic}
\end{equation}
in which case we call it a \textit{Fermionic trajectory} [this can be weakened to $T^t_{\psi a} \le 1$].

It is clear that every element of a Fermionic trajectory must be either 0 or 1. This means we can easily convert the union term in equation \ref{linearSupport}
\[
\mathcal{P}(\pi'(t,\psi,.), a) \cup \left\{T^s_{\xi b}: T^t_{\psi a} = 0\right\}
\]
into a set of linear constraints. Expanding $\mathcal{P}$ we have
\[
\left(\bigcap_j \sum_i a_{ij}x_i \le b_j \right) \cup \left\{X: x_k = 0\right\} = \bigcap_j \left( \sum_i a_{ij}x_i \le b_j  \cup \left\{X: x_k = 0\right\}\right)
\]
where we've projected the elements of the trajectory $T^t_{\psi a}$ into a vector $X$. But
\[
\sum_i a_{ij}x_i \le b_j  \cup \left\{X: x_k = 0\right\} = \sum_i a_{ij}x_i + \left(\sum_i \frac{|a_{ij}| + a_{ij}}{2} - b_j\right)x_k \le \sum_i \frac{|a_{ij}| + a_{ij}}{2}
\]
since all $x_i$ are also either 0 or 1 so $\sum_i a_{ij}x_i$ must be less than $\frac{|a_{ij}| + a_{ij}}{2}$ and the constraint is satisfied if $x_k$ is 0, whereas we have the original constraint if $x_k$ is 1.

So, taking our predator prey agent of algorithm \ref{agentTimestep} as an example, we can see that the move, reproduce and die actions can be returned irrespective of the state of other agents\footnote{the Fermionic constraint is dealt with separately, so we don't need to worry about that here}, so for these actions the union is also unconditionally true. However, the reproduction actions of a predator are conditional on there being some prey on neighbouring gridsquares. So, suppose we have a predator in state $\psi$ at time $t$, the support of it's timestep function given that it returns a reproduction action is
\[
\sum_{i \in N^t_\psi} -x_i \le -1
\]
where $N^t_\psi$ is the set of indices that correspond to elements of $T^t_{\phi a}$ where $\phi$ is a prey on a gridsquare neighbouring $\psi$ at time $t$. So, taking the union with $\left\{X: x_k = 0\right\}$, the final constraint for a predator in state $\psi$ at time $t$ is
\[
\sum_{i \in N^t_\psi} -x_i  + x_k  \le 0
\]

\begin{theorem}
All trajectories that satisfy equations \ref{nonNegativeInt} and \ref{fermionic} are at extreme points. Where an extreme point is one that can't be expressed as a linear combination of two other points that satisfy the constraints.
\end{theorem}
\begin{proof}

Inequality \ref{fermionic} can only be satisfied by non-negative integers if, for any given $t$ and $\psi$ either all $T^t_{\psi a}$ are zero or one element of $T^t_{\psi a}$ is one and the rest zero. In the first case the solution is touching all $|a|$ of the $T^t_{\psi a} \ge 0$ constraints, in the second case the solution is touching $|a|-1$ of the $T^t_{\psi a} \ge 0$ constraints and one $\sum_a T^t_{\psi a} \le 1$ constraint to give a total of $|a|$.

[Conversely, we can show that all extreme points are integer points, so we have the polytope of integer solutions]

[In the case of the action Fermionic constraint, this can be seen immediately since equations \ref{nonNegativeInt} and the action Fermionic constraint describe a unit hupercube, so all integer solutions are on vertices.]

\end{proof}

%\begin{theorem}
%For any pair of valid trajectories, $A$ and $B$, there exists a path along edges of the polyhedron from $A$ to $B$ that only traverses through integer solutions.
%\end{theorem}
%\begin{proof}
%[Take the difference A-B and decompose into integer null vectors (loops)]
%\end{proof}

This seemingly abstract fact has a profound practical consequence for the efficiency of our proposal function.
Given a system of $m$ linear constraints on $n$ variables expressed in the form $AX=B$, let a \textit{pivot state} be a partition of the variables into $m$ \textit{basic variables} and $n-m$ \textit{non-basic variables} so we can express the constraints in the form
\begin{equation}
A_bX_b + A_nX_n = B
\label{tableau1}
\end{equation}
where $X_b$ and $X_n$ are the basic and non-basic variables respectively, $A_b$ is the matrix formed from the columns in $A$ that correspond to the basic variables and $A_n$ is the matrix formed from the columns of $A$ that correspond to the non-basic variables. If we now constrain all non-basic variables to zero then we're left with $A_bX_b = B$ but since $A_b$ is an $m \times m$ square matrix then $X_b = A_b^{-1}B$ gives a unique solution, as long as $A_b$ is invertable. If $A_b$ is invertable and $X_b$ has no negative entries then we say that the pivot state is valid. It can be shown that all valid pivot states of equations \ref{Axy} correspond to extreme points\cite{dantzig1955generalized}.

This idea leads to a computationally very efficient way of perturbing trajectories. We begin by representing the constraints and the current trajectory in the form of equation \ref{tableau1}. If we multiply any row of $A_b$ by some constant, $c$, then the truth of equation \ref{tableau1} can be maintained by multiplying the same row of $A_n$ and of $B$ by $c$ as well. Similarly if we add one row of $A_b$ to another row, the equations can be maintained by performing the same row operation on $A_n$ and $B$. If the pivot state is valid, then $A_b$ is invertable so we can use Gaussian elimination, by performing a sequence of row additions and multiplications, to transform $A_b$ into a form such that each row and column is zero on all elements except one, which is 1. We'll call this the standard form. Once in this form, if we assume $X_n = 0$ the solution for $X_b$ can be read off directly from $B$.

Given a valid pivot state in standard form, a perturbed valid solution can be generated by choosing a column, $C$, of $A_n$, with at least one positive-valued row, and choosing from among those rows the one, $C_i$, that minimises $B_i/C_i$. We then make the variable corresponding to column $C$ a basic variable and make the basic variable corresponding to row $i$ of $A_b$ non-basic by swapping column $C$ with the column on row $i$ of $A_b$ that has value 1. $A_b$ is now not in standard form as column $C$ will, in general, have more than one non-zero value so we perform Gaussian elimination (row additions and multiplications) to return it to standard form. This perturbation is known as a \textit{pivot} operation and is the same method as employed in the Simplex algorithm\cite{dantzig1955generalized}\cite{vanderbei2015linear}. This is exactly what we need for our proposal function since it efficiently generates a new valid trajectory from a current one.

\subsubsection{Fractional solutions and feasibility pumps}
%#########################################################

Although pivoting ensures solutions are positive, there may exist extreme points that don't fall on the grid of integer solutions and so do not satisfy the integer constraint. However, if we posit the existence of a \textit{null trajectory} and associate all fractional solutions with the null trajectory, then simply ignore null samples, then we end up with samples from the posterior, as required (i.e. the Markov chain can pass through fractional states, but no sample is generated when doing so). However, we don't want to spend too much computational effort moving between fractional states without generating samples, so we encourage the Markov process away from these fractional states using a \textit{feasibility pump} which works in the following way: [Score pivots from a fractional state by sum of difference between integer rounding of this state and pivoted state (no update of target integer solution), and by difference between integer rounding of pivoted state and pivoted state and the difference between the integer roundings of this and pivoted state (target integer solution updated). ]

%by multiplying their probability by a ``fractional penalty'' $e^{-kn}$ where $n$ is the number of non-integer values in the solution and $k$ is a constant parameter [better to use a stochastic extension of Fischetti(2005) feasibility pump here. Fractional solutions are ``augmented'' with an integer, infeasible(negative) solution which also changes stochastically].

\subsubsection{Dealing with degeneracy}
%######################################

The use of pivoting is complicated somewhat when the solutions are degenerate. A degenerate solution is one that can be generated by more than one pivot state. This can occur when a pivot state has basic variables with value zero (in which case we say the pivot state is degenerate and call the zero variables \textit{degenerate varaibles} of the pivot state). Pivoting on a degenerate variable does not change the solution since during a pivot the outgoing basic variable becomes constrained to zero; but if the basic-variable's value was zero to start with, constraining it to zero has no effect on the solution (for a thorough theoretical development of degeneracy see \cite{zornig93degeneracy}). This is problematic because we need to assign a probability to each Markov state but under degeneracy a pivot state can't be given the probability of its trajectory because this would result in degenerate trajectories being ``counted'' multiple times, once for each degenerate pivot state of the trajectory. To maintain the correct distribution, the probabilities of all the degenerate pivot states of a given trajectory should sum to the probability of that trajectory.

To solve this problem in a computationally efficient way we introduce the concept of an \textit{ordered pivot state} which consists of a partition of the variables into basic and non-basic variables as before, but in addition there is now an ordering of the basic variables. Since we represent $A_b$ as a matrix in our standard form representation, it is easy to represent this ordering in the ordering of the rows of $A_b$. As before, the validity of equation \ref{tableau1} can be maintained under permutations of the ordering of the rows of $A_b$ by performing the same row permutations on $A_n$ and $B$. We now associate a Markov state with each ordered pivot state, assigning it a probability according to algorithm \ref{probAlgorithm}:

\begin{algorithm}
\caption{Algorithm to calculate probability of an ordered pivot state}
\label{probAlgorithm}
\begin{algorithmic}
\Function{Probability}{$A_b$,$A_n$,$B$} \Comment{Standard representation of an ordered pivot state}
\State $P_T \leftarrow$ the probability of the trajectory of the current solution
\State $P_d \leftarrow 1.0$
\State $C \leftarrow \emptyset$
\State $r \leftarrow 0$
\For{$i = |B|-1 \text{ down to } 0$}
  \If{B[i] == 0}
	\State $C \leftarrow C \ \cup $ the indices of the non-zero entries in row $i$ of $A_n$
	\State $P_d \leftarrow P_d/(|C|+r+1)$
	\State $r \leftarrow r + 1$
  \EndIf
\EndFor
\State \Return $\frac{P_d P_T\sigma!}{|B|!}$
\EndFunction
\end{algorithmic}
\end{algorithm}

The proposal function for ordered pivot states is shown in algorithm \ref{proposal}

\begin{algorithm}
\caption{Proposal function for ordered pivot states}
\label{proposal}
\begin{algorithmic}
\Function{Proposal}{$A_b$, $A_n$, $B$} \Comment{Standard representation of the current ordered pivot state}
\State $A_b',A_n',B' \leftarrow A_b,A_n,B$
\State $\alpha, \beta \leftarrow$ constant parameters
\State $p_0 \leftarrow$ the set of degenerate pivots of $(A_b,A_n,B)$ (i.e. the ones that do not change the solution)
\State $p_1 \leftarrow$ the set of non-degenerate pivots of $(A_b,A_n,B)$ (i.e. the ones that change the solution)
\State $P_{f/b} \leftarrow 1$ \Comment{the ratio of probabilities of making this transition forwards/backwards}
\If{\Call{Bernoulli}{$\alpha$}}
  \State $r \leftarrow$ choose member of $p_1$ with uniform probability
  \State perform pivot $r$ on $(A_b',A_n',B')$
  \State $p'_1 \leftarrow $ number of non-degenerate pivots of $(A_b',A_n',B')$
  \State $P_{f/b} \leftarrow \frac{|p'_1|}{|p_1|}$
\ElsIf{\Call{Bernoulli}{$\beta$}}
  \State $r \leftarrow$ choose member of $p_0$ with uniform probability
  \State perform pivot $r$ on $(A_b',A_n',B')$
  \State $p'_0 \leftarrow $ number of degenerate pivots of $(A_b',A_n',B')$
  \State $P_{f/b} \leftarrow \frac{|p'_0|}{|p_0|}$
\Else
  \State choose two rows $a$ and $b$ with uniform probability
  \State swap rows $a$ and $b$ of $(A_b',A_n',B')$
\EndIf
\State \Return $(A_b',A_n',B',P_{f/b})$
\EndFunction
\end{algorithmic}
\end{algorithm}

We now show that this assignment of probability to ordered pivot states has the property that the sum of the probabilities of ordered pivot states associated with a trajecotry is the probability of that trajectory.

First some notation: let $\mathbb{E}(T)$ be the set of all ordered pivot states with solution $T$, let $T(S)$ be the trajectory associated with pivot state $S$, let $D(S)$ be the tuple of degenerate variables in ordered pivot state $S$, let $D_{\le n}(S)$ be the n-tuple consisting of the first $n$ elements of $D(S)$ and let
\begin{equation}
C(\mathbb{S} \mid V) = \left\|\left\{ V^+ :  S \in \mathbb{S}, V^+ = D_{\le |V|+1}(S), V^+_{\le |V|}=V \right\}\right\|
\label{count}
\end{equation}
i.e. among the members of $\mathbb{S}$ that have the degenerate variable prefix $V$, $C(\mathbb{S}|V)$ counts how many distinct degenerate variable prefixes of length $|V|+1$ there are.

\begin{theorem}
If we assign the probability
\begin{equation}
P(S) =  \frac{\sigma_{T(S)}!}{m!} \frac{P_{T(S)}}{\prod_{q=0}^{\sigma_{T(S)}-1} C(\mathbb{E}(T(S)) \mid D_{\le q}(S))}
\label{pivotProb}
\end{equation}
to pivot state $S$, where $m$ is the number of constraints and $\sigma_{T}$ is the degeneracy of trajectory $T$ (i.e. $m$ minus the number of non-zero variables in $T$) then
\[
\sum_{S \in \mathbb{E}(T)} P(S) = P_{T}
\]
\end{theorem}
\begin{proof}
Expanding the sum over degenerate states
\[
\sum_{S \in \mathbb{E}(T)} P(S) =
P_T \frac{\sigma_T!}{m!} \sum_{S \in \mathbb{E}(T)} \frac{1}{\prod_{q=0}^{\sigma_T-1} C(\mathbb{E}(T) \mid D_{\le q}(S))}
\]

Notice first that all pivot states with the same $\sigma_T$-tuple of degenerate variables, $D(S)$, have the same probability. There are exactly $\frac{m!}{\sigma_T!}$ ordered pivot states with a given $D(S)$, corresponding to the different placings of the $m-\sigma_T$ non-degenerate basic variables among the $m$ possible positions. So, if we let
\[
\mathbb{D}(T) = \left\{ D(S) : S \in \mathbb{E}(T) \right\}
\]
then
\[
\sum_{S \in \mathbb{E}(T)} P(S) = P_T \sum_{D \in \mathbb{D}(T)} \frac{1}{\prod_{q=0}^{\sigma_T-1} C(\mathbb{E}(T) \mid D_{\le q})}
\]
Since $C(\mathbb{S} \mid V)$ depends only on the order of the degenerate variables in the members of $\mathbb{S}$
\[
C(\mathbb{E}(T) \mid V) = C(\mathbb{D}(T) \mid V)
\]
so
\begin{equation}
\sum_{S \in \mathbb{E}(T)} P(S) = P_T \sum_{D \in \mathbb{D}(T)} \frac{1}{\prod_{q=0}^{\sigma_T-1} C(\mathbb{D}(T) \mid D_{\le q})}
\label{normalForm}
\end{equation}
if we separate the terms in the sum into groups that share the same prefix apart from the last variable then we get
\[
\sum_{S \in \mathbb{E}(T)} P(S) =
P_T \sum_{V : D \in \mathbb{D}(T), V = D_{\le (\sigma_T-1)}}
\left(
\frac{1}{\prod_{q=0}^{\sigma_T-2} C(\mathbb{D}(T|V_{\le q}))}
\sum_{V^+: V^+ \in \mathbb{D}(T), V = V^+_{\le (\sigma_T-1)}}
\frac{1}{C(\mathbb{D}(T|V))}
\right)
\]
So
\[
\sum_{S \in \mathbb{E}(T)} P(S) =
P_T \sum_{V : D \in \mathbb{D}(T), V = D_{\le (\sigma_T-1)}}
\frac{1}{\prod_{q=0}^{\sigma_T-2} C(\mathbb{D}(T|V_{\le q}))}
\frac{\left\|\left\{V^+: V^+ \in \mathbb{D}(T), V = V^+_{\le (\sigma_T-1)}\right\}\right\|}{C(\mathbb{D}(T|V_{\le (\sigma_T-1)}))}
\]
but the final fraction here is 1 by equation \ref{count}, so
\[
\sum_{S \in \mathbb{E}(T)} P(S) =
P_T \sum_{V : D \in \mathbb{D}(T), V = D_{\le (\sigma_T-1)}}
\frac{1}{\prod_{q=0}^{\sigma_T-2} C(\mathbb{D}(T|V_{\le q}))}
\]
we can now use the same argument again to reduce the upper bound of $q$ iteratively until we show that the sum on the right hand equals one, so
\[
\sum_{S \in \mathbb{E}(T)} P(S) = P_T
\]
\end{proof}

\begin{theorem}
Algorithm \ref{probAlgorithm} assigns a probability
\[
P(S) =  \frac{\sigma_{T(S)}!}{m!} \frac{P_{T(S)}}{\prod_{q=0}^{\sigma_{T(S)}-1} C(\mathbb{E}(T(S)) \mid D_{\le q}(S))}
\]
to pivot state $S$
\end{theorem}
\begin{proof}
By inspection of the algorithm, it can be seen that the algorithm assigns probability
\[
P(S) =  \frac{\sigma_{T(S)}!}{m!} \frac{P_{T(S)}}{\prod_{r=0}^{\sigma_{T(S)}-1} \left(\left\|C'(S, r)\right\| + r + 1\right)}
\]
where
\[
C'(S,r) = C'(S,r-1) \cup N(S,r)
\]
\[
C'(S,-1) = \emptyset
\]
and $N(S,r)$ is the set of indices of the non-zero entries in the $r$-from-last degenerate row of $A_n$. So, $C'(S,r)$ is the set of columns that have at least one non-zero entry on a degenerate row on or below the $r$-from-last.

However, $C(\mathbb{E}(T(S)) \mid D_{\le q}(S))$ is, by definition, the number of different degenerate variables that can take the $(q+1)^{th}$ place in the list of degenerate variables, given that the first $q$ are given by $D_{\le q}(S)$.

Now, given an ordered pivot state, $S$ in standard form; if there exists a column, $j$, with a non-zero entry in a degenerate row, $i$,  of $A_n$ or $A_b$ below the $q^{th}$ degenerate row, then we can generate a valid member of $\mathbb{E}(T(S))$ with $j$ the $(q+1)^{th}$ variable by pivoting on column $j$ and row $i$ (if not already pivoted in), then swapping row $i$ with whichever row makes it the $(q+1)^{th}$ degenerate variable. Conversely, if there exists a member of $\mathbb{E}(T(S))$ with $j$ as the $(q+1)^{th}$ variable, it must be possible to pivot on the $j^{th}$ column without pivoting out any of the first $q$ degenerate variables or the non-degenerate variables. The only way this is possible is if there is either a non-zero entry in the $j^{th}$ column of $A_n$ or $A_b$ below the $q^{th}$ degenerate row. So, the set of degenerate variables that can take the $(q+1)^{th}$ place is the set of columns that have non-zero entries in degenerate rows below the $q^{th}$ in $A_n$ or $A_b$. From the form of $A_b$ we know immediately that there are $r+1$ columns with non-zero degenerate entries on or below the $r$-to-last degenerate row. So
\[
C(\mathbb{E}(T(S)) \mid D_{\le \sigma_{T(S)} -1 - r}(S)) = \left\|C'(S, r)\right\| + r + 1
\]

\end{proof}

\subsubsection{Metropolis-Hastings on ordered pivot states}

All this can now be assembled into a sampling algorithm based on Metropolis-Hastings. Starting with the constraints in the form of equation \ref{Axy}, recursively choose a non-zero element on an un-pivoted row and pivot on that element, until we reach an ordered pivot state. In general, $B$ will contain negative elements which means that the solution will not conform to the non-negative constraint. In order to find an initial positive solution let $I^- = \left\{i: B_i < 0\right\}$ be the set of rows of $B$ that are negative and pivot on the column, $j$, that minimises $\sum_{i\in I^-}A_{ij}$. Repeat until a positive solution is found (or no column has a negative sum, in which case no positive solution exists).

From the initial valid pivot state, $S$, generate a proposal pivot state $S'$ and a transition probability ratio $\frac{P(S\rightarrow S')}{P(S' \rightarrow S)}$ using algorithm \ref{proposal} and accept with probability
\[
\alpha = \frac{P(S')}{P(S)}\frac{P(S'\rightarrow S)}{P(S \rightarrow S')}
\]
where the probability of a pivot state is given by algorithm \ref{probAlgorithm}.

Given this, it's clear that all the necessary properties of the proposal function are fulfilled.


\documentclass[letterpaper, 10pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb,scrextend}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{silence}
\WarningFilter{latex}{You have requested package}
\input{ltx/pkg/preamble}

\begin{document}

\lhead{MAT224 Linear Algebra II}
\chead{Inverses, Change of Basis \& Determinant}
\rhead{Week 07}

\title{Linear Algebra II \\ \Large{MAT224}}
\author{Lennart Döppenschmitt}
% \maketitle
% \tableofcontents

\newpage
\lb
\textbf{Recall}
\lb
For two composable linear transformations $\map{U}[S]{V}$ and $\map{V}[T]{W}$ and bases
$α$, $β$ and $γ$ respectively, in matrices this means
\[ [ T \circ S ] _α ^γ = [ T ] _β ^γ [ S ] _α ^ β \]

\vspace{150pt}
\lb
\textbf{Discussion}
\lb
Recall that the matrix representing $\map{\pol{n}{\R}}[\frac{d}{dx}]{\pol{n}{\R}}$ in the
basis $α = \cb{1, x, x^2}$ is given by

\[ A = \begin{pmatrix}
    0 & 1 & 0 \\
    0 & 0 & 2 \\
    0 & 0 & 0
\end{pmatrix} \]
Verify that $A^2$ is the matrix representing $ \frac{d^2}{dx^2}$

\vspace{150pt}
\lb
\textbf{Remark}
\lb
\emph{Literally} everything we know about linear transformations also holds for matrices.
\begin{enumerate}
    \item
        $A(BC) = (AB)C$
    \item
        $I \cdot A = A$
    \item
        $\iv A \cdot A = I$
    \item
        $ \tx{rank}(A) = \dim(\tx{col}(A)) $
    \item
        $\cdots$
\end{enumerate}

\lb
\textbf{Discussion}
\lb
Given two composable matrices, is
\[ AB = BA \]
true?

\newpage
\section*{The Inverse of a Linear Transformation}%
\textbf{Textbook:} Section 2.6
\lb

\lb
\textbf{Definitions}
\lb
\begin{enumerate}
    \item
        An \emph{inverse }to a linear transformation $\map{V}[T]{W}$ is another transformation
        $\map{W}[S]{V}$ such that
        \[ S \circ T = \id{V} \]
        and
        \[ T \circ S = \id{W} \]
    \item
        A linear transformation that has an inverse is called \emph{invetible}.
\end{enumerate}

\lb
\textbf{Proposition 2.6.2 \& 2.6.1}
\begin{enumerate}
    \item
        A linear transformation $\map{V}[T]{W}$ has an inverse if and only if it is bijective.
    \item
        The inverse of a linear transformation is also linear.
\end{enumerate}
\textbf{Remark:} Remember, we called bjective linear transformations \emph{isomorphisms}, this
is now the same as invertible linear transformations.
\begin{proof}
\end{proof}

\newpage

\lb
\textbf{Proposition (not in book)}
\lb
An inverse transformation to $\map{V}[T]{W}$, if it exists, is unique
\pr
We can therefore denote it by \emph{the} inverse and use the notation $T^{-1}$
\begin{proof}
\end{proof}

\vspace{200pt}

\lb
\textbf{Examples}
\begin{enumerate}
    \item
        Rotation $\map{\R^2}[R_θ]{\R^2}$ has the inverse $\map{\R^2}[R_{-θ}]{\R^2}$
    \item
    The evaluation map $\map{\pol{n}{\R}}[ev_4]{\R}$ does not have an
    inverse because it is not injective for $ n \geq 1$.
    \item
        Define for $\vec x, \vec y ∈ \R^n$ the
        \begin{enumerate}
            \item
                \emph{dot product}
                \[ \vec x \cdot \vec y = x_1 y_1 + x_2 y_2 + \ldots + x_n y_n \]
            \item
                \emph{norm}
                \[ \nrm{\vec x}^2 = \vec x \cdot \vec x = x_1^2  + x_2^2 + \ldots + x_n^2 \]
        \end{enumerate}
    The projection of a vector $\vec x$ onto a vector $\vec y$ is not invertible.
    \[ \tx{proj}_{\vec x} (\vec y) = \frac{\vec x \cdot \vec y }{\nrm{\vec x}^2} \cdot \vec x \]
\end{enumerate}

\vspace{200pt}

\lb
\textbf{Exercise}
Fix a vector $\ttpl{a}{b}$ in $\R^2$ and compute the matrix representing the linear
transformation
\[ \vec y \mapsto \tx{proj}_{\ttpl{a}{b}}(\vec y) \]

\newpage

\lb
\textbf{Discussion}
\lb
Define for a vector space $V$ with basis $α = \cb{α_1, α_2, α_3, α_4}$
the \emph{left shift operator} $\map{V}[L]{V}$ by
\[ T(α_i) = α_{i+1} \]
for $i = 1, 2, 3$ and
\[ T(α_4) = α_1 \]
What is the matrix representing $L$ in the basis $α$? Is $L$ invertible?

\vspace{200pt}

\lb
\textbf{Proposition 2.6.7}
\lb
Two finite dimensional vector spaces $V$ and $W$ are isomorphic if and only if
$\dim(V) = \dim(W)$.
\begin{proof}
\end{proof}

\vspace{200pt}

\lb
\textbf{Examples}
\lb
Since the following vector spaces have the same dimension, they have to be isomorphic
\begin{enumerate}
    \item \[ \pol{n-1}{\R}\]  and \[ \R^n \]
    \item
        \[ W = \cb{\vec x ∈ \R^3 ~ \vert ~ x_1 - x_2 + x_3 = 0 } \]
        and
        \[ \R^2 \]
\end{enumerate}

\newpage

\lb
\textbf{Discussion}
\lb
At how many points do we need to evaluate a polynomial of degree $3$  to reconstruct it?
Explain your answer in terms of a linear transformation
\begin{align*}
    \pol{3}{\R} &\ra \R^n \\
    p(x) &\mapsto \tttpl{p(x_1)}{\vdots}{p(x_n)}
\end{align*}

\newpage
\lb
\textbf{Observation}
\lb
Let $\map{V}[T]{W}$ be an invertible linear transformation with inverse $T^{-1}$.
The matrices of $T$ and $\iv T$ in some bases $α$ and $β$ satisfy
\[ [T]_α^β \cdot [\iv T]_β^α  = I_n\]

\vspace{300pt}

\lb
\textbf{Note: } All matrices in the remainder of this section are assumed to be square matrices.

\lb
This motivates the following.
\lb
\textbf{Definition}
A matrix $A$ of size $n \times n$ is invertible if there exists another matrix $B$ such that
\[ AB = I_n \]
and
\[ BA = I_n \]

\lb
The results for invertible transformations apply as well to matrices.
\lb
\textbf{Proposition}
\begin{enumerate}
    \item The inverse of a matrix $A$ is unique if it exists.
    \item A matrix $A$ is invertible if and only if it has full rank.
        This means for an $n\times n$ matrix
        that its rank, which is the number of leading 1s in $RREF(A)$, is equal to $n$.
\end{enumerate}

\newpage
\lb
\textbf{Proposition 2.6.11}
\lb
For a linear transformation $\map{V}[T]{W}$ between vector spaces with bases $α$ and $β$
\[ ([T]_α^β)^{-1} = [\iv T]_β^α \]
\begin{proof}
\end{proof}

\vspace{200pt}

\lb
\textbf{Algorithm (page 119)}
\lb
How to find the inverse of a matrix $A$
\begin{enumerate}
    \item Write $A$ and $I_n$ together in a $n \times 2n$ matrix
        \[ [ A ~ \vert ~ I_n ] \]
    \item
        Reduce this matrix to its RREF
        \[ [ A ~ \vert ~ I_n ]  \rightsquigarrow R \]
    \item
        resulting matrix will contain the inverse of $A$ as the right $n \times n$ block.
        \[ [ A ~ \vert ~ I_n ]  \rightsquigarrow [ I_n ~ \vert ~ \iv A ] \]
\end{enumerate}

\lb
\textbf{Example (2.6.9)}
\lb

\newpage
\lb
\textbf{Discussion}
\begin{enumerate}
    \item
    Compute the inverse of the left shift operator $L$ as a matrix.
    \item
Compute the inverse of the matrix
\[ \begin{pmatrix}
    1 & 1 & 0 \\
    0 & -1 & 1 \\
    0 & 0 & 1 \\
\end{pmatrix}
\]
\end{enumerate}

\newpage
\section*{Change of Basis}%
\textbf{Textbook:} Section 2.6

\lb
The choice of the right basis makes some computations a lot easier. Take for example
the projection onto the line of slope $m$ in $\R^2$.
\lb
\textbf{Example}
\lb

\vspace{300pt}
\lb
\textbf{Question}
\lb
Given a vector space $V$ with two bases $α$ and $β$, a vector $\vec v ∈ V$ has two different
coordinate representations $[v]_α$ and $[v]_β$.
\lb
How can we change from the basis $α$ to the basis $β$?

\lb
\textbf{Example (2.7.1)}
\lb

\newpage
\lb
\textbf{Proposition (2.7.3)}
\lb
Given a vector space $V$ with two bases $α$ and $β$. The coordinate tuples of a vector
$\vec v ∈ V$ are related by the matrix representing the identity transformation
\[ \map{V}[\id{V}]{V} \]
from the basis $α$ to the basis $β$.
\[ [ I] _α ^β \cdot [ v ]_α = [v]_β \]

\lb
We therefore call $ [I]_α^β$ the \emph{change of basis} matrix from $α$ to $β$.

\begin{proof}

\end{proof}

\vspace{250pt}
\lb
\textbf{Discussion}
\lb
\begin{enumerate}
    \item
        Are change of basis matrices invertible? If so, what are there inverses?
    \item
        Compute the change of basis matrix going in $\pol{2}{\R}$
        from $ α = \cb{1 + x + x^2, 1, x }$ to $β = \cb{1, x, x^2}$.
\end{enumerate}

\newpage
\lb
\textbf{Proposition}
\lb
Let $\map{V}[T]{W}$ be a linear transformation between vector spaces with bases $α$ and $β$
that is represented by
\[ [T]_α ^ β \]
When we introduce new bases $α'$ and $β'$ on $V$ and $W$ respectively with change of basis
matrices $[I_V]_α^{α'}$ and $[I_W]_β^{β'}$, the matrix representing $T$ in these bases is given
by
\[ [T] _{α'}^{β'} = [I_W]_β^{β'} [T]_α^β [I_V]_{α'}^α \]

\vspace{200pt}
\lb
\textbf{Definition}
\lb
If we forget that these matrices come from linear transformations, we may define for
two $n \times n$ matrices $A$ and $B$ related by
\[ A = P B \iv P \]
for some invertible $n \times n$ matrix $P$ to be \emph{similar} matrices.

\lb
\textbf{Proposition}
\lb
Similar matrices have the same rank.

\newpage
\lb
\textbf{Example}
\lb
Change the basis for the transformation of $\tx{proj}_{\vec x}(\vec y)$ from the standard basis
to a more convenient basis.

\newpage
\section*{The Determinant}%
\textbf{Textbook:} Section 3.1
\lb

tbd

\end{document}


Predecessor problem queries are everywhere. They can be found in several different applications, including database management systems (DBMS), internet routing, machine learning algorithms, big data, statistics, data compression, and spellchecking. They can be understood as a simpler version of the nearest neighbor problem \cite{bille2020massive}. Moreover, because they are at the basis of many systems, it is of great importance and relevance that the data structures that provide these queries are space and time-efficient.

This thesis investigates the theoretical data structure presented in the «Dynamic Integer Sets with Optimal Rank, Select, and Predecessor Search» paper, from 2014 and authored by Pătrașcu and Thorup. Concretely, we follow the paper closely up to chapter 3.1 (inclusive), implementing the data structure and its algorithms, discussing and evaluating the running times.

The authors claim that their proposal solves the dynamic predecessor problem optimally.
Fusion Nodes compute static predecessor queries in $O(1)$ time \cite{fredman1993surpassing}, but updates may entail recomputing the instance variables, making such operation to take polynomial time.
It is stated that, for this reason, Fusion Trees only solve the static predecessor problem \cite{nelsonjelanilec2}.
Pătrașcu and Thorup address this by improving on how the keys in the set are compressed, allowing them to be computed and queried in $O(1)$ time for sets of size $n = w^{O(1)}$, e.g., the \textit{Dynamic Fusion Node} \cite{patrascu2014dynamic}.
This is made possible due to the smart use of techniques such as bitwise tricks, word-level parallelism, key compression, and wildcards (denoted "don't cares" in this report and also by Pătrașcu and Thorup), which in the word RAM model take $O(1)$ time.

\section{Context}

The aforementioned paper presents a data structure called \textit{Dynamic Fusion Node}. It is designed to maintain a dynamic set of integers, which improves on the fusion node authored by Fredman and Willard and published in \cite{fredman1993surpassing} while incorporating concepts from other data structures such as Patricia tries.

Like in the Fusion Tree by Fredman and Willard, the \textit{Dynamic Fusion Node} is to be used in a $k$-ary tree implementation, thus solving the dynamic predecessor problem for sets of arbitrary size. When this goal is reached, Pătrașcu and Thorup claim that the running time for the dynamic rank and select is $O(\log_w n)$, proven by Fredman and Saks to be optimal \cite{fredman1989cell}. This is possible because all operations at the node level take $O(1)$ time, making the overall running time bounded by the tree structure the node is to be implemented in.

This project can also be seen as a follow-up on the data structures discussed in the \textit{Integer Data Structure} lectures from the «6.851: Advanced Data Structures»\footnote{The lectures notes, recordings, and other course materials are available through the link: \url{http://courses.csail.mit.edu/6.851/fall17/lectures/}} and «CS 224: Advanced Algorithms»\footnote{The lectures notes, recordings, and other course materials are available through the link: \url{http://people.seas.harvard.edu/~minilek/cs224/fall14/index.html}} courses from Harvard University and MIT, respectively.
These courses' scribe notes have also been an important resource for the work we have developed in this project.
In particular, Demaine's lecture $12$ \cite{erikdemainelec12} and Nelson's lecture $2$ \cite{nelsonjelanilec2} of the aforementioned courses discuss the Fusion Trees, which, as previously mentioned, are data structures that establish many important concepts that the \textit{Dynamic Fusion Tree} from Pătrașcu and Thorup builds upon.
This is relevant because, in their lectures, they present the Fusion Tree as a static predecessor problem data structure, e.g., it can be efficiently queried but does not support efficient updates, whereas the present implementation is focused on supporting efficient updates as well.

\section{Goals}

Using the \cite{patrascu2014dynamic} as the main literature resource, this project aims at:
\begin{itemize}
    \item
    Exposing the data structure present in the aforementioned paper by implementing it as close as possible to its description.

    \item
    Reporting on the implementation, highlighting the algorithms and techniques that enable the data structure's functionality by means of illustrative examples.

    \item
    Evaluating the soundness of Pătrașcu and Thorup's claim, e.g., if it is possible to implement the data structure and its algorithms by using only $O(1)$ time operations in the word RAM model.

    \item
    Elaboration of a small survey on existing (dynamic) predecessor problem data structures, paving the way for a future benchmark between the present and the surveyed data structures.
\end{itemize}

\section{Approach} % methodology

We will be guided by the \cite{patrascu2014dynamic} publication, using contributions from other authors whenever needed. The data structure will be implemented by taking incremental steps, which more or less correspond to the Chapters and Sections of our primary reference.

This project encompasses the present document and the algorithms and data structures here reported. All the code developed in the scope of this project has been made publicly available in a GitHub repository, accessible through the link \url{https://github.com/hugo-brito/DynamicIntegerSetsWithOptimalRankSelectPredecessorSearch} and subject to an MIT License.
The programs were implemented using the Java programming language.

\section{Scope} \label{sec:scope}

This project's scope is limited to:
\begin{itemize}
    \item
    Implementation of helper functions that implement supporting algorithms for the \textit{Dynamic Fusion Node} data structure.

    \item
    Implementation of the algorithms encompassed by the \textit{Dynamic Fusion Node} as presented in \cite{patrascu2014dynamic} but up to Chapter 3.1 (inclusive).
\end{itemize}

\section{Report Structure}

The present chapter provides the context, the approach taken, and establishes the scope of the project.

Chapter~\ref{sec:backgroundChapter} provides the background needed to understand the problem at hand and some non-trivial techniques used by Pătrașcu and Thorup in their proposal.
In Section~\ref{sec:IntegerSets}, we will also see different ways to solve the predecessor problem, from the naive Array up until the Fusion Tree.
The latter is also an essential premise for enabling the data structure of this paper.

Chapter~\ref{sec:implementationsChapter} builds a library of relevant functions used for the \textit{Dynamic Fusion Node}, which is also presented in the following sections of the chapter.
Based on the theoretical algorithms presented in \cite{patrascu2014dynamic}, the implementation is presented in iterative steps, starting from a naive way up to the insertion method while using all the algorithms and techniques described up to that point.

We validate the implemented algorithms and data structures with correctness tests. These appear described in Chapter~\ref{sec:validationChapter}.

Chapter~\ref{sec:conclusionChapter} concludes the project, leaving some remarks and suggestions for future work.

We have also included an appendix, \ref{sec:appendix}, where we mention some additional carried work that could be useful for future work.

\section{Files}

This project encompassed a set of files, which are handed in together with the present report in a {\ttfamily zip} archive.
Once extracted, this  {\ttfamily zip} archive includes the following:

\paragraph*{Read-Me File} The file named {\ttfamily README.md} and located in the root folder.

\paragraph*{Written Report} The file named {\ttfamily report.pdf} and located in the root folder.

\paragraph*{Report Source Code} Located in the {\ttfamily .\textbackslash src\textbackslash tex} folder.

\paragraph*{Javadoc} The {\ttfamily Javadoc} documentation can be found in the {\ttfamily .\textbackslash src\textbackslash javadoc} folder. For convenience, a shortcut was included in the root folder: {\ttfamily Javadoc.html}.

\paragraph*{Implementation Source Code} Located in the {\ttfamily .\textbackslash src\textbackslash main\textbackslash java\textbackslash integersets} folder.

\paragraph*{Testing Classes Source Code} Located in the {\ttfamily.\textbackslash src\textbackslash test\textbackslash java} folder.

The remaining non-listed files include a {\ttfamily Gradle} build, which allows to run the testing classes for the implementations. To do so, open the terminal in the root folder and type {\ttfamily gradlew test}. Note that it might take several hours for the tests to conclude since the tests are currently configured to run on large instances.


\documentclass[main.tex]{subfiles}
\begin{document}

\section{GW gauge}

\subsection{Detector frame Newtonian waves}

% Let us select one end of our interferometer, and impose the condition \(g_{\mu \nu } = \eta_{\mu \nu }\) at that point, as well as \(g_{\mu \nu , \rho } = 0\).

% If we Taylor-expand the metric (along the spatial directions only) from that point,
% the deviations from flatness will kick in with the second order term:
% %
% \begin{align}
% g_{\mu \nu } (x) = \eta_{\mu \nu } + \eval{\partial_{i} \partial_{j} g_{\mu \nu }}_{x_0 }
% (x-x_0 )^{i}
% (x-x_0 )^{j} + \order{(x-x_0 )^3}
% \,.
% \end{align}

As we will discuss later, the expression for the geodesic deviation is
%
\begin{align}
\dv[2]{\xi^{i}}{\tau } &= R^{i}{}_{\mu \nu j} u^{\mu} u^{\nu } \xi^{j}
\,,
\end{align}
%
therefore we need to find out what the four-velocity is. To zeroth order it is \(u^{\mu } = (1, \vec{0})\), and the first order contributions would result in a second-order term since the Riemann tensor itself is only nonzero at first order. Also, to zeroth order \(\tau = t\).
Also, the Christoffel symbols themselves are first order, so the \(\Gamma \Gamma \) terms are of second order.
With all of these considerations, the expression reduces to
%
\begin{align}
\dv[2]{\xi^{i}}{t} &= R^{i}{}_{t t j} \xi^{j} = \qty( \partial_{t} \Gamma^{i}_{t j} - \partial_{j} \Gamma^{i}_{tt}) \xi^{j}
\,.
\end{align}
%

The relevant Christoffel symbols for a perturbed metric \(g = \eta + h\), where the only nonzero components of \(h\) are the spatial ones (which holds as long as we are in the TT gauge), are to first order
%
\begin{align}
\Gamma^{i}_{t j } &= \frac{1}{2} \eta^{i k }
\qty( 2 g_{k (j , t )} - g_{t j, k })  \\
&= \frac{1}{2} \eta^{ik} h_{kj, t}   \\
\Gamma^{i}_{tt} &= \frac{1}{2} \eta^{i k}
\qty( 2 g_{k (t, t)} - g_{tt, k} ) = 0
\,,
\end{align}
%
so we get
%
\begin{align}
R^{i}{}_{t t j} &= \partial_{t} \Gamma^{i}_{t j} - \underbrace{\partial_{j} \Gamma^{i}_{tt}}_{ = 0}  \\
&= \frac{1}{2} \eta^{i k} h_{k j, tt}
\,.
\end{align}

Therefore, the equation for geodesic deviation in the proper detector frame to first order reads
%
\begin{align}
\dv[2]{\xi^{i}}{t} &= \qty( \frac{1}{2} \pdv[2]{h^{i}_{j}}{t} ) \xi^{j}
\,.
\end{align}

This can be interpreted as a Newtonian force, which acts on a particle with mass \(m\) as
%
\begin{align}
F_{(h)}^{i} = m \qty( \frac{1}{2} \pdv[2]{h^{i}_{j}}{t} ) \xi^{j}
\,.
\end{align}

\subsection{Fictional waves}

We want to show that the perturbation
%
\begin{align}
h_{\mu \nu } &= - 2 \partial_{(\mu } \zeta_{\nu )}  \\
\zeta_{\mu } &= c_\mu \exp( - i k_\alpha x^{\alpha })
\,
\end{align}
%
is ``fictional'', that is, it is indistinguishable flat spacetime.

One way to do so is to explicitly show the transformation that maps this to flat spacetime: the perturbation transforms under diffeomorphisms as
%
\begin{align}
h_{\mu \nu } (x) \to h'_{\mu \nu } (x') = h_{\mu \nu } (x) + 2 \partial_{(\mu } \chi_{\nu )} (x)
\,
\end{align}
%
for a generic vector \(\chi_{\nu }\), therefore if we set \(\chi _\nu = \zeta _\nu \) we directly have \(h'_{\mu \nu } (x') = 0\) identically.

Alternatively, we can compute the Riemann tensor: to linear order
%
\begin{align}
\Gamma_{\mu \alpha \beta } &= \frac{1}{2} \qty(h_{\mu  (\alpha , \beta )} - h_{\alpha \beta , \mu })  \\
R_{\mu \nu \rho \sigma } &= - 2 \Gamma_{\mu \nu [\rho , \sigma ]}
\,,
\end{align}
%
so we get
%
\begin{align}
h_{\mu \nu } &= 2 i c_{(\mu } k_{\nu )} e^{-i k \cdot x}  \\
h_{\mu \nu , \alpha } &= - 2 c_{(\mu } k_{\nu )} k_\alpha e^{-i k \cdot x} \\
h_{\mu \nu , \alpha \beta } &= + 2 i c_{(\mu } k_{\nu )} k_\alpha k_\beta e^{- i k \cdot x} \\
\Gamma_{\mu \alpha \beta } &= \frac{1}{2} \qty( h_{\mu \alpha , \beta } + h_{\mu \beta, \alpha } - h_{\alpha \beta , \mu })  \\
&= \frac{1}{2} (-2) \qty(
    c_{(\mu } k_{\alpha  )} k_\beta +
    c_{(\mu } k_{\beta   )} k_\alpha -
    c_{(\alpha  } k_{\beta   )} k_\mu
)
e^{-i k \cdot x}  \\
R_{\mu \nu \rho \sigma } &=
2 i \qty(
    c_{(\mu } k_{\nu)}  k_{[\rho} k_{\sigma ]} +
    c_{(\mu } k_{\rho)} k_{[\nu } k_{\sigma ]} -
    c_{(\nu } k_{\rho)} k_{[\mu } k_{\sigma ]}
) e^{- i k \cdot x}
\,,
\end{align}
%
so all the terms in the Riemann tensor contain a term in the form \(k_{[\mu } k_{\nu ]} = 0\), since it is the antisymmetrization of two equal vectors.

\section{NS surface tidal forces}

We want to estimate the tidal forces on a \(h =\SI{1.6}{m}\) tall person standing on the surface of a nonrotating neutron star with \(M = 1.4 M_{\odot}\) and \(R = \SI{12}{km}\).\footnote{A first sanity check: the corresponding Schwarzschild radius is around \SI{4}{km}, so the compactness ratio \(R c^2 / G M\) is almost 6, well above the causal constraint of \(\sim 2.83\) \cite[eq.\ 5, fig.\ 2]{lattimerNeutronStarObservations2007a}.}

We shall do so by comparing three different methods: Newtonian mechanics, weak-field GR and then full GR (still with the spherical, stationary NR assumption).
The weak-field GR formulation will make the various approximations of the Newtonian approach more explicit.

\subsection{Newtonian mechanics}

Here, the gravitational acceleration \(a (r)\) looks like
%
\begin{align}
a (r) = - \frac{GM}{r^2}
\,,
\end{align}
%
so the differential acceleration between the head and feet of our test person will be
%
\begin{align}
\Delta a = - \frac{GM}{(r + h)^2} + \frac{GM}{r^2} \approx 2GM \frac{h}{r^3} + \order{ (h / r)^2}
\,,
\end{align}
%
where the scale of the term we are neglecting is \((h / r)^2 \sim (\num{e-4})^2 = \num{e-8}\) --- definitely acceptable for this rough calculation.

With this expression we get \(\Delta a \approx \SI{3.4e8}{m/s^2}\) (both in the exact case and with the approximation).

\subsection{Weak-field GR}

In general, the expression for the geodesic deviation \(d^{\mu }\) corresponding to an observer described by a space-like vector \(\xi^{\mu }\) moving along a geodesic with four-velocity \(u^{\mu }\)  is \cite[eq.\ 3.208]{carrollSpacetimeGeometryIntroduction2019}
%
\begin{align}
d^{\mu } = R^{\mu }{}_{\nu \rho \sigma  } u^{\nu } u^{\rho } \xi^{\sigma }
\,.
\end{align}

Our stationary observer on the surface is not moving along a geodesic, but as a working hypothesis we can assume that
the acceleration due to being stationary in Schwarzschild coordinates is shared by head and feet --- it is the zeroth-order contribution, so to speak.
The geodesic deviation \(d^{\mu }\) goes directly to the first order.

In the weak-field approximation, we directly consider the component \(d^{r}\) to estimate \(\abs{\Delta a}\), since we assume (wrongly, in this case!) that the metric is not far off from the Minkowski one.
The space-like displacement vector describing the person is \(\xi^{\mu } = h \hat{e}^{r}\), while their four-velocity in this weak-field context is \(u^{\mu } = \hat{e}^{t}\).

Therefore, the formula simplifies to
%
\begin{align}
\abs{\Delta a} \approx \abs{d^{r}} = \abs{h R^{r}{}_{ttr} }
\,,
\end{align}
%
and we can approximate this component of the Riemann tensor as such:
%
\begin{align}
-R^{r}_{ttr} =
+R^{r}_{trt} &= \partial_{r} \Gamma^{r}_{tt} - \underbrace{\partial_t \Gamma^{r}_{rt}}_{\text{zero by stationarity}}
\underbrace{\Gamma^{r}_{r \mu } \Gamma^{\mu }_{tt} - \Gamma^{r}_{t \mu } \Gamma^{\mu}_{r t}}_{\text{second order}}  \\
&\approx \partial_{r} \Gamma^{r}_{tt} = \frac{1}{2} \partial_{r} \qty[ g^{r \mu } (\underbrace{2\partial_{t} g_{(\mu t)}}_{\mathclap{\text{zero by stationarity}}}  - \partial_{\mu } g_{00})]  \\
&\approx - \frac{1}{2} \partial_{r} \partial_{r} g_{00}
\approx \partial_{r} \partial_{r} \Phi  \\
&\approx \partial_{r} \partial_{r} \qty(- \frac{GM}{r}) = \partial_{r} \frac{GM}{r^2} = - \frac{2GM}{r^3}
\,,
\end{align}
%
so our result is exactly the same as the Newtonian one: \(\Delta a = 2GM h / r^3 = \SI{3.4}{m / s^2}\).

\subsection{Full GR}

Now that we have established the weak-field calculation, let us approach it accounting for the strong-field regime.

The geodesic deviation equation is still valid, but we must adapt the expressions for \(\xi^{\mu }\) and \(u^{\mu }\):
we want their moduli to be \(h\) and \(-1\) respectively, and their directions to be \(\hat{e}^{r} \) and \(\hat{e}^{t}\).
In order for this to be true we need to rescale the, so that their nonzero components will read \(\xi^{r} = h / \sqrt{g_{rr}}\) and \(u^{t} = 1/\sqrt{g_{tt}}\).

Also, we cannot only compute \(d^{r}\): we must consider the full vector \(d^{\mu }\) and compute its modulus \(\sqrt{d^{\mu } d_\mu }\).
Notice the lowered index: the component \(d^{r}\) appears in the modulus of the acceleration as \(d^{r} \sqrt{g_{rr}}\), and \(\sqrt{g_{rr}} \approx \num{1.23}\) is noticeably different from 1 in this situation.

This way, we have all the components to plug into a computer algebra system like Cadabra:\footnote{See the code \href{https://jacopok.github.io/schwarzschild.html}{here}!}
the acceleration comes out to have \(d^{r}\) as its only nonvanishing component,
%
\begin{align}
d^{r} = \frac{1}{r} \frac{h}{r} \frac{2GM}{r} \qty(1 - \frac{2GM}{rc^2})^{-1/2}
\,,
\end{align}
%
whose modulus is then
%
\begin{align}
\abs{d} = d^{r} \sqrt{g_{rr}} = \frac{2GMh}{r^3}
\,,
\end{align}
%
which, incidentally, is proportional to the square root of the Kretschmann scalar \(K = R^{\mu \nu \rho \sigma } R_{\mu \nu \rho \sigma } \propto G^2M^2c^{-4} r^{-6}\).

This is the same result we found in the weak field regime!
The factors from the metric cancelled out.

\subsection{Full GR, revised}

There is a problem in the computation we did before: we applied the formula for geodesic deviation despite the fact that our observer is not moving along a geodesic.

An alternative way to calculate the tidal deformation would be to simply compute the acceleration at the feet and head and subtract them.
Let us see how this approach goes.

Both the head and feet are stationary in Schwarzschild coordinates; their four-velocities are therefore time-directed, and normalized like before as \(u^{\mu } = \hat{e}^{t} / \sqrt{g_{tt}}\).

The four-acceleration of an observer is the derivative of the four-velocity along itself (with respect to proper time):
%
\begin{align}
a^{\mu } = \dv{u^{\mu }}{\tau } = u^{\nu } \nabla_{\nu } u^{\mu }
\,,
\end{align}
%
whose only nonvanishing component is
%
\begin{align}
a^{r} = \Gamma^{r}_{tt} \qty( u^{t})^2 = \frac{GM}{r^2}
\,,
\end{align}
%
but, like before, we need to account for the metric component if we want to compute its modulus:
%
\begin{align}
\abs{a} = a^{r} \sqrt{ g_{rr}} = \frac{GM}{r^2} \frac{1}{\sqrt{1 - 2GM / r}}
\,.
\end{align}

Now, all that is left is to compute the difference between this quantity at \(r\) and \(r + h\):
%
\begin{align}
\Delta a =
\frac{GM}{(r + h)^2} \frac{1}{\sqrt{1- \frac{2GM}{c^2(r+h)}}}
-
\frac{GM}{r^2} \frac{1}{\sqrt{1- \frac{2GM}{c^2r}}}
\,,
\end{align}
%
which could be approximated to linear order like before,
but we can directly compute the difference:
it comes out to be \(\Delta a \approx \SI{4.8e8}{m / s^2}\).

This shows that, accounting for relativistic effects,
an observer staying still on the surface of the star experiences \(\sim \SI{40}{\percent}\) more tidal acceleration.

\end{document}