\subsection{Decision Trees}
\label{decision_trees}

\noindent{\bf Description}
Decision trees (for classification) is a classifier that is considered
more interpretable than other statistical classifiers. This implementation
is well-suited to handle large-scale data and builds a (binary) decision
tree in parallel.\\

\noindent{\bf Usage}
\begin{tabbing}
\texttt{-f} \textit{path}/\texttt{decision-tree.dml -nvargs}
\=\texttt{X=}\textit{path}/\textit{file}
  \texttt{Y=}\textit{path}/\textit{file}
  \texttt{types=}\textit{path}/\textit{file}\\
\>\texttt{model=}\textit{path}/\textit{file}
  \texttt{bins=}\textit{int}
  \texttt{depth=}\textit{int}\\
\>\texttt{num\_leaf=}\textit{int}
  \texttt{num\_samples=}\textit{int}\\
\>\texttt{Log=}\textit{path}/\textit{file}
  \texttt{fmt=}\textit{csv}$\vert$\textit{text}
\end{tabbing}

\begin{tabbing}
\texttt{-f} \textit{path}/\texttt{decision-tree-predict.dml -nvargs}
\=\texttt{X=}\textit{path}/\textit{file}
  \texttt{Y=}\textit{path}/\textit{file}
  \texttt{model=}\textit{path}/\textit{file}\\
\>\texttt{fmt=}\textit{csv}$\vert$\textit{text}
  \texttt{accuracy=}\textit{path}/\textit{file}\\
\>\texttt{confusion=}\textit{path}/\textit{file}
  \texttt{predictions=}\textit{path}/\textit{file}
\end{tabbing}

\noindent{\bf Arguments}

\begin{itemize}
\item X: Location (on HDFS) to read the matrix of feature vectors;
each row constitutes one feature vector.
\item Y: Location (on HDFS) to read the one-column matrix of (categorical)
labels that correspond to feature vectors in X. Classes are assumed to be
contiguously labeled beginning from 1. Note that, this argument is optional
for prediction.
\item model: Location (on HDFS) that contains the learnt decision tree.
\item types: Location (on HDFS) that contains each feature's type. 1 denotes
continuous-valued (scale) and 2 denotes categorical.
\item bins (default: {\tt 50}): Number of thresholds to choose for each
continuous-valued feature (deterimined by equi-height binning).
\item depth (default: {\tt 10}): Maximum depth of the learnt decision tree.
\item num\_leaf (default: {\tt 1}): Parameter that controls pruning. The tree
is not expanded if a node receives less than num\_leaf training examples.
\item num\_samples (default: {\tt 10}): Parameter that decides when to switch
to in-memory building of subtrees. If a node $v$ receives less than num\_samples
training examples then this implementation switches to an in-memory subtree
building procedure to build the subtree under $v$ in its entirety.
\item Log: Location (on HDFS) that collects various useful metrics that indicate
training progress.
\item predictions: Location (on HDFS) to store predictions for a held-out test set.
Note that, this is an optional argument.
\item fmt (default: {\tt text}): Specifies the output format. Choice of
comma-separated values (csv) or as a sparse-matrix (text).
\item accuracy: Location (on HDFS) to store the testing accuracy from a
held-out test set during prediction. Note that, this is an optional argument.
\item confusion: Location (on HDFS) to store the confusion matrix
computed using a held-out test set. Note that, this is an optional
argument.
\end{itemize}

\noindent{\bf Details}

Decision trees (Breiman et al, 1984) are simple models of
classification that,  due to their structure,  are easy to
interpret. Given an example feature vector, each node in the learnt
tree runs a simple test on it. Based on the result of the test, the
example is either diverted to the left subtree or to the right
subtree. Once the example reaches a leaf, then the label stored at the
leaf is returned as the prediction for the example.

\par

Building a decision tree from a fully labeled training set entails
choosing appropriate tests for each internal node in the tree and this
is usually performed in a top-down manner. Choosing a test requires
first choosing a feature $j$ and depending on the type of $j$, either
a threshold $\sigma$, in case $j$ is continuous-valued, or a subset of
values $S \subseteq \text{Dom}(j)$ where $\text{Dom}(j)$ denotes
domain of $j$, in case it is categorical. For continuous-valued
features the test is thus of form $x_j < \sigma$ and for categorical
features it is of form $x_j \in S$, where $x_j$ denotes the $j^{th}$
feature value of feature vector $x$. One way to determine which test
to include, is to compare impurities of the subtrees induced by the
test and this implementation uses {\it Gini impurity}.

\par

The current implementation allows the user to specify the maximum
depth of the learnt tree using the argument {\it depth}. It also
allows for some automated pruning via the argument {\it num\_leaf}. If
a node receives $\leq$ {\it num\_leaf} training examples, then a leaf
is built in its place. Furthermore, for a continuous-valued feature
$j$ the number of candidate thresholds $\sigma$ to choose from is of
the order of the number of examples present in the training set. Since
for large-scale data this can result in a large number of candidate
thresholds, the user can limit this number via the arguments {\it
  bins} which controls the number of candidate thresholds considered
for each continuous-valued feature. For each continuous-valued
feature, the implementation computes an equi-height histogram to
generate one candidate threshold per equi-height bin. To determine the
best value subset to split on in the case of categorical features,
this implementation greedily includes values from the feature's domain
until the sum of the impurities of the subtrees induced stops
improving.

\par

Learning a decision tree on large-scale data has received  some
attention in the literature. The current implementation includes logic
for choosing tests for multiple nodes that belong to the same level in
the decision tree in parallel (breadth-first expansion) and for
building entire subtrees under multiple nodes in parallel (depth-first
subtree building). Empirically it has been demonstrated that it is
advantageous to perform breadth-first expansion for the nodes
belonging to the top levels of the tree and to perform depth-first
subtree building for nodes belonging to the lower levels of the tree
(Panda et al, 2009). The parameter {\it num\_samples} controls when we
switch to  depth-first subtree building. Any node in the decision tree
that receives $\leq$ {\it num\_samples} training examples, the subtree
under it is built in its entirety in one shot.

\par

{\it Description of the model}: The learnt decision tree is represented using a matrix that
contains at least 3 rows. Each column in the matrix contains the parameters relevant
to a single node in the tree. The $i^{th}$ column denotes the parameters for the $i^{th}$ node
whose left child is stored in the $2i^{th}$ column and right child is stored in the $2i+1^{th}$
column. Here is a brief description of what each row in the matrix contains:
\begin{itemize}
\item $1^{st}$ row: Contains the feature index of the feature that this node looks at if
the node is an internal node, otherwise -1.
\item $2^{nd}$ row: Contains the type of the feature that this node looks at if the node is
an internal node, otherwise the label this leaf node is supposed to predict.
1 denotes continuous-valued feature and 2 denotes categorical.
\item $3^{rd}$: Only applicable for internal nodes. Contains the threshold the example's
feature value is compared to if the feature chosen for this node is a continuous-valued feature.
If on the other hand, the feature chosen for this node is categorical then the size of the
subset of values is stored here.
\item $4^{th}$ row onwards: Only applicable in the case of internal nodes where the feature
chosen is a categorical feature. Rows $4, 5 \ldots$ depict the value subset
chosen for this node.
\end{itemize}
As an example, Figure \ref{dtree} shows a decision tree with $5$ nodes and its matrix
representation.

\begin{figure}
\begin{minipage}{0.3\linewidth}
\begin{center}
\begin{tikzpicture}
\node (labelleft) [draw,shape=circle,minimum size=16pt] at (0,0) {$2$};
\node (labelright) [draw,shape=circle,minimum size=16pt] at (1,0) {$1$};
\node (rootleft) [draw,shape=rectangle,minimum size=16pt] at (0.5,1) {$x_5 \in \{2,3\}$};
\node (rootlabel) [draw,shape=circle,minimum size=16pt] at (2.5,1) {$1$};
\node (root) [draw,shape=rectangle,minimum size=16pt] at (1.75,2) {$x_3 < 0.45$};

\draw[-latex] (root) -- (rootleft);
\draw[-latex] (root) -- (rootlabel);
\draw[-latex] (rootleft) -- (labelleft);
\draw[-latex] (rootleft) -- (labelright);

