\subsection{Machine learning models}
    \label{sec:models}

    The main goal of this work is to assert whether traditional machine learning models can ``separate'' documents according to BP citations. We will explore two approaches: unsupervised and supervised learning, with a focus on the latter.

    Unsupervised learning means extracting patterns from data that is not labeled, \eg, our raw documents. Considering this, we will present the raw documents to some algorithms, without explaining which precedents are being cited, and we will check their outputs to see if there is some pattern.

    We have vectors representing our texts as $X$ and cited BPs as $y$, the definition of supervised learning. With this in mind, we will also adjust several supervised models in this data.

    \paragraph{Latent Dirichlet allocation.} Because we are dealing with texts, it is very convenient to experiment with latent Dirichlet allocation (LDA), the most common topic modeling technique. Assuming the existence of $K$ topics, each document has a distribution over the topics, and each topic has a distribution over the words. Mathematically, the formulation is:

    \begin{itemize}
            \item Each topic $k \in \{1, \cdots, K\}$ has distribution $\beta_k \sim \text{Dirichlet}(\eta)$ over the words;
            \item Each document $d \in \{1, \cdots, D\}$ has distribution $\theta_d \sim \text{Dirichlet}(\alpha)$ over the topics;
            \item Given a document $d$, the topics have distribution $z | \theta_d \sim \text{Multinomial}(\theta_d)$;
            \item Given a topic $k$, the words have distribution $w | \beta_k \sim \text{Multinomial}(\beta_k)$.
    \end{itemize}

    The idea of using this model is to verify whether LDA can recover the topic of the BP being cited in a document. For example, if there were only two cited precedents on the dataset, one could fit LDA using two topics and verify if the most important words for each topic are representative of the precedents themselves. Even more, it is possible to verify if this topic-to-precedent assignment is good.

    \paragraph{Truncated SVD dimensionality reduction.} We will reduce the dimensionality of TF-IDF vectors to visualize if they are of some kind separable. The dimensionality reduction technique will be the (truncated) singular value decomposition, already explained in \autoref{sec:document_embedding}. We will experiment with dimensionalities 2 and 3, which can be visualized on a 2-dimensional screen.

    \paragraph{K-nearest neighbors.} As TF-IDF vectors lie in some vector space, the decision of which BP is being cited could be taken considering its neighbor's cited precedent. This is what the k-nearest neighbors (k-NN) model does. The number of neighbors (parameter $k$) is chosen by cross-validation.

    \paragraph{Linear regression.} This model is already well known, but for regression. For classification, and when the target is binary, it is easy to fit a regression model using $y \in \{-1, 1\}$ and considering the predicted class as the prediction sign. For multiclass, it is fitted one regression per target, and the class with the highest value is chosen. We will also use Ridge regularization, and the hyperparameter is chosen by cross-validation.

    Although using linear regression for classification is not that convenient, mainly because the output can't be interpreted as a probability, we can use this model to assert if our vectors are \textbf{linearly separable} in the very high dimensional space.

    \paragraph{Logistic regression.} Logistic regression is like a linear regression for classification, so it is more suitable for our task. For adjusting the model for various classes, we will minimize the multinomial logistic regression loss \cite{bishop2006pattern}:
    \[- \sum_{n=1}^N \sum_{k=1}^K y_{nk} \ln p_{nk},\]
    $y_{nk}$ indicating that the sample $n$ belongs to class $k$, $p_{nk}$ the estimated probability of sample $n$ belonging to class $k$ (calculated with softmax of linear functions of $X_n$). We also experiment with $\ell^2$ regularization, with hyperparameter chosen by cross-validation.

    \paragraph{Linear discriminant analysis.} Linear discriminant analysis fits a probability distribution for each class, considering the priori as the proportion of that class in the data and the distribution of data, given the class, as a multivariate Gaussian. The decision boundary is linear, so we can also assert the \textbf{linear separability} of the documents using this model.

    \paragraph{Random forest.} From the decision tree-based models, we experiment with random forests. Just like an ordinary decision tree, but many of them aggregated, each considering a bootstrap sample of the dataset and also a sample of the predictors. For a regression task, we have learned that, when the decision space is divided into various nodes, the output of the model is the mean of the samples inside that node.

    It is natural to extend regression decision trees to classification decision trees. For example, with the already adjusted model, the predicted class is the class with more samples inside the node, and the probabilities are the class sample proportions.

    There are some hyperparameters to be chosen, \eg, depth of the trees and number of trees. These are chosen using cross-validation.

    \paragraph{Support vector machines.} Support vector machines (SVM) are very powerful, even having a simple mathematical formulation. Intuitively, they fit an optimal hyperplane dividing (a transformation of) the dataset, but also allowing some points to not obey this restriction. The optimization problem is:
    \[\begin{aligned}
        \min_{w, b, \zeta} \quad & \frac{1}{2}w^\intercal w + C \sum_{i = 1}^{N}{\zeta_i} \\
        \textrm{s.t.} \quad & y_i (w^\intercal \phi(x_i) + b) \ge 1 - \zeta_i \\
        & \zeta_i \ge 0, \ \ i = 1, \cdots, n. \\
    \end{aligned}\]
    The regularization $C$ parameter is chosen by cross-validation. The kernel function also is chosen by CV, between linear and radial basis function (RBF), so as the $\gamma$ hyperparameter of RBF.


\section{Scaling and Critical Phenomena}
Free energy density is
\begin{align}
    f\left( t, h \right)
\end{align}
where $f=F/V$, $t=\frac{|T - T_c|}{T_c}$ and $h$ is external field.
Then the scaling hypothesis is
\begin{align}
    f\left( \lambda^{u} t, \lambda^{w} h \right)
    &=
    \lambda f\left( t, h \right)
\end{align}
for some scaling parameters $u,w$.

From the scaling hypothesis you can drive these beautiful equalities
\begin{align}
    \alpha + \beta\left( \delta + 1 \right) &= 2\\
    \alpha + 2\beta + \gamma &= 2
\end{align}
They are called the Griffiths equality and Rushbrooke equality.


%!TEX root =  ../main.tex

\subsection{Babylonian Method}

\objective{Graph and understand the slope of root functions.}

Square roots are useful whenever someone tell you the square footage of an area.  If it
were a square, how long would each side be?  Cube roots are similar for volume.

Dealing with square roots without calculator can be rather intimidating.  But it doesn't have
to be.  3,000 years ago, the Babylonians found a method that truly repays the effort put into it:
each iteration double the number of digits in the estimate!  Few formula converge so quickly.

The square root of $A$:

$R_{n+1}=\cfrac{R_n + \frac{A}{R_n}}{2}$

In short:
\begin{enumerate}
\item Pick the best number you can.
\item Divide the original by ``the number''
\item Average the answer with ``the number''
\item Return to step 2 with this as ``the number''
\end{enumerate}

\subsection{Even vs. Odd}
OK, so you found the square or cube root of some number to as many decimal places as
you need.  Rather than doing \emph{that} more than once, wouldn't it be preferable to find
out how much adding to the inside of a square root changes the output?  In other words,
what is the rate of change, or the derivative?

You will need to multiply top and bottom of the difference quotient by the conjugate of the
numerator.

\begin{example}
\exProblem
A woman rode a train where the cost of train ride is directly proportional to the square
root of the distance ridden.  Her ticket to ride 140 miles cost \$24.40.
Find the final per mile rate of a man who got off after 35 miles,
compared the woman's final rate.

\exSolution
Directly proportional means we can set up an equation $c = k \sqrt{d}$, where $c$ and $d$
are the cost and distance.  If $24.4 = k\sqrt{140}$ then $k=\frac{24.4}{\sqrt{140}}$.  The
derivative of $\frac{24.4}{\sqrt{140}}\sqrt{x} = \frac{12.2}{\sqrt{140x}}$.  At 35, the
derivative is equal to $\frac{24.4}{sqrt{140}} \approx 0.17$, and at 140 it is
$\frac{12.2}{\sqrt{140}} \approx 0.09$.  This means the woman exited paying over
\$0.08 less per mile than the man.
\end{example}

~\vfill


\subsection{One-Parameter Subgroups and the Exponential Map}

\subsubsection{One-Parameter Subgroups}

\begin{theorem}[20.1]\textbf{(Characterizations of One-Parameter Subgroups)}
\end{theorem}

\begin{proposition}[20.8] \textbf{(Properties of Exponential Map)} Let $\begin{aligned} & \quad \\
    & G \quad \, & \text{ Lie group } \\
    & \mathfrak{g} \quad \, & \text{ Lie algebra } \end{aligned}$

\begin{enumerate}
\item[(a)] $\exp: \mathfrak{g} \to g$ smooth
\item[(b)] $\forall \, X \in \mathfrak{g}$, $s,t\in \mathbb{R}$, $\exp{(s+t)}X = \exp{sX}\exp{tX}$
\item[(c)] $\forall \, X \in \mathfrak{g}$, $(\exp{X})^{-1} = \exp{(-X)}$
\item[(d)] $\forall \, X \in \mathfrak{g}$, $n\in \mathbb{Z}$, $(\exp{X})^n = \exp{(nX)}$
\item[(e)] differential $(d\exp)_0:T_0\mathfrak{g} \to T_eG$ is identity, under canonical identifications of both $T_0\mathfrak{g}$ and $T_eG$ with $\mathfrak{g}$ itself.
\item[(f)] $\exp$ restricts to diffeomorphism from some neighborhood of $0$ in $\mathfrak{g}$ to neighborhood of $e$ in $G$.
\item[(g)] if $\Phi:G\to H$ lie group homomorphism, following commutes

\begin{tikzpicture}
  \matrix (m) [matrix of math nodes, row sep=3em, column sep=2em, minimum width=1em]
  {
 \mathfrak{g}  &   \mathfrak{h}      \\
G     &   H   \\ };
%  \path[-stealth]
\path[->]
  (m-1-1) edge node [above] {$\Phi_*$} (m-1-2)
edge node [left] {$\exp$} (m-2-1)
(m-1-2) edge node [auto] {$\exp$} (m-2-2)
  (m-2-1) edge node [auto] {$\Phi$} (m-2-2);
\end{tikzpicture}

\item[(h)] flow $\theta$ of left-invariant vector field $X$, $\theta_t = R_{\exp{tX}}$
\end{enumerate}
\end{proposition}

\begin{proof}
\begin{enumerate}
\item[(a)]
\item[(b)]
\item[(c)]
\item[(d)]
\item[(e)] Let $X\in \mathfrak{g}$ arbitrary, let $\begin{aligned} & \quad \\
  & \sigma: \mathbb{R} \to \mathfrak{g} \\
  & \sigma(t) = tX \\
  & \dot{\sigma}(0) = X \end{aligned}$

\begin{tikzpicture}
  \matrix (m) [matrix of math nodes, row sep=3em, column sep=2em, minimum width=1em]
  {
 \mathfrak{g}  &   G      \\
T_0\mathfrak{g}     &   T_eG   \\ };
%  \path[-stealth]
\path[->]
  (m-1-1) edge node [above] {$\exp$} (m-1-2)
edge node [left] {$$} (m-2-1)
(m-1-2) edge node [auto] {$(d\exp)_0$} (m-2-2)
  (m-2-1) edge node [auto] {$$} (m-2-2);
\end{tikzpicture}

\[
(d\exp )_0(X) = (d\exp )_0(\dot{\sigma}(0)) = (\exp \circ \sigma)'(0) = (\exp{ (tX)})'(0) = \left. \frac{d}{dt} \right|_{t=0} \exp{ (tX)}= X
\]
\item[(f)] $(d\exp )_0 =1$ and so by inverse function thm., $\exists \, (d\exp)_0^{-1}=1^{-1}=1$, and so $\begin{aligned} & \quad \\
  & U \ni 0 \\
  & U \subseteq \mathfrak{g} \end{aligned} \xrightarrow{ \simeq } \begin{aligned} & \quad \\
  & V \ni e \\
  & V \subseteq G \end{aligned}$
\end{enumerate}

\end{proof}


\section{Finite types}

\subsection{The pigeonhole principle}

The pigeonhole principle states that if we place more than $n$ balls in $n$ bags, then at least one bag will contain more than one ball. In this section we will give a type theoretical proof of the pigeonhole principle.

First we give a definition of a function that counts the number of elements in a decidable subset of $\Fin(n)$.

\begin{defn}
  Let $P$ be a decidable subset of $\Fin(n)$. We define the number $|P|:\N$ of elements in $P$.
\end{defn}

\begin{proof}[Construction]
  We give the construction of $|P|$ by induction on $n:\N$. In the base case we note that $\Fin(\zeroN)$ has no elements, so we define $|P|\defeq\zeroN$.

  For the inductive step, we define $|P|$ by case analysis on $P(\inr(\ttt))+\neg P(\inr(\ttt))$. Let $P'$ be the family over $\Fin(n)$ given by $P'(i)\defeq P(\inl(i))$. In the case where $P(\inr(\ttt))$ holds, then we define $|P|\defeq \succN |P'|$. In the case where $P(\inr(\ttt))$ doesn't hold we define $|P|\defeq |P'|$.
\end{proof}

\begin{defn}
  For any $i:\Fin(\succN(n))$ we define a function
  \begin{equation*}
    \skipFin(i):\Fin(n)\to\Fin(\succN(n)).
  \end{equation*}
\end{defn}

