%auto-ignore
\providecommand{\MainFolder}{..}
\documentclass[\MainFolder/Text.tex]{subfiles}
\begin{document}
\section{Approximation using heat form}\label{Sec:Hwe}
\allowdisplaybreaks

Given an oriented Riemannian manifold $(M,g)$, consider the \emph{heat form}
\begin{equation}\label{Eq:HK}
\KKer_t(x,y) = \sum (-1)^{kn}e^{-\lambda_i t} (\star e_i)(x) \wedge e_i(y),
\end{equation}
where $(e_i)$ are eigenvectors of $\Delta$ and $\lambda_i$ the corresponding eigenvalues. It is equivalently the Schwartz form of the operator $\exp(-\Delta t)$ (see \cite[Chapter~3]{Harris2004}) or a unique solution of the equation $\Laplace \KKer_t(x,y) = - \frac{\partial}{\partial t}\KKer_t(x,y)$ with $\lim_{t\to 0} \KKer_t = \DR_\Id$, where $\DR_\Id$ is the Schwartz form of the identity (see \cite{Hein2006}).
%
\begin{Proposition}[Properties of the heat kernel]\label{Prop:Heasd}
Let $M$ be an oriented Riemannian manifold. The heat form $\KKer_t(x,y)$ is smooth on $M\times M \times (0,\infty)$ and satisfies
$$ \Dd \KKer_t = 0, \quad \tau^*\KKer_t = (-1)^n\KKer_t\quad\text{and}\quad \frac{1}{2}\Delta \KKer_t = \Diag_x \KKer_t = - \frac{\partial }{\partial t} \KKer_t. $$
\end{Proposition}
\begin{proof}
Straightforward computation and a nice combinatorial argument.
\end{proof}
%
%\begin{proof}
%Well-known.
%%\begin{description}
%%\item[E)]
%%It suffices to prove it for $e^{-d(x,y)^2/t}$ by heat expansion.  Quantitively, i.e.\ making no distinction between $x_i$'s, we have
%%$$ \partial^l e^{-r^2/t} = p_l(x,t) e^{-r^2/t}, $$
%%where $p_l = (\frac{x}{t} + \partial_x)p_{l-1}$ with $p_0 = 1$, where
%%$$ \Abs{\underbrace{\frac{\partial }{\partial x_i} d^2(x,y)}_{\sim x}} \le C d(x,y), $$
%%and hence in the estimate we can replace $x$ by $r$. For $t\in (0,1)$ it holds $\frac{1}{t^a} \le \frac{1}{t^b}$ iff $a\le b$. In every term we have only
%%$$ \frac{r^a}{t^b} e^{-\frac{r^2}{t}} = \frac{1}{t^{b-\frac{a}{2}}}(\frac{r^2}{t})^{\frac{a}{2}} e^{-\frac{r^2}{t}} = \frac{1}{t^{b-\frac{a}{2}}}  \underbrace{\bigl[ 2^{\frac{a}{2}}\bigl(\frac{r^2}{2t}\bigr)^{\frac{a}{2}} e^{-\frac{r^2}{2t}} \bigr]}_{=:g_a(x), y= \frac{r^2}{2t}} e^{-\frac{r^2}{2t}} $$
%%for $a\le b$. But $g_a(y), y\ge 0$ is a bounded function. We have $p_l$ polynoms in $\frac{r}{t}$. By the recursive formulas we get that the terms $\frac{1}{t^{l/2}}$, resp. $\frac{r}{t^{(l+1)/2}}$ for $l$ even resp. $l$ odd have biggest $b-a/2$ equal to $l/2$. Indeed, both operations $x/t \cdot$ and $\partial_x$ increase $a-b/2$ by maximally $1/2$. We start with $a = 0$, $b=0$ for $l=0$ and we proceed by induction that the maximum of $a-b/2$ is $l/2$ in $p_l$. \qedhere
%%\end{description}
%\end{proof}
%
%We define
%$$ \begin{aligned}
%\GKer_t & = \int_t^\infty \bigl(\KKer_t - \HKer\bigr) \\
%\Prpg_t & = \int_t^\infty \CoDd\KKer_t
%\end{aligned} $$
%and denote $\GKer = \GKer_0$, $\Prpg = \Prpg_0$.
%
%\begin{Lemma}[Integrals dependent on parameter] \label{Lem:IntPar}
%Let $I$, $J\subset \R$ be intervals and $f(a,u) : I \times J \rightarrow \R$. Suppose that:
%\begin{enumerate}
% \item $I$ is open
% \item For all $a\in I$ is $f(a,\cdot)$ measurable in $J$.
% \item For almost all $u\in J$ is $f(\cdot,u)$ differentiable in $I$
% \item There is a $g\in L^1(J)$ such that $\Abs{\frac{\partial}{\partial a} f(a,u)} \le g(u)$ for almost all $u\in J$ and all $a\in I$
% \item There is an $a_0\in I$ such that $f(a_0,\cdot) \in L^1(J)$
%\end{enumerate}
%Then $F(a) \coloneqq \int_J f(a,u) \Diff{u}$ is finite and
%$$ F'(a) = \int_J \frac{\partial }{\partial a} f(a,u) \Diff{u} $$
%\end{Lemma}
\begin{Proposition}[Approximation using heat form]\label{Prop:HeatKerFormulas}
Let $M$ be an oriented Riemannian manifold and $\KKer_t(x,y)$ the heat form.
For all $(t,x,y)\in \bigl([0,\infty)\times M \times M\bigr)\backslash \{0\}\times \Diag =: D(\KKer)$, define
\begin{equation}\label{Eq:HeatKerApprox}
\begin{aligned}
\GKer_t(x,y) &\coloneqq \int_t^\infty \KKer_\tau(x,y)\Diff{\tau}\quad\text{and}\\
\Prpg_t(x,y) &\coloneqq (-1)^{n+1}\int_t^\infty (\Id\COtimes\CoDd_y) \KKer_\tau(x,y)\Diff{\tau}.
\end{aligned}
\end{equation}
Then:
\begin{ClaimList}
\item The forms $\GKer_t$ and $\Prpg_t$ are smooth on $D(\KKer)$, the point-wise limits $\GKer'$ and $\Prpg'$ as $t\to 0$ exist, and it holds $\GKer_t \darrow[t]\GKer'$ and $\Prpg_t\darrow[t]\Prpg'$ as $t\to 0$ (uniform convergence) in $C^\infty_{\text{loc}}(M\times M\backslash\Diag)$.
\item On $D(\KKer)$, the following relations hold:
\begin{align*}
\Dd \GKer_t &= 0 & \Prpg_t &= (-1)^{n+1}\frac{1}{2} \CoDd\GKer_t \\
\Laplace \GKer_t &= \KKer_t - \HKer & \Dd \Prpg_t &= (-1)^n(\HKer - \KKer_t) \\
\tau^*\GKer_t &=(-1)^n\GKer_t  & \tau^* \Prpg_t &= (-1)^n \Prpg_t.
\end{align*}
It follows that $\GKer' = \GKer$ is the (Laplace) Green form and $\Prpg' = \StdPrpg$ the standard Hodge propagator.
\end{ClaimList}
\end{Proposition}

\begin{proof}
The formal computation is clear. An honest proof uses the standard heat kernel estimates.
\ToDo[caption={Say more},noline]{Say more about the proofs!}
%The relations follow from
%$$ \Dd \KKer_t = 0\quad\text{and}\quad (\CoDd_x\COtimes\Id)\KKer_t = (\Id\COtimes\CoDd_y) = \frac{1}{2}\CoDd\KKer_t. $$
%Use properties of the heat kernel,... Also see Harris, Heine,.. We have that $\CoDd = \CoDd_x \COtimes \Id + \Id \COtimes \CoDd_y$, it holds $(\CoDd_x \COtimes \Id)\tau^* = \Id \COtimes \CoDd_y$.
\end{proof}

\begin{Proposition}[$\StdPrpg$ is codifferential of $\GKer$]\label{Prop:StdCodifInt}
Let $M$ be an oriented Riemannian manifold, and let $\GKer\in \DR^n(M\times M\backslash\Diag)$ be the Green form. Then the standard Hodge propagator $\StdPrpg$ satisfies
\begin{equation}\label{Eq:FormForPUsingG}
\StdPrpg(x,y)= (-1)^{n+1}(\Id\otimes \CoDd_y)\GKer(x,y),
\end{equation}
where $\Id\otimes \CoDd_y: \DR^\bullet(M\times M) \rightarrow \DR^{\bullet-1}(M \times M)$ is the differential operator defined in local coordinates by commuting $\CoDd$ over the first factor with the Koszul sign and applying it to the second factor.
\end{Proposition}
\begin{proof}
As for the signs, $(-1)^n$ comes from $\TOp \GOp \omega(y) = (-1)^{nT}\int_x(\Id \otimes \TOp_y)\GKer(x,y)\omega(x)$ with $T=\CoDd$ and $-1$ from $\StdHtp = - \CoDd\GOp$.
The rest can be proven using the heat kernel approximation and standard heat kernel estimates.
There is an other method using the asymptotic expansion of $\GKer$, which was shown to the author by Prof.~Dr.~Christian~Bär.
\ToDo[caption={Say more},noline]{Say more about the proofs!}
\end{proof}
%We have the following.
%
%\begin{Proposition}[Asymptotic expansion of $\KKer_t$ and $\GKer$]
%\end{Proposition}
%
%\begin{itemize}
% \item Asymptotic expansion of
% \item
%\end{itemize}

%
%Does $\GKer$ extend to the blow-up?
%
%Can I use this to show that $\CoDd \GKer$ does too extend to blow-up?

