\section{Methods}
As our project is to create a model with reinforcement learning, specifically DQN,
we do not have a predefined dataset. With this in mind, our methodology can be separated
into three parts.

\subsection{Reinforcement Learning}
Reinforcement Learning (RL) is a subset of Machine Learning (ML) where the
aim is to teach a model, called agent, via its interactions with the surrounding environment.
This method of learning requires no set of labeled or unlabeled data to be collected before
the learning actually starts. Instead the agent, typically a neural network,
is used for predicting the optimal action to be taken at each step based on its observation
and a reward is determined by the environment which is used for training the agent.
In RL, this environment is modeled as a Markov Decision Process (MDP)~\autocite{BUSONIU20188}.

\subsubsection{Markov Decision Process}
MDP is a mathematical framework based on Markov Chains for decision making processes
with inherent randomness. In Markov Decision Processes, we define:
\begin{itemize}
    \item \(S\): State Space (finite set),
    \item \(A\): Action Space (finite set),
    \item \(A_s\): Set of actions available at state \(s\),
    \item
          \(P^a_{ss'} = P_a(s,s') = P(s_{t+1} = s' \;|\; s_t = s, a_t = a)\) is the probability of action \(a\) in state \(s\) leading to state \(s'\),
    \item \(R_a(s, s')\): Reward received after transitioning from \(s\) to \(s'\).
\end{itemize}

\subsubsection{Markov Property}
For RL agents to work with MDPs we need the environment to be fully observable,
meaning that state \(s\) must capture all the characteristics of the environment.
In more technical terms, any state is \(S\) must satisfy the Markov Property which is defined as.
\begin{equation}
    P[s_{t+1} \;|\; s_t] = P[s_{t+1} \;|\; s_1, \ldots, s_t]
\end{equation}
This property essentially enables the environment to be memoryless which is required for
Markov Chains and more importantly its extension MDPs.

\subsubsection{Policy}
The objective in an MDP is to optimize the \textit{policy} of the decision making algorithm.
Here, we define the function \(\pi(s)\) that outputs the action chosen by the decision maker
based on the current state \(s\). This optimization mainly done by maximizing the cumulative
reward function. This function can be expressed as:
\begin{equation}
    G_t = \sum^{\infty}_{t=0}{\gamma^t R_a(s_t, s_{t+1})},\;
\end{equation}
where \(0\leq \gamma \leq 1\) is the discount factor. The equation above introduces the concept of \textit{discount factors}. This parameter is quite important as it is one of the hyperparameters of RL training loops. It is useful for avoiding cyclic behavior and infinite returns, and representing an exponentially increasing uncertainty for the future time steps.

Moreover, the policy that maximizes the function given above is regarded as as the
\textit{optimal policy} and denoted as \(\pi^*(s)\). It should be noted that this optimal policy is not necessarily unique.

\subsubsection{State-Value Function}
The state-value function, or just value function, is denoted by \(V_\pi(s)\). It is the expected return stating from state \(s\) and following policy \(\pi \) of an MDP\@. In most applications, it is used to evaluate how good being in a state is. It is mathematically expressed as:
\begin{equation}
    V_\pi(s) = E_\pi[G_t \;|\; s_t = s]
\end{equation}
As can be seen in the equation above, calculating the cumulative reward function is required
to find the value function of a state. This can be decomposed into a recursive function
as the current reward plus the discounted value function of the successor by utilizing
the Bellman Equation.
\begin{equation}
    V_\pi(s) = E_\pi[R_{t+1} + \gamma V_\pi(s_{t+1}) \;|\; s_t = s]
\end{equation}
With this done, we now define the state-value function that is produced by the optimal
policy \(\pi^*\) as the \textit{optimal state-value function}. In mathematical terms:
\begin{equation}
    V_*(s) = \max_\pi{V_\pi(s)}
\end{equation}

\subsubsection{Action Value Function}
The action value function, also called the Q-function, is the expected return starting from state \(s\), taking action \(a\), and then following policy \(\pi \). This is expressed as:
\begin{equation}
    Q_\pi(s,a) = E_\pi[G_t \;|\; s_t = s, a_t = a]
\end{equation}
Similar to the state-action function, we can also decompose this into a recursive function by redefining
\begin{equation}
    G_{t} = R_{t+1} + \gamma Q_\pi(s_{t+1}, a_{t+1}),
\end{equation}
and define the optimal action-value function as:
\begin{equation}
    Q_*(s,a) = \max_\pi{Q_\pi(s,a)}
\end{equation}

\subsubsection{Finding the Optimal Policy}
In all MDPs, three conditions are satisfied:
\begin{itemize}
    \item An optimal policy \(\pi^*\) exists (not necessarily unique),
    \item Optimal policy achieves the optimal state-value function
          \begin{equation}
              V_{\pi^*}(s) = V_*(s)
          \end{equation}
    \item Optimal policy achieves the optimal action-value function
          \begin{equation}
              Q_{\pi^*}(s,a) = Q_*(s, a)
          \end{equation}
\end{itemize}
These three assumptions can be used to shows that finding the optimal policy
\(\pi^*\) to solve the MDP can be done by maximizing over \(Q_*(s,a)\) with:
\begin{equation}
    \pi_*(a | s) =
    \begin{cases}
        1 & \text{if }\argmax_{a}{Q_*(s,a)} \\
        0 & \text{otherwise}
    \end{cases}
\end{equation}
This equation essentially means that finding the optimal policy can be done by
following the path given by \(Q_*(s,a)\) assuming that the optimal Q function is known~\autocite{mnih2013playing}.

\subsection{Neural Networks}
Artificial Neural Networks (ANNs), or simply Neural Networks (NNs), are
machine learning models that are loosely based on real life neurons and their connections.
Although the theoretical work for these types of models were mostly developed in mid
20\textsuperscript{th} century, the applications of them were quite limited considering
the computational power required for the necessary calculations.

\begin{figure}[h]
    \centering{}
    \includegraphics[width=\linewidth, height=0.3\textheight, keepaspectratio]{img/ann.png}
    \caption{A Basic ANN Structure~\autocite{buseyarentekin}}~\label{fig:ANN}
\end{figure}

In our project, the observation provided to the RL agent compromises of screenshots of
the game's graphics. Therefore, the NN we used is made up of mainly convolutional layers
for image processing with smaller fully connected layers at the end for decision making.
The entire neural network used is:
\begin{enumerate}
    \item Convolutional Layer: 32 filters,
    \item ReLu Activation Layer,
    \item Convolutional Layer: 64 filters,
    \item ReLu Activation Layer,
    \item Convolutional Layer: 64 filters,
    \item ReLu Activation Layer,
    \item Fully Connected Layer: 512 wide,
    \item ReLu Activation Layer,
    \item Fully Connected Layer: 3 wide
\end{enumerate}

\subsection{Deep Q Learning (DQN)}
The standard Q learning algorithm uses the training episodes to fill up a Q table where
each value corresponds to a specific action taken in a specific state. This method
functions properly when the amount of state action pairs are relatively low.
For example, the ``Cliff Walking Problem'', shown below, is simple enough that a Q table would be the feasible. In fact, the table would only have between 160 to 192 entries depending on  how the environment is defined.

\begin{figure}[h]
    \centering{}
    \includegraphics[width=\linewidth, height=0.3\textheight, keepaspectratio]{img/cliff_walking.png}
    \caption{Cliff Walking Problem~\autocite{Cliff_photo}}~\label{fig:CliffWalk}
\end{figure}

For more complicated environments, this approach quickly loses its feasibility.
In those cases, like what we have in Breakout, a neural network is used as a
nonlinear function approximator to replace the table. In DQN, the nn provides a
mapping from the give state \(s\), to the Q function of all possible actions in that
state \(Q(s,\underline{a})\).\\
The training loop of our network has a few additional, but still standard, features.
These are:
\begin{enumerate}
    \item
          \(\epsilon \)-greedy Policy: To simulate \textit{exploration}, the action will be chosen randomly with probability \(\epsilon \),
    \item Experience Replay Memory: To combat the unstable nature of deep reinforcement
          learning, a circular memory with a set size will be used to reduce the correlations
          between the sequence of observations used in every training batch.
    \item Target Network: An additional \textit{target network} is used to increase the
          stability of the learning.
\end{enumerate}
The final learning algorithm is given in~\autoref{algo:dqn} in~\autoref{app:dqn}.


\documentclass[12pt, a4paper]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{listings}
\usepackage{caption}
\usepackage{physics}
\usepackage[shortlabels]{enumitem}

\setlength\parindent{0pt}
\newcommand{\code}{\lstinline[basicstyle=\small]}
\lstset{
    language=Python,
    basicstyle=\scriptsize
}

\title{EE2703: Applied Programming Lab \\ \Large Assignment 6: The Laplace Transform}
\author{Soham Roy \\ \normalsize EE20B130}
\date{\today}
\begin{document}

\maketitle % Insert the title, author and date

\section{Introduction}
The goals of this assignment are:
\begin{itemize}
    \item To analyze LTI Systems using Laplace Transform.
    \item To see how RLC systems can be used as a low pass filter.
    \item To understand how to use the \code{scipy.signals} toolkit.
    \item To plot graphs to understand the Laplace Transform.
\end{itemize}

\section{Subquestions}
\subsection{Time Response of a Spring}
We use the Laplace transform to solve a simple spring system.
The system is characterized by the differential equation:
\[\dv[2]{x}{t} + 2.25x = f(t)\]
with initial values being 0.
The Laplace Transform of the differential equation is:
\[H(s) =  \frac{1}{s^2+2.25}\]
The input signal is of the form \(f(t) = \cos{(\omega t)}\exp(-at)u(t)\),
where $a$ is the decay factor and $\omega$ is the frequency of the cosine. \\
The Laplace Transform of the input signal is:
\[ F(s) = \frac{s+a}{(s+a)^2+\omega^2 }\]
First, these function are defined using numpy polynomials and multiply to get the output Laplace Transform.
Then, the ILT of the function is evaluated using \code{sp.impulse} to get the time domain sequences.
This is done for $\omega=1.5$ (natural frequency of the system), and decay of 0.5.
\pagebreak

The time response is evaluated using:
\begin{lstlisting}
    def time_response_spring(decay):
        den = np.polymul([1, 0, 1.5**2], [1, 2*decay, decay**2 + 1.5**2])
        return sp.lti([1, decay], den)
\end{lstlisting}

The impulse response is then plotted using:
\begin{lstlisting}
    t, x = sp.impulse(time_response_spring(0.5), T=np.linspace(0, 50, 501))
    plot(t, x, "Damped Oscillator with 0.5/s Decay", "$t$", "$x$")
\end{lstlisting}

Thus, we get:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{1.png}
\end{figure}

\subsection{Time Response with a Smaller Decay}
We do the same for a decay of 0.05. The impulse response is then plotted using:
\begin{lstlisting}
    _, x = sp.impulse(time_response_spring(0.05), T=t)
    plot(t, x, "Damped Oscillator with 0.05/s Decay", "$t$", "$x$")
\end{lstlisting}

Thus, we get:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{2.png}
\end{figure}

\subsection{Time Response at Varying Frequencies}
The frequency of the cosine term in $f(t)$ is varied from 1.4 to 1.6 in steps of 0.05
and its effect plotted using:
\begin{lstlisting}
    H = sp.lti([1], [1, 0, 1.5 ** 2])

    for i, freq in enumerate(np.linspace(1.4, 1.6, 5)):
        f = np.cos(freq * t) * np.exp(-0.05 * t) * (t >= 0)
        x = sp.lsim(H, f, t)[1]
        plt.plot(t, x)
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.55]{3.png}
\end{figure}

When the input frequency is at the natural frequency, the output amplitude is maximum.
In the other cases the output amplitude decreases. \\
This is due to resonance.

\subsection{Coupled Spring Problem}
In this problem we have two differential equations and two variables to solve for.
The equations are:
\[\dv[2]{x}{t} +(x-y) = 0 \]
\[\dv[2]{y}{t} +2(y-x) = 0 \]
with initial condition as $x(0) = 1$. $y$ in the second equation is substituted from the first,
and a fourth order differential equation in terms of $x$ is obtained.
Simplifying this results in:
\[X(s) = \frac{s^2+2}{s^3+3s} \]
\[Y(s) =  \frac{2}{s^3+3s} \]
Taking the ILT of these two expressions gives the time domain expressions of $x(t)$ and $y(t)$:
\begin{lstlisting}
    # d2x/dt2 + (x - y) = 0
    # d2y/dt2 + 2*(y - x) = 0
    t, x = sp.impulse(sp.lti([1, 0, 2], [1, 0, 3, 0]), T=np.linspace(0, 20, 201))
    _, y = sp.impulse(sp.lti([2], [1, 0, 3, 0]), T=t)

    plot(t, x)
    plot(t, y, "Coupled Springs", "t", "$x(t)$  &  $y(t)$", ["$x(t)$", "$y(t)$"])
\end{lstlisting}
These have been plotted as functions of time:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{4.png}
\end{figure}

\subsection{Magnitude \& Phase Responses of Transfer Function}
The transfer function of the given RLC filter is:
\[ H(s) = \frac{1}{LCs^2 + RCs + 1}\]
with the initial conditions being zero. This is evaluated using:
\begin{lstlisting}
    H = sp.lti([1], [1e-6 * 1e-6, 100 * 1e-6, 1])
\end{lstlisting}
The magnitude and phase responses of the transfer function are plotted using:
\begin{lstlisting}
    w, S, phi = H.bode()
    plt.semilogx(w, S)
    plt.semilogx(w, phi)
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{5.png}
\end{figure}

From the Bode plots, it is clear that the RLC System is a second order low pass filter,
with a 3db bandwidth of $10^4$ rad/s.
\pagebreak

\subsection{Analysis of the Output Signal}
The input is of the form:
\[v_i(t) = \cos{(10^3t)}u(t) - \cos{(10^6t)}u(t)\]
The output voltage $v_o(t)$ is obtained by:
\begin{lstlisting}
    def v_o(t, H):
        v_i = np.cos(1e3 * t) * (t >= 0) - np.cos(1e6 * t) * (t >= 0)
        return sp.lsim(H, v_i, t)[1]
\end{lstlisting}

The output voltage is then plotted using:
\begin{lstlisting}
    y = v_o(np.linspace(0, 30e-6, 10000), H)
    plot(np.linspace(0, 30, 10000), y,
         "Output Voltage for $0<t<30\mu$s (short term)",
         "$t$ (in $\mu$s)", "$V_o(t)$")

    y = v_o(np.linspace(0, 15e-3, 10000), H)
    plot(np.linspace(0, 15, 10000), y,
         "Output Voltage for $0<t<15m$s (long term)", "$t$ (in ms)", "$V_o(t)$")
\end{lstlisting}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{6a.png}
\end{figure}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{6b.png}
\end{figure}

From the Bode plot, it is clear that the RLC System is a second order low pass filter,
with a 3db bandwidth of $10^4$ rad/s. \\
The short term response plot shows that the capacitor is charging up to meet the input amplitude. \\
The high frequency component can be seen as a ripple in the short term response plot.
This component is highly attenuated and hence not visible in the long term response plot. \\
In the long term response plot, the low frequency component passes almost unchanged,
and the amplitude is almost 1. This is because $\omega = 10^3$ rad/s is well within the 3-dB bandwidth
($\omega_{3dB} = 10^4$ rad/s) of the system. \\
Clearly, this demonstrates the fact that this is a low pass filter with bandwidth
$\omega_{3dB} = 10^4$ rad/s.

\section{Conclusion}
\begin{itemize}
    \item We analyzed LTI Systems using the Laplace Transform.
    \item We saw a low pass filter constructed from an RLC circuit.
    \item We used the scipy signals toolkit to calculate the time domain response and the Bode Plot.
    \item We plotted graphs to understand the above.
\end{itemize}

\end{document}


%*******************************************************************************
%*********************************** First Chapter *****************************
%*******************************************************************************
%!TEX root = 0.main.tex

\section{Graph Spherical Convolutions}\label{sec:Graph Spherical Convolutions}
In this chapter we first introduce DeepSphere \cite{DeepSphere}, an example of Graph Spherical Convolutional Neural Network that uses a modified version of the HKGL to perform graph convolutions and hierarchical pooling to achieve rotation equivariance, and computational efficiency. In section \ref{sec:Chapter2:pointwise convergence of the Heat Kernel Graph Laplacian on the Sphere} we prove a pointwise convergence result of the full HKGL in the case of the sphere using a regular deterministic sampling scheme. In section \ref{sec:Chapter2:How to build a good graph} we show a way of modifying the original graph of DeepSphere to improve its spectral convergence to $\Delta_{\mathbb S^2}$ while managing to contain the computational costs of graph convolutions.
\subsection{Graph Spherical Convolutional Neural Networks} \label{sec:Chapter1:DeepSphere}
Perraudin et al. \cite{DeepSphere} have proposed a Spherical CNN to process and analyze spherical sky maps, as the Cosmic Radiation map in figure \ref{fig:cosmicradiation}. Sky images are modeled as signals on the vertices of a \textit{sparse} graph $G$ on the vertex set $V$ of the image pixels $(v_i)$ with weights
$$
w_{ij}=\exp \left(-\frac{\norm {v_i-v_j}^2}{4t}\right)
$$
where the kernel width $t$ is a parameter to optimize. In an earlier work, Belkin et al. \cite{NIPS2006_2989} proved the convergence of eigenvectors of the \textit{full} graph $G$ to the eigenfunctions of the Laplace Beltrami operator $\Delta_{\mathbb S^2}$, when the data is sampled from a uniform distribution on $\mathbb S^2$ (see Section \ref{sec:Chapter1:theoretical foundations}, theorem \ref{theo:spectral convergence}). For this reason and for the intuition we presented at the beginning of Chapter 2 when introducing the mean equivariance error $\overline E_G$, we expect the construction of Perraudin et al. to work well only for images that were sampled with equi area sampling schemes. On such sampling schemes, since the graph Laplacian eigenvectors well approximate the spherical harmonics, the graph convolution (\ref{eq:graph convolution}) well approximates the true spherical convolution (\ref{eq:convolution}).
\begin{equation}\label{eq:approx}
\mathbf f^\intercal\mathbf v_{i(\ell, m)} \approx \int_{\eta \in \mathbb S^2}f(\eta)Y_\ell^m(\eta)d\mu(\eta)=\hat f(\ell,m)
\end{equation}
The most used sampling scheme by cosmologists and astrophysics is called HEALPix \cite{HEALPix}, and is the one implemented in DeepSphere. HEALPix is an acronym for Hierarchical Equal Area isoLatitude Pixelization of a sphere. This sampling scheme produces a subdivision of the sphere in which each pixel covers the same surface area as every other pixel. It is parametrized by a parameter $N_{side}\in\mathbb N$, and is made of $n=12N_{side}^2$ pixels. The points of this sampling lie on isolatitude rings, making it possible to implement an FFT algorithm for the discrete SHT. The minimal resolution for HEALPix is given by $N_{side}=1$ and is made by 12 pixels. For each increasing value of $N_{side}$ each patch is divided into 4 equal area patches centered around the pixels of the new sampling (figure \ref{fig:healpix sampling}).

In chapter \ref{sec:Chapter4} of this work we'll deepen the relationship between the continuous spherical Fourier transform and the graph Fourier transform in case of non-uniform sampling measures. Perraudin et al. propose an efficient implementation of the graph convolution (\ref{eq:graph convolution}) to be implemented in each layer of their Graph Spherical Convolutional Neural Network. They propose to learn only those filters that are polynomials of degree $q$ of the eigenvalues $\lambda_i$
\begin{equation}\label{eq:deepsphere filter}
\begin{aligned}
k(\lambda) &= \sum_{j=0}^{q} \theta_{j} \lambda^{j}\\
\Omega_k \mathbf f &= \mathbf{V}\left(\sum_{j=0}^{q} \theta_{j} \mathbf{\Lambda}^{j}\right) \mathbf{V}^{\top} \mathbf{f}=\sum_{j=0}^{q} \theta_{j} \mathbf{L}^{j} \mathbf{f}
\end{aligned}
\end{equation}
Learning the filter $k$ means learning the $q+1$ coefficients $\theta_j$. In this way they solve different problems at once: first, to compute the graph convolution (\ref{eq:graph convolution}) there's no need of computing the expensive eigen decomposition of $\mathbf L$, but they just need to evaluate a polynomial of the sparse matrix $\mathbf L$. Thanks to a suitable parametrization of the polynomial $\sum_{j=0}^{q} \theta_{j} \mathbf{L}^{j}$ in term of Chebyshev polynomials, they manage to reduce the computations needed to evaluate such filter to $\mathcal O(|E|)$. Since in a NN this filtering operation has to be computed in every forward and backward step of the training phase, this gain in efficiency is dramatically important and led to speedups of different orders of magnitude compared to the architecture of Cohen et al. (see Chapter 5, table \ref{tab:SHREC17_class}). The filtering operation (\ref{eq:deepsphere filter}) can be seen also in the vertex domain as a weighted sum of the $q$-neighborhoods of each vertex. This is due to the fact that $\mathbf L $ has the same sparsity structure of the adjacency matrix of the graph, and thus $(\mathbf L^q)_{ij}$ will be non-zero if and only if the vertices $v_i, v_j$ are connected by a path of length $q$.

\begin{figure}
	\centering
	\includegraphics[width=0.5\textwidth]{figs/chapter1/healpix.jpg}
	\caption{\label{fig:healpix sampling}HEALPix sampling for $N_{side}=1,2,3,4$ \cite{HEALPix}}
\end{figure}

 Despite theorem \ref{theo:spectral convergence} \cite{Belkin:2005:TTF:2138147.2138189} states the spectral convergence of the \textit{full} HKGL to $\Delta_{\mathbb S^2}$, the \textit{sparse} version of the HKGL of Perraudin et al. does not seem to show such convergence. In figure (\ref{fig:Old spectrum1}) we see the correspondence between the subspaces spanned by the graph Fourier modes of the graph Laplacian used by Perraudin et al. and the true spherical harmonics. We can also see the plot of the graph eigenvalues: we can see that they clearly come in groups of $(2\ell+1)$ eigenvalues corresponding to each $\ell$th degree of the spherical harmonics. We thus call $\mathbf v_{i(\ell, m)}$ the $i$th graph eigenmode corresponding the the one of degree $\ell$ and order $m$. We compute the normalized Discrete Spherical Harmonic Transform (DSHT) of each $\mathbf v_{i(\ell, m)}$ up to the degree $\ell_\text{max}$. The entry $(\ell, \kappa)$ of the matrix represented in the figure corresponds to the percentage of energy of the $\ell$th eigenspace $V_\ell = \text{span}\{\mathbf v_{i(\ell, -\ell)}, \mathbf v_{i(\ell, -\ell+1)},...,\mathbf v_{i(\ell, \ell)}\}$ contained in the $\kappa$th eigenspace of the true spherical harmonics.
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=1\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/deepsphere_original.png}
		\includegraphics[width=1\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/deepsphere_original_diagonal.png}
		\includegraphics[width=1\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/05_figs/old_results3.png}
	\end{center}
	\caption{\label{fig:Old spectrum1}Alignment of the eigenvectors of the graph Laplacian of the DeepSphere graph, the starting point of this work. In the middle we plot the diagonals of the matrices on top, and on the bottom we plot its spectrum for $N_{side}=16$.}
