Probability Lab

Author

Mouyseam Tran

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.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)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata

Acess 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 means there is one hit followed by one miss. So, in a streak of length 1, there is one hit and one miss. For example, in the sequence “H M”, there is one hit followed by one miss.

A streak length of 0 means there are no hits before a miss. So, in a streak of length 0, there are no hits before the miss. For example, in the sequence “M”, there are no hits before the miss.

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: There are 76 observations of 1 variable. His typical streak length is 4. His longest strike lenght is 40.

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.

# Set a seed for reproducibility
set.seed(35797)

# Define outcomes
coin_outcomes <- c("heads", "tails")

# Simulate flipping an unfair coin 100 times
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))

# Count the number of heads and tails
table(sim_unfair_coin)
sim_unfair_coin
heads tails 
   26    74 

Answer: The flips came up heads are 26. The flips came up tails are 74.

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.

# Set a seed for reproducibility
set.seed(35797)

# Define outcomes
shot_outcomes <- c("H", "M")

# Simulate shooting with a 45% shooting percentage
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))

Answer: The prob specifies the probability weights for each outcome, where 0.45 represents the probability of a hit (H) and 0.55 represents the probability of a miss (M), ensuring a 45% shooting percentage.

Exercise 5

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

# Define the calc_streak function
calc_streak <- function(outcomes) {
  lengths <- rle(outcomes)$lengths
  values <- rle(outcomes)$values
  streak_df <- data.frame(length = lengths, outcome = values)
  return(streak_df)
}

# Calculate streak lengths of sim_basket
sim_streak <- calc_streak(sim_basket)

# View the resulting dataframe
head(sim_streak)
  length outcome
1      1       H
2      1       M
3      1       H
4      1       M
5      2       H
6      1       M

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.

# Load necessary library
library(ggplot2)

# Calculate summary statistics
mean_streak_length <- mean(sim_streak$length)
max_streak_length <- max(sim_streak$length)

# Histogram
ggplot(data = sim_streak, aes(x = length)) +
  geom_histogram(binwidth = 1, fill = "skyblue", color = "skyblue") +
  labs(title = "Distribution of Streak Lengths for Simulated Shooter",
       x = "Streak Length", y = "Frequency") +
  theme_minimal()

# Print summary statistics
cat("Typical Streak Length:", mean_streak_length, "\n")
Typical Streak Length: 2.145161 
cat("Longest Streak of Baskets:", max_streak_length, "\n")
Longest Streak of Baskets: 8 

Answer: The typical streak length is 2.145161. His longest streak of baskets is 8.

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 was to run the simulation of the independent shooter again, I wouldn’t expect the streak distribution to be exactly the same as before. This is because each simulation involves random chance, so the specific streak lengths might vary.

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: The Kobe Bryant’s streak distribution shows a different pattern compared to the simulated shooter, it suggest that his shooting patterns are not consistent with an independent shooter and could support the hot hand model(psychological Factor like confidence).