%
%\begin{Remark}[Operators not extending to blow-up]
%Under the change $x=u+r\omega$, $y=u-r\omega$ and $\tilde{f}(r,\omega,u) = f(x,y)$, we compute the following
%\begin{align*}
%\frac{\partial \tilde{f}}{\partial x^i} & = \frac{1}{2} \omega_i \frac{\partial \tilde{f}}{\partial r} + \frac{1}{2r}\sum_{j=1}^{n+1}(\delta_{ij} - \omega_i\omega_j)\frac{\partial\tilde{f}}{\partial \omega^j} + \frac{1}{2}\frac{\partial\tilde{f}}{\partial u^i} \\
%\frac{\partial \tilde{f}}{\partial y^i} & = -\frac{1}{2} \omega_i \frac{\partial \tilde{f}}{\partial r} - \frac{1}{2r}\sum_{j=1}^{n+1}(\delta_{ij} - \omega_i\omega_j)\frac{\partial\tilde{f}}{\partial \omega^j} + \frac{1}{2}\frac{\partial\tilde{f}}{\partial u^i}
%\end{align*}
%Because of the middle $\frac{1}{r}$ these do not always descend. We also compute
%\end{Remark}
\end{document}


% Adapted from https://www.overleaf.com/latex/templates/problem-set-template/bdwzvbkxyjfg

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{graphicx} % for figures
\usepackage{grffile}
\usepackage{amsmath}  % for extended math markup
\usepackage{amssymb}
\usepackage{bm}
\usepackage{xfrac}
\usepackage{enumerate}
\usepackage{float}
\usepackage{multirow}
\usepackage[margin=1cm]{caption}
\usepackage[bookmarks=false]{hyperref} % for URL embedding

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

\newcommand{\Course}{CSE515T: Bayesian Methods in Machine Learning}

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

% these are common math formatting commands that aren't defined by default
\newcommand{\union}{\cup}
\newcommand{\isect}{\cap}
\newcommand{\ceil}[1]{\ensuremath \left\lceil #1 \right\rceil}
\newcommand{\floor}[1]{\ensuremath \left\lfloor #1 \right\rfloor}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\norm}[1]{\ensuremath \mid\mid #1 \mid\mid}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\data}{\mc{D}}

\DeclareMathOperator*{\argmin}{\arg\min}
\DeclareMathOperator*{\argmax}{\arg\max}

\newenvironment{problem}[2][Problem]
{\ifnum\value{page}>1 \newpage\else\fi\noindent{\bfseries #1}  {\bfseries #2.}}
{\mbox{}\\\vspace{-.5em}\noindent\rule{\textwidth}{0.4pt}\hfil\vspace{1em}}

\numberwithin{equation}{section}

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

\begin{document}
\title{Project Report}
\author{Alexis Park, Jonathan Chen, Kevin Xie \\ \Course}

\maketitle

\section{Data visualization}

The Branin unction is defined as follows:

\begin{equation}
  f(\bm{x}) = a(x_2 - bx_1^2 + cx_1 - r)^2 + s(1 - t)\cos(x_1) + s
  \label{eq:Branin}
\end{equation}

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Data Visualization/trueBraninHeatmap.png}
  \caption{Heatmap of True Branin function values with domain $\mc{X} = [-5, 10] \times [0, 15]$  with 1000 values per dimension.}
  \label{fig:true_Branin_heatmap}
\end{figure}

From figure \ref{fig:true_Branin_heatmap}, we can see that the function's
values fluctuate in a somewhat sinusoidal manner, with a large minimal
``trench'' spanning diagonally across the center of the domain with steadily
increasing regions along the trench's sides. Therefore the function does not
appear to be stationary.

In order to make the function more stationary, we tried a variety of
transformations. By analyzing the equation, we noted that the sinusoidal
fluctuations can be attributed to both the added cosine term and the 4th
order polynomial term. We thus attempted to pass the data through a
transformation that combined an inverse cosine function ($\arccos$) with the
cumulative density function of the normal probability distribution
($\Phi(x)$). However, taking the $\log$ of the data gave us the most
stationary result as shown in figure \ref{fig:log_Branin_heatmap}.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Data Visualization/Log_Branin_Heatmap.png}
  \caption{Heatmap of log Branin function values with domain $\mc{X} = [-5,
  10] \times [0, 15]$ with 1000 values per dimension. The plot shows the
  behavior of the Branin function is more constant throughout the domain when
  compared to the original function's behavior.}
  \label{fig:log_Branin_heatmap}
\end{figure}

Next, we plotted a kernel density estimate (KDE) of LDA and SVM benchmarks
(Fig. \ref{fig:kde-lda-svm}A and \ref{fig:kde-lda-svm}C). By analyzing the
resultant probability density graphs, we identified that KDE of LDA show
log-normal like distribution. Also, both estimates have relatively similar
behavior but on significantly different scales.

Since KDE of LDA show log-normal like distribution, the log of LDA values
should produce the normal distribution, which resulted in better performance
(Fig. \ref{fig:kde-lda-svm}B). As we expected, the (log) LDA KDE is more
distributed throughout the graph and relatively less concentrated around the
peak. However, the (log) SVM KDE made the distribution less normal as shown
in figure \ref{fig:kde-lda-svm}D.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/kde_lda_svm.eps}
  \caption{Kernel density estimates for LDA (green, A and B) and SVM (purple, C and D) datasets, along with the estimates for their logs.}
  \label{fig:kde-lda-svm}
\end{figure}

\section{Model fitting}
\subsection*{Model fitting with Branin function}
For model fitting, we used 32 Sobol sequence Branin function training points
and fit the data to a Gaussian Process (GP) model with constant mean with
initial value of 0, squared exponential covariance with initial values of
length scale and output scale of 1. After optimization by maximizing the
marginal likelihood, we obtained the following values for the
hyperparameters:
\begin{itemize}
  \item
    mean: 137.19
  \item
    length scale: 4.00
  \item
    output scale: 114.05
\end{itemize}
Although we assumed that there is no noise in the data, the exact Gaussian likelihood is parameterized and the optimization procedure also fit it's value (1e-3). This is fine because the number is small, but also because this increases the numerical stability of the GP.

Based on figure \ref{fig:gp_post_mean_heatmap}, both the mean and the length
scale values are reasonable. The range of the Branin function extends from
approximately 0 to 350, with more mass below the approximate "midpoint" value
of 175. Thus, an optimized function mean of 137.19 is also reasonable. The
overall slowly oscillating behavior of the Branin function also appears to
have a "periodicity" of approximately six units; thus, a length scale of
4.00 is acceptable. The oscillations themselves have some combination of
amplitudes that range from small towards the center of the function's domain
to very large towards the edges; thus, an output scale of
114.05 is acceptable.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Model Fitting/GP_Posterior_Mean.png}
  \caption{Heatmap of GP posterior mean}
  \label{fig:gp_post_mean_heatmap}
\end{figure}

Comparing the predicted GP posterior mean to the true Branin function values,
figure \ref{fig:gp_postmean_and_trueval} shows that the training points lie in
the darkest spots on the heatmap, where the predicted and
true values are almost identical. Similarly, as the predictive mean's
location departs from the areas where the training values are known, the absolute
differences increase.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Model Fitting/GP_Posterior_Mean_vs_True_Branin.png}
  \caption{Heatmap of GP posterior mean and True Branin values}
  \label{fig:gp_postmean_and_trueval}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Model Fitting/GP_Posterior_s2_With_Training_Points.png}
  \caption{Heatmap of the GP posterior standard deviation}
  \label{fig:gp_post_std}
\end{figure}
As shown in figure \ref{fig:gp_post_std}, as you approach the training data points, the standard deviation approaches zero; Areas far from the training points therefore have significantly higher uncertainty. Furthermore, the scale of the standard deviation extends from 0 to 25. This behavior is expected, as one would expect the GP model to be certain near the training points (where it knows the underlying function passes through), and more uncertain as it gets farther from these points. The scale of this uncertainty is similarly reasonable. As previously mentioned, the Branin function has both small and large variations. Thus, the GP must return a relatively high standard deviation at the locations where it is uncertain in order to properly accommodate the large range of possible fluctuations.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Model Fitting/Z_Score_KDE.png}
  \caption{Kernel density estimate of the z-scores of the residuals between the GP posterior mean and true values.}
  \label{fig:zscore_kde}
\end{figure}
Based on figure \ref{fig:zscore_kde}, the KDE of the Z-score distribution follows an approximately standard normal distribution with more concentrated density at the peak.

\subsection*{Model fitting with log transformation of Branin function}
We repeated model fitting using a log transformation of the Branin function. After maximizing the marginal likelihood under the same conditions as the previous fitting attempt, we obtained the following hyperparameter values:
\begin{itemize}
  \item
    mean: 3.62
  \item
    length scale: 3.32
  \item
    output scale: 1.63
\end{itemize}
Again, the optimization procedure also fit the likelihood, resulting in a hyperparameter value of 0.001.

The log Branin posterior mean ranges from approximately 0.5 to 6 as shown in figure \ref{fig:gp_post_mean_log}, with more area above the midpoint value of 3.25. Thus, the hyperparameter mean value with a slight increment from the "midpoint" follows our expectation. The range of the function also supports an output scale of 1.63, which indicates a relatively "flat" function, as expected. Finally, the oscillatory behavior of the log branin function appears to be slightly less frequent; thus, a length scale of 3.32 is reasonable.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Log Fits/GP_Posterior_Mean_Log.png}
  \caption{Heatmap of GP posterior mean of log transformed Branin function values}
  \label{fig:gp_post_mean_log}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Log Fits/GP_Posterior_vs_Log_True.png}
  \caption{Heatmap of GP posterior mean and true log transformed Branin function values}
  \label{fig:gp_post_mean_true_log}
\end{figure}
The absolute difference between the predicted values and the true values are nearly zero as shown in the darkest areas of figure \ref{fig:gp_post_mean_true_log} like in figure \ref{fig:gp_postmean_and_trueval}. Additionally, the values farther away from the training points have increasing absolute differences, including areas which show very high absolute differences.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Log Fits/GP_Posterior_S2_and_Training_points.png}
  \caption{Heatmap of GP posterior standard deviation of log transformed Branin function values}
  \label{fig:gp_post_std_log}
\end{figure}
As shown in figure \ref{fig:gp_post_std_log}, the standard deviation again approaches zero as the predicted values approach the training points. The scale of the standard deviation extends from 0 to approximately 0.7. As explained previously, this behavior is expected: the GP model should be more confident the closer it is to a training point. The application of the log function to the Branin model should also reduce the scale of the standard deviation, as seen.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Log Fits/Z_Score_KDE.png}
  \caption{Kernel density estimate of z-score of log transformed Branin function values}
  \label{fig:zscore_kde_log}
\end{figure}
Based on figure \ref{fig:zscore_kde_log}, the KDE of the Z-score distribution is similar a "focused" Gaussian distribution.

By using a log transformation of the Branin function, the marginal likelihood is improved and more calibrated: the absolute difference between predicted and true values are much lower in figure \ref{fig:gp_post_mean_true_log} than in figure \ref{fig:gp_postmean_and_trueval}, which indicates that the model trained from log transformed data has higher marginal likelihood.

\subsection*{Bayesian Information Criterion (BIC)}
In order to compare which model fits better with the dataset, we used the BIC
to evaluate how well those choices fit a given dataset. To compute the BIC,
we found the values of the hyperparameters maximizing the (log) marginal
likelihood using \ref{eq:hyperparameter-estimate}, and then \ref{eq:bic},
where $\mid \theta \mid$ is the total number of hyperparameters and $\mid
\data \mid$ is the number of observations. We used three for $\mid \theta
\mid$ and 32 for $\mid \data \mid$.

From equation \ref{eq:bic}, it can be seen that the first term should be
minimized to prevent overfitting, while the second term should be maximized,
since it corresponds to the model evidence. Overall, lower BIC values imply
better model fit.

\begin{equation}
  \hat \theta = \argmax_\theta \log p(y \mid X, \theta)
  \label{eq:hyperparameter-estimate}
\end{equation}

\begin{equation}
  \text{BIC} = \mid \theta \mid \log \mid \data \mid - 2 \log p(y \mid X, \hat \theta)
  \label{eq:bic}
\end{equation}

\begin{table}[H]
  \centering
  \begin{tabular}{| l | l |}
    \hline
    Data       & BIC score \\
    \hline
    Branin     & 304.3118  \\
    \hline
    log Branin & 61.4710   \\
    \hline
  \end{tabular}
  \caption{BIC score of data from model fitting (Branin and (log) Branin)}
  \label{tab:basic_bic}
\end{table}

Now considering BIC as a function of the choice of mean and covariance functions $(\mu, K)$, we used MeanConst and four different covariance functions, Squared Exponential (SE), Rational Quadratic (RQ), sum of SE and RQ, and product of SE and RQ. Table \ref{tab:bic_score} shows the best model and BIC scores we found for each function.

\begin{table}[H]
  \centering
  \begin{tabular}{| l | l | l |}
    \hline
    Data       & Model                & BIC score \\
    \hline
    Branin     & Rational Quadratic   & 299.367   \\
    \hline
    log Branin & Squared Exponential  & \textbf{61.471}    \\
    \hline
    LDA        & Product of SE and RQ & 6.777e6 \\
    \hline
    log LDA    & Rational Quadratic   & \textbf{17.298}    \\
    \hline
    SVM        & Product of SE and RQ & \textbf{-138.894}  \\
    \hline
    log SVM    & Rational Quadratic   & -77.547   \\
    \hline
  \end{tabular}
  \caption{Computed BIC scores and best model for different functions and their log transformations. The best value for each function is shown in bold.}
  \label{tab:bic_score}
\end{table}

\section{Bayesian Optimization}

The best-fitting models were selected from the previous experiments. We then used the Expected Improvement (EI) acquisition function, defined as:
\begin{equation}
  a_{EI}(\bm{x}) = (f' - \mu(\bm{x}))\Phi(f';\mu(\bm{x}),K(\bm{x},\bm{x})) + K(\bm{x},\bm{x})\mathcal{N}(f';\mu(\bm{x}),K(\bm{x},\bm{x})),
  \label{eq:Expected Improvement}
\end{equation}
where $\Phi(\bm{x})$ is the cumulative probability density of the Normal distribution, and $f'$ is the minimum value of the current observations.
Note that the EI acquisition function combines both exploitation and exploration by using the posterior mean and known minimum value, and the posterior standard deviation, respectively.

Using the previously selected 32 points, a GP model was fit using the aforementioned optimized settings and the following heatmaps of the posterior mean and standard deviation of the log Branin function were created:

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Optimization Plots/EI_FMu_Log.png}
  \caption{Predictive posterior mean of the log Branin function, calculated using a previously optimized GP model trained on 32 Sobol sequence points. Warmer colors indicate higher values. Note the predicted minimum areas in dark blue at the top left and bottom right corners of the plot.}
  \label{fig:ei-fmu-log}
\end{figure}

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Optimization Plots/EI_S_Log.png}
  \caption{Predictive posterior standard deviation of the log Branin function, calculated using a previously optimized GP model trained on 32 Sobol sequence points. Warmer colors indicate higher deviations and imply greater uncertainty in the prediction.}
  \label{fig:ei-fs-log}
\end{figure}

The EI function values were then calculated using the posteriors and the point $(7.658, 0)$ was identified as the optimal point to observe next.
A heatmap of the EI values is shown below with the optimal observation location marked as a black point:

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/Optimization Plots/EI_Log_Optimal_point.png}
  \caption{Expected Improvement acquisition values of the optimized GP log Branin model trained on 32 Sobol sequence points. Warmer colors indicate higher expected improvement. The maximum expected improvement is marked as a black point and denotes the optimal location for the next observation.}
  \label{fig:ei-optimalPoint-log}
\end{figure}

Analyzing figures \ref{fig:ei-fmu-log}, \ref{fig:ei-fs-log}, and \ref{fig:ei-optimalPoint-log} above, we reason that the proposed optimal testing point is ideal. From the posterior mean, it can be seen that the point $(7.658, 0)$ is within a region of predicted minimal values. Furthermore, from the posterior standard deviation, it can be seen that the point is also within a region of higher uncertainty and therefore reduced predictive confidence. Thus, it is plausible that the EI acquisition function would seek to test this area and this specific point in order to identify a possible global minimum and improve confidence in an area of uncertainty.

The following Bayesian active learning experiment was then applied independently to the log Branin, log LDA, and SVM functions:
\begin{enumerate}
  \item Five initial observations were randomly selected, constituting the initial dataset $\mathcal{D}$.
  \item For the log Branin function only, a dense, evenly spaced grid of 250,000 points was generated within the domain of the function for use in calculating the GP predictive posterior.
  \item A GP model using the respective aforementioned optimized settings was fit to $\mathcal{D}$.
  \item A new point \emph{x} was found using the EI acquisition function and the GP predictive posterior.
  \item The function value \emph{f(x)} was calculated and the point (\emph{x}, \emph{f(x)}) was added to $\mathcal{D}$.
  \item Steps 3-5 were repeated 30 times, resulting in a final dataset $\mathcal{D}$ of 35 points.
\end{enumerate}

The performance of each of the above experiments was evaluated using the "gap" measure, defined for minimization as:
\begin{equation}
  \text{gap} = \dfrac{f(\text{best found}) - f(\text{best initial})}{f(\text{minimum}) - f(\text{best initial})}
  \label{eq:gap}
\end{equation}
The gap can be interpreted as a measure of the accuracy or performance of the optimization; as the gap value approaches one, the attempted optimization approaches the true global minimum.

The gaps for the log Branin, log LDA, and SVM models were calculated to be 1.00, 0.79, and 0.96, respectively.
This implies that EI successfully found a global minimum of the log Branin function, but missed the global minimum of the log LDA and SVM functions.

The above bayesian active learning experiment was then modified as such:
\begin{enumerate}
  \item A seed for the random number generator (RNG) was chosen.
  \item Five initial observations were randomly selected, constituting the initial dataset $\mathcal{D}$.
  \item For the log Branin function only, a dense, evenly spaced grid of 250,000 points was generated within the domain of the function for use in calculating the GP predictive posterior.
  \item A GP model using the respective aforementioned optimized settings was fit to $\mathcal{D}$.
  \item A new point \emph{x} was found using the EI acquisition function and the GP predictive posterior.
  \item The function value \emph{f(x)} was calculated and the point (\emph{x}, \emph{f(x)}) was added to $\mathcal{D}$.
  \item Steps 4-6 were repeated 150 times, resulting in a final dataset $\mathcal{D}$ of 155 points.
\end{enumerate}
The above procedure was duplicated, only the Random Search acquisition function (which selects a point at random) was used in place of EI.
These experiments were repeated 20 times with 20 different RNG seeds to create different random initializations for the log Branin, log LDA, and SVM functions, resulting in a total of 60 experiments.
The learning curves of these 60 trials using only the first 30 new observations for both EI and RS acquisition functions are shown in the figures below:
\begin{figure}[H]
  \centering
  \includegraphics[width=\textwidth]{figures/optimization_log_Branin.eps}
  \caption{Learning curves for 20 log Branin function experiments. The EI acquisition function is shown in blue, and the RS acquisition function is shown in orange. Observe that EI consistently outperforms RS in both rate of gap increase and final gap convergence at 30 observations.}
  \label{fig:ei-v-rand-branin}
\end{figure}
\begin{figure}[H]
  \centering
  \includegraphics[width=\textwidth]{figures/optimization_log_lda.eps}
  \caption{Learning curves for 20 log LDA function experiments. The EI acquisition function is shown in blue, and the RS acquisition function is shown in orange. Note that EI and RS both consistently converge towards the same gap value at 30 observations}
  \label{fig:ei-v-rand-lda}
\end{figure}
\begin{figure}[H]
  \centering
  \includegraphics[width=\textwidth]{figures/optimization_norm_svm.eps}
  \caption{Learning curves for 20 SVM function experiments. The EI acquisition function is shown in blue, and the RS acquisition function is shown in orange. It can be seen that EI performs at least as well as RS in the majority of experiments, but can find and get stuck in a local minima, as seen in Seed 18.}
  \label{fig:ei-v-rand-svm}
\end{figure}
From figures \ref{fig:ei-v-rand-branin}, and \ref{fig:ei-v-rand-svm} above, it can be seen that EI significantly outperforms RS on both the log Branin and the SVM functions.
It is evident that in these experiments, EI often converges to its final gap value in significantly fewer observations than RS does, and that its final gap values at 30 observations are generally higher than those of RS. This behavior was expected, as EI combines both exploitation and exploration together to identify locations with higher expected benefit, whereas RS only applies (random) exploration. The addition of exploitation and non-random exploration in EI allows it to actually navigate towards optima, whereas RS can only approach optima through chance.
However, there are cases where EI and RS have similar performance: from figure \ref{fig:ei-v-rand-lda}, it can be seen that the two acquisition functions perform similarly to each other in the majority of experiments at 30 observations.
We believe that EI's lack of performance on the log LDA function can be attributed to a large amount of local minima; EI can often become stuck in local minima, thereby decreasing its performance, whereas RS is purely exploratory and therefore unhindered by the presence of any local optima.