\end{figure}
In a perfect situation, this matrix would be the identity matrix, being all the energy of the $\ell$th graph eigenspace contained in the corresponding one spanned by the true spherical harmonics. It can be seen that the eigenmodes of the graph Laplacian span almost the same subspaces as the spherical harmonics in the low frequencies, but this alignment gets worse at higher frequencies. Furthermore, it can be noticed that even by improving the resolution of the graph, the low frequency eigenspaces do not get better aligned.

\subsection{Pointwise convergence of the Heat Kernel Graph Laplacian on the Sphere}

\label{sec:Chapter2:pointwise convergence of the Heat Kernel Graph Laplacian on the Sphere}
Here we prove a pointwise convergence result of the full graph Laplacian in the case of the sphere on a deterministic sampling scheme that is regular enough. Our proof will be constructed following the ideas presented in the proof of theorem \ref{theo:Belkin pointwise convergence}.
\vspace{0.5cm}
\begin{definition}{}(Heat Kernel Graph Laplacian operator)\\
	\label{def:Heat Kernel Graph Laplacian operator}
	\text{Given a sampling $\{x_i\in\mathcal M\}_{i=0}^{n-1}$ of the manifold we define the \textbf{operator} }$L_n^t$ such that
	$$L_n^tf(y) := \frac{1}{n}\left[ \sum_{i=0}^{n-1} \exp{-\frac{||x_i-y||^2}{4t}}\left(f(y)-f(x_i)\right)\right]$$
