Programming/Kdb/Labs/Option pricing
Background: the Black-Scholes formulae
Recall the celebrated Black-Scholes equation
Here
- is a time in years; we generally use 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:
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
and
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,
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:
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
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 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
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
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 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
and hence