\section{Results}
\subsection{Simplices}
The first set of topological statistics we look at are the simplices. These statistics describe the local connectivity of a random graph. The higher the dimension of a simplex and the more simplices observed in each dimension, the more complex the structure becomes. The higher the dimension of the simplex, the more ordered this structure becomes.

Beginning with the Bio-M MC, we observe large numbers of simplices across the dimensions that went as high as dimension 6. In dimensions 2 and 3, we see simplices of almost as many as 80 and 70 million respectively.

The first model of comparison is the \ER model. This model is able to match the simplicial counts of the Bio-M MC in the $0^{th}$ and $1^{st}$ dimensions, however, with a lack of order applied to the model, the complexity and ordered structure diminishes, the higher the simplicial dimension we attain.

The second model, the General Biological model, as produced here \cite{Reimann_2017}, again matches the Bio-M MC in the $0^{th}$ and $1^{st}$ dimension. The number of $2^{nd}$ dimensional simplices observed here is somewhat larger than what has been produced by the \ER model. There are approximately 35 million 2-dimensional simplices and just below 10 million 3-dimensional simplices. These two dimensions in particular are a focus point for which to be able to compare the four models proposed here.

The Configuration model, like all subsequent models, produced precisely the same number of $0^{th}$ and $1^{st}$ dimensional simplices as that of the \ER model and the General Biological model, since these describe a single vertex and a directed edge joining two vertices as part of an ordered set respectively. The first difference occurs with the 2-dimensional simplices. Of the four proposed models, the Configuration model produced, consistently, the fewest 2-dimensional simplices. We observed approximately 33.5 million each time, 3 million fewer than the General Biological model. There is almost the same difference in the number of simplices between the Configuration model and the General Biological model for simplices in the $3^{rd}$ dimension compared to the $2^{nd}$ dimension.

Applying the geometric constraint in the Geometric Configuration model saw an improvement in matching the complexity of the Bio-M MC for 2-dimensional and higher simplices when compared to the Configuration model and the General Biological model. This refinement in the model, allowed for the biggest jump in accuracy between models, showing as many as 5 million more 2-dimensional simplices than the Configuration model, and almost 3 million more in the $3^{rd}$ dimension.

The next model, the Block Configuration model saw the application of a biological constraint. This constraint is the layer to layer interactions of the neurons. By applying this layer to layer constraint, we were able to further improve the model's likeness to the Bio-M MC. This model observed as many as 41 million 2-dimensional simplices and 8.5 million 3-dimensional simplices. This is an improvement in both dimensions from the Geometric Configuration model.

The Block Geometric Configuration model produced a similar number of 2- and 3-dimensional simplices as the Block Configuration model. So, there was no significant improvement to the numbers in these dimensions. However, this model did produce some more complex simplicial structures. We observed simplices in dimensions as high as the $6^{th}$, matching that of the Bio-M MC, just not as numerous.

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[height=8cm,width=12cm]{graph/simplices_zoomed.png}
\caption{Simplicial Comparisons}
\end{center}
\end{figure}

\subsection{Betti Numbers and Euler Characteristic}
We want to compare the global connectivity of each model. We do this with Betti Numbers. In particular, we want to compare this connectivity in the highest dimension for which each model observes a non-zero Betti Number. This occurs in dimension 3. We have below, a set of results for 100 realisations of each model to give ourselves a statistical range to work with.

In figures 40-42, we have individual box-plots for each model, followed by a box-plot with all the models side by side. The reason behind this can be observed with the scales used in each individual box-plot. We begin with the \ER model observing anywhere between 0 and 10 $3^{rd}$ dimensional Betti Numbers, all the way up to the Geometric model observing approximately $30,000$. Given that Betti Numbers is a count for the organisation of the structure of the graph, we can see that with a geometric constraint applied to the network, this enables the MC to maintain a lot more ordered structure than without this constraint. This appears more apparent between the Configuration model and the Geometric Configuration model than the respective Block models. This would suggest that the bigger the volume, the more important a geometric constraint is when simulating the connectivity of neurons.

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering \ER Model]{{\includegraphics[width=7cm]{graph/betti3_er.png} }}%
    \qquad
    \subfloat[\centering Configuration Model]{{\includegraphics[width=7cm]{graph/betti3_conf.png} }}%
    \caption{Box-plots of Betti Numbers observed in the $3^{rd}$ dimension for the models}%
    \label{fig:example}%
\end{figure}

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Geometric Configuration Model]{{\includegraphics[width=7cm]{graph/betti3_geo.png} }}%
    \qquad
    \subfloat[\centering Block Configuration Model]{{\includegraphics[width=7cm]{graph/betti3_block_conf.png} }}%
    \caption{Box-plots of Betti Numbers observed in the $3^{rd}$ dimension for the models}%
    \label{fig:example}%
\end{figure}

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Block Geometric Configuration Model]{{\includegraphics[width=7cm]{graph/betti3_block_geo.png} }}%
    \qquad
    \subfloat[\centering All Models]{{\includegraphics[width=7cm]{graph/all_betti.png} }}%
    \caption{Box-plots of Betti Numbers observed in the $3^{rd}$ dimension for the models}%
    \label{fig:example}%
\end{figure}

We also compare the $3^{rd}$ Betti Number with the Euler Characteristic for our 4 models. We can see that they cluster together within each model with a larger variation in the Betti Number. The block models observed similar values with respect to one another, especially in comparison to the other two models. The size of the graph can clearly give large differences in their clustered values as we see from the Configuration and Geometric Configuration models.

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{graph/bVe.png}
\caption{Betti number in dimension 3 against Euler characteristic}
\end{center}
\end{figure}

\subsection{Total Variation distance: Distance Distribution}
One of the statistics we mention in Section 2, Mathematical Preliminaries, is the Total Variation (TV) distance. We use this statistic to give us a measure of how well one model may fit to that of the Bio-M MC in various statistical measures. However, here, we use it to check the fit of distance distributions and in the next subsection, how well the Block-wise Edge densities fit for each model.

So, we ran 100 realisations of each model with the intent to see how well the distributions of each model fit the distribution of the Bio-M MC. Below, after 100 realisations of each model, we have a table of the mean of each distribution.

We can see that the models observed a wide range of TV distances in relation to one another, with the \ER model being the weakest and the GB model being the strongest. As mentioned in their respective sections, the BC model and the BGC model observe remarkably similar TV distances when comparing to the Bio-M MC. The spread of values for each respective model, was rather small, suggesting some consistency in the way the models reconnect the neurons in the MC.
\begin{center}
 \begin{tabular}{| c | c |}
 \hline
 \textbf{Model} & \textbf{Mean Total Variation distance}  \\ [0.5ex]
 \hline
 \ER &  0.7092   \\
 \hline
 Configuration & 0.6595    \\
 \hline
 Geometric Configuration & 0.3169   \\
 \hline
 Block Configuration & 0.3939   \\
 \hline
 Block Geometric Configuration & 0.3937 \\
 \hline
 General Biological & 1.5341e-08  \\
 \hline
