Reputation: 510
I am seeking a random array with non-normal distribution and values in some defined interval, say, from -100 to 100. I cannot see how to do that with the arguments available in numpy.random.name_of_distribution. For example,
k = np.random.noncentral_chisquare(1, 50, 100)
has arguments (degrees of freedom, noncentrality and size). The range of values seems to depend on where I set that noncentrality; in other words, changing it to 90 moves the top end of the distribution up to 120 or 130.
np.random.exponential(10,100)
is producing a range from 0 to 60 or so. Must one be clever and rescale the output algebraically, or is there a quicker fix?
Upvotes: 1
Views: 807
Reputation: 19854
If the input distribution has a bounded range, then rescaling algebraically is the simplest solution. However, your chosen example of a noncentral_chisquare
is unbounded above. In that case you'll need to use something like an acceptance/rejection scheme to throw away values larger than some upper bound. That can be done either before or after algebraic rescaling, as long as you adjust the rescaling parameterization appropriately.
There are other alternatives if you're generating the target distribution directly via inversion sampling, but acceptance/rejection is going to be your best bet with library functions.
Upvotes: 2