Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use python please, thank you! 2 Optimized table lookup. The Python code below implements a lookup table to generate a random variate from the Binomial

Use python please, thank you!

image text in transcribed

2 Optimized table lookup. The Python code below implements a lookup table to generate a random variate from the Binomial distribution Bin(n = 5, p = 3). Yi 0 1 2 3 4 5 Pi 1024 15 1024 90 1024 270 1024 405 1024 243 1024 e import random def tablelookuply,p): """Sample from y[i] with probabilities p[i]""'" u = random.random() sump = 0.0 for i in range(len(p)):- sumP += p[i] if us sump: return y[i] random.seed(123) y = [0,1,2,3,4,5) p = (1.0/1024, 15.0/1024, 90.0/1024, 270.0/1024,405.0/1024, 243.0/1024] 4 m = 1000000 valuetotal = 0.0 for k in range(m): d = tablelookup(yp) valuetotal += d print valuetotal/m We want this table lookup to be as fast as possible by making the average number of steps through the for loop in the tablelookup function as small as possible. (a) In each of the following cases, determine (and print out) the average number of steps over m trials. Use a suitably large value of m.. (i) y = [0,1,2,3,4,5) (ii) y = [5,4,3,2,1,0) with p suitably rearranged (iii) p arranged in descending probability order with y suitably rearranged As a check, make sure that the sample mean of the y values (the result valuetotal/m above) is the same each time (with perhaps some very small differences). Comment on which order gives the fastest table lookup. pyz116 2 Optimized table lookup. The Python code below implements a lookup table to generate a random variate from the Binomial distribution Bin(n = 5, p = 3). Yi 0 1 2 3 4 5 Pi 1024 15 1024 90 1024 270 1024 405 1024 243 1024 e import random def tablelookuply,p): """Sample from y[i] with probabilities p[i]""'" u = random.random() sump = 0.0 for i in range(len(p)):- sumP += p[i] if us sump: return y[i] random.seed(123) y = [0,1,2,3,4,5) p = (1.0/1024, 15.0/1024, 90.0/1024, 270.0/1024,405.0/1024, 243.0/1024] 4 m = 1000000 valuetotal = 0.0 for k in range(m): d = tablelookup(yp) valuetotal += d print valuetotal/m We want this table lookup to be as fast as possible by making the average number of steps through the for loop in the tablelookup function as small as possible. (a) In each of the following cases, determine (and print out) the average number of steps over m trials. Use a suitably large value of m.. (i) y = [0,1,2,3,4,5) (ii) y = [5,4,3,2,1,0) with p suitably rearranged (iii) p arranged in descending probability order with y suitably rearranged As a check, make sure that the sample mean of the y values (the result valuetotal/m above) is the same each time (with perhaps some very small differences). Comment on which order gives the fastest table lookup. pyz116

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

Select the maximum outgoing edges a node has in a tree.

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago