\documentclass[a4paper,12pt]{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{hyperref}

\geometry{a4paper, total={17cm,25cm}, left=2cm, top=2cm}
\numberwithin{equation}{section}
\hypersetup{colorlinks=true, linkcolor=blue}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}{Lemma}[theorem]
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\theoremstyle{remark}
\newtheorem{remark}{Remark}[section]

\title{
    \textit{Derivatives in Financial Markets with \\
    Stochastic Volatility} \\
    --- Notes and Extensions on Chapter 1 \& 2
}
\author{ }
\date{April 17, 2020}

\begin{document}
\maketitle
\tableofcontents\newpage

\section{The Black-Scholes Theory}
The aim of this chapter is to review the basic objects, ideas,
and results of the classical Black-Scholes theory of derivative
pricing. There are lots of methods for BS's theory derivation:
\begin{enumerate}
    \item derive BS-PDE via replicating strategies
    and self-financing property
    \item derive BS-PDE via hedging
    \item obtain pricing formula from BS-PDE by PDE methods, like
    Feynman-Kac formula or Fourier transform.
    \item approximate price by binomial tree or Monte Carlo simulation
    \item risk-neutral valuation via martingale theory (finding ELMM)
\end{enumerate}

\subsection{Brownian Motion}
Following Samuelson, we assume that there are two assets in the
market. One is a risk-free asset with price $\beta_t$,
\begin{equation}
\label{eq:riskless_price}
    d \beta_{t} = r \beta_{t} dt,
\end{equation}
where $r \geq 0$ is the instantaneous interest rate for lending
or borrowing money. Setting $\beta_{0} = 1$, we have
$\beta_{t} = e^{rt}$ for $t \geq 0$. The other is the risky asset
with price $X_t$,
\begin{equation}
\label{eq:risky_price}
    d X_{t} = \mu X_{t} dt + \sigma X_{t} dW_{t},
\end{equation}
where $\mu$ is a constant mean return rate (drift rate),
$\sigma > 0$ is a constant volatility, and $(W_{t})_{t\geq 0}$
is a standard Brownian motion (Wiener process).

\begin{definition}
\label{def:bm}
Brownian Motion is a real-valued stochastic process with
continuous trajectories $t \rightarrow W_t$ that have
independent and stationary increments, i.e.
\begin{itemize}
    \item $W_{0} = 0$;
    \item for any $0<t_1 <\cdots< t_n$, the random variables
    $\left(W_{t_1},W_{t_2}-W_{t_1},\dots,W_{t_n}-W_{t_{n-1}}\right)$
    are independent;
    \item for any $0\leq s<t$, the increment
    $W_{t}-W_{s}\sim N(0,t-s)$. Especially, $W_{t} \sim N(0,t)$.
\end{itemize}
\end{definition}

\begin{definition}
The probability space is denoted by $(\Omega,\mathcal{F},P)$,
$P$ is the Wiener measure.
\end{definition}

\begin{definition}
The increasing family of $\sigma$-algebras $\mathcal{F}_t$
generated by $(W_{s})_{s\leq t}$ are called the information
on $W$ up to time $t$.
\end{definition}

\begin{definition}
All the sets of probability 0 in $\mathcal{F}_t$ is called
\textit{the natural filtration} of the BM.
\end{definition}

\begin{definition}
$(X_t)_{t\geq 0}$ is \textit{adapted} to the filtration
$(\mathcal{F}_t)_{t\geq 0}$ if $X_t$ is
$\mathcal{F}_t$-measurable for every $t$, and $X_t$ is said to
be $\mathcal{F}_t$-\textit{adapted}.
\end{definition}

\begin{remark}
We can summarize definition \ref{def:bm} via
\textit{conditional characteristic functions}. For $0\leq s<t$
and $u \in \mathbb{R}$,
\begin{equation}
\label{eq:bm_characteristic}
    E\left[e^{iu(W_{t}-W_{s})}|\mathcal{F}_{s}\right]
    =e^{-u^{2}(t-s)/2}.
\end{equation}
\end{remark}

\begin{remark}
The drawback of BM is that the trajectories are not of
bounded variation, as followed:
\begin{equation}
    E\left[\sum_{i=1}^{n}|W_{t_i}-W_{t_{i-1}}|\right]
    =nE\left[|W_{\frac{t}{n}}|\right]
    =n\sqrt{\frac{t}{n}}E\left[|W_1|\right].
\end{equation}
\end{remark}

\subsection{Stochastic Integral}
\begin{definition}
For $T$ a fixed finite time, let $(X_t)_{0\leq t\leq T}$ be a
stochastic process adapted to $(\mathcal{F}_t)_{0\leq t\leq T}$,
the filtration of the BM up to time $T$, such that
\begin{equation}
\label{eq:stochastic_integrals_condition}
    E\left[\int_{0}^{T} (X_t)^{2} dt \right] < + \infty
\end{equation}
The \textit{stochastic integral} of $X_t$ with respect to BM
is defined as a limit on $L^{2}(\Omega)$,
\begin{equation}
\label{eq:stochastic_integral}
    \int_{0}^{t} X_s dW_s = \lim_{n\rightarrow+\infty}
    \sum_{i=1}^{n} X_{t_{i-1}}(W_{t_i}-W_{t_{i-1}}),
\end{equation}
as the mesh size of the subdivision goes to zero.
\end{definition}

\begin{remark}
By iterated conditional expectations and the independent
increments property of BM, we note that
\begin{equation}
    E\left[\left(\sum_{i=1}^{n}X_{t_{i-1}}
    (W_{t_i}-W_{t_{i-1}})\right)^{2} \right]
    = E\left[\sum_{i=1}^{n}(X_{t_{i-1}})^{2}(t_{i}-t_{i-1})\right]
\end{equation}
for $t\leq T$. So, we have that
\begin{equation}
\label{eq:stochastic_integral_expectation}
    E\left[\left( \int_{0}^{t} X_s dW_s \right)^2 \right]
    = E\left[ \int_{0}^{t} X_s^2 ds \right] < +\infty
\end{equation}
\end{remark}

\begin{remark}
Also, stochastic integral in \eqref{eq:stochastic_integral} has the
\textit{martingale property}:
\begin{equation}
\label{eq:stochastic_integral_martingale}
    E\left[\int_{0}^{t} X_u dW_{u} | \mathcal{F}_{s}\right]
    = \int_{0}^{s} X_u dW_u \quad (a.s.\quad s\leq t).
\end{equation}
\end{remark}

\begin{definition}
The quadratic variation $\langle Y \rangle_t$ of the stochastic
integral $Y_t = \int_{0}^{t} X_u dW_u$ is
\begin{equation}
\label{eq:stochastic_integral_QV}
    \langle Y \rangle_t = \lim_{n\rightarrow+\infty} \sum_{i=1}^{n} (Y_{t_i} - Y_{t_{i-1}})^2
    = \int_{0}^{t} X_s^2 ds.
\end{equation}
\end{definition}

\begin{theorem}
$Y_t$ is a mean-zero, continuous and square integral martingale.
The converse is also true: every mean-zero, continuous, and square
integrable martingale is a Brownian stochastic integral.
\end{theorem}

