[DeepHomeBrew] Numerical, Monte Carlo Methods: Calculating Pi

Conrad Salinas
3 min readDec 19, 2019

--

Info:

Monte Carlo methods are an awesome class of methods that approach problems with no prior knowledge of the environment. Often called the model-free approach.

A lot of times they can be used as numerical approximations of something that cannot be solved exactly such as an mathematical equation or model because it can be complex and non-trivial. But somehow… Using brute force and the Law-of-Large Numbers we can get close to an approximate estimate and this can be fine for most problems.

A Monte Carlo method is any method that uses randomness to solve problems. The algorithm repeats suitable random sampling and observes the fraction of samples that obey particular properties in order to make numerical estimations. [6.1].

Yuxi (Hayden) Liu,
PyTorch 1.x Reinforcement Learning Cookbook

Example:

A great example is to use Monte Carlo to calculate the humble π.

Givens:

Assume a square has an area of 4.

Area = Length * Width

Length = 2

Width = 2

Centered at origin

Range [-1<=x<=1 and -1<=y<=1]

Assume a circle inside the square and our target is π.

Area = π * radius²

radius = 1

Range [-1<=x<=1 and -1<=y<=1]

Calculate Area Square

Area = Length * Width = 2 * 2

Area = 4

Calculate Area Circle

Area = π * radius² = π * 1²

Area = π

Derive:

Given what we know above we can do some tricks and arrive to the elusive π estimation.

Ratio = Area Circle / Area Square

Ratio = (π * radius²) / (Length * Width)

Ratio = π / 4

Above is only 1/4 of the problem. We need to multiply the Ratio by 4 to give π .

π = Ratio * 4

π = (π / 4) * 4

Implement:

Results:

Notice that around the 4,000 iteration π converges pretty good.

References:

--

--

No responses yet