The difference in performance between EI and RS can be further seen when one considers their mean gaps at 30, 60, 90, 120 and 150 observations, as shown in the table below:
\begin{table}[H]
  \centering
  \begin{tabular}{| c | c | c | c | c | c | c |}
    \hline
    \# Observations & EI (Branin) & RS (Branin) & EI (LDA)& RS (LDA) & EI (SVM)& RS (SVM)\\
    \hline
    30 & 0.9072 & 0.3409 & 0.4877 & 0.7122 & 0.9106 & 0.3728 \\
    \hline
    60 & 0.9532 & 0.6086 & 0.9611 & 0.7944 & 0.9415 & 0.5676 \\
    \hline
    90 & 0.9532 & 0.7124 & 0.9753 & 0.9174 & 0.9592 & 0.9190 \\
    \hline
    120 & 0.9532 & 0.9096 & 0.9753 & 0.9633 & 0.9018 & 0.9716 \\
    \hline
    150 & 0.9532 & 0.9202 & 0.9473 & 0.9016 & 0.9018 & 0.9737 \\
    \hline
   \end{tabular}
   \caption{Mean Gaps of EI and RS on the log Branin, log LDA, and SVM functions at 30, 60, 90, 120 and 150 observations. Note that EI reaches a higher gap at 150 observations in all functions when compared to RS, signifying better performance overall. Furthermore, for the log Branin and SVM functions, EI achieves these higher values significantly faster than RS does.}
   \label{tab:ei-rs-observations}
\end{table}

It is evident that EI outperforms RS on all three functions, including log LDA, at 150 observations.
Additionally, EI generally requires less observations to achieve higher gap values, implying a faster learning rate than RS. We also note that EI likely spends considerable time early on navigating local minima in the log LDA function,
as evident by its noticeably worse performance at 30 observations when compared to that of RS. However, EI for log LDA rapidly improves following this local-minima exploration and quickly outperforms RS. It is also important to note that on average, no method was able to find the global minimum. This result was expected for RS, which would need to be extraordinarily lucky to find the global minimum, but was somewhat surprising for EI. It appears that in the limit, EI may become entrenched in local minima and thus converges towards a suboptimal result.

We more rigorously compare the performance of EI and RS on all three functions using a paired t-test and evaluate the null hypothesis that the gap values attained for EI and RS come from the same distribution (i.e. the performance of EI and RS are equivalent or similar). We begin the test using only 30 observations for both EI and RS and calculate the corresponding p-values, as seen in the table below:

\begin{table}[H]
  \centering
  \begin{tabular}{| c | c |}
    \hline
    Function & P-Value  \\
    \hline
    Log Branin   & 5.39e-4 \\
    \hline
    Log LDA      & 0.38  \\
    \hline
    SVM      & 0.039   \\
    \hline
  \end{tabular}
  \caption{P-Values comparing the similarity of performance between EI and gap on the log Branin, log LDA and SVM functions. Higher p-values indicate more similar performance.}
  \label{tab:ei-rs-pvals}
\end{table}

From the table above, it is quite obvious that at 30 observations, EI and RS have significantly different performance on the log Branin and SVM functions; however, the two acquisition functions have similar performance on the LDA function. Such behavior was supported visually in figures \ref{fig:ei-v-rand-branin}, \ref{fig:ei-v-rand-lda} and \ref{fig:ei-v-rand-svm}.

To identify the number of observations required for RS and EI to exhibit similar performance, we then began RS at one observation and increment the number of observations for RS while holding EI at 30 observations until the p-value exceeds 0.05. The resultant number of observations before RS attains possibly similar performance to EI is shown below:

\begin{table}[H]
  \centering
  \begin{tabular}{| c | c | c |}
    \hline
    Function & Observations Required & P-Value \\
    \hline
    Log Branin & 59 & 0.0624 \\
    \hline
    Log LDA & 10 & 0.0599 \\
    \hline
    SVM & 3 & 0.0951 \\
    \hline
  \end{tabular}
  \caption{Number of observations required for RS before it achieves possibly similar performance to EI with 30 observations for the log Branin, log LDA, and SVM functions.}
  \label{tab:ei-rs-pvals-to-equivalence}
\end{table}

Looking at tables \ref{tab:ei-rs-pvals} and \ref{tab:ei-rs-pvals-to-equivalence}, one can see that in the log Branin function, EI significantly outperforms RS. Specifically, 59 points must be observed before RS and EI show possibly similar performance.
However, it is also evident that RS likely performs as well as EI on the log LDA function. This was expected, given their similar learning curves above in figure \ref{fig:ei-v-rand-lda}.
Perhaps more interestingly, the performance of RS varies significantly relative to that of EI for the SVM function: when there are less observations, the two acquisition methods, on average, demonstrate similar performance. However, as the number of observations increase, the two methods begin to differ before gradually converging with each other again. Another iterative paired t-test experiment was performed, and it was determined that such oscillatory behavior continued until 44 new observations were attained, after which the performance of EI and RS remained consistently comparable.

\section{Bonus}
For the bonus section, we implemented two more acquisition functions: max
variance and lower confidence bound (LCB). We also tried optimizing hyperparameters with the addition of each observation to the data set, dubbed ``online optimization'' (OO) for fun, and injected more exploration by randomly sampling the next point instead of using the acquisition function suggested point with some probability.

\subsection*{More Acquisition Functions}
We chose to implement max variance (eq. \ref{eq:max-variance}) as one of our
acquisition functions because it seemed like an intuitive way to explore a
function's values and shape. If our objective is to build a model of a target
function by actively searching the parameter space, then it makes sense to
use our current belief about $f$ (the posterior) to pick the next point to
sample. In the case of max variance, we would sample where our model is least
certain. \emph{A priori}, we wouldn't expect max variance to be optimal for
identifying the minima of $f$ since the objective it is optimizing only
incentivizes exploration and not exploitation.
\begin{equation}
  \alpha_{\max \text{Var}}(x) = K(x, x)
  \label{eq:max-variance}
\end{equation}

We also selected the minimization formulation of the upper confidence bound acquisition function, dubbed lower confidence bound (eq. \ref{eq:lcb}). This  computes the trade off between exploitation and exploration directly by mixing naive greedy exploitation with just the minimum of the mean function and max variance from before.
\begin{equation}
  \alpha_{LCB}(x; \beta) = \beta \sigma(x) - \mu(x), \text{ where } \sigma(x) = \sqrt{K(x, x)}
  \label{eq:lcb}
\end{equation}
Looking at it's formulation, we can see that it is the difference between the
standard deviation and the mean function value of the posterior. It is also
parameterized by $\beta$, which allows us to control the amount of
exploration we want to do. We can see that the function will be greatest when
the mean function value is low and when the variance is high. LCB effectively
addresses the limitation of the max variance acquisition function by
extending it to do exploitation as well.

We evaluated the performance of each acquisition function on a 300-by-300
grid from the Branin function using a procedure similar to the one used in
Section 3: starting with five randomly selected points, we fit a GP, computed
acquisition values, used the maximum of those values to choose our next
point, and computed the gap value of the new dataset. We did ten trials with
100 iterations of active observation for each function and averaged the gap
values to get those shown in Figure \ref{fig:bonus-gaps} as solid lines.

On their own, max variance and LCB are slower to find the global minimum than
expected improvement. Although not shown in the figure, max variance
performed similarly to random search. This is what we expected since max
variance does not do any exploitation. LCB performs relatively better, but is
slower to move out of local minima. This is likely because of it's
naive-greedy nature and is somewhat expected.

\begin{figure}[h]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/bonus-gaps-by-acqfn.eps}
  \caption{A busy plot showing gap as a function of the number of
  observations made.}
  \label{fig:bonus-gaps}
\end{figure}

\subsection*{Forcing More Exploration}
It is possible for our acquisition values to get stuck in local minima for too long. To mitigate this, we force each acquisition function to do more exploration by using a randomly sampled point with probability $p = 0.1$ instead of the one suggested by the acquisition function. From Figure \ref{fig:bonus-gaps}, we can see that occasionally picking random samples did not help our models converge. It is possible that this is true only for the Branin function, but we can't say from this experiment alone.

\subsection*{Online Optimization}
We also tried optimizing the model hyperparameters after each iteration. This
is representative of how Bayesian optimization is performed in practice. It
makes sense to do this for acquisition functions that rely on the model posterior. If we are evaluating an expectation, like in EI, then it makes sense to use the posterior of the most-likely model. For LCB, we use the mean and variance of the posterior directly, which means that its performance will improve if we have better estimates of both from a more likely model. We didn't expect max variance to improve because it's lack of exploitation still constrains its performance even with optimized hyperparameters. These hypotheses are confirmed in Figure \ref{fig:bonus-gaps}. The gap values with online optimization converge to one faster for both EI and LCB. Here we see that LCB actually converges faster that EI with online optimization.
\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\section{Sheet 5}

\subsection{Alternative derivation of the contracted Bianchi identities}

The form of the Riemann tensor in a LIF was derived in section \ref{sec:LIF-form-Riemann-tensor}.

\subsubsection{Bianchi identities of the Riemann tensor}

In the LIF, given that \(  R_{\mu \nu \rho \sigma} = g_{\mu [\sigma |, \nu |\rho ]} - g_{\nu  [\sigma |, \mu | \rho ]} \), we want to show that \(R_{\mu \nu [\rho \sigma ; \alpha  ]} = R_{\mu \nu [\rho \sigma , \alpha ]} =0\).

This is equivalent to the formulation of the Bianchi identities given in the problem sheet, because of the antisymmetry of the Riemann tensor in its last two indices: there are six terms in the antisymmetrization of \(R_{\mu \nu [\rho \sigma , \alpha ]}\), but they are pairwise equal: the term \(R_{\mu \nu \rho \sigma , \alpha }\) is equal to \(- R_{\mu \nu \sigma \rho , \alpha }\) by antisymmetry, and these are exactly the pairs of terms which appear in the three-index antisymmetrization.

