\subsection{Solo Diagrams}\label{ssec:diagram-definitions}

    The Solo Calculus was further developed by~\cite{solo-diagrams} to provide a one-to-one correspondence between these  expressions and `diagram-like' objects.
    This provides a strong analog to real-world systems and an applicability to be used as a modelling tool for groups of communicating systems.
    Furthermore, as discussed by~\cite{learning-styles}, a visual output of information is often found to be preferable for cognition than verbal or textual information.\\

    \begin{definition}{Edge\\}
        An edge is defined to be:
        \begin{align*}
            E \defeq \, \langl a, a_1 \ldots a_k\rangl_t \quad \text{for } t \in \{ i, o \}
        \end{align*}
        where $a, a_i$ are \textit{nodes}, $\langl \ldots\rangl_i$ is an \textit{input edge}, $\langl\ldots\rangl_o$ is an \textit{output edge} and $k$ the edge's \textit{arity}.
    \end{definition}

    \begin{figure}[H]
        \centering
        \begin{subfigure}{0.4\linewidth}
            \centering
            \begin{tikzpicture}[transform shape, every node/.style={circle, fill=black!100, inner sep=0.05cm}]
                \node[anchor=center, label=below:{$a$}](a){};
                \coordinate[right=1cm of a](ax);
                \node[above right=1cm of ax, label=below:{$a_1$}](a1){};
                \node[below right=1cm of ax, label=below:{$a_2$}](a2){};
                \draw[-{>[scale=2]}] (ax) -- (a);
                \draw[-] (a1) -- (ax) -- (a2);
            \end{tikzpicture}
            \caption*{Output edge $\langl a, a_1, a_2\rangl_o$}
        \end{subfigure}
        \begin{subfigure}{0.4\linewidth}
            \centering
            \begin{tikzpicture}[transform shape, every node/.style={circle, fill=black!100, inner sep=0.05cm}]
                \node[anchor=center, label=below:{a}](a){};
                \coordinate[right=1cm of a](ax);
                \node[above right=1cm of ax, label=below:{$a_1$}](a1){};
                \node[below right=1cm of ax, label=below:{$a_2$}](a2){};
                \draw[-{<[scale=2]}] (ax) -- (a);
                \draw[-] (a1) -- (ax) -- (a2);
            \end{tikzpicture}
            \caption*{Input edge $\langl a, a_1, a_2\rangl_i$}
        \end{subfigure}
    \end{figure}

    This is analogous to an input or output solo $u \, \tilde{x}$ in the calculus, where $a$ is $u$ or $\bar{u}$ and $a_1 \ldots a_n$ is $\tilde{x}$ as written in Definition~\ref{solo-calculus-syntax}.
    The intuition here is to define such an object within the diagrams for each type of agent in the calculus.
    Note that inputs and outputs must have matching arity --- a 2-arity input cannot communicate with a 3-arity output for obvious reasons.

    \begin{definition}{Box\\}
        A box is defined to be:
        \begin{align*}
            B \defeq \, \langl G, S \rangl \quad \text{for } S \subset nodes(G)\footnotemark
        \end{align*}\footnotetext{This is written as shorthand for all nodes contained within a given object, in this case $\{a \text{ s.t. } a \in nodes(S),\, S \in G \}$}
        where G is a \textit{graph} (or multiset of \textit{edges}) and S is a set of \textit{nodes}, referred to as the \textit{internal nodes} of $B$.
        The \textit{principal nodes} of $B$ are then $nodes(G) \setminus S$.
    \end{definition}

    \begin{figure}[H]
        \centering
        \begin{subfigure}{0.4\linewidth}
            \centering
            \begin{tikzpicture}[transform shape, every node/.style={circle, fill=black!100, inner sep=0.05cm}]
                \coordinate[anchor=center](nw);
                \coordinate[below=3cm of nw](sw);
                \coordinate[right=3cm of nw](ne);
                \coordinate[below right=3cm and 3cm of nw](se);
                \draw[-] (nw) -- (ne) -- (se) -- (sw) -- (nw);
                \node[right=1.5cm of sw, label=below:{$x$}](x){};
                \node[below=1.5cm of ne, label=right:{$y$}](y){};
                \node[below right=1.5cm of nw, label=above left:{$w$}](w){};
                \coordinate[below right=1cm of w](wxyx){};
                \draw[-] (y) -- (wxyx) -- (w);
                \draw[-{<[scale=2]}] (wxyx) -- (x);
                \draw[-{>[scale=2]}] (y) to [out=140, in=10] (w);
            \end{tikzpicture}
            \caption*{Box representing $!(w)(x \, w y \; | \; \tilde{w} \, y)$}
        \end{subfigure}\footnotemark
    \end{figure}\footnotetext{Usually the $w$ in the diagram would be excluded, but is included here for illustration purposes only.}

    This can then be seen to be analogous to the replication operator, with the idea being that the principal nodes form the perimeter of a box and cannot be replicated --- they serve as the interface to the internals of the box.

    \begin{definition}{Diagram\\}
        A solo diagram is defined to be:
        \begin{align*}
            SD \defeq (G, M, \ell)
        \end{align*}
        where $G$ is a finite multiset of \textit{edges}, $M$ is a finite multiset of \textit{boxes} and $\ell$ a labelling of the $nodes(G)$ and of $principals(M)$.
    \end{definition}

    From here, we can convert Solo Calculus to Diagrams, where composition is intuitively just including two separate diagrams together and scope is simply any connected nodes labelled by $\ell$.
    There are then four required reduction cases (edge-edge, edge-box, box-box and box internals) which can be deduced from the definition of the calculus.

    \begin{definition}{Diagram Reduction\\}
        Let $G, G_1, G_2 \ldots$ be arbitrary graphs, $M, M'$ arbitrary box multisets, $\alpha \defeq \langl a, a_1 \ldots a_k\rangl_i$, $\beta \defeq \langl a, a_1' \ldots a_k'\rangl_o$, $\sigma \defeq a_i \mapsto a_i'$, $\rho$ a arbitrary but fresh relabelling and $G\sigma$ shorthand for $\sigma(G) \defeq G\{dom(\sigma) / ran(\sigma)\}$ the application of the renaming $\sigma$ on the edges of G.
        $\alpha$ and $\beta$ need not be fixed to input and output respectively, but must be opposite parity.
        Then, the following reductions may be made:
        \begin{align*}
            (G \cup \{\alpha, \beta\}, M, \ell)                                                                             & \rightarrow (G\sigma, M\sigma, \ell') \\
            (G_1 \cup \{\alpha\}, M \defeq \langl G_2 \cup \{\beta\}, S\rangl, \ell)                                        & \rightarrow ((G_1 \cup G_2\rho)\sigma, M\sigma, \ell') \\
            (G, M \defeq \{\langl \{ \alpha \} \cup G_1, S_1\rangl,\langl \{ \beta \} \cup G_2, S_2\rangl\} \cup M', \ell)    & \rightarrow ((G \cup G_1\rho \cup G_2\rho)\sigma, M\sigma, \ell') \\
            (G, M \defeq \langl \{ \alpha, \beta \} \cup G_1, S\rangl \cup M', \ell)                                          & \rightarrow ((G \cup G_1\rho)\sigma, M\sigma, \ell')
        \end{align*}
        where each represents reduction of an edge-edge, edge-box, box-box and of box internals respectively.
    \end{definition}

    \begin{example*}
        \begin{figure}[H]
            \centering
            \begin{subfigure}{0.4\linewidth}
                \centering
                \begin{tikzpicture}[transform shape, every node/.style={circle, fill=black!100, inner sep=0.05cm}]
                    \coordinate[anchor=center](nw);
                    \coordinate[below=3cm of nw](sw);
                    \coordinate[right=3cm of nw](ne);
                    \coordinate[below right=3cm and 3cm of nw](se);
                    \draw[-] (nw) -- (ne) -- (se) -- (sw) -- (nw);
                    \node[right=1.5cm of sw, label=below left:{$x$}](x){};
                    \coordinate[below=1cm of x](xba){};
                    \coordinate[above=1cm of x](xaa){};
                    \node[above left=1cm of xaa](v){};
                    \node[above right=1cm of xaa](w){};
                    \node[below left=1cm of xba, label=left:{$y$}](y){};
                    \node[below right=1cm of xba, label=right:{$z$}](z){};
                    \draw[-{<[scale=2]}] (xaa) -- (x);
                    \draw[-{>[scale=2]}] (xba) -- (x);
                    \draw[-{>[scale=2]}] (w) to [out=140, in=40] (v);
                    \draw[-] (w) -- (xaa) -- (v);
                    \draw[-] (y) -- (xba) -- (z);
                \end{tikzpicture}
                \caption*{$\bar{x}\, y \, z \; | \; !(u \, v)(x \, u \, v \; | \; \bar{u} \, v)$}
            \end{subfigure}
            $\longrightarrow$
            \begin{subfigure}{0.4\linewidth}
                \centering
                \begin{tikzpicture}[transform shape, every node/.style={circle, fill=black!100, inner sep=0.05cm}]
                    \coordinate[anchor=center](nw);
                    \coordinate[below=3cm of nw](sw);
                    \coordinate[right=3cm of nw](ne);
                    \coordinate[below right=3cm and 3cm of nw](se);
                    \draw[-] (nw) -- (ne) -- (se) -- (sw) -- (nw);
                    \node[right=1.5cm of sw, label=below left:{$x$}](x){};
                    \coordinate[below=1cm of x](xba){};
                    \coordinate[above=1cm of x](xaa){};
                    \node[above left=1cm of xaa](v){};
                    \node[above right=1cm of xaa](w){};
                    \node[below left=1cm of xba, label=left:{$y$}](y){};
                    \node[below right=1cm of xba, label=right:{$z$}](z){};
                    \draw[-{<[scale=2]}] (xaa) -- (x);
                    \draw[-{>[scale=2]}] (w) to [out=140, in=40] (v);
                    \draw[-] (w) -- (xaa) -- (v);
                    \draw[-{>[scale=2]}] (z) -- (y);
                \end{tikzpicture}
                \caption*{$\bar{y} \, z \; | \; !(u \, v)(x \, u \, v \; | \; \bar{u} \, v)$}
            \end{subfigure}
        \end{figure}
    \end{example*}

    \begin{remark*}
        The graph objects associated with Solo Diagrams are particularly similar to bigraphs.
        These are a combination of a (possibly cyclic) `link graph' and a (strictly acyclic) `place graph', where the collection of nodes and edges forms the former and the subgraphs within boxes dictate the latter.
        Despite these two similar areas of research, including overlapping research by~\cite{bigraphs-pervasive-calculus}, it is found to be less relevant for this topic.
    \end{remark*}


% Copyright 2018-2021 Melvin Eloy Irizarry-Gelpí
\setcounter{chapter}{5}
\chapter{RLC Circuits}
%
In this experiment you will study circuits with resistors, inductors, and capacitors interacting with a source of alternating currents.
%
\section{Preliminary}
%
In a previous experiment, you studied RC and RL circuits with a \textbf{battery} (actually, a few batteries connected in series) as the source of current. Batteries provide \textbf{DC} or \textbf{direct currents}, meaning that over time the current does not change direction. Strictly speaking the previous circuits were DC circuits.

When the current switches direction over time, you have an \textbf{AC} or \textbf{alternating current}. A circuit with an AC source is called an \textbf{AC circuit}. An important parameter that describes how often an AC switches direction is $f$, the \textbf{frequency of oscillation}. Recall that frequency is a quantity measured in \textbf{hertz} (Hz). One hertz is equivalent to one full oscillation per second:
\begin{equation}
	1 \ \text{Hz} = 1 \ \text{oscillation/s}
\end{equation}
For an AC with a frequency of 1 Hz, this means that it takes 1 second for the current to do a full oscillation. Note that a full current oscillation involves two switches of current direction: the first switch changes the original direction and the second switch returns it to where it was originally. Another useful quantity is $T$, the \textbf{period of oscillation}. The period $T$ is related to the frequency $f$ via
\begin{equation} \label{eq.06.period}
	T = \frac{1}{f}
\end{equation}
Sometimes it will be useful to use frequency and sometimes it will be useful to use period.

A quick reminder that electrical \textbf{resistance} is measured in units of ohms;
\begin{equation}
	1 \ \text{ohm} = 1 \ \text{V/A}
\end{equation}
\textbf{capacitance} is measured in units of farads;
\begin{equation}
	1 \ \text{farad} = 1 \ \text{C/V} = 1 \ \text{s/ohm}
\end{equation}
and \textbf{inductance} is measured in units of henrys;
\begin{equation}
	1 \ \text{henry} = 1 \ \text{V{ }\textperiodcentered{ }s/A} = 1 \ \text{ohm{ }\textperiodcentered{ }s}
\end{equation}
%
\subsection{Reactance}
%
Resistors, capacitors and inductors behave differently in an AC circuit. Each of these components display an opposition to the change in electric current that is called \textbf{reactance}. The mathematical symbol for reactance is $X$. There is \textbf{resistive} reactance $X_{R}$, \textbf{capacitive} reactance $X_{C}$, and \textbf{inductive} reactance $X_{L}$. Each reactance quantity is measured in units of ohms (same as resistance).
%
\subsubsection{Resistive Reactance}
%
The reactance associated to a resistor is called resistive reactance. For all practical purposes, the \textbf{resistive reactance} $X_{R}$ of a given resistor is equal to the resistance $R$:
\begin{equation} \label{eq.06.reactance.R}
	X_{R} = R
\end{equation}
Note that $X_{R}$ only depends on the properties of the resistor and not on the frequency of the AC source. That is, it does not matter how often the current switches direction, the resistive reactance is the same.
%
\subsubsection{Capacitive Reactance}
%
The reactance associated to a capacitor is called capacitive reactance. The \textbf{capacitive reactance} $X_{C}$ of a given capacitor is given by
\begin{equation} \label{eq.06.reactance.C}
	X_{C} = \frac{1}{2 \pi f C} = \frac{T}{2 \pi C}