\subsection{It\^o's Formula}
Based on Taylor's formula and the fact that $W_t$ is not
differentiable, we can use

\begin{theorem}
(It\^o's Lemma)
\begin{equation}
\label{eq:ito_BM}
    dg(W_t) = g'(W_t)dW_t + \frac{1}{2}g''(W_t)dt
\end{equation}
\end{theorem}

\begin{theorem}
(It\^o's Formula)
For $dX_t = \mu(t,X_t)dt + \sigma(t,X_t)dW_t$,
\begin{equation}
    \label{eq:ito_general}
    \begin{split}
        dg(t,X_t) & = \frac{\partial g}{\partial t}dt+\frac{\partial g}{\partial x}dX_{t}+\frac{1}{2} \frac{\partial^{2} g}{\partial {x}^{2}}d\langle X\rangle_t \\
        & = \left(\frac{\partial g}{\partial t}+\mu(t,X_t)\frac{\partial g}{\partial x}+\frac{1}{2}\sigma^{2}(t,X_t)\frac{\partial^{2} g}{\partial {x}^{2}}\right)dt + \sigma(t,X_t)\frac{\partial g}{\partial x}dW_t ,
    \end{split}
\end{equation}
where all the partial derivatives of $g$ are evaluated at $(t,X_t)$.
\end{theorem}

\begin{corollary}
\begin{equation}
\label{eq:ito_multi}
    d(X_{t}Y_{t})=X_{t}dY_{t}+Y_{t}dX_{t}+d\langle X,Y\rangle_t,
\end{equation}
where the co-variation (aka. "bracket") of $X$ and $Y$ is given by
\begin{equation}
    d\langle X,Y\rangle_t = \sigma_{X}(t,X_t)\sigma_{Y}(t,Y_t)dt.
\end{equation}
\end{corollary}

\begin{remark}
    $\langle X,X\rangle_{t}=\langle X\rangle_t$.
\end{remark}

\begin{remark}
By guessing and testing, we find the solution of
\eqref{eq:risky_price} is GBM, and the return $X_{t}/X_{0}$ is
\textit{log-normal}, i.e.
\begin{equation}
\label{eq:GBM}
    X_{t}=
    X_{0}\exp\left[(\mu-\frac{1}{2}\sigma^{2})t+\sigma W_{t}\right].
\end{equation}
\end{remark}

\begin{remark}
Notice that bankruptcy (zero stock price) in this model is a
permanent state, since it stays at zero for all time thereafter
if $X_t$ becomes zero.
\end{remark}

\subsection*{Derivative Contracts}
\label{sec:derivative_contract}
\begin{itemize}
    \item Derivatives are contracts based on the underlying
    asset price. They are also called \textit{contingent claims}.
    \item strike price $K$, \textit{maturity} date $T$
    \item \textit{Payoff} $h(X_T)$
    is the non-negative value of this contract at maturity.
    \item \textit{Derivative price} $P(t,x)$ is the value of
    this contract at time $t$ for a underlying asset price
    $X_t = x$.
    \item Exercise time $\tau$ is a \textit{stopping time}
    wrt the filtration $(\mathcal{F}_t)$,
    i.e. random time s.t. the event ${\tau\leq t}$ belongs
    to $\mathcal{F}_t$ for any $t\leq T$.
    \item "Exotic option" refers here to not a standard European
    or American option.
\end{itemize}

\subsection{Replicating Strategies}
\begin{definition}
\textbf{(replicating)} This is a way for traders to
find the value of an asset by replicating its cash flows or price
movement using other assets whose values they already know.
\end{definition}

Considering a European-style derivative with payoff $h(X_{T})$,
we replicate the derivative at maturity:
\begin{equation}
\label{eq:replicating_bs}
    a_T X_T + b_T e^{rT} = h(X_T)\quad a.s.
\end{equation}

This portfolio is to be \textit{self-financing}, i.e.
no further funds are required after the initial investment,
so
\begin{equation}
\label{eq:self_financing_definition}
    d(a_{t}X_{t}+b_{t}e^{rt})=a_{t}dX_{t}+rb_{t}e^{rt}dt,
\end{equation}
which implies that
\begin{equation}
    X_{t}da_{t}+e^{rt}db_{t}+d\langle a,X\rangle_t = 0,
\end{equation}
and integral form is
\begin{equation}
    a_{t}X_{t}+b_{t}e^{rt}=a_{0}X_{0}+b_{0}+
    \int_{0}^{t} a_s dX_{s}+\int_{0}^{t} rb_{s}e^{rs} ds,
    \quad 0\leq t\leq T.
\end{equation}

Now we assume a pricing function $P(t,x)$ in
Section \ref{sec:derivative_contract} exists and is regular
enough to apply It\^o's formula \eqref{eq:ito_general}.
\textit{No-arbitrage} assumption implies that
\begin{equation}
\label{eq:no_arbitrage}
    a_t X_t + b_t e^{rt} = P(t,X_t)\qquad \forall\;0\leq t \leq T.
\end{equation}

Differentiating \eqref{eq:no_arbitrage} and using self-financing
property \eqref{eq:self_financing_definition}, It\^o's formula \eqref{eq:ito_general}
and \eqref{eq:risky_price}, we obtain
\begin{equation}
\label{eq:BS_prototype}
    (a_{t}\mu X_{t}+b_{t}re^{rt})dt+a_{t}\sigma X_{t}dW_{t}
    =\left(\frac{\partial P}{\partial t}
    +\mu X_{t}\frac{\partial P}{\partial x} + \frac{1}{2}\sigma^{2}X_{t}^{2}\frac{\partial^{2} P}{\partial {x}^{2}}
    \right)dt + \sigma X_{t}\frac{\partial P}{\partial x}dW_{t},
\end{equation}
where all the partial derivatives of $P$ are evaluated at $(t,X_t)$.
Equating the coefficients of the $dW_{t}$ terms gives
\begin{equation}
\label{eq:risky_asset_weight}
    a_{t}=\frac{\partial P}{\partial x}(t,X_t).
\end{equation}
From \eqref{eq:no_arbitrage} we obtain
\begin{equation}
\label{eq:riskless_asset_weight}
    b_{t}=(P(t,X_{t})-a_{t}X_{t})e^{-rt}.
\end{equation}
Equating the $dt$ terms in \eqref{eq:BS_prototype} gives
\begin{equation}
\label{eq:bs_pde_0}
    r\left(P-X_{t}\frac{\partial P}{\partial x}\right)
    =\frac{\partial P}{\partial t}+\frac{1}{2}\sigma^{2}X_{t}^{2}\frac{\partial^{2} P}{\partial {x}^{2}},
\end{equation}

\begin{definition}
\textbf{(Black-Scholes partial differential equation)}
\begin{equation}
\label{eq:BS_pde}
    \mathcal{L}_{BS}(\sigma)P=0
\end{equation}
where
\begin{equation}
\label{eq:BS_op}
    \mathcal{L}_{BS}=\frac{\partial }{\partial t}
    +\frac{1}{2}\sigma^{2}x^{2}\frac{\partial^{2} }{\partial {x}^{2}}
    +r\left(x\frac{\partial }{\partial x}-\cdot\right)
\end{equation}
holds in the domain $t\leq T$ and $x>0$.
\end{definition}

\begin{remark}
It is to be solved via the final condition $P(T,x)=h(x)$.
\end{remark}

\begin{remark}
The rate of return $\mu$ does not enter at all in the valuation
of this portfolio.
\end{remark}

\subsection{Hedging}
There is another way to derive the Black-Scholes partial
differential equation that emphasizes risk elimination, aka.
\textbf{hedging}.

Let $P_{t}=P(t,X_{t})$ be the price of the option. If we sell $N_t$
options and hold $A_t$ risky asset, then the change in the value of
this portfolio is $A_{t}dX_{t}-N_{t}dP_{t}$ due to being self-financing.
This portfolio should be risk-free, so the coefficient of $dW_{t}$
should be zero, i.e.
\begin{equation}
    A_{t}dX_{t}-N_{t}dP_{t}=r(A_{t}X_{t}-N_{t}P_{t})dt.
\end{equation}
Using It\^o's formula \eqref{eq:ito_general} and
\eqref{eq:risky_price}, we have
\begin{equation}
    \begin{split}
        A_{t}(\mu X_{t}dt+\sigma X_{t}dW_{t})-N_{t}\left\{\left(
        \frac{\partial P}{\partial t}
        +\mu X_{t}\frac{\partial P}{\partial x}
        +\frac{1}{2}\sigma^{2}X_{t}^{2}\frac{\partial^{2} P}{\partial {x}^{2}}
        \right)dt-\sigma X_{t}\frac{\partial P}{\partial x}dW_{t}\right\} \\
        =r(A_{t}X_{t}-N_{t}P_{t})dt.
    \end{split}
\end{equation}
Eliminating the $dW_{t}$ terms gives
\begin{equation}
    A_{t}=N_{t}\frac{\partial P}{\partial x}(t,X_{t}).
\end{equation}

\begin{remark}
The derivation is known as \textit{Delta Hedging}: selling the
option and holding a dynamically adjusted amount of the risky
asset.
\end{remark}

\subsection{Black-Scholes Formula}
BS-PDE \eqref{eq:BS_pde} is solved with the final condition
$h(x)=(x-K)^+$. Let $C_{BS}(t,x)$ be the price of European call option.

\begin{theorem}
(BS formula for call options)
\begin{equation}
\label{eq:BS_call}
    C_{BS}(t,x)=xN(d_{1})-Ke^{-r(T-t)}N(d_{2}),
\end{equation}
where
\begin{equation}
    \label{eq:BS_d1_d2}
    \begin{split}
    d_{1}&=\frac{\log(\frac{x}{K})+
    (r+\frac{1}{2}\sigma^{2})(T-t)}{\sigma\sqrt{T-t}}, \\
    d_{2}&=d_{1}-\sigma\sqrt{T-t},
    \end{split}
\end{equation}
and
\begin{equation}
\label{eq:normal_cdf}
    N(z)=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{z} e^{-\frac{y^{2}}{2}} dy.
\end{equation}
\end{theorem}

Let $P_{BS}(t,x)$ be the price of a European put option. There is
a \textit{model-free} relationship that follows from no-arbitrage
arguments at time $T$.

\begin{theorem}
(put-call parity)
\begin{equation}
\label{eq:call_put_parity}
    C_{BS}(t,X_t)-P_{BS}(t,X_t)=X_{t}-Ke^{-r(T-t)},
\end{equation}
between put and call options with the same maturity and strike
price.
\end{theorem}

\begin{theorem}
(BS formula for put option)
\begin{equation}
\label{eq:BS_put}
    P_{BS}(t,x)=Ke^{-r(T-t)}N(-d_{2})-xN(-d_{1}),
\end{equation}
where $d_{1},d_{2},N$ are as in \eqref{eq:BS_d1_d2} and
\eqref{eq:normal_cdf}, respectively.
\end{theorem}

\begin{remark}
Through plotting, we find that the pricing function of put option
crosses over its terminal payoff for some (small enough) $x$,
which does not happen with the call option function. This fact is
important for pricing American options.
\end{remark}

\subsection{Risk-Neutral Valuation}\label{sec:risk_neutral_BS}
In the BS framework, there is a \textit{unique} probability measure
$P^{*}$ equivalent to $P$ s.t. under this probability measure:
\begin{enumerate}
    \item the discounted price $\widetilde{X}_{t}=e^{-rt}X_{t}$ is a martingale;
    \item the expected value under $P^{*}$ of the discounted
    payoff of a derivative gives its no-arbitrage price.
\end{enumerate}

Such a probability measure describing a \textit{risk-neutral} world
is called an \textit{equivalent martingale measure} or risk-neutral
measure. To find unique EMM of the discounted price, we use
\begin{equation}
    d\widetilde{X}_{t}=\sigma\widetilde{X}_{t}
    \left[dW_{t}+\left(\frac{\mu-r}{\sigma}\right)dt\right].
\end{equation}
We set
\begin{equation}
    \theta = \frac{\mu-r}{\sigma},
\end{equation}
called the \textit{market price of asset risk}, and define
\begin{equation}
\label{eq:risk_neutral_BM}
    W_{t}^{*}=W_{t}+\int_{0}^{t} \theta ds = W_{t}+\theta t,
\end{equation}
so that
\begin{equation}
\label{eq:EMM}
    d\widetilde{X}_{t}=\sigma\widetilde{X}_{t}dW_{t}^{*}.
\end{equation}

We try to construct EMM $P^{*}$, an equivalent measure to $P$,
meaning that it has the same null sets. According to
\textbf{Radon-Nikodym theorem}, we can change the measure. Let
$P^{*}$ has the density $\xi_{T}^{\theta}$ wrt $P$:
\begin{equation}
    dP^{*}=\xi_{T}^{\theta}dP
\end{equation}
where
\begin{equation}
    \xi_{T}^{\theta}=\exp(-\theta W_{T}-\frac{1}{2}\theta^{2}T).
\end{equation}

\begin{remark}
$E[\xi_{T}^{\theta}]=1$
\end{remark}

\begin{remark}\label{Radon_Nikodym}
The \textit{Radon-Nikodym} process $(\xi_{t}^{\theta})_{0\leq t\leq T}$
is a martingale wrt $\mathcal{F}_t$.
\end{remark}

\begin{remark}
For any integrable random variable $Z$,
$E^{*}[Z]=E[\xi_{T}^{\theta}Z]$.
\end{remark}

\begin{remark}
For any adapted and integrable process $(Z_t)$,\footnote{The details
can be seen in Section 2.3 of coursenotes2003.pdf.}
\begin{equation}\label{eq:EMM_expectation}
    E^{*}[Z_{t}|\mathcal{F}_{t}]
    =\frac{1}{\xi_{s}^{\theta}}E[\xi_{t}^{\theta}Z_{t}|\mathcal{F}_s].
\end{equation}
\end{remark}

\begin{remark}\label{Girsanov_TH}
Under the probability $P^{*}$, the process $(W_{t}^{*})$ is a
standard BM. This result in its full generality, when $\theta$ is
an adapted stochastic process, is known as \textbf{Girsanov's theorem}.
\footnote{The details and proof can be seen in Section 4 of
coursenotes2003.pdf.} In our case ($\theta$ is constant), it is
easily derived by using the characterization
\eqref{eq:bm_characteristic} and formula \eqref{eq:EMM_expectation}.
\end{remark}

\begin{definition}
\textbf{(Self-financing)}
Let $V_t$ be the portfolio value at time $t$
\begin{equation}
    V_t = a_t X_t + b_t e^{rt}.
\end{equation}
Then the self-financing means that $\widetilde{V}_{t}=e^{-rt}V_{t}$
is a martingale under EMM $P^{*}$, i.e.
\begin{equation}
\label{eq:value_risk_neutral}
    d\widetilde{V}_{t}=a_{t}d\widetilde{X}_{t}
    =\sigma a_{t}\widetilde{X}_{t}dW_{t}^{*}.
\end{equation}
\end{definition}

\begin{remark}
Due to being self-financing,
\begin{equation}
\label{eq:risk_neutral_valuation}
    V_{t}=E^{*}[e^{-r(T-t)}H|\mathcal{F}_{t}]
\end{equation}
where $H=h(X_T)$.
\end{remark}

The existence of self-financing portfolio is guaranteed by

\begin{theorem}\label{th:martingale_representation}
    (Martingale Representation Theorem) For $0\leq t\leq T$,
    \begin{equation}
        M_{t}=E^{*}[e^{-rT}H|\mathcal{F}_{t}]
    \end{equation}
    defines a square integrable martingale under $P^{*}$ wrt
    $(\mathcal{F}_t)$, which is also the natural filtration of the
    BM $W^{*}$.
\end{theorem}

\begin{definition}
\textbf{(Markov Property)}
$X_t$ is Markovian if conditioning wrt $\mathcal{F}_{t}$ is the
same as conditioning wrt $X_t$, i.e.
$E[\cdot|\mathcal{F}_{t}]=E[\cdot|X_{t}]$.
\end{definition}

\begin{remark}
Through this method, we can obtain the same pricing formula, i.e.
\begin{equation}
    \begin{split}
        P(t,x)&=E^{*}[e^{-r(T-t)}h(X_{T})|X_{t}=x] \\
        &=\frac{1}{\sqrt{2\pi(T-t)}}\int_{-\infty}^{+\infty} e^{-r(T-t)}h(xe^{(r-\frac{\sigma^{2}}{2})(T-t)+\sigma z})e^{-\frac{z^{2}}{2(T-t)}} dz.
    \end{split}
\end{equation}
\end{remark}

\subsection*{American Options}
Via the theory of \textit{optimal stopping}, it can be shown
that the price of an American derivative with payoff function $h$
is obtained by maximizing the expected value of the discounted
payoff. In other words,
\begin{equation}
\label{eq:American_formula}
    P(t,x)=\sup_{t\leq\tau\leq T} E^{*}[e^{-r(T-t)}h(X_{\tau}^{t,x})],
\end{equation}
where $(X_{\tau}^{t,x})_{\tau\geq t}$ is the stock price starting
at $X_{t}=x$.

In order to determine the optimal stopping time $\tau^{*}$, this
leads to a \textit{free boundary value problem}. According to a
no-arbitrage argument, we have

\begin{theorem}
For non-negative interest rates and no dividend paid, the price of
an American call option is the same as its corresponding European
option.
\end{theorem}

\begin{remark}
The price of an American put option is in general strictly higher
than the price of the corresponding European put option.
\end{remark}

\subsection*{Complete Market}
In the previous sections we assumed the existence, uniqueness and regularity
of the solution of the \textit{parabolic partial differential
equation (aka. heat equation)}
\begin{equation}
\label{eq:parabolic_pde}
    \frac{\partial u}{\partial t}
    +\frac{1}{2}\sigma^{2}(t,x)\frac{\partial^{2} u}{\partial {x}^{2}}
    +\mu(t,x)\frac{\partial u}{\partial x}-ru=0
\end{equation}
with the final condition $u(T,x)=h(x)$ in order to apply It\^o's formula.
A sufficient condition for this is that the coefficients $\mu$ and $\sigma$
are regular enough and that there exists a positive constant $A$ s.t.
\begin{equation}
\label{eq:sigma_not_too_small}
    \sigma^{2}(t,x)\geq A\geq 0\quad \forall\; t\geq 0,x\in \mathcal{D}
\end{equation}
where $\mathcal{D}$ is the domain of the process $(X_t)$.

In addition, the model we have analyzed here is an example of a
\textit{complete} market model.
\begin{definition}
    Complete market is one in which every contingent claim can be
    replicated by a self-financing trading strategy in the stock
    and bond.
\end{definition}

When looking at stochastic volatility market models, we shall see
that the market is \textit{incomplete}. There is a whole family of
EMMs, and derivatives securities cannot be perfectly hedged with
just the stock and bond.

\newpage
\section{Stochastic Volatility Models}
The BS model needs a number of assumptions that are, to some extent,
"counterfactual":
\begin{enumerate}
    \item continuity of the stock-price process (it does not jump),
    \item the ability to hedge continuously without transaction costs,
    \item independent Gaussian returns,
    \item constant volatility.
\end{enumerate}
We shall focus here on relaxing the last assumption by allowing
volatility to vary randomly, for the following reason: a well-known
discrepancy between BS-predicted European option prices and market-traded
options prices, \textit{the smile curve}, can be accounted for by
SV models.

\subsection{Implied Volatility and the Smile Curve}
\begin{definition}
\textbf{(Implied Volatility)}
Given an European call option with observed price $C^{obs}$, strike
price $K$ and expiration date $T$. The implied volatility $I$ is
the value of the volatility parameter that must go into BS formula
\eqref{eq:BS_call} to match,
\begin{equation}
\label{eq:implied_volatility}
    C_{BS}(t,x;K,T;I)=C^{obs}.
\end{equation}
\end{definition}

\begin{remark}
A unique non-negative IV $I>0$ can be found if
$C^{obs}>C_{BS}(t,x;K,T;0)$ since
\begin{equation}
    \frac{\partial C_{BS}}{\partial \sigma}
    =\frac{xe^{-\frac{d_{1}^{2}}{2}}\sqrt{T-t}}{\sqrt{2\pi}}>0.
\end{equation}
\end{remark}

\begin{remark}
The implied volatilities from put and call options of the same $K,T$
are the same due to put-call parity \eqref{eq:call_put_parity}.
\end{remark}

\begin{definition}
\textbf{(Smile Effect)} Implied volatilities of the market prices
vary with strike price and the time to maturity of the contract.
\end{definition}

Before the 1987 crash, the graph of $I(K)$ for fixed $t,x,T$
from market options prices was often observed to be the
$\cup$-shaped, called the smile curve or \textit{smirk}. Since 1987,
the curve is more typically downward sloping at and near the money
$(95\%\leq K/x\leq105\%)$ and then curves upwards for far
out-of-the-money strikes $(K\gg x)$. Other qualitative features of
IV from stock options are that it is higher than historical
volatility and is often decreasing with time to maturity.

\begin{remark}
Smirk tells us that there is a premium
charged for out-of-the-money put options and in-the-money calls
above their BS price computed with the at-the-money IV.
\end{remark}

Bounds of $\partial I/\partial K$ can be obtained by the
BS formula \eqref{eq:BS_call}. Since no-arbitrage assumption, call
prices must be decreasing with $K$. Thus, differentiating
\eqref{eq:implied_volatility} wrt $K$ gives
\begin{equation}
    \begin{split}
        \frac{\partial C^{obs}}{\partial K}&=
        \frac{\partial C_{BS}}{\partial K}+
        \frac{\partial C_{BS}}{\partial \sigma}
        \frac{\partial I}{\partial K}\leq 0, \\
        \Rightarrow\frac{\partial I}{\partial K}&\leq
        -\frac{\partial C_{BS}/\partial K}{\partial C_{BS}/\partial \sigma}.
    \end{split}
\end{equation}
Similarly for put options
\begin{equation}
    \begin{split}
        \frac{\partial P^{obs}}{\partial K}&=
        \frac{\partial P_{BS}}{\partial K}+
        \frac{\partial P_{BS}}{\partial \sigma}
        \frac{\partial I}{\partial K}\geq 0, \\
        \Rightarrow\frac{\partial I}{\partial K}&\geq
        -\frac{\partial P_{BS}/\partial K}{\partial P_{BS}/\partial \sigma}.
    \end{split}
\end{equation}

\subsection{Implied Deterministic Volatility}
One popular way to modify the lognormal model is to suppose that
volatility is a deterministic positive function of time and stock
price: $\sigma=\sigma(t,X_{t})$.

In the special case $\sigma(t,x)=\sigma(t)$, we can solve the SDE
\begin{equation}
    dX_{t}=rX_{t}dt+\sigma(t)X_{t}dW_{t}^{*}
\end{equation}
under EMM $P^{*}$ by the logarithmic transformation to obtain
\begin{equation}
    X_{T}=X_{t}\exp\left(r(T-t)-\frac{1}{2}
    \int_{t}^{T} \sigma^{2}(s) ds
    +\int_{t}^{T} \sigma(s) dW_{s}^{*}\right),
\end{equation}
so that $\log(X_{T}/X_{t}) \sim N\left((r-\frac{1}{2}\bar{\sigma^{2}})(T-t)),(\bar{\sigma^{2}}(T-t)\right)$,
where
\begin{equation}
    \bar{\sigma^{2}}=\frac{1}{T-t}\int_{t}^{T} \sigma^{2}(s) ds.
\end{equation}

Thus, the answer is still the BS formula with volatility parameter
$\sqrt{\bar{\sigma^{2}}}$, the root-mean-square volatility. In this
time-dependent case, there is no smile across strike prices. To
have a smile across strike prices, we need $\sigma$ to depend on $x$
and $t$ in this framework, which is the case of level-dependent
volatility.

There are many competing ways --- parametric or non-parametric ---
to estimate the volatility surface $\sigma(t,x)$ from traded option
prices. This is called "finding the implied deterministic
volatility." It has the advantage of preserving a complete market
model.

\subsection{Modelling Stochastic Volatility}
In "pure" SV models, the asset price $(X_{t})_{t\geq 0}$ satisfies
the SDE
\begin{equation}
\label{eq:stochastic_volatility_asset_price}
    dX_{t}=\mu X_{t}dt+\sigma_{t}X_{t}dW_{t},
\end{equation}
where $(\sigma_{t})_{t\geq 0}$ is called the volatility process.
It must satisfy some regularity conditions for the model to be
well-defined, but \textit{it does not have to be an It\^o process:
it can be a jump process, a Markov chain, etc}.

\begin{definition}
\textbf{(Mean Reversion)} Let us denote
\begin{equation}
    \sigma_{t}=f(Y_{t}),
\end{equation}
where $f$ is some positive function.
Then mean-reverting stochastic volatility means that the SDE for
$(Y_t)$ looks like
\begin{equation}
    dY_{t}=\alpha(m-Y_{t})dt+\cdots d\hat{Z}_{t},
\end{equation}
where $(\hat{Z}_{t})_{t\geq 0}$ is a BM correlated with $(W_t)$.
Here $\alpha$ is called the rate of mean-reversion and $m$ is the
long-run mean level of $Y_t$.
\end{definition}

\begin{definition}
\textbf{(Ornstein-Uhlenbeck process)} A stochastic process
satisfies the SDE
\begin{equation}
\label{eq:OU_process}
    dY_{t}=\alpha(m-Y_{t})dt+\beta d\hat{Z}_{t}.
\end{equation}
\end{definition}

\begin{remark}
OU process is a Gaussian process explicitly given in terms of its
starting value $y$ by
\begin{equation}
\label{eq:OU_solution}
    Y_{t}=m+(y-m)e^{-\alpha t}+\beta\int_{0}^{t} e^{-\alpha(t-s)} d\hat{Z}_{s},
\end{equation}
so that $Y_{t}\sim N(m+(y-m)e^{-\alpha t},\frac{\beta^{2}}{2\alpha}(1-e^{-2\alpha t}))$.
Its \textit{invariant distribution}, obtained as
$t\rightarrow\infty$, is $N(m,\frac{\beta}{2\alpha})$, which does
not depend on $y$.
\end{remark}

\begin{remark}
The second BM $(\hat{Z}_{t})$ is typically correlated with the BM
$(W_{t})$ driving \eqref{eq:stochastic_volatility_asset_price}. We
denote by $\rho\in[-1,1]$ the instantaneous correlation coefficient
defined by $d\langle W,\hat{Z}\rangle_{t}=\rho dt$. It is also
convenient to write
\begin{equation}
\label{eq:bm_relationship}
    \hat{Z}_{t}=\rho W_{t}+\sqrt{1-\rho^{2}}Z_{t},
\end{equation}
where $(Z_{t})$ is a standard BM independent of $(W_{t})$.
\end{remark}
Some facts and assumptions are:
\begin{itemize}
    \item It is often found from financial data that $\rho<0$, and there are
    economic arguments for a negative correlation or \textit{leverage effect}
    between stock price and volatility shocks.
    \item Notice the fat tails of stock-price distribution due to the
    random volatility. In particular, the negative correlation causes
    the tails to be asymmetric: the left tail is fatter.
    \item We assume $\rho$ a constant from now on for simplicity
    and because it is taken to be such in most practical situations.
    \item We still consider the underlying probability space
    $(\Omega,\mathcal{F},P)$, where now we can take
    $\Omega=C([0,\infty):\mathbb{R}^{2})$, the space of all continuous
    trajectories $(W_{t}(\omega),Z_{t}(\omega))=\omega(t)$ in $\mathbb{R}^{2}$.
    \item The filtration $(\mathcal{F}_{t})_{t\geq 0}$ represents the information
    on the two BMs.
\end{itemize}
Some common driving processes $(Y_{t})$ are:
\begin{enumerate}
    \item Log-Normal (LN) $dY_{t}=c_{1}Y_{t}dt+c_{2}Y_{t}d\hat{Z}_{t}$
    \item Ornstein-Uhlenbeck (OU) $dY_{t}=\alpha(m-Y_{t})dt+\beta d\hat{Z}_{t}$
    \item Feller or Cox-Ingersoll-Ross (CIR) $dY_{t}=\kappa(m'-Y_{t})dt+\nu\sqrt{Y_{t}}d\hat{Z}_{t}$
\end{enumerate}
Some models often studied in the literature are listed in Table
\ref{tab:sv_models}.

\begin{table}[h]
    \centering
    \caption{Models of Stochastic Volatility}
    \begin{tabular}{cccc}
        \hline\hline
        Authors & Correlation & $f(y)$ & Process \\
        \hline
        Hull-White & $\rho=0$ & $\sqrt{y}$ & LN \\
        Scott & $\rho=0$ & $\exp(y)$ & OU \\
        Stein-Stein & $\rho=0$ & $|y|$ & OU \\
        Ball-Roma & $\rho=0$ & $\sqrt{y}$ & CIR \\
        Heston & $\rho\neq 0$ & $\sqrt{y}$ & CIR \\
        \hline\hline
    \end{tabular}
    \label{tab:sv_models}
\end{table}

\subsection{Derivative Pricing}\label{sec:sv_BS}
When the volatility is a \textit{Markovian It\^o process}, we can
find a pricing function for European derivatives of the form
$P(t,X_{t},Y_{t})$ from no-arbitrage arguments, as in the BS case.
We will derive the pricing PDE.\footnote{This PDE is not solved here.}
Assuming that volatility is a function of a mean-reverting OU
process:
\begin{equation}\label{eq:SV_SDE_0}
    \begin{split}
        dX_{t}&=\mu X_{t}dt+\sigma_{t} X_{t}dW_{t}, \\
        \sigma_{t}&=f(Y_{t}), \\
        dY_{t}&=\alpha(m-Y_{t})dt+\beta d\hat{Z}_{t}, \\
        \hat{Z}_{t}&=\rho W_{t}+\sqrt{1-\rho^{2}}Z_{t},
    \end{split}
\end{equation}
where $W_{t}$ and $Z_{t}$ are independent BMs.

Unlike the BS case, it is not sufficient to hedge solely with the
underlying asset, since there are two Wiener processes. Thus, we
try to hedge with the underlying asset and \textit{another option}
has a different expiration date. Let $P^{(1)}$ be the price of a
European derivative with expiration date $T_1$ and payoff function
$h$, $P^{(2)}$ be the price of a European contract with
$T_{2}>T_{1}>t$ and $h$. We try to find processes
$\{a_{t},b_{t},c_{t}\}$ s.t.
\begin{equation}
\label{eq:hedging_eq_sv_0}
    P^{(1)}(T_{1},X_{T_{1}},Y_{T_{1}})=a_{T_{1}}X_{T_{1}}
    +b_{T_{1}}\beta_{T_{1}}+c_{T_{1}}P^{(2)}(T_{1},X_{T_{1}},Y_{T_{1}}),
\end{equation}
where $\beta_{t}=e^{rt}$ is the price of a risk-free bond under
the prevailing short-term constant interest rate $r$. Also, the
portfolio is to be self-financing, so that
\begin{equation}
\label{eq:self_financing_sv}
    dP^{(1)}=a_{t}dX_{t}+b_{t}re^{rt}dt+c_{t}dP^{(2)}.
\end{equation}

If such a portfolio can be found then, in order for there to be
no-arbitrage opportunities, it must be that
\begin{equation}
\label{eq:hedging_eq_sv}
    P^{(1)}(t,X_{t},Y_{t})=a_{t}X_{t}+b_{t}\beta_{t}
    +c_{t}P^{(2)}(t,X_{t},Y_{t}),\quad \forall\;t<T_{1}.
\end{equation}

\begin{theorem}
(Two-Dimensional Version of It\^o's Formula)
\begin{equation}
\label{eq:ito_formula_2dim}
    dg(t,X_{t},Y_{t})=\frac{\partial g}{\partial t}dt+\frac{\partial g}{\partial x}dX_{t}
    +\frac{\partial g}{\partial y}dY_{t}+\frac{1}{2}
    \left(\frac{\partial^{2} g}{\partial {x}^{2}}d\langle X\rangle_{t}
    +2\frac{\partial^{2} g}{\partial x\partial y}d\langle X,Y\rangle_{t}
    +\frac{\partial^{2} g}{\partial {y}^{2}}d\langle Y\rangle_{t}\right).
\end{equation}
\end{theorem}

Applying this to both sides of \eqref{eq:self_financing_sv} yields
\begin{equation}
    \label{eq:ou_sv_0}
    \begin{split}
        &\left(\frac{\partial P^{(1)}}{\partial t}+\mathcal{M}_{1}P^{(1)}\right)dt+
        \frac{\partial P^{(1)}}{\partial x}dX_{t}+\frac{\partial P^{(1)}}{\partial y}dY_{t} \\
        &=\left(a_{t}+c_{t}\frac{\partial P^{(2)}}{\partial x}\right)dX_{t}
        +c_{t}\frac{\partial P^{(2)}}{\partial y}dY_{t}
        +\left[c_{t}\left(\frac{\partial }{\partial t}+\mathcal{M}_{1}\right)P^{(2)}+b_{t}re^{rt}\right]dt,
    \end{split}
\end{equation}
where
\begin{equation}
    \mathcal{M}_{1}=\frac{1}{2}f(y)^{2}x^{2}\frac{\partial^{2} }{\partial {x}^{2}}
    +\rho\beta xf(y)\frac{\partial^{2} }{\partial x\partial y}
    +\frac{1}{2}\beta^{2}\frac{\partial^{2} }{\partial {y}^{2}}.
\end{equation}
Equating $dY_{t}, dX_{t}$ and using \eqref{eq:hedging_eq_sv},
which gives
\begin{equation}
    \begin{split}
        c_{t}&=\frac{\partial P^{(1)}/\partial y}{\partial P^{(2)}/\partial y}, \\
        a_{t}&=\frac{\partial P^{(1)}}{\partial x}-c_{t}\frac{\partial P^{(2)}}{\partial x}, \\
        b_{t}&=(P_{t}^{(1)}-a_{t}X_{t}-c_{t}P^{(2)})e^{-rt}.
    \end{split}
\end{equation}
Comparing $dt$ terms in \eqref{eq:ou_sv_0} gives
\begin{equation}\label{eq:eliminate_dt_sv}
    \left(\frac{\partial P^{(1)}}{\partial y}\right)^{-1}
    \mathcal{M}_{2}P^{(1)}(t,X_{t},Y_{t})
    =\left(\frac{\partial P^{(2)}}{\partial y}\right)^{-1}
    \mathcal{M}_{2}P^{(2)}(t,X_{t},Y_{t}),
\end{equation}
where
\begin{equation}
    \mathcal{M}_{2}=\frac{\partial }{\partial t}+\mathcal{M}_{1}
    +r\left(x\frac{\partial }{\partial x}-\cdot\right).
\end{equation}

Now, the left-hand side of \eqref{eq:eliminate_dt_sv} contains
terms depending on $T_{1}$ but not $T_{2}$ and vice versa for the
right-hand side. Thus, both sides must be equal to a function that
does not depend on expiration date $T_{1},\,T_{2}$. We denote this
function by
\begin{equation}
    -\left(\alpha(m-y)-\beta\left(\rho\frac{\mu-r}{f(y)}
    +\gamma(t,x,y)\sqrt{1-\rho^{2}}\right)\right)
\end{equation}
where $\gamma(t,x,y)$ is an arbitrary function.\footnote{The
original idea was to guess the form.} Thus, the pricing function
$P(t,x,y)$ must satisfy

\begin{definition}
\textbf{(Kolmogorov or Feynman-Kac PDE)}
\begin{equation}
\label{eq:sv_pde}
    \begin{split}
        \frac{\partial P}{\partial t}+\frac{1}{2}f(y)^{2}x^{2}\frac{\partial^{2} P}{\partial {x}^{2}}
        +\rho\beta xf(y)\frac{\partial^{2} P}{\partial x\partial y}
        +\frac{1}{2}\beta^{2}\frac{\partial^{2} P}{\partial {y}^{2}} \\
        +r\left(x\frac{\partial P}{\partial x}-P\right)
        +[(\alpha(m-y)-\beta\Lambda(t,x,y))]\frac{\partial P}{\partial y}=0
    \end{split}
\end{equation}
where
\begin{equation}
\label{eq:sv_pde_Lambda}
    \Lambda(t,x,y)=\rho\frac{\mu-r}{f(y)}+\gamma(t,x,y)\sqrt{1-\rho^{2}}.
\end{equation}
The terminal condition is $P(T,x,y)=h(x)$.
\end{definition}

\begin{remark}
The function $\gamma$ in \eqref{eq:sv_pde_Lambda} is the
\textit{risk premium factor} from the second source of randomness
$Z_{t}$ in \eqref{eq:bm_relationship} that drives the volatility.
In the perfectly correlated case $|\rho|=1$ it does not appear,
as expected. The reason for this terminology is the calculation
\begin{equation}
    \begin{split}
        dP(t,X_{t},Y_{t})=&\left[\underline{\frac{\mu-r}{f(y)}}
        \left(
        xf(y)\frac{\partial P}{\partial x}
        +\underline{\beta\rho\frac{\partial P}{\partial y}}
        \right)+rP+\underline{\gamma\beta
        \sqrt{1-\rho^{2}}\frac{\partial P}{\partial y}}
        \right]dt \\
        &+\left(xf(y)\frac{\partial P}{\partial x}
        +\beta\rho\frac{\partial P}{\partial y}\right)dW_{t}
        +\beta\sqrt{1-\rho^{2}}\frac{\partial P}{\partial y}dZ_{t},
    \end{split}
\end{equation}
which is obtained by the It\^o's formula \eqref{eq:ito_formula_2dim}
and the PDE \eqref{eq:sv_pde} satisfied by $P$. From this expression
we see that an infinitesimal fractional increase in the volatility
risk $\beta$ increases the infinitesimal rate of return on the option
by $\gamma$ times that fraction, in addition to the increase from
the excess return-to-risk ratio $(\mu-r)/f(y)$.
\end{remark}

\subsection{Pricing with Equivalent Martingale Measures}
\label{sec:sv_emm}

We give an alternative derivation of the no-arbitrage derivative
price using \textit{risk-neutral} theory, again for the model
\eqref{eq:SV_SDE_0}, but the procedure is valid for
\textit{general models}, including non-Markovian ones.

Suppose that there is a an EMM $P^{*}$ under which the discounted
stock price $\widetilde{X}_{t}=e^{-rt}X_{t}$ is a martingale. Thus,
we can price any derivative with expiration $T$ and payoff $h$ by
\begin{equation}
    V_{t}=E^{*}[e^{-r(T-t)}H|\mathcal{F}_{t}]\quad \forall\;t\leq T,
\end{equation}
where $H=h(X_{T})$, then there is no-arbitrage opportunity (See
Section \ref{sec:risk_neutral_BS}).

We absorb the drift term of $\widetilde{X}_{t}$ in its martingale
term by setting
\begin{equation}
    W_{t}^{*}=W_{t}+\int_{0}^{t} \frac{\mu-r}{f(Y_{s})} ds.
\end{equation}

Any shift of the \textit{second independent BM} of the form
\begin{equation}
    Z_{t}^{*}=Z_{t}+\int_{0}^{t} \gamma_{s} ds
\end{equation}
will not change the drift of $\widehat{X}_{t}$. Here $(\gamma_{t})$
is any adapted (and suitably regular) process. By \textit{
Girsanov's theorem}\footnote{See Remark \ref{Girsanov_TH}.},
$(W^{*})$ and $(Z^{*})$ are independent standard BMs under a
measure $P^{*(\gamma)}$ defined by\footnote{The first equation is
Radon-Nikodym derivative mentioned in Remark \ref{Radon_Nikodym}.}
\begin{equation}
    \begin{split}
        \frac{d P^{*(\gamma)}}{d P}&=\exp
        \left\{-\int_{0}^{T} \theta_{t}^{(1)} dW_{t}
        -\int_{0}^{T} \theta_{t}^{(2)} dZ_{t}
        -\frac{1}{2}
        \int_{0}^{T} [(\theta_{t}^{(1)})^{2}+
        (\theta_{t}^{(2)})^{2}] dt\right\}, \\
        \theta_{t}^{(1)}&=\frac{\mu-r}{f(Y_{t})},\quad
        \theta_{t}^{(2)}=\gamma_{t}.
    \end{split}
\end{equation}

Technically, we shall make an assumption on the pair
$(\theta_{t}^{(1)},\theta_{t}^{(2)})$ so that $P^{*(\gamma)}$ is
well-defined as a probability measure.\footnote{For Example, Heston
model needs the Feller condition for the existence of ELMM, detailed
in Section 3 of Wong2006.pdf and arxiv20191230.pdf.} In particular,
this will be the case if $f$ is bounded away from zero and
$(\gamma_{t})$ is bounded. Then, under $P^{*(\gamma)}$, the SDEs
\eqref{eq:SV_SDE_0} become
\begin{equation}
    \begin{split}
        dX_{t}&=rX_{t}dt+f(Y_{t})X_{t}dW_{t}^{*}, \\
        dY_{t}&=\left[\alpha(m-Y_{t})-\beta\left(\rho
        \frac{\mu-r}{f(Y_{t})}+\gamma_{t}\sqrt{1-\rho^{2}}
        \right)\right]dt+\beta d\hat{Z}_{t}^{*}, \\
        \hat{Z}_{t}^{*}&=\rho W_{t}^{*}+\sqrt{1-\rho^{2}}Z_{t}^{*}.
    \end{split}
\end{equation}

Any allowable choice of $\gamma$ leads to an EMM $P^{*(\gamma)}$
and the possible no-arbitrage prices are\footnote{This expectation can
be obtained by \eqref{eq:EMM_expectation}.}
\begin{equation}\label{eq:sv_risk_neutral_valuation}
    V_{t}=E^{*(\gamma)}[e^{-r(T-t)}H|\mathcal{F}_{t}].
\end{equation}

The process $(\gamma_{t})$ is called the risk premium factor or the
\textit{market price of volatility risk} from the second source of
randomness $Z$ that drives the volatility. As a result, the
\textit{martingale representation theorem}
\ref{th:martingale_representation} says that under $P^{*(\gamma)}$
martingale $M_{t}=e^{-rt}V_{t}$ is a stochastic integral
wrt $(W^{*},Z^{*})$:
\begin{equation}\label{eq:martingale_representation_sv}
    M_{t}=M_{0}+\int_{0}^{t} \eta_{s}^{(1)} dW_{s}^{*}
    +\int_{0}^{t} \eta_s^{(2)} dZ^{*}_{s}
\end{equation}
for some adapted and suitably bounded processes $(\eta_{t}^{(1)})$
and $(\eta_{t}^{(2)})$. So, we cannot replicate the claim by
trading in stock and bond only due to the last integral in
\eqref{eq:martingale_representation_sv}.\footnote{Q: How about VIX
options? If we trade volatility derivatives, is there any difference?}
We can, however, hedge one derivative contract $P^{(1)}$ with the
stock and another derivative security $P^{(2)}$. The calculation
is like the Markovian case in Section \ref{sec:sv_BS}, but the
hedging ratios are \textit{non-unique} since they depend on
$\gamma$. This procedure is usually unsatisfactory owing to the
\textit{higher transaction costs} and \textit{less liquidity}
associated with trading the second derivative.

\subsection{Implied Volatility as a Function of Moneyness}
Another reason that IV is a particularly useful measure of the
performance of a SV model is that IV is a function of a European
option contract's \textit{moneyness} $K/x$. Given any Markovian SV model under
which the stock price satisfies
\begin{equation}\label{eq:stock_sde_markovian_sv}
    dX_{s}=rX_{s}ds+\sigma_{s}X_{s}dW_{s}^{*}
\end{equation}
under some risk-neutral measure $P^{*(\gamma)}$, suppose it is now
time $t,X_{t}=x$ and define $\widetilde{X}=X/x$. Then
$(\widetilde{X}_{s})_{s\geq t}$ satisfies the same SDE
\eqref{eq:stock_sde_markovian_sv}, with initial value
$\widetilde{X}_{t}=1$, neither of which depends on $x$. The call
option price
\begin{equation}
    \begin{split}
        C&=E^{*(\gamma)}[e^{-r(T-t)}(X_{T}-K)^{+}|X_{t}=x,
        \sigma_{t}] \\
        &=E^{*(\gamma)}[e^{-r(T-t)}(x\widetilde{X}_{T}-K)^{+}
        |\widetilde{X}_{t}=1,\sigma_{t}] \\
        &=KE^{*(\gamma)}[e^{-r(T-t)}(\frac{x}{K}\widetilde{X}_{T}
        -1)^{+}|\widetilde{X}_{t}=1,\sigma_{t}] \\
        &=KQ_{1}(t,K/x;T)
    \end{split}
\end{equation}
for some function $Q_{1}$ depending on $K/x$ but not on $x$ and
$K$ separately. The IV $I$ is computed from
\begin{equation}
    C=C_{BS}(t,x;K,I),
\end{equation}
and from the BS-formula \eqref{eq:BS_call} we have
\begin{equation}
    C_{BS}(t,x;K,I)=KQ_{2}(t,K/x;I)
\end{equation}
for some function $Q_{2}$ also depending on $K/x$ but not on $x$
and $K$ separately. From the relation $KQ_{1}(t,K/x;T)=KQ_{2}(t,K/x;I)$
we see that $I$ must be a function of moneyness $K/x$ but not of
$K$ and $x$ separately.

This is useful because it tells us that we can obtain the implied
volatility curve predicted by a SV model by solving the PDE
\eqref{eq:sv_pde} with terminal condition $h(x)=(x-K)^{+}$ for
\textit{a fixed strike price} $K$. This is because plotting the
resulting implied volatilities as a function of moneyness for
different starting values $x$ gives the same curve as if we were
varying $K$.

\subsection{Market Price of Volatility Risk and Data}
Now, there are more details.

\textbf{Bound:} In Section \ref{sec:sv_emm}, we know that
\eqref{eq:sv_risk_neutral_valuation} is a possible no-arbitrage
derivative pricing formula for any EMM $P^{*(\gamma)}$. If the
volatility is unbounded, then the range of European call option
prices given by \eqref{eq:sv_risk_neutral_valuation} with
$H=(X_{T}-K)^{+}$ is between the price of stock and intrinsic value
of the contract, $(X_{T}-K)^{+}\leq V_{t}\leq X_{t},$ and the
extreme values are attained for some EMMs. When volatility is assumed
to be bounded with $\sigma_{t}\in[\sigma_{\mathrm{min}},\sigma_{\mathrm{max}}]$,
the bounds are $C_{BS}(\sigma_{\mathrm{min}})\leq V_{t}\leq C_{BS}(\sigma_{\mathrm{max}})$.

\textbf{Super-hedge:} The viewpoint, market selects a \textit{unique} EMM
under which derivative contracts are priced, is sometimes called
\textit{selecting an approximating complete market}. Although discounted
derivative prices cannot be replicated by stock and bond alone,
they can be \textit{super-replicated}: for example, buying the
stock at time $t<T$ and holding it until expiration
\textit{super-hedges} a short call position because
$X_{T}>(X_{T}-K)^{+}$; this may yield a profit but never a loss.
This (trivial) strategy is very expensive (it costs \$ $X_t$), so
many researches concern finding the cheapest super-hedging strategy.

\textbf{Parameter Estimation:} When we estimate the parameters for
our model, we could use econometric methods such as maximum
likelihood or method of moments on historical stock-price
time-series data to find $(\alpha,\beta,m,\rho)$ plus the present
volatility in the model \eqref{eq:SV_SDE_0}. Then we would
need some derivative data to estimate $\gamma$, assuming for
instance that it is constant. The common practice, called
\textit{cross-sectional fitting}, is to estimate all the
parameters from derivative data, usually \textit{at-the-money}
European option prices (or a section of the observed implied
volatility surface). This ignores the statistical basis for the
modeling but is easier to implement than time-series methods,
which suffer because the $(\sigma_{t})$ process is not directly
observable. If today is time $t=0$ and we denote
$\vartheta=(\alpha,\beta,m,\rho,\sigma_{0},\gamma,\mu)$,
then a typical \textit{least-square fit} is to observe call option
prices $C^{obs}(K,T)$ for $(K,T)$ in some set $\mathcal{K}$ and to
solve
\begin{equation}
    \min_{\vartheta} \sum_{(K,T)\in\mathcal{K}} (C(K,T;\vartheta)
    -C^{obs}(K,T))^{2},
\end{equation}
where $C(K,T;\vartheta)$ is the model-predicted call option price
(either from solving the PDE \eqref{eq:sv_pde} with
$h(x)=(x-K)^{+}$ or from Monte Carlo simulation). But this process
can be very slow and computationally intensive.

\newpage
\section{Heston Stochastic Volatility Model}\label{sec:Heston}
This section introduces the \textit{Heston model} which is used by
Steven L. Heston in 1993.\footnote{All researches mentioned in this
section can be found in the reference of Heston1993\_RFS.pdf.}
Heston used a new technique to derive a closed-form solution for
the price of European call option on an asset with SV. The model
allows arbitrary correlation between volatility and spot-asset
returns. He also shows that correlation is important for explaining
return skewness and strike-price biases in the BS model. The
solution technique is based on \textit{characteristic functions}.

\subsection{Stochastic Differential Equations}
Assuming that the spot asset price $S_t$ satisfies the diffusion
equation, the volatility follows the OU process, and It\^o's lemma
\eqref{eq:ito_BM} shows that the variance $v_{t}$ follows the CIR
process,
\begin{equation}
\label{eq:Heston_SDE}
\begin{split}
    dS_{t}&=\mu S_{t}dt+\sqrt{v_{t}}S_{t}dW_{t}, \\
    d\sqrt{v_{t}}&=-\beta\sqrt{v_{t}}dt+\delta d\hat{Z}_{t}, \\
    dv_{t}&=\kappa(\theta-v_{t})dt+\sigma\sqrt{v_{t}}d\hat{Z}_{t}, \\
    d\langle W,\hat{Z}\rangle_{t}&=\rho dt.
\end{split}
\end{equation}
where $W_{t},\hat{Z}_{t}$ are Wiener processes. Assume $\kappa,\theta,\sigma>0$
and $-1<\rho<1$. Also, we assume the variance process satisfies
the Feller condition $2\kappa\theta>\sigma^{2}$, so it is always
positive and cannot reach zero.

For simplicity, we assume a constant interest rate $r$. Therefore,
the price at time $t$ of a unit discount bond that matures at time
$t+\tau$ is
\begin{equation}
\label{eq:Heston_riskless}
    P(t,t+\tau)=e^{-r\tau}.
\end{equation}

\subsection{Heston Partial Differential Equation}
Standard arbitrage arguments demonstrate that the value of any
asset $U(S,v,t)$ must satisfy the PDE\footnote{This is Feynman-Kac
PDE derived in Section \ref{sec:sv_BS}.}
\begin{equation}
\label{eq:sv_pde_Heston}
\begin{split}
    \frac{1}{2}vS^{2} & \frac{\partial^{2} U}{\partial {S}^{2}}
    +\rho\sigma vS\frac{\partial^{2} U}{\partial {S} \partial {v}}
    +\frac{1}{2}\sigma^{2}v\frac{\partial^{2} U}{\partial {v}^{2}}+rS\frac{\partial U}{\partial S} \\
    & +[\kappa(\theta-v_{t})-\lambda(S,v,t)]\frac{\partial U}{\partial v}
    -rU+\frac{\partial U}{\partial t}=0.
\end{split}
\end{equation}

The unspecified term $\lambda(S,v,t)$ represents \textit{the price
of volatility risk}, and must be independent of the particular
asset. To motivate the choice of $\lambda(S,v,t)$, Heston noted
that in Breeden's (1979) consumption-based model,
\begin{equation}
\label{eq:volatility_risk_price}
    \lambda(S,v,t)dt=\gamma\,\mathrm{Cov}[dv,dC/C],
\end{equation}
where $C_{t}$ is the consumption rate and $\gamma$ is the
relative-risk aversion of an investor. Consider the consumption
process that emerges in the Cox, Ingersoll and Ross (1985) model
\begin{equation}
\label{eq:CIR_consumption}
    dC_{t}=\mu_{c}v_{t}C_{t}dt+\sigma_{c}\sqrt{v_{t}}C_{t}d\hat{Z}_{c,t},
\end{equation}
where $\hat{Z}_{c,t}$ is another Wiener process with the constant
correlations with $W_{t},\hat{Z}_{t}$. This generates a risk
premium proportional to $v$, i.e. $\lambda(S,v,t)=\lambda v$.

A European call option with strike price $K$ and maturing at time
$T$ satisfies the PDE \eqref{eq:sv_pde_Heston} subject to the
following boundary conditions:
\begin{equation}
    \label{eq:Heston_boundary_conditions}
    \begin{split}
        U(S,v,T)&=(S_{T}-K)^{+}, \\
        U(0,v,t)&=0, \\
        \frac{\partial U}{\partial S}(\infty,v,t)&=1, \\
        \left(rS\frac{\partial U}{\partial S}+\kappa\theta
        \frac{\partial U}{\partial v}-rU
        +\frac{\partial U}{\partial t}\right)(S,0,t)&=0, \\
        U(S,\infty,t)&=S_{t}.
    \end{split}
\end{equation}

\begin{remark}
Although we will use this form of the risk premium, the pricing
results are obtained by arbitrage and do not depend on the other
assumptions of the Breeden (1979) or CIR (1985) models.
\end{remark}

\subsection{The Call Option Price}
By analogy with the BS formula, we guess a solution of the form
\footnote{This is a corollary of Girsanov's Theorem.}
\begin{equation}
\label{eq:European_call_Heston}
    C(S,v,t)=S_{t}\,P_{1}-KP(t,T)\,P_{2},
\end{equation}
where the first term is the present value of the spot asset upon
optimal exercise, and the second term is the present value of the
strike-price payment. Both of these terms must satisfy the
original PDE \eqref{eq:sv_pde_Heston}. For notional convenience,
let
\begin{equation}
\label{eq:stock_log_Heston}
    x_{t}=\log(S_{t}).
\end{equation}
Substituting the proposed solution \eqref{eq:European_call_Heston}
into the original PDE \eqref{eq:sv_pde_Heston} shows that $P_1$ and
$P_2$ must satisfy the PDEs\footnote{Just set $S_{t}=1,\,K=0$ and
$S_{t}=0,\,K=-1/P(t,T)$.}
\begin{equation}\label{eq:heston_factors}
    \begin{split}
        \frac{1}{2}v & \frac{\partial^{2} P_{j}}{\partial {x}^{2}}
        +\rho\sigma v\frac{\partial^{2} P_{j}}{\partial {x} \partial {v}}
        +\frac{1}{2}\sigma^{2}v\frac{\partial^{2} P_{j}}{\partial {v}^{2}}
        +(r+u_{j}v)\frac{\partial P_{j}}{\partial x} \\
        & +(a-b_{j}v)\frac{\partial P_{j}}{\partial v}
        +\frac{\partial P_{j}}{\partial t}=0.
    \end{split}
\end{equation}
for $j=1,2$, where
\begin{equation}
    u_{1}=\frac{1}{2},\quad u_{2}=-\frac{1}{2},\quad
    a=\kappa\theta,\quad b_{1}=\kappa+\lambda-\rho\sigma,\quad
    b_{2}=\kappa+\lambda.
\end{equation}
For the option price to satisfy the terminal conditions in
\eqref{eq:Heston_boundary_conditions}, these PDEs
\eqref{eq:heston_factors} are subject to the terminal condition
\begin{equation}\label{eq:heston_discounters}
    P_{j}(x,v,T;K)=1_{\{x_{T}\geq\ln{K}\}}.
\end{equation}

\subsection{Risk-Neutral Valuation}\label{sec:heston_Riccati}
Following Heston, we know that $P_{1,2}$ are risk-neutral when
$x_{t},v_{t}$ satisfy respectively\footnote{In fact, we will
choose $j=2$ and set $\lambda=0$.}
\begin{equation}
    \label{eq:heston_EMM}
    \begin{split}
        dx_{t}&=(r+u_{j}v_{t})dt+\sqrt{v_{t}}dW^{*}_{t}, \\
        dv_{t}&=(a-b_{j}v_{t})dt+\sigma\sqrt{v_{t}}d\hat{Z}^{*}_{t}.
    \end{split}
\end{equation}
Then we obtain
\begin{equation}
    P_{j}(x,v,t;K)=E^{*}[1_{\{x_{T}\geq\ln{K}\}}|x_{t}=x,v_{t}=v].
\end{equation}

They are not immediately available in closed form. However, we know
that\footnote{There are details in the Appendix of Heston1993\_RFS.pdf.}
their characteristic functions satisfy the same PDEs
\eqref{eq:heston_factors}, subject to the terminal condition
under the risk-neutral measure
\begin{equation}
    \Phi_{j}(x,v,T;u)=e^{iux}.
\end{equation}

To obtain the solution, we guess the form of characteristic functions
\footnote{This exploits the linearity of the coefficients in the
PDE \eqref{eq:heston_factors}.}
\begin{equation}
    \Phi_{j}(\tau,u)=E^{*}\left[e^{iux_{T}}|x_{t}=x,v_{t}=v\right]
    =\exp[{C_{j}(\tau,u)+D_{j}(\tau,u)v+iux}],
\end{equation}
where $\tau=T-t$. Substituting these into \eqref{eq:heston_factors}
shows a \textit{Riccati equation} and a straightforward ODE
\begin{equation}
    \begin{split}
        \frac{dD_j}{d\tau}&=\rho\sigma iuD_{j}-\frac{1}{2}u^{2}
        +\frac{1}{2}\sigma^{2}D_{j}^{2}+u_{j}iu-b_{j}D_{j}, \\
        \frac{dC_j}{d\tau}&=riu+aD_{j}.
    \end{split}
\end{equation}
Heston specifies the initial conditions $C_{j}(0,u)=D_{j}(0,u)=0$.
Then the results are
\begin{equation}
    \begin{split}
        D_{j}(\tau,u)&=\frac{b_{j}-\rho\sigma iu+d_{j}}{\sigma^{2}}
        \left(\frac{1-e^{d_{j}\tau}}{1-g_{j}e^{d_{j}\tau}}\right), \\
        C_{j}(\tau,u)&=riu\tau+\frac{a}{\sigma^{2}}\left[(b_{j}-
        \rho\sigma iu+d_{j})\tau-2\ln\left(\frac{1-g_{j}e^{d_{j}\tau}}{1-g_{j}}
        \right)\right],
    \end{split}
\end{equation}
where
\begin{equation}
    \begin{split}
        d_{j}&=\sqrt{(\rho\sigma iu-b_{j})^{2}-\sigma^{2}(2u_{j}iu-u^{2})}, \\
        g_{j}&=\frac{b_{j}-\rho\sigma iu+d_{j}}{b_{j}-\rho\sigma iu-d_{j}}.
    \end{split}
\end{equation}

\subsection{The Heston Pricing Formula}
Through some calculations, we find that
\begin{equation}
    P(t,T)\Phi_{2}(\tau,u)=e^{-r(T-t)}\Phi_{2}(\tau,u)
    =e^{x}\Phi_{1}(\tau,u+i)=S_{t}\Phi_{1}(\tau,u+i).
\end{equation}
Also, one can invert the characteristic functions to get cdf and
\eqref{eq:heston_discounters} becomes
\begin{equation}
\begin{split}
    P_{j}&=1-F^{*}_{j}(\ln K)=\frac{1}{2\pi}\lim_{y\rightarrow+\infty}
    \lim_{L\rightarrow\infty}\int_{-L}^{L} \frac{e^{-iu\ln K}-e^{-iuy}}{iu}\Phi_{j} du \\
    &=\frac{1}{2\pi}\lim_{y\rightarrow+\infty}\lim_{L\rightarrow\infty}
    \int_{-L}^{L} \frac{1-e^{-iuy}}{iu}\Phi_{j} du+\frac{1}{2\pi}
    \lim_{L\rightarrow\infty}\int_{-L}^{L} \frac{e^{-iu\ln K}-1}{iu}\Phi_{j} du \\
    &=\frac{1}{2}+\frac{1}{\pi}\int_{0}^{\infty} \Re
    \left(\frac{e^{-iu\ln K}\Phi_{j}}{iu}\right) du.
\end{split}
\end{equation}
The third equal sign adds 1 on the numerators so the integrands
can be finite at $u=0$. The last equal sign is based on characteristic
function's properties $\Phi(0)=1,\,\Phi(u)=\overline{\Phi(-u)}$
and a useful integral
\begin{equation}
    \int_{0}^{+\infty} \frac{\sin\alpha x}{x} dx=
    \begin{cases}
        \frac{\pi}{2} & \alpha>0 \\
        0 & \alpha=0 \\
        -\frac{\pi}{2} & \alpha<0
    \end{cases}
\end{equation}

Finally, we can obtain the European call option price
\begin{equation}
\label{eq:heston_call_formula}
    C(S,v,t;K,T,r)=P(t,T)\left[\frac{1}{2}(F_{t}-K)+\frac{1}{\pi}
    \int_{0}^{\infty} \left(F_{t}\cdot f_{1}-K\cdot f_{2}\right) du\right]
\end{equation}
where $F_{t}=\frac{S_{t}}{P(t,T)}=S_{t}e^{r(T-t)}$ and
\begin{equation}
    \begin{split}
        f_{1}&=\Re\left(\frac{e^{-iu\ln K}\varphi(u-i)}{iuF_{t}}\right), \\
        f_{2}&=\Re\left(\frac{e^{-iu\ln K}\varphi(u)}{iu}\right), \\
        \varphi(u)&=\Phi_{2}(\tau;u)=\exp({C(\tau,u)+D(\tau,u)v+iu\ln S_{t}}), \\
        C(\tau,u)&=\frac{\kappa\theta}{\sigma^{2}}\left((\kappa-i\rho\sigma u+d(u))
        \tau-2\ln\left(\frac{1-g(u)e^{d(u)\tau}}{1-g(u)}\right)\right), \\
        D(\tau,u)&=\frac{\kappa-i\rho\sigma u+d(u)}{\sigma^{2}}
        \left(\frac{1-e^{d(u)\tau}}{1-g(u)e^{d(u)\tau}}\right), \\
        d(u)&=\sqrt{(\kappa-i\rho\sigma u)^{2}+\sigma^{2}(u^{2}+iu)}, \\
        g(u)&=\frac{\kappa-i\rho\sigma u+d(u)}{\kappa-i\rho\sigma u-d(u)}.
    \end{split}
\end{equation}

\begin{remark}
There are mainly two troubles when doing numerical quadrature in
\eqref{eq:heston_call_formula}. One is that the Heston factors
$f_{1,2}$ are usually oscillatory. The other difficulty is the
calculations of complex logarithm and square root, which can result
in numerical instability.\footnote{There are discussions in
HestonTrap.pdf and NotSoComplexLogarithmsInTheHestonModel.pdf.}
\end{remark}

\begin{remark}
The put option price can be obtained by put-call parity
\eqref{eq:call_put_parity}.
\end{remark}

\subsection{Finite Difference Schemes}

\subsection{Numerical Integration}
Gauss-Kronrod quadrature

\newpage
\section{Heston-Nandi GARCH Model}
This section introduce Heston's further research in 2000.\footnote{
All reference in this section can be found in Heston2000\_RFS.pdf}
In their paper, Heston and Nandi develops a closed-form option
valuation formula for a spot asset whose variance follows a
Generalized Auto-regressive Conditional Heteroskedasticity (GARCH)
process that can be correlated with the returns of the spot asset.
It provides the first readily computed option formula for a random
volatility model that can be estimated and implemented solely on
the basis of observables. They also show that the improvement,
to \textit{ad hoc} BS model, is largely due to the ability
of the GARCH model to simultaneously capture the correlation of
volatility with spot returns and the path dependence in volatility.

\subsection{Time Series Model}
Under the physical measure $P$, the discertized form of SDE
\eqref{eq:Heston_SDE} is a GARCH$(1,1)$ process, which looks
like Engle and Ng's NGARCH rather than Duan's classic
GARCH. We assume the variance process follows GARCH$(p,q)$:
\begin{equation}
\label{eq:heston_GARCH}
\begin{split}
    r_{t}&=\ln\left(\frac{S_{t}}{S_{t-1}}\right)
    =r+\lambda v_{t}+\sqrt{v_{t}}z_{t}, \\
    v_{t}&=w+\sum_{i=1}^{p}b_{i}v_{t-i}
    +\sum_{j=1}^{q}a_{j}(z_{t-j}-c_{j}\sqrt{v_{t-j}})^{2} \\
    &=w+\sum_{i=1}^{p}b_{i}v_{t-i}+\sum_{j=1}^{q}a_{j}
    \frac{(r_{t-j}-r-\lambda v_{t-j}-c_{j}v_{t-j})^{2}}{v_{t-j}},
\end{split}
\end{equation}
where $S_{t}$ is the underlying asset price at time $t$, $r$ is
the continuously compounded risk-free rate, $v_{t}$ is the
conditional variance of the log return between $t-1$ and $t$ and
is known at time $t-1$, $\lambda v_{t}$ is \textit{equity risk
premium}, $z_{t}$ is a standard BM.

At this point we cannot value options or other contingent because
we do not know the risk-neutral distribution of the spot price.
Thus, we rearrange SDE \eqref{eq:heston_GARCH} in the form
\begin{equation}\label{eq:heston_GARCH_emm}
    \begin{split}
        r_{t}&=r-\frac{1}{2}v_{t}+\sqrt{v_{t}}z^{*}_{t}, \\
        v_{t}&=w+\sum_{i=1}^{p}b_{i}v_{t-i}+\sum_{j=2}^{q}a_{j}
        (z_{t-j}-c_{j}\sqrt{v_{t-j}})^{2}+a_{1}(z^{*}_{t-1}-
        c^{*}_{1}\sqrt{v_{t-1}})^{2},
    \end{split}
\end{equation}
where $z^{*}_{t}=z_{t}+(\lambda+1/2)\sqrt{v_{t}}$ and
$c_{1}^{*}=c_{1}+\lambda+1/2$.

There is no reason for the risk-neutral distribution of $z^{*}_{t}$
to be normal because BS price do not follow absence of arbitrage
with discrete-time trading. In order for $z^{*}_{t}$ to have a
standard normal risk-neutral distribution, we need a assumption:
\textit{the value of a call option with one period to expiration
obeys the Black-Scholes-Rubinstein formula}.

\begin{remark}
Following Heston and Nandi, we will focus on the first-order, i.e.
$p=q=1$. The first-order process remains stationary with finite
mean and variance if $b_{1}+a_{1}c_{1}^{2}<1$. In the multiple
factor case, one must add a condition that the roots of
$x^{p}-\sum_{i=1}^{p}(b_{i}+a_{i}c_{i}^{2})x^{p-i}$ lie inside the
unit circle.
\end{remark}

\subsection{Estimation of Model Parameters}
Typically, we use GARCH$(1,1)$, which converges to continuous time
Heston's SV model, when we deal with the short term data, like
daily data. Thus, we need to estimate five parameters
$(a,b,c,w,\lambda)$. We do this with the Maximum Likelihood
Estimation (MLE) used by Bollerslev (1986) and many others.
\footnote{Q: How to solve? GD or manually?} Since
$z_{t}\sim N(0,1)$, the likelihood function is
\begin{equation}
    L(a,b,c,w,\lambda;r_{t})
    =\prod_{t=1}^{T}\frac{1}{\sqrt{2\pi v_{t}}}\exp\left[
        -\frac{(r_{t}-r-\lambda v_{t})^{2}}{2v_{t}}\right].
\end{equation}
Then the log-likelihood function is
\begin{equation}
    \ln L=-\frac{1}{2}\sum_{t=1}^{T}\left[\ln(2\pi v_{t})
    +\frac{(r_{t}-r-\lambda v_{t})^{2}}{v_{t}}\right],
\end{equation}
where
\begin{equation}
    v_{t}=w+bv_{t-1}+a
    \frac{(r_{t-1}-r-\lambda v_{t-1}-cv_{t-1})^{2}}{v_{t-1}}.
\end{equation}

\begin{remark}
Following Heston and Nandi, we assume that $v_{1}$ equals to the
sample variance of $r_t$. Due to strong mean reversion of
volatility, all results should be insensitive to the starting value
$v_{1}$.
\end{remark}

\subsection{Call Option Pricing}
There are mainly two methods about pricing the call option after
we get the parameters in \eqref{eq:heston_GARCH}. The first one
is Monte Carlo Simulation under EMM, which will be slow and
computationally intensive for empirical work. In contrast, we can
use the Heston model's analytic formula introduced in Section
\ref{sec:Heston}.

Let $f(u)$ be the moment generating function of the log asset price
under the physical measure. Following the derivation in Section
\ref{sec:heston_Riccati}, we can get
\begin{equation}
    f(u)=E_{t}[e^{u\ln S_{T}}]=E_{t}[S_{T}]
    =S_{t}^{u}\exp(A_{t}+B_{t}v_{t+1}).
\end{equation}

The two coefficients can be calculated recursively, by working
backward from the maturity date and using the terminal conditions
\footnote{The details are in the Appendix A of Heston\_2000.pdf.}
\begin{equation}
    \begin{split}
        A_{t}&=A_{t+1}+ur+B_{t+1}w-\frac{1}{2}\ln (1-2aB_{t+1}), \\
        B_{t}&= u(\lambda+c)-\frac{1}{2}c^{2}+bB_{t+1}
        +\frac{(u-c)^{2}}{2(1-2aB_{t+1})}, \\
        A_{T}&=B_{T}=0.
    \end{split}
\end{equation}

We can rewrite the closed-form formula
\eqref{eq:heston_call_formula} as followed:
\begin{equation}
\label{eq:heston_call_formula_simplified}
\begin{split}
    C=\frac{1}{2}S_{t}&+\frac{e^{-r(T-t)}}{\pi}
    \int_{0}^{\infty} \Re\left[\frac{K^{-iu}f^{*}(iu+1)}{iu}
    \right] du \\
    &-Ke^{-r(T-t)}\left\{\frac{1}{2}+\frac{1}{\pi}
    \int_{0}^{\infty} \Re\left[\frac{K^{-iu}f^{*}(iu)}{iu}
    \right] du\right\},
\end{split}
\end{equation}
where $f^{*}(iu)$ is the conditional characteristic function of
the log asset price under the EMM.

\begin{remark}
Observing the GARCH forms in \eqref{eq:heston_GARCH} and
\eqref{eq:heston_GARCH_emm}, we find the similarity if $p=q=1$.
Since we have the formula of $f(u)$, the calculation of $f^{*}(iu)$
only needs to replace $\lambda$ with $-1/2$ and $c$ with
$c+\lambda+1/2$.
\end{remark}

\end{document}


\documentclass{article}
\author{Chang Liu}
\title{Introduction to ML for AI \- Notes}

\begin{document}

\maketitle

\section{Introduction}
This document is used for recording my learning for this article.

\section{Content}

\subsection{Formation of learning}
\textbf{Target}: for the samples from unknown process $P(Z)$, we have got some samples $D=\{z_{1}, z_{2}, z_{3}, ... z_{n}\}$, now that we want to find this process $P(Z)$.\newline

\noindent \textbf{Method}: here we could first assume an loss function $L$, that takes parameters of \textbf{decision function} and \textbf{these samples $z_{i}$}, the decision function is what we assumed before like linear function or quadratic function, samples are the sets $D$. By minimizing the loss function $L(f, D)$, we can get the unknown variables $A, B...$ in the loss function and get its exact representation.\newline

\noindent \textbf{Result}: the loss function $L$ is just our assumed process $P(Z)$, we can assume many loss function and find the best one among them.

\subsection{Supervised learning}
For supervised learning, in the above section, we can know that there are two situations for that, that is:

1) Regression.

2) Classification.

\subsubsection{Regression}
In this category, let's assume that $Y$ is the real-valued scalar or vector, then the output of function $f$(here we should recall that $f$ is the decision function) should also be in the same set of $Y$, which means its value should be in the set $Y$.

Then loss function should be :

$$L(f, (X,Y)) = ||f(X) - Y||$$

\subsubsection{Classification}
Classification is different, as the output $Y$ is finite integer(or symbol) corresponding to the index, and the \textbf{loss function} should be the \textbf{negative} \emph{log-likelihood} value,

$$f_i(X) = P(Y = i | X)$$

And the loss function should be:

$$L(f, (X,Y)) = -\log{f_Y(X)}$$

\textbf{NOTE}: the decision function $f$ must have the constrain that:

$$\sum_i{f_i(X)} = 1, f_i(X) \geq 0$$

\subsubsection{Summary}
From the above two kinds of \textbf{supervised learning}, I know that for regression since we can get continual value, so the loss function is to calculate the \textbf{absolute distance} between the decision function and real value.

But for classification problem, since it's the classify label or index, we cannot function, so here we use the likelihood, and to use the \emph{sigmoid} function like the $\log{X}$ function. And all the samples forms the whole training set, so the constrain is also given as above.

\subsection{Unsupervised learning}
In unsupervised learning, the learning function $f$ is used to characterize the unknown distribution function $P(Z)$, this function $f$ is may have the following categories:

1) some is just the direct represent of the $P(Z)$, which is called density estimation.

2) some is to characterize where the density concentrates.

Clustering: clustering algorithm is used to divide the space into regions, that have two types:

1) hard partition. (k-means)

2) soft partition. (Gaussian mixed model)

3) form a new representation for $Z$. (Principle Component Analysis)

