\documentclass[notitlepage]{simple}

\title{Algebraic Properties of the Gaussian Integers}
\author{Matt McCarthy}
\date{April 2016}

\begin{document}

\maketitle

\begin{thm*}
	The Gaussian Integers, denoted $\ZZ(i)$, form a Euclidean domain.
\end{thm*}

\section{Background}

Before we can talk about Euclidean domains, we first need to introduce the definition of a ring.

\begin{definition}[Ring]
	Let $R$ be a nonempty set, and let $+:R^2\rightarrow R$ and $\cdot:R^2\rightarrow R$ be binary operations on $R$.
	Then we say $R$ is a \textit{ring} if all of the following hold.
	\begin{enumerate}
		\item The structure $(R,+)$ is an abelian group whose identity we denote as 0.
		\item For any $a,b,c\in R$, $a(bc)=(ab)c$ (Multiplicative Associativity).
		\item For any $a,b,c\in R$, $a(b+c)=ab+ac$ (Left Distributivity).
		\item For any $a,b,c\in R$, $(a+b)c=ac+bc$ (Right Distributivity).
	\end{enumerate}
	If one says $R$ is a ring, we imply that there exists some addition and some multiplication operators which we denote as $a+b$ and $ab$ respectively.
\end{definition}

\begin{definition}[Ring with Unity]
	Let $R$ be a ring.
	Then $R$ is a \textit{ring with unity} if there exists a $1\in R$ such that for any $a\in R$, $a\cdot 1 = 1\cdot a = a$.
	If such a $1$ exists, we call it the \textit{unity}.
\end{definition}

\begin{definition}[Commutative Ring]
	Let $R$ be a ring.
	Then we say $R$ is \textit{commutative} if for any $a,b\in R$, $ab=ba$.
\end{definition}

The integers, denoted $\ZZ$, are a commutative ring with unity because they satisfy all of the above properties under the usual addition and multiplication.
Another helpful definition is that of a subring.

\begin{definition}[Subring]
	Let $R$ be a ring and let $S$ be a nonempty subset of $R$.
	Then $S$ is a \textit{subring} of $R$ if $(S,+,\cdot)$ is also a ring.
\end{definition}

Furthermore, we have a test which makes it easier to show a subset is a subring.

\begin{proposition}[Subring Test]
	Let $R$ be a ring, and let $S\subseteq R$ be nonempty.
	Then $S$ is a subring of $R$ if and only if for any $a,b\in S$, $a-b$ and $ab$ are also in $S$.
\end{proposition}

Now we need a few more definitions and then we can proceed to proving the theorem.
First, we need to define what a zero divisor is.

\begin{definition}[Zero Divisor]
	Let $R$ be a ring and let $a\in R$ be nonzero.
	We say $a$ is a \textit{zero divisor} if there exists a nonzero $b\in R$ such that $ab = 0$.
\end{definition}

An example of a zero divisor is 2 in $\ZZ_6$, since $2\cdot 3\equiv 0\mod{6}$.
An important property of zero divisors is that they cannot be inverted.
Thus, if our ring has no zero divisors it is fairly nice; in fact it is nice enough that we name it.

\begin{definition}[Integral Domain]
	Let $R$ be a commutative ring with unity.
	Then we say $R$ is an \textit{integral domain} if $R$ has no zero-divisors.
\end{definition}

We call these structures integral domains, because they behave like the integers.
That is there is a unity, multiplication commutes, and we can multiply any nonzero elements together to get another nonzero element.

Next we will define one of the strongest structures in algebra, the field.

\begin{definition}[Field]
	Let $\FF$ be a commutative ring with unity.
	Then $\FF$ is a \textit{field}, if for each $a\in\FF\setminus\set{0}$, there exists a $a^{-1}\in\FF$ such that $aa^{-1}=1$.
\end{definition}

One field that we will use in our proof is the complex numbers, denoted $\CC$.
Lastly, we define Euclidean domains.

\begin{definition}[Euclidean Domain]
	Let $R$ be an integral domain.
	Then we say $R$ is a \textit{Euclidean domain} if there exists a function $d:R\rightarrow(\ZZ^+\cup\set{0})$ such that
	\begin{enumerate}
		\item for any $x,y\in R\setminus\set{0}$, $d(xy)\geq d(x)$,
		\item and there exist $q,r\in R$ where $x=yq+r$ with $r=0$ or $d(r) < d(y)$.
	\end{enumerate}
	Any such $d$ is called a \textit{measure}.
\end{definition}
Essentially, Euclidean domains are rings where the division algorithm works.

\section{Solution}

To start, we define $\ZZ(i)$, the Gaussian Integers.

\begin{definition}[Gaussian Integers]
	The \textit{Gaussian Integers} are
	\[
		\ZZ(i)=\set{a+bi|a,b\in\ZZ}.
	\]
\end{definition}

We first need to show that $\ZZ(i)$ is an integral domain.

\begin{lemma}
	$\ZZ(i)$ is an integral domain under standard complex addition and multiplication.
\end{lemma}
\begin{proof}
	We know that $\CC$ is a field, therefore it is a commutative ring with identity and no zero divisors.
	Thus, it suffices to show that $\ZZ(i)$ is a subring of $\CC$ that contains 1.
	Since $1,0\in\ZZ$, $1+0i=1\in\ZZ(i)$.
	Thus $\ZZ(i)$ is nonempty since it contains the unity.
	We now need to show that for any $z=a+bi,w=c+di\in\ZZ(i)$, $z-w,zw\in\ZZ(i)$.
	We know that $z-w=(a-c)+(b-d)i$ and $zw=(ac-bd)+(ad+bc)i$.
	Since $\ZZ$ is a ring, $a-c,b-d,ac-bd$, and $ad+bc$ are in $\ZZ$ by closure.
	Therefore, $z-w,zw\in\ZZ(i)$.
	Thus $\ZZ(i)$ is a commutative ring with unity that has no zero divisors.
	Hence, $\ZZ(i)$ is an integral domain.
\end{proof}

Since $\ZZ(i)$ is an integral domain, we can embed it in what we call the \textit{field of fractions}, otherwise known as $\QQ(i)$.
We will assume that $\QQ(i)=\set{a+bi|a,b\in\QQ}$, which is true but requires a significant amount of background to show.
The proof of the following theorem hinges upon the previous assumption.

\begin{thm}
	$\ZZ(i)$ is a Euclidean domain.
\end{thm}
\begin{proof}
	In order to show that an integral domain is a Euclidean domain, we need to propose a measure.
	We claim that $d:\ZZ(i)\rightarrow\ZZ^{+}\cup\set{0}$ given by $d(z)=|z|^2$ is such a measure.

	To start we need to show that for any $z,w\in\ZZ(i)\setminus\set{0}$, $d(zw)\geq d(z)$.
	We know that $d(zw)=|zw|^2$.
	However, from Euler's formula, we know that $|zw|=|z||w|$.
	Therefore, $d(zw)=|z|^2|w|^2$.
	Furthermore, by Euler's formula, the only element with modulus less than 1 in $\ZZ(i)$ is 0.
	Therefore, $d(zw)\geq |z|^2=d(z)$.

	Next, we need to find $q,r\in\ZZ(i)$ such that $z=wq+r$ where $r=0$ or $d(r) < d(w)$.
	To do so, we embed $\ZZ(i)$ is $\QQ(i)$ and consider $z/w$.
	We know $z/w=\alpha+\beta i$ with $\alpha,\beta\in\QQ$.
	Let $\alpha',\beta'$ be the nearest integers to $\alpha$ and $\beta$ respectively.
	Then $|\alpha-\alpha'|\leq 1/2$ and $|\beta-\beta'|\leq 1/2$.
	Furthermore,
	\[
		\frac{z}{w}=\alpha-\alpha'+\alpha' + (\beta-\beta'+\beta')i=(\alpha'+\beta' i)+((\alpha-\alpha')+(\beta-\beta')i).
	\]
	Solving for $z$ yields,
	\[
		z=(\alpha'+\beta' i)w+((\alpha-\alpha')+(\beta-\beta')i)w.
	\]
	Since $\alpha',\beta'\in\ZZ$, we know that $\alpha'+\beta' i\in\ZZ(i)$.
	Thus,
	\[
		((\alpha-\alpha')+(\beta-\beta')i)w=z-(\alpha'+\beta' i)w\in\ZZ(i)
	\]
	by closure.
	Take $q=\alpha'+\beta' i$ and $r=((\alpha-\alpha')+(\beta-\beta')i)w$.
	If $r=0$, we are done, otherwise consider $d(r)$.
	\[
		d(r)=|(\alpha-\alpha')+(\beta-\beta')i|^2d(w)=(|\alpha-\alpha'|^2+|\beta-\beta'|^2)d(w)
	\]
	However, we know that $|\alpha-\alpha'|\leq 1/2$ and $|\beta-\beta'|\leq 1/2$.
	Therefore,
	\[
		d(r)\leq \paren{\frac{1}{4}+\frac{1}{4}}d(w)=\frac{1}{2}d(w) < d(w)
	\]
	and $d$ is a measure on $\ZZ(i)$.
	Thus $\ZZ(i)$ is Euclidean.
\end{proof}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS630: Database Management Systems
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 5}

Suppose you are given a relation with four attributes ABCD and the following set of FDs: $\{AB\rightarrow C, BC\rightarrow D\}$.

\begin{enumerate}[label=(\alph*)]
\item Identify the candidate key(s) for $R$.
\item Determine if R is in BCNF, 3NF, or neither of them.
If it is not in BCNF, decompose it into a set of BCNF relations.
\end{enumerate}

\textbf{Solution:}

Table \ref{table1} is constructed to find candidate key(s) for $R$.
Based on attribute closures of each attribute combination, combinations whose closure is equivalent to relation $R$ are underlined as minimal candidate keys.

\begin{table}[H]
\centering\caption{Closure of different combinations of relation $R$}\label{table1}
\begin{tabular}{|c|c||c|c|}
\hline
$X$ & $X^+$ & $X$ & $X^+$\\
\hline
A & A & BC & BCD\\
B & B & BD & BD\\
C & C & CD & CD\\
D & D & ABC & ABCD\\
\underline{\bf AB} & ABCD & ABD & ABCD\\
AC & AC & ACD & ACD\\
AD & AD & BCD & BCD\\
\hline
\end{tabular}
\end{table}

As shown in Table \ref{table1}(a), set of minimal candidate keys are $\{AB\}$.
To check if $R$ is in \textit{BCNF} or \textit{3NF}, closure of functional dependencies are constructed in Equation \ref{equation1}.

\begin{equation}\label{equation1}
F^+ = \{AB\rightarrow C, BC\rightarrow D\} \cup \{AB\rightarrow D\}
\end{equation}

Each functional dependency (FD) is now verified to conform to $BCNF$ and $3NF$ form, as given in Table \ref{table2}.

\begin{table}[H]
\centering\caption{Conformance verification of relation $R$}\label{table2}
\begin{tabular}{|r||c|c|c|}
\hline
 & $AB\rightarrow C$ & $BC\rightarrow D$ & $AB \rightarrow D$\\
\hline
$BCNF$ & \checkmark & $\times$ & \checkmark \\
$3NF$ & \checkmark & $\times$ & \checkmark \\
\hline
\end{tabular}
\end{table}

As shown in Table \ref{table2}, relation $R$ is not \textit{BCNF} since functional dependency $BC\rightarrow D$ violates \textit{BCNF} condition.
To resolve the violation, $R$ is decomposed into $ABC$ and $BCD$ whose closures are respectively $\{AB\rightarrow C, AB\rightarrow D\}$ and $\{BC\rightarrow D\}$ both conforming to \textit{BCNF} form.


\subsection{Test 3}

\begin{enumerate}[label=\arabic*.]
	\item
		Write the integral definition of the Laplace transform.
	\item
		Find the Laplace transform of the following functions. Be sure to give the domains. Use the integral definition for (a) and (e). Be sure to state the domain of the transformed function and any rules of Laplace transforms you use.
		\begin{enumerate}[label = (\alph*)]
			\item
				\begin{equation*}
					f(x) = e^x
				\end{equation*}
			\item
				\begin{equation*}
					h(t) = \sin{t} + 2\cos{3t}
				\end{equation*}
			\item
				\begin{equation*}
					j(t) = e^{3t}\left(t^2 + 3t + 2\right)
				\end{equation*}
			\item
				\begin{equation*}
					b(t) = \dd{}{t}{(e^{3t+1} + e^{1-t})}
				\end{equation*}
