\chapter{Singular cohomology}
Here's one way to motivate this chapter. It turns out that:
\begin{itemize}
	\ii $H_n(\CP^2) \cong H_n(S^2 \vee S^4)$ for every $n$.
	\ii $H_n(\CP^3) \cong H_n(S^2 \times S^4)$ for every $n$.
\end{itemize}
This is unfortunate, because if possible we would like
to be able to tell these spaces apart (as they are
in fact not homotopy equivalent), but the homology groups
cannot tell the difference between them.

In this chapter, we'll define a \emph{cohomology group} $H^n(X)$ and $H^n(Y)$.
In fact, the $H^n$'s are completely determined by the $H_n$'s
by the so-called \emph{universal coefficient theorem}.
However, it turns out that one can take all the cohomology groups and put
them together to form a \emph{cohomology ring} $H^\bullet$.
We will then see that $H^\bullet(X) \not\cong H^\bullet(Y)$ as rings.

\section{Cochain complexes}
\begin{definition}
A \vocab{cochain complex} $A^\bullet$ is algebraically the same as a chain complex, except that the indices increase.
So it is a sequence of abelian groups
\[ \dots \taking{\delta} A^{n-1} \taking\delta A^n \taking\delta A^{n+1} \taking\delta \dots. \]
such that $\delta^2 = 0$.
Notation-wise, we're now using subscripts, and use $\delta$ rather $\partial$.
We define the \vocab{cohomology groups} by
\[ H^n(A^\bullet) = \ker\left( A^n \taking\delta A^{n+1} \right)
	/ \img\left( A^{n-1} \taking\delta A^n \right). \]
\end{definition}

\begin{example}[de Rham cohomology]
	We have already met one example of a cochain complex:
	let $M$ be a smooth manifold and $\Omega^k(M)$ be the
	additive group of $k$-forms on $M$.
	Then we have a cochain complex
	\[ 0 \taking d \Omega^0(M)
		\taking d \Omega^1(M) \taking d \Omega^2(M)
		\taking d \dots. \]
	The resulting cohomology is called \vocab{de Rham cohomology},
	described later.
\end{example}

Aside from de Rham's cochain complex,
\textbf{the most common way to get a cochain complex
is to \emph{dualize} a chain complex.}
Specifically, pick an abelian group $G$;
note that $\Hom(-, G)$ is a contravariant functor,
and thus takes every chain complex
\[ \dots \taking\partial A_{n+1} \taking\partial
	A_n \taking\partial A_{n-1} \taking\partial \dots \]
into a cochain complex: letting $A^n = \Hom(A_n, G)$ we obtain
\[ \dots \taking\delta A^{n-1} \taking\delta
	A^n \taking\delta A^{n+1} \taking\delta \dots. \]
where $\delta(A_n \taking{f} G) = A_{n+1} \taking\partial A \taking{f} G$.

These are the cohomology groups we study most in algebraic topology,
so we give a special notation to them.
\begin{definition}
	Given a chain complex $A_\bullet$ of abelian groups and another group $G$,
	we let \[ H^n(A_\bullet; G) \] denote the cohomology groups
	of the dual cochain complex $A^\bullet$ obtained by applying $\Hom(-,G)$.
	In other words, $H^n(A_\bullet; G) = H^n(A^\bullet)$.
\end{definition}

\section{Cohomology of spaces}
\prototype{$C^0(X;G)$ all functions $X \to G$ while $H^0(X)$ are those functions $X \to G$
constant on path components.}

The case of interest is our usual geometric situation, with $C_\bullet(X)$.
\begin{definition}
	For a space $X$ and abelian group $G$,
	we define $C^\bullet(X;G)$ to be the dual to the
	singular chain complex $C_\bullet(X)$,
	called the \vocab{singular cochain complex} of $X$;
	its elements are called \vocab{cochains}.

	Then we define the \vocab{cohomology groups}
	of the space $X$ as
	\[ H^n(X; G) \defeq H^n(C_\bullet(X); G) = H_n(C^\bullet(X;G)). \]
\end{definition}
\begin{remark}
	Note that if $G$ is also a ring (like $\ZZ$ or $\RR$),
	then $H^n(X; G)$ is not only an abelian group but actually a $G$-module.
\end{remark}

\begin{example}
	[$C^0(X; G)$, $C^1(X; G)$, and $H^0(X;G)$]
	Let $X$ be a topological space and consider $C^\bullet(X)$.
	\begin{itemize}
		\ii $C_0(X)$ is the free abelian group on $X$,
		and $C^0(X) = \Hom(C_0(X), G)$.
		So a $0$-cochain is a function that
		takes every point of $X$ to an element of $G$.
		\ii $C_1(X)$ is the free abelian group on $1$-simplices in $X$.
		So $C^1(X)$ needs to take every $1$-simplex to an element of $G$.
	\end{itemize}
	Let's now try to understand $\delta : C^0(X) \to C^1(X)$.
	Given a $0$-cochain $\phi \in C^0(X)$,
	i.e.\ a homomorphism $\phi : C^0(X) \to G$,
	what is $\delta\phi : C^1(X) \to G$?
	Answer:
	\[ \delta\phi : [v_0, v_1] \mapsto \phi([v_0]) - \phi([v_1]). \]
	Hence, elements of
	$\ker(C^0 \taking\delta C^1) \cong H^0(X;G)$
	are those cochains
	that are \emph{constant on path-connected components}.
\end{example}
In particular, much like $H_0(X)$, we have \[ H^0(X) \cong G^{\oplus r} \]
if $X$ has $r$ path-connected components (where $r$ is finite\footnote{%
	Something funny happens if $X$ has \emph{infinitely} many path-connected components:
	say $X = \coprod_\alpha X_\alpha$ over an infinite indexing set.
	In this case we have
	$H_0(X) = \bigoplus_\alpha G$ while $H^0(X) = \prod_\alpha G$.
	For homology we get a \emph{direct sum} while
	for cohomology we get a \emph{direct product}.

	These are actually different for infinite indexing sets.
	For general modules $\bigoplus_\alpha M_\alpha$ is \emph{defined} to only allow
	to have \emph{finitely many} zero terms.
	(This was never mentioned earlier in the Napkin,
	since I only ever defined $M \oplus N$ and extended it to finite direct sums.)
	No such restriction holds for $\prod_\alpha G_\alpha$ a product of groups.
	This corresponds to the fact that $C_0(X)$ is formal linear sums of $0$-chains
	(which, like all formal sums, are finite)
	from the path-connected components of $G$.
	But a cochain of $C^0(X)$ is a \emph{function}
	from each path-connected component of $X$ to $G$,
	where there is no restriction.
}).

To the best of my knowledge, the higher cohomology groups $H^n(X; G)$
(or even the cochain groups $C^n(X; G) = \Hom(C_n(X), G)$) are harder to describe concretely.

\begin{abuse}
	In this chapter the only cochain complexes
	we will consider are dual complexes as above.
	So, any time we write a chain complex $A^\bullet$ it is implicitly given
	by applying $\Hom(-,G)$ to $A_\bullet$.
\end{abuse}

\section{Cohomology of spaces is functorial}
We now check that the cohomology groups still exhibit the same nice functorial behavior.
First, let's categorize the previous results we had:

\begin{ques}
	Define $\catname{CoCmplx}$
	the category of cochain complexes.
\end{ques}

\begin{exercise}
	Interpret $\Hom(-,G)$ as a contravariant functor
	from \[ \Hom(-,G) : \catname{Cmplx}\op \to \catname{CoCmplx}. \]
	This means in particular that given a chain map $f : A_\bullet \to B_\bullet$,
	we naturally obtain a dual map $f^\vee : B^\bullet \to A^\bullet$.
\end{exercise}

\begin{ques}
	Interpret $H^n : \catname{CoCmplx} \to \catname{Grp}$ as a functor.
	Compose these to get a contravariant functor
	$H^n(-;G) : \catname{Cmplx}\op \to \catname{Grp}$.
\end{ques}

Then in exact analog to our result that $H_n : \catname{hTop} \to \catname{Grp}$ we have:
\begin{theorem}[$H^n (-;G): \catname{hTop}\op \to \catname{Grp}$]
	For every $n$, $H^n(-;G)$ is a contravariant functor
	from $\catname{hTop}\op$ to $\catname{Grp}$.
\end{theorem}
\begin{proof}
	The idea is to leverage the work we already did in constructing
	the prism operator earlier.
	First, we construct the entire sequence of functors
	from $\catname{Top}\op \to \catname{Grp}$:
	\begin{diagram}
		\catname{Top}\op & \rTo^{C_\bullet} & \catname{Cmplx}\op & \rTo^{\Hom(-;G)}
		& \catname{CoCmplx} & \rTo^{H^n} & \catname{Grp} \\
		X && C_\bullet(X) && C^\bullet(X;G) && H^n(X;G) \\
		\dTo^f &\rMapsto& \dTo^{f_\sharp} &\rMapsto&
		\uTo^{f^\sharp} &\rMapsto& \uTo^{f^\ast} \\
		Y && C_\bullet(Y) && C^\bullet(Y;G) && H^n(Y;G).
	\end{diagram}
	Here $f^\sharp = (f_\sharp)^\vee$, and $f^\ast$
	is the resulting induced map on homology groups of the cochain complex.

	So as before all we have to show is that $f \simeq g$,
	then $f^\ast = g^\ast$.
	Recall now that there is a prism operator such that
	$f_\sharp - g_\sharp = P \partial + \partial P$.
	If we apply the entire functor $\Hom(-;G)$ we get that
	$f^\sharp - g^\sharp = \delta P^\vee + P^\vee \delta$
	where $P^\vee : C^{n+1}(Y;G) \to C^n(X;G)$.
	So $f^\sharp$ and $g^\sharp$ are chain homotopic thus $f^\ast = g^\ast$.
\end{proof}

\section{Universal coefficient theorem}
We now wish to show that the cohomology groups are determined up to isomorphism
by the homology groups: given $H_n(A_\bullet)$, we can extract $H^n(A_\bullet; G)$.
This is achieved by the \emph{universal coefficient theorem}.
\begin{theorem}
	[Universal coefficient theorem]
	Let $A_\bullet$ be a chain complex of \emph{free} abelian groups,
	and let $G$ be another abelian group.
	Then there is a natural short exact sequence
	\[
		0 \to \Ext(H_{n-1}(A_\bullet), G) \to H^n(A_\bullet; G)
		\taking{h} \Hom(H_n(A_\bullet), G) \to 0. \]
	In addition, this exact sequence is \emph{split}
	so in particular
	\[ H^n(C_\bullet; G) \cong \Ext(H_{n-1}(A_\bullet, G))
		\oplus \Hom(H_n(A_\bullet), G). \]
\end{theorem}
Fortunately, in our case of interest, $A_\bullet$ is $C_\bullet(X)$
which is by definition free.

There are two things we need to explain, what the map $h$ is and the map $\Ext$ is.

It's not too hard to guess how \[ h : H^n(A_\bullet; G) \to \Hom(H_n(A_\bullet), G) \] is defined.
An element of $H^n(A_\bullet;G)$ is represented by a function which sends a cycle
in $A_n$ to an element of $G$.
The content of the theorem is to show that $h$ is surjective with kernel $\Ext(H_{n-1}(A_\bullet), G)$.

What about $\Ext$?
It turns out that $\Ext(-,G)$ is the so-called \vocab{Ext functor}, defined as follows.
Let $H$ be an abelian group, and consider a \vocab{free resolution} of $H$,
by which we mean an exact sequence
\[ \dots \taking{f_2} F_1 \taking{f_1} F_0 \taking{f_0} H \to 0 \]
with each $F_i$ free.
Then we can apply $\Hom(-,G)$ to get a cochain complex
\[ \dots \xleftarrow{f_2^\vee} \Hom(F_1, G) \xleftarrow{f_1^\vee}
	\Hom(F_0, G) \xleftarrow{f_0^\vee} \Hom(H,G) \leftarrow 0. \]
but \emph{this cochain complex need not be exact}
(in categorical terms, $\Hom(-,G)$ does not preserve exactness).
We define \[ \Ext(H,G) \defeq \ker(f_2^\vee) / \img(f_1^\vee) \]
and it's a theorem that this doesn't depend on the choice of the free resolution.
There's a lot of homological algebra that goes into this,
which I won't take the time to discuss;
but the upshot of the little bit that I did include is that the $\Ext$
functor is very easy to compute in practice, since
you can pick any free resolution you want and compute the above.

%By ``natural'', we mean that if $f : A_\bullet \to B_\bullet$ is a chain map,
%then we obtain a commutative diagram
%\begin{diagram}
%	0 & \rTo & \Ext(H_{n-1}(A_\bullet), G) & \rTo
%		& H^n(A_\bullet;G) & \rTo & \Hom(H_n(A_\bullet), G) & \rTo & 0 \\
%	& & \uTo^{ \Ext(f_\ast, G) } & & \uTo^{f^\ast} & & \uTo^{\Hom(f_\ast, G)} & & \\
%	0 & \rTo & \Ext(H_{n-1}(B_\bullet), G) & \rTo
%		& H^n(A_\bullet;G) & \rTo & \Hom(H_n(B_\bullet), G) & \rTo & 0 \\
%\end{diagram}
%where $f_\ast$ is the induced arrow $H_n(A_\bullet) \to H_n(B_\bullet)$.

\begin{lemma}
	[Computing the $\Ext$ functor]
	For any abelian groups $G$, $H$, $H'$ we have
	\begin{enumerate}[(a)]
		\ii $\Ext(H \oplus H', G) = \Ext(H, G) \oplus \Ext(H', G)$.
		\ii $\Ext(H,G) = 0$ for $H$ free, and
		\ii $\Ext(\Zc n, G) = G / nG$.
	\end{enumerate}
\end{lemma}
\begin{proof}
	For (a), note that if $\dots \to F_1 \to F_0 \to H \to 0$
	and $\dots \to F_1' \to F_0' \to F_0' \to H' \to 0$ are free resolutions,
	then so is $F_1 \oplus F_1' \to F_0 \oplus F_0' \to H \oplus H' \to 0$.

	For (b), note that $0 \to H \to H \to 0$ is a free resolution.

	Part (c) follows by taking the free resolution
	\[ 0 \to \ZZ \taking{\times n} \ZZ \to \Zc n \to 0 \]
	and applying $\Hom(-,G)$ to it.
	\begin{ques}
		Finish the proof of (c) from here. \qedhere
	\end{ques}
\end{proof}

\begin{ques}
	Some $\Ext$ practice: compute
	$\Ext(\ZZ^{\oplus 2015}, G)$ and $\Ext(\Zc{30}, \Zc 4)$.
\end{ques}

\section{Example computation of cohomology groups}
\prototype{Possibly $H^n(S^m)$.}

The universal coefficient theorem gives us a direct way to compute
any cohomology groups, provided we know the homology ones.

\begin{example}
	[Cohomolgy groups of $S^m$]
	It is straightforward to compute $H^n(S^m)$ now:
	all the $\Ext$ terms vanish since $H_n(S^m)$ is always free,
	and hence we obtain that
	\[ H^n(S^m) \cong \Hom(H_n(S^m), G) \cong
		\begin{cases}
			G & n=m, n=0 \\
			0 & \text{otherwise}.
		\end{cases}
	\]
%	By UCT for reduced groups, we also have
%	\[ \wt H^n(S^m) \cong \Hom(\wt H_n(S^m), G) \cong
%		\begin{cases}
%			G & n=m \\
%			0 & \text{otherwise}.
%		\end{cases}
%	\]
%	since $\Hom(\ZZ, G)$.
\end{example}

\begin{example}
	[Cohomolgy groups of torus]
	This example has no nonzero $\Ext$ terms either,
	since this time $H^n(S^1 \times S^1)$ is always free.
	So we obtain
	\[ H^n(S^1 \times S^1) \cong \Hom(H_n(S^1 \times S^1), G). \]
	Since $H_n(S^1 \times S^1)$ is $\ZZ$, $\ZZ^{\oplus 2}$, $\ZZ$
	in dimensions $n=1,2,1$ we derive that
	\[
		H^n(S^1 \times S^1)
		\cong
		\begin{cases}
			G & n = 0,2 \\
			G^{\oplus 2} & n = 1.
		\end{cases}
	\]
\end{example}

From these examples one might notice that:
\begin{lemma}
	[$0$th homology groups are just duals]
	For $n = 0$ and $n = 1$, we have
	\[ H^n(X;G) \cong \Hom(H_n(X), G). \]
\end{lemma}
\begin{proof}
	It's already been shown for $n=0$.
	For $n=1$, notice that $H_0(X)$ is free,
	so the $\Ext$ term vanishes.
\end{proof}

\begin{example}
	[Cohomolgy groups of Klein bottle]
	This example will actually have $\Ext$ term.
	Recall that if $K$ is a Klein Bottle then its homology groups are
	$\ZZ$ in dimension $n=0$ and $\ZZ \oplus \Zc 2$ in $n=1$, and $0$ elsewhere.

	For $n=0$, we again just have $H^0(K;G) \cong \Hom(\ZZ, G) \cong G$.
	For $n=1$, the $\Ext$ term is $\Ext(H_0(K), G) \cong \Ext(\ZZ, G) = 0$
	so \[ H^1(K;G) \cong \Hom(\ZZ \oplus \Zc2, G) \cong G \oplus \Hom(\Zc2, G). \]
	We have that $\Hom(\Zc2,G)$ is the subgroup
	of elements of order $2$ in $G$ (and $0 \in G$).

	But for $n=2$, we have our first interesting $\Ext$ group:
	the exact sequence is
	\[ 0 \to \Ext(\ZZ \oplus \Zc 2, G) \to H^2(X;G) \to \underbrace{H_2(X)}_{=0} \to 0. \]
	Thus, we have
	\[ H^2(X;G) \cong \left( \Ext(\ZZ,G) \oplus \Ext(\Zc2,G) \right) \oplus 0
		\cong G/2G. \]
	All the higher groups vanish.
	In summary:
	\[
		H^n(X;G) \cong
		\begin{cases}
			G & n = 0 \\
			G \oplus \Hom(\Zc2, G) & n = 1 \\
			G/2G & n = 2 \\
			0 & n \ge 3.
		\end{cases}
	\]
\end{example}

\section{Relative cohomology groups}
One can also define relative cohomology groups in the obvious way:
dualize the chain complex
\[ \dots \taking\partial C_1(X,A) \taking\partial C_0(X,A) \to 0 \]
to obtain a cochain complex
\[
	\dots \xleftarrow\delta C^1(X,A;G) \xleftarrow\delta C^0(X,A;G)
	\leftarrow 0.
\]
We can take the cohomology groups ofthis.
\begin{definition}
	The groups thus obtained are the \vocab{relative cohomology groups}
	are denoted $H^n(X,A;G)$.
\end{definition}

In addition, we can define reduced cohomology groups as well.
One way to do it is to take the augmented singular chain complex
\[ \dots \taking\partial C_1(X) \taking\partial C_0(X) \taking\eps \ZZ \to 0 \]
and dualize it to obtain
\[
	\dots \xleftarrow\delta C^1(X;G) \xleftarrow\delta C^0(X;G)
	\xleftarrow{\eps^\vee} \underbrace{\Hom(\ZZ, G)}_{\cong G}
	\leftarrow 0.
\]
Since the $\ZZ$ we add is also free,
the universal coefficient theorem still applies.
So this will give us reduced cohomology groups.

However, since we already defined the relative cohomology groups,
it is easiest to simply define:
\begin{definition}
	The \vocab{reduced cohomology groups} of a nonempty space $X$,
	denoted $\wt H^n(X; G)$,
	are defined to be $H^n(X, \{\ast\} ; G)$
	for some point $\ast \in X$.
\end{definition}

\section\problemhead
\begin{sproblem}
	[Wedge product cohomology]
	For any $G$ and $n$ we have
	\[
		\wt H^n(X \vee Y; G)
		\cong
		\wt H^n(X; G) \oplus \wt H^n(Y; G).
	\]
\end{sproblem}

\begin{dproblem}
	Prove that for a field $F$ of characteristic zero and a space $X$
	with finitely generated homology groups:
	\[ H^k(X, F) \cong \left( H_k(X) \right)^\vee.  \]
	Thus over fields cohomology is the dual of homology.
\end{dproblem}

\begin{problem}[$\Zc2$-cohomology of $\RP^n$]
	Prove that
	\[
		H^m(\RP^n, \Zc2)
		\cong
		\begin{cases}
			\ZZ & \text{$m=0$, or $m$ is odd and $m=n$} \\
			\Zc2 & \text{$0 < m < n$ and $m$ is odd} \\
			0 & \text{otherwise}.
		\end{cases}
	\]
\end{problem}


\section{Introduction}
\subsection{Motivation and Examples}
We start by asking a natural question that appears frequently in the study of topology:
\begin{problem}\label{homeo}
    How can we tell if two topological space are different (i.e. not homoeomorphic)?
\end{problem}
For example, if we let $X$ to be the donut-like surface in $\mathbb R^3$, and $Y$ be a similar donut but with two holes instead.
Our intuition tells us they are not homeomorphic because, well, $X$ has one hole and $Y$ has two -- but this is not much a proof.\\
Our basic strategy of coping with problems like these in algebraic topology is to associate each topological space $X$ (often, in a specific class) with a group $H(X)$ and associate each continuous map $f:X\to Y$ a homomorphism of groups $H(f):H(X)\to H(Y)$.
We obviously want $H$ to preserve compositions of functions and identities as well.
\footnote{In short, we just want $H$ to be a functor from a subcategory of $\mathbf{Top}$ to an abelian category, preferably $\mathbf{Grp}$.}
With certain constructions, if $X\cong Y$ as topological spaces, we can ensure that $H(X)\cong H(Y)$ as groups.
In other words, $H$ exists as an algebraic invariant.
As it is much easier to show two groups are not isomorphic, we can solve some cases of Problem \ref{homeo} where $H(X)$ can be shown not to isomorphic to $H(Y)$.
The study of algebraic topology is basically the hunt of such algebraic invariants.\\
Another application of this idea is to solve the Extension Problem:
\begin{problem}[The Extension Problem]
    Let $X$ be a topological space and $A\subset X$ a subspace.
    If we have $f:A\to Y$ is a continuous map, how do we know if there is a continuous map $F:X\to Y$ with $F|_A=f$?
\end{problem}
That is, we want to know if there exists $F$ such that the diagram
\[
    \begin{tikzcd}
        A \arrow[hookrightarrow]{d} \arrow{r}{f} & Y\\
        X \arrow[dashed,swap]{ur}{F}&
    \end{tikzcd}
\]
commutes.
An example of this is the following theorem:
\begin{theorem}
    There is no continuous function $F:D^n\to S^{n-1}$ such that
    \[
        \begin{tikzcd}
            S^{n-1} \arrow[hookrightarrow]{d} \arrow{r}{\operatorname{id}} & S^{n-1}\\
            D^n \arrow[swap]{ur}{F}
        \end{tikzcd}
    \]
    commutes.
\end{theorem}
Here, $D^n$ is the $n$-disk while $S^{n-1}$ is the $(n-1)$-sphere, which is simply the hypersurface enclosing $D^n$.
How does the idea of the algebraic invariant come in handy on this problem?
Obviously we do not yet have the correct tool to do it now that the course has just started, but we can take a glimpse of the idea involved.
\begin{proof}
    Construct an invariant $H$ such that $H(S^{n-1})\cong\mathbb Z$ and $H(D^n)\cong 0$.
    Then the diagram in the statement of the theorem will implies that
    \[
        \begin{tikzcd}
            \mathbb Z\arrow{d} \arrow{r}{\operatorname{id}} & \mathbb Z\\
            0 \arrow{ur}
        \end{tikzcd}
    \]
    commute.
    But this is absurd.
\end{proof}
All these seems nice, right?
But first, we really need a clever construction of such a nontrivial algebraic invariants bearing such nice properties.
\subsection{Conventions}
Unless otherwise stated, we adopt the following conventions:\\
When we mention ``space'', we always mean a topological space.
And when we say a ``map'' between two spaces, we always mean a continuous one.
By $I$, we mean the unit interval $[0,1]$.


\chapter{Appendix}
%\addcontentsline{toc}{chapter}{Appendix}
\section{Code}
\begin{labwork}[PDF and DF of a $\normal(\mu,\sigma^2)$ RV]\label{Mf: NormalCdfPdf}
Here are the functions to evaluate the PDF and DF of a $\normal(\mu,\sigma^2)$ RV $X$ at a given $x$.
{\VrbMf[label=NormalPdf.m]{scripts/NormalPdf.m}}
{\VrbMf[label=NormalCdf.m]{scripts/NormalCdf.m}}
Plots of the PDF and DF of several Normally distributed RVs depicted in \hyperref[F:plotPdfCdfNormals]{Figure~\ref*{F:plotPdfCdfNormals}} were generated using the following script file:
{\VrbMf[label=PlotPdfCdfNormal.m]{scripts/PlotPdfCdfNormal.m}}
\end{labwork}

\begin{labwork}[PDF and DF of an $\exponential(\lambda)$ RV $X$]\label{Mf:ExponentialPdfCdf}
Here are the functions to evaluate the PDF and DF of an $\exponential(\lambda)$ RV $X$ at a  given $x$ (point or a vector).
{\VrbMf[label=ExponentialPdf.m]{scripts/ExponentialPdf.m}}
{\VrbMf[label=ExponentialCdf.m]{scripts/ExponentialCdf.m}}
Plots of the PDF and DF of several Exponentially distributed RVs at four axes scales that are depicted in \hyperref[F:plotPdfCdfExponentials]{Figure \ref*{F:plotPdfCdfExponentials}} were generated using the following script file:
{\VrbMf[label=PlotPdfCdfExponential.m]{scripts/PlotPdfCdfExponential.m}}
\end{labwork}

%\begin{labwork}\label{Mf:ECDF}
%A {\sc Matlab} function to plot the empirical DF \eqref{E:ECDF} of $n$ user-specified samples.  Read the following M-file for the algorithm:
%{\VrbMf[label=ECDF.m]{scripts/ECDF.m}}
%Ideally, this function needs to be rewritten using primitives such as {\sc Matlab}'s {\tt line} commands.
%\end{labwork}

\begin{labwork}[Plotting the empirical DF]\label{Mf:ECDF}
A {\sc Matlab} function to plot the empirical DF \eqref{E:ECDF} of $n$ user-specified samples efficiently for massive number of samples.  Read the following M-file for the algorithm:
{\VrbMf[label=ECDF.m]{scripts/ECDF.m}}
Ideally, this function needs to be rewritten using primitives such as {\sc Matlab}'s {\tt line} commands.
\end{labwork}

\begin{labwork}[q-th sample quantile]\label{Mf:qthSampleQuantile}
Let us implement \hyperref[A:qthSampleQuantile]{Algorithm \ref*{A:qthSampleQuantile}} as the following {\sc Matlab} function:
{\VrbMf[label=qthSampleQuantile.m]{scripts/qthSampleQuantile.m}}
\end{labwork}

\begin{labwork}[Loading ]\label{Mf:NZEQChCch20110222}
Let us save all the steps done in \hyperref[LW:NZEQChCch20110222]{Labwork~\ref*{LW:NZEQChCch20110222}} into the following script M-file:
{\VrbMf[label=NZEQChCch20110222.m]{scripts/NZEQChCch20110222.m}}
\end{labwork}

