Difference between revisions of "Programming/Kdb/Labs/Option pricing"

From Thalesians Wiki
< Programming‎ | Kdb‎ | Labs
 
(27 intermediate revisions by the same user not shown)
Line 41: Line 41:
=Task 1: Implementing the standard normal cumulative distribution function=
=Task 1: Implementing the standard normal cumulative distribution function=


<math>N(x)</math> can be approximated by
As mentioned in the [https://www.amazon.co.uk/Handbook-Mathematical-Functions-Formulas-Graphs/dp/161427617X/ Handbook of Mathematical Functions], <math>N(x)</math> can be approximated by
<center><math>
<center><math>
\left\{
\left\{
   \begin{array}{ll}
   \begin{array}{ll}
     1 - \phi(x) \left[ c_1 k + c_2 k^2 + c_3 k^3 + c_4 k^4 + c_5 k^5 \right], & \hbox{B,} \\
     1 - \phi(x) \left[ c_1 k + c_2 k^2 + c_3 k^3 + c_4 k^4 + c_5 k^5 \right], & x \geq 0, \\
     C, & \hbox{D,}
     1 - N(-x), & x < 0,
   \end{array}
   \end{array}
\right.
\right.
</math></center>
</math></center>
where
<center><math>
<center><math>
\left\{
\phi(x) = \exp(-x^2 / 2)/\sqrt{2\pi},
   \begin{array}{ll}
</math></center>
    1 - \phi(x) \left[ c_1 k + c_2 k^2 + c_3 k^3 + c_4 k^4 + c_5 k^5 \right], & \hbox{x \geq 0,} \\
<math>k = 1 / (1 + 0.2316419x), c_1 = 0.319381530, c_2 = -0.356563782, c_3 = 1.781477937, c_4 = -1.821255978, c_5 = 1.330274429.</math>
    1 - N(-x), & \hbox{x < 0,}
 
  \end{array}
Can you implement this function in q?
\right.
 
First, we need
<pre>
pi:acos -1;
</pre>
 
One (terse) implementation would be
<pre>
normal_cdf:{abs(x>0)-(exp[-.5*x*x]%sqrt 2*pi)*t*.31938153+t*-.356563782+t*1.781477937+t*-1.821255978+1.330274429*t:1%1+.2316419*abs x};
</pre>
 
=Task 2: Implement the Black-Scholes formula for the European call and put=
 
Equipped with our implementation of <tt>normal_cdf</tt>, can you implement the Black-Scholes formula for the European call?
 
First, we need
<pre>
compute_d1:{[S;K;r;q;sigma;tau](log[S%K]+((r-q)+.5*sigma*sigma)*tau)%sigma*sqrt tau};
</pre>
 
Then,
<pre>
call_price:{[S;K;r;q;sigma;tau]
   d1:compute_d1[S;K;r;q;sigma;tau];
  d2:d1-sigma*sqrt tau;
  F:S*exp tau*r-q;
  (exp neg r*tau)*(F*normal_cdf d1)-K*normal_cdf d2};
</pre>
and
<pre>
put_price:{[S;K;r;q;sigma;tau]
  d1:compute_d1[S;K;r;q;sigma;tau];
  d2:d1-sigma*sqrt tau;
  F:S*exp tau*r-q;
  (exp neg r*tau)*(K*normal_cdf neg d2)-F*normal_cdf neg d1};
</pre>
 
We can test these implementations on a few sets of parameters, for example
<pre>
call_price[100f;105f;.05;.07;.1;.5]
</pre>
gives 0.7991363, whereas
<pre>
put_price[100f;105f;.05;.07;.1;.5]
</pre>
gives 6.646135.
 
=Background: a simple Monte Carlo model=
 
The Black-Scholes equation may not have analytic solutions for all derivatives that we are interested in. However, it may still be possible to solve it numerically using Monte Carlo methods.
 
The model for stock price evolution is
<center><math>
dS_t = \mu S_t \, dt + \sigma S_t \, dW_t,
</math></center>
where <math>\mu</math> is the drift. The Black-Scholes pricing theory then tells us that the price of a vanilla option with pay-off <math>f</math> is equal to
<center><math>
e^{-r\tau}\mathbb{E}[f(S_T)],
</math></center>
where the expectation is taken under the associated risk-neutral process,
<center><math>
dS_t = (r - q) S_t \, dt + \sigma S_t \, dW_t.
</math></center>
 
We solve this equation by passing to the log and using Ito's lemma; we compute
<center><math>
d \ln S_t = \left(r - q - \frac{1}{2}\sigma^2\right) \, dt + \sigma \, dW_t.
</math></center>
 
As this process is constant-coefficient, it has the solution
<center><math>
\ln S_t = \ln S_0 + \left(r - q - \frac{q}{2}\sigma^2\right) t + \sigma W_t.
</math></center>
 
Since <math>W_t</math> is a Brownian motion, <math>W_T</math> is distributed as a Gaussian with mean zero and variance <math>T</math>, so we can write
<center><math>
W_T = \sqrt{T} N(0, 1),
</math></center>
and hence
<center><math>
\ln S_T = \ln S_0 + \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1),
</math></center>
or equivalently,
<center><math>
S_T = S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right].
</math></center>
 
The price of a vanilla option is therefore equal to
<center><math>
e^{-rT} \mathbb{E}\left[ f\left( S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right] \right) \right].
</math></center>
 
The objective of our Monte Carlo simulation is to approximate this expectation by using the law of large numbers, which tells us that if <math>Y_j</math> are a sequence of identically distributed independent random variables, then with probability 1 the sequence
<center><math>
\frac{1}{N} \sum_{j=1}^N Y_j
</math></center>
converges to <math>\mathbb{E}[Y_1]</math>.
 
So the algorithm to price a call option by Monte Carlo is clear. We draw a random variable, <math>x</math>, from an <math>N(0, 1)</math> distribution and compute
<center><math>
f\left( S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right] \right).
</math></center>
We do this many times and take the average. We then multiply this average by <math>e^{-rT}</math> and we are done.
 
