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

时间:2014-03-29 04:56:01

标签: c++ probability discrete-mathematics

我正在考虑一个有任意数量人的社会。每个人只有两个选择。要么他或她留在她当前的选择,要么她切换。在我想写的代码中,用户输入人切换的概率。

为了弄清楚我想要做什么,假设用户告诉计算机社会中有3个人,每个人选择切换的概率由(p1,p2,p3)给出。考虑人1.他有切换p1的概率。使用他作为我们计算的基础,给予人1作为基础的概率,即社会中没有人选择切换的概率由

给出

P_ {1}(0)=(1-P2)*(1-P3)

以人1作为基础的概率,正是社会中一个人选择转换的概率由

给出

P_ {1}(1)= P2 *(1-P3)+(1-P2)* P3。

我无法弄清楚如何在C ++中编写这个概率函数而不会在总和中写出每个术语。我考虑使用二项式系数,但我无法找出总和的闭合表达式,因为根据用户输入,需要考虑任意多的概率。

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

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

我很感激有关如何解决这个问题的任何提示。请注意,我是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;
}

1 个答案:

答案 0 :(得分:1)

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

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;
}

此处使用称为递归函数的东西。这对我来说是全新的,非常有启发性。我把这归功于来自Math stack exchange的Calle。我稍微修改了他的版本,以便在一些帮助下使用向量而不是数组。