我正在考虑一个有任意人数的社会。每个人只有两个选择。他或她保留当前的选择,或者她切换。在我要编写的代码中,用户输入了切换人员的可能性。
为了弄清楚我要做什么,假设用户告诉计算机社会中有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;
}
最佳答案
供将来参考,对于任何尝试这样做的人,概率函数将类似于:
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交流的Calle。我稍微修改了他的版本,在一些帮助下采用了 vector 而不是数组。
关于c++ - 计算类似于二项式和的条件概率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22727196/