statistics

The Monte Carlo Method Explained Simply with Real-World Applications

What is the Monte Carlo method

The story of the Monte Carlo method begins in the most unlikely way: with a mathematician in bed playing cards. In 1946, Stanisław Ulam, a Polish mathematician recovering from surgery, found himself playing solitaire to pass the time. Being a mathematician, he wondered: what are the chances of winning a game?

The problem was theoretically solvable: just enumerate every possible combination of cards and count the favorable ones. In practice, however, the number of combinations was so enormous that an exact calculation was completely impractical. Ulam then had an insight as simple as it was powerful: instead of computing the exact probability, why not simulate hundreds of games and count how many times you win?

The idea is disarmingly simple. If we play 1,000 games and win 230 of them, we can estimate the probability of winning at about 23%. The more games we simulate, the closer our estimate gets to the true value. This is, in essence, the Monte Carlo method: using random simulation to solve problems that would be too complex to tackle analytically.

Ulam shared the idea with his colleague John von Neumann, arguably the most brilliant mathematician of the 20th century, who immediately saw its potential. Von Neumann realized that ENIAC — one of the very first electronic computers, which filled an entire room — could run thousands of simulations in reasonable time. Together, they developed the method for a problem far more serious than solitaire: the diffusion of neutrons in atomic weapons, as part of the Manhattan Project at Los Alamos.

The name “Monte Carlo” was chosen as a code name, a reference to the famous Monte Carlo Casino in Monaco. Legend has it that the inspiration came from Ulam’s uncle, a notorious gambler. After all, the heart of the method is chance itself: generating random numbers to explore spaces of possibility too vast to traverse systematically.

From those early nuclear experiments of the 1940s, the Monte Carlo method has spread to every field of science and engineering. Today it is one of the most widely used computational tools in the world, from particle physics to finance, from cinematic rendering to drug discovery. Let’s see how it works.

What we’ll cover:

Preliminary concepts: chance, large numbers, convergence

The Monte Carlo method rests on a statistical principle we’ve encountered before: the law of large numbers. In simple terms, this law tells us that the average of a random sample approaches the population average as the sample grows. Translated into Monte Carlo language: the more simulations we run, the more accurate our result will be.

To run a Monte Carlo simulation, we need random numbers. In practice, computers don’t generate truly random numbers: they use deterministic algorithms that produce sequences of pseudo-random numbers with statistical properties indistinguishable from real randomness.
In R, for example, the runif() function generates uniformly distributed numbers between 0 and 1. Their being deterministic has a valuable upside: by fixing the seed with set.seed(), anyone who re-runs the same code gets exactly the same numbers — which is why the results we’re about to see are reproducible to the decimal.

A crucial aspect is the rate of convergence. The Monte Carlo estimation error decreases as 1/√n, where n is the number of simulations. This means that to halve the error, we need to quadruple our simulations; to gain one more decimal digit of precision, we need 100 times more iterations.
It’s not particularly efficient, but the beauty of the method lies in the fact that it works regardless of the problem’s complexity: whether the problem has 2 or 2,000 variables, the convergence rate remains the same.

On the left the estimate of π settles as the number of simulations grows; on the right the typical error falls along the 1/√n line, measured over many independent repetitions.

In practice, we must always balance desired precision with available computational resources. Increasing the number of simulations comes at a cost in computation time. Fortunately, modern computers make this trade-off much more favorable than in the days of ENIAC.

The method in four steps

Let’s see concretely how the Monte Carlo method is applied. The procedure follows four fundamental steps.

1. Define the model. First, we identify the problem’s variables and the probability distributions that govern them. For instance, if we want to simulate an investment’s return, the model will include the expected return (mean) and volatility (standard deviation), typically assuming normally distributed returns.

2. Generate random scenarios. Using a pseudo-random number generator, we produce thousands of possible scenarios. Each scenario represents an “alternative history”: one way things could play out.

3. Compute the result for each scenario. For each scenario, we apply the model and obtain a result. If we’re simulating an investment, the result is the final portfolio value.

4. Aggregate the results. Finally, we analyze the set of results: we compute the mean, the median, the percentiles. This gives us not just an estimate of the expected outcome, but an entire distribution of possibilities. And this is where Monte Carlo truly shines: it tells us not only “how much we’re likely to earn” but also “how much we could lose in the worst case.”

