Monte Carlo Method

Stumbled into a stats lecture and finally got it — turns out if you scatter enough random dots and do a little ratio math, you can actually calculate π. Wild, right?

The Monte Carlo method is…

hmm… use repeated random samples to estimate an expected value or an integral. With independent samples of finite variance, the usual sampling error shrinks like $1/\sqrt{N}$ — although one particular estimate need not improve at every step.

Feels kinda like the law of large numbers, right?

Back in the day when I was shepherding high school kids around campus, walking them from department to department for major intros — when I dragged them into the statistics department —

that’s where they explained this “Monte Carlo Method.”

And dang, I caught myself actually paying attention lol lol lol lol

Up till then I’d only ever heard the name and had no real clue what it was, but those stats students broke it down clean lol lol lol

The name points to Monte Carlo in Monaco, famous for its casino. A fitting name for a method that uses random samples, heh.

The little geographical detour I copied from Naver Knowledge Encyclopedia — the Dictionary of World Place-Name Origins (Seongji Munhwasa, February 1, 2006) — described Monte Carlo as one of Monaco’s four districts, across the harbour from Monaco City. It connected the casino authorization by Prince Charles III with its fame as a Mediterranean resort, dated its renaming from Spelugues to 1866, and mentioned hotels, theatres, apartments, motor racing and sports festivals. That’s the older reference’s description I was reading, not a claim that its administrative classification is current. Quite a detour before we even start calculating, lol.

And now, in academia —

the modern computer method developed at postwar Los Alamos — in the simplest possible terms:

I had introduced it with the shorthand ‘this method started while making an atomic bomb.’ That was the weapons-research association in my notes; ‘postwar Los Alamos’ above is the more careful setting for the modern computer method, not a claim that every random-sampling idea began with a bomb.

(for the actual details, hit Wikipedia or some other blog.)

Equation from the original notes

For this pi example, sample points uniformly over a region containing the square. Count the points inside the square, then how many of those are also inside its inscribed circle. The circle-to-square count ratio estimates the area ratio $\pi/4$.

Just — fire away, randomly.

You can absolutely make a computer plot dots randomly.

Run it dead simple, and when the computer drops 5,000 dots,

it counts how many landed inside the square. Apparently around 1,917.

OK now, which dots are we counting —

Equation from the original notes

we count the dots that fell inside a circle of radius r, like this.

When you count, apparently about 1,503 get caught.

And then when you take the ratio:

Equation from the original notes

You get this. And what we just calculated is…. (an Entry block-coding project )

Equation from the original notes

Apparently that’s what we calculated…..

Whoa. So what happens if we multiply by 4…?

Equation from the original notes

Yep…. apparently you can compute π this way.

We’re not at 3.14 yet, but they say if you crank the number of trials up more and more and more and more and more and more, you can pin down the ratio to a truly astonishing degree.

(Yeah. I tried it lol lol lol lol)

