Search code examples
pythonsamplenormal-distributionprobability-distribution

Generate a deterministic set of values from non-uniform distributions


Numpy has some routines like np.linspace that creates an array of evenly spaced numbers.

Is there a similar way to generate non-evenly spaced numbers that follow a specific distribution like the normal or beta distribution? I know that there are lots of functions that can create a random sample, but I am looking for a deterministic set of values that fit a distribution.

For example, like this:

arange_normal(n=10, mean=10, std=1)
[1, 5, 8, 9, 10, 10, 11, 12, 15, 19] 

The numbers here are guessed, but the idea is that they would give a perfect fit for the specified distribution. Similarly, I would also be looking for something like arange_beta.


Solution

  • What you may be looking for is the quantiles of the beta distribution. You can get them using SciPy's scipy.stats.beta.ppf method. The following code prints 20 evenly spaced quantiles:

    import numpy as np
    import scipy as sp
    print(sp.stats.beta.ppf(np.linspace(0,1,20),a=0.5,b=0.5))
    

    Note that other distributions, such as the normal distribution, cover either or both halves of the real line, so that their values at 0 and/or 1 may be infinity. In that case, you have to choose a slightly smaller domain for linspace, such as this example:

    import numpy as np
    import scipy as sp
    print(sp.stats.norm.ppf(np.linspace(0.001,0.999,20),loc=0,scale=1))