%			\item
%				\begin{equation*}
%					\delta(x) = \lim\limits_{a \to 0}{\frac{1}{\abs{x}\sqrt{\pi}}e^{(x/a)^2}}
%				\end{equation*}
		\end{enumerate}
	\item
		Find the inverse Laplace transform of the following functions.
		\begin{enumerate}[label=(\alph*)]
			\item
				\begin{equation*}
					F(s) = \frac{1}{1+s}
				\end{equation*}
			\item
				\begin{equation*}
					H(s) = \frac{s^2+2s+1}{s^3 - 4s^2 + 5s - 2}
				\end{equation*}
			\item
				\begin{equation*}
					J(s) = \frac{s-4}{s^2 -8s + 32}
				\end{equation*}
			\item
				\begin{equation*}
					A(s) = \frac{768}{(2s+3)^5}
				\end{equation*}
		\end{enumerate}
	\item
		Find the general solution to the following differential equation using one of the methods you learned previously and by Laplace transform. Show that the two methods give the same answer.
		\begin{equation*}
			2y'' - 3y' + y = 10\sin{x}
		\end{equation*}
	\item
		Solve the following IVP by Laplace transform.
		\begin{equation*}
			\begin{cases}
				2y'' + 4y' - 6y = te^{-3t} \\
				y'(0) = 0 \\
				y(0) = 1
			\end{cases}
		\end{equation*}
\end{enumerate}


% Copyright (C) He Guanyuming 2020
% The file is licensed under the MIT license.

\section{Integers and Rationals}
Now we are going to extend natural numbers to integers and rationals.

\subsection{The Integers}

\declareexercise{4.1.1}
\begin{proof}
It is immediately given by the fact that
\[
a+b = a+b \equiv a -- b = a -- b
\]
\end{proof}

\paragraph{Lemma 4.1.3}
\[
(m--0)+(n--0) = (m+n)--0
\]
\[
(m--0) \times (n--0) = (mn) -- 0
\]
ensures that the definition $m--0:=m$ is consistent with addition and multiplication.

\declareexercise{4.1.2}
\begin{proof}
\[
a--b = a'--b' \equiv a=b \wedge a'=b'
\]
Then,
\[
(b--a) = (b'--a') \equiv -(a--b) = -(a'--b')
\]
\end{proof}

\declareexercise{4.1.3}
\begin{proof}
\begin{align*}
-1 \times a
&= (0 -- 1) \times (a -- 0) \\
&= (0\times a + 1 \times 0) -- (0 \times 0 + 1 \times a) \\
&= 0 -- a \\
&= -a
\end{align*}
\end{proof}

\declareexercise{4.1.4}
\begin{proof}
Let $x=(a--b),y=(c--d),z=(e--f)$.

(1)
\begin{align*}
(a--b) + (c--d)
&= (a+c) -- (b+d) \\
&= (c+a) -- (d+b) \\
&= (c--d) + (a--b)
\end{align*}

(2)
\begin{align*}
((a--b) + (c--d)) + (e--f)
&= ((a+c)+e) -- ((b+d)+f) \\
&= (a+(c+e)) -- (b+(d+f)) \\
&= (a--b) + ((c--d) + (e--f))
\end{align*}

(3)
First ,
\[
(a--b) + (0--0) = (a--b)
\].

Second, by (1) we have $0+x=x+0$.

(4)
First,
\begin{align*}
(a--b) + (b--a)
&= (a+b) -- (a+b) \\
&= 0 -- 0 \tag{$a+b+0=a+b+0$}
\end{align*}

Second, by (1) we have $x+(-x) = (-x) + x$.

(5)
\begin{align*}
(a--b)(c--d)
&= (ac + bd) -- (ad + bc) \\
&= (ca + db) -- (cb + da) \\
&= (c--d)(a--b)
\end{align*}

(6)
The book proved this.

(7)
First,
\[
(1--0)(a--b) = (1a + 0b) -- (1b+0a) = (a--b)
\]

Second, by (5) we have $1x=x1$.

(8)
\begin{align*}
&(a--b)((c--d)+(e--f)) \\
&= (a--b)((c+e)--(d+f)) \\
&= (a(c+e) + b(d+f)) -- (a(d+f) + b(c+e)) \\
&= ((ac + bd)+(ae + bf)) -- ((ad + bc)+(af + be)) \\
&= (ac+bd)--(ad+bc) + (ae+bf)--(af+be) \\
&= (a--b)(c--d) + (a--b)(e--f)
\end{align*}

(9)
This can be easily concluded from (5) and (8).
\end{proof}

\declareexercise{4.1.5}
\begin{proof}
We need to show that
\[
a \neq 0 \wedge b \neq 0 \Longrightarrow ab \neq 0
\]

Since $a,b$ are not 0, they can be either positive or negative. If they are both positive, the case is
already proven.

When at least one of them is negative, we can divide the $-1$ from the negative ones. That is, if $a=-m$,
where $m$ is positive, then we substitute $a$ with $-1 \times m$. Then we may get $ab$ in either the form
$(-1)(-1) mn$ or $(-1) mn$, where the former is a positive number because $(-1)(-1) =1$ and the latter is
negative.
\end{proof}

\declareexercise{4.1.6}
\begin{proof}
We check the value of $ac-bc$. We know that $ac=bc$, so $ac - bc = 0 - 0 = 0$. According to (9) in
Proposition 4.1.6,
\[
ac - bc = ac+(-b)c = (a+(-b))c = 0
\]

As stated by Proposition 4.1.8, since that $c \neq 0$, $a+(-b) = 0$, which means $a-b=0$. Then we have
$a=b$.
\end{proof}

\declareexercise{4.1.7}
In the following contents, $p$ stands for a positive natural number, $n$ stands for a natural number.

\begin{proof}
(a)
\begin{align*}
a>b
&\equiv a = b+p \\
&\equiv a+(-b) = b + (-b) + p  \tag{See the following explanation} \\
&\equiv a-b = p
\end{align*}
We now explain why $a = b+p \equiv a+(-b) = b + (-b) + p$. Using the substitution law and the
commutativity of addition, it is clear to see that $a = b+p \Longrightarrow a+(-b) = b + (-b) + p$. We now
show the cancellation law of addition, that is,
\begin{lem}
\[
a+c = b+c \Longrightarrow a = b
\]
\end{lem}
\begin{proof}
\begin{align*}
a+c=b+c
&\Longrightarrow a+c+(-c) = b+c+(-c) \\
&\Longrightarrow a+(c+(-c)) = b + (c+(-c)) \\
&\Longrightarrow a=b
\end{align*}
\end{proof}

So we get the inverse result: $a = b+p \Longleftarrow a+(-b) = b + (-b) + p$.

Note that by the definition of integer and what we have know now, we can conclude that
\begin{lem}
For every integer
$i = a - b, j = c - d$, there exists exactly one integer $k$ such that $i = j+k$.
\end{lem}

(b)
\begin{align*}
a>b
&\equiv a = b + p \\
&\Longrightarrow a+c = b+c+p \\
&\Longrightarrow a+c>b+c
\end{align*}

(c)
\begin{align*}
a>b
&\equiv a=b+p \\
&\Longrightarrow ac = (b+p)c = bc + pc \\
&\Longrightarrow ac > bc \tag{$pc > 0$ by Lemma 2.3.3}
\end{align*}

(d)
\[
a>b \equiv a = b+p
\]
Then
\[
-a = -(b+p) = (-1)(b+p) = -b - p
\]
So
\[
-a+p=-b-p+p
\]
That is,
\[
-b=-a+p \equiv -b>-a
\]

(e)
Let
\[
a = b+p_1,b=c+p_2
\]
Then $a = c+(p_1+p_2)$. Obviously $p_1+p_2$ is positive, so $a>c$.

Note that $-a,-b$ are also integers, and plus that $-(-a)=a$, so we can give a stronger conclusion:
\[
a>b \equiv -a<-b
\]

(f)
If $a,b$ are all natural numbers, the statement was proven before.

If one of them (say $a$) is negative,
the other ($b$) is a natural number, then $a=-n$, and we know that $b>0$ and
$0 = a+n \Longrightarrow 0 >-a$, so by (e) we have $b>a$.

If they are both negative, then their negations satisfy the statement. Then
\[
-a<-b \equiv a>b, -a=-b \equiv a=b, -a>-b \equiv a<b
\].
\end{proof}

\declareexercise{4.1.8}
An example: $P(i): i>=0$. It is obvious that $P(0)$ and $P(i) \Longrightarrow P(i+1)$ is true. But for any
negative integer $n$, $P(n)$ is not true.

We additionally prove one more property:
\begin{lem}
For integers $a,b$,
\[
a-b=0\Longrightarrow a=b
\]
\end{lem}
\begin{proof}
We can add $b$ to both side to obtain $a=b$.
\end{proof}

\subsection{The Rationals}
\declareexercise{4.2.1}
\begin{proof}
Reflectivity:
\[
a//b = a//b \equiv ab=ab
\]

Being Symmetric:
\begin{align*}
a//b = c//d
&\equiv ad = bc \\
&\equiv cb = da \\
&\equiv c//d = a//d
\end{align*}

Transitivity:
\[
a//b = c//d \equiv ad = bc
\]
\[
c//d = e//f \equiv cf = de
\]
Thus,
\[
(ad)(cf) = (bc)(de)
\]
We then have
\[
afcd = becd
\]
We can cancel $d$ since $d \neq 0$ to obtain $afc=bec$. If $a=0$, we can conclude that $c,e$ also must be
$0$. Under this occasion, $af=be$ is also true because they all equal to $0$.
\end{proof}

\paragraph{Definition 4.2.2}
It is useful to prove that
\begin{lem} \label{lem4.2.3}
\[
(-a)//b = a//(-b)
\]
,
\[
a//b = (-a)//(-b)
\]
\end{lem}
\begin{proof}
The first is immediately given since $(-a)(-b) = ab$. The latter is proven as $a(-b) = b(-a) = -ab$.
\end{proof}

We may notice that subtraction is not mentioned here. This is because that we can get $a-b$ by adding $a$
and $-b$, where addition $+$ and negation $-$ are mentioned.

\declareexercise{4.2.2}
\begin{proof}
(1) is deduced in the book.

(2)
I don't quiet understand why Tao used this $*$ sign instead of $\times$. I know it is a new definition,
but the $\times$ sign is undefined for rationals (except for integers, but for which we can verify that
the two definitions are the same). We will use the $\times $ sign or just leave it off here.

