Solved – Gamma Distribution for generating random numbers

gamma distributionpythonrandom variablerandom-generation

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.

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.

This question is a very tiny part of my project. I have another discrete number list, which has only 60 numbers from 6800 to 112000. This 60 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.

Please use this link Cars DataFrame to create the Cars Dataframe. Sorry for this inconvenience.

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)

Best Answer

The problem is in your implementation. We'd need to see your code to be sure. Here is mine:

enter image description here

from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt

shape,scale = 4.2503, 7037
Gamma = gamma(a = shape, scale = scale)
x = np.arange(1,100000)

fig, ax = plt.subplots(ncols = 3, nrows = 1, figsize = (20,5), sharex = True)
ax[0].plot(x, Gamma.pdf(x));

ax[1].hist(Gamma.rvs(460), edgecolor = 'white', bins = 50);


samps = np.concatenate([Gamma.rvs(j) for j in [200, 200, 50 , 10]])
ax[2].hist(samps, edgecolor = 'white', bins = 50);