\remove{
\begin{labwork}[Importance Resampler Demo]\label{Mf:ImpResamplerCauchyViaNormal}
Visualisation of the Importance Resampler of \hyperref[A:ImpReSampler]{Algorithm~\ref*{A:ImpReSampler}} by producing approximate samples from $\cauchy$ using samples from $\normal(0,1)$.
%\hyperref[F:LevyDensityPlot]{Figure \ref*{F:LevyDensityPlot}} was made with the following script file.
{\VrbMf[label=ImpResamplerCauchyViaNormal.m]{scripts/ImpResamplerCauchyViaNormal.m}}
\end{labwork}

\begin{labwork}[Levy density plot]\label{Mf:LevyDensityPlot}
\hyperref[F:LevyDensityPlot]{Figure \ref*{F:LevyDensityPlot}} was made with the following script file.
{\VrbMf[label=LevyDensityPlot.m]{scripts/LevyDensityPlot.m}}
\end{labwork}

\begin{labwork}[Negative of the Levy density]\label{Mf:NegLevyDensity}
The negative of the Levy density \eqref{E:LevyDensity} is encoded in the following M-file as a function to be passed to {\sc Matlab}'s {\tt fminsearch}.
{\VrbMf[label=NegLevyDensity.m]{scripts/NegLevyDensity.m}}
\end{labwork}

\begin{labwork}[Log-likelihood of $\lognormal$]\label{Mf:LogNormalLogLklPlot}
\hyperref[F:LogNormalLogLklPlot]{Figure \ref*{F:LogNormalLogLklPlot}} was made with the following script file.
{\VrbMf[label=LogNormalLogLklPlot.m]{scripts/LogNormalLogLklPlot.m}}
\end{labwork}
}

\begin{labwork}[Consistency of MLE in Bernoulli experiment]\label{Mf:BernoulliMLEConsistency}
\hyperref[F:BernoulliMLEConsistency]{Figure~\ref*{F:BernoulliMLEConsistency}} was made with the following script file.
{\VrbMf[label=BernoulliMLEConsistency.m]{scripts/BernoulliMLEConsistency.m}}
\end{labwork}

\begin{labwork}[Gilvenko-Cantelli Lemma for $\uniform(0,1)$]\label{Mf:GilvenkoCantelliUnif01n10n100n100ECDFs}
The following script was used to generate the \hyperref[F:GilvenkoCantelliUnif01n10n100n100ECDFs]{Figure \ref*{F:GilvenkoCantelliUnif01n10n100n100ECDFs}}.
{\VrbMf[label=GilvenkoCantelliUnif01.m]{scripts/GilvenkoCantelliUnif01.m}}
\end{labwork}

\section{Data}\label{S:Data}
%\addcontentsline{toc}{chapter}{Data Appendix}

Here we describe some of the data sets we analyze.
\begin{data}[Our Maths \& Stats Dept. Web Logs]\label{DA:WebLogs}
We assume access to a {\tt Unix} terminal ({\tt Linux, Mas OS X, Sun Solaris}, etc).  We show how to get your hands dirty with web logs that track among others, every {\tt IP} address and its time of login to our department web server over the world-wide-web.  The raw text files of web logs may be manipulated but they are typically huge files and need some {\tt Unix} command-line utilities.
\begin{VrbM}
rsa64@mathopt03:~> cd October010203WebLogs/
rsa64@mathopt03:~/October010203WebLogs> ls -al
-rw-r--r--+  1 rsa64 math 7527169 2007-10-04 09:38 access-07_log.2
-rw-r--r--+  1 rsa64 math 7727745 2007-10-04 09:38 access-07_log.3
\end{VrbM}
The files are quite large over 7.5 MB each.  So we need to compress it.  We use the {\tt gzip} and {\tt gunzip} utility in any {\tt Unix} environment to compress and decompress these large text files of web logs.  After compression the file sizes are more reasonable.
\begin{VrbM}
rsa64@mathopt03:~/October010203WebLogs> gzip access-07_log.3
rsa64@mathopt03:~/October010203WebLogs> gzip access-07_log.2
rsa64@mathopt03:~/October010203WebLogs> zcat access-07_log.2.gz | grep ' 200 '
| awk '{ print \$4}'| sed -e 's/\[\([0-9]\{2\}\)\/\([a-Z]\{3\}\)\/\([0-9]\{4\}\)
:\([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)/\3 10 \1 \4 \5 \6/'
2007 10 02 03 57 48
2007 10 02 03 58 31
.
.
.
2007 10 03 03 56 21
2007 10 03 03 56 52
\end{VrbM}
Finally, there are $56485$ and $53966$ logins for the two 24-hour cycles, starting {\tt 01/Oct} and {\tt 01/Oct}, respectively.  We can easily get these counts by further piping the previous output into the line counting utility {\tt wc} with the {-l} option.  All the {\tt Unix} command-line tools mentioned earlier can be learned by typing {\tt man } followed by the tool-name, for eg.~type {\tt man sed} to learn about the usage of {\tt sed} ata  {\tt Unix} command shell.  We further pipe the output of login times for the two 24-hour cycles starting {\tt 01/Oct} and {\tt 02/Oct} in format {\tt YYYY MM DD HH MM SS} to
{\tt | sed -e 's/2007 10 //' > WebLogTimes20071001035730.dat} and
{\tt ... > WebLogTimes20071002035730.dat}, respectively to strip away the redundant information on {\tt YYYY MM }, namely {\tt 2007 10 }, and only save the relevant information of {\tt DD HH MM SS} in files named
{\tt WebLogTimes20071001035730.dat} and {\tt WebLogTimes20071002035730.dat}, respectively.  These two files have the data of interest to us.  Note that the size of these two uncompressed final data files in plain text are smaller than the compressed raw web log files we started out from.
\begin{VrbM}
rsa64@mathopt03:~/October010203WebLogs> ls -al
-rw-r--r--+  1 rsa64 math 677820 2007-10-05 15:36 WebLogTimes20071001035730.dat
-rw-r--r--+  1 rsa64 math 647592 2007-10-05 15:36 WebLogTimes20071002035730.dat
-rw-r--r--+  1 rsa64 math 657913 2007-10-04 09:38 access-07_log.2.gz
-rw-r--r--+  1 rsa64 math 700320 2007-10-04 09:38 access-07_log.3.gz
\end{VrbM}
Now that we have been familiarized with the data of login times to our web-server over 2 24-hour cycles, let us do some statistics.  The log files and basic scripts are courtesy of the Department's computer systems administrators Paul Brouwers and Steve Gourdie.  This data processing activity was shared in such detail to show you that statistics is only meaningful when the data and the process that generated it are clear to the experimenter.  Let us process the data and visualize the empirical distribution functions using the following script:
{\VrbMf[label=WebLogDataProc.m]{scripts/WebLogDataProc.m}}
\end{data}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Optimization}
\label{chap:opt}
% TODO
% TODO many types of optimizers, two main classes of gradient and non-gradient methods.
% TODO A greedy optimizer finds the local optimum at each iteration, which may or may not converge to a global optimum.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Maximum Likelihood Estimation (MLE)}
\label{opt:MLE}

Maximum likelihood estimation (MLE) is a method
of choosing the optimal parameters of a probability distribution
to match some set of observed data.
As the name suggests, MLE works by maximizing the likelihood of observing the given data.
Note that the terms likelihood and probability are closely related, but different, in this context.
Here probability refers to the probability of observing $x$ given parameters $\vb*{\beta}$, $P\left(x \mid \vb*{\beta}\right)$,
while likelihood refers to the likelihood of the distribution having parameters $\vb*{\beta}$ given an observation $x$, $L\left(\vb*{\beta} \mid x\right)$.
$L$ and $P$ are both equal to relevant probability distribution,
such as \cref{eq:stats:gaus:P} for the Gaussian distribution,
but which variables are independent and dependent changes.

As we wish to maximize the likelihood with respect to $\vb*{\beta}$,
we will be taking partial derivatives and finding where they equal zero, $\partial_{\vb*{\beta}} L = 0$.
In practice, likelihood functions are complicated\footnote{The value of $L$
is often close to $0$, making lack of precision an issue when doing computations.
$\log\left(L\right)$ is larger and also helps address this problem.} and
it is easier to maximize the log-likelihood $\log\left(L\right)$,
which has the same maximum as the original likelihood $L$,
but removes exponents and turns multiplication in to addition.
Generally there are $m$ points $\vb{x}_{i}$, each with $n$ dimensions, in a given dataset,
thus we are solving \cref{eq:MLE} for $\vb*{\beta} = \hat{\vb*{\beta}}_{\text{MLE}}$:

\begin{subequations}\label{eq:MLE}
\begin{align}
0 &= \partial_{\vb*{\beta}} \, L\left(\vb*{\beta} \mid \vb{x}\right) \label{eq:MLE:L} \\
\implies 0 &= \partial_{\vb*{\beta}} \log\left(L\left(\vb*{\beta} \mid \mathbf{X}\right)\right) = \partial_{\vb*{\beta}} \, \log\left(\prod_{i=1}^{m} \, P\left(\vb{x}_{i} \mid \vb*{\beta}\right)\right) \label{eq:MLE:log_L} \\
&= \sum_{i=1}^{m} \, \partial_{\vb*{\beta}} \log\left(P\left(\vb{x}_{i} \mid \vb*{\beta}\right)\right) \label{eq:MLE:log_L_sum}
\end{align}
\end{subequations}

In practice, it is unlikely that a closed form solution for $\hat{\vb*{\beta}}_{\text{MLE}}$ can be found
and numerical optimizers, such as gradient descent, are used instead.
Under certain conditions it can be shown that as $n \to \infty$ the MLE converges to $\hat{\vb*{\beta}}$,
\ie in the limit $n \to \infty$ no consistent estimator
has a lower MSE\footnote{More formally, the MLE achieves the Cram\'er--Rao lower bound.}.
Lastly, while the MLE is usually a biased estimator, see \cref{stats:bias},
the bias is also reduced as $n \to \infty$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Exponential Distribution Example}
\label{opt:MLE:exp_ex}

We can use MLE to find the optimal $\hat{\lambda}_{\text{MLE}}$ of
the exponential distribution \cref{eq:stats:exp:P}
given a dataset of $m$ points, $x_{i}$, in 1 dimension:

\begin{subequations}\label{eq:MLE:exp_ex}
\begin{align}
0 &= \sum_{i=1}^{m} \partial_{\lambda} \log\left(P\left(x_{i} \mid \lambda\right)\right) = \sum_{i=1}^{m} \partial_{\lambda} \log\left(\lambda e^{-\lambda x_{i}}\right), \label{eq:MLE:exp_ex:log_L_sum} \\
&= \sum_{i=1}^{m}\partial_{\lambda} \left( \log\left(\lambda\right) -\lambda x_{i}\right) = \sum_{i=1}^{m} \frac{1}{\lambda} - x_{i} = \frac{m}{\lambda} - \sum_{i=1}^{m} x_{i}, \label{eq:MLE:exp_ex:solve} \\
\implies \hat{\lambda}_{\text{MLE}} &= \frac{m}{\sum_{i=1}^{m} x_{i}} = \frac{1}{\expval{x}}. \label{eq:MLE:exp_ex:lambda}
\end{align}
\end{subequations}

For another example of MLE see logistic regression \cref{eq:logistic:L_Pr}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Maximum A Posteriori (MAP)}
\label{opt:MAP}
% TODO In maximum \aposteriori (MAP) estimation
% TODO MLE is a special case of MAP with a uniform prior

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Gradient Descent}
\label{opt:grad_descent}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Stochastic Gradient Descent (SGD)}
\label{opt:grad_descent:stochastic}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Lagrange Multipliers}
\label{opt:lagrange_mult}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Newton's Method}
\label{opt:newton}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Exploration-Exploitation Tradeoff}
\label{opt:EE_tradeoff}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Bayesian Bandits}
\label{opt:BB}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Evolutionary \& Genetic Algorithms}
\label{opt:evo}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Bayesian Optimization}
\label{opt:BO}

