Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Random number generators initially provide uniformly distributed random numbers

ID: 3782228 • Letter: R

Question

Random number generators initially provide uniformly distributed random numbers in a interval of unit length, but often we need to sample from other distributions. Suppose that we wish to simulate a random variable n drawn from P_binom(n, N = 3, p). We can do this by partitioning the unit segment into four bins of width (1 - p)^3, 3p(1 - p)^2, 3p^2(1 - p) and p^3, corresponding to n = 0, 1, 2, 3. The width of each bin is thus proportional to the probability of occurrence. To sample from P_binom(n, N, p) one thus simply draws a uniformly distributed random number and finds the bin index into which that number falls. Write a function binom_setup that accepts a value of p and returns a list of locations of the bin edges appropriate for N=5. Write another function binom_dist that accepts the list of bin edges as input and returns a single binomially distributed random variable. Write a short piece of wrapper code that first calls binom_setup and then generates a list of 100 binomially distributed random variables. Plot a (normalized if possible) histogram of the data, find the sample mean (x) and variance sigma^2 and compare to the expected values for large N, (x) = Np and sigma^2 = Np(1 - p). Repeat for 1000 random numbers and comment.

Explanation / Answer

A) you can use the function called numpy.histogram for the first part of your question

B) you can use the function called numpy.random.variable for this part