\title{Variational Inference}

\subsection{Variational Inference}

Variational inference is an umbrella term for algorithms which cast
posterior inference as optimization
\citep{hinton1993keeping,waterhouse1996bayesian,jordan1999introduction}.

The core idea involves two steps:
\begin{enumerate}
   \item posit a family of distributions $q(\mathbf{z}\;;\;\lambda)$
   over the latent variables;
   \item match $q(\mathbf{z}\;;\;\lambda)$ to the posterior by
   optimizing over its parameters $\lambda$.
 \end{enumerate}
This strategy converts the problem of computing the posterior
$p(\mathbf{z} \mid \mathbf{x})$ into an optimization problem:
minimize a divergence measure
\begin{align*}
  \lambda^*
  &=
  \arg\min_\lambda \text{divergence}(
  p(\mathbf{z} \mid \mathbf{x})
  ,
  q(\mathbf{z}\;;\;\lambda)
  ).
\end{align*}
The optimized distribution $q(\mathbf{z}\;;\;\lambda^*)$ is used as
a proxy to the posterior $p(\mathbf{z}\mid \mathbf{x})$.

Edward takes the perspective that the posterior is (typically)
intractable, and thus we must build a model of latent variables that
best approximates the posterior.
It is analogous to the perspective
that the true data generating process is unknown, and thus we build
models of data to best approximate the true process.

For details on variational inference classes defined in Edward,
see the \href{/api/inference}{inference API}.
For background on specific variational inference algorithms in
Edward, see the other inference \href{/tutorials/}{tutorials}.

\subsubsection{References}\label{references}


\subsection{part a}
$$\vec X_0 = \begin{bmatrix}
	-1\\1
\end{bmatrix}$$
Tolerance is: $10^{-7}$

Answer is:
$$\vec X_{ans} = \begin{bmatrix}
	-1.7556\\
	0.3655
\end{bmatrix}$$
\subsubsection{figures}
\newpage
\begin{itemize}
	\item Steepest Descent
	\begin{itemize}
		\item Quadratic Interpolation
		\begin{figure}[H]
			\caption{Steepest Descent and Quadratic Interpolation}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q1/part a Steepest Descent + Quadratic Interpolation.png}
		\end{figure}
		\item Golden Section
		\begin{figure}[H]
			\caption{Steepest Descent and Golden Section}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q1/part a Steepest Descent + Golden Section.png}
		\end{figure}
	\end{itemize}
	\item BFGS
	\begin{itemize}
		\item Quadratic Interpolation
		\begin{figure}[H]
			\caption{BFGS and Quadratic Interpolation}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q1/part a BFGS + Quadratic Interpolation.png}
		\end{figure}
		\item Golden Section
			\begin{figure}[H]
				\caption{BFGS and Golden Section}
				\centering
				\includegraphics[width=11.5cm]{../Figure/Q1/part a BFGS + Golden Section.png}
		\end{figure}
	\end{itemize}
\end{itemize}
\subsubsection{result}
\begin{itemize}
	\item Time
\begin{table}[h]
	\caption {Time compare between four methods}
	\begin{center}
		\begin{tabular}{ |l|l|l|l| }
			\hline
			\multicolumn{2}{|c|}{Steepest Descent} &
			\multicolumn{2}{|c|}{BFGS} \Tstrut\\
			\hline
			Quadratic Interpolation & Golden Section & Quadratic Interpolation &
			Golden Section \Tstrut\\
			\hline
			$0.238\sec$ & $0.183\sec$ & $0.164\sec$ & $0.102\sec$\Tstrut\\
			\hline
		\end{tabular}
	\end{center}
\end{table}
	\item Number of Cost calculation
\begin{table}[h]
	\caption {Number of Cost calculation compare between four methods}
	\begin{center}
		\begin{tabular}{ |l|l|l|l| }
			\hline
			\multicolumn{2}{|c|}{Steepest Descent} &
			\multicolumn{2}{|c|}{BFGS} \Tstrut\\
			\hline
			Quadratic Interpolation & Golden Section & Quadratic Interpolation &
			Golden Section \Tstrut\\
			\hline
			$360$ & $336$ & $242$ & $213$\Tstrut\\
			\hline
		\end{tabular}
	\end{center}
\end{table}
	\item Number of Gradient calculation
\begin{table}[h]
	\caption {Number of Gradient calculation compare between four methods}
	\begin{center}
		\begin{tabular}{ |l|l|l|l| }
			\hline
			\multicolumn{2}{|c|}{Steepest Descent} &
			\multicolumn{2}{|c|}{BFGS} \Tstrut\\
			\hline
			Quadratic Interpolation & Golden Section & Quadratic Interpolation &
			Golden Section \Tstrut\\
			\hline
			$19$ & $13$ & $13$ & $9$\Tstrut\\
			\hline
		\end{tabular}
	\end{center}
\end{table}
\end{itemize}


\section{Radial orbital density plots}
The radial orbital density, ${\psi}^2(r)$, plots are created by integrating over angular portion of the norm of the single particle wave function.

\begin{equation}
    {\psi}^2(r) = \int_0^{2\pi}\int_0^{\pi} \psi^2(r,\theta,\phi) r^2 \sin(\theta) dr d\theta d\phi
\end{equation}

Discretizing this expression using a uniform radial grid and a Lebedev-Laikov quadrature for the angular components, yields a form that can be readily evaluated.

\begin{equation}\label{eq:plotted}
    {\psi}^2(r_i) = 4 \pi {r_i^2}  \sum_j^{N^{ang}} w^{ang}_j \psi^2(r_i,\theta_j,\phi_j)
\end{equation}

The function ${\psi}^2(r_i)$ from Equation~\ref{eq:plotted} can be plotted with the points $r_i$ serving as the abscissa.
Since the singly occupied orbitals are normalized, the proximity of the sum of the radial quadrature to unity is used as a check.

\begin{equation}\label{eq:norm}
    \sum_i^{N^{rad}} {\psi}^2(r_i) w^{rad}_i = \sum_i^{N^{rad}} {\psi}^2(r_i) {\Delta}r \approx  1
\end{equation}

\subsection{Required software versions}
\begin{table}[H]
\begin{tabular}{ll}
Required software      & version     \\
\texttt{numpy}  & 1.18.4      \\
\texttt{quadpy} & 0.16.2      \\
\texttt{pyscf}  & 1.7.0       \\
\texttt{cclib}  & 1.6.3       \\
\end{tabular}
\end{table}

