\title{Ideals, Varieties and \Mtwo}
\titlerunning{Ideals, Varieties and \Mtwo}
\toctitle{Ideals, Varieties and \Mtwo}
\author{Bernd Sturmfels\thanks{Partially supported by
the National Science Foundation (DMS-9970254).}}
\authorrunning{B. Sturmfels}
% \institute{University of California at Berkeley, Department of Mathematics,
%   Berkeley, CA 94720, USA}
\maketitle

\begin{abstract}
This chapter introduces \Mtwo commands for
some elementary computations in algebraic geometry.
Familiarity with Gr\"obner bases is assumed.
\end{abstract}

Many students and researchers alike have their first encounter with
Gr\"ob\-ner bases through the delightful text books \cite{CLO1} and \cite{CLO2}
by David Cox, John Little and Donal O'Shea. This chapter illustrates
the use of \Mtwo for some computations discussed in these books.
It can be used as a supplement for an advanced undergraduate course or
first-year graduate course in computational algebraic geometry. The
mathematically advanced reader will find this chapter a useful summary
of some basic \Mtwo commands.

\section{A Curve in Affine Three-Space}

Our first example concerns geometric objects in
(complex) affine 3-space. We start by
setting up the ring of polynomial functions with rational coefficients.
<<<R = QQ[x,y,z]>>>
Various monomial orderings are available in \Mtwo; since we did not specify
one explicitly, the monomials in the ring ${\tt R}$ will be sorted in
graded reverse lexicographic order  \cite[\S I.2, Definition 6]{CLO1}.
We define an ideal generated by two polynomials
in this ring and assign it to the variable named
{\tt curve}.

<<<curve = ideal( x^4-y^5, x^3-y^7 )>>>
We compute the reduced Gr\"obner basis of our ideal:
<<<gb curve>>>
By inspecting leading terms (and using \cite[\S 9.3, Theorem 8]{CLO1}),
we see that our ideal {\tt curve} does indeed
define a one-dimensional affine variety. This can be tested directly
with the following commands in \Mtwo:
<<<dim curve>>>
<<<codim curve>>>
The {\it degree} of a curve in complex affine $3$-space is the
number of intersection points with a general plane. It coincides
with the degree  \cite[\S 6.4]{CLO2} of the projective closure
\cite[\S 8.4]{CLO1} of our curve, which we compute as follows:
<<<degree curve>>>
The Gr\"obner basis in {\tt o3} contains two polynomials which are not
irreducible: they contain a factor of $x^3$. This shows that our curve
is not irreducible over ${\bf Q}$. We first extract the components
which are transverse to the plane $x=0$:
<<<curve1 = saturate(curve,ideal(x))>>>
And next we extract the component which lies in the plane $x=0$:
<<<curve2 = saturate(curve,curve1)>>>
The second component is a multiple line. Hence our input ideal was not radical.
To test equality of ideals we use the command {\tt ==}\indexcmd{==} .
<<<curve == radical curve>>>
We now replace our curve by its first component:
<<<curve = curve1>>>
<<<degree curve>>>
The ideal of this curve is radical:
<<<curve == radical curve>>>
Notice that the variable ${\bf z}$ does not appear
among the generators of the ideal. Our curve consists of
$13$ straight lines (over {\bf C}) parallel to the {\tt z}-axis.

\section{Intersecting Our Curve With a Surface}

In this section we explore basic operations on ideals,
starting with those described in \cite[\S 4.3]{CLO1}.
Consider the following surface in affine $3$-space:
<<<surface = ideal( x^5 + y^5 + z^5 - 1)>>>
The union of the curve and the surface is represented by the
intersection of their ideals:
<<<theirunion = intersect(curve,surface)>>>
In this example this coincides with the product of the two ideals:
<<<curve*surface == theirunion>>>
The intersection of the curve and the surface is represented by the
sum of their ideals. We get a finite set of points:
<<<ourpoints = curve + surface>>>
<<<dim ourpoints>>>
The number of points is sixty five:
<<<degree ourpoints>>>
Each of the points is multiplicity-free:
<<<degree radical ourpoints>>>
The number of points coincides with the number of
monomials not in the initial ideal \cite[\S 2.2]{CLO2}.
These are called the {\it standard monomials}.
<<<staircase = ideal leadTerm ourpoints>>>
The {\tt basis} command can be used to list all the standard monomials
<<<T = R/staircase;>>>
<<<basis T>>>

The assignment of the quotient ring to the global variable {\tt T} had a side
effect: the variables {\tt x}, {\tt y}, and {\tt z} now have values in that
ring.
To bring the variables of {\tt R} to the fore again, we must say:
<<<use R;>>>
Every polynomial function on our 65 points can be written uniquely
as a linear combination of these standard monomials. This
representation can be computed using the normal form command {\tt \%}\indexcmd{\%}.

<<<anyOldPolynomial = y^5*x^5-x^9-y^8+y^3*x^5>>>
<<<anyOldPolynomial % ourpoints>>>
Clearly, the normal form is zero if and only the polynomial is in the ideal.
<<<anotherPolynomial = y^5*x^5-x^9-y^8+y^3*x^4>>>
<<<anotherPolynomial % ourpoints>>>

\section{Changing the Ambient Polynomial Ring}

During a \Mtwo session it sometimes becomes necessary to change the
ambient ring in which the computations takes place. Our original
ring, defined in {\tt i1}, is the polynomial ring in three variables
over the field  {\bf Q} of rational numbers
with the graded reverse lexicographic order. In this section
two modifications are made: first we replace the field of coefficients
by a finite field, and later we replace the  monomial order
by an elimination order.

An important operation in algebraic geometry is
the decomposition of algebraic varieties
into irreducible components \cite[\S 4.6]{CLO1}.
Algebraic algorithms for this purpose are based on the
{\it primary decomposition} of ideals \cite[\S 4.7]{CLO1}.
A future version of \Mtwo will have an implementation of
primary decomposition over any polynomial ring.
The current version of \Mtwo has a command
{\tt decompose} for finding all the minimal primes of an ideal,
but, as it stands, this works only over a finite field.

Let us change our coefficient field to the field with $101$ elements:
<<<R' = ZZ/101[x,y,z];>>>

We next move our ideal from the previous section into the new ring
(fortunately, none of the coefficients of its generators have 101 in the
denominator):

<<<ourpoints' = substitute(ourpoints,R')>>>
<<<decompose ourpoints'>>>
Oops, that didn't fit on the display, so let's print them out one per line.
<<<oo / print @@ print;>>>
If we just want to see the degrees of the irreducible components, then
we say:
<<<ooo / degree>>>
Note that the expressions ${\tt oo}$
and ${\tt ooo}$ refer to the previous and
prior-to-previous output lines respectively.

\medskip

Suppose we wish to compute the $x$-coordinates of our sixty five points.
Then we must use an elimination order, for instance, the
one described in \cite[\S 3.2, Exercise 6.a]{CLO1}.
We define a  new polynomial ring with the elimination order
for $\{y,z\} > \{x\}$ as follows:
<<<S = QQ[z,y,x, MonomialOrder => Eliminate 2]>>>
We move our ideal into the new ring,
<<<ourpoints'' = substitute(ourpoints,S)>>>
and we compute the reduced Gr\"obner basis in this new order:
<<<G = gens gb ourpoints''>>>
To compute the elimination ideal we use the following command:
<<<ideal selectInSubring(1,G)>>>

\section{Monomials Under the Staircase}

Invariants of an algebraic variety, such as its dimension
and degree, are computed from an initial monomial ideal.
This computation amounts to the combinatorial task
of analyzing the collection of standard monomials,
that is, the monomials under the staircase \cite[Chapter 9]{CLO1}.
In this section we demonstrate some basic operations on
monomial ideals in \Mtwo.

Let us create a non-trivial staircase in three dimensions
by taking the third power of the initial monomial from line {\tt i20}.
<<<M = staircase^3>>>
The number of current generators of this ideal equals
<<<numgens M>>>
To see all generators we can transpose the matrix of minimal generators:
<<<transpose gens M>>>
Note that this generating set is not minimal; see {\tt o48} below.
The number of standard monomials equals
<<<degree M>>>
To list all the standard monomials we first create the residue ring
<<<S = R/M>>>
and then we ask for a vector space basis of the residue ring:
<<<basis S>>>
Let us count how many standard monomials there are of a given degree.
The following table represents the Hilbert function
of the residue ring.
<<<tally apply(flatten entries basis(S),degree)>>>
Thus the largest degree of a standard monomial is nineteen,
and there are three standard monomials of that degree:
<<<basis(19,S)>>>
The most recently defined ring involving {\tt x}, {\tt y}, and {\tt z} was
{\tt S}, so all computations involving those variables are done in the
residue ring {\tt S}.
%% The current ring {\tt S} is the residue ring. All calculations
%% are done in {\tt S}.
For instance, we can also obtain the
standard monomials of  degree nineteen as follows:
<<<(x+y+z)^19>>>
An operation on ideals which will occur frequently throughout this
book is the computation of minimal free resolutions. This is done as follows:
<<<C = res M>>>
This shows that our ideal {\tt M} has sixteen minimal generators.
They are the entries in the leftmost matrix of the chain complex {\tt C}:
<<<C.dd_1>>>
This means that four of the twenty generators in {\tt o39} were redundant.
We construct the set consisting of the four redundant generators
as follows:
<<<set flatten entries gens M - set flatten entries C.dd_1>>>
Here {\tt flatten entries} turns the matrix ${\tt M}$ into a single list.
The command {\tt set} turns that list into a set, to which we
can apply the difference operation for sets.

Let us now take a look at the first syzygies
(or {\it minimal S-pairs} \cite[\S 2.9]{CLO1})
among  the sixteen minimal generators.
They correspond to the columns of the second matrix in our resolution {\tt C}:
<<<C.dd_2>>>
The first column represents the S-pair between the
first generator $x^3 y^6 $ and the third generator $x^2 y^9$.
It is natural to form the {\it S-pair graph} with $16$ vertices and
$27$ edges represented by  this matrix. According to the
general theory described in \cite{MS}, this is a planar graph
with $12$ regions. The regions correspond to the $12$ second syzygies,
that is, to the columns of the matrix
<<<C.dd_3>>>
But we are getting ahead of ourselves. Homological algebra and resolutions
will be covered in the next chapter, and monomial ideals
will appear in the chapter of Ho\c{s}ten and Smith.
Let us return to Cox, Little and O'Shea \cite{CLO2}.

\section{Pennies, Nickels, Dimes and Quarters}

We now come to an application of Gr\"obner bases which appears in
\cite[Section 8.1]{CLO2}: {\sl Integer Programming}. This is the problem of
 minimizing a linear objective function over the set of non-negative
integer solutions of a system of linear equations.  We demonstrate
some techniques for doing this in \Mtwo. Along the way, we learn about
multigraded polynomial rings and how to compute
Gr\"obner bases with respect to monomial orders defined by weights.
Our running example is the linear system defined  by the matrix:
<<<A = {{1, 1, 1, 1},
     {1, 5,10,25}}>>>
For the algebraic study of integer programming problems, a good starting
point is to work in a multigraded polynomial ring, here in four variables:
<<<R = QQ[p,n,d,q, Degrees => transpose A]>>>
The degree of each variable is the corresponding column vector of the matrix
Each variable represents one of the four coins in the U.S. currency system:
<<<degree d>>>
<<<degree q>>>
Each monomial represents a collection of coins. For instance, suppose
you own four  pennies, eight nickels, ten dimes, and three quarters:
<<<degree(p^4*n^8*d^10*q^3)>>>
Then you have a total of 25 coins worth two dollars and nineteen cents.
There are nine other possible ways of having 25 coins of the same value:
<<<h = basis({25,219}, R)>>>
For just counting the number of columns of this matrix
we can use the command
<<<rank source h>>>
How many ways can you make change for ten dollars using $100$ coins?
<<<rank source basis({100,1000}, R)>>>
A typical integer programming problem is this: among all 182 ways of
expressing ten dollars using 100 coins, which one uses the fewest dimes?
We set up the  Conti-Traverso algorithm \cite[\S 8.1]{CLO2} for
answering this question. We use the following ring with the lexicographic
order and with the variable order:
dimes (d) before pennies (p) before nickels (n) before quarters (q).
<<<S = QQ[x, y, d, p, n, q,
    MonomialOrder => Lex, MonomialSize => 16]>>>
The option {\tt MonomialSize} advises \Mtwo to use more space to store the
exponents of monomials, thereby avoiding a potential overflow.

We define an ideal with one generator for each column of the matrix A.
<<<I = ideal( p - x*y, n - x*y^5, d - x*y^10, q - x*y^25)>>>
The integer program is solved by normal form reduction with respect
to the following Gr\"obner basis consisting of binomials.
<<<transpose gens gb I>>>
We fix the quotient ring, so the reduction to normal form
will happen automatically.
<<<S' = S/I>>>
You need at least two dimes to express one dollar with ten coins.
<<<x^10 * y^100>>>
But you can express ten dollars with a hundred coins none of which is a dime.
<<<x^100 * y^1000>>>
The integer program is infeasible if and only if the normal form still
contains the variable $x$ or the variable $y$. For instance, you cannot
express ten dollars with less than forty coins:
<<<x^39 * y^1000>>>
We now introduce a new term order on the polynomial ring, defined
by assigning a weight to each variable. Specifically, we assign
weights for each of the coins. For instance,
let pennies have weight 5, nickels weight 7,
dimes weight 13 and quarters weight 17.
<<<weight = (5,7,13,17)>>>
We set up a new ring with the resulting weight term order, and work modulo
the same ideal as before in this new ring.
<<<T = QQ[x, y, p, n, d, q,
          Weights => {{1,1,0,0,0,0},{0,0,weight}},
          MonomialSize => 16]/
      (p - x*y, n - x*y^5, d - x*y^10, q - x*y^25);>>>
One dollar with ten coins:
<<<x^10 * y^100>>>
Ten dollars with one hundred coins:
<<<x^100 * y^1000>>>
Here is an optimal solution which involves all four types of coins:
<<<x^234 * y^5677>>>


\begin{quote}\textit{
	``To this end, we're going to give a crash course in category theory over the next few lectures; the door is
	over there.''
}\end{quote}
Remember that our general agenda is to match algebra and geometry; one way to express this idea is to take the
category of rings and identify it with some category of geometric objects. However, we're going to reverse the
arrows, and we'll get the category of affine schemes. These are some geometric spaces, with a contravariant functor
from affine schemes to rings given by taking the ring of functions and a functor in the opposite direction called
\(\Spec\).

One potential issue is that spaces may not have enough functions, e.g.\ \(\C\P^1\) as a complex manifold only has
constant functions; as such, we'll enlarge our category to a whole category of schemes, which will also have an
algebraic interpretation. Another weird aspect is that functions may take values in varying fields.

Schemes generalize geometry in three different directions: gluing spaces together to ensure we have enough
functions is topology, like making manifolds; functions having varying codomains is useful for arithmetic and
number theory; and allowing for rings with nilpotents feels a little like analysis.

Last time, we defined \(\MSpec(R)\) for a ring \(R\), the set of maximal ideals. It turns out that topology is not
sufficient to understand these spaces; for example, the class of \term{local rings} are those with only one maximal
ideal. There are many such rings, e.g.\ \(\C[x]/(x^n)\), whose maximal ideal is \((x)\). In short, \(\MSpec\)
doesn't see nilpotents.

To any ring \(R\), one can attach the category \(\mathsf{Mod}_R\), whose objects are \(R\)-modules and morphisms
are \(R\)-linear maps (those commuting with the action of \(R\)). This category is one of the more important things
one studies in algebra, and we also want to express them in terms of geometric objects that are related somehow to
\(\Spec R\). This should also help us understand the algebraic properties of \(R\)-modules too.
\subsection*{Crash Course in Categories.}
There's a lot of categorical notions in algebraic geometry; it does strike one as a painful way to start a course,
but hopefully we can get it out of our systems and move on to geometry knowing what we need. This corresponds to
chapters 1 and 2 in the book.

We've seen several examples of categories: sets, groups, rings, etc. The next example is a useful class of
categories.
\begin{defn}
A \term{poset} is a set \(\fS\) and a relation \(\le\) on \(\fS\) that is
\begin{itemize}
	\item \term{reflexive}, so \(x\le x\) for all \(x\in\fS\);
	\item \term{transitive}, so if \(x\le y\) and \(y\le z\), then \(x\le z\); and
	\item \term{antisymmetric}, so if \(x\le y\) and \(y\le x\), then \(x = y\).
\end{itemize}
\(\fS\) has the structure of a category: the objects are the elements of \(\fS\), and \(\Hom(x,y)\) is
\(\set{\pt}\) if \(x\le y\) and is empty otherwise.
\end{defn}
Transitivity means that we have composition, and reflexivity gives us identity maps.

This is an unusual example compared to things like ``the category of all (somethings),'' but is quite useful: a
functor from the poset \(\bullet\to\bullet\) to another category \(\fC\) is a choice of \(A,B\in\fC\) and a map
\(A\to B\); a functor from the poset \(\N\) is the same as an infinite sequence in \(\fC\), and a commutative
diagram is the same as a functor out of the category
\[\xymatrix{
	\bullet\ar[r]\ar[d] & \bullet\ar[d]\\
	\bullet\ar[r] & \bullet
}\]
into \(\fC\).
\begin{exm}
A particularly important example of this: if \(X\) is a topological space, then its open subsets form a poset under
inclusion. Hence, they form a category, called \(\Top(X)\). This category is important for sheaf theory, which we
will say more about later. For example, if \(A\) is an abelian group and \(U\subset X\) is open, then let
\(\sO_A(U)\) denote the abelian group of \(A\)-valued functions on \(U\) (for example, \(A\) might be \(\C\), so
\(\sO_A(U) = C^\infty(U)\)). If \(V\subset U\), then restriction of functions defines a map \(\res_U^V:
\sO_A(U)\to\sO_A(V)\). Since restriction obeys composition, then we've defined a functor \(\sO_A:
\Top(X)\op\to\mathsf{Ab}\) (or perhaps to \(\C\)-algebras, or another category); this is a \term{presheaf of
abelian groups} (or \(\C\)-algebras, etc.).
\end{exm}
To be precise, a \term{presheaf} on \(X\) is a functor out of \(\Top(X)\op\). This is a way of organizing functions
in a way that captures restriction; it will be very useful throughout this class.

Returning to category theory, one of its greatest uses is to capture structure through universal properties, rather
than using explicit details of a given category. We'll give a few universal properties here.
\begin{defn}
Let \(\fC\) be a category.
\begin{itemize}
	\item A \term{final} (or \term{terminal}) object in \(\fC\) is a \(*\in\fC\) such that for all \(X\in\fC\),
	there's a unique map \(X\to*\).
	\item An \term{initial} object is a \(*\in\fC\) such that for all \(X\in\fC\), there's a unique map \(*\to X\).
\end{itemize}
\end{defn}
This is not the last time we'll have dual constructions produced by reversing the arrows.
\begin{exm}
If \(\fC\) is a poset, then a terminal object is exactly a maximum element, and an initial object is a minimum
element. Thus, in particular, they do not necessarily exist.
\end{exm}
Nonetheless, if a final (or initial)  object exists, it's necessarily unique.
\begin{prop}
Let \(*\) and \(*'\) be terminal objects in \(\fC\); then, there's a unique isomorphism \(*\) to \(*'\).
\end{prop}
\begin{proof}
There's a unique map \(*\to *\), which therefore must be the identity, and there are unique maps \(*\to *'\) and
\(*'\to *\), so composing these, we must get the identity, so such an isomorphism exists, and it must be unique,
since there's only one map \(*\to *'\).
\end{proof}
By reversing the arrows, the same thing is true for initial objects. Thus, if such an object exists, it's unique,
so one often hears ``the'' initial or final object. These will be useful for constructing other universal
properties.
\begin{comp}{exm}{enumerate}
	\item In the category of sets, or in the category of topological spaces, the final object is a single point:
	everything maps to the point. The initial object is the empty set, since there's a unique (empty) map to any
	set or space.
	\item In \(\mathsf{Ab}\) or \(\mathsf{Vect}_k\) (abelian groups and vector spaces, respectively), \(0\) is both
	initial and terminal: the unique map is the zero map. An object that is initial and final is called a
	\term{zero object}; as in the case of sets, it may not exist.
	\item In the category of rings, \(0\) is terminal, but not initial (since a map out of \(0\) must send \(0 =
	1\) to \(0\) and \(1\)). \(\Z\) is initial, with the unique map determined by \(1\mapsto 1\).\footnote{That
	rings and ring homomorphisms are unital is important for this to be true.}
	\item Even though we don't really understand what an affine scheme is yet, we know that \(\Spec\Z\) has to be a
	terminal object, and \(\Spec 0\) has to be the initial object. Since we want this to be geometric, then
	\(\Spec\Z\) will play the role of a point. It might not look like a point, but categorically it behaves like
	one.
	\item The category of fields is also interesting: setting \(1 = 0\) isn't allowed, so there are neither initial
	nor terminal objects! If we specialize to fields of a given characteristic, then we get a unique map out of
	\(\Q\) or \(\F_p\), so the category of fields of a given characteristic is initial.
	\item The poset \(\Top(X)\) has \(\varnothing\) initial and \(X\) terminal: it has top and bottom objects.
\end{comp}
The fact that initial and terminal objects are unique means that if you characterize an object in terms of initial
or terminal objects, then you know they're unique as soon as they exist.
\begin{defn}
If \(R\) is a ring, we have the category \(\Alg_R\) of \(R\)-algebras (rings \(T\) with the extra structure of a
map \(R\to T\); morphisms must commute with this map). This is an example of something more general, called an
\term{undercategory}: if \(\fC\) is a category and \(X\in\fC\), then the undercategory \(X\downarrow \fC\) is the
category whose objects are data of \(Y\in\fC\) with \(\fC\)-morphisms \(a_Y: X\to Y\) and whose morphisms are
commutative diagrams
\[\xymatrix@C=0.5cm{
	Y_1\ar[rr]^\vp && Y_2\\
	& X.\ar[ul]^{a_{Y_1}}\ar[ur]_{a_{Y_2}}
}\]
In the same way, the \term{overcategory} \(X\uparrow\fC\) is the same idea, but with maps to \(X\) rather than from
\(X\) (e.g.\ spaces over a given space \(X\)).
\end{defn}
Thus, it's possible to concisely define \(\Alg_R = R\downarrow\mathsf{Ring}\). We will see other examples of this.
\begin{exm}[Localization]
Let \(R\) be a ring and \(S\subset R\) be a multiplicative subset. Then, the \term{localization at \(S\)} is
\(S^{-1}R = \set{r/s\mid r\in R, s\in S: r/s = r/s'\text{ when } s''(rs' - r's) = 0\text{ for some } s''\in S}\).
This is a construction we'll use a lot, so it will be useful to have a canonical characterization of them.

Now, let \(\fC\) be the category of \(R\)-algebras \(T\) with maps \((\vp_T:R\to T\) such that (and this is
a property, not structure) \(\vp_T(s)\) is invertible in \(T\) for all \(s\in S\).
\begin{ex}
Show that \(S^{-1}R\) is the initial object in \(\fC\).
\end{ex}
The naïve idea that localization is ``fractions in \(S\)'' is true if \(R\) is an integral domain, but if
we have zero divisors, the \(R\)-algebra structure map \(R\to S^{-1}R\) need not be injective. But the point is
that if \(T\) is an \(R\)-algebra where the elements of \(S\) become invertible, the map \(\vp_T\) factors through
\(S^{-1}R\); this means that \(S^{-1}R\) is the element of \(\fC\) that's ``closest to \(R\).'' However, you still
have to concretely build it to show that it exists; however, we know already that it's determined up to unique
isomorphism, so we say ``the'' localization.
\end{exm}
Another very fundamental language for making constructions is that of limits and colimits. It may seem a little
strange, but it's quite important.
\begin{defn}
Let \(I\) be a \term{small category} (so its objects form a set); in the context of limits, we will refer to it as
an \term{index category}. Then, a functor \(A:I\to\fC\) is called a \term{\(I\)-shaped} (or \term{\(I\)-indexed})
\term{diagram in \(\fC\)}.
\end{defn}
That is, if \(m:i\to j\) is a morphism in \(I\), then this diagram contains an arrow \(A(m): A_i\to A_j\).
\begin{defn}
Let \(A\) be an \(I\)-shaped diagram in \(\fC\). Then, a \term{cone} on \(A\) is the data of an object \(B\in\fC\)
and maps \(A_i\to B\) for every \(i\in I\) commuting with the morphisms in \(I\).
\begin{figure}[h!]
\[\xymatrix{
	A_1\ar[r]\ar[d]\ar@/_0.3cm/[drrr] & A_3\ar[drr]\\
	A_2\ar[ur]\dblarrow[r][f][g] &&& B
}\]
\caption{A cone on a diagram \(A\).}
\end{figure}
The cones on \(A\) form a category \(\mathsf{Cones}_A\), where the morphisms are maps \(B\to B'\) commuting with
all the maps in the cone.
\end{defn}
We can also take the category of ``co-cones,'' which are data of maps from \(B\) \emph{into} the diagram. This is
not quite the opposite category (since we want maps \(B\to B'\) commuting with the maps into the
diagram).\footnote{Some people switch the definitions of cones and co-cones, but since we're not going to use these
words very much, it doesn't matter all that much.}
\begin{comp}{defn}{itemize}
	\item The \term{colimit} \(\varinjlim_I A\) is the initial object in the category of cones of \(A\).
	\item The \term{limit} \(\varprojlim_I A\) is the terminal object in the category of co-cones of \(A\).
\end{comp}
As before, colimits and limits may or may not exist, but if they do, they're unique up to unique isomorphism.

Colimits act like a quotient, and it's easier to map out of them. Correspondingly, limits behave like a subobject,
and it's easier to map into them.
\begin{exm}[Products and Coproducts]
Let \(I = \bullet\ \bullet\) be a two-element discrete set (no non-identity arrows). Thus, an \(I\)-shaped diagram
is just a choice of two spaces \(A_1\) and \(A_2\), so a colimit \(C_1\) is the data of a unique map \(\vp_B\) for
each \(B\in\fC\) fitting into the following diagram.
\[\xymatrix@R=0.3cm{
	A_1\ar[dr]\ar@/^0.4cm/[drr]\\
	& C_1\ar[r]^{\vp_B} & B\\
	A_2\ar[ur]\ar@/_0.4cm/[urr]
}\]
This is called the \term{coproduct} of \(A_1\) and \(A_2\), denoted \(A_1\sqcup A_2\) or \(A_1\amalg A_2\).

Similarly, the limit of \(A\) is called the \term{product} of \(A_1\) and \(A_2\), is denoted \(A_1\times A_2\),
and fits into the diagram
\[\xymatrix@R=0.3cm{
	&& A_1\\
	B\ar[r]\ar[urr]\ar[drr] & A_1\times A_2\ar[ur]\ar[dr]\\
	&& A_2
}\]
In the same way, if \(I\) is a larger discrete set, we get coproducts and products of objects in \(\fC\) indexed by
\(I\), denoted \(\coprod_I A_i\) and \(\prod_I A_i\), respectively.

In the category of sets, the product is Cartesian product, and the coproduct is disjoint union. The same is true in
topological spaces.

In the category of groups, the product is once again Cartesian product, but the coproduct is the free product
(mapping out of it is the same as mapping out of the individual components, which is not true of the direct
product). As underlying sets, this is distinct from the coproduct of sets.

In linear categories, e.g.\ \(\mathsf{Ab}\), \(\mathsf{Mod}_R\), or \(\mathsf{Vect}_k\), \(V\oplus W\) is the
product and coproduct, and the same is true over all finite \(I\). However, this is \emph{not} true when \(I\) is
infinite: the coproduct is the direct sum, which takes finite sums of elements, and the product is the Cartesian
product, which takes arbitrary sums of elements. It's worth working out why this is, and how it works.
\end{exm}
Many of these categories are ``sets with structure,'' e.g.\ groups, vector spaces, topological spaces, and so on.
In these cases, there is a \term{forgetful functor} which forgets this structure: indeed, a group homomorphisms
(continuous map, linear map) is a map of sets too.\footnote{If this seems vague, that's all right; it's possible to
define and find forgetful functors more formally.}

There's a useful principle here: \emph{forgetful functors preserve limits}: if \(F\) is a forgetful functor, then
there is a canonical isomorphism \(F(\varprojlim A)\cong\varprojlim F(A)\). This is something that can be defined
more rigorously and proven. But one important corollary is that if you know what the limit looks like for sets,
it's the same in groups, rings, vector spaces, topological spaces, and so on. However, this is very false for
coproducts, e.g.\ the coproduct on groups is not the same as the one on sets.

This becomes a little cooler once we see limits that aren't just products.
\begin{exm}
Consider the diagram of rings
\[\xymatrix{
\dotsb\ar[r] & \Z/p^n\ar[r] & \dotsb\ar[r] & \Z/p^3\ar[r] & \Z/p^2\ar[r] & \Z/p,
}\]
where each map is given by modding out by \(p\). One can show that the limit exists, and it'll be the same as
the limit of the underlying sets, a sequence of compatible elements; this limit is called the \term{\(p\)-adic
integers}, denoted \(\Z_p\). More generally, the same thing works for \(\varprojlim R/I^n\) for an ideal \(I\subset
R\), and defines the \term{\(I\)-adic completion} \(\widehat R_I\), which we'll revisit, since it has useful
geometric meaning.
\end{exm}


\documentclass[simplex.tex]{subfiles}
% NO NEED TO INPUT PREAMBLES HERE
% packages are inherited; you can compile this on its own
\begin{document}
\subsection{Non-Parametric Shape Clustering}
%% Jan
We developed a prototype algorithm for 2-dimensional shape clustering, which
is invariant under affine transformations. This employs the procrustes
distance between two objects, which requires feature extraction to obtain
landmark points; see Fig.~\ref{fig:nonpar} for an example.
We intend to apply a variant of this method to analyze neural synapses,
since we have such datasets from our collaborators. However, to this particular
dataset, the preprocessing techniques may be highly sophisticated, and a new
metric to compare different objects may be necessary. Moreover, these shapes are
3-dimensional. We are currently working on this project
with our collaborators, extending our existing  techniques to this
particular dataset.

We also started working on a related, and more general, project.
We intend to develop non-parametric clustering algorithms with statistical
guarantees. We will use an energy-statistics based approach. Given
two datasets $X$ and $Y$, there is an energy function $\mathcal{E}(X,Y)$
test statistic which allows us to infer if $X$ and $Y$ have the same
distribution. Our results thus far suggest that this can be written
as a quadratic optimization problem
with quadratic constraints:
\begin{equation}
\max_{x,z\in \mathbb{R}^N} x^T \Delta z \qquad \mbox{s.t. $x_i^2=1$, $x+z = 0$}
\end{equation}
where $\Delta$ is a dissimilarity data matrix.
There is not enough literature on
this interesting problem, so this will very likely lead to new methods which
can have interesting applications, in particular to neuroscience datasets.

\begin{figure}[h!]
\begin{cframed}
\centering
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\textwidth]{../../figs/nonpar57.png}
\label{fig:nonpar57}
\caption{
  MNIST digits with extracted landmarks and alignment.
  }
\end{subfigure}
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\textwidth]{../../figs/nonPar357.png}
\label{fig:nonpar357}
\caption{
Classification error against the size of each
cluster (the three classes have the same number of points) is
shown in blue. The red line is standard
K-means with Euclidean distance for comparison.}
\end{subfigure}
\caption{
  MNIST handwritten digits and classification error results.
}
\label{fig:nonpar}
\end{cframed}
\end{figure}
\clearpage

%%% Feb
We have been mostly focused on developing
non-parametric clustering methods.
To this purpose, we are exploring ideas from energy statistics,
which is non-parametric,
robust, and rotational invariant, thus it incorporates the main ingredients
that we are looking for. The main difficulty is to formulate an
algorithm based on this, i.e. to identify the correct test
statistic, or to formulate it as a feasible optimization problem.
Consider $K$-Means clustering problem which is
$
\min_{\{\mathcal{C}_k\}}
\sum_{k=1}^K \sum_{x \in \mathcal{C}_k} \| x - \mu_k  \|^2,
$
where $\mathcal{C}_k$ is the $k$th cluster and $\mu_k$ the mean of its points.
We showed that this problem is equivalent to
\begin{equation}
\max_{G} \textnormal{Tr}\left( G^T K G \right) \qquad
\mbox{s.t. \, $G \ge 0$, $G^T G=I$, $G G^T e_1 = e_1$}.
\end{equation}
where $e_1 = (1,1,\dotsc,1)^T$.
This is a Quadratically Constrained Quadratic Problem (QCQP), which is usually
NP-hard.
Analogously, consider the energy function
$\mathcal{E}(F,G) = 2\mathbb{E} \| X - Y\| - \mathbb{E}\| X - X'\| - \mathbb{E}
\| Y - Y'\|$ between $X,X' \sim F$ and $Y,Y' \sim G$. We showed that this can
be written as $\mathcal{E}(A,B) = e_1^T \Delta e_1 $, where $\Delta$ is a
dissimilarity
matrix between the two sets of data points
$A \stackrel{iid}{\sim} F$
and $B \stackrel{iid}{\sim} G$.
Consequently, a simple two-class clustering problem would be
\begin{equation}
\max_{x,z\in \mathbb{R}^N} x^T \Delta z \qquad
\mbox{s.t. $x_i^2=1$, $x+z = 0$},
\end{equation}
which is also a QCQP problem. We are currently investigating this problem and
trying to generalize it correctly for more classes.
A simple check of the energy function as a test statistic
is shown in Fig.~\ref{fig:nonpar}.
Under the null $F = G$,
$T$ converges to a quadratic form of normally distributed random variables. This
seems to be the case in the first (blue) histogram, while it is definitely not
the case in the other (red and green) histograms.
For the blue histogram a single test gives
$T \approx 0.32$ (small), for the red
histogram $T \approx 4000 $ (large), and for the green
histogram $T \approx 105$ (large), with
only a few points. Thus, energy statistics based approach is
able to distinguish between
different distributions, even when the clusters have the same mean, which is a
property that $K$-Means cannot resolve.

%
\begin{figure}[h!]
\begin{cframed}
\centering
%\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\textwidth]{../../figs/energy_hists.pdf}
%\label{fig:nonpar57}
\caption{
Distribution of test statistic
$T\equiv\tfrac{n m }{n+m}\mathcal{E}(A,B)$
for an ensamble obtained from two distributions:
$A \stackrel{iid}{\sim} \mathcal{N}(\mu_A,\sigma_A^2)$ and
$B \stackrel{iid}{\sim} \mathcal{N}(\mu_B, \sigma_B^2)$, where
$|A|=n$ and $|B|=m$.
Blue histogram: $\mu_A = \mu_B = 0$ and $\sigma_A = \sigma_B = 1$;
Red histogram: $\mu_A = - \mu_B = 1$ and $\sigma_A = \sigma_B = 1$;
Green histogram: $\mu_A = \mu_B = 0$, $\sigma_A = 1$ and $\sigma_B = 1.5$.
}
\label{fig:nonpar}
\end{cframed}
\end{figure}
%
\clearpage

%% March
Energy statistics provides a nonparametric test for equality of distributions.
It is rotational invariant which is a highly desirable quality
for clustering. For a two-class problem, $X,X' \sim \mu$ and
$Y,Y' \sim \nu$, where $\mu,\nu$ are CDFs, it reads
\begin{equation}\label{eq:energy}
\mathcal{E}(X,Y) = 2\mathbb{E} \| X - Y \|
- \mathbb{E} \| X - X' \| - \mathbb{E} \| Y - Y'\|.
\end{equation}
We are developing a clustering framework based on \ref{eq:energy}.
Our criteria is that $\mathcal{E}$ should be a maximum when data points are
correctly classified.
%
It is possible to show that there is a map from the data space of $X,Y$
to the probability space of $\mu,\nu$ which is a Hilbert space whose
inner product  can be obtained from a kernel
function related to \ref{eq:energy},
$\langle \mu, \nu \rangle
= k(x,y)$.
This enables us to formulate our clustering problem as follows:
\begin{equation}\label{eq:opt}
\textnormal{max} \left\{ \textnormal{Tr} L^{1/2} Z^T K Z L^{1/2} \right\}
\quad
\textnormal{s.t.} \quad
\, Z_{ij}\in\{0,1\}, \sum_i Z_{ij} = N_j, \sum_j Z_{ij}=1,
Z^T Z = L^{-1}
\end{equation}
where $N_j$ is the number of elements in the $j$th cluster,
$L^{-1}=\textnormal{diag}(N_1,\dotsc,N_k)$, and $K$ is the Gram
matrix obtained
from the kernel. Let $\mathcal{X}$ be the pooled data matrix. If we
replace $K \to \mathcal{X}^T \mathcal{X}$ we recover the well-known
$k$-means problem, which in this formulation is related
to spectral clustering and normalized cuts. Problem
\ref{eq:opt} is
NP-hard and a numerical implementation is prohibitive even
for small data sets. We are investigating how to
solve \ref{eq:opt} in
a feasible way. As an evidence that \ref{eq:opt} is the correct optimization
problem, and more importantly, it illustrates the power behind our proposal,
in Fig.~\ref{fig:plots} we generate data and plot the objective in
\ref{eq:opt} versus $n$, where $n$ is the number of points randomly shuffled
from one class to the other. Therefore, for $n=0$ the function must be a
maximum. We do this for the kernel related to \ref{eq:energy} (blue dots)
and compare with the kernel related to $k$-means (red dots).
Clearly, \ref{eq:opt} based on \ref{eq:energy} is able to distinguish
between different cluster even for complex data sets that are
not linearly separable. Moreover, in our formulation
there are no free-parameters in the kernel.

\begin{figure}[h!]
\begin{cframed}
\centering
\begin{minipage}{0.49\textwidth}
\includegraphics[scale=.37]{../../figs/plot1.pdf}
\includegraphics[scale=.37]{../../figs/plot2.pdf}
\end{minipage}
\begin{minipage}{0.49\textwidth}
\includegraphics[scale=.37]{../../figs/plot3.pdf}
\includegraphics[scale=.37]{../../figs/plot4.pdf}
\end{minipage}
\caption{
Two dimensional datasets and the objective function
in \ref{eq:opt} as a function of $n$, where $n$ is the number of shuffled
points from its correct class to the wrong class. Blue dots
are for \ref{eq:energy} and red dots for $k$-means. A good function must
be monotonically decreasing. We can clearly see that \ref{eq:energy} is
way more powerful than $k$-means.
}
\label{fig:plots}
\end{cframed}
\end{figure}
\clearpage
\end{document}


\section{Control Panel} \label{sec:cp}
Before we dive in an start modelling the planet, let us first set up a control panel that will influence how the model will behave and effectively decides what type of planet we model.

\subsection{The Beginning}
In the beginning there was nothing, and then there was "Hello World!" Or at least that is how many projects start. Why? you might ask, which is a perfectly valid question. In Computer Science,
"Hello World!" is very simple code that we use to test whether all the tools we need to get coding works. This checks whether the computer compiles the code and is able to execute it and whether
the code editor (IDE, Integrated Development Environment) starts the right processes to get the code compiled and executed. Oh right we were talking about CLAuDE, ahem.

Every project must have its beginning. And with CLAuDE I made the decision to start explaining the Control Panel first. This is to get you familiar with notation and to lay down some basics. To
do that we start with the fixed part of the Control Panel, the physical constants. Many things vary from planet to planet, how much radiation they receive from their star, how strong their
gravity is, how fast they spin around their axis and many many more. What does not change are the physical constants, well because they are constant. The Stefan-Boltzmann constant for instance
does not change. Whether you are on Earth, in space or on Jupiter, the value of the Stefan-Boltzmann constant will remain the same.

The Stefan-Boltzmann constant is denoted by $\sigma$ and has a value of $5.670373 \cdot 10^-8$ (\si{Wm^{-2}K^{-4}}) \cite{stefan-boltzmann}. The $\sigma$ is a greek letter called sigma. Greek
letters are often used in mathematics, as well as in physics or any other discipline that relies on maths (spoiler alert, quite a lot). Treat it like a normal letter in maths, representing a
number that you either do not know yet or is too long or cumbersome to write down every time. The Stefan-Boltzmann constant is denoted in scientific notation, a number followed by the order of
magnitude. It is denoted as a multiplication, because that is what you have to do to get the real number. An example: $4.3 \cdot 10^2 = 430$ and $4.3 \cdot 10^{-2} = 0.043$. The
letters behind the numbers are units, how we give meaning to the numbers. If I say that I am $1.67$ does not mean anything. Do I mean inches, centimeters, meters, miles? That is why we need units
as they give meaning to the number. they tell us whether the number is talking about speed, distance, time, energy and many other things. In this manual we will use SI units. Behind all the
letters you will find the following: [number]. This is a citation, a reference to an external source where you can check whether I can still read. If I pull a value out somewhere I will insert a
citation to show that I am not making these numbers up. This is what scientists use to back up their claims if they do not want to redo the work that others have done. I mean what is the point
of re-inventing the wheel if there is a tyre company next door? That is why scientists citate.

So with that out of the way, let us write down some constants. Why do I do this here? Because a lot of constants are used everywhere and I am too lazy to replicate them every time. If you see a
letter or symbol that is not explicitly explained, then it is most likely a constant that we discuss here in the control panel.

\subsection{Physical Constants}
As mentioned before, physical constants do not change based on where you are in the universe. Below you will find an overview of all the relevant constants together with their units. And a short
explanation where they are used or what they represent. To see them in action, consult the other sections of this manual, you will find them in equations all throughout this document.

\subsubsection{The Gas Constant} \label{sec:gas constant}
The Gas constant, $R = 8.3144621$ (\si{JK^{-1}mol^{-1}}) \cite{idealGas} is the constant used to relate the temperature of the gas to the pressure and the volume. One would expect this constant to be
different per gas, but under high enough temperatures and low enough pressure the gas constant is the same for all gases.

\subsubsection{The Specific Heat Capacity}
The specific heat capacity $c$ depicts how much energy is required to heat the object by one degree Kelvin per unit mass (\si{Jkg^{-1}K^{-1}}) \cite{specificHeat}. This varies per material
and is usually indicated by a subscript. The specific heat capacity for water for instance is $c_w = 4190$ \si{Jkg^{-1}K^{-1}}. Specific heat capacities also exist in the form of
\si{Jkg^{-1}K^{-1}}, \si{Jmol^{-1}K^{-1}} and \si{Jcm^{-3}K^{-1}} which you can use in various circumstances, depending on what information you have.

\subsubsection{Mole}
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 (\si{u})\cite{mole}. This is not a physical constant perse, but more like a unit (\si{mol}). Though it is still important enough to be added here for future reference. All other units are
way more intuitive and are assumed to be known.

\subsubsection{The Stefan-Boltzmann Constant}
The Stefan-Boltzmann constant, $\sigma = 5.670373 \cdot 10^-8$ \si{Wm^{-2}K^{-4})} \cite{stefan-boltzmann} is used in the Stefan-Boltzmann law (more on that in \autoref{sec:first thermolaw}).

\subsection{Planet Specific Variables}
The following set of variables vary per planet, that's why we call them variables since they vary. Makes sense right? We add them here as we will use them throughout the manual. The advantage
of that is quite significant. If you want to test things for a different planet, you only need to change the values in one place, instead of all places where you use it. If there is one thing
that we computer scientists hate is doing work, we like being lazy and defining things in one place means that we can be lazy if we need to change it. So we put in the extra work now, so we do
not have to do the extra work in the future. That's actually a quite accurate description of computer scientists, doing hard work so that they can be lazy in the future.

\subsubsection{The Passage of Time}
On Earth we have various indications of how much time has passed. While most of them remain the same throughout the universe, like seconds, minutes and hours, others vary throughout the universe,
like days, months and years. Here we specify how long the variable quantities of time are for the planet we want to consider as they are used in the code. This can be seen in
\autoref{alg:time constants}. Here a $\leftarrow$ indicates that we assign a value to the variable name before it, so that we can use the variable name in the code instead of the value, which
has the advantage I indicated before. // means that we start a comment, which is text that the code ignores and does not tell the cpu about. Not that the cpu would understand it, but that just
means less work for the computer. Yes computers are lazy too.

\begin{algorithm*}
    \caption{Definition of how much time it takes for a day and a year on a planet and how much time on the planet passes before we start another calculation run}
    \label{alg:time constants}
    \SetKwComment{Comment}{//}{}
    $day \leftarrow 60*60*24$ \Comment*[l]{Length of one day in seconds (\si{s})}
    $year \leftarrow 365*day$ \Comment*[l]{Length of one year in seconds (\si{s})}
    $\delta t \leftarrow 60 * 9$ \Comment*[l]{How much time is between each calculation run in seconds (\si{s})}
\end{algorithm*}

\subsubsection{The Planet Passport}
Each planet is different, so why should they all have the same gravity? Oh wait, they don't. Just as they are not all the same size, tilted as much and their atmospheres differ. So here we define
all the relevant variables that are unique to a planet, or well not necessarily unique but you get the idea. This can all be found in \autoref{alg:planet constants}.

\begin{algorithm}
    \caption{Defining the constants that are specific to a planet}
    \label{alg:planet constants}
    \SetKwComment{Comment}{//}{}
    $g \leftarrow 9.81$ \Comment*[l]{Magnitude of gravity on the planet in \si{ms^{-2}}}
    $\alpha \leftarrow -23.5$ \Comment*[l]{By how many degrees the planet is tilted with respect to the star's plane}
    $top \leftarrow 50*10^3$ \Comment*[l]{How high the top of the atmosphere is with respect to the planet surface in meters (\si{m})}
    $ins \leftarrow 1370$ \Comment*[l]{Amount of energy from the star that reaches the planet per unit area (\si{Jm^{-2}})}
    $\epsilon \leftarrow 0.75$ \Comment*[l]{Absorbtivity of the atmosphere, fraction of how much of the total energy is absorbed (unitless)}
    $r \leftarrow 6.4*10^6$ \Comment*[l]{The radius of the planet in meters (\si{m})}
    $p_z \leftarrow [100000, 95000, 90000, 80000, 70000, 60000, 50000, 55000, 40000, 5000, 30000, 25000, 20000, 15000, 10000, 7500, 5000, 2500, 1000, 500, 200, 100]$ \Comment*[l]{The pressure
    of the different levels (\si{Pa}) noted in an array}
\end{algorithm}

\subsubsection{Model Specific Parameters}
These parameters cannot be found out in the wild, they only exist within our model. They control things like the size of a cell on the latitude longitude grid (more on that in later sections),
how much time the model gets to spin up. We need the model to spin up in order to avoid 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 it for a bit. You will
see that the whole system oscilates wildly as it adjusts to the sudden changes and eventually it will stabilise. We define the amount of time it needs to stabilise as the spin up time. All
definitions can be found in \autoref{alg:model constants}. What the $adv$ boolean does is enabling or disabling advection, a process described in <section> which does not work yet.

\begin{algorithm}
    \caption{Defining the paramters that only apply to the model}
    \label{alg:model constants}
    \SetKwComment{Comment}{//}{}
    $resolution \leftarrow 3$ \Comment*[l]{The amount of degrees on the latitude longitude grid that each cell has, with this setting each cell is 3 degrees latitude high and 3 degrees
    longitude wide}
    $nlevels \leftarrow 10$ \Comment*[l]{The amount of layers in the atmosphere}
    $\delta t_s \leftarrow 60*137$ \Comment*[l]{The time between calculation rounds during the spin up period in seconds (\si{s})}
    $t_s \leftarrow 5*day$ \Comment*[l]{How long we let the planet spin up in seconds (\si{s})}
    $adv \leftarrow \texttt{FALSE}$ \Comment*[l]{Whether we want to enable advection or not}
    $adv\_boun \leftarrow 8$ \Comment*[l]{How many cells away from the poles where we want to stop calculating the effects of advection}
    $C_a \leftarrow 287$ \Comment*[l]{Heat capacity of the atmosphere in \si{Jkg^{-1}K^{-1}}}
    $C_p \leftarrow 1 \cdot 10^6$ \Comment*[l]{Heat capacity of the planet in \si{Jkg^{-1}K^{-1}}}
    $\delta y \leftarrow \frac{2\pi r}{nlat}$ \Comment*[l]{How far apart the gridpoints in the y direction are (degrees latitude)}
    $\alpha_a \leftarrow 2 \cdot 10^{-5}$ \Comment*[l]{The diffusivity constant for the atmosphere}
    $\alpha_p \leftarrow 1.5 \cdot 10^{-6}$ \Comment*[l]{The diffusivity constant for the planet surface}
    $smooth_t \leftarrow 0.9$ \Comment*[l]{the smoothing parameter for the temperature}
    $smooth_u \leftarrow 0.8$ \Comment*[l]{The smoothing parameter for the $u$ component of the velocity}
    $smooth_v \leftarrow 0.8$ \Comment*[l]{The smoothing parameter for the $v$ component of the velocity}
    $smooth_w \leftarrow 0.3$ \Comment*[l]{The smoothing parameter for the $w$ component of the velocity}
    $smooth_{vert} \leftarrow 0.3$ \Comment*[l]{The smoothing parameter for the vertical part of the velocities}
    $count \leftarrow 0$ \;
    \For{$j \in [0, top]$}{
        $heights[j] \leftarrow count$ \Comment*[l]{The height of a layer (\si{m})}
        $count \leftarrow count + \frac{top}{nlevels}$ \;
    }

    \For{$i \in [0, nlat]$}{
        $\delta x[i] \leftarrow \delta y\cos(lat[i]\frac{\pi}{180})$ \Comment*[l]{How far apart the gridpoints in the x direction are (degrees longitude)}
    }

    \For{$k \in [0, nlevels - 1]$}{
        $\delta z[k] \leftarrow p_z[k + 1] - p_z[k]$ \Comment*[l]{How far apart the gridpoints in the z direction are (\si{Pa})}
    }

    $\Pi \leftarrow$ Array with the same amount of elements as $p_z$ \Comment*[l]{Dimensionless pressure that is used to easily convert from potential temperature to absolute temperature and
    vice versa}
    $\kappa \leftarrow \frac{R}{C_p}$ \;
    \For{$i \leftarrow 0$ \KwTo $\Pi.length$}{
        $\Pi[i] \leftarrow C_p(\frac{p_z[i]}{p_z[0]})^{\kappa}$ \;
    }
    $poleLowerLatLimit \leftarrow 4$ \Comment*[l]{Polar Plane Approximation Lower Grid Limit}
    $poleHigherLatLimit \leftarrow 4$ \Comment*[l]{Polar Plane Approximation Upper Grid Limit}
\end{algorithm}


\chapter{Local Outlier Factor} \label{LOF} \label{Local_Outlier_Factor}
It is one of the density based outlier detection methods \citep{Silvia}. The idea of the Local Outlier Factor (LOF) method is to assign a probability for each object/observation of being an outlier \citep{Markus}. The outlier factor is local because it takes into account only the neighbors of each observation, therefore, it doesn't require comparing observations with the global data distribution \citep{LOF}. Figure ~\ref{LOF-MD2} shows the basic idea of LOF of a point with densities of its neighbors.
 \begin{figure}[!h]
 \centering
 \includegraphics[width=0.35\textwidth]{images/OF-MD2-grayscale}
 \caption{Point A has a much lower density than its neighbors \citep{LOF}.}
 \label{LOF-MD2}
 \end{figure}

The LOF of an observation is based on one parameter $k$, which is the number of nearest neighbors that were considered to the observation \citep{Markus}. Developing the LOF method requires some notations and definitions which will be described in this section. Consider $p,q,o$ as observations in the dataset; the distance between observation $p$ and $q$ is given by $d(p,q)$. Whereas $C$ denote a cluster and $D$ is a dataset, then $d(p,C)=\min\{d(p,q),q\in C\}$ \citep{Knorr}.

\begin{defn}[Global Outlier in dataset $D$] \label{LOF1} An observation $p$ in a dataset $D$ is a Distance-based$(pct,k)$-outlier if at least a percentage ($pct$) of the observations in $D$ lies larger than distance $k$ from $p$ \citep{Markus}.
\end{defn}
Definition~\ref{LOF1} captures only some of the outliers (global outliers) which are far from the clusters of the dataset. However, in some datasets the interest lies in the observations that are outliers relative to their local neighbourhoods with respect to neighbourhood densities (local outliers). LOF method use a previous concepts to assign degrees to observations of being outlying, unlike other algorithms that use a binary property, therefore, the observation is either normal point or an outlier.
\begin{defn}[$k$-Distance of an object $p$] \label{LOF2} For any positive $k$, $k-distance(p)$ is defined as the distance $d(p,o)$ between $p$ and an object $o \in D$ such that:
\begin{align}
\label{a}
\begin{gathered}
\text{for at least $k$ objects } o' \in D \setminus \{p\} \text{ it holds that } d(p,o') \leq d(p,o), \\ \text{for at least $k-1$ objects } o' \in D \setminus \{p\} \text{ it holds that } d(p,o') < d(p,o).
\end{gathered}
\end{align}
In this step the distance that is the farthest to $k$ observations that near to $p$ is defined \citep{Markus}.
\end{defn}

\begin{defn}[$k$-Distance Neighbourhood of an object $p$] \label{LOF3} The $k$-distance neighbourhood is the $k$-distance of $p$ neighbors \citep{Markus}.
\begin{align}
\label{b}
N_{k}(p) = \lbrace q \in D \ \{p\} | d(p,q) \leq k-distance(p) \rbrace
\end{align}
\end{defn}

\begin{defn}[Reachability Distance of an Object $p$ with respect to object $o$] \label{LOF4} The reachability distance is the distance between $p$ and $o$ but at least the $k$-distance($o$) \citep{LOF}.
\begin{align}
\label{c}
reach-dist_k(p,o) = \max \lbrace k-distance(o),d(p,o) \rbrace
\end{align}
The reachability distance step~\ref{LOF4} is used to smooth statistical fluctuations \citep{Markus}; and it is a replacement for Euclidean distance. If the Euclidean distance between the two points is very small, the following steps will give a bias ratio of distance. Therefore, the LOF algorithm uses reachability distance instead \citep{Mathew}.
\end{defn}

\begin{defn}[Local Reachability Density of an object $p$] \label{LOF5} It is an inverse of the average reachability distance of an object $p$ from its neighbours of $p$ \citep{LOF}:
\begin{align}
\label{d}
lrd(p) = 1/ \bigg( \frac{\sum_{o \in N_{k}(p)} reach-dist_{k}(p,o)}{|N_{k}(p)|} \bigg)
\end{align}
In Figure~\ref{reach_dist} the reachability distance of the observation of $p_1$ is different than the reachability distance of observation $p_2$, because $p_2$ is not one of the $k$-nearest neighbour of $o$.
\begin{figure}[!h]
\centering
\includegraphics[width=0.5\textwidth]{images/reach_dist}
\caption{Reachability density ($k$=3) of two points $p_1$ and $p_2$ w.r.t. point $o$ \citep{LOF}.}
\label{reach_dist}
\end{figure}
\end{defn}
If the summation of reachability distances is 0, then the local density tends to infinity ($\infty$), which can only happens if there are two or more observations that share the same spatial coordinates \citep{Markus}.

\begin{defn}[Local Outlier Factor for an object $p$] \label{LOF6}
This step compares the local reachability density of an observation with the local reachability density of its neighbours, which captures the degree to which $p$ is called an outlier \citep{Hongyin}. The higher the value of LOF, the more likely it is to be an outlier, in other words, if density around $p$ is lower than density around $p$’s neighbors, then $p$ is an outlier \citep{Markus}.
\begin{align}
\label{e}
LOF_{k}(p) = \frac{\sum_{o \in N_{k}(p)}\frac{lrd_{k}(o)}{lrd_{k}(p)}} {|N_{k}(p)|}
\end{align}
\end{defn}

\section{Properties of LOF}
\subsection{LOF for Observations Deep in a Cluster} The LOF value for the observations that is in the cluster $C$ is approximately equal to 1, indicating that they shouldn't be labeled as outliers.
\begin{lem}
\label{lem1}
\citep{Markus} Let $C$ be a collection of observations.
Let the minimum reachability distance and the maximum reachability distance of observations in $C$ be $\min \lbrace reach-dist(p,q)|p,q \in C \rbrace$ and $\max \lbrace reach-dist(p,q)|p,q \in C \rbrace$ respectively. Let $\epsilon$ be defined as $min-reach-dist/max-reach-dist-1$. Then for all observations $p \in C$ it holds:
\begin{align}
1/(1+\epsilon) \leq LOF(p) \leq (1+\epsilon)
\end{align}
such that:
\begin{align}
\forall N_k(p):N_k(p) \in C \\
\forall N_k(q):N_k(q) \in C
\end{align}
\begin{comment}
(i) all the $k$-nearest neighbors q of p are in C, and
(ii) all the $k$-nearest neighbors o of q are also in C,
it holds that 1/(1 + ε) ≤ LOF(p) ≤ (1 + ε).
\end{comment}
\end{lem}
Consider a tight cluster $C$, the $\epsilon$ value in Lemma~\ref{lem1} can be quite small for deep observations $p$, thus it forces the LOF of $p$ to be close to 1 \citep{Markus}.
\subsection{Upper and Lower Bound on LOF}
Lemma~\ref{lem1} shows that the LOF value for the deep observations is approximately equal to 1. The observations outside and in the border of the cluster $C$; as well as upper and lower bound of the LOF value should be obtained by using Theorem~\ref{thm1} which requires the following terms \citep{Markus}:
\begin{itemize}
\item{Minimum reachability distance between $p$ and $k$-nearest neighbour of $p$ is}
\begin{align}
direct_{min}(p) = min \lbrace reach-dist(p,q) | q \in N_{k}(p) \rbrace
\end{align}
\item{Maximum reachability distance between $p$ and $k$-nearest neighbour of $p$ is}
\begin{align}
direct_{max}(p) = max \lbrace reach-dist(p,q) | q \in N_{k}(p) \rbrace
\end{align}
\item{Minimum reachability distance between $q$ and $k$-nearest neighbour of $q$ is}
\begin{align}
indirect_{min}(p) = min \lbrace reach-dist(q,o) | q \in N_{k}(p) \text{ and } o \in N_{k}(q) \rbrace
\end{align}
\item{Maximum reachability distance between $q$ and $k$-nearest neighbour of $q$ is}
\begin{align}
indirect_{max}(p) = max \lbrace reach-dist(q,o) | q \in N_{k}(p) \text{ and } o \in N_{k}(q) \rbrace
\end{align}
\end{itemize}
\begin{thm}\label{thm1}
Let $p$ be an observation from the dataset $D$, and $1 \leq k \leq |D|$ \citep{Markus}:
\begin{align}
\frac{direct_{min}(p)}{indirect_{max}(p)} \leq LOF(p) \leq \frac{direct_{max}(p)}{indirect_{min}(p)}
\end{align}
\end{thm}
Theorem~\ref{thm1} is a function of the reachability distances in $p$ that is in direct neighbourhood relative to those in $p$ that is in indirect neighbourhood. It could be applied to all the observations $p$ in the dataset $D$ and it can give tighter bounds for deep observations than Lemma~\ref{lem1} does, and this because Theorem~\ref{thm1} is based on $k$-nearest neighbours, unlike Lemma~\ref{lem1} which is based on minimum and maximum reachability distances \citep{Markus}.
\subsection{Bounds Tightness} Tightness of the bounds depends on the nature of the point under consideration. It defines the difference is between the LOF upper and lower bounds. If there is a fluctuation of the average reachability distance in the direct and indirect neighbourhoods that is small, then the bounds are tight. But if the observation $p$ has neighbours in multiple clusters having different densities, then the bounds are not tight \citep{Markus}.
\subsection{impact of $k$ on LOF} The value of LOF is affected by the parameter $k$, but $k$ has unpredictable impact on LOF, and this results in problems. However, there is a heuristic method determines bounds for $k$, that helps to avoid the previous weakness. The method proposed by Markus M. Breunig and others \citep{Markus}.

\section{LOF Algorithm}
The algorithm described below \citep{Malak} computes the LOF($p$) for all $p \in D$, Definition~\ref{LOF6} with details to local reachability density.
\begin{algorithm}[H]
  \begin{algorithmic}[1]
  \INPUT\tikzmark{a}
  \Statex $k$ \Comment The number of neighbours
  \Statex $D$  \Comment A set of observations
  \OUTPUT\tikzmark{a}
    \Statex $lof values$ \Comment This algorithm returns a vector with local density factors
   \ASSUME,\tikzmark{a}
   \Statex $k-dist(D,p)$ \Comment Return a matrix the $k$-dist neighbours~\ref{LOF2} and their $k$-distances~\ref{LOF3}
   \Statex $reach-dist_k(p)$  \Comment Return the local reachability density of each $p \in D$ ~\ref{LOF4}
    \Procedure{LOF}{$k,D$}
    \State $lof \gets NULL$
            \For{each point $p$}\Comment{Assign value to $k$-th nearest neighbours}
              \State $kNNeighbours \gets k-dist(D,k)$
              \State $lrd \gets reach-dist_k(KNNeighbours,k)$
             \For{each $p$ in kNNeigbours}
			             \State $\max \lbrace lof,templof \rbrace$ \Comment{Find the reachability distance between $p$ and $k$-neighbours ~\ref{LOF4}}
		                 \State $templof[i] \gets \sum ((lrd[o \in N(p)]) / lrd[i])/ |N(p)|$

		                 \Comment{This step computes the local reachability density of point $p$ ~\ref{LOF5}}
                   \EndFor\label{loffor}
            \EndFor\label{loffor}
    \State \textbf{return} $\top(lof)$\Comment{return the top values of the lof vector~\ref{LOF6}}
    \EndProcedure
  \end{algorithmic}
  \caption{Local Outlier Factor algorithm}\label{lof}
\end{algorithm}
% % % % % % % % % lof algorithm % % % % % % % %
     \begin{comment}
    \FOR{each point $p$}\Comment{We have the answer if r is 0}
      \State $kNNeighbours \gets k-dist(D,k)$
      \State $lrd \gets reach-dist_k(KNNeighbours,k)$

     \FOR{each $p$ in kNNeigbours}
               \State $templof[i] \gets \sum ((lrd[o \in N(p)]) / lrd[i])/ |N(p)|$
               \State $\max \lbrace lof,templof \rbrace$
           \EndFor\label{loffor}

    \EndFor\label{loffor}
     \end{comment}
% testing algorithms % % % % % % % % % % % % % %
\begin{comment}
\begin{algorithm}[H]
  \begin{algorithmic}[1]
  \INPUT\tikzmark{a}
  \Statex $a$ \Comment The a of the algo
  \Statex $b$  \Comment The b of the algo
  \Statex $c$  \Comment The c of the algo\tikzmark{b}
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
  \caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
% % % % % % % % % % % % % % % %
\begin{algorithm}
\caption{Expand $K$- Plex}{ }
\begin{algorithmic}[1]
\Statex
\INPUT\tikzmark{a}
\Statex $a$ \Comment The a of the algo
\Statex $b$  \Comment The b of the algo
\Statex $c$  \Comment The c of the algo\tikzmark{b}
\State $V \gets 0$
\end{algorithmic}
\end{algorithm}
\end{comment}