Hello,
I've been messing around with a binomial probability program. I've been learning A-level stats (S1), but the book I have doesn't have a statistical table. Since i've been messing around with C++ too, I thought this would be the perfect opportunity to test what I know. Unfortunately, I keep running into problems!
So far, this has demonstrated that I know the basics of C++, but I seem to fall down on the details.
This is what i've written:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
/*
Calculates the probabilities of getting exactly i things in a sample size of n, if there is p chance of getting one of the things in the first place.
(ie. if there are 3 lightbulbs, and there is a 0.25 chance of a bulb
being broken, then outputs:
P(x=0)
P(x=1)
P(x=2)
P(x=3)
)
Outputs single probabilities and cumulative.
*/
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;
int fact(int n);
int C(int n, int r);
int main(){
int n;
cout << "Please enter the sample size: ";
cin >> n;
double p;
cout << "Enter the probability of a certain behaviour occurring: ";
cin >> p;
double k = 0;
int i;
double j;
for(i = 0; i <= n; i++){
j = C(n,i)*pow(p,i)*pow(1-p,n-i);
cout << "P(x = " << i << ")=" << j;
cout << setw(30);
cout << "P(x <= " << i << ")=" << j+k;
cout << '\n';
k = j+k;
}
return 0;
}
int fact(int n){
int t, answer;
answer = 1;
for(t=1;t<=n;t++) answer = answer * t;
return(answer);
}
int C(int n, int r){
int a;
a = fact(n) / (fact(r)*fact(n - r) );
return a;
}
|
I've posted this program before here:
http://www.cplusplus.com/forum/beginner/171139/
but since this is a new problem, I thought i'd make a new topic.
I've changed the program to output the individual probabilities on the left, and the cumulative probabilities on the right.
Here's an example of what I get:
name@computer ~/Documents $ ./binomial
Please enter the sample size: 8
Enter the probability of a certain behaviour occurring: 0.5
P(x = 0)=0.00390625 P(x <= 0)=0.00390625
P(x = 1)=0.03125 P(x <= 1)=0.0351562
P(x = 2)=0.109375 P(x <= 2)=0.144531
P(x = 3)=0.21875 P(x <= 3)=0.363281
P(x = 4)=0.273438 P(x <= 4)=0.636719
P(x = 5)=0.21875 P(x <= 5)=0.855469
P(x = 6)=0.109375 P(x <= 6)=0.964844
P(x = 7)=0.03125 P(x <= 7)=0.996094
P(x = 8)=0.00390625 P(x <= 8)=1
|
This is what I should get. The individual probabilities on the left are all positive, and they add up to 1.
You can see this on a statistical binomial table found here:
http://i.stack.imgur.com/2Blui.png
(look at n=8 on the left hand column, and 0.5 on the top).
However, let's try a "big" number like 20:
name@computer ~/Documents $ ./binomial
Please enter the sample size: 20
Enter the probability of a certain behaviour occurring: 0.5
P(x = 0)=9.53674e-07 P(x <= 0)=9.53674e-07
P(x = 1)=-1.81198e-05 P(x <= 1)=-1.71661e-05
P(x = 2)=9.53674e-07 P(x <= 2)=-1.62125e-05
P(x = 3)=9.53674e-07 P(x <= 3)=-1.52588e-05
P(x = 4)=-1.90735e-06 P(x <= 4)=-1.71661e-05
P(x = 5)=0.00207329 P(x <= 5)=0.00205612
P(x = 6)=-9.53674e-07 P(x <= 6)=0.00205517
P(x = 7)=-1.90735e-06 P(x <= 7)=0.00205326
P(x = 8)=9.53674e-07 P(x <= 8)=0.00205421
P(x = 9)=9.53674e-07 P(x <= 9)=0.00205517
P(x = 10)=1.04904e-05 P(x <= 10)=0.00206566
P(x = 11)=9.53674e-07 P(x <= 11)=0.00206661
P(x = 12)=9.53674e-07 P(x <= 12)=0.00206757
P(x = 13)=-1.90735e-06 P(x <= 13)=0.00206566
P(x = 14)=-9.53674e-07 P(x <= 14)=0.0020647
P(x = 15)=0.00207329 P(x <= 15)=0.00413799
P(x = 16)=-1.90735e-06 P(x <= 16)=0.00413609
P(x = 17)=9.53674e-07 P(x <= 17)=0.00413704
P(x = 18)=9.53674e-07 P(x <= 18)=0.00413799
P(x = 19)=-1.81198e-05 P(x <= 19)=0.00411987
P(x = 20)=9.53674e-07 P(x <= 20)=0.00412083 |
This is going wrong! For some reason we're getting negative numbers on the left. This has a knock on effect when they're added meaning the individual probabilities don't add up to 1.
I have a feeling that the problem may have to do with small probabilities. By this I mean that the probabilities get so small, they no longer count as doubles.
Also, if you put an n value in that is bigger than 32, you get:
name@computer ~/Documents $ ./binomial
Please enter the sample size: 50
Enter the probability of a certain behaviour occurring: 0.1
Floating point exception |
I'm used to the compiler picking up errors, what's this on about?