numpy 中范围 [-1,1] 的变换狄利克雷数组 [英] Transformed Dirichlet array with range [-1,1] in numpy
问题描述
从狄利克雷分布中采样的随机向量包含落在域 [0,1] 中的值,并且它们的总和为 1.在 numpy 中,可以像这样对向量大小为 5 进行编程:
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))
相反,我想要一个包含 [-1,1] 和总和为 1 的值的随机向量,其中 我被告知可以通过转换狄利克雷生成的x
向量为 y = 2x -1
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
下面是这种转变的尝试.然而,该脚本无法正常工作,因为 y
根据需要不等于 1.如何修复,或者 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))
输出:
[0.0209344 0.44791586 0.21002354 0.04107336 0.28005284] 1.0
[-0.9581312 -0.10416828 -0.57995291 -0.91785327 -0.43989433] -3.0000000000000004
推荐答案
尝试 y=1/(dimension/3)-2*x
.这对我有用.
这篇关于numpy 中范围 [-1,1] 的变换狄利克雷数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!