\input{../common/common.tex}

\title{Math notes - Twelve Coins}
\author{Uwe Hoffmann}
\hypersetup{colorlinks, pdftitle={Math notes - Twelve Coins}}

\begin{document}

\setcounter{chapter}{1}
\section*{Twelve Coins}

\newthought{Coin weighings} are the topics of the problem \footnote{\bibentry{Canin94} \\ or \\ Problem 1-111. on page 47 in \bibentry{loehr2017combinatorics}} in this note.\index{coin weighings}

\vspace{10 mm}
\begin{problem}
Of twelve coins, one is counterfeit and weighs either more or less than all the others. The others weigh the same. With a balance scale, on which one side may be weighed against the other, you are to use only three weighings to determine the counterfeit and its type (lighter or heavier).
\end{problem}

We first present a hand-tailored solution for twelve coins.

Let $M$ be the set of coins, $|M|=12$. We have weighing function
\[
 	w : M \rightarrow \{a, b\},\  a \neq b,\ a, b \in \mathbb{R}^+.
\]

\noindent We have $| \{ c \in M : w(c) = a \} | = 11$ and  $| \{ c \in M : w(c) = b \} | = 1$. We are asked to find
$c_f \in M$ with $w(c_f) = b$ in three weighings.\bigskip

\noindent For a subset $S \subseteq M$ we define
\[
	w(S) = \sum_{c \in S} w(c).
\]

