\section{Modelling precision}
In this section, we try to build a basic model of precision errors that we can use to obtain a rough but reliable estimate of a program's precision.

\subsection{The issue with ``absolute or relative'' error}\label{ss:abs-rel}
When the output of a problem is some real value like a distance or an area, the problem statement often specifies a constraint such as: ``The answer should be accurate to an absolute or relative error of at most $10^{-5}$.'' While considering the relative accuracy of an answer can be a useful and convenient way to specify the required precision of an answer in some cases (for example in tasks where only addition and multiplication of positive values are performed), we think that for most geometry problems it is unsuitable.

The reason for this is the need to subtract\footnote{Strictly speaking, we mean both subtraction of values of the same sign, and addition of values of opposite signs.} large values of similar magnitude. For example, suppose that we are able to compute two values with relative precision $10^{-6}$, such as $A=1000 \pm 10^{-3}$ and $B = 999 \pm 10^{-3}$. If we compute their difference, we obtain $A-B = 1 \pm 2 \times 10^{-3}$. The absolute error remains of a comparable size, being only multiplied by 2, but on the other hand relative error increases drastically from $10^{-6}$ to $2\times 10^{-3}$ because of the decrease in magnitude. This phenomenon is called \emph{catastrophic cancellation}.

In fact, whenever a certain relative error can affect big numbers, catastrophic cancellation can cause the corresponding absolute error to appear on very small values. The consequence is that if a problem statement has a certain tolerance on the relative error of the answer, and a correct solution has an error close to it for the biggest possible values, then the problem statement also needs to specify a tolerance on the corresponding absolute error in case catastrophic cancellation happens. And since that tolerance on absolute error is at least as tolerant as the tolerance on relative error for all possible values, it makes it redundant. This is why we think that tolerance on ``absolute or relative error'' is misleading at best.\footnote{In fact, working with relative error tolerances would make sense if this ``relative error'' was defined based on the magnitude of the input coordinates rather than on the magnitude of the answer, as we will see starting from section~\ref{ss:biggest-mag}. For example, if all input coordinates are bounded by $M$, it would make sense to require an absolute precision of $M^2\times 10^{-6}$ on an area. But since the answer can remain very small even if the magnitude of the input grows, requiring a fixed relative precision on it is usually too constraining for test cases with inputs of large magnitude.}

Catastrophic cancellation shows that relative precision is not a reliable way to think about precision whenever subtractions are involved --- and that includes the wide majority of geometry problems.
In fact, the most common geometric operations (distances, intersections, even dot/cross products) all involve subtractions of values which could be very similar in magnitude.

Examples of this appear in two of the case studies of section~\ref{s:case-studies}: in problem ``Keeping the Dogs Apart'' and when finding the solution of a quadratic equation.

Another example occurs when computing areas of polygons made of imprecise points. Even when the area ends up being small, the imprecision on it can be large if there were computations on large values in intermediate steps, which is the case when the coordinates have large magnitudes.

\centerFig{modelling0}

Because of this, we advise against trying to use relative error to build precision guarantees on the global scale of a whole algorithm, and we recommend to reason about those based on absolute error instead, as we describe below.

\subsection{Precision guarantees from IEEE 754}
%\emph{Note: This section is not meant to be a full description of how floating-point numbers work, but only a reminder of some useful guarantees. If you are completely unfamiliar with how floating-point numbers work or want to know more details, a good reference is \cite{goldberg-floating}.}

Nearly all implementations of floating-point numbers obey the specifications of the IEEE 754 standard. This includes \lstinline|float| and \lstinline|double| in Java and C++, and \lstinline|long double| in C++. The IEEE 754 standard gives strong guarantees that ensure floating-point numbers will have similar behavior even in different languages and over different platforms, and gives users a basis to build guarantees on the precision of their computations.

The basic guarantees given by the standard are:
\begin{enumerate}
\item decimal values entered in the source code or a file input are represented by the closest representable value;
\item the five basic operations ($+,-,\times,/,\sqrt{x}$) are performed as if they were performed with infinite precision and then rounded to the closest representable value.
\end{enumerate}

There are several implications. First, this means that integers are represented exactly, and basic operations on them ($+,-,\times$) will have exact results, as long as they are small enough to fit within the significant digits of the type: $\geq 9\times 10^{15}$ for \lstinline|double|, and $\geq 1.8 \times 10^{19}$ for \lstinline|long double|. In particular, \lstinline|long double| can perform exactly all the operations that a 64-bit integer type can perform.

Secondly, if the inputs are exact, the relative error on the result of any of those five operations ($+,-,\times,/,\sqrt{x}$) will be bounded by a small constant that depends on the number of significant digits in the type.\footnote{This assumes the magnitudes do not go outside the allowable range ($\approx 10^{\pm 308}$ for \lstinline|double| and $\approx 10^{\pm 4932}$ for \lstinline|long double|) which almost never happens for geometry problems.} This constant is $< 1.2 \times 10^{-16}$ for \lstinline|double| and $< 5.5 \times 10^{-20}$ for \lstinline|long double|. It is called the \emph{machine epsilon} and we will often write it $\epsilon$.

\subsection{Considering the biggest possible magnitude}\label{ss:biggest-mag}
We explained earlier why we need to work with absolute error, but since IEEE 754 gives us guarantees in terms of relative errors, we need to consider the biggest magnitude that will be reached during the computations. In other words, if all computations are precise up to a relative error of $\epsilon$, and the magnitude of the values never goes over $M$, then the absolute error of an operation is at most $M\epsilon$.

This allows us to give good guarantees for numbers obtained after a certain number of $+$ and $-$ operations: a value that is computed in $n$ operations\footnote{Note that when we say a value is ``computed in $n$ operations'' we mean that it is computed by a single formula that contains $n$ operations, and not that $n$ operations are necessary to actually compute it. For example $(a+b)+(a+b)$ is considered to be ``computed in 3 operations'' even though we can implement this with only 2 additions.} will have an absolute error of at most $nM\epsilon$ compared to the theoretical result.
%\footnote{By this we mean, a value that is computed by a single formula involving only $+$ and $-$ operations, and at most $n$ of them.}

%To clarify what we mean here by ``computed in $n$ operations'', we define it this way:
%\begin{itemize}
%\item exact inputs are ``computed in $0$ operations'';
%\item if $a$ is ``computed in $n_a$ operations'' and $b$ is ``computed in $n_b$ operations'' then the sum $a+b$ and subtraction $a-b$, is ``computed in $n_a+n_b+1$ operations''.
%\end{itemize}

We can prove the guarantee by induction: let's imagine we have two intermediate results $a$ and $b$ who were computed in $n_a$ and $n_b$ operations respectively. By the inductive hypothesis their imprecise computed values $a'$ and $b'$ respect the following conditions.
\[|a'-a| \leq n_aM\epsilon \qquad |b'-b| \leq n_bM\epsilon\]

The result of the floating-point addition of $a'$ and $b'$ is $\round(a'+b')$ where $\round()$ is the function that rounds a real value to the closest representable floating-point value. We know that $|\round(x)-x| \leq M\epsilon$, so we can find a bound on the error of the addition:
\begin{align*}
|\round(a'+b') &- (a+b)|\\
&= \left|\left[\round(a'+b') - (a'+b')\right] + \left[(a'+b') - (a+b)\right]\right|\\
&\leq |\round(a'+b') - (a'+b')| + |(a'+b') - (a+b)|\\
&\leq M\epsilon + |(a'-a) + (b'-b)| \\
&\leq M\epsilon + |a'-a| + |b'-b| \\
&\leq M\epsilon + n_aM\epsilon + n_bM\epsilon \\
&= (n_a + n_b + 1) M \epsilon
\end{align*}
where the first two steps follow from the triangle inequality.
Since the sum is ``computed in $n_a+n_b+1$ operations'', the bound of $(n_a + n_b + 1) M \epsilon$ that is obtained is small enough. The proof for subtraction is very similar.

\subsection{Incorporating multiplication}
The model above gives good guarantees but is very limited: it only works for computations that use only addition and subtraction. Multiplication does not give guarantees of the form $nM\epsilon$. However, we can still say interesting things if we take a closer look the different types of values we use in geometry:
\begin{itemize}
\item Adimensional ``0D'' values: e.g. angles, constant factors;% Their magnitude is typically limited by some small constant (e.g. $2\pi$ for angles).
\item 1D values: e.g. coordinates, lengths, distances, radii;% Their magnitude is limited by a constant $M$, which can be deduced from the input limits.
\item 2D values: e.g. areas, dot products, cross products;% Since they are based on products of 1D numbers, their magnitude is typically within a small constant factor of $M^2$.
\item 3D values: e.g. volumes, mixed products.% For the same reasons, their magnitude is typically within a small constant factor of $M^3$.
\end{itemize}

Usually, the problem statement gives guarantees on the magnitude of coordinates, so we can find some constant $M$ so that all 1D values that will be computed in the code have a magnitude less than $M$. And since 2D and 3D values are usually created by products of 1D values, we can usually say that 2D values are bounded in magnitude by $M^2$ and 3D values by $M^3$ (we may need to multiply $M$ by a constant factor).

It turns out that computations made of $+,-,\times$ and in which all $d$-dimensional values are bounded in magnitude by $M^d$ have good precision guarantees. In fact, we can prove that the absolute error of a $d$-dimensional number computed in $n$ operations is at most $M^d\left((1+\epsilon)^n-1\right)$, which assuming $n\epsilon \ll 1$ is about $nM^d\epsilon$.

The proof is similar in spirit to what we did with only $+$ and $-$ earlier. Since it is a bit long, we will not detail it here, but it can be found in section~\ref{sec:proof-precision}, along with a more precise definition of the precision guarantees and its underlying assumptions.

Note that this does \emph{not} cover multiplication by an adimensional factor bigger than 1: this makes sense, since for example successive multiplication by 2 of a small value could make the absolute error grow out of control even if the magnitude remains under $M^d$ for a while.

In other cases, this formula $nM^d\epsilon$ gives us a quick and reliable way to estimate precision errors.

\subsection{Why other operations do not work as well}\label{ss:other-operations}
Now that we have precision guarantees for $+,-,\times$ operations, one might be tempted to try and include division as well. However, if that was possible, then it would be possible to give strong precision guarantees for line intersection, and we saw in subsection~\ref{ss:numerically-unstable} that this is not the case.

The core of the problem is: if some value $x$ is very close to zero, then a small absolute error on $x$ will create a large absolute error on $1/x$. In fact, if $x$ is smaller than its absolute error, the computed value $1/x$ might be arbitrarily big, both in the positive or negative direction, and might not exist. This is why it is hard to give guarantees on the results of a division whose operands are already imprecise.

An operation that also has some problematic behavior is $\sqrt{x}$. If $x$ is smaller than its absolute error, then $\sqrt{x}$ might or might not be defined in the reals. However, if we ignore the issue of existence by assuming that the theoretical and actual value of $x$ are both nonnegative, then we \emph{can} say some things on the precision.

Because $\sqrt{x}$ is a concave increasing function, a small imprecision on $x$ will have the most impact on $\sqrt{x}$ near 0.

\centerFig{modelling1}

Therefore for a given imprecision $\delta$, the biggest imprecision on $\sqrt{x}$ it might cause is $\sqrt{\delta}$. This is usually pretty bad: if the argument of the square root had an imprecision of $nM^2\epsilon$ then in the worst case the result will have an imprecision of $\sqrt{n}M\sqrt{\epsilon}$, instead of the $nM\epsilon$ bound that we have for $+,-,\times$ operations.

For example let us consider a circle $\mathcal{C}$ of radius tangent to a line $l$. If $\mathcal{C}$ gets closer to $l$ by $10^{-6}$, then the intersection points will move by about
\[\sqrt{1^2 - (1-10^{-6})^2} \approx \sqrt{2\times 10^{-6}} = \sqrt{2} \times 10^{-3}\]
away from the tangency point, as pictured below.

\centerFig{modelling2}

Note that here we have only shown that $1/x$ and $\sqrt{x}$ perform poorly on imprecise inputs. Please bear in mind that on exact inputs, the IEEE 754 guarantees that the result is the closest represented floating-point number. So when the lines and circles are defined by integers, line intersections and circle-line intersections have a relative precision error proportional to $\epsilon$ and thus an absolute error proportional to $M\epsilon$.


\documentclass[11pt, numbers=endperiod, parskip=half]{scrartcl}

\usepackage{amsmath}
\usepackage{color}
\usepackage{semantic}
\usepackage{soul}
\usepackage{pdflscape}

\title{Assignment 2}
\subtitle{COS30023 - Languages in Software Development}
\author{Daniel Parker - 971328X}

\date{\today}

\begin{document}
\maketitle