=Task 3: Generating standard Gaussian random variates=
 
To generate 10 standard uniform random variates in q, we can simply use
<pre>
10?1f
</pre>
 
How can we convert these standard uniform random variates into standard Gaussian random variates?
 
One approach is to use the '''Box-Muller transform'''. Suppose <math>U_1</math> and <math>U_2</math> are independent samples chosen from the uniform distribution on the unit interval <math>[0, 1]</math>. Let
<center><math>
Z_0 = \sqrt{-2 \ln U_1} \cos(2\pi U_2)
</math></center>
</math></center>
where
and
<center><math>
<center><math>
\phi(x) = \exp(-x^2 / 2)/\sqrt{2\pi},
Z_1 = \sqrt{-2 \ln U_1} \sin(2\pi U_2).
</math><center>
</math></center>
<math>k = 1 / (1 + 0.2316419x), c_1 = 0.319381530, c_2 = -0.356563782, c_3 = 1.781477937, c_4 = -1.821255978, c_5 = 1.330274429</math>.
Then <math>Z_0</math> and <math>Z_1</math> are independent random variables with a standard normal distribution.
 
Equipped with Box-Muller transform, implement a function in q that, given a number <math>x</math>, will return <math>x</math> standard Gaussian random variates.
 
<pre>
normal_variates:{$[x=2*n:x div 2;raze sqrt[-2*log n?1f]*/:(sin;cos)@\:(2*pi)*n?1f;-1_.z.s 1+x]}
</pre>
 
=Task 4: Implement a Monte Carlo pricer=
 
We are now ready to implement a Monte Carlo pricer. We can use it to price more complicated options than the European call and put. However, the European call and put are convenient &mdash; the analytic solutions enable us to test our Monte Carlo pricer.
 
<pre>
mc:{[S;r;q;sigma;T;path_count;payoff]
  root_variance:sqrt variance:sigma*sigma*T;
  moved_spot:S*exp (ito_correction:-.5*variance)+(r-q)*T;
  exp[neg T*r]*avg payoff moved_spot*exp[root_variance*normal_variates path_count]};
</pre>
 
Let's define the European call and put payoffs:
 
<pre>
call_payoff:{[K;S]0|S-K}
put_payoff:{[K;S]0|K-S}
</pre>
 
We can now test our Monte Carlo pricer on these payoffs and confirm that we get similar answers to those produced by the analytic formulae:
 
<pre>
mc[100f;.05;.07;.1;.5;100000;call_payoff[105f]]
mc[100f;.05;.07;.1;.5;100000;put_payoff[105f]]
</pre>
 
=Task 5: Pricing a double digital option=
 
We shall now use our q Monte Carlo pricer to price a double digital option.
 
First, we implement the double digital payoff:
<pre>
double_digital_payoff:{[L;U;S]?[(S<L)|S>U;z;1f+z:0f*S]}
</pre>
 
