Programming/Kdb/Labs/Option pricing

From Thalesians Wiki
< Programming‎ | Kdb‎ | Labs
Revision as of 23:22, 17 June 2021 by Admin (talk | contribs)

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

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;T](log[S%K]+((r-q)+.5*sigma*sigma)*T)%sigma*sqrt[T]};

Then,

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