\section{Problem 1}
\subsection{Hoare Triple}
\begin{verbatim}
{a > 4, b = 7}
  x := b;
  y := a;
{a > 4, b = 7, x = 7, y > 4}
\end{verbatim}

\subsection{Rules}
%%%%%%%%%%%%%%%%%%%%%%%%%
%		  RULES         %
%%%%%%%%%%%%%%%%%%%%%%%%%
\inference[C = \textsuperscript{def} C.Target := C.Source : ]{true}{\{Q[C.Target \\\ C.Source]\} C \{Q\}}
\bigskip
\inference[C = \textsuperscript{def} C\textsubscript{1}; C\textsubscript{2} : ]{\{P\}C\textsubscript{1}\{R\} & \{R\}C\textsubscript{2}\{Q\}}{\{P\}C\textsubscript{1};C\textsubscript{2}\{Q\}}

\subsection{Proof}

\inference{\{ a > 4, b = 7 \} x := b \{R\} & \{R\} y := a \{ a > 4, b = 7, x = 7, y > 4 \} }{ \{ a > 4, b = 7 \} x := b; y := a; \{ a > 4, b = 7, x = 7, y > 4 \} }

\bigskip
\(\{R\} C_2: y := a; \{ a > 4, b = 7, x = 7, y > 4\}\)
\begin{flalign*}
\{R\} &= \{ a > 4, b = 7, x = 7, y > 4\}\lbrack y \backslash a \rbrack &\\
      &= \{ a > 4, b = 7, x = 7, a > 4\} \\
      &= \{ a > 4, b = 7, x = 7, \hbox{\st{a $>$ 4}}\} \\
\{R\} &= \{ a > 4, b = 7, x = 7\}
\end{flalign*}

\bigskip
\(\{ a > 4, b = 7\} C_1: x := b; \{R: a > 4, b = 7, x = 7\}\)
\begin{flalign*}
\{ a > 4, b = 7\}	&= \{ a > 4, b = 7, x = 7\}\lbrack x \backslash b \rbrack &\\
					&= \{ a > 4, b = 7, b = 7\} \\
					&= \{ a > 4, b = 7, \hbox{\st{b = 7}}\} \\
\{ a > 4, b = 7\}	&= \{ a > 4, b = 7\} \\
\end{flalign*}

\section{Problem 2}
\subsection{Hoare Triple}
\begin{verbatim}
{true}
  if x < 0 then val := -x; else val := x;
{val = abs(x)}
\end{verbatim}

\subsection{Rules}
\inference[C = \textsuperscript{def} if C.Test then C.Then else C.Else : ]{ \{ P \wedge C.Test \} C.Then \{ Q \} & \{ P \wedge \neg C.Test \} C.Else \{ Q \} }{ \{P\}C\{Q\} }
\bigskip
\inference{P => P' & \{P'\} C \{Q'\} & Q' => Q }{\{P\}C\{Q\}}

\subsection{Proof}
\inference{ \{true \wedge x < 0\} val := -x; \{ val = abs(x) \} & \{ true \wedge x \geq 0 \} val := x; \{ val = abs(x) \} }{ \{ true \}\ if\ x < 0\ then\ val := -x;\ else\ val := x; \{ val = abs(x) \} }

\subsubsection{Premise 1}
\inference{x < 0 => -x = abs(x) & \inference{true}{ \{ -x = abs(x) \} val := -x; \{ val = abs(x) \} } }{ \{ x < 0 \} val := -x; \{ val = abs(x) \} }

\subsubsection{Premise 2}
\inference{x \geq 0 => x = abs(x) & \inference{true}{ \{ x = abs(x) \} val := x; \{ val = abs(x) \} } }{ \{ x \geq 0 \} val := x; \{ val = abs(x) \} }

\setpremisesspace{1em}
\setpremisesend{0.5em}
\begin{landscape}
\subsubsection{Full Proof}
\inference{ \inference{x < 0 => -x = abs(x) & \inference{true}{ \{ -x = abs(x) \} val := -x; \{ val = abs(x) \} } }{ \{ x < 0 \wedge true \} val := -x; \{ val = abs(x) \} } & \inference{x \geq 0 => x = abs(x) & \inference{true}{ \{ x = abs(x) \} val := x; \{ val = abs(x) \} } }{ \{ x \geq 0 \wedge true \} val := x; \{ val = abs(x) \} } }{ \{ true \}\ if\ x < 0\ then\ val := -x;\ else\ val := x; \{ val = abs(x) \} }
\end{landscape}
\end{document}


         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         % Information sheet for the Matlab lab - Maths 6111 %
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[10pt]{article}
\input ma_no_html_header

\usepackage{color}
\usepackage{hyperref}
\hypersetup{breaklinks=true,colorlinks=true}
%\input ma_header
\setlength{\parindent}{0pt}
\pagestyle{myheadings}
% \markright{
% \protect {\protect \epsfxsize=0.2 true cm \protect \epsffile {dolph.line.eps}}
% \it Maths 3018/6111 - Numerical methods \hfill}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\thispagestyle{empty}
\begin{center}
\textbf{\Large Maths 3018/6111 - Numerical Methods \\*[8mm]
Worksheet 7 - Solutions}\\*[.8cm]
\end{center}

\section*{Theory}

\begin{enumerate}
\item Find the linear system that results from using central
  differencing to solve the elliptic equation
  \begin{equation*}
    u_{xx} + u_{yy} = \sin ( \pi y ) \left( 2 - 6 x - (\pi x)^2 (1 -
      x) \right).
  \end{equation*}
  Central differencing, trivial boundary conditions, and a $3 \times
  2$ grid should be used.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
  The question is incomplete as it does not specify the spatial
  domain. However, it is reasonably to expect it to be the unit square
  $(x, y) \in [0, 1]^2$. So we first specify the grid using the
  coordinates
  \begin{align*}
    x_i & = i h_x & i & = 0, \dots, 4, & h_x & = \frac{1}{4}, \\
    y_j & = j h_y & j & = 0, \dots, 3, & h_y & = \frac{1}{3}.
  \end{align*}
  This gives an evenly spaced grid with 3 interior points in $x$ and 2
  interior points in $y$. The boundary conditions are imposed as
  \begin{align*}
    u_{0, j} & = 0, & u_{4, j} & = 0, \\
    u_{i, 0} & = 0, & u_{i, 3} & = 0.
  \end{align*}
  We are of course using the notation $u_{i,j} \approx u(x_i, y_j)$.

  We use central differencing at all interior points to get
  \begin{align*}
    u_{xx} & \approx \frac{u_{i+1,j} + u_{i-1,j} - 2 u_{i,j}}{h_x^2}, \\
    u_{yy} & \approx \frac{u_{i,j+1} + u_{i,j-1} - 2 u_{i,j}}{h_y^2}.
  \end{align*}
  We then substitute these approximations into the differential
  equation to find the difference equation satisfied at all interior
  points,
  \begin{equation*}
    \left(u_{i+1,j} + u_{i-1,j} - 2 u_{i,j} \right) + \left(
      \frac{h_x}{h_y} \right)^2 \left( u_{i,j+1} + u_{i,j-1} - 2
      u_{i,j} \right) = h_x^2 \sin ( \pi y_j ) \left( 2 - 6 x_i - (\pi
      x_i)^2 (1 - x_i) \right).
  \end{equation*}
  We will write $h_x / h_y = \alpha$.

  We choose natural ordering by rows to write the unknowns $u_{i,j}$
  as a vector ${\bf u}$ as
  \begin{equation*}
    {\bf u} = \left[ u_{1,1}, u_{1,2}, u_{2,1}, u_{2,2}, u_{3,1},
      u_{3,2} \right]^T.
  \end{equation*}
  It then follows that the matrix that defines the system has the form
  \begin{equation*}
    A =
    \begin{pmatrix}
      -2 ( 1 + \alpha ) & \alpha & 1 & 0 & 0 & 0 \\
      \alpha & -2 ( 1 + \alpha ) & 0 & 1 & 0 & 0 \\
      1 & 0 & -2 ( 1 + \alpha ) & 1 & \alpha & 0 \\
      0 & 1 & \alpha & -2 ( 1 + \alpha ) & 0 & 1 \\
      0 & 0 & 1 & 0 & -2 ( 1 + \alpha ) & \alpha \\
      0 & 0 & 0 & \alpha & 1 &  -2 ( 1 + \alpha )
    \end{pmatrix}
  \end{equation*}
  and the right hand side vector is solely in terms of the right hand
  side data
  \begin{equation*}
    h_x^2 \sin ( \pi y_j ) \left( 2 - 6 x_i - (\pi x_i)^2 (1 - x_i) \right)
  \end{equation*}
  as all the boundary data is trivial.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
\item Explain the strategy for solving evolutionary PDEs using finite
  differencing methods.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
  The domain is discretized using a set of points in space. These
  points will usually be evenly spaced with a (representative) step
  length $h$. The grid is extended to cover the time domain as well by
  discretizing time using a time step $\delta$. The derivatives
  defining the differential equation are replaced with finite
  difference approximations, the initial data fixes the unknowns at
  the initial time, and the boundary conditions imposed using
  equivalent (or consistent) discrete boundary data. This implies a
  finite difference formula which gives the unknown data at timestep
  $n+1$ in terms of the known data at timestep $n$ and the boundary
  data.

  Depending on the finite difference formula derived, there may be a
  relation between the time step $\delta$ and space step $h$ for which
  the algorithm is stable and convergent, which can be checked using
  von Neumann analysis.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
\item Write out a FTCS method for the equations
  \begin{align*}
    \partial_t y - \partial_{xx} y & = \sin(x), & x & \in [0,1], &
    y(t, 0) & = 0 = y(t, 1), \\
    \partial_t y + \partial_{x} y & = e^{-(x-1/2)^2}, & x & \in [0,1], &
    y(t, 0) & = 0 = y(t, 1).
  \end{align*}
  In both cases the initial data should be taken to be $y(0, x) =
  f(x)$. Full details of the grid, the discrete initial and boundary
  conditions, and the discrete update algorithm should be given.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
  We shall use $N+1$ points in space, denoted $x_i = i h$ with $i = 0,
  \dots, N$ and the space step $h = 1 / N$; this is an evenly spaced
  grid with boundary points $x_0 = 0$ and $x_N = 1$. We will also
  discretize time using $t^n = n \delta$ where $\delta$, the timestep,
  is unconstrained. We will denote our approximate solution at $(x, t)
  = (x_i, t^n)$ as $y_i^n \approx y(x_i, t^n)$.

  The initial data will be imposed by the equivalent discrete
  condition $y_i^0 = f(x_i)$. The (trivial Dirichlet) boundary
  conditions to be imposed in both cases will be given by the discrete
  conditions
  \begin{equation*}
    y_0^n = 0, \qquad y_N^n = 0, \qquad \forall n \ge 0.
  \end{equation*}

  To form the discrete update algorithm we use forward differencing in
  time (FT),
  \begin{equation*}
    y_t|_{x=x_i, t = t^n} \approx \frac{y_i^{n+1} - y_i^{n}}{\delta},
  \end{equation*}
  and central differencing in space (CS),
  \begin{align*}
    y_x|_{x=x_i, t = t^n} & \approx \frac{y_{i+1}^{n} - y_{i-1}^{n}}{2
      h}, \\
    y_{xx}|_{x=x_i, t = t^n} & \approx \frac{y_{i+1}^{n} + y_{i-1}^{n}
      - 2 y_i^n}{h^2}.
  \end{align*}
  We then replace the derivatives in the PDE with the finite
  difference approximations and rearrange, to get for the heat equation
  \begin{align*}
    \frac{y_i^{n+1} - y_i^{n}}{\delta} - \frac{y_{i+1}^{n} + y_{i-1}^{n}
      - 2 y_i^n}{h^2} & = \sin(x_i) \\
    \Rightarrow \qquad y_i^{n+1} = y_i^n + \delta \sin(x_i) + s \left(
      y_{i+1}^{n} + y_{i-1}^{n} - 2 y_i^n \right),
  \end{align*}
  where $s = \delta / h^2$, and for the advection equation
  \begin{align*}
    \frac{y_i^{n+1} - y_i^{n}}{\delta} + \frac{y_{i+1}^{n} -
      y_{i-1}^{n}}{2 h} & = e^{-(x_i - 1/2)^2} \\
    \Rightarrow \qquad y_i^{n+1} & = y_i^n + \delta e^{-(x_i - 1/2)^2}
    - c \left( y_{i+1}^n - y_{i-1}^n \right)
  \end{align*}
  where $c = \delta / (2 h)$. In both cases these equations hold for
  the interior points $n \ge 0$ and $i = 1, \dots, N-1$. The boundary
  points are set using the above initial and boundary data.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
