\chapter{Rings and ideals}
\section{Some motivational metaphors about rings vs groups}
In this chapter we'll introduce the notion of
a \textbf{commutative ring} $R$.
It is a larger structure than a group:
it will have two operations addition and multiplication,
rather than just one.
We will then immediately define a \textbf{ring homomorphism}
$R \to S$ between pairs of rings.

This time, instead of having normal subgroups $H \normalin G$,
rings will instead have subsets $I \subseteq R$ called \textbf{ideals},
which are not themselves rings but satisfy some niceness conditions.
We will then show how you to define $R/I$,
in analogy to $G/H$ as before.
Finally, like with groups, we will talk a bit about how to generate ideals.

Here is a possibly helpful table of analogies to help you keep track:
\begin{center}
	\begin{tabular}[h]{lcc}
		& Group & Ring \\
		\hline
		Notation & $G$ & $R$ \\
		Operations & $\cdot$ & $+$, $\times$ \\
		Commutativity & only if abelian & for us, always \\
		Sub-structure & subgroup & (not discussed) \\
		Homomorphism & grp hom.\ $G \to H$ & ring hom.\ $R \to S$ \\
		Kernel & normal subgroup & ideal \\
		Quotient & $G/H$ & $R/I$ \\
	\end{tabular}
\end{center}

\section{(Optional) Pedagogical notes on motivation}
I wrote most of these examples with a number theoretic eye in mind;
thus if you liked elementary number theory,
a lot of your intuition will carry over.
Basically, we'll try to generalize properties of the ring $\ZZ$ to
any abelian structure in which we can also multiply.
That's why, for example, you can talk about
``irreducible polynomials in $\QQ[x]$'' in the same
way you can talk about ``primes in $\ZZ$'',
or about ``factoring polynomials modulo $p$''
in the same way we can talk ``unique factorization in $\ZZ$''.
Even if you only care about $\ZZ$
(say, you're a number theorist), this has a lot of value:
I assure you that trying to solve $x^n+y^n = z^n$ (for $n > 2$)
requires going into a ring other than $\ZZ$!

Thus for all the sections that follow, keep $\ZZ$ in mind as your prototype.

I mention this here because
commutative algebra is \emph{also} closely tied to algebraic geometry.
Lots of the ideas in commutative algebra have nice
``geometric'' interpretations that motivate the definitions,
and these connections are explored in the corresponding part later.
So, I want to admit outright that this is not
the only good way (perhaps not even the most natural one)
of motivating what is to follow.

\section{Definition and examples of rings}
\prototype{$\ZZ$ all the way! Also $R[x]$ and various fields (next section).}

Well, I guess I'll define a ring\footnote{Or,
	according to some authors, a ``ring with identity'';
	some authors don't require rings to have multiplicative identity.
	For us, ``ring'' always means ``ring with $1$''.}.

\begin{definition}
	A \vocab{ring} is a triple $(R, +, \times)$,
	the two operations usually called addition and multiplication, such that
	\begin{enumerate}[(i)]
		\ii $(R,+)$ is an abelian group, with identity $0_R$, or just $0$.
		\ii $\times$ is an associative, binary operation on $R$ with some
		identity, written $1_R$ or just $1$.
		\ii Multiplication distributes over addition.
	\end{enumerate}
	The ring $R$ is \vocab{commutative} if $\times$ is commutative.
\end{definition}
\begin{abuse}
	As usual, we will abbreviate $(R, +, \times)$ to $R$.
\end{abuse}
\begin{abuse}
	For simplicity, assume all rings are commutative
	for the rest of this chapter.
	We'll run into some noncommutative rings eventually,
	but for such rings we won't need the full theory of this chapter anyways.
\end{abuse}

These definitions are just here for completeness.
The examples are much more important.
\begin{example}[Typical rings]
	\listhack
	\begin{enumerate}[(a)]
		\ii The sets $\ZZ$, $\QQ$, $\RR$ and $\CC$ are all rings
		with the usual addition and multiplication.
		\ii The integers modulo $n$ are also a ring
		with the usual addition and multiplication.
		We also denote it by $\Zc n$.
	\end{enumerate}
\end{example}

Here is also a trivial example.
\begin{definition}
	The \vocab{zero ring} is the ring $R$ with a single element.
	We denote the zero ring by $0$.
	A ring is \vocab{nontrivial} if it is not the zero ring.
\end{definition}
\begin{exercise}
	[Comedic]
	Show that a ring is nontrivial if and only if $0_R \ne 1_R$.
\end{exercise}

Since I've defined this structure, I may as well state the obligatory facts about it.
\begin{fact}
	For any ring $R$ and $r \in R$, $r \cdot 0_R = 0_R$.
	Moreover, $r \cdot (-1_R) = -r$.
\end{fact}

Here are some more examples of rings.
\begin{example}
	[Product ring]
	\label{ex:product_ring}
	Given two rings $R$ and $S$ the \vocab{product ring},
	denoted $R \times S$, is defined as ordered pairs $(r,s)$
	with both operations done component-wise.
	For example, the Chinese remainder theorem says
	that \[ \Zc{15} \cong \Zc3 \times \Zc5 \]
	with the isomorphism $n \bmod{15} \mapsto (n \bmod 3, n \bmod 5)$.
\end{example}
\begin{remark}
	Equivalently, we can define $R \times S$ as the abelian group $R \oplus S$,
	and endow it with the multiplication where $r \cdot s = 0$
	for $r \in R$, $s \in S$.
\end{remark}
\begin{ques}
	Which $(r,s)$ is the identity element of the product ring $R \times S$?
\end{ques}

\begin{example}[Polynomial ring]
	Given any ring $R$,
	the \vocab{polynomial ring} $R[x]$ is defined as the set of polynomials
	with coefficients in $R$:
	\[ R[x] = \left\{ a_n x^n+a_{n-1}x^{n-1}+\dots+a_0
		\mid a_0, \dots, a_n \in R \right\}. \]
	This is pronounced ``$R$ adjoin $x$''.
	Addition and multiplication are done exactly in the way you would expect.
