Monte Carlo Method

Berkay Diril
4 min readMar 15, 2024

--

Unlike the cool name, as far as I know, Monte Carlo, which is one example of a stochastic model, is usually selected when others are difficult to apply.

For finance, it basically helps to explain the impact of risk and uncertainty in prediction and forecasting models, and requires assigning multiple values to an uncertain variable to achieve multiple results and then averaging(avg) the results to obtain an estimate[1]. One of the biggest handicaps is that Monte Carlo simulations assume perfectly efficient markets[1].

This method was invented by John von Neumann and Stanislaw Ulam during World War II to improve decision-making under uncertain conditions, then it was named after a well-known casino town, called Monaco since the element of chance is core to the modelling approach, similar to a game of roulette[2].

The remarkable point here is that some methods replace the uncertain variable with a single average number when faced with significant uncertainty in making a forecast or estimate. The Monte Carlo Simulation instead uses multiple values and then averages the results[3].

Here is a short video on conceptual overview;

https://www.youtube.com/watch?v=slbZ-SLpIgg

# The uniform distribution is selected to give equal pieces to all ranges.
# Furthermore,
https://statisticsbyjim.com/probability/uniform-distribution

Alternative Scenario;
Suppose that you’re a milk runner or milk distributor, delivering the fresh milk every day to certain locations meaning that you get on your truck and drive through the delivery points every day. You usually take 1 countryside point to visit per day, but, what a bit of luck, today you’ve been told you need to visit 2 of them today, and there is more, hahahaha, you also knew that your girlfriend had invited her parents for dinner, which nobody has any doubt on that you’re certainly joining to them, so you can’t be on overtime, because you would miss the dinner, which all would make your cuppy-cake and her parents upset...
It is also given that one of the tasks requires 1–5 hours, whereas the other needs 2–6 hours, and you can skip the lunch break that you would have totally 9 hours to work.
So, let’s try to see the possibility that you can check both on the same day in 9 hours without being late for dinner.

import matplotlib.pyplot as plt
import numpy as np
sims = 1000000 # the number of simulations
# the uniform distribution is selected to give equal pieces to all ranges.
# furthermore, https://statisticsbyjim.com/probability/uniform-distribution
A = np.random.uniform(1,5,sims)
B = np.random.uniform(2,6,sims)
duration = A + B
# total duration for both tasks.
# so totally, it's expected them to be finished by 3-11 hours.
# we basically have 1 million random samples.
# plt.figure(figsize = (3,1.5)) # smaller graph.
plt.figure(figsize = (6,3))
plt.hist(duration, density=True) # it puts the density
c = 9 # how many hours do you have to finish them?
# if it takes more than 9 hours, you'll be late for dinner.
plt.axvline(c, color='r')
# drawing a line to vertical axis to point a condition.
# the number as indicator, separator, color of the line.
plt.show()
print((duration > c).sum()/sims)
# the ratio of values bigger than 9(c) to the number of total simulations.
# meaning that by the chance of __, it can get a value greater than 9.
print('Meaning that by the chance of {}%, it can get a value greater than {}. '.format(((duration > c).sum()/sims),c))

The density parameter, which normalises bin heights so that the integral of the histogram is 1. The resulting histogram is an approximation of the probability density function[4].

# function version tidied up
def chances(maxhours):
sims = 1000000 # the number of simulations
A = np.random.uniform(1,5,sims)
B = np.random.uniform(2,6,sims)
duration = A + B
plt.figure(figsize = (6,3))
plt.hist(duration, density=True) # it puts the density
plt.axvline(maxhours, color='r')
plt.show()
print((duration > maxhours).sum()/sims)
print('Meaning that by the chance of {}%, it can get and need a value greater than {}. '.format(((duration > c).sum()/sims),c))
chances(9) # assumed we need 9 hours.

Basically, if we assume one of them takes time between 1–5 hours and the other needs 2–6 hours, then supposing you have c = 9 hours to finish your duties without overtime and missing the dinner, we can say that by chance of 0.12%, you may miss the dinner and need more than 9 hours to work.
# This c value, meaning the maximum working hours to not be late , can be adjustable by the user, and assigned as the independent variable.

The Monte Carlo Simulation and Machine Learning
Machine learning (ML) is a computer technology that uses a large sample of input and output (I/O) data to train software to understand the correlation between both. A Monte Carlo simulation, on the other hand, uses samples of input data and a known mathematical model to predict probable outcomes occurring in a system. You use ML models to test and confirm the results in Monte Carlo simulations[5].

Here is what probability distribution Monte-Carlo Method -most likely- would result by AWS’s words as a quick final reminder;

https://aws.amazon.com/what-is/monte-carlo-simulation/

References:

  1. https://www.investopedia.com/terms/m/montecarlosimulation.asp
  2. https://www.ibm.com/topics/monte-carlo-simulation
  3. https://www.investopedia.com/terms/m/montecarlosimulation.asp
  4. https://matplotlib.org/stable/gallery/statistics/histogram_features.html
  5. https://aws.amazon.com/what-is/monte-carlo-simulation

--

--