0

I want to uniformly sample from a simplex in python. I found the following in R but nothing so far in python

 runif_in_simplex(n, simplex)

EDIT: Is the following solution correct?

  1. generate N random numbers (N = no of vertices of simplex)
  2. Normalize these N numbers (divide by sum)
  3. Form the linear combination of N numbers and Vertice's coordinates.
MrFlick
  • 195,160
  • 17
  • 277
  • 295
RahimEnt
  • 199
  • 1
  • 1
  • 16
  • 1
    Does this answer your question? [Generating N uniform random numbers that sum to M](https://stackoverflow.com/questions/30658932/generating-n-uniform-random-numbers-that-sum-to-m) – Peter O. Dec 05 '20 at 08:27
  • `Is the following solution correct?` Assuming that your random numbers are floats in [0...1) range, your solution is incorrect – Severin Pappadeux Dec 05 '20 at 15:42

1 Answers1

3

Unit exponential variables is what you need (related post).

def runif_in_simplex(n):
  ''' Return uniformly random vector in the n-simplex '''

  k = np.random.exponential(scale=1.0, size=n)
  return k / sum(k)

The procedure that you proposed is not uniformly distributed in the simplex.

Refs

Onn, S., & Weissman, I. (2011). Generating uniform random vectors over a simplex with implications to the volume of a certain polytope and to multivariate extremes. Annals of Operations Research, 189(1), 331-342.