Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll talk about Monte Carlo simulation.
Monte Carlo Simulation is a technique that approximates the solution to a problem through
statistical sampling.
It is used to measure how uncertainties, that means, probability distributions of parameters,
spread from model inputs to model outputs.
In Monte Carlo Simulation, a model is simulated a large number of times.
Each simulation is referred to as a realization, and it represents a possible "future" of the
system.
The most famous application of Monte Carlo Simulation is in solving key problems when
developing the atomic bomb in the Manhattan Project in the 1940s.
It was named after the Monte Carlo Casino in Monaco, where many games of chance involve
repetitive events with known probabilities.
I'll use two examples to demonstrate how to use functions in Python's random library to
implement Monte Carlo simulation.
The first example is Calculating pi.
The value of pi can be approximated by counting dots.
The basic idea is demonstrated in the following image.
We know that the ratio between the area of the small green square and the area of the
large orange square is 1/4.
Therefore, if we randomly spread 100,000 dots over the large square, about 25,000 dots will
be inside of the small square.
Now consider the following image.
The ratio between the area of the circle and the area of the square is area circle over
area square equals pi r squared over 2r squared, which is pi over 4.
Let's re-write this equation.
pi equals 4 times area circle over area square.
This area ratio can be approximated using the ratio between the number of dots inside
of the circle and the number of dots inside of the square.
So, pi approximately equals 4 times the number of dots in the circle over the number of dots
in the square.
Let's see the program.
We will need to use some of the functions in the random and math libraries.
So, let's import these libraries first.
Then we initialize the number of dots in the circle and the number of dots in the square
to be zero.
We use this for loop to simulate this for one million times.
For each iteration, we generate one dot in the square of the size one by one.
If one dot is located within the circle with a radius one, then, n circle is increased
by one.
Every dot is within the square, so n square is increased by one.
After the simulation, we calculate pi's value using this formula.
Finally, we print the approximate value of pi based on the simulation and the exact value
of pi from the math library so we can compare.
It turns out, these two numbers are very close.
Another example of Monte Carlo simulation is the Monty Hall problem.
The Monty Hall Problem is a puzzle based on a live TV game show in the 1960s.
It was named after the game show host, Monty Hall.
For a detailed description of the Monty Hall problem and how to solve it, you can watch
these two YouTube videos.
The problem is described as follows.
A contestant is given the chance of three doors.
Behind one door is a sports car, which the contestant really wants to win; behind the
other two doors are goats.
Of course, the doors are not transparent glass doors like in these images.
The contestant picks a door, say number 1, and then the host, who knows everything, opens
a losing door, say number 2, which has a goat.
The host then asks whether the contestant would like to stick to the original choice
or switch to the remaining unopened door number 3.
It turns out it is always better to switch.
Let me explain why.
The first time the contestant picks a door, the chance of winning is 1/3.
That means, there is a 2/3 chance that the car is somewhere else.
That is, behind one of the other two doors.
The host then eliminates one door that does not have the car.
Thus, there is a 2/3 chance that the car is behind the remaining door, and the contestant
better switches.
However, this is not to say that this strategy guarantees the contestant wins the car.
It just means, by switching, the chance of winning is changed from 1/3 to 2/3.
We use Monte Carlo simulation to solve this problem.
First, we import the random library.
Then we generate a list of two goats and one car.
The total number of simulation is 100,000 times.
Switch wins represents how many times the contestant will win out of these 100,000 simulations
if he or she always switches.
Stick wins represents how many times the contestant will win if he or she always sticks to the
original choice.
Their initial values are both 0.
We do the simulation using the for loop.
For each simulation, the objects behind the doors are shuffled so that the car is placed
randomly behind one of three doors.
Next, the contestant randomly picks one of these three doors.
Say, this door is number k.
If the original choice has no car, then the contestant will win if he or she switches.
The number of switch wins will be increased by one.
Otherwise, the original choice has the car.
Then the contestant will win if he or she sticks.
The number of stick wins will be increased by one.
After the 100,000 simulations, we calculate the winning probability if the contestant
always switches and the winning probability if the contestant always sticks to the original
choice.
This 0.67 is about 2/3 and this 0.33 is about 1/3, which confirms our analysis.
In this program, we don't even need to simulate which door the host picks because it doesn't
matter.
All that matters is the contestant's initial pick k.
What if there are 10 doors instead of 3?
How should we change the program?
I'll just leave this for you to think about and practice.
Okay, that was about Monte Carlo simulation.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.