What we need to do is to take the derivative of the Riemann tensor in the LIF:
%
\begin{align}
    R_{\mu \nu \rho \sigma , \alpha } =
    g_{\mu [\sigma |, \nu |\rho ] \alpha } - g_{\nu  [\sigma |, \mu | \rho ] \alpha }
\,,
\end{align}
%
and permute the three indices \(\rho \sigma  \alpha \) cyclically. Writing all the terms out we get (up to a factor \(2\), which is irrelevant since we will find that all the terms cancel and everything is equal to 0):
%
\begin{subequations}
\begin{align}
    \begin{split}
        +&g_{\mu \sigma , \nu \rho \alpha } - g_{\nu  \sigma , \mu  \rho  \alpha }
        - g_{\mu \rho  , \nu \sigma \alpha } + g_{\nu  \rho  , \mu  \sigma  \alpha } + \\
        +&g_{\mu \alpha , \nu \sigma  \rho  } - g_{\nu  \alpha  , \mu  \sigma \rho  }
        - g_{\mu \sigma  , \nu \alpha \rho  } + g_{\nu  \sigma   , \mu  \alpha \rho  } + \\
        + &g_{\mu \rho  , \nu \alpha \sigma  } - g_{\nu  \rho  , \mu  \alpha \sigma  }
        - g_{\mu \alpha   , \nu \rho \sigma  } + g_{\nu  \alpha   , \mu \rho \sigma  }
        \,,
    \end{split}
\end{align}
\end{subequations}
%
so we have \(6 \) terms with a + sign, and \(6\) with a \(-\) sign: they cancel pairwise, since the partial  derivatives commute.

\subsubsection{Contracting the identities}

We start by contracting \(2 R_{\mu \nu [ \rho \sigma ; \alpha ]}\) with \(g^{\mu \rho }\): we get
%
\begin{align}
    0=
  g^{\mu \rho } \qty(R_{\mu \nu \rho \sigma ; \alpha } + R_{\mu \nu \alpha \rho ; \sigma } + R_{\mu \nu \sigma \alpha ; \rho }) = R_{\nu \sigma ; \alpha } - R_{\nu \alpha ; \sigma } + g^{\mu \rho } R_{\mu \nu \sigma \alpha ; \rho }
\,,
\end{align}
%
where, in the second term, we used the antisymmetry of the first two indices of the Riemann tensor in order to get the form which allowed us to use the definition of the Ricci tensor \(R_{\mu \nu } = g^{\rho \sigma } R_{\rho \mu \sigma \nu }\).
Also, we brought the metric inside the covariant derivatives since it is covariantly constant.
Then, we contract the expression we found with \(g^{\nu \sigma }\):
%
\begin{subequations}
\begin{align}
  0=
  g^{\nu \sigma } \qty(R_{\nu \sigma ; \alpha } - R_{\nu \alpha ; \sigma } + g^{\mu \rho } R_{\mu \nu \sigma \alpha ; \rho })
  &= R_{;\alpha  } - \tensor{R}{^{\sigma }_{\alpha; \sigma  }} - g^{\mu \rho} R_{\mu \alpha ; \rho }  \\
  &= R_{;\alpha  } - \tensor{R}{^{\sigma }_{\alpha; \sigma  }} - \tensor{R}{^{\sigma }_{\alpha ; \sigma }}
\,,
\end{align}
\end{subequations}
%
where we used the same properties as before and the definition of the scalar curvature \(R = g^{\mu \nu }R_{\mu \nu }\). So, we have the contracted Bianchi identities \(0 = R_{;\alpha } - 2 \tensor{R}{^{\sigma }_{\alpha ; \sigma }}\).
Raising an index with the inverse metric \(g^{\alpha \beta }\) and relabeling \(\sigma \) to \(\alpha \) in the second term (after having raised the index), these can be written as
%
\begin{align}
  \nabla_{\alpha } \qty(R g^{\alpha \beta } - 2R^{\alpha \beta })
\,.
\end{align}
%

\subsection{Weak-field geodesic equation}

This was already treated in section \ref{sec:weak-field-gravitational-potential}.

\subsection{Hyperbolic plane geodesics}

Our coordinates are \((x, y)\), and our metric is \(g_{ij } = y^{-2} \delta_{ij }\), with inverse \(g^{ij} = y^2 \delta^{ij}\).

So, we can calculate the Christoffel symbols as:
%
\begin{align}
  \Gamma^{i}_{jk} = \frac{1}{2} g^{im} \qty(g_{mj,k} + g_{m k, j } - g_{jk,m})
\,,
\end{align}
%
this calculation is simplified by the fact that the only nonvanishing derivatives of the metric are \(g_{00,1}=g_{11,1} = -2 y^{-3} \). If the index \(i\) in \(\Gamma^{i}_{jk}\) is zero, then the last term in the sum vanishes since it corresponds to a derivative with respect to \(x\).
With these we get:
%
\begin{subequations}
\begin{align}
  \Gamma^{0}_{00} &=  \frac{1}{2} y^2 \qty(2g_{00,0})= 0 \\
  \Gamma^{0}_{01} &=  \frac{1}{2} y^2 \qty(g_{00,1})= - \frac{1}{y}  \\
  \Gamma^{0}_{11} &=  \frac{1}{2} y^2 \qty(g_{01,1} + g_{01,1})= 0  \\
  \Gamma^{1}_{00} &=  \frac{1}{2} y^2 \qty(- g_{00,1})= \frac{1}{y}  \\
  \Gamma^{1}_{01} &=  \frac{1}{2} y^2 \qty(g_{10,0} + g_{11,0} - g_{01,1})= 0   \\
  \Gamma^{1}_{11} &=  \frac{1}{2} y^2 \qty(g_{11,1}+g_{11,1}-g_{11,1})= -\frac{1}{y}
\,,
\end{align}
\end{subequations}
%
and the geodesic equation \(u^{\mu } \nabla_{\mu } u^{\nu }= 0 \) is written with respect to these.

\subsubsection{Vertical lines}

First of all we want to parametrize these vertical lines: we choose our parameter so that the length of the velocity vector is everywhere equal to one.

Since the lines are vertical, we want the position \(x^{i}\) with respect to the parameter \(s\) to look something like \(x^{i}(s) = (x_0, y(s))\).

We use the arclength parameter:
%
\begin{align}
  s = \int \sqrt{g_{ij} u^{i} u^{j}} \dd{\lambda }
\,,
\end{align}
%
where \(u^{i} = \dv*{x^{i}}{\lambda }\) and \(\lambda \) is an arbitrary parameter.

We can rewrite this integral with respect to the Euclidean norm \(\norm{u}_E^2 = \delta_{ij} u^{i} u^{j}\): we get
%
\begin{align}
  s = \int \frac{1}{y} \norm{u}_E \dd{\lambda }
\,,
\end{align}
%
so we can see that we get \(s = \int \dd{\lambda }\), or \(s = \lambda \), iff \(\norm{u}_E = y\): so, let us drop the distinction between \(s\) and \(\lambda \) and apply this condition.
The velocity vector is
%
\begin{align}
  u^{i} = \dv{x^{i}}{s} = \qty(0, \dv{y}{s})
\,,
\end{align}
%
whose Euclidean norm is just (the absolute value of) \(\dv*{y}{s}\).
So, we must impose the condition \(y = \dv*{y}{s}\), which can be solved by separation of variables to yield \(s = \log y\), or \(y = e^{s}\).

So our parametrization for the curve is \(s \rightarrow x^{i} = (x_0, e^{s})\), the velocity is \(u^{i} = (0, e^{s})\) and the derivative of velocity with respect to \(s\) is again
%
\begin{align}
  \dv[]{u^{i}}{s} = (0, e^{s})
\,.
\end{align}

Now we can plug these into our geodesic equation, which is simplified by the fact that \(u^{0} = 0\), therefore there is only one relevant term in the Christoffel sum:
%
\begin{align}
  \dv[]{ u^{i}}{s} + \Gamma^{i}_{11} u^{1} u^{1} = 0
\,,
\end{align}
%
whose components are
%
\begin{align}
  \underbrace{\dv{ u^{0}}{s}}_{0} + \underbrace{\Gamma^{0}_{11}}_{0} y^2 = 0
\,
\end{align}
%
and
%
\begin{subequations}
  \begin{align}
    \dv{ u^{1}}{s} + \Gamma^{1}_{11} u^{1} u^{1} &= 0  \\
    y + \qty(- \frac{1}{y}) y^2 &=0
    \,,
  \end{align}
\end{subequations}
%
which are identities, therefore vertical lines are indeed geodesics in this hyperbolic plane.

\subsubsection{More solutions}

We have the Killing vector field \(\xi = (1,0)\) corresponding to the symmetry with respect to translations along the \(x\) axis: then, the quantity \(\vec{\xi} \cdot \vec{u}\) is conserved: so \(\xi^{\mu } u^{\nu } g_{\mu \nu }=\dot{x} g_{00} = \dot{x} y^{-2}\) is constant along the trajectory (here we denote derivatives with respect to the parameter \(s\) by a dot).

We also know that, if we parametrize with arclength, \(\norm{u}_{E} / y \equiv 1 \), which means
%
\begin{align}
  y = \norm{u}_{E} = \sqrt{\dot{x}^2 + \dot{y}^2} = \dot{x} \sqrt{\qty(y^{\prime })^2 + 1}
\,,
\end{align}
%
where \(y^{\prime } = \dv*{y}{x} = \dot{y} / \dot{x}\).

Then, the conserved quantity can be rewritten as
%
\begin{align}
  \dot{x} y^{-2} = y^{-2} \frac{y}{\sqrt{y^{\prime 2} +1}}
  = \frac{1}{y \sqrt{y^{\prime 2} + 1}}
\,,
\end{align}
%
which the exercise sheet calls \(A\) but I will call it \(1/R\): so \(R = y \sqrt{y^{\prime 2}+1}\). We can square this to write the conservation equation
%
\begin{align}
  y^2 \qty(y^{\prime 2} + 1 ) = R^2 = \const
\,.
\end{align}
%

