Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.. UVL'uuv 11.1 I As we discussed in class, a Python dictionary makes a good data structure for storing a probability distribution. In this structure,

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
1.. UVL'uuv 11.1 "I As we discussed in class, a Python dictionary makes a good data structure for storing a probability distribution. In this structure, the keys of the dictionary represent outcomes or statements, and the values represent their corresponding probabilities. For example, the dictionary {"heads": 0.5, "tails": 0.5} represents the probability distribution of a fair coin ip. Our goal in this assignment is to implement some common functionality by creating a class that contains a dictionary representing a probability distribution, and has several methods for performing operations common in probability theory. We'll revisit this class in Programming HW 3 and 4, Where we'll subclass it to perform more specialized modeling tasks. Write a Python script called hw1 . py. It should contain a definition for a class Distribution. You may not use helper functions outside the class definition, but you can feel free to implement a main function for your own testing or experimentation purposes. 2.1. Documentation. Your script must contain a header docstring containing your name, ISTA 311, the date, and a brief description of the module. Each function/method must contain a doc- string. Each function docstring should include a description of the function's purpose, the name and type of each parameter, and the type and meaning of the function's return value. Omission of docstrings will result in a 2 point deduction (per function/method, and/ or for the header). 2.2. Template. A template for the class is provided with this assignment on D2L, with the _init-_ method already defined, and a plot pmf method that plots the probability distribution. You do not need to modify either of the provided methods. You can create an instance of the class by calling the constructor Distribution (initial_data) The Distribution instance created by this line depends on the type of data passed in as initial data: . If initial data is a dictionary, it is directly stored in the instance as self . dist. . If initial data is any other iterable object, such as a list or array, then the dictionary self . dist is built using the elements of initial data as the keys of the dictionary. A default probability of 1 is assigned as the value for each key, where n is the length of initial_data.o If initial_data is any non-iterable object, you will probably get an error. The internal dictionary is stored in an instance variable called dist. 2.3. Testing. A test script, testlml .py is on D2L. Run the script from the command line as $ python test_hw1.py Make sure that your script hwl .py is in the same folder. If you instead run 35 python test_hw1.py --make-plots then the script will create several plots that may be useful for diagnosing problems with the sample method. 2.4. Grading. Your submission will be graded by running it through the test script and examining your code. Your score will be determined by the percentage of automated tests passed, subject to any deductions (such as missing docstrings, or using a disallowed source of randomness in sample). 2.5. Method specications. Note: everything below is an instance method, so all methods should take self as their rst parameter, followed by any parameters described in the spec. You can implement the methods in any order, but they are ordered in approximately increasing order of complexity below. (Also, note that normalize can be used in conditional, so it may be helpful to implement it rst.) set_dist: This takes a Python dictionary representing a probability distribution, and as- signs it to the distribution stored in the instance, replacing whatever was already stored. get_dist: This takes no parameters and returns the dictionary stored in the instance. prob: This takes an event A in the form of an iterable Python object (such as a list, set, or array), and returns the total probability of elements of that object. Remove duplicates in the object by converting to a set before you do your calculation. probc: This takes an event A in the form of iterable Python object, and returns the total probability of all outcomes in the distribution that are not in the set. (The \"c\" is for \"complement.\" ) normalize: This takes no parameters. It modies the dictionary inplace to ensure that the total probability in the distribution is 1. Iterate through the dictionary and update each probability by dividing by the original sum of probabilities. conditional: This takes an event A (same as in prob and probe) and computes the conditional probability distribution, given the event. The conditional distribution should be a new Distribution object. The keys in its internal dictionary should be the outcomes in the event A. The values should be the probabilities for those outcomes, taken from the original dictionary self . dist, and rescaled so that they add up to 1. (Hint: you can rescale either by dividing by the probability of A, which you can calculate with the prob method of the original distribution, or by calling the normalize method of the new Distribution object.) Return the new Distribution. sample: This is the trickiest one. This method returns one of the keys of the dictionary, randomly selected with probability determined by the values stored in the dictionary. The Only source of randomness you may use for this is a function that generates a random oat between 0 and 1 (example: random.ra11dom() or numpy.ra.ndom.u.niform())

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions