具有条件概率的随机选择

Random selection with conditional probabilities

我有列表说 y = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10],我想用条件概率从中随机选择。列表中大于 10 的数字有 0.8 的概率被选中,而其余的有 0.2 的概率被选中。

试试这个: 它使用列表中的 np.random.choice 个元素成员,其中包含一个列表,其中包含每个元素的概率 for 循环将从输入列表中生成该列表:

import numpy as np
y = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
t=[]
sup=0
inf=0
for j in y:
    if j>10:
        sup=sup+1
    else:
        inf=inf+1
print(sup)
print(inf)
p=[]
for i in y:
    if i>10:
        p.append(1/sup)
    else:
        p.append(0/inf)
print(p)
x=np.random.choice(y, 100, p=p)

print(x)

输出

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.25, 0.25, 0.25, 0.0]
[12 23 23 43 23 12 12 43 24 12]

概率为 0.5 和 0.5 的另一个输出:

[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.125, 0.125, 0.125, 0.125, 0.05]
[24 24 43  8  4 12 23 24  6  5]

另一个:

[0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.2, 0.2, 0.2, 0.2, 0.02]
[24 43 23 24  7 24 12 12 43 24]

由于random.choice 提供统一分布,您必须分两步进行。首先 select 值组之间(低于 10 和高于 10)。然后select组内的一个值。

要获得各组之间的不同概率,您可以创建一个列表,其中包含每个组的适当重复次数。例如,对于 0.2 和 0.8,您将在列表中有 2 个 "below10" 组实例和 8 个 "above10" 组实例。这会将正则分布转换为相对于每个组的加权分布。

import random

treshold = 10
y       = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
group1  = [v for v in y if v <  treshold]
group2  = [v for v in y if v >= treshold]

def getValue():
    group = random.choice([group1]*2 + [group2]*8)
    return random.choice(group)

要测试分布是否符合要求,您可以多次使用该函数并计算每个组中的值被select编辑了多少次。

lowCount  = 0
highCount = 0
N         = 10000
for _ in range(N):
    v = getValue()
    if v < treshold:
        lowCount += 1
    else:
        highCount += 1
print(round(lowCount/N,2),round(highCount/N,2))

# 0.2 0.8

如果你只有 2 个组,你可以对 selection 使用一个简单的 if-else 语句:

def getValue():
    return random.choice(group1) if random.random() <= 0.2 else random.choice(group2)

编辑 对于概率为 0.9 的单个值(比方说 23),方法是相同的:

y       = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
group1  = [23]
group2  = [v for v in y if v not in group1]
def getValue():
    return random.choice(group1) if random.random() <= 0.9 else random.choice(group2)

lowCount  = 0
highCount = 0
N         = 10000
for _ in range(N):
    v = getValue()
    if v == 23:        # <== same condition as the grouping rule.
        lowCount += 1
    else:
        highCount += 1
print(round(lowCount/N,2),round(highCount/N,2))

# 0.9 0.1

但是您必须相应地调整测试循环