Alternatively, once one has found the equations of motion
%
\begin{subequations}
\begin{align}
  y \ddot{y} &= \dot{y}^2 - \dot{x}^2  \\
  y \ddot{x} &= 2 \dot{x} \dot{y}
\,,
\end{align}
\end{subequations}

to prove the constancy of \(\dot{x}
 y^{-2}\) one can observe that
%
\begin{align}
  y^2 \dv{}{s} \qty(\frac{\dot{x}}{y^2})
  = \ddot{x} - 2 \frac{\dot{x} \dot{y}}{y}
\,,
\end{align}
%

so \(\dot{x} y^{-2}\) being a constant is equivalent to the second equation of motion holding.

% \subsubsection{Old, ugly solution}

% Before finding the aforementioned derivation of the first integral, here is what I wrote.

% I find the solution which follows quite ugly and unjustified, I have yet to find a geometric justification for these manipulations, which were derived by reverse-engineering the first integral.
% Anyhow, these manipulations work\dots

% We insert the Christoffel symbols into the geodesic equations and multiplying through by \(y\) we get the following system, in which we denote differentiation with respect to \(s\) by a dot:
% %
% \begin{subequations}
% \begin{align}
%   y \ddot{y} &= \dot{y}^2 - \dot{x}^2  \\
%   y \ddot{x} &= 2 \dot{x} \dot{y}
% \,.
% \end{align}
% \end{subequations}

% Now, we will denote the derivative of \(y \) with respect to \(x\) as
% %
% \begin{align}
%   y' = \dv[]{y}{x} = \frac{\dot{y}}{\dot{x}}
% \,.
% \end{align}

