Monday, March 13, 2023

A new journey restarted - the beginner's step - Monte Carlo Methpd

Monte Carlo Simulation, also known as the Monte Carlo Method or a multiple probability simulation, is a mathematical technique, which is used to estimate the possible outcomes of an uncertain event. The Monte Carlo Method was invented by John von Neumann and Stanislaw Ulam during World War II to improve decision-making under uncertain conditions.


Unlike a normal forecasting model, Monte Carlo Simulation predicts a set of outcomes based on an estimated range of values versus a set of fixed input values.

Example 1

Problem: the probability of three heads and two tails for a coin tossed five times (assuming a fair coin)

# Answer (from Maths Probability theory) : binomial(3 + 2, 3) 2^(-(3 + 2)) = ((3 + 2)!)/(3! 2! 2^(3 + 2))= 5/16 ≈ 0.3125

Example 2

Problem: The probability of getting three consecutive Heads at the beginning if a coin is tossed 5 times

# Answer (Using Probability theory): ½ X ½ X 1/2  = ⅛ = 0.125


The Mathematical model of Monte Carlo written in Python

Problem 1

def calculateprobability1(attempts):
success = 0
for i in range(0, attempts):
if (random.randint(0, 1) + random.randint(0, 1) +
random.randint(0, 1) + random.randint(0, 1)
+ random.randint(
0, 1)) != 3:
pass
else:
success += 1
print("Number of attempts = ", attempts)
print("Number of success = ", success)

return success / attempts


Problem 2

def calculateprobability2(attempts):
success = 0
for i in range(0, attempts):
if (random.randint(0, 1) == 1 and random.randint(0, 1) == 1
and random.randint(0, 1) == 1):
success += 1
else:
pass
print("Number of attempts = ", attempts)
print("Number of success = ", success)

return success / attempts



The Monte Carlo examples implemented in Python...

No comments: