\subsection*{MATH Library}

The following names are provided by the MATH library:
\begin{itemize}
\item \href{https://sourceacademy.org/sicpjs/1.1.4\#p8}{\lstinline{math_}$\textit{name}$},
where $\textit{name}$ is any name specified in the
JavaScript
\texttt{Math} library, see\\
\href{https://www.ecma-international.org/ecma-262/9.0/index.html\#sec-math-object}{\color{DarkBlue}ECMAScript Specification, Section 20.2}. Examples:
\begin{itemize}
\item \verb#math_PI#: \textit{primitive}, refers to the mathematical constant $\pi$,
\item \verb#math_sqrt#\texttt{(n)}: \textit{primitive}, returns the square root of the \emph{number} \texttt{n}.
\end{itemize}
\end{itemize}
All functions can be assumed to run in $O(1)$ time and are considered
\textit{primitive}.


\chapter{Motor selection considerations}
One non-obvious matter that differs FOC-enabled motor controllers (like Zubax Komar)
from conventional controllers with trapezoidal or six step commutation
is so called voltage utilization factor.
Every BLDC motor has a characteristic that defines its theoretical maximum rotational speed.
It is called  motor speed constant K\textsubscript{v}.
It is measured in revolutions per minute (RPM) per volt or radians per volt second [rad/(V*s)].
Physical meaning of this constant is the number of revolutions per minute (rpm) that a motor turns when 1 V (one volt)
is applied with no load attached to that motor.

FOC-enabled motor controllers have additional voltage utilization factor that decreases the maximum RPM
for a given motor and supply voltage. For Komar this factor is:

\[F\textsubscript{util} = 0.99\]
\[RPM\textsubscript{max} = K\textsubscript{v} \times V\textsubscript{supply} \times F\textsubscript{util}\]

RPM\textsubscript{max} of a FOC enabled motor controller will always be lower than RPM\textsubscript{max}
of a conventional controller with trapezoidal or six step commutation. This should be taken into account
when designing a propulsion system.

For example, a motor with speed constant K\textsubscript{v} = 320 controlled by Komar
running on fully charged 10S $\text{LiCoO}_\text{2}$ battery will have the following theoretical maximum RPM:

\[RPM\textsubscript{max} = 320 \times 10 \times 4.2 \times 0.99 = 13305\]


\begin{definition}~
	\begin{itemize}
		\item $ \mathbb{A} $ is the set of \emph{names}
		\item $ \overline{\mathbb{A}} = \left\lbrace\overline{a}\middle\vert a\in\mathbb{A}\right\rbrace $ is the set of \emph{co-names}
		\item $ \mathbb{L}=\mathbb{A}\cup\overline{\mathbb{A}} $ is the set of \emph{labels}
		\item $ \act = \mathbb{L}\cup\{\tau\} $ is the set of \emph{actions}, where $ \tau $ is the \emph{internal}(or \emph{silent}) action
		\item $ \overline{\overline{a}} = a $
		\item A function $ f:\act\rightarrow\act $ is a \emph{relabelling function} if $ f(\tau)=\tau $ and $ f(\overline{a}) = \overline{f(a)} $
		\item $ \mathcal{P} $ is the set of all CCS expressions
		\item $ \mathbb{K} $ is the set of all constants
	\end{itemize}
\end{definition}
\subsection*{Syntax of CCS expressions}
\begin{align*}
P:=&K&\text{constant} (K\in\mathbb{K})\\
&\alpha.P&\text{prefixing}(\alpha\in\act)\\
&\sum_{i\in I}P_i&\text{summation}\\
&P\vert Q&\text{parallel composition}\\
&P\backslash L&\text{restriction}(L\subseteq \mathbb{A})\\
&P[f]&\text{relabelling}(f:\act\rightarrow\act)
\end{align*}
\subsection*{Notation}
\begin{itemize}
	\item $ P_1+P_2=\sum_{i\in\{1,2\}}P_i $
	\item $ 0 = \sum_{i\in\emptyset}P_i $
\end{itemize}
\subsection*{Precedence of operators}
\begin{enumerate}
	\item Restriction and relabelling
	\item Action prefixing
	\item parallel composition
	\item summation
\end{enumerate}
\subsection*{SOS Rules for CCS}
\begin{align*}
&(ACT)\dfrac{}{\alpha.P\xrightarrow{\alpha}P}
&(SUM_j)\dfrac{P_j\xrightarrow{\alpha}P_j'}{\sum_{i\in I}P_i\xrightarrow{\alpha}P_j'}\text{where } j\in I\\
&(COM1)\dfrac{P\xrightarrow{\alpha}P'}{P\vert Q\xrightarrow{\alpha}P'\vert Q}
&(COM2)\dfrac{Q\xrightarrow{\alpha}Q'}{P\vert Q\xrightarrow{\alpha}P\vert Q'}\\
&(COM3)\dfrac{P\xrightarrow{a}P'~~Q\xrightarrow{\overline{a}}Q'}{P\vert Q\xrightarrow{\tau}P'\vert Q'}
&(RES)\dfrac{P\xrightarrow{\alpha}P'}{P\backslash L\xrightarrow{\alpha}P'\backslash L}\text{where } a,\overline{a}\notin L\\
&(REL)\dfrac{P\xrightarrow{\alpha}P'}{P[f]\xrightarrow{f(\alpha)}P'[f]}
&(CON)\dfrac{P\xrightarrow{\alpha}P'}{K\xrightarrow{\alpha}P'}\text{where }K\definedby P
\end{align*}

Modeling and Verification - Reference/CCS.tex


%!TEX root = ../TTT4150-Summary.tex
\section{Precise positioning with carrier phase}
Code measurement accuracy tops out at the meter level. Precise positioning with carrier phase measurements, can achieve centimeter-level accuracy.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Carrier phase and integer ambiguity resolution: A simple model}
Two antennas receive the same carrier signal. It's off by $N$ full cycles, and a partial cycle. The partial cycle (phase shift) can be measured, but we don't know $N$. This gives integer ambiguity.

If we wait for the satellite to move a bit and measure again, we have two equations in two unknowns, and can solve for $N$. Satellite geometry change is essential for the quality of the estimates.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Precise point positioning (PPP)}

PPP is centimeter-level positioning without a reference station, based on precise estimation of all significant error sources. The necessary orbit data is available in various latencies and accuracies. Near real-time is possible, but you get better accuracy if you wait a while.

The errors we must estimate are:
\begin{itemize}
    \item Satellite ephemeris and clock error: You can get these from IGS.
    \item Ionospheric delay: Eliminate with dual-frequency measurements.
    \item Tropospheric delay: Must estimate.
    \item Multipath and receiver noise: At least no reference station multipath error now.
    \item Phase wind-up correction: Antenna rotation (rover or satellite) will change the carrier phase.
    \item Satellite antenna offsets: Orbit models give position of satellite mass center, but pseudorange measurements refer to the antenna phase center.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Carrier phase integer ambiguity}
As mentioned, carrier phase has an integer ambiguity. This integer can be fixed.

\subsubsection{Using code measurements to estimate integers}
We can use the code measurement to estimate the carrier integer. But it's hard to use the fairly coarse code pseudorange to estimate this, and you need to average codes over a long time horizon to get a good enough estimate. Therefore, it doesn't really work.


\chapter{Markov chain Monte Carlo (MCMC)inference}

\section{Introduction}
In Chapter \ref{chap:Monte-Carlo-inference}, we introduced some simple Monte Carlo methods, including rejection sampling and importance sampling. The trouble with these methods is that they do not work well in high dimensional spaces. The most popular method for sampling from high-dimensional distributions is \textbf{Markov chain Monte Carlo} or \textbf{MCMC}.

The basic idea behind MCMC is to construct a Markov chain (Section \ref{sec:Markov-models}) on the state space $\mathcal{X}$ whose stationary distribution is the target density $p^*(\vec{x})$ of interest (this may be a prior or a posterior). That is, we perform a random walk on the state space, in such a way that the fraction of time we spend in each state $\vec{x}$ is proportional to $p^*(\vec{x})$. By drawing (correlated!) samples $\vec{x}_0, \vec{x}_1, \vec{x}_2, \cdots$ from the chain, we can perform Monte Carlo integration wrt $p^*$.

\section{Metropolis Hastings algorithm}

\section{Gibbs sampling}

\section{Speed and accuracy of MCMC}

\section{Auxiliary variable MCMC *}


%!TEX root=report.tex

\subsection{Time series analysis - ARIMA}
\label{section:result-ts}

To get an indication of whether its possible to use time series analysis on the GRACE data, a single position (63.5 N 49.5 W, west coast of Greenland) has been selected.

To analyze the data using an ARIMA model an equidistant dataset is required. For example it would otherwise not be possible to solve the Yule-Walker equations \cite[s.~122]{time-series-analysis}. In the original GRACE dataset some values are missing, thus they should be interpolated (linear interpolation was used). Also in order to get an indication of the model performance, the last 36 observations (corresponding to one year) have been separated for model validation.

\begin{figure}[H]
\centering
\includegraphics[height=5cm]{figures/ts-initial-split}
\caption{GRACE data at 63.5 N 49.5 W, where missing values are interpolated. Blue is the training data and green is the  test data.}
\label{fig:ts-initial-split}
\end{figure}

The time series in Figure \ref{fig:ts-initial-split} is clearly not stationary, thus it is necessary to consider the time series difference.
\begin{figure}[H]
	\centering
	\includegraphics[height=5cm]{figures/ts-residual-i1s0}
	\caption{The ARIMA$(0,1,0) \times (0,0,0)_{36}$ residuals.}
	\label{fig:ts-residual-i1s0}
\end{figure}
On Figure \ref{fig:ts-residual-i1s0} there are some seasonal periods where the mean and variance are not the same as the remaining period, thus it is not completely stationary. Taking also the seasonal difference (assuming the season is 36 observations, a year) gives however a very stationary output.

\begin{figure}[H]
	\centering
	\includegraphics[height=5cm]{figures/ts-residual-i1s1}
	\caption{The ARIMA$(0,1,0) \times (0,1,0)_{36}$ residuals.}
	\label{fig:ts-residual-i1s1}
\end{figure}
On Figure \ref{fig:ts-residual-i1s1} its seen that the first 37 observations act strange, this is because there aren't enough past observations to estimate the EWH correctly, thus they should be excluded from further analysis.

To determine the AR and MA terms in the ARIMA model, the ACF and PACF should be estimated using the residuals from Figure \ref{fig:ts-residual-i1s1}.
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{figures/ts-acf-ar0s0}
	\caption{ACF and PACF for ARIMA$(0,1,0) \times (0,1,0)_{36}$ residuals}
	\label{fig:ts-acf-ar0s0}
\end{figure}

Using the PACF in Figure \ref{fig:ts-acf-ar0s0} and the rules for the AR term \cite[Table~6.1]{time-series-analysis} \texttt{AR(2)}, seams like a good guess for the non-seasonal AR term.
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{figures/ts-acf-ar2s0}
	\caption{ACF and PACF for ARIMA$(2,1,0) \times (0,1,0)_{36}$ residuals}
	\label{fig:ts-acf-ar2s0}
\end{figure}

This clearly fitted the non-seasonal trend. From Figure \ref{fig:ts-acf-ar0s0} it might have looked like there was a \texttt{AR(3)} or \texttt{MA(2)} term, but the \texttt{AR(2)} is the simplest of those and fit the trend just fine. The seasonal part is now extremely apparent in Figure \ref{fig:ts-acf-ar2s0}, where it looks like either a \texttt{SAR(2)}- or a \texttt{SAR(1)}-term.
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{figures/ts-acf-ar2s2}
	\caption{ACF and PACF for ARIMA$(2,1,0) \times (2,1,0)_{36}$ residuals}
	\label{fig:ts-acf-ar2s2}
\end{figure}

From just looking at the estimated ACF and PACF in Figure \ref{fig:ts-acf-ar2s2}, ARIMA$(2,1,0) \times (2,1,0)_{36}$ seems like a good choice. To finally validate the model, a good start is to look at the residuals.
\begin{figure}[H]
	\centering
	\includegraphics[height=5cm]{figures/ts-final-residual}
	\caption{ARIMA$(2,1,0) \times (2,1,0)_{36}$ residuals. The first 111 residuals have been skipped since they cannot be estimated correctly.}
	\label{fig:ts-final-residual}
\end{figure}

Figure \ref{fig:ts-final-residual} looks stationary, there are no outliers nor seasonal trends. To validate the model further the Ljung-Box test can be used. This however requires the residuals to be normally distributed, a QQ-plot (Figure \ref{fig:ts-final-qq}) shows that this is the case:
\begin{figure}[H]
	\centering
	\includegraphics[height=5cm]{figures/ts-final-qq}
	\caption{QQ-plot for the ARIMA$(2,1,0) \times (2,1,0)_{36}$ residuals.}
	\label{fig:ts-final-qq}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[height=5cm]{figures/ts-final-ljungbox}
	\caption{Ljung-Box test for the ARIMA$(2,1,0) \times (2,1,0)_{36}$ residuals.}
	\label{fig:ts-final-ljungbox}
\end{figure}

From the Ljung-Box test (Figure \ref{fig:ts-final-ljungbox}) the p-value for the first many lags looks good, however after 25 it can be with 95\% confidence statically significant concluded that the residuals are correlated. In terms of pure time series analysis is makes the model quite useless, however it is still valuable to do the cross validation.

\begin{figure}[H]
	\centering
	\centerline{\includegraphics[height=6cm]{figures/ts-final-forecast}}
	\caption{Forecast on Cross Validation. Blue is the training data, green is the test data. Red is then the predicted test values with its 95\% confidence interval marked with gray lines.}
	\label{fig:ts-final-forecast}
\end{figure}

From Figure \ref{fig:ts-final-forecast} it quite clear that the test data is consistently bellow the expectation line (red). While it is still inside the 95\% confidence interval, this high correlation in the error between lags indicates that the model is not particular useful.


\section{Introduction}
\label{sec:introduction}

The Rosenblatt Perceptron \cite{rosenblatt1958perceptron} is a device designed to solve a binary classification problem, i.e. the problem of choosing the correct class for given examples, represented as feature vectors.
The perceptron needs to be trained on some training examples in order to determine the best hyperplane to separate them:
hopefully, the learned hyperplane will be able to correctly classify also new points.

In this work, we implement and train a Rosenblatt Perceptron and run some computer simulations to verify its theoretical properties.
In particular, we try to estimate the capacity of the separation hyperplane learned by the perceptron.


\chapter{The long exact sequence}
In this chapter we introduce the key fact about chain complexes that will allow us to compute
the homology groups of any space: the so-called ``long exact sequence''.

For those that haven't read about abelian categories:
a sequence of morphisms of abelian groups
\[ \dots \to G_{n+1} \to G_n \to G_{n-1} \to \dots \]
is \vocab{exact} if the image of any arrow is equal to the kernel of the next arrow.
In particular,
\begin{itemize}
	\ii The map $0 \to A \to B$ is exact if and only if $A \to B$ is injective.
	\ii the map $A \to B \to 0$ is exact if and only if $A \to B$ is surjective.
\end{itemize}
(On that note: what do you call a chain complex whose homology groups are all trivial?)
A short exact sequence is one of the form $0 \to A \injto B \surjto C \to 0$.

\section{Short exact sequences and four examples}
\prototype{Relative sequence and Mayer-Vietoris sequence.}
Let $\AA = \catname{AbGrp}$.
Recall that we defined a morphism of chain complexes in $\AA$ already.
\begin{definition}
Suppose we have a map of chain complexes
\[ 0 \to A_\bullet \taking f B_\bullet \taking g C_\bullet \to 0 \]
It is said to be \vocab{short exact} if \emph{each row} of the diagram below is short exact.
\begin{diagram}
	&& \vdots && \vdots && \vdots && \\
	&& \dTo^{\partial_A} && \dTo^{\partial_B} && \dTo^{\partial_C} && \\
	0 & \rTo & A_{n+1} & \rInj^{f_{n+1}} & B_{n+1} & \rSurj^{g_{n+1}} & C_{n+1} & \rTo & 0 \\
	&& \dTo^{\partial_A} && \dTo^{\partial_B} && \dTo^{\partial_C} && \\
	0 & \rTo & A_n & \rInj^{f_n} & B_n & \rSurj^{g_n} & C_n & \rTo & 0 \\
	&& \dTo^{\partial_A} && \dTo^{\partial_B} && \dTo^{\partial_C} && \\
	0 & \rTo & A_{n-1} & \rInj^{f_{n-1}} & B_{n-1} & \rSurj^{g_{n-1}} & C_{n-1} & \rTo & 0 \\
	&& \dTo^{\partial_A} && \dTo^{\partial_B} && \dTo^{\partial_C} && \\
	&& \vdots && \vdots && \vdots &&
\end{diagram}
\end{definition}

\begin{example}
	[Mayer-Vietoris short exact sequence and its augmentation]
	\label{ex:mayer_short_exact}
	Let $X = U \cup V$ be an open cover.
	For each $n$ consider
	\begin{diagram}
		C_n(U \cap V) & \rInj & C_n(U) \oplus C_n(V) & \rSurj & C_n(U + V) \\
		c & \rMapsto & (c, -c) && \\
		&& (c, d) & \rMapsto & c + d
	\end{diagram}
	One can easily see (by taking a suitable basis)
	that the kernel of the latter map is exactly
	the image of the first map.
	This generates a short exact sequence
	\[ 0 \to  C_\bullet(U \cap V) \injto C_\bullet(U) \oplus C_\bullet(V)
	\surjto C_\bullet(U + V) \to 0. \]
\end{example}
\begin{example}
	[Augmented Mayer-Vietoris sequence]
	We can \emph{augment} each of the chain complexes in the Mayer-Vietoris
	sequence as well, by appending
	\begin{diagram}
		0 & \rTo & C_0(U \cap V) & \rInj & C_0(U) \oplus C_0(V) & \rSurj & C_0(U+V) & \rTo & 0\\
		&& \dSurj^{\eps} && \dSurj^{\eps \oplus \eps} && \dTo^\eps && \\
		0 & \rTo & \ZZ & \rTo & \ZZ \oplus \ZZ & \rTo & \ZZ & \rTo & 0
	\end{diagram}
	to the bottom of the diagram.
	In other words we modify the above into
	\[ 0 \to  \wt C_\bullet(U \cap V)
		\injto \wt C_\bullet(U) \oplus \wt C_\bullet(V)
		\surjto \wt C_\bullet(U + V) \to 0 \]
	where $\wt C_\bullet$ is the chain complex defined in \Cref{def:augment}.
\end{example}

\begin{example}
	[Relative chain short exact sequence]
	\label{ex:rel_short_exact}
	Since $C_n(X,A) \defeq C_n(X) / C_n(A)$, we have a short exact sequence
	\[ 0 \to C_\bullet(A) \injto C_\bullet(X) \surjto C_\bullet(X,A) \to 0 \]
	for every space $X$ and subspace $A$.
	%The maps for each $n$ are the obvious ones:
	%$C_n(A) \injto C_n(X)$ inclusion and $C_n(X) \surjto C_n(X,A)$ projection.
	This can be augmented: we get
	\[ 0 \to \wt C_\bullet(A) \injto \wt C_\bullet(X)
		\surjto C_\bullet(X,A) \to 0 \]
	by adding the final row
	\begin{diagram}
		0 & \rTo & C_0(A) & \rInj & C_0(X) & \rSurj & C_0(X,A) & \rTo & 0\\
		&& \dSurj^{\eps} && \dSurj^{\eps} && \dTo && \\
		0 & \rTo & \ZZ & \rTo_\id & \ZZ & \rTo & 0 & \rTo & 0.
	\end{diagram}
\end{example}

\section{The long exact sequence of homology groups}
Consider a short exact sequence $0 \to A_\bullet \taking f B_\bullet \taking g C_\bullet \to 0$.
Now, we know that we get induced maps of homology groups, i.e.\ we have
\begin{diagram}
	\vdots && \vdots && \vdots  \\
	H_{n+1}(A_\bullet) & \rTo^{f_\ast} & H_{n+1}(B_\bullet) & \rTo^{g_\ast} & H_{n+1}(C_\bullet) \\
	H_{n}(A_\bullet) & \rTo^{f_\ast} & H_{n}(B_\bullet) & \rTo^{g_\ast} & H_{n}(C_\bullet) \\
	H_{n-1}(A_\bullet) & \rTo^{f_\ast} & H_{n-1}(B_\bullet) & \rTo^{g_\ast} & H_{n-1}(C_\bullet) \\
	\vdots && \vdots && \vdots \\
\end{diagram}
But the theorem is that we can string these all together,
taking each $H_{n+1}(C_\bullet)$ to $H_n(A_\bullet)$.

\begin{theorem}[Short exact $\implies$ long exact]
	\label{thm:long_exact}
	Let $0 \to A_\bullet \taking f B_\bullet \taking g C_\bullet \to 0$
	be \emph{any} short exact sequence of chain complexes we like.
	Then there is an \emph{exact} sequence
	\begin{diagram}
		&& \dots & \rTo & H_{n+2}(C_\bullet) \\
		H_{n+1}(A_\bullet) & \rTo_{f_\ast} &
		H_{n+1}(B_\bullet) & \ldTo(4,1)~\partial \rTo^{g_\ast} & H_{n+1}(C_\bullet) \\
		H_{n}(A_\bullet) & \rTo_{f_\ast} &
		H_{n}(B_\bullet) & \rTo^{g_\ast} \ldTo(4,1)~{\partial} & H_{n}(C_\bullet) \\
		H_{n-1}(A_\bullet) & \rTo_{f_\ast} &
		H_{n-1}(B_\bullet) & \rTo^{g_\ast} \ldTo(4,1)~{\partial} & H_{n-1}(C_\bullet) \\
		H_{n-2}(A_\bullet) & \rTo & \dots & \ldTo(4,1)~\partial &
	\end{diagram}
	This is called a \vocab{long exact sequence} of homology groups.
