\chapter{P-positions and N-positions}
\label{chapter:combinatorial-games}
\marginpar{%
  This part is based on Part I of
  ``Game Theory''
  by Ferguson.
}
\marginurl{%
    Combinatorial Games:\\\noindent
    Introduction to Combinatorial Game Theory \#1
}{youtu.be/DbCKHPlMN2c}
In this part we use our knowledge about basics of mathematical reasoning
to study games similar to checkers, chess, shogi, and tic tac toe. The games we
are going to study are called combinatorial games. In these games there are two
players, each know all the information, there are no chance moves, and when the
game ends there is always a winner.
(The last condition implies that among the aforementioned games only checkers
are combinatorial since all of them allow draws; however, we may change the
rules to disallow the draws and this change would make all of them
combinatorial.) Such a game is determined by a set of positions, and possible
moves from each position for each player. Usually, players are taking turns
until they reach a position such that no moves are possible and one of the
player is declared a winner.

\section{Take-Away Game}

Since chess, shogi and even tic tac toe are relatively complicated,
we are going to start from much simpler example of combinatorial games.
\begin{game}[Take-Away Game]
\label{game:take-away-21-3-2-1}
  In this game there are two players.
  \begin{itemize}
    \item They have a pile of $21$ chips.
    \item They make moves in turns with player I starting,
      each move consists of moving one, two or three chips out of the pile.
    \item The player that removes the last chip wins.
  \end{itemize}
\end{game}
The question we would like to answer is whether there is a strategy for one of
the players to always win. So in the rest of this part we assume that both
players are playing optimally; i.e., if there is a winning strategy they follow
the strategy.

To analyze this game we need the following two observations:
\begin{enumerate}
  \item the game is symmetric and the only difference between the players is
    who makes the first move, and
  \item if at some point the players have $n$ chips it does not matter how they
    achieved this, it will not affect the rest of the game.
\end{enumerate}
Using these remarks and induction (this style of induction is sometimes
referred as \emph{backward induction}) we are able to analyze the game.

Let us consider some certain states of the game.
Assume that they have at most $3$ chips left, in this case the player that make
the move wins. However, if there are $4$ chips, the
player that makes the first move should always take at least $1$ chip so she
loses since after her turn there are at most $3$ chip. Similarly, if there
are $5$ chips, the first player to move wins since she can take a chip and
make the second player to start with $4$.

So we can formulate the following conjecture.
Assume that $n$ chips left in the pile. Let $r$ be the remainder of $n$ modulo
$4$. Then if $r = 0$, the first player to move loses, otherwise, the other
player loses.

Let us prove this using induction. We already proved the base case so
we need to prove the induction step from $n$ to $n + 1$.
\begin{itemize}
  \item If $n \equiv 0 \pmod{4}$, then the first player to move can remove one
    chip and the other player will start with $n$ chips so by the induction
    hypothesis he/she loses.
  \item If $n \equiv 1 \pmod{4}$, then the first player to move can remove two
    chips and the other player will start with $n$ chips so by the induction
    hypothesis he/she loses.
  \item If $n \equiv 2 \pmod{4}$, then the first player to move can remove three
    chips and the other player will start with $n$ chips so by the induction
    hypothesis he/she loses.
  \item If $n \equiv 3 \pmod{4}$, then after the current player moves the other
    player will start with either $n$, or $n - 1$, or $n - 2$ chips. But all
    these numbers have non-zero remainders modulo $4$. So the other player
    can win in any case.
\end{itemize}