\begin{marginfigure}
\includegraphics[scale=0.50]{coins.png}
\end{marginfigure}
\marginnote{There are many variations of coin weighing problems. Some require identifying the type of the counterfeit (lighter or heavier), some don't. Some have more than one scale, some have more than one counterfeit, some don't state the existence of the counterfeit, some allow using scale weights or a known genuine coin. The Wikipedia page \url{https://en.wikipedia.org/wiki/Balance_puzzle} has a good overview. In this section we always want to find the counterfeit coin (we know there is exactly one counterfeit of unknown type) and its type and our scale is a balance scale with coins on both sides.}

\noindent Let's partition M into 3 subsets $S_0, S_1, S_2$
\[
	\begin{array}{l}
		S_0 \cup S_1 \cup S_2 = M \\
		\forall \ 0 \leq i < 3 : | S_i | = 4 \\
		\forall \ 0 \leq i <  j < 3 : S_i \cap S_j = \emptyset
	\end{array}
\]

\noindent At this point we consume the first weighing:
\begin{center} \textbf{\textit{1st weighing}}: compare $w(S_1)$ with $w(S_2)$ \end{center}

\subsection{Case $w(S_1) = w(S_2)$}

In this case $c_f \in S_0$. We partition $S_0$ into $S_0 = S_0^1 \cup S_0^3$ with $| S_0^1 | = 1$ and
$| S_0^3 | = 3$. We also consider $S_1^3$, a subset of $S_1$ with $| S_1^3 | = 3$. We consume the second weighing:
\begin{center} \textbf{\textit{2nd weighing}}: compare $w(S_0^3)$ with $w(S_1^3)$ \end{center}

\begin{description}
	\item [ subcase 1: $w(S_0^3) = w(S_1^3)$. ] In this subcase $c_f \in S_0^1$ and we're done after just two weighings.
	\item [ subcase 2: $w(S_0^3) > w(S_1^3)$. ] In this subcase $S_0^3$ has the counterfeit coin and $b > a$. We consume the third weighing: Let $S_0^3 = \{ c_1, c_2, c_3 \}$. We weigh $c_1$ against $c_2$.
\begin{center} \textbf{\textit{3rd weighing}}: compare $w(c_1)$ with $w(c_2)$ \end{center}
If $w(c_1) = w(c_2)$ then $c_f = c_3$, if $w(c_1) > w(c_2)$ then $c_f = c_1$.
	\item	[ case 3: $w(S_0^3) < w(S_1^3)$. ]  In this case $S_0^3$ has the counterfeit coin and $b < a$. Analog to previous case (replace heavy with light).
\end{description}

\subsection{Case $w(S_1) > w(S_2)$}

In this case the counterfeit coin is either in $S_1$ or in $S_2$.

\begin{marginfigure}[0.0in]
\includegraphics[scale=0.5]{freeman.jpg}
\end{marginfigure}
\marginnote{Freeman J. Dyson. \url{https://en.wikipedia.org/wiki/Freeman_Dyson}}

\noindent We consider 4 subsets:

\begin{description}
	\item $S_0^3 \subset S_0, |S_0^3| = 3$,
	\item $A$ with three coins from $S_1$,
	\item $B$ with one coin from $S_2$,
	\item $C$ with remaining coin from $S_1$: $C = S_1 \setminus A$.
\end{description}

\noindent We consume the second weighing:
\begin{center} \textbf{\textit{2nd weighing}}: compare $w(S_0^3 \cup C)$ with $w(A \cup B)$ \end{center}

\begin{description}
	\item [ subcase 1: $w(S_0^3 \cup C) = w(A \cup B)$. ] In this subcase $c_f \in S_2 \setminus B$ and because $w(S_1) > w(S_2)$  we know that $b < a$. Let $\{c_1, c_2, c_3\} = S_2 \setminus B$ and we consume third weighing:
\begin{center} \textbf{\textit{3rd weighing}}: compare $w(c_1)$ with $w(c_2)$ \end{center}
If $w(c_1) = w(c_2)$ then $c_f = c_3$, if $w(c_1) > w(c_2)$ then $c_f = c_2$.
	\item [ subcase 2: $w(S_0^3 \cup C) < w(A \cup B)$. ] Assume $c_f \in C \subset S_1$. That would mean that $b > a$ because $w(S_1) > w(S_2)$ but that contradicts with \newline
	$w(S_0^3 \cup C) = 3 a + b < w(A \cup B) = 4 a$. Assume $c_f \in B \subset S_2$. That would mean that $b < a$ because $w(S_1) > w(S_2)$ but that contradicts also with
	$w(S_0^3 \cup C) = 4 a < w(A \cup B) = 3 a + b$. The only possibility remaining is $c_f \in A$. We
	use the \textbf{\textit{third weighing}} analog to the previous case to find the counterfeit coin in a three-coin set using the fact that $b > a$.
	\item	[ subcase 3: $w(S_0^3 \cup C) > w(A \cup B)$. ]  In this case the counterfeit coin can be either in $B$ or in $C$. It cannot be in $A$ according to a reasoning analog to previous case that leads to a contradiction. Both $B$ and $C$ only have one coin each so compare the coin in $B$ with any good coin to find the counterfeit coin in this \textbf{\textit{third weighing}}.
\end{description}

\noindent This covers all the cases and we're done.

The solution brings up questions: how would it work for 13 coins and in general, how many weighings would be necessary for $N$ coins. To answer these questions we present an elegant general solution\footnote{\bibentry{freemanDysonCoins} \\ We follow an exposition of this solution by G. Shestopal.} invented by Freeman J. Dyson.

In the hand-tailored solution for twelve coins we kept seeing a partition of coin sets into 3 subsets, two equally sized subsets that were weighed against each other and one subset that didn't participate in the current weighing. Depending on the result of the weighing and prior information from previous weighings we could narrow the search. Each weighing seems to contribute roughly three pieces of "information". A sequence of $n$ weighings would generate $3^n$ amount of information that should somehow map to finding the counterfeit amongst $N$ coins. This is not strictly accurate\footnote{The hand-tailored solution is an \textbf{adaptive} solution: later weighings are set up according to the result of earlier weighings. Freeman J. Dyson's general solution is a \textbf{non-adaptive} solution if the number of coins is divisible by three: all the weighings are pre-determined regardless of their outcome.} because information deduced from weighings uses prior information from weighings before and the narrowing doesn't completely discard sets of coins but instead uses them as scale weights with known type (ie none are counterfeits). But this $3^n$ observation does suggest $3^n \approx N$ and point to the objective of finding a bijection between ternary codewords of length $n$ and a set of size $N$.

Lets first consider $N$ coins with $N$ of the form $N = \frac{1}{2} (3^n - 3)$ for some $n \geq 2$. We will show how to find the counterfeit and its type in $n$ weighings\footnote{Notice that $N=12$ fits this form: $12 = \frac{1}{2} (3^3 - 3)$, so $n=3$ weighings.}.

The set of codewords of length $n$ from alphabet $\{0, 1, 2\}$ has size $3^n$. We discard the three codewords with all digits equal: $0\ldots0$, $1\ldots1$ and $2\ldots2$. The set $W$ of the remaining codewords has size $3^n - 3$. We split\footnote{The expression $3^n - 3$ is the subtraction of two odd numbers, so it is even.} $W$ into $\frac{3^n - 3}{2}$ pairs of complements:

\begin{defn}
Two codewords $a_1 a_2 \ldots a_n \in W$ and $b_1 b_2 \ldots b_n \in W$ are \textbf{complements} of each other if
$$
\forall i: 1 \leq i \leq n: a_i + b_i = 2
$$
We use the notation $a_1^c a_2^c \ldots a_n^c$ for the complement of $a_1 a_2 \ldots a_n$.
\end{defn}

\begin{defn}
The function $\delta: W \mapsto \{0, 1, 2\}^2$ finds the first two digits of a codeword that differ\footnote{The function $\delta$ is well-defined because $0\ldots0 \notin W$, $1\ldots1 \notin W$ and $2\ldots2 \notin W$.}
$$
\delta (a_1 a_2 \ldots a_n) = a_i a_{i+1} \text{ such that } \forall 1 \leq j < i: a_j = a_i \text{ and } a_i \neq a_{i+1}
$$
\end{defn}

\begin{defn}
Codeword $w \in W$ is called a \textbf{left} codeword if $\delta(w) \in \{10, 21, 02\}$ otherwise it is called a \textbf{right} codeword.
\end{defn}

It is easy to see that the set of left codewords and the set of right codewords do not overlap and that if a codeword is a left codeword its complement is a right codeword\footnote{For example let's prove that if $a_1 a_2 \ldots a_n$ is a right codeword then $a_1^c a_2^c \ldots a_n^c$ is a left codeword. If $a_j=a_{j+1}$ then $a_j^c=a_{j+1}^c$ and likewise if $a_j \neq a_{j+1}$ then $a_j^c \neq a_{j+1}^c$. So the index of the first two digits that differ is the same for a codeword and its complement. The complements of $\{01, 12, 20\}$ are exactly $\{21, 10, 02\}$ so the complement of a right codeword is a left codeword with differing digits at the same index as the right codeword.}. A good mnemonic of left and right is this: $0 \rightarrow 1 \rightarrow 2$ moves digits to the right so $\{01, 12, 20\}$ corresponds to the right codewords and the right direction. Conversely $0 \leftarrow 1 \leftarrow 2$ moves digits to the left so $\{10, 21, 02\}$ corresponds to the left codewords and the left direction.

To summarize we now have $\frac{3^n - 3}{2}$ pairs of codewords from $W$ such that each pair has a left codeword and a right codeword that are complements of each other. Let $P$ be the set of these pairs. We have $N=\frac{3^n - 3}{2}$ coins. Let $C$ be the set of coins. We pick an arbitrary bijection $\mu: C \mapsto P$ that assigns a pair to a coin, marking it with a left and a right codeword\footnote{For example when $N=12$ we could pick $\mu$ like this:
\begin{center}
\begin{tabular}{ |c|c|c| }
 \hline
 coin & left codeword & right codeword \\
 \hline
 $c_1$ & $211$ & $011$ \\
 $c_2$ & $100$ & $122$ \\
 $c_3$ & $022$ & $200$ \\
 $c_4$ & $212$ & $010$ \\
 $c_5$ & $101$ & $121$ \\
 $c_6$ & $020$ & $202$ \\
 $c_7$ & $210$ & $012$ \\
 $c_8$ & $102$ & $120$ \\
 $c_9$ & $021$ & $201$ \\
 $c_{10}$ & $221$ & $001$ \\
 $c_{11}$ & $110$ & $112$ \\
 $c_{12}$ & $002$ & $220$ \\
 \hline
\end{tabular}
\end{center}
$C_3(2)$ means the subset of coins for which the third digit in the right codeword is $2$. In this example $C_3(2) = \{c_2, c_6, c_7, c_{11}\}$.}.

 We adopt the notation $\mu(c).right$ to denote the right codeword assigned to coin $c \in C$ and $\mu(c).left$ the left codeword. Also $\mu(c).right(i)$ denotes the i-th digit of the right codeword, so if $\mu(c).right = a_1 a_2 \ldots a_n$ then $\mu(c).right(i) = a_i$. Analog $\mu(c).left(i)$ for the i-th digit of the left codeword assigned to coin $c$.

We define $\forall i: 1 \leq i \leq n$ and $\forall d: 0 \leq d \leq 2$ the subsets $C_i(d) \subset C$  with
$$
C_i(d) = \{ c \in C: \mu(c).right(i) = d\}
$$

It is easy to see that $C = C_i(0) \cup C_i(1) \cup C_i(2)$ for each $1 \leq i \leq n$ and that $C_i(0) \cap C_i(1) = C_i(0) \cap C_i(2) = C_i(1) \cap C_i(2) =\emptyset$, so $C_i(0)$, $C_i(1)$ and $C_i(2)$ partition $C$.

Now we execute $n$ weighings. For the i-th weighing we place the coins from $C_i(0)$ on the left pan of the scale and the coins from $C_i(2)$ on the right pan of the scale. We capture the $n$ weighings in a codeword $x_1 x_2 \ldots x_n \in W$: if in the i-th weighing the left pan sinks, then $x_i=0$, if the weighing is balanced then $x_i=1$ and if the right pan sinks then $x_i=2$.

We claim that the weighing codeword $x_1 x_2 \ldots x_n$ will be either the left or the right marker codeword of the counterfeit depending whether the counterfeit is lighter or heavier and prove the following theorem:

\begin{thm}\label{coin_encoding}
Let $x_1 x_2 \ldots x_n$ be the result of the $n$ weighings and let $c_f \in C$ be the counterfeit coin.
Then $\mu(c_f).right = x_1 x_2 \ldots x_n$ if $c_f$ is heavier and $\mu(c_f).left = x_1 x_2 \ldots x_n$ if $c_f$ is lighter.
\end{thm}

\begin{proof}
Assume the counterfeit $c_f$ is lighter than the other coins (we deal with the case of $c_f$ heavier afterwards).

There are three cases for the i-th weighing:

\textbf{Case 1.(lighter)} The scale is balanced, so $x_i=1$. This means that $c_f$ does not participate in the i-th weighing\footnote{Otherwise the scale wouldn't be balanced.}: $c_f \notin C_i(0)$ and $c_f \notin C_i(2)$. Since $C_i(0)$, $C_i(1)$ and $C_i(2)$ partition $C$, we have $c_f \in C_i(1)$. By definition this means that $\mu(c_f).right(i) = x_i = 1$ and since right and left are complements it also means that $\mu(c_f).left(i) = x_i= 1$.

\textbf{Case 2.(lighter)} The left pan sinks, so $x_i=0$. Here $c_f$ participates and $c_f \in C_i(2)$ because it is lighter so on the right pan. By definition this means that $\mu(c_f).right(i) = 2$ and since $\mu(c_f).left(i)$ is the complement it also means $\mu(c_f).left(i) = x_i = 0$.

\textbf{Case 3.(lighter)} The pan sinks, so $x_i=2$. In this case $c_f$ also participates and $c_f \in C_i(0)$ because it is lighter so on the left pan. This means $\mu(c_f).right(i) = 0$ and by complement $\mu(c_f).left(i) = x_i= 2$.

For every $i$ we have seen that $\mu(c_f).left(i) = x_i$, so $\mu(c_f).left = x_1 x_2 \ldots x_n$.

Now assume $c_f$ is heavier. We proceed in analog fashion. There are three cases for the i-th weighing:

\textbf{Case 1.(heavier)} The scale is balanced, so $x_i=1$. This means that $c_f$ does not participate in the i-th weighing and we have $c_f \in C_i(1)$ and $\mu(c_f).right(i) = x_i = 1$.

\textbf{Case 2.(heavier)} The left pan sinks, so $x_i=0$. Here $c_f$ participates and $c_f \in C_i(0)$ because it is heavier so on the left pan. Then $\mu(c_f).right(i) = x_i = 0$.

\textbf{Case 3.(heavier)} The right pan sinks, so $x_i=2$. In this case $c_f$ also participates and $c_f \in C_i(2)$ because it is heavier so on the right pan. So $\mu(c_f).right(i) = x_i= 2$.

We see that for every $i$ we have $\mu(c_f).right(i) = x_i$, so $\mu(c_f).right = x_1 x_2 \ldots x_n$.
\end{proof}

Theorem \ref{coin_encoding} shows that the $n$ weighings detect the counterfeit coin and its type if there are $N = \frac{3^n - 3}{2}$ coins.

If $N < \frac{3^n - 3}{2}$ we have to be more careful how we mark coins with codewords\footnote{For example assume there are only ten coins and we arbitrarily assign the following codeword pairs:
\begin{center}
\begin{tabular}{ |c|c|c| }
 \hline
 coin & left codeword & right codeword \\
 \hline
 $c_1$ & $211$ & $011$ \\
 $c_2$ & $100$ & $122$ \\
 $c_3$ & $022$ & $200$ \\
 $c_4$ & $212$ & $010$ \\
 $c_5$ & $101$ & $121$ \\
 $c_6$ & $020$ & $202$ \\
 $c_7$ & $210$ & $012$ \\
 $c_8$ & $102$ & $120$ \\
 $c_9$ & $021$ & $201$ \\
 $c_{10}$ & $221$ & $001$ \\
 \hline
\end{tabular}
\end{center}
Then $C_2(2) = \{c_2, c_5, c_8\}$ and $C_2(0) = \{c_3, c_6, c_9, c_{10}\}$. This is a problem because the two sets that will be put on the scale in the two pans in the second weighing have different number of coins. We cannot deduce any information from this weighing anymore.
}.

Let $\pi: W \mapsto W$ be the function on the set of codewords $W$ with $\pi(a_1 a_2 \ldots a_n) = b_1 b_2 \ldots b_n$ such that $\forall i: 1 \leq i \leq n: b_i \equiv (a_i + 1) \mod 2$.

\begin{lem}\label{coin_pi}
The function $\pi$ preserves the rightiness of codewords: if $w \in W$ is a right codeword, then $\pi(w)$ is also a right codeword.
\end{lem}

\begin{proof}
Let $a_1 a_2 \ldots a_n$ be a right codeword. By definition it means that $\delta(a_1 a_2 \ldots a_n)=a_i a_{i+1}$ with $\forall 1 \leq j < i: a_j = a_i$ and $a_i a_{i+1} \in \{01, 12, 20\}$. Let $\pi(a_1 a_2 \ldots a_n) = b_1 b_2 \ldots b_n$. This means that $\forall 1 \leq j < i: b_j \equiv a_j + 1 \equiv a_i + 1 \equiv b_i \mod 2$. So $\delta(b_1 b_2 \ldots b_n) = b_i b_{i+1}$. If $a_i a_{i+1}=01$ then $b_i b_{i+1}=12$, if $a_i a_{i+1}=12$ then $b_i b_{i+1}=20$ and if $a_i a_{i+1}=20$ then $b_i b_{i+1}=01$. In all three cases $b_1 b_2 \ldots b_n$ is also a right codeword.
\end{proof}

From the definition of $\pi$ it is clear that $\pi^3(w) = w$. From this and lemma \ref{coin_pi} it follows that $\pi$ partitions the set of right codewords into subsets of size three: $\{w, \pi(w), \pi^2(w)\}$.

We now partition $W$ into subsets of size six:
$$
\{w, w^c, \pi(w), (\pi(w))^c, \pi^2(w), (\pi^2(w))^c\}
$$
grouping $\pi$-generated right codewords and their left complements. The group with right codewords $00\ldots01$, $11\ldots12$ and $22\ldots20$ we set aside and call the left-over group.

For the bijection $\mu: C \mapsto W$ we group coins in groups of three. For each group we pick a codeword group of six other than the left-over group and assign left and right complementing codewords to each coin in the group. If there are one or two coins left over from the grouping into threes, then we use the left-over codeword group to assign left and right codewords to those coins. If only one coin is left over we assign it the right codeword $11\ldots12$ and if two are left over we assign the right codewords $00\ldots01$ and $22\ldots20$.

With $\mu$ defined this way if $N \equiv 1 \mod 3$ then the left-over coin has right codeword $11\ldots12$ and will not participate in the first $n-1$ weighings. If $N \equiv 2 \mod 3$ the two left-over coins with right codewords $00\ldots01$ and $22\ldots20$ will both participate in every of the first $n-1$ weighings. This ensures that the left-over coins don't disrupt the first $n-1$ weighings. For a coin that is not in the left-over group it is easy to see that if it participates in a weighing then there is another coin in the same group (apply $\pi$ twice to get to its right codeword) that also participates on the opposite pan. Overall we have satisfied the requirement $|C_i(0)| = |C_i(2)|$ for all $1 \leq i < n$. The first $n-1$ weighings can proceed as before.

For the last weighing we have the following cases (here the solution turns adaptive, ie the setup for the last weighing depends on the outcome of the previous weighings):

The first two cases are for $N \equiv 1 \mod 3$, so one left-over coin $c_l$ with right codeword $\mu(c_l).right=11\ldots12$.

\textbf{Case 1.} The first $n-1$ weighings yielded $x_1 x_2 \ldots x_{n-1} = 11\ldots1$ (the scale was balanced in all $n-1$ weighings). In this case we know the left-over coin is the counterfeit $c_l=c_f$ and we can just put it on one pan on the scale and any other coin on the other to determine its type.

\textbf{Case 2.} The first $n-1$ weighings yielded $x_1 x_2 \ldots x_{n-1} \neq 11\ldots1$. In this case we know the left-over coin is not the counterfeit. We can just leave it out and put $C_n(0)$ on the left pan and $C_n(2) \setminus \{c_l\}$ on the right pan of the balance scale. The resulting complete weighing codeword will point to the counterfeit and its type.

The next cases are for for $N \equiv 2 \mod 3$, with two left-over coins $c_{l0}$ and $c_{l2}$ with right codewords $\mu(c_{l0}).right=00\ldots01$ and $\mu(c_{l2}).right=22\ldots20$.

\textbf{Case 3.} The first $n-1$ weighings yielded $x_1 x_2 \ldots x_{n-1} = 22\ldots2$. Both $c_{l0}$ and $c_{l2}$ participated in all $n-1$ weighings (according to their right codewords that start with $00\ldots0$ and $22\ldots2$ respectively). It means that either $c_{l0}$ or $c_{l2}$ is the counterfeit.  In the last weighing we pit $c_{l0}$ against any coin that is not $c_{l2}$. If the scale is balanced then $c_{l2}$ is the counterfeit and it is heavier. If the scale tilts one way or the other it shows $c_{l0}$ as a counterfeit and its type.

\textbf{Case 4.} The first $n-1$ weighings yielded $x_1 x_2 \ldots x_{n-1} = 00\ldots0$. Again both $c_{l0}$ and $c_{l2}$ participated in all $n-1$ weighings and again it means that either $c_{l0}$ or $c_{l2}$ is the counterfeit.  In the last weighing we pit $c_{l2}$ against any coin that is not $c_{l0}$. If the scale is balanced then $c_{l0}$ is the counterfeit and it is heavier. If the scale tilts one way or the other it shows $c_{l2}$ as a counterfeit and its type.

\textbf{Case 5.} The first $n-1$ weighings yielded $x_1 x_2 \ldots x_{n-1} \neq 00\ldots0$ and $x_1 x_2 \ldots x_{n-1} \neq 22\ldots2$. In this case both $c_{l0}$ and $c_{l2}$ cannot be counterfeits. In the last weighing we can just do $C_n(0)$ on the left pan and $C_n(2)$ on the right. The resulting complete weighing codeword will point to the counterfeit and its type.

This covers all the cases and shows that we can find the counterfeit coin and its type in $n$ weighings if the number of coins $N \leq \frac{3^n - 3}{2}$.

Is this optimal or does a strategy exist that needs less than $n$ weighings?

To answer this question we want to find a lower bound for the number of weighings given $N$ coins.

\begin{thm}\label{coin_lower_bound}
Given are $N$ coins of which one is a counterfeit. If the counterfeit coin and its type can be found in $n$ weighings using a non-adaptive strategy, then $2 N \leq 3^n - 3$.
\end{thm}

\begin{proof}

There are $n$ weighings producing a weighing codeword $x_1 x_2 \ldots x_n$ with $x_i=0$ if left pan sinks in i-th weighing, $x_i=1$ if i-th weighing is balanced and $x_i=2$ if right pan sinks. We have $3^n$ possible codewords from $n$ weighings.

Assume we have a non-adaptive strategy that finds the counterfeit coin and its type in $n$ weighings. Since it is non-adaptive the information of which coin participates in which weighing on which pan is already pre-determined. Let's capture this information in a matrix $p \in \{0, 1, 2\}^{n \times N}$ which we call the \textbf{participation matrix}:

$$
\begin{array}{l}
\forall 1 \leq i \leq n, 1 \leq j \leq N: \\
p_{ij} =
\begin{cases}
0 \text{,  \quad  coin $j$ on left pan in i-th weighing} \\
1 \text{,  \quad  coin $j$ not in i-th weighing} \\
2 \text{,  \quad  coin $j$ on right pan in i-th weighing}
\end{cases}
\end{array}
$$

The number of potential answers to the question of which coin is the counterfeit and what is its type is $2 N$ ($N$ coins and two possibilities for each coin - lighter or heavier). For notational convenience we define the index sequence $N_{\pm} = \{1, -1, 2, -2, \ldots , N, -N\}$. For all $1 \leq j \leq N$ index $j$ means coin $j$ is the counterfeit and it is heavier, index $-j$ means coin $j$ is the counterfeit and it is lighter.

Given the participation matrix we define a matrix $a \in \{0, 1, 2\}^{n \times N_{\pm}}$. Cell $a_{ij}$ tells us what result of weighing $i$ keeps the answer that coin $j$ is the heavier counterfeit as still a possibility. Cell $a_{i(-j)}$ tells us what result of weighing $i$ keeps the answer that coin $j$ is the lighter counterfeit as still a possibility. For example if $p_{ij} = 0$ then coin $j$ is on the left pan in the i-th weighing and for coin $j$ to be the counterfeit and heavier in the i-th weighing the left pan needs to sink, the corresponding weighing codeword component needs to be $x_i=0$ and $a_{ij} = 0$.

In other words matrix $a$ shows for each potential answer what needs to happen in each weighing so that the answer becomes the actual answer:

$$
\begin{array}{l}
\forall 1 \leq i \leq n, 1 \leq j \leq N: \\
a_{ij} =
\begin{cases}
0  \text{,\quad if } p_{ij} = 0\\
1  \text{,\quad if } p_{ij} = 1\\
2  \text{,\quad if } p_{ij} = 2
\end{cases}
a_{i(-j)} =
\begin{cases}
0  \text{,\quad if } p_{ij} = 2\\
1  \text{,\quad if } p_{ij} = 1\\
2  \text{,\quad if } p_{ij} = 0
\end{cases}
\end{array}
$$

The column $j$ in matrix $a$ is the weighing codeword required for making coin $j$ and its type (from the sign of $j$) the actual answer.

For the strategy (represented by the participation matrix $p$) to work it needs to not rely on "luck", i.e.\ a given weighing codeword happens to be a column in matrix $a$. All the weighing codewords that can occur need to be columns in $a$ exactly once.

Since the strategy is successful we know that each coin participates in at least one weighing\footnote{Otherwise there would be no way to find its type even if we find the counterfeit.}, so the column vector $(1, 1, \ldots, 1)^T$ does not appear in $p$. This means that weighing codeword $x1 x2 \ldots x_n=11\ldots1$ cannot occur and we have $3^n - 1$ remaining weighing codewords that can happen.

If $2 N > 3^n - 1$ then it must be that a weighing codeword will appear more than once in $a$, so more than one potential answer could be the actual answer and we wouldn't know which one. So $2 N \leq 3^n - 1$.

We observe that in each row $i$ of $a$ if $a_{ij}=0$ then $a_{i(-j)}=2$ and vice versa. That means that there are $k_i$ zeros and $k_i$ twos in row $i$, so $2 N - 2 k_i$ ones. Also because an even number of coins participates in each weighing we have that $k_i$ is even. So in each row we have an even number of zeros, ones and twos. There are $3^{n - 1}$ possible weighing codewords that start with a one. That means at most $3^{n-1}$ columns in $a$ can have a one in the first position. Since the number of ones is even and $3^{n-1}$ is odd we can have at most $3^{n-1}-1$ ones in the first row. Similarly there are $3^{n - 1}$ possible weighing codewords that start with a zero, so $k_1 \leq 3^{n-1} - 1$. We then have:

$$
2N = 2N - 2 k_1 + 2 k_1 \leq 3^{n-1} - 1 + 2 (3^{n-1} - 1) = 3^n - 3
$$
\end{proof}

\todo{Explain connection to Hamming codes for number of columns smaller than $3^n - 3$}

\bibliographystyle{plainnat}
\bibliography{../common/math}

\end{document}


% !TEX root=/home/tavant/these/manuscript/src/manuscript.tex

\section{Conclusion }
\label{sec-ch3conclusion}

Using the kinetic informations from the \ac{PIC} simulations, we have seen that the electrons are not Maxwellian, in contrast with the hypothesis of the usual models for plasma wall interactions.
The electron distribution function is affected by two phenomena\string:
\begin{itemize}
  \item the absorption of high energy electrons at the wall
  \item the electron-neutral scattering
\end{itemize}
\vspace{1em}
The absorption depletes rapidly the high energy tail of the EEPF for energies higher than the local plasma potential relative to the wall.
However, the low energy population is not affected by the wall.

The collisions affect the electrons more slowly, by replenishing the high energy tail by scattering.
Indeed, in the directions parallel to the wall, the high energy tail is not depleted.
However, for large energies ($\ek > 10\,\volt$), the electron-neutral scattering angle is small \citep{vahedi1995}, hence the time scale over which the collisions impact the EEPF is much longer than the typical time between two collisions.

The electron trajectory in the discharge chamber is hence mostly collisionless.
We have successfully confirmed this by confronting the EEDF measurements to the 1D stationary Vlasov equation.
Following the work of \citet{zhang2016} on the collisionless evolution of non-Maxwellian electron through a potential drop, we have found that a polytropic closure for the electron describes very accurately the electron temperature evolution\string:
\begin{equation*} \label{eq-polyp2}
  \Te n_e^{1-\gamma} = cst, \text{ with $\gamma$ the polytropic index}
\end{equation*}

The polytropic state law for the electrons, when used in fluid model, allows to obtain the same densities and plasma potential as in the \ac{PIC} simulation.
This paves the way for a modified sheath model to compare to the \ac{2D} \ac{PIC} simulation of the \ac{HET} of \cref{ch-2}.

We have also seen in \cref{sec-realistic_1D} that the polytropic state law also stands when a self-consistent heating mechanism is used, even if the agreement is not as good as in the other case.

In \cref{eq-polyp2}, the value of the polytropic index $\gamma$ depends on the shape of the \ac{EVDF}.
We showed in \cref{sec-MCM} that a Monte Carlo computation can be used in order to obtain the \ac{EVDF} for a given plasma potential profile and neutral pressure.
As the Monte Carlo approach does not need the Poisson equation to be solved, it produces the \ac{EVDF} much faster than a \ac{PIC} simulation.
Hence, we could couple the Monte Carlo calculation with a fluid model to accurately take into account the real shape of the \ac{EVDF} in the closure of terms in fluid models.


%
% API Documentation for Peach - Computational Intelligence for Python
% Module peach.fuzzy.defuzzy
%
% Generated by epydoc 3.0beta1
% [Mon Dec 21 08:51:35 2009]
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                          Module Description                           %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}|(}
\section{Module peach.fuzzy.defuzzy}

    \label{peach:fuzzy:defuzzy}

This package implements defuzzification methods for use with fuzzy controllers.

Defuzzification methods take a set of numerical values, their corresponding
fuzzy membership values and calculate a defuzzified value for them. They're
implemented as functions, not as classes. So, to implement your own, use the
directions below.

These methods are implemented as functions with the signature \texttt{(mf, y)}, where
\texttt{mf} is the fuzzy set, and \texttt{y} is an array of values. That is, \texttt{mf} is a
fuzzy set containing the membership values of each one in the \texttt{y} array, in
the respective order. Both arrays should have the same dimensions, or else the
methods won't work.

See the example:
\begin{quote}{\ttfamily \raggedright \noindent
>{}>{}>~import~numpy~\\
>{}>{}>~from~peach~import~*~\\
>{}>{}>~y~=~numpy.linspace(0.,~5.,~100)~\\
>{}>{}>~m{\_}y~=~Triangle(1.,~2.,~3.)~\\
>{}>{}>~Centroid(m{\_}y(y),~y)~\\
2.0001030715316435
}\end{quote}

The methods defined here are the most commonly used.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                               Functions                               %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \subsection{Functions}

    \label{peach:fuzzy:defuzzy:Centroid}
    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}!peach.fuzzy.defuzzy.Centroid \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{Centroid}(\textit{mf}, \textit{y})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Center of gravity method.

The center of gravity is calculate using the standard formula found in any
calculus book. The integrals are calculated using the trapezoid method.
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xx}

          \item[mf]

Fuzzy set containing the membership values of the elements in the
vector given in sequence
          \item[y]

Array of domain values of the defuzzified variable.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      \textbf{Return Value}
      \begin{quote}

The center of gravity of the fuzzy set.
      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

    \label{peach:fuzzy:defuzzy:Bissector}
    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}!peach.fuzzy.defuzzy.Bissector \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{Bissector}(\textit{mf}, \textit{y})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Bissection method

The bissection method finds a coordinate \texttt{y} in domain that divides the
fuzzy set in two subsets with the same area. Integrals are calculated using
the trapezoid method. This method only works if the values in \texttt{y} are
equally spaced, otherwise, the method will fail.
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xx}

          \item[mf]