\item Using von Neumann analysis, find when the BTCS method is stable
  for the advection equation.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
  The finite difference formula for the BTCS method for the advection
  equation
  \begin{equation*}
    \partial_t y + v \partial_x y = 0
  \end{equation*}
  is straightforwardly checked to be
  \begin{equation*}
    y_i^{n+1} = y_i^n - c \left( y_{i+1}^{n+1} - y_{i-1}^{n+1} \right)
  \end{equation*}
  where the convection number $c = v \delta / (2 h)$ with $\delta$ the
  timestep and $h$ the space step.

  We use the standard von Neumann analysis ansatz, which is that
  $y_{\ell}^k = q^k e^{\alpha \ell j h}$ where $q$ is the (unknown)
  growth rate to be found, $\alpha$ is related to the frequency of the
  (generic) initial data (and hence can take any real value), $h$ is
  the space step and $j = \sqrt{-1}$.

  Identifying $\ell \leftrightarrow i$ and $k \leftrightarrow n$ and
  substituting the ansatz into the finite difference equation we see
  that
  \begin{equation*}
    q = 1 - q c \left( e^{\alpha j h} - e^{- \alpha j h} \right).
  \end{equation*}
  which can be written as
  \begin{align*}
    q & = 1 - 2 j q c \sin \left(\alpha h \right) \\
    \Rightarrow \qquad q & = \frac{1}{1 + 2 j c \sin \left( \alpha h
      \right)} \\
    \Rightarrow \qquad |q|^2 & = \left| \frac{1 - 2 j c \sin \left(
          \alpha h \right)}{1 + 4 c^2 \sin^2 \left( \alpha h \right)}
    \right|^2 \\
    & = \frac{1 + 4 c^2 \sin^2 \left( \alpha h \right)}{\left[ 1 + 4
        c^2 \sin^2 \left( \alpha h \right) \right]^2} \\
    & = \frac{1}{1 + 4 c^2 \sin^2 \left( \alpha h \right)} \\
    & \le 1 \qquad \forall \alpha.
  \end{align*}
  Therefore BTCS is unconditionally stable for the advection equation.
  %
  \begin{center}
    \rule{0.9\textwidth}{.1pt}
  \end{center}
  %
\end{enumerate}

\end{document}


\documentclass{article}

\usepackage{pgfplots} %http://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat=newest, set layers=standard}

\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}

\title{Numerical Integration}
\begin{document}
\def\horzbar{\text{magic}}

  \pagenumbering{gobble}
  \maketitle
  \newpage
  \pagenumbering{arabic}

\section*{Trapeziod method}

\pgfplotsset{
    integral axis/.style={
        axis lines=middle,
        enlarge y limits=upper,
        axis equal image, width=12cm,
        xlabel=$x$, ylabel=$y$,
        ytick=\empty,
        xticklabel style={font=\small, text height=1.5ex, anchor=north},
        samples=100
    },
    integral/.style={
            domain=2:10,
            samples=9
    },
    integral fill/.style={
            integral,
            draw=none, fill=#1,
            on layer=axis background
        },
        integral fill/.default=cyan!10,
        integral line/.style={
            integral,
            very thick,
            draw=#1
        },
        integral line/.default=black
}

\begin{tikzpicture}[
    % The function that is used for all the plots
    declare function={f=x/5-cos(deg(x*1.85))/2+2;}
]
\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
]
% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;
\end{axis}
\end{tikzpicture}

$h = \frac{b-a}{N -1}$

N - number of points

Total area (integral)

$$\int_a^b f(x)dx \approx h\sum_{k=1}^{N} \frac{f(x_{k-1})+f(x_k)}{2}$$

\section*{Simpson's method}

\begin{tikzpicture}
\begin{axis}[axis lines=center, ytick=\empty,
ymax=1.3, ymin=0, xmax=2.6,xmin=0,
xtick={0.3,1.3,2.3}, xticklabels={$a$,$\frac{a+b}{2}$,$b$},
axis line style={-}, xlabel=$x$, ylabel=$y$]
\draw[name path=A, purple] (2.3,1.2) parabola (0.3,0.5);
\node[] at (1.6,1.2){$y=f(x)$};
\path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0)
                   --(\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot[gray!50] fill between[of=A and B,soft clip={domain=0.3:2.3}];
\draw[name path=C] (1.3,0) -- (1.3,1.025);
\path[name intersections={of=A and C}] coordinate (midpoint) at (intersection-1);
\draw (0.3,0.5) ..controls ++(0.1,0.3) and ++(-0.2,-0.05) .. (midpoint)
                node[pos=0.5,above left]{$y=p_2(x)$}
                ..controls ++(0.2,0.05) and ++(-0.1,-0.05) .. (2.3,1.2);
\end{axis}
\end{tikzpicture}

$h = \frac{b-a}{2}$

$$\int_a^b f(x)dx \approx \frac{h}{3} \sum_{k=1}^{N/2} \{f(x_{2k-2})+4f(x_{2k-1})+f(x_{2k}))\}$$

\end{document}


\section{$\cHH^\ast$ as a cohomology theory, and the fully relative $\cap$ product}
pset 6 is now doe December 7. If you read any book about Poincar\'{e} duality, there'll be an incomprehensible smear of stuff about cap products. I want to give you a cleaner explanation. The motivation is that it's the main step in the proof of Poincar\'{e} duality.

Let $X$ be any space, and let $K\subseteq X$ be a closed subspace. We'll write $\cHH^p(K)=\varinjlim_{U\in\mathcal{U}_K} H^p(U)$ (and call it the \v{C}ech cohomology -- but there's a different definition, although in certain cases they are isomorphic). We should write down a relative version of this as well. Suppose $L\subseteq K$ be closed. Define $\cHH^p(K,L)=\varinjlim_{(U,V)\in\mathcal{U}_{K,L}} H^p(U,V)$ where $K\supseteq L$ and $K\subseteq U$, and $L\subseteq V$ such that $V\subseteq U$. Here $\mathcal{U}_{K,L}$ is the directed set:
\begin{equation*}
\mathcal{U}_{K,L}\text{ is the set of }\xymatrix{K\ar@{^(->}[d] & L\ar@{^(->}[d]\ar@{^(->}[l] \\ U & V\ar@{^(->}[l]}
\end{equation*}
such that $U$ and $V$ are open.
\begin{theorem}
There is a lexseq:
\begin{equation*}
\cdots\to\cHH^p(K,L)\to\cHH^p(K)\to\cHH^p(L)\xrightarrow{\delta}\cHH^{p+1}(K,L)\to\cdots
\end{equation*}
\end{theorem}
All the maps in the lexseq are mysterious.

Also, excision holds:
\begin{theorem}[Excision]
Suppose $A,B\subseteq X$ are closed. Then $\cHH^p(A\cup B,A)\cong\cHH^p(B,A\cap B)$. This is a form of excision that doesn't hold for ordinary cohomology.
\end{theorem}
So \v{C}ech cohomology is better suited for talking about closed sets.

We defined $\cap:\cHH^p(K)\otimes H_n(X,X-K)\to H_q(X,X-K)$, such that $p+q=n$. Fix $x_K\in H_n(X,X-K)$. Then capping with $x_K$ gives a map $\cap x_k:\cHH^p(K)\to H_q(X,X-K)$.

Now, if $K\supseteq L$, then $X-K\subseteq X-L$, so I get $H_n(X,X-K)\xrightarrow{i_\ast} H_n(X,X-L)$. Then $x_K\mapsto x_L$. We can now extend our lexseq:
\begin{equation*}
\xymatrix{
	\cdots\ar[r] & \cHH^p(K,L)\ar[r] & \cHH^p(K)\ar[r]\ar[d]_{-\cap x_k} & \cHH^p(L)\ar[r]^\delta\ar[d]_{-\cap x_L} & \cHH^{p+1}(K,L)\ar[r] & \cdots\\
	& H_q(X-L,X-K)\ar[r]& H_q(X,X-K)\ar[r] & H_q(X,X-L)\ar[r]^\partial & H_{q-1}(X-L,X-K)\ar[r] & \cdots
}
\end{equation*}
Where the bottom thing comes from the lexseq of the triple (see exercise 9 on your pset) $X-K\subseteq X-L\subseteq X$ (I think). Then we can extend our theorem on the lexseq:
\begin{theorem}
There is a lexseq and a ladder:
\begin{equation*}
\xymatrix{
	\cdots\ar[r] & \cHH^p(K,L)\ar[r]\ar@{-->}[d]_{-\cap x_{K}} & \cHH^p(K)\ar[r]\ar[d]_{-\cap x_k} & \cHH^p(L)\ar[r]^\delta\ar[d]_{-\cap x_L} & \cHH^{p+1}(K,L)\ar[r]\ar[d]_{-\cap x_{K}} & \cdots\\
	\cdots\ar[r] & H_q(X-L,X-K)\ar[r]& H_q(X,X-K)\ar[r] & H_q(X,X-L)\ar[r]^\partial & H_{q-1}(X-L,X-K)\ar[r] & \cdots
}
\end{equation*}
\end{theorem}
What I have to do is define a cap product of the following form (bottom row):
\begin{equation*}
\xymatrix{
	\cHH^p(K)\otimes H_n(X,X-K)\ar[r]^{\cap} & H_q(X,X-K)\\
	\cHH^p(K,L)\otimes H_n(X,X-K)\ar[u]\ar[r]^{\cap} & H_q(X-L,X-K)
}
\end{equation*}
(where $p+q=n$)

I want to define this fully relative cap product $\cHH^p(K,L)\otimes H_n(X,X-K)\to H_q(X-L,X-K)$. We'll use this in the inductive proof of (some) important theorem.

Our map $\cHH^p(K)\otimes H_n(X,X-K)\to H_q(X,X-K)$ came from $S^p(U)\otimes S_n(U,U-K)\to S_q(U,U-K)$ where $U\supseteq K$, defined via $\beta\otimes\sigma\mapsto\beta(\sigma\circ\alpha_p)\cdot(\sigma\circ\omega_q)$. I'm hoping to get:
\begin{equation*}
\xymatrix{
	S^p(U)\otimes S_n(U,U-K)\ar[r] & S_q(U,U-K)\\
	S^p(U,V)\otimes S_n(U-L)/S_n(U-K)\ar[r]\ar[u] & S_q(U-L)/S_q(U-K)\ar[u]
}
\end{equation*}
where again we have inclusions ($U,V$ open and $K,L$ closed):
\begin{equation*}
\xymatrix{K\ar@{^(->}[d] & L\ar@{^(->}[d]\ar@{^(->}[l] \\ U & V\ar@{^(->}[l]}
\end{equation*}
The bottom map $S^p(U,V)\otimes S_n(U-L)/S_n(U-K)\to S_q(U-L)/S_q(U-K)$ makes sense. We can evaluate a cochain that kills everything on $V$. This means that we can add in $S_n(V)$ to get $S^p(U,V)\otimes (S_n(U-L)+S_n(V))/S_n(U-K)\to S_q(U-L)/S_q(U-K)$ by sending $\beta\otimes\tau\mapsto 0$ where $\tau:\Delta^n\to V$. This means that the diagram:
\begin{equation*}
\xymatrix{
	S^p(U)\otimes S_n(U,U-K)\ar[r] & S_q(U,U-K)\\
	S^p(U,V)\otimes (S_n(U-L)+S_n(V))/S_n(U-K)\ar[r]\ar[u] & S_q(U-L)/S_q(U-K)\ar[u]
}
\end{equation*}
commutes. It's not that far off from where we want to go.

Now, $(U-L)\cup V=U$. I have this covering of $U$ by two open sets. In $S_n(U-L)+S_n(V)$ we're taking the sum of $n$-chains. We have a map $S_\ast(U-L)+S_\ast(V)\to S_\ast(U)$. We have already worked through this -- the locality principle! This tells us that $S_\ast(U-L)+S_\ast(V)\to S_\ast(U)$ is a homotopy equivalence. Hence we can extend our diagram:
\begin{equation*}
\xymatrix{
	S^p(U)\otimes S_n(U,U-K)\ar[r] & S_q(U,U-K)\\
	S^p(U,V)\otimes (S_n(U-L)+S_n(V))/S_n(U-K)\ar[r]\ar[d]^{\simeq}\ar[u] & S_q(U-L)/S_q(U-K)\ar[u]\\
	S^p(U,V)\otimes S_n(U)/S_n(U-K) &
}
\end{equation*}
We want the homology of $S_n(U)/S_n(U-K)$ to approximate $H_n(X,X-K)$.
\begin{claim}
There is an isomorphism $H_n(S_\ast(U)/S_\ast(U-K))=H_n(U,U-K)\to H_n(X,X-K)$.
\end{claim}
\begin{proof}
This is exactly excision! Remember our recasting of excision in the previous lecture.
\end{proof}
This means that what we've constructed really \emph{is} what we want! We now have our large lexseq:
\begin{equation*}
\xymatrix{
	\cdots\ar[r] & \cHH^p(K,L)\ar[r]\ar@{-->}[d]_{-\cap x_{K}} & \cHH^p(K)\ar[r]\ar[d]_{-\cap x_k} & \cHH^p(L)\ar[r]^\delta\ar[d]_{-\cap x_L} & \cHH^{p+1}(K,L)\ar[r]\ar[d]_{-\cap x_{K}} & \cdots\\
	\cdots\ar[r] & H_q(X-L,X-K)\ar[r]& H_q(X,X-K)\ar[r] & H_q(X,X-L)\ar[r]^\partial & H_{q-1}(X-L,X-K)\ar[r] & \cdots\\
	\cdots\ar[r] & H_q(U-L,U-K)\ar[r]\ar[u]^{\cong,\text{ five-lemma}} & H_q(U,U-K)\ar[r]\ar[u]^{\cong,\text{ locality}} & H_q(U,U-L)\ar[r]\ar[u]^{\cong,\text{ locality}} & \cdots
}
\end{equation*}
As desired.

The diagram:
\begin{equation*}
\xymatrix{
	\cHH^p(L)\ar[r]^\delta\ar[d]^{-\cap x_L} & \cHH^{p+q}(K,L)\ar[d]^{-\cap x_K}\\
	H_q(X,X-L)\ar[r]^{\partial} & H_{q-1}(X-L,X-K)
}
\end{equation*}
says that:
\begin{equation*}
(\delta b)\cap x_k=\partial(b\cap x_L)
\end{equation*}
It's rather wonderful! You have a decreasing sequence below and an increasing one above.

I want to reformulate all of this in a more useful fashion, from Mayer-Vietoris. We had two different proofs, one from locality, and another one that we'll remind you of:
\begin{equation*}
\xymatrix{
	\cdots\ar[r] & A_n\ar[r]\ar[d] & B_n\ar[r]\ar[d]^\cong & C_n\ar[r]\ar[d] & A_{n-1}\ar[r]\ar[d] & \cdots\\
	\cdots\ar[r] & A_n^\prime\ar[r] & B_n^\prime\ar[r] & C^\prime_n\ar[r] & A^\prime_{n-1}\ar[r] & \cdots
}
\end{equation*}
then you get a lexseq:
\begin{equation*}
\cdots\to C_{n+1}\to C^\prime_{n+1}\oplus A_n\to A^\prime_n\xrightarrow{\partial} C_n\to\cdots
\end{equation*}
You can use this to prove Mayer-Vietoris -- I will do this in a special case. (This is exactly what I did in a homework assignment\footnote{Suppose $A\subseteq X$ is a subspace of $X$. Then there is a lexseq in reduced homology $\cdots\to \widetilde{ H}_n(A)\to \widetilde{ H}_n(X)\to H_n(X,A)\to\widetilde{ H}_{n-1}(A)\to\cdots$ that can be obtained by using the lexseq in homology of the sexseq $0\to\widetilde{S}_\ast(A)\to\widetilde{S}_\ast(X)\to S_\ast(X,A)\to 0$.

Now suppose $X=A\cup B$. Consider the ladder:
\begin{equation*}
\xymatrix@C=10pt{\cdots\ar[r] & H_{n+1}(A,A\cap B)\ar[r]\ar[d] & \widetilde{ H}_n(A\cap B)\ar[r]\ar[d] & \widetilde{ H}_n(A)\ar[r]\ar[d] & H_n(A,A\cap B)\ar[r]\ar[d] & \cdots\\
\cdots\ar[r] & H_{n+1}(X,B)\ar[r] & \widetilde{ H}_n(B)\ar[r] & \widetilde{ H}_n(X)\ar[r] & H_n(X,B)\ar[r] & \cdots}
\end{equation*}
The first and fourth maps as shown are isomorphisms because of excision. The lexseq from the ladder (see above) therefore yields the Mayer-Vietoris sequence $\cdots\to \widetilde{ H}_n(A\cap B)\to \widetilde{ H}_n(B)\oplus \widetilde{ H}_n(A)\to \widetilde{ H}_n(X)\to \widetilde{ H}_{n-1}(A\cap B)\to\cdots$.}!) We have a ladder of lexseqs:
\begin{equation*}
\xymatrix{
	\cdots\ar[r] & H_q(X,X-A\cup B)\ar[r]\ar[d] & H_q(X,X-A)\ar[r]\ar[d] & H_{q-1}(X-A,X-A\cup B)\ar[r]\ar[d]^{\cong,\text{ excision}} & \cdots\\
	\cdots\ar[r] & H_q(X,X-B)\ar[r] & H_q(X,X-A\cap B)\ar[r] & H_{q-1}(X-A\cap B,X-B)\ar[r] & \cdots
}
\end{equation*}
This means that (using the lexseq of the ladder) you have a lexseq:
\begin{equation*}
\cdots\to H_q(X,X-A\cup B)\to H_q(X,X-A)\oplus H_q(X,X-B)\to H_q(X,X-A\cap B)\to H_{q-1}(X,X-A\cup B)\to\cdots
\end{equation*}
This can be used to give a lexseq for \v{C}ech cohomology:
\begin{equation*}
\cdots\to \cHH^p(A\cup B)\to \cHH^p(A)\oplus \cHH^p(B)\to \cHH^p(A\cap B)\to \cHH^{p+q}(A\cup B)\to\cdots
\end{equation*}
so that we're going to get a commutative Mayer-Vietoris ladder:
\begin{theorem}
There's a ``Mayer-Vietoris'' ladder:
\begin{equation*}
\xymatrix{
	\to\cHH^p(A\cup B)\ar[r]\ar[d]^{-\cap x_{A\cup B}} & \cHH^p(A)\oplus \cHH^p(B)\ar[r]\ar[d]^{(-\cap x_A)\oplus -\cap x_B} & \cHH^p(A\cap B)\ar[r]\ar[d] & \cHH^{p+q}(A\cup B)\ar[r]\ar[d] & \cdots\\
	\to H_q(X,X-A\cup B)\ar[r]& H_q(X,X-A)\oplus H_q(X,X-B)\ar[r]& H_q(X,X-A\cap B)\ar[r]& H_{q-1}(X,X-A\cup B)\ar[r]&\cdots
}
\end{equation*}
where I have four cohomology classes $x_{A \cup B},x_A,x_B,x_{A\cap B}$ that commute in:
\begin{equation*}
\xymatrix{
	 & H_n(X,X-A)\ar[dr] & \\
	H_n(X,X-A\cap B)\ar[ur]\ar[dr] & & H_n(X,X-A\cap B)\\
	 & H_n(X,X-B)\ar[ur]
}
\end{equation*}
\end{theorem}
This is the most complicated blackboard for the rest of the course. Also xymatrix is not compiling properly because the diagram is too big!


\section{Introduction}

\noindent Catalan numbers were introduced with this name by
Eugene Catalan in $1838$, although they have been used even before by Ming Antu
in $1730$. Let $C_{n}$ be the $n$-th Catalan number, then the following
table shows the first fifteen element of the infinite sequence
$\left(C_{n}\right)_{n\in\mathbb{N}}$,
\begin{displaymath}
    \footnotesize
    \begin{array}{c|ccccccccccccccc}
        n & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 \\
        \hline
        C_{n} & 1 & 1 & 2 & 5 & 14 & 42 & 132 & 429 & 1430 & 4862 & 16796 & 58786 & 208012 & 742900 & 2674440
    \end{array}
\end{displaymath}
known as $A000108$\footnote{Sequences mentioned in this paper are
identified by keys of the form $Annnnnn$ that refer to entries in the
encyclopedia.}in the Online Encyclopedia of Integer Sequences \cite{sloane:oeis}.

A comprehensive manuscript having origin in $1970$, entirely devoted to these
numbers, has been recently published by Stanley \cite{stanley:2015} where it is
possible to find their history, formal characterizations and $214$
combinatorial interpretations.  For the sake of clarity, we report three
classes of objects counted by $C_{n}$ and many more can be found in
\cite{ConcreteMath}: first, binary trees with $n$ nodes; second, admissible
bracketing of a string with length $n+1$ using $n-1$ pairs of curly braces;
third, Dyck paths of length $2n$ using raising $\diagup$ and falling
$\diagdown$ steps.

It is possible to augment $C_{n}$ with an additional dimension, obtaining
coefficients $C_{n,k}$ defining a lower triangular, infinite matrix
$\mathcal{C}$ ($A033184$): in \autoref{tab:catalan:array} we report the upper
chunk of that matrix where a coefficient at row $n$ and column $k$ can be
interpreted as the number of Dyck paths of length $2(n+1)$ with $k$ returns to
the ground, start and end points excluded -- conventionally, indexes are
$0$-based.

\input{catalan/catalan-traditional-standard-ignore-negatives-centered-colouring-127-rows-mod2-partitioning-include-matrix.tex}

In the literature, matrix $\mathcal{C}$ is known as \emph{Catalan triangle} and
it turns out to be a Riordan array. Those arrays form a family of matrices
enjoying nice properties and, looking at them from the perspective of abstract
algebra, they are a \textit{group}. Formally, a Riordan array $\mathcal{R}$ is
denoted by a pair of functions $d$ and $h$ which provides a unique
characterization for the generic coefficient $d_{n,k}\in\mathcal{R}$ lying at
row $n$ and column $k$, by means of the $n$-th coefficient extraction from
$\displaystyle d_{n,k} = [t^{n}]d(t)h(t)^{k}$.
Riordan arrays have been introduced by Shapiro et al. \cite{shapiro:1991} in $1991$;
thereafter, in $1994$, Sprugnoli \cite{sprugnoli:1991} pointed out their importance
to solve combinatorial sums.

In this paper we apply a modular transformation to $\mathcal{C}$ which maps
every coefficient to its remainder with respect to the congruence relation
$\equiv_{2}$: the resulting matrix is denoted by $\mathcal{C}_{\equiv_{2}}$ and
is graphically represented in
\autoref{fig:catalan-traditional-standard-ignore-negatives-centered-colouring-127-rows-mod2-partitioning-triangle}.
This is a parallel study of the same modular transformation applied to the
Pascal array $\mathcal{P}$, represented in
\autoref{fig:pascal-standard-handle-negatives-centered-colouring-127-rows-mod2-partitioning-triangle},
where a coefficient lying at row $n$ and column $k$ counts the number of
subsets with $k$ elements out of a set with $n$ elements, ${{n}\choose{k}}$ in
symbols.  In both pictures, we center the root of the triangle and represent
every coefficient with a colored dot, using colors blue and orange for
\textcolor{blue}{\emph{even}} and \textcolor{orange}{\emph{odd}} remainders,
respectively.

Although the array $\mathcal{P}_{\equiv_{2}}$ has been deeply studied already,
because of both its (i)~relation to congruences about binomial coefficients and
(ii)~connection with the Sierpinski gasket \cite{sokolov,
stewart:four:encounters:sierpinski}, to the best of our knowledge the array
$\mathcal{C}_{\equiv_{2}}$ seems to be fresh and our approach interleaves with
studies about modular transformations of Catalan numbers
\cite{alter:kubota:prime:power:catalan:divisibility,
egecioglu:parity:via:lattice:paths,
konvalinka:divisibility:generalized:catalan:numbers}, where their remainders
have been characterized both arithmetically and combinatorially.

The present study deepens our previous work \cite{merlini:nocentini:lecco} and
takes the long track toward to a formal characterization of
$\mathcal{C}_{\equiv_{2}}$; first, using the Riordan array definition of
$\mathcal{C}$ we find different closed formulae for the coefficient
$c_{n,k}\in\mathcal{C}$; second, we prove congruences over coefficients lying
in different regions of the matrix; third, we use such congruences to implement
an efficient procedure that builds $\mathcal{C}_{\equiv_{2}}$ inductively.
To support our work, a bunch of functions have been implemented using the
Python programming language on top of the Sage framework \cite{sage}, that
allow us (i)~to table a Riordan array the hard way by series expansion, (ii)~to
generate the \LaTeX\,code of graphical representations of triangles and, finally,
(iii)~to build $\mathcal{C}_{\equiv_{2}}$ in a cheaper way.

\input{catalan/catalan-traditional-standard-ignore-negatives-centered-colouring-127-rows-mod2-partitioning-include-figure.tex}
\input{pascal/pascal-standard-handle-negatives-centered-colouring-127-rows-mod2-partitioning-include-figure}

Our work is organized as follows: the current section introduces Catalan
numbers and the concept of Riordan arrays together with the modular
transformation under study; \Cref{sec:previous:work} recalls important results
from the existing literature about the application of modular arithmetic to
infinite, lower triangular matrices; \Cref{sec:C:precisely} formally defines
the Catalan array $\mathcal{C}$ and presents some facts about it;
\Cref{sec:catalan:characterization} takes the matrix $\mathcal{C}_{\equiv_{2}}$
apart in order to reason on it piece-wise; finally, \Cref{sec:conclusions}
concludes with final comments and leaves open questions.

\vfill


\documentclass[12pt]{article}

\usepackage[left=1cm,right=1cm,
    top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage[T2A]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\parindent=0.5cm

\usepackage{hyperref}
\usepackage{indentfirst}

\numberwithin{equation}{section}
%\usepackage{showkeys}

\begin{document}
\section*{MSAI Statistics \& Probability – Week 6 Seminar \& HW}\\

\textbf{Problem 1:} Consider an Erd\H{o}s-R\'{e}nyi random graph $G(n,p)$ on $n$ vertices (that is, for any two vertices, there is an edge between them with probability $p$ and no edge with probability $1-p$; all edges are independent of each other). Find the \textbf{variance} of the number of triangles in $G(n,p)$ (triples of vertices all pairwise connected with an edge). (In previous week HW you had to find the \textbf{expectation}).
\\

\textbf{Problem 2:} Find $\textrm{Var}(\xi)$ if $\xi$ have the following distribution function:
$$F(x)=
\begin{cases}
0, ~~~ x<-2,\\
1/5, ~~~-2\leq x<1 \\
x^2/4, ~~~1\leq x<2 \\
1, ~~~ x\geq 2
\end{cases}$$
(In previous week HW you had to find the expectation of $\xi$).
\\

\textbf{Problem 3:} Can you think of two such \textbf{dependent} random variables $\xi,~\eta$, such that $\textrm{Cov}(\xi,\eta)=0$?

\end{document}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{ambifuwb}
\section*{\hspace*{-1.6cm} ambifuwb}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Wide-band ambiguity function.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[waf,tau,theta] = ambifuwb(x)
[waf,tau,theta] = ambifuwb(x,fmin,fmax)
[waf,tau,theta] = ambifuwb(x,fmin,fmax,N)
[waf,tau,theta] = ambifuwb(x,fmin,fmax,N,trace)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty ambifuwb} calculates the asymetric wide-band ambiguity
        function, defined as
\begin{eqnarray*}
\Xi_x(a,\tau) = \frac{1}{\sqrt{a}}\ \int_{-\infty}^{+\infty} x(t)\
x^*(t/a-\tau)\ dt = \sqrt{a} \int_{-\infty}^{+\infty} X(\nu)\ X^*(a\nu)\
e^{j2\pi a \tau\nu}\ d\nu.
\end{eqnarray*}

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & signal (in time) to be analyzed (the analytic associated
                signal is considered), of length {\ty Nx} &\\
        {\ty fmin, fmax} & respectively lower and upper frequency bounds of
                the analyzed signal. When specified, these parameters fix
                the equivalent frequency bandwidth (both are expressed in
                Hz)             & {\ty 0, 0.5}\\
        {\ty N}     & number of Mellin points. This number is needed when {\ty fmin}
                and {\ty fmax} are forced     & {\ty Nx}\\
        {\ty trace} & if non-zero, the progression of the algorithm is shown
                                        & 0\\
\hline
        {\ty waf}    & matrix containing the coefficients of the ambiguity
                function. X-coordinate corresponds to the dual variable of
                scale parameter ; Y-coordinate corresponds to time delay,
                dual variable of frequency.\\
        {\ty tau}   & X-coordinate corresponding to time delay\\
        {\ty theta} & Y-coordinate corresponding to the $\log(a)$ variable,
		where $a$ is the scale\\
\hline
\end{tabular*}
\vspace*{.2cm}

When called without output arguments, {\ty ambifuwb} displays the squared
modulus of the ambiguity function by means of {\ty contour}.
\end{minipage}

\newpage

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Consider a BPSK signal (see {\ty anabpsk}) of 256 points, with a keying
period of 8 points, and analyze it with the wide-band ambiguity
function\,:
\begin{verbatim}
         sig=anabpsk(256,8);
         ambifunb(sig);
\end{verbatim}
The result, to be compared with the one obtained with the narrow-band
ambiguity function, presents a thin high peak at the origin of the
ambiguity plane, but with more important sidelobes than with the
narrow-band ambiguity function. It means that the narrow-band assumption is
not very well adapted to this signal, and that the ambiguity in the
estimation of its arrival time and mean frequency is not so small.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
ambifunb.
\end{verbatim}
\end{minipage}


\section{Grothendieck's construction of Chern classes}
%No class Monday because I'll be out!
%We have a few lectures left and I'll use them to talk about characteristic classes, and we'll see some applications.
%Maybe we'll have time to give a construction of Steenrod operations as well.
\subsection{Generalities on characteristic classes}
We would like to apply algebraic techniques to study $G$-bundles on a space.
Let $A$ be an abelian group, and $n\geq 0$ an integer.
\begin{definition}
    A \emph{characteristic class} for principal $G$-bundles (with values in
    $H^n(-;A)$) is a natural transformation of functors $\Top\to \Ab$:
    $$\Bun_G(X) \xar{c} H^n(X;A)$$
    Concretely: if $P\to Y$ is a principal $G$-bundle over a space $X$, and
    $f:X\to Y$ is a continuous map of spaces, then
    $$c(f^\ast P) = f^\ast c(P).$$
\end{definition}
The motivation behind this definition is that $\Bun_G(X)$ is still rather
mysterious, but we have techniques (developed in the last section) to compute
the cohomology groups $H^n(X;A)$. It follows by construction that if two
bundles over $X$ have two different characteristic classes, then they cannot be
isomorphic. Often, we can use characteristic classes to distinguish a given
bundle from the trivial bundle.

\begin{example}
    The Euler class takes an oriented real $n$-plane vector bundle (with a
    chosen orientation) and produces an $n$-dimensional cohomology class
    $e:\Vect^{or}_n(X) = \Bun_{SO(n)}(X)\to H^n(X;\Z)$. This is a
    characteristic class. To see this, we need to argue that if $\xi\downarrow
    X$ is a principal $G$-bundle, we can pull the Euler class back via $f:X \to
    Y$. The bundle $f^\ast\xi\downarrow Y$ has a orientation if $\xi$ does, so
    it makes sense to even talk about the Euler class of $f^\ast\xi$. Since all
    of our constructions were natural, it follows that $e(f^\ast\xi) = f^\ast
    e(\xi)$.

    Similarly, the {mod $2$ Euler class} is $e_2:\Vect_n(X) = \Bun_{O(n)}(X)
    \to H^n(X;\Z/2\Z)$ is another Euler class. Since everything has an
    orientation with respect to $\Z/2\Z$, the mod $2$ Euler class is
    well-defined.
\end{example}

By our discussion in \S \ref{classifying-g-bundles}, we know that $\Bun_G(X) =
[X,BG]$. Moreover, as we stated in Theorem \ref{brown-rep}, we know that
$H^n(X;A) = [X,K(A,n)]$ (at least if $X$ is a CW-complex). One moral reason for
cohomology to be easier to compute is that the spaces $K(A,n)$ are infinite
loop spaces (i.e., they can be delooped infinitely many times). It follows from
the Yoneda lemma that characteristic classes are simply maps $BG\to K(A,n)$,
i.e., elements of $H^n(BG;A)$.

\begin{example}
    The Euler class $e$ lives in $H^n(BSO(n);\Z)$; in fact, it is $e(\xi)$, the
    Euler class of the universal oriented $n$-plane bundle over $BSO(n)$. A
    similar statement holds for $e_2\in H^n(BO(n);\Z/2\Z)$. For instance, if
    $n=2$, then $SO(2) = S^1$. It follows that
    $$BSO(2) = BS^1 = \CP^\infty.$$
    We know that $H^\ast(\CP^\infty;\Z) = \Z[e]$ --- it's the polynomial
    algebra on the ``universal'' Euler class! Similarly, $O(1) = \Z/2\Z$, so
    $$BO(1) = B\Z/2 = \RP^\infty.$$
    We know that $H^\ast(\RP^\infty;\FF_2) = \FF_2[e_2]$ --- as above, it is
    the polynomial algebra over $\Z/2\Z$ on the ``universal'' mod $2$ Euler
    class.
\end{example}

\subsection{Chern classes}
These are one of the most fundamental example of characteristic classes.
\begin{theorem}[Chern classes]\label{chern-classes}
    There is a unique family of characteristic classes for complex vector
    bundles that assigns to a complex $n$-plane bundle $\xi$ over $X$ the
    \emph{$n$th Chern class} $c^{(n)}_k(\xi)\in H^{2k}(X;\Z)$, such that:
    \begin{enumerate}
	\item $c^{(n)}_0(\xi) = 1$.
	\item If $\xi$ is a line bundle, then $c^{(1)}_1(\xi) = -e(\xi)$.
	\item The \emph{Whitney sum formula} holds: if $\xi$ is a $p$-plane
	    bundle and $\eta$ is a $q$-plane bundle (and if $\xi\oplus\eta$
	    denotes the fiberwise direct sum), then
	    \begin{equation*}
		c^{(p+q)}_k(\xi\oplus \eta) = \sum_{i+j=k} c^{(p)}_i(\xi)\cup
		c^{(q)}_j(\eta) \in H^{2k}(X;\Z).
	    \end{equation*}
    \end{enumerate}
    Moreover, if $\xi_n$ is the universal $n$-plane bundle, then
    $$H^\ast(BU(n);\Z) \simeq \Z[c_1^{(n)}, \cdots, c^{(n)}_n],$$
    where $c^{(n)}_k = c^{(n)}_k(\xi_n)$.
\end{theorem}
This result says that all characteristic classes for complex vector bundles are
given by polynomials in the Chern classes because the cohomology of $BU(n)$
gives all the characteristic classes.  It also says that there are no universal
algebraic relations among the Chern classes: you can specify them
independently.

\begin{remark}
    The $(p+q)$-plane bundle $\xi_p\times \xi_q = \pr_1^\ast \xi_p\oplus
    \pr_2^\ast\xi_q$ over $BU(p)\times BU(q)$ is classified by a map
    $BU(p)\times BU(q) \xar{\mu} BU(p+q)$. The Whitney sum formula computes the
    effect of $\mu$ on cohomology:
    $$\mu^\ast(c^{(n)}_k) = \sum_{i+j = k} c^{(p)}_i \times c^{(q)}_j \in
    H^{2k}(BU(p)\times BU(q)),$$
    where, you'll recall,
    $$x\times y := \pr_1^\ast x \cup \pr_2^\ast y.$$
\end{remark}

The Chern classes are ``stable'', in the following sense. Let $\epsilon$ be the
trivial one-dimensional complex vector bundle, and let $\xi$ be an
$n$-dimensional vector bundle. What is $c^{(n+q)}_k(\xi\oplus\epsilon^q)$? For
this, the Whitney sum formula is valuable.

The trivial bundle is characterized by the pullback:
\begin{equation*}
    \xymatrix{
	X\times \cC^n = n\epsilon \ar[r]\ar[d] & \cC^n\ar[d]\\
	X\ar[r] & \ast
    }
\end{equation*}
By naturality, we find that if $k>0$, then $c^{(n)}_k(n\epsilon) = 0$. The
Whitney sum formula therefore implies that
$$c^{(n+q)}_k(\xi\oplus \epsilon^q) = c^{(n)}_k(\xi).$$
This phenomenon is called stability: the Chern class only depends on the
``stable equivalence class'' of the vector bundle (really, they are only
defined on ``K-theory'', for those in the know). For this reason, we will drop
the superscript on $c^{(n)}_k(\xi)$, and simply write $c_k(\xi)$.

\subsection{Grothendieck's construction}\label{grothendieck-chern}
Let $\xi$ be an $n$-plane bundle. We can consider the vector bundle
$\pi:\PP(\xi)\to X$, the projectivization of $\xi$: an element of the fiber of
$\PP(\xi)$ over $x\in X$ is a line inside $\xi_x$, so the fibers are therefore
all isomorphic to $\CP^{n-1}$.

Let us compute the cohomology of $\PP(\xi)$. For this, the Serre spectral
sequence will come in handy:
$$
E_2^{s,t} = H^s(X; H^t(\CP^{n-1})) \Rightarrow H^{s+t}(\PP(\xi)).
$$
\begin{remark}
    Why is the local coefficient system constant? The space $X$ need not be
    simply connected, but $BU(n)$ is simply connected since $U(n)$ is simply
    connected. Consider the projectivization of the universal bundle
    $\xi_n\downarrow BU(n)$; pulling back via $f:X\to BU(n)$ gives the bundle
    $\pi:\PP(\xi)\to X$. The map on fibers $H^\ast(\PP(\xi_n)_{f(x)}) \to
    H^\ast(\PP(\xi_n)_{x})$ is an isomorphism which is equivariant with respect
    to the action of the fundamental group of $\pi_1(X)$ via the map $\pi_1 (X)
    \to \pi_1(BU(n)) = 0$.
\end{remark}
Because $H^\ast(\CP^{n-1})$ is torsion-free and finitely generated in each
dimension, we know that
$$E_2^{s,t} \simeq H^s(X) \otimes H^t(\CP^{n-1}).$$
The spectral sequence collapses at $E_2$, i.e., that $E_2 \simeq E_\infty$,
i.e., there are no differentials. We know that the $E_2$-page is generated as
an algebra by elements in the cohomology of the fiber and elements in the
cohomology of the base. Thus, it suffices to check that elements in the
cohomology of the fiber survive to $E_\infty$. We know that
$$E_2^{0,2t} = \Z\langle x^t\rangle,\text{ and }E_2^{0,2t+1} = 0,$$
where $x = e(\lambda)$ is the Euler class of the canonical line bundle
$\lambda\downarrow\CP^{n-1}$.

In order for the Euler class to survive the spectral sequence, it suffices to
come up with a two dimensional cohomology class in $\PP(\xi)$ that restricts to
the Euler class over $\CP^{n-1}$. We know that $\lambda$ itself is the
restriction of the tautologous line bundle over $\CP^\infty$. There is a
tautologous line bundle $\lambda_\xi \downarrow \PP(\xi)$, given by the
tautologous line bundle on each fiber. Explicitly:
$$E(\lambda_\xi)=\{(\ell,y)\in \PP(\xi)\times_X E(\xi) | y\in \ell\subseteq
\xi_x\}.$$
Thus, $x$ is the restriction $e(\lambda_\xi)|_\text{fiber}$ of the Euler class
to the fiber. It follows that the class $x$ survives to the $E_\infty$-page.

Using the Leray-Hirsch theorem (Theorem \ref{leray-hirsch}), we conclude that
$$H^\ast(\PP(\xi)) = H^\ast(X)\langle 1, e(\lambda_\xi), e(\lambda_\xi)^2,
\cdots, e(\lambda_\xi)^{n-1}\rangle.$$
For simplicity, let us write $e = e(\lambda_\xi)$. Unforunately, we don't know
what $e^n$ is, although we do know that it is a linear combination of the $e^k$
for $k<n$. In other words, we have a relation
$$e^n + c_1e^{n-1} + \cdots + c_{n-1} e + c_n = 0,$$
where the $c_k$ are elements of $H^{2k}(X)$. These are the Chern classes of
$\xi$. By construction, they are unique!

To prove Theorem \ref{chern-classes}(2), note that when $n=1$ the above
equation reads
$$e+c_1 = 0,$$
as desired.

%\begin{question}
%    Why do we know that they are the Chern classes?
%    My margin is too narrow to provide a proof.
%\end{question}
%We'll prove this on Wednesday.


\documentclass[modern]{aastex62}

\usepackage{amsmath}
\usepackage{amssymb}

% Derivatives
\newcommand{\dd}{\mathrm{d}}
\newcommand{\diff}[2]{\frac{\dd #1}{\dd #2}}

% Abbreviated Variables
\newcommand{\Ndet}{N_\mathrm{det}}
\newcommand{\Nndet}{N_\mathrm{ndet}}
\newcommand{\Nnobs}{N_\mathrm{nobs}}
\newcommand{\Nobs}{N_\mathrm{obs}}
\newcommand{\Ntotal}{N_\mathrm{total}}

% Vector shortcuts
\newcommand{\vd}{\vec{d}}
\newcommand{\vlambda}{\vec{\lambda}}
\newcommand{\vtheta}{\vec{\theta}}

\begin{document}
\title{Incorporating Selection Effects in Population Analyses}

\author[0000-0003-1540-8562]{Will M. Farr}
\email{will.farr@stonybrook.edu}
\affiliation{Department of Physics and Astronomy, Stony Brook University, Stony Brook NY 11794, USA}
\affiliation{Center for Computational Astronomy, Flatiron Institute, 162 5th Ave., New York NY 10010, USA}

\author[0000-0002-6134-8946]{Ilya Mandel}
\email{imandel@star.sr.bham.ac.uk}
\affiliation{Birmingham Institute for Gravitational Wave Astronomy and School of Physics and Astronomy, University of Birmingham, Birmingham, B15 2TT, United Kingdom}

\author{Jonathan R. Gair}
\email{J.Gair@ed.ac.uk}

\section*{}

Consider a population of objects, each described by some set of parameters
$\vtheta$, that follows a number density $\diff{N}{\vtheta}$.  Let the number
density be parameterized by parameters, $\vlambda$.  For each object in the
population we make a noisy measurement of $\vtheta$, represented by a likelihood
function relating the measured data, $\vd$, to the parameters of the object,
$\vtheta$: $p\left( \vd \mid \vtheta \right)$.  If we have observed a
representative sample (i.e.\ a ``fair draw''), then the appropriate
(unnormalised) joint distribution for the parameters $\left\{ \vtheta_i
\right\}_{i=1}^{\Ntotal}$ and observations $\left\{ \vd_i \right\}$ of the $i =
1, \ldots, \Ntotal$ objects given the parameters $\vlambda$ describing the
population
%
\begin{equation}
  \pi\left(\left\{ \vtheta_i \right\}, \left\{ d_i \right\} \mid \vlambda \right) \propto \left[ \prod_{i=1}^{\Ntotal} p\left( \vd_i \mid \vtheta_i \right) \diff{N}{\vtheta_i}\left( \vlambda \right) \right] \exp\left[ - N\left( \vlambda \right) \right],
\end{equation}
%
where
%
\begin{equation}
N\left( \vlambda \right) \equiv \int \dd \vd \, \dd \vtheta p\left( \vd \mid \vtheta \right) \diff{N}{\vtheta}\left( \lambda \right)
\end{equation}
%
is the expected number of objects in the population\footnote{The rationale for
writing this as a double-integral, when the integral over $\vd$ is in fact
trivial---since the likelihood is normalised over $\vd$---will become apparent
below.}.  This is the standard posterior for a hierarchical analysis of an
inhomogeneous Poisson process
\citep{Loredo1995,Hogg2010,Mandel2010,Youdin2011,Foreman-Mackey2014,Farr2015,Barrett2018}.

Now suppose that, based on the observed data, some objects are classed as
``observable'' and others are ``un-observable.''  For example, a survey may
impose a per-pixel or per-apeture threshold on the flux for inclusion of
point-sources in a catalog, or a gravitational wave detector my only report
events whose signal-to-noise ratio rises above some predetermined threshold,
or\ldots.  The key point here is that the selection of ``observable'' objects is
made by examining the data, $\vd_i$, for each object; this is by far the most
common case for astronomical observations.  Then the complete set of
observations partitions into two subsets:
%
\begin{multline}
  \pi\left(\left\{ \vtheta_i \right\}, \left\{ \vtheta_j \right\}, \left\{ d_i \right\}, \left\{ d_j \right\} \mid \vlambda \right) \propto \left[ \prod_{i=1}^{\Nobs} p\left( \vd_i \mid \vtheta_i \right) \diff{N}{\vtheta_i}\left( \vlambda \right) \right] \\ \times \left[ \prod_{j=1}^{\Nnobs} p\left( \vd_j \mid \vtheta_j \right) \diff{N}{\vtheta_j}\left( \vlambda \right) \right] \exp\left[ - N\left( \vlambda \right) \right].
\end{multline}
%
Again, a key point is that we can perform this partitioning simply by examining
the \emph{data} obtained for each object.

It is common for the data associated with ``non-observable'' objects to be
completely \emph{censored}; that is, it often does not appear in a catalog or
otherwise at all.  In this case, it is appropriate to marginalize over the
parameters and (unknown) data for the ``non-observable'' objects.  Doing so
destroys the distinguishability inherent in the inhomogeneous Poisson
distribution, so we must introduce a factor of $\Nnobs!$ to account for the
over-counting:
%
\begin{equation}
  \pi\left(\left\{ \vtheta_i \right\}, \left\{ d_i \right\} \mid \vlambda \right) \propto \left[ \prod_{i=1}^{\Nobs} p\left( \vd_i \mid \vtheta_i \right) \diff{N}{\vtheta_i}\left( \vlambda \right) \right] \frac{\Nndet^{\Nnobs}\left( \vlambda \right)}{\Nnobs!} \exp\left[ - N\left( \vlambda \right) \right],
\end{equation}
%
where
%
\begin{equation}
\Nndet\left( \vlambda \right) \equiv \int_{\left\{ \vd \mid \textnormal{non-detection} \right\}} \dd \vd \, \dd \vtheta \, p\left( \vd \mid \vtheta \right) \diff{N}{\vtheta}\left( \vlambda \right)
\end{equation}
%
is the expected number of non-detections in the population model.  It is further
common to not even know \emph{how many} non-detected objects there were in a
given survey or data set.  In this case we must marginalize---sum, since
counting is a discrete operation---over the unknown number of non-detections,
$\Nnobs$, yielding
%
\begin{equation}
\pi\left(\left\{ \vtheta_i \right\}, \left\{ d_i \right\} \mid \vlambda \right) \propto \left[ \prod_{i=1}^{\Nobs} p\left( \vd_i \mid \vtheta_i \right) \diff{N}{\vtheta_i}\left( \vlambda \right) \right] \exp\left[ - \left( N\left( \vlambda \right) - \Nndet\left( \vlambda \right) \right) \right],
\end{equation}
%
or
%
\begin{equation}
  \pi\left(\left\{ \vtheta_i \right\}, \left\{ d_i \right\} \mid \vlambda \right) \propto \left[ \prod_{i=1}^{\Nobs} p\left( \vd_i \mid \vtheta_i \right) \diff{N}{\vtheta_i}\left( \vlambda \right) \right] \exp\left[ - \Ndet\left( \vlambda \right) \right],
\end{equation}
%
where $\Ndet$---the compliment of $\Nndet$---is the expected number of
detections under the population model:
%
\begin{equation}
  \Ndet\left( \vlambda \right) \equiv \int_{\left\{ \vd \mid \textnormal{detection} \right\}} \dd \vd \, \dd \vtheta \, p\left( \vd \mid \vtheta \right) \diff{N}{\vtheta}\left( \vlambda \right).
\end{equation}
%
This equation is the posterior for a hierarchical analysis of the number density
and properties of objects from a data set subject to selection effects
\citep[e.g.][]{Gair2010,Youdin2011,Fishbach2018,Wysocki2018}.

If we re-parameterize $\diff{N}{\vtheta}$ so that we can write
%
\begin{equation}
  \diff{N}{\vtheta} \equiv \Lambda p\left( \vtheta \mid \vlambda{}' \right)
\end{equation}
%
with $p\left( \vtheta \mid \vlambda{}'\right)$ integrating to 1 over the
population for any value of the new parameters $\vlambda{}'$, impose a prior
$p\left( \Lambda \right)\propto 1/\Lambda$, and marginalize over $\Lambda$, we
arrive at the treatment of selection functions for estimating population
distributions from \citet{Loredo2004,O1-BBH,Mandel2016}, as noted by
\citet{Fishbach2018}.

Note that the commonly-employed technique of modifying $\diff{N}{\vtheta}$ to
account for the selection function is not correct, and will lead to biased
results as long as the selection is dependent only on the observed
data\footnote{An example where the selection may be parameter- rather than
data-dependent is in surveys of objects that have been selected based on data in
yet other surveys.  For example, X-ray selected populations of galaxy clusters
in a weak-lensing catalog.}.

\acknowledgments

We thank Maggie Lieu for pointing out the counterexample given in the final
footnote of this document.  An example of a worked population analysis using
this method can be found at \url{https://github.com/farr/SelectionExample}.
IM's work was performed in part at Aspen Center for Physics, which is supported
by National Science Foundation grant PHY-1607611; IM's visit there was partially
supported by a grant from the Simons Foundation.

\newpage

\bibliography{selection}

\end{document}


\documentclass[11pt,twoside,a5paper]{article}

\usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.2cm,right=2.2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{verbatim}
\usepackage[normalem]{ulem} % for striking out with \sout
\usepackage{amsfonts,amsmath,amssymb} % for more math support
\usepackage{times}
\usepackage[italic]{mathastext} % use normal text font (times) in equations
\usepackage{enumitem}
%\usepackage[draft]{hyperref}
\usepackage[colorlinks,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
\usepackage{color}

\newcommand{\bfx}{{\bf x}}
\newcommand{\bfy}{{\bf y}}
\newcommand{\bfz}{{\bf z}}

\newcommand{\norm}{{\cal N}}

\begin{document}
\title{Emulators: How they work}
\author{Tom McClintock}
\maketitle

\noindent Note: all of this comes from David Barber's book
{\bf Bayesian Reasoning and Machine Learning}.

\section{Multivariate Gaussian}

A multivariate Gaussian distribution is given by
\begin{equation}
  \label{eq:multivariate_gaussian}
  p(\bfx|\mu,\Sigma) = \norm(\bfx|\mu,\Sigma)= \frac{1}{\sqrt{\det(2\pi\Sigma)}}\exp\left(-\frac{1}{2}(\bfx-\mu)^T\Sigma^{-1}(\bfx-\mu)\right)
\end{equation}
where $\mu$ is the mean vector of the distribution, $\Sigma$ is the covariance matrix. Note that the inverse covariance $\Sigma^{-1}$ is called the precision matrix.

\section{Partitioned Gaussian}

A feature of multivariate Gaussians for our purposes is the idea of a {\it partitioned} multivariate Gaussian. Consider $\norm(\bfz|\mu,\Sigma)$ defined jointly over two vectors $\bfx$ and $\bfy$ of potentially different dimensions,
\begin{equation}
  \label{eq:z}
  \bfz = \begin{pmatrix}
    \bfx\\ \bfy
    \end{pmatrix}
\end{equation}
with corresponding mean and partitioned covariance
\begin{equation}
  \label{eq:partitioned_mean}
  \mu = \begin{pmatrix}
    \mu_x\\ \mu_y
  \end{pmatrix}
\end{equation}
\begin{equation}
  \label{eq:partitioned_cov}
  \Sigma = \begin{pmatrix}
    \Sigma_{xx} & \Sigma_{xy} \\
    \Sigma_{yx} & \Sigma_{yy}
  \end{pmatrix}
\end{equation}
where $\Sigma_{xy}=\sigma_{yx}$. The marginal distribution is given by
\begin{equation}
  \label{eq:marginal_distribution}
  p(\bfx) = \norm(\bfx|\mu_x,\ \Sigma_{xx})
\end{equation}
but the {\it conditional} distribution is given by
\begin{equation}
  \label{eq:conditional_distribution}
  p(\bfx|\bfy) = \norm(\bfx|\mu_x+\Sigma_{xy}\Sigma_{yy}^{-1}(\bfy-\mu_y),\
  \Sigma_{xx} - \Sigma_{xy}\Sigma_{yy}^{-1}\Sigma_{yx}).
\end{equation}
In other words, we have separated two parts of a multivarate Gaussian, and written the conditional probability of one vector given the other. Note that if there is no correlation $\Sigma_{xy}=0$ then \ref{eq:conditional_distribution} reduces to \ref{eq:marginal_distribution}.

\section{Gaussian Process}

A Gaussian process is a process by which we assume that given some set of observations $\bfy(\bfx)$, a future observation $y^*(x^*)$ is drawn from a multivariate Gaussian, given by \ref{eq:conditional_distribution}. ({\bf Note}: Gaussian processes always assume that $\bar{\bfy}=0$, or that $\mu_y$ has already been subtracted off of $\bfy$ and can be added on later.)

Further, since every $y$ is a function of some location in the domain $x$, we assume that we have some function (the {\it kernel} function) that describes the covariance between observations made at different locations in the domain $k(x_1,x_2)$. Therefore, any element of a covariance matrix constructed between observations is given by
\begin{equation}
  \label{eq:kernel}
  [K]_{1,2} = k(x_1,x_2).
\end{equation}
This means that the covariance matrix of the observations $\bfy$ is
\begin{equation}
  \label{eq:K_matrix}
  [K_{x,x}]_{i,j} = k(x_i,x_j),\ \ \ i,j=1,...,N
\end{equation}
where $N$ are the number of observations. Additionally, the covariance between the prediction $y^*$ and the observations $\bfy$ is a vector
\begin{equation}
  \label{eq:K_matrix}
  [K_{x,x^*}]_{i} = k(x_i,x^*),\ \ \ i=1,...,N.
\end{equation}

Since $\bfy$ and $y^*$ form a multivariate Gaussian, we can immediately write down a prediction and uncertainty on that prediction from \ref{eq:conditional_distribution}
\begin{equation}
  \label{eq:gaussian_process}
  p(y^*|\bfy) = \norm(y^* | K_{x^*x}K_{xx}^{-1}\bfy,\
  K_{x^*x^*} - K_{x^*x}K_{xx}^{-1}K_{xx^*})
\end{equation}
where $K_{xx^*} = K_{x^*x}^T$. If the observations $\bfy$ have variances assiciated with them, $\sigma^2$ then we make the transformation $K_{xx}\rightarrow K_{xx}+{\bf I}\sigma^2$.

\section{Kernel Functions}

The kernel function (aka covariance function) isn't specified apriori, and in general could take almost any form. In Barber's book he explains in detail how different functions are appropriate for different purposes. In general, we will use the squared exponential kernel
\begin{equation}
  \label{eq:se_kernel}
  k(\bfx_1,\bfx_2) = k(|\bfx_1-\bfx_2|) = k_0\exp\left(-\frac{1}{2}\frac{(\bfx_1-\bfx_2)^2}{L}\right).
\end{equation}
where $k_0$ is the covariance amplitude, and $L$ is known as the ``kernel length''. The domain $\bfx$ can be multidimensional, with each dimension having slightly different meanings (e.g. $\Omega_m$, $w$, $\sigma_8$ in cosmology), so $L$ is actually an array containing a kernel length for each dimension. These parameters $k_0$ and $L$ are known as {\it hyperparameters} in the literature.

\section{Hyperparameters}

Values for the hyperparameters aren't random, but are informed by the observations. In general, $L$ corresponds to the ``feature size'', or the approximate size in the domain of features in your observations, while $\sqrt{k_0}$ is the error bar on an uncorrelated prediction. Another way to interpret $\sqrt{k_0}$ is the error bar on a prediction from a Gaussian process that is extrapolating.

The best way to find an optimal choice of hyperparameters is to write a likelihood of your observations which you can then maximize. This likelihood is
\begin{equation}
  \label{eq:likelihood}
  \log p(\bfy | \bfx) = -\frac{1}{2}\bfy^TK_{xx}^{-1}\bfy - \frac{1}{2}\log\det(2\pi K_{xx}).
\end{equation}
This also lets you obtain full posteriors for your hyperparameters, which can be propogated forward into uncertainties into $y^*$ if you want to be thorough.

\section{Complexity and Limitations}

Training a Gaussian process is $O(N^3)$ in time because it requires matrix inversion (faster if you have smart inverters), and $O(N^2)$ in space because it requires the matrix to be stored completely. Therefore, as your training data becomes large the algorithm becomes unweildy. Note: in very specific instances this can be mitigated, see Foreman-Mackay et al. 2017 (https://arxiv.org/abs/1703.09710).

If an optimizer is used to find the hyperparameters, then the resulting Gaussian process will be sensitive to the optimizer used. The best advice I can give is to try a few methods and see what works best.

\end{document}


\chapter{More on generalized limit}

\begin{defn}
I will call a permutation group \emph{fixed point free} when
every element of it except of identity has no fixed points.
\end{defn}

\begin{defn}
A funcoid~$f$ is \emph{Kolmogorov} when
$\rsupfun{f}\{x\}\ne\rsupfun{f}\{y\}$ for every distinct points
$x,y\in\dom f$.
\end{defn}

\section{Hausdorff funcoids}

\begin{defn}
\emph{Limit}~$\lim\mathcal{F}=x$ of a filter~$\mathcal{F}$
regarding funcoid~$f$ is such a point that $\rsupfun{f}\{x\}\sqsupseteq\mathcal{F}$.
\end{defn}

\begin{defn}
\emph{Hausdorff} funcoid is such a funcoid that every proper
filter on its image has at most one limit.
\end{defn}

\begin{prop}
The following are pairwise equivalent for every funcoid~$f$:
\begin{enumerate}
\item\label{hd:eq-d} $f$~is Hausdorff.
\item\label{hd:eq-op}
$x\ne y\Rightarrow\rsupfun{f}\{x\}\asymp\rsupfun{f}\{y\}$.
\end{enumerate}
\end{prop}

\begin{proof}
~
\begin{description}
\item[\ref{hd:eq-d}$\Rightarrow$\ref{hd:eq-op}]
If~\ref{hd:eq-op} does not hold,
then there exist distinct points~$x$ and~$y$ such that
$\rsupfun{f}\{x\}\nasymp\rsupfun{f}\{y\}$.
So~$x$ and~$y$ are both limit points of
$\rsupfun{f}\{x\}\sqcap\rsupfun{f}\{y\}$, and thus~$f$ is not
Hausdorff.
\item[\ref{hd:eq-op}$\Rightarrow$\ref{hd:eq-d}]
Suppose~$\mathcal{F}$ is proper.
\[ \rsupfun{f}\{x\}\sqsupseteq\mathcal{F}\land
\rsupfun{f}\{y\}\sqsupseteq\mathcal{F}\Rightarrow
\rsupfun{f}\{x\}\nasymp\rsupfun{f}\{y\}\Rightarrow x=y. \]
\end{description}
\end{proof}

\begin{cor}
Every entirely defined Hausdorff funcoid is Kolmogorov.
\end{cor}

\begin{rem}
It is enough to be ``almost entirely defined'' (having nonempty
value everywhere except of one point).
\end{rem}

\begin{obvious}
For a complete funcoid induced by a topological space this
coincides with the traditional definition of a Hausdorff
topological space.
\end{obvious}

\section{Restoring functions from limit}

Consider alternative definition of generalized limit:
\[ \xlim f = \lambda r\in G: \nu\circ f\circ\uparrow r. \]

Or:
\[ \xlim_a f = \setcond{(\rsupfun{r^{-1}}a, \nu\circ f\circ\uparrow r)}{r\in G} \]
(note this requires explicit filter in the definition of generalized limit).

Operations on the set of generalized limits can be defined (twice) pointwise. \fxwarning{First define operations on
funcoids.}

\begin{prop}
The above defined $\xlim_{\rsupfun{\mu}\{x\}} f$ is a monovalued
function if~$\mu$ is Kolmogorov and~$G$ is fixed point free.
\end{prop}

\begin{proof}
We need to prove $\supfun{r^{-1}}\rsupfun{\mu}\{x\}\ne
\supfun{s^{-1}}\rsupfun{\mu}\{x\}$ for $r,s\in G$, $r\ne s$.
Really, by definition of generalized limit, they commute, so our
formula is equivalent to
$\rsupfun{\mu}\rsupfun{r^{-1}}\{x\}\ne
\rsupfun{\mu}\rsupfun{s^{-1}}\{x\}$;
$\rsupfun{\mu}\rsupfun{r^{-1}\circ s}\rsupfun{s^{-1}}\{x\}\ne
\rsupfun{\mu}\rsupfun{s^{-1}}\{x\}$.
But $r^{-1}\circ s\ne e$, so because it is fixed point free,
$\rsupfun{r^{-1}\circ s}\rsupfun{s^{-1}}\{x\}\ne
\rsupfun{s^{-1}}\{x\}$ and thus by kolmogorovness, we have the
thesis.
\end{proof}

\begin{lem}
Let~$\mu$ and~$\nu$ be Hausdorff funcoids. If function~$f$ is defined at point~$x$, then
\[ fx=
\lim\rsupfun{(\xlim_{\rsupfun{\mu}\{x\}}f)\rsupfun{\mu}\{x\}}\{x\}
\]
\end{lem}

\begin{rem}
The right part is correctly defined because
$\xlim_a f$ is monovalued.
\end{rem}

\begin{proof}
$\lim\rsupfun{(\xlim_{\rsupfun{\mu}\{x\}}f)\rsupfun{\mu}\{x\}}\{x\}=
\lim\rsupfun{\nu\circ f}\{x\}=\lim\rsupfun{\nu}fx=fx$.
\end{proof}

\begin{cor}
Let~$\mu$ and~$\nu$ be Hausdorff funcoids.
Then function~$f$ can be restored from values of $\xlim_{\rsupfun{\mu}\{x\}}f$.
\end{cor}


\documentclass[12pt]{article}

\title{$LDL^T$ Notes}
\author{Nick Henderson}
\date{\today}

\usepackage{amsmath}

\begin{document}
\maketitle

\section{Cholesky: $A=LL^T$}

Overall $LDL^T = A$ factorization:

\begin{equation*}
\begin{pmatrix}
L_{11} &        &        \\
L_{21} & L_{22} &        \\
L_{31} & L_{32} & L_{33}
\end{pmatrix}
\begin{pmatrix}
L_{11}^T & L_{21}^T & L_{31}^T \\
         & L_{22}^T & L_{32}^T \\
         &          & L_{33}^T
\end{pmatrix}
=
\begin{pmatrix}
A_{11} & A_{12} & A_{13} \\
A_{21} & A_{22} & A_{23} \\
A_{31} & A_{32} & A_{33}
\end{pmatrix}
\end{equation*}

\section{$A=LDL^T$}

Overall $LDL^T = A$ factorization:

\begin{equation*}
\begin{pmatrix}
L_{11} &        &        \\
L_{21} & L_{22} &        \\
L_{31} & L_{32} & L_{33}
\end{pmatrix}
\begin{pmatrix}
D_{11} &        &        \\
       & D_{22} &        \\
       &        & D_{33}
\end{pmatrix}
\begin{pmatrix}
L_{11}^T & L_{21}^T & L_{31}^T \\
         & L_{22}^T & L_{32}^T \\
         &          & L_{33}^T
\end{pmatrix}
=
\begin{pmatrix}
A_{11} & A_{12} & A_{13} \\
A_{21} & A_{22} & A_{23} \\
A_{31} & A_{32} & A_{33}
\end{pmatrix}
\end{equation*}

Multiply $LD$:

\begin{equation*}
\begin{pmatrix}
L_{11} D_{11} &               &              \\
L_{21} D_{11} & L_{22} D_{22} &              \\
L_{31} D_{11} & L_{32} D_{22} & L_{33} D_{33}
\end{pmatrix}
\begin{pmatrix}
L_{11}^T & L_{21}^T & L_{31}^T \\
         & L_{22}^T & L_{32}^T \\
         &          & L_{33}^T
\end{pmatrix}
=
\begin{pmatrix}
A_{11} & A_{12} & A_{13} \\
A_{21} & A_{22} & A_{23} \\
A_{31} & A_{32} & A_{33}
\end{pmatrix}
\end{equation*}

Equations that focus on $D_{22}$, $L_{22}$, and $L_{32}$:

\begin{align*}
A_{22} &= L_{21}D_{11}L_{21}^T + L_{22}D_{22}L_{22}^T \\
A_{32} &= L_{31}D_{11}L_{21}^T + L_{32}D_{22}L_{22}^T
\end{align*}

Out of place algorithm for when $L_{22}$ is element:
\begin{align*}
L_{12} & \gets D_{11}L_{21}^T        & \text{no blas function for this} \\
D_{22} & \gets A_{22} - L_{21}L_{12} & \texttt{dot} \\
L_{32} & \gets A_{32} - L_{31}L_{12} & \texttt{gemv} \\
L_{32} & \gets L_{32} / D_{22}       & \texttt{scal}
\end{align*}

In place algorithm for when $L_{22}$ is element:
\begin{align*}
A_{12} & \gets D_{11}A_{21}^T        & \text{no blas function for this, $D_{11}=\mathsf{diag}(A_{11})$} \\
A_{22} & \gets A_{22} - A_{21}A_{12} & \texttt{dot} \\
A_{32} & \gets A_{32} - A_{31}A_{12} & \texttt{gemv} \\
A_{32} & \gets A_{32} / A_{22}       & \texttt{scal}
\end{align*}

\section{$A = U^TDU$}

Overall $U^TDU = A$ factorization:

\begin{equation*}
\begin{pmatrix}
U_{11}^T &         &        \\
U_{12}^T & U_{22}^T &        \\
U_{13}^T & U_{23}^T & U_{33}^T
\end{pmatrix}
\begin{pmatrix}
D_{11} &        &        \\
       & D_{22} &        \\
       &        & D_{33}
\end{pmatrix}
\begin{pmatrix}
U_{11} & U_{12} & U_{13} \\
       & U_{22} & U_{23} \\
       &        & U_{33}
\end{pmatrix}
=
\begin{pmatrix}
A_{11} & A_{12} & A_{13} \\
A_{21} & A_{22} & A_{23} \\
A_{31} & A_{32} & A_{33}
\end{pmatrix}
\end{equation*}

Multiply $U^TD$:

\begin{equation*}
\begin{pmatrix}
U_{11}^TD_{11} &               &              \\
U_{12}^TD_{11} & U_{22}^TD_{22} &              \\
U_{13}^TD_{11} & U_{23}^TD_{22} & U_{33}^TD_{33}
\end{pmatrix}
\begin{pmatrix}
U_{11} & U_{12} & U_{13} \\
       & U_{22} & U_{23} \\
       &        & U_{33}
\end{pmatrix}
=
\begin{pmatrix}
A_{11} & A_{12} & A_{13} \\
A_{21} & A_{22} & A_{23} \\
A_{31} & A_{32} & A_{33}
\end{pmatrix}
\end{equation*}

Equations that involve $A_{22}$, $U_{22}$, and $U_{23}$:

\begin{align*}
A_{22} &= U_{12}^TD_{11}U_{12} + U_{22}^TD_{22}U_{22} \\
A_{23} &= U_{12}^TD_{11}U_{13} + U_{22}^TD_{22}U_{23}
\end{align*}

Out of place algorithm for when $U_{22}$ is element:
\begin{align*}
U_{21} & \gets U_{12}^TD_{11}        & \text{no blas function for this} \\
D_{22} & \gets A_{22} - U_{21}U_{12} & \texttt{dot} \\
U_{23} & \gets A_{23} - U_{21}U_{13} & \texttt{gemv} \\
U_{23} & \gets U_{23} / D_{22}       & \texttt{scal}
\end{align*}

In place algorithm for when $U_{22}$ is element:
\begin{align*}
A_{21} & \gets A_{12}^TD_{11}        & \text{no blas function for this, $D_{11}=\mathsf{diag}(A_{11})$} \\
D_{22} & \gets A_{22} - A_{21}A_{12} & \texttt{dot} \\
A_{23} & \gets A_{23} - A_{21}A_{13} & \texttt{gemv} \\
A_{23} & \gets A_{23} / D_{22}       & \texttt{scal}
\end{align*}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


%!TEX root = ../notes.tex
\section{April 12, 2022}
\subsection{Ideals and Fractional Ideals}
Let $R$ be a commutative ring with an identity.

Recall that if $I, J$ are ideals of $R$, then
\[I + J := \left\{ a_i + b_j \mid a_i\in I, b_j\in J \right\}\]
and
\[IJ := \left\{\sum a_ib_j \mid a_i\in I, b_j\in J\right\}\]
Let $K$ be a number field. An ideal of $riO_K$ is sometimes called an \ul{intgral ideal}. This is to contrast them with fractional ideals.
\begin{definition}
    A fractional ideal of $\riO_K$ is a set of the form $c^{-1}\mathfrak{b}$ when $\mathfrak{b}$ is an ideal of $\riO_K$ and $0\neq c\in\riO_K$.
\end{definition}
\begin{example}
    The fractional ideals of $\ZZ$ are of the form $r\ZZ$ where $r\in\QQ$.

    $\frac{2}{5}\ZZ$ is a fractional ideal of $\ZZ$.
\end{example}
\textbf{Caution!} If $\riO_K$ is a PID, then fractional ideals are of the form
\[c^{-1}\langle d\rangle\]
for $0\neq c\in\riO_K$ and $d\in\riO_K$. This is just $c^{-1}d\riO_K = \alpha\riO_k$ where $\alpha = c^{-1}d$.

Addition/multiplication of fractional ideals works similarly as in the case of ideals:

If $\mathfrak{a}, \mathfrak{b}$ are fractional ideals, then
\begin{align*}
    \mathfrak{a}\mathfrak{b}    & := \left\{ \text{finite sums }\sum a_ib_j\mid a_i\in\mathfrak{a}, b_j\in\mathfrak{b} \right\} \\
    \mathfrak{a} + \mathfrak{b} & := \left\{a_i + b_j\mid a_i\in\mathfrak{a}, b_j\in\mathfrak{b} \right\}
\end{align*}
If $a_1 = c_1^{-1}\mathfrak{b}_1$ and $a_2 = c_2^{-1}\mathfrak{b}_2$ where $\mathfrak{b}_1, \mathfrak{b}_2$ are integral ideals, then we have
\[\mathfrak{a}_1\mathfrak{a}_2 = (c_1c_2)^{-1}\mathfrak{b}_1\mathfrak{b}_2\]
The multiplication is obviously associative and commutative, with $\riO_K$ as the identity.

Thus, the set of \emph{nonzero} fractional ideals forms a monoid\footnote{Also a commutative ring with addition, actually.} under (commutative) multiplication.

If we want the structure of an Abelian group, we need to build the inverses in.
\begin{theorem}[p. 109 \cite{stewart2015algebraic}]\label{thm:fractional-ideals-group}
    The nonzero fractional ideals of $\riO_K$ form a group under multiplication.
\end{theorem}
For each ideal $\mathfrak{a}\subseteq \riO_K$, define
\[\mathfrak{a}^{-1} := \left\{ x\in K\mid x\mathfrak{a}\subseteq \riO_K \right\}\]
Automatically, this set contains all of $\riO_K$.

If $\mathfrak{a}\neq 0$, then for any $0\neq c\in \mathfrak{a}$, $c\mathfrak{a}^{-1}\subseteq \riO_K$. Fixing such a $c$, we have that $c\mathfrak{a}^{-1} =: \mathfrak{b}$ is an ideal of $\riO_K$. (Why? $c\mathfrak{a}^{-1}$ is an $\riO_K$-submodule of $\riO_K$, i.e. that is to say, an ideal of $\riO_K$.)

\begin{example}
    Let's take $K = \QQ$ so that $\riO_K = \ZZ$
    \begin{align*}
        \mathfrak{a}      & = 5\ZZ           \\
        \mathfrak{a}^{-1} & = \frac{1}{5}\ZZ
    \end{align*}
    Thus $\mathfrak{a}^{-1} = c^{-1}\mathfrak{b}$, so that $\mathfrak{a}^{-1}$ is a fractional ideal.
\end{example}

By definition,
\[\mathfrak{a}\mathfrak{a}^{-1} = \mathfrak{a}^{-1}\mathfrak{a} \subseteq \riO_K\]
Harder to show: $\mathfrak{a}\mathfrak{a}^{-1} = \riO_K$. We blackbox this for the moment (p.110-112 \cite{stewart2015algebraic}, uses fact that $\riO_K$ is a Dedekind domain).
We can extend this discussion to fractional ideals $\mathfrak{a}$. Assuming this, we have shown \cref{thm:fractional-ideals-group}.

\begin{theorem*}[p. 109 \cite{stewart2015algebraic}]
    The nonzero fractional ideals of $\riO_K$ form a group under multiplication.
\end{theorem*}
\begin{proof}
    Let $\mathfrak{a}$ be a nonzero fractional ideal of $\riO_K$. We have $\mathfrak{a} = c^{-1}\mathfrak{b}$ with $\mathfrak{b}$ integral. We define $\mathfrak{a}' = c\mathfrak{b}^{-1}$, which is a fractional ideal, and $\mathfrak{a}\mathfrak{a}' = \riO_K$. So $\mathfrak{a}'$ is the inverse of $\mathfrak{a}$.
\end{proof}

\recall A prime ideal of a commutative ring $R$ can be defined in a couple of different ways:

\begin{definition}[Prime Ideal]
    An ideal $\mathfrak{p}$ is \ul{prime} if $IJ\subseteq \mathfrak{p}$ implies $I\subseteq \mathfrak{p}$ or $J\subseteq \mathfrak{p}$.
\end{definition}
\begin{definition*}[Prime Ideal (alternative)]
    $\mathfrak{p}$ is \ul{prime} if $ab\in\mathfrak{p}$ implies that $a\in\mathfrak{p}$ or $b\in\mathfrak{p}$.
\end{definition*}

To prove unique factorization of nonzero ideals, we first need to prove $\riO_K$ is a Dedekind domain.
\begin{theorem}[p.108 \cite{stewart2015algebraic}]
    The ring of integers $\riO_K$:
    \begin{enumerate}[a)]
        \item is an integral domain,
        \item is Noetherian (every ascending chain of ideals terminates\footnote{A chain of ideals is a sequence of inclusions $I_1\subseteq I_2\subseteq I_3\subseteq \cdots$; and for such a chain to \ul{terminate} means that $\exists N$ such that $I_n = I_N$ for all $n\geq N$.}, or every ideal is finitely generated),
        \item is integrally closed in its field of fractions (that is, if $\alpha\in\mathsf{Frac}(\riO_K) = K$ satisfies a monic polynomial equation with coefficients in $\riO_K$, then $\alpha\in\riO_K$),
        \item has that every nonzero prime ideal of $\riO_K$ is maximal.
    \end{enumerate}
    We note that a ring satisfying (a)--(d) is called a \ul{Dedekind domain}.
\end{theorem}