Tversky and his colleagues studied the records of 48 of the Philadelphia 76ers basketball games in the 1980–81 season to see if a player had times when he was hot and every shot went in, and other times when he was cold and barely able to hit the backboard. The players estimated that they were about 25 percent more likely to make a shot after a hit than after a miss. In fact, the opposite was true—the 76ers were 6 percent more likely to score after a miss than after a hit. Tversky reports that the number of hot and cold streaks was about what one would expect by purely random effects. Assuming that a player has a fifty-fifty chance of making a shot and makes 20 shots a game, estimate by simulation the proportion of the games in which the player will have a streak of 5 or more hits.
streaksize <- function(x){
# Get the run lengths and values
df.rle <- rle(x>0)
ngroups <- length(df.rle$lengths)
length <- df.rle$lengths
val <- df.rle$values
# calculate the sums
id <- rep(1:ngroups,length)
sums <- tapply(x,id,sum)
# find the largest runs for positive (val) and negative (!val)
rmax <- which(length==max(length[val]) & val)
return(max(length[rmax]))
}
shots <- function(){
# make 20 shots a game, using runif for random num generator
sample <- runif(20, min=0, max=1)
# assign 0 and 1 using .5 from random numbers between 0 and 1
sample[sample < .5] <- 0
sample[sample >= .5] <- 1
wins = streaksize(sample)
return(wins)
}
simulation <- function(n){
loop = 1
success = 0
while (loop < n){
sim <- shots()
loop = loop + 1
if (sim >= 5){
success <- success + 1
}
}
return(success)
}
n_100 <- simulation(100)
n_1000 <- simulation(1000)
n_10000 <- simulation(10000)
n_100000 <- simulation(100000)
n_1000000 <- simulation(1000000)
The value appears to be approximately .25
n_100/100
## [1] 0.23
n_1000/1000
## [1] 0.229
n_10000/10000
## [1] 0.2486
n_100000/100000
## [1] 0.25041
n_1000000/1000000
## [1] 0.249537