Estimating Pi Using the Monte Carlo Method in Python (Financial Engineering Programming #23)

Calculating pi using the Monte Carlo method (Calculate pi by using Monte Carlo Method)

Watch the video on YouTube It’s slow when you actually run it~ the video is though lol lol lol so…

gdpresent.blog.me

… Are you starting to get a feel for what the Monte Carlo method is going for??? lol lol lol lol lol

So I want to use that kind of method here — but not with the formula from before. With a different one.

Quick recap before we move on, though.

Brownian motion from physics, when it crossed over into the financial world,

Equation from the original notes

morphed into this. And

in real life, measuring something like dt or dS — that kind of crazy-tiny infinitesimal change — is basically impossible,

so I figured: change dt, into something a bit chunkier, Δt, and dS into ΔS.

So I just went ahead and swapped them. Boldly.

Equation from the original notes

After that swap, a gentleman named Wiener —

a gentleman who is not a loser — proved through statistical properties that

Equation from the original notes

we get this property. (And ε ~ N(0, 1).)

So the finished formula came out as

Equation from the original notes

and we even coded it up.

But — that finite-step formula is an Euler approximation to GBM, not its exact transition.

The continuous-time model is still the same GBM stochastic differential equation:

Equation from the original notes

this one, and

Equation from the original notes

the continuous-time equation specifies that model.

Replacing the continuous-time equation with

Equation from the original notes

means stepping

Equation from the original notes

on a finite grid. That introduces discretization error; it does not mean the underlying GBM equation was wrong.

So — using the famous Itô’s Lemma —

applied to $\ln S$, we obtain the exact finite-step transition for constant $\mu$ and $\sigma$:

Equation from the original notes

This is the exact transition of the same GBM model, rather than a different, more accurate model of the real market. Keep that distinction in view!

Now to actually see how the two values differ from each other,

we can code it up and check with our own eyes.

Shall we?

Equation from the original notes

Equation from the original notes

Same starting value,

St is the Euler approximation to a GBM stock-price path, not the Wiener process itself,

and St2 uses the exact GBM transition with the same normal increments. Blue and orange.

Now use independent exact-transition samples to estimate the model’s expected terminal stock price. In this first example, the variable named r is simply the chosen scenario drift, 0.15; it need not be the real stock’s risk-free rate.

My original sketch’s declaration line was literally Dim ~~~~ too annoying ~~~~ As Double lol. Very diligent of me… The version below actually declares the variables so it can run!

Sub monte_carlo_thing()
    Dim S As Double, v As Double, r As Double, T As Double
    Dim vvt As Double, u As Double, this_Gaussian As Double
    Dim this_spot As Double, running_sum As Double, mean As Double
    Dim iter As Long, i As Long

    S = 100: v = 0.3: r = 0.15: T = 1: iter = 5000
    vvt = v * v * T
    running_sum = 0
    Randomize

    For i = 1 To iter
        Do
            u = Rnd()
        Loop Until u > 0
        this_Gaussian = WorksheetFunction.NormSInv(u)
        this_spot = S * Exp((r - 0.5 * v * v) * T + Sqr(vvt) * this_Gaussian)
        running_sum = running_sum + this_spot
    Next i

    mean = running_sum / iter
    Debug.Print mean
End Sub

Setting v — volatility — to 0.3 as the standard deviation of returns, fine, no surprises there.

But I was kinda curious why the expected return μ got written as r specifically,

for option valuation under the risk-neutral measure, the no-dividend drift is set to the risk-free rate. That does not say the stock’s real-world expected return equals the risk-free rate. The first stock-mean example can instead use whatever physical drift the scenario assumes.

In the code above, this_Gaussian

is just the same NormInv(Rnd(), 0, 1) thing from before,

and this_spot is one terminal price after time $T$ from the same starting price $S$, not a price change.

After For → Next, i bumps to 2 and we draw a fresh shock for another independent terminal-price sample.

We do not feed the previous sample back as the next starting value. The retained picture below instead illustrates a successive step along one path:

Equation from the original notes

Our loop, by contrast, always uses the unchanged initial S; each fresh terminal price gets added to running_sum,

then through Next again — a third independent terminal sample is added to running_sum,

and as 5,000 this_spots pile up into running_sum like that,

at the very end, all 5,000 get divided by 5,000 to get the average!!!

And that’s what we call mean!!!

Eek!

What does it mean….

Here’s how I’m thinking about it.

Like before — instead of trusting some single $S_t$ that fell out of running the simulation once,

run it 5,000 times —

no, 10,348,319,840,913,890,438,109,809 times — and trust the average of all those. That feels more trustworthy….. yeah, something like that is what I’m getting at.

Using the Monte Carlo method (which carries this philosophy),

this time let’s also try estimating the price of a Call option.

But — the call payoff at maturity is $\max(S_T-K,0)$, not the option’s present price. Average those payoffs under the risk-neutral model and discount to get the price… heh.

I’ll just throw the code at you again. Here r is the risk-free rate and there are no dividends. Pass the desired S, k, r, v, T and positive integer iter; do not overwrite them inside the function. Call Randomize once in the caller before a simulation run, rather than reseeding at every valuation.

Function monte_carlo_Euro_call(S, k, r, v, T, iter) As Double
    Dim vvt As Double, u As Double, this_Gaussian As Double
    Dim this_spot As Double, this_payoff As Double, running_sum As Double
    Dim mean As Double, i As Long

    vvt = v * v * T
    running_sum = 0
    For i = 1 To iter
        Do
            u = Rnd()
        Loop Until u > 0
        this_Gaussian = WorksheetFunction.NormSInv(u)
        this_spot = S * Exp((r - 0.5 * v * v) * T + Sqr(vvt) * this_Gaussian)
        this_payoff = WorksheetFunction.Max(this_spot - k, 0)
        running_sum = running_sum + this_payoff
    Next i

    mean = running_sum / iter
    monte_carlo_Euro_call = mean * Exp(-r * T)
End Function

OK first thing — we need to distinguish between Sub procedures and Function procedures,

because the one above isn’t a Sub like everything we’ve done so far. It’s a Function.

Sub procedure: think of it as a function, but no return value.

Function procedure: this one has a return value.

I think it’s enough to just file it away as “the one with a return value” and move on.

So inside the For loop up through this_spot, it draws independent terminal stock prices from the same initial S,

and on top of that we added this_payoff and slapped a condition on it.

So when i = k,

Equation from the original notes

if we say that, then running_sum is

Equation from the original notes

this. But each individual

Equation from the original notes

has to pass the “is it positive?” check to have a value,

and when something negative pops out, we just shove it to 0, so among all those

Equation from the original notes

there’ll definitely be some zeros mixed in. That’s the deal.

Anyway,

Equation from the original notes

with those mixed in too,

we sum up all the

Equation from the original notes

and take the average lol lol lol lol lol.

That average then gets discounted back to present value at the end,

and the return value of the declared Function gets defined as exactly that.

And the book takes this Function we just built and uses it inside another Sub procedure,~~~~

Looks like we’ll have a chance to actually use it when we get to the 5 Greeks.

That’s it for today!

Questions or corrections?

Did a step lose you, or does a number look off? Email me or use the contact page. Include the note title and the step or figure you mean.

Feedback goes to me privately by email; it is not posted as a public comment.