\end{tikzpicture}
\end{center}
\begin{center}
(a)
\end{center}
\end{minipage}
\hfill
\begin{minipage}{0.65\linewidth}
\begin{center}
\begin{tabular}{c|c|c|c|c|c|}
& Col 1 & Col 2 & Col 3 & Col 4 & Col 5\\
\hline
Row 1 & 3 & 5 & -1 & -1 & -1\\
\hline
Row 2 & 1 & 2 & 1 & 2 & 1\\
\hline
Row 3 & 0.45 & 2 &  &  & \\
\hline
Row 4 &  & 2 &  &  & \\
\hline
Row 5 &  & 3 &  &  & \\
\hline
\end{tabular}
\end{center}
\begin{center}
(b)
\end{center}
\end{minipage}
\caption{(a) An example tree and its (b) matrix representation. $x$ denotes an example and $x_j$ the $j^{th}$ feature's value in it.}
\label{dtree}
\end{figure}

\vspace{16pt}

\noindent{\bf Returns}

The matrix corresponding to the learnt model is written to a file in the format requested. See
details where the structure of the model matrix is
described. Depending on what arguments are provided during
invocation, decision-tree-predict.dml may compute one or more of
predictions,  accuracy and confusion matrix in the requested output format.
\\

\noindent{\bf Examples}
\begin{verbatim}
hadoop jar SystemML.jar -f decision-tree.dml -nvargs
                           X=/user/biadmin/X.mtx
                           Y=/user/biadmin/y.mtx
                           types=/user/biadmin/types.mtx
                           model=/user/biadmin/model.mtx
                           bins=50 depth=10 num_leaf=1
                           num_samples=250 fmt=csv
                           Log=/user/biadmin/accuracy.csv
\end{verbatim}

\begin{verbatim}
hadoop jar SystemML.jar -f decision-tree-predict.dml -nvargs
                           X=/user/biadmin/X.mtx
                           Y=/user/biadmin/y.mtx
                           model=/user/biadmin/model.mtx
                           fmt=csv
                           predictions=/user/biadmin/probabilities.csv
                           accuracy=/user/biadmin/accuracy.csv
                           confusion=/user/biadmin/confusion.csv
\end{verbatim}

\noindent{\bf References}

\begin{itemize}
\item B. Panda, J. Herbach, S. Basu, and R. Bayardo. \newblock{PLANET: massively parallel learning of tree ensembles with MapReduce}. In Proceedings of the VLDB Endowment, 2009.
\item L. Breiman, J. Friedman, R. Olshen, and C. Stone. \newblock{Classification and Regression Trees}. Wadsworth and Brooks, 1984.
\end{itemize}


\section{Let's Get the Atmosphere Moving}
In its current state, CLaUDE has a static planet. This means that the planet remains in place and does not move. However we know that planets move in orbit and more importantly, spin around
themselves. But before we start adding layers, let's talk about a term you will hear more often: numerical instability.

Numerical instability occurs when you first run the model. This is due to the nature of the equations. Nearly all equations are continuous, which means that they are always at work. However
when you start the model, the equations were not at work yet. It is as if you suddenly give a random meteor an atmosphere, place it in orbit around a star and don't touch i for a bit. You will
see that the whole system oscilates wildly as it adjusts to the sudden changes and eventually it will stabilise. Another term you might encounter is blow up, this occurs when when the model
suddenly no longer behaves like it should. This is most likely caused by mistakes in the code or incorrect paramter initialisation. Be wary of the existence of both factors, and do not dismiss
a model if it behaves weirdly as it has just started up.

\subsection{Equation of State and the Incompressible Atmosphere}
The equation of state relates one or more variables in a dynamical system (like the atmosphee) to another. The most common equation of state in the atmosphere is the ideal gas equation as
described by \autoref{eq:ideal gas} \cite{idealGas}. The symbols in that equation represent:

\begin{itemize}
    \item $p$: The gas pressure ($Pa$).
    \item $V$: The volume of the gas ($m^3$).
    \item $n$: The amount of moles\footnote{Mole is the amount of particles ($6.02214076 \cdot 10^{23}$) in a substance, where the average weight of one mole of particles in grams is about the
    same as the weight of one particle in atomic mass units ($u$)\cite{mole}} in the gas.
    \item $R$: The Gas constant, $8.3144621$ ($J(mol)^{-1}K$) \cite{idealGas}.
    \item $T$: The temperature opf the gas ($K$).
\end{itemize}

If we divide everything in \autoref{eq:ideal gas} by $V$ and set it to be unit (in this case, set it to be exactly $1 m^3$) we can add in the molar mass in both the top and bottom parts of the
division as show in \autoref{eq:gas unit}. We can then replace $\frac{nm}{V}$ by $\rho$ the density of the gas ($kgm^{-3}$) and $\frac{R}{m}$ by $R_s$ the specific gas constant (gas constant that varies per
gas in $J(mol)^{-1}K$) as shown in \autoref{eq:state gas}. the resulting equation is the equation of state that you get that most atmospheric physicists use when talking about the atmosphere \cite{simon}.

\begin{subequations}
    \begin{equation}
        pV = nRT
        \label{eq:ideal gas}
    \end{equation}
    \begin{equation}
        p = \frac{nR}{V}T = \frac{nmR}{Vm}T
        \label{eq:gas unit}
    \end{equation}
    \begin{equation}
        p = \rho R_sT
        \label{eq:state gas}
    \end{equation}
\end{subequations}

The pressure is quite important, as air moves from a high pressure point to a low pressure point. So if we know the density and the temperature, then we know the pressure and we can work out
where the air will be moving to (i.e. how the wind will blow). In our current model, we know the atmospheric temperature but we do not know the density. For simplicities sake, we will now assume
that the atmosphere is Incompressible, meaning that we have a constant density. Obviously we know that air can be compressed and hence our atmosphere can be compressed too but that is not
important enough to account for yet, especially considering the current complexity of our model.

The code that corresponds to this is quite simple, the only change that we need to make in \autoref{eq:state gas} is that we need to replace $T$ by $T_a$, the temperature of the atmosphere. As
$T_a$ is a matrix (known to programmers as a double array), $p$ will be a matrix as well. Now we only need to fill in some values. $\rho = 1.2$\cite{densityAir}, $R_s = 287$\cite{specificGasConstantAir}.

\subsection{The Primitive Equations and Geostrophy}
The primitive equations (also known as the momentum equations) is what makes the air move. It is actually kind of an injoke between physicists as they are called the primitive equations but
actually look quite complicated (and it says $fu$ at the end! \cite{simon}). The primitive equations are a set of equations dictating the direction in the $u$ and $v$ directions as shown in
\autoref{eq:primitive u} and \autoref{eq:primitive v}. We can make the equations simpler by using and approximation called geostrophy which means that we have no vertical motion, such that the
terms with $\omega$ in \autoref{eq:primitive u} and \autoref{eq:primitive v} become 0. We also assume that we are in a steady state, i.e. there is no acceleration which in turn means that the
whole middle part of the equations are $0$. Hence we are left with \autoref{eq:primitive u final} and \autoref{eq:primitive v final}.

\begin{subequations}
    \begin{equation}
        \frac{du}{dt} = \frac{\delta u}{\delta t} + u\frac{\delta u}{ \delta x} + v\frac{\delta u}{\delta v} + \omega\frac{\delta u}{\delta p} = -\frac{\delta \Phi}{\delta x} + fv
        \label{eq:primitive u}
    \end{equation}
    \begin{equation}
        \frac{dv}{dt} = \frac{\delta v}{\delta t} + u\frac{\delta v}{ \delta x} + v\frac{\delta v}{\delta v} + \omega\frac{\delta v}{\delta p} = -\frac{\delta \Phi}{\delta y} - fu
        \label{eq:primitive v}
    \end{equation}

    \begin{equation}
        0 = -\frac{\delta \Phi}{\delta x} + fv
        \label{eq:primitive u final}
    \end{equation}
    \begin{equation}
        0 = -\frac{\delta \Phi}{\delta y} - fu
        \label{eq:primitive v final}
    \end{equation}
\end{subequations}

\autoref{eq:primitive u final} can be split up into to parts, the $\frac{\delta \Phi}{\delta x}$ part (the gradient force) and the $fv$ part (the coriolis force). The same applies to
\autoref{eq:primitive v final}. Effectively we have a balance between the gradient and the coriolis force as shown in \autoref{eq:pu simple} and \autoref{eq:pv simple}. The symbols in both of
these equations are:

\begin{itemize}
    \item $\Phi$: The geopotential, potential (more explanation in \autoref{sec:potential}) of the planet's gravity field ($Jkg^{-1}$).
    \item $x$: The change in the East direction along the planet surface ($m$).
    \item $y$: The change in the North direction along the planet surface ($m$).
    \item $f$: The coriolis parameter as described by \autoref{eq:coriolis}, where $\Omega$ is the rotation rate of the planet (for Earth $7.2921 \cdot 10^{-5}$) ($rad \ s^{-1}$) and $\theta$ is the
    latitude \cite{coriolis}.
    \item $u$: The velocity in the latitude ($ms^{-1}$).
    \item $v$: The velocity in the longitude ($ms^{-1}$).
\end{itemize}

\begin{subequations}
    \begin{equation}
        f = 2\Omega\sin(\theta)
        \label{eq:coriolis}
    \end{equation}
    \begin{equation}
        \frac{\delta \Phi}{\delta x} = fv
        \label{eq:pu simple}
    \end{equation}
    \begin{equation}
        \frac{\delta \Phi}{\delta y} = -fu
        \label{eq:pv simple}
    \end{equation}
    \begin{equation}
        \frac{\delta p}{\rho \delta x} = fv
        \label{eq:pu simple final}
    \end{equation}
    \begin{equation}
        \frac{\delta p}{\rho \delta y} = -fu
        \label{eq:pv simple final}
    \end{equation}
\end{subequations}

Since we want to know how the atmosphere moves, we want to get the v and u components of the velocity vector (since $v$ and $u$ are the veolicites in longitude and latitude, if we combine them in a
vector we get the direction of the overall velocity). So it is time to start coding and calculating! If we look back at \autoref{alg:stream1v2}, we can see that we already have a double for loop.
In computer science, having multiple loops is generally considered a bad coding practice as you usually can just reuse the indices of the already existing loop, so you do not need to create a new
one. However this is a special case, since we are calculating new temperatures in the double for loop. If we then also would start to calculate the velocities then we would use new information
and old information at the same time. Since at index $i - 1$ the new temperature has already been calculated, but at the index $i + 1$ the old one is still there. So in order to fix that we need
a second double for loop to ensure that we always use the new temperatures. We display this specific loop in \autoref{alg:stream2}. Do note that everything in \autoref{alg:stream1v2} is still
defined and can still be used, but since we want to focus on the new code, we leave out the old code to keep it concise and to prevent clutter.

\begin{algorithm}[hbt]
    \SetAlgoLined

    \While{\texttt{TRUE}}{
        \For{$lat \in [-nlat, nlat]$}{
            \For{$lon \in [0, nlon]$}{
                $u[lat, lon] \leftarrow -\frac{p[lat + 1, lon] - p[lat - 1, lon]}{\delta y} \cdot \frac{1}{f[lat]\rho}$ \;
                $v[lat, lon] \leftarrow \frac{p[lat, lon + 1] - p[lat, lon - 1]}{\delta x[lat]} \cdot \frac{1}{f[lat]\rho}$ \;
            }
        }
    }
    \caption{The main loop of the velocity of the atmosphere calculations}
    \label{alg:stream2}
\end{algorithm}

The gradient calculation is done in \autoref{alg:gradient}. For this to work, we need the circumference of the planet. Herefore we need to assume that the planet is a sphere. While that is not
technically true, it makes little difference in practice and is good enough for our model. The equation for the circumference can be found in \autoref{eq:circumference} \cite{circumference},
where $r$ is the radius of the planet. Here we also use the f-plane approximation, where the coriolis paramter has one value for the northern hemisphere and one value for the southern hemisphere
\cite{fplane}.

\begin{equation}
    2 \pi r
    \label{eq:circumference}
\end{equation}

\begin{algorithm}
    \SetAlgoLined
    $C \leftarrow 2\pi R$ \;
    $\delta y \leftarrow \frac{C}{nlat}$ \;

    \For{$lat \in [-nlat, nlat]$}{
        $\delta x[lat] \leftarrow \delta y \cos(lat \cdot \frac{\pi}{180})$ \;

        \eIf{$lat < 0$}{
            $f[lat] \leftarrow -10^{-4}$ \;
        }{
            $f[lat] \leftarrow 10^{-4}$ \;
        }
    }
    \caption{Calculating the gradient $\delta x$}
    \label{alg:gradient}
\end{algorithm}

Because of the geometry of the planet and the construction of the longitude latitude grid, we run into some problems when calculating the gradient. Since the planet is not flat ("controversial
I know"\cite{simon}) whenever we reach the end of the longitude we need to loop around to get to the right spot to calculate the gradients (as the planet does not stop at the end of the
longitude line but loops around). So to fix that we use the modulus (mod) function which does the looping for us if we exceed the grid's boundaries. We do haveanother problem though, the poles.
As the latitude grows closer to the poles, they are converging on the center point of the pole. Looping around there is much more difficult so to fix it, we just do not consider that center
point in the main loop. The changed algorithm can be found in \autoref{alg:stream2v2}

\begin{algorithm}[hbt]
    \SetAlgoLined

    \While{\texttt{TRUE}}{
        \For{$lat \in [-nlat + 1, nlat - 1]$}{
            \For{$lon \in [0, nlon]$}{
                $u[lat, lon] \leftarrow -\frac{p[(lat + 1) \text{ mod } nlat, lon] - p[(lat -1) \text{ mod } nlat, lon]}{\delta y} \cdot \frac{1}{f[lat]\rho}$ \;
                $v[lat, lon] \leftarrow \frac{p[lat, (lon + 1) \text{ mod } nlon] - p[lat, (lon -1) \text{ mod } nlon]}{\delta x[lat]} \cdot \frac{1}{f[lat]\rho}$ \;
            }
        }
    }
    \caption{The main loop of the velocity of the atmosphere calculations}
    \label{alg:stream2v2}
\end{algorithm}

Do note that the pressure calculation is done between the temperature calculation in \autoref{alg:stream1v2} and the $u, v$ calculations in \autoref{alg:stream2v2}. At this point our model shows
a symmetric vortex around the sun that moves with the sun. This is not very realistic as you usually have convection and air flowing from warm to cold, but we do not have that complexity yet
(due to our single layer atmosphere).

\subsection{Introducing an Ocean}
Now we want to introduce an ocean, because most of the Earth is covered by oceans it plays quite an important role in atmospheric physics. To do this we need a new concept called albedo. Albedo
is basically the reflectiveness of a material (in our case the planet's surface) \cite{albedo}. The average albedo of the Earth is about 0.3. Now to add an ocean to the grid, we define a few
areas where the albedo differs. Where you do this does not really matter for the current complexity. Defining the oceans is as easy as hardcoding (what we computer scientists refer to when
setting parts of an array to be a specific value, where if you want to change the value you need to change it everywhere instead of doing it in a variable) the albedo value for the specific
regions as we do in \autoref{alg:albedo}. Water also takes longer to warm up, so let us change the specific heat capacity ($C_p$ in \autoref{alg:stream1v2}) from a constant to an array. The new
$C_p$ can also be found in \autoref{alg:albedo}, where we have made the specific heat capacity of water one order of magnitude (i.e. $10$ times) larger.

\begin{algorithm}[hbt]
    $a \leftarrow 0.5$ \;
    $a[5-55, 9-20] \leftarrow 0.2$ \;
    $a[23-50, 45-70] \leftarrow 0.2$ \;
    $a[2-30, 85-110] \leftarrow 0.2$ \;

    $C_p \leftarrow 10^7$ \;
    $C_p[5-55, 9-20] \leftarrow 10^8$ \;
    $C_p[23-50, 45-70] \leftarrow 10^8$ \;
    $C_p[2-30, 85-110] \leftarrow 10^8$ \;
    \caption{Defining the oceans}
    \label{alg:albedo}
\end{algorithm}

Now that we have that defined, we need to adjust the main loop of the program (\autoref{alg:stream1v2}). For clarity, all the defined constants have been left out. We need to add albedo into the
equation and change $C_p$ from a constant to an array. The algorithm after these changes can be found in \autoref{alg:stream2v3}. We multiply by $1 - a$ since albedo represents how much energy is
reflected instead of absorbed, where we need the amount that is absorbed which is exactly equal to $1$ minus the amount that is reflected.

\begin{algorithm}[hbt]
    \SetAlgoLined

    \While{\texttt{TRUE}}{
        \For{$lat \in [-nlat, nlat]$}{
            \For{$lon \in [0, nlot]$}{
                $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t ((1 - a[lat, lon])S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p[lat, lon]}$ \;
                $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
                $t \leftarrow t + \delta t$ \;
            }
        }
    }
    \caption{The main loop of the temperature calculations}
    \label{alg:stream2v3}
\end{algorithm}


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

\subsection{Stationary SSD emission}

\marginpar{Tuesday\\ 2020-11-17, \\ compiled \\ \today}

Let us start by stating some assumption and definitions:
%
\begin{enumerate}
    \item \(\rho = \Sigma / H\);
    \item \(H = c_s (R^3 / GM)^{1/2} = R c_s / v_\phi \);
    \item \(c_s^2 = P / \rho \);
    \item \(P = P _{\text{gas}} + P _{\text{rad}} = k_B \rho T / (\mu m_p) + a T^{4} / 3\).
\end{enumerate}

From the center of the disk radiation can travel outward, however it will be optically thick: \(\tau = \int_{0}^{r} \alpha \dd{s} >1\).
We can approximate it as \(\tau \approx \kappa _R \rho H = \kappa _R \Sigma > 1\), where \(\kappa _R\) is the Rosseland mean opacity.

Radiative transport in a slab can be treated analytically in the diffusion approximation to yield the radiative flux
%
\begin{align}
F(z) = \frac{16 \sigma T^3}{3 \kappa _R \rho } \pdv{T}{z} = - \frac{4}{3} \frac{\sigma}{\kappa _R \rho } \pdv{(T^{4})}{z}
\,.
\end{align}

We can define the surface as the height at which \(\tau =1\).

The flux crossing the \(z = 0\) surface is
%
\begin{align}
F(0) \approx \frac{4}{3} \frac{\sigma }{\kappa _R \rho _c} \frac{T_c^{4}}{H} \approx \frac{4}{3} \frac{\sigma T_c^{4}}{\tau _c}
\,.
\end{align}

% \todo[inline]{not zero?}

The flux at the surface, on the other hand, is
%
\begin{align}
F(s) \approx \frac{4}{3} \frac{\sigma}{\tau _s} T_s^{4} \approx \frac{4}{3} \sigma T_s^{4}
\,.
\end{align}

So, their ratio is
%
\begin{align}
\frac{F(s)}{F(0)} = \qty(\frac{T_s}{T_c})^{4} \tau _c
\,.
\end{align}

We expect \(T_s < T_c\), as is natural if flux is going from inside to outside. Then, \((T_s / T_c)^{4} \ll 1\), so unless \(\tau _c\) is extremely large (and, as we will see, it is not) we get \(F(s) < F(0)\).
The difference \(F(0) - F(s) \approx F(0)\) corresponds to the produced energy \(D(R)\), so
%
\begin{align}
\frac{4}{3} \sigma \frac{T_c^{4}}{\tau _c} \approx D(R) = \frac{3 GM \dot{M}}{8 \pi R^3} \qty[1 - \qty(\frac{R _{\text{in}}}{R})^{1/2}]
\,.
\end{align}

This will be another assumption for us.
Also, we will use the relations
%
\begin{align}
\tau &= \kappa _R \Sigma  \\
\nu \Sigma &= \frac{\dot{M}}{3 \pi } \qty[1 - \qty(\frac{R _{\text{in}}}{R})^{1/2}]  \\
v_R &= - \frac{3 \nu }{2R} \qty[1 - \qty(\frac{R _{\text{in}}}{R})^{1/2}]^{-1}  \\
\kappa _R &= \kappa _R (\rho , T, \dots)  \\
\nu &= \nu  (\rho , T, \dots) = \alpha H c_s
\,.
\end{align}

Since we want to see what the emission will look like, we seek an explicit expression for the surface temperature:
%
\begin{align}
\sigma T_s^{4} &= D(R) = \frac{3 GM \dot{M}}{8 \pi R^3} \qty[1 - \qty(\frac{R _{\text{in}}}{R})^{1/2}]  \\
T_s &= \qty(\frac{3 GM \dot{M}}{8 \pi \sigma })^{1/4} R^{-3/4} \qty[1 - \qty(\frac{R _{\text{in}}}{R})^{1/2}]^{1/4}  \\
T_s &\approx \qty(\frac{3 GM \dot{M}}{8 \pi \sigma R _{\text{in}}^3 })^{1/4} \qty( \frac{R _{\text{in}}}{R})^{3/4}
\marginnote{If  \(R \gg R _{\text{in}}\).}
\label{eq:temperature-accretion-disk}
\,.
\end{align}

Then, we can see that the temperature decreases as a function of \(R\).

Let us define a typical surface temperature \(T _s^{\text{typical}}\) as
%
\begin{align}
T _s^{\text{typical}}
=
\qty(\frac{3 GM \dot{M}}{8 \pi \sigma R _{\text{in}}^3 })^{1/4}
\approx \SI{e7}{K} \qty(\frac{\dot{M}}{\SI{e17}{g /s}})^{1/4}
\qty(\frac{M}{M_{\odot}})^{1/4} \qty(\frac{R _{\text{in}}}{\SI{e6}{cm}})^{-3/4}
\,,
\end{align}
%
which yields soft \(X\)-rays, at around \SI{1}{keV}.
Using the complete formula, we find that the temperature is in the form
%
\begin{align}
T^{4} \propto \frac{1}{x^{3/4}} \qty(1 - \frac{1}{x^{1/2}})
\,,
\end{align}
%
where \(x = R / R _{\text{in}}\).
We can maximize this, and we find that the maximum temperature of the disk is found to be \(T _{\text{max}} \approx \num{.5} T^{\text{typical}}_s\).

There are several radiative processes taking place in an accretion disk:
\begin{enumerate}
    \item electron scattering;
    \item thermal free-free emission (bremsstrahlung).
\end{enumerate}

The latter is dominant, and the characteristic Rosseland mean opacity is
%
\begin{align}
\kappa _R^{\text{bremss}} \approx \num{6.6e22} \rho T^{-7/2} \SI{}{cm^2 g^{-1}}
\,.
\end{align}

For convenience, let us define
%
\begin{align}
f = 1 - \sqrt{ \frac{R _{\text{in}}}{R}}
\,.
\end{align}

We find an algebraic system with all the equations, and finally we get
%
\begin{align}
H = \SI{1.7e8}{cm} \times \alpha^{-1/10} (\dot{M}_{16})^{3/20} M^{-3/8} R_{10}^{9/8} f^{3/5}
\,,
\end{align}
%
from which we can confirm that \(R \gg H\).
Here, if a quantity has a subscript such as \(\dot{M}_{16}\), it means that we are normalizing the number to \SI{e16}{g/s}, or the appropriate cgs unit in general.
We can also calculate the central optical depth:
%
\begin{align}
\tau _c = 33 \alpha^{-4/5} \dot{M}_{16}^{1/5} f^{4/5}
\,.
\end{align}

Also, we find that \(T_s = T_c / 2\).

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Carlos Santos             %
% ECE 351-51                %
% Lab 10                    %
% 04/14/2020                %
%                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12pt]{article}

% Language and font encoding
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{rotating}
\usepackage{setspace}

% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

% Useful packages
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{listings}
\usepackage{gensymb}
\usepackage{graphicx} %package to manage images
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Python,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

%Line Spacing
\setstretch{1.5}

%Info for Title Page
\title{ECE 351 Lab 10 Report \\ Section 52}
\date{April 14, 2020}
\author{Carlos Santos}
\begin{document}

%Make a Title Page
\vspace{\fill}
\maketitle
\vspace{\fill}
\clearpage

\maketitle
\tableofcontents

%Introduction
\section{Introduction}
This lab's purpose was focused on frequency tools and bode plots using Python.
%Equations
\section{Equations}

Transfer Function:
\begin{equation}
    H(s) = \frac{\frac{1}{RC}s}{s^2+\frac{1}{RC}s+\frac{1}{LC}}
\end{equation}

Magnitude:
\begin{equation}
    H|j\omega| = \frac{\frac{1}{RC}\omega}{\sqrt{\omega ^ 4 + [\frac{1}{RC}^2 - \frac{2}{LC}]\omega ^ 2}+\frac{1}{LC}^2}
\end{equation}

Phase:
\begin{equation}
    <H(J\omega) = \frac{\pi}{2} - tan^{-1}(\frac{\frac{1}{RC}\omega}{-\omega ^ 2 + \frac{1}{LC}})
\end{equation}

\begin{equation}
    x(t) = cos(2\pi*100t) + cos(2\pi*3024t) + sin(2\pi * 50000t)
\end{equation}

\begin{figure}[H]
\caption{RLC Circuit}
\centering
\includegraphics[width=.8\textwidth]{RLC_Circuit.png}
\end{figure}

%Methodology
\section{Methodology}
\begin{enumerate}
    \item First find the magnitude and phase equations for the transfer function equation.
    \item Turn the magnitude and phase equations into Python equations.
    \item Plot the magnitude and phase from $10^3 rad/s \leq \omega \leq 10^6 rad/s$
    \item Use scipy.signal.bode to plot the magnitude and phase on a Bode plot.
    \item Plot equation 4 from $0\leq t \leq 0.01s$
    \item Convert equation 4 into the Z-domian using scipy.signal.bilinear().
    \item Pass equation 4 through the filer using scipy.signal.lfilter().
    \item Lasly. plot the output signal y(t)
\end{enumerate}

%Results
\section{Results}

\begin{figure}[H]
\caption{Task 3.3.1}
\centering
\includegraphics[width=.8\textwidth]{hand_solved.png}
\end{figure}

\begin{figure}[H]
\caption{Task 3.3.2}
\centering
\includegraphics[width=.8\textwidth]{sig_bode.png}
\end{figure}

\begin{figure}[H]
\caption{Task 3.3.3}
\centering
\includegraphics[width=.8\textwidth]{log_plot.png}
\end{figure}

\begin{figure}[H]
\caption{Task 4.3.4}
\centering
\includegraphics[width=.8\textwidth]{signal_filtering.png}
\end{figure}

%Questions
\section{Questions}
\begin{enumerate}
    \item Explain how the filter and filtered output in Part 2 makes sense given the Bode plots from
Part 1. Discuss how the filter modifies specific frequency bands, in Hz.
    \begin{enumerate}
        \item By looking at the Bode plot it seems like our filter is a center-band filter. This means it keeps all the frequencies near the middle and discards the rest. This makes sense by looking at the filtered signal plot versus the unfiltered signal plot. It kept all the signals between -1 and 1. Any high frequency signals are going to be clipped.
    \end{enumerate}

    \item Discuss the purpose and workings of
scipy.signal.bilinear() and scipy.signal.lfilter().
    \begin{enumerate}
        \item The "bilinear()" function converts our equation over to the Z-domain. It returns two items which are the numerator and denominator. The "lfilter()" passes the signal through the defined filter and provides the output signal which is filtered.
    \end{enumerate}
    \item What happens if you use a different sampling frequency in scipy.signal.bilinear() than
you used for the time-domain signal?
    \begin{enumerate}
        \item If sampling frequency is smaller we will get less points on the graph. Likewise the higher the sampling frequency the more points on the graph.
    \end{enumerate}
    \item Leave any feedback on the clarity of lab tasks, expectations, and deliverables.
\end{enumerate}
    \begin{enumerate}
        \item N/A
    \end{enumerate}

%Conclusion
\section{Conclusion}
In conclusion Bode plots helps us visualize the characteristics of a filter. Generating those plots in Python proved to be a simple task given the proper libraries and functions. For Bode plots we need sig.bode(), con.TransferFunction(), and con.bode().

\end{document}


\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{tcolorbox}

\title{\vspace{-5.0cm}DD2421 Machine Learning - Lab 1: Decision Trees \\ Python version}
\author{\"Orjan Ekeberg\\ Updated 2017 by Martin Hjelm \& Nils Bore }

\begin{document}
\maketitle

\section{Preparations}

In this lab you will use a set of predefined Python functions to
build and manipulate decision trees.  In order to run this, you
need to have Python installed.  We also use the Qt graphics library,
PyQt, for plotting. PyQt comes in different versions 4 or 5. For
Mac users version 5 is recommended.

For different Windows versions consult the Internet. For Mac use Homebrew
to install Python and PyQt5. For Ubuntu Python and PyQt are available in the
debian package repository. Python and PyQt are also installed on the Unix computers in
the computer halls.

\textbf{Note:} It is possible to do the lab without using the
plotting functions found in PyQt, but then you will not be able to see the generated
decision trees.

\section{MONK datasets}

This lab uses the artificial MONK dataset from the UC Irvine repository.
The MONK's problems are a collection of three binary classification
problems MONK-1, MONK-2 and MONK-3 over a six-attribute discrete domain.
The attributes \(a_1, a_2, a_3, a_4, a_5, a_6\) may take the following values:

\begin{center}
  \begin{tabular}{lll}
    \(a_1 \in \{1, 2, 3\}\) &
    \(a_2 \in \{1, 2, 3\}\) &
    \(a_3 \in \{1, 2\}\)\\
    \(a_4 \in \{1, 2, 3\}\) &
    \(a_5 \in \{1, 2, 3, 4\}\) &
    \(a_6 \in \{1, 2\}\)\\
  \end{tabular}
\end{center}
Consequently, there are 432 possible combinations of attribute values.
The \emph{true} concepts underlying each MONK's problem are given by
table \ref{tab:truemonk}.

\begin{tcolorbox}
\textbf{Assignment 0:}
Each one of the datasets has properties which makes them hard to learn.
Motivate which of the three problems is most difficult for a decision
tree algorithm to learn.
\end{tcolorbox}

\begin{table}
  \caption{True concepts behind the MONK datasets \label{tab:truemonk}}
  \begin{center}
    \begin{tabular}{|l|l|}
      \hline
      MONK-1 & \((a_1=a_2)\vee(a_5=1)\)\\
      % (attribute\_1 = attribute\_2) or (attribute\_5 = 1)
      \hline
      MONK-2 & \(a_i=1\) for exacly two \(i \in \{1, 2, \ldots, 6\}\)\\
      % (attribute\_n = 1) for EXACTLY TWO choices of n $\in \{1,2,...,6\}$
      \hline
      MONK-3 & \((a_5=1 \wedge a_4=1) \vee (a_5\ne 4 \wedge a_2\ne 3)\)\\
      %(attribute\_5  = 3 and attribute\_4  = 1) or\\
      %(attribute\_5 != 4 and attribute\_2 != 3)\\
      \hline
    \end{tabular}
  \end{center}
  MONK-3 has 5\% additional noise (misclassification) in the training set.
\end{table}

\begin{table}
  \caption{Characteristics of the three MONK datasets \label{tab:monk}}
  \begin{center}
    \begin{tabular}{|c|c|c|c|c|}\hline
      Name & \# train & \# test & \# attributes & \# classes\\ \hline \hline
      MONK-1 & 124 & 432 & 6 & 2\\ \hline
      MONK-2 & 169 & 432 & 6 & 2\\ \hline
      MONK-3 & 122 & 432 & 6 & 2\\ \hline
    \end{tabular}
  \end{center}
\end{table}

The data consists of three separate datasets MONK-1, MONK-2 and
MONK-3.  Each dataset is further divided into a training and test set,
where the first one is used for learning the decision tree, and the
second one to evaluate its classification accuracy (see table
\ref{tab:monk}).  The datasets are available in
the file \verb!monkdata.py!.  In particular, six variables are defined
which contain the datasets:
\verb!monk1!, \verb!monk1test!, \verb!monk2!,
\verb!monk2test!, \verb!monk3! and \verb!monk3test!.
Each dataset is a sequence (more precisely, a \emph{tuple}) of instances
of the class \texttt{Sample}, defined in the same file.

You can access the data in your own Python scripts by importing
the \texttt{monkdata.py} file as a module like this:
\begin{verbatim}
import monkdata as m
\end{verbatim}

This makes the variable \texttt{m} a shorthand for the module so that
you can access the datasets by writing \verb!m.monk1!, etc.

\section{Entropy}

In order to decide on which attribute to split, decision tree learning
algorithms such as ID3 and C4.5 use a statistical property called
\emph{information gain}.  It measures how well a particular attribute
distinguishes among different target classifications.  Information
gain is measured in terms of the expected reduction in the
\emph{entropy} or impurity of the data.  The entropy of an arbitrary
collection of examples is measured by
\begin{equation}
\textrm{Entropy}(S) = - \sum_i p_i \log_2 p_i
\label{eq:entropy}
\end{equation}
in which $p_i$ denotes the proportion of examples of class $i$ in $S$.
The monk dataset is a binary classification problem (class 0 or 1) and
therefore equation (\ref{eq:entropy}) simplifies to
\begin{equation}
\textrm{Entropy}(S) = - p_0 \log_2 p_0 - p_1 \log_2 p_1
\end{equation}
where $p_0$ and $p_1=1-p_0$ are the proportions of examples belonging to class
$0$ and $1$.\\[2ex]

\begin{tcolorbox}
\textbf{Assignment 1:} The file \verb!dtree.py! defines a function
\texttt{entropy} which calculates the entropy of a dataset.  Import
this file along with the monks datasets and use it to calculate the
entropy of the \emph{training} datasets.
\end{tcolorbox}

\begin{center}
  \begin{tabular*}{0.9\textwidth}{|c|c@{\extracolsep{\fill}}c|}
    \hline
    Dataset & Entropy & \\
    \hline\hline
    MONK-1 & & \\
    \hline
    MONK-2 & & \\
    \hline
    MONK-3 & & \\
    \hline
  \end{tabular*}
\end{center}

\begin{tcolorbox}
\textbf{Assignment 2:}
Explain entropy for a uniform distribution
%and a Gaussian distribution with high and low variance.
and a non-uniform distribution, present some example distributions with high and low entropy.
\end{tcolorbox}

\section{Information Gain}

The information gain measures the expected reduction in impurity
caused by partitioning the examples according to an attribute.
It thereby indicates the effectiveness of an attribute in classifying the
training data. The information gain of an attribute $A$, relative to
a collection of examples $S$ is defined as
\begin{equation}
\textrm{Gain}(S,A) = \textrm{Entropy}(S) -
 \sum_{k \in \textrm{values}(A)} \frac{|S_k|}{|S|} \textrm{Entropy}(S_k)
\end{equation}
where $S_k$ is the subset of examples in $S$ for the attribute $A$ has the value $k$.

\begin{tcolorbox}
\textbf{Assignment 3:} Use the function \texttt{averageGain} (defined
in \verb!dtree.py!)  to calculate the expected information gain
corresponding to each of the six attributes.  Note that the attributes
are represented as instances of the class Attribute (defined in
\verb!monkdata.py!) which you can access via \verb!m.attributes[0]!,
..., \verb!m.attributes[5]!. Based on the results, which attribute
should be used for splitting the examples at the root node?
\end{tcolorbox}

\begin{center}
  Information Gain\\[0.5ex]
  \begin{tabular*}{\textwidth}{|c@{\extracolsep{\fill}}|c|c|c|c|c|c|}
    \hline
    Dataset & $a_1$ & $a_2$ & $a_3$ & $a_4$ & $a_5$ & $a_6$ \\
    \hline
    \verb!MONK-1 ! & & & & & & \\
    \hline
    \verb!MONK-2 ! & & & & & & \\
    \hline
    \verb!MONK-3 ! & & & & & & \\
    \hline
  \end{tabular*}
\end{center}

\begin{tcolorbox}
\textbf{Assignment 4:}
For splitting we choose the attribute that maximizes the information gain, Eq.3.
Looking at Eq.3 how does the entropy of the subsets, $S_k$, look like when the
information gain is maximized? How can we motivate using the information gain
as a heuristic for picking an attribute for splitting? Think about reduction
in entropy after the split and what the entropy implies.
\end{tcolorbox}

\section{Building Decision Trees}

Split the \texttt{monk1} data into subsets according to the selected
attribute using the function \texttt{select} (again, defined in
\verb!dtree.py!)  and compute the information gains for the nodes on
the next level of the tree.  Which attributes should be tested for
these nodes?

For the \texttt{monk1} data draw the decision tree up to the first two
levels and assign the majority class of the subsets that resulted from
the two splits to the leaf nodes.  You can use the predefined function
\texttt{mostCommon} (in \verb!dtree.py!) to obtain the majority class
for a dataset.

Now compare your results with that of a predefined routine for ID3.
Use the function \verb!buildTree(data, m.attributes)! to build the
decision tree.  If you pass a third, optional, parameter to
\texttt{buildTree}, you can limit the depth of the generated tree.

You can use \texttt{print} to print the resulting tree in text form,
or use the function \texttt{drawTree} from the file \verb!drawtree_qt4.py!
or \verb!drawtree_qt5.py!, depending on your PyQt version, to draw a graphical
representation.

\begin{tcolorbox}
\textbf{Assignment 5:}
Build the full decision trees for all three Monk datasets using
\texttt{buildTree}.  Then, use the function \texttt{check} to measure the performance
of the decision tree on both the training and test datasets.

For example to built a tree for \texttt{monk1} and compute the performance on the test data
you could use
\begin{verbatim}
import monkdata as m
import dtree as d

t=d.buildTree(m.monk1, m.attributes);
print(d.check(t, m.monk1test))
\end{verbatim}

Compute the train and test set errors for the three Monk datasets for
the full trees. Were your assumptions about the datasets correct? Explain the
results you get for the training and test datasets.
\end{tcolorbox}

\begin{center}
  \begin{tabular*}{0.7\textwidth}{|c|@{\extracolsep{\fill}}c|c|}
    \hline
    & $E_\textrm{train}$ & $E_\textrm{test}$ \\
    \hline\hline
    \verb#MONK-1# & & \\
    \hline
    \verb#MONK-2# & & \\
    \hline
    \verb#MONK-3# & & \\
    \hline
  \end{tabular*}
\end{center}

\section{Pruning}
The idea of \emph{reduced error pruning} is to consider each node in
the tree as a candidate for removal.  A node is removed if the
resulting pruned tree performs at least as well as the original tree
over a separate \emph{validation dataset}, i.e. a dataset not used
during training.  When a node is removed, the subtree rooted at that
node is replaced by a leaf node, to which the majority classification
of examples in that node is assigned.

For the purpose of pruning, we have to split our original training
data into one training set for building the tree and one validation
set for pruning.  Notice, that using the test set for validation would
be cheating because we would then no longer be able to use the test
set for independently estimating the true error of our pruned decision
tree.  Instead, we will randomly partition the original training set into
training and validation set.  This can be done by defining a function
which randomly reorders the data samples and returns the first and second
parts separately:
\begin{verbatim}
import random

def partition(data, fraction):
    ldata = list(data)
    random.shuffle(ldata)
    breakPoint = int(len(ldata) * fraction)
    return ldata[:breakPoint], ldata[breakPoint:]

monk1train, monk1val = partition(m.monk1, 0.6)
\end{verbatim}

In the file \verb!dtree.py! there is a utility function \texttt{allPruned}
which returns a sequence of all possible ways a given tree can be pruned.

Write code which performs the complete pruning by repeatedly calling
\texttt{allPruned} and picking the tree which gives the best
classification performance on the validation dataset.  You should stop
pruning when all the pruned trees perform worse than the current
candidate.

\begin{tcolorbox}
\textbf{Assignment 6:}
Explain pruning from a bias variance trade-off perspective.
\end{tcolorbox}

\begin{tcolorbox}
\textbf{Assignment 7:} Evaluate the effect pruning has on the test
error for the \texttt{monk1} and \texttt{monk3} datasets, in
particular determine the optimal partition into training and pruning
by optimizing the parameter \texttt{fraction}.  Plot the
classification error on the test sets as a function of the parameter
\texttt{fraction} $\in \{0.3,0.4,0.5,0.6,0.7,0.8\}$. \\

Note that the split of the data is random. We therefore need to compute
the statistics over several runs of the split to be able to draw any
conclusions. Reasonable statistics includes mean and a measure of the spread.
Do remember to print axes labels, legends and data points as you will not pass without them.
\end{tcolorbox}

\end{document}


\documentclass[12pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amsmath, amssymb, amsthm}
\usepackage{wasysym}
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{mathtools}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\graphicspath{ {./} }
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\prob}{\mathbb{P}}
\newcommand{\degrees}{^{\circ}}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\author{Tianshuang (Ethan) Qiu}
\begin{document}
\title{Math 74, Week 14}
\maketitle

\section{Mon Lec, 3a}
Let $a$ be the length of this rectangle that is opposite the wall, and $b$ be the length of the other side. So we have $a+2b \leq 36$, and we try to maximize $ab$. We can simplify the first equation to $a = 36 - 2b$
\newline
By AM-GM, we have that $\frac{2a+b}{2} \geq \sqrt{2ab}$, now since we know that $a+2b \leq 36$, we can subsitute that in.
$$18 \geq \frac{2a+b}{2} \geq \sqrt{2ab}$$
Thus the maximum the area $ab$ can be is $18^2/2 = 162$. Now we try to find an $a,b$ such that $ab = 162$.
Let $a = 9, b = 18$, and $ab=162$, achieving the maximum.

\section{Mon Lec, 5a}
We manipulate $2 \sqrt x > 3 - \frac{1}{x}$, and it is equivalent to showing
$$2 \sqrt{x} + \frac{1}{x} \geq 3$$
Then apply AM-GM to see that $\frac{\sqrt{x}+\sqrt{x}+1/x}{3} \geq
\sqrt[3]{\sqrt{x}\sqrt{x}1/x} = 1$. Rearranging this gives
$$2 \sqrt{x} + \frac{1}{x} \geq 3$$
Thus we have proven the statement.

\section{Mon Lec, 6}
We say two inequalities are equivalent when they are true and false at the same time.
\newline
In the first equation $(x-a)^2+1>0$, $(x-a)^2$ is non-negative, so the statement is always true.
$$4ax^2 + 4x + 1 > 0$$
$$(4a-4)x^2 + (2x+1)^2 > 0$$
This inequality holds true when $(4a-4)>0$, so the two inequalities are equivalent when $a > 1$
\newpage

\section{Mon Dis, 1a}
By AM-GM, $\frac{a+b}{2} \geq \sqrt{ab}$, $\frac{b+c}{2} \geq \sqrt{bc}$, etc.
\newline
We can multiply these equations to get
$$\frac{(a+b)(b+c)...(e+a)}{2^5} \geq \sqrt{a^2b^2c^2d^2e^2}$$
$$(a+b)(b+c)(c+d)(d+e)(e+a) \geq 32abcde$$

\section{Mon Dis, 1b}
By AM-CM, $(\sum_{n=1}^{2021} n) /2021 \geq \sqrt[2021]{2021!}$. We can apply the arithmetic series sum to the lhs:
$$\frac{(2021+1)2021}{2 \times 2021} \geq \sqrt[2021]{2021!}$$
$$(\frac{2022}{2}) ^ {2021} \geq 2021!$$
Thus it is proven.

\section{Mon Dis, 3c}
Let our box intersect the ellipsoid in octant 1 at $(x,y,z)$. Since it is on the ellipsoid we have $\frac{x^2}{a^2}+\frac{y^2}{b^2}+\frac{z^2}{c^2}=1$.
\newline
By AM-GM inequality we have $$(\frac{x^2}{a^2}+\frac{y^2}{b^2}+\frac{z^2}{c^2})/3 \geq \sqrt[3]{\frac{x^2}{a^2}\frac{y^2}{b^2}\frac{z^2}{c^2}}$$
$$\frac{1}{3} \geq \sqrt[3]{\frac{x^2y^2z^2}{a^2b^2c^2}}$$
$$\frac{1}{27} \geq \frac{x^2y^2z^2}{a^2b^2c^2}$$
Now the volume of our cube is simply $8xyz$ since each side is double the intersection point in octant 1.
$$\frac{1}{27a^2b^2c^2} \geq x^2y^2z^2$$
$$xyz \leq abc\sqrt{\frac{1}{27}}$$
$$8xyz \leq 8abc\sqrt{\frac{1}{27}}$$
Thus we have found the maximum value.
\end{document}


% Sample LaTeX file for creating a paper in the Morgan Kaufmannn two
% column, 8 1/2 by 11 inch proceedings format.

\documentclass[]{article}
\usepackage{proceed2e}
\usepackage{hyperref}
\usepackage{url}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{natbib}
\usepackage{multirow}
\usepackage{fancyhdr}
\usepackage{latexsym,amsbsy,amssymb,color,xspace,booktabs}

\include{macros}
\include{local_macros}

\title{Collaborative Multi-output Gaussian Processes}

\author{} % LEAVE BLANK FOR ORIGINAL SUBMISSION.
          % UAI  reviewing is double-blind.

% The author names and affiliations should appear only in the accepted paper.
%
\author{ {\bf Trung V. Nguyen} \\
ANU \& NICTA \\
Canberra, Australia\\
\And
{\bf Edwin V. Bonilla}  \\
NICTA \& ANU          \\
Sydney, Australia \\
}

\begin{document}

\maketitle

\begin{abstract}
\input{abstract}
\end{abstract}

\section{INTRODUCTION}
\input{intro}

\section{MODEL SPECIFICATION \label{sec:model}}
\input{model}

\section{INFERENCE \label{sec:inference}}
\input{inference}

\section{EXPERIMENTS \label{sec:experiments}}
\input{experiments}

\section{DISCUSSION \label{sec:discussion}}
\input{discussion}

\subsubsection*{Acknowledgements}
\input{acks}

\bibliographystyle{apalike}
\bibliography{references}

\end{document}


\documentclass[]{article}
\usepackage[left=3cm, right3cm, top=2cm, bottom=3cm]{geometry}

\usepackage[table]{xcolor}
\usepackage{rotating}

%opening
\author{Matt B\ae{}kgaard Pedersen}
\title{The ProcessJ Type System}

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

\section{ProcessJ Types}

\subsection{Primitive Types}
ProcessJ has 11 primitive types shown in Table~\ref{tab:primitiveTypes}.
\begin{table}[!h]
  \begin{center}
    \caption{ProcessJ primitive types}
    \label{tab:primitiveTypes}
    \begin{tabular}{|l|l|}\hline
      Type Name & Values \\ \hline\hline
      byte & \\
      short & \\
      char & \\
      int & \\
      long & \\
      float & \\
      double & \\
      bool & \{true, false\}\\
      string & \\
      barrier & \\
      timer & \\ \hline
    \end{tabular}
  \end{center}
\end{table}

\subsection{Constructed Types}
If we count mobile procedures, ProcessJ has 6 constructed types; they are shown in Table~\ref{tab:constructedTypes}.
\begin{table}[!h]
  \begin{center}
    \caption{ProcessJ constructed types}
    \label{tab:constructedTypes}
    \begin{tabular}{|l|l|}\hline
      Type Name & Representation \\ \hline\hline
      Array    & {\it Array}($\alpha$, {\it index})\\
      Record   & {\it Record}({\it name},\{($n_1,t_1$),$\ldots$,($n_m,t_m$)\})\\
      Protocol & {\it Protocol}({\it name},\{({\it tag}$_1$,\{($n_{1,1},t_{1,1}$),$\ldots$,($n_{1,m_1},t_{1,m_1}$)\})\\
               & \hspace*{2.6cm}$\vdots$\\
      & \hspace*{2.475cm}({\it tag}$_k$,\{($n_{k,1},t_{k,1}$),$\ldots$,($n_{k,m_k},t_{1,m_k}$)\})\}\\
      Channel  & {\it Channel}($\alpha$,{\it access})\\
      Channel end & {\it ChannelEnd}($\alpha$, {\it end}), $\alpha$ is a Channel.\\
      Procedure & {\it Procedure}({\it name},($t_1,t_2,\ldots,t_n$),$t$)\\\hline
    \end{tabular}
  \end{center}
\end{table}

\newcommand{\teq}{=_{\cal T}}
\newcommand{\tev}{\sim_{\cal T}}
\newcommand{\tac}{:=_{\cal T}}
\newcommand{\tlt}{<_{\cal T}}
\newcommand{\tle}{\leq_{\cal T}}

\section{Helper Functions}

\subsection{Ceiling}

\[
\lceil \alpha, \beta \rceil := \left\{
\begin{array}{ll}
\alpha & \beta \tle \alpha\\
\beta  & \alpha \tlt \beta\\
\bot   & \mbox{otherwise}
\end{array}\right.
\]

\subsection{$\tlt$}

\subsubsection{Primitive Types}

A primitive type $\alpha$ is ``type-wise less than'' another primitive type $\beta$, if
a variable of type $\beta$ can hold any value of type $\alpha$. This definition seems to be exactly that of assignment compatible -- and it is, but it has to be defined somewhere, so here we go:

\[
byte \tlt short \tlt char \tlt int \tlt long
\]
as well as
\[
float \tlt double
\]
but also
\[
int \tlt float \wedge long \tlt double
\]
$\tlt$ is, of course, transitive, so if $(\alpha \tlt \beta) \wedge (\beta \tlt \delta) \Rightarrow \alpha \tlt \delta$.

\begin{center}
\begin{tabular}{|l||l|l|l|l|l|l|l|l|l|l|l|}\hline
\begin{turn}{45}$\,\,\alpha \tlt \beta$\end{turn} &
	\begin{turn}{90}{byte}\end{turn} &
	\begin{turn}{90}{short}\end{turn} &
	\begin{turn}{90}{char}\end{turn} &
	\begin{turn}{90}{int}\end{turn} &
	\begin{turn}{90}{long}\end{turn} &
	\begin{turn}{90}{float} \end{turn}&
	\begin{turn}{90}{double\quad{}\quad{}}\end{turn} &
	\begin{turn}{90}{bool}\end{turn}&
	\begin{turn}{90}{string}\end{turn} &
	\begin{turn}{90}{barrier}\end{turn} &
	\begin{turn}{90}{timer}\end{turn} \\ \hline\hline
byte    & F & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & F & F & F & F\\ \hline
short   & F & F & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & F & F& F & F\\ \hline
char    & F & F & F & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & F & F & F & F\\ \hline
int     & F & F & F & F & \cellcolor{gray!25}T & \cellcolor{gray!25}T & \cellcolor{gray!25}T & F & F & F & F\\ \hline
long    & F & F & F & F & F & \cellcolor{gray!25}T & \cellcolor{gray!25}T & F & F & F & F\\ \hline
float   & F & F & F & F & F & F & \cellcolor{gray!25}T & F & F & F & F\\ \hline
double  & F & F & F & F & F & F & F & F & F & F & F\\ \hline
bool    & F & F & F & F & F & F & F & F & F & F & F \\ \hline
string  & F & F & F & F & F & F & F & F & F & F & F \\ \hline
barrier & F & F & F & F & F & F & F & F & F & F & F \\ \hline
timer   & F & F & F & F & F & F & F & F & F & F & F\\ \hline
\end{tabular}
\end{center}

\subsubsection{Constructed Types}

\paragraph{Protocol}

\[
\alpha = Protocol(name_1,\{(tag_{1,1},\{(n_{1,1,1},t_{1,1,1}),\ldots,(n_{1,1,m_{1,1}},t_{1,1,m_{1,1}})\}),
\]
\[
\hspace*{3.6cm}(tag_{1,2},\{(n_{1,2,1},t_{1,2,1}),\ldots,(n_{1,2,m_{1,2}},t_{1,2,m_{1,2}})\}),
\]
\[
\vdots
\]
\[
\hspace*{3.9cm}(tag_{1,k_1},\{(n_{1,k_1,1},t_{1,k_1,1}),\ldots,(n_{1,k_1,m_{1,k_1}},t_{1,k_1,m_{1,k_1}})\})\})
\]
\[
\beta = Protocol(name_2,\{(tag_{2,1},\{(n_{2,1,1},t_{2,1,1}),\ldots,(n_{2,1,m_{2,1}},t_{2,1,m_{2,1}})\}),
\]
\[
\hspace*{3.6cm}(tag_{2,2},\{(n_{2,2,1},t_{2,2,1}),\ldots,(n_{2,2,m_{2,2}},t_{2,2,m_{2,2}})\}),
\]
\[
\vdots
\]
\[
\hspace*{3.9cm}(tag_{2,k_2},\{(n_{2,k_2,1},t_{3,k_2,1}),\ldots,(n_{2,k_2,m_{2,k_2}},t_{1,k_2,m_{2,k_2}})\})\})
\]

\[
\alpha \tle \beta \Leftrightarrow (\forall i: (1 \leq i \leq k_1): \exists j: (1 \leq j \leq k_2): tag_{1,i} = tag_{2,j} \wedge
\]
\[
(m_{1,i} = m_{2,j}) \wedge \bigwedge^{m_{1,i}}_{k=1}(n_{1,i,k} = n_{2,i,k}) \wedge (t_{1,i,k} \tev t_{2,i,k})
\]

\paragraph{Procedures}

Not sure what goes here yet.

\subsection{$\tle$}

\[
\alpha \tle \beta \Leftrightarrow (\alpha \teq \beta) \vee (\alpha \tle \beta)
\]

\section{Primitive Types}

\subsection{Type Equality ($\teq$)}

\[
\alpha \teq \beta\ \Leftrightarrow \mbox{Primitive}_?(\alpha) \wedge \mbox{Primitive}_?(\beta) \wedge \alpha = \beta
\]
where $\alpha$ and $\beta$ are types.

\subsection{Type Equivalence ($\tev$)}

For primitive types, type equivalence is the same as type equality, so for two types $\alpha$ and $\beta$:
\[
\alpha \tev \beta \Leftrightarrow Primitive_?(\alpha) \wedge Primitive_?(\beta) \wedge \alpha \tle \beta
\]

\subsection{Type Assignment Compatibility ($\tac$)}

\[
\alpha \tac \beta \Leftrightarrow Primitive_?(\alpha) \wedge Primitive_?(\beta) \wedge  \beta \leq \alpha
\]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Constructed Types}

\subsection{Type Equality ($\teq$)}

\subsubsection{Arrays}
\[
\alpha = Array(t_1, I_1)\ \wedge \beta =  Array(t_2, I_2)
\]
\[
\alpha \teq \beta \Leftrightarrow Array_?(\alpha) \wedge Array_?(\beta) \wedge (t_1 \teq t_2) \wedge ((I_1 = I_2) \vee (I_1 = \bot) \vee (I_2 = \bot))
\]

\subsubsection{Records}

\[
\alpha = Record(name_1, \{(n_{1,1},t_{1,1}),\ldots,(n_{1,m_1}, t_{1,m_1})\})
\]
\[
\beta = Record(name_2, \{(n_{2,1},t_{2,1}),\ldots,(n_{2,m_2}, t_{2,m_2})\})
\]
\paragraph{Name Equality:}
\[
\alpha \teq \beta \Leftrightarrow Record_?(\alpha) \wedge Record_?(\beta) \wedge (name_1 = name_2)
\]
\paragraph{Structural Equality:}
\[
\alpha \teq \beta \Leftrightarrow Record_?(\alpha) \wedge Record_?(\beta) \wedge (m_1 = m_2) \wedge \bigwedge^{m_1}_{i=1} (t_{1,i} \teq t_{2,i})
\]

\subsubsection{Protocols}

\subsubsection{Channel}

The access part of a channel can be {\it shared}, {\it shared read}, {\it shared write}, or {\it not shared}.
\
\[
\alpha = Channel(t_1, a_1) \wedge \beta = Channel(t_2, a_2)\]

\[
\alpha \teq \beta \Leftrightarrow Channel_?(\alpha) \wedge Channel_?(\beta) \wedge (t_1 \teq t_2) \wedge (a_1 = a_2)
\]

\subsubsection{Channel Ends}

For channel ends to be equivalent, they have to be the same ends and their channels have to be equivalent:
\[
\alpha = ChannelEnd(\delta, end_1) \wedge \beta = ChannelEnd(\gamma, end_2)
\]
\[
\alpha \teq \beta \Leftrightarrow ChannelEnd_?(\alpha) \wedge ChannelEnd_?(\beta) \wedge Channel_?(\delta) \wedge Channel_?(\gamma) \wedge (end_1 = end_2)
\]

\subsubsection{Procedures}

\[
\alpha = procedure(name_1, \{t_{1,1},\ldots,t_{1,m_1}\}, t_1) \wedge
\beta = procedure(name_2, \{t_{2,1},\ldots,t_{2,m_2}\}, t_2) \wedge
\]
\[
\alpha \teq \beta \Leftrightarrow (m_1 = m_2) \wedge (t_1 \teq t_2) \wedge (name_1 = name_2) \wedge \bigwedge^{m_1}_{i=1}(t_{1,i} \teq t_{2,i})
\]
\subsection{Type Equivalence ($\teq=v$)}

For all constructed types $\alpha$ and $\beta$, we have:
\[
\alpha \tev \beta \Leftrightarrow \alpha \teq \beta
\]

\subsection{Type Assignment Compatibility ($\tac$)}

\subsubsection{Arrays}

\[
\alpha = Array(t_1, I_1) \wedge \beta =  Array(t_2, I_2)
\]
\[
\alpha \tac \beta \Leftrightarrow Array_?(\alpha) \wedge Array_?(\beta) \wedge
\]
\[
((Protocol_?(t_1) \wedge Protocol_?(t_2) \wedge (t_2 \tle t_1)) \vee
(\neg{}Protocol_?(t_1) \wedge \neg{}Protocol_?(t_2) \wedge (t_1 \teq t_2)
\]
\subsubsection{Records}

\[
\alpha = Record(name_1, \{(n_{1,1},t_{1,1}),\ldots,(n_{1,m_1}, t_{1,m_1})\})
\]
\[
\beta = Record(name_2, \{(n_{2,1},t_{2,1}),\ldots,(n_{2,m_2}, t_{2,m_2})\})
\]

\[
\alpha \tac \beta \Leftrightarrow \alpha \tev \beta
\]
\subsubsection{Protocols}

\subsubsection{Channels}

Channels are non-assignable.

\subsubsection{Channel Ends}

\[
\alpha = ChannelEnd(\delta, end_1) \wedge \beta = ChannelEnd(\gamma, end_2)
\]
\[
\alpha \tac \beta \Leftrightarrow (end_1 = end_2) \wedge
\]
\[
 ((Protocol_?(\delta) \wedge Protocol_?(\gamma) \wedge (\delta \tle \gamma)) \vee
\]
\[
(\neg{}Protocol_?(\delta) \wedge \neg{}Protocol_?(\gamma) \wedge (\delta \teq \gamma))
\]

\subsubsection{Proceudres}

\end{document}


%%
%% Template appendix.tex
%%

\appendix

\chapter{How to Obtain the Datasets}
\label{cha:datasets}

The following SQL query is used to get the main SDSS dataset containing 2.8 million
labelled objects from the Sloan SkyServer:\footnote{
	\url{http://skyserver.sdss.org/CasJobs/}}

\begin{minted}[fontsize=\footnotesize, frame=single, tabsize=4]{sql}
SELECT
	-- right ascension and declination in degrees
	p.ra, p.dec,

	-- class of object, expert opinion (galaxy, star, or quasar)
	CASE s.class WHEN 'GALAXY' THEN 'Galaxy'
				 WHEN 'STAR' THEN 'Star'
				 WHEN 'QSO' THEN 'Quasar'
				 END AS class,

	s.subclass, -- subclass of object

	-- redshift of object from spectrum with error, expert opinion
	s.z AS redshift,
	s.zErr AS redshiftErr,
	s.zWarning,

	-- PSF and Petrosian mags in 5 bands (ugriz) with error
	p.psfMag_u, p.psfMagErr_u,
	p.psfMag_g, p.psfMagErr_g,
	p.psfMag_r, p.psfMagErr_r,
	p.psfMag_i, p.psfMagErr_i,
	p.psfMag_z, p.psfMagErr_z,

	p.petroMag_u, p.petroMagErr_u,
	p.petroMag_g, p.petroMagErr_g,
	p.petroMag_r, p.petroMagErr_r,
	p.petroMag_i, p.petroMagErr_i,
	p.petroMag_z, p.petroMagErr_z,

	-- extinction values
	p.extinction_u, p.extinction_g, p.extinction_r,
	p.extinction_i, p.extinction_z,

	-- size measurement in r-band in arc seconds
	p.petroRad_r, p.petroRadErr_r

FROM PhotoObj AS p
JOIN SpecObj AS s
ON s.bestobjid = p.objid

WHERE
	-- only include objects with complete and reasonably accurate data
	p.psfMagErr_u BETWEEN 0 AND 3
	AND p.psfMagErr_g BETWEEN 0 AND 3
	AND p.psfMagErr_r BETWEEN 0 AND 3
	AND p.psfMagErr_i BETWEEN 0 AND 3
	AND p.psfMagErr_z BETWEEN 0 AND 3
	AND p.petroMagErr_u BETWEEN 0 AND 3
	AND p.petroMagErr_g BETWEEN 0 AND 3
	AND p.petroMagErr_r BETWEEN 0 AND 3
	AND p.petroMagErr_i BETWEEN 0 AND 3
	AND p.petroMagErr_z BETWEEN 0 AND 3
	AND p.petroRadErr_r BETWEEN 0 AND 3
	AND s.zErr BETWEEN 0 AND 0.1
	AND s.zWarning = 0    -- spectrum is ok
\end{minted}

The following query is used to extract photometric measurements from all 800 million
in the database. Since the file is fairly big (around 200 GB in size),
a special request might need to be made.

\begin{minted}[fontsize=\footnotesize, frame=single, tabsize=4]{sql}
SELECT
	p.ra, p.dec,
	CASE s.class WHEN 'GALAXY' THEN 'Galaxy'
				 WHEN 'STAR' THEN 'Star'
				 WHEN 'QSO' THEN 'Quasar'
				 END AS class,
	s.subclass,
	s.z AS redshift,
	s.zErr AS redshiftErr,
	s.zWarning,
	p.psfMag_u, p.psfMagErr_u,
	p.psfMag_g, p.psfMagErr_g,
	p.psfMag_r, p.psfMagErr_r,
	p.psfMag_i, p.psfMagErr_i,
	p.psfMag_z, p.psfMagErr_z,
	p.petroMag_u, p.petroMagErr_u,
	p.petroMag_g, p.petroMagErr_g,
	p.petroMag_r, p.petroMagErr_r,
	p.petroMag_i, p.petroMagErr_i,
	p.petroMag_z, p.petroMagErr_z,
	p.extinction_u, p.extinction_g, p.extinction_r,
	p.extinction_i, p.extinction_z,
	p.petroRad_r, p.petroRadErr_r
FROM PhotoObj AS p
LEFT JOIN SpecObj AS s
ON s.bestobjid = p.objid
\end{minted}

%\chapter{Guide to Using mclearn}
%\label{cha:mclearn}

%\section{Installation}
%\label{sub:installation}

%\section{Usage and Examples}
%\label{sub:usage}

%\chapter{Vectorisation of the Variance Estimation}
%\label{cha:vectorise}

%In estimating the variance of the unlabelled pool, there are two matrices we wish to compute...

\chapter{Dust Extinction Vectors} \index{dust extinction}
\label{cha:dustvectors}

The SDF98 extinction values are given in the SDSS dataset. To calculate the other
two extinction vectors, we start with a reference reddening quantity
\begin{IEEEeqnarray*}{lCl}
	E_{B-V} &=& \frac{\A_r}{2.751}
\end{IEEEeqnarray*}
where $\A_r$ is the SDF98 extinction value in the r-band.

As a check, we can actually recover the SDF98 extinction vector as follows:
\begin{IEEEeqnarray*}{lCl}
	\A_u &=& 5.155 \cdot E_{B-V} \\
	\A_g &=& 3.793 \cdot E_{B-V} \\
	\A_r &=& 2.751 \cdot E_{B-V} \\
	\A_i &=& 2.086 \cdot E_{B-V} \\
	\A_z &=& 1.479 \cdot E_{B-V}
\end{IEEEeqnarray*}
Later, \citeN{schlafly11} applied a different extinction curve, giving us the following
correction values:
\begin{IEEEeqnarray*}{lCl}
	\A_u &=& 4.239 \cdot E_{B-V} \\
	\A_g &=& 3.303 \cdot E_{B-V} \\
	\A_r &=& 2.285 \cdot E_{B-V} \\
	\A_i &=& 1.698 \cdot E_{B-V} \\
	\A_z &=& 1.263 \cdot E_{B-V}
\end{IEEEeqnarray*}
Recently, \citeN{wolf14} remapped the $E_{B-V}$ scale to
\begin{IEEEeqnarray*}{lCl}
	E'_{B-V} &=&
	\begin{cases}
		E_{B-V} & \text{if } E_{B-V} \in [0, 0.04], \\
		E_{B-V} + 0.5(E_{B-V} - 0.04) & \text{if } E_{B-V} \in [0, 0.08], \\
		E_{B-V} + 0.02 & \text{if } E_{B-V} \in [0.08, +\infty].
	\end{cases}
\end{IEEEeqnarray*}
which can then be used to calculate a new set of correction values:
\begin{IEEEeqnarray*}{lCl}
	\A_u &=& 4.305 \cdot E'_{B-V} \\
	\A_g &=& 3.288 \cdot E'_{B-V} \\
	\A_r &=& 2.261 \cdot E'_{B-V} \\
	\A_i &=& 1.714 \cdot E'_{B-V} \\
	\A_z &=& 1.263 \cdot E'_{B-V}
\end{IEEEeqnarray*}
Each of these correction values need to be subtracted from the corresponding magnitudes
to make up for the loss of the scattered light.

\chapter{Supplementary Results}
\label{cha:supp}

In this Appendix, we present results that are not vital to the main narrative but still somewhat
interesting.

%\section{Reliability of Probabilities Estimates}
%\label{sec:forest_prob}
%
%Figure \ref{fig:forest_multinom} shows the learning curves
%
%We found that the probabilities estimated by both random forests and multinomial regression
%to be unreliable. [To do: include learning curves of random forest, one-vs-rest, and
%multinomial regression, and warm-start here, test both SDSS and VST ATLAS]
%
%\begin{figure}[p]
%	\centering
%	\begin{subfigure}{\textwidth}
%		\centering
%		\includegraphics[width=\textwidth]{figures/appendix/sdss_forest_multinom}
%		\caption{SDSS dataset}
%		\label{fig:sdss_forest_multinom}
%	\end{subfigure}\\
%	\begin{subfigure}{\textwidth}
%		\centering
%		\includegraphics[width=\linewidth]{figures/appendix/vstatlas_forest_multinom}
%		\caption{VST ATLAS dataset}
%		\label{fig:vstatlas_forest_multinom}
%	\end{subfigure}
%	\caption[Reliability of probability estimates]{Learning curves of various classifiers: }
%	\label{fig:forest_multinom}
%\end{figure}

\section{Effects of Dust Extinction on Recall}

In Chapter \ref{cha:expt1} we tested the effects of three different extinction vectors
on the accuracy rate. Figure \ref{fig:map_recall_uncorrected} shows how the recall rate is distributed
over the celestial sphere. Overall, the recall on galaxies is almost perfect, while
the recall on stars is fairly average. On the three pages after that, Figures \ref{fig:map_recall_sfd98}, \ref{fig:map_recall_sf11}, and \ref{fig:map_recall_w14} show
the improvement on recall after each extinction vector is applied. The interesting bit
is that there is a patch of stars right next to the Milky Way plane that gets a big improvement
in recall after reddening correction. Thus the extinction vector would be very important
if there were more objects that are closer to the Milky Way plane (which is the case in the SkyMapper project).

\begin{figure}[p]
	\centering
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\textwidth]{figures/appendix/map_recall_uncorrected_Galaxy}
		\caption{Recall map of galaxies.}
		\label{fig:map_recall_uncorrected_galaxies}
	\end{subfigure}\\
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_uncorrected_Star}
		\caption{Recall map of stars.}
		\label{fig:map_recall_uncorrected_stars}
	\end{subfigure}
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_uncorrected_Quasar}
		\caption{Recall map of quasars.}
		\label{fig:map_recall_uncorrected_quasars}
	\end{subfigure}
	\caption[Recall maps when there is no corrections]{
        Recall maps when there is no corrections.}
	\label{fig:map_recall_uncorrected}
\end{figure}

\begin{figure}[p]
	\centering
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\textwidth]{figures/appendix/map_recall_sfd98_Galaxy}
		\caption{Recall improvement map of galaxies.}
		\label{fig:map_recall_sfd98_galaxies}
	\end{subfigure}\\
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_sfd98_Star}
		\caption{Recall improvement map of stars.}
		\label{fig:map_recall_sfd98_stars}
	\end{subfigure}
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_sfd98_Quasar}
		\caption{Recall improvement map of quasars.}
		\label{fig:map_recall_sfd98_quasars}
	\end{subfigure}
	\caption[Recall improvement maps with SFD98]{
        Recall improvement maps when the SFD98 extinction vector is used.}
	\label{fig:map_recall_sfd98}
\end{figure}

\begin{figure}[p]
	\centering
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\textwidth]{figures/appendix/map_recall_sf11_Galaxy}
		\caption{Recall improvement map of galaxies.}
		\label{fig:map_recall_sf11_galaxies}
	\end{subfigure}\\
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_sf11_Star}
		\caption{Recall improvement map of stars.}
		\label{fig:map_recall_sf11_stars}
	\end{subfigure}
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_sf11_Quasar}
		\caption{Recall improvement map of quasars.}
		\label{fig:map_recall_sf11_quasars}
	\end{subfigure}
	\caption[Recall improvement maps of when with SF11]{
        Recall improvement maps of when the SF11 extinction vector is used.}
	\label{fig:map_recall_sf11}
\end{figure}

\begin{figure}[p]
	\centering
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\textwidth]{figures/appendix/map_recall_w14_Galaxy}
		\caption{Recall improvement map of galaxies.}
		\label{fig:map_recall_w14_galaxies}
	\end{subfigure}\\
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_w14_Star}
		\caption{Recall improvement map of stars.}
		\label{fig:map_recall_w14_stars}
	\end{subfigure}
	\begin{subfigure}{\textwidth}
		\centering
		\includegraphics[width=0.75\linewidth]{figures/appendix/map_recall_w14_Quasar}
		\caption{Recall improvement map of quasars.}
		\label{fig:map_recall_w14_quasars}
	\end{subfigure}
	\caption[Recall improvement maps with W14]{
        Recall improvement maps of when the W14 extinction vector is used.}
	\label{fig:map_recall_w14}
\end{figure}

\section{Variance of the Mean Reward in Thompson Sampling}

Finally, Figures \ref{fig:sdss_sigmas} and \ref{fig:vstatlas_sigmas} show how $\sigma^2$,
the variance of the expected reward, changes with the training set size. As we would expect,
the variance decreases exponentially over time, since as we increase
the training size, we become more certain of the classifier's accuracy. In addition, the incremental change
of the accuracy rate shrinks over time as we approach an accuracy of 100\%.

\begin{figure}[p]
	\centering
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\textwidth]{figures/5_thompson/vstatlas_bl_sigmas}
		\caption{Balanced pool and logistic regression}
		\label{fig:sdss_bl_sigmas}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{figures/5_thompson/sdss_br_sigmas}
		\caption{Balanced pool and RBF SVM}
		\label{fig:sdss_br_sigmas}
	\end{subfigure}
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\textwidth]{figures/5_thompson/sdss_ul_sigmas}
		\caption{Unbalanced pool and logistic regression}
		\label{fig:sdss_ul_sigmas}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{figures/5_thompson/sdss_ur_sigmas}
		\caption{Unbalanced pool and RBF SVM}
		\label{fig:sdss_ur_sigmas}
	\end{subfigure}
	\caption[Variance of the mean reward of heuristics (SDSS)]{
		Variance (average of 10 trials) of the expected reward in Thompson sampling with the SDSS dataset.}
	\label{fig:sdss_sigmas}
\end{figure}

\begin{figure}[p]
	\centering
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\textwidth]{figures/5_thompson/vstatlas_bl_sigmas}
		\caption{Balanced pool and logistic regression}
		\label{fig:vstatlas_bl_sigmas}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{figures/5_thompson/vstatlas_br_sigmas}
		\caption{Balanced pool and RBF SVM}
		\label{fig:vstatlas_br_sigmas}
	\end{subfigure}
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\textwidth]{figures/5_thompson/vstatlas_ul_sigmas}
		\caption{Unbalanced pool and logistic regression}
		\label{fig:vstatlas_ul_sigmas}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{figures/5_thompson/vstatlas_ur_sigmas}
		\caption{Unbalanced pool and RBF SVM}
		\label{fig:vstatlas_ur_sigmas}
	\end{subfigure}
	\caption[Variance of the mean reward of heuristics (VST ATLAS)]{
		Variance (average of 10 trials) of the expected reward in Thompson sampling with the VST ATLAS dataset.}
	\label{fig:vstatlas_sigmas}
\end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "thesis"
%%% End:


% !TEX root = ./physics_of_fluids.tex
% !TEX TS-program = xelatex
% !TEX encoding = UTF-8 Unicode
\chapter{Viscous flows}
\label{chap:viscous_flows}
Whenever the viscous diffusion timescale $\rho L^2/\mu$ is short with respect to inertial $L/U$ or imposed $\omega^{-1}$ timescales, flows will be dominated by viscosity. This situation occurs of course if the viscosity of the material is high, e.g. with mud, magma or glass melt (figure~\ref{fig:viscous_flows}) but not only. Glaciers were reported to flow as early as 1873 \citep{Aitken1873}. These so-called \textit{rivers of ice} indeed flow, albeit very slowly -- typically less than a metre a day -- making inertial phenomena irrelevant (figure~\ref{fig:viscous_flows}; see also the slow motion footage taken by BBC Earth Lab \url{https://youtu.be/ghC-Ut0fW4o}). At very small scales where bacteria and algae evolve, diffusion competes and usually overcomes inertial effects. As a result, microorganisms living at such scales have evolved non-intuitive strategies to move as we will see next.

In all these examples the ratio between the diffusive and inertial timescales -- the Reynolds number Re = $\rho U L/\mu$ -- is much smaller than one. Fluid motion can still be described with the Navier-Stokes equations in this context, but we will now see that the condition Re $\ll$ 1 implies that some terms have not the same order of magnitude than others. Exploiting the low-Re number hypothesis will enable us to obtain the relevant equation for viscous flow motion, the \textbf{Stokes equation}.

\begin{figure}[htbp]
\begin{center}
\includegraphics[height=4cm]{Briksdalsbreen.jpg}
\includegraphics[height=4cm]{fiberglass.jpg}
\includegraphics[height=4cm]{sorin.png}
\includegraphics[width=15cm]{salmonella.png}
\caption{\textbf{Flows dominated by viscosity.} Top left : the Briksdalsbreen glacier in Norway is slowly flowing into a lake (photograph by vicrogo, public domain). Top middle: glass fibre manufacturing (photograph Saint-Gobain). Top right: modern optical fibre drawing process allow to produce multilayered fibres \citep{Abouraddy2007}. Bottom: micron-sized salmonellae swim with the help of a bundle of rotating helicoidal flagellae \citep{Elgeti2015}.}
\label{fig:viscous_flows}
\end{center}
\end{figure}

\section{Low-Re number flows}
Whenever fluids have a high viscosity $\mu$, or flow at small scale $L$ or with a low velocity $U$, the equations describing their motion can be greatly simplified. This can be seen by non dimensionalising the equations of motions with the natural scales of the problem $\bU=U\bu$, $\bX=L\bx$ (here we denote dimensioned quantities with capital letters). The pressure can be made dimensionless with a natural viscous scale $P=\mu\frac{U}{L}p$ and finally if there is a imposed timescale $\omega^{-1}$ (e.g. the inverse of the swimming frequency) we may write $T=\omega^{-1}t$.
\begin{equation}
\mathrm{Re}_\omega\pd{\bu}{t} + \mathrm{Re}\lp\bu\cdot\nabla\rp\bu=-\nabla p+\Delta \bu.
\label{eq:pre-stokes_equation}
\end{equation}
Here, two Reynolds numbers appear:
\begin{equation}
\mathrm{Re}_\omega=\frac{\rho L^2 \omega}{\mu} \quad \text{and} \quad \mathrm{Re}=\frac{\rho U L}{\mu},
\end{equation}
which each may be interpreted as a ratio of timescales as seen in the introduction of this chapter. Note that for e.g. flagellae propelled microorganisms, the oscillatory Reynolds number $\mathrm{Re}_\omega$ involves the relevant velocity scale $L\omega$ for the fluid set into motion by the oscillating flagella.

In equation~\eqref{eq:pre-stokes_equation} each variable has been rescaled with its expected range of variation range. Therefore each of the force terms (right hand side) is $\mathcal O(1)$ while the unsteady and convective part of momentum variation (left hand side) are respectively of order $\mathrm{Re}_\omega$ and $\mathrm{Re}$. For flows without an imposed frequency (flowing glass melts or glacier flow), these two numbers will be identical. But if the flow is produced by an oscillating object, they can significantly differ. Table~\ref{tbl:Reynolds} reports an estimation of these two Reynolds numbers for a range of organisms living in aqueous environments, where it can be seen that the double condition $\mathrm{Re}_\omega\ll 1$, $\mathrm{Re}\ll 1$ is fulfilled in the realm of microorganisms.
\begin{table}
\begin{center}
\begin{tabular}{ccccccc}
Organism & length & velocity & frequency & Re & Re$_\omega$\\
\hline\hline
\textbf{Bacterium} & 10 $\mu$m & 10 $\mu$m/s & 100 Hz & \textbf{10$^\text{-4}$} & \textbf{10$^\text{-2}$}\\
\textbf{Spermatozoon} & 100 $\mu$m & 100 $\mu$m/s & 10 Hz & \textbf{10$^\text{-2}$} & \textbf{10$^\text{-1}$}\\
\textbf{Ciliate} & 100 $\mu$m & 1 mm/s & 10 Hz & \textbf{10$^\text{-1}$} & \textbf{10$^\text{-1}$}\\
Tadpole & 1 cm & 10 cm/s & 10 Hz & 10$^\text{3}$ & 10$^\text{3}$\\
Small fish & 10 cm & 10 cm/s & 10 Hz & 10$^\text{4}$ & 10$^\text{5}$\\
Penguin & 1 m & 1 m/s & 1 Hz & 10$^\text{6}$ & 10$^\text{6}$\\
Sperm whale & 10 m & 1 m/s & 0.1 Hz & 10$^\text{7}$ & 10$^\text{7}$\\
\hline
\end{tabular}
\end{center}
\caption{\textbf{Reynolds number for different living organisms.} Bacteria, spermatozoa and ciliates are all characterised by Reynolds numbers Re and Re$_\omega$ much smaller than unity. The flowing fluid in their vicinity is therefore accurately described with the Stokes equation. Data from \citet{Lauga2020}.}
\label{tbl:Reynolds}
\end{table}

In this limit, the Navier-Stokes equation reduces to the much simpler \textbf{Stokes equation}\index{Stokes equation}:
\begin{equation}
\nabla p=\Delta \bu\quad\text{or, in its dimensioned version:}\quad\nabla P=\mu\Delta \bU.
\label{eq:stokes_equation}
\end{equation}

\prg{Cauchy equation.} The Stokes equation may also be rewritten as the following \textit{Cauchy equation}:
\begin{equation}
\nabla\cdot\tensorsym\sigma=\matrixsym 0.
\end{equation}
With this formulation it becomes apparent that viscous flows are \textit{force free}: in absence of significant inertia, the forces balance each other.

\section{Stokes flow's properties}
\prg{Linearity.} The Stokes equation~\eqref{eq:stokes_equation} is \textbf{linear}: this means that elementary solutions can be used to construct more involved ones, either as a weighted sum of individual singular solutions or as a convolution integral between an appropriate Green's function and data boundaries. For example, the knowledge of the velocity $\bU$ at the boundary $S$ of a fluid domain allows to express the fluid velocity $\bu$ at any point $\br$ of the domain as:
\begin{equation}
u_i(\br)=\int_S \mathcal G_{ij}(\br|\br_0)U_j(\br_0)\,\mathrm dS(\br_0).
\label{eq:stokes_green_velocity_boundary}
\end{equation}
Similarly a knowledge of the \textit{forces}\footnote{\textit{Mixed boundary conditions}, constituted of velocity data on part of the boundaries, and forces data on other, can be treated with the same procedure.} $\bF$ at the domain boundary would lead to the following formal form for the solution:
\begin{equation}
u_i(\br)=\int_S g_{ij}(\br|\br_0)F_j(\br_0)\,\mathrm dS(\br_0).
\end{equation}
A practical consequence of these relations is the \textbf{unicity} of the Stokes flow solution: the boundary conditions uniquely determine the solution. This contrasts with the multitude of solutions that can be encountered for higher Reynolds number and originate (mathematically) from the nonlinear term of the Navier-Stokes equation.
\prg{Reversibility.\index{Stokes reversibility}} Another quite surprising property of Stokes flow is their \textbf{reversibility}.
\begin{figure}[htbp]
\begin{center}
\includegraphics[width=4cm]{taylor_reversibility_1.png}
\includegraphics[width=4cm]{taylor_reversibility_2.png}
\includegraphics[width=4cm]{taylor_reversibility_3.png}
\caption{\textbf{Stokes flow kinematic reversibility.} Left: a drop of dye is injected in a quiescent viscous liquid filling the space between two concentric cylinders. Middle: on rotating the inner cylinder with a handle, the dye is stretched and stirred so that it becomes barely visible. Right: reversing the cylinder motion allows to relocate the dye's drop at its initial position almost perfectly (from G.I. Taylor's \textit{Low-Reynolds-Numbers Flows} movie \copyright\ National Committee for Fluid Mechanics Films / Education Development Center).}
\label{fig:reversibility}
\end{center}
\end{figure}
This counter-intuitive phenomenon is illustrated on figure~\ref{fig:reversibility}. A drop of dye mixed by differential rotation in a Taylor-Couette apparatus can be ``unmixed'' by reversing the boundary velocity. From a mathematical point of view, this reversal property can be understood by noting that changing the sign of the boundary velocity in \eqref{eq:stokes_green_velocity_boundary} simply changes the sign of the solution. Alternatively it can also be noted that the transformation $(\bu,p)\to(-\bu,-p)$ also yields a solution of the Stokes equation~\eqref{eq:stokes_equation} (provided that the boundary conditions are transformed as well).

\prg{A paradox?} From a physical point of view, this reversibility is more troublesome: if diffusion is associated with irreversible microscopic phenomena, how can diffusion-dominated flows be reversible? Actually the reversal illustrated figure~\ref{fig:reversibility} is purely \textit{kinematic}: while the velocity fields have been reversed and the drop of dye retrieved its overall initial position, molecular diffusion has acted on the microscopic scale as evidenced by the slightly smeared aspect of the final drop, heat has been produced by viscous dissipation and the entropy of the final state is indeed higher than that of the starting state.
\section{Moving in a viscous world}
A paradigm for motion in viscous fluids is the settlement of a sphere, first investigated by Stokes. Arguably lengthy calculations (see tutorial) allow to obtain the expression for the velocity and pressure field around a sphere of radius $R$ settling steadily at velocity $-\bV^\infty$ in a quiescent viscous fluid in its reference frame:
\begin{subequations}
\label{eq:stokes_sphere}
\begin{empheq}[left=\empheqlbrace]{alignat=2}
u_i &\,=\,&& -\frac{3R}{4}V^\infty_j\lp\frac{\delta_{ij}}{r}+\frac{r_ir_j}{r^3}\rp -\frac{3R^3}{4}V^\infty_j\lp\frac{\delta_{ij}}{3r^3}-\frac{r_ir_j}{r^5}\rp,\\
p-p_\infty&\,=\,&&-\frac{3\mu R}{2}\frac{V^\infty_jr_j}{r^3}.
\end{empheq}
\end{subequations}
\begin{figure}[htbp]
\begin{center}
\includegraphics{guazzelli_fixed_sphere.pdf}
\includegraphics{guazzelli_moving_sphere.pdf}
\caption{\textbf{Spheres in viscous flows}. Left: A fixed sphere deflects the surrounding flowing fluid. Right: A moving sphere in a still environment pushes the fluid in its vicinity \citep{Guazzelli2011}.}
\label{fig:viscous_spheres}
\end{center}
\end{figure}
These expressions allow to evaluate the stresses at the sphere surface and to deduce the well-known \textbf{Stokes drag}\index{Stokes drag}  $\bF = 6 \pi \mu R \bV^\infty$ exerted on the sphere.

An alternative and very useful viewpoint is to present these results in terms of the force $\mathbf f=-\bF$ exerted by the sphere on the fluid:
\begin{equation}
u_i \,=\, \underbrace{\frac{1}{8 \pi\mu R}f_j\lp\frac{\delta_{ij}}{r}+\frac{r_ir_j}{r^3}\rp}_\text{Stokeslet contribution} +\frac{R^2}{8\pi\mu R}f_j\lp\frac{\delta_{ij}}{3r^3}-\frac{r_ir_j}{r^5}\rp.
\label{eq:stokes_force_sphere}
\end{equation}
Interestingly if we were to shrink the size of the sphere to 0 while keeping the force constant, the only remaining term in the flow field would be the first one. This so-called Stokeslet contribution is of fundamental importance in suspension dynamics, bacteria hydrodynamics and more generally in the modelling of viscous flows.
\subsection{Point force induced flow: the Stokeslet\index{Stokeslet}}
The Stokeslet is a fundamental solution for the Stokes equation, and describes the flow that would be induced by a point force $\mathbf f\, \delta(\bx-\bx_0)$ located at $\bx = \bx_0$. The corresponding velocity and pressure fields therefore satisfy the following \textit{forced Stokes equation}:
\begin{equation}
-\nabla p + \mu \Delta \bu + \mathbf f \,\delta(\bx-\bx_0) = \matrixsym 0.
\label{eq:forced_stokes_equation}
\end{equation}
Note that in this expression $\mathbf f$ is a constant vector.

The flow field solution is termed \textbf{Stokeslet} and is characterized by:
\begin{equation}
\left.\bu_{\text{stokeslet}}\right|_i(\bx)=\frac{1}{8\mathrm\pi\mu}\mathcal S_{ij}(\bx |\bx_0) f_j,
\label{eq:stokeslet_velocity_component}
\end{equation}
with $\mathcal S_{ij}$ being the \textit{Oseen-Burgers tensor} defined as:
\begin{equation}
\mathcal S_{ij}=\frac{\delta_{ij}}{r}+\frac{r_i r_j}{r^3}.
\end{equation}
Let's now see in more details how this solution is constructed. To so so it will prove useful to first introduce the Green's function for Laplace equation $g(\bx |\by)$.
\prg{Green's function\index{Green's function} for Laplace equation.} The Green's function $g(x|y)$ for Laplace equation is the function satisfying:
\begin{equation}
\Delta g(\bx |\by) = \delta\lp\bx-\by\rp.
\end{equation}
It is an harmonic function of space except at the point $\bx=\by$ where it is singular. Symmetry considerations on this function suggest that it only depends on the radius $r = \left\|\bx-\by\right\|$. On integrating over a small ball containing the singularity we get:
$$
\oiint \pd{g}{r} \mathrm dS = 1,
$$
so that the Green's function for Laplace equation is:
\begin{equation}
g(\bx |\by) = -\frac{1}{4\mathrm{\pi}r}.
\end{equation}
\prg{Stokeslet obtention.} With the help of the Green's function for Laplace equation, the divergence of the forced Stokes equation~\eqref{eq:forced_stokes_equation} may be written as
\begin{equation}
\Delta\lp p-\mathbf f\cdot\nabla\lp-\frac{1}{4\pi r}\rp\rp = 0.
\end{equation}
The maximum principle for harmonic functions allows us to directly write the pressure as:
\begin{equation}
p=\mathbf f\cdot\nabla\lp-\frac{1}{4\pi r}\rp.
\end{equation}
Injecting this form for the pressure in equation~\eqref{eq:forced_stokes_equation} we obtain:
\begin{equation}
\mu \Delta u_i=\frac{1}{4\mathrm\pi}\lp\underbrace{\delta_{ij}\pd{}{x_k}\pd{}{x_k}}_{\tensorsym I \Delta}\lp\frac{1}{r}\rp-\underbrace{\pd{}{x_i}\pd{}{x_j}}_{\nabla\nabla}\lp\frac{1}{r}\rp\rp f_j.
\label{eq:stokeslet_velocity_laplacian}
\end{equation}
From the structure of this relationship, the adventurous reader might tempt to look for a solution of the form:
\begin{equation}
 u_i=\frac{1}{4\mathrm\pi}\lp\delta_{ij}\Delta \mathcal H-\pd{^2\mathcal H}{x_ix_j}\rp f_j.
\label{eq:stokeslet_velocity_form}
\end{equation}
Note that this velocity field is solenoidal, as:
\begin{equation}
 u_{i,i}=\frac{1}{4\mathrm\pi}\lp\Delta \mathcal H_{,j}-\Delta \mathcal H_{,j}\rp f_j \equiv 0.
\end{equation}
Injecting~\eqref{eq:stokeslet_velocity_form} into~\eqref{eq:stokeslet_velocity_laplacian} we get:
\begin{equation}
\frac{1}{4\mathrm\pi}\lp\tensorsym I\delta-\nabla\nabla\rp\lp\mu\Delta\mathcal H-\lp\frac{1}{r}\rp\rp=0.
\end{equation}
This reduces to the following Poisson equation for $\mathcal H$\footnote{Note that we did not consider the integration constants because they would not appear in the velocity field expression anyway.}:
\begin{equation}
\mu\Delta\mathcal H=\frac{1}{r} \quad\text{with solution:}\quad\mathcal H=\tfrac{1}{2\mu}r.
\end{equation}
Noting that $r=\lp r_kr_k\rp^{1/2}$, we deduce:
\begin{equation}
r_{,i}=\frac{r_{k,i}r_k}{(r_mr_m)^{1/2}}\equiv\frac{x_i}{r} \quad \text{and similarly} \quad r_{,ij}=\frac{\delta_{ij}}{r}-\frac{r_ir_j}{r^3},
\end{equation}
to finally obtain the expression of the Stokeslet velocity field~\eqref{eq:stokeslet_velocity_component} we were looking for:
\begin{equation}
\bu_\text{stokeslet}=\frac{1}{8\pi\mu}\tensorsym S \mathbf f.
\end{equation}
\subsection{The motion of slender objects}
We have seen that in the limit of radius shrinking down to zero, a sphere applying a force to a viscous fluid generates a Stokeslet flow. But looking back at the full expression for the flow set into motion by a sphere of finite size~\eqref{eq:stokes_force_sphere}, it is apparent that the total contribution is actually composed of two parts: a Stokeslet and higher-order singularity -- a dipole. Without entering into the details of the derivation, \citet{Hancock1953} and \citet{Lighthill1975} proposed to describe the fluid flows around more general, and slender, objects, as integrals of Stokeslets and dipole contribution. More precisely it appears that the force exerted by a viscous fluid on a very long cylinder of radius $R$ and length $L$ depends on the orientation of the flow. If the flow is perpendicular to the filament axis, the force exerted on the cylinder per unit length is:
\begin{subequations}
\begin{equation}
f_\perp \simeq c_\perp u_\perp \quad \text{with}\quad c_\perp=\frac{4\pi\mu}{\ln(L/R)}.
\end{equation}
And similarly, if the flow is now parallel to the fibre:
\begin{equation}
f_\parallel \simeq c_\parallel u_\parallel \quad \text{with}\quad c_\parallel=\frac{2\pi\mu}{\ln(L/R)}.
\end{equation}
\end{subequations}
Note that there is a factor 2 between $f_\perp$ and $f_\parallel$. This drag anisotropy has consequences on the settling of slender objects that we shall not detail here, but the interested reader will find detailed and useful accounts in \citet{Duprat2016} or \citet{Lauga2020} for example.
%\section{Bacterial hydrodynamics}
%
%Waving sheet Taylor
%
%Scallop theorem
%
%Bacteria with flagellae
%
%\section{Flows at the nanoscale}
%Slip length


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath,amsfonts}
\usepackage{url}
\usepackage{mathpartir}