Fuzzy set containing the membership values of the elements in the
vector given in sequence
          \item[y]

Array of domain values of the defuzzified variable.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      \textbf{Return Value}
      \begin{quote}

Defuzzified value by the bissection method.
      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

    \label{peach:fuzzy:defuzzy:SmallestOfMaxima}
    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}!peach.fuzzy.defuzzy.SmallestOfMaxima \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{SmallestOfMaxima}(\textit{mf}, \textit{y})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Smallest of maxima method.

This method finds all the points in the domain which have maximum membership
value in the fuzzy set, and returns the smallest of them.
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xx}

          \item[mf]

Fuzzy set containing the membership values of the elements in the
vector given in sequence
          \item[y]

Array of domain values of the defuzzified variable.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      \textbf{Return Value}
      \begin{quote}

Defuzzified value by the smallest of maxima method.
      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

    \label{peach:fuzzy:defuzzy:LargestOfMaxima}
    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}!peach.fuzzy.defuzzy.LargestOfMaxima \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{LargestOfMaxima}(\textit{mf}, \textit{y})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Largest of maxima method.

This method finds all the points in the domain which have maximum membership
value in the fuzzy set, and returns the largest of them.
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xx}

          \item[mf]

Fuzzy set containing the membership values of the elements in the
vector given in sequence
          \item[y]

Array of domain values of the defuzzified variable.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      \textbf{Return Value}
      \begin{quote}

Defuzzified value by the largest of maxima method.
      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

    \label{peach:fuzzy:defuzzy:MeanOfMaxima}
    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}!peach.fuzzy.defuzzy.MeanOfMaxima \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{MeanOfMaxima}(\textit{mf}, \textit{y})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Mean of maxima method.

This method finds the smallest and largest of maxima, and returns their
average.
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xx}

          \item[mf]

Fuzzy set containing the membership values of the elements in the
vector given in sequence
          \item[y]

Array of domain values of the defuzzified variable.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      \textbf{Return Value}
      \begin{quote}

Defuzzified value by the  of maxima method.
      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                               Variables                               %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \subsection{Variables}

\begin{longtable}{|p{.30\textwidth}|p{.62\textwidth}|l}
\cline{1-2}
\cline{1-2} \centering \textbf{Name} & \centering \textbf{Description}& \\
\cline{1-2}
\endhead\cline{1-2}\multicolumn{3}{r}{\small\textit{continued on next page}}\\\endfoot\cline{1-2}
\endlastfoot\raggedright \_\-\_\-d\-o\-c\-\_\-\_\- & \raggedright \textbf{Value:}
{\tt \texttt{...}}&\\
\cline{1-2}
\end{longtable}

    \index{peach \textit{(package)}!peach.fuzzy \textit{(package)}!peach.fuzzy.defuzzy \textit{(module)}|)}


\documentclass[12pt, letterpaper]{report}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{subfig}
\graphicspath{ {./img/} }
\setlength\parindent{0pt}
\renewcommand\thesection{\Roman{section}.}
\renewcommand{\thesubsection}{\alph{subsection}.}

\title{CS1675 - Assignment 6}
\author{Zachary M. Mattis}

\begin{document}

\maketitle

\section{Problem 1 - Support Vector Machines}

% A
\subsection{Weight / Bias}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|r| }
		\hline
		\textbf{Attribute} & \textbf{Weight} \\
		\hline
		1 & 0.0445 \\
		\hline
		2 & 0.0116 \\
		\hline
		3 & -0.0050 \\
		\hline
		4 & 0.0003 \\
		\hline
		5 & -0.0002 \\
		\hline
		6 & 0.0264 \\
		\hline
		7 & 0.3524 \\
		\hline
		8 & 0.0063 \\
		\hline
	\end{tabular}
	\caption{Training Weights}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|r| }
		\hline
		\textbf{Bias} & -2.7182 \\
		\hline
	\end{tabular}
	\caption{Training Bias}
\end{table}

% B
\subsection{SVML}

\begin{verbatim}
apply_svml.m
\end{verbatim}

\[ w^{T}x + b \geq 0 \]

% C
\subsection{Misclassification, Confusion, Sensitivity, Specificity}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|r|r|r| }
		\hline
		\textbf{Dataset} & \textbf{Misclassification Error} & \textbf{Sensitivity} & \textbf{Specificity}\\
		\hline
		Training & 0.2319 & 0.5750 & 0.8820 \\
		\hline
		Testing & 0.1965 & 0.6176 & 0.8820 \\
		\hline
	\end{tabular}
	\caption{Misclassification Error, Sensitivity, Specificity}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|r|r| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 115 & 40 \\
		\hline
		\textbf{0} & 85 & 299 \\
		\hline
	\end{tabular}
	\caption{Training Confusion Matrix}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|r|r| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 42 & 19 \\
		\hline
		\textbf{0} & 26 & 142 \\
		\hline
	\end{tabular}
	\caption{Test Confusion Matrix}
\end{table}

% D
\subsection{LR. vs. NB vs. SVML}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|r|r|r| }
		\hline
		\textbf{Model} & \textbf{Misclassification} & \textbf{Sensitivity} & \textbf{Specificity} \\
		\hline
		LR & 0.2988 & 0.5900 & 0.8761 \\
		\hline
		NB & 0.4644 & 0.8400 & 0.5398 \\
		\hline
		SVML & 0.2319 & 0.5750 & 0.8820 \\
		\hline
	\end{tabular}
	\caption{Training Model Comparison}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|r|r|r| }
		\hline
		\textbf{Model} & \textbf{Misclassification} & \textbf{Sensitivity} & \textbf{Specificity} \\
		\hline
		LR & 0.2722 & 0.6765 & 0.8323 \\
		\hline
		NB & 0.3759 & 0.8676 & 0.5093 \\
		\hline
		SVML & 0.1965 & 0.6176 & 0.8820 \\
		\hline
	\end{tabular}
	\caption{Testing Model Comparison}
\end{table}

In all cases, the SVML model performed better than both Logistic Regression and Naïve Bayes from the assignment 5 implementation, with a lower sensitivity and a higher specificity.

