Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 9

Monte Carlo

Answer a question you can't easily work out by hand: play it out thousands of times and count. How close the count gets, and what no count can fix.

Know the basics? Read the lesson notes

Four rolls of a die

Here is a bet. Roll a fair die four times: each roll is equally likely to show any of its six faces. You win if at least one six shows. What is your chance of winning?

Keep your guess in mind. On the next screen you find out by rolling.

Roll it, then let a computer roll

One game is four rolls. Roll a game yourself. Then let the computer play many more and keep count of the share you would have won. The line tracks that share as the games pile up.

Four rolls of a die, game after game (simulated)

A game is won when at least one of its four rolls is a six. Across: the number of games played, each mark ten times the one before.

Games played0
Won–
Share won–

After 10 simulated games the share was 70%, after 100 games 56%, after 1,000 51.2% and after 10,000 52.0%. It swings a lot at first, then settles: a little over 1 in 2, answer C.

"Four rolls at 1 in 6 each" counts a game with two sixes as two wins, but it is one win. Answering a question by playing it out many times with random numbers and counting is called a Monte Carlo simulation, after the casino town of Monte Carlo. Each game played is one run.

Check it by hand

This question also has an exact answer. When rolls don't affect each other (they are independent), the chance that all of them do something is their chances multiplied together. One roll misses a six 5 times in 6. All four miss: 5/6 × 5/6 × 5/6 × 5/6 = 625 ÷ 1,296 = 48.2%. Every other game has at least one six: 100% − 48.2% = 51.8%.

Your 10,000 games said 52.0%, which is 0.2 percentage points away (a percentage point is the gap between two percentages: from 51.8% to 52.0% is 0.2 points). Why don't they match exactly?

The dice are fair by design. A count still depends on which games happened to come up: another 10,000 games would land a little differently.
Another 10,000 games would land a little differently. The next screen measures how far a count can land from the exact answer.
625 ÷ 1,296 is 48.225…%, so 51.8% is right to one decimal. The gap is in the count: another 10,000 games would land a little differently.
Checking a simulation on a question with a known answer shows whether it works before you trust it with one you can't work out.

How far can a count miss?

To see how far a count can land from the exact answer, the computer repeated the whole count 1,000 times, each time with fresh games. Slide to change how many games go into each count: down to 25, then up to 6,400.

1,000 counts of the same question (simulated)

Bars: how many of the 1,000 counts landed at each share won. Dashed line: the exact 51.8%. Shaded: one typical miss either side of it.

Typical miss–

The typical miss is the counts' standard deviation: how far a typical count lands from their average, which here is close to the exact 51.8%. In this simulation, counts of 100 games typically miss by 5.11 points.

Four times the games, about half the miss. At 6,400 games a count typically misses by 0.62 points.

Theory

The error of a Monte Carlo answer

The pattern has a formula. Call the rules and chances the computer plays out the model: here, four rolls of a fair die. Score each run 1 if the thing happened and 0 if it didn't. The share of hits is then the average score of the runs, and its typical miss is called the standard error, here the Monte Carlo error:

error = σ ÷ √N  ·  σ = √(p × (1 − p))
error
the standard error: how far a simulated answer typically lands from the answer that endless runs of the same model would give
N
the number of runs
σ
the standard deviation of one run's score (σ is the Greek letter sigma); for a run that scores 1 or 0 it is √(p × (1 − p))
p
the chance of a hit, as a share: 0.518 for 51.8%

Counts pile into a bell-shaped hill around the model's answer (the one endless runs would give; for the dice, the exact 51.8%), so about 68 in 100 land within one error of it and about 95 in 100 within two (this is the hill in the last step).

Worked example: your dice

p = 0.518, so σ = √(0.518 × 0.482) = √0.2497 = 0.50. For N = 100: 0.50 ÷ √100 = 0.50 ÷ 10 = 0.05, which is 5 points; the 1,000 counts measured 5.11. For N = 10,000: 0.50 ÷ 100 = 0.005, which is 0.5 points, so your 52.0% sat 0.2 points from 51.8%, inside one error.

