Go to content ALT+c

Quantitative Applications for Data Analysis  (Winter 2021) (Old site; new site is at https://scinet.courses)

Friday September 20, 2024 - 18:21

6.5 Assignment 5

Due date: Thursday, February 25th 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.

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:

  1. randomly pick the door behind which is the car,
  2. randomly pick the door the contestant selects,
  3. randomly pick the door which Monty Hall opens (subject to the conditions described above),
  4. if the contestant chooses to change doors, as indicated by the function's boolean argument, then the door the contestant selects is changed,
  5. 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()
[
13
pick.door()
[
11
>
opened.door(2,2)
[
13
opened.door(2,2)
[
11
opened.door(1,2)
[
13
>
play.many.monty.halls(100TRUE)
[
10.65
play.many.monty.halls(100FALSE)
[
20.27
>

2) Create an R script, named Run.Monty.Hall.R , which:

  • 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!

Submit your Monty.Hall.Utilities.R and Run.Monty.Hall.R files and the output of "git log" from your assignment repository to the Assignment Dropbox.

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 February 25th 2021 (midnight), with 0.5 point penalty per day for late submission until the cut-off date of March 4th 2021, at 11:00am.

Last Modified: Thursday Feb 25, 2021 - 12:25. Revision: 23. Release Date: Thursday Feb 11, 2021 - 10:00.


Content Navigation


Course Calendar


Forum Posts


Related



Questions? Contact Support.
Web site engine's code is copyright © ATutor®.
Modifications and code of added modules are copyright of SciNet.