Homework Assignment #5 - Monte Carlo simulations



Homework 5 Assigned: September 25, 2014Due: October 3, 2014, 6:00 PM00CS-1004, Introduction to Programming for Non-majors, A-term 201400CS-1004, Introduction to Programming for Non-majors, A-term 2014 Homework 5 — Monte Carlo SimulationObjectivesWrite a simulation based on random numbersWrite a program in more than one moduleAssignment — Simulate the game of CrapsWrite a program that simulates the game of Craps and plots the probabilities of winning and losing.In Craps, a player rolls a pair of normal, six-sided dice. If this initial roll is 2, 3, or 12, the player loses. If this initial roll is 7 or 11, the player wins. With any other value for the initial roll, the player “rolls for point.” That is, the player keeps rolling until either a 7 or the value of the initial roll appears. If the initial roll appears before the 7, the player wins. If a 7 appears before the initial value of the roll, the player loses.In this simulation, you need to play lots of games — thousands of them — to determine the likelihood of winning or losing on the first roll, second roll, third roll, etc. The output of the program should be a pylab/matplotlib graph with two lines or two sets of points. One line shows the percentage of games that are won on the first roll, second roll, third roll, etc. The other line shows the percentage of games lost on the first roll, second roll, third roll, etc.Monte Carlo simulationA simulation based on “rolling the dice” to drive a computational model is called a Monte Carlo simulation. The simulation program “plays” the computational model over and over again and builds up a probabilistic characterization of the system that it is trying to model. Monte Carlo simulations are used to model systems that are too difficult or impossible to characterize by a set of equations in “closed form” but that are relatively easy to characterize by probabilities. A prime example is weather modeling, in which forecasters start with measureable parameters such as temperature, humidity, wind speed, etc., and then play out the physics by applying probabilities at an extremely local level to get a picture of the likelihood or sun, rain, or snow at the regional level.Random numbersTo carry out such simulations, one needs a way of modelling the randomness of such things as the throws of dice, the decay of a radioactive particle, or the impact of a movement of traffic on a highway. Fortunately, there is a long, detailed history of the study and use of random number generators in computer programs. Such models depend upon having a sequence of numbers that appear, for all practical purposes, to be “random” — that is unpredictable in value but adhering to some particular distribution. The function random() in the module random provides such a sequence of uniformly distributed numbers in the interval [0.0, 1.0) — i.e., greater than or equal to zero and strictly less than one.Try it:– In an IDLE shell, type “from random import random”. Then invoke the function random() many times and see what numbers it produces.Similarly, the function randrange(stop) in the same module provides a sequence of random integers in the range [0, stop) — i.e., from zero up to, but not including, stop. Try this, also.Seeding the Random number generatorThe advantage of a good random number generator is that it produces a sequence of seemingly unpredictable numbers that does not repeat. This is very good for modeling probabilistic behavior.The disadvantage of a good random number generator is that it produces a sequence of seemingly unpredictable numbers that does not repeat. This is terrible for debugging when you need to reproduce certain behavior on demand.To cope with this, random number generators have “seeds” — i.e., values for initializing the seemingly random sequence so that it can be reproduced for purpose of debugging.The method seed() in the module random allows you to reset the random number generator to a reproducible state, so that it will generate the same sequence again. The seed() function takes a string as its argument. Try it:– Restart your IDLE shell, type “from random import random, seed”, and then call seed() with your name as its argument.Next, call random() multiple times and note the values returned. The numbers still look a random sequence.Now, restart the shell and call seed() again with exactly same argument as before. Next, call random() multiple times again. You should see that the sequence of numbers is exactly the same as the previous time.Your programYour program for this assignment should consist of at least two modules — one for playing the game of craps and a separate one for keeping statistics. As usual, you should also have a wrapper for controlling the program and helping to test it. The seed should be set in the wrapper! This keeps it separate from the simulation itself.Playing the gameThe function playGame() should do exactly that — roll the pair of dice as many times as necessary to win or lose. Each roll of the dice is simulated by two calls to randrange(6), one for each die. Increase the result of each roll by one in order to get the number of spots on the face of that die. Do the same for the other die, and then add them together to get the result of the roll. Apply this result to the rules of the game to determine whether you have won, lost, or continue playing.When the game is either won or lost after a number of rolls, the playGame() function should return two values — a true/false value indicating whether the game was won or lost (true means won) and an integer indicating the number of rolls.Note: Returning multiple values from a function is just like Simultaneous Assignment as described in §2.5.3 of the textbook. That is, the return statement can take a set of values separated by commas. These can be assigned to an equal number of comma-separated variables on the left side of an assignment in the calling function.Keeping track of the resultsThe function tallyResults() should maintain two data structures — one for wins and one for losses. These indicate the number of games won or lost by the number of throws. The tallyResults() function should repeatedly call playGame() and increment the appropriate number of wins or losses for the number of throws returned.This data structure needs to be open-ended. That is, while most games may finish within, say, a dozen throws, some game may go for 20, 30, or more throws before recording a win or a loss. If you use an open-ended structure such as a list, you would be able to grow it if an unusually long game occurs. Of course, the intervening counts of wins and losses would be set to zero.Plotting and conclusionsOnce the simulation has been run for many, many games, the results should be plotted on a graph using pyplot/matplotlib. The x-axis should be the numbers throws needed to complete each game. The y-axis should be the percentage won (or lost) in that number of throws.You may plot one graph with two lines (won versus lost), or your may plot two separate graphs. You may choose to show points on the graph or not, as you feel best conveys your information.Save each graph to a file by clicking on the Save button at the bottom of the matplotlib window. Most graphs are saved in .png format. Be sure to submit your graph(s) along with your README file as described below.README documentYou should also include a short document in .txt or .doc format called README. In this document, you mustDescribe the data structure used to accumulate the results of playing many games, and Summarize your conclusions about what the graphs tell you about the game of Craps. In particular, you must answer the following questions from the Lecture Notes:–What is the probability that player wins eventually wins?What percentage of games are decided on 1st roll?2nd roll? 3rd roll? etc.?What is average number of rolls per game?SubmissionYour wrapper must be called HW5.py. It should prompt for the number of games being played, play that many games, and then produce a set of plots/graphs. It should also prompt the user for a string to use to seed the random number generator. If the user returns the null string, the random number generator should remain “unseeded” — i.e., let it start wherever it wants, which will be different each time it is invoked.GradingThis project is to be carried out in two-person teams. Both team members will receive the same project grade.This assignment is worth 100 points, allocated as follows:–Logical organization into separate .py files – 10 pointsDesign of data structure to capture numbers of wins and losses vs. numbers of throws – 10 pointsSeeding Random number generator (for reproducibility) – 10 pointsWrapper, including any test code, user prompting, seeding the random number generator, managing data – 10 pointsPlayGame() – 30 pointsCorrect use of random number generator for two dice – (10)Actually playing a whole game – (10)Accumulating results – (10)Output – 20 pointsCorrect use of pyplot/matplotlib to show results – (10)Submission of one or more graphs in support of README document (10)README document (as described above) – 10 pointsPenaltiesPenalty for any function longer than 25 lines – 10 points per function ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download