Let’s use a quick example to illustrate convergence. Imagine flipping a coin and trying to estimate the probability of heads. After 10 flips, we might get 7 heads (70%), an estimate far from the true 50%. After 100 flips, we’ll be closer, perhaps 53%. After 10,000 flips, our estimate will be very close to 50%. This is Monte Carlo in action: replacing a theoretical calculation with an experiment repeated thousands of times.

The power of the method lies in its flexibility. While analytical methods require closed-form solutions (which often don’t exist for complex problems), Monte Carlo only requires the ability to simulate the process. If we can write a program that generates one scenario, Monte Carlo gives us the distribution of outcomes.

Example 1 — estimating π with random numbers

The most classic and pedagogically effective example of the Monte Carlo method is estimating the number π. The idea is elegant: consider a square of side 2 with a circle of radius 1 inscribed inside it. The area of the square is 4, the area of the circle is π. If we generate random points inside the square, the proportion falling inside the circle will be approximately π/4.

I compute this in R with 100,000 points:

set.seed(123)
n <- 100000
x <- runif(n, -1, 1)
y <- runif(n, -1, 1)
inside <- (x^2 + y^2) <= 1
pi_estimate <- 4 * sum(inside) / n
pi_estimate
# [1] 3.14632

The same in Python:

import random
random.seed(123)
n = 100000
inside = sum(1 for _ in range(n)
             if random.uniform(-1, 1)**2 + random.uniform(-1, 1)**2 <= 1)
pi_estimate = 4 * inside / n
print(pi_estimate)
# 3.13688
100,000 random points in the square: the share falling inside the inscribed circle, multiplied by 4, estimates π (here 3.146).

With 100,000 points we already get a reasonable estimate, though not extremely precise: we’re accurate to about two decimal places. As we can see, R and Python start from the same seed but use different generators and land on slightly different numbers (3.146 versus 3.137): both dance around the true π by the amount the 1/√n curve predicts. Gaining another digit of precision would require roughly 100 times more points. The computer does all the heavy lifting.

Example 2 — the risk of a portfolio

Let’s move to an example closer to real-world applications. Suppose we have a portfolio of three stocks with the following characteristics:

StockExpected returnStandard deviationPortfolio weight
A8%12%40%
B10%15%30%
C12%18%30%

We want to estimate the probability that the portfolio return exceeds 10%. I simulate in R with 10,000 scenarios:

set.seed(42)
sim_A <- rnorm(10000, mean = 0.08, sd = 0.12)
sim_B <- rnorm(10000, mean = 0.10, sd = 0.15)
sim_C <- rnorm(10000, mean = 0.12, sd = 0.18)
sim_portfolio <- 0.4 * sim_A + 0.3 * sim_B + 0.3 * sim_C
prob_result <- mean(sim_portfolio >= 0.10)
prob_result
# [1] 0.4903

The same in Python:

import random
random.seed(42)
n = 10000
count = 0
for _ in range(n):
    a = random.gauss(0.08, 0.12)
    b = random.gauss(0.10, 0.15)
    c = random.gauss(0.12, 0.18)
    ptf = 0.4 * a + 0.3 * b + 0.3 * c
    if ptf >= 0.10:
        count += 1
print(count / n)
# 0.4898

The result tells us there’s roughly a 49% chance of exceeding a 10% return. But the truly valuable thing is not this single number: it’s the entire distribution that Monte Carlo puts before our eyes. From it we also read the median return, the worst-case 5th percentile and — anything but a minor detail — the probability of closing at a loss, which here is close to 13%.

The 10,000 simulated portfolio returns: about 49% exceed the 10% target, about 13% close at a loss.

Example 3 — how many clicks does climbing the rankings really bring

So far physics and finance; but Monte Carlo is at home in our field too. Let’s use a quick example. We’re weighing whether it’s worth investing to move a page from ninth position to third on an interesting keyword. The client’s question is blunt: “how many extra clicks does it bring?”. The temptation is to answer with a back-of-the-envelope figure — average monthly impressions times average CTR in third position:

12000 * 0.11
# [1] 1320

