One Random Price at Expiry
Under the model, the price of an asset at expiry is the spot price times an exponential of a drift and a random shock. A seed fixes the draws, so the same seed gives the same prices and another seed gives other ones.
The question
A call option pays out depending on the price of an asset at its expiry date, and nobody knows that price today. The model used in this book gives it a distribution: each draw of a standard normal number z, a bell-shaped random number with average 0 and spread 1, gives one possible price. What is the price for a given z, and what do many draws look like?
For a spot price S, a continuous rate r, a volatility v and a time T in years, the model's price at expiry is S exp((r - v^2/2) T + v sqrt(T) z). This lesson draws sixteen such prices from a seed. The numbers are assumptions, not a forecast of any real price.
Work it by hand
Take spot 100.00, rate 0.05, volatility 0.2 and one year. The drift term is r - v^2/2 = 0.05 - 0.02 = 0.03. The first draw of the seed in the demo below is z = -0.3199, a value read from the run. The exponent is 0.03 + 0.2 x (-0.3199) = -0.03398, and exp(-0.03398) is about 0.9666, so the price is about 96.66.
Another z gives another price: z = 0 gives 100 exp(0.03), about 103.05, and z = 1 gives about 125.9. The prices are spread around the forward price S exp(rT) = 100 exp(0.05), which is 105.13. The model chooses the drift so that this is the average price at expiry, which is what the answer of the demo is.
Run it
The program builds the model with model.black_scholes (line 1) and asks for n prices at expiry after one year with model.terminal_samples and the seed (line 2). The loop of lines 7 to 11 adds up the prices, counts those above the spot (line 9) and keeps the first 16 for the picture, in cents (line 10). Line 12 recovers the first draw z from the first price. Lines 13 to 17 return the values, and line 18 returns the model's average price at expiry in whole cents.
The picture shows the first 16 prices as bars. The returned values come from the seed: with seed 42 and 16 draws the mean of the prices is 103.06 and 9 of them lie above the spot. Another seed gives other numbers.

let m = model.black_scholes(spot, rate, 0.0, vol);
let st = model.terminal_samples(m, 1.0, n, seed);
let label = str_list();
let shown = float_list();
let above = 0;
let total = 0.0;
for (i in range(0, st.len())) {
total = total + st.at(i);
if (st.at(i) > spot) { above = above + 1; }
if (i < 16) { label.add(str(i + 1)); shown.add(lmf.round(st.at(i), 2)); }
}
let first = lmf.log(st.at(0) / spot) - (rate - vol * vol / 2.0);
lmcode_return_put("label", label);
lmcode_return_put("price", shown);
lmcode_return_put("mean", lmf.money(total / float(st.len())));
lmcode_return_put("above", above);
lmcode_return_put("z_first", lmf.round(first / vol, 4));
let answer = int(math_round(lmf.round(spot * lmf.exp(rate), 2) * 100.0));
{
"answer": 10513,
"label": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16"
],
"price": [
96.66,
121.64,
143.39,
86.03,
116.62,
120.23,
108.33,
108.22,
84.18,
100.28,
84.18,
79.74,
75.26,
100.96,
128.62,
94.56
],
"mean": "103.06",
"above": 9,
"z_first": -0.3199
}You can change spot, rate, vol, seed and n.
n must be from 1 to 10000, otherwise line 2 stops with "model.terminal_samples: n must be 1..10000, got 10001"; the seed must be at least 1, otherwise it stops with "model.terminal_samples: seed must be 1..2147483647 (0 would mean a clock-based seed), got 0". A spot of 0 or less or a volatility of 0 or less stops line 1 with "model.black_scholes: spot must be positive" or "model.black_scholes: volatility must be positive". The picture shows at most 16 prices, however large n is.
Inputs far outside any sensible range stop the run as well: a rate of 30 makes the answer too large to return ("lmcode return: invalid JSON value at /answer: unsafe_integer"), and a volatility of 40 makes the first price 0, so that line 12 stops with "lmf.log: argument must be positive, got 0".
Your turn
The list draws holds standard normal numbers. The program below is meant to return the average of the prices at expiry that they give, in whole cents, for the spot price, the rate, the volatility and a whole number of years. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

let total = 0.0;
for (i in range(0, draws.len())) {
let t = float(years);
let drift = (rate - vol * vol) * t;
let shock = vol * sqrt(t) * draws.at(i);
total = total + spot * lmf.exp(drift + shock);
}
let mean = total / float(draws.len());
let answer = int(math_round(lmf.round(mean, 2) * 100.0));
{
"answer": 12068
}The list draws holds standard normal numbers. For each draw z the price at expiry is spot times exp((rate - vol^2 / 2) * years + vol * sqrt(years) * z). Return the average of these prices over the list, in whole cents. Check runs each candidate on the data shown and on four others.
Read the result carefully
The draws here are made-up numbers given as inputs, so the answer is an exact calculation; no random numbers are drawn. The drift is the risk-free rate, which is a model input chosen so that the model prices options consistently. It says nothing about the returns anyone should expect, and the page does not forecast any price.
The model has one constant volatility and no costs, jumps or dividends. A sample of prices from it is not a history of any real price. Money answers are whole cents because the engine works in binary floating point, where numbers such as 1.1 or 105.05 have no exact form and a raw result can end in a stray digit; the page rounds with lmf.round before it shows or compares. It is a description of an arithmetic model and not investment advice.
Recap
- The model's price at expiry is S exp((r - v^2/2) T + v sqrt(T) z) for a standard normal draw z.
- A seed fixes the draws: the same seed gives the same prices, another seed gives other ones.
- The average price at expiry is the forward price S exp(rT); a sample average only comes near it.
model.terminal_samplestakes n from 1 to 10000 and a seed from 1 up.