%
% CMPT 310: Artificial Intelligence - A Course Overview
% Section: Probability
%
% Author: Jeffrey Leung
%

\section{Probability}
	\label{sec:probability}
\subsection{Fundamentals of Probability}
	\label{subsec:probability-fundamentals}
\begin{easylist}

& Fundamentals:
	&& Continuous variable: Variable which can be any continuous value between two given points defining the domain
		&&& Uniform distribution: Variable which has a equal possibility of being any value in the domain
	&& Inverse of a probability: $P(a) = 1 - P(\lnot a)$
	&& Probability of at least one of two events: $P(a \lor b) = P(a) + P(b) - P(a \land b)$
	&& Probability of both events: $P(a \land b) = P(a, b) = P(a) + P(b) - P(a \lor b)$

& \textbf{Factor/probability table:} Set of probabilities of all possible values of a specific set of random variables
	&& May not sum to 1 (in contrast with a probability table)
		&&& \textbf{Normalization constant:} Value equaling the sum of the probabilities, by which every probability is divided by in order to reduce a probability function to a total probability of 1
	&& E.g. See figure~\ref{sec:prob-table}.

\end{easylist}
\begin{figure}[!htb]
	\caption{Example Probability Table}
	\label{sec:prob-table}
	\centering
	\begin{tabular}{ r | c | c | c | c }
		& \multicolumn{2}{ | c | }{Toothache} & \multicolumn{2}{ | c }{$\lnot$ Toothache} \\
		& Rough & $\lnot$ Rough & Rough & $\lnot$ Rough \\
		\hline
		Cavity & 0.108 & 0.012 & 0.072 & 0.008 \\
		\hline
		$\lnot$ Cavity & 0.016 & 0.064 & 0.144 & 0.578
	\end{tabular}
\end{figure}
\begin{easylist}

& \textbf{Probabilistic inference:} Using previous information to compute specific probabilities

& \textbf{Conditional probability:} Probability of one event occurring given the guarantee of another event happening
	&& Notation: Probability of $a$ given $b = P(a|b) = \frac{P(a, b)}{P(b)}$
	&& Inverted: $P(a \land b) = P(a | b) \times P(b)$
	&& \textbf{Conditional Probability Table (CPT):} Probability table showing the probability of the query variable being true given all possible combinations of values of the condition variables
		&&& E.g. See figure~\ref{fig:cpt-example}

\end{easylist}
\begin{figure}[!htb]
	\caption{Example of a Conditional Probability Table}
	\label{fig:cpt-example}
	\centering
	\begin{tabular}{ c | c || c }
		A & B & P(C $\vert$ A, B) \\
		\hline
		T & T & 0.5 \\
		T & F & 0.14 \\
		F & T & 0.29 \\
		F & F & 0.001
	\end{tabular}
\end{figure}
\begin{easylist}

	&& \textbf{Query variable:} Variable for which the value is sought
		&&& E.g. For variables $A, B, C, D$ and the conditional probability $P(a|b, c)$, the query variable is $a$
	&& \textbf{Evidence/condition variable:} Variable which is known
		&&& E.g. For variables $A, B, C, D$ and the conditional probability $P(a|b, c)$, the condition variables are $b, c$
	&& \textbf{Hidden variable:} Variable which is unrelated and not included
		&&& E.g. For variables $A, B, C, D$ and the conditional probability $P(a|b, c)$, the hidden variable is $d$

& \textbf{Chain rule:} Derivation of the probability of two events:

	\end{easylist}
	\begin{align*}
	P(a, b) &= P(a) P(b|a) = P(b) P(a|b) \\
	P(x_1, \dotsc, x_n) &= \prod_{i=1}^n P(x_i | x_1, \dotsc, x_{i-1})
	\end{align*}
	\begin{easylist}

	&& When conditional dependence is added:
	\[
		P(a, b | c) &= P(a | c) P(b | a, c)
	\]

	&& Derived from the formula for conditional probability

