产生N个均匀随机数的总和为M [英] Generating N uniform random numbers that sum to M

查看:288
本文介绍了产生N个均匀随机数的总和为M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问过,但我从来没有真正见过一个很好的答案。

This question has been asked before, but I've never really seen a good answer.

  1. 我要产生8随机数的总和为0.5。

  1. I want to generate 8 random numbers that sum to 0.5.

我想每个号码是随机从均匀分布(即简单的功能下将无法正常工作,因为数字将不会被均匀分布)选择。

I want each number to be randomly chosen from a uniform distribution (ie, the simple function below will not work because the numbers will not be uniformly distributed).

def rand_constrained(n,tot):
    r = [random.random() for i in range(n)]  
    s = sum(r)
    r = [(i/s*tot) for i in r] 
    return r

在code应该具有普遍性,这样就可以产生N个均匀随机数的总和M(其中M是正浮点数)。如果可能的话,可以请你也解释了(或显示有图)为什么你的解决方案,在适当的范围内均匀产生随机数?

The code should be generalizable, so that you can generate N uniform random numbers that sum to M (where M is a positive float). If possible, can you please also explain (or show with a plot) why your solution generates random numbers uniformly in the appropriate range?

有关问题的错过了标记:

Related questions that miss the mark:

<一个href="http://stackoverflow.com/questions/3589214/generate-multiple-random-numbers-to-equal-a-value-in-python">Generate多个随机数等于在python 值(目前接受的答案并不一致 - 另一个答案是唯一的均匀与整数作品)

Generate multiple random numbers to equal a value in python (current accepted answer isn't uniform--another answer which is uniform only works with integers)

<一个href="http://stackoverflow.com/questions/2640053/getting-n-random-numbers-that-the-sum-is-m?lq=1">Getting N个随机数的总和为M (在Java中同样的问题,目前公认的答案是完全错误的,也没有答案,分布均匀)

Getting N random numbers that the sum is M (same question in Java, currently accepted answer is just plain wrong, also no answers with uniform distribution)

产生N个随机整数那笔到M在研发(同样的问题,但在研发与正常 - 不统一 - 分布)

Generate N random integers that sum to M in R (same question, but in R with a normal--not uniform--distribution)

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

推荐答案

您所要求的似乎是不可能的。

What you are asking for seems to be impossible.

不过,我会再跨preT你的问题,使其更有意义,并有可能解决。你需要的是对七维超平面 X_1 + X_2 + ... + x_8 = 0.5 的概率分布。由于该超平面是无限的范围内,就整体而言超平面的均匀分布将不起作用。 (?),你可能想要的是超平面的块,所有的 x_i&GT; 0 。该区域是一个单纯形,三角形的一般化,并且在单纯形的均匀分布是狄利克雷分布的特殊情况。

However, I will re-interpret your question so that it makes more sense and is possible to solve. What you need is a probability distribution on the seven-dimensional hyperplane x_1 + x_2 + ... + x_8 = 0.5. Since the hyperplane is infinite in extent, a uniform distribution on the whole hyperplane will not work. What you probably(?) want is the chunk of the hyperplane where all the x_i>0. That region is a simplex, a generalization of a triangle, and the uniform distribution on the simplex is a special case of the Dirichlet Distribution.

您可能会发现本节中的狄氏分布维基百科的文章,弦切,特别是启发性。

You may find this section of the Dirichlet Distribution Wikipedia article, string cutting, particularly illuminating.

执行

维基百科的文章给出了在Python以下实现在随机数生成部分:

The Wikipedia article gives the following implementation in Python in the Random Number Generation section:

params = [a1, a2, ..., ak]
sample = [random.gammavariate(a,1) for a in params]
sample = [v/sum(sample) for v in sample]

你可能(?)想什么情况下,所有的 Al = 1 这会导致对单纯的均匀分布。在这里, K 对应 N 在你的问题的数量。为了获得,而不是 1 的样本,以和为 M ,只是乘样品 M

What you probably(?) want is the case where all ai=1 which results in a uniform distribution on the simplex. Here k corresponds to the number N in your question. To get the samples to sum to M instead of 1, just multiply sample by M.

更新

感谢塞韦林Pappadeux的指出gammavariate可以在极少数情况下返回无穷大。这在数学上是不可能的,但可以在浮点数方面的原因有实现的神器。我的建议来处理这种情况下是样品首先计算后,检查它;如果任何样品的组件都是无穷大,将所有的非无穷成分为0,将所有的无穷组件为1。然后,当的计算,成果如喜= 1,其他所有X的= 0 喜= 1/2,XJ = 1/2,所有其他X的= 0 将导致统称角样品和边缘样本。

Thanks to Severin Pappadeux for pointing out that gammavariate can return infinity under rare circumstances. That is mathematically "impossible" but can occur as an artifact of the implementation in terms of floating point numbers. My suggestion for handling that case is to check for it after sample is first calculated; if any of the components of sample are infinity, set all the non-infinity components to 0 and set all the infinity components to 1. Then when the xi are calculated, outcomes like xi=1, all other x's=0, or xi=1/2, xj=1/2, all other x's=0 will result, collectively "corner samples" and "edge samples".

另一个非常低概率的可能性是用于gammavariates的总和溢出。我猜想,我们可以通过整个底层伪随机数序列运行,并没有看到这种情况发生,但理论上是可能的(取决于底层伪随机数生成器)。这种情况可以通过重新调整处理样品,例如样品中的所有元素划分ñ 的gammavariates经计算后,但的计算的X前。就个人而言,我不会理会,因为赔率是如此之低;程序崩溃是由于其他原因,将有较高的概率。

Another very-low-probability possibility is for the sum of the gammavariates to overflow. I would guess that we could run through the entire underlying pseudo-random number sequence and not see that happen, but theoretically it is possible (depending on the underlying pseudorandom number generator). The situation could be handled by rescaling sample, e.g., dividing all elements of sample by N, after the gammavariates have been calculated but before the x's are calculated. Personally, I wouldn't bother because the odds are so low; program crashes due to other reasons would have higher probability.

这篇关于产生N个均匀随机数的总和为M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