Question: Q4. Back to roulette. Here you will write two functions that simulate various bets in roulette. Review the slides from class to see the possible
Q4. Back to roulette.
Here you will write two functions that simulate various bets in roulette. Review the slides from class to see the possible bets and their pay offs.
This should give you practice in the following aspects of function writing:
- Identify the parameters to a function and write a signature for the function
- Use if/else statements to control the flow of code evaluation
- Compare the efficiency of vectorized computations to for loops
Additionally, you will compare various betting strategies.
In class (see the slides), we looked at a function to simulate betting red in roulette. We reproduce this function here for your convenience.
betRed = function(numBets, betAmt = 1) { winLoss = sample(rep(c(-1, 1), c(20, 18)), size = numBets, replace = TRUE) netGain = sum(winLoss * betAmt) return(netGain) }
We compare the net gain of two types of betting strategies: 100 bets of $1 on red and 1 bet of $100 on red. To do this we repeated the betting process 10,000 times to see the kinds of net gains we might get with each strategy.
gain100B.1D = replicate(10000, betRed(numBets = 100, betAmt = 1)) gain1B.100D = replicate(10000, betRed(numBets = 1, betAmt = 100))
- We examined the distribution of the 10,000 outcomes from these betting strategies with a plot. Do you remember the density plot produced in class to compare the strategies, i.e., the distribution of wins for each strategy? Write a function that takes as arguments the two list of gains (like gain100B.1D and gain1B.100D above) that will produce a plot overlaying the two distributions. Make sure the plot looks nice!
You should be able to use the above plotting function to compare the net gains from the 2 strategies by running it as follows:
plotGains(gain1B.100D, gain100B.1D)
Can anyone help me with this question? Thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