\end{example}
\begin{remark}
	[Digression on division]
	Happily, polynomial division also does what we expect:
	if $p \in R[x]$ is a polynomial, and $p(a) = 0$,
	then $(x-a)q(x) = p(x)$ for some polynomial $q$.
	Proof: do polynomial long division.

	With that, note the caveat that
	\[ x^2-1 \equiv (x-1)(x+1) \pmod 8 \]
	has \emph{four} roots $1$, $3$, $5$, $7$ in $\Zc8$.

	The problem is that $2 \cdot 4 = 0$ even though $2$ and $4$ are not zero;
	we call $2$ and $4$ \emph{zero divisors} for that reason.
	In an \emph{integral domain} (a ring without zero divisors),
	this pathology goes away,
	and just about everything you know about polynomials carries over.
	(I'll say this all again next section.)
\end{remark}
\begin{example}
	[Multi-variable polynomial ring]
	We can consider polynomials in $n$ variables with coefficients in $R$,
	denoted $R[x_1, \dots, x_n]$.
	(We can even adjoin infinitely many $x$'s if we like!)
\end{example}

\begin{example}
	[Gaussian integers are a ring]
	The \vocab{Gaussian integers} are the set of complex numbers
	with integer real and imaginary parts, that is
	\[ \ZZ[i] = \left\{ a+bi \mid a,b \in \ZZ \right\}. \]
\end{example}
\begin{abuse}
	[Liberal use of adjoinment]
	Careful readers will detect some abuse in notation here.
	$\ZZ[i]$ should officially be
	``integer-coefficient polynomials in a variable $i$''.
	However, it is understood from context that $i^2=-1$;
	and a polynomial in $i = \sqrt{-1}$ ``is'' a Gaussian integer.
\end{abuse}
\begin{example}
	[Cube root of $2$]
	As another example (using the same abuse of notation):
	\[ \ZZ[\sqrt[3]{2}] = \left\{ a + b\sqrt[3]{2} + c\sqrt[3]4
		\mid a,b,c \in \ZZ \right\}. \]
\end{example}

\section{Fields}
\prototype{$\QQ$ is a field, but $\ZZ$ is not.}

Although we won't need to know what a field is until next chapter,
they're so convenient for examples I will go ahead and introduce them now.

As you might already know, if the multiplication is invertible,
then we call the ring a field.
To be explicit, let me write the relevant definitions.

\begin{definition}
	\label{def:unit}
	A \vocab{unit} of a ring $R$
	is an element $u \in R$ which is invertible:
	for some $x \in R$ we have $ux = 1_R$.
\end{definition}

\begin{example}
	[Examples of units]
	\listhack
	\begin{enumerate}[(a)]
	\ii The units of $\ZZ$ are $\pm 1$,
	because these are the only things which ``divide $1$''
	(which is the reason for the name ``unit'').
	\ii On the other hand, in $\QQ$ everything is a unit (except $0$).
	For example, $\frac 35$ is a unit since
	$\frac 35 \cdot \frac 53 = 1$.
	\ii The Gaussian integers $\ZZ[i]$ have four units:
	$\pm 1$ and $\pm i$.
	\end{enumerate}
\end{example}

\begin{definition}
	A nontrivial (commutative) ring is a \vocab{field}
	when all its nonzero elements are units.
\end{definition}

Colloquially, we say that
\begin{moral}
	A field is a structure where you can add, subtract, multiply, and divide.
\end{moral}
Depending on context, they are often denoted
either $k$, $K$, $F$.

\begin{example}
	[First examples of fields]
	\listhack
	\begin{enumerate}[(a)]
		\ii $\QQ$, $\RR$, $\CC$ are fields,
		since the notion $\frac 1c$ makes sense in them.
		\ii If $p$ is a prime, then $\Zc p$ is a field,
		which we denote will usually denote by $\FF_p$.
	\end{enumerate}
	The trivial ring $0$ is \emph{not} considered a field,
	since we require fields to be nontrivial.
\end{example}

%\begin{remark}
%	You might say at this point that ``fields are nicer than rings'',
%	but as you'll see in this chapter, the conditions for
%	being a field are somehow ``too strong''.
%	To give an example of what I mean:
%	if you try to think about the concept of ``divisibility''
%	in $\ZZ$, you've stepped into the vast and bizarre realm of
%	number theory.  Try to do the same thing in $\QQ$ and you get nothing:
%	any nonzero $a$ ``divides'' any nonzero $b$
%	because $b = a \cdot \frac ba$.
%
%	I know at least one person who instead
%	thinks of this as an argument for why people
%	shouldn't care about number theory
%	(studying chaos rather than order).
%\end{remark}

\section{Homomorphisms}
\prototype{$\ZZ \to \Zc5$ by modding out by $5$.}
This section is going to go briskly --
it's the obvious generalization of all the stuff
we did with quotient groups.\footnote{I once found an
	abstract algebra textbook which teaches rings before groups.
	At the time I didn't understand why,
	but now I think I get it -- modding out by things in
	commutative rings is far more natural, and you can start talking
	about all the various flavors of rings and fields.
	You also have (in my opinion) more vivid first examples
	for rings than for groups.
	I actually sympathize a lot with this approach --- maybe I'll convert
	Napkin to follow it one day.}

First, we define a homomorphism and isomorphism.
\begin{definition}
	Let $R = (R, +_R, \times_R)$ and $S = (S, +_S, \times_S)$ be rings.
	A \vocab{ring homomorphism} is a map $\phi : R \to S$
	such that
	\begin{enumerate}[(i)]
		\ii $\phi(x +_R y) = \phi(x) +_S \phi(y)$ for each $x,y \in R$.
		\ii $\phi(x \times_R y) = \phi(x) \times_S \phi(y)$ for each $x,y \in R$.
		\ii $\phi(1_R) = 1_S$.
	\end{enumerate}
	If $\phi$ is a bijection then $\phi$ is an \vocab{isomorphism}
	and we say that rings $R$ and $S$ are \vocab{isomorphic}.
\end{definition}
Just what you would expect.
The only surprise is that we also demand $\phi(1_R)$ to go to $1_S$.
This condition is not extraneous:
consider the map $\ZZ \to \ZZ$ called ``multiply by zero''.
\begin{example}
	[Examples of homomorphisms]
	\listhack
	\begin{enumerate}[(a)]
		\ii The identity map, as always.
		\ii The map $\ZZ \to \Zc5$ modding out by $5$.
		\ii The map $\RR[x] \to \RR$ by $p(x) \mapsto p(0)$
		by taking the constant term.
		\ii For any ring $R$, there is a trivial ring homomorphism $R \to 0$.
	\end{enumerate}
\end{example}
\begin{example}
	[Non-examples of homomorphisms]
	Because we require $1_R$ to $1_S$, some maps that you
	might have thought were homomorphisms will fail.
	\begin{enumerate}[(a)]
		\ii The map $\ZZ \to \ZZ$ by $x \mapsto 2x$
		is not a ring homomomorphism.
		Aside from the fact it sends $1$ to $2$,
		it also does not preserve multiplication.

		\ii If $S$ is a nontrivial ring,
		the map $R \to S$ by $x \mapsto 0$ is not
		a ring homomorphism, even though it preserves multiplication.

		\ii There is no ring homomorphism $\Zc{2016} \to \ZZ$ at all.
	\end{enumerate}
	In particular, whereas for groups $G$ and $H$
	there was always a trivial group homomorphism sending
	everything in $G$ to $1_H$, this is not the case for rings.
\end{example}

\section{Ideals}
\prototype{The multiples of $5$ are an ideal of $\ZZ$.}
Now, just like we were able to mod out by groups,
we'd also like to define quotient rings.
So once again,
\begin{definition}
	The \vocab{kernel} of a ring homomorphism $\phi \colon R \to S$,
	denoted $\ker \phi$, is the set of $r \in R$ such that $\phi(r) = 0$.
\end{definition}

In group theory, we were able to characterize the ``normal'' subgroups by a few
obviously necessary conditions (namely, $gHg\inv = H$).
We can do the same thing for rings, and it's in fact easier because our operations are commutative.

First, note two obvious facts:
\begin{itemize}
	\ii If $\phi(x) = \phi(y) = 0$, then $\phi(x+y) = 0$ as well.
	So $\ker \phi$ should be closed under addition.
	\ii If $\phi(x) = 0$, then for any $r \in R$ we have
	$\phi(rx) = \phi(r)\phi(x) = 0$ too.
	So for $x \in \ker \phi$ and \emph{any} $r \in R$,
	we have $rx \in \ker\phi$.
\end{itemize}

A (nonempty) subset $I \subseteq R$ is called
an ideal if it satisfies these properties.
That is,
\begin{definition}
	A nonempty subset $I \subseteq R$ is an \vocab{ideal}
	if it is closed under addition, and for each $x \in I$,
	$rx \in I$ for all $r \in R$.
	It is \vocab{proper} if $I \neq R$.
\end{definition}

Note that in the second condition, $r$ need not be in $I$!
So this is stronger than merely saying $I$ is closed under multiplication.
\begin{remark}
	If $R$ is not commutative, we also need the condition $xr \in I$.
	That is, the ideal is \emph{two-sided}: it absorbs multiplication
	from both the left and the right.
	But since rings in Napkin are commutative
	we needn't worry with this distinction.
\end{remark}

\begin{example}
	[Prototypical example of an ideal]
	Consider the set $I = 5\ZZ = \{\dots,-10,-5,0,5,10,\dots\}$ as an ideal in $\ZZ$.
	We indeed see $I$ is the kernel of the ``take mod $5$'' homomorphism:
	\[ \ZZ \surjto \ZZ/5\ZZ. \]
	It's clearly closed under addition,
	but it absorbs multiplication from \emph{all} elements of $\ZZ$:
	given $15 \in I$, $999 \in \ZZ$, we get $15 \cdot 999 \in I$.
\end{example}

\begin{exercise}
	[Mandatory: fields have two ideals]
	If $K$ is a field, show that $K$ has exactly two ideals.
	What are they?
	\label{exer:field_ideal}
\end{exercise}

Now we claim that these conditions are sufficient.
More explicitly,
\begin{theorem}
	[Ring analog of normal subgroups]
	Let $R$ be a ring and $I \subsetneq R$.
	Then $I$ is the kernel of some homomorphism if and only if it's an ideal.
\end{theorem}
\begin{proof}
	It's quite similar to the proof for the normal subgroup thing,
	and you might try it yourself as an exercise.

	Obviously the conditions are necessary.
	To see they're sufficient, we \emph{define} a ring by ``cosets''
	\[ S = \left\{ r + I \mid r \in R \right\}. \]
	These are the equivalence where we say $r_1 \sim r_2$ if $r_1 - r_2 \in I$
	(think of this as taking ``mod $I$'').
	To see that these form a ring, we have to check that the addition
	and multiplication we put on them is well-defined.
	Specifically, we want to check that if $r_1 \sim s_1$ and $r_2 \sim s_2$,
	then $r_1 + r_2 \sim s_1 + s_2$ and $r_1r_2 \sim s_1s_2$.
	We actually already did the first part
	-- just think of $R$ and $S$ as abelian
	groups, forgetting for the moment that we can multiply.
	The multiplication is more interesting.
	\begin{exercise}
		[Recommended]
		Show that if $r_1 \sim s_1$ and $r_2 \sim s_2$, then $r_1r_2 \sim s_1s_2$.
		You will need to use the fact that $I$ absorbs multiplication
		from \emph{any} elements of $R$, not just those in $I$.
	\end{exercise}
	Anyways, since this addition and multiplication is well-defined there
	is now a surjective homomorphism $R \to S$ with kernel exactly $I$.
\end{proof}

\begin{definition}
	Given an ideal $I$, we define as above the \vocab{quotient ring}
	\[ R/I \defeq \left\{ r+I \mid r \in R \right\}. \]
	It's the ring of these equivalence classes.
	This ring is pronounced ``$R$ mod $I$''.
\end{definition}
\begin{example}[$\ZZ/5\ZZ$]
	The integers modulo $5$ formed by ``modding out additively by $5$''
	are the $\Zc 5$ we have already met.
\end{example}
But here's an important point:
just as we don't actually think of $\ZZ/5\ZZ$ as consisting of
$k + 5\ZZ$ for $k=0,\dots,4$,
we also don't really want to think about $R/I$ as elements $r+I$.
The better way to think about it is
\begin{moral}
	$R/I$ is the result when we declare that elements of $I$ are all zero;
	that is, we ``mod out by elements of $I$''.
\end{moral}
For example, modding out by $5\ZZ$ means that we consider
all elements in $\ZZ$ divisible by $5$ to be zero.
This gives you the usual modular arithmetic!

\begin{exercise}
	Earlier, we wrote $\ZZ[i]$ for the Gaussian integers,
	which was a slight abuse of notation.
	Convince yourself that this ring
	could instead be written as $\ZZ[x] / (x^2+1)$,
	if we wanted to be perfectly formal.
	(We will stick with $\ZZ[i]$ though --- it's more natural.)

	Figure out the analogous formalization of $\ZZ[\sqrt[3]{2}]$.
\end{exercise}

\section{Generating ideals}
\prototype{In $\ZZ$, the ideals are all of the form $(n)$.}

Let's give you some practice with ideals.

An important piece of intuition is that once an ideal
contains a unit, it contains $1$, and
thus must contain the entire ring.
That's why the notion of ``proper ideal''
is useful language.
To expand on that:
\begin{proposition}
	[Proper ideal $\iff$ no units]
	Let $R$ be a ring and $I \subseteq R$ an ideal.
	Then $I$ is proper (i.e.\ $I \ne R$)
	if and only if it contains no units of $R$.
\end{proposition}
\begin{proof}
	Suppose $I$ contains a unit $u$, i.e.\ an element $u$
	with an inverse $u\inv$.
	Then it contains $u \cdot u\inv = 1$, and thus $I = R$.
	Conversely, if $I$ contains no units, it is obviously proper.
\end{proof}
As a consequence, if $K$ is a field,
then its only ideals are $(0)$ and $K$
(this was \Cref{exer:field_ideal}).
So for our practice purposes, we'll be working with rings that aren't fields.

First practice: $\ZZ$.
\begin{exercise}
	Show that the only ideals of $\ZZ$ are precisely those
	sets of the form $n\ZZ$, where $n$ is a nonnegative integer.
\end{exercise}

Thus, while ideals of fields are not terribly interesting,
ideals of $\ZZ$ look eerily like elements of $\ZZ$.
Let's make this more precise.
\begin{definition}
	Let $R$ be a ring.
	The \vocab{ideal generated} by a set of elements $x_1, \dots, x_n \in R$
	is denoted by $I = (x_1, x_2, \dots, x_n)$
	and given by
	\[ I = \left\{ r_1 x_1 + \dots + r_n x_n \mid r_i \in R \right\}.  \]
	One can think of this as ``the smallest ideal containing all the $x_i$''.
\end{definition}

The analogy of putting the $\{x_i\}$ in a sealed box and shaking vigorously
kind of works here too.
\begin{remark}
	[Linear algebra digression]
	If you know linear algebra,
	you can summarize this as: an ideal is an $R$-module.
	The ideal $(x_1, \dots, x_n)$ is the submodule spanned by $x_1, \dots, x_n$.
\end{remark}

In particular, if $I = (x)$ then $I$ consists of exactly the
``multiples of $x$'', i.e.\ numbers of the form $rx$ for $r \in R$.
\begin{remark}
	We can also apply this definition to infinite generating sets,
	as long as only finitely many of the $r_i$ are not zero
	(since infinite sums don't make sense in general).
\end{remark}

\begin{example}[Examples of generated ideals]
	\listhack
	\begin{enumerate}[(a)]
		\ii As $(n) = n\ZZ$ for all $ \in \ZZ$,
		every ideal in $\ZZ$ is of the form $(n)$.
		\ii In $\ZZ[i]$, we have
		$(5) = \left\{ 5a + 5b i \mid a,b \in \ZZ \right\}$.
		\ii In $\ZZ[x]$, the ideal $(x)$ consists of polynomials
		with zero constant terms.
		\ii In $\ZZ[x,y]$, the ideal $(x,y)$ again consists
		of polynomials with zero constant terms.
		\ii In $\ZZ[x]$, the ideal $(x,5)$ consists of polynomials
		whose constant term is divisible by $5$.
	\end{enumerate}
\end{example}
\begin{ques}
	Please check that the set
	$I = \left\{ r_1 x_1 + \dots + r_n x_n \mid r_i \in R \right\}$
	is indeed always an ideal (closed under addition,
	and absorbs multiplication).
\end{ques}
Now suppose $I = (x_1, \dots, x_n)$.
What does $R/I$ look like?
According to what I said at the end of the last section,
it's what happens when we ``mod out'' by each of the elements $x_i$.
For example\dots
\begin{example}
	[Modding out by generated ideals]
	\listhack
	\begin{enumerate}[(a)]
		\ii Let $R = \ZZ$ and $I = (5)$. Then $R/I$ is literally
		$\ZZ/5\ZZ$, or the ``integers modulo $5$'':
		it is the result of declaring $5 = 0$.
		\ii Let $R = \ZZ[x]$ and $I = (x)$.
		Then $R/I$ means we send $x$ to zero; hence $R/I \cong \ZZ$
		as given any polynomial $p(x) \in R$,
		we simply get its constant term.
		\ii Let $R = \ZZ[x]$ again and now let $I = (x-3)$.
		Then $R/I$ should be thought of as the quotient when $x-3 \equiv 0$,
		that is, $x \equiv 3$.
		So given a polynomial $p(x)$ its image after
		we mod out should be thought of as $p(3)$.
		Again $R/I \cong \ZZ$, but in a different way.
		\ii Finally, let $I = (x-3,5)$.
		Then $R/I$ not only sends $x$ to three, but also $5$ to zero.
		So given $p \in R$, we get $p(3) \pmod 5$.
		Then $R/I \cong \ZZ/5\ZZ$.
	\end{enumerate}
\end{example}
\begin{remark}
	[Mod notation]
	By the way, given an ideal $I$ of a ring $R$, it's totally legit to write
	\[ x \equiv y \pmod I \]
	to mean that $x-y \in I$.
	Everything you learned about modular arithmetic carries over.
\end{remark}

\section{Principal ideal domains}
\prototype{$\ZZ$ is a PID, $\ZZ[x]$ is not.
$\CC[x]$ is a PID, $\CC[x,y]$ is not.}

What happens if we put multiple generators in an ideal,
like $(10,15) \subseteq \ZZ$?
Well, we have by definition that $(10,15)$ is given as a set by
\[ (10,15) \defeq \left\{ 10x + 15y \mid x,y \in \ZZ \right\}.  \]
If you're good at number theory you'll instantly
recognize that this as $5\ZZ = (5)$.
Surprise! In $\ZZ$, the ideal $(a,b)$ is exactly $\gcd(a,b) \ZZ$.
And that's exactly the reason you often see the GCD of two numbers denoted $(a,b)$.

We call such an ideal (one generated by a single element) a \vocab{principal ideal}.
So, in $\ZZ$, every ideal is principal.
But the same is not true in more general rings.
\begin{example}
	[A non-principal ideal]
	In $\ZZ[x]$, $I = (x,2015)$ is \emph{not} a principal ideal.

	For if $I = (f)$ for some polynomial $f \in I$
	then $f$ divides $x$ and $2015$.
	This can only occur if $f = \pm 1$,
	but then $I$ contains $\pm1$, which it does not.
\end{example}
A ring with the property that all its ideals
are principal is called a \vocab{principal ideal ring}.
We like this property because they effectively
let us take the ``greatest common factor''
in a similar way as the GCD in $\ZZ$.

In practice, we actually usually care about
so-called \textbf{principal ideal domains (PID's)}.
But we haven't defined what a domain is yet.
Nonetheless, all the examples below are actually PID's,
so we will go ahead and use this word for now,
and tell you what the additional condition is in the next chapter.

\begin{example}
	[Examples of PID's]
	To reiterate, for now you should just verify
	that these are principal ideal rings,
	even though we are using the word PID.
	\begin{enumerate}[(a)]
		\ii As we saw, $\ZZ$ is a PID.

		\ii As we also saw, $\ZZ[x]$ is not a PID,
		since $I = (x,2015)$ for example is not principal.

		\ii It turns out that for a field $k$
		the ring $k[x]$ is always a PID.
		For example, $\QQ[x]$, $\RR[x]$, $\CC[x]$ are PID's.

		If you want to try and prove this,
		first prove an analog of Bezout's lemma,
		which implies the result.

		\ii $\CC[x,y]$ is not a PID, because $(x,y)$
		is not principal.
	\end{enumerate}
\end{example}

\section{Noetherian rings}
\prototype{$\ZZ[x_1, x_2, \dots]$ is not Noetherian,
	but most reasonable rings are.
	In particular polynomial rings are.
	(Equivalently, only weirdos care about non-Noetherian rings).}

If it's too much to ask that an ideal is generated by \emph{one} element,
perhaps we can at least ask that our ideals
are generated by \emph{finitely many} elements.
Unfortunately, in certain weird rings this is also not the case.
\begin{example}
	[Non-Noetherian ring]
	Consider the ring $R = \ZZ[x_1, x_2, x_3, \dots]$
	which has \emph{infinitely} many free variables.
	Then the ideal $I = (x_1, x_2, \dots) \subseteq R$
	cannot be written with a finite generating set.
\end{example}
Nonetheless, most ``sane'' rings we work in
\emph{do} have the property that their ideals are finitely generated.
We now name such rings and give two equivalent definitions:
\begin{proposition}[The equvialent definitions of a Noetherian ring]
	For a ring $R$, the following are equivalent:
	\begin{enumerate}[(a)]
		\ii Every ideal $I$ of $R$ is finitely generated
		(i.e.\ can be written with a finite generating set).
		\ii There does \emph{not} exist an
		infinite ascending chain of ideals
		\[ I_1 \subsetneq I_2 \subsetneq I_3 \subsetneq \dots. \]
		The absence of such chains is often
		called the \vocab{ascending chain condition}.
	\end{enumerate}
	Such rings are called \vocab{Noetherian}.
\end{proposition}

\begin{example}
	[Non-Noetherian ring breaks ACC]
	In the ring $R = \ZZ[x_1, x_2, x_3, \dots]$ we have
	an infinite ascending chain
	\[ (x_1) \subsetneq (x_1, x_2) \subsetneq (x_1,x_2,x_3) \subsetneq \dots. \]
\end{example}
From the example, you can kind of see why the proposition is true:
from an infinitely generated ideal you can extract an ascending chain
by throwing elements in one at a time.
I'll leave the proof to you if you want to
do it.\footnote{On the other hand, every undergraduate
	class in this topic I've seen makes you do it as homework.
	Admittedly I haven't gone to that many such classes.}

\begin{ques}
	Why are fields Noetherian?
	Why are PID's (such as $\ZZ$) Noetherian?
\end{ques}

This leaves the question:
is our prototypical non-example of a PID,
$\ZZ[x]$, a Noetherian ring?
The answer is a glorious yes,
according to the celebrated Hilbert basis theorem.
\begin{theorem}[Hilbert basis theorem]
	Given a Noetherian ring $R$,
	the ring $R[x]$ is also Noetherian.
	Thus by induction, $R[x_1, x_2, \dots, x_n]$ is Noetherian
	for any integer $n$.
	\label{thm:hilbert_basis}
\end{theorem}
The proof of this theorem is really olympiad flavored,
so I couldn't possibly spoil it -- I've
left it as a problem at the end of this chapter.

Noetherian rings really shine in algebraic geometry,
and it's a bit hard for me to motivate them right now,
other than to say
``most rings you'll encounter are Noetherian''.
Please bear with me!

\section{\problemhead}

\begin{problem}
	The ring $R = \RR[x] / (x^2+1)$ is one that you've seen before.
	What is its name?
	\begin{hint}
		$R = \RR[i]$.
	\end{hint}
	\begin{sol}
		This is just $\RR[i] = \CC$.
		The isomorphism is given by $x \mapsto i$,
		which has kernel $(x^2+1)$.
	\end{sol}
\end{problem}

\begin{problem}
	Show that $\CC[x] / (x^2-x) \cong \CC \times \CC$.
	\begin{hint}
		The isomorphism is given by $x \mapsto (1,0)$
		and $1-x \mapsto (0,1)$.
	\end{hint}
\end{problem}

\begin{problem}
	In the ring $\ZZ$, let $I = (2016)$ and $J = (30)$.
	Show that $I \cap J$ is an ideal of $\ZZ$ and compute its elements.
\end{problem}

\begin{sproblem}
	\label{prob:inclusion_preserving}
	Let $R$ be a ring and $I$ an ideal.
	Find an inclusion-preserving bijection between
	\begin{itemize}
		\ii ideals of $R/I$, and
		\ii ideals of $R$ which contain $I$.
	\end{itemize}
\end{sproblem}

\begin{problem}
	Let $R$ be a ring.
	\begin{enumerate}[(a)]
		\ii Prove that there is exactly one ring homomorphism $\ZZ \to R$.
		\ii Prove that the number of ring homomorphisms
		$\ZZ[x] \to R$ is equal to the number of elements of $R$.
	\end{enumerate}
	\begin{hint}
		For (b) homomorphism is uniquely determined by the choice of $\psi(x) \in R$
	\end{hint}
\end{problem}

%\begin{problem}
%	[$\phi(1_R) = 1_S$ is really necessary]
%	Find two rings $R$ and $S$ and a \emph{nonzero} function
%	$f \colon R \to S$ such that
%	\begin{itemize}
%		\ii $f(x+y) = f(x) + f(y)$ for $x,y \in R$,
%		\ii $f(xy) = f(x) f(y)$ for $x,y \in R$,
%		\ii $f(1_R) \ne 1_S$ (i.e.\ $f$ is not a ring homomorphism).
%	\end{itemize}
%	\begin{hint}
%		Take $S = R \times R$ with $R$ nonzero.
%	\end{hint}
%	\begin{sol}
%		The map $R \to R \times R$ by $x \mapsto (x,0)$ will always work,
%		with $R$ nonzero.
%	\end{sol}
%\end{problem}

\begin{problem}
	\gim
	Prove the Hilbert basis theorem, \Cref{thm:hilbert_basis}.
\end{problem}

\begin{problem}
	[USA Team Selection Test 2016]
	Let $\FF_p$ denote the integers modulo a fixed prime number $p$.
	Define $\Psi \colon \FF_p[x] \to \FF_p[x]$ by
	\[ \Psi\left( \sum_{i=0}^n a_i x^i \right) = \sum_{i=0}^n a_i x^{p^i}. \]
	Let $S$ denote the image of $\Psi$.
	\begin{enumerate}[(a)]
		\ii Show that $S$ is a ring with addition
		given by polynomial addition,
		and multiplication given by \emph{function composition}.
		\ii Prove that $\Psi \colon \FF_p[x] \to S$
		is then a ring isomorphism.
	\end{enumerate}
\end{problem}

\begin{problem} % from Brian Chen
	\yod
	Let $A \subseteq B \subseteq C$ be rings.
	Suppose $C$ is a finitely generated $A$-module.
	Does it follow that $B$ is a finitely generated $A$-module?
	% Assume $A$ is Noetherian. Show that $B$ is finitely generated as an $A$-module.
	% Find a counterexample where $A$ is not Noetherian.
	\begin{hint}
		I think the result is true if you add the assumption $A$ is Noetherian,
		so look for trouble by picking $A$ not Noetherian.
	\end{hint}
	\begin{sol}
		Nope! Pick
		\begin{align*}
			A &= \ZZ[x_1, x_2, \dots] \\
			B &= \ZZ[x_1, x_2, \dots, \eps x_1, \eps x_2, \dots] \\
			C &= \ZZ[x_1, x_2, \dots, \eps].
		\end{align*}
		where $\eps \neq 0$ but $\eps^2 = 0$.
		I think the result is true if you add the assumption $A$ is Noetherian.
	\end{sol}
\end{problem}


\section{Model Description}
This module is a simulation environment module which simulates the analog voltage interface of a RW cluster.  The input is an array of voltages $V_{i}$.  The Reaction Wheel (RW) motor torque $u_{s_{i}}$ is evaluated using a linear mapping
\begin{equation}
u_{s_{i}} = V_{i} \gamma_{i} SF_{i} + b_{i}
\end{equation}
where $\gamma$ is constant value gain. SF is the scale factor error (i.e. a constant 1\% gain error SF = 1.01). $b$ is the bias term (i.e. a constant 1 Nm error on torque b = 1.)  The output of the module is an array of RW motor torques.  The deadband and saturation behavior of the RW speed is modeled inside the RW dynamics model.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PROBLEM 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Problem 1}

Consider an infinite slab of material described by diffusion coefficient $D$ and macroscopic cross section $\Sigma_a$.
The material extends infinitely in two dimensions, but has a vacuum boundaries at $x=a$ and $x=0$.
At $x=0$ there is also a uniformly distributed source plane with strength $s''$ [neutrons per area].
Find the flux in this geometry.
(You may use the substitution $L = \sqrt{\frac{D}{\Sigma_a}}$.)


% -*- compile-command: "stack exec --package diagrams-lib --package diagrams-pgf --package diagrams-contrib --package diagrams-builder --package palette -- pdflatex --enable-write18 groups.tex" -*-
\documentclass{article}

\usepackage{hyperref}
\usepackage{url}
\usepackage{amsmath}

\usepackage[outputdir=diagrams, extension=pgf, backend=pgf, input]{diagrams-latex}
\usepackage{pgf}

\usepackage{graphicx}
\graphicspath{{images/}}

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

\title{Operation tables}
\author{Brent Yorgey, \href{http://www.mathlesstraveled.com}{\texttt{mathlesstraveled.com}} \\ \raisebox{-0.4em}{\includegraphics[width=44px]{../CC-BY.png}} \href{http://creativecommons.org/licenses/by/4.0/}{\texttt{creativecommons.org/licenses/by/4.0/}}}

\begin{document}

\maketitle

\fontsize{16}{20}\selectfont

In this activity you're going to fill out some \emph{operation
  tables}.  Each table will have some items listed across the top and
side.  For a given operation, your job is to fill each square with the
result of that operation on the items in the corresponding row and
column.  For example, if we list $2$, $3$, and $5$ along the side and
top, and the operation is addition, then the operation table would
look like this:

\begin{center}
\begin{tabular}{l|lll}
$+$ & 2 & 3 & 5  \\ \hline
2   & 4 & 5 & 7  \\
3   & 5 & 6 & 8  \\
5   & 7 & 8 & 10
\end{tabular}
\end{center}

\section*{XOR}

Fill in each spot in the table with the XOR of the two Boolean values.

\begin{center}
\begin{tabular}{l|ll}
$\oplus$ & F & T \\ \hline
F        &   &   \\
T        &   &
\end{tabular}
\end{center}

\section*{Addition}

Fill in the table using addition.

\begin{center}
\begin{tabular}{l|lll}
$+$ & 0 & 1 & 2 \\ \hline
0        &   &   &   \\
1        &   &   &   \\
2        &   &   &
\end{tabular}
\end{center}

\section*{Addition modulo 3}

Fill in the table using \emph{addition modulo $3$}.  That is, imagine
that the number line ``wraps around'' back to $0$ when it gets to
$3$.  If you get an answer bigger than $3$, subtract $3$ from it until
you are back down under $3$.  For example, $2 +_3 2 = 1$ since $2 + 2
= 4$ and $4 - 3 = 1$.

\begin{center}
\begin{tabular}{l|lll}
$+_3$ & 0 & 1 & 2 \\ \hline
0        &   &   &   \\
1        &   &   &   \\
2        &   &   &
\end{tabular}
\end{center}

\section*{Addition modulo 5}

\begin{center}
\begin{tabular}{l|lllll}
$+_5$ & 0 & 1 & 2 & 3 & 4 \\ \hline
0        &   &   &   &   &   \\
1        &   &   &   &   &   \\
2        &   &   &   &   &   \\
3        &   &   &   &   &   \\
4        &   &   &   &   &
\end{tabular}
\end{center}

\section*{Max}

Fill in the table using the $\max$ operation, which returns the
largest of its two inputs.

\begin{center}
\begin{tabular}{l|lll}
$\max$ & 0 & 1 & 2 \\ \hline
0      &   &   &   \\
1      &   &   &   \\
2      &   &   &
\end{tabular}
\end{center}

\newpage

\section*{Multiplication modulo 5}

Multiplication modulo $5$ works just like addition modulo $5$, but
with multiplication: to compute $a \times_5 b$, first multiply $a$ and
$b$, then keep subtracting $5$ until you get something smaller than
$5$.  For example, $3 \times_5 4 = 2$ since $3 \times 4 = 12$ and $12
- 5 - 5 = 2$.

\begin{center}
\begin{tabular}{l|llll}
$\times_5$ & 1 & 2 & 3 & 4 \\ \hline
1          &   &   &   &   \\
2          &   &   &   &   \\
3          &   &   &   &   \\
4          &   &   &   &
\end{tabular}
\end{center}

\newpage

Cut out the square on this page.  Ideally, you will also be able to
see the letter F through the back of the paper.  If you can't see it
very well you are welcome to draw in a backwards F on the back that
matches up with the F on the front. \vspace{1in}

\begin{center}
\begin{diagram}[width=300]
dia = text "F" # fontSizeL 0.25 <> square 1
\end{diagram}
\end{center}

\newpage

\newcommand{\elt}[1]{\raisebox{-0.25em}{\includegraphics[height=1.2em]{#1.png}}}

Let's list all the different things we can do to the square so that it
ends up still being a square in the same orientation (that is, we want
to list all the \emph{symmetries} of the square).  Each icon on the
next page represents an operation we can do to the square.  For
example, the \elt{dot} in the first row means to do nothing; the \elt{r1} in
the second row means to rotate the square by 1/4 turn, and so on.  On
the right of the colon is a graphical depiction of the operation.  If
you start your square with the letter F facing you and perform the
operation, it should end up looking like the square at the end.  For
example, the row for \elt{r1} looks like \elt{r1row}, meaning that if
you start with \elt{f} and perform the \elt{r1} operation, the square
ends up looking like \elt{r1f}.  You should physically do all the
operations with your square to make sure you understand how they work!

\begin{center}
  \includegraphics[width=3in]{d4.png}
\end{center}

\newpage

We can also \emph{combine} two operations on the square by doing first
one and then the other.  In the end this will have the same effect as
if we had done a single operation.  For example, flipping the square
vertically and then flipping it vertically again will put it back the
way it started, so

\begin{center}
  \includegraphics[height=0.5in]{vv.png}
\end{center}

As another example,

\begin{center}
  \includegraphics[height=0.5in]{dh.png}
\end{center}

(Try this with your physical square and make sure you understand it!)

\newpage

Fill in the table below, using the combining operation explained on
the previous page.

\begin{center}
  \includegraphics[width=5in]{table.png}
\end{center}

What patterns do you notice?

\end{document}


\subsection{Subdifferentials}\label{subsec:subdifferentials}

Let \( X \) be a Hausdorff \hyperref[def:topological_vector_space]{topological vector space}, let \( D \subseteq X \) be an open set and \( f: D \to \BbbR \) be any function.

\begin{definition}\label{def:subdifferentials}
  We fix a point \( x \in D \). We define different types of \term{subgradients} and \term{subdifferentials}. Subgradients are linear functionals \( x^* \in X^* \) that approximate \( f \) at the point \( x \) in a certain way, and a subdifferential is the set of all subgradients of a given type.

  \begin{thmenum}
    \thmitem{def:subdifferentials/convex}\mcite[59]{Clarke2013}We say that \( x^* \in X^* \) is a \term{subgradient of \( f \) at \( x \)} if for every \( y \in D \) we have
    \begin{equation*}
      f(y) - f(x) \geq \inprod {x^*} {y - x}.
    \end{equation*}

    The \term{subdifferential of \( f \) at \( x \)} is denoted by \( \partial f(x) \) and is also sometimes called the \term{convex subdifferential} because of \fullref{thm:convex_iff_subdifferential_nonempty}.

    \thmitem{def:subdifferentials/clarke}\mcite[def. 10.3]{Clarke2013}We say that \( x^* \in X^* \) is a \term{Clarke (generalized) subgradient of \( f \) at \( x \)} if for every direction \( h \in X \) we have
    \begin{equation*}
      f^\circ(x)(h) \geq \inprod {x^*} h,
    \end{equation*}
    where \( f^\circ(x)(h) \) is the generalized Clarke \hyperref[def:nonsmooth_derivatives/clarke]{derivative}.

    The \term{subdifferential of \( f \) at \( x \)} is denoted by \( \partial_C f(x) \). Confusingly, the Clarke subdifferential is called the \enquote{generalized gradient} by Clarke himself with no special name for the Clarke subgradients.

    See \fullref{subsec:clarke_gradients} for properties of these subgradients.

    \thmitem{def:subdifferentials/proximal}\mcite[227]{Clarke2013}We say that \( x^* \in X^* \) is a \term{proximal subgradient of \( f \) at \( x \)} if there exist \( \sigma > 0 \) and a neighborhood \( V \subseteq X \) of \( x \) such that for every \( y \in D \cap V \) we have
    \begin{equation*}
      f(y) - f(x) + \sigma \norm{y - x}^2 \geq \inprod {x^*} {y - x}.
    \end{equation*}

    The \term{proximal subdifferential of \( f \) at \( x \)} is denoted by \( \partial_P f(x) \).

    \thmitem{def:subdifferentials/limiting}\mcite[def. 11.10]{Clarke2013}Suppose the following are satisfied:
    \begin{enumerate}
      \item \( \{ x_n \}_n \subseteq D \) is a sequence of points converging to \( x \)
      \item \( f(x_n) \to f(x) \) (redundant if \( f \) is continuous)
      \item \( x_n^* \) is a proximal subgradient for \( f \) at \( x_n \) for every \( n \in \BbbZ_{>0} \).
    \end{enumerate}

    If the limit \( x^* \coloneqq \lim_n x_n^* \) exists and is a continuous linear functional, we call \( x^* \) a \term{limiting subgradient of \( f \) at \( x \)}.

    The \term{limiting subdifferential of \( f \) at \( x \)} is denoted by \( \partial_P f(x) \).
  \end{thmenum}
\end{definition}


\documentclass{article}
\usepackage{graphicx}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{geometry}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage{float}
\usepackage{cite}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{titletoc}

\geometry{left=3cm,right=3cm,top=3cm,bottom=3cm}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\var}{var}
\DeclareMathOperator*{\expec}{E}

\begin{document}
\title{\textsf{Homework 2 for Pattern Recognition}}
\author{Fan JIN\quad (2015011506)}
\maketitle

\section*{Question 1}
{
    \subsection*{(1)}
    {
        The error function is minimized when optimized:
        $$\frac{\partial}{\partial w_0} E(w, w_0) = \sum_{i=1}^{n} {(w^T x_i + w_0 - t_i)} $$
        $$= n w^T m + nw_0 + n_1 \frac{n}{n_1} + n_2 \frac{-n}{n_2} = n ( w^T m + w_0 ) = 0,$$
        which yields $$w_0 = -w^T m$$.
    }

    \subsection*{(2)}
    {
        Denote the observation matrix and the response matrix as
        $$x = [x_1-m, x_2-m, \cdots, x_n-m]$$ and $$t = [t_1, t_2, \cdots, t_n].$$
        Plug in the optimal $w_0 = -w^T m$, and the error function can thus be expressed as
        $$E(w) = \frac{1}{2} (w^T x - t)(w^T x - t)^T.$$

        By matrix calculus\footnote{See https://en.wikipedia.org/wiki/Matrix\_calculus}, we have
        $$\frac{\partial}{\partial w} E(w) = -x (t^T - x^T w) = 0,$$
        that is, $$x x^T w = x t^T.$$

        Note that $$x t^T = \left[ \sum_{i=1}^{n} {(x_i t_i)} \right] - m \cdot \sum_{i=1}^{n} {x_i} $$
        $$= \left[ \left(n_1 \frac{n}{n_1} m_1 + n_2 \frac{-n}{n_2} m_2 \right) \right] - m \cdot \left( n_1 \frac{n}{n_1} + n_2 \frac{-n}{n_2} \right) = n (m_1 - m_2),$$ and
        $$m = \frac{n_1}{n}m_1 + \frac{n_2}{n}m_2 = m_1 - \frac{n_2}{n}(m_1-m_2) = m_2 + \frac{n_1}{n}(m_1-m_2),$$ we have
        $$x x^T = \sum_{i\in C_1}{(x_i - m_1 + \frac{n_2}{n}(m_1-m_2))(x_i - m_1 + \frac{n_2}{n}(m_1-m_2))^T} $$$$+ \sum_{i\in C_2}{(x_i - m_2 - \frac{n_1}{n}(m_1-m_2))(x_i - m_2 - \frac{n_1}{n}(m_1-m_2))^T}$$
        $$= \sum_{i\in C_1}{(x_i - m_1)(x_i - m_1)^T} + \sum_{i\in C_2}{(x_i - m_2)(x_i - m_2)^T} + \frac{n_1 n_2}{n^2} \sum_{i=1}^{n} {(m_1 - m_2)(m_1 - m_2)^T}$$
        $$= S_w + \frac{n_1 n_2}{n} S_B.$$

        Therefore, we proved that $$\left( S_w + \frac{n_1 n_2}{n} S_B \right) w = n (m_1 - m2)$$ when $w$ is optimal.

    }

    \subsection*{(3)}
    {
        Note that $$S_B w = (m_1 - m_2) (m_1 - m_2)^T w = (m_1 - m_2) \cdot \left[ (m_1 - m_2)^T w \right],$$ and that $(m_1 - m_2)^T w$ is a scalar. Therefore, the vector $S_B w$ is proportional to $m_1-m_2$, which means $$w \propto S_w^{-1} (m_1 - m_2).$$
    }
}

\section*{Question 2}
{
    \subsection*{Data Visualization}
    {
        Since the dimension of original data is high, we apply PCA (Principal Component Analysis) and extract the first two principal compenents for a scatter plot.

        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{pca.png}
            \caption{Scatter plot}
        \end{figure}

        The data seems linearly separable, although there are a few outliers.
    }

    \subsection*{Logistic Regression}
    {
        I implement the algorithm on my own, using a loss function based on cross entropy
        $$L(\theta) = - \sum_{i} {\left[ y_i \log{(h_\theta (x_i))} + (1 - y_i) \log{(1 - h_\theta (x_i))}\right]},$$
        where $$h_\theta (x_i) = \frac{1}{1 + \exp{(- \theta^T x_i)}}.$$

        Gradient descent is employed to solve the optimal parameter $\theta$. A column of ones is attached to the data matrix, in order not to write the intercept in the expression.

        The error rate is $3.33\%$ on the validation set.

    }

    \subsection*{Fisher's Discriminant}
    {
        I implement the algorithm on my own, using the formula in Question 1.

        The error rate is $1.43\%$ on the validation set.

    }

    \subsection*{Discussion}
    {
        Why does the Fisher's method have better performance than logistic regression?

        Their main difference is their loss functions. Logistic regression uses the cross entropy, while the Fisher's method adopts the ordinary quadratic loss, or the sum of squared errors (SSE). With outliers in consideration, the cross entropy tends to punish more on outliers, compared to the SSE loss function, and therefore, is more likely to result in over fitting.

        Another reason is that the Fisher's method considers the variance of two categories. It can predict the data distribution well if the positive samples have a variance different from that of the negative samples.

    }
}

\section*{Source Code}
{
    Please download the souece code from http://39.106.23.58/files/PR2\_2015011506.7z
}

\clearpage
\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\title{Method of Moments for Mixture of Gaussians}
\author{Yao Zhu\thanks{yzhu221@bloomberg.net}\\ Derivatives Pricing, Bloomberg LP}
\date{}

\begin{document}
\maketitle

\section{Mixture of Spherical Gaussians with the same variance parameter $\sigma^2$}
For clarity of presenting the idea of method of moments, we consider the mixture of spherical Gaussians with the same covariance. Under this setting, a random variable $X\in R^{d}$ is generated as follows:
\begin{align}
p(Z=i) &= w_i
\label{eq-mog-gen}
\\
\nonumber
X|Z=i &\sim \mathcal{N}(\mu_i,\sigma^2 I_{d})
\end{align}
for $i=1,\ldots,k$, where $k$ is a known number and $k\leq d$, $I_d$ is the $d\times d$ identity matrix, and $\sigma^2$ is the variance parameter common to all $k$ components. We further assume the following non-degeneracy condition:
\par
\textbf{Non-degeneracy Condition}: $\mu_i,i=1,\ldots,k$ are linearly independent, and $w_i>0$ for all $i=1,\ldots,k$.
\par
We target to recover the parameters $\{w_i,\mu_i\}_{i=1}^k$ and $\sigma^2$ from a sample of data points $\{x_n\}_{n=1}^N$ generated according to \eqref{eq-mog-gen}.
\section{Raw Moments}
\subsection{Tensor product}
In order to work with moments (especially moments of order higher than $2$), we need to the notion of \textit{tensor product}, denoted by $\otimes$. Let $X_1,X_2,X_3\in R^{d}$, we define $X_1\otimes X_2\otimes X_3$ such that
\begin{align}
\left(X_1\otimes X_2\otimes X_3\right)_{jlm} &= (X_1)_{j}(X_2)_{l}(X_3)_{m}
\end{align}
for $j,l,m=1,\ldots, d$. In a similar style, we define $X_1\otimes X_2=X_1 X_2^{T}$.
\subsection{Moments of Mixture of Gaussians}
Given \eqref{eq-mog-gen}, when $Z=i$ we can write and
\begin{align}
X&=\mu_i + Y \\
\nonumber
Y&\sim \mathcal{N}(0,\sigma^2 I_{d})
\end{align}
We can compute the following raw moments up to order $3$
\begin{align}
\mathbb{E}[X]&=\sum_{i=1}^k w_i \mu_i
\end{align}
We will denote $M_1 = \mathbb{E}[X]$ in the following.
\begin{align}
\mathbb{E}[X\otimes X]&=\sum_{i=1}^k w_i \mathbb{E}[X\otimes X|Z=i]\\
&= \sum_{i=1}^k w_i \mathbb{E}[(\mu_i + Y)\otimes (\mu_i + Y)]\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i +  \sigma^2 I_d
\end{align}
Note we have used the fact that $\mathbb{E}[Y]=0$, and $\mathbb{E}[Y\otimes Y]=\sigma^2 I_d$. Plus the fact that $\mathbb{E}[Y\otimes Y\otimes Y]=0$, we compute the $3$rd order moment
\begin{align}
\mathbb{E}[X\otimes X \otimes X]&=\sum_{i=1}^k w_i \mathbb{E}[X\otimes X\otimes X|Z=i]\\
&= \sum_{i=1}^k w_i \mathbb{E}[(\mu_i + Y)\otimes (\mu_i + Y)\otimes (\mu_i + Y)]\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i\otimes \mu_i + \\
\nonumber
& \sum_{i=1}^k w_i\left( \mathbb{E}[\mu_i\otimes Y\otimes Y] + \mathbb{E}[ Y\otimes\mu_i\otimes Y] + \mathbb{E}[Y\otimes Y\otimes\mu_i]\right)
\end{align}
Now let's take a closer look at the term
\begin{align*}
\sum_{i=1}^k w_i  \mathbb{E}[ Y\otimes\mu_i\otimes Y] &= \mathbb{E}[ Y\otimes(\sum_{i=1}^k w_i\mu_i)\otimes Y]\\
&= \mathbb{E}[ Y\otimes M_1\otimes Y]
\end{align*}
In order to further simplify it, we look at a particular cell
\begin{align*}
\mathbb{E}[ (Y\otimes M_1\otimes Y)_{jlm}] &= (M_1)_l \mathbb{E}[ Y_j Y_m]\\
&= (M_1)_l \sigma^2 \delta_{jm}
\end{align*}
where $\delta_{jm}$ is the Kronecker delta. Thus, in tensor form we have
\begin{align}
\mathbb{E}[ Y\otimes M_1\otimes Y] &= \sigma^2\sum_{j=1}^d e_j \otimes M_1 \otimes e_j
\end{align}
where $\{e_1,\ldots,e_d\}$ is the canonical basis of $d$ dimension. Similarly we have
\begin{align}
\mathbb{E}[ M_1 \otimes Y\otimes Y] &= \sigma^2\sum_{j=1}^d  M_1 \otimes e_j \otimes e_j\\
\mathbb{E}[Y\otimes Y\otimes M_1] &= \sigma^2\sum_{j=1}^d   e_j \otimes e_j \otimes M_1
\end{align}
Thus, in summary we have
\begin{align}
\mathbb{E}[X\otimes X \otimes X]&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i\otimes \mu_i +\\
\nonumber
&\sigma^2\sum_{j=1}^d  (M_1 \otimes e_j \otimes e_j + e_j \otimes M_1 \otimes e_j + e_j \otimes e_j \otimes M_1)
\end{align}
\section{Parameter identification from the moments}
From the data sample $\{x_n\}_{n=1}^N$, we can compute the empirical moments $\widetilde{\mathbb{E}}[X]$, $\widetilde{\mathbb{E}}[X\otimes X]$, and $\widetilde{\mathbb{E}}[X\otimes X \otimes X]$ as estimates of the theoretical moments. For this reason, we say $\mathbb{E}[X]$, $\mathbb{E}[X\otimes X]$, and $\mathbb{E}[X\otimes X \otimes X]$ are observable. We want to come up with a recipe to identify the parameters $\{w_i,\mu_i\}_{i=1}^k$ and $\sigma^2$ from these observable moments.
\subsection{Identify $\sigma^2$}
Let's compute the covariance matrix of $X$
\begin{align}
cov(X)&=\mathbb{E}[(X-M_1)\otimes (X-M_1)]\\
&=\sum_{i=1}^k w_i \mathbb{E}[(X-M_1)\otimes (X-M_1)|Z=i]\\
&=\sum_{i=1}^k w_i \mathbb{E}[(\mu_i-M_1+Y)\otimes (\mu_i-M_1+Y)]\\
&=\sum_{i=1}^k w_i (\mu_i-M_1)\otimes (\mu_i-M_1) + \sigma^2 I_d
\label{eq-cov-X}
\end{align}
Note that because $\sum_{i=1}^k w_i (\mu_i-M_1)=0$, the $k$ vectors $(\mu_i-M_1)$ for $i=1,\ldots,k$ are linearly dependent. Thus, from \eqref{eq-cov-X} we know $\sigma^2=\lambda_{\min}(cov(X))$, i.e., $\sigma^2$ is the smallest eigenvalue of $cov(X)$. Note because $cov(X)=\mathbb{E}[X\otimes X]-M_1\otimes M_1$, $cov(X)$ is also observable.
\subsection{Identify $\{w_i,\mu_i\}_{i=1}^k$}
We define the following two purified moments
\begin{align}
M_2&=\mathbb{E}[X\otimes X] - \sigma^2 I_d
\label{eq-M2-obs}
\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i
\label{eq-M2-tensor-decomp}
\end{align}
\begin{align}
M_3&=\mathbb{E}[X\otimes X\otimes X] - \sigma^2\sum_{j=1}^d  (M_1 \otimes e_j \otimes e_j + e_j \otimes M_1 \otimes e_j + e_j \otimes e_j \otimes M_1)
\label{eq-M3-obs}
\\
&=\sum_{i=1}^k w_i \mu_i\otimes \mu_i \otimes \mu_i
\label{eq-M3-tensor-decomp}
\end{align}
Once we have identified $\sigma^2$, $M_2$ is observable thanks to equation \eqref{eq-M2-obs}, and $M_3$ is observable thanks to equation \eqref{eq-M3-obs}. In the \textbf{Non-degeneracy Condition}, we only assume $\mu_i,i=1,\ldots,k$ to be linearly independent, which is not strong enough for us to extract $\mu_i$ directly through the tensor decomposition in equation \eqref{eq-M3-tensor-decomp}. We want to cook up another tensor that admits an \textit{orthogonal tensor decomposition}, on which we can apply the \textit{tensor power method}. From \eqref{eq-M2-tensor-decomp}, we see that $M_2$ is a symmetric positive semidefinite matrix with rank $k$. Thus, it admits a thin eigendecomposition
\begin{align}
M_2 &= U\Lambda U^{T}
\label{eq-M2-thin-eigendecomp}
\end{align}
where $U=(u_1,\ldots,u_k)\in R^{d\times k}$, and $\Lambda=diag(\lambda_1,\ldots,\lambda_k)$ is a diagonal matrix with $\lambda_i>0$ for $i=1,\ldots, k$.
Now let's define whitening matrix
\begin{align}
B&=U\Lambda^{-1/2}
\label{eq-whiten-matrix}
\end{align}
and the following whitened vectors
\begin{align}
\widehat{\mu_i}&=\sqrt{w_i}B^{T}\mu_i
\label{eq-whiten-vector}
\end{align}
Also note that because $\mu_i\in span(U)$, and $w_i>0$, we recover $\mu_i$ by
\begin{align}
\mu_i&=\frac{1}{\sqrt{w_i}}(B^{T})^{\dagger}\widehat{\mu_i}
\label{eq-whiten-vector-recover}
\end{align}
where $B^{T})^{\dagger}$ is the Moore-Penrose pseudoinverse on the right of $B^{T}$ such that $B^{T}(B^{T})^{\dagger}=I_k$. From the definition of \eqref{eq-whiten-matrix}, we have
\begin{align}
I_k&=B^{T}M_2 B=M_2(B,B)\\
&=\sum_{i=1}^k w_i (B^{T}\mu_i)\otimes (B^{T}\mu_i)\\
&=\sum_{i=1}^k (\sqrt{w_i}B^{T}\mu_i)\otimes (\sqrt{w_i}B^{T}\mu_i)\\
&=\sum_{i=1}^k \widehat{\mu_i}\otimes \widehat{\mu_i}
\end{align}
Thus, the vectors $\widehat{\mu_i},i=1,\ldots,k$ are orthogonal. Now we apply the whitening to $M_3$ as follows:
\begin{align}
M_3(B,B,B)&=\sum_{i=1}^k w_i (B^{T}\mu_i)\otimes (B^{T}\mu_i)\\
&=\sum_{i=1}^k \frac{1}{\sqrt{w_i}} \widehat{\mu_i}\otimes \widehat{\mu_i}\otimes \widehat{\mu_i}
\label{eq-M3-whiten-orth-tensor-decomp}
\end{align}
Thus, the whitened tensor $M_3(B,B,B)$ admits an orthogonal decomposition \eqref{eq-M3-whiten-orth-tensor-decomp}, which is the merit we need in order to apply the tensor power method for identifying $\widehat{\mu_i}$, for $i=1,\ldots,k$. We denote $M_3(B,B,B)=\widehat{M_3}$. The tensor power method is given by the following iteration
\begin{align}
\theta_{t+1} & \leftarrow \frac{\widehat{M_3}(:,\theta_{t}, \theta_{t})}{\|\widehat{M_3}(:,\theta_{t}, \theta_{t})\|}
\label{eq-tensor-power-iter}
\end{align}
starting from an initial random vector $\theta_0$ on the unit sphere $\mathcal{S}^{k}$. It can be proved that $\theta_t$ will converge to a certain eigenvector $\widehat{\mu_i}$ of $\widehat{M_3}$. Once we have an estimate of $\widehat{\mu_i}$, we can identify $w_i$ by
\begin{align}
\frac{1}{\sqrt{w_i}}&=\widehat{M_3}(\widehat{\mu_i},\widehat{\mu_i},\widehat{\mu_i})
\label{eq-wi-estimate}
\end{align}
Now we want to find another $\widehat{\mu_j}$ different from $\widehat{\mu_i}$. For this purpose, we need to \textit{deflate} $\widehat{\mu_i}$ from $\widehat{M_3}$. Let $\mathcal{I}$ be the index set such that $i\in \mathcal{I}$ if and only if $(\widehat{\mu_i}, w_i)$ have been identified. The deflation is defined by
\begin{align}
\widehat{M_3} &\leftarrow \widehat{M_3} - \sum_{i\in \mathcal{I}}\frac{1}{\sqrt{w_i}} \widehat{\mu_i}\otimes \widehat{\mu_i} \otimes \widehat{\mu_i}
\label{eq-deflation}
\end{align}
Please see~\cite{AnandkumarG2014} for the details of orthogonal tensor decomposition and the tensor power method.
\subsection{Recipe}
In summary, our recipe using the method of moments are as follows:
\begin{enumerate}
\item Compute the empirical moments explicitly $\widetilde{M_1}=\widetilde{\mathbb{E}}[X]$ and $\widetilde{\mathbb{E}}[X\otimes X]$.
\item Identify $\sigma^2$ by extracting the smallest eigenvalue of $\widetilde{\mathbb{E}}[X\otimes X]-\widetilde{M_1}\otimes \widetilde{M_1}$.
\item Form $M_2$ explicitly by \eqref{eq-M2-obs}, and do the thin eigendecomposition \eqref{eq-M2-thin-eigendecomp} to extract the whitening matrix $B$ in \eqref{eq-whiten-matrix}.
\item Start with $\mathcal{I}=\emptyset$. For $i=1,\ldots,k$, do the tensor power iteration \eqref{eq-tensor-power-iter} using the deflated version \eqref{eq-deflation} until converge (or maximum number of iterations met). We can estimate $w_i$ by \eqref{eq-wi-estimate}. Let $\mathcal{I}=\mathcal{I}\cup {i}$.
\par
Note because in the tensor power iteration, only the action $\widehat{M_3}(:,\theta_{t}, \theta_{t})$ is needed, we don't need to explicitly form $\widehat{M_3}$. Instead, from \eqref{eq-M3-obs}, we have
\begin{align}
\widehat{M_3}(:,\theta_{t}, \theta_{t}) &= \mathbb{E}[B^{T}X (\theta_t^{T}B^{T}X)^2] -\\
\nonumber
& \sigma^2\sum_{j=1}^d  \left(B^{T}M_1 (\theta_t^{T}B^{T}e_j)^2  +  2 B^{T}e_j (\theta_t^{T}B^{T}e_j)(\theta_t^{T}B^{T}M_1) \right)
\end{align}
\item Recover $\mu_i$ from $\widehat{\mu_i}$ by \eqref{eq-whiten-vector-recover}.
\end{enumerate}
\section{More general Gaussians}
\subsection{Differing $\sigma_i^2$ for $i=1,\ldots,k$}
The method presented above can be straightforwardly extended to the case where each mixture component has as different variance parameter $\sigma_i^2$, with some tweaks to the form of the observed moments $M_2$ and $M_3$. Please see~\cite{HsuK2013} for the details.
\subsection{General covariance matrices $\Sigma_i$}
Intuitively, when we have general covariance matrices $\Sigma_i$ for $i=1,\ldots,k$, we have many more parameters to estimate (each $\Sigma_i$ have $\frac{d(d+1)}{2}$ entries) than the case of spherical Gaussians. It turns out we need the $4$th and $6$th order moments in order to approximately recover $\Sigma_i$ (with the assumption that $d=O(k^2)$). The algorithm is much more complicated and non-trivial to implement, please see~\cite{GeHK2015} for the details.

\begin{thebibliography}{6}

\bibitem{AnandkumarG2014}A. Anandkumar and R. Ge and D. Hsu and S. M. Kakade and M. Telgarsky, Tensor decompositions for learning latent variable models. \textit{Journal of Machine Learning Research}, Vol. 15, Issue 1, pp. 2773-2832, January 2014.

\bibitem{HsuK2013} D. Hsu and S. M. Kakade, Learning mixtures of spherical Gaussians: moment methods and spectral decompositions, \textit{Proceedings of the fourth Innovations in Theoretical Computer Science}, pp. 11-20, January, 2013.

\bibitem{GeHK2015} R. Ge and Q. Huang and S. M. Kakade, Learning mixtures of Gaussians in high dimensions, \textit{Proceedings of the forty-seventh annual ACM symposium on Theory of computing}, pp. 761-770, June, 2015.

\end{thebibliography}
\end{document}

\end{document}


%!TEX root = dsp_2nd_program_hw.tex

\subsection{Realization of DTMF using FFT --- dif\_fft.h}
This part is explained in the previous report.
The code is shown in section~\ref{code:fft}

See https://github.com/lzhbrian/Fast-Fourier-Transform for more information.

\subsection{Realization of DTMF using Goertzel --- goertzel.h}
Goertzel algorithm is a method by which we can only calculate the amplitude of certain frequency.
By the following functions, We can obtain the $X[k]$ we want:
\begin{equation}\label{equation:iter}
v_{k}[n] = x[n] + 2cos(\omega_{k})v_{k}[n-1]-v_{k}[n-2]
\end{equation}
\begin{equation}\label{equation:xk_calc}
X[k] = v_{k}[N-1] - W^{k}_{N}v_{k}[N-2]
\end{equation}
where
$$\omega_{k} = 2\pi k/N, W_{N}=e^{2\pi /N}, v_{k}[-2]=v_{k}[-1]=0, v_{k}[0]=x[0]$$

In this DTMF detection, we want to acquire the amplitude of
$$697Hz, 770Hz, 852Hz, 941Hz, 1209Hz, 1336Hz, 1477Hz, 1633Hz$$
Note that we get the $k$ for each frequency by the following equation:
\begin{equation}
k = ( N * f ) / SamplingRate;
\end{equation}
where $N$ is the length of the sequence, and f is the targeted frequency.

So we first iteratively calculate the value of $v_{k}[N-1]$ and $v_{k}[N-2]$ using equation~(\ref{equation:iter}), then we use them to get the value of X[k] by equation~(\ref{equation:xk_calc}).
In the real practice, we further return the amplitude of X[k] by calculating their sum of squares.

The code is shown in section~\ref{code:goertzel}


%SourceDoc ../YourName-Dissertation.tex
% \vspace*{-80mm}
\chapter{Background} \label{chapter1:Background}

\section{\sloppy Gaussian Processes}

Consider a \emph{spatial stochastic process}~\cite{gelfand2010handbook} $\{Y(\bm{s}): \bm{s} \in \mathcal{D} \subseteq \mathbb{R}^d\}$ that varies over some continuous spatial domain $\mathcal{D}$. We will focus on the most common case for spatial statistics, where the dimension is $d = 2$, although the methods presented here are valid for any $d$.  For a finite collection of spatial locations $\{\bm{s}_1, \dots, \bm{s}_n\} \subset \mathcal{D}$, $\bm{Y} = (Y(\bm{s}_1), \dots, Y(\bm{s}_n))^T$ is a random vector where each element is associated with one location. The multivariate distribution of $\bm{Y}$ contains information about the spatial dependencies between all $n$ locations.

The distribution of the process $\{Y(\bm{s})\}$ may be described through its finite-dimensional joint distributions
\begin{equation} \label{eq:joint}
F(y_1, \dots, y_n; \bm{s}_1, \dots, \bm{s}_n) = P(Y(\bm{s}_1) \leq y_1, \dots, Y(\bm{s}_n) \leq y_n)
\end{equation}
for every value of $n$ and every set of $n$ spatial locations in $\mathcal{D}$. A \emph{Gaussian process} is a special case in which every distribution in \eqref{eq:joint} is multivariate normal~\cite{gelfand2010handbook}. As a result, the distributions are all completely characterized by their means and covariance matrices, which makes Gaussian processes much easier to work with than non-Gaussian spatial stochastic processes.

% section gaussian_processes (end)

\section{Stationarity and Isotropy} % (fold)
\label{sec:stationarity_and_isotropy}

Throughout this work we will be making two key assumptions:  \emph{stationarity} and \emph{isotropy}. A stationary Gaussian process is one that does not vary with spatial shifts. That is, for any lag vector $\bm{h} \in \mathbb{R}^d$,
\[
E[Y(\bm{s})] = E[Y(\bm{s} + \bm{h})] = \bm{\mu}
\]
and
\[
\textrm{Cov}(Y(\bm{s}), Y(\bm{s} + \bm{h})) = \textrm{Cov}(Y(\bm{0}), Y(\bm{h})) = C(\bm{h}).
\]
Here $C(\bm{h})$ is called the \emph{covariance function}.

For isotropic Gaussian processes, the covariance function depends on $\bm{h}$ only through its magnitude $||\bm{h}||$. In this setting, we can express the covariance function simply as $C: [0, \infty) \to \mathbb{R}$. If we assume without loss of generality that the process is standardized, we can further stipulate that $C(0) = 1$ and $E[Y(\bm{s})] = 0$ for all $\bm{s} \in \mathcal{D}$.

% Define $\mathcal{C}_d$ as the set of all continuous isotropic covariance functions in $d$ dimensions. Further define $\mathcal{C}_\infty = \bigcap_{d=1}^\infty \mathcal{C}_d$ as the set of all functions that are valid isotropic covariance functions in \emph{all} dimensions. It can be shown~\cite{Stein1999}~\cite{schoenberg1938metric} that
% \[
% 	\mathcal{C}_1 \supseteq \mathcal{C}_2 \supseteq \cdots \supseteq \mathcal{C}_\infty
% \]
% and that a function $C(h)$ is in $\mathcal{C}_\infty$ if and only if it can be written in the form
% \[
% 	C(h) = \int_0^\infty \exp(-h^2u^2) \; dG(u)
% \]
% for some $G$ bounded and non-decreasing on $[0, \infty)$~\cite{Stein1999}.

% section stationarity_and_isotropy (end)

\section{Covariance Function Estimation} % (fold)
\label{sec:covariance_function_estimation}

Because Gaussian processes are completely characterized by their mean and covariance functions, estimating $C$ given a collection of observations $\{y_1, \dots, y_n\}$ is a topic of great interest~\cite{ver1993multivariable}~\cite{banerjee2014hierarchical}. The problem of estimating $C$ differs from the general problem function estimation because only a restricted class of functions will produce a valid Gaussian process. In particular, $C$ must belong to the class of \emph{positive definite functions}, which is defined as the set of all functions $C$ such that
\[
  \sum_{i=1}^n \sum_{j=1}^n a_i a_j C(\bm{s_i}\bm{s}_j) > 0
\]
for any $n$, any $\{\bm{s}_1, \dots, \bm{s}_n\} \in \mathcal{D}$, and any $a_1, \ldots, a_n \in \mathbb{R}$. This condition guarantees that the joint distribution of any finite collection of observations $\{Y(\bm{s}_1), \ldots, Y(\bm{s}_n)\}$ has a positive definite covariance matrix. This turns out to be a strong restriction, and it is very difficult in general to directly estimate $C$ in such a way that forces positive definiteness.

The classical approach to overcoming this difficulty is to select a parametric family of covariance functions that is known to be positive definite for a given range of the parameters, and to estimate the parameters using moment- or likelihood-based methods\cite{gelfand2010handbook}. A popular choice of parametric families is the \emph{Mat\'{e}rn} class of functions~\cite{handcock1994approach}, which take the form
\begin{equation} \label{eq:matern}
C(h) = \frac{\sigma^2}{2^{\nu - 1}\Gamma(\nu)} \left( \frac{2\nu^{1/2}h}{\rho} \right)^{\nu} \mathcal{K}_{\nu} \left( \frac{2\nu^{1/2}h}{\rho} \right), \qquad \sigma^2, \nu, \rho > 0,
\end{equation}
where $\mathcal{K}_\nu$ is a modified Bessel function of the third kind. This is a three-parameter family, with $\sigma$ controlling the marginal variance, $\rho$ controlling the range of correlation, and $\nu$ controlling the smoothness of the sample paths. All functions in this class are positive definite for observations in any dimension~\cite{Stein1999}. Some examples of Mat\'ern covariance functions are shown in Figure~\ref{fig:matern_examples}.

\begin{figure}[!htb]
	\centering
	\includegraphics[width=0.95\textwidth]{matern_examples.pdf}
	\caption{\small The Mat\'ern isotropic covariance function \eqref{eq:matern} for various choices of $\nu$ and $\rho$, each with $\sigma^2 = 1$.}
	\label{fig:matern_examples}
\end{figure}

Another positive definite family, which we will use in examples below, is the \emph{exponentially damped cosine} class,
\begin{equation} \label{eq:dampedcos}
	C(h) = \sigma^2 \exp \left( -\tau \frac{h}{\lambda} \right) \cos \left( \frac{h}{\lambda} \right), \qquad \tau \geq \frac{1}{\tan \frac{\pi}{2d}}, \quad \sigma^2,\lambda > 0.
\end{equation}
Unlike the Mat\'ern class, the damped cosine class allows for negative correlations at certain distances. This non-monotonic behavior is known in geostatistical applications as a \emph{hole effect}~\cite{Ye2015}, and is shown in Figure~\ref{fig:dampedcos_examples}. The restriction on $\tau$ that depends on $d$ is necessary for functions of this form to be positive definite. %Note, however, that $\tan(\pi/2d) \to 0$ as $d \to \infty$, so this set of functions is not a member of $\mathcal{C}_\infty$.
The damped cosine family is not nearly as commonly used as the Mat\'ern family, but it is useful in some applications because allows for stochastically periodic behavior as well as negative correlations, which a Mat\'ern family cannot capture. However, it does not share the attractive feature of the Mat\'ern family that the smoothness of the process (i.e. the degree of differentiability) is controlled by a parameter that can be estimated from the data. The smoothness is closely related to the local behavior of the process, which crucial to obtaining good performance when interpolating the process at unobserved locations~\cite{Stein1999}.

\begin{figure}[!htb]
	\centering
	\includegraphics[width=0.95\textwidth]{dampedcos_examples.eps}
	\caption{\small The damped cosine isotropic covariance function \eqref{eq:dampedcos} for $\tau = 1$, $\lambda = 1$, and $\sigma^2=1$. Notice that $C(h) < 0$ for certain values of $h$.}
	\label{fig:dampedcos_examples}
\end{figure}

The strategy of fitting the data to a parametric family of covariance functions can produce a good estimate if the true covariance structure is close to the structure assumed by the choice of parametric family. However, there is no guarantee that this will be the case. For instance, if the true covariance contains a hole effect, there is no way for a Mat\'ern model to capture it since $C(h) > 0$ for all $h$. More preferable would be a flexible method that allows the spatial dependence in the data itself, rather than the choice of parametric family, to determine the shape of $C$.

% subsection estimating_the_covariance_function (end)

\section{The Spectral Domain and Bochner's Theorem} % (fold)
\label{sec:bochner_s_theorem}

The method proposed in this work takes advantage of the result from Bochner~\cite{bochner1955harmonic} which says that a real-valued continuous function $C$ is positive definite if and only if it is the Fourier transform of a symmetric, non-negative, finite measure $F$ on $\mathbb{R}^d$. That is, $C$ is positive definite if and only if
\begin{equation} \label{eq:bochner}
C(\bm{h}) = \int_{\mathbb{R}^d} \exp(i \bm{h}^T \bm{\omega}) \; dF(\bm{\omega}),
\end{equation}
In most circumstances, the measure $F$ has a Lebesgue density, $f(\bm{\omega})$, which is referred to as the \emph{spectral density}~\cite{gelfand2010handbook}.  Furthermore, because we are assuming that the Gaussian process described by $C$ is isotropic, $C$ is a function of a scalar $h$, so that
\begin{equation} \label{eq:bochner2}
C(h) = \int_{\mathbb{R}} \cos(h\omega) \; f(\omega) \; d\omega.
\end{equation}
Stein~\cite{Stein1999} argues that for Gaussian process covariance functions to be realistic models for spatial phenomena, spectral densities $f(\omega)$ must be heavy tailed.

The relationship between the covariance function and the spectral density given in \eqref{eq:bochner2} suggests us an alternative way to estimate the covariance function $C(h)$. If we can estimate the the symmetric spectral density $f(\omega)$, then we are assured that the corresponding covariance function $C(h)$ is positive definite.  Flexibly modeling $C(h)$ directly is difficult because of the restriction of positive definiteness, but flexibly modeling $f(\omega) $ is easy because it can be any symmetric density.

There have been previous approaches for non- and semi-parametric methods that estimate $C(h)$ using the spectral domain. Most closely related to our approach is that of Im, Stein, and Zhu~\cite{IM2007}, who estimate the spectral density using a combination of cubic B-splines with an explicitly specified algebraically decaying tail.   Constraints are placed on the spline coefficients to ensure that the spectral density is non-negative, thereby resulting in a covariance function is positive definite.  Im, Stein, and Zhu use the fact that when under isotropy, \eqref{eq:bochner} can be written as an integral over only one dimension,
\[
	C(h) = 2^{(d-2)/2}\Gamma(d/2) \int_0^\infty (hu)^{-(d-2)/2} J_{(d-2)/2}(hu) \; dG(u),
\]
where $J_\nu(\cdot)$ is the Bessel function of the first kind of order $\nu$. The spline-based spectral density is transformed into a covariance function using numerical integration, which is then used to construct the likelihood of the spline coefficients given the data.  This likelihood is be maximized over the constrained set of spline coefficients to produce $d\hat{G}(u)$, and hence $\hat{C}(h)$.  Our method follows this same concept, with some notable differences outlined in Chapter~\ref{chapter2:Procedure}.

Another, earlier method was put forth by Hall, Fisher and Hoffmann~\cite{Hall1994}. They proposed a multistep process which begins with a kernel estimate of the covariogram. Because the kernel estimate is not necessarily positive definite, they numerically compute its Fourier transform, set all frequencies beyond the first negative value to zero, and then numerically Fourier transform it back to the spatial domain. In the simulation study in Chapter~\ref{chapter3:Simulation-Study}, we compare our method to the one from Hall et al., as well as to the Mat\'ern model fit by maximum likelihood.

In Chapter~\ref{chapter4:Data-Application}, we apply our method to data from a paper by Singh et al. \cite{Singh2014}, where we model the thickness of a thin film semiconductor in a photovoltaic cell as a Gaussian process. The results are discussed in Chapter~\ref{chapter5:Conclusions}, as well as possibilities for improvements and future directions for this work.

% section bochner_s_theorem (end)


\section{Language models}

\paragraph{Language model}
A language model is the probability distribution
of a sequence of words, it can be used for all sort
of applications.
$$p(w_1, w_2, \dots, w_n)$$

\paragraph{Examples}
This probability could be used to predict the next word
given a sequence
$$p(w_n | w_1, w_2, \dots, w_{n-1})$$
Another usage is to predict a feature given the sequence,
for instance find the author from the text
$$p(\mathit{author} | w_1, w_2, \dots, w_n)$$

\paragraph{Bases and evaluation}
The goal of language modeling is to build a computational model of the
language of an entity $X$, that can be any entity in a domain.

Language models are entirely \emph{data-driven}. In other terms,
we need to train models on the right data. One of the disadvantages of
this fact is that instead of modeling the language of $X$, we are modelling
the language of the \emph{documents about X}.

\subsection{N-grams models}
The easier way to estimate a sequence probability is to count
frequencies, but that could be unfeasible. We discussed a similar approach
in Section \vref{problangmodel} in \emph{Computing a sentence probability}.

\paragraph{Unigram models}
A first approach is to use word frequencies,
the idea is to find the relative frequency of a word, by simply counting
it in the corpus and use it to estimate
the probability of a sequence.
$$p(w_n, w_{n-1}\dots, w_1) = p(w_n)p(w_{n-1})\dots p(w_1)$$
Note that this approach does not take into consideration the sequence in
any way, this is a \emph{Unigram model} .

These models might be useful to compare two documents, where we
need to compare the probability of seeing a given word,
or to find how much relevant is a given word for a document.
We can do that by comparing the word probability in all the dataset and in the
single document.

\paragraph{Pointwise Mutual Information}
$$\mathit{pmi}(a,b) = \log\frac{p(a,b)}{p(a)p(b)} = \log \frac{p(a | b)}{p(a)} = \log\frac{p(b|a)}{p(b)}$$
We can set the two variables to the event of extracting the document $C$ from
the corpus and the probability $w$ of extracting a given word.
$$\mathit{pmi}(w, C) = \log\frac{p(w, C)}{p(w)p(C)}$$
By computing the \emph{pmi} for the same word and two different documents,
we can compare the difference of relevance of a word for the two
documents.

\paragraph{Chain rule}
We now want to consider the sequence instead of taking words independently
as in the unigram model.
$$p(w_k | w_1, \dots, w_{k-1}) =
\frac{\mathit{count}(w_1, \dots, w_{k-1}, w_k)}{\sum_w \mathit{count}(w_1, \dots, w_{k-1}, w)}$$
Basically to compute the probability of finding a given word
after a sequence, we count how many times we observe the sequence with
that word, over the count of all other words after the sequence.

In practice, we can reduce a long sequence to a shorter one, for instance
of two or three words, to potentially have enough data to estimate the probability.

\subsection{Evaluation}

There are basically two ways of evaluating a language model:
\begin{itemize}
    \item \emph{Extrinsic evaluation} : embed the system in a real applications
    and measure improvement
    \item \emph{intrinsic evaluation} : define a metric and evaluate the
    model independent from any application, this requires a training set and
    a testing set, where the second must be different but consistent
    with the language learned by the model
\end{itemize}

\paragraph{Perplexity}
In the case of intrinsic evaluation, the main idea
 is to check if the model fits the test data.
This measure is called perplexity.
$$\mathit{pp}(T) = p(w_1, \dots, w_n)^{-\frac{1}{n}}$$
For a two-gram model the measure becomes:
$$\mathit{pp}(T)= \sqrt[n]{\prod_{i=1}^n \frac{1}{p(w_i\;|\;w_{i-1})}}$$

\paragraph{Generalization and zero probabilities}
Language models have an issue when a word is present in the test set
but not in the training set, this becomes unknown with no null probability.

To fix this we apply smoothing, for instance \emph{Laplace smoothing}, i.e.
adding one to each word count, or a parameter k.

\paragraph{Backoff and interpolation}
Another way to deal with null probabilities
is to apply \emph{backoff and interpolation}. Basically to estimate
a $n$-gram frequency, if we do not have enough evidence, we use the $(n-1)$-gram
frequency, until we have just a unigram.
An example can be:
$$p(\mathit{New\;York\;City}) = p(\mathit{City}\;|\;\mathit{York})\;p(\mathit{York}\;|\;\mathit{New}) $$

\emph{Interpolation} means computing a weighted sum over an $n$-gram levels,
this way, even if we only observe the last word of a $n$-gram, we do not
have zero probability.

\subsection{Word-Context models}
The problem of data sparsity is mitigated by the n-gram models, but not
solved completely.

\paragraph{Skip-gram model}
The main of the model is to fix the cases where two sentences
are really similar, i.e. they give the same information, but they do
not have any common n-gram.

We introduce the concept of \emph{word context},
basically we take a n-gram by skipping at most $k$ words,
this leads to skip noisy terms.
By doing so, we could potentially match two sentences
that differs for a few words but that do not share commons
grams.

\paragraph{Continuous Bag of Words}
The idea of this model is to take the context of a word,
for instance a collection of 2-grams, and predict a word.

\paragraph{Word context matrix}
We can think of a word context matrix, where we
store information about appearance of a word $w$
within $t$ other words.

Taking into account the word order depends on the focus of
the model, in language the order is important, in semantics
it is less relevant.

The context matrix stores in $c_{i, j}$ for each word $w_i$
how many times the word $w_j$ occurs in its context.
If we consider the row of that matrix as a vector, two words
are close in that space if they have a similar context.

After computing the matrix, we can factorize it with singular value
decomposition to obtain a more compact representation.
A problem is that such a factorization has many zero terms, to
fix it we can \emph{weighted matrix factorization}.

\subsection{Word embeddings}

\paragraph{Words as vectors}
Representing words as vectors, as we did with models previously,
is a very efficient way to compute words similarity and
represent documents as regions in the vector space.
An example of word as vectors can be found in the context matrix.

The goal is to obtain \emph{dense word vectors}, so with small dimensionality
and with the absence of sparsity.

\paragraph{Distributional hypothesis}
In a real world scenario a word has a meaning, which is related
to the object, or the concept it represent, in our case there is
no such thing, so we must find a way to deal with this.

We can assume that words that appear in the same
context have the same meaning.

\subsubsection{Neural network models}

A first example of using a neural network is
to take as input an $n$-gram of a word $w$ and
as output the probability distribution over the next word.

We saw this concept in the previous section when talking about
\emph{Continuous Bag of Words}.

Basically what the network is doing is to compute
this function
$$f(w_t, \dots, w_{t-n+1}) = P(w_t \; | \; w_1, \dots, w_{t-1})$$

\paragraph{Decomposition}
The idea used by the model is to decompose the function in
two parts:
\begin{itemize}
    \item A mapping from any word $w_i \in V$ to a vector $w_i \in \mathbb{R}^m$ dimensions,
    this creates a matrix $W \in \mathbb{R}^{V \times m}$, where $V$ is the number of words
    \item A probability function over words, that takes in input
    a sequence of vectors of $d$ dimensions, obtained via the previous
    mapping, and produces a vector $g$ in the original $V$ space.
\end{itemize}

\paragraph{Why is this relevant for word embeddings?}
The mapping to a $d$ dimensional space could be the word
embedding we want to obtain, we are not interested
in the network output.

We are assuming that the embedding made by the network
correspond to the real meaning of the word, as such a
mapping is obtained minimizing the objective function.

In other words, if the network is able to perform well, that should
be a good embedding.

\paragraph{Word2vec}
The idea is to train the neural network with positive couples of
words and negative ones, with the hope the network will learn
to output good words sequences.

The structure of the network in this implementation
has an input and output layer of dimensions $V \times V$,
and a middle layer of $V \times m$, initialized with random values.

After training we discard the output layer and keep the
middle one, as discussed in the previous section.
The main principle is that similar vectors in the hidden
layer corresponds to words with similar context.

This is due to the fact that two words with the same context
should have similar consequent predictions, thus, the weights in
the middle layer for this two words must be close, as the
prediction made by the network must be more o less the same for
the twos.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\textwidth]{images/nn-words.png}
    \caption{Word2vec implementation of Neural Network embedding}
\end{figure}

\subsubsection{Using embeddings}

\paragraph{Adding information}
We can use the word embeddings obtained in some way to improve
retrieval, for instance by applying query expansion.

\paragraph{Similarity}
We can also compute similarity among group of words,
as we have a vector space where for instance cosine similarity is
applicable.


\section{Conclusion}

This article illustrated the verification method equational reasoning by example. We proved that the monoid law known as left identity holds for a given function definition.

The type class laws provide a specification for the verification process. In addition, we can rely on properties of existing type class instances to prove further properties.

Type classes allow us to generalize definitions. A proof for the generalization is valid for all specializations. Hence, the proof is reusable.

The examination of the topic improved my comprehension for the advantages of a purely functional language. The reason why verification in a purely functional language like Haskell is easier then in imperative language is because functions are just equalities. We can reason about Haskell code in the same way we reason about mathematical equations. The definitions are stateless. This fact does not apply to mainstream languages. A function definition of an imperative language is allowed to change the context. These definitions are stateful.

Personally, I found the process of proving the left identity law for the given definition tedious and difficult. The proof requires creativity and a strong mathematical background. The verification process would be too cumbersome and expensive to apply it to every piece of software. Although equational reasoning  is not suitable for software with a short life cycle, I think it is important to know the difference between testing and verification by proof.


%%%%%%%%%%%%%%%%%%%%%%%%% Shortest Paths %%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Shortest Paths}

\begin{frame}
  \frametitle{Shortest paths}

  \textcolor{purple}{Different editions of shortest paths problems:}
  \begin{enumerate}
    \item shortest(longest) path in DAG  \\
      \emph{\textcolor{blue}{Dynamic Programming}}
    \item single-source shortest paths
      \begin{itemize}
        \item No negative edges. \\
          \emph{\textcolor{blue}{Dijkstra algorithm.}}
        \item With negative edges (No negative cycle). \\
          \emph{\textcolor{gray}{Bellman-Ford algorithm.}}
      \end{itemize}
    \item all pairs shortest paths \\
      \emph{\textcolor{blue}{Floyd-Warshall algorithm.}}
  \end{enumerate}

\end{frame}

%\begin{frame}
%  \frametitle{Shortest paths}
%
%  \textcolor{purple}{To better understand these problems and algorithms:}
%  \vspace{0.40cm}
%
%  \begin{enumerate}
%    \setlength{\itemsep}{0.30cm}
%    \item Why the algorithm works for this problem ?
%    \item Does the algorithm also work for another problem ?
%  \end{enumerate}
%
%\end{frame}

\begin{frame}
  \frametitle{Shortest paths}

  \textcolor{purple}{DAG can be topologically sorted, so \emph{DP} works.}

  \begin{figure}
    \begin{center}
      \includegraphics[scale=0.30]{figure/bfs_dfs/daglinear}
      \caption{{\scriptsize A dag and its topological sorting.}}
      \label{fig:daglinear}
    \end{center}
  \end{figure}

  \[
    dist(D) = \min \lbrace dist(B)+1, dist(C)+3 \rbrace.
  \]

\end{frame}

\begin{frame}
  \frametitle{Shortest paths}

  \textcolor{purple}{Shortest path without negative edges : \emph{Dijkstra algorithm}}

  \begin{figure}
    \begin{center}
      \includegraphics[scale=0.40]{figure/bfs_dfs/dijkstra}
      \caption{{\scriptsize Property of shortest paths.}}
      \label{fig:dijkstra}
    \end{center}
  \end{figure}

\end{frame}

\begin{frame}
  \frametitle{Priority queue implementations}

  \textcolor{purple}{Complexity of Dijkstra algorithm:}
  \begin{enumerate}
    \item makequeue, $\lvert V \rvert \cdot insert$
    \item $\lvert V \rvert \cdot deletemin$
    \item $\lvert E \rvert \cdot descreaseKey$
  \end{enumerate}

  \pause
  \vspace{0.50cm}

  \textcolor{purple}{Different implementations of priority queue:}

  {\scriptsize

    \begin{tabular}{|c||c|c|c|}
      \hline
      Implementation     & deletemin        & insert, decreaseKey       & total                 \\ \hline \hline
      Array              & $O(V)$           & $O(1)$                    & $O(V^2)$              \\ \hline
      Binary heap        & $O(\log{V})$     & $O(\log{V})$              & $O((V+E) \log {V})$   \\ \hline
      Fibonacci heap     & $O(\log{V})$     & $O(1)$                    & O$(V \log {V} + E)$   \\
      \hline
    \end{tabular}
  }

\end{frame}

\begin{frame}
  \frametitle{Shortest path with negative edges: \emph{Bellman-Ford algorithm}}

  \begin{figure}
    \begin{center}
      \includegraphics[scale=0.40]{figure/bfs_dfs/dijkstranegative}
      \caption{{\scriptsize Dijkstra algorithm fails if there are negative edges ([\textcolor{blue}{$P_{418}$ 8.14}]).}}
      \label{fig:dijkstranegative}
    \end{center}
  \end{figure}

\end{frame}

\begin{frame}
  \frametitle{All pairs of shortest paths: \emph{Floyd-Warshall algorithm}}

  \begin{figure}
    \begin{center}
      \includegraphics[scale=0.40]{figure/bfs_dfs/warshall}
      \label{fig:warshall}
    \end{center}
  \end{figure}

  \pause
  \vspace{0.40cm}
  \textcolor{red}{Assumption: No negative cycles.}

\end{frame}

\begin{frame}
  \frametitle{All pairs of shortest paths: \emph{Floyd-Warshall algorithm}}

  \begin{itemize}
    \item Routing table for all-pair shortest path ([\textcolor{blue}{$P_{448}$ 9.10}]).
    \item Length of shortest cycle in digraph ([\textcolor{blue}{$P_{448}$ 9.12}]).
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{All pairs of shortest paths: \emph{Floyd-Warshall algorithm}}

  \begin{itemize}
    \item Routing table for all-pair shortest path ([\textcolor{blue}{$P_{448}$ 9.10}]).
    \vspace{0.50cm}

      \begin{figure}
        \begin{center}
          \includegraphics[scale=0.35]{figure/shortest_paths/route0}
          \caption{{\scriptsize Construction of routing table.}}
          \label{fig:route0}
        \end{center}
      \end{figure}
    \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{All pairs of shortest paths: \emph{Floyd-Warshall algorithm}}

  \begin{itemize}
    \item Length of shortest cycle in digraph ([\textcolor{blue}{$P_{448}$ 9.12}]).
    \[
      path \lbrack i \rbrack \lbrack i \rbrack
    \]
    \[
      path \lbrack i \rbrack \lbrack i \rbrack < 0 ?
    \]
    \textcolor{red}{Fail for undirected graph:}
    \[
      \lbrace v, w \rbrace \to (v,w,v).
    \]
  \end{itemize}

\end{frame}

\begin{frame}
  \begin{figure}
    \begin{center}
      \includegraphics[scale=0.60]{figure/thank}
      \label{fig:thank}
    \end{center}
  \end{figure}
\end{frame}


The \eslmod{stats} module is the foundation of a set of statistics
modules. It contains special functions like $\Gamma(x)$ and $\Psi(x)$,
and elementary statistics routines such as linear regression fitting
and $\chi^2$ testing. Table~\ref{tbl:stats_api} lists the functions in
the \eslmod{stats} API.

\begin{table}[hbp]
\begin{center}
{\small
\begin{tabular}{|ll|}\hline
\hyperlink{func:esl_stats_DMean()}{\ccode{esl\_stats\_\{D,F,I\}Mean()}} & Calculates mean and $\sigma^2$ for samples $x_i$.\\
\hyperlink{func:esl_stats_LogGamma()}{\ccode{esl\_stats\_LogGamma()}} & Calculates $\log \Gamma(x)$.\\
\hyperlink{func:esl_stats_Psi()}{\ccode{esl\_stats\_Psi()}} & Calculates $\Psi(x)$ (the digamma function).\\
\hyperlink{func:esl_stats_IncompleteGamma()}{\ccode{esl\_stats\_IncompleteGamma()}} & Calculates the incomplete Gamma function.
\\
\hyperlink{func:esl_stats_ChiSquaredTest()}{\ccode{esl\_stats\_ChiSquaredTest()}} & Calculates a $\chi^2$ P-value.\\
\hyperlink{func:esl_stats_LinearRegression()}{\ccode{esl\_stats\_LinearRegression()}} & Fit data to a straight line.\\
\hline
\end{tabular}
}
\end{center}
\caption{The \eslmod{stats} API.}
\label{tbl:stats_api}
\end{table}

\subsection{An example of using the stats API}

Figure~\ref{fig:stats_example} shows an example of using one of the
routines in the \eslmod{stats} module, linear regression fitting. It
generates a set of $n$ points dispersed around a line, $y_i = a + bx +
N(\sigma)$ with Gaussian noise $N(\sigma)$, then fits the data to a
line to obtain estimates $\hat{a}$ and $\hat{b}$.

\begin{figure}
\input{cexcerpts/stats_example}
\caption{An example of using the \eslmod{stats} module.}
\label{fig:stats_example}
\end{figure}