\end{definition}
\vspace{0.5cm}
Observe that the Heat Kernel Graph Laplacian operator restricted on the sample points $x_0, ..., x_{n-1}$ acts as the usual Heat Kernel Graph Laplacian matrix $\mathbf L_n^t$ rescaled by a factor of $\frac{1}{n}$:
$$L_n^tf(x_i) = \frac{1}{n} (\mathbf L_n^t\mathbf f)_i$$.
\vspace{0.5cm}
\begin{snugshade*}
\begin{theorem}{from \cite[Belkin et al.]{Belkin:2005:TTF:2138147.2138189}}\\
	\label{theo:Belkin pointwise convergence}
	Let $\mathcal M$ be a $k$-dimensional compact smooth manifold embedded in some euclidean space $\mathbb R^N$, and fix $p\in\mathcal M$. Let the data points $x_1, ... x_n$ be sampled form a uniform distribution on the manifold $\mathcal M$. Set $t_n=n^{-\frac{1}{k+2+\alpha}}$, for any $\alpha>0$ and let $f\in\mathcal C_\infty(\mathcal M)$. Then:

	$$\forall \epsilon>0\quad \mathbb{P}\left[\left|\frac{1}{t}\frac{1}{(4 \pi t)^{k/2}}L_{n}^{t_n} f(p)-  \frac{1}{\text{vol}(\mathcal M)}L^{t_n} f(p)\right|>\epsilon\right] \xrightarrow{n\to\infty} 0$$
\end{theorem}
\end{snugshade*}
\vspace{0.5cm}
This theorem states a convergence in probability of $L_n^t$ to $L^t$, that is far from being as strong as spectral convergence of theorem \ref{theo:spectral convergence}. However, we want to show that a similar result still holds in the specific case of the manifold $\mathcal M$ being the 2-Sphere $\mathbb S^2$ and where the points $x_1, ..., x_n$ are not sampled from a random distribution on $\mathbb S^2$, but are defined by the HEALPix sampling. To understand the differences between theorem \ref{theo:Belkin pointwise convergence} and theorem \ref{theo:pointwise convergence in the healpix case} it is useful to first review the proof of theorem \ref{theo:Belkin pointwise convergence}. For this proof we'll need to use the Hoeffding's inequality that we recall here under:

