# clear the existing environment
rm(list = ls())
# load data (local, bare filename -- no URLs)
kobe_basket <- read.csv("kobe_basket.csv")
# streak helper defined in the lab walkthrough -- do not alter
calc_streak <- function(x) {
if (!is.atomic(x)) {
x <- x[, 1]
}
if (any(!x %in% c("H", "M"))) {
stop('Input should only contain hits ("H") and misses ("M")')
}
y <- rep(0, length(x))
y[x == "H"] <- 1
y <- c(0, y, 0)
wz <- which(y == 0)
streak <- diff(wz) - 1
return(data.frame(length = streak))
}shot_outcomes <- c("H", "M")
sim_fair <- sample(shot_outcomes, size = 133, replace = TRUE)
table(sim_fair)## sim_fair
## H M
## 70 63
Written answer:
sim_long <- sample(shot_outcomes, size = 5000, replace = TRUE,
prob = c(0.45, 0.55))
run_prop <- cumsum(sim_long == "H") / (1:5000)
plot(run_prop, type = "l", col = "#317EAC", lwd = 2, ylim = c(0, 1),
main = "Running proportion of hits, 45% shooter",
xlab = "Number of shots", ylab = "Proportion of hits so far")
abline(h = 0.45, col = "#D55E00", lwd = 2, lty = 2)Written answer:Over the first 100 shots the running proportion swings across the whole range from 0 to 0.5, and it is still visibly unsettled a few hundred shots in (0.42 after 50 shots, 0.478 after 500). By the end it has flattened onto the reference line, finishing at 0.4538. This is the law of large numbers:a probability is a long-run proportion, so the short run is highly variable while the long run is very predictable.
## [1] 0.4360902
Written answer:‘p_hit’=0.4360902, so Kobe made about 43.6% of his attempts in the 2009 Finals. Read as a long-run proportion, this is our estimate of the probability that any single attempt of his goes in: if he took a very large number of shots under these same conditions, we would expect close to 43.6% of them to be hits.
kobe_streak <- calc_streak(kobe_basket$shot)$length
barplot(table(kobe_streak), col = "#317EAC",
main = "Kobe Bryant's streak lengths, 2009 NBA Finals",
xlab = "Streak length", ylab = "Number of streaks")Written answer:The 133 shots break into 76 streaks: 39 of length 0, 24 of length 1, 6 of length 2, 6 of length 3, and 1 of length 4. The median streak is 0 and the longest is 4 The distribution is strongly right-skewed – most streaks are very short, and the frequencies fall away sharply as the length grows. A streak of length O means a miss with no made basket immediately before it; since a streak ends at the first miss, its length counts only the hits inside it.
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(p_hit, 1 - p_hit))
sim_streak <- calc_streak(sim_basket)$length
barplot(table(sim_streak), col = "#D55E00",
main = "Simulated independent shooter's streak lengths",
xlab = "Streak length", ylab = "Number of streaks")## [1] 0
## [1] 5
## [1] 0.6962025
Written answer:The simulated shooter made 55 of 133 attempts and produced 79 streaks: 42 of length 0, 26 of length 1, 6 of length 2, 4 of length 3, and 1 of length 5. Its median streak is 0 and its longest is 5
## kobe_streak
## 0 1 2 3 4
## 39 24 6 6 1
## sim_streak
## 0 1 2 3 5
## 42 26 6 4 1
Written answer:The two distributions are close. Both have a median streak of 0 and the same sharply right-skewed shape; Kobe’s mean streak is 0.763 against the simulation’s 0.696, and the simulated shooter’s longest run (5) is actually longer than Kobe’s (4). There is no sign here that Kobe’s runs outlast what independent shooting produces at the same shooting percentage, so this comparison gives no evidence for the hot hand in the 2009 Finals.
n <- nrow(kobe_basket)
kobe_prev <- kobe_basket$shot[1:(n - 1)]
kobe_next <- kobe_basket$shot[2:n]
kobe_tbl <- table(kobe_prev, kobe_next)
prop.table(kobe_tbl, 1)## kobe_next
## kobe_prev H M
## H 0.3684211 0.6315789
## M 0.4800000 0.5200000
## [1] 0.4318182
##
## H M
## H 0.3272727 0.6727273
## M 0.4805195 0.5194805
##
## H M
## H 0.3272727 0.6727273
## M 0.4805195 0.5194805
Written answer:For Kobe, \(P(|text{hit} \mid (text{previous hit}) = 0.3684\) against an overall hit proportion of \(0.4318\) over the same shots – a gap of \(-0.063\). The conditional probability is lower than the unconditional one, the opposite of what the hot hand predicts. But the simulated shooter, whose shots we know to be independent, shows \(0.3273\) against \(0.4167\), a gap of \(-0.089\) – larger than Kobe’s, and in the same direction, purely by chance. So Kobe’s gap is well inside what independence produces, and this lens agrees with Exercise 6: no evidence of a hot hand.
## [1] 78
## [1] 0.5641026
## [1] 0.4360902
Written answer:Both Curry and Kobe made 133 attempts to shoot. Kobe made 70 out of 133, whereas Curry made 78 out of 133. This means Curry had more accurate shots out of the two.
curry_streak <- calc_streak(curry_basket$shot)$length
barplot(table(curry_streak), col = "#317EAC",
main = "Stephan Curry's streak lengths, 2017 Western Conference Finals",
xlab = "Streak length", ylab = "Number of streaks")## curry_streak
## 0 1 2 3 4 5
## 14 8 6 5 1 1
## [1] 0
## [1] 5
Written answer:The 133 shots break into 78 streaks: 14 of length 0, 8 of length 1, 6 of
length 2, 5 of length 3, and 1 of length 4. The **median streak is 0** and the
**longest is 4** The distribution is strongly right-skewed -- most streaks are very
short, and the frequencies fall away sharply as the length grows.
A streak of length O
means a miss with no made basket immediately before it; since a streak ends at the
first miss, its length counts only the hits inside it.
#### 3: [10 pts]
``` r
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(p_hit, 1 - p_hit))
sim_streak <- calc_streak(sim_basket)$length
barplot(table(sim_streak), col = "#D55E00",
main = "Simulated independent shooter's streak lengths",
xlab = "Streak length", ylab = "Number of streaks")
## [1] 1
## [1] 6
Written answer:The simulated shooter made 40 of 133 attempts and produced 78
streaks: 40 of
length 0, 15 of length 1, 10 of length 2, 8 of length 3, and 1 of length 5.
Its
**median streak is 0** and its
**longest is 5**
#### 4: [10 pts]
``` r
n <- nrow(curry_basket)
curry_prev <- curry_basket$shot[1:(n - 1)]
curry_next <- curry_basket$shot[2:n]
curry_tbl <- table(curry_prev, curry_next)
prop.table(curry_tbl, 1)
## curry_next
## curry_prev H M
## H 0.5348837 0.4651163
## M 0.5882353 0.4117647
## [1] 0.5584416
##
## H M
## H 0.4615385 0.5384615
## M 0.5263158 0.4736842
## [1] 0.4935065
```
Written answer:The two distributions are close. Both have a median streak of 0 and the same sharply right-skewed shape; Curry’s mean streak is 0.558 against the simulation’s 0.428, and the simulated shooter’s longest run (5) is actually longer than Kobe’s (4). There is no sign here that Curry’s runs outlast what independent shooting produces at the same shooting percentage, so this comparison gives no evidence for the hot hand in the 2017 Finals.
Written answer: Since the distributions are very similar, I can conclude that there is no evidence for a “hot hand” and that Curry’s shooting patterns are consistent with random chance.
| Team member | Attendance | Author | Contribution % |
|---|---|---|---|
| Daisy Herrera | Yes | Yes | |
| Desirae Jaurequi | Yes | No | |
| Ish Dubey | Yes | No | |
| Name of member 4 | Yes / No | Yes / No | |
| Name of member 5 | Yes / No | Yes / No | |
| Total | 100% |