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.
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)
(4 points) general code quality across all questions.
(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)
{
win = (x > 18)
ifelse(win,1,-1)
}
Hint: Use even(x)
below as a template.
even = function(x)
{
win = (x %% 2 == 0) & (x != 0)
ifelse(win, 1, -1)
}
(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 >= 1)
ifelse(win,1,-1)
}
(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.
straightup = function(x,d = 1)
{
win = (x == d)
ifelse(win,35,-1)
}
(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).
Let f(x) be a random variable representing the amount you win or lose for a bet on the first dozen numbers {1,..,12} defined as follows:
\[ f(x) = \begin{cases} -1 &\mbox{if } x > 12 \\ 2 & \mbox{if } 1 \leq x \leq 12 \end{cases} \]
firstdozen = function(x)
{
win = (x >= 1) & (x <= 12)
ifelse(win,2,-1)
}
(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.
single_play = data.frame(x)
bet = 50
digit = 2
# high bet
single_play$high = high(x)
high_win = nrow(single_play[single_play$high == 1,])
# low bet
single_play$low = low(x)
low_win = nrow(single_play[single_play$low == 1,])
# straight up bet
single_play$straightup = straightup(x,digit)
straightup_win = nrow(single_play[single_play$straightup == 35,])
# first dozen bet
single_play$firstdozen = firstdozen(x)
firstdozen_win = nrow(single_play[single_play$firstdozen == 2,])
single_play$Total_After_Each_Roll = rowSums(single_play[2:5])
total = data.frame(colSums(single_play,100))
# print summary
printSinglePlay = function(bet,payout,total,type,eachWin)
{
cat("Bet",bet,"dollar(s) on",type, "with payout",payout,fill=TRUE)
payoff = total[type,"colSums.single_play..100."]
cat("Ball hits on the number(s):",eachWin,"times.","\nThen you",ifelse(payoff >= 0,"win","lose"),abs(payoff*bet),"dollar(s).\n\n",fill=FALSE)
}
printSinglePlay(bet,"1:1",total,"high",high_win)
## Bet 50 dollar(s) on high with payout 1:1
## Ball hits on the number(s): 50 times.
## Then you win 0 dollar(s).
printSinglePlay(bet,"1:1",total,"low",low_win)
## Bet 50 dollar(s) on low with payout 1:1
## Ball hits on the number(s): 47 times.
## Then you lose 300 dollar(s).
printSinglePlay(bet,"35:1",total,"straightup",straightup_win)
## Bet 50 dollar(s) on straightup with payout 35:1
## Ball hits on the number(s): 3 times.
## Then you win 400 dollar(s).
printSinglePlay(bet,"2:1",total,"firstdozen",firstdozen_win)
## Bet 50 dollar(s) on firstdozen with payout 2:1
## Ball hits on the number(s): 35 times.
## Then you win 250 dollar(s).
payoff_total = total["Total_After_Each_Roll","colSums.single_play..100."]
cat("If you bet on all four,","you",ifelse(payoff_total >= 0,"win","lose"),abs(payoff_total*bet),"dollars.\n\n",fill=FALSE)
## If you bet on all four, you win 350 dollars.
# data frames
single_play
## x high low straightup firstdozen Total_After_Each_Roll
## 1 34 1 -1 -1 -1 -2
## 2 8 -1 1 -1 2 1
## 3 24 1 -1 -1 -1 -2
## 4 35 1 -1 -1 -1 -2
## 5 26 1 -1 -1 -1 -2
## 6 34 1 -1 -1 -1 -2
## 7 30 1 -1 -1 -1 -2
## 8 16 -1 1 -1 -1 -2
## 9 22 1 -1 -1 -1 -2
## 10 28 1 -1 -1 -1 -2
## 11 8 -1 1 -1 2 1
## 12 11 -1 1 -1 2 1
## 13 7 -1 1 -1 2 1
## 14 25 1 -1 -1 -1 -2
## 15 24 1 -1 -1 -1 -2
## 16 31 1 -1 -1 -1 -2
## 17 35 1 -1 -1 -1 -2
## 18 17 -1 1 -1 -1 -2
## 19 1 -1 1 -1 2 1
## 20 20 1 -1 -1 -1 -2
## 21 6 -1 1 -1 2 1
## 22 14 -1 1 -1 -1 -2
## 23 22 1 -1 -1 -1 -2
## 24 30 1 -1 -1 -1 -2
## 25 4 -1 1 -1 2 1
## 26 34 1 -1 -1 -1 -2
## 27 27 1 -1 -1 -1 -2
## 28 0 -1 -1 -1 -1 -4
## 29 3 -1 1 -1 2 1
## 30 2 -1 1 35 2 37
## 31 10 -1 1 -1 2 1
## 32 36 1 -1 -1 -1 -2
## 33 23 1 -1 -1 -1 -2
## 34 30 1 -1 -1 -1 -2
## 35 28 1 -1 -1 -1 -2
## 36 7 -1 1 -1 2 1
## 37 19 1 -1 -1 -1 -2
## 38 24 1 -1 -1 -1 -2
## 39 6 -1 1 -1 2 1
## 40 30 1 -1 -1 -1 -2
## 41 13 -1 1 -1 -1 -2
## 42 21 1 -1 -1 -1 -2
## 43 2 -1 1 35 2 37
## 44 22 1 -1 -1 -1 -2
## 45 19 1 -1 -1 -1 -2
## 46 2 -1 1 35 2 37
## 47 21 1 -1 -1 -1 -2
## 48 10 -1 1 -1 2 1
## 49 11 -1 1 -1 2 1
## 50 1 -1 1 -1 2 1
## 51 11 -1 1 -1 2 1
## 52 0 -1 -1 -1 -1 -4
## 53 8 -1 1 -1 2 1
## 54 15 -1 1 -1 -1 -2
## 55 18 -1 1 -1 -1 -2
## 56 9 -1 1 -1 2 1
## 57 30 1 -1 -1 -1 -2
## 58 15 -1 1 -1 -1 -2
## 59 8 -1 1 -1 2 1
## 60 1 -1 1 -1 2 1
## 61 31 1 -1 -1 -1 -2
## 62 5 -1 1 -1 2 1
## 63 31 1 -1 -1 -1 -2
## 64 14 -1 1 -1 -1 -2
## 65 33 1 -1 -1 -1 -2
## 66 27 1 -1 -1 -1 -2
## 67 16 -1 1 -1 -1 -2
## 68 0 -1 -1 -1 -1 -4
## 69 5 -1 1 -1 2 1
## 70 33 1 -1 -1 -1 -2
## 71 8 -1 1 -1 2 1
## 72 6 -1 1 -1 2 1
## 73 33 1 -1 -1 -1 -2
## 74 13 -1 1 -1 -1 -2
## 75 25 1 -1 -1 -1 -2
## 76 26 1 -1 -1 -1 -2
## 77 36 1 -1 -1 -1 -2
## 78 24 1 -1 -1 -1 -2
## 79 20 1 -1 -1 -1 -2
## 80 19 1 -1 -1 -1 -2
## 81 9 -1 1 -1 2 1
## 82 6 -1 1 -1 2 1
## 83 1 -1 1 -1 2 1
## 84 12 -1 1 -1 2 1
## 85 25 1 -1 -1 -1 -2
## 86 31 1 -1 -1 -1 -2
## 87 10 -1 1 -1 2 1
## 88 31 1 -1 -1 -1 -2
## 89 3 -1 1 -1 2 1
## 90 27 1 -1 -1 -1 -2
## 91 17 -1 1 -1 -1 -2
## 92 4 -1 1 -1 2 1
## 93 26 1 -1 -1 -1 -2
## 94 6 -1 1 -1 2 1
## 95 20 1 -1 -1 -1 -2
## 96 9 -1 1 -1 2 1
## 97 33 1 -1 -1 -1 -2
## 98 16 -1 1 -1 -1 -2
## 99 26 1 -1 -1 -1 -2
## 100 36 1 -1 -1 -1 -2
total
## colSums.single_play..100.
## x 1781
## high 0
## low -6
## straightup 8
## firstdozen 5
## Total_After_Each_Roll 7