\end{equation}
Here $f$ is the frequency of oscillation of the AC source, $T$ is the period of oscillation of the AC source, and $C$ is the amount of capacitance in the capacitor. Note that $X_{C}$ is directly proportional to the period, or, equivalently, inversely proportional to the frequency. That is, if you increase the frequency of the AC source, then the reactance should decrease.
%
\subsubsection{Inductive Reactance}
%
The reactance associated to an inductor is called inductive reactance. The \textbf{inductive reactance} $X_{L}$ of a given inductor is defined as
\begin{equation} \label{eq.06.reactance.L}
	X_{L} = 2 \pi f L = \frac{2 \pi L}{T}
\end{equation}
Here $f$ is the frequency of oscillation of the AC source, $T$ is the period of oscillation of the AC source, and $L$ is the amount of inductance in the inductor. Note that $X_{L}$ is directly proportional to the frequency, or, equivalently, inversely proportional to the period. That is, if you increase the frequency of the AC source, then the reactance should increase.
%
\subsection{Impedance}
%
Reactance is associated with an individual component in a circuit. To describe the \textbf{entire circuit}, you use the \textbf{impedance} $Z$. Just like reactance and resistance, impedance is also measured in units of ohms. The exact definition of impedance depends on what the components in a circuit are. You are going to need the impedance for an RL and RLC circuit.
%
\subsubsection{RL AC Circuit}
%
An \textbf{RL AC circuit} consist of an \textbf{AC source} connected to a \textbf{resistor} and an \textbf{ideal inductor} in series. Thus there is some amount of resistive and inductive reactance. The RL impedance $Z$ takes these two contributions into account:
\begin{equation} \label{eq.06.impedance.RL}
	Z = \sqrt{X_{R}^{2} + X_{L}^{2}} = \sqrt{R^{2} + 4 \pi^{2} f^{2} L^{2}}
\end{equation}
You can think of $Z$ as the hypotenuse of a right triangle with the reactances $X_{R}$ and $X_{L}$ as sides. The squared impedance takes the form
\begin{equation} \label{eq.06.impedance.squared}
	Z^{2} = R^{2} + 4\pi^{2} f^{2} L^{2}
\end{equation}
Note that in this form, the relation between squared impedance and squared frequency lends itself for a linear fit: the intercept is the squared resistance, and the slope is proportional to the squared inductance.

An ideal inductor has negligible electrical resistance. In practice you work with \textbf{non-ideal inductors}, with a non-negligible amount of electrical resistance $r$. This can be taken into account as follows:
\begin{equation}
	Z^{2} = (R + r)^{2} + 4 \pi^{2} f^{2} L^{2}
\end{equation}
That is, the resistance from the inductor affects the value of the intercept of the linear fit.
%
\subsubsection{RLC AC Circuit}
%
An \textbf{RLC AC circuit} consist of an \textbf{AC source} connected to a \textbf{resistor}, a \textbf{capacitor}, and an \textbf{ideal inductor} in series. Now there is some amount of resistive, capacitive, and inductive reactance. The RLC impedance $Z$ now takes these three contributions into account:
\begin{equation} \label{eq.06.eq.06.impedance.RLC}
	Z = \sqrt{X_{R}^{2} + X_{L}^{2} - X_{C}^{2}} = \sqrt{R^{2} + 4 \pi^{2} f^{2} L^{2} - \frac{1}{4 \pi^{2} f^{2} C^{2}}}
\end{equation}
Something special happens when $X_{L} = X_{C}$:
\begin{align} \label{eq.06.resonant.frequency}
	X_{L}(f_{\text{LC}}) = X_{C}(f_{\text{LC}}) && \Longrightarrow && 2\pi f_{\text{LC}} L = \frac{1}{2\pi f_{\text{LC}} C} && \Longrightarrow && f_{\text{LC}} = \frac{1}{2 \pi \sqrt{LC}}
\end{align}
That is, when the frequency takes the special value $f_{\text{LC}}$ (that depends on the amount of inductance and capacitance), the impedance is given entirely by the amount of resistance. This situation is called \textbf{resonance} and $f_{\text{LC}}$ is known as the \textbf{resonant frequency}. When the values of the capacitance and inductance in the circuit conspire to achieve the resonant frequency, the impedance reduces to the resistance:
\begin{equation}
	Z = R
\end{equation}
This is the minimum value possible of impedance.
%
\section{Experiment}
%
There are four experiments:
\begin{enumerate}
	\item Capacitor in AC circuit
	\item RL AC circuit
	\item RLC AC circuit without the core
	\item RLC AC circuit with the core
\end{enumerate}
In the first two experiments you measure the voltage and current as the frequency of the AC is changed. In the last two experiments, you see the effects of the resonance state.
%
\subsection{Part 1: Capacitor AC Circuit}
%
The circuit has \textbf{one capacitor} (10\textsuperscript{\textminus 5} F) connected to an AC source whose frequency you can adjust. You measured voltage and current over time. For each measurement you change the frequency of the AC source. The range of frequencies is from 100 Hz to 1000 Hz. Table \ref{table.capacitor.student} has the frequency values used for each run.

Note that the voltage measured here is the \textbf{voltage across the capacitor}.
%
\subsection{Part 2: RL AC Circuit}
%
The circuit has \textbf{one resistor} (10 ohm) and \textbf{one non-ideal inductor} (0.005 H and 4.20 ohm) connected in series to an AC source whose frequency you can adjust. You measured voltage and current over time. For each measurement you change the frequency of the AC source. The range of frequencies is from 100 Hz to 1000 Hz. Table \ref{table.RL.student} has the frequency values used for each run.

Note that the voltage measured here is the \textbf{voltage across the resistor and inductor in series}. That is, between the input terminal of the resistor, and the output terminal of the inductor.
%
\subsection{Part 3: RLC AC Circuit (without core)}
%
The circuit has \textbf{one resistor} (in the form of a light bulb, with unknown resistance), \textbf{one capacitor} (10\textsuperscript{\textminus 5} F), and \textbf{one non-ideal inductor} (0.005 H and 4.20 ohm; without core) connected in series to an AC source whose frequency you can adjust. You measured current over time. For each measurement you change the frequency of the AC source. The range of frequencies is from 100 Hz to 1300 Hz. Table \ref{table.RLC.student} has the frequency values used for each run.
%
\subsection{Part 4: RLC AC Circuit (with core)}
%
The circuit has \textbf{one resistor} (in the form of a light bulb, with unknown resistance), \textbf{one capacitor} (10\textsuperscript{\textminus 5} F), and \textbf{one non-ideal inductor} (0.0213 H and 4.20 ohm; with core) connected in series to an AC source whose frequency you can adjust. You measured current over time. For each measurement you change the frequency of the AC source. The range of frequencies is from 100 Hz to 550 Hz. Table \ref{table.RLCcore.student} has the frequency values used for each run.
%
\section{Analysis}
%
Here is what to do for each part.
%
\subsection{Part 1: Capacitor AC Circuit}
%
The impedance of a circuit with a single capacitor is just the capacitive reactance:
\begin{equation}
	Z = X_{C} = \left(\frac{1}{2 \pi C}\right) \frac{1}{f} = \left(\frac{1}{2 \pi C}\right) T
\end{equation}
In this part you would like to check that the inductance is inversely proportional to the frequency of the AC source, or equivalently, directly proportional to the period of the AC source. You know the values of the frequencies used (and thus of the periods used), so you just need to find the experimental values of $Z$. To find this, you use the voltage and current data. For each run, you find the maximum voltage $V_{\text{max}}$ and the maximum current in $I_{\text{max}}$. Then, the impedance is given by the ratio
\begin{equation}
	Z = \frac{V_{\text{max}}}{I_{\text{max}}}
\end{equation}
Once you have the impedance, you can check that the chart of impedance versus frequency is not linear, but the chart of impedance versus period is linear. You can then add a linear fit. As stated by (\ref{eq.06.reactance.C}), the slope is related to the amount of capacitance:
\begin{equation}
	\text{slope} = \frac{1}{2 \pi C}
\end{equation}
You can calculate the expected slope, the observed slope (use the \texttt{SLOPE}) function, and then calculate the percent difference:
\begin{equation}
	\text{Percent Difference} = 100 \times \left( \frac{\text{observed } - \text{ expected}}{\text{expected}} \right)
\end{equation}
%
\subsection{Part 2: RL AC Circuit}
%
Since an RL AC circuit is used in this part, you can check the relationship between impedance $Z$ and frequency (that is, you are not measuring the reactance of individual components). The impedance is found in the same way as before:
\begin{equation}
	Z = \frac{V_{\text{max}}}{I_{\text{max}}}
\end{equation}
You can chart impedance versus frequency and the shape is almost linear. But there is no linear relationship between these two quantities! Looking at (\ref{eq.06.impedance.squared}), the expected linear relationship is between $Z^{2}$ and $f^{2}$. Make a second chart with these quantities and add a best-fit line. Compare the slope and intercept of the best-fit line to the expected values in (\ref{eq.06.impedance.squared}). The slope should be related to the inductance, and the intercept should be related to the \textbf{total amount of resistance} in the circuit. Recall that the inductor has some inherent amount of resistance, as was found in Laboratory 5:
\begin{equation}
	r = 4.20 \ \text{ohm}
\end{equation}
Adding this to the 10 ohm from the resistor, the total resistance is
\begin{equation}
	R_{\text{eq}} = R + r = 14.20 \ \text{ohm}
\end{equation}
The expected slope is
\begin{equation}
	\text{slope} = 4 \pi^{2} L^{2}
\end{equation}
and the expected intercept is
\begin{equation}
	\text{intercept} = (R + r)^{2}
\end{equation}
Again, you can also find the percent difference to judge the quality of your results.
%
\subsection{Part 3: RLC AC Circuit (without core)}
%
For the RLC circuit, you can see what happens to the current in the circuit as the frequency of the AC source  approaches the value of the resonant frequency. To do this, find the maximum current for each frequency run and then make a chart of $I_{\text{max}}$ versus frequency. There should be a peak. The peak corresponds to the resonant frequency. That is, at the resonant frequency, the current in the circuit oscillates with the largest amplitude.

The expected resonant frequency is given by (\ref{eq.06.resonant.frequency}), with the inductance equal to 0.005 H.
%
\subsection{Part 4: RLC AC Circuit (with core)}
%
In Laboratory 5 you found that when the core was used, the inductance is larger than what is written on the label. Indeed, you should have found a value close to 0.0213 H. This makes the resonant frequency smaller. You can repeat the steps as in Part 3 and confirm that the current now has a peak in a lower frequency.
%
\section{My Data}
%
My results for Part 1 are in Table \ref{table.results.C}. As you can see from Figure \ref{figure.06.part.1.f}, in this circuit the impedance is not directly proportional to the frequency. Indeed, as you can see from Figure \ref{figure.06.part.1.T}, the impedance is directly proportional to the period of oscillation. The result is that as the frequency is increased, the impedance decreases, and thus the amount of current allowed increases.

My results for Part 2 are in Table \ref{table.results.RL}. Figure \ref{figure.06.part.2.f} appears to indicate that the impedance is directly proportional to the amount of frequency. However, the best fit is off for some values. Looking at the squared impedance versus the squared frequency in Figure \ref{figure.06.part.2.f2} shows that the linear relation is stronger between those two quantities. Opposite to what happens in Part 1, when the frequency is increased, the impedance also increases, and thus the amount of current allowed decreases.

My results for Part 3 are in Table \ref{table.results.RLC}. As you can see from Figure \ref{figure.06.part.3.f}, the current is largest in a certain region consistent with the theoretical expectation. Note that when the current is largest, the impedance is smallest.

My results for Part 4 are in Table \ref{table.results.RLCcore}. When the core is used, the inductance becomes larger. This should have the effect of making the resonant frequency smaller. As Figure \ref{figure.06.part.4.f} shows, this is indeed the case.
%
\section{Your Data}
%
Your data for Parts 1, 2, 3, and 4 is structured in the same way as Tables \ref{table.capacitor.student}, \ref{table.RL.student}, \ref{table.RLC.student}, and \ref{table.RLCcore.student}.
%
\pagebreak
\section{Your Lab Report}
%
Your laboratory report should include the following:
\begin{itemize}
	\item A table like Table \ref{table.results.C} with your results for part 1.
	\item A chart like Figure \ref{figure.06.part.1.T} with impedance in the vertical axis, and period in the horizontal axis for the data in part 1. Include the best fit line and display the equation.
	\item A table like Table \ref{table.results.RL} with your results for part 2.
	\item A chart like Figure \ref{figure.06.part.2.f2} with squared impedance in the vertical axis, and squared frequency in the horizontal axis for the data in part 2. Include the best fit line and display the equation.
	\item A chart like Figure \ref{figure.06.part.3.f} with maximum current in the vertical axis, and frequency in the horizontal axis for the data in part 3. Confirm that the maximum current corresponds occurs near frequencies that are close to the expected resonant frequency.
	\item A chart like Figure \ref{figure.06.part.4.f} with maximum current in the vertical axis, and frequency in the horizontal axis for the data in part 4. Confirm that the maximum current corresponds occurs near frequencies that are close to the expected resonant frequency.
\end{itemize}
%
\newpage
\section{Tables}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r|r|r|r}
			\textbf{Run} & $f$ (Hz) & $1/f$ (s) & $V_{\text{max}}$ (V) & $I_{\text{max}}$ (A) & $V_{\text{max}}/I_{\text{max}}$ (ohm) \\
			\hline
			1 & 100 & & & & \\
			2 & 250 & & & & \\
			3 & 400 & & & & \\
			4 & 550 & & & & \\
			5 & 700 & & & & \\
			6 & 850 & & & & \\
			7 & 1000 & & & & \\
			\hline
		\end{tabular}
	\end{center}
	\caption{Capacitor AC Circuit}
	\label{table.capacitor.student}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r|r|r|r}
			\textbf{Run} & $f$ (Hz) & $1/f$ (s) & $V_{\text{max}}$ (V) & $I_{\text{max}}$ (A) & $V_{\text{max}}/I_{\text{max}}$ (ohm) \\
			\hline
			1 & 100 & & & & \\
			2 & 250 & & & & \\
			3 & 400 & & & & \\
			4 & 550 & & & & \\
			5 & 700 & & & & \\
			6 & 850 & & & & \\
			7 & 1000 & & & & \\
			\hline
		\end{tabular}
	\end{center}
	\caption{RL AC Circuit}
	\label{table.RL.student}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r}
			\textbf{Run} & $f$ (Hz) & $I_{\text{max}}$ (A) \\
			\hline
			1 & 100 & \\
			2 & 250 & \\
			3 & 400 & \\
			4 & 550 & \\
			5 & 700 & \\
			6 & 850 & \\
			7 & 1000 & \\
			8 & 1150 & \\
			9 & 1300 & \\
			\hline
		\end{tabular}
	\end{center}
	\caption{RLC AC Circuit without core}
	\label{table.RLC.student}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r}
			\textbf{Run} & $f$ (Hz) & $I_{\text{max}}$ (A) \\
			\hline
			1 & 100 & \\
			2 & 250 & \\
			3 & 400 & \\
			4 & 550 & \\
			5 & 200 & \\
			6 & 300 & \\
			7 & 350 & \\
			8 & 450 & \\
			9 & 500 & \\
			\hline
		\end{tabular}
	\end{center}
	\caption{RLC AC Circuit with core}
	\label{table.RLCcore.student}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r|r}
			\textbf{Quantity} & \textbf{Observed} & \textbf{Expected} & \textbf{P.D.} (\%) \\
			\hline
			Slope (ohm/s) & 14683.346 & 15915.494 & \textminus 7.742 \\
			\hline
		\end{tabular}
	\end{center}
	\caption{Results for capacitor AC circuit}
	\label{table.results.C}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r|r|r}
			\textbf{Quantity} & \textbf{Observed} & \textbf{Expected} & \textbf{P.D.} (\%) \\
			\hline
			Slope (ohm\textsuperscript{2}/Hz\textsuperscript{2}) & 9.87{ }\texttimes{ }10\textsuperscript{\textminus 4} & 9.92{ }\texttimes{ }10\textsuperscript{\textminus 4} & 0.52 \\
			Intercept (ohm\textsuperscript{2}) & 199.778 & 201.64 & \textminus 0.924 \\
			\hline
		\end{tabular}
	\end{center}
	\caption{Results for RL AC circuit}
	\label{table.results.RL}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r}
			Observed Resonant Frequency & Expected Resonant Frequency \\
			\hline
			Between 700 Hz and 800 Hz & 711.76 Hz \\
			\hline
		\end{tabular}
	\end{center}
	\caption{Results for RLC AC circuit (without core)}
	\label{table.results.RLC}
\end{table}
%
\begin{table}[ht]
	\begin{center}
		\begin{tabular}{l|r}
			Observed Resonant Frequency & Expected Resonant Frequency \\
			\hline
			Between 300 Hz and 350 Hz & 344.85 Hz \\
			\hline
		\end{tabular}
	\end{center}
	\caption{Results for RLC AC circuit (with core)}
	\label{table.results.RLCcore}
\end{table}
%
\newpage
\FloatBarrier
\section{Figures}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-1-f.pdf}
	\caption{Part 1: Period}
	\label{figure.06.part.1.f}
\end{figure}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-1-T.pdf}
	\caption{Part 1: Frequency}
	\label{figure.06.part.1.T}
\end{figure}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-2-f.pdf}
	\caption{Part 2: Frequency}
	\label{figure.06.part.2.f}
\end{figure}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-2-f2.pdf}
	\caption{Part 2: Squared Frequency}
	\label{figure.06.part.2.f2}
\end{figure}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-3.pdf}
	\caption{Part 3}
	\label{figure.06.part.3.f}
\end{figure}
%
\begin{figure}[ht]
	\centering
	\includegraphics[scale=0.74]{image/06-RLC/part-4.pdf}
	\caption{Part 4}
	\label{figure.06.part.4.f}
\end{figure}
%


\chapter{A Spatial Model for Rare Binary Events}
\chaptermark{Spatial Rare Binary}
\label{chap:three}

\section{Introduction}\label{rbs:intro}

The goals of spatial binary data analysis are often to estimate covariate effects while accounting for spatial dependence and to make predictions at locations without samples.
A common approach to incorporate spatial dependence in the model for binary data is relating a continuous spatial process $Z(\bs) \in \mathbb{R}$ to the binary response $Y(\bs)$ by thresholding, $Y(\bs) = I[Z(\bs) > c]$, where $I[\cdot]$ is an indicator function.
In many spatial analyses of binary data, a Gaussian process is used to model $Z(\bs)$.
This is true for both spatial probit and spatial logistic regression.
In these models, spatial dependence is determined by the joint probability that two sites simultaneously exceed the threshold $c$.
However, when $c$ is large, and thus $Y(\bs) = 1$ is rare, then the asymptotic theory suggests that the Gaussian process will model dependence poorly.
In fact, even under strong spatial correlation for $Z(\bs)$, it gives asymptotic independence \citep{Sibuya1960}, suggesting that for rare binary data, the Gaussian model will not perform very well.

We propose using a latent max-stable process \citep{deHaan1984} for $Z(\bs)$ because it allows for asymptotic dependence.
The max-stable process arises as the limit of the location-wise maximum of infinitely many spatial processes, and any finite-dimensional representation of a max-stable process has generalized extreme value distribution (GEV) marginal distributions.
Max-stable processes are extremely flexible, but are often challenging to work with in high dimensions \citep{Wadsworth2014,Thibaud2013a}.
To address this challenge, methods have been proposed that implement composite likelihood techniques for max-stable processes \citep{Padoan2010,Genton2011,Huser2014}.
Composite likelihoods have also been used to model binary spatial data \citep{Heagerty1998}, but not using max-stable processes.
As an alternative to these composite approaches, \citet{Reich2012} present a hierarchical model that implements a low-rank representation for a max-stable process.
We choose to use this low-rank representation for our rare binary spatial regression model.
Our model builds on related work by \citet{Wang2010} who use a GEV link for non-spatial binary data.
The proposed model generalizes this to have spatial dependence.

The paper proceeds as follows.
In \sref{rbs:maxstab} we present the proposed latent max-stable process for spatially dependent rare binary analysis.
In \sref{rbs:multivariate} we give the bivariate distribution for our model.
In \sref{rbs:spatdep} we show a link between a commonly used measure of dependence between binary variables and another metric for extremal dependence.
The computing for our model is outlined in \sref{rbs:comp}.
Finally, we present a simulation study in \sref{rbs:sim} which is followed in \sref{rbs:dataanalysis} by a data analysis of two species: \tamarix{} and \hedysarum{}.
Lastly, in \sref{rbs:conclusions} we provide some discussion and possibilities for future research.

\section{Spatial dependence for binary regression} \label{rbs:maxstab}

Let $Y(\bs)$ be the binary response at spatial location $\bs$ in a spatial domain of interest $\calD \in \mathbb{R}^2$.
We assume $Y(\bs) = I[Z(\bs) > 0]$ where $Z(\bs)$ is a latent continuous max-stable process.
The marginal distribution of $Z(\bs)$ at site $\bs$ is GEV with location $\bX(\bs)^\top \bbeta$, scale $\sigma > 0$, and shape $\xi$, where $\bX(\bs)$ is a $p$-vector of spatial covariates at site $\bs$ and $\bbeta$ is a $p$-vector of regression coefficients.
We set $\sigma = 1$ for identifiability because only the sign and not the scale of $Z$ affects $Y$.
If $\bX(\bs)^\top \bbeta = \mu$ for all $\bs$, then $P(Y = 1)$ is the same for all observations, and the two parameters $\mu$ and $\xi$ are not individually identifiable.
So when there are no covariates, we fix $\xi = 0$.
Although $\bbeta$ and $\xi$ could be permitted to vary across space, we assume that they are constant across $\calD$.
At spatial location $\bs$, the marginal distribution (over $Z(\bs)$) is
\begin{align}
	P[Y(\bs) = 1] = 1 - \exp\left\{ -\displaystyle\frac{ 1 }{ z(\bs)} \right\}
\end{align}
where $z(\bs) = \left[1 - \xi \bX(\bs)^\top \bbeta\right]^{1 / \xi}$.
This is the same as the marginal distribution given by \citet{Wang2010}.

For a finite collection of locations $\bs_1, \ldots, \bs_n,$ we denote by $\bY = [Y(\bs_1), \ldots, Y(\bs_n)]^\top$ the vector of observations.
The spatial dependence of $\bY$ is determined by the joint distribution of the latent variable $\bZ = [Z(\bs_1),\ldots, Z(\bs_n)]^\top$.
To incorporate spatial dependence, we consider the hierarchical representation of the max-stable process proposed in \citet{Reich2012}.
Consider a set of positive stable (PS) random effects $A_1, \ldots, A_L \iid \text{PS}(\alpha)$ associated with spatial knots $\bv_1, \ldots, \bv_L \in \mathbb{R}^2$.
The hierarchical model is given by
\begin{align} \label{rbeq:hiermodel}
Z(\bs_i) | A_1, \ldots, A_L \ind \text{GEV}\left[\bX(\bs_i)^\top \bbeta + \frac{\theta(\bs_i)^\xi - 1}{\xi}, \alpha \theta(\bs_i), \xi \alpha\right] \quad \text{and} \quad \theta(\bs_i) = \left[\sum_{l = 1}^L A_l w_l (\bs_i)^{1 / \alpha} \right]^\alpha
\end{align}
where $w_{l}(\bs_i) > 0$ are a set of $L$ weight functions that vary smoothly across space and satisfy the condition \mbox{$\displaystyle \sum_{l = 1}^L w_l(\bs) = 1$} for all $\bs$, and $\alpha\in(0,1)$ determines the strength of dependence, with $\alpha$ near zero giving strong dependence and $\alpha=1$ giving joint independence.

Because the latent $\bZ(\bs)$ are independent given the random effects $\theta(\bs)$, the binary responses are also conditionally independent.
This leads to the tractable likelihood
\begin{align} \label{rbeq:condlike}
Y(\bs_i) | A_l, \ldots, A_L &\ind \text{Bern}[\pi(\bs_i)]
\end{align}
where
\begin{align} \label{rbeq:pyeq1cond}
\pi(\bs_i) &= 1 - \exp \left\{ -\displaystyle \sum_{ l = 1 }^{L} A_l \left[ \frac{ w_{l}(\bs_i) }{ z(\bs_i) } \right]^{ 1/\alpha} \right\}
\end{align}
and $z(\bs) = [1 + \xi \bX(\bs)^\top \bbeta)]^{1 / \xi}$.
Marginally over the $A_l$, this gives
\begin{align}
Z(\bs) \sim \text{GEV}\left[\bX(\bs)^\top \bbeta, 1, \xi\right],
\end{align}
and thus $P[Y(\bs) = 1] = 1 - \exp\left\{-\displaystyle \frac{1}{z(\bs)}\right\}$.

Many weight functions are possible, but the weights must be constrained so that $\displaystyle \sum_{l=1}^L w_{l}(\bs_i)=1$ for $i=1,\ldots,n$ to preserve the marginal GEV distribution.
For example, \cite{Reich2012} take the weights to be scaled Gaussian kernels with knots $\bv_l$,
\begin{align}\label{rbeq:w}
w_{l}(\bs_i) = \frac{\exp\left[-0.5\left(||\bs_i-\bv_l||/\rho\right)^2\right]}
{\displaystyle \sum_{j=1}^L\exp\left[-0.5\left(||\bs_i-\bv_j||/\rho\right)^2\right]}
\end{align}
where $||\bs_i - \bv_l||$ is the distance between site $\bs_i$ and knot $\bv_l$, and the kernel bandwidth $\rho>0$ determines the spatial range of the dependence, with large $\rho$ giving long-range dependence and vice versa.

After marginalizing out the positive stable random effects, the joint distribution of $\bZ$ is
\begin{align}\label{rbeq:jointCDF}
G(\bz) = P\left[Z(\bs_1) < z(\bs_1), \ldots, Z(\bs_n) < z(\bs_n)\right] = \exp\left\{-\sum_{l=1}^L\left[\sum_{i=1}^n\left(\frac{w_{l}(\bs_i)}{z(\bs_i)}\right)^{1/\alpha}\right]^{\alpha}\right\},
\end{align}
where $G(\cdot)$ is the CDF of a multivariate GEV distribution.
This is a special case of the multivariate GEV distribution with asymmetric Laplace dependence function \citep{Tawn1990}.

\section{Joint distribution}\label{rbs:multivariate}

We give an exact expression in the case where there are only two spatial locations which is useful for constructing a pairwise composite likelihood \citep{Padoan2010} and studying spatial dependence.
When $n = 2$, the probability mass function is given by
\begin{align} \label{rbeq:biv}
\text{P}[Y(\bs_i) = y_i, Y(\bs_j) = y_j] = \begin{cases}
\varphi(\bz), &y_i = 0, y_j = 0\\
\exp\left\{ - \displaystyle \frac{ 1 }{ z(\bs_i) } \right\} - \varphi(\bz), &y_i = 1, y_j = 0 \\
\exp\left\{ - \displaystyle \frac{ 1 }{ z(\bs_j) } \right\} - \varphi(\bz), &y_i = 0, y_j = 1 \\
1 - \exp\left\{ - \displaystyle\frac{ 1 }{ z(\bs_i) } \right\} - \exp \left\{ -\displaystyle \frac{ 1 }{z(\bs_j)} \right\} + \varphi(\bz), \quad &y_i = 1, y_j = 1
\end{cases}
\end{align}
where $\varphi(\bz) = \exp \left\{ - \displaystyle \sum_{ l = 1 }^{ L } \left[ \left( \displaystyle \frac{ w_{ l }(\bs_i) }{ z(\bs_i) } \right)^{1/\alpha} + \left( \displaystyle \frac{ w_{l }(\bs_j)}{z(\bs_j)} \right)^{1/\alpha} \right]^{\alpha} \right\}$.
For more than two locations, we are also able to compute the exact likelihood when $n$ is large but the number of events $K = \displaystyle \sum_{i = 1}^n Y(\bs_i)$ is small, as might be expected for very rare events, see \aref{rba:likelihoodderivation}.

