\chapter{Decomposition}
\section{Direct Decomposition}
First, see some examples of \textit{direct decomposition} of a vector space.
\begin{remind}[direct sum] Call $W_i$'s are \textbf{independent} and denote $$\bigoplus W_i = \sum W_i$$ if for every vector in $\sum W_i$ the coordinate representation of it is unique.
\end{remind}

It is obvious that $W_i$'s are \textbf{independent} iff $\mathfrak B = (\mathfrak B_i)$ is an ordered basis for $\sum W_i$ where each $\mathfrak B_i$ is one for $W_i$.

\begin{remind}[projection, or idempotent] One such that $E^2 = E$.
\end{remind}
We have $V = \ker E \oplus \operatorname{im} E.$ And $E$ is trivially diagonalizable with $$[E]_\mathfrak B = \begin{pmatrix}I & \mathbf 0 \\  \mathbf 0 &  \mathbf 0 \end{pmatrix}$$ where $\mathfrak B = (\text{basis for }\operatorname{im} E,~\text{basis for }\operatorname{ker} E).$

\begin{theorem}If $V = \bigoplus W_i$, then there exist projections $E_i$ such that:
\begin{itemize}
\item $E_i E_j = 0$ if $i\ne j$,
\item $I = \sum E_i$,
\item $\operatorname{im} E_i = W_i.$
\end{itemize}
Conversely, if there are projections $E_i$ which satisfy above two from the top and let $\operatorname{im} E_i =: W_i$, then $V = \bigoplus W_i.$
\end{theorem}
\begin{proof}($\Longrightarrow$) Take $$E_j:~\bigoplus W_i\xrightarrow[projection]{canonical} W_j.$$

($\Longleftarrow$) Obvious. (Find the unique coordinate representation of a vector.)
\end{proof}

Suppose each of $W_i$ is invariant under $T$, then $T_i = T\upharpoonright _{W_i}$ is a linear operator on $W_i$, and $$Tv = \sum T_i v_i$$ if $v = \sum v_i$ is the unique coordinate representation with $v_i \in W_i.$ We says that $T$ is the direct sum of $T_i$'s. If the basis is given by $\mathfrak B = (\mathfrak B_i)$ where each $\mathfrak B_i$ is one for $W_i$, then $[T]_{\mathfrak B}^{\mathfrak B} $ is a form of block diagonal matrix: $$[T]_{\mathfrak B}^{\mathfrak B} = \begin{pmatrix}
[T_1]_{\mathfrak B_1}^{\mathfrak B_1} & \mathbf 0 & \cdots & \mathbf 0 \\
\mathbf 0 & [T_2]_{\mathfrak B_2}^{\mathfrak B_2} & \cdots & \mathbf 0 \\
\vdots  & \vdots & \ddots & \vdots  \\
\mathbf 0  & \mathbf 0  & \cdots& [T_k]_{\mathfrak B_k}^{\mathfrak B_k} \\
\end{pmatrix}. $$ Hence for matrices, $A$ is the direct sum of $A_i$'s if $A = \operatorname{diag}(A_1,\cdots,A_k)$ where diag denotes the block diagonal.

\begin{theorem}Let $V=\bigoplus W_i$ and $E_i$'s be canonical projections. Then $W_i$'s are all invariant under $T$ iff $T$ commutes with each of $E_i$'s.
\end{theorem}
\begin{proof}($\Longrightarrow$) Let $v=\sum v_i$, then $$ E_j Tv = E_j \sum T_i v_i = E_j T_jv_j = T_j v_j =  T v_j = T E_j v.$$

($\Longleftarrow$) $$TW_i = TE_iV = E_i TV \le E_i V = W_i.$$
\end{proof}

Similar procedure can be adopted to the eigenspace decomposition $V = \bigoplus E_{\lambda_i}$:

\begin{theorem}Let $T$ be a diagonalizable operator(hence there is the eigenspace decomposition of $V$ w.r.t. $T$), then there exist projections $D_i$ such that:
\begin{itemize}
\item $T = \sum \lambda_i D_i$,
\item $I = \sum D_i$,
\item $D_i D_j = 0$ if $i\ne j$,
\item $\operatorname{im} D_i = E_{\lambda_i}.$
\end{itemize}
Conversely, if there are distinct scalars $\lambda_i$ and nonzero operators $D_i$ which satisfy above three from the top, then $T$ is diagonalizable, $\lambda_i$'s are eigenvalues, and $D_i$'s are projections satisfy $\operatorname{im} D_i = E_{\lambda_i}.$
\end{theorem}
\begin{proof}TOTALLY SAME PROCEDURE. Omit.\end{proof}

Hence, if $T = \sum \lambda_i D_i$, then for any polynomial $g$, $$g(T) = \sum g(\lambda_i)D_i.$$ And we obtain $$T^r = \left( \sum \lambda_i D_i\right)^r =  \sum \lambda_i^r D_i ,$$ since all of heterogeneous terms disappear. From this formulation, we have $$g(T) = 0 \Longleftrightarrow \forall i ~ g(\lambda_i) = 0,$$ which means $m_T(t) = \prod (t -\lambda_i).$

