gpt4 book ai didi

c++ - 计算类似于二项式和的条件概率

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:06 24 4
gpt4 key购买 nike

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

为了弄清楚我要做什么,假设用户告诉计算机社会中有 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 exchange 的 Calle。在一些帮助下,我稍微修改了他的版本以使用 vector 而不是数组。

关于c++ - 计算类似于二项式和的条件概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22727196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com