计算类似于二项和的条件概率
Calculating a conditional probability that is similar to a binomial sum
我正在考虑一个社会,其中有任意数量的人。每个人只有两个选择。他或她要么保持目前的选择,要么改变。在我要写的代码中,这个人切换的概率是由用户输入的。
为了更清楚地说明我在做什么,假设用户告诉计算机,社会上有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。我稍微修改了他的版本,在一些帮助下使用向量而不是数组。
相关文章:
- 绘制二维矩阵的行和列
- 类成员和依赖项注入的对象与引用
- 如何在bazel中同时处理外部依赖项和我的项目
- 可变模板和初始值设定项列表的类型缩小差异
- 在二叉搜索树中查找最小和的算法改进
- 二叉树的最大值和最小值
- 从注册表中删除项和子项:代码引发未经处理的异常
- 从序遍历和预序遍历构造二叉树的时间复杂性
- MSBuild、未解析的外部项和Visual Studio 2005
- 大括号或等号初始值设定项和初始化列表之间的C++求值顺序
- C++识别以像素为单位的二次和三次显示
- 二叉树上的递归搜索返回真和假
- 时间和空间复杂性在二叉树的每个级别创建元素向量(NON-BST)
- 访问列表项时的 UI 和工作线程同步
- 获取二叉树的大小和高度
- 二维矩阵的Push和pop操作,并在c++中显示
- 列举Windows注册表项中的所有子键和值
- 列表中具有重复项和位置的唯一对
- C++11非静态成员初始值设定项和已删除的复制构造函数
- 计算类似于二项和的条件概率