\begin{proof}[Construction]
  The function $\skipFin(i)$ is defined by induction on $n:\N$. In the base case, the function
  \begin{equation*}
    \skipFin(i):\Fin(\zeroN)\to\Fin(\succN(\zeroN))
  \end{equation*}
  is defined to be the unique map out of the empty type. In the successor case we define
  \begin{equation*}
    \skipFin(i) : \Fin(\succN(n))\to\Fin(\succN(\succN(n)))
  \end{equation*}
  by induction on $i:\Fin(\succN(\succN(n)))$. The function
  \begin{equation*}
    \skipFin(\inl(i)):\Fin(\succN(n))\to\Fin(\succN(\succN(n)))
  \end{equation*}
  is a map between coproducts, so it can be defined using the functorial action of coproducts of \cref{ex:coproduct_functor}. We take
  \begin{equation*}
    \skipFin(\inl(i))\defeq \skipFin(i)+\idfunc.
  \end{equation*}
  The function
  \begin{equation*}
    \skipFin(\inr(i)):\Fin(\succN(n))\to\Fin(\succN(\succN(n)))
  \end{equation*}
  is just the function $\inl$.
\end{proof}

\begin{lem}
  For each $i:\Fin(\succN(n))$, the function
  \begin{equation*}
    \skipFin(i):\Fin(n)\to\Fin(\succN(n))
  \end{equation*}
  is an embedding.
\end{lem}

\begin{proof}
  This assertion is proven by induction on $n$. In the base case, we note that any map out of the empty type is an embedding, by \cref{ex:is-emb-empty}. In the inductive step we proceed by case analysis on $i:\Fin(\succN(\succN(n)))$. In the case of $\inl(i)$ we note that
  \begin{equation*}
    \skipFin(\inl(i))\jdeq \skipFin(i)+\idfunc
  \end{equation*}
  is the functorial action of coproducts on two embeddings. Therefore we conclude by \cref{ex:is-emb-coprod} that this map is an embedding. In the case of $\inr(i)$ we note that $\inl$ is an embedding by \cref{ex:is-emb-inl-inr}.
\end{proof}

\begin{lem}
  Consider a map $g:\Fin(m)\to\Fin(\succN(n))$. Furthermore, suppose that $i:\Fin(\succN(n))$ is not in the image of $g$, i.e.~that $\neg(\fib{g}{i})$. Then we can construct a commuting triangle
  \begin{equation*}
    \begin{tikzcd}
      & \Fin(n) \arrow[d,"\skipFin(i)"] \\
      \Fin(m) \arrow[r,swap,"g"] \arrow[ur,densely dotted,"f"] & \Fin(\succN(n)).
    \end{tikzcd}
  \end{equation*}
\end{lem}

Finally, we prove the pigeonhole principle.

\begin{thm}\label{thm:pigeonhole}
  For any $m,n:\N$ and any function $f:\Fin(m)\to\Fin(n)$, if $m>n$, then there is an $i:\Fin(n)$ which is in the image of more than one point in $\Fin(m)$.
\end{thm}