We are now ready to price a double digital option:
<pre>
mc[100f;.05;.07;.1;.5;100000;double_digital_payoff[105f;110f]]
</pre>
 
The result will vary (due to the Monte Carlo randomness), but it will be around 0.1260532.

Latest revision as of 22:12, 21 December 2021

Background: the Black-Scholes formulae

Recall the celebrated Black-Scholes equation

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{\partial V}{\partial t} + \frac{1}{2} \sigma^2 S^2 \frac{\partial^2 V}{\partial S^2} + (r - q) S \frac{\partial V}{\partial S} - r V = 0. }

Here

  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle t} is a time in years; we generally use Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle t = 0} as now;
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V(S, t)} is the value of the option;
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle S(t)} is the price of the underlying asset at time Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle t} ;
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sigma} is the volatility — the standard deviation of the asset's returns;
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle r} is the annualized risk-free interest rate, continuously compounded;
  • Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle q} is the annualized (continuous) dividend yield.

The solution of this equation depends on the payoff of the option — the terminal condition. In particular, if at the time of expiration, Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle T} , the payoff is given by Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V(S, T) = C(S, T) =: \max\{S - K, 0\}} , in other words, the option is a European call option, then the value of the option at time Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle t} is given by the Black-Scholes formula for the European call:

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle C(S_t, t) = e^{-r\tau} [F_t N(d_1) - K N(d_2)] }

where Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \tau = T - t} is the time to maturity, Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F = S_t e^{(r - q)\tau}} is the forward price, and

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle d_1 = \frac{1}{\sigma\sqrt{\tau}} \left[ \ln\left(\frac{S_t}{K}\right) + \left(r - q + \frac{1}{2}\sigma^2\right)\tau \right] }

and

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle d_2 = d_1 - \sigma\sqrt{\tau}. }

Here we have used Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle N(x)} to denote the standard normal cumulative distribution function,

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle N(x) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^x e^{-z^2/2} \, dz. }

Similarly, if the payoff is given by Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V(S, T) = P(S, T) =: \max\{K - S, 0\}} , in other words, the option is a European put option, then the value of the option at time Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle t} is given by the Black-Scholes formula for the European put:

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle P(S_t, t) = e^{-r\tau} [K N(-d_2) - F_t N(-d_1)]. }

We will implement the formulae for the European call and European put in q. However, our first task is to implement Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle N(x)} .

Task 1: Implementing the standard normal cumulative distribution function

As mentioned in the Handbook of Mathematical Functions, Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle N(x)} can be approximated by

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \left\{ \begin{array}{ll} 1 - \phi(x) \left[ c_1 k + c_2 k^2 + c_3 k^3 + c_4 k^4 + c_5 k^5 \right], & x \geq 0, \\ 1 - N(-x), & x < 0, \end{array} \right. }

where

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \phi(x) = \exp(-x^2 / 2)/\sqrt{2\pi}, }

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle k = 1 / (1 + 0.2316419x), c_1 = 0.319381530, c_2 = -0.356563782, c_3 = 1.781477937, c_4 = -1.821255978, c_5 = 1.330274429.}

Can you implement this function in q?

First, we need

pi:acos -1;

One (terse) implementation would be

normal_cdf:{abs(x>0)-(exp[-.5*x*x]%sqrt 2*pi)*t*.31938153+t*-.356563782+t*1.781477937+t*-1.821255978+1.330274429*t:1%1+.2316419*abs x};

Task 2: Implement the Black-Scholes formula for the European call and put

Equipped with our implementation of normal_cdf, can you implement the Black-Scholes formula for the European call?

First, we need

compute_d1:{[S;K;r;q;sigma;tau](log[S%K]+((r-q)+.5*sigma*sigma)*tau)%sigma*sqrt tau};

Then,

call_price:{[S;K;r;q;sigma;tau]
  d1:compute_d1[S;K;r;q;sigma;tau];
  d2:d1-sigma*sqrt tau;
  F:S*exp tau*r-q;
  (exp neg r*tau)*(F*normal_cdf d1)-K*normal_cdf d2};

and

put_price:{[S;K;r;q;sigma;tau]
  d1:compute_d1[S;K;r;q;sigma;tau];
  d2:d1-sigma*sqrt tau;
  F:S*exp tau*r-q;
  (exp neg r*tau)*(K*normal_cdf neg d2)-F*normal_cdf neg d1};

We can test these implementations on a few sets of parameters, for example

call_price[100f;105f;.05;.07;.1;.5]

gives 0.7991363, whereas

put_price[100f;105f;.05;.07;.1;.5]