% - Assume Z1 and Z2 are both GEV(\beta,1,1) so that the probability of Yi=1 decreases to zero as beta increases
% - A common measure of dependence between binary variables is Cohen’s Kappa, K(beta) =
% - For the spatial model we get K(beta)=
% - To measure extremal dependence let beta go to infinity so that event are increasing more rare
% - K = limK(beta) = …
% - This is similar to the extremal coefficient in Reich and Shaby

\section{Quantifying spatial dependence} \label{rbs:spatdep}

Assume that $Z(\bs_1)$ and $Z(\bs_2)$ are both GEV$(\beta, 1, 1)$ so that $P(Y_i = 1)$ decreases to zero as $\beta$ increases.
A common measure of dependence between binary variables is Cohen's kappa \citep{Cohen1960},
\begin{align}
\kappa(\beta) = \frac{P_A - P_E}{1 - P_E}
\end{align}
where $P_A$ is the joint probability of agreement $P(Y_1 = Y_2)$ and $P_E$ is the joint probability of agreement under an assumption of independence $P(Y_i = 1)^2 + P(Y_i = 0)^2$.
For the spatial model,
\begin{align*}
P_A(\beta) &= 1 - 2 \exp\left\{ -\frac{1}{\beta} \right\} + 2 \exp\left\{-\frac{\vartheta(\bs_1, \bs_2)}{\beta}  \right\} \nonumber \\
P_E(\beta) &= 1 - 2 \exp \left\{ -\frac{1}{\beta} \right\} + 2 \exp \left\{ -\frac{2}{\beta} \right\}, \nonumber
\end{align*}
and
\begin{align} \label{rbeq:kappa}
\kappa(\beta) &= \frac{P_A(\beta) - P_E(\beta)}{1 - P_E(\beta)} = \frac{\exp\left\{-\displaystyle\frac{\vartheta(\bs_1, \bs_2) - 1}{\beta}  \right\} - \exp \left\{ -\displaystyle\frac{1}{\beta} \right\}}{1 - \exp \left\{ -\displaystyle\frac{1}{\beta} \right\}}
\end{align}
where $\vartheta (\bs_i, \bs_j) = \displaystyle \sum_{ l = 1 }^{ L } \left[ w_{l}(\bs_i)^{ 1/\alpha } +  w_{ l}(\bs_j)^{ 1/\alpha } \right]^\alpha$ is the pairwise extremal coefficient given by \citet{Reich2012}.
To measure extremal dependence, let $\beta \rightarrow \infty$ so that events are increasingly rare.
Then,
\begin{align}
\kappa = \lim_{\beta \rightarrow \infty} \kappa(\beta) = 2 - \vartheta(\bs_1, \bs_2)
\end{align}
which is the same as the $\chi$ statistic of \citet{Coles2001}, a commonly used measure of extremal dependence.

\section{Computation}\label{rbs:comp}

For small $K$, we can evaluate the likelihood directly.
When $K$ is large, we use Markov chain Monte Carlo (MCMC) methods with the random effects model to explore the posterior distribution.
To overcome challenges with evaluating the positive stable density, we follow \citet{Reich2012} and introduce a set of auxiliary variables $B_1, \ldots, B_L$ following the auxiliary variable technique of \citet{Stephenson2009} \citep[see Appendix A.3]{Reich2012}.
So, the hierarchical model is given by
\begin{align}
Y(\bs_i) | \pi(\bs_i) &\ind \text{Bern}[\pi(\bs_i)] \\
\pi(\bs_i) &= 1 - \exp \left\{ -\displaystyle \sum_{ l = 1 }^{L} A_l \left[ \frac{ w_{l}(\bs_i) }{ z(\bs_i) } \right]^{ 1/\alpha} \right\} \nonumber\\
% Z(\bs_i) | A_l, \ldots, A_L &\ind \text{GEV}[\bX(\bs_i)^\top \bbeta + \theta(\bs_i), \alpha \theta(\bs_i), \xi \alpha] \nonumber\\
A_l &\sim \text{PS}(\alpha) \nonumber
\end{align}
with priors $\bbeta \sim \text{N}(\bZero, \sigma^2_\beta \bI_p )$, $\xi \sim \text{N}(0, \sigma^2_\xi)$, $\rho \sim \text{Unif}(\rho_l, \rho_u)$, and $\alpha \sim \text{Beta}(a_\alpha, b_\alpha)$.
The model parameters are updated using Metropolis Hastings (MH) update steps, and the random effects $A_1, \ldots, A_L$, and auxiliary variables $B_1, \ldots, B_L$ are updated using Hamiltonian Monte Carlo (HMC) update steps.
The code for this is available online through \url{https://github.com/sammorris81/rare-binary}.

\section{Simulation study}\label{rbs:sim}

For our simulation study, we generate $n_m = 50$ datasets under 12 different simulation settings to explore the impact of sample size, sampling technique, and misspecification of link function.
We generate data from three possible underlying processes.
For each of the underlying processes, we generate complete datasets on a $100 \times 100$ rectangular grid of $n = 10,000$ locations.
If a simulated population is generated and $K < 50$, it is discarded and a new simulated population is generated.
This is done to guarantee that the rarity for all datasets is no lower than $0.5\%$.
For model fitting, we select a subsample and use the remaining sites to evaluate predictive performance.
For all models, we run the MCMC sampler for 25,000 iterations with a burn-in period of 20,000 iterations.
Convergence is assessed through visual inspection of traceplots.

\subsection{Latent processes} \label{rbs:simsettings}

The first process is a latent max-stable process that uses the GEV link described in \eref{rbeq:hiermodel} with knots on a $50 \times 50$ regularly spaced grid on $[0, 1] \times [0, 1]$.
For this process, we set $\alpha = 0.35$, $\rho = 0.1$, and $\beta_0 \approx 2.97$ which gives $K = 500$ (5\% rarity), on average.
Because there are no covariates, we set $\xi = 0$.
We then set $Y(\bs) = I[Z(\bs) > 0]$.

For the second process, we generate a latent variable from a spatial Gaussian process with a mean of $\text{logit}(0.05) \approx -2.94$ and an exponential covariance given by
\begin{align}
\text{Cov}(\bs_1, \bs_2) = \tau_\text{Gau}^2 \exp\left\{- \frac{||\bs_1 - \bs_2||}{\rho_\text{Gau}}\right\}
\end{align}
with $\tau_\text{Gau} = 10$ and $\rho_\text{Gau} = 0.1$.
Finally, we generate $Y(\bs_i) \ind \text{Bern}[\pi(\bs_i)]$
where $\pi(\bs_i) = \displaystyle \frac{ \exp\{z(\bs)\}}{1 + \exp\{z(\bs)\} }$.

For the third process, we generate data using a hotspot method.
For this process, we first generate hotspots throughout the space.
Let $n_\text{hs}$ be the number of hotspots in the space.
Then \mbox{$n_\text{hs} - 1 \sim$ Poisson$(2)$}.
This generation scheme ensures that every dataset has at least one hotspot.
We generate the hotspot locations $\bh_1, \ldots, \bh_{n_\text{hs}} \sim$ Unif$(0, 1)^2$.
Let $B_h$ be a circle of radius of radius $r_h$ around hotspot $h = 1, \ldots, n_\text{hs}$.
The $r_h$ differ for each hotspot and are generated i.i.d. from a Unif$(0.03, 0.08)$ distribution.
We set $P[Y(\bs_i) = 1] = 0.85$ for all $\bs_i$ in $B_h$, and $P[Y(\bs_i) = 1] = 0.0005$ for all $\bs_i$ outside of $B_h$.
These settings are selected to give an average of approximately $K = 500$ for the datasets.
\fref{rbfig:simulateddata} gives an example dataset from each of the data settings.

\begin{figure}  % code/analysis/simstudy/combine-tables.R
	\centering
	\includegraphics[width=\linewidth, trim={0em, 1em, 0em, 1em}]{plots/simulateddata}
	\caption{One simulated dataset from spatial GEV (left), spatial logistic (center), and hotspot (right) designs.}
	\label{rbfig:simulateddata}
\end{figure}

\subsection{Sampling methods} \label{rbs:simsampling}

We subsample the generated data using $n_s = 100$, $250$ initial locations for two different sampling designs.
The first is a two-stage spatially-adaptive cluster technique (CLU) taken from \citet{Pacifici2016}.
In this design, if an initial location is occupied, we also include the four rook neighbor (north, east, south, and west) sites in the sample.
For the second design, we use a simple random sample (SRS) with the same number of sites included in the cluster sample.
For the GEV setting, when $n_s = 100$, there are on average $117$ sites and at most $142$ sites in a sample, and when $n_s = 250$, there are on average $286$ sites and at most $332$ sites in a sample.
For the logistic setting, when $n_s = 100$, there are on average $118$ sites and at most $147$ sites in a sample, and when $n_s = 250$, there are on average $290$ sites and at most $330$ sites in a sample.
For the hotspot setting, when $n_s = 100$, there are on average $110$ sites and at most $128$ sites in a sample, and when $n_s = 250$, there are on average $275$ sites and at most $306$ sites in a sample.

\subsection{Methods} \label{rbs:methods}

For each dataset, we fit the model using three different models: the proposed spatial GEV model, a spatial probit model, and a spatial logistic model.
Logistic and probit methods assume the underlying process is Gaussian.
In this case, we assume that $Z(\bs)$ follows a Gaussian process with mean $\bX(\bs)^\top \bbeta$ and variance $\tau^2$.
For the simulation study, we use an intercept only model.
The marginal distributions are given by
\begin{align}
P\left[Y(\bs) = 1\right] = \begin{cases}
\displaystyle \frac{\exp\left[\bX^\top(\bs) \bbeta + \bW (\bs) \bepsilon \right]}{1 + \exp\left[\bX^\top(\bs) \bbeta + \bW (\bs) \bepsilon \right]}, \qquad &\text{logistic}\\
\Phi\left[\bX^\top \bbeta (\bs) + \bW (\bs) \bepsilon \right], \qquad &\text{probit}
\end{cases}
\end{align}
where $\bepsilon \sim$ N$(\bZero, \tau^2 \bI_L)$ are Gaussian random effects at the knot locations, and $\bW(\bs)$ are a set of $L$ basis functions given to recreate the Gaussian process at all sites.
We use our own code for the spatial probit model, but we use the \texttt{spGLM} function in the \texttt{spBayes} package \citep{Finley2015} to fit the spatial logistic model.
For the probit model, we use
\begin{align}
\bW_l(\bs_i) = \frac{\exp\left[-\left(||\bs_i-\bv_l||/\rho\right)^2\right]}
{\sqrt{\displaystyle \sum_{j=1}^L\exp\left[-\left(||\bs_i-\bv_j||/\rho\right)^2\right]^2}}.
\end{align}
For the logistic model, the $\bW_l(\bs_i)$ are the default implementation from \texttt{spGLM}.

\subsection{Priors} \label{rbs:simpriors}

For all models, we only include an intercept term $\beta_0$ in the model, and the prior for the intercept is \mbox{$\beta_0 \sim$ N$(0, 10)$}.
Additionally, for all models, the prior for the bandwidth is $\rho \sim$ Unif$(0.001, 1)$.
In all methods, we place knots at all data points.
For the GEV method, the prior for the spatial dependence parameter is $\alpha \sim$ Beta$(2, 5)$.
We select this prior because it gives greater weight to $\alpha < 0.5$, which is the point at which spatial dependence becomes fairly week, but also avoids values below 0.1 which can lead to numerical problems.
We fix $\xi = 0$ because we do not include any covariates.
For both the spatial probit and logistic models, the prior on the variance term for the random effects is IG$(0.1, 0.1)$ where IG$(\cdot)$ is an Inverse Gamma distribution.

\subsection{Model comparisons}\label{rbs:cv}

For each dataset, we fit the model using the $n_s$ observations as a training set, and validate the model's predictive power at the remaining grid points.
Let $\bs^*_j$ be the $j$th site in the validation set.
From the posterior distributions of the parameters we can calculate $P[Y(\bs^*_j) = 1]$.
To obtain $\hat{P}[Y(\bs^*_j) = 1]$, we take the average of the posterior distribution for each $\bs^*_j$.
We consider a few different metrics for comparing model performance.
One score is the Brier scores \citep[BS]{Gneiting2007}.
The Brier score for predicting an occurrence at site $\bs$ is given by $\{I[Y(\bs)=1] - \hat{P}[Y(\bs)=1]\}^2$.
We average the Brier scores over all test sites, and a lower score indicates a better fit.
The Brier score equally penalizes false negatives and false positives, but in the case of rare data, this may not be the best metric due to the unbalanced nature of the data.
Therefore, we also consider the receiver operating characteristic (ROC) curve, and the area under the ROC curve (AUROC) for the different methods and settings.
The ROC curve and AUROC are obtained via the \texttt{ROCR} \citep{Sing2005} package in \texttt{R} \citep{Rmanual}.
We then average AUROC across all datasets for each method and setting to obtain a single AUROC for each combination of method and setting.

\subsection{Results} \label{rbs:simresults}

Overall, we find that the spatial probit model actually performs quite well in all cases.
\tref{rbtbl:simbsresults} gives the Brier scores and AUROC for each of the methods.
Looking at Brier scores, we see that our model is outperformed by the probit model in all cases, and by the logistic models in many settings.
For AUROC, in a few of the settings, we do demonstrate a small improvement over the probit and logistic models.
Because these results are somewhat surprising, we also considered the performance metrics by rareness of the data.
We plot the AUROC for each link function with all sampling settings in \fref{rbfig:simsmooth-gev} -- \fref{rbfig:simsmooth-hot} using a Loess smoother.
These plots give evidence to suggest that as rareness increases, the spatial GEV method has potential to outperform the spatial probit and logistic models based on AUROC.

\begin{figure}[htbp]  % code/analysis/simstudy/combine-tables.R
	\centering
	\includegraphics[width=0.47\linewidth]{plots/byrareness-1}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-2}

	\includegraphics[width=0.47\linewidth]{plots/byrareness-3}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-4}
	\caption{Smooth of AUROC by rareness for each sample technique for the GEV link.}
	\label{rbfig:simsmooth-gev}