Frequently we are fortunate enough to have a fairly explicit form of
the objective function $S\left(\vb*{\beta}\right)$ to be optimized in order to solve a problem.
However, when $S\left(\vb*{\beta}\right)$ is not well-known,
is expensive to compute\footnote{The performance of a machine learning model as a function of its hyperparameters is a classic example of this.
In that case, evaluating $S$ amounts to training the model with a particular set of hyperparameters $\vb*{\beta}$,
then determining it's performance on a metric such as ROC AUC.}, or is non-differentiable,
the usual gradient based approaches, such as SGD and Newton's method, break down.
In these cases ``black box''\footnote{Black box as in
we do not have a closed-form expression for $S$, or know $\grad S$.} methods\footnote{Other examples of black box optimizers include
Tree-Structured Parzen Estimators (TPE),
genetic algorithms,
and some additional tree based methods described in \cite{Hutter2011,Hutter2014}.
A TPE is a close relative of Bayesian optimization, making use of the opposite side of Bayes' theorem by
estimating $P\left(\vb*{\beta} \mid S\right)$ and $P\left(S\right)$ rather than $P\left(S \mid \vb*{\beta}\right)$.
TPEs can accommodate categorical $\beta_{i}$ in a hierarchical manner,
however they can not model interactions between the $\beta_{i}$ \cite{bissuel_2019,NIPS2011_4443}.},
such as Bayesian optimization, may be used instead.

In Bayesian optimization\footnote{Generalized
formally as Sequential Model-Based Optimization (SMBO) \cite{NIPS2011_4443}.} \cite{Brochu2010,1301.1942,Borisyak,NIPS2011_4443},
$S$ is approximated by a well-known surrogate function\footnote{Also known as a response surface.}.
The surrogate typically is a Gaussian process (GP),
but can be any well-behaved regressor such as
a Random Forest or Boosted Decision Tree (BDT).
GPs \cref{eq:GP} are
extensions of Gaussian distributions which return a Gaussian at any point along their domain.
They are parameterized by a mean function\footnote{For convenience,
the prior $m\left(\vb{x}\right)$ is usually assumed to be zero, $m\left(\vb{x}\right)=0$.}, $m\left(\vb{x}\right)$,
and covariance function, $k\left(\vb{x}_{i}, \vb{x}_{j}\right)$,
\ie kernel\footnote{The kernel of the GP is a hyperparameter to be chosen in advance.
Standard kernel choices include
the radial basis function kernel $k\left(\vb{x}_{i}, \vb{x}_{j}\right) = \exp\left(-\frac{1}{2\sigma^{2}}\norm{\vb{x}_{i}-\vb{x}{j}}^{2}\right)$,
Mat\'{e}rn kernel,
and white noise kernel $k\left(\vb{x}_{i}, \vb{x}_{j}\right) \propto \delta_{ij}$.},
instead of a constant mean $\mu$ and variance $\sigma^{2}$.
An illustration of a GP is provided in \cref{fig:GP_ex}.

\begin{equation}\label{eq:GP}
f\left(\vb{x}\right) \sim \mathcal{GP}\left(m\left(\vb{x}\right), k\left(\vb{x}_{i}, \vb{x}_{j}\right)\right).
\end{equation}

\begin{figure}[H] % TODo might want to eventually remove
\centering
\includegraphics[width=0.85\textwidth]{figures/ml/gp}
\caption{
Illustration of a Gaussian process (GP) in 1D \cite{Brochu2010}.
Note that at any point $\vb{x}_{1}$, $\vb{x}_{2}$, $\vb{x}_{3}$ the GP
returns a Gaussian distribution characterizing the estimated mean and uncertainty on the
unknown true function, \ie the objective function $S\left(\vb*{\beta}\right)$ in the case of Bayesian optimization.
}
\label{fig:GP_ex}
\end{figure}

The surrogate function is initially fit to
a random sample of $\vb*{\beta}$, $S\left(\vb*{\beta}\right)$ points.
From this prior, Bayesian optimization
operates by iteratively sampling $S\left(\vb*{\beta}\right)$ and updating
the posterior surrogate function as each new piece of information is gained.
An acquisition function, $u\left(\cdot\right)$, directs the sampling,
estimating where $S\left(\vb*{\beta}\right)$ may be large
due to a high predicted value, large uncertainty, or some combination of the two.
The exploration-exploitation tradeoff inherent in $u\left(\cdot\right)$
can be tuned in various ways, see Section 2.3 of \cite{Brochu2010} for a full description\footnote{Common types of acquisition function include the
Expected Improvement (EI);
$\text{EI}\left(\vb*{\beta}\right) = \expvalE{\max\left(0,\,S\big(\vb*{\beta}\big) - S\big(\hat{\vb*{\beta}}\big)\right)}$
where $S\big(\hat{\vb*{\beta}}\big)$ is the current optimal value of $S$,
Upper Confidence Bound (UCB);
$\text{UCB}\left(\vb*{\beta}\right) = E_{\text{GP}} \left(\vb*{\beta}\right) + \kappa\,\text{var}_{\text{GP}} \left(\vb*{\beta}\right)$ where the mean and variance are of the GP and $\kappa$ sets the exploration-exploitation tradeoff,
and Maximum Probability of Improvement (MPI).}.
The iterative nature of Bayesian optimization is illustrated in \cref{fig:BO_ex}.
An accessible implementation of Bayesian optimization is available in \skopt \cite{scikit-optimize,Borisyak}.

\begin{figure}[H] % TODo might want to eventually remove
\centering
\includegraphics[width=0.85\textwidth]{figures/ml/toyGPtext3}
\caption{
Illustration of Bayesian optimization over three iterations in a toy 1D maximization problem \cite{Brochu2010}.
Note that the maximum of the acquisition function $u\left(\cdot\right)$
locates where $S\left(\vb*{\beta}\right)$ should be sampled next.
The GP estimated posterior distribution of $S\left(\vb*{\beta}\right)$
and $u\left(\cdot\right)$ are then updated.
This iterative process is repeated until the estimated maximum is satisfactory.
}
\label{fig:BO_ex}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Minimum Mean Square Error (MMSE) Estimator}
\label{opt:BO:MMSE}
% TODO


\section{Operation Research}
\begin{definition}[Corner point feasible solution(CPF]
A solution that lies at the corner of a feasible region (solution space)
If a solution has only one feasible solution, it must be a CPF
\end{definition}

\begin{definition}[KKT-conditions]
\begin{enumerate}
    \item $\frac{\partial{f}}{\partial{x_{j}}} - \sum\limits_{i=1}^{m} u_i
        \frac{\partial{g_i}}{\partial{x_j}} \leq 0 $
    \item $x_j^{*}(\frac{\partial{f}}{\partial{x_{j}}} - \sum\limits_{i=1}^{m} u_i
        \frac{\partial{g_i}}{\partial{x_j}}) = 0 $
    \item $g_i(x^{*}) - b_i \leq 0$
    \item $u_i(g_i(x^{*}) - b_i) \leq 0$
    \item $x_j^{*} \geq 0$
    \item $u_j^{*} \geq 0$
\end{enumerate}
\end{definition}

\begin{definition}[Feasile region]
    Aka solution space. The set of all valid solutions, i.e. those that do not
    violate constraints.
\end{definition}

\begin{definition}[Slope-intercept form]
    Instead of expressing an objective function, show it as
    an expression of your parameters. (e.g. $x_{1}, c_{i,j}$)
\end{definition}


\documentclass[11pt]{amsbook}
\usepackage{../HBSuerDemir}
\begin{document}
    \hPage{b2p2/414}
    \section{GEOMETRIC AND PHYSICAL APPLICATIONS}
    \subsection{GEOMETRIC APPLICATION (Area of a surface): }
    Let S: F(x, y, z) = 0 be a surface over the region R
in xy-plane (i.e. R is the projection of S on xy-planes). Then
the area of S is
    \begin{equation*}
        \hAbs{S}=\iint_S \,d\sigma
    \end{equation*}
    and we have\\
    \begin{equation*}
        \iint_S \,d\sigma= \iint_R \sec \gamma \overline {\,dx \,dy}
    \end{equation*}
\begin {figure}[ htbp ]
\begin {center}
\includegraphics[width=0.4\columnwidth]{images/b2p1-130.pdf}

\label {fig: image}
\end{center}
\end{figure}

    where $d\sigma$ is the elementary area of S and y is the acute angle between the normal line to S at a point in $d\sigma$ and z-axis.\\
    Then,
    \begin{align*}
    &\cos \gamma= \hAbs{\vec{n}.k}\\
    \Longrightarrow&\sec \gamma= \frac{1}{\hAbs{\vec{n}.k}}=
    \begin{cases}
        \sqrt{1+z_{x}^{2}+z_{y}^{2}}\quad if \quad z=f(x,y)\\
        \frac{\sqrt{F_{x}^{2}}+\sqrt{F_{y}^{2}}+\sqrt{F_{z}^{2}}}{\hAbs{F_{z}}}\quad if \quad f(x,y,z)=0
    \end{cases}
    \end{align*}

    and
    \begin{equation*}
        \hAbs{S}= \iint_R \sqrt{1+z_{x}^{2}+z_{y}^{2}}\ \overline {\,dx \,dy}\quad (or \iint_R \frac{\sqrt{F_{x}^{2}}+\sqrt{F_{y}^{2}}+\sqrt{F_{z}^{2}}}{\hAbs{F_{z}}}\ \overline {\,dx \,dy})
    \end{equation*}
    Similarly
        \begin{equation*}
        \hAbs{S}= \iint_R \sqrt{1+x_{x}^{2}+x_{z}^{2}}\ \overline {\,dx \,dz}\quad (or \iint_R \frac{\sqrt{F_{x}^{2}}+\sqrt{F_{y}^{2}}+\sqrt{F_{z}^{2}}}{\hAbs{F_{x}}}\ \overline {\,dy \,dz})
    \end{equation*}
        \begin{equation*}
        \hAbs{S}= \iint_R \sqrt{1+y_{x}^{2}+y_{z}^{2}}\ \overline {\,dx \,dz}\quad (or \iint_R \frac{\sqrt{F_{x}^{2}}+\sqrt{F_{y}^{2}}+\sqrt{F_{z}^{2}}}{\hAbs{F_{y}}}\ \overline {\,dx \,dz})
    \end{equation*}
    To obtain a simpler region R or a simpler integrand, one
projects S onto a convenient coordinate plane.
\end{document}


\chapter{Array}
\section{Circular Array}
This section describes common patterns for solving problems with circular arrays.

Normally, we should solve the linear problem and circular problem differently.

\subsection{Circular max sum}
Linear problem can be solved linear with dp algorithm for maximum subarray sum - Section \ref{dpSequence}.

The circular sum should use dp.

Problem description: Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Return the index of the first number and the index of the last number.
\runinhead{Core clues:}
\begin{enumerate}
\item \textbf{State definitions}:

Construct left max sum $L_i$ for max sum over the $[0..i]$ with subarray starting at 0 (\textit{forward} starting from the left side).

Construct right max sum $R_i$ for max sum over the indexes $[i+1..n -1]$, with subarray ending at -1 (\textit{backward} starting from the right side).

Notice, for the two max sums, the index ends AT or BEFORE $i$.

\item \textbf{Transition functions:}
\begin{align*}
L_i = \max\Big(L_{i-1}, sum(A[:i])\Big) \\
R_i = \max\Big(R_{i+1}, sum(A[i:])\Big)
\end{align*}

\item \textbf{Global result}:
$$maxa = \max(R_i+L_{i-1}, \forall i)$$
\end{enumerate}

\subsection{Non-adjacent cell}
Maximum sum of non-adjacent cells in an array $A$.

To solve circular non-adjacent array problem in linear way, we should consider 2 cases:
\begin{enumerate}
\item Not consider the $A[1]$
\item Not consider the $A[-1]$
\end{enumerate}
and solve them using linear maximum sum of non-adjacent cells separately  - Section \ref{dpSequence}.
\subsection{Binary search}
Searching for an element in a circular sorted array. Half of the array is sorted while the other half is not.
\begin{enumerate}
\item If $A[0] < A[mid]$, then all values in the first half of the array are sorted.
\item If $A[mid] < A[-1]$, then all values in the second half of the array are sorted.
\item Then \textit{derive and decide} whether to got the \textbf{sorted half} or the \textbf{unsorted half}.
\end{enumerate}
\section{Voting Algorithm}
\subsection{Majority Number}
\subsubsection{$\frac{1}{2}$ of the Size}
Given an array of integers, the majority number is the number that occurs more than half of the size of the array.

Algorithm: Majority Vote Algorithm. Maintain a counter to count how many times the majority number appear more than any other elements before index $i$ and after re-initialization. Re-initialization happens when the counter drops to 0.

Proof: assuming there is a majority number $x$, if at the index $i$, the current count is $j$ and the current counter does not capture the majority number, there are less than $\frac{i-j}{2}$ $x$, thus there are more than $\frac{n-i+j}{2}$ $x$ after the index $i$. The $j$ $x$ beats against the counter and $\frac{n-i-j}{2}$ $x$ will make it counted by counter.

If the counter captures the majority number, two cases will happen. The one is that the counter continue to capture the majority number till the end; then the counter will captures the correct majority number. The other case is that the majority number counter is beaten by other numbers, which will in turn fall back to the case that the counter does not capture the majority number.

This algorithm needs to re-check the current number being counted is indeed the majority number.

\begin{python}
def majorityElement(self, nums):
    """
    Algorithm:
    O(n lgn) sort and take the middle one
    O(n) Moore's Voting Algorithm
    """
    mjr = nums[0]
    cnt = 0
    for i, v in enumerate(nums):
        if mjr == v:
            cnt += 1
        else:
            cnt -= 1

        if cnt < 0:
            mjr = v
            cnt = 1

    return mjr

\end{python}
\subsubsection{$\frac{1}{3}$ of the Size}
Given an array of integers, the majority number is the number that occurs more than $\frac{1}{3}$ of the size of the array. This question can be generalized to be solved by $\frac{1}{k}$ case.

\subsubsection{$\frac{1}{k}$ of the Size}
Given an array of integers and a number k, the majority number is the number that occurs more than $\frac{1}{k}$ of the size of the array. In this case, we need to generalize the solution to $\frac{1}{2}$ majority number problem.
\newpag
\begin{python}

def majorityNumber(self, nums, k):
    """
    Since majority elements appears more
    than ceil(n/k) times, there are at
    most k-1 majority number
    """
    cnt = defaultdict(int)
    for num in nums:
        if num in cnt:
            cnt[num] += 1
        else:
            if len(cnt) < k-1:
                cnt[num] += 1
            else:
                for key in cnt.keys():
                    cnt[key] -= 1
                    if cnt[key] == 0: del cnt[key]

    # filter, double-check
    for key in cnt.keys():
        if (len(filter(lambda x: x == key, nums))
            > len(nums)/k):
            return key

    raise Exception
\end{python}

\section{Two Pointers}
\subsection{Interleaving}
\runinhead{Interleaving positive and negative numbers.} Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
\begin{lstlisting}
Input:
[-33, -19, 30, 26, 21, -9]
Output:
[-33, 30, -19, 26, -9, 21]
\end{lstlisting}
Core clues:
\begin{enumerate}
\item In 1-pass.
\item What (positive or negative) is expected for the current position.
\item Where is the next positive and negative element.
\end{enumerate}
\begin{python}
def rerange(self, A):
    n = len(A)
    pos_cnt = len(filter(lambda x: x > 0, A))
    pos_expt = True if pos_cnt*2 > n else False

    neg = 0  # next negative
    pos = 0  # next positive
    for i in xrange(n):
        while neg < n and A[neg] > 0: neg += 1
        while pos < n and A[pos] < 0: pos += 1
        if pos_expt:
            A[i], A[pos] = A[pos], A[i]
        else:
            A[i], A[neg] = A[neg], A[i]

        if i == neg: neg += 1
        if i == pos: pos += 1

        pos_expt = not pos_expt
\end{python}


\sec{The Method of Variation of Parameters}
\subsection{Introduction}
This is a powerful method to find a particular solution to a linear ODE when the general solution of the associated homogeneous equation is known.
\begin{mdframed}[style=boxstyle, frametitle={The Setup}]
	Consider the second order ODE
	\begin{equation} \label{eq:mvp}
		y'' + P(x)y' + Q(x)y = R(x).
	\end{equation}
	Assume that the general solution $y_g(x)$ of the associated homogeneous equation
	\begin{equation} \label{eq:mvphomo}
		y'' + P(x)y' + Q(x)y = 0
	\end{equation}
	is known.
\end{mdframed}
\begin{mdframed}[style=boxstyle2, frametitle={The Solution - derivation. (Can be ignored.)}]
	Let $y_1$ and $y_2$ be two linearly independent solutions of (\ref{eq:mvphomo}).\\
	We seek to find a particular solution $y_p(x)$ of (\ref{eq:mvp}) of the form
	\begin{equation} \label{eq:ypform}
		y_p(x) = v_1(x)y_1(x) + v_2(x)y_2(x).
	\end{equation}
	(It is our \emph{assumption - I} that we would have a solution of this form.)\\
	Now, we place the following restriction
	\begin{equation} \label{eq:mvpcond1}
		v_1'y_1 + v_2'y_2 \equiv 0.
	\end{equation}
	(This is \emph{assumption - II}.)\\
	Under this assumption, we get
	\begin{align*}
		y_p' &= v_1y_1' + v_2y_2',\\
		y_p'' &= v_1y_1'' + v_1'y_1' + v_2y_2''+ v_2'y_2'.
	\end{align*}
	Substituting this back into (\ref{eq:mvp}) and using the fact that $y_1$ and $y_2$ are solutions of (\ref{eq:mvphomo}) gives us that
	\begin{equation} \label{eq:mvpcond2}
		v_1'y_1' + v_2'y_2' = R(x).
	\end{equation}
	Now, note that the system of equations (\ref{eq:mvpcond1}) and (\ref{eq:mvpcond2}) can be uniquely solved for $v_1'$ and $v_2'$. (Since $\{y_1, y_2\}$ is linearly independent and hence, the Wronskian is always nonzero. Recall Corollary \ref{cor:wronskian}.)\\
	Integrate to obtain $v_1$ and $v_2$ and finally, a particular solution $y_p$ is given by (\ref{eq:ypform}). The general solution is, as usual, $y_g + y_p.$
\end{mdframed}
\begin{mdframed}[style=boxstyle, frametitle={The Solution}]
	Let $y_1$ and $y_2$ be two linearly independent solutions of (\ref{eq:mvphomo}).\\
	Solve the following system of equations for functions $v_1'(x)$ and $v_2'(x)$:
	\begin{align*}
		v_1'y_1 + v_2'y_2 &= 0,\\
		v_1'y_1' + v_2'y_2' &= R(x).
	\end{align*}
	Integrate $v_1'$ and $v_2'$ to get $v_1$ and $v_2.$ Then, a particular solution of (\ref{eq:mvp}) is:
	\begin{equation*}
		y_p(x) = v_1(x)y_1(x) + v_2(x)y_2(x).
	\end{equation*}
	The general solution of (\ref{eq:mvp}) is then $y_g + y_p.$
\end{mdframed}
\exercise{%
Solve $y'' + y = \tan x.$
}


\chapter{Bayesian statistics}
\label{chap:Bayesian statistics}

\section{Introduction}
Using the posterior distribution to summarize everything we know about a set of unknown variables is at the core of Bayesian statistics. In this chapter, we discuss this approach to statistics in more detail.

\section{Summarizing posterior distributions}
The posterior $p(\vec{\theta}|\mathcal{D})$ summarizes everything we know about the unknown quantities $\vec{\theta}$. In this section, we discuss some simple quantities that can be derived from a probability distribution, such as a posterior. These summary statistics are often easier to understand and visualize than the full joint.

\subsection{MAP estimation}
We can easily compute a \textbf{point estimate} of an unknown quantity by computing the posterior mean, median or mode. In Section \ref{sec:Bayesian-decision-theory}, we discuss how to use decision theory to choose between these methods. Typically the posterior mean or median is the most appropriate choice for a realvalued quantity, and the vector of posterior marginals is the best choice for a discrete quantity. However, the posterior mode, aka the MAP estimate, is the most popular choice because it reduces to an optimization problem, for which efficient algorithms often exist. Futhermore, MAP estimation can be interpreted in non-Bayesian terms, by thinking of the log prior as a regularizer (see Section TODO for more details).

Although this approach is computationally appealing, it is important to point out that there are various drawbacks to MAP estimation, which we briefly discuss below. This will provide motivation for the more thoroughly Bayesian approach which we will study later in this chapter(and elsewhere in this book).

\subsubsection{No measure of uncertainty}
The most obvious drawback of MAP estimation, and indeed of any other \emph{point estimate} such as the posterior mean or median, is that it does not provide any measure of uncertainty. In many applications, it is important to know how much one can trust a given estimate. We can derive such confidence measures from the posterior, as we discuss in Section \ref{sec:Credible-intervals}.

\subsubsection{Plugging in the MAP estimate can result in overfitting}
If we don’t model the uncertainty in our parameters, then our predictive distribution will be overconfident. Overconfidence in predictions is particularly problematic in situations where we may be risk averse; see Section \ref{sec:Bayesian-decision-theory} for details.

\subsubsection{The mode is an untypical point}
Choosing the mode as a summary of a posterior distribution is often a very poor choice, since the mode is usually quite untypical of the distribution, unlike the mean or median. The basic problem is that the mode is a point of measure zero, whereas the mean and median take the volume of the space into account. See Figure \ref{fig:untypical-point}.

\begin{figure}[hbtp]
\centering
\subfloat[]{\includegraphics[scale=.50]{untypical-point-a.png}} \\
\subfloat[]{\includegraphics[scale=.50]{untypical-point-b.png}}
\caption{(a) A bimodal distribution in which the mode is very untypical of the distribution. The thin blue vertical line is the mean, which is arguably a better summary of the distribution, since it is near the majority of the probability mass. (b) A skewed distribution in which the mode is quite different from the mean.}
\label{fig:untypical-point}
\end{figure}

How should we summarize a posterior if the mode is not a good choice? The answer is to use decision theory, which we discuss in Section \ref{sec:Bayesian-decision-theory}. The basic idea is to specify a loss function, where $L(\theta,\hat{\theta})$ is the loss you incur if the truth is $\theta$ and your estimate is $\hat{\theta}$. If we use 0-1 loss $L(\theta,\hat{\theta})=\mathbb{I}(\theta \neq \hat{\theta})$(see section \ref{sec:Loss-function-and-risk-function}), then the optimal estimate is the posterior mode. 0-1 loss means you only get “points” if you make no errors, otherwise you get nothing: there is no “partial credit” under this loss function! For continuous-valued quantities, we often prefer to use squared error loss, $L(\theta,\hat{\theta})=(\theta-\hat{\theta})^2$ ; the corresponding optimal estimator is then the posterior mean, as we show in Section \ref{sec:Bayesian-decision-theory}. Or we can use a more robust loss function, $L(\theta,\hat{\theta})=|\theta-\hat{\theta}|$, which gives rise to the posterior median.

\subsubsection{MAP estimation is not invariant to reparameterization *}
A more subtle problem with MAP estimation is that the result we get depends on how we parameterize the probability distribution. Changing from one representation to another equivalent representation changes the result, which is not very desirable, since the units of measurement are arbitrary (e.g., when measuring distance, we can use centimetres or inches).

To understand the problem, suppose we compute the posterior forx. If we define y=f(x), the distribution for yis given by Equation \ref{eqn:General-transformations}. The $\frac{\mathrm{d}x}{\mathrm{d}y}$ term is called the Jacobian, and it measures the change in size of a unit volume passed
through $f$. Let $\hat{x}=\arg\max_x p_x(x)$ be the MAP estimate for $x$. In general it is not the case that $\hat{x}=\arg\max_x p_x(x)$ is given by $f(\hat{x})$. For example, let $X \sim \mathcal{N}(6,1)$ and $y=f(x)$, where $f(x)=1/(1+\exp(-x+5))$.

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.70]{mode-reparameterization.png}
\caption{Example of the transformation of a density under a nonlinear transform. Note how the mode of the transformed distribution is not the transform of the original mode. Based on Exercise 1.4 of (Bishop 2006b).}
\label{fig:mode-reparameterization}
\end{figure}

