如何在python中计算加权置换概率

问题描述 投票:0回答:1

我有一个数字字符串0-12,并且具有与之关联的权重。权重数组如下:[150000, 150000, 50, 150000, 150000, 800, 130000, 130000, 25000, 100000, 100000, 100000]

我已经在Python中模拟了数字0-12的所有组合,但是我想编写一些其他代码来模拟选择特定组合而不进行替换的可能性。例如,选择(1,2,5,3)的概率应为(150000/total weight)*(150000/total weight-150000)*(150000/total weight-150000-150000)*(50/total weight-150000-150000-150000-50)

由于“没有替换”部分,我不知道如何编码,我正在考虑使用一个for循环,该循环查看排列中位置的特定数字,并确定在该位置选择该数字的可能性位置,但希望有一种方法可以查看每个单独的排列。

python combinations permutation computer-science probability
1个回答
0
投票

我不知道为什么会有一串数字,但这是一个整数列表的有效示例,我认为这更有意义:

import random

weights =  [150000, 150000, 50, 150000, 150000, 800, 130000, 130000, 25000, 100000, 100000, 100000, 10000]
total_weight = sum(weights)
l = list(range(13))

tmp_weight = 0
new_weight = 1
for i in range(5):
    number = random.choice(l)
    print(number)
    new_weight *= weights[number]/(total_weight-tmp_weight)
    tmp_weight += weights[number]
    l.remove(number)

print(new_weight)

我不确定逻辑是否如您所愿,因为您的示例不清楚。

© www.soinside.com 2019 - 2024. All rights reserved.