\end{figure}

\begin{figure}[htbp]  % code/analysis/simstudy/combine-tables.R
	\centering
	\includegraphics[width=0.47\linewidth]{plots/byrareness-5}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-6}

	\includegraphics[width=0.47\linewidth]{plots/byrareness-7}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-8}
	\caption{Smooth of AUROC by rareness for each sample technique for the logistic link.}
	\label{rbfig:simsmooth-log}
\end{figure}

\begin{figure}[htbp]  % code/analysis/simstudy/combine-tables.R
	\centering
	\includegraphics[width=0.47\linewidth]{plots/byrareness-9}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-10}

	\includegraphics[width=0.47\linewidth]{plots/byrareness-11}
	\includegraphics[width=0.47\linewidth]{plots/byrareness-12}
	\caption{Smooth of AUROC by rareness for each sample technique for the hotspot link.}
	\label{rbfig:simsmooth-hot}
\end{figure}

\begin{table}
	\caption{Brier scores ($\times 100$) [SE] and AUROC [SE] for GEV, Probit, and Logistic methods from the simulation study.}
	\label{rbtbl:simbsresults}
	\centering
	\scriptsize
	\begin{tabular}{c c c ccc c ccc}
		\toprule
		\multicolumn{3}{c}{ }& \multicolumn{3}{c}{BS} && \multicolumn{3}{c}{AUROC}\\
		\cmidrule{4-6} \cmidrule{8-10}
		Setting & $n$ & Sample & GEV & Probit & Logistic && GEV & Probit & Logistic\\
		\midrule
		GEV & 100 & CLU & 3.10 [0.27] & 2.45 [0.19] & 2.79 [0.25]
		&& 0.926 [0.009] & 0.942 [0.009] & 0.900 [0.020]\\
		&     & SRS & 2.92 [0.20] & 2.54 [0.18] & 2.92 [0.25]
		&& 0.938 [0.007] & 0.951 [0.007] & 0.879 [0.021]\\
		& 250 & CLU & 2.18 [0.15] & 1.87 [0.13] & 2.05 [0.14]
		&& 0.951 [0.008] & 0.948 [0.011] & 0.922 [0.017]\\
		&     & SRS & 2.29 [0.15] & 2.06 [0.13] & 2.26 [0.15]
		&& 0.949 [0.009] & 0.949 [0.010] & 0.908 [0.020]\\
		\midrule
		Logistic & 100 & CLU & 5.29 [0.25] & 4.94 [0.23] & 5.10 [0.25]
		&& 0.659 [0.012] & 0.676 [0.014] & 0.643 [0.013]\\
		&     & SRS & 5.32 [0.23] & 5.09 [0.24] & 5.34 [0.26]
		&& 0.690 [0.012] & 0.693 [0.012] & 0.613 [0.012]\\
		& 250 & CLU & 4.81 [0.21] & 4.55 [0.21] & 4.66 [0.22]
		&& 0.731 [0.010] & 0.749 [0.010] & 0.714 [0.014]\\
		&     & SRS & 4.86 [0.22] & 4.63 [0.20] & 5.01 [0.23]
		&& 0.742 [0.010] & 0.760 [0.010] & 0.698 [0.015]\\
		\midrule
		Hotspot & 100 & CLU & 2.29 [0.17] & 2.01 [0.15] & 1.81 [0.12]
		&& 0.841 [0.016] & 0.833 [0.019] & 0.824 [0.020]\\
		&     & SRS & 2.09 [0.13] & 1.87 [0.12] & 2.13 [0.15]
		&& 0.885 [0.015] &  0.906 [0.013] & 0.844 [0.015]\\
		& 250 & CLU & 1.65 [0.11] & 1.25 [0.08] & 1.40 [0.09]
		&& 0.934 [0.009] &  0.949 [0.008] & 0.939 [0.011]\\
		&     & SRS & 1.53 [0.10] & 1.31 [0.08] & 1.63 [0.11]
		&& 0.947 [0.007] &  0.960 [0.005] & 0.918 [0.015]\\
		\bottomrule
	\end{tabular}
\end{table}

%\begin{figure}
%  % code/analysis/simstudy/combine-tables.R
%  \includegraphics[width=\linewidth]{plots/sim-perf-gev}
%  \caption{Vertically averaged ROC curves for GEV simulation setting.}
%  \label{rbfig:simrocgev}
%\end{figure}
%
%\begin{figure}
%  % code/analysis/simstudy/combine-tables.R
%  \includegraphics[width=\linewidth]{plots/sim-perf-probit}
%  \caption{Vertically averaged ROC curves for probit simulation setting.}
%  \label{rbfig:simrocpro}
%\end{figure}
%
%\begin{figure}
%  % code/analysis/simstudy/combine-tables.R
%  \includegraphics[width=\linewidth]{plots/sim-perf-hotspot}
%  \caption{Vertically averaged ROC curves for hotpost simulation setting.}
%  \label{rbfig:simrochot}
%\end{figure}

% \begin{table}
%   \caption{Relative AUC for GEV and Probit methods}
%   \label{rbtbl:simaucresults}
%   \centering
%   \begin{tabular}{r|lll}
%     \cline{2-4}
%               & GEV    & Probit & Logit\\
%     \hline
%     Setting 1 & 0.8998 & 0.8973 & 0.8897\\
%     Setting 2 & 0.9458 & 0.9399 & 0.9356\\
%     Setting 3 & 0.7288 & 0.7371 & 0.7157\\
%     Setting 4 & 0.7906 & 0.8056 & 0.8115\\
%     Setting 5 & 0.8426 & 0.8458 & 0.8388\\
%     Setting 6 & 0.8756 & 0.8686 & 0.8765\\
%     \hline
%   \end{tabular}
% \end{table}

% We analyzed the results for this simulation study using a Friedman test at $\alpha = 0.05$ to see if at least one method had a significantly different Brier score or AUC.
% For any setting that yielded a significant p-value, we conducted a Wilcoxon-Nemenyi-McDonald-Thompson test to see which of the methods had different results.
% The full results for the Wilcoxon-Nemenyi-McDonald-Thompson tests are given in \aref{rba:pdiffs}.
% For all settings, we find significant results for the Friedman test comparing the Brier scores for the methods.
% Specifcally, we see a statistically significant reduction in Brier score using the GEV compared to logit for settings one and two and compared to probit for setting two.
% However, in the other settings, the logit and probit methods tend to perform better than the GEV method.

% The results using AUC are much less conclusive with only settings one and four demonstrating significant differences between the methods at $\alpha = 0.05$.
% As with the Brier scores, the GEV method shows a statistically significant increase in AUC over the logit method for setting one, and for setting four, the both the probit and logit methods show a statistically significant improvement in AUC over the GEV method.

% \fref{rbfig:post-med-2} and \fref{rbfig:post-med-3} show the posterior median of $P(Y =1)$ for settings 2 and 3 respectively of a simulated dataset.
% As you can see on the figures, the both the spatial probit and logistic models oversmooth $P(Y = 1)$, whereas the spatial GEV method is able to capture small pockets of spatial dependence.
% Plots for the medians of settings 1 and 4 look similar, so they are not included here.

% \begin{figure}
%   % this figure comes from markdown/sim-hmc/predict-maps.R
%   \includegraphics[width=\linewidth]{plots/post-med-2.pdf}
%   \caption{Posterior median $P(Y = 1)$ for spatial GEV, probit, and logistic regression for setting 2.}
%   \label{rbfig:post-med-2}
% \end{figure}

% \begin{figure}
%   % this figure comes from markdown/sim-hmc/predict-maps.R
%   \includegraphics[width=\linewidth]{plots/post-med-3.pdf}
%   \caption{Posterior median $P(Y = 1)$ for spatial GEV, probit, and logistic regression for setting 3.}
%   \label{rbfig:post-med-3}
% \end{figure}

% If needed, species 2 is \emph{Hedysarum scoparium} , and \emph{Hedysarum scoparium} can be found in $0.54\%$ of the grid cells.

\section{Data analysis}\label{rbs:dataanalysis}

We compare our method to the spatial probit and logistic models for mapping the probability of the occurrence of \tamarix{} (TR) and \hedysarum{} (HS), two  plant species, for a 1-km$^2$ study region of PR China \citep{Smith2012}.
The Chinese Academy of Forestry conducted a full census of the area, and the true occupancy of the species are plotted in \fref{rbfig:occupancy}.
\begin{figure}
	\centering
	\includegraphics[width=\linewidth, trim=0 10em 0 10em]{plots/plant-census.pdf}
	\caption{True occupancy of \tamarix{} (left) and \hedysarum{} (right) from a 1-km$^2$ study region of PR China.}
	\label{rbfig:occupancy}
\end{figure}
The region is split into $10$-m $\times$ $10$-m grid cells.
\tamarix{} can be found in approximately $6\%$ of the grid cells, and \hedysarum{} can be found in approximately $0.54\%$ of the grid cells.

\subsection{Methods} \label{rbs:datamethods}

For the data analysis, we generate 50 subsamples using the CLU and SRS sampling methods with $n_s = 100$, $250$ initial locations.
For each subsample, we fit the spatial GEV, spatial probit, and spatial logistic models.
Knot placement, prior distributions, and MCMC details for the data analysis are the same as the simulation study.
To compare models, we use similar metrics as in the simulation study, but we average the metrics over subsamples.

\subsection{Results}\label{rbs:dataresults}

The results of the real data analysis mirror those of the simulation study.
\tref{rbtbl:dataresults} gives summary Brier scores ($\times 100$) and AUROC for the \tamarix{} and \hedysarum{} analysis along with the time (in seconds) for 1,000 iterations of the MCMC sampler.
These timings come from a single core of an Intel Core i7-5820K Haswell-E processor, using the OpenBLAS optimized BLAS library (\url{http://www.openblas.net}).
\fref{rbfig:data1roc} gives the vertically averaged ROC curves for each method and sampling setting for \tamarix{} and \fref{rbfig:data2roc} gives the vertically averaged ROC curves for \hedysarum{}.
These results appear to support the suggestion from the simulation study that spatial GEV gives an advantage as rareness increases.
For \tamarix, when $n = 100$, there is a small distinction between the spatial GEV and probit models.
The results suggest that the GEV model has a small improvement over probit in the case of cluster sampling, but that using a probit model demonstrates a small improvement over the GEV model in the simple random setting.
However, when $n = 250$, both logistic and probit models appear to outperform the GEV model.
For the rarer species, \hedysarum, we find more conclusive evidence that the GEV model provides an improvement for cluster sampling when $n = 100$.
At this sample size, there is also some evidence to suggest that the GEV model gives some improvement over the probit model for simple random sampling.
For $n = 250$, we have evidence that using a cluster sampling strategy, the GEV model gives the best performance, but for simple random sampling, the probit model performs the best.

\section{Discussion and future research}\label{rbs:conclusions}

In this paper, we present a max-stable spatial method for rare binary data.
The principal finding in this paper is that the spatial probit model is sufficient for binary data except in the most extreme cases with occurring for less than 1\% of the observations.
This finding is surprising given that the max-stable process is the theoretically justified spatial process for extreme value distributions, and it leads to possible research questions in the future.
Nevertheless, both the probit and GEV models  outperform the logistic method, which is often the default method chosen for analysis of binary data.

It is unusual that the spatial probit model should outperform the proposed model, particularly when the data are generated directly from the proposed model.
One possible explanation is that for the simulated data, there is a wide range of rarity in the data (GEV: $0.5\%$ -- $35.9\%$, Logistic: $1.4\%$ -- $14.4\%$, and Hotspot: $0.5\%$ -- $6.8\%$).
Given that for both the GEV and logistic data settings, we have a number of datasets with a relatively high rate of occurrence, it is possible that probit is competitive partly due to the fact that the data are not rare.
Both the simulation study and data analysis appear to support the idea that the GEV method will perform better on rarer datasets.
Therefore, it may be useful to conduct more research on rare datasets or through simulation with a slightly more restrictive data generation strategy (i.e. restrict datasets to populations that are rarer than $5\%$).

\begin{figure}
	% code/analysis/swd/combine-tables.R
	\includegraphics[width=\linewidth]{plots/data-perf-species1}
	\caption{Vertically averaged ROC curves for \tamarix{}.}
	\label{rbfig:data1roc}
\end{figure}

\begin{figure}
	% code/analysis/swd/combine-tables.R
	\includegraphics[width=\linewidth]{plots/data-perf-species2}
	\caption{Vertically averaged ROC curves for \hedysarum{}.}
	\label{rbfig:data2roc}
\end{figure}

\begin{landscape}
	\thispagestyle{lscapedplain}
	\begin{table}
		\caption{Brier scores ($\times 100$) [SE], AUROC [SE], and time (in seconds) for 1,000 iterations of GEV, Probit, and Logistic methods for \tamarix{} and \hedysarum{}.}
		\label{rbtbl:dataresults}
		\centering
		\scriptsize
		\subfloat[\tamarix{}]{
			\begin{tabular}{c c ccc c ccc c rrr}
				\toprule
				\multicolumn{2}{c}{ }& \multicolumn{3}{c}{BS} && \multicolumn{3}{c}{AUROC} && \multicolumn{3}{c}{Time} \\
				\cmidrule{3-5} \cmidrule{7-9} \cmidrule{11-13}
				$n$ & Samp. & GEV    & Probit & Logistic && GEV    & Probit & Logistic && GEV    & Probit & Logistic \\
				\midrule
				100 & CLU & 5.120 [0.050] & 5.039 [0.049] & 5.382 [0.029]
				&& 0.732 [0.014] & 0.731 [0.014] & 0.699 [0.012]
				&& 6.1 & 1.1 & 2.4\\
				& SRS & 4.997 [0.045] & 4.938 [0.055] & 5.500 [0.027]
				&& 0.798 [0.008] & 0.802 [0.009] & 0.636 [0.012]
				&& 6.2 & 1.1 & 2.6\\
				250 & CLU & 4.779 [0.049] & 4.657 [0.045] & 4.950 [0.051]
				&& 0.771 [0.013] & 0.784 [0.013] & 0.798 [0.011]
				&& 32.0 & 7.1 & 21.2 \\
				& SRS & 4.823 [0.053] & 4.735 [0.048] & 5.120 [0.071]
				&& 0.827 [0.011] & 0.851 [0.007] & 0.717 [0.019]
				&& 32.6 & 7.0 & 21.0 \\
				\bottomrule
			\end{tabular}
		}

		\subfloat[\hedysarum]{
			\begin{tabular}{c c ccc c ccc c rrr}
				\toprule
				\multicolumn{2}{c}{ }& \multicolumn{3}{c}{BS} && \multicolumn{3}{c}{AUROC} && \multicolumn{3}{c}{Time}\\
				\cmidrule{3-5} \cmidrule{7-9} \cmidrule{11-13}
				$n$ & Samp. & GEV    & Probit & Logistic && GEV    & Probit & Logistic && GEV    & Probit & Logistic \\
				\midrule
				100 & CLU & 1.765 [0.018] & 1.831 [0.029] & 1.679 [0.002]
				&& 0.642 [0.010] & 0.617 [0.012] & 0.573 [0.008]
				&& 5.7 & 1.0 & 2.1\\
				& SRS & 1.914 [0.066] & 1.996 [0.083] & 1.685 [0.002] && 0.686 [0.009] & 0.683 [0.011] & 0.587 [0.008]
				&& 5.7 & 1.0 & 2.2\\
				250 & CLU & 1.667 [0.005] & 1.657 [0.006] & 1.679 [0.001]
				&& 0.659 [0.009] & 0.648 [0.011] & 0.566 [0.005]
				&& 27.6 & 5.9 & 17.6 \\
				& SRS & 1.691 [0.017] & 1.666 [0.010] & 1.684 [0.001] && 0.691 [0.010] & 0.709 [0.012] & 0.574 [0.007]
				&& 27.9 & 5.9 & 18.1 \\
				\bottomrule
			\end{tabular}
		}
	\end{table}
\end{landscape}


\documentclass[10pt, a4paper, twoside]{basestyle}

\usepackage[backend=biber,firstinits=true,maxnames=100,style=alphabetic,maxalphanames=4,doi=true,isbn=false,url=false,eprint=true]{biblatex}
\bibliography{bibliography}

\usepackage[Mathematics]{semtex}
\usepackage{chngcntr}
\counterwithout{equation}{section}

%%%% Shorthands.

\newcommand{\p}{\AssociatedLegendreFunctionAstro}

%%%% Title and authors.

\title{%
\textdisplay{%
Geopotential%
}%
}
\author{Pascal~Leroy (phl) and Robin~Leroy (egg)}
\begin{document}
\maketitle
\begin{sloppypar}
\noindent
This document describes the computations that are performed by the method \texttt{GeneralSphericalHarmonicsAcceleration} of class \texttt{Geopotential} to determine the acceleration exerted by a non-spherical celestial on a point mass.
\end{sloppypar}

\subsection*{Notation}
Let $\vr$ be the vector going from the centre of the celestial to the point mass.  Let $\tuple{\hat{\vx}, \hat{\vy}, \hat{\vz}}$ be a (direct) base whose $\hat{\vz}$ axis is along the axis of rotation of the celestial and whose $\hat{\vx}$ axis points toward a reference point on the celestial.  In this base $\vr$ has coordinates $\tuple{x, y, z}$ which can be expressed in terms of the latitude $\gb \in \intclos{\frac{\Pi}{2}}{\frac{\Pi}{2}}$ and the longitude
$\gl \in \intclos{0}{2 \Pi}$:
\[
\vr=\begin{pmatrix}
x \\ y \\ z
\end{pmatrix}
=\begin{pmatrix}
r \cos \gb \cos \gl\\
r \cos \gb \sin \gl\\
r \sin \gb
\end{pmatrix}
\]
where $r$ is the norm of $\vr$.  Note that $\cos \gb > 0$, which will come handy when simplifying expressions like $\sqrt{1 - \sin^2 \gb}$.

\subsection*{Potential and acceleration}
The gravitational potential due to the celestial has the form\cite{IERSConventions2010}:
\[
U\of\vr = -\frac{\gm}{r}\pa{1 + \sum{n = 1}[\infty] \sum{m = 0}[n]
\pa{\frac{R}{r}}^n \p{n}{m}\of{\sin \gb}
\pa{C_{n m} \cos m \gl + S_{n m} \sin m \gl}
}
\]
where $\p{n}{m}$ is the associated Legendre function defined as\cite[appendix]{RiesBettadpurEanesKangKoMcCulloughNagelPiePooleRichterSaveTapley2016}:
\[
\p{n}{m} = \pa{1 - t^2}^{\frac{m}{2}} \deriv[m] t {\LegendrePolynomial n\of t}
\]
The terms that are relevant for evaluating the acceleration exerted by the spherical harmonics on the point mass have the form:
\[
V_{n m}\of\vr = \frac{1}{r}\pa{\frac{R}{r}}^n \p{n}{m}\of{\sin \gb}
\pa{C_{n m} \cos m \gl + S_{n m} \sin m \gl}
\]
and the acceleration itself is proportional to $\grad V_{n m}\of\vr$.

$V_{n m}\of\vr$ can be written as the product of a radial term, a latitudinal term and a longitudinal term, dependent of $r$, $\gb$, and $\gl$, respectively:
\[
V_{n m}\of\vr = {\mathfrak R\of r}\,{\mathfrak B\of \gb}\,{\mathfrak L\of \gl}
\]
where:
\[
\begin{dcases}
\mathfrak R\of r &= \frac{1}{r}\pa{\frac{R}{r}}^n\\
\mathfrak B\of \gb &= \p{n}{m}\of{\sin \gb}\\
\mathfrak L\of \gl &= C_{n m} \cos m \gl + S_{n m} \sin m \gl
\end{dcases}
\]
The overall gradient can then be written as:
\[
\grad V_{n m}\of\vr =
{\grad \mathfrak R\of r}\,{\mathfrak B\of \gb}\,{\mathfrak L\of \gl} +
{\mathfrak R\of r}\,{\grad \mathfrak B\of \gb}\,{\mathfrak L\of \gl} +
{\mathfrak R\of r}\,{\mathfrak B\of \gb}\,{\grad \mathfrak L\of \gl}
\]
\subsection*{Lemmata}
To compute the acceleration, we first determine the gradient of various elements appearing in the potential.  The following lemma determines the gradient of the $\mathfrak R$ term:
\begin{lemma}
\[
\grad r^n = n r^{n - 2} \vr\text.
\]
\begin{proof}
We have trivially:
\[
\grad r = \grad \sqrt{x^2 + y^2 + z^2} = \frac{\vr}{r}
\]
from which we deduce:
\[
\grad r^n = n r^{n - 1} \grad r = n r^{n - 2} \vr
\]
\end{proof}
\end{lemma}
The following lemma is useful for computing the gradient of the $\mathfrak B$ term:
\begin{lemma}
\[
\grad \sin \gb =\frac{\cos \gb}{r}\begin{pmatrix}
-\sin \gb \cos \gl \\
-\sin \gb \sin \gl \\
\cos \gb
\end{pmatrix}\text.
\]
\begin{proof}
Noting that $\grad z = \hat{\vz}$ we have:
\[
\grad \sin \gb = \grad \frac{z}{r}
= \frac{r \hat{\vz} - z r^{-1} \vr}{r^2} = \frac{r^2 \hat{\vz} - z \vr}{r^3}
\]
which can be written, in coordinates:
\[
\grad \sin \gb =\frac{1}{r^3}\begin{pmatrix}
-x z \\ -y z \\ x^2 + y^2
\end{pmatrix}
=\frac{1}{r^3}\begin{pmatrix}
-r^2 \sin \gb \cos \gb \cos \gl \\
-r^2 \sin \gb \cos \gb \sin \gl \\
r^2 \cos^2 \gb
\end{pmatrix}
=\frac{\cos \gb}{r}\begin{pmatrix}
-\sin \gb \cos \gl \\
-\sin \gb \sin \gl \\
\cos \gb
\end{pmatrix}
\]
\end{proof}
\end{lemma}
For the $\mathfrak L$ term, we will need to evaluate the quantities:
\[
\begin{cases}
\grad \cos m \gl &= -m \sin m \gl \, \grad \gl \\
\grad \sin m \gl &= m \cos m \gl \, \grad \gl
\end{cases}
\]
The following lemma helps with that computation:
\begin{lemma}
\[
\grad \gl = \frac{1}{r \cos \gb}\begin{pmatrix}
-\sin \gl \\
\cos \gl \\
0
\end{pmatrix}\text.
\]
\begin{proof}
The angle $\gl$ is $\arctan\frac{y}{x}$ thus:
\[
\grad \gl = \frac{1}{1 + \pa{\frac{y}{x}}^2} \grad\of{\frac{y}{x}}
= \frac{1}{1 + \pa{\frac{y}{x}}^2}\frac{x \hat{\vy} - y \hat{\vx}}{x^2}
= \frac{x \hat{\vy} - y \hat{\vx}}{x^2 + y^2}
\]
which can be written, in coordinates:
\[
\grad \gl = \frac{1}{x^2 + y^2}\begin{pmatrix}
-y \\
x \\
0
\end{pmatrix}
= \frac{1}{r^2 \cos^2 \gb}\begin{pmatrix}
-r \cos \gb \sin \gl \\
r \cos \gb \cos \gl \\
0
\end{pmatrix}
= \frac{1}{r \cos \gb}\begin{pmatrix}
-\sin \gl \\
\cos \gl \\
0
\end{pmatrix}
\]
\end{proof}
\end{lemma}
Finally, we will need the gradient of the associated Legendre polynomial, given by the following lemma:
\begin{lemma}
\[
\p{n}{m}'\of{t}
= \pa{1 - t^2}^{\frac{m}{2}}\deriv[m + 1]{t}{\LegendrePolynomial{n}\of t} -
m t \pa{1 - t^2}^{\frac{m - 2}{2}} \deriv[m]{t}{\LegendrePolynomial{n}\of t}\text.
\]
\begin{proof}
This follows trivially from the definition:
\[
\p{n}{m}'\of{t} = \frac{m}{2} \pa{1 - t^2}^{\frac{m}{2} - 1}
\pa{-2 t} \deriv[m]{t}{\LegendrePolynomial{n}\of t} +
\pa{1 - t^2}^{\frac{m}{2}} \deriv[m + 1]{t}{\LegendrePolynomial{n}\of t}
\]
\end{proof}
\end{lemma}
\subsection*{Gradients}
We can now compute the gradient of the three terms that make up $V_{n m}\of\vr$.  First, the radial term:
\[
\grad \mathfrak R\of r = R^n \grad r^{-\pa{n + 1}} = -\pa{n + 1} R^n r^{-\pa{n + 3}} \vr
=-\pa{n + 1}\frac{\mathfrak R\of r}{r^2} \vr
\]
For the latitudinal term, the chain rule yields:
\[
\grad \mathfrak B\of \gb = \p{n}{m}\of{\sin \gb} \, \grad \sin \gb
= \p{n}{m}'\of{\sin \gb} \frac{\cos \gb}{r}\begin{pmatrix}
-\sin \gb \cos \gl \\
-\sin \gb \sin \gl \\
\cos \gb
\end{pmatrix}
\]
Substituting $\sin \gb$ for the argument of $\p{n}{m}$ and its derivative we obtain:
\[
\begin{dcases}
\p{n}{m}\of{\sin \gb} = {\cos^m \gb}
\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb} \\
\p{n}{m}'\of{\sin \gb}
= {\cos^m \gb} \Evaluate{\deriv[m + 1]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb} -
m \sin \gb \pa{\cos \gb}^{m - 2}
\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb}
\end{dcases}
\]
and thus:
\[
\begin{dcases}
\mathfrak B\of \gb = {\cos^m \gb}
\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb} \\
\grad \mathfrak B\of \gb = \frac{1}{r}
\pa{\pa{\cos \gb}^{m + 1}
\Evaluate{\deriv[m + 1]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb} -
m \sin \gb \pa{\cos \gb}^{m - 1}
\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb}\qquad\ \,}
\begin{pmatrix}
-\sin \gb \cos \gl \\
-\sin \gb \sin \gl \\
\cos \gb
\end{pmatrix}
\end{dcases}
\]
For the longitudinal term we have:
\[
\grad \mathfrak L\of \gl = \pa{-m C_{n m} \sin m \gl + m S_{n m} \cos m \gl} \grad \gl
= \frac{m}{r \cos \gb} \pa{S_{n m} \cos m \gl - C_{n m} \sin m \gl} \begin{pmatrix}
-\sin \gl \\
\cos \gl \\
0
\end{pmatrix}
\]
\subsection*{Singularities}
While the above formulæ are all we need to compute the gradient of the geopotential, they present a number of singularities that require some care for implementation purposes.

There is obviously a pole when $r = 0$.  This one is not very interesting as we are never going to compute the acceleration at the centre of the celestial.

There are however removable singularities that arise when $\cos \gb$ appears at the denominator: any point on the axis of rotation of the celestial has $\cos \gb = 0$, but clearly the acceleration there is finite.

Consider $\grad \mathfrak B\of \gb$.  When $m = 0$ it includes a term in $\pa{\cos \gb}^{-1}$.  However, that term is multiplied by $m$, so it vanishes.  Thus, for $m = 0$ we must use the special formula:
\[
{\grad \mathfrak B\of \gb}_{m = 0} =  \frac{\cos \gb}{r}
\Evaluate{\deriv{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb}\qquad\begin{pmatrix}
-\sin \gb \cos \gl \\
-\sin \gb \sin \gl \\
\cos \gb
\end{pmatrix}
\]
Similarly $\grad \mathfrak L\of \gl$ has $\cos \gb$ as its denominator.  To eliminate this term we note that $\grad \mathfrak L\of \gl$ always occurs in a product involving $\mathfrak B\of \gb$.  Let's write this product:
\[
{\mathfrak B\of \gb}{\grad \mathfrak L\of \gl} = \frac{m \pa{\cos \gb}^{m - 1}}{r}
\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb}
\pa{S_{n m} \cos m \gl - C_{n m} \sin m \gl}\begin{pmatrix}
-\sin \gl \\
\cos \gl \\
0
\end{pmatrix}
\]
When $m = 0$ this expression has a term in $\pa{\cos \gb}^{-1}$ so we need to special-case it:
\[
{\mathfrak B\of \gb}{\grad \mathfrak L\of \gl}_{m = 0} = 0
\]
\subsection*{Implementation considerations}
A naïve implementation of the preceding formulæ yields poor performance because of the computation of trigonometric functions, powers and polynomials.  It is advantageous
to use simple geometric identities and recurrence formulæ to avoid these computations, at the expense of a small amount of storage.  First, we note that:
\[
x^2 + y^2 = r^2 \cos^2 \gb
\]
and therefore we obtain the trigonometric functions for $\gb$:
\[
\begin{dcases}
\cos \gb = \frac{\sqrt{x^2 + y^2}}{r} \\
\sin \gb = \frac{z}{r}
\end{dcases}
\]
and for $\gl$:
\[
\begin{dcases}
\cos \gl = \frac{x}{\sqrt{x^2 + y^2}} \\
\sin \gl = \frac{y}{\sqrt{x^2 + y^2}}
\end{dcases}
\]
In the case where $\sqrt{x^2 + y^2}$ is $0$, any value will do for $\gl$, so we can for instance pick $\cos \gl = 1$ and $\sin \gl = 0$.

The value of $\cos^m \gb$ may be computed by recurrence with a relative error growing like $\BigO\of m$.  If $m$ is even, we have:
\[
\begin{dcases}
h = \frac m 2 \\
\cos^m \gb = \pa{\cos^h \gb}^2
\end{dcases}
\]
and if $m$ is odd:
\[
\begin{dcases}
h_1 = \Floor{\frac m 2} \\
h_2 = m - h_1 \\
\cos^m \gb = \pa{\cos^{h_1} \gb}\pa{\cos^{h_2} \gb}
\end{dcases}
\]
The value of $\cos m \gl$ and $\sin m \gl$ may similarly be computed using elementary trigonometric identities with an absolute error growing like $\BigO\of {m^2}$.  If $m$ is even, we have:
\[
\begin{dcases}
h = \frac m 2 \\
\cos m \gl = \pa{\cos h \gl + \sin h \gl}\pa{\cos h \gl - \sin h \gl} \\
\sin m \gl = 2 \sin h \gl \cos h \gl
\end{dcases}
\]
and if $m$ is odd:
\[
\begin{dcases}
h_1 = \Floor{\frac m 2} \\
h_2 = m - h_1 \\
\sin m \gl = \sin h_1 \gl \cos h_2 \gl + \cos h_1 \gl \sin h_2 \gl \\
\cos m \gl = \cos h_1 \gl \cos h_2 \gl - \sin h_1 \gl \sin h_2 \gl
\end{dcases}
\]
For the Legendre polynomials, we are looking for a recurrence relationship among terms of the form $\deriv[m]{t}{\LegendrePolynomial{n}\of t}$.  First note that these terms are $0$ when $m > n$.  We start with the recurrence defining the polynomials themselves:
\[
n \LegendrePolynomial{n}\of t = \pa{2 n - 1} t \LegendrePolynomial{n - 1}\of t - \pa{n - 1} \LegendrePolynomial{n - 2}\of t
\]
and we derive $m$ times:
\[
n \deriv[m]{t}{\LegendrePolynomial{n}\of t} = \pa{2 n - 1} \deriv[m]{t}{\pa{t \LegendrePolynomial{n - 1}\of t}} - \pa{n - 1} \deriv[m]{t}{\LegendrePolynomial{n - 2}\of t}
\]
It's easy to verify, by recurrence, that:
\[
\deriv[m]{t}{\pa{t \LegendrePolynomial{n}\of t}} = t \deriv[m]{t}{\LegendrePolynomial{n}\of t} + m \deriv[m - 1]{t}{\LegendrePolynomial{n}\of t}
\]
so we end up with:
\[
n \deriv[m]{t}{\LegendrePolynomial{n}\of t} =
\pa{2 n - 1}\pa{t \deriv[m]{t}{\LegendrePolynomial{n - 1}\of t} + m \deriv[m - 1]{t}{\LegendrePolynomial{n - 1}\of t}} -
\pa{n - 1} \deriv[m]{t}{\LegendrePolynomial{n - 2}\of t}
\]
This recurrence starts with ${\LegendrePolynomial{0}\of t} = 1$, ${\LegendrePolynomial{1}\of t} = t$ and ${\deriv{t}{\LegendrePolynomial{1}\of t}} = 1$.  It is similar to \cite[eqn. (12)]{Westra2017} and makes it possible to efficiently evaluate $\Evaluate{\deriv[m]{t}{\LegendrePolynomial{n}\of t}}{t = \sin \gb}$ for increasing values of $n$ and $m$.

\section*{Damping}
In order to reduce the computational cost, we ignore high degree and order terms at long range.
This is done by replacing $\mathfrak R\of{r}$ by $\gs\of r \mathfrak R\of r$ in $V_{nm}$, where
$\gs\of r = 0$ for all $r>s_1$, and $\gs\of r = 1$ for all $r<s_0$, so that the $V_{nm}$ term
vanishes at long range and is unaffected at short range.

In the force computations, this means that $\grad\mathfrak R$ is replaced by\[
\grad \pa{\gs\mathfrak R} = \pa{\gs'\mathfrak R + \gs\mathfrak R'} \frac \vr r\text.
\]

\subsection*{Sigmoid}
Since we want the force to remain continuous, we need $\gs$ to be $\Cont[1]$; we achieve this by
making $\gs$ the cubic Hermite interpolation on the non-constant interval $\intclos {s_0} {s_1}$,
\[
\gs\of r=
\begin{cases}
1 &\text{for }r \leq s_0\text, \\
\frac{\pa{r-s_1}^2\pa{2r-3s_0+s_1}}{\pa{s_1-s_0}^3} &\text{for }r \in \intclos {s_0} {s_1}\text, \\
0 &\text{for }r \geq s_1\text.
\end{cases}
\]
The radial component of the acceleration resulting from $V_{nm}$ is
\[
\gm\InnerProduct {\grad V_{nm}}{\frac \vr r} = \gm \deriv r {{\gs\of r \mathfrak R\of r}}{\mathfrak B\of \gb}\,{\mathfrak L\of \gl}\text.
\]
We require that it and all its derivatives be monotone, i.e., that
\[
\forall k \in \Positives, \deriv[k+1] r {\gs\of r \mathfrak R\of r}{\mathfrak B\of \gb}\,{\mathfrak L\of \gl}
\]
have constant sign for $r \in \intclos {s_0} {s_1}$.

Removing those factors that do not depend on $r$, this means that we must have
\begin{equation}
\forall r \in \intopen{s_0} {s_1}, \deriv[k+1] r {\gs\of r r^{-\pa{n+1}}} \neq 0\label{eqnderiv}\text.
\end{equation}

\begin{proposition}
The smallest value of $s_1$ such that (\ref{eqnderiv}) holds for all $k>0$ and $n≥2$ is $s_1 = 3s_0$.
\begin{proof}
We start by writing:
\[
\gs\of{r} = \sum{m = 0}[3] a_m r^m
\]
where:
\[
\begin{dcases}
a_0 &= \frac{\pa{s_1-3s_0}{s_1}^2}{\pa{s_1-s_0}^3} \\
a_1 &= \frac{6s_0 s_1}{\pa{s_1-s_0}^3} \\
a_2 &= \frac{-3\pa{s_0+s_1}}{\pa{s_1-s_0}^3} \\
a_3 &= \frac{2}{\pa{s_1-s_0}^3}
\end{dcases}
\]
The function being derived in (\ref{eqnderiv}) is:
\[
\gs\of{r} r^{-\pa{n+1}} = \sum{m = 0}[3] a_m r^{-\pa{n-m+1}}
\]
The p-th derivative of $r^{-q}$ for $q > 0$ is:
\[
\deriv[p] r {r^{-q}} = \pa{-1}^p \frac{\Factorial{\pa{q+p-1}}}{\Factorial{\pa{q-1}}} r^{-\pa{q+p}}
\]
Whence:
\begin{align*}
\deriv[k+1] r {\gs\of r r^{-\pa{n+1}}} &= \pa{-1}^{\pa{k+1}} \sum{m = 0}[3] a_m \frac{\Factorial{\pa{n+k-m+1}}}{\Factorial{\pa{n-m}}} r^{-\pa{n+k-m+2}} \\
&= \pa{-1}^{\pa{k+1}} r^{-\pa{n+k+2}} \frac{\Factorial{\pa{n+k-2}}}{\Factorial{n}}
\begin{aligned}[t]
&\;\Bigl[a_0 \pa{n+k+1}\pa{n+k}\pa{n+k-1} \\
&+a_1 \pa{n+k}\pa{n+k-1} n r \\
&+a_2 \pa{n+k-1} n \pa{n-1} r^2 \\
&+a_3 n \pa{n-1}\pa{n-2} r^3\Bigr]
\end{aligned}
\end{align*}
Substituting the values of the $\pa{a_i}$, we see that the zeroes of $\deriv[k+1] r {\gs\of r r^{-\pa{n+1}}}$ are those of the polynomial:
\begin{align*}
d_{n k}\of{s_0, s_1; r} &= \pa{s_1-3s_0}{s_1}^2 \pa{n+k+1}\pa{n+k}\pa{n+k-1} \\
&\quad + 6s_0 s_1 \pa{n+k}\pa{n+k-1} n r \\
&\quad -3\pa{s_0+s_1}\pa{n+k-1} n \pa{n-1} r^2 \\
&\quad + 2 n \pa{n-1}\pa{n-2} r^3
\end{align*}
The following three lemmata establish properties of the roots of $d_{n k}\of{s_0, s_1; r}$:
\begin{lemma}[necessary condition, general case]
If $\frac{9}{7}s_0<s_1<3s_0$ then there exist $k$ and $n$ such that $d_{n k}\of{s_0, s_1; r}$ has a zero in $\intopen{s_0} {s_1}$.
\end{lemma}
\begin{lemma}[necessary condition, special case]
If $s_0<s_1<\pa{1+\frac{\sqrt{2}}{2}}s_0$ then there exist $k$ and $n$ such that $d_{n k}\of{s_0, s_1; r}$ has a zero in $\intopen{s_0} {s_1}$.
\end{lemma}
\begin{lemma}[sufficient condition]
If $s_1 = 3s_0$ then the positive roots of $d_{n k}\of{s_0, s_1; r}$ are greater than $s_1$.
\end{lemma}
These lemmata trivially constitute necessary and sufficient conditions, respectively, for the proposition.
\end{proof}
\end{proposition}
\begin{proof}[of necessary condition, general case]
In this proof we set $n=2$, which causes the term in $r^3$ of $d_{n k}\of{s_0, s_1; r}$ to vanish:
\begin{multline*}
d_{2,k}\of{s_0, s_1; r} = \pa{s_1-3s_0}s_1^2\pa{k+3}\pa{k+2}\pa{k+1} \\+ 12s_0 s_1\pa{k+2}\pa{k+1}r - 6\pa{s_0+s_1}\pa{k+1}r^2
\end{multline*}
The roots of $d_{2,k}\of{s_0, s_1; r}$ can be computed by solving:
\[
\pa{s_1-3s_0}s_1^2\pa{k+3}\pa{k+2} + 12s_0 s_1\pa{k+2}r - 6\pa{s_0+s_1}r^2 = 0
\]
The reduced discriminant of this equation is:
\begin{align*}
\gD' &= 36 s_0^2 s_1^2 \pa{k+2}^2 + 6\pa{s_0+s_1}\pa{s_1-3s_0}s_1^2\pa{k+2}\pa{k+3}\\
&= 6 s_1^2\pa{k+2}\pa{6 s_0^2\pa{k+2}+\pa{s_1^2-2 s_0 s_1-3s_0^2}\pa{k+3}}\\
&= 6 s_1^2\pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2}
\end{align*}
and its solutions are:
\[
r_± = s_1\frac{6 s_0\pa{k+2} ± \sqrt{6\pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2}}}{6\pa{s_0+s_1}}
\]
We will now define $k_1$ and $k_2$ such that the following claim holds:
\begin{claim}
For every $k \in \intopen{k_2}{k_1}\Intersection\intclop{2}{+\infty}\Intersection{\Naturals}$, $r_- \in \intopen{s_0}{s_1}$.
\begin{proof}
First, consider the inequality $r_-<s_1$.  It can be rewritten as follows:
\begin{align*}
&6 s_0\pa{k+2} - 6\pa{s_0+s_1} < \sqrt{6\pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2}}
\end{align*}
If $k≥2$ the left-hand side is positive and we can further rewrite the inequality:
\begin{align*}
&6\pa{k s_0 + \pa{s_0-s_1}}^2 < \pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2} \\
\iff &6\pa{k^2 s_0^2 + 2 k s_0 \pa{s_0 - s_1} + \pa{s_0 - s_1}^2} < \\
&\qquad k^2 \pa{s_1^2-2 s_0 s_1 + 3s_0^2} + k \pa{3\pa{s_1-s_0}^2 + 2\pa{s_1^2-2 s_0 s_1 + 3s_0^2}} + 6\pa{s_0 - s_1}^2 \\
\iff &k^2\pa{3s_0^2 + 2 s_0 s_1 - s_1^2} + k\pa{12 s_0\pa{s_0-s_1} - 3\pa{s_1-s_0}^2 - 2\pa{s_1^2-2 s_0 s_1 + 3s_0^2}} < 0 \\
\iff &k\pa{3s_0^2 + 2 s_0 s_1 - s_1^2} + \pa{3s_0^2 -2 s_0 s_1 - 5 s_1^2} < 0 \\
\iff &k\pa{3s_0-s_1}\pa{s_0+s_1} + \pa{3s_0 - 5s_1}\pa{s_0+s_1} < 0 \\
\iff &k < \frac{5s_1-3s_0}{3s_0-s_1}=:k_1
\end{align*}
Similarly, the inequality $s_0<r_-$ can be rewritten as follows:
\begin{align*}
&6 s_0 s_1\pa{k+2} - 6\pa{s_0+s_1}s_0 > s_1 \sqrt{6\pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2}} \\
\iff &6 s_0^2\pa{k s_1 + \pa{s_1-s_0}}^2 > s_1^2\pa{k+2}\pa{k\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + 3\pa{s_1-s_0}^2} \\
\iff &6 s_0^2\pa{k^2 s_1^2 + 2 k s_1\pa{s_1-s_0} + \pa{s_1-s_0}^2} > \\
&\qquad k^2 s_1^2\pa{s_1^2-2 s_0 s_1 + 3s_0^2} + k s_1^2\pa{3\pa{s_1-s_0}^2 + 2\pa{s_1^2-2 s_0 s_1 + 3s_0^2}} + 6 s_1^2\pa{s_0 - s_1}^2 \\
\iff &k^2 s_1^2\pa{3s_0^2 + 2 s_0 s_1 - s_1^2} + k s_1\pa{-5s_1^3 + 10 s_0 s_1^2 + 3 s_0^2 s_1 - 12 s_0^3} + 6\pa{s_0-s_1}^3\pa{s_0+s_1} > 0 \\
\iff &k^2 s_1^2\pa{3s_0-s_1}\pa{s_0+s_1} + k s_1\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2}\pa{s_0+s_1} + 6\pa{s_0-s_1}^3\pa{s_0+s_1} > 0 \\
\iff &k^2 s_1^2\pa{3s_0-s_1} + k s_1\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2} + 6\pa{s_0-s_1}^3 > 0
\end{align*}
When $s_1<3s_0$, the coefficient of $k^2$ in the polynomial above is positive.  The constant term is negative, therefore the product of the roots is negative.  For the inequality to be true, $k$ must be greater than the largest root the polynomial.  The discriminant of the polynomial is:
\[
\gD = s_1^2\pa{\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2}^2 - 24\pa{3s_0-s_1}\pa{s_0-s_1}^3}
\]
And the largest root is:
\[
k_2 = \frac{-s_1\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2} + \sqrt{\gD}}{2s_1^2\pa{3s_0-s_1}}
\]
\end{proof}
\end{claim}
\begin{claim}
$\intopen{k_2}{k_1}\Intersection\intclop{2}{+\infty}\Intersection{\Naturals}$ is nonempty when $s_1>\frac{9}{7}s_0 \approx 1.286s_0$.
\begin{proof}
For this set to be nonempty we must have $k_1>2$ which is equivalent to $s_1>\frac{9}{7}s_0$.  Given $k_1>2$, a sufficient condition for the interval $\intopen{k_2}{k_1}$ to contain an integer greater than $1$ is $k_1-k_2>1$.  This inequality can be rewritten as follows:
\begin{align*}
&2s_1^2\pa{5s_1-3s_0} + s_1\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2} - 2s_1^2\pa{3s_0-s_1} - \sqrt{\gD} > 0 \\
\iff &s_1\pa{7s_1^2 + 3 s_0 s_1 - 12s_0^2} > \sqrt{\gD}
\end{align*}
This inequality can only be true if the left-hand side is positive, that is, if $s_1 ≥ \frac{\sqrt{345}-3}{14}s_0 \approx 1.112s_0$.  Assuming that this condition is met we can further rewrite the inequality:
\begin{align*}
&\pa{7s_1^2 + 3 s_0 s_1 - 12s_0^2}^2 > \pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2}^2 - 24\pa{3s_0-s_1}\pa{s_0-s_1}^3 \\
\iff &49s_1^4 + 42 s_0 s_1^3 - 159 s_0^2 s_1^2 - 72 s_0^3 s_1 + 144 s_0^4 > \\
&\qquad s_1^4 - 6 s_0 s_1^3 + 57 s_0^2 s_1^2 - 120 s_0^3 s_1 + 72 s_0^4 \\
\iff &24\pa{s_1-s_0}\pa{2s_1^3 + 4 s_0 s_1^2 - 5 s_0^2 s_1 - 3s_0^3} > 0
\end{align*}
The sum of the roots of $2s_1^3 + 4 s_0 s_1^2 - 5 s_0^2 s_1 - 3s_0^3$ is $-2s_0$ and the product is $3s_0/2$.  This means that this polynomial has a single positive root in $s_1$ and that when $s_1$ is greater than this root the inequality above holds.  Solving the cubic using $\emph{Mathematica}$ yields this root:
\begin{align*}
\hat\gq &= \frac{1}{3} \arctan\of{\frac{3 \sqrt{2517}}{41}} \\
\hat{s}_1 &= \frac{s_0}{6}\pa{-4+\sqrt{138}\sin\hat\gq + \sqrt{46}\cos\hat\gq} \approx 1.183s_0
\end{align*}
Noting that $\hat{s}_1<\frac{9}{7}s_0$, we conclude that, if $s_1 \in \intopen{\frac{9}{7}s_0}{3s_0}$ it is possible to find an integer $k≥2$ such that the smallest root of $d_{2,k}\of{s_0, s_1; r}$ is in $\intopen{s_0} {s_1}$.
\end{proof}
\end{claim}
\end{proof}

