1

I want to generate normal distributed values and have found this post: How to specify upper and lower limits when using numpy.random.normal However when I run the following code I get negative values below the lower limit as well

seed = 1
random.seed(seed)
np.random.seed(seed)

lower = 100_000
mean =  600_000_000
sigma = 700_000_000
nrows = 2000
upper = 10000000000


series = pd.Series(stats.truncnorm(
                    (lower - mean) / sigma,
                    (upper - mean) / sigma,
                    loc=mean,
                    scale=sigma,
                )
                .rvs(size=nrows)
                .astype(int))
series.hist()

enter image description here

What can I do to only get values above the lower limit?

Hans Geber
  • 111
  • 8
  • 1
    Your code looks alright. I don't get any values below the lower limit. – fsl Jan 24 '22 at 13:27
  • that's strange, I have no idea why we could have different results, I added a seed, but I still get the same histogram – Hans Geber Jan 24 '22 at 13:45
  • so I always get -2147483648 as the negative number on my local pc. I tried in colab and there I also had no issues – Hans Geber Jan 24 '22 at 14:53
  • 1
    Try scaling your parameters down by an order of magnitude and run again. My only guess is that you are overflowing somewhere.. – fsl Jan 24 '22 at 15:04
  • okay so I tested, on colab it gives me correctly 2257122278 on my local pc it converts it to -2147483648. It would be great if I could keep the magnitude – Hans Geber Jan 24 '22 at 15:05
  • I just found out, that it is the .astype(int) conversion that generates the error – Hans Geber Jan 24 '22 at 15:08
  • 1
    so I replaced it with .astype(np.int64) and it works now. Thanks a lot for your help! – Hans Geber Jan 24 '22 at 15:09

1 Answers1

0

It's solved, the problem was with the .astype(int) conversion, with .astype(np.int64) it works just fine now

Hans Geber
  • 111
  • 8