Due date: Thursday, October 21st at midnight (Thursday night).
0) Be sure to use version control ("git"), as you develop your code. We suggest you create a new directory to hold this assignment, "assignment5" for example, and initialize a new git repository within it. Do "
git add ...., git commit"
repeatedly as you add to your scripts. You will hand in the output of "
git log" for your assignment repository as part of the assignment. You must have a significant number of commits representing the modifications, alterations
and changes in your scripts. If your log does not show a significant number of meaningful commits you will loose marks.
This assignment will explore the Monty Hall Problem, an interesting application of statistics to a game show. The problem is based on one of the games played on Let's Make a Deal, a game show where the host (originally Monty Hall), would make deals with the game-show contestants.
The game in question proceeds as follows. There are 3 doors. One door has a car behind it, the other two doors each have a goat. The contestant is asked to pick one of the doors. Monty Hall then reveals what is behind one of the doors the contestant did not pick: it is a goat. The contestant is then offered a choice: switch her choice of door to the one which is still closed which she did not originally pick, or stay with the door she originally picked. The contestant then wins whatever is behind the door that she ends up picking.
The interesting part of this problem lies in the fact that the probability of guessing the correct door (the door with the car) changes once Monty Hall reveals a door that contains a goat. We will explore this problem in this assignment by repeatedly simulating the game and seeing which option is more successful, or if the choice to switch doors matters at all.
1) Create a file named
Monty.Hall.Utilities.R. It will contain the functions described in part 1 of the assignment.
1a) Create a function called
pick.door. This function will randomly pick a door from 3 possible doors. It will return the number of the chosen door. The "sample" function might be useful here.
1b) Create a function called
opened.door. This function takes 2 arguments, the number of the door with the car, and the number of the door which the contestant has chosen. The function will randomly select and return the
number of the door which Monty Hall will open before the contestant. The number of the door cannot be the door which the contestant has already chosen, nor the door behind which is the car.
Please note an unexpected feature of the sample function:
> > sample(c(2),1)  2 > sample(c(2),1)  1 > sample(c(3),1)  2 >
1c) Create a function called
play.monty.hall. This function will take a single argument, a boolean, which indicates whether or not the contestant chooses to switch doors. The function will perform the following steps:
- randomly pick the door behind which is the car,
- randomly pick the door the contestant selects,
- randomly pick the door which Monty Hall opens (subject to the conditions described above),
- if the contestant chooses to change doors, as indicated by the function's boolean argument, then the door the contestant selects is changed,
- returns a boolean value, indicating whether or not the contestant won the car.
1d) Create a function called
play.many.monty.halls. This function takes two arguments, n, an integer, and a boolean. This function will call the
play.monty.hall function n times, using the value of the boolean
given as an argument. The function will return the fraction of times the contestant chooses the winning door.
Your functions should behave similar to this:
> > source("Monty.Hall.Utilities.R") > > pick.door()  3 > pick.door()  1 > > opened.door(2,2)  3 > opened.door(2,2)  1 > opened.door(1,2)  3 > > play.many.monty.halls(100, TRUE)  0.65 > play.many.monty.halls(100, FALSE)  0.27 >
2) Create an R script, named
- sources the file "Monty.Hall.Utilities.R"
- reads an argument from the command line.
- If the argument is "Keep" the script calls play.many.monty.halls 100 times without switching the contestant's choice of door. It will then print out a sentence with the result: "When the door is not switched, the fraction of wins is ...".
- If the command-line argument is "Switch", the script calls play.many.monty.halls 100 times with switching the contestant's choice of door. It will then print out a similar appropriate sentence.
- If the command-line argument is neither of the above the script should exit with an appropriate error message.
- If the number of command-line arguments is not 1 the script should exit with an error message.
Note that, starting with this assignment and for the rest of the semester, you will be expected to use coding best practices in all of the work that you submit. This includes, but is not limited to:
- Plenty of comments in the code, describing what you have done.
- Sensible variable names.
- Explicitly returning values, if the function in question is returning a value.
- Not using the print() function to return values.
- Proper indentation of code blocks.
- No use of global variables.
- Using existing R functionality, when possible.
- Creating modular code. Using functions.
- Never copy-and-pasting code!
Run.Monty.Hall.R files and the output of "git log" from your assignment repository.
Both R scripts must be added and committed frequently to the repository. To capture the output of 'git log' use redirection ( git log > git.log, and hand in the "git.log" file).Assignments will be graded on a 10 point basis. Due date is October 21st 2021 (midnight), with 0.5 point penalty per day for late submission until the cut-off date of October 28th 2021, at 12:00pm.