STAT 415 and STAT 515 Project 1 Fall 2019 Page 1
Instructions:
• Follow the instructions indicated in this project carefully.
• This project should be considered to be an exam. That means that you should do your own work.
• You should not speak about this exam to other students in the class.
• You may use class resources and other R related resources found in the library or internet.
• Any indications of cheating on this project will result in a failure in the course.
• The project should be turned in using blackboard. Note that you will turn in exactly three files: simfunction.txt This is a text file that contains the R function that you should create for Part 1. Name the
file exactly as specified.
project.pdf This is a PDF file that contains the code and results from the indicated simulation runs, along with an narrative that answers the question posed in Part 2. Be sure that you gather enough data from your simulations to justify your conclusions. Any graphics that you use should be imbedded in the PDF file using the LATEX package graphicx. Name the file exactly as specified.
help.pdf This is a PDF file generated using LATEX that contains a help file for your function written by in in the same style as the help files used in R. Name the file exactly as specified.
• Relax, take your time, and have fun!
STAT 415 and STAT 515 Project 1 Fall 2019 Page 2
As the last rays of sunshine cast an amber pall over your office window one fall evening, and you watch the final rays blink from the sky, your phone rings and interrupts your quiet solitude and snaps you back to reality. It is a consulting client, and you know this will be a tough phone call. This client has many questions, and few answers. To compound matters, they are always questions about probability, a subject which they seem to have no training in, and little intuition. That is why you are their consultant, to answer questions in a way that they can understand and appreciate. That is why you get those excellent consulting fees. You sigh and take the phone call, and immediately you feel a new headache rooting itself deep in you head. It is going to be a long night.
The client is the famed Antoine Gombaud, a rich gambler from France. Mr. Gombaud is fascinated with gambling games, and likes to entertain at parties with fashionable games involving dice and cards. He is suave and personable, but not good at mathematics, and so he has hired you to do his math for him. In particular he needs to know how to set payouts for the games that dreams up. The payouts need to be fair. Obviously, if he pays his friends to much for winning a game then he will soon lose all of his money. On the other hand, if he pays too little, then they will not want to play the games as they will lose all of their money. The usual way to set a payout for a game is based on expected winnings, or the long-term average amount won or lost by the gambler in repeated plays of a game. There are theoretical methods for computing these values, but Mr. Gombaud does not trust these methods. However, he does trust simulating the game on a computer, and looking at the average winnings over large number of simulated games. Thankfully, he also knows how to run R.
For example, consider the very simple game of flipping a fair coin once. Suppose that a gambler will win against Antoine if the coin flip is Heads. Suppose the gambler pays $1 to play the game Antoine is considering paying back $3 if the flip is Heads ($2 as a prize, plus the $1 that was bet for a net loss of $2). Otherwise, he will keep the $1. Standard probability theory easily solves this problem by stating the Antoine is expected to lose $0.50 per play of the game. But Antoine does not like theory, so the same question can be explored through simulation. We could simulate a large number, say b, coin flips using R. For each Heads observed in the simulation we would subtract $2 from the total winnings and if the flip is Tails then we would add $1 to the winnings (This is taken from Antoine’s viewpoint). We would then add up these simulated winnings and divide by b to get an estimate of the expected gain (or loss) for each play of the game.
The game that Antoine is interested in is only a little more complex. Antoine suggests rolling a six-sided fair die n times. If a six is not rolled in the n rolls of the die then the player receives w + 1 dollars, equal to the one dollar they payed to play the game and w dollars of winnings. In this case Antoine would have net winnings of −w dollars. If a six is rolled at least once in the n rolls then Antoine keeps the one dollar the player paid to play the game. In this case Antoine would have net winnings of one dollar.
1. Write a function in R that follows the specifications:
• The name of the function should be sim.game.
• The arguments of the function should be n, w, b, plot, seed, and …, where b has a default value of 1000, plot has a default value of FALSE, and seed is an optional argument.
• The function should simulate b plays of the game and keep track of how much Antoine would win or lose for each simulated play of the game.
• If seed is specified, then the function set.seed should be executed with the value specified by seed. Otherwise, the set.seed function should not be executed at all.
• The function should return a data frame with the following columns:
– Columns 1 through n should contain the results of the n rolls of the fair die (integer named “Roll 1” to “Roll n”).
– Column n + 1 should identify the winner as the player (“P”) or as Antoine (“A”) (factor named “Winner”).
– Column n + 2 should show how much Antoine won or lost on the game (double named “Winnings”).
– Column n + 3 should show the average winnings for Antoine over the games completed to that time
(double named “Average”).
• If plot=TRUE then the function should produce a plot. The horizontal axis of the plot should indicate the simulated game and the vertical axis should indicate the average winnings at that time. The plot should be nice looking with proper labels. The argument … should be passed to the plot command for optional plot specifications.
STAT 415 and STAT 515 Project 1 Fall 2019 Page 3
As an example, a run of the function should look like:
sim.game(n=5,w=2,b=10,plot=TRUE,col=”blue”)
Roll 1 Roll 2 Roll 3 Roll 4 Roll 5 Winner Winnings Average 1 1 1 2 3 3 P -2-2.0000000 2 6 2 4 4 1 A 1-0.5000000 3 3 1 6 2 1 A 10.0000000 4 2 6 3 2 2 A 10.2500000 5 2 2 1 4 1 P -2-0.2000000 6 4 1 5 2 5 P -2-0.5000000 7 2 5 2 2 6 A 1-0.2857143 8 5 3 4 3 3 P -2-0.5000000 9 1 2 4 2 3 P -2-0.6666667 10 4 2 3 1 2 P -2-0.8000000
In this case the following plot would be produced
Average Winnings from 10 Simulated Games
2 4 6 8 10
Game
Full credit will be given to functions that do at least come error checking. Turn in this function to Blackboard in a text file called simfunction.txt.
2. Run your function with n=10, b=100 for a range of values of w. Using these results, suggest a value of w that Antoine should use that would allow him to make some money, but not so much that players would not like to gamble with him. Note that w need not be an integer. Include the code and results from your runs, as well as your suggestions for w in a PDF file generated by LATEX named project.pdf.
3. Write a help file for your code in the same style as the help files used in R. Include a description of all of the arguments, the default values, how the function works, and what type of object it returns. Include one or two examples. This should be contained in a PDF file generated by LATEX named help.pdf.
Average
-2.0 -1.5 -1.0 -0.5 0.0