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)
{
ifelse(x > 18, "high","low")
}
high(x)
## [1] "low" "high" "high" "high" "low" "low" "low" "high" "high" "low"
## [11] "low" "high" "high" "high" "low" "low" "high" "low" "low" "low"
## [21] "high" "low" "low" "high" "low" "low" "low" "low" "high" "high"
## [31] "low" "high" "low" "low" "low" "high" "high" "high" "high" "high"
## [41] "high" "high" "high" "low" "low" "low" "high" "low" "high" "high"
## [51] "high" "low" "low" "low" "high" "low" "low" "high" "high" "high"
## [61] "high" "high" "high" "low" "low" "high" "high" "low" "high" "high"
## [71] "low" "high" "low" "high" "low" "high" "low" "high" "high" "high"
## [81] "low" "high" "low" "high" "low" "low" "high" "low" "high" "low"
## [91] "low" "low" "low" "high" "low" "low" "low" "low" "high" "high"
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.
y_2 = 0:1000
y = sample(y_2, n, replace = TRUE)
low = function(x, y)
{
win = (x <= 18) & (x =! 0)
ifelse(win, 1, -1) * 2 * y
}
low(x, y)
## [1] 1908 -1018 -324 -766 1922 210 712 -944 -734 1520 724 -1754
## [13] -780 -1266 1558 536 -1598 612 874 1556 -1474 764 164 -1930
## [25] 236 1660 1808 1046 -276 -356 1228 -1514 890 1790 1032 -1978
## [37] -1524 -1336 -1374 -1102 -1480 -236 -1208 1156 204 1560 -1458 1084
## [49] -1812 -750 -1390 684 238 1996 -1732 862 646 -1594 -734 -446
## [61] -794 -482 -184 686 864 -986 -860 460 -898 -532 1404 -356
## [73] 756 -1820 620 -1030 1746 -792 -1494 -842 494 -1810 1328 -1174
## [85] 1492 1772 -1952 324 -338 522 296 1574 1006 -1066 1890 1686
## [97] 572 56 -48 -14
In this case, y is the placed bet. I maxed it out 1000 for this case to keep at a scale.
(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.
Gao: I believe 0 is included. If not, then win = (x == d) & (x =! 0)
d = sample(roulette_values, 1, replace = TRUE)
straightup = function(x, y)
{
win = (x == d)
ifelse(win, 1, -1) * 36 * y
}
straightup(x, y)
## [1] -34344 -18324 -5832 -13788 -34596 -3780 -12816 -16992 -13212 -27360
## [11] -13032 -31572 -14040 -22788 -28044 -9648 -28764 -11016 -15732 -28008
## [21] -26532 -13752 -2952 -34740 -4248 -29880 -32544 -18828 -4968 -6408
## [31] -22104 -27252 -16020 -32220 -18576 -35604 -27432 -24048 -24732 -19836
## [41] -26640 -4248 -21744 -20808 -3672 -28080 -26244 -19512 -32616 -13500
## [51] -25020 -12312 -4284 -35928 -31176 -15516 -11628 28692 -13212 -8028
## [61] -14292 -8676 -3312 -12348 -15552 -17748 -15480 -8280 -16164 -9576
## [71] -25272 -6408 -13608 -32760 -11160 -18540 -31428 -14256 -26892 -15156
## [81] -8892 -32580 -23904 -21132 -26856 -31896 -35136 -5832 -6084 -9396
## [91] -5328 -28332 -18108 -19188 -34020 -30348 -10296 -1008 -864 -252
(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} \]
twelve1 = function(x, y)
{
win = (x <= 12) & (x =! 0)
ifelse(win, 1, -1) * 3 * y
}
twelve1(x, y)
## [1] 2862 -1527 -486 -1149 -2883 315 1068 -1416 -1101 -2280 -1086 -2631
## [13] -1170 -1899 2337 -804 -2397 918 -1311 -2334 -2211 -1146 246 -2895
## [25] 354 -2490 2712 1569 -414 -534 1842 -2271 -1335 2685 -1548 -2967
## [37] -2286 -2004 -2061 -1653 -2220 -354 -1812 -1734 306 -2340 -2187 1626
## [49] -2718 -1125 -2085 1026 357 -2994 -2598 1293 969 -2391 -1101 -669
## [61] -1191 -723 -276 1029 1296 -1479 -1290 -690 -1347 -798 -2106 -534
## [73] 1134 -2730 930 -1545 2619 -1188 -2241 -1263 741 -2715 1992 -1761
## [85] 2238 2658 -2928 486 -507 783 444 2361 -1509 -1599 -2835 -2529
## [97] 858 84 -72 -21
(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 the question didn't ask for a bet. So I included a new function that included the bet 'y'
high2 = function(x, y)
{
win = (x > 18)
ifelse(win, 1, -1) * 2 * y
}
sum(high2(x, y))
## [1] 1632
# 2
sum(low(x, y))
## [1] -1632
# 3
sum(straightup(x, y))
## [1] -1798200
# 4
sum(twelve1(x, y))
## [1] -70356