\section{Problem 2 - Deep Learning}

% A
\subsection{Deep Learning Toolbox}

\begin{verbatim}
logistic_NN.m
\end{verbatim}

% B
\subsection{Multi-layer Neural Network}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|r|r|r| }
		\hline
		\textbf{Model} & \textbf{Hidden Layers} & \textbf{Hidden Units} & \textbf{Misclassification} \\
		\hline
		LR & 0 & - & 0.2356 \\
		\hline
		NN & 1 & 2 & 0.2263 \\
		\hline
		NN & 1 & 3 & 0.2171 \\
		\hline
		NN & 1 & 5 & 0.2263 \\
		\hline
		NN & 1 & 10 & 0.2393 \\
		\hline
	\end{tabular}
	\caption{Training NN Comparison}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|r|r|r| }
		\hline
		\textbf{Model} & \textbf{Hidden Layers} & \textbf{Hidden Units} & \textbf{Misclassification} \\
		\hline
		LR & 0 & - & 0.2402 \\
		\hline
		NN & 1 & 2 & 0.2227 \\
		\hline
		NN & 1 & 3 & 0.2271 \\
		\hline
		NN & 1 & 5 & 0.2096 \\
		\hline
		NN & 1 & 10 & 0.1965 \\
		\hline
	\end{tabular}
	\caption{Testing NN Comparison}
\end{table}

Overall, the Neural Network model produced better Misclassification errors than the Logistic Regression model on both training and testing data, proving to be a superior model in this case.

\end{document}


\section{Mathematical Models}
\subsection{Descriptive and Optimization Models}

The descriptive model describes the situation with all it's variables, parameters and constraints, but not with a specific optimization goal. The model describes \textbf{"What if?"}. The optimization model has an additional objective function, which leads then to an optimal solution, it describes \textbf{"What's best?"}.

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/optimization_model.png}
\caption{Optimization Model}
\end{figure}

\textit{Model itself does not calculate solution: Optimization algorithms needed}

\subsubsection{Example Optimization Model}

\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{figures/example_optimization_model.png}
\caption{Example for an optimization model}
\end{figure}

\subsection{Problem Formulation}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/problem_formulation.png}
\caption{Discrete Optimization - Problem formulation}
\end{figure}

\subsection{Definition of Solution Space}
\textbf{Functional Constraints} \\
e.g. $S = \{x \in R \times Z \times \{0,1\}:x_1 + x_2 + x_3 = 1, x \geq 0\}$ \\
$x = (x_1, x_2, x_3)^T$ and $x_1 \in R$, $x_2 \in Z$, $x_3 \in \{0,1\}$

\textbf{Non-Functional Constraints} \\
e.g. $S = \{x \in Z^n$ : x is a permutation of 1 ... n \} $\Rightarrow$ Hard to solve! \\
The non-functional constraint is just describing the problem.

\subsection{Problems and Problem Instances}
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/problemandinstance.png}
\caption{Discrete Optimization - Problem and Problem Instances}
\end{figure}

\subsection{Neighborhoods}
\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/neighborhood.png}
\caption{Discrete Optimization - Neighborhood}
\end{figure}

A neighborhood is a set of subsets of the powerset, where the subsets are neighbors of x. For instance, if the neighborhood of 1 is 1 and 3, we write $N(1) = \{1, 3\}$.

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
If we have a feasible Solution $x \in S$, we search in the neighborhood for a better solution. If we don't find any better solution in this neighborhood, we have a local optimum. \\
Doing so and repeating the search in the neighborhoods is called \textbf{Principle of Local Search Methods or Trajectory - Based Search}.
\end{tcolorbox}

\subsection{Some Topological Notes}
\textbf{Interior Point:} Point within the boundary, NOT on the boundary \\
\textbf{Boundary Point:} Point on the boundary \\
\textbf{Closed:} All boundary points of S are in S \\
\textbf{Open:} All points are interior points

\subsection{Convex Optimization}

The following pictures shows us different possibilities for convex problems.

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/convexOptimization1.png}
\caption{Discrete Optimization - Convex Optimization Problem 1}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/convexOptimization2.png}
\caption{Discrete Optimization - Convex Optimization Problem 2}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{figures/convexOptimization2.png}
\caption{Discrete Optimization - Convex Optimization Problem 2}
\end{figure}

\textbf{Convex optimization problem:} \textbf{min}$\{f(x): x \in S\}$ with $f$ \textbf{convex} and $S$ convex \\
\textbf{(Also) Convex optimization problem:} \textbf{max}$\{f(x): x \in S\}$ with $f$ \textbf{concave} and $S$ convex

\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
\textbf{Theorem}: In convex optimization problem, local optimum is global optimum.\\
\textbf{Theorem}: Consider max$\{f(x) : x \in S\}$ with $f$ convex und $S$ convex, closed, bounded (or min$\{f(x): x \in S \}$ with $f$ concave): Every local optimum is on boundary.
\end{tcolorbox}

\clearpage


\documentclass[t]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, bm, tcolorbox}

\title{Matrix Algebra}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
\maketitle
\end{frame}

\section{Find the sum and difference of two or more matrices.}

\begin{frame}{What is a Matrix?}
\begin{tcolorbox}[colback=red!20!white, colframe=red!30!black, title=\textbf{Matrix}]
A \textbf{matrix} is a rectangular array of numbers.
\end{tcolorbox}
\vspace{10pt} \pause

\begin{tcolorbox}[colback=red!20!white, colframe=red!30!black, title=\textbf{Matrix Dimensions}]
The \textbf{dimensions} of a matrix (\textit{pl: matrices}) are listed as
\begin{center}
Number of rows ($r$) $\times$ Number of columns ($c$)
\end{center}
\end{tcolorbox}
\end{frame}

\begin{frame}{What is a Matrix?}
We use upper-case letters to refer to entire matrices. \newline\\	\pause

Example of some matrices:	\newline\\
\begin{tabular}{c|c|c}
    $3 \times 2$ matrix &   $2 \times 2$ matrix   &   $1 \times 4$ matrix: \\    \hline
        &   &   \\
    $A = \begin{bmatrix}
    5   &   -3  \\
    2   &   1   \\
    0   &   4.5 \\
    \end{bmatrix}$
    &
    $B = \begin{bmatrix}
    1   &   0   \\
    0   &   1   \\
    \end{bmatrix}$
    &
    $C = \begin{bmatrix}
    8   &   6   &   7   &   5   \\
    \end{bmatrix}$
\end{tabular}
\end{frame}

\begin{frame}{What is a Matrix?}
\begin{tcolorbox}[colback=red!20!white, colframe=red!30!black, title=\textbf{Square Matrix}]
A square matrix is a matrix in which the number of rows equals the number of columns.
\end{tcolorbox}
\vspace{8pt} \pause

\begin{tcolorbox}[colback=red!20!white, colframe=red!30!black, title=\textbf{Matrix Elements}]
The elements of a matrix refers to the individual numbers within the matrix.
\end{tcolorbox}
\vspace{8pt}	\pause

We use lower-case letters to refer to individual elements of a matrix. \newline\\ \pause

For instance, the element in row 3 column 1 of matrix $A$ is denoted \[a_{3,1}\]
\end{frame}

\begin{frame}{Adding and Subtracting Matrices}
We can add and subtract matrices {\color{blue}\textbf{that are of the same dimension}} by adding or subtracting corresponding elements.
\end{frame}

\begin{frame}{Example 1}
For the matrices
\[
A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}
\quad   \text{and}  \quad
C =
\begin{bmatrix}
10  &   9   \\
-5  &   7   \\
\end{bmatrix}
\]
find each of the following, if possible.	\newline\\
(a) \quad $A + B$
\[
\begin{bmatrix}
\onslide<2->{1} & \onslide<3->{-2} & \onslide<4->{16} \\
\onslide<5->{9} & \onslide<6->{2} & \onslide<7->{-10} \\
\end{bmatrix}
\]
\end{frame}

\begin{frame}{Example 1 \quad $A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}$}
(b) \quad $A - B$
\[
\begin{bmatrix}
\onslide<2->{9} & \onslide<3->{-14} & \onslide<4->{-8} \\
\onslide<5->{-5} & \onslide<6->{-2} & \onslide<7->{8} \\
\end{bmatrix}
\]
\end{frame}

\begin{frame}{Example 1 \quad $A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}$}
(c) \quad $B + A$
\[
\begin{bmatrix}
\onslide<2->{1} & \onslide<3->{-2} & \onslide<4->{16} \\
\onslide<5->{9} & \onslide<6->{2} & \onslide<7->{-10} \\
\end{bmatrix}
\]
\end{frame}

\begin{frame}{Example 1 \quad $A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad
C =
\begin{bmatrix}
10  &   9   \\
-5  &   7   \\
\end{bmatrix}$}
(d) \quad $A + C$	\newline\\
\onslide<2->{The dimensions are not the same.
\[A_{2\times 3} \quad C_{2\times  2}\]}
\onslide<3->{Matrix addition is \textbf{not possible}.}
\end{frame}

\section{Multiply a matrix by a scalar.}

\begin{frame}{What is a Scalar?}
\begin{tcolorbox}[colback=red!20!white, colframe=red!30!black, title=\textbf{Scalar}]
A \textbf{scalar} is a real number.
\end{tcolorbox}
\vspace{10pt} \pause

To multiply a matrix by a scalar, multiply each element of the matrix by the scalar.
\end{frame}

\begin{frame}{Example 2}
For the matrices
\[
A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad \text{and} \quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}
\]
find each of the following.	\newline\\
(a) \quad $5A$
\[
\begin{bmatrix}
\onslide<2->{25} & \onslide<3->{-40} & \onslide<4->{20} \\
\onslide<5->{10} & \onslide<6->{0} & \onslide<7->{-5} \\
\end{bmatrix}
\]
\end{frame}

\begin{frame}{Example 2 \quad $A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad \text{and} \quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}$}
(b) \quad $6B$
\[
\begin{bmatrix}
\onslide<2->{-24} & \onslide<3->{36} & \onslide<4->{72} \\
\onslide<5->{42} & \onslide<6->{12} & \onslide<7->{-54} \\
\end{bmatrix}
\]
\end{frame}

\begin{frame}{Example 2 \quad $A =
\begin{bmatrix}
5   &   -8  &   4   \\
2   &   0   &   -1  \\
\end{bmatrix}
\quad \text{and} \quad
B =
\begin{bmatrix}
-4  &   6   &   12  \\
7   &   2   &   -9  \\
\end{bmatrix}$}
(c) \quad $2A - 3B$
\onslide<2->{
\[
2A =
\begin{bmatrix}
10 & -16 & 8 \\
4 & 0 & -2 \\
\end{bmatrix}
\quad
3B =
\begin{bmatrix}
-12 & 18 & 36 \\
21 & 6 & -27 \\
\end{bmatrix}
\]}
\onslide<3->{\[2A - 3B =
\begin{bmatrix}
22 & -34 & -28 \\
-17 & -6 & 25 \\
\end{bmatrix}\]
}
\end{frame}

\section{Multiply matrices together.}

\begin{frame}{Matrix Multiplication}
Multiplying matrices does not work in the way you might think. \newline\\ \pause

When multiplying matrices, we {\color{blue}\textbf{DO NOT}} multiply corresponding elements (like when we add and subtract corresponding elements with adding and subtracting matrices).
\end{frame}

\begin{frame}{Matrix Multiplication}
Instead, we multiply corresponding elements of a row by corresponding elements of a column and store the sum of that product of row and column in the intersection of that row and column. \newline\\ \pause

That being said: \pause
\begin{itemize}
    \item We can only multiply matrices if the number of columns in the first matrix equals the number of rows in the second. \pause
    \item Our final result will be a matrix with dimensions equal to the number of rows in the first matrix $\times$ the number of columns in the second.
\end{itemize}
\end{frame}

\begin{frame}{Matrix Multiplication}
So to multiply
\[
D =
\begin{bmatrix}
4   &   -1  &   -7 \\
2   &   0   &   6   \\
\end{bmatrix}
\quad   \text{and}  \quad
E =
\begin{bmatrix}
1   &   9   \\
3   &   -2  \\
4   &   0   \\
\end{bmatrix}
\]
\pause
Note that the dimensions of $D$ are ${\color{blue}\textbf{2}} \times \mathbf{3}$ and the dimensions of $E$ are $\mathbf{3} \times {\color{blue}\textbf{2}}$. \newline\\
\pause

Since the number of columns in $D$ (\textbf{3}) equals the number of rows in $E$ (\textbf{3}), we can multiply the matrices. \newline\\

The product will be a matrix with {\color{blue}\textbf{2}} rows and {\color{blue}\textbf{2}} columns.
\end{frame}

\begin{frame}{Matrix Multiplication}
\[
\begin{bmatrix}
4 & -1 & -7 \\
2	&	0	&	6 \\
\end{bmatrix}  \cdot
\begin{bmatrix}
1	&	9	\\
3	&	-2	\\
4	&	0	\\
\end{bmatrix}
\]
\vspace{10pt}
\[
\onslide<2->{
\begin{bmatrix}
\onslide<3->{4(1) + (-1)(3) + (-7)(4)}	\quad &	\onslide<5->{4(9) + (-1)(-2) + (-7)(0)} 	\\[4pt]
\onslide<7->{2(1) + 0(3) + 6(4)}	\quad &	\onslide<9->{2(9) + 0(-2) + 6(0)}	\\
\end{bmatrix} }
\]
\vspace{10pt}
\[
\onslide<4->{
\begin{bmatrix}
-27 						& \onslide<6->{38}	\\[4pt]
\onslide<8->{26}	&	\onslide<10->{18}\\
\end{bmatrix} }
\]
\end{frame}

