0

My approach

N=20
X1 = np.zeros((N,))
X2 = np.zeros((N,))
X3 = np.zeros((N,))
for i in range(N):
    while (X1[i]+X2[i]+X3[i]>1+sys.float_info.epsilon) or (X1[i]+X2[i]+X3[i]<1-sys.float_info.epsilon):
        X1[i]= np.random.random()
        X2[i]= np.random.random()
        X3[i]= np.random.random()

This is not going to work because the computer program will run for a long time. Will there any smart approach?

Ceci Chaung
  • 11
  • 1
  • 3
  • because it require X1 X2 X3 be uniform distribution from 0 to 1 – Ceci Chaung Feb 24 '21 at 13:42
  • Pretty sure that's not possible mathematically for n>2. Your rejection sampling will make the final distributions non-uniform, even with the method you show. – Daniel F Feb 24 '21 at 13:57

1 Answers1

2

You can't have a uniform distribution on all 3 variables.

In any valid triple, at least 2/3 of the variables must be < 1/2. If you want all the variables to have the same distribution, then the average density <= 1/2 must be at least twice the average density >= 1/2.

One particularly nice way of getting 3 variables that sum to 1 is to choose a random point on the surface of the unit sphere, and then use x2, y2, z2.

Amazingly, choosing a point on the unit sphere is very simple. If you choose a uniformly distributed height, and a uniformly distributed longitude, then results will be uniformly distributed on the surface.

If you choose your 3 variables this way, then their square roots will be uniformly distributed. 70% of values will be < 1/2, which agrees with the statement above.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87