## Exercises

Consider the following two games.

- In Game A you flip a fair coin. If the coin comes up Heads you get two dollars, whereas if it comes up Tails you get one dollar.
- In Game B you roll a fair die. If the six-spot comes up, you win twenty-five dollars. If you get 2, 3, 4, or 5, nothing happens. If the one-spot comes up, you lose fifteen dollars.

In this exercise you need to answer two questions:

- If you could choose to play either Game A or Game B
*just once*, which game would you prefer to play, and why? - Suppose that you can choose either to play Game A ten thousand times, or Game B ten thousand times. Which choice would you prefer, and why?

Answers to the first question will vary from person to person, depending on circumstances and personal taste. On the other hand, astute consideration of expected values leads most people to answer the second question in the same way. Therefore, before you answer the question please write two simulation functions: one called

`GameA`

that provides an estimate of the expected value of the winnings from a play of Game A, and another called`GameB`

that estimates the expected value of the winnings from a play of Game B. The functions should have two parameters:- a
`reps`

parameter to permit the user to choose the number of repetitions of the game; - a
`seed`

parameter to permit the user to choose a starting seed.

Use both functions with fifty thousand reps, in order to obtain estimates for the expected amount of winnings in a single play of each game. Use a simple starting seed for each function. Report the seeds you used, along with the estimated expected values for each game. Use your results to decide which game is the better choice to play ten thousand times.

**Note**: Please keep in mind that the fifty-thousand figure is just the number of times you repeat your simulations in order to estimate the expected value of a*single play*of one of the games. It’s**not**how many times you actually get to play either game—that’s ten thousand.Reconsider the meeting of Anna and Raj. Suppose that they still plan to meet during the same one-hour period, but that Anna is willing to wait \(a\) minutes and Raj is willing to wait \(r\) minutes, where \(a\) and \(r\) could differ. Modify the

`meetupSim()`

function so that it accepts two additional parameters:`a`

: the time Anna is willing to wait;`r`

: the time Raj is willing to wait;

Apply the function with ten thousand repetitions (and a simple seed that you report) in order to estimate the probability of a meeting, assuming that Anna is willing to wait 13 minutes and Raj is willing to wait 7 minutes.

**Hint**: Suppose that Anna arrvies at time \(Anna\) and Raj arrives at times \(raj\). We want a simple mathematical expression that gives the conditions for Anna and Raj to connect. In the example in the text, this was:\[\vert\ anna - raj \ \vert < 10\]

because both Anna and Raj were willing to wait the same amount of time: ten minutes. Now we have to think a bit more carefully when Anna waits \(a\) minutes and Raj waits \(r\) minutes, where \(a\) and \(r\) may differ. You might start by observing that we must have

\[anna - r \le raj,\]

for if not then Raj would arrive and leave before Anna gets there. Also, we would have to have

\[raj \le anna + a,\]

for if not then Anna would arrive and leave before Raj gets there. But if we have

*both*of those conditions:\[anna - r \le raj \text{ and } raj \le anna + a\]

then they must connect, since neither person will arrive too much before the other.

Review the function

`dual()`

in the Practice Exercises of this Chapter (see Section 6.3.4). Modify the function as follows:- Add a parameter
`abe`

that gives the probability for Abe to hit when he shoots, and a parameter`bo`

that gives the chance for Bo to hit when he shoots. the probabilities should be expressed as numbers between 0 and 1. (Thus, a 30% chance of hitting is 0.30.) - Make the function estimate the expected value of the number of shots taken in a dual, rather than the chance for Abe to win.

When you have your function working, use it to estimate the expected number of shots when:

- Abe’s chance of hitting is 50 percent and Bo’s chance if 50%;
- Abe’s chance of hitting is 20% and Bo’s chance of hitting is 25%.

In each simulation, use one hundred thousand repetitions and a seed that you choose.

- Add a parameter
A candy-maker makes a particular type of candy that always has 20 candies in each bag. When you buy a bag of this candy, you take a few candies from it every day until they are all gone. The amount you take each day is a random whole number from 1 to 10. A possible scenario is:

- Day 1: take 4 candies. (There are 16 left.)
- Day 2: take 8 candies. (There are 8 left.)
- Day 3: take 3 candies. (There are 5 left.)
- Day 4: take 6 candies. (There were only 5 left, so you take all the remaining candies.)