\begin{frame}{Matrix Multiplication}
\emph{{\color{blue}\textbf{Note}}}: Always remember to check that the number of columns in the first matrix equals the number of rows in the second. If this is not the case, you can not multiply the matrices together.
\end{frame}

\begin{frame}{Example 3}
For the matrices below, find the products.
\[
A =
\begin{bmatrix}
4   &   8   &   -9  \\
2   &   0   &   3   \\
\end{bmatrix}
\quad
B =
\begin{bmatrix}
7   &   4   \\
1   &   -5  \\
0   &   6   \\
\end{bmatrix}
\quad
C =
\begin{bmatrix}
6   &   -5  \\
4   &   1   \\
\end{bmatrix}
\]
(a) \quad $AB$
\onslide<2->{
\[
\begin{bmatrix}
\onslide<3->{4(7) + 8(1) + (-9)(0)} \quad & \onslide<6->{4(4) + 8(-5) + (-9)(6)} \\[4pt]
\onslide<8->{2(7) + 0(1) + 3(0)} \quad & \onslide<10->{2(4) + 0(-5) + 3(6)} \\[4pt]
\end{bmatrix}
\]
\vspace{8pt}
}
\onslide<4->{
\[
\begin{bmatrix}
\onslide<5->{36}	&	\onslide<7->{-78} \\
\onslide<9->{14}	&	\onslide<11->{26}	\\
\end{bmatrix}
\]
}
\end{frame}

\begin{frame}{Example 3 \quad $
B =
\begin{bmatrix}
7   &   4   \\
1   &   -5  \\
0   &   6   \\
\end{bmatrix} \quad
A =
\begin{bmatrix}
4   &   8   &   -9  \\
2   &   0   &   3   \\
\end{bmatrix}
$}
(b) \quad $BA$
\onslide<2->{
\[
\begin{bmatrix}
\onslide<4->{7(4) + 4(2)} 		\quad & \onslide<6->{7(8) + 4(0)} 		\quad & \onslide<8->{7(-9) + 4(3)} \\[4pt]
\onslide<10->{1(4) + (-5)(2)} 	\quad & \onslide<12->{1(8) + (-5)(0)} 	\quad & \onslide<14->{1(-9) + (-5)(3)} \\[4pt]
\onslide<16->{0(4) + 6(2)} 		\onslide<18->{\quad & 0(8) + 6(0)} 		\onslide<20->{\quad & 0(-9) + 6(3)}		\\[4pt]
\end{bmatrix}
\]
}
\onslide<3->{
\[
\begin{bmatrix}
\onslide<5->{36}	&	\onslide<7->{56}	&	\onslide<9->{-51}	\\
\onslide<11->{-6}		&	\onslide<13->{8}		&	\onslide<15->{-24}	\\
\onslide<17->{12}	&	\onslide<19->{0}		&	\onslide<21->{18}	\\
\end{bmatrix}
\]
}
\end{frame}

\begin{frame}{Example 3 \quad $A =
\begin{bmatrix}
4   &   8   &   -9  \\
2   &   0   &   3   \\
\end{bmatrix}
\quad
C =
\begin{bmatrix}
6   &   -5  \\
4   &   1   \\
\end{bmatrix}$}
(c) \quad $AC$	\newline\\
\onslide<2->{Not possible}
\end{frame}

\begin{frame}{Example 3 \quad $C =
\begin{bmatrix}
6   &   -5  \\
4   &   1   \\
\end{bmatrix}$}
(d) \quad $C^2$
\onslide<2->{
\[
\begin{bmatrix}
6	&	-5	\\
4	&	1	\\
\end{bmatrix}
\cdot
\begin{bmatrix}
6	&	-5	\\
4	&	1	\\
\end{bmatrix}
\]
}
\vspace{10pt}
\onslide<3->{
\[
\begin{bmatrix}
\onslide<4->{6(6) + (-5)(4)} 	\quad & \onslide<6->{6(-5) + (-5)(1)} \\[4pt]
\onslide<8->{4(6) + 1(4)}		\quad & \onslide<10->{4(-5) + 1(1)}		\\[4pt]
\end{bmatrix}
\]
}
\vspace{10pt}
\onslide<5->{
\[
\begin{bmatrix}
16	&	\onslide<7->{-35}	\\
\onslide<9->{28}	&	\onslide<11->{-19}	\\
\end{bmatrix}
\]
}
\end{frame}
\end{document}


% This is a model template for the solutions in computational science. You can find a very useful documentation for LaTeX in Finnish at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/finnish/ or in English at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/english/. The section List of mathematical symbols in Chapter 3 is especially useful for the typesetting of mathematical formulas.

% Compile the document to PDF by command 'pdflatex model.tex' in the terminal. The command must be run twice for the references in the text to be correct.

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
% This includes letters such as � and �
\usepackage[T1]{fontenc}
% Use here 'Finnish' for Finnish hyphenation. You may have to compile the code twice after the change.
\usepackage[english]{babel}
\usepackage{graphicx}
% Some math stuff
\usepackage{amsmath,amsfonts,amssymb,amsbsy,commath,booktabs}
% This is just to include the urls
\usepackage{hyperref}
\usepackage[margin=2cm]{geometry}

\setlength{\parindent}{0mm}
\setlength{\parskip}{1.0\baselineskip}

\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
	language=Python,
	aboveskip=3mm,
	belowskip=3mm,
	showstringspaces=false,
	columns=flexible,
	basicstyle={\small\ttfamily},
	numbers=none,
	numberstyle=\tiny\color{gray},
	keywordstyle=\color{blue},
	commentstyle=\color{dkgreen},
	stringstyle=\color{mauve},
	breaklines=true,
	breakatwhitespace=true,
	tabsize=4
}

\begin{document}

\title{Becs-114.1100 Computational Science -- exercise round 1} % Replace the exercise round number
\author{Kunal Ghosh, 546247} % Replace with your name and student number
\maketitle

\section{Solution to Question 1}\label{prob1}

In this question we have been asked to compute the approximate values of $\epsilon_t$ the truncation error and $\epsilon_r$ the round off error of the function $f(x) = sin(x)$ at $x=0.5$

The round off error $(\epsilon_r)$ is caused by the limited numerical precision of computers. i.e. if our computer is correct upto 6 significant digits then 5.000000 will be equal to 5.0000001 however these two numbers are not equal. Therefore there is clearly, a possibility of error in representing numbers with fixed precision.

The truncation error $(\epsilon_t)$ is caused when we try to use infinite series expansion (for example taylor series) in our computation and we limit the number of terms in the expansion. For the terms that are not included in the expansion, an error is introduced. for example the truncation error using Taylor's theorem is $-\frac{h}{6}f'''(\zeta)$

The total error $\epsilon$ is calculated by summing up truncation and round off errors.
\begin{equation}
\epsilon = \epsilon_t + \epsilon_r
\end{equation}

Specifically, we have been asked to analyse the $sin(x)$ function. assuming $\zeta = 0.5$ we get the truncation error as:
\begin{equation}
\epsilon_t = -\frac{h}{6}(-cos(0.5))
\label{eqn:et}
\end{equation}

Also, the approximate value of the first derivative of a function using central limit formula is:
\begin{equation}
f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}
\end{equation}

Since we need to calculate the first derivative about $ x = 0.5 $ then the above equation becomes..

\begin{equation}
f'(x) \approx \frac{f(0.5+h) - f(0.5-h)}{2h}
\end{equation}

and the analytical first derivative of $sin(x)$, the function under consideration, is $cos(x)$. So the total error $\epsilon$is calculated as..

\begin{equation}
\epsilon \approx \abs{\frac{f(0.5+h) - f(0.5-h)}{2h} - cos(x)}
\label{eqn:fdash}
\end{equation}

and from \ref{eqn:fdash} and \ref{eqn:et} we get..

\begin{equation}
\epsilon_r = \epsilon - \epsilon_t
\label{eqn:er}
\end{equation}
which is also a function of $h$. Now we generate 25 (arbitrary, we could have chosen more) values of $h$ by the following update equation $h \leftarrow {h} / {4}$.

Plotting \ref{eqn:fdash} , \ref{eqn:et} and \ref{eqn:er} as a function of $h$ on the log scale gives us \ref{fig:solution1_fig}. We use the log scale because in that scale really small values are scaled up which helps us visualize the values better.

\begin{figure}[h]
\centering
\includegraphics[scale=0.45]{solution1_fig}
\caption{Total, truncation and rounding errors for $sin(x)$ at $x = 0.5$.}
\label{fig:solution1_fig}
\end{figure}

The corresponding python code can be found at \ref{code:problem1}
\clearpage

\section{Solution to Question 3}\label{prob3}

\begin{figure}[h]
	\centering
	\includegraphics[scale=0.45]{figure_3.pdf}
	\caption{The error between analytical solution and taylor approximation. First row is for equation $(e^x - 1)/x$ and the second row for equation $(e^x - e^{-x}) / 2x$. The third column shows the graphs in the second column magnified at the point of inflexion.}
	\label{fig:solution3_fig}
\end{figure}

\begin{center}
	\begin{table}[h!]
	\begin{tabular}{|c|c|c|c|c|c|}
\hline
& Index & Approximation ln(n) & Error & Approx ln(n + 0.5) & Error \\
\hline
& 100 & 0.582207331652 & 0.00499166675 & 0.57721979014 & 4.12523895743e-06 \\
& 200 & 0.579713581573 & 0.00249791667188 & 0.577216701375 & 1.03647328931e-06 \\
& 300 & 0.578881405643 & 0.00166574074177 & 0.577216126324 & 4.61422707043e-07 \\
& 400 & 0.578465144069 & 0.00124947916699 & 0.577215924668 & 2.59766558375e-07 \\
& 500 & 0.578215331568 & 0.000999666666796 & 0.577215831235 & 1.66333712026e-07 \\
& 600 & 0.578048766753 & 0.000833101851918 & 0.57721578045 & 1.15548026147e-07 \\
& 700 & 0.577929780548 & 0.000714115646296 & 0.577215749814 & 8.49126385871e-08 \\
& 800 & 0.577840534693 & 0.000624869791689 & 0.577215729924 & 6.50228465515e-08 \\
& 900 & 0.577771117576 & 0.000555452674911 & 0.577215716285 & 5.13832113525e-08 \\
& 1000 & 0.577715581568 & 0.000499916666674 & 0.577215706527 & 4.16250224289e-08 \\
& 1100 & 0.577670141486 & 0.000454476584025 & 0.577215699306 & 3.44039702282e-08 \\
& 1200 & 0.577632273698 & 0.000416608796297 & 0.577215693813 & 2.89110814178e-08 \\
& 1300 & 0.577600230976 & 0.000384566074948 & 0.577215689537 & 2.46358701217e-08 \\
& 1400 & 0.577572765242 & 0.000357100340135 & 0.577215686145 & 2.12433216573e-08 \\
& 1500 & 0.577548961198 & 0.000333296296296 & 0.577215683408 & 1.8506176036e-08 \\
& 1600 & 0.577528132349 & 0.000312467447919 & 0.577215681167 & 1.62658729819e-08 \\
& 1700 & 0.577509753714 & 0.000294088811997 & 0.577215679311 & 1.44090542831e-08 \\
& 1800 & 0.577493416959 & 0.000277752057617 & 0.577215677754 & 1.28529425991e-08 \\
& 1900 & 0.577478799712 & 0.000263134810718 & 0.577215676437 & 1.15359471975e-08 \\
& 2000 & 0.577465644068 & 0.000249979166669 & 0.577215675313 & 1.04114609156e-08 \\
& 2100 & 0.577453741243 & 0.000238076341646 & 0.577215674345 & 9.44372391398e-09 \\
& 2200 & 0.577442920411 & 0.000227255509644 & 0.577215673506 & 8.60490512178e-09 \\
& 2300 & 0.577433040453 & 0.000217375551359 & 0.577215672775 & 7.87307719019e-09 \\
& 2400 & 0.577423983767 & 0.000208318865748 & 0.577215672132 & 7.23079007781e-09 \\
& 2500 & 0.577415651568 & 0.000199986666667 & 0.577215671566 & 6.66399990745e-09 \\
& 2600 & 0.577407960266 & 0.000192295364887 & 0.577215671063 & 6.16133355447e-09 \\
& 2700 & 0.577400838656 & 0.000185173753993 & 0.577215670615 & 5.7134669218e-09 \\
& 2800 & 0.577394225701 & 0.000178560799306 & 0.577215670214 & 5.3127137134e-09 \\
& 2900 & 0.577388068786 & 0.00017240388425 & 0.577215669854 & 4.95269592005e-09 \\
& 3000 & 0.577382322309 & 0.00016665740739 & 0.57721566953 & 4.62806759582e-09 \\
& 3100 & 0.577376946553 & 0.000161281651047 & 0.577215669236 & 4.33435054514e-09 \\
& 3200 & 0.577371906763 & 0.000156241861965 & 0.577215668969 & 4.06772471262e-09 \\
& 3300 & 0.577367172401 & 0.000151507499219 & 0.577215668726 & 3.82496601059e-09 \\
& 3400 & 0.577362716516 & 0.000147051614738 & 0.577215668505 & 3.60329799332e-09 \\
& 3500 & 0.577358515242 & 0.000142850340118 & 0.577215668302 & 3.4003705407e-09 \\
& 3600 & 0.57735454736 & 0.000138882458819 & 0.577215668116 & 3.2140982098e-09 \\
& 3700 & 0.577350793949 & 0.000135129047945 & 0.577215667944 & 3.04274017093e-09 \\
& 3800 & 0.577347238078 & 0.000131573176341 & 0.577215667786 & 2.88472257193e-09 \\
& 3900 & 0.577343864551 & 0.000128199649333 & 0.57721566764 & 2.73870248702e-09 \\
& 4000 & 0.577340659693 & 0.000124994791638 & 0.577215667505 & 2.6034862044e-09 \\
& 4100 & 0.577337611164 & 0.000121946262113 & 0.57721566738 & 2.47804521347e-09 \\
& 4200 & 0.577334707796 & 0.000119042894908 & 0.577215667263 & 2.36146646682e-09 \\
& 4300 & 0.577331939464 & 0.0001162745628 & 0.577215667154 & 2.25292040579e-09 \\
& 4400 & 0.577329296961 & 0.000113632059199 & 0.577215667053 & 2.15168582951e-09 \\
& 4500 & 0.577326771897 & 0.000111106995859 & 0.577215666959 & 2.05713035495e-09 \\
& 4600 & 0.577324356615 & 0.000108691713897 & 0.57721566687 & 1.96866778435e-09 \\
& 4700 & 0.577322044108 & 0.000106379206252 & 0.577215666787 & 1.88579540872e-09 \\
& 4800 & 0.577319827951 & 0.000104163049742 & 0.57721566671 & 1.80804604621e-09 \\
& 4900 & 0.577317702247 & 0.000102037345518 & 0.577215666637 & 1.73500225298e-09 \\
& 5000 & 0.577315661568 & 9.99966666332e-05 & 0.577215666568 & 1.66629987586e-09 \\
\hline
\end{tabular}
\caption{ Table logging the Euler value approximations every 100 iterations. The second and the third columns correspond to the convergence equation without the constant and the third and fourth and fifth are ones with a constant (0.5 in this case) }
\label{table:1}
\end{table}
\end{center}