(\textit{Hoeffding's inequality})\\
Let \(X_{1}, \ldots, X_{n}\) be independent identically distributed random variables, such that
\(\left|X_{i}\right| \leqslant K .\) Then
\begin{equation}\label{eq:Hoeffding}
\mathbb P\left\{\left|\frac{\sum_{i} X_{i}}{n}-\mathbb{E} X_{i}\right|>\epsilon\right\}<2 \exp \left(-\frac{\epsilon^{2} n}{2 K^{2}}\right)
\end{equation}

\begin{proof}[Proof of Theorem \ref{theo:Belkin pointwise convergence}]
 The first step is to observe that for any fixed $t>0$, any fixed function $f$ and a fixed point $y\in\mathbb S^2$,  the Heat Kernel Graph Laplacian $L_n^t$ is an unbiased estimator for the Functional Approximation of the Laplace-Beltrami $L^t$. In other words, $L_n^tf(y)$ is the empirical average of $n$ i.i.d. random variables $X_i= e^{-\frac{||x_i-y||^2}{4t}}\left(f(y)-f(x_i)\right)$ with expected value corresponding to $L^tf(y)$. Thus,
\begin{equation}
\label{eq:expected value of heat kernel grah laplacian}
	\mathbb E L_n^tf(y) = 	\mathbb E \frac{1}{n}X_i = \mathbb E X_i = L^tf(y),
\end{equation}
and by the strong law of large numbers we have that
\begin{equation}
\label{eq:convergence in probability}
\lim_{n\to\infty}L_n^tf(y) = L^t(y).
\end{equation}
The core of the work of Belkin et al. is the proof, that we will not discuss, of the following proposition.

\begin{prop} Under the same hypothesis of theorem \ref{theo:Belkin pointwise convergence}, we have the following pointwise convergence
	$$\frac{1}{t}\frac{1}{(4\pi t)^{k/2}} L^tf(p) \xrightarrow{t\to 0 } \frac{1}{\text{vol}(\mathcal M)}\triangle_{\mathcal M}f(p).$$
	\label{prop:3}
\end{prop}
Thanks to Proposition \ref{prop:3} and equation (\ref{eq:convergence in probability}), a straightforward application of Hoeffding's inequality with $K=\frac{1}{t}\frac{1}{(4\pi t)^{k/2}}$ together with equation  (\ref{eq:expected value of heat kernel grah laplacian}) leads to

\begin{equation}
	\label{eq:hoeffding applied}
	\mathbb{P}\left[\frac{1}{t(4 \pi t)^{k / 2}}\left|L_{n}^{t} f(y)- L^{t} f(y)\right|>\epsilon\right] \leq 2 e^{-1 / 2 \epsilon^{2} n t(4 \pi t)^{k / 2}}
\end{equation}

We want the right hand side of equation (\ref{eq:hoeffding applied}) to go to $0$ for $n\to\infty, t\to0$ at the same time. For this to happen, we need to find a sequence $(t_n)$ such that
$$\begin{cases}
t_n\xrightarrow{n\to\infty}0\\
2 e^{-1 / 2 \epsilon^{2} n t_n(4 \pi t_n)^{k / 2}}\xrightarrow{n\to\infty}0\\
\end{cases}$$

By fixing $t_n=n^{-\frac{1}{k+2+\alpha}}$, for any $\alpha>0$, it is easy to check that \\$-1 / 2 \epsilon^{2} n t_n(4 \pi t_n)^{k / 2}\xrightarrow{n\to\infty}+\infty$, thus concluding the proof.

\end{proof}

Now we can observe that in order to adapt this proof to the case of the sphere with an equi area sampling scheme we need to modify two key things. First, due to the deterministic nature of the sampling scheme, we need to prove that for any fixed $t>0$, any fixed function $f$ and any point $y\in\mathbb S^2$
\begin{equation}\label{eq:limit}
	\left|L_n^tf(y)-L^tf(y)\right|\xrightarrow{n\to \infty} 0,
\end{equation}
without relying on the strong law of large numbers. Once proven such result, we need to prove that

$$\left|\frac{1}{4\pi t^2}\left(L_n^tf(x) - L^tf(x)\right)\right|\xrightarrow[n\to \infty]{t\to 0}0$$

We need now to define some geometrical quantities that we'll need. Given a sampling $x_0, ..., x_{n-1}$ define $\sigma_i$ to be the patch of the surface of the sphere corresponding to the $i$th point of the sampling, define $A_i$ to be its corresponding area and $d_i$ to be the radius of the smallest ball in $\mathbb R^3$ containing the i-th patch (see Figure \ref{fig:Geometric characteristics of a patch}). Define $d^{(n)} := \max_{i=0, ..., n}d_i$ and $A^{(n)}=\max_{i=0, ..., n}A_i$.\\
Once proven the limit (\ref{eq:limit}), Proposition \ref{prop:3} leads to our main result:
\vspace{1cm}
\begin{snugshade*}
	\begin{theorem}
		For a sampling $\mathcal P = \{x_i\in\mathbb S^2\}_{i=0}^{n-1}$ of the sphere that is equi area and such that $d^{(n)})\leq \frac{C}{\sqrt{n}}$, for all $f: \mathbb S^2 \rightarrow \mathbb R$ Lipschitz with respect to the euclidean distance in $\mathbb R^3$, for all $y\in\mathbb S^2$, there exists a sequence $t_n = n^\beta$ such that the rescaled Heat Kernel Graph Laplacian operator $\frac{|\mathbb S^2|}{4\pi t_n}L^t_n$ converges pointwise to the Laplace Beltrami operator on the sphere $\triangle_{\mathbb S^2}$  for $n\to\infty$:
		$$ \lim_{n\to\infty}\frac{|\mathbb S^2|}{4\pi t_n} L_n^{t_n}f(y) =  \triangle_{\mathbb S^2}f(y).$$
		\label{theo:pointwise convergence in the healpix case}
	\end{theorem}
\end{snugshade*}

\vspace{1cm}
\begin{minipage}{.4\textwidth}
	\centering
	\includegraphics[width=0.8\linewidth]{figs/chapter1/d_iA_i.jpg}
	\captionof{figure}{\label{fig:Geometric characteristics of a patch}Geometric characteristics of the $i$th patch}
\end{minipage}%
\hfill
\begin{minipage}{.5\textwidth}
	\centering
	\includegraphics[width=\linewidth]{figs/chapter1/Heal_Base.png}
	\captionof{figure}{\label{fig:HEALPix equal areas patches}HEALPix equal areas patches for $N_{side}=1$, $N_{side}=2$}
	\vspace{0.5cm}
\end{minipage}

\subsubsection{Proof of the pointwise convergence of the Heat Kernel Graph Laplacian on the Sphere for an equi area sampling scheme}

Our first goal is to prove the following Proposition:
\vspace{0.5cm}
\begin{prop}\label{prop:1}
	For an equal area sampling $\{x_i\in\mathbb S^2\}_{i=0}^{n-1}: A_i=A_j \forall i,j$ of the sphere it is true that for all $f: \mathbb S^2 \rightarrow \mathbb R$ Lipschitz with respect to the euclidean distance $||\cdot||$ with Lipschitz constant $\mathcal L_f$
	$$
	\left| \int_{\mathbb S^2}f({ x})\text{d}{\mu(x)} - \frac{1}{n}\sum_i f( x_i)\right|\leq \mathcal L_fd^{(n)}.
	$$
	Furthermore, for all $y\in\mathbb S^2$ the Heat Kernel Graph Laplacian operator $L^t_n$ converges pointwise to the functional approximation of the Laplace Beltrami operator $L^t$
	$$ L_n^tf(y)\xrightarrow{n\to\infty} L^tf(y).$$
\end{prop}
\vspace{0.5cm}

\begin{proof}
	Let us assume that the function $f:\mathbb R^3\rightarrow \mathbb R$ is Lipschitz with Lipschitz constant $\mathcal L_f$, we have

	$$\left| \int_{\sigma_{i}}f({ x})\text{d}{\mu(x)} - \frac{1}{n}f( x_i)\right| \leq \mathcal L_fd^{(n)}\frac{1}{n} $$

	So, by triangular inequality and by summing all the contributions of all the $n$ patches
	$$\left| \int_{\mathbb S^2}f({ x})\text{d}{\mu(x)} - \frac{1}{n}\sum_i f( x_i)\right| \leq \sum_i \left| \int_{\sigma_{i}}f({ x})\text{d}{\mu(x)} - \frac{1}{n}f( x_i)\right|\leq n  \mathcal L_fd^{(n)}\frac{1}{n} = \mathcal L_fd^{(n)}$$
	Thanks to this result, we have the following two pointwise convergences

	$$\forall f \text{ Lipschiz,}\quad \forall y\in\mathbb S^2,  \quad\quad \frac{1}{n}\sum_i e^{-\frac{||x_i-y||^2}{4t}}\rightarrow \int e^{-\frac{||x-y||^2}{4t}}d\mu(x)$$
	$$\forall f \text{ Lipschiz,}\quad \forall y\in\mathbb S^2,  \quad\quad \frac{1}{n}\sum_i e^{-\frac{||x_i-y||^2}{4t}}f(x_i)\rightarrow \int e^{-\frac{||x-y||^2}{4t}}f(x)d\mu(x)$$

	Definitions \ref{def:Heat Kernel Graph Laplacian operator} and \ref{def:Functional approximation to the Laplace-Beltrami operator} end the proof.
\end{proof}
\vspace{0.5cm}

Now, we just proved that \textit{keeping t fixed} $L_n^tf(x)\rightarrow L^tf(x)$. Now our goal is to prove that:

\vspace{0.5cm}
\begin{prop}\label{prop:2}
	Given a sampling regular enough i.e., for which we assume $A_i=A_j \ \forall i,j\text{ and }d^{(n)}\leq \frac{C}{\sqrt{n}}$, for a fixed $t>0$, a fixed Lipschitz function $f$ and a fixed point $y\in\mathbb S^2$ there exists a sequence $t_n = n^\beta, \beta<0$ such that
$$
\forall f \text{ Lipschitz, } \forall x\in\mathbb S^2 \quad \left|\frac{1}{4\pi t_n^2}\left(L_n^{t_n}f(x) - L^{t_n}f(x)\right)\right|\xrightarrow{n\to \infty}0.
$$
\end{prop}
\vspace{0.5cm}

The main result of this section, theorem  \ref{theo:pointwise convergence in the healpix case}, is then an immediate consequence of Proposition \ref{prop:2} and Proposition \ref{prop:3}.

\begin{proof}[Proof of Proposition \ref{prop:2}]

	We define for simplicity of notation
	\begin{align*}
		\phi^t(x;y) &:= e^{-\frac{||x-y||^2}{4t}}\left(f(y)-f(x)\right)\\
		K^t(x,y) &:=  e^{-\frac{||x-y||^2}{4t}}
	\end{align*}
	We start by writing the following chain of inequalities
	\begin{align*}
		||L_n^tf-L^tf||_\infty &= \max _{y\in \mathbb S^2} \left|L_n^tf(y)-L^tf(y)\right|\\
		&= \max _{y\in \mathbb S^2} \left| \frac{1}{n} \sum_{i=1}^n \phi^t(x_i; y)- \int_{\mathbb S^2} \phi^t(x;y)d\mu(x) \right|\\
		&\leq \max _{y\in \mathbb S^2}  \sum_{i=1}^n   \left| \frac{1}{n}  \phi^t(x_i; y)- \int_{\sigma_i} \phi^t(x;y)d\mu(x) \right|\\
		&\leq  \max _{y\in \mathbb S^2} \left[\mathcal L_{\phi^t_y}d^{(n)} \right]\\
	\end{align*}
	where $\mathcal L_{\phi^t_y}$ is the Lipschitz constant of $x \rightarrow \phi^t(x, y)$ and where we used for the last inequality Proposition \ref{prop:1}. If we assume $d^{(n)}\leq \frac{C}{\sqrt{n}}$ we have that

	$$||L_n^tf-L^tf||_\infty  \leq  \max _{y\in \mathbb S^2} \left[ \mathcal L_{\phi^t_y} \frac{C}{\sqrt{n}} \right]$$

	Let's now find the explicit dependence $t\rightarrow \mathcal L_{\phi^t_y}$
	\begin{align*}
		\mathcal L_{\phi^t_y} &= ||\partial_x\phi^t(\cdot;y)||_\infty\\&
		= ||\partial_x\left(K^t(\cdot;y)f\right)||_\infty\\&
		= ||\partial_x K^t(\cdot;y)f + K^t(\cdot;y)\partial_x f||_\infty\\&
		\leq ||\partial_x K^t(\cdot;y)f||_\infty + ||K^t(\cdot;y)\partial_x f||_\infty\\&
		\leq  ||\partial_x K^t(\cdot;y)||_\infty||f||_\infty + ||K^t(\cdot;y)||_\infty||\partial_x f||_\infty\\&
		= ||\partial_x K^t(\cdot;y)||_\infty||f||_\infty + ||\partial_x f||_\infty\\&
		= \mathcal L_{K^t_y} ||f||_\infty + ||\partial_xf||_\infty\\&
		= \mathcal L_{K^t_y} ||f||_\infty + \mathcal L_f
	\end{align*}
	where $\mathcal L_{K^t_y}$ is the Lipschitz constant of $x\rightarrow K^t(x;y)$. We can observe that such constant does not depend on $y$:

	$\mathcal L_{K^t_y} = \norm{\partial_x e^{-\frac{x^2}{4t}}}_\infty = \norm{\frac{x}{2t}e^{-\frac{x^2}{4t}}}_\infty = \left. \frac{x}{2t}e^{-\frac{x^2}{4t}}\right|_{x=\sqrt{2t}}=(2et)^{-\frac{1}{2}}\propto t ^ {-\frac{1}{2}}$

	So we can continue
	\begin{align*}
		\max _{y\in \mathbb S^2} \left[  \mathcal L_{\phi^t_y} \frac{C}{\sqrt{n}} \right]
		&\leq  \frac{C}{\sqrt{n}} \left( (2et)^{-\frac{1}{2}} \norm{f}_\infty + \mathcal L_f \right)\\
		&\leq \frac{C\norm{f}_\infty}{\sqrt{n}(2et)^\frac{1}{2}} +   \frac{C}{\sqrt{n}}\mathcal L_f\\
	\end{align*}
	So we have that, rescaling by a factor $\frac{1}{4\pi t^2}$
	\begin{align*}
		\norm{\frac{1}{4\pi t^2}\left(L_n^tf-L^tf\right)}_\infty&\leq \frac{1}{4\pi t^2}\norm{\left(L_n^tf-L^tf\right)}_\infty \\
		&\leq \frac{C}{4\pi}\left[\frac{\norm{f}_\infty}{\sqrt{2e}}\frac{1}{\sqrt{n}t^\frac{5}{2}} + \frac{\mathcal L_f}{\sqrt{n}t^2}\right]
	\end{align*}

	we want $\begin{cases}
	t \rightarrow 0\\
	n \rightarrow \infty\\
	\sqrt{n}t^\frac{5}{2} \rightarrow \infty\\
	\sqrt{n}t^2 \rightarrow \infty
	\end{cases}$ in order for $ \frac{C}{4\pi}\left[\frac{\norm{f}_\infty}{\sqrt{2e}}\frac{1}{\sqrt{n}t^\frac{5}{2}} + \frac{\mathcal L_f}{\sqrt{n}t^2}\right] \xrightarrow[t\to 0 ]{n\to\infty}0$

	This is true if $\begin{cases}
	t(n) = n^\beta, &\beta\in(-\frac{1}{5}, 0) \\
	t(n) = n^\beta, &\beta\in(-\frac{1}{4}, 0)
	\end{cases} \implies t(n) = n^\beta, \quad \beta\in(-\frac{1}{5}, 0)$

	Indeed

	$\sqrt{n}t^\frac{5}{2}=n^{5/2\beta+1/2}\xrightarrow{n \to \infty} \infty$ since $\frac{5}{2}\beta+1/2>0 \iff \beta>-\frac{1}{5}$

	$\sqrt{n}t^2=n^{2\beta+1/2}\xrightarrow {N \to \infty} \infty$ since $2\beta+1/2>0 \iff \beta>-\frac{1}{4}$

	So, for $t=n^\beta$ with $\beta\in(-\frac{1}{5}, 0)$ we have that

	$$\begin{cases}
	(t_n)\xrightarrow{n\to\infty}0\\
	\norm{\frac{1}{4\pi t_n^2}L_n^{t_n}f-\frac{1}{4\pi t_n^2}L^{t_n}f}_\infty  \xrightarrow{n\to\infty}0
	\end{cases}$$

\end{proof}

The proof of theorem \ref{theo:pointwise convergence in the healpix case} is now trivial:
\begin{proof}[Proof of Theorem \ref{theo:pointwise convergence in the healpix case}]
	Thanks to Proposition \ref{prop:2} and Proposition \ref{prop:3}	we conclude that $\forall y\in\mathbb S^2 $
	$$\lim_{n\to\infty}\frac{1}{4\pi t_n^2} L_n^{t_n}f(y) =  \lim_{n\to\infty}\frac{1}{4\pi t_n^2} L^{t_n}f(y) = \frac{1}{|\mathbb S^2|}\triangle_{\mathbb S^2}f(y) $$
\end{proof}

The proof of this result is instructive since it shows that we need to impose some regularity conditions on the sampling. If the sampling is equal area as HEALPix, meaning that all the patches $\sigma_i$ have the same area (i.e., HEALPix, see figure \ref{fig:HEALPix equal areas patches}), then we need to impose that $ d^{(n)}\leq \frac{1}{\sqrt{n}}$. If the sampling is not equal area, meaning that in general $A_i\neq A_j$, it can be shown that we need a slightly more complex condition: $\max_{i=0,...,n-1}d_iA_i\leq Cn^{-\frac{3}{2}}$.\\
In the work of Belkin et al. \cite{Belkin:2005:TTF:2138147.2138189} the sampling is drawn form a uniform random distribution on the sphere, and their proof heavily relies on the uniformity properties of the distribution from which the sampling is drawn. In our case the sampling is deterministic, and the fact that for a sphere there doesn't exist a regular sampling with more than 12 points (the vertices of a icosahedron) is indeed a problem that we need to overcome by imposing the regularity conditions above.

To conclude, we can see that the result obtained has the same form than the result obtained in \cite{Belkin:2005:TTF:2138147.2138189}. Given the kernel density $t(n)=n^\beta$, if Belkin et al. proved convergence in the random case for $\beta \in (-\frac{1}{4}, 0)$, we proved convergence in the HEALPix case for $\beta \in (-\frac{1}{5}, 0)$. This kind of result can be interpreted in the following way. In order to have this pointwise convergence, we need to reduce the kernel width but \textit{not so fast} compared to the resolution of the graph. In other words, the kernel width has to be reduced but is somewhat limited by the resolution of the graph. In the next section we'll see how to set in practice a good kernel width $t$ given a graph resolution $n$.
\begin{remark}
	Pointwise convergence is just a necessary condition for spectral convergence.  Theorem \ref{theo:pointwise convergence in the healpix case} does not imply convergence of eigenvalues and eigenvectors.
\end{remark}

\subsection{How to build a good graph to approximate spherical convolutions}
\label{sec:Chapter2:How to build a good graph}
The current state of the art of rotation equivariant Graph CNN is DeepSphere \cite{DeepSphere}. However, if we measure the alignment of the eigenspaces spanned by the eigenvectors of its graph Laplacian and the ones spanned by the spherical harmonics we see that it does not get better as $N_{side}$ increases (figure \ref{fig:deepsphere results}). We'll see that the main cause of this bad behavior of the eigenspaces is the fixed number of neighbors used for the construction of the graph. In this subsection we'll see that \textit{to obtain the desired spectral convergence it is necessary to increase the number of neighbors as we decrease the kernel width} $t$. We'll follow in practice what we did in proving theorem \ref{theo:pointwise convergence in the healpix case}: first we'll build a full graph, and let the number of pixels $n$ increase while keeping the kernel width $t$ fixed. After having discussed the results, we'll try to find a sequence $(t_{N_{side}})$ to obtain the expected spectral convergence. Only in the end we'll find a way to make the graph sparse to limit the computational costs of graph convolutions but keeping the eigen decomposition of the graph Laplacian as close to the spherical harmonics as possible.

\subsubsection{Full graph, $n\to\infty$}\label{sec:Chapter1: n to infty}
Here we analyze what happens to the power density spectrum of the \textit{full} Heat Kernel Graph Laplacian as we make $n$ go to infinity while keeping $t$ fixed. Since in the previous section we proved that (Proposition \ref{prop:3}) for a sampling regular enough and a fixed $t$, a fixed function $f$, a fixed point $y$
$$L_n^tf(y)\xrightarrow{n\to\infty}L^tf(y)$$
Since HEALPix is a very regular sampling of the sphere, we expect to observe (even if we didn't prove it) the corresponding spectral convergence proved in theorem \ref{theo:spectral convergence}. The results obtained are in figure \ref{fig:n to infinity1}, \ref{fig:n to infinity3}.

\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/n.png}
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/n_diagonal.png}
	\caption{\label{fig:n to infinity1}Alignment of the eigenvectors of the HKGL with a fixed kernel width $t$}

