简体   繁体   English

概率实现

[英]Probability Realization

If I have the probabilities of x, y, and z events occurring, how can I programatically realize the choosing between these three events?如果我有 x、y 和 z 事件发生的概率,我如何以编程方式实现这三个事件之间的选择?

Example: Pr(x) = 0.3 Pr(y) = 0.2 Pr(z) = 0.5示例:Pr(x) = 0.3 Pr(y) = 0.2 Pr(z) = 0.5

I'd like a function to give me a realization of these probabilities to say I should choose y, or maybe z, etc.我想要一个 function 给我一个实现这些概率说我应该选择 y 或 z 等。

Any pointers or references are appreciated, thanks.任何指针或参考表示赞赏,谢谢。

If you have a function rand() that returns a uniform random number in [0,1] , then如果您有一个 function rand()[0,1]中返回一个统一的随机数,那么

float r = rand();
if (rand < Pr(x)) {
    return x;
} else if (rand < Pr(x)+Pr(y)) {
    return y;
} else {
    return z;
}

Similarly, if you can generate a random integer between, say, 1 and 100, then同样,如果您可以生成介于 1 和 100 之间的随机 integer,那么

int r = rand();
if (rand < Pr(x)*100) {
    return x;
} else if (rand < (Pr(x)+Pr(y))*100) {
    return y;
} else {
    return z;
}

If you have probs of events, take function returning normal distrib in [0, 1) and check in which range it falls.如果您有事件的概率,请使用 function 返回 [0, 1) 中的正常分布并检查它落在哪个范围内。 If it in [0, 0.3) return A, if it in [0.3, 0.5) return B and otherwise return C.如果在 [0, 0.3) 中返回 A,如果在 [0.3, 0.5) 中返回 B,否则返回 C。

In python you could write a program which defines a method 'chooser' which takes two parameters px and py those being the probabilties of the first two events occuring independently.在 python 中,您可以编写一个程序,该程序定义一个“选择器”方法,该方法采用两个参数 px 和 py,它们是前两个事件独立发生的概率。 The method would return x,y or z as appropriate, python code is below:该方法将根据需要返回 x、y 或 z,python 代码如下:

import random 

def chooser(px,py):
    prob = random.randint(1, 10)
    if (prob<=(px*10)):
        return x
    else if (prob<=((px+py)*10)):
        return y
    else:
        return z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM