\section{Surface Area}{}{}\label{sec:Surface Area}
Another geometric question that arises naturally is: ``What is the
surface area of a volume?'' For example, what is the surface area of a
sphere? More advanced techniques are required to approach this
question in general, but we can compute the areas of some volumes
generated by revolution.

As usual, the question is: How might we approximate the surface area?
For a surface obtained by rotating a curve around an axis, we can take
a polygonal approximation to the curve, as in the last section, and
rotate it around the same axis. This gives a surface composed of many
``truncated cones''; a truncated cone is called a \dfont{frustum} of a cone.
Figure~\ref{fig:approximating surface area} illustrates this approximation.

\figure[H]
%\texonly
\vbox{\centerline{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <0.25truecm,0.25truecm>
\setplotarea x from 0 to 30, y from -15 to 15
\put {\hbox{\epsfxsize6cm\epsfbox{images/surface_area_approx.eps}}} at 30 0
\put {\hbox{\epsfxsize6cm\epsfbox{images/surface_area.eps}}} at 0 0
\endpicture}}
\caption{\label{fig:approximating surface area}
Approximating a surface (left) by portions of cones (right).}
%\endcaption
%\endtexonly
%\figrdef{fig:approximating surface area}
%\htmlfigure{Integration_applications-surface_area_approximation.html}
%\htmlonly
%\begincaption
%Approximating a surface (left) by portions of cones (right).
%You can download the Sage
%worksheet
%for this plot and upload it to your own sage account.
%\endcaption
%\endhtmlonly
\endfigure

So we need to be able to compute the area of a frustum of a cone.
Since the frustum can be formed by removing a small cone from the top
of a larger one, we can compute the desired area if we know the
surface area of a cone.
Suppose a right circular cone has base radius $r$ and slant height
$h$. If we cut the cone from the vertex to the base circle and
flatten it out, we obtain a sector of a circle with radius $h$ and arc
length $2\pi r$, as in Figure~\ref{fig:area of cone}. The angle at
the center, in radians, is then $2\pi r/h$, and the area of the cone
is equal to the area of the sector of the circle. Let $A$ be the area
of the sector; since the area of the entire circle is $\ds \pi h^2$, we
have
$${A\over\pi h^2}={2\pi r/h\over 2\pi}$$
$$A = \pi r h.$$

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\sevenpoint
\setcoordinatesystem units <1truecm,1truecm> point at 0 0
\setplotarea x from -1.5 to 1.5, y from -1 to 5.3
\ellipticalarc  axes ratio 3:1  180 degrees from -1.5 0 center at 0 0
\plot 1.5 0 0 5 -1.5 0 /
\putrule from -1.5 0 to 0 0
\put {$r$} [t] <0pt,-3pt> at -0.75 0
\put {$h$} [bl] <2pt,2pt> at 0.75 2.5
\setdashes
\ellipticalarc  axes ratio 3:1  180 degrees from 1.5 0 center at 0 0
\setsolid
\setcoordinatesystem units <1truecm,1truecm> point at -4 0
\circulararc 104 degrees from 5.22 0 center at 0 0
\circulararc 104 degrees from 1 0 center at 0 0
\plot 5.22 0 0 0 -1.26 5.065 /
\put {$h$} [t] <0pt,-3pt> at 2.61 0
\put {$2\pi r$} [bl] <2pt,2pt> at 3.69 3.69
\put {$2\pi r/h$} [bl] <2pt,2pt> at 0.71 0.71
\endpicture}}
%\endtexonly
%\figrdef{fig:area of cone}
%\htmlfigure{Integration_applications-area_of_cone.html}
\caption{\label{fig:area of cone}
The area of a cone.}
%\endcaption
\endfigure

Now suppose we have a frustum of a cone with slant height $h$ and
radii $\ds r_0$ and $\ds r_1$, as in Figure~\xrefn{fig:frustum}. The area of
the entire cone is $\ds \pi r_1(h_0+h)$, and the area of the small cone is
$\ds \pi r_0 h_0$; thus, the area of the frustum is $\ds \pi r_1(h_0+h)-\pi
r_0 h_0=\pi((r_1-r_0)h_0+r_1h)$. By similar triangles,
$${h_0\over r_0}={h_0+h\over r_1}.$$
With a bit of algebra this becomes $\ds (r_1-r_0)h_0= r_0h$; substitution
into the area gives
$$
  \pi((r_1-r_0)h_0+r_1h)=\pi(r_0h+r_1h)=\pi h(r_0+r_1)=2\pi
  {r_0+r_1\over2} h = 2\pi r h.
$$
The final form is particularly easy to remember, with $r$ equal to the
average of $\ds r_0$ and $\ds r_1$, as it is also the formula for the area of
a cylinder. (Think of a cylinder of radius $r$ and height $h$ as the
frustum of a cone of infinite height.)

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\sevenpoint
\setcoordinatesystem units <1truecm,1truecm> point at 0 0
\setplotarea x from -1.5 to 1.5, y from -1 to 5
\ellipticalarc  axes ratio 3:1  180 degrees from -1.5 0 center at 0 0
\plot 1.5 0 0 5 -1.5 0 /
\putrule from -1.5 0 to 0 0
\put {$r_1$} [t] <0pt,-3pt> at -0.75 0
\put {$h_0$} [bl] <2pt,2pt> at 0.45 3.5
\ellipticalarc  axes ratio 3:1  180 degrees from -0.9 2 center at 0 2
\putrule from -0.9 2 to 0 2
\put {$r_0$} [b] <0pt,3pt> at -0.45 2
\put {$h$} [bl] <2pt,2pt> at 1.2 1
\setdashes
\ellipticalarc  axes ratio 3:1  180 degrees from 1.5 0 center at 0 0
\ellipticalarc  axes ratio 3:1  110 degrees from 0.9 2 center at 0 2
\endpicture}}
%\endtexonly
%\figrdef{fig:frustum}
%\htmlfigure{Integration_applications-area_of_frustum.html}
\caption
{\label{fig:frustum}
The area of a frustum.}
%\endcaption
\endfigure

