/************************************************************************* Functional inverse of Student's t distribution Given probability p, finds the argument t such that stdtr(k,t) is equal to p. ACCURACY: Tested at random 1 <= k <= 100. The "domain" refers to p: Relative error: arithmetic domain # trials peak rms IEEE .001,.999 25000 5.7e-15 8.0e-16 IEEE 10^-6,.001 25000 2.0e-12 2.9e-14 Cephes Math Library Release 2.8: June, 2000 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier *************************************************************************/ double invstudenttdistribution(int k, double p) { double result; double t; double rk; double z; int rflg; ap::ap_error::make_assertion(k>0&&p>0&&p<1); rk = k; if( p>0.25&&p<0.75 ) { if( p==0.5 ) { result = 0; return result; } z = 1.0-2.0*p; z = invincompletebeta(0.5, 0.5*rk, fabs(z)); t = sqrt(rk*z/(1.0-z)); if( p<0.5 ) { t = -t; } result = t; return result; } rflg = -1; if( p>=0.5 ) { p = 1.0-p; rflg = 1; } z = invincompletebeta(0.5*rk, 0.5, 2.0*p); if( ap::maxrealnumber*z<rk ) { result = rflg*ap::maxrealnumber; return result; } t = sqrt(rk/z-rk); result = rflg*t; return result; }
/************************************************************************* Inverse binomial distribution Finds the event probability p such that the sum of the terms 0 through k of the Binomial probability density is equal to the given cumulative probability y. This is accomplished using the inverse beta integral function and the relation 1 - p = incbi( n-k, k+1, y ). ACCURACY: Tested at random points (a,b,p). a,b Relative error: arithmetic domain # trials peak rms For p between 0.001 and 1: IEEE 0,100 100000 2.3e-14 6.4e-16 IEEE 0,10000 100000 6.6e-12 1.2e-13 For p between 10^-6 and 0.001: IEEE 0,100 100000 2.0e-12 1.3e-14 IEEE 0,10000 100000 1.5e-12 3.2e-14 Cephes Math Library Release 2.8: June, 2000 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier *************************************************************************/ double invbinomialdistribution(int k, int n, double y) { double result; double dk; double dn; double p; ap::ap_error::make_assertion(p>=0&&p<=1, "Domain error in InvBinomialDistribution"); ap::ap_error::make_assertion(k>=0&&k<n, "Domain error in InvBinomialDistribution"); dn = n-k; if( k==0 ) { if( y>0.8 ) { p = -expm1(log1p(y-1.0)/dn); } else { p = 1.0-pow(y, 1.0/dn); } } else { dk = k+1; p = incompletebeta(dn, dk, 0.5); if( p>0.5 ) { p = invincompletebeta(dk, dn, 1.0-y); } else { p = 1.0-invincompletebeta(dn, dk, y); } } result = p; return result; }