\subsection{Local generalization}
Defintion: when $x_i$ is close to $x_j$, then the $f(x_i)$ should also be close to $f(x_j)$.

1) When the target unknown function is more than the input parameters $X$, then it's hard to generalize since there are so many variables, \textbf{curse of dimension}.

2) for example, there are $n$ variables with 10 different value, then it has $10^{n}$ configurations.

\end{document}


\section{Biconnectivity}

%%%%%%%%%%%%%%%
\begin{frame}{Biconnectivity algorithm}
  \begin{center}
    $G$ is biconnected $\iff$ $G$ has no articulation vertex.

    $v$ is an articulation vertex $\iff$ $\exists x,y \in V: \forall p \equiv x \sim y, x \sim v \sim y$.

    Biconnectivity algorithm by Tarjan\&Hopcroft, 1971
  \end{center}

  \begin{exampleblock}{Root of DFS tree as an articulation vertex \pno{3.4.8}}
    DFS on undirected graph $G$ $\Rightarrow$ DFS tree $T$:
    \begin{itemize}
      \item leaf node
      \item root node $r$: $\text{out}[r] \ge 2$
	\begin{proof}
	  \begin{itemize}
	    \item $\Leftarrow$: ($G = $ tree + back); delete $r$; disconnect subtrees
	    \item $\Rightarrow$: By contradiction. $\text{out}[r] = 1 \Rightarrow r$ is not an articulation vertex ($\forall x,y$).
	  \end{itemize}
	\end{proof}
      \item internal node
    \end{itemize}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Biconnectivity algorithm}
  \begin{exampleblock}{Articulation checking \pno{3.4.10}}
    DFS on undirected graph $G$ $\Rightarrow$ back edges:
    \begin{center}
      $v$ is not an articulation vertex $\iff \textcolor{red}{\forall} \text{subtree } T_{v} \text{ of } v, \text{back}[T_{v}] = v.\text{ancestor}$

      $v$ is an articulation vertex $\iff \textcolor{red}{\exists} T_{v} \text{ of } v, \text{back}[T_{v}] = v \lor v.\text{descendant}$
    \end{center}

    $\exists$ $\Rightarrow$ checking articulation ($\text{wBack} \ge \text{d}[v]$) when backtracking from $w$ to $v$
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Biconnectivity algorithm}
  \begin{exampleblock}{Initialization of $\text{back}[v]$ \pno{3.4.9}}
    \begin{center}
      $\text{back}[v] = \infty, 2n+1$ vs. $\text{back}[v] = d[v]$
    \end{center}
  \end{exampleblock}

  \begin{block}{Solution.}
    $\text{back}[v]$: the earliest reachable ancestor of $v$ by tree + back edges
    \begin{itemize}
      \item tree edge ($\to v$; discovered): $\text{back}[v] = d[v]$
      \item back edge ($v \to w$): $\text{back}[v] = \min \set{\text{back}[v], d[w]}$
      \item backtracking from $w$: $\text{back}[v] = \min \set{\text{back}[v], \text{back}[w] = \text{wBack}}$
    \end{itemize}

    $\text{back}[v] = \infty$:
    \begin{itemize}
      \item if updated
      \item if never updated: $\text{wBack} = \infty > d[v]$ vs. $\text{wBack} = d[w] > d[v]$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}{Bridge}
  \begin{exampleblock}{Bridge \pno{3.4.24}}

  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%