Now we are ready to approximate the area of a surface of
revolution. On one subinterval, the situation is as shown in
Figure~\ref{fig:surface subinterval}. When the line joining two
points on the curve is rotated around the $x$-axis, it forms a frustum
of a cone. The area is
$$
  2\pi r h= 2\pi {f(x_i)+f(x_{i+1})\over2}
    \sqrt{1+(f'(t_i))^2}\,\Delta x.
$$
Here
$\ds \sqrt{1+(f'(t_i))^2}\,\Delta x$ is the length of the line segment,
as we found in the previous section. Assuming $f$ is a continuous
function, there must be some $\ds x_i^*$ in $\ds [x_i,x_{i+1}]$
such that
$\ds (f(x_i)+f(x_{i+1}))/2 = f(x_i^*)$, so
the approximation for the
surface area is
$$\sum_{i=0}^{n-1} 2\pi f(x_i^*)\sqrt{1+(f'(t_i))^2}\,\Delta x.$$
This is not quite the sort of sum we have seen before, as it contains
two different values in the interval $\ds [x_i,x_{i+1}]$, namely
$\ds x_i^*$ and $\ds t_i$. Nevertheless, using more advanced techniques
than we have available here, it turns out that
$$\lim_{n\to\infty}
\sum_{i=0}^{n-1} 2\pi f(x_i^*)\sqrt{1+(f'(t_i))^2}\,\Delta x=
\int_a^b 2\pi f(x)\sqrt{1+(f'(x))^2}\,dx$$
is the surface area we seek. (Roughly speaking, this is because while
$\ds x_i^*$ and $\ds t_i$ are distinct values in $\ds[x_i,x_{i+1}]$,
they get closer and closer to each other as the length of the interval
shrinks.)

\figure[H]
%\texonly
\centerline{\vbox{\beginpicture
\normalgraphs
%\sevenpoint
\setcoordinatesystem units <1.3truecm,1.3truecm> point at 0 0
\setplotarea x from 0 to 5, y from 0 to 3
\axis left /
\axis bottom ticks short withvalues {$x_i$}
  {$x_i^*$} {$x_{i+1}$} / at 2.5 3 4 / /
\plot 2.5 1.5 4 2.5 /
\setquadratic
\plot 2.5 1.5 3 2 4 2.5 /
\put {$(x_i,f(x_i))$} [r] <-3pt,0pt> at 2.5 1.5
\put {$(x_{i+1},f(x_{i+1}))$} [l] <3pt,0pt> at 4 2.5
%\putrule from 3.25 0 to 3.25 2
\setdashes <2pt>
\putrule from 3 0 to 3 2
\putrule from 3 2 to 3.25 2
\setdashes
\putrule from 2.5 0 to 2.5 1.5
\putrule from 4 0 to 4 2.5
\endpicture}}
%\endtexonly
%\figrdef{fig:surface subinterval}
%\htmlfigure{Integration_applications-surface_area_one_interval.html}
\caption{\label{fig:surface subinterval}
One subinterval.}
\endfigure

\begin{example}{Surface Area of a Sphere}{Surface Area of a Sphere}\label{Surface Area of a Sphere}
Compute the surface area of a sphere of radius $r$.
\end{example}

\begin{solution}
The sphere can be obtained by rotating the graph of
  $\ds f(x)=\sqrt{r^2 - x^2}$ about the $x$-axis.
The derivative $f'$ is $\ds -x/\sqrt{r^2-x^2}$, so the surface area is
given by
\begin{eqnarray*}
A&=&2\pi \int_{-r }^r \sqrt{r^2 - x^2}\sqrt{1+{x^2\over r^2-x^2}}\,dx\cr
&=&2\pi \int_{-r }^r \sqrt{r^2 - x^2}\sqrt{r^2\over r^2-x^2}\,dx\cr
&=&2\pi \int_{-r }^r r\,dx=2\pi r\int_{-r }^r 1\,dx=4\pi r^2
\end{eqnarray*}\vskip-10pt
\end{solution}

If the curve is rotated around the $y$ axis, the formula is nearly
identical, because the length of the line segment we use to
approximate a portion of the curve doesn't change. Instead of the
radius $\ds f(x_i^*)$, we use the new radius $\ds \bar x_i=
(x_i+x_{i+1})/2$, and the surface area integral becomes
$$\int_a^b 2\pi x\sqrt{1+(f'(x))^2}\,dx.$$

\begin{example}{Surface Around y-axis}{Surface Around y-axis}\label{Surface Around y-axis}
Compute the area of the surface formed when $\ds f(x)
=x^2$ between $0$ and $2$ is rotated around the $y$-axis.
\end{example}

\begin{solution}
We compute $f'(x)= 2x$, and then
$$2\pi\int_0^2 x\sqrt{1+4x^2}\,dx={\pi\over6}(17^{3/2}-1),$$
by a simple substitution.
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:Surface Area}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
 Compute the area of the surface formed when $\ds f(x)=2\sqrt{1-x}$
between $-1$ and $0$ is rotated around the $x$-axis.
\begin{sol}
 $\ds 8\pi\sqrt3-{16\pi\sqrt2\over 3}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Compute the surface area of example~\ref{Surface Around y-axis} by rotating $\ds f(x)=\sqrt x$ around the $x$-axis.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Compute the area of the surface formed when
$\ds f(x)=x^3$ between $1$ and $3$ is rotated around the $x$-axis.
\begin{sol}
 $\ds {730\pi\sqrt{730}\over27}-{10\pi\sqrt{10}\over 27}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Compute the area of the surface formed when
$\ds f(x)=2 +\cosh (x)$ between $0$ and $1$ is rotated around the
  $x$-axis.
\begin{sol}
 $\ds \pi +2\pi e+ {1\over4}\pi e^2-{\pi\over4e^2}-{2\pi\over e}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the surface obtained by rotating the graph of $\ds
f(x)=1/x$, $x\geq 1$, around the $x$-axis. This surface is called
\dfont{Gabriel's horn} or \dfont{Toricelli's trumpet}.
%In exercise~\xrefn{exer:gabriels horn} in
%section~\xrefn{sec:improper integrals} we saw that Gabriel's horn has
%finite volume.
Show that Gabriel's horn has
infinite surface area.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the circle $\ds (x-2)^2+y^2 = 1$. Sketch the
surface obtained by rotating this circle about the $y$-axis. (The
surface is called a \dfont{torus}.) What is the surface area?
\begin{sol}
 $8\pi^2$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the ellipse with equation $\ds x^2/4+y^2 = 1$.
If the ellipse is rotated around the $x$-axis it forms
an \dfont{ellipsoid}.
Compute the surface area.
\begin{sol}
 $\ds 2\pi+{8\pi^2\over 3\sqrt{3}}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Generalize the preceding result: rotate the ellipse
given by $\ds x^2/a^2+y^2/b^2=1$ about the
$x$-axis and find the surface area of the resulting ellipsoid. You
should consider two cases, when $a>b$ and when $ab$: $\ds 2\pi b^2+$\hfill\break
\hbox{\hskip1cm}$\ds {2\pi a^2b\over\sqrt{a^2-b^2}}
  \arcsin(\sqrt{a^2-b^2}/a)$,\hfill\break
$a<b$: $\ds 2\pi b^2+ $\hfill\break
\hbox{\hskip1cm}$\ds {2\pi a^2b\over\sqrt{b^2-a^2}}
  \ln\left({b\over a}+{\sqrt{b^2-a^2}\over a}\right)$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\documentclass[11pt, oneside]{article}

\usepackage{../../shared/preamble}
\addbibresource{../../shared/references.bib}

\usepackage{../sets/sets}
\usepackage{groups}

\title{Groups}
\author{Arthur Ryman, {\tt arthur.ryman@gmail.com}}
\date{\today}

\begin{document}

\maketitle

\begin{abstract}
This article contains Z Notation type declarations for groups and some related objects.
It has been type checked by \fuzz.
\end{abstract}

\section{Introduction}

Groups are ubiquitous throughout mathematics and physics.
This article defines the basic algebraic objects related to groups and their homomorphisms.

\section{Binary Operations}

Let $\genT$ be a set. We refer to the members of $\genT$ as its {\em elements}.
A {\em binary operation} on $\genT$ is a function that maps pairs of elements to elements.

\subsection{\zcmd{binop}}

Let $\binop \genT$ denote the set of all binary operations on $\genT$.

\begin{zed}
\binop \genT == \genT \cross \genT \fun \genT
\end{zed}

\subsection{Infix Operator Symbols \zcmd{timesG}, \zcmd{mulG}, and \zcmd{addG}}

The result of applying a binary operation to the pair of elements $(x, y)$
is often denoted by an expression formed using an infix operator symbol,
e.g. $x \timesG y$, $x \mulG y$ or $x \addG y$.

\subsection{$MapPerservesOperation$}

Let $\genT$ and $\genU$ be sets and let $A$ and $B$ be binary operations on them.
Let $f$ be a function that maps $\genT$ to $\genU$.
The function $f$ is said to {\em preserve the operations} if it maps the product of elements to
the product of the mapped elements.

Let $MapPreservesOperation$ denote this situation.

\begin{schema}{MapPreservesOperation}[\genT, \genU]
f: \genT \fun \genU \\
A: \binop \genT \\
B: \binop \genU
\where
\LET (\_ \mulG \_) == A; (\_ \timesG \_) == B @ \\
\t1	\forall x, y: \genT @ \\
\t2		f(x \mulG y) = (f~x) \timesG (f~y)
\end{schema}

\subsection{\zcmd{homBinOp}}

A map that preserves operations is said to be an {\em operation homomorphism}.

Let $A$ and $B$ be binary operations.
Let $\homBinOp(A,B)$ denote the set of operation homomorphisms from $A$ to $B$.

\begin{gendef}[\genT, \genU]
\homBinOp: \binop \genT \cross \binop \genU  \fun \power (\genT \fun \genU)
\where
\homBinOp = (\lambda A: \binop \genT; B: \binop \genU @ \\
\t1	\{~ f: \genT \fun \genU | MapPreservesOperation[\genT, \genU] ~\})
\end{gendef}

\begin{remark}
The identity map is an operation homomorphism.
\end{remark}

\begin{remark}
The composition of two operation homomorphisms is an operation homomorphism.
\end{remark}

\section{Semigroups}

\subsection{$OperationIsAssociative$}

A binary operation is said to be {\em associative} if the result of applying it to three elements
is independent of the order in which it is applied pairwise.

Let $OperationIsAssociative$ denote this situation.

\begin{schema}{OperationIsAssociative}[\genT]
A: \binop \genT
\where
\LET (\_ \mulG \_) == A @ \\
\t1	\forall x, y, z: \genT @ \\
\t2		(x \mulG y) \mulG z = x \mulG (y \mulG z)
\end{schema}

\subsection{\zcmd{semigroup}}

Let $\semigroup \genT$ denote the set of all semigroups on the set of elements $\genT$.

\begin{zed}
\semigroup \genT == \{~ A: \binop \genT | OperationIsAssociative[\genT] ~\}
\end{zed}

\subsection{\zcmd{homSemigroup}}

A {\em semigroup homomorphism} from $A$ to $B$ is a homomorphism of the underlying binary operation.

Let $\homSemigroup(A, B)$ denote the set of all semigroup homomorphisms from $A$ to $B$.

\begin{gendef}[\genT, \genU]
\homSemigroup: \semigroup \genT \cross \semigroup \genU \fun \power (\genT \pfun \genU)
\where
\homSemigroup = \\
\t1	(\lambda A: \semigroup \genT; B: \semigroup \genU @ \homBinOp(A, B))
\end{gendef}

\begin{remark}
The identity mapping is a semigroup homomorphism.
\end{remark}

\begin{remark}
The composition of two semigroup homomorphisms is another semigroup homomorphism.
\end{remark}

\section{Monoids}

\subsection{$IdentityElement$}

Let $\genT$ be a set, let $A$ be a binary operation over $\genT$, and let $e$ be an element of $\genT$.
The element $e$ is said to be an {\em identity element} of $A$ if left and right
products with it leave all elements unchanged.

Let $IdentityElement$ denote this situation.

\begin{schema}{IdentityElement}[\genT]
A: \binop \genT \\
e: \genT
\where
\LET (\_ \mulG \_) == A @ \\
\t1	\forall x: \genT @ \\
\t2		e \mulG x = x = x \mulG e
\end{schema}

\subsection{$identity\_element$}

Let $identity\_element$ denote the relation that associates a binary operation one of its identity elements.

\begin{gendef}[\genT]
identity\_element: \binop \genT \rel \genT
\where
identity\_element = \\
\t1	\{~ IdentityElement[\genT] @ A \mapsto e ~\}
\end{gendef}

\begin{remark}
If a binary operation has an identity element then it is unique.
\end{remark}

\begin{proof}
Let $\mulG$ be a binary operation. Suppose $e$ and $e'$ are identity elements.
\begin{argue}
e \\
\t1	= e \mulG e'	& $e'$ is an identity element \\
\t1	= e'			& $e$ is an identity element
\end{argue}
\end{proof}

\begin{remark}
Since identity elements are unique if they exist, the relation from binary operations to identity elements is a partial function.

\begin{zed}
identity\_element \in \binop \setT \pfun \setT
\end{zed}

\end{remark}

\subsection{Identity Element Symbols \zcmd{zeroG}, and \zcmd{oneG}}

Identity elements are typically denoted by the symbols  $\zeroG$ or $\oneG$.

\subsection{\zcmd{monoid}}

Let $\genT$ be a set of elements.
A {\em monoid} over $\genT$ is a semigroup over $\genT$ that has an identity element.

Let $\monoid \genT$ denote the set of all monoids over $\genT$.

\begin{zed}
\monoid \genT == \{~ A: \semigroup \genT | \exists e: \genT @ IdentityElement[\genT] ~\}
\end{zed}

\subsection{$MapPreservesIdentity$}

Let $A$ and $B$ be monoids and let $f$ map the elements of $A$ to the elements of $B$.
The map $f$ is said to {\em preserve the identity element} if it maps the identity element of $A$
to the identity element of $B$.

Let $MapPreservesIdentity$ denote this situation.

\begin{schema}{MapPreservesIdentity}[\genT, \genU]
f: \genT \fun \genU \\
A: \monoid \genT \\
B: \monoid \genU
\where
\LET e == identity\_element~A; \\
\t1	e' == identity\_element~B @ \\
\t2		f~e = e'
\end{schema}

\subsubsection{\zcmd{homMonoid}}

A {\em monoid homomorphism} from $A$ to $B$ is a homomorphism $f$ of the underlying semigroups
that preserves identity.

Let $\homMonoid(A, B)$ denote the set of all monoid homomorphisms from $A$ to $B$.

\begin{gendef}[\genT, \genU]
\homMonoid: \monoid \genT \cross \monoid \genU \fun \power (\genT \fun \genU)
\where
\homMonoid = \\
\t1	(\lambda A: \monoid \genT; B: \monoid \genU @ \\
\t2		\{~ f: \homSemigroup(A, B) | \\
\t3			MapPreservesIdentity[\genT, \genU] ~\})
\end{gendef}

\begin{remark}
The identity mapping is a monoid homomorphism.
\end{remark}

\begin{remark}
The composition of two monoid homomorphisms is another monoid homomorphism.
\end{remark}

\section{Groups}

\subsection{$InverseOperation$ and Postfix Operator symbol \zcmd{invG}}

Let $\genT$ be a set of elements and let $A$ be a monoid on $\genT$.
A function $inv \in \genT \fun \genT$ is said to be an {\em inverse operation} if it maps each element
to an element whose product with it is the identity element.
Typically, the expression $x \invG$ is used to denote the inverse of $x$.

Let $InverseOperation$ denote this situation.

\begin{schema}{InverseOperation}[\genT]
A: \monoid \genT \\
inv: \genT \fun \genT
\where
\LET (\_ \mulG \_) == A; \\
\t1	\oneG == identity\_element~A; \\
\t1	(\_ \invG) == inv @ \\
\t2		\forall x: \genT @ \\
\t3			x \mulG x \invG = \oneG = x \invG  \mulG x
\end{schema}

\subsection{$inverse\_operation$}

Let $inverse\_operation$ denote the relation between monoids and their inverse operations.

\begin{gendef}[\genT]
inverse\_operation: \monoid \genT \rel \genT \fun \genT
\where
inverse\_operation = \\
\t1	\{~ InverseOperation[\genT] @ A \mapsto inv ~\}
\end{gendef}

\begin{remark}
If a monoid has an inverse operation then it is unique.
\end{remark}

\begin{proof}
Let $x$ be any element.
Suppose $x \invG$ and $x \daggerG$ are inverses of $x$.
\begin{argue}
x\daggerG \\
\t1	= x\daggerG \mulG \oneG				& $\oneG$ is an identity element \\
\t1	= x\daggerG \mulG (x \mulG x \invG)		& $x \invG$ is an inverse \\
\t1	= (x\daggerG \mulG x) \mulG x \invG		& associativity \\
\t1	= \oneG \mulG x \invG				& $x \daggerG$ is an inverse \\
\t1	= x \invG							& $\oneG$ is an identity element
\end{argue}
\end{proof}

\begin{remark}
Since if inverse operation exist they are unique, the relation between monoids and inverse operations
is a partial function.

\begin{zed}
inverse\_operation \in \monoid \setT \pfun \setT \fun \setT
\end{zed}

\end{remark}

\subsection{$\group$}

A {\em group} is a monoid that has an inverse operation.

Let $\genT$ be a set of elements.
Let $\group \genT$ denote the set of all groups over $\genT$.

\begin{zed}
\group \genT == \{~ A: \monoid \genT | \exists inv: \genT \fun \genT @ InverseOperation[\genT] ~\}
\end{zed}

\subsubsection{$MapPreservesInverse$}

Let $\genT$ and $\genU$ be sets of elements,
let $A$ and $B$ be groups over $\genT$ and $\genU$,
and let $f$ map $\genT$ to $\genU$.
The map $f$ is said to {\em preserve the inverses} if it maps the inverses of elements of $A$
to the inverses of the corresponding elements of $B$.

Let $MapPreservesInverse$ denote this situation.

\begin{schema}{MapPreservesInverse}[\genT, \genU]
f: \genT \fun \genU \\
A: \group \genT \\
B: \group \genU
\where
\LET (\_ \invG) == inverse\_operation~A; \\
\t1	(\_ \daggerG) == inverse\_operation~B @ \\
\t2		\forall x: \genT @ \\
\t3			f(x \invG) = (f~x) \daggerG
\end{schema}

\subsubsection{\zcmd{homGroup}}

Let $A$ and $B$ be groups.
A {\em group homomorphism} from $A$ to $B$ is a monoid homomorphism
from $A$ to $B$ that preserves inverses.

Let $\homGroup(A, B)$ denote the set of all group homomorphisms from $A$ to $B$.

\begin{gendef}[\genT, \genU]
\homGroup: \group \genT \cross \group \genU \fun \power (\genT \fun \genU)
\where
\homGroup = \\
\t1	(\lambda A: \group \genT; B: \group \genU @ \\
\t2		\{~ f: \homMonoid(A, B) | \\
\t3			MapPreservesInverse[\genT, \genU] ~\})
\end{gendef}

\begin{remark}
The identity mapping is a group homomorphism.
\end{remark}

\begin{remark}
The composition of two group homomorphisms is another group homomorphism.
\end{remark}

\subsection{$bij$}

Let $\genT$ be a set and let $bij[\genT]$ denote the set of a bijections $\genT \bij \genT$ from $\genT$ to itself.

\begin{gendef}[\genT]
	bij: \power (\genT \fun \genT)
\where
	bij = \genT \bij \genT
\end{gendef}

\begin{remark}
The composition of bijections is a bijection.

\begin{zed}
	\forall f, g: bij[\setT] @ \\
	\t1	f \circ g \in bij[\setT]
\end{zed}

\end{remark}

\begin{remark}
Composition is associative.

\begin{zed}
	\forall f, g, h: bij[\setT] @ \\
	\t1	f \circ (g \circ h) = (f \circ g) \circ h
\end{zed}

\end{remark}

\begin{remark}
The identity function $\id \setT$ acts as a left and right identity element under composition.

\begin{zed}
	\forall f: bij[\setT] @ \\
	\t1	 \id \setT \circ f = f = f \circ \id \setT
\end{zed}

\end{remark}

\begin{remark}
The inverse $f \inv$ of a bijection $f$ is its left and right inverse under composition.

\begin{zed}
	\forall f: bij[\setT] @ \\
	\t1	f \circ f \inv = \id \setT = f \inv \circ f
\end{zed}

\end{remark}

\subsection{$Bij$}

The preceding remarks show that set $bij[\genT]$ under the operation of composition has the structure of a group.
Let $Bij[\genT]$ denote this group.

\begin{gendef}[\genT]
	Bij: bij[\genT] \cross bij[\genT] \fun bij[\genT]
\where
	Bij = (\lambda f, g: bij[\genT] @ f \circ g)
\end{gendef}

\begin{example}
Let $\setT$ be any non-empty set.
The composition operation $Bij[\setT]$ is a group over the set of bijections $bij[\setT]$ from $\setT$ to $\setT$.

\begin{zed}
\setT \neq \emptyset \implies \\
\t1	Bij[\setT] \in \group bij[\setT]
\end{zed}

\end{example}

\section{Abelian Groups}

\subsection{OperationIsCommutative}

Let $\genT$ be a set of elements.
A binary operation $A$ over $\genT$ is said to be {\em commutative} when the product of two elements doesn't depend on
their order.

Let $OperationIsCommutative$ denote this situation.

\begin{schema}{OperationIsCommutative}[\genT]
A: \binop \genT
\where
\LET (\_ \mulG \_) == A @ \\
\t1	\forall x, y: \genT @ \\
\t2		x \mulG y = y \mulG x
\end{schema}

\subsection{\zcmd{abgroup}}

An {\em Abelian group} is a group in which the binary operation is commutative.
Let $\genT$ be a set of elements.

Let $\abgroup \genT$ denote the set of all Abelian groups over $\genT$.

\begin{zed}
\abgroup \genT == \{~ A: \group \genT | OperationIsCommutative[\genT] ~\}
\end{zed}

\subsection{\zcmd{addG}, \zcmd{zeroG}, and \zcmd{negG}}

Often in an Abelian group the binary operation is denoted as addition $x \addG y$,
the identity element as a zero $\zeroG$, and the inverse operation as negation $\negG x$.

\begin{example}
Addition over the integers is an Abelian group.

\begin{zed}
	(\_ + \_) \in \abgroup \num
\end{zed}

\end{example}

\printbibliography

\end{document}


\section{Interpolation: Interpolation}
Given a set of x, f(x) pairs (e.g., in a pair of arrays
xarr,yarr), and given an x, use interpolation to find y.

There are general purpose approaches, and also some
specialized to general polynomials and others specialized to
orthogonal approximatrixions
such as Chebyshev approximatrixions.  Here we consider only the
general cases.

The first step is to find the closest xa value as a start
point.  Then one can interpolationolate from that value and its
yarr pair.  We can do linear, quadtraic, nth order
polynomial interpolations.

\subsection*{interpolation\_linear}
Pretty straightforward.  I found it giving errors of 1--5\%
for a 10-value sin table, and 0.01\% error for a 100-value
sin table.

\subsection*{interpolation\_polynomial}
NR92 describes it on pg 109.  In the basic P form, the
result is a polynomial with n terms, of which all but 1
(e.g., k) go to zero.  So one then has:
\begin{equation}
     P(x)=\frac{(x-x_1)(x-x_2) \cdots (x-x_n)}
                 {((x_k-x_1)(x_k-x_2) \cdots (x_k-x_n))y_k}
\end{equation}
So we need to collect the numerator and we need to find the
right k.  Eqn 3.1.3 computes these on the fly, col by col.
According to several other books, polynomial interpolation
is numerically unstable.  But NR92's suggests improvements, based on
capturing differences (NR92's eqn 3.1.5):
\begin{eqnarray}
  D_{m+1,i} & = & \frac{(x_{i+m+1}-x)(C_{m,i+1}-D_{m,i})}{x_i-x_{i+m+1}}\\
  C_{m+1,i} & = & \frac{(x_{i}    -x)(C_{m,i+1}-D_{m,i})}{x_i-x_{i+m+1}}
\end{eqnarray}

Note that the recurrence
slides the whole equilateral triangle into a right triangle
form, with the {\em first} item in each col placed in the C,D
arrays at index 1.  Thus as we go along the cols, the number
of invalid values grows from the bottom of the arrays.

Once we have the full col worth of C and D values per the
recurrence, we select which one to use.  NR92 uses a cryptic
formula:
\begin{verbatim}
     if 2*ns < (n-m) then
       dy=c[ns+1]
     else
       dy=d[ns--]
     end
\end{verbatim}

Here, ns is the index aligned with the input x.  Note that
after using d we decrement ns.  That is, we move further
toward the top of the array.  But after using c we stay put.
We do momentarily peek further down the array as c[ns+1],
but ns stays the same.

The next thing to notice is that n-m is the total number of
new values in this col.  [From there to n we have old data
from previous cols.]  ns should be centered in this set of
values, which are offset from 1.  So ns should be
$\approx ((n-m)-1)/2$.  NR92 replaces the div with a mul by using
$2 \mbox{ns} \approx (n-m)$.

What if ns is too small?  That means we are too far
toward the top of the array.  That will be alright once we
go to the next col, because the array will slide up and ns
will be correctly centered again.  But if we are too far
down, we need to decrement ns.  Also, if we are dead on, we
still need to decrement in order to get centered for the
next col.

The remaining problem is to understand 3.1.5.  First we note
that $m$ means the old column, and $m+1$ means the new
column we are currently generating.  But the easier way to
do this is to think of the righthand side c and d as m-1 and
the lefthand side as m.  Then as we do the big loop for the
columns (m=1,2,...n-1), we just need to use the old c and d,
and create the new ones in situ.  With this understanding,
x[i+m+1] means the x[i+m] as seen for the new column.

Next, note that we have a common factor of $(C_{m,i+1}-
D{m,i})/(x_i-x_{i+m+1})$.  Before we compute that, we need to
check to see if the denominator is zero.

Next, we need to know how many of the cells are valid for a
given col.  We start with n cells, and lose one every time
we calc a new col.  NR92 recalcs this as n-m every loop.  I
have pulled it out as col\_n.  Mainly I did it to make it
more readable, but it just might be faster too.

Once we have the loops running m=1\dots n-1 and i=1\dots col\_n, we
need to access the right cells.  I made c and d easier by
writing them to 0..n arrays and just using the 1\dots n part.
xa is still in the 0\dots n-1 form, so I have to replace all {\tt i}
with {\tt i-1}.  I capture the resulting xa accesses as xi for
x[i] in 3.1.5 and xim1 for x[i+m+1] in 3.1.5.

To do offsetting (e.g., to do 4 point interpolation out of a much
larger table): The idea is to pass start and len with
defaults of 0.  If $len \ne 0$ then we know we need to do
partial access.  From there, we use symbolic start and end
values to keep track of indexes. C and D still live in their
1\dots n world, but we now have to offset the xa[] accesses by
the xn1 value (which is usually 0).

The relative error on even a 10-value sin table is
impressive (on the order of 1.0e-8).  It may be complex, but
it sure does its job.


\subsection{Cournot competition}

With competition, the elasticity of demand refers to the whole market, not just a single producer. Instead we have:

\(\epsilon = \dfrac{p}{Q}\dfrac{\delta Q}{\delta p}\)

\(Q=\sum_j q_j\)

We now get:

\(p[1+\dfrac{q}{Q}\dfrac{\delta Q}{\delta q}\dfrac{Q}{p}\dfrac{\delta p}{\delta Q}]=MC\)

\(p[1+\dfrac{\mu }{\epsilon }]=MC\)

Using the firm's size elasticity: \(\mu = \dfrac{q}{Q}\dfrac{\delta Q}{\delta q}\)

With monopoly this is:

\(\mu = 1\)


\section{The Gradient}
\noindent
If you are on a surface $f: \mathbb{R}^2 \to \mathbb{R}$, what direction $\langle \Delta x, \Delta y \rangle$ should you go to maximize the change of $f$?\\
We saw earlier that $\Delta z\approx \langle f_x, f_y \rangle \cdot \langle \Delta x, \Delta y \rangle$.
To maximize a dot product, $\langle \Delta x, \Delta y \rangle$ should be in the same direction as $\langle f_x, f_y \rangle$.
This directional vector is called the gradient: the direction of steepest ascent.\\
Notated mathematically,
\begin{equation*}
	\nabla f(x,y) = \langle f_x, f_y \rangle.
\end{equation*}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.5\textwidth]{./differentialMultivariableCalculus/gradient.png}
	\caption{A surface and its gradient vectors}
\end{figure}

\input{./differentialMultivariableCalculus/gradientProperties}
\input{./differentialMultivariableCalculus/linearApproximationsGradient}
\input{./differentialMultivariableCalculus/gradientCLevelCurves}


\section{The model}\label{sec:model}

Before investigating the model in more depth some elaboration of the notation used in this paper: $\Omega$ is used to denote covariance matrices, with individual elements of $\Omega$ denoted $\sigma^{2}_{i,j}$, and noting that the variances are the diagonal of the matrix, these being denoted = $\sigma^{2}_{i,i} = \sigma^{2}_{i}$. Moving on,  $\mu = (\mu_1, \mu_2, \cdots \mu_{k})$ is used for denoting a vector of $k$ expected returns. It should be noted that $\Omega$ is a $k\times k$ matrix, or rather the length $\mu$ should correspond to the size of the symmetric covariance matrix. The returns at time $t$ are denoted as $\rr^{(t)} = (r_1^{(t)}, r_2^{(t)}, \cdots, r_k^{(t)})$. In general the stochastic variables will be upper case, and the realization will be denoted in lower case. such that $\RR^{(t)}$ is a stochastic vector, and $\rr^{(t)}$ is vector of realized returns. Let $\bar{r}$ denote the risk free asset, and let $\ones$ denote a vector of 1's of length $k$). Sharpe ratio is denoted $sr$. Vectors will be denoted in bold when possible.

\subsection{CAPM}

As alluded to in the introduction, in CAPM it's assumed that the returns of stocks are drawn $i.i.d$ from an multivariate distribution with fixed mean $\mu$ and covariance matrix $\Omega$. Assuming that the returns are normally distributed the structural model driving the data generating process can be summed up into a single equation:

\begin{equation}
    \RR^{(t)} \sim N(\mu, \Omega)
\end{equation}

\subsection{Model formulation with structural breaks}

As alluded to in chapter \ref{sec:data}, the assumption, that a single covariance matrix and expected returns can represent the data generating process seems very unlikely. There are in general two approaches to this problem:

\begin{enumerate}
    \item Assuming the covariance matrix and expected returns steadily moves over time, something akin to a AR process.
    \item Assuming in each period with a certain probability a structural break will occur, and this will generate a vector of expected returns vector and a covariance matrix.
\end{enumerate}

In this paper the ladder option is investigated. This leads to the following structural model:

First a variable $b^{(t)}\in \{0,1\}$ is drawn which represent a structural break. If $b=1$ a structural break is assumed. The parameter $p$ in equation \ref{eq:structuralbreak} denotes the probability of a structural break.

\begin{equation}\label{eq:structuralbreak}
    b^{(t)} = bern(p)
\end{equation}

If $b = 1$ a new covariance matrix, and expected returns vector is drawn, where $d_{\mu}, d_{\Omega}$ are the distributions of the vector of expected returns  and covariance matrix. These distributions will be addressed later.

\begin{equation}
    \mu^{(t)} \sim d_{\mu} \qquad \Omega^{(t)} \sim d_{\Omega}
\end{equation}

If $b=0$ the covariance matrix and expected returns vector is equal to the previous:

\begin{equation}
    \mu^{(t)} = \mu^{(t-1)} \qquad \Omega^{(t)} = \Omega^{(t-1)}
\end{equation}

Lastly we draw the returns just as in the regular CAPM formulation, with the caveat $\mu$ and $\Omega$ contains a top-script $t$:

\begin{equation}
    \RR^{(t)} \sim N(\mu^{(t)}, \Omega^{(t)})
\end{equation}

So in any given period the tangency portfolio can be calculated, however this tangency portfolio will change each time a structural break have occurred. The only parameter that cannot be estimated looking to the real data is $p$ the an exogenous parameter to the model.

\subsection{Approach for stocking picking}

In this paper Sharpe ratio is used as criteria for stock picking. Sharpe ratio is a risk adjusted measure of the performance of a portfolio, defined as:

\begin{equation}
 sr = \frac{\E[R - \bar{r}]}{\std(R)}
\end{equation}

where we have used the risk free asset as benchmark. In this paper stock picking implies selecting a single stock. This corresponds to a portfolio with all weights at 0, except a weight of 1 for the stock chosen, the choice of stock would be the stock with highest Sharpe ratio.

Under the normal CAPM assumptions $\Omega$ and $\mu$ could be estimated using historical data. This is feasible in the normal CAPM formulation due to law of large numbers, that basically will ensure that as the number of observations increases, the parameter estimates will converge to the true value:

\begin{equation}
    (\hat{\mu}, \hat{\Omega}) \overset{d}{\rightarrow} (\mu, \Omega) \qquad \text{for } n \rightarrow \infty
\end{equation}

Having these summary statistics the entire period would imply that we could chose the single stock with highest Sharpe ratio for the entire period.

This is however not the case under the alternative model formulation, since a structural break can occur at any time which makes it impossible to estimate $\Omega$, $\mu$ by using the entire sample of historical data. A different approach is therefore taken in this paper.

Since we know the structural model underlying the problem, we are able to sample from it. Using this fact a generated dataset of arbitrary size can be made with returns and Sharpe ratios for individual stocks. We can the use this data set to train an algorithm, that maps a set of $k$ stock observations to a set of $k$ Sharpe ratios.

More formally we can denote this as:

\begin{equation}
    f_{\theta}: \R^{k} \mapsto \R^{k}
\end{equation}

Where $f_{\theta}$ is the algorithm used, that has a set of parameters $\theta$.

We can then establish a loss function $L$ such that:

\begin{equation}
    \hat{f_{\theta}} = \underset{\theta}{\argmin} \sum L(\mathbf{sr}^{(t)}, \hat{\mathbf{sr}}^{(t)})
\end{equation}

where $\hat{\mathbf{sr}}$ is the prediction of the weights in period $t$ returned by the  algorithm.

Finally when having found the algorithm that performs the best in the simulated data set, we can take it to the real data. This approach allows three things: 1) We can train data hungry algorithms. We have approximately 7000 observations in the real data, but some algorithms (the LSTM mentioned later), needs in excess of a million observations to converge. 2) We have latent variables. Using real data we can only make estimates of $\Omega$ and $\mu$, however, when we have simulated from the underlying structural causal model, we can find the true values at any given time in the data set, allowing us to calculate the actual Sharpe ratio for each stock. 3) It is not possible to overfit the data out-of-sample. Had we trained, and tuned the models in-sample, that is used real data for these generalize out-of-sample. This is because our stock picking algorithms might have captured noise in the data, and used that to get overly optimistic estimates of the performance of our algorithms.


% To be compiled by XeLaTeX, preferably under TeX Live.
% LaTeX source for ``Yanqi Lake Lectures on Algebra'' Part III.
% Copyright 2019  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
% https://creativecommons.org/licenses/by-nc/4.0/

% To be included
\chapter{From completions to dimensions}

The main references are \cite{Mat80,Eis95}.
\section{Completions}
Consider a ring $R$ together with a family of ideals $\mathcal{I} \neq \emptyset$, such that for any $I, J \in \mathcal{I}$ there exists $K \in \mathcal{I}$ with $K \subset I \cap J$. This turns $R$ into a topological ring, characterized by the property that $\mathcal{I}$ forms a local base of open neighborhoods of $0$. Recall that being a topological ring means that addition, multiplication and $x \mapsto -x$ are all continuous. By standard arguments, $R$ is Hausdorff if and only if $\{0\}$ is closed, if and only if $\bigcap_{I \in \mathcal{I}} I = \{0\}$.\index{topological ring}

To simplify matters, we assume that
\begin{compactitem}
	\item the family $\mathcal{I}$ is countable, so that the topological properties (accumulation points, etc.) are detected by convergence of \emph{sequences} as in the case of metric spaces;
	\item furthermore, we may arrange that $\mathcal{I} = \{I \supset J \supset K \supset \cdots \}$, in other words our topology comes from \emph{filtrations}.
\end{compactitem}
Without the countability assumption, the sequences will have to be replaced by \emph{filters}.

It makes sense to talk about topological $R$-modules for a topological ring $R$. By replacing filtration by ideals by filtration by $R$-submodules subject to the usual compatibility relation $F^i R \cdot F^j M \subset F^{i+j} M$, the recipe above applies to $R$-modules as well. Given $N \subset M$, the topology so obtained on $M$ passes to $M/N$ by taking the quotient topology, or equivalently the quotient filtration $(F^\bullet M+N)/N$. If the filtration in question is $I$-adic, where $I \subsetneq R$ is an ideal, we obtain the \emph{$I$-adic topology} on rings and modules.\index{topological ring!$I$-adic}

An $R$-module $M$ equipped with a topology as above is \emph{complete} if every Cauchy sequence $(x_n)_{n \geq 1}$ has a limit; a Cauchy sequence $(x_n)_{n \geq 1}$ is a sequence satisfying
\[ \forall I \in \mathcal{I}, \; \exists N \quad i,j \geq N \implies x_i - x_j \in I. \]
As in the familiar case of metric spaces, one has the \emph{completion} of $M$. It is actually a morphism $M \to \hat{M}$ with $\hat{M}$ complete Hausdorff, characterized by the following universal property:\index{completion}
\[
\begin{tikzcd}[row sep=tiny, column sep=tiny]
	M \arrow[rr, "\varphi:\; \text{cont. homo.}" inner sep=0.7em] & & L \\
	& & \scriptsize\text{complete Hausdorff}
\end{tikzcd} \quad \leadsto
\begin{tikzcd}
	M \arrow[rd, "\varphi"'] \arrow[r] & \hat{M} \arrow[dashed, d, "\exists! \hat{\varphi}"] \\
	& L
\end{tikzcd}\]

The uniqueness results immediately, and the formation of $M \mapsto \hat{M}$ is seen to be functorial in $M$. If $M$ is already complete Hausdorff, one may take $\hat{M} = M$. Certainly, the same applies to the ring $R$.

\begin{exercise}
	Suppose that $R$ is complete Hausdorff with respect to the $I$-adic topology, where $I$ is a proper ideal. Show that every element of the form $u+x$, $u \in R^\times$ and $x \in I$, is invertible.
\end{exercise}

From the algebraic perspective, the completion of a filtered $R$-module $M = F^0 M \supset F^1 M \supset \cdots$ can be constructed as the projective limit
\begin{align*}
	\hat{M} & := \varprojlim_{i \geq 1} M/F^i M \\
	& = \left\{ (x_i)_{i \geq 1} : i \leq j \implies x_i \equiv x_j \pmod{F^i M} \right\} \subset \prod_{i \geq 1} M/F^i M.
\end{align*}
The morphism $M \to \hat{M}$ is the diagonal map. The topology on $\hat{M}$ arises from the filtration
\[ F^i \hat{M} := \Ker\left[ p_i: \hat{M} \to M/F^i M \right] = \left\{ (x_n)_n \in \hat{M}: i \leq k \implies x_i=0 \right\}, \]
so that the preimage of $F^i \hat{M}$ in $M$ is precisely $F^i M$. In the case where $M=R$ and $F^i R$ are ideals, we obtain the complete Hausdorff ring $\hat{R}$, which is a subring of $\prod_{i \geq 1} R/F^i R$. Since the filtrations on $R$ and $M$ are assumed compatible, $\hat{M}$ is an $\hat{R}$-module.

\begin{example}
	Fix a prime number $p$. The completion of $\Z$ with respect to the ideal $p\Z$ is nothing but the ring $\Z_p$ of $p$-adic integers. Similarly, the completion of $\Bbbk[X]$ with respect to $(X)$ is isomorphic to the $\Bbbk$-algebra $\Bbbk\llbracket X\rrbracket$.
\end{example}

\begin{exercise}
	Describe the kernel of $M \to \hat{M}$ and show $M \hookrightarrow \hat{M}$ if and only if $M$ is Hausdorff.
\end{exercise}

\begin{exercise}
	Show that the topology of $\hat{M}$ is the restriction of the product topology of $\prod_i M/F^i M$, provided that each $M/F^i M$ is endowed with the discrete topology. Show that $\hat{M}$ is a closed subspace of $\prod_i M/F^i M$
\end{exercise}

\begin{lemma}\label{prop:quotient-completion}
	Let $M$ be a complete $R$-module with respect to some filtration $F^\bullet M$. For any submodule $N$, the quotient $M/N$ is also complete with respect to the quotient topology, or equivalently with respect to the quotient filtration $(F^\bullet M + N)/N$.
\end{lemma}
\begin{proof}
	Let $\bar{x}_n$ be a Cauchy sequence in $M/N$. Choose preimages $M \ni x_n \mapsto \bar{x}_n$ for all $n$. We have $\bar{x}_{n+1} - \bar{x}_n \in F^{i(n)}M + N$ where $\lim_{n \to \infty} i(n) = \infty$, therefore we can write $x_{n+1} - x_n = y_n + \delta_n$ where $y_n \in F^{i(n)} M$ and $\delta_n \in N$. We contend that $x'_n := x_1 + \sum_{i < n} y_i$ is a Cauchy sequence in $M$. Indeed, for any $i > j$ we have $x'_i - x'_j = \sum_{j \leq k < i} y_k$, which lies in $F^{\inf_k i(k)} M$. This implies $(x'_n)_n$ is a Cauchy sequence, hence has a limit $x \in M$. It is also clear that $x'_n \mapsto \bar{x}_n$. Hence $\bar{x}'_n$ has a limit $\bar{x} = x \bmod N$ in $M/N$.
\end{proof}

Let us turn to the exactness of completion. This should be understood in the broader framework of $\varprojlim$ of arbitrary projective systems. For simplicity, we only consider $I$-adic topologies on finitely generated modules over a Noetherian ring.

Observe that any homomorphism $\varphi: M \to N$ between $R$-modules is automatically $\mathfrak{a}$-adically continuous, for that $\varphi(\mathfrak{a}^n M) \subset \mathfrak{a}^n N$.

\begin{proposition}\label{prop:completion-exactness}
	Let $R$ be a Noetherian ring and $I \subsetneq R$ an ideal. Suppose $0 \to M' \to M \to M'' \to 0$ is an exact sequence of finitely generated $R$-modules, each term equipped with the $I$-adic topology. The completed sequence $0 \to \hat{M}' \to \hat{M} \to \hat{M''} \to 0$ is also exact.
\end{proposition}
Consequently, completion preserves the exactness of sequences formed by finitely generated $R$-modules. This is what makes completion so useful.
\begin{proof}
	We shall show
	\begin{compactenum}[(i)]
		\item the topology on $M'$ induced from $M$ is the same as the $I$-adic topology;
		\item the quotient topology on $M'' = M/M'$ is $I$-adic;
		\item the completion $\hat{M}'$ is naturally identified with the closure of the image of $M'$ in $\hat{M}$;
		\item $\hat{M}''$ is naturally identified with the quotient of $\hat{M}$ by $\hat{M}'$.
	\end{compactenum}

	(i) is a direct consequence of Artin--Rees Theorem \ref{prop:Artin-Rees}: for $n \gg 0$ we have
	\begin{gather}\label{eqn:AR-induced-top}
		I^n M' \subset M' \cap I^n M = I (M' \cap I^{n-1} M) \subset I^{n-1} M',
	\end{gather}
	and this suffices to identify the resulting topologies.

	(ii) is immediate. As for (iii), we may embed $M'$ into $M$ and work with the induced topology. Realize $\hat{M}$ as $\varprojlim_n M/I^n M$. As a topological module $\hat{M}'$ equals
	\begin{align*}
		\varprojlim_n \frac{M'}{M' \cap I^n M} & = \left\{ \hat{x} = (x_n)_n \in \hat{M} : \forall k, \; x_k \text{ comes from } M' \right\} \\
		& = \left\{ \hat{x} \in \hat{M}: \forall k \; \exists y \in M' \text{ s.t. } \; \hat{x} \in y + F^k \hat{M} \right\}
	\end{align*}
	which is readily seen to be the closure of the image of $M'$.

	For (iv), the quotient $\hat{M}/\hat{M}'$ is Hausdorff since $\hat{M}'$ is a closed submodule. It is also complete by Lemma \ref{prop:quotient-completion}. There is a natural homomorphism $M/M' \to \hat{M}/\hat{M}'$. Given any continuous homomorphism $\varphi: M/M' \to N$ to a complete Hausdorff $R$-module $N$, we may pull it back to $M \to N$, which corresponds to a unique continuous $\hat{M} \to N$ that is trivial on the image of $M'$; but such a homomorphism must also vanish on the closure $\hat{M}'$. This yields the required $\hat{\varphi}: \hat{M}/\hat{M}' \to N$ in the universal property.
\end{proof}

For any $R$-module $M$ endowed with $I$-adic topology, there is a canonical homomorphism $M \dotimes{R} \hat{R} \to \hat{M}$. Indeed, $\hat{M} = \varprojlim_n M/I^n M$ is a $\hat{R} = \varprojlim_n R/I^n$-module by
\[ (r_n)_{n \geq 1} \cdot (x_n)_{n \geq 1} = (r_n x_n)_{n \geq 1}, \quad (r_n)_n \in \hat{R}, \; (x_n)_n \in \hat{M}, \]
hence the $R$-homomorphism $M \to \hat{M}$ gives rise to $M \dotimes{R} \hat{R} \to \hat{M}$ by the universal property of base change. This homomorphism is continuous if $M \dotimes{R} \hat{R}$ is filtered by the images of $M \dotimes{R} (F^\bullet \hat{R})$, which makes it into a topological $\hat{R}$-module.

\begin{theorem}\label{prop:completion-tensor}
	Suppose $R$ is Noetherian. Let $M$ be a finitely generated $R$-module endowed with the $I$-adic topology. The homomorphism $M \dotimes{R} \hat{R} \to \hat{M}$ is then an isomorphism.
\end{theorem}
\begin{proof}
	Write down a finite presentation $R^{\oplus a} \to R^{\oplus b} \to M \to 0$. By the naturality of the homomorphism above, the right-exactness of $\otimes$ and Proposition \ref{prop:completion-exactness}, we have a commutative diagram
	\[\begin{tikzcd}
		R^{\oplus a} \dotimes{R} \hat{R} \arrow[r] \arrow[d] & R^{\oplus a} \dotimes{R} \hat{R} \arrow[r] \arrow[d] & M \dotimes{R} \hat{R} \arrow[r] \arrow[d] & 0 \\
		\widehat{R^{\oplus a}} \arrow[r] & \widehat{R^{\oplus b}} \arrow[r] & \hat{M} \arrow[r] & 0
	\end{tikzcd}\]
	with exact rows. Completion commutes with direct sums: $(N_1 \oplus N_2)^\wedge = \hat{N}_1 \oplus \hat{N}_2$ canonically (easy, and the categorical reason is that completion is left adjoint to oblivion $R\dcate{CompHausMod} \to R\dcate{TopMod}$). Therefore the first two vertical arrows may be identified with the canonical arrow $R^{\oplus \star} \dotimes{R} \hat{R} \to \hat{R}^{\oplus \star}$, $\star \in \{a,b\}$, which is an isomorphism of topological $\hat{R}$-modules. We infer that $M \dotimes{R} \hat{R} \rightiso \hat{M}$ topologically.
\end{proof}
\begin{remark}
	In fact $M \dotimes{R} \hat{R} \rightiso \hat{M}$ is also a homeomorphism. It suffices to observe that in the rows of the commutative diagram above, $M \dotimes{R} \hat{R}$ and $\hat{M}$ are both realized as quotient topological $\hat{R}$-modules, and that $R^{\oplus \star} \dotimes{R} \hat{R} \rightiso \hat{R}^{\oplus \star}$ is a homeomorphism. The second point has been observed in the proof of Proposition \ref{prop:completion-exactness}, and the first follows from the fact completed modules carry the $\hat{I}$-adic topology. See Proposition \ref{prop:completion-top}. We do not need this result.
\end{remark}

In the following statements, $R$ is Noetherian and an ideal $I \subsetneq R$ is chosen.
\begin{corollary}
	The canonical homomorphism $R \to \hat{R}$ is flat.
\end{corollary}
\begin{proof}
	Flatness can be tested on short exact sequences of the form $0 \to \mathfrak{a} \to R \to R/\mathfrak{a} \to 0$ where $\mathfrak{a}$ is a finitely generated ideal of $R$. Its base-change to $\hat{R}$ is the same as completion, and completion is an exact functor by Proposition \ref{prop:completion-exactness}.
\end{proof}

\begin{corollary}\label{prop:completion-auto}
	Assume $R$ is $I$-adically complete Hausdorff. Then every finitely generated $R$-module $M$ is $I$-adically complete Hausdorff, and any submodule $N \subset M$ is closed.
\end{corollary}
\begin{proof}
	For the first assertion: the completion of $M$ can be identified with the composite $M = M \dotimes{R} R \to M \dotimes{R} \hat{R} \to \hat{M}$, which is bijective. Therefore every Cauchy sequence in $M$ has a limit in $M$.

	As to the second assertion, recall that the $I$-adic topology on $N$ is the same as the one restricted from $M$ by \eqref{eqn:AR-induced-top}. It remains to notice that complete subspaces must be closed.
\end{proof}

\section{Further properties of completion}
Let $R$ be a Noetherian ring and $M$ a finitely generated $R$-module. Fix an ideal $I \subsetneq R$. Unless otherwise specified, the topologies and completions are always $I$-adic.

\begin{proposition}\label{prop:completion-aux}
	Let $\mathfrak{a}$ be an ideal, then $\mathfrak{a} \hat{M} = \widehat{\mathfrak{a}M} = \hat{\mathfrak{a}}\hat{M}$ as submodules of $\hat{M}$. Consequently $\hat{M}/\mathfrak{a}\hat{M} \simeq (M/\mathfrak{a}M)^\wedge$ canonically.
\end{proposition}
\begin{proof}
	By the exactness of completion (Proposition \ref{prop:completion-exactness}), we may realize $\hat{\mathfrak{a}}$ as an ideal of $\hat{R}$; in fact it is the image $\mathfrak{a} \hat{R}$ of $\mathfrak{a} \dotimes{R} \hat{R} \to R \dotimes{R} \hat{R} = \hat{R}$. Hence $\mathfrak{a}\hat{R} = \hat{\mathfrak{a}}$. Now consider the commutative diagram
	\[\begin{tikzcd}
		\mathfrak{a} \dotimes{R} M \dotimes{R} \hat{R} \arrow[r] \arrow[rd] \arrow[twoheadrightarrow, d] & \mathfrak{a} \dotimes{R} \hat{M} \arrow[d] \\
		\mathfrak{a}M \dotimes{R} \hat{R} \arrow[r] & \hat{M}
	\end{tikzcd}\]
	The upper horizontal arrow is an isomorphism by Theorem \ref{prop:completion-tensor}, therefore the diagonal arrow has image equal to $\mathfrak{a}\hat{M}$. The lower horizontal arrow is just the completion of $\mathfrak{a}M \hookrightarrow M$, thus injective with image $(\mathfrak{a}M)^\wedge$ by Proposition \ref{prop:completion-exactness}. A comparison yields $(\mathfrak{a}M)^\wedge = \mathfrak{a}\hat{M}$. Also note that $\hat{\mathfrak{a}}\hat{M} = \mathfrak{a}\hat{R}\hat{M} = \mathfrak{a}\hat{M}$. The final assertion results from the exactness of completion.
\end{proof}
Since the $I$-adic topology on $M/I^n M$ is discrete, as a special case ($\mathfrak{a} = I^n$) we deduce the natural identifications
\begin{gather*}
	M/I^n M = \hat{M}/I^n \hat{M} = \hat{M}/\hat{I}^n \hat{M}, \quad \forall n \geq 0, \\
	\gr_I(R) = \gr_I(\hat{R}) = \gr_{\hat{I}}(\hat{R}), \\
	\gr_I(M) = \gr_I(\hat{M}) = \gr_{\hat{I}}(\hat{M}).
\end{gather*}

\begin{proposition}\label{prop:completion-top}
	For any finitely generated $R$-module $M$, the topology on $\hat{M}$ coincides with the $\hat{I}$-adic one.
\end{proposition}
\begin{proof}
	Consider the closure of the image of $I^n M$ in $\hat{M}$. It is readily seen to be $\{(x_k)_k: i \leq n \implies x_i=0 \} = F^n \hat{M}$. On the other hand, we have seen that this closure is $\widehat{I^n M} \subset \hat{M}$. By virtue of Proposition \ref{prop:completion-aux}, we have $\widehat{I^n M} = \widehat{I^n}\hat{M}$ and $\widehat{I^n} = I^n \hat{R} = (I\hat{R})^n = \hat{I}^n$.
\end{proof}

\begin{lemma}\label{prop:gr-surjective}
	Consider a homomorphism $\varphi: L \to N$ between filtered modules over some ring, such that $L$ is complete, $N$ is Hausdorff and exhaustive (see \S\ref{sec:Artin-Rees}) with respect to their filtrations, and $\gr(\varphi): \gr(L) \to \gr(N)$ is surjective. Then $\varphi$ is also surjective.
\end{lemma}
\begin{proof}
	Let $y \in F^d N$, we may take $x \in F^d L$ such that $y' := y- \varphi(x) \in F^{d+1} N$. Next, take $x' \in F^{d+1} L$ with $y'' := y' -  \varphi(x') \in F^{d+2} N$, and so forth. Use the completeness of $L$ to define $x_\infty := x + x' + x'' + \cdots$, which maps to $y$ since $N$ is Hausdorff.
\end{proof}

\begin{proposition}\label{prop:completion-rad}
	The ring $\hat{R}$ is also Noetherian, and $\hat{I} \subset \mathrm{rad}(\hat{R})$.
\end{proposition}
\begin{proof}
	Let $\mathfrak{A}$ be any ideal of $\hat{R}$, equipped with the filtration $F^n \mathfrak{A} := \hat{I}^n \cap \mathfrak{A}$. We have to show $\mathfrak{A}$ is finitely generated. Since $\gr_I(R) = \gr_{\hat{I}}(\hat{R})$ is Noetherian, so is $\gr_F(\mathfrak{A})$. Take $t_1, \ldots, t_n \in \mathfrak{A}$, $t_i \in F^{d_i} \mathfrak{A}$, whose images $\bar{t}_i$ in $\gr^{d_i}_F(\mathfrak{A})$ generates $\gr_F(\mathfrak{A})$. Using an appropriately shifted filtration on $L := \hat{R}^{\oplus n}$, we obtain a filtered homomorphism $\varphi: L \to  \mathfrak{A}$ with image $(t_1, \ldots, t_n)$, such that $\gr(\varphi)$ is surjective. Now apply the previous Lemma to obtain the first assertion.

	One of the characterizations of Jacobson radical says that $\hat{I} \subset \mathrm{rad}(\hat{R})$ if and only if $1 - \hat{I} \subset \hat{R}^\times$. This is verified by noting that $(1-t)^{-1} = 1+t+t^2+ \cdots$ converges $I$-adically. This proves the second assertion.
\end{proof}

\begin{proposition}\label{prop:completion-semilocal}
	The map $\mathfrak{p} \mapsto \hat{\mathfrak{p}}$ furnishes an injection from $V(I)$ to $\Spec(\hat{R})$ satisfying $R/\mathfrak{p} \simeq \hat{R}/\hat{\mathfrak{p}}$ (as rings). It restricts to a bijection $\MaxSpec(R) \cap V(I) \xrightarrow{1:1} \MaxSpec(\hat{R})$.

	Consequently, if $R$ is local (resp. semi-local), so is $\hat{R}$.
\end{proposition}
\begin{proof}
	Since $\mathfrak{p} \supset I$, the $I$-adic topology on $R/\mathfrak{p}$ is discrete. By Proposition \ref{prop:completion-aux}, $\hat{R}/\hat{p} \simeq (R/\mathfrak{p})^\wedge = R/\mathfrak{p}$, and here the isomorphism even respects ring structures. Therefore $\hat{\mathfrak{p}}$ is a prime ideal. Moreover, it is maximal if and only if $\mathfrak{p}$ is. Claim: $\mathfrak{p}$ is the preimage of $\hat{\mathfrak{p}} = \varprojlim_n \mathfrak{p}/(I^n \cap \mathfrak{p})$ under $R \to \hat{R} = \varprojlim_n R/I^n$. Indeed, lying in that preimage amounts to $x \in I^n + \mathfrak{p} = \mathfrak{p}$, for all $n \geq 1$. Injectivity follows.

	Lemma \ref{prop:completion-rad} implies that every $\mathfrak{A} \in \MaxSpec(\hat{R})$ contains $\hat{I}$, therefore is open by Proposition \ref{prop:completion-top}. Since $\hat{R}$ is Noetherian, $\mathfrak{A}$ is also closed by Corollary \ref{prop:completion-auto}. As $R \to \hat{R}$ has dense image, we conclude that $\mathfrak{A}$ equals the completion of its preimage $\mathfrak{m} \in V(I) \subset \Spec(R)$. By the previous paragraph, $\mathfrak{m}$ is a maximal ideal.
\end{proof}

\section{Hilbert--Samuel polynomials}
For a graded ring $R = \bigoplus_\gamma R_\gamma$ (we always assume $1 \in R_0$) and a given $\eta$, we may define its twist $R(\eta)$ as the graded $R$-module
\[ R(\eta)_\gamma := R_{\gamma+\eta}. \]

To generate a graded $R$-module $M$ by finitely many homogeneous elements $x_1, \ldots, x_n$, of degrees $\eta_1, \ldots, \eta_n$ respectively, is equivalent to giving a surjection of graded $R$-modules
\begin{equation}\label{eqn:homogeneous-generation} \begin{tikzcd}[row sep=tiny]
	\bigoplus_{i=1}^n R(-\eta_i) \arrow[r, twoheadrightarrow] & M \\
	(\ldots, 0, \underbracket{1}_{i-\text{th slot}}, 0, \ldots) \arrow[r, mapsto] & x_i.
\end{tikzcd}\end{equation}

Hereafter, we assume that
\begin{itemize}
	\item everything is graded by $\Gamma = (\Z^N_{\geq 0}, +)$ for some fixed $N$,
	\item $R_0/R_0 \cap \mathrm{ann}(M)$ is an Artinian ring,
	\item $R$ is finitely generated over $R_0$.
\end{itemize}
The appearance of $\mathrm{ann}(M)$ is harmless since $R$ can be safely replaced by $R/\mathfrak{ann}(M)$, which is legitimate since $\mathrm{ann}(M)$ is a graded ideal of $R$. To see this, write $\mathrm{ann}(M)$ as the intersection of $\mathrm{ann}(x)$ where $x$ ranges over the homogeneous elements of $M$, and observe that $\mathrm{ann}(x)$ must be graded.

\begin{lemma}\label{prop:Hilbert-poly-0}
	For $R$ as above and $M$ a finitely generated graded $R$-module, each graded piece $M_\gamma$ is an $R_0$-module of finite length.
\end{lemma}
\begin{proof}
	Using \eqref{eqn:homogeneous-generation} this is readily reduced to the case $M = R(-\eta)$, and then to $M=R$. Write $R = R_0[x_1, \ldots, x_n]$ where each $x_i$ is homogeneous of degree $d_i$. Given $\gamma$, the $R_0$-module $M_\gamma$ is generated by monomials $x_1^{a_1} \cdots x_n^{a_n}$ with $\sum_i a_i d_i = \gamma$ and $a_1, \ldots, a_n \in \Z_{\geq 0}$; this admits only finitely many solutions $(a_1, \ldots, a_n)$. We conclude that $M_\gamma$ has finite length since $R_0/R_0 \cap \text{ann}(M)$ is an Artinian ring.
\end{proof}

Recall that saying a module $N$ over a ring $A$ has finite length means that there exists a composition series
\[ N = N_0 \supset \cdots \supset N_n = \{0\}, \quad \forall N_i/N_{i+1} \text{ is simple.} \]
The unique number (Jordan--Hölder Theorem) is called the \emph{length} of $N$, denoted by $\ell_A(N)$. The length function is additive in short exact sequences. When $A$ is a field we have $\ell_A = \dim_A$. \index{length}

\begin{definition}
	For $R$ and $M$ as in Lemma \ref{prop:Hilbert-poly-0}, we define the functions
	\[ \chi(M, \gamma) := \ell_{R_0}(M_\gamma), \quad \gamma \in \Gamma := \Z_{\geq 0}^N \]
	with values in $\Z_{\geq 0}$.
\end{definition}
One sees immediately that for a short exact sequence $0 \to M' \to M \to M'' \to 0$ of finitely generated graded $R$-modules, we have $\chi(M,\gamma) = \chi(M',\gamma) + \chi(M'',\gamma)$ for all $\gamma \in \Gamma$. This extends to alternating sums of $\chi$ in finite exact sequences.

One can control the behavior of $\chi(M, \cdot)$ by forming the Poincaré series $P_M(\mathbf{X}) = \sum_{\gamma \in \Gamma} \chi(M,\gamma) \mathbf{X}^\gamma$; see \cite[6.D]{BG09}. Here we shall restrict to the case $N=1$, i.e. $\Gamma = \Z_{\geq 0}$, in order to gain more control of $\chi(M,\gamma)$. As a preparation, we say a function $H: \Z \to \CC$ is a \emph{quasi-polynomial} of period $\varpi$ if its restriction to each congruence class modulo $\varpi$ coincides with a polynomial function (necessarily unique); the degree of $\chi$ is defined by taken the maximum among congruence classes. In particular, a quasi-polynomial of period $1$ is just a polynomial.

\begin{theorem}
	Assume $\Gamma = \Z_{\geq 0}$. Suppose $R$ is generated by homogeneous elements $x_1, \ldots, x_n$ over $R_0$. There exists a unique quasi-polynomial $H_M$ of degree $\leq n-1$, with coefficients in $\Q$ and period $e := \mathrm{lcm}(\deg x_1, \ldots, \deg x_n)$, such that
	\[ \chi(M, \gamma) = H_M(\gamma), \quad |\gamma| \gg 0. \]
\end{theorem}
\begin{proof}
	Uniqueness is clear. We construct $H_M$ by induction on the minimal number of generators $n$. If $n=0$ then $R = R_0$ and $M_\gamma = 0$ for $|\gamma| \gg 0$, in which case $H_M = 0$.

	For $n \geq 1$, write $R = R_0[x_1, \ldots, x_n]$ as usual. We may assume that $x_i \neq 0$ has degree $\eta_i$, for $i=1,\ldots,n$. Fix $i$ and define the graded modules
	\[ Z := \Ker\left( M \xrightarrow{\cdot x_i} M(\eta_i) \right), \quad Y := \Coker\left( M(-\eta_i) \xrightarrow{\cdot x_i} M \right) \]
	which are again finitely generated, so that we have the exact sequence
	\[ 0 \to Z_\gamma \to M_\gamma \to M_{\gamma + \eta_i} \to Y_{\gamma + \eta_i} \to 0, \quad \gamma \in \Gamma. \]
	Since $x_i$ annihilates $Z$ and $Y$, induction hypothesis entails
	\[ \chi(M, \gamma+\eta_i) - \chi(M, \gamma) = \chi(Y, \gamma+\eta_i) - \chi(Z, \gamma), \]
	the right-hand side being quasi-polynomials of period $\text{lcm}(\ldots, \widehat{\eta_i}, \ldots)$ for large $|\gamma|$ and of degrees $\leq n-2$, since $x_i$ acts trivially on $Z$ and $Y$. Doing this for all $i$ yields difference equations that witness the polynomiality of $\chi(M, \gamma)$ for $|\gamma| \gg 0$ in every congruence class modulo $e$.
\end{proof}
In particular, if $R$ is generated by $R_1$ over $R_0$, the period $e=1$ and we have the notion of \emph{Hilbert--Samuel polynomials}.\index{Hilbert--Samuel polynomial}

\begin{example}\label{eg:count-monomials}
	For $R = M = \Bbbk[X_1, \ldots, X_n]$ graded by total degree, where $\Bbbk$ is a field, our assumptions are readily verified. We see $\chi(M, \gamma) = \dim_{\Bbbk} \Bbbk[X_1, \ldots, X_n]_{\deg=\gamma}$ for all $\gamma \in \Z_{\geq 0}$, which equals $\binom{\gamma + n - 1}{n- 1}$ by high school combinatorics. Hence the Hilbert--Samuel polynomial is $H_M(X) = \binom{X+n-1}{n-1} \in \Q[X]$.
\end{example}

\section{Definition of Krull dimension}\label{sec:Krull-dimension}
Let $R$ be a ring.

\begin{definition}[Height and dimension]\label{def:height-dimension}\index{height}\index{dimension}
	For any prime ideal $\mathfrak{p}$ of $R$, define its \emph{height} $\text{ht}(\mathfrak{p})$ as the supremum of the lengths of prime chains
	\[ \mathfrak{p} = \mathfrak{p}_0 \supsetneq \mathfrak{p}_1 \supsetneq \cdots \supsetneq \mathfrak{p}_n, \quad \text{length} := n. \]
	For any ideal $\mathfrak{a}$ of $R$, we define $\text{ht}(\mathfrak{a}) := \inf\{ \text{ht}(\mathfrak{p}) : \mathfrak{p} \supset \mathfrak{a} \}$.

	Define the \emph{Krull dimension} of $R$ to be $\dim R := \sup_{\mathfrak{p} \in \Spec(R)} \text{ht}(\mathfrak{p})$.
\end{definition}

The following results are immediate.
\begin{itemize}
	\item The zero prime in a domain has height $0$.
	\item Fields have dimension zero. In fact, a ring has dimension zero if and only if every prime ideal is maximal.
	\item We have $\text{ht}(\mathfrak{p}) = \dim R_{\mathfrak{p}}$ for every prime ideal $\mathfrak{p} \subset R$.
	\item For any ideal $\mathfrak{a}$ we have $\dim R \geq \dim(R/\mathfrak{a}) + \text{ht}(\mathfrak{a})$.
\end{itemize}

\begin{exercise}
	Verify the last property above.
\end{exercise}

\begin{exercise}
	Show that every principal ideal domain which is not a field has dimension one.
\end{exercise}

More generally, we define the dimension of an $R$-module $M$ as
\[ \dim M := \dim(R/\text{ann}(M)), \quad \dim\{0\} := -\infty . \]
For a short exact sequence $0 \to M' \to M \to M'' \to 0$ we have $\dim M', \dim M'' \leq \dim M$.

\begin{lemma}\label{prop:dim-M-equiv}
	Suppose $R$ is Noetherian. The following are equivalent for a finitely generated $R$-module $M \neq \{0\}$.
	\begin{enumerate}[(i)]
		\item $\dim M = 0$.
		\item $R/\mathrm{ann}(M)$ is Artinian.
		\item $M$ has finite length.
	\end{enumerate}
\end{lemma}
\begin{proof}
	(i) $\iff$ (ii) is already known: recall that a Noetherian ring is Artinian if and only if its prime ideals are all maximal (Corollary \ref{prop:Artinian-dim-0}). Let us show (i) or (ii) $\implies$ (iii). By writing $M = M_1 + \cdots + M_n$ where each $M_i$ is generated by one element,  we may assume $M \simeq R/\mathfrak{a}$ for some ideal $\mathfrak{a} = \text{ann}(M)$. It has been shown that $R/\mathfrak{a}$ has finite length as a module since it is an Artinian ring.

	(iii) $\implies$ (i). Upon modulo $\text{ann}(M)$ we may assume $\text{ann}(M)=\{0\}$. Take any minimal prime $\mathfrak{p}$ in $R$. As $\text{ann}(M) = \{0\}$ we have $M_{\mathfrak{p}} \neq \{0\}$. Therefore $\mathfrak{p}$ is a minimal element of $\Supp(M)$, hence belongs to $\text{Ass}(M)$. We may embed $R/\mathfrak{p}$ into $M$. The $R$-module $R/\mathfrak{p}$ has finite length since $M$ does, therefore $R/\mathfrak{p}$ is an Artinian ring. This implies $\mathfrak{p}$ is a maximal ideal, therefore $\dim R = 0$ since every prime in $R$ lies over a minimal prime.
\end{proof}

Our strategy is to study the Krull dimension via completions and Hilbert polynomials. As a preparation, we begin with the local, or more generally the semi-local rings.

\begin{definition}\index{parameter ideal}
	Let $R$ be a Noetherian semi-local ring (i.e. there are finitely many maximal ideals $\mathfrak{m}_1, \ldots, \mathfrak{m}_n$). Let $M \neq \{0\}$ be a finitely generated $R$-module. We say an ideal $I$ is a \emph{parameter ideal} for $M$ if $I \subset \text{rad}(R)$ and $M/IM$ has finite length.
\end{definition}
Parameter ideals are often called \emph{ideals of definition}. Here we follow the terminologies of \cite{Eis95}.

\begin{exercise}
	Show that $I$ is a parameter ideal for $R$ if and only if there exists $k$ with
	\[ \text{rad}(R)^k \subset I \subset \text{rad}(R). \]
	Show that such an ideal is a parameter ideal for every $M$. Hint: If $I \supset \text{rad}(R)^k$, every prime ideal $\mathfrak{p} \supset I$ must contain $(\mathfrak{m}_1 \cdots \mathfrak{m}_n)^k$, hence $\mathfrak{p} = \mathfrak{m}_i$ for some $i$. Conversely, show that in an Artinian ring we have $\text{rad}(R)^k = 0$ for $k \gg 0$, using Corollary \ref{prop:Artinian-dim-0}. Hint: for Artinian rings, $\text{rad}(R)$ equals the nilpotent radical, and is finitely generated.
\end{exercise}

Dimension theory for modules can be built solely on the parameter ideals for $R$, but we opt to introduce the general notion here.

Hereafter we fix a Noetherian semi-local ring $R$ and a finitely generated $R$-module $M \neq \{0\}$.

\begin{lemma}\label{prop:para-ideal-characterization}
	An ideal $I \subset \mathrm{rad}(R)$ is a parameter ideal for $M$ if and only if there exists $k$ with $\mathrm{rad}(R)^k \subset I + \mathrm{ann}(M)$. In this case $R/(I+\mathrm{ann}(M))$ is an Artinian ring.

	In particular, $\mathrm{rad}(R)$ is a parameter ideal for any $M$.
\end{lemma}
\begin{proof}
	First we claim that $V(\text{ann}(M/IM)) = \Supp(M/IM)$ equals $V(I+\text{ann}(M))$. By the exactness of localizations together with Nakayama's Lemma, we have
	\[ \Supp(M/IM) = \Supp(M) \cap \left\{ \mathfrak{p}: IR_{\mathfrak{p}} \subsetneq R_{\mathfrak{p}} \right\}; \]
	the last term equals $\Supp(M) \cap V(I) = V(\text{ann}(M)+I)$, thereby proving our claim. By applying to $M/IM$ the Lemma \ref{prop:dim-M-equiv}, $I \subset \text{rad}(R)$ being a parameter ideal for $M$ is equivalent to any one of the following
	\begin{align*}
		\frac{R}{\text{ann}(M/IM)} \text{ is Artinian} & \iff V(\text{ann}(M/IM)) \subset \MaxSpec(R) \\
		& \iff V(I+\text{ann}(M)) \subset \MaxSpec(R) \\
		& \iff \bar{R} := \frac{R}{I+\text{ann}(M)} \text{ is Artinian}.
	\end{align*}
	If $\bar{R}$ is Artinian, then the image of $\text{rad}(R)$ in $\bar{R}$ is contained in $\text{rad}(\bar{R})$, and we know $\text{rad}(\bar{R})^k = 0$ for large $k$.

	Conversely, suppose $\text{rad}(R)^k \subset I + \text{ann}(M)$. We claim that $R/\text{rad}(R)^k$ is Artinian: $\mathrm{rad}(R)$ contains the product $\mathfrak{m}_1 \cdots \mathfrak{m}_k$ of all maximal ideals, so every over-prime of $\text{rad}(R)^k$ is some $\mathfrak{m}_i$, thus maximal. This shows that $M/IM$ has finite length by the previous equivalences and Lemma \ref{prop:dim-M-equiv}.
\end{proof}

Given an parameter ideal $I$ for $M$. The $I$-adic grading gives rise to the graded objects
\[ \gr_I(R) = \bigoplus_{n \geq 0} \frac{I^n}{I^{n+1}}, \quad \gr_I(M) = \bigoplus_{n \geq 0} \frac{I^n M}{I^{n+1}M}. \]

Recall that
\begin{compactitem}
	\item $\gr_I(R)$ is finitely generated over $\gr^0_I(R) = R/I$ as an algebra and is Noetherian (Proposition \ref{prop:gr-Noetherian});
	\item more precisely, $\gr_I(R)$ is generated by $\gr^1_I(R)$ over $R/I$.
	\item $\gr_I(M)$ is a finitely generated $\gr_I(R)$-module (Proposition \ref{prop:gr-fg});
	\item the ring $\gr^0_I(R) = R/I$ becomes Artinian after modulo $\gr^0_I(R) \cap \text{ann}(\gr_I(M))$, which contains $(\text{ann}(M)+I)/I$ (use Lemma \ref{prop:para-ideal-characterization}).
\end{compactitem}
Upon recalling Lemma \ref{prop:Hilbert-poly-0}, it are justified to define
\[ \chi(M, I; n) := \ell_{R/I^n}(M/I^n M) = \sum_{j=0}^{n-1} \ell_{R/I}(I^j M/I^{j+1} M), \quad n \in \Z_{\geq 0}. \]
By the theory of Hilbert--Samuel polynomials, $n \mapsto \chi(M,I; n)$ is a polynomial $H_I(M, \cdot)$ whenever $n \gg 0$, with $\deg H_I(M, \cdot)$ bounded by the minimal number of generators of $\gr_I(R)$ over $\gr^0_I(R) = R/I$.
\begin{lemma}\label{prop:Hilbert-poly-I}
	Let $M \neq \{0\}$ be a finitely generated $R$-module with parameter ideal $I$.
	\begin{enumerate}[(i)]
		\item The degree $d(M)$ of $H_I(M, \cdot)$ is independent of the choice of the parameter ideal $I$.
		\item In a short exact sequence $0 \to M' \to M \to M'' \to 0$ of finitely generated $M$-modules, we have
		\[ \deg H_I(M', \cdot), \deg H_I(M'', \cdot) \leq \deg H_I(M, \cdot), \]
		and $H_I(M, \cdot) - H_I(M', \cdot) - H_I(M'', \cdot)$ has degree $< d(M)$.
	\end{enumerate}
\end{lemma}
\begin{proof}
	(i): To compare the graded objects associated to two parameter ideals $I, J$, we apply the characterization in Lemma \ref{prop:para-ideal-characterization}: it suffices to take $J = \mathrm{rad}(R)$, so that
	\[ J^m + \text{ann}(M) \subset I + \text{ann}(M) \subset J + \text{ann}(M) \]
	for some $m \geq 1$. This implies $\chi(M,J; n) \leq \chi(M,I;n)$ and $\chi(M,I;n) \leq \chi(M,J;mn)$ for all $n \geq 0$. Whence (i).

	(ii): For the first part, note that $M/I^n M \to M''/I^n M''$ is surjective, so $\chi(M'', I; n) \leq \chi(M, I; n)$. On the other hand, $M'/I^n M' \to M/I^n M$ has kernel $\frac{M' \cap I^n M}{I^n M'}$. For $n \geq n_0 \gg 0$, Artin--Rees (Theorem \ref{prop:Artin-Rees}) gives
	\begin{multline*}
		\ell\left(\frac{M' \cap I^n M}{I^n M'}\right) = \ell\left(\frac{I^{n - n_0}(M' \cap I^{n_0} M)}{I^n M'}\right) \leq \ell\left(\frac{I^{n - n_0} M'}{I^n M'}\right) \\
		= \ell\left( \frac{M'}{I^n M'} \right) - \ell\left( \frac{M'}{I^{n - n_0} M'}\right) = \chi(M', I; n) - \chi(M', I; n-n_0)
	\end{multline*}
	which has degree inferior to $H_I(M', \cdot)$. Hence $\deg H_I(M', \cdot) \leq \deg H_I(M, \cdot)$.

	To establish the second part of (ii), we consider $\chi(M, I; n) - \chi(M'', I; n)$ which equals
	\begin{gather*}
		\ell\left(\frac{M}{I^n M}\right) - \ell\left( \frac{M}{M' + I^n M} \right) = \ell\left( \frac{M' + I^n M}{I^n M} \right) = \ell\left( \frac{M'}{M' \cap I^n M} \right).
	\end{gather*}
	By Artin--Rees, the rightmost term is squeezed between $\ell(M'/I^n M') = \chi(M',I; n)$ and $\ell( M'/I^{n-k} M') = \chi(M', I; n-k)$ for some $k$ independent of $n \gg 0$. Hence $\chi(M, I; n) - \chi(M'', I; n)$ is a polynomial with the same leading term as $\chi(M', I; n)$, for $n \gg 0$.
\end{proof}

Define $s(M)$ to be the smallest integer $s$ such that there exist $t_1, \ldots, t_s \in \text{rad}(R)$ with $M/\sum_{i=1}^s t_i M$ of finite length. In other words, $s(M)$ is the minimal number of generators for parameter ideals for $M$. Observe that $M/\sum_{i=1}^s t_i M \neq \{0\}$, otherwise Nakayama's Lemma will lead to $M = \{0\}$.

\begin{theorem}\label{prop:dim-s-d}
	For any finitely generated nonzero $R$-module $M$, we have $\dim M = s(M) = d(M)$. In particular $\dim M$ is finite.
\end{theorem}
\begin{proof}
	We argue inductively on $d(M)$ to show $\dim M \leq d(M)$. If $d(M)=0$ then $I^n M = I^{n+1} M = \cdots$ for $n \gg 0$. Corollary \ref{prop:Krull-intersection-rad} implies $I^n M = \{0\}$, hence $M=M/I^n M$ has finite length and $\dim M = 0$ by Lemma \ref{prop:dim-M-equiv}.

	Now assume $d(M) \geq 1$. Take a minimal $\mathfrak{p} \in \text{Ass}(M)$ verifying $\dim(R/\mathfrak{p}) = \dim M$, so that $R/\mathfrak{p} \hookrightarrow M$. As $d(R/\mathfrak{p}) \leq d(M)$, we are reduced to the case $M = R/\mathfrak{p}$. Consider a chain of prime ideals
	\[ \mathfrak{p} = \mathfrak{p}_0 \subsetneq \cdots \subsetneq \mathfrak{p}_m \]
	in $R$. We claim that $m \leq d(R/\mathfrak{p})$. We may surely suppose $m \geq 1$. Take $t \in \mathfrak{p}_1 \smallsetminus \mathfrak{p}_0$. Reduction modulo $Rt + \mathfrak{p}$ yields a prime chain of length $m-1$, namely
	\[ \frac{\mathfrak{p}_1}{Rt + \mathfrak{p}} \subsetneq \cdots \frac{\mathfrak{p}_m}{Rt + \mathfrak{p}} \]
	in $R/(Rt + \mathfrak{p})$. Hence $\dim(R/Rt+\mathfrak{p}) \geq m-1$. In view of the exactness of
	\[ 0 \to R/\mathfrak{p} \xrightarrow{t} R/\mathfrak{p} \to \frac{R}{Rt+\mathfrak{p}} \to 0, \]
	Lemma \ref{prop:Hilbert-poly-I} (ii) implies that $d(R/Rt+\mathfrak{p}) < d(R/\mathfrak{p})$. By induction we deduce $d(R/\mathfrak{p}) > d(R/Rt+\mathfrak{p}) \geq \dim(R/Rt+\mathfrak{p}) \geq m-1$, hence $d(R/\mathfrak{p}) \geq m$ as required.

	Next, let us show $s(M) \leq \dim M$. Set $r := \dim M$. We contend that there exist $t_1, \ldots, t_r \in \text{rad}(R)$ such that $M/(t_1, \ldots, t_r)M$ has finite length; therefore $s \leq r$. When $r=0$ this follows from Lemma \ref{prop:dim-M-equiv}. For $r > 0$, we have $\text{rad}(R) \not\subset \mathfrak{p}$ for any minimal $\mathfrak{p} \in \text{Ass}(M)$ verifying $\dim R/\mathfrak{p} = \dim M$, for otherwise $\mathfrak{p}$ will contain, thus equal to a maximal ideal as $R$ is semi-local, and we would get $\dim M = 0$. Using prime avoidance (Proposition \ref{prop:prime-avoidance} applied to $I := \mathrm{rad}(R)$ and the primes $\mathfrak{p}$ above), we may pick $t_1 \in \text{rad}(R)$ that does not belong to any $\mathfrak{p}$ above. From $\text{ann}(M/t_1 M) \supset Rt_1 + \text{ann}(M)$ and our choice of $t_1,$, we see $\dim M/t_1 M < \dim M$. Our claim results from induction on $r$.

	We finish the proof by showing $d(M) \leq s(M)$. Suppose $M/\sum_{i=1}^s t_i M \neq \{0\}$ has finite length. We contend that for any $t \in \text{rad}(R)$ we have
	\begin{gather}\label{eqn:d-drop}
		d(M) \geq d(M/tM) \geq d(M)-1.
	\end{gather}
	If this holds, we can look at the sequence $M, M/t_1 M, M/(t_1M + t_2 M), \ldots$: at each step $d(\cdots)$ drops at most by one; at the end $L := M/\sum_{j=1}^s t_j M$ we have $d(L)=0$: indeed, as $L$ has finite length, $\ell(L/I^n L)$ is uniformly bounded by $\ell(L)$ so that $d(L)=0$. Thus $d(M) \leq s$ as expected.

	To prove \eqref{eqn:d-drop}, first note that $d(M) \geq d(M/tM)$ is known. Take any parameter ideal $I \ni t$. We bound $\chi(M/tM, I; n)$ as follows
	\[ \ell\left( \frac{M}{tM + I^n M} \right) = \ell\left( \frac{M}{I^n M} \right) - \ell\left( \frac{tM + I^n M}{I^n M} \right). \]
	Note that
	\begin{align*}
		\frac{M}{I^{n-1} M}\twoheadrightarrow \frac{M}{ \{ x \in M : tx \in I^n M \} } & \rightiso \frac{tM}{tM \cap I^n M} \simeq \frac{tM + I^n M}{I^n M} \\
		y & \mapsto ty.
	\end{align*}
	Hence $\chi(M/tM, I; n) \geq \chi(M, I; n) - \chi(M, I; n-1)$ for $n \gg 0$, proving the second inequality in \eqref{eqn:d-drop}.
\end{proof}

\begin{corollary}
	Under the same assumptions, we have $\dim_R M = \dim_{\hat{R}} \hat{M}$, where we take $I$-adic completions.
\end{corollary}
\begin{proof}
	Proposition \ref{prop:completion-aux} gives identifications $\gr_I(R) = \gr_{\hat{I}}(\hat{R})$ and $\gr_I(M) = \gr_{\hat{I}}(\hat{M})$; moreover $\hat{R}$ is still semi-local and $\hat{I}$ is still a parameter ideal for $\hat{M}$ (see Proposition \ref{prop:completion-rad}, \ref{prop:completion-semilocal}). Since $d(M)$ and $d(\hat{M})$ are read from these graded modules, they are equal.
\end{proof}

These results will be applied to the case $M=R$ in the next section.

\section{Krull's theorems and regularity}
We still assume $R$ is a Noetherian ring.

\begin{theorem}[Krull]
	Suppose $\mathfrak{a} = (t_1, \ldots, t_r)$ is a proper ideal of $R$, then for every minimal over-prime ideal $\mathfrak{p}$ of $\mathfrak{a}$, we have $\mathrm{ht}(\mathfrak{p}) \leq r$.
\end{theorem}
From Definition \ref{def:height-dimension} we infer that $\text{ht}(\mathfrak{a}) \leq r$. The special case $r=1$ says that every principal ideal $(t) \neq R$ has height at most one (exactly one if $t$ is not a zero divisor --- use Theorem \ref{prop:Ass-properties} (ii)); this is called the Hauptidealsatz.\index{Hauptidealsatz}
\begin{proof}
	We work in $R_{\mathfrak{p}}$ and $I := \mathfrak{a} R_{\mathfrak{p}} = (t_1, \ldots, t_r)$. Since $I \subset \text{rad}(R_{\mathfrak{p}})$ and $R_{\mathfrak{p}}/I$ has dimension zero, thus Artinian, $I$ is a parameter ideal. We conclude by Theorem \ref{prop:dim-s-d} and $\text{ht}(\mathfrak{p}) = \dim R_{\mathfrak{p}}$.
\end{proof}

Now assume $R$ is Noetherian and local with maximal ideal $\mathfrak{m}$. The parameter ideals of $R$ are precisely those squeezed between $\mathfrak{m}$ and $\mathfrak{m}^k$ for some $k \geq 1$, by Lemma \ref{prop:para-ideal-characterization}. Set $d := \dim R$, which is finite by Theorem \ref{prop:dim-s-d}. The same theorem tells us that we can generate some parameter ideal $I \subset \mathfrak{m}$ (namely $R/I$ Artinian) by elements $t_1, \ldots, t_d \in \mathfrak{m}$. These elements form a \emph{system of parameters} of $R$.

\begin{proposition}\label{prop:system-parameter}
	Suppose $R$ is a Noetherian local ring with a system of parameters $t_1, \ldots, t_d$, which generate a parameter ideal $I$. For any $0 \leq i \leq d$ we have $\dim(R/(t_1, \ldots, t_i)) = d-i$, and $t_{i+1}, \ldots, t_d$ form a system of parameters for $R/(t_1, \ldots, t_i)$.
\end{proposition}
\begin{proof}
	Consider the sequence $R, R/(t_1), R/(t_1, t_2), \ldots, R/(t_1, \ldots, t_d)$. Recall the formalism in Theorem \ref{prop:dim-s-d}: at each stage $d(R/\cdots)$ drops at most by one, by \eqref{eqn:d-drop}. After $d$ steps we arrive at $R/I$ with $\dim(\cdot)=d(\cdot)=s(\cdot)=0$, since it has finite length. Hence $d$ drops exactly by one at each stage. The remaining assertions are immediate.
\end{proof}

A natural question arises: when can we assure $I=\mathfrak{m}$?
\begin{definition}\index{regular local ring}
	We say a Noetherian local ring $R$ is a \emph{regular local ring} if $\mathfrak{m}$ can be generated by $d = \dim R$ elements $t_1, \ldots, t_d$. In this case we say $t_1, \ldots, t_r$ form a \emph{regular system of parameters}.
\end{definition}
In particular, $\mathfrak{m}=\{0\}$ if $R$ is regular local with $\dim R = 0$.

\begin{exercise}
	Let $\Bbbk$ be a field. The $\Bbbk$-algebra of formal power series $\Bbbk\llbracket X_1, \ldots, X_d \rrbracket$ is a regular local ring. Indeed, it is Noetherian with maximal ideal $\mathfrak{m} = (X_1, \ldots, X_d)$, and $X_1, \ldots, X_d$ form a regular system of parameters. On the other hand, $\mathfrak{m}/\mathfrak{m}^2$ has a $\Bbbk$-basis formed by the images of $X_1, \ldots, X_d$. One way to determine its dimension and prove its regularity is to calculate the functions $n \mapsto \dim_\Bbbk(\mathfrak{m}^n/\mathfrak{m}^{n+1})$ explicitly, i.e. count the monomials in $d$ variables with total degree $n$. You should get a polynomial in $n$ with degree $d-1$, cf. Exercise \ref{eg:count-monomials}.
\end{exercise}

\begin{theorem}
	For any Noetherian local ring $R$ with maximal ideal $\mathfrak{m}$ and residue field $\Bbbk$, we have $\dim R \leq \dim_\Bbbk \mathfrak{m}/\mathfrak{m}^2$. Equality holds if and only if $R$ is a regular local ring.
\end{theorem}
\begin{proof}
	By Nakayama's Lemma (more precisely, Corollary \ref{prop:NAK-generation}), $\mathfrak{m}/\mathfrak{m}^2$ can be generated over $\Bbbk$ by $s$ elements if and only if $\mathfrak{m}$ can be generated over $R$ by $s$ elements, for any $s \in \Z_{\geq 0}$. Hence Theorem \ref{prop:dim-s-d} imposes the bound $s \geq \dim R$, and equality holds if and only if $R$ admits a regular system of parameters.
\end{proof}

\begin{figure}[h]
	\centering \includegraphics[height=130pt]{OZariski.jpg} \\ \vspace{1em}
	\begin{minipage}{0.7\textwidth}
		\small The $\Bbbk$-vector space $\mathfrak{m}/\mathfrak{m}^2$ is called the \emph{Zariski cotangent space} of $\Spec(R)$, in honor of Oscar Zariski (1899--1986). Picture taken from \href{https://commons.wikimedia.org/wiki/File:Oscar_Zariski.jpg}{Wikimedia Commons}.
	\end{minipage}
\end{figure}

Due to time constraints, we cannot say too much about regular local rings. Below is one of their wonderful properties.
\begin{theorem}\label{prop:regular-local-domain}
	Regular local rings are integral domains.
\end{theorem}
\begin{proof}
	Induction on $d := \dim R$. If $\dim R=0$ then $\mathfrak{m}=\{0\}$, hence $R$ is a field. Assume hereafter that $d > 0$. We know there are only finitely many minimal prime ideals and $\dim_\Bbbk \mathfrak{m}/\mathfrak{m}^2 \geq 1$. By prime avoidance (Proposition \ref{prop:prime-avoidance}) applied to $I := \mathfrak{m}$, the minimal prime ideals and $\mathfrak{m}^2$, there exists $t \in \mathfrak{m} \smallsetminus \mathfrak{m}^2$ that does not lie in any minimal prime ideal. Put $R' := R/(t)$ with maximal ideal $\mathfrak{m}' = \mathfrak{m}/(t)$; our choice of $t$ together with Proposition \ref{prop:system-parameter} imply
	\begin{align*}
		\dim R' & = \dim R - 1, \\
		\dim_\Bbbk \mathfrak{m}'/(\mathfrak{m}')^2 & = \dim_\Bbbk \mathfrak{m}/\mathfrak{m}^2 - 1 = \dim R - 1.
	\end{align*}
	Hence $R'$ is still regular local, and by induction it is a domain. This implies $(t)$ is prime.

	Take any minimal prime $\mathfrak{p}$ below $(t)$; note that $t \notin \mathfrak{p}$ by construction. To show $R$ is a domain it suffices to prove $\mathfrak{p}=\{0\}$. Indeed, every $s \in \mathfrak{p}$ can be written as $s=at$, $a \in R$. Since $t \notin \mathfrak{p}$, we must have $a \in \mathfrak{p}$. Hence $\mathfrak{p}=t\mathfrak{p} \subset \mathfrak{m}\mathfrak{p}$. Nakayama's Lemma (Theorem \ref{prop:NAK}) implies $\mathfrak{p} = \{0\}$.
\end{proof}


\section{Density Estimation}

% ===
%\subsection{Bayesianism / Frequentism}

\emph{Bayesianism:}
Define prior $P(\theta)$, define likelihood $P(X\mid\theta)$, compute posterior $P(\theta\mid x_{1...n})$.
\\
\textbf{Bayes:}
$P(\theta\mid X) = \frac{P(X\mid\theta)P(\theta)}{P(X)}$,
{\footnotesize $P(X) {=} \sum_\theta P(X\vert\theta_i) P(\theta_i)$}

\emph{Frequentism:}
%Define a parametric model $\theta$ (e.g. $\Gauss{\theta,1}$), compute likelihood of data and compute MLE: $\hat\theta\ped{MLE} = \argmax_\theta P(y_{1...n}\mid\theta)$.
Define param. model $P(Y\mid X,\theta)$, compute likelihood of data $P(X,Y\mid\theta)$ and compute $\hat\theta\ped{MLE}$ via $\argmax_\theta$ of likelihood.

% ===
\subsection{Estimation - MLE Properties}

\emph{Consistency:}
$\forall\epsilon>0, \; \mathbb P\{ \abs{\hat\theta_n - \theta^\ast} > \epsilon \} \overset{n\to\infty}{\longrightarrow} 0$

\emph{Equivariance:}
If $\hat\theta_n$ is MLE of $\theta$, then $g(\hat\theta_n)$ is MLE of $g(\theta)$.

\emph{Asympt. normality:}\\
$\sqrt{n} (\hat\theta_n - \theta^\ast) \to \Gauss{0,\, J^{-1}(\theta^\ast) I_n(\theta^\ast) J^{-1}(\theta^\ast)}$%, where $J = -\E*[x\mid\theta^\ast]{\frac{\partial^2 \log \P{x\mid\theta}}{\partial\theta\partial\theta^\top}}$ and $I(\theta_0) \triangleq \textrm{Fisher info}$.

\emph{Asympt. efficiency:}
$\hat\theta_n$ minimises $\E{(\hat\theta_n - \theta^\ast)^2}$ as $n{\to}\infty$, i.e. $\E{(\hat\theta_n - \theta^\ast)^2} \overset{n\to\infty}{=} \frac{1}{I_n(\theta^\ast)}$ (Rao Cr.)\\
Among all consistent estimators $\hat\theta_n$ has \textit{smallest variance}: $\lim_{n\to\infty} (\V{\hat\theta_n} I_n(\theta^\ast))^{-1} = 1$

% ===
\subsection{Rao Cramer inequality \hfill {\normalfont\footnotesize all $\mathbb E$ w.r.t. $P(x\mid\theta^\ast)$}}
%(all $\E{}$ w.r.t. $\P{x\mid\theta^\ast}$)

Score func.: $\bm\Lambda = \pderiv{\log\P{\bm x\mid\theta}}{\theta}$,\; $\E{\bm\Lambda} = 0$

Fisher info.: $I_n(\theta) = \V{\bm\Lambda}$\\
$J(\theta) = \E{\bm\Lambda^2} = -\E*{\frac{\partial^2 \log \P{x\mid\theta}}{\partial\theta\partial\theta^\top}} = -\E*{\pderiv{\bm\Lambda}{\theta}}$

\emph{General bound:} $\E{(\hat\theta_n - \theta^\ast)^2} \geq \frac{\paren*{ 1 + \frac{\partial}{\partial\theta} \mathrm{b}_{\hat\theta} }^2\!\!}{\E{\bm\Lambda^2}} + \mathrm{b}_{\hat\theta}^2$

\emph{Unbiased case:} $\E{(\hat\theta_n - \theta^\ast)^2} = \V{\hat\theta_n} \geq \frac{1}{I_n(\theta^\ast)}$

\emph{Tradeoff:}
$\E{(\hat\theta_n - \theta^\ast)^2} = \V{\hat\theta_n} + \mathrm{bias}^2(\hat\theta_n)$

\emph{Bias:}
$\mathrm{bias}(\hat\theta_n) \equiv \mathrm{b}_{\hat\theta}(\theta^\ast) = \E{\hat\theta_n} - \theta^\ast \overset{\textrm{unbiased}}{=} 0$

% ===


\documentclass[../../main.tex]{subfiles}

\begin{document}

\subsection{Motivation}
There are several well-known tensor decomposition methods, but there is no clear understanding how they compare in terms of quality.

In this work we aim to compare different high-order tensor decomposition techniques, provided in HottBox\footnote{\url{https://hottbox.github.io/stable/index.html}} tooling. More specifically, we benchmark CPD, Tucker Decomposition and Tensor-Train against three quality criterions: precision, stability and computational complexity.

As an input data we use Tensor Flow logo (three dimensional tensor). For this tensor we find low-rank approximations and then calculate quality criterions listed above.

\subsection{Problem statement}

Formally, let's state $X$ is an 3rd order tensor (input image). We define approximations in the following way:

\begin{equation} \label{eq:kotua_1}
\underline{\mathbf{X}}=\sum_{r=1}^{R} \underline{\mathbf{X}}_{r}=\sum_{r=1}^{R} \lambda_{r} \cdot \mathbf{a}_{r} \circ \mathbf{b}_{r} \circ \mathbf{c}_{r} \end{equation}

\begin{equation} \label{eq:kotua_2}
\underline{\mathbf{X}}=\underline{\mathbf{G}} \times_{1} \mathbf{A} \times_{2} \mathbf{B} \times_{3} \mathbf{C}\end{equation}

\begin{equation} \label{eq:kotua_3}
\underline{\mathbf{X}}=\mathbf{A} \times{ }_{2}^{1} \underline{\mathbf{G}}^{(1)} \times_{3}^{1} \underline{\mathbf{G}}^{(2)} \times_{3}^{1} \cdots \times_{3}^{1} \underline{\mathbf{G}}^{(N-1)} \times_{3}^{1} \mathbf{B}\end{equation}

Where \ref{eq:kotua_1} - \ref{eq:kotua_3} correspond to CPD, HOSVD and Tensor-Train decompositions. Corresponding output tensors we define as $X_{cpd}, X_{ho}, X_{tt}$.

Precision is defined the following way:

\begin{equation}
    Precision = \frac{|X - X_i|}{|X|},  \forall i \in \set{\{cpd, ho, tt\}}
\end{equation}

Complexity is calculated as time taken to complete decomposition. Stability is estimated as increase in error when some Gaussian noise is added to the input tensor. In this work Gaussian noise is a tensor of same shape as X, where each element is sampled from the normal distribution with parameters $\mu$ and $\sigma$.

\subsection{Experiment description}

Tensor Flow logo is taken as an input image. The input converted to the 3rd order tensor with shape (232, 217, 4).

Then, three experiments are conducted for each type of tensor decomposition (CPD, HOSVD and Tensor Train):

\begin{enumerate}
    \item Calculate relative error of decomposition for different ranks of core tensor
    \item Measure time taken to compute the decomposition for each of the rank
    \item Calculate relative error of decomposition when Gaussian noise with different sigma is added to the input image. Repeat it for each rank and then plot 3D graph, illustrating the stability.
\end{enumerate}

\subsection{Plots and analysis}

First of all, we compare how relative error decreases with regards to the rank for different tensor decompositions. As it can be seen from the chart \ref{fig:kotua:2}, TT and HOSVD showcase almost identical results while CPD is quite noisy and its errors decrease slower.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/errors}
\caption{Comparison of relative error of decomposition w.r.t rank for HOSVD, CPD and TT.}
\label{fig:kotua:2}
\end{figure}

In terms of the computational complexity, HOSVD is a clear leader. CPD, on the other hand, is again an outsider. On the chart \ref{fig:kotua:3} the time axis is plotted in the logarithmic scale, so it can be seen, that CPD is slower than HOSVD by a factor of 100.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/times}
\caption{Comparison of complexity of decomposition w.r.t rank for HOSVD, CPD and TT.}
\label{fig:kotua:3}
\end{figure}

\subsubsection{Noisy Data}

Charts \ref{fig:kotua:4} - \ref{fig:kotua:6} show, how noise influences stability of algorithms. In general, algorithms behave identically: the error decreases with regards to rank when noise is insignificant. But when noise becomes large errors start to increase with regards to rank.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/cpd}
\caption{Relative error of CPD decomposition w.r.t to rank and standard deviation of Gaussian noise.}
\label{fig:kotua:4}
\end{figure}

Interestingly enough, regardless of noise amplitude, at low ranks errors decrease before reaching some threshold. After that point the growth trend takes its lead.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/hosvd}
\caption{Relative error of HOSVD decomposition w.r.t to rank and standard deviation of Gaussian noise.}
\label{fig:kotua:5}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/tt}
\caption{Relative error of TT decomposition w.r.t to rank and standard deviation of Gaussian noise.}
\label{fig:kotua:6}
\end{figure}

To summarize staiblity criterions, let's take a look on the chart \ref{fig:kotua:7}. It compares algorithms when small noise (std = 0.1) is added to the picture.As you can see from this chart, HOSVD is the most stable algorithm. CPD is also quite good at high ranks, but no so good at low ranks. Tensor Train is on par with HOSVD at low ranks, but it become less stable at high ranks.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{figures/stability}
\caption{Relative error of TT decomposition w.r.t to rank and standard deviation of Gaussian noise.}
\label{fig:kotua:7}
\end{figure}

\subsection{Conclusion}
Judging by all three parameters (precision, stability and complexity) HOSVD is the best algorithm. It's very fast, stable and on par with Tensor Train in terms of precision. Tensor Train has a bit more accurate results, but it's definitely slower and not so stable. CPD is computationally expensive and its error decreases very slow with regards to rank. Though it is quite stable when it comes to high ranks.

\end{document}


\subsection{Transition detection: mr1d\_pix}
Program {\em mr1d\_pix} detects the transitions in all scales at a
given position. The used wavelet transform is the Haar transform,
so a given wavelet coefficient at position $x$ and at scale $j$ ($j=1..P$,
$P$ being the number of scales)
is calculated from pixel values between  positions $x-2^{j}+1$ and $x$.
Only pixels in the signal which are on the left of a given position $x$
(or before a given time for temporal signal) are
used for the calculation of the wavelet coefficients at position $x$.
It allows us to detect new event in a temporal serie whatever the time
scale of the event.
By default, the analysed position is the last one of the signal, but other
position can as well being analysed using the "-x" option. The program
prints for each scale $j$ the following information corresponding to
the position $x$:
\begin{itemize}
\item {\bf No detection} \\
 if the wavelet coefficient $\mid w_j(x) \mid  < k \sigma_j$
\item {\bf New upward detection}\\
 if $  w_j(x) > k \sigma_j$  and $\mid w_j(x-1) \mid  < k \sigma_j$
\item {\bf New downward detection}\\
 if $  w_j(x) < - k \sigma_j$ and $\mid w_j(x-1) \mid  < k \sigma_j$
\item {\bf Positive significant structure}\\
 if $  w_j(x) > k \sigma_j$ and $\mid w_j(x-1) \mid  > k \sigma_j$ \\
The first detected coefficient of the structure is also given.
\item {\bf Negative significant structure}\\
 if $  w_j(x) < -k \sigma_j$ and $\mid w_j(x-1) \mid  > k \sigma_j$ \\
The first detected coefficient of the structure is also given.
\item {\bf End of significant structure}\\
 if $\mid w_j(x) \mid  < k \sigma_j$ and $\mid w_j(x-1) \mid  > k \sigma_j$
\end{itemize}
Furthermore the signal to noise ratio of the wavelet coefficient is given.
{\bf
\begin{center}
 USAGE: mr1d\_pix option signal\_in
\end{center}}
where options are:
\begin{itemize}
\item {\bf [-m type\_of\_noise]}
{\small
\begin{enumerate}
\baselineskip=0.4truecm
\item Gaussian Noise
\item Poisson Noise
\item Poisson Noise + Gaussian Noise
\item Multiplicative Noise
\item Non-stationary additive noise
\item Non-stationary multiplicative noise
\item Undefined stationary noise
\item Undefined noise
\end{enumerate}
}
Description in section~\ref{sect_filter}. Default is Gaussian noise.
\item {\bf [-g sigma]}
\item {\bf [-c gain,sigma,mean]}
\item {\bf [-n number\_of\_scales]}
\item {\bf [-s NSigma]}
\item {\bf [-n number\_of\_scales]}
\item {\bf [-x Position]}  \\
Position to analyse. Default is the last point.
\end{itemize}
\subsubsection*{Examples:}
\begin{itemize}
\item mr1d\_pix sig.dat  \\
Analyse the last point of the signal with all default option.
\item mr1d\_pix -x 55 -s 10 sig.dat  \\
Analyse the point at position 55, and detect the transition with a
signal to noise ratio equal to 10.
\end{itemize}