Holds when
  • each run is drawn fresh, unaffected by the others (the runs are independent);
  • every run follows the same model, so there is one answer to close in on;
  • hits are not so rare that only a handful of runs score one.
Breaks when

An event is rarer than the runs can see: 0 hits in 1,000 runs doesn't mean a chance of 0. An error bar does not show a wrong model: a later step in this lesson shows one giving a wrong answer with a small error.

Try it on new numbers. A simulation of 400 runs finds that something happens in 20% of them. What is the error of that 20%, in percentage points?

σ = √(0.2 × 0.8) = √0.16 = 0.4, and √400 = 20, so the error is 0.4 ÷ 20 = 0.02: 2 points. About 95 counts in 100 like it land within 4 points of the model's answer.
σ = √(0.2 × 0.8) = √0.16 = 0.4 (the square root, not 0.16 itself). √400 = 20, so the error is 0.4 ÷ 20 = 0.02: 2 points.

A year of a made-up price

Now a question about a price. Take a made-up price that starts at €100. Each trading day (a day the market is open; a year has about 252) a fair coin moves it: heads up €1, tails down €1. A running total of random steps like this is called a random walk. How likely is the price to end the year above €110?

A made-up price, one year at a time (simulated)

Each line is one simulated year of 252 days from €100. Coloured: years that end above the line. At most 200 are drawn; the numbers count every year walked.

Years walked0
Ended above the line–
Share–

Of 10,000 simulated years, 2,511 ended above €110: 25.1%.

When the exact answer is unknown, use your simulated share as p. Its error: √(0.251 × 0.749) = 0.43, and 0.43 ÷ √10,000 = 0.0043, so 0.43 points. This question can also be answered exactly, by counting every possible year: each day goes up or down, so there are 2 × 2 × … 252 times over, all equally likely, and 24.4% of them end above €110 (the counting is the one step to take on trust). The simulation is 0.7 points away, within two errors.

Did it touch €90?

Now a question about the whole year, not only its end. How often does the price fall to €90 or lower on some day during the year, even if it climbs back later?

Keep your guess in mind. On the next screen you mark those years.

Count the dips

The same 10,000 years. Mark every year whose price touched €90 or lower on any day.

The years that touched €90 (simulated)

Coloured: years whose lowest price was €90 or lower. At most 200 are drawn; the numbers count all 10,000.

Touched €90 or lower–
Ended at €90 or lower–

5,299 of the 10,000 simulated years touched €90 or lower: 53.0%, about 1 in 2, answer A. Its error is 0.5 points.

Only 2,820 of those years ended at €90 or lower; the rest dipped and climbed back. A question about the path needs every day of every year, not only the end. Many such questions have no simple sum to work them out by hand; this coin walk is an exception (its exact answer is in the box below). A simulation doesn't need one: it walks every day and checks.

For the precise: the exact answer for the coin walkFor a fair ±€1 walk, the chance of touching €90 or lower within 252 days is, by the reflection principle, the chance of ending at €90 or lower (28.5%) plus the chance of ending at €88 or lower, which comes to 53.0% (the script also steps every price's exact chance forward one day at a time and gets the same fraction). The simulated 28.2% for ending at €90 or lower sits 0.3 points from its exact 28.5%.

A rule's range of outcomes

A trading rule decides when to trade. Take one that wins 40% of its trades. A win pays 2R and a loss costs 1R, where R is a fixed amount of money, like €10. Its expectancy, the average result per trade, is 0.4 × 2R − 0.6 × 1R = +0.2R, so 100 trades make +20R on average (this made-up rule pays no trading costs).

Picture 10,000 simulated accounts, each taking 100 trades of this rule. How many end below where they started?

Keep your guess in mind. On the next screen the 10,000 accounts trade.

10,000 accounts

Every account trades the same rule, with its own random wins and losses. It starts at 10 trades each; slide right to give each account more trades.

10,000 accounts of one rule (simulated)

Wins 40% of trades: +2R a win, −1R a loss. Bars: how many accounts ended with each result. Coloured: below the start.

Average result–
Below the start–
Worst · best–

After 100 trades, 942 of the 10,000 simulated accounts were below their start: 9.4%, about 1 in 10, answer C.

Their average was +19.9R, next to the +20R expectancy, but the worst lost 37R and the best made 80R. After 1,000 trades, none of the 10,000 was below its start. The expectancy says where the accounts head on average; a simulation shows the whole range around it.

Garbage in, confident answer out

Every answer so far is an answer about a model: a fair coin each day, a rule that wins exactly 40%. Suppose the 40% came from a short backtest (the rule run over past prices to see what it would have made), and the rule really wins 35%. Its expectancy is then 0.35 × 2R − 0.65 × 1R = +0.05R a trade. Compare 10,000 accounts of 100 trades under each model.

The same question under two models (simulated)

Bars: 10,000 accounts' results after 100 trades. Coloured: below the start.

Expectancy+0.2R a trade
Below the start–
Error–

Below the start after 100 trades, in 10,000 simulated accounts each. Assumed model: 9.4%, error 0.29 points. True model: 39.1%, error 0.49 points (exact 38.0%: this run landed a little high, which happens about 3 times in 100).

Both answers are precise, and the first is wrong by about 30 points. More runs shrink the error around the model's own answer; they do nothing for a model that is wrong. The coin-walk price has the same limit: real prices have more extreme days than a fair coin gives (fat tails), so its 53.0% describes the coin walk, not a market.

What this means when you simulate

Three habits follow. Turn them over one at a time.

  1. Simulate what is hard to work out by hand.

    Write the model down, run it many times and count. Questions about a path, such as a dip to €90 at some point in the year, are where it helps most.

Practice 1 of 3

A friend's simulation

The situationA friend simulated a made-up account 2,500 times, each run with fresh random trades. It ended below its start in 20% of the runs.

What is the error of that 20%, in percentage points?

Hint: first σ = √(p × (1 − p)) with p = 0.2; then divide by √N.

σ = √(0.2 × 0.8) = 0.4 and √2,500 = 50, so the error is 0.4 ÷ 50 = 0.008: 0.8 points. About 95 counts in 100 like it land within 1.6 points of the model's answer.
σ = √(0.2 × 0.8) = √0.16 = 0.4 and √2,500 = 50, so the error is 0.4 ÷ 50 = 0.008 as a share, which is 0.8 points.
Practice 2 of 3

Twice as precise

The situationYour simulation of 10,000 independent runs gives 52.0% with an error of 0.5 points. You want an error of 0.25 points.

How many runs do you need?

The error is σ ÷ √N, so halving it needs √N twice as big, which is N four times as big: 4 × 10,000 = 40,000.
Twice the runs shrinks the error only by √2 ≈ 1.41, to about 0.35 points. Halving it needs four times the runs: 40,000.
That works, but it is more than needed: 100,000 runs give 0.5 ÷ √10 ≈ 0.16 points. Halving the error needs four times the runs: 40,000.
Practice 3 of 3

A tiny error

The situationA friend models a share price as this lesson's coin walk: from €100, up or down €1 each trading day. They run 1,000,000 simulated years and report a fall to €60 or lower within a year in 1.2% of them, error 0.01 points.

What does the small error tell you?

The error measures only how far the runs' luck could move them from the model's own answer. Real prices have more extreme days than a coin gives, so the real chance could be quite different, and the simulation can't say by how much.
A million runs is plenty here: √(0.012 × 0.988) ÷ 1,000 ≈ 0.0001, about 0.01 points, as reported. The weak part is the model, not the number of runs.
The runs pinned down the coin walk's answer closely (its exact answer rounds to 1.2% too). Whether a coin walk describes a real share price is a separate question that no number of runs answers.
✓

Lesson done

A Monte Carlo simulation answers a question by playing out a model many times and counting. When the runs are independent, its error shrinks like 1 ÷ √(number of runs): four times the runs halve it. The answer is only as good as the model, and more runs never fix a wrong one.

Key ideas

Monte Carlo simulation
answering a question by playing out a model many times with random numbers and counting what comes out
Run
one play of the model: one game of four rolls, one simulated year, one account
Model
the made-up world a simulation plays out: its rules and their chances
Standard error (Monte Carlo error)
how far a simulated answer typically lands from the answer endless runs of the same model would give; it measures luck in the runs, not whether the model is right
Random walk
a running total of random steps, such as a price moved up or down by a coin each day
Percentage point
the gap between two percentages: from 51.8% to 52.0% is 0.2 points

Formulas

error = σ ÷ √N  ·  σ = √(p × (1 − p))

For a yes-or-no question with chance p, over N independent runs of one model. Four times the runs: half the error. About 95 counts in 100 land within two errors of the model's answer.

Lesson notes

A Monte Carlo simulation answers a question by playing out a model (a made-up world with set rules and chances) many times with random numbers and counting what comes out. When the runs are independent, its error (how far its answer typically lands from the model's true answer) shrinks like 1 ÷ √(number of runs), so four times the runs halve it. The answer is only as good as the model: more runs make the answer more precise about the model, never more right about the world.

Four rolls of a die

The question: roll a fair die four times; what is the chance at least one six shows? A tempting answer is 4 × 1 in 6, about 2 in 3, but that counts a game with two sixes as two wins. The lesson's simulation played 10,000 games (each game is one run). The share won was 70% after 10 games, 56% after 100, 51.2% after 1,000 and 52.0% after 10,000: it swings early, then settles.

The exact answer uses the multiplication rule: when rolls are independent (none affects another), the chance that all of them do something is their chances multiplied. All four rolls miss a six: (5/6)⁴ = 625 ÷ 1,296 = 48.2%; so at least one six: 51.8%. The 10,000 games landed 0.2 percentage points away (a percentage point is the gap between two percentages). Checking a simulation on a question with a known answer shows whether it works before you trust it with one you can't work out.

How far a count misses

The simulation repeated the whole count 1,000 times for each number of games, with fresh games each time. The typical miss is the counts' standard deviation: how far a typical count lands from their average, which was close to 51.8%. In this simulation it was 9.93 points for counts of 25 games, 5.11 for 100, 2.5 for 400, 1.27 for 1,600 and 0.62 for 6,400: four times the games, about half the miss.

A year of a made-up price

A made-up price starts at €100 and, each of 252 trading days (days the market is open, about a year), moves up €1 on heads and down €1 on tails: a random walk, a running total of random steps. Of 10,000 simulated years, 2,511 ended above €110, 25.1% (error 0.43 points); counting every possible year exactly gives 24.4%, 0.7 points away, within two errors.

A question about the whole path: in how many years did the price touch €90 or lower on some day? 5,299 of 10,000, 53.0%, about 1 in 2 (error 0.5 points). 2,820 years ended at €90 or lower; the rest dipped and climbed back. Many questions about paths have no simple sum (this coin walk is an exception: its exact answer is 53.0%, found by a counting argument called the reflection principle), but a simulation just walks every day and checks.

A rule's range of outcomes

A trading rule wins 40% of its trades; a win pays 2R and a loss costs 1R (R is a fixed amount of money, like €10). Its expectancy, the average result per trade, is 0.4 × 2R − 0.6 × 1R = +0.2R, so +20R over 100 trades on average (this made-up rule pays no trading costs and its trades are independent). In 10,000 simulated accounts of 100 trades each, the average was +19.9R, yet 942 accounts (9.4%, about 1 in 10) ended below their start; the worst lost 37R and the best made 80R. After 1,000 trades none was below its start. The expectancy says where accounts head on average; the simulation shows the range around it.

Garbage in, confident answer out

Every answer is an answer about its model. Suppose the 40% came from a short backtest (the rule run over past prices) and the rule really wins 35%: its expectancy is 0.35 × 2R − 0.65 × 1R = +0.05R a trade. In 10,000 simulated accounts of 100 trades, the share below the start was 9.4% (error 0.29 points) under the assumed 40% and 39.1% (error 0.49 points; exact 38.0%, so this run landed a little high, which happens about 3 times in 100) under the true 35%. Both are precise; the first is wrong by about 30 points. Likewise, real prices have more extreme days than a fair coin gives (fat tails), so the coin walk's 53.0% describes the coin walk, not a market.

Formula: the error of a Monte Carlo answer

Score each run 1 for a hit and 0 for a miss. The share of hits is the average score, and its typical miss is the standard error (Monte Carlo error). Counts pile into a bell-shaped hill around the model's answer, so about 68 in 100 land within one error and about 95 within two. (√ is the square root: the number that, multiplied by itself, gives the number under it, √100 = 10.)

error = σ ÷ √N  ·  σ = √(p × (1 − p))
error
the standard error: how far a simulated answer typically lands from the answer that endless runs of the same model would give
N
the number of runs
σ
the standard deviation of one run's score; for a run that scores 1 or 0 it is √(p × (1 − p))
p
the chance of a hit, as a share: 0.518 for 51.8%
Worked example: the dice

p = 0.518, so σ = √(0.518 × 0.482) = √0.2497 = 0.50. For N = 100: 0.50 ÷ 10 = 0.05, which is 5 points; the 1,000 counts of 100 games measured 5.11. For N = 10,000: 0.50 ÷ 100 = 0.005, which is 0.5 points, so 52.0% sat 0.2 points from 51.8%, inside one error.

Holds whenBreaks when

An event is rarer than the runs can see: 0 hits in 1,000 runs doesn't mean a chance of 0. An error bar does not show a wrong model: a wrong model gives a wrong answer with a small error.

One more worked example

Made-up round numbers

A simulation of 900 independent runs finds that a made-up account ends below its start in 10% of them.

Spread of one run's score: σ = √(0.1 × 0.9) = √0.09 = 0.3.

Error: 0.3 ÷ √900 = 0.3 ÷ 30 = 0.01, which is 1 point.

About 95 counts in 100 like this one land within two errors, 2 points, of the model's answer.

To get the error down to 0.5 points, four times the runs: 3,600.

Common mistakes

Check yourself

  1. What is the chance of at least one six in two rolls of a fair die?
    AnswerNo six in two rolls: 5/6 × 5/6 = 25/36 = 69.4%. So at least one six: 100% − 69.4% = 30.6%.
  2. A simulation of 1,600 independent runs finds a chance of 50%. What is its error, in percentage points?
    Answerσ = √(0.5 × 0.5) = 0.5 and √1,600 = 40, so 0.5 ÷ 40 = 0.0125: 1.25 points.
  3. A simulation with 2,500 runs has an error of 1 point. How many runs bring it to 0.5 points?
    AnswerFour times as many: 10,000. The error falls with √N, and √4 = 2.
  4. The rule that wins 40% of its trades (+2R a win, −1R a loss) makes +20R on average over 100 trades. Does that mean every account ends up?
    AnswerNo. In the lesson's 10,000 simulated accounts, 942 (9.4%) ended below their start after 100 trades; the worst lost 37R.
  5. A simulation of 1,000,000 runs reports an answer with an error of 0.01 points. Why might the answer still be far from what happens in a market?
    AnswerThe error measures only luck in the runs around the model's own answer. If the model is wrong (for example a coin walk standing in for prices that have more extreme days), the answer is wrong by more than any error shows.

Try it yourself

Run the simulation. Every die, year and account in this lesson comes from one Python script with a fixed random seed (below). Change the number of runs and watch the error move with 1 ÷ √(runs).

By hand or in a spreadsheet. Roll four dice 20 times and count the games with a six. In a spreadsheet, =RANDBETWEEN(1,6) in four cells makes one game and =IF(COUNTIF(A1:D1,6)>0,1,0) scores it; copy the row down 10,000 times and average the scores.

The code behind every number (Python, seed 2026111108)
Loading…
For the preciseFor counts of 25, 100, 400, 1,600 and 6,400 games, the formula's typical misses are 9.99, 5.0, 2.5, 1.25 and 0.62 points. The exact chance of ending below the start after 100 trades is 9.13% at a 40% win rate (simulated 9.4%) and 38.0% at 35% (simulated 39.1%, about 2.2 errors away, which a count does about 3 times in 100). The year's end across 10,000 walks had a standard deviation of €15.85, against √252 = 15.87 for a ±€1 walk.

Honest notes

Where this comes from