Question

I am considering a society where there are an arbitrary number of people. Each person has just two choices. Either he or she stays with her current choice or she switches. In the code that I want to write, the probability that the person switches is inputted by the user.

To make clear what I am trying to do, suppose that the user tells the computer that there are 3 people in the society where the probabilities that each person chooses to switch is given by (p1,p2,p3). Consider person 1. He has probability of p1 of switching. Using him as a base for our calculation, the probability given person 1 as a base, that exactly no one in the society chooses to switch is given by

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

and the probability using person 1 as a base, that exactly one person in the society chooses to switch is given by

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

I can't figure out how to write this probability function in C++ without writing out every term in the sum. I considered using the binomial coefficient but I can't figure out a closed form expression for the sum since depending on user input, there are arbitrarily many probabilities that need to be considered.

I have attached what I have. The probability function is only a part of what I am trying to do but it is also the hardest part. I named the probability function probab and what I have in the for loop within the function is obviously wrong.

EDIT: Basically I want to calculate the probability of choosing a subset where each element in that subset has a different probability of being chosen.

I would appreciate any tips on how to go about this. Note that I am a beginner at C++ so any tips on improving my programming skills is also appreciated.

#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;
}
Was it helpful?

Solution

For future reference, for anybody attempting to do this, the probability function will look something like:

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

Something called a recursive function is used here. This is completely new to me and very illuminating. I credit this to Calle from Math stack exchange. I modified his version slightly to take vectors instead of arrays with some help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top