We can derive the distribution of $y$ using Monte Carlo simulation (see Section \ref{sec:Monte-Carlo-approximation}). The result is shown in Figure \ref{sec:mode-reparameterization}. We see that the original Gaussian has become “squashed” by the sigmoid nonlinearity. In particular, we see that the mode of the transformed distribution is not equal to the transform of the original mode.

The MLE does not suffer from this since the likelihood is a function, not a probability density. Bayesian inference does not suffer from this problem either, since the change of measure is taken into account when integrating over the parameter space.

\subsection{Credible intervals}
\label{sec:Credible-intervals}
In addition to point estimates, we often want a measure of confidence. A standard measure of confidence in some (scalar) quantity $\theta$ is the “width” of its posterior distribution. This can be measured using a $100(1−\alpha)\%$ credible interval, which is a (contiguous) region $C=(\ell,u)$(standing for lower and upper) which contains $1−\alpha$ of the posterior probability mass, i.e.,
\begin{equation}
C_{\alpha}(\mathcal{D}) \quad \text{where } P(\ell \leq \theta \leq u)=1-\alpha
\end{equation}

There may be many such intervals, so we choose one such that there is $(1−\alpha)/2$ mass in each tail; this is called a \textbf{central interval}.

If the posterior has a known functional form, we can compute the posterior central interval using $\ell=F^{-1}(\alpha/2)$ and $u=F^{-1}(1-\alpha/2)$, where $F$ is the cdf of the posterior.

If we don’t know the functional form, but we can draw samples from the posterior, then we can use a Monte Carlo approximation to the posterior quantiles: we simply sort the $\mathcal{S}$ samples, and find the one that occurs at location $\alpha/\mathcal{S}$ along the sorted list. As $\mathcal{S} \rightarrow \infty$, this converges to the true quantile.

People often confuse Bayesian credible intervals with frequentist confidence intervals. However, they are not the same thing, as we discuss in Section TODO. In general, credible intervals are usually what people want to compute, but confidence intervals are usually what they actually compute, because most people are taught frequentist statistics but not Bayesian statistics. Fortunately, the mechanics of computing a credible interval is just as easy as computing a confidence interval.