\end{tabular}
\end{center}

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering \ER Model ]{{\includegraphics[width=7cm]{results_imgs/erModel.png} }}%
    \qquad
    \subfloat[\centering Configuration Model]{{\includegraphics[width=7cm]{results_imgs/cModel.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Geometric Configuration Model]{{\includegraphics[width=7cm]{results_imgs/gcModel.png} }}%
    \qquad
    \subfloat[\centering Block Configuration Model]{{\includegraphics[width=7cm]{results_imgs/bcModel.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Block Geometric Configuration Model]{{\includegraphics[width=7cm]{results_imgs/bgcModel.png} }}%
    \qquad
    \subfloat[\centering General Biological Model]{{\includegraphics[width=7cm]{results_imgs/gbModel.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}
A note on the General Biological model from what we see in Figure 46(b) is that over the 100 realisations we obtained a TV distance greater than zero for one realisation. Given that this occurred only once and that the difference is insignificant, we do not concern ourselves with this.

\subsection{Total Variation distance: Block-wise Edge Densities}
Here, we compare the Block-wise Edge densities between each model and the Bio-M MC. As we mentioned previously, there were 100 realisations of each model and each were compared to the Bio-M MC. To begin comparison, we compare each block and then sum over all blocks to ascertain the TV distance of each model to the Bio-M MC and where relevant, we then give a breakdown of TV distances, block by block.

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering \ER Model]{{\includegraphics[width=7cm]{results_imgs/er_block_var.png} }}%
    \qquad
    \subfloat[\centering Configuration Model]{{\includegraphics[width=7cm]{results_imgs/conf1_overall.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}
Figure 47 shows us the \ER model and the Configuration model. Both models show a similar range of values, however, the range of values given by the Configuration model is closer to that of the Bio-M MC. This 0.12 improvement between models is somewhat significant, highlighting the importance to maintain the in-degrees and out-degrees of the respective neurons in an already observed MC.

\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Geometric Configuration Model]{{\includegraphics[width=7cm]{results_imgs/gc1_overall.png} }}%
    \qquad
    \subfloat[\centering Block Configuration Model]{{\includegraphics[width=7cm]{results_imgs/bc_block_var.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}

Figure 48(a) highlights further the importance of a geometric constraint applied to the MC. The improvement of Block-wise Edge densities almost doubles from the Configuration model. Furthermore, we have no outliers in our range of values, showing that the model can statistically be more consistent with the Bio-M MC. The Block Configuration model exhibits no differences to the Bio-M MC as is the case for the BGC model and the GB model. This is since they rearrange connections within the layer by layer blocks or subsets of these (morphological blocks).
\begin{figure}[H]%
    \centering
    \captionsetup{justification=centering}
    \subfloat[\centering Block Geometric Configuration Model]{{\includegraphics[width=7cm]{results_imgs/bgc_block_var.png} }}%
    \qquad
    \subfloat[\centering General Biological Model]{{\includegraphics[width=7cm]{results_imgs/GB_block_var.png} }}%
    \caption{Box-plots of observed total distance variation from each model to the Bio-M MC}%
    \label{fig:example}%
\end{figure}

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{results_imgs/er_bed.png}
\caption{Block-wise Edge Density Total Variation Distance Block by Block: \ER Model}
\end{center}
\end{figure}
The \ER model gives a range of TV distances for each block. The biggest occurring in L6 to L6. The smallest sets of values occur in the blocks that relate to Layer 1, whether it be the pre-synaptic set of neurons or the post-synaptic set of neurons.
\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{results_imgs/conf_bed.png}
\caption{Block-wise Edge Density Total Variation Distance Block by Block: Configuration Model}
\end{center}
\end{figure}
Now, likewise with the \ER model, the Configuration Model observes the largest set of TV distances from L6-L6 and similarly much smaller values for L1, whether it be from pre-synaptic neurons or to post-synaptic neurons, the TV distance values here are just a lot smaller.

\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{results_imgs/gc_bed.png}
\caption{Block-wise Edge Density Total Variation Distance Block by Block: Geometric Configuration Model}
\end{center}
\end{figure}
As mentioned for the \ER model and the Configuration model, the smallest differences occur in relation to neurons in Layer 1 for the GC model. However, we note here that in fact, there is a large improvement to the difference in respect of the L6-L6 connections, where we only see a mean TV distance of approximately 0.025, a large improvement on the previous two models, highlighting the strength of the geometric constraint.

\subsection{Timings}
\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{graph/timings.png}
\caption{Computation times for the random graphs}
\end{center}
\end{figure}

\begin{center}
 \begin{tabular}{| c | c | c | c |}
 \hline
 \textbf{Model} & \textbf{Min(s)} & \textbf{Mean(s)} & \textbf{Max(s)} \\ [0.5ex]
 \hline
 \ER & 5 & 5    & 5    \\
 \hline
 Configuration &  49.78 & 50.63  & 51.75   \\
 \hline
 Geometric Configuration & 854.87
 & 870.93 &  901.63   \\
 \hline
 Block Configuration & 34.77 & 34.01 & 35.55   \\
 \hline
 Block Geometric Configuration & 94.77
  & 97.55 & 99.89 \\
 \hline
 General Biological & 420  & 426     & 432   \\
 \hline
\end{tabular}
\end{center}
Finally, we have our timings for the models. All of these were computed on the same machine, of which the details are listed below. However, there were slight differences in the methods between my models and that of the General Biological model and Bio-M MC. The General Biological model and Bio-M MC were computed first by converting the file formats from H5 to CSV and then converted to an NPY array, whereas my models just simply needed to take the saved NPY array.

A note on the geometrically constrained models is that they had a similar computation time up until approximately 80$\%$ completion and then slowed down significantly in order to connect the remaining vertices. Therefore the remaining 20$\%$ of connections took the majority of time to compute.
\newpage
\begin{lstlisting}[language=bash]
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          12
On-line CPU(s) list:             0-11
Thread(s) per core:              2
Core(s) per socket:              6
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           113
Model name:                      AMD Ryzen 5 3600 6-Core Processor
Stepping:                        0
Frequency boost:                 enabled
CPU MHz:                         2199.811
CPU max MHz:                     3600,0000
CPU min MHz:                     2200,0000
BogoMIPS:                        7199.87
Virtualization:                  AMD-V
L1d cache:                       192 KiB
L1i cache:                       192 KiB
L2 cache:                        3 MiB
L3 cache:                        32 MiB
NUMA node0 CPU(s):               0-11
\end{lstlisting}


\section{Group theory}\label{sec:group_theory}

Modern algebra takes its roots in abstracting \hyperref[def:set_of_integers]{integers} and \hyperref[def:set_of_real_numbers]{real numbers} and their addition and multiplication. Both of these operations are \hyperref[def:magma/commutative]{commutative} and, if we want to generalize their properties, it is sensible to study commutative operations.

Another type of objects that usually fits in the same algebraic framework are \hyperref[def:function]{functions} and their \hyperref[def:multi_valued_function/composition]{composition}. Functions from a set to itself can be composed to form another function of the same type, similarly to how two integers can be added to obtain another integer. The main difference is in the non-commutativity of function composition.

This suggests that we use the same algebraic structures to study both generalizations of numbers and generalizations of functions over a set. The first case is commutative, the second is not. This is why commutative and non-commutative structures, even though they are similarly defined, can have very different properties and applications.

We shall not attempt to give a precise definition for an \term{algebraic structure}. There are very general frameworks for doing so, however their complexity is unjustified for us. We will instead build standard algebraic structures from \enquote{base building blocks}, although we will utilize very general definitions like categorical kernels and images defined in \fullref{def:zero_morphisms}.

The simplest algebraic structures that will be of interest to us are \hyperref[def:group]{groups} and their less well-behaved generalizations, \hyperref[def:magma/associative]{semigroups} and \hyperref[def:unital_magma/monoid]{monoids}.

Except as a building block for more complicated algebraic structures, groups arise whenever some mathematical structure exhibits symmetries, and this concept is formalized via \hyperref[def:automorphism_group]{automorphism groups} and \hyperref[def:left_group_action]{group actions}. If, instead of symmetries we have non-invertible but nonetheless well-behaved transformations, we can instead study \hyperref[def:endomorphism_monoid]{endomorphism monoids} and \hyperref[def:left_monoid_action]{monoid actions}.


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

\begin{document}

\setcounter{chapter}{3}

\chapter{Packet 4.2: Sections 16.5-16.9}

\setcounter{chapter}{16}
\setcounter{section}{4}

\section{Curl and Divergence} %16.5

\begin{definition}
  The \textbf{curl} of a vector field $\vect F=\<P,Q,R\>$
  is given by the expression
  \[
    \text{curl }\vect{F}
      =
    \nabla \times \vect{F}
      =
    \left\<
      \frac{\partial}{\partial x},
      \frac{\partial}{\partial y},
      \frac{\partial}{\partial z}
    \right\>
      \times
    \<P,Q,R\>
      =
    \<R_y-Q_z,P_z-R_x,Q_x-P_y\>
  \]
\end{definition}

          \begin{problem}
            Prove that if $\vect{F}$ is conservative, then
            $\text{curl }\vect{F}=\vect{0}$.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{remark}
  For a vector field $\vect{F}$ and direction $\vect{u}$,
  $(\text{curl }\vect{F})\cdot\vect{u}$ may be thought of as
  the tendency of $\vect{F}$ to ``spin'' counter-clockwise
  around $\vect{u}$.
\end{remark}

          \begin{problem}
            Compute the curl of $\<x+y,z^2-3,yz\>$ around the point
            $(2,0,-1)$.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{theorem}
  Green's Theorem may be rewritten in terms of curl as follows:
  \[
    \int_C \vect{F}\cdot\dvar{\vect{r}}
      =
    \iint_R (\text{curl }\vect{F}) \cdot \veck \dvar{A}
  \]
\end{theorem}

          \begin{problem}
            Prove the previous theorem.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{definition}
  The \textbf{divergence} of a vector field $\vect F=\<P,Q,R\>$
  is given by the expression
  \[
    \text{div }\vect{F}
      =
    \nabla \cdot \vect{F}
      =
    \left\<
      \frac{\partial}{\partial x},
      \frac{\partial}{\partial y},
      \frac{\partial}{\partial z}
    \right\>
      \cdot
    \<P,Q,R\>
      =
    P_x+Q_y+R_z
  \]
\end{definition}

          \begin{problem}
            Prove that the divergence of a curl vector field
            is always $0$. Put another way, show that
            $\text{div }(\text{curl }\vect{F})=0$.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{remark}
  Divergence measures the tendency of a vector field to diverge away
  from a point.
\end{remark}

          \begin{problem}
            Compute the divergence of $\<x+y,z^2-3,yz\>$ away from the point
            $(2,0,-1)$.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{definition}
  The \textbf{flux} of a velocity vector field $\vect{F}$ across a closed
  curve $C$ is given by
  \[
    \int_C \vect{F}\cdot\vect{n}\dvar{s}
  \]
  where $\vect n$ yields outward unit normal vectors to $C$.
\end{definition}

\begin{remark}
  Flux measures the tendency of a vector field to flow outward from
  a closed and bounded region (or inward if the flux is negative).
\end{remark}

\begin{theorem}
  Green's Theorem may be rewritten in terms of divergence as follows:
  \[
    \int_C \vect{F}\cdot\vect{n}\dvar{s}
      =
    \iint_R \text{div }\vect{F} \dvar{A}
  \]
\end{theorem}

          \begin{problem}
            Compute the flux of the velocity vector field
            $\<x+y,x^2+y^2\>$ across the boundary of the unit square.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\section{Parametric Surfaces} %16.6

\begin{remark}
  Just like a curve may be parameterized by $\vect{r}(t)$
  for an interval $a\leq t\leq b$, a surface may be parameterized by
  $\vect{r}(u,v)$ for a region $R$ in the $uv$ plane.
\end{remark}

\begin{theorem}
  Following are some common surface parameterizations.
  \begin{itemize}
    \item The surface $z=f(x,y)$ may be parametrized by
      \[
        \vect{r}(x,y) = \<x,y,f(x,y)\>
      \]
    \item A surface determined by a cylindrical coordinate equation may
    be parametrized by substituting into
      \[
        \vect{r} = \<r\cos\theta, r\sin\theta, z\>
      \]
    \item A surface determined by a spherical coordinate equation may
    be parametrized by substituting into
      \[
        \vect{r} =
        \<\rho\sin\phi\cos\theta,
        \rho\sin\phi\sin\theta,
        \rho\cos\phi \>
      \]
  \end{itemize}
\end{theorem}

          \begin{problem}
            Find a parameterization from the $xy$ plane to the
            plane $2x-y+z=7$ in $xyz$ space.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

          \begin{problem}
            Find the parameterization from the rectangle $0\leq z\leq 3$
            and $0\leq\theta\leq2\pi$ to the conical surface $z=\sqrt{x^2+y^2}$
            below the plane $z=3$ in $xyz$ space. (Hint: find the cylindrical
            coordinate equation for the surface.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

          \begin{problem}
            Find the parameterization from the rectangle $0\leq\phi\leq\pi$
            and $0\leq\theta\leq2\pi$ to the spherical surface
            $x^2+y^2+z^2=9$ in $xyz$ space. (Hint: find the spherical
            coordinate equation for the surface.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\section{Surface Integrals} %16.7

\begin{definition}
  The \textbf{surface integral} of a function $f(x,y,z)$ over a surface
  $S$ in $xyz$ space is given by
  \[
    \iint_S f(\vect{r})\dvar{\sigma}
      =
    \iint_R f(\vect{r}(u,v))|\vect{r}_u\times\vect{r}_v|\dvar{A}
  \]
  where $\vect{r}(u,v)$ is a parameterization from the region $R$ in
  the $uv$ plane to the surface $S$.
\end{definition}

\begin{theorem}
  The surface area of $S$ is given by
  \[
    \iint_S \dvar{\sigma} = \iint_S 1\dvar{\sigma}
  \]
\end{theorem}

          \begin{problem}
            Use the parameterization
            \[
              \vect{r}(\phi,\theta)
                =
              \<
                \sin\phi\cos\theta,
                \sin\phi\sin\theta,
                \cos\phi
              \>
            \]
            from $0\leq\phi\leq\pi,0\leq\theta\leq2\pi$ to the unit
            sphere to show that the surface area of the unit sphere
            is $4\pi$. (Note that this matches the formula $SA=4\pi r^2$ used
            in high school geometry.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

          \begin{problem}
            Show that the area of the parallelogram with vertices $(0,0,0)$,
            $(2,1,2)$, $(0,2,-1)$, and $(2,3,1)$ is $3\sqrt{5}$ using a surface
            integral.
            (Hint: use $\vect{r}(u,v)=\<2u,u+2v,2u-v\>$.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\begin{definition}
  An \textbf{orientation} of a surface is a continuous unit
  vector field normal to the surface.
\end{definition}

\begin{remark}
  Orienting a surface is akin to choosing one side or another of the surface.
\end{remark}

\begin{remark}
  Examples of non-orientable surfaces are the Mobi\"us strip and Klein bottle.
\end{remark}

\begin{definition}
  The \textbf{surface integral} of a vector field $\vect{F}$ over an
  oriented surface $S$ in $xyz$ space is given by
  \[
    \iint_S \vect{F}\cdot\dvar{\vect\sigma}
      =
    \iint_S \vect{F}\cdot\vect{n}\dvar\sigma
      =
    \iint_R \vect{F}\cdot(\vect{r}_u\times\vect{r}_v)\dvar{A}
  \]
  where $\vect n$ is the orientation of the surface and giving
  its orientation, and
  $\vect{r}(u,v)$ is an appropriate parameterization from the region $R$ in
  the $uv$ plane to the surface $S$.
\end{definition}

\begin{definition}
  The \textbf{flux} across a closed oriented surface (such as
  the boundary of a solid) is given by
  \[
    \iint_S \vect{F}\cdot\dvar{\vect\sigma}
  \]
\end{definition}

          \begin{problem}
            Use the parameterization
            \[
              \vect{r}(\phi,\theta)
                =
              \<
                3\sin\phi\cos\theta,
                3\sin\phi\sin\theta,
                3\cos\phi
              \>
            \]
            from $0\leq\phi\leq\pi,0\leq\theta\leq2\pi$ to the sphere
            $x^2+y^2+z^2=9$ to prove that the flux across it for the
            vector field $\<x,y,z\>$ is
            \[
              \int_0^{2\pi}\int_0^\pi 27\sin\phi \dvar{\phi}\dvar{\theta}
            \]
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\section{Stokes' Theorem}%16.8

\begin{theorem}
  Let $S$ be a surface with orientation $\vect{n}$
  and with boundary $C$ oriented counter-clockwise with respect to $\vect{n}$.
  Then
  \[
    \iint_S \text{curl }\vect{F}\cdot\dvar{\vect\sigma}
      =
    \int_C \vect{F}\cdot\dvar{\vect{r}}
  \]
\end{theorem}

          \begin{problem}
            Let $S$ be the upper hemisphere $z=\sqrt{1-x^2-y^2}$. Use
            Stokes' Theorem to prove that
            \[
              \iint_S \<2y,2z,2x\>\cdot\dvar{\vect\sigma}
                =
              \int_0^{2\pi} \cos^3(t) \dvar{t}
            \]
            (Hint: what's the curl of $\<z^2,x^2,y^2\>$?).
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\section{Divergence Theorem}%16.8

\begin{theorem}
  Let $S$ be the boundary of a solid $D$ oriented outwards.
  Then
  \[
    \iint_S \vect{F}\cdot\dvar{\vect\sigma}
      =
    \iiint_D \text{div }\vect{F}\dvar{V}
  \]
\end{theorem}

          \begin{problem}
            Let $S$ be the boundary of the unit cube in $xyz$ space.
            Use the Divergence Theorem to prove that
            \[
              \iint_S \<x+y,y^2+z^2,z^3+x^3\>\cdot\dvar{\vect\sigma}
                =
              \int_0^1\int_0^1\int_0^1 1+2y+3z^2 \dvar{z}\dvar{y}\dvar{x}
            \]
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\section{A small remark and puzzle}

\begin{remark}
  Using derivatives, gradients, curl, and divergence, we may observe that
  several kinds of integrals may be evaluated by observing how the
  integrand behaves on the boundary of the domain of integration, and
  vice versa.
  \[
    \int_{[a,b]} f'(x)\dvar{x} = [f(x)]_a^b
  \]
  \[
    \int_C \nabla f\cdot \dvar{\vect r} = [f(P)]_A^B
  \]
  \[
    \iint_R \text{div }\vect{F} \dvar{A}
      =
    \int_C \vect{F}\cdot\vect{n}\dvar{s}
  \]
  \[
    \iint_R Q_x-P_y \dvar{A}
      =
    \int_C \<P,Q\>\cdot\dvar{\vect r}
  \]
  \[
    \iint_S \text{curl }\vect{F}\cdot\dvar{\vect\sigma}
      =
    \int_C \vect{F}\cdot\dvar{\vect{r}}
  \]
  \[
    \iiint_D \text{div }\vect{F}\dvar{V}
      =
    \iint_S \vect{F}\cdot\dvar{\vect\sigma}
  \]
\end{remark}

\begin{problem}
  (OPTIONAL)
  This has nothing to do with the above remark, but here's a puzzle for
  reading this far.

  Wayne Brady is hosting a gameshow, and you've
  been called down from the audience to attempt to win fabulous prizes.
  Wayne gives you the choice of three doors: $A$, $B$, and $C$. He asks
  you to choose a door, explaining that only one of the three doors holds
  a prize behind it.

  After you choose, Wayne opens one of the doors that you didn't choose to
  reveal nothing behind it. He then offers you the opportunity to switch
  your door with the other unopened door, after which you will immediately
  be given whatever is behind it. Should you stick with your initial
  guess, or should you switch, or does it even matter? Why?
\end{problem}

          \begin{solution}

          \end{solution}

          \begin{contributors}

          \end{contributors}

\end{document}


\subsection{Transition maps}

Where two charts overlap we can express the points where the charts overlap as two different coordinates.

We can express the mapping from these coordinates as a function. This is a transition map.

\subsubsection{Overlapping charts}

If two charts cover some of the same points on a manifold then we can define a function for those points where we move from one vector to another.

We can represent moving between charts as:

\(ab^{-1}\)


\subsection{Damped Forced Vibrations ($b \neq 0$)}
\noindent
Our equation is
\begin{equation*}
	my' + by' + ky = F_0\cos{(\gamma t)}
\end{equation*}
Depending on if $\Delta = b^2 - 4mk$ is positive, zero, or negative, we'll get different results and thus different guesses for $y_p$ and thus different solutions. There is, however, one long, complicated and not very useful for $y$.
\begin{equation*}
	y = C_1e^{\frac{-b+\sqrt{b^2-4mk}}{2m}t} + C_2e^{\frac{-b-\sqrt{b^2-4mk}}{2m}t} +
	 \frac{F_0\left(b\gamma\sin{(\gamma t)} + \left(k-\gamma^2m\right)\cos{(\gamma t)}\right)}{b^2\gamma^2 + (k-\gamma^2m)^2}
\end{equation*}
The one useful thing this formula does tell us is that assuming $m$, $b$, $k$, $F_0$, and $\gamma$ are all positive and non-zero, the exponential terms quickly decrease to 0, so in the limit the function looks like the particular solution part.

\begin{center}
	\includegraphics[width=0.75\textwidth]{./higherOrder/forcedVibrs/damped_forced.png}
\end{center}

\ifodd\includeHigherOrderExamples\input{./higherOrder/forcedVibrs/dampedForcedVibrs_example.tex}\fi


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Linear Magnetic Inversion}\label{sec:forward magnetic}
For the magnetic inversion we use the anomaly of the magnetic flux
density~\index{magnetic flux density} of the Earth.
The controlling material parameter is the susceptibility~\index{susceptibility}
$k$ of the rock.
With magnetization $M$ and inducing magnetic field anomaly $H$, the magnetic
flux density anomaly $B$ is given as
\begin{equation}\label{ref:MAG:EQU:1}
B_i = \mu_0 \cdot ( H_i  + M_i )
\end{equation}
where $\mu_0 = 4 \pi \cdot 10^{-7} \frac{Vs}{Am}$.
In this forward model we make the simplifying assumption that the magnetization
is proportional to the known geomagnetic flux density $B^b$:
\begin{equation}\label{ref:MAG:EQU:4}
\mu_0  \cdot M_i = k \cdot B^b_i \;.
\end{equation}
Values for the magnetic flux density can be obtained by the International
Geomagnetic Reference Field (IGRF)~\cite{IGRF}
(or the Australian Geomagnetic Reference Field (AGRF)~\cite{AGRF}).
In most cases it is reasonable to assume that that the background field is
constant across the domain.

The magnetic field anomaly $H$ can be represented by the gradient of a
magnetic scalar potential\index{scalar potential!magnetic} $\psi$.
We use the form
\begin{equation}\label{ref:MAG:EQU:6}
\mu_0  \cdot H_i = - \psi_{,i}
\end{equation}
With this notation one gets from Equations~(\ref{ref:MAG:EQU:1}) and~(\ref{ref:MAG:EQU:4}):
\begin{equation}\label{ref:MAG:EQU:7}
B_i = - \psi_{,i}  + k \cdot B^b_i
\end{equation}
As the $B$ magnetic flux density anomaly is divergence free ($B_{i,i}=0$) we obtain the PDE
\begin{equation}\label{ref:MAG:EQU:8}
- \psi_{,ii} = - (k B^r_i)_{,i}
\end{equation}
with $B^r_i=B^b_i$ which needs to be solved for a given susceptibility $k$.
The magnetic scalar potential is set to zero at the top of the domain
$\Gamma_{0}$.
On all other faces the normal component of the magnetic flux density anomaly
$B_i$ is set to zero, i.e. $n_i \psi_{,i} = k \cdot n_i  B^b_i$ with outer
normal field $n_i$.

From the magnetic scalar potential we can calculate the magnetic flux density
anomaly via Equation~(\ref{ref:MAG:EQU:8}) to calculate the defect to the given
data.
If $B^{(s)}_i$ is a measurement of the magnetic flux density anomaly for
survey $s$ and $\omega^{(s)}_i$ is a weighting factor the data defect
$J^{mag}(k)$ in the notation of Chapter~\ref{chapter:ref:inversion cost function} is given as
\begin{equation}\label{ref:MAG:EQU:9}
J^{mag}(k) = \frac{1}{2}\sum_{s} \int_{\Omega} ( \omega^{(s)}_i \cdot (B_{i}- B^{(s)}_i) ) ^2 dx
\end{equation}
Summation over $i$ is performed.
The cost function kernel\index{cost function!kernel} is given as
\begin{equation}\label{ref:MAG:EQU:10}
K^{mag}(\psi_{,i},k) = \frac{1}{2}\sum_{s} ( \omega^{(s)}_i \cdot (k \cdot B^b_i - \psi_{,i} - B^{(s)}_i) ) ^2
\end{equation}
Notice that if magnetic flux density is measured in air one can ignore the
$k\cdot B^b_i$ as the susceptibility is zero.

In practice the magnetic flux density $b^{(s)}$ is measured along a certain
direction $d^{(s)}_i$ with a standard error deviation $\sigma^{(s)}$ at
certain locations in the domain.
In this case one sets $B^{(s)}_i=b^{(s)} \cdot d^{(s)}_i$ and the weighting
factors $\omega^{(s)}$ as
\begin{equation}\label{ref:MAG:EQU:11}
\omega^{(s)}_i
= \left\{
\begin{array}{lcl}
f \cdot  \frac{d^{(s)}_i}{\sigma^{(s)}} & & \mbox{data are available} \\
& \mbox{ where } & \\
0 & & \mbox{ otherwise } \\
\end{array}
\right.
\end{equation}
where it is assumed that $d^{(s)}_i \cdot d^{(s)}_i =1$. With the objective to control the
gradient of the cost function the scaling factor $f$ is chosen in the way that
\begin{equation}\label{ref:MAG:EQU:12}
\sum_{s} \int_{\Omega} ( \omega^{(s)}_i B^{(s)}_i )
 \cdot ( \omega^{(s)}_j \frac{1}{L_j} ) \cdot L^2 \cdot
( B^b_n \frac{1}{L_n} )
 \cdot k' \;
 dx =\alpha
\end{equation}
where $\alpha$ defines a scaling factor which is typically set to one and $L$ is defined by equation~(\ref{ref:EQU:REG:6b}).
$k'$ is considering the
derivative of the density with respect to the level set function.

\subsection{Usage}

\begin{classdesc}{MagneticModel}{domain, w, B, background_field,
        \optional{, coordinates=\None}
        \optional{, fixPotentialAtBottom=False},
        \optional{, tol=1e-8},
}
opens a magnetic forward model over the \Domain \member{domain} with
weighting factors \member{w} ($=\omega^{(s)}$) and measured magnetic flux
density anomalies \member{B} ($=B^{(s)}$).
The weighting factors and the  measured magnetic flux density anomalies must be vectors.
\member{background_field} defines the background magnetic flux density $B^b$
as a vector with north, east and vertical components.
\member{tol} sets the tolerance for the solution of the PDE~(\ref{ref:MAG:EQU:8}).
If \member{fixPotentialAtBottom} is set to  \True, the magnetic potential
at the bottom is set to zero in addition to the potential on the top.
\member{coordinates} set the reference coordinate system to be used. By the default the
Cartesian coordinate system is used.
\end{classdesc}

\begin{methoddesc}[MagneticModel]{rescaleWeights}{
        \optional{scale=1.}
 \optional{k_scale=1.}}
rescale the weighting factors such condition~(\ref{ref:MAG:EQU:12}) holds where
\member{scale} sets the scale $\alpha$
and \member{k_scale} sets $k'$. This method should be called before any inversion is started
in order to make sure that all components of the cost function are appropriately scaled.
\end{methoddesc}

\subsection{Gradient Calculation}
This section briefly explains how the gradient
$\frac{\partial J^{mag}}{\partial k}$ of the cost function $J^{mag}$ with
respect to the susceptibility $k$ is calculated.  We follow the concept as outlined in section~\ref{chapter:ref:inversion cost function:gradient}.

The magnetic potential $\psi$ from PDE~(\ref{ref:MAG:EQU:8}) is solved in weak form:
\begin{equation}\label{ref:MAG:EQU:201}
\int_{\Omega} q_{,i} \psi_{,i} \; dx  = \int_{\Omega}  k \cdot q_{,i}  B^r_i \; dx
\end{equation}
for all $q$ with $q=0$ on $\Gamma_{0}$.
In the following we set $\Psi[k]=\psi$ for a given susceptibility $k$ as
solution of the variational problem~(\ref{ref:MAG:EQU:201}).
If $\Gamma_{k}$ denotes the region of the domain where the susceptibility is
known and for a given direction $p$ with $p=0$ on $\Gamma_{k}$ one has
\begin{equation}\label{ref:MAG:EQU:201aa}
\int_{\Omega}   \frac{\partial J^{mag}}{\partial k} \cdot p \; dx  = \int_{\Omega}
\sum_{s} (\omega^{(s)}_j
( B^{(s)}_j-B_{j}))  \cdot ( \omega^{(s)}_i ( \Psi[p]_{,i} - p  \cdot B^b_i  ) ) \; dx
\end{equation}
With
\begin{equation}\label{ref:MAG:EQU:202c}
Y_i[\psi]=   \sum_{s} (\omega^{(s)}_j
(B^{(s)}_j - B_{j}) )  \cdot \omega^{(s)}_i
\end{equation}
this is written as
\begin{equation}\label{ref:MAG:EQU:202cc}
\int_{\Omega}   \frac{\partial J^{mag}}{\partial k} \cdot p \;  dx  = \int_{\Omega}
Y_i[\psi] \Psi[p]_{,i} - p \cdot Y_i[\psi]B^b_i   \; dx
\end{equation}
We then set adjoint function $Y^*[\psi]$ as the solution of the equation
\begin{equation}\label{ref:MAG:EQU:202d}
\int_{\Omega} r_{,i} Y^*[\psi]_{,i} \; dx  =  \int_{\Omega} r_{,i} Y_i[\psi]  \; dx  \mbox{ for all } r \mbox{ with } r=0 \mbox{ on } \Gamma_{0}
\end{equation}
with $Y^*[\psi]=0$ on $\Gamma_{0}$. With $r=\Psi[p]$ we get
\begin{equation}\label{ref:MAG:EQU:202dd}
\int_{\Omega} \Psi[p]_{,i} Y^*[\psi]_{,i} \; dx  =  \int_{\Omega} \Psi[p]_{,i} Y_i[\psi]  \; dx
\end{equation}
and from Equation~(\ref{ref:MAG:EQU:201}) with $q=Y^*[\psi]$ we get
\begin{equation}\label{ref:MAG:EQU:20e}
\int_{\Omega} Y^*[\psi]_{,i}  \Psi[p]_{,i} \; dx  = \int_{\Omega}  p \cdot Y^*[\psi]_{,i}  B^r_i \; dx
\end{equation}
which leads to
\begin{equation}\label{ref:MAG:EQU:20ee}
\int_{\Omega} \Psi[p]_{,i} ,Y_i[\psi]  \; dx  = \int_{\Omega}  p \cdot Y^*[\psi]_{,i}  B^r_i \; dx
\end{equation}
and finally
\begin{equation}\label{ref:MAG:EQU:201a}
\int_{\Omega}   \frac{\partial J^{mag}}{\partial k} \cdot p \;  dx  = \int_{\Omega}
p \cdot (Y^*[\psi]_{,i}  B^r_i - Y_i[\psi] B^b_i) \; dx
\end{equation}
or
\begin{equation}\label{ref:MAG:EQU:201b}
\frac{\partial J^{mag}}{\partial k} = Y^*[\psi]_{,i}  B^r_i - Y_i[\psi] B^b_i
\end{equation}

\subsection{Geodetic Coordinates}
For geodetic coordinates $(\phi, \lambda, h)$, see Chapter~\ref{Chp:ref:coordinates}, the solution process needs to be slightly modified.
Observations are recorded along the geodetic coordinates axes $\alpha$ rather than the Cartesian axes $i$. In fact we
have in equation~\ref{ref:MAG:EQU:9}:
\begin{equation}\label{ref:MAG:EQU:300}
\omega^{(s)}_i \cdot (B_{i}- B^{(s)}_i) = \omega^{(s)}_{\alpha} \cdot (B_{{\alpha}}- B^{(s)}_{\alpha})
\end{equation}
where now $B^{(s)}_{\alpha}$ are the observational data with weighting factors $\omega^{(s)}_{\alpha}$.  Using the
fact that $B_{{\alpha}} = k \cdot B^b_{{\alpha}} -d_{\alpha \alpha} \psi_{,\alpha}$
equation~\ref{ref:MAG:EQU:10} translates to
\begin{equation}\label{ref:MAG:EQU:301}
J^{mag}(k) = \frac{1}{2}\sum_{s} \int_{\widehat{\Omega}}
( \omega^{(s)}_{\alpha} \cdot (d_{\alpha \alpha}  \psi_{,\alpha} - k \cdot B^b_{{\alpha}}  + B^{(s)}_{\alpha} ) ) ^2 \; v \; d\widehat{x}
\end{equation}
where $\widehat{\Omega}$ and $d\widehat{x}$ refer to integration over the geodetic coordinates axes. This can be rearranged to
\begin{equation}\label{ref:MAG:EQU:301bb}
J^{mag}(k) = \frac{1}{2}\sum_{s} \int_{\widehat{\Omega}}
(  \omega^{(s)}_{\alpha} v^{\frac{1}{2}} d_{\alpha \alpha} \cdot (
 \psi_{,\alpha} -  k \cdot v_{\alpha \alpha} B^b_{{\alpha}} + v_{\alpha \alpha} B^{(s)}_{\alpha} ) ) ^2 \; d\widehat{x}
=\frac{1}{2}\sum_{s} \int_{\widehat{\Omega}}
(  {\widehat{\omega}}^{(s)}_{\alpha}\cdot ( \psi_{,\alpha} -  k \cdot \widehat{B}^b_{{\alpha}}+  \widehat{B}^{(s)}_{\alpha} ) ) ^2 \; d\widehat{x}
\end{equation}
with
\begin{equation}\label{ref:MAG:EQU:301b}
 \widehat{\omega}^{(s)}_{\alpha} = \omega^{(s)}_{\alpha} v^{\frac{1}{2}} d_{\alpha \alpha} \mbox{ , }
\widehat{B}^{(s)}_{\alpha}=
\frac{1}{d_{\alpha \alpha}} B^{(s)}_{\alpha}  \mbox{ and } \widehat{B}^b_{{\alpha}} = \frac{1}{d_{\alpha \alpha}}  B^b_{{\alpha}}
\end{equation}
which means one can apply the Cartesian formulation to the geodetic coordinates using modified data.

The magnetic potential is calculated from
\begin{equation}\label{ref:MAG:EQU:302}
\int_{\widehat{\Omega}} v \; d_{\alpha \alpha}^2 q_{,\alpha} \psi_{,\alpha} \;  d\widehat{x}
=  \int_{\widehat{\Omega}} v \; d_{\alpha \alpha} k \cdot q_{,\alpha}  B^r_{\alpha} \; d\widehat{x}
=  \int_{\widehat{\Omega}}  k \cdot q_{,\alpha}  \widehat{B}^r_{\alpha} \; d\widehat{x}
\end{equation}
with
\begin{equation}\label{ref:MAG:EQU:302b}
\widehat{B}^r_{\alpha}  =v \; d_{\alpha \alpha} \widehat{B}^r_{\alpha}
\end{equation}
see equation~\ref{ref:MAG:EQU:201}, and the adjoint function $Y^*[\psi]$ for $Y_{\alpha}[\psi]$ is given from
\begin{equation}\label{ref:MAG:EQU:303}
\int_{\widehat{\Omega}} v \; d_{\alpha \alpha}^2 q_{,\alpha} Y^*[\psi]_{,\alpha } \;d\widehat{x}  =
\int_{\widehat{\Omega}} r_{,{\alpha}} ,Y_{\alpha}[\psi]  \;d\widehat{x}
\end{equation}
and finally
\begin{equation}\label{ref:MAG:EQU:310}
\frac{\partial J^{mag}}{\partial k} = Y^*[\psi]_{,{\alpha}}  B^r_{\alpha} - Y_i[\psi] B^b_{\alpha}
\end{equation}


\chapter{Power Analysis by Simulation for Chapter \ref{ga-lottery}}\label{oa-power}

The purpose of a power analysis by simulation is to estimate $\mathrm{Pr}(\mathrm{Reject \, H_0} | \mathrm{H_0 \, is \, false})$ at a fixed significance level ($\alpha =0.05$) and sample size ($N=21,732$) for different treatment effects $\Delta_{1, \ldots, j}$. In this case $N$ is the size of the observed sample of participants, excluding women and orphans. The simulation proceeds as follows:

\begin{enumerate}
	\item Take a random sample of size $N$ without replacement from from the observed distribution of treatment assignments, weighted by the observed propensity score, to create a vector of simulated treatment assignments.
	\item Simulate response values with $\Delta_{j}$ as the difference-in-means between the simulated treated and control units. Generate random values from the binomial distribution with the probability of success on each trial equal to the mean of the response in the observed sample.
	\item Run linear model on the simulated data and extract the $p$ value.
\end{enumerate}

Repeat the simulation $\mathcal{I}$ times and calculate power of the test by dividing the count of the number of $p$ values that are less than $\alpha$ over $\mathcal{I}$. Normally, 80\% power is required to justify a study.  Fig. \ref{power-plot-bin} provides the results of power analysis simulations for the officeholding response.


\documentclass[11pt, oneside]{article}
\usepackage{amsmath}
\begin{document}

\section{Bogoliubov Tranformation for the Kondo Lattice}

We start from the Hamiltonian for the Kondo lattice

$$ H = - \sum_{i,\sigma} t_{ij}c_{i\sigma}^\dagger c_{j\sigma} +
J \sum_r \mathbf{S}_r\cdot \mathbf{s}_r +
\sum_{\langle r, s \rangle} I_{rs} \mathbf{S}_r \cdot \mathbf{S}_s $$

where $\mathbf{S}_{i},\mathbf{s}_{i}$ are the spin operators for the
localized $f$-electrons and the itinerant $d$-electrons.
Next, we use that

$$ \mathbf{S}_{r}=\mathbf{S}_{r}^{f}+\mathbf{S}_{r}^{b} $$

representing the fermionic and bosonic components of the spin. For the
fermionic component, we use

$$ \mathbf{S}_{r}^{f} =\frac{1}{2}\sum_{\alpha ,\beta }f_{r,\alpha }^{\dagger
}\mathbf{\sigma }_{\alpha \beta }f_{r,\beta } $$

$$ \mathbf{s}_{r} =\frac{1}{2}\sum_{\alpha ,\beta }c_{r,\alpha }^{\dagger }
\mathbf{\sigma }_{\alpha \beta }c_{r,\beta } $$

We will assume that the bosonic part of the spins orders
antiferromagnetically, and hence use the Holstein-Primakoff transformation
on the $A$ sublattice

$$ S_{r}^{z} = S-a_{r}^{\dagger }a_{r} $$
$$ S_{r}^{+} = \sqrt{2S}\sqrt{1-\frac{a_{r}^{\dagger }a_{r}}{2S}}a_{r} $$
$$ S_{r}^{-} = \sqrt{2S}a_{r}^{\dagger }\sqrt{1-\frac{a_{r}^{\dagger }a_{r}}{2S}} $$

and on the $B$ sublattice

$$ S_{r}^{z} = -S+a_{r}^{\dagger }a_{r} $$
$$ S_{r}^{-} = \sqrt{2S}a_{r}^{\dagger }\sqrt{1-\frac{a_{r}^{\dagger }a_{r}}{2S}} $$
$$ S_{r}^{+} = \sqrt{2S}\sqrt{1-\frac{a_{r}^{\dagger }a_{r}}{2S}}a_{r} $$

We obtain

$$ H_{MF}=\sum_{k}
\left(\begin{array}{cccc}
c_{k,\sigma }^{\dagger } & f_{k,\sigma }^{\dagger } & c_{k+Q,\sigma
}^{\dagger } & f_{k+Q,\sigma }^{\dagger }
\end{array} \right)
\left( \begin{array}{cccc}
\varepsilon _{k} & -V & U_{c} & 0 \\
-V & \chi _{k} & 0 & U_{f} \\
U_{c} & 0 & \varepsilon _{k+Q} & -V \\
0 & U_{f} & -V & \chi _{k+Q}
\end{array}\right)
\left(\begin{array}{c}
c_{k,\sigma } \\
f_{k,\sigma } \\
c_{k+Q,\sigma } \\
f_{k+Q,\sigma }
\end{array}\right) $$

$$ \hat{H_k} =
\left( \begin{array}{cccc}
\varepsilon _{k} & -V & U_{c} & 0 \\
-V & \chi _{k} & 0 & U_{f} \\
U_{c} & 0 & \varepsilon _{k+Q} & -V \\
0 & U_{f} & -V & \chi _{k+Q}
\end{array}\right) $$

Where

\begin{eqnarray*}
\varepsilon_k &=& -2 \cdot t (\cos(k_x) + \cos(k_y)) - \mu \\
\chi_k &=& -2 \cdot \chi_0 (\cos(k_x) + \cos(k_y)) - \varepsilon_f \\
V &=& \frac{J N}{4} \sum_{q,\alpha} \langle f_{q\alpha}^\dagger c_{q\alpha} \rangle \\
U_c &=& \frac{J S}{2} sgn(\sigma) \\
U_f &=& \frac{I S}{2} sgn(\sigma)
\end{eqnarray*}

Then the Green's function matrix is defined as
$$ \hat{G_R}(k,\omega+i\delta) = [(\omega+i\delta)\hat{I} - \hat{H_k}]^{-1} $$
From which we calculate the QPI spectrum
$$ g(q,\omega)=\int \frac{d^2k}{(2 \pi)^2}Im[ [\hat{G_R}(k,\omega)]_{11}[\hat{G_R}(k+q,\omega)]_{11}] $$

\end{document}


\section{Analysis}
\label{analysis}

In this section, we assess the protocol's resistance to various attacks. We claim that in most cases, the protocol behaves no worse than a pure PoW network.

\subsection{Double-spend attack}
\label{double_spend_attack}

The \textit{double-spend attack} is the well-known, but somewhat poorly named, ``51\% attack'' \cite{selfish_mining}. In the simplest version of this attack, a malicious agent sends some transaction to the network to reap an off-chain benefit (say she sells a large amount of her on-chain assets on an exchange), and then releases a side-chain in which that transaction never occurred. If this side-chain becomes the main-chain, she has
exploited the network.

Large parts of the protocol were designed with the specific goal of frustrating this attack. The interleaving of PoW and PoS blocks serves to make this attack much harder to launch.

We model this attack by letting the average difficulty of PoW blocks on the chain be $d_w$, and the average difficulty of PoS blocks $d_s$. We let the attacker have $k$ times the hashpower and $l$ times the stake of the honest block producers on the network. Once the difficulty has adjusted on the attacker's side-chain, she will be producing PoW blocks of average difficulty $kd_w$, and PoS blocks of average difficulty $ld_s$. She should expect to win if her chain grows heavier
than the honest-chain in the long-run. Mathematically, this means she needs $$kd_w + ld_s > d_w + d_s$$

We can easily observe that if $k < 1$ and $l < 1$, the attacker is not expected to win, and if \textit {both} $k>1$ and $l>1$, the attacker is always expected to win. Therefore, 51\% of at least one
domain is necessary to expect to win, and 51\% of both is sufficient to expect to win.

The intermediate case is when the attacker has a majority of one domain, but not the other. We let the attacker have $k>1$ and $l<1$ (the other case is symmetric). We can rewrite the winning condition as
$$k > 1 + (1-l)\frac{d_s}{d_w}$$

Note that the difficulty values $d_w$ and $d_s$ are just indications of how hard it was to produce a block, and therefore can be controlled to some extent by the designers of a protocol. This gives us
the nice property that by manipulating these difficulty values, we can make the attack harder to launch in one direction than the other. On the OAN, for instance, we believe that it will be easier for attackers to acquire a majority of the hash power ($k > 1$) than a majority of the stake. Accordingly, we set the typical
$d_s$ to be several orders of magnitude larger than $d_w$ (roughly $10^{14}$ times larger). The result is that an attacker with, say, 9/10ths the stake of the honest network still needs to have over $10^{10}$ times as much hash power as the honest work. The converse effect is that an attacker with a majority of the stake needs very little hash power to dominate the network, but we believe acquiring a majority of the stake to be infeasibly difficult.

\subsection{Nothing-at-Stake}

The  \textit{Nothing-at-Stake} problem arises in pure PoS blockchains when stakers try to produce blocks on every branch they see \cite{brown2018formal}. Since there is no cost to PoS block production, this is a profit-maximising strategy for PoS block forgers. This can lead to a fragile network with many branches and side-chains.

Our protocol is not susceptible to this problem since the miners will resolve the branch; of several valid PoS blocks at the same level, whichever one has a PoW block built on top of it first will likely be included into the canonical chain.

\subsection{Stake Grinding}

A PoS block producer is said to be \textit{stake grinding} if, in the production of a block, they can somehow increase the chances that they produce the next block too \cite{buterin_randomness}. In our protocol, the randomness that determines block production is entirely determined by the seed of the previous PoS block, and the private key of the block producer \S\ref{pos_block_generation}.

At every block an attacker will try to brute force compute an account that generates a lower delta for the next $k$ blocks, then attempt to transfer ownership of coins over to that account. The attack would proceed as follows: assume the attacker knows that they can produce block $n$. In the time that the attacker has before he has to announce his block ($\Delta$), the attacker can select an account (via time-bounded brute-force search) which minimizes the $\Delta$ for the next PoS block. The attacker then includes a transaction into block $n$ which transfers the attacker's stake to the selected account. When it's time to produce the next block, assuming the attacker has the smallest $\Delta$ and is the block producer at this round as well, the attacker repeats this behavior as long as they can sustain this \emph{streak} of discovering favorable accounts which yield small $\Delta$s.

Notice that this attack is made feasible by the instantaneous transfer of stake from one account to another. Therefore, if we enforce a lockout period (of $x$ blocks) before a staker is allowed to transfer stake to another account, the attacker must now win at-least $x+1$ blocks consecutively under the same account in order to successfully launch this attack. Therefore, the probability of success of the stake grinding attack can be reduced to the probability for an honest staker of winning $x+1$ blocks consecutively.

Let $s_i$ denote the absolute stake controlled by the $i$th staker and let $p_i$ denote the probability that the next block is proposed by the $i$th staker. It is easy to see that for PoS blocks, the probability of winning the next block for an honest staker is denoted by:

$$p_i=s_{i}/\sum_i{s_i}$$

Assuming that $\sum_i{s_i}$ is constant for the next $x$ blocks, it follows that the $i$th staker's probability of winning $x+1$ blocks consecutively is denoted by

\begin{equation}
    \label{eq:attacker_success}
    P(\texttt{wining x consecutive blocks})=p_i^{x}
\end{equation}

This equation implies that the probability that an honest staker can produce $x$ consecutive blocks diminishes by a factor of $p_i$ for each incremental increase in $x$.

\subsection{Denial of Service}

Since our protocol strictly interleaves PoW and PoS blocks, the network stalls if miners or stakers do not produce a block for some reason. This is a concern, but it is not significantly worse than a pure PoW network.

\subsection{Selfish Mining}

The problem of \textit{selfish mining} in pure PoW networks is that miners who adopt this strategy hold onto blocks, mining from an advantageous position \cite{selfish_mining}. This is done until such a point that the network catches up to them, at which point, they publish their chain. This strategy wastes the efforts (work) of honest miners, thus netting the attacker a block production rate disproportionate to their actual mining power. Our protocol does not suffer from this problem specifically, since mining and staking blocks must be interleaved.

There is, however, a similar problem that our protocol is vulnerable to. A staker can collude with a miner by sending the miner a PoS block before the waiting time has elapsed. This miner now gets a head start on mining a PoW block on top of it. This costs the staker nothing (since she can still release her block to the wider network when it becomes eligible). It does give the miner the chance to win more of the blocks than their share of the hash power. Further analysis is required to define the bounds around the advantage a miner can achieve by participating in such a scheme.


\subsection{Dynamics Constraints}
\label{text:approach/constraint/dynamics}
The dynamics constraints subside all constraints in problem \ref{problem:general} that are not directly related to safety, i.e.

\begin{align}
\label{eq:constraint_f}
&\x_{t+1} = \f(\x_t, \u_t) \qquad& \forall t \in [0, T - 1] \\
\label{eq:constraint_x}
&\x_t \in \xset & \forall t \in [0, T]\\
\label{eq:constraint_u}
&\u_t \in \uset & \forall t \in [0, T]\\
\label{eq:constraint_x0}
&\x_0 \in \xset_0
\end{align}

Using the shooting trajectory optimization paradigm, the decision variables are the robot's control inputs $\u_t \in \uset$. The states $\x_t$ are obtained by unrolling the controls starting at some initial state $\x_0 \in \xset_0$ and iteratively using the robot's dynamics $\f(\cdot)$. \ac{IPOPT} on the other side solves a general \ac{NLP} of the form \cite{Wachter2006} \\

\begin{problem}{General IPOPT problem formulation}
\begin{align}
\min_{x \in \mathbb{R}^n} \quad & \f(x) \\
\textrm{s.t. } \quad & g^L \leq g(x) \leq g^U \\
& x^L \leq x \leq x^U
\end{align}
\label{problem:general_ipopt}
\end{problem}

As indicated in Section \ref{text:approach/formulation}, the bounds of the controls $\u_t$ have a shape, as the decision variable $\x$ in problem \ref{problem:general_ipopt}. Therefore \ref{eq:constraint_u} is implicitly posed, without further ado, just by using the robot's control input bounds as bounds of the decision variable. Also, the constraints \ref{eq:constraint_f} and  \ref{eq:constraint_x0} are satisfied by the problem design, using the shooting method, leaving merely the constraint \ref{eq:constraint_x} to be explicitly defined. The state $\x$ of the robot incorporates both its position and its velocity. While the prediction models, objectives, and constraints only depend on relative measures regarding the agent's positions, the positional subset of $\xset$ can be safely assumed to be unbounded ($\xset_{pos} = \mathbb{R}^2$). Due to the speed boundaries imposed on the robot ($||v||_1 \leq v_{max}$, comp. Section \ref{text:approach/formulation}), in order for the solution to be feasible, a maximal speed constraint must be established:

\begin{equation}
\x_t \in \xset \Rightarrow -v_{max} \leq g_{v_{max}}(\x_t) = \dot{\x}_t \leq v_{max} \quad \forall t \in [0, T]
\label{eq:constraint_v_max}
\end{equation}

Computing the Jacobian for constraint \ref{eq:constraint_v_max} is straightforward using the chain rule and exploiting the linear robot dynamics (as above when deriving the goal objective's gradient in equation \ref{eq:goal_gradient_dynamics}):

\begin{align}
\nabla g_{v_{max}} &= \pd{g_{v_{max}}}{\u_{0:T-1}} = \pd{g_{v_{max}}}{\x_{0:T}} \cdot \pd{\x_{0:T}}{\u_{0:T-1}} \\
\Rightarrow \pd{g_{v_{max}}}{\x_{0:T}} &= \begin{bmatrix} \pd{g_{v_{max}}^1}{\x_{0:T}} & \hdots & \pd{g_{v_{max}}^T}{\x_{0:T}}\end{bmatrix}^T  \\
&= \begin{bmatrix}
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & \hdots & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & \hdots & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & \hdots & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & \hdots & 0 \\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \hdots & 1 \end{bmatrix} \\
\Rightarrow \pd{\x_{0:T}}{\u_{0:T-1}} &= \begin{bmatrix} \mathbf{0}_{n \times m} \\ B_n \end{bmatrix}
\end{align}


\chapter{Accurate Newton's Method for B\'{e}zier Curve Intersection}
\label{chap:compensated-newton}

\section{Introduction}

When using Newton's method to find the root of a function via
\begin{equation}
G\left(\bm{x}\right) = \bm{x} - J^{-1} F\left(\bm{x}\right)
\end{equation}
there are three computations performed that can introduce instability:
evaluation of the residual function \(F\left(\bm{x}\right)\), evaluation
of the Jacobian \(J\) and solution of the linear system \(J \bm{y} =
F\left(\bm{x}\right)\). In \cite{Tisseur2001}, the author showed that by
just using a more precise evaluation of the residual function, the
accuracy of Newton's method can be improved.

This chapter considers Newton's method applied to two problems:
root-finding for polynomials expressed in Bernstein form and
intersection of two B\'{e}zier curves in \(\reals^2\). In both problems,
the compensated de Casteljau method (see Chapter~\ref{chap:k-compensated}) is
used for evaluation of the residual. When evaluating a polynomial
\(p(s)\) this is straightforward, but when evaluating the difference
\(b_1(s) - b_2(t)\) between two curves special care must be taken.

\begin{figure}
  \includegraphics{../images/compensated-newton/newton_jghplus13.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Comparing relative error to condition number when using Newton's
    method to find a root of \(p(s) = (s - 1)^n - 2^{-31}\), where polynomial
    evaluation occurs via Horner's method.}
  \label{fig:jgh+13}
\end{figure}

In \cite{Graillat2008}, the problem of finding simple roots \(\alpha\) of
polynomials \(p(s)\) expressed in the monomial basis is considered.
A standard Newton's method (\texttt{HNewtonBasic}) that evaluates \(p(s)\)
and \(p'(s)\) using Horner's method is compared to a modified Newton's method
(\texttt{HNewtonAccurate}) that evaluates \(p(s)\) with a compensated
Horner's method. This proceeds as in \cite{Tisseur2001}: the evaluation of
the residual is done with greater accuracy but the rest of the process
is the same. When computing a root \(\alpha\), the standard Newton's method
has a relative error that grows linearly with the condition number of the
root (which will be defined in Section~\ref{sec:conditioning}). The
modified Newton's method is fully accurate to machine precision (i.e.
the relative error is \(\bigO{\mach}\)) until \(\cond{\alpha}\) reaches
\(1/\mach\), as seen in Figure~\ref{fig:jgh+13}.

After the point where \texttt{HNewtonAccurate} loses accuracy, we'd
expect a linear increase in relative error based on a compensated
rule of thumb:
\begin{equation}\label{eq:comp-rule-thumb}
\frac{\left|\widehat{\alpha} - \alpha\right|}{\left|\alpha\right|} \leq
  c_1 \mach + c_2 \cond{\alpha} \mach^2
\end{equation}
where \(c_1 \mach\) corresponds to rounding into the given
precision and \(c_2 \cond{\alpha} \mach^2\) reflects the
typical error but from computations done with working precision
\(\overline{\mach} = \mach^2\). The point
where the condition number exceeds \(1/\mach\) should correspond to
the point where the second term is larger that the
first term. However, this is not possible unless the
Jacobian (i.e. \(p'(s)\)) is also evaluated with a compensated
method. A second modified Newton's method (\texttt{HNewtonFull}) is
introduced in \cite[Section~8]{Jiang2013} and the author shows that
this second modified Newton's method does indeed follow a compensated
rule of thumb under appropriate conditions. We see in
Figure~\ref{fig:jgh+13} that \texttt{HNewtonFull} enables
\(\bigO{\mach}\) relative errors until the condition number reaches
\(1/\mach\) and then a linear increase in error as the condition number
grows from \(1/\mach\) to \(1/\mach^2\).

We'll proceed similarly for polynomials in
Bernstein form. Since this is a one dimensional Newton's method, improving
the evaluation of the Jacobian is straightforward. The results agree with
what has been observed when using Horner's method for evaluation.

Computing the intersection(s) of two parametric plane curves is a
common task in computational geometry and has many uses, e.g. in
finite element methods that use overlapping curved meshes. Many
methods have been described in the literature to solve this problem.
Algebraic methods such as implicitization and eigenvalue-based
methods (e.g. \cite{Manocha:CSD-92-698}) suffer
from accuracy issues for moderately high degrees and can often be
very complex to implement. Some (\cite{Bates2008}) even rely on symbolic
algebraic manipulations, which can be quite costly since it requires
arbitrary precision.
Geometric methods (e.g. \cite{Sederberg1986, Sederberg1990, Kim1998})
typically use a form of domain splitting to focus on subproblems and
eliminate parts of the domain where an intersection is guaranteed not
to occur. After a domain has been sufficiently reduced, Newton's method
is used for the last few bits of accuracy.

We'll focus on transversal curve intersections that are ill-conditioned.
Transversal intersections are an extension of the concept of a
simple root. A non-transversal intersection occurs when the curves
or tangent at the point of intersection or when one of the curves
has a zero tangent vector at that point, either due to an improper
parameterization (e.g. \(x(s) = s^2, y(s) = s^2 + 1\)) or a cusp.
In many cases, transversal intersections that are ``almost tangent'' have
very high condition numbers.

The chapter is organized as follows. In Section~\ref{sec:conditioning}
we define and discuss the conditioning of both a simple root and
a transversal intersection. In Section~\ref{sec:compensated-simple-roots}
we describe two compensated Newton's methods for finding simple roots
and perform a numerical experiment verifying the expected behavior.
In Section~\ref{sec:compensated-curve-intersect} we describe a
compensated Newton's method for B\'{e}zier curve intersection and
perform sever numerical experiements to verify the expected behavior
on both transversal intersections and tangent intersections (i.e.
intersections with infinite condition number).
Section~\ref{sec:false-starts} acts as a
coda: it describes some failed attempts at constructing numerical
examples. This section provides an in-depth discussion of a particular
family of polynomials that has much better than expected conditioning
when written in the Bernstein basis.

\section{Problem conditioning}\label{sec:conditioning}

Consider a smooth function \(F: \reals^n \longrightarrow \reals^n\)
with Jacobian \(F_{\bm{x}} = J\). We want to consider a special class of
functions of the form \(F\left(\bm{x}\right) = \sum_j c_j
\phi_j\left(\bm{x}\right)\) where the basis
functions \(\phi_j\) are also smooth functions on \(\reals^n\)
and each \(c_j \in \reals\). We want to consider the effects on a root
\(\bm{\alpha} \in \reals^n\) of a perturbation in one of the
coefficients \(c_j\). We examine the perturbed functions
\begin{equation}
G(x, \delta) = F\left(\bm{x}\right) + \delta \phi_j\left(\bm{x}\right).
\end{equation}
Since \(G\left(\bm{\alpha}, 0\right) = \bm{0}\), if \(J^{-1}\) exists at
\(\bm{x} = \bm{\alpha}\) then
the implicit function theorem tells us that we can define
\(\bm{x}\) via
\begin{equation}
G\left(\bm{x}\left(\delta\right), \delta\right) = \bm{0}.
\end{equation}
Taking the derivative with respect to \(\delta\) we find that
\(\bm{0} = G_{\bm{x}} \bm{x}_{\delta} + G_{\delta}\). Plugging in
\(\delta = 0\) we find that \(0 = J\left(\bm{\alpha}\right) \bm{x}_{\delta} +
\phi_j\left(\bm{\alpha}\right)\), hence we
conclude that
\begin{equation}
\bm{x}\left(\delta\right) = \bm{\alpha} - J\left(\bm{\alpha}\right)^{-1}
  \phi_j\left(\bm{\alpha}\right) \delta + \bigO{\delta^2}.
\end{equation}
This gives a relative condition number (for the root) of
\begin{equation}
\frac{\left \lVert J\left(\bm{\alpha}\right)^{-1}
  \phi_j\left(\bm{\alpha}\right) \right \rVert}{
  \left \lVert \bm{\alpha} \right \rVert}.
\end{equation}

By considering perturbations in \emph{all} of the coefficients:
\(\left|\delta_j\right| \leq \eps \left|c_j\right|\), a similar analysis
gives a root function
\begin{equation}
\bm{x}\left(\delta_0, \ldots, \delta_n\right) = \bm{\alpha} -
  J\left(\bm{\alpha}\right)^{-1} \sum_{j = 0}^n \delta_j
  \phi_j\left(\bm{\alpha}\right) + \bigO{\eps^2}.
\end{equation}
With this, we can define a root condition number
\begin{equation}\label{eq:abstract-cond-num}
\kappa_{\bm{\alpha}} =
  \lim_{\eps \to 0} \left(\sup \frac{\left \lVert\delta \bm{\alpha}
  \right \rVert / \eps}{\left \lVert\bm{\alpha}\right \rVert}\right) =
  \lim_{\eps \to 0} \left(\sup \frac{\left \lVert
  J\left(\bm{\alpha}\right)^{-1} \sum_j \delta_j
  \phi_j\left(\bm{\alpha}\right) \right \rVert / \eps}{
  \left \lVert\bm{\alpha}\right \rVert}\right).
\end{equation}

When \(n = 1\), \(J^{-1}\) is simply \(1 / F'\) and we find
\begin{equation}
\kappa_{\alpha} =
  \frac{1}{\left|\alpha F'(\alpha)\right|} \sum_{j = 0}^n \left|
  c_j \phi_j(\alpha)\right|.
\end{equation}
This value is given by the triangle inequality applied to
\(\delta \alpha\)  and equality can be attained since the sign
of each \(\delta_j = \pm c_j \eps\) can be modified at will to make
\(\phi_j(\alpha) \delta_j = \left|\phi_j(\alpha) c_j\right| \eps\).

When \(n > 1\), the triangle inequality tells us that
\begin{equation}
\kappa_{\bm{\alpha}} =
  \lim_{\eps \to 0} \left(\sup \frac{\left \lVert\delta \bm{\alpha} /
  \eps\right \rVert}{\left \lVert\bm{\alpha}\right \rVert}\right) \leq
  \frac{1}{\left \lVert\bm{\alpha}\right \rVert} \sum_{j = 0}^n
  \left|c_j\right| \left \lVert J\left(\bm{\alpha}\right)^{-1}
  \phi_j(\bm{\alpha})\right \rVert.
\end{equation}
However, this bound is only attainable if all
\(\phi_j(\bm{\alpha})\) are parallel. However, we'll seldom need to
compute the exact condition number and are instead typically
interested in the order of magnitude. In this case a lower
bound
\begin{equation}
\frac{1}{\left \lVert\bm{\alpha}\right \rVert}
\max_j \left|c_j\right| \left \lVert J\left(\bm{\alpha}\right)^{-1}
\phi_j(\bm{\alpha})\right \rVert
\end{equation}
for \(\kappa_{\bm{\alpha}}\)
will suffice as an approximate condition number.

For an example, consider
\begin{equation}
\phi_0 = \left[ \begin{array}{c} x_0 \\ 2 \\ 0 \end{array}\right],
\phi_1 = \left[ \begin{array}{c} 0 \\ x_1 \\ 3 \end{array}\right],
\phi_2 = \left[ \begin{array}{c} 2 \\ 0 \\ x_2 \end{array}\right],
F = \phi_0 + 2 \phi_1 + 3 \phi_2,
\bm{\alpha} = \left[ \begin{array}{c} -6 \\ -1 \\ -2 \end{array}\right].
\end{equation}
For a given \(\eps\), the maximum root perturbation occurs when
\(\delta_0 = \eps, \delta_1 = 2 \eps, \delta_2 = -3 \eps\) and
gives
\(\left \lVert J\left(\bm{\alpha}\right)^{-1} \sum_j
\delta_j \phi_j\left(\bm{\alpha}\right) \right \rVert
= 4 \sqrt{10} \eps \approx 12.65 \eps\).
The pessimistic triangle inequality bound gives
\(\sum_j \left|c_j\right| \left \lVert J\left(\bm{\alpha}\right)^{-1}
\phi_j(\bm{\alpha})\right \rVert \approx 14.64 \eps\) and the
maximum individual perturbation is \(2 \sqrt{10} \eps \approx 6.325 \eps\)
(this occurs when \(\delta_0 = \delta_1 = 0, \delta_2 = \pm 3 \eps\)).

In this general framework, we can define a condition number both
for a simple root of a polynomial in Bernstein form and for the
intersection of two planar B\'{e}zier curves. For the first,
\(\phi_j(s) = \binom{n}{j} (1 - s)^{n - j} s^j\) the Bernstein basis
functions, a polynomial \(p(s) = \sum_j b_j \phi_j(s)\) with
a simple root \(\alpha \in \left(0, 1\right]\) has root condition number
\begin{equation}
\kappa_{\alpha} =
  \frac{1}{\alpha \left|p'(\alpha)\right|} \sum_{j = 0}^n \left|
  b_j \phi_j(\alpha)\right| = \frac{\widetilde{p}(\alpha)}{
  \alpha \left|p'(\alpha)\right|}.
\end{equation}
For the intersection of a degree \(m\) curve \(b_1(s)\) and
a degree \(n\) curve \(b_2(t)\), we have basis functions
\begin{multline}
\phi_{0, -1, 1} = \left[ \begin{array}{c} B_{0, m}(s) \\ 0 \end{array}\right],
\phi_{0, -1, 2} = \left[ \begin{array}{c} 0 \\ B_{0, m}(s) \end{array}\right],
\cdots, \\
\phi_{m, -1, 1} = \left[ \begin{array}{c} B_{m, m}(s) \\ 0 \end{array}\right],
\phi_{m, -1, 2} = \left[ \begin{array}{c} 0 \\
  B_{m, m}(s) \end{array}\right], \\
\phi_{-1, 0, 1} = \left[ \begin{array}{c} -B_{0, n}(t) \\
  0 \end{array}\right],
\phi_{-1, 0, 2} = \left[ \begin{array}{c} 0 \\
  -B_{0, n}(t) \end{array}\right], \cdots, \\
\phi_{-1, n, 1} = \left[ \begin{array}{c} -B_{n, n}(t) \\
  0 \end{array}\right], \phi_{-1, n, 2} = \left[ \begin{array}{c} 0 \\
  -B_{n, n}(t) \end{array}\right].
\end{multline}
Since \(F(s, t) = b_1(s) - b_2(t)\) we have Jacobian \(J(s, t) =
\left[ \begin{array}{c c} b_1'(s) & -b_2'(t) \end{array}\right]\). We'll
consider a transversal intersection \(F(\alpha, \beta) = \bm{0}\) with
\(\det J(\alpha, \beta) \neq 0\). Since each of the
\(\phi_j\) is just a scalar multiple of the standard basis
vectors, writing \(J^{-1} = \left[ \begin{array}{c c}
\bm{v}_1 & \bm{v}_2 \end{array}\right]\), we have
\begin{multline}
J\left(\alpha, \beta\right)^{-1} \sum_{\bm{j}} \delta_{\bm{j}}
  \phi_{\bm{j}}\left(\alpha, \beta\right) = \left[\sum_{i = 0}^m
  \delta_{i, -1, 1} B_{i, m}\left(\alpha\right) + \sum_{j = 0}^n
  \delta_{-1, j, 1} B_{j, n}\left(\beta\right)\right] \bm{v}_1 \\
+ \left[\sum_{i = 0}^m
  \delta_{i, -1, 2} B_{i, m}\left(\alpha\right) + \sum_{j = 0}^n
  \delta_{-1, j, 2} B_{j, n}\left(\beta\right)\right] \bm{v}_2 =
  \nu_1 \bm{v}_1 + \nu_2 \bm{v}_2.
\end{multline}
where
\begin{equation}\label{eq:nu-bounds}
\left|\nu_k\right| / \eps \leq \sum_{i = 0}^m
  \left|c_{i, -1, k}\right| B_{i, m}\left(\alpha\right) + \sum_{j = 0}^n
  \left|c_{-1, j, k}\right| B_{j, n}\left(\beta\right) = \mu_k
\end{equation}
and the bound can be attained for both \(k = 1, 2\) by making the
signs of the \(\delta_{\bm{j}}\) agree. If we name the components of each
curve via
\(b_1(s) = \left[ \begin{array}{c c} x_1(s) & y_1(s) \end{array}\right]^T\)
and \(b_2(t) = \left[ \begin{array}{c c} x_2(t) & y_2(t) \end{array}\right]^T\)
then we see that \(\mu_1 = \widetilde{x}_1(\alpha) + \widetilde{x}_2(\beta)\)
and \(\mu_2 = \widetilde{y}_1(\alpha) + \widetilde{y}_2(\beta)\).
Thus we have condition number
\begin{align}
\kappa_{\alpha, \beta} &= \frac{1}{\sqrt{\alpha^2 + \beta^2}}
  \max_{\left|\nu_k\right| \leq \mu_k} \left \lVert \nu_1 \bm{v}_1 +
  \nu_2 \bm{v}_2 \right \rVert_2 \\
  &=
  \sqrt{\frac{\max_{\left|\nu_k\right| \leq \mu_k}
  \nu_1^2 \left(\bm{v}_1 \cdot \bm{v}_1\right) +
  2 \nu_1 \nu_2 \left(\bm{v}_1 \cdot \bm{v}_2\right) +
  \nu_2^2 \left(\bm{v}_2 \cdot \bm{v}_2\right)}{\alpha^2 + \beta^2}}
  \label{eq:intersect-cond-num}.
\end{align}
Since \(J^{-1}\) is invertible, we know \(\bm{v}_1\) and \(\bm{v}_2\) are
not parallel which can be used to show that the only internal critical
point of the function to be maximimized in~\eqref{eq:intersect-cond-num}
is \(\nu_1 = \nu_2 = 0\), which is the global minimum. Along the boundary of
the rectangle
\(\left[-\mu_1, \mu_1\right] \times \left[-\mu_2, \mu_2\right]\),
we fix one of \(\nu_1\) or \(\nu_2\) and the resulting univariate function is
an up-opening parabola, hence any critical point must be a local
minimum. Thus we know the maximum occurs at two of the four corners of the
rectangle:
\begin{equation}\label{eq:intersect-cond-num-too}
\kappa_{\alpha, \beta} = \sqrt{\frac{\mu_1^2
  \left(\bm{v}_1 \cdot \bm{v}_1\right) +
  2 \mu_1 \mu_2 \left|\bm{v}_1 \cdot \bm{v}_2\right| +
  \mu_2^2 \left(\bm{v}_2 \cdot \bm{v}_2\right)}{\alpha^2 + \beta^2}}.
\end{equation}

As far as the author can tell,
a condition number for the intersection of two planar B\'{e}zier curves
has not been described in the Computer Aided Geometric Design (CAGD)
literature. In \cite[Chapter~25, Equation 25.11]{Higham2002}
a more generic condition number is defined for the root of a nonlinear
algebraic system that is similar to the definition above.

For an example, consider the line
\(b_1(s) = \left[ \begin{array}{c c} 2s & 2s \end{array}\right]^T\)
and improperly parameterized line
\(b_2(t) = \left[ \begin{array}{c c} 4t^2 & 2 - 4t^2
\end{array}\right]^T\) which intersect at \(\alpha = \beta = 1/2\).
At the intersection we have \(J^{-1} = \frac{1}{8}
\left[ \begin{array}{c c} 2 & 2 \\ -1 & 1 \end{array}\right]\),
so that \(\bm{v}_1 \cdot \bm{v}_1 = \bm{v}_2 \cdot \bm{v}_2 =
5/64\) and \(\bm{v}_1 \cdot \bm{v}_2 = 3/64\). Since the
\(x\)-component of \(F(s, t)\) can be written as
\(2s - 4t^2 = 2 B_{1, 1}(s) - 4 B_{2, 2}(t)\) and the
\(y\)-component as \(2s + 4t^2 - 2 = 2 B_{1, 1}(s) - 2 B_{0, 2}(t)
- 2 B_{1, 2}(t) + 2 B_{2, 2}(t)\) we have
\begin{alignat}{2}
\mu_1 &= 2 B_{1, 1}(\alpha) &&+ 4 B_{2, 2}(\beta) = 2 \\
\mu_2 &= 2 B_{1, 1}(\alpha) + 2 B_{0, 2}(\beta) +
  2 B_{1, 2}(\beta) &&+ 2 B_{2, 2}(\beta) = 3.
\end{alignat}
Following~\eqref{eq:intersect-cond-num-too}, this gives
\(\kappa_{\alpha, \beta} = \sqrt{202}/8 \approx 1.78\).

\section{Simple polynomial roots}\label{sec:compensated-simple-roots}

Similar to \cite{Graillat2008} we define an algorithm to perform
standard Newton's method \\ (\texttt{DNewtonBasic}) for polynomials in
Bernstein form as well as a modified Newton's \\
(\texttt{DNewtonAccurate}) that
uses compensated de Casteljau (Algorithm~\ref{alg:comp-de-casteljau}) to
evaluate the residual. Via
the \texttt{NewtonGeneric} helper (Algorithm~\ref{alg:generic-newton}), we
need only define the callable that computes \(p(s) / p'(s)\). (Such callables
will have a single input \(s\) but will also be parameterized by the
coefficients of \(p\). This is often referred to as a closure in programming
languages.)

\begin{breakablealgorithm}
  \caption{\textit{Newton's method for polynomial in Bernstein form.}}
  \label{alg:d-newton-basic}

  \begin{algorithmic}
    \Function{\(s_{\ast} = \mathtt{DNewtonBasic}\)}
             {$b, s_0, \mathtt{tol}, \mathtt{max\_iter}$}
      \State \(n = \texttt{length}(b) - 1\)
      \For{\(j = 0, \ldots, n - 1\)}
        \State \(\Delta b_j = b_{j + 1} - b_j\)
      \EndFor
      \\
      \Function{\(\mathtt{update} = \mathtt{update\_fn}\)}{$s$}
        \State \(\widehat{b} = \mathtt{DeCasteljau}(b, s)\)
        \State \(\widehat{b'} = n \otimes \mathtt{DeCasteljau}(\Delta b, s)\)
        \State \(\mathtt{update} = \widehat{b} \oslash \widehat{b'}\)
      \EndFunction
      \\
      \State \(s_{\ast} = \mathtt{NewtonGeneric}\left(
        \mathtt{update\_fn}, s_0, \mathtt{tol}, \mathtt{max\_iter}\right)\)
    \EndFunction
  \end{algorithmic}
\end{breakablealgorithm}

\begin{breakablealgorithm}
  \caption{\textit{Modified Newton's method for polynomial in Bernstein form.}}
  \label{alg:d-newton-accurate}

  \begin{algorithmic}
    \Function{\(s_{\ast} = \mathtt{DNewtonAccurate}\)}
             {$b, s_0, \mathtt{tol}, \mathtt{max\_iter}$}
      \State \(n = \texttt{length}(b) - 1\)
      \For{\(j = 0, \ldots, n - 1\)}
        \State \(\Delta b_j = b_{j + 1} - b_j\)
      \EndFor
      \\
      \Function{\(\mathtt{update} = \mathtt{update\_fn}\)}{$s$}
        \State \(\widehat{b} = \mathtt{CompDeCasteljau}(b, s)\)
        \State \(\widehat{b'} = n \otimes \mathtt{DeCasteljau}(\Delta b, s)\)
        \State \(\mathtt{update} = \widehat{b} \oslash \widehat{b'}\)
      \EndFunction
      \\
      \State \(s_{\ast} = \mathtt{NewtonGeneric}\left(
        \mathtt{update\_fn}, s_0, \mathtt{tol}, \mathtt{max\_iter}\right)\)
    \EndFunction
  \end{algorithmic}
\end{breakablealgorithm}

As seen in \cite[Section~8]{Jiang2013}, by using greater accuracy
when computing the Jacobian \(p'(s)\), we can mitigate
the drastic increase in error as the condition number rises.
By using \texttt{CompDeCasteljauDer}
(Algorithm~\ref{alg:comp-de-casteljau-derivative}) to compute
\(p'(s)\) we define a ``fully accurate'' modified Newton's method
(\texttt{DNewtonFull}).

\begin{breakablealgorithm}
  \caption{\textit{Fully Accurate modified Newton's method for
      polynomial in Bernstein form.}}
  \label{alg:d-newton-full}

  \begin{algorithmic}
    \Function{\(s_{\ast} = \mathtt{DNewtonFull}\)}
             {$b, s_0, \mathtt{tol}, \mathtt{max\_iter}$}
      \Function{\(\mathtt{update} = \mathtt{update\_fn}\)}{$s$}
        \State \(\widehat{b} = \mathtt{CompDeCasteljau}(b, s)\)
        \State \(\widehat{b'} = \mathtt{CompDeCasteljauDer}(b, s)\)
        \State \(\mathtt{update} = \widehat{b} \oslash \widehat{b'}\)
      \EndFunction
      \\
      \State \(s_{\ast} = \mathtt{NewtonGeneric}\left(
        \mathtt{update\_fn}, s_0, \mathtt{tol}, \mathtt{max\_iter}\right)\)
    \EndFunction
  \end{algorithmic}
\end{breakablealgorithm}

\begin{figure}
  \includegraphics{../images/compensated-newton/root_plots.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Roots of \(p(s) = (1 - 5s)^n + 2^{30} (1 - 3s)^n\) for
    \(n = 5, 15\) and \(25\).}
  \label{fig:root-plots}
\end{figure}

In order to verify the accuracy of the three proposed methods, we
consider the family of polynomials
\begin{equation}
p(s) = (1 - 5s)^n + 2^{d} (1 - 3s)^n = \sum_{j = 0}^n \left[(-4)^j +
  2^{d} (-2)^j\right] B_{j, n}(s).
\end{equation}
The coefficients can be represented exactly when \(\left|(2j) -
(j + d)\right| \leq 52\), so we specialize to \(d = 30\) to get a
suitable upper bound for \(n\). We restrict to \(n\) odd so that
\((1 - 5s)^n = 2^{d} (3s - 1)^n\) has \(n\) distinct roots, only one of
which is real. The distribution of the roots can be seen in
Figure~\ref{fig:root-plots} for \(n = 5, 15\) and \(25\).

To discuss the lone real root, we define \(\omega \in \reals\) such that
\((1 + \omega)^n = 2^{30}\) and \(\omega \to 0^+\). Solving
\((1 - 5s) = (1 + \omega)(3s - 1)\) gives the root
\begin{equation}
\alpha = \frac{2 + \omega}{8 + 3 \omega} \in \left[\frac{1}{4},
  \frac{1}{3}\right].
\end{equation}
with condition number
\begin{equation}
\kappa_{\alpha} = \frac{\widetilde{p}(\alpha)}{\alpha \left|p'(\alpha)\right|}
  \sim \frac{7^n}{2^{20} \sqrt[7]{2} \; n}.
\end{equation}

\begin{figure}
  \includegraphics{../images/compensated-newton/newton_de_casteljau.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Comparing relative error to condition number when using Newton's
    method to find a root of \(p(s) = (1 - 5s)^n + 2^{30} (1 - 3s)^n\) for
    \(n\) odd.}
  \label{fig:newton-de-casteljau-1d}
\end{figure}

We use Newton's method with an absolute error tolerance of \(10^{-15}\)
and a maximum of 100 iterations. Since we know
\(\alpha \in \left[1/4, 1/3\right]\) we use \(s_0 = 1/2\) as a starting
point. Figure~\ref{fig:newton-de-casteljau-1d} plots the relative error
\(\left|\alpha - \widehat{\alpha}\right| / \alpha\) against the condition
number \(\kappa_{\alpha}\). Both the root \(\alpha\) and the relative error
are computed using a 500-bit extended precision arithmetic. As can be
seen in the Figure, the standard Newton's method (\texttt{DNewtonBasic})
has relative error that is linear in \(\kappa_{\alpha}\). Both
the first modified Newton's (\texttt{DNewtonAccurate}) and the
the ``fully accurate'' modified Newton's (\texttt{DNewtonFull}) have
\(\bigO{\mach}\) errors until \(\kappa_{\alpha}\) reaches
\(1 / \mach\). At this point, the accuracy in \texttt{DNewtonAccurate}
totally collapses to \(\bigO{1}\) while \texttt{DNewtonFull} follows
a compensated rule of thumb~\eqref{eq:comp-rule-thumb}. As expected, the
relative error for \texttt{DNewtonFull} linearly increases from
\(\bigO{\mach}\) to \(\bigO{1}\) as \(\kappa_{\alpha}\) increases from
\(1 / \mach\) to \(1 / \mach^2\), at which point the condition number
overwhelms.

\section{B\'{e}zier curve intersection}\label{sec:compensated-curve-intersect}

When intersecting curves, our residual \(F(s, t) = b_1(s) - b_2(t)\) involved
the evaluation of four polynomials in Bernstein form: \(x_1(s), y_1(s),
x_2(t), y_2(t)\). In order to compute a more accurate residual, the
straightforward approach would just use \texttt{CompDeCasteljau}
(Algorithm~\ref{alg:comp-de-casteljau}) to compute each
polynomial:
\begin{equation}
\widehat{F} = \left[ \begin{array}{c}
  \mathtt{CompDeCasteljau}(x_1, s) \ominus \mathtt{CompDeCasteljau}(x_2, t) \\
  \mathtt{CompDeCasteljau}(y_1, s) \ominus \mathtt{CompDeCasteljau}(y_2, t)
  \end{array}\right].
\end{equation}
However, when the common value (e.g. \(x_1(\alpha) = x_2(\beta)\)) at an
intersection is significantly larger than zero, this may not be helpful.
When computing \(\widehat{b} + \widehat{\partial b}\), the values may
be so far apart in magnitude that the compensated value is the same
as the non-compensated value. For example, consider the equation
\begin{equation}
1088 = s^2 + 60 s + 1076 = 1076 (1 - s)^2 + 1106 \cdot 2(1 - s)s + 1137s^2.
\end{equation}
This has one root in the unit interval \(\alpha = 4 \sqrt{57} - 30 \approx
0.1993\). For a nearby value like \(s = \frac{51}{256} + \frac{1}{2^{22}}
\approx 0.1992\) we compute \(\widehat{\partial b} = 512 \mach\), but this
value is too small relative to \(1088\) so
\(\widehat{b} \oplus \widehat{\partial b} = \widehat{b}\).

At intersections, we expect \(x_1(\alpha) - x_2(\beta)\) to be small
but don't necessarily expect the common value to be small (similarly for
the \(y\)-component). Hence we use \texttt{DeCasteljauEFT}
(Algorithm~\ref{alg:eft-de-casteljau}, the EFT associated with
\texttt{CompDeCasteljau}) to separate the ``large part'' (computed value)
from the ``small part'' (compensation term).
When subtracting \(x_2(t) = \widehat{x_2} +
\partial x_2\) from \(x_1(s) = \widehat{x_1} +
\partial x_1\) at an intersection, the values \(\widehat{x_j}\) may
be large but their difference should be small and only that difference
is suited to interact with the compensation terms:
\(\left[D, \sigma\right] =
\mathtt{TwoSum}(\widehat{x_1}, -\widehat{x_2})\). Then the
computation becomes
\begin{equation}
\left[\widehat{x_1} + \partial x_1\right] -
\left[\widehat{x_2} + \partial x_2\right] =
D + \left(\partial x_1 - \partial x_2 + \sigma\right).
\end{equation}
Unfortunately the compensation term \(\tau = \partial x_1 -
\partial x_2 + \sigma\) can't be computed without rounding,
but \(\widehat{\tau} = \left[\widehat{\partial x_1} \ominus
\widehat{\partial x_2}\right] \oplus \sigma\) can be used instead.

With this, we specify a compensated algorithm (\texttt{CompCurveResidual})
for evaluating the residual \(F(s, t)\):
\begin{breakablealgorithm}
  \caption{\textit{Compensated method for residual of
      B\'{e}zier curve intersection.}}
  \label{alg:compensated-residual}

  \begin{algorithmic}
    \Function{\(\widehat{F} = \mathtt{CompCurveResidual}\)}
             {$x_1, y_1, s, x_2, y_2, t$}
      \State \(\left[\widehat{x_1}, \widehat{\partial x_1}\right] =
        \mathtt{DeCasteljauEFT}(x_1, s)\)
      \State \(\left[\widehat{x_2}, \widehat{\partial x_2}\right] =
        \mathtt{DeCasteljauEFT}(x_2, t)\)
      \State \(\left[D, \sigma\right] = \mathtt{TwoSum}(
        \widehat{x_1}, -\widehat{x_2})\)
      \State \(\widehat{\tau} = \left[\widehat{\partial x_1} \ominus
        \widehat{\partial x_2}\right] \oplus \sigma\)
      \State \(\widehat{F}_0 = D \oplus \widehat{\tau}\)
      \\
      \State \(\left[\widehat{y_1}, \widehat{\partial y_1}\right] =
        \mathtt{DeCasteljauEFT}(y_1, s)\)
      \State \(\left[\widehat{y_2}, \widehat{\partial y_2}\right] =
        \mathtt{DeCasteljauEFT}(y_2, t)\)
      \State \(\left[D, \sigma\right] = \mathtt{TwoSum}(
        \widehat{y_1}, -\widehat{y_2})\)
      \State \(\widehat{\tau} = \left[\widehat{\partial y_1} \ominus
        \widehat{\partial y_2}\right] \oplus \sigma\)
      \State \(\widehat{F}_1 = D \oplus \widehat{\tau}\)
    \EndFunction
  \end{algorithmic}
\end{breakablealgorithm}

\begin{figure}
  \includegraphics{../images/compensated-newton/tangent_intersection.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Intersection of two B\'{e}zier curves that are tangent and have
    the same curvature at the point of tangency.}
  \label{fig:tangent-intersection}
\end{figure}

In order to compare Newton's method with the compensated residual to
standard Newton's, we consider a few examples. The first is on an
intersection (Figure~\ref{fig:tangent-intersection}) with infinite
condition number:
\begin{equation}
F(s, t) = \left[ \begin{array}{c} 2(4s^2 - 1) \\ (2s - 1)^2 + 1
\end{array}\right] - \left[ \begin{array}{c} 4(4t^2 - 1) \\ 4(2t - 1)^2 + 1
\end{array}\right].
\end{equation}
These curves are tangent and have the same curvature when \(\alpha = \beta =
1/2\); this is equivalent to a polynomial with a triple root.

\begin{figure}
  \includegraphics{../images/compensated-newton/newton_linear_converge.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Relative error plots for the computed intersection
    \(\alpha = \beta = 1/2\)
    when using standard Newton's method and a modified Newton's method that
    uses a compensated residual.}
  \label{fig:newton-linear-converge}
\end{figure}

When using Newton's method to find non-transveral intersections (i.e. roots
where the Jacobian is singular), it is well-known that convergence will
be linear rather than quadratic. However, it is less well known that
convergence will typically stop prematurely due to loss of accuracy when
computing the residual. For our example, we start with \(s_0 = 1 - 2^{-40}\)
and \(t = 3/4 + 2^{-20}\); these slight perturbations are to avoid a
``direct'' path to the intersection. We stop converging once the length
of the update vector is below \(10^{-15}\) or after 50 iterations,
whichever comes first. In Figure~\ref{fig:newton-linear-converge}
we see that the standard Newton's method stops converging when the relative
error is around \(\sqrt[3]{\mach}\), both for the \(s\) and \(t\) component
of the intersection. This is because the cubic-like behavior of the
intersection makes \(\left(\sqrt[3]{\mach}\right)^3\) resemble zero and
falsely produces a zero residual (which results in a zero Newton update).
The modified Newton's continues for much longer, until the relative error
reaches \(\sqrt[3]{\mach^2}\).

It is from this example that we base our next numerical experiment. By
introducing a small perturbation \(r\) to the \(x\)-component of
one of the curves, we can make the tangent intersection split into three
distinct intersections. Similarly, a large perturbation \(1/r\) added to
both \(y\)-components will leave the solution intact but will make the
condition number increase. This gives a family (parameterized by \(r\)):
\begin{equation}
F(s, t) = \left[ \begin{array}{c} 2(4s^2 - 1) - r \\ (2s - 1)^2 + 1 + 1/r
\end{array}\right] - \left[ \begin{array}{c} 4(4t^2 - 1) \\
4(2t - 1)^2 + 1 + 1/r \end{array}\right].
\end{equation}
We'll focus on \(r = 2^{-n}\) for \(2 \leq n \leq 50\), since for these
values the coefficients of \(F\) can be represented exactly in
\(\floats\). These curves intersect at
\begin{align}
(\alpha_1, \beta_1) &= \left(\frac{1 + \sqrt{r}}{2},
  \frac{2 + \sqrt{r}}{4}\right), \\
(\alpha_2, \beta_2) &= \left(\frac{1 - \sqrt{r}}{2},
  \frac{2 - \sqrt{r}}{4}\right), \\
(\alpha_3, \beta_3) &= \left(\frac{-3 + \sqrt{16 + r}}{2},
  \frac{6 - \sqrt{16 + r}}{4}\right).
\end{align}
When starting Newton's method from the \(s_0 = t_0 = 1\) (the ``top-right''
of the unit square), we'll converge to the first intersection
\((\alpha_1, \beta_1)\). For our chosen values of \(r\) (which will
determine which coefficients are positive, e.g. in \(\widetilde{y}_2\)),
one can show that
\begin{equation}
\kappa_{\alpha_1, \beta_1} = \frac{\sqrt{10}}{2r^2} -
  \frac{3 \sqrt{10}}{40 r \sqrt{r}} + \bigO{\frac{1}{r}}.
\end{equation}

\begin{figure}
  \includegraphics{../images/compensated-newton/almost_tangent.pdf}
  \centering
  \captionsetup{width=.75\linewidth}
  \caption{Relative error plots for the computed intersection
    \(\alpha = \beta = 1/2\)
    when using standard Newton's method and a modified Newton's method that
    uses a compensated residual.}
  \label{fig:almost-tangent}
\end{figure}

We compare the standard Newton's to the modified Newton's with a compensated
residual as \(r \to 0^+\). We use \(s_0 = t_0 = 1\) as mentioned above
and the same stopping criterion from the first example: stop once the length
of the update vector is below \(10^{-15}\) or after 50 iterations, whichever
comes first. As seen in Figure~\ref{fig:almost-tangent}, the standard
Newton's method has relative error that increases linearly with
the condition number, i.e. \(\bigO{\mach} \kappa_{\alpha_1, \beta_1}\),
until it reaches \(\bigO{1}\). The
modified Newton's method satisfies a compensated rule of
thumb~\eqref{eq:comp-rule-thumb}: the relative error is \(\bigO{\mach}\)
(i.e. fully accurate up to rounding) until \(\kappa_{\alpha_1, \beta_1}\)
reaches \(1 / \mach\) at which point the relative error resembles
\(\bigO{\mach^2} \kappa_{\alpha_1, \beta_1}\). What's more, when
\(r = 2^{-2m}\) has a rational square root, the intersection
can be represented exactly in \(\floats\) and the compensated method
computes this intersection without any error.

\section{False starts}\label{sec:false-starts}

It was quite challenging to generate families of polynomials with known
ill-conditioned simple roots and families of B\'{e}zier curve pairs with known
ill-conditioned transversal intersections. The issue was not in finding a
family with moderately high condition number, but in finding one where the
condition number would grow past \(1 / \mach\) and even as far as
\(1 / \mach^2\). In many failed attempts, as the condition increased
in a given family, it became impossible to represent the
coefficients exactly in \(\floats\). Once the coefficients are rounded,
the roots or intertesections themselves will likely move erratically.
This erratic behavior is the goal when generating ill-conditioned problems,
but makes the values themselves less useful.

To give a sense of the nontrivial effort involved in finding a
family, consider the following families of polynomials that
\emph{failed}:
\begin{itemize}
  \itemsep 0em
  \item \(p(s) = (1 - s)^n - 2^{-d}\) at the simple root
    \(s_{\ast} = 1 - 2^{-d/n}\). In the monomial basis, the
    condition number of \(s_{\ast}\) grows exponentially and was used
    as the example in \cite{Graillat2008} and \cite[Section~8]{Jiang2013}.
    However in the Bernstein basis, the root condition
    number is always less than \(1\).
  \item \(p(s) = (as - 1)^n - 2^{-d} (1 - s)^n\) at the simple root
    \(s_{\ast} = \frac{1 + 2^{-d/n}}{a + 2^{-d/n}}\). This is problematic
    because when \(a - 1\) is not a power of \(2\), the coefficients
    in \((as - 1)^n = \left[-(1 - s) + (a - 1)s\right]^n\) cannot be
    represented exactly in \(\floats\) for high enough degree. The rounded
    coefficients result in sporadic behavior for the resulting polynomials.
    When \(a - 1 = 2^d\), Newton's method converges with much better than
    expected accuracy due to a unique quirk of the de Casteljau algorithm.
    This case is interesting enough to consider in greater detail
    in \cite{Hermes2018}.
  \item \(p(s) = (2s - 1)^n - 2^{-52}\) at the simple root
    \(s_{\ast} = \frac{1 + 2^{-52/n}}{2}\). The coefficients \(b_j =
    (-1)^{n - j} - 2^{-52}\) can always be represented in \(\floats\),
    but as \(n \to \infty\) we have \(s_{\ast} \to 1\).
    A root exactly equal to \(1\) is perfectly conditioned, so
    unsurprisingly \(\kappa \to 0\) after reaching a maximum
    value of \(\approx 3 \cdot 10^{13}\) when \(n = 28\).
  \item \(p(s) = (3s - 1)^n - 2^{-d}\) at the simple root \(s_{\ast} = \frac{
    1 + 2^{-d/n}}{3}\). The coefficients \(b_j = (-1)^{n - j} 2^j - 2^{-d}\)
    can be represented exactly in \(\floats\) for \(\left|j + d\right|
    \leq 52\) (or \(53\), depending on \(n\)). This limitation stops the
    condition number from growing large enough. For example, when \(d = 10\),
    the largest term that can be represented exactly is \(\kappa_{43}
    \approx 1.03 \cdot 10^{10}\),
    when \(d = 20\), the largest term that
    can be represented exactly is \(\kappa_{33} \approx 2.53 \cdot 10^{10}\)
    and when \(d = 30\), the largest term that
    can be represented exactly is \(\kappa_{23} \approx 9.24 \cdot 10^{10}\).
\end{itemize}

In addition, several false starts were made when generating a family of
B\'{e}zier curve pairs with ill-conditioned intersection. In the
case of polynomials we increased the condition number via the degree,
but in the case of curve pairs it was increased by reducing a continuous
parameter towards zero at which point the pair becomes tangent. The
most problematic case came from
\begin{equation}
F(s, t; r) = \left[ \begin{array}{c} 2(4s^2 - 1) + r \\ (2s - 1)^2
\end{array}\right] - \left[ \begin{array}{c} 4(4t^2 - 1) \\ 4(2t - 1)^2
\end{array}\right].
\end{equation}
In this case, the polynomials \((2s - 1)^2\) and \(4(2t - 1)^2\) come
from a very special class of polynomials described in \cite{Hermes2018}
where the de Casteljau algorithm is very accurate (much better than
the a priori upper bound). As a result, the \(y\)-contributions to the
relative error were essentially zero. This can be thought of as setting
\(\mu_2 = 0\) when computing the condition number. However, for this family
of curves the \(x\)-components contribute \(\bigO{1}\) to the condition
number while the \(y\)-components \emph{should} contribute \(\bigO{1/r}\)
but instead contribution nothing. Hence the solution can always be computed
by Newton's method with \(\bigO{\mach}\) relative error.


\section{}

\subsection{}

\subsection{}

\subsection{}

	\begin{problem}
		From Page 320 number 6. If we take the 95\% upper, $n=60$, $s^2=12.5$, and $\overline{x}=18.6$ the formula we are going to be using is $\overline{x}+Z_\alpha\cdot \frac{s}{\sqrt{n} }$.

		\begin{equation}
			18.6+1.645 \frac{\sqrt{12.5}}{\sqrt{60}}\approx19.55
		\end{equation}

		Because our result was 19.55, our interval is $(-\infty, 19.55)$.
	\end{problem}

	If we were to take $\overline{x_1}-\overline{x_2}$, we would need to do

	\begin{equation}
		\overline{x_1}-\overline{x_2}+Z_\alpha\cdot \sqrt{\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2}}.
	\end{equation}

	So if we were to have the $\hat{p_1}-\hat{p_2}$ lower,

	\begin{equation}
		\hat{p_1}-\hat{p_2}-Z_\alpha\sqrt{\frac{\hat{p_1}\hat{q_2}}{n_1}+\frac{\hat{p_2}\hat{q_2}}{n_2}}.
	\end{equation}

	\begin{problem}
		Let's take a look at a problem, where 55\% of 2000 American adults surveyed said they have watched digitally streamed TV programming on some type of device. $\hat{W}$ sample size would be required for the width of a 99\% CI to be at most 0.5 irrespective of the value of $\hat{p}$. We know that the value of $\hat{p}$ is 55\%. and we need to find the $[a,b]$ interval.

		$$
		\begin{aligned}
			\left(\hat{p}+2.576\sqrt{\frac{\hat{p}\hat{q}}{n}}\right)-\left(\hat{p}-2.576\sqrt{\frac{\hat{p}\hat{q}}{n}}\right)\\
			&=2\cdot 2.576\sqrt{\frac{\hat{p}\hat{q}}{n}}\\
			&= \text{size of the CI}\\
			0.5 &> 2\cdot 2.576\sqrt{\frac{\hat{p}\hat{q}}{n}}
		\end{aligned}
		$$

		Consider the worst scenario:

		$$
		\begin{aligned}
			2\cdot 2.576\sqrt{\frac{\frac{1}{2}\frac{1}{2}}{n}}&<0.05\\
			\sqrt{\frac{\frac{1}{4}}{\sqrt{n}}}&<\frac{0.5}{2\cdot2.576}\\
			\frac{\frac{1}{2}}{\sqrt{n}}&<\frac{.05}{2*2.576}\\
			\frac{2.576}{0.5}&<\sqrt{n}\\
			n&>\left(\frac{2.576}{.05}\right)^2\\
			n&=2655
		\end{aligned}
		$$
		\end{problem}