Skip to content

Black and Scholes's d₂, live

Black and Scholes's d₂ = d₁ − s, with s = σ √(T / P) the volatility over the option's life: Hull's d₁ − σ √T when σ is over a year (lusine.finance.d2). N(d₂) is the risk-neutral chance the option finishes in the money. Below, the stock of Hull's Example 15.6 (42 EUR, a strike of 40 EUR, 10% a year, a volatility of 20% a year, six months) is drawn at expiry on your GPU as you read, 4096 times a step, from the model Black and Scholes assume: ln S_T normal, with mean ln(S) + (r − σ²/2) T and variance σ² T. The share of draws finishing above the strike is held to N(d₂), the library's d₂ through its normal distribution; and thirty-two options are held to Black-Scholes in float64.

This page runs the program on your GPU through WebGPU, so it needs a browser with WebGPU (a recent Chrome, Edge, Safari or Firefox). Without it the program does not run and nothing is drawn below.

What it shows

  • The strip above the controls is green while every check holds, and turns red for good once one fails.
  • In the money: the chart draws the share of draws so far finishing in the money, as its distance from N(d₂) in standard errors, a point a step, between the bounds of three either side. Below it, the draws, the share and N(d₂).
  • The checks: Black-Scholes in float64, the thirty-two options' d₂ each within 10⁻⁶ (1 + |d₂|) of d₂ in float64, at the parameters as binary32 holds them; the chance in the money, at every hundredth step, 409 600 draws, the share finishing in the money within three standard errors of N(d₂), 0.7349. Each is held at every step.

Every number here is computed by the program on the GPU; the page only reads the values back and draws them. The seed and Hull's example are the program's settings, which the host may write; this page offers no control to change them yet.

The program

The options, the draws, the share and the checks, the panels the page draws, and the one procedure, main, run each step: the stock drawn at expiry 4096 times, the share in the money, the thirty-two options, and the checks.

// Black and Scholes's d2's live page: thirty-two options held to Black-Scholes in float64; and Hull's Example 15.6's
// stock drawn at expiry 4096 times a step from the model Black and Scholes assume, the share of draws finishing in the
// money held to N(d2), the risk-neutral chance of it, with the page's checks computed here on the GPU and shown by the
// panels the program declares.

namespace d2;

lusine.version version = { { number = 1 } };

// The reader's settings: the seed the options are drawn from; the sweep's option, its strike, rate, volatility over a
// year and life; and the spot the sweep starts at and its step.
host struct {
  integer seed = 391; float<EUR> strike = 50.0 EUR; float<year^-1> rate = 0.05 year^-1; float volatility = 0.2;
  float<year> years = 1.0 year; float<year> period = 1.0 year; float<EUR> first = 10.0 EUR;
  float<EUR> stride = 1.0 EUR;
} settings;

// Hull's examples: Example 15.6, a stock at 42 EUR, a strike of 40 EUR, 10% a year, a volatility of 20% a year and six
// months left; and chapter 19's running example, 49 EUR, 50 EUR, 5%, 20% and twenty weeks.
host struct {
  float<EUR> spot = 42.0 EUR; float<EUR> strike = 40.0 EUR; float<year^-1> rate = 0.1 year^-1; float volatility = 0.2;
  float<year> years = 0.5 year;
} example;
host struct {
  float<EUR> spot = 49.0 EUR; float<EUR> strike = 50.0 EUR; float<year^-1> rate = 0.05 year^-1; float volatility = 0.2;
  float<year> years = 0.38461538461538464 year;
} running;

