从n维单位单纯形均匀地随机采样 [英] Sample uniformly at random from an n-dimensional unit simplex
问题描述
从n维单位单纯形中均匀地随机采样是一种表达您想要n个随机数的奇特方法,
Sampling uniformly at random from an n-dimensional unit simplex is the fancy way to say that you want n random numbers such that
- 它们都是非负的
- 它们加起来等于一,并且
- n个非负数之和等于1的所有可能矢量都是同等可能.
在n = 2的情况下,您要从位于正象限的x + y = 1(即y = 1-x)线段中进行均匀采样. 在n = 3的情况下,您是从平面x + y + z = 1的三角形部分采样的,该部分位于R3的正八分圆中:
In the n=2 case you want to sample uniformly from the segment of the line x+y=1 (ie, y=1-x) that is in the positive quadrant. In the n=3 case you're sampling from the triangle-shaped part of the plane x+y+z=1 that is in the positive octant of R3:
(图片来自 http://en.wikipedia.org/wiki/Simplex . )
请注意,选择n个统一的随机数,然后对其进行归一化,使它们求和为1是行不通的.您最终偏向于减少极端数字.
Note that picking n uniform random numbers and then normalizing them so they sum to one does not work. You end up with a bias towards less extreme numbers.
类似地,选择n-1个均匀随机数,然后将nth取为1减去它们的总和,也会引入偏差.
Similarly, picking n-1 uniform random numbers and then taking the nth to be one minus the sum of them also introduces bias.
Wikipedia提供了两种算法来正确执行此操作: http://en.wikipedia.org/Wiki/Simplex#Random_sampling (尽管第二个目前声称仅在实践中是正确的,但从理论上讲是正确的.我希望对此有所了解或加以澄清.我起初是坚持警告:某某纸质声明Wikipedia页面上出现以下错误",其他人将其变成仅在实践中有效"的警告.)
Wikipedia gives two algorithms to do this correctly: http://en.wikipedia.org/wiki/Simplex#Random_sampling (Though the second one currently claims to only be correct in practice, not in theory. I'm hoping to clean that up or clarify it when I understand this better. I initially stuck in a "WARNING: such-and-such paper claims the following is wrong" on that Wikipedia page and someone else turned it into the "works only in practice" caveat.)
最后,问题是: 您认为Mathematica中最佳的单纯形采样实现方式是什么(最好是凭经验确认其正确性)?
Finally, the question: What do you consider the best implementation of simplex sampling in Mathematica (preferably with empirical confirmation that it's correct)?
相关问题
- Generating a probability distribution
- java random percentages
推荐答案
此代码可以正常工作:
samples[n_] := Differences[Join[{0}, Sort[RandomReal[Range[0, 1], n - 1]], {1}]]
基本上,您只需选择间隔[0,1]
上的n - 1
个位置以将其拆分,然后使用Differences
取每个块的大小.
Basically you just choose n - 1
places on the interval [0,1]
to split it up then take the size of each of the pieces using Differences
.
对此进行快速的Timing
显示,它比Janus的第一个答案要快一点.
A quick run of Timing
on this shows that it's a little faster than Janus's first answer.
这篇关于从n维单位单纯形均匀地随机采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!