\chapter{The Basics of Classification \label{chapter:classification}}

Classification is a form of supervised learning in which our goal is to learn a mapping between some features, $x$, and an output, $y$. In classification, the output, $y$, is a category. In \textbf{binary classification} (by far the most common), there are only two categories: yes or no, usually represented as ``0'' (no) or ``1'' (yes). In \textbf{multi-class classification}, there are more than two categories.

To learn an appropriate mapping, we feed \textbf{training data} to a \textbf{learning algorithm}. Different algorithms learn different types of mappings.

\section{Definitions}

\begin{itemize}
\item \textbf{Training data:} The data used, along with an appropriate learning algorithm, to create the mapping between input and output. It is composed of \textbf{training examples}, a.k.a. \textbf{samples}, each consisting of one or more input features and a single output.
\item \textbf{Test data:} An independent dataset, not used in model training, on which the performance of a trained supervised learning model is evaluated.
\item \textbf{Feature:} Also known as a \textbf{predictor}, or \textbf{covariate}, one of the inputs to a supervised learning algorithm.
\item \textbf{Output:} Also known as the \textbf{outcome}, or \textbf{label}, the thing you are trying to predict.
\item \textbf{Feature space:} Envisioning each feature as having its own axis that is orthogonal to all of the other features' axes, the multidimensional space spanned by those axes (or rather: unit vectors in the directions of those axes)
\item \textbf{Extrapolation:} Making predictions outside the region of the feature space occupied by the training data. This will often lead to errors.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Visualizing the Classification Problem \label{section:visualizingclass}}

Imagine we want to predict whether a patient will be readmitted to the emergency room (ER) within $30$~days of hospital discharge. We gather data on two predictors: a disease severity score ($x_1$), which characterizes the severity of illness, and a social determinants score ($x_2$), which characterizes the patient's socioeconomic status. We have data on $200$~patients.

\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-just-data.png}
\end{center}

In this figure, the color refers to whether a patient was readmitted (blue = ``no'', red = ``yes''). The location of each point is governed by the patient's disease severity score ($x_1$, horizontal axis) and social determinants score ($x_2$, vertical axis). Our goal in classification is to draw a \textbf{decision boundary}\index{decision boundary} through this space, on one side of which we will predict that the patient is readmitted, and on the other side not.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Three Classification Algorithms}

\subsection{Logistic Regression \label{ssect:logreg}}

The simplest decision boundary is, arguably, a line. The logistic regression\index{logistic regression} algorithm simply draws a line\footnote{In a higher-dimensional feature space, the decision boundary for logistic regression is a \textbf{hyperplane}.} through the feature space that divides the positive and negative training examples.

\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-logistic.png}
\end{center}

\noindent The output of a fitted logistic regression model from R looks like this:

\begin{center}
\includegraphics[width=0.7\textwidth]{img/glm-binomial-example.png}
\end{center}

\noindent The equation of the line (or, in higher dimensions, hyperplane) that forms the decision boundary in logistic regression can be obtained by setting the linear sum of coefficients of this model equal to zero.
\begin{align*}
0.9780 + 0.1344 x_1 - 1.3981 x_2 = 0 \\
\implies x_2 = \frac{0.9780 + 0.1344 x_1}{1.3981}
\end{align*}

\noindent At any point, $(x_1, x_2)$, in the feature space, the model's predicted probability of a positive outcome (i.e. probability of an ER readmission) is related to the coefficients by this equation
$$ \log \frac{P[Y=1]}{1 - P[Y=1]} = 0.9780 + 0.1344 x_1 - 1.3981 x_2 $$
The decision boundary occurs when $P[Y=1] = 0.5$ (total uncertainty, e.g. a coin toss). Another way to write this equation is:
$$ P[Y=1] = \frac{1}{1 + \exp(-(0.9780 + 0.1344 x_1 - 1.3981 x_2))} $$
The functional form on the right, $1/(1 + \exp(-z))$, is called the \textbf{logistic function}; this is how logistic regression got its name. We will learn much more about the math behind logistic regression in subsequent chapters.

\subsection{K Nearest Neighbors (KNN)}

Another -- completely different -- approach to classification is to start with no assumptions about the shape of the decision boundary. To make a prediction about a new patient, we simply identify the $K$ nearest neighbors to that patient from our training set and allow them to vote on whether or not the new patient will be readmitted. The parameter $K$ must be set independently and is called a \textbf{hyperparameter}.

\noindent Here is the decision boundary for KNN with $K=15$:
\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-knn-15.png}
\end{center}

\subsection{Decision Tree \label{ssect:class_decision_tree}}

Finally, we may choose to use our training data to build a decision tree\index{decision tree}, which will allow us to make predictions on new patients using a series of simple yes/no questions. There are different decision tree learning algorithms, but here is the tree produced by a famous one called CART:
\begin{center}
\includegraphics[width=0.6\textwidth]{img/esl-decision-tree-just-tree.png}
\end{center}
And here is the decision boundary produced by this tree:
\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-decision-tree.png}
\end{center}

\begin{question}{}
How can you tell, just by looking at these images, which feature ($x_1$ or $x_2$) impacts the outcome the most? Which one is it?
\end{question}

\begin{question}{}
There are six rectangular regions in the picture of the decision tree decision boundary. Each corresponds to one of the six leaves of the tree. Identify all six and which leaves they correspond to on the decision tree.
\end{question}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Classification with Probabilities}

We can think of classification as simply drawing a decision boundary, but underlying each algorithm is a quantitative assessment of each point in the feature space. Each algorithm is, in its own way, able to provide a degree of certainty, or \textbf{probability}\footnote{Pedantic footnote: this is a Bayesian definition of probability, as opposed to a frequentist definition. More on that later.}, that a point belongs to the positive outcome class.

For example, here is the feature space of the example we just saw, colored by the probability, according to logistic regression, that a sample at each point should be classified as positive (i.e. the patient will be readmitted to the ER):
\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-logistic-prob.png}
\end{center}

The solid line is the decision boundary, and the dashed lines indicate where the probability of a positive outcome (ER readmission) is 25\% (top line) and 75\% (bottom line). You can see that the color of the background gets purer red or purer blue the further you get from the decision boundary, but that near the decision boundary, the color is rather murky. That murkiness reflects the algorithm's uncertainty about the outcome. At the decision boundary, it is maximally uncertain. There the probability of a positive outcome is 50\%: a coin toss. Here is a similar plot for KNN ($K=15$):
\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-knn-15-prob.png}
\end{center}

You can see that the shapes of the 25\% and 75\% probability lines have much more complex shapes than for logistic regression, but the story is the same: you have regions of pure blue or red, where the algorithm is certain, and you have a murky region near the decision boundary. Now, finally, here is the same plot for the decision tree:
\begin{center}
\includegraphics[width=0.7\textwidth]{img/esl-decision-tree-prob.png}
\end{center}
The color of the background in the regions corresponding to the six leaves of the tree is the same throughout each region. That's because the probability in each rectangular region (corresponding to each leaf of the tree) is constant. It equals the number of red dots in that region divided by the total number of dots.

\begin{question}{}
What are the advantages and disadvantages of each algorithm?
  \begin{enumerate}
  \item Logistic regression?
  \item KNN ($K=15$)?
  \item Decision tree?
  \end{enumerate}
\end{question}

\begin{question}{}
What makes a good classification algorithm? Consider issues of accuracy, generalizability, and speed (both to train the algorithm and to use it to make predictions on new samples).
\end{question}


\section*{Week 6A: Financial Statement Analysis and Ratios}

\subsection*{Solvency and Liquidity Ratios}

$Debt/EquityRatio = \frac{Total Liabilibies}{Total Shareholder's Equity}$ \\

$Leverage Ratio = \frac{TotalAssets}{Total Shareholder's Equity}$ \\

$Leverage Ratio = \frac{A}{L}=\frac{E+L}{L} = 1+ Debt/EquityRatio$

$Current Ratio = \frac{Current Assets}{Current Liabilities}$ \\

$Working Capital = \\ Current Assets - Current Liabilities $

\subsection*{Solvency and Liquidity Ratios}

$Net Margin = \frac{Net Income}{Revenue}$ \\

$Gross Margin = \frac{Revenue - COGS}{Revenue}$ \\

$ROA = \frac{Net Income}{Total Assets}$ \\

$ROE = \frac{Net Income}{Shareholders' Equity}$

\subsection*{Operating Efficiency}

$Asset Turnover = \frac{Revenue}{Total Assets}$  \\

$A/R Turnover = \frac{Revenue}{Net Accounts Receivable}$  \\

$Inventory Turnover = \frac{COGS}{Inventory}$  \\

$Days Receivable = \frac{365}{A/R Turnover}$

\subsection*{DuPont Decomposition}

$ROE = \frac{NI}{Equity}$ \\

$ROE = \frac{NI}{Assets} \frac{Assets}{Equity} = ROA \cdot Leverage$  \\

$ROE = \frac{NI}{Sales} \frac{Sales}{Assets}\frac{Assets}{Equity} \\
 ROE = ProfitMargin \cdot AssetTurnover \cdot Leverage $  \\

\section*{Week 6B: Income Taxes}

$AccountingIncome \neq Taxable Income$ \\
$Tax Expense \neq Cash Taxes $ \\

\subsection*{Deferred Tax Liability - DTL}

Deferred tax liabilities increase when a timing difference leads to: \\
 $PretaxIncome_{GAAP}>Taxable Income_{TaxCode}	$

Balance sheet equation for cash paid in tax vs tax liability and income tax expense:
\begin{tabular}{ |c||c|c| }
	\hline
	  Assets = & Liab +  & S/E	 \\
	\hline
	  CashTax & DefTaxLiab  & InTaxExp	 \\

	\hline
\end{tabular}

If a company has a net deferred tax liability on its balance sheet, cash taxes in the future will be higher than future tax expense.

\subsection*{ Deferred Tax Assets - DTA}

Deferred tax assets increase when a timing difference leads to: \\
 $PretaxIncome_{GAAP}<Taxable Income_{TaxCode}	$ \\
more tax cash early, less cash taxes later.  $DefTaxAsset$ is similar to prepaid expense:

\begin{tabular}{ |c||c|c| }
	\hline
	Assets = & Liab +  & S/E	 \\
	\hline
	CashTax  DefTaxAsset &  & InTaxExp	 \\

	\hline
\end{tabular}

\subsection*{ Tax Disclosures}

When the tax rate falls, the  DTA (or DTL) shrinks. When a company has net DTL, we can think of this shrinking DTL as a one-time tax benefit which will reduce tax expense.

The DTL will shrink by the ratio of the rates:  $\frac{tax_{new}}{tax_{old}}$ \\

$ DTL_{new} = DTL _{old} \frac{tax_{new}}{tax_{old}} $ \\

$ \Delta DTL = DTL_{new} - DTL_{old} = (\frac{tax_{new}}{tax_{old}}-1)DTL_{old}$

\begin{tabular}{ |c||c|c| }
	\hline
	Assets = & Liab +  & S/E	 \\
	\hline
	& $\Delta DTL$ &   $-\Delta DTL$	 \\

	\hline
\end{tabular}

Similarly when a company has a net DTA: \\

$ \Delta DTA = DTA_{new} - DTA_{old} = (\frac{tax_{new}}{tax_{old}}-1)DTA_{old}$

\begin{tabular}{ |c||c|c| }
	\hline
	Assets = & Liab +  & S/E	 \\
	\hline
	 $\Delta DTA$ &  & $\Delta DTA$	 \\

	\hline
\end{tabular}

\subsection*{ Effective Tax Rate}

$EffectiveTaxRate = \frac{TaxExpense}{GAAPpretaxIncome}$  \\
$ pretaxIncome = NetIncome + TaxExpense $ \\
$EffectiveTaxRate = \frac{TaxExpense}{TaxExpense+NetIncome}$

\subsection*{DTAs and Valuation Allowance}

Deferred tax assets arise when future taxes payable will be less than future tax expense. DTAs are like “pre-paid” assets.
Firms reduce deferred tax assets by creating a valuation allowance, a contra-asset that is
similar to the allowance for doubtful accounts.\\

Example: In 2015, a firm has a \$30,000 deferred tax asset.
Suppose instead, at end of 2016, management expects that it will not have enough future income
to use the DTA:
\begin{tabular}{ |c|c||c|c| }
	\hline
	Asset &-ContraAsset = & Liab +  & S/E	 \\
	\hline
	& $30000$ &  & $-30000$	 \\
	\hline
\end{tabular}


% !TeX program = lualatex
\documentclass[]{article}

\usepackage{caption,subcaption,graphicx,float,url,amsmath,amssymb,amsthm,tocloft,cancel,thmtools,gensymb,braket,tikz-feynman,mathtools,color, colortbl}
\usepackage[toc,nonumberlist]{glossaries}
\usepackage{glossaries-extra}
\usepackage[toc,page]{appendix}

\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}
\renewcommand{\thesection}{1.\arabic{section}}
\newtheorem{thm}{Theorem}
\newtheorem{defn}[thm]{Definition}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lemma}[thm]{Lemma}
\graphicspath{{figs/}}
\widowpenalty10000
\clubpenalty10000
\setcounter{tocdepth}{2}
\tikzfeynmanset{compat=1.1.0}
\definecolor{Gray}{gray}{0.5}
\DeclareMathOperator{\Tr}{Tr \;}
\newcommand{\Lagr}{\mathcal{L}}
\setlength{\cftsubsecindent}{0em}
\setlength{\cftsecnumwidth}{3em}
\setlength{\cftsubsecnumwidth}{3em}

%opening
\title{Quantum Field Theory\\
Part I: Motivation and Foundation}
\author{Simon Crase (compiler)\\simon@greenweaves.nz}

\begin{document}

\maketitle

\begin{abstract}

This document contains derivations of equations from \cite[Part I: Motivation and Foundation]{zee2010quantum}.

\end{abstract}

\tableofcontents

\section{Path Integral Formulation}

\begin{thm}[Some useful integrals]
	\begin{align*}
		\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}x^2} =& \sqrt{2\pi} \numberthis \label{eq:integral:gaussian0}\\
		\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} =& \bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2} \numberthis \label{eq:integral:gaussian}\\
		\int_{-\infty}^{\infty} dx\,e^{-\frac{a}{2} x^2+Jx}=&\bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2}e^\frac{J^2}{2a} \numberthis \label{eq:integral:gaussian1}\\
		\int_{-\infty}^{\infty} dx\,e^{-\frac{a}{2} x^2+iJx}=&\bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2}e^{-\frac{J^2}{2a}} \numberthis \label{eq:integral:gaussian2}
	\end{align*}