\end{figure}
\begin{figure}[h!]
	\centering
	\includegraphics[width=0.49\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/n_eigenvalues.png}
		\includegraphics[width=0.49\textwidth]{figs/chapter1/trueeigenvalues.png}
	\caption{\label{fig:n to infinity3}Left: spectrum of the HKGL with a fixed kernel width $t$. Right: true spectrum of $\Delta_{\mathbb S^2}$}
\end{figure}

In figure \ref{fig:n to infinity1} we see two things: first that there's a frequency threshold beyond which the Graph Laplacian is completely blind, approximately located at the 15th degree, and second that before this frequency threshold, we actually see the convergence expected: the alignment gets better as $n$ gets bigger.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.45\textwidth]{figs/chapter1/frequency_threshold1.png}	\hfill
	\includegraphics[width=0.45\textwidth]{figs/chapter1/frequency_threshold2.png}
	\caption{\label{fig:n to infinity4}Frequency threshold explained.}
\end{figure}

 To explain this we refer to figure \ref{fig:n to infinity4} where we show a simplified situation where we are sampling the interval $[0, 2]$ and we plot the Gaussian kernel centered around the first pixel of the sampling corresponding to the origin. On the leftmost image the pixels are correctly spaced with respect to the kernel width, in the sense that the values of the kernel evaluated on the pixels are well far apart from each other. This makes the graph able to "see" all the pixels differently, and thus all the frequencies with wavelength around the order of magnitude of the average pixel distance will be captured by the graph. On the rightmost image in figure \ref{fig:n to infinity4} there are too many pixels with respect to the kernel width: the values of the kernel evaluated on the pixels close to the origin, because of the slope of the kernel being almost zero are too close to each other (in red); because of this any variation of a signal on the red pixels would be almost invisible to the graph Laplacian. With this fixed kernel width $t$, no matter how much we sample the interval $[0,2]$, any frequency with wavelength shorter than the radius $r\approx0.25$ becomes invisible to the graph Laplacian.\\
 This phenomenon can be seen also in the spectrum represented in figure \ref{fig:n to infinity3} where we plot the eigenvalues of the matrix $\mathbf L_n^t$: as $N_{side}$ gets bigger the eigenvalues get more and more grouped in the usual groups of the same multiplicity of the corresponding spherical harmonics; however, there's a frequency (corresponding approximately to the degree $\ell=15$) from which all the eigenspaces tend to merge into one, corresponding to the eigenvalue $1$.

\subsubsection{Full graph, $t\to 0$}
In this section we fix the parameter $N_{side}$ and and we make the kernel width $t$ go to $0$.
\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/t_sensitivity}
	\caption{\label{fig:t_sensitivity_eigenspaces}Alignment of the eigenspaces of the HKGL with a fixed number of points $n$ corresponding to $N_{side}=8$}
\end{figure}
\begin{figure}
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/t_sensitivity_diagonal.png}
	\caption{\label{fig:t_sensitivity_diagonal}Whole trend}
\end{figure}%
\begin{figure}
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/t_sensitivity_diagonal_2.png}
	\caption{\label{fig:t_sensitivity_diagonal_2}First trend: error stays low for low frequencies, and gets lower for high frequencies}
	\vspace{0.5cm}
\end{figure}
\begin{figure}
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/t_sensitivity_diagonal_1.png}
	\caption{\label{fig:t_sensitivity_diagonal_1}Second trend: error gets higher for both high and low frequencies}
\end{figure}%
\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{figs/chapter1/t.png}
	\caption{\label{fig:weights}One row of the weight matrix for different values of the kernel width $t$ plotted on the HEALPix sampling with $N_{side}=8$. On the left, for a too large $t$, the HKGL can not capture high frequencies. On the right, for a too small $t$, the graph becomes almost completely disconnected. In the center a good value for $t$ makes the HKGL able to see the most frequencies.}
\end{figure}

Results are in figure \ref{fig:t_sensitivity_eigenspaces}, \ref{fig:t_sensitivity_diagonal}: Starting from $t=0.32$, the error in the high frequencies starts to get smaller, while the error in the low frequencies keeps staying low (Figure \ref{fig:t_sensitivity_diagonal_2}) up to $t=0.05$. For $t$ that gets smaller and smaller up to $t=0.01$, we get worse alignment both in high and low frequencies (Figure \ref{fig:t_sensitivity_diagonal_1}). This behavior can be explained with the same arguments used in the previous section: high values of the kernel width correspond to a very flat kernel (figure \ref{fig:weights}, left), and thus the graph loses the capacity of individuating high frequencies as discussed before. On the opposite side, low values of the kernel width correspond to a very peaked kernel, that causes all the weights to be close to zero and thus the graph becomes less and less connected loosing every capability of identifying different frequencies  (figure \ref{fig:weights}, right).

\subsubsection{Putting it together: full graph, $n\to\infty$ and $t\to 0$}
A grid search has been used to find the following optimal kernel width for different values of $N_{side}$, always in the case of a full graph, where we maximized the number of graph eigenspaces with the alignment value in figure \ref{fig:optimal graph} bigger than $80\%$. The optimal values of the kernel width $t$ are shown in figure \ref{fig:t}. In DeepSphere $t$ is set to the average of the non zeros weight matrix entries, where the number of neighbors of each vertex is fixed between 7 and 8. We can see that the heuristic way of DeepSphere of setting the standard deviation $t$ produces results of the same order of magnitude of the optimal value. It can be seen that the optimal values of $t$ are very close to a linear trend in the log-log plot, showing an approximately polynomial relationship with the parameter $N_{side}$ that could be used to extrapolate possible values of $t$ for higher $N_{side}$.
\begin{figure}[h]
	\centering
	\includegraphics[width=0.7\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/kernelwidth.png}
\caption{\label{fig:t}Standard deviation of the Gaussian kernel  in a log-log plot. A straight line indicates a polynomial relation.}
\end{figure}
\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_full.png}
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_full_diagonal.png}
\caption{\label{fig:optimal graph}Alignment of eigenspaces of the optimal full graph.}
\end{figure}
In figure \ref{fig:optimal graph} we can appreciate that for a full graph, each time we double the parameter $N_{side}$, we approximately double the degree $\ell$ at which the graph eigenvectors are correctly aligned with the spherical harmonics.
\subsubsection{Reducing the number of neighbors}
For what it concerns how to make the graph sparse, the intuition is the following: remember that we want our graph Laplacian to approximate the operator $L^t$, that for sufficiently small $t$ approximates $\Delta$
$$\frac{1}{n}\left(\sum_i e^{-\frac{||x_i-y||^2}{4t}}(f(y)-f(x_i)) \right)  \approxeq  \int_{\mathbb S^2} e^{-\frac{||x-y||^2}{4t}}\left(f(y)-f(x)\right)d\mu(x) \approxeq \Delta_{\mathbb S^2} f(y)$$

So far we showed how to do so optimally with a full graph; however, a full graph comes at the cost of leading to a matrix $\mathbf L_n^t$ that is full, and thus to a graph filtering cost of $\mathcal O(n^2)$, worse than the common SHT cost of $\mathcal O(n^{3/2})$. Perraudin et al. \cite{DeepSphere} constructed a nearest neighbor graph constraining the number of neighbors for each vertex to be fixed, making the graph filtering cost linear in the number of pixels. However, as we saw at the beginning of this section this leads to a poor alignment of the graph eigenvectors with the spherical harmonics and thus to a not so optimal rotation equivariance. Here we propose a different approach, based on the following intuition: making the graph sparse means deciding which weights $w_{ij}=\exp{-\frac{||x_i-x_j||^2}{4t}}$ to set to zero. For this approximation to be accurate we want to set to zero only those weights that are small enough: let's define a new \textit{epsilon graph} $G'$ by fixing a threshold $\epsilon$ on $w_{ij}=e^{-\frac{||x_i-x_j||^2}{4t}}$ such that

$$w'_{ij} = \begin{cases}
e^{-\frac{||x_i-x_j||^2}{4t}}\quad& \text{if } e^{-\frac{||x_i-x_j||^2}{4t}} \geq \epsilon\\
0 \quad & \text{if } e^{-\frac{||x_i-x_j||^2}{4t}} < \epsilon
\end{cases}$$

By setting $\epsilon = 0.01$ - or equivalently, thresholding the weights at \\$\norm{x_i-x_j}\approx 3\sigma$ where $\sigma=\sqrt{2t}$ is the standard deviation of the kernel - in figure \ref{fig:optimal_thresholded} we can see the usual alignment plots of the graph $G'$:

\begin{table}[h]
	\centering
	\begin{tabular}{ c|c}
		$N_{side}$ & Number of neighbors \\
		1 & 11 \\
		2 & 16 \\
		4 & 37 \\
		8 & 43 \\
		16 & 52 \\
	\end{tabular}
\caption{\label{table:NN}}
\end{table}

\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_thresholded.png}
	\includegraphics[width=\textwidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_thresholded_diagonal.png}
	\caption{\label{fig:optimal_thresholded}Optimal construction \textbf{thresholded at $k=0.01$}}
\end{figure}

We see that we need to increase the number of neighbors as $N_{side}$ gets bigger. Again, the intuition is the following: to have spectral convergence (a strong type of convergence) we need more and more global information and more precise. By fitting the relationship
$$
\text{Number of neighbors} = (N_{side})^\alpha
$$
to the data in table \ref{table:NN} we obtain that $\alpha$ should be close to $1/2$, meaning that the complexity graph filtering with $G'$ could be approximated by
$$
\mathcal O(|E|) = \mathcal O(n\sqrt{N_{side}})  = \mathcal{O}(n^{5/4}).
$$
where $n$ is the number of vertices of the graph. This complexity is exactly in the middle between the linear complexity of DeepSphere and the complexity $\mathcal O(n^{3/2})$ of the SCNNs of Cohen and Esteves \cite{SCNN} \cite{Esteves}. In practice the number of neighbors grows very slowly with the number of pixels, making graph convolutions with $G'$ still very efficient and fast (see section \ref{sec:Chapter5:Experimental validation}, table \ref{table:results}).

To conclude, we show in figure \ref{fig:Old spectrum}, \ref{fig:New spectrum} a confront between the alignment of the graph Laplacian eigenvectors of the DeepSphere graph $G$, the starting point of this work, and  of the graph $G'$. It can appreciated how the alignment plots show a much better behavior of the graph Laplacian eigenvectors of $G'$, and how the spectrum of $G'$ resembles more accurately the spectrum of $\Delta_{\mathbb S^2}$.\\
\begin{minipage}{.5\textwidth}
	\centering
	\vspace{0.4cm}
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/deepsphere_original.png}
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/deepsphere_original_diagonal.png}
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/05_figs/old_results3.png}
	\captionof{figure}{\label{fig:Old spectrum}Alignment of the graph Laplacian eigenvectors of the DeepSphere graph $G$, the starting point of this work, and its spectrum.}
\end{minipage}%
\begin{minipage}{.5\textwidth}
	\centering
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_thresholded.png}
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_thresholded_diagonal.png}
	\includegraphics[width=0.95\linewidth]{../codes/02.HeatKernelGraphLaplacian/HEALPix/06_figures/optimal_thresholded_eigenvalues.png}
	\captionof{figure}{\label{fig:New spectrum}Alignment of the graph Laplacian eigenvectors of the proposed graph $G'$, and its spectrum.}
\end{minipage}
\subsubsection{Equivariance error}
So far we used the plots of the alignment of the eigenvectors with the spherical harmonics as a proxy of the quantity we are really interested in, the mean equivariance error $\overline E_G$, because they gave us more valuable interpretations about what was happening. Now we want to have the confirmation that the proposed graph $G'$ led to a smaller mean equivariance error than the one of the original graph $G$ of DeepSphere.
In figure \ref{fig:DeepSphere equivariance error} we plot the mean equivariance error
$$\overline E = \mathbb E_{f, g}\ E(f, g)
$$ of the diffusion filter $k(\lambda_i) = \exp(-\lambda_i)$ for both graphs $G, G'$ by spherical harmonic degree $\ell$ at different resolutions. This was obtained as the empirical average over a uniform sample of rotations $g\in SO(3)$ and a uniform sample of functions $f\in V_\ell = \text{span}\{Y_\ell^m, |m|\leq \ell\}$. We recall that for HEALPix there's no sampling theorem that guarantees the existence of an exact reconstruction operator $T^{-1}$, so to calculate this quantity we had to rotate the sampled signal in the discrete domain, introducing important interpolation errors. We can see that DeepSphere has a mean equivariance error that is almost 30\% in the low frequencies, and decreases slowly for higher ones. The graph $G'$ has a much better behavior: the error stays low for the small frequencies, rising up for the higher ones and always remaining confined under 5\%. To compare it we reported also the results for the full HKGL, where we can appreciate a behavior similar to $G'$ but with an error always smaller than 2\%.

\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{../codes/06.Equivariance_error/DeepSphereonHEALPix.png}
	\includegraphics[width=\textwidth]{../codes/06.Equivariance_error/OptimalHKGLonHEALPix.png}
		\includegraphics[width=\textwidth]{../codes/06.Equivariance_error/FullHKGLonHEALPix.png}
	\caption{\label{fig:DeepSphere equivariance error}Mean equivariance error of the diffusion filter $\exp(-\Lambda)$ for $G$, $G'$ and the full HKGL, by spherical harmonic degree. Notice the difference in the scale of the y axis for DeepSphere, that reaches errors up to 30\%.}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{../codes/06.Equivariance_error/img_example_DeepSphere/DS.png}
	\caption{\label{fig:DeepSphere equivariance error in practice}DeepSphere V1 equivariance error. On the left, a signal $f$. Top right, $f$ was first rotated and then filtered through a diffusion filter $k(\lambda) = \exp (-\lambda)$. Bottom right, $f$ was first filtered and then rotated. The difference in the two outcomes is evident.}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{../codes/06.Equivariance_error/img_example_thresholdedHKGL/thresholdedHKGL.png}
	\caption{\label{fig:Optimal equivariance error in practice}DeepSphere V2 equivariance error. On the left, a signal $f$. Top right, $f$ was first rotated and then filtered through a diffusion filter $k(\lambda) = \exp (-\lambda)$. Bottom right, $f$ was first filtered and then rotated. No difference in the two outcomes is visible to the human eye.}
\end{figure}

In figure \ref{fig:DeepSphere equivariance error in practice} we see the visualization of the equivariance error of DeepSphere: on the left, the original sampled signal $Tf$. Top right, the rotated and filtered signal $\Omega_k T \Lambda(g) f$. Bottom right, the filtered and rotated signal $T \Lambda(g) T^{-1} \Omega_k T f $. In figure \ref{fig:Optimal equivariance error in practice} we see the visualization of the equivariance error of the graph $G'$: on the left, the same, original sampled signal $Tf$. Top right, the rotated and filtered signal $\Omega_k T \Lambda(g) f$. Bottom right, the filtered and rotated signal $T \Lambda(g) T^{-1} \Omega_k T f $. No difference can be appreciated at a visual analysis for the graph $G'$, while for DeepSphere the difference is clearly visible.
\clearpage
To conclude, we report in table \ref{tab:final results}, as final metric for the evaluation of rotation equivariance of the two graphs $G$, $G'$, the mean equivariance error for a different values of $N_{side}$ that we computed by sampling random coefficients $\theta_\ell^m \in(0,1)$ of linear combinations of all the spherical harmonics up to degree $\ell=16$
$$f(x) = \sum_{\ell\leq 16,\ |m|\leq\ell}\theta_\ell^m Y_\ell^m(x)
$$
and by averaging on random rotations $g\in SO(3)$. Our graph shows lower errors as $N_{side}$ grows, and a much lower error than DeepSphere.

\begin{table}
		\centering
\begin{tabular}{c|ccc}
	Mean equivariance error $\overline{E}$& $N_{side}=4$& $N_{side}=8$&$N_{side}=16$ \\\hline
	DeepSphere graph $G$ & 12.37\% & 12.03\% & 12.23\% \\
	Optimal graph $G'$ & 4.57\% & 3.98 \% & 1.54\%
\end{tabular}
\caption{\label{tab:final results}}
\end{table}


\section{Enhanced techniques}\label{sec:enhanced}
\subsection{Combining different data types - MT 1d inversion}\label{sec:mt1d}
File \file{doc/tutorial/code/enhanced/mt1dinv0.cpp}\\
In magnetotelluric (MT) inversion for every period an amplitude $\rho^a$ and a phase $\phi$ is computed from the electric and magnetic registrations. The file \file{1000\_100\_1000\_n5\_1.dat} contains a synthetic three layer case of 1000$\Omega$m-100$\Omega$m-1000$\Omega$m with 5\,\% Gaussian noise on the $\rho^a$ and 1 degree on the phases.
The three columns $T$, $\rho^a$ and $\phi$ are read extracted as vectors by
\begin{lstlisting}
    RMatrix TRP; //! real matrix with period(T), resistivity(R) & phase(P)
    loadMatrixCol( TRP, dataFileName ); //! read column-based file
    size_t nP = TRP.rows(); //! number of data
    RVector T( TRP[0] ), rhoa( TRP[1] ), phi( TRP[2] ); //! columns
\end{lstlisting}
It is based on the forward operator \lstinline|MT1dModelling| in \file{src/em1dmodelling.h/cpp}, giving back a vector that consists of the amplitudes and phases for each period.
Of course both have completely different valid ranges.
The phases a linearly related between 0 and $\pi/2$, whereas the amplitudes are usually treated logarithmically.
On the (1d block) model side, thickness and apparent resistivity use log or logLU transforms as done for DC resistivity.
\begin{lstlisting}
    /*! Model transformations: log for resistivity and thickness */
    RTransLog transThk;
    RTransLogLU transRho( lbound, ubound );
    /*! Data transformations: log apparent resistivity, linear phases */
    RTransLog transRhoa;
    RTrans transPhi;
\end{lstlisting}
Since amplitudes and phases are combined in one vector, we create a cumulative transformation of the two by specifying their lengths.
Similarly, the assumed relative error of the $\rho^a$ and $\phi$ are combined using a cat command.
\begin{lstlisting}
    CumulativeTrans< RVector > transData; //! combination of two trans
    transData.push_back( transRhoa, nP ); //! append rhoa trans (length nP)
    transData.push_back( transPhi, nP );  //! append phi trans
    RVector error( cat( RVector(nP,errRhoa/100), RVector(errPhase/phi) ) );
\end{lstlisting}

Similar to DC resistivity inversion, we create a 1d block mesh, initialise the forward operator and set up options for the two regions (0-thicknesses,1-resistivities).
Starting values for the $\rho_i$ and $d_i$ are computed by the mean apparent resistivity and an associated skin depth.
\begin{lstlisting}
    MT1dModelling f( T, nlay, false );
    double medrhoa = median( rhoa ); //! median apparent resistivity
    double medskindepth = sqrt( median( T ) * medrhoa ) * 503.0; //!skin d.
    f.region( 0 )->setTransModel( transThk ); //! transform
    f.region( 1 )->setTransModel( transRho );
    f.region( 0 )->setConstraintType( 0 ); //! min. length
    f.region( 1 )->setConstraintType( 0 );
    f.region( 0 )->setStartValue( medskindepth / nlay );
    f.region( 1 )->setStartValue( medrhoa );
    /*! Real valued inversion with combined rhoa/phi and forward op. */
    RInversion inv( cat( rhoa, phi ), f, verbose, dosave );
\end{lstlisting}

The rest is done as for DC resistivity block inversion.
In Figure~\ref{fig:mt1dblock-resres} the inversion result and its resolution matrix is shown.
The model is very close to the synthetic one and represents an equivalent solution.
This is also represented by the resolution matrix, where $\rho_1$, $\rho_3$ and $d_1$ are resolved almost perfectly, whereas $\rho_2$ and $d_2$ show slight deviations.
\begin{figure}[htbp]
\centering\includegraphics[width=0.7\textwidth]{mt1dblock-resres}\\[-3ex]
~\hfill a\hfill ~ \hfill ~~~~~b \hfill ~ \hfill ~ \hfill ~
\caption{a) Block 1d resistivity inversion result (red-synthetic model, blue-estimated model)) and b) resolution matrix}\label{fig:mt1dblock-resres}
\end{figure}

One can easily test the inversion only based on $\rho^a$ or $\phi$ by increasing the errors of the others by a large factor.
According to (\ref{eq:phid}) the corresponding weight goes to zero.
By skipping $\phi$ the model deviates slightly and the cell resolutions for $\rho_2$ and $d_2$ decrease to 0.9 and 0.86, respectively. If the $\rho^a$ are neglected the solution becomes obviously non-unique. Only $d_1$ is determined pretty well, the other parameters obtain cell resolutions of 0.6-0.8. Note also that for local regularisation the resolution does not contain the resolution of the preceding models \citep{friedel03}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Combining different parameter types - offsets in travel time}\label{sec:ttoffset}
Next, we consider a 2d travel-time tomographic problem.
In the library there is a forward operator called \cw{TTDijkstraModelling}, which is using a Dijkstra \citep{dijkstra} algorithm that restricts the ray paths to mesh edges. Although this is only an approximation, it is sufficiently accurate for high-quality meshes.
Assume the zero point (shot) of the traces is not exactly known. This might be due to long trigger cables, problems in the device or placing besides the profile.
Aim is to include an unknown delay for each shot position into the inversion\footnote{A similar problem is the issue of static shift in MT inversion caused by local conductivity inhomogeneity, which shifts the apparent resistivity curves.}.

First, we derive a new forward modelling class \cw{TTOffsetModelling}  from the existing \cw{TTDijkstraModelling} (abbreviated by \cw{TTMod}) since we want to use their functionality.
Additionally to the existing class we need the number of shot positions and a simple 0d/1d mesh holding the offset values for them added to the original mesh.
\begin{lstlisting}
class TTOffsetModelling : public TTMod {
public:
    TTOffsetModelling( Mesh & mesh, DataContainer & data )
      : TTMod( mesh, data ) {
        //! find occuring shots, and map them to indices starting from zero
        shots_ = unique( sort( dataContainer.get("s") ) );
        std::cout << "found " << shots_.size() << " shots." << std::endl;
        for ( size_t i = 0 ; i < shots_.size() ; i++ )
            shotMap_.insert( std::pair< int, int >( shots_[ i ], i ) );
        //! create new region containing offsets with special marker
        offsetMesh_ = createMesh1D( shots_.size() );
        for ( size_t i = 0 ; i < offsetMesh_.cellCount() ; i++ )
            offsetMesh_.cell( i ).setMarker( NEWREGION );
        regionManager().createRegion( NEWREGION, offsetMesh_ );
    }
...
\end{lstlisting}

The two functions \lstinline|response| and \lstinline|createJacobian| need to be overwritten.
However we want to expand the original functions by the changes needed.
This is straight forward for the response vector.
First part of the model is the slowness vector whose response is calculated calling the original function.

\begin{lstlisting}
    RVector TTOffsetModelling::response( const RVector & model ){
        //! extract slowness from model and call old function
        RVector slowness( model, 0, model.size() - shots_.size() );
        RVector offsets( model, model.size() - shots_.size(), model.size());
        RVector resp = TTMod::response( slowness ); //! normal response
        RVector shotpos = dataContainer_->get( "s" );
        for ( size_t i = 0; i < resp.size() ; i++ ){
            resp[ i ] += offsets[ shotMap_[ shotpos[ i ] ] ];
        }
        return resp;
    }
\end{lstlisting}

For the Jacobian the case is a bit more complicated.
Instead of increasing the size of the matrix a-posteriori, we use a block matrix type \lstinline|H2SparseMapMatrix| consisting of two horizontally concatenated matrices called by \lstinline|H1()| and \lstinline|H2()| \footnote{See appendix on matrices \ref{app:matrix} for existing matrix types.}.
The first is the normal way matrix holding the path lengths.
The second is a matrix with a value of 1 in the position of the shot number and 0 elsewhere.

\begin{lstlisting}
    RVector TTOffsetModelling::createJacobian( H2Matrix & jacobian,
                                          const RVector & model ){
        //! extract slowness from model and call old function
        RVector slowness( model, 0, model.size() - shots_.size() );
        RVector offsets( model, model.size() - shots_.size(), model.size());
        TTMod::createJacobian( jacobian.H1(), slowness );
        jacobian.H2().setRows( dataContainer_->size() );
        jacobian.H2().setCols( offsets.size() );
        //! set 1 entries for the used shot
        RVector shotpos = dataContainer_->get( "s" );
        for ( size_t i = 0; i < dataContainer_->size(); i++ ) {
            jacobian.H2().setVal( i, shotMap_[ shotpos[ i ] ], 1.0 );
        }
    }
\end{lstlisting}

As a result the model vector holds both slowness values and the offsets, which can be sliced out of the vector for individual post-processing.

%%%%%%%%%%%%%%%%%%%%%%%
\subsection{What else?}
\begin{itemize}
	\item Full waveform TDR inversion?
	\item Gravity 2d or 3d inversion?
	\item what is enhanced?
\end{itemize}


%!TEX root = thesis.tex

\chapter{Derivations and Equations}
\label{app:Derivations}
% --------------------------------------------------------- %
\section{Belief Update}
\label{app:BeliefUpdates}
% --------------------------------------------------------- %
\begin{equation}
  B_t^i \defeq p(S_t = i | A_{1:t}, O_{1:t})
\end{equation}

\begin{subequations}\label{belief_update}
  \begin{align}
    b_t^j &= p(S_t = j | a_{1:t}, o_{1:t})\label{belief_state} \\
          &= \frac{ p(S_t = j, o_t | a_{1:t}, o_{1:t-1}) } { p(o_t) } \\
          &= \frac{ p(o_t | S_t = j, a_{1:t}, o_{1:t-1}) p(S_t = j | a_{1:t}, o_{1:t-1})} { p(o_t) } \\
          \intertext{and since $o_t$ is independent of $o_{1:t-1}$ we can write}
          &= \frac{ p(o_t | S_t = j, a_{1:t}) p(S_t = j | a_{1:t}, o_{1:t-1})} { p(o_t) } \\
          &= \frac{ p(o_t | S_t = j, a_{1:t}) \sum_{i} p(S_t = j, S_{t-1} = i | a_{1:t}, o_{1:t-1})} { p(o_t) } \\
          &= \frac{ p(o_t | S_t = j, a_{1:t}) \sum_{i} p(S_t = j | S_{t-1} = i, a_{1:t}, o_{1:t-1}) p(S_{t-1} = i | a_{1:t-1}, o_{1:t-1})} { p(o_t) } \\
          \intertext{Markovian assumption; given $S_{t-1}$, $S_t$ is independent of $a_{1:t-1}, o_{1:t-1}$ so we get}
          &= \frac{ p(o_t | S_t = j, a_{1:t}) \sum_{i} p(S_t = j | S_{t-1} = i, a_t) p(S_{t-1} = i | a_{1:t-1}, o_{1:t-1}) } { p(o_t) } \\
          \intertext{Using \eqref{belief_state} we can write}
          &= \frac{ p(o_t | S_t = j, a_{1:t}) \sum_{i} p(S_t = j | S_{t-1} = i, a_t) b_{t-1}^i } { p(o_t) } \\
          \intertext{Markovian assumption again; given $S_t$, $o_t$ is independent of $a_{1:t-1}$ so we get}
          &= \frac{ p(o_t | S_t = j, a_t) \sum_{i} p(S_t = j | S_{t-1} = i, a_t) b_{t-1}^i } { p(o_t) }
  \end{align}
\end{subequations}
If the state never changes we have
\begin{equation}\label{fixed_target}
  p(S_t = j | S_{t-1} = i, a_t) =
  \begin{cases}
    1 & \text{if $j = i$} \\
    0 & \text{otherwise}
  \end{cases}
\end{equation}
and then \eqref{belief_update} becomes
\begin{equation}\label{belief_update_fixed}
  b_t^j = \frac{ p(o_t | S_t = j, a_t) b_{t-1}^j }{ p(o_t) }
\end{equation}
where $p(o_t)$ can be treated as a normalization factor.

% --------------------------------------------------------- %
\section{Observations}
% --------------------------------------------------------- %
The observation model is
\begin{equation}\label{app:ObservationModel}
  \begin{split}
    O_t^j &= \delta (S_t, j) d_{j,k} + Z_t^j \\
          &= \begin{cases}
                d_{j,k} + Z_t^j & \text{if $S_t = j$}\\
                Z_t^j           & \text{otherwise}
             \end{cases}
  \end{split}
\end{equation}
where $Z_t^j$ is zero mean, unit variance Gaussian random noise (i.e. white noise) and in the case of an exponential-decay vision system
\begin{equation}
  d_{j,k} = 3 \cdot e^{-dist(j,k)}
\end{equation}
where $dist(j,k)$ is the Euclidean distance between locations $j$ and $k$.

We assume that given an image, individual observations (i.e. each pixel)
\begin{equation}
  o_t = (o_t^1, \dotsc, o_t^{|\mathcal{S}|})
\end{equation}
are conditionally independent. Then we can write the probability of an observation as
\begin{subequations}
  \begin{align}
    p(o_t | S_t = i, A_t = k)
      &= \prod_j p(o_t^j | S_t = i, A_t = k) \\
      &= p(o_t^i | S_t = i, A_t = k) \prod_{j \neq i} p(o_t^j | S_t = i, A_t = k) \\
      \intertext{Given the observation model from equation \eqref{app:ObservationModel} we get}
      &= \gaussianexp{o_t^i - d_{i,k}} \prod_{j \neq i} \gaussianexp{o_t^j} \\
      &= \frac{1}{\sqrt{2\pi}} \frac{\gaussianexppart{o_t^i - d_{i,k}}}{\gaussianexppart{o_t^i}} \prod_j \gaussianexp{o_t^j} \\
      &= \frac{\gaussianexppart{o_t^i - d_{i,k}}}{\gaussianexppart{o_t^i}} Z \\
      &= \exp((o_t^i - \frac{d_{i,k}}{2}) d_{i,k}) K
  \end{align}
\end{subequations}
where $K$ is a constant. Ignoring the constant $K$ and terms not containing $o_t^i$ we can write
\begin{equation}
  \label{app:ProportionalObservationLikelihood}
  p(o_t | S_t = i, A_t = k) \propto \exp{(d_{i,k} o_t^i)}
\end{equation}

% --------------------------------------------------------- %
\section{Proportional Belief Update}
% --------------------------------------------------------- %
Combining \eqref{belief_update_fixed} and \eqref{app:ProportionalObservationLikelihood} yields the proportional belief update
\begin{equation}
  % b_{t+1}^i \propto \exp(\alpha_{i,k} d_{i,k}) b_t^i
  b_{t+1}^i \propto \exp{(d_{i,k} o_t^i)} b_t^i
\end{equation}


\subsection{Paths and loops}

\subsubsection{Paths}

We have the set \(X\). We define a mapping \([0,1]\rightarrow X\)

If a path exists between any two points, then the space is path-connected.

\subsubsection{Loops}

This is a path which ends on itself.

If \(f(0)=f(1)\) then it is a loop.


\section*{Appendix}

\subsubsection{Derivations of the Model for homogenous factors $\Lambda_k = \Lambda$}
\label{derivation}
Here, we provide a detailed study of the possible behaviors of the model, also around the critical point $\Lambda =1$.

Three regimes must be considered:
\begin{enumerate}
\item For $\Lambda < 1$, the distribution is given by
\be
P_{\Lambda <1}(S  \geq s) = (1-\beta) \left(1 - {s \over s_{\rm max}}\right)^c~, ~~~s_{\rm max} := {S_0 \Lambda \over 1-\Lambda}~, ~~c := {\rm{ln}{\beta}
\over \rm{ln}{\Lambda}} >0~.
\label{trjruyk5i}
\ee
This distribution can be approximated in its central part, away from the maximum possible reward $s_{\rm max}$,
by a Weibull distribution of the form

\be
{\rm Pr}_{\rm }({\rm S} \geq s) \sim e^{-(s/d)^{c}}~.
\label{trjeargquju}
\ee

For $\Lambda \to 1^-$,
we have $s_{\rm max} \to +\infty$ and, for $s \ll s_{\rm max}$,
expression (\ref{trjruyk5i}) simplifies into a simple exponential function
\be
P_{\Lambda \to 1^-}(S  \geq s) \sim e^{-|\ln(\beta)| s/S_0}~.
\label{jrtikik}
\ee

\item For $\Lambda = 1$, the distribution of rewards  is a simple exponential function since $S_{n} = n S_{0}$ is linear in the rank $n$ and the probability of reaching rank $n$ is the exponential
$P(n) = \beta^{n} (1-\beta)$. Actually, the expression
(\ref{jrtikik}) becomes asymptotical exact as
\be
P_{\Lambda =1}(S  \geq s) = (1-\beta) e^{-|\ln(\beta)| s/S_0}~.
\label{jrtikik}
\ee

\item For $\Lambda > 1$, the distribution of rewards is of the form,

\be
P_{\Lambda >1}(S  \geq s) = \frac{1}{{{(1+ \frac{s}{s*})}^{c}}}~,~~s^{*} := {S_0 \Lambda \over \Lambda -1}~, ~~c := {|\rm{ln}{\beta}|\over \rm{ln}{\Lambda}}~,
\label{jrtisdfjl}
\ee

which develops to a power law distribution of reward of the form
${\rm Pr}({\rm reward} \geq S) = C/S^{\mu}$ with $\mu = c$, when $\Lambda \rightarrow +\infty$.

%For $\Lambda \to 1^+$, the tail is still power law and the exponent $\mu$ grows without bound if the probability $\beta$ does not converge to $1$ at the same rate.  Denoting $\Lambda = 1-a$, $\beta = 1-\rho a$, with $a \to 0^+$ and $\rho$ constant, we have $\mu \to \rho$.
\end{enumerate}


\chapter{Miscellaneous problems}

%=====================================================================
% \section{Fun problems}
% \label{sec:funproblems}
\sectionWithAnswers{Fun problems}{sec:funproblems}

In this section we present a number of problems for the interested
reader to pursue.

\begin{Exercise}\label{ex:misc:riffle}
  A riffle shuffle of a deck of even-numbered cards splits the deck
  into two equal parts, and then alternates the cards. That is, if the
  cards were originally numbered $1$ \ldots $10$, they will end up $1,
  6, 2, 7, 3, 8, 4, 9, 5, 10$. Write a function {\tt riffle} that
  takes an even number of elements and returns the elements in the new
  order. The signature of your function should be:
\begin{code}
   riffle : {a b} (fin a) => [2*a]b -> [2*a]b;
\end{code}
You might find the following Cryptol primitive functions useful:
\begin{Verbatim}
   length : {n, a, b} (fin n, Literal n b) => [n]a -> b
   take : {front, back, a} (fin front) => [front + back]a -> [front]a
   drop : {front, back, a} (fin front) => [front + back]a -> [back]a
   join : {parts, each, a} (fin each) => [parts][each]a -> [parts * each]
\end{Verbatim}
Use the interpreter to pass arguments to these functions to
familiarize yourself with their operation first. What happens when you
call {\tt riffle} with a sequence that has an odd number of elements?
\end{Exercise}

\begin{Answer}\ansref{ex:misc:riffle}
\begin{code}
  riffle xs = join [ [x, y] | x <- fh | y <- sh ]
    where [fh, sh] = split xs
\end{code}
The Cryptol type-checker will {\em not} allow passing an odd number of
elements to {\tt riffle}. To see the error message, issue: {\tt riffle
  [1..5]}.
\end{Answer}

\begin{Exercise}\label{ex:misc:riffle2}
  Given a deck of 52 cards, 8 riffles returns the deck to its original
  position. Write a theorem stating this fact and prove it.
\end{Exercise}
\begin{Answer}\ansref{ex:misc:riffle2}
  Note that the actual contents of the input sequence is immaterial;
  we will just use 8-bit numbers.
\begin{code}
   riffle8 : [52][8] -> Bit;
   property riffle8 deck = decks @ 8 == deck
     where decks = [deck] # [ riffle d | d <- decks ]
\end{code}
\end{Answer}

\begin{Exercise}\label{ex:misc:sort}
  Define the merge-sort function in Cryptol, that works over arbitrary
  finite sequences of words. Prove its correctness. You might want to
  split the proof in two parts. First prove that the output is a
  permutation of the input, and then prove that the output is always
  in increasing order.
\end{Exercise}
\begin{Answer}\ansref{ex:misc:sort}
  A solution to the merge-sort problem can be found in the {\tt
    Examples} directory of the Cryptol distribution.
\end{Answer}

\begin{Exercise}\label{ex:misc:legato}
  Legato's challenge refers to the verification of an 8-bit
  multiplication algorithm encoded in Mostek assembly code. More
  information on Legato's challenge can be found at:
\begin{center}
 \url{http://www.cs.utexas.edu/~moore/acl2/workshop-2004/contrib/legato/Weakest-Preconditions-Report.pdf}
\end{center}
Write a Cryptol program to model Legato's multiplication algorithm
following the machine model. Prove that the multiplier is correct.
\end{Exercise}
\begin{Answer}\ansref{ex:misc:legato}
A solution can be found at:
\begin{center} \url{http://www.galois.com/blog/2009/07/08/legatosmultiplierincryptol}
\end{center}
\end{Answer}

\begin{Exercise}\label{ex:misc:skein}
  The Cryptol distribution comes with a variety of crypto algorithm
  implementations, including AES and the SHA-3 candidate Skein.  Study
  these definitions and experiment with them using the Cryptol
  interpreter.
\end{Exercise}


% \begin{savequote}[75mm]
% Nulla facilisi. In vel sem. Morbi id urna in diam dignissim feugiat. Proin molestie tortor eu velit. Aliquam erat volutpat. Nullam ultrices, diam tempus vulputate egestas, eros pede varius leo.
% \qauthor{Quoteauthor Lastname}
% \end{savequote}

\chapter{Statistics}
Statistics is a branch of mathematics dealing with the collection, organization, analysis, interpretation and presentation of data.
\href{https://en.wikipedia.org/wiki/Statistics}{wikipedia}

\section{Probability}
TODO: Probability (general + simple), CDF, Variance, Markov-Property, etc.
\subsection{$L_p$-Space for Random-Variables}
The $L_p$-Norm for Random-Variables $X$, where $\mathbb{E}|X|^p < \infty$, is defined through:
\begin{align*}
	||X||_p:=(\mathbb{E}[|X|^p])^{\frac{1}{p}}
\end{align*}
\href{http://www2.stat.duke.edu/courses/Fall18/sta711/lec/wk-05.pdf}{lecture}

\subsection{Jensens-Inequality for Random Variables}
If $\phi$ is a konvex function and $X$ a Random-Variable, then
\begin{align*}
	\phi(\mathbb{E}X) \leq \mathbb{E}\phi(X)
\end{align*}
\href{https://en.wikipedia.org/wiki/Jensen%27s_inequality}{wikipedia}

\subsection{Fisher-Information}
For the parametric family $\mathcal{P} \in \{ \mathcal{P}_\theta | \theta \in \Theta_L\}$...TODO\\
If we assume $p_\theta(x,y)=p_\theta(y|x)p(x)$, the Fisher-Information Matrix $\mathcal{I}(\theta)$ becomes:
\begin{align*}
	\mathcal{I}(\theta) = \mathbb{E}_{(X,Y)\sim {P}_\theta}[\nabla_{\theta}\log p_{\theta}(Y|X)\otimes\nabla_{\theta}\log p_{\theta}(Y|X)]\text{,}
\end{align*}
where $\otimes$ is the inner-product.\\
\href{https://arxiv.org/abs/1711.01530}{paper}

\section{Distributions}
In this section, $X$ denotes a Random Vairable and $f$ the density-function.\\
TODO: More common distributions
\subsection{Normal Distribution}
If $X \sim {\mathcal {N}}(\mu ,\sigma ^{2})$ for ${\displaystyle \mu \in \mathbb {R}}$ and $\sigma ^{2} > 0 \in \mathbb {R}$, then:
\begin{align*}
	f(x) &= {\displaystyle {\frac {1}{\sqrt {2\pi \sigma ^{2}}}}e^{-{\frac {(x-\mu )^{2}}{2\sigma ^{2}}}}}\\
	\mathbb{E}X &= \mu \\
	Var[X] &= \sigma^2
\end{align*}
\href{https://en.wikipedia.org/wiki/Normal_distribution}{wikipedia}

\subsection{Normal Distribution (Multivariate)}
If $X \sim {\mathcal {N}}(\mu ,\Sigma)$ for $\mu \in \mathbb {R}^k$ and $\Sigma \in \mathbb {R}^{k \times k}$ with $\Sigma$ being positve semi-definite, then:
\begin{align*}
	f(x) &= \operatorname {det} (2\pi {\boldsymbol {\Sigma }})^{-{\frac {1}{2}}}\,e^{-{\frac {1}{2}}(\mathbf {x} -{\boldsymbol {\mu }})'{\boldsymbol {\Sigma }}^{-1}(\mathbf {x} -{\boldsymbol {\mu }})}\\
	\mathbb{E}X &= \mu \\
	Var[X] &= \Sigma
\end{align*}
\href{https://en.wikipedia.org/wiki/Multivariate_normal_distribution}{wikipedia}

\subsection{Empirical Distribution}
For any observation $X'=(x'_1, \cdots, x'_n)$, the empirical distribution is defined as:
\begin{align*}
	f(x) &= \hat{f}(x) =\frac{1}{n}\sum_{i=1}^{n}\delta(x - x_i)\text{, where $\delta$ is the dirac-delta function}\\
	\mathbb{E}X &= \hat{\mathbb{E}}X = \frac{1}{n}\sum_{i=1}^{n}x_i \\
	Var[X] &= \hat{Var}[X] =\frac{1}{n}\sum_{i=1}^{n}(x_i-\hat{\mathbb{E}}X)^2
\end{align*}
\href{http://www.stat.umn.edu/geyer/5102/slides/s1.pdf}{lecture}

\section{Estimation}
TODO: ML, Score-Function, biased/unbiased, Cramér–Rao bound, confidence-interval

\section{Divergences}
Conventions for this section: $P$ and $Q$ are probability measures over a set $X$, and $P$ is absolutely continuous with respect to $Q$. $S$ is a space of all probability distributions with common support.
\subsection{Divergence}
A divergence on $S$ is a function $D: S \times S \rightarrow R$ satisfying
\begin{enumerate}
	\item $D(p || q) \geq 0  \forall p, q \in S$,
	\item $D(p || q) = 0 \Leftrightarrow p = q$
\end{enumerate}
\textit{A divergence is a "sense" of distance between two probability distributions. It's not a metric, but a pre-metric.}\\
\href{https://en.wikipedia.org/wiki/Divergence_(statistics)}{wikipedia}

\subsection{f-Divergence}
\begin{enumerate}
	\item Generalization of whole family of divergences
	\item For a convex function $f$ such that $f(1) = 0$, the f-divergence of $P$ from $Q$ is defined as:\\
	$D_{f}(P\parallel Q)\equiv \int _{{\Omega }}f\left({\frac{dP}{dQ}}\right)\,dQ$
	\item \href{https://en.wikipedia.org/wiki/Divergence_(statistics)}{wikipedia}
\end{enumerate}

\subsection{KL-Divergence}
\begin{enumerate}
	\item The Kullback–Leibler divergence from $Q$ to $P$ is defined as\\
	$D_{\mathrm {KL} }(P\|Q)=\int _{X}\log {\frac {dP}{dQ}}\,dP=D_{t\log t}$.
	\item maxmizing likelihood is equivalent to minimizing $D_{KL}(P(. \vert \theta^{\ast}) \, \Vert \, P(. \vert \theta))$ (the foreward-KL Divergence), where $P(. \vert \theta^{\ast})$ is the true distribution and $P(. \vert \theta)$ is our estimate.
	\item \href{https://en.wikipedia.org/wiki/Kullback–Leibler_divergence}{wikipedia}
	\item TODO: Fisher-Matrix infitesimal relationship
\end{enumerate}

\subsection{Jensen–Shannon divergence}
The Jensen–Shannon divergence from $Q$ to $P$ is defined as
\begin{align*}
	{{\rm {JSD}}}(P\parallel Q)={\frac  {1}{2}}D(P\parallel M)+{\frac  {1}{2}}D(Q\parallel M)
\end{align*}, where $M={\frac  {1}{2}}(P+Q)$\\
\href{https://en.wikipedia.org/wiki/Jensen–Shannon_divergence}{wikipedia}

\subsection{TODO: Wasserstein \& Wasserstein Dual}

\section{Information Geometry}
Information Geometry defines a Riemannian Manifold over probability distributions for statistical models.\\
\subsection{Fisher-Rao Metric}
For the parametric family $\mathcal{P} \in \{ \mathcal{P}_\theta | \theta \in \Theta_L\}$ and every $\alpha, \beta \in \mathbb{R}^d$ with their tangent-vectors $\bar{\alpha}=dp_{\theta + t\alpha}/dt|_{t=0}$ and $\bar{\beta}=dp_{\theta + t\beta}/dt|_{t=0}$, we define the inner local product as follows:
\begin{align*}
	<\bar{\alpha}, \bar{\beta}> &:= \int_M\frac{\bar{\alpha}}{p_\theta}\frac{\bar{\beta}}{p_\theta}p_\theta\\
	&=<\alpha, \mathcal{I}(\theta)\beta>\text{,}
\end{align*}
where $\mathcal{I}(\theta)$ is the Fisher-Information Matrix.
\subsection{Natural Gradient}
The natural gradient is the gradient descent induced by the Fisher-Rao geometry of $\{\mathcal{P}_\theta \}$.\\
\href{https://arxiv.org/abs/1711.01530}{paper}