0

Basically I'm trying to create a curve using scipy's random variable state but I'd like to set limits for the random value generated.

Basically I'm looking for something like:

from scipy import stats

data = stats.gamma.rvs(2, loc=1.5, scale=2, size=10000, min=800, max=1000)

Or is it something that you preface in scipy?

Plant_Boy
  • 13
  • 4

1 Answers1

1

You cannot set boundaries for a gamma distribution but you can clamp resulting values to [minimum, maximum]

import numpy as np
from scipy import stats

data = stats.gamma.rvs(2, loc=1.5, scale=2, size=10000)
print(data) # [1.55651126 7.25914918 5.03753265 ... 5.56524255 3.05888237 2.17275238]

# clamp to [minimum, maximum]
minimum = 3
maximum = 5
# assert minimum < maximum

data = np.where(data < minimum, minimum, data)
data = np.where(data > maximum, maximum, data)
print(data) # [3.         5.         5.         ... 5.         3.05888237 3.        ]

Adjusting for your comment: if you want to sample n elements in [minimum, maximum] without clamping you could do something like:

from scipy import stats

minimum = 3
maximum = 5

sampled, n = [], 10_000
while len(sampled) != n:
    data = stats.gamma.rvs(2, loc=1.5, scale=2, size=n - len(sampled))
    sampled.extend(data[(minimum <= data) & (data <= maximum)])
Stefan B
  • 1,617
  • 4
  • 15
  • Thanks for the reply and confirmation on what I was coming to the conclusion of. I was looking to create a set of data the same length and features as a similar data set. Currently I'm running a loop to calculate a new dataset, storing it in a pandas series, and trimming off the new values that lie outside of the original data min/max values. Then running the .rvs again to create values in the min/max boundaries to append onto the new dataset. – Plant_Boy Feb 13 '21 at 13:34
  • Hopefully the edit now better answers your question :) – Stefan B Feb 13 '21 at 13:42