Difference between revisions of "Programming/Kdb/Labs/Option pricing"
(47 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Background: the Black-Scholes formulae= | |||
Recall the celebrated '''Black-Scholes equation''' | Recall the celebrated '''Black-Scholes equation''' | ||
<center><math> | <center><math> | ||
Line 6: | Line 8: | ||
Here | Here | ||
* <math>t</math> is a time in years; we generally use <math>t = 0</math> as now; | * <math>t</math> is a time in years; we generally use <math>t = 0</math> as now; | ||
* <math>V(S, t)</math> is the | * <math>V(S, t)</math> is the value of the option; | ||
* <math>S(t)</math> is the price of the underlying asset at time | * <math>S(t)</math> is the price of the underlying asset at time <math>t</math>; | ||
* <math>\sigma</math> is the volatility — the standard deviation of the asset's returns; | * <math>\sigma</math> is the volatility — the standard deviation of the asset's returns; | ||
* <math>r</math> is the annualized risk-free interest rate, continuously compounded; | * <math>r</math> is the annualized risk-free interest rate, continuously compounded; | ||
* <math>q</math> is the annualized (continuous) dividend yield. | * <math>q</math> 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, <math>T</math>, the payoff is given by <math>V(S, T) = C(S, T) =: \max\{S - K, 0\}</math>, in other words, the option is a '''European call option''', then the value of the option at time <math>t</math> is given by the '''Black-Scholes formula for the European call''': | |||
<center><math> | |||
C(S_t, t) = e^{-r\tau} [F_t N(d_1) - K N(d_2)] | |||
</math></center> | |||
where <math>\tau = T - t</math> is the time to maturity, <math>F = S_t e^{(r - q)\tau}</math> is the forward price, and | |||
<center><math> | |||
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] | |||
</math></center> | |||
and | |||
<center><math> | |||
d_2 = d_1 - \sigma\sqrt{\tau}. | |||
</math></center> | |||
Here we have used <math>N(x)</math> to denote the standard normal cumulative distribution function, | |||
<center><math> | |||
N(x) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^x e^{-z^2/2} \, dz. | |||
</math></center> | |||
Similarly, if the payoff is given by <math>V(S, T) = P(S, T) =: \max\{K - S, 0\}</math>, in other words, the option is a '''European put option''', then the value of the option at time <math>t</math> is given by the '''Black-Scholes formula for the European put''': | |||
<center><math> | |||
P(S_t, t) = e^{-r\tau} [K N(-d_2) - F_t N(-d_1)]. | |||
</math></center> | |||
We will implement the formulae for the European call and European put in q. However, our first task is to implement <math>N(x)</math>. | |||
=Task 1: Implementing the standard normal cumulative distribution function= | |||
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> | |||
\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. | |||
</math></center> | |||
where | |||
<center><math> | |||
\phi(x) = \exp(-x^2 / 2)/\sqrt{2\pi}, | |||
</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> | |||
Can you implement this function in q? | |||
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> | |||
and | |||
<center><math> | |||
Z_1 = \sqrt{-2 \ln U_1} \sin(2\pi U_2). | |||
</math></center> | |||
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 — 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
Here
- is a time in years; we generally use as now;
- is the value of the option;
- is the price of the underlying asset at time ;
- is the volatility — the standard deviation of the asset's returns;
- is the annualized risk-free interest rate, continuously compounded;
- 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, , the payoff is given by , in other words, the option is a European call option, then the value of the option at time is given by the Black-Scholes formula for the European call:
where is the time to maturity, is the forward price, and
and
Here we have used to denote the standard normal cumulative distribution function,
Similarly, if the payoff is given by , in other words, the option is a European put option, then the value of the option at time is given by the Black-Scholes formula for the European put:
We will implement the formulae for the European call and European put in q. However, our first task is to implement .
Task 1: Implementing the standard normal cumulative distribution function
As mentioned in the Handbook of Mathematical Functions, can be approximated by
where
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
where is the drift. The Black-Scholes pricing theory then tells us that the price of a vanilla option with pay-off is equal to
where the expectation is taken under the associated risk-neutral process,
We solve this equation by passing to the log and using Ito's lemma; we compute
As this process is constant-coefficient, it has the solution
Since is a Brownian motion, is distributed as a Gaussian with mean zero and variance , so we can write
and hence
or equivalently,
The price of a vanilla option is therefore equal to
The objective of our Monte Carlo simulation is to approximate this expectation by using the law of large numbers, which tells us that if are a sequence of identically distributed independent random variables, then with probability 1 the sequence
converges to .
So the algorithm to price a call option by Monte Carlo is clear. We draw a random variable, , from an distribution and compute
We do this many times and take the average. We then multiply this average by 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 and are independent samples chosen from the uniform distribution on the unit interval . Let
and
Then and are independent random variables with a standard normal distribution.
Equipped with Box-Muller transform, implement a function in q that, given a number , will return 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.