\subsection{Step 1: Generating a Molden file}
Molden files were generated using \texttt{cclib}, with the exception of the natural orbital from the CIPSI calculations.
Since QuantumPackage is not supported by \texttt{cclib}, Molden files were created using the native utility in QuantumPackage 2.0.
For the Molden files generated with cclib, the \texttt{-g/--ghost} flag indicates the presence of a ghost atom.
By default the only molecular orbitals can be written to a Molden file, therefore the \texttt{-n/--naturalorbtials} flag was created to allow natural orbitals to be written in place of molecular orbitals.
This flag is not yet available in the official distribution, but a request to incorporate it in the official distribution has been opened (\url{https://github.com/cclib/cclib/pull/948}).

%\lstinputlisting[label=cclibmoldenbasic,caption=Using \texttt{cclib} to write a Molden file from an output file. ,language={bash}]{parts/cclibmolden.sh}
\inputminted{zsh}{parts/cclibmolden.sh}

\subsection{Step 2: Integrating over the angular components of the singly occupied orbital}

\texttt{quadpy} was used to generate the Lebedev-Laikov integration weights and points.
The singly occupied molecular/natural orbital was evaluated at these points using \texttt{PySCF}.

%\lstinputlisting[label=anionradialint,caption=Using \texttt{Quadpy} and \texttt{PySCF} to integrate the singly occupied orbital. ,language={Python}]{parts/anionradialint.py}
\inputminted{python}{parts/anionradialint.py}


\subsection{Power series}\label{subsec:power_series}

\begin{definition}\label{def:convergent_power_series}
  Let \( \BbbK\Bracks{X} \) be the space of formal power series defined in \fullref{def:formal_power_series}.

  To each formal power series
  \begin{equation*}
    \sum_{k=0}^\infty a_k X^k
  \end{equation*}
  there corresponds a function, called a \term{power series}
  \begin{equation}\label{def:convergent_power_series/series}
    p(x) \coloneqq \sum_{k=0}^\infty a_k x^k.
  \end{equation}

  We sometimes slightly generalize this notion slightly by using a \enquote{shift} by \( \alpha \in \BbbK \): define the function
  \begin{equation}\label{def:convergent_power_series/shifted_series}
    p(x) \coloneqq \sum_{k=0}^\infty a_k (x - \alpha)^k.
  \end{equation}

  If the limit exists (as a \hyperref[def:convergent_series]{numeric series}) for a certain \( x \in \BbbK \), we say that the series \term{converges} at \( x \).

  The series is no longer \enquote{formal} because it is now a proper function instead of an abstract algebraic object, although a power series may only be defined in a subset of \( \BbbK \) (that is, a \hyperref[def:partial_function]{partial function}).
\end{definition}

\begin{theorem}\label{thm:power_series_radius_of_convergence}
  For every power series \eqref{def:convergent_power_series/series}, there exists a nonnegative extended real number \( r \in [0, +\infty] \), called its \term{radius of convergence}, such that \eqref{def:convergent_power_series/series} converges absolutely if \( \abs{x} < r \) and diverges if \( \abs{x} > r \).

  The behavior of the series is more complicated when \( \abs{x} = r \) (unless \( r = 0 \), in which case the power series converges if and only if \( x = 0 \)).
\end{theorem}
\begin{proof}
  Define
  \begin{equation*}
    q \coloneqq \limsup_{n \to \infty} \sqrt[n]{\abs{a_n}},
  \end{equation*}
  where we put \( q = +\infty \) if the limit does not exist. We have
  \begin{equation*}
    \limsup_{n \to \infty} \sqrt[n]{\abs{x^n a_n}} = \abs{x} q.
  \end{equation*}

  By \fullref{thm:cauchys_root_test}, \eqref{def:convergent_power_series/series} converges absolutely if \( \abs{z} q < 1 \) and diverges if \( \abs{z} q > 1 \).

  Thus, \( r \coloneqq \tfrac 1 q \) is the desired radius of convergence.

  Note that we may also use \fullref{thm:dalamberts_ratio_test} for finding the same radius of convergence by \fullref{rem:nonnegative_series_convergence_test_equivalence}.
\end{proof}

\begin{proposition}\label{thm:power_series_parity}
  Power series of the form
  \begin{equation}\label{thm:power_series_parity/odd}
    f_o(z) \coloneqq \sum_{m \text{ is odd}} a_m z^m = \sum_{k=0}^\infty a_{2k+1} z^{2k+1}
  \end{equation}
  are \hyperref[def:group/function_parity]{odd functions} and power series of the form
  \begin{equation}\label{thm:power_series_parity/even}
    f_e(z) \coloneqq \sum_{m \text{ is even}} a_m z^m = \sum_{k=0}^\infty a_{2k} z^{2k}
  \end{equation}
  are even functions.
\end{proposition}
\begin{proof}
  If \eqref{thm:power_series_parity/odd} converges for \( z \in \BbbC \),
  \begin{equation*}
    f_o(-z)
    =
    \sum_{k=0}^\infty a_{2k+1} (-z)^{2k+1}
    =
    \sum_{k=0}^\infty a_{2k+1} (-1)^{2k+1} z^{2k+1}
    =
    - \sum_{k=0}^\infty a_{2k+1} z^{2k+1}
    =
    - f_o(z).
  \end{equation*}

  Analogously, since \( (-1)^{2k} = 1 \), we have \( f_e(-z) = f_e(z) \).
\end{proof}

\begin{proposition}\label{thm:power_series_are_locally_uniform_convergent}
  A power series is \hyperref[def:function_net_convergence/locally_uniform]{locally uniformly convergent} in the interior of its domain of convergence.
\end{proposition}
\begin{proof}
  Assume that the series \eqref{def:convergent_power_series/series} converges inside the ball \( B(0, R) \). Fix \( x \in B(0, R) \) and \( R_x < R - \abs{x} \). Then the geometric series
  \begin{equation*}
    \sum_{k=0}^\infty a_k R_x^k
  \end{equation*}
  converges and dominates \eqref{def:convergent_power_series/series} in the ball \( B(x, R_x) \). Thus, by \fullref{thm:weierstrass_series_criterion}, \eqref{def:convergent_power_series/series} converges uniformly in \( B(x, R_x) \).

  Since the choice of \( x \in B(0, R) \) was arbitrary, we conclude that \eqref{def:convergent_power_series/series} is locally uniformly convergent.
\end{proof}

\begin{theorem}\label{thm:series_termwise_operations}
  Suppose that the power series \eqref{def:convergent_power_series/series} has a (potentially infinite) radius of convergence \( R \).

  \begin{thmenum}
    \thmitem{thm:series_termwise_operations/differentiation} \( p(x) \) is differentiable in \( B(0, R) \) and can be differentiated termwise as
    \begin{equation}\label{thm:series_termwise_operations/derivative}
      p'(x) = \sum_{k=0}^\infty a_{k+1} (k+1) x^k.
    \end{equation}

    Furthermore, \( p'(x) \) has the same radius of convergence as \( p(x) \).

    \thmitem{thm:series_termwise_operations/integration} If the series is real and \( \abs{x} < R \), \( p(x) \) is integrable in \( [0, x] \) (or \( [x, 0] \)) and can be integrated termwise as
    \begin{equation}\label{thm:series_termwise_operations/primitive}
      \int_0^x p(t) dt = \sum_{k=0}^\infty a_k \frac {x^{k+1}} {k+1}.
    \end{equation}
  \end{thmenum}
\end{theorem}
\begin{proof}
  \SubProofOf{thm:series_termwise_operations/differentiation} Note that the right-hand side of \fullref{thm:series_termwise_operations/derivative} is a power series. Furthermore, its radius of convergence is, by \fullref{thm:power_series_radius_of_convergence},
  \begin{equation*}
    \lim_{k \to \infty} \abs{\frac {a_{k+1} (k+1) x^k} {a_{k+2} (k+2) x^{k+1}}}
    =
    \abs{x} \lim_{k \to \infty} \frac {k+1} {k+2} \abs{\frac {a_{k+1}} {a_{k+2}}}
    =
    R.
  \end{equation*}

  Fix \( x \in B(0, R) \) and choose \( r \in (\abs{x}, R) \). Both series are uniformly convergent in \( B(0, r) \). By \fullref{thm:derivative_limit_exchange/sequence}, the equality \fullref{thm:series_termwise_operations/derivative} holds in \( B(0, r) \), hence it also holds for \( x \).

  \SubProofOf{thm:series_termwise_operations/integration} Analogously to \fullref{thm:series_termwise_operations/differentiation}, we conclude that the right-hand side of \fullref{thm:series_termwise_operations/primitive} is a power series with radius of convergence \( R \).

  The rest follows directly from \fullref{thm:riemann_intergral_limit_exchange}.
\end{proof}


\documentclass{article}
%\usepackage{authblk}
\usepackage{amssymb, amsmath}
\usepackage{hyperref}
\usepackage{verbatim}

%\author[1]{Todor Milev}
\date{April 2018}
%\affil[1]{FA Enterprise System}
\title{Elliptic curve secp256k1 \\ Implementation notes}
\author{Todor Milev\footnote{FA Enterprise System}\\ todor@fa.biz}
\newcommand{\secpTwoFiveSixKone}{{\bf secp256k1}}
\renewcommand{\mod}{{~\bf mod~}}
\begin{document}
\maketitle
\section{Introdution}
Public/private key cryptography is arguably the most important aspect of modern crypto-currency systems. The somewhat slow execution of private/public key cryptography algorithms appears to be one of the main bottlenecks of FAB's Kanban system.

Following Bitcoin, FAB coin uses the standard public/private key cryptography ECDSA over \secpTwoFiveSixKone. Here, ECDSA stands for Elliptic Curve Digital Signature Algorithm and \secpTwoFiveSixKone{} stands for the elliptic curve:

\[
y^2 = x^3 + 7
\]
(we specify the base point later), over the finite field:

\[
\mathbb Z / p\mathbb Z,
\]
where
\begin{equation}\label{eqThePrime}
p= 2^{256} - 2^{32} - 977.
\end{equation}

In this document, we discuss and document technical details of FAB's implementation of ECDSA over  \secpTwoFiveSixKone{}. Our openCL implementation is based on the project \cite{secp256k1:openCLimplementationHanh0}, which is in turn based on the C project libsecp256k1 \cite{Wuille:secp256k1}.

\section{Operations in $\mathbb Z / p\mathbb Z$}
Recall from (\ref{eqThePrime}) that $p$ is the prime given by
\[
p= 2^{256} - 2^{32} - 977.
\]
In this section, we describe our implementation of $Z / p\mathbb Z$.
\subsection{Representations of numbers}
A number in $x$ in $\mathbb Z / p\mathbb Z$ is represented by a large integer $X$, in turn represented by a sequence of $10$ small integers $x_0,\dots, x_9$ for which $0 \leq x_i < 2^{32}$ and such that
\[
X = \sum_{i=0}^{9} x_i \left(2^{26}\right)^i.
\]
The representations of $x$ is not unique but becomes so when we request that
\[
0\leq x_i < 2^{26}
\]
and
\[
0 \leq X < p = 2^{256} - 2^{32} - 977.
\]
We say that the unique representation $x_0, \dots, x_9$ of $x$ above is its \emph{normal form} (and $x$ is \emph{normalized}). Two elements of $ \mathbb Z / p \mathbb Z$ are equal if and only if their normal forms are equal. We will not assume that a number $x$ is represented by its normal form as some of the operations described below do not require that.

In what follows, we shall use the notation
\[
a'=a \mod q
\]
to denote remainder $0\leq a' <q$  of $a$ when dividing $a$ by $q$. For the rest of this section, we also set
\[
d= 2^{26}.
\]
In this way the normal form of a large number $X$ is its $d$-base representation (here $d$ is the ``digit'' of the base).

\subsection{Computing the normal form of $x$}\label{secNormalFormOfFieldElement}

\subsection{Multiplying two elements}
Let $a$ be an element represented by $A = \sum a_i d^i$ with $a_0, \dots, a_8<2^{30}$ and $a_9<2^{22}$. Likewise, let $b$ be represented by $B=\sum b_j d^j$ with analogous inequalities on the coefficients $b_j$. In this section, we show how to compute the normal form of $a\cdot b$. This operation is implemented in the function \verb|ECMultiplyFieldElementsInner|.

Compute as follows.
\[
\begin{array}{rcl}
\displaystyle A\cdot B &=&\displaystyle \left(\sum_{i=0}^9 a_i d^i \right) \left(\sum_{j=0}^9 b_j d^j\right) \\
&=&\displaystyle  \sum_{k=0}^{18} \left(\underbrace{ \sum_{i=0}^k a_i b_{k-i} }_{=\bar t_k} \right) d^k \\
&=&\displaystyle \sum_{k=0}^{18} \bar t^k d^k,
\end{array}
\]
where we have set $\displaystyle \bar t_k = \left(\sum_{i=0}^k a_i b_{k-i} \right)$. Since $0\leq a_i,b_i < d$, the $\bar t_k$'s are smaller than $2^{64}$ and can be computed by standard arithmetic using the \verb|uint64_t| integer type of the GPU.

Let
\[
A\cdot B = \sum_{k=0}^{19} t_k d^k
\]
with $0\leq t_k < d$ be the unique representation of $A\cdot B$ base $d$. Then the $t_k$ can be computed from the $\bar t_k$'s consecutively via
\[
\begin{array}{rcl}
\displaystyle t_0 &= &\displaystyle \bar t_0 \mod d\\
\displaystyle c_0 &= &\displaystyle \left\lfloor \frac{\bar t_0}{d} \right\rfloor\\ \\\hline \\
\displaystyle t_1 &= &\displaystyle c_0 + \bar t_0 \mod d\\
\displaystyle c_1 &= &\displaystyle \left\lfloor \frac{c_0 + \bar t_0}{d} \right\rfloor \\
\displaystyle t_2 &= &\displaystyle c_1 + \bar t_1 \mod d\\
\displaystyle c_2 &= &\displaystyle \left\lfloor \frac{c_1 + \bar t_1}{d} \right\rfloor \\
&\vdots&\\
\displaystyle t_{18} &= &\displaystyle c_{17} + \bar t_{17} \mod d\\
\displaystyle c_{18} &= &\displaystyle \left\lfloor \frac{c_{17} + \bar t_{17}}{d} \right\rfloor \\ \\ \hline
\displaystyle t_{19} &= &\displaystyle  c_{18}.
\end{array}
\]
In the above, all computations except the first and the last are similar, as indicated by the horizontal line, and $\lfloor\bullet \rfloor$ stands as usual for the floor function. Both $\mod d$ and division by $d$ are carried out by bit-shift operations and are therefore fast.

Let $S = (A\cdot B \mod p)$; computing the normal form $S = \sum s_i d^i$, $0\leq s_i < d$, $S < p$ is the final aim of the present discussion. Since $2^{256} \mod p = 2^{32}+977$, it follows that
\begin{equation} \label{eqReducet0plust10dPower10}
\begin{array}{rcll}
t_0+ t_{10} d^{10} \mod p
&=&\displaystyle t_0+t_{10} 2^{26\cdot 10} &\mod p\\
&=&\displaystyle t_0 + t_{10} \cdot 2^4\cdot 2^{256}  &\mod p\\
&=&\displaystyle t_0 + t_{10} 2^4 \left(2^{32} + 977 \right)&\mod p\\
&=&\displaystyle \left(t_0 +t_{10}2^4\cdot 977\right) + t_{10} 2^{10}\cdot d&\mod p \\
\displaystyle \text{set }g_{0} = t_0 +t_{10}2^4\cdot 977 \mod d \\
\displaystyle \text{set }f_{0} =\left\lfloor \frac{t_0 +t_{10}2^4\cdot 977}{d}\right\rfloor \\
&=& g_0 + \left(\underbrace{ f_0+t_{10} 2^{10}}_{=h_0}\right) d &\mod p.
\end{array}
\end{equation}
Set $\displaystyle h_0 = f_0+t_{10} 2^{10}$ as indicated above. The computation above shows how ``reduce'' the $d$-digit $t_{10}$ by modifying the two least significant $d$-digits. Accounting for the ``carry-over'' digit $h_0$, we can continue with this process for the next pair of digits $ t_1 d+t_{11} d^{11}$, and so on. This is done below (similar steps have been omitted).
\[
\begin{array}{rcll}
h_0 d +t_1 d+ t_{11} d^{11} \mod p
&=&\displaystyle \left(h_0+t_1+t_{11} d^{10}\right) d &\mod p\\
&=& \dots \text{compute as in (\ref{eqReducet0plust10dPower10})} \dots\\
\displaystyle \text{set }g_{1} = h_0+t_1 +t_{11}2^4\cdot 977 \mod d \\
\displaystyle \text{set }f_{1} =\left\lfloor \frac{h_0+t_1 +t_{11}2^4\cdot 977}{d}\right\rfloor \\
&=& g_1d + \left(\underbrace{ f_1+t_{11} 2^{10}}_{=h_1}\right) d^2 &\mod p\\
&\vdots&\\
h_8 d^9 + t_{9} d^9 + t_{19} d^{19}\mod p &=& \dots\\
\displaystyle \text{set }g_{9} = h_8+t_9 +t_{19}2^4\cdot 977 \mod d \\
\displaystyle \text{set }f_{9} =\left\lfloor \frac{h_8+t_9 +t_{19}2^4\cdot 977}{d}\right\rfloor \\
&=&g_9 d^9 + \left( \underbrace{f_9+t_{19} 2^{10}}_{=h_9}\right)d^{10} &\mod p.
\end{array}
\]
In the computations above, $t_{19}$ is maximum $26$ bits long and so
\begin{equation}\label{eqh9inequality}
h_9 < f_9 + 2^{26}2^{10} < 2^{37}.
\end{equation}
In order to obtain the normal form of $S$, we need to modify the digits $g_9$ and $h_9$ as follows.
\[
\begin{array}{rcll}
\displaystyle g_9 d^9 + h_9 d^{10} \mod p &=&\displaystyle  (g_9+h_9 d )d^9 &\mod p \\
\displaystyle \text{set }r =g_9 + h_9 d \mod 2^{22}\\
\displaystyle \text{set }m = \left\lfloor \frac{g_9 + h_9 d }{2^{22}} \right\rfloor\\
&=& \left( r + m \cdot 2^{22} \right) d^9  &\mod p\\
&=& r d^9 + m 2^{256} &\mod p\\
&=& r d^9 + m\left(2^{32}+977\right) &\mod p.\\
\end{array}
\]
From inequality (\ref{eqh9inequality}), we get that $h_9 d < 2^{37} 2^{26} = 2^{63}$, and so the computations above fit in \verb|uint64_t| type. Again using (\ref{eqh9inequality}) we get the following estimates for the sizes of digits.
\[
\begin{array}{rcl}

\displaystyle g_9+ h_9 d &<& \displaystyle 2^{64}\\
\displaystyle m=\left\lfloor \frac{g_9 + h_9 d }{2^{22}} \right\rfloor&<& \displaystyle \frac{2^{64}}{2^{22}} = 2^{42}\\
m\left(2^{32}+977\right) &<& 2^{33}\cdot 2^{42} = 2^{75} < d^3.
\end{array}
\]
Thus the number $m\left(2^{32}+977\right)$ can be written in the form $z_0 + z_1 d + z_2 d^2 $ with $0\leq z_0, z_1, z_2 < d$. Collecting the information so far, we get
\begin{equation}\label{eqAtimesBalmostNormalForm}
A\cdot B \mod p = (z_0 + g_0) + (z_1 + g_1)d + (z_2+g_2)d^2 + \sum _{k=3}^{8} g_k d^k + r d^9,
\end{equation}
where $z_0+g_0 < 2d$, $z_1+g_1 <2d$, $z_2+g_2<2d$, $g_k< d$, $r < 2^{22}$. Except in the cases when $z_i+g_i\geq d$, this expression is normalized. To ensure our result is normalized, we need to reduce the representation above according to Section \ref{secNormalFormOfFieldElement}. This does not need to be done immediately as the form \eqref{eqAtimesBalmostNormalForm} satisfies all assumptions of the present discussion.

\bibliographystyle{plain}
\bibliography{../bibliography.bib}
\end{document}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\begin{document}
Jon Allen

September 11, 2013

Section 2.2 Problems 25,28,43,51 Section 2.3 Problems 13,19
\section*{2.2}
\subsection*{25}
Solve \(x \sin \left( x^2 \right) \mathrm{d}x=\frac{\cos \sqrt{y}}{\sqrt{y}}\mathrm{d}y\)

\(u=x^2, \mathrm{d}u=2x\,\mathrm{d}x, v=y^{1/2}, \mathrm{d}v=\frac{1}{2}y^{-1/2}\,\mathrm{d}y\)

\begin{align*}
\frac{1}{2}\int{\sin u\,\mathrm{d}u}&=2\int{\cos v\,\mathrm{d}v}\\
-\frac{1}{2}\cos{x^2}&=2\sin{\sqrt{y}}+C
\end{align*}
\subsection*{28}
Solve \(\frac{\mathrm{d}\phi}{\mathrm{d}\theta}=\frac{\left(5-2\cos\theta\right)^3 \sin\theta\cos^4\phi}{\sin\phi}\)

\( \frac{\sin\phi}{\cos^4\phi}\,\mathrm{d}\phi=\left(5-2\cos\theta\right)^3\sin\theta\,\mathrm{d}\theta,\quad u=\cos\phi,\quad \mathrm{d}u=-\sin\phi\,\mathrm{d}\phi,\quad v=5-2\cos\theta,\quad \mathrm{d}v=2\sin\theta\,\mathrm{d}\theta \)
\begin{align*}
-\int{u^{-4}\,\mathrm{d}u}&=\frac{1}{2}\int{v^3\,\mathrm{d}v}\\
\frac{1}{3}u^{-3}&=\frac{1}{8}v^4+C\\
\frac{1}{3}\cos^3\phi&=\frac{1}{8}\left(5-2\cos\theta\right)^4+C
\end{align*}
\subsection*{43}
Solve the IVP
\(\mathrm{d}x=\cos{y}\mathrm{d}y, x(0)=2\)
\begin{align*}
\int{\mathrm{d}x}&=\int{\cos{y}\mathrm{d}y}\\
x&=\sin{y} + C\\
2&=\sin{0}+C\\
x&=\sin{y}+2
\end{align*}
\subsection*{51}
Solve the IVP
\(\frac{\mathrm{d}y}{\mathrm{d}x}=\frac{1}{1+x^2},\; y(0)=1\)
\begin{align*}
\int{\mathrm{d}y}&=\int{\frac{1}{1+x^2}\,\mathrm{d}x}\\
y&=\arctan{x}+C\\
1&=\arctan{0}+C=1\\
y&=\arctan{x}+1
\end{align*}
\section*{2.3}
\subsection*{13}
Solve \(\frac{\mathrm{d}y}{\mathrm{d}t}+y\cot{t}=\cos{t}\)
\begin{align*}
\mu (t)&=e^{\int{\cot{t}\,\mathrm{d}t}}\\
\cot t&=\frac{\cos t}{\sin t},\;u=\sin t,\;\mathrm{d}u=\cos t\,\mathrm{d}t\\
\mu(t)&=e^{\int{\frac{1}{u}\,\mathrm{d}u}}\\
&=e^{\ln\left\lvert\sin t\right\rvert}=\sin t\\
\int{\frac{\mathrm{d}}{\mathrm{d}t}\left(y\sin t\right)\,\mathrm{d}t}&=\int{\sin{t}\cos{t}\,\mathrm{d}t},\;u=\sin t,\;\mathrm{d}u=\cos t\,\mathrm{d}t\\
y\sin t&=\frac{\sin^2 t}{2}+C\\
y&=\frac{1}{2}\sin t+C\csc t
\end{align*}
Note that \(-\frac{1}{2}\cos t \cot t+C_0\csc t=-\frac{1}{2}\frac{\cos^2 t}{\sin t}+C_0\csc t=-\frac{1}{2}\frac{1-\sin^2 t}{\sin t}+C_0\csc t=-\frac{1}{2}\left(\frac{1}{\sin t}-\sin t\right)+C_0\csc t=\frac{1}{2}\sin t-\frac{1}{2}\csc t+C_0\csc t=\frac{1}{2}\sin t+(C_0-\frac{1}{2})\csc t=\frac{1}{2}\sin t+C_1\csc t\) and so my answer is equivalent to the book's answer.
\subsection*{19}
Solve \(\frac{\mathrm{d}\theta}{\mathrm{d}r}-r\theta=r\)
\begin{align*}
\mu(r)&=e^{\int{-r\,\mathrm{d}r}}=e^{-\frac{r^2}{2}}\\
\int{\frac{\mathrm{d}}{\mathrm{d}t}\left(\theta e^{-\frac{r^2}{2}}\right)\,\mathrm{d}t}&=\int{r e^{-\frac{r^2}{2}}\,\mathrm{d}r}\\
\theta e^{-\frac{r^2}{2}}&=-e^{-\frac{r^2}{2}}+C\\
\theta&=-1+Ce^{\frac{r^2}{2}}
\end{align*}

\end{document}


\section{09/09}
We can apply the algorithm given previously to actually \emph{find} an
assignment of the boolean variables which will satisfy at least $\sfrac{7m}{8}$
clauses. Specifically, consider the following algorithm
\begin{algorithm}
    \caption{Randomized Max 3-SAT Algorithm}
    \begin{algorithmic}[1]
        \Function{Solve}{\texttt{formula}}
            \State $\texttt{result} \gets 0$
            \While{$\texttt{result} < \sfrac{7m}{8}$}
                \State $\texttt{bool-vec}, \texttt{result} \gets \Call{Try-Solve}{\texttt{formula}}$
            \EndWhile
            \State \Return \texttt{bool-vec}
        \EndFunction
        \Function{Try-Solve}{\texttt{formula}}
            \State $\texttt{bool-vec} \gets \Call{Sample}{\set{0,1}^n}$
            \State $k \gets$ number of satisfied clauses in \texttt{formula}
            \State \Return $(\texttt{bool-vec}, k)$
        \EndFunction
    \end{algorithmic}
\end{algorithm}
Clearly, the above algorithm will \emph{eventually} return the desired boolean
vector. However, we are concerned with how many iterations this algorithm will
require. To determine this, we must first determine the probability that the
randomly selected boolean vector will satisfy at least $\sfrac{7m}{8}$ clauses.

Let $X$ represent the number of satisfied clauses. Recall that $\expectation{X}
= \sfrac{7m}{8}$. Let us call this value $\mu$. Then
\begin{align*}\mu
    &= \expectation{X}\\
    &= \sum_{x=0}^m x\prob{X = x}\\
    &= \sum_{x < \mu} x\prob{X = x} + \sum_{x \geq \mu} x\prob{X = x}\\
    \shortintertext{Notice that, since $x < \mu$, $x \leq \mu - c$ for some constant $0 < c \leq 1$}
    &\leq \left(\mu - c\right)\sum_{x < \mu}\prob{X = x} + m\sum_{x \geq \mu} \prob{X = x}\\
    \shortintertext{setting $p = \prob{X \geq \mu}$}\\
    &= (\mu - c)(1 - p) + mp
    \shortintertext{Solving for $p$}\\
    p &\geq \frac{c}{c - \mu + m}\\
      &= \bigTh{\frac{1}{m}}
\end{align*}

Before we finish this analysis, we should consider the Geometric Random
Variable.

\subsection{Geometric Random Variable}
\begin{definition}{Geometric Random Variable}{geometricrv}
    Consider some Bernoulli trial, i.e., an event with exactly two possible
    outcomes, denotes ``success'' and ``failure'', and suppose success occurs
    with probability $p$. The \emph{Geometric Distribution}, denoted $X \sim
    G(p)$, is the number of trials necessary until a success occurs.
\end{definition}

\begin{lemma}{}{}
    Let $X$ be some discrete random variable that takes integer values. Then
    \[\expectation{X} = \sum_{x = 1} \prob{X \geq x}\]
\end{lemma}

\begin{proof}
    \begin{align*}\expectation{X}
        &= \sum_{x=1}^{\infty}x \prob{X=x}\\
        &= \sum_{x=1}^{\infty}\sum_{i=1}^x \prob{X=x}\\
        &= \sum_{x=1}^{\infty}\sum_{i=x}^{\infty}\prob{X=i}\\
        &= \sum_{x=1}^{\infty}\prob{X \geq x}
    \end{align*}
\end{proof}

\begin{theorem}{Expectation of Geometric Random Variable}{}
    Let $X \sim G(p)$. Then $\expectation{X} = \frac{1}{p}$.
\end{theorem}

\begin{proof}
    Observe that $\prob{X = k} = (1 - p)^{k - 1}p$, hence
    \[\prob{X \geq k} = (1 - p)^{k - 1}\]
    and
    \begin{align*}\expectation{X}
        &= \sum_{k=1}^{\infty} k\prob{X = k}\\
        &= \sum_{k=1}^{\infty} \prob{X \geq k}\\
        &= \sum_{k=1}^{\infty} (1 - p)^{i - 1}\\
        &= \frac{1}{1 - (1 - p)}\\
        &= \frac{1}{p}
    \end{align*}
\end{proof}

Now, returning to our Max 3-SAT algorithm, notice that the probability that it
requires $k$ iterations to output an assignment of at least $\sfrac{7m}{8}$
\True clauses is
\begin{align*}(1 - p)^k
    &\leq \left(1 - \frac{c}{m}\right)^k\hbox{ for some $c > 0$}\\
    &\leq e^{\frac{-ck}{m}}\\
    \shortintertext{setting $k = \sfrac{m}{c}\ln{n}$}
    &= \frac{1}{m}
\end{align*}
Observe that our algorithm is a Las Vegas algorithm. However, there is an
analogous Monte Carlo algorithm:
\begin{algorithm}
    \caption{Randomized Max 3-SAT Algorithm (MC Variant)}
    \begin{algorithmic}[1]
        \Function{Solve}{\texttt{formula}}
            \State $\texttt{result} \gets 0$
            \State $\texttt{bool-vec} \gets \vec{0}$
            \ForRange{$i$}{1}{$\sfrac{m}{c}\ln{n}$}
                \State $\texttt{new-bool-vec}, \texttt{new-result} \gets \Call{Try-Solve}{\texttt{formula}}$
                \If{$\texttt{new-result} > \texttt{result}$}
                    \State $\texttt{result} \gets \texttt{new-result}$
                    \State $\texttt{bool-vec} \gets \texttt{new-bool-vec}$
                \EndIf
            \EndForRange
            \State \Return \texttt{bool-vec}
        \EndFunction
    \end{algorithmic}
\end{algorithm}

\subsection{Binomial Random Variable}
\begin{definition}{Geometric Random Variable}{}
    Consider some Bernoulli trial, i.e., an event with exactly two possible
    outcomes, denotes ``success'' and ``failure'', and suppose success occurs
    with probability $p$. The \emph{Binomial Distribution}, denoted $X \sim
    B(n, p)$, is the number of successes after $n$ trials.
\end{definition}

\begin{theorem}{}{}
    Let $X \sim B(n, p)$. Then $\expectation{X} = np$.
\end{theorem}

\begin{proof}
    Again, \nameref{thm:linexp} is useful here. Let $X_i$ be the indicator
    random variable that is 1 when trial $i$ is a success. Then
    \[\expectation{X_i} = \prob{X_i = 1} = p\]
    hence
    \[\expectation{X} = \sum_{i=1}^n \expectation{X_i} = np\qedhere\]
\end{proof}

\subsection{Randomized Quicksort}
The \emph{Quicksort} algorithm is a computer science staple, one of the most
famous algorithms of the 20th century. A simplified version of the original
implementation is below:

\begin{algorithm}
    \caption{Simplified variant of Quicksort algorithm.}
    \label{alg:quicksort}
    \begin{algorithmic}[1]
        \Function{Quicksort}{\texttt{arr}}
            \State $n \gets \Call{Len}{\texttt{arr}}$
            \If{$n \leq 1}$
                \State \Return \texttt{arr}
            \Else
                \State $\texttt{left}, P, \texttt{right} \gets \Call{Partition}{\texttt{arr}}$
                \State \Return $\Call{Quicksort}{\texttt{left}} + P + \Call{Quicksort}{\texttt{right}}$
            \EndIf
        \EndFunction
        \Function{Partition}{\texttt{arr}}
            \State $\texttt{pivot} \gets \texttt{arr}[0]$
            \State $\texttt{left}, P, \texttt{right} \gets [], [], []$
            \For{$a \in \texttt{arr}}$
                \If{$a < \texttt{pivot}}$
                    \State $\texttt{left}.\Call{Append}{a}$
                \ElsIf{$a = \texttt{pivot}$}
                    \State $P.\Call{Append}{a}$
                \Else
                    \State $\texttt{right}.\Call{Append}{a}$
                \EndIf
            \EndFor
            \State \Return $\texttt{left}, P, \texttt{right}$
        \EndFunction
    \end{algorithmic}
\end{algorithm}

Notice that the worst case of this algorithm is $\bigO{n^2}$.
%TODO demonstrate worst case quicksort
On the other hand, the best case is $\bigO{n\log{n}}$.
%TODO demonstrate best case quicksort
We can consider a randomized variant --- functionally, the same algorithm, but
with the partition scheme changed. Rather than selecting the element with index
0 as the pivot, select a \emph{random} element as the pivot. Let us consider the
number of comparisons in this variant.

\begin{theorem}{}{}
    The above randomized variant of Quicksort has an expected runtime of
    $\bigO{n\log{n}}$.
\end{theorem}

\begin{proof}
    Let $X$ denote the number of comparisons. It should be clear that
    $\prob{X=x}$ is impractical to compute directly. Instead, notice that two
    elements of the array will be compared \emph{at most} once --- when one of
    the two elements is the pivot \emph{end}.

    Assume the sorted order of the elements is $a_0$, $a_1$, \dots, $a_n$, and
    let $X_{i,j}$ denote the indicator random variable that is 1 when $a_i$ and
    $a_j$ are compared. Notice that if any of $a_{i+1}$, $a_{i + 2}$, \dots,
    $a_{j - 1}$ are chosen, then $a_i$ and $a_j$ will end up in separate
    partitions and never be compared. Additionally, if any values $a_0$, $a_1$,
    \dots, $a_{i - 1}$ or $a_{j + 1}$, $a_{j + 2}$, \dots, $a_{n-1}$ are chosen,
    this will have no effect on whether $a_i$ and $a_j$ are compared.

    Thus, the probability that $a_i$ and $a_j$ are compared is simply the
    probability that either value is chosen as the pivot from $a_i$, $a_{i +
    1}$, \dots, $a_j$, which is
    \[\frac{2}{j - i + 1}\]
    Thus, we have
    \begin{align*}\expectation{X}
        &=\sum_{i=0}^{n-1}\sum_{j=i+1}^{n-1}\expectation{X_{i,j}}\\
        &=\sum_{i=0}^{n-1}\sum_{j=i+1}^{n-1}\frac{2}{j - i + 1}\\
        \shortintertext{writing $k = j - i$}\\
        &=\sum_{i=0}^{n-1}\sum_{k=1}^{n - i - 1}\frac{2}{k + 1}\\
        &\leq\sum_{i=0}^{n-1}2H_n\\
        &\leq 2nH_n\\
        &=\bigO{n\log{n}}\qedhere
    \end{align*}
\end{proof}


\chapter{Intensity models}
\label{chap:intensity}

\added{A rigorous treatment of intesity modeling can be found in} \textcite{bieleckirutkowski2002credit} and \textcite{brigo2007interest} contains a gentler introduction.

\section{Foundations of intensity models}

\subsection{Introduction}

\added{This section is based on } \textcite[pp. 759--764]{brigo2007interest}.

Let $(\Omega, \F, \Pm)$ be a fixed probability space and $\default$ a random time, meaning that $\default : \Omega \rightarrow \R_+ \cup \{ 0 \}$ is a measurable random variable. In our setting $\default( \omega )$ will be the time of the default. The corresponding indicator variable is $H_t = \1_{ \{\default \leq t \} }$. Hence, $H_t=0$ before the default and $H_t = 1$ at the default and after it.

If we assume that $\default$ is exponentially distributed with parameter $\gamma > 0$, then
  \begin{align}
    \label{exponential-cumulative-inverse}
    \Pm( \default > t ) = \e^{- \gamma t } .
  \end{align}
We note that the random variable $\default \gamma$ is exponentially distributed with parameter $1$, since
  \begin{align}
    \label{exponentiallywithparameter1}
    \Pm( \default \gamma > t ) = \Pm( \default > \frac{t}{\gamma} ) = \e^{-t} .
  \end{align}
Now
  \begin{align}
    \Pm( \default > t + \dx t \ | \ \default > t ) & = \frac{ \Pm( \default > t + \dx t ) }{ \Pm( \default > t ) } \\ &= \e^{ - \gamma ( t + \dx t ) } \e^{ \gamma t } \\
    & =  \e^{ - \gamma \dx t } \\
    &= \Pm( \default > \dx t ) ,
  \end{align}
which implies that the distribution has no memory. While memorylessness is often a desirable property, time homogeneous is not a desirable property in credit risk modeling.

However, we can easily see the theoretical connection between interest rate and default intensity using a simplified model, where the risk-free rate $r$ and the parameter $\gamma$ are positive constants. Now the zero-coupon bond with maturity $T$ and no recovery value at the default has the expected discounted value of
  \begin{align}
    \Pm( \default > t ) \e^{-r T} + \Pm( \default \leq t ) \cdot 0 = \e^{ - (r+\gamma) T} .
  \end{align}
Thus the parameter $\gamma > 0$ can be seen as a credit spread over the risk-free rate.

We can generalize the equation \ref{exponential-cumulative-inverse} and introduce time-dependency by setting
  \begin{align}
    \Pm( \default > t ) = \e^{- \Gamma(t) } ,
  \end{align}
where $\Gamma(t)$ is the cumulative hazard function. Intuitively we assume that $\Gamma$ is a strictly increasing function. Now if $r(t)$ is a deterministic short-rate, then
a defaultable zero-coupon bond with maturity $T$ and no recovery value has expected discounted value of
  \begin{align}
    \label{price_with_cumulative_hazard_function}
    \e^{ - \left( \int_0^T r(s) \dx s + \Gamma(T) \right) }.
  \end{align}

As in equation \ref{exponentiallywithparameter1}, if $\xi = \Gamma(\default)$, then
  \begin{align}
    \Pm( \Gamma(\default) > t ) = \Pm( \default > \Gamma^{-1}(t) ) = \e^{-t}
  \end{align}
meaning that $\xi$ is exponentially distributed with parameter $1$ and $\default \sim \Gamma^{-1}(\xi)$. Now we may simulate the default time by drawing a realization of $\xi$ and taking $\default = \Gamma^{-1}(\xi)$.

Next we assume that
  \begin{align}
    \label{Gammaisgammaintegral}
    \Gamma(t) = \int_0^t \gamma (s) \dx s,
  \end{align}
where $\gamma > 0$ almost everywhere. Now the equation \ref{price_with_cumulative_hazard_function} can be written as
  \begin{align}
    \e^{ - \int_0^T \left( r(s) + \gamma(s) \right) \dx s }
  \end{align}
and again $\gamma(t)$ can be viewed as a credit spread over the risk-free rate. Now
  \begin{align}
    \Pm( t \leq \default < t + \dx t ) &= \e^{- \Gamma(t) } - \e^{- \Gamma(t + \dx t) } \\
      &=\e^{- \Gamma(t) } \left( 1 - \e^{ - \int_t^{t + \dx t} \gamma (s) \dx s } \right) \\
      &\approx \e^{- \Gamma(t) } \int_t^{t + \dx t} \gamma (s) \dx s \\
      &\approx \e^{- \Gamma(t) } \lambda(t) \dx t \\
      &= \e^{ - \int_0^t \gamma (s) \dx s } \lambda(t) \dx t ,
  \end{align}
where the first approximation uses $\e^x \approx 1 + x$ given $x \approx 0$ and the second is based on the definition of the integral and the assumption that $\lambda$.

Similarly the conditional probability has the following approximation
  \begin{align}
    \Pm (\default \leq t + \dx t | \default > t) &= \frac{ \Pm (t < \default \leq t + \dx t) }{ \Pm (\default > t) } \\
      &= \frac{ \e^{- \Gamma(t) } - \e^{- \Gamma(t + \dx t) } }{ \e^{- \Gamma(t) } } \\
      &= 1 - \e^{ - \int_t^{t + \dx t} \gamma (s) \dx s } \\
      &\approx \gamma(t) \dx t
  \end{align}

Suppose that $F$ is the cumulative distribution function of $\default$, so
  \begin{align}
    F(t) = \Pm ( \default \leq t ),
  \end{align}
and the function $F$ is absolutely continuous. This means that the derivative of $F$ exists and $F' = f$ almost everywhere, where $f$ is the density funtion of $\default$. We denote $\bar{F} (t) = 1 - F (t) = \Pm ( \default > t )$ and make the following assumptions
  \begin{enumerate}[labelindent=\parindent, leftmargin=*, label*=(\Alph*)]
    \item $\Pm ( \default = t ) = F ( 0 ) = 0$ for all $t \geq 0$. \label{intensityassumptionA}
    \item $F (t) < 1$ for all $0 \leq t < \infty$. \label{intensityassumptionB}
  \end{enumerate}
Now we may use Bayes rule to see that
\begin{align}
     \frac{\Pm (\default \leq t + \dx t | \default > t)}{\dx t} &= \frac{\Pm (t < \default \leq t + \dx t)}{ \Pm (\default > t) \dx t } \\
     &=  \frac{F(t+\dx t)-F(t)}{ \Pm (\default > t) \dx t } \\
     &\longrightarrow \frac{f(t)}{\Pm (\default > t)} \\
     &= \frac{f(t)}{\bar{F} (t)} \\
     &= - \frac{\dx}{\dx t} \log ( \bar{F} (t) ) \\
     &= \frac{\dx}{\dx t} \Gamma(t) \\
     &= \gamma(t)
  \end{align}
as $\dx t \rightarrow 0^+$.

We note that
	\begin{align}
		\dx \Pm ( \default > t ) &= - \lambda (t) \e^{ - \int_0^t \gamma (s)  \dx s } \\
			&= - \lambda (t) \dx \Pm ( \default > t )
	\end{align}
holds.

In summary,
  \begin{align}
    \gamma (t) &= \Gamma' (t) \\
    \Gamma (t) &= \int_0^t \gamma (s) \dx s \\
    \Pm ( \default > t ) &= \e^{- \Gamma (t) } = \e^{ - \int_0^t \gamma (s)  \dx s } \\
    \gamma (t) \dx t &\approx \Pm (\default \leq t + \dx t | \default > t) .
  \end{align}
In general setting, the function $\gamma(t)$ is the hazard function of $\default$. The hazard function can be seen as the instantaneous probability of default happening just after the time $t$ given the survival up to time $t$. In the context of credit risk, we shall call the function $\gamma$ as the intensity function.

If $\lambda (t) = \lambda > 0$ is a deterministic constant, then $F$ is the cumulative distribution function of exponential distribution with parameter $\lambda$ and therefore $\E (\default) = 1/\lambda$ and $\Var (\default) = 1/\lambda^2$. Also
  \begin{align}
    \label{survivalexpectationforconstanthazard}
    \E ( \1_{ \{ \default > t \} } ) = \Pm ( \default > t ) = \e^{-\int_0^t \lambda_s \dx s} = \e^{-\lambda t} .
  \end{align}
Thus $\default$ is signaled by the first jump of time-homogenous Poisson distribution with parameter $\lambda$. Similarly, if $\lambda (t) > 0$ is deterministic function, then $\default$ is the first jump of non-homogenous Poisson distribution with rate function $\lambda(t)$. If $\lambda (t)$ is a stochastic process, then $\default$ will follow a Cox process.

\subsection{The credit triangle}

\added{The derivation of the credit triangle follows } \textcite[pp. 54--55]{o2011modelling} although the note after the credit triangle might be original.

In this section we consider a simplified CDS contract in the intensity framework. We assume the following

\begin{enumerate}[labelindent=\parindent, leftmargin=*]
	\item $\lambda (t) = \lambda > 0$ is a deterministic constant.
	\item The timing of default $\default$ is independent from interest rates under the measure $\Pm$.
	\item The recovery rate $0 \leq \Rec \leq 1$ is a deterministic constant and it is paid at the moment of the default.
	\item CDS with no upfront costs pays premium continuously at rate $s$ until the default $\default$ or the termination date $T$.
\end{enumerate}

The last assumption means that in the interval $[t,t+ \dx t]$ the paid premium is $s \dx t$ and if $\dx t$ is tiny, then the present value of this is $\Bond(0,t) s \dx t$. The value of the premium leg is then
\begin{align}
\label{trianglevaluationpremium}
\E \left( \int_0^T \DF(0,t) s \1_{\{ \default > t \}} \dx t  \right) &= s \int_0^T \E \left( \DF(0,t) \1_{\{ \default > t \}} \right) \dx t \\
&= s \int_0^T  \Bond(0,t) \Pm ( \default > t ) \dx t .
\end{align}

For the valuation of the protection leg, we calculate
\begin{align}
\E \left( \DF(0,\default) (1-\Rec) \1_{ \{ \default \leq T \} } \right) &= (1-\Rec) \E \left( \int_0^T \DF(0,t) \1_{ \{ t \leq \default < \default + \dx t \} } \right) \\
&= \LGD \int_0^T \E \left( \DF(0,t) \1_{ \{ t \leq \default < \default + \dx t \} } \right) \\
&= \LGD \int_0^T \Bond(0,t) \Pm ( t \leq \default < t + \dx t ) \\
&= - \LGD \int_0^T \Bond(0,t) \dx \Pm ( \default > t ),
\end{align}
where in the last step we used the derivative of the identity $\Pm ( \default \leq t ) = 1 - \Pm ( \default > t )$.

Now
\begin{align}
\E \left( \DF(0,\default) \LGD \1_{ \{ \default \leq T \} } \right) = \LGD \lambda \int_0^T \Bond(0,t) \Pm ( \default > t ) \dx t
\end{align}
and since this must be equal to value in equation (\ref{trianglevaluationpremium}), we get the following identity
\begin{align}
s = \lambda \LGD.
\end{align}
This is the credit triangle. It is also quick and easy to understand, but only one of the three variables are actually directly observable from market data. If $\Rec = 0$, then $s = \lambda$ and the default intensity is the coupon rate intensity of the CDS.

It should be noted that the model has pathological behavior if $\Rec \approx 1$. If $\Rec = 1$, then a defaultable zero coupon bond is more valuable than otherwise identical risk-free bond since the defaultable bond might pay the principal earlier\footnote{One reasonable restriction that will preclude this is $\Rec < \Bond(0,T)$}. Since the recovery value is rarely near the notional value, this is not a serious problem.

Nowadays credit default swaps are traded with standardized coupon rates and upfront payments. However, if the CDS has a upfront value of $U$, then
	\begin{align}
		U &= s \int_0^T  \Bond(0,t) \Pm ( \default > t ) \dx t - \LGD \lambda \int_0^T \Bond(0,t) \Pm ( \default > t ) \dx t \\
		&= (s - \lambda \LGD) Q(r, \lambda) ,
	\end{align}
where
	\begin{align}
		Q(r, \lambda) &= \int_0^T  \Bond(0,t) \Pm ( \default > t ) \dx t .
	\end{align}
If we assume that the short-rate is roughly a constant $r$ for all times $0 < t < T$, then
	\begin{align}
		Q(r, \lambda) &\approx \int_0^T  \e^{-(r+\lambda)t} \dx t \\
			&= \frac{1-\e^{-(r+\lambda)T}}{r+\lambda}
	\end{align}
and thus
	\begin{align}
		U &= (s - \lambda \LGD) \frac{1-\e^{-(r+\lambda)T}}{r+\lambda}.
	\end{align}

\section{Pricing}

\added{The pricing argumentation follows closely } \textcite[pp. 790--792]{brigo2007interest}

In this section 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}
\G_t = \F_t \vee \Hf_t
\end{align}
we denote the smallest $\sigma$-algebra containing $\F_t$ and $\Hf_t$. We assume that conditions \ref{DS1} and \ref{DS2} of Section \ref{sec:doublystochastic} are satisfied by the process $\lambda$.

Theorem \ref{eq_takingthedefaultinformationoutdoubly} is an important tool in our arsenal, so we restate it here. Under very reasonable assumptions, we have that
	\begin{align}
		\E_{\Pm} \left( \1_{ \{ \default > T \} } X | \G_t  \right) = \1_{ \{ \default > t \} } \e^{ \int_0^t \lambda(s) \dx s }  \left( \1_{ \{ \default > T \} } X | \F_t \right)
	\end{align}
for random variables $X$ and $T \geq t$.

\subsubsection{Defaultable zero coupon bond with no recovery}

A defaultable $T$-bond with no recovery has pay-off $H_T = \1_{\{\default > T\}}$. Now by Lemma \ref{eq_takingthedefaultinformationoutdoubly},
  \begin{align}
    \DBond_0(t,T) =& \E_{\Pm} \left( \exp^{ - \int_t^T r(s) \dx s } H_T \ | \ \F_t \vee \Hf_t \right) \\
    =& \1_{\{\default > t\}} \exp^{ \int_0^t \lambda (s) \dx s } \E_{\Pm} \left( \exp^{ - \int_t^T r(s) \dx s } H_T \ | \ \F_t \right) \\
    =& \1_{\{\default > t\}} \exp^{ \int_0^t \lambda (s) \dx s } \E_{\Pm} \left( \exp^{ - \int_t^T r(s) \dx s } \E_{\Pm} \left( H_T | \F_T \right) \ | \ \F_t \right) \\
    =& \1_{\{\default > t\}} \exp^{ \int_0^t \lambda (s) \dx s } \E_{\Pm} \left( \exp^{ - \int_t^T r(s) \dx s } \exp^{ - \int_0^T \lambda (s) \dx s }  \ | \ \F_t \right) \\
=& \1_{\{\default > t\}} \E_{\Pm} \left( \exp^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right)
  \end{align}
If $\lambda(s) \geq 0$ almost surely, then we may see
  \begin{align}
  r(s) + \lambda (s) \geq r(s)
  \end{align}
is the defaultable short-rate. Thus we may reuse all the machinery from the short-rate models.

\subsubsection{Defaultable zero coupon bond with partial recovery $0 < \Rec < 1$ at the maturity}

A defaultable zero coupon bond with maturity $T$ and partial recovery at the maturity has pay-off
  \begin{align}
    \1_{\{\default > T\}} + \Rec \1_{\{\default \leq T\}} &= ( 1 - \Rec ) \1_{\{\default > T\}} + \Rec \\
    &= \1_{\{\default > T\}} \LGD + \Rec
  \end{align}
at the maturity. Thus the price of it at the time $t$ is
  \begin{align}
    \DBond_M(t,T) = \DBond_0(t,T) \LGD +  \Bond(t,T) \Rec,
  \end{align}
where $\DBond_0$ is the price of defaultable zero coupon bond with no recovery and $\Bond$ is the price of non-defaultable zero coupon bond.

\subsubsection{Defaultable zero coupon bond with partial recovery at the default}

The price of a defaultable zero coupon bond with partial recovery at the default is
\begin{align}
\DBond_D(t,T) = \DBond_0 (t,T) + \Rec Q(t,T) ,
\end{align}
where
  \begin{align}
    Q(t,T) = \E_{\Pm} \left( \e^{ - \int_t^{\default} r(s) \dx s } \1_{\{t < \default \leq T\}} \ | \ \F_t \vee \Hf_t \right),
  \end{align}
which is the expected value of $1$ paid at the time of the default at the time $t$. Now
  \begin{align}
    Q(t,T) &= \frac{ \1_{ \{\default > t \} } }{ \Pm (\default > t | \F_t) } \E_{\Pm} \left( \e^{ - \int_t^{\default} r(s) \dx s } \1_{\{t < \default \leq T\}} \ | \ \F_t \right) \\
    = & \1_{ \{\default > t \} } \e^{ \int_0^t \lambda(s) \dx s } \E_{\Pm} \left( \int_0^{\infty} \1_{\{t < \default \leq T\}} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right) \\
    = & \1_{ \{\default > t \} } \e^{ \int_0^t \lambda(s) \dx s } \E_{\Pm} \left( \int_{t}^{T} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right) .
  \end{align}
Now we can use Fubini's theorem to evaluate
  \begin{align}
  &\E_{\Pm} \left( \int_{t}^{T} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right) \\
  = &\E_{\Pm} \left( \E_{\Pm}  \left( \int_{t}^{T} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    = &\E_{\Pm} \left( \int_{t}^{T} \DF(t,s) \E_{\Pm}  \left( \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    = &\E_{\Pm} \left( \int_{t}^{T} \DF(t,s) \Pm \left( s \leq \default < s + \dx s \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    =  &\E_{\Pm} \left( \int_{t}^{T} \DF(t,s) \lambda(s) \e^{ - \int_0^s \lambda(u) \dx u } \dx s \ | \ \F_t \right) \\
    =  &\E_{\Pm} \left( \int_{t}^{T} \e^{ - \int_t^s r(u) \dx u }  \lambda(s) \e^{ - \int_0^s \lambda(u) \dx u } \dx s \ | \ \F_t \right) .
  \end{align}
Thus
  \begin{align}
    Q(t,T) &= \1_{ \{\default > t \} } \E_{\Pm} \left( \int_{t}^{T}  \lambda(s) \e^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \dx s \ | \ \F_t \right) \\
    &= \1_{ \{\default > t \} } \int_{t}^{T} \E_{\Pm} \left( \lambda(s) \e^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \ | \ \F_t \right) \dx s
  \end{align}
 and
\begin{align}
\DBond_D(t,T) = \DBond_0 (t,T) + \1_{ \{\default > t \} } \Rec \int_{t}^{T} \E_{\Pm} \left( \lambda(s) \e^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \ | \ \F_t \right) \dx s
\end{align}
\subsection{The protection leg of a credit default swap}

We now developed a price for the protection leg that pays $\LGD$ at the default $\default$, if $S < \default \leq T$. The price of it at the time $0 \leq t < T$ is
  \begin{align}
    \Protection(t) &= \1_{ \{ \default > t \} } \E_{\Pm} \left( \1_{ \{ S < \default < T \} } \DF(t,\default) \LGD \ | \ \G_t \right) \\
    &= \frac{ \1_{ \{ \default > t \} } }{ \Pm (\default > t | \F_t ) } \E_{\Pm} \left( \1_{ \{ S < \default < T \} } \DF(t,\default) \LGD \ | \ \F_t \right) .
  \end{align}
Now heuristically
  \begin{align}
    & \E_{\Pm} \left( \1_{ \{ S < \default < T \} } \DF(t,\default) \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_0^{\infty} \1_{ \{ S < s < T \} } \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_{S}^{T} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \E_{\Pm}  \left( \int_{S}^{T} \DF(t,s) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_{S}^{T} \DF(t,s) \E_{\Pm}  \left( \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_{S}^{T} \DF(t,s) \Pm \left( s \leq \default < s + \dx s \ | \ \F_{T} \right) \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_{S}^{T} \DF(t,s) \lambda(u) \exp^{ - \int_0^s \lambda(u) \dx u } \dx s \ | \ \F_t \right) \\
    = & \E_{\Pm} \left( \int_{S}^{T} \exp^{ - \int_t^s r(u) \dx u }  \lambda(u) \exp^{ - \int_0^s \lambda(u) \dx u } \dx s \ | \ \F_t \right) \\
    = & \exp^{ \int_0^t \lambda(u) \dx u } \E_{\Pm} \left( \int_{S}^{T}  \lambda(s) \exp^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \dx s \ | \ \F_t \right) .
  \end{align}
Thus
 \begin{align}
\Protection(t) &= \1_{ \{ \default > t \} } \LGD \E_{\Pm} \left( \int_{S}^{T}  \lambda(s) \exp^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \dx s \ | \ \F_t \right) \\
	&= \1_{ \{ \default > t \} } \LGD \int_{S}^{T} \E_{\Pm} \left( \lambda(u) \exp^{ - \int_t^s (r(u) + \lambda(s)) \dx u }  \ | \ \F_t \right) \dx s,
\end{align}
where we have assumed that the $\LGD$ is a constant.

\subsection{The premium leg of a credit default swap}

The premium leg of a CDS with a coupon rate $C$ has a value
\begin{align}
\Premium(t, C) = &\1_{ \{ \default > t \} } \E_{\Pm} \left( \DF(t,\default) C^{h(\default)} \1_{ \{ S < \default < T \} } \ | \ \G_t \right) \\
	&+ \1_{ \{ \default > t \} } \sum_{i=1}^n \E_{\Pm} \left( \DF(t,t_i) C_i \1_{ \{ \default > t_i \} } \ | \ \G_t \right) ,
\end{align}
where $C_i = C \dayc (t_{i-1}, t_i)$, $t_{h(\default)}$ is the last coupon date before the default (if it occurs) and $C^{h(\default)} = C \dayc (t_{h(\default)}, \default) \approx C(\default - t_{h(\default)})$. We have also re-indexed the coupon dates so that $t_0 \leq t \leq t_1$.

Now
	\begin{align}
	 	C_i(t,T) &= \E_{\Pm} \left( \DF(t,t_i) \1_{ \{ \default > t_i \} } \ | \ \G_t \right) \\
	 	&= \1_{ \{ \default > t_i \} } \exp^{ \int_0^t \lambda(s) \dx s } \E_{\Pm} \left( \DF(t,t_i) \1_{ \{ \default > t_i \} } \ | \ \F_t \right) \\
	 	&= \1_{ \{ \default > t_i \} } \exp^{ \int_0^t \lambda(s) \dx s } \E_{\Pm} \left( \exp^{ \int_t^{t_i} r(s) \dx s } \exp^{ \int_0^{t_i} \lambda(s) \dx s } \ | \ \F_t \right) \\
	 	&= \1_{ \{ \default > t_i \} } \E_{\Pm} \left( \exp^{ \int_t^{t_i} (r(s) - \lambda(s)) \dx s } \ | \ \F_t \right)
	\end{align}
and, by recycling the earlier calculations, we get that
	\begin{align}
C^{h(\default)}(t,T) &= \E_{\Pm} \left( (\default - t_{h(\default)}) \DF(t,\default) \1_{ \{ S < \default < T \} } \ | \ \G_t \right) \\
	&= \1_{ \{ \default > t \} } \exp^{ \int_0^t \lambda(s) \dx s } E
\end{align}
where the expectation
	\begin{align}
		E &= \E_{\Pm} \left( (\default - t_{h(\default)}) \DF(t,\default) \1_{ \{ S < \default < T \} } \ | \ \F_t \right) \\
		&= \E_{\Pm} \left( \int_t^{\infty} (s - t_{h(s)}) \DF(t,s)  \1_{ \{ S < s < T \} } \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_t \right)	\\
		&= \E_{\Pm} \left( \E_{\Pm} \left( \int_S^T (s - t_{h(s)}) \DF(t,\default) \1_{ \{ s \leq \default < s + \dx s \} } \ | \ \F_T \right)  \ | \ \F_t \right) \\
		&= \E_{\Pm} \left( \int_S^T (s - t_{h(s)}) \DF(t,s) \Pm( s \leq \default < s + \dx s \ | \ \F_T )  \ | \ \F_t \right) \\
		&= \E_{\Pm} \left( \int_S^T (s - t_{h(s)}) \DF(t,s) \e^{ -\int_0^s \lambda(u) \dx u } \lambda(s) \ \dx s  \ | \ \F_t \right) \\
		&= \int_S^T \E_{\Pm} \left(  (s - t_{h(s)}) \DF(t,s) \e^{ -\int_0^s \lambda(u) \dx u } \lambda(s)  \ | \ \F_t \right) \dx s .
	\end{align}
Hence
	\begin{align}
A &= C C^{h(\default)}(t,T) \\
&= \1_{ \{ \default > t \} } C \int_S^T \E_{\Pm} \left(  (s - t_{h(s)}) \DF(t,s) \e^{ -\int_t^s \lambda(u) \dx u } \lambda(s)  \ | \ \F_t \right) \dx s	.
	\end{align}
Thus
\begin{align}
\Premium(t, C) &= \1_{ \{ \default > t \} } C \left( C^{s(\default)}(t,T) + \sum_{i=1}^n \dayc (t_{i-1}, t_i) C_i(t,T) \right) \\
	&= C \left( \sum_{i=1}^n \dayc (t_{i-1}, t_i) \DBond_0(t,t_i) \right) + A ,
\end{align}
where $\DBond_0$ is the price of zero coupon bond with no recovery and $A$ is term representing the accrued coupon before the default.

As we saw here, the most complicated part in the pricing of the premium leg of a CDS is the accrued coupon payment before the default. If we wish to simplify the model, then the accrual payment could be dropped or we could assume that premium is paid continuously. Both will result in biased priced, but this might be acceptable. If the accrued coupon payment is dropped, then the premium leg is just a portfolio of defaultable zero-coupon bonds with no recovery.

If the accrued coupon payment term has to simplified, it could be assumed that default happens in the middle of the coupon period or that the coupon is paid continuously during the accrual period.

\subsubsection{Premium leg of a CDS with continuous premium}

We may also suppose that premium leg pays a continuous premium $c$. If $\dx t > 0$ is small, then premium leg pays from $t$ to $\dx t$ the amount of $c \dx t$ assuming that the credit event does not occur. By using the old tricks, we may value this default intensity as
	\begin{align}
		&\E_{\Pm} \left( c \e^{ - \int_t^{t+\dx t} r(s) \dx s} \dx t \1_{ \{ \default > t + \dx t	  \} } \ | \ \G_t \right) \\
		= &\1_{ \{ \default  > t \} } \E_{\Pm} \left( c \e^{ - \int_t^{t+\dx t} (r(s) + \lambda(s)) \dx s} \dx t \ | \ \F_t \right) .
	\end{align}
By taking the limit of this process we have that
	\begin{align}
		\Premium(t, c) &= c \int_t^T \1_{ \{ \default  > u \} } \E_{\Pm} \left(  \e^{ - \int_t^{u} (r(s) + \lambda(s)) \dx s} \ | \ \F_t \right) \dx u \\
		&= c \int_t^T \DBond_0(t,s) \dx s ,
	\end{align}
where $\DBond_0(t,u)$ is the price of a defaultable $u$-bond with no recovery at the time $t$.

\section{The assumption that the default is independent from interest rates}

All the pricing formulas had the term
	\begin{align}
		\E_{\Pm} \left( \exp^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right) .
	\end{align}
If the default is independent of the interest rates under the risk neutral measure, then we may write
	\begin{align}
		\E_{\Pm} \left( \exp^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right) &= \Bond(t,T) \Gamma(t,T),
	\end{align}
where
	\begin{align}
		\Gamma(t,T) &= \E_{\Pm} \left( \exp^{ - \int_t^T \lambda(s) \dx s } \ | \ \F_t \right)
	\end{align}
Hence
	\begin{align}
		\DBond_0(t,T) &= \1_{ \{\default > t \} } \Bond(t,T) \Gamma(t,T) \\
		\DBond_M(t,T) &= \1_{ \{\default > t \} } \Bond(t,T) \left( \Gamma(t,T) \LGD + \Rec \right)
	\end{align}
for bonds with zero recovery or partial recovery at the maturity. As now
  \begin{align}
		Q(t,T) &= \1_{ \{\default > t \} } \int_{t}^{T} \E_{\Pm} \left( \lambda(s) \e^{ - \int_t^s (r(u) + \lambda(u)) \dx u } \ | \ \F_t \right) \dx s \\
		&= \1_{ \{\default > t \} } \int_{t}^{T} \Bond(t,s) \E_{\Pm} \left( \lambda(s) \e^{ - \int_t^s \lambda(u) \dx u } \ | \ \F_t \right) \dx s \\
		&= \1_{ \{\default > t \} } \int_{t}^{T} \Bond(t,s) \E_{\Pm} \left( - \frac{\partial}{\partial s} \e^{ - \int_t^s \lambda(u) \dx u } \ | \ \F_t \right) \dx s ,
	\end{align}
we have that
  \begin{align}
	\DBond_D(t,T) = \DBond_0 (t,T) + \1_{ \{\default > t \} } \Rec \int_{t}^{T} \Bond(t,s) \E_{\Pm} \left( - \frac{\partial}{\partial s} \e^{ - \int_t^s \lambda(u) \dx u } \ | \ \F_t \right) \dx s
	\end{align}

\section{$A(M,N)$ model for credit risk}

We assume that there are $N$ state-variables driving short-rate and intensity processes under the risk-neutral measures. Of these, $M$ follow square-root process and $N-M$ are gaussian. We follow the presentation in \textcite[pp. 457--476]{nawalkabeliaevasoto2007dynamic}.

More precisely, the correlated gaussian process are
\begin{align}
\dx Y_i(t) &= - k_i Y_i(t) \dx t + \nu_i \dx W_i (t),
\end{align}
where $W_i$ is a Wiener process and
\begin{align}
\dx W_i (t) \dx W_j (t) &= \rho_{ij} \dx t
\end{align}
for all $i,j = 1,2, \ldots, N-M$. Here $-1 < \rho_{ij} = \rho_{ji} < 1$ and $\rho_{ii} = 1$. The $M$ square-root processes are
\begin{align}
\dx X_m(t) = \alpha_m ( \theta_m - X_m(t) ) \dx t + \sigma_m \sqrt{ X_m(t) } \dx Z_m (t)
\end{align}
where $Z_m$ are independent Wiener process and
\begin{align}
\dx W_i (t) \dx Z_m (t) &= 0
\end{align}
for all $i = 1,2, \ldots, N-M$ and $m = 1,2, \ldots, M$. The short-rate is defined by
\begin{align}
\label{riskfree_process}
r(t) = \delta_r + \sum_{m=1}^{M} a_mX_m(t) + \sum_{i=1}^{N-M} c_iY_i(t),
\end{align}
and the default intensity by
\begin{align}
\label{spreadprocess}
\lambda(t) = \delta_{\lambda} + \sum_{m=1}^{M} b_mX_m(t) + \sum_{i=1}^{N-M} d_iY_i(t),
\end{align}
where $\delta_r, \delta_{\lambda}$ are constants and $a_m, c_m$ for $m=1,2, \ldots, M$ and $c_i, d_i$ for $i=1,2, \ldots, N-M$ for non-negative constants.

As shown earlier in Equation \ref{}, the price of a defaultable $T$-bond with recovery of a face value is given by
	\begin{align}
		\DBond(t,T) = \DBond_0(t,T) +\Rec \int_t^T \E_{\Pm} \left( \lambda(u) \e^{ - \int_t^u (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right) \dx u ,
	\end{align}
where
	\begin{align}
		\DBond_0(t,T) = \E_{\Pm} \left( \e^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right)
	\end{align}
is the price of a defaultable bond with no recovery.

We denote
	\begin{align}
		G(t,T) &= \E_{\Pm} \left( \lambda(T) \e^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \ | \ \F_t \right) \\
		&= \frac{\partial}{\partial \phi} \left( \eta(t,T,\phi) \right)_{\phi=0} ,
	\end{align}
where
	\begin{align}
		\eta(t,T,\phi) &= \E_{\Pm} \left( \e^{ - \int_t^T (r(s) + \lambda(s)) \dx s } \e^{\phi \lambda(T)} \ | \ \F_t \right)
	\end{align}
The solution to this expectation is given by (under certain assumptions)
	\begin{align}
		\eta(t,T,\phi) = \e^{ A^{\dagger}(\tau) - \sum\limits_{m=1}^M(a_m+b_m)B_m^{\dagger}(\tau)X_m(t) - \sum\limits_{i=1}^{N-M} (c_i + d_i) C_i^{\dagger}(\tau) Y_i(t) - H^{\dagger}(t,T) },
	\end{align}
where $\tau = T-t$ and
\begin{align}
H^{\dagger}(t,T) &= \int_t^T (\delta_r + \delta_{\lambda}) \dx x = (\delta_r + \delta_{\lambda}) \tau, \\
\beta_{1m} &= \sqrt{\alpha_m^2 + 2 (a_m+b_m) \sigma_m^2} , \\
\beta_{2m} &= \frac{-\alpha_m+\beta_{1m}}{2} , \\
\beta_{3m} &= \frac{-\alpha_m-\beta_{1m}}{2} , \\
\beta_{4m} &= \frac{-\alpha_m - \beta_{1m} + \phi b_m \sigma_m^2}{-\alpha_m + \beta_{1m} + \phi b_m \sigma_m^2} , \\
B_m^{\dagger}(\tau) &= \frac{ 2 }{ (a_m + b_m) \sigma_m^2 } \left( \frac{ \beta_{2m} \beta_{4m} \e^{\beta_{1m} \tau} - \beta_{3m} }{ \beta_{4m} \e^{\beta_{1m} \tau} - 1 } \right), \\
A_X^{\dagger}(\tau) &= \sum_{m=1}^M \frac{\alpha_m \theta_m}{\sigma_m^2} \left( \beta_{3m} \tau + \log \left( \frac{ 1-\beta_{4m}\e^{ \beta_{1m} \tau }  }{1-\beta_{4m}} \right) \right) \\
q_i &= 1 + \phi k_i \frac{d_i}{c_i + d_i} \\
C_i^{\dagger}(\tau) &= \frac{1 - q_i\e^{-k_i \tau}}{k_i}, \\
D^{\dagger} (\tau) &= \tau - q_i C_i^{\dagger}(\tau) - q_j C_j^{\dagger}(\tau) + q_iq_j \frac{ 1 - \e^{ - (k_i + k_j) \tau} }{k_i + k_j} \\
A_Y^{\dagger}(\tau) &= \sum_{i=1}^{N-M} \sum_{j=1}^{N-M}
	\frac{ (c_i+d_i)(c_j+d_j) \nu_i \nu_j \rho_{ij} }{ k_i k_j }
	 D^{\dagger} (\tau) \\
A^{\dagger}(\tau) &= \phi \delta_{\lambda} + \frac{1}{2}A_Y^{\dagger}(\tau) - 2 A_X^{\dagger}(\tau)
\end{align}
for all $i = 1,2, \ldots, N-M$ and $m=1,2, \ldots, M$. Now $G(t,T)$ can be approximated as the numerical derivative of $\eta(t,T,\phi)$ at $0$.

Under this model
	\begin{align}
		\DBond_0(t,T) = \e^{ A^{\dagger}(\tau) - \sum\limits_{m=1}^M(a_m+b_m)B_m^{\dagger}(\tau)X_m(t) - \sum\limits_{i=1}^{N-M} (c_i + d_i) C_i^{\dagger}(\tau) Y_i(t) - H^{\dagger}(t,T) }
	\end{align},
where $\tau = T-t$ and
\begin{align}
H(t,T) &= \int_t^T \left( \delta_r + \delta_{\lambda} \right) \dx x = \left( \delta_r + \delta_{\lambda} \right) \tau, \\
\beta_m &= \sqrt{ \alpha_m^2 + 2(a_c+b_m)\sigma_m^2 }, \\
B_m(\tau) &= \frac{2 (\e^{\beta_m \tau} - 1) }{ \beta_m + (\e^{\beta_m \tau} - 1) + 2 \beta_m }, \\
A_X (\tau) &=  \sum_{m=1}^M \frac{\alpha_m \theta_m}{\sigma_m^2} \log
	\frac{ 2 \beta_m \e^{ \frac{(\beta_m + \alpha_m)\tau}{2} }  }{ (\beta_m + \alpha_m) + (\e^{\beta_m \tau} - 1) + 2 \beta_m }, \\
C_i (\tau) &= \frac{1 - \e^{ - k_i \tau } }{ k_i }, \\
D(\tau) &= \tau - C_i(\tau) - C_j(\tau) + \frac{ 1 - \e^{ -(k_i+k_j)\tau } }{k_i + k_j}, \\
A_Y (\tau) &= \sum_{i=1}^{N_M} \sum_{j=1}^{N_M} \frac{ (c_i+d_i)(c_j+d_j)\nu_i \nu_j \rho_{ij} }{k_i k_j} D(\tau), \\
A(\tau) &= 2 A_X (\tau) + \frac{1}{2} A_Y (\tau) .
\end{align}
The risk-free bond $\Bond(t,T)$ may be priced using the equation above, but with $b_m = 0$ and $d_i = 0$ for all $m=1,2, \ldots M$ and $i=1,2, \ldots, N-M$. By differentiating of $Y_i^*(t) = c_i Y_i(t)$, we get that
	\begin{align}
		\dx Y_i^*(t) &= c_i \dx Y_i(t) \\
			&= -k_i c_i Y_i(t) + c_i \nu_i \dx W_i(t) \\
			&= -k_i Y_i^*(t) + \left( c_i \nu_i \right) \dx W_i(t) .
	\end{align}
This implies that we may use the formulas in Subsection \ref{subsec-AMN-interestrate} by replacing $\nu_i$ with $c_i \nu_i$ and $Y_i(t)$ with $c_i Y_i(t)$. Similarly differentiating $X_m^* = a_m X_m(t)$ yields
	\begin{align}
		\dx X_m^*(t) &= a_m \dx X_m(t) \\
			&= \alpha_m( a_m \theta_m - a_m X_m(t) ) \dx t + \sigma_m \sqrt{a_m} \sqrt{a_mX_m(t)} \dx Z_m(t) \\
			&= \alpha_m( (a_m \theta_m) - X_m^*(t) ) \dx t + (\sigma_m \sqrt{a_m}) \sqrt{X_m^*(t)} \dx Z_m(t)
\end{align}
end we see that $\theta_m$ needs to be replaced to $a_m \theta_m$, $\sigma_m$ to $\sigma_m \sqrt{a_m}$ and $X_m(t)$ to $a_m X_m(t)$. Thus we may also use the machinery of Chapter \ref{chap:fourier} to price derivatives that only depends on the risk-free rate with similar changes.

Since common state-variables may drive both risk-free rate and the default intensity, they may be correlated under the models of this family. As some of the state-variables may not be shared, these models have potentially a very rich structure.

We adopt the following notation. The model $D((a_X,b_X,c_X),(a_Y,b_Y,c_Y))$ is the model defined in the Equations \ref{riskfree_process} and \ref{spreadprocess} with the following properties
	\begin{itemize}
		\item $a_X$ is the number of square-root processes that are present in both Equations \ref{riskfree_process} and \ref{spreadprocess},
		\item $b_X$ is the number of square-root processes that are unique to the risk-free rate process,
		\item $c_X$ is the number of square-root processes that are unique to the spread process,
		\item $a_Y$ is the number of gaussian processes that are present in both Equations \ref{riskfree_process} and \ref{spreadprocess},
		\item $b_Y$ is the number of gaussian processes that are unique to the risk-free rate process and
		\item $c_Y$ is the number of gaussian processes that are unique to the spread process.
	\end{itemize}
Thus it is $A(M,N)$ model with
	\begin{align}
		M &= a_X + b_X + c_X \\
		N - M &= a_Y + b_Y + c_Y .
	\end{align}


\mychapter{20}{Lesson 20} %181205

\section{Random Oracle Model (ROM)}

The Random Oracle Model treats a given hash function $H$ as a truly random function. As a reminder: a truly random function $R$ is defined to have a specific evaluation behaviour. They do act, in fact, as truth tables\footnotemark:

\begin{itemize}
    \item if the argument hasn't been submitted to the function beforehand, then a value is chosen \uar{} from the codomain, and assigned as the image of said argument in the function;
    \item otherwise, the function will return the image as assigned in the corresponding previous evaluation.
\end{itemize}

\footnotetext{Such tables are also aptly called \emph{rainbow tables}.}

\subsection{Full domain hashing}

Let $(f, f^{-1}, \Gen)$ be a \tdp{} scheme over some domain $\mathcal{X}_{\pk}$

Take \rsa:
\begin{itemize}
    \item $(m, \pk, \sk) \pickUAR \Gen\rsa(1^\lambda)$
    \item $f(\pk, x) = x^{\pk} \mod n$
    \item $f^{-1}(\sk, y) = y^{\sk} \mod n$
\end{itemize}

Build a similar asymmetric-authentication scheme as such:

\begin{itemize}
    \item $(m, \pk, \sk) \pickUAR \Gen\rsa(1^\lambda)$
    \item $Sign_{\sk, H}(x): \sigma = f^{-1}(\sk, H(m))$
    \item $Verify_{\pk, H}: H(m) = f(pk, \sigma)$
\end{itemize}

\begin{exercise}
    Show \rsa-sign is not secure without $H$. (Hint: The scheme becomes malleable)
\end{exercise}

\begin{theorem}
    If the above scheme (full-domain hash) uses a \tdp{} for $f, f^{-1}$, then it is (asymmetric)-\ufcma under the random oracle model.
\end{theorem}

\begin{proof}
    Idea: Reduce to \tdp, program the random oracle.

    \begin{cryptoredux}
        {fdhufcma}
        {}
        {tdp}
        {ufcma(a)}

        \receive{\shortstack[l]{
            $(pk, sk) \pickUAR \Gen(1^\lambda)$ \\
            $x \pickUAR \mathcal{X}_{pk}$ \\
            $y = f(pk, x)$
        }}{$pk, y$}{}

        \invoke{$*$}{$pk$}{}

        \cseqdelay
        \cseqbeginloop
        \return{}{$m$}{}
        \invoke{}{$\sigma$}{}
        \cseqendloop
        \cseqdelay

        \return{}{$(m^*, \sigma^*)$}{}

        \send{}{$\sigma^*$}{}
    \end{cryptoredux}

    Notes: this is a loose reduction

    Some assumptions are made:

    \begin{itemize}
        \item The adversary makes the same number of RO queries as the number of signing queries done by the distinguisher (without loss of generality)
        \item The RO query must be done \emph{before} the corresponding sign query, otherwise the adversary cannot sign the messages, as specified by the scheme
    \end{itemize}

    The RO queries are actually an analogue of the definition of a random function, and it is the \emph{programming} step of the oracle itself; then if the signing queries do not correspond to any RO query, abort the game.

\end{proof}

\section{ID Scheme}

\subsubsection{``Sigma'' protocol}

\subsection{Fiat-Shamir scheme}

\subsubsection{Honest Verifier Zero-Knowledge (HVZK)}

\subsubsection{Special Soundness (SS)}