The corresponding python code can be found at \ref{code:problem3}

\clearpage
%\ldots

\section{Solution to Question 5}\label{prob5}

In this problem we have to calculate the error in analytical values and approximate Taylor values of given functions arguments tend to zero. The taylor approximates are useful because as the value of arguments tend to zero the analytical functions would become really large since the argument is in the denominator.

Deriving the taylor approximate of $f_1(x) = \frac{e^x - 1}{x}$ upto the 4th term (arbitrarily chosen)
\begin{equation}
f_1(x) = \frac{1}{x} (e^x - 1)
\label{eqn:q5_1}
\end{equation}
From taylor's expansion
\begin{equation}
e^x = e^0 + \sum_{i=1}^{n-1} \frac{x^i}{i!} e^{(i)}(0) + \frac{x^n}{n!} e^{(n)}(\zeta)
\label{eqn:q5_2}
\end{equation}
substituting \ref{eqn:q5_2} (upto the 4th term i.e. n=4)in \ref{eqn:q5_1} and simplifying ( the $x$ in the denominator cancels out) we get.
\begin{equation}
\frac{1}{x} (e^x - 1) = \frac{1}{x} \left( \frac{x}{1!}e(0) + \frac{x^2}{2!}e^{2}(0) + \frac{x^3}{3!}e^{3}(0) + \frac{x^4}{4!}e^{4}(4) \right)
\end{equation}
\begin{equation}
\frac{1}{x} (e^x - 1) = 1 + \frac{x}{2} + \frac{x^2}{6} + \frac{x^3}{24}e^{4}
\label{eqn:q5_4}
\end{equation}

The approximate value of \ref{eqn:q5_1} is calculated using \ref{eqn:q5_4} for small values of $x$ and then the two are subtracted to get the error which is plotted in \ref{fig:solution5_fig}

Similarly for $f_2(x) = (e^x - e^{-x}) / 2x$ Deriving the taylor approximate upto the 4th term we get.
\begin{equation}
\frac{(e^x - e^{-x})}{2x}  = \frac{1}{2x} \left[ \left( \frac{x}{1!}e(0) + \frac{x^2}{2!}e^{2}(0) + \frac{x^3}{3!}e^{3}(0) + \frac{x^4}{4!}e^{4}(4) \right)
-  \left( \frac{x}{1!}e(0) - \frac{x^2}{2!}e^{2}(0) - \frac{x^3}{3!}e^{3}(0) + \frac{x^4}{4!}e^{4}(4) \right) \right]
\label{eqn:q5_5}
\end{equation}

\begin{equation}
\frac{(e^x - e^{-x})}{2x}  = 1 + \frac{x^2}{3!}
\label{eqn:q5_6}
\end{equation}

As for the first function, approximate value of \ref{eqn:q5_5} is calculated using \ref{eqn:q5_6} for small values of $x$ and then the two are subtracted to get the error which is plotted in \ref{fig:solution5_fig}

\begin{figure}[h]
	\centering
	\includegraphics[scale=0.45]{solution_5_fig.pdf}
	\caption{}
	\label{fig:solution5_fig}
\end{figure}

The corresponding python code can be found at \ref{code:problem5}

\clearpage
\appendix

\section{Appendix A}\label{code:problem1}
Python source code for \ref{prob1}.
{\footnotesize
\begin{lstlisting}
from __future__ import division
import pylab as pl
import numpy as np
from numpy import sin,cos

def f_dash_central_diff(f,x,h):
	# Calculating the derivative of f at x with interval size h
	# using the central limit theorem
    return (f(x + h) - f(x - h)) / (2 * h)

def calculate_error_and_plot():
	# The value at which the function is to be evaluated.
    x = 0.5

    # Initial value of the step size
    h_initial = 0.5
    # Generate a list of values of h each obtained by dividing by a power of 4
    h = np.asarray([h_initial/(4 ** _) for _ in range(25)])

    # The function being evaluated
    f = sin
    # The analytical first derivative of sin(x)
    f_dash = cos
    # The analytical 3rd derivative of sin(x)
    f_3dash = lambda x: -1 * cos(x)

    # Total error
    error_total = abs(f_dash_central_diff(f,x,h) - f_dash(x))
    # Truncation error
    error_trunc = abs((-1 * (h ** 2)/6) * f_3dash(x))
    # Round off error
    error_round = abs(error_total - error_trunc)

    print(h, error_total,  error_trunc, error_round)
    # Plotting the values on the log scale
    pl.loglog(h, error_total, h, error_trunc, h, error_round)
    pl.legend(["$\epsilon$", "$\epsilon_t$", "$\epsilon_r$"], loc = 4)
    pl.savefig("solution1_fig.pdf")
    pl.show()

if __name__ == '__main__':
    calculate_error_and_plot()
\end{lstlisting}
}

\clearpage
\section{Appendix B}\label{code:problem3}
Python source code for \ref{prob3}.
{\footnotesize
\begin{lstlisting}
# Program to estimate the Euler's Constant.
# Print intermediate answers at every 100 steps
from __future__ import division
import math
import sys
import pylab as pl

Euler_Constant = 0.5772156649015329

running_sum_for_all_consts = {}
def print_stdout(index, approx1, error1, approx_with_const, error_with_const):
sys.stdout.write("{:>5} -- {:<20} {:<20} -- {:<20} {:<20}\n".format(index, approx1, error1,  approx_with_const, error_with_const))

def get_euler(n, const=0):
# This method returns the approximation
# of the euler's constant when considering a
# partial sum of n terms in the harmonic series
# Const : is used to speed up the convergence

running_sum = None
if const in running_sum_for_all_consts:
running_sum = running_sum_for_all_consts[const]
else:
running_sum_for_all_consts[const] = {}
running_sum = running_sum_for_all_consts[const]

harmonic_sum = 0
if n-1 in running_sum.keys():
# If we have calculated the sum of harmonic
# series till n-1 use that. (Memoization)
harmonic_sum = running_sum[n-1] + 1/n
running_sum[n] = harmonic_sum
else:
# usually happens for n=1
harmonic_sum = 1/n
running_sum[n] = harmonic_sum

euler_approx = harmonic_sum - math.log(n + const)
return euler_approx

steps = 5000

if __name__ == "__main__":
errors_without_const = []
errors_with_const = []
indices =  []
const = 0.5
print_stdout("Index","Approximation ln(n)", "Error","Approx ln(n + {})".format(const), "Error")
for i in xrange(steps+1):
# +1 because xrange(n) generates values from 0 to n-1
if i == 0:
# we only want to calculate i from 1 to n(steps in this case)
# using range(steps)[1:] is very memory inefficient for
# large values of "steps"
continue
approx_val = get_euler(i)
approx_val_const = get_euler(i,const)

indices.append(i)
errors_without_const.append(abs(approx_val - Euler_Constant))
errors_with_const.append(abs(approx_val_const - Euler_Constant))
if i % 100 == 0:
print_stdout(i, approx_val, errors_without_const[-1], approx_val_const, errors_with_const[-1])

pl.figure(1)
pl.subplot(211)
pl.plot(indices, errors_without_const)
pl.legend(["Error without const factor"], loc = 1)
pl.ylabel("Error")
pl.ylim(ymin=0,ymax=0.005)
# pl.xlim(xmin=0,xmax=200)
pl.subplot(212)
pl.plot(indices, errors_with_const)
pl.legend(["Error with Const factor of {}".format(const)], loc = 1)
pl.xlabel("N")
pl.ylabel("Error")
pl.ylim(ymin=0,ymax=0.00005)
# pl.xlim(xmin=0,xmax=50)
pl.savefig("figure_3.pdf")
pl.show()
\end{lstlisting}
}

\clearpage
\section{Appendix C}\label{code:problem5}
Python source code for \ref{prob5}.
{\footnotesize
\begin{lstlisting}

from __future__ import division
import numpy as np
import doctest
import pylab as pl

# Class encapsulating the first function
class function_1:
	@staticmethod
	def exact(x):
		# Exact analytical value
		'''
		>>> function_1.exact(1)
		1.718281828459045
		>>> function_1.exact(0.5)
		1.2974425414002564
		>>> function_1.exact(1/2)
		1.2974425414002564
		'''
		return ((np.e ** x) - 1) / x

	@staticmethod
	def taylor_approx(x):
		# Taylor approximation
		'''
		>>> function_1.taylor_approx(1)
		3.941589584714343
		>>> function_1.taylor_approx(0.5)
		1.5760320314226264
		>>> function_1.taylor_approx(1/2)
		1.5760320314226264
		>>> function_1.taylor_approx(0)
		1.0
		'''
		return 1 + (x/2) + ((x ** 2)/6) + ((x ** 3) * (np.e ** 4)/24)

# Class encapsulating the second function
class function_2:
	@staticmethod
	def exact(x):
		# Exact analytical value
		return ((np.e ** x) - (np.e ** (-1 * x))) / (2 * x)

	@staticmethod
	def taylor_approx(x):
		# Taylor approximation
		return 1 + ((x ** 2)/6)

if __name__ == "__main__":
	# Running the doc tests.
	doctest.testmod()

	#Generate 1000 values of x from 10^-9 to 1
	x = np.linspace(10 ** -9, 1, 1000)
	error_function1 = np.abs(function_1.taylor_approx(x) - function_1.exact(x))
	error_function2 = np.abs(function_2.taylor_approx(x) - function_2.exact(x))

	for index in xrange(len(error_function1)):
		print index, error_function1[index], error_function2[index]

	# Plot the first function
	pl.figure(1, figsize=(15,8))
	pl.subplot(231)
	pl.loglog(x,error_function1)
	pl.title("Log scale error func $(e^x - 1)/x$")
	pl.ylabel("Error")

	pl.subplot(232)
	pl.plot(x,error_function1)
	pl.title("Error func $(e^x - 1)/x$ ")

	pl.subplot(233)
	pl.plot(x,error_function1)
	pl.xlim(xmin=10**(-4), xmax=10**(-2))
	pl.ylim(ymin=10**(-9), ymax=10**(-7))
	pl.title("Error func  $(e^x - 1)/x$ near inflexion")

	# Plot the second function
	pl.subplot(234)
	pl.loglog(x, error_function2)
	pl.title("Log scale error func $(e^x - e^{-x}) / 2x$")
	pl.ylabel("Error")
	pl.xlabel("x")

	pl.subplot(235)
	pl.plot(x, error_function2)
	pl.title("Error func $(e^x - e^{-x}) / 2x$")
	pl.xlabel("x")

	pl.subplot(236)
	pl.plot(x, error_function2)
	pl.xlim(xmin=10**(-4), xmax=10**(-2))
	pl.ylim(ymin=10**(-14), ymax=10**(-12))
	pl.title("Error func $(e^x - e^{-x}) / 2x$ near inflexion")
	pl.xlabel("x")

	# Save the figure as a pdf
	pl.savefig("solution_5_fig.pdf")
	# Show the figure
	pl.show()

\end{lstlisting}
}

\end{document}


%!TEX root = ../OGUSAdoc.tex

\section{Small Open Economy}
In the small open economy version of \ogindia, the county faces an exogenous world interest rate, $r^{*}_{t}$ that determines the amount of savings and investment.  If the supply of savings from households does not meet the demand for private capital and private borrowing, foreign capital will flow in to make excess demand zero at the world interest rate.  Let the total capital stock be given by the quantity of domestically supplied capital and foreign supplied capital, i.e., $K_{t}= K^{d}_{t}+K^{f}_{t}$.  Then foreign capital is given by:

