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.


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]:
        if len(choice_idx) != 0:
            randomchoice = np.random.choice(choice_idx)
        target = min(list1, key=lambda x:abs(x-sample))
        randomchoice = list1.index(target)
  • $\begingroup$ could you post the code used to generate these? it looks fine for me using something like np.concatenate([np.random.gamma(4.2503, scale=7.037, size=i) for i in [200, 200, 50, 10]]) $\endgroup$ – confused00 Jul 30 '19 at 15:33

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);
| cite | improve this answer | |
  • $\begingroup$ Thank you for your answer. I updated my post. $\endgroup$ – WJPING Jul 30 '19 at 16:09
  • 2
    $\begingroup$ @WJPING You should edit your code into a minimal working example. We don't have access to the Cars data frame, so we can't run or edit your example $\endgroup$ – Demetri Pananos Jul 30 '19 at 16:28
  • $\begingroup$ Hi @DemetriPananos I use a link to share the Cars Dataframe. Hope you will check. Thank you $\endgroup$ – WJPING Jul 30 '19 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.