Probability Lab

Author

Natalie Field

Probability Lab

Load libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata

Access the data

data("kobe_basket")
glimpse(kobe_basket)
Rows: 133
Columns: 6
$ vs          <fct> ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL…
$ game        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ quarter     <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3…
$ time        <fct> 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:35…
$ description <fct> Kobe Bryant makes 4-foot two point shot, Kobe Bryant misse…
$ shot        <chr> "H", "M", "M", "H", "H", "M", "M", "M", "M", "H", "H", "H"…

Exercise 1

What does a streak length of 1 mean, i.e. how many hits and misses are in a streak of 1? What about a streak length of 0?

# Answer: A streak length of 1 includes one hit and one miss. It means that one miss followed after one hit. A streak length of 0 includes zero hits and one miss. Each time a miss is made, a new streak is started.  

Count the streak lengths for all 133 shots:

kobe_streak <- calc_streak(kobe_basket$shot)

Display the distribution of these streak lengths:

ggplot(data = kobe_streak, aes(x = length)) +
  geom_bar()

Exercise 2

Describe the distribution of Kobe’s streak lengths from the 2009 NBA finals. What was his typical streak length? How long was his longest streak of baskets? Make sure to include the accompanying plot in your answer.

# Answer: Looking at the distribution plot above, we can see that 0 has the highest frequency. This means that Kobe typically had a streak length of 0. His longest streak of baskets was 4; this streak occurred one time.

Simulations in R

We can use R to simulate random processes, like the flipping of a fair coin. The following command does just that:

coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
[1] "tails"

If you wanted to simulate flipping a fair coin 100 times, you could either run the above command 100 times or, more simply, adjust the “size” argument to 100. The “size” argument determines how many samples to draw. Use the adjusted command to flip the coin 100 times:

set.seed(0986) 
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)

To view the results from the simulation above, run the following command:

sim_fair_coin
  [1] "tails" "heads" "heads" "heads" "heads" "heads" "tails" "tails" "heads"
 [10] "heads" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
 [19] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads" "heads"
 [28] "tails" "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
 [37] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads" "heads"
 [46] "heads" "tails" "heads" "tails" "heads" "tails" "tails" "heads" "tails"
 [55] "heads" "tails" "heads" "tails" "tails" "heads" "tails" "tails" "tails"
 [64] "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails" "heads"
 [73] "heads" "heads" "tails" "heads" "heads" "heads" "heads" "tails" "heads"
 [82] "heads" "heads" "heads" "heads" "heads" "tails" "tails" "tails" "tails"
 [91] "tails" "heads" "heads" "tails" "tails" "tails" "heads" "heads" "heads"
[100] "tails"
table(sim_fair_coin)
sim_fair_coin
heads tails 
   46    54 

Consider an unfair coin that only lands heads 20% of the time. We can simulate flipping such a coin by adding an argument called “prob,” which provides a vector of two probability weights. See this in the command below:

set.seed(6937)
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))

The last argument indicates that for the two elements in the “outcomes” vector, we want to select the first one, “heads,” with a probability of 0.2 and the second one, “tails,” with a probability of 0.8.

To view the results produced by the unfair coin, run the following command:

sim_unfair_coin
  [1] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
 [10] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads" "tails"
 [19] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads" "tails"
 [28] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
 [37] "tails" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails"
 [46] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
 [55] "tails" "heads" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
 [64] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
 [73] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
 [82] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
 [91] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "heads" "tails"
[100] "tails"
table(sim_unfair_coin)
sim_unfair_coin
heads tails 
   18    82 

Exercise 3

In your simulation of flipping the unfair coin 100 times, how many flips came up heads? Include the code for sampling the unfair coin in your response.

# Answer: After flipping the unfair coin 100 times, we landed heads 18 times and tails 82 times. This result makes sense given that the unfair coin lands heads only 20% of the time. (18/100 = 18%, which is close to 20%.) The code that was used to generate this simulation can be seen in the chunks above.

Simulating the Independent Shooter

To simulate a single shot from an independent shooter with a shooting percentage of 50%, run the following command:

shot_outcomes <- c("H", "M")
sample(shot_outcomes, size = 1, replace = TRUE)
[1] "H"

