The Average Payoff Is the Estimate
The Monte Carlo price of a call is the average of its payoffs over the sampled prices at expiry, discounted to today. The engine's own call does the same thing with the same draws and gives the same number.
The question
The previous lesson drew prices at expiry. A call pays max(S - K, 0) at expiry, the amount by which the price is above the strike, and nothing if it is not. What is the call worth today, if the prices at expiry are samples from the model?
The Monte Carlo answer: average the payoffs of the samples and discount that average from expiry to today. The model price is the expected discounted payoff, and the average of many samples estimates it. This lesson computes the estimate in a loop, sets the engine's own estimate beside it and the model's closed form, and watches the running average settle.
Work it by hand
Suppose, as an assumption for this hand example only, that four sampled prices at expiry are 90, 105, 120 and 80, and the strike is 100. The payoffs are 0, 5, 20 and 0. Their average is 25 / 4 = 6.25.
That average is a value at expiry. With a rate of 0.05 and one year, the discount factor is exp(-0.05), about 0.9512, so the estimate is 6.25 x 0.9512, about 5.95. The discounting happens once, on the average; discounting each payoff first gives the same number.
Four samples give a rough estimate, and a different four would give another. The demo below uses 10,000 samples of the seed, so the average is much steadier, though it is still one draw of a random quantity.
Run it
The program builds the model and the call (lines 1 and 2), draws n prices (line 3) and asks the engine for the model's closed-form price (line 4). The loop of lines 10 to 17 adds up the payoffs with call.payoff (line 11) and, about 50 times along the way, stores the running discounted average (lines 13 to 15). Line 18 asks the engine for its Monte Carlo estimate with pricing.monte_carlo_plain and the same seed. Line 24 returns the closed form in whole cents.
The picture plots the running average against the number of samples, with the closed form as a flat line.

let m = model.black_scholes(spot, rate, 0.0, vol);
let call = instrument.european_call(strike, 1.0);
let st = model.terminal_samples(m, 1.0, n, seed);
let exact = pricing.price(call, m, pricing.analytic()).price();
let every = (st.len() + 49) / 50;
let total = 0.0;
let x = float_list();
let avg = float_list();
let flat = float_list();
for (i in range(0, st.len())) {
total = total + call.payoff(st.at(i));
if ((i + 1) % every == 0) {
x.add(float(i + 1));
avg.add(lmf.round(total * lmf.exp(0.0 - rate) / float(i + 1), 4));
flat.add(lmf.round(exact, 4));
}
}
let engine = pricing.price(call, m, pricing.monte_carlo_plain(n, seed)).price();
lmcode_return_put("samples", x);
lmcode_return_put("running", avg);
lmcode_return_put("closed", flat);
lmcode_return_put("own", lmf.money(total * lmf.exp(0.0 - rate) / float(n)));
lmcode_return_put("engine", lmf.money(engine));
let answer = int(math_round(lmf.round(exact, 2) * 100.0));
{
"answer": 1045,
"samples": [
200,
400,
600,
800,
1000,
1200,
1400,
1600,
1800,
2000,
2200,
2400,
2600,
2800,
3000,
3200,
3400,
3600,
3800,
4000,
4200,
4400,
4600,
4800,
5000,
5200,
5400,
5600,
5800,
6000,
6200,
6400,
6600,
6800,
7000,
7200,
7400,
7600,
7800,
8000,
8200,
8400,
8600,
8800,
9000,
9200,
9400,
9600,
9800,
10000
],
"running": [
9.7163,
10.1349,
10.4344,
10.2424,
10.635,
10.74,
10.4426,
10.4513,
10.3398,
10.2827,
10.4865,
10.6448,
10.632,
10.5716,
10.6012,
10.5208,
10.5054,
10.4333,
10.4702,
10.4351,
10.397,
10.4452,
10.4352,
10.3891,
10.415,
10.4286,
10.4301,
10.4802,
10.5214,
10.519,
10.4989,
10.4879,
10.4531,
10.4343,
10.4899,
10.4911,
10.4814,
10.4784,
10.457,
10.4147,
10.3979,
10.4094,
10.3898,
10.3815,
10.3685,
10.3237,
10.3576,
10.3683,
10.3603,
10.347
],
"closed": [
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506,
10.4506
],
"own": "10.35",
"engine": "10.35"
}With seed 42 and 10,000 samples the loop's own estimate is 10.35 and the engine's is 10.35, the closed form is 10.45, and the running average after 2,000 samples is 10.28. These come from the seed; another seed gives other numbers.
You can change spot, strike, rate, vol, n and seed. n must be from 2 to 10000: 10001 stops line 3 with "model.terminal_samples: n must be 1..10000, got 10001", and 1 stops line 18 with "monte carlo: the standard error needs at least 2 paths, got 1". A seed of 0 stops line 3 with "model.terminal_samples: seed must be 1..2147483647 (0 would mean a clock-based seed), got 0". The picture has at most 50 points.
A spot 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", and a strike of 0 or less stops line 2 with "option strike must be positive". Inputs far outside any sensible range stop the run as well: a rate of 800 stops line 4 with "discount (0) must be positive".
Your turn
The list prices holds prices at expiry. The program below is meant to return the Monte Carlo estimate of the call with strike strike, in whole cents: the average payoff over the list, discounted to today for years years at the continuous rate rate. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

let curve = market.flat_curve_compounded(rate, "annual");
let call = instrument.european_call(strike, float(years));
let total = 0.0;
let used = 0;
for (i in range(0, prices.len())) {
total = total + call.payoff(prices.at(i));
used = used + 1;
}
let mean = total / float(used);
let value = lmf.round(mean * curve.discount(float(years)), 2);
let answer = int(math_round(value * 100.0));
{
"answer": 1675
}The list prices holds prices at expiry. For the call with the given strike, return the Monte Carlo estimate in whole cents: the average of max(price - strike, 0) over the whole list, multiplied by the discount factor of a continuously compounded rate for the given number of years. Check runs each candidate on the data shown and on four others.
Read the result carefully
The estimate is not the price: it is one draw of a random quantity whose average over seeds is the price. Its error comes from the sampling, and a second error, from the model's own assumptions of a constant volatility and no costs, is not reduced by more samples.
In the exercise the prices are given numbers, so the answer is an exact calculation and no seed is involved. The page does not say what any option is worth in practice. 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 Monte Carlo estimate of a call is the average of max(S - K, 0) over the samples, discounted once from expiry to today.
pricing.monte_carlo_plaindoes the same with the same draws asmodel.terminal_samplesand the same seed.- The running average wanders and settles; it does not reach the closed form exactly.
- The estimate is a random quantity; the closed form is the model price it estimates.