\end{theorem}
\begin{proof}
	A very long diagram chase, valid over any abelian category.
	(Alternatively, it's actually possible to use the snake lemma twice.)
\end{proof}

\begin{remark}
	\label{rem:leftdownleft}
	The map $\partial : H_n(C_\bullet) \to H_{n-1}(A_\bullet)$ can be written explicitly as follows.
	Recall that $H_n$ is ``cycles modulo boundaries'', and consider the sub-diagram
	\begin{diagram}
	&& B_n & \rSurj^{g_n} & C_n \\
	 && \dTo^{\partial_B} && \dTo_{\partial_C} \\
	A_{n-1} & \rInj^{f_{n-1}} & B_{n-1} & \rSurj_{g_{n-1}} & C_{n-1} \\
	\end{diagram}
	We need to take every cycle in $C_n$ to a cycle in $A_{n-1}$.
	(Then we need to check a ton of ``well-defined'' issues,
	but let's put that aside for now.)

	Suppose $c \in C_n$ is a cycle (so $\partial_C(c) = 0$).
	By surjectivity, there is a $b \in B_n$ with $g_n(b) = c$,
	which maps down to $\partial_B(b)$.
	Now, the image of $\partial_B(b)$ under $g_{n-1}$ is zero by commutativity of the square,
	and so we can pull back under $f_{n-1}$ to get a unique element of $A_{n-1}$
	(by exactness at $B_{n-1}$).

	In summary: we go ``\emph{left, down, left}'' to go from $c$ to $a$:
	\begin{diagram}
		&& b & \rMapsto^{g_n} & \boxed c \\
		&& \dMapsto^{\partial_B} && \dMapsto_{\partial_C} \\
		\boxed a & \rMapsto^{f_{n-1}} & \partial_B(b) & \rMapsto_{g_{n-1}} & 0
	\end{diagram}
\end{remark}
\begin{exercise}
	Check quickly that the recovered $a$ is actually a cycle,
	meaning $\partial_A(a) = 0$.
	(You'll need another row, and the fact that $\partial_B^2 = 0$.)
\end{exercise}

The final word is that:
\begin{moral}
	Short exact sequences of chain complexes give
	long exact sequences of homology groups.
\end{moral}
In particular, let us take the four examples given earlier.
\begin{example}[Mayer-Vietoris long exact sequence, provisional version]
	The Mayer-Vietoris ones give, for $X = U \cup V$ an open cover,
	\[ \dots \to H_n(U \cap V) \to H_n(U) \oplus H_n(V) \to H_n(U+V) \to H_{n-1}(U \cap V) \to \dots. \]
	and its reduced version
	\[ \dots \to \wt H_n(U \cap V) \to \wt H_n(U) \oplus \wt H_n(V)
	\to \wt H_n(U+V) \to \wt H_{n-1}(U \cap V) \to \dots. \]
\end{example}
This version is ``provisional'' because in the next section
we will replace $H_n(U+V)$ and $\wt H_n(U+V)$ with something better.
As for the relative homology sequences, we have:
\begin{theorem}[Long exact sequence for relative homology]
	\label{thm:long_exact_rel}
	Let $X$ be a space, and let $A \subseteq X$ be a subspace.
	There are long exact sequences
	\[ \dots \to H_n(A) \to H_n(X) \to H_n(X,A) \to H_{n-1}(A) \to \dots. \]
	and
	\[ \dots \to \wt H_n(A) \to \wt H_n(X) \to H_n(X,A) \to \wt H_{n-1}(A) \to \dots. \]
\end{theorem}
The exactness of these sequences will give \textbf{tons of information}
about $H_n(X)$ if only we knew something about what $H_n(U+V)$
or $H_n(X,A)$ looked like.  This is the purpose of the next chapter.

\section{The Mayer-Vietoris sequence}
\prototype{The computation of $H_n(S^m)$ by splitting $S^m$ into two hemispheres.}

Now that we have done so much algebra, we need to invoke some geometry.
There are two major geometric results in the Napkin.
One is the excision theorem, which we discuss next chapter.
The other we present here, which will let us take advantage of the
Mayer-Vietoris sequence.
The proofs are somewhat involved and are thus omitted;
see \cite{ref:hatcher} for details.

The first theorem is that the notation $H_n(U+V)$ that we have kept until now
is redundant, and can be replaced with just $H_n(X)$:
\begin{theorem}[Open cover homology theorem]
	\label{thm:open_cover_homology}
	Consider the inclusion $\iota : C_\bullet(U+V) \injto C_\bullet(X)$.
	Then $\iota$ induces an isomorphism
	\[ H_n(U+V) \cong H_n(X). \]
	% Then there exists a $\rho : C_\bullet(X) \to C_\bullet(U+V)$ such that
	% $\rho\iota$ and $\iota\rho$ are chain homotopic to the identities.
	% Thus $\iota$ induces an isomorphism
\end{theorem}
\begin{remark}
	In fact, this is true for any open cover (even uncountable),
	not just those with two covers $U \cup V$.
	But we only state the special case with two open sets,
	because this is what is needed for \Cref{ex:mayer_short_exact}.
\end{remark}
So, \Cref{ex:mayer_short_exact} together with the above theorem implies,
after replacing all the $H_n(U+V)$'s with $H_n(X)$'s:
\begin{theorem}[Mayer-Vietoris long exact sequence]
	If $X = U \cup V$ is an open cover, then we have long exact sequences
	\[ \dots \to H_n(U \cap V) \to H_n(U) \oplus H_n(V)
		\to H_n(X) \to H_{n-1}(U \cap V) \to \dots. \]
	and
	\[ \dots \to \wt H_n(U \cap V) \to \wt H_n(U) \oplus \wt H_n(V) \to
		\wt H_n(X) \to \wt H_{n-1}(U \cap V) \to \dots. \]
\end{theorem}

At long last, we can compute the homology groups of the spheres.
\begin{theorem}[The homology groups of $S^m$]
	\label{thm:reduced_homology_sphere}
	For integers $m$ and $n$,
	\[ \wt H_n(S^m) \cong
	\begin{cases}
		\ZZ & n=m \\
		0 & \text{otherwise}.
	\end{cases}
	\]
	The generator $\wt H_n(S^n)$ is an $n$-cell which covers $S^n$
	exactly once (for example, the generator for $\wt H_1(S^1)$
	is a loop which wraps around $S^1$ once).
\end{theorem}
\begin{proof}
	This one's fun, so I'll only spoil the case $m=1$, and leave the rest to you.
	Decompose the circle $S^1$ into two arcs $U$ and $V$, as shown:
	\begin{center}
		\begin{asy}
			size(4cm);
			draw(unitcircle);
			label("$S^1$", dir(45), dir(45));
			real R = 0.1;
			draw(arc(origin,1-R,-100,100), red+1);
			label("$V$", (1-R)*dir(0), dir(180), red);
			draw(arc(origin,1+R,80,280), blue+1);
			label("$U$", (1+R)*dir(180), dir(180), blue);
		\end{asy}
	\end{center}
	Each of $U$ and $V$ is contractible, so all their reduced homology groups vanish.
	Moreover, $U \cap V$ is homotopy equivalent to two points,
	hence
	\[ \wt H_n(U \cap V) \cong
		\begin{cases}
			\ZZ & n = 0 \\
			0 & \text{otherwise}.
		\end{cases}
	\]
	Now consider again the segment of the short exact sequence
	\[
		\dots \to
		\underbrace{\wt H_n(U) \oplus \wt H_n(V)}_{= 0} \to
		\wt H_n(S^1) \taking{\partial} \wt H_{n-1}(U \cap V) \to
		\underbrace{\wt H_{n-1}(U) \oplus \wt H_{n-1}(V)}_{=0} \to \dots.
	\]
	From this we derive that $\wt H_n(S^1)$ is $\ZZ$ for $n=1$ and $0$ elsewhere.

	It remains to analyze the generators of $\wt H_1(S^1)$.
	Note that the isomorphism was given by the connecting homomorphism $\partial$,
	which is given by a ``left, down, left'' procedure (\Cref{rem:leftdownleft})
	in the diagram
	\begin{diagram}
		&& C_1(U) \oplus C_1(V) & \rTo & C_1(U+V) \\
		&& \dTo^{\partial \oplus \partial} && \\
		C_0(U \cap V) & \rTo & C_0(U) \oplus C_0(V). &&
	\end{diagram}
	Mark the points $a$ and $b$ as shown in the two disjoint paths of $U \cap V$.
	\begin{center}
		\begin{asy}
			size(3cm);
			label("$S^1$", dir(45), dir(45));
			real R = 0.1;
			/*
			draw(arc(origin,1-R,-100,100), red+1);
			label("$V$", (1-R)*dir(0), dir(180), red);
			draw(arc(origin,1+R,80,280), blue+1);
			label("$U$", (1+R)*dir(180), dir(180), blue);
			*/
			dot("$a$", dir(90), dir(90));
			dot("$b$", dir(-90), dir(-90));
			draw(arc(origin,1,90,270), EndArrow, Margins);
			draw(arc(origin,1,90,-90), EndArrow, Margins);
			label("$c$", dir(180), dir(180));
			label("$d$", dir(0), dir(0));
		\end{asy}
	\end{center}
	Then $a-b$ is a cycle which represents a generator of $H_0(U \cap V)$.
	We can find the pre-image of $\partial$ as follows:
	letting $c$ and $d$ be the chains joining $a$ and $b$, with $c$ contained
	in $U$, and $d$ contained in $V$, the diagram completes as
	\begin{diagram}
		&& (c,d) & \rMapsto & c-d \\
		&& \dMapsto && \\
		a-b & \rMapsto & (a-b, a-b) &&
	\end{diagram}
	In other words $\partial(c-d) = a-b$, so $c-d$ is a generator for $\wt H^1(S^1)$.

	Thus we wish to show that $c-d$ is (in $H^1(S^1)$) equivalent to the loop $\gamma$
	wrapping around $S^1$ once, counterclockwise.
	This was illustrated in \Cref{ex:S1_c_minus_d}.
\end{proof}

Thus, the key idea in Mayer-Vietoris is that
\begin{moral}
	Mayer-Vietoris lets us compute $H_n(X)$
	by splitting $X$ into two open sets.
\end{moral}

Here are some more examples.
\begin{proposition}[The homology groups of the figure eight]
	Let $X = S^1 \wedge S^1$ be the figure eight.
	Then
	\[
		\wt H_n(X) \cong
		\begin{cases}
			\ZZ^{\oplus 2} & n = 1 \\
			0 & \text{otherwise}.
		\end{cases}
	\]
	The generators for $\wt H_1(X)$ are the two loops of the figure eight.
\end{proposition}
\begin{proof}
	Again, for simplicity we work with reduced homology groups.
	Let $U$ be the ``left'' half of the figure eight plus a little bit of the right,
	as shown below.
	\begin{center}
		\begin{asy}
			size(4cm);
			draw(unitcircle);
			draw(CR(2*dir(180),1), blue+2);
			draw(arc(origin,1,135,225), blue+2);
			label("$U$", 2*dir(180)+dir(135), dir(135), blue);
			label("$S^1 \wedge S^1$", dir(15), dir(15));
		\end{asy}
	\end{center}
	The set $V$ is defined symmetrically.
	In this case $U \cap V$ is contractible, while each of $U$ and $V$
	is homotopic to $S^1$.

	Thus, we can read a segment of the long exact sequence as
	\[
		\dots \to
		\underbrace{\wt H_n(U \cap V)}_{=0}
		\to \wt H_n(U) \oplus \wt H_n(V) \to \wt H_n(X) \to
		\underbrace{\wt H_{n-1}(U \cap V)}_{=0} \to \dots.
	\]
	So we get that $\wt H_n(X) \cong \wt H_n(S^1) \oplus \wt H_n(S^1)$,
	The claim about the generators follows from the fact that,
	according to the isomorphism above,
	the generators of $\wt H_n(X)$ are the generators of $\wt H_n(U)$
	and $\wt H_n(V)$, which we described geometrically
	in the last theorem.
\end{proof}

Up until now, we have been very fortunate that we have always been able to make
certain parts of the space contractible.
This is not always the case, and in the next example we will have to
actually understand the maps in question to complete the solution.

\begin{proposition}
	[Homology groups of the torus]
	Let $X = S^1 \times S^1$ be the torus.
	Then
	\[
		\wt H_n(X)
		=
		\begin{cases}
			\ZZ^{\oplus 2} & n = 1 \\
			\ZZ & n = 2 \\
			0 & \text{otherwise}.
		\end{cases}
	\]
\end{proposition}
\begin{proof}
	To make our diagram look good on 2D paper,
	we'll represent the torus as a square with its edges identified,
	though three-dimensionally the picture makes sense as well.
	Consider $U$ (shaded light orange) and $V$ (shaded green) as shown.
	(Note that $V$ is connected due to the identification of the left and right (blue) edges,
	even if it doesn't look connected in the picture).
	\begin{center}
		\begin{asy}
			pair A = (0,0);
			pair B = (1,0);
			pair C = (1,1);
			pair D = (0,1);
			draw(A--B, red+1.5, MidArrow);
			draw(B--C, blue+1.5, MidArrow);
			draw(D--C, red+1.5, MidArrow);
			draw(A--D, blue+1.5, MidArrow);
			fill(box((0.2,0),(0.8,1)), orange+opacity(0.2));
			fill(box(A,(0.3,1)), heavygreen+opacity(0.2));
			fill(box((0.7,0),C), heavygreen+opacity(0.2));
			draw( (0.3,0)--(0.3,1), heavygreen+dashed+1.2);
			draw( (0.7,0)--(0.7,1), heavygreen+dashed+1.2);
			draw( (0.2,0)--(0.2,1), orange+dashed+1.2);
			draw( (0.8,0)--(0.8,1), orange+dashed+1.2);

			label("$U$", (0.5, 0.5));
			label("$V$", (0.1, 0.8));
			label("$V$", (0.9, 0.8));
		\end{asy}
	\end{center}
	In the three dimensional picture, $U$ and $V$ are two cylinders which together give the torus.
	This time, $U$ and $V$ are each homotopic to $S^1$, and the intersection $U \cap V$
	is the disjoint union of two circles: thus $\wt H_1(U \cap V) \cong \ZZ \oplus \ZZ$,
	and $H_0(U \cap V) \cong \ZZ^{\oplus 2} \implies \wt H_0(U \cap V) \cong \ZZ$.

	For $n \ge 3$, we have
	\[
		\dots \to
		\underbrace{\wt H_n(U \cap V)}_{=0}
		\to \wt H_n(U) \oplus \wt H_n(V) \to \wt H_n(X) \to
		\underbrace{\wt H_{n-1}(U \cap V)}_{=0} \to \dots.
	\]
	and so $H_n(X) \cong 0$ for $n \ge 3$.
	Also, we have $H_0(X) \cong \ZZ$ since $X$ is path-connected.
	So it remains to compute $H_2(X)$ and $H_1(X)$.

	Let's find $H_2(X)$ first.
	We first consider the segment
	\[
		\dots \to
		\underbrace{\wt H_2(U) \oplus \wt H_2(V)}_{=0} \to \wt H_2(X) \xhookrightarrow{\delta}
		\underbrace{\wt H_1(U \cap V)}_{\cong \ZZ \oplus \ZZ} \xrightarrow{\phi}
		\underbrace{\wt H_1(U) \oplus \wt H_1(V)}_{\cong \ZZ \oplus \ZZ} \to \dots
	\]
	Unfortunately, this time it's not immediately clear what $\wt H_2(X)$ because
	we only have one zero at the left.
	In order to do this, we have to actually figure out what the maps $\delta$ and $\phi$ look like.
	Note that, as we'll see, $\phi$ isn't an isomorphism even though the groups are isomorphic.

	The presence of the zero term has allowed us to make the connecting map $\delta$ injective.
	First, $\wt H_2(X)$ is isomorphic to the image of of $\delta$, which is
	exactly the kernel of the arrow $\phi$ inserted.
	To figure out what $\ker \phi$ is, we have to think back to how the map
	$C_\bullet(U \cap V) \to C_\bullet(U) \oplus C_\bullet(V)$ was constructed:
	it was $c \mapsto (c, -c)$.
	So the induced maps of homology groups is actually what you would guess:
	a $1$-cycle $z$ in $\wt H_1(U \cap V)$ gets sent $(z, -z)$ in $\wt H_1(U) \oplus \wt H_1(V)$.

	In particular, consider the two generators $z_1$ and $z_2$ of
	$\wt H_1(U \cap V) = \ZZ \oplus \ZZ$,
	i.e.\ one cycle in each connected component of $U \cap V$.
	(To clarify: $U \cap V$ consists of two ``wristbands'';
	$z_i$ wraps around the $i$th one once.)
	Moreover, let $\alpha_U$ denote a generator of $\wt H_1(U) \cong \ZZ$,
	and $\alpha_V$ a generator of $H_2(U) \cong \ZZ$.
	Then we have that
	\[ z_1 \mapsto (\alpha_U, -\alpha_V) \qquad\text{and}\qquad z_2 \mapsto (\alpha_U, -\alpha_V). \]
	(The signs may differ on which direction you pick for the generators;
	note that $\ZZ$ has two possible generators.)
	We can even format this as a matrix:
	\[ \phi = \begin{bmatrix} 1 & 1 \\ -1 & -1 \end{bmatrix}. \]
	And we observe $\phi(z_1 - z_2) = 0$, meaning this map has nontrivial kernel!
	That is, \[ \ker\phi = \left< z_1 - z_2 \right> \cong \ZZ. \]
	Thus, $\wt H_2(X) \cong \img \delta \cong \ker \phi \cong \ZZ$.
	We'll also note that $\img \phi$ is the set generated by $(\alpha_U, -\alpha_V)$;
	(in particular $\img\phi \cong \ZZ$ and the quotient by $\img\phi$ is $\ZZ$ too).

	The situation is similar with $\wt H_1(X)$: this time, we have
	\[
		\dots
		%\to \underbrace{\wt H_1(U \cap V)}_{\cong \ZZ \oplus \ZZ}
		\xrightarrow{\phi} \underbrace{\wt H_1(U) \oplus \wt H_1(V)}_{\cong \ZZ \oplus \ZZ}
		\overset{\psi}{\to} \wt H_1(X) \overset\partial\surjto
		\underbrace{\wt H_0(U \cap V)}_{\cong \ZZ}
		\to \underbrace{\wt H_0(U) \oplus \wt H_0(V)}_{=0} \to \dots
	\]
	and so we know that the connecting map $\partial$ is surjective,
	hence $\img \partial \cong \ZZ$.
	Now, we also have
	\begin{align*}
		\ker \partial \cong \img \psi &\cong \left( \wt H_1(U) \oplus \wt H_1(V) \right) / \ker \psi \\
		&\cong \left( \wt H_1(U) \oplus \wt H_1(V) \right) / \img \phi \\
		&\cong \ZZ
	\end{align*}
	by what we knew about $\img \phi$ already.
	To finish off we need some algebraic tricks. The first is \Cref{prop:break_exact},
	which gives us a short exact sequence
	\[
		0 \to \underbrace{\ker\partial}_{\cong \img\psi \cong \ZZ}
		\injto \wt H_1(X)
		\surjto \underbrace{\img\partial}_{\cong \ZZ} \to 0.
	\]
	You should satisfy yourself that $\wt H_1(X) \cong \ZZ \oplus \ZZ$ is the
	only possibility, but we'll prove this rigorously with \Cref{lem:split_exact}.
\end{proof}

Note that the previous example is of a different attitude than the previous ones,
because we had to figure out what the maps in the long exact sequence actually were
to even compute the groups.
In principle, you could also figure out all the isomorphisms in the previous proof
and explicitly compute the generators of $\wt H_1(S^1 \times S^1)$,
but to avoid getting bogged down in detail I won't do so here.

Finally, to fully justify the last step, we present:
\begin{lemma}[Splitting lemma]
	\label{lem:split_exact}
	For a short exact sequence $0 \to A \taking f B \taking g C \to 0$
	of abelian groups, the following are equivalent:
	\begin{enumerate}[(a)]
		\ii There exists $p : B \to A$ such that $A \taking f B \taking p A$ is the identity.
		\ii There exists $s : C \to B$ such that $C \taking s B \taking g C$ is the identity.
		\ii There is an isomorphism from $B$ to $A \oplus C$ such that the diagram
		\begin{diagram}
			&&& & B & &&&  \\
			0 & \rTo & A & \ruInj(2,1)^f & \dIsom^\cong & \rdSurj(2,1)^g & C & \rTo & 0 \\
			&&& \rdInj(2,1) & A \oplus C & \ruSurj(2,1) &&&
		\end{diagram}
		commutes. (The maps attached to $A \oplus C$ are the obvious ones.)
	\end{enumerate}
	In particular, (b) holds anytime $C$ is free.
\end{lemma}
In these cases we say the short exact sequence \vocab{splits}. The point is that
\begin{moral}
	An exact sequence which splits let us obtain $B$ given $A$ and $C$.
\end{moral}
In particular, for $C = \ZZ$ or any free abelian group,
condition (b) is necessarily true.
So, once we obtained the short exact sequence $0 \to \ZZ \to \wt H_1(X) \to \ZZ \to 0$,
we were done.
\begin{remark}
	Unfortunately, not all exact sequences split:
	An example of a short exact sequence which doesn't split is
	\[ 0 \to \Zc 2 \xhookrightarrow{\times 2} \Zc 4 \surjto \Zc 2 \to 0 \]
	since it is not true that $\Zc 4 \cong \Zc2 \oplus \Zc 2$.
\end{remark}
\begin{remark}
	The splitting lemma is true in any abelian category.
	The ``direct sum'' is the colimit of the two objects $A$ and $C$.
\end{remark}

\section\problemhead
\begin{problem}
	Complete the proof of \Cref{thm:reduced_homology_sphere},
	i.e.\ compute $H_n(S^m)$ for all $m$ and $n$.
	(Try doing $m=2$ first, and you'll see how to proceed.)
	\begin{hint}
		Induction on $m$, using hemispheres.
	\end{hint}
\end{problem}

\begin{problem}
	Compute the reduced homology groups
	of $\RR^n$ with $p \ge 1$ points removed.
	\begin{hint}
		One strategy is induction on $p$, with base case $p=1$.
		Another strategy is to let $U$ be the desired space and let $V$
		be the union of $p$ non intersecting balls.
	\end{hint}
	\begin{sol}
		The answer is $\wt H_{n-1}(X) \cong \ZZ^{\oplus p}$,
		with all other groups vanishing.
		For $p=1$, $\RR^n - \{\ast\} \cong S^{n-1}$ so we're done.
		For all other $p$, draw a hyperplane dividing the $p$ points into two halves
		with $a$ points on one side and $b$ points on the other (so $a+b=p$).
		Set $U$ and $V$ and use induction.

		Alternatively, let $U$ be the desired space and let $V$
		be the union of $p$ disjoint balls, one around every point.
		Then $U \cup V = \RR^n$ has all reduced homology groups trivial.
		From the Mayer-Vietoris sequence we can read $\wt H_k(U \cap V) \cong \wt H_k(U) \cap \wt H_k(V)$.
		Then $U \cap V$ is $p$ punctured balls, which are each the same as $S^{n-1}$.
		One can read the conclusion from here.
	\end{sol}
\end{problem}

\begin{sproblem}
	Let $n \ge 1$ and $k \ge 0$ be integers.
	Compute $H_k(\RR^n, \RR^n \setminus \{0\})$.
	\begin{hint}
		Use \Cref{thm:long_exact_rel}.
		Note that $\RR^n \setminus \{0\}$ is homotopy
		equivalent to $S^{n-1}$.
	\end{hint}
	\begin{sol}
		It is $\ZZ$ for $k=n$ and $0$ otherwise.
	\end{sol}
\end{sproblem}

\begin{problem}
	[Nine lemma]
	Consider a commutative diagram
	\begin{center}
	\begin{tikzcd}
		& 0 \ar[d] & 0 \ar[d] & 0 \ar[d] \\
		0 \ar[r] & A_1 \ar[r] \ar[d] & B_1 \ar[r] \ar[d] & C_1 \ar[r] \ar[d] & 0 \\
		0 \ar[r] & A_2 \ar[r] \ar[d] & B_2 \ar[r] \ar[d] & C_2 \ar[r] \ar[d] & 0 \\
		0 \ar[r] & A_3 \ar[r] \ar[d] & B_3 \ar[r] \ar[d] & C_3 \ar[r] \ar[d] & 0 \\
		& 0 & 0 & 0 &
	\end{tikzcd}
	\end{center}
	and assume that all rows are exact,
	and two of the columns are exact.
	Show that the third column is exact as well.
	\begin{hint}
		$0 \to A_\bullet \to B_\bullet \to C_\bullet \to 0$
		is a short exact sequence of chain complexes.
		Write out the corresponding long exact sequence.
		Nearly all terms will vanish.
	\end{hint}
\end{problem}

\begin{sproblem}[Klein bottle]
	\gim
	Show that the reduced homology groups of the Klein bottle $K$ are given by
	\[
		\wt H_n(K) =
		\begin{cases}
			\ZZ \oplus \Zc 2 & n = 1 \\
			0 & \text{otherwise}.
		\end{cases}
	\]
	\begin{hint}
		It's possible to use two cylinders with $U$ and $V$.
		This time the matrix is $\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}$
		or some variant though; in particular, it's injective, so $\wt H_2(X) = 0$.
	\end{hint}
\end{sproblem}

\begin{sproblem}
	[Triple long exact sequence]
	\label{prob:triple_long_exact}
	Let $A \subseteq B \subseteq X$ be subspaces.
	Show that there is a long exact sequence
	\[
		\dots \to H_n(B,A) \to H_n(X,A)
		\to H_n(X,B) \to H_{n-1}(B,A) \to \dots.
	\]
	\begin{hint}
		Find a new short exact sequence
		to apply \Cref{thm:long_exact} to.
	\end{hint}
	\begin{sol}
		Use the short exact sequence
		\[ 0 \to C_\bullet(A,B) \to C_\bullet(C,B) \to C_\bullet(C,A) \to 0 \]
		of chain complexes.
	\end{sol}
\end{sproblem}


\section*{Exercises}

\begin{ex}
  Let $B = \set{\begin{mymatrix}{r} 2 \\ -1 \end{mymatrix},
    \begin{mymatrix}{r} 3 \\ 2 \end{mymatrix}}$ be a basis of $\R^2$
  and let $\vect{x} = \begin{mymatrix}{r} 5 \\ -7 \end{mymatrix}$ be a
  vector in $\R^2$. Find $\coord{\vect{x}}_B$.
\end{ex}

\begin{ex}
  Let $B = \set{\begin{mymatrix}{r} 1 \\ -1 \\ 2 \end{mymatrix},
    \begin{mymatrix}{r} 2 \\ 1 \\ 2 \end{mymatrix},
    \begin{mymatrix}{r} -1 \\ 0 \\ 2 \end{mymatrix}}$ be a basis of
  $\R^3$ and let
  $\vect{x} = \begin{mymatrix}{r} 5 \\ -1 \\ 4 \end{mymatrix}$ be a
  vector in $\R^2$. Find $\coord{\vect{x}}_B$.
  \begin{sol}
    $\coord{\vect{x}}_B
    = \begin{mymatrix}{r} 2 \\ 1 \\ -1 \end{mymatrix}$.
  \end{sol}
\end{ex}

\begin{ex}
  Suppose $T:\R^3\to\R^3$ is a linear transformation such that
  \begin{equation*}
    T\begin{mymatrix}{r} 1 \\ 0 \\ 0 \end{mymatrix}
    = \begin{mymatrix}{r} 3 \\ 3 \\ 3 \end{mymatrix},
    \quad
    T\begin{mymatrix}{r} 0 \\ 1 \\ 0 \end{mymatrix}
    = \begin{mymatrix}{r} 1 \\ 2 \\ 3 \end{mymatrix},
    \quad
    T\begin{mymatrix}{r} 0 \\ 0 \\ 1 \end{mymatrix}
    = \begin{mymatrix}{r} 1 \\ 3 \\ -1 \end{mymatrix}.
  \end{equation*}
  Let $E=\set{\vect{e}_1,\vect{e}_2,\vect{e}_3}$ be the standard basis
  of $\R^3$, and let
  \begin{equation*}
    B = \set{\vect{v}_1,\vect{v}_2,\vect{v}_3} = \set{
      \begin{mymatrix}{r} 1 \\ 0 \\ 0 \end{mymatrix},~
      \begin{mymatrix}{r} 1 \\ 1 \\ 0 \end{mymatrix},~
      \begin{mymatrix}{r} 2 \\ 1 \\ 1 \end{mymatrix}
    }
  \end{equation*}
  be another basis.
  \begin{enumerate}
  \item  Find the matrix of $T$ with respect to $E$, i.e.,
    find $\coord{T}_{E,E}$.
  \item Find $\coord{T}_{B,B}$.
  \end{enumerate}
  \begin{sol}
    \begin{enumerate}
      \item The $i\th$ column of $\coord{T}_{E,E}$ is
      $\coord{T(\vect{e}_i)}_E = T(\vect{e}_i)$, so
      \begin{equation*}
        \coord{T}_{E,E} =
        \begin{mymatrix}{ccc}
          3 & 1 & 1 \\
          3 & 2 & 3 \\
          3 & 3 & -1
        \end{mymatrix}.
      \end{equation*}
      \item The $i\th$ column of $\coord{T}_{B,B}$ is
        $\coord{T(\vect{v}_i)}_B$. This requires a calculation:
        \begin{equation*}
          T(\vect{v}_1) = T\begin{mymatrix}{c} 1 \\ 0 \\ 0 \end{mymatrix}
          = \begin{mymatrix}{c} 3 \\ 3 \\ 3 \end{mymatrix} =
          - 3\begin{mymatrix}{c} 1 \\ 0 \\ 0 \end{mymatrix}
          + 0\begin{mymatrix}{c} 1 \\ 1 \\ 0 \end{mymatrix}
          + 3\begin{mymatrix}{c} 2 \\ 1 \\ 1 \end{mymatrix},
          \quad\mbox{therefore}
          \coord{T(\vect{v}_1)}_B =
          \begin{mymatrix}{r} -3 \\ 0 \\ 3 \end{mymatrix}.
        \end{equation*}
        \begin{equation*}
          T(\vect{v}_2) = T\begin{mymatrix}{c} 1 \\ 1 \\ 0 \end{mymatrix}
          = \begin{mymatrix}{c} 4 \\ 5 \\ 6 \end{mymatrix} =
          - 7\begin{mymatrix}{c} 1 \\ 0 \\ 0 \end{mymatrix}
          - 1\begin{mymatrix}{c} 1 \\ 1 \\ 0 \end{mymatrix}
          + 6\begin{mymatrix}{c} 2 \\ 1 \\ 1 \end{mymatrix},
          \quad\mbox{therefore}
          \coord{T(\vect{v}_2)}_B =
          \begin{mymatrix}{r} -7 \\ -1 \\ 6 \end{mymatrix}.
        \end{equation*}
        \begin{equation*}
          T(\vect{v}_3) = T\begin{mymatrix}{c} 2 \\ 1 \\ 1 \end{mymatrix}
          = \begin{mymatrix}{c} 8 \\ 11 \\ 8 \end{mymatrix} =
          - 11\begin{mymatrix}{c} 1 \\ 0 \\ 0 \end{mymatrix}
          + 3\begin{mymatrix}{c} 1 \\ 1 \\ 0 \end{mymatrix}
          + 8\begin{mymatrix}{c} 2 \\ 1 \\ 1 \end{mymatrix},
          \quad\mbox{therefore}
          \coord{T(\vect{v}_3)}_B =
          \begin{mymatrix}{c} -11 \\ 3 \\ 8 \end{mymatrix}.
        \end{equation*}
        We therefore have
        \begin{equation*}
          \coord{T}_{B,B} =
          \begin{mymatrix}{rrc}
            -3 & -7 & -11 \\
            0  & -1 &   3 \\
            3  &  6 &   8 \\
          \end{mymatrix}.
        \end{equation*}
      \end{enumerate}
\end{sol}
\end{ex}

\begin{ex}
  Let $T: \R^2 \to \R^2$ be a linear transformation defined by
  \begin{equation*}
    T \paren{\begin{mymatrix}{c} a \\ b \end{mymatrix}}
    = \begin{mymatrix}{c} a+b \\ a-b \end{mymatrix}.
  \end{equation*}
  Consider the two bases
  \begin{equation*}
    B_1 = \set{\begin{mymatrix}{r} 1 \\ 0 \end{mymatrix},
      \begin{mymatrix}{r} -1 \\ 1 \end{mymatrix}
    }
  \end{equation*}
  and
  \begin{equation*}
    B_2 = \set{\begin{mymatrix}{r} 1 \\ 1 \end{mymatrix},
      \begin{mymatrix}{r} 1 \\ -1 \end{mymatrix}
    }.
  \end{equation*}
  Find the matrix $M_{B_2,B_1}$ of $T$ with respect to the bases $B_1$
  and $B_2$.
  \begin{sol}
    $M_{B_2 B_1} = \begin{mymatrix}{rr}
      1 & -1 \\
      0 & 1
    \end{mymatrix}$.
  \end{sol}
\end{ex}

\begin{ex}
  Let $M=\begin{mymatrix}{rr} 1 & 2 \\ -2 & 1 \end{mymatrix}$, and
  consider the linear transformation $T:\Mat_{2,2}\to\Mat_{2,2}$ given
  by $T(A) = MAM$. Find the matrix of $T$ with respect to the basis
  \begin{equation*}
    B=\set{
      \begin{mymatrix}{cc} 1 & 0 \\ 0 & 0 \end{mymatrix},
      \begin{mymatrix}{cc} 0 & 1 \\ 0 & 0 \end{mymatrix},
      \begin{mymatrix}{cc} 0 & 0 \\ 1 & 0 \end{mymatrix},
      \begin{mymatrix}{cc} 0 & 0 \\ 0 & 1 \end{mymatrix}.
    }
  \end{equation*}
\end{ex}

\begin{ex}
  Consider the linear transformation $T:\Poly_3\to\Poly_3$ given by
  $T(p(x)) = p(x+1)$. Find $\coord{T}_{B,B}$, where $B=\set{1,x,x^2,x^3}$.
\end{ex}

\begin{ex}
  Let $\vect{v}=\begin{mymatrix}{r} 1 \\ -2 \\ 3 \end{mymatrix}$
  and consider the linear function
  $T(\vect{w}) = \proj_{\vect{v}}(\vect{w})$.  Find the matrix of $T$
  with respect to the standard basis of $\R^3$.
  \begin{sol}
    Recall that
    $\proj_{\vect{v}}(\vect{w})
    =\frac{\vect{v}\dotprod\vect{w}}{\norm{\vect{v}}^2}\vect{v}$. The
    desired matrix has $i\th$ column equal to
    $\proj_{\vect{v}}(\vect{e}_i)$. Therefore, the desired matrix is
    \begin{equation*}
      \frac{1}{14}\begin{mymatrix}{rrr}
        1 & -2 & 3 \\
        -2 & 4 & -6 \\
        3 & -6 & 9
      \end{mymatrix}.
    \end{equation*}
  \end{sol}
\end{ex}

\begin{ex}
  Let $\vect{v}=\begin{mymatrix}{r} 1 \\ -2 \\ 3 \end{mymatrix}$
  and consider the linear function
  $T(\vect{w}) = \proj_{\vect{v}}(\vect{w})$.  Find the matrix of $T$
  with respect to the basis
  \begin{equation*}
    B = \set{\vect{v}_1,\vect{v}_2,\vect{v}_3} = \set{
      \begin{mymatrix}{r} 1 \\ -2 \\ 3 \end{mymatrix},~
      \begin{mymatrix}{r} 2 \\ 1 \\ 0 \end{mymatrix},~
      \begin{mymatrix}{r} 3 \\ 0 \\ 1 \end{mymatrix}
    }.
  \end{equation*}
  \begin{sol}
    We have $T(\vect{v}_1) = \vect{v}_1$, $T(\vect{v}_2) = \vect{0}$,
    and $T(\vect{v}_3) = \vect{0}$. Therefore
    \begin{equation*}
      \coord{T}_{B,B} =
      \begin{mymatrix}{rrr}
        1 & 0 & 0 \\
        0 & 0 & 0 \\
        0 & 0 & 0
      \end{mymatrix}.
    \end{equation*}
  \end{sol}
\end{ex}

\begin{ex}
  Suppose that $V$ and $W$ are finite-dimensional vector spaces with
  bases $B$ and $C$, respectively. Let $T:V\to W$ be a linear
  transformation such that
  \begin{equation*}
    T(\vect{v}_i)=\vect{w}_i
  \end{equation*}
  for $i=1,\ldots,n$. Let $M$ be the matrix whose columns are
  $\coord{\vect{v}_1}_B,\ldots,\coord{\vect{v}_n}_B$, and let $N$ be
  the matrix whose columns are
  $\coord{\vect{w}_1}_C,\ldots,\coord{\vect{w}_n}_C$.  Suppose that
  $M$ is invertible. Show that $\coord{T}_{C,B} = NM^{-1}$.
  \begin{sol}
    Since $M$ is invertible, its columns
    $\coord{\vect{v}_1}_B,\ldots,\coord{\vect{v}_n}_B$ are linearly
    independent and span $\R^n$; it follows that
    $\vect{v}_1,\ldots,\vect{v}_n$ is a basis of $V$. To show that
    $\coord{T}_{C,B} = NM^{-1}$, it is sufficient to check that
    $NM^{-1}\coord{\vect{v}_i}_B = \coord{\vect{w}_i}_C$, for all
    $i=1,\ldots,n$. But by assumption, $\coord{\vect{v}_i}_B$ is the
    $i\th$ column of $M$, so that $\coord{\vect{v}_i}_B =
    M\vect{e}_i$, where $\vect{e}_i$ is the $i\th$ basis
    vector. Therefore $M^{-1}\coord{\vect{v}_i}_B  = \vect{e}_i$.
    On the other hand, $N\vect{e}_i$ is the $i\th$ column of $N$,
    i.e., $\coord{\vect{w}_i}_C$. We therefore have
    \begin{equation*}
      NM^{-1}\coord{\vect{v}_i}_B = N\vect{e}_i = \coord{\vect{w}_i}_C,
    \end{equation*}
    as desired.
  \end{sol}
\end{ex}


\subsection{Leaf-Zone Thermoregulation}
\label{sec:airthermoregulation}

\textbf{Purpose}: Maintaining desired leaf-zone air temperature and circulating air.

\textbf{Function}:
\begin{itemize}
    \item \textbf{Inputs}: Power, air temperature control signal (\ref{sec:automation}), air circulation control signal (\ref{sec:automation})
    \item \textbf{Outputs}: Heat to/from environment, by-product heat from/to surroundings, internal air circulation, internal air temperature sensor readings (\ref{sec:automation})
\end{itemize}

\textbf{Method}:
\begin{enumerate}
    \item \textit{Testing}:
    \begin{itemize}
        \item Heat pump direction and magnitude respond to control signal as expected;
        \item Fans operate as expected;
        \item Heat pump power exceeds maximum heat loss (temperature extremes)\footnote{i.e. if X Watts leave the system at MAX$\degree$C internal, and Y Watts enter the system at MIN$\degree$C internal, the heat pump must transfer >X, >Y Watts.};
        \item Heat pump power exceeds that required to reach temperature extremes in under 120 seconds given the system's heat capacity;
    \end{itemize}
    \item \textit{Process}:
    \begin{enumerate}
        \item Air is circulated throughout the environment;
        \item Temperature is measured, sent to automation system (\ref{sec:automation});
        \item Control module controls heat pump speed and direction (heating vs. cooling environment, \ref{sec:automation});
    \end{enumerate}
\end{enumerate}

\textbf{Calculations}:

Assuming an atmospheric pressure $P$ of 101.325kPa, a surroundings temperature range $T_{surr}$ of 22$\degree$C, a system target temperature range $[T_{sys-min}$, $T_{sys-max}]$ of 10-35$\degree$C, a molar mass of dry air $M$ of 28.97 $\frac g{mol}$, a specific heat capacity of dry air $c_p$ of $1.006 \frac{J}{g*\text{K}}$\footnote{Water vapour has a maximum concentration of 30g/kg at 30$\degree$C, or 3\%, which is negligible for mass and heat capacity calculations.}, a 4U Class 2 expanded configuration (2x2 units, 16 faces; see \ref{sec:housing}), and a face insulation RSI per mm of $0.0328\text{m}^2~  \degree \text{C}~\text{W}^{-1}~\text{mm}^{-1}$ (see \ref{sec:housing}):\\
\vspace{.05cm}
\begin{gather}
    \label{eqn:heatloss}
    Q_{loss}=\frac{(T_{surr}-T_{sys-max}) * A}{\text{RSI per mm} * \ell}=\frac{(22\degree \text{C}-35\degree \text{C}) * (16 \text{ faces} * 0.5\text{m} * 0.5\text{m})}{0.0328 \text{m}^2~  \degree \text{C}~\text{W}^{-1}~\text{mm}^{-1} * 25.4 \text{mm}}=-62.42 W\\
    \label{eqn:heatgain}
    Q_{gain}=\frac{(T_{surr}-T_{sys-min}) * A}{\text{RSI per mm} * \ell}=\frac{(22\degree \text{C}-10\degree \text{C}) * (16 \text{ faces} * 0.5\text{m} * 0.5\text{m})}{0.0328 \text{m}^2~  \degree \text{C}~\text{W}^{-1}~\text{mm}^{-1} * 25.4 \text{mm}}=57.61 W\\
    \label{eqn:airmass}
    m_{air}=\frac{P*V*M}{R*T_{avg}}=\frac{101325\text{Pa}*(0.5\text{m}*0.5\text{m}*0.5\text{m}*4\text{ units})*28.97\frac g{mol}}{8.314\frac{J}{\text{mol}*K}*300\text{K}}=588.4g\\
    \label{eqn:heating}
    Q_{heating}=\frac{m*c_p*(T_{surr}-T_{sys-max})}{t}=\frac{588.4g*1.006\frac{J}{g*\text{K}}*(22\degree \text{C}-35\degree \text{C})}{120\text{ sec}}=-64.13\text{W}\\
    \label{eqn:cooling}
    Q_{cooling}=\frac{m*c_p*(T_{surr}-T_{sys-min})}{t}=\frac{588.4g*1.006\frac{J}{g*\text{K}}*(22\degree \text{C}-10\degree \text{C})}{120\text{ sec}}=59.19\text{W}
\end{gather}

\clearpage

$\therefore$ A thermoelectric system able to transfer at least \textbf{70W} (such as \cite{peltier}, which transfers up to 85W) will supply enough power to heat/cool the system from ambient to extremes in 120 seconds and maintain temperature.

\begin{gather}
    \label{eqn:thermalresistance-hot}
    R_{\theta~Peltier-Surr}=R_{\theta~Peltier-Sink}+R_{\theta~Sink-Air}\le\frac{T_{h~max} - T_{surr}}{Q_{max}}=\frac{50\degree C - 22\degree C}{85W}=0.329\degree \text{C W}^{-1}\\
    \label{eqn:thermalresistance-cold}
    R_{\theta~Peltier-Sys}=R_{\theta~Peltier-Sink}+R_{\theta~Sink-Air}
\end{gather}

\textbf{Features}:
\begin{itemize}
    \item \textit{Circulation Fans}: Located in growth environment to circulate air for even temperature distribution, rapid system flushing, and automatic pollination.
    \item \textit{Temperature Sensors}: Multiple temperature and humidity sensors \cite{sht31} on small daughterboards frame-mounted throughout the growth environment to measure air temperature ($\degree$C). Informs the \textbf{PID control loop}.
    \item \textit{Heat Pump}: Pumps heat in or out of the growth environment. Is comprised of:
    \begin{itemize}
        \item \textit{Peltier Device}: 85W bidirectional solid-state \textbf{thermoelectric device} (aka Peltier tile) \cite{peltier} pumps heat from one face to the other. Better space efficiency, less complexity (no liquids, pressurized fluids, etc.), and more precise than other methods.
        \item \textit{Thermoelectric Driver Board}: Controls \textit{magnitude} and \textit{direction} of heat transfer via a \textbf{dimmable voltage source} (low-pass-filtered PWM to a voltage buffer and amplifier w/ feedback) and \textbf{relay H-bridge}, respectively. See Figures \ref{fig:peltierdriver} and \ref{fig:thermoregulation_driver}.
        \item \textit{Heat Sinks}: Aluminum blocks with fins hold and exchange heat between air and Peltier devices. One set on each side of the Peltier (inside and outside environment) builds "heat pump". Mating face coated with thermal compound for better transfer.
        \item \textit{Heat Sink Fans}: Located on both sets of heat sinks for better heat dissipation.
    \end{itemize}
    \item \textit{PID Control Loop}: A propotional-integral-derivative control loop enables increased accuracy (see equation \ref{eqn:pid}). Temperature sensors inform the loop, "error" is calculated (current vs desired temperature, see $E(t)$ \ref{eqn:piderror}), and this informs the magnitude and direction of heat pump control ($u(t)$). Requires tuning of parameters ($K_p, K_i, K_d$; automatic). Built into the automation system (see \ref{sec:automation});
\end{itemize}

\begin{gather}
    \label{eqn:piderror}
    E(t)=T_{target}(t)-T_{measured}(t)\\
    \label{eqn:pid}
    u(t)=K_pE(t)+K_i\int_0^{t}E(t)dt+K_d\frac{dE(t)}{dt}
\end{gather}

\clearpage

\textbf{Figures}

\begin{figure}[h!]
  \centering
  \includegraphics[width=\textwidth]{../assets/figures/airthermoregulation_simulation.png}
  \hfill
  \caption{Thermoelectric driver circuit simulation \cite{thermo-falstad}}
  \label{fig:peltierdriver}
\end{figure}

\begin{figure}[h!]
    \centering
    \begin{subfigure}{.49\textwidth}
        \centering
        \frame{\includegraphics[width=\textwidth]{../assets/schematics/thermoregulation_driver_sch.png}}
        \caption{Schematic.}
        \label{fig:thermoregulation_driver_sch}
      \end{subfigure}
      \hspace{.02\textwidth}
      \begin{subfigure}{.43\textwidth}
        \centering
        \frame{\includegraphics[width=\textwidth]{../assets/schematics/thermoregulation_driver_brd.png}}
        \caption{PCB layout.}
        \label{fig:thermoregulation_driver_brd}
      \end{subfigure}
      \caption{Thermoelectric driver board.}
      \label{fig:thermoregulation_driver}
\end{figure}

\clearpage


\subsection{Definition of the \acrshort{twm} problem}%
\label{sub:definition_of_the_twm_problem}

Our modelization is practically the same as the one defined by \citeauthor{comp_mcts_mo}\cite{comp_mcts_mo} with minor changes.
The \gls{twm} is set on a squared grid of cells \(X\).
Each cell \(x \in X\) is defined by two attributes:

\begin{itemize}
    \item \(B(x)\) is a boolean indicating if the cell \(x\) is on fire.
    \item \(F(x)\) is an integer representing the amount of fuel on cell \(x\). The more fuel, the longer it burns.
\end{itemize}

A state is then represented by the values of these two attributes for all \(x \in X\).
A state is considered final when no cell is burning anymore.

We have a set \(T\) of firefighter teams which we consider identical.
An action consists in placing each team \(t \in T\) setting the attribute \(a^{i} \in X\).
Every team can be assigned to any cell and can even be assigned to the same cells.

A burning cell consumes its fuel at a constant rate.
If a cell has no more fuel, it extinguishes.
The burning rate constitutes the time unite of the game.
Meaning that, each turn, all burning cells consume one fuel, therefore:

\[
    F_{t+1}(x) =
    \begin{cases}
        F_{t}(x)& \text{if } \lnot B_{t}(x) \lor F_{t}(x) = 0\\
        F_{t}(x) - 1& \text{otherwise }
    \end{cases}
\]
The evolution of the attribute \(B(x)\) is stochastic.
It is set on a terminal state machine.
This transition model is given by the following equation defining \(\rho_{1}\) and \(\rho_{2}\) and the figure~\ref{fig:B_state_machine}.

\[
    \rho_{1} =
    \begin{cases}
        1 - \Pi_{y}(1 - P(x, y)B_{t}(x))& \text{if } F_{t}(x) > 0\\
        0& \text{otherwise }
    \end{cases}
\]
\[
    \rho_{2} =
    \begin{cases}
        1& \text{if } F_{t}(x) = 0\\
        1 - \Pi_{i}(1 - S(x)\delta_{x}(a^{i}))& \text{otherwise }
    \end{cases}
\]

\begin{figure}[htpb]
    \ctikzfig{../../../figures/B_state_machine}
    \caption{\(B(x)\) transition model}%
    \label{fig:B_state_machine}
\end{figure}

\(P(x, y)\) represents the probability that \(y\) ignites \(x\) if \(y\) is burning.
We will only consider case where only neighbors cell can ignite each other.
\(S(x)\) is the probability that a firefighter team extinguish the cell \(x\).
\(\delta_{x}(a^{i})\) equals \(1\) if firefighter team \(i\) has been placed on cell \(x\) and \(0\) otherwise.

The reward \(R(x)\) is always negative and is the cost of the fire on cell \(x\).
The reward at time \(t\) is therefore \(\Sigma_{x \in X} B_{t}(x)R(x)\).
The score of a final state is the sum of all rewards.


\section{Statistical Analysis}

The Analysis module contains some simple statistics support in the
form of a \hyperref{TCL exported C++ data type}{TCL exported C++ data
  type (\S}{)}{TCLTYPE}. The class definition is:

\begin{verbatim}

struct Stats: public array_ns::array<float>
{
  double sum, sumsq;
  float max, min;
  Stats(): sum(0), sumsq(0), max(-std::numeric_limits<float>::max()),
           min(std::numeric_limits<float>::max()) {}

  void clear();
  double av();
  double median();
  double stddev();

  Stats& operator<<=(float x);
  Stats& operator<<=(const array_ns::array<float>& x);
  Stats& operator<<=(const array_ns::array<double>& x);

  void add_data(TCL_args args);
};

struct HistoStats: public Stats
{
  unsigned nbins;
  bool logbins;
  HistoStats(): nbins(100), logbins(false) {}
  array_ns::array<double> histogram();
  array_ns::array<double> bins();

  double loglikelihood(TCL_args args);

  array_ns::array<double> fitPowerLaw(); //< fit x^{-a} - return a and x_min
  double fitExponential()                //< fit exp(-x/a) - return a
  array_ns::array<double> fitNormal();   //< fit exp(-(x-m)^2/2s, return m, s

  array_ns::array<double> fitLogNormal(); //< fit exp(-(log(x)-log m)^2/2s, return m, s
};
\end{verbatim}

These classes can be used from the TCL programming environment like
this example:

\begin{verbatim}

HistoStats h

unuran rand
rand.set_gen {distr=pareto(.5,1.7);}

for {set i 0} {$i<10000} {incr i} {
    h.add_data [rand.rand]
}

# obtain average, median, min, max, standard deviation and no. samples
puts stdout "[h.av] [h.median] [h.min] [h.max] [h.stddev] [h.size]"

# fit power law distribution, returning slope and xmin
puts stdout "[h.fitPowerLaw]"

# return log likelihood ratio for power law versus lognormal
array set pl [h.fitPowerLaw]
array set ln [h.fitLogNormal]
set R [h.loglikelihood powerlaw($pl(0),$pl(1))
                       lognormal($ln(0),$ln(1) $pl(1)]
puts stdout "R=$R p=[expr fabs([erfc $R])]"
\end{verbatim}

Fitting parameters is achieved using the likelihood method as
described in \cite{Clauset-etal07}.

The log likelihood ratio function returns ${\cal R}/\sqrt{2n}\sigma$,
where ${\cal R}=\ln \prod_i p_1(x_i)/p_2(x_i)$ is the logarithm of the
ratio of likelihoods for the two distributions $p_1$ and $p_2$.

If the log likelihood ratio is positive, it means the $p_1$ is more
likely to fit the data than $p_2$, and negative is the other way
around. $p=|\mathrm{erfc}({\cal R})|$ is the probability that this conclusion
is wrong.\cite{Clauset-etal07}

The histogram function allows one to do histograms without using the
GUI widget, which is useful for larger collections of data. The
parameters to the histogram method are the number of bins (default
100) and whether linear or logarithmic binning is used.


\chapter{Matrix Games}
This part uses mathematical methods to model interactions and conflicts between
different actors. To simplify the analysis we are going to consider interactions
between \emph{two} actors that are trying to maximize their payoffs.

\begin{definition}
  The \emph{strategic form}, or \emph{normal form}, of a two-person game is
  a tuple $(X, Y, A, B)$ such that $X$ and $Y$ are some nonempty sets, and
  $A, B  : X \times Y \to \R$.

  We say that this game is a matrix game if $X$ and $Y$ are finite.
\end{definition}
The interpretation is as follows. Simultaneously, the first player chooses
a strategy $x \in X$ and the second player chooses a strategy $y \in Y$ , each
unaware of the choice of the other. Then their choices are made known and the
first player wins $A(x, y)$ and the second player wins $B(x, y)$. (Depending on
the monetary unit involved, $A(x, y)$ and $B(x, y)$ will be dollars, rubles,
euros, etc.) If $A(x, y)$ or $B(x, y)$ is negative, the corresponding player
loses the absolute value of this amount.

It is important to note that the notion of a strategy is very broad; e.g., a
strategy for a game of chess, is a complete description of how to play the game,
of what move to make in every possible situation that could occur. We are going
to ignore the fact that in the game of chess it is physically impossible to
describe all possible strategies since there are too many of them (in fact,
there are more strategies than there are atoms in the known universe). On the
other hand, the number of games of tic tac toe is rather small, so that it is
possible to study all strategies and find an optimal strategy for each player.

In cases when $X$ and $Y$ are small sets it is convenient to describe such games
using tables. Let us consider the game described by
\Cref{table:heads-and-tales-game}.
\begin{table}
  \begin{center}
    \begin{tabular}{l l l  l  l  l  l  l  l}
      \toprule
            & heads  & tales   \\
      \midrule
      heads & 1, -1 & -1, 1   \\
      tales & -1, 1 & 1, -1   \\
      \bottomrule
    \end{tabular}
  \end{center}
  \caption{Heads and tales game}
  \label{table:heads-and-tales-game}
\end{table}
In this game two players put one coin each on the table: if the the coins have
the same side up, then the second player pays $1$ dollar to the first player,
otherwise the first player pays $1$ dollar to the second player. In other words,
the firs number denotes the payoff of the first player and the second denotes
the payoff of the second player.

\begin{exercise}
  Describe the game in normal form corresponding to rock paper scissors.
\end{exercise}

An important class of games is zero-sum games.
\begin{definition}
  A game $(X, Y, A, B)$ is \emph{zero-sum} if $A(x, y) = -B(x, y)$ for all
  $x \in X$ and $y \in Y$.
\end{definition}
It is clear that the game we described is a zero-sum game.

\section{Domination and Pareto Optimal Strategies}
In \Cref{part:combinatorial-games} we studies optimal strategies; however, in
case of games in the normal form it is not clear what does it mean optimal. To
illustrate this difficulty, let us discuss the most famous game, the prisoner's
dilemma:
\begin{game}
  Two members of a criminal gang are arrested and imprisoned. Each prisoner is in
  solitary confinement with no means of communicating with the other. The
  prosecutors lack sufficient evidence to convict the pair on the principal
  charge, but they have enough to convict both on a lesser charge. Simultaneously,
  the prosecutors offer each prisoner a bargain. Each prisoner is given the
  opportunity either to betray the other by testifying that the other committed
  the crime, or to cooperate with the other by remaining silent. The possible
  outcomes are:
  \begin{itemize}
    \item If A and B each betray the other, each of them serves two years in
      prison.
    \item If A betrays B but B remains silent, A will be set free and B will serve
      three years in prison (and vice versa).
    \item If A and B both remain silent, both of them will serve only one year in
      prison (on the lesser charge).
  \end{itemize}
\end{game}

It is clear that this game can be described using the following table.
\begin{center}
  \begin{tabular}{l l l  l  l  l  l  l  l}
    \toprule
               & cooperates  & defects   \\
    \midrule
    cooperates & -1, -1      & -3, 0    \\
    defects    & 0, -3       & -2, -2   \\
    \bottomrule
  \end{tabular}
\end{center}

Let us try to put ourselves into these prisoners shoes. If our partner is silent,
then it is better for us to defect (in this case we are free immediately); if
our partner defects, then is is also better for us to defect (we'll get two
years instead of three). Therefore, no matter what our partner does it is always
better to defect. Since the game is symmetric both players come to this
conclusion and get two years each; however, if both of them cooperates they
would serve only one year\footnote{%
  In 1993 Frank, Gilovich, and Regan conducted an experimental study of the
  prisoner's dilemma. The subjects were students in their first and final years
  of undergraduate economics, and undergraduates in other disciplines. Subjects
  were paired, placed in a typical game scenario, then asked to choose either to
  ``cooperate'' or to ``defect''.

  First year economics students, and students doing disciplines other than
  economics, overwhelmingly chose to cooperate. But 4th year students in
  economics tended to not cooperate. Therefore, the authors concluded that
  that the study of economics reduces cooperation in games. The idea is
  that much of the time cooperation and consideration of other's perspective are
  irrational in the narrow sense of the word. Thus, learning that cooperation is
  irrational in some situations is influencing the behavior of the students
  towards less cooperation, presumably to the negative.
}.

Let us generalise the argument we used to justify why defecting is better than
cooperating.
\begin{definition}
  Let $(X, Y, A, B)$ be a game in normal form. We say that $x_1 \in X$
  \emph{dominates} $x_2 \in X$ iff $A(x_1, y) \ge A(x_2, y)$ for all $y \in Y$.
  We also say that $x_1$ \emph{strictly dominates} $x_2$ if $A(x_1, y) > A(x_2,
  y)$ for all $y \in Y$.

  Similarly we may define domination of strategies for the second player.
\end{definition}
It seems reasonable to never choose $x_2$ provided that $x_1$ dominates $x_2$; in
the prisoners dilemma ``defects'' dominates ``cooperates''; hence, it seems
choosing ``defects'' is the best behaviour for rational agents.

However, it also obvious that if both players cooperate is way better than if
both of them defects; this observation leads to the following definition.
\begin{definition}
  Let $(X, Y, A, B)$  be a game in the normal form. We say that a pair of
  strategies $(x, y) \in X \times Y$ is Pareto optimal if either
  $A(x', y') < A(x, y)$ or $B(x', y') < B(x, y)$ for any
  $(x', y') \in X \times Y$.
\end{definition}
In other words, a pair of strategies is Pareto optimal if any other choice would
decrease the payoff for at least one of the players.

\section{Prisoners Dilemma In Real Life}
The reason that prisoner's dilemma is that famous is because there are many
examples human interactions as well as interactions in nature that have the same
payoff matrix. Let us consider two of them.

\paragraph{Political science.} In political science, the prisoner's dilemma is
often used to demonstrate the coherence of strategic realism, which holds that
in international relations, all countries (regardless of their internal policies
or professed ideology), will act in their rational self-interest given
international anarchy. A standard example is an arms race like the Cold War:
during the Cold War the opposing alliances of NATO and the Warsaw Pact both had
the choice to arm or disarm. From each side's point of view, disarming when
their opponent continued to arm would have led to military inferiority and
possible annihilation. Conversely, arming whilst their opponent disarmed would
have led to superiority. If both sides chose to arm,
neither could afford to attack the other, but both incurred the high cost of
developing and maintaining a nuclear arsenal. If both sides chose to disarm, war
would be avoided and there would be no costs.

Although the ``best'' overall outcome is for both sides to disarm, the rational
course for both sides is to arm, and this is indeed what happened. Both sides
poured enormous resources into military research and armament in a war of
attrition for the next thirty years until the Soviet Union could not withstand
the economic cost.

\paragraph{Sports.} Another example is doping in sports. Two competing athletes
have the option to use an illegal and/or dangerous drug to boost their
performance. If neither athlete takes the drug, then neither gains an advantage.
If only one does, then that athlete gains a significant advantage over their
competitor, reduced by the legal and/or medical dangers of having taken the
drug. If both athletes take the drug, however, the benefits cancel out and only
the dangers remain, putting them both in a worse position than if neither had
used doping.


% Created 2021-10-22 Fri 12:18
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\author{dnx}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={dnx},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 27.2 (Org mode 9.4.4)},
 pdflang={English}}
\begin{document}

\tableofcontents

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\usepackage{subcaption}
\begin{document}

	\title{Introduction to Circuitry}
	\author{}

	\maketitle

	\begin{abstract}
	In this article we will have an in-depth look at some of the math necessary for circuit analysis, namely exponential function and natural logarithm.
	We also see their uses in a simple circuit as well.
	\end{abstract}

	\section{Natural Logarithm}
	Let's find a function that would satisfy the following equation:

	\begin{equation}
	\label{simple_equation}
	f(ax) = f(a) + f(x)
	\end{equation}

	($f$ as a function of $x$ and $a$ as an arbitrary constant.)

	Why do we want such a function? Because it has numerous important properties that we will discuss later in this document.
	For now let's continue finding such a function.
	A function $f$ that would satisfy an equation such as above, could have many different forms and finding them all would be a difficult or even an impossible task.
	What we want to do instead, is to add more properties to the equation that would narrow down the function we are looking for.
	The first property is that we don't want $f$ to be a function that maps all of its inputs to $0$; Because such a function \textit{would} satisfy the equation (1), but it wouldn't be useful at all.
	The next important property that we want to add which makes a lot sense too, is to find an $f$ that would be continuous and differentiable\footnote[1]{Because in the wide areas of mathematics we are only interested in continuous and differentiable functions.}.
	In other words, Finding an $f$ that would make the derivative (in respect to $x$) of the right-hand side and left-hand side of equation (1), equal.
	If we find such a function that for a range of inputs $\frac{d}{dx}f(ax)$ gives \textbf{equal} values as $\frac{d}{dx}(f(a) + f(x))$, we can use the fundamental theorem of Calculus to find the function itself.
	The function found in this procedure not only could be an answer for equation (1), moreover it would be differentiable for that range of the values of $x$.
	Now to find such a function let's differentiate both sides of equation (1). We get:

	$$ \frac{d}{dx} f(ax) = \frac{d}{dx} f(a) + \frac{d}{dx} f(x) $$

	Now $\frac{d}{dx} f(a)$ is just $0$ (because it's not a function of $x$), therefore we get:

	$$ \frac{d}{dx} f(ax) = \frac{d}{dx} f(x) $$

	What function $f$ would make the above equation valid? Let's differentiate left side and see what we get.
	By implicit differentiation:

	$$ \frac{d}{dx} f(ax) = \frac{d}{dx} ax \frac{d}{dx} f(ax) = a \frac{d}{dx} f(ax) $$

	Now if $$\frac{d}{dx} f(ax) $$ yields the reciprocal of it's argument $ax$, namely $\frac{1}{ax}$ then:

	$$ \frac{d}{dx} f(ax) =  a . \frac{1}{ax} = \frac{1}{x} = \frac{d}{dx} f(x) $$

	We now know the \textit{derivative} of $f$ should give the reciprocal of it's argument, then $f$ itself is the \textit{integration} of the reciprocal of it's argument:

	$$f(x) = \int_{c}^{x} (1/t) dt$$

	We are almost there.
	To recap, the function $f$ that we got so far has the following property:

	$$ \frac{d}{dx} f(ax) = \frac{d}{dx} f(x) $$

	However, we initially differentiated the equation (1) and we got to this function so far.
	If the equation had \textbf{any other} constant beside $ln(a)$, it would still cancel out in the differentiation process.
	So by the (second) fundamental theorem of Calculus, integrating both sides, give the functions \textit{plus} some constant that it could have any value.
	Therefore for the function we got so far:

	$$ f(ax) = f(x) + C $$

	For some constant $C$.
	However if we define $f$ as the integral of $1/t$ from \textbf{"\boldmath{$1$} to \boldmath{$x$}}"  instead of an \textbf{"arbitrary \boldmath{$c$} to \boldmath{$x$}"}, namely:

	$$f(x) = \int_{1}^{x} (1/t) dt$$

	Not only the derivative of $f(ax)$ would equal to derivative of $f(x)$, moreover:

	$$ f(ax) = f(x) + f(a)$$

	Because by letting $x = 1$ we get:

	$$ f(a) = f(1) + C$$
	$$ f(a) = 0 + C$$
	$$ f(a) = C $$

	And $f(ax)$ for other $x$s besides $x = 1$ would still give this constant - which is equal to $f(a)$ - plus $f(x)$. We are now done.

	Such a function has a special name in math and its called \textbf{natural logarithm} and it's denoted by $ln$:

	\begin{equation}
	ln(x) = \int_{1}^{x} (1/t) dt
	\end{equation}

	\subsection{Logarithm in "other bases"}

	If $b > 0$, $b \neq 1$ and if $x > 0$, the logarithm of $x$ to the "base" $b$ is defined as:

	$$ \log_b x = \frac{ln x}{ln b} $$

	A common base $b$ is 10. Example:

	$$ \log_{10} 1000 = \frac{ln 1000}{ln 10} = \frac{\ln (10 * 10 * 10)}{ \ln 10} = \frac{3 \ln 10}{\ln 10} = 3 $$

	or for example another widely used base, logarithm in base 2:

	$$ \log_{2} 8 = \frac{ln 8}{ln 2} = \frac{\ln (2 * 2 * 2)}{ \ln 2} = \frac{3 \ln 2}{\ln 2} = 3 $$

	\section{The constant $e$}
	The constant $e$ is a number that we define as the value in which $\ln(e) = 1$.
	Plugging it in the definition of the natural logarithm, it means it's a number which the area under the curve of $\frac{1}{x}$ \textit{from $1$ to that number $e$} is 1.

	Now a property of natural logarithm that we left out is the following property, for a \textit{rational} $n$:

	$$ \ln(x^n) = n\ln(x) $$

	Proof (by power rule):

	$$ \frac{d}{dx} \ln(x^n) = \frac{1}{x^n} . \frac{d}{dx} x^n = \frac{n}{x^n} . x^{n-1} = \frac{n}{x} = n \ln x$$

	Even though the following property is true only for the rational powers of $x^n$, we can "fill in" for the irrational numbers as well.
	By defining the output for the irrational powers as the limit approaching to the closest neighboring rational power.
	Graphically we fill in the wholes in the graph for irrational numbers.

	To recap, we have found a function and we called it $\ln(x)$ which has the following properties:
	\begin{flalign*}
	& 1) \; \ln(ax) = \ln(a) + \ln(x) &\\
	& 2) \;\; (\ln x)' = \frac{1}{x} &\\
	& 3) \; \ln(x^n) = n \ln x &
	\end{flalign*}

	Now we want to find the \textit{inverse} of the natural logarithm.
	To do so, what expression should we give to the natural logarithm that it would give us the term $x$ itself?
	Such an expressions would be the inverse of the natural logarithm.
	This expression is $e^x$; Because:

	$$ \ln e^x = x \ln e = x $$

	Therefore, $e^x$ is defined as the inverse of the natural logarithm:

	$$ e^x = \ln ^{-1} x $$

	Now we will discover one of the most significant mathematical properties of this function, which appears frequently in the universe.

	\section{The Function $\mathbf{e^x}$}
	The function $e^x$ has numerous important properties.
	One of the most important ones, which we use over and over in circuitry and other fields as well is \textit{the derivative of the function $e^x$ which is $e^x$ itself}.

	Proof:
	$$ y = e^x $$

	Therefore (by the 3nd property of $\ln$):

	$$ \ln e^x = x $$

	Taking implicit differentiation of both-sides (by the 2nd property of $\ln$):

	$$ \frac{d}{dx} \ln y = \frac{d}{dx}x $$
	$$ \frac{1}{y} \frac{d}{dx} y = 1$$

	Finally:
	$$\frac{d}{dx} y = y$$

	Which means:

	\vspace{5mm}

	\begin{equation}
	\frac{d}{dx} e^x = e^x
	\end{equation}

	\vspace{5mm}

	This function is called the exponential function and is also denoted by \boldmath$\exp(x)$.
	This function is the solution to \textit{$y' = y$}.
	In other words, what function gives the same values as it's instantaneous rate of change? $e^x$.

	Concept of logarithms and exponential function could be approached in many different ways.
	One can differentiate the function $a^x$ via plugging it into the definition of the derivative.
	Using this method we get a function times a weird-looking limit.
	Thus continuing to prove that this limit is the inverse of the famous constant $e$ to the power of $a$; Then we shall call this inverse function as the logarithm in base $e$.
	Another way which we used in this article is by defining the logarithm \textit{first} then defining the constant $e$ as a number that if we give to the natural logarithm, it yields $1$.
	Anyhow all the different ways lead to the same concepts but we used the latter one which we think is the right way to explain these topics.

	\subsection{Applications of the Function $e^x$}
	We now examine a case which the exponential function appear in circuit analysis.
	Assume a simple circuit consisting of a battery, a resistor and an inductor.
	These elements are connected in series and has the value as shown in the following schematic:

	\begin{figure}[h!]
 	\centering
	\begin{subfigure}[b]{0.6\linewidth}
		\includegraphics[width=\linewidth]{circuit1.png}
	\end{subfigure}
	\end{figure}

	We now want to analyze this circuit; This means that our goal is to find the voltage drops (of each element) and the currents flowing through each loop.
	For analyzing this circuit we use the laws that come from the nature.
	Namely, conservative of energy (which is called KVL in circuit analysis), Ohm's law and the formula for induced voltage of an inductor.
	Let's start analyzing this circuit step-by-step.

	By the law of conservation of energy, adding up the voltage drops of each element should sum up to zero.
	This means:

	$$V_{battery} + V_{resistor} + V_{inductor} = 0$$

	Now this let's substitute corresponding values of each term.
	Voltage drop across a battery is constant over time and in this example is 5; Let's denote this constant $\varepsilon$ for a general battery.
	Therefore $V_{battery} = \varepsilon$.
	For the resistor by Ohm's law, voltage drop across the resistor should be proportional to a constant, times the current flowing through it: $V_{resistor} = Ri$.
	As for the inductor, voltage drop across it is proportional to a constant times the \textit{instantaneous rate of change} of it's current over time\footnote[1]{The exact reason why the induced voltage of an inductor (or more generally Faraday's law of induction) is proportional to rate of change of it's current over time comes from a more general theory which is the "theory of relativity".
	We shall discuss these topics in the later articles in-details.
	But in basic terms when electrons gain speed they (and space between them) gets contracted; Thus producing "magnetic effect".
	Changing of this magnetic effect causes instant accumulation of electrons which EMF gets produced.}.
	Hence: $V_{inductor} = L\frac{di}{dt}$.
	Now let's plug all of these into the equation.

	$$\varepsilon - Ri(t) - L\frac{di}{dt} = 0 $$

	Bringing $-L\frac{di}{dt}$ to the other side we get:

	$$\varepsilon - Ri(t) = L\frac{di}{dt} $$

	Now here comes the interesting part.
	Let's take the whole expression on the left-hand side as a single term and call it $U$.
	Therefore:

	$$ U = \varepsilon - Ri(t) $$

	Taking a look at this expression, we see $\varepsilon$ term is just a constant, therefore the $U$ is merely a function of $i$ over time.
	On the right-hand side we have the derivative of $i$ over time! (times a constant $L$ too).
	Essentially what we have here is a function on one side and it's derivative on the other side.
	To find this $U$ we simply have to look at a function \textit{whose value at different inputs, are same as it's derivative at those points}.
	This is were exponents come to play.
	Solving this equation, we get:

	\begin{equation}
	 	i(t) = \frac{\varepsilon}{R}(1 - e^{-(R/L)t})
	\end{equation}

	By the way, the exact steps of how we got to this expression is omitted; They are merely algebraic manipulations for solving these differential equations.
	The important part is that this function is in form of exponential function $e^x$.

	To recap what really happened, we know by the physical property of an inductor that the voltage drop across it, is proportional to the rate of change of it's current over time.
	And this value, because of the conservation of energy, should be equal to the voltage gain/drop of the battery plus the resistor.
	The voltage gain of battery is just a fixed number over time; But for the resistor we know from Ohm's law that the voltage drop across it is linearly proportional (approx. for a real resistor) to it's current. So from putting these facts together, we are looking for a set of values over time in which their instantaneous rate of change is the same as their own values at those points.

	Finally substituting values of $\varepsilon$, $R$ and $L$ for this into this equation we get:

	$$ i(t) = 0.05(1 - e^{-(100)t})  $$

	Plotting this function we get:
	\begin{figure}[h!]
		\centering
		\begin{subfigure}[b]{0.9\linewidth}
			\includegraphics[width=\linewidth]{Figure_1.png}
		\end{subfigure}
	\end{figure}

	Similar situation would happen if we had a capacitor instead of an inductor.
	With the difference that the part in which the derivative appears (in this case derivative of charge $Q$ over time instead of current $i$ over time), would be for the resistor.
	Namely, if we had a capacitor instead of an inductor in this circuit, we should have gotten:

	$$\varepsilon - Ri(t) - \frac{Q}{C} = 0 $$

	Because the voltage drop across a capacitor is proportional to the accumulated charges $Q$ in one plate over a constant $C$ (capacitance).
	And that number, should be equal to the resistivity  $R$, times the current for the resistor part (Ohm's law).
	Current by definition is just the change of $Q$ overtime ($i(t) = \frac{dq}{dt}$).
	Consequently, solving that differential equation should give us the change of $Q$ over time (instead of $i$) as an exponential.

	In future tutorials we shall discuss and analyze the circuits consisting of both capacitor and inductor beside resistors and power supplies.
	For now, our goal was to get the big picture of the math behind this circuits and solve a basic one.

	\section{What's Next?}
	In the follow up article, we will discuss some math behind circuits consisting of sinusoidal signals; Namely \textit{root mean square (rms)} and to see why is's convenient to define such an expression in AC circuitry.
	Subsequently we'll start writing some software code that we'd use in later articles and projects as boilerplate.

	These documents are published under an open license (see the project's root directory for more info), and were intended to be part of an open and a collaborative project.
	Feel free to fork this document, send pull request and also give your feedback.
	Thanks for reading!

\end{document}
\end{document}