\end{thm}

\begin{proof}
	\begin{align*}
		\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}x^2}\big)\big(\int_{-\infty}^{\infty} dy e^{-\frac{1}{2}y^2}\big) =& \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} dx dy e^{-\frac{1}{2}(x^2+y^2)} \\
		=& \int_{0}^{2\pi} d\theta \int_{0}^{\infty}dr\;r\;e^\frac{-r^2}{2} \text{, substituting $x=r\cos{\theta}$,$y=r\sin{\theta}$}\\
		=&- 2\pi \int_{0}^{\infty}du e^{-u}\;\text{, substituting $u=\frac{-r^2}{2}$}\\
		=&- 2\pi e^{-u}\bigg\vert_0^\infty\\
		=& 2\pi
	\end{align*}
	Taking square roots of both side, we get \eqref{eq:integral:gaussian0}.
	\begin{align*}
		\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} =&\frac{1}{\sqrt{a}} \int_{-\infty}^{\infty} dx^\prime e^{-\frac{1}{2}{x^\prime}^2} \text{, substituting $x^\prime = \sqrt{a}x$} \\
		=& \bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2} \text{, from \eqref{eq:integral:gaussian0}, 	which gives \eqref{eq:integral:gaussian}}
	\end{align*}

	\begin{align*}
		\int_{-\infty}^{\infty} dx\,e^{-\frac{a}{2} x^2+Jx}=&\int_{-\infty}^{\infty} dx\,e^{-\frac{a}{2} \big[x^2-2 \big(\frac{J}{a}\big)x+\big(\frac{J}{a}\big)^2\big]} e^{\frac{a}{2}\big(\frac{J}{a}\big)^2}\text{, completing the square}\\
		=&e^\frac{J^2}{2a}\int_{-\infty}^{\infty} dx e^{-\frac{a}{2} \big[x-\frac{J}{a}\big]^2} \text{, now substitute $x^\prime=x-\frac{J}{a}$}\\
		=&e^\frac{J^2}{2a}\int_{-\infty}^{\infty} dx^\prime e^{-\frac{a}{2} {x^\prime}^2}
	\end{align*}
	Substituting \eqref{eq:integral:gaussian} gives \eqref{eq:integral:gaussian1}. Replacing $J$ by $iJ$ gives \eqref{eq:integral:gaussian2}.
\end{proof}

\begin{thm}[Feynman Path Integral--Exercise I.2.1 - eq(5)]
	If the Hamiltonian is given by
	\begin{align*}
		H =& \frac{\hat{p}^2}{2m} + V(\hat{q}) \text{, and the corresponding Lagrangian is} \numberthis \label{eq:H} \\
		L =& \frac{\hat{p}^2}{2m} - V(\hat{q}) \text{, then} \numberthis \label{eq:L}\\
		\braket{q_F\vert H\vert q_I} =& \int Dq(t) e^{i \int_{0}^{T}dt L} \text{, where}\numberthis \label{eq:Path:itegral}\\
		\int Dq(t)\triangleq& \lim_{N\rightarrow\infty} \big(\frac{-i m}{2\pi \delta t}\big)^\frac{N}{2} \big(\prod_{j=1}^{N-1} \int dq_j\big) \numberthis \label{eq:D}
	\end{align*}
\end{thm}
\begin{proof}
	For some N, define $\delta t =T/N$ and $q_j= q(j \delta t)$.

	\begin{align*}
		\braket{q_F\vert H\vert q_I} =& \big(\prod_{j=1}^{N-1} \int dq_j\big)\prod_{i=0}^{N-1} \braket{q_{j+1} \vert e^{-iHT} \vert q_j}\\
		=& \big(\prod_{j=1}^{N-1} \int dq_j\big)\prod_{i=0}^{N-1} \braket{q_{j+1} \vert e^{-i\big(\frac{\hat{p}^2}{2m} + V(\hat{q})\big)\delta t} \vert q_j} \text{, from \eqref{eq:H}}\\
		=& \big(\prod_{j=1}^{N-1} \int dq_j\big)\prod_{i=0}^{N-1} \braket{q_{j+1} \vert e^{-i\frac{\hat{p}^2}{2m} \delta t} e^{-i V(\hat{q}) \delta t} \vert q_j} \text{, we'll expand in eigenvectors of $\hat{q}$}\\
		=& \big(\prod_{j=1}^{N-1} \int dq_j\big)\prod_{i=0}^{N-1} \int dq \braket{q_{j+1} \vert e^{-i\frac{\hat{p}^2}{2m} \delta t}\vert q} \braket{q\vert e^{-i V(\hat{q}) \delta t} \vert q_j} \text{, since $\ket{q}\bra{q}=I$} \numberthis \label{eq:qHq}
	\end{align*}
	 We will evaluate the integrand in two steps, starting with the potential energy.
	\begin{align*}
		\braket{q\vert e^{-i V(\hat{q}) \delta t} \vert q_j} =& \braket{ e^{i V(\hat{q}) \delta t} q \vert q_j}\\
		=& \braket{ e^{i V(q) \delta t} q \vert q_j} \text{, since $\ket{q}$ is an eigenvector, with eigenvalue $q$}\\
		=& \braket{ e^{i V(q) \delta t} q \vert q_j} \\
		=& e^{-i V(q) \delta t} \braket{ q \vert q_j}  \numberthis \label{eq:qVq}
	\end{align*}
	Now for the kinetic energy.
	\begin{align*}
		\braket{q_{j+1} \vert e^{-i\frac{\hat{p}^2}{2m} \delta t}  \vert q}=& \int \frac{dp}{2\pi}\braket{q_{j+1} \vert e^{-i\frac{\hat{p}^2}{2m} \delta t}  \vert p} \braket{p\vert q}\\
		=& \int \frac{dp}{2\pi} e^{-i\frac{p^2}{2m} \delta t} \braket{q_{j+1} \vert p} \braket{p\vert q} \text{, since $\ket{p}$ is an eigenvector of $\hat{p}$}\\
		=& \int \frac{dp}{2\pi} e^{-i\frac{p^2}{2m} \delta t} e^{ip q_{j+1}} e^{-ip q}
	\end{align*}
	Using \eqref{eq:integral:gaussian2} with $a=\frac{i \delta t}{m}$ and $J=q_{j+1}-q$:
	\begin{align*}
	\braket{q_{j+1} \vert e^{-i\frac{\hat{p}^2}{2m} \delta t}  \vert q}=& \int \frac{dp}{2\pi} e^{\frac{-i \delta t}{2m}p^2} e^{ip [q_{j+1}-q]} \\
	=& \frac{1}{2\pi} \big(\frac{2\pi m}{i \delta t}\big)^\frac{1}{2} e^{-\frac{m (q_{j+1}-q)^2}{2 i \delta t}}\\
	=& \big(\frac{-i m}{2\pi \delta t}\big)^\frac{1}{2} e^{i \delta t \frac{m (q_{j+1}-q)^2}{2  {\delta t}^2}}  \numberthis \label{eq:qP2q}
	\end{align*}
	Substituting \eqref{eq:qVq} and \eqref{eq:qP2q} in \eqref{eq:qHq}:
	\begin{align*}
		\braket{q_F\vert H\vert q_I} =& \big(\frac{-i m}{2\pi \delta t}\big)^\frac{N}{2} \big(\prod_{j=1}^{N-1} \int dq_j\big) \prod_{i=0}^{N-1} \big(\frac{-i m}{2\pi \delta t}\big)^\frac{1}{2} \int dq e^{i \delta t \frac{m (q_{j+1}-q)^2}{2  {\delta t}^2}} e^{-i V(q) \delta t} \underbrace{\braket{ q \vert q_j}}_\text{$=m^2$}\delta(q-q_j) \\
		=& \big(\frac{-i m}{2\pi \delta t}\big)^\frac{N}{2} \big(\prod_{j=1}^{N-1} \int dq_j\big) \prod_{j=0}^{N-1}  e^{i \delta t \bigg[\frac{m (q_{j+1}-q_j)^2}{2  {\delta t}^2} - V(q_j)\bigg]}\\
		=& \big(\frac{-i m}{2\pi \delta t}\big)^\frac{N}{2} \big(\prod_{j=1}^{N-1} \int dq_j\big)  e^{i  \sum_{i=0}^{N-1} \delta t \bigg[\frac{m (q_{j+1}-q_j)^2}{2  {\delta t}^2} - V(q_j)\bigg]} \numberthis \label{eq:big:feynman:sum}
	\end{align*}
	Now, as $N\rightarrow\infty$
	\begin{align*}
	\sum_{i=0}^{N-1} \delta t \rightarrow &\int dt\\
	\frac{m (q_{j+1}-q_j)^2}{2  {\delta t}^2} \rightarrow & \frac{m \dot{q}}{2} \text{, so, substituting \eqref{eq:L} and \eqref{eq:D}, \eqref{eq:big:feynman:sum} becomes}\\
	\braket{q_F\vert H\vert q_I} \rightarrow& \int Dq(t) e^{i \int_{0}^{T}dt L} \text{i.e. \eqref{eq:Path:itegral}}
	\end{align*}
\end{proof}

\begin{thm}
	\begin{align*}
		\frac{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}x^{2n}}{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}} =&\frac{\prod_{k=1}^{n}\big(2k-1\big)}{a^n}
	\end{align*}
\end{thm}

\begin{proof}
	The proof by mathematical induction starts by defining the proposition $P(n)$:
	\begin{align*}
		P(n) \equiv& \bigg[\frac{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}x^{2n}}{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}}=\frac{\prod_{k=1}^{n}\big(2k-1\big)}{a^n}\bigg]
	\end{align*}
	\begin{lemma}\label{thm:P_0}
		P(0) is true.
	\end{lemma}
	\begin{proof}
		$P(0)$ reduces to $\frac{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}}{\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}}=1$
	\end{proof}
    \begin{lemma}\label{lemma:uniform:convergence}
    	\begin{align*}
    		\forall \alpha >0 \text{, }\frac{d}{d\alpha}\int_{-\infty}^\infty dx e^{-\frac{1}{2}\alpha x^2}x^{2n} =&\int_{-\infty}^\infty dx \frac{\partial}{\partial \alpha} \big[e^{-\frac{1}{2}\alpha x^2}x^{2n}\big]
    	\end{align*}
    \end{lemma}
	\begin{proof}
		\begin{align*}
			\forall a>0, \forall \alpha_0 \in (0,\alpha)\; \int_{a}^\infty dx e^{-\frac{1}{2}\alpha x^2}x^{2n} =&\int_{a}^\infty dx e^{-\frac{1}{2}\alpha_0 x^2} e^{-\frac{1}{2}(\alpha-\alpha_0) x^2}x^{2n} \\
			\text{Now define}\; M(x) =& e^{-\frac{1}{2}\alpha_0 x^2}
		\end{align*}
		We can choose $a$ large enough that $e^{-\frac{1}{2}(\alpha-\alpha_0) x^2}x^{2n}$ is monotone decreasing for $x>a$. Clearly
		\begin{align*}
			e^{-\frac{1}{2}\alpha_0 x^2} e^{-\frac{1}{2}(\alpha-\alpha_0) x^2}x^{2n} \in C\\
				e^{-\frac{1}{2}\alpha_0 x^2} \in C\\
			e^{-\frac{1}{2}\alpha_0 x^2} e^{-\frac{1}{2}(\alpha-\alpha_0) x^2}x^{2n}<&M(x)\; \forall x>a \text{ and}\\
			\int_{a}^\infty dx M(x)<&\infty
		\end{align*}
		 for $\alpha$ in closed interval [A,B] that includes $\alpha_0$; the Weierstrass M-Test \cite[Chapter 10, 6.1]{widder1961advanced} shows that the integral converges uniformly for $\alpha\in[A,B]$. Hence we can differentiate under the integral sign \cite[Chapter 10, 8.3]{widder1961advanced}.
		Now
		\begin{align*}
			\int_{-\infty}^\infty dx e^{-\frac{1}{2}\alpha x^2}x^{2n} =& 2 \int_{a}^\infty dx e^{-\frac{1}{2}\alpha x^2}x^{2n} + \int_{-a}^a dx e^{-\frac{1}{2}\alpha x^2}x^{2n}
		\end{align*}
	Noting that $e^{-\frac{1}{2}\alpha x^2}x^{2n} \in C^1$ for x in [-a,a], we can also differentiate the second integral.
	\end{proof}

	\begin{lemma}\label{thm:P_1}
		$P(0) \implies P(1)$
	\end{lemma}
	\begin{proof}
		Since (\ref{eq:integral:gaussian}) converges uniformly, Lemma \ref{lemma:uniform:convergence} allows us to differentiate under the integral sign\cite{widder1961advanced}.
		\begin{align*}
			-2\frac{d}{da}\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} =& -2\frac{d}{da} \bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2}\\
			\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} x^2 =& \bigg(\frac{2\pi}{a^3}\bigg)^\frac{1}{2}\\
			=& \bigg(\frac{2\pi}{a}\bigg)^\frac{1}{2} \frac{1}{a}\\
			=& \int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} \frac{1}{a}
		\end{align*}
	\end{proof}
	\begin{lemma}\label{thm:P_n}
		$P(n) \land P(1)\implies P(n+1)$
	\end{lemma}
	\begin{proof}
		\begin{align*}
		P(n) \implies &\\
		\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}x^{2n}=&\prod_{k=1}^{n}\big(2k-1\big)	\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2} \frac{1}{a^n}\\
		-2\frac{d}{da}\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}x^{2n}=&-2 \prod_{k=1}^{n}\big(2k-1\big)\frac{d}{da} \big[	\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2} \frac{1}{a^n}\big]\\
		\int_{-\infty}^\infty dx e^{-\frac{1}{2}ax^2}x^{2(n+1)}=&-2 \prod_{k=1}^{n}\big(2k-1\big)\bigg[\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} (-\frac{1}{2}x^2)\frac{1}{a^n}-\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}\frac{n}{a^{n+1}}\bigg] \\
		=&\prod_{k=1}^{n}\big(2k-1\big)\bigg[\underbrace{\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}x^2}_\text{Now we apply $P(1)$} \frac{1}{a^n}+2\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}\frac{n}{a^{n+1}}\bigg]\\
		=&\prod_{k=1}^{n}\big(2k-1\big)\bigg[ \int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}\frac{1}{a} \frac{1}{a^n} + \int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2} \frac{2n}{a^{n+1}}\bigg]\\
		=&\prod_{k=1}^{n}\big(2k-1\big) \frac{2n+1}{a^{n+1}}\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}\\
		=&\prod_{k=1}^{n+1}\big(2k-1\big) \frac{1}{a^{n+1}}\int_{-\infty}^{\infty} dx e^{-\frac{1}{2}ax^2}\\
		\equiv& P(n+1)
		\end{align*}
	\end{proof}
	Summarizing:
	\begin{align*}
		\text{Lemma }\ref{thm:P_0} \implies& P(0)\\
		P(0) \land \text{Lemma } \ref{thm:P_1} \implies& P(1)\\
		P(0) \land P(1)\land P(n) \land \text{Lemma } \ref{thm:P_n} \implies& P(n+1)
	\end{align*}