To study combinatorial games we need to give a formal definition of them.
\begin{definition}
  A game is combinatorial if
  \begin{itemize}
    \item there are two players,
    \item there is a set of possible positions in the game,
    \item for each position and each player, there is a fixed set of possible
      legal moves,
    \item players alternate moving,
    \item the game ends when no moves are possible for the player whose
      turn is to move.
  \end{itemize}
  There are possible winning conditions,
  \begin{description}
    \item [normal play rule:] the player that made the last move wins, and
    \item [mis\`ere play rule:] the player that made the last move loses.
  \end{description}
  If the game never ends, we declare a draw. If the game always ends, we
  say that the game satisfies \emph{the ending condition}.

  If the possible moves are the same for both players the game is
  called \emph{impartial} otherwise it is called \emph{partisan}.
\end{definition}

Note that these games do not allow random moves, hidden information,
simultaneous moves, and a draw in a finite number of steps so
poker, battleships, rock-paper-scissors, and tic tac toe are not
combinatorial games.

Since we gave a formal definition of combinatorial games we can give a framework
that allows to analyze these games.
\marginurl{%
    P-positions and N-positions:\\\noindent
    Introduction to Combinatorial Game Theory \#2
}{youtu.be/YV_oWBi1_ck}
\begin{definition}
  We say that a position in a combinatorial game is \emph{terminal} if there
  are no legal moves.

  All terminal positions are \emph{P-positions}. Every position that allows for
  the current player to move to a P-position is an \emph{N-position}. If all
  possible moves lead to N-positions, then the position is a P-position.

  For the game using the Mis\`ere rule, the definition is the same except the
  terminal positions are N-positions.
\end{definition}

Using this definition, one may create the following procedure that would allow
to determine which positions are P-positions and which are N-positions.
\begin{template}
  \textbf{Steps necessary to find P- and N-positions} \\

  \begin{enumerate}
    \item Label all terminal positions as P-positions.
    \item If some position is not labeled but all the moves lead to labeled
      positions, then label the position using the definition; i.e., if there
      are moves leading to P-position, it is an N-position, otherwise it is a
      P-position.
    \item If not all the positions are labeled, go to Step~2.
  \end{enumerate}
\end{template}

Note that P- and N-positions are defined recursively so in some games not all
the positions are either P- or N-positions. (For example, if there are no
terminal positions.) However, \Cref{theorem:grundy-to-np}
proves that if the game satisfies the ending condition, then all the positions
are either P- or N-positions.

\begin{table}[h!]
  \centering
  \begin{tabular}{l l l l l l l l l}
      \toprule
      0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
      \midrule
      P & N & N & N & P & N & N & N & P \\
      \bottomrule
  \end{tabular}
  \caption{P-positions and N-positions for \Cref{game:take-away-21-3-2-1}}
  \label{table:take-away-21-3-2-1}
\end{table}

So in \Cref{game:take-away-21-3-2-1} the only terminal position is $0$;
hence, $0$ is a P-position. Similarly we can go to $0$ from $1$, $2$, and
$3$ so they are N-positions. Hence, $4$ is a P-position,
since all the moves from $4$ lead to N-positions.
\begin{exercise}
  Show that a position $n$ is a P-position if $4$ divides $n$, and
  it is an N-position otherwise.
\end{exercise}

In other words, in this game, P-positions coincide with the positions where the
current player loses. However, it is not a coincidence.
\begin{theorem}
  If some position in a combinatorial game is an N-position, then the player to
  move has a winning strategy if we start from this position. If the position
  is a P-position, then the other player has a winning strategy.
\end{theorem}

\subsection{Subtractraction Games}
Let us define a big class of games that generalizes the take-away game we
discussed at the beginning of the chapter.
\begin{game}
  Let $S \subseteq \N$ be some set. The subtraction game with the subtraction
  set $S$ is the following combinatorial game.
  Two players start with a pile of $n$ chips.
  On each move they remove $s \in S$ chips out of the pile.
\end{game}

So \Cref{game:take-away-21-3-2-1} is the subtraction game with the subtraction
set $\set{1, 2, 3}$.

Let us analyze the subtraction game with the subtraction set $\set{1, 3, 4}$.
\begin{table}[h!]
  \centering
  \begin{tabular}{l l l l l l l l l}
      \toprule
      0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
      \midrule
      P & N & P & N & N & N & N & P & N \\
      \bottomrule
  \end{tabular}
  \caption{P-positions and N-positions for \Cref{game:take-away-21-3-2-1}}
  \label{table:subtraction-4-3-1}
\end{table}
Clearly $0$ is a P-position since it is the only terminal position in the game.
We can go to $0$ from $1$ so $1$ is an N-position. The only possible move from
$2$ is to $1$ so $2$ is a P-position. From $3$ and $4$ we can go to $0$
so they are N-positions. From $5$ and $6$ one may go to $2$ so they are a
N-positions as well. Hence, $7$ is a P-position.

Now we may notice the pattern: $n$ is a P-position iff $n \equiv 0 \pmod{7}$
or $n \equiv 2 \pmod{7}$. We prove is using induction. The base case for $n < 8$
we already proved. Let us now prove the induction step. Assume that the
statement is true for all $k < n$. Consider the following cases.
\begin{enumerate}
  \item If $n \equiv 0 \pmod{7}$, the current player can move to
    $n - 1 \equiv 5 \pmod {7}$, $n - 3 \equiv 4 \pmod {7}$, or
    $n - 3 \equiv 5 \pmod {7}$ which are all N-positions so $n$ is a P-position.
  \item If $n \equiv 1 \pmod{7}$, the current player can move to $n - 1$ which
    is a P-position so $n$ is an N-position.
  \item If $n \equiv 2 \pmod{7}$, the current player can move to
    $n - 1 \equiv 1 \pmod {7}$, $n - 3 \equiv 6 \pmod {7}$, or
    $n - 4 \equiv 5 \pmod {7}$ which are all N-positions so $n$ is a P-position.
  \item If $n \equiv 3 \pmod{7}$, the current player can move to $n - 1$ which
    is a P-position so $n$ is an N-position.
  \item If $n \equiv 4 \pmod{7}$, the current player can move to $n - 4$ which
    is a P-position so $n$ is an N-position.
  \item If $n \equiv 5 \pmod{7}$, the current player can move to $n - 3$ which
    is a P-position so $n$ is an N-position.
  \item If $n \equiv 6 \pmod{7}$, the current player can move to $n - 4$ which
    is a P-position so $n$ is an N-position.
\end{enumerate}

\begin{chapterendexercises}
  \exercise
    Two players I and II are playing the following game.
    \begin{itemize}
      \item They start with a number $0$ written on a blackboard.
      \item On each step one of the players replace a number $n$ on the
        blackboard by either $n + 1$ or by $n + 2$.
      \item Player I makes the first move and players do moves one
        after another.
      \item The player who writes $20$ wins.
    \end{itemize}
    Who has a winning strategy?
    (Note that the game is not a combinatorial game).
  \exercise
    Two players I and II are playing the following game.
    \begin{itemize}
      \item Initially, there are $20$ numbers written on a blackboard:
        $10$ numbers $1$ and $10$ numbers $2$.
      \item On each step one of the players select two numbers;
        and if they were the same, replace them by $2$;
        otherwise, replace them by $1$.
      \item Player I makes the first move and players do moves one
        after another.
    \end{itemize}
    Who is the winner? (Note that the game is not a combinatorial game).
  \exercise Consider the subtraction game where players may subtract $2$ and $3$
    chips on their turn, is $5$ an N-position?
  \exercise Consider the Mis\`ere subtraction game where players may subtract
    $1$, $2$ or $5$ chips on their turn, identify N-positions and
    P-positions.
  \exercise Consider the Mis\`ere subtraction game where players may subtract
    $1$, $5$ or $6$ chips on their turn, identify N-positions and
    P-positions.
  \exercise
    In the subtraction game where players may subtract $1$, $2$, or $5$ chips
    on their turn, identify N-positions and P-positions.
  \exercise Two players one by one put bishops on the chessboard such that none
    of the bishops attack each other. Determine the winning strategy.
  \exercise Consider the following game: two players I and II are writing an
    $11$-digit number from left to right, one digit after another. Player I
    wins  if $7$ divides the number and player II wins otherwise.
    Determine who is the winner if player I makes the first move.
\end{chapterendexercises}


\section{HW2}
%\begin{enumerate}
\begin{QandA}
	\item Translate the following sentences into Predicate Logic.
	(You may choose your own letters to serve as non-logical constants,)
	\begin{QandA}
		\item If one person is taller than another person and that second person is taller than a third one, then the first is taller than the third one.
		\begin{answered}
			$(\forall x)(\forall y)(\forall z)(\text{person}(x) \land \text{person}(y) \land \text{person}(z) \land
			x \ne y \land x \ne z \land y \ne z \land \text{taller}(x, y) \land \text{taller}(y,z)) \rightarrow (\text{taller}(x,z))$
		\end{answered}

		\item If one number is between two other numbers, then neither of the two others is between it and the third one.
		\begin{answered}
			$(\forall a)(\forall b)(\forall c)(\text{number}(a) \land \text{number}(b) \land \text{number}(c) \land a < b \land b < c)
			\rightarrow (\neg (b < a \land a < c) \land \neg (b < c \land c < a))$
		\end{answered}

		\item If you move a wolf, a goat and a cabbage across a river and you have a boat that can hold two but no more than two of the four of you, then there is exactly one strategy (for getting all of you safely across) .
		(N.B. you do not need to translate the part in parentheses.)
		\begin{answered}
			$(\forall x)(\forall y)(\forall z)(\forall k)(\forall w)(\text{wolf}(x) \land \text{goat}(y) \land \text{cabbage}(z) \land
			\text{you}(k) \land \text{boat}(w) \land (\text{canHold}(w,\lbrace x \rbrace) \land \text{canHold}(w,\lbrace y \rbrace) \land \text{canHold}(w,\lbrace z \rbrace)
			\land \text{canHold}(w,\lbrace k \rbrace) \land \text{canHold}(w,\lbrace x,y \rbrace) \land \text{canHold}(w,\lbrace x,z \rbrace) \land \text{canHold}(w,\lbrace x,k \rbrace) \land
			\text{canHold}(w,\lbrace y,z \rbrace) \land \text{canHold}(w,\lbrace y,k \rbrace) \land \text{canHold}(w,\lbrace z,k \rbrace) \land \neg \text{canHold}(w,\lbrace x,y,z \rbrace) \land
			\neg \text{canHold}(w,\lbrace x,y,k \rbrace) \land \neg \text{canHold}(w,\lbrace x,z,k \rbrace) \land \neg \text{canHold}(w,\lbrace y,z,k \rbrace) \land \neg \text{canHold}(w,\lbrace x,y,z,k \rbrace) \land \text{moveAcrossRiver}(x) \land \text{moveAcrossRiver}(y)
			\land \text{moveAcrossRiver}(z) \land \text{moveAcrossRiver}(k)) \rightarrow
			(\exists x)(\text{strategy}(x) \land \text{True}(x)) \land (\forall y)((\text{strategy}(y) \land y \ne x) \rightarrow (\neg \text{True}(y)))$
		\end{answered}
	\end{QandA}
	\item Give as many non-equivalent translations of sentence (a) into Predicate Logic as you can think of.
	\begin{QandA}
		\item Three girls met two boys.
		\begin{answered}
			\begin{itemize}
			\item $(\forall x)(\forall y)(\forall z)(\forall a)(\forall b)(\text{girl}(x) \land \text{girl}(y) \land \text{girl}(z) \land
			x \ne y \land y \ne z \land x \ne z \land \text{boy}(a) \land \text{boy}(b) \land a \ne b \land
			\text{met}(\lbrace x,y,z \rbrace, \lbrace a,b \rbrace)$
			\footnote{
			$``\text{met}"$ in $ \text{met}(\lbrace x,y,z \rbrace, \lbrace a,b \rbrace)$ has meaning of
			$\lbrace \alpha | (\forall \alpha)(\forall \beta)((\text{function}(\alpha) \land \text{function}(\beta) \land \alpha \ne \beta)
			\rightarrow (\alpha \in \lbrace a, b \rbrace ^ {\lbrace x, y, z \rbrace} \land \beta \not\in \lbrace a, b \rbrace ^ {\lbrace x, y, z \rbrace})) \rbrace$ (we use PC loosely here as ). In words, $``\text{met}"$ is a set of functions that maps set $\lbrace x,y,z \rbrace$ to set
			$\lbrace a,b \rbrace$. $\lbrace a, b \rbrace ^ {\lbrace x, y, z \rbrace}$
			represents all possible combinations of $``\text{met}"$ relation that can happen between boys and girls.}
			\item $(\forall x)(\forall y)(\forall z)(\forall a)(\forall b)(\text{girl}(x) \land \text{girl}(y) \land \text{girl}(z) \land
						x \ne y \land y \ne z \land x \ne z \land \text{boy}(a) \land \text{boy}(b) \land a \ne b
						\land \text{met}(x,a) \land \text{met}(x,b) \land \text{met}(y,a) \land \text{met}(y,b) \land \text{met}(z,a) \land
						\text{met}(z,b)
				)$
			\end{itemize}
		\end{answered}
	\end{QandA}

	\item (Note well: this exercise consists of 4 parts: a, b, c and d.)
	\begin{QandA}
		\item Translate the following sentences into Predicate Logic. Use the 1-place predicate P for ‘person’ and the 2-place predicate R for `met’.
		\begin{QandA}
			\item No one has met everyone.
			\begin{answered}
				$(\forall x)(\exists y)(P(x) \land P(y) \land x \ne y \land \neg R(x,y))$
			\end{answered}
			\item No one has met anyone.
			\begin{answered}
				$\neg ((\exists x)(\exists y)(P(x) \land P(y) \land x \ne y \land R(x,y)))$
			\end{answered}
			\item No one has met no one.
			\begin{answered}
				$(\forall x)(\exists y)(P(x) \land P(y) \land x \ne y \land R(x,y))$
			\end{answered}
			\item If someone has met no one, then no one has met everyone.
			\begin{answered}
				$(\exists x)(\forall y)((P(x) \land P(y) \land x \ne y \land \neg R(x,y)) \rightarrow
				(\neg (\exists z)(P(z) \land z \ne y \land R(z,y)))$
			\end{answered}
			\item If someone has met someone, then it is not the case that no one has met anyone.
			\begin{answered}
				$((\exists x)(\exists y)(P(x) \land P(y) \land x \ne y \land R(x,y)) \rightarrow
				(\neg (\forall x)(\forall y)(P(x) \land P(y) \land x \ne y \land \neg R(x,y)))$
			\end{answered}
		\end{QandA}
		\item Let $M = \langle U,I \rangle$ be the following model for the language ${P,R}$:
		\begin{align*}
			  & U = \lbrace a,b,c,d \rbrace                                                   \\
			  & I(P) = U (\text{so every individual in the universe of M is a person})        \\
			  & I(R) = \lbrace <a,b>, <a,c>, <a,d>, <b,a>, <c,a>, <d,a>, <c,d>, <d,c>,\rbrace
		\end{align*}
		\begin{QandA}
			\item Which of the sentences (i)-(v) are true in M and which false, assuming that the words
			no one, everyone, anyone, someone range over persons and that R translates the transitive verb meet?
			\begin{answered}
				(i), (ii), (iv) are false and (iii), (v) are true.
			\end{answered}
			\item Go through the calculation of the truth value in M of (i) by applying the truth definition for PC to the translation of (i) into PC you have given under (a).
			\begin{answered}
				\begin{align*}
				 & \llbracket (\forall x)(\exists y)(P(x) \land P(y) \land x \ne y \land \neg R(x,y)) \rrbracket_{M,a} = 1  \\
				\text{iff} & \; \text{for every} \; a' \approx_{x} a \quad \llbracket (\exists y)(P(x) \land P(y) \land x \ne y \land \neg R(x,y)) \rrbracket_{M,a'} = 1 \\
				\text{iff} & \; \text{for every} \; a' \approx_{x} a \; \text{for some} \; a'' \approx_{y} a' \quad \llbracket (P(x) \land P(y) \land x \ne y \land \neg R(x,y)) \rrbracket_{M,a''} = 1 \\
				\text{iff} &\; \text{for every} \; a' \approx_{x} a \; \text{for some} \; a'' \approx_{y} a' \quad
				\langle \llbracket x \rrbracket_{M,a''} \rangle \in I_{M}(P) \; and \; \\
				& \langle \llbracket y \rrbracket_{M,a''} \rangle \in I_{M}(P)
				\; and \;  \llbracket x \rrbracket_{M,a''} \ne \llbracket y \rrbracket_{M,a''} \; and \; \llbracket R(x,y) \rrbracket_{M,a''} = 0 \\
				\text{iff} &\; \text{for every} \; a' \approx_{x} a \; \text{for some} \; a'' \approx_{y} a' \quad
				\langle \llbracket x \rrbracket_{M,a''} \rangle \in I_{M}(P) \; and \; \\
				& \langle \llbracket y \rrbracket_{M,a''} \rangle \in I_{M}(P)
				\; and \;  \llbracket x \rrbracket_{M,a''} \ne \llbracket y \rrbracket_{M,a''} \; and \;
				\langle \llbracket x \rrbracket_{M,a''}, \llbracket y \rrbracket_{M,a''} \rangle \not\in I_{M}(R) \\
				\text{iff} &\; \text{for every} \; a' \approx_{x} a \; \text{for some} \; a'' \approx_{y} a' \quad
				\langle a''(x) \rangle \in I_{M}(P) \; and \; \\
				& \langle a''(y) \rangle \in I_{M}(P)
				\; and \;  a''(x) \ne a''(y) \; and \;
				\langle a''(x), a''(y) \rangle \not\in I_{M}(R)
				\end{align*}
				The above statement is false because if we let $a''(x) = a$ and there is no such $a''(y)$ that can make whole PC to
				true. If $a''(y) = a$, then $a''(x) \ne a''(y)$ is false. If $a''(y) = b$, then $\langle a, b \rangle \in I_M(R)$,
				which makes $\langle a''(x), a''(y) \rangle \not\in I_{M}(R)$ false. If $a''(y) = c$, then $\langle a, c \rangle \in I_M(R)$,
				which makes $\langle a''(x), a''(y) \rangle \not\in I_{M}(R)$ false.
			\end{answered}
		\end{QandA}
		\item Which of the sentences (i)-(v), if any is/are true in all models for PC?
		\begin{answered}
			(v)
		\end{answered}
		\item For each of the sentences (i)-(v) that is not true in M modify the interpretation of $I(M)$ in such a way that this sentence comes out true in the modified model. (But leave the Universe $U$ as it is!)
		\begin{answered}
			\begin{itemize}
			\item (i) We modify the model by removing $\langle a,b \rangle$ from $I(R)$
			\item (ii) We modify the model by making $I(R) = \lbrace \langle a, a \rangle \rbrace$
			\item (iv) We modify the model by removing $\langle a,b \rangle, \langle a,c \rangle, \langle a,d \rangle$ from $I(R)$
			\end{itemize}
		\end{answered}
	\end{QandA}
	%\end{enumerate}
\end{QandA}


\section{Derivatives of Exponential \& Logarithmic Functions}\label{sec:DerivativeExpLog}
As with the sine function, we don't know anything about derivatives that allows
us to compute the derivatives of the exponential and logarithmic
functions without going back to basics. Let's do a little work with
the definition again:
\begin{eqnarray*}
\frac{d}{dx}a^x&=&\lim_{\Delta x\to 0} \frac{a^{x+\Delta x}-a^x}{\Delta x}\cr
\\
&=&\lim_{\Delta x\to 0} \frac{a^xa^{\Delta x}-a^x}{\Delta x}\cr
\\
&=&\lim_{\Delta x\to 0} a^x\frac{a^{\Delta x}-1}{\Delta x}\cr
\\
&=&a^x\lim_{\Delta x\to 0} \frac{a^{\Delta x}-1}{\Delta x}\cr
\end{eqnarray*}
There are two interesting things to note here: As in the case of the
sine function we are left with a limit that involves $\Delta x$ but
not $x$, which means that if $\ds \lim_{\Delta x\to 0} (a^{\Delta x}-1)/\Delta x$ exists,
then it is a constant number. This means that $\ds a^x$ has a remarkable property: its
derivative is a constant times itself.

We earlier remarked that the hardest limit we would compute is
$\ds \lim_{x\to0}\sin x/x=1$; we now have a limit that is just a bit too
hard to include here. In fact the hard part is to see that
$\ds \lim_{\Delta x\to 0} (a^{\Delta x}-1)/\Delta x$ even exists---does this fraction really get closer
and closer to some fixed value? Yes it does, but we will not prove
this fact.

We can look at some examples. Consider $\ds (2^x-1)/x$ for some small
values of $x$: 1, $0.828427124$, $0.756828460$, $0.724061864$,
$0.70838051$, $0.70070877$ when $x$ is 1, $1/2$, $1/4$, $1/8$, $1/16$,
$1/32$, respectively. It looks like this is settling in around $0.7$,
which turns out to be true (but the limit is not exactly $0.7$).
Consider next $\ds (3^x-1)/x$: $2$,  $1.464101616$,
$1.264296052$, $1.177621520$, $1.13720773$, $1.11768854$, at the same
values of $x$. It turns out to be true that in the limit this
is about $1.1$. Two examples don't establish a pattern, but if you do
more examples you will find that the limit varies directly with the
value of $a$: bigger $a$, bigger limit; smaller $a$, smaller limit. As
we can already see, some of these limits will be less than 1 and some
larger than 1. Somewhere between $a=2$ and $a=3$ the limit will be
exactly 1; the value at which this happens is called $e$, so that
$$\lim_{\Delta x\to 0} {e^{\Delta x}-1\over \Delta x}=1.$$ As you
might guess from our two examples, $e$ is closer to 3 than to 2, and
in fact $e\approx 2.718$.

Now we see that the function $\ds e^x$ has a truly remarkable property:
\begin{eqnarray*}
{d\over dx}e^x&=&\lim_{\Delta x\to 0} {e^{x+\Delta x}-e^x\over \Delta x}\cr\\
&=&\lim_{\Delta x\to 0} {e^xe^{\Delta x}-e^x\over \Delta x}\cr\\
&=&\lim_{\Delta x\to 0} e^x{e^{\Delta x}-1\over \Delta x}\cr\\
&=&e^x\lim_{\Delta x\to 0} {e^{\Delta x}-1\over \Delta x}\cr\\
&=&e^x\cr
\end{eqnarray*}
That is, $\ds e^x$ is its own derivative, or in other words the
slope of $\ds e^x$ is the same as its height, or the same as its second
coordinate: The function $\ds f(x)=e^x$ goes through the point $\ds (z,e^z)$
and has slope $\ds e^z$ there, no matter what $z$ is. It is sometimes
convenient to express the function $\ds e^x$ without an exponent, since
complicated exponents can be hard to read. In such cases we use
$\exp(x)$, e.g., $\ds \exp(1+x^2)$ instead of
$\ds e^{1+x^2}$.

What about the logarithm function? This too is hard, but as the
cosine function was easier to do once the sine was done, so is the
logarithm easier to do now that we know the derivative of the
exponential function. Let's start with $\ds \log_e x$, which as you
probably know is often abbreviated $\ln x$ and called the ``natural
logarithm'' function.

Consider the relationship between the two functions,
namely, that they are inverses, that one ``undoes'' the
other. Graphically this means that they have the same graph except
that one is ``flipped'' or ``reflected'' through the line $y=x$:
\figure[!ht]
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <1truecm,1truecm> point at 0 0
\setplotarea x from -2 to 2, y from 0 to 4
\axis left shiftedto x=0 /
\axis bottom shiftedto y=0 /
\setquadratic
\plot  -2.000 0.250 -1.867 0.274 -1.733 0.301 -1.600 0.330 -1.467 0.362
-1.333 0.397 -1.200 0.435 -1.067 0.477 -0.933 0.524 -0.800 0.574
-0.667 0.630 -0.533 0.691 -0.400 0.758 -0.267 0.831 -0.133 0.912
0.000 1.000 0.133 1.097 0.267 1.203 0.400 1.320 0.533 1.447
0.667 1.587 0.800 1.741 0.933 1.910 1.067 2.095 1.200 2.297
1.333 2.520 1.467 2.764 1.600 3.031 1.733 3.325 1.867 3.647
2.000 4.000 /
\setcoordinatesystem units <1truecm,1truecm> point at -5 -2
\setplotarea x from 0 to 4, y from -2 to 2
\axis left shiftedto x=0 /
\axis bottom shiftedto y=0 /
\setquadratic
\plot 0.250 -2.000 0.375 -1.415 0.500 -1.000 0.625 -0.678 0.750 -0.415
0.875 -0.193 1.000 0.000 1.125 0.170 1.250 0.322 1.375 0.459
1.500 0.585 1.625 0.700 1.750 0.807 1.875 0.907 2.000 1.000
2.125 1.087 2.250 1.170 2.375 1.248 2.500 1.322 2.625 1.392
2.750 1.459 2.875 1.524 3.000 1.585 3.125 1.644 3.250 1.700
3.375 1.755 3.500 1.807 3.625 1.858 3.750 1.907 3.875 1.954
4.000 2.000 /
\endpicture}}
\caption{The exponential and logarithmic functions. \label{fig:exponential and log functions}}
\endfigure

