Today's video I want to cover with you. Monte Carlo Simulation. Monte Carlo simulation is used
widely in fields such as engineering, physics and finance. We'll cover how this intuitive framework can solve challenging
probabilistic questions, and I'll show you how we can analyze a real problem
using Monte Carlo in Python. And we'll do all of this in just over 5 minutes,
so let's jump right into today's topic. To introduce Monte Carlo simulation,
let's consider the following situation. It's 9 a.m. on a Thursday when your boss, Todd,
comes into the office demanding that you have two reports on his desk
by end of day. Now, let's just say that
Todd is not the easiest boss to work for, but his demands today are particularly frustrating to you
as you scheduled months ago a meet up with your friends and family
that's taking place at 6 p.m. Now, for better or worse,
you want to keep your job, but you also don't want to miss out
on the event that is in just 9 hours. And so in pondering what to do next,
you ask yourself, ‘what is the chance that you could even
finish these two reports in time for the party?’ And ‘how
could you actually even go about computing that chance?’ Now,
to answer this question, we are going to rely on Monte
Carlo simulation, and what's wonderful about this technique is that we can answer
challenging probabilistic questions like this one with limited background
actually in probability. So let's go back to our situation. Todd is expecting that you have
two reports on his desk by end of day. Based on past experience,
you're going to make a couple of guesses. These two reports will take anywhere
between 1 to 5 and 2 to 6 hours, respectively,
to complet, and your performance on one report
has no bearing on the other. So those assumptions are going to inform
the first step of Monte Carlo simulation, which is we will come up with some measure
of uncertainty for each uncertain factor
underlying our decision-making process. So based on a quick search online, you decide to represent the time
to complete each report as following what's called a uniform distribution,
which is a really intuitive idea. The probability distribution
assumes the relative likelihood of all outcomes between a defined min
and a defined max are the same. Now, here is where the beauty and simplicity of Monte
Carlo simulation comes into play. What we are going to do
now is rely on a computer to randomly pick the time to complete each report. Those selected values are going to be
based on a defined uniform distributions, which is why it is really important
to make sure that you come up with a good estimate
of those distributions at the start. Now, let's speed this process up and do it
maybe thousands, maybe millions of times. And we're going to store
all of these randomly selected values. And once we do that, we can use
those stored results to approximately estimate the distribution around the time
to complete both reports, which we can now use to figure out the chance
we'll make it to the party in time. And that's really the crux of Monte
Carlo simulation. And so you can imagine for problems
with many sources of uncertainty, Monte Carlo simulation provides
a simple way to analyze complex systems. So to really appreciate Monte Carlo
simulation, let's quickly show how you can implement it, and I'm going to do
so in this video through Python. To first go about conducting our Monte
Carlo simulation, we're going to rely on a library called NumPy, which has available functionality
to conduct Monte Carlo simulation. So I'm going to import NumPy and propose
that we do 1000, ehh, let's just make it 1
million Monte Carlo simulations. And we'll do this for our two reports,
which I'll refer to as being ‘A’ and ‘B’, and they will take anywhere between 1
to 5 hours and 2 to 6 hours to complete. So let's define those values
for the arguments for these functions. And of course, the total time to complete
these two reports is simply their summation. So let's add them up and voila,
we now have 1 million random samples, which is great,
but we now need to be able to use them to properly figure out the chance we’ll
make it to the party on time. So to do this,
what I'll do now is bring in matplotlib, which I frequently rely
on to visualize data. And what we're going to do once
we've imported matplotlib is initialize
a plot, which will be a histogram, and you'll notice that I set the argument
density being equal to true so that we can plot everything
in the form of probability densities. And let's also layer on top of this plot
a vertical red line at the value 9 as the barbecue is in just 9 hours,
making it a bit easier to visualize the probability the total time to complete
both reports exceeds that threshold. And let's just do one more thing. After showing our plot, let's print the percentage of simulations
that exceeded a total of 9 hours. We can do this by simply
summing up the number of instances that meet a condition, in this case,
the time to complete exceeds 9 hours, and dividing it
by the total number of simulations. And how cool is that? Without doing any hand
calculations or derivations, you could pretty quickly estimate
that there is a reasonable chance that you won't make it to the barbeque
in 9 hours, around 12 to 13%. So, based on this probability,
should you tell Todd to get lost? Should you take on these two reports, risking the chance
that you'll miss out on the party? I'll leave that up to you. But what I can tell you is that Monte
Carlo simulation is straightforward and intuitive and can be used to answer
probability questions pretty simply with little required
background knowledge of probability. And you can use this information to
ultimately make the best choice for you. Now, if you've made it
to the end of this video, definitely don't hesitate
to subscribe to the channel if you're interested in learning more
about this type of content. It definitely helps the channel reach
and support a much broader community. And for those interested,
I've included the link in the description to the code used in this video. Thank you
as always for your interest and support. I look forward to seeing you soon.