\chapter{Constructing the Borel and Lebesgue measure}
It's very difficult to define in one breath a measure
on the Borel space $\SB(\RR^n)$.
It is easier if we define a weaker notion first.
There are two such weaker notions that we will define:
\begin{itemize}
	\ii A \textbf{pre-measure}:
	satisfies the axioms of a measure,
	but defined on \emph{fewer} sets than a measure:
	they'll be defined on an ``algebra''
	rather than the full-fledged ``$\sigma$-algebra''.

	\ii An \textbf{outer measure}:
	defined on $2^\Omega$ but satisfies weaker axioms.
\end{itemize}
It will turn out that pre-measures yield outer measures,
and outer measures yield measures.

\section{Pre-measures}
\prototype{Let $\Omega = \RR^2$. Then we take $\SA_0$ generated by rectangles,
	with $\mu_0$ the usual area.}
The way to define a pre-measure is to weaken
the $\sigma$-algebra to an algebra.
\begin{definition}
	Let $\Omega$ be a set.
	We define notions of an \vocab{algebra},
	which is the same as $\sigma$-algebra except
	with ``countable'' replaced by finite everywhere.

	That is: an algebra $\SA_0$ on $\Omega$ is a
	nonempty subset of $2^\Omega$,
	which is closed under complement and \emph{finite} union.
	The smallest algebra containing a subset $\SF \subseteq 2^\Omega$
	is the \vocab{algebra generated by $\SF$}.
\end{definition}
In practice, we will basically always use generation for algebras.
\begin{example}
	When $\Omega = \RR^n$,
	we can let $\mathcal{L}_0$ be the algebra generated by
	$[a_1, b_1] \times \dots \times [a_n, b_n]$.
	A typical element might look like:
	\begin{center}
	\begin{asy}
		size(5cm);
		filldraw( (0,0)--(9,0)--(9,2)--(6,2)--(6,5)--(0,5)--cycle,
			opacity(0.1)+lightcyan, heavycyan );
		filldraw( (7,3)--(12,3)--(12,6)--(7,6)--cycle,
			opacity(0.1)+lightcyan, heavycyan );
	\end{asy}
	\end{center}
	Unsurprisingly, since we have \emph{finitely} many
	rectangles and their complements involved,
	in this case we actually \emph{can}
	unambiguously assign an area, and will do so soon.
\end{example}

\begin{definition}
	A \vocab{pre-measure} $\mu_0$ on a algebra $\SA_0$
	is a function $\mu_0 \colon \SA_0 \to [0, +\infty]$
	which satisfies the axioms
	\begin{itemize}
		\ii $\mu_0(\varnothing) = 0$, and
		\ii \textbf{Countable additivity}:
		if $A_1$, $A_2$, \dots are disjoint sets in $\SA_0$
		and \emph{moreover} the disjoint union $\bigsqcup A_i$
		is contained in $\SA_0$ (not guaranteed by algebra axioms!),
		then
		\[ \mu_0\left( \bigsqcup_n A_n \right) = \sum_n \mu_0(A_n). \]
	\end{itemize}
\end{definition}

\begin{example}
	[The pre-measure on $\RR^n$]
	Let $\Omega = \RR^2$.
	Then, let $\mathcal{L}_0$ be the algebra generated by rectangles
	$[a_1, a_2] \times [b_1, b_2]$.
	We then let
	\[ \mu_0\left( [a_1, a_2] \times [b_1, b_2] \right)
	= (a_2-a_1)(b_2-b_1) \]
	the area of the rectangle.
	As elements of $\mathcal{L}_0$ are simply \emph{finite} unions
	of rectangles and their complements (picture drawn earlier),
	it's not difficult to extend this to a pre-measure $\lambda_0$
	which behaves as you expect --- although we won't do this.
\end{example}