// Thirty-two options, each with its d2 by Black-Scholes in float64, at the parameters as binary32 holds them; the
// kernel's d2, and whether it is within 1e-6 (1 + |d2|) of it.
struct option {
  bool call; float<EUR> spot; float<EUR> strike; float<year^-1> rate; float volatility; float<year> years;
  float reference; float d2; bool holds;
};
host table<option> options(capacity = 32) = {
  { call = true, spot = 20.886307268226673 EUR, strike = 58.665533887237984 EUR,
    rate = 0.013447202845124347 year^-1, volatility = 0.15902594726357758, years = 4.38507015721194 year,
    reference = -3.0907230099463323 },
  { call = false, spot = 188.59482662705486 EUR, strike = 135.69979791601557 EUR,
    rate = 0.05057192816027696 year^-1, volatility = 0.3115418699091693, years = 4.222032830755394 year,
    reference = 0.5276638915301485 },
  { call = true, spot = 118.91243378167061 EUR, strike = 185.2483042029888 EUR,
    rate = 0.09011600873524846 year^-1, volatility = 0.31585264386633055, years = 3.721298802897474 year,
    reference = -0.4818389750762827 },
  { call = true, spot = 163.8803382421281 EUR, strike = 27.370130355000377 EUR,
    rate = 0.08699557563034188 year^-1, volatility = 0.3805289170746753, years = 2.9718848042202395 year,
    reference = 2.794295729011472 },
  { call = false, spot = 156.01246541016698 EUR, strike = 98.66825482181102 EUR,
    rate = 0.14785047611735416 year^-1, volatility = 0.7774526364639268, years = 2.9756117175286025 year,
    reference = -0.0008650324768491036 },
  { call = true, spot = 120.21559443564263 EUR, strike = 39.4609317943744 EUR,
    rate = 0.031629090453178174 year^-1, volatility = 0.41657887517742265, years = 2.621120091497525 year,
    reference = 1.4374193590517512 },
  { call = true, spot = 125.27283792580695 EUR, strike = 113.42753373611995 EUR,
    rate = 0.13960486210071893 year^-1, volatility = 0.32822630365082595, years = 0.663718099301309 year,
    reference = 0.5842735703273556 },
  { call = true, spot = 111.5146720923226 EUR, strike = 91.70554427769596 EUR,
    rate = 0.09035406599957747 year^-1, volatility = 0.6424737027251702, years = 0.9491809736885719 year,
    reference = 0.13649609962882658 },
  { call = true, spot = 104.7904253041208 EUR, strike = 54.552235260291766 EUR,
    rate = 0.035530099141647786 year^-1, volatility = 0.5662054014919639, years = 4.101686172995604 year,
    reference = 0.12301261158145294 },
  { call = true, spot = 131.9704638511787 EUR, strike = 199.51590533067628 EUR,
    rate = 0.12150206984240143 year^-1, volatility = 0.07353334528136175, years = 1.013456937268504 year,
    reference = -3.9569455419685853 },
  { call = true, spot = 189.31412648919076 EUR, strike = 101.79493838011325 EUR,
    rate = 0.1486055774391051 year^-1, volatility = 0.17001756302296653, years = 3.6964262270876262 year,
    reference = 3.4151417627221483 },
  { call = false, spot = 184.9460138697643 EUR, strike = 154.73367705909305 EUR,
    rate = 0.06516181953678835 year^-1, volatility = 0.8088997581932528, years = 4.43441302148932 year,
    reference = -0.5773486925689537 },
  { call = true, spot = 184.149544077138 EUR, strike = 138.86885284372516 EUR,
    rate = 0.08453121905364037 year^-1, volatility = 0.8815777025835502, years = 4.812131996853199 year,
    reference = -0.6106643099459543 },
  { call = false, spot = 179.92226669131634 EUR, strike = 66.09962335996232 EUR,
    rate = 0.09058104655070943 year^-1, volatility = 0.4365392531203102, years = 0.6805432126265991 year,
    reference = 2.7717227610370028 },
  { call = true, spot = 58.98985403872521 EUR, strike = 69.53690348359211 EUR,
    rate = 0.09882627092796568 year^-1, volatility = 0.8984296966945114, years = 4.194961386027854 year,
    reference = -0.7841603840584548 },
  { call = false, spot = 70.83381725423173 EUR, strike = 196.1692404299365 EUR,
    rate = 0.048636007021675504 year^-1, volatility = 0.619002303682292, years = 0.44163010411418885 year,
    reference = -2.629744292053026 },
  { call = true, spot = 182.0562557898536 EUR, strike = 173.70967347304358 EUR,
    rate = 0.04764858389327849 year^-1, volatility = 0.6800378619360005, years = 4.203906336381006 year,
    reference = -0.5198344148973303 },
  { call = false, spot = 66.23051665247758 EUR, strike = 170.51126513679412 EUR,
    rate = 0.0634771790745104 year^-1, volatility = 0.7740285718864518, years = 2.4297303435717232 year,
    reference = -1.2592187927895504 },
  { call = true, spot = 61.040122087469854 EUR, strike = 25.81738810852937 EUR,
    rate = -0.01776675074159802 year^-1, volatility = 0.6865046104255538, years = 4.1593741745940465 year,
    reference = -0.13823930684363672 },
  { call = true, spot = 183.22855401336517 EUR, strike = 180.44889574953785 EUR,
    rate = 0.022967575251798763 year^-1, volatility = 0.44718485541057645, years = 4.795045054343254 year,
    reference = -0.3615357446479456 },
  { call = false, spot = 55.846083764705995 EUR, strike = 53.78166280056927 EUR,
    rate = 0.12564516571602508 year^-1, volatility = 0.47603203171217895, years = 0.9746002449187416 year,
    reference = 0.10574649484879628 },
  { call = false, spot = 49.43203182526905 EUR, strike = 181.98461219516167 EUR,
    rate = 0.12752664626939325 year^-1, volatility = 0.10648053312598907, years = 0.6180760422632282 year,
    reference = -14.669300949417794 },
  { call = true, spot = 155.62101568917203 EUR, strike = 162.04319920333035 EUR,
    rate = 0.10691574785743298 year^-1, volatility = 0.60801310807133, years = 2.030221913104255 year,
    reference = -0.22929150063505443 },
  { call = false, spot = 143.56758434849291 EUR, strike = 148.75367003537065 EUR,
    rate = 0.12858898784657127 year^-1, volatility = 0.7002982559148344, years = 4.144197661866689 year,
    reference = -0.3638993062601137 },
  { call = false, spot = 197.30127196208014 EUR, strike = 87.76729450260797 EUR,
    rate = 0.13782776344187664 year^-1, volatility = 0.284284922323631, years = 4.1099414619067645 year,
    reference = 2.1002331129437115 },
  { call = true, spot = 176.93386638351492 EUR, strike = 24.762009772194894 EUR,
    rate = 0.002562164252424219 year^-1, volatility = 0.5123741136193225, years = 0.8223266264332635 year,
    reference = 4.004523596243677 },
  { call = true, spot = 22.383447260917606 EUR, strike = 87.65377119357558 EUR,
    rate = 0.12941660432027086 year^-1, volatility = 0.8920939592482549, years = 4.887267819355272 year,
    reference = -1.3575425061198112 },
  { call = false, spot = 198.92285038640267 EUR, strike = 28.77192047369769 EUR,
    rate = 0.07350521936785097 year^-1, volatility = 0.259816172475044, years = 4.990816046864923 year,
    reference = 3.672978124931299 },
  { call = true, spot = 29.333652993672846 EUR, strike = 155.9661717776625 EUR,
    rate = 0.13608761873657335 year^-1, volatility = 0.7921456835739259, years = 0.5821716906310608 year,
    reference = -2.935650410048017 },
  { call = true, spot = 96.2014567940601 EUR, strike = 187.00927996286674 EUR,
    rate = 0.028803522459524823 year^-1, volatility = 0.3952282877503409, years = 4.334615968395207 year,
    reference = -1.067510921755987 },
  { call = false, spot = 84.30148792212937 EUR, strike = 34.445458985634986 EUR,
    rate = 0.07128430993582072 year^-1, volatility = 0.24547475493628396, years = 4.191866317697686 year,
    reference = 2.1240939690670784 },
  { call = true, spot = 159.96599103543278 EUR, strike = 135.11847347833998 EUR,
    rate = 0.09059069229451537 year^-1, volatility = 0.11230768482870263, years = 0.8190242915046948 year,
    reference = 2.3400609266612986 },
};

