Simulations in Data Science
With Three Code Examples
In this document, we will look at three simulation examples.
- Statistical Theory 101
- Monty Hall Problem
- Airline Optimization
Introduction
While in high school and grad school I always struggled with courses related to:
- Proofs
- Statistical Theory
With practice, I eventually gained an understanding of the information in these courses. And now I am out in the real world — as a practicing Data Scientist—obviously these courses will have provided me with a wealth of knowledge that I need for my day-to-day work and life… I wouldn’t go that far!
To be honest, I did learn a lot in these courses. But really, I largely have a new way of answering a lot of the questions asked in these classes. In fact, I use the same method to answer almost every question these days! The solution is SIMULATION.
Let’s look at an example from an introductory statistical theory class.
Statistical Theory 101
Let’s say you are tasked with finding the probability of flipping a coin 50 times and getting exactly 20 tails?
Did you know that this is a Binomial distribution? If you did, chances are you quickly calculated that these odds are ~4.2%. I personally found the answer by using the online calculator at statisticshelper.com, which shows the probability mass function (all the below formulas/solutions are image captures from the above website):
Here is an image that shows what each of these letters represents from our problem setup.
What if the coin is rigged and it flips heads 80% of the time. Now, what is the chance of 20 tails? If you are comfortable with the above formula, you would have found the answer as 0.06%.
But what if you didn’t know this was the Binomial distribution? Or what if that formula makes no sense to you? Or what if the question was different?
What if the question was what are the chances the first tail would flip on the 5th flip and the first 4 were all heads? What about the chances the first tail was on the 100th flip and the first 99 were heads?
Well now, you need to know about the Geometric distribution. 🤦♂️
What about the chances that you achieve the 5th tail flip on the 100th flip? And the other 4 head flips can be scattered throughout the first 99 flips anywhere. What are the odds?
Well, now you need to know about the Negative Binomial distribution. ️️🤦♀️. This is getting out of control!
Simulation Example 1: Binomial Distribution
If you didn’t understand any of the solutions above, that’s ok. That’s the power of this post. You don’t need to!
Instead of knowing about all of these statistical distributions, you could also simulate the results. Let’s revisit the first question: say you are tasked with finding the probability of flipping a coin 50 times and getting exactly 20 tails?
We can simulate a single flip using the above. random.random() returns a value between 0 and 1 with equal probability. So coin_flip() simulates a fair coin flip by default, but allows for unfair coins by updating the default probability value as well.
Get Josh Bernhard’s stories in your inbox
Join Medium for free to get updates from this writer.
Now we could simulate 50 flips of this coin, and take a look at the results.
Your results could vary, as this is random, and we didn’t set the seed. You can see in this first set of 50 coin flips, we achieved 26 heads and 24 tails. The question is how often would we achieve exactly 20 tails. We could now simulate this process 10,000 times and look at how often 20 tails occur.
Running this code, I obtain the result of ~4.2%! 🎉 🎊
You can easily modify this by changing the probability to 0.2. My simulation result provided 0.0005, which isn’t exactly the same as the theoretical result. However, it is a very reasonable value to continue forward with in the real world.
Simulation Example 2: Monty Hall Problem
If you are unfamiliar with the three-door Monty Hall problem, this article by David Burton can help get you up to speed. You can also try out the problem yourself at the UCSD website here. Overall, there are three doors. One door has a car behind it, the other two are duds. You choose a door, then are shown one of the duds. At this point, you can update your choice to the other door or stay with the door you originally chose.
Changing doors after being shown one of the dud doors to improve the odds of choosing the car was never intuitive for me. However, we can simulate the problem in order to prove updating our choice is the correct decision.
The following code will iterate one game where we choose to update our original choice with a door change.
We could now run 10,000 iterations of this scenario and track how often we win the car. This could vary a bit, but my run of the simulation provided nearly the exact theoretical solution where 66% of the time we win with a door change, while only 33% of the time do we win by keeping the original door choice.
Simulation Example 3: Airline Problem
As a final simulation example, consider you work for an airline. Imagine we know that a plane seats 100 people and that each passenger that purchases a ticket has a 10% of not showing up. Additionally, the profit per ticket is $75 dollars. If you oversell the plane, you lose money for each passenger that needs to be transferred to a new flight should there be no seats available. The loss on these oversold seats is $25 dollars.
Imagine, you collect the profit whether or not the passenger shows.
If you sell 100 tickets, you make $7,500 dollars.
If you sell 101 tickets and everyone shows, you make
$75 x 100 -$25 x 1 = $7,475
However, if there is at least 1 no-show, you earn:
$75 x 101 = $7,575
What is the optimal number of seats that should be sold to maximize profit?
We can calculate the revenue using the functions below with a default of selling 100 tickets, which should always provide $7,500 revenue.
We then could simulate many runs of the calculated_revenue function, but with different numbers of tickets sold to understand the optimal number. Let’s say we run 10,000 iterations at each possible number of tickets sold. Then we look at the possible revenue at each number of tickets sold. The code below performs the simulations.
Then we could plot the possible revenue values.
If we want the max possible revenue, we might choose to increase risk all the way up to selling 130+ tickets. However, the possible downside drives the expected profit at this very high number of seats sold below what we would expect at a much lower number of seats. Depending on our risk tolerance, there is no correct answer. This plot provides the information needed to make an educated decision.
Using the mean (the suggested optimal in most traditional courses) provides the following plot and a solution of 115 seats sold.
Conclusion
This concludes our trip through simulated solutions. The next time you have a problem you aren’t sure of the correct answer remember you can likely simulate the possibilities! Then use the results to make the right decision!











