[431c9e0] | 1 | /* nbdtr.c |
---|
| 2 | * |
---|
| 3 | * Negative binomial distribution |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * |
---|
| 7 | * SYNOPSIS: |
---|
| 8 | * |
---|
| 9 | * int k, n; |
---|
| 10 | * double p, y, nbdtr(); |
---|
| 11 | * |
---|
| 12 | * y = nbdtr( k, n, p ); |
---|
| 13 | * |
---|
| 14 | * DESCRIPTION: |
---|
| 15 | * |
---|
| 16 | * Returns the sum of the terms 0 through k of the negative |
---|
| 17 | * binomial distribution: |
---|
| 18 | * |
---|
| 19 | * k |
---|
| 20 | * -- ( n+j-1 ) n j |
---|
| 21 | * > ( ) p (1-p) |
---|
| 22 | * -- ( j ) |
---|
| 23 | * j=0 |
---|
| 24 | * |
---|
| 25 | * In a sequence of Bernoulli trials, this is the probability |
---|
| 26 | * that k or fewer failures precede the nth success. |
---|
| 27 | * |
---|
| 28 | * The terms are not computed individually; instead the incomplete |
---|
| 29 | * beta integral is employed, according to the formula |
---|
| 30 | * |
---|
| 31 | * y = nbdtr( k, n, p ) = incbet( n, k+1, p ). |
---|
| 32 | * |
---|
| 33 | * The arguments must be positive, with p ranging from 0 to 1. |
---|
| 34 | * |
---|
| 35 | * ACCURACY: |
---|
| 36 | * |
---|
| 37 | * Tested at random points (a,b,p), with p between 0 and 1. |
---|
| 38 | * |
---|
| 39 | * a,b Relative error: |
---|
| 40 | * arithmetic domain # trials peak rms |
---|
| 41 | * IEEE 0,100 100000 1.7e-13 8.8e-15 |
---|
| 42 | * See also incbet.c. |
---|
| 43 | * |
---|
| 44 | */ |
---|
| 45 | /* nbdtr.c |
---|
| 46 | * |
---|
| 47 | * Complemented negative binomial distribution |
---|
| 48 | * |
---|
| 49 | * |
---|
| 50 | * |
---|
| 51 | * SYNOPSIS: |
---|
| 52 | * |
---|
| 53 | * int k, n; |
---|
| 54 | * double p, y, nbdtrc(); |
---|
| 55 | * |
---|
| 56 | * y = nbdtrc( k, n, p ); |
---|
| 57 | * |
---|
| 58 | * DESCRIPTION: |
---|
| 59 | * |
---|
| 60 | * Returns the sum of the terms k+1 to infinity of the negative |
---|
| 61 | * binomial distribution: |
---|
| 62 | * |
---|
| 63 | * inf |
---|
| 64 | * -- ( n+j-1 ) n j |
---|
| 65 | * > ( ) p (1-p) |
---|
| 66 | * -- ( j ) |
---|
| 67 | * j=k+1 |
---|
| 68 | * |
---|
| 69 | * The terms are not computed individually; instead the incomplete |
---|
| 70 | * beta integral is employed, according to the formula |
---|
| 71 | * |
---|
| 72 | * y = nbdtrc( k, n, p ) = incbet( k+1, n, 1-p ). |
---|
| 73 | * |
---|
| 74 | * The arguments must be positive, with p ranging from 0 to 1. |
---|
| 75 | * |
---|
| 76 | * ACCURACY: |
---|
| 77 | * |
---|
| 78 | * Tested at random points (a,b,p), with p between 0 and 1. |
---|
| 79 | * |
---|
| 80 | * a,b Relative error: |
---|
| 81 | * arithmetic domain # trials peak rms |
---|
| 82 | * IEEE 0,100 100000 1.7e-13 8.8e-15 |
---|
| 83 | * See also incbet.c. |
---|
| 84 | */ |
---|
| 85 | /* nbdtr.c |
---|
| 86 | * |
---|
| 87 | * Functional inverse of negative binomial distribution |
---|
| 88 | * |
---|
| 89 | * |
---|
| 90 | * |
---|
| 91 | * SYNOPSIS: |
---|
| 92 | * |
---|
| 93 | * int k, n; |
---|
| 94 | * double p, y, nbdtri(); |
---|
| 95 | * |
---|
| 96 | * p = nbdtri( k, n, y ); |
---|
| 97 | * |
---|
| 98 | * DESCRIPTION: |
---|
| 99 | * |
---|
| 100 | * Finds the argument p such that nbdtr(k,n,p) is equal to y. |
---|
| 101 | * |
---|
| 102 | * ACCURACY: |
---|
| 103 | * |
---|
| 104 | * Tested at random points (a,b,y), with y between 0 and 1. |
---|
| 105 | * |
---|
| 106 | * a,b Relative error: |
---|
| 107 | * arithmetic domain # trials peak rms |
---|
| 108 | * IEEE 0,100 100000 1.5e-14 8.5e-16 |
---|
| 109 | * See also incbi.c. |
---|
| 110 | */ |
---|
| 111 | |
---|
| 112 | /* |
---|
| 113 | Cephes Math Library Release 2.8: June, 2000 |
---|
| 114 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier |
---|
| 115 | */ |
---|
| 116 | |
---|
| 117 | #include "mconf.h" |
---|
| 118 | #ifdef ANSIPROT |
---|
| 119 | extern double incbet ( double, double, double ); |
---|
| 120 | extern double incbi ( double, double, double ); |
---|
| 121 | #else |
---|
| 122 | double incbet(), incbi(); |
---|
| 123 | #endif |
---|
| 124 | |
---|
| 125 | double nbdtrc( k, n, p ) |
---|
| 126 | int k, n; |
---|
| 127 | double p; |
---|
| 128 | { |
---|
| 129 | double dk, dn; |
---|
| 130 | |
---|
| 131 | if( (p < 0.0) || (p > 1.0) ) |
---|
| 132 | goto domerr; |
---|
| 133 | if( k < 0 ) |
---|
| 134 | { |
---|
| 135 | domerr: |
---|
| 136 | mtherr( "nbdtr", DOMAIN ); |
---|
| 137 | return( 0.0 ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | dk = k+1; |
---|
| 141 | dn = n; |
---|
| 142 | return( incbet( dk, dn, 1.0 - p ) ); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | double nbdtr( k, n, p ) |
---|
| 148 | int k, n; |
---|
| 149 | double p; |
---|
| 150 | { |
---|
| 151 | double dk, dn; |
---|
| 152 | |
---|
| 153 | if( (p < 0.0) || (p > 1.0) ) |
---|
| 154 | goto domerr; |
---|
| 155 | if( k < 0 ) |
---|
| 156 | { |
---|
| 157 | domerr: |
---|
| 158 | mtherr( "nbdtr", DOMAIN ); |
---|
| 159 | return( 0.0 ); |
---|
| 160 | } |
---|
| 161 | dk = k+1; |
---|
| 162 | dn = n; |
---|
| 163 | return( incbet( dn, dk, p ) ); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | double nbdtri( k, n, p ) |
---|
| 169 | int k, n; |
---|
| 170 | double p; |
---|
| 171 | { |
---|
| 172 | double dk, dn, w; |
---|
| 173 | |
---|
| 174 | if( (p < 0.0) || (p > 1.0) ) |
---|
| 175 | goto domerr; |
---|
| 176 | if( k < 0 ) |
---|
| 177 | { |
---|
| 178 | domerr: |
---|
| 179 | mtherr( "nbdtri", DOMAIN ); |
---|
| 180 | return( 0.0 ); |
---|
| 181 | } |
---|
| 182 | dk = k+1; |
---|
| 183 | dn = n; |
---|
| 184 | w = incbi( dn, dk, p ); |
---|
| 185 | return( w ); |
---|
| 186 | } |
---|