// The draws, one a cell: the stock at expiry, S e^((r - sigma^2 / 2) T + sigma sqrt(T) z) with z a standard normal
// drawn from the seed, and whether it finishes above the strike.
struct path { float<EUR> end; bool above; };
lattice<path, 2> paths(spacing = 1 m, boundary = wall);

// The draws so far: the step, the draws and those finishing in the money; their share, N(d2) by the library's d2 and
// normal distribution, and the share's standard error; and, for the chart, the share's distance from N(d2) in standard
// errors, beside the bounds of three.
struct {
  integer step; integer draws; integer above; float share; float chance; float error; float distance; float low = -3.0;
  float high = 3.0;
} money;

// The checks: the thirty-two options each within 1e-6 (1 + |d2|) of the float64 d2; and at every hundredth step, 409
// 600 draws, the share finishing in the money within three standard errors of N(d2).
struct { bool reference = true; bool chance = true; } checks;

// A strip of lamps the page draws, lit green while every check holds and red once one fails.
struct lamp { vector<float, 4> colour; };
lattice<lamp, 2> lamps(spacing = 1 m, boundary = wall);

vector<float, 4> lamp_colour(bool holds)
{
  return holds ? vector<float, 4>(0.15, 0.6, 0.3, 1.0) : vector<float, 4>(0.75, 0.15, 0.15, 1.0);
}