Note that, if $p_j(t) = \prod_{i\ne j} \frac{t-\lambda_i}{\lambda_j - \lambda_i}$, we have $p_j(\lambda_i) = \delta_{ij}$ whence $$p_j(T) =p_j \left(\sum \lambda_i D_i \right) = \sum \delta_{ij} D_i= D_j.$$ (Hence $D_j$'s not only commute with $T$ but every polynomials in $T$.)

In fact, we have $$g(t) = \sum g(\lambda_i)p_j(t).$$ Plugging $g=1$ and $g=t$,
$$1 = \sum p_i,\qquad t = \sum \lambda_i p_i.$$(Except $k=1$. In this case $T$ is trivially diagonalizable.) Evaluating $T$ and using above formulae, $$I = \sum D_i,\qquad T = \sum \lambda_i D_i.$$ Observe that if $i\ne j$, then $p|p_i p_j$ whence $D_iD_j = 0.$ And $p_i(T)\ne 0$ since $\operatorname{deg} p_i < \operatorname{deg}p.$ Applying to above theorem, we just proved the sufficient-necessary condition of diagonalizability with another method.

\section{Primary Decomposition}
It is a generalization of what we did above.
\begin{theorem}Let $T$ be a linear operator on $V$, and factorize $$m_T(t) = \prod_{i=1}^k p_i(t)^{r_i},$$where $p_i$'s are distince irreducible monic polynomials. Let $W_i = \ker p_i(T)^{r_i}$, then:
\begin{itemize}
\item $V = \bigoplus W_i$,
\item $TW_i \le W_i$,
\item letting $T_i = T\upharpoonright _{W_i}$, $m_{T_i}(t) = p_{i}(t)^{r_i}.$
\end{itemize}
\end{theorem}


\documentclass[12pt]{article}

\usepackage{natbib,amsfonts,graphics,amsmath}
\usepackage{graphicx}

\include{newcommands}

\begin{document}

	\title{Scaling the lee wave equations including rotation}

	\author{Oliver Fringer and Eric Mayer}

	\maketitle

\section{Including Rotation}
Including rotation in the nondimensional equations requires only slight modification. Because rotational effects necessarily involve a span wise direction, we must now include an equation for the span wise momentum.

We begin by positing that the background currents are in geostrophic balance
\begin{eqnarray*}
	0 &=& -\D{P_G}{x} + fV \,,\\
	0 &=& -\D{P_G}{y} - fU \,,\\
\end{eqnarray*}
where $P_G$ is a geostrophic pressure field that is decoupled from the perturbation pressure due the lee wave. It is analogous to $P$ in the irrotational equations above.

To keep the system as simple as possible, we further assume: it is in steady state; the bathymytery varies only in the $x$-direction; and rotation has a constant rate $f=\Omega sin(\bar{\phi})$, where $\Omega$ is the earth's rate of rotation, and $\bar{\phi}$ is the average lattitude of the domain. The assumption of steady state filters out inertial oscillations, and may be invalid in regions of the ocean where rotation is strong, such as the ACC ~\citep{Nikurashin2010a}. However, in regions closer to the equator, such as Palau, this assumption is quite good, as the following scaling analysis will demonstrate.  In combination, these three assumptions allow us to neglect all span wise gradients in the perturbation fields because the hill only perturbs the flow in the $x$-direction, there are no inertial oscillations to deflect the flow from its hill-perturbed state, and rotation remains constant at all locations in the domain. Thus, again making the Boussinesq approximation, the steady momentum and density transport equations that include (some representation of) rotation are given by

\begin{eqnarray*}
U \D{u'}{x} +u' \D{u'}{x} + w' \D{u'}{z} &=& -\D{p'}{x} + fv' \,,\\
U \D{v'}{x} + u' \D{v'}{x} + w' \D{v'}{z} &=& - fu' \,,\\
U \D{w'}{x} + u' \D{w'}{x} + w' \D{w'}{z} &=& -\D{p}{z} - \frac{\rho}{\rho_0} g \,,\\
U \D{\rho'}{x} + u' \D{\rho'}{x} + w' \D{\rho'}{z} &=& \frac{\rho_0 N^2}{g} w\,,
\end{eqnarray*}
where $N^2 = -g/\rho_0 \partial\rhobar/\partial z$, subject to continuity $\nabla\cdot\ub'=0$, and
the kinematic bottom boundary condition
\[
U\D{h}{x} + u' \D{h}{x} = w'\,.
\]

Nondimensionalize with
\begin{eqnarray*}
	u' &=& u_0 u^*\,,\\
	v' &=& v_0 v^*\,,\\
	w' &=& w_0 w^*\,,\\
	\rho' &=& R \rho^*\,,\\
	p' &=& P p^*\,,\\
	x &=& k^{-1} x^*\,,\\
	z &=& \delta z^*\,.
\end{eqnarray*}

Nondimensionalizing as above, with the addition of $v = u_0v*$, the $u$-momentum equation becomes (after ignoring the *)
\begin{eqnarray*}
k u_0 U \D{u}{x} + k u_0^2\ub\cdot\nabla u  &=& -k P \D{p}{x} + fu_0v\,.
\end{eqnarray*}
Again, requiring a first order balance between linear advection and pressure gives
\begin{eqnarray*}
\D{u}{x} + J\ub\cdot\nabla u  &=& \D{p}{x} +Ro^{-1} \ v \,,
\end{eqnarray*}
where $Ro =  \frac{Uk}{f}$.
Nondimensionalizing the $v$-momentum equations, we have
\begin{eqnarray*}
k u_0 U  \D{v}{x} +k u_0^2 \ub\cdot\nabla v &=& -k P\D{p}{y} - fu_0fu \,.
\end{eqnarray*}
Upon again requiring a balance of linear advection and pressure, this becomes
\begin{eqnarray*}
	\D{v}{x} + J\ub\cdot\nabla v  &=& \D{p}{y} - Ro^{-1} \ u \,.
\end{eqnarray*}
Nondimensionalizing the $w$-momentum equations with the scaling $w_0 = Uk h_0$ that we gleaned from the bottom boundary condition gives
\[
k^2 h_0 U^2 \D{w}{x} + k^2 h_0 u_0^2\ub\cdot\nabla w = -\frac{P}{\delta}\D{p}{z} - \frac{g R}{\rho_0} \rho \,.
\]
Requiring hydrostatic balance, as before, we have
\[
\epsilon^2 \left(\D{w}{x} + F\ub\cdot\nabla w\right) = -\D{p}{z} - \rho \,.
\]
And recalling $\delta = U/N$, this simplifies to
\[
\epsilon^2 \left(\D{w}{x} + F\ub\cdot\nabla w\right) = -\D{p}{z} - \rho\,.
\]
Lastly, the nondimensional density transport equation is
\[
k U R \D{\rho}{x} + k u_0 R \ub\cdot\nabla\rho = \frac{k \rho_0 h_0 N^2 U}{g} w\,.
\]
Requiring a balance between the linear advection terms, as before, results in
\[
\epsilon^2 \left(\D{w}{x} + F\ub\cdot\nabla w\right) = -\D{p}{z} - \rho\,.
\]

\bibliographystyle{elsarticle-harv}
\bibliography{bibliography}

\end{document}


	\chapter{Miscellaneous}
	\section{Some Math}
	\renewcommand{\thepage}{}
	Again, we present you a small mathematical example. Get familiar with this syntax in order to create your own formulas\footnote{\url{https://en.wikibooks.org/wiki/LaTeX/Mathematics}}.
	\begin{equation}
	\varpi_q(k,l)=\begin{cases}
	&1\quad \quad G_q(k,l)>\rho_{\text{th}}\\
	&0\quad \quad \text{otherwise}\\
	\end{cases}
	\end{equation}
	with $\rho_{\text{th}}$ defined as a constant threshold. The remixing error spreading is performed as follows:
	\begin{eqnarray}
	\hat{X}^{(i+1)}_q(k,l)&=&{\varpi}_q(k,l)\left(\tilde{{X}}^{(i)}_q(k,l)+\frac{E^{(i)}(k,l)}{\sum_{q=1}^Q{{\varpi}_q(k,l)}}\right)\nonumber\\
	\tilde{\mathbf{X}}^{(i)}&=&\mathcal{G}(\hat{X}_q^{(i)}),
	\end{eqnarray}
	where $\sum_{q=1}^Q{{\varpi}_q(k,l)}$ accounts for the overal contributions of sources in time frequency error distribution and the remixing error $E^{(i)}(k,l)$ is defined as
	\begin{equation}
	E^{(i)}(k,l)=Y(k,l)-\sum_{q=1}^{Q}{\hat{X}_q(k,l)}.
	\end{equation}

	\begin{mdframed}
		\begin{lstlisting}[caption={A new math example}]
		\begin{equation}
			\varpi_q(k,l)=\begin{cases}
			&1\quad \quad G_q(k,l)>\rho_{\text{th}}\\
			&0\quad \quad \text{otherwise}\\
			\end{cases}
		\end{equation}
		with $\rho_{\text{th}}$ defined as a constant threshold. The remixing error spreading is performed as follows:
		\begin{eqnarray}
			\hat{X}^{(i+1)}_q(k,l)&=&{\varpi}_q(k,l)\left(\tilde{{X}}^{(i)}_q(k,l)+\frac{E^{(i)}(k,l)}{\sum_{q=1}^Q{{\varpi}_q(k,l)}}\right)\nonumber\\
			\tilde{\mathbf{X}}^{(i)}&=&\mathcal{G}(\hat{X}_q^{(i)}),
		\end{eqnarray}
		where $\sum_{q=1}^Q{{\varpi}_q(k,l)}$ accounts for the overal contributions of sources in time frequency error distribution and the remixing error $E^{(i)}(k,l)$ is defined as
		\begin{equation}
			E^{(i)}(k,l)=Y(k,l)-\sum_{q=1}^{Q}{\hat{X}_q(k,l)}.
		\end{equation}
		\end{lstlisting}
		By adding a label to your equation, you will be able to refer to the equation within the text!
	\end{mdframed}

\section{Program code / listing}
Three types of source codes are supported: code snippets, code segments, and
listings of stand alone files. Snippets are placed inside paragraphs and the others as
separate paragraphs the difference is the same as between text style and display
style formulas\footnote{\url{https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings}}. In the following, we will give you a short introduction on all three code listing types.

\subsection{individual added program code}
\begin{lstlisting}[caption = {Single code}]
#include <stdio.h>
#define N 10

int main()
{
int i;

puts("Hello world!");

for (i = 0; i < N; i++)
{
	puts("LaTeX is also great for programmers!");
}

return 0;
}
\end{lstlisting}

\begin{mdframed}
	\begin{lstlisting}[caption = {Example of \emph{Single code}}]
	\begin{lstlisting}[caption = {Single code}]
	#include <stdio.h>
	#define N 10

	int main()
	{
	int i;

	puts("Hello world!");

	for (i = 0; i < N; i++)
	{
	puts("LaTeX is also great for programmers!");
	}

	return 0;
	}
	\end {lstlisting}
	\end{lstlisting}
\end{mdframed}

\subsection{Add specific code file}

\lstinputlisting[language=C, caption = {Same code but now we added the code file instead of copying the code into latex}]{code/test.c}

\begin{mdframed}
	\begin{lstlisting}[caption = {Example of \emph{Adding specific code file}}]

		\lstinputlisting[language=C, caption = {Same code but now we added the code file instead of copying the code into latex}]{code/test.c}

	\end{lstlisting}
\end{mdframed}

\subsection{Scope on specific code file}

\lstinputlisting[language=C, firstline=6, lastline=13, caption = {Scope on specific code file}]{code/test.c}

\begin{mdframed}
	\begin{lstlisting}[caption = {Example of \emph{Scope on specific code file}}]

	\lstinputlisting[language=C, firstline=6, lastline=13, caption = {Specific scope on code file}]{code/test.c}

	\end{lstlisting}
\end{mdframed}


% !TeX encoding = UTF-8
\section{Introduction}
\textit{The graph coloring problem} is one of the most famous \textit{NP-complete} problems. Given a graph $G = (V, E)$ and $k$, where $V$ is the set of vertices in the graph, $E$ is the set of edges and $k$ is the number of available colors. The problem is to assign a certain color to every vertex $v \in V$ with the constraint: there is no pair of vertices which are adjacent and  have the same color.
This problem has many applications:
\begin{itemize}
    \item[(1)] \textit{Map Coloring:} Coloring geographical maps of countries or states where no two adjacent countries can be assigned same color. Four colors are enough to color.
    \item[(2)] \textit{Sudoku:} Sudoku is a variation of graph coloring problem where every cell represents a vertex. There is an edge between two vertices if they are in same row or same column or same block.
    \item[(3)] \textit{Register Allocation:} In compiler optimization, register allocation is the process of assigning a large number of target program variables onto a small number of CPU registers. \cite{example}
\end{itemize}

\begin{definition}
A \textit{graph} is a tuple $G = (V, E)$ consisting of a nonempty set $V$ of vertices and a set of edges $E$. \cite{bondy1976graph}
\end{definition}

\begin{definition}
A \textit{vertex coloring} of a graph $G = (V, E)$ is a map $c: V \longrightarrow S$ such that $c(v) \ne c(w)$ whenever $v$ and $w$ are adjacent. The elements of the set $S$ are called the available colors. If $G$ has a $k$-COLORING, namely the size of $S$ is $k$, then we call the graph $G$ is $k$-colorable.
 \cite{diestel2010graph}
\end{definition}
\begin{claim}
2-COLORING problem can be solved in polynomial time.
\end{claim}

\begin{proof}
Suppose the given graph $G = (V, E)$ and color $c_1, c_2$.
\begin{itemize}
    \item[(1)] Randomly pick $v \in V$, color it with $c_1$.
    \item[(2)] Apply BFS starting with vertex $v$ and color its neighbors with $c_2$. The point is that for $\forall u \in V$, we should color its neighbors with the other color alternatively: assume that the color of $u$ is $c_1$, then we assign $c_2$ to its neighbors, and vice versa.
    \item[(3)] After finishing BFS, go through all vertices again to check whether there is a vertex that is assigned with same colors other than its neighbor(s). If \textit{yes}, then the graph is \textit{not} 2-colorable, \textit{otherwise} it is 2-colorable.
\end{itemize}
The running time is similar to BFS: $\mathcal{O}(|V| + |E|)$.
\end{proof}

\begin{observation}
We can check whether a graph is bipartite by coloring the graph using two colors. If a given graph is 2-colorable, then it is bipartite.
\end{observation}

\begin{observation}
$k$-COLORING problems are $NP-complete$ for $k \geq 3$.
\end{observation}

As previously stated, the k-COLORING problem is NP-complete, which means that it seems hardly possible to have a polynomial time algorithm for this problem. Moving on now to consider 3-COLORING problem under circumstance that there is no triangle within the given graph $G$. Grötzsch’s theorem \cite{grotzsch1959dreifarbensatz} states that each triangle-free planar graph is 3-colorable. Thomassen \cite{Thomassen1994Grtzschs3T} has also found two proofs and extended the result in various way, by which a quadratic algorithm for finding suitable 3-coloring can be developed possibly. Kowalik \cite{article} maintains a complex data structure called \textit{Short Path Data Structure (SPDS)}, which will be built in linear time and enables that finding shortest paths of length at most 2 in planar graph takes $\mathcal{O}(1)$ time. The SPDS will be constantly updated during the particular sequence of operations. And its running time for finding 3-COLORING is $\mathcal{O}(n\log{}n)$. After that, Dvorak, Kawaravayashi and Thomas \cite{dvorak2013threecoloring} have designed a linear-time algorithm which still relies on the Grötzsch’s theorem but avoids complex data structures. Nevertheless, their paper is quite complicated for readers. So this thesis will give a deeper and detailed view into their paper for better understanding of their main ideas and proofs. \\

In the second section, we will give some needed definitions and give an overview of this linear-time algorithm. In the third section, we will introduce a attribute called safety with which it's possible to reduce the size of graph. Then in the fourth section, we will give a short proof of Grötzsch's theorem by two lemmas. Next, we will give an overview how a naive algorithm for three-coloring is implemented and show some improvements. In the last two sections, the way to have a linear-time algorithm will be given and its correctness will be also proven.


\section{NP-Complete}

%%%%%%%%%%
\begin{frame}{NP-hard}
  \begin{definition}[NP-hard]
    $B$ is NP-hard if
    \[
      \forall A \in \text{NP}: A \le_{P} B.
    \]
  \end{definition}

  \begin{alertblock}{Remark.}
    NP-hard problems are at least as hard as any problem in NP.
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{NP-complete}
  \begin{definition}[NP-complete]
    $B$ is NP-complete if:
    \begin{enumerate}
      \item $B \in \text{NP}$
      \item $B$ is NP-hard.
    \end{enumerate}
  \end{definition}

  \begin{alertblock}{Remark.}
    NP-complete problems are the hardest problems in NP.
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{NP-complete}
  \begin{theorem}
    If $B$ is NP-complete and $B \in \text{P}$, then $\text{P} = \text{NP}$.
  \end{theorem}

  \begin{proof}
    \begin{enumerate}
      \item $\text{P} \subseteq \text{NP}$
	\begin{itemize}
	  \item already proved
	\end{itemize}
      \item $\text{NP} \subseteq \text{P}$:
        \begin{itemize}
	  \item $\forall A \in \text{NP}, A \le_{P} B \land B \in \text{P} \Rightarrow A \in \text{P}$
	\end{itemize}
    \end{enumerate}
  \end{proof}
\end{frame}
%%%%%%%%%%
\begin{frame}{NP-complete}
  \begin{theorem}
    If $B$ is NP-complete and $B \le_{P} C \in \text{NP}$, then $C$ is NP-complete.
  \end{theorem}

  \begin{proof}
    \begin{enumerate}
      \item $C \in \text{NP}$
      \item $C$ is NP-hard:
	\begin{itemize}
	  \item $\forall A \in \text{NP}, A \le_{P} B \le_{P} C \Rightarrow A \le_{P} C$.
	\end{itemize}
    \end{enumerate}
  \end{proof}
\end{frame}
%%%%%%%%%%
\begin{frame}{Proof of being in NP-complete}
  \begin{exampleblock}{To prove $C$ is NP-complete.}
    \begin{enumerate}
      \item $C \in \text{NP}$:
	\begin{itemize}
	  \item non-deterministic polynomial algorithm
	\end{itemize}
      \item $C$ is NP-hard:
	\begin{itemize}
	  \item choose a known NP-complete problem $B$
	  \item prove $B \le_{P} C$
	\end{itemize}
    \end{enumerate}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{NP-complete problem}
  \begin{exampleblock}{The first known NP-complete problem.}
    SAT (circuit satisfiablity) is NP-complete.
  \end{exampleblock}

  \fignocaption{width = 0.40\textwidth, angle = -90}{fig/karp21.png}
\end{frame}
%%%%%%%%%%
\begin{frame}
  \fignocaption{width = 1.0\textwidth}{fig/npc-graph.png}

  \begin{center}
    \url{http://adriann.github.io/npc/npc.html}
  \end{center}
\end{frame}
%%%%%%%%%%


\section{Simple linear regression}

\subsection{Model}
\begin{equation}
\label{eq:simple_linear_model}
Y = \theta_0 + \theta_1 * X
\end{equation}

\subsection{Data}
\begin{figure}[!ht]
  \includegraphics[width=\textwidth,height=0.4\textheight,keepaspectratio]{output_1/scatter_plot.png}
  \caption{Scatter plot of data}
  \label{fig:scatter_plot}
\end{figure}

% For alpha=0.01
\subsection{For $\alpha$ = 0.01}
\subsubsection{Initial values of parameters}
\begin{equation}
\theta_0 = 0.8962072600475405
\end{equation}
\begin{equation}
\theta_1 = 0.2669940858224631
\end{equation}

\subsubsection{Final values of parameters}
\begin{equation}
\theta_0 = 1.0600275383405036
\end{equation}
\begin{equation}
\theta_1 = 1.9933270664119627
\end{equation}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth,height=0.4\textheight,keepaspectratio]{output_1/regression_line_0_01.png}
    \caption{Regression line for $\alpha$ = 0.01}
    \label{fig:regression_line_alpha_0_01}
\end{figure}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth,height=0.4\textheight, keepaspectratio]{output_1/cost_function_alpha_0_01.png}
    \caption{Mean squared error for $\alpha$ = 0.01}
    \label{fig:mean_square_alpha_0_01}
\end{figure}

% For alpha=0.1
\subsection{For $\alpha$ = 0.1}
\subsubsection{Initial values of parameters}
\begin{equation}
\theta_0 = 0.6463591916414174
\end{equation}
\begin{equation}
\theta_1 = 0.6158535662289891
\end{equation}

\subsubsection{Final values of parameters}
\begin{equation}
\theta_0 = 1.0599999999999916
\end{equation}
\begin{equation}
\theta_1 = 1.9933333333333352
\end{equation}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth,height=0.4\textheight]{output_1/regression_line_0_1.png}
    \caption{Regression line for $\alpha$ = 0.1}
    \label{fig:regression_line_alpha_0_1}
\end{figure}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth,height=0.4\textheight]{output_1/cost_function_alpha_0_1.png}
    \caption{Mean squared error for $\alpha$ = 0.1}
    \label{fig:mean_square_alpha_0_1}
\end{figure}

% For alpha=1
\subsection{For $\alpha$ = 1.0}
\subsubsection{Initial values of parameters}
\begin{equation}
\theta_0 = 0.5560688289301584
\end{equation}
\begin{equation}
\theta_1 = 0.9611765272334949
\end{equation}

\subsubsection{Final values of parameters}
\begin{equation}
\theta_0 = 1.0295053205606435e+155
\end{equation}
\begin{equation}
\theta_1 = 4.523890821517764e+155
\end{equation}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth,height=.4\textheight]{output_1/regression_line_1_0.png}
    \caption{Regression line for $\alpha$ = 1.0}
    \label{fig:regression_line_alpha_1_0}
\end{figure}

\begin{figure}[!ht]
    \includegraphics[width=\textwidth, height=.4\textheight]{output_1/cost_function_alpha_1_0.png}
    \caption{Mean squared error for $\alpha$ = 1.0}
    \label{fig:mean_squared_error_alpha_1_0}
\end{figure}

\subsection{Observation}
The scatter plot of data in figure \ref{fig:scatter_plot} shows that there is a linear correlation between the predictor variable X and prediction variable Y. The equation \ref{eq:simple_linear_model} was used as a model for the linear regression. Using different values for $\alpha$ resulted in different cost value graph and regression line.

For $\alpha$ = 0.01 and $\alpha$ = 0.1 the regression algorithm converged with decreasing cost function as shown in figure \ref{fig:mean_square_alpha_0_01} and figure \ref{fig:mean_square_alpha_0_1} respectively. The regression obtained for $\alpha$ = 0.01 shown in figure \ref{fig:regression_line_alpha_0_01} shows that the regression line properly fits the data. Also, for $\alpha$ = 0.1 a similar regression line is obtained as shown in figure \ref{fig:regression_line_alpha_0_1}.

For $\alpha$ = 1, the gradient descent algorithm started to overshoot which resulted in increasing cost function as shown in figure \ref{fig:mean_squared_error_alpha_1_0}. The cost function continued to increase until a maximum limit was reached after which it was terminated. The resulting regression line was far from being an optimal one so was a poor fit for the data which is shown by figure \ref{fig:regression_line_alpha_1_0}.

\subsection{Conclusion}
Our observations obtained by varying the learning rate($\alpha$) shows that the learning rate has to carefully chosen for gradient descent step in linear regression algorithm. A higher learning rate may result in faster convergence but it may result in oscillation and overshooting which are not desired.
\subsection{Source Code}

\lstinputlisting[language=python]{task_1.py}


\section{Building an Adding Machine}
All of these little units, experiments, and instruction have been moving towards a goal -- to make a primitive electronic calculator. Here, we will be able to add two four-bit numbers using only full adders, toggle switches, and LED blocks, all wired together on a breadboard. At the end of this unit, you will be able to enter two numbers in binary using toggle switches, and read the result on an LED readout, also in binary. There's no reason you have to stop at 4 bits -- the 8-LED block means you could add two 7-bit numbers if you have seven full adders and more toggle switches, without \emph{overflowing} the capabilities of the setup. An ``overflow" is a type of error, and means the answer is not correctly displayed on the LED output bar. Either you have tried to put numbers into the system that cannot be represented by the computer because they are too large, or else the result of the two numbers is too large to display.

Anyway, let's build a calculator!

\subsection*{Wiring up the adders}

Put two sets of toggle switches on a breadboard and enough LEDs to display the sum (that is, at least 4 LEDs). Wire up power and ground to the switches and LEDs. If you're using your own LEDs, be sure and run a 220$\Omega$ or 330$\Omega$ resistor from the negative terminal of each LED to ground. Wire the switch outputs to the LED inputs, moving from right to left, because the rightmost digit is the smallest number value in our example, either 0 or 1.

It may be helpful to use one color LED for each bank of switches, and a different color for the ``result" display. The 4-toggle LED boards included in this workshop have built-in LEDs to make it easy.

Run an additional wire from each switch output, in order, to the input of a full adder. Wire up each ``carry-out'' from one full adder to the next (leftward!) ``carry-in'' of the next full adder. The left-most carry-out digit should run to another LED, perhaps an LED of a different color, to indicate an overflow.

Next, run each adder ``sum" wire to each output LED's positive terminal. In doing this you will have wired up each column of output, meaning you can read off the sum of the two numbers in binary.

\begin{figure}
\includegraphics[scale=0.15]{4bitadder.jpg}
\caption{A properly wired 4-bit adder, with a carryout. This setup can count to 30. Here, it is adding ($8 + 4 + 0 + 1$) and ($8 + 0 + 2 + 1$), and the result is 24. The input values are color coded in pairs, so the 1s are yellow, the 2s are orange, the 4s are red, and the 8s are white. The carry-out and carry-in jumpers are blue, and the sum lines (that go to the LEDs) are green.}

\end{figure}

% Subtracting is adding the two's complement (one's complement + 1 );
% so, to subtract, you'll need a vector of NOT gates to get the one's complement,
% plus a trip through the adder circuit for both numbers
% (and someplace to store the result!) before you can implement subtraction.
% Probably only suitable an an appendix to this document because of the
% additional "moving parts" and the memory requirement (8 latches or SRAM cells isn't
% that much work, but scope creep is already a problem with this project).


\documentclass[a4paper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}

\title{\textbf{COMP90056 Assignment A}}
\author{Tingsheng (Tinson) Lai (731319)}
\date{\today}

\begin{document}
    \maketitle
    \section{Part 1}
        To formulate the problem, let $y$ be an arbitrary element of the universe, $U$. The false positive rate can be expressed as a probability
        $$Pr \left[ y \in U \backslash S \land \lor_{x \in S} \left( h(x) = h(y) \right) \right]$$
        By Kolmogorov definition, it can be further decomposed into the product of two probabilities
        $$Pr \left[ \lor_{x \in S} \left( h(x) = h(y) \right) | y \in U \backslash S \right] \cdot Pr \left[ y \in U \backslash S \right]$$
        For the latter probability it is pretty simple, which is saying to draw an arbitrary item from the universe $U$ but it should not be an element of the subset $S$. So it is simply
        $$Pr \left[ y \in U \backslash S \right] = \frac{n - m}{n}$$
        Based on the universality property of 2-universal hash family, we know that
        $$\forall x, y \in U \ x \neq y \Rightarrow Pr \left[ h(x) = h(y) \right] \leq \frac{1}{r}$$
        Draw an arbitrary $y$ outside of the subset $S$ but within the universe $U$, and let $c = h(y)$. By the property above, we can get that
        $$\forall x \in S \  Pr \left[ h(x) = c \right] \leq \frac{1}{r}$$
        Thus, after $m$ updates, the probability of collision, by the assumption of independence, raises to
        $$Pr \left[ \lor_{x \in S} \left( h(x) = c \right) \right] = \sum_{x \in S} Pr \left[ h(x) = c \right] \leq \frac{m}{r}$$
        In another word, it is an conditional probability which is
        $$Pr \left[ \lor_{x \in S} \left( h(x) = h(y) \right) | y \in U \backslash S \right] \leq \frac{m}{r}$$
        Thus, the probability of false positive is
        $$Pr \left[ \lor_{x \in S} \left( h(x) = h(y) \right) \land y \in U \backslash S \right] = Pr \left[ \lor_{x \in S} \left( h(x) = h(y) \right) | y \in U \backslash S \right] \cdot Pr \left[ y \in U \backslash S \right] \leq \frac{(n - m)m}{r n}$$
        Introducing the parameter $\epsilon$, we get
        \begin{equation*}
            \begin{split}
                \frac{(n - m)m}{r n} & \leq \epsilon \\
                r & \geq \frac{(n - m)m}{n \epsilon}
            \end{split}
        \end{equation*}
\end{document}


\documentclass[letterpaper, twoside, 12pt]{book}
\usepackage{packet}

\begin{document}

\setcounter{chapter}{1}

\chapter{Part 2.1: Sections 13.3-13.4}

\setcounter{chapter}{13}
\setcounter{section}{2}

\section{Arc Length and Curvature}

          \begin{problem}
            Let $\vect{r}(t)=\<6t, t^3, 3t^2\>$. Use the lengths of
            the line segments
            connecting $\vect{r}(0)$, $\vect{r}(1)$, $\vect{r}(2)$,
            and $\vect{r}(3)$ to approximate the length of the curve
            from $t=0$ to $t=3$.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
Let $\harpvec{r}(t) = \<f(t),g(t),h(t)\>$ be a vector function.
Then the \textbf{arclength} or \textbf{length} of the curve given by
$\harpvec{r}(t)$ from $t=a$ to $t=b$ is
\[
  L
    =
  \int_a^b
  \left|
    \lim_{\Delta{t}\to0}
    \frac{\vect{r}(t+\Delta{t})-\vect{r}(t)}{\Delta{t}}
  \right|
  \dvar{t}
  =
  \int_a^b |\vect{r}'(t)| \dvar{t}
\]
\end{definition}

          \begin{problem}
            Find the length of the curve given by
            $\vect{r}(t)=\<6t, t^3, 3t^2\>$
            from $t=0$ to $t=3$.
            (Hint: $9t^4+36t^2+36$ is a perfect square polynomial.)
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
Let $s(t)$ be the \textbf{arclength function/parameter} representing the
length of a curve from the point given by
$\harpvec{r}(0)$ to the point given by $\harpvec{r}(t)$.
(Assume $s(t)<0$ for $t<0$.)
\end{definition}

\begin{theorem}
The arclength function $s(t)$ is given by the definite integral
\[
  s(t)
    =
  \int_0^t |\vect{r}'(\tau)| \dvar{\tau}
\]
\end{theorem}

\begin{theorem}
The derivative of the arclength function gives the lengths of
the tangent vectors given by the derivative of the position function:
\[
  \frac{ds}{dt} = \left|\frac{d\vect{r}}{dt}\right|
\]
\end{theorem}

          \begin{problem}
            Compute $s(t)$ for $\vect{r}(t)=\<6t, t^3, 3t^2\>$,
            and use it to find the arclength parameter corresponding
            to $t=-2$.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Find the length of an arc of the circular helix with
            vector equation
            $\vect{r}(t) = \<\cos(t),\sin(t),t\>$
            from $(1,0,0)$ to $(1,0,2\pi)$.
          \end{problem}

\begin{definition}
  The \textbf{unit tangent vector} $\vect{T}$ to a curve $\vect{r}$ is the
  direction of the derivative $\vect{r}'(t)=\frac{d\vect{r}}{dt}$.
\end{definition}

\begin{theorem}
  \[
    \vect{T} = \frac{d\vect{r}/dt}{|d\vect{r}/dt|} = \frac{d\vect{r}}{ds}
  \]
\end{theorem}

          \begin{problem}
            Find the unit tangent vector to the curve given by
            $\vect{r}(t)=\<3t^2,2t\>$ at the point where $t=-3$.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
  The \textbf{curvature} $\kappa$ of a curve $C$ at a given point is
  the magnitude of the rate of change of $\vect{T}$ with respect to
  arclength $s$.
\end{definition}

\begin{theorem}
  \[
    \kappa
      =
    \left|
    \frac{d\vect{T}}{ds}
    \right|
      =
    \left|
    \frac{1}{ds/dt}
    \frac{d\vect{T}}{dt}
    \right|
      =
    \frac{1}{|d\vect{r}/dt|}
    \left|
      \frac{d\vect{T}}{dt}
    \right|
  \]
\end{theorem}

\begin{theorem}
  An alternate formula for curvature is given by
  \[
    \kappa =
    \frac{|\vect{r}'(t)\times\vect{r}''(t)|}{|\vect{r}'(t)|^3}
  \]
\end{theorem}

          \begin{problem}
            Prove that the helix given by the vector equation
            $\vect{r}(t) = \<\cos(t),\sin(t),t\>$
            has constant curvature.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            (OPTIONAL)
            Prove that the alternate formula for curvature is
            accurate by showing
            \[
              \frac{1}{|d\vect{r}/dt|}
              \left|
                \frac{d\vect{T}}{dt}
              \right|
                =
              \frac{|\vect{r}'\times\vect{r}''|}{|\vect{r}'|^3}
            \]
            (Some of the solution has been provided.)
          \end{problem}

          \begin{solution}
            Begin by observing that
            $
              \vect{r}'
                =
              \left|\frac{d\vect{r}}{dt}\right|\vect{T}
                =
              \frac{ds}{dt}\vect{T}
            $, and by the product rule it follows that
            $
              \vect{r}''
                =
              \frac{d^2s}{dt^2}\vect{T} + \frac{ds}{dt}\vect{T}'
            $.

            (...)

            % (Continue this argument by taking the cross-product of
            % $\vect{r}'$ and $\vect{r}''$, simplifying by using the fact that
            % $\vect{v}\times\vect{v}=\vect{0}$, then taking its magnitude
            % and simplifying using the fact that $|\vect{T}|=1$ and
            % $\vect{T},\vect{T}'$ are perpendicular (why?).
            % You should end up with
            % $\left(\frac{ds}{dt}\right)^2\left|\vect{T}'\right|$, which can
            % be used with $\frac{ds}{dt}=|\vect{r}'|$ to finish the proof.)
          \end{solution}

\begin{definition}
  The \textbf{unit normal vector} $\vect{N}$ to a curve $\vect{r}$ is the
  direction of the derivative of the unit tangent vector
  $\vect{T}'(t)=\frac{d\vect{T}}{dt}$.
  (By definition, this vector points into the direction of the curve.)
\end{definition}

\begin{theorem}
  \[
    \vect{N} = \frac{\vect{T}'}{|\vect{T}'|}
  \]
\end{theorem}

          \begin{problem}
            Prove that $\vect{N}$ actually is normal to the curve by
            using a theorem from a previous section. (Hint: $|\vect{T}|=1$.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Plot the curve given by $\vect{r}(t)=\<\cos(2t),\sin(2t)\>$,
            along with $\vect{T},\vect{N}$ at the point where
            $t=\frac{\pi}{2}$.
          \end{problem}

          \begin{problem}
            Give formuals for $\vect{T},\vect{N}$ in terms of $t$ for
            the vector function
            \[\vect{r}(t) = \< \sqrt{2}\sin t,2\cos t,\sqrt{2}\sin t \>\]
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
  The \textbf{binormal vector} $\harpvec{B}$ is the direction
  normal to both $\harpvec{T}$ and $\harpvec{N}$ according to
  the right-hand rule.
\end{definition}

\begin{theorem}
  \[
    \vect{B}=\vect{T}\times\vect{N}
  \]
\end{theorem}

          \begin{problem}
            Prove that $\vect{T}\times\vect{N}$ is a unit vector.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
          Given the following information about $\vect{r}(t)$ at a point,
          evaluate the binormal vector $\vect{B}$ and curvature
          $\kappa$ at that same point:
            \[\frac{d\vect{r}}{dt}=\<-3,0,3\sqrt{3}\>\]
            \[\frac{d\vect{T}}{dt}=\<-\sqrt{3},0,-1\>\]
            \[\vect{T}=\<-\frac{1}{2},0,\frac{\sqrt{3}}{2}\>\]
            \[\vect{N}=\<-\frac{\sqrt{3}}{2},0,-\frac{1}{2}\>\]
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
  A \textbf{right-handed frame} is a group of three unit vectors which
  are all normal to one another and satisfy the right hand rule.
\end{definition}

\begin{example}
  $\veci,\vecj,\veck$ and $\vect T,\vect N,\vect B$ are examples
  of right-handed frames.
\end{example}

\begin{theorem}
  Any vector is a linear combination of the vectors in a right-handed frame.
\end{theorem}

\section{Motion in Space, Velocity, and Acceleration}

\begin{definition}
The \textbf{velocity} $\vect{v}(t)$ of a particle at time $t$ on a position
function $\vect{r}(t)$ is its rate of change with respect to $t$.
\end{definition}

\begin{definition}
The \textbf{speed} $|\vect{v}(t)|$ of a particle at time $t$ on a position
function $\vect{r}(t)$ is the magnitude of its velocity.
\end{definition}

\begin{definition}
The \textbf{direction} $\vect{T}(t)$ of a particle at time $t$ on a position
function $\vect{r}(t)$ is the direction of its velocity.
\end{definition}

\begin{definition}
The \textbf{acceleration} $\vect{a}(t)$ of a particle at time $t$ on a position
function $\vect{r}(t)$ is the rate of change of its velocity with respect to $t$.
\end{definition}

\begin{theorem}
\[
  \vect{v}(t)=\vect{r}'(t)
\]
\[
  |\vect{v}(t)|=|\vect{r}'(t)|=\frac{ds}{dt}
\]
\[
  \vect{T}(t) = \frac{\vect v}{|\vect v|}
\]
\[
  \vect{a}(t)=\vect{v}'(t)=\vect{r}''(t)
\]
\end{theorem}

\begin{problem}
  Given a position function $\harpvec{r}(t) = \<t^3,t^2\>$ find its velocity,
  speed, and acceleration at $t = 1$.
\end{problem}

\begin{definition}
  \textbf{Ideal projectile motion} is an approximation of real-world
  motion assuming constant acceleration due to gravity in the $y$ direction
  and no acceleration in the $x$ direction:
    \[
      \vect{a}(t) = \<0,-g\>
    \]
\end{definition}

\begin{theorem}
  The velocity and position functions for a particle with initial velocity
  $\vect{v}_0=\<v_{x,0},v_{y,0}\>$ and beginning at position
  $P_0=\<x_0,y_0\>$ assuming ideal projectile motion are:
    \[
      \vect{v}(t) = \<v_{x,0},-gt+v_{y,0}\>
    \]
    \[
      \vect{r}(t) = \left\<v_{x,0}t+x_0,-\frac{1}{2}gt^2+v_{y,0}t+y_0\right\>
    \]
\end{theorem}

          \begin{problem}
            Assume ideal projectile motion and and $g=10\frac{m}{s^2}$.
            What is the flight time of a projectile shot from the ground
            at an angle of $\pi/6$ with initial speed $100\frac{m}{s}$?
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Assume ideal projectile motion and and $g=10\frac{m}{s^2}$.
            What must have been the initial speed of a projectile shot
            from the ground at an angle of $\pi/3$ if it
            traveled $60$ meters horizontally after $4$ seconds?
          \end{problem}

          \begin{solution}

          \end{solution}

\end{document}


\section{Value of Different Policies [35 pts]}
In many situations such as healthcare or education, we cannot run any arbitrary policy and collect data from running those policies for evaluation. In these cases, we may need to take data collected from following one policy and use it to evaluate the value of a different policy. The equality proved in the following exercise can be an important tool for achieving this.
The purpose of this exercise is to get familiar on how to compare the value of different policies, $\pi_1$ and $\pi_2$, on a fixed horizon MDP. A fixed horizon MDP is an MDP where the agent's state is reset after $H$ timesteps; $H$ is called the \emph{horizon} of the MDP. There is no discount (i.e., $\gamma=1$) and policies are allowed to be non-stationary, i.e., the action identified by a policy depends on the timestep in addition to the state.
Let $x_t\sim \pi$ denote the distribution over states at timestep $t$ (for $1\leq t \leq H$) upon following policy $\pi$ and $V^{\pi}_t(x_t)$ denote the value function of policy $\pi$ in state
$x_t$ and timestep $t$, and $Q_t^{\pi}(x_t,a)$ denote the corresponding
$Q$ value associated to action $a$. As a clarifying example, we denote $\E_{x_t \sim \pi_1} V(x_t)$ to represent the average value of the value function $V(\cdot)$ over the states at timestep $t$ encountered upon following policy $\pi_1$. Please show the following:
\begin{equation}
\label{eq:1}
V_1^{\pi_1}(x_1) - V_1^{\pi_2}(x_1) =  \sum_{t=1}^H \E_{x_t \sim \pi_2} \Big( Q_t^{\pi_1}(x_t,\pi_1(x_t,t)) - Q_t^{\pi_1}(x_t,\pi_2(x_t,t)) \Big)
\end{equation}

\textbf{Intuition:} The above expression can be interpreted in the following way. For concreteness, assume that $\pi_1$ is the better policy, i.e., achieving $V_1^{\pi_1}(x_1) \geq V_1^{\pi_2}(x_1)$. Suppose you're following policy $\pi_2$ and you are at timestep $t$ in state $x_t$.
You have the option to follow $\pi_1$ (the better policy) until the end of the episode, totalling $Q_t^{\pi_1}(x_t,\pi_1(x_t,t))$ return from the current state-timestep; or you have the option to follow $\pi_2$ for one timestep and then follow $\pi_1$ instead until the end of the episode (you can follow many other policies of course). This would you give you a ``loss'' of $Q_t^{\pi_1}(x_t,\pi_1(x_t,t)) - Q_t^{\pi_1}(x_t,\pi_2(x_t,t))$ that originates from following the worse policy $\pi_2$ instead of $\pi_1$ in that timestep.
% Then equation \ref{eq:1}
Then the equation above
means that the value difference of the two policies is the sum of all the losses induced by following the suboptimal policy for every timestep, weighted by the expected trajectory of the policy you're following.

\textbf{Answer:}

\begin{equation}
\begin{split}
V^{\pi_1}_{t}(x_t) - V^{\pi_2}_{t}(x_t) & = Q^{\pi_1}_{t}(x_t,\pi_1(x_t,t)) - Q^{\pi_2}_{t}(x_t, \pi_2(x_t,t)) \\
& = Q^{\pi_1}_{t}(x_t,\pi_1(x_t,t)) - Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t)) + Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t)) - Q^{\pi_2}_{t}(x_t, \pi_2(x_t,t))
\end{split}
\end{equation}

\begin{equation}
\begin{split}
Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t)) - Q^{\pi_2}_{t}(x_t, \pi_2(x_t,t)) & = r_t(x_t, \pi_2(x_t, t)) + {\mathbb E}_{s' \sim p(x_t, \pi_2(x_t, t))}V^{\pi_1}_{t+1}(s') \\
& \quad - r_t(x_t, \pi_2(x_t, t)) - {\mathbb E}_{s' \sim p(x_t, \pi_2(x_t, t))}V^{\pi_2}_{t+1}(s') \\
& = {\mathbb E}_{s' \sim p(x_t, \pi_2(x_t, t))}(V^{\pi_1}_{t+1}(s') - V^{\pi_2}_{t+1}(s'))
\end{split}
\end{equation}

Plug back to previous formula:

\begin{equation}
\begin{split}
V^{\pi_1}_{t}(x_t) - V^{\pi_2}_{t}(x_t) & = Q^{\pi_1}_{t}(x_t,\pi_1(x_t,t)) - Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t)) + {\mathbb E}_{s' \sim p(x_t, \pi_2(x_t, t))}(V^{\pi_1}_{t+1}(s') - V^{\pi_2}_{t+1}(s'))
\end{split}
\end{equation}
\begin{equation}
\begin{split}
{\mathbb E}_{x_t \sim \pi_2}(V^{\pi_1}_{t}(x_t) - V^{\pi_2}_{t}(x_t)) & = {\mathbb E}_{x_t \sim \pi_2}(Q^{\pi_1}_{t}(x_t,\pi_1(x_t,t)) - Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t))) + {\mathbb E}_{x_{t+1} \sim \pi_2}(V^{\pi_1}_{t+1}(x_{t+1}) - V^{\pi_2}_{t+1}(x_{t+1})) \\
& = {\mathbb E}_{x_t \sim \pi_2}(Q^{\pi_1}_{t}(x_t,\pi_1(x_t,t)) - Q^{\pi_1}_{t}(x_t, \pi_2(x_t,t))) \\
& \quad + \sum_{\tau=t+1}^H({\mathbb E}_{x_\tau \sim \pi_2}(Q_\tau^{\pi_1}(x_\tau, \pi_1(x_\tau, \tau)) - Q_\tau^{\pi_2}(x_\tau, \pi_2(x_\tau, \tau)))) \\
& = \sum_{\tau=t}^H({\mathbb E}_{x_\tau \sim \pi_2}(Q_\tau^{\pi_1}(x_\tau, \pi_1(x_\tau, \tau)) - Q_\tau^{\pi_2}(x_\tau, \pi_2(x_\tau, \tau))))
\end{split}
\end{equation}

By induction, above expression is true for any $1 \leq \tau \leq H$, this gives the thesis.


\documentclass[a4paper,11pt]{article}

\usepackage{physics}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm, mathtools}
%\usepackage{hyperref}
\usepackage{color}
\usepackage{jheppub}
\usepackage[T1]{fontenc} % if needed

\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bes}{\begin{equation*}}
\newcommand{\ees}{\end{equation*}}
\newcommand{\bea}{\begin{flalign*}}
\newcommand{\eea}{\end{flalign*}}
%\linespread{1.0}
%\setlength{\parindent}{0em}
%\setlength{\parskip}{0.8em}

\title{\textbf{Notes on Gravity as a Quantum Theory}}
\author{Aditya Vijaykumar}
\affiliation{International Centre for Theoretical Sciences, Bengaluru, India.}
\emailAdd{aditya.vijaykumar@icts.res.in}
\abstract{One has always wondered how one describes quantum field in the presence of gravity, and it is time one finally gained a good understanding. One has no clue where this will lead, but one chooses to follow the book by Prof. Mukhanov, and read from other references if needed.}

\begin{document}
\maketitle

\section{All is Classical, All is Quantum}

\subsection{The Classical Field}
$\phi(\va{x},t)$ gives the value of a classical field at every point in spacetime. The simplest classical field is the \textit{real scalar field}, which is characterized only by real numbers. The Klein-Gordon equation governs a free massive scalar field.

$$\pdv[2]{\phi}{t} - \sum_{x_j} \pdv[2]{\phi}{x_j} + m^2 \phi =0 $$

An interesting part about the free scalar field is that one can describe it as an infinite set of decoupled harmonic oscillators. Put this field into a box of length $L$ and volume $V=L^3$, and having periodic boundary conditions. One can Fourier decompose this as,
$$\phi(\va{x},t) = \frac{1}{\sqrt{V}} \sum_{\va{k}} \phi_{\vb{k}} (t) \exp(i \va{k}\vdot\va{x}) \text{ where } k_x =\frac
{2\pi n_x}{L} ,\ldots$$

Substituting this into the first equation, we find that the harmonic oscillators get nicely decoupled into an infinite set of ODEs of the form,
$$\ddot{\phi_{\vb{k}}} +(k^2+m^2)\phi_{\vb{k}} = 0$$
which is basically the harmonic oscillator equation with frequency $\omega_k = \sqrt{k^2 + m^2}$.The energy of oscillators in simply equal to the sum of individual energies of the oscillators,
$$E = \sum_{\vb{k}}\left[ \frac{1}{2} \dot{\phi_{\vb{k}}}^2 + \frac{1}{2}\omega_k^2 \phi_{\vb{k}}^2 \right]$$

Equivalently, when $V \rightarrow \infty $ and $k$ is a continuous variable, the summation is just replaced by an integral over all $k$,
$$\phi(\vb{x},t)= \int \frac{d^3\vb{k}}{(2\pi)^{3/2}}e^{i \vb{k} \vdot \vb{x}} \phi_{\vb{k}}(t)$$

\subsection{Quantizing Fields}
As mentioned earlier, a field can be thought of as a collection of decoupled harmonic oscillators. We quantize each field $\phi_{\vb{k}}$ as a separate harmonic oscillator. We identify the position and momentum as operators $\hat{\phi_{\vb{k}}}$ and $\hat{\pi_{\vb{k}}}$. The commutation relations for the harmonic oscillator as $V\rightarrow \infty$ can now be written as,
$$\comm{\hat{\phi_{\vb{k}}}(t)}{\hat{\pi_{\vb{k'}}}(t)} = i \delta(\vb{k}+\vb{k'})$$
The vacuum state is the state corresponding to the lowest energy configuration. One can clearly see that the commutation relations cannot be satisfied for the most intuitive low energy configuration \textit{ie.} $\phi(\vb{x},t) = 0$, implying that the vacuum state is really something non-trivial. But since, for a free field, all the $\phi_{\vb{k}}$ are decoupled, we can write the vacuum state wave functional as the product of all wavefunctions, each describing the ground state of the harmonic oscillator with the wavenumber $\vb{k}$. Again, for large volume, one can write,
$$\psi[\phi] \propto \exp(-\frac{1}{2}\int d^3 \vb{k} \abs{\phi_{\vb{k}}}^2 \omega_{\vb{k}})$$
Consider the integral inside the exponential,
\begin{flalign*}
\int d^3 \vb{k} \abs{\phi_{\vb{k}}}^2 \omega_{\vb{k}} &= \int  d^3 \vb{k}  \phi_{\vb{k}} \phi_{\vb{k}}^* \sqrt{k^2 + m^2}\\
&= \int   d^3 \vb{x} d^3 \vb{y} \phi(\vb{x}) \phi(\vb{y}) \int  d^3 \vb{k}  e^{i\vb{k}(\vb{y} - \vb{x})} \sqrt{k^2 + m^2}\\
&= \int   d^3 \vb{x} d^3 \vb{y} \phi(\vb{x}) \phi(\vb{y}) K(\vb{x},\vb{y})
\end{flalign*}
where $K(\vb{x},\vb{y})$ is called the kernel.

The vacuum energy density is just the sum of all ground state energies,
$$\frac{E_o}{V}= \int\frac{d^3 \vb{k}}{(2\pi)^3} \frac{\omega_{k}}{2}$$
Okay, now this is a very interesting expression for the energy. We see that because $\omega_k = \sqrt{k^2+m^2}$, we can see that this integral diverges as $k^4$. If quantum gravity is assumed to be modelled as a scalar field, and we put a cutoff for our integration at let's say the Planckian scale, we see that the vacuum energy density is of the order unity in Planck units, which in turn corresponds to a mass density of $10^{94} g/cm^3$. The mass of the \textit{entire} observable universe is $10^{55}g$! One can try to resolve this problem by \textit{positing} that vacuum energy does not contribute to gravity, or by using some supersymmetric variants of such theories.

\subsection{Vacuum Fluctuations}
The fluctuation in the quantum field can be written as,
$$\delta \phi_{\vb{k}} = \sqrt{\expval{\abs{\phi_{\vb{k}}}^2} - \expval{\phi_{\vb{k}}}^2} = \sqrt{\expval{\abs{\phi_{\vb{k}}}^2}}$$
We know that $$\phi_{\vb{k}} = \frac{a_{\vb{k}} + a_{-\vb{k}}}{\sqrt{2\omega_k}}$$ which means that
$$\abs{\phi_{\vb{k}}^2} = \frac{(a_{\vb{k}} + a_{-\vb{k}})(a_{\vb{k}} + a_{-\vb{k}})}{{2\omega_k}}$$
Taking the ground state expectation value of this expression, one obtains that $\delta \phi_{\vb{k}} \sim \omega_k^{-1/2}$. What if we measure the average value of a field over space? Lets consider a cubical box of side $L$  and define the average value $\phi_L$ as follows,
$$\phi_L = \frac{1}{L^3} \int_{-L/2}^{-L/2}dx \int_{-L/2}^{-L/2}dy \int_{-L/2}^{-L/2}dz \ \phi(\vb{x})$$
We again calculate fluctuations in this average value by the formula $\delta \phi_L = \sqrt{\expval{\phi_L^2}}$.
\begin{flalign*}
\phi_L &\sim \frac{1}{L^3}  \int_{-L/2}^{-L/2}dx \int_{-L/2}^{-L/2}dy \int_{-L/2}^{-L/2}dz \ \int \phi_{\vb{k}}(t) e^{i \vb{k} \vdot \vb{x}} d^3\vb{k}\\
&\sim \frac{1}{L^3} \int \frac{1}{k_x k_y k_z}\sin \frac{k_xL}{2} \sin \frac{k_yL}{2} \sin \frac{k_zL}{2}  \  \phi_{\vb{k}}(t) e^{i \vb{k} \vdot \vb{x}} d^3\vb{k}
\end{flalign*}
Let us say that $f_x =\frac{1}{k_x} \sin \frac{k_xL}{2}$ and so forth for $y$ and $z$. $f_x \rightarrow L/2$ for small $k_x$ and $0$ for large $k_x$. So the contribution to the integral can be taken to be from small $k$, and hence is of the order $L^3$. So $\delta \phi_L = \sqrt{\expval{\phi_L^2}} \sim [(\delta \phi_{\vb{k}})^2 / L^3]^{1/2}$.

\subsection{Gravity Can Create Particles?}
Consider a single harmonic oscillator with the following features.
$$\underbrace{\ddot{q}(t) + \omega^2 q = 0}_{t<0 \text{ and } t>T} \text{ ; } \underbrace{\ddot{q}(t) - \Omega^2 q = 0}_{0<t<T}$$
The solution, obviously, is
$$q(t) = \underbrace{q_1 \sin(\omega t)}_{t<0 \text{ (assume)}} \text{ ; } \underbrace{Ae^{\Omega t} + Be^{-\Omega t}}_{0<t<T} \text{ ; } \underbrace{q_2 \sin (\omega t + \alpha)}_{t>T}$$
Matching $q(t)$ and $\dot{q}(t)$ at $t=0,T$, we get the condition,
$$\tan(\omega T + \alpha) = \frac{\omega}{\Omega} \text{ ; } q_2 \sin (\omega T + \alpha) = A e^{\Omega T} \text{ ; } A = \frac{q_1}{2} \frac{\omega}{\Omega}$$

Hence,
$$q_2 \approx \frac{1}{2} q_1 \sqrt{1 + \frac{\omega^2}{\Omega^2}} e^{\Omega T}$$

We see that the final state has a much larger energy as compared to the initial state, which we, in turn, interpret as the creation of many particles the time interval $[0,T]$. Can we look at how many particles are produced, approximately? The exact relation for the amplitudes (valid at all times) is
\bes
q_2 =  q_1 \sqrt{1 + \frac{\omega^2}{\Omega^2}} \sinh \Omega_0 T
\ees
The oscillator energies $\propto q^2$. If we take the initial state to be the ground state, it is straightforward to see that the number of particles $ n  $ produced is
\bes
n = \dfrac{q_2^2}{q_1^2} = \qty(1+ \dfrac{\omega^2}{\Omega^2})\sinh^2 \Omega_0 T
\ees
Hmm, can something of this sort happen in gravity? Let's hope.

\end{document}


\documentclass[12pt, a4paper]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}

\title{
    EE2703: Applied Programming Lab \\
    \Large Assignment 3: Linear Least Squares Fitting
}

\author{Soham Roy \\ \normalsize EE20B130}

\date{\today}

\begin{document}

\maketitle % Insert the title, author and date

\section{Introduction}
For this assignment we use the Bessel function and Gaussian noise to study
the effect of changing standard deviation on the linear fitting of the data.
\begin{equation*}
    f(t) = AJ_2(t) + Bt + n(t)
\end{equation*}
Where, $A = 1.05$, $B = -0.105$, $J_2 =$ Bessel function, $n(t) =$ Noise function.
Our aim is to relate the error in estimating $A$, $B$ to the
standard deviation of the Gaussian noise.

\section{Subquestions}
\subsection{Generate the Data}
On running the python script \texttt{generate\_data.py}, the data is written to
the file \texttt{fitting.dat}. The \texttt{scipy} library has been used to calculate
the Bessel function. The \texttt{numpy.array t} contains 101 equally spaced numbers
between 0 and 10, which are fed into the Bessel function and added with noise to
generate the data.

\subsection{Load the Data}
The data from \texttt{fitting.dat} is loaded using \texttt{numpy.loadtxt()}.
The first column of \texttt{raw\_data} are the time values, and the subsequent
columns are the corresponding noisy data values.
\begin{verbatim}
    raw_data = loadtxt(DATAFILE)

    Time = raw_data[:, 0]
    F = raw_data[:, 1:]
\end{verbatim}

\subsection{The Function and the Noise}
The true values are generated using \texttt{F\_true = g(Time)},
where the function \texttt{g(t,A,B)} defined as:
\begin{verbatim}
    def g(t, A=A_true, B=B_true):
        return A * jn(2, t) + B * t
\end{verbatim}
where \texttt{A\_true, B\_true = 1.05, -0.105} are the true values of $A$ and $B$. \\
The noise is generated using \texttt{Sigma = logspace(-1, -3, K)}, where
\texttt{K = 9} is the number of curves to be plotted.

\subsection{Plot the Data}
We plot the data along with the function \texttt{g(t,A,B)} for
\texttt{A=1.05}, \texttt{B=-0.105}.
\begin{verbatim}
    plot(Time, F)
    plot(Time, F_true, color='black', lw=2)
\end{verbatim}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{Q4.png}
\end{figure}

\subsection{Plot with Error Bars}
A plot of the first column of data with error bars has been generated, with every
$5^{th}$ data item plotted for readability. The exact curve has also been plotted
to see how much the data diverges.
\begin{verbatim}
    errorbar(Time[::5], F[::5, 0], Sigma[0], fmt="ro")
    plot(Time, F_true, color='black', lw=2)
\end{verbatim}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{Q5.png}
\end{figure}

\subsection{Equate the Vectors}
\begin{gather}
    g(t,A,B) =
    \begin{pmatrix}
        J_2(t_1) & t_1   \\
        \dots    & \dots \\
        J_2(t_m) & t_m
    \end{pmatrix}
    \begin{pmatrix}
        A \\ B
    \end{pmatrix}
    \equiv
    M \cdot P
    \label{eq:M}
\end{gather}
\texttt{F\_true.reshape(N, 1)} is $g(t,A,B)$, the vector of the true values. \\
\texttt{M = c\_[jn(2, Time), Time]} generates $M$, which is multiplied by \\
\texttt{[[A\_true], [B\_true]]}, i.e. $P$, to obtain the RHS vector.\\
\texttt{assert} ensures that the two vectors are equal by evaluating \\
\texttt{numpy.allclose()}, as we cannot reliably equate floats.

\subsection{Mean Squared Error}
The mean squared error between the data ($f_k$) and the assumed model has been
calcuated for every combination of $A$ and $B$, where $A$ and $B$ range from 0 to 1
and -0.2 to 0 respectively. \\
The following formula has been implemented:
\begin{equation*}
    \epsilon_{ij} = \frac{1}{101} \sum_{k=0}^{101} (f_k - g(t_k, A_i, B_j))^2
\end{equation*}
by looping over the following line of code:
\begin{verbatim}
    eps[i][j] = mean((F[:, 0] - g(Time, A[i], B[j])) ** 2)
\end{verbatim}

\subsection{Plot the MSE}
The contour plot has been generated by \texttt{contour}, and labeled using
\texttt{clabel}. Further, the exact location of \texttt{(A\_true, B\_true)} has been
plotted and annotated.
\begin{verbatim}
    clabel(contour(A, B, eps, 15))
    plot([A_true], [B_true], "ro")
    annotate("Exact location", xy=(A_true, B_true), size=16)
\end{verbatim}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{Q8.png}
\end{figure}

\subsection{Best Estimate for A and B}
The matrix $M$, defined in Equation \ref{eq:M}, has been used to find the best estimate
of $A$ and $B$ for the first column of data. This was done by computing the
least-squares solution for it using \texttt{scipy.linalg.lstsq()} to print:
\begin{verbatim}
"Best estimate:   A = {}, B = {}".format(*lstsq(M, F[:, 0])[0])
\end{verbatim}

\subsection{Plot the Errors in A, B}
The errors in $A$ and $B$ have been calculated by subtracting the true values:
\begin{verbatim}
    Aerr, Berr = abs(lstsq(M, F)[0] - [[A_true], [B_true]])
\end{verbatim}
These have thus been plotted against the standard deviations of the data:
\begin{verbatim}
    plot(Sigma, Aerr, 'o', linestyle="dashed")
    plot(Sigma, Berr, 'o', linestyle="dashed")
\end{verbatim}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{Q10.png}
\end{figure}
\pagebreak

\subsection{Plot using log-log Scale}
The scale of the graph has been changed to log-log, with an \texttt{errorbar()} plot:
\begin{verbatim}
    xscale("log")
    yscale("log")
    errorbar(Sigma, Aerr, Sigma, fmt="o")
    errorbar(Sigma, Berr, Sigma, fmt="o")
\end{verbatim}
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{Q11.png}
\end{figure}

\section{Conclusion}
As we see from the plots, the error in estimated $A$ and $B$ increases with increase in
the standard deviation of the Gaussian noise in the data. Further, we see that the
increase is somewhat linear when plotted on a log-log scale.

\end{document}


\section{Model}

The TMD system is described by
the effective tight-biding, low-energy, two-valley Hamiltonian
\cite{PhysRevLett.108.196802},
\begin{equation}
  \label{eq:hamiltonian}
  H_τ^0 \ofK
  = a t \left( τ k_x \hat{σ}_x + k_y \hat{σ}_y \right)
  + \frac{E_g}{2} \hat{σ}_z - E_{\text{soc}} τ \frac{\hat{σ}_z - 1}{2} \hat{s}_z.
\end{equation}
where the Pauli matrices $\hat{s}_i$ operate in the spin space and
$\hat{σ}_i$ operate in the orbital space
with the two Bloch orbital states $\ketOrb{ν}{\ofK}$
(indexed by $ν = +$ for the in-plane orbital state
$\Ket{d_{x^2 - y^2}} + i τ \Ket{d_{xy}}$
and $ν = -$ for the out-of-plane orbital state $\Ket{d_{z^2}}$),
${\s} = ±$ is the spin index, and $τ = ±$ is the valley index corresponding
to the $± \vc{K}$ point, respectively.
The momentum $\vK = \left( k_x, k_y \right)$
is measured from the valley center, $a$ is the lattice constant,
$t$ is the hopping parameter, $E_g$ represents the energy
gap between the conduction and valence bands, and $2E_{\text{soc}}$ is the
spin splitting energy in the valence bands due to spin-orbit interaction.

The energy spectrum,
\begin{equation}
  \label{eq:energy}
  2 \fnEnergy{n} \of{k}
  = τ {\s} E_{\text{soc}} + n \sqrt{{\left( 2 a t k \right)}^2
  + {\left( E_g - τ {\s} E_{\text{soc}} \right)}^2}.
\end{equation}
with $k = \abs{\vK}$
and $n = 1$ ($n = -1$) indexing the conduction (valence) band
is shown in \cref{fig:energy}.

\begin{figure}
  \includegraphics[width=\columnwidth]{figures/energy-bands}
  \caption{%
    Energy bands for $\ce{WSe2}$ as given by \cref{eq:energy}
    with $a t = \SI{3.939}{\electronvolt \per \angstrom}$,
    $E_g = \SI{1.60}{\electronvolt}$,
    and $E_{\text{soc}} = \SI{0.23}{\electronvolt}$.
    Each valley is centered at $± \vc{K}$ relative to the center of the
    Brillouin zone.
    The energy for a given band depends only on the distance $k$
    measured from the valley center.
  }\label{fig:energy}
\end{figure}

We focus on doped systems
such that the chemical potential $μ$ lies in the upper valence bands.
Within each band, the Bloch basis eigenstates are written
in terms of the orbital states as elements on the Block sphere,
\begin{equation}
  \begin{aligned}
    \Ket{u_{τ {\s}}^n \of{k, ϕ}}
    = & \cos{\frac{\fnTheta{n}}{2}} \ketOrb{+}{\of{k, ϕ}} \\
    + e^{-i τ ϕ}
      & \sin{\frac{\fnTheta{n}}{2}} \ketOrb{-}{\of{k, ϕ}},
  \end{aligned}
\end{equation}
where $k_x + i τ k_y = k e^{i τ ϕ}$ and
\begin{equation}
  \tan{\frac{\fnTheta{n}}{2}}
  = \frac{a t τ k}{\dfrac{E_g}{2} - \fnEnergy{-n} \of{k}}
  = \frac{a t τ k}{\fnEnergy{n} \of{k} - \fnEnergy{-} \of{0}}.
\end{equation}
The polar angle on the Bloch sphere
of the conduction and valence bands are related by
$\fnTheta{-} - \fnTheta{+} = τ π$.
The mapping of the energy band to the Bloch sphere,
parametrized by $\left( θ, ϕ \right)$,
encodes the topological character:
as one moves from the node out to infinity,
the states sweep either the northern or southern hemisphere
with a chirality determined by the Berry curvature.


\subsection{Space Partitioning}
\subsubsection{Description of the Algorithm}
The main idea is a combination of the divide and conquer principle and
the convex hull. The algorithm divides the point cloud by a line and
recursively process those sets. Finally the points of the sets were
pushed to the polygon. The fact that every set has a convex hull that
has only one point in common with adjacent sets guarantees a simple
polygon.
\\[12pt]
Following a description of the algorithm in detail:

\begin{enumerate}
  \item select two points
  \item divide the point cloud by a line through those points \fref{sp:line}
  \item recursively process those two sets of points \fref{sp:line2} to \fref{sp:line5}
  \begin{enumerate}
    \item exit point set of points == 2 || == 3 push points on the final list
    \item calculate a random point
    \item calculate a random line with this point
    \item divide the point cloud by that line
  \end{enumerate}
\end{enumerate}

\subsubsection{Implementation description}
\begin{enumerate}
  \item a random function selects two points ($s_l$ and $s_f$). Point $s_l$ is the
    first point in rotation direction and $s_f$ the second point.
  \item to divide the point cloud in two independent sets we create a
    line through the two points. The \textit{Line\_2} class from the cgal
    framework has a \textit{has\_on\_positive} and \textit{has\_on\_negative} function.
    With these it is possible to distinguish between a point of one or
    the other side of the line.
  \item with the created sets we call recursivelly the function
    \textit{recursiveDivide} and give the function the created set $S$, point $s_f$,
    point $s_l$ and the polygon $C$. One call has as first element $s_f$ the other has $s_l$
    as first element of the two points. This is necessary to close the
    polygon.
  \begin{enumerate}
    \item there are two exit points of the \textit{recursiveDivide} function.
      The first is with set \textit{S.size == 2} and pushs the point $s_l$ to the
      polygon $C$. The second is with set \textit{S.size == 3}. Here we have to
      search the third point $s$, which is not $s_l$ or $s_f$ and then we
      could push $s$ and $s_l$ to the polygon $C$.
    \item if the exit points does not occure we calculate a random
      point. The random point should be selected randomly. The
      condition is that it is not a point equal to and not collinear
      with $s_f$ and $s_l$. The next step is to calculate a random line
      through the point s and a point of the line through $s_f$ and
      $s_l$. Essentially is that $s_f$ lies on the positive side of the
      calculated random line.
    \item the division of the set is equal as in the parent function
  \end{enumerate}
\end{enumerate}

\subsubsection{Complexity}
$\bigO(n^2)$ if it is done in a good way then it should be $\bigO(nlogn)$

\subsubsection{Parameters}
\begin{description}
  \item [--nodes] how many nodes the polygon has to have. [default: 100]
  \item [--sampling-grid] the area within the polygon could grow. [default: 1500x800]
\end{description}

\subsubsection{Examples}

\begin{figure}[ht]
  \centering

  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10),(33,15),(39,22),(48,9),(55,17),(68,19)} {
        \node[point] (\arabic{i}) at \p {};
        \stepcounter{i}
      }
    \end{tikzpicture}
    \caption{Base random point cloud}
    \label{fig:sp:base}
  \end{minipage}\hfill
  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10)} {
        \node[point, fill=red] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \foreach \p in {(33,15),(39,22)} {
        \node[point, fill=violet] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \foreach \p in {(48,9),(55,17),(68,19)} {
        \node[point, fill=blue] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \path (5) -- (6) coordinate[pos=-3.5](dd) coordinate[pos=10](ff);
      \draw[green, dashed] (dd) -- (5);
      \draw[green] (5) node[right=1pt, black] {$s_f$} -- (6) node[left=1pt, black] {$s_l$};
      \draw[green, dashed] (6) -- (ff);
    \end{tikzpicture}
    \caption{First two points with the line and the two sets. Points on the line are in both sets.}
    \label{fig:sp:line}
  \end{minipage}
\end{figure}

\begin{figure}[ht]
  \centering

  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10),(33,15),(39,22),(48,9),(55,17),(68,19)} {
        \node[point] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \draw[green] (5) node[right=1pt, black] {$s_f$} -- node[placeholder](a){} (6) node[right=1pt, black] {$s_l$};
      \draw[red] (a) -- (1) node[right=1pt, black] {$s$};
    \end{tikzpicture}
    \caption{First split of left set}
    \label{fig:sp:line2}
  \end{minipage}\hfill
  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10),(33,15),(39,22),(48,9),(55,17),(68,19)} {
        \node[point] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \draw (5) -- node[placeholder](a){} (6);
      \draw (a) -- node[placeholder](b){} (1);
      \draw (2) -- (b);
    \end{tikzpicture}
    \caption{Second split of left set}
    \label{fig:sp:line3}
  \end{minipage}
\end{figure}

\begin{figure}[ht]
  \centering

  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10),(33,15),(39,22),(48,9),(55,17),(68,19)} {
        \node[point] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \draw (5) -- node[placeholder](a){} (6);
      \draw (a) -- node[placeholder](b){} (1);
      \draw (2) -- (b);
      \draw (8) -- (a);
    \end{tikzpicture}
    \caption{First split of right set}
    \label{fig:sp:line4}
  \end{minipage}\hfill
  \begin{minipage}[t]{0.4\textwidth}
    \begin{tikzpicture}[yscale=0.05,xscale=0.05]
      \setcounter{i}{1}

      \draw[->] (0,0) -- (80,0) node[below] {$x$};
      \draw[->] (0,0) -- (0,70) node[left] {$y$};

      \foreach \p in {(5,40),(10,20),(20,41),(21,10),(33,15),(39,22),(48,9),(55,17),(68,19)} {
        \node[point] (\arabic{i}) at \p {};
        \stepcounter{i}
      }

      \draw (5) -- (4) -- (2) -- (1) -- (3) -- (6) -- (9) -- (8) -- (7) -- (5);
    \end{tikzpicture}
    \caption{Final polygon}
    \label{fig:sp:line5}
  \end{minipage}
\end{figure}

\FloatBarrier


\subsection{The cross-sectional model}

\subsubsection{Hierarchical data}

Our standard linear model is:

\(y_i=\alpha + X_i\theta +\epsilon_i\)

If we had two sets of data we could view these as:

\(y_{i,0}=\alpha_0 + X_{i,0}\theta_0 +\epsilon_{i,0}\)

\(y_{i,1}=\alpha_1 + X_{i,1}\theta_1 +\epsilon_{i,1}\)

Here, the data data from \(1\) does not affect the parameters in \(2\).

\subsubsection{Pooled data}

If we think the data generating process is similar between models, then by restricting the freedom of parameters between models we can get more data for each estimate.

For example if we think that all parameters are the same between the models we can estimate:

\(y_{i,0}=\alpha + X_{i,0}\theta +\epsilon_{i,0}\)

\(y_{i,1}=\alpha + X_{i,1}\theta +\epsilon_{i,1}\)

Or:

\(y_{ij}=\alpha + X_{ij}\theta + \epsilon_{ij}\)

\subsubsection{Fixed slopes}

Intercepts may be different between the groups. In this case we can instead use the model:

\(y_{ij}=\alpha + X_{ij}\theta + \xi_j + \epsilon_{ij}\)

There are different ways of estimating this model:

\begin{itemize}
\item Pooled OLS
\item Fixed effects
\item Random effects
\end{itemize}


\lab{Fourier Transform Extensions}{Fourier Transform Extensions}
\objective{Learn about two particular extensions of the Fourier Transform: cepstral analysis and the two-dimensional
FFT.}

The ideas at the basis of the one dimensional Fourier Transform that you explored in previous labs can be
extended in many ways to a variety of different settings.
One way to extend the Fourier Transform, which is a linear theory, is to compose it with non-linear operations.
This is the basis of Cepstral Analysis, which we will briefly discuss and then apply to the study of sound signals.
Another way to generalize the basic Fourier Transform is to move into higher dimensions, which we do when exploring the two-dimensional FFT.
Although we only address these two topics in this lab, keep in mind that the Fourier Transform and its generalizations are central to the mathematical field of harmonic analysis as well as myriad applications in physics, engineering, statistics, and other fields.

\subsection*{The Fourier Transform in Python}
SciPy includes the module \li{scipy.fftpack} that contains several useful and easy to use functions
for calculating the Fourier Transform and its variants.
Refer to the documentation to learn about the available tools.
This module does not, however, take full advantage of the optimizations provided by the FFTW library written in the C programming language.
We recommend PyFFTW, a Python module that is built around FFTW.
For convenience, the submodule \li{pyfftw.interfaces.scipy_fftpack} mimics the
\li{scipy.fftpack} module, containing the same functions with the same interfaces, just higher-performance.

\subsection*{Cepstral Analysis and Mel-Frequency Cepstral Coefficients}

When analyzing complex sound signals such as speech or music, the simple spectral information provided by
the Fourier Transform by itself is often insufficient.
For example, while the Fourier Transform does reveal the prominent frequencies in a sound signal, it does a poor job of highlighting the more subjective
tonal qualities, or \emph{timbre}, of the signal (qualities that help us distinguish between different singers
or different musical instruments).

Cepstral analysis was developed to partially redress this situation, and it provides a collection of signal
processing techniques that go beyond the basic Fourier Transform.
The basic idea is to analyze the \emph{cepstrum} of a signal as opposed to the \emph{spectrum} (which is the approach of basic Fourier analysis).
There are various ways to define the cepstrum, and they all involve composing the Fourier Transform with non-linear operations.
Given a signal $f(t)$, the \emph{Power Cepstrum} is defined by
$$
\left|\mathcal{F}^{-1}\{\log(|\mathcal{F}(f(t))|^2)\}\right|^2,
$$
where $\mathcal{F}$ denotes the Fourier transform. The \emph{Complex Cepstrum} is defined by
$$
\mathcal{F}\{\log(\mathcal{F}(f(t)))+j2\pi m\},
$$
where $j$ is the imaginary unit and $m$ is a carefully chosen integer, and the \emph{Real Cepstrum}
is defined by
$$
\mathcal{F}^{-1}\{\log(|\mathcal{F}(f(t))|^2)\}.
$$
The usefulness of each type of Cepstrum is determined by the theoretical setting or the application.
The complex Cepstrum has the interesting and useful property of transforming the convolution operation into an additive operation.
In particular, if $f$ and $g$ are signals with corresponding complex cepstra $f'$ and $g'$, and if $*$ represents the convolution operation, we have
$$
f * g \rightarrow f' + g'.
$$
The reason for this relies on the fact that convolution can be converted to multiplication by the Fourier transform, and when the logarithm is applied it becomes addition.
This is useful when trying to separate two signals that have been convolved with each other, such as a source and a filter, a problem known as \emph{deconvolution}.
Other uses for the various cepstra include fundamental pitch detection in speech signals, voice identification, and analysis of signals containing echoes.

As a side note, the field of Cepstral analysis comes with its own distinctive jargon, including ``cepstrum",
``quefrency", ``liftering", and ``alanysis". These are derived, respectively, from ``spectrum",
``frequency", ``filtering", and ``analysis". See Figure \ref{fourierext:pc} for an example of the Power
Cepstrum.

\begin{figure}
\centering
\begin{subfigure}[t]{.4\textwidth}
\centering
\includegraphics[height=1.2in]{OriginalSignal.pdf}
\caption*{Original Signal}
\end{subfigure}
~
\begin{subfigure}[t]{.4\textwidth}
\centering
\includegraphics[height=1.2in]{FourierTransform.pdf}
\caption*{Fourier Transform}
\end{subfigure}

\begin{subfigure}[t]{.4\textwidth}
\centering
\includegraphics[height=1.2in]{SquaredLog.pdf}
\caption*{Squared Log}
\end{subfigure}
~
\begin{subfigure}[t]{.4\textwidth}
\centering
\includegraphics[height=1.2in]{PowerCepstrum.pdf}
\caption*{Power Cepstrum}
\end{subfigure}

\caption{Example of the steps in calculating the Power Cepstrum.}
\label{fourierext:pc}
\end{figure}

\begin{problem}
Write a function \li{powerCepstrum} that accepts a one-dimensional array $f$ and computes the
power cepstrum of $f$, according to the definition given above.

Because you need to use the log function in the process, you will want to set all values of the Fourier transform below a certain threshold to that threshold value (i.e. get rid of 0's).
For our purpose, set this threshold to the value \li{1e-100}.
\end{problem}

We now turn our attention to the computation of \emph{Mel-frequency cepstral coefficients} (MFCCs), which
are data useful in signal classification problems such as speech recognition and automatic musical instrument classification.

Expressed in simple terms, calculating the MFCCs involves splitting the signal up into a sequence of
short segments called \emph{frames}, calculating a variant of the Power Cepstrum for each one,
and reducing the dimensionality of the results through a process called \emph{binning}.
What we are left with is a collection of numbers that summarizes each of the frames, with the hope that these
numbers capture important spectral and timbral information about the original signal.

Suppose we have a sound signal 2 seconds long sampled at a standard rate of 44100 Hz, which means the
signal has 88200 entries.
The  MFCCs are obtained through a series of steps, which are outlined
as follows:

\begin{itemize}
\item \emph{Windowing}. This refers to splitting up the original signal into a sequence of overlapping
short segments called frames, and then applying a so-called \emph{window-function}, which scales down
the edges of the frames.

In the particular case at hand, we wish to split up the signal into frames approximately 30 ms in
duration, with adjacent frames overlapping by 20 ms.
This means splitting our signal into $198$ frames, each frame containing $1323$ values from the original signal, overlapping $882$ values with each frame
immediately preceding and following it.
We then multiply each frame by a \emph{Hamming} window of length 1323 (we have provided the code to calculate a Hamming window of a given length).
See Figure \ref{fourierext:ham} for an example of the Hamming window applied to a signal.

\begin{figure}
\centering

\begin{subfigure}[t]{.3\textwidth}
\centering
\includegraphics[height=1.2in]{HammingWindowFunction.pdf}
\caption*{Hamming Window Function}
\end{subfigure}
~
\begin{subfigure}[t]{.3\textwidth}
\centering
\includegraphics[height=1.2in]{Original.pdf}
\caption*{Original Signal}
\end{subfigure}
~
\begin{subfigure}[t]{.3\textwidth}
\centering
\includegraphics[height=1.2in]{WindowedSignal.pdf}
\caption*{Windowed Signal}
\end{subfigure}

\caption{The Hamming Window.}
\label{fourierext:ham}
\end{figure}

\begin{problem}
Write a function \li{window} that accepts a 2-second sound signal with sample rate 44100, and
returns a list of the 198 windowed frames calculated as in the description above.
\end{problem}

\item \emph{Pre-emphasis and Power Spectrum}. For each frame $\tilde{f}$, we next apply
a filtering process known as \emph{pre-emphasis} used to improve the signal-to-noise ratio as follows:
\begin{equation*}
\widehat{f}_{n} = \tilde{f}_{n} - 0.95 \tilde{f}_{n-1}
\end{equation*}
for $n = 1,\ldots,1322$, and $\widehat{f}_0 = \tilde{f}_0$ (in Python, this can be done in one line of code using vectorization).
We then compute the \emph{power spectrum} of the result, which is the square of the magnitude of the Fourier transform.

\begin{problem}
Write a function \li{powerSpectrum} that accepts a frame (one dimensional array of length 1323) and
applies pre-emphasis and then computes the power spectrum of the result.
In this computation, pad the frame with zeros so that it is of length 2048 when computing the fourier transform.
This can be done by using the keyword argument \li{n = 2048} when calling \li{pyfftw.interfaces.scipy_fftpack.fft}.
Furthermore, only return the first 1025 entries of the result, since the remaining entries are just a mirror image of the first half.
\end{problem}

\item \emph{Mel Scale}. The Mel Frequency scale is based on human perception of pitch.
It maps frequency in Herz to a slightly distorted scale where equal distances between values correspond to equal perceived differences in pitch.
Using matrix multiplication, we will map the power spectrum of each frame to mel scale bins, thereby reducing the dimensionality of the frames and representing them in a scale more closely aligned with human perception of pitch.
See Figure \ref{fourierext:mel} for a depiction of the Mel Scale as well as a typical Mel filterbank, which defines the binning process.

We have provided the code to calculate the required mel scale matrix (use 40 filters, with FFT length 2048 and sample rate 44100).
Before we perform this multiplication, however, we first want to prevent underflow problems.
We therefore want to set all values of the power spectrum below a certain threshold to that threshold value.
For our purposes, we will set this threshold value to \li{1e-100}.

\item \emph{Discrete Cosine Transform}. To complete the process, we take the Discrete Cosine Transform
of the \emph{logarithm} of the resulting mel scale power spectrum frames, multiply the result by 0.25, and retain only entries 2
through 11 (yielding an array of length 10), since these are the entries that contain most of the desired information.
The Discrete Cosine Transform is very similar to the Fourier Transform, but only uses
cosine functions rather than a combination of cosine and sine functions in the series expansion of the signal.
In many cases, sums of cosine functions alone can more efficiently represent a signal than
sums of both sine and cosine functions.
\end{itemize}

After this process, we should end up with a list of 198 arrays, each of length 10. These are our MFCCs,
which can then be used in various classification algorithms.

\begin{problem}
Write a function \li{extract} which accepts an array of length 88200 (representing a two second sound sample
with rate 44100 Hz), and returns an array of shape (198,10), where each row gives the calculated MFCCs for
a particular windowed frame of the original signal.
Use your previous solutions as you see fit, and try to avoid unnecessary looping.

Hint: If M is the mel scale matrix and X is the power spectrum of the original signal, use \li{np.dot(M,X)} to get the mel scale power spectrum frames.
Use \li{pyfftw.interfaces.scipy_fftpack.dct()} to compute the Discrete Cosine Transform.
\end{problem}

\begin{figure}

\begin{subfigure}{\textwidth}
\centering
\includegraphics[ trim=0in 3in 0in 0in, clip, height=1.8in]{MelScale.pdf}
\end{subfigure}

\begin{subfigure}{\textwidth}
\centering
\includegraphics[trim = 0in 0in 0in 3in, clip, height=1.8in]{MelScale.pdf}
\end{subfigure}

\caption{The Mel Scale and Mel Filterbank.}
\label{fourierext:mel}
\end{figure}

\subsection*{The 2-dimensional FFT}

You know how to calculate the Fourier Transform for one-dimensional signals.
In fact, the theory of Fourier Transforms may be readily extended to any number of dimensions.
Computationally, the problem reduces to performing the one-dimensional Fourier Transform iteratively
along each of the dimensions.
We will focus on calculating the Fourier Transform of two-dimensional matrices.
In this setting, we think of the matrices not as functions of time, but rather as functions of
space.
Once we have the 2-dimensional Fourier Transform, we can perform many useful operations for
denoising, compression, edge-detection, image enhancement, and more.

Given a matrix $A$, we first calculate the one-dimensional Fourier Transform of each column, storing the
result column-wise in an array the same shape as $A$.
We then calculate the Fourier Transform of each row of this resulting array, and this yields the two-dimensional Fourier Transform of $A$.

Calculating the two-dimensional inverse Fourier Transform is done in a similar fashion, but in the
oppositie order: first calculate the inverse Fourier Transform of the rows, then the columns.

\begin{problem}
Write a function \li{fft2} that accepts a two-dimensional array as input and returns the two-dimensional Fourier Transform of the input.
You may use built-in methods to calculate the one-dimensional Fourier Transform.

Write another function \li{ifft2} that accepts a two-dimensional array as input and returns the two-dimensional inverse Fourier Transform.
Once again, you may use built-in methods to calculate the one-dimensional inverse Fourier Transform.
\end{problem}

\begin{figure}
\begin{subfigure}[t]{0.4\textwidth}
\centering
\includegraphics[height=1.5in]{ecoli.jpg}
\caption*{E. Coli Image}
\end{subfigure}
~
\begin{subfigure}[t]{0.4\textwidth}
\centering
\includegraphics[height=1.5in]{ecoliFFT.pdf}
\caption*{FT of E. Coli Image}
\end{subfigure}
\caption{The 2D Fourier Transform Applied to Images.}
\label{fourierext:2dfft}
\end{figure}

We can visualize the two-dimensional Fourier Transform as follows:
\begin{lstlisting}
import numpy as np
import pyfftw
from matplotlib import pyplot as plt

# In the following, F is the output of 2dFFT
# Shift values in F for optimal visualization
F_s = pyfftw.interfaces.scipy_fftpack.fftshift(F)

# Convert all values to nonnegative real numbers
F_mag = np.abs(F_s)

# Scale the values, then amplify
amp = 1000 # you may want to test other values for amp
F_mag *= amp/F_mag.max()

# Set values larger than 1 to 1
F_mag[F_mag > 1] = 1

# Plot the result
plt.imshow(F_mag, plt.cm.Greys)
plt.show()
plt.clf()
\end{lstlisting}

See Figure \ref{fourierext:2dfft} for an example.
The visualization indicates the relative strengths of various frequencies in the original image.
As you will notice, the largest values of the fourier transform are concentrated near the center of each visualization, which tells us that the original images are
composed mostly of smaller frequencies.
The presence of higher frequencies is indicated by values farther away from the center of the visualization, and these account for the
finer details of the original images.
%Does anyone know of a better way to intuitively describe these visualizations?


\subsection{Order and size of graphs}

The order of a graph is the number of vertices, \(|V|\).

The size of a graph is the number of edges, \(|E|\).


\paragraph{Question 1.}

Let the alphabet \(\Sigma = \{a, b\}\) and the following regular
expressions:
\begin{align*}
  r &= a \lparen a \, \disjM{} \, b \rparen\kleeneM{} ba,\\
  s &= \lparen ab \rparen\kleeneM{} \, \disjM{} \, \lparen ba
  \rparen\kleeneM{} \, \disjM{} \, \lparen a\kleeneM{}
  \, \disjM{} \, b\kleeneM\rparen.
\end{align*}
The language denoted by~\(r\) is noted \(L(r)\) and the language
denoted by~\(s\) is noted \(L(s)\). Find a word~\(x\) such that
\begin{enumerate*}

  \item \(x \in L(r)\) and \(x \not\in L(s)\),

  \item \(x \not\in L(r)\) and \(x \in L(s)\),

  \item \(x \in L(r)\) and \(x \in L(s)\),

  \item \(x \not\in L(r)\) and \(x \not\in L(s)\).

\end{enumerate*}