pythonnumpytransformationdirichlet

Transformed Dirichlet array with range [-1,1] in numpy


A random vector sampled from the Dirichlet distribution contains values that fall in the domain [0,1] and they sum to 1. In numpy it can be programmed like this for a vector size of 5:

x = numpy.random.dirichlet(np.ones(5))

Instead, I would like a random vector that contains values that are [-1,1] and sum to 1, which I was told can be achieved by transforming the Dirichlet generated x vector as y = 2x -1

Below is an attempt at this transformation. The script doesn't work properly however because y doesn't sum to 1 as needed. How can it be fixed, or could it be that y = 2x -1 does not do what they said?

x = numpy.random.dirichlet(np.ones(5))
y = 2*x -1
print(x, np.sum(x))
print(y, np.sum(y))

which outputs:

[0.0209344  0.44791586 0.21002354 0.04107336 0.28005284] 1.0
[-0.9581312  -0.10416828 -0.57995291 -0.91785327 -0.43989433] -3.0000000000000004

Solution

  • Try y=1/(dimension/3)-2*x. That worked for me.