% We want to find a first integral. We start with the definition of \(y'\):
% %
% \begin{align}
%   \dot{y} - y^{\prime } \dot{x} = 0
% \,,
% \end{align}
% %
% and add and subtract the quantity \(\dot{y} y^{\prime 2}\):
% %
% \begin{align}
%   \dot{y} + (\dot{y} y^{\prime } - \dot{x}) y^{\prime } - \dot{y} y^{\prime 2} =0
% \,,
% \end{align}
% %
% which can also be written as
% %
% \begin{align} \label{eq:hyperbolic-plane-geodesics-integral-step1}
%   \dot{y} + (\dot{y} y^{\prime } - \dot{x}) y^{\prime } + \dot{y} y^{\prime 2} - 2 \dot{y} y^{\prime 2} = 0
% \,.
% \end{align}

% Now, we want to substitute in our equations of motion: we will need them in the form \(y \ddot{y} / \dot{x} = \dot{y} y^{\prime } - \dot{x}\) and \(y \ddot{x} / \dot{x} = 2 \dot{y}\).
% We recognize the LHS of both of these in \eqref{eq:hyperbolic-plane-geodesics-integral-step1} and plug them in: it becomes
% %
% \begin{align} \label{eq:hyperbolic-plane-geodesics-integral-step2}
%   \dot{y} + \frac{y \ddot{y}}{\dot{x}} y^{\prime } + \dot{y} y^{\prime 2} - \frac{y \ddot{x}}{\dot{x}} y^{\prime 2} = 0
% \,.
% \end{align}

% We can recognize that the second derivative terms look similar to the derivative of \(y'\), which is:
% %
% \begin{align}
%   \dv[]{}{s} y^{\prime }
%   = \frac{\ddot{y}}{\dot{x}} - \frac{\dot{y}}{\dot{x}
%   } \ddot{x}
%   = \frac{\ddot{y}}{\dot{x}} - y^{\prime } \ddot{x}
% \,,
% \end{align}
% %
% so equation \eqref{eq:hyperbolic-plane-geodesics-integral-step2} becomes:
% %
% \begin{align}
%   \dot{y} + \dot{y} y^{\prime 2} + y y^{\prime } \dv{y'}{s}=0
% \,,
% \end{align}
% %
% which is \(1/(2y)\) times the derivative of \(y^2 (y^{\prime 2} + 1)\), which comes out to be:
% %
% \begin{align}
%   \frac{1}{2y}
%   \dv{}{s} \qty(y^2 (y^{\prime 2} + 1))
%   = \frac{1}{2y}\qty(2 y \dot{y}
%   (y^{\prime 2} + 1)
%   + y^2 2 y^{\prime } \dv{}{s} y^{\prime })
% \,,
% \end{align}
% %
% exactly what we had before.

% So, \(y^2 (y^{\prime 2} + 1) = \const\), and we can call this constant \(R^2\) (which is \(1/A^2\) in the homework notation: I find this notation to be more suggestive).

\subsubsection{Solving the equation}

The equation \(y^2 (y^{\prime 2}+1) = R^2\) can be rewritten as
%
\begin{align}
  \dv{y}{x} = \pm \sqrt{\frac{R^2}{y^2} - 1}
\,,
\end{align}
%
which means that if we fix \(R\) the value of the derivative of the curve can only attain two opposite values. Do note that we can go from one branch to the other with the transformation \(x \rightarrow -x\), a mirror symmetry around some center.
Then, we can just make the gauge choice \(y^{\prime }>0\) and integrate by separation of varibles:
%
\begin{align}
  \int \frac{y \dd{y}}{\sqrt{R^2-y^2}} = \int \dd{x}
\,,
\end{align}
%
which can be solved with the substitution \( y = R \sin(\theta )\), with \(\dd{y} = R \cos(\theta ) \dd{\theta }\). Inserting this, we find:
%
\begin{align}
  x - x_0  = \int \frac{R \sin\theta R \cos\theta  \dd{\theta }}{R \sqrt{1 - \sin^2 \theta }}
  = R \int \sin \theta  \dd{\theta } = - R \cos \theta
\,,
\end{align}
%
which can be squared to find \((x- x_0 )^2 = R^2 (1 - \sin^2 \theta ) = R^2 - y^2\), or
%
\begin{align}
  R^2 = (x-x_0  )^2 + y^2
\,,
\end{align}
%
the equation of a circle.
We can then confirm that this solution also holds in the other branch, up to a change of integration constant, by rewriting \((x- x_0 )^2 = ((-x) - x_1 )^2\): this is solved by \(x_0 = - x_1 \).

Geometrically, we are looking at circles with origins on the \(x\) axis and radius \(R\). If \(y\) and \(R\) are fixed, then there are only two possible circles, which can be found by connecting a certain point at height \(y\) to the \(x\) axis with a segment of length \(R\).
One can then see that the right halves of the circles we found can be found from the left halves by symmetry.

\end{document}


\chapter{Affine schemes: the sheaf}
\label{ch:spec_sheaf}

We now complete our definition of $X = \Spec A$ by
defining the sheaf $\OO_X$ on it, making it into a ringed space.
This is done quickly in the first section.

However, we will then spend the next several chapters
trying to convince the reader to \emph{forget}
the definition we gave, in practice.
This is because practically,
the sections of the sheaves are best computed by not using
the definition directly, but by using some other results.

Along the way we'll develop some related theory:
in computing the stalks we'll find out the definition of a local ring,
and in computing the sections we'll find out about distinguished open sets.

A reminder once again: \Cref{ch:spec_examples} has
many more concrete examples.
It's not a bad idea to look through there for more examples
if anything in this chapter trips you up.

\section{A useless definition of the structure sheaf}
\prototype{Still $\CC[x_1, \dots, x_n] / I$.}

We have now endowed $\Spec A$ with the Zariski topology,
and so all that remains is to put a sheaf $\OO_{\Spec A}$ on it.
To do this we want a notion of ``regular functions'' as before.

This is easy to do since we have localizations on hand.
\begin{definition}
	First, let $\SF$ be the pre-sheaf of ``globally rational'' functions:
	i.e.\ we define $\SF(U)$ to be the localization
	\[
		\SF(U) = \left\{
			\frac fg \mid f, g \in A
			\text{ and } g(\kp) \neq 0 \; \forall \kp \in U
		\right\}
		= \left(A \setminus \bigcup_{\kp \in U} \kp \right)\inv A.
	\]
	We now define the structure sheaf on $\Spec A$.
	It is
	\[ \OO_{\Spec A} = \SF\sh \]
	i.e.\ the sheafification of the $\SF$ we just defined.
\end{definition}
\begin{exercise}
	Compare this with the definition for $\OO_V$
	with $V$ a complex variety, and check that they essentially match.
\end{exercise}
And thus, we have completed the transition to adulthood,
with a complete definition of the affine scheme.

If you really like compatible germs,
you can write out the definition:
\begin{definition}
	Let $A$ be a ring.
	Then $\Spec A$ is made into a ringed space by setting
	\[ \OO_{\Spec A}(U)
		= \left\{ (f_\kp \in A_\kp)_{\kp \in U}
		\text{ which are locally quotients} \right\}. \]
	That is, it consists of sequence $(f_\kp)_{\kp \in U}$, with
	each $f_\kp \in A_\kp$, such that for every point $\kp$ there
	is an open neighborhood $U_\kp$ and an $f,g \in A$ such that
	$f_\kq = \frac fg \in A_\kq$ for all $\kq \in U_\kp$.
\end{definition}

We will now \textbf{basically forget about this definition},
because we will never use it in practice.
In the next two sections, we will show you:
\begin{itemize}
	\ii that the stalks $\OO_{\Spec A, \kp}$ are just $A_\kp$, and
	\ii that the sections $\OO_{\Spec A}(U)$
	can be computed, for any open set $U$,
	by focusing only on the special case where $U = D(f)$
	is a distinguished open set.
\end{itemize}
These two results will be good enough for all of our purposes,
so we will be able to not use this definition.
(Hence the lack of examples in this section.)

\section{The value of distinguished open sets (or: how to actually compute sections)}
\prototype{$D(x)$ in $\Spec \CC[x]$ is the punctured line.}

We will now really hammer in the importance of
the distinguished open sets.
The definition is analogous to before:
\begin{definition}
	Let $f \in \Spec A$.
	Then $D(f)$ is the set of $\kp$ such that $f(\kp) \neq 0$,
	a \vocab{distinguished open set}.
\end{definition}
Distinguished open sets will have three absolutely crucial properties,
which build on each other.

\subsection{A basis of the Zariski topology}
The first is a topological observation:
\begin{theorem}
	[Distinguished open sets form a base]
	\label{thm:distinguished_base}
	The distinguished open sets $D(f)$
	form a basis for the Zariski topology:
	any open set $U$ is a union of distinguished open sets.
\end{theorem}
\begin{proof}
	Let $U$ be an open set;
	suppose it is the complement of closed set $V(I)$.
	Then verify that \[ U = \bigcup_{f \in I} D(f). \qedhere \]
\end{proof}

\subsection{Sections are computable}
The second critical fact is that the sections
on distinguished open sets can be computed explicitly.
\begin{theorem}
	[Sections of $D(f)$ are localizations away from $f$]
	Let $A$ be a ring and $f \in A$.
	Then \[ \OO_{\Spec A}(D(f)) \cong A[1/f]. \]
\end{theorem}
\begin{proof}
	Omitted, but similar to
	\Cref{thm:reg_func_distinguish_open}.
\end{proof}

\begin{example}
	[The punctured line is isomorphic to a hyperbola]
	The ``hyperbola effect'' appears again:
	\[ \OO_{\Spec \CC[x]} (D(x))
		= \CC[x, x\inv]
		\cong \CC[x,y] / (xy-1). \]
\end{example}

On a tangential note,
we had better also note somewhere that $\Spec A = D(1)$
is itself distinguished open, so the global sections can be recovered.
\begin{corollary}
	[$A$ is the ring of global sections]
	The ring of global sections of $\Spec A$ is $A$.
\end{corollary}
\begin{proof}
	By previous theorem, $\OO_{\Spec A}(\Spec A)
	= \OO_{\Spec A}(D(1)) = A[1/1] = A$.
\end{proof}

\subsection{They are affine}
\label{subsec:distinguished_open_affine}
We know $\OO_X(D(f)) = A[1/f]$.
In fact, if you draw $\Spec A[1/f]$,
you will find that it looks exactly like $D(f)$.
So the third final important fact is that
$D(f)$ will actually be \emph{isomorphic} to $\Spec A[1/f]$
(just like the line minus the origin is isomorphic to the hyperbola).
We can't make this precise yet,
because we have not yet discussed morphisms of schemes,
but it will be handy later (though not right away).
%However, you can already see this at the level of topological spaces;
%see \Cref{prob:homeomorphism}.
%Since distinguished open sets form a base,
%though, this means that open sets of affine schemes
%are, at least locally, themselves affine schemes:
%given any open set $U \subseteq \Spec A$, and point $\kp \in U$,
%there is some open neighborhood $V \ni p$ contained in $U$
%which is itself affine.

\subsection{Classic example: the punctured plane}
\label{subsec:punctured_plane}
We now give the classical example of a computation which shows
how you can forget about sheafification,
if you never liked it.\footnote{This perspective is
	so useful that some sources, like Vakil \cite[\S4.1]{ref:vakil}
	will \emph{define} $\OO_{\Spec A}$
	by requiring $\OO_{\Spec A}(D(f)) = A[1/f]$,
	rather than use sheafification as we did.}
The idea is that:
\begin{moral}
	We can compute any section $\OO_X(U)$ in practice
	by using distinguished open sets and sheaf axioms.
\end{moral}

Let $X = \Spec \CC[x,y]$,
and consider the origin, i.e.\ the point $\km = (x,y)$.
This ideal is maximal, so it corresponds to a closed point,
and we can consider the open set $U$
consisting of all the points other than $\km$.
We wish to compute $\OO_X(U)$.

\begin{center}
\begin{asy}
	graph.xaxis("$\mathcal{V}(y)$", red);
	graph.yaxis("$\mathcal{V}(x)$", red);
	fill(box( (-3,-3), (3,3) ), opacity(0.2)+lightcyan);
	opendot(origin, blue+1.5);
	label("$\mathfrak m = (x,y)$", origin, dir(45), blue);
\end{asy}
\end{center}

Unfortunately, $U$ is not distinguished open.
But, we can compute it anyways by writing $U = D(x) \cup D(y)$:
conveniently, $D(x) \cap D(y) = D(xy)$.
By the sheaf axioms,
we have a pullback square
\begin{center}
\begin{tikzcd}
	\OO_X(U) \ar[r] \ar[d] & \OO_X(D(x)) = \CC[x,y,x\inv] \ar[d] \\
	\OO_X(D(x)) = \CC[x,y,y\inv]_{y} \ar[r] & \OO_X(D(xy)) = \CC[x,y,x\inv, y\inv].
\end{tikzcd}
\end{center}
In other words, $\OO_X(U)$ consists of pairs
\begin{align*}
	f &\in \CC[x,y,x\inv] \\
	g &\in \CC[x,y,y\inv]
\end{align*}
which agree on the overlap:
$f = g$ on $D(x) \cap D(y)$.
Well, we can describe
$f$ as a polynomial with some $x$'s in the denominator, and
$g$ as a polynomial with some $y$'s in the denominator.
If they match, the denominator is actually constant.
Put crudely,
\[ \CC[x,y,x\inv] \cap \CC[x,y,y\inv] = \CC[x,y]. \]
In conclusion,
\[ \OO_X(U) = \CC[x,y]. \]
That is, we get no additional functions.

\section{The stalks of the structure sheaf}
\prototype{The stalk of $\Spec \CC[x,y]$ at $\km = (x,y)$
are rational functions defined at the origin.}

Don't worry, this one is easier than last section.

\subsection{They are localizations}
\begin{theorem}
	[Stalks of $\Spec A$ are $A_\kp$]
	Let $A$ be a ring and let $\kp \in \Spec A$.
	Then \[ \OO_{\Spec A, \kp} \cong A_\kp. \]
	In particular $X$ is a locally ringed space.
\end{theorem}
\begin{proof}
	Since sheafification preserved stalks,
	it's enough to check it for $\SF$ the pre-sheaf
	of globally rational functions in our definition.
	The proof is basically the same as \Cref{thm:stalks_affine_var}:
	there is an obvious map $\SF_\kp \to A_\kp$ on germs by
	\[ \left(U, f/g \in \SF(U) \right)
		\mapsto f/g \in A_\kp . \]
	(Note the $f/g$ on the left lives in $\SF(U)$
	but the one on the right lives in $A_\kp$).
	We show injectivity and surjectivity:
	\begin{itemize}
		\ii Injective: suppose $(U_1, f_1 / g_1)$ and $(U_2, f_2 / g_2)$
		are two germs with $f_1/g_1 = f_2/g_2 \in A_\kp$.
		This means $h(g_1 f_2 - f_2 g_1) = 0$ in $A$, for some nonzero $h$.
		Then both germs identify with
		the germ $(U_1 \cap U_2 \cap D(h), f_1 / g_1)$.
		\ii Surjective: let $U = D(g)$. \qedhere
	\end{itemize}
\end{proof}

\begin{example}
	[Denominators not divisible by $x$]
	We have seen this example so many times
	that I will only write it in the new notation,
	and make no further comment:
	if $X = \Spec \CC[x]$ then
	\[ \OO_{\Spec X, (x)} = \CC[x]_{(x)}
		= \left\{  \frac fg \mid g(0) \ne 0 \right\}. \]
\end{example}
\begin{example}
	[Denominators not divisible by $x$]
	Let $X = \Spec \CC[x,y]$ and let $\km = (x,y)$ be the origin.
	Then
	\[ \CC[x,y]_{(x,y)}
		= \left\{ \frac{f(x,y)}{g(x,y)} \mid g(0,0) \ne 0 \right\}. \]
\end{example}

If you want more examples,
take any of the ones from \Cref{sec:localize_prime_ideal},
and try to think about what they mean geometrically.

\subsection{Motivating local rings: germs should package values}
Let's return to our well-worn example $X = \Spec \CC[x,y]$
and consider $\km = (x,y)$ the origin.
The stalk was
\[ \OO_{X, \km} = \CC[x,y]_{(x,y)}
	= \left\{ \frac{f(x,y)}{g(x,y)} \mid g(0,0) \ne 0 \right\}. \]
So let's take some section like $f = \frac{1}{xy + 4}$,
which is a section of $U = D(xy+4)$ (or some smaller open set,
but we'll just use this one for simplicity).
We also have $U \ni m$, and so $f$ gives a germ at $\km$.

On the other hand, $f$ also has a value at $\km$:
it is $f \pmod{\km} = \frac 14$.
And in general, the ring of possible values of a section
at the origin $\km$ is $\CC[x,y] / \km \cong \CC$.

Now, you might recall that I pressed the point of view
that a germ might be thought of as an ``enriched value''.
Then it makes sense that if you know the germ of a section $f$ at
a point $\km$ --- i.e., you know the ``enriched value'' ---
then you should be able to value as well.
What this means is that we ought to have some map
\[ A_\km \to A/\km \]
sending germs to their associated values.

Indeed you can, and this leads us to\dots

\section{Local rings and residue fields:
linking germs to values}
\prototype{The residue field of $\Spec \CC[x,y]$ at $\km = (x,y)$
is $\CC$.}

\subsection{Localizations give local rings}
This notation is about to get really terrible, but bear with me.
\begin{theorem}
	[Stalks are local rings]
	\label{thm:stalks_local_ring}
	Let $A$ be a ring and $\kp$ any prime ideal.
	Then the localization $A_\kp$ has exactly one maximal ideal,
	given explicitly by
	\[ \kp A_\kp =
		\left\{ \frac fg \mid f \in \kp \; g \notin \kp \right\}. \]
\end{theorem}
The ideal $\kp A_\kp$ thus captures the idea
of ``germs vanishing at $\kp$''.\footnote{The notation $\kp A_\kp$ really means
the set of $f \cdot h$ where $f \in \kp$
(viewed as a subset of $A_\kp$ by $f \mapsto \frac f1$) and $h \in A_{\kp}$.
I personally find this is more confusing than helpful,
so I'm footnoting it.}

Proof in a moment;
for now let's introduce some words so we can
give our examples in the proper language.
\begin{definition}
	A ring $R$ with exactly one maximal ideal $\km$
	will be called a \vocab{local ring}.
	The \vocab{residue field} is the quotient $A / \km$.
\end{definition}
\begin{ques}
	Are fields local rings?
\end{ques}

Thus what we find is that:
\begin{moral}
	The stalks consist of the possible enriched values (germs);
	the residue field is the set of (un-enriched) values.
\end{moral}

\begin{example}
	[The stalk at the origin of {$\Spec \CC[x,y]$}]
	Again set $A = \CC[x,y]$, $X = \Spec A$ and $\kp = (x,y)$
	so that $\OO_{X,\kp} = A_\kp$.
	(I switched to $\kp$ for the origin,
	to avoid confusion with the maximal ideal $\kp A_{\kp}$
	of the local ring $A_\kp$.)
	As we said many times already,
	$A_{\kp}$ consists of rational functions not vanishing at the origin,
	such as $f = \frac{1}{xy+4}$.

	What is the unique maximal ideal $\kp A_\kp$?
	Answer: it consists of the rational functions
	which \emph{vanish} at the origin:
	for example, $\frac{x}{x^2+3y}$, or $\frac{3x+5y}{2}$,
	or $\frac{-xy}{4(xy+4)}$.
	If we allow ourselves to mod out by such functions,
	we get the residue field $\CC$,
	and $f$ will have the value $\frac14$, since
	\[ \frac{1}{xy+4} -
		{\underbrace{\frac{-xy}{4(xy+4)}}_{\text{vanishes at origin}}}
		= \frac14. \]

	More generally, suppose $f$ is any section of some open
	set containing $\kp$.
	Let $c \in \CC$ be the value $f(\kp)$, that is, $f \pmod \kp$.
	Then $f - c$ is going to be another section
	which vanishes at the origin $\kp$,
	so as promised, $f \equiv c \pmod{\kp A_{\kp}}$.
\end{example}

Okay, we can write down a proof of the theorem now.
\begin{proof}
	[Proof of \Cref{thm:stalks_local_ring}]
	One may check set $I = \kp A_\kp$ is an ideal of $A_\kp$.
	Moreover, $1 \notin I$, so $I$ is proper.

	To prove it is maximal and unique,
	it suffices to prove that any $f \in A_\kp$ with $f \notin I$
	is a \emph{unit} of $A_\kp$.
	This will imply $I$ is maximal: there are no more non-units to add.
	It will also imply $I$ is the only maximal ideal:
	because any proper ideal can't contain units, so is contained in $I$.

	This is actually easy.
	An element of $A_\kp$ not in $I$ must be $x = \frac fg$
	for $f,g \in A$ and $f,g \notin \kp$.
	For such an element, $x\inv = \frac gf \notin \kp$ too.
	So $x$ is a unit. End proof.
\end{proof}

Even more generally:
\begin{moral}
	If a sheaf $\SF$ consists of ``field-valued functions'',
	the stalk $\SF_p$ probably has a maximal ideal
	consisting of the germs vanishing at $p$.
\end{moral}

\begin{example}[Local rings in non-algebaric geometry sheaves]
Let's go back to the example of $X = \RR$ and $\SF(U)$ the smooth functions,
and consider the stalk $\SF_{p}$, where $p \in X$.
Define the ideal $\km_p$ to be the set of germs $(s,U)$ for which $s(p) = 0$.

Then $\km_p$ is maximal: we have an exact sequence
\[ 0 \to \km_p \to \SF_p \taking{(s,U) \mapsto s(p)} \RR \to 0 \]
and so $\SF_p / \km_p \cong \RR$, which is a field.

It remains to check there are no nonzero maximal ideals.
Now note that if $s \notin \km_p$,
then $s$ is nonzero in some open neighborhood of $p$,
and one can construct the function $1/s$ on it.
So \textbf{every element of $\SF_p \setminus \km_p$ is a unit};
and again $\km_p$ is in fact the only maximal ideal!

Thus the stalks of each of the following types of sheaves
are local rings, too.
\begin{itemize}
	\ii Sheaves of continuous real/complex functions on a topological space
	\ii Sheaves of smooth functions on any manifold
	\ii etc.
\end{itemize}
\end{example}

\subsection{Computing values: a convenient square}
Very careful readers might have noticed something
a little uncomfortable in our extended example with $\Spec A$
with $A = \CC[x,y]$ and $\kp = (x,y)$ the origin.
Let's consider $f = \frac{1}{xy+4}$.
We took $f \pmod{x,y}$ in the original ring $A$ in order
to decide the value ``should'' be $\frac14$.
However, all our calculations actually
took place not in the ring $A$, but instead in the ring $A_\kp$.
Does this cause issues?

Thankfully, no, nothing goes wrong, even in a general ring $A$.

\begin{definition}
	We let the quotient $A_\kp / \kp A_\kp$,
	i.e.\ the \vocab{residue field} of the stalk of $\Spec A$ at $\kp$,
	be denoted by $\kappa(\kp)$.
\end{definition}

The following result from commutative algebra
shows that the order doesn't matter.
\begin{theorem}
	[The germ-to-value square]
	Let $A$ be a ring and $\kp$ a prime ideal.
	The following diagram commutes:
	\begin{center}
	\begin{tikzcd}
		A \ar[r, "\text{localize}"] \ar[d, "\bmod \kp"']
			& A_\kp \ar[d, "\bmod \kp"] \\
		A/\kp \ar[r, "\Frac(-)"] & \kappa (\kp)
	\end{tikzcd}
	\end{center}
	In particular, $\kappa(\kp)$
	can also be described as $\Frac(A/\kp)$.
\end{theorem}
So for example, if $A = \CC[x,y]$ and $\kp = (x,y)$,
then $A/\kp = \CC$ and $\Frac(A_\kp) = \Frac(\CC) = \CC$, as we expected.
In practice, $\Frac(A/\kp)$ is probably the easier way
to compute $\kappa(\kp)$ for any prime ideal $\kp$.

\section{Recap}
To recap the last two chapters, let $A$ be a ring.
\begin{itemize}
	\ii We define $X = \Spec A$ to be the set of prime ideals of $A$.
	\begin{itemize}
		\ii The maximal ideals are the ``closed points'' we are used to,
		but the prime ideals are ``generic points''.
	\end{itemize}

	\ii We equip $\Spec A$ with the Zariski topology by declaring
	$\VV(I)$ to be the closed sets, for ideals $I \subseteq A$.
	\begin{itemize}
		\ii The distinguished open sets $D(f)$,
		form a topological basis.
		\ii The irreducible closed sets are exactly the closures of points.
	\end{itemize}

	\ii Finally, we defined a sheaf $\OO_X$.
	We set up the definition such that
	\begin{itemize}
		\ii $\OO_{X}(D(f)) = A[1/f]$:
		at distinguished open sets $D(f)$,
		we get localizations too.
		\ii $\OO_{X,\kp} = A_\kp$:
		the stalks are localizations at a prime.
	\end{itemize}
	Since $D(f)$ is a basis,
	these two properties lets us explicitly compute $\OO_X(U)$
	for any open set $U$,
	so we don't have to resort to the definition using sheafification.
\end{itemize}

\section{Functions are determined by germs, not values}
\prototype{The functions $0$ and $x$ on $\Spec \CC[x]/(x^2)$.}

We close the chapter with a word of warning.
In any ringed space, a section is determined by its germs;
so that on $\Spec A$ a function $f \in A$ is determined
by its germ in each stalk $A_\kp$.
However, we now will mention that an $f \in A$ is \emph{not}
determined by its value $f(\kp) = f \pmod \kp$ at each point.

The famous example is:
\begin{example}
	[On the double point, all multiples of $x$ are zero at all points]
	The space $\Spec \CC[x] / (x^2)$ has only one point, $(x)$.
	The functions $0$ and $x$ (and for that matter $2x$, $3x$, \dots)
	all vanish on it.
	This shows that functions are not determined uniquely
	by values in general.
\end{example}

Fortunately, we can explicitly characterize
when this sort of ``bad'' behavior happens.
Indeed, we want to see when $f(\kp) = g(\kp)$ for every $\kp$,
or equivalently, $h = f-g$ vanishes on every prime ideal $\kp$.
This is equivalent to having
\[ h \in \bigcap_{\kp} \kp = \sqrt{(0)} \]
the radical of the \emph{zero} ideal.
Thus in the prototype, the failure was caused by the fact that $x^n = 0$
for some large $n$.

\begin{definition}
	For a ring $A$, the radical of the zero ideal, $\sqrt{(0)}$,
	is called the \vocab{nilradical} of $A$.
	Elements of the nilradical are called \vocab{nilpotents}.
	We say $A$ is \vocab{reduced} if $0$ is the only nilpotent,
	i.e.\ $\sqrt{(0)} = (0)$.
\end{definition}
\begin{ques}
	Are integral domains reduced?
\end{ques}

Then our above discussion gives:
\begin{theorem}
	[Nilpotents are the only issue]
	Two functions $f$ and $g$ have the same value
	on all points of $\Spec A$ if and only if $f-g$ is nilpotent.
\end{theorem}
In particular, when $A$ is a reduced ring,
even the values $f(\kp)$ as $\kp \in \Spec A$
are enough to determine $f \in A$.

\section{\problemhead}
As \Cref{ch:spec_examples} contains many
examples of affine schemes to train your intuition;
it's likely to be worth reading even before attempting these problems.

\begin{dproblem}
	[Spectrums are quasicompact]
	\gim
	Show that $\Spec A$ is quasicompact for any ring $A$.
\end{dproblem}

\begin{problem}
	[Punctured gyrotop, communicated by Aaron Pixton]
	The gyrotop is the scheme $X = \Spec \CC[x,y,z] / (xy,z)$.
	We let $U$ denote the open subset obtained
	by deleting the closed point $\km = (x,y,z)$.
	Compute $\OO_X(U)$.
	\begin{hint}
		$k[x,y] \times k[z,z\inv]$.
	\end{hint}
\end{problem}

\begin{problem}
	Show that a ring $R$ is a local ring
	if and only of the following property is true:
	for any $x \in R$,
	either $x$ or $1-x$ is a unit.
\end{problem}

\begin{problem}
	Let $R$ be a local ring, and $\km$ be its maximal ideal.
	Describe $R_\km$.
	\begin{hint}
		It's isomorphic to $R$!
	\end{hint}
\end{problem}

\begin{problem}
	Let $A$ be a ring, and $\km$ a maximal ideal.
	Consider $\km$ as point of $\Spec A$
	Show that $\kappa(\km) \cong A/\km$.
\end{problem}