\begin{proof}
  The pigeonhole principle is proven by induction on $m,n:\N$. In the base case for $m$ there is nothing to show because $m>n$ is empty. For the inductive step on $m$ and the base case for $n$, we note that $\Fin(\succN(m))\jdeq \Fin(m)+\unit$ and $\Fin(\zeroN)\jdeq \emptyt$. Therefore $f:\Fin(\succN(m))\to\Fin(\zeroN)$ is a function from a pointed type to the empty type, which gives us a contradiction.

  It remains to give the inductive step for $n$. Let $i\defeq f(\inr(\ttt)):\Fin(\succN(n))$. Since the ordering relation $<$ on $\N$ is decidable, we can decide whether $i$ is in the image of more than one point in $\Fin(m)$ by deciding whether or not $1<|P|$ holds for
  \begin{equation*}
    P(j)\defeq (f(j)=i)
  \end{equation*}
  If this is the case, this completes the proof.

  Now suppose that $1\not<|P|$. Since $P(\inr(\ttt))$ holds it follows that $|P|=1$. Now we observe that $i$ is not in the image of $f\circ \inl$. Therefore we obtain a commuting square
  \begin{equation*}
    \begin{tikzcd}
      \Fin(m) \arrow[r,densely dotted,"{f'}"] \arrow[d,swap,"\inl"] & \Fin(n) \arrow[d,"{\skipFin(i)}"] \\
      \Fin(\succN(m)) \arrow[r,swap,"f"] & \Fin(\succN(n)).
    \end{tikzcd}
  \end{equation*}

  Note that the induction hypothesis the pigeonhole principle applies to the function $f':\Fin(m)\to\Fin(n)$. Since $m>n$ it follows that there is an element $j:\Fin(n)$ that is in the image of $f'$ of more than one element of $\Fin(m)$. Now we observe that there is an equivalence
  \begin{equation*}
    \fib{f'}{j}\simeq \fib{f}{\skipFin(i,j)}
  \end{equation*}
  because both the left and right maps in the commuting square are embeddings. Therefore we conclude that $\skipFin(i,j)$ is in the image of $f$ of more than one element of $\Fin(\succN(m))$.
 \end{proof}

\begin{cor}\label{cor:pigeonhole}
  Given $m>n$, no function $\Fin(m)\to\Fin(n)$ is an embedding.
\end{cor}

It is straightforward to see that the statements of \cref{thm:pigeonhole,cor:pigeonhole} are equivalent, and one might argue that the statement of \cref{cor:pigeonhole} is the more `type theoretical way' of phrasing the pigeonhole principle. However, the relation to counting the number of points that get mapped to

\begin{thm}\label{thm:generalized-pigeonhole}
  For any $m,n:\N$ and any function $f:\Fin(m)\to\Fin(n)$, if $m>kn$ for some $k:\N$, then there is an $i:\Fin(n)$ which is in the image of more than $k$ points in $\Fin(m)$.
\end{thm}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{fmhyp}
\section*{\hspace*{-1.6cm} fmhyp}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Signal with hyperbolic frequency modulation or group delay law.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[x,iflaw] = fmhyp(N,P1)
[x,iflaw] = fmhyp(N,P1,P2)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty fmhyp} generates a signal with a hyperbolic frequency
        modulation
\[        x(t) = \exp\left(i2\pi\left(f_0 t +
\frac{c}{log|t|}\right)\right).\]

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c} Name &
Description & Default value\\ \hline {\ty N} & number of points in time\\
{\ty P1} & if {\ty nargin==2, P1} is a vector containing the two
coefficients {\ty [f0 c]}.  If {\ty nargin==3, P1} (as {\ty P2}) is a
time-frequency point of the form {\ty [ti fi]}. {\ty ti} is in seconds and
{\ty fi} is a normalized frequency (between 0 and 0.5). The coefficients
{\ty f0} and {\ty c} are then deduced such that the frequency modulation
law fits the points {\ty P1} and {\ty P2}\\ {\ty P2} & same as {\ty P1} if
{\ty nargin==3} & optional\\ \hline {\ty x } & time row vector containing
the modulated signal samples \\ {\ty iflaw} & instantaneous frequency law\\

\hline
\end{tabular*}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Examples}
\begin{verbatim}
         [X,iflaw]=fmhyp(100,[1 .5],[32 0.1]);
         subplot(211); plot(real(X));
         subplot(212); plot(iflaw);
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
fmlin, fmsin, fmpar, fmconst, fmodany, fmpower.
\end{verbatim}
\end{minipage}


\chapter{Algebraic Integers}
	\setcounter{page}{1}
	\pagenumbering{arabic}
	\section{Integral closure and algebraic integers}
		\subsection{Definition and examples}
			Problems in solving polynomial equations give rise to a lot of concepts in algebra and geometry. If we are specifically interested in $\mds{Z}$, we have the concept of \textbf{algebraic integers}.
			\begin{definition}
				A finite extension $K$ of the rational number $\mds{Q}$ is called a \textbf{number field}. The integral closure of $\mds{Z}$ in $K$ is called the ring of \textbf{algebraic integers} of $K$, and is denoted by $\OK$. To be precise, every element $x \in \OK$ is a zero of a monic polynomial $f \in \Z[X].$
			\end{definition}

			For this concept we have a lot of classic examples:

			\begin{example}
				If $K=\Q$, then $\OK$ is simply $\Z$. This is intuitive because suppose $x=a/b \in \Q$ is integral over $\Z$ where $(a,b)=1$, then
				\[
				x^n+c_1x^{n-1}+\cdots+c_n = 0
				\]
				where $c_i \in \Z$. Multiplying by $b^n$ yields
				\[
				a^n+c_1a^{n-1}b+\cdots+c_nb = 0
				\]
				Hence $b$ divides $a^n$. But we also have $(a^n,b)=1$, hence $b=\pm 1$, which is to say $x \in \Z$.

				There is an more general setting. Since $\Z$ is a unique factorial domain (UFD), and UFD is integrally closed \href{https://proofwiki.org/wiki/Unique_Factorization_Domain_is_Integrally_Closed}{[proof]}, we have $\Z=\OK$.
			\end{example}

			\begin{example}
				The Gaussian rational $\Q(i)=K$. Indeed it is natural to consider Gaussian integer $\Z[i]$ first. For any $z=m+ni \in \Z[i]$, we have
				\[
				z^2-2mz+m^2+n^2=0
				\]
				Hence $\Z[i] \subset \OK$. The converse is similar to our proof when $K=\Q$.
			\end{example}

			\begin{example}
				Cyclotomic field $K=Q(\xi_n)$, where $\xi_n$ is the root of $x^n-1$. In this case we have $\OK = \Z[\xi_n]$.
			\end{example}

			\begin{example}
				Quadratic field $K=\Q(\sqrt{d})$, where $d$ is a square-free integer $>1$. This time the algebraic integer ring is different from what you may have thought: $\OK = \Z[\omega]$ where
				\[
				\omega = \begin{cases}
					\frac{1+\sqrt{d}}{2}, &\quad d = 4k+1, \\
					\sqrt{d}, &\quad \text{otherwise}.
				\end{cases}
				\]
			\end{example}
			It turns out we are studying polynomials such as
			\begin{itemize}
				\item $x^2+1=0$.
				\item $x^n-1=0$.
				\item $x^2-d=0$.
			\end{itemize}
		It also turns out that many properties are not restricted to $\Z$, but to a specific class of rings. Hence we will investigate some properties in the sense of commutative ring theory.
		\subsection{Algebraic extension and integral closure}
			First of all we show that being algebraic almost implies being integral.
			\begin{lemma}\label{alg-int}
				Let $A$ be a domain, $K$ its quotient field, and $x$ algebraic over $K$. Then there exists an element $c \ne 0$ of $A$ such that $cx$ is integral over $A$.
			\end{lemma}
			\begin{proof}
				Since $x$ is algebraic, we have an equation
				\[
					a_nx^n+\cdots+a_0=0
				\]
				with $a_i \in A$ and $a_n \ne 0$. Hence
				\[
					a_n^{n-1}(a_nx^n+\cdots+a_0)=(a_nx)^n+\cdots+a_0a_n^{n-1}=0
				\]
				which is to say $a_nx$ is integral over $A$.
			\end{proof}

			Since finite extensions are algebraic, we are always free to use this lemma for the topic of number field.

			\begin{theorem}\label{closure-f-g}
				Let $A$ be an integrally closed Noetherian ring. Let $L$ be a finite separable extension of its quotient field $K$. Then the integral closure of $A$ in $L$ is finitely generated over $A$.
			\end{theorem}
			\begin{proof}
				Since $A$ is Noetherian, all submodules of a finitely generated module over $A$ is finitely generated. Hence it suffices to prove that the integral closure of $A$ is contained in a finitely generated $A$-module.\\
				Let $w_1,\dots,w_n$ be a basis of $L$ over $K$. After multiplying each $w_i$ by some suitable element of $A$ (see lemma \ref{alg-int}), we may assume without loss of generality that the $w_i$ are integral over $A$. To study the integral closure of $A$ in $L$, we pick an arbitrary element $z = b_1w_1+\cdots+b_nw_n$ and study its coefficients. \\
				Since $L/K$ is separable, the \href{https://stacks.math.columbia.edu/tag/0BIF}{field trace} form
				\[
					Q_{L/K}:L \times L \to K, \quad (x,y) \mapsto \tr_{L/K}(xy)
				\]
				is non-degenerate  \href{https://stacks.math.columbia.edu/tag/0BIL}{[proof]}, so we claim that $L^\ast$ is isomorphic to $L$ under $Q_{L/K}$. Indeed, one can define a $K$-linear map
				\[
					d:L \to L^\ast, \quad x \mapsto (y \mapsto Q_{L/K}(x,y)=\tr(xy)).
				\]
				This map is injective because $Q_{L/K}$ is non-degenerate. Since $L$ and $L^\ast$ has the same dimension, $d$ has to be surjective.\\
				Let $w^1,\dots,w^n$ be the dual basis of $w_1,\dots,w_n$. If we put $v_i=d^{-1}(w^i)$, we have
				\[
					\tr(v_i w_j) = \delta_{ij}.
				\]
				Let $c \ne 0$ be an element of $A$ such that $cv_i$ is integral over $A$, then $cv_iz$ is integral and so is $\tr(cv_iz)$. Since $\tr$ is a $K$-valued function, we have
				\[
					\tr(czv_i)=c\tr(v_iz)=c d(v_i)(z) = cb_i \in A \implies b_i \in Ac^{-1}.
				\]
				Hence
				\[
					z \in Ac^{-1}w_1+\cdots+Ac^{-1}w_n
				\]
				which is to say $z$ is finitely generated. Since $z$ is arbitrarily picked, the closure itself is contained in a finitely generated $A$-module, which finishes the proof.
			\end{proof}
			Note $Z$ is itself a Noetherian ring and integrally closed. $\Q$ is the fraction ring of $\Z$, and finite extensions of $\Q$ are always separable. It follows (non-trivially) that
			\begin{corollary}
				$\OK$ is finitely generated over $\Z$.
			\end{corollary}

			Next we study the rank of $\OK$ over $\Z$. Being finitely generated is not exactly what we want.

			\begin{theorem}
				Let $A$ be a principal ideal ring, and $L$ a finite separable extension of its quotient field $K$, of degree $n$. Let $B$ be the integral closure of $A$ in $L$. Then $B$ is a free module of rank $n$ over $A$.
			\end{theorem}

			\begin{proof}
				Since $A$ is contained in $K$, $B$ is contained in $L$, whenever $ab=0$ with $a \in A$, $b \in B$, we have $a=0$ or $b=0$. Hence $B$ is torsion-free. Therefore as a finitely generated (theorem \ref{closure-f-g}) torsion-free module, $B$ is a free module over $A$ \href{http://du.ac.in/du/uploads/departments/mathematics/study-material/MMATH18-201\%20_MT_PID.pdf}{[Theorem 2.7]}. Since $L$ is a $n$-dimensional vector space over $K$, for $y \in L$ we have
				\[
					y = c_1e_1+\cdots+c_ne_n
				\]
				where $e_1,\dots,e_n$ is a basis and $c_1,\cdots,c_n \in K$. When $y \in B$, we must have $c_1,\cdots,c_n \in A$, which is to say $B$ has rank $[L:K]=n$.
			\end{proof}
			Hence the rank of $\OK$ over $\Z$ is determined by $[K:\Q]$.

		\subsection{Localisation}

			\begin{theorem}
				Let $A \subset B$ be rings, and $S$ a multiplicatively closed subset of $A$. If $B$ is integral over $A$, then $S^{-1}B$ is integrally closed in $S^{-1}A$. If $C$ is the integral closure of $A$ in $B$, then $S^{-1}C$ is the integral closure of $S^{-1}A$.
			\end{theorem}

			\begin{proof}
				First we assume $B$ is integral over $A$. Pick $x/s \in S^{-1}B$ with $x \in B$ and $s \in S$. By definition we have
				\[
					x^n + a_1x^{n-1}+\cdots + a_n = 0
				\]
				with $a_i \in A$. Multiplying by $(1/s)^n$ gives
				\[
					(x/s)^n + (a_1/s)(x/s)^{n-1}+\cdots+a_n/s^n = 0
				\]
				which shows that $x/s$ is integral over $S^{-1}A$. Hence the first statement is proved. \\
				Now we assume $C$ is the integral closure of $A$ in $B$. By the first statement we see $S^{-1}C$ is integral over $S^{-1}A$. Suppose $b/s \in S^{-1}B$ is integral over $S^{-1}A$, we have an equation of the form
				\[
					(b/s)^n+(a_1/s_1)(b/s)^{n-1}+\cdots+a_n/s_n=0.
				\]
				Multiplying by $(st)^n$ where $t=s_1\cdots s_n$ gives an equation of integral independence for $bt$ over $A$. Hence $bt \in C$. But $b/s = bt/st$, hence $b/s \in S^{-1}C$ and we are done.
			\end{proof}

			If $S$ happens to be a complement of a prime ideal $\mfk{p}$, we have a satisfying result

			\begin{corollary}\label{int-loc}
				If $B$ is integral over $A$, then $B_\mfk{p}$ is integral over $A_\mfk{p}$.
			\end{corollary}

			If $B$ is replaced by a field extension $L$ of the quotient field of $A$, and $C$ is replaced by the integral closure of $A$, we have the following corollary:

			\begin{corollary}
				If $B$ is the integral closure of $A$ in some field extension $L$ of the quotient field of $A$, then $S^{-1}B$ is the integral closure of $S^{-1}A$ in $L$.
			\end{corollary}

			% TODO: ADD SOME EXAMPLES
		\subsection{Prime Ideals}
			By theorem \ref{closure-f-g}, $\OK$ is a finitely-generated $\Z$-module, hence is a Noetherian domain. By transitivity of integral closures, $\OK$ is integrally closed. We are now interested in the Krull dimension of $\OK$. To do this, we investigate more of the prime ideal with respect to integral closure.
			\begin{definition}
				Let $B$ be a ring containing a ring $A$. Let $\mfk{p}$ be a prime ideal of $A$ and $\mfk{P}$ a prime ideal of $B$. We say that $\mfk{P}$ \textbf{lies above} $\mfk{p}$ if $\mfk{P} \cap A = \mfk{p}$ and we then write $\mfk{P}|\mfk{p}$.
			\end{definition}

			If $\mfk{P}|\mfk{p}$, we have a commutative diagram:
			\[
			\begin{tikzcd}
				B \arrow[r, "\pi'"]               & B/\mathfrak{P}                  \\
				A \arrow[r, "\pi"] \arrow[u, "i"] & A/\mathfrak{p} \arrow[u, "i'"']
			\end{tikzcd}
			\]
			where $i$ and $i'$ are inclusions, $\pi$ and $\pi'$ are canonical homomorphisms. \\
			If $B$ is integral over $A$, then $B/\mfk{P}$ is integral over $A/\mfk{p}$, this is because of the following lemma if we take $\sigma$ to be $\pi$:
			\begin{lemma}
				Let $A \subset B$ be rings, and $\sigma:B \to C$ be a homomorphism. If $B$ is integral over $A$, then $\sigma(B)$ is integral over $\sigma(A)$.
			\end{lemma}
			\begin{proof}
				If $B$ is integral over $A$, then for any $x \in B$ there is an equation
				\[
				x^n + a_{n-1}x^{n-1}+\cdots+a_0 = 0.
				\]
				Therefore
				\[
				\begin{aligned}
					\sigma(x^n+a_{n-1}x^{n-1}+\cdots+a_0) &= \sigma(x^n)+\sigma(a_{n-1}x^{n-1})+\cdots+\sigma(a_0) \\
					&= \sigma(x)^n + \sigma(a_{n-1})\sigma(x)^{n-1}+\cdots+\sigma(\sigma) \\
					&= 0.
				\end{aligned}
				\]
				Hence $\sigma(x)$ is integral in $\sigma(A)$.
			\end{proof}

			We want to show that prime ideals of $\OK$ is maximal, and they should be corresponded to prime ideals in $\Z$, which is maximal. For this reason we show the existence of lying-above prime ideals.

			\begin{theorem}\label{lying-above}
				Let $A$ be a ring, $\mfk{p}$ a prime ideal, and $B \supset A$ integral over $A$. Then $\mfk{p}B \ne B$, and there exists a prime ideal $\mfk{P}$ of $B$ lying above $\mfk{p}$.
			\end{theorem}

			\begin{proof}
				We know that $B_\mfk{p}$ is integral over $A_\mfk{p}$ (corollary \ref{int-loc}) and that $A_\mfk{p}$ is local with maximal ideal $\mfk{m}_\mfk{p}=\mfk{p}A_\mfk{p}$. It follows that
				\[
				\mfk{p}B_\mfk{p}=\mfk{p}A_\mfk{p}B = \mfk{p}A_\mfk{p}B_\mfk{p}=\mfk{m}_\mfk{p}B_\mfk{p}.
				\]
				Hence it suffices to prove our assertion when $A$ is local. If $\mfk{p}B=B$, we have an equation
				\[
				1 = a_1b_1+\cdots+a_nb_n
				\]
				with $a_i \in \mfk{p}$ and $b_i \in B$. Let $B_0 = A[b_1,\cdots,b_n]$. Then $\mfk{p}B_0=B_0$ and $B_0$ is a finitely generated $A$-module. Hence by Nakayama's lemma, $B_0=0$, which is absurd.

				To prove the existence of $\mfk{P}$, consider the following commutative diagram:
				\[
				\begin{tikzcd}
					B \arrow[r]           & B_\mathfrak{p}           \\
					A \arrow[r] \arrow[u] & A_\mathfrak{p} \arrow[u]
				\end{tikzcd}
				\]
				where all arrows are natural inclusions. As is proved, $\mfk{m}_\mfk{p}B_\mfk{p} \ne B_\mfk{p}$. Hence $\mfk{m}_\mfk{p}B_\mfk{p}$ is contained in a maximal ideal $\mfk{M}$ of $\mfk{p}$, and therefore $\mfk{M} \cap A_\mfk{p}$ contains $\mfk{m}_\mfk{p}$. And we pick $\mfk{P}=\mfk{M} \cap B$. Then $\mfk{P}$ is a prime ideal of $B$, and taking intersection with $A$ going both ways around our diagram shows that $\mfk{M} \cap A = \mfk{p}$, so that
				\[
				\mfk{P} \cap A = \mfk{p},
				\]
				as was to be shown.
			\end{proof}

			Now we proceed to the crucial theorem to determine whether a prime lying above is maximal.

			\begin{theorem}\label{lie-above-maximal}
				Let $A$ be a subring of $B$, and assume $B$ is integral over $A$. Let $\mfk{P}$ be a prime ideal of $B$ lying over a prime ideal $\mfk{p}$ of $A$. Then $\mfk{P}$ is maximal $\iff$ $\mfk{p}$ is maximal.
			\end{theorem}

			\begin{proof}
				$\implies$: Note $B/\mfk{P}$ is a field and is integral over the ring $A/\mfk{p}$. Were $A/\mfk{p}$ not a field, there would be a non-trivial ideal $\mfk{m}$ of it, and $B/\mfk{P}$ would have a prime ideal $\mfk{M}$ lying above $\mfk{m}$, by theorem \ref{lying-above}. A contradiction. \\
				$\impliedby$: Note $A/\mfk{p}$ is a field. It suffices to prove that a ring $R$ which is integral over a field $k$ is a field. If $k$ is a field and non-zero $x \in R$ is integral over $k$, we have a minimal polynomial
				\[
				x^n+c_{n-1}y^{n-1}+\cdots+c_0=0
				\]
				with $c_i \in k$. Since $R$ is integral, we have $c_0 \ne 0$. We can clearly write
				\[
				x^{-1}=-c_0^{-1}(x^{n-1}+c_{n-1}y_{n-2}+\cdots+c_1) \in R,
				\]
				which is to say $R$ is integral, and the theorem is therefore proved.
			\end{proof}
			With respect to localisation, we can show the stability of prime ideals lying above:
			\begin{corollary}
				Let $A \subset B$ be rings, $B$ integral over $A$; Let $\mfk{P}$ and $\mfk{P}'$ be prime ideals of $B$ such that $\mfk{P} \subset \mfk{P}'$ and both $\mfk{P}$ and $\mfk{P}'$ lie above a prime ideal $\mfk{p}$ of $A$, then $\mfk{P}=\mfk{P}'$.
			\end{corollary}
			\begin{proof}
				By corollary \ref{int-loc}, $B_\mfk{p}$ is integral over $A_\mfk{p}$. Let $\mfk{m}$ be the extension of $\mfk{p}$ in $A_\mfk{p}$ and $\mfk{M},\mfk{M}'$ be the extensions of $\mfk{P}$ and $\mfk{P}'$ respectively in $B_\mfk{p}$. Then $\mfk{m}$ is the maximal ideal of $A_\mfk{p}$; $\mfk{M} \subset \mfk{M}'$, and $\mfk{M}$, $\mfk{M}'$ lies above $\mfk{m}$. Hence by theorem \ref{lie-above-maximal}, $\mfk{M}$ and $\mfk{M}'$ are both maximal, hence equal. This reduces to $\mfk{P}=\mfk{P}'$.
			\end{proof}
			And now we are ready to prove that $\OK$ is Dedekind.

			\begin{theorem}\label{o_k-dedekind}
				Every prime ideal $\mfk{P}$ in $\OK$ is maximal. Hence $\OK$ is of Krull dimension $1$ and is therefore Dedekind.
			\end{theorem}

			\begin{proof}
				Note it suffices to prove that every prime ideal $\mfk{P}$ of $\OK$ lies above some prime ideal of $\Z$, since $\Z$ has Krull dimension $1$, and the proof follows from theorem \ref{lie-above-maximal}. All we need to do is to prove that $\mfk{P} \cap \Z$ is non-zero: since the inverse image of a prime ideal is prime, we are done. For each $x \in \mfk{P}$, we have a minimal polynomial $f \in \Z[X]$ such that
				\[
				f(x) = x^n+c_{n-1}x^{n-1}+\cdots+c_0=0
				\]
				with $c_i \in \Z$ and $c_0 \ne 0$. It follows that
				\[
				c_0=-(x^n+c_{n-1}x^{n-1}+\cdots+c_1x) \in \mfk{P} \cap \Z,
				\]
				which is to say $\mfk{P} \cap \Z$ is indeed non-zero. This concludes the proof.
			\end{proof}
			\begin{example}
				As a classic example, consider $K=\Q(\sqrt{-5})$ and $\OK = \Z[\sqrt{-5}]$. This ring is not a unique factorial domain because we have
				\[
				6 = 2 \cdot 3 = (1-\sqrt{-5}) \cdot (1+\sqrt{-5}).
				\]
				But if we view in the sense of product of ideals, nothing goes wrong. Let $\mfk{m}$ be the maximal ideal containing $6$, then
				\[
				\mfk{m} = (2,1-\sqrt{-5})(2,1+\sqrt{-5})
				\]
				is unique. Note two ideals on the right hand side are indeed maximal (hence prime) because
				\[
				\begin{aligned}
					\frac{\Z[\sqrt{-5}]}{(2,1-\sqrt{-5})} &\cong \frac{\Z[X]/(X^2+5)}{(2,1-X,X^2+5)/(X^2+5)} \\
					&\cong \frac{\Z[X]}{(2,1-X,X^2+5)} \cong \frac{\Z_2[X]}{(1-X,X^2-1)} \cong \frac{\Z_2[X]}{(1-X)} \cong \Z_2.
				\end{aligned}
				\]
				Likewise,
				\[
				\begin{aligned}
					\frac{\Z[\sqrt{-5}]}{(3,1+\sqrt{-5})} &\cong \frac{\Z[X]/(X^2+5)}{(3,1+X,X^2+5)/(X^2+5)} \\
					&\cong \frac{\Z[X]}{(3,1+X,X^2+5)} \cong \frac{\Z_3[X]}{(1+X,X^2-1)} \cong \frac{\Z_3[X]}{(1+X)} \cong \Z_3.
				\end{aligned}
				\]
			\end{example}
	\section{Galois extensions}
		\subsection{Decomposition group and decomposition field}
			If $K$ is a Galois extension of $\Q$, then the Galois group allows us to transform amongst prime ideals in a natural way. This is because of the following theorem.
			\[
				\begin{tikzcd}
					\mathfrak{P} \arrow[rr, "\exists \sigma \in G"] &                                    & \mathfrak{Q} \\
					& \mathfrak{p} \arrow[lu] \arrow[ru] &
				\end{tikzcd}
			\]
			\begin{theorem}\label{galois-lie-above}
				Let $A$ be a ring, integrally closed in its quotient field $K$. Let $L$ be a finite Galois extension of $K$ with group $G$. Let $\mfk{p}$ be a maximal ideal of $A$, and let $\mfk{P}$, $\mfk{Q}$ be prime ideals of the integral closure of $A$ in $L$ lying above $\mfk{p}$. Then there exists $\sigma \in G$ such that $\sigma\mfk{P} = \mfk{Q}$.
			\end{theorem}
			\begin{proof}
				Suppose that $\mfk{P}=\sigma\mfk{Q}$ for all $\sigma \in G$. By the Chinese remainder theorem, we have some $x \in B$ such that
				\[
					\begin{aligned}
						x &\equiv 0 \mod \mfk{P} \\
						x &\equiv 1 \mod \sigma\mfk{Q}, \quad \forall \sigma \in G.
					\end{aligned}
				\]
				Then the norm
				\[
					N_K^L(x) = \prod_{\sigma \in G}\sigma{x}
				\]
				lies in $B \cap K = A$ since $A$ is integrally closed, and lies in $\mfk{P} \cap A = \mfk{p}=\mfk{Q} \cap A \subset \mfk{Q}$. But we also have $\sigma{x} \not\in \mfk{Q}$ for all $\sigma \in G$, hence $N_K^L(x) \not \in \mfk{Q}$, a contradiction.
			\end{proof}
			If one localise, the consideration on whether a prime ideal is maximal is not required. Besides, if $A$ is of Krull dimension $1$, then one has no need to consider as well. Since we have shown that $\OK$ is a Dedekind domain, this theorem can be applied as well. Next we show the finiteness of prime ideals lying above.

			\begin{corollary}
				Let $A$ be an integrally closed domain whose field of fraction is $K$. Let $E$ be a finite separable extension of $K$, and $B$ the integral closure of $A$ in $E$. Let $\mfk{p}$ be a maximal ideal of $A$. Then there exists only a finite number of prime ideals of $B$ lying above $\mfk{p}$.
			\end{corollary}
			\begin{proof}
				 If $E$ is Galois over $K$, then by theorem \ref{galois-lie-above}, $\sigma\mfk{P}_1 = \mfk{P}_2$ for some $\sigma \in \gal(E/K)$. Suppose $\mfk{P}_1|\mfk{p}$, then the set of prime ideals lying above $\mfk{p}$ is contained in the set
				\[
					\{\mfk{Q} \subset B: \mfk{Q}=\sigma\mfk{P}_1,\sigma\in\gal(E/K)\},
				\]
				hence is finite because $\gal(E/K)$ is finite. If $E$ is not necessarily Galois, we can pick the smallest Galois extension $L/K$ containing $E$, which is a finite extension as well. Let $C$ be the integral closure of $A$ in $L$. Suppose $\mfk{P},\mfk{Q} \in \spec(B)$ are two distinct prime ideals lying above $\mfk{p}$, and $\mfk{P}',\mfk{Q}' \in \spec(C)$ lying above $\mfk{P}$ and $\mfk{Q}$ respectively. Note $\mfk{P}' \ne \mfk{Q}'$ because if not then $\mfk{P}=\mfk{Q}$, a contradiction. Therefore the distinct prime ideals of $B$ lying above $\mfk{p}$ are less than the distinct prime ideals of $C$ lying above $\mfk{p}$, which proves our assertion.
				\[
					\begin{tikzcd}
						\mathfrak{P}'          &                                    & \mathfrak{Q}'          & C                   \\
						\mathfrak{P} \arrow[u] &                                    & \mathfrak{Q} \arrow[u] & B \arrow[u, dashed] \\
						& \mathfrak{p} \arrow[lu] \arrow[ru] &                        & A \arrow[u, dashed]
					\end{tikzcd}
				\]
			\end{proof}
		% TODO: ADD SOME EXAMPLES
		\subsection{Inertia group and field}
		\subsection{Automorphisms}
	\section{Dedekind domain}

		\subsection{Ramification index}

		\subsection{Norm}

		\subsection{Discrete valuation rings}
			A \textbf{discrete valuation ring} can be considered as a localisation of Dedekind domain. Indeed, if $A$ is a discrete valuation ring, then $A$ is Noetherian and of Krull dimension $1$, and is integrally closed, hence Dedekind. If $A$ is local and Dedekind, then $A$ is a discrete valuation ring. In general, a Noetherian domain $A$ of Krull dimension one is Dedekind if and only if the localisation $A_\mfk{p}$ is a discrete valuation ring for all prime $\mfk{p}$. With respect to localisation we have a natural result:
			\begin{theorem}
				Let $A$ be a Dedekind ring and $M,N$ two modules over $A$. If $M_\mfk{p} \subset N_\mfk{p}$ for all prime $\mfk{p}$, then $M \subset N$.
			\end{theorem}
			\begin{proof}
				Let $a \in M$. For each $\mfk{p}$ we can find $x_\mfk{p} \in N$ and $s_\mfk{p} \in A \setminus \mfk{p}$ such that $a = x_\mfk{p}/s_\mfk{p}$. Let $\mfk{b}$ be the ideal generated by the $s_\mfk{p}$, ranging through all $\mfk{p} \in \spec(A)$. Then $\mfk{b}$ is the unit ideal $A$, and we can write
				\[
					1 = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}s_\mfk{p}
				\]
				with elements $y_\mfk{p} \in A$ all but a finite number of which are $0$. This yields
				\[
					a = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}s_\mfk{p}a = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}x_\mfk{p} \in N
				\]
				as desired.
			\end{proof}

			Now we study torsion-free modules over a discrete valuation ring. If $A$ is a discrete valuation ring, then in particular, $A$ is a principal ideal ring, and any finitely generated torsion-free module $M$ over $A$ is free. If its rank is $n$, and if $\mfk{p}$ is the maximal idea, then $M/\mfk{p}M$ is a free module of rank $n$. Further, we have
			\begin{theorem}
				Let $A$ be a local ring and $M$ a free module of rank $n$ over $A$. Let $\mfk{p}$ be the maximal ideal of $A$. Then $M/\mfk{p}M$ is a vector space of dimension $n$ over $A/\mfk{p}$.
			\end{theorem}
			\begin{proof}
				Let $\{x_1,\dots,x_n\}$ be a basis of $M$ over $A$, then
				\[
					M \cong \bigoplus_{i}Ax_i
				\]
				and
				\[
					M/\mfk{p}M \cong \bigoplus_{i}(A/\mfk{p})\overline{x}_i
				\]
				where $\overline{x}_i$ is the residue class of $x_i$ mod $\mfk{p}$.
			\end{proof}


\section{Moment and Center of Mass}\label{sec:MomentCenterMass}

Using a single integral we were able to compute the center of mass for
a one-dimensional object with variable density, and a two dimensional
object with constant density. With a double integral we can handle two
dimensions and variable density.

Just as before, the coordinates of the center of mass are
\[\bar x={M_y\over M} \qquad \bar y={M_x\over M},\]
where $M$ is the total mass, $M_y$ is the moment around the $y$-axis,
and $M_x$ is the moment around the $x$-axis. (You may want to review
the concepts in Section~\ref{sec:centerofmass}.)\index{moment}\index{center of mass}

The key to the computation, just as before, is the approximation of
mass. In the two-dimensional case, we treat density $\sigma$ as mass
per square area, so when density is constant, mass is
$(\hbox{density})(\hbox{area})$. If we have a two-dimensional region
with varying density given by $\sigma(x,y)$, and we divide the region
into small subregions with area $\Delta A$, then the mass of one
subregion is approximately $\sigma(x_i,y_j)\Delta A$, the total mass
is approximately the sum of many of these,
and as usual the sum
turns into an integral in the limit:
$$M=\int_{x_0}^{x_1}\int_{y_0}^{y_1} \sigma(x,y)\,dy\,dx,$$
and similarly for computations in cylindrical coordinates.
Then as before
\begin{align*}
M_x &= \int_{x_0}^{x_1}\int_{y_0}^{y_1} y\sigma(x,y)\,dy\,dx	\\
M_y &= \int_{x_0}^{x_1}\int_{y_0}^{y_1} x\sigma(x,y)\,dy\,dx.
\end{align*}

\begin{example}{Center of Mass of Uniform Plate}{CenterMassUniformPlate}
Find the center of mass of a thin, uniform plate whose shape
is the region between $y=\cos x$ and the $x$-axis between $x=-\pi/2$
and $x=\pi/2$.
\end{example}
\begin{solution}
Since the density is constant, we may take
$\sigma(x,y)=1$.

It is clear that $\bar x=0$, but for practice let's
compute it anyway. First we compute the mass:
\[
M=\int_{-\pi/2}^{\pi/2} \int_0^{\cos x} 1\,dy\,dx
=\int_{-\pi/2}^{\pi/2} \cos x\,dx
=\left.\sin x\right|_{-\pi/2}^{\pi/2}=2.
\]
Next,
\[
M_x=\int_{-\pi/2}^{\pi/2} \int_0^{\cos x} y\,dy\,dx
=\int_{-\pi/2}^{\pi/2} {1\over2}\cos^2 x\,dx={\pi\over4}.
\]
Finally,
\[
M_y=\int_{-\pi/2}^{\pi/2} \int_0^{\cos x} x\,dy\,dx
=\int_{-\pi/2}^{\pi/2} x\cos x\,dx=0.
\]
So $\bar x=0$ as expected, and $\bar y=\pi/4/2=\pi/8$.
This is the same problem as in Example~\ref{exa:centerofmassundercos};
it may be helpful to compare the two solutions.
\end{solution}

\begin{example}{Center of Mass of 2-D Plate}{CenterMass2DPlate}
Find the center of mass of a two-dimensional plate
that occupies the quarter circle $x^2+y^2\le1$ in the
first quadrant and has density
$k(x^2+y^2)$.
\end{example}
\begin{solution}
It seems clear that because of the symmetry of both the
region and the density function (both are important!), $\bar x=\bar y$.
We'll do both to check our work.

Jumping right in:
\[
M=\int_0^1 \int_0^{\sqrt{1-x^2}} k(x^2+y^2)\,dy\,dx
=k\int_0^1 x^2\sqrt{1-x^2}+{(1-x^2)^{3/2}\over3}\,dx.
\]
This integral is something we can do, but it's a bit unpleasant. Since
everything in sight is related to a circle, let's back up and try
polar coordinates. Then $x^2+y^2=r^2$ and
\[M=\int_0^{\pi/2} \int_0^{1} k(r^2)\,r\,dr\,d\theta
=k\int_0^{\pi/2}\left.{r^4\over4}\right|_0^1\,d\theta
=k\int_0^{\pi/2} {1\over4}\,d\theta
=k{\pi\over8}.
\]
Much better. Next, since $y=r\sin\theta$,
\[M_x=k\int_0^{\pi/2} \int_0^{1} r^4\sin\theta\,dr\,d\theta
=k\int_0^{\pi/2} {1\over5}\sin\theta\,d\theta
=k\left.-{1\over5}\cos\theta\right|_0^{\pi/2}={k\over5}.
\]
Similarly,
\[M_y=k\int_0^{\pi/2} \int_0^{1} r^4\cos\theta\,dr\,d\theta
=k\int_0^{\pi/2} {1\over5}\cos\theta\,d\theta
=k\left.{1\over5}\sin\theta\right|_0^{\pi/2}={k\over5}.
\]
Finally, $\ds\bar x = \bar y = {8\over5\pi}$.
\end{solution}

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

\begin{enumialphparenastyle}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the square $[0,1]\times[0,1]$
and has density
function $xy$.
\begin{sol}
$\bar x=\bar y=2/3$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the triangle $0\le x\le1$, $0\le y\le x$,
and has density
function $xy$.
\begin{sol}
$\bar x=4/5$, $\bar y=8/15$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the upper unit semicircle centered at $(0,0)$
and has density
function $y$.
\begin{sol}
$\bar x=0$, $\bar y=3\pi/16$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the upper unit semicircle centered at $(0,0)$
and has density
function $x^2$.
\begin{sol}
$\bar x=0$, $\bar y=16/(15\pi)$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the triangle formed by $x=2$, $y=x$, and $y=2x$
and has density
function $2x$.
\begin{sol}
$\bar x=3/2$, $\bar y=9/4$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the triangle formed by $x=0$, $y=x$, and $2x+y=6$
and has density
function $\ds x^2$.
\begin{sol}
$\bar x=6/5$, $\bar y=12/5$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two-dimensional plate
that occupies the region enclosed by the parabolas $x=y^2$, $y=x^2$
and has density
function $\ds\sqrt{x}$.
\begin{sol}
$\bar x=14/27$, $\bar y=28/55$
\end{sol}
\end{ex}

\begin{ex}
Find the centroid of the area in the first quadrant bounded by
 $x^2-8y+4=0$, $x^2=4y$, and $x=0$. (Recall that the centroid\index{centroid}
is the center of mass when the density is 1 everywhere.)
\begin{sol}
$(3/4,2/5)$
\end{sol}
\end{ex}

\begin{ex}
Find the centroid of one loop of the three-leaf rose
$r=\cos(3\theta)$.  (Recall that the centroid is the
center of mass when the density is 1 everywhere, and that the mass in
this case is the same as the area, which was the subject of
Exercise~\ref{ex:areaofthreeleafroseloop} in
Section~\ref{sec:DoubleIntegralsinPolarCoordinates}.)  The
computations of the integrals for the moments $M_x$ and $M_y$ are
elementary but quite long; Sage can help.
\begin{sol}
$\ds\left({81\sqrt3\over80\pi},0\right)$
\end{sol}
\end{ex}

\begin{ex}
Find the center of mass of a two dimensional
object that occupies the region $0\le x\le \pi$, $0\le y\le \sin x$,
with density $\sigma=1$.
\begin{sol}
$\bar x=\pi/2$, $\bar y=\pi/8$
\end{sol}
\end{ex}

\begin{ex}
A two-dimensional object has shape given by
$r=1+\cos\theta$ and density $\sigma(r,\theta)=2+\cos\theta$. Set up
the three integrals required to compute the center of mass.
\begin{sol}
$\ds M=\int_0^{2\pi} \int_0^{1+\cos\theta} (2+\cos\theta)r\,dr\,d\theta$,
\hfill\break
$\ds M_x=\int_0^{2\pi} \int_0^{1+\cos\theta} \sin\theta(2+\cos\theta)r^2\,dr\,d\theta$,
\hfill\break
$\ds M_y=\int_0^{2\pi} \int_0^{1+\cos\theta} \cos\theta(2+\cos\theta)r^2\,dr\,d\theta$.
\end{sol}
\end{ex}

\begin{ex}
A two-dimensional object has shape given by
$r=\cos\theta$ and density $\sigma(r,\theta)=r+1$. Set up
the three integrals required to compute the center of mass.
\begin{sol}
$\ds M=\int_{-\pi/2}^{\pi/2} \int_0^{\cos\theta} (r+1)r\,dr\,d\theta$,
\hfill\break
$\ds M_x=\int_{-\pi/2}^{\pi/2} \int_0^{\cos\theta} \sin\theta(r+1)r^2\,dr\,d\theta$,
\hfill\break
$\ds M_y=\int_{-\pi/2}^{\pi/2} \int_0^{\cos\theta} \cos\theta(r+1)r^2\,dr\,d\theta$.
\end{sol}
\end{ex}

\begin{ex}
A two-dimensional object sits inside $r=1+\cos\theta$
and outside $r=\cos\theta$, and has density $1$ everywhere.
Set up
the integrals required to compute the center of mass.
\begin{sol}
$\ds M= \int_{-\pi/2}^{\pi/2}\int_{\cos\theta}^{1+\cos\theta}
r\,dr\,d\theta + \int_{\pi/2}^{3\pi/2}\int_0^{1+\cos\theta}r\,dr\,d\theta$,
\hfill\break
$\ds M_x=\int_{-\pi/2}^{\pi/2}\int_{\cos\theta}^{1+\cos\theta}
r^2\sin\theta\,dr\,d\theta + \int_{\pi/2}^{3\pi/2}\int_0^{1+\cos\theta}r^2\sin\theta\,dr\,d\theta$,
\hfill\break
$\ds M_y=\int_{-\pi/2}^{\pi/2}\int_{\cos\theta}^{1+\cos\theta}
r^2\cos\theta\,dr\,d\theta + \int_{\pi/2}^{3\pi/2}\int_0^{1+\cos\theta}r^2\cos\theta\,dr\,d\theta$.
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\documentclass[11pt, oneside]{article}

\usepackage{preamble}
\addbibresource{../../shared/references.bib}

\usepackage{sets}
\usepackage{groups}
\usepackage{integers}

\title{Integers}
\author{Arthur Ryman, {\tt arthur.ryman@gmail.com}}
\date{\today}

\begin{document}

\maketitle

\begin{abstract}
This article contains Z Notation type declarations for the integers, $\num$, and some related objects.
It has been type checked by \fuzz.
\end{abstract}

\section{Introduction}

The integers, $\num$, are built-in to Z Notation.
This article provides type declarations for some related objects so that they can be used and type checked in formal Z specifications.

\section{Integers}

\subsection{$AddIntegerSequences$}

Let $l$ be a natural number and
let $x$ and $y$ be two integer sequences of length $l$.
Their sum $z = x + y$ is the integer sequence of length $l$ defined by point-wise addition of
of the terms in $x$ and $y$.
Let the schema $AddIntegerSequences$ denote this situation.

\begin{schema}{AddIntegerSequences}
	l : \nat \\
	x, y, z : \seq \num
\where
	l = \# x = \# y
\also
	z = (\lambda i : 1 \upto l @ x~i + y~i)
\end{schema}
\begin{itemize}
	\item The sequence $z$ is defined by pointwise addition of the sequences $x$ and $y$.
\end{itemize}

\subsection{$add\_int\_seq$}

Let the function $add\_int\_seq(x, y) = z$ be the sum of two equal-length integer sequences.

\begin{zed}
	add\_int\_seq == \{~ AddIntegerSequences @ (x, y) \mapsto z ~\}
\end{zed}

\subsection{\zcmd{addSeqZ}}

We introduce the notation $x \addSeqZ y = add\_int\_seq(x, y)$.

\begin{zed}
	(\_ \addSeqZ \_) == add\_int\_seq
\end{zed}

\printbibliography

\end{document}


%%%%%%%%%%%%%%%%%
% Basic Notions %
%%%%%%%%%%%%%%%%%

\section{Basic Notions and Notation}

\begin{example}{1.1}{}

    Simplest \SigmaAlgebra:

        \begin{itemize}
            \setlength{\parskip}{0em}
            \item $\{\emptyset, \Omega\}$, \emph{contained in every} \SigmaAlgebra \ on $\Omega$,
            \item Family of all subsets of $\Omega$, \emph{containing every} \SigmaAlgebra on $\Omega$.
        \end{itemize}

\end{example}

\begin{exercise}{1.1}{}

    Let $\CalF$ be a \SigmaAlgebra. Then $A_n \in \CalF$ for every integer $n \geq 1$ $\Rightarrow \bigcap_{n=1}^{\infty} A_n \in \CalF$.

\end{exercise}

\begin{proposition}{1.2}{}

    Let $P$ be a probability measure on \SigmaAlgebra\ $\CalF$. Then the following statements hold:

        \begin{enumerate}[(i)]
            \setlength{\parskip}{0em}
            \item $A, B \in \CalF$ s.t. $A \subseteq B$ $\Rightarrow$ $P(A) \leq P(B)$;
            \item For \emph{increasing} sequence $(A_n)_{n=1}^{\infty}$ we have

                \begin{align*}
                    \lim_{n \to \infty} P(A_n) = P\left(\bigcup_{n=1}^{\infty} A_n \right);
                \end{align*}
            \item For \emph{decreasing} sequence $(A_n)_{n=1}^{\infty}$ we have

                \begin{align*}
                    \lim_{n \to \infty} P(A_n) = P\left(\bigcap_{n=1}^{\infty} A_n \right).
                \end{align*}
        \end{enumerate}

\end{proposition}

\begin{proposition}{1.2}{General}

    Let $\mu$ be a measure on \SigmaAlgebra\ $\CalF$. Then the following statements hold:

        \begin{enumerate}[(i)]
            \setlength{\parskip}{0em}
            \item $A, B \in \CalF$ s.t. $A \subseteq B$ $\Rightarrow$ $\mu(A) \leq \mu(B)$;
            \item For \emph{increasing} sequence $(A_n)_{n=1}^{\infty}$ we have

                \begin{align*}
                    \lim_{n \to \infty} \mu(A_n) = \mu\left(\bigcup_{n=1}^{\infty} A_n \right);
                \end{align*}
            \item For \emph{decreasing} sequence $(A_n)_{n=1}^{\infty}$ we have

                \begin{align*}
                    \lim_{n \to \infty} \mu(A_n) = \mu\left(\bigcap_{n=1}^{\infty} A_n \right).
                \end{align*}
        \end{enumerate}

\end{proposition}

\begin{proposition}{}{Bounding Intersections}

    Let $A, B \in \CalF$. Then $\mu(A \cap B) \leq \mu(A)$.

    \Hint $\sigma$-additivity and $A = (A \cap B) \cup (A \setminus B)$.

\end{proposition}

\begin{proposition}{}{Measure of Set Difference, I}

    Let $A, B \in \CalF$, then $\mu(A \setminus B) = \mu(A) - \mu(A \cap B)$.

\end{proposition}

\begin{proposition}{}{Measure of Set Difference, II}

    Let $A, B \in \CalF$ and $B \subseteq A$, then $\mu(A \setminus B) = \mu(A) - \mu(B)$.

\end{proposition}

\begin{proposition}{}{Complement of Limit Inferior/Superior}

    Let $(A_n)_{n=1}^{\infty}$ be a sequence of sets in $\CalF$, then:

        \begin{enumerate}[(i)]
            \setlength{\parskip}{0em}
            \item
                \begin{align*}
                    \left(\liminf_{n \to \infty} A_n\right)^C = \limsup_{n \to \infty} A_n^C
                \end{align*}
            \item
                \begin{align*}
                    \left(\limsup_{n \to \infty} A_n\right)^C = \liminf_{n \to \infty} A_n^C
                \end{align*}
        \end{enumerate}

\end{proposition}

\begin{exercise}{Ws 2, 1}{Limit Inferior/Superior Properties}

       Let $(A_n)_{n=1}^{\infty}$ be a sequence of sets in $\CalF$, then:

        \begin{enumerate}[(i)]
            \setlength{\parskip}{0em}
            \item
                \begin{align*}
                    \liminf_{n \to \infty} A_n \coloneqq \bigcup_{n = 1}^{\infty}\bigcap_{k = n}^{\infty} A_k
                \end{align*}

                is the set of those $\omega$ that are \emph{in all but finitely many $A_n$}, i.e. that uphold the property $A_n$ captures for all except a finite amount of values of $n$.
            \item
                \begin{align*}
                    \limsup_{n \to \infty} A_n \coloneqq \bigcap_{n = 1}^{\infty}\bigcup_{k = n}^{\infty} A_k
                \end{align*}

                is the set of those $\omega$ that are \emph{in infinitely many $A_n$}, i.e. that uphold the property $A_n$ captures for an infinite amount of values of $n$.
        \end{enumerate}

\end{exercise}

\begin{proposition}{}{Continuous Implies Borel-Measurability}

    Let $f: \mathbb{R} \to \overline{\mathbb{R}}$ be a \emph{continuous} function. Then $f$ is Borel-measurable.

\end{proposition}

\begin{proposition}{}{Countable Sets}

    Every countable subset of $\mathbb{R}$ is Borel-measurable.

\end{proposition}


% Question  ##################################################################################################################
\section{Question 1}\label{ssec:pt1q1}
\textbf{The objective of this question is to analyse the data for different instruments, based on last year’s data, and provide your opinion in terms of their respective risk and return.}

% END Question  ##############################################################################################################

% Question (i) ###############################################################################################################

\subsection{Q1 (i)}\label{sssec:pt1q1i}
\textbf{Download daily closing price data for S\&P500, FTSE100 and Gold (SPDR) for the years 2014 to 2017.}

\noindent
Code to download the closing prices for the assets specified in this question can be found in ‘Question 1 (i)’ in the python notebook. The data was downloaded from Yahoo Finance and saved as .csv format in the following path \textit{‘src/data/part\_1’}. The files were saved as follows \textit{‘FTSE100.csv’}, \textit{‘S\&P500.csv’} and \textit{‘GOLD.csv’}. The data was then reloaded from the files and converted into pandas dataframes.

% END Question (i) ###########################################################################################################

% Question (ii) ##############################################################################################################

\subsection{Q1 (ii)}\label{sssec:pt1q1ii}
\textbf{Why log returns are normally preferred from standard arithmetic returns?}

\noindent
Log returns are preferred over arithmetic returns for several reasons, one of which is that when using log returns you are inherently normalizing all the values. The process of normalization makes the returns easier to compare with and this is very useful in analytical situations or machine learning. Another advantage is time-additivity, meaning when using log-returns it is easier to compound returns since you only need to add the values unlike when using arithmetic returns \cite{meucci2010quant}. Also, in theory prices are assumed to be distributed log normally (not always true for every price series) and transforming to log makes the values normally distributed. This is very useful in situations where it is assumed that the values are normally distributed, which is quite common in machine learning and statistics.

% END Question (ii) ##########################################################################################################

% Question (iii) #############################################################################################################

\subsection{Q1 (iii)}\label{sssec:pt1q1iii}

\textbf{Identify the first 4 distribution moments for each index/product mentioned in part (i). For your
calculations utilise daily log returns. In your answer describe the calculations/steps performed.}

\noindent
First the log returns were calculated for each index/product using the adjusted closing price. Once the log returns were computed a new column was added in each pandas dataframe called “Log Returns”, so it can be utilised when calculating the distribution moments.  A function to compute the log returns was created in the ‘fintech’ library and another function was also created to compute the four distribution moments. These functions can be shown in Fig.~\ref{fig:logretfunc} and Fig.~\ref{fig:distmomfunc}. The first and second distribution moments were calculated using numpy \cite{python:numpy} functions. The third and the forth distribution moments were calculated using scipy \cite{python:scipy}. \\

\begin{figure}[H]
\centering
  \includegraphics[scale = .65]{imgs/log_diff_func.png}
  \caption{Function to compute log returns.}
  \label{fig:logretfunc}
\end{figure}

\begin{figure}[H]
\centering
  \includegraphics[scale = .7]{imgs/dist_mom_func.png}
  \caption{Function to compute four distribution moments.}
  \label{fig:distmomfunc}
\end{figure} \\

\noindent
To further explain these functions used in the code, the following equations are presented. Eq.~\ref{eq:logdiff} is used to compute the log returns, which basically takes the natural log of the adjusted price at $t$ divided by the adjusted price at $t - 1$.

\begin{equation} \label{eq:logdiff}
    Log \ Returns = \ln(\frac{S_t}{S_{t-1}})
\end{equation}

\noindent
The first distribution moment is calculated using the Eq.~\ref{eq:1mom} which basically is the mean of the computed log returns. By finding the mean of the log returns we get a value of the expected return and it gives us the centre point under the distribution. The second distribution moment as shown in Eq.~\ref{eq:2mom} is the standard deviation of the log returns. Such value shows us the volatility of the index/product and measures the disperation in the distribution.

\noindent
The third moment is the computed Skew of the log returns and this is shown in Eq.~\ref{eq:3mom}. Skew is calculated by taking the sum of the log return $Y_i$ subtracted by the log return mean $\overline{Y}$ and raised to the power of 3. Then this summation is divided by the number of the log return values ($N$). After doing so this result is divided by the standard deviation raised to the power of 3  which is shown as $s^3$. This gives us the measure of symmetry for the distribution and show us how skewed the log returns are. Similarly the fourth moment, which is also know as Kurtosis, is calculated in the same way but raised to the power of 4 as shown in Eq.~\ref{eq:4mom}. This measures the shape of our distribution for the log returns (tails, tall or flat) and the distribution is set to be normally distributed if it is close to 3.

\begin{equation} \label{eq:1mom}
    1st \ Moment = \frac{\Sigma x}{N}
\end{equation}

\begin{equation} \label{eq:2mom}
    2nd \ Moment = \sqrt{\frac{\Sigma(x - \overline{x})^2}{N}}
\end{equation}

\begin{equation} \label{eq:3mom}
    3rd \ Moment = \frac{\sum_{i=1}^{N}(Y_i - \overline{Y})^3 / N}{s^3}
\end{equation}

\begin{equation} \label{eq:4mom}
    4th \ Moment = \frac{\sum_{i=1}^{N}(Y_i - \overline{Y})^4 / N}{s^4}
\end{equation}

\noindent
The functions described above were called from the notebook as shown in Fig.~\ref{fig:momcode} and the other Fig.~\ref{fig:momresults} shows the output of the distribution moments for each index/product.

\begin{figure}[H]
\centering
  \includegraphics[scale = .60]{imgs/moments_code.png}
  \caption{Code used in the notebook to get distribution moments.}
  \label{fig:momcode}
\end{figure}

\begin{figure}[H]
\centering
  \includegraphics[scale = .60]{imgs/moments_results.png}
  \caption{Function to compute log returns }
  \label{fig:momresults}
\end{figure}

% END Question (iii) #########################################################################################################

% Question (iv) ##############################################################################################################

\subsection{Q1 (iv)}\label{sssec:pt1q1iv}

\textbf{Comment on the measured statistics from the perspective of risk and return. In your answer
compare the results obtained.}

\noindent
As described the first moment is the expected return of an asset, while the second moment gives as the expected volatility of an asset. This means that the volatility gives as the dispersion of where the price might go. The higher the volatility the larger the swings for the price over time, which can make an asset quite risky since the price can move in an upward or downward direction. The Skew measure will help us determine the extremes of where the price might go. The more skewed an asset is the less accurate financial models will be, since most of them rely on normally distributed data. When having a positively skewed returns, means that there were frequent small losses and a few large gains, while a negatively skewed returns, means that there were frequent small gains and a few large loses. In terms of risk and reward an attractive asset would be an asset with the following traits; high expected returns, low volatility, a positive skew and a Kurtosis measure close to 3 (normal distribution).

\noindent
Looking at the S\&P500 statistics the expected returns (0.036\%) and volatility (0.7621\%) is more attractive in terms of risk and return than the FTSE100 index with expected returns (0.0134\%) and volatility (0.8815\%). This is because S\&P500 has higher expected return with less volatility making it a safer bet according to historic data. Both assets have a negative Skew and the Kurtosis measure for both assets are close to 3 with a difference of +0.120574 and –0.2278 respectively.

\noindent
On the other hand, the gold asset has an expected return (0.0062\%) and volatility (0.8785\%), which although it is less volatile than the FTSE100, the expected return is significantly lower. Unlike the other assets, the FTSE100 has a positively skewed return which is preferred over negatively skewed values. Also, the Kurtosis is close to 3 with a difference of -0.72061.

\noindent
In terms of risk and return, the most attractive asset from the results obtained is the S\&P500 index.

% END Question (iv) ##########################################################################################################

% Question (v) ###############################################################################################################

\subsection{Q1 (v)}\label{sssec:pt1q1v}

\textbf{Annualize daily return (first moment) and volatility (second moment). In your scaling process assume 250 days for the year. In your answer describe the calculations/steps performed.}

\noindent
The code for this task can be found in ‘Question 1 (v)’ in the python notebook. A function called ‘annretvol\_asset’ was created in the ‘fintech’ library to annualize the log returns and volatility. This function is shown in Fig.~\ref{fig:annasset}.

\begin{figure}[H]
\centering
  \includegraphics[scale = .65]{imgs/annualize_asset.png}
  \caption{Function to annualize returns and volatility for an asset. }
  \label{fig:annasset}
\end{figure}

\noindent
In the first line of the function the first moment and second moment are computed for the specific asset. Once these are computed the daily log return is annualized by simply using ($First \ moment \times 250$) and the volatility is annualized using  ($Second \ moment \times \sqrt{250}$). The formulas described were used since returns scale with time, while volatility scales with square root of time. The computed values are then converted to percentages and returned by the function. To calculate the annualized returns and volatility for each asset, this function was called from a loop and the daily log returns found in (iii) were passed as a parameter. Results for these two measurements are shown in Fig.~\ref{fig:annassetresult}.

\begin{figure}[H]
\centering
  \includegraphics[scale = .75]{imgs/annualize_asset_results.png}
  \caption{Annualized returns and volatility for the three assets. }
  \label{fig:annassetresult}
\end{figure}

% END Question (v) ###########################################################################################################

% Question (vi) ##############################################################################################################

\subsection{Q1 (vi)}\label{sssec:pt1q1vi}

\textbf{By considering the last closing price at the end of 2017, and the annualized volatility from question (v), what would be the price level of S&P 500 after 1 month, that according to normal probability, there is a 32\% chance that the actual price will be above it. Show your workings.}

\noindent
Code for this task can be found in ‘Question 1 (vi)’ in the python notebook. To find the price level for the asset after one month the last closing price was fetched, which was equal to \$2,673.61. After doing so the annualized volatility $(12.05\%)$ was scaled to one month using the following formula $(\frac{12.05}{100} \times \sqrt{\frac{20}{250}})$ and it was assumed that a one month period contains 20 days. Using these two values the price deviation could be computed using $(\$2,673.61 \times (\frac{12.05}{100} \times \sqrt{\frac{20}{250}}))$ which outputs \$91.12. So the price levels after one month is in the range of ($\$2,673.61 - \$91.12$) to ($\$2,673.61 + \$91.12$) which is equal to the range of \$2,582.49 - \$2,764.73.

\noindent
Using the Z-score equation as shown in Eq.~\ref{eq:zscore}, we can find the number of standard deviations a value is from the mean. In this case the mean is set to be the last closing price for the asset, which is equal to \$2,673.61. We found that the price has a 16\% chance that it will be above of \$2,764.73. To find a price which has a 32\% chance of being above the actual price, we rearrange the formula to find $X$. Using a z-score table we know that a z-score of 0.47 has 68\% area under the distribution. So, by finding this value we would be able to find a price which has 32\% chance that the actual price will be above it.

\begin{equation} \label{eq:zscore}
    Z = \frac{X - \mu}{\sigma}
\end{equation}

\noindent
The price which has 32\% chance of being above the actual price is ($0.47 \times \$91.12 + \$2,673.61$), which is equal to \$2716.44.

% END Question (vi) ##########################################################################################################

% Question (vii) #############################################################################################################

\subsection{Q1 (vii)}\label{sssec:pt1q1vii}

\textbf{Download the Google and Amazon daily prices for the last 5 years (till 31/12/2017). By utilizing a
regression model, perform the Beta-test against the S\&P 500 index. Comment on your findings.}

\noindent
Code for this task can be found in ‘Question 1 (vii)’ in the python notebook. The data was downloaded from Yahoo Finance and saved as .csv format in the following path \textit{‘src/data/part\_1’}. The files were saved as \textit{‘GOOGLE.csv’}, \textit{‘AMAZON.csv’} and \textit{‘S\&P500BETA.csv’}. The data was then reloaded from the files and converted into pandas dataframes. A new dataframe with the percentage changes (daily adjusted closing prices) for the three assets was created, with each column holding the percentage changes for each asset.

\noindent
A function called ‘beta\_test\_ols’ (uses Ordinary Least Squares from 'statsmodels' \cite{seabold2010statsmodels}) was created in the ‘fintech’ library and was utilised to perform the beta-test. Two beta-test were conducted ‘GOOGLE VS S\&P500’ as shown in Fig.~\ref{fig:beta_1} and ‘AMAZON vs S\&P500’ as shown in Fig.~\ref{fig:beta_2}. The plots for the results are shown in Fig.~\ref{fig:beta_1_plot} and Fig.~\ref{fig:beta_2_plot} respectively.

\begin{figure}[H]
     \centering
     \begin{subfigure}[b]{0.8\textwidth}
         \centering
         \includegraphics[width=\textwidth]{imgs/beta_1.png}
         \caption{GOOGLE VS S\&P500 Beta-test.}
         \label{fig:beta_1}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.8\textwidth}
         \centering
         \includegraphics[width=\textwidth]{imgs/beta_2.png}
         \caption{AMAZON vs S\&P500 Beta-test.}
         \label{fig:beta_2}
     \end{subfigure}
\end{figure}

\noindent
The beta-test (Capital Asset Pricing Model (CAPM)) is used to provide a measure which describes the risk/return ratio for the two assets. In this task we use the beta-test to test the relation of a stock price relative to a stock market index (systematic risk). The beta is calculated using Eq.~\ref{eq:beta}.

\begin{equation} \label{eq:beta}
    Market \ Return = \alpha + (\beta \times stock \ return)
\end{equation}

\noindent
Let’s look at the first result ‘GOOGLE vs S\&P500’ which has a beta of 1.0707 and a p-value which makes this measurement statistically significant. With 95\% confidence that the value lies between 0.9876 and 1.1538. Since the $\beta > 1$, this asset is volatile and moves with the rest of the market. This means that if the market moves in an upwards direction this asset is likely to move in the same direction and the same goes if the market moves in a downward direction. So as a benchmark we know that this asset is positively correlated with the market index as shown in the plot in Fig.~\ref{fig:beta_1_plot}. Looking at the confidence levels, although the beta can be less than 1 it is still a very high value and this means that this stock is likely to move with the market or follows a similar trend.

\noindent
In the other test ‘AMAZON VS S\&P500’ the beta was 1.2151 and has a p-vale which makes the measurement statistically significant. With 95\% confidence that the value lies between 1.0977 and 1.3325. Like the previous test this stock is volatile and moves with the rest of the market, since it’s correlated according to the test. One must note that the beta confidence levels and the beta is a bit higher than the previous stock which makes this asset to move a bit higher or lower than the benchmark (market-index). This means if the market is moving in an upward direction this stock will see more returns relative to the market and it will see more loses when moving in a downward direction. In fact the beta for the Google stock is close to 1 meaning it will move very similar to the market and this difference between the movements of the two assets can be seen in both the plots Fig.~\ref{fig:beta_1_plot} and Fig.~\ref{fig:beta_2_plot}.

\begin{figure}[H]
     \centering
     \begin{subfigure}[b]{0.6\textwidth}
         \centering
         \includegraphics[width=\textwidth]{imgs/beta_1_plot.png}
         \caption{GOOGLE VS S\&P500 Beta-test plot.}
         \label{fig:beta_1_plot}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.6\textwidth}
         \centering
         \includegraphics[width=\textwidth]{imgs/beta_2_plot.png}
         \caption{AMAZON vs S\&P500 Beta-test plot.}
         \label{fig:beta_2_plot}
     \end{subfigure}
\end{figure}

\noindent
We would like to add that beta-test does not detect any unsystematic risk. Since we are measuring beta for separate stocks it will give us an indication of how much risk such assets will add or subtract to a portfolio. Such measure does not always predict the stock movements, but it can be a useful indication when building a portfolio as it gives us some indication of how a stock moves with the market. It is also important to note that we used the daily values to measure the beta while it is more common to use the monthly measurements. When using the monthly data, it can faster to compute, easier to identify change in trends and can be good for long term forecasting but if you are looking at daily changes it is better to use daily data. On the other hand, daily is more optimal if you are forecasting for short to medium periods of time but data can be susceptible to noise. Choosing the time period to work with depends on the problem you are trying to solve/predict.

% END Question (vii) #########################################################################################################


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

\section*{Question 5}

A word on an alphabet A is \textit{square-free} if it contains no infix of the form $xx$, where $x \in A^+$.
\begin{enumerate}[label=(\alph*)]
	\item
	List all square-free words of length three over the alphabet \{a,b\}.

	\item
	Show that for the alphabet $\{a,b\}$ there are no square-free words of length at least equal to 4.

	\item
	Let $f:A \rightarrow A$ be a one-to-one mapping.
	Prove that if $x$ is square-free then so is $f(x)$.

\end{enumerate}

\subsection*{Solution}

\begin{enumerate}[label=(\alph*)]

	\item
	The only three-letter square-free words over the alphabet $\{a,b\}$ are $\{aba,bab\}$.

	\item
	A four-letter word is constructed by adding a symbol to the end of one of the possible three-letter combinations of which only \textit{aba} and \textit{bab} are square-free. If other three-letter combinations are chosen to construct upon, they have already failed to satisfy square-free condition. Table \ref{5tab1} shows that even if three-letter square-free words be chosen to construct upon, addition of any symbol would lead to infixes. As cases presented in table \ref{5tab1} are inclusive, it is concluded that no four-letter words over alphabet $\{a,b\}$ are square-free.

	As any word of more than four symbols contains a four-letter combination, it is proven that no words of length at least equal to four are square free.

	\begin{table}
		\centering
		\begin{tabular}{c|c|c|c}
			\textbf{initial} & \textbf{$4^{th}$ letter} & \textbf{final combination} & \textbf{infix}\\
			\hline
			aba & a & abaa & a\\
			aba & b & abab & ab\\
			bab & a & baba & ba\\
			bab & b & babb & b
		\end{tabular}
		\caption{Constructing 4-letter words from 3-letter words}\label{5tab1}
	\end{table}

	\item
	Statement is shown to be true using proof of contradiction. It is assumed that there is a $y$ that is square-free whose $f(y)$ is not. $y$ can be represented as $klmn$ where $k, l, m, n \in A^*$ and $k \neq l \neq m \neq n$. $f(y)$ can also be represented as $tuuv$ where $t, u, v \in A^*$ and $u \neq \lambda$ and $t \neq u \neq v$.

	Taking advantage of the assumption that $f:A\rightarrow A$ is one-to-one,

	\begin{equation}\label{5eq1}
	f(y) = f(k)f(l)f(m)f(n)
	\end{equation}

	Based on our assumption, it is true that

	\begin{equation}\label{5eq2}
	\exists k, l, m, n \mid y = klmn, f(k)=t, f(l)=u, f(m)=u, f(n)=v
	\end{equation}

	However \eqref{5eq2} claims that $f(l) = f(m)$ which is in contrast to our given assumption that $f:A\rightarrow A$ is one-to-one. Hence, our assumption is not valid and statement is proven to be true.

\end{enumerate}


% Studentds question collection
%# Author Alejandro Gonzalez Recuenco and SKR students
%# e-mail <alejandrogonzalezrecuenco@gmail.com>

\documentclass{exam}
%SET-UP:
\usepackage{amsmath, physics, tikz, tcolorbox, graphicx}

%! TexExamRandomizer = {"noutput":2}
%! TexExamRandomizer = {"randominfo": {"randomnumber":100000000, "switchnumber":["even", "odd", "stop"]}}
%! TexExamRandomizer = {"layercmd":["item", "(choice|CorrectChoice)"],"layernames":["itemize", "choices"]}
%! TexExamRandomizer = {"table":"TestClass.csv"}
%! TexExamRandomizer = {"extrainfo":{"Class":"class", "Roll Number":"rollnumber","Nickname":"nickname"}}

\newcommand{\randomnumber}{3}
\newcommand{\switchnumber}{hi}
\newcommand{\class}{class}
\newcommand{\rollnumber}{rollnumber}
\newcommand{\nickname}{nickname}
\newcommand\myversion{0}
\newcommand\rseed{seed}

% DOCUMENT STARTS HERE
\begin{document}

\author{\class\ --- \nickname --- \rollnumber}
\title{\textsc{Exam collection} --- mini-exam, random \randomnumber, switch \switchnumber }

\maketitle

\section{Word problems}\begin{itemize}

	\item What is the mathematical definition of derivative.

	\begin{choices}
		\choice $f'(x) = \lim_{h\to 0}$ $\frac{f(x)-f(x)}{h+x}$
		\choice $f'(x) = \lim_{h\to 0}$ $\frac{f(x)-f(h)}{x}$
		\choice $f'(x) = \lim_{h\to 0}$ $\frac{f(x+h)+f(h)}{x}$
		\CorrectChoice $f'(x) = \lim_{h\to 0}$ $\frac{f(x+h)-f(x)}{h}$
	\end{choices}
	\item What is the derivative of the function $h(x) = \frac{f(x)}{g(x)}$,

	\begin{choices}
		\choice $h'(x) = f'(g(x)) \cdot g'(x).$
		\choice $h'(x) = f'(x)g(x)-f(x)g'(x).$
		\choice $h'(x) = f'(x)g(x)+f(x)g'(x).$
		\CorrectChoice $h'(x) = \frac{f(x)g'(x)-f'(x)g(x)}{(g(x))^2}.$
	\end{choices}

	\item Which one is the correct form of the chain rule ?

	\begin{choices}
		\choice $(f(g'))(x)=f'(g(x'))g'(x)$
		\choice $(f(g'))'(x)=f'(g(x'))g'(x)$
		\choice $(f(g))'(x)=f'(g'(x'))g'(x')$
		\CorrectChoice $(f(g))'(x)=f'(g(x))g'(x)$
	\end{choices}

	\item Which of the following is NOT a type of discontinuity?

	\begin{choices}
		\choice Removable.
		\choice Infinite jump.
		\choice Finite jump.
		\CorrectChoice Endpoint.
	\end{choices}

	\item What does a `Derivative' describe?
	\begin{choices}
		\choice It describes the instantaneous change of rate of the functions at $x$ axis.
		\choice It describes the instantaneous change of rate of the functions at $y$ axis.
		\choice It describes the instantaneous change of rate of the functions at some point.
		\CorrectChoice It describes the instantaneous change of rate of the functions at every point.
	\end{choices}
	\item What is a tangent line to a curve at a point $x = a$?

	\begin{choices}
		\choice A line that crosses a  curve once.
		\choice None of the other choices are correct.
		\choice A line that crosses a curve in two points.
		\CorrectChoice A line that has the same slope as the curve at the point $x = a$.
	\end{choices}
	\item What type of graph is the derivative of $f(x)= 5x^3+6x^2+5x-1$ graph?

	\begin{choices}
		\choice Line.
		\choice Circle.
		\choice Hyperbola.
		\CorrectChoice Parabola.
	\end{choices}
	\item The derivative of a function at a point $x = a$ tells us \ldots

	\begin{choices}
		\choice The limit of the function
		\choice The integral of the function.
		\choice The average rate of change.
		\CorrectChoice The slope of a tangent line of the graph at $x = a$.
	\end{choices}

\end{itemize}

\section{Easy}

\begin{itemize}
	\item If $y=\cos5x$ find $\dv{y}{x}$.

	\begin{choices}
		\choice $\dv{y}{x} = 5\cos5x$.
		\choice $\dv{y}{x} = -2\sin5x$.
		\choice $\dv{y}{x} = 5\cos2x$.
		\CorrectChoice $\dv{y}{x} = -5\sin5x$.
	\end{choices}
	\item Given $y=2x^2-3x+5$, What is $\dv[2]{y}{x}$

	\begin{choices}
		\choice $\dv[2]{y}{x}=4x-3$
		\choice $\dv[2]{y}{x}=16x-12$
		\choice $\dv[2]{y}{x}=0$
		\CorrectChoice $\dv[2]{y}{x}=4$
	\end{choices}
	\item What is the derivative of $y=\sin (2x + 5)$

	\begin{choices}
		\choice $ \cos(2x+5)$
		\CorrectChoice $ 2\cos(2x+5)$
		\choice $ 2\cos(2x)+5$
		\choice $ -2\cos(2x-5)$
	\end{choices}

	\item If the functions $f(x)$ and $g(x)$ are continuous everywhere then, what can we say about the function $h(x) = \frac{f(x)}{g(x)}$:

	\begin{choices}
		\CorrectChoice $\frac{f(x)}{g(x)}$ is also continuous everywhere except at the zeros of $g(x)$.
		\choice $h(x) = \frac{f(x)}{g(x)}$ is also continuous everywhere.
		\choice $h(x)$ will never cross the x axis.
		\choice More information is needed to answer this question.
	\end{choices}

	\item Find the value of $\lim_{x\to 2}\frac{x-1}{x^2-x-1}$

	\begin{choices}
		\choice $0$.
		\CorrectChoice $1$.
		\choice $\infty$.
		\choice Not possible.
	\end{choices}

	\item What is the derivative of $f(x)= 3x^4+2x^3-3x-2$
	\begin{choices}
		\choice $f'(x)= 3x^7+2x^6-3x^3-2$.
		\choice $f'(x)= 12x^3+6x^2-5$.
		\choice $f'(x)= 7x^4+5x^3-4x-2$.
		\CorrectChoice $f'(x)= 12x^3+6x^2-3$.
	\end{choices}
	\item What is the derivative of $(x^{2} + 3)(5 x + 2)$ ?

	\begin{choices}
		\choice $15 x^{2} + 19 x$
		\choice $50 x^{2} + 60 x$
		\choice $2 x + 5$
		\CorrectChoice $15 x^{2} + 4 x + 15$
	\end{choices}
	\item When $f'(x) = 0$ what happens?

	\begin{choices}
		\choice $f''(x) = 1$
		\choice $f''(x) = 0 $
		\CorrectChoice The point is a critical  point
		\choice Local maximum or minimum or an inflection point.
	\end{choices}
	\item What is the derivative of $f(x) = (2x+8)^2$

	\begin{choices}
		\choice $32$
		\choice $3x+32$
		\choice $4x+32$
		\CorrectChoice $8x+32$
	\end{choices}

	\item If $f(x) =\sqrt{x^3 - 4x}$, calculate when $f(x)=0 $

	\begin{choices}
		\choice $x = 0,\,1,\,{-1}$
		\choice $x= 0, $
		\choice $x= 0,\,2 $
		\CorrectChoice$ x= 0,\,2,\,{-2} $
	\end{choices}
	\item Given $f(x) = \frac{x^3-4}{2x+2}$, then $\lim_{x\to 4} f(x) = \ldots$

	\begin{choices}
		\choice $\ldots3.$
		\choice $\ldots4.$
		\choice $\ldots5.$
		\CorrectChoice $\ldots6.$
	\end{choices}
	\item  Suppose $f(x) = x^2 + 3$  and $g(x) = x - 2$. Which of the following is  $(f-g)(x)$?

	\begin{choices}
		\choice  $(f-g)(x)=x^2 - x +1$
     	\choice  $(f-g)(x)=x^3 + 2x^2 + 3x -2$
		\choice  $(f-g)(x)=x^2 - 4x + 7$
		\CorrectChoice   $(f-g)(x)=x^2 - x + 5$
	\end{choices}

	\item Given the functions $f(x) = x^4+3$ and $g(x) = \sqrt{x}$, find the value of $(f\circ g)'(x)\ldots$

	\begin{choices}
		\choice $(f\circ g)'(x) = x$
		\CorrectChoice $(f\circ g)'(x) = 2x$
		\choice $(f\circ g)'(x) = 3x$
		\choice $(f\circ g)'(x) = 4x$
	\end{choices}
	\item Which one is the correct form of the product rule?

	\begin{choices}
		\choice $(f(x)\cdot g(x))' = f'(x)g'(x)+f'(x)g'(x)$
		\choice $(f(x)\cdot g(x))' = f'(x)g'(x)+f(x)g(x)$
		\choice $(f(x)\cdot g(x))' = f(x)g'(x)+f(x)g'(x)$
		\CorrectChoice $(f(x)\cdot g(x))' = f'(x)g(x) + f(x)g'(x)$
	\end{choices}

	\item What is the derivative of $f(x) = \sqrt{4x+5}$

	\begin{choices}
		\choice $2\sqrt{x+5}$.
		\choice $8$.
		\choice $\frac{\sqrt{4x+5}}{2}$.
		\CorrectChoice $\frac{2}{\sqrt{4x+5}}$.
	\end{choices}
	\item Calculate the derivative of $f(x) = 2x^2+3$

	\begin{choices}
		\CorrectChoice $f'(x) = 4x$
		\choice $f'(x) = 2x$
		\choice $f'(x) = 5x$
		\choice $f'(x) = 6x$
	\end{choices}
	\item Calculate the derivative  of $y=\sin(3x{^2}+1)$

	\begin{choices}
		\choice $\dv{y}{x} = \sin(3x{^2}+1)$
		\choice $\dv{y}{x} = \cos(3x{^2}+1)$
        \choice $\dv{y}{x} = 6x\sin(3x{^2}+1)$
		\CorrectChoice $\dv{y}{x} = 6x \cos(3x{^2}+1)$
	\end{choices}
	\item Find the derivative of $f(x) = \sqrt[6]{4x+4}$

	\begin{choices}
		\choice $f'(x) = 4 (4x+4)^{-1/3} $
		\choice $f'(x) = \frac{4x }{6 \sqrt[3]{4x+4}}$
		\choice $f'(x) = \frac{4 x + 4}{6 \sqrt[6]{(4x + 4)^{5}}} $
		\CorrectChoice $f'(x) = \frac{4}{6 \sqrt[6]{(4x + 4)^{5}}}$
	\end{choices}

\end{itemize}

\section{Medium}

\begin{itemize}
	\item What is the derivative of f(x)=$(1-6x^2)^4$
	\begin{choices}
		\choice $4(1-6x^2)^3$
		\choice $4x(1-6x^2)^3$
		\choice $48x(1-6x^2)^3$
		\CorrectChoice $-48x(1-6x^2)^3 y$
	\end{choices}

	\item Find the slope of the tangent line of the curve $y = \frac{1}{x}$ at the point $(3,\frac13)$

	\begin{choices}
		\choice $\frac{1}{3}$
		\choice $\frac{-1}{3}$
		\choice $\frac{1}{9}$
		\CorrectChoice $\frac{-1}{9}$
	\end{choices}

	\item Calculate the following limit: $\lim_{x \to -2} = \frac{x^3+8}{x+2}\ $

	\begin{choices}
		\choice $\infty$.
		\choice $4$.
		\choice $1$.
		\CorrectChoice $12$.
	\end{choices}
	\item  Is this function continuous or discontinuous? $f(x) = \frac{x+8}{x-4}$

	\begin{choices}
		\choice  Continuous.
		\choice Discontinuous, contains a removable discontinuities.
		\choice No other answer is correct.
		\CorrectChoice Discontinuous, contains a jump discontinuities.
	\end{choices}

	\item $\displaystyle \lim_{x\to 4}{x^{2} + 2 x - 4}$ ?

	\begin{choices}
		\choice 14
		\choice 16
		\choice 18
		\CorrectChoice 20
	\end{choices}

	\item What is the name of the functions $\frac1{\cos x}$ ?
	\begin{choices}
		\choice $\csc x$
		\choice $\cot x$
		\choice $\tan x$
		\CorrectChoice $\sec x$
	\end{choices}

	\item Find the value of $\displaystyle \lim_{x \to 6} \frac{x^2 -36}{x^3 -216}$

	\begin{choices}
		\choice $\frac{1}{6}$
		\CorrectChoice $\frac{1}{9}$
		\choice $\frac{1}{12}$
		\choice $\frac{1}{15}$
	\end{choices}
	\item What is the function $f$ whose derivative is $f'(x)=x^5 +30$

    \begin{choices}
		\choice $f(x) = 5x^4 +30x$
		\choice $f(x) = 5x^4$
		\choice $f(x) = x^6 +30x$
		\CorrectChoice $f(x) = \frac{x^6}{6} +30x$
	\end{choices}
	\item Find the derivative of $f(x) = \frac{x-1}{x+1}$

	\begin{choices}
		\choice $f'(x) = \frac{-2}{(x+1)^2}$
		\choice $f'(x) = \frac{2}{(x-1)^2}$
		\choice $f'(x) = \frac{-2}{(x-1)^2}$
		\CorrectChoice $f'(x) = \frac{2}{(x+1)^2}$
	\end{choices}

\end{itemize}

\section{Hard}

\begin{itemize}
	\item  Calculate $\lim_{x\to 1}\frac{\sin (2x^2-2)}{x^2-1}$. (Hint: L'H\^opital rule)

	\begin{choices}
		\choice   $1$
		\choice  $-1$
		\choice  The limit does not exist.
		\CorrectChoice  $2$
	\end{choices}
	\item What is the local minimum of $x^3-2x^2$ in the range where $-1<x<1$?

	\begin{choices}
		\choice  $x=0$
		\choice  $x=-1$
		\choice There is no local minimum in that range.
		\CorrectChoice  $x=\frac43$
	\end{choices}
	\item What is the derivative of $f(x) = 2x \sin x + 2 \cos x - x^{2}\cos x.$

	\begin{choices}
		\choice $f'(x) = x^{2} \sin x + 2 x \cos x -2\sin x$.
		\choice $f'(x) = x^{2}\cos x$.
		\choice $f'(x) = 2x\cos x$.
		\CorrectChoice $f'(x) = x^{2}\sin x$.
	\end{choices}

	\item What is value of the $\displaystyle \lim_{h \to 0} \frac{f(x+h) -f(x)}{h}$ tells about the function?

	\begin{choices}
		\choice\label{choice:der1} The Slope of the graph.
		\choice\label{choice:der2} The critical point of the function , when the value of the limit is 0.
		\CorrectChoice Both options, \ref{choice:der1} and \ref{choice:der2}, are correct.
		\choice None of the above.
	\end{choices}

	\item What is the derivative of $f(x) = \arctan(2x)$ ?

	\begin{choices}
		\choice $\frac{2}{1 + x^{2}}$
		\choice $\frac{2}{2 + x^{2}}$
		\choice $\frac{2}{2 + 4x^{2}}$
		\CorrectChoice $\frac{2}{1 + 4x^{2}}$
	\end{choices}
	\item Find the derivatives of f(x) = $\sin^6(x^4)$

	\begin{choices}
		\choice $6\sin^{5}(x^4)$
		\choice $24 x^{3}\sin(x^3) \sin(x^4)$
		\choice $24\sin(x^4)  \cos(x^3)$
		\CorrectChoice $24 x^{3} \sin^{5}(x^4)  \cos(x^4) $
	\end{choices}
	\item Find the derivative of $f(x)$ = $\sqrt{x^2+3x}$.

	\begin{choices}
		\choice $f'(x) = \frac{1}{2}\sqrt{2x+3}$.
		\choice $f'(x) = \frac{1}{x^2+3x}(2x+3)$.
		\choice $f'(x) = \frac{-1}{2 \sqrt{2x + 3}}$.
		\CorrectChoice $f'(x) = \frac{2x + 3}{2 \sqrt{2x + 3}}$.
	\end{choices}

\end{itemize}

\section{Graphic problems}

\begin{itemize}
	\item
	From the following graph find $\lim_{x \to -2 } f(x)$
	\par\nopagebreak
	\includegraphics[width = 6cm]{limgraph.jpg}

	\begin{choices}
		\choice $0$.
		\choice $5$.
		\choice $4$.
		\CorrectChoice undefined.
	\end{choices}

\end{itemize}

\section{Bonus, integration}

\begin{itemize}
	\item Find the area of the graph between f(x) = $x^4$ and f(x) = $(x-6)^4$

	\begin{choices}
		\choice $\frac{243}{5}$
		\choice $\frac{243}{10}$
		\choice $\frac{486}{10}$
		\CorrectChoice $\frac{486}{5}$
	\end{choices}
	\item Find the integral of $ 4x^3+3x^2 $

	\begin{choices}
		\choice $12x^2 + 6x$
		\choice $4x^2 + 3x$
		\choice $4x^4 + 3x^3 + c$
		\CorrectChoice $x^4 + x^3 +c$
	\end{choices}
	\item What is the area under the graph $y = x^3$, the line $y = 0$ and the lines $x=0$ and $x=2$?

	\begin{choices}
		\choice 8
		\choice 16
		\choice 24
		\CorrectChoice 4
	\end{choices}
	\item Which of these processes are used to calculate the area under a curve?
	\begin{choices}
		\choice First derivative
		\choice Product rule
		\choice Second derivative
		\CorrectChoice Integral
	\end{choices}

\end{itemize}

\end{document}


% !TEX root = ../master/master.tex

%% Dexter Barrows, 2016
%% dbarrows.github.io

\section{Spatial SIR}

	Spatial epidemic models provide a way to capture not just the temporal trend in an epidemic, but to also integrate spatial data and infer how the infection is spreading in both space and time. One such model we can use is a dynamic spatiotemporal SIR model.

	We wish to construct a model build upon the stochastic SIR compartment model described previously but one that consists of several connected spatial locations, each with its own set of compartments. Consider a set of locations numbered $i = 1, ..., N$, where $N$ is the number of locations. Further, let $N_i$ be the number of neighbours location $i$ has. The model is then

	\begin{equation}
		\begin{aligned}
			\frac{dS_i}{dt} & = - \left( 1 - \phi \frac{N_i}{N_i + 1} \right) \beta_i S_i I_i - \left( \frac{\phi}{N_i + 1} \right) S_i \sum_{j = 0}^{N_i} \beta_j I_j \\
			\frac{dI_i}{dt} & = \left( 1 - \phi \frac{N_i}{N_i + 1} \right) \beta_i S_i I_i + \left( \frac{\phi}{N_i + 1} \right) S_i \sum_{j = 0}^{N_i} \beta_j I_j - \gamma I \\
			\frac{dR_i}{dt} & = \gamma I,
		\end{aligned}
	\end{equation}

	Neighbours for a particular location are numbered $j = 1, ..., N_i$. We have a new parameter, $\phi \in [0,1]$, which is the degree of connectivity. If we let $\phi = 0$ we have total spatial isolation, and the dynamics reduce to the basic SIR model. If we let $\phi = 1$ then each of the neighbouring locations will have weight equivalent to the parent location.

	As before we let $\beta$ embark on a geometric random walk defined as

	\begin{equation}
		\beta_{i, t+1} = \exp \left( \log(\beta_{i, t}) + \eta (\log(\bar{\beta}) - \log(\beta_{i, t})) + \epsilon_{t} \right).
	\end{equation}

	Note that as $\beta$ is a state variable, each location has its own stochastic process driving the evolution of its $\beta$ state.

	If we imagine a circular topology in which each of $8$ locations is connected to exactly two neighbours (i.e. location $1$ is connected to locations $N$ and $2$, location $2$ is connected to locations $1$ and $3$, etc.), and we start each location with completely susceptible populations except for a handful of infected individuals in one of the locations, we obtain a plot of the outbreak progression in Figure [\ref{spatialdataplot}].

	\begin{figure}
        \centering
        \captionsetup{width=.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/dataplot.pdf}
        \caption{Evolution of a spatial epidemic in a ring topology. The outbreak was started with 5 cases in Location 2. Parameters were $\mathcal{R}_0 = 3.0$, $\gamma = 0.1$, $\eta = 0.5$, $\sigma_{err} = 0.5$, and $\phi = 0.5$. \label{spatialdataplot}}
    \end{figure}

    If we add noise to the data from Figure [\ref{spatialdataplot}], we obtain Figure [\ref{spatialdataplot2}].

    \begin{figure}
        \centering
        \captionsetup{width=.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/dataplot2.pdf}
        \caption{Evolution of a spatial epidemic as in Figure [\ref{spatialdataplot}], with added observation noise drawn from $\mathcal{N}(0,10)$. \label{spatialdataplot2}}
    \end{figure}

\section{Dewdrop Regression}

	Dewdrop regression \cite{Hsieh2008} aims to overcome the primary disadvantage suffered by methods such as the S-map or its cousin Simplex Projection: the requirement of long time series from which to build a library. Suggested by Sugihara's group in 2008, Dewdrop Regression works by stitching together shorter, related, time series, in order to give the S-map or similar methods  enough data to operate on. The underlying idea is that as long as the underlying dynamics of the time series display similar behaviour (such as potentially collapsing to the same attractor), they can be treated as part of the same overarching system.

	It is not enough to simply concatenate the shorter time series together -- several procedures must be carried out and a few caveats observed. First, as the individual time series can be or drastically differing scales and breadths, they all must be rescaled to unit mean and variance. Then the library is constructed as before with an embedding dimension $E$, but any library vectors that span any of the seams joining the time series are discarded. Further, and predictions stemming from a library vector must stay within the time series from which they originated. In this way we are allowing the ``shadow'' of of the underlying dynamics of the separate time series to infer the forecasts for segments of other time series. Once the library has been constructed, S-mapping can be carried out as previously specified.

	This procedure is especially well-suited to the spatial model we are using. While the dynamics are stochastic, they still display very similar means and variances. This means the rescaling process in Dewdrop Regression is not necessary and can be skipped. Further, the overall variation between the epidemic curves in each location is on the smaller side, meaning the S-map will have a high-quality library from which to build forecasts.

\section{Spatial Model Forecasting}

	In order to compare the forecasting efficacy of Dewdrop Regression with S-mapping against IF2 and HMC, we generated 20 independent spatial data sets up to time $T = 50$ weeks in each of $L = 10$ locations and forecasted $10$ weeks into the future. Forecasts were compared to that of the true model evolution, and the average $SSE$ for each week ahead in the forecast were computed. The number of bootstrapping trajectories used by IF2 and HMC was reduced from 200 to 50 to curtail running times.

	The results are shown in Figure [\ref{spatialsseplot}].

	\begin{figure}
        \centering
        \captionsetup{width=.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/sseplot.pdf}
        \caption{Average SSE (log scale) across each location and all trials as a function  of the number of weeks ahead in the forecast. \label{spatialsseplot}}
    \end{figure}

    The results show a clear delineation in forecast fidelity between methods. IF2 maintains an advantage regardless of how long the forecast produced. Interestingly, Dewdrop Regression with S-mapping performs almost as well as IF2, and outperforms HMC. HMC lags behind both methods by a healthy margin.

    If we examine the runtimes for each forecast framework, we obtain the data in Figure [\ref{spatialtimeplot}].

    \begin{figure}
        \centering
        \captionsetup{width=.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/timeplot.pdf}
        \caption{Runtimes for producing spatial SIR forecasts. The box shows the middle 50th percent, the bold line is the median, and the dots are outliers. \label{spatialtimeplot}}
    \end{figure}

    As before, the S-map with Dewdrop Regression runs faster than the other two methods with a huge margin. It is again hard to see exactly how large the margin is from the figure due to the scale, but we can examine the average values: the average running time for S-mapping with Dewdrop Regression was about $249$ seconds, whereas the average times for IF2 and HMC were about $29,000$ seconds and $38,800$ seconds, respectively. This is a speed-up of just over 116x over IF2 and 156x over HMC.

    Considering how well S-mapping performed with regards to forecast error, it shows a significant advantage over HMC in particular -- it outperforms it in both forecast error and running times.

    As before, we are interested in coverage. Again, a full coverage analysis would require roughly a 100-fold increase in computational complexity, but we can use the trajectories generated by IF2 and HMC to display forecast coverage across data sets, given particular weeks in the forecast.

    Figure [\ref{sirscoverage}] shows such plots for forecasts 2 and 10 weeks ahead in location 8. Location 8 was used as it lands in the middle of the cohort of locations in terms of outbreak progression. We can see that the error bars are much wider when attempting to predict further into the future. HMC is consistently underestimates the intensity of the epidemic in both forecast lengths, but produces smaller error bars for the longer forecast.

    \begin{figure}
        \centering
        \captionsetup{width=.8\linewidth}
        \includegraphics[width=0.8\textwidth]{./images/coverage.pdf}
        \caption{Coverage plots for forecast weeks 2 (top) and 10 (bottom) in location 8. Black bars are from IF2 forecast trajectories, and grey bars are from HMC trajectories. \label{sirscoverage}}
    \end{figure}