mardi 30 juillet 2019

Random numbers with Gamma Distribution in Python

I defined a gamma distribution with following parameters: shape,scale = 4.2503, 7037. This dribuation is used to generate random numbers. The random numbers will be recalculated to the x-asix value.

I have another discrete number list (Please use this link Cars DataFrame to create this list. Sorry for this inconvenience.), which has only 59 numbers from 6800 to 112000. This 59 numbers are not equality divided in this range. I cut the discrete number list into 25 parts. So I have 26 bins. Every time when I generate a gamma random numbers, I use this number to find the corresponding bin in the discrete list. And then, use np.random to choose a number from [bin-1] to [bin]. Since the number is from a list, so it also have an index. The histogram above is actually the corresponding number.

Now I have a question: if I generate 460 random numbers at once, the histogram of those numbers doesn't look perfectly but it looks like a gamma distribution as I defined.

460 random numbers generated at once.

But if I generate 200, 200, 50 and 10 random numbers, and use the histogram to present all of them, I got such a wierd graph.

200-200-50-10

I'm not very good at math. But can anyone explain why would the difference between these two methods are too big? Thank you very much. Apart from this, if you have any suggestions for implementing the idea that I described above, please share it to me. Thank you!

import pandas as pd
import numpy as np
cats, bins = pd.cut(Cars['Preis'], 25, retbins=True)
list1 = Cars['Preis'].tolist()
for i in range(0,460):
    sample = np.random.gamma(shape, scale)
    while sample < min(list1) or sample > max(list1): # list1 is the discrete number list
        sample = np.random.gamma(shape, scale)
    bin_idx = np.digitize(sample,bins)
    if  0 < bin_idx < len(bins):
        for index, row in Cars.iterrows():
            if bins[bin_idx-1] < row.Preis <= bins[bin_idx]:
                choice_idx.append(index)
            else:
                pass
        if len(choice_idx) != 0:
            randomchoice = np.random.choice(choice_idx)
    else:
        target = min(list1, key=lambda x:abs(x-sample))
        randomchoice = list1.index(target)
    test.append(randomchoice)




Aucun commentaire:

Enregistrer un commentaire