计算类似于二项和的条件概率

Calculating a conditional probability that is similar to a binomial sum

本文关键字:二项 和的 条件 概率 类似于 计算      更新时间:2023-10-16

我正在考虑一个社会,其中有任意数量的人。每个人只有两个选择。他或她要么保持目前的选择,要么改变。在我要写的代码中,这个人切换的概率是由用户输入的。

为了更清楚地说明我在做什么,假设用户告诉计算机,社会上有3个人,每个人选择交换的概率为(p1,p2,p3)。考虑第一个人。他切换的概率是p1。以他为基础进行计算,以人1为基础,社会中没有人选择转换的概率为

P_ {1} (0) = (1-p2) * (1-p3)

,以人1为基数,社会中只有一个人选择转换的概率为

P_ {1} (1) = p2 * (1-p3) + (1-p2) * p3。

我不知道如何在c++中写出这个概率函数,而不写出总和中的每一项。我考虑过使用二项式系数,但我无法计算出总和的封闭形式表达式,因为根据用户输入,需要考虑任意多的概率。

我已经附上了我所拥有的。概率函数只是我要做的一部分但它也是最难的部分。我将概率函数命名为probab函数中for循环中的内容显然是错误的。

EDIT:基本上我想计算选择一个子集的概率,其中该子集中的每个元素都有不同的被选择概率。

如果有任何关于如何做这件事的建议,我将不胜感激。请注意,我是c++的初学者,所以任何提高我的编程技能的建议也很感激。
#include <iostream>
#include <vector>
using namespace std;
unsigned int factorial(unsigned int n);                              
unsigned int binomial(unsigned int bin, unsigned int cho);            
double probab(int numOfPeople, vector<double> probs, int p, int num);
int main() {
    char correctness;
    int numOfPeople = 0;
    cout << "Enter the # of people: ";
    cin >> numOfPeople;
    vector<double> probs(numOfPeople); // Create a vector of size numOfPeople;
    for (int i = 1; i < numOfPeople+1; i++) {
        cout << "Enter the probability of person "<< i << " will accept change: ";
        cin >> probs[i-1];
    }
    cout << "You have entered the following probabilities of accepting change: (";
    for (int i = 1; i < numOfPeople+1; i++) {
        cout << probs[i-1];
        if (i == numOfPeople) {
            cout << ")";
        }
        else {
            cout << ",";
        }
    }
    cout << endl;
    cout << "Is this correct? (Enter y for yes, n for no): ";
    cin >> correctness;
    if (correctness == 'n') {
        return 0;
    }
    return 0;
}
unsigned int factorial(unsigned int n){                                     // Factorial function
    unsigned int ret = 1;
    for(unsigned int i = 1; i <= n; ++i) {
        ret *= i;
    }
    return ret;
}
unsigned int binomial(unsigned int totl, unsigned int choose) {             // Binomial function
    unsigned int bin = 0;
    bin = factorial(totl)/(factorial(choose)*factorial(totl-choose));
    return bin;
}
double probab(int numOfPeople, vector<double> probs, int p, int num) {      // Probability function
    double prob = 0;
    for (int i = 1; i < numOfPeople; i++) {
        prob += binomial(numOfPeople, i-1)/probs[p]*probs[i-1];
    }
    return prob;
}

为了将来的参考,对于任何试图这样做的人,概率函数看起来像这样:

double probability (vector<double> &yesprobabilities, unsigned int numOfPeople, unsigned int yesNumber, unsigned int startIndex) {
    double kprobability = 0;
    // Not enough people!
    if (numOfPeople-1 < yesNumber) {
        kprobability = 0;
    }
    // n == k, the only way k people will say yes is if all the remaining people say yes.
    else if (numOfPeople-1 == yesNumber) {
        kprobability = 1;
        for (int i = startIndex; i < numOfPeople-1; ++i) {
            kprobability = kprobability * yesprobabilities[i];
        }
    }
    else if (yesprobabilities[startIndex] == 1) {
        kprobability += probability(yesprobabilities,numOfPeople-1,yesNumber-1,startIndex+1);
    }
    else {
        // The first person says yes, k - 1 of the other persons have to say yes.
        kprobability += yesprobabilities[startIndex] * probability(yesprobabilities,numOfPeople-1,yesNumber-1,startIndex+1);
        // The first person says no, k of the other persons have to say yes.
        kprobability += (1 - yesprobabilities[startIndex]) * probability(yesprobabilities,numOfPeople-1,yesNumber,startIndex+1);
    }
    return probability;
}

这里使用了递归函数。这对我来说是全新的,非常有启发性。我把这归功于数学堆栈交换的Calle。我稍微修改了他的版本,在一些帮助下使用向量而不是数组。