# Monte Carlo simulation to answer LeBron'sÂ question

## Video transcript

LeBron: It's LeBron here. I got
a quick brain teaser for you. If I'm down 3 with 30 seconds left is it better to take the 3? Or is it easier to take
the 2 and attempt to fowl a bad free throw shooter and
get another possession? Here's my friend Sal with an answer. Sal: That's a very very
interesting question, LeBron. Calculating the probabilities
of if you were to take a 3 what's your probability of tying the game and then being able to win in overtime that can be done on paper but the scenario where you take
a 2 and you attempt to fowl the opposing team and
they might make none or 1 or two free-throws and then
you might get another posession and then you take another 2 That's more complicated. You
could attempt to do it on paper but instead, I've written a
little computer simulation here and this type of a simulation is called
a Monte-Carlo simulation and literally, it'll run the scenarios and
it will do it as many times as we put in this variable over
here. So we're gonna set it up so we get an accurate number.
We're gonna do it a THOUSAND times for each of those scenarios and
see what fraction in each scenario which fraction do we end up winning?
And that's a pretty good estimate of the probability of us
actually winning the game depending on which strategy we choose. The rest of these things, these
variables that we set right over here these are the parameters
that will essentially drive the outcomes. This three-point
percentage, this is your three-point percentage, I have
it low here because I'm assuming it's going to be a hard 3.
It's going to be lower than a typical 3 point percentage. But obviously if this
number, if you set it higher than the three will look
better. If you set it lower than the 2 is going to look better. This is your 2 point
percentage. I'm assuming here it's going to be a relatively
easy 2 because the other team isn't going to be defending
as heavily against a 2. So I put it a little higher but
you can play with the numbers to see what you get in
different scenarios. This is the probability of
the opponent taking a 2, this is the opponent's
free-throw percentage. Once again if this is very
high than the fowling is not going to look as good. If this is
low the fowling might make sense. And I put this a little bit
lower than the league average because I'm assuming
you'll attempt to fowl or you have a reasonable chance of fowling
one of the weaker free-throw shooters. This is how much time goes off
the clock when you take a 2 This is how long it takes you to
fowl. This is offensive rebound percentage. And then I have a slightly different thing that I separated out is the, the percent chance of
a team taking a free-throw. If they miss their second free-throw the
percent chance that they'll actually be able to rebound it. So that's
15% you can play with that And this is just a probability,or
your percent chance of winning in overtime. And
I just put that at 50/50 and the rest of this really
just uses those parameters to go through the simulations.
This is a little code right over here just to draw the
background or actually load the images This is the meat. These two functions. So take 3 is a scenario
where we go for the 3. So we try to make the 3,
if we make the 3 then we have essentially tied the game
and now we're going into overtime and if we win in overtime, then we win! If neither of those things are true, or if one of those things
end up not being true either we make the 3 and
we lose in overtime, or we don't make the 3, then
false, we didn't win. And this function is going to
be called as many times as that variable trials. So it could
be called a thousand times a million times, whatever we
set it to, to figure out what fraction of the scenarios do we win in? This does the same thing for the
taking the 2 and trying to fowl. It gets a little bit more
complicated but it's not too bad. And it's a very simple simulation, I
think a lot of people listening might be able to add some,
little bit more complexity to maybe make it a
little bit more accurate. But what it does, it keeps
of who has the posession, how many points you're
down,how much time is left, and the way I implimented it, it
really only works for specific scenario we talked about. You would
probably want to make it fancier if you had more time on
the clock or if you wanted to cover more scenarios of different point spreads. But this
is essentially saying, look, while we're gonna
keep running this scenario, while there's time left on the
clock, this clause is talking about what do we do if we have the possession? If we're down by more than 3,
then we're gonna want to take it as quickly as possible and
we just take the minimum time off necessary to shoot a 2.
Otherwise, we would acutally want to run down the
clock. So if we happened, for whatever reason while going
through this over and over again to be up, we would probably
want to run the clock down before we take a shot. Now
this right over here says, remember, this is a scenario
where we're taking a 2, so this says, okay, lets try
to go for the 2 if we make it, then we're down by 2 less. And then we,
the possession goes to the other team. If we do not make it, does
the opposing team rebound it? So this is essentially
saying, if the opposing team makes the rebound, so we
did not make the offensive rebound, then they now
have the possession. This clause over is, what about the
cases where we don't have the possession? This is essentially what we say
if we're down by a more than zero points than we're
going to try to fowl. We're going to take the
time off the clock to fowl. Then the opponent is going
to take their two free-throws right over here, and there's
a little bit of code to see if in the situation that they
missed the second free-throw, there's some probability that
the opponant might actually be able to get the offensive rebound, so
we actually put that in there as well. Then there's the scenarios where
maybe we just happen not to be down, in which case, the opponent
is likely to take a 2. At least we assume that.
This is a case where I think other people could modify
it more. The bottom line is if we're not down, we
definitely do not want to fowl. So we're not fowling in this scenario. Remember, we're in this 2 point scenario. This essentially says,
look, when the clock ends, when time goes to zero, if
we're down by more than zero we lost. If we're down by
less than zero, we won. Otherwise, it's tied
and we go into overtime. So this clause right over here
tests to see if we win in overtime. All of the other things
are really just to draw. This draws the bars, this draws
some of the numbers on top of it Then, this is we actually run the trial. So we call this as many
times as there are trials that trials variables, and
we count how many times do we win taking a 3? How many
times do we lose taking a 3? How many times do we win
taking a 2? How many times lose taking a 2? Then we just
display everything right over there. Now that we have a decent
understanding of the program itself, let's get a
little bit of a drumroll, and see whether it makes
sense to take the 3, given these assumptions.
Instead of having ten trials let's give it a big number.
Let's say a thousand trials, let me reset this this thing.
So restart it right over here. You see its getting better approximations. This is what fraction of
the trial so far resulted in a victory. So right now taking
a 2, looks like it's about 15-16%, looks like we got
a victory. Here its 12-13% As we got more and more trials
we're more likely to have a much much more accurate
sense of our probabilities. A more accurate approximation
of the probabilities. But let's see, it's getting close... And we get... 2. Well based on
this, it looks like there's... Based on the assumptions that
we made, and this is important, based on these assumptions, it
looks like we were better off by a little bit, slightly higher,
taking the easy 2 then trying to fowl. But if we change things, and
I'll make it lower trials so that we don't have to wait all
day, let's make it 300 trials, if you were to raise your
3 point percentage to say, let's say it's 35% and let's
say the 2 is much harder than we had before, let's
say its 40% and let's say your opponent is a really
good free-throw shooter so let's say it's 80%, I suspect that
you will have a different outcome. Let's restart it and see what happens. Now all of a sudden, it looks
like you're much better off given these assumptions. You're much
better off going for the 3 to tie. and go into overtime. So these
are really the assumptions that drive it for this simulation.