Here we present how to implement the Black-Scholes formula
for vanilla options in Mathematica.
One way to do this is to create a package, which can
then be used in any Mathematica notebook.
BeginPackage["Options`Vanilla`"]
(************************************************************************************
author: Uwe Wystup
date : November 1999
*************************************************************************************)
ncum::usage = "standard normal cumulative distribution function"
ndf::usage = "standard normal density function"
Vanilla::usage = "Vanilla[x, K, vol, r, rf, T, fi, G]\n
Black-Scholes values and Greeks\n
for European put and call options\n
x: spot\n
K: strike\n
vol: annual volatility\n
r: domestic risk free rate: discounting is done as (1+r)^{-T}\n
rf: foreign risk free rate or dividend rate: discounting is done as
(1+rf)^{-T}\n
T: time to maturity in years\n
fi: +1 for call, -1 for put\n
G: Greek: \n
0 : value\n
1 : spot delta\n
2 : spot gamma\n
3 : theta (in years)\n
4 : vega\n
5 : domestic rho\n
6 : foreign rho\n
"
Begin["`Private`"]
ncum[x_] := 1/2*(Erf[x/Sqrt[2]] + 1);
ndf[x_] := Evaluate[D[ncum[x], x]];
Vanilla[x_, K_, vol_, r_, rf_, T_, fi_, G_]:=Block[{dp, dm},
dp = (Log[x/K] + (Log[1+r] - Log[1+rf] + 0.5*vol*vol)*T)/(vol*Sqrt[T]);
dm = (Log[x/K] + (Log[1+r] - Log[1+rf] - 0.5*vol*vol)*T)/(vol*Sqrt[T]);
Switch[G,
0,fi*(Exp[-Log[1+rf]*T]*x*ncum[fi*dp] - Exp[-Log[1+r]*T]*K*ncum[fi*dm]),
1,fi*Exp[-Log[1+rf]*T]*ncum[fi*dp],
2,Exp[-Log[1+rf]*T]*ndf[dp]/(x*vol*Sqrt[T]),
3,-Exp[-Log[1+rf]*T]*ndf[dp]*x*vol/(2*Sqrt[T])+
fi*(Log[1+rf]*Exp[-Log[1+rf]*T]*x*ncum[fi*dp] - Log[1+r]*Exp[-Log[1+r]*T]*K*ncum[fi*dm]),
4,Exp[-Log[1+rf]*T]*ndf[dp]*x*Sqrt[T],
5,fi*T*Exp[-Log[1+r]*T]*K*ncum[fi*dm]/(1+r),
6,-fi*T*Exp[-Log[1+rf]*T]*x*ncum[fi*dp]/(1+rf),
_, 0]];
End[]
EndPackage[]
<<Options`Vanilla`
?Vanilla
Vanilla[1.8,1.8,0.1,0,0,1,1,1]