\subsection{Heat Engines \& The Carnot Cycle}
Perhaps one of the most useful applications of thermodynamics is the development of heat engines (and heat pumps, which are just their inverse). With everything we have just discussed, we have basically all the tools we need to discuss and understand how heat engines work\footnote{Unintentional pun}. We will start by introducing a very abstract model for a heat engine, and then try to cement our understanding with a more concrete example. \\

In essence, a heat engine is a medium (in the context of Science One, a purely gaseuous medium\footnote{Sorry steam engines... you'll have to wait until second year}) that you can supply energy to in the form of heat, and you will get back some energy in the form of useful work. The end goal of the heat engine is to do as much work for us as possible. Essentially, the process goes as follows:
\begin{enumerate}
    \item The heat engine starts in its original state.
    \item The heat engine accepts some heat $Q_H$ from an external hot bath of temperature $T_H$.
    \item The heat engine does work $W$ from the energy given to it in the form of heat.
    \item The heat engine throws away some "waste heat" $Q_C$ into a cold bath of temperature $T_C$.
    \item The heat engine returns to its original state, and the cycle begins again.
\end{enumerate}
This is demonstrated in a diagram below. Note that this is just a general outline, but really steps 2-4 can occur in a different order, multiple times, and sometimes concurrently. A diagram illustrates this below:
\begin{center}
    \begin{tikzpicture}[scale=3]
    \filldraw[fill=pink] (0,0) rectangle (1,1);
    \draw (1.75,0.25) rectangle (2.25,0.75);
    \filldraw[fill=blue!50] (3,0) rectangle (4,1);
    \draw[->] (1.15,0.5) -- (1.6,0.5);
    \draw[->] (2.4,0.5) -- (2.85,0.5);
    \draw[->] (2,0.2) -- (2,0);
    \node[above] at (1.375,0.5) {$Q_H$};
    \node[above] at (2.625,0.5) {$Q_C$};
    \node[left] at (2,0.1) {$W$};
    \draw (2,0.5) node {Engine};
    \draw (0.5,0.5) node {Hot bath at $T_H$};
    \draw (3.5,0.5) node {Cold bath at $T_C$};=
    \end{tikzpicture}
\end{center}
Of course, we could also flip the direction in which we do things to give the opposite result, which results in a heat pump! Instead of putting heat at a high temperature $T_H$ to get work $W$ out, I can instead supply the heat engine with heat $Q_C$ at a low temperature $T_C$ (which is very cheap) and then supply work to the heat engine to get heat $Q_H$ out at a high temperature.  \\
A measure of how good a heat engine is is given by its efficiency. Clearly, we want to be getting as much useful work $W$ out of the steam engine from the energy/heat $Q_H$ that we are putting into it! Then, a very natural measure of the efficiency becomes:
\begin{equation}
    \eta = \frac{W}{Q_H} = \frac{W_{net}}{Q_{in}}
\end{equation}
We can observe that this efficiency measure is a percentage; It can range from $0$ (where $W=0$ and we have a broken heat engine) to $1$ (where $W=Q_H$ and we have a perfect conversion of the energy we put in to the work we get out). However, we will soon see that a 100\% efficient heat engine is not realistic at all; this would require that no waste heat $Q_C$ is produced at all, which is realistically impossible. In fact, in a little while we will introduce a theoretical maximum for just how efficient a heat engine can be!
\subsubsection{Heat Engines on PV diagrams}
As you might have guessed from all the time we spent setting up how different thermodynamic processes look on PV diagrams, a great way for representing heat engines is to draw them as a closed, non-intersecting curve\footnote{The curve has to end where it began, else the heat engine won't be in the same place where it started after one cycle!} on a PV diagram! Let's illustrate this with an example. Pictured below is a representation of a simple heat engine. For simplicity, let's imagine that the gas medium is 1 mole of monoatomic gas (with 3 degrees of freedom).

\begin{center}
\begin{tikzpicture}
 \draw[stealth-stealth] (0,5) node[below left]{$P$} |- (5,0) node[below left]{$V$};
\filldraw (1,1) circle (2pt);
\filldraw (4,1) circle (2pt);
\filldraw (4,4) circle (2pt);
\filldraw (1,4) circle (2pt);

\draw[thick,->] (1,4) -- (2.5,4);
\draw[thick] (2.5,4) -- (4,4);
\draw[thick,->] (4,4) -- (4,2.5);
\draw[thick] (4,2.5) -- (4,1);
\draw[thick,->] (4,1) -- (2.5,1);
\draw[thick] (2.5,1) -- (1,1);
\draw[thick,->] (1,1) -- (1,2.5);
\draw[thick] (1,2.5) -- (1,4);

\node[above] at (1,4) {$2$};
\node[above] at (4,4) {$3$};
\node[below] at (4,1) {$4$};
\node[below] at (1,1) {$1$};

%\draw[dashed] (0,4) -- (1,4);
%\draw[dashed] (0,1) -- (1,1);
%\draw[dashed] (1,0) -- (1,1);
%\draw[dashed] (4,0) -- (4,1);

\node[below] at (4,0) {$2v$};
\node[below] at (1,0) {$v$};
\node[left] at (0,1) {$p$};
\node[left] at (0,4) {$2p$};
\end{tikzpicture}
\end{center}

What exactly can we get out of this graph? There's a lot of information packed into it, so to start, let's break it down into the individual steps of the heat engine. The gas in the engine starts at state $1$, at pressure $p$ and volume $v$. We heat the gas isochorically, giving it heat, until it reaches state $2$ at pressure $2p$. Then, we add more heat and let it isobarically expand until it reaches state $3$ at volume $2v$, Then, we let the gas isochorically cool and give off heat until it falls back down to pressure $p$. Finally, we isobarically compress the gas (by doing work on it), the gas gives off more heat in the process, until the gas is back to volume $v$ and it has returned to its initial state. \\
So we've broken down the steps qualitatively, but what can we say quantitatively about things like temperature, work and efficiency?

To start, let's figure out the temperature at each state (depending on the information given in the problem, you may or may not always be able to do this straight away). Here, since we know that $n=1\text{ mol}$, and we have an ideal gas/monoatomic gas medium, our job is pretty easy. We can just apply the ideal gas law at each of the four states. Rearranging the ideal gas law, we have:
\[ T = \frac{PV}{nR} \]
So from this, we have that $T_1 = \frac{pv}{R}$, $T_2 = \frac{2pv}{R}$, $T_3 = \frac{4pv}{R}$, and $T_4 = \frac{2pv}{R}$. If I were to ask what was the hottest temperature $T_H$ and coldest temperature $T_C$ of the cycle, you could then say that $T_H = T_3 = \frac{4pv}{R}$, and $T_C = T_1 = \frac{pv}{R}$. Here, their ratio is $4:1$, so the hottest temperature is 4 times that of the coldest. \\

Next, let's figure out the work. One wonderful consequence of the PV diagram representation of the heat engine is that we can actually get the work done by the engine in one cycle just from the area enclosed by the curve! Let's explore why this is true, by again looking at our square heat engine. We know that from $1$ to $3$, the area under the curve represents the \textbf{postive} work that the engine does \textbf{for us}:
\begin{center}
\begin{tikzpicture}
 \draw[stealth-stealth] (0,5) node[below left]{$P$} |- (5,0) node[below left]{$V$};
\filldraw[fill=red] (1,0) rectangle (4,4);
\filldraw (1,1) circle (2pt);
\filldraw (4,4) circle (2pt);
\filldraw (1,4) circle (2pt);

\draw[thick,->] (1,4) -- (2.5,4);
\draw[thick] (2.5,4) -- (4,4);
\draw[thick,->] (1,1) -- (1,2.5);
\draw[thick] (1,2.5) -- (1,4);

\node[above] at (1,4) {$2$};
\node[above] at (4,4) {$3$};
\node[below] at (1,1) {$1$};

%\draw[dashed] (0,4) -- (1,4);
%\draw[dashed] (0,1) -- (1,1);
%\draw[dashed] (1,0) -- (1,1);
%\draw[dashed] (4,0) -- (4,1);

\node[below] at (4,0) {$2v$};
\node[below] at (1,0) {$v$};
\node[left] at (0,1) {$p$};
\node[left] at (0,4) {$2p$};

\draw (2.5,2) node {$+$};
\end{tikzpicture}
\end{center}

And from $3$ back to $1$, the area below the curve represents work that we do \textbf{on} the gas, or the gas doing \textbf{negative} work:

\begin{center}
\begin{tikzpicture}
 \draw[stealth-stealth] (0,5) node[below left]{$P$} |- (5,0) node[below left]{$V$};
 \filldraw[fill=blue] (1,0) rectangle (4,1);
\filldraw (1,1) circle (2pt);
\filldraw (4,1) circle (2pt);
\filldraw (4,4) circle (2pt);

\draw[thick,->] (4,4) -- (4,2.5);
\draw[thick] (4,2.5) -- (4,1);
\draw[thick,->] (4,1) -- (2.5,1);
\draw[thick] (2.5,1) -- (1,1);

\node[above] at (4,4) {$3$};
\node[below] at (4,1) {$4$};
\node[below] at (1,1) {$1$};

%\draw[dashed] (0,4) -- (1,4);
%\draw[dashed] (0,1) -- (1,1);
%\draw[dashed] (1,0) -- (1,1);
%\draw[dashed] (4,0) -- (4,1);

\node[below] at (4,0) {$2v$};
\node[below] at (1,0) {$v$};
\node[left] at (0,1) {$p$};
\node[left] at (0,4) {$2p$};
\draw (2.5,0.5) node {$-$};
\end{tikzpicture}
\end{center}

Therefore, over the whole cycle, the net work that the gas/heat engine does for us is just the sum of the two parts:

\begin{center}
\begin{tikzpicture}
 \draw[stealth-stealth] (0,5) node[below left]{$P$} |- (5,0) node[below left]{$V$};
\filldraw[fill=purple] (1,1) rectangle (4,4);
\filldraw (1,1) circle (2pt);
\filldraw (4,1) circle (2pt);
\filldraw (4,4) circle (2pt);
\filldraw (1,4) circle (2pt);

\draw[thick,->] (1,4) -- (2.5,4);
\draw[thick] (2.5,4) -- (4,4);
\draw[thick,->] (4,4) -- (4,2.5);
\draw[thick] (4,2.5) -- (4,1);
\draw[thick,->] (4,1) -- (2.5,1);
\draw[thick] (2.5,1) -- (1,1);
\draw[thick,->] (1,1) -- (1,2.5);
\draw[thick] (1,2.5) -- (1,4);

\node[above] at (1,4) {$2$};
\node[above] at (4,4) {$3$};
\node[below] at (4,1) {$4$};
\node[below] at (1,1) {$1$};

%\draw[dashed] (0,4) -- (1,4);
%\draw[dashed] (0,1) -- (1,1);
%\draw[dashed] (1,0) -- (1,1);
%\draw[dashed] (4,0) -- (4,1);

\node[below] at (4,0) {$2v$};
\node[below] at (1,0) {$v$};
\node[left] at (0,1) {$p$};
\node[left] at (0,4) {$2p$};
\draw (2.5,2.5) node {$W_{net}$};
\end{tikzpicture}
\end{center}
So in our case, the work the engine does for us is simple; we simply calculate the area of the square, which is just $pv$! Easy! Note that with more complicated looking heat engines (say, with isothermal or adiabatic curves) we might need to be more clever (and either use the formulas we derived\footnote{Which are at heart the same thing as the area argument, because we got those via integration}, or tricks with the first law of thermodynamics and the definition of energy change $\Delta E = nc_v\Delta T$). Of course, there was nothing stopping you from applying those isobaric process work formulas to solve this problem, either. You would find you get an identical result! \\
To figure out the heat that goes in/out of the engine during each process, we can use the formulas that we have derived. Let's start with the heat from $1 \rightarrow 2$. Here, the gas is being isochorically heated, so we know that we are adding heat into the system. We know that its an isochoric process, so there's no work that's done; hence, the only energy change we look at is the heat. Therefore, we have \[ Q_{1\rightarrow 2} = \Delta E  = nc_v \Delta T \]
The temperature difference between $1$ and $2$ is just:
\[\Delta T = T_2 -T_1 = \frac{2pv}{R} - \frac{pv}{R} = \frac{pv}{R} \]
And for a monoatomic gas, we know that the constant volume heat capacity $c_v$ is:
\[ c_v = \frac{\chi}{2}R = \frac{3}{2}R \]
So therefore the heat that we put in during the $1 \rightarrow 2$ process is:
\[Q_{1\rightarrow 2} = \frac{3}{2}R\frac{pv}{R} = \frac{3}{2}pv \]
Now let's consider the heat flow in the $2 \rightarrow 3$ process. We have isobaric expansion, so we know that we'll have to be putting in heat in order to make that happen, and that heat will have value (from our previous derivation):
\[Q = nc_p\Delta T\]
The temperature difference between $2$ and $3$ can be calculated as:
\[\Delta T = T_3 -T_2 = \frac{4pv}{R} - \frac{2pv}{R} = \frac{2pv}{R} \]
And the constant pressure heat capacity is:
\[c_p = c_v + R = \frac{3}{2}R + R = \frac{5}{2}R \]
So the heat that we put in during the $2 \rightarrow 3$ process is:
\[Q_{2\rightarrow 3} = \frac{5}{2}R\frac{2pv}{R} = 5pv \]
I'll leave the heat flow for segments $3 \rightarrow 4$ and $4 \rightarrow 1$ for you! However, for both of those last two segments, heat is coming out of the gas (rather than us putting heat into it) so we have everything we need to calculate the efficiency of the heat engine. We have that:
\[Q_{\text{in}} = Q_{1 \rightarrow 2} + Q_{2 \rightarrow 3} = \frac{3}{2}pv + 5pv = \frac{13}{2}pv \]
And the total work done is just $pv$, so the efficiency of our square heat engine is:
\[ \eta = \frac{W_{net}}{Q_{in}} = \frac{pv}{\frac{13}{2}pv} = \frac{2}{13} \approx 15 \% \]
Wow, our cute boxy heat engine turns out to be pretty crummy at its job. It's only 15\% efficient at converting heat into work. Fortunately, not all heat engines are as bad; we'll introduce a much more efficient heat engine in the next section!

\subsubsection{The Carnot Engine}
The Carnot engine is more than just an engine. Its a limit on the efficiency of real-world thermodynamic processes, and was used to develop the first notions of entropy. That being said, like the example above the Carnot engine is a four step cycle.
\begin{center}
            \begin{tikzpicture}[
          dot/.style = {draw,fill,circle,inner sep=1pt},
          arrow inside/.style = {postaction=decorate,decoration={markings,mark=at position .55 with \arrow{>}}}
          ]
          %\draw[<->] (0,6) node[above right] {$P$} |- (6,0) node[right] {$V$};
          %\draw[->] (0,0) node[above] {$P$} %|- (6,0) node[right] {$V$};
 \draw[stealth-stealth] (0,6) node[below left]{$P$} |- (6,0) node[below left]{$V$};
          \node[dot,label={right:$2$}] (@b) at (4.5,4) {};
          \node[dot,label={left:$1$}] (@a) at (1,4.5) {};
          \node[dot,label={below left:$4$}] (@d) at (1.5,1.5) {};
          \node[dot,label={right:$3$}] (@c) at (5,1) {};
          \draw[fleche={0.5:black} ] (@a) to[looseness=.7,bend right=20] (@b);
          \draw[fleche={0.5:black} ] (@b) to[looseness=.7,bend right=20] (@c);
          \draw[fleche={0.5:black} ] (@c) to[looseness=.7,bend left=20] (@d);
          \draw[fleche={0.5:black} ] (@d) to[looseness=.7,bend left=20] (@a);
        \end{tikzpicture}

\end{center}
If we start at state $1$ in the diagram, the engine first undergoes an isothermal expansion to state $2$, while in contact with the hot heat bath. It then undergoes an adiabatic expansion to state $3$ in contact with no heat bath. This is followed by an isothermal compression to state $4$, which occurs while in contact with a cool heat bath. Finally, the engine undergoes an adiabatic compression to return to state $1$. Note that since the processes $1 \rightarrow 2$ and $3 \rightarrow 4$ are isothermal, the engine has only two temperatures. States $1$ and $2$ are at a high temperature ($T_H$), and states $3$ and $4$ are at a low temperature ($T_C)$. \\
While the optional derivation will be given in the next section, you will want to know that the efficiency of the Carnot engine is given by:
\begin{equation}
    \eta = 1 -\frac{T_C}{T_H}
\end{equation}
Where $T_C$ is the coldest/minimum temperature in the cycle and $T_H$ is the hottest/maximum temperature in the cycle. A brief fact about the maximum and minimum temperatures of the heat engine; often these two parameters are given by the ambient temperature and the combustion temperature of the gas in the engine, respectively. This means that if you were to put a Carnot engine inside of your car (though I'm about to tell you why you can't do that in a few sentences), you would find that during the winter your car will run more efficiently than during the summer! Alternatively, if you were to use a gas medium with a higher combustion temperature, you would see a similar effect.\\
One surprising fact: The Carnot efficiency is the maximum possible efficiency attainable by a heat engine\footnote{An interesting consequence of this is that your heat engine can only be 100\% efficient if it's a Carnot engine, and the lowest temperature is $T_C=0$, or the maximum temperature is $T_H = \infty$. Not very realistic either way.}. This isn't just me trying to challenge you to think of something that could be better; it's actually a theorem, and a consequence of the extremely fundamental second law of thermodynamics, which we will explore in depth in the next section. Unfortunately for the world, a truly isothermal process takes an infinitely long time (we'll explore this more in the next section as well). This means that while the Carnot engine might be the most energy efficient, its power output (i.e. the work it does divided by the time it takes) is zero. So unfortunately, you won't be installing a Carnot engine into your car anytime soon..
\subsubsection{(Optional) Deriving the Carnot Efficiency}
Deriving the efficiency of a Carnot engine is difficult, but doable with the knowledge we have so far. I'd suggest you take a shot at it before looking at this section. \\
I'll label the different states of the Carnot cycle as in the diagram above. The efficiency of a heat engine $\eta$ is defined as
\begin{equation*}
    \eta = \frac{W}{Q_{in}}
\end{equation*}
the ratio of the total work done by the system and the heat put into it. From the first law of thermodynamics, we know that $\Delta E = Q - W$, where $W$ is the work done by the system. Since this process ends in the same state it started, we can infer that $\Delta E = 0$, so $Q = W$. Therefore, we get that $W = Q_{in} - Q_{out}$. So we have that
\begin{equation*}
    \eta = 1 - \frac{Q_{out}}{Q_{in}}
\end{equation*}
Heat only flows out of the system from states $c$ to $d$, and only flows into the system from states $a$ to $b$. Both processes are isothermal, so we get
\begin{equation*}
    \eta = 1 - \frac{-P_{3}V_{3}\ln{\Big(\frac{V_4}{V_3}\Big)}}{P_{1}V_{1}\ln{\Big(\frac{V_2}{V_1}\Big)}}
\end{equation*}
To be honest I am hand-waving the signs a bit here, so take some time to make sure you understand them. If you get stuck, remember that in this case positive heat flow is into the system and that the natural logarithm of a number less than one is negative. Anyways, since $T_c$ is the temperature of the low-temperature heat bath and $T_a$ is the temperature of the high-temperature heat bath, I can substitute temperature into the equation using ideal gas law to get
\begin{equation*}
    \eta = 1 - \frac{T_{C}\ln{\Big(\frac{V_3}{V_4}\Big)}}{T_{H}\ln{\Big(\frac{V_2}{V_1}\Big)}}
\end{equation*}
Now all that remains is to show that $\frac{V_2}{V_1} = \frac{V_3}{V_4}$. I'll leave that part to you in one of the questions below. Once you do get this result, you can use it to cancel the logarithms, leaving the equation as
\begin{equation*}
    \eta = 1 - \frac{T_C}{T_H}
\end{equation*}


\documentclass{beamer}

\mode<presentation>
{
  \usetheme{Hawke}
  \setbeamercovered{transparent}
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{multimedia}

%%%%%%
% My Commands
%%%%%%

\newcommand{\ml}{{\sc matlab}}
\newcommand{\bb}{{\boldsymbol{b}}}
\newcommand{\bx}{{\boldsymbol{x}}}
\newcommand{\by}{{\boldsymbol{y}}}
\newcommand{\bfm}[1]{{\boldsymbol{#1}}}

%%%%

\title[Lecture 6] %
{Lecture 6 - Iterative Methods}

\author[I. Hawke]{I.~Hawke}

\institute[University of Southampton]%
{
  School of Mathematics, \\
  University of Southampton, UK
}

\date[Semester 1]{MATH3018/6141, Semester 1}

\subject{Numerical methods}

\pgfdeclareimage[height=0.5cm]{university-logo}{mathematics_7469}
\logo{\pgfuseimage{university-logo}}

\AtBeginSection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\section{Iterative Methods}

\subsection{Iterative Methods}

\begin{frame}
  \frametitle{Iterative Methods}

   We (still) want to solve the linear system
   \begin{equation*}
     A \bx = \bb.
   \end{equation*}
   Iterative methods build a \emph{sequence} of approximate
   solutions. Start from ``guess'' $\bx^{(0)}$ and compute
   approximations $\bx^{(1)}, \bx^{(2)}, \dots, \bx^{(N)}$ which
   converge to the exact solution. \pause

   \vspace{1ex}

   Standard ``notation'' is to split the matrix as
   \begin{equation*}
     A = N - P, \quad \implies \quad N \bx^{(i)} = P \bx^{(i - 1)} + \bb
   \end{equation*}
   and for Jacobi use $N=I$. \pause

   \vspace{1ex}

   Jacobi converges slowly: error drops by a constant amount each step.

\end{frame}

\subsection{Gauss-Seidel Method}

\begin{frame}
  \frametitle{Gauss-Seidel Method}

  In the Gauss-Seidel method split the coefficient matrix as
  \begin{equation*}
    A = I - (A_L + A_U)
  \end{equation*}
  as before. Choose
  \begin{align*}
    N & = I - A_L, \\
    P & = A_U.
  \end{align*}
  This makes the iteration scheme
  \begin{equation*}
    \bx^{(i)} =  A_L \bx^{(i)} + A_U \bx^{(i - 1)} + \bb, \quad i = 1,
    2, \dots
  \end{equation*} \pause

  Written in this form as $A_L$ \emph{strictly} lower
  triangular. Consider each component of the iteration scheme from the
  top down: each coefficient of $\bx^{(i)}$ is computed on the left
  before it is used on the right.

\end{frame}

\begin{frame}
  \frametitle{Example of Gauss-Seidel Method}

  As above we look at
  \begin{equation*}
    \begin{pmatrix}
      3 & 1 \\
      1 & 3
    \end{pmatrix}
    \begin{pmatrix}
      x_1 \\ x_2
    \end{pmatrix} =
    \begin{pmatrix}
      5 \\ 7
    \end{pmatrix} \rightarrow
    \begin{pmatrix}
      1 & \tfrac{1}{3} \\
      \tfrac{1}{3} & 1 \\
    \end{pmatrix}
    \begin{pmatrix}
      x_1 \\ x_2
    \end{pmatrix} =
    \begin{pmatrix}
      \tfrac{5}{3} \\ \tfrac{7}{3}
    \end{pmatrix}.
  \end{equation*}
  We have
  \begin{equation*}
    A_L =
    \begin{pmatrix}
      0 & 0 \\
      -1/3 & 0
    \end{pmatrix}, \quad
    A_U =
    \begin{pmatrix}
      0 & -1/3 \\
      0 & 0
    \end{pmatrix}
  \end{equation*}
  and hence the iteration algorithm becomes
  \begin{align*}
    \begin{pmatrix}
      x_1^{(i)} \\ x_2^{(i)}
    \end{pmatrix} & = \frac{1}{3} \left[
      \begin{pmatrix}
        -x_2^{(i-1)} \\ - x_1^{(i)}
      \end{pmatrix} +
      \begin{pmatrix}
        5 \\ 7
      \end{pmatrix} \right]
    = \frac{1}{3}
    \begin{pmatrix}
      5 - x_2^{(i-1)} \\ 7 - x_1^{(i)}
    \end{pmatrix}.
  \end{align*}
  If read row-by-row from the top we have always computed the
  $\bx^{(i)}$ entries before they are used.

\end{frame}

\begin{frame}
  \frametitle{Example of Gauss-Seidel Method 2}

  \begin{columns}
    \begin{column}{0.475\textwidth}
      The sequence has entries
      \begin{tabular}{|c|c c|}
        $i$ & $x_1$ & $x_2$ \\ \hline
        $0$ & $0$ & $0$ \\
        $1$ & $5 / 3$ & $16 / 9$ \\
        $2$ & $1.074074$ & $1.975309$ \\
        $5$ & $1.000102$ & $1.999966$ \\
        $10$ & $1.000000$ & $2.000000$ \\
        $100$ & $1.000000$ & $2.000000$
      \end{tabular}
    \end{column}
    \begin{column}{0.525\textwidth}
      \begin{center}
        \includegraphics[width=\textwidth]{figures/GaussSeidel1}
      \end{center}
    \end{column}
  \end{columns}

  \vspace{1ex}

  A starting ``guess'' of $\bx = \bfm{0}$ appears to converge to $\bx
  = (1, 2)^T$. The convergence is exponential with a slope $\sim 2$,
  much faster than Jacobi's Method.

\end{frame}

\subsection{Successive Over Relaxation}

\begin{frame}
  \frametitle{Successive Over Relaxation}

  View the sequence as repeated ``corrections'' to the previous term,
  \begin{equation*}
    \bx^{(i)} = \bx^{(i-1)} + \bfm{c},
  \end{equation*}
  where correction is based e.g.\ on Gauss-Seidel method,
  \begin{equation*}
    \bfm{c} = A_L \bx^{(i)} + (A_U - I) \bx^{(i-1)} + \bb.
  \end{equation*} \pause

  Modify correction by factor $\omega$:
  \begin{equation*}
    \bx^{(i)} = \bx^{(i-1)} + \omega \bfm{c}.
  \end{equation*} \pause

  \emph{A priori} best value of $\omega$ not clear.  Typically it is
  set close to one. However, poor choices may cause the algorithm to
  diverge!

\end{frame}

\begin{frame}
  \frametitle{Example of SOR Method 1}

  \begin{columns}
    \begin{column}{0.475\textwidth}
      Solving the previous example using $\omega = 1.035$, the
      sequence has entries
      \begin{tabular}{|c|c c|}
        $i$ & $x_1$ & $x_2$ \\ \hline
        $0$ & $0$ & $0$ \\
        $1$ & $1.725000$ & $1.819875$ \\
        $2$ & $1.036768$ & $1.993619$ \\
        $5$ & $0.999999$ & $2.000000$ \\
        $10$ & $1.000000$ & $2.000000$ \\
        $100$ & $1.000000$ & $2.000000$
      \end{tabular}
    \end{column}
    \begin{column}{0.525\textwidth}
      \begin{center}
        \includegraphics[width=\textwidth]{figures/SOR1}
      \end{center}
    \end{column}
  \end{columns}

  \vspace{1ex}

  A starting ``guess'' of $\bx = \bfm{0}$ appears to converge to $\bx
  = (1, 2)^T$. The convergence is exponential with a slope $\sim 3$,
  the fastest method so far.

\end{frame}

\begin{frame}
  \frametitle{Example of SOR Method 2}

  \begin{columns}
    \begin{column}{0.475\textwidth}
      Solving the previous example using $\omega = 1.2$, the
      sequence has entries
      \begin{tabular}{|c|c c|}
        $i$ & $x_1$ & $x_2$ \\ \hline
        $0$ & $0$ & $0$ \\
        $1$ & $2.000000$ & $2.000000$ \\
        $2$ & $0.800000$ & $2.080000$ \\
        $5$ & $0.998221$ & $2.000430$ \\
        $10$ & $1.000000$ & $2.000000$ \\
        $100$ & $1.000000$ & $2.000000$
      \end{tabular}
    \end{column}
    \begin{column}{0.525\textwidth}
      \begin{center}
        \includegraphics[width=\textwidth]{figures/SOR2}
      \end{center}
    \end{column}
  \end{columns}

  \vspace{1ex}

  A starting ``guess'' of $\bx = \bfm{0}$ appears to converge to $\bx
  = (1, 2)^T$. The convergence is exponential with a slope $\sim 1.6$,
  which is not as good as Gauss-Seidel.

\end{frame}

\begin{frame}
  \frametitle{The relaxation parameter}

  \begin{center}
    \includegraphics[width=0.9\textwidth]{figures/SOR}
  \end{center}
  The norm of the error obtained with SOR after four iterations.  The
  convergence behaviour depends in a highly non-trivial way on
  $\omega$.

  \vspace{1ex}

  SOR algorithms are much harder to analyze than Jacobi or
  Gauss-Seidel.

\end{frame}

\subsection{Convergence Analysis}

\begin{frame}
  \frametitle{Convergence analysis}

  Two theorems about \emph{when} these methods will work:

  \vspace{2ex}

  {\bf Theorem 1}: If the coefficient matrix $A$ is \emph{strictly}
  diagonally dominant then both the Jacobi method and the Gauss-Seidel
  method will converge.

  \vspace{2ex}

  {\bf Theorem 2}: If the coefficient matrix $A$ is symmetric and
  positive definite then the Gauss-Seidel method will converge.

\end{frame}

\section{Other matrix operations}

\subsection{Other matrix operations}

\begin{frame}
  \frametitle{Other matrix operations}

  We have so far only considered the linear system
  \begin{equation*}
    A \bx = \bb.
  \end{equation*}
  We have given methods to solve this system accurately without
  computing the matrix inverse or determinant. However, these methods
  can also be used to efficiently and accurately compute these
  quantities.
\end{frame}

\begin{frame}
  \frametitle{Determinants}

  Given the $LU$ decomposition we have
  \begin{equation*}
    \det (A) = \det(L) \times \det(U), \quad \text{and} \quad \det(U)
    = \prod_{i=1}^n u_{ii}.
  \end{equation*}
  Choose $\ell_{ii}=1$ giving $\det(L) = 1$: determinant
  follows. \pause

  \vspace{1ex}

  Similarly Gaussian elimination with partial pivoting gives
  \begin{equation*}
    \det(A) = \pm \prod_{i=1}^n u_{ii},
  \end{equation*}
  where the sign depends on the number of row swaps performed.\pause

  \vspace{1ex}

  {\bf Note:} Gaussian elimination, and hence finding the determinant,
  is an ${\cal O}(n^3)$ operation.  The expansion in minors is an
  ${\cal O}(n!)$ operation.

\end{frame}

\begin{frame}
  \frametitle{Matrix inversion}

  Solving for the matrix inverse can be written as a set of linear
  system problems. We write
  \begin{equation*}
    A A^{-1} = I
  \end{equation*}
  and consider each column of $A^{-1}$ as a separate problem. \pause
  Writing $\bfm{c}_i$ for the (unknown) column of $A^{-1}$ we have
  \begin{equation*}
    A \bfm{c}_i =
    \begin{pmatrix}
       \vdots \\ 0 \\ 1 \\ 0 \\ \vdots
    \end{pmatrix}
  \end{equation*}
  where the $1$ appears in the $i^{\text{th}}$ row. \pause

  \vspace{1ex}

  This finds the inverse by solving $n$ linear systems, which is much
  faster than evaluating the $n+1$ determinants required by Cramer's
  rule.

\end{frame}

\section{Summary}

\subsection{Summary}

\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item Iterative methods are often based on the split
    \begin{equation*}
      A = N - P.
    \end{equation*}
  \item Convergence depends on $M = N^{-1}P$.
  \item Jacobi chooses $N = I$ and converges slowly.
  \item Gauss-Seidel chooses $N = I - A_U$ and uses coefficients as
    soon as they become available. Slightly harder to code, it
    converges faster than Jacobi.
  \item SOR tries to ``accelerate'' the convergence of Gauss-Seidel
    depending on a parameter $\omega$. It can be faster, but the
    behaviour depends non-trivially on $\omega$ and it is very
    difficult to analyze.
  \item Strict diagonal dominance is sufficient for convergence of
    both Jacobi and Gauss-Seidel methods.
  \item Computing determinants and inverses from the solution of
    linear systems is usually much faster than ``standard''
    techniques.
  \end{itemize}

\end{frame}

\end{document}

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS240-2016S: Programming in C
% Copyright 2016 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS240-2016S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 2}

Bellatrix Lestrange, a CS240 student, was asked to write a program that takes an arbitrary number of non-negative integers as command line arguments and prints their average.
A sample run of the program was expected to be as given below.

\begin{terminal}
$ gcc command-line.c -o command-line
$ ./command-line
error: missing command line arguments
$ ./command-line 2 5 8
5.00
$ ./command-line 1 2 3 4 5 6
3.50
\end{terminal}

To solve this problem, she wrote the following program.

\lstset{language=c,tabsize=4}
\begin{lstlisting}
// Bellatrix Lestrange
// <bellatrix.lestran001@umb.edu>

#include <stdio.h>
#include <string.h>

int str2num(char str[]);

int main(int argc, char *argv[])
{
	int i;
	int sum = 0;
	float avg;
	for (i = 0; i < argc; i++)
		sum += str2num(argv[i]);
	avg = sum / argc;
	printf("%.2f\n", avg);
}

int str2num(char str[])
{
	int i;
	int res = 0;
	for (i = 0; i < sizeof(str); i++)
		res = res * 10 + str[i] - '0';
	return res;
}
\end{lstlisting}

Her program produces the following output.

\begin{terminal}
$ gcc command-line.c -o command-line
$ ./command-line
-15202348.00
$ ./command-line 2 5 8
-1147083.00
$ ./command-line 1 2 3 4 5 6
-12761818.00
\end{terminal}

Clearly, something is wrong with Bellatrix's code.
This is why she is asking for your help to debug her program and fix all its bugs.
Rewrite the program \texttt{command-line.c} such that its output matches the expected output provided previously.


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

\marginpar{Friday\\ 2020-3-20, \\ compiled \\ \today}

Today we will discuss another application of entanglement: Quantum Key Distribution.
For more details, see \cite[]{scaraniSecurityPracticalQuantum2009}.

\section{Quantum Key Distribution}

The idea is that it is possible to exchange two keys between two people, in an \emph{unconditional} way, as opposed to the current way of doing security, which is based on hard-to-solve problems.

If Alice and Bob have a shared key \(K\), they can use a \emph{one-time-pad} to communicate.

Say \(X\) is our message: then Alice constructs \(Y = X \oplus K\), which is completely random since \(K\) is.

When Bob receives the message, he does \(Y \oplus K = X \oplus K \oplus K = X\).
This is old classical cryptography, it was discovered by Shannon.

This works as long as \(K\) is indeed \emph{one-time}: if it is reused, an attacker can start reconstructing the message: otherwise, .

So, the QKD is about transmitting the key.
The most famous protocol to do this is BB84.
Most of the things which will be covered today are covered in
Rev Mod Phys 81, 1301 (2009).

The basis of the algorithm is a public quantum channel between Alice and Bob. The thing which is needed is for the channel to be \emph{verified}, so that Alice is sure to be talking to Bob.
Eve can be watching the passing qubits.

Alice can prepare four possible states, which she associates to two logical bits: \(\ket{0}\) and \(\ket{+}\) are associated with 0, while \(\ket{1}\) and \(\ket{-}\) are associated with 1.

Bob either measures \(\sigma_{z}\) or \(\sigma_{x}\).
It they use the same basis, Bob measures the same thing Alice sent.

If they use a different basis, then the result is random, since \(\abs{\braket{\pm}{0}}^2 =  \abs{\braket{\pm}{1}}= 1 / 2\).

So, there is a need to do \emph{sifting} later: after the transmission, Alice and Bob say publicly which bases they used.
With this information, they discard the qubits for which they used different bases.

Any measurement by the eavesdropper necessarily increases the Q-Bit Error Rate, which can be detected by Alice and Bob.

If Eve performs an ``Intercept and Resend'' attack, \(1/2\) of the time she will get the basis wrong, and those times she will create an error half of the time.
So, in this case we will have a QBER equal to \SI{25}{\percent}.

This can be measured by selecting, a posteriori, some qubits to be used as a check.

Alice could also use the states \(\ket{+i}\) and \(\ket{-i}\), which are eigenstates of \(\sigma_{y}\). Using these states we can increase the security.

The probabilities can be changed: we can have probabilities \(\epsilon \) for \(\sigma_{x}\) and \(\sigma_{y}\), while we use \(1-2\epsilon \) probability for \(\sigma_{z}\).

Then, we encode the key using only \(\sigma_{z}\), so we are communicating \(\approx1 -4 \epsilon  \) of the time.
We use the qubits encoded with \(\sigma_{x}\) and \(\sigma_{y}\) to measure how many times Eve has been measuring, since she will be always measuring \(\sigma_{z}\).

Eve cannot determine which basis is being used by Alice, since the mixed states corresponding to having \(\ket{0}\) or \(\ket{1}\) with equal probability and to having \(\ket{+}\) or \(\ket{-}\) with equal probability are the same, \(\rho = \frac{1}{2} \mathbb{1}\).

This works well if the devices are working as expected.
If the implementation is insecure, QKD can still be breached: so, we want to get \emph{device-independent} implementations.

A possible implementation is one in which we use an entangled source which gives us pairs with \(\ket{\Psi^{-}}\).

Then, Alice measures \(\sigma_{x}\) and \(\sigma_{z}\) and so does Bob.
Using the correlations between Alice and Bob we can determine whether the qubits are being generated entangled or not.
The state is
%
\begin{align}
\ket{\Psi^{-}} = \frac{1}{\sqrt{2}} \qty(\ket{01} - \ket{10})
= \frac{1}{\sqrt{2}} \qty(\ket{+-} - \ket{- +})
\,,
\end{align}
%
so the measurements are precisely anticorrelated for both bases 01 and +-.

So, we can do QKD with black boxes.
However, this is difficult to implement.

The receiver is more vulnerable to attacks, since they can receive any signal from the outside.
So, we want to do \emph{measurement device independent} coding: Alice and Bob each prepare the four states with equal probabilities, they send it to an untrusted station \(C\), in which there is a beamsplitter.
This performs a Bell measurement: if \(C\) sees a coincidence, then they communicate this to \(A\) and \(B\): this means that the measured state is \(\ket{\Psi^{-}}\). This means that the original qubits were opposite, but there is no information as to what the states originally were.

If the operator at \(C\) does anything else other than the Bell measurement, this can be detected by Alice and Bob by looking at the QBER.

By adding some more detectors we can also measure \(\ket{\Psi^{+}}\); then we do efficient BB84 by mostly sending in one basis.
This way, we can also get an efficiency of \(1- \epsilon \).

Now, how do we measure the secret Key Rate?
It is defined by
%
\begin{align}
r = I_{AB} - I_E
\,,
\end{align}
%
where \(I_{AB}\) is the mutual information of Alice and Bob, while \(I_E\) is the information of the Eavesdropper.

The definition of \(I_{AB}\) is:
%
\begin{align}
I_{AB} = H(A) - H(A|B)
\,,
\end{align}
%
where \(H(A)\) is the Shannon entropy of Alice:
%
\begin{align}
H(A) = - \sum_{x = 0,1} p_x \log_2  p_x
\,.
\end{align}

If the bits are random, then \(H(A )= 1\). On the other hand,
%
\begin{align}
H(A|B) = - \sum _{\substack{a=0,1 \\ b=0,1} } p_{ab} \log_2 p_{a|b}
\,,
\end{align}
%
which is zero if Alice and Bob's bits are perfectly correlated. It can be shown that
%
\begin{align}
H(A|B) = h_2  (Q) = -Q \log_2 Q - (1-Q) \log_2 (1-Q)
\,,
\end{align}
%
where \(Q\) is the error rate.

So, we write this as \(I_{AB} = 1 - h_2 (Q)\).
\(Q\) is the number of bits which are wrong, and the formula tells us that in order to correct these errors we need to reveal \(h_2 (Q)\).

It can be shown that the information of the eavesdropper in the BB84 case is
%
\begin{align}
I_E = h_2 (Q)
\,.
\end{align}

With the 6-state coding we have
%
\begin{align}
I_E = Q + (1- Q) h_2 \qty(\frac{1 - 3 Q / 2}{1 - Q})
\,.
\end{align}

This is purely quantum: classically there is no connection between \(I_E\) and \(Q\).
So, for BB84 the error rate must be \(Q \leq \SI{11}{\percent}\).

So, we need to do \emph{error correction} and then \emph{privacy amplification} so that the eavesdropper has no residual information.
Privacy amplification reduces the key length by a factor \(1/r\), so we get bits which are surely secure.

What we do, practically, is to use an attenuated laser. The state of the laser is a coherent one:
%
\begin{align}
\ket{\alpha } = e^{- \abs{\alpha }^2 / 2} \sum _{n} \frac{\alpha ^{n}}{\sqrt{n!}} \ket{k}
\,.
\end{align}

We can attenuate the laser so that we have \(\abs{\alpha } = \mu \), with \(\mu = 1\): usually we will have zero photons, often one, but sometimes \(2\): this is an issue, since the attacker can take one and leave the other: this is called Photon Number Splitting.

What can be done is to use a \emph{decoy state}: we use three values for the intensity, \(\mu = 1 \), but  also  \( \nu_1 = .1 \) and \(\nu_2 =  0\). Using this, we can see whether there is a PNS attack or not.

So, from the source point of view we have no issue in using a classical source like a laser.

\end{document}


\section{Heavy Armor Training}\label{perk:heavyArmorTraining}
\textbf{Cost:} 2,500CP\\
\textbf{Requirements:} Medium Armor Training I or Light Armor Training I\\
\textbf{Passive, Repeatable, Source(2,500 Gold)}\\
You are trained with heavy armor.\\
You add your level to Dodge and Evade checks while equipped with armor with the "Heavy" descriptor.\\
Additionally, the resistance you gain from such armor against blunt, cutting and piercing is increased by 1.\\
\\

Level Progression:\\
\\
\rowcolors{2}{lightgray}{white}
\begin{tabular}{l | l | l | l | l}
    Level & CP Cost & Gold Cost & Effect & Resistance bonus \\
    II
    & 7,500CP
    & 7,500 Gold
    & +1d4 Evade, +2 Dodge
    & 2
    \\
    III
    & 10,000CP
    & 10,000 Gold
    & +2d4 Evade, +4 Dodge
    & 3
    \\
    IV
    & 15,000CP
    & 15,000 Gold
    & +3d4 Evade, +6 Dodge
    & 4
    \\
\end{tabular}


\section{Performance Evaluation}

The three dimensions of library evaluation are (i) scalability (ii) reliability or availability (iii) reasonable efficient.  Standard methodology for evaluating performance has been found in literature.  Evaluation methodology for the other two aspects, unfortunately, requires more studies.

The performance evaluation methodology presented in this section is derived from Hennessy and Patterson\rq{}s book \cite{HePa06}, which provides quantitative approaches used in the design and analysis of hardware development.  I found the presented approach is also applicable to software performance evaluation.

In hardware evaluation, benchmark suites are used to measure performance.  Unfortunately, by the time of this writing, there is no standard benchmark suite for Akka or other distributed programming frameworks.  Therefore, a selection of benchmarking examples is required for assessing the overall performance of the TAkka library.  To avoid unintentional optimization in TAkka implementation, benchmarks should be selected from existing applications written in other platforms and modifications should be made at the minimal level.

Once the suite of benchmarks is decided, we could use geometric mean of all performance measures to summarize the overall performance.  In addition, to assess the variability of the chosen suite, the geometric standard deviations could be used as an indicator to \''decide whether the mean is likely to be a a good predictor\".  Hennessy and Patterson\cite{HePa06} further suggest employing statistic tools to analysis results.


\title{\bf Distance Ladder}

\section{Basics \& Nomenclature}

Determination of distance is a fundamental problem in astronomy, to
which is owed numerous breakthroughs. The distance to nearby galaxies
has been established through a calibrated {\it distance ladder},
refined over the past century.

The distance ladder begins with the measurement of {\it parallax},
which is the apparent motion of sufficiently nearby stars due to the
motion of the Earth around the Sun. Precise measurements of stars over
time in a fixed reference frame yield {\it proper motions} and
parallax-based distances. Most recently, the Gaia satellite has
provided a large sample of stars with measured parallaxes reaching
across our Galaxy.

To reach distances outside our galaxy, we must rely on less direct
measurements of distance. Usually this involves finding objects or
phenomena that have pairs of observables with a known relation but
with different dependences on distance.

Many of these techniques fall into the {\it standard candle} category:
a luminosity is estimated by some means, and a flux is measured, and
the distance is inferred from the inverse-square law $f=L/4\pi D^2$.
The most prominent standard candles are:
\begin{itemize}
\item {\it RR Lyrae} variable stars. These stars are relatively low mass
  horizontal branch stars. They vary due to the $\kappa$ mechanism,
  which is an instability associated with the reaction of the
  atmospheric opacity to changes in pressure and density that causes
  oscillations in stellar size and luminosity.  They have periods of
  order a day. They are prominent in globular clusters but can also be
  found throughout the Milky Way. Historically they have been most
  useful for determining distances within the Galaxy. However, the
  advent of Gaia and the James Webb Space Telescope opens up the
  possibility of observing these stars in nearby galaxies.
\item {\it Cepheid} variable stars. These stars are high mass and
  luminous.  They also vary due to the $\kappa$ mechanism but have
  longer periods. Historically, they have provided one of the most
  direct links between local distance scales and distant galaxies.
\item {\it Tip of the Red Giant Branch} stars. The red giant branch is
  commences when core hydrogen burning ends; at this point, shell
  hydrogen burning commences and the star becomes rapidly brighter and
  much larger. At a certain point the helium ash at the core ignites,
  moving the star to its next phase, the smaller and less luminous
  horizontal branch. The core ignites rapidly at a specific stage in
  evolution, so that at a given metallicity red giant stars all end
  this stage at the same luminosity. This leads to a rapid fall-off in
  the luminosity distribution of RGB stars and a clear tip of the red
  giant branch that can be used as a standard candle.
\item {\it Type Ia supernovae}. These supernovae result from the
  nuclear detonation of white dwarfs, probably due to accretion from
  or collision with a binary companion. The Phillips relation connects
  the time scale of the supernova light curve to the luminosity of the
  supernova. These sources were used to provide the first definitive
  evidence for the existence of cosmic acceleration.
\end{itemize}
Historically, a number of other distance measurements have also been
used that could be categorized as standard candle distances. Some
methods have relied on other supernova types. Other methods have
generally capitalized on scaling relations of galaxies, such as the
Tully-Fisher relation for spirals relating luminosity and circular
velocity, and the Fundamental Plane for ellipticals relating
luminosity, size, and velocity dispersion. The difficulty in
implementing these methods in a way that is free of systematic errors
generally has led to the use of supernovae for the most recent
measurements of the distance ladder.

Several standard candle techniques prove useful, or may soon do so. I
will discuss eclipsing binaries, masers, and gravitational waves:
\begin{itemize}
\item Some binary stars, like Algol, eclipse each other during their
orbit. Their light curve alone yields a period $P$ and their radii
relative to the orbit size, $r_1/a$ and $r_2/a$ for any given
eccentricity and phase of eclipse. If both their spectra can also be
measured, this can yield velocities, which yields enough information
to determine their total mass $M$, eccentricity $e$, and the
line-of-sight semimajor axis of the orbit $a \sin i$. The mass and
semimajor axis are related by:
\begin{equation}
M = \frac{4\pi^2 a^3}{GP^2}.
\end{equation}
For eclipsing binaries $i\approx 90^\circ$.  Thus, from gravitational
and geometric considerations, you can determine the sizes of the
stars. Spectra can yield constraints on their surface
temperatures. The sizes and temperatures determines the luminosities,
which combined with fluxes determines the distance.
\item Under certain conditions the supermassive
black holes at the centers of galaxies may contain H$_2$O
masers. Under even more special conditions, these systems can be
sufficiently clean that one may measure velocities and accelerations
over time of clouds in circular orbits, which yields a physical
distance. Combined , this combination yields
the mass of the black hole and the distance. Only one clean enough
system is known, about 8 Mpc away in NGC 4258.
\item Colliding compact objects whose gravitational waves are detected can
yield constraints on distance from the gravitational waveform, because
the total mass of the system is related to both the waveform frequency
and the total energy. If the redshift is known from an electromagnetic
counterpart, then the rest-frame waveform frequency can be determined,
and the object can be placed on the redshift-distance relation.
\end{itemize}

A major application of these methods is to compare the redshifts and
distances of local galaxies. This comparison led Hubble to the Hubble
Law:
\begin{equation}
v = H_0 d
\end{equation}
where $v$ is velocity, $H_0$ is the Hubble constant, and $d$ is the
distance. Gravitational attraction causes galaxies to move toward each
other with respect to this flow, with {\it peculiar velocities} of
100s of km s$^{-1}$. For example, M31 is moving toward us at 400 km
s$^{-1}$, and large clusters, such as Virgo and Coma, have internal
motions and thus peculiar velocities of 1000s of km s$^{-1}$. Thus,
determinations of the Hubble constant need to use galaxies
sufficiently far to reduce this effect. Alternatively, they need to
estimate and correct for these peculiar velocities, using the density
field of galaxies itself to estimate the magnitudes and directions of
the expected velocities.

The Hubble Law is a fundamental measurement of the nature of the

universe we are in, but it also is a tool to map the universe on
larger scales, since measuring Doppler shifts is relatively easy
compared to galactic distances.

\section{Commentary}

Briefly, the most precise current estimates of the local Hubble
constant come from the \citet{riess16a} group. These use a number of
Milky Way Cepheids with parallax distances, along with eclipsing
binaries and NGC 4258, to anchor extragalactic Cepheids in galaxies
with SNe Ia, and use those to anchor a much larger SN Ia data set. As
of 2018, the results for $H_0$ from these analyses tend to be
considerably higher (several sigma) than those inferred from cosmic
microwave background and large scale structure measurements, using
standard cosmological models to extrapolate to $z=0$. The cause of
this discrepancy is not known.

The current ladder to a precise $H_0$ is strongly dependent on the
Cepheid step. There are several important issues with the
interpretation of the Cepheid distance scale, the most prominent of
which is that the period-luminosity relation is metallicity
dependent. The LMC has a much lower metallicity than the other
galaxies in the analysis, and so if improperly modelled the
metallicity dependence can cause a bias.

\citet{beaton16a} present an alternative path using different
indicators (RR Lyrae and TRGB) to calibrate the SN Ia, both of which
tend to trace an older, more metal poor population even within metal
rich galaxies. This path may soon become competitive. This second
indepedent measurement will be critical in assessing the existing
discrepancy.

The detection of much more distant masing systems suitable for an
analysis similar to that of NGC 4258, or a sufficient number of
gravitational wave sources, could provide a ``one step'' alternative
to the distance ladder for the purpose of determining the cosmological
constant.

\section{Key References}

\begin{itemize}
  \item
    {\it A 2.4\% Determination of the Local Value of the Hubble
    Constant}, \citet{riess16a}
  \item
    {\it The Carnegie-Chicago Hubble Program. I. An Independent
    Approach to the Extragalactic Distance Scale Using Only Population
    II Distance Indicators}, \citet{beaton16a}
\end{itemize}

\section{Order-of-magnitude Exercises}

\begin{enumerate}
\item Estimate age of the universe from the Hubble constant assuming
    constant expansion.

\begin{answer}[Author: Jiarong Zhu]
Hubble's law states $v = H_0 d$, where $H_0\approx 70$ km s$^{-1}$
Mpc$^{-1}$. If we assume constant expansion (no acceleration or
deceleration), the age of universe will be:
\begin{equation}
T = \frac{d}{v}=\frac{1}{H_0}\approx
\frac{{\rm Mpc}}{70 {\rm ~km} {\rm ~s}^{-1}} =
\frac{10^6*3.086*10^{13} {\rm ~km}}{70 {\rm ~km}} {\rm s}\approx
4.4*10^{17} {\rm ~s}\approx 13.9 {\rm ~Gyr}.
\end{equation}
\end{answer}

\item What is the maximum parallax at 1 pc, 10 pc, 1 kpc, and 1 Mpc?
    Assuming you can get good positions to with a few hundredths of a
    FWHM, what size telescopes do you need so that you can measure
    parallax at these distances?

\begin{answer}[Author: Jiarong Zhu]
The definition of the maximum parallax p is:
\begin{equation}
    p = \frac{1 {\rm ~AU}}{d} = \frac{1''}{d / 1 {\rm ~pc}}.
\end{equation}

Thus if $d$ equals 1 pc, 10 pc, 1 kpc, 1 Mpc, the parallax will be
$1''$, $0.1''$, $10^{-3}\,''$, $10^{-6}\,''$, respectively.

If we can get good positions to $\sim 10^{-2}$ FWHM, suppose the
telescope has size $D$ and measures light at a wavelength $\lambda$,
we find we can measure parallaxes of:
\begin{equation}
    p \sim \frac{\lambda}{D} \times 10^{-2}
\end{equation}
Recalling that $1'' = \pi / 180 / 3600 {\rm ~rad} \approx 4.84*10^{-6}
{\rm ~rad}$, then:
\begin{equation}
    D \sim 10^{-2} \times \frac{\lambda}{p}\approx \frac{\lambda (d /
    1 {\rm ~pc})}{484\times 10^{-6}} \approx
    \left(\frac{\lambda}{484 {\rm ~nm}}\right)
    \left(\frac{d}{1 {\rm ~pc}}\right) 10^{-3} {\rm ~m}
\end{equation}

For instance, if the telescope is optical telescope, $\lambda \sim
500$ nm, the size $D$ of the telescope would need to be $\sim 1$ km in
order to have the resolution to measure parallax.

This diameter requirement only accounts for the diffraction limit and
not other considerations. For example, although the diffraction limit
of a $\sim$ mm-sized telescope is in principle sufficient to measure
parallax at 1 pc, obviously that is impractical because you cannot
gather enough photons in a reasonable time to perform the measurement!
Conversely, although a baseline $D\sim 1$ km is necessary based on the
diffraction limit to measure parallax at 1 Mpc, you may not need a
km-diameter worth of light-gathering power for that measurement.
E.g. using an interferometer fed by two large but possible-to-build
optical telescopes, separated by a km, would be enough (though also
challenging and expensive to do!!).
\end{answer}

\item If the typical peculiar velocity of a galaxy is $\sim 300$ km
    s$^{-1}$, at what redshift does its contribution to the distance
   error drop below 5\%?
\item Based on their luminosities, what are the relative distances to
   which you should be able to determine distances to RR Lyrae,
   TRGB stars, Cepheids, and SN Type Ia?

\begin{answer}[Author: Nanoom Lee]
Let us assume there is a minimum flux required for the object to be
detected and measured at some given precision. For this fixed minimum
flux, the distance to some object is proportional to $L^{1/2}$, where
$L$ is the luminosity of the object. Roughly, the typical luminosity
of RR Lyrae stars is $10^2 L_\text{sun}$, of TRGB stars is $10^4
L_\text{sun}$, of Cepheids is $10^2$--$10^4 L_\text{sun}$, and of SN
Type Ia is $10^{10}L_\text{sun}$. Therefore the distances (relative to
RR Lyrae) to which we should be able to determine distance with these
standard candles is 1, 10, 1--10, and $10^4$ for RR Lyrae, TRGB stars,
Cepheids, and SN Type Ia, respectively. This explains why RR Lyrae are
the most difficult to use for extragalactic distances, why SN Type Ia
are necessary in order to reach distances which can measure the Hubble
flow well, and why the Cepheid and TRGB methods are in between.
\end{answer}

\end{enumerate}

\section{Numerics and Data Exercises}

\begin{enumerate}
\item Allen Sandage used to use brightest galaxies in clusters as a
   standard
   candle. Download \href{http://risa.stanford.edu/redmapper/}{redmapper},
   a modern catalog of galaxy clusters from SDSS. Select the brightest
   cluster galaxies for massive clusters. Based on looking at these
   galaxies within a narrow range of redshift, are they reasonable
   standard candles? Show a Hubble diagram assuming that they are and
   compare to a standard Planck cosmology.
\item \citet{clementini18a} publish a catalog of RR Lyrae variables
   from Gaia DR2. \citet{muraveva18a} publish a calibration of the RR
   Lyrae period-luminosity-metallicity (PLZ) relation. Use the RR
   Lyrae stars in the catalog and the published PLZ relation to
   estimate the ratio of the distance to the Sculptor dwarf galaxy to
   the distance to the Messier 62 globular cluuster.
\end{enumerate}

\bibliographystyle{apj}
\bibliography{exex}


\documentclass[12pt]{article}
\usepackage[T1]{fontenc}

% Document Details
\newcommand{\CLASS}{AMATH 586}
\newcommand{\assigmentnum}{Numerical SDE Solvers}

\usepackage[margin = 1.1in, top = 1.25in, bottom = 1.in]{geometry}
\input{../../TeX_headers/title.tex} % Title Styling
\input{../../TeX_headers/styling.tex} % General Styling
\input{../../TeX_headers/code.tex} % Code Display Setup
\input{../../TeX_headers/math.tex} % Math shortcuts

%\usepackage{algorithm}
%\usepackage[noend]{algpseudocode}

\usepackage{amsthm}
\newtheorem*{lemma}{Lemma}
\newtheorem*{theorem}{Theorem}
\newtheorem*{definition}{Definition}
\newtheorem*{prop}{Proposition}
\renewenvironment{proof}{{\bfseries Proof.}}{\qed}

\hypersetup{
   colorlinks=true,       % false: boxed links; true: colored links
   linkcolor=violet,          % color of internal links (change box color with linkbordercolor)
   citecolor=blue,        % color of links to bibliography
   filecolor=magenta,      % color of file links
   urlcolor=cyan           % color of external links
}

\begin{document}
\maketitle

\section*{Abstract}
This report is intended as a starting reference for numerical methods to SDEs and contains content similar to \cite{holmes,sdes}. The standard text on this topic is \cite{kloeden}, which provides an in-depth approach to stochastic calculus, and numerical methods for SDEs. As a starting point to stochastic calculus in general we refer readers to \cite{lorig}.

\section{Introduction}

A Stochastic Differential Equation (SDE) is an equation of the form,
\begin{align}
    \d X_t =  \mu(X_t,t)\d t + \sigma(X_t,t)\d W_t \label{diffform}
\end{align}
where \( W_t \) denotes a standard Brownian motion \cite{lorig}. We can write this in integral form as,
\begin{align}
    X_{t} - X_{0} = \int_{0}^{t} \mu(s,X_s)\d s + \int_{0}^{t} \sigma(s,X_s) \d W_s \label{intform}
\end{align}

In general we would like to write our solution \( X_T \) as an explicit function of a Brownian motion trajectory. For some processes this is possible, however in general it is not. As a result, numerical methods are required for many SDEs.

SDEs are commonly used to model processes such as molecular dynamics, neurodynamics, financial quantities (stocks, asset prices, interest rates, etc.) SDEs can also be used to efficiently solve high dimensional PDEs numerically, (standard mesh based methods scale exponentially with the dimension of the system).

In this report we first introduce Brownian motion and stochastic calculus. We then discuss how Brownian motion can be approximated numerically and discuss what it means for a numerical SDE method to converge. Next we present simple numerical method and two higher order methods which are obtained using stochastic Taylor expansions. Finally, we conduct some experiments to corroborate the theoretical claims we present earlier in the paper.

\section{Brownian motion}
There are many equivalent characterizations of Brownian motion. Perhaps the most standard is the following definition.
\begin{definition}
A Brownian motion is a stochastic process \( W = (W_t)_{t\geq0} \) defined on some probability space \( (\Omega,\cF,\bP) \) satisfying,
\begin{enumerate}[nolistsep]
    \item \( W_0 = 0 \)
    \item \( (W_d-W_c) \indep (W_b-W_a) \) for all \( 0\leq a\leq b\leq c\leq d \))
    \item \( (W_t - W_s) \sim \cN(0,t-s) \) for all \( 0\leq s\leq t \)
    \item the map \( t \to W_t(\omega) \) is continuous for almost all \( \omega\in\Omega \)
\end{enumerate}
\end{definition}

We attempt to parse this definition and notation. The notation \( W_t \) denotes a random variable which depends on the time \( t \). That is, \( W_t \) is a function from \( \Omega \to \bR \). For a fixed \( \omega \in \Omega \), \( W_t(\omega) \) can be seen as a function of \( t \), and is referred to as a path or a trajectory of the Brownian motion.

Condition 1 means that all paths of the Brownian motion starts start at the origin. Condition 2 means that the random variables \( W_d-W_c \) and \( W_b - W_a \) are independent of one another. That is, knowing how much the a path of the Brownian motion changes over the time interval \( [a,b] \) does not give any information about how much the path changes over the interval \( [c,d] \). Condition 3 means that over any time interval \( [s,t] \) how much the Brownian motion changes is a normal random variable with mean zero and variance \( t-s \). Finally, Condition 4 means that almost all paths of the Brownian motion are continuous.

Brownian motion can be constructed as a scaled random walk. Such a construction can be readily obtained on the internet or in \cite{lorig}. We will simply note that it may be useful to view Brownian motion as the continuous analog to a discrete random walk.

\section{Stochastic Calculus}
In this section we aim to provide enough background to allow all statements in the main portion of the paper to be understood. That said, this done in the admittedly unsatisfying way of black box lemma's and heuristics which we do not explain or prove. For a more complete overview of Stochastic Calculus we refer readers to \cite{kloeden, lorig}.

We first introduce Riemann--Stieltjes integrals.

\begin{definition}
For real valued functions \( f \) and \( g \) the Riemann--Stieltjes integral is defined as,
\begin{align*}
    \int_{a}^{b} f(x) \d g(x) := \lim\limits_{\norm{\Pi}\to 0} \sum_{i=0}^{n-1} f(c_i)(g(x_{i+1}) - g(x_i))
\end{align*}
where \( \Pi = \{a=x_0<x_1 < \cdots < x_n = b\} \) is a partition of \( [a,b] \), \( \norm{\Pi} \) is the length of the largest subinterval, and \( c_i \) is any point in \( [x_i,x_{i+1}] \).
\end{definition}

We note that if \( g(x) = x \) the Riemann--Stieltjes integral is the standard Riemann integral and that if \( g \) is continuously differentiable,
\begin{align*}
    f(g(t)) - f(g(0)) = \int_0^t \d f(g(s)) = \int_0^t f'(g(s))g'(s) \d s
\end{align*}

Brownian motion and many processes involving Brownian motion are not differentiable. It\^o's Lemma gives us a way to compute the analogous result for a class of stochastic processes called It\^o (drift-diffussion) processes. For our purposes we can think of It\^o processes as processes with an integral with respect to \( t \) and an integral with respect to \( W_t \).

\begin{lemma}[It\^o] For \( f:\bR\to\bR \) sufficiently differentiable and It\^o process \( X_t \),
\begin{align}
    \d f(X_t) &=  f'(X_t)\d X_t + \dfrac{1}{2}f''(X_t)\d[X,X]_t %\label{ito}
\end{align}
\end{lemma}
Here we have used the notation \( [X,X]_t \) to denote the quadratic variation of the process \( X_t \). Heuristically we can compute \( \d[X,X]_s \) by expanding  \( (\d X_s)(\d X_s) \) and simplifying using the identities,
\begin{align}
    \d t \d t = 0, && \d t \d W_t = 0, && \d W_t \d W_t = \d t \label{heuristics}
\end{align}

It\^o's Lemma can be generalized to functions and processes of higher dimension.
\begin{lemma}[It\^o] For \( f:\bR^n\to\bR^n \) sufficiently differentiable and It\^o process \( X_t = [X_t^1, X_t^2, \ldots X_t^n]^T \),
\begin{align}
    \d f(X_t) = \sum_{i=1}^{n} \left[\pp{}{x_i}f(X_t)\right] \d X_t^i + \dfrac{1}{2} \sum_{i=1}^{n} \sum_{j=1}^{n}\left[ \pp{}{x_i} \pp{}{x_j} f(X_t) \right] \d [X^i,X^j]_t \label{itond}
\end{align}
\end{lemma}

Similar to before, we compute \( \d[X^i,X^j]_t \) by expanding \( (\d X^i)(\d X^j) \) and using the heuristics in (\ref{heuristics}).

Consider the special case when \( n=2 \), \( X_t^1 = t\), and \( X_t^2 = X_t \). Using our heuristics in (\ref{heuristics}) we have \( \d[X^1,X^2]_t = (\d t)(\d X_t) = 0 \) and \( \d[X^1,X^1] = (\d t)(\d t) = 0 \). Therefore, by (\ref{itond}),
\begin{align}
    \d f(t,X_t) &= \pp{}{t} f(t,X_t) + \pp{}{x} f(t,X_t) \d X_t + \dfrac{1}{2} \pp[2]{}{x}f(t,X_t) \d [X,X]_t
    \\ &= \left( \pp{}{t} + \dfrac{1}{2}\pp[2]{}{x} \right)f(t,X_t)\d t + \pp{}{x} f(t,X_t) \d X_t \label{2dito}
\end{align}

\section{Example Processes}
In this section we provide some results about a few important stochastic processes which we will use in our numerical tests. Both the SDEs we introduce can be solved analytically and the mean and variance can be computed

For constants \( \mu \) and \( \sigma \) a Geometric Brownian motion satisfies,
\begin{align}
    \d X_t = \mu X_t \d t + \sigma X_t \d W_t, && t\in[0,T] \label{GBMeqn}
\end{align}

We will solve this equation to build some familiarity with stochastic calculus.
To this end, note that \( \d X_t = \mu X_t \d t + \sigma X_t \d W_t \) so using the heuristics (\ref{heuristics}) we have \( \d[X,X]_t = \sigma^2 \d t \). We note apply It\^o's Lemma to \( \ln(x) \) to obtain,
\begin{align*}
    \d \ln (X_t) &= \dfrac{1}{X_t} \d X_t - \dfrac{1}{2} \dfrac{1}{X_t^2} \d[X,X]_t
    = \left( \mu - \dfrac{\sigma^2}{2} \right) \d t + \sigma \d W_t
\end{align*}

Integrating both sides from \( T \) to \( t \) we obtain,
\begin{align*}
    \ln (X_t) = \int_0^t \d \ln(X_s) = \int_0^t \left( \mu - \dfrac{\sigma^2}{2} \right)\d s + \int_0^t \sigma \d W_s = \left( \mu - \dfrac{\sigma^2}{2} \right) t + \sigma W_t
\end{align*}

Therefore, the solution to (\ref{GBMeqn}) is,
\begin{align*}
    X_t = X_0 \exp\left( \left( \mu - \dfrac{\sigma^2}{2} \right)t + \sigma W_t \right)
\end{align*}

For constants \( \theta, \mu, \sigma \), an Ornstein--Uhlenbeck (OU) process satisfies,
\begin{align}
    \d X_t = \theta(\mu-X_t) \d t + \sigma \d W_t, && \theta > 0 \label{OUeqn}
\end{align}

We note that if \( X_t \) is away from \( \mu \) it will tend towards this value in expectation. Figure~\ref{OU_em} shows Euler--Maruyama method applied to (\ref{OUeqn}). As expected, the trajectories all end up ``centered'' about \( \mu \). More precisely, (\ref{OUeqn}) has solution,
\begin{align*}
    X_t = X_0 \exp(-\theta t) + \mu(1-\exp(-\theta t)) + \sigma \int_0^t \exp(-\theta(t-s))\d W_s
\end{align*}

The mean of \( X_t \) is,
\begin{align*}
    \bE[X_t] = (X_0 - \mu)\exp \left( -\theta t \right)+ \mu
\end{align*}
Likewise, the variance of \( X_t \) is,
\begin{align*}
    \bE\left[(X_t-\bE[X_t])^2\right] = \dfrac{\sigma^2}{2\theta}(1-\exp(-2\theta t))
\end{align*}

\section{Generating Brownian motion Numerically} \label{brownian_motion}

Given the four properties of Brownian motion, how can we numerically generate trajectories which are representative of a Brownian motion? The most standard way to do this is to first pick a time mesh on which we will have the values of the Brownian motion. Let \( t = [t_0, t_1, \ldots, T] \) where \( 0 = t_0 < t_1 < \cdots < t_N = T \) be such a mesh. We will generate a numerical realization \( \hat{W} = [0, \hat{W}_1, \ldots, \hat{W}_T] \) of a Brownian motion trajectory. To do this we generate the incriments. That is, we start with \( \hat{W}_0 = 0 \) and recursively define,
\begin{align*}
    \hat{W}_{t_{k+1}} = \hat{W}_{t_k} + \Delta \hat{W}_{t_k}
\end{align*}
where, using property (3) of Brownian motion, we must sample \( \Delta \hat{W}_{t_k} \) from a normal distribution with mean 0 and variance \( t_{k+1} -t_{k} \).

Since we only sample at discrete points, we have not generated a ``true'' trajectory of the Brownian motion. In fact, refining the mesh and generating the interior points is not trivial. However, it is clear that the property 2 is satisfied since we sample or increments independently. Similarly, property 3 is satisfied since the sum of iid normal random variables is a iid random variable with mean equal to the sum of the means, and variance equal to the sum of the variances. Therefore this method gives us trajectories which satisfy the first three properties of Brownian motion.

We now make a few practical observations about implementing this procedure on a computer. First, while we have explicitly defined the vector \( \hat{W} \), it may be more efficient to save the vector \( \Delta \hat{W} \) since the numerical methods we present involve \( \Delta W_t \). Second, if it is slow to sample normal random variables, other simpler distributions coud be used. For instance, we could pick \( \Delta W_t \in\{-\sqrt{\d t}, \sqrt{\d t}\} \) with equal probabilities for each choice. In this case the mean and variance will still match Brownian motion, but the higher moments may not (i.e. the increments will no longer be normally distributed). Whether or not this is important is dependent on the application.

\section{Convergence of Numerical Methods}
A SDE solution may have many distinct trajectories as the solution itself is a random process. Our standard definitions of convergence for IVPs are no longer sufficient.

For some applications we would like to be able to compute trajectories numerically which are representative of the actual trajectories of the solution to the SDE. This gives rise to the following definition convergence.
\begin{definition}
A discrete time approximation \( \hat{X}^{(k)} \) is said to converge strongly to the solution \( X_t \) at time \( T \) if,
\begin{align*}
    \lim_{k\to 0}\bE\left[\left| X_T - \hat{X}_T^{(k)} \right|\right] = 0
\end{align*}
and is said to converge strongly with order \( m \) if for sufficiently small \( k \),
\begin{align*}
    \bE\left[\left| X_T - \hat{X}_T^{(k)} \right|\right] = \cO(k^m)
\end{align*}
\end{definition}

Since strong convergence looks at the absolute difference of \( \hat{X}_T^{(k)} \) and \( X_T \) it tells us about how individual trajectories vary. That is, to compute this expectation we first find the absolute difference of our solution and the exact solution for every trajectory of Brownian motion, and then take the mean.

For other applications it may be sufficient to generate trajectories whose moments (mean, variance, etc.) are the same as the trajectories of the solution to the SDE, even if individual trajectories don't necessarily converge to trajectories of the solution. This gives rise to the notion of weak convergence.
\begin{definition}
A discrete time approximation \( \hat{X}^{(k)} \) is said to converge weakly to the solution \( X_t \) at time \( T \) if for every polynomial \( p \),
\begin{align*}
    \lim_{k\to 0} \left| \bE\left[ p(X_T) - p\left(\hat{X}_T^{(k)}\right) \right] \right| = 0
\end{align*}
and is said to converge weakly with order \( m \) if for sufficiently small \( k \) and every polynomial \( p \),
\begin{align*}
    \left|\bE\left[ p(X_T) - p\left(\hat{X}_T^{(k)}\right) \right] \right| = \cO(k^m)
\end{align*}
\end{definition}

Since weak convergence looks at the signed difference of \( \hat{X}_T^{(k)} \) and \( X_T \) the errors can ``cancel'' with one another.

Note that the term corresponding to \( \d t \) is deterministic. This means that if \( \sigma \equiv 0 \), Equation (\ref{intform}) becomes the initial value problem \( \d X_t/\d t = \mu(X_t,t) \) with initial condition at time \( t \) given by the value of \( X_t \). In this case both definitions of convergence agree with the standard definition of convergence for IVPs provided in \cite{leveque}.

We now prove the following (unsurprising) theorem following closely to the proof provided in \cite{holmes}.
\begin{theorem}
The weak order of a SDE method is always less than or equal to the strong order of the method.
\end{theorem}
\begin{proof}
Suppose a method \( \hat{X}^{(k)} \) is strongly convergent. Then the set \( S:=\{\hat{X}_T^{(k)}\}_k\cup\{X_T\} \) is bounded.
Therefore, there exists \( K > 0 \) such that \( K > |p'(x)| \) for all \( x \) in some finite interval containing \( S \).

Since \( |\bE Z| \leq \bE|Z| \) for any random variable \( Z \), and by the Mean Value Theorem,
\begin{align*}
    \left|\bE\left[ p(X_T) - p\left(\hat{X}_T^{(k)}\right) \right] \right|
    \leq \bE\left[\left| p(X_T)  -  p\left(\hat{X}_T^{(k)}\right) \right| \right]
    \leq K \bE\left[\left| X_T - \hat{X}_T^{(k)} \right| \right]
\end{align*}
This proves the result.
\end{proof}

\section{A simple Numerical Method}
All the numerical methods in this report find an approximate solution to a given SDE on some time grid. More specifically, the methods take in a series of time points and the value of a Brownian motion trajectory at these time points and output the approximate solution.

We first introduce the natural generalization of the Forward Euler method. The so called Euler--Maruyama method \cite{sdes} is given by the relation,
\begin{align*}
        \hat{X}_{t+\Delta t} &= \hat{X}_t + \mu(t,\hat{X}_{t}) \Delta t + \sigma(t,\hat{X}_{t}) \Delta W_{t}
\end{align*}
where \( \Delta W_t:=W_{t+\Delta t}-W_t \).

On SDEs where \( \sigma \) and \( \mu \) satisfy the appropriate conditions (these condtions are generally satisifed), the Euler-Maruyama method has strong and weak orders of 1/2 and 1 respectively. A commonly cited proof of this result can be found in \cite{kloeden}. However we note that on some SDEs strong convergence of order 1 is attained.

We test the Euler--Maruyama method on the Ornstein--Uhlenbeck equation and Geometric Brownian motion equation. Figure~\ref{EM_tests} shows the result of two tests. More specifically, Figure~\ref{OU_em} shows 10 trajectories of the OU process computed using the Euler-Maruyama method. As expected these trajectories all seem to be heading towards a mean of 1 as time progresses. Figure~\ref{GBM_em} shows the result of the Euler-Maruyama a large step size in comparison to the exact solution.

\begin{figure}[ht!]\centering
\begin{subfigure}{.48\textwidth}\centering
    \includegraphics[width=\textwidth]{img/OU_em_10.pdf}
    \caption{10 trajectories of the OU equation with \( \theta = 10 \), \( \mu = 1 \), \( \sigma = 1/2 \) computed using the Euler--Maruyama method with a step size of \( \Delta t = 0.004 \).}
    \label{OU_em}
\end{subfigure}\hfill
\begin{subfigure}{.48\textwidth}\centering
    \includegraphics[width=\textwidth]{img/GBM_true_vs_10.pdf}
    \caption{Exact trajectory of Geometric Brownian motion with \( \mu = 1 \), \( \sigma = 1/2 \), and solution computed using the Euler--Maruyama method with a step size of \( \Delta t = 1/10 \).}
    \label{GBM_em}
\end{subfigure}
\caption{Basic numerical tests of Euler--Maruyama method}
\label{EM_tests}
\end{figure}

\section{Higher Order Methods}
The Euler--Maruyama method is commonly used due to its simplicity and ease of implementation. However, the strong convergence of this method is quite slow. As a result, higher order methods have been developed and studied. We introduce two methods in the hopes of illustrating how such methods can be derived. Both methods have strong and weak order one. A similar derivation can be found in \cite{holmes}.

By It\^o's Lemma (\ref{2dito}), if \( X_T \) is the solution to (\ref{intform}), for any sufficiently differentiable function \( f \) we have,
\begin{align}
    f(T,X_T) &= f(t,X_t) + \int_0^t \cM f(s,X_s)\d s + \int_0^t \cS f(s,X_s) \d W_s \label{ito1}
\end{align}
where,
\begin{align*}
    \cM := \pp{}{s} +  \mu(s,X_s)\pp{}{x} + \frac{1}{2}\sigma^2(s,X_s) \pp[2]{}{x}, && \cS := \sigma(s,X_s) \pp{}{x}
\end{align*}

Applying the relation in (\ref{ito1}) with \( f = \mu \) and \( f=\sigma \) gives,
\begin{align*}
    \mu(s,X_s) &= \mu(t,X_t) + \int_0^s \cM \mu(u,X_u) \d u + \int_0^s \cS \mu(u,X_u)\d W_u \\
    \sigma(s,X_s) &= \sigma(t,X_t) + \int_0^s \cM \sigma(u,X_u) \d u + \int_0^s \cS \sigma(u,X_u)\d W_u
\end{align*}

Therefore, inserting these expressions into (\ref{intform}) gives the relations,
\begin{align}
    X_t %&= X_t + \int_t^T \mu(t,X_t) \d s + \int_t^T \sigma(t,X_t) \d W_s + R \\
    &= X_0 + \mu(t,X_t) \int_0^t \d s +  \sigma(t,X_t)\int_0^t \d W_s + R \label{emexpr}
\end{align}
where
\begin{align}
    R &=  \int_0^t \int_0^s \cM \mu(u,X_u) \d u \d s + \int_0^t \int_0^s \cS \mu(u,X_u)\d W_u  \d s \tag*{}
    \\ &\hspace{4em} + \int_0^t \int_0^s \cM \sigma(u,X_u)\d u \d W_s + \int_0^t \int_0^s \cS \sigma(u,X_u) \d W_u \d W_s
\end{align}

Note the Euler--Maruyama method arises from (\ref{emexpr}) after dropping all the terms in \( R \) and setting \( T = t+\Delta t \).

Using the hueristics (\ref{heuristics}) the \( \d W_s \d W_t \) term is the highest order term of \( R \), so a higher order method must keep more of this term. We therefore apply the relation in (\ref{ito1}) with \( f = \cS \sigma \) to obtain,
\begin{align*}
    \cS \sigma(u,X_u) = \cS \sigma(t,X_t) + \int_{t}^{u} \cM \cM \sigma(v,X_v) \d v + \int_t^u \cS \cM \sigma(v,X_v) \d W_v
\end{align*}

Therefore,
\begin{align}
    X_t = X_0 + \int_0^t \mu(t,X_t) \d s + \int_0^t \sigma(t,X_t) \d W_s + \int_0^t \int_0^s \cS \sigma(t,X_t)  \d W_u \d W_v + R \label{milexpr}
\end{align}
where \( R \) contains higher order terms (and is different from the \( R \) in (\ref{emexpr})).

Applying It\^o's Lemma to \( f(x) = x^2 \) we obtained the well known result,
\begin{align*}
    \int_0^t \int_t^s \d W_u \d W_s
    = \int_0^t W_{s} \d W_s
    = \dfrac{1}{2} \left( (W_{t})^2 - t \right)
\end{align*}

Dropping all the terms in the new \( R \) and setting \( T = t+\Delta t \), (\ref{milexpr}) gives rise to the  Milstein method,
\begin{align*}
    \hat{X}_{t+\Delta t} &= \hat{X}_t + \mu(t,\hat{X}_{t}) \Delta t + \sigma(t,\hat{X}_{t}) \Delta W_{t} + \sigma(t,\hat{X}_t) \partial_x \sigma(t,\hat{X}_t) \left( (\Delta W_t)^2 - \Delta t \right)/2
\end{align*}

Both the Milstein and Forward Euler methods can be seen as Taylor series methods. Using similar approximations to what we have done here arbitrary order methods can be obtained. However, deriving and implementing such methods can easily become unwieldy. Therefore, as with deterministic IVP methods, the order of accuracy of a method must be balanced with the complexity of the method when deciding which method to apply.

As with deterministic IVP Taylor methods, knowing the derivative to a function is necessary. It is sometimes preferable to have methods which do not rely on knowing the derivative of any functions ahead of time.

We can approximate the \( x \)-derivative of \( \sigma \) by,
\begin{align}
    \pp{}{x}\sigma(\hat{X}_t,t) = \dfrac{\sigma(t,\hat{X}_t + \sigma(\hat{X})\sqrt{\Delta t}) - \sigma(t,\hat{X}_t)}{\sigma(\hat{X},t)\sqrt{\Delta t}} + \cO\left(\sigma(t,\hat{X}_t)\sqrt{\Delta t}\right) \label{derivapprox}
\end{align}

Using our heuristics (\ref{heuristics}) we expect substituting the right hand side to give a first order method which is the case (although not proved here).

Using the approximation in (\ref{derivapprox}) yields the strong first order Runge-Kutta method,
\begin{align*}
    \hat{X}_{t+\Delta t} &= \hat{X}_t + \mu(t,\hat{X}_{t}) \Delta t + \sigma(t,\hat{X}_{t}) \Delta W_{t} + \sigma(t,\hat{X}_t-X^*) \left( (\Delta W_t)^2 - \Delta t \right) / (2 \sqrt{\Delta t}) \\
    X^* &=  \hat{X}_t  + \sigma(t,\hat{X}_t)\sqrt{\Delta t}
\end{align*}

\section{Numerical Convergence Experiments}
In this section we conduct a few numerical experiments to see if they agree with the theoretical results presented above.

We implement the Euler-Maruyama and Runge-Kutta methods described above in Python using Numpy. These functions take in the Brownian motion in increment form as suggested previously. Note the notation change where we have denoted \( \mu \) and \( \sigma \) by \( a \) and \( b \).
\lstinputlisting[linerange=\#<start:em>-\#<end:em>]{solvers.py}
\lstinputlisting[linerange=\#<start:rk>-\#<end:rk>]{solvers.py}

Figure~\ref{weak_order_test} shows the absolute difference between the mean and variance of the actual solution and the mean and variance of various numerical methods. All three methods tested match the expected orders of weak convergence for the mean and variance of the process.

\begin{figure}[b!]\centering
\begin{subfigure}{.48\textwidth}\centering
    \includegraphics[width=\textwidth]{img/weak_order_2000.pdf}
    \caption{\( \dfrac{\big|\bE[X_T] - \bE[\hat{X}_T]\big|}{\big|\bE[X_T]\big|} \)}
    \label{mean}
\end{subfigure}
\begin{subfigure}{.48\textwidth}\centering
    \includegraphics[width=\textwidth]{img/weak_order_2_mom_2000.pdf}
    \caption{\( \dfrac{\big|\bE[(X_T-\bE[X_T])^2] - \bE[(\hat{X}_T-\bE[X_T])^2]\big|}{\big|\bE[(X_T-\bE[X_T])^2]\big|} \) }
    \label{var}
\end{subfigure}
    \caption{Statistics based on 2000 trajectories, where \( T = 1 \) and \( X_t \) is a OU process with \( \theta = 1 \), \( \mu = 1 \), and \( \sigma = 1 \) for various \( \hat{X}_t \).  Circles correspond to the Euler--Maruyama method at various mesh sizes, triangles correspond to the Runge-Kutta method at various mesh sizes, and squares correspond to Euler--Maruyama method with \( \Delta W_t \in \{-\Delta t, \Delta t\} \). }
\label{weak_order_test}
\end{figure}

Note that the best fit lines in Figure~\ref{mean} have slopes of \protect\input{img/weak_order_em_2000.txt}\unskip, \protect\input{img/weak_order_rk_2000.txt}\unskip, and \protect\input{img/weak_order_em2_2000.txt}respectively. This is the expected weak convergence of order one.

Interestingly, the best fit lines in Figure~\ref{var} have slopes of \protect\input{img/weak_order_em_2_mom_2000.txt}\unskip, \protect\input{img/weak_order_rk_2_mom_2000.txt}\unskip, and \protect\input{img/weak_order_em2_2_mom_2000.txt}respectively. This seems to happen when adjusting the parameters of the processes as well as the number of trajectories used, etc. While it still satisfies the definition of weak convergence of order 1 it was unexpected.

We can now use our numerical solutions to generate trajectories of a Geometric Brownian motion. In particular, if for each trajectory we output the values of \( \Delta W_t \) at each time point, the Brownian motion \( W_t \) can be computer. This means we can compare our computed results at time \( T \) to the exact solution at time \( T \).

Figure~\ref{strong_order_test} shows the results of this for the Euler-Maruayma and Runge-Kutat methods. Note that the best fit lines have slopes of \protect\input{img/strong_order_em_1000.txt}and \protect\input{img/strong_order_rk_1000.txt}respectively. These are the order of strong convergence expected for both methods.

\begin{figure}[t!]\centering
    \includegraphics[width=.5\textwidth]{img/strong_order_1000.pdf}
    \caption{Values of \( \bE|X_T - \hat{X}_T|/\bE|X_T| \) approximated using 1000 trajectories, where \( T = 8 \) and \( X_t \) is a Geometric Brownian motion with \( \mu = 1 \) and \( \sigma = 1/2 \) for various \( \hat{X}_t \).  Circles correspond to the Euler--Maruyama method at various mesh sizes, and triangles correspond to the Runge-Kutta method at various mesh sizes.}
\label{strong_order_test}
\end{figure}

\section{Conclusion}
We introduce stochastic differential equations. In order to solve these numerically we introduced methods of generating Brownian motion on a discrete time mesh. We then introduced numerical SDE methods by approximating the solution using a (stochastic) Taylor expansion. Finally some numerical results were tested. For the most part the figures matched our expectations. However, the plot of Figure~\ref{var} shows that \( \big|\bE[(X_T-\bE[X_T])^2] - \bE[(\hat{X}_T-\bE[X_T])^2]\big|/\big|\bE[(X_T-\bE[X_T])^2]\big| \) goes to zero like \( k^2 \) rather than \( k \). At the moment it is not clear what affect the polynomial we chose, the processes, and the method have on this result. A more detailed look at this would be warranted.

\bibliography{project}{}
\bibliographystyle{ieeetr}

\pagebreak
\section{Code for Numerical Tests}
\lstinputlisting[basicstyle=\tiny\tt]{solvers.py}

\end{document}


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{parskip}
\addtolength{\jot}{1em}
\title{Binomial Options Pricing Model}
\date{October 2018}

\begin{document}
\begin{titlepage}
\maketitle
\end{titlepage}

\section{One-Step Binomial Model}
\paragraph{No-Arbitrage Assumption} \hspace{0pt}

Consider a stock currently priced at $S_{0}=\text{\$100}$, and at the end of three months ($t_{T}$), the stock price will be either $S_{T}=\text{\$110}$ or $S_{T}=\text{\$90}$. Assume the scenario where we are valuing a European call option to purchase the stock for \$105 at $t_{T}$. There are two possible outcomes at $t_{T}$: if $S_{T}=\text{\$110}$, the value of the call option will be \$5; if $S_{T}=\text{\$90}$, the value of the call option will be 0.

Assume that arbitrage opportunities do not exist. The scenario is also set up in such a way that there is no uncertainty about the value of the stock at the end of three months. In the absence of arbitrage opportunities and uncertainties, the return it earns must then equal the risk-free rate of interest ($R_{f}$). It is always possible to set up a riskless portfolio because there are two securities (stock and call option) and only two possible outcomes.

Consider a portfolio consisting of a long position in $\Delta$ shares of the stock and a short position in one call option. Then, calculate the value of $\Delta$ that makes the portfolio riskless. If the $S$ moves up from $S_{0}=100$ to $S_{T}=110$, the value of the shares is $110\Delta$ and the value of the option is 1; the portfolio value is $110\Delta-1$. If the $S$ moves down from $S_{0}=100$ to $S_{T}=90$, the value of the shares is $90\Delta$ and the value of the option is 0; the portfolio value is $90\Delta$. To set up a riskless portfolio, the portfolio value at $t_{T}$ must be the same for either outcome.
\begin{align*}
110\Delta-1&=90\Delta\\
\Delta&=0.05
\end{align*}
The portfolio is riskless if it consists of a long position in 0.05 shares and a short position in one call option. In either outcome, the value of the portfolio is always 4.5 at $t_{T}$. $\Delta$ is the number of shares necessary to hedge a short position in one call option.

In the absence of arbitrage opportunities, a riskles portfolio must earn the risk-free rate of interest, $R_{f}$. Assuming that $R_{f}=0.05$ per annum, the value of the portfolio today must be:
\begin{align*}
4.5e^{-0.05\cdot3/12}&=4.4441
\end{align*}
The stock price today at $t_{0}$ is known to be \$100. Let $f=\text{option price today}$. The value of the portfolio today must be:
\begin{align*}
100\cdot0.05-f&=4.4441\\
5-f&=4.4441\\
f&=0.5559
\end{align*}
In the absence of arbitrage opportunities, the value of the option at $t_{0}$ must be $f=0.5559$. If $f>0.5559$, the portfolio would cost less than 4.4441 to set up and would earn more than $R_{f}$. Conversely, if $f<0.5559$, shorting the stock and buying the option would allow for borrowing at less than $R_{f}$.

\paragraph{Generalization} \hspace{0pt}

Let the current stock price $=S_{0}$ and the stock option price $=f$. Suppose that during the life of the option (from $t_{0}$ to $t_{T}$), the stock price can either move up from $S_{0}$ to $S_{0}u$ or move down from $S_{0}$ to $S_{0}d$; where $(u>1; d<1)$. The proportional increase in stock price in an up movement is $u-1$; the proportional decrease in stock price in a down movement is $1-d$. Suppose that for $S_{0}u$, the option payoff is $f_{u}$ and for $S_{0}d$, the option payoff is $f_{d}$.

Consider a portfolio consisting of a long position in $\Delta$ shares and a short position in one stock option. In an up movement in the stock price, the portfolio value at $t_{T}$ is:
\begin{align*}
S_{0}u\Delta-f_{u}
\end{align*}
In a down movement in the stock price, the portfolio value at $t_{T}$ is:
\begin{align*}
S_{0}d\Delta-f_{d}
\end{align*}
For a riskless portfolio, the two must equal:
\begin{align*}
S_{0}u\Delta-f_{u}&=S_{0}d\Delta-f_{d}\\
\Delta&=\frac{f_{u}-f_{d}}{S_{0}u-S_{0}d}
\end{align*}
$\Delta$ is the ratio of the change in option price to the change in stock price as $t_{0}$ approaches $t_{T}$. Let the risk-free rate of interest $=r$. The present value of the portfolio is:
\begin{align*}
(S_{0}u\Delta-f_{u})e^{-rT}
\end{align*}
The portfolio set up cost (at $t_{0}$) is:
\begin{align*}
S_{0}\Delta-f
\end{align*}
Then,
\begin{align*}
S_{0}\Delta-f&=(S_{0}u\Delta-f_{u})e^{-rT}\\
f&=S_{0}\Delta(1-ue^{-rT})+f_{u}e^{-rT}
\end{align*}
Recall that
\begin{align*}
\Delta&=\frac{f_{u}-f_{d}}{S_{0}u-S_{0}d}
\end{align*}
Substituting $\Delta$,
\begin{align*}
f&=S_{0}\left(\frac{f_{u}-f_{d}}{S_{0}u-S_{0}d}\right)(1-ue^{-rT})+f_{u}e^{-rT}\\
&=S_{0}\left(\frac{f_{u}-f_{u}ue^{-rT}-f_{d}+f_{d}ue^{-rT}}{S_{0}u-S_{0}d}\right)+f_{u}e^{-rT}\\
&=\frac{S_{0}f_{u}-S_{0}f_{u}ue^{-rT}-S_{0}f_{d}+S_{0}f_{d}ue^{-rT}+S_{0}f_{u}ue^{-rT}-S_{0}f_{u}de^{-rT}}{S_{0}u-S_{0}d}\\
&=\frac{S_{0}f_{u}-S_{0}f_{d}+S_{0}f_{d}ue^{-rT}-S_{0}f_{u}de^{-rT}}{S_{0}u-S_{0}d}\\
&=\frac{f_{u}(1-de^{-rT})+f_{d}(ue^{-rT}-1)}{u-d}
\end{align*}
Let
\begin{align*}
fe^{rT}&=\frac{f_{u}(e^{rT}-e^{rT}de^{-rT})+f_{d}(e^{rT}ue^{-rT}-e^{rT})}{u-d}\\
&=\frac{f_{u}(e^{rT}-d)+f_{d}(u-e^{rT})}{u-d}\\
&=f_{u}\left(\frac{e^{rT}-d}{u-d}\right)+f_{d}\left(\frac{u-e^{rT}}{u-d}\right)
\end{align*}
Where
\begin{align*}
p=\frac{e^{rT}-d}{u-d} \text{ ; }
1-p=\frac{u-e^{rT}}{u-d}
\end{align*}
Then
\begin{align*}
f=[f_{u}(p)+f_{d}(1-p)]e^{-rT}\\
\end{align*}
Consider the same scenario, where $r=0.05$, $T=0.25$, $f_{u}=1$, $f_{d}=0$, $u=1.10$, and $d=0.90$:\\
\begin{align*}
p&=\frac{e^{0.05\cdot0.25}-0.9}{1.1-0.9}=0.5629\\
f&=e^{-0.05\cdot0.25}(0.5629\cdot1+0.4371\cdot0)=0.5559
\end{align*}

\newpage
\begin{thebibliography}{1}
\bibitem{} Hull, John C. {\em Fundamentals of Futures and Options Markets.} 2017.
\end{thebibliography}

\end{document}