Intervals Against the Closed Form
When a payoff has a closed form, a simulation can be checked against it: over many seeds, about 95% of the 95% intervals should contain the closed form. One seed tells little, and the check holds the simulation to the model, not the model to anything.
The question
The standard error gave an interval around the estimate: the estimate plus or minus 1.96 standard errors. The claim attached to it is that about 95 of 100 such intervals, from 100 different seeds, contain the model price. For a call in this model the model price is known in closed form, so the claim can be tested. Does it hold in a run?
This lesson runs the same simulation for 16 seeds and reports how far each estimate is from the closed form, measured in its own standard errors. If the intervals are honest, most of these distances are below 1.96 in size, and about one in twenty is not.
Work it by hand
A distance in standard errors is (estimate - exact) / standard error. Suppose, as an assumption for this hand example, that an estimate is 10.20 with the standard error 0.10 and the exact value is 10.45. The distance is -0.25 / 0.10 = -2.5, which is beyond 1.96 in size: the 95% interval, 10.20 plus or minus 0.196, does not contain 10.45.
So a single miss of 2.5 standard errors is possible, but rare. Whether one miss in a run of 16 means anything depends on how many runs there are; the point of the check is the count over many.
Run it
The loop of lines 7 to 13 runs the plain Monte Carlo engine for the seeds 1 to 16 with paths paths each (line 8). For each seed it forms the distance in standard errors (line 9), keeps it rounded to two decimals (line 11) and counts the seeds for which its size is at most 1.96 (line 12). Lines 14 to 16 return the seeds, the distances and the count, and line 17 returns the closed form in whole cents.

let m = model.black_scholes(spot, rate, 0.0, vol);
let call = instrument.european_call(strike, 1.0);
let exact = pricing.price(call, m, pricing.analytic()).price();
let label = str_list();
let miss = float_list();
let inside = 0;
for (s in range(1, 17)) {
let run = pricing.price(call, m, pricing.monte_carlo_plain(paths, s));
let t = (run.price() - exact) / run.stderr();
label.add(str(s));
miss.add(lmf.round(t, 2));
if (abs(t) <= 1.96) { inside = inside + 1; }
}
lmcode_return_put("seed", label);
lmcode_return_put("misses", miss);
lmcode_return_put("inside", inside);
let answer = int(math_round(lmf.round(exact, 2) * 100.0));
{
"answer": 1045,
"seed": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16"
],
"misses": [
0.27,
-1.4,
-1.06,
1.62,
1.18,
1.5,
-0.31,
-0.89,
-1.02,
-1.08,
0.06,
-0.8,
0.52,
1.78,
0.32,
-0.36
],
"inside": 16
}With 5,000 paths and the default inputs the distances for the seeds 1 to 16 are 0.27, -1.40, -1.06, 1.62, 1.18, 1.50, -0.31, -0.89, -1.02, -1.08, 0.06, -0.80, 0.52, 1.78, 0.32 and -0.36, and all 16 are inside 1.96. That is a typical result and not a guarantee: 95% of 16 is 15.2, so one or two seeds outside would also be normal.
You can change paths and the option inputs.
paths must be at least 4 with the default inputs: 1 stops line 8 with "monte carlo: the standard error needs at least 2 paths, got 1", and 0 stops it with "monte carlo: paths must be 1..4000000, got 0"; 2 or 3 stop line 11 with "LmfApi.round argument 1 must be finite", because for at least one of the 16 seeds every sampled payoff is 0, so the standard error is 0.
A strike of 0 or less stops line 2, and a strike so far out of the money that every sampled payoff is 0 gives the same stop at line 11. 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". Inputs far outside any sensible range stop the run as well: a rate of 800 stops line 3 with "discount (0) must be positive".
With few paths the distances are often far beyond 1.96 (10 paths give -22.06 for seed 9), because the standard error is itself badly estimated; the pattern above needs thousands of paths. Keep paths at most about 5,000 for a quick run: 4,000,000 paths, the most the engine accepts, took about a minute when this page was measured, and 4000001 stops line 8 with "monte carlo: paths must be 1..4000000, got 4000001".
Your turn
The lists estimates and errors hold Monte Carlo estimates and their standard errors, and exact is the closed form. The program below is meant to return how many of the intervals, each the estimate plus or minus z standard errors, contain exact, with the ends included. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

let inside = 0;
for (i in range(0, estimates.len())) {
let reach = 1.96 * errors.at(i);
if (abs(estimates.at(i) - exact) <= reach) { inside = inside + 1; }
}
let answer = inside;
{
"answer": 2
}The lists estimates and errors hold Monte Carlo estimates and their standard errors, in the same order; exact is the closed form and z a multiplier. Return how many of the intervals from estimate - z * error to estimate + z * error contain exact, counting an interval whose end equals exact. Check runs each candidate on the data shown and on four others.
Read the result carefully
The check works only because this payoff has a closed form in this model. It tests the simulation against the model, and it says nothing about whether the model describes any real price. Where there is no closed form, as in the last lesson of this book, the interval is the only measure of the sampling error there is.
One run of 16 intervals does not prove or disprove the simulation: all 16 covering the exact value is typical, and one miss would be too. The numbers come from fixed seeds, and other seeds give other counts.
The demo rounds each distance to two decimals; the exercise returns a count of intervals, which needs no rounding. It is a description of an arithmetic model and not investment advice.
Where the closed form of this lesson comes from is shown in Black-Scholes Step by Step.
Recap
- The distance of an estimate from the exact value in standard errors is (estimate - exact) / standard error.
- About 95% of 95% intervals should contain the exact value; 15 or 16 of 16 is what that suggests, not a guarantee.
- The check compares the simulation with the model's closed form, not the model with anything.
- Where no closed form exists, the interval is all there is.