\noindent
This means that the slopes of these two functions are closely related
as well: For example, the slope of $\ds e^x$ is $e$ at $x=1$; at the
corresponding point on the $\ln(x)$ curve, the slope must be $1/e$,
because the ``rise'' and the ``run'' have been interchanged. Since the
slope of $\ds e^x$ is $e$ at the point $(1,e)$, the slope of $\ln(x)$ is
$1/e$ at the point $(e,1)$.

\figure
\centerline{\vbox{\beginpicture
\normalgraphs
%\ninepoint
\setcoordinatesystem units <1truecm,1truecm> point at 0 0
\setplotarea x from -2 to 2, y from 0 to 4
\axis left shiftedto x=0 /
\axis bottom shiftedto y=0 /
\setquadratic
\plot  -2.000 0.250 -1.867 0.274 -1.733 0.301 -1.600 0.330 -1.467 0.362
-1.333 0.397 -1.200 0.435 -1.067 0.477 -0.933 0.524 -0.800 0.574
-0.667 0.630 -0.533 0.691 -0.400 0.758 -0.267 0.831 -0.133 0.912
0.000 1.000 0.133 1.097 0.267 1.203 0.400 1.320 0.533 1.447
0.667 1.587 0.800 1.741 0.933 1.910 1.067 2.095 1.200 2.297
1.333 2.520 1.467 2.764 1.600 3.031 1.733 3.325 1.867 3.647
2.000 4.000 /
\setlinear
\plot -0.443 0 2 3.386 /
\setcoordinatesystem units <1truecm,1truecm> point at -5 -2
\setplotarea x from 0 to 4, y from -2 to 2
\axis left shiftedto x=0 /
\axis bottom shiftedto y=0 /
\setquadratic
\plot 0.250 -2.000 0.375 -1.415 0.500 -1.000 0.625 -0.678 0.750 -0.415
0.875 -0.193 1.000 0.000 1.125 0.170 1.250 0.322 1.375 0.459
1.500 0.585 1.625 0.700 1.750 0.807 1.875 0.907 2.000 1.000
2.125 1.087 2.250 1.170 2.375 1.248 2.500 1.322 2.625 1.392
2.750 1.459 2.875 1.524 3.000 1.585 3.125 1.644 3.250 1.700
3.375 1.755 3.500 1.807 3.625 1.858 3.750 1.907 3.875 1.954
4.000 2.000 /
\setlinear
\plot 0 -0.443 3.386 2 /
\endpicture}}
\caption{The exponential and logarithmic functions. \label{fig:slope of exponential and log functions}}
\endfigure

More generally, we know that the slope of $\ds e^x$ is $\ds e^z$ at the point
$\ds (z,e^z)$, so the slope of $\ln(x)$ is $\ds 1/e^z$ at $\ds (e^z,z)$.
In other words, the slope of $\ln x$ is the reciprocal of the first
coordinate at any point; this means that the slope of $\ln x$ at
$(x,\ln x)$ is $1/x$. The upshot is:
$${d\over dx}\ln x = {1\over x}.$$
We have discussed this from the point of view of the graphs, which is
easy to understand but is not normally considered a rigorous
proof---it is too easy to be led astray by pictures that seem
reasonable but that miss some hard point. It is possible to do this
derivation without resorting to pictures, and indeed we will see an
alternate approach soon.

Note that $\ln x$ is defined only for $x>0$. It is sometimes useful to
consider the function $\ln |x|$, a function defined for
$x\not=0$. When $x<0$, $\ln |x|=\ln(-x)$ and
$${d\over dx}\ln |x|={d\over dx}\ln (-x)={1\over -x}(-1)={1\over x}.$$
Thus whether $x$ is positive or negative, the derivative is the same.

What about the functions $\ds a^x$ and $\ds \log_a x$? We know that the
derivative of $\ds a^x$ is some constant times $\ds a^x$ itself, but what
constant? Remember that ``the logarithm is the exponent'' and you will
see that $\ds a=e^{\ln a}$. Then
$$a^x = (e^{\ln a})^x = e^{x\ln a},$$
and we can compute the derivative using the chain rule:
$${d\over dx} a^x = {d\over dx}(e^{\ln a})^x = {d\over dx}e^{x\ln a} =
(\ln a)e^{x\ln a} =(\ln a)a^x.$$
The constant is simply $\ln a$. Likewise we can compute the derivative
of the logarithm function $\ds \log_a x$. Since
$$x=e^{\ln x}$$
we can take the logarithm base $a$ of both sides to get
$$
\log_a(x)=\log_a(e^{\ln x})=\ln x \log_a e.
$$
Then
$${d\over dx}\log_a x = {1\over x}\log_a e.$$
This is a perfectly good answer, but we can improve it slightly.
Since
\begin{eqnarray*}
a&=&e^{\ln a}\cr
\log_a(a) &=& \log_a(e^{\ln a}) = \ln a\log_a e\cr
1&=&\ln a\log_a e\cr
{1\over \ln a}&=&\log_a e,\cr
\end{eqnarray*}
we can replace $\ds \log_a e$ to get
$${d\over dx}\log_a x = {1\over x\ln a}.$$

You may if you wish memorize the formulas.

\begin{formulabox}[Derivative Formulas for $a^x$ and $\log_ax$]
$${d\over dx}a^x = (\ln a)a^x \quad \hbox{and}\quad
{d\over dx}\log_a x = {1\over x\ln a}.$$
\end{formulabox}

Because the ``trick'' $\ds a=e^{\ln a}$ is often useful, and sometimes
essential, it may be better to remember the trick, not the formula.

\begin{example}{Derivative of Exponential Function}{DerivativeExponentialFunction}
Compute the derivative of $\ds f(x)=2^x$.
\end{example}

\begin{solution}
\begin{eqnarray*}
{d\over dx}2^{x} &=& {d\over dx}(e^{\ln 2})^x\\
\\
&=& {d\over dx}e^{x\ln 2}\\
\\
&=& \left({d\over dx} x\ln 2\right) e^{x\ln 2}\\
\\
&=& (\ln 2)  e^{x\ln 2}=2^x\ln2
\end{eqnarray*}\vspace{-0.2cm}
\end{solution}

\begin{example}{Derivative of Exponential Function}{DerivativeExponentialFunction2}
Compute the derivative of $\ds f(x)=2^{x^2}=2^{(x^2)}$.
\end{example}

\begin{solution}
\begin{eqnarray*}
{d\over dx}2^{x^2} &=& {d\over dx}e^{x^2\ln 2}\\
\\
&=& \left({d\over dx} x^2\ln 2\right) e^{x^2\ln 2}\\
\\
&=& (2\ln 2) x  e^{x^2\ln 2}\\
\\
&= & (2\ln 2) x 2^{x^2}
\end{eqnarray*}\vspace{-0.2cm}
\end{solution}

\begin{example}{Power Rule}{PowerRule}
Recall that we have not justified the power rule except when the
exponent is a positive or negative integer.
\end{example}

\begin{solution}
We can use the exponential function to take care of other exponents.
\begin{eqnarray*}
{d\over dx}x^r&=&{d\over dx}e^{r\ln x}\\
\\
&=&\left({d\over dx}r\ln x\right)e^{r\ln x}\\
\\
&=&(r{1\over x})x^r\\
\\
&=&rx^{r-1}
\end{eqnarray*}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for Section \ref{sec:DerivativeExpLog}}

\begin{enumialphparenastyle}

Find the derivatives of the functions.

%%%%%%%%%%
\begin{ex}
$\ds 3^{x^2}$
\begin{sol}
	$\ds 2\ln(3)x3^{x^2}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds {\sin x \over e^x}$
\begin{sol}
	$\ds {\cos x-\sin x \over e^x}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds (e^x)^2$
\begin{sol}
	$\ds 2e^{2x}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds \sin(e^x)$
\begin{sol}
	$\ds e^x\cos(e^x)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds e^{\sin x}$
\begin{sol}
	$\ds  \cos (x) e^{\sin x}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds x^{\sin x}$
\begin{sol}
	$\ds x^{\sin x}\left(\cos x\ln x+{\sin x\over x}\right)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds x^3e^x$
\begin{sol}
	$\ds 3x^2e^x+x^3e^x$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds x+2^x$
\begin{sol}
	$\ds 1+2^x\ln(2)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds (1/3)^{x^2}$
\begin{sol}
	$\ds -2x\ln(3)(1/3)^{x^2}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds e^{4x}/x$
\begin{sol}
	$\ds e^{4x}(4x-1)/x^2$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds \ln(x^3+3x)$
\begin{sol}
	$\ds (3x^2+3)/(x^3+3x)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds \ln(\cos(x))$
\begin{sol}
	$\ds -\tan(x)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds\sqrt{\ln(x^2)}/x$
\begin{sol}
	$\ds (1-\ln(x^2))/(x^2\sqrt{\ln(x^2)})$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds \ln(\sec(x) + \tan(x))$
\begin{sol}
	$\ds \sec(x)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds x^{\cos(x)}$
\begin{sol}
	$\ds x^{\cos(x)}(\cos(x)/x-\cos(x)\ln(x))$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds x\ln x$
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ln (\ln (3x) )$
\end{ex}

%%%%%%%%%%
\begin{ex}
$\ds {1+\ln (3x^2 )\over 1+ \ln(4x)}$
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the value of $a$ so that the tangent line to
$y=\ln(x)$ at $x=a$ is a line through the origin.  Sketch the
 resulting situation.
\begin{sol}
$e$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
If $\ds f(x) = \ln(x^3 + 2)$ compute $\ds f'(e^{1/3})$.
\end{ex}

\end{enumialphparenastyle}


\chapter{Mathematical appendix}
\label{chap:math}

In this chapter, we shall review the intuition behind basic results in probability theory and stochastic calculus. We shall omit most of the measure theory needed and be pretty vague of the technical arguments.

In the following we let $(\Omega, \F)$ be a probability space with a measure $\Pf$ be a probability measure. Here $\Omega$ is the state space, $\F$ is the $\sigma$-algebra of $\Omega$. Let $\Pm$ be also a probability measure on the space $(\Omega, \F)$. If for all $A \in \F$, it holds that $\Pf (A) = 0$ implies $\Pm (A) = 0$, then we say that $\Pm$ is absolutely continous with respect to $\Pf$ on $\F$ and we write $\Pm \ll \Pf$. If $\Pm \ll \Pf$ and $\Pf \ll \Pm$, then the measures are said to be equivalent on $\F$. Thus measures are equivalent if and only if their null sets are the same.

\section{Characteristic function and Fourier transformation}

A probability distribution function of random variable $X$ is any measurable function $f_X$ that satisfies
	\begin{align}
		\Pf ( X \in A ) = \int_A f_X \ \dx \mu
	\end{align}
for all $A \in \F$, where $\mu$ is the Lebesgue measure. The characteristic function $g_X$ is the function
	\begin{align}
		g_X (\omega) &= \E_{\Pf} \left( \e^{i \omega X} \right) \\
			&= \int_{-\infty}^{\infty} \e^{i \omega x} f_X(x) \ \dx x
	\end{align}
