Skip to content Skip to sidebar Skip to footer

How Can I Generate Random Variables Using Np.random.zipf For A Given Range Of Values?

I have a given price range and i had used random uniform to get random generated random results from it. How can i introduce np.random.zipf to do the same ? i have tried the follow

Solution 1:

While @RobinNicole is right wrt Zipf distribution, you could simulate truncated Zipf using discrete sampling. Along the lines

import numpy as np
from matplotlib import pyplot as plt

defZipf(a: np.float64, min: np.uint64, max: np.uint64, size=None):
    """
    Generate Zipf-like random variables,
    but in inclusive [min...max] interval
    """ifmin == 0:
        raise ZeroDivisionError("")

    v = np.arange(min, max+1) # values to sample
    p = 1.0 / np.power(v, a)  # probabilities
    p /= np.sum(p)            # normalizedreturn np.random.choice(v, size=size, replace=True, p=p)

min = np.uint64(3)
max = np.uint64(8)

q = Zipf(1.2, min, max, 10000)
print(q)

h, bins = np.histogram(q, bins = int(max-min+1),range=(min-0.5,max+0.5))
print(h)
print(bins)

plt.hist(q, bins = bins)
plt.title("Zipf")
plt.show()

Will make graph like this

enter image description here

Solution 2:

You cannot tune the parameter of the Zipf law to restrict it to a given interval as you do it with the uniform distribution. The reason for that is that the Zipf distribution is always defined on the set of all the positive integers independently of its parameters.

Post a Comment for "How Can I Generate Random Variables Using Np.random.zipf For A Given Range Of Values?"