Exercise 4

What change needs to be made to the “sample” function so that it reflects a shooting percentage of 45%? Make this adjustment, then run a simulation to sample 133 shots. Assign the output of this simulation to a new object called “sim_basket.”

# Answer: We can adjust the "sample" function by adding a "prob" argument. Within the argument, we can assign a probability of 0.45 to "H" and a probability of 0.55 to "M." To sample 133 shots, set "size" equal to 133. I have made these adjustments in the code below. 

set.seed(8563)
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))

To view the 133 shots produced by the independent shooter, run the following command:

sim_basket
  [1] "H" "H" "M" "H" "M" "H" "M" "M" "M" "H" "H" "M" "H" "H" "H" "M" "M" "H"
 [19] "H" "M" "H" "M" "M" "H" "M" "M" "M" "M" "H" "H" "H" "H" "H" "M" "M" "M"
 [37] "M" "M" "H" "M" "M" "H" "H" "M" "M" "H" "M" "H" "H" "M" "M" "M" "M" "M"
 [55] "M" "M" "H" "H" "M" "M" "M" "H" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H"
 [73] "H" "H" "M" "M" "M" "H" "H" "M" "H" "M" "M" "M" "M" "H" "M" "M" "M" "M"
 [91] "M" "M" "M" "H" "H" "M" "M" "M" "M" "M" "H" "H" "H" "H" "H" "H" "M" "H"
[109] "M" "H" "M" "M" "M" "H" "M" "M" "M" "M" "H" "H" "M" "H" "M" "M" "M" "H"
[127] "M" "M" "M" "M" "M" "H" "H"
table(sim_basket)
sim_basket
 H  M 
54 79 

Exercise 5

Using “calc_streak,” compute the streak lengths of “sim_basket,” and save the results in a data frame called “sim_streak.”

sim_streak <- calc_streak(sim_basket) 

Exercise 6

Display the distribution of these simulated streak lengths:

ggplot(data = sim_streak, aes(x = length)) +
  geom_bar()

Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45% shooting percentage? How long is the player’s longest streak of baskets in 133 shots?

# Answer: Looking at the distribution plot above, we can see that 0 has the highest frequency (there are 50 instances of a 0 streak). This means that our simulated independent shooter typically had a streak length of 0. The simulated shooter's longest streak of baskets was 6; this streak occurred once.

Exercise 7

If you were to run the simulation of the independent shooter a second time, how would you expect its streak distribution to compare to the distribution from the question above? Exactly the same? Somewhat similar? Totally different? Explain your reasoning.

# Answer: If I were to run the simulation of the independent shooter a second time, I would expect its streak distribution to be somewhat similar to its first one. With a 45% shooting percentage, the shooter typically had a streak length of 0. I would expect to see this trend in the second run of the simulation, since over half (50/80, or 62.5%) of the streak lengths in the first run were 0's. However, I would expect the length of the longest streak of baskets to vary, since this outcome seems to rely more on random chance (after all, we are using a random number generator to get these values).

Exercise 8

How does Kobe Bryant’s distribution of streak lengths compare to the distribution of streak lengths for the simulated shooter? Using this comparison, do you have evidence that the hot hand model fits Kobe’s shooting patterns? Explain.

# Answer: Kobe Bryant's distribution of streak lengths is similar to that of the independent shooter as both distributions are right-skewed. In addition, both Kobe Bryant and the independent shooter typically had a streak length of 0. If Kobe were to have a hot hand, then the probability of making the next shot conditional on making the last would be greater. Considering this, we would expect Kobe to have longer streaks due to the increased likelihood of each consecutive basket. However, of the streaks that were made (1, 2, 3, and 4), 65% (24/37) were 1's. This means that when he did make a shot, he missed the next one 65% of the time. Since this is over 50%, it does not seem like he has a hot hand. And, just by looking at the skewness of the distribution, we can see that as we go from a streak of 1 to a streak of 2, the frequency of 2's decrease (which goes against the hot hand model). As we go from a streak of 3 to a streak of 4, the frequency of 4's decreases even more. And, to wrap it up, the distribution of the independent shooter follows this same pattern as well, which further suggests that Kobe does not have a hot hand.