Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

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 bets and their pay offs.This should give you practice in

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))

  1. 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)

  1. A Wallet

When we play roulette we start with a particular sum of money, e.g., we may have $100 in our wallet. Then what’s in our wallet impacts how much and how long we can bet. For example, with $100 in our wallet we cannot place a $200 bet. Also, depending on our winnings, we may be able to place 200 $1 bets or we may run out of money before then.

For your first function, augment the betRed function to accept an additional parameter called wallet that contains the starting amount of money. Call this new function betRed2. Give wallet a default value so that it runs like the original betRed function if the default value of wallet is used.

The return value from this function should be the net gain. As before, this is the winnings less the losses. However, if at some point in your betting, you no longer have enough funds to place a bet then you can’t continue your betting.

NOTE: DO NOT USE A FOR LOOP IN THIS FUNCTION. Consider instead the cumsum() function and logical expressions.

Here are some tests that you can run to see if your code is working correctly (note: there are errors right now because the code has not been written!)

betRed2(betAmt = 100, numBets = 1, wallet = 50)

## Error in eval(expr, envir, enclos): could not find function "betRed2"

# returns 0 betRed2(betAmt = 100, numBets = 1, wallet = 100)

## Error in eval(expr, envir, enclos): could not find function "betRed2"

# returns either -100 or 100 test1 = replicate(10000, betRed2(betAmt = 1, numBets = 100, wallet = Inf))

## Error in FUN(X[[i]], ...): could not find function "betRed2"

mean(test1)

## Error in mean(test1): object 'test1' not found

# should be near -5.26 test2 = replicate(10000, betRed2(betAmt = 100, numBets = 100, wallet = 500))

## Error in FUN(X[[i]], ...): could not find function "betRed2"

plotGains(test2)

## Error in eval(expr, envir, enclos): could not find function "plotGains"

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question