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 notesFour 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?
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.
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?
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.
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.
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
- 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).
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.
- 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.
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?
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.
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?
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.
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 walk
For 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?
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.
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.
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.
- 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.
- Check the method, then give each answer with its error.
First run it on a question with a known answer, as with the dice. Then report each answer with its error, σ ÷ √N, when the runs are independent: four times the runs halve it.
- See how far the answer moves when the model changes.
No number of runs fixes a wrong model. Run again with the inputs you are unsure of, such as 35% instead of 40%, and see how far the answer moves. Monte Carlo, a later lesson in Backtesting Essentials, uses the same tool on a backtest's own trades.
A friend's simulation
What is the error of that 20%, in percentage points?
Hint: first σ = √(p × (1 − p)) with p = 0.2; then divide by √N.
Twice as precise
How many runs do you need?
A tiny error
What does the small error tell you?
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.