Probability Lab_TLT

Author

TLT

Probability

Loading libraries

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ 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)
Warning: package 'openintro' was built under R version 4.3.2
Loading required package: airports
Warning: package 'airports' was built under R version 4.3.2
Loading required package: cherryblossom
Warning: package 'cherryblossom' was built under R version 4.3.2
Loading required package: usdata
Warning: package 'usdata' was built under R version 4.3.2

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 mean there is one hit followed by one miss. A streak length of 0 is no hit and one miss.

kobe_streak <- calc_streak(kobe_basket$shot)
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: The distribution is right skew. His typical streak length was 0. His longest streak of baskets was 4.

coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 100, replace = TRUE)
  [1] "heads" "tails" "heads" "heads" "tails" "heads" "heads" "tails" "tails"
 [10] "tails" "tails" "tails" "heads" "tails" "heads" "heads" "tails" "heads"
 [19] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "tails" "tails"
 [28] "tails" "tails" "tails" "heads" "tails" "heads" "tails" "tails" "heads"
 [37] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails"
 [46] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads" "tails"
 [55] "heads" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
 [64] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
 [73] "heads" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
 [82] "heads" "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
 [91] "tails" "heads" "tails" "heads" "heads" "tails" "tails" "heads" "heads"
[100] "heads"
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
sim_fair_coin
  [1] "heads" "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails"
 [10] "heads" "heads" "tails" "tails" "heads" "tails" "heads" "tails" "tails"
 [19] "heads" "heads" "heads" "tails" "heads" "tails" "heads" "heads" "tails"
 [28] "tails" "tails" "heads" "tails" "tails" "heads" "tails" "tails" "tails"
 [37] "tails" "tails" "heads" "heads" "heads" "heads" "heads" "tails" "tails"
 [46] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "heads" "tails"
 [55] "heads" "heads" "heads" "heads" "heads" "tails" "tails" "tails" "tails"
 [64] "heads" "heads" "tails" "heads" "heads" "heads" "heads" "tails" "heads"
 [73] "tails" "heads" "heads" "tails" "tails" "heads" "heads" "tails" "tails"
 [82] "tails" "tails" "tails" "tails" "tails" "tails" "heads" "heads" "tails"
 [91] "tails" "tails" "heads" "heads" "tails" "tails" "heads" "heads" "tails"
[100] "tails"
table(sim_fair_coin)
sim_fair_coin
heads tails 
   45    55 
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, 
                          prob = c(0.2, 0.8))

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. Since the markdown file will run the code, and generate a new sample each time you Knit it, you should also “set a seed” before you sample. Read more about setting a seed below.

Answer: Simulation of flipping the unfair coin 100 times: 20 flips came up heads.

set.seed(77777)
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 100, replace = TRUE)
  [1] "heads" "heads" "tails" "heads" "tails" "tails" "tails" "heads" "tails"
 [10] "tails" "heads" "heads" "heads" "heads" "heads" "heads" "heads" "tails"
 [19] "tails" "tails" "tails" "heads" "heads" "tails" "tails" "tails" "heads"
 [28] "tails" "tails" "heads" "tails" "heads" "tails" "heads" "heads" "heads"
 [37] "tails" "heads" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
 [46] "tails" "heads" "heads" "heads" "heads" "heads" "heads" "heads" "heads"
 [55] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads" "heads"
 [64] "heads" "heads" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
 [73] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads" "heads"
 [82] "tails" "tails" "heads" "heads" "heads" "tails" "tails" "heads" "tails"
 [91] "heads" "heads" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
[100] "heads"
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, 
                          prob = c(0.2, 0.8))
sim_unfair_coin
  [1] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "tails" "tails"
 [10] "tails" "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails"
 [19] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
 [28] "tails" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails"
 [37] "heads" "tails" "heads" "tails" "heads" "tails" "tails" "tails" "tails"
 [46] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
 [55] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
 [64] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
 [73] "tails" "heads" "heads" "tails" "heads" "heads" "heads" "tails" "tails"
 [82] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
 [91] "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
[100] "tails"
table(sim_unfair_coin)
sim_unfair_coin
heads tails 
   20    80 
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 100, replace = TRUE)
sim_basket
  [1] "H" "M" "M" "M" "H" "M" "M" "M" "M" "M" "H" "M" "M" "H" "H" "H" "M" "H"
 [19] "H" "M" "M" "H" "M" "M" "H" "H" "M" "H" "H" "M" "M" "H" "M" "H" "M" "H"
 [37] "M" "H" "H" "M" "M" "H" "H" "H" "M" "M" "H" "H" "H" "H" "H" "H" "M" "M"
 [55] "M" "H" "H" "M" "M" "M" "H" "H" "M" "H" "H" "H" "M" "M" "H" "H" "H" "M"
 [73] "M" "M" "M" "M" "M" "H" "H" "H" "M" "M" "M" "H" "H" "M" "H" "M" "H" "H"
 [91] "M" "M" "M" "H" "H" "H" "M" "M" "M" "H"
table(sim_basket)
sim_basket
 H  M 
49 51 

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: make argument prob = (c0.45, 0.55) and size = 133

set.seed(88888)
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, 
                          prob = c(0.45, 0.55))
sim_basket
  [1] "M" "H" "H" "H" "M" "H" "M" "M" "H" "M" "M" "M" "H" "H" "M" "H" "M" "H"
 [19] "M" "M" "M" "H" "H" "H" "H" "M" "H" "M" "M" "M" "H" "H" "M" "H" "H" "M"
 [37] "H" "H" "M" "M" "M" "M" "H" "M" "M" "M" "M" "H" "H" "M" "M" "M" "M" "M"
 [55] "M" "H" "M" "M" "M" "H" "H" "H" "H" "M" "H" "M" "H" "M" "H" "H" "M" "M"
 [73] "H" "H" "H" "H" "H" "M" "M" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M" "H"
 [91] "M" "H" "H" "M" "H" "H" "M" "M" "H" "M" "M" "H" "M" "M" "M" "M" "M" "H"
[109] "M" "M" "M" "H" "H" "M" "M" "M" "H" "H" "M" "M" "M" "H" "H" "M" "M" "M"
[127] "H" "H" "M" "M" "H" "H" "H"
table(sim_basket)
sim_basket
 H  M 
58 75 

Exercise 5

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

Answer:

sim_streak <- calc_streak(sim_basket)

Exercise 6

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? Make sure to include a plot in your answer.

Answer: This is the right skew distribution. The typical streak length is 0. The longest streak of basket is 5.

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

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 the simulation is run another time, the streak distribution would be somewhat similar to the distribution above because the sample size and the probability is set, but a new sample is generated each time. If the we set the seed then the same sample is selected by R each time the document is knitted and this make sure results wont change.

set.seed(99999)
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, 
                          prob = c(0.45, 0.55))
sim_basket
  [1] "M" "H" "M" "H" "M" "M" "H" "M" "H" "M" "H" "M" "M" "M" "M" "H" "M" "M"
 [19] "M" "H" "H" "M" "M" "H" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "H" "H"
 [37] "H" "M" "H" "H" "H" "M" "H" "H" "M" "M" "M" "M" "M" "H" "H" "M" "M" "H"
 [55] "H" "H" "H" "M" "H" "H" "H" "M" "M" "H" "H" "M" "M" "M" "M" "M" "M" "H"
 [73] "M" "M" "M" "H" "M" "M" "H" "H" "M" "H" "H" "H" "H" "M" "H" "M" "H" "M"
 [91] "M" "M" "M" "H" "M" "M" "H" "H" "H" "H" "M" "H" "M" "H" "H" "M" "H" "H"
[109] "M" "H" "H" "M" "H" "M" "M" "M" "M" "M" "H" "H" "M" "M" "H" "H" "H" "M"
[127] "H" "H" "M" "H" "H" "H" "H"
table(sim_basket)
sim_basket
 H  M 
66 67 
sim_streak <- calc_streak(sim_basket)
ggplot(data = sim_streak, aes(x = length)) +
  geom_bar()

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 of lengths is similar to that of the independent simulated shooter (right skew, typical length is 0 followed by 1. The longest streak of basket is 4.