\title{Interface for ROSlab and REACT}
\date{\today}

\begin{document}

\maketitle

\newcommand{\tr}[1]{\stackrel{#1}{\longrightarrow}}

For the segway robot: so we give a 2 dimensional model.
%assume the center of the robot's frame is the geometric center of the robot.\\
This model is obviously (too simple) and idealized but it gives us a starting point.

REACT will provide a global controller that sends commands to the robots.
The robots store the commands in a local FIFO buffer until they executed.
The commands tells the robot how to move.
In this version, the controller tells the robot at which speed to move and rotate.
Furthermore, we assume the robots have a clock (assumed perfect), the message receptions (and similar computations) take no time.

If everybody agrees with the semantics, we can proceed to specify a set of ROS messages for the integration of the REACT controller in a system of robots programmed with ROSlab.
We can also discuss other types of commands, e.g., trajectory/path to follow, gripping.

\paragraph{Robot description.}~\\

A robot is described by the tuple $(id, \vec p, \theta, \mathit{cmds})$ where
\begin{itemize}
\item $id$ is a unique identifier.
\item $\vec p$ is the position. In the 2D case, $\vec p = \begin{pmatrix} x \\ y \end{pmatrix}$.
\item $\theta$ is the angle between the robot frame and the global frame. In the 2D case, it is a single angle.
\item $\mathit{cmds}$ is a queue of commands. We use $c::C$ to match command $c$ at the head of the queue $C$ and $C::c$ for the tail. $[]$ is the empty queue.
\end{itemize}

Each command has the form ``$\text{move}(v, \omega, t)$'' where
$v$ is the speed (along the $x$ axis of the robot frame),
$\omega$ is an angular speed (rotation center is $(0,0)^T$ in the robot frame),
$t$ is the time for witch this command should be executed.

\paragraph{Semantic for a single robot.}~\\

A transition from state $R$ to $R'$ taking time $t$ and with action $a$ is written as $R \tr{a,t} R'$.
The action $a$ is used to synchronize messages.
$\tau$ is the silent action.
$id?c$ means robot with identifier $id$ receive a message.
$id!c$ means a message is sent to robot with identifier $id$.

\[
\inferrule[idle]
          { R = (id, \vec p, \theta, []) \\ dt \geq 0}
          { R \tr{\tau,dt} R }
\]

\[
\inferrule[recv]
          { R = (id, \vec p, \theta, C) \\
            R' = (id, \vec p, \theta, C :: c) }
          { R \tr{id?c,0} R' }
\]

\[
\inferrule[cmd done]
          { R = (id, \vec p, \theta, \text{move}(v, \omega, 0) :: C) \\
            R' = (id, \vec p, \theta, C) }
          { R \tr{\tau,0} R' }
\]

\[
\inferrule[exec]
    { R = (id, \vec p, \theta, \text{move}(v, \omega, t) :: C) \\
      R' = (id, \vec p', \theta', \text{move}(v, \omega, t - dt) :: C ) \\\\
      t \geq dt \geq 0 \\
      \theta' = \theta + \omega \cdot dt \\
      \vec p' = \vec p + \left({\begin{tabular}{cc} $\cos(\theta)$ & $\sin(\theta)$ \\ $\sin(\theta)$ & $\cos(\theta)$ \end{tabular}}\right) \vec q \\\\
      \text{where } \vec q = \left\{ {\begin{tabular}{lr} $(v \cdot dt, 0)$ & if $\omega = 0$ \\
                                     $(v/\omega \cdot \sin(\omega \cdot dt), v/\omega \cdot (1- \cos(\omega \cdot dt)))$ & otherwise \end{tabular}} \right.\\
    }
    { R \tr{\tau,dt} R' }
\]

\paragraph{Multiple robots.}

We have transitions of the individual robots are labeled with time.
When synchronising the transitions of multiple robots we simply requires that the time on the transition of every robot agree:

\[
\inferrule[time]
          { \forall i \in I. R_i \tr{\tau,t} R_i' }
          { \prod_{i \in I} R_i \tr{\tau,t} R_i' }
\]

\noindent
$||$ denotes the parallel composition, $\prod$ the index parallel composition.

For messages, we require:
\[
\inferrule[msg]
          { R_a \tr{id!c,0} R_a' \\ R_b \tr{id?c,0} R_b' \\
            \forall i \in I \setminus \{a,b\}. R_i \tr{\tau,0} R_i' }
          { R_a || R_b ~|| \prod_{i \in I \setminus \{a,b\}} R_i ~~\tr{\tau,0}~~ R_a' || R_b' ~|| \prod_{i \in I \setminus \{a,b\}} R_i' }
\]

In this formulation the controller is a special robot with the ability to send messages.

\end{document}