\begin{equation}
  K^{f}_{t} = K^{demand}_{t} - B_{t} - D_{t},
\end{equation}

where $B_{t}$ is aggregate household savings and $D_{t}$ is government borrowing.  Capital demand is determined from the firm's first order condition for its choice of capital.

\section{Partially Open Economy}

In the partially open economy version of \ogindia, the openness of the economy is modeled through two parameters that capture the extent of foreign lending to the domestic government and the amount of foreign lending of private capital to firms.

The parameter $\zeta_{D}$ gives the share of new debt issues that are purchased by foreigners.  The law of motion for foreign-held debt is therefore given by:

\begin{equation}
  D^{f}_{t+1} = D^{f}_{t} + \zeta_{D}(D_{t+1} - D_{t})
\end{equation}

Domestic debt holdings as then the remaining debt holdings needed to meet government demand for debt:

\begin{equation}
  D^{d}_{t} = D_{t} - D^{f}_{t}
\end{equation}

The parameters $\zeta_{K}$ helps to determine the share of domestic capital held by foreigners.  In particular, $\zeta_{D}$ is the share of foreign capital held by foreigners in the small open economy specification:

\begin{equation}
  K^{f}_{t} = \zeta_{K}K^{open}_{t}
\end{equation}

$K^{open}_{t}$ is the amount of capital that would need to flow into the country to meet firm demand for capital at the exogenous world interest rate from the small open economy specification, net of what domestic households can supply:

\begin{equation}
  K^{open}_{t} = K^{demand, open}_{t} - (B_{t} - D^{d}_{t})
\end{equation}

where, $K^{demand, open}_{t}$ is total capital demand by domestic firms at $r^
{*}_{t}$, $B_{t}$ are total asset holdings of domestic households, and $D^{d}_{t}$ are holdings of government debt by domestic households.  Total asset holdings from households result from solving the household problem at the endogenous home country interest rate.  Note that there is a disconnect between the interest rates that determine firm capital demand and domestic household savings and the interest rate used to determine $K^{demand, open}_{t}$.  This assumption is useful in that it nests the small open economy case into the partial open economy model.  However, it does leave out the realistic responses of foreign capital supply to differentials in the home country interest rate and the world interest rate.

Given the two equations above, we can find the total supply of capital as:

\begin{equation}
  \begin{split}
  K^{supply}_{t} & = K^{d}_{t} + K^{f}_{t} \\
   & = B_{t} - D^{d}_{t} + \zeta_{K}K^{open}_{t} \\
  \end{split}
\end{equation}

\subsection{Stationarization}

\subsubsection{Foreign debt purchases}

The amount of government debt is growing by the rate of productivity growth and the rate of population growth.  Thus, stationarized government debt is given by:

\begin{equation}
  \hat{D}_{t} = \frac{D_{t}}{e^{g_{y}t}N_{t}}
\end{equation}

The stationarized form of the foreign and domestic capital holdings thus become:

\begin{equation}
  \begin{split}
    \hat{D}^{f}_{t+1} & = \frac{D^{f}_{t+1}}{e^{g_{y}t+1}N_{t+1}} = \frac{D^{f}_{t}}{e^{g_{y}t+1}N_{t+1}} + \zeta_{D}(\frac{D_{t+1}}{e^{g_{y}t+1}N_{t+1}} - \frac{D_{t}}{e^{g_{y}t+1}N_{t+1}}) \\
    & = \frac{\hat{D}^{f}_{t}N_{t}}{e^{g_{y}}N_{t+1}} + \zeta_{D}(\hat{D}_{t+1} - \frac{\hat{D}_{t}N_{t}}{e^{g_{y}}N_{t+1}}) = \frac{\hat{D}^{f}_{t}}{e^{g_{y}}g_{n,t+1}} + \zeta_{D}(\hat{D}_{t+1} - \frac{\hat{D}_{t}}{e^{g_{y}}g_{n,t+1}})
  \end{split}
\end{equation}

and

\begin{equation}
  \hat{D}^{d}_{t} = \frac{D^{d}_{t}}{e^{g_{y}t}N_{t}} = \frac{D_{t}}{e^{g_{y}t}N_{t}} - \frac{D^{f}_{t}}{e^{g_{y}t}N_{t}} = \hat{D}_{t} - \hat{D}^{f}_{t}
\end{equation}

Note that in the steady-state, we still have $\hat{D}^{f} = \zeta_{D}\hat{D}$

\subsubsection{Foreign capital purchase}

In the equation for foreign capital purchases, all quantities are growing at the rate of technological change and population growth.  Thus, to stationarize this equation, we find:

\begin{equation}
  \hat{K}^{f}_{t} = \frac{K^{f}_{t}}{e^{g_{y}t}N_{t}}= \zeta_{K}\frac{K^{open}_{t}}{e^{g_{y}t}N_{t}} = \zeta_{K}\hat{K}^{open}_{t}
\end{equation}

and

\begin{equation}
  \hat{K}^{open}_{t} = \frac{K^{open}_{t}}{e^{g_{y}t}N_{t}}= \frac{K^{demand, open}_{t}}{e^{g_{y}t}N_{t}} - \left(\frac{B_{t}}{e^{g_{y}t}N_{t}} - \frac{D^{d}_{t}}{e^{g_{y}t}N_{t}}\right) = \hat{K}^{demand, open}_{t} - (\hat{B}_{t}-\hat{D}_{t})
\end{equation}

and

\begin{equation}
  \begin{split}
  \hat{K}^{supply}_{t} &= \frac{K^{supply}_{t}}{e^{g_{y}t}N_{t}} = \frac{K^{d}_{t}}{e^{g_{y}t}N_{t}} + \frac{K^{f}_{t}}{e^{g_{y}t}N_{t}} = \hat{K}^{d}_{t} + \hat{K}^{f}_{t} \\
   & = \frac{B_{t}}{e^{g_{y}t}N_{t}} - \frac{D^{d}_{t}}{e^{g_{y}t}N_{t}} + \zeta_{K}\frac{K^{open}_{t}}{e^{g_{y}t}N_{t}} = \hat{B}_{t} - \hat{D}^{d}_{t} + \zeta_{K}\hat{K}^{open}_{t} \\
  \end{split}
\end{equation}

\subsubsection{Resource Constraint}

As a result of the foreign ownership of capital, the resource constraint is modified.  In a closed economy, the resource constraint is given by:

\begin{equation}
  Y_{t} = C_{t} + I_{t} + G_{t}
\end{equation}

In the partially open economy, some of the output is paid to the foreign owners of capital.  This amount is given by $r_{t}K^{f}_{t}$.  In addition, foreign lending to the home country's government relaxes the resource constraint.  In the case , the resource constraint is given by:

\begin{equation}
  Y_{t} = C_{t} + (K^{d}_{t+1} - K^{d}_{t}) + \delta K_{t} +  G_{t} + r_{t}K^{f}_{t} - (D^{f}_{t+1}-D^{f}_{t}) + rD^{f}_{t}
\end{equation}

The stationarized version of this becomes:

\begin{equation}
  \hat{Y}_{t} = \hat{C}_{t} + (\hat{K}^{d}_{t+1}e^{g_{y}}(1+g_{n,t+1}) - \hat{K}^{d}_{t}) + \delta \hat{K}_{t} +  \hat{G}_{t} + r_{t}\hat{K}^{f}_{t} - (\hat{D}^{f}_{t+1}e^{g_{y}}(1+g_{n,t+1})- \hat{D}^{f}_{t}) + r_{t}\hat{D}^{f}_{t}
\end{equation}

Note that with a wedge between the interest rate on government debt and private capital as outlined in Chapter \ref{SecRateWedge} we need to be careful about the interest rates paid and the amount of capital and debt held.  In the case of the partially open economy with an interest rate wedge, we assume that domestic and foreign investors earn a rate of return on their portfolio of:

\begin{equation}
  r_{hh,t} = \frac{r_{t}K_{t} + r_{gov,t}D_{t}}{K_{t} + D_{t}}
\end{equation}

