【问题标题】:array of Weighted random numbers with constant sum总和为常数的加权随机数数组
【发布时间】:2018-06-17 20:45:22
【问题描述】:

是否有任何众所周知的算法可以生成具有相同常数和的加权随机数数组?

这个问题可以换一种说法:比如说,我的总价值是 20。应该分成 3 个部分,每个部分的权重分别为 2、4、3。 所以,我需要 3 个随机数,总和为 20,但分布将遵循权重。

我试过了:

Range=20
W=[2,4,3]
Prob=[i/float(sum(W)) for i in W]
Weighted_array=np.random.multinomial(Range, Prob)

还有更好的选择吗?

【问题讨论】:

  • 我在 python 中尝试过 random.multinomial()。
  • 请提供更多细节。你的意思是什么类型的数字——只有正整数,还是允许浮点数?您所说的“分配将遵循重量”是什么意思?您希望结果采用哪种概率分布 - 某种均匀分布在所有可能性上,您不在乎,还是其他?
  • 抱歉不清楚。我需要生成整数。对于该示例, weighted_array 可以是:[4,10,6],[3,12,5] 等。“分布将遵循权重”意味着生成的数组元素将根据 W 个数组元素进行加权。希望清楚。
  • 但是你有 20 个潜在的数字被采样并且只有三个权重。假设我将您的代码用作黑匣子,采样 100 万个样本,然后将它们放在从 1(或零?有点不清楚)到 20 的直方图中。这三个权重将如何表现出来?

标签: python random statistics


【解决方案1】:

象征性:

这是n=3 变量的线性丢番图方程。要在 python 中解析解决它,您可以使用sympy,如this answer

from random import randint
from sympy.solvers.diophantine import diop_linear
from sympy.abc import x, y, z

# Our original equation is 2 * x + 4 * y + 3 * z = 20
# Re-arrange to 2 * x + 4 * y + 3 * z - 20 = 0, and input the left side to sympy
general_solution = diop_linear(2 * x + 4 * y + 3 * z - 20)

def get_random_valid_triple():
    t0,t1 = general_solution[2].free_symbols
    # You can pick whatever bounds you want here
    a = randint(-100, 100)
    b = randint(-100, 100)
    solution = [s.subs({t0: a, t1:b}) for s in general_solution]
    print(solution)
# Get a random solution
get_random_valid_triple()

蛮力:

或者,至少对于较小的n 和每个变量的严格界限,您可以预先计算所有可能的解决方案并使用random.choice 每次选择一个。例如,我们将所有变量限制为正数,然后将它们强制在[0, 20 / coefficient] 中,并且只有 14 个解决方案。我们可以在python中生成这些如下:

import random
import itertools
n = 20
coefficients = [2, 4, 3]
valid_solutions = []
ranges = [range(0, n // k + 1) for k in coefficients]
for value in itertools.product(*ranges):
    if sum(value[j] * i for j, i in enumerate(coefficients)) == n:
        valid_solutions.append(value)

print("All solutions:")
print("\n".join(str(i) for i in valid_solutions))
print("Random solution:")
print(random.choice(valid_solutions))

这会产生:

All solutions:
(0, 2, 4)
(0, 5, 0)
(1, 0, 6)
(1, 3, 2)
(2, 1, 4)
(2, 4, 0)
(3, 2, 2)
(4, 0, 4)
(4, 3, 0)
(5, 1, 2)
(6, 2, 0)
(7, 0, 2)
(8, 1, 0)
(10, 0, 0)
Random solution:
(10, 0, 0)

【讨论】:

  • 非常感谢您的回答。但问题是我需要 n 个变量的通用解决方案。因为我的数组 W 可以有 100 个或更多元素。所以,我认为丢番汀不会是有效的。
猜你喜欢
  • 2013-05-28
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多