Since we are sweeping something under the rug that
turns out to be conceptually important,
I'll go ahead and blue-box it.
\begin{proposition}
	[Geometry sanity check that we won't prove]
	\label{prop:lebesgue_rectangle}
	For $\Omega = \RR^n$ and $\mathcal{L}_0$
	the algebra generated by rectangular prisms,
	one can define a pre-measure $\lambda_0$ on $\mathcal{L}_0$.
\end{proposition}
From this point forwards, we will basically do
almost no geometry\footnote{White lie.
	Technically, we will use one more fact:
	that open sets of $\RR^n$ can be covered by countably
	infinitely many rectangles,
	as in \Cref{exer:cubes_vs_open}.
	This step doesn't involve any area assignments, though.}
whatsoever in defining the measure $\SB(\RR^n)$,
and only use set theory to extend our measure.
So, \Cref{prop:lebesgue_rectangle} is the only sentry
which checks to make sure that our ``initial definition'' is sane.

To put the point another way,
suppose an \textbf{insane scientist}\footnote{Because
	``mad scientists'' are overrated.}
tried to define a notion
of area in which every rectangle had area $1$.
Intuitively, this shouldn't be possible:
every rectangle can be dissected into two halves
and we ought to have $1+1 \ne 1$.
However, the only thing that would stop them is that they couldn't
extend their pre-measure on the algebra $\mathcal{L}_0$.
If they somehow got past that barrier and got a pre-measure,
nothing in the rest of the section would prevent them
from getting an entire \emph{bona fide} measure with this property.
Thus, in our construction of the Lebesgue measure,
most of the geometric work is captured in the (omitted) proof
of \Cref{prop:lebesgue_rectangle}.

\section{Outer measures}
\prototype{Keep taking $\Omega = \RR^2$; see the picture to follow.}
The other way to weaken a measure is to relax the countable additivity,
and this yields the following:
\begin{definition}
	An \vocab{outer measure} $\mu^\ast$ on a set $\Omega$
	is a function $\mu^\ast \colon 2^\Omega \to [0, +\infty]$
	satisfying the following axioms:
	\begin{itemize}
		\ii $\mu^\ast(\varnothing) = 0$;
		\ii if $E \subseteq F$ and $E,F \in 2^{\Omega}$
		then $\mu^\ast(E) \le \mu^\ast(F)$;
		\ii for any subsets $E_1$, $E_2$, \dots of $\Omega$ we have
		\[ \mu^\ast \left( \bigcup_n E_n \right)
			\le \sum_n \mu^\ast(E_n). \]
	\end{itemize}
	(I don't really like the word ``outer measure'',
	since I think it is a bit of a misnomer:
	I would rather call it ``fake measure'',
	since it's not a measure either.)
\end{definition}

The reason for the name ``outer measure''
is that you almost always obtain outer measures
by approximating them from ``outside'' sets.
Officially, the result is often stated as follows
(as \Cref{pr:construct_outer_measure}).
\begin{quote}
	For a set $\Omega$, let $\mathcal{E}$ be \emph{any} subset of $2^{\Omega}$
	and let $\rho \colon \mathcal{E} \to [0,+\infty]$ be \emph{any} function.
	Then
	\[ \mu^\ast(E) = \inf \left\{ \sum_{n=1}^\infty \rho(E_n) \mid
		E_n \in \mathcal{E}, \;
		E \subseteq \bigcup_{n=1}^\infty E_n \right\} \]
	is an outer measure.
\end{quote}

However, I think the above theorem is basically always
wrong to use in practice, because it is \emph{way too general}.
As I warned with the insane scientist,
we really do want some sort of sanity conditions on $\rho$:
otherwise, if we apply the above result as stated,
there is no guarantee that $\mu^\ast$ will
be compatible with $\rho$ in any way.

%So, the recipe so far goes like:
%define an algebra $\SA_0$ generated by some sets with
%``known'' measure (like rectangles) and checking it extends well,
%then get an outer measure from it.
%In the next section, we will then see every outer measure gives a true measure.

So, I think it is really better to apply the theorem to pre-measures $\mu_0$
for which one \emph{does} have some sort of guarantee
that the resulting $\mu^\ast$ is compatible with $\mu_0$.
In practice, this is always how we will want to construct our outer measures.
\begin{theorem}
	[Constructing outer measures from pre-measures]
	\label{thm:construct_outer}
	Let $\mu_0$ be a pre-measure on an algebra $\SA_0$ on a set $\Omega$.
	\begin{enumerate}[(a)]
		\ii The map $\mu^\ast \colon 2^\Omega \to [0,+\infty]$ defined by
		\[ \mu^\ast(E) = \inf \left\{ \sum_{n=1}^\infty \mu_0(A_n) \mid
			A_n \in \SA_0, \; E \subseteq \bigcup_{n=1}^\infty A_n \right\} \]
		is an outer measure.
		\ii Moreover, this measure agrees with $\mu_0$ on sets in $\SA_0$.
	\end{enumerate}
\end{theorem}
Intuitively, what is going on is that
$\mu^\ast(A)$ is the infimum of coverings of $A$ by
countable unions of elements in $\SA_0$.
Part (b) is the first half of the compatibility condition I promised;
the other half appears later as \Cref{prop:cm_compatible}.

\begin{proof}
	[Proof of \Cref{thm:construct_outer}]
	As alluded to already, part (a)
	is a special case of \Cref{pr:construct_outer_measure}
	(and proving it in this generality is actually easier,
	because you won't be distracted by unnecessary properties).

	We now check (b), that $\mu^\ast(A) = \mu_0(A)$ for $A \in \SA_0$.
	One bound is quick:
	\begin{ques}
		Show that $\mu^\ast(A) \le \mu_0(A)$.
	\end{ques}
	For the reverse, suppose that $A \subseteq \bigcup_n A_n$.
	Then, define the sets
	\begin{align*}
		B_1 &= A \cap A_1 \\
		B_2 &= (A \cap A_2) \setminus B_1 \\
		B_3 &= (A \cap A_3) \setminus B_2 \\
		&\vdotswithin=
	\end{align*}
	and so on.
	Then the $B_n$ are disjoint elements of $\SA_0$ with $B_n \subset A_n$,
	and we have rigged the definition so that $\bigsqcup_n B_n = A$.
	Thus by definition of pre-measure,
	\[ \mu_0(A) = \sum_n \mu_0(B_n) \le \sum_n \mu_0(A_n) \]
	as desired.
\end{proof}

\begin{example}
	Let $\Omega = \RR^2$ and $\lambda_0$ the pre-measure from before.
	Then $\lambda^\ast(A)$ is, intuitively,
	the infimum of coverings of the set $A$ by rectangles.
	Here is a picture you might use to imagine the
	situation with $A$ being the unit disk.
	\missingfigure{circles covered by rectangles}
\end{example}

\section{Carath\'{e}odory extension for outer measures}
We will now take any outer measure and turn it into a proper measure.
To do this, we first need to specify the $\sigma$-algebra
on which we will define the measure.

\begin{definition}
	Let $\mu^\ast$ be an outer measure.
	We say a set $A$ is \vocab{Carath\'{e}odory measurable with respect
	to $\mu^\ast$}, or just \vocab{$\mu^\ast$-measurable},
	if the following condition holds:
	for any set $E \in 2^{\Omega}$,
	\[ \mu^\ast(E) = \mu^\ast(E \cap A) + \mu^\ast(E \setminus A). \]
\end{definition}
This definition is hard to motivate, but turns out to be the right one.
One way to motivate is this:
it turns out that in $\RR^n$,
it will be equivalent to a reasonable geometric condition
(which I will state in \Cref{prop:lebesgue_geo}),
but since that geometric definition requires information about $\RR^n$ itself,
this is the ``right'' generalization for general measure spaces.

Since our goal was to extend our $\SA_0$,
we had better make sure this definition
lets us measure the initial sets that we started with!
\begin{proposition}
	[Carath\'{e}odory measurability is compatible with the initial $\SA_0$]
	\label{prop:cm_compatible}
	Suppose $\mu^\ast$ was obtained from a pre-measure $\mu_0$ on
	an algebra $\SA_0$, as in \Cref{thm:construct_outer}.
	Then every set in $\SA_0$ is $\mu^\ast$-measurable.
\end{proposition}
This is the second half of the compatibility condition
that we get if we make sure our initial $\mu_0$
at least satisfies the pre-measure axioms.
(The first half was (b) of \Cref{thm:construct_outer}.)
\begin{proof}
	Let $A \in \SA_0$ and $E \in 2^{\Omega}$; we wish to prove
	$\mu^\ast(E) = \mu^\ast(E \cap A) + \mu^\ast(E \setminus A)$.
	The definition of outer measure already requires
	$\mu^\ast(E) \le \mu^\ast(E \cap A) + \mu^\ast(E \setminus A)$
	and so it's enough to prove the reverse inequality.

	By definition of infimum, for any $\eps > 0$,
	there is a covering $E \subset \bigcup_n A_n$
	with $\mu^\ast(E) + \eps \ge \sum_n \mu_0(A_n)$.
	But \[ \sum_n \mu_0(A_n)
		= \sum_n \left( \mu_0(A_n \cap A) + \mu_0(A_n \setminus A) \right)
		\ge \mu^\ast(E \cap A) + \mu^\ast(E \setminus A)  \]
	with the first equality being the definition of pre-measure
	on $\SA_0$, the second just being by definition of $\mu^\ast$
	(since $A_n \cap A$ certainly covers $E \cap A$, for example).
	Thus $\mu^\ast(E) + \eps \ge \mu^\ast(E \cap A) + \mu^\ast(E \setminus A)$.
	Since the inequality holds for any $\eps > 0$, we're done.
\end{proof}

To add extra icing onto the cake,
here is one more niceness condition
which our constructed measure will happen to satisfy.
\begin{definition}
	A \vocab{null set} of a measure space $(\Omega, \SA, \mu)$
	is a set $A \in \SA$ with $\mu(A) = 0$.
	A measure space $(\Omega, \SA, \mu)$ is \vocab{complete}
	if whenever $A$ is a null set,
	then all subsets of $A$ are in $\SA$ as well (and hence null sets).
\end{definition}
This is a nice property to have, for obvious reasons.
Visually, if I have a bunch of dust which I \emph{already} assigned weight zero,
and I blow away some of the dust,
then the remainder should still have an assigned weight --- zero.
The extension theorem will give us $\sigma$-algebras with this property.

\begin{theorem}
	[Carath\'{e}odory extension theorem for outer measures]
	\label{thm:cara_outer}
	If $\mu^\ast$ is an outer measure,
	and $\SA\cme$ is the set of $\mu^\ast$-measurable sets
	with respect to $\mu^\ast$,
	then $\SA\cme$ is a $\sigma$-algebra on $\Omega$,
	and the restriction $\mu\cme$ of $\mu^\ast$ to $\SA\cme$
	gives a \emph{complete} measure space.
\end{theorem}
(Phonetic remark: you can think of the superscript ${}\cme$ as standing
for either ``Carath\'{e}odory measurable'' or ``complete''.
Both are helpful for remembering what this represents.
This notation is not standard but the pun was too good to resist.)

Thus, if we compose \Cref{thm:construct_outer} with \Cref{thm:cara_outer},
we find that every pre-measure $\mu_0$ on an algebra $\SA_0$ naturally
gives a $\sigma$-algebra $\SA\cme$ with a complete measure $\mu\cme$,
and our two compatibility results
(namely (b) of \Cref{thm:construct_outer},
together with \Cref{prop:cm_compatible})
means that $\SA\cme \supset \SA_0$
and $\mu\cme$ agrees with $\mu$.

Here is a table showing the process,
where going down each row of the table corresponds to restriction process.
\begin{center}
	\begin{tabular}[h]{llcl}
		& & Construct order & Notes \\ \hline
		$2^\Omega$ & $\mu^\ast$ & Step 2 &
			$\mu^\ast$ is outer measure obtained from $\mu_0$ \\[1em]
		$\SA\cme$ & $\mu\cme$ & Step 3 & $\SA\cme$ defined as $\mu^\ast$-measurable sets, \\
		&&& $(\SA\cme, \mu\cme)$ is complete. \\[1em]
		$\SA_0$ & $\mu_0$ & Step 1 & $\mu_0$ is a pre-measure
	\end{tabular}
\end{center}

\section{Defining the Lebesgue measure}
This lets us finally define the Lebesgue measure on $\RR^n$.
We wrap everything together at once now.
\begin{definition}
	We create a measure on $\RR^n$ by the following procedure.
	\begin{itemize}
		\ii Start with the algebra $\mathcal{L}_0$
		generated by rectangular prisms,
		and define a \emph{pre-measure} $\lambda_0$ on this $\mathcal{L}_0$
		(this was glossed over in the example).
		\ii By \Cref{thm:construct_outer},
		this gives the \vocab{Lebesgue outer measure}
		$\lambda^\ast$ on $2^{\RR^n}$,
		which is compatible on all the rectangular prisms.
		\ii By Carath\'{e}odory (\Cref{thm:cara_outer}),
		this restricts to a complete measure $\lambda$
		on the $\sigma$-algebra $\mathcal{L}(\RR^n)$
		of $\lambda^\ast$-measurable sets
		(which as promised contains all rectangular prisms).\footnote{If
			I wanted to be consistent with the previous theorems,
			I might prefer to write $\mathcal{L}\cme$
			and $\lambda\cme$ for emphasis.
			It seems no one does this, though, so I won't.}
	\end{itemize}
	The resulting complete measure, denoted $\lambda$,
	is called the \vocab{Lebesgue measure}.

	The algebra $\mathcal{L}(\RR^n)$ we obtained will be called the
	\vocab{Lebesgue $\sigma$-algebra};
	sets in it are said to be \vocab{Lebesgue measurable}.
\end{definition}

Here is the same table from before,
with the values filled in for the special case $\Omega = \RR^n$,
which gives us the Lebesgue algebra.
\begin{center}
	\begin{tabular}[h]{llcl}
		& & Construct order & Notes \\ \hline
		$2^{\RR^n}$ & $\lambda^\ast$ & Step 2 &
			$\lambda^\ast$ is Lebesgue outer measure \\[1em]
		$\mathcal L(\RR^n)$ & $\lambda$ & Step 3 & Lebesgue $\sigma$-algebra (complete) \\[1em]
		$\mathcal L_0$ & $\lambda_0$ & Step 1 & Define pre-measure on rectangles
	\end{tabular}
\end{center}

Of course, now that we've gotten all the way here,
if we actually want to \emph{compute} any measures,
we can mostly gleefully forget about how we actually constructed
the measure and just use the properties.
The hard part was to showing that there \emph{is}
a way to assign measures consistently;
actually figuring out what that measure's value is
\emph{given that it exists} is often much easier.
Here is an example.

\begin{example}
	[The Cantor set has measure zero]
	The standard \vocab{middle-thirds Cantor set} is the subset
	$[0,1]$ obtained as follows:
	we first delete the open interval $(1/3, 2/3)$.
	This leaves two intervals $[0,1/3]$ and $[2/3,1]$
	from which we delete the middle thirds again from both,
	i.e.\ deleting $(1/9,2/9)$ and $(7/9,8/9)$.
	We repeat this procedure indefinitely and let $C$ denote the result.
	An illustration is shown below.
	\begin{center}
		\includegraphics[width=0.8\textwidth]{media/cantor-thirds.png} \\
		\footnotesize Image from \cite{img:cantor}
	\end{center}
	It is a classic fact that $C$ is uncountable
	(it consists of ternary expansions omitting the digit $1$).
	But it is measurable (it is an intersection of closed sets!)
	and we contend it has measure zero.
	Indeed, at the $n$th step, the result has measure $(2/3)^n$ leftover.
	So $\mu(C) \le (2/3)^n$ for every $n$, forcing $\mu(C) = 0$.
\end{example}

This is fantastic, but there is one elephant in the room:
how are the Lebesgue $\sigma$-algebra and the Borel $\sigma$-algebra related?
To answer this question briefly, I will state two results
(but another answer is given in the next section).
The first is a geometric interpretation of the strange
Carath\'{e}odory measurable hypothesis.
\begin{proposition}
	[A geometric interpretation of Lebesgue measurability]
	\label{prop:lebesgue_geo}
	A set $A \subseteq \RR^n$ is Lebesgue measurable
	if and only if for every $\eps > 0$,
	there is an open set $U \supset A$ such that
	\[ \lambda^\ast(U \setminus A) < \eps \]
	where $\lambda^\ast$ is the Lebesgue outer measure.
\end{proposition}
I want to say that this was Lebesgue's original formulation
of ``measurable'', but I'm not sure about that.
In any case, we won't need to use this,
but it's good to see that our definition of Lebesgue measurable
has a down-to-earth geometric interpretation.

\begin{ques}
	Deduce that every open set is Lebesgue measurable.
	Conclude that the Lebesgue $\sigma$-algebra
	contains the Borel $\sigma$-algebra.
	(A different proof is given later on.)
\end{ques}

However, the containment is proper:
there are more Lebesgue measurable sets than Borel ones.
Indeed, it can actually be proven using transfinite induction
(though we won't) that
$\left\lvert \SB(\RR) \right\rvert = \left\lvert \RR \right\rvert$.
Using this, one obtains:
\begin{exercise}
	Show the Borel $\sigma$-algebra is not complete.
	(Hint: consider the Cantor set.
	You won't be able to write down an example of a non-measurable
	set, but you can use cardinality arguments.)
	Thus the Lebesgue $\sigma$-algebra strictly contains the Borel one.
	% It should contain every subset of the Cantor set,
	% since Lebesgue is complete.
\end{exercise}

Nonetheless, there is a great way to describe the Lebesgue $\sigma$-algebra,
using the idea of completeness.
\begin{definition}
	Let $(\Omega, \SA, \mu)$ be a measure space.
	The \vocab{completion} $(\Omega, \ol{\SA}, \ol{\mu})$
	is defined as follows:
	we let
	\[ \ol{\SA} = \left\{ A \cup N \mid A \in \SA,
		N \text{ subset of null set} \right\}. \]
	and $\ol{\mu}(A \cup N) = \mu(A)$.
	One can check this is well-defined,
	and in fact $\ol{\mu}$ is the unique extension
	of $\mu$ from $\SA$ to $\ol{\SA}$.

	This looks more complicated than it is.
	Intuitively, all we are doing is ``completing'' the measure
	by telling $\ol{\mu}$ to regard any subset of a null set
	as having measure zero, too.
\end{definition}

Then, the saving grace:
\begin{theorem}
	[Lebesgue is completion of Borel]
	For $\RR^n$, the Lebesgue measure is the completion of the Borel measure.
\end{theorem}
\begin{proof}
	This actually follows from results in the next section,
	namely \Cref{exer:cubes_vs_open}
	and part (c) of Carath\'{e}odory for pre-measures (\Cref{thm:cara_premeasure}).
\end{proof}

\section{A fourth row: Carath\'{e}odory for pre-measures}
\prototype{The fourth row for the Lebesgue measure is $\SB(\RR^n)$.}
In many cases, $\SA\cme$ is actually bigger than our original goal,
and instead we only need to extend $\mu_0$ on $\SA_0$
to $\mu$ on $\SA$, where $\SA$ is the $\sigma$-algebra generated by $\SA_0$.
Indeed, our original goal was to get $\SB(\RR^n)$, and in fact:
\begin{exercise}
	Show that $\SB(\RR^n)$ is the $\sigma$-algebra generated
	by the $\mathcal{L}_0$ we defined earlier.
	\label{exer:cubes_vs_open}
\end{exercise}

Fortunately, this restriction is trivial to do.
\begin{ques}
	Show that $\SA\cme \supset \SA$,
	so we can just restrict $\mu\cme$ to $\SA$.
\end{ques}
We will in a moment add this as the fourth row in our table.

However, if this is the end goal,
than a somewhat different Carath\'{e}odory theorem
can be stated because often one more niceness condition holds:
\begin{definition}
	A pre-measure or measure $\mu$ on $\Omega$ is \vocab{$\sigma$-finite}
	if $\Omega$ can be written as a countable union $\Omega = \bigcup_n A_n$
	with $\mu(A_n) < \infty$ for each $n$.
\end{definition}
\begin{ques}
	Show that the pre-measure $\lambda_0$ we had,
	as well as the Borel measure $\SB(\RR^n)$,
	are both $\sigma$-finite.
\end{ques}
Actually, for us, $\sigma$-finite is basically always going to be true,
so you can more or less just take it for granted.

\begin{theorem}
	[Carath\'{e}odory extension theorem for pre-measures]
	\label{thm:cara_premeasure}
	Let $\mu_0$ be a pre-measure on an algebra $\SA_0$ of $\Omega$,
	and let $\SA$ denote the $\sigma$-algebra generated by $\SA_0$.
	Let $\SA\cme$, $\mu\cme$ be as in \Cref{thm:cara_outer}.
	Then:
	\begin{enumerate}[(a)]
		\ii The restriction of $\mu\cme$ to $\SA$
		gives a measure $\mu$ extending $\mu_0$.
		\ii If $\mu_0$ was $\sigma$-finite,
		then $\mu$ is the unique extension of $\mu_0$ to $\SA$.
		\ii If $\mu_0$ was $\sigma$-finite,
		then $\mu\cme$ is the completion of $\mu$,
		hence the unique extension of $\mu_0$ to $\SA\cme$.
	\end{enumerate}
\end{theorem}
Here is the updated table, with comments if $\mu_0$ was indeed $\sigma$-finite.
\begin{center}
	\begin{tabular}[h]{llcl}
		& & Construct order & Notes \\ \hline
		$2^\Omega$ & $\mu^\ast$ & Step 2 &
			$\mu^\ast$ is outer measure obtained from $\mu_0$ \\[1em]
		$\SA\cme$ & $\mu\cme$ & Step 3 & $(\SA\cme, \mu\cme)$ is completion $(\SA, \mu)$, \\
		&&& $\SA\cme$ defined as $\mu^\ast$-measurable sets \\[1em]
		$\SA$ & $\mu$ & Step 4 & $\SA$ defined as $\sigma$-alg.\ generated by $\SA_0$ \\[1em]
		$\SA_0$ & $\mu_0$ & Step 1 & $\mu_0$ is a pre-measure
	\end{tabular}
\end{center}
And here is the table for $\Omega = \RR^n$,
with Borel and Lebesgue in it.
\begin{center}
	\begin{tabular}[h]{llcl}
		& & Construct order & Notes \\ \hline
		$2^{\RR^n}$ & $\lambda^\ast$ & Step 2 &
			$\lambda^\ast$ is Lebesgue outer measure \\[1em]
		$\mathcal L(\RR^n)$ & $\lambda$ & Step 3 &
			Lebesgue $\sigma$-algebra, completion of Borel one \\[1em]
		$\SB(\RR^n)$ & $\mu$ & Step 4 &
			Borel $\sigma$-algebra, generated by $\mathcal{L}_0$ \\[1em]
		$\mathcal L_0$ & $\lambda_0$ & Step 1 & Define pre-measure on rectangles
	\end{tabular}
\end{center}

Going down one row of the table corresponds to restriction,
while each of $\mu_0 \to \mu \to \mu\cme$ is a unique extension
when $\mu_0$ is $\sigma$-finite.
\begin{proof}
	[Proof of \Cref{thm:cara_premeasure}]
	For (a): this is just \Cref{thm:construct_outer} and \Cref{thm:cara_outer}
	put together, combined with the observation that $\SA^\ast \supset \SA_0$
	and hence $\SA^\ast \supset \SA$.
	Parts (b) and (c) are more technical, and omitted.
\end{proof}

\section{From now on, we assume the Borel measure}
\todo{explain why}

\section{\problemhead}
\begin{dproblem}
	[Constructing outer measures from arbitrary $\rho$]
	\label{pr:construct_outer_measure}
	For a set $\Omega$,
	let $\mathcal{E}$ be \emph{any} subset of $2^{\Omega}$
	and let $\rho \colon \mathcal{E} \to [0,+\infty]$
	be \emph{any} function.
	Prove that
	\[ \mu^\ast(E) = \inf \left\{ \sum_{n=1}^\infty \rho(E_n) \mid
		E_n \in \mathcal{E}, \;
		E \subseteq \bigcup_{n=1}^\infty E_n \right\} \]
	is an outer measure.
\end{dproblem}

\begin{problem}
	[The insane scientist]
	Let $\Omega = \RR^2$, and let $\mathcal{E}$
	be the set of (non-degenerate) rectangles.
	Let $\rho(E) = 1$ for every rectangle $E \in \mathcal{E}$.
	Ignoring my advice, the insane scientist
	uses $\rho$ to construct an outer measure $\mu^\ast$,
	as in \Cref{pr:construct_outer_measure}.
	\begin{enumerate}[(a)]
		\ii Find $\mu^\ast(S)$ for each subset $S$ of $\RR^2$.
		\ii Which sets are $\mu^\ast$-measurable?
	\end{enumerate}
	You should find that no rectangle is $\mu^\ast$-measurable,
	unsurprisingly foiling the scientist.
	\begin{hint}
		Show that
		\[ \mu^\ast(S) = \begin{cases}
				0 & S = \varnothing \\
				1 & S \text{ bounded and nonempty} \\
				\infty & S \text{ not bounded}.
			\end{cases}
		\]
		This lets you solve (b) readily;
		I think the answer is just unbounded sets,
		$\varnothing$, and one-point sets.
	\end{hint}
\end{problem}

\begin{problem}
	\gim
	A function $f \colon \RR \to \RR$ is continuous.
	Must $f$ be measurable with respect to the Lebesgue measure on $\RR$?
\end{problem}


\section{Constrained Dynamic String Sampling}
  \label{sec:ConstrainedAlg}

  While the algorithm presented in Sec. \ref{sec:GreedyAlg} is fast for sufficiently smooth families of loss surfaces with few saddle points, here we present a slightly modified version which, while slower, provides more control over the convergence of the string.  We did not use the algorithm presented in this section for our numerical studies.

  Instead of training intermediate models via full SGD to a desired accuracy as in step $8$ of the algorithm, intermediate models are be subject to a constraint that ensures they are ``close'' to the neighboring models on the string.  Specifically, intermediate models are constrained to the unique hyperplane in weightspace equidistant from its two neighbors.  This can be further modified by additional regularization terms to control the ``springy-ness'' of the string.  These heuristics could be chosen to try to more faithfully sample the geodesic between two models.

  In practice, for a given model on the string, $\theta_i$, these two regularizations augment the standard loss by: $\tilde{F}(\theta) = F(\theta)+\zeta(\|\theta_{i-1} - \theta_i\|+\|\theta_{i+1} - \theta_i\|) + \kappa \|\frac{(\theta_{i-1} - \theta_{i+1})/2}{\|(\theta_{i-1} - \theta_{i+1})/2\|} \cdot \frac{(\theta_i - (\theta_{i-1} - \theta_{i+1})/2)}{\| (\theta_i - (\theta_{i-1} - \theta_{i+1})/2)\|}\|$.  The $\zeta$ regularization term controls the ``springy-ness'' of the weightstring, and the $\kappa$ regularization term controls how far off the hyperplane a new model can deviate.

  Because adapting DSS to use this constraint is straightforward, here we will describe an alternative ``breadth-first'' approach wherein models are trained in parallel until convergence.  This alternative approach has the advantage that it will indicate a disconnection between two models ``sooner'' in training.  The precise geometry of the loss surface will dictate which approach to use in practice.

  Given two random models $\sigma_i$ and $\sigma_j$ where $|\sigma_i - \sigma_j| < L_0$, we aim to follow the evolution of the family of models connecting $\sigma_i$ to $\sigma_j$.  Intuitively, almost every continuous path in the space of random models connecting $\sigma_i$ to $\sigma_j$ has, on average, the same (high) loss.  For simplicity, we choose to initialize the string to the linear segment interpolating between these two models.  If this entire segment is evolved via gradient descent, the segment will either evolve into a string which is entirely contained in a basin of the loss surface, or some number of points will become fixed at a higher loss.  These fixed points are difficult to detect directly, but will be indirectly detected by the persistence of a large interpolated loss between two adjacent models on the string.

  The algorithm proceeds as follows:

  (0.) Initialize model string to have two models, $\sigma_i$ and $\sigma_j$.

  1. Begin training all models to the desired loss, keeping the instantaneous loss, $L_0(t)$, of all models being trained approximately constant.

  2. If the pairwise interpolated loss between $\sigma_n$ and $\sigma_{n+1}$ exceeds $L_0(t)$, insert a new model at the maximum of the interpolated loss (or halfway) between these two models.

  3. Repeat steps (1) and (2) until all models (and interpolated errors) are below a threshold loss $L_0(t_{\rm{final}}):=L_0$, or until a chosen failure condition (see \ref{sec:Fail}).


\chapter{Interior Point Methods for Maximum Flow}
%%%% ADD MACROS HERE
% feel free to add more macros here

\newcommand{\sdiv}{\tilde{D}}
\newcommand\cvec[1]{\overrightarrow{\left(#1\right)}}

\section*{Background and Notation}

In this chapter, we'll learn about interior point methods for solving
maximum flow, which is a rich and active area of research \cite{DS08,M13,LS20a,LS20b}.

We're going to frequently need to refer to vectors
arising from elementwise operations combining other vectors.

To that end, given two vector $\aa \in \R^m$, and $\bb \in \R^m$, we
will use $\cvec{\aa(i) \bb(i)}$ to denote the vector $\zz$ with
$\zz(i) = \aa(i) \bb(i)$ and so on.

Throughout this chapter, when we are working in the context of some
given graph $G$ with vertices $V$ and edges $E$, we will let $m =
\abs{E}$ and $n = \abs{V}$.

The plots in this chapter were made using Mathematica, which is
available to ETH students for download through the ETH IT Shop.

\section{An Interior Point Method}
\paragraph{The Maximum Flow problem in undirected graphs.}
 \begin{align}
   \label{eq:maxflow}
\max_{\ff \in \R^E}  & \quad F    \\
   \textrm{s.t. }  \BB\ff= F \bb_{st}
     \tag*{``The Undirected Maximum Flow Problem''}
   \\
   \nonumber
   -\cc \leq \ff \leq \cc
\end{align}
We use $\val(\ff)$ to denote $F$ when $B\ff = F\bb_{st}$.

As we develop algorithms for this problem, we will assume that we know
the maximum flow value $F^*$.
Let $\ff^*$ denote some maximum flow, i.e. a flow with $-\cc \leq \ff
\leq \cc$ can $\val(\ff^*) = F^*$.

In general, an a lower
bound $F \leq F^*$ will allow us to find a flow with value $F$, and
because of this, we can use a binary search to approximate $F^*$.

\subsection{A barrier function and an algorithmic framework}
\[
V(\ff) = \sum_e -\log(\cc(e)-\ff(e))  -\log(\cc(e)+\ff(e))
\]

We assume the optimal value of Program~\eqref{eq:maxflow} is $F^*$.
Then for a given $0 \leq \alpha < 1$ we define a program
\begin{align}
   \label{eq:barrierflow}
\min_{\ff \in \R^E} & \quad  V(\ff)\\
\textrm{s.t. }  \BB\ff= \alpha F^* \bb_{st}
  \tag*{``The Barrier Problem''}
\end{align}
This problem makes sense for any $0 \leq \alpha < 1$.
When $\alpha = 0$, we are not routing any flow yet. This will be our
starting point.
For any $0 \leq \alpha < 1$, the scaled-down maximum
flow $\alpha\ff^*$ strictly satisfies the capacities $ -\cc < \alpha\ff^* < \cc$, and
$\BB\alpha\ff^*= \alpha F^* \bb_{st}$.
Hence $\alpha\ff^*$ is a feasible flow for this value of $\alpha$ and
hence $V(\alpha\ff^*) < \infty$ and so the optimal flow for the
Barrier Problem at this $\alpha$ must also have objective value
strictly below $\infty$, and hence in
turn strictly satisfy the capacity constraints.
Thus, if we can find the optimal flow for Program
\eqref{eq:barrierflow} for $\alpha = 1-\epsilon$, we will have a
feasible flow with Program~\eqref{eq:maxflow}, the Undirected Maximum Flow Problem, routing
$(1-\epsilon)F^*$.
This is how we will develop an algorithm for computing the maximum flow.

Program~\eqref{eq:barrierflow}  has the Lagrangian
\[
\calL(\ff,\xx) = V(\ff) + \xx^{\trp}( \alpha F^* \bb_{st}  - \BB\ff   )
\]

And we have optimality when
\begin{align}
  \label{eq:barrierfeasibility}
  \BB\ff= \alpha  F^* \bb_{st}
  \text{ and }
  -\cc \leq \ff \leq \cc
  \\
  \tag*{``Barrier feasibility''}
\end{align}

and $\grad_{\ff} \calL(\ff,\xx) = \veczero$, i.e.
\begin{align}
  \label{eq:barrierlagrangegrad}
  \grad V(\ff) = \BB^{\trp} \xx
  \\
  \tag*{"Barrier Lagrangian gradient optimality"}
\end{align}

Let $\ff^*_{\alpha}$ denote the optimal solution to
Problem~\ref{eq:barrierflow} for a given $0 \leq \alpha < 1$,
and let $\xx^*_{\alpha}$ be optimal dual voltages such that $\grad V(\ff^*_{\alpha}) = \BB^{\trp} \xx^*_{\alpha}$.

It turns out that, if we have a solution $\ff^*_{\alpha}$ to this problem for some
$\alpha < 1$, then we can find a solution $\ff_{\alpha+\alpha'}$ for
some $\alpha' < 1-\alpha$.
And, we can compute $\ff_{\alpha+\alpha'}$ using a small number of Newton
steps, each of which will only require a Laplacian linear equation
solve, and hence is computable in $\Otil(m)$ time.
Concretely, for any $0 \leq \alpha < 1$,
given the optimal flow at this $\alpha$, we will be able to compute
the optimal flow at $\alpha_{\text{new}} = \alpha + (1-\alpha)
\frac{1}{150\sqrt{m}}$.
This means that after $T = 150\sqrt{m}\log(1/\epsilon)$ updates, we have a solution
for $\alpha \geq 1-\epsilon$.

We can state the update problem as

\begin{align}
 \label{eq:updateflow}
  \min_{\ddelta \in \R^E} & \quad
    V(\ddelta+\ff)
  \\
\textrm{s.t. }  \BB\ddelta = \alpha' F^* \bb_{st}
  \tag*{``The Update Problem''}
\end{align}

\subsection{Updates using Divergence}

It turns out that for the purposes of analysis, it will be useful to
ensure that our ``Update Problem'' uses an objective function that is
minimized at $\ddelta = \veczero$.

This leads to a variant of the Update Problem, which we call the
``Divergence Update Problem''.
We obtain our new problem by switching from
$ V(\ddelta+\ff)$ as our objective to  $V(\ddelta+\ff)
  -
  (V(\ff)
  +
  \ip{\grad V(\ff), \ddelta})$ as our objective, and this is called
  the \emph{divergece} of $V$ w.r.t. $\ddelta$ \emph{based} at $\ff$.

\begin{align}
 \label{eq:divflow}
  \min_{\ddelta \in \R^E} & \quad
    V(\ddelta+\ff)
  -
  (V(\ff)
  +
  \ip{\grad V(\ff), \ddelta})
  \\
\textrm{s.t. }  \BB\ddelta = \alpha' F^* \bb_{st}
  \tag*{``The Divergence Update Problem''}
\end{align}

Now, for any flow $\ddelta$ such that $\BB\ddelta = \alpha' F^*
\bb_{st}$, using the Lagrangian gradient condition
\eqref{eq:barrierlagrangegrad},
we have
$\ip{\grad V(\ff^*_{\alpha}), \ddelta}) = \ip{\xx^*_{\alpha}, \alpha' F^*
  \bb_{st}})$.
Hence, for such $\ddelta$, we have
\[
    V(\ddelta+\ff^*_{\alpha})
  -
\left(
  V(\ff^*_{\alpha})
  +
  \ip{\grad V(\ff^*_{\alpha}), \ddelta}
\right)
=
    V(\ddelta+\ff^*_{\alpha})
  -
\left(
 V(\ff^*_{\alpha})
  +
\ip{\xx^*_{\alpha}, \alpha' F^*\bb_{st}}
\right)
\]
We conclude that the objectives of the Update
Problem~\eqref{eq:updateflow} and the Divergence Update
Problem~\eqref{eq:divflow} have the same minimizer, which we denote
$\ddelta^*_{\alpha'}$, although, to be precise, it is also a function of $\alpha$.

% This problem has Lagrangian
% \[
% \calM(\ddelta,\zz) = V(\ff) + \zz^{\trp}((\alpha+\alpha') F
% \bb_{st}-  \BB(\ff+\ddelta) )
% \]

% And we have optimality when
% \begin{align}
%   \label{eq:divfeasibility}
%   \BB(\ff+\ddelta)= (\alpha+\alpha') F^* \bb_{st}
%   \\
%     \tag*{``Update feasibility''}
% \text{and}
% -\cc \leq \ff + \ddelta\leq \cc
% \end{align}
% % \begin{align}
% %   \label{eq:divlagrangegrad}
% %   \grad V(\ff+\ddelta) - \grad V(\ff) = \BB^{\trp} \zz
% %   \tag*{Divergence Langrange Gradient.}
% % \end{align}
% % $\BB(\ff+\ddelta)= (1-\alpha+\alpha') F^* \bb_{st}$
% % and
% % $ -\cc \leq \ff + \ddelta\leq \cc$,
% and $\grad_{\ddelta} \calM(\ddelta,\zz) = \veczero$, i.e.
% \begin{align}
%   \label{eq:divlagrangegrad}
% \grad V(\ff+\ddelta) - \grad V(\ff) = \BB^{\trp} \zz
% \end{align}

% Note that if we simultaneously have \eqref{eq:barrierfeasibility}, the
% ``barrier feasibility''  condition, and \eqref{eq:divfeasibility}, the``update feasibility'' condition,
% satisfied along with Equations~\eqref{eq:barrierlagrangegrad}
% and~\eqref{eq:divlagrangegrad}, then
% $\ff + \ddelta$ satisfies the ``barrier feasibility'' with $\alpha$
% replaced by $\alpha + \alpha'$ and we have
% \[
% \grad V(\ff+\ddelta) = \BB^{\trp} (\xx+\zz).
% \]

Thus $\ff^*_{\alpha}+\ddelta^*_{\alpha'}$ is optimal for the
optimization problem
  \begin{equation}
   \label{eq:barrierflowupdated}
\begin{aligned}
\min_{\ff \in \R^E} & \quad V(\ff)\\
\textrm{s.t. }  \BB\ff= (\alpha+\alpha') F^* \bb_{st}
\end{aligned}
\end{equation}

\begin{lemma}
  \label{lem:updateoptimality}
  Suppose $\ff^*_{\alpha}$ is the minimizer of Problem~\eqref{eq:barrierflow}
  (the Barrier Problem with parameter $\alpha$) and
  $\ddelta^*_{\alpha'}$ is the minimizer of Problem~\eqref{eq:divflow} (the
  Update Problem with parameters $\ff^*_{\alpha}$ and $\alpha'$),
  then $\ff^*_{\alpha} + \ddelta^*_{\alpha'}$ is optimal for Problem~\eqref{eq:barrierflow}
  with parameter $\alpha+\alpha'$ (i.e. a new instance of the Barrier
  problem).
\end{lemma}

% \begin{remark}
%   \[
%     \ip{\grad V(\ff), \ddelta}) = \ip{\BB^{\trp}\xx, \ddelta} =
%     \ip{\xx, \alpha' F^* \bb_{st}}
%   \]
%   and why it's still useful, despite
%   being a constant.
% \end{remark}

\begin{algorithm}[H]
  \SetAlgoLined
  $\ff \leftarrow \veczero$\;
  $\alpha \leftarrow 0$\;
  \While{$\alpha < 1 - \epsilon$}{
    $a' \leftarrow  \frac{1-\alpha}{20\sqrt{m}}$\;
    Compute $\ddelta$, the minimizer of Problem~\eqref{eq:divflow}\;
    Let $\ff \leftarrow \ff + \ddelta$ and
    $\alpha \leftarrow \alpha + \alpha'$\;
  }
  \Return{\ff}
  \caption{\textsc{Interior Point Method}}
  \label{alg:ipm}
\end{algorithm}

\begin{pseudotheorem}
  \label{thm:updatealgo}
  Let $\ff$ be the minimizer of Problem~\eqref{eq:barrierflow}.
  Then, when  $a' \leq \frac{1-\alpha}{20\sqrt{m}}$, the minimizer
  $\ddelta$ of Problem~\eqref{eq:barrierflowupdated} can be computed
  in $\Otil(m)$ time.
\end{pseudotheorem}

The key insight in this type of interior point method is that when the
update $\alpha'$ is small enough,
\begin{theorem}\label{thm:maxflowipm}
  Algorithm~\ref{alg:ipm} returns a flow $\ff$ that is feasible for
  Problem~\eqref{eq:maxflow} in time $\Otil(m^{1.5}\log(1/\epsilon))$.
\end{theorem}

\begin{proof}[Proof Sketch]
  First note that for $\alpha = 0$, the minimizer of
  Problem~\eqref{eq:barrierflow} is $\ff = \veczero$.
  The proof now essentially follows by
  Lemma~\eqref{lem:updateoptimality}, and
  Pseudotheorem~\ref{thm:updatealgo}.
  Note that $1-\alpha$ shrinks by a factor $(1-\frac{1}{20\sqrt{m}})$
    in each iteration of the while-loop, and so after
    $20\sqrt{m}\log(1/\epsilon)$ iterations, we have $1-\alpha \leq
    \epsilon$, at which point the loop terminates.
   To turn this into a formal proof, we need to take care of the fact
   the proper theorem corresponding to
   Pseudotheorem~\ref{thm:updatealgo} only gives a highly accurate
   but not exact solution $\delta$ to the ``Update Problem''.
   But it's possible to show that this is good enough (even though
   both $\ff$ and $\ddelta$ end up not being exactly optimal in each iteration).
\end{proof}

\begin{remark}
  For the maximum flow problem, when capacities are integral and
  polynomially bounded, if we choose $\epsilon = m^{-c}$ for some
  large enough constant $c$, given a feasible flow with $\val(\ff) =
  1-\epsilon$, is it possible to compute an exact maximum flow in
  nearly linear time.
  Thus Theorem~\ref{thm:maxflowipm} can also be used to compute an
  exact maximum flow in $\Otil(m)$ time, but we omit the proof.
  The idea is to first round to an almost optimal, feasible integral flow (which
  requires a non-trivial combinatorial algorithm), and then to recover
  the exact flow using Ford-Fulkerson.
  See \cite{M13} for details.
\end{remark}

\begin{remark}
  It is possible to reduce an instance of directed maximum flow to an
  instance of undirected maximum flow in nearly-linear time, in such a
  way that if we can \emph{exactly} solve the undirected instance,
  then in nearly-linear time we can recover an exact solution to the
  directed maximum flow problem.
  Thus Theorem~\eqref{thm:maxflowipm} can also be used to solve
  directed maximum flow.
  We will ask you to develop this reduction in Graded Homework 2.
\end{remark}

\begin{remark}
  For sparse graphs with $m = \Otil(n)$ and large capacities, this
  running time is the best known, and improving it is major open problem.
\end{remark}

\subsection{Understanding the Divergence Objective}
% \begin{align*}
%   D( \ddelta+\ff \mid \ff )
% \end{align*}
% \[
% D(x) = -\log(1-x) - x
% \]
Note that if $V(x) = -\log(1-x)$, then $D(x) = V(x) - (V(0) + V'(0) x)$.

% \begin{figure}[H]
%   \centering
%   % \includegraphics[width=0.5\linewidth]{fig/logbarrier.png}
%   \begin{minipage}{0.4\textwidth}
%     \centering
%     \includegraphics[width=0.9\textwidth]{fig/logbarrier.png} % first figure itself
%     \caption{Plot showing ${V(x) = -\log(1-x)}$ and then linear
%       approximation ${V(0) + V'(0) x}$.}
%   \end{minipage}\hfill
%   \begin{minipage}{0.4\textwidth}
%     \centering
%     \includegraphics[width=0.9\textwidth]{fig/logdivergence.png}% second figure itself
%     \caption{Plot showing ${D(x) = V(x) - (V(0) + V'(0) x)}$.}
%   \end{minipage}
% \end{figure}

\begin{figure}[H]
  \centering
    \includegraphics[width=0.6\textwidth]{fig/logbarrier.png} % first figure itself
    \caption{Plot showing ${V(x) = -\log(1-x)}$ and then linear
      approximation ${V(0) + V'(0) x}$.}
\end{figure}

\begin{figure}[H]
  \centering
    \includegraphics[width=0.6\textwidth]{fig/logdivergence.png}% second figure itself
    \caption{Plot showing ${D(x) = V(x) - (V(0) + V'(0) x)}$.}
\end{figure}

We let
\[
  \cc_+(e) = \cc(e) - \ff(e) \text{ and } \cc_-(e) = \cc(e) + \ff(e)
\]

So then
\begin{align*}
  D_V( \ddelta )
  &=
  V(\ddelta+\ff)
  -
  (V(\ff)
  +
  \ip{\grad V(\ff), \ddelta})
  \\
  &=
  \sum_e
  -\log
  \left(\frac{\cc(e)-(\ddelta(e)+\ff(e))}
  {\cc(e)-\ff(e)}
    \right)
   -
\frac{\ddelta(e)}
  {\cc(e)-\ff(e)}
  \\
  &\quad\quad\,\,\,\,
  -\log
\left(
  \frac{\cc(e)+(\ddelta(e)+\ff(e))}
  {\cc(e)+\ff(e)}
  \right)
   +
\frac{\ddelta(e)}
    {\cc(e)+\ff(e)}
  \\
  &=
    \sum_e
    D\left(\frac{\ddelta(e)}
    {\cc(e)-\ff(e)}
    \right)
    +
     D\left(-\frac{\ddelta(e)}
    {\cc(e)+\ff(e)}
    \right)
   \\
  &=
    \sum_e
    D\left(\frac{\ddelta(e)}{\cc_+(e)}\right)
    +
    D\left(-\frac{\ddelta(e)}{\cc_-(e)}\right)
\end{align*}

Note that we can express Problem~\eqref{eq:divflow} as
% \begin{equation}
%    \label{eq:divflow2}
% \begin{aligned}
%   \min
%   _{\ddelta \in \R^E} & \quad
%      D_V( \ddelta )
%   \\
% \textrm{s.t. }  \BB\ddelta = \alpha' F^* \bb_{st}
% \end{aligned}
% \end{equation}
\begin{align}
   \label{eq:divflow2}
  \min_{\ddelta \in \R^E} & \quad
      D_V( \ddelta )
  \\
  \textrm{s.t. }  \BB\ddelta = \alpha' F^* \bb_{st}
\tag*{The Update Problem, restated}
\end{align}

Note that  $D_V( \ddelta )$ is strictly convex of over the
feasible set, so the argmin is unique.

\subsection{Quadratically Smoothing Divergence and Local Agreement}

\[
  \sdiv_{\epsilon}(x) =
  \begin{cases}
    -\log(1-x) - x & \text{ if } \abs{x} \leq \epsilon \\
    D(\epsilon) + D'(\epsilon) (x - \epsilon)
    +\frac{D''(\epsilon)}{2} (x - \epsilon)^2
    & \text{ if }  x \geq \epsilon \\
    D(-\epsilon) + D'(-\epsilon) (x + \epsilon)
    +\frac{D''(-\epsilon)}{2} (x +\epsilon)^2
    & \text{ if }  x \leq -\epsilon \\
  \end{cases}
\]

For brevity, we define
\[
  \sdiv(x) =\sdiv_{0.1}(x)
\]
\begin{lemma}
  \label{lem:sdivderivs}
  \noindent
  \begin{enumerate}
  \item $1/2 \leq \sdiv''(\xx) \leq 2$.
  \item For $x \geq 0$, we have $x/2 \leq \sdiv'(\xx) \leq 2x$
and $-2x \leq \sdiv'(-\xx) \leq -x/2$.
\item $x^2/4 \leq \sdiv(\xx) \leq x^2$.
  \end{enumerate}
\end{lemma}

What's happening here? We glue together $D(x)$ for small $x$ with its
quadratic approximation for $\abs{x} > \epsilon$.
For $x > 0$, we ``glue in'' a Taylor series expansion based at $x =
\epsilon$.

\begin{figure}[H]
  \centering
    \includegraphics[width=\textwidth]{fig/quad-div-apx.png} % first figure itself
    \caption{Plot showing ${D(x) = -\log(1-x)}$ and the quadratic
      approximation based at $x = 0.1$.}
\end{figure}

We also define
  \begin{align*}
  \sdiv_V( \ddelta )
 &=
    \sum_e
     \sdiv\left(\frac{\ddelta(e)}{\cc_+(e)}\right)
    +
     \sdiv\left(-\frac{\ddelta(e)}{\cc_-(e)}\right)
  \end{align*}

We can now introduce the smoothed optimization problem
\begin{align}
   \label{eq:sdivflow}
  \min_{\ddelta \in \R^E} & \quad
     \sdiv_V( \ddelta )
  \\
  \textrm{s.t. }  \BB\ddelta = \alpha' F^* \bb_{st}
\tag*{``The Smoothed Update Problem''}
\end{align}
Note that  $\sdiv_V( \ddelta )$ is strictly convex of over the
feasible set, so the argmin is unique.

\begin{pseudoclaim}
  We can compute the argmin $\ddelta^*$ of
  Problem~\eqref{eq:sdivflow}, the Smoothed Update Problem, using the Newton-Steps for
  $K$-stable Hessian convex functions that we saw in the previous chapter, in
  $\Otil(m)$ time.
\end{pseudoclaim}
\begin{proof}[Sketch of proof]
  Problem~\eqref{eq:sdivflow} fits the class of problems for which we
  showed in the previous chapter
  that (appropriately scaled) Newton steps converge.
  This is true because the Hessian is always a $2$-spectral
  approximation of the Hessian at $\sdiv_V( \ddelta^* )$, as can be
  shown from Lemma~\ref{lem:sdivderivs}.
  Because the Hessian of $\sdiv_V( \ddelta )$ is diagonal, and the
  constraints are flow constraints, each
  Newton step boils down to solving a Laplacian linear system, which can
  be done to high accuracy $\Otil(m)$ time.
\end{proof}

\begin{remark}
  There are three things we need to modify to turn the pseudoclaim
  into a true claim, addressing the errors arising from both Laplacian
  solvers and Newton steps:
  \begin{enumerate}
  \item We need to rephrase the claim to so that we only claim
    $\ddelta^*$ has been computed to high accuracy, rather than exactly.
  \item We need to show that we can construct an initial guess to
    start off Newton's method $\ddelta_0$ for which the value
    $\sdiv_V( \ddelta_0 )$ is not too large. (This is easy).
  \item We need show that Newton steps converge despite using a
    Laplacian solver that doesn't give exact solutions, only high
    accuracy solutions. (Takes a bit of work, but is ultimately not
    too difficult).
  \end{enumerate}
Importantly, to ensure our overall interior point method still works,
  we also need to show that it converges,
even if we're using approximate solutions
everywhere. This also takes some work to show, again is not too difficult.
\end{remark}

\paragraph{Local Agreement Implies Same Optimum.}
\begin{lemma}
  \label{lem:argminsfromlocalagreement}
  Suppose $S \subseteq \R^n$ is a convex set, and let $f, g: S \to R$ be convex
  functions.
  Let $\xx^* = \argmin_{\xx \in S} f(\xx)$.
  Suppose $f,g$ agree on a neighborhood of $\xx^*$ in $S$ (i.e. an
  open set containing $\xx^*$).
  Then $\xx^* = \argmin_{\xx \in S} f(\xx)$.
\end{lemma}
\begin{proof}[Proof Sketch]
  We sketch the proof in the case when both $f,g$ are differentiable: Observe
  that $\veczero = \grad f(\xx^*) = \grad g(\xx^*)$, and hence $\xx^*$
  is also minimized at $\xx^*$.
\end{proof}

We define
\begin{equation}
  \label{eq:symrescap}
  \cchat(e) = \min(\cc_+(e) , \cc_-(e) )
\end{equation}
% and for a positive vector $\cc > 0$, we define
% \[
% \norm{\yy}_{\cc,\infty} = \norm{}
%   \]

\begin{lemma}
  Suppose $\ddelta^*$ is the argmin of
  Problem~\eqref{eq:sdivflow}, the Smoothed Update Problem, and
  $\norm{\cvec{\ddelta^*(e)/\cchat(e)}}_{\infty} < 0.1$.
  Then $\ddelta^*$ is the argmin of Problem~\eqref{eq:divflow2}.
\end{lemma}
\begin{proof}
  We observe that if   $\norm{\cvec{\ddelta^*(e)/\cchat(e)}}_{\infty}
  < 0.1$, then $\sdiv_V( \ddelta^*) = D_V( \ddelta^*)$, and,
  for all $\ttau \in \R^m$ with norm
  \[
    \norm{\cvec{\ttau(e)/\cchat(e)}}_{\infty} < 0.1 -
    \norm{\cvec{\ddelta^*(e)/\cchat(e)}}_{\infty}
  \]
  we have that
  $\sdiv_V( \ddelta^*+\ttau) = D_V( \ddelta^*+\ttau
  )$.
  Thus $\sdiv_V$ and $D_V$ agree on a neighborhood around
  $\ddelta^*$ and hence by
  Lemma~\ref{lem:argminsfromlocalagreement}, we have that
  $\ddelta^*$ is the argmin of Problem~\eqref{eq:divflow2}.
\end{proof}

\subsection{Step size for divergence update}

\begin{definition}[\emph{$s$-$t$ well-conditioned} graph]
   An undirected, capacitated multi-graph $G = (V,E,\cc)$ with source $s$
   and sink $t$ is
   \emph{$s$-$t$ well-conditioned} if,
   letting $U$ denote the maximum edge capacity $U =
   \norm{\cc}_{\infty}$,
   we have at least $\frac{2}{3} m$ multi-edges of capacity $U$ going directly
   from $s$ to $t$.
 \end{definition}

 \begin{remark}
   It is straightforward to make a graph $s$-$t$ well-conditioned.
   We just add $2m$ new edges of capacity $U$ directly between $s$ and
   $t$.
   Given an exact maximum flow in the new graph, it is trivial to get
   one in the original graph: Just remove the flow on the new edges.
 \end{remark}

 \begin{definition}
   Given a \emph{directed} graph $G = (V,E,\cc)$, the
   \emph{symmetrization}
   of $G$ is the undirected $\Ghat = (V,\Ehat,\cchat)$ is the undirected graph given by
\[
\setof{a,b} \in \Ehat \text{ if } (a,b) \in E \text{ AND } (b,a) \in E
\]
and
\[
  \cchat(\setof{a,b}) = \min(\cc(a,b),\cc(b,a))
  .
\]
\end{definition}
Note that when  $\Ghat_{\ff}$ is the symmetrization of the residual
   graph $G_{\ff}$ (which we defined in Chapter~\ref{cha:maxflow1}), then $\cchat$ matches
   exactly the definition of $\cchat$ in Equation~\eqref{eq:symrescap}.
\begin{lemma}
\label{lem:symres}
  Let $G$ be an undirected, capacitated multi-graph $G = (V,E,\cc)$
  which is $s$-$t$ well-conditioned.
   Let $\ff$ be the minimizer of Program~\eqref{eq:barrierflow}.
   Let $\Ghat_{\ff}$ be the \emph{symmetrization} of the residual
   graph $G_{\ff}$ (in the sense of Lecture~10).
   Then there exists a flow $\ddeltahat$ which satisfies $\BB \ddeltahat = \frac{1-\alpha}{5} F^*
   \bb_{st} $ and is feasible in
   $\Ghat_{\ff}$. Note that we can also state the feasibility in
   $\Ghat_{\ff}$ as
   \[
\norm{\cvec{\ddeltahat(e)/\cchat(e)}}_{\infty} \leq 1
     \]
\end{lemma}
\begin{proof}
  We recall since $\ff$ is the minimizer of
  Program~\eqref{eq:barrierflow}, there exists dual-optimal voltages
  $\xx$ such that
  \[
    \BB^{\trp} \xx = \grad V(\ff) =
    \cvec{\frac{1}
    {\cc(e)-\ff(e)}
    -
    \frac{1}
    {\cc(e)+\ff(e)}}
  \]
From Lecture~10, we know that there is flow $\ddeltabar$ that is feasible with
respect to the residual graph capacities of the graph $G_{\ff}$ such
that $\BB \ddeltabar = (1-\alpha)F^* \bb_{st}$.
Note when treating $\ddeltabar$ as an undirected
flow, feasibility in the residual graph means that $\ddeltabar(e) < \cc(e)-\ff(e)$
and $-\ddeltabar(e) < \cc(e)+\ff(e)$.
Thus,
\[
  (1-\alpha)F^* \bb_{st}^{\trp}
  \xx
  =
  \ddeltabar
  \BB^{\trp} \xx
  =
  \sum_e
  \frac{\ddeltabar}
    {\cc(e)-\ff(e)}
    -
    \frac{\ddeltabar}
    {\cc(e)+\ff(e)}
    \leq
    m
  \]
  Now, because the graph is $s$-$t$ well-conditioned,
  there are at $\frac{2}{3} m $ edges directly from $s$ to $t$ with capacity $U$
  and each of these $e$
 satisfy by the Lagrangian gradient optimality condition~\eqref{eq:barrierlagrangegrad}
 \[
   \bb_{st}^{\trp}
  \xx
   =
  \frac{1}
    {U-\ff(e)}
    -
    \frac{1}
    {U+\ff(e)}
  \]
Note that $\frac{2}{3} mU \leq F^* \leq mU$ because the graph is $s$-$t$
well-conditioned.
To complete the analysis, we consider three cases.

\emph{Case 1: $\abs{\ff(e)} \leq \frac{2}{3} U$.}
Then the capacity on each of these edges in the symmetrized residual
graph $\Ghat_{\ff}$ is at least $U/3$.
As there are $\frac{2}{3} m $ of them,
we get that there is a feasible flow in $\Ghat_{\ff}$ of value at least
$\frac{2}{9} mU\geq \frac{1}{10} F^*$.

\emph{Case 2: $\ff(e) < -\frac{2}{3} U$.}
By the gradient condition, we have the same flow on all of the $\frac{2}{3} m$
$s$-$t$ edges, adding  up to at least $\frac{2}{3} mU$ going from $t$ to $s$.
This means that we must have at least $\frac{2}{3} mU$ flow going from
$s$ to $t$ via the remaining edges. But, their combined capacity is at
most $\frac{1}{3} mU$, so that cannot happen. Thus we can rule out
this case entirely.

\emph{Case 3: $\ff(e) > \frac{2}{3} U$.}
Then
\[
  \frac{m}{(1-\alpha)F^* }
  \geq
     \bb_{st}^{\trp}
     \xx
 \geq
  \frac{1}
    {U-\ff(e)}
    -
    \frac{1}
    {U+\ff(e)}
 \geq
   \frac{4/5}
    {U-\ff(e)}
  \]
So
\[
  U-\ff(e) \geq \frac{4}{5}  \frac{(1-\alpha)F^* }{m}
  \geq \frac{1}{2}
(1-\alpha) U
\]
In this case, the capacity on each of the $\frac{2}{3} m$
$s$-$t$ edges with capacity $U$ in $G$ will
have capacity $(1-\alpha) U/2$ in $\Ghat_{\ff}$.
This guarantees that there is feasible flow in $\Ghat_{\ff}$ of value
at least $\frac{1}{3} (1-\alpha) mU \geq \frac{1}{3} (1-\alpha) F^*$.
\end{proof}
\begin{lemma}
  Let $0 < \alpha' \leq \frac{1-\alpha}{150\sqrt{m}}$.
  Then the minimizer $\ddelta^*$ of Problem~\eqref{eq:sdivflow}
  satisfies $\norm{\cvec{\ddelta^*(e)/\cchat(e)}}_{\infty} < 0.1$.
\end{lemma}
\begin{proof}
  By Lemma~\ref{lem:symres}, there exists a flow $\ddeltahat$ which satisfies $\BB \ddeltahat = \frac{1-\alpha}{5}  F^*
   \bb_{st} $ and ${\norm{\cvec{\ddeltahat(e)/\cchat(e)}}_{\infty} \leq
   1}$.
 Hence for any
 $0 < \alpha' \leq \frac{1-\alpha}{150\sqrt{m}}$,
 the flow
 $\ddeltatil = \alpha' \frac{5}{1-\alpha}\ddeltahat$
 satisfies
$\BB \ddeltatil = \alpha' F^*\bb_{st}$
and
$\norm{\cvec{\ddeltatil(e)/\cchat(e)}}_{\infty}
\leq
\frac{1}{30\sqrt{m}} $.
   This means that
\begin{align*}
     \sdiv_V( \ddeltatil )
     &=
    \sum_e
     \sdiv\left(\frac{\ddeltatil(e)}{\cc_+(e)}\right)
    +
    \sdiv\left(-\frac{\ddeltatil(e)}{\cc_-(e)}\right)
  \\
  &\leq
    \sum_e
     4\left(\frac{\ddeltatil(e)}{\cc_+(e)}\right)^2
    +
    4\left(-\frac{\ddeltatil(e)}{\cc_-(e)}\right)^2
  \\
  &
  \leq
    \sum_e
    8\left(\frac{\ddeltatil(e)}{\cchat
        (e)}\right)^2
 \\
     &\leq 8/900 < 1/100
.
\end{align*}

This then means that the minimizer $\ddelta^*$ of
Problem~\eqref{eq:sdivflow} also satisfies
$\sdiv_V( \ddeltatil ) < 1/100$.

\begin{align*}
  \norm{\cvec{\ddelta^*/\cchat(e)}}_{\infty}^2
&\leq
      \sum_e
     \left(\frac{\ddelta^*(e)}{\cc_+(e)}\right)^2
    +
    \left(-\frac{\ddelta^*(e)}{\cc_-(e)}\right)^2
\\
&\leq
    \sum_e
     \sdiv\left(\frac{\ddelta^*(e)}{\cc_+(e)}\right)
    +
    \sdiv\left(-\frac{\ddelta^*(e)}{\cc_-(e)}\right)
\tag*{By Lemma~\ref{lem:sdivderivs}.}
\\
  &=
\sdiv_V( \ddeltatil )
<
1/100
.
\end{align*}
Hence $\norm{\cvec{\ddelta^*/\cchat(e)}}_{\infty} < 0.1$.
\end{proof}

% \todo{writing plan}
%  \begin{itemize}
%  \item well-cond def
%  \item symmetrized res graph
%  \item  state existence of a big, feasible step in sym-res graph (inf norm)
%    (asym -> sym)
%  \item use of that to say that smoothed divergence opt is within $\infty$
%    ball for
%  \item prove existence proof of the step
%  \end{itemize}

% \begin{definition}
% Given a feasible flow $\ff$ in $G = (V,E,\cc)$
% \end{definition}

% \begin{lemma}
%    Suppose the original graph is $G = (V,E,\cc)$ is \emph{$s$-$t$
%      well-conditioned},
%    and suppose $\ff$ is the
% \end{lemma}

% Suppose the original graph is $G = (V,E,\cc)$ is \emph{preconditioned}

% \subsection{old step size writing}

% We define a strict barrier feasbility condition:
% \begin{align}
%   \label{eq:strictbarrierfeasibility}
%   \BB\ff= \alpha F^* \bb_{st}
%   \text{ and }
%   -\cc < \ff < \cc
%   \\
%   \tag*{"Strict barrier feasibility"}
% \end{align}

% \begin{lemma}
%   Suppose $\ff$ is a flow  satisfying the strict barrier
%   feasibility conditions.
%   Note that
%   Consider
%   There exists a flow $\ddelta_{\infty}$ s.t.
% \end{lemma}

% oops

% \begin{itemize}
% \item the problem is that if we SUBSTANTIALLY use a forward edge
% \item then we still need to make sure it doesn't exceed the backward
%   edge capacity :(
% \end{itemize}

% \todo{you can leave comments like this}

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


\chapter{Example}

This example will cover a calculator that can parse mathematical expressions and print the result. The resulting code for this, can be found in the \textit{examples} folder.

\section{Define the classes}

The first step should be the specify, which nonterminal and terminal symbols are needed. In our case, we have the following symbols:

\begin{itemize}
    \item Nonterminal
    \begin{itemize}
        \item Plus
        \item Minus
        \item Division
        \item Multiplication
    \end{itemize}

    \item Terminal
    \begin{itemize}
        \item Number
    \end{itemize}
\end{itemize}

The nonterminal symbols need, of course, two child nodes. This could for example be a terminal but also nonterminal symbol. With these symbols, we can basically define a simple syntax tree.

\section{Implement the classes}

Now that we have defined the classes, we can implement them. As already mentioned in the last chapter, each class derives from an abstract class, that has the \textit{interpret} method.

\begin{verbatim}
package expressions;

public abstract class AbstractExpression {
    public abstract int interpret();

    @Override
    public abstract String toString();
}
\end{verbatim}

You can see that this class also overrides the \textit{toString} method. You'll see later why. The different expressions then derive the \textit{AbstractExpression} class and implement the \textit{interpret} method. This could look like this:

\subsection{Nonterminal Expressions}

\begin{verbatim}
package expressions;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class MultiplyExpression extends AbstractExpression {
    private AbstractExpression lhs;
    private AbstractExpression rhs;

    @Override
    public int interpret() {
        return lhs.interpret() * rhs.interpret();
    }

    @Override
    public String toString() {
        return String.format("(%s * %s)", lhs.toString(), rhs.toString());
    }
}
\end{verbatim}

I should probably mention, that I used \textit{Lombok} to automatically generate the getter and setters for me so I don't have to write that much code and keep it simple. The only difference between the different nonterminal symbols is, that they have a different \textit{interpret} method. For example the \textit{PlusExpression} will just use the \textit{+} instead of the \textit{*} operator. As you have seen in the UML diagram, the nonterminal expression forward interprets the terminal expressions that it stores internally.

You can also see that I implemented the overridden \textit{toString} method, which will later be used to print our custom parse tree. It just takes the left and right side of the mathematical expression and prints it. Just like you would write it on paper.

\subsection{Terminal Expressions}

This is basically the same as the nonterminal expression above, with the only difference, that it does not have child nodes. It has only a simple number, which cannot be replaced.

\begin{verbatim}
package expressions;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class NumberExpression extends AbstractExpression {
    private int number;

    public NumberExpression(String s) {
        this.number = Integer.parseInt(s);
    }

    @Override
    public int interpret() {
        return number;
    }

    @Override
    public String toString() {
        return String.format("%s", number);
    }
}
\end{verbatim}

\section{Define an input format}

I decided to use the \nameref{sec:reverse-polish-notation} as input format. This remove the need of parentheses, which makes parsing the input easier. The input format for our calculator can also be described in the \nameref{sec:backus-naur-form}, which will look like this:

\begin{verbatim}
expression  ::= plus | minus | variable | number
plus        ::= expression expression '+'
minus       ::= expression expression '-'
variable    ::= 'a' | 'b' | 'c' | ... | 'z'
digit       = '0' | '1' | ... | '9'
number      ::= digit | digit number
\end{verbatim}

This defines a language that contains \nameref{sec:reverse-polish-notation} like these:

\begin{verbatim}
a b +
a b c + -
a b + c a - -
\end{verbatim}

\section{Parse the input}

The input can be parsed, by first splitting the string and then finding the correct expression for the token. Using a stack makes parsing the \nameref{sec:reverse-polish-notation} input really easy.

\begin{verbatim}
private static AbstractExpression createAbstractSyntaxTree (String tokenString) {
    var tokenList = tokenString.split(" ");
    var stack = new Stack<AbstractExpression>();

    // Iterate though the token list.
    for (var token : tokenList) {
        if (isOperator(token)) {
            var rhs = stack.pop();
            var lhs = stack.pop();

            var operator = getOperator(token, lhs, rhs);

            stack.push(operator);
        } else {
            var number = new NumberExpression(token);
            stack.push(number);
        }
    }

    return stack.pop();
}
\end{verbatim}

\section{Result}

When you use all the functions, you can add the input string like \textbf{4 3 2 - 1 + *} in this example, generate the \nameref{sec:abstract-syntax-tree} and then either print it or call \textit{interpret}. The \textit{toString} method also forward calls \textit{toString}, thus, if we print it, the entire \nameref{sec:abstract-syntax-tree} will get printed to the console. The output will be \textit{(4 * ((3 - 2) + 1)) = 8} and \textit{((12 * 2) + (64 / 2)) = 56}

\begin{verbatim}
public static void main(String[] args) {
    //
    // Create expression tree from string
    //
    var tokenString = "4 3 2 - 1 + *";
    var result = createAbstractSyntaxTree(tokenString);
    System.out.println(String.format("%s = %s",
        result, result.interpret()));

    //
    // Create expression tree manually
    //
    var customExpression = new PlusExpression(
            new MultiplyExpression(new NumberExpression(12),
                new NumberExpression(2)),
            new DivideExpression(new NumberExpression(64),
                new NumberExpression(2))
    );
    System.out.println(String.format("%s = %s",
        customExpression, customExpression.interpret()));
}
\end{verbatim}


\documentclass[a4paper]{article}
\usepackage[letterpaper, margin=1in]{geometry} % page format
\usepackage{listings} % this package is for including code
\usepackage{graphicx} % this package is for including figures
\usepackage{amsmath}  % this package is for math and matrices
\usepackage{amsfonts} % this package is for math fonts
\usepackage{tikz} % for drawings
\usepackage{hyperref} % for urls

\title{Homework 1}
\author{Amy Pitts}
\date{2/5/19}

\begin{document}
\lstset{language=Python}

\maketitle

\section{Problem 1.2}
Consider the perceptron in two dimensions: $h(x)=sign(w^Tx)$
where $w=[w_0,w_1,w_2]^T$ and $x=[1,x_1,x_2]^t$.
Technically, $x$ has three coordinates, but we call this
perception two-dimensional because the first coordinates
is fixed at 1. \\
(a) Show that the regions on the plane where $h(x)=+1$
and $h(x)=-1$ are separated by a line. If we express
this line by the equation $x_2=ax_1+b$ what are
the slope $a$ and intercept $b$ in terms of $w_0,w_1,x_2$? \\
\indent \textbf{Solution:} If we have $h(x)=1$ and $h(x)=-1$ this
implies that for $h(x)=1$ we have $w^Tx>0$ and with $h(x)=-1$ we
have $w^Tx<0$ so the separation between the two is just the
x-axis or when $w^Tx=0$. \\
For the equation $x_2=ax_1+b$ we have that $a= -\frac{w_1}{w_2}$ and $b=-\frac{w_0}{w_2}$

(b) Draw a picture for the cases $w=[1,2,3]^T$ and $w=-[1,2,3]^T$. \\
In more than two dimensions, the $+1$ and $-1$ regions are
separated by a hyperplane, the generalization of a line. \\
\indent \textbf{Solution:} \\
\includegraphics[width=85mm]{positive.png}
\includegraphics[width=85mm]{negative.png} \\
\lstinputlisting[language=Python,frame=single]{plotting_practice.py}

\section{Problem 1.4}
In Exercise 1.4 we use an artificial data set to study the
perceptron learning algorithm. This problem leads you to explore the algorithm
further with data sets of different sizes and dimensions. \\
\indent For the following question I will be using and modifying
this code:
\lstinputlisting[language=Python,frame=single]{plotP.py}
\indent \textbf{(a):} Generate a linearly separable data set of
size 20 as indicated in Exercise 1.4. Plot the examples
$\{(x_n,y_n)\}$ as well as the target function $f$ on a plane. Be sure
to mark the examples from different classes differently,
and add labels to the axes of the plot. \\
\includegraphics[width=85mm]{a_1.png}
\includegraphics[width=85mm]{a_2.png} \\
The program took 5 iterations to properly split up the two groups. \\

\indent \textbf{(b):} Run the perceptron learning algorithm on
the data set above. Report the number of updates that the algorithm
takes before converging. Plot the examples $\{(x_n,y_n)\}$, and the
final hypothesis $g$ in the same figure. Comment on whether $f$ is
close to $g$. \\
\includegraphics[width=85mm]{b_1.png}
\includegraphics[width=85mm]{b_2.png} \\
The program took 2 iterations to properly split up the two groups.
We are unable to tell if $f$ is close to $g$ because we do not know
what $f$ is. \\

\indent \textbf{(c):} Repeat everything in (b) with another randomly
generated data set of size 20. Compare your results with (b). \\
\includegraphics[width=85mm]{c_1.png}
\includegraphics[width=85mm]{c_2.png} \\
The program took 1 iteration to properly split up the two groups.
We are unable to tell if $f$ is close to $g$ because we do not know
what $f$ is. \\

\indent \textbf{(d):} Repeat everything in (b) with another randomly
generated data set of size 100. Compare your results with (b). \\
\includegraphics[width=85mm]{d_1.png}
\includegraphics[width=85mm]{d_2.png} \\
The program took 5 iteration to properly split up the two groups.\\

\indent \textbf{(e):} Repeat everything in (b) with another randomly
generated data set of size 1,000. Compare your results with (b). \\
\includegraphics[width=85mm]{e_1.png}
\includegraphics[width=85mm]{e_2.png} \\
The program took 1 iteration to properly split up the two groups.
I thought it was going to take more iterations but the
data generator is making perfectly separated groups making
it super easy to separate. Also, after running the program
a couple of times to see what happens I found that I either get
stuck in a loop of never being able to find the perfect line
or the groups are easily separated and it take 1 to 2 iterations.
\\

\indent \textbf{(f):} Modify the algorithm such that it takes
$x_n \in \mathbb{R}^{10}$ instead of $\mathbb{R}^2$. Randomly generate
a linearly separable data set of size 1,000 with $x_n \in \mathbb{R}^{10}$
and feed the data set to the algorithm. How many updates does the
algorithm take to converge? \\
\includegraphics[width=85mm]{f.png} \\
The algorithm took one update to converge. \\

\indent \textbf{(g):} Repeat the algorithm on the same data set as (f)
for 100 experiments. In the iterations of each experiment, pick
$x(t)$ randomly instead of deterministically. Plot a histogram
for the number of updates that the algorithm take to converge. \\
\includegraphics[width=150mm]{g.png} \\
\lstinputlisting[language=Python,frame=single]{10_dims.py}

\indent \textbf{(h):} Summarize your conclusions with respect to
accuracy and running time as a function of $N$ and $d$. \\

I found that the generated data if broken up takes about 1 to
2 iterations to properly split the data. However,
when there were only two dimensions there was a more
likely chance that a data set was generated that was not
able to be split up resulting in an endless loop.
Running the 10-dimensional data set there was less of a
chance that the data would not be able to be split.
Also when $N$ was increased there was a higher chance
that a data set was produced that was not able to be split
by a line. I wonder what would happen if we used a
different data generator.

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS110-2015S: Introduction to Computing
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS110-2015S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \topDirectory {.}
\def \texDirectory {\topDirectory/src/main/tex}

\documentclass[12pt,letterpaper,twoside]{article}
\usepackage{\texDirectory/template/style/directives}
\usepackage{\texDirectory/template/style/assignment}
\input{\texDirectory/template/config}

\begin{document}

\doc{title}{Solution to Quiz 2(a)}
\doc{date-pub}{Mar 10, 2015 at 01:00 PM}
\doc{date-due}{Mar 10, 2015 at 11:00 PM}
\doc{points}{4}

\prepare{header}

\section*{Question 1}

Write a program \texttt{PrimeRelative.java} that asks for \texttt{N} from the user and prints out an \texttt{N}-by-\texttt{N} table such that there is an \texttt{*} in row \texttt{i} and column \texttt{j}, if the greatest common divisor of \texttt{i} and \texttt{j} is 1, and a space in that position if otherwise.

\subsection*{Solution}

\lstset{language=Java,tabsize=2}
\begin{lstlisting}
// we use scanner to ask for number N
import java.util.Scanner;
public class PrimeRelative {
	public static void main(String[] args)
	{
		int i; // 1st for loop (matrix row)
		int j; // 2nd for loop (matrix col)
		int k; // 3rd for loop (division checker)
		int numberN;
		boolean notPrimeRelative;
		System.out.print("Please insert the number N: ");
		Scanner input = new Scanner(System.in);
		numberN = input.nextInt();
		System.out.println("Your entered number is " + numberN + ".");
		input.close(); // remember to close the scanner object
		numberN++; // to compensate our sacrifice of first row and column
		for (i = 0 ; i < numberN ; i++) {
			for (j = 0 ; j < numberN ; j++) {
				// if first row, print number of columns
				if (i == 0) {
					System.out.printf("%-2d ",j);
				}
				// else if first column, print number of rows
				else if (j == 0) {
					System.out.printf("%-2d ",i);
				}
				// for matrix elements
				else {
					// we initially assume gcd(i,j)=1
					notPrimeRelative = false;
					// now we check if a number k can be found
					// that divides both i and j
					for (k = 2; k <= Math.min(i, j); k++) {
						// if it divides both i and j
						// gcd(i,j) != 1 for sure
						if (i % k == 0 && j % k == 0) {
							// so they are not prime relative
							notPrimeRelative = true;
						}
					}
					if (notPrimeRelative) {
						System.out.print("   ");
					}
					else {
						System.out.print("*  ");
					}
				}
			}
			// after each row, a feed line is inserted
			System.out.println();
		}
	}
}
\end{lstlisting}
\newpage

\section*{Question 2}

Write a program \texttt{MatrixDeterminant.java} that asks for elements of a three-by-three matrix and prints its determinant.

\subsection*{Solution}

\lstset{language=Java,tabsize=2}
\begin{lstlisting}
// import Scanner class to use Scanner
import java.util.Scanner;
public class MatrixDeterminant {
	public static void main(String[] args) {
		int i; // 1st loop (row)
		int j; // 2nd loop (col)
		int[][] matrix = new int[3][3]; // array of int for 3x3 matrix
		long determinant; // long is bigger than int
		Scanner input = new Scanner(System.in); // we create scanner object
		// ask user to initialize matrix
		for (i = 0; i < 3; i++) {
			for (j = 0; j < 3; j++) {
				System.out.printf("Insert element in row %d and col %d: ",i,j);
				matrix[i][j] = input.nextInt();
			}
		}
		// close Scanner object as we don't need it anymore
		input.close();
		// show initialized matrix to user
		System.out.println("Matrix is: ");
		for (i = 0; i < 3; i++) {
			for (j = 0; j < 3; j++) {
				System.out.printf("%5d ",matrix[i][j]);
			}
			System.out.println();
		}
		// show determinant
		System.out.print("Determinant for matrix is: ");
		determinant = matrix[0][0]*matrix[1][1]*matrix[2][2]
				+ matrix[1][0]*matrix[2][1]*matrix[1][2]
				+ matrix[0][1]*matrix[1][2]*matrix[2][0]
				- matrix[0][2]*matrix[1][1]*matrix[2][0]
				- matrix[0][0]*matrix[1][2]*matrix[2][1]
				- matrix[2][2]*matrix[1][0]*matrix[0][1];
		System.out.println(determinant);
	}
}
\end{lstlisting}

\end{document}


\documentclass[a4paper]{article}
\usepackage[margin=20mm]{geometry}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{slashbox}
\usepackage{booktabs}
\usepackage[table,x11names]{xcolor}
\usepackage{pgfplots}

\setlength{\intextsep}{1pt}

\begin{document}
    \pagenumbering{gobble}
    \section*{Percent point function (PPF) of the normal distribution}

\pgfmathdeclarefunction{gauss}{3}{%
  \pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}

\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[
  no markers,
  domain=0:6,
  samples=100,
  ymin=0,
  axis lines*=left,
  xlabel=$x$,
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=4cm,
  width=12cm,
  xtick=\empty,
  ytick=\empty,
  enlargelimits=false,
  clip=false,
  axis on top,
  grid = major,
  hide y axis
  ]

 \addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};

\pgfmathsetmacro\valueA{gauss(1,3,1)}
\pgfmathsetmacro\valueB{gauss(2,3,1)}
\draw [gray] (axis cs:1,0) -- (axis cs:1,\valueA)
    (axis cs:5,0) -- (axis cs:5,\valueA);
\draw [gray] (axis cs:2,0) -- (axis cs:2,\valueB)
    (axis cs:4,0) -- (axis cs:4,\valueB);
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);

\node[below] at (axis cs:1, 0)  {$\mu - 2\sigma$};
\node[below] at (axis cs:2, 0)  {$\mu - \sigma$};
\node[below] at (axis cs:3, 0)  {$\mu$};
\end{axis}
\end{tikzpicture}
\end{figure}

\begin{table}[!h]
    \centering
    \begin{tabular}{c|ccccc|ccccc}
    \toprule
    \backslashbox{$x$}{$\Delta x$}  & \textbf{0.00} & \textbf{0.01} & \textbf{0.02} & \textbf{0.03} & \textbf{0.04} & \textbf{0.05} & \textbf{0.06} & \textbf{0.07} & \textbf{0.08} & \textbf{0.09} \\\midrule
\textbf{0.0} & -inf & -2.3263 & -2.0537 & -1.8808 & -1.7507 & -1.6449 & -1.5548 & -1.4758 & -1.4051 & -1.3408\\
\textbf{0.1} & -1.2816 & -1.2265 & -1.1750 & -1.1264 & -1.0803 & -1.0364 & -0.9945 & -0.9542 & -0.9154 & -0.8779\\
\textbf{0.2} & -0.8416 & -0.8064 & -0.7722 & -0.7388 & -0.7063 & -0.6745 & -0.6433 & -0.6128 & -0.5828 & -0.5534\\
\textbf{0.3} & -0.5244 & -0.4959 & -0.4677 & -0.4399 & -0.4125 & -0.3853 & -0.3585 & -0.3319 & -0.3055 & -0.2793\\
\textbf{0.4} & -0.2533 & -0.2275 & -0.2019 & -0.1764 & -0.1510 & -0.1257 & -0.1004 & -0.0753 & -0.0502 & -0.0251\\
\textbf{0.5} & 0.0000 & 0.0251 & 0.0502 & 0.0753 & 0.1004 & 0.1257 & 0.1510 & 0.1764 & 0.2019 & 0.2275\\
\textbf{0.6} & 0.2533 & 0.2793 & 0.3055 & 0.3319 & 0.3585 & 0.3853 & 0.4125 & 0.4399 & 0.4677 & 0.4959\\
\textbf{0.7} & 0.5244 & 0.5534 & 0.5828 & 0.6128 & 0.6433 & 0.6745 & 0.7063 & 0.7388 & 0.7722 & 0.8064\\
\textbf{0.8} & 0.8416 & 0.8779 & 0.9154 & 0.9542 & 0.9945 & 1.0364 & 1.0803 & 1.1264 & 1.1750 & 1.2265\\
\textbf{0.9} & 1.2816 & 1.3408 & 1.4051 & 1.4758 & 1.5548 & 1.6449 & 1.7507 & 1.8808 & 2.0537 & 2.3263\\
        \bottomrule
        \end{tabular}
        \caption{Approximations of $\Phi_{0;1}^{-1}(x + \Delta x)$}
    \end{table}
    \begin{align*}
        \Phi_{0;1}(x) &= \frac{1}{\sqrt{2 \pi}} \int_{-\infty}^{x} e^{- t^2 / 2} \mathrm{d} t &
        \Phi_{0;1}(1.65) &\approx 0.9505\\
        \Phi_{\mu; \sigma^2}(x) &= \Phi_{0;1} \left (\frac{x-\mu}{\sigma} \right ) &
        \Phi_{0;1}(-x) &= 1 - \Phi_{0;1}(x)\\
        z_\alpha &= \Phi_{0;1}^{-1}(1-\alpha)
    \end{align*}
\end{document}


\section{Object Class Recognition}

Two main tasks: Classification and Detection

Classification: is there a car in this image ? A binary answer is enough\\
Detection: where is the car ? Need localization

\subsection{Bag of words approaches: Visual words}
\subsubsection{Local features}
Local (textured) patches (‘regions’) • Detection co-variant with translation, scale, and sometimes affine transformations
• Cover the same portion of the object in any view

Each patch / region has a descriptor, which is a point in some high-dimensional feature space (e.g., SIFT)

Close points in feature space have similar descriptors, which indicates similar image content.

\includegraphics[width=\columnwidth]{pictures/localfeature}

\subsubsection{Visual Words}
1) Extract some local features from a number of images

2) Map high-dimensional descriptors to words by quantizing the feature space (Quantize via clustering, let cluster centers be the prototype “words”)

3) Map high-dimensional descriptors to words by quantizing the feature space. Determine which word to assign to each new image region by finding the closest cluster center

Building Visual Vocabularies
Issues: • Sampling strategy: where to extract features? ->
For object classes dense sampling works best
• Clustering / quantization algorithm ->
Many options, often k-means works well enough
• What corpus provides features (universal vocabulary?) ->
Typically as close as possible to your application • Vocabulary size, number of words\\

\subsection{Bag of Words}
Summarize entire image based on its distribution (histogram) of word occurrences.
(Analogous to bag of words representation commonly used for documents.) Quantize feature space to make discrete set of visual words

Bag of words enable to describe the unordered feature set with a single vector of fixed dimensionality

Works pretty well for whole-image classification

Classifier Choises

\begin{itemize}
	\item Nearest Neighbor
	\item Neural Networks
	\item Support Vector Machines
	\item Boosting
\end{itemize}

Nearest Neighbor - Assign label of nearest training data point to each test data point. Works very fast when training data is huge.
Support Vector Machines - Maximize the margin between the positive and negative training examples

The bag of words removes spatial layout. This is both a strength and a weakness

\subsubsection{Discussion: Bag-of-Words}
Pros:
\begin{itemize}
	\item Flexible to geometry / deformations / viewpoint
	\item Compact summary of image content
	\item Provides vector representation for sets (bags to be precise)
	\item Empirically good recognition results in practice
\end{itemize}
Cons:
\begin{itemize}
	\item Basic model ignores geometry – can verify afterwards, or embed within feature descriptors
	\item Background and foreground mixed when bag covers whole image - Interest points or sampling: no guarantee to capture object-level parts
	\item Optimal vocabulary formation remains unclear

\end{itemize}

\subsection{Classification: convolutional neural networks}
Neural network with specialized connectivity structure.

Typical building blocks of CNNs for classification in vision

\begin{itemize}
	\item Feed forward
	\item Most layers (often)
	\begin{itemize}
		\item Convolve input (filters)
		\item Non-linearity (rectified linear)
		\item Pooling (local max)
	\end{itemize}
	\item Last few layers (often)
	\begin{itemize}
		\item fully connected (linear combinations)
		\item Non-linearity (rectified linear)
	\end{itemize}
	\item output: softmax distribution over classes
\end{itemize}
supervise, train convolutional filters by back-propagatin classification error

\subsection{Detection: Sliding-window approaches}
If object may be in a cluttered scene, slide a window around looking for it. - A brute force approach with many local decisions

\subsubsection{Gradient based Representations}
Summarize local distribution of gradients with histogram

\subsection{Boosting}
Intuition
Consider a 2D feature
space with positive and negative examples.
Each weak classifier splits the training examples with at least 50% accuracy.
Examples misclassified by a previous weak learner are given more emphasis at future rounds. Final classifier is combination of the weak classifiers

\subsection{Discussion: Sliding-Windows }
Pros
\begin{itemize}
	\item Simple detection protocol to implement
	\item Good feature choices critical
	\item Past successes for certain classes
	\item Good detectors available (Viola\&Jones, HOG, etc.)
\end{itemize}
Cons/Limitations
\begin{itemize}
	\item High computational complexity
	\begin{itemize}
		\item – For example: 250,000 locations x 30 orientations x 4 scales = 30,000,000 evaluations!
		\item This puts tight constraints on the classifiers we can use.
		\item If training binary detectors independently, this means cost increases linearly with number of classes.
	\end{itemize}
	\item With so many windows, false positive rate better be low
	\item Typically need fully supervised training data (= bounding-boxes)
	\item Some object do not fit a box well (diagonal bottle) Sensitive to partial occlusion (unless in training data)
\end{itemize}

\subsection{Detection: proposal-based approaches}
\subsubsection{Object proposals}
Every object has at least one of these properties
• Well-defined, closed boundary in space • Different appearance than its surroundings • Might be unique within the image (salient)

Objectness(w) = probability that w covers an object

Objectness cues
\begin{itemize}
	\item Density of salient pixels
	\item color contrast
	\item superpixel straddling
\end{itemize}
\subsubsection{R-CNN}
\includegraphics[width=\columnwidth]{pictures/R-CNN}
Pros
\begin{itemize}
	\item Accurate!
	\item Any deep architecture can immediately be plugged in
\end{itemize}
Cons
\begin{itemize}
	\item Ad hoc training objectives
	\begin{itemize}
		\item Fine-tuning network with softmax classifier (log loss)
		\item Train post-hoc linear SVMs (hinge loss)
		\item Train post-hoc bounding-box regressions (least squares)
	\end{itemize}
	\item Training is slow (84h), takes a lot of disk space
	\item Inference (detection) is slow
\end{itemize}
\subsubsection{Fast R-CNN}
Full Image first through ConvNet and afterwards Region selection
\subsection{Detection: a star model}
Recognition of object categories

\includegraphics[width=\columnwidth]{pictures/star_models}

\subsubsection{Implicit Shape Model}
Visual vocabulary is used to index votes for object position [a visual word = a “part”].
Objects are detected as consistent configurations of the observed parts (visual words).

\includegraphics[width=\columnwidth]{pictures/ISM}
\includegraphics[width=\columnwidth]{pictures/ism_spatial}

Pros:
\begin{itemize}
	\item Works well for many different object categories – Both rigid and articulated objects
	\item Flexible geometric model – Can recombine parts seen on different training examples
	\item Learning from relatively few (50-100) training examples
	\item Optimized for detection, good localization properties
\end{itemize}
Cons:
\begin{itemize}
	\item Needs supervised training data – Object bounding boxes for detection \& Segmentations for top-down segmentation
	\item Only weak geometric constraints – Result segmentations may contain superfluous body parts.
	\item Purely representative model – No discriminative learning
\end{itemize}