\begin{proof}[of necessary condition, special case]
In this proof we set $n=2$ and $k=1$.
\begin{claim}
If $s_1<2s_0$, then for every $k \in \intopen{k_2}{k_1}\Intersection{\Positives}$, $r_- \in \intopen{s_0}{s_1}$.
\begin{proof}
The inequality $r_-<s_1$ can be rewritten as in the previous proof, with the caveat that since the condition $k≥2$ does not hold anymore, we have to require $s_1<2s_0$ for the quantity $s_0\pa{k+2}-\pa{s_0+s_1}$ to be positive.  This is acceptable since we are looking for a proof when $s_1$ is close to $s_0$.  The inequality $s_0<r_-$ is unaffected by any condition on $k$ so the definition of $k_2$ remains valid.
\end{proof}
\end{claim}
\begin{claim}
$1 \in \intopen{k_2}{k_1}$ when $s_1<\pa{1+\frac{\sqrt{2}}{2}}s_0 \approx 1.707s_0$.
\begin{proof}
The inequality $1<k_1$ can be rewritten:
\[
1 < \frac{5s_1-3s_0}{3s_0-s_1} \iff s_0 > s_1 \qquad\text{which is trivially true.}
\]
The inequality $s_0<r_-$ can be rewritten as in the previous proof and becomes, substituting $k$:
\begin{align*}
&k^2 s_1^2\pa{3s_0-s_1} + k s_1\pa{-5s_1^2 + 15 s_0 s_1 - 12s_0^2} + 6\pa{s_0-s_1}^3 > 0 \\
\iff &s_1\pa{-6s_1^2 + 18 s_0 s_1 - 12 s_0^2} + 6\pa{s_0-s_1}^3 > 0 \\
\iff &-2s_1^3 + 6 s_0 s_1^2 - 5 s_0^2 s_1 + s_0^3 > 0 \\
\iff &\pa{s_1 - s_0}\pa{-2 s_1^2 + 4 s_0 s_1 - s_0^2} > 0
\end{align*}
The quadratic term in the above condition is positive between its roots, and its only root greater than $s_0$ is $\pa{1+\frac{\sqrt{2}}{2}}s_0$.  Thus, if $s_1 \in \intopen{s_0}{\pa{1+\frac{\sqrt{2}}{2}}s_0}$ the smallest root of $d_{2,1}\of{s_0, s_1; r}$ is in $\intopen{s_0} {s_1}$.
\end{proof}
\end{claim}
\end{proof}

\begin{proof}[of sufficient condition]
With the choice $s_1=3s_0$ the constant term of the polynomial $d_{n k}\of{s_0, s_1; r}$ vanishes and $r=0$ is a trivial root.  We are left with the equation:
\[
6s_0 s_1 \pa{n+k}\pa{n+k-1} - 3\pa{s_0+s_1}\pa{n+k-1} \pa{n-1} r + 2 \pa{n-1}\pa{n-2} r^2 = 0
\]
or, substituting $s_1$:
\begin{equation}
9{s_0}^2 \pa{n+k}\pa{n+k-1} - 6 s_0 \pa{n+k-1} \pa{n-1} r + \pa{n-1}\pa{n-2} r^2 = 0 \label{eqn3s0}
\end{equation}
The reduced discriminant of equation (\ref{eqn3s0}) is:
\begin{align*}
\gD' &= 9 {s_0}^2 \pa{n+k-1}^2 \pa{n-1}^2 - 9 {s_0}^2 \pa{n+k}\pa{n+k-1}\pa{n-1}\pa{n-2}\\
&=9 {s_0}^2\pa{n+k-1}\pa{n-1}\pa{k+1}
\end{align*}
and its solutions are, when $n>2$:
\[
r_± = 3 s_0\frac{\pa{n+k-1}\pa{n-1} ± \sqrt{\pa{n+k-1}\pa{n-1}\pa{k+1}}}{\pa{n-1}\pa{n-2}}
\]
We want to prove that the smallest root, $r_-$ verifies $r_{-}>3s_0$.  This inequality can be rewritten:
\begin{align*}
&\pa{n+k-1}\pa{n-1} - \pa{n-1}\pa{n-2} > \sqrt{\pa{n+k-1}\pa{n-1}\pa{k+1}} \\
\iff &\pa{n-1}\pa{k+1} > \sqrt{\pa{n+k-1}\pa{n-1}\pa{k+1}} \\
\iff &\pa{n-1}\pa{k+1} > n+k-1 \\
\iff &k\pa{n-2} > 0 \quad \text{which is trivally true.}
\end{align*}
When $n=2$ equation (\ref{eqn3s0}) further reduces to a first-degree equation with root $r = \frac{3}{2} s_0 \pa{k+2}$ which verifies $r≥3s_0$.  Note however that the latter inequality is not strict, i.e., the value $3s_0$ may be reached if $k=0$ and $n=2$.
\end{proof}
Accordingly, we pick $s_1=3s_0$.
The expression for $\gs$ on $\intclos {s_0} {s_1}$ simplifies to\[
\gs\of r = \frac{r \pa{r-3s_0}^2} {4 s_0^3} = \frac{r^3}{4 s_0^3} - \frac{3 r^2}{2s_0^2} + \frac{9r}{4s_0}\text.
\]

\subsection*{Threshold}
For a tolerance $\gce$, we choose $s_0$ as the smallest distance such that the radial component of the force at $s_0$
does not exceed $\gce$ times the magnitude of the central force,
\[
\max_{\gl,\gb} \abs{s_0^2\mathfrak R' \of {s_0} \mathfrak B \of \gb \mathfrak L\of\gl}
= \gce\text.
\]
This maximum decomposes to
\[
\max_{\gl,\gb} \abs{s_0^2\mathfrak R' \of {s_0} \mathfrak B \of \gb \mathfrak L\of\gl}
= \abs{s_0^2\mathfrak R' \of {s_0}} \max_\gb\abs{\mathfrak B \of \gb} \max_\gl \abs{\mathfrak L\of\gl}\text.
\]
The first factor is
\[
\abs{s_0^2\mathfrak R' \of {s_0}}=s_0^2\frac{\pa{n+1}\mathfrak R\of{s_0}}{s_0}=\pa{n+1}\frac{R^n}{s_0^n}\text.
\]
We have \begin{align*}
\max_\gl \abs{\mathfrak L\of\gl}
&= \max_\gl \abs{C_{nm}\cos m\gl + S_{nm} \sin m\gl}\\
&= \max_\gl \abs{\operatorname{Re}\of{\pa{C_{nm} + \I S_{nm}}\E^{-\I m\gl}}}\\
&= \abs{C_{nm} + \I S_{nm}} = \sqrt{C_{nm}^2 + S_{nm}^2}\text,
\end{align*}
and we numerically compute and tabulate \[
\max_\gb\abs{\mathfrak B \of \gb} = \max_\gb\abs{\p nm\of{\sin \gb}}\text.
\]
Thus we can compute the desired $s_0$ as
\[
s_0 = R \pa{\frac{\max_\gb\abs{\p nm\of{\sin \gb}}\pa{n+1}\sqrt{C_{nm}^2 + S_{nm}^2}}{\gce}}^{\frac 1 n}\text.
\]

\printbibliography
\end{document}


\documentclass[12pt]{article}
\input{physics1}
\begin{document}

\section*{NYU Physics I---oscillations}

Consider a mass $M$ on a spring of spring constant $k$, released from
rest at $t=0$ but from a distance $X$ (in the $x$-direction, which is
parallel to the spring) away from the equilibrium position for the
mass.  Assume there are no other forces acting!

With a partner, do the following graphing assignments. They might
require some planning, because we want to align all the time axes.  In
all the parts below, when we say ``label'' we mean ``give the
horizontal and vertical position of''. That is, we want the graphs to
be quantitatively correct.

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the position of the mass as a function of time for a few periods.
Label minima, maxima, and zero crossings in time, and label the amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the velocity of the mass as a function of time for the same time
interval, on a new figure, but time-aligned with the previous plot.  Label
minima, maxima, and zero crossings in time, and label the velocity amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the acceleration of the mass as a function of time for the same time
interval, on a new figure, but time-aligned with the previous plot.  Label
minima, maxima, and zero crossings in time, and label the acceleration amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the kinetic energy of the mass as a function of time for the same time
interval, on a new figure, but time-aligned with the previous plot.  Label
minima, maxima, and zero crossings (if any) and label the kinetic-energy amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Work out the potential energy of a spring stretched by $x$ by
integrating force times distance.

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the potential energy of the mass as a function of time for the same time
interval, on a new figure, but time-aligned with the previous plot.  Label
minima, maxima, and zero crossings (if any) and label the potential-energy amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Guess what you will get if you plot the sum of the potential and kinetic energy plots!

\paragraph{\theproblem}\refstepcounter{problem}%
Plot the total of potential plus kinetic energy as a function of time for the same time
interval, on a new figure, but time-aligned with the previous plot.  Label
minima, maxima, and zero crossings (if any) and label the total-energy amplitude.

\paragraph{\theproblem}\refstepcounter{problem}%
Guess what your plots would have looked like if there had been a bit of
damping---a bit of air drag or friction.

\end{document}