In the scenario above, it took you four days to finish the bag.

Write a function called

`daysNeededSim()`

that will estimate the expected number of days required to empty a bag. It should take two parameters:`reps`

: the number of times to simulate the process of emptying one bag. The default value should be 10,000.`seed`

: a random seed that the user can set. The default value should be`NULL`

.

Apply the function with 50,000 repetitions and a seed that you report, in order to estimate the expected number of days required to empty the bag.

**Hint:**This scenario is quite similar to the`numberNeededSim()`

problem from this Chapter. Begin by reviewing it.(*) Refer back to the

`numberNeededSim()`

function. Write a program that computes the estimated expected number needed, for the following targets:`<- seq(0.05, 0.95, by = 0.05) targets`

Each estimate should be based on ten thousand repetitions. Start with a simple seed that you report. Report the estimated expected values for all of the required targets. Compare these estimates with

`exp(targets)`

(the number \(e\) raised to the power of each of the targets.) Formulate a conjecture about the value of the expected number of numbers needed, when the target is a real number between 0 and 1.**Hint**: Recall that`numberNeededSim()`

function returns its estimate of the expected number needed. Hence you could use it inside a`for`

-loop that iterates through the elements of the`targets`

vector above, something like this:`<- seq(0.05, 0.95, by = 0.05) targets <- length(targets) n <- numeric(n) results set.seed(3030) # or some other simple seed that you like for ( i in 1:n ) { # use numberNeededSim() with: # * 10000 reps # * seed left at the default NULL (you provided one already) # * target set to targets[i] # * report left at FALSE (no need to have R talk to you at each step) # make sure to store the estimate in results[i] }# after the loop, you can compare results with exp(targets)`

(*) A pipe-smoker has two boxes of matches in his pocket. Both boxes contain 40 matches. Every time he lights his pipe, the smoker reaches into his pocket and randomly picks one of the boxes, pulling a match from the box. Eventually one of the boxes runs out. What is the expected value of the number of matches remaining in the other box at that time? Write a simulation function to estimate the answer. Your function should have at least a

`reps`

and a`seed`

parameter. Apply the function with ten thousand repetitions—and with a simple seed that you report—in order to estimate the expected value.(*) A gambler starts with 10 dollars, and repeatedly plays a game. If she wins the game she gets one dollar and if she loses the game she loses one dollar. Her chance of winning each game is a fixed number that is less than 0.50. Write a function to simulate the gambler repeatedly playing the game until her money runs out. The function should keep track of how much money she has left after each play, so that it can produce a line graph (similar to the one in the section on the Drunken Turtle) of the money left after each play. Write the function so that it has

- a
`start`

parameter for the initial amount of money the gambler has; - a
`seed`

parameter`; - a
`p`

parameter for the chance of winning, so that the user can enter any chance less than or equal to 0.5. (The function should stop the user if the user enters more than 0.5.)

Apply the function with a simple seed that you report, a starting amount of ten dollars, and a chance of winning equal to 0.45.

- a
(*) An absent-minded professor walks to and from campus every day. Prior to every commute—either from home to office or the reverse—she checks the weather, taking an umbrella with her if it is raining. The problem is that she

*never*takes an umbrella when the weather is dry, so even though she owns quite a few umbrellas the time comes eventually when it is raining but all of her umbrellas are at her destination, so that she will have to put up with a wet commute.Suppose that there are \(x\) umbrellas at her house and \(y\) umbrellas at her office, and that she begins at home. Suppose further that the chance of rain on any commute is \(p\), a number between 0 and 1.

Write a simulation function to estimate the expected number of dry commutes that she will enjoy before her first wet commute. The function should have five parameters:

`x`

: the initial number of umbrellas at home (default 1);`y`

: the initial number of umbrellas at the office (default 1);`p`

: the probability of rain at any commute (default 0.5);`seed`

: an seed for the randomizers (default`NULL`

).`reps`

: the number of times to simulate commuting until wet.

Apply the function with

`x`

and`y`

set at three, for the values 0.1, 0.5 and 0.9 for`p`

. Use 10,000 repetitions for each simulation, with seeds that you report. Which value of`p`

results in the smallest expected number of dry commutes?