Binomial distribution Returns the sum of the terms 0 through k of the Binomial probability density: k -- ( n ) j n-j > ( ) p (1-p) -- ( j ) j=0 The terms are not summed directly; instead the incomplete beta integral is employed, according to the formula y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ). The arguments must be positive, with p ranging from 0 to 1. ACCURACY:
| 60 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier |
| 61 | *************************************************************************/ |
| 62 | double binomialdistribution(int k, int n, double p) |
| 63 | { |
| 64 | double result; |
| 65 | double dk; |
| 66 | double dn; |
| 67 | |
| 68 | ap::ap_error::make_assertion(ap::fp_greater_eq(p,0)&&ap::fp_less_eq(p,1), "Domain error in BinomialDistribution"); |
| 69 | ap::ap_error::make_assertion(k>=-1&&k<=n, "Domain error in BinomialDistribution"); |
| 70 | if( k==-1 ) |
| 71 | { |
| 72 | result = 0; |
| 73 | return result; |
| 74 | } |
| 75 | if( k==n ) |
| 76 | { |
| 77 | result = 1; |
| 78 | return result; |
| 79 | } |
| 80 | dn = n-k; |
| 81 | if( k==0 ) |
| 82 | { |
| 83 | dk = pow(1.0-p, dn); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | dk = k+1; |
| 88 | dk = incompletebeta(dn, dk, 1.0-p); |
| 89 | } |
| 90 | result = dk; |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /************************************************************************* |
nothing calls this directly
no test coverage detected