\end{proof}

\begin{thm}
	If $A$ is a symmetric matrix
	\begin{align*}
		\int_{-\infty}^{\infty}dx_1 dx_2...dx_N e^{-\frac{1}{2}\vec{x}A\vec{x} + \vec{J} \cdot \vec{x}} =& \bigg(\frac{(2\pi)^N}{\vert A \vert}\bigg)^\frac{1}{2} e^{\frac{1}{2}\vec{J}\cdot A^{-1} \vec{J}} \numberthis \label {eq:Zee_I_2_22}
	\end{align*}
\end{thm}
\begin{proof}
	Since $A$ is symmetric, there exists an orthogonal matrix $O$ such that $A=O^T D O$, where $D$ is a diagonal matrix--\cite{bellman1970introduction}. Define
	\begin{align*}
		y_i =& \sum_j O_{ij}x_j\\
		\int_{-\infty}^{\infty}dx_1 dx_2...dx_N e^{-\frac{1}{2}\vec{x}A\vec{x} + \vec{J} \cdot \vec{x}}=&\bigg(\frac{1}{\vert O \vert}\bigg)^N \int_{-\infty}^{\infty}dy_1 dy_2...dy_Ne^{-\frac{1}{2}\vec{x}O^T D O\vec{x} + \vec{J} \cdot \vec{x}}\\
		=& \int_{-\infty}^{\infty}dy_1 dy_2...dy_N e^{-\frac{1}{2}(\vec{x}O^T) D (O\vec{x}) + \vec{J} \cdot \vec{x}}\\
		=& \int_{-\infty}^{\infty}dy_1 dy_2...dy_N e^{- \frac{1}{2} \vec{y}D \vec{y} + (O\vec{J})\cdot \vec{y}}\\
		=& \prod_{i=1}^N \int_{-\infty}^{\infty}dy_i e^{- \frac{1}{2} D_{ii}y_i^2 + (OJ)_i y_i}\\
		=& \prod_{i=1}^N \bigg[\bigg(\frac{2\pi}{D_{ii}}\bigg)^\frac{1}{2} e^{\big(\frac{[(OJ)_i]^2}{2D_{ii}}\big)}\bigg] \numberthis \label {eq:Zee_I_2_22_a}
	\end{align*}
	But
	\begin{align*}
		\prod_{i=1}^N \bigg(\frac{2\pi}{D_{ii}}\bigg) =& \frac{({2\pi})^N}{\vert D \vert}\\
		=& \frac{({2\pi})^N}{\vert A \vert} \text{, since $O$ is orthogonal} \numberthis \label {eq:Zee_I_2_22_b}
	\end{align*}
	And
	\begin{align*}
		\prod_{i=1}^N e^{\big(\frac{[(OJ)_i]^2}{2D_{ii}}\big)} =& e^{\sum_{i=1}^N {\big(\frac{[(OJ)_i]^2}{2D_{ii}}\big)}}\\
		\sum_{i=1}^N {\big(\frac{[(OJ)_i]^2}{2D_{ii}}\big)} =& \frac{1}{2} \sum_{i=1}^N (OJ)_i (D^{-1})_{ii} (OJ)_i\\
		 =& \frac{1}{2} \sum_{i,j,k,l=1}^N O_{ik} J_k (D^{-1})_{ij} O_{jl} J_l\\
		 =& \frac{1}{2} \sum_{i,j,k,l=1}^N J_k O^T_{ki}  (D^{-1})_{ij} O_{jl} J_l \\
		 =& \frac{1}{2} \sum_{k,l=1}^N J_k \bigg(\sum_{k,l=1}^N O^T_{ki}  (D^{-1})_{ij} O_{jl}\bigg) J_l\\
		  \\
		 =& \frac{1}{2} \sum_{k,l=1}^N J_k \big(A^{-1}\big)_{kl} J_l \\
		 =& \frac{1}{2} J A^{-1} J \numberthis \label {eq:Zee_I_2_22_c}
	\end{align*}
	Substituting (\ref{eq:Zee_I_2_22_b}) and (\ref{eq:Zee_I_2_22_c}) in (\ref{eq:Zee_I_2_22_a}), we obtain (\ref{eq:Zee_I_2_22}).
\end{proof}

\section{From Field to Particle to Force}
\begin{align*}
	W(J) =& - \frac{1}{2} \int \int d^4x d^4y J(x) D(x-y) J(y)\\
	D(x-y) =& \int \frac{d^4k}{(2\pi)^4} \frac{e^{ik(x-y)}}{k^2-m^2+i\epsilon}\\
	J(x) =& J_1(x) + J_2(x) \text{, where}\\
	J_{a}(x) =&\delta^{(3)}(\vec{x}-\vec{x_a})
\end{align*}
Considering only the cross terms
\begin{align*}
	W(J) =& - \int \frac{1}{(2\pi)^4} \cancel{2} \cancel{\frac{1}{2}} \int \int d^4x d^4y d^4k \delta^{(3)}(\vec{x}-\vec{x_1})   \frac{e^{ik(x-y)}}{k^2-m^2+i\epsilon} \delta^{(3)}(\vec{y}-\vec{x_2}))\\
	=& - \frac{1}{(2\pi)^4} \int \int \int \int \int \int dx^0 d\vec{x} dy^0 d\vec{y} dk^0 d\vec{k}  \delta^{(3)}(\vec{x}-\vec{x_1})   \frac{e^{ik^0(x^0-y^0)-i\vec{k} \cdot (\vec{x}-\vec{y})}}{k^2-m^2+i\epsilon} \delta^{(3)}(\vec{y}-\vec{x_2}))\\
	=& - \frac{1}{(2\pi)^4} \int \int \int \int \int dx^0 d\vec{x} dy^0 dk^0 d\vec{k}  \delta^{(3)}(\vec{x}-\vec{x_1})   \frac{e^{ik^0(x^0-y^0)-i\vec{k} \cdot (\vec{x}-\vec{x_2})}}{k^2-m^2+i\epsilon} \\
	=& - \frac{1}{(2\pi)^4} \int \int \int \int dx^0  dy^0 dk^0 d\vec{k} \;    \frac{e^{ik^0(x^0-y^0)-i\vec{k} \cdot (\vec{x_1}-\vec{x_2})}}{k^2-m^2+i\epsilon}\\
	=& - \frac{1}{(2\pi)^3} \int \int dx^0  dy^0 \underbrace{\int \frac{dk^0}{2\pi} e^{ik^0(x^0-y^0)}}_\text{$\delta(x^0-y^0)$} \int d\vec{k} \; \frac{e^{i\vec{k} \cdot (\vec{x}-\vec{y})}}{k^2-m^2+i\epsilon}\\
	=& - \frac{1}{(2\pi)^3} \int  dx^0  \int d\vec{k} \; \frac{e^{i\vec{k} \cdot (\vec{x}-\vec{y})}}{k^2-m^2+i\epsilon}
\end{align*}

\section{Coulomb and Newton}
The Lagrangian Density is given by:
\begin{align*}
	\Lagr =& -\frac{1}{4} F_{\mu\nu}F^{\mu\nu} + \frac{1}{2}m^2 A_{\mu}A^{\mu} + A_{\mu}J^{\mu} \text{, where}\\
	F_{\mu\nu} =& \partial_{\mu} A_{\nu} - \partial_{\nu} A_{\mu} \text{ and}\\
	\partial_{\mu}J^{\nu}=& 0 \text{. We define the action} \numberthis \label{eq:EM_continuity}\\
	S(A) =&\int d^4 \Lagr\\
	=&  \int d^4x \bigg[-\frac{1}{4} F_{\mu\nu}F^{\mu\nu} + \frac{1}{2}m^2 A_{\mu}A^{\mu} + A_{\mu}J^{\mu} \bigg]
\end{align*}
Expanding the first term in the Lagrangian
\begin{align*}
	F_{\mu\nu}F^{\mu\nu}=& g^{\mu\rho} g^{\nu\sigma}F_{\mu\nu}F_{\rho\sigma}\\
	=& g^{\mu\rho} g^{\nu\sigma} \big(\partial_{\mu} A_{\nu} - \partial_{\nu} A_{\mu}\big)\big(\partial_{\rho} A_{\sigma} - \partial_{\sigma} A_{\rho}\big)\\
	=& g^{\mu\rho} g^{\nu\sigma}\big[\partial_{\mu} A_{\nu}\partial_{\rho} A_{\sigma} -\partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho} -\partial_{\nu} A_{\mu}\partial_{\rho} A_{\sigma} + \partial_{\nu} A_{\mu}\partial_{\sigma} A_{\rho}\big]\\
	=& g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\rho} A_{\sigma} -g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho} -\underbrace{g^{\mu\rho} g^{\nu\sigma} \partial_{\nu} A_{\mu}\partial_{\rho} A_{\sigma}}_\text{substitute: $\mu\nu\rho\sigma\rightarrow\nu\mu\sigma\rho$} +\underbrace{g^{\mu\rho} g^{\nu\sigma} \partial_{\nu} A_{\mu}\partial_{\sigma} A_{\rho}}_\text{$\mu\nu\rho\sigma\rightarrow\nu\mu\sigma\rho$}	\\
	=& g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\rho} A_{\sigma} -g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho} - g^{\nu\sigma} g^{\mu\rho} \partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho} +g^{\nu\sigma} g^{\mu\rho} \partial_{\mu} A_{\nu}\partial_{\rho} A_{ssigma}	\\
	=& 2 \big[g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\rho} A_{\sigma} -g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho}\big]
\end{align*}
So the first term in the action is
\begin{align*}
	 -\frac{1}{4} \int d^4x F_{\mu\nu}F^{\mu\nu} =&  -\frac{2}{4} \int d^4x \big[g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\rho} A_{\sigma} -g^{\mu\rho} g^{\nu\sigma} \partial_{\mu} A_{\nu}\partial_{\sigma} A_{\rho}\big] \text{. Integrating by parts gives}\\
	 =& \frac{1}{2} \int d^4x \big[g^{\mu\rho} g^{\nu\sigma}  A_{\nu}\partial_{\mu}\partial_{\rho} A_{\sigma} -g^{\mu\rho} g^{\nu\sigma}  A_{\nu}\partial_{\mu} \partial_{\sigma} A_{\rho}\big]\\
	 =& \frac{1}{2} \int d^4x \big[ g^{\nu\sigma}  A_{\nu}\partial^2 A_{\sigma} -  A_{\nu}\partial^{\rho} \partial^{\nu} A_{\rho}\big]\\
	 =& \frac{1}{2} \int d^4x A_{\nu} \big[ g^{\nu\sigma}  \partial^2  -  \partial^{\sigma} \partial^{\nu} \big] A_{\sigma}
\end{align*}

So the action becomes
\begin{align*}
	\int d^4 \Lagr=&  \int d^4x \bigg[\frac{1}{2} A_{\nu} \big[ g^{\nu\sigma}  \partial^2  -  \partial^{\sigma} \partial^{\nu} \big] A_{\sigma} + \frac{1}{2}m^2 A_{\mu}A^{\mu} + A_{\mu}J^{\mu} \bigg]\\
	=&\int d^4x \bigg[\frac{1}{2} A_{\nu} \big[ g^{\nu\sigma}  (\partial^2 +m^2)  -  \partial^{\sigma} \partial^{\nu} \big] A_{\sigma} +  A_{\mu}J^{\mu} \bigg]
\end{align*}

\begin{align*}
	D_{\mu\nu} \big(D^{-1}\big)^{\nu\lambda} =& \delta_{\mu}^{\lambda}
\end{align*}
From Lecture. How do we get a repulsive force? This is tied up with spin. If we want particles fro field to have spin 1, we need a vector--$A_{\mu}$. We need to reduce $A_{\mu}$ to 3 degrees of freedom (polarization), and \eqref{eq:EM_continuity} is the only Lorentz invariant way to do this. We will start with the physics--Figure \ref{fig:electromagnetic:current}.
\begin{figure}[H]
	\begin{center}
		\caption{Electromagnetic Field with source and sink}\label{fig:electromagnetic:current}
		\includegraphics[width=0.5\textwidth]{qft_liecture2_1}
	\end{center}
\end{figure}
\begin{align*}
	J \phi \rightarrow& J_{\mu} A^{\mu}\\
	\frac{1}{k^2 + m^2} \rightarrow& \frac{1}{k^2 + m^2} \underbrace{\underbrace{\sum_{a=1}^{3}}_\text{3 polarizations} \epsilon_\mu^{(a)}(k) \epsilon_\nu^{(a)}(k)}_\text{A Lorentz tensor, $-G_{\mu\nu}$ say} \numberthis \label{eq:em:propagator}