gives 6.646135.

Background: a simple Monte Carlo model

The Black-Scholes equation may not have analytic solutions for all derivatives that we are interested in. However, it may still be possible to solve it numerically using Monte Carlo methods.

The model for stock price evolution is

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle dS_t = \mu S_t \, dt + \sigma S_t \, dW_t, }

where Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mu} is the drift. The Black-Scholes pricing theory then tells us that the price of a vanilla option with pay-off Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f} is equal to

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle e^{-r\tau}\mathbb{E}[f(S_T)], }

where the expectation is taken under the associated risk-neutral process,

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle dS_t = (r - q) S_t \, dt + \sigma S_t \, dW_t. }

We solve this equation by passing to the log and using Ito's lemma; we compute

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle d \ln S_t = \left(r - q - \frac{1}{2}\sigma^2\right) \, dt + \sigma \, dW_t. }

As this process is constant-coefficient, it has the solution

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \ln S_t = \ln S_0 + \left(r - q - \frac{q}{2}\sigma^2\right) t + \sigma W_t. }

Since Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle W_t} is a Brownian motion, Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle W_T} is distributed as a Gaussian with mean zero and variance Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle T} , so we can write

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle W_T = \sqrt{T} N(0, 1), }

and hence

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \ln S_T = \ln S_0 + \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1), }

or equivalently,

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle S_T = S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right]. }

The price of a vanilla option is therefore equal to

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle e^{-rT} \mathbb{E}\left[ f\left( S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right] \right) \right]. }

The objective of our Monte Carlo simulation is to approximate this expectation by using the law of large numbers, which tells us that if Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Y_j} are a sequence of identically distributed independent random variables, then with probability 1 the sequence

converges to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathbb{E}[Y_1]} .

So the algorithm to price a call option by Monte Carlo is clear. We draw a random variable, Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} , from an Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle N(0, 1)} distribution and compute

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle f\left( S_0 \exp \left[ \left(r - q - \frac{q}{2}\sigma^2\right) T + \sigma \sqrt{T} N(0, 1) \right] \right). }

We do this many times and take the average. We then multiply this average by Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle e^{-rT}} and we are done.

Task 3: Generating standard Gaussian random variates

To generate 10 standard uniform random variates in q, we can simply use

10?1f

How can we convert these standard uniform random variates into standard Gaussian random variates?

One approach is to use the Box-Muller transform. Suppose Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle U_1} and Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle U_2} are independent samples chosen from the uniform distribution on the unit interval Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle [0, 1]} . Let

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Z_0 = \sqrt{-2 \ln U_1} \cos(2\pi U_2) }

and

Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Z_1 = \sqrt{-2 \ln U_1} \sin(2\pi U_2). }

Then Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Z_0} and Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Z_1} are independent random variables with a standard normal distribution.

Equipped with Box-Muller transform, implement a function in q that, given a number Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} , will return Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} standard Gaussian random variates.

normal_variates:{$[x=2*n:x div 2;raze sqrt[-2*log n?1f]*/:(sin;cos)@\:(2*pi)*n?1f;-1_.z.s 1+x]}

Task 4: Implement a Monte Carlo pricer

We are now ready to implement a Monte Carlo pricer. We can use it to price more complicated options than the European call and put. However, the European call and put are convenient — the analytic solutions enable us to test our Monte Carlo pricer.

mc:{[S;r;q;sigma;T;path_count;payoff]
  root_variance:sqrt variance:sigma*sigma*T;
  moved_spot:S*exp (ito_correction:-.5*variance)+(r-q)*T;
  exp[neg T*r]*avg payoff moved_spot*exp[root_variance*normal_variates path_count]};

Let's define the European call and put payoffs:

call_payoff:{[K;S]0|S-K}
put_payoff:{[K;S]0|K-S}

We can now test our Monte Carlo pricer on these payoffs and confirm that we get similar answers to those produced by the analytic formulae:

mc[100f;.05;.07;.1;.5;100000;call_payoff[105f]]
mc[100f;.05;.07;.1;.5;100000;put_payoff[105f]]

Task 5: Pricing a double digital option

We shall now use our q Monte Carlo pricer to price a double digital option.

First, we implement the double digital payoff:

double_digital_payoff:{[L;U;S]?[(S<L)|S>U;z;1f+z:0f*S]}

We are now ready to price a double digital option:

mc[100f;.05;.07;.1;.5;100000;double_digital_payoff[105f;110f]]

The result will vary (due to the Monte Carlo randomness), but it will be around 0.1260532.