if the expectation exists. This is just the Fourier transformation of the probability distribution function. If the characteristic function $g_X$ is integrable, then the inverse Fourier transformation gives
	\begin{align}
		f_X = \frac{1}{2 \pi} \int_{-\infty}^{\infty} \e^{- i \omega x} g_X x \ \dx x .
	\end{align}
According to \cite{carrmadan1999optionvaluation}, Gil-Pelaez' Inversion gives that
	\begin{align}
		\Pf( x < X ) = \frac{1}{2} - \frac{1}{\pi} \int_0^{\infty} \frac{\im \left( \e^{-i \omega x} g_X( \omega) \right)}{ \omega} \ \dx  \omega
	\end{align}
and
	\begin{align}
		\Pf( x \geq X ) = \frac{1}{2} + \frac{1}{\pi} \int_0^{\infty} \re \left( \frac{\e^{-i  \omega x} g_X( \omega)}{ i  \omega} \right) \ \dx  \omega .
	\end{align}

\section{Radon-Nikod\'{y}m-theorem and the change of measure}
\label{sec:radonnikodymtheorem}

One of the basic tool of the probability measures is the Radon-Nikod\'{y}m-theorem. For the proof, see any basic text on the measure theory (for example, \textcite[pp. 449-450]{billingsley2012probabilityandmeasure}). We recall that a function $f$ is $\F$-measurable if $\{ \omega \in \Omega \ | \ f(\omega) \leq x \} \in \F$ for any $x \in \R$.

\begin{thm}[Radon-Nikod\'{y}m-theorem]
	If $\Pf$ and $\Pm$ be probability measures on measurable space $(\Omega, \F)$ and $\Pm \ll \Pf$, then there exists a non-negative $\F$-measurable function $\xi$ such that
	\begin{align}
	&\int_{\Omega} \xi \ \dx \Pf < \infty \text{ and } \\
	\Pm (A) = &\int_A \xi \ \dx \Pf
	\end{align}
	for all $A \in \F$. The function $\xi$ is $\Pf$-unique and it is called as the Radon-Nikod\'{y}m-derivate of $\Pf$ with respect to measure $\Pm$ and filtration $\F$.
\end{thm}

The Radon-Nikod\'{y}m-derivate of $\Pf$ with respect to $\Pm$ is denoted by
\begin{align}
\xi = \frac{\dx \Pm}{\dx \Pf}
\end{align}
and alternatively we may write
\begin{align}
\dx \Pm = \xi \dx \Pf .
\end{align}
Since $\Pm$ is a probability measure, it is clear that $\E_{\Pf} (\xi) = 1$.

If $X = \1_A$ for some $A \in \F$, then
\begin{align}
\E_{\Pm} (X) = \Pm (A) = \int_A \xi \dx \Pf = \E_{\Pf} ( \1_A \xi ) = \E_{\Pf} (\xi X) .
\end{align}
If $X$ is integrable and $\F$-measurable random variable, then we may approximate it with simple functions and we may conclude the following important consequence of the Radon-Nikod\'{y}m-derivate.

\begin{lemma}
	\label{radonnikodymconsequence}
	Suppose that the function $\xi$ is the Radon-Nikod\'{y}m-derivate of $\Pf$ with respect to $\Pm$. If $X$ is integrable and $\F$-measurable random variable, then
	\begin{align}
	\E_{\Pm} (X) = \E_{\Pf} ( \xi X).
	\end{align}
\end{lemma}

Conversely, if $\xi$ is $\F$-measurable, integrable and non-negative function with $\E_{\Pf} (\xi) = 1$, then we may define a function $\Pm : \F \rightarrow [0,1]$ by
\begin{align}
\label{RadonNikodymbyrandomvariable}
\Pm ( A ) = &\int_A \xi \dx \Pf = \E_{\Pf} ( \1_A \xi )
\end{align}
for all $A \in \F$. It is easy to see that $\Pm$ is a probability measure on measurable space $(\Omega, \F)$ and $\Pm \ll \Pf$. Also
\begin{align}
\xi = \frac{\dx \Pm}{\dx \Pf} .
\end{align}
By Equation \ref{RadonNikodymbyrandomvariable},
	\begin{align}
		\E_{\Pm} (X) = \E_{\Pf} (X \frac{\dx \Pm}{\dx \Pf} )
	\end{align}
holds for simple functions and, by limit argumentation, it holds for any integrable random variable. Heuristically
	\begin{align}
		 \int_A X \ \dx \Pm &= \int_A X \ \dx \Pf \ \frac{\dx \Pm}{\dx \Pf} \\
		 	&= \int_A X \ \dx \Pf
	\end{align}
for all $A \in \F$.

\section{Conditional expectation}

The associated $\sigma$-algebra $\F$ can be seen as the known information structure. Random variables are $\F$-measurable functions which means that the sets\footnote{These sets generate the Borel algebra of the reals.} $\{ \ \omega \ | \ X(\omega) \leq a \ \} \in \F$ for all $a \in \R$. This may be interpreted as that $\F$-measurable functions are those functions whose outcome is known based on the information $\F$.

If $\F = \{ \emptyset, \Omega \}$, then only constant functions are $\F$-measurable and knowning the value of random variable gives no information about the true state of the system $\omega$. If $\emptyset \not = A \subset \Omega$ and $A \in \F$, then $\1_A$ is $\F$-measurable function. So if we know the value of $\1_A$, we may deduce either $\omega \in A$ or $\omega \not \in A$ although we may not have exact information about the true state $\omega$ of the random system $\Omega$. So if $\G$ is a $\sigma$-sub-algebra of $\F$, then $\F$ carries more information than $\G$.

Let $X$ be a $\F$-measurable random variable and $\G$ a $\sigma$-sub-algebra of $\F$ generated by partion $B_1, B_2, \ldots, B_m$ of $\Omega$. For simplicity, we assume that $X$ is simple, meaning that
  \begin{align}
    X &= \sum_{i=1}^n x_i \1_{A_i}
  \end{align}
where $x_i \in \R$ and $A_i \in \F$ for $i=1,2, \ldots, n$ and the collection $A_1, A_2, \ldots A_n$ is a partition of $\Omega$. We denote $C_{ij} = A_i \cap B_j$.

If we know that the event $B_j$ is true, then $X = x_i$ only if $C_{ij} \not = \emptyset$ and, in a sense, the average value of $X$ will be
  \begin{align}
    y_j = \sum_{i=1}^n x_i \frac{\Pf( C_{ij} )}{ \Pf( B_j ) }
  \end{align}
assuming that $\Pf (B_j ) \not = 0$. If $\Pf (B_j ) = 0$, then
  \begin{align}
    \E (X \1_{B_j} ) = 0
  \end{align}
and we set $y_j = 0$. Now we may define a new random variable
  \begin{align}
    Y = \sum_{j=1}^m y_j \1_{B_j} .
  \end{align}
Now $Y$ is $\G$-measurable and integrable. Furthermore,
  \begin{align}
    \E (X \1_{B_j} ) &= \sum_{i=1}^n \E ( x_i \1_{A_i} \1_{B_j} ) \\
      &= \sum_{i=1}^n x_i \Pf ( C_{ij} ) \\
      &= y_j \Pf ( B_j ) \\
      &= \E (Y \1_{B_j} )
  \end{align}
for all $j = 1,2, \ldots, m$. This motivates us to define the conditional expectation given a $\sigma$-sub-algebra $\G$.

For a fixed $\F$-measurable and integrable random variable $X$, the conditional expectation of $X$ given $\G$ is the random variable $\E (X | \G)$ with the following properties:
  \begin{enumerate}[label=\roman*)]
    \item $\E (X | \G)$ is $\G$-measurable and integrable,
    \item for every $G \in \G$,
      \begin{align}
        \label{conditionalsecondcond}
        \int_G X \dx \Pf = \int_G \E (X | \G) \dx \Pf.
      \end{align}
  \end{enumerate}

Thus conditional expectation is a random variable and
\begin{align}
	\E (X) = \E (\E (X | \G)).
\end{align}
We may use Radon-Nikod\'{y}m-theorem or orthogonal projections in $\Le^2$-space to prove the existance of a conditional expectations and it is unique $\Pf$-surely. In the following, we shall not always make the distinction between sets or random variables that match everywhere or just $\Pf$-everywhere.

Let $X_i$ be the throw of a fair coin at the time $i$. So $X_i (\omega) \in \{ 0, 1\}$ with equal probabilities. For simplicity, we consider only two time periods $i=1,2$ and we code
	\begin{align}
		\Omega = \{ \ X_iX_j \ | \ i,j \in \{0,1\} \ \} = \{ \ 00, 01, 10, 11 \ \},
	\end{align}
with $\Pf( \omega ) = 1/4$ for all $\omega \in \Omega$. Let $\F = \{ \emptyset, \Omega \}$ and $X(ij)= X_1(i)+X_2(j)$. Now
\begin{align}
\E_{\Pf} \left( X \ | \ \F \right) = 1
\end{align}
since
	\begin{align}
		\int_\emptyset X \ \dx \Pf &= 0, \\
		\int_\Omega X \ \dx \Pf &= 1 \\
	\end{align}
If we know the result of the first throw, then we may pick
	\begin{align}
		\G = \F \ \cup \ \{ 00, 01 \} \ \cup \ \{ 10, 11 \}
	\end{align}
Now
	\begin{align}
		\int_{ \{ 00, 01 \} } X \ \dx \Pf &= \frac{X_1(0) + X_1(0) + X_2(0) + X_2(1)}{4} = \frac{1}{4}, \\
		\int_{ \{ 10, 11 \} } X \ \dx \Pf &= \frac{X_1(1) + X_1(1) + X_2(0) + X_2(1)}{4} = \frac{3}{4}, \\
		\int_{ \{ 00, 01 \} } \alpha \ \dx \Pf &= \frac{\alpha}{2}, \\
		\int_{ \{ 10, 11 \} } \beta \ \dx \Pf &= \frac{\beta}{2}, \\
	\end{align}
implies that  $Y$ defined by $Y(00) = Y(01) = \frac{1}{2}$ and $Y(10) = Y(11) = \frac{3}{2}$ is the conditional expectation of $X$ over $\G$ as now
	\begin{align}
		\int_G Y \ \dx \Pf = \int_G X \ \dx \Pf
	\end{align}
for all $G \in \G$.

If $\G = \{ \emptyset, \Omega \}$, then $\G$-measurable functions are constant functions. The integral over the empty set is zero for all integrable randon variables and
      \begin{align}
        \int_{\Omega} X \dx \Pf = \E (X) = \int_{\Omega} \E (X) \dx \Pf.
      \end{align}
We see that $\E (X | \{ \emptyset, \Omega \}) = \E (X)$ and the conditional expectation gives no further information. If $X$ is $\G$-measurable, then the equation \ref{conditionalsecondcond} is trivially satisfied and we see that $X = \E (X | \G)$. In particular, $X = \E (X | \F)$ as $X$ is $\F$-measurable.

We present some of the basic properties of conditional expectations.

\begin{thm}
Suppose that $\G$ and $\Ho$ are sub-$\sigma$-fields of $\F$ and $X$ and $Y$ are integrable random variables. Then
  \begin{enumerate}[label=\roman*)]
    \item $\E \left| \E ( X | \G ) \right| \leq \E \left| X \right|$,
    \item $\E (aX+bY | \G ) = a\E (X | \G ) + b\E (Y | \G )$ for all $a,b \in \R$,
    \item if $X \leq Y$, then $\E (X| \G) \leq \E (Y| \G)$,
    \item $\left| \E (X | \G) \right| \leq \E (\left| X \right| | \G)$,
    \item if $XY \in \Le^1 (\Omega, P)$ and $Y$ if $G$-measurable, then $\E (XY | \G) = Y \E (X | \G)$,
    \item if $\Ho \subseteq \G$, then $\E ( \E (X | \G ) | \Ho ) = \E ( \E (X | \Ho ) | \G ) =  \E (X | \Ho )$,
    \item if $\sigma (X)$ and $\G$ are independent, then $\E (X | \G) = \E (X)$,
    \item if $P(G) \in \{ 0,1 \}$ for all $G \in \G$, then $\E (X | \G) = \E (X)$,
  \end{enumerate}
\end{thm}

See \textcite[pp. 472--477]{billingsley2012probabilityandmeasure}

\section{Filtrations and martingales}

A collection $(\F_t)$  of $\sigma$-sub-algebras of $\F$ is called a filtration if $\F_t \subseteq F_s$ for all $0 \leq t \leq s$. Informally a filtration presents the flow of information. We assume some standard technical conditions for the filtrations. Every $\Pf$-null set must be a member of $\F_0$ and
  \begin{align}
    \F_t = \bigcap_{t < s} \F_s
  \end{align}
for all $t \geq 0$.

A stochastic process $X$ is a function $X : (\R_+ \cup \{ 0 \}) \times \Omega \rightarrow \R$. It is often written as $X = (X(t))$, where the argument $\omega \in \Omega$ is dropped. It is then a collection of random variables with index set $\{t \geq 0\}$. We say that the process $(X(t))$ is adapted to the filtration $(\F_t)$ if $X(t)$ is $\F_t$-measurable for each $t$. Thus the variable $X(t)$ of an adapted process contains the information of the process that has been accumulated so far.

A $(\F_t)$-adapted stochastic process is a martingale if $\E_{\Pf} \left( \left| X(t) \right| \right) < \infty$ and
  \begin{align}
    \E_{\Pf} ( X(s) \ | \ \F_t ) = X(t)
  \end{align}
for all $0 \leq t < s < \infty$. Thus a martingale is a process, where the present value is the best estimate for the all future expected values given the past information contained in the process.

\section{A stopping time and localization}
\label{sec:stoppingtime}

A random variable $\tau : \Omega \rightarrow \R_+ \cup \{ 0 \}$ is a stopping time with respect to the filtration $\F_t$ if
	\begin{align}
		\label{stoppingtimedefinition}
		\{ \ \tau \leq t \ \} = \{ \omega \in \Omega \ | \ \tau(\omega) \leq t \} \in \F_t
	\end{align}
holds for all $t \geq 0$. This is equivalent to the existence of $(\F_t)$-adapted random process $(X(t))$ that
	\begin{align}
		X(t) = \begin{cases} 0, \ &t \leq \tau, \\ 1, \ &t > \tau . \end{cases}
	\end{align}
If $\tau$ is time of a default, then the condition of the Equation \ref{stoppingtimedefinition} means that at the time $t$ the information in the filtration will tell if the default has occured or not, that is $\tau \leq t$ or not.

Stopping times are used to localized behavior. For example, a local martingale is a $(\F_t)$-adapted process
if there is such a sequence $(\tau_n)$ of stopping times that
	\begin{align}
		\Pf ( \tau_n < \tau_{n+1} ) &= 1,
		\Pf ( \lim\limits_{n \rightarrow \infty} \tau_n = \infty ) &= 1,
	\end{align}
and the stopped process defined by
	\begin{align}
		X_{\tau_n}(t) = X( \min(t, \tau_n) )
	\end{align}
is a $(\F_t)$-martingale for all $n \geq 1$.

\section{Brownian motion}

For reference, see \textcite[pp. 530--545]{billingsley2012probabilityandmeasure}

In order to keep notation efficient, we denote in this section
	\begin{align}
		\E_t( \cdot ) = \E_t( \ \cdot \ | \ \F_t ) .
	\end{align}
We also write $W(t, \omega) = W(t)$.

Let $(\F_t)$ be a filtration of the probability space. A Brownian motion (or a Wiener process) $W(t), t \geq 0$ with respect to filtration $(\F_t)$ is a stochastic process satisfying the following
  \begin{enumerate}
    \item $W(0) = 0$ almost surely,
    \item $W(t)$ is $\F_t$-measurable for each $ t\geq 0$,
    \item $t \mapsto W(t)$ is $\Pf$-surely continuous,
    \item for any finite set of times $0 \leq t_1 < t_2 < \ldots < t_n \leq T$, the random variables
      \begin{align}
        W(t_2) - W(t_1), W(t_3) - W(t_2), \ldots , W(t_n) - W(t_{n-1})
      \end{align}
      are independent,
    \item $W(s) - W(t) \sim N(0, s-t)$ for all $0 < t < s$.
  \end{enumerate}
These imply that $W(t) = W(t) - W(0) \sim N(0,t)$ and
  \begin{align}
    \Var (W(s) - W(t) = \E \left( (W(s) - W(t)^2 \right) = s - t
  \end{align}
for all $0 < t < s$.

A Brownian motion $W(t)$ is indeed a martingale in respect to the natural filtration since $\E_t ( W(s) - W(0 ) = 0$ and
  \begin{align}
    \E_t ( W(s) ) = \E_t ( W(s) - W(t) + W(t) ) = \E_t ( W(s) - W(t ) + W(t) = W(t) .
  \end{align}
Similarly $W(s)^2 = (W(s) - W(t)^2 + 2W(s)W(t) - W(t)^2$ implies that
  \begin{align}
    \E_t ( W(s)^2 -s ) &= \E_t \left( (W(s) - W(t)^2 + 2W(s)W(t) - W(t)^2 \right) -s \\
      &= \Var_t (W(s) - W(t)) + 2 \E_t (W(s)) W(t) - W(t)^2 - s \\
      &= s-t + W(t)^2 - s \\ &= W(t)^2 -t
  \end{align}
meaning that $W(t)^2 - t$ is also a martingale.

If $f,g : [0,T] \rightarrow \R$ are functions, then the covariation of $f$ and $g$ up to time $T$ is
  \begin{align}
    \langle f,g \rangle (T) = \lim_{ \left| \Pi \right| \rightarrow 0} \sum_{i=0}^{n-1} (f( t_{i+1} ) - f (t_i) )(g( t_{i+1} ) - g (t_i) ),
  \end{align}
where $\Pi = \{ t_0, t_1, \ldots , t_n \}$, $0 = t_0 < t_1 < \ldots < t_n = T$ is a partition with mesh $\left| \Pi \right| = \max_i ( t_{i+1} - t_{i} ) $. The quadratic variation of a function $f$ up to time $T$ is
  \begin{align}
    \langle f \rangle_T = \langle f,f \rangle (T) = \lim_{ \left| \Pi \right| \rightarrow 0} \sum_{i=0}^{n-1} (f( t_{i+1} ) - f (t_i) )^2.
  \end{align}

If the function $f$ has continuous derivate, then we may use intermediate value theorem to conclude that
  \begin{align}
    \sum_{i=0}^{n-1} (f( t_{i+1} ) - f (t_i) )^2 &= \sum_{i=0}^{n-1} (f'(s_i))^2 (t_{i+1} - t_i)^2 \\
      & \leq \left| \Pi \right| \sum_{i=0}^{n-1} (f'(s_i))^2 (t_{i+1} - t_i),
  \end{align}
for some $t_i \leq s_i \leq t_{i+1}$ and where
  \begin{align}
     \sum_{i=0}^{n-1} (f'(s_i))^2 (t_{i+1} - t_i) \rightarrow \int_0^T (f'(t))^2 \dx t < \infty
  \end{align}
as $\left| \Pi \right| \rightarrow 0$. Here we also used the continuity of $f'$ to keep the integral finite. This implies that $\langle f \rangle_T = 0$ for a smooth function $f$.

For random processes the quadratic variation is defined when the limit in probability exists for any sequence of partitions.

\begin{thm}
If $W= (W(t))$ is a Brownian motion, then $\langle W \rangle_T = T$ for all $T \geq 0$.
\end{thm}

\begin{proof}
First $\E \left( (W(t_{i+1}) - W(t_i) )^2 \right) = t_{i+1} - t_i$. We recall the fact that for a random variable $ X \sim N(0, \sigma^2)$ we have $\Var (X^2) = 2 \sigma^4$. Thus
  \begin{align}
    \Var \left( (W(t_{i+1}) - W(t_i) )^2 \right) =  2 (t_{i+1} - t_i)^2.
  \end{align}
These and the independence of increments implies that
  \begin{align}
    \E \left( \sum_{i=0}^{n-1} (W(t_{i+1}) - W(t_i) )^2 \right) = T
  \end{align}
and
  \begin{align}
    \Var \left( \sum_{i=0}^{n-1} (W(t_{i+1}) - W(t_i) )^2 \right) = 2 \sum_{i=0}^{n-1} (t_{i+1} - t_i)^2 \leq 2 \left| \Pi \right| T .
  \end{align}
This means that $\langle W \rangle_T$ $\Le^2$-converges to $T$.
\end{proof}

The Brownian motion accumulates one unit of quadratic variation per unit of time. The previous result is often written informally as $\dx W(t) \dx W(t) = dt$. Similarly we may calculate the covariation $\langle W, t \rangle_T = 0$. It is enough to note that
  \begin{align}
    \left| \sum_{i=0}^{n-1} (W(t_{i+1}) - W(t_i) )( t_{i+1} - t_i ) \right| \leq T \max_i \left| W(t_{i+1}) - W(t_i) \right|
  \end{align}
and by continuity of the paths we may force $\max_i \left| W(t_{i+1}) - W(t_i) \right|$ converge to zero. This we will use informally as $\dx W(t) \dx t = 0$. Since $f(t) = t$ is a smooth function, we have that $\langle t, t \rangle (T) = 0$ and thus $\dx t \dx t = 0$. Hence
  \begin{align}
    \dx W(t) \ \dx W(t) &= dt, \\
    \dx W(t) \ \dx t &= 0, \\
    \dx t \ \dx t &= 0.
  \end{align}

\section{It\^{o}-integral}

For reference, see \textcite[pp. 21--55]{oksendal2003stochastic}.

We would like to calculate the integral of a stochastic function $h$ with respect to a Brownian motion $W$ over the time interval $[0,T]$. Let $(\F_t)$ be the filtration induced by the Brownian motion $W$. Since $h$ and $W$ are random and $W$ is $\Pf$-nowhere differentiable, we need to tread carefully. If a $(\F_t)$ adapted function $h(\omega,t)$ is constant on each subinterval $[t_i, t_{i+1}]$ given a partition $0 = t_0 < t_1 < \ldots < t_n = T$ of $[0,T]$, then $h$ is called simple. For a simple function we may write
  \begin{align}
    I(h) (\omega) = \sum_{i=0}^{n-1} h(\omega,t_i) ( W(t_{i+1}, \omega) - W(t_i, \omega) ) .
  \end{align}
The key here is that the function $h(\omega,t_i)$ is at the earliest moment and therefore It\^{o}-integral is not forward looking. Now $I(h)$ itself is a random variable. If $h$ is structured enough, then we may approximate it with simple function and define It\^{o}-integral as the limit of this process, if it is exists. Without going into details, we note that if $(\F_t)$-adapted process satisfies\footnote{Unless otherwise noted, we shall always assume that this condition will be satisfies.} that
  	\begin{align}
	\label{H-ito-restriction}
	\E \left( \int_0^T h^2 (\omega,u) \dx u \right) < \infty
	\end{align}
then the limit
	\begin{align}
		I(t, \omega) = \int_0^t  h (\omega,u) \ \dx W(u, \omega)
	\end{align}
exists and is called the It\^{o}-integral of $h$ from $0$ to $t$.

Under these common assumptions, the It\^{o}-integral $I(t)$ satifies the following
	\begin{enumerate}
		\item $I(t)$ is $(\F_t)$-adapted,
		\item $I(t)$ is continuous,
		\item $I(t)$ is a martingale and $\E( I(t) \ | \ \F_0 ) = 0$ for all $t \geq 0$ and
		\item $I(t)$ satisfies the It\^{o}-isometry
		\begin{align}
		\E \left( I(h)^2 \right) = \E \left( \int_0^T h^2 (\omega,u) \dx u \right) < \infty .
		\end{align}
	\end{enumerate}

The  quadratic variation of It\^{o}-integral up to time $t$ is
  \begin{align}
    \int_0^t h^2 \dx s .
  \end{align}

Nothing restricts It\^{o}-integral to be one dimensional. If $W(t)$ is $d$-dimensional Wiener process and $h(t, \omega) : \R \times \Omega \rightarrow \R^{p \times d}$, then we demand that
	\begin{align}
		\E \left( \int_0^t \left| h(s, \omega) \right|^2 \ \dx s \right) < \infty
	\end{align}
where
	\begin{align}
		\left| h(s, \omega) \right|^2 = \mathrm{tr} ( h(s, \omega)^{\top} h(s, \omega) ) .
	\end{align}
If we relax the condition above, then we may not guarantee that the It\^{o}-integral will be martingale even if the integral exists.

\section{It\^{o} processes and It\^{o}'s lemma}
\label{sec:itoprocess}

For reference, see \textcite[pp. 21--55]{oksendal2003stochastic}.

An adapted process $X$ is called an It\^{o}-process if it can be written as
  \begin{align}
    X(t) = X(0) + \int_0^t \mu(s) \dx s + \int_0^t \sigma(s) \dx W(s) ,
  \end{align}
where $\mu(t, \omega), \sigma(t, \omega)$ are adapted and integrable processes. This is usually written in more informal differential notation as
  \begin{align}
    \dx X(t) = \mu(t) \dx t + \sigma(t) \dx W(t) .
  \end{align}
The function $\mu(t)$ is called as the drift and the function $\sigma(t)$ is called as the volatility. The quadratic variation of the It\^{o} process is
  \begin{align}
    \langle X \rangle_t = \int_0^t \sigma^2(u) \dx u .
  \end{align}
This is consistent with the heurestic calculation
  \begin{align}
    \dx X(t) \ \dx X(t) &= ( \mu(t) \dx t )^2 + 2 \mu(t) \sigma(t) \dx t \dx W(t) + ( \sigma(t) \dx W(t )^2 \\
      &= \sigma^2 (t) \dx t .
  \end{align}

\begin{thm}[It\^{o}'s lemma]
If a function $g : \R \times [0,T] \rightarrow \R$ is in the class $C^{2,1}$ and $Y(t) = g(t, X(t))$, where $X(t)$ is an It\^{o}-process, then $Y(t)$ is also an It\^{o}-process with the presentation
  \begin{align}
    \dx Y(t) = \left( \frac{\partial g}{\partial t} + \frac{\partial g}{\partial x} \mu(t) + \frac{1}{2} \frac{\partial^2 g}{\partial x^2} \sigma^2(t) \right) \dx t + \frac{\partial g}{\partial t} \sigma(t) \dx W(t) .
  \end{align}
\end{thm}

The formula itself is a short-hand for
  \begin{align}
    Y_t = Y_0 + \int_0^t g_t (u) \dx u + \int_0^t g_x (u) \dx X_u + \frac{1}{2} \int_0^t g_{xx} (u) \dx \langle X \rangle_u ,
  \end{align}
where, for example, $g_x$ is the partial differential of $g$ with respect to first variable.

If It\^{o}-process
	\begin{align}
		\dx X(t) = \mu(t) \dx t + \sigma(t) \dx W(t) .
	\end{align}
has $\mu: \R \times \Omega \rightarrow \R^d$, $\mu: \R \times \Omega \rightarrow \R^{p \times d}$ and $W(t)$ is $d$-dimensional Wiener process, then the equation in It\^{o}'s lemma can be written as
	\begin{align}
		\dx Y = g_t \dx t + g_x \mu \dx t + g_x \sigma \dx W + \frac{1}{2} \sum_{i=1}^p \sum_{j=1}^p g_{x_i, x_j} (\sigma^{\top} \sigma)_{i,j} \dx t ,
	\end{align}
where $g_x = \left( g_{x_1} g_{x_2} \cdots g_{x_p}  \right)$.

It\^{o}'s lemma can be used to calculate integrals. If $g(x,t) = x^2$ and $X_t = W_t$, then
  \begin{align}
    \dx g(W_t,t) = \dx t + 2 W(t) \dx W(t),
  \end{align}
which is the short-hand for
  \begin{align}
    W(t)^2 = t + 2 \int_0^t W(s) \dx W(s) .
  \end{align}
This means that
  \begin{align}
    \int_0^t W(s) \dx W(s) = \frac{1}{2} \left( W(t)^2 - t \right).
  \end{align}

\section{Geometric Brownian motion}

A stochastic process $X$ is a geometric Brownian motion if it satisfies the stochastic differential equation
  \begin{align}
    \dx X(t) = \mu X(t) \dx t + \sigma X(t) \dx W(t),
  \end{align}
where $\mu$ and $\sigma > 0$ are constants and $W$ is a Brownian motion. The constant $\mu$ is the drift and $\sigma$ is the diffusion (or volatility). The solution of this SDE can be calculated by using It\^{o}'s lemma with the function $g(x, t) = \log(x)$. Now $\frac{\partial g}{\partial x} = 1/x$ and $\frac{\partial^2 g}{\partial x^2} = -1/x^2$. Thus
   \begin{align}
    \dx \log X(t) &= \left( 0+ \mu X(t) / X(t) - \frac{1}{2} \sigma^2 X^2 (t) / X^2 (t) \right) \dx t + \sigma X(t) / X(t) \dx W(t) \\
      &= \left( \mu  - \frac{1}{2} \sigma^2 \right) \dx t + \sigma \dx W(t)
  \end{align}
and integrating from $0$ to $t$ gives us
  \begin{align}
    \log (X(t) / X(0) ) = \left( \mu  - \frac{1}{2} \sigma^2 \right) t + \sigma W(t) .
  \end{align}
Thus
  \begin{align}
    X(t) = X(0) \e^{ (\mu  - \frac{1}{2} \sigma^2 ) t + \sigma W(t) } .
  \end{align}
This means that for given $X(0)$, the variable $X(t)$ is log-normally distributed with parameters $( \mu  - \frac{1}{2} \sigma^2 ) t$ and $\sqrt{t}\sigma$. Now the mean of $X(t)$ is
  \begin{align}
    \exp \left( ( \mu  - \frac{1}{2} \sigma^2 ) t + \frac{1}{2} ( \sqrt{t} \sigma )^2 \right) = \exp \left( \mu t \right)
  \end{align}
and the variance is
  \begin{align}
    ( \exp ( \sigma^2 t ) - 1 ) \exp ( )
  \end{align}

\section{Girsanov's theorem}
\label{sec:girsanov}

For reference, see \textcite[pp. 161--171]{oksendal2003stochastic}.

As a converse of Radon-Nikod\'{y}m-theorem, if $L$ almost surely positive and $\E_{\Pf} (L) = 1$, then we may define a new measure
	\begin{align}
    \label{radonnikodymgirsanovconnection}
		\Pm(A) = \E_{\Pf} (L \1_A)
	\end{align}
for all $A \in \F$ and now
	\begin{align}
		L = \frac{\dx \Pm}{\dx \Pf}
	\end{align}
is the Radon-Nikod\'{y}m-derivate with respect to $\Pf$ and $\Pm$.

\begin{thm}[Girsanov's theorem]
	Let $W(t)$ be a Brownian motion in the probability space $(\Omega, \F, \Pf)$ with respect to filtration $(\F_t)$ and assume that the process $\kappa(t)$ is an $(\F_t)$-adapted process. We define
	\begin{align}
	\label{doleansexponential}
	L(t) &= \exp \left( \int_0^t \kappa(s) \dx W(s) - \frac{1}{2} \int_0^t \kappa^2 (s) \dx s \right) \\
	&= \exp \left( \int_0^t \kappa(s) \dx W(s - \frac{1}{2} \langle \kappa(s) \rangle_t \right),
	\end{align}
	If we assume that $\E_{\Pf} (L_T) = 1$, then the process
	\begin{align}
	W^*(t) &= W(t) - \int_0^t \kappa(s) \dx s.
	\end{align}
	is a Brownian motion under the equivalent probability measure $\Pm$ defined by the Equation \ref{radonnikodymgirsanovconnection}.
\end{thm}

The process $\kappa$ is called as the Girsanov kernel of the probability transformation. If we assume that the Girsanov kernel satisfies the Novikov Condition
  \begin{align}
    \E_{\Pf} \exp \left( \frac{1}{2} \int_0^T \kappa^2 (s) \dx s \right) < \infty ,
  \end{align}
then the condition $\E_{\Pf} (L(T)) = 1$ is satisfied.

The process $L(t)$ defined in the equation \ref{doleansexponential} is the solution to the stochastic differential equation
  \begin{align}
    \dx L(t) = \kappa(t) L(t) \dx W(t)
  \end{align}
with the condition $L(0) = 1$.

The significant consequence of the theorem is the fact that the drift of stochastic process is very malleable. If
  \begin{align}
    \dx X(t) = \mu(t) \dx t + \sigma(t) \dx W(t),
  \end{align}
where $\sigma(t) > 0$, then we choose $\kappa(t) = - (\mu(t) - a(t)) / \sigma(t)$. If the Girsanov kernel satisfies assumptions of the Girsanov's theorem, then we have a equivalent measure $\Pm$ under which
  \begin{align}
    W^*(t) = W(t) - \int_0^t \kappa(s) \dx s
  \end{align}
is a Brownian motion. Thus $\dx W(t) = \dx W^*(t) + \kappa(t) \dx t$ and
  \begin{align}
    \dx X(t) = \mu(t) \dx t + \sigma(t) (\dx W^*(t) + \kappa(t) \dx t) = a(t) \dx t + \sigma(t) \dx W^* (t)
  \end{align}
under the measure $\Pm$. We see that the measure change leaves diffusion unchanged, but the measure may be changed almost at the will. The Brownian motions $W(t)$ and $W^*(t)$ are not the same, but a priori their statistical properties are the same. Especially if $a(t) \equiv 0$, then the process $X(t)$ is driftless under the measure $\Pm$. Thus
  \begin{align}
    \E_{\Pm} (X(T) | \F_t) = X(t)
  \end{align}
and it is a martingale.

\section{Martingale representation theorem}

Suppose that the filtration $(\F_t)$ is generated by a Brownian motion $W(t)$. One of the properties of the It\^{o}-integral was that
	\begin{align}
		I(t) = \int_0^t h(s) \dx W(s)
	\end{align}
was a martingale, when $h(t)$ satisfies Equation \ref{H-ito-restriction}. Martingale representation states that if $M$ is a square integrable martingale, that is
	\begin{align}
		\E_{\Pf} (M^2) < \infty ,
	\end{align}
then
	\begin{align}
		M(t) = \int_0^t h(s) \dx W(s),
	\end{align}
where $h(t)$ is $(\F_t)$-adapted and it satisfies Equation \ref{H-ito-restriction}. Thus
	\begin{align}
		\dx M(t) =  h(t) \dx W(t),
	\end{align}

\section{Feynman-Kac theorem}
\label{sec:faynmankac}

For reference, see \textcite[pp. 145--147]{oksendal2003stochastic}.

The Feynman-Kac theorem states that the solution of the partial differential equation
	\begin{align}
		\frac{\partial V(t,x)}{\partial t} + \frac{\partial V(t,x)}{\partial x} f(x) + \frac{1}{2} \frac{\partial^2 V(t,x)}{\partial x^2} \sigma^2 (x) = r V(t,x)
	\end{align}
with the terminal boundary condition $V(T,x) = g(x)$ is
	\begin{align}
		V(t,x) = \e^{-r(T-t)} \E_{\Pm} \left( g(X_T) \ | \ X(t) = x \right) ,
	\end{align}
where the process $X(t) = x$ satisfies
	\begin{align}
		\dx X(s) = f(X(s)) \dx s + \sigma( X(s) ) \dx W(s)
	\end{align}
under the probability measure $\Pm$, where $W(s)$ is a Brownian motion under the measure $\Pm$.

\section{Partial information}

We assume that the $\sigma$-algebra $(\F_t)$ presents partial market information without default and
\begin{align}
\Hf_t = \sigma( \1_{ \{\default \leq s \} } | s \leq t ) = \sigma( H(s) | s \leq t )
\end{align}
is the knowledge of the default up to time $t$. By
\begin{align}
\F_t \vee \Hf_t
\end{align}
we denote the smallest $\sigma$-algebra containing $\F_t$ and $\Hf_t$. Also
\begin{align}
\F_{\infty} = \bigvee_t \F_t
\end{align}
the smallest $\sigma$-algebra containing all algebras $\F_t$.

\begin{lemma}
	\label{lemma-weaksigmasameness}
	For every $A \in \F_t \vee \Hf_t$, there exists such $B \in \F_t$ that
	\begin{align}
	\label{eq-weaksigmasameness}
	A \cap \{ \default > t \} = B \cap \{ \default > t \},
	\end{align}
	where $t \in \R_+$.
\end{lemma}

\begin{proof}
	We consider the filtration given by
	\begin{align}
	\G_t = \{ A \in \F_t \vee \Hf_t \ | \ A \cap \{ \default > t \} = B \cap \{ \default > t \} \textup{ for some } B \in \F_t \}
	\end{align}
	and it is sufficient to show that $\F_t \vee \Hf_t \subseteq \G_t$. It is clear that $\F_t \subseteq \G_t$. If $A \in \Hf_t$, then $A \cap \{ \default > t \}$ is either $\emptyset$ or $\{ \default > t \}$ and it follows that $\Hf_t \subseteq \G_t$.

	Trivially $\Omega \in \G_t$ and it is also easy to see that $\G_t$ is closed under countable unions. If $A \in \G_t$ and $B \in \F_t$ satisfies the equation (\ref{eq-weaksigmasameness}), then
	\begin{align}
	\kom{A} \cup \{ \default \leq t \} = \kom{B} \cup \{ \default \leq t \}
	\end{align}
	and
	\begin{align}
	\kom{A} \cap \{ \default > t \} &= ( \kom{A} \cup \{ \default \leq t \} ) \cap \{ \default > t \} \\
	&= ( \kom{B} \cup \{ \default \leq t \} ) \cap \{ \default > t \} \\
	&= \kom{B} \cap \{ \default > t \}
	\end{align}
	which implies that $\kom{A} \in \G_t$. Hence, $\G_t$ is a $\sigma$-algebra and $\F_t \vee \Hf_t \subseteq \G_t$.
\end{proof}

This can be used to show the following important result.

\begin{lemma}
\label{lemma_takingthedefaultinformationout}
If $X$ is non-negative integrable random variable, then
	\begin{align}
	\E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \vee \Hf_t \right) = \frac{\1_{ \{ \default > t \} }}{\Pm (\default > t | \F_t )} \E_{\Pm}  \left( \1_{ \{ \default > t \} } X | \F_t \right) .
	\end{align}
\end{lemma}

\begin{proof}
	Let $A \in \F_t \vee \Hf_t$. By Lemma \ref{lemma-weaksigmasameness}, there is $B \in \F_t$ such that
	\begin{align}
	\1_A \1_{ \{ \default > t \} } = \1_B \1_{ \{ \default > t \} } .
	\end{align}
	By this and the definition of conditional expectation we have
	\begin{align}
	\int_A \1_{ \{ \default > t \} } X \Pm( \default > t | \F_t ) \ \dx \Pm & = \int_B \1_{ \{ \default > t \} } X \Pm( \default > t | \F_t ) \ \dx \Pm \\
	&= \int_B \E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \right) \Pm( \default > t | \F_t ) \ \dx \Pm \\
	&= \int_B \1_{ \{ \default > t \} } \E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \right) \ \dx \Pm \\
	&= \int_A \1_{ \{ \default > t \} } \E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \right) \ \dx \Pm .
	\end{align}
	Since $A \in \F_t \vee \Hf_t$ is arbitrary, we have that
	\begin{align}
	\E_{\Pm} \left( \1_{ \{ \default > t \} } X \Pm( \default > t | \F_t ) | \F_t \vee \Hf_t \right)
	&= \1_{ \{ \default > t \} } \E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \right),
	\end{align}
	where we have used facts $\1_{ \{ \default > t \} } \in \Hf_t$ and $\E_{\Pm} \left( \1_{ \{ \default > t \} } X | \F_t \right) \in \F_t$. As $\Pm( \default > t | \F_t )$ is $\F_t \vee \Hf_t$-measurable, it can be taken out of the expectations. Since it is non-zero, we have the claim.
\end{proof}

\begin{thm}
\label{eq_takingthedefaultinformationout}
Let $T > t$. If $X$ is non-negative integrable $\F_T$-measurable random variable, then
	\begin{align}
	\E_{\Pm} \left( \1_{ \{ \default > T \} } X | \F_t \vee \Hf_t \right) = \frac{\1_{ \{ \default > t \} }}{\Pm (\default > t | \F_t )} \E_{\Pm}  \left( \1_{ \{ \default > T \} } X | \F_t \right) .
	\end{align}
\end{thm}

\begin{proof}
We will consider variable $Y = \1_{ \{T> \default \} } X$. If $T > t$, then
	\begin{align}
		\1_{ \{T> \default \} } X = Y = \1_{ \{t> \default \} } Y,
	\end{align}
and by the assumptions we may use Lemma \ref{lemma_takingthedefaultinformationout}. Hence
	\begin{align}
		\E_{\Pm} \left( \1_{ \{ \default > T \} } X | \F_t \vee \Hf_t \right)
		&= \E_{\Pm} \left( \1_{ \{t> \default \} } Y  | \F_t \vee \Hf_t \right) \\
		&= \frac{\1_{ \{ \default > t \} }}{\Pm (\default > t | \F_t )} \E_{\Pm}  \left( \1_{ \{ \default > t \} } Y | \F_t \right) \\
		&= \frac{\1_{ \{ \default > t \} }}{\Pm (\default > t | \F_t )} \E_{\Pm}  \left( \1_{ \{ \default > T \} } X | \F_t \right)
	\end{align}
as required.
\end{proof}

\section{Doubly stochastic default time}
\label{sec:doublystochastic}

The introduce the following technical assumptions:
\begin{DS}
	\item\label{DS1} There exists a non-negative $\F_t$-progressive process $\lambda (t)$ such that
	\begin{align}
	\Pm (\default > t | \F_t ) = \e^{ - \int_0^t \lambda(s) \dx s }
	\end{align}
	\item\label{DS2} For all $t \geq 0$, it holds that
	\begin{align}
	\Pm (\default > t | \F_t ) = \Pm (\default > t | \F_{\infty} ),
	\end{align}
	where
	\begin{align}
	\F_{\infty} = \bigvee_{t \geq 0} F_t .
	\end{align}
\end{DS}
The stopping times satisfying both \ref{DS1} and \ref{DS2} are doubly stochastic stopping times.

The condition \ref{DS1} means that with only partial market information $\F_t$, the exact default time is never known as
\begin{align}
0 < \Pm (\default > t | \F_t ) < 1
\end{align}
for all $t \geq 0$. Hence $\F_t \not = \F_t \vee \G_t$ and $\default$ is not a stopping time under the filtration $(\F_t)$.

Now the Theorem \ref{eq_takingthedefaultinformationout} directly implies the following.

\begin{thm}
	\label{eq_takingthedefaultinformationoutdoubly}
	If we assume \ref{DS1} and $T > t$, then
	\begin{align}
	\E_{\Pm} \left( \1_{ \{ \default > T \} } X | \F_t \vee \Hf_t \right) = \1_{ \{ \default > t \} } \e^{ \int_0^t \lambda(s) \dx s }  \left( \1_{ \{ \default > T \} } X | \F_t \right)
	\end{align}
holds for every  non-negative integrable $\F_T$-measurable random variable $X$.
\end{thm}

The previous Theorem is important, because we usually assume that the short rate $r(t)$ is adapted to partial information $(\F_t)$. In pricing instruments that are sensitive to credit risk, we have to start with full market information but the Theorem shows how we may switch back to partial information set.

Since
\begin{align}
\E_{\Pm} \left( \1_{ \{ \default > t \} } (1 - \1_{ \{ \default > T \} } ) | \F_t  \right) &= \E_{\Pm} \left( \1_{ \{ \default > t \} } - \1_{ \{ \default > T \} } | \F_t  \right) \\
&= \E_{\Pm} \left( \E_{\Pm} \left( \1_{ \{ \default > t \} } - \1_{ \{ \default > T \} } | \F_T \right) | \F_t  \right) \\
&= \E_{\Pm} \left( \1_{ \{ \default > t \} } - \Pm (\default > T | \F_T ) | \F_t  \right)
\end{align}
we know now that if \ref{DS1} holds, then
\begin{align}
\Pm ( t <\default \leq T | \F_t \vee \Hf_t ) &= \E_{\Pm} \left( \1_{ \{ \default > t \} } - \1_{ \{ \default > T \} } | \F_t \vee \Hf_t \right) \\
&= \E_{\Pm} \left( \1_{ \{ \default > t \} } (1 - \1_{ \{ \default > T \} } ) | \F_t \vee \Hf_t \right) \\
&= \frac{\1_{ \{ \default > t \} }}{\Pm (\default > t | \F_t )} \E_{\Pm} \left( \1_{ \{ \default > t \} } (1 - \1_{ \{ \default > T \} } ) | \F_t  \right) \\
&= \1_{ \{ \default > t \} } \E_{\Pm} \left( 1 - \e^{ - \int_t^T \lambda (s) \dx s } | \F_t  \right)
\end{align}
Thus we may approximate that
\begin{align}
\Pm ( t <\default \leq t+ \dx t | \F_t \vee \Hf_t ) \approx \1_{ \{ \default > t \} } \lambda (t) \dx t .
\end{align}

The condition \ref{DS2} is equivalent to the condition that every $(\F_t)$-martingale is also a $(\F_t \vee \Hf_t)$-martingale. See, for example, \cite{filipovic2009term}.

\section{Characteristic functions and Fourier inversion method}

This following method is based on work of \textcite{heston1993closed} and the presentation is based on \textcite[pp. 222--233]{nawalkabeliaevasoto2007dynamic}.

Given a probability density function $f(x)$ under a measure $\Pm$, the characteristic function associated with the density function is
  \begin{align}
    g(t) = \E_{\Pm} \left( \exp^{\boldsymbol{i}tx} \right) = \int_{-\infty}^{\infty} \exp^{\boldsymbol{i}tx} f(x) \dx x .
  \end{align}
It can be shown that there is a one-to-one correspondence between probability density function and characteristic functions and we can calculate
  \begin{align}
    \int_k^{\infty} f(x) \dx x = \frac{1}{2} + \frac{1}{\pi} \int_0^{\infty} \Re \left( \frac{\exp^{-\boldsymbol{i}sk} g(s)}{\boldsymbol{i} s} \right) \dx s
  \end{align}

In order to calculate certain derivative prices, we have to evaluate expectations in the form of
  \begin{align}
    \Pi_{1t} &= \E_{\Pm} \left( \frac{\exp^{-\int_t^S r(u) \dx u}Z(S,T) \1_{ \{ Z(S,T) \geq K \} } }{Z(t,T)} \right) \\
     Z(t,T) &= \E_{\Pm} \left( \exp^{-\int_t^S r(u) \dx u} \right)
  \end{align}
a
  \begin{align}
    \Pi_{1t} &= \int_k^{\infty} f_{1t} (y) \dx y \\
    &= \frac{1}{2} + \frac{1}{\pi} \int_0^{\infty} \Re \left( \frac{\exp^{-\boldsymbol{i}sk} g_{1t}(s)}{\boldsymbol{i} s} \right) \dx s
  \end{align}

\section{A Gaussian calculation}

For a continuous function $g$, we may approximate $\int_0^t g(s) \dx s$ by the sequence
  \begin{align}
    \frac{t}{n} \sum_{k=1}^n g \left( \frac{kt}{n} \right)
  \end{align}

\begin{thm}
	\label{gaussiancalculation}
If $X$ is continuous and Gaussian stochastic process, then the stochastic process
  \begin{align}
    Y = \int_0^t X_s \dx s
  \end{align}
also has Gaussian distribution with
  \begin{align}
    \E Y &= \int_0^t \E X_s \dx s, \\
    \Var Y &= \int_0^t \int_0^t \Cov (X_s, X_u) \dx s \dx u .
  \end{align}
\end{thm}

\begin{proof}
By using the idea above, we may approximate the distribution of $Y$ by the distributions of
  \begin{align}
    Y_n = \frac{t}{n} \sum_{k=1}^n X \left( \frac{kt}{n} \right),
  \end{align}
which are gaussian as they are linear combinations of gaussian variables. Since the expectation is a linear operation, we have
  \begin{align}
    \E Y_n = \frac{t}{n} \sum_{k=1}^n \E X \left( \frac{kt}{n} \right) \rightarrow \int_0^t \E X_s \dx s
  \end{align}
and
  \begin{align}
    \Var Y_n = \frac{t}{n} \sum_{k=1}^n \sum_{j=1}^n \Cov \left( X \left( \frac{kt}{n} \right), X \left( \frac{jt}{n} \right) \right) \rightarrow \int_0^t \int_0^t \Cov (X_s,X_u) \dx s \dx u .
  \end{align}
\end{proof}

\section{Differential evolution}

Differential evolution (DE) is a class of optimization methods based on evolutionary algorithms. It was introduced by \cite{storn1996usage} and \cite{storn1997differential}. DE requires no prior knowledge about the optimization problem. On the other hand, this means that after the algorithm has ran its course, we have no guarantees that the result is actually an optimal. It can be used to canvas large areas of the solution space. Therefore it can be useful to find the initial guess for other optimization algorithms that are sensitive to the precision of the initial value.

Suppose that $f: D \rightarrow \R$ is the fitness function that is to be minimized. Here $D$ is a hypercube in real space $\R^n$.

The basic algorithm starts be selection of the initial population $I_0 \subset D$. Let $I_k$ be the the population of $k$:th generation. The for every $x \in I_k$ we generate a distinct alternative candidate $z$ has some heritage with $x$. If $f(z) < f(x)$, then we replace $x$ with $z$ in $(k+1)$:th generation. Therefore the fitness of the next generation as at least as good as the previous. The canon way of choosing the candidate is as follows:

\begin{enumerate}
	\item For all $x \in I_k$ pick three distinct points $a, b, c$ from $I_k \setminus \{ x \}$.
	\item Let $y = a + F(b-c)$ and randomly pick an index $j = 1,2, \ldots, n$. We generate an evolutionary agent $z = (z_1, z_2, \ldots, z_n)$ by having $z_j = y_j$. For $z_l$, where $j \not = l \in \{1,2, \ldots, n\}$ we pick $y_l$ with the probability of $C$ and otherwise $x_l$. Thus $z \not = x$. If $z \not \in D$ then a new candidate is picked or it is scaled back to the search space.
\end{enumerate}

The number $F$ is called the differential weight and usually $F \in [ 0,2]$. The probability $C$ is the crossover probability. The choice of these parameters obviously influence the convergence. For example, small differential weights and crossover probabilities causes the population to converge quickly.

Since the basic setup of algorithm is very flexible, there are many variants. In the following, we outline the algorithm used in the data analysis of this thesis.

The initial population is chosen with uniform distribution. The size of the population is 1024 for models without default and 512 otherwise. For every 10 (or 5 for models with default) generations, a random check is made to see if there is a culling . The probability of this grows with each generations. The size of the culling is inversely proportional to the amount of replaces during the previous cycle. Only the members with worst fitness are removed. However, the population will always has at least $32$ members.

For the candidate vectors we use two different strategies. Either we pick five distinct $a,b,c,d,e$ and
	\begin{align}
		z = a + F(b-c) + F(d-e)
	\end{align}
or we pick four distinct candidates $p, a, b, c$ and
	\begin{align}
	z = a + F(p-a) + F(b-c)
	\end{align}
where $p$ is in the top $5 \%$ of the candidates. This first behaviour increases the chances of exploration while the latter approach will boost convergence. The algorithm is set up so that earlier exploration is preferred while later on the convergence is favored. Differential weight and the crossover probability are choosen randomly for each candidate. The algorithm is more likely to choose values that promote convergence later on the run.


%!TEX root = ../notes.tex
\section{March 3, 2022}
\subsection{Quadratic Residues \emph{continued}}

\recall \cref{defn:legendre-symbol} and \cref{prop:5.1.2} from last class (right above).

We now prove the earlier proposition:
\begin{proof}[Proof of \cref{prop:5.1.2}]
    ~\begin{enumerate}
        \item[(c)] is clear.
        \item[(a)]
            By Fermat's Little Theorem, if $p\nmid a$, we have $a^{p-1}\equiv 1\mod p$, so
            \[(a^{(p-1)/2}+1)(a^{(p-1)/2}-1)\equiv 0\mod p\]
            Since mod $p$ we have an integral domain, then we have that $a^{(p-1)/2}\equiv \pm 1\mod p$. We know that $a^{(p-1)/2}\equiv 1\mod p$ if and only if $a$ is a quadratic residue mod $p$,
        \item[(b)]
            This applies (a).
            \[\lege{ab}{p} = (ab)^{(p-1)/2} = a^{(p-1)/2}\cdot b^{(p-1)/2} = \lege{a}{p}\lege{b}{p}\]
    \end{enumerate}
\end{proof}
\begin{corollary}
    We have some corollaries:
    \begin{enumerate}[1)]
        \item There are exactly $\frac{p-1}{2}$ quadratic residues and $\frac{p-1}{2}$ quadratic non-residues mod $p$.
        \item The product of two residues is a residue, the product of a residue and a non-residue is a non-residue, and the product of a non-residue and a non-residue is a residue.
        \item If $g$ is a primitive root modulo $p$, then
              \[\lege{g^i}{p} = (-1)^i.\]
        \item We have
              \[\lege{-1}{p} = (-1)^{(p-1)/2}\]
              which has a fancy name. This is the \emph{``First Supplemental Law of Quadratic Reciprocity''}.
    \end{enumerate}
\end{corollary}

\subsection{Gauss's Lemma}
We now discuss a characterization of the Legendre symbol due to Gauss.
\begin{definition}
    For $p\in\ZZ_+$ an odd prime,
    \[S = \left\{-\frac{p-1}{2}, -\frac{p-3}{2}, \dots, -1, 1, 2, \dots, \frac{p-1}{2}\right\}\]
    is called the \ul{set of least residues mod $p$}.
\end{definition}

\begin{definition}
    Let $a\in\ZZ$ such that $p\nmid a$. Define $\mu$ to be the number of negative least residues of the integers
    \[a, 2a, 3a, \dots, \left(\frac{p-1}{2}\right)a\]
\end{definition}

\begin{example}
    If $p=7$ and $a=4$, then $\frac{p-1}{2} = 3$, and
    $1\cdot 4, 2\cdot 4, 3\cdot 4$ are congruent to $-3, 1, -2$ mod $7$. Thus $\mu = 2$.
\end{example}
\begin{lemma}[Gauss's Lemma]\label{lemma:gauss-lemma}
    Let $p\in\ZZ_+$ be an odd prime and let $a\in\ZZ$ be such that $p\nmid a$. Then
    \[\lege{a}{p} = (-1)^\mu.\]
\end{lemma}
\begin{proof}
    It is convenient for us to partition the list $S$ as
    \begin{align*}
        P & = \{1, 2, \cdots, \frac{p-1}{2}\}    \\
        N & = \{-1, -2, \cdots, -\frac{p-1}{2}\}
    \end{align*}
    so that $\mu = |aP\cap N|$\footnote{There's an abuse of notation here, but we conflate integers with their equivalence classes in $S$.}.

    A key observation is that if $x, y\in P$ with $x\neq y$, then
    \[ax\not\equiv \pm ay\mod p\]
    for otherwise,
    \[a \equiv \pm y\mod p\]
    which is impossible since $x$ and $y$ are distinct elements of $P$ (positive residue classes, so not in $N$).

    Thus $aP = \{\varepsilon_i i\mid 1\leq u\leq \frac{p-1}{2}\}$ for some $\varepsilon_i = \pm 1$.

    Now we mimic the elementary proof of Euler's Theorem:
    \begin{align*}
        a^{(p-1)/2}\cdot \left(\frac{p-1}{2}\right)! & \equiv \left(\prod_{i=1}^{(p-1)/2} \varepsilon_i\right)\cdot \left(\frac{p-1}{2}\right)! \\
        a^{(p-1)/2}                                  & \equiv \left(\prod_{i=1}^{(p-1)/2} \varepsilon_i\right)                                  \\
                                                     & \equiv (-1)^\mu
    \end{align*}
    since $\mu$ is equal to the order of the set that is contributing every $-1$ as $\varepsilon_i$.

    Applying \cref{prop:5.1.2} (Euler's criterion), this concludes the proof.
\end{proof}
We'll use this lemma to prove Quadratic Reciprocity later.

We now use it to prove the \emph{Second Supplemental Law} of Quadratic Reciprocity.
\begin{proposition}[5.1.3, the \emph{Second Supplemental Law of Quadratic Reciprocity}]
    For $p\in\ZZ_+$ be an odd prime.
    \[\lege{2}{p} = (-1)^{(p^2 - 1)/8}\]
    We note $\dfrac{p^2-1}{8} = \dfrac{(p-1)(p+1)}{2\cdot 4}$. From this, it follows that we're really saying
    \[\lege{2}{p} = \begin{cases}
            1  & \text{when $p = \pm 1\mod 8$} \\
            -1 & \text{when $p = \pm 3\mod 8$}
        \end{cases}\]
\end{proposition}
\begin{proof}
    We apply Gauss's Lemma with $a = 2$.
    \[2P = \{2, 4, 6, \dots, p-1\}\]
    First suppose that $p\equiv 1\mod 4$. Then $\frac{p-1}{2}$ is even, so
    \[2P = \bigg\{\underbrace{2, 4, 6, \dots, \frac{p-1}{2}}_{\in P}, \underbrace{\frac{p+3}{2}, \dots, p-1}_{\in N}\bigg\}\]
    with the first $\frac{p-1}{4}$ elements in $P$ and the last $\frac{p-1}{4}$ elements in $N$. So $\mu = |2P\cap N| = \frac{p-1}{4}$, so Gauss's Lemma gives
    \[\lege{2}{p}
        = (-1)^{\frac{p-1}{4}}
        = \left((-1)^{\frac{p-1}{4}}\right)^{\frac{p+1}{2}}
        = (-1)^{\frac{p^2-1}{8}}\]
    since $\frac{p+1}{2}$ is odd.

    Now suppose $p\equiv 3\mod 4$. Then
    \[2P = \bigg\{\underbrace{2, 4, 6, \dots, \frac{p-3}{2}}_{\in P}, \underbrace{\frac{p+1}{2}, \dots, p-1}_{\in N}\bigg\}\]
    The first $\frac{p-3}{4}$ elements are in $P$, and the last $\frac{p+1}{4}$ elements are in $N$. Then $\mu = \frac{p+1}{4}$, so
    \[\lege{2}{p}
        = (-1)^{\frac{p+1}{4}}
        = \left((-1)^{\frac{p+1}{4}}\right)^{\frac{p-1}{2}}
        = (-1)^{\frac{p^2-1}{8}}\]
\end{proof}

\subsection{Quadratic Reciprocity}
\begin{theorem}[Law of Quadratic Reciprocity]\label{thm:qr}
    Let $p, q\in\ZZ_+$ be distinct odd positive primes. Then
    \begin{equation*}
        \lege{p}{q}\lege{q}{p} = (-1)^{\frac{p-1}{2}\frac{q-1}{2}}
    \end{equation*}
    In other words,
    \[\lege{p}{q} = \lege{q}{p}\]
    if and only if at least one of $p, q$ is congruent to $1$ mod $4$.
\end{theorem}
\begin{proof}
    \emph{coming soon!}
\end{proof}

Here's the motivation behind this: we used Euler's Criterion to easily calculate the quadratic character of some $a$ mod $p$. With Quadratic Reciprocity, we can solve the question ``If we fix odd prime $q$, for which $p$ is $q$ a quadratic residue?''

\begin{example}
    Which odd primes $p\in\ZZ_+$ have $3$ as a quadratic residue?

    Suppose $p\equiv 1\mod 4$. Then
    \[\lege{3}{p} = \lege{p}{3} = \begin{cases}
            1  & \text{if $p\equiv 1\mod 3$}  \\
            -1 & \text{if $p\equiv -1\mod 3$}
        \end{cases}\]
    Then $p\equiv 1\mod 4$ and $p\equiv 1\mod 3$ so $p\equiv 1\mod 12$ gives us \[\lege{3}{p} = 1\] and $p\equiv 5\mod 12$ gives \[\lege{3}{p} = -1.\]

    Now suppose $p\equiv 3\mod 4$. Then
    \[\lege{3}{p} = -\lege{p}{3} = \begin{cases}
            1  & \text{if $p\equiv -1\mod 3$} \\
            -1 & \text{if $p\equiv 1\mod 3$}
        \end{cases}\]
    Thus,
    \begin{align*}
        p & \equiv 11\mod 12\text{ gives }\lege{3}{p} = 1\text{, and } \\
        p & \equiv 7\mod 12\text{ gives }\lege{3}{p} = -1
    \end{align*}

    So we conclude that $\lege{3}{p} = 1$ iff $p\equiv \pm 1\mod 12$.
\end{example}


\lab{Application}{Cracking Blackjack}{Cracking Blackjack}
\label{Ch:BJ}

\objective{Exploit the weaknesses of a pseudorandom number generator that is based on a Linear Congruential Generator.}

\section*{Blackjack}

\begin{figure}[H]
\includegraphics[scale = .9]{Blackjack_game_1.jpg}
\caption{Initial Round of a BlackJack Game}
\end{figure}

Black Jack is a card game that involves the use of randomness.
The game is simple.
The dealer deals the player and himself each two cards.
He flips over his first card so that the player can see it.
The player has to choose to take another card ("hit") or not ("stand").
If the player hits he gets another card and again has the choice to hit or stand.

The goal is to get your hand to be at or as close to 21 without going over.
Face cards are worth 10 points.
Aces can count either as 11 or 1.
The value of all other cards are equal to number on the card.

Once the player has decided to stand the dealer flips over his second card and deals himself cards until his hand value is 17 or greater.

If the player value goes above 21 he automatically loses.
If his value is 21 and below and dealer has above 21 then the player wins.
If they both have 21 or under than the player with the hand of highest value wins.
If both hands have the same value, the game is a tie.

\section*{Shuffling Algorithms}

One use of Pseudorandom Number Generators (PRNGs) is to shuffle cards.
The main goal of these algorithms is that the card order be random--so that no single player has an advantage based on order.
Often, as strange as it may seem, online gambling sites will post their shuffling algorithms online.
The only things they do not post are their seed values.
Often the time in milliseconds from midnight is used as the seed value.

John von Neumann said ``Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."
As seen in the last lab, weak PRNGs are periodic and are predictable once a few outputs are known.
This lab will have you break blackjack based on a weak PRNG.

\section*{Cracking Blackjack}
For these next problems you will need three files that are provided with this lab: Black.py, BlackEasy.py, and bjHelp.py.
Black.py and BlackEasy.py are are programs that run games of Blackjack that use a Linear Congruentail Generator (LCG) to shuffle the cards.
They generate 52 random numbers and then the argsort of those numbers is the order of the cards.
The parameters for BlackEasy.py are a$=2521$, c$=13$, mod$=2^{16}$.
For Black.py they are a$=25214903917$, c$=11$, mod$=2^{48}$.
In order to play them type \li{python <<filename>> <<numberofgames>>} in your command line.
They are both seeded initially by the time.

bjHelp.py contains two functions that will help you "predict" the cards:
SuffleHack(n,a,c,mod,seed) gives the first $n$ card shuffles given the parameters for a LCG.
The shuffles are represented by numbers.
Hacker(Stats,['card','card','card']) Stats is the output of SuffleHack and takes a list of 3 cards (see below).
It prints all shuffles as a list of cards in Stats that have the same first three cards as the inputted list.

The trick to being able to "predict" the cards is to find the initial seed value.

Cards- A, 2-10, J, Q, or K combined with heart, diamond, club, or spade in single quotes.
Examples: '6diamond', 'Kclub'.

\begin{warn}
Both BlackEasy.py and Black.py use functions that are incompatible with ipython. They need to run \li{python Black.py} in command line.
\end{warn}

\begin{problem}
Play 10 games of BlackEasy.py and by the 5th game be able to predict the cards.
You can write your own functions or use the ones in bjHelp.py.
You will want to open two command prompts, one to play the game and one to predict the cards.
\end{problem}

Not too hard.
That is because there is only $2^{16}$ seed values.
This next one you will have to look at more hands until you can find out the initial seed value.

\begin{problem}
Play 20 games of Black.py and by the 15th game be able to predict the cards.
\end{problem}


\paragraph{Performance Measure} \label{performance_measure}
The presented parameters for a sliding window simulation are resulting in a huge amount of different configured
simulations. Every configured simulation tries to detect gestures in the test data stream for every experimentee. It can
be argued how to compare the performance of those simulations. Have misrecognized gestures in the assessment more weight
than correctly recognized or the other way around? It is no easy decision to weigh the mistakes of a simulation against
the success of a simulation.

All containing data points of a supposed detected gesture are labeled by a simulation. Those labels can be compared
to the original labels that were made by the experimentee. This results in true positive ($tp$), true negative
($tn$), false positive ($fp$) and false negative ($fn$) labels for every gesture $GesA, GesB, \dots, GesH$. Seen from
this point of view, a simulation is a multi-class classificator for the gesture classes
$K_{GesA}, K_{GesB}, \dots, K_{GesH}$. Common performance measures for multi-class classificator are $Precision_{\mu}$,
$Recall_{\mu}$ and $F_{\beta}score_{\mu}$ as mentioned in \cite{sokolova2009systematic}.

\begin{equation}
    Precision_{\mu} = \frac{\sum \limits_{i=1}^{l} tp_i}{\sum \limits_{i=1}^{l} (tp_i + fp_i)}
\end{equation}
\begin{equation}
    Recall_{\mu} = \frac{\sum \limits_{i=1}^{l} tp_i}{\sum \limits_{i=1}^{l} (tp_i + fn_i)}
\end{equation}
\begin{equation}
    F_{\beta}score_{\mu} = \frac{(\beta^2 + 1)Precision_{\mu} Recall_{\mu}}{\beta^2 Precision_{\mu} + Recall_{\mu}}
\end{equation}

The $F_{1}score_{\mu}$ is used in the following subsection to rank the different simulations.