\begin{frame}[plain]
  \begin{figure}[t]
    \centering
    \includegraphics[width = 0.50\textwidth]{figures/thankyou.jpg}
  \end{figure}
\end{frame}
%%%%%%%%%%%%%%%


\subsection{Designing Statistics Calculator} % (fold)
\label{sub:designing_statistics_calculator}

\tref{tbl:stats-calc-prog} contains a description of a small statistics programs. This program reads a number of values from the user, and then outputs some statistics calculated from these values. This program will make use of arrays to store the values entered, and then calculate the required statistics.

\begin{table}[h]
\centering
\begin{tabular}{l|p{10cm}}
  \hline
  \multicolumn{2}{c}{\textbf{Program Description}} \\
  \hline
  \textbf{Name} & \emph{Statistics Calculator} \\
  \\
  \textbf{Description} & Reads values from the user and calculates a range of statistics that are output to the Terminal. Statistics output include \textbf{mean}, \textbf{maximum}, \textbf{sum}, and \textbf{variance}.\\
  \hline
\end{tabular}
\caption{Description of the Statistics Calculator program.}
\label{tbl:stats-calc-prog}
\end{table}

As before, the process to design and implement this program will follow a number of steps:
\begin{enumerate}
  \item Understand the problem, and get some ideas on the tasks that need to be performed.
  \item Choose the artefacts we will create and use
  \item Design the control flow for the procedural\footnote{The program, and any Functions and Procedures.} artefacts
  \item Map these artefacts to code
  \item Compile and run the program
