Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Create a script named monty_hall

Create a script named monty_hall

Computer Science

Create a script named monty_hall. This script should import random, and create a class named Simulation.

? Simulation should take an attribute, an integer, which represents the amount of doors that the simulation will use. When an instance of Simulation is created it should use this number to create a list of “zonk” strings. This list should be as long as the number. Meaning, if the number passed in is 2, it should create a list containing two “zonks” ([“zonk”,”zonk”]). It should then replace one of those items in the list to the string “car” at random. It should then pick a random item from the list which represents the door that the user has chosen. It should then remove a “zonk” from the list (the order of the zonk doesn't matter for this part, it can be the first zonk, it can be the last zonk or it can be a random zonk). It should then pick a random door that the user has not chosen as the alternate door (similar to Deal or No Deal).

? Simulation should contain a method named play_game which has 2 keyword arguments, switch (boolean, default value of False) and iterations (int, default value of 1). Iterations pertains to the amount of times that the game will be played in this Simulation instance. The goal of this method is to return the percentage of the amount of times that the game was won as a decimal (float). So for example, if my iterations is 3 (this means I play the game 3 times) and I win 2 of those 3 times, then my returned value is .66. If my iterations is 1 and I win 1 time then my returned value is 1. If my iterations is 4 and I dont win any games then my returned value is 0.

- The way to determine if a game has been won is to look at whether the door that the player chose is the “car” and the switch parameter is False or if the alternate door is the “car” and the switch parameter is True. If those conditions are met then it counts as a win. We will want to keep track of how many times we win and lose given how many iterations we are running and calculate the final win percentage.

? This script should contain an “if name == main” statement where you create an instance of simulation with 3 doors and you print the returned value from calling the play_game method. This instance should invoke the play_game method with switch as False and iterations as 100.

- One note: You may be so inclined to test your code using more iterations, keep in mind, this takes a lot of computational power. Be weary of adding too many 0s into your script. A sweet spot is 1000. You should notice that your returned value is somewhere near .66 for this instance.

Create a script named visualization. This script should import monty_hall, pandas and seaborn. This script should create a class named Plot.

? Plot should have 2 keyword parameters, doors (int) and simulations (int). Doors represents the amount of doors that a simulation instance will use simulations pertains to the amount of simulation instances that we will create. Plot should also create an attribute named sequence that will be a list that will eventually contain dictionaries that we will use to create a dataframe later. Plot should then contain a loop that will run for as many times as the value of simulations. Starting at 1, the loop will determine if the current iteration is odd or even. If even the loop should create an instance of simulation from the monty_hall namespace and invoke the play_game method. It will pass in the number of doors when created, then pass in switched as True and iterations will be passed in using the current iteration that we are on. If the current iteration number is odd then it will do the same thing but it will create an instance of simulation where the switched value is False. Using the returned percentage, the loop will append a dictionary to the sequence attribute where the keys are “iterations” (what our current iteration is), “percentage” (our returned percentage value), “doors” (how many doors our simulation uses) and “switched” (our boolean as a string representing whether we switched the door or not. After the loop is completed we will call the make_plot method by passing in the sequence attribute.

- For example: Lets say that my simulations parameter is 3. I will start at 1 and create that simulation instance where my iterations argument for the play_game method is 1 and append that dictionary to the list. I will then go to 2 and do the same where the iterations argument is 2. I will then go to 3 and do the same where the iterations argument is 3 and stop. ? Plot should contain a method named make_plot. This method will take an argument, named sequence which it will use in order to create a pandas Dataframe. We will then use the lmplot method of seaborn in order to create a plot object where x is “iterations”, y is “percentage”, data is the pandas dataframe that we created and “hue” is “switched”. You should then use the savefig method of the plot object in order to export a png of the visualization. The name of your png file should include the simulations attribute and the doors attribute.

? This script should contain an “if name == main” statement which will call an instance of Plot by passing in doors as 5 and simulations as 100.

? Write docstrings for each class and method.

INST326 • Fall 2020 • HW4 General Instructions - The name of your files should consist exclusively of lower-case letters, numbers, and underscores, and the file extension .py. Your filename should not start with a number. Background For this homework we will be creating a simulation of the famous Monty Hall Problem. For context on this problem watch this video which explains the math behind the Monty Hall problem. We will be writing two scripts, one script which simulates the Monty Hall Problem given different conditions that we determine (number of times that we play the game, whether or not we switch doors and how many doors there are). The other script will run simulations and use the conditions of the simulations that we run, as well as the percentage of times that we won in order to create a visualization where the x axis is how many times we played the game, and the y axis is the win percentage. What we should be seeing is that as we play the game more and more times, the win percentage starts to approach the statistical probability of a certain condition being met. Essentially, through this exercise we will be demonstrating a proof of the “Central Limit Theorem”. For more information on the central limit theorem please watch this video. You will know that you performed the homework correctly when the output of the second script is something similar to this image. In this case, the Monty Hall game is being simulated 400 times where half of those times the simulation is switching doors and half of those times it is not switching doors. Each simulation is using 3 doors to play the game. We can see that as the amount of times that we play the game increases, that all of the values cluster around the specific probabilities, where the probability of winning if we switch doors is 66 % and the probability of winning if we don't switch doors is 33 %. Script Instructions ? Create a script named monty_hall. This script should import random, and create a class named Simulation. ? Simulation should take an attribute, an integer, which represents the amount of doors that the simulation will use. When an instance of Simulation is created it should call the method set_doors using the number of doors. ? Set doors will use this number to create a list of “zonk” strings. This list should be as long as the number. Meaning, if the number passed in is 2, it should create a list containing two “zonks” ([“zonk”,”zonk”]). It should then replace one of those items in the list to the string “car” at random. It should then pick a random item from the list which represents the door that the user has chosen. It should then remove a “zonk” from the list (the order of the zonk doesn't matter for this part, it can be the first zonk, it can be the last zonk or it can be a random zonk). It should then pick a random door that the user has not chosen as the alternate door (similar to Deal or No Deal). ? Simulation should contain a method named play_game which has 2 keyword arguments, switch (boolean, default value of False) and iterations (int, default value of 1). Iterations pertains to the amount of times that the game will be played in this Simulation instance. The goal of this method is to return the percentage of the amount of times that the game was won as a decimal (float). So for example, if my iterations is 3 (this means I play the game 3 times) and I win 2 of those 3 times, then my returned value ? ? is .66. If my iterations is 1 and I win 1 time then my returned value is 1. If my iterations is 4 and I dont win any games then my returned value is 0. ? The way to determine if a game has been won is to look at whether the door that the player chose is the “car” and the switch parameter is False or if the alternate door is the “car” and the switch parameter is True. If those conditions are met then it counts as a win. We will want to keep track of how many times we win and lose given how many iterations we are running and calculate the final win percentage. ? This script should contain an “if name == main” statement where you create an instance of simulation with 3 doors and you print the returned value from calling the play_game method. This instance should invoke the play_game method with switch as False and iterations as 100. ? One note: You may be so inclined to test your code using more iterations, keep in mind, this takes a lot of computational power. Be weary of adding too many 0s into your script. A sweet spot is 1000. You should notice that your returned value is somewhere near .66 for this instance. Create a script named visualization. This script should import monty_hall, pandas and seaborn. This script should create a class named Plot. ? Plot should have 2 keyword parameters, doors (int) and simulations (int). Doors represents the amount of doors that a simulation instance will use simulations pertains to the amount of simulation instances that we will create. Plot should also create an attribute named sequence that will be a list that will eventually contain dictionaries that we will use to create a dataframe later. Plot should then contain a loop that will run for as many times as the value of simulations. Starting at 1, the loop will determine if the current iteration is odd or even. If even the loop should create an instance of simulation from the monty_hall namespace and invoke the play_game method. It will pass in the number of doors when created, then pass in switched as True and iterations will be passed in using the current iteration that we are on. If the current iteration number is odd then it will do the same thing but it will create an instance of simulation where the switched value is False. Using the returned percentage, the loop will append a dictionary to the sequence attribute where the keys are “iterations” (what our current iteration is), “percentage” (our returned percentage value), “doors” (how many doors our simulation uses) and “switched” (our boolean as a string representing whether we switched the door or not. After the loop is completed we will call the make_plot method by passing in the sequence attribute. ? For example: Lets say that my simulations parameter is 3. I will start at 1 and create that simulation instance where my iterations argument for the play_game method is 1 and append that dictionary to the list. I will then go to 2 and do the same where the iterations argument is 2. I will then go to 3 and do the same where the iterations argument is 3 and stop. ? Plot should contain a method named make_plot. This method will take an argument, named sequence which it will use in order to create a pandas Dataframe. We will then use the lmplot method of seaborn in order to create a plot object where x is “iterations”, y is “percentage”, data is the pandas dataframe that we created and “hue” is “switched”. You should then use the savefig method of the plot object in order to export a png of the visualization. The name of your png file should include the simulations attribute and the doors attribute. ? This script should contain an “if name == main” statement which will call an instance of Plot by passing in doors as 5 and simulations as 100. Write docstrings for each class and method. Grading Key ? Monty Hall ? Simulation Class ? Init ? Correctly sets doors or calls method to set doors or both ? play_Game ? Contains correct logic to play a simulation of the game ? Proper method or logic that sets code is present ? If name == main ? Follows the following instructions ? Create an instance of simulation with 3 doors ? print the returned value from calling the play_game method. ? invoke the play_game method with switch as False and iterations as 100. ? Visualizations ? Plot ? Init (its ok if this logic is within another method and the init is calling the method) ? Accepts the correct arguments ? Contains for loop that creates the correct instances based on the number of simulations. ? Creates sequence (list of dictionaries with data for each simulation) ? Calls the makeplot method ? Makeplot method ? Takes a sequence ? Uses seaborn to make plot ? If name == main ? Creates an instance of plot ? Docstrings

Option 1

Low Cost Option
Download this past answer in few clicks

16.89 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE