Understanding Monte Carlo Simulation Through Basketball | by Vinicius Monteiro | Towards Data Science

Monte Carlo Simulation is a type of simulation where the events are chosen to happen randomly. By iterating and trying out various outcomes many times, arbitrarily, it gives great confidence in the result. [1] The name comes from Monte Carlo, located in Monaco, known for its strong gambling activity.
Let’s use a simple example, a coin toss. Suppose we don’t know, and it’s not easy to calculate the percentage of landing heads or tails. If you toss the coin 10 times and 9 out of 10 it lands on heads, does it mean it’s 90% heads and 10% tails? Of course not. It was merely luck.
The way to increase the confidence on the correct percentage of each side is to throw a lot more times than 10, let’s say 500 times. The count of tails and heads should come out more evenly.
For a coin toss, definitely, there is no need to do such a simulation to know the result. However, often, there are complex use cases with many variables and paths, which is hard to calculate the probability. That’s where Monte Carlo Simulation can be a powerful tool. It can predict something with accuracy by letting the scenario, with all the conditions, evolve randomly. It’s used in Project Management, Artificial Intelligence and to help to make decisions in general.
To provide you with a better perspective on what I’ve explained and to have some fun, I’m going to demonstrate how it works through a specific scenario that can happen at the end of a game of basketball.
Basketball is a complex sport (isn’t it just about putting a ball in a basket?!). It has many constraints and conditions that are constantly being analysed to make the best decision. Players and coaches are always watching for the right position on the court, time on the clock, the number of fouls, timeouts left and more. There are a lot of variables that come into play, literally.
It’s often compared to Chess as some characteristics are similar. For example, each player/piece has its role and function. Many both games are about opening lines and creating space, misleading. ‘fakes’ are also key. Both games make extensive use of set plays that explore these aspects.
The most crucial part of the game, without a doubt, is the end when scores are closed, within two or three points difference, for example. A team can win or lose in a blink of an eye, forcing players and coaches to get the most creative.
A common situation is: A team is down by three points, with 30 seconds left on the clock — is it better to try to take the three (a longer, hence more difficult shot) to tie and go to overtime, OR to try a quick two (usually shorter, easier shot), try to intentionally foul a bad free throw shooter in next possession — hoping that he/she misses one-shot or both — and go for another quick easier two and possibly win the game, or go to overtime?
Salman Khan, from Khan Academy, addressed exactly this situation [2]. Lebron James asked a question, for which Salman Khan answered and demonstrated the best decision using Monte Carlo simulation.
In this post, I’ll do the same, but with one minor difference. I’m going to take into account that according to NBA rules, a player can’t simply intentionally foul ANY player from the other team with under two minutes left in the game. It has to be the one with the ball. Otherwise, there are consequences.
With under two minutes left in the game (and from 2016, at the end of any quarter), if a player fouls another player that is away from the ball, the team is awarded one free throw and retains possession of the ball, there is no opportunity for the rebound. It’s crucial to emphasize that the TEAM is awarded one free throw — this means that the team can select any player on the floor to attempt the shot (most likely a high percentage free throw shooter) [3].
In general, the algorithm mimics the real game. It randomly tests different combinations of plays inside a loop and counts how many times the team won by taking the three or the two-plus intentional foul. The bigger number of trials / the loop, the more reliable the results are. There is a threshold for which increasing the number of trials doesn’t make much difference; the program will take longer to finish without adding much value.
Overall the code is divided into five sections:
It starts by defining the average of various attributes, such as free throw percentage, rebound, two and three-shot percentage, etc.
Definition of the logic that takes the three and misses it or makes it, and if it makes, whether it wins in overtime or not.
The logic for taking the two-plus intentionally fouling. It tests if it makes or misses the two if the opponent makes one or two free throws, grabs the rebound, etc.
It counts how many times the team wins by taking the three and how many times by taking the two.
It prints the result.
Here you can see the result without considering the foul-away-from-the-ball rule. Taking the three is just about better.
Wins taking the three: 1314Wins taking the two: 1151
By tweaking it a little bit, the opponent’s free throw percentage, from 65% to 40%, for example, taking the two, becomes the best option. This is maybe even more realistic, assuming that the player fouled can be the one away from the ball. It’s not uncommon to have such a bad free throw shooter in a team.
Wins taking the three: 1331Wins taking the two: 1564
To consider the rule and penalty that awards one free throw shot and retaining the ball, I added a small change in sim_take_two. In the while loop, I added the logic to check if the bad free throw shooter has the ball or not. If he/she does it, the normal two free throws plus rebound flow is followed. Otherwise (if fouled away from the ball), a team player attempts one shot and retains the possession. See below the snippet and full code.
For this scenario, it’s assumed that the team has a 40% free throw shooter on the floor that might end up with the ball and a high percentage one that the team would select to shoot the free throw if the foul is away from the ball.
Another significant constraint is the percentage that the ball is inbounded to the bad free throw shooter. The losing team would have to work really hard to force this situation. I was generous and set it to a 50% chance to happen.
Wins taking the three: 1326Wins taking the two: 776
With this rule, it becomes almost illogical for the team to try to win by going for the quick two-plus intentional foul under two minutes. The fact that the current winning team gets to retain the ball is a great advantage. It’s an effective rule to stop ‘Hack-a-Shaq’. Named after one of the worst free-throw shooters: Shaquille O’neal. Also, because the team can select any player to attempt the shot, it is even harder. If that weren’t the case, it would be less impossible. But still, the bad free throw shooter would have to have as low as a 15% average to make sense — Below is the result following this logic.
Wins taking the three: 1311Wins taking the two: 1360
What’s the answer? Is it better to take the three or quick two-plus intentional foul? It is definitely better to take the three. Especially considering the away-from-the-ball rule. Without considering this rule, if the team has a terrible free-throw shooter, bad as 40%, for example. Then taking the two-plus intentional foul could be considered. It depends on other variables as well, such as rebound percentage. Lastly, considering how the NBA game has evolved to have more prolific three-point shooters, it’s natural to favour shooting the three.
[1] Sampling and Monte Carlo Simulation https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-2/lecture-14-sampling-and-monte-carlo-simulation/
[2] Monte Carlo Simulation to Answer LeBron’s Question. https://www.youtube.com/watch?v=-fCVxTTAtFQ
[3] 2019 / 2020 NBA Rulebook https://official.nba.com/rulebook/