\[
(a'//b')(c//d) \equiv a'd = b'c \equiv ad=bc \equiv (a//b)(c//d)
\]
Similarly we can verify this for $c'//d'$.

(3)
\[
-ab'=-a'b \equiv (-a)//b = (-a')//b'
\]
\end{proof}

For the sake of simplification, we hereby introduce some useful lemmas:
\begin{lem} \label{lem4.2.1}
\[
b=d\neq 0 \Longrightarrow (a//b = c//d \equiv a=c)
\]
\end{lem}
\begin{proof}
Assume that $b=d\neq 0$.

On one hand, if $a//b=c//d$, then $ad=bc$. Since that $b=d \neq 0$, we can cancel them to obtain $a=c$.

On the other hand, if $a=c$, then if we multiply them by the same integer (namely $b=d$), and the
results are still equal ($ad=bc$). So $a//b=c//d$.
\end{proof}
\begin{lem} \label{lem4.2.2}
\[
c \neq 0 \Longrightarrow a//b=ac//bc
\]
\end{lem}
\begin{proof}
Assume that $c\neq 0$.

First we know that $ab=ab$. Then we can further obtain $abc=abc$, which
means $a//b=ac//bc$.
\end{proof}

\declareexercise{4.2.3}
\begin{proof}
(1)
We have
\[
a//b + c//d = (ad+bc)//(bd)
\]
\[
c//d + a//b = (cb+da)//(db)
\]
It is easy to see that they are equal.

(2)
It is proven in the book.

(3)
We just deduce $x+0=x$ here, for we have $0+x=x+0$ according to (1).
\[
a//b + 0//1 = (a1+b0)//(b1) = a//b
\]

(4)
We only prove $x+(-x)=0$ here, for we have $x+(-x)=(-x)+x$ according to (1).
\[
a//b+(-a)//b= (ab-ab)//bb = 0//b^2=0
\]

(5)
\begin{align*}
a//b \times c//d
&= ac//bd \\
&= ca//db \\
&= c//d \times a//b
\end{align*}

(6)
\begin{align*}
&(a//b \times c//d) \times e//f \\
&= ac//bd \times e//f \\
&= ace//bdf \\
&= a//b \times ce//df \\
&= a//b \times (c//d \times e//f)
\end{align*}

(7)
We only prove $x1=x$ here, for we have $x1=1x$ according to (4).
\[
a//b \times 1//1 = a1//b1 = a//b
\]

(8)
\begin{align*}
&a//b (c//d + e//f) \\
&= a//b ((cf+ed) // (df)) \\
&= a(cf+ed)//bdf \\
&= ab(cf+ed) // b^2df \tag{See Lemma \ref{lem4.2.2}} \\
&= ((ac)(bf) + (bd)(ae)) // (bd)(bf) \\
&= ac//bd + ae//bf \\
&= (a//b \times c//d) + (a//b + e//f)
\end{align*}

(9)
This can be deduced from (5) and (8).

(10)
We merely conclude $xx^{-1} = 1$ here, since we have $xx^{-1} = x^{-1}x$ from (5).

\[
a//b \times b//a  = ab//ba = (ab)1//(ab)1 = 1//1
\]
The last step is done by Lemma \ref{lem4.2.2}.
\end{proof}

\declareexercise{4.2.4}
\begin{proof}
For any rational $r = a/b$, $a,b$ are integers.
They are either positive, $0$, or negative (except that $b$ cannot be 0). When $a,b$ are both positive,
then $r$ is also positive. When $a$ is positive but $b$ is negative, then let $b=-p$, where $p$ is
positive, thus $a/b = a/(-p) = (-a)/p$ is negative. When $a=0$, $r=0$. When $a$ is negative, and $b$ is
positive, then by definition $r$ is negative. When $a,b$ are both negative, according to Lemma
\ref{lem4.2.3}, $r$ is positive.

Therefore, we have iterated through all possible situations and verified that there is and only is one
statement for a rational is true.
\end{proof}

\declareexercise{4.2.5}
\begin{proof}
Let $x=a/b,y=c/d,z=e/f$. Before proving the following components, we will introduce some useful
propositions here.
\begin{lem} \label{lem4.2.4}
\begin{enumerate}
\item $x>0$ is logically equivalent to $x$ being positive.
\item $x<0$ is logically equivalent to $x$ being negative.
\end{enumerate}
\end{lem}
\begin{proof}
\[
x-0 =x
\]
is itself, so whether $x$ is positive or negative, the same is $x-0$, then we can deduce $x>0$ or $x<0$,
and vice versa.
\end{proof}

We can now use simplified notation $x>0$ to express the same meaning: $x$ is positive.

(a)
We check the value of
\[
\delta = x-y = a/b +(-c)/d = (ad-bc)/bd
\]
$\delta$ is also a rational number. According to the previous exercise, it is either positive, negative,
or $0$. So $x$ either $>y$, $<y$, or $=y$ (We haven't yet proven $x-y=0 \Longrightarrow x = y$. Let's
prove it now. We can add $y$ to both side of $x-y=0$ to obtain the result).

(b)
According to Lemma \ref{lem4.2.4}, $x<y \Longrightarrow x-y<0$. Then we multiply $-1/1$ with $x-y$ to
obtain (It is easy to see that for rational number $r$, $-1r = -r$ and $-(-r)=r$)
\[
-1/1 \times (x + (-y)) = -x + -(-y) = y - x
\]
Since $x-y$ is negative, and the negation of a positive number is negative, so the negation of $x-y$,
$y-x$, is positive, which means that $y>x$.

(c)
By the hypothesis, $x-y<0 \wedge y-z<0$. We are now proving that $i,j<0 \Longrightarrow i + j <0$. We can
write $i,j$ as $o/p,q/s$ respectively. Let $p,s>0$, then $o,q<0$. Then $o/p+q/s = (os+pq)/ps$. We know
that $os,pq<0$ (Write a negative integer as a negation of a positive integer to see that the product of a
positive and a negative is also negative).

Now we show that for two positive integers, their sum is still
positive. Integers who are positive are also natural number, and their sum remains a natural number. So
the sum itself equals to $0$ plus itself, which means it is positive. The negation of this sum, which is
also $-m+(-n)$, is thus negative. Since that $-m,-n$ can present any negative integer, the fact means that
the sum of two negative integers remains a negative integer.

So $os+pq<0$. But $ps>0$, so $i+j<0$. Thus, $(x-y) + (y-z) = x-z<0$, which means $x<z$.

(d)
\begin{align*}
x+z-(y+z)
&= x+z + (-)(y+z) \\
&= x+z + (-1)(y+z) \\
&= x+z -z - y \\
&= x-y <0
\end{align*}

(e)
It is easy to verify that the product of two positive rationals is still positive (Writing them as
$a/b,c/d$, where $a,b,c,d>0$, then $ac/bd$ also $>0$). Then $xz-yz=z(x-y)$, which is the product of a
positive number and a negative number, and is thus a negative number.
\end{proof}

\declareexercise{4.2.6}
\begin{proof}
According to (e) of Proposition 4.2.9, we need only to show that $x<y \Longrightarrow -x>-y$. Then we can
multiply $xz>yz$ with $-1$ to obtain what we want.

We know that the negation operation will turn a positive into negative and vice versa. Now we have
$x-y<0$, so the negation $-(x-y) = -x+y = -x - (-y)>0$, which means that $-x>-y$.
\end{proof}

There are still many properties about rationals that we use for granted (e.g. $x^-1$ has the same sign as
$x$; the two definitions of order are the same, that is, $x-y>0 \equiv x=y+p \equiv x>y$, where $p>0$).
Although they need to be proven prior to being used, we can not cover all of them here. We will
prove some of them in the future only if they are used. Also, most of them are not hard to prove. We need
not to worry.

We will prove some important ones here:
\begin{prop} \label{prop.different.def.of.order}
For rational numbers $x,y,p>0$
\[
x-y>0 \equiv x=y+p \equiv x>y
\]
\end{prop}
\begin{proof}
$x-y>0 \equiv x>y$ is the definition of order. We merely need to prove $x-y>0 \equiv x=y+p$ here.

On one hand, if $x-y>0$, then $p$ is not others, but the very number $x-y$.

On the other hand, if there exists a $p>0$ such that $x=y+p$. Then add $-y$ to the both side of the
equation, and we can get $x-y = p$, which means $x-y$ is positive. So $x-y>0$.
\end{proof}

\begin{prop} \label{prop.4.2.add.ineq}
We can add two inequalities together. That is,
\[
a<b \wedge c<d \Longrightarrow a+c<b+d
\]
\end{prop}
\begin{proof}
We know that
\[
a<b \Longrightarrow a+c<b+c
\]
, and
\[
c<d \Longrightarrow b+c<d+b
\]
According to the transitivity of order, we can derive that
\[
a+c<b+c<b+d
\]
\end{proof}

\begin{prop} \label{prop.4.2.multiply.ineq}
We can multiply two inequalities of positives or negatives together. That is,
\[
a,b,c,d >0 \wedge a<b \wedge c<d \Longrightarrow ac<bd
\]
, and
\[
a,b,c,d <0 \wedge a<b \wedge c<d \Longrightarrow ac>bd
\]
\end{prop}
\begin{proof}
When they are all positive,
we know that
\[
a<b \Longrightarrow ac<bc
\]
, and
\[
c<d \Longrightarrow bc<bd
\]
According to the transitivity of order, we can derive that
\[
ac<bc<bd
\]

When they are all negative,
we know that
\[
a<b \Longrightarrow ac>bc
\]
, and
\[
c<d \Longrightarrow bc>bd
\]
According to the transitivity of order, we can derive that
\[
ac>bc>bd
\]
\end{proof}

Note that we can already add or multiply equations because of the axiom of substitution, so we can change
the $<$ in the inequalities to $\leq$ in the previous two propositions whenever needed.

\subsection{Absolute Value and Exponentiation}
\declareexercise{4.3.1}
\begin{proof}
(a)
$x>0 \Rightarrow |x| >0$, $x=0\Rightarrow |x|=0$, $x<0 \Rightarrow |x| >0$. So $|x| \geq 0$.

And we can see that only when $x=0$ can $|x| = 0$.

(b)
This one is very tedious to prove. Let's enumerate all conditions:
\begin{enumerate}
\item $x,y>0$. On this occasion,
\[
|x+y| = x+y = |x|+|y|
\]

\item At least one of them is $0$. On this occasion, let's just let $x$ be 0, the other
situations are similar.
\[
|x+y| = |0+y| = |y| = |0| + |y| = |x| + |y|
\]

\item $x=y>0$. On this occasion, $|x+y| = |2x| = 2x = |x|+|x|$.

\item $x=y<0$. On this occasion, $|x+y| = |2x| = -2x = |x|+|x|$.

\item $x,y<0$. On this occasion, $|x+y| = -(x+y) = -x -y = |x| + |y|$.

\item One of them is positive, the other is negative. We specify $x>0,y<0$ here. But the other conditions
are similar. Under this condition, we further divide the situation into three occasions:
\begin{itemize}
\item $x+y>0$ On this occasion, $|x+y| = x+y$, $|x| + |y| = x-y$. Note that $x-y = x+y +2(-y)$, where
$2(-y) >0$, so $|x+y| < |x|+|y|$ (See Proposition \ref{prop.different.def.of.order}).
\item $x+y<0$ On this occasion, $|x+y| = -x-y$, $|x|+|y|=x-y$. Note that $-x-y - (x-y) = 2(-x) < 0$, so
$|x+y| < |x| + |y|$.
\item $x+y=0$ On this occasion, $|x+y| = 0 \leq |x| + |y|$ (Recall that the sum of two positive rationals
remains positive).
\end{itemize}
\end{enumerate}

We have iterated through all conditions.

(c)
We shall prove that
\[
-|x| \leq x \leq |x|
\]
first.

\begin{enumerate}
\item If $x>0$, then $x=|x|>0$. And $0>-|x|$, so by the transitivity of order, $-|x| < x$.
\item If $x=0$, then $|x|=-|x| = x=0$.
\item If $x<0$, then $x=-|x|<0$ And $0<|x|$, so $x<|x|$.
\end{enumerate}
This also means that $x$ either equals to $|x|$ or $-|x|$.

Then we prove that $-y \leq x \leq y \equiv y \geq |x|$.

On one hand, if $-y \leq x \leq y$, then when $x=|x|$, we have $|x| \leq y$; when $x=-|x|$, we have
$-y \leq -|x| \equiv y \geq |x|$. As stated previously, we know that at least one of the two conditions
are satisfied.

On the other hand, if $y \geq |x|$, then $-y \leq -|x|$. But since that $-|x| \leq x \leq |x|$, we can
obtain what we want by the transitivity of order.

(d)
\begin{enumerate}
\item If $x=y=0$, then $|xy| = 0 = |x||y|$.
\item If $x,y>0$, then $|xy| = xy = |x||y|$.
\item If $x,y<0$, then $xy>0$, $|xy| = xy = (-x)(-y) = |x||y|$.
\item If one of them is positive, and the other is negative, (say $x>0,y<0$), then
$|xy|=-xy=x(-y)=|x||y|$. The other conditions are similar.
\end{enumerate}

Thus, $|-x| = |-1||x| = 1|x| = |x|$.

(e)
This can be easily conclude from (a).

(f)
Since that $|-x|=|x|$, we have $|x-y| = |-(x-y)| = |y-x|$.

(g)
Note that $x-z = (x-y) = (y-z)$. Then from (b) we can deduce that $|x-z| \leq |x-y| + |y-z|$, which is
$d(x,z) \leq d(x,y) + d(y,z)$.
\end{proof}

\declareexercise{4.3.2}
\begin{proof}
(a)
If $x=y$, then $|x-y| = 0$. And any positive rational $\varepsilon >0$, so $|x-y|\leq\varepsilon$.

The other statement is much better easier to prove after we have know the denseness of rationals. We
essentially repeat some of the proof work that are done afterwards here.
On the other hand, suppose the negation, that is, $(\forall \varepsilon>0)(|x-y| \leq \varepsilon)$, but
$x\neq y$. Then $x-y \neq 0$. Let $\delta = |x-y| \neq 0$. We know that $2^{-1} = 1/2 > 0$, so
$\delta / 2 > 0$. Also we have $\delta /2 + \delta /2 = \delta \Longrightarrow \delta /2 < \delta$. Then let
$\varepsilon = \delta /2$. So we have both $|x-y| < \delta/2$ and $|x-y| > \delta /2$, which is impossible.

(b)
It is immediately derived from $|x-y| = |y-x|$.

(c)
\[
|x-z| \leq |x-y| + |y-z| \leq \varepsilon + \delta
\]

(d)
\[
|x+z - (y+w)| = |x-y + z-w| \leq |x-y| + |z-w| \leq \varepsilon + \delta
\]
\[
|x-z - (y-w)| = |x-y + w-z)| \leq |x-y| + |w-z| \leq \varepsilon + \delta
\]

(e)
\[
|x-y| \leq \varepsilon < \varepsilon'
\]

(f)
From (c) of Proposition 4.3.3, we can derive that
\[
|x-z| \leq \varepsilon \equiv -\varepsilon \leq x-z \leq \varepsilon
\equiv z-\varepsilon \leq x \leq z+ \varepsilon
\]
, and that
\[
|x-y| \leq \varepsilon \equiv y-\varepsilon \leq x \leq y + \varepsilon
\]
Thus we have
\[
y-\varepsilon \leq x \leq z + \varepsilon
\]

We will only prove the statement when $z\leq w \leq y$, another one is similar. On this occasion,
$-y \leq -w \leq -z$. Add this inequality to $y-\varepsilon \leq x \leq z + \varepsilon$ to obtain that
\[
-\varepsilon \leq x-w \leq \varepsilon
\]

(g)
\[
|xz-yz| = |x-y||z| \leq \varepsilon|z|
\]

(h)
We will explain why $|a| \leq \varepsilon \wedge |b| \leq \delta$ implies
$|a||z| + |b||x| + |a||b| \leq \varepsilon|z| + \delta|x| + \varepsilon\delta$.

First, multiply $|a| \leq \varepsilon$ with $|z|$ to obtain $|a||z| \leq \varepsilon |z|$. Then add both sides
of the inequality with $|b||x| + |a||b|$ to gain
\[
|a||z| + |b||x| + |a||b| \leq \varepsilon |z| + |b||x| + |a||b| \tag{1}
\]
Similarly,
\[
|a||z| + |b||x| + |a||b| \leq |a||z| + \delta |x| + |a||b| \tag{2}
\]
Finally, we can multiply $|a| \leq \varepsilon$ with $|b| \leq \delta$ as stated by Proposition
\ref{prop.4.2.multiply.ineq} to derive $|a||b| \leq \varepsilon\delta$. So after some addition we have
\[
|a||z| + |b||x| + |a||b| \leq |a||z| + |b||x| + \varepsilon\delta \tag{3}
\]

Using Proposition \ref{prop.4.2.add.ineq}, we add (1),(2) and (3) together:
\[
3(|a||z| + |b||x| + |a||b|) \leq (\varepsilon|z| + \delta|x| + \varepsilon\delta) + 2(|a||z| + |b||x| + |a||b|)
\],
which can be simplified to
\[
|a||z| + |b||x| + |a||b| \leq \varepsilon|z| + \delta|x| + \varepsilon\delta
\]

Also note that if we use $x-y$ as $a$, $z-w$ as $b$, and derive $|xz-yw|$ from
\[
xz=(y+a)(w+b),
\]
then what we will get is that $xz,yw$ are ($\delta|y| + \varepsilon|w| + \delta\varepsilon$) close.

This consequence may seem obvious, but in fact it isn't. And should we change some variables of them, the
result may vary. This example tells us that we should be very cautious when dealing with inequalities. What we
should do is to carefully derive conclusions from what we have proven instead of taking intuitive things for
granted.
\end{proof}

\declareexercise{4.3.3}
\begin{proof}
(a)
\begin{enumerate}
\item Use induction. We induct on $m$. First, $x^nx^0 = x^n1 = x^{n+0}$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
x^nx^{m+1}
&= x^n(x^m\times x)\\
&= x^nx^m \times x \\
&= x^{n+m} \times x \tag{The induction hypothesis} \\
&= x^{n+m+1}
\end{align*}

\item Use induction. We induct on $m$. First, $(x^n)^0 = 1 = x^{n\times 0}$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
(x^n)^{m+1}
&= (x^n)^m \times x^n \\
&= x^{mn} \times x^n \tag{The induction hypothesis} \\
&= x^{mn + n} \tag{By the previous statement} \\
&= x^{n(m+1)}
\end{align*}

\item Use induction. We induct on $n$. First, $(xy)^0 = 1 = x^0y^0$.

Suppose that for $m$, the statement is already true. Then
\begin{align*}
(xy)^{m+1}
&= (xy)^m \times xy \\
&= x^my^m \times xy \tag{The induction hypothesis} \\
&= x^m \times x \times y^m \times y \\
&= x^{m+1} y^{m+1}
\end{align*}
\end{enumerate}

(b)
On one hand, if $x=0$, then for $n>0$, $0^n=0$.

On the other hand, if for $n>0$, $x^n=0$, we need to prove $x=0$. We try to show that
$x \neq 0 \Longrightarrow x^n \neq 0$. Use induction. Since that $n\neq 0$, we start from $n=1$.
$x^1 = x^0 \times x = x$.

Suppose that for $n$, the statement is already true. Then
\[
x^{n+1} = x^n \times x,
\]
which is the product of two positive rationals, and which is thus positive.

(c)
(1)
Use induction: $x^0 = y^0 =1>0$.

Suppose that for $n$, the statement is already true. Then we have two inequalities here:
\[
x^n \geq y^n \geq 0
\]
and
\[
x \geq y \geq 0
\]
We can multiply the two because of Proposition \ref{prop.4.2.multiply.ineq}. Then we have
\[
x^{n+1} \geq y^{n+1} \geq 0
\]

If $n>0$, then we induct from $1$. The process resembles to what we have just done, so I don't write it here.

(d)
Use induction: $|x^0| = |1| =1 =|1|^0$.

Suppose that for $n$, the statement is already true. Then
\[
|x^{n+1}| = |x^n \times x| = |x^n| |x| = |x|^n |x| = |x|^{n+1}
\]
\end{proof}

\paragraph{Definition 4.3.11}
We can see that there are now two versions of $x^{-1}$. Now we try to show that they express the same thing.
Write $x$ as $a/b$. The first version is $x^{-1} = b/a$.

The second version is $x^{-1} = 1/x = 1 \times x^{-1}\text{(version 1)} = x^{-1} = b/a$.

Note that only after we have known this can we say that for the second version of $x^{-1}$,
$(x^{-1})^{-1} = x$.

Now we can also say that $x^{-n} = (x^n)^{-1}$

\declareexercise{4.3.4}
\begin{proof}
Except for (3),
we have already proven these properties when $m,n \in \mathbb{N}$. Then we will just write $m,n$ as $-m,-n$.

(a)
(1)
\[
x^{-m}x^{-n} = \frac{1}{x^m}\frac{1}{x^n} = \frac{1}{x^mx^n}=1/x^{m+n} = x^{-m-n}
\]

(2)
Before doing this, we must derive that for integers $a,b$ and natural number $n$, $(a/b)^n = a^n/b^n$. Use
induction: $(a/b)^0 = 1 = 1/1 = a^0/b^0$.

Suppose that for $n$, the statement is already true. Then
\[
(a/b)^{n+1} = (a^n/b^n)(a/b) = (a^{n+1}/b^{n+1})
\]

We can now close the induction.

Thus,
\begin{align*}
&(x^{-n})^{-m} \\
&= ((1/(x^n))^m)^{-1} \tag{$x^{-n} = (x^n)^{-1}$} \\
&= (1/(x^n)^m)^{-1} \\
&= (x^n)^m \\
&= x^{mn}
\end{align*}

(3)
\begin{align*}
(xy)^{-n}
&= 1/(xy)^n \\
&= 1/(x^ny^n) \\
&= (1/x^n) (1/y^n) \\
&= x^{-n}y^{-n}
\end{align*}

(b)
\begin{lem}
For $x = (a/b) >  y = (c/d) > 0$,
\[
0 < x^{-1} < y{-1}
\]
\end{lem}
\begin{proof}
Let $a,b,c,d>0$. We know that
\[
a \times b^{-1} > c \times d^{-1}
\]
Multiply it with $bd>0$, we have
\[
ad>bc
\].
Multiply it with $a^{-1}c^{-1} >0$, we have
\[
d/c>b/a
\]
That is,
\[
y^{-1} > x^{-1}
\]
And they are obviously bigger than $0$.
\end{proof}

According to the lemma,
\[
x^{n} \geq y^{n} \Longrightarrow ((x^{n})^{-1} \leq (y^{n})^{-1} \equiv x^{-n} \leq y^{-n})
\]

(c)
We first show that for positive integer $n$, the statement is true. We assume that $x>y$. Another situation is
similar.
Use induction, we try to prove that $x\neq y \Longrightarrow x^n \neq y^n$. We need to start from $n=1$. First,
$x^1=x > y^1=y$.

Suppose that for $n$, the statement is already true. Then
Multiply $x>y$ with $x^n > y^n$,
we have $x^{n+1} > y^{n+1}$.

We can now close the induction.

Then for negative ones, we know that $1/x = 1/y$ iff $x=y$, so $x^n \neq y^n \equiv x^{-n} \neq y^{-n}$.

(d)
It is immediately derived since $1/x = 1/y \equiv x=y$.
\end{proof}

\declareexercise{4.3.5}
\begin{proof}
Use induction from $1$.

$2^1 = 2 >1$.

Suppose that for $N$, the statement is already true. Then
\[
2^{N+1} = 2\times 2^N > 2N \geq N+1
\]
(Note that $N\geq 1 \Longrightarrow 2N \geq N+1$)
\end{proof}

\subsection{Gaps In The Rational Numbers}
\declareexercise{4.4.1}
\begin{proof}
Existence: We show that when $x \geq 0$, $(\exists n \in \mathbb{Z})(n \leq x < n+1)$. Write $x$ as $a/b$,
where $a \geq 0,b>0$. If $a= 0$, then $x=0$, $n=0$. If $a\neq 0$, then according to Proposition 2.3.9,
\[
\exists m \exists r(a = mb+r),
\]
where $m,r \in \mathbb{N}$ and $r < b$. Because of this, $mb+b >a$, so $(mb+b)/b>a/b=x$, which means $m+1>x$.
On the other hand, $mb \leq a$, so $mb/b \leq a/b =x$, which means $m \leq x$.

Then when $x<0$, then $-x>0$, and $(\exists n \in \mathbb{Z})(n \leq -x < n+1)$, so
\[
-n \geq x > -n -1
\]
$\geq$ means $>$ or $=$ (exclusive). When $-n > x >-n-1$, let $m=-n-1$, then $m \leq x < m+1$ is true. When
$-n =x > -n-1$, let $m=-n$, then $m \leq x < m+1$ is also true. So $m$ is the integer we want if $x<0$.

Uniqueness:
For $n \leq x < n+1$, and $m \neq n$, we try to prove that $m \leq x<m+1$ is not possible. Before doing this,
we need some lemmas:
\begin{lem}
(1) For integers $i,j$, $i<j \equiv i+1\leq j$.
(2) For integer $i$, there is no integer $j$ such that $i<j<i+1$.
\end{lem}
\begin{proof}
(1) It has already been proven for natural numbers. If $i,j<0$, then $-i,-j>0$.
\[
i<j \equiv -i>-j \equiv -i \geq -j+1 \equiv i \leq j-1 \equiv i+1 \leq j
\]

(2)
Suppose the negation, that there exists a integer $j$ such that $i<j<i+1$. We know that
$i<j \equiv i+1 \leq j \equiv j \geq i+1$. But we also have $j<i+1$, which is impossible.
\end{proof}

$m$ either $<$ or $>n$. On the former case, $m+1\leq n\leq x$, so $m+1>x$ is not possible. One the latter case,
$x<n+1\leq m$, so $m \leq x$ is impossible.
\end{proof}

\declareexercise{4.4.2}
\begin{proof}
(a) We will use a different approach from the hint the book provided here. After assuming the negation, we
try to prove that
$a_n \leq a_0 -n$. Note that subtraction may results in a overflow (that is, natural numbers flows to negative
integers). So we will first define $a_n$ as integers. And we try to show that no such infinite descent
sequences can only lie in  $\mathbb{N}$.

Use induction: $a_0 \leq a_0-0$.

Suppose the statement for $n$ is already true, then
$a_{n+1} < a_n \equiv a_{n+1} +1 \leq a_n \equiv a_{n+1} \leq a_n - 1$. Then we have
$a_{n+1} \leq (a_0 -n -1 = a_n -(n+1))$. We can now close the induction.

However, let $n = a_0$, then $a_{n+1} \leq n-n-1 = -1$, which means that $a_{n+1}$ does not lie in
$\mathbb{N}$.

(b)
(1) Yes. For example, $a_n:=-n$ satisfies our restrictions.

(2) Yes. Because it is always possible to find a rational between $0$ and $a_0$.
\end{proof}

\declareexercise{4.4.3}
\begin{proof}
(1) Suppose the negation, that natural number $n=2k=2k'+1$, where $k,k'$ are also natural. Then $2k=2k'+1$
Then we have $2k > 2k' \Longrightarrow k>k'$. But $2k+1 = 2(k'+1)$, so $2(k'+1) > 2k \Longrightarrow k'+1 >k$.
But we know that between $k',k'+1$ there exists no natural numbers. so it is impossible. (Note that we don't
have a proposition saying $ac>bc \Longrightarrow a>b$ for natural numbers, but we can first deal with them with
the range of rationals. Multiply them with $c^{-1}$, and we will see that the result of the two sides are also
natural numbers, so for natural numbers this is true.)

(2)
\[
p^2 = (2k+1)^2 = 4k^2+4k + 1 = 2(2k^2+2k) +1
\]

(3)
Treat $p,q$ as rationals.
$p^2/2=q^2 \Longrightarrow q^2<p^2$. We show that $q \geq p$ can not be true. It is obvious that $q \neq p$.
And when $q>p$, multiply it with itself, $q^2>p^2$, which is impossible.
\end{proof}


\documentclass[11pt]{article}
\title{Lagrange Multipliers}
\author{Suhas Arehalli}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}

\newtheorem{theorem}{Theorem}[section]

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

\begin{document}
\maketitle

\section{Preliminaries}
\begin{definition}[Manifold]
    A smooth $k$-manifold $M$ in $\mathbb{R}^n$ can be defined as a set of points
    \[ M = \{ \mathbf{x} \in \mathbb{R}^n | F(\mathbf{x}) = \mathbf{0}\} \]
    For some function $F: \mathbb{R}^n \rightarrow \mathbb{R}^{n-k}$ such that
    $\left[\mathbf{D}F(\mathbf{x})\right]$ has maximal rank for all $\mathbf{x}$.
    This is equivalent to the conventional definition in terms of local charts and
    such.
\end{definition}
\begin{definition}[Tangent Space]
    The tangent space to a manifold at point $\mathbf{a}$ is defined as the set of vectors
    that locally describe movement on the manifold. Since staying on the manifold requires
    that it's defining constaint function $F$ remains 0, we can formally define the tangent
    space at $\mathbf{a}$ as
    \begin{align*}
        T_{\mathbf{a}}M &= \{ \mathbf{x} \in \mathbb{R}^n | [\mathbf{D}F(\mathbf{a})]\mathbf{x} = \mathbf{0}\} \\
        T_{\mathbf{a}}M &= ker[\mathbf{D}F(\mathbf{a})]
    \end{align*}
\end{definition}
\section{Background}
Given a function $f: \mathbb{R}^n \rightarrow \mathbb{R}$, we'd like to find a vector $\mathbf{x}$ on
a smooth $k$-manifold $M$ embedded in $\mathbb{R}^n$. That maximizes or minimizes the function $f$. This
is called \textit{Constrained Optimization}, and can be solved using the method of Lagrange Multipliers.

Due to the definition of a manifold we gave earlier, we can phrase this problem in a somewhat more
accessable light. Suppose we have a system of $p$ \textit{constraint equations} of $n$ variables
represented by the equation $F(x) = 0$ for  $F: \mathbb{R}^n \rightarrow \mathbb{R}^{p}$. This
defines a set of points in $\mathbb{R}^n$ that satisfy these equations, and if we find that
the derivative of $F$ is onto, then we know that these points form a smooth manifold. Now given
these constraints, it seems natural in many practical scenarios that we'd want to find extremum
that satisfy these constaints. For instance, we'd like to know how to minimize cost or maximize
profit or utility given some set of constraints. Given some cost or utility function $f$, Lagrange
multipliers give us a way to answer that question.

\section{Statement}
\begin{theorem}[Method of Lagrange Multipliers]
    Suppose $M$ is a smooth $k$-manifold described by the function $F: \mathbb{R}^n \rightarrow \mathbb{R}^{n-k}$
    such that $F(\mathbf{x}) = 0$ and $\mathbf{a}$ is an extremum of some function  $f: \mathbb{R}^n
    \rightarrow \mathbb{R}$ on $M$. Then
    \[ \left[ \mathbf{D}f(\mathbf{a}) \right] = \sum_i^{n-k} \lambda_i \left[\mathbf{D}F_i(\mathbf{a})\right] \]
    Or in terms of gradients,
    \[ \nabla f(\mathbf{a}) = \sum_i^{n-k} \lambda_i \nabla F_i(\mathbf{a}) \]
    \[ \nabla f(\mathbf{a}) \in span\left(\{\nabla F_1(\mathbf{a}), \dots,\nabla F_{n-k}(\mathbf{a})\}\right) \]
\end{theorem}

\section{Intuition}
First let's discuss \textit{why} this should be true, and then discuss a strategy for proving it.

Naively, we begin to optimize functions by using the first derivative test - a function is at a local
extremum if movement in any direction will no longer increase/descrease the value of the function.
More precisely, if the directional derivative is $0$ for all vectors ($[\mathbf{D}f(\mathbf{a})]\mathbf{v} = \mathbf{0}, \forall \mathbf{v} \in \mathbb{R}^n$), then we're at a potential extremum.

Note, however, that in order to maximize $f$ on the manifold, we cannot simply seek out a maximum for $f$.
There's no guarantee that any maximum of $f$ exists in $\mathbb{R}^n$, let alone lies on $M$. Thus we must begin
working from the manifold, rather than from the function we optimize.

The main benefit of working on the manifold is that we have few directions we can move, and thus it is easier
to be a local extremum. Consider that the gradient $\nabla f$ is considered the direction of greatest
increase, and it's negation is the direction of greatest decrease. If we want to be at an extremum, these must
be orthogonal to the manifold - no movement on the manifold can change the value of $f$. However, the consider
each of the  individual constraint functions, $F_1, ..., F_{n-k}$. Each of these has a gradient function
$\nabla F_i$ which is orthogonal to the manifold - In fact, these span the orthogonal complement of the
tangent space of the manifold at a point. Thus, at an extrema, $\nabla f$ should be in the span of
$\{ \nabla F_1, ..., \nabla F_{n-k} \}$, which is exactly what the method of Lagrange Multipliers
says.

\section{Proof}
At a local maximum of $f$ on $M$, $\mathbf{a}$, we must be able to move in every direction on the
manifold without increasing the value of the $f$ (otherwise, a point $\varepsilon$ in that direction
would give a greater value of $f$ than $\mathbf{a}$, contradicting the fact that $\mathbf{a}$ is a
local maximum. The same argument goes for minima - we must be able to move in every direction without
decreasing $f$. Since derivatives are continuous on smooth manifolds, we must have
    \[\forall \mathbf{v} \in T_{\mathbf{a}}M = \ker[\mathbf{D}F(\mathbf{a})] \]
that
    \begin{align*}
    [\mathbf{D}f(\mathbf{a})&]\mathbf{v} = 0  \\
    \mathbf{v} \in \ker&[\mathbf{D}f(\mathbf{a})]
    \end{align*}
Which implies that
    \[\ker[\mathbf{D}F(\mathbf{a})] \subset  \ker[\mathbf{D}f(\mathbf{a})] \]
This is often one stopping point for this result, but deriving the original equation requires further
manipulation. First note that since $[\mathbf{D}F(\mathbf{a})]\mathbf{v} = \mathbf{0}$ (since
$\mathbf{v} \in \ker[\mathbf{D}F(\mathbf{a})]$), then $\forall i \in \{1, \dots,  n-k\}$
    \[ [\mathbf{D}F_i(\mathbf{a})]\mathbf{v} = 0 \]
plus, from the above
    \[ [\mathbf{D}f(\mathbf{a})]\mathbf{v} = 0 \]
For all $\mathbf{v} \in T_{\mathbf{a}}M$. We can consider these in terms of gradients:
\begin{align*}
    \nabla F_1 \cdot \mathbf{v} &= 0  \\
    \dots \\
    \nabla F_{n-k} \cdot \mathbf{v} &= 0 \\
    \nabla f \cdot \mathbf{v} &= 0
\end{align*}
Now it is clear to see that $\{\nabla f, \nabla F_1, ..., \nabla F_{n-k}\}$ lie in the orthogonal complement
to the Tangent Space $T_{\mathbf{a}}M$ (they're all orthogonal to every vector in the tangent space!).

Now, since they both live in $\mathbb{R}^n$, the orthogonal complement
should have dimension $n - dim(T_{\mathbf{a}}M) = n - k$, since the sum of dimension of any space and
it's orthogonal complement must be the dimension of the space it's embedded in. Now we have $n-k+1$
vectors in a space with dimension $n-k$. Thus they cannot be linearly independent, and one must be a linear
combination of the others. thus we can write
    \[ \nabla f = \sum_i^{n-k} \lambda_i \nabla F_i \]
Or, back in matrix notation,
    \[ [\mathbf{D}f(\mathbf{a})] = \sum_i^{n-k} [\mathbf{D}F_i(\mathbf{a})] \]
Concluding our proof.
\end{document}


\documentclass{article}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{listings}
\usepackage{graphicx}
\usepackage[section]{placeins}
\usepackage{titlesec}
\usepackage{amsmath}
\usepackage[margin=1in]{geometry}
\pagestyle{fancy}
\fancyhf{}
\rhead{Rahul Shah}
\lhead{Machine Learning: Nanodegree}
\rfoot{\thepage}
\title{Supervised Learning: Linear Regression}
\begin{document}
\maketitle
\section{Motivation}
% Talk about the housing problem and trying to predict the next house in
Imagine you are trying to predict the prices of houses. The only data you have available to you are the prices of a bunch of houses, and the square footage of all the houses. If given another house, alongside the square footage of said house, can you predict the price of the house?

If we assume that increasing square footage is approximately proportional to the price by a constant (the data is approximately linear), we can plot each point $P_i = (x, y) = (sq ft, \$\$)$ in a scatter plot and find a line of best fit to the data.

\section{Fitting a Line}
% If we know the thing is linear, then we can use a linear model to predict it
For any set of data \emph{assuming the data is approximately linear}, we can use a linear model to predict the output of any new data that comes in.
% make sure you mention the linear model we are using and what the coefficients mean
As a reminder, the linear model we are working with (for now) is:\newline $\hat{y} = \beta_1x + \beta_2$\newline where \newline $\hat{y} :=$ Predicted output of the linear model.\newline Note that if we increase $\beta_1$, we rotate the line CCW, if we decrease $\beta_1$, we rotate the line CW, and increasing/decreasing $\beta_2$ pulls up/down the line.
\section{Moving a Line Towards a Point}
% This is how we can approximate a line movement
Let's assume the line we have to work with is: $y(x) = \beta_1x + \beta_2$ ($y(x)$ will be treated as y unless explicitly used as a function), where $\beta_1, \beta_2$ are arbitrary constants, like a really bad guess as to what the actual line would be. Place a point reasonably far away from the line, $P = (p, q)$. How do we, using the data that we have, move the line such that it gets closer to the point?
\subsection{Absolute Trick}
The Absolute Trick is as such: Every iteration, we change $\beta_1$ by the value $p$ (the x coordinate), and change the value $\beta_2$ by the value 1 (we will get to cases where we increase/decrease in a second). So our model transforms to:\\
$y = \beta_1x+\beta_2 \rightarrow y = (\beta_1 \pm p)x+(\beta_2 \pm 1)$, where each $\pm$ is independent of one another\\
If we continue under the assumption that the point is far away, we might get a good approximation. In all likelihood, however, we are likely to overshoot. So instead we iterate over this multiple times, taking steps towards the right solution. We can do this by multiplying $p$ and 1 by $\alpha$, the learning rate. So our formula becomes:\\
$y = \beta_1x+\beta_2 \rightarrow y = (\beta_1 \pm p\alpha)x+(\beta_2 \pm \alpha)$\\
To determine whether to use + or -, we need to look at how we want the line to move. For $\beta_1$, we see which direction (CCW or CW) we should rotate the line to reach the point (smallest angle to sweep across so the line meets the point). For $\beta_2$, do the same thing, but this time your distance is up/down.
\subsection{Square Trick}
The above trick works to get near to the point, but we never factor in the y distance from the line (other than to determine the sign). What if we adjust $\beta_1$ and $\beta_2$ the same way as above, but also account for the vertical distance from the point to the line? We can do this by multiplying our $p\alpha$ and $\alpha$ term by $q-\hat{q}$, where $\hat{q} = y(p)$. So our formula for the square trick becomes:\\
$y = \beta_1x+\beta_2 \rightarrow y = (\beta_1 + p\alpha(q-\hat{q}))x+(\beta_2 + \alpha(q-\hat{q}))$\\
Note that we don't need to use $\pm$ anymore because multiplying by $q-\hat{q}$ means that if our point is below our line, this difference will yield a negative number,  and if the point is above our line, the difference will yield a positive number.
\subsection{Why not just do $\frac{q}{p}x$ as our line?}
Obviously these tricks are pretty bad standalone operations, but their power is in the fact that they aren't exact. When we do linear regression, we're modifying our line of best fit so it coincides with all the data, and, unless all of our data points are co-linear (HIGHLY unlikely!), we want our line of best fit to be close to all of our data points. Think of each point pulling towards a line, the farther away the point from the line, the harder it pulls the line towards it.

\section{What is the Line of Best Fit}
% Note that if we define error as the difference between our predicted y from our actual point, our best fit line is essentially the placement of the line such that the sum of all errors is at its smallest
Now that we have an iterative approach to pulling a line closer to a point, we need to define what a line of best fit is. If we define error (roughly) as the difference between our predicted y for each x value in our data, $\hat{y} = y(x)$, and the actual y value from each data point in our value, then our line of best fit is such that $\sum f(|y-\hat{y}|)$, such that $f$ is a monotonic function, is at a minimum.

\section{Linear Least Squares}
% Note on Linear Least Squares (matrix solution), and how, though it does solve the problem completely, it also involves inverting a matrix, which can be computationally taxing.

Turns out, finding the exact line of best fit for a set of points $(x_i, y_i)$ is an already solved problem, with a nice, closed form solution. Let our linear model be $\beta_1 x + \beta_0 = y$. If we consider the linear system:\\
$X\beta = y$\\
Where:\\
$X :=$ the overdetermined matrix
$\begin{bmatrix}
	1 & x_1\\
	\vdots & \vdots\\
	1 & x_n
\end{bmatrix}$\\
$y:=$ the vector of y values associated with each x in the data\\
$\beta :=$ the vector of beta constants in the linear system (from this model, it's $\beta_1$ and $\beta_0$)\\
We have an overdetermined system that does not have exact solutions for $\beta_1, \beta_0$. Since we need a square matrix were $X$ is, so we can solve it, we can multiply both sides by $X^T$ to get:\\
$X^TX\beta=X^Ty$\\
Solving for $\beta$, we get:\\
$\beta=(X^TX)^{-1}y$, where $\beta$ is guaranteed to be such that our model $\beta_1 x + \beta_0 = y$ minimizes the error.

%NEED TO CHECK THIS THERE IS A WAY BUT I DONT WANT TO DO THIS
%This can even be generalized, if we have a model $y = \beta_0 + \sum_{1}^{n} beta_if_i(\vec{x})$, where each $f$ is a polynomial, we can make the entries of each row of our matrix the functions $f_i(\vec{x})$, with every row in the matrix being one point from the input data $\vec{x} = x_1, ..., x_n$\\

Unfortunately, this linear least squares approach is rarely used. This is because the formula $\beta=(X^TX)^{-1}y$ means that we need to calculate the inverse of a matrix, which is an $O(n^3)$ operation.

% So, we take an iterative approach, (mention the mountain thing, and how, to get to the bottom, you just go where it falls off the most)

Since we can't solve this exactly, we need to get an approximation of our answer via iterative methods.

\section{Error Functions}
Since our method must be iterative and will not give us the exact answer, it would be useful to have an idea of what our error for any given model is.
\subsection{Mean Approximation Error (MAE)}
MAE $=\frac{1}{m}\sum |y_i - \hat{y}(x_i)$\\
We use absolute value so we do not have negative differences accidentally canceling positive ones. It's also a metric.
\subsection{Mean Squared Error (MSE)}
MSE $=\frac{1}{2m}\sum (y_i - \hat{y}(x_i))^2$\\
The added $\frac{1}{2} is a constant added for convienience.$

\section{Gradient Descent}
% Gradient is basically just the slope of the moutain at any point around you
Since our goal is to minimize error, why not just change $\beta_0, \beta_1$ in the direction of the greatest decrease of the error function, multiplied by a small number (so we don't overshoot)? That is, every iteration,\\
$\beta_i = \beta_i - \alpha\frac{\partial}{\partial \beta_i} Error = \beta_i - \alpha\frac{\partial Error}{\partial y_i}\frac{\partial y_i}{\partial x_i}$\\
If we apply this to our MAE and MSE square functions, we essentially get the Absolute and Square Trick, respectively

\subsection{Types of Gradient Descent}
% Batch, Stochastic, and Mini-Batch
% First two are basically terrible
% give pseudocode for mini-batch
Batch - compute the average change in $\beta$ using all data points, update $\beta$, and iterate over that
Stochastic - compute the change in $\beta$ using one data point, updating $\beta$, then do another, etc.
Mini-Batch - Split your data into small, even groups, and compute the change, and iterate over that.

Batch and Stochastic are pretty slow, so we do mini batch typically
% Can we make b just part of the matrix
\begin{algorithm}
	\caption{Gradient Descent- Mini Batch with MSE}
	\begin{algorithmic}
		\Procedure{Step}{$X$, $\beta$, $y$, $b$, $\alpha$}\\
			 \Comment $X$ the matrix containing loaded input data, $\beta$ the current values of our model's coefficients, $b$ current value of the regression intercept ($\beta_0$), $\alpha$ the step size.

			 $\hat{y} = XW + b$
			 $error = y - \hat{y}$

			 $W += \alpha\times error\times X$ \Comment this is just the derivative (negative is removed)
			 $b += \alpha * \sum error_i$
		\EndProcedure
	\end{algorithmic}
\end{algorithm}

\section{Generalizing to Higher Dimensions}
% Absolutely give matrix translations here or else nothing makes any goddamn sense

For an input vector $\vec{x} = x_1, ..., x_n$, we get an $n-1$ dimensional hyperplane.

\section{Warnings}
% Just copy whats on the Udacity site basically

\section{Polynomial Regression}
Basically, you can fit your model to any polynomial, so long as you treat each power of each input variable as another term on your x vector. Meaning, to model a quadratic,, you need rows of length 3 (const, power 1, power 2)

\section{Regularization}
Naturally we can fit multiple models to the same set of data. We can also minimize error by just doing linear least squares to a polynomial of an arbitrarily high power, but this makes an extremely computationally complex problem to solve. We also end up with a model that does not generalize to new data. To account for that, we regularize the model by summing up the elements in $\beta$ and tacking that onto our total error. Models that do not generalize well will have a much larger regularization parameter.

Sometimes, we want our model to be more accurate (less actual error) and are okay if the model is complex, so we multiply this regularization parameter by $\lambda$ to lower the error "punishment". If we can tolerate higher errors & need simplicity (perhaps operating on a lot of data), we use a higher $\lambda$ on our regularization parameter. \\

2 kinds of regularization:\\
L1: take the L1 distance of the coefficients\\
L2:    "     L2             "
\section{Feature Scaling}
When we implement regularization or are using a distance based metric, we need to scale the range of our input vector values so that the penalty for error is uniform, and you reach convergence faster. Essentially what you want to do is to make each feature (each $x_i$ for each piece of data) and make it so its mean is 0 and standard deviation is 1. You can also just normalize the data from 0 to 1 or standardize the values such that each value is a z-score
\section{External Resources}
%no later i hate this
\end{document}


% !TEX root = ../master/master.tex

%% Dexter Barrows, 2016
%% dbarrows.github.io

	Particle filters are similar to MCMC-based methods in that they use likelihoods to evaluate the validity of proposed parameter sets given observed data $D$, but differ in that they are largely trying to produce point estimates of the parameters instead of samples from the posterior distribution.

	Instead of constructing a Markov chain and approximating its stationary distribution, a cohort of ``particles'' are used to move through the data in an on-line (sequential) fashion with the cohort being culled of poorly-performing particles at each iteration via importance sampling. If the culled particles are not replenished, this will be a Sequential Importance Sampling (SIS) particle filter. If the culled particles are replenished from surviving particles, in a sense setting up a process analogous to Darwinian selection, then this will be a Sequential Importance Resampling (SIR) particle filter \cite{Arulampalam2002}.

\section{Formulation}

	Particle filters, also called Sequential Monte-Carlo (SMC) filters, feature similar core functionality as the venerable Kalman Filter. As the algorithm moves through the data (sequence of observations), a prediction-update cycle is used to simulate the evolution of the model $M$ with different particular parameter selections, track how closely these predictions approximate the new observed value, and update the current cohort appropriately \cite{Arulampalam2002}.

	Two separate functions are used to simulate the evolution and observation processes. The ``true'' state evolution is specified by

	\begin{equation}
		X_{t+1} \sim f_1 (X_t, \theta),
	\end{equation}

	And the observation process by

	\begin{equation}
		Y_t \sim f_2 (X_t, \theta).
	\end{equation}

	Components of $\theta$ can contribute to both functions, but a typical formulation is to have some components contribute to $f_1 (\cdot, \theta)$ and others to $f_2 (\cdot,\theta)$.

	The prediction part of the cycle uses $f_1 (\cdot, \theta)$ to update each particle's current state estimate to the next time step, while $f_2 (\cdot, \theta)$ is used to evaluate a weighting $w$ for each particle which will be used to determine how closely that particle is estimating the true underlying state of the system. Note that $f_2 (\cdot, \theta)$ could be thought of as a probability of observing a piece of data $y_t$ given the particle's current state estimate and parameter set, $P(y_t | X_t, \theta)$. Then, the new cohort of particles is drawn from the old cohort proportional to the weights. This process is repeated until the set of observations $D$ is exhausted.

\section{Algorithm}

    Now we will formalize the particle filter.

    We will denote each particle $p^{(j)}$ as the $j^{\text{th}}$ particle consisting of a state estimate at time $t$, $X_t^{(j)}$, a parameter set $\theta^{(j)}$, and a weight $w^{(j)}$. Note that the state estimates will evolve with the system as the cohort traverses the data.

    The algorithm for a Sequential Importance Resampling particle is shown in Algorithm [\ref{pfsir}].

    \begin{algorithm}

        \BlankLine

        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \DontPrintSemicolon

        \tcc{Select a starting point}
        \Input{Observations $D = y_1, y_2, ..., y_T$, initial particle distribution $P_0$ of size $J$}

        \BlankLine

        \tcc{Setup}
        Initialize particle cohort by sampling $(p^{(1)}, p^{(2)}, ..., p^{(J)})$ from $P_0$

        \BlankLine

        \For{$t = 1:T$}{

            \BlankLine

            \tcc{Evolve}
            \For{j = 1:J}{
            	$X_t^{(j)} \gets f_1 (X_{t-1}^{(j)}, \theta^{(j)})$
            }

            \BlankLine

            \tcc{Weight}
            \For{j = 1:J}{
            	$w^{(j)} \gets P(y_t | X_t^{(j)}, \theta^{(j)}) = f_2 (X_t^{(j)}, \theta^{(j)})$
            }

            \BlankLine

            \tcc{Normalize}
            \For{j = 1:J}{
            	$w^{(j)} \gets w^{(j)} / \sum_{1}^{J} w^{(j)}$
            }

            \BlankLine

            \tcc{Resample}
            $p^{(1:J)} \gets \text{sample}(p^{(1:J)}, \text{prob} = w, \text{replace} = true)$
        }

        \BlankLine

        \tcc{Samples from approximated posterior distribution}
        \Output{Cohort of posterior samples $(\theta^{(1)},\theta^{(2)},...,\theta^{(J)})$}

        \BlankLine

        \caption{SIR particle filter \label{pfsir}}

    \end{algorithm}

\section{Particle Collapse}

	Often, a situation may arise in which a single particle is assigned a normalized weight very close to 1 and all the other particles are assigned weights very close to 0. When this occurs, the next generation of the cohort will overwhelmingly consist of descendants of the heavily-weighted particle, termed particle collapse or degeneracy \cite{Bengtsson2008}\cite{Arulampalam2002}.

	Since the basic SIR particle filter does not perturb either the particle system states or system parameter values, the cohort will quickly consist solely of identical particles, effectively halting further exploration of the parameter space as new data is introduced.

	A similar situation occurs when a small number of particles (but not necessarily a single particle) split almost all of the normalized weight between them, then jointly dominate the resampling process for the remainder of the iterations. This again halts the exploration of the parameter space with new data.

	In either case, the hallmark feature used to detect collapse is the same -- at some point the cohort will consist of particles with very similar or identical parameter sets which will consequently result in their assigned weights being extremely close together.

	Mathematically, we are interested in the number of effective particles, $N_{\small\text{eff}}$, which represents the number of particles that are acceptably dissimilar. This is estimated by evaluating

	\begin{equation}
		N_{\small\text{eff}} = \frac{1}{\sum_1^J (w^{(j)})^2}.
	\end{equation}

	This can be used to diagnose not only when collapse has occurred, but can also indicate when it is near \cite{Arulampalam2002}.

\section{Iterated Filtering and Data Cloning}

	A particle filter hinges on the idea that as it progresses through the data set $D$, its estimate of the posterior carried in the cohort of particles approaches maximum likelihood. However, this convergence may not be fast enough so that the estimate it produces is of quality before the data runs out. One way around this problem is to ``clone'' the data and make multiple passes through it as if it were a continuation of the original time series. Note that the system state contained in each particle will have to be reset with each pass.

	Rigorous proofs have been developed \cite{Ionides2006}\cite{Ionides2015} that show that by treating the parameters as stochastic processes instead of fixed values, the multiple passes through the data will indeed force convergence of the process mean toward maximum likelihood, and the process variance toward 0.

\section{Iterated Filtering 2 (IF2)}

	The successor to Iterated Filtering 1 \cite{Ionides2006}, Iterated Filtering 2 \cite{Ionides2015} is simpler, faster, and demonstrated better convergence toward maximum likelihood. The core concept involves a two-pronged approach. First, a data cloning-like procedure is used to allow more time for the parameter stochastic process means to converge to maximum likelihood, and frequent cooled perturbation of the particle parameters allow better exploration of the parameter space while still allowing convergence to good point estimates.

	IF2 is not designed to estimate the full posterior distribution, instead to produce a Maximum Likelihood (ML) point estimate. Further, IF2 thwarts the problem of particle collapse by keeping at least some perturbation in the system at all times. It is important to note that while true particle collapse will not occur, there is still risk of a pseudo-collapse in which all particles will be extremely close to one another so as to be virtually indistinguishable. However this will only occur with the use of overly-aggressive cooling strategies or by specifying an excessive number of passes through the data.

	An important new quantity is the particle perturbation density denoted $h(\theta|,\sigma)$. Typically this is multivariate Normal with $\sigma$ being a vector of variances proportional to the expected values of $\theta$. In practice the proportionality can be derived from current means or specified ahead of time. Further, these intensities must decrease over time. This can be done via exponential or geometric cooling, a decreasing step function, a combination of these, or through some other similar scheme.

	The algorithm for IF2 can be seen in Algorithm [\ref{if2}].\\

    \begin{algorithm}

        \BlankLine

        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \DontPrintSemicolon

        \tcc{Select a starting point}
        \Input{Observations $D = y_1, y_2, ..., y_T$, initial particle distribution $P_0$ of size $J$, decreasing sequence of perturbation intensity vectors $\sigma_1, \sigma_2, ..., \sigma_M$}

        \BlankLine

        \tcc{Setup}
        Initialize particle cohort by sampling $(p^{(1)}, p^{(2)}, ..., p^{(J)})$ from $P_0$

        \BlankLine

        \tcc{Particle seeding distribution}
        $\Theta \gets P_0$

        \BlankLine

        \For{$m = 1:M$}{

        	\BlankLine

        	\tcc{Pass perturbation}
            \For{j = 1:J}{
            	$p^{(j)} \sim h(\Theta^{(j)}, \sigma_m)$
            }

            \BlankLine

	        \For{$t = 1:T$}{

	        	\BlankLine

		        \For{j = 1:J}{

		        	\BlankLine
		        	\tcc{Iteration perturbation}
	            	$p^{(j)} \sim h(p^{(j)}, \sigma_m)$

	            	\BlankLine
	            	\tcc{Evolve}
	            	$X_t^{(j)} \gets f_1 (X_{t-1}^{(j)}, \theta^{(j)})$

	            	\BlankLine
	            	\tcc{Weight}
	            	$w^{(j)} \gets P(y_t | X_t^{(j)}, \theta^{(j)}) = f_2 (X_t^{(j)}, \theta^{(j)})$

	            }

	            \BlankLine

	            \tcc{Normalize}
	            \For{j = 1:J}{
	            	$w^{(j)} \gets w^{(j)} / \sum_{1}^{J} w^{(j)}$
	            }

	            \BlankLine

	            \tcc{Resample}
	            $p^{(1:J)} \gets \text{sample}(p^{(1:J)}, \text{prob} = w, \text{replace} = true)$

	        }

	        \BlankLine

	        \tcc{Collect particles for next pass}
	        \For{$j = 1:J$}{
	        	$\Theta^{(j)} \gets p^{(j)}$
	        }

	    }

        \BlankLine

        \tcc{Samples from approximated posterior distribution}
        \Output{Cohort of posterior samples $(\theta^{(1)},\theta^{(2)},...,\theta^{(J)})$}

        \BlankLine

        \caption{IF2 \label{if2}}

    \end{algorithm}

\section{IF2 Fitting}

    Here we will examine a test case in which IF2 will be used to fit a Susceptible-Infected-Removed (SIR) epidemic model to mock infectious count data.

    As in the previous section, the model in Equation [\ref{sirode}] was use to produce synthetic data. The same parameters and initial conditions were used, namely: parameter values were set to $\mathcal{R}_0 = 3.0, \gamma = 0.1, N = 500$, initial conditions were set to 5 infectious individuals, 495 people susceptible to infection, and no one had yet recovered from infection and been removed, and observation error was taken to be $\varepsilon_{obs} \sim \mathcal{N}(0,\sigma)$, where individual values were drawn for each synthetic data point.

    Figure [\ref{mcmcdataplot}] in the previous section shows the true SIR ODE system solution and data.

    The IF2 algorithm was implemented in C++ for speed, and integrated into the R workflow using the Rcpp package.

    There are three primary reasons we implemented our own version of IF2 instead of using POMP. First, POMP does not provide final particle state distributions, making it difficult to calibrate the algorithm parameters against the parameters used in RStan (this procedure is described in the next chapter). Second, it is prudent to cross-check the validity of an algorithm using another implementation. Third, this code can then serve as a jumping-off point for further development using Graphics Processing Unit acceleration (outlined in Chapter 8). We must acknowledge the disadvantages as well: POMP has been extensively vetted with real-world usage, and using it would require far less work as we would only need to specify the model. That being said, we believe the advantages outweigh the disadvantages in this case, and so have proceeded to develop our own implementation of IF2.

	Figure [\ref{if2kernelplot}] shows the final kernel estimates for four of the key parameters. As with HMC, the distributions are not perfect, but are promising. Unlike with HMC, these distributions are not meant to consist of samples from the true posterior distribution, but rather serve a diagnostic role.

    \begin{figure}
        \centering
        \captionsetup{width=0.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/if2kernels.pdf}
        \caption{Kernel estimates for four essential system parameters. True values are indicated by dashed lines. \label{if2kernelplot}}
    \end{figure}


\section{Washburn's Algorithm}

We have attempted to break down Washburn's algorithm and explain it in
somewhat more detail than he does in his paper. Readers should be
warned that we use different notation and our $i,j$ indices are
transposed from his.

Washburn notes that the problem can be conceived as that of finding
the appropriate links to draw between the two parts of a bipartite
graph with Area nodes on one side and Resource nodes on the other. For
example, in Figure \ref{Fig:wbnodes} there are 4 areas (index $i$) and
3 resources (index $j$).
\begin{figure}[htbp]
  \begin{center}
    \includegraphics{wbnodes.eps}
    \caption{Illustration of the nodes in a Washburn graph. Any link between A and R is an allocation of resource $j$ to area $i$.}
    \label{fig:wbnodes}
  \end{center}
\end{figure}
In his paper, Washburn proves that an optimal allocation will involve
no cycles in the graph. That proof is what makes his algorithm
possible.

\subsection{Initialization Step}
\label{sec:init}
To start the algorithm, choose an initial set of links and initialize
some variables. Let $G$ be the corresponding graph.
\begin{enumerate}
\item Choose an initial set of links (corresponding to combinations
  which would have $t_{ij} > 0)$ if we used this as our allocation) such that:
  \begin{enumerate}
  \item Each resource is assigned. $\forall T_j > 0, \exists i :
    t_{ij} > 0$
  \item If $t_{ij} > 0$, $w_{ij} > 0$. Otherwise the algorithm breaks.
  \item $G$ is a forest. (It contains no cycles.)
  \end{enumerate}
\item Let $\Delta_1 = 0$  \fbox{Necessary?}
\item Let $p = 1$. \fbox{Necessary?}
\end{enumerate}

We have made an arbitrary resource allocation. A fine choice is to
allocate all resources to area 1. Washburn initially allocates all
resources to some highly improbable fake area. It does not seem to be
important.

The overview is that with each iteration we update our choices by
deleting some links and adding others. In Washburn's paper, he
calculates the resulting $t_ij$ at every step. However, that appears
to be unnecessary. Note that we do not calculate or specify them for
this initialization step.

\subsection{Step 1}
\label{sec:step1}

\subsection{Step 2: Construct $\lambda$ and $\mu$}
\label{sec:step2}
Construct vectors $\lambda$ and $\mu$ such that:
\begin{enumerate}
\item $\lambda_j = w_{ij}\mu_i$ for every edge $(ij)$ in $G$
\item $\lambda_j > 0, j = 1..R$
\item $\mu_i > 0, i = 1..A$
\end{enumerate}
You are free to set your initial $\mu$ to 1. The $\mu$s correspond to
areas, and the $\lambda$s correspond to resources. If there are any
joins in the graph (several resources assigned to the same area, or
several areas served by the same resource), there will be some
constraints on your choice of $\mu$. However, because there will be no
cycles, you can just walk the tree of nearest neighbors from your
first node setting things as you go. We have sample MATLAB code that
does this.

\subsection{Section 3: Find the $\alpha$s}
\label{sec:alpha}

The graph $G$ may not be connected.

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


\chapter{Chapter 3. Elementary Set Theory}

\section*{3.9 Self-Study Questions}

	\begin{enumerate}

		\item[3.9.1] (a) is the definition of $X\subseteq Y$. Note that (d) is equivalent to (a): if there exists no $x\in X$ such that $x\notin Y$, then every $x\in X$ must also be in $Y$. Why? Well, can you find a counterexample? (h) is perhaps the most difficult to see to be correct. Also here it helps to think whether you can find a counterexample if (h) holds. Suppose that $X\nsubseteq Y$ and that (h) holds. That $X\nsubseteq Y$ means that there exists an $x\in X$ and $x\notin Y$. And (h) says that every $x\notin Y$ is also such that $x\notin X$. This would mean that some $x$ would need to be both such that $x\in Y$ and $x\notin X$, which is impossible. Hence $X\nsubseteq Y$ cannot be and we have to have $X\subseteq Y$, instead.

		\item[3.9.2] (e) is the only correct answer.

		\item[3.9.3] (a) is correct by the axiom of extensionality. (b) is not correct, since we might have, for example, \emph{one} element which is in both sets (and hence in the one iff in the other), but \emph{another} which is only in one and not the other (and hence the sets are different). (c) is more or less obviously not enough. (d) can be seen to be correct by the reasoning from 3.9.1.(d). (e) is not correct since it's not enough that we can ``pair'' the elements, they need to be the same. And (f) is too weak, it only implies $X\subseteq Y$.

		\item[3.9.4] Remember that two sets are distinct as soon as they have different members.

		\item[3.9.5--3.9.8] The correct answers follow immediately from the definitions of $\cap$ and $\cup$. Note that in logic and mathematics, we read ``or'' inclusively and therefore (e) is also correct in 3.9.5. Note further that to say that it's not the case that one thing or the other is the case is to say that both are not the case. Similarly, to say that it's not the case that two things are the case is to say that at least one of them is not the case. This, hopefully, helps with 3.9.6 and 3.9.8.

		\item[3.9.9] The correct answers follow directly from the conditions on what a function needs to do.

		\item[3.9.10] Remember from 3.6.11 that $\{f(x):x\in X'\}$ for $f:X\to Y$ and $X'\subseteq X$ is defined as the set $\{y:\text{there exists a }x\in X'\text{, such that }y=f(x)\}$. This means that an element $m$ is not in the set $\{n^2:n\in\mathbb{N}\text{ and }0\leq n\leq 10\}$ just in case it's not the case that there exists an $n\in\mathbb{N}$ such that $0\leq n\leq 10$ and $m=n^2$. But that's just the same as (b). Note that (a) is not correct since $4\in \{n^2:n\in\mathbb{N}\text{ and }0\leq n\leq 10\}$ since $0\leq 2\leq 4$ and $2^2=4$ but there exists nine numbers $0\leq n\leq 10$ with $n^2\neq 4$: $0^2=0, 1^2=1, 3^2=9, \mathellipsis, 10^2=100$.

	\end{enumerate}

\section*{3.10 Exercises}

	\begin{itemize}

	\item[3.10.1]

	\begin{enumerate}[(a)]
		    \item $\{1,3\}$
 		   \item $\{1,2,3,5\}$
 		   \item $X\setminus Y = \{2\},\ Y\setminus X = \{5\}$
   		 \item $\wp(X) = \{\emptyset,\{1\},\{2\},\{3\}, \{1,2\},\{1,3\},\{2,3\},\{1,2,3\}\}$\\$\wp(Y)=\{\emptyset,\{1\},\{3\},\{5\}, \{1,3\},\{1,5\},\{3,5\},\{1,3,5\}\} $
   		 \item $X \times Y = \{(1,1),(1,3),(1,5),(2,1),(2,3),(2,5),(3,1),(3,3),(3,5)\}$\\$Y \times X = \{(1,1),(1,2),(1,3),(3,1),(3,2),(3,3),(5,1),(5,2),(5,3)\}$
		\end{enumerate}

      \item[3.10.2]

        \begin{enumerate}[(a)]

          \item To prove $X\subseteq Y$ iff $X\cup Y = Y$, we need to prove both directions.\\$\Rightarrow\\ to\ prove:\\$If $ X\subseteq Y,\ $then $ X\cup Y = Y \\$ Suppose $X \subseteq Y$. We'll show that $X \cup Y = Y$. To show this, we must prove two things:

                \begin{enumerate}[(i)]
                  \item $Y \subseteq X \cup Y$: from the definition of union follows that $X\cup Y$ consists of all elements in $X$ and all elements in $Y$, hence all elements in $Y$ are in $X\cup Y$, which is what we needed to show.
                  \item $X\cup Y \subseteq Y$ Let $x$ be an arbitrary element of $X \cup Y$.
                        By the definition of union, we know that $x \in Y$ or $X \in X$.
                        We now make a distinction by cases (see \S2.3.9).
                        When $x \in Y$, we are fine, as we want to show that $x \in Y$.
                        When $x \in X$, we know by our assumption that $X\subseteq Y$, that $x \in Y$.
                        Therefore all $x \in X\cup Y$ are in $Y$, therefore $X\cup Y \subseteq Y$.
                \end{enumerate}
            Using the axiom of Extensionality, we can now conclude that $X \cup Y = Y$. \\\\
            $\Leftarrow\\to\ prove:$\\If $X\cup Y=Y$, then $X\subseteq Y$. We'll prove the contrapositive. We assume that $X \not \subseteq Y$. Hence there must be some element $x\in X$ such that $x\not \in Y$. As $X \cup Y$ contains all elements of $X$, it must also contain $x$. As there's one element that is in $X\cup Y$ and not in $Y$, $X \cup Y \neq Y$, which is what we needed to prove.\\\\ As we've now proved both directions, we've proved $X\subseteq Y$ iff $X\cup Y = Y \square$

          \item  We aim to show that $X \subseteq Y$ iff $X \cap Y = X$. To prove this we need to prove both sides of the biconditional:
            \begin{itemize}
              \item $\Rightarrow$. We need to show that if $X \subseteq Y$ then  $X \cap Y = X$. So, assume $X \subseteq Y$.
                We want to show that $X \cap Y = X$.
                By extionsionality, we must show two things:
                \begin{itemize}
                  \item First, we need to show that $X \cap Y \subseteq X$.
                    So, let an element $x \in X \cap Y$.
                    That means, by definition of $\cap$, that $x \in X$ and $x \in Y$.
                    But then certainly, $x \in X$, as desired.
                  \item Second, we need to show that $X \subseteq X \cap Y$.
                    So, let an element $x \in X$.
                    We have assumed that $X \subseteq Y$,
                    from which it follows that $x \in Y$.
                    As such we have $x \in X$ and $x \in Y$, which means, by definition of $\cap$, that $x \in X \cap Y$.
                \end{itemize}

                By the axiom of extensionality we can conclude $X \cap Y = X$.

              \item $\Leftarrow$. We need to show that if $X \cap Y = X$, then $X \subseteq Y$.
                We do a proof by contraposition.
                So, assume $X \not \subseteq Y$.
                That means there is an element $x \in X$ for which $x \not \in Y$.
                If $x \not \in Y$, then $x \not \in X \cap Y$.
                So $X \not \subseteq X \cap Y$ (because there is an $x \in X$ for which $x \not \in X \cap Y$) so $X \not = X \cap Y$.

            \end{itemize}

We have now proved both directions, so $X \subseteq Y$ iff $X \cap Y = X$.

\end{enumerate}
\item[3.10.3] \
\begin{enumerate}
    \item $	\begin{matrix}
		(1,1) && \overset{f}{\mapsto} && 1 \\
		(1,2) && \overset{f}{\mapsto} && 1 \\
		(1,3) && \overset{f}{\mapsto} && 1 \\
	\end{matrix}
	\qquad
	\begin{matrix}
		(2,1) && \overset{f}{\mapsto} && 1 \\
		(2,2) && \overset{f}{\mapsto} && 2\\
		(2,3) && \overset{f}{\mapsto} && 2\\
	\end{matrix}
	\qquad
		\begin{matrix}
 		(3,1)&& \overset{f}{\mapsto} && 1\\
		(3,2)&& \overset{f}{\mapsto} && 2\\
		(3,3) && \overset{f}{\mapsto} && 3\\
	\end{matrix}$
	\item $\begin{array}{c|ccc}
		f & 1 & 2 & 3 \\ \hline
		1 & 1& 1& 1 \\
		2 & 1 & 2 &2 \\
		3 & 1 & 2 & 3
	\end{array}$
	\item 	$
	f((x, y)) =
	\begin{cases}
	x \text{ if } x < y,\\
	y \text{ otherwise}
	\end{cases}
	$
\end{enumerate}{}

      \item[3.10.5] We want to prove that $f(n,m) = n + m$ for all $n, m \in \mathbb{N}$.
        We use the principal of mathematical induction.
        \emph{Base case}:
        We need to show that $f(n,0) = n + 0$.
        By the definition $f(n,0) = n$.
        It is trivial that $n = n + 0$.
        \emph{Induction step}:
        We need to show that for all $n, m \in \mathbb{N}$,
        if $f(n,m)=n+m$, then $f(n,m+1)=(n+m)+1$.
        So, let $n, m \in \mathbb{N}$.
        Assume the induction hypothesis that $f(n,m) = n + m$ (IH).
        Consider $f(n, m + 1)$.
        By the definition $f(n,m+1) = f(n,m) + 1$.
        By the IH and get $f(n,m+1) = n + m + 1$, which is what we had to show.
        By the principle of mathematical induction,
        we conclude that for all $n, m \in \mathbb{N}$ it is true that $f(n,m) = n + m$.

\item[3.10.7]
\begin{enumerate}[(a)]
    \item
    $l: Gargle \rightarrow \mathbb{N}$:\begin{enumerate}[(i)]
        \item \begin{enumerate}[(a)]
            \item $l(\clubsuit) = 1$
        \item $l(\spadesuit) = 1$
        \end{enumerate}
        \item \begin{enumerate}[(a)]
            \item $l(\Diamond x\Diamond)= l(x) + 2$
            \item $l(x\heartsuit y)= l(x)+l(y)+1 $
        \end{enumerate}
    \end{enumerate}

    \item     $\textbf{1}_\heartsuit: Gargle \rightarrow \{0,1\}$:\begin{enumerate}[(i)]
        \item    $\textbf{1}_\heartsuit(\clubsuit) =\textbf{1}_\heartsuit(\spadesuit) = 0$
        \item \begin{enumerate}[(a)]
            \item $\textbf{1}_\heartsuit(\Diamond x\Diamond)= \textbf{1}_\heartsuit(x)$
            \item $\textbf{1}_\heartsuit (x\heartsuit y)= 1$
        \end{enumerate}
    \end{enumerate}
\end{enumerate}
\item[3.10.8] We will prove by induction that the amount of $\Diamond$'s in a gargle is always even.
\begin{enumerate}[(i)]
    \item Base case 1: $\spadesuit$ has an even number of $\Diamond$'s, as 0 is even.
Base case 2: $\clubsuit$ has an even number of $\Diamond$'s, as 0 is even.
\item \begin{enumerate}[(a)]
    \item Assume $x$ has an even number of $\Diamond$'s.Then $\Diamond x \Diamond$ must also have an even number of $\Diamond$'s, as the number of $\Diamond$'s of $\Diamond x \Diamond$ is the number of $\Diamond$'s of $x$+2. We know that an even number +2 results in another even number.
    \item Assume $x,\ y$ have an even number of $\Diamond$'s. Then the number of $\Diamond$'s of $x\heartsuit y$ will be the number of $\Diamond$'s of $x$ + the number of $\Diamond$'s of $y$, which will be an even number, as an even number added to an even number results in an even number, which is what we needed to show.\\
\end{enumerate}
\end{enumerate}
    By means of induction we've now proved that the number of $\Diamond$'s in a Gargle is always even.
\item[3.10.9]
We will prove that $\spadesuit\Diamond\heartsuit\spadesuit \not \in Gargle$. We have just proven that every gargle has an even amount of $\Diamond$, but this one has exactly 1, and $1 = 0 \cdot 2 + 1$. Therefore 1 is odd, and not even. So we must conclude it's not a Gargle.
\end{itemize}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../logic.tex"
%%% End: