Question: 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!

Use python please, thank you! 2 Optimized table lookup. The Python code

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!