\end{align*}
We will evaluate in the rest frame of the particle, where $k=(m,0,0,0)$, and polarization is the 3 directions in Cartesian space.
\begin{align*}
	\epsilon_\mu^{(1)}(k) =& (0,1,0,0)\\
	\epsilon_\mu^{(2)}(k) =& (0,0,1,0)\\
	\epsilon_\mu^{(3)}(k) =& (0,0,0,1)\\
	k^{\mu}\epsilon_\mu =& 0 \text{, since $k=(m,0,0,0)$} \numberthis \label{eq:k_mu_0}
\end{align*}
\eqref{eq:k_mu_0} is Lorentz Invariant! c.f. $\partial_{\mu}A^{\mu}=0$. We will establish the tensor $G_{\mu\nu}$ in \eqref{eq:em:propagator} using symmetry and Lorentz invariance. There are only two things to construct $G_{\mu\nu}$ from, $k_{\mu}$ and $g_{\mu\nu}$.
\begin{align*}
	- G_{\mu\nu} =& A k_{\mu} k_{\nu} + B g_{\mu\nu}\\
	 G_{\mu\nu} k^{\mu} =& 0 \text{ from \eqref{eq:k_mu_0}, whence}\\
	 \big(A k_{\mu} k_{\nu} + B g_{\mu\nu}\big) k^{\mu} =& 0\\
	 \big(A \underbrace{k^2}_\text{$=m^2$}+B\big)k_{\nu} =& 0\\
	 A =& -\frac{B}{m^2}\\
	 - G_{\mu\nu} =& -\big(g_{\mu\nu}-\frac{k_{\mu}k_{\nu}}{m^2}\big) \text{. Normalization fixed by rest frame.}\\
	 \frac{1}{k^2 + m^2} \rightarrow& \frac{-g_{\mu\nu}+\frac{k_{\mu}k_{\nu}}{m^2}}{k^2 + m^2}
\end{align*}
Imagine two stationary charges: then $J^i=0$
\begin{align*}
	W(J) =& \int  \frac{d^4k}{(2\pi)4} J^{\mu}(k)^* \frac{-g_{\mu\nu}+\frac{k_{\mu}k_{\nu}}{m^2}}{k^2 + m^2} J^{\nu}(k)\\
	\partial_{\mu} J^{\mu} =& 0 \text{ implies}\\
	k_{\mu} J^{\mu} =& 0\\
	W(J) =& \int  \frac{d^4k}{(2\pi)4} J^0(k)^* \frac{-g_{00}}{k^2 + m^2} J^0(k)\\
	=& \int  \frac{d^4k}{(2\pi)4} J^0(k)^* \frac{-1}{k^2 + m^2} J^0(k) \text{--Like charges repel}
\end{align*}

Spin 2 has two indices $h_{\mu\nu}$. Two sign switches cancel!

Even spin can exchange lumps, but not odd.

\begin{thm}
	\begin{align*}
		\big(\delta^2+m^2\big) A^{\mu}=&0 \text{ and} \numberthis \label{eq:cn:1}\\
		 \partial_{\mu} A^{\mu}=&0 \text{ are equivalent to } \numberthis \label{eq:cn:2}\\
		 \big(g^{\mu\nu}\partial^2-\partial^\mu\partial^\nu\big)A_\nu + m^2 A^\mu=&0 \numberthis \label{eq:cn:3}
	\end{align*}
\end{thm}
\begin{proof}[Proof: $\eqref{eq:cn:1} \land \eqref{eq:cn:2} \implies \eqref{eq:cn:3}$]
	 From	\eqref{eq:cn:2}
	\begin{align*}
		 \partial^\nu A_\nu =& 0 \text{, whence}\\
		 \partial^\mu \partial^\nu A_\nu =& 0 \text{. Now \eqref{eq:cn:1} can be rearranged} \numberthis \label{eq:cn:4}\\
		 \partial^2 \big(g^{\mu\nu}A_\nu\big) + m^2 A^\mu =&0\\
		 g^{\mu\nu} \partial^2 A_\nu + m^2 A^\mu =&0 \text{. Now, using \eqref{eq:cn:4}}\\
		 g^{\mu\nu} \partial^2 A_\nu - \partial^\mu \partial^\nu A_\nu + m^2 A^\mu =&0 \text{, which rearranges to \eqref{eq:cn:3}}
	\end{align*}
\end{proof}
\begin{proof}[$\eqref{eq:cn:3} \implies \eqref{eq:cn:1} \land \eqref{eq:cn:2}$]
	From \eqref{eq:cn:3}
	\begin{align*}
	 	m^2 A^\mu=&\big(\partial^\mu\partial^\nu-g^{\mu\nu}\partial^2\big)A_\nu \\
	 	m^2 \partial_\mu A^\mu =&\partial_\mu \big(\partial^\mu\partial^\nu-g^{\mu\nu}\partial^2\big)A_\nu\\
	 	=&\partial_\mu\partial^\mu\partial^\nu A_\nu -  g^{\mu\nu}\partial_\mu\partial^2A_\nu\\
	 	=&\partial^2\partial^\nu A_\nu - \partial^\nu\partial^2A_\nu\\
	 	=& 0 \text{, which is \eqref{eq:cn:2}. Now \eqref{eq:cn:3} becomes}\\
	 	g^{\mu\nu}\partial^2 A_\nu-\cancel{\partial^\mu\partial^\nu A_\nu} + m^2 A^\mu=&0 \text{, which is \eqref{eq:cn:1}}
	\end{align*}
\end{proof}

\section{Feynman Diagrams}

\begin{align*}
	Z(J,\lambda) =& \int D\phi \; e^{i\int d^4x\;\big[\frac{1}{2}[(\partial \phi)^2-m^2\phi^2]-\frac{\lambda}{4!}\phi^4+J\phi\big]}\\
	=& \int D\phi \; e^{i\int d^4x\;\big[\frac{1}{2}[(\partial \phi)^2-m^2\phi^2]+J\phi\big]e^{-i\frac{\lambda}{4!}\phi^4}}
\end{align*}

The \emph{functional derivative} is defined in \cite{enwiki:1034704989}:
\begin{align*}
	\int \frac{\delta F(x)}{\delta \rho} \phi(x) dx \triangleq& \lim_{\epsilon \rightarrow 0} \frac{F(\rho + \epsilon \phi)-f[\rho]}{\epsilon}\\
	=&\bigg[\frac{dF(\rho+\epsilon\phi)}{d\rho}\bigg]_\text{$\epsilon=0$}
\end{align*}

\begin{thm}
	\begin{align*}
		Z(J,\lambda) =&	e^{-\frac{i\lambda}{4!}\int dx \frac{\delta^4}{(\delta J)^4}}Z(J,0)
	\end{align*}
\end{thm}

\begin{proof}
	After \cite{straub2004feynman}...
\end{proof}

% Endmatter

\bibliographystyle{unsrt}
\addcontentsline{toc}{section}{Bibliography}
\raggedright
\bibliography{tm}

\end{document}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{pagecolor}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage{mathtools, nccmath}
\usepackage{courier}
\usepackage[overload]{empheq}
\usepackage[inline, shortlabels]{enumitem}
\usepackage{amsmath}
\usepackage{mathtools}
\color{white}
\title{Solution to Number Theory \# 1}
\author{@all.about.mathematics}

\pagecolor{black}
\begin{document}
\maketitle
\section{Problem}
Let $S\subset\mathbb{N}$ such that $|S|=n$. Prove that $\exists A\subseteq S$ such that the sum of all elements in $A$ are divisible by $n$.
\section{Solution}
Let $S=\{a_1,a_2,\cdots,a_n\}$.
Then define the following sums: $$S_1=a_1$$
$$S_2=a_1+a_2$$
$$\vdots$$
$$S_n=a_1+a_2+\cdots+a_n$$
\subsection{Case 1}
If one of the above sums are divisible by $n$, then the claim is proved in this case.
\subsection{Case 2}
If none of the sums are divisible by $n$, then, by the Pigeonhole Principle, at least 2 of the sums must have the same remainder when divided by $n$. This implies that we can pick 2 sums $S_b$ and $S_c$  with $b>c$ such that
$$S_b-S_c=a_c+1+\cdots+a_b\equiv0(mod\:n)$$
Therefore the claim is proved in this case.

\end{document}

Number theory/all.about.mathematics' questions/Number theory 1.tex


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\setlength{\parindent}{0pt}

\newtheorem*{theorem}{Theorem}
\newtheorem*{definition}{Definition}
\newtheorem*{lemma}{Lemma}
\newtheorem*{corollary}{Corollary}
\newtheorem{example}{Example}
\newtheorem*{trick}{Trick}
\newtheorem*{question}{Question}

\title{Lecture 4: Square Systems}
\author{}
\date{}

\begin{document}

\maketitle

\section{Plane Equations}

An equation of the form
$ax + by + cz = d \; \textnormal{(a, b, c, d are constants)}$ defines a plane.

\begin{example}
  Find the equation of the plane through the origin with a normal vector
  $\vec{N} = <1, 5, 10>$.

  By thinking geometrically, a point $P$ is in the plane $\iff$
  $OP \perp \vec{N}$ $\iff$ $\vec{OP} \cdot \vec{N} = 0$. Therefore, the
  equation of the described plane is
  \[
    <x, y, z> \cdot <1, 5, 10> = 0
  \]
  \[
    x + 5y + 10z = 0
  \]
\end{example}

\begin{example}
  Find the equation of the plane through the point $P_0$ (2, 1, -1) with a
  normal vector $\vec{N} = <1, 5, 10>$.

  Similarly by thinking geometrically, a point $P$ (x, y, z) is in the plane
  $\iff$ $\vec{P_0P} \perp \vec{N}$ $\iff$ $\vec{P_0P} \cdot \vec{N} = 0$.
  Therefore, the equation of the described plane is
  \[
    <x - 2, y - 1, z + 1> \cdot <1, 5, 10> = 0
  \]
  \[
    (x - 2) + 5(y - 1) + 10(z + 1) = 0
  \]
  \[
    x + 5y + 10z = -3
  \]
\end{example}

From the above example we can see that the coefficients of the equation of a
plane are actually the components of one of its normal vectors.

The right-hand side constant in the equation of a plane is an indicator of the
distance to its parallel plane through the origin. For example, we can derive
that $x + 5y + 10z = 3$ and $x + 5y + 10z = -3$ are in the two sides of the
plane $x + 5y + 10z = 0$ respectively, and $x + 5y + 10z = -1$ has a short
distance to $x + 5y + 10z = 0$ than $x + 5y + 10z = -3$.

\begin{example}
  The vector $\vec{v} = <1, 2, -1>$ and the plane $x + y + 3z = 5$ are \underline{A}.

  A. parallel \\
  B. perpendicular \\
  C. neither

  Reasons: \\
  A normal vector of the plane $x + y + 3z = 5$ is $\vec{N} = <1, 1, 3>$. Then
  \[
    \begin{split}
    \vec{v} \cdot \vec{N} &= <1, 2, -1> \cdot <1, 1, 3> \\
                          &= 1 + 2 - 3 \\
                          &= 0
    \end{split}
  \]
  Hence $\vec{v} \perp \vec{N}$, from which we can derive that $\vec{v}$ is
  perpendicular to the plane.
\end{example}

\section{Geometric Interpretation of Linear Systems}

\subsection{Linear systems in geometry}

A linear system describes the intersection of some objects or point sets in
geometry.

Take a $3 \times 3$ linear system as an example:
\[
  \left\{ \begin{array}{ll}
  x + z = 1 \\
  x + y = 2 \\
  x + 2y + 3z = 3 \\
  \end{array} \right.
\]
The solution to this linear system is the set of points $(x, y, z)$ satisfying
all three linear equations, where each of them defines a plane. Therefore, the
points in the solution set are in all of three planes. Hence, the solution to
the linear system describes the intersection of these three planes in geometry.

To find the solution to a linear system, algebraic methods are easier than
geometric methods. Recall that a linear system can be expressed with matrix
products as:
\[
  AX = B
\]
\[
  X = A^{-1}B
\]
$A^{-1}B$ is the unique solution to the linear system, which is the
intersection point in geometry.

\subsection{Exceptions of unique solution in geometry}

However, there are exceptions to this algebraic method.
\begin{example}
  If the solution set to a $3 \times 3$ linear system is not a single point, it
  could be \underline{A C E}.

  A. no solution \\
  B. two points \\
  C. a line \\
  D. a tetrahedron \\
  E. a plane \\
  F. I don't know

  Reasons:
  For A, the situation would be at least two of the three planes are parallel to
  each other and not the same plane, or the intersection line of two planes is
  parallel to and not contained in the third plane.

  For C, the situation would be the intersection of two planes, which is a line,
  is contained in the third plane.

  For E, the situation would be that the three planes are the same.
\end{example}
The exceptions to a single solution to a linear system are described in the
above example.

\subsection{Algebraic point of view on exceptions of unique solutions}

Recall that the formula of the unique solution to a linear system is
\[
  X = A^{-1}B
\]
It turns out this formula doesn't always hold. In those exception cases,
$A^{-1}$ doesn't exist, i.e. $A$ is not invertible.

\[
  A^{-1} = \frac{1}{det(A)}adj(A)
\]
Since $adj(A)$ always exists, $A^{-1}$ exists $\iff$ $A$ is invertible $\iff$
$det(A) \neq 0$.

\bigskip

Further discussion on different cases:

1. Homogeneous cases: $AX = 0$

For Homogeneous cases, there is always a trivial solution $X = 0$ since all
planes pass through the origin.

If $det(A) \neq 0$, then $A$ is invertible, then the linear system has a unique
solution, which must be $0$ since $0$ is always a solution to the linear
system. We can also derive the conclusion in algebra:
\[
  X = A^{-1}0 = 0
\]

If $det(A) = 0$, then since each row in $A$ is a normal vector of the
corresponding plane, $det(\vec{N_1}, \vec{N_2}, \vec{N_3}) = 0$, which means
that $\vec{N_1}$, $\vec{N_2}$, $\vec{N_3}$ are coplanar.

  If $\vec{N_1}$, $\vec{N_2}$, $\vec{N_3}$ are the same, then the planes in the
  linear system are the same, so the solution set is the plane.

  If $\vec{N_1}$, $\vec{N_2}$, $\vec{N_3}$ are different, then the line passing
  through the origin and perpendicular to the plane containing $\vec{N_1}$,
  $\vec{N_2}$, and $\vec{N_3}$ must be in all three planes in the linear
  system. Therefore, the solution is $\vec{N_1} \times \vec{N_2}$, or
  $\vec{N_2} \times \vec{N_3}$, or $\vec{N_1} \times \vec{N_3}$.

2. General cases: $AX = B$

If $det(A) \neq 0$, there is a unique solution to the linear system.

If $det(A) = 0$, there is either none or infinite solution to the linear system.

\end{document}


\documentclass{article}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    \usepackage{mathrsfs}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{tikz}
    \usepackage{fancyhdr}
    \usepackage{multicol}

    \usetikzlibrary{positioning}

\pagestyle{fancy}
\fancyhf{}
\rhead{Edgar Jacob Rivera Rios - A01184125}

\renewcommand{\labelenumi}{\alph{enumi})}

\begin{document}
\section*{3.2.1}
In an effort to estimate the mean amount spent per customer for dinner at a major Atlanta restaurant, data were collected for a sample of 49 customers. Assume a population standard deviation of \$5.
\begin{enumerate}
  \item At 95\% confidence, what is the margin of error?
  \begin{align*}
    \sigma &= 5\\
    n &= 49\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.05/2}(5/ \sqrt{49})\\
    &=Z_{.025}(5/7)\\
    &=1.96(5/7)\\
    &=1.4\\
  \end{align*}
  \item If the sample mean is \$24.80, what is the 95\% confidence interval for the population mean?
  \begin{align*}
    \bar{x} &= 24.80\\
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 24.80 \pm 1.4\\
    &= (23.40, 26.20)
  \end{align*}
\end{enumerate}

\section*{3.2.2}
The Wall Street Journal reported that automobile crashes cost the United States \$162 billion annually (The Wall Street Journal, March 5, 2008). The average cost per person for crashes in the Tampa, Florida, area was reported to be \$1,599. Suppose this average cost was based on a sample of 50 persons who had been involved in car crashes and that the population standard deviation is $\sigma = 600$
\begin{enumerate}
  \item What is the margin of error for a 95\% confidence interval?
  \begin{align*}
    \sigma &= 600\\
    n &= 50\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.05/2}(600/ \sqrt{50})\\
    &=Z_{.025}(84.8528137423)\\
    &=1.96(84.8528137423)\\
    &=166.311514\\
  \end{align*}
  \item What would you recommend if the study required a margin of error of \$150 or less?

  We should reduce the confidence interval to get a smaller error margin
\end{enumerate}

\pagebreak

\section*{3.2.3}
The National Quality Research Center at the University of Michigan provides a quarterly measure of consumer opinions about products and services (The Wall Street Journal, February 18, 2003). A survey of 10 restaurants in the Fast Food/Pizza group showed a sample mean customer satisfaction index of 71. Past data indicate that the population standard deviation of the index has been relatively stable with $\sigma = 5$.
\begin{enumerate}
  \item What assumption should the researcher be willing to make if a margin of error is desired?

  That the data has a normal distribution and that you can based the margins in that

  \item Using 95\% confidence, what is the margin of error?
  \begin{align*}
    \sigma &= 5\\
    n &= 10\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.05/2}(5/ \sqrt{10})\\
    &=Z_{.025}(1.58113883)\\
    &=1.96(1.58113883)\\
    &=3.0990321\\
  \end{align*}

  \item What is the margin of error if 99\% confidence is desired?
  \begin{align*}
    \sigma &= 5\\
    n &= 10\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.01/2}(5/ \sqrt{10})\\
    &=Z_{.005}(1.58113883)\\
    &=2.576(1.58113883)\\
    &=4.07301362\\
  \end{align*}
\end{enumerate}

\section*{3.1.4}
Playbill magazine reported that the mean annual household income of its readers is \$119,155 (Playbill, January 2006). Assume this estimate of the mean annual household income is based on a sample of 80 households, and based on past studies, the population standard deviation is known to be $\sigma = \$30,000$.
\begin{enumerate}
  \item Develop a 90\% confidence interval estimate of the population mean.
  \begin{align*}
    \sigma &= 30,000\\
    n &= 80\\
    \bar{x} &= 119,155\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.1/2}(30,000/ \sqrt{80})\\
    &=Z_{.05}(3354.101966)\\
    &=1.645(3354.101966)\\
    &=5,517.4977344\\
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 119,155 \pm 5,517.4977344\\
    &= (113637.5022656, 124672.4977344)
  \end{align*}

  \item Develop a 95\% confidence interval estimate of the population mean.
  \begin{align*}
    \sigma &= 30,000\\
    n &= 80\\
    \bar{x} &= 119,155\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.05/2}(30,000/ \sqrt{80})\\
    &=Z_{.025}(3354.101966)\\
    &=1.96(3354.101966)\\
    &=6,574.0398538\\
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 119,155 \pm 6,574.0398538\\
    &= (112580.9601462, 125729.0398538)
  \end{align*}

  \item Develop a 99\% confidence interval estimate of the population mean.
  \begin{align*}
    \sigma &= 30,000\\
    n &= 80\\
    \bar{x} &= 119,155\\
    \text{Error Margin} &= Z_{a/2}(\sigma / \sqrt{n})\\
    &=Z_{.01/2}(30,000/ \sqrt{80})\\
    &=Z_{.005}(3354.101966)\\
    &=2.576(3354.101966)\\
    &=8,640.166665\\
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 119,155 \pm 8,640.166665\\
    &= (110514.833335, 127795.166665)
  \end{align*}
  \item Discuss what happens to the width of the confidence interval as the confidence level is increased. Does this result seem reasonable? Explain.

  We can see that the width of the confidence interval grows as the confidence level is increased. It seems reasonable because a confidence interval of 99 must include 99\% of the results, which logically must be a wider part than a 95\% or 90\%.
\end{enumerate}

\end{document}


\documentclass{memoir}
\usepackage{linalg}

\begin{document}
\section{Dimension}%
\label{cha:dimension}
\begin{defn}[Dimension]
	The \textbf{dimension} of a finite-dimensional vector space is the length of any basis of the vector space.
\end{defn}
\begin{lemma}[Dimension of a subspace]
	If $V$ is finite-dimensional and $U$ is a subspace of $V$, then
	\begin{align*}
	\text{dim}U \leq \text{dim}V.
	\end{align*}
\end{lemma}

\begin{lemma}
	Suppose $V$ is finite-dimensional. Then every linearly independent list of vectors in $V$ with length $\text{dim}V$ is a basis of $V$.
\end{lemma}
\subsection{Dimension of a sum of two spaces}
\begin{prop}
Let $U_1,U_2 \in V$ be finite dimensional vector spaces over some field $F$.\\

Then
\begin{align*}
	\textrm{dim}(U_1+U_2) = \textrm{dim}U_1 + \textrm{dim}U_2 - \textrm{dim}(U_1\cap U_2)
\end{align*}
\end{prop}
\begin{cor}
	Suppose $V$ is finite-dimensional and $U$ is a subspace of $V$ such that $\text{dim}U = \text{dim}V$. Then $U = V$.
\end{cor}
\end{document}


%!TEX root = ../notes.tex
\section{February 9, 2022}
\subsection{Asymmetric/Public Key Cryptography}
The premise is that we have \emph{Alice} and \emph{Bob} who are communicating, and \emph{Eve} intercepts \ul{all} communications between them. There is \textbf{no} communication between Alice and Bob ahead of time. A priori, it's not entirely obvious that this is possible\dots

We'll see that this is indeed possible!

\begin{example}
    \ul{Analogy}: Alice and Bob are communicating by writing messages on pieces of paper.

    Symmetric cryptography is having a shared safe, Alice and Bob both have the key/know the combination to, and both can leave messages and retrieve messages.

    \begin{enumerate}
        \item Alice sets up a box with a thin slot with a lock on it. Alice has the key to this lock.
        \item Bob is able to deposit messages into the slot in the box, and Alice can retrieve it using her key.
    \end{enumerate}
\end{example}

Our key is now $k = (k_\mathsf{priv}, k_\mathsf{pub})\in \mathcal{K} = \mathcal{K}_\mathsf{priv}\times \mathcal{K}_\mathsf{pub}$ which consists of a private key and public key.

Our encryption and decryption functions are now
\begin{align*}
    e & : \mathcal{K}_\mathsf{pub} \times \mathcal{M} \to \mathcal{C}  \\
    d & : \mathcal{K}_\mathsf{priv} \times \mathcal{C} \to \mathcal{M}
\end{align*}
\[d(k_\mathsf{priv}, e(k_\mathsf{pub}, m)) = m\]
We want it to be easy to compute $e_{k_\mathsf{pub}}$ and $d_{k_\mathsf{priv}}$, but hard to compute $d_{k_\mathsf{priv}}$ only knowing $k_\mathsf{pub}$.

Something easier to construct, before a full-fledged public key system, is a key exchange:
\subsection{Diffie-Hellman Key Exchange}
\textbf{Q: How can Alice and Bob agree on a secret key over an insecure channel?}

\begin{example}
    \ul{Analogy}: A lockbox that can only be used by one person...and both people have to participate to set it up.
\end{example}

Both parties have to agree on a key and have a line of communication before agreeing on a key. This can only be used if both parties are online at the same time.

We start with a prime $p$ and $g\in(\ZZ/p\ZZ)^\times$ suitably. Alice and bob does the following, all mod $p$:
\begin{center}
    \begin{tabular}{ll}
        \toprule
        Alice              & Bob                 \\ \midrule
        Generates $a$      & Generates $b$       \\
        \quad $\downarrow$ & \quad $\downarrow$  \\
        Computes $g^a$     & Computes $g^b$      \\
        Send $g^a$ to Bob  & Send $g^b$ to Alice \\
        Computes $(g^b)^a$ & Computes $(g^a)^b$  \\
        \bottomrule
    \end{tabular}
\end{center}

Alice and Bob now know $g^{ab}$, which is the secret key. Eve, however, only knows $g^a$ and $g^b$. Alice and Bob can now use this shared secret $g^{ab}$ as a key for symmetric cryptography.

\begin{definition}[The Diffie-Hellman Problem (DHP)]
    Given $g^a, g^b$, calculate $g^{ab}$.
\end{definition}
\begin{remark}
    If we can solve the discrete log problem, we can solve the Diffie-Hellman problem.
\end{remark}

Vice versa? Can one solve DLP given solution to DHP? \emph{This is unknown\footnote{There is no known method.}.}

\subsection{Elgamal Public Key Cryptography}
We again start with $p$ prime and $g\in (\ZZ/p\ZZ)^\times$ suitably. This could be public knowledge, or Alice selects these.

Alice: We have $a$ be Alice's private key, and $A = g^a$ be Alice's public key.

Bob: Has message $m$ he wishes to send. Bob does the following:
\begin{enumerate}
    \item Generate random $k$ (used only once, to send this message).
    \item Compute the following:
          \begin{enumerate}
              \item $c_1 = g^k\mod{p}$
              \item $c_2 = m\cdot A^k \mod{p}$
          \end{enumerate}
    \item Send $c_1$ and $c_2$ to Alice.
\end{enumerate}

Alice:
\[(c_1^a) = A^k \text{ so } c_2\cdot (c_1^a)^{-1}\equiv m\left((g^a)^k\right)\cdot \left((g^a)^k\right)^{-1} \equiv m\]
Basically, they are using Diffie-Hellman key exchange, except $g^a$ is a public key and Bob assumes a secret key, and uses that to encrypt the message and sends it in one go.

\subsubsection{Implementation}
We have the following algorithm for encryption and decryption in Elgamal:
\begin{lstlisting}[language=Python]
import ext_gcd, pow_mod
from random import randrange
def e(A, m):
    k = randrange(p)
    return (pow_mod(g, k, p), m * pow_mod(A, k, p))

def d(a, c):
    pow_mod(c[0])
    ...
\end{lstlisting}
\emph{to be continued...}


\documentclass[main.tex]{subfiles}

\begin{document}

%%%%%%%%%%%%%%%%%%%%
\section{Differential Geometry}
There is a lot of equation and stuff to remember for DG. A lot of connection stuff, but I think (thankfully) no gauge theory.

%%%%%%%%%%%%%%%%%%%%
\subsection{Connection and curvature}

\begin{definition}
A connection $\nabla$ on a vector bundle $E$ on a manifold $M$ is a linear map
$$
\nabla: \Gamma(M, E) \rightarrow \Gamma(M, \Omega^1(M) \otimes E )
$$
satisfying the Leibniz rule:
$$
\nabla(f s) = f \nabla(s) + df \otimes s
$$
where $f \in C^{\infty}M$ and $s \in \Gamma(M)$. Given $X \in \Gamma(M)$, we can define
$\nabla_X(s) \coloneqq \nabla(s) (\mu)$. Note that this is $C^{\infty}M$-linear in $X$, that is, $\nabla_{fX}(s) = f \nabla_{X}(s)$
\end{definition}

Naturally, if I have a connection $\nabla$ on $p: E \rightarrow M$, and a map $f : N \rightarrow M$, then we get pullback connection $f^*\nabla$ on $f^*E \rightarrow N$.

In local coordinates, we have basis $s_j$ for $E$ and $dx^i$ for $T^*M$, then we have
$$
\nabla(s_j) = \Gamma_{ij}^k dx^i \otimes s_k
$$
Equivalently,
$$
\nabla_{\partial_i} s_j = \Gamma_{ij}^k s_k
$$
$\Gamma_{ij}^k$ are called the Christoffel symbols.

Alternatively, one can write
$$
\omega(s_j) = \omega_j ^k s_k
$$
where $\omega_j ^k = \Gamma_{ij}^k dx^i$ are connection 1-forms.

From this and Leibnitz rule we can determine the formula for any local section.
Given a covariant derivative $\nabla$ on $E$, we can extend $d_\nabla: \Gamma(M, \Omega^i(M) \otimes E) \rightarrow \Gamma(M, \Omega^{i+1}(M) \otimes E)$.

We also get connection on tensors, direct sums, and duals of a vector bundle:
\begin{construction}
Given $(E, \nabla')$, $(F, \nabla'')$, we get a connection $\nabla$ on $E \sum F$ as follows:
$$
\nabla(s, t) = (\nabla' s, \nabla'' t)
$$

Similarly, we have connection $\nabla$ on $E \otimes F$:
$$
\nabla s\otimes t = \nabla's \otimes t + s \otimes \nabla'' t
$$

The formula on the symmetric and exterior powers are the same.

lastly, there is a connection on the dual, defined by:
$$
\nabla \alpha (s) \coloneqq d (\alpha(s)) -  \alpha (\nabla' s)
$$
This is uniquely determined by making the pairing map $(E \otimes E^*, \nabla' \otimes \nabla) \rightarrow (\underline{\mathbb{R}}, d)$ a map of bundles with connections (that is, taking connection commutes).
\end{construction}

%%%%%%%%%%%%%%
\subsubsection{Curvature}

\todo[inline]{Mean curvature, scalar curvature etc, Ricci curvature, the symmetry of the Riemann curvature tensor}
From this, we can define the curvature of a connection:

\begin{definition}
Given a connection $\nabla$ on $E$, we have the curvature:
$$
R(\mu, \nu) s  \coloneqq \nabla_{\mu} \nabla_{\nu} s - \nabla_{\nu} \nabla_{\mu} s  - \nabla_{[\mu, \nu]} s
$$
In fact, $R: \Gamma(E) \rightarrow \Gamma(\Omega^2(M) \otimes E)$ is precisely $d_\nabla ^2$. It is in fact a tensor (that is it is linear in $\mu, \nu$ and $s$).

In coordinates,
$$
R^l _{k, i, j} = dx^l (R(\partial_i, \partial_j) \partial_k) = \partial_i \Gamma_{jk}^l - \partial_j \Gamma_{ik}^l + \Gamma_{im}^l \ \Gamma_{jk}^m   -  \Gamma_{jm}^l \  \Gamma_{ik}^m
$$

Using connection 1-forms $\omega$, we have
$$
R \coloneqq d\omega + \omega \wedge \omega
$$
Note $\omega \wedge \omega$ does matrix multiplication on the $(k,l)$ components.

A connection is flat if its curvature is 0. Thus gives local systems on the manifold.
\end{definition}

\subsubsection{Torsion}
The most interesting connections are the ones on the tangent bundle, which is what we called a connection on a manifold.

\begin{definition}Given a connection $\nabla$ on $M$ ($TM$), then its torsion tensor is
$$
T(X,Y) \coloneqq \nabla_X(Y) - \nabla_Y(X) - [X,Y]
$$
It can be shown that this is a tensor ($C^{\infty}M$-linear) and ofc anti-symmetric.
\end{definition}

In local coordinates and Christoffel symbols, this means that
$$
\Gamma_{ij}^k = \Gamma_{ji}^k
$$
%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Parallel Transport}
Given a bundle $E$ over the interval $[0,1] \in \mathbb{R}$, together with a connection $\nabla$. At any point $v \in E|_0$, there exists a unique section $s_v : [0,1] \rightarrow E$ with starting point $v$ such that $\nabla_{\partial_x} s_v = 0$.

Locally, write a basis for $E$ with $s_v$ being an basis element (ofc, when $s_v$ is nonzero), and we have Christoffel symbol:
$$
\nabla_{\partial_x} s_i = \Gamma_{i}^k s_k
$$
By the fundamental theorem of ODE, we see that this has a unique solution that exists for all $x \in [0,1]$.

Thus given a connection $\nabla$ on $E \rightarrow M$, and a path $\gamma: [0,1] \rightarrow M$, then we get a parallel transport map: $E|_{\gamma(0)} \rightarrow E|_{\gamma(1)} $. Ofc this is compatible with concatenation. Moreover, the difference between how infinitesimally small path give different parallel transport is precisely measure by the curvature. So when the curvature vanishes, then any two homotopic smooth paths gives the same transport, and we get a local system. Another way to put it is that we have local flat sections, over a local neighborhood, not just a path.

%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Geodesics}
Given a connection $\nabla$ on (the tangent space of) manifold $M$. Given any path $\gamma: [0,1] \rightarrow M$, we can ask for the following
$$
\nabla_{\dot{\gamma}} \dot{\gamma} = 0
$$
In coordinates, where $t$ is the coordinate of the interval, and $x^i$ are the local coordinates of the manifold, we have the geodesic equations:
$$
\frac{d^2 x^k}{dt ^2} + \Gamma_{ij}^k \frac{dx^i}{dt} \frac{dx^j}{dt} = 0
$$
Note that this is $n$ 2nd order ODE in $n$ variables, and the existence and uniqueness theorem tells us that there exists locally a unique solution, determined by the starting point as well as the velocity. That is, given any $p \in M$ and $v \in T_p M$, there is locally a unique geodesic going through $p$ with velocity $v$.
%%%%%%%%%%%%%%%%%%%%
\subsection{Riemannian Geometry: General Theory}
Let $(M, g)$ be a Riemannian manifold, then we have an inner product
$(-,-)$ on $T_p M$ for every point $p \in M$. This identifies vectors with covectors, and gives us a way to turn $(i, j)$ tensors to $(i-1, j+1)$ tensors and vice versa. This is the raising and lowering of indicies.

Given a Riemnnian metric $g$ on a vector bundle $E$, a connection $\nabla$ on $E$ is compatible with the metric if $\nabla g = 0$. In formula, this means that for any $X \in TM$, $s, t$ sections of $E$, we have
$$
d(s,t) = (\nabla s, t) + (s, \nabla t)
$$

\begin{definition}
There is a unique torsion-free, compatible connection $\nabla$ associated to $g$, called the Levi-Civita connection.
\end{definition}
In local coordinates,
$$
\Gamma_{ij}^k = \frac{1}{2} g^{kl}(\partial_i g_{jl} + \partial_j g_{il} - \partial_l g_{ij})
$$
\begin{remark}
Given a 1-form $\alpha$, its covariant derivative $\nabla \alpha$ is the dual of $\nabla V_\alpha$, where $V_\alpha$ is the dual of $\alpha$.
\end{remark}

\begin{remark}
On a Riemannian manifold $M$, the holonomy around a loop $C$ is the parallel transport of the tangent vector around the loop.
\end{remark}

\subsubsection{Curvature of Riemannian manifolds}
Recall that we have the curvature tensor $R_{ijk}^l$. We can also lower the $l$ to get the tensor $R_{ijkl}$. This tensor has a lot of symmetries:
$$
R_{ijkl} = R_{klij}, R_{ijkl} = -R_{jikl} (R_{ijkl} = - R_{ijlk}), R_{ijkl} + R_{iljk} + R_{iljk} = 0
$$
In dimension $n$, it has
$$
\frac{n^2 (n^2 - 1)}{12}
$$
components.
Taking trace gives us the Ricci curvature:
\begin{definition}
The Ricci curvature tensor is
$$
R_{jk} = R^i _{kij}
$$
It is symmetric in j and k.
\end{definition}

Taking trace once more, we get the scalar curvature
$S = R^j _j = g^{ij} R_{ij}$.

%%%%%%%%%%%%%%%%%%%%
\subsubsection{Riemannian Geometry: Submanifolds}

Given an immersion of manifold $i: N \rightarrow M$, then a Riemannian metric $g$ on $M$ pullback to a Riemannian metric $g'$ on $N$. However, we can define the Levi-Civita connection
$\nabla'$ on $N$ more easily:
\begin{lemma}
$\nabla'_{\mu} \nu = pr (\nabla_{\mu} \nu)$, where the projection is $TM \rightarrow TN$ using the metric $g$ on $M$.
\end{lemma}

We can also project down to the normal bundle:
\begin{definition}
$II: TN \times TN \rightarrow N$ is defined as $II(X, Y) \coloneqq (\nabla_{X} Y)^\perp$, where $\perp$ is the projection $TM \rightarrow N$, where $N$ is the normal bundle. This is symmetric because the Levi-Civita connection is torsion-free (and the lie bracket of two tangent vector field that lies on a submanifold stays on the submanifold).
Given $\mathfrak{n} \in N$, then we get a pairing $II_\mathfrak{n}: TN \times TN \rightarrow \mathbb{R}$ given by
$$
X, Y \mapsto (\nabla_{X} Y, \mathfrak{n}) = - (\nabla_{X} \mathfrak{n}, Y)
$$
Note that the equation is by the compatibility of $\nabla$ with $g$ as well as the fact that $(Y, \mathfrak{n}) = 0$ as $\mathfrak{n}$ is normal.

The compatibility conditions implies that
$\nabla_X v$ is perpedicular to $v$ when $v$ is unit lenghth. Thus the holonomy are in $SO(T_pM)$.

Here's some equivalent definition for $\nabla$ to be compatible with $g$:
\begin{enumerate}
    \item $\nabla g = 0$.
    \item the pairing $(-,-): TM \times TM \rightarrow \underline{\mathbb{R}}$ is map of bundles with connections.
    \item the iso $TM \cong T^*M$ arising from $g$ is a map of bundles with connections.
\end{enumerate}

Note the last one tells us how to compute covariant derivative for 1-forms: just dualize.

Dually, we have the shape operator associated to $\mathfrak{n}$:
\begin{align*}
S_{\mathfrak{n}}: TN &\rightarrow TN \\
X  \mapsto & - (\nabla_{X} \mathfrak{n})
\end{align*}
\end{definition}

In coordinates, we have
$$
S^i _j =  II^{ik} g_{kj}
$$
One can check that $\nabla_{X} \mathfrak{n}$ lies on the tangent space of $M$

As the shape operator is symmetric (with respect to the Riemannian inner product of $N$), its eigenvalues are all real, and they are all orthogonal, no generalize eigenspaces. Thus there are eigenvalues and eigensubspaces of $S$. The eigenvalues are called the principal curvatures and the tangent vector there curve in circle around the normal vector.

\subsubsection{Geodesics in a Riemannian manifold}
Given a Riemann manifold $(M, g)$, since it has a Levi-Civita connection, thus we can ask for geodesics. But in fact there is a more direct interpretation (and use more often for quals), which is using the Lagrangian formulism.

Given any path $\gamma: [0,1] \rightarrow M$, we have the following functional
$$
\mathcal{L} \coloneqq \int_{[0,1]} g_{ab} \dot{\gamma^a} \dot{\gamma b}
$$, this is a form of length (or energy) of the path.

whose Euler Lagrange equation is precisely the geodesic equation.

Recall the Euler-Lagrange equation, whose solution are the local extrema of the variation problem is given by
$$
\frac{\partial \mathcal{L}}{\partial x^i} - \frac{d}{dt} \frac{\partial \mathcal{L}}{\partial \dot{x^i}} = 0
$$

\subsubsection{Covariant derivative in Orthogonal frames}

On a Riemannian manifold $(M, g)$, given an orthogonal frame $\theta_i$ and coframe $\theta^i$, we would like to calculate the covariant derivative (Christoffel symbols) of the Levi-Civita connection $\nabla$ from the frame. Using torsion-free and compatibility property.

\todo[inline]{Orthonormal frame and Cartan's structure equation, also using the connection 1-form $\omega$}

In this frame, we have the connection 1-form
$$
\nabla \theta_j = \omega_j ^k \theta_k
$$
with
$$
\omega_j ^k = \Gamma_{ij}^k \theta^i
$$
As $(s_i, s_j) = \delta_{i,j}$, the compatibility condition says that
$$
(\nabla \theta_j, \theta_k) = -(\nabla \theta_k, \theta_j)
$$
On coordinates, this means that
$$
\omega_j ^k = - \omega_k ^j
$$

Using the torsion free assumption, we also have the 1st Cartan's structural formula:
$$
d\theta^k = \omega_i ^k \wedge \theta^i
$$
This formula is super important in calculating covariant derivatives. This is a part of the orthonormal decomposition method!
(evaluating both sides at $(\theta_m, \theta_l)$, then the left hand side is given by $-\theta^k([\theta_m, \theta_l])$ while the right hand side gives the corresponding $k$-th Christoffel symbol for $\nabla_{\theta_m} \theta_l - \nabla_{\theta_m}\theta_l \ d\theta^k= \omega^k _{ml} - \omega^k _{lm}$.

\subsubsection{Gauss map}
Given a immersion $i: M \rightarrow \mathbb{R}^{n+1}$ where $M$ is a compact oriented $n$-dimensional manifold (aka a hypersurface), then taking the (oriented) normal vector gives a map $N_i: M \rightarrow S^n$. The differential of this map is the shape operator (because we have doing $\partial_i \mathfrak{n} = \nabla_{\partial_i} \mathfrak{n}$).
If $n=2$, then the Jacobian (the determinant of the differential) it thus the Gaussian curvature.
Moreoever, the degree of this map is 1/2 of the Euler characteristic of $M$. This is because the Euler characteristic is the Euler class of the tangent bundle of the sphere, which pulls back to the tangent bundle of the manifold. And taking Euler class intertwines between vector bundles and cohomology classes. Lastly the Euler class of the sphere is $1 = \frac{1}{2} \chi(S^2)$.

\subsection{Riemannian Geometry: Surfaces in $\mathbb{R}^3$}
\todo[inline]{Gauss map, interpreting the curvature from the Gauss map, Gauss-Bonnet theorem.}

Let $S \subset \mathbb{R}^3$ be an embedded smooth surface, and $\mathfrak{n}$ a normal vector (this implicitly assumed a co-orientation of $S$, equivalently, an orientation of $S$, since $\mathbb{R}^3$ is oriented).

Since there is a canonical choice of normal vector, we get the second fundamental form. At any point $p$, the shape has two (not necessarily distinct) eigenvalues, $k1$ and $k2$, and they are the maximal point of the curvature of the normal planes intersecting with the curve. The Gaussian curvature $K \coloneqq k_1 k_2$. It is the determinant of the shape operator, equivalently, in any coordinate,
$$
K = det S = \frac{det II}{det I}
$$

The mean curvature is $k_1 + k_2$ and it is the trace of the shape operator:
$$
H = tr(S) = g_{ij} II^{ij}
$$

For the Euclidean space $\mathbb{R}^n$ with its standard coordinates $x^i$, the covariant derivative is simply taking derivative, that is, all the Christoffel symbol are all zero.

In three dimensions, there are vector calculus calculations we can use, namely the cross product. Given a parametrized surface $\Phi: U \rightarrow \mathbb{R}^3$, to find the normal vector, we take two independent local vector field $\mu$, $\nu$ on $U$, then we $\Phi_* {\mu}$ and $\Phi_* \nu$ spans the tangent spaces of the surfaces, and the cross product $\Phi_* \mu \times \Phi_* \nu$ is perpendicular to both, those spans the normal line bundle. Therefore
$$
\mathfrak{n} \coloneqq \frac{\Phi_* \mu \times \Phi_* \nu}{|\Phi_* \mu \times \Phi_* \nu|}
$$
is a unit length normal vector.

\subsubsection{Curvature in 2 space}

In two dimensions, the Riemann curvature tensor only have 1 degree of freedom and is determined by
$R^1 _{212}$.

The relationship between Riemann curvature and Gaussian curvature is as follows:
$$
R_{1212} = det(g) K
$$
Thus in with orthonormal frames they are the same. Coordinate independently, we have
$$
R_{klij} = K (g_{ki}g_{lj} - g_{kj}g_{lj})
$$
and multiple with $g^{ki}$, we get
$$
Ric_{lj} = K g_{lj}
$$
Multiple with $g^{lj}$, we get that the scalar curvature:
$$
S = 2K
$$

\begin{remark}
Note that holonomies of loops on a surface are rotations as $SO(2) = U(1)$.
\end{remark}
\subsubsection{Gauss-Bonnet, Theorem-Egrenium}
\todo[inline]{State those theorem, at least know the proof of Theorem-Egrenium, and how does Guass-Bonnet relate to the first chern class of the tangent bundle?}

Given a surface $S \subset \mathbb{R}^3$, Theorem Egregium relates the Gaussian curvature, an external curvature invariant, with the Riemann curvature tensor coefficients (or the scalar curvature). It says that
$$
K = R(X, Y) X Y
$$
for $X, Y$ local orthonormal vector fields.

On the other hand, the Gauss-Bonnet theorem says that the integral of the Gaussian curvature is a topological invariant:
$$
\int_S K dA = 2 \pi \chi(M)
$$

It has an extension to compact Riemannian manifold $S$ with boundary:
$$
\int_S K dA + \int_{\partial S}k_g ds = 2\pi \chi(M)
$$
where $k_g$ is the geodesic curvature of the boundary of $S$ (as a curve in $S$).

Given a curve $\gamma$, then
$$
\nabla_{\dot{\gamma}} \dot{\gamma}
$$
is the normal vector of the curve. In dimension 2, this is a multiple of the normal vector, thus we get a scalar $k_g$.

\subsubsection{Orthonormal method to compute curvature of surfaces}
This is a standard method for computing curvature in 2-dimensions. First we take a orthonormal coframe $\theta^i$ and the dual frame $\theta_i$. We have the connection 2-form $\omega_j ^k$. From above, we have that
$$
\omega_j ^k = - \omega_k ^j
$$
This tells us right away that we only have to calculate
$$
\omega_1 ^2 = - \omega_2 ^1
$$
Then using Cartan's structural equation we have $$
d\theta^k = \omega^k _i \wedge \theta^i
$$
and this gives us two equations
$$
d\theta^1 = \omega^1 _2 \wedge \theta^i
$$ and
$$
d\theta^2 = \omega^2 _1 \wedge \theta^1
$$.
Hopefully this is enough to compute $\omega^1_2$. Then we have
$$
R = d \omega + \omega \wedge \omega
$$

\subsection{Calculus of differential forms}
Lie brackets and exterior derivatives are in some sense dual.

The Lie bracket has the unique properties  that
\begin{enumerate}
    \item $\mathcal{L}_X \sigma \otimes \tau = \mathcal{L}_X \sigma \otimes \tau + \sigma \otimes \mathcal{L}_X \tau$
    \item $\mathcal{L}_X f = X f = df (X)$
    \item $\mathcal{L}_X \sigma(Y_1, ..., Y_n) = \mathcal{L_X} (\sigma)(Y_1, ..., Y_n) + \sigma (\mathcal{L_X}Y_1, Y_2,...) + ...$
\end{enumerate}

For a 0-form $f$, we have
$$
df(X) = X(f) = \mathcal{L}_X f
$$

For a 1-form $\omega$, we have
$$
d\omega(X,Y) = X(\omega(Y)) - Y(\omega(X)) - \omega([X,Y])
$$

We have the Cartan's magic formula:
$$
\mathcal{L}_X \omega = (\iota_X d + d \iota_X) \omega
$$

There is a formula expressing $d\omega$ as evaluation of $\omega$ and lie brackets.
Using the Cartan's magic formula as well as expanding $\mathcal{L}_X$ on pairings of vectors and covectors.

\subsection{Standard Coordinates}
The polar coordinates on $\mathbb{R}^2 - {0}$:
$$
x = r cos \theta, y = r sin \theta
$$
. The standard metric becomes
$$
ds^2 = dr^2 + r^2 d\theta^2
$$
with $r \in \mathbb{R}^{>0}$ and $\theta \in [0,2\pi)$
The spherical coordinate on $S^2$ is
$$
x = cos \phi \ sin \theta,
y = sin \phi \ sin \theta,
z = cos \theta
$$
with $\phi \in [0,2\pi)$ and $\theta \in (0, \pi)$
the sphere metric becomes
$$
ds^2 = d\theta^2 + sin^2 \theta \ d\phi^2
$$

\end{document}


\chapter{Balanced Search Tree}
\section{2-3 Search Tree}
\subsection{Insertion}
Insertion into a 3-node at bottom:
\begin{enumerate}
\item Add new key to the 3-node to create a temporary 4-node.
\item Move middle key of the 4-node into the parent (including root's parent).
\item Split the modified 4-node.
\item Repeat recursively up the trees as necessary.
\end{enumerate}
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.8]{23insert1}}
\caption{Insertion 1}
\label{fig:LABEL}
\end{figure}

\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.8]{23insert2}}
\caption{insert 2}
\label{fig:LABEL}
\end{figure}

\subsection{Splitting}
Summary of splitting the tree.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.65]{23splitting}}
\caption{Splitting temporary 4-ndoe summary}
\label{fig:splitting}
\end{figure}

\subsection{Properties}
When inserting a new key into a 2-3 tree, under which one of the following scenarios must the height of the 2-3 tree increase by one? When every node on the search path from the root is a 3-node

\section{Red-Black Tree}\label{rbtree}
\subsection{Properties}
Red-black tree is an implementation of 2-3 tree using \textbf{leaning-left red link}. \begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=1.1]{rbtree11}}
\caption{RB-tree and 2-3 tree}
\label{fig:LABEL}
\end{figure}
The height of the RB-tree is at most $2\lg N$ where alternating red and black links. Red is the special link while black is the default link.

\runinhead{Perfect black balance.}Every path from root to null link has the same number of black links.
\subsection{Operations}
\runinhead{Elementary operations:}
\begin{enumerate}
\item Left rotation: orient a (temporarily) right-leaning red link to lean left. Rotate leftward.
\item Right rotation: orient a (temporarily) left-leaning red link to lean right.
\item Color flip: Recolor to split a (temporary) 4-node. Rotate rightward.
\end{enumerate}
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=1.20]{rbrotate}}
\caption{Rotate left/right}
\label{fig:LABEL}
\end{figure}

\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=1.20]{rbflip}}
\caption{Flip colors}
\label{fig:LABEL}
\end{figure}

\runinhead{Insertion.} When doing insertion, from the child's perspective, need to have the information of current leaning direction and parent's color. Or from the parent's perspective - need to have the information of children's and grandchildren's color and directions.

For every new insertion, the node is always attached with red links.

The following code is the simplest version of RB-tree insertion:
\newpage
\begin{java}
Node put(Node h, Key key, Value val) {
  if (h == null)  // std red insert (link to parent).
    return new Node(key, val, 1, RED);
  int cmp = key.compareTo(h.key);
  if      (cmp < 0) h.left  = put(h.left,  key, val);
  else if (cmp > 0) h.right = put(h.right, key, val);
  else h.val = val; // pass

  if (isRed(h.right) && !isRed(h.left))
    h = rotateLeft(h);
  if (isRed(h.left) && isRed(h.left.left))
    h = rotateRight(h);
  if (isRed(h.left) && isRed(h.right))
    flipColors(h);

  h.N = 1+size(h.left)+size(h.right);
  return h;
}
\end{java}

Rotate left, rotate right, then flip colors.

\runinhead{Illustration of cases.} Insert into a single 2-node: Figure-\ref{fig:rb_2}. Insert into a single 3-node: Figure-\ref{fig:rb_3}
\begin{figure}[t]
\begin{tabular}{cc}
  \includegraphics[height = 1.7in]{rb_left} &
  \includegraphics[height = 1.7in]{rb_right}\\
\end{tabular}
\caption{(a) smaller than 2-node (b) larger than 2-nod}
\label{fig:rb_2}
\end{figure}

\begin{figure}[t]
        \centerline{\includegraphics[height = 2.8in]{rb_3_left_right_btw}}
        \caption{(a) larger than 3-node (b) smaller than 3-node (c) between 3-node.}
    \label{fig:rb_3}
\end{figure}

\runinhead{Deletion.} Deletion is more complicated.

\section{B-Tree}
B-tree is the generalization of 2-3 tree.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.50]{b-tree}}
\caption{B-Tree}
\label{fig:b-tree}
\end{figure}
\subsection{Basics}
Half-full principle:

\begin{tabular}{lll}
\hline\noalign{\smallskip}
\textbf{Attrs} & \textbf{Non-leaf} & \textbf{Leaf} \\
\noalign{\smallskip}\hline\noalign{\smallskip}
Ptrs & \lceil\frac{n+1}{2}\rceil & \lfloor\frac{n+1}{2}\rfloor \\
\noalign{\smallskip}\hline\noalign{
\caption{Nodes at least half-full}
\end{tabular}

\subsection{Operations}
Core clues
\begin{enumerate}
\item \textbf{Invariant}: children balanced or left-leaning
\item \textbf{Split}: split half, thus invariant.
\item \textbf{Leaf-Up}: no delete, recursively move up the right node's first child;
thus invariant.
\item \textbf{Nonleaf-Up}: delete and recursively move up the left's last if left-leaning
or right's first if balanced; thus invariant.
\end{enumerate}

\section{AVL Tree}
TODO

\section{Cartesian Tree}
\subsection{Basics}
Also known as max tree (or min tree). The root is the maximum number in the array. The left subtree and right subtree are the max trees of the subarray divided by the root number.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.60]{Cartesian_tree}}
\caption{Cartesian Tree}
\label{fig:cartesianTree}
\end{figure}
\begin{java}
Given [2, 5, 6, 0, 3, 1], the max tree is
     6
    / \
   5   3
  /   / \
 2   0   1
\end{java}
\runinhead{Construction algorithm.} Similar to all nearest smaller (or larger) values problem - Section \ref{allNearestSmaller}.

Core clues:
\begin{enumerate}
\item Use stack to maintain a \textit{strictly decreasing} stack, similar to find the all nearest large elements.
\itm Maintain the tree for currently scanning $A_i$ with the subarray $A[:i]$.
\begin{enumerate}
\item \rih{Left tree.} For each currently scanning node $A_i$, if ${stk}_{-1} \leq A_i$, then ${stk}_{-1}$ is the left subtree of $A_i$. Then pop the stack and iteratively look at ${stk}_{-1}$ again (previously ${stk}_{-2}$). Notice that the original left subtree of $A_i$ should become the right subtree of ${stk}_{-1} $, because the original left subtree appears later and satisfies the decreasing relationship.
\item \rih{Right tree.} In this stack, ${stk}_{-1} < {stk}_{-2}$ and ${stk}_{-1}$ appears later than ${stk}_{-2}$; thus ${stk}_{-1}$ is the right subtree of ${stk}_{-2}$. The strictly decreasing relationship of stack will be processed when popping the stack.
\end{enumerate}
\end{enumerate}

$O(n)$ since each node on the tree is pushed and popped out from stack once.

\newpage
\begin{python}
def maxTree(self, A):
    stk = []
    for a in A:
        cur = TreeNode(a)
        while stk and stk[-1].val <= cur.val:
            pre = stk.pop()
            pre.right = cur.left
            cur.left = pre

        stk.append(cur)

    pre = None
    while stk:
        cur = stk.pop()
        cur.right = pre
        pre = cur

    return pre
\end{python}

Usually, min tree is more common.
\subsection{Treap}
\rih{Randomized Cartesian tree}. Heap-like tree. It is a Cartesian tree in which each key is given a (randomly chosen) numeric priority. As with any binary search tree, the inorder traversal order of the nodes is the same as the sorted order of the keys.

\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.80]{treap}}
\caption{Treap. Each node x is labeled with x.key: x.priority.}
\label{fig:treap}
\end{figure}

Construct a Treap for an array $A$ with index as the $x.key$ randomly chosen priority $x.priority$ $O(n)$. Thus support search, insert, delete into array (i.e. Treap) $O(\log n)$ on average.

Insertion and deletion - need to perform \textit{rotations} to maintain the min-treap property.


\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{float}
\usepackage{breqn}
\usepackage{graphicx}
\usepackage[labelformat=empty]{caption}
\usepackage{outlines}
\author{Calvin A. Whealton (caw324@cornell.edu)}
\title{Geothermal Play Fairway Analysis Code Documentation}

