Week 5 Discussion

Chapter 1.1 Discrete Probability Distributions

  1. Modify the program CoinTosses to toss a coin n times and print out after every 100 tosses the proportion of heads minus 1/2. Do these numbers appear to approach 0 as n increases? Modify the program again to print out, every 100 times, both of the following quantities: the proportion of heads minus 1/2, and the number of heads minus half the number of tosses. Do these numbers appear to approach 0 as n increases?
CoinTosses <- function(num_of_rolls){
  counter <- 1
  heads <- 0
  tails <- 0
  
  flip_list <- sample(0:1, num_of_rolls, replace = TRUE)
  
  for (i in flip_list){
    if(i == 1){
      #print("Heads")
      heads <- heads + 1
    } else{
      #print("Tails")
      tails <- tails + 1
    }
  
    if (counter %% 100 == 0){
      print(paste0("Counter: ", counter))
      print(paste0("Proportion of heads minus 1/2: ", heads/(heads+tails)-0.5))
    }
    counter <- counter + 1
  }
  
  print(paste0("Total Heads: ", heads))
  print(paste0("Total tails: ", tails))
}

CoinTosses(1000)
## [1] "Counter: 100"
## [1] "Proportion of heads minus 1/2: 0.07"
## [1] "Counter: 200"
## [1] "Proportion of heads minus 1/2: 0.035"
## [1] "Counter: 300"
## [1] "Proportion of heads minus 1/2: 0.03"
## [1] "Counter: 400"
## [1] "Proportion of heads minus 1/2: 0.0275"
## [1] "Counter: 500"
## [1] "Proportion of heads minus 1/2: 0.018"
## [1] "Counter: 600"
## [1] "Proportion of heads minus 1/2: 0.005"
## [1] "Counter: 700"
## [1] "Proportion of heads minus 1/2: 0.0128571428571429"
## [1] "Counter: 800"
## [1] "Proportion of heads minus 1/2: 0.015"
## [1] "Counter: 900"
## [1] "Proportion of heads minus 1/2: 0.0144444444444445"
## [1] "Counter: 1000"
## [1] "Proportion of heads minus 1/2: 0.013"
## [1] "Total Heads: 513"
## [1] "Total tails: 487"

The numbers do appear to be getting closer to 0 as the n increases.

  1. In Las Vegas, a roulette wheel has 38 slots numbered 0, 00, 1, 2, . . . , 36. The 0 and 00 slots are green and half of the remaining 36 slots are red and half are black. A croupier spins the wheel and throws in an ivory ball. If you bet 1 dollar on red, you win 1 dollar if the ball stops in a red slot and otherwise you lose 1 dollar. Write a program to find the total winnings for a player who makes 1000 bets on red.
roulette <- function(num_of_spins){
  dollars <- 0       # Assume that zero is the baseline
  
  spins <- sample(1:38, num_of_spins, replace = TRUE)
  
  for (count in spins){
    if(count <= 18){
      # There are 18 red slots out of a total of 38 possibilities
      dollars <- dollars + 1
    } else {
      dollars <- dollars - 1
    }
  }
  print(paste0("Total winnings: $", dollars))
}

# Let's run the program 10 times
for (i in 1:10){
  roulette(1000)
}
## [1] "Total winnings: $-122"
## [1] "Total winnings: $-54"
## [1] "Total winnings: $-66"
## [1] "Total winnings: $-48"
## [1] "Total winnings: $-26"
## [1] "Total winnings: $-14"
## [1] "Total winnings: $-20"
## [1] "Total winnings: $-42"
## [1] "Total winnings: $-6"
## [1] "Total winnings: $-126"

It looks like Roulette may not be the best game to play after all.