float eps() { return 1.0 / 8388608.0; }

integer failing(table<bool> holds) { return count(holds) where !holds; }

bool near(float d2, float reference) { return abs(d2 - reference) <= 0.000001 * (1.0 + abs(reference)); }

// A draw at expiry: the cell's place in the lattice its index in the step's draws.
float<EUR> ending(integer seed, integer step, float<EUR> spot, float<year^-1> rate, float volatility, float<year> years,
                  float<year> period)
{
  let index = count(along(-x)) + 64 * count(along(-y));
  let z = lusine.error.normal(first = lusine.error.draw(seed = bits(seed), step = bits(step), key = fnv1a("z"),
                                                        index = bits(index)),
                              second = lusine.error.draw(seed = bits(seed), step = bits(step), key = fnv1a("angle"),
                                                         index = bits(index)));
  let s = volatility * sqrt(years / period);
  return spot * exp(rate * years - 0.5 * s * s + s * z);
}

bool finishing(float<EUR> end, float<EUR> strike) { return end > strike; }

integer finished(lattice<bool, 2> above) { return count(above) where above; }
integer drawn(lattice<bool, 2> above) { return count(above); }

output table<lusine.widget> panels = {
  lusine.series { { panel = "in the money", chart = "standard errors from N(d2)",
                    quantity = money.distance, steps = 200, label = "the draws'" } },
  lusine.series { { panel = "in the money", chart = "standard errors from N(d2)", quantity = money.low,
                    steps = 200, label = "-3" } },
  lusine.series { { panel = "in the money", chart = "standard errors from N(d2)",
                    quantity = money.high, steps = 200, label = "+3" } },
  lusine.value { { panel = "in the money", quantity = money.draws, label = "draws" } },
  lusine.value { { panel = "in the money", quantity = money.share, label = "finishing in the money" } },
  lusine.value { { panel = "in the money", quantity = money.chance, label = "N(d2)" } },
  lusine.value { { panel = "the checks", quantity = checks.reference, label = "Black-Scholes in float64" } },
  lusine.value { { panel = "the checks", quantity = checks.chance, label = "the chance in the money" } },
};

void main()
{
  money.step = money.step + 1;
  paths.end = ending(settings.seed, step = money.step, example.spot, example.rate, example.volatility, example.years,
                     settings.period);
  paths.above = finishing(paths.end, example.strike);
  money.draws = money.draws + drawn(above = paths.above);
  money.above = money.above + finished(paths.above);
  money.share = float(money.above) / float(money.draws);
  money.chance = lusine.finance.normal_cdf(x = lusine.finance.d2(example.spot, example.strike, example.rate,
                                                                 example.volatility, settings.period, example.years));
  money.error = sqrt(money.share * (1.0 - money.share) / float(money.draws));
  money.distance = (money.share - money.chance) / money.error;
  options.d2 = lusine.finance.d2(options.spot, options.strike, options.rate, options.volatility, settings.period,
                                 options.years);
  options.holds = near(options.d2, options.reference);
  checks.reference = checks.reference && failing(holds = options.holds) == 0;
  checks.chance = checks.chance && (money.step % 100 != 0 || abs(money.share - money.chance) <= 3.0 * money.error);
  lamps.colour = lamp_colour(holds = checks.reference && checks.chance);
}

The kernel's other proof (a thousand options against Black-Scholes in float64, and 400 000 draws on the host) is on its proof page.