\renewcommand{\thesection}{}
\renewcommand{\thesubsection}{}

\setcounter{tocdepth}{5}

\begin{document}

\maketitle

\tableofcontents

\newpage

The following document provides supporting information for codes written by Calvin Whealton for the Geothermal Play Fairway Analysis grant. This is not a comprehensive list of all code written for the project. Section 1 is for bottom-hole temperature corrections, section 2 is for outlier identification, and section 3 is for reservoir ideality.

\section*{Bottom-hole Temperature Corrections}

These scripts, code, and documents pertain to the bottom-hole temperature correction calculations.

\subsection*{\textsf{func\_BHT\_NY\_PA\_WV\_corr.R}}

\textbf{Description}: Script with a function to compute bottom-hole temperature corrections for NY, PA, and WV based on the region. Function accepts an R data frame and returns the data frame with two additional columns for the corrected bottom-hole temperature and the error.

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Variable} & \textbf{Description}\\
\hline
\textsf{X} 			 & R data frame with variables named\\
 & \textsf{bht\_c}: Recorded bottom-hole temperature in Celsius\\
 & \textsf{calc\_depth\_m}: Calculated depth of well in meters\\
 & \textsf{reg}:  Region for that point\\
 & 0=Rome Trough and areas south east in PA \\
 & 2=West VA Correction\\
 & 3=Allegheny Plateau with drilling fluid information\\
\hline
\end{tabular}
\end{table}

The output is a data frame with the following columns added.

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Output} & \textbf{Description}\\
\hline
\textsf{corr\_bht\_c} 	 & Corrected bottom-hole temperature in Celsius \\
\textsf{corr\_error}		 & Error from calculation of correcting the bottom-hole temperature\\
  & 0: no errors\\
  & 20: depth outside of normal range\\
  & 21: depth is negative\\
  & 22: depth is missing for Allegheny Plateau or West Virginia data\\
  & 30: categorical variable not 0, 2, or 3\\
  & 32: categorical variable missing\\
  & 42: bottom-hole missing\\
\hline
\end{tabular}
\end{table}

\textbf{Equations}: The equations used to calculate the temperature correction $\Delta T$ are given below. Depth ($z_0$) is in meters and temperature correction ($\Delta T$) is in Celsius. See Frone and Blackwell (2010) for details on the modified Harrison correction. If no region is specified no temperature correction is computed and an error is printed in the output.

\begin{equation} \label{DT_rt}
{\Delta T}_{Rome Tr.} = 0
\end{equation}

\begin{equation} \label{DT_wv}
{\Delta T}_{W Va.} = \min\{15, -1.99 + 0.00652z_0 \}
\end{equation}

\begin{equation} \label{AP_mud}
{\Delta T}_{Alle. Plat., mud} =  \left\{
\begin{array}{ll}
0.0155\left[ \left(1650^3 + z_0^3\right)^{1/3} - 1650 \right],	& (z_0 < 4000 \mathrm{m})\\
37.8,  & (z_0 > 4000 \mathrm{m})\\
\end{array} \right .
\end{equation}

\begin{equation} \label{AP_air}
{\Delta T}_{Alle. Plat., air} =  \left\{
\begin{array}{ll}
0.0104\left[ \left(1090^3 + z_0^3\right)^{1/3} -1090 \right],	& (z_0 < 2500 \mathrm{m})\\
15.4,  & (z_0 > 2500\mathrm{m})\\
\end{array} \right .
\end{equation}

\begin{equation} \label{AP_oth}
{\Delta T}_{Alle. Plat., other} = \mathrm{Prob}(mud) {\Delta T}_{Alle. Plat., mud} + \mathrm{Prob}(air){\Delta T}_{Alle. Plat., air}
\end{equation}

\subsection*{\textsf{example\_bht\_corr.R}}

\textbf{Description}: Script to run the function \textsf{func\_BHT\_NY\_PA\_WV\_corr.R} with synthetic data in \textsf{bht\_test\_data.csv}. The output should be corrected BHTs and cases that generate all errors for missing values or values outside typical ranges.

\subsection*{\textsf{bht\_test\_data.csv}}

\textbf{Description}: Synthetic data to test \textsf{func\_BHT\_NY\_PA\_WV\_corr.R} for proper corrections and generating all possible errors.

\newpage

\section*{Outliers}
These codes, scripts, and files pertain to the outlier identification procedures.

\subsection*{\textsf{outlier\_identification.R}}

\textbf{Description}: Script with several functions to compute outliers using one of the specified algorithms with the specified inputs. Most algorithms have separate functions described below.

\begin{table}[H]
\begin{tabular} {p{2.5cm} p{10cm}}
\hline
\textbf{Function} & \textbf{Description}\\
\hline
\textsf{outlier\_iden} & General function to call other functions and perform outlier identification \\
\textsf{outlier\_loc\_pts} & Local outlier identification for \textsf{algo}=1\\
\textsf{outlier\_loc\_rad} & Local outlier identification for \textsf{algo}=2\\
\textsf{outlier\_loc\_grid} & Local outlier identification for \textsf{algo}=3\\
\textsf{outlier\_glob} & Global outlier identification\\
\hline
\end{tabular}
\end{table}

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Variable} & \textbf{Description}\\
\hline
\textsf{X} 			 & R data frame with variables named \\
 & \textsf{x\_coord}: Longitude coordinate in km\\
 & \textsf{y\_coord}: Latitude coordinate in km\\
 & \textsf{test}: Variable to be tested for being an outlier\\
\textsf{algo} & algorithm for determining local outlier\\
 & 1: (Default) Finds nearest points\\
 & 2: Finding all points within a given radius\\
 & 3: Gridding data\\
\textsf{outcri} & Outlier criteria\\
 & 1: (Default) Only local outliers flagged as outliers\\
 & 2: Only local and global outliers flagged as outliers\\
 & 3: Only global outliers flagged as outliers\\
\textsf{pt\_eval} & Number of points used in when \textsf{algo}=1 (default = 25)\\
\textsf{rad\_eval} & Radius (in km) at which to take points when \textsf{algo}=2 (default = 16)\\
\textsf{box\_size} & Size of spacing (in km) to form grids when \textsf{algo}=3 (default = 32)\\
\textsf{pt\_min} & Minimum number of points required to perform local test for \textsf{algo}=2 or 3 (default = 25)\\
\textsf{rad\_max} & Maximum radius (in km) at which to take points when \textsf{algo}=1 (default = 16)\\
\textsf{k\_glob} & Constant multiplied by the upper- and lower-half quartile ranges in global analysis (default = 3)\\
\textsf{k\_loc} & Constant multiplied by the upper- and lower-half quartile ranges in local analysis (default = 3)\\
\textsf{type}	 & Type of quantile estimation (default = 7, see R documentation)\\
\hline
\end{tabular}
\end{table}

The output is a data frame with the following columns added. The local outlier columns will only be added when \textsf{outcri}=1 or 2. The global outlier columns will only be added when \textsf{outcri}=2 or 3. The \textsf{outs} column will be present in all output.

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Output} & \textbf{Description}\\
\hline
\textsf{outs}	 & Binary variable for points being an outlier (1=outlier)\\
\hline
\textsf{out\_loc\_lo} & Binary variable for point being a local low outlier (1=outlier)) \\
\textsf{out\_loc\_hi} & Binary variable for point being a local high outlier (1=outlier) \\
\textsf{out\_loc\_lq} & Lower quartile for local outlier test (NA if not tested) \\
\textsf{out\_loc\_mq} & Median for local outlier test (NA if not tested) \\
\textsf{out\_loc\_uq} & Upper for local outlier test (NA if not tested) \\
\textsf{out\_loc\_lb} & Lower bound for local outlier test (NA if not tested)\\
\textsf{out\_loc\_ub} & Upper bound for local outlier test (NA if not tested)\\
\textsf{out\_loc\_rad} & Maximum distance to point (only for algo=1)\\
\textsf{out\_loc\_pts} & Number of points in local area (only for algo=2 and 3)\\
\textsf{out\_loc\_error} & Error in local outlier calculation\\
 & 0: No errors\\
 & 1: Some points outside \textsf{rad\_max} when \textsf{algo}=1)\\
 & 2: Fewer than \textsf{pt\_min} points in region when \textsf{algo}=2 or 3)\\
\hline
\textsf{out\_glob\_lo} &  Binary variable for points being an global low outlier (1=outlier)\\
\textsf{out\_glob\_hi} &  Binary variable for points being an global high outlier (1=outlier)\\
\hline
\end{tabular}
\end{table}

\textbf{Equations}: The equations used to calculate low and high outlier bounds are given below. In these equations $q$ is a variable of interest (\textsf{test} in the data) with subscripts denoting quantiles and $k$ is the constant (\textsf{k\_loc} or \textsf{k\_glob}). The equations can be applied locally or globally. Aguirre (2014) uses an outlier test that is similar, but the version implemented in this code is more flexible.

\begin{equation} \label{lb}
B_{lower} = q_{0.25} - k(q_{0.5} - q_{0.25})
\end{equation}

\begin{equation} \label{ub}
B_{upper} = q_{0.75} + k(q_{0.75} - q_{0.5})
\end{equation}

\subsection*{\textsf{example\_outlier\_code.R}}

\textbf{Description}: Script to run outlier identification functions with test data and the Cornell dataset. Later portions do not need to be run because they were testing sensitivity of the algorithm to the input parameters. They are kept in the code for potential future analysis.

\subsection*{\textsf{out\_test\_grid.csv}}

\textbf{Description}: Synthetic data to test the local outlier identification algorithm that uses gridding.

\subsection*{\textsf{out\_test\_rad.csv}}

\textbf{Description}: Synthetic data to test the local outlier identification algorithm that uses maximum radius.

\subsection*{\textsf{out\_test\_pt.csv}}

\textbf{Description}: Synthetic data to test the local outlier identification algorithm that uses number of points.

\subsection*{\textsf{cornell\_data.csv}}

\textbf{Description}: Cornell heat flow database with 8,919 points used to test sensitivity of the outlier identification algorithm. See Cornell University (2014).

\newpage

\section*{Reservoir Ideality}

\subsection*{\textsf{MainIdeality.m}}

\textbf{Description}: Script that runs the reservoir ideality uncertainty analysis. Subsidiary functions are called from this script. This script also imports an example dataset and uncertainty mapping from \textsf{TestFormationData.csv} and \textsf{TestUncertaintyLevels.csv}, respectively. This script can be modified to include more graphs and statistical analysis.

\subsection*{\textsf{GenRandNums.m}}

\textbf{Description}: Function to generate random numbers from uniform, triangular, normal, or lognormal distributions.

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Variable} & \textbf{Description}\\
\hline
\textsf{mean} 			 & Mean value of the distribution (real-space)\\
\textsf{unc} & uncertainty (spread) of the distribution as a percentage of the mean\\
 & uniform: bounds are defined from uncertainty\\
 & triangular: bounds are defined from uncertainty\\
 & normal: 95\% central region of distribution defined by uncertainty\\
 & lognormal: real-space coefficient of variation  defined by uncertainty\\
\textsf{dist} & Distribution selected\\
 & 1: Non-standard uniform distribution\\
 & 2: Triangular distribution, symmetric\\
 & 3: Normal distribution\\
 & 4: Lognormal distribution\\
\textsf{reps} & Number of replicates to generate\\
\hline
\end{tabular}
\end{table}

The output is a column vector with of numbers for the specified distribution.

\subsection*{\textsf{MonteCarloApprox.m}}

\textbf{Description}: Function to calculate Monte Carlo Approximation of the distribution of reservoir ideality.

\begin{table}[H]
\begin{tabular} {p{2cm} p{11cm}}
\hline
\textbf{Variable} & \textbf{Description}\\
\hline
\textsf{mat} 			 & Matrix of random values for reservoir ideality calculation\\
 & column 1: $k$, permeability/conductivity\\
 & column 2: $H$, thickness\\
 & column 3: $P_o$, pressure\\
 & column 4: $P_b$, pressure\\
 & column 5: $R_o$, radius\\
\textsf{mu} & Viscosity\\
\textsf{Rb} & Casing radius\\
\textsf{ideality} & ideality metric\\
 & 1: $(2\pi/\mu)kH(P_o - P_b)/(\ln(R_b) - \ln(R_o))$\\
 & 2: not currently used\\
\textsf{reps} & Number of replicates to generate\\
\hline
\end{tabular}
\end{table}

Note that in the code $P_{diff} = -|P_o - P_b|$ is used. The difference between $P_o$ and $P_b$ should be large so that this is generally not necessary, but it ensures that there are no problems. The output is a column vector with the distribution of the reservoir ideality.

\subsection*{\textsf{TestFormationData.csv}}

\textbf{Description}: Example file for input of formation data. The version of MATLAB used to develop this could not handle strings and numbers, so all input converted to numbers. The header line is dropped when reading-in the file.

\subsection*{\textsf{TestUncertaintyLevels.csv}}

\textbf{Description}: Example file for mapping uncertainty levels in \textsf{TestFormationData.csv} to percentage uncertainty. Column 1 is the uncertainty mapping (1-5). Columns 2-6 are the percentage uncertainty associated with that level for $k$, $H$, $P_o$, $P_b$, and $R_o$, respectively.

\newpage

\section*{References}

Aguirre, G. A. (2014). \textit{Geothermal Resource Assessment: A Case Study of Spatial Variability and Uncertainty Analysis for the States of New York and Pennsylvania}. Master's Thesis, Environmental and Water Resources Systems Engineering, School of Civil and Environmental Engineering, Cornell University.\hfill
\bigskip

\noindent
Cornell University (2014). Cornell University Heat Flow Database (NY and PA). Southern Methodist University Geothermal Laboratory. (Accessed 16 June 2014) geothermal.smu.edu/static/DownloadFilesButtonPage.htm \hfill
\bigskip

\noindent
Frone, Z. and Blackwell, D. (2010). Geothermal Map of the Northeastern United States and the West Virginia Thermal Anomaly. \textit{Geothermal Resources Council Transactions 34}:339-344.
\end{document}