Thirteen hundred clicks a month, a clean and reassuring number. Too bad it’s falsely precise: it hides two uncertainties the size of a house. Impressions are not a constant — they swing with seasonality and market demand — and the expected CTR in third position is not a fixed number, but depends on the SERP, on the presence of elements like AI Overviews, on search intent. Instead of multiplying two averages, we feed Monte Carlo the uncertainty of both.

I simulate ten thousand scenarios in R, with impressions around 12,000 (standard deviation 2,500) and CTR around 11% (standard deviation 3 points):

set.seed(7)
n <- 10000
impressions <- rnorm(n, mean = 12000, sd = 2500)
impressions[impressions < 0] <- 0
ctr <- rnorm(n, mean = 0.11, sd = 0.03)
ctr[ctr < 0] <- 0
clicks <- impressions * ctr

mean(clicks)                     # ~ 1323 clicks/month
median(clicks)                   # ~ 1282
quantile(clicks, c(0.05, 0.95))  # ~ 632 ... 2139
mean(clicks > 1500)              # ~ 0.33

The mean (about 1,320 clicks) matches the back-of-the-envelope figure, and so far no surprise. But the simulation adds exactly what the single number erased: the plausible scenarios range from about 630 to about 2,140 clicks a month, and if the project only breaks even above 1,500 clicks, the probability of clearing that bar is barely one in three. It’s the same histogram as the portfolio example, applied to clicks instead of returns — and it tells a story that “1,320” on its own could not.

An honest SEO forecast is not a number, it’s a distribution: it says how much we can expect and how wide the uncertainty around that expectation is. Bringing that width to the client’s table, instead of hiding it behind a round figure, is what separates a promise from a defensible estimate.

The simulator: geometric Brownian motion

To make the concept even more tangible, we’ve built an interactive simulator that applies the Monte Carlo method to predict the future value of an investment. The underlying model is geometric Brownian motion (GBM), the same model used in the famous Black-Scholes framework for options pricing.

Intuitively, an asset’s future price is computed as the current price multiplied by a random growth factor. The formula that generates one step of the path is:

\( S_{t+1} = S_t \cdot \exp\left( \left(\mu – \frac{\sigma^2}{2}\right)\Delta t + \sigma \sqrt{\Delta t}\; Z \right) \\ \)

where μ is the expected annual return (the “average growth”), σ is the volatility (how much the price fluctuates — our measure of uncertainty), Δt is the width of the time step, and Z is a random number drawn from a normal distribution. Each simulation generates a different path: some scenarios see the portfolio grow substantially, others see it decline.

Simulated paths of an investment’s value over one year: the 5th–95th percentile band widens over time while the median grows slowly.

The chart above shows the fan of possible paths; the simulator below lets you turn the knobs — return, volatility, horizon — and watch how the distribution of final outcomes changes.

Where the Monte Carlo method is used today

From the nuclear physics of the 1940s, the Monte Carlo method has spread to domains that Ulam and von Neumann could never have imagined. Let’s look at some of the most fascinating applications.

3D rendering and cinema. Every time we watch a Pixar film or a blockbuster with visual effects, we’re admiring Monte Carlo at work. The technique is called path tracing: to compute the color of each pixel, the software simulates millions of light rays bouncing between surfaces in the scene. Each ray follows a random path, and the average of thousands of paths produces the photorealistic image we see on screen.

Finance and risk management. In the financial world, Monte Carlo is ubiquitous. Banks use it to calculate Value at Risk (VaR) — the maximum probable loss of a portfolio over a given time horizon. It’s the same principle as our simulator, applied to portfolios with hundreds of assets and complex correlations. Pricing exotic options that lack closed-form solutions also relies on Monte Carlo simulations.

Drug discovery. In pharmaceutical research, Monte Carlo is used to simulate molecular docking: how a candidate molecule binds to a target protein. By simulating millions of possible spatial configurations, researchers identify the most promising compounds before synthesizing them in the lab, saving years of experimentation.

Climate models. Models predicting climate change are inherently uncertain: they depend on emission scenarios, atmospheric feedback, ocean dynamics. Monte Carlo allows exploration of thousands of parameter combinations and generates the uncertainty bands we see in IPCC reports. Not a single prediction, but a distribution of possible futures.

Artificial intelligence. In machine learning, a technique called Monte Carlo dropout uses simulation to estimate the uncertainty of a neural network’s predictions. And the famous AlphaGo by DeepMind, which in 2016 defeated the world Go champion, used Monte Carlo Tree Search (MCTS) to explore possible moves in a game with more configurations than atoms in the universe.

FieldExampleWhat is simulated
Cinema/3DPath tracing (Pixar)Light ray paths
FinanceValue at RiskMarket scenarios
PharmaceuticalsMolecular dockingSpatial configurations
ClimateIPCC modelsParameter combinations
AIAlphaGo (MCTS)Possible moves

Advantages, limits and the caveat to remember

Like any statistical tool, the Monte Carlo method has its strengths and limitations. Let’s examine them honestly.

Flexibility. The greatest advantage is versatility: Monte Carlo applies to complex problems of any size and in any field, from finance to engineering, physics to biology. It doesn’t require closed-form solutions, only the ability to simulate the process.

Accuracy. With a sufficient number of simulations, the estimate can be made arbitrarily precise. The more we run the method, the closer the result converges to the true value.

Scalability. Unlike grid-based methods, which suffer from the “curse of dimensionality” (cost explodes with the number of variables), Monte Carlo maintains the same convergence rate regardless of the number of dimensions. This makes it the only practical tool for high-dimensional problems.

However, the method also presents significant limitations. The first is slow convergence: the 1/√n rate means that gaining one digit of precision requires 100 times more simulations, and for problems demanding very high precision this can be prohibitive. The second is computational cost: for complex problems each individual simulation may require significant time, and multiplied by millions of iterations the bill becomes steep.

There is, however, a more insidious limit, one that concerns not the computation but the reasoning:

A word of warning: Monte Carlo does not create information out of nothing, it propagates it. The distribution of results is reliable exactly as much as the distributions we feed into the model. If we get the means, standard deviations or correlations of the inputs wrong, we’ll get an output that is precise in form but false in substance — the classic garbage in, garbage out. And the thousands of simulations lend an illusion of rigor that the underlying model may not deserve: the hard part isn’t running the code, it’s choosing the distributions well.

To mitigate the limits of cost and convergence, variance reduction techniques have been developed over the years, enabling more precise results with fewer simulations:

  • Importance sampling: sampling from an alternative distribution that “concentrates” simulations in the most informative regions.
  • Control variates: using a correlated variable with known expected value to reduce the estimate’s variance.
  • Stratified sampling: dividing the space into homogeneous subgroups and sampling from each.
  • Antithetic variates: exploiting pairs of negatively correlated random numbers to reduce variance.

The Monte Carlo method represents one of the most powerful tools in computational statistics. In future articles, we’ll explore how some of these techniques — particularly the bootstrap, a close relative of Monte Carlo — apply to concrete problems in statistical inference.


Further reading

For a deeper dive into the Monte Carlo method and its applications in finance, Monte Carlo Methods in Financial Engineering by Paul Glasserman is the most comprehensive reference: it covers theory and practice with detailed examples in derivative pricing and risk management. For the more popular side — the idea that a good forecast is a distribution of scenarios rather than a single number — The Signal and the Noise by Nate Silver is an excellent read on probabilistic reasoning applied to prediction.

Paolo Gironi

Recent Posts

Keyword Clustering: grouping thousands of queries with K-means and hierarchical clustering

It happens with every reasonably serious project: you export the keyword list from Search Console…

3 weeks ago

Expected vs Actual CTR: finding the pages that earn fewer clicks than their position deserves

Anyone who spends their days inside Search Console knows that little nagging feeling: a page…

3 weeks ago

Naive Bayes: classifying search intent with Bayes’ theorem

In the article on the multi-armed bandit we used Bayes to decide between variants: shifting…

3 weeks ago

Multi-armed bandit: optimising the variants while the test is still running

In the article on Bayesian A/B testing we compared two variants at a fixed sample…

4 weeks ago

Bayesian A/B Testing: not just “whether” B beats A, but “by how much”

In the article on classic A/B testing we saw how to compare two variants with…

4 weeks ago

Bayesian Conversion Rate Estimation: how much can we trust limited data

In the article on the foundations of Bayesian statistics, we saw how Bayesian updating works…

4 weeks ago