& \textbf{Bayes' Rule:} Equation which allows conditional probability calculation given the independent probabilities and inverse conditional probability
	\[
		P(B | A) = \frac{P(A | B) \times P(B)}{P(A)}
	\]

	&& $P(A | B)$: Causal probability (e.g. probability that symptoms occur given a disease is present)
	&& $P(B | A)$: Diagnostic probability (e.g. probability that a disease is present given the symptoms)

\end{easylist}
\begin{align*}
	P(A \land B) &= P(A | B) \times P(B) = P(B | A) \times P(A) \\
	P(B | A) \times P(B) &= P(A | B) \times P(A) \\
	P(B | A) &= \frac{P(A | B) \times P(B)}{P(A)}
\end{align*}
\begin{easylist}

& \textbf{Joint probability distribution:} Probability model consisting of all the possible variables in a model
	&& Size: $d^n-1$ where $d$ = size of the domain, $n$ = number of variables
	&& Runtime: $O(d^n)$ where $d$ = size of the domain, $n$ = number of variables
	&& Requires all data (amount proportional to $d^n$) to create parameters beforehand

& \textbf{Marginalization/summing out:} Determining the probability of a variable from a joint probability distribution by adding up the probabilities for all values of other variables, effectively eliminating the other variables
	&& E.g. For variables $A, B, C$, $P(a, b) = P(a, b, c) + P(a, b, \lnot c)$

& \textbf{Independence:} Property of two variables which do not rely on each other (i.e. knowing if one event occurred has no effect on the probability of the other event)
	&& Definition: $A, B$ are independent iff $P(A | B) = P(A)$ or $P(A, B) = P(A) P(B)$
	&& In a Bayesian network, two variables are dependent if they share a parent nodes (including themselves)
& \textbf{Conditional independence:} Property of two variables given a third variable where, if the third variable is known, then knowing if one event occurred has no effect on the probability of the other event
	&& Definition: $A, B$ are conditionally independent given C if $P(A, B|C) = P(A|C) P(B|C)$
	&& In a Bayesian network, two variables are conditiionally dependent if they share a child node (including themselves)

\end{easylist}
\subsection{Bayesian Networks}
	\label{subsec:bayesian-networks}
\begin{easylist}

& \href{https://www.youtube.com/watch?v=VfyxPtlqZh4}{\textbf{Bayesian Network:}} Acyclic digraph where each node (representing a variable) has a probability conditional on its parents
	&& Nodes represent random variables, directed edges represent conditional dependence
	&& Each node is independent of all nodes which do not share a child node with the original node
	&& Each node is conditionally independent of all nodes which are not its descendents
	&& Runtime: $O(n d^p (d-1))$ where $n$ = number of variables, $d$ = values in the domain, and $p$ = number of parents
	&& For an example, see figure~\ref{fig:bay-net-example}

\end{easylist}
\begin{figure}[!htb]
	\caption{Example of a Bayesian Network}
	\label{fig:bay-net-example}
	\centering
	\begin{tikzpicture}[
		node distance=1cm and 0cm,
		baynetnode/.style={draw, ellipse, text width=2cm, align=center}
	]
		\node[baynetnode] (a) {Alarm};
		\node[right=0.5cm of a]{
			\begin{tabular}{ c | c || c }
				B & E & P(A $\vert$ B, E) \\
				\hline
				T & T & 0.95 \\
				T & F & 0.94 \\
				F & T & 0.29 \\
				F & F & 0.001
			\end{tabular}
		};
		%
		\node[baynetnode, above left=2.5cm of a] (b) {Burglary}
			edge [-latex] (a)
			;
		\node[above=0.5cm of b]{
			\begin{tabular}{ c }
				P(B) \\
				\hline
				0.001
			\end{tabular}
		};
		%
		\node[baynetnode, above right=2.5cm of a] (e) {Earthquake}
			edge [-latex] (a)
			;
		\node[above=0.5cm of e]{
			\begin{tabular}{ c }
				P(E) \\
				\hline
				0.002
			\end{tabular}
		};
		%
		\node[baynetnode, below left=2.5cm of a] (jc) {John calls}
			edge [latex-] (a)
			;
		\node[below=0.5cm of jc]{
			\begin{tabular}{ c || c }
				A & P(J $\vert$ A) \\
				\hline
				T & 0.9 \\
				F & 0.05
			\end{tabular}
		};
		%
		\node[baynetnode, below right=2.5cm of a] (mc) {Mary calls}
			edge [latex-] (a)
			;
		\node[below=0.5cm of mc]{
			\begin{tabular}{ c || c }
				A & P(M $\vert$ A) \\
				\hline
				T & 0.7 \\
				F & 0.01
			\end{tabular}
		};
	\end{tikzpicture}
\end{figure}
\begin{easylist}

\end{easylist}
\subsection{Exact Inference}
	\label{subsec:exact-inference}
\begin{easylist}

& \textbf{Exact inference:} Methods of finding the exact probabilities of a specific solution to specific variables, through application of the chain rule to a probability distribution
	&& Runtime: Exponential
	&& Methods: Joint probability table, enumeration
	&& Equation:
	\[
		P(x_1, \dotsc, x_n) = \prod_{i=1}^n P(x_i | parents(x_i))
	\]

& \textbf{Factor multiplication:} Determining the probability of a specific solution to specific variables by multiplying together the probabilities of the relevant relationships in the Bayesian network
	&& E.g. Given the network in figure~\ref{fig:bay-net-example}, to find the probabilities of the conditionally dependent variables $M, A, B, E$, see figure~\ref{tab:factor-mult}

\end{easylist}
\begin{figure}[!htb]
	\caption{Factor multiplication example}
	\label{tab:factor-mult}
	\centering
	\begin{math}
		\begin{array}{ c | c | c | c || c }
			M & A & B & E & \textrm{Probability} \\
			\hline
			m & a & b & e & P(m|a) P(a, b|e) \\
			m & a & b & \lnot e & P(m|a) P(a, b|\lnot e) \\
			m & a & \lnot b & e & P(m|a) P(a, \lnot b|e) \\
			\vdots & \vdots & \vdots & \vdots & \vdots
		\end{array}
	\end{math}
\end{figure}
\begin{easylist}

	&& \textbf{Inference by enumeration:} Determining the probability of a specific solution to all variables by multiplying together the probabilities of all relationships in the Bayesian network
		&&& E.g. Given the network in figure~\ref{fig:bay-net-example}:

		\end{easylist}
		\begin{align*}
			P(b, \lnot e, \lnot a, j, \lnot m)
			&= P(b) P(\lnot e) P(\lnot a \vert b, \lnot e) P(j \vert \lnot a) P(\lnot m \vert \lnot a) \\
			P(B | j, m)
			&= \alpha P(B) \sum_e P(e) \sum_a P(a | B, e) P(j|a) P(m|a) \\
			&= \alpha P(B) \sum_e P(e) \bigg[
				P(a|B,e) P(j|a) P(m|a) +
				P(\lnot a|B,e) P(j|\lnot a) P(m|\lnot a)
			\bigg] \\
			&= \alpha P(B) \bigg[
				P(a|B,e) P(j|a) P(m|a) +
				P(\lnot a|B,e) P(j|\lnot a) P(m|\lnot a) \\
			&+ 	P(a|B,\lnot e) P(j|a) P(m|a) +
				P(\lnot a|B,\lnot e) P(j|\lnot a) P(m|\lnot a)
			\bigg]
		\end{align*}
		\begin{easylist}

	&& Runtime: $O(n d^n)$ ($n$ probability tables, $d^n$ entries in the probability tables)

& \textbf{Inference by variable elimination:} Algorithm for efficiently executing inference by enumeration which combines relationships between probabilities and marginalizes out hidden variables to reduce factor size
	&& Order can affect runtime; prefer starting from nodes which have no parents
	&& Runtime: Exponential (worst-case) but often better than creating the entire table
	&& E.g. Given variables $A, B, C$, query and condition variables $A, B$, and factors $(C), (A,B,C)$, all factors involving hidden variable $C$ are collected and combined in the first iteration of the algorithm and $C$ is marginalized out to create the factor $A, B$
	&& E.g. Given the network in figure~\ref{fig:bay-net-example}: (warning: expanded version is not actually correct)

	\end{easylist}
	\begin{align*}
		P(B | j, m)
		&= \alpha P(B) \sum_e P(e) \sum_a P(a | B, e) P(j|a) P(m|a) \\[.5cm]
		\textrm{Let } f_1(B, E, j, m)
		&= \sum_a P(a | B, e) P(j|a) P(m|a) \\
		&= P(a | B, e) P(j|a) P(m|a) + P(\lnot a | B, e) P(j|\lnot a) P(m|\lnot a) \\[.5cm]
		P(B | j, m)
		&= \alpha P(B) \sum_e P(e) f_1(B, E, j, m) \\[.5cm]
		\textrm{Let } f_2(B, j, m)
		&= \sum_e P(e) f_1(B, E, j, m) \\
		&= P(e) f_1(B, e, j, m) + P(\lnot e) f_1(B, \lnot e, j, m) \\[.5cm]
		P(B | j, m)
		&= \alpha P(B) f_2(B, j, m) \\[1cm]
		\textrm{Expanded: } P(B | j, m)
		&= P(B) f_2 (B, j, m) \\
		&= \alpha P(B) \Big[ P(e) f_1(B, e, j, m) + P(\lnot e) f_1(B, \lnot e, j, m) \Big] \\
		&= \alpha P(B) \bigg\{
			P(e) \Big[
				P(a | B, e) P(j|a) P(m|a) +
				P(\lnot a | B, e) P(j|\lnot a) P(m|\lnot a)
			\Big] \\
		&+	P(\lnot e) \Big[
				P(a | B, \lnot e) P(j|a) P(m|a) +
				P(\lnot a | B, \lnot e) P(j|\lnot a) P(m|\lnot a)
			\Big]
		\bigg\} \\
		&= \alpha \cdot 0.001 \bigg\{
			0.002 \cdot \Big[
				0.95 \cdot 0.90 \cdot 0.7 +
				0.05 \cdot 0.05 \cdot 0.01
			\Big] \\
		&+	0.998 \Big[
				0.94 \cdot 0.90 \cdot 0.7 +
				0.06 \cdot 0.05 \cdot 0.01
			\Big]
		\bigg\} \\
		&= \begin{pmatrix} 0.284 \\ 0.716 \end{pmatrix}
	\end{align*}
	\begin{easylist}

	&& Algorithm:

\begin{Verbatim}
FUNCTION order(vars)
	RETURN vars in a specific order
END FUNCTION

SET baynet to bayesian_network
SET hidden to baynet.hidden_vars
SET factors to baynet.factors_of_relationships
FOR each var in order(hidden)
	SET current_factors to factors.find(f WHERE f contains var)
	REMOVE current_factors from factors
	SET combined_factors to product_of(current_factors)
	SET combined_factors to marginalize(combined_factors, var)
	# var no longer appears in any factor
	ADD combined_factors to factors
	END IF
END FOR
RETURN factors
\end{Verbatim}

\end{easylist}
\subsection{Approximate Inference}
	\label{subsec:approximate-inference}
\begin{easylist}

& \textbf{Approximate/sampling-based inference:} Methods of inferring the probabilities of a specific solution to specific variables by sampling to create an approximate solution

& \textbf{Rejection sampling:} Determining the approximate solution to specific variables by iterating through variables and for each, sampling the result of a probability multiple times (given all previous probabilities) and dividing the number of preferred results by the number of all results
	&& Parents must be processed before their children (i.e. topological ordering)
	&& Runtime: $O(NVD)$ where $N$ = number of samples, $V$ = number of variables, and $D$ = size of the domain

& \textbf{Gibbs sampling:} Determining the approximate solution to specific variables by fixing evidence variables, randomly assigning values to non-evidence variables, taking a non-evidence variable, sampling given the other assigned values, assigning the result, and repeating with another non-evidence variable
	&& Results converge to the probability
	&& \textbf{Markov blanket:} The parents, children, and all parents of the children of a node, which the node is dependent on, and so the node is independent of all other variables
	&& \textbf{Markov Chain Monte Carlo (MCMC) algorithm:} Algorithm which alters a solution, uses the solution to create a next state, and repeats

\end{easylist}
\subsection{Temporal Bayesian Networks}
	\label{subsec:temporal-bayesian-networks}
\begin{easylist}

& \textbf{Temporal Bayesian Network:} Bayesian network where nodes represent the value of a variable at a specific time, and where each node may be a repeat of an antecedent node which represents the variable at a later time
	&& \textbf{Tree width:} Number of edges crossed by a cut of the graph

\end{easylist}
\subsection{Markov Models}
	\label{subsec:markov-models}
\begin{easylist}

& \textbf{Markov model:} State-change model which uses probabilities based on previous states
	&& Notation: $e_t: e_1, e_2, \dotsc, e_n$ where $e_t$ = known evidence of the state of the model at time $t$
	&& \textbf{First-order Markov model:} Markov model which depends only on the previous state (i.e. $P(x_t | x_{t-1})$)
	&& \textbf{$n$\textsuperscript{th}-order Markov model:} Markov model which depends on the previous $n$ states (i.e. \\ $P(x_t | x_{t-1}, \dotsc, x_{t-n})$)
	&& \textbf{Hidden Markov model:} Markov model which contains hidden states which are conditional upon evidence states
		&&& Notation: $P(x_t | e_{t-1}, e_{t-2}, \dotsc)$ where $x_t =$ unknown state of the model at time $t$;
			&&&& \textbf{Initial distribution:} $P(e_1)$
			&&&& \textbf{Transition distribution:} $P(x_t | x_{t-1})$
			&&&& \textbf{Emission distribution/corrector:} $P(e_t | x_t)$
		&&& For a diagram, see figure~\ref{fig:hidden-markov-model}

		\end{easylist}
		\begin{figure}[!htb]
			\caption{Diagram of a Hidden Markov Model}
			\label{fig:hidden-markov-model}
			\centering
			\begin{tikzpicture}[
				node distance=1cm and 0cm,
				hmmnode/.style={draw, circle, minimum size=1cm, align=center}
			]
				\node[hmmnode] (x1) {$x_1$};
				\node[hmmnode, ->, below=1cm of x1] (e1) {$e_1$} edge [latex-] (x1);
				\node[hmmnode, ->, right=1cm of x1] (x2) {$x_2$} edge [latex-] (x1);
				\node[hmmnode, ->, below=1cm of x2] (e2) {$e_2$} edge [latex-] (x2);
				\node[hmmnode, ->, right=1cm of x2] (ellipses) {$\dotso$} edge [latex-] (x2);
				\node[hmmnode, ->, right=1cm of ellipses] (xt-1) {$x_{t-1}$} edge [latex-] (ellipses);
				\node[hmmnode, ->, below=1cm of xt-1] (et-1) {$e_{t-1}$} edge [latex-] (xt-1);
				\node[hmmnode, ->, right=1cm of xt-1] (xt) {$x_t$} edge [latex-] (xt-1);
				\node[hmmnode, ->, below=1cm of xt] (et) {$e_t$} edge [latex-] (xt);
			\end{tikzpicture}
		\end{figure}
		\begin{easylist}

& \textbf{Filtering:} Method of hidden Markov model inference which finds an approximate hidden probability given evidences of all previous probabilities (i.e. finding $x_t$ given $e_{1:t}$)
	&& Derivation: Given $P(x_{t-1} | e_{1:t-1})$, find $P(x_t|e_{1:t})$. (See equation~\ref{eq:filtering-deriv}.)
	\end{easylist}

\begin{landscape}
	\begin{figure}[!htb]
		\caption{Derivation of Calculation for Filtering}
		\label{eq:filtering-deriv}
		\begin{align*}
			P(x_t | e_{1:t})
			&= P(x_t | e_{1:t-1}, e_t ) \\
			&= \frac{1}{z} P(x_t, e_t | e_{1:t-1}) & \textrm{Move } e_t \textrm{ out of condition} \\
			&= \frac{1}{z} \sum_{x_{t-1}} P(x_{t-1}, x_t, e_t | e_{1:t-1}) & \textrm{Reverse marginalization} \\
			&= \frac{1}{z} \sum_{x_{t-1}} P(x_{t-1}, x_t | e_{1:t-1}) \cdot P(e_t | x_{t-1}, x_t, e_{1:t-1}) & \textrm{Chain rule} \\
			&= \frac{1}{z} \sum_{x_{t-1}} P(x_{t-1} | e_{1:t-1}) \cdot P(x_t | x_{t-1}, e_{1:t-1}) \cdot P(e_t | x_{t-1}, x_t, e_{1:t-1}) & \textrm{Chain rule} \\
			&= \frac{1}{z} \sum_{x_{t-1}} P(x_{t-1} | e_{1:t-1}) \cdot P(x_t | x_{t-1}) \cdot P(e_t | x_t) & \textrm{Conditional independence} \\
			&= P(e_t | x_t) \cdot \frac{1}{z} \sum_{x_{t-1}} P(x_{t-1} | e_{1:t-1}) \cdot P(x_t | x_{t-1}) \\
			\textrm{where } & P(e_t | x_t) \textrm{ is the emission distribution,} \\
			& P(x_{t-1} | e_{1:t-1}) \textrm{ is the provided probability and evidence, and} \\
			& P(x_t | x_{t-1}) \textrm{ is the transition distribution.}
		\end{align*}
	\end{figure}
\end{landscape}

	\begin{easylist}

	&& Runtime: $O(T D^2)$ where $T =$ number of time periods and $D =$ degree of the hidden variable
	&& Space complexity: $O(D)$ where $D =$ degree of the hidden variable
		&&& Only the last two time periods of probabilities of each degree is necessary

& \textbf{Smoothing (forward/backward algorithm):} Method of hidden Markov model inference which finds the approximated previous hidden probabilities given evidence of previous and future probabilities (i.e. finding $x_t$ given $e_{1:T}$ where $1 \leq t < T$)
	&& Derivation: Given $e_{1:t}$, find $P(x_k | e_{1:t})$. (See equation~\ref{eq:smoothing-deriv}.)
	\end{easylist}

\begin{landscape}
	\begin{figure}[!htb]
		\caption{Derivation of Calculation for Smoothing}
		\label{eq:smoothing-deriv}
		\begin{align*}
			P(x_k | e_{1:t})
			&= \frac{1}{z} P(x_k, e_{k+1:t} | e_{1:k}) & \textrm{Chain rule} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot P(e_{k+1:t} | x_k, e_{1:k}) & \textrm{Chain rule} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot P(e_{k+1:t} | x_k) & \textrm{(Step 3) Conditional 	independence} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot \sum_{x_{k+1}} P(x_{k+1}, e_{k+1:t} | x_k) & \textrm{Reverse marginalization} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot \sum_{x_{k+1}} P(x_{k+1} | x_k) \cdot P(e_{k+1:t} | x_{k+1}, x_k) & \textrm{Chain rule} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot \sum_{x_{k+1}} P(x_{k+1} | x_k) \cdot P(e_{k+1:t} | x_{k+1}) & \textrm{Conditional independence} \\
			&= \frac{1}{z} P(x_k | e_{1:k}) \cdot \sum_{x_{k+1}} P(x_{k+1} | x_k) \cdot P(e_{k+1} | x_{k+1}) \cdot P(e_{k+2:t} | x_{k+1}) & \textrm{Separating multiplied probabilities} \\
			\textrm{where } & P(x_k | e_{1:k}) \textrm{ can be found as the result of filtering,} \\
			& P(x_{k+1} | x_k) \textrm{ is the transition distribution,} \\
			& P(e_{k+1} | x_{k+1}) \textrm{ is the emission distribution, and} \\
			& P(e_{k+2:t} | x_{k+1}) \textrm{ is recursively solved from Step 3.} \\
		\end{align*}
	\end{figure}
\end{landscape}

	\begin{easylist}

& \textbf{Prediction:} Method of hidden Markov model inference which finds the approximated future hidden probabilities given evidence of all previous probabilities (i.e. finding $x_t$ given $e_{1:t-\delta}$)

& \textbf{Most likely sequence of states:} Method of hidden Markov model inference which finds the most likely sequence of states given evidence of their probabilities (i.e. finding $\underset{x_{1:t}}{arg \, max} \; P(x_{1:t} | e_{1:t})$, the most likely states $x_{1:t}$ given $e_{1:t}$)
	&& \textbf{Viterbi algorithm:} Algorithm which finds the most likely sequence of states
		&&& Algorithm:

\begin{lstlisting}[breaklines=true, mathescape=true]
# Initialize arrays
SET vitr[num_observ][num_states] to [][]
SET prev[num_observ][num_states] to [][]

FOR each state from 1 to n
	SET vitr[0][state] to original_prob[state] × emission(state i, observation 0)
	SET prev[0][state] to 0
END FOR

# For each observation and state
FOR each observation current_observ from 1 to num_observ-1
	FOR each state current_state from 0 to num_states-1

		# Calculate all possible probabilities towards current state
		SET max_prob to 0
		FOR each prev_state from 0 to n-1
			SET prob to vitr[current_observ-1][prev_state] $\times$ transition(prev_state, current_state) $\times$ emission(current_state, current_observ)
			IF prob $>$ max_prob THEN
				SET max_prob to prob
			END IF
		END FOR

		# Set the max and argmax
		SET vitr[current_observ][current_state] to max_prob
		SET prev[current_observ][current_state] to vitr[current_observ].index(max_prob)
	END FOR
END FOR

# Trace back to find most likely sequence
SET sequence to []
SET current_state to vitr[current_observ].index(max(vitr[current_observ]))
FOR each observation current_observ from t-1 to -1
	SET sequence to [current_state] + sequence
	SET current_state to prev[current_observ][current_state]
END FOR

RETURN sequence
\end{lstlisting}

\end{easylist}
\clearpage


\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1.0in]{geometry}
\usepackage{xcolor}

\begin{document}

\noindent
Does $\displaystyle \sum_{n=2}^\infty \frac{1}{n \ln n}$
diverge, converge absolutely, or converge conditionally?

\subsection*{Solution}

The function $f(x)=\frac{1}{x \ln x}$ is continuous, positive, and decreasing on $[3,\infty)$. We consider the integral
\[ \int_3^\infty \frac{1}{x \ln x}\,dx = \lim_{t \to \infty} \int_3^t \frac{1}{x \ln x}\,dx.\]
We do the indefinite integral using the substitution $u= \ln x$, so $du = \frac1x\,dx$:
\[
\int \frac{1}{x \ln x}\,dx
= \int \frac1u\,du = \ln |u| + C = \ln | \ln (x) | + C
\]
so, back to the improper integral,
\begin{align*}
\int_3^\infty \frac1{x \ln x}\,dx
&= \lim_{t \to \infty} \int_3^t \frac1{x\ln x}\,dx\\
&= \lim_{t \to \infty}\left[   \ln | \ln (t) |  -  \ln | \ln (3) |  \right] \\
&= \infty.
\end{align*}
Since the integral $\displaystyle  \int_3^\infty \frac{1}{x \ln x}\,dx$ diverges, the series  $\displaystyle \sum_{n=3}^\infty \frac{1}{n \ln n}$ diverges by the Integral Test. So the series $\displaystyle \sum_{n=2}^\infty \frac{1}{n \ln n}$ diverges as well.

\end{document}%%%%%%%%%%%%%%%%%

\begin{align*}
L&=\lim_{n \to \infty} \sqrt[n]{|a_n|}\\
&= \lim_{n \to \infty} \sqrt[n]{\left| \right|}\\
\end{align*}

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty AAAAAAAAAAAAAA$ converges absolutely.

Since $|r| < 1$, the series ...  converges by the Geometric Series Test.

Since $|r| \geq 1$, the series ...  diverges by the Geometric Series Test.

The function $f(x)=\frac{}{}$ is continuous, positive, and decreasing on $[1,\infty)$.

\subsection*{Solution}


\section{Cylindrical and Spherical Coordinates}\label{sec:CylSphCoords}

We have seen that sometimes double integrals are simplified by doing
them in polar coordinates; not surprisingly, triple integrals are
sometimes simpler in cylindrical coordinates or spherical coordinates.
To set up integrals in polar coordinates, we had to understand the
shape and area of a typical small region into which the region of
integration was divided. We need to do the same thing here, for three
dimensional regions.\index{cylindrical coordinates!triple integral}\index{spherical coordinates!triple integral}

The cylindrical coordinate system is the simplest, since it is just
the polar coordinate system plus a $z$ coordinate. A typical small
unit of volume is the shape shown in Figure~\ref{fig:polarcoordinatesregions}
``fattened up'' in the $z$ direction, so its
volume is $r\Delta r\Delta \theta\Delta z$, or in the limit,
$r\,dr\,d\theta\,dz$.

\begin{example}{Finding Volume}{FindVolumeExample}
Find the volume under $z=\sqrt{4-r^2}$
above the quarter circle inside $x^2+y^2=4$
in the first quadrant.
\end{example}
\begin{solution}
We could of course do this with a double integral, but we'll use a
triple integral:
$$\int_0^{\pi/2}\int_0^2\int_0^{\sqrt{4-r^2}} r\,dz\,dr\,d\theta=
\int_0^{\pi/2}\int_0^2 \sqrt{4-r^2}\; r\,dr\,d\theta=
{4\pi\over3}.$$
Compare this to Example~\ref{exa:integrationinpolarcoordinates}.
\end{solution}

\begin{example}{Mass using Cylindrical Coordinates}{MassCylCoords}
An object occupies the space inside both the cylinder
$x^2+y^2=1$ and the sphere $x^2+y^2+z^2=4$, and has density $x^2$ at
$(x,y,z)$. Find the total mass.
\end{example}
\begin{solution}
We set this up in cylindrical coordinates, recalling that
$x=r\cos\theta$:
\begin{align*}
\int_0^{2\pi}\int_0^1\int_{-\sqrt{4-r^2}}^{\sqrt{4-r^2}}
r^3\cos^2(\theta)\,dz\,dr\,d\theta
&=\int_0^{2\pi}\int_0^1 2\sqrt{4-r^2}\;r^3\cos^2(\theta)\,dr\,d\theta	\\
&=\int_0^{2\pi}\left({128\over15}-{22\over5}\sqrt3\right)\cos^2(\theta)\,d\theta	\\
&=\left({128\over15}-{22\over5}\sqrt3\right)\pi
\end{align*}
\end{solution}

Spherical coordinates are somewhat more difficult to understand. The
small volume we want will be defined by $\Delta\rho$, $\Delta\phi$,
and $\Delta\theta$, as pictured in Figure~\ref{fig:sphericalvolumeunit}.
The small volume is nearly box shaped, with 4 flat sides and two sides
formed from bits of concentric spheres. When $\Delta\rho$, $\Delta\phi$,
and $\Delta\theta$ are all very small, the volume of this little
region will be nearly the volume we get by treating it as a box.
One dimension of the box is simply $\Delta\rho$, the change in distance
from the origin. The other two dimensions are the lengths of small
circular arcs, so they are $r\Delta\alpha$ for some suitable
$r$ and $\alpha$, just as in the polar coordinates case.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <1.5truecm,1.5truecm>
\setplotarea x from 0 to 2.1, y from -1.1 to 1.1
\put {\hbox{\epsfxsize9cm\epsfbox{images/spherical_volume_unit.eps}}} at 0 0
\endpicture}}
\caption{A small unit of volume for spherical coordinates.}
\label{fig:sphericalvolumeunit}
\end{figure}

The easiest of these to understand is the arc corresponding to a
change in $\phi$, which is nearly identical to the derivation for
polar coordinates, as shown in the left graph in Figure~\ref{fig:intspherical}.
In that graph we are looking ``face on'' at the side of
the box we are interested in, so the small angle pictured is
precisely $\Delta\phi$, the vertical axis really is the $z$ axis, but
the horizontal axis is \emph{not} a real axis---it is just some line
in the $x$-$y$ plane.
Because the other arc is governed by $\theta$, we need
to imagine looking straight down the $z$ axis, so that the apparent
angle we see is $\Delta\theta$. In this view, the axes really are the
$x$ and $y$ axes.
In this graph, the apparent distance from the
origin is not $\rho$ but $\rho\sin\phi$, as indicated in the left
graph.

\begin{figure}[H]
\centerline{
\vbox{\beginpicture
\normalgraphs
\setcoordinatesystem units <1.5truecm,1.5truecm>
\setplotarea x from 0 to 3.5, y from 0 to 3
\axis left  /
\axis bottom  /
\circulararc 15 degrees from 3.17 1.48 center at 0 0
\circulararc 15 degrees from 2.266 1.057 center at 0 0
\setlinear
\plot 3.17 1.48 0 0 2.68 2.25 /
\betweenarrows {$\rho\sin\phi$} [t] <0pt,-3pt> from 0 0 to 3.17 0
\setdashes
\plot 3.17 1.48 3.17 0 /
\put {$z$} [b] <0pt,3pt> at 0 3
\put {$\Delta \rho$} [tl] <0pt,-3pt> at 2.72 1.27
\put {$\rho\Delta \phi$} [bl] <2pt,2pt> at 2.95 1.88
\put {$\Delta\phi$} at 1.01 0.645
\setsolid
\setcoordinatesystem units <1.5truecm,1.5truecm> point at -5 0
\setplotarea x from 0 to 3.5, y from 0 to 3
\axis left  /
\axis bottom  /
\circulararc 15 degrees from 2.87 1.34 center at 0 0
\circulararc 15 degrees from 2.05 0.955 center at 0 0
\setlinear
\plot 2.87 1.34 0 0 2.43 2.04 /
\put {$x$} [l] <3pt,0pt> at 3.5 0
\put {$y$} [b] <0pt,3pt> at 0 3
\put {$\rho\sin\phi\Delta \theta$} [bl] <2pt,2pt> at 2.67 1.7
\put {$\Delta\theta$} at 1.01 0.645
\endpicture}}
\caption{Setting up integration in spherical coordinates.}
\label{fig:intspherical}
\end{figure}

The upshot is that the volume of the little box is approximately
$\Delta\rho(\rho\Delta\phi)(\rho\sin\phi\Delta\theta)
=\rho^2\sin\phi\Delta\rho\Delta\phi\Delta\theta$, or in the limit
$\rho^2\sin\phi\,d\rho\,d\phi\,d\theta$.

\begin{example}{Average Temperature in a Unit Sphere}{AvgTempUnitSphere}
Suppose the temperature at $(x,y,z)$ is
$T=1/(1+x^2+y^2+z^2)$. Find the average temperature in the unit sphere
centered at the origin.
\end{example}
\begin{solution}
In two dimensions we add up the temperature at ``each'' point and
divide by the area; here we add up the temperatures and divide by the
volume, $(4/3)\pi$:
\[{3\over4\pi}\int_{-1}^1\int_{-\sqrt{1-x^2}}^{\sqrt{1-x^2}}
\int_{-\sqrt{1-x^2-y^2}}^{\sqrt{1-x^2-y^2}}
{1\over1+x^2+y^2+z^2}\,dz\,dy\,dx\]
This looks quite messy; since everything in the problem is closely
related to a sphere, we'll convert to spherical coordinates.
\[{3\over4\pi}\int_0^{2\pi}\int_0^\pi\int_0^1{1\over1+\rho^2}\,\rho^2\sin\phi\,d\rho\,d\phi\,d\theta
={3\over4\pi}(4\pi -\pi^2)=3-{3\pi\over4}.\]
\end{solution}

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

\begin{enumialphparenastyle}

\begin{ex}
Evaluate $\ds\int_{0}^{1}\int_{0}^{x}\int_{0}^{\sqrt{x^2+y^2}}
{(x^2+y^2)^{3/2}\over x^2+y^2+z^2}\,dz\,dy\,dx$.
\begin{sol}
$\pi/12$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int_{-1}^{1}\int_{0}^{\sqrt{1-x^2}}
\int_{\sqrt{x^2+y^2}}^{\sqrt{2-x^2-y^2}}\sqrt{x^2+y^2+z^2}\,dz\,dy\,dx$.
\begin{sol}
$\pi(1-\sqrt2/2)$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int x^2\,dV$
over the interior of the cylinder $x^2+y^2=1$ between $z=0$ and $z=5$.
\begin{sol}
$5\pi/4$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int xy\,dV$
over the interior of the cylinder $x^2+y^2=1$ between $z=0$ and $z=5$.
\begin{sol}
$0$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int z\,dV$
over the region above the $x$-$y$ plane, inside $x^2+y^2-2x=0$ and
under $x^2+y^2+z^2=4$.
\begin{sol}
$5\pi/4$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int yz\,dV$
over the region in the first octant, inside $x^2+y^2-2x=0$ and
under $x^2+y^2+z^2=4$.
\begin{sol}
$4/5$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int x^2+y^2\,dV$
over the interior of $x^2+y^2+z^2=4$.
\begin{sol}
$256\pi/15$
\end{sol}
\end{ex}

\begin{ex}
Evaluate $\ds\int\int\int \sqrt{x^2+y^2}\,dV$
over the interior of $x^2+y^2+z^2=4$.
\begin{sol}
$4\pi^2$
\end{sol}
\end{ex}

\begin{ex}
Compute $\ds\int\int\int
x+y+z\,dV$ over the region inside
$x^2+y^2+z^2 = 1$ in the first octant.
\begin{sol}
$\ds {3\pi\over16}$
\end{sol}
\end{ex}

\begin{ex}
Find the mass of a right circular cone of height $h$ and
base radius $a$ if the density is proportional to the distance from
the base.
\begin{sol}
$\pi kh^2a^2/12$
\end{sol}
\end{ex}

\begin{ex}
Find the mass of a right circular cone of height $h$ and
base radius $a$ if the density is proportional to the distance from
its axis of symmetry.
\begin{sol}
$\pi kha^3/6$
\end{sol}
\end{ex}

\begin{ex}
An object occupies the region inside the unit sphere at the
origin, and has density equal to the distance from the $x$-axis. Find
the mass.
\begin{sol}
$\pi^2/4$
\end{sol}
\end{ex}

\begin{ex}
An object occupies the region inside the unit sphere at the
origin, and has density equal to the square of the distance from the
origin. Find the mass.
\begin{sol}
$4\pi/5$
\end{sol}
\end{ex}

\begin{ex}
An object occupies the region between the unit sphere at the
origin and a sphere of radius 2 with center at the origin, and has
density equal to the distance from the origin. Find the mass.
\begin{sol}
$15\pi$
\end{sol}
\end{ex}

\begin{ex}
An object occupies the region in the first octant bounded by
the cones $\phi = \pi/4$ and $\phi = \arctan 2$, and the sphere $\rho
= \sqrt{6}$, and has density proportional to the distance from the
origin. Find the mass.
\begin{sol}
$9k\pi(5\sqrt2-2\sqrt5)/20$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\subsection{Real convergence}\label{subsec:real_convergence}

\begin{theorem}[Bolzano-Weierstrass]\label{def:bolzano_weierstrass}
  Every bounded sequence in \( \BbbR \) has a \hyperref[def:net_convergence/limit]{convergent} \hyperref[def:sequence]{subsequence}.
\end{theorem}
\begin{proof}
  Let \( \{ x_k \}_{k=1}^\infty \) be a bounded sequence in \( \BbbR \) and let \( a \leq b \) be a lower and upper \hyperref[def:partially_ordered_set_extremal_points/upper_and_lower_bounds]{bound}, respectively. Construct the sequence \( \{ F_k \}_{k=1}^\infty \) of closed intervals as follows: define \( \alpha_1 \coloneqq a \) and \( \beta_1 \coloneqq b \) and, at step \( k = 1, 2, \ldots \), put
  \begin{balign*}
    F_k \coloneqq \begin{cases}
      [\alpha_k, \tfrac{\alpha_k+\beta_k} 2], & [\alpha_k, \tfrac{\alpha_k+\beta_k} 2]\text{ contains infinitely many sequence members}, \\
      [\tfrac{\alpha_k+\beta_k} 2, \beta_k],  & \text{otherwise}.
    \end{cases}
  \end{balign*}

  Then put \( \alpha_{k+1} \) and \( \beta_{k+1} \) to be the endpoints of the interval \( F_k \) and repeat with \( k+1 \) instead of \( k \). Note that for any \( k = 1, 2, \ldots \), \( \diam(F_k) = \tfrac 1 2 \diam(F_{k-1}) \), thus \( \diam(F_k) \xrightarrow[k \to \infty]{} 0 \). As in \fullref{thm:cantors_nested_compact_theorem}, it follows that if we choose a sequence
  \begin{equation*}
    x_k \in F_k, k = 1, 2, \ldots,
  \end{equation*}
  it will be a fundamental sequence. Since the space is complete, this fundamental sequence necessarily converges.
\end{proof}

\begin{theorem}\label{def:set_of_real_numbers_complete_metric_space}
  The metric space \( \BbbR \) is complete.
\end{theorem}
\begin{proof}
  Let \( \{ x_k \}_{k=1}^\infty \) be a fundamental sequence of real numbers. By \fullref{thm:fundamental_sequence_is_bounded}, the sequence is bounded. By \fullref{def:bolzano_weierstrass}, it has a convergent subsequence
  \begin{equation*}
    \{ x_{k_m} \}_{m=1}^\infty \to x.
  \end{equation*}

  By \fullref{thm:fundamental_subsequence_convergence}, the sequence itself has the same limit \( \lim_{k \to \infty} x_k = x \).
\end{proof}

\begin{proposition}\label{thm:one_sided_squeeze_lemma}
  Fix two convergent sequences \( \{ x_k \}_{k=1}^\infty \) and \( \{ y_k \}_{k=1}^\infty \) of real numbers.

  If \( x_k \leq y_k \) for all \( k = 1, 2, \ldots \), then
  \begin{equation*}
    \lim_{k \to \infty} x_k \leq \lim_{k \to \infty} y_k.
  \end{equation*}
\end{proposition}
\begin{proof}
  Denote the respective limits by \( x \) and \( y \).

  Fix \( \varepsilon > 0 \). Then by \fullref{def:net_convergence/limit}, there exist indices \( k_0 \) and \( m_0 \) such that
  \begin{balign*}
     & \abs{x - x_k} < \frac \varepsilon 2 \quad\forall k \geq k_0 \\
     & \abs{y - y_m} < \frac \varepsilon 2 \quad\forall m \geq m_0
  \end{balign*}

  Take \( k \geq \max \{ k_0, m_0 \} \). Then \( y_k \geq x_k \) and
  \begin{balign*}
    y - x
     & =
    (y - y_k) + (y_k - x_k) + (x_k - x)
    \geq \\ &\geq
    (y - y_k) + (x - x_k)
    >    \\ &>
    - \frac \varepsilon 2 - \frac \varepsilon 2
    =
    - \varepsilon.
  \end{balign*}

  Since \( \varepsilon \) was chosen arbitrary, \( y - x \) cannot equal any negative number, because otherwise we could choose another \( \varepsilon \) smaller than the magnitude of the negative number and obtain a contradiction.

  Thus, \( y \geq x \).
\end{proof}

\begin{lemma}[Squeeze lemma]\label{thm:squeeze_lemma}
  Let \( I \) be a closed \hyperref[def:partially_ordered_set_interval/closed]{interval} in \( \BbbR \).

  \begin{thmenum}
    \thmitem{thm:squeeze_lemma/sequences} Let \( \{ x_k \}_{k=1}^\infty, \{ x_k^- \}_{k=1}^\infty, \{ x_k^+ \}_{k=1}^\infty \) be three sequences in \( I \). If both \( \{ x_k^- \}_{k=1}^\infty \) and \( \{ x_k^+ \}_{k=1}^\infty \) converge to the same value \( \overline x \in I \) and if the following inequalities
    \begin{equation*}
      x_k^- \leq x_k \leq x_k^+
    \end{equation*}
    hold for all \( k = 1, 2, \ldots \), then the \enquote{squeezed in} sequence \( \{ x_k \}_{k=1}^\infty \) also converges to \( \overline x \).

    \thmitem{thm:squeeze_lemma/functions} Let \( f, f_-, f_+: I \to \BbbR \) be three functions and let \( \overline x \in I \). If both limits \( \lim_{x \to \overline x} f_-(x) \) and \( \lim_{x \to \overline x} f_+(x) \) converge to the same value \( \overline y \in \BbbR \) and if the following inequalities
    \begin{equation*}
      f_-(x) \leq f(x) \leq f_+(x)
    \end{equation*}
    hold for all \( x \in I \), then the \enquote{squeezed in} function \( f \) also converges to \( \overline y \) at \( \overline x \).
  \end{thmenum}
\end{lemma}
\begin{proof}
  \SubProofOf{thm:squeeze_lemma/sequences} Fix \( \varepsilon > 0 \). Then by \fullref{def:net_convergence/limit}, there exist indices \( k^- \) and \( k^+ \) such that
  \begin{balign*}
     & \abs{\overline x - x_k^-} < \frac \varepsilon 3 \quad\forall k \geq k^- \\
     & \abs{\overline x - x_k^+} < \frac \varepsilon 3 \quad\forall k \geq k^+
  \end{balign*}

  By taking \( k \geq \max \{ k^-, k^+ \} \), we obtain
  \begin{equation*}
    \abs{x_k^+ - x_k^-} \leq \abs{x_k^+ - \overline x} + \abs{\overline x - x_k^-} < \frac 2 3 \varepsilon.
  \end{equation*}

  Since \( \abs{x_k^+ - x_k} \leq \abs{x_k^+ - x_k^-} \), it follows that \( \abs{x_k^+ - x_k} < \frac 2 3 \varepsilon \).

  Thus,
  \begin{equation*}
    \abs{\overline x - x_k} \leq \abs{\overline x - x_k^+} + \abs{x_k^+ - x_k} < \varepsilon.
  \end{equation*}

  \Fullref{def:net_convergence/limit} is satisfied, hence \( \{ x_k \} \) converges to \( \overline x \).

  \SubProofOf{thm:squeeze_lemma/functions} The proof is analogous to that of \fullref{thm:squeeze_lemma/sequences}, but the machinery is different. Fix \( \varepsilon > 0 \). Then by \fullref{def:local_convergence/neighborhoods}, there exist radii \( \delta^- \) and \( \delta^+ \) such that
  \begin{balign*}
     & f_-(I \cap B(\overline x, \delta^-)) \subseteq B(\overline y, \tfrac \varepsilon 3) \\
     & f_+(I \cap B(\overline x, \delta^+)) \subseteq B(\overline y, \tfrac \varepsilon 3)
  \end{balign*}

  Take \( \delta < \min \{ \delta^-, \delta^+ \} \) and \( x \in I \cap B(\overline x, \delta) \). Analogously to the proof of \fullref{thm:squeeze_lemma/sequences}, we obtain the inequality
  \begin{equation*}
    \abs{f(x) - \overline x} \leq \abs{f(x) - f^-(x)} + \abs{f^-(x) - \overline x} < \varepsilon.
  \end{equation*}

  We conclude that
  \begin{equation*}
    f(I \cap B(\overline x, \delta)) \subseteq B(\overline y, \varepsilon)
  \end{equation*}
  holds and thus by \fullref{def:local_convergence/neighborhoods}, the function \( f \) converges to \( \overline y \) at \( \overline x \).
\end{proof}

\begin{proposition}\label{thm:real_monotone_sequence_converges_iff_bounded}
  A \hyperref[def:partially_ordered_set/homomorphism]{monotone} sequence of real numbers \hyperref[def:net_convergence/limit]{converges} if and only if it is \hyperref[def:metric_space/bounded_sequence]{bounded}.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Let \( \{ x_k \}_{k=1}^\infty \) be a convergent monotone sequence. Denote its limit by \( x \). Fix \( \varepsilon > 0 \). By \fullref{def:net_convergence/limit}, there exists \( k_0 \) such that
  \begin{equation*}
    \abs{x - x_k} < \varepsilon \quad\forall k \geq k_0.
  \end{equation*}

  Thus, \( \{ x_k \colon k \geq k_0 \} \subseteq B(x, \varepsilon) \).

  Also note that
  \begin{equation*}
    \{ x_k \colon k < k_0 \} \subseteq B(x, \max_{i < k_0} \{ \abs{x - x_k} \}).
  \end{equation*}

  We obtained that the entire sequence
  \begin{equation*}
    \{ x_k \colon k \geq 1 \} = \{ x_k \colon k < k_0 \} \cup \{ x_k \colon k \geq k_0 \}
  \end{equation*}
  is contained in a union of two balls and is therefore bounded.

  \NecessitySubProof Now let \( \{ x_k \}_{k=1}^\infty \) be a bounded monotone sequence. Denote its supremum by \( \alpha \). Note that
  \begin{equation*}
    \abs{x_n - x_m} = x_n - x_m \leq \alpha \quad\forall n \geq m.
  \end{equation*}

  Fix \( \varepsilon > 0 \). Then there exists at least one element \( x_{m_0} > \alpha - \varepsilon \) because otherwise \( \alpha \) would not be a supremum.

  Then for any index \( n \geq m_0 \) we have
  \begin{equation*}
    \abs{x_n - x_{m_0}} = x_n - x_{m_0} < \alpha - (\alpha - \varepsilon) = \varepsilon.
  \end{equation*}

  Thus, \fullref{def:net_convergence/limit} is satisfied and the sequence \( \{ x_k \}_{k=1}^\infty \) converges.
\end{proof}


\subsection{Generating sets}

We can define a group through a generating set and an operation.

And define the group as \(G=< S >\)

\subsection{Finite groups}

Consider the set of natural numbers and addition modulo 4. This forms a group containing:

\(\{0,1,2,3\}\)

This can be written as \(Z_4\) or more generally as \(Z_n\), or \(Z/nZ\).