\subsection{Inference for a difference in proportions}
Sometimes we have multiple parameters, and we are interested in computing the posterior distribution of some function of these parameters. For example, suppose you are about to buy something from Amazon.com, and there are two sellers offering it for the same price. Seller 1 has 90 positive reviews and 10 negative reviews. Seller 2 has 2 positive reviews and 0 negative reviews. Who should you buy from?\footnote{This example is from \url{http://www.johndcook.com/blog/2011/09/27/bayesian-amazon/}}.

On the face of it, you should pick seller 2, but we cannot be very confident that seller 2 is better since it has had so few reviews. In this section, we sketch a Bayesian analysis of this problem. Similar methodology can be used to compare rates or proportions across groups for a variety of other settings.

Let $\theta_1$ and $\theta_2$ be the unknown reliabilities of the two sellers. Since we don’t know much about them, we’ll endow them both with uniform priors, $\theta_i \sim \text{Beta}(1,1)$. The posteriors are $p(\theta_1|\mathcal{D}_1)=\text{Beta}(91,11)$ and $p(\theta_2|\mathcal{D}_2)=\text{Beta}(3,1)$.

We want to compute $p(\theta_1 >\theta_2|\mathcal{D})$. For convenience, let us define $\delta=\theta_1-\theta_2$ as the difference in the rates. (Alternatively we might want to work in terms of the log-odds ratio.) We can compute the desired quantity using numerical integration
\begin{equation}
\begin{split}
p(\delta>0|\mathcal{D}) & = \int_0^1\int_0^1 \mathbb{I}(\theta_1>\theta_2)\text{Beta}(\theta_1|91,11) \\
                        & \quad \text{Beta}(\theta_2|3,1)\mathrm{d}\theta_1\mathrm{d}\theta_2
\end{split}
\end{equation}

We find $p(\delta>0|\mathcal{D})=0.710$, which means you are better off buying from seller 1!

\section{Bayesian model selection}
\label{sec:Bayesian-model-selection}

In general, when faced with a set of models (i.e., families of parametric distributions) of different complexity, how should we choose the best one? This is called the \textbf{model selection} problem.

One approach is to use cross-validation to estimate the generalization error of all the candidate models, and then to pick the model that seems the best. However, this requires fitting each model $K$ times, where $K$ is the number of CV folds. A more efficient approach is to compute the posterior over models,
\begin{equation}
p(m|\mathcal{D})=\dfrac{p(\mathcal{D}|m)p(m)}{\sum_{m'}p(\mathcal{D}|m')p(m')}
\end{equation}

From this, we can easily compute the MAP model, $\hat{m}=\arg\max_m{p(m|\mathcal{D})}$. This is called \textbf{Bayesian model selection}.

If we use a uniform prior over models, this amounts to picking the model which maximizes
\begin{equation}\label{eqn:marginal-likelihood}
p(\mathcal{D}|m)=\int{p(\mathcal{D}|\vec{\theta})p(\vec{\theta}|m)}\mathrm{d}\vec{\theta}
\end{equation}

This quantity is called the \textbf{marginal likelihood}, the \textbf{integrated likelihood}, or the \textbf{evidence} for model $m$. The details on how to perform this integral will be discussed in Section \ref{sec:Computing-the-marginal-likelihood}. But first we give an intuitive interpretation of what this quantity means.

\subsection{Bayesian Occam's razor}
One might think that using $p(\mathcal{D}|m)$ to select models would always favour the model with the most parameters. This is true if we use $p(\mathcal{D}|\hat{\vec{\theta}}_m)$ to select models, where $\hat{\vec{\theta}}_m)$ is the MLE or MAP estimate of the parameters for model $m$, because models with more parameters will fit the data better, and hence achieve higher likelihood. However, if we integrate out the parameters, rather than maximizing them, we are automatically protected from overfitting: models with more parameters do not necessarily have higher \emph{marginal likelihood}. This is called the \textbf{Bayesian Occam’s razor} effect (MacKay 1995b; Murray and Ghahramani 2005), named after the principle known as \textbf{Occam’s razor}, which says one should pick the simplest model that adequately explains the data.

One way to understand the Bayesian Occam’s razor is to notice that the marginal likelihood can be rewritten as follows, based on the chain rule of probability (Equation \ref{eqn:product-rule}):
\begin{equation}\begin{split}
p(D) & =p((\vec{x}_1,y_1))p((\vec{x}_2,y_2)|(\vec{x}_1,y_1)) \\
     & \quad p((\vec{x}_3,y_3)|(\vec{x}_1,y_1):(\vec{x}_2,y_2))\cdots \\
	 & \quad p((\vec{x}_N,y_N)|(\vec{x}_1,y_1):(\vec{x}_{N-1},y_{N-1}))
\end{split}\end{equation}

This is similar to a leave-one-out cross-validation estimate (Section \ref{sec:Cross-validation}) of the likelihood, since we predict each future point given all the previous ones. (Of course, the order of the data does not matter in the above expression.) If a model is too complex, it will overfit the “early” examples and will then predict the remaining ones poorly.

Another way to understand the Bayesian Occam’s razor effect is to note that probabilities must sum to one. Hence $\sum_{p(\mathcal{D}')} p(m|\mathcal{D}')=1$, where the sum is over all possible data sets. Complex models, which can predict many things, must spread their probability mass thinly, and hence will not obtain as large a probability for any given data set as simpler models. This is sometimes called the \textbf{conservation of probability mass} principle, and is illustrated in Figure \ref{fig:Bayesian-Occams-razor}.

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.80]{Bayesian-Occams-razor.png}
\caption{A schematic illustration of the Bayesian Occam’s razor. The broad (green) curve corresponds to a complex model, the narrow (blue) curve to a simple model, and the middle (red) curve is just right. Based on Figure 3.13 of (Bishop 2006a). }
\label{fig:Bayesian-Occams-razor}
\end{figure}

When using the Bayesian approach, we are not restricted to evaluating the evidence at a finite grid of values. Instead, we can use numerical optimization to find $\lambda^*=\arg\max_{\lambda}p(\mathcal{D}|\lambda)$. This technique is called \textbf{empirical Bayes} or \textbf{type II maximum likelihood} (see Section \ref{sec:Empirical-Bayes} for details). An example is shown in Figure TODO(b): we see that the curve has a similar shape to the CV estimate, but it can be computed more efficiently.

\subsection{Computing the marginal likelihood (evidence)}
\label{sec:Computing-the-marginal-likelihood}
When discussing parameter inference for a fixed model, we often wrote
\begin{equation}
p(\vec{\theta}|\mathcal{D},m) \propto p(\vec{\theta}|m)p(\mathcal{D}|\vec{\theta},m)
\end{equation}
thus ignoring the normalization constant $p(\mathcal{D}|m)$. This is valid since $p(\mathcal{D}|m)$is constant wrt $\vec{\theta}$. However, when comparing models, we need to know how to compute the marginal likelihood, $p(\mathcal{D}|m)$. In general, this can be quite hard, since we have to integrate over all possible parameter values, but when we have a conjugate prior, it is easy to compute, as we now show.

Let $p(\vec{\theta})=q(\vec{\theta})/Z_0$ be our prior, where $q(\vec{\theta})$ is an unnormalized distribution, and $Z_0$ is the normalization constant of the prior. Let $p(\mathcal{D}|\vec{\theta})=q(\mathcal{D}|\vec{\theta})/Z_{\ell}$ be the likelihood, where $Z_{\ell}$ contains any constant factors in the likelihood. Finally let $p(\vec{\theta}|\mathcal{D})=q(\vec{\theta}|\mathcal{D})/Z_N$ be our posterior , where $q(\vec{\theta}|\mathcal{D})=q(\mathcal{D}|\vec{\theta})q(\vec{\theta})$ is the unnormalized posterior, and $Z_N$ is the normalization constant of the posterior. We have
\begin{align}
p(\vec{\theta}|\mathcal{D})& =\dfrac{p(\mathcal{D}|\vec{\theta})p(\vec{\theta})}{p(\mathcal{D})} \\
\dfrac{q(\vec{\theta}|\mathcal{D})}{Z_N}& =\dfrac{q(\mathcal{D}|\vec{\theta})q(\vec{\theta})}{Z_{\ell}Z_0p(\mathcal{D})} \\
p(\mathcal{D})& = \dfrac{Z_N}{Z_0Z_{\ell}}
\end{align}

So assuming the relevant normalization constants are tractable, we have an easy way to compute the marginal likelihood. We give some examples below.

\subsubsection{Beta-binomial model}
Let us apply the above result to the Beta-binomial model. Since we know $p(\vec{\theta}|\mathcal{D})=\mathrm{Beta}(\vec{\theta}|a',b')$, where $a'=a+N_1$, $b'=b+N_0$, we know the normalization constant of the posterior is $B(a',b')$. Hence
\begin{align}
p(\theta|\mathcal{D})& =\dfrac{p(\mathcal{D}|\theta)p(\theta)}{p(\mathcal{D})} \\
    & =\dfrac{1}{p(\mathcal{D})}\left[\dfrac{1}{B(a,b)}\theta^{a-1}(1-\theta)^{b-1}\right] \nonumber \\
	& \quad \left[\dbinom{N}{N_1}\theta^{N_1}(1-\theta)^{N_0}\right] \\
	& =\dbinom{N}{N_1}\dfrac{1}{p(\mathcal{D})}\dfrac{1}{B(a,b)}\left[\theta^{a+N_1-1}(1-\theta)^{b+N_0-1}\right]
\end{align}

So
\begin{align}
\dfrac{1}{B(a+N_1,b+N_0)} & = \dbinom{N}{N_1}\dfrac{1}{p(\mathcal{D})}\dfrac{1}{B(a,b)} \\
p(\mathcal{D}) & = \dbinom{N}{N_1}\dfrac{B(a+N_1,b+N_0)}{B(a,b)}
\end{align}

The marginal likelihood for the Beta-Bernoulli model is the same as above, except it is missingthe $\binom{N}{N_1}$ term.

\subsubsection{Dirichlet-multinoulli model}
By the same reasoning as the Beta-Bernoulli case, one can show that the marginal likelihood for the Dirichlet-multinoulli model is given by
\begin{align}
p(\mathcal{D}) & =\dfrac{B(\vec{N}+\vec{\alpha})}{B(\vec{\alpha})} \\
   & = \dfrac{\Gamma(\sum_k \alpha_k)}{\Gamma(N+\sum_k \alpha_k)}\prod\limits_k \dfrac{\Gamma(N_k+\alpha_k)}{\Gamma(\alpha_k)}
\end{align}

\subsubsection{Gaussian-Gaussian-Wishart model}
Consider the case of an MVN with a conjugate NIW prior. Let $Z_0$ be the normalizer for the prior, $Z_N$ be normalizer for the posterior, and let $Z_{\ell}(2\pi)^{ND/2}=$ be the normalizer for the likelihood. Then it is easy to see that
\begin{align}
p(\mathcal{D})& =\dfrac{Z_N}{Z_0Z_{\ell}} \\
   & = \dfrac{1}{(2\pi)^{ND/2}}\dfrac{\left(\frac{2\pi}{\kappa_N}\right)^{D/2}|\vec{S}_N|^{-\nu_N/2}2^{(\nu_0+N)D/2}\Gamma_D(\nu_N/2)}{\left(\frac{2\pi}{\kappa_0}\right)^{D/2}|\vec{S}_0|^{-\nu_0/2}2^{\nu_0D/2}\Gamma_D(\nu_0/2)} \\
   & = \dfrac{1}{\pi^{ND/2}}\left(\dfrac{\kappa_0}{\kappa_N}\right)^{D/2}\dfrac{|\vec{S}_0|^{\nu_0/2}}{|\vec{S}_N|^{\nu_N/2}}\dfrac{\Gamma_D(\nu_N/2)}{\Gamma_D(\nu_0/2)}
\end{align}

\subsubsection{BIC approximation to log marginal likelihood}
In general, computing the integral in Equation \ref{eqn:marginal-likelihood} can be quite difficult. One simple but popular approximation is known as the \textbf{Bayesian information criterion} or \textbf{BIC}, which has the following form (Schwarz 1978):
\begin{equation}
\mathrm{BIC} \triangleq \log p(\mathcal{D}|\hat{\vec{\theta}})-\dfrac{\mathrm{dof}(\hat{\vec{\theta}})}{2}\log{N}
\end{equation}
where $\mathrm{dof}(\hat{\vec{\theta}})$ is the number of \textbf{degrees of freedom} in the model, and $\hat{\vec{\theta}}$ is the MLE for the model. We see that this has the form of a \textbf{penalized log likelihood}, where the penalty term depends on the model’s complexity. See Section TODO for the derivation of the BIC score.

As an example, consider linear regression. As we show in Section TODO, the MLE is given by $\hat{\vec{w}}=(\vec{X}^T\vec{X})^{-1}\vec{X}^T\vec{y}$ and $\sigma^2=\frac{1}{N}\sum_{i=1}^N (y_i-\hat{\vec{w}}^T\vec{x}_i)$. The corresponding log likelihood is given by
\begin{equation}
\log p(\mathcal{D}|\hat{\vec{\theta}}) = -\dfrac{N}{2}\log(2\pi\hat{\sigma}^2)-\dfrac{N}{2}
\end{equation}

Hence the BIC score is as follows (dropping constant terms)
\begin{equation}
\mathrm{BIC}=-\dfrac{N}{2}\log(\hat{\sigma}^2)-\dfrac{D}{2}\log{N}
\end{equation}
where $D$ is the number of variables in the model. In the statistics literature, it is common to use an alternative definition of BIC, which we call the BIC \emph{cost}(since we want to minimize it):
\begin{equation}
\mathrm{BIC\text{-}cost} \triangleq -2\log p(\mathcal{D}|\hat{\vec{\theta}})-\mathrm{dof}(\hat{\vec{\theta}})\log{N} \approx -2\log{p(\mathcal{D})}
\end{equation}

In the context of linear regression, this becomes
\begin{equation}
\mathrm{BIC\text{-}cost} = N\log(\hat{\sigma}^2)+D\log{N}
\end{equation}

The BIC method is very closely related to the \textbf{minimum description length} or \textbf{MDL} principle, which characterizes the score for a model in terms of how well it fits the data, minus how complex the model is to define. See (Hansen and Yu 2001) for details.

There is a very similar expression to BIC/ MDL called the \textbf{Akaike information criterion} or \textbf{AIC}, defined as
\begin{equation}
\mathrm{AIC}(m,\mathcal{D}) = \log{p(\mathcal{D}|\hat{\vec{\theta}}_{MLE})}-\mathrm{dof}(m)
\end{equation}

This is derived from a frequentist framework, and cannot be interpreted as an approximation to the marginal likelihood. Nevertheless, the form of this expression is very similar to BIC. We see that the penalty for AIC is less than for BIC. This causes AIC to pick more complex models. However, this can result in better predictive accuracy. See e.g., (Clarke et al. 2009, sec 10.2) for further discussion on such information criteria.

\subsubsection{Effect of the prior}
Sometimes it is not clear how to set the prior. When we are performing posterior inference, the details of the prior may not matter too much, since the likelihood often overwhelms the prior anyway. But when computing the marginal likelihood, the prior plays a much more important role, since we are averaging the likelihood over all possible parameter settings, as weighted by the prior.

If the prior is unknown, the correct Bayesian procedure is to put a prior on the prior. If the prior is unknown, the correct Bayesian procedure is to put a prior on the prior.

\subsection{Bayes factors}
Suppose our prior on models is uniform, $p(m) \propto 1$. Then model selection is equivalent to picking the model with the highest marginal likelihood. Now suppose we just have two models we are considering, call them the \textbf{null hypothesis}, $M_0$, and the \textbf{alternative hypothesis}, $M_1$. Define the \textbf{Bayes factor} as the ratio of marginal likelihoods:
\begin{equation}
\mathrm{BF}_{1,0} \triangleq \dfrac{p(\mathcal{D}|M_1)}{p(\mathcal{D}|M_0)}=\dfrac{p(M_1|\mathcal{D})}{p(M_2|\mathcal{D})}/\dfrac{p(M_1)}{p(M_0)}
\end{equation}

\section{Priors}
The most controversial aspect of Bayesian statistics is its reliance on priors. Bayesians argue this is unavoidable, since nobody is a \textbf{tabula rasa} or \textbf{blank slate}: all inference must be done conditional on certain assumptions about the world. Nevertheless, one might be interested in minimizing the impact of one’s prior assumptions. We briefly discuss some ways to do this below.

\subsection{Uninformative priors}
If we don’t have strong beliefs about what $\theta$ should be, it is common to use an \textbf{uninformative} or \textbf{non-informative} prior, and to “let the data speak for itself”.

\subsection{Robust priors}
In many cases, we are not very confident in our prior, so we want to make sure it does not have an undue influence on the result. This can be done by using \textbf{robust priors}(Insua and Ruggeri 2000), which typically have heavy tails, which avoids forcing things to be too close to the prior mean.

\subsection{Mixtures of conjugate priors}
Robust priors are useful, but can be computationally expensive to use. Conjugate priors simplify the computation, but are often not robust, and not flexible enough to encode our prior knowledge. However, it turns out that a \textbf{mixture of conjugate priors} is also conjugate, and can approximate any kind of prior (Dallal and Hall 1983; Diaconis and Ylvisaker 1985). Thus such priors provide a good compromise between computational convenience and flexibility.

\section{Hierarchical Bayes}
A key requirement for computing the posterior $p(\vec{\theta}|\mathcal{D})$ is the specification of a prior $p(\vec{\theta}|\vec{\eta})$, where $\vec{\eta}$ are the hyper-parameters. What if we don’t know how to set $\vec{\eta}$? In some cases, we can use uninformative priors, we we discussed above. A more Bayesian approach is to put a prior on our priors! In terms of graphical models (Chapter TODO), we can represent the situation as follows:
\begin{equation}
\vec{\eta} \rightarrow \vec{\theta} \rightarrow \mathcal{D}
\end{equation}

This is an example of a \textbf{hierarchical Bayesian model}, also called a \textbf{multi-level} model, since there are multiple levels of unknown quantities.

\section{Empirical Bayes}
\label{sec:Empirical-Bayes}

\begin{table}
\normalsize
\centering
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Method} & \textbf{Definition} \\
\noalign{\smallskip}\svhline\noalign{\smallskip}
Maximum likelihood & $\hat{\vec{\theta}}=\arg\max_{\vec{\theta}} p(\mathcal{D}|\vec{\theta})$ \\
MAP estimation & $\hat{\vec{\theta}}=\arg\max_{\vec{\theta}} p(\mathcal{D}|\vec{\theta})p(\vec{\theta}|\vec{\eta})$ \\
\multirow{2}{*}{ML-II (\textbf{Empirical Bayes})} & $\hat{\vec{\eta}}=\arg\max_{\vec{\eta}} \int p(\mathcal{D}|\vec{\theta})p(\vec{\theta}|\vec{\eta})\mathrm{d}\vec{\theta}$ \\
                                                  & $\quad =\arg\max_{\vec{\eta}}p(\mathcal{D}|\vec{\eta})$ \\
\multirow{2}{*}{MAP-II} & $\hat{\vec{\eta}}=\arg\max_{\vec{\eta}} \int p(\mathcal{D}|\vec{\theta})p(\vec{\theta}|\vec{\eta})p(\vec{\eta})\mathrm{d}\vec{\theta}$ \\
                        & $\quad =\arg\max_{\vec{\eta}}p(\mathcal{D}|\vec{\eta})p(\vec{\eta})$ \\
Full Bayes & $p(\vec{\theta},\vec{\eta}|\mathcal{D}) \propto p(\mathcal{D}|\vec{\theta})p(\vec{\theta}|\vec{\eta})$ \\
\noalign{\smallskip}\hline
\end{tabular}
\end{table}

\section{Bayesian decision theory}
\label{sec:Bayesian-decision-theory}
We have seen how probability theory can be used to represent and updates our beliefs about the state of the world. However, ultimately our goal is to convert our beliefs into actions. In this section, we discuss the optimal way to do this.

Our goal is to devise a \textbf{decision procedure} or \textbf{policy}, $f(\vec{x}) : \mathcal{X} \rightarrow \mathcal{Y}$, which minimizes the \textbf{expected loss} $R_{\mathrm{exp}}(f)$(see Equation \ref{eqn:expected-loss}).

In the Bayesian approach to decision theory, the optimal output, having observed $\vec{x}$, is defined as the output $a$ that minimizes the \textbf{posterior expected loss}:
\begin{equation}
\rho(f)=\mathbb{E}_{p(y|\vec{x})}[L(y,f(\vec{x}))]=\begin{cases}
\sum\limits_y L[y,f(\vec{x})]p(y|\vec{x}) \\
\int\limits_y L[y,f(\vec{x})]p(y|\vec{x})\mathrm{d}y
\end{cases}
\end{equation}

Hence the \textbf{Bayes estimator}, also called the \textbf{Bayes decision rule}, is given by
\begin{equation}
\delta(\vec{x})=\arg\min\limits_{f \in \mathcal{H}} \rho(f)
\end{equation}

\subsection{Bayes estimators for common loss functions}

\subsubsection{MAP estimate minimizes 0-1 loss}
When $L(y,f(x))$ is \textbf{0-1 loss}(Section \ref{sec:Loss-function-and-risk-function}), we can proof that MAP estimate minimizes 0-1 loss,
\begin{align*}
\arg\min\limits_{f \in \mathcal{H}} \rho(f)& =\arg\min\limits_{f \in \mathcal{H}} \sum\limits_{i=1}^K{L[C_k,f(\vec{x})]p(C_k|\vec{x})} \\
         & =\arg\min\limits_{f \in \mathcal{H}} \sum\limits_{i=1}^K{\mathbb{I}(f(\vec{x}) \neq C_k)p(C_k|\vec{x})} \\
		 & =\arg\min\limits_{f \in \mathcal{H}} \sum\limits_{i=1}^K{p(f(\vec{x}) \neq C_k|\vec{x})} \\
		 & =\arg\min\limits_{f \in \mathcal{H}} \left[1-{p(f(\vec{x}) = C_k|\vec{x})}\right] \\
		 & =\arg\max\limits_{f \in \mathcal{H}} p(f(\vec{x}) = C_k|\vec{x})
\end{align*}

\subsubsection{Posterior mean minimizes	$\ell_2$(quadratic) loss}
For continuous parameters, a more appropriate loss function is \textbf{squared error}, \textbf{$\ell_2$ loss}, or \textbf{quadratic loss}, defined as $L(y,f(\vec{x}))=\left[y-f(\vec{x})\right]^2$.

The posterior expected loss is given by
\begin{equation}\begin{split}
\rho(f) & =\int\limits_y L[y,f(\vec{x})]p(y|\vec{x})\mathrm{d}y \\
        & =\int\limits_y \left[y-f(\vec{x})\right]^2p(y|\vec{x})\mathrm{d}y \\
        & =\int\limits_y \left[y^2-2yf(\vec{x})+f(\vec{x})^2\right]p(y|\vec{x})\mathrm{d}y
\end{split}\end{equation}

Hence the optimal estimate is the posterior mean:
\begin{align}
& \dfrac{\partial \rho}{\partial f} =\int\limits_y [-2y+2f(\vec{x})]p(y|\vec{x})\mathrm{d}y=0 \Rightarrow \nonumber \\
& \int\limits_y f(\vec{x})p(y|\vec{x})\mathrm{d}y = \int\limits_y yp(y|\vec{x})\mathrm{d}y \nonumber \\
& f(\vec{x}) \int\limits_y p(y|\vec{x})\mathrm{d}y = \mathbb{E}_{p(y|\vec{x})}[y] \nonumber \\
& f(\vec{x}) = \mathbb{E}_{p(y|\vec{x})}[y]
\end{align}

This is often called the \textbf{minimum mean squared error} estimate or \textbf{MMSE} estimate.

\subsubsection{Posterior median minimizes $\ell_1$(absolute) loss}
The $\ell_2$ loss penalizes deviations from the truth quadratically, and thus is sensitive to outliers. A more robust alternative is the absolute or $\ell_1$ loss. The optimal estimate is the posterior median, i.e., a value $a$ such that $P(y<a|\vec{x})=P(y \geq a|\vec{x})=0.5$.

\begin{proof}
\begin{align*}
\rho(f)& =\int\limits_y L[y,f(\vec{x})]p(y|\vec{x})\mathrm{d}y=\int\limits_y |y-f(\vec{x})|p(y|\vec{x})\mathrm{d}y \\
       & =\int\limits_y [f(\vec{x})-y]p(y<f(\vec{x})|\vec{x})+ \\
	   & \quad [y-f(\vec{x})]p(y \geq f(\vec{x})|\vec{x})\mathrm{d}y \\
& \dfrac{\partial \rho}{\partial f}=\int\limits_y \left[p(y<f(\vec{x})|\vec{x})-p(y \geq f(\vec{x})|\vec{x})\right]\mathrm{d}y=0 \Rightarrow \\
& p(y<f(\vec{x})|\vec{x})=p(y \geq f(\vec{x})|\vec{x})=0.5 \\
& \therefore f(\vec{x})=\text{median}
\end{align*}
\end{proof}

\subsubsection{Reject option}
In classification problems where $p(y|\vec{x})$ is very uncertain, we may prefer to choose a reject action, in which we refuse to classify the example as any of the specified classes, and instead say “don’t know”. Such ambiguous cases can be handled by e.g., a human expert. This is useful in \textbf{risk averse} domains such as medicine and finance.

We can formalize the reject option as follows. Let choosing $f(\vec{x})=c_{K+1}$ correspond to picking the reject action, and choosing $f(\vec{x}) \in \{C_1,...,C_k\}$ correspond to picking one of the classes. Suppose we define the loss function as
\begin{equation}
L(f(\vec{x}), y)=\begin{cases}
0 & \text{if } f(\vec{x})=y \text{ and } f(\vec{x}),y \in \{C_1,...,C_k\} \\
\lambda_s & \text{if } f(\vec{x}) \neq y \text{ and } f(\vec{x}),y \in \{C_1,...,C_k\} \\
\lambda_r & \text{if } f(\vec{x})=C_{K+1}
\end{cases}
\end{equation}
where $\lambda_s$ is the cost of a substitution error, and $\lambda_r$ is the cost of the reject action.

\subsubsection{Supervised learning}
We can define the loss incurred by $f(\vec{x})$ (i.e., using this predictor) when the unknown state of nature is $\vec{\theta}$(the parameters of the data generating mechanism) as follows:
\begin{equation}
L(\vec{\theta},f) \triangleq \mathbb{E}_{p(\vec{x},y|\vec{\theta})}[\ell(y-f(\vec{x}))]
\end{equation}

This is known as the \textbf{generalization error}. Our goal is to minimize the posterior expected loss, given by
\begin{equation}
\rho(f|\mathcal{D}) = \int{p(\vec{\theta}|\mathcal{D})L(\vec{\theta},f)}\mathrm{d}\vec{\theta}
\end{equation}

This should be contrasted with the frequentist risk which is defined in Equation TODO.

\subsection{The false positive vs false negative tradeoff}
In this section, we focus on binary decision problems, such as hypothesis testing, two-class classification, object/ event detection, etc. There are two types of error we can make: a \textbf{false positive}(aka \textbf{false alarm}), or a \textbf{false negative}(aka \textbf{missed detection}). The 0-1 loss treats these two kinds of errors equivalently. However, we can consider the following more general loss matrix:

TODO


\section{Exercises in Expectations of Random Variables}\label{S:xsExpectationsOfRVs} %S:xsMultivariateRVs
\begin{ExerciseList}
\Exercise
Let $X$ be the number of air
  conditioners a  store  sells each day, and assume that $X$ has
  probability mass function $f(10)=0.1$, $f(11)=0.3$,
  $f(12)=0.4$, $f(13)=0.2$.
\be
\item  Find the expected number of conditioners that the store sells
  each day.
\item If the profit per conditioner is $\$55$, what is the expected daily profit?
\ee
\Answer
\be
\item
The expected number of conditioners that the store sells daily  is
\ba{\E(X)&=\;\sum_{i=1}^{n}x_ip_{x_i}\\[6pt]
&=(10\times0.1+11\times0.3+12\times0.4+13\times0.2)\\[6pt]
&=1+3.3+4.8+2.6\\[6pt]
&=11.7\enspace . }
\item
The profit per conditioner is $\$55$, and so the expected daily profit given by
\[E( 55 \,X)\;=\; 55 E(X) \;=\; 55 \times 11.7 \;=\; 643.50\, ,\]
is $\$643.50$.
\ee

\Exercise
A small petrol station is supplied with fuel  every Saturday
  afternoon. Assume that its volume of sales $X$, in ten thousands of
  litres, has density
$$f(x)\;=\;\begin{cases}6x(1-x)&0\leq x \leq
  1\\0&\textrm{otherwise}\end{cases}\,.$$
Determine the mean and  variance of $X$.
\Answer
The expected value $\E(X)$ is
\ba{\E(X)&=\int^1_0 6x(1-x)x\,dx\\
&=\;\int^1_0 (6x^2-6x^3)\,dx\\[3pt]
&=\;\left. 2x^3-\frac{6}{4}x^4\right]^1_0\\
&=\;2(1^3-0)-\frac{6}{4}(1^4-0)\\
&=\;0.5}

\ba{\E(X^2)&=\int^1_0 6x(1-x)x^2\,dx\\[3pt]
&=\;\int^1_0 6x^3-6x^4\,dx\\[3pt]
&=\;\left.\frac{6}{4}x^4-\frac{6}{5}x^5\right]^1_0\\
&=\;\frac{6}{4}(1^4-0)-\frac{6}{5}(1^5-0)\\
&=0.3
}
the variance is $\V(X)\;=\;\E(X^2)-(\E(X))^2\;=\;0.3-0.5^2\;=\;0.05$

\Exercise
Starting from the definition of the variance of a random variable (Definition~\ref{D:VarianceofX}) show that
\[\V(X) = \E(X^2) - \left(\E(X)\right)^2 \enspace .\]
\Answer
This was already done in Sec.~\ref{S:PropOfEs} on Properties of Expectation. Make sure you understand each step.

\Exercise
Show that $V(aX+b) = a^2 V(X)$ for constants $a$ and $b$ and a random variable $X$.
\Answer
Using the definition of variance, expectations and by completing the square, we get:
\begin{multline*}
V(aX+b) = E ((aX+b)^2) - (E(aX+b))^2 = E \left( (aX)^2 + 2aXb + b^2 \right) - \left( aE(X)+b\right)^2 \\
= a^2 E \left(X^2\right) + 2ab E(X) + b^2 - a^2(E(X))^2 - 2abE(X) - b^2
= a^2 \left( E \left(X^2\right) - (E(X))^2 \right) = a^2 V(X) \, .
\end{multline*}

\Exercise
{**}Let $X$ be a discrete random variable with PMF given by
\[
f(x) =
\begin{cases}
\frac{x}{10} & \text{ if } x \in \{1,2,3,4\} ,\\
0 & \text{ otherwise.}
\end{cases}
\]
\begin{itemize}
\item[(a)] Find:
\begin{itemize}
\item[(i)] $\P(X=0)$
\item[(ii)] $\P(2.5 < X < 5)$
\item[(iii)] $\E(X)$
\item[(iv)] $\V(X)$
\end{itemize}
\item[(b)] Write down the DF (or CDF) of $X$.
\item[(c)] Plot the PMF and CDF of $X$.
\end{itemize}

\Exercise
Find the mean and the variance of the following  random variables.
\be
\item $X$ a discrete uniform random variable on $\{1,2,3,4,5,6\}$, i.e., \textit{the number a fair die turns up}.
 \item $X$ is a $\uniform(0,8)$ random variable, i.e., \textit{a continuous uniform random variable from the interval} $[0,8]$.

\item $X$ has a density function
$$
f(x)\,=\,
\begin{cases}
2e^{-2x} & \text{ if }  x \geq 0\\
0 & \text{ otherwise} \, .
\end{cases}
$$
\ee
\Answer
\be
\item
The probability mass function of $X$ is
$$f(x)=\begin{cases}\frac{1}{6}&x=1\\
\frac{1}{6}&x=2\\
\frac{1}{6}&x=3\\
\frac{1}{6}&x=4\\
\frac{1}{6}&x=5\\
\frac{1}{6}&x=6
\end{cases}$$
and so
$$\E(X)\;=\;\sum_{i=1}^6
x_if(x_i)\;=\;\frac{1}{6}+\frac{2}{6}+\frac{3}{6}+\frac{4}{6}+\frac{5}{6}+\frac{6}{6}\;=\;3.5\,.$$
Now,
$$\E(X^2)\;=\;\sum_{i=1}^6 x^2_if(x_i)\;=\;\frac{1}{6}+\frac{4}{6}+\frac{9}{6}+\frac{16}{6}+\frac{25}{6}+\frac{36}{6}\;=\;\frac{91}{6}\;=\;15.1667$$
and so  the variance is
\[V(X)\; =\; \E(X^2) - (\E(X))^2 \;=\; 15.1667-3.5^2\;=\;2.9167\,.\]

\item The density function of the uniform distribution, $X$,  on $[0,8]$ is  $$f(x)=\begin{cases}\frac{1}{8}&0<x<8\\0&\textrm{otherwise}\end{cases}$$
Therefore, $$\E(X)\;=\;\frac{8+0}{2}\;=\;4\enspace,$$
and $$\V(X)\;=\;\frac{(8-0)^2}{12}\;=\;\frac{16}{3}$$

\item Since $f(x)$ is the density function of an $\exponential(\lambda)$ random variable with parameter $\lambda=2$, we can use earlier results to get
$$\E(X)\;=\;\frac{1}{2}\quad \text{and}\quad
\V(X)\;=\;\frac{1}{4}\enspace.$$

(You should also be able to do this by integration!)
\ee

\end{ExerciseList}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS622: Theory of Formal Languages
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 3}

