Outcomes

Background

In this assignment we’ll practice writing random variables using the game of Roulette. We’ll use them in the next assignment to analyze and understand the game.

A European roulette wheel has numbers 1-36 in red and black, and number 0 in green for a house win. The dealer spins the board, and the the ball lands randomly on one of these 37 numbers. Here’s a brief video introducing the rules of roulette.

Instructions

In the code that follows, think of x as a random variable uniformly distributed on the integers 0 through 36, representing the possible values for a game of roulette. Here’s one way to produce a vector containing a sample of size n from x.

n = 100
roulette_values = 0:36
x = sample(roulette_values, size = n, replace = TRUE)

Questions

(4 points) general code quality across all questions.

1 - outside bet

(3 points)

The elements of our sample space are the roulette numbers: {0, 1, …, 36}. In statistics, a random variable is a function that maps an element of the sample space to a real number. Let h(x) be a random variable representing the amount you win or lose for a bet on the high numbers defined as follows:

\[ h(x) = \begin{cases} -1 &\mbox{if } x \leq 18 \\ 1 & \mbox{if } 18 < x \end{cases} \]

Define a vectorized function high that accepts a sample vector x and implements h(x).

high = function(x)
{
  
  ifelse(x > 18, "high","low")

}

high(x)
##   [1] "low"  "high" "high" "high" "high" "low"  "low"  "low"  "low"  "low" 
##  [11] "high" "high" "high" "high" "low"  "low"  "low"  "low"  "low"  "low" 
##  [21] "low"  "low"  "low"  "high" "low"  "low"  "low"  "high" "high" "high"
##  [31] "low"  "low"  "low"  "low"  "low"  "high" "low"  "high" "low"  "low" 
##  [41] "high" "high" "high" "high" "low"  "low"  "low"  "high" "high" "low" 
##  [51] "high" "low"  "high" "low"  "low"  "high" "high" "low"  "low"  "low" 
##  [61] "low"  "high" "high" "low"  "high" "high" "low"  "high" "high" "high"
##  [71] "low"  "low"  "high" "high" "low"  "low"  "high" "low"  "low"  "high"
##  [81] "high" "low"  "low"  "low"  "high" "high" "high" "high" "low"  "low" 
##  [91] "low"  "high" "low"  "high" "high" "high" "low"  "low"  "low"  "low"

Hint: Use even(x) below as a template.

even = function(x)
{
    win = (x %% 2 == 0) & (x != 0)
    ifelse(win, 1, -1)
}

2

(2 points)

Define a vectorized function low that accepts a sample vector x and returns the amount you win or lose if you bet one unit money on the low numbers, from 1 to 18. Hint: you lose if X = 0.

low = function(x)
{
    win = (x <= 18) & (x =! 0)
    ifelse(win, 1, -1) * 2
}

low(x)
##   [1]  2 -2 -2 -2 -2  2  2  2  2  2 -2 -2 -2 -2  2  2  2  2  2  2  2  2  2 -2  2
##  [26]  2  2 -2 -2 -2  2  2  2  2  2 -2  2 -2  2  2 -2 -2 -2 -2  2  2  2 -2 -2  2
##  [51] -2  2 -2  2  2 -2 -2  2  2  2  2 -2 -2  2 -2 -2  2 -2 -2 -2  2  2 -2 -2  2
##  [76]  2 -2  2  2 -2 -2  2  2  2 -2 -2 -2 -2  2  2  2 -2  2 -2 -2 -2  2  2  2  2

3 - straight up

(3 points)

Define a vectorized function straightup that accepts x, a sample vector, and d, a digit between 0 and 36, and returns the amount you win or lose in roulette if you bet one unit money on the single value d. Hint: The payout for a straight up bet is 35 to 1.

d = sample(roulette_values, 1, replace = TRUE)

straightup = function(x)
{
    win = (x == d) 
    ifelse(win, 1, -1) * 36
}

straightup(x)
##   [1] -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36
##  [19] -36 -36 -36 -36 -36 -36 -36 -36 -36 -36  36 -36 -36 -36 -36 -36 -36 -36
##  [37] -36 -36 -36 -36 -36 -36  36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36
##  [55] -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36
##  [73] -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36 -36  36 -36 -36 -36 -36 -36
##  [91] -36 -36 -36 -36 -36 -36 -36 -36 -36 -36

4 - something different

(5 points)

Pick another possible roulette bet that does not have a 1 to 1 payout, and implement an appropriately named vectorized function that accepts x, and returns the amount you win or lose in roulette if the ball lands in x. Write it in mathematical notation below as f(x).

\[ f(x) = \begin{cases} 1 &\mbox{if } x \leq 12 , x \neq 0 \\ -1 & \mbox{if } 13 < x, x = 0 \end{cases} \] This is the first 12 (or 1-12)

twelve1 = function(x)
{
    win = (x <= 12) & (x =! 0)
    ifelse(win, 1, -1) * 3
}

twelve1(x)
##   [1]  3 -3 -3 -3 -3  3 -3  3 -3 -3 -3 -3 -3 -3 -3  3  3 -3  3  3  3  3  3 -3  3
##  [26]  3  3 -3 -3 -3 -3  3  3  3  3 -3 -3 -3  3 -3 -3 -3 -3 -3  3 -3  3 -3 -3 -3
##  [51] -3  3 -3  3  3 -3 -3  3 -3  3  3 -3 -3 -3 -3 -3  3 -3 -3 -3 -3  3 -3 -3  3
##  [76] -3 -3  3 -3 -3 -3  3  3  3 -3 -3 -3 -3 -3  3  3 -3  3 -3 -3 -3  3  3  3 -3

5

(3 points)

Calculate the expected value of your winnings after a single play for each of the 4 betting strategies described above. It should be negative, because the house always wins.

# 1 
high2 = function(x)
{
  win = (x > 18)
  ifelse(win, 1, -1)
}

sum(high2(x), 1/2)
## [1] -11.5
# 2
sum(low(x), 1/2)
## [1] 24.5
# 3
sum(straightup(x), 1/36)
## [1] -3383.972
# 4
sum(twelve1(x), 1/3)
## [1] -65.66667