In the partially open economy, the ratio of private capital to debt held by domestic households might differ from the ratio held by foreign households, but we assume they still earn the same rate of return on their portfolio.\footnote{One reason for this assumption is that it simplifies our solution since we do not need to know the domestic versus foreign holdings of capital before solving the households' problems.}  With this assumption, we modify the resource constraint to be a function of this portfolio interest rate:

\begin{equation}
  \hat{Y}_{t} = \hat{C}_{t} + (\hat{K}^{d}_{t+1}e^{g_{y}}(1+g_{n,t+1}) - \hat{K}^{d}_{t}) + \delta \hat{K}_{t} +  \hat{G}_{t} + r_{hh, t}\hat{K}^{f}_{t} - (\hat{D}^{f}_{t+1}e^{g_{y}}(1+g_{n,t+1})- \hat{D}^{f}_{t}) + r_{hh,t}\hat{D}^{f}_{t}
\end{equation}


\section{Introduction}
Haskell is a strongly typed, purely functional programming language \cite{haskell}. An advantage of this is that it simplifies reasoning about the correctness of algorithms and data structures. Even though this reasoning is simple, Haskell does not provide mechanisms to guarantee this correctness as the code changes, so there is still a risk of making mistakes. Using tests can also never guarantee that code is correct. In contrast, Agda is a dependently typed programming language and interactive theorem prover \cite{agda}. Using Agda and the Curry-Howard correspondence \cite{chc}, it is possible to write a formal proof about the code in the language itself, and to use the compiler to verify the correctness of the proof \cite{schwaab, van}. The compiler also verifies that the proof is still valid each time the code changes.

Agda2hs \cite{agda2hs} is a program which identifies a common subset of Agda and Haskell, and provides a tool that automatically translates code from this subset of Agda to Haskell. This makes it possible to write a program in this subset, using the power of Agda to prove properties about it, and then translate the Agda code to readable Haskell code. An additional advantage is that Agda developers can then also use the verified library. However, Agda2hs is not completed yet, as it still lacks some Haskell features that it cannot compile from Agda. It is also not yet known how much extra effort it takes to write code in this subset of Agda.

The goal of this paper is to investigate whether agda2hs can be used to produce a verified implementation of the QuadTree library\cite{quadtree}. The following questions will be answered:
\begin{enumerate}[label=(\roman*)]
	\itemsep-0.2em
	\item Can the QuadTree library be implemented in the subset of Agda that can be compiled by Agda2hs? (section \ref{implementation})
	\item What properties does the QuadTree library guarantee? (section \ref{finding_props_to_prove}-\ref{props_to_prove})
	\item How can the properties that the QuadTree library guarantees be proven? (section \ref{prove_invariants}-\ref{results})
	\item How can the time and effort required to verify the QuadTree library be reduced? (section \ref{reduce_time})
\end{enumerate}


%
% STAT 100: Chance and Data Analysis - A Course Overview
% Section: Elements of Statistics
%
% Author: Jeffrey Leung
%

\section{Elements of Statistics}
	\label{sec:elements-of-statistics}
\subsection{Subject of Study}
	\label{subsec:elements-of-statistics:subject-of-study}
\begin{easylist}

	& \emph{Statistics:} Collection, organization, analysis, and interpretation of data
		&& \emph{Statistic:} Number which summarizes data about a sample
		&& \emph{Descriptive statistic:} Description or summary about a sample
			&&& Often inferred from analyzed statistics
		&& \emph{Parameter:} Value which summarizes data about a population
			&&& Calculated exactly by collecting data from the entire population (see \emph{census}, subsection~ %TODO reference, and 'exactly'?
			&&& Estimated by calculating a statistic about a representative sample of the population

	\bigskip

	& \emph{Individual:} Object of study about which data is collected
		&& Not necessarily a person
	& \emph{Sample:} Individual from which data is collected directly
		&& See \emph{sample survey}, subsection~\ref{subsec:data-collection:methods}
	& \emph{Population:} Set of \emph{all} individuals for which data is inferred
		&& Inference about the population is made from the data of the sample
		&& See \emph{census}, subsection~\ref{subsec:data-collection:methods}

	\medskip

	& Example: In an analysis of salary distribution of all UBC recent graduates, a research team collected information from 500 individuals. The data contained the annual salary, age, occupation, gender, and year of graduation of the individual. \smallskip \\
	Individual: A UBC recent graduate \\
	Sample: The 500 surveyed UBC recent graduates \\
	Population: All UBC recent graduates

\end{easylist}
\subsection{Categorical Variables}
	\label{subsec:elements-of-statistics:categorical-variables}
\begin{easylist}

	& \emph{Variable:} Data collected from an individual

	& \emph{Categorical:} Variable which is a label or category
		&& Example: Occupation, gender
		&& Has no unit of measurement
		&& All possible options must be specified
		&& Statistics of categorical data are the percentages/proportions of all categories
		&& Displayed using a bar graph

	& Example: A study is conducted to collect the following information from an SFU student.
		&& Whether or not an SFU student has a profile on Facebook \\
			(categorical variable - options are yes or no)
		&& Number of text messages sent recently \\
			(not a categorial variable - number, not specific options)
		&& How long it took to download the most recent video game \\
			(not a categorical variable - amount, not specific options)

	& For the analysis of categorical data, see subsection~\ref{subsec:analysis-of-single-variable-data:categorical-data}
	& For an example with both quantitative and categorical variables, see \emph{Quantitative Variables}, subsection~\ref{subsec:elements-of-statistics:quantitative-variables}

\end{easylist}
\subsection{Quantitative Variables}
	\label{subsec:elements-of-statistics:quantitative-variables}
\begin{easylist}

	& See \emph{variable}, subsection~\ref{subsec:elements-of-statistics:categorical-variables}

	& \emph{Quantitative:} Variable which is countable
		&& E.g. Salary, age
		&& Always has a unit of measurement
		&& Can have basic mathematical operations applied to it
		&& Examples of quantitative statistics:
			&&& Average/mean
			&&& Median
			&&& Standard deviation
			&&& Quartiles
			&&& Maximum/minimum
		&& Displayed using a histogram

	\medskip
	& Example: A study is conducted to collect the following information from an SFU student.
		&& Whether or not an SFU student has a profile on Facebook \\
			(not a quantitative variable - not a number)
		&& Number of text messages sent recently \\
			(quantitative variable - number of messages)
		&& How long it took to download the most recent video game \\
			(quantitative variable - amount of time)

	& For the analysis of quantitative data, see subsection~\ref{subsec:analysis-of-single-variable-data:quantitative-data}

	& Example: 7 countries were studied; the results are shown in table~\ref{tab:information-gathered-on-7-countries}.

	\Deactivate
	\begin{table}[!htb]
		\centering
		\caption{Information gathered on 7 Countries}
		\label{tab:information-gathered-on-7-countries}
		\begin{tabular}{ l | l r r r }
			& & Land area & Population & GDP \\
			Country & Continent & (km\textsuperscript{2}) & (millions) & (per capita) \\
			\hline
			Canada & North America & 9,093,510 & 33.31 & 46,236 \\
			China & Asia & 9,327,480 & 1324.66 & 4,428 \\
			Germany & Europe & 348,630 & 82.11 & 40,152 \\
			India & Asia & 2,974,190 & 1,139.97 & 1,475 \\
			Japan & Asia & 364,500 & 127.70 & 42,831 \\
			South Africa & Africa & 1,214,470 & 48.79 & 7,275 \\
			United States & North America & 9,147,420 & 304.38 & 47,199
		\end{tabular}
	\end{table}
	\Activate

	\medskip
	Individual: A country in the world \\
	Sample: The 7 countries surveyed \\
	Population: All countries in the world \\
	Quantitative variables: Land area, populations, GDP \\
	Categorical variable: Continent

\end{easylist}
\clearpage


\lab{Appendix}{More Matplotlib}{More Matplotlib}
\label{mpltables}

\objective{Display relevant matplotlib information}

The documentation for matplotlib can be frustrating to maneuver and relevant information is sometimes difficult to find. For this reason, we have supplied some of the more applicable and useful information here.
For a more general introduction to matplotlib, see lab \ref{lab:Matplotlib_and_Mayavi}.

\section*{Colours}
By default, every plot is assigned a different color specified by a ''color cycle". It can be overwritten by specifying what color is desired. \li{matplotlib} recognizes some basic built-in colours.

\begin{tabular}
{|l||l|}
\hline
color & command \\
\hline
black & k \\
green & g \\
blue & b \\
red & r \\
cyan & c \\
magenta & m \\
yellow & y \\
white & w\\
\hline

\end{tabular}

The following displays how these colours can be  implemented. All the built-in colours are displayed for your convenience in Figure \ref{colours}
\lstinputlisting[style=fromfile]{colours.py}

\begin{figure}
\includegraphics[width=\textwidth]{colours.pdf}
\caption{A display of all the built-in colours.}
\label{colours}
\end{figure}

There are many other ways to specific colours. A popular method to access colours that are not built-in is to use a \li{RGB} tuple. Another way is to specify the color using an html hex string or its associated html color name like \li{DarkOliveGreen, FireBrick, LemonChiffon, MidnightBle, PapayaWhip,}, or \li{SeaGreen}.

\section*{Axes}
You may have noticed the use of \li{plt.ylim([ymin, ymax])} in the previous code. This explicitly sets the boundary of the y-axis. Similarily, \li{plt.xlim([xmin, xmax])} can be used to set the boundary of the x-axis.
Doing both commands simultaneously is possible with the \li{plt.axis([xmin, xmax, ymin, ymax])}.
Remember that these commands must be executed after the plot.

Another popular command is \li{plt.axis("tight")} which adjusts the axes so all data is visible and centered.

\section*{lines}
\subsection*{width}
You may have noticed that the width of the lines above seemed thin considering we wanted to inspect the line colour. \li{linewidth} is a keyword argument that is defaulted to be \li{None} but can be given any real number to adjust the line width.

The following displays how \li{linewidth} is implemented. It is displayed in Figure \ref{linewidth}
\lstinputlisting[style=fromfile]{linewidth.py}

\begin{figure}
\includegraphics[width=\textwidth]{linewidth.pdf}
\caption{plot of varying linewidths.}
\label{linewidth}
\end{figure}

\subsection*{style}
By default, plots are executed with a solid line style. We can easily change that. The following are accepted format string characters to indicate line style.

\begin{tabular}
{|c||l|}
\hline
character & description \\
\hline
- & solid line style \\
-{}- & dashed line style\\
-. & dash-dot line style \\
: & dotted line style \\
. & point marker \\
, & pixel marker \\
o & circle marker \\
v & triangle\_down marker \\
\^{} & triangle\_up marker \\
$<$ & triangle\_left marker \\
$>$ & triangle\_right marker \\
1 & tri\_down marker \\
2 & tri\_up marker \\
3 & tri\_left marker \\
4 & tri\_right marker \\
s & square marker \\
p & pentagon marker \\
* & star marker \\
h & hexagon1 marker \\
H & hexagon2 marker \\
+ & plus marker \\
x & x marker \\
D & diamond marker \\
d & thin\_diamond marker \\
$|$ & vline marker \\
\_{} & hline marker \\
\hline
\end{tabular}

The following displays how \li{linestyle} can be  implemented.

It is displayed in Figure \ref{linestyle}
\lstinputlisting[style=fromfile]{linestyle.py}

\begin{figure}
\includegraphics[width=\textwidth]{linestyle.pdf}
\caption{plot of varying linestyles.}
\label{linestyle}
\end{figure}

\section*{Text}
It is also possible to add text to your plots.

To label your axes, the \li{plt.xlabel()} and the \li{plt.ylabel()} can both be used.

It is also possible to give your entire plot a title by using the \li{plt.title()} command.

All of the \li{text()} commands can be customized with \li{fontsize} and \li{color} keyword arguments.

We can add these elements to our previous example.

It is displayed in Figure \ref{text}
\lstinputlisting[style=fromfile]{text.py}

\begin{figure}
\includegraphics[width=\textwidth]{text.pdf}
\caption{plot of varying linestyles using text labels.}
\label{text}
\end{figure}


%              %
%%            %%
%%% PREAMBLE %%%
%%            %%
%              %

% Document class
\documentclass[11pt]{beamer}

\usetheme{Boadilla}
\usecolortheme{beaver}
\useinnertheme{rectangles}

\setbeamertemplate{navigation symbols}{}

% Font
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}

% Language and typography
\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguage{french}

\usepackage{csquotes}

\usepackage{microtype}

% Mathematics
\usepackage{mathtools}

% Floats
\usepackage{float}
\usepackage{booktabs}
\usepackage{multicol}

% References
\usepackage{cleveref}

%              %
%%            %%
%%% DOCUMENT %%%
%%            %%
%              %

% Information
\title{Mathematics}
\subtitle{Basics}
\author[A. Quenon]{Alexandre Quenon}

% Text
\begin{document}

% *** Title page *** %
\begin{frame}
	\titlepage
\end{frame}

% *** Contents *** %
\begin{frame}
	\frametitle{Overview}

	\tableofcontents
\end{frame}

% *** Tutorial *** %
%-----
\section{Useful packages}

\begin{frame}
	\frametitle{Packages for mathematics}

	Some packages very useful for mathematics are listed here below:
	\begin{itemize}
		\item the very well-known \emph{amsmath} package which is the backbone for mathematics with \LaTeX{},
		\item \alert{\emph{mathtools}} which is mainly an upgrade of \emph{amsmath},
		\item \emph{cases} which provides the \texttt{numcases} command to number all lines of a system of equations (cf. tutorial C100),
		\item \emph{systeme} which provides command to format a system of equations for better readility (cf. tutorial C100), and
		\item \emph{physics} which provides many commands to facilitate the writing of \enquote{complex} equations including derivatives and partial derivatives (cf. tutorial C102).
	\end{itemize}
\end{frame}

%-----
\section{Writing equations}

\begin{frame}
	\frametitle{Equations}
	\framesubtitle{Main environment for mathematics}

	The \structure{main \LaTeX{}'s environment} to write an equation is\dots{} \alert{\texttt{equation}}.
	As an example:
	\begin{equation}
		\vec{\nabla}\cdot\vec{B}=0
		\label{eq::Maxwell:no_magnetic_monopole}
	\end{equation}

	The starred version (\texttt{equation*}) disables numbering:
	\begin{equation*}
		\vec{\nabla}\times\vec{E}=-\frac{\partial\vec{B}}{\partial t}
	\end{equation*}

	There are also shorter forms:
	\begin{itemize}
		\item the \textbackslash{}[ \ldots \textbackslash{}] wrapper surrounding the equation,
		\item the double \$\$ symbol surrounding the equation (still overly used but it is plain \TeX{} and should not be used).
	\end{itemize}

	\structure{Recommendation}: use \texttt{equation} instead of the short forms:
	\begin{itemize}
		\item it highlights the mathematics in the \LaTeX{} code,
		\item versatility between the numbered and the unnumbered version.
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Equations}
	\framesubtitle{Inline equations -- text-mode \& math-mode}

	\structure{Need}: it is sometimes useful to write mathematics inside a text, for instance to describe the variable $\vec{B}$ appearing in \cref{eq::Maxwell:no_magnetic_monopole}.
	To do so, the mathematical formula must be wrapped by single \$ signs.

	Inline equations underlines a fundamental behaviour of \LaTeX{}: the difference between \alert{math-mode and text-mode}.
	Compare:
	\begin{itemize}
		\item regular text (text-mode),
		\item $text in mathematical environment$ (math-mode).
	\end{itemize}
	Know the mode inside an environment to understand how \LaTeX{} will behave.

	\structure{Recommendation}: use inline equations only
	\begin{itemize}
		\item to express a variable,
		\item for a very short and well-known formula that must not be referred and that do not contain big symbols (integral, sum, etc.).
	\end{itemize}
\end{frame}

%-----
\section{Grouping equations}

\begin{frame}
	\frametitle{Grouping equations}
	\framesubtitle{No alignment inside the group}

	\structure{Tool}: \alert{\texttt{gather}} environment, double backslash (\textbackslash\textbackslash) before starting a new equation.

	Example with the local equation from Ampere theorem:
	\begin{gather}
		\vec{\nabla} \times \vec{B}
			= \mu_0 \vec{\jmath}
			+ \varepsilon_0 \mu_0 \frac{\partial \vec{E}}{\partial t}, \\
		\intertext{which can be written in the integral form by applying the Green theorem}
		\oint_{C} \vec{B} \cdot \mathrm{d} \vec{l}
			= \mu_0 \iint_S \vec{\jmath} \cdot \mathrm{d} \vec{S}
			+ \varepsilon_0 \mu_0 \iint_S \frac{\partial \vec{E}}{\partial t} \cdot \mathrm{d} \vec{S}.
	\end{gather}

	\structure{Extra}: text can be written between equations thanks to the \texttt{intertext} and \texttt{shortintertext} commands.
	\LaTeX{} is in text-mode within these commands and in math-mode within the rest of the \texttt{gather} environment.
\end{frame}

\begin{frame}
	\frametitle{Grouping equations}
	\framesubtitle{Alignment inside the group}

	\structure{Tool}: \texttt{align} environment, double backslash (\textbackslash\textbackslash) before starting a new equation, ampersand (\&) to indicate where the alignment is performed.

	Examples with the vector potential:
	\begin{align}
		\vec{B} &= \vec{\nabla} \times \vec{A} \\
		\vec{E} &= -\vec{\nabla} V - \frac{\partial \vec{A}}{\partial t}
	\end{align}

	 \structure{Extra}: the \texttt{intertext} and \texttt{shortintertext} commands are also available.

	 The alignment is generally performed on the equal sign.
\end{frame}

%-----
\section{Symbols for mathematics}

\begin{frame}
	\frametitle{Common symbols for mathematics}

	Mathematics would not be mathematics without any symbols.
	As there is no point to show an exhaustive list of existing mathematical symbols in this tutorial, here follows a list of references:
	\begin{itemize}
		\item your \LaTeX{} editor, which generally provides a list of shortcuts and buttons to generate the correct commands,
		\item a \href{https://fr.sharelatex.com/learn/List_of_Greek_letters_and_math_symbols}{quick review} of symbols from ShareLaTeX,
		\item a \href{http://www.rpi.edu/dept/arc/training/latex/LaTeX_symbols.pdf}{big list} of symbols native from \TeX{} and coming from different packages.
	\end{itemize}
\end{frame}

%-----
%\section{Laying out the mathematics}
%
%\begin{frame}
%	\frametitle{Package options for layout modification}
%
%	It is possible to change the layout of equations thanks to package options:
%	\begin{itemize}
%		\item position of equation numbers
%		\begin{itemize}
%			\item on the right (default) with the \texttt{reqno} option,
%			\item on the left with the \texttt{leqno} option.
%		\end{itemize}
%	\end{itemize}
%\end{frame}

% *** END *** %
\end{document}