0

I'm trying to generate a frozen discrete Uniform Distribution (like stats.randint(low, high)) but with steps higher than one, is there any way to do this with scipy ?
I think it could be something close to hyperopt's hp.uniformint.

abdelgha4
  • 351
  • 1
  • 16

2 Answers2

1

rv_discrete(values=(xk, pk)) constructs a distribution with support xk and provabilities pk.

See an example in the docs: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.html

ev-br
  • 24,968
  • 9
  • 65
  • 78
1

IIUC you want to generate a uniform discrete variable with a step (eg., step=3 with low=2 and high=10 gives a universe of [2,5,8])

You can generate a different uniform variable and rescale:

from scipy import stats
low = 2
high = 10
step = 3
r = stats.randint(0, (high-low+1)//step)
low+r.rvs(size=10)*step

example output: array([2, 2, 2, 2, 8, 8, 2, 5, 5, 2])

mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thank you, this indeed generate the required results, but it does not generate a _frozen distribution_ for later use. – abdelgha4 Sep 12 '21 at 18:09
  • 1
    Nice! (+1) . It's always refreshing to see a simple DIY solution irrespective on if a heavy framework implements a one-liner. NB : you can use np.random.randint and avoid the scipy.stats dependency altogether. – ev-br Sep 12 '21 at 20:09
  • @ev-br thanks, I also like numpy's API a lot, I often find scipy's a bit too convoluted despite its incredible number of methods ;) – mozway Sep 12 '21 at 20:16