\subsection{Green's Theorem for Flux}
\begin{theorem}[Green's Theorem from Flux]
	Let $C$ be a closed, counter-clockwise oriented curve in $\mathbb{R}^2$ and let $D$ be the region contained within $C$. For any differentiable vector field $\vec{F}(x,y)$,
	\begin{equation*}
		\iint\limits_{D}{\nabla \cdot \vec{f}\mathrm{d}A} = \oint\limits_{C}{\vec{F} \cdot \hat{n}\mathrm{d}s}
	\end{equation*}
\end{theorem}

[INSERT IMAGE]

\noindent
This is saying that the sum of the divergence within $D$ is equal to the flux through $C$.\\
Our intuition for this is that divergence is the tendency for integral curves of $\vec{F}$ to spread out, and flux would then be these integral curves crossing the boundary curve $C$.\\

\noindent
This ability to convert between a line integral and surface integral often makes flux problems easier to solve. For example, one would have to calculate four integrals to find the flux through a rectangular region, but only a single double integral over the simple interior region.


\section{Methodology}
\label{sec:methodology}

\subsection{Fundamentals of magnetic equivalent layer and the positive magnetic-moment distribution}
\label{subsec:mag_eqlayer}

Let $\Delta T(x, y, z)$ be the total-field anomaly produced by a set of magnetic
sources at a point $(x, y, z)$ referred to a topocentric Cartesian coordinate system
with $x$-, $y$- and $z$-axis being oriented to north, east and down, respectively.
Consider that the main geomagnetic field has a constant inclination $I_{0}$ and declination
$D_{0}$ throughout the study area, so that its direction can be defined by the unit vector
\begin{equation}
\hat{\mathbf{F}}_{0} = \begin{bmatrix}
\cos I_{0} \cos D_{0} \\
\cos I_{0} \sin D_{0} \\
\sin I_{0}
\end{bmatrix} \: .
\label{eq:main_field}
\end{equation}
Additionally, consider that the magnetic sources have a constant total magnetization
direction defined by the unit vector
\begin{equation}
\hat{\mathbf{m}}(\mathbf{q}) = \begin{bmatrix}
\cos {I} \cos {D} \\
\cos {I} \sin {D} \\
\sin {I}
\end{bmatrix} \: ,
\label{eq:mag_vec}
\end{equation}
where the constants $I$ and $D$ represent its inclination and declination, respectively,
and $\mathbf{q}$ is a $2 \times 1$ vector given by:
\begin{equation}
\mathbf{q} = \begin{bmatrix}
I \\
D
\end{bmatrix} \: .
\label{eq:q_vector}
\end{equation}
For convenience, we call $\mathbf{q}$ as magnetization direction vector.
In this case, the total-field anomaly $\Delta T(x, y, z)$ can be written as follows:
\begin{equation}
\Delta T(x, y, z) = \hat{\mathbf{F}}_{0}^{\top} \mathbf{M}(x, y, z) \:
\hat{\mathbf{m}}(\mathbf{q}) \: ,
\label{eq:tfanomaly}
\end{equation}
where $\mathbf{M}(x, y, z)$ is a matrix given by
\begin{equation}
	\mathbf{M}(x, y, z) = \begin{bmatrix}
		\partial_{xx} \Gamma(x, y, z) &
		\partial_{xy} \Gamma(x, y, z) &
		\partial_{xz} \Gamma(x, y, z) \\
		\partial_{xy} \Gamma(x, y, z) &
		\partial_{yy} \Gamma(x, y, z) &
		\partial_{yz} \Gamma(x, y, z) \\
		\partial_{xz} \Gamma(x, y, z) &
		\partial_{yz} \Gamma(x, y, z) &
		\partial_{zz} \Gamma(x, y, z)
	\end{bmatrix} \: ,
	\label{eq:M-matrix}
\end{equation}
with elements
$\partial_{\alpha\beta} \Gamma(x, y, z) \equiv
\frac{\partial^{2} \Gamma(x, y, z)}{\partial \alpha \partial \beta}$,
$\alpha, \beta = x, y, z$, representing the second derivatives of the harmonic
function
\begin{equation}
\Gamma(x, y, z) = \gamma_{m} \, \iiint\limits_{\upsilon}
\frac{m(x^{\prime}, y^{\prime}, z^{\prime}) \: d\upsilon^{\prime}}
{\left[ (x-x^{\prime})^2 + (y-y^{\prime})^2 + (z-z^{\prime})^2 \right]^{\frac{1}{2}}} \: .
\label{eq:Gamma-volume-integral}
\end{equation}
In this equation, $\gamma_{m} = 10^{-9} \frac{\mu_{0}}{4 \pi}$ (in H/m),
$\mu_{0}$ is the vacuum magnetic permeability and
$x^{\prime}$, $y^{\prime}$ and $z^{\prime}$ are the coordinates
of the volume element $d \upsilon^{\prime}$, which has total-magnetization intensity
$m(x^{\prime}, y^{\prime}, z^{\prime})$ (in A/m) and is located within the
volume $\upsilon$ of the magnetic sources.
We consider that the total-magnetization intensity $m(x^{\prime}, y^{\prime}, z^{\prime})$
is strictly positive at all points within the magnetic sources. Consequently,
$\Gamma(x, y, z)$ is positive at all points located outside the magnetic sources.
From the mathematical point of view, $\mathbf{M}(x, y, z)$ (equation \ref{eq:M-matrix})
and $\Gamma(x, y, z)$ (equation \ref{eq:Gamma-volume-integral}) resemble, respectively,
the gradient tensor and the corresponding pseudo-gravitational potential that would be produced by the magnetic sources, at the point $(x, y, z)$, if they had a density distribution proportional
to $m(x^{\prime}, y^{\prime}, z^{\prime})$.
Notice that $\mathbf{M}(x, y, z)$ is symmetric, its trace is identically zero at all points
$(x, y, z)$ outside the magnetic sources and it has five independent components that are
themselves harmonic functions \citep{pedersen_rasmussen1990}.
By exploring these properties, we can conveniently rewrite the
total-field anomaly $\Delta T(x, y, z)$ (equation \ref{eq:tfanomaly}) as a linear combination
of five independent harmonic functions as follows:
\begin{equation}
	\begin{split}
		\Delta T(x, y, z) = \:
		& a_{xx} \, \partial_{xx} \Gamma(x, y, z) +
		a_{xy} \, \partial_{xy} \Gamma(x, y, z) +
		a_{xz} \, \partial_{xz} \Gamma(x, y, z) + \\
		& a_{yy} \, \partial_{yy} \Gamma(x, y, z) +
		a_{yz} \, \partial_{yz} \Gamma(x, y, z)
	\end{split} \quad ,
	\label{eq:tfanomaly-alternative}
\end{equation}
where
\begin{equation}
	\begin{split}
		a_{xx} &= m_{x} F_{x} - m_{z} F_{z} \\
		a_{xy} &= m_{x} F_{y} + m_{y} F_{x} \\
		a_{xz} &= m_{x} F_{z} + m_{z} F_{x} \\
		a_{yy} &= m_{y} F_{y} - m_{z} F_{z} \\
		a_{yz} &= m_{y} F_{z} + m_{z} F_{y}
	\end{split}
\label{eq:a-coefficients}
\end{equation}
are constants defined by the elements $F_{\alpha}$ and $m_{\beta}$,
$\alpha = x, y, z$, $\beta = x, y, z$,
of the vectors $\hat{\mathbf{F}}_{0}$ (equation \ref{eq:main_field}) and
$\hat{\mathbf{m}}(\mathbf{q})$ (equation \ref{eq:mag_vec}), respectively.
For simplicity, we have omitted the dependence on the
parameters $I_{0}$ and $D_{0}$ (equation \ref{eq:main_field}) and
$I$ and $D$ (equation \ref{eq:mag_vec}).

Let $\Delta \tilde{T}(x, y, z)$ be the total-field anomaly produced by a
continuous layer of dipoles that have constant magnetization direction defined by
the unit vector $\hat{\mathbf{m}}(\mathbf{q})$ (equation \ref{eq:q_vector})
and are located at the constant depth $z_{c}$. The total-field anomaly produced
by this fictitious layer may be defined as
\begin{equation}
	\Delta \tilde{T}(x, y, z) = \hat{\mathbf{F}}_{0}^{\top}
	\tilde{\mathbf{M}}(x, y, z) \: \hat{\mathbf{m}}(\mathbf{q}) \: ,
	\label{eq:tfanomaly-eqlayer}
\end{equation}
where $\tilde{\mathbf{M}}(x, y, z)$ is a matrix given by
\begin{equation}
	\tilde{\mathbf{M}}(x, y, z) = \begin{bmatrix}
		\partial_{xx} \Phi(x, y, z) &
		\partial_{xy} \Phi(x, y, z) &
		\partial_{xz} \Phi(x, y, z) \\
		\partial_{xy} \Phi(x, y, z) &
		\partial_{yy} \Phi(x, y, z) &
		\partial_{yz} \Phi(x, y, z) \\
		\partial_{xz} \Phi(x, y, z) &
		\partial_{yz} \Phi(x, y, z) &
		\partial_{zz} \Phi(x, y, z)
	\end{bmatrix} \quad ,
	\label{eq:M-matrix-eqlayer}
\end{equation}
with elements
$\partial_{\alpha\beta} \Phi(x, y, z) \equiv
\frac{\partial^{2} \Phi(x, y, z)}{\partial \alpha \partial \beta}$,
$\alpha, \beta = x, y, z$, representing the second derivatives of the harmonic
function
\begin{equation}
	\Phi(x, y, z) = \gamma_{m} \,
	\int\limits_{-\infty}^{+\infty}\int\limits_{-\infty}^{+\infty}
	\frac{p(x'', y'', z_{c}) \: dS''}
	{\left[ (x-x'')^2 + (y-y'')^2 + (z-z_{c})^2 \right]^{\frac{1}{2}}} \: ,
	\quad z_{c} > z \: .
	\label{eq:Phi-surface-integral}
\end{equation}
In this equation, $x''$, $y''$ and $z_{c}$ are the coordinates
of the area element $dS''$, which has magnetic moment per unit area
defined by the function $p(x'', y'', z_{c})$ (in $A$).
Note that $\tilde{\mathbf{M}}(x, y, z)$ (equation \ref{eq:M-matrix-eqlayer}) also represents
a gradient tensor \citep{pedersen_rasmussen1990} and, consequently, it is symmetric,
its trace is identically zero at all points $(x, y, z)$ above the layer (with $z < z_{c}$),
and it has five independent components that are themselves harmonic functions.
These properties also permit rewrite $\Delta \tilde{T}(x, y, z)$
(equation \ref{eq:tfanomaly-eqlayer}) as a linear combination of independent
harmonic functions given by
\begin{equation}
	\begin{split}
		\Delta \tilde{T}(x, y, z) = \:
		& a_{xx} \, \partial_{xx} \Phi(x, y, z) +
		a_{xy} \, \partial_{xy} \Phi(x, y, z) +
		a_{xz} \, \partial_{xz} \Phi(x, y, z) + \\
		& a_{yy} \, \partial_{yy} \Phi(x, y, z) +
		a_{yz} \, \partial_{yz} \Phi(x, y, z)
		\end{split} \quad ,
	\label{eq:tfanomaly-eqlayer-alternative}
\end{equation}
with coefficients $a_{\alpha\beta}$, $\alpha = x, y$, $\beta = x, y, z$, defined by
equation \ref{eq:a-coefficients}.

% Impose that the equivalent layer fit the total-field anomaly data
We know from potential theory that it is possible to find a function $p(x'', y'', z_{c})$
(equation \ref{eq:Phi-surface-integral}) so that the condition
$\Delta T(x, y, z) = \Delta \tilde{T}(x, y, z)$ holds true for all points $(x, y, z)$
located above the fictitious layer of dipoles.
In this case, the layer is called \textit{equivalent layer}.
To investigate the properties of $p(x'', y'', z_{c})$, we must first
observe that, by imposing the aforementioned condition and using equations \ref{eq:tfanomaly-alternative} and \ref{eq:tfanomaly-eqlayer-alternative}, we obtain
\begin{equation}
\begin{split}
a_{xx} \, &\left[\partial_{xx} \Phi(x, y, z) - \partial_{xx} \Gamma(x, y, z) \right] + \\
a_{xy} \, &\left[\partial_{xy} \Phi(x, y, z) - \partial_{xy} \Gamma(x, y, z) \right] + \\
a_{xz} \, &\left[\partial_{xz} \Phi(x, y, z) - \partial_{xz} \Gamma(x, y, z) \right] + \\
a_{yy} \, &\left[\partial_{yy} \Phi(x, y, z) - \partial_{yy} \Gamma(x, y, z) \right] + \\
a_{yz} \, &\left[\partial_{yz} \Phi(x, y, z) - \partial_{yz} \Gamma(x, y, z) \right] = 0
\: , \quad z < z_{c} \: ,
\end{split}
\label{eq:tfanomaly-alternative-equality}
\end{equation}
where the coefficients $a_{\alpha\beta}$ (equation \ref{eq:a-coefficients}),
$\alpha = x, y$, $\beta = x, y, z$, are defined by arbitrary values of
$I_{0}$ and $D_{0}$ (equation \ref{eq:main_field}) and
$I$ and $D$ (equation \ref{eq:mag_vec}).
Because equation \ref{eq:tfanomaly-alternative-equality} is valid
for any possible values of $a_{\alpha\beta}$, which are defined for any
values of $I_{0}$, $D_{0}$, $I$ and $D$, the five linearly independent
harmonic functions in brackets must be identically zero for all points
$(x, y, z)$ above the equivalent layer, where $z < z_{c}$.
By equating each independent function to zero and rewriting the
second derivatives of the surface
integral $\Phi(x, y, z)$ (equation \ref{eq:Phi-surface-integral}), we get
\begin{equation}
\partial_{\alpha\beta} \Gamma(x, y, z) =
\int\limits_{-\infty}^{+\infty}\int\limits_{-\infty}^{+\infty}
p(x'', y'', z_{c}) \: \partial_{\alpha\beta} \frac{1}{r} \:\: dS'' \: ,
\quad z_{c} > z \: ,
\label{eq:D_alpha_beta_Gamma}
\end{equation}
where $(x'', y'', z_{c})$ is a point on the equivalent layer and
$\partial_{\alpha\beta} \frac{1}{r} \equiv
\frac{\partial^{2}}{\partial \alpha \partial \beta} \frac{1}{r}$
represents the second derivative,
with respect to $\alpha = x, y$ and $\beta = x, y, z$, of the inverse distance
function
\begin{equation}
\frac{1}{r} \equiv
\frac{1}{\left[ (x-x'')^2 + (y-y'')^2 + (z-z_{c})^2 \right]^{\frac{1}{2}}} \: .
\label{eq:inverse-distance}
\end{equation}
A possible solution for equation \ref{eq:D_alpha_beta_Gamma} can be obtained
by deriving both sides of
\begin{equation}
\Gamma(x, y, z) =
\int\limits_{-\infty}^{+\infty}\int\limits_{-\infty}^{+\infty}
p(x'', y'', z_{c}) \: \frac{1}{r} \:\: dS'' \: ,
\quad z_{c} > z \: .
\label{eq:Gamma_integral_equation}
\end{equation}
Notice that the function $p(x'', y'', z_{c})$ that solves this
integral equation for $\Gamma(x, y, z)$ (equation \ref{eq:Gamma_integral_equation})
also solves the integral equations for the second derivatives
$\partial_{\alpha\beta} \Gamma(x, y, z)$ (equation \ref{eq:D_alpha_beta_Gamma}).
It can be shown (see Appendix A) that equation \ref{eq:Gamma_integral_equation}
has a solution
\begin{equation}
p(x'', y'', z_{c}) = \frac{1}{2\pi} \partial_{z} \Gamma(x'', y'', z_{c}) \: ,
\label{eq:positivity_prop}
\end{equation}
where, according to equation \ref{eq:Gamma-volume-integral},
\begin{equation}
\partial_{z} \Gamma(x'', y'', z_{c}) = \gamma_{m} \, \iiint\limits_{\upsilon}
\frac{m(x^{\prime}, y^{\prime}, z^{\prime}) (z^{\prime} - z_{c}) \:
	d\upsilon^{\prime}}
{\left[ (x''-x^{\prime})^2 + (y''-y^{\prime})^2 + (z_{c}-z^{\prime})^2 \right]^{\frac{3}{2}}} \: , \quad z^{\prime} > z_{c} \: .
\label{eq:DzGamma-volume-integral}
\end{equation}
From the physical point of view, equation \ref{eq:DzGamma-volume-integral}
represents the vertical component of the gravitational attraction
(or the pseudogravity anomaly) that would be produced by the magnetic sources,
on the equivalent layer, if they had a density distribution proportional to
$m(x^{\prime}, y^{\prime}, z^{\prime})$.
Since $m(x^{\prime}, y^{\prime}, z^{\prime})$ is strictly positive
at all points $(x^{\prime}, y^{\prime}, z^{\prime})$ within the magnetic sources,
$\partial_{z} \Gamma(x'', y'', z_{c})$ is positive at all points
$(x'', y'', z_{c})$ located on the equivalent layer.

The most interesting aspect of magnetic-moment distribution $p(x'', y'', z_{c})$
(equation \ref{eq:positivity_prop}) is that it is defined as the product of a
positive constant $\frac{1}{2\pi}$ and the
function $\partial_{z} \Gamma(x'',y'',z_{c})$, which is strictly
positive at all points $(x'',y'',z_{c})$ on the equivalent layer.
Hence, $p(x'', y'', z_{c})$ is strictly positive at all points on the
equivalent layer as well.
This relation is similar to that presented by \cite{pedersen1991} and
\cite{li_etal_2014}. They determine, in the wavenumber domain, the
magnetic-moment distribution within a continuous equivalent layer
vertically magnetized by induction.
They also consider a planar equivalent layer located below and parallel to a
horizontal plane containing the observed total-field anomaly data.
Under these assumptions, \cite{pedersen1991} and \cite{li_etal_2014}
conclude that the magnetic-moment distribution within the continuous equivalent
layer is all positive and proportional to the pseudogravity anomaly produced by
the source on the plane of the equivalent layer.
Here, we do not follow the same wavenumber-domain
reasoning used by those authors. Moreover, equation \ref{eq:positivity_prop}
generalizes this positivity condition because (1) it holds true for all cases
in which the magnetization of the equivalent layer has the same direction as
the true total-magnetization of the sources, whether it is purely induced or not,
and (2) it does not require that the observed total-field anomaly data be on
a plane.

%% Discretizing layer
\subsection{Parametrization and forward problem}

In practical situations, it is not possible to determine a continuous magnetic-moment
distribution $p(x'',y'',z_{c})$ (equation \ref{eq:positivity_prop}) over the
equivalent layer.
For this reason, the layer has to be approximated by a discrete set of dipoles
(the equivalent sources) with unit volume located at the constant depth $z = z_c$.
The total-field anomaly produced by this discrete layer (the predicted total-field anomaly)
at a given point $(x_{i}, y_{i}, z_{i})$, $i = 1, \dots, N$ (Figure \ref{fig:eqlayer_figure}),
is given by
\begin{equation}
\Delta T_{i}(\mathbf{s}) = \mathbf{g}_{i}(\mathbf{q})^{\top} \mathbf{p},
\label{eq:tfa_pred_i}
\end{equation}
where $\mathbf{s}$ is an $(M + 2) \times 1$ partitioned vector (the parameter vector) given by
\begin{equation}
	\mathbf{s} = \begin{bmatrix}
		\mathbf{p} \\
		\mathbf{q}
	\end{bmatrix} \: ,
	\label{eq:parameter-vector}
\end{equation}
$\mathbf{q}$ is the magnetization direction vector (equation \ref{eq:q_vector}), $\mathbf{p}$ is an
$M \times 1$ vector (the magnetic-moment vector) whose $j$th element, $j = 1, \dots, M$, is the
magnetic moment intensity $p_{j}$ (in A~m$^{2}$) of the $j$th dipole and
$\mathbf{g}_{i} (\mathbf{q})$ is another $M \times 1$ vector whose $j$th element is defined
by the harmonic function
\begin{equation}
g_{ij} (\mathbf{q})  = \gamma_m \hat{\mathbf{F}}_{0}^T \,
\mathbf{M}_{ij} \, \hat{\mathbf{m}}(\mathbf{q}) \: .
\label{eq:g_ij}
\end{equation}
In this equation, $\mathbf{M}_{ij}$ is a $3 \times 3$ matrix given by
\begin{equation}
\mathbf{M}_{ij} = \begin{bmatrix}
\partial_{xx} \frac{1}{r} &
\partial_{xy} \frac{1}{r} &
\partial_{xz} \frac{1}{r} \\
\partial_{xy} \frac{1}{r} &
\partial_{yy} \frac{1}{r} &
\partial_{yz} \frac{1}{r} \\
\partial_{xz} \frac{1}{r} &
\partial_{yz} \frac{1}{r} &
\partial_{zz} \frac{1}{r}
\end{bmatrix} \quad ,
\label{eq:Mij-matrix}
\end{equation}
where $\partial_{\alpha\beta} \frac{1}{r} \equiv
\frac{\partial^{2}}{\partial \alpha \partial \beta} \frac{1}{r}$
represent the second derivatives,
with respect to $\alpha = x, y, z$ and $\beta = x, y, z$, of the inverse distance
$\frac{1}{r}$ (equation \ref{eq:inverse-distance}) between the coordinates of the
observation points $(x, y, z) = (x_{i}, y_{i}, z_{i})$ and the coordinates of the
equivalent sources $(x'', y'', z_{c}) = (x_{j}, y_{j}, z_{c})$
(Figure \ref{fig:eqlayer_figure}).
Equations $\ref{eq:tfa_pred_i}$-$\ref{eq:Mij-matrix}$ show that the predicted total-field anomaly
$\Delta T_{i}(\mathbf{s})$ has a linear relation with the magnetic-moment vector $\mathbf{p}$
and a nonlinear relation with the magnetization direction vector $\mathbf{q}$
(equation \ref{eq:q_vector}).

\subsection{Inverse problem}

%%%% Defining the objective function
Let $\mathbf{\Delta T}^{o}$ be the observed-data vector whose $i$th element $\Delta T_{i}^{o}$
is the observed total-field anomaly produced by the magnetic sources at the point
$(x_{i},y_{i},z_{i})$, $i = 1, \dots, N$ (Figure \ref{fig:eqlayer_figure}).
Similarly, let $\mathbf{\Delta T} (\mathbf{s})$ be the predicted-data vector whose $i$th element
$\Delta T_{i}(\mathbf{s})$ (equation \ref{eq:tfa_pred_i}) is the predicted total-field anomaly
produced by the discrete equivalent layer at the same point $(x_{i},y_{i},z_{i})$.
To estimate the parameter vector $\mathbf{s}$ (equation \ref{eq:parameter-vector})
minimizing the difference between $\mathbf{\Delta T}^{o}$ and
$\mathbf{\Delta T}(\mathbf{s})$, we solve the following inverse problem:
\begin{subequations}
	\begin{align}
	& \text{minimizing}
	& &\Psi(\mathbf{s}) =\lVert \mathbf{\Delta T}^{o} - \mathbf{\Delta T} (\mathbf{s})
	\rVert_{2}^{2} + \, \mu f_0 \parallel \mathbf{p} \parallel_{2}^{2} \: , \\
	& \text{subject to}
	& & \mathbf{p} \geqslant \mathbf{0} \: .
	\end{align}
	\label{eq:positivity_goal_function}
\end{subequations}
On the right side of equation \ref{eq:positivity_goal_function}a, the first
and second terms are
the data-misfit function and the zeroth-order Tikhonov regularization
function, $\mu$ is the regularizing parameter, $\| \cdot \|_{2}^{2}$ represents
the squared Euclidean norm and $f_0$ is a normalizing factor.
This factor makes a trade-off
between the data-misfit and zeroth-order Tikhonov regularization functions.
In the inequality \ref{eq:positivity_goal_function}b, $\mathbf{0}$ is an
$M \times 1$ vector with all elements equal to zero and the inequality sign
is applied element
by element. This positivity constraint on the magnetic-moment vector $\mathbf{p}$
is incorporated by using the nonnegative least squares (NNLS) proposed by
\cite{lawson_hanson_1974}.

%% Deducing the equations
To solve this constrained inverse problem, let us first consider the following
second-order expansion of the goal function (equation \ref{eq:positivity_goal_function}a)
around $\mathbf{s} = \mathbf{s}^{k}$ (equation \ref{eq:parameter-vector}):
\begin{equation}
\Psi(\mathbf{s}^{k} + \mathbf{\Delta s}^{k}) \approx \Psi(\mathbf{s}^{k}) +
{\mathbf{J}^{k}}^{\top} \mathbf{\Delta s}^{k} +
\frac{1}{2} {\mathbf{\Delta s}^{k}}^{\top} \mathbf{H}^{k} \mathbf{\Delta s}^{k}  \: ,
\label{eq:sec_ord_goal}
\end{equation}
where $\mathbf{\Delta s}^{k}$ is a perturbation on the parameter vector
and the terms $\mathbf{J}^{k}$ and $\mathbf{H}^{k}$ are, respectively, the gradient vector
and the Hessian matrix evaluated at $\mathbf{s}^{k}$.
Then, we estimate the perturbation vector $\bar{\mathbf{\Delta s}}^k$ that minimizes the
expanded function (equation \ref{eq:sec_ord_goal}) by taking the gradient with respect
to $\mathbf{\Delta s}^k$ and setting the result equal to the null vector.
This procedure leads to the linear system
\begin{equation}
\mathbf{H}^{k} \bar{\mathbf{\Delta s}}^{k} = - \mathbf{J}^{k} \: ,
\label{eq:linear_sys_GN}
\end{equation}
which represents the $k$th step of the Gauss-Newton method \citep{aster2005} for minimizing
our goal function (equation \ref{eq:positivity_goal_function}a).
We rewrite this linear system by neglecting the cross-derivatives in the Hessian matrix
as follows:
\begin{equation}
\left[
\begin{array}{c|c}
\mathbf{H}_{pp}^{k} & \mathbf{0} \\
\hline
\mathbf{0}^{\top} & \mathbf{H}_{qq}^{k}
\end{array}
\right] \left[ \begin{array}{c}
\bar{\mathbf{\Delta p}}^{k} \\
\bar{\mathbf{\Delta q}}^{k}
\end{array} \right] \approx -\left[ \begin{array}{c}
\mathbf{J}_{p}^{k} \\
\mathbf{J}_{q}^{k}
\end{array} \right] ,
\label{eq:linear_sys_GN_block}
\end{equation}
in which $\mathbf{0}$ is an $M \times 2$ matrix containing all elements equal to zero,
$\bar{\mathbf{\Delta p}}^{k} = \bar{\mathbf{p}}^{k+1} - \bar{\mathbf{p}}^{k}$
is a correction on the magnetic-moment vector $\mathbf{p}$,
$\bar{\mathbf{\Delta q}}^{k} = \bar{\mathbf{q}}^{k+1} - \bar{\mathbf{q}}^{k}$
is a correction on the magnetization direction $\mathbf{q}$ and the terms
$\mathbf{J}_{\alpha}^{k}$ and $\mathbf{H}_{\alpha \alpha}^{k}$, $\alpha = p,q$,
are the gradient vector and the Hessian matrix calculated with respect to the elements of
$\mathbf{p}$ and $\mathbf{q}$, respectively.
The gradient vector $\mathbf{J}_{p}^{k}$ and the Hessian matrix $\mathbf{H}_{pp}^{k}$
(equation \ref{eq:linear_sys_GN_block}) related to the magnetic-moment vector
$\mathbf{p}$ (equation \ref{eq:parameter-vector}) are, respectively,
\begin{equation}
\mathbf{J}_{p}^{k} = -2 {\mathbf{G}_{p}^{k}}^{\top}
\left[ \mathbf{\Delta T}^{o} - \mathbf{\Delta T} (\bar{\mathbf{s}}^{k}) \right] +
2\mu f_{0}^{k} \bar{\mathbf{p}}^{k}
\label{eq:grad_p}
\end{equation}
and
\begin{equation}
\mathbf{H}_{pp}^{k} = 2 {\mathbf{G}_{p}^{k}}^{\top} \mathbf{G}_{p}^{k} +
2 \mu f_{0}^{k} \mathbf{I} \: ,
\label{eq:hess_p}
\end{equation}
where $\mathbf{G}_p^{k}$ is an $N \times M$ matrix whose $ij$th element is given by the harmonic
function $g_{ij}(\bar{\mathbf{q}}^{k})$ (equation \ref{eq:g_ij}) evaluated at the
magnetization direction $\bar{\mathbf{q}}^{k}$, $\mathbf{I}$ is the $M \times M$ identity matrix and
$f_{0}^{k}$ is a  normalizing factor equal to
\begin{equation}
f_{0}^{k} = \dfrac{trace \left({\mathbf{G}_{p}^{k}}^{\top} \mathbf{G}_{p}^{k} \right)}{M} \, .
\label{eq:norm_factor}
\end{equation}
This factor is used with the purpose of making a trade-off between the terms
forming the gradient vector $\mathbf{J}_{p}^{k}$ (equation \ref{eq:grad_p}) and
the Hessian matrix $\mathbf{H}_{pp}^{k}$ (equation \ref{eq:hess_p})
along the iterative process. The gradient vector $\mathbf{J}_{q}^{k}$ and
the Hessian matrix $\mathbf{H}_{qq}^{k}$
(equation \ref{eq:linear_sys_GN_block}) related to the magnetization direction
$\mathbf{q}$ (equation \ref{eq:q_vector}) are, respectively,
\begin{equation}
\mathbf{J}_{q}^{k} = -2 {\mathbf{G}_{q}^{k}}^{\top}
\left[ \mathbf{\Delta T}^{o} - \mathbf{\Delta T} (\bar{\mathbf{s}}^{k}) \right]
\label{eq:grad_q}
\end{equation}
and
\begin{equation}
\mathbf{H}_{qq}^{k} \approx 2 {\mathbf{G}_{q}^{k}}{^\top} \mathbf{G}_{q}^{k} \: ,
\label{eq:hess_q}
\end{equation}
in which $\mathbf{G}_{q}^{k}$ is a $N \times 2$ matrix given by
\begin{equation}
\mathbf{G}_{q}^{k} = \begin{bmatrix}
\partial_{I} \mathbf{g}_{1}(\bar{\mathbf{q}}^{k})^{\top} \bar{\mathbf{p}}^{k} &
\partial_{D} \mathbf{g}_{1}(\bar{\mathbf{q}}^{k})^{\top} \bar{\mathbf{p}}^{k} \\
\vdots & \vdots  \\
\partial_{I} \mathbf{g}_{N}(\bar{\mathbf{q}}^{k})^{\top} \bar{\mathbf{p}}^{k} &
\partial_{D} \mathbf{g}_{N}(\bar{\mathbf{q}}^{k})^{\top} \bar{\mathbf{p}}^{k}
\end{bmatrix} \: ,
\label{eq:Gq}
\end{equation}
where
$\partial_{\alpha} \mathbf{g}_{i}(\bar{\mathbf{q}}^{k}) \equiv
\frac{\partial \mathbf{g}_{i}(\bar{\mathbf{q}}^{k})}{\partial \alpha}$, $\alpha= I, D$,
represent the first derivatives of vector
$\mathbf{g}_{i}(\bar{\mathbf{q}}^{k})$ (equation \ref{eq:tfa_pred_i}) with respect to the
inclination $I$ and the declination $D$ of the total magnetization of the sources.

\subsection{Iterative algorithm for solving the inverse problem}

The iteration $k = 0$ of our algorithm starts with an initial guess
$\bar{\mathbf{q}}^{k} = \bar{\mathbf{q}}^{0}$ for the direction vector $\mathbf{q}$
(equation \ref{eq:q_vector}).
By using this $\bar{\mathbf{q}}^{k}$, the upper part of equation
\ref{eq:linear_sys_GN_block} leads to the following linear system for the magnetic-moment
vector:
\begin{equation}
\left[ {\mathbf{G}_{p}^{k}}^{\top} \mathbf{G}_{p}^{k} +
\mu f_{0}^{k} \mathbf{I} \right] \bar{\mathbf{p}}^{k} = {\mathbf{G}_{p}^{k}}^{\top} \mathbf{\Delta T}^{o} \: .
\label{eq:linear_sys_p}
\end{equation}
To impose the positivity constraint (equation \ref{eq:positivity_goal_function}b) on the
magnetic-moment distribution $\bar{\mathbf{p}}^{k+1}$ within the equivalent layer,
we solve this linear system (equation \ref{eq:linear_sys_p}) by using the nonnegative least
squares (NNLS) method \citep{lawson_hanson_1974, silvadias_etal_2007}.
This positive magnetic-moment distribution is then used to estimate a correction
$\bar{\mathbf{\Delta q}}^{k}$ on the magnetization direction by solving the following
unconstrained nonlinear system via Levenberg-Marquardt method \citep{aster2005}:
\begin{equation}
\left[ {\mathbf{G}_{q}^{k}}^{\top} \mathbf{G}_{q}^{k} + \lambda \, \mathbf{I} \right]
\bar{\mathbf{\Delta q}}^{k} = {\mathbf{G}_{q}^{k}}^{\top}
\left[ \mathbf{\Delta T}^{o} - \mathbf{\Delta T} (\mathbf{s}^{k}) \right] \: ,
\label{eq:linear_sys_q}
\end{equation}
where $\lambda$ is the Marquardt parameter and $\mathbf{I}$ is the identity matrix.
After estimating the correction $\bar{\mathbf{\Delta q}}^{k}$
at the $k$th iteration, we update the magnetization direction as follows
\begin{equation}
\bar{\mathbf{q}}^{k+1} = \bar{\mathbf{q}}^{k} + \bar{\mathbf{\Delta q}}^{k} \: ,
\label{eq:q_next}
\end{equation}
use it as input for estimating a new positive magnetic-moment distribution
with equation \ref{eq:linear_sys_p} and so on.
The iterative process stops when the goal function (equation \ref{eq:positivity_goal_function}a)
is invariant along successive iterations.
We show in Appendix B that our method fails if the sources are vertically magnetized.

\subsection{The choice of layer depth $\mathbf{z_{c}}$ and regularization parameter $\mathbf{\mu}$}

The procedure for the use of our methodology for estimating the total magnetization
require the choice of two main parameters. The first one is the layer depth $z_c$
(Figure \ref{fig:eqlayer_figure}) and the second is the regularization parameter
$\mu$ (equation \ref{eq:linear_sys_p}).

There is a classical criterion proposed by \cite{dampney1969} to define the layer
depth based on the horizontal data sampling. This criterion states that the
distance between the plane containing the data and the
plane defining the layer should vary from $2.5$ to $6.0$ times the horizontal
data sampling. This criterion, however, is valid for evenly spaced data.
Here, we define the layer depth $z_c$ by using the horizontal space between
adjacent flight lines of an airborne survey. We found empirically that $z_c$
can vary from $2$ to $3$ times the spacing between adjacent flight lines.
Notice that the range we found empirically is smaller than that proposed by
Dampney. Apparently, this is due to the fact that, in an airborne survey,
the data sampling along lines is smaller than the space between lines.

To solve the equation \ref{eq:linear_sys_p} we have to choose a reliable
regularization parameter $\mu$. For this purpose, we use the L-curve method
\citep{hansen1992}. This approach is widely used in the literature to find a
regularizing parameter, which filters out enough noise without losing too much
information in the final solution. The procedure of finding the parameter plot a
curve of optimal values between the solution and residual norms. The corner of
the curve is the optimal regularization parameter which establishes a trade-off
between the regularizing and data-misfit functions.


%&LaTeX

\section{To Infinity and Response!}

By the end of this lab you should feel comfortable manipulating and
using feedback filters for simple problems. You should also be
comfortable with the concept of a filter with an infinite impulse
response. All feedback filters have an infinite impulse response
and are also known as IIR filters. Feedback filters use the previous
outputs of the filter, feeding them back to compute the output for the
current sample. The ``fed back'' outputs are weighted by coefficients,
$a_\ell$.

\subsection{A Note About Matlab Filter Coefficients}

Note that the Matlab \verb|filter| function uses \emph{negative}
values for the $a_\ell$ (feedback) coefficients, from the transfer
function. In other words, in the text, a second-order feedback
filter's defining equation might be:
\begin{align}
  y[n] &= a_1 y[n-1] + a_2 y[n-2] + b_0 x[n] \label{eq:def} \\
  y[n] -a_1 y[n-1] - a_2 y[n-2]&=   b_0 x[n]
\end{align}
This yields the transfer function:
\begin{align}
  Y(z)(1 - a_1 z^{-1} - a_2 z^{-2}) &= b_0X(z) \\
  Y(z)/X(z) &= \frac{b_0}{1 - a_1 z^{-1} - a_2 z^{-2}} \\
  H(z) &= \frac{b_0}{1 - a_1 z^{-1} - a_2 z^{-2}} \label{eq:trans}
\end{align}
The coefficients used by \verb|filter|, rather than being the $a_\ell$
from the defining equation~(\ref{eq:def}) are the \emph{negative}
$a_\ell$ from the transfer function~(\ref{eq:trans}) --- the ratio of
two polynomials. In other words, to properly compute the above filter
in Matlab, you will need to use:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
a = [1.0 -a1 -a2];
b = [b0];
y = filter(b, a, x);
\end{lstlisting}
Note also that in this example the filter includes the $a[0]$
coefficient (of course, per Matlab one-based indices, as \verb|a(1)|),
which we will always leave as 1.0 (it's the first ``1'' in the
denominator of the transfer function).

And finally, note that one form of the \verb|filter| function takes a
fourth argument, which is the initial conditions for the feedback
delays (used in computing the output values that have delay terms
which come before the first value in the $x$ vector). These default to
all zero if not specified.

\subsection{Feedback Filters as Recurrence Relations}

You may notice that the defining equation for a feedback filter is in
the form of a recurrence relation. In fact, we can use a feedback
filter to implement a recurrence relation if we set the input to be
an impulse, $x[n] = C \delta[n]$, with amplitude $C$ being the initial
value for the iteration. Let's start out with the Fibonacci sequence,
which you'll remember to be:
\begin{equation}
  F[n] = \left\{ \begin{array}{ll}
      1 & n < 2 \\
      F[n-1] + F[n-2] & n \geq 2
    \end{array} \right.
\end{equation}

We can rewrite this recurrence relation as:
\begin{equation}
  y[n] = y[n-1] + y[n-2] + x[n]
  \label{eq:fib}
\end{equation}
and we will get the Fibonacci sequence \emph{if} we input an impulse
(hence, the appearance of the $x[n]$ on the right hand side, which
serves only to initialize the filter). In Matlab, this can be done
trivially by taking a vector of all zeros --- let's call this vector
\verb|x| --- and setting its first value only (\verb|x(1)|) to
$C$. This is also a very good demonstration of the first ``I'' in the
acronym ``IIR'': the impulse response of this filter has infinite
duration.

\paragraph{Step 1.1} If we set $x[n]=\delta[n]$ in~(\ref{eq:fib}), we
should see that the impulse response of this filter is indeed the
Fibonacci sequence. Implement this filter in Matlab and verify that its
impulse response is the Fibonacci sequence. What are the values of the
coefficients that you used?

\paragraph{Step 1.2} What is the value for $n=19$ (\verb|y(20)| in
Matlab)?

\paragraph{Step 1.3} Is this filter stable?

\paragraph{Step 1.4} Let's do something similar with the recurrence
relation for computing the series $y[n] = 1/3^n$ in the text (as
always, remember that Matlab indices start at 1). Set the
coefficients for a feedback filter to implement equation~(5-43) in the
text, $y[n] = 1/3 y[n-1] + x[n]$. What are the filter coefficients?

\paragraph{Step 1.5} What are the pole location(s) for this filter?

\paragraph{Step 1.6} Now use Matlab to calculate the impulse repsonse.
Set the amplitude of the input impulse to be 0.99996. Is this filter
stable?  Is its impulse response consistent with the result of
iterating equation~(5-43) in the textbook?

\subsection{Telephone Touch Tone Dialing}
Telephone touch pads generate dual tone multi frequency (DTMF) signals
to dial a telephone. When any key is pressed, the tones of the
corresponding column and row in the table below are generated, hence
it is a ``dual tone'' code. As an example, pressing the 5 button
generates the tones 770Hz and 1336Hz summed together.

\begin{center}
  \begin{tabular}{l|ccc}
    & 1209Hz & 1336Hz & 1477Hz \\ \hline
    697Hz &   1    &   2    &   3    \\
    770Hz &   4    &   5    &   6    \\
    852Hz &   7    &   8    &   9    \\
    941Hz &   $\ast $    &   0    &   \#
  \end{tabular}
\end{center}

The frequencies in the table above were chosen to avoid harmonics. No
frequency is a multiple of another, the difference between any two
frequencies does not equal any of the frequencies, and the sum of any
two frequencies does not equal any of the frequencies.\footnote{More
  information can be found at:
  \url{http://en.wikipedia.org/wiki/DTMF}} This makes it easier to
detect exactly which tones are present in the dial signal in the
presence of line distortions.

It is possible to decode such a signal by first using a \emph{filter
  bank} composed of seven bandpass filters, one for each of the
frequencies above. When a button is pressed, it will produce a
combination of two tones, and thus, at the decoder end, two of the
bandpass filters will produce significantly higher outputs than the
others. A good measure of the output levels is the average power at
the filter outputs. This is calculated by squaring the filter outputs
and averaging over a short time interval.

\paragraph{Step 2.1} First of all, please write a Matlab
\verb|DTMFCoder| function. This function should take in one argument
--- a telephone key number --- and return a digital waveform
containing the appropriate summed tones. Internally, it should do this
by generating AnalogSignals, summing them, and then sampling them at
8kHz and quantizing them at 16 bits. DTMF signal duration should be
1s. For each of the seven tone frequencies in Hz, what is the
corresponding digital frequency in the range $[0, \pi]$?

\paragraph{Step 2.2} In this step, please construct a bandpass filter
for the 697Hz tone. Use a feedback filter with complex conjugate
poles. Locate these complex conjugate poles at the correct location
for $\pm$697Hz. Use equation~(5-38) of section~5.1.4 of the text to
set the radius of those poles so that the closest other tone
frequency, 770Hz, lies outside the passband (in other words, to set
the bandwidth so that it is significantly smaller than twice the
difference between 697Hz and 770Hz). What were your pole locations?

Compute the corresponding filter coefficients. You can verify your
filter performance by plotting its frequency response.

Verify that the filter output for \verb|DTMFCoder| output for keys 1,
2, and 3 are pretty much identical, and that all other buttons produce
much lower amplitude output.  In your report, include a plot of the
filter output for one of the buttons 1, 2, or 3 and a plot for one of
the buttons 4, 5, or 6.

\paragraph{Step 2.3} Now we are ready to decide whether a particular
frequency is present. Write a Matlab \verb|RMS| function that takes a
vector as input and returns a scalar root mean squared value for it
--- this function should square each value of the vector, take the
mean of those squares, and then take the square root of that
mean. Determine the RMS filter output for telephone buttons 1, 2, and
3 and compare them to the other phone buttons. You should see a much
higher value for 1, 2, and 3 than the other buttons; additionally, the
RMS values for those three buttons should be almost identical. What
are the RMS values you get for pressing 1 versus 4?

\paragraph{Step 2.4} Now we will assemble a filter bank. Implement
filters in Matlab for each of the six other DTMF frequencies and
verify that they work as expected. Now, write a Matlab function
\verb|DTMFDecoder| that takes a single input --- a vector (for which
you will use the \verb|DTMFCoder| output).  \verb|DTMFDecoder| should
compute the RMS value of the output of each of the seven filters in
the filter bank. It should output (to the Matlab console) these RMS
values, and then detect the two highest values. It should use a lookup
table or equivalent logic to decode which button was ``pressed,'' and
output (to the Matlab console) that button.

% LocalWords:  WebQ MATLAB DSP


\subsection{The Metropolis-Hastings algorithm}

\subsubsection{The Metropolis-Hastings algorithm}

The Metropolis-Hastings algorithm creates a set of samples \(x\) such that the distribution of the samples approaches the goal distribution.

\subsubsection{Initialisation}

The algorithm takes an arbitrary starting sample \(x_0\). It then must decide which sample to consider next.

\subsubsection{Generation}

It does this using a Markov chain. That is, there is a map \(g(x_j, x_i)\).

This distribution is generally a normal distribution around \(x_i\), making the process a random walk.

\subsubsection{Acceptance}

Now we have a considered sample, we can either accept or reject it. It is this step that makes the end distribution approximage the function.

We accept if \(\dfrac{f(x_j)}{f(x_i)}>u\), where \(u\) is a random variable between \(0\) and \(1\), generated each time.

We can calculate this because we know this function.

\subsubsection{Properties}


%================================
\section{Compactness}
%================================

%--------------------------------
\begin{definition}
	[compactness]
	\label{def: compactness}
	A topological space $(X, \mathcal T)$ is said to be \textit{compact} iff every open cover of $X$ has a finite subcover. That is,
	$$
	\forall \mathcal C \subseteq \mathcal T : \bigcup \mathcal C = X : \exists \mathcal S \subseteq \mathcal C : \bigcup \mathcal S = X : |\mathcal S| < \aleph_0.
	$$
\end{definition}
%--------------------------------


\subsection{L'Hôpital's rule}

\subsubsection{L'Hôpital's rule}

If there are two functions which are both tend to \(0\) at a limit, calculating the limit of their divisor is hard. We can use L'Hopital's rule.

We want to calculate:

\(\lim_{x\rightarrow c}\dfrac{f(x)}{g(x)}\)

This is:

\(\lim_{x\rightarrow c}\dfrac{f(x)}{g(x)}=\lim_{x\rightarrow c}\dfrac{\dfrac{f(x)-0}{\delta}}{\dfrac{g(x)-0}{\delta}}\)

If:

\(\lim_{x\rightarrow c}f(x)=\lim_{x\rightarrow c}g(x)=0\)

Then

\(\lim_{x\rightarrow c}\dfrac{f(x)}{g(x)}=\lim_{x\rightarrow c}\dfrac{\dfrac{f(x)-f(c)}{\delta}}{\dfrac{g(x)-f(c)}{\delta}}\)

\(\lim_{x\rightarrow c}\dfrac{f(x)}{g(x)}=\dfrac{f'(x)}{g'(x)}\)


\section{Classification of Some Small Finite Groups}
Consider
$$\underline{1}=
\begin{pmatrix}
    1&0\\
    0&1
\end{pmatrix}
\underline{i}=
\begin{pmatrix}
    i&0\\
    0&-i
\end{pmatrix}
\underline{j}=
\begin{pmatrix}
    0&1\\
    -1&0
\end{pmatrix}
\underline{k}=
\begin{pmatrix}
    0&i\\
    i&0
\end{pmatrix}
$$
One can check that
$Q_8=\{\pm\underline{1},\pm\underline{i},\pm\underline{j},\pm\underline{k}\}$ is a group under matrix multiplication, we also have $\underline{i}^2=\underline{j}^2=\underline{k}^2=\underline{1},\underline{i}\underline{j}=\underline{k}=-\underline{j}\underline{i},\underline{j}\underline{k}=\underline{i}=-\underline{k}\underline{j},\underline{k}\underline{i}=\underline{j}=-\underline{i}\underline{k}$
\begin{definition}
    If $G,H$ are groups, then we can have the product group $G\times H$ under the group operation
    $$(g_1,h_1)(g_2,h_2)=(g_1g_2,h_1h_2)$$
\end{definition}
One can check that $(G\times H)\times K\cong G\times (H\times K)$.
\begin{theorem}[Chinese Remainder Theorem]\label{crt}
    If $m,n\in\mathbb N$ such that $m,n\ge 2$ and $(m,n)=1$, then the following function $\phi: \mathbb Z_{mn}\to\mathbb Z_m\times\mathbb Z_n$ defined by
    $$a\mapsto (a\bmod m,a\bmod n)$$
    is an isomorphism.
\end{theorem}
\begin{proof}
    It is trivial that $\phi$ is well-defined and is a homomorphism.
    Both groups has the same size, so it suffices to show that injectivity.
    To see it, consider $\ker\phi$.
    If $a\in\ker\phi$, then $a\equiv 0\pmod{m}$ and $a\equiv 0\pmod{n}$, so $a\equiv 0\pmod{mn}$.
    So the kernel of $\phi$ is trivial, hence it is injective.
\end{proof}
\begin{theorem}\label{direct_product_thm}
    Let $H_1,H_2\le G$, then if\\
    1. $H_1\cap H_2=\{e\}$\\
    2. $h_1h_2=h_2h_1$ for any $h_1\in H_1,h_2\in H_2$.\\
    3. $\forall g\in G,\exists h_1\in H_1, h_2\in H_2, h_1h_2=g$.\\
    Then $G\cong H_1\times H_2$.
\end{theorem}
\begin{proof}
    Consider the map $\phi:H_1\times H_2\to G$ such that $\phi:(h_1,h_2)\mapsto h_1h_2$.
    It is a homomorphism by 2, indeed, if $h_1,h_1'\in H_1, h_2,h_2'=H_2$, then
    \begin{align*}
        \phi(h_1,h_2)\phi(h_1',h_2')=h_1h_2h_1'h_2'=h_1h_1'h_2h_2'\\
        =\phi(h_1h_1',h_2h_2')=\phi((h_1,h_2)(h_1',h_2'))
    \end{align*}
    Its surjectivity is implied by 3.
    If $\phi(h_1,h_2)=e$, then $h_1h_2=e\implies H_1\ni h_1=h_2^{-1}\in H_2$, so $h_1=h_2=e$, so $\ker\phi=\{e\}$, thus it is injective.
    Therefore it is a isomorphism.
\end{proof}
we now wish to classify finite groups of order at most $8$.
For $|G|=1,2,3,5,7$, we already know that $G$ would be cyclic, so it remains to find those in $4,6,8$.
\begin{theorem}
    For a finite group $G$ such that each element is with order $2$, then we know that $|G|$ is even, also it is isomorphic to the direct product of $C_2$'s.
\end{theorem}
\begin{proof}
    We know that $G$ is abelian.
    \footnote{Proved a long time ago in example sheet.}
    So it is done by Theorem \ref{direct_product_thm} and the associativity of group direct products (up to isomorphism).
\end{proof}
Thus, for $|G|=4$, either there is an element of order $4$, in which case $G\cong C_4$, or every element has order $2$, where we have $G\cong K_4:=(C_2)^2=C_2\times C_2$.
For $|G|=6$, then if there is an element of order $6$, then $G\cong C_6$, otherwise there is an element $r$ of order $3$ and $s$ of order $2$ (by Theorem \ref{cauchy}) such that $sr\neq rs$ (since if so them $G\cong C_2\times C_3\cong C_6$ by Theorem \ref{direct_product_thm} and Theorem \ref{crt}).
But the elements $\{e,s,r,r^2,rs,r^2s\}$ are distinct, by inspection we must have $sr=r^2s$, but this would give the full definition of the group operation which is identical to that of the Dihedral group on a $3$-gon (aka equilateral triangle), so $G\cong D_6\cong S_3$.\\
Groups of order $8$ is a little bit more complicated.
\begin{claim}
    There are only $5$ groups of order $8$, and they are
    $$C_8,C_4\times C_2,(C_2)^3,D_8,Q_8$$
    up to isomorphism.
\end{claim}
\begin{proof}
    $C_8,C_4\times C_2, C_2\times C_2\times C_2$ are Abelian, and $D_8,Q_8$ are not.
    Furthermore, by looking at the order of elements, $C_8,C_4\times C_2, C_2\times C_2\times C_2$ are all distinct.
    And by essentially the same method, $D_8,Q_8$ are distinct as well, as $Q_8$ has only one element of order $2$ but $D_8$ has more.\\
    So it remains to show that every group of order $8$ is one of them.\\
    Let $G$ be the group.
    Since $|G|=8$, any element of $G$ must have orders $1,2,4,8$.
    If it has element of order $8$, then $G\cong C_8$; if all its elements are of order $2$, then $G\cong (C_2)^3$.
    Assume henceforth that $G$ has at least one element $f$ of order $4$ but none of order $8$.
    Let $g\notin\langle f\rangle$, so $G=\langle f\rangle\cup g\langle f\rangle$, in order words
    $$G=\{e,f,f^2,f^3,g,gf,gf^2,gf^3\}$$
    Note that $g^2\notin g\langle f\rangle$, thus $g^2=e$ or $g^2=f^2$ since $g$ does not have order $8$.\\
    If $g^2=e$, then $fg=gf\implies G\cong C_4\times C_2$ by Theorem \ref{direct_product_thm}, otherwise we have $fg=g^3f$, which means that $G\cong D_8$\\
    Otherwise $g^2=f^2$, then $fg\neq e,f,f^2,f^3$ by inspection.
    Now if $g$ is abelian then $g^2f^{-2}=e\implies (gf^{-1})^2=e$, $gf^{-1}\notin\langle f\rangle\implies G\cong C_4\times C_2$.
    Otherwise, by inspection $fg=gf^3$, therefore we have defined the group action completely, so it can only be isomorphic to $Q_8$, which is not in any of the preceding cases.
    \footnote{Alternatively we can easily construct an explicit isomorphism.}
    So the claim is proved.
\end{proof}


\section{Vector bundles, principal bundles}
Let $X$ be a topological space. A point in $X$ can be viewed as a map $\ast\to
X$; this is a cross section of the canonical map $X\to \ast$. Motivated by
this, we will define a vector space over $B$ to be a space $E\to B$ over $B$
with the following extra data:
\begin{itemize}
    \item a multiplication $\mu:E\times_B E\to E$, compatible with the maps
	down to $B$;
    \item a ``zero'' section $s:B\to E$ such that the composite $B\xar{s}E\to
	B$ is the identity;
    \item an inverse $\chi:E\to E$, compatible with the map down to $B$; and
    \item an action of $\RR$:
	\begin{equation*}
	    \xymatrix{
		\RR\times E\ar[dr]_{p\circ \pr_2}\ar@{=}[r] &
		(B\times\RR)\times_B E\ar[r]\ar[d] & E\ar[dl]^p\\
		& B &
	    }
	\end{equation*}
\end{itemize}
Because $\RR$ is a field, the last piece of data shows that $p^{-1}(b)$ is a
$\RR$-vector space for any point $b\in B$.
\begin{example}\label{trivialvectorbundle}
    A rather silly example of a vector space over $B$ is the projection
    $B\times V\to B$ where $V$ is a (real) vector space, which we will always
    assume to be finite-dimensional.
\end{example}
\begin{example}
    Consider the map
    $$\RR\times\RR\xrightarrow{(s,t)\mapsto(s,st)}\RR\times\RR,$$
    over $\RR$ (the structure maps are given by projecting onto the first
    factor). It is an isomorphism on all fibers, but is zero everywhere else.
    The kernel is therefore $0$ everywhere, except over the point $0\in\RR$.
    This the ``skyscraper'' vector bundle over $B$.
\end{example}
Sheaf theory accommodates examples like this.

One can only go so far you can go with this simplistic notion of a ``vector
space'' over $B$. Most interesting and naturally arising examples have a little
more structure, which is exemplified in the following definition.
\begin{definition}
    A \emph{vector bundle} over $B$ is a vector space over $B$ that is locally
    trivial (in the sense of Definition \ref{fiberbundle}).
\end{definition}
\begin{remark}
    We will always assume that the space $B$ admits a numerable open cover (see
    Definition \ref{numerable}) which trivializes the vector bundle. Moreover,
    the dimension of the fiber will always be finite.
\end{remark}
If $p:E\to B$ is a vector bundle, then $E$ is called the \emph{total space},
$p$ is called the \emph{projection map}, and $B$ is called the \emph{base
space}. We will always use a Greek letter like $\xi$ or $\zeta$ to denote a
vector bundle, and $E(\xi)\to B(\xi)$ denotes the actual projection map from
the total space to the base space. The phrase ``$\xi$ is a vector bundle over
$B$'' will also be shortened to $\xi\downarrow B$.
\begin{example}\label{grassmannianvb}
\begin{enumerate}
    \item Following Example \ref{trivialvectorbundle}, one example of a vector
	bundle is the trivial bundle $B\times\RR^n\to B$, denoted by
	$n\epsilon$.
    \item In contrast to this silly example, one gets extremely interesting
	examples from the Grassmannians $\Gra_k(\RR^n)$, $\Gra_k(\cC^n)$, and
	$\Gra_k(\HH^n)$. For simplicity, let $K$ denote $\RR,\cC$, or $\HH$.
	Over $\Gra_k(K^n)$ lies the \emph{tautological bundle} $\gamma$. This
	is a sub-bundle of $n\epsilon$ (i.e., the fiber over any point $x\in
	\Gra_k(K^n)$ is a subspace of the fiber of $n\epsilon$ over $x$). The
	total space of $\gamma$ is defined as:
	\begin{equation*}
	    E(\gamma) = \{(V,x)\in\Gra_k(K^n)\times K^n:x\in V\}
	\end{equation*}
	This projection map down to $\Gra_k(K^n)$ is the literal projection map
	$$(V,x)\mapsto V.$$
	\begin{exercise}
	    Prove that $\gamma$, as defined above, is locally trivial; so
	    $\gamma$ defines a vector bundle over $\Gra_k(K^n)$.
	\end{exercise}
	    For instance, when $k=1$, we have $\Gra_1(\RR^n) = \RP^{n-1}$. In
	    this case, $\gamma$ is one-dimensional (i.e., the fibers are all of
	    dimension $1$); this is called a \emph{line bundle}. In fact, it is
	    the ``canonical line bundle'' over $\RP^{n-1}$.
    \item Let $M$ be a smooth manifold. Define $\tau_M$ to be the tangent
	bundle $TM\to M$ over $M$. For example, if $M = S^{n-1}$, then
	$$TS^{n-1} = \{(x,v)\in S^{n-1}\times\RR^n:v\cdot x = 0\}.$$
\end{enumerate}
\end{example}
\subsection{Constructions with vector bundles}
One cannot take the kernels of a map of vector bundles; but just about
anything which can be done for vector spaces can also be done for vector
bundles:
\begin{enumerate}
    \item Pullbacks are legal: if $p^\prime:E^\prime\to B^\prime$, then the
	leftmost map in the diagram below is also a vector bundle.
	\begin{equation*}
	    \xymatrix{
		E\ar[r]\ar[d] & E^\prime\ar[d]^{p^\prime}\\
		B\ar[r]_f & B^\prime
		}
	\end{equation*}
	For instance, if $B=\ast$, the pullback is just the fiber of $E^\prime$
	over the point $\ast\to B^\prime$. If $\xi$ is the bundle $E^\prime\to
	B^\prime$, we denote the pullback $E\to B$ as $f^\ast \xi$.
    \item If $p:E\to B$ and $p^\prime:E^\prime\to B^\prime$, then we can take
	the product $E\times E^\prime\xrightarrow{p\times p^\prime}B\times
	B^\prime$.
    \item If $B=B^\prime$, we can form the pullback:
	\begin{equation*}
	    \xymatrix{
		E\oplus E^\prime\ar[r]\ar[d] & E\times E^\prime\ar[d]\\
		B\ar[r]_{\Delta} & B\times B
		}
	\end{equation*}
	The bundle $E\oplus E^\prime$ is called the \emph{Whitney sum}. For
	instance, it is an easy exercise to see that
	$$n\epsilon = \epsilon\oplus\cdots\oplus\epsilon.$$
    \item If $E,E^\prime\to B$ are two vector bundles over $B$, we can form
	another vector bundle $E\otimes_\RR E^\prime\to B$ by taking the
	fiberwise tensor product. Likewise, taking the fiberwise Hom begets a
	vector bundle $\Hom_\RR(E,E^\prime)\to B$.
\end{enumerate}
\begin{example}
    Recall from Example \ref{grassmannianvb}(2) that the tautological bundle
    $\gamma$ lives over $\RP^{n-1}$; we will write $L = E(\gamma)$. The tangent
    bundle $\tau_{\RP^{n-1}}$ also lives over $\RP^{n-1}$. As this is the first
    explicit pair of vector bundles over the same space, it is natural to
    wonder what is the relationship between these two bundles.

    At first glance, one might guess that $\tau_{\RP^{n-1}} = \gamma^\perp$;
    but this is false! Instead,
    $$\tau_{\RP^{n-1}} = \Hom(\gamma,\gamma^\perp).$$
    To see this, note that we have a $2$-fold covering map $S^{n-1}\to
    \RP^{n-1}$; therefore, $T_x(\RP^{n-1})$ is a quotient of $T(S^n)$ by the
    map sending $(x,v)\mapsto (-x,-v)$, where $v\in T_x(S^n)$. Therefore,
    $$T_x\RP^{n-1} = \{(x,v)\in S^{n-1}\times\RR^n:v\cdot x =
    0\}/((x,v)\sim(-x,-v)).$$
    This is exactly the fiber of $\Hom(\gamma,\gamma^\perp)$ over $x\in
    \RP^{n-1}$, since the line through $x$ can be mapped to the line through
    $\pm v$.
\end{example}
\begin{exercise}
    Prove that if $\gamma$ is the tautological vector bundle over
    $\Gra_k(K^n)$, for $K=\RR,\cC,\HH$, then
    $$\tau_{\Gra_k(K^n)} = \Hom(\gamma,\gamma^\perp).$$
\end{exercise}
\subsection{Metrics and splitting exact sequences}
A \emph{metric} on a vector bundle is a continuous choice of inner products on
fibers.
\begin{lemma}
    Any vector bundle $\xi$ over $X$ admits a metric.
\end{lemma}
Intuitively speaking, this is true because if $g,g^\prime$ are both inner
products on $V$, then $tg+(1-t)g^\prime$ is another. Said differently, the
space of metrics forms a real affine space.
\begin{proof}
    Pick a trivializing open cover of $X$, and a subordinate partition of
    unity. This means that we have a map $\phi_U:U\to [0,1]$, such that the
    preimage of the complement of $0$ is $U$. Moreover,
    $$\sum_{x\in U} \phi_U(x) = 1.$$
    Over each one of these trivial pieces, pick a metric $g_U$ on $E|_{U}$.
    Let
    $$g \coloneqq \sum_{U}\phi_U g_U;$$
    this is the desired metric on $\xi$.
\end{proof}
We remark that, in general, one cannot pick metrics for vector bundles. For
instance, this is the case for vector bundles which arise in algebraic
geometry.
\begin{definition}
    Suppose $E,E^\prime\to B$ are vector bundles over $B$. An
    \emph{isomorphism} is a map $\alpha:E\to E^\prime$ over $B$ that is a
    linear isomorphism on each fiber.
\end{definition}
In particular, the map $\alpha$ admits an inverse (over $B$).
\begin{corollary}\label{split}
    Any exact\footnote{This is the obvious definition.} sequence $0\to
    E^\prime\to E\to E^{\prime\prime}\to 0$ of vector bundles (over the same
    base) splits.
\end{corollary}
\begin{proof}[Proof sketch]
    Pick a metric for $E$. Consider the composite
    $${E^\prime}^\perp\subseteq E\to E^{\prime\prime}.$$
    This is an isomorphism: the dimensions of the fibers are the same. It
    follows that
    $$E\cong E^\prime\oplus {E^\prime}^{\perp}\cong E^\prime\oplus
    E^{\prime\prime},$$
    as desired.
\end{proof}
Note that this splitting is not natural.