0

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
shimeji42
  • 313
  • 1
  • 11

2 Answers2

1

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))
Peter O.
  • 32,158
  • 14
  • 82
  • 96
0

From what I understood, you want to compute the beta function's probability distribution function.

You can do so by simply applying scipy.stat's beta.pdf function on an numpy array defined by an arange. Example given below for alpha = beta = 0.5:

from scipy.stats import beta
import numpy as np

def arange_beta(start, stop, step):
    x = np.arange(start, stop, step)
    return beta.pdf(x, a=0.5, b=0.5)
Seon
  • 3,332
  • 8
  • 27
  • Thanks! I meant something different, i.e. how to generate a set of numbers whose distribution would fit a given the beta distribution. – shimeji42 Apr 20 '21 at 16:52