Consider the \textit{dfa} $\mathcal{M} = \left( \{a,b\}, \{q_0,q_1,q_2\}, \delta, q_0, \{q_1\} \right) $ whose graph is given in Figure \ref{fig:DR4}. Determine the language accepted by the automaton $\mathcal{M}$.

\begin{figure}[H]\centering
	\begin{tikzpicture}[->, >=stealth', shorten >=1pt, auto, node distance=3cm, semithick]
		\tikzstyle{final}=[circle,thick,draw=black,fill=gray!40,text=black]
		\node[state, initial]	(0) 	  				{$q_0$};
		\node[state] 			(2)	[above right of=0]	  	{$q_2$};
		\node[state, final]   (1) [below right of=2] 	  	{$q_1$};
		\path
			(0) edge [bend left]	node {b} (2)
				edge [bend right]	node {a} (1)
			(1) edge [loop right] 	node {b} (1)
				edge [bend left]  	node {a} (2)
			(2) edge [bend left]  	node {b} (1)
				edge [loop above] 	node {a} (2);
	\end{tikzpicture}
	\caption{Graph of the Deterministic Finite Automaton $\mathcal{M}$}
	\label{fig:DR4}
\end{figure}

\subsection*{Solution}

By definition, the language accepted by the given Deterministic Finite Automaton (\textit{dfa}) is the set $ L \left( \mathcal{M} \right) = \{ x \in \{a\}^*\{b\}^* | \delta ^* \left( q_0,x \right) \in \{q1\} \}$.
The final state $q_1$ can be reached from initial state $q_0$ either directly or through $q_2$.
Final state can be reached directly from state $q_0$ using the symbol $a$ or indirectly by state $q_1$ using words of the form $b\{a\}^*b$.
Once $q_1$ is reached, we can stay in $q_1$ by any arbitrary number of $b$ and/or any arbitrary number of loops of the form $a\{a\}^*b$.
Therefore, the language $L(\mathcal{M})$ accepted by the \textit{dfa} $\mathcal{M}$ would be

\begin{equation}
	L(\mathcal{M}) = \{a \cup b\{a\}^*b\}\{b\}^*\{a\{a\}^*b\}^*\{b\}^*
\end{equation}


%!TEX root = ceres-solver.tex
\chapter{Non-linear Least Squares}
\label{chapter:tutorial:nonlinsq}
Let $x \in \reals^n$ be an $n$-dimensional vector of variables, and
$F(x) = \left[f_1(x); \hdots ; f_k(x)\right]$ be a vector of residuals $f_i(x)$.
The function $f_i(x)$ can be a scalar or a vector valued
function.  Then,
\begin{equation}
	\arg \min_x \frac{1}{2} \sum_{i=1}^k \|f_i(x)\|^2.
\end{equation}
is a Non-linear least squares problem~\footnote{Ceres can solve a more general version of this problem, but for pedagogical reasons, we will restrict ourselves to this class of problems for now. See section~\ref{chapter:overview} for a full description of the problems that Ceres can solve}. Here $\|\cdot\|$ denotes the Euclidean norm of a vector.

Such optimization problems arise in almost every area of science and engineering. Whenever there is data to be analyzed, curves to be fitted, there is usually a linear or a non-linear least squares problem lurking in there somewhere.

Perhaps the simplest example of such a problem is the problem of Ordinary Linear Regression, where given observations $(x_1,y_1),\hdots, (x_k,y_k)$, we wish to find the line $y = mx + c$, that best explains $y$ as a function of $x$. One way to solve this problem is to find the solution to the following optimization problem
\begin{equation}
		\arg\min_{m,c} \sum_{i=1}^k (y_i - m x_i - c)^2.
\end{equation}
With a little bit of calculus, this problem can be solved easily by hand. But what if, instead of a line we were interested in a more complicated relationship between $x$ and $y$, say for example $y = e^{mx + c}$. Then the optimization problem becomes
\begin{equation}
		\arg\min_{m,c} \sum_{i=1}^k \left(y_i - e^{m x_i + c}\right)^2.
\end{equation}
This is a  non-linear regression problem and solving it by hand is much more tedious.  Ceres is designed to help you model and solve problems like this easily and efficiently.


\section{Linear Maps and Matrices}
The set of linear maps from $V$ to $W$ is a vector space over the same field $F$.
\begin{definition}
    Let $V,W$ be vector spaces over $F$.
    We define
    $$L(V,W)=\{\alpha:V\to W\text{, linear}\}$$
    to be the vector space of linear maps from $V$ to $W$ under the operations $(\alpha+\beta)(v)=\alpha(v)+\beta(v)$ and $(\lambda\alpha)(v)=\lambda\alpha(v)$.
\end{definition}
It is easy to verify that this is indeed a vector space.
There is a very important theorem
\begin{proposition}\label{linear_map_matrices}
    If $V,W$ are finite dimensional, so is $L(V,W)$ and we have $\dim L(V,W)=(\dim V)(\dim W)$.
\end{proposition}
\begin{definition}
    An $m\times n$ matrix over $F$ is an array with $m$ rows and $n$ columns with entries in $F$.
    As a convention, for a matrix $A$ we write $A=(a_{ij})_{1\le i\le m,1\le j\le n}=(A_{ij})_{1\le i\le m,1\le j\le n}$ where $i$ refers to the row number and $j$ the column number.
    We write $M_{m,n}(F)$ to denote the set of $m\times n$ matrices over $F$.
\end{definition}
Note that we can (and often) identify vectors in $\mathbb R^m$ as $m\times 1$ matrices.
\begin{proposition}
    $M_{m,n}(F)$ is a vector space over $F$ under $(a_{ij})+(b_{ij})=(a_{ij}+b_{ij})$ and $\lambda(a_{ij})=(\lambda a_{ij})$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
\begin{proposition}
    $\dim M_{m,n}(F)=mn$.
\end{proposition}
\begin{proof}
    The set $\{(\delta_{ia}\delta_{jb}):1\le a\le m,1\le b\le n\}$ with size $mn$ is a basis of $M_{m,n}(F)$.
\end{proof}
We want to represent linear maps by matrices.
For $V,W$ vector spaces over $F$ of finite dimensions $n,m$, we choose ordered bases $B=(v_1,\ldots,v_n),C=(w_1,\ldots,w_m)$ for $V,W$ respectively.
For $v\in V$, its coordinate under $B$ is $[v]_B=(v_1,\ldots,v_n)$ where $v=\sum_{i}v_ib_i$ is the unique decomposition of $v$ in this basis.
Similarly, the coordinate of $w\in W$ under $C$ is $[w]_C=(w_1,\ldots,w_m)$ where $w=\sum_{i}w_ic_i$ is the unique decomposition of $w$ under $C$.
\begin{definition}
    The matrix of $\alpha:V\to W$ in the bases $B,C$ is the matrix
    $$([\alpha]_{B,C})_{ij}=(\alpha(v_j))_i\in M_{m\times n}(F)$$
\end{definition}
If the bases are understood, we often write $([\alpha]_{B,C})_{ij}$ as $\alpha_{ij}$.
\begin{definition}
    For matrices $M\in M_{m\times n}(F)$ and $N\in M_{n\times l}(F)$, their matrix product is the $m\times l$ matrix defined by $(MN)_{ij}=(\sum_kM_{ik}N_{kj})_{ij}$.
\end{definition}
The particular case where $N$ is a column vector exhibits how $m\times n$ matrices induce linear maps $F^n\to F^m$.
\begin{lemma}
    For any $v\in V$,
    $$[\alpha(v)]_C=[\alpha]_{B,C}[v]_B$$
\end{lemma}
\begin{proof}
    Linearity.
\end{proof}
We already know that composition of linear maps is linear, what's more is
\begin{lemma}
    Let $U,V,W$ be finite dimensional vector spaces over $F$ with chosen bases $A,B,C$ respectively.
    Let $\beta:U\to V$ and $\alpha:V\to W$ be linear maps, then
    $$[\alpha\circ\beta]_{A,C}=[\alpha]_{B,C}[\beta]_{A,B}$$
\end{lemma}
\begin{proof}
    Linearity again.
\end{proof}
\begin{proposition}
    We have $L(V,W)\cong M_{m\times n}(F)$ via $\alpha\mapsto[\alpha]_{B,C}$ where $B$ is a basis of $V$ and $C$ is a basis of $W$.
\end{proposition}
\begin{proof}
    Just check.
\end{proof}
\begin{proof}[Proof of Proposition \ref{linear_map_matrices}]
    Simple corollary of the preceding proposition.
\end{proof}
\begin{remark}
    Let $\alpha:V\to W$ be linear.
    The diagram
    \[
        \begin{tikzcd}
            V\arrow{r}{\alpha}\arrow[swap]{d}{v\mapsto [v]_B}&W\arrow{d}{w\mapsto [w]_C}\\
            F^n\arrow[swap]{r}{[\alpha]_{B,C}}&F^m
        \end{tikzcd}
    \]
    commutes.
\end{remark}
\begin{example}
    Let $\alpha:V\to W$ be linear and $Y\le V$ a subspace.
    Let $B$ be a basis of $V$ extending a basis $B'$ of $Y$ and $C$ a basis of $W$ extending a basis $C'$ of $Z\ge\alpha(Y)$.
    Then the entries of $[\alpha]_{B,C}$ that are relevant to $B'\subset B,C'\subset C$ are exactly $[\alpha|_{Y\to Z}]_{B',C'}$.
    So if we rearrange the bases so that $B'$ are in the front of $B$ and $C'$ in the front of $C$, then the matrix will look like
    $$[\alpha]_{B,C}=\left(\begin{array}{c|c}
        [\alpha|_{Y\to Z}]_{B',C'}&\ast\\
        \hline
        0&\ast
    \end{array}\right)$$
\end{example}
\begin{proposition}
    Let $\alpha:V\to W$ be linear and $\alpha(Y)\le Z\le W$, then $\alpha$ induces a map $\bar\alpha:V/Y\to W/Z$ via $v+Y\mapsto\alpha(v)+Z$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}