\end{enumerate}

% subsection designing_statistics_calculator (end)

\subsection{Understanding the Statistics Calculator} % (fold)
\label{sub:understanding_the_statistics_calculator}

Most of the ideas around the Statistics Calculator should be fairly straight forward. The main thing to be checked if the equation needed to calculate the different statistics, and then to convert these into steps for the computer.

\subsubsection{Calculating Sum} % (fold)
\label{ssub:calculating_sum}

The \textbf{sum} is the simplest of the Statistics to calculate. This involves adding together all of the numbers in the array. The main issue here is that the computer cannot add all of these values together, and we must rethink our logic to express it in terms of processing \emph{for each} element.

Think about the way you would sum a list of numbers, this is now the task you need to code for the Computer. What is it that you do with each number? To think this through write a list of random numbers down, and then calculate the sum. Do it slowly, and think about the tasks that you are performing \emph{for each number}.

You should have noticed that you are keeping a running total, and that you add the value of each number from the list to that. When you have done this for each number in the list you have the total. The Pseudocode for this is shown in \lref{plst:sum}.

\pseudocode{plst:sum}{Pseudocode for Sum}{topics/arrays/application/Sum.txt}

\csection{\ccode{clst:sum}{C code for Sum Function. You can read the for loop as \emph{`i starts at 0; while i is less than size; increment i at the end of the loop'}}{topics/arrays/application/sum.c}}

\passection{\pascode{paslst:sum}{Pascal code for Sum Function}{topics/arrays/application/Sum.pas}}

There are three key things to notice about the Pseudocode in \lref{plst:sum}. Firstly the \nameref{sub:for_loop} is used to repeat the loop once for each element in the array. Second, the \texttt{i} variable moves through the valid indexes for the array. Finally, the total is used to keep the running total throughout the code.

The for loop in \nameref{sub:for_loop} will repeat its body once for each value in the array. The \texttt{i} variable will be updated to have the \emph{current} index value each time the loop is repeated. Within the loop the $i^{th}$ value from the array is accessed. This is how the for loop processes each of the values from the array.

The \texttt{total} value keeps track of the current running total. Before the loop its value is set to 0, ensuring that it is appropriately initialised. In the body of the loop the current ($i^{th}$) value of the array is added to the \texttt{total}, and the result stored back into \texttt{total}. This means that by the end of the loop the \texttt{total} variable is now storing the sum of all of the elements of the array.

C and Pascal differ in the amount of support they have for working with arrays. C has very limited support, meaning that you need to do some extra work. Pascal has more build in support for arrays, making some common tasks easier to achieve. The main difference is that C does not keep track of the length of an array. This means that you need to pass the number of elements in the array along with the array to functions and procedures that will work with this data. Pascal, on the other hand, does keep track of the length of arrays and gives you three functions you can use to manage this: \texttt{Low} returns the first index of the array, \texttt{High} returns the last index of the array, \texttt{Length} returns the number of elements in the array.

\csection{The C code for the \texttt{Sum} function is in \lref{clst:sum}. Notice how the \texttt{size} parameter is being used to represent the number of elements in the array. This loop is the standard pattern used to code \nameref{sub:for_loop}s that loop over elements in an array in C.}

\passection{The Pascal code for the \texttt{Sum} function is in \lref{paslst:sum}. Notice how it is using the \texttt{Low} and \texttt{High} functions to get the range of valid array indexes. This loop is the standard pattern used to code \nameref{sub:for_loop}s that loop over elements in an array in Pascal.}

% subsubsection calculating_sum (end)

\clearpage
\subsubsection{Calculating Mean} % (fold)
\label{ssub:calculating_mean}

The mean of a list of values is the sum of those values divided by the number of values. In the case of the Statistics Calculator program there is already a \texttt{Sum} function, so the \texttt{Mean} function does not need to recalculate the sum, it can just call the \texttt{Sum} and use the result returned.

The length of the array can be calculated in Pascal using its \texttt{Length} function, where as C can use the \texttt{size} parameter to determine the number of elements in the array. In both cases the basic logic is the same, you use the \texttt{Sum} function to calculate the sum and then divide this by the number of elements in the array.

\pseudocode{plst:mean}{Pseudocode for Mean}{topics/arrays/application/Mean.txt}

\csection{\ccode{clst:mean}{C code for Mean Function}{topics/arrays/application/mean.c}}

\passection{\pascode{paslst:mean}{Pascal code for Mean Function}{topics/arrays/application/Mean.pas}}

% subsubsection calculating_mean (end)

\clearpage
\subsubsection{Calculating Maximum} % (fold)
\label{ssub:calculating_maximum}

Calculating the largest value in the array, the maximum, will require the logic be adjusted to use the \emph{for each} style. How do you calculate the largest value in a list of numbers? With a small list you are likely to just quickly scan it and see the largest value. Think about the tasks you need to perform, and maybe think about how you would do it for a very long list of numbers, one that spans across many pages.

The algorithm needed to find the maximum value in an array needs to perform an action \emph{for each} element of the array. It needs to process each value in isolation, ignoring the other values from the list.

The key is similar to the logic from the \texttt{Sum} function. You need to keep a \emph{running} maximum. This will store the \emph{current} maximum from the array as you loop through \emph{each element} of the array. Like the sum this value can be updated within the loop.

The Pseudocode for this is in \lref{plst:max}. Notice that its basic layout if the same as the \texttt{Sum} function in \lref{plst:sum}. It initialises the \texttt{max} value and then loops through the array performing an action for each value. In this case the action is to check if the $i^{th}$ value of the array is larger than the running maximum in the \texttt{max} variable. When this is the case a new maximum has been found and is stored in the \texttt{max} variable.

One of the important differences between \texttt{Maximum} and \texttt{Sum} is the initialisation of the \texttt{max} value. In \texttt{Maximum} this cannot be initialised to 0 as this would fail to find the maximum if all values were negative. The maximum must be a value from the array, so it is initialised to the first value in the array. The for loop will then start looping from the $2^{nd}$ element, as the $1^{st}$ has already been processed.

\pseudocode{plst:max}{Pseudocode for Maximum}{topics/arrays/application/Max.txt}

% \csection{\ccode{clst:max}{C code for Maximum Function}{topics/arrays/application/max.c}}
%
% \passection{\pascode{paslst:max}{Pascal code for Maximum Function}{topics/arrays/application/Max.pas}}

% subsubsection calculating_maximum (end)

\clearpage
\subsubsection{Calculating Variance} % (fold)
\label{ssub:calculating_variance}

The last statistic to calculate is the Variance. The processing for this will be very similar to the \texttt{Sum} and \texttt{Maximum} functions, though the actual calculation is a little more complex. \eref{eq:var} shows how the Variance of a sample is calculated.

\begin{equation}
  \label{eq:var}
  var(x) = \frac{\displaystyle \sum_{i=1}^{n}(x_{i} - \overline{x})^2}{n - 1}
\end{equation}

In \eref{eq:var} $x$ is the array being processed, $\overline{x}$ is the mean of $x$, $x_i$ is the value of the $i^{th}$ element of $x$, and $n$ is the number of elements in the array. The Sigma indicates that $x_{i} - \overline{x}$ needs to be summed for each element of $x$.

The steps to calculate the Variance are therefore:
\begin{enumerate}
  \item Determine the value of the mean ($\overline{x}$).
  \item Calculate $(x_{i} - \overline{x})^2$ for each element, and store these in a running sum (called \texttt{temp}).
  \item Divide the value (from \texttt{temp}) by the number of elements in the array minus one.
\end{enumerate}

The matching Pseudocode for this is shown in \lref{plst:variance}. In this case $x$ is the \texttt{data} array. In Step 1 the mean ($\overline{x}$) is calculated once and stored in \texttt{avg}. The loop starts at Step 3, and runs \emph{for each} element of the array. The value $(x_{i} - \overline{x})^2$ is calculated for each element in Step 4, and added to the running total stored in \texttt{temp}. The final result is then calculated and the result returned in Step 5.

\pseudocode{plst:variance}{Pseudocode for Variance}{topics/arrays/application/Variance.txt}

% subsubsection calculating_variance (end)

% subsection understanding_the_statistics_calculator (end)

\clearpage
\subsection{Choosing Artefacts for Statistics Calculator} % (fold)
\label{sub:choosing_artefacts_for_statistics_calculator}

In understanding these concepts we have uncovered some Functions that will be included in the program's design.

With the calculations thought through the design seems to be coming together. So far we have thought through the steps needed to calculate the output, but we have not thought about how these values will be read into the program.

Programs can be thought of as transforming data, taking inputs and generating outputs, as shown in \fref{fig:input-output-overview}. So far we have examined the processing needed to create the outputs, but we still need to consider how the data gets into the program, the inputs.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.6\textwidth]{./topics/arrays/diagrams/ProcessingOverview}
   \caption{Programs convert Inputs to Outputs}
   \label{fig:input-output-overview}
\end{figure}

At the start of the program the user will need to enter the values that will be stored in the array. This task can be coded in a \texttt{Populate Array} procedure. This will get the user to enter all of the values into the array. In other words it will allow the user to enter \emph{each value} in the array.

The logic for populating the array can be split into a \texttt{Populate Array} procedure that calls a \texttt{Read Double} function. The \texttt{Read Double} function will be very useful across a number of different programs, so this may be able to be used elsewhere.

\subsubsection{Reading double values from the user} % (fold)
\label{ssub:reading_double_values_from_the_user}

\fref{fig:read-double-flow} shows the flowchart for the process of reading a double value from the user. This includes a \nameref{sub:pre_test_loop} that repeatedly asks the user to enter a number if they value they enter is not a number. This demonstrates a standard \emph{validation} loop, in which you read a value, and check that it is valid in a loop.

The C and Pascal code for this are both slightly different to the flowchart due to different way they handle input and the features they offer to for converting the value read to a number. Details of these are shown alongside \lref{clst:read_double} and \lref{paslst:read_double}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.45\textwidth]{./topics/arrays/diagrams/ReadDouble}
   \caption{Flowchart showing the process for reading a double}
   \label{fig:read-double-flow}
\end{figure}

\clearpage

\csection{The C version of \texttt{Read Double}, shown in \lref{clst:read_double}, uses \texttt{scanf} (see \nameref{sub:c_terminal_input}) to read and format the number in the one action. The \texttt{scanf} Function returns a value indicating the number of conversions that were successful. This can be checked in the loop, and the loop is repeated \emph{while} \texttt{scanf} did not convert one value.
\newline\newline
In the loop itself the \texttt{scanf} is used to read past the value that is not a number, as \texttt{scanf} does not proceed when it finds an error. The format string in \texttt{scanf} indicates that it should read everything up to the end of the line.
\ccode{clst:read_double}{C code for \texttt{Read Double}}{topics/arrays/application/read-double.c}}

\passection{The Pascal version of \texttt{Read Double}, shown in \lref{paslst:read_double}, reads the input as a string and then try to convert it to a double. The \texttt{TryStrToFloat} Function attempts to convert the text read into a number, storing the value in the \emph{result} variable.
\pascode{paslst:read_double}{Pascal code for \texttt{Read Double}}{topics/arrays/application/ReadDouble.pas}}

% subsubsection reading_double_values_from_the_user (end)

\clearpage
\subsubsection{Populating the Array} % (fold)
\label{ssub:populating_the_array}

With the logic for \texttt{Read Double} in place the next step is to determine the steps needed in the \texttt{Populate Array} procedure. This procedure will loop and read a value from the user for each element of the array. This can use the \texttt{Read Double} function to get the value from the user, and then store this in the array's elements.

A flowchart illustrating the steps in \texttt{Populate Array} is shown in \fref{fig:populate-array-flow}. The decision node is being used to show the control mechanism of the for loop, counting from the lowest index of the array to the highest index. Within the body of the loop the two instructions build a prompt string, and then use this in the call to \texttt{Read Double}. The result returned from \texttt{Read Double} is stored in the current ($i^{th}$) element of the array.

Once again the C and Pascal code differ in how this is implemented, centred on how the \emph{prompt} is built within the loop. Pascal has built in support for Strings, so its code is much simpler. The C code for this requires you to coordinate the steps needed to build the text for the prompt. The details for these are shown in the text accompanying \lref{clst:populate_array} and \lref{paslst:populate_array}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.6\textwidth]{./topics/arrays/diagrams/PopulateArray}
   \caption{Flowchart showing the process for \texttt{Populate Array}}
   \label{fig:populate-array-flow}
\end{figure}

\begin{figure}[htbp]
\csection{The C version of \texttt{Populate Array} is shown in \lref{clst:populate_array}. This uses the following functions from \texttt{strings.h}. As C does not know the length of the string each of these functions takes a number (\texttt{n}) that indicates the maximum number of characters to copy.
\begin{itemize}
  \item \texttt{\textbf{strncpy}}: String Copy (n characters). The first parameter is the destination, the second the source, the third is the number of characters.
  \item \texttt{\textbf{sprintf}}: Same as \texttt{printf} (see \nameref{sub:c_console_output}), except that the output is stored in a c-string. In this case the \csnipet{"\% 100"} ensures that only 2 characters (plus the terminator) are written into the string. See \sref{sub:c_string} \nameref{sub:c_string}.
  \item \textbf{\texttt{strncat}}: String Concatenate (n characters). Adds the text in parameter 2 to the end of the string in parameter 1.
\end{itemize}

\ccode{clst:populate_array}{C code for \texttt{Populate Array}}{topics/arrays/application/populate-array.c}}
\end{figure}

\begin{figure}[htbp]
  \passection{The Pascal version of \texttt{Populate Array} is shown in \lref{paslst:populate_array}. This uses Pascal's \textbf{\texttt{IntToStr}} function to convert the value \texttt{i + 1} from Integer to String.
  \pascode{paslst:populate_array}{Pascal code for \texttt{Populate Array}}{topics/arrays/application/PopulateArray.pas}}
\end{figure}

% subsubsection populating_the_array (end)

\subsubsection{Where is the data stored} % (fold)
\label{ssub:where_is_the_data_stored}

The last question to remain is where will the data be stored. The array is a kind of variable, and therefore the array could be a \nameref{sub:local_variable} or a \nameref{sub:global_variable}. As Global Variables should be avoided where possible, this will be coded as a \textbf{Local Variable} within the program's \texttt{Main} procedure. It can then be passed from there to the other Functions and Procedures in the code.

% subsubsection where_is_the_data_stored (end)

\clearpage
\subsubsection{Overview of Statistics Calculator's design} % (fold)
\label{ssub:overview_of_statistics_calculators_design}

That completes the logic needed to implement the Statistics Calculator Program. The final structure is shown in \fref{fig:stats-calc-struct} as a Structure Chart. Notice the double headed arrow on \texttt{data} in the call from \texttt{Main} to \texttt{Populate Array}. This indicates that the data parameter is passing the values into, and getting values out of the \texttt{Populate Array} procedure. Also see how the \texttt{data} value is passed out of \texttt{Main} to the functions that calculate the statistics.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/diagrams/StatsCalcStruct}
   \caption{Structure Chart showing the structure of the \texttt{Statistics Calculator} program}
   \label{fig:stats-calc-struct}
\end{figure}

\mynote{
\begin{itemize}
  \item Remember anytime you need to do something with all of the elements in an array you need to work out how you can achieve this \textbf{for each} element in the array.
  \item Notice that the processing of individual value from the arrays are always similar.
  \item Make sure you can see how the for loop is able to perform these values for each element of the array.
\end{itemize}
}

% subsubsection overview_of_statistics_calculators_design (end)
% subsection choosing_artefacts_for_statistics_calculator (end)

\subsection{Writing the Code for Statistics Calculator} % (fold)
\label{sub:writing_the_code_for_statistics_calculator}

The flowcharts and Pseudocode shown communicate the logic that needs to be coded into the Functions and Procedures of this program. The following two sections, \sref{sec:arrays_in_c} \nameref{sec:arrays_in_c} and  \sref{sec:arrays_in_pascal} \nameref{sec:arrays_in_pascal}, contain a description of the syntax needed to code arrays in the C and Pascal programming languages. This information can be used to write the code for the Statistics Calculator, and other programs.

% subsection writing_the_code_for_statistics_calculator (end)
\clearpage
\subsection{Compiling and Running Statistics Calculator} % (fold)
\label{sub:compiling_and_running_statistics_calculator}

When the code is finished you can compile and run the program. It is a good idea to implement the solution a little bit at a time, compiling and running it frequently as you progress. Try implementing the solution using the following smaller steps, and the tests shown for each.

\begin{enumerate}
  \item Start by getting \texttt{Read Double} to work. In \texttt{Main} just read a single value and output it to the Terminal. \textbf{Tests}:
  \begin{itemize}
    \item Check that a number can be read correctly.
    \item Try entering text, and check the error message is shown and that you can enter a number the next time.
    \item Try entering multiple text values on a single line.
    \item Try entering multiple text values, one after the other.
  \end{itemize}
  \item Implement \texttt{Populate Array}. Include an array in \texttt{Main}, and have its values read in by \texttt{Populate Array}. Print the values back to the Terminal so that you can check this code is working. \textbf{Tests}:
  \begin{itemize}
    \item Enter each of the values and check they are printed out correctly.
    \item Try entering text, this should be handled by \texttt{Read Double} but check it is working correctly with \texttt{Populate Array}.
  \end{itemize}
  \item Implement the \texttt{Sum} Function.
  \textbf{Tests}:
  \begin{itemize}
    \item Test that it works with some basic values.
    \item Try all negative values.
    \item Try a mix of positive and negative values.
  \end{itemize}
  \item Implement the \texttt{Mean} Function. Same tests as \texttt{Sum}.
  \item Implement the \texttt{Variance} Function. Same tests as \texttt{Sum}.
  \item Finish by implementing the \texttt{Maximum} Function. Same tests as \texttt{Sum}.
\end{enumerate}

By building the code a little at a time, and running tests as you go, you will have less code to search when you do find an issue. This makes it easier to fix those little errors that are likely to slip into the code from time to time.

When this iterative process is complete you should have a solution for the Statistics Calculator. You should be able to easily change this so that it can read in ten, a hundred, or even a thousand values from the user. This is something that would not have been possible without using arrays.

\mynote{
\begin{itemize}
  \item Get your code running as soon as you can.
  \item Build a little and test a little as you go.
  \item When you find a bug start looking for it in the code you just added (or changed).
  \item You can perform this as a \emph{\textbf{design, implement, compile, test}} cycle. This enables you to build the program a small piece at a time. Before you start this it is likely to be a good idea to have at least a sketched out plan for the design.
  \item As you develop your design skills you will be able to create larger designs before you code. While you are learning to program it is ok to design and code at the same time as this lets you to quickly test if your design will work.
\end{itemize}
}

% subsection compiling_and_running_statistics_calculator (end)


\subsection{Loss functions for soft classifiers}

\subsubsection{Hinge loss}

\subsubsection{Brier score}


\documentclass{amsart}

\begin{document}

\section*{ {\tt test/twodOseen.jl} }
The verification example is constructed on a domain consisting of
concentric cylinders ($\Omega = \{ (x,y) | 1<r<2 \}$).  For discretization,
we use Taylor-Hood elements generated using the Gmsh package and
a penalty method (parameter $\epsilon$).  Given a divergence-free
advection velocity ${\bf v} = (-\omega y,\omega x)$,
the residual of the Oseen equations are
%
\begin{align*}
  0 &= -\left( {\bf v}\cdot\nabla \right) {\bf u} - \left( {\bf u}\cdot \nabla
  \right) {\bf v} - \nabla p + \mu \nabla \cdot \left(\nabla u + (\nabla u)^T
  \right) + {\bf f} \\
  0 &= \nabla \cdot {\bf u} + \epsilon p
\end{align*}

With an analytical solution ${\bf u}= \frac{(r-1)}{r}(-y,x)$, and $p=r^2$,
the forcing function ${\bf f}$ is set as
%
\begin{displaymath}
  {\bf f} = \left[ \begin{array}{c}
      (2xr^5-3\mu\omega y^3+\omega^2 x( r^2-r^5)-3\mu\omega x^2 y +
      4\mu\omega y r^2)/r^5 \\
      (2yr^5+3\mu\omega x^3 + \omega^2 y(r^2-r^5)+ 3\mu\omega x y^2
      -4\mu\omega x r^2)/r^5
  \end{array} \right].
\end{displaymath}
%
Note that the $\epsilon$-dependence is not accounted for.
\end{document}