/************************************************************************** * Compute the regularized incomplete beta function in log space. * This is a modification of the above betai. **************************************************************************/ double log_betai(double a, double b, double x) { double log_bt; if (x < 0.0 || x > 1.0) { fprintf(stderr, "Bad x in routine betai"); exit(EXIT_FAILURE); } if (x == 0.0 || x == 1.0) { log_bt = 1.0; } else { log_bt = lgamma(a + b) - lgamma(a) - lgamma(b)+ a*log(x)+ b*log(1.0 - x); } // to ensure the quick convergance of the // continued fraction use the symmetry relation: // I_x(a, b) = 1 - I_(1-x)(b, a) if (x < ((a + 1.0) / (a + b + 2.0))) { return log_bt + log(betacf(a, b, x)/a); } else { // we believe that (exp(log_bt) * betacf(b, a, 1.0 - x) / b) // will not be very close to 1 or 0 in this case // as the original author would not have subtracted it from 1 // because that would have resulted in restricting the number // to the limits of precision of floating point or 1e-16 and // we don't think he's stupid. return log(1.0 - exp(log_bt) * betacf(b, a, 1.0 - x) / b); } }
/* ribeta: regularized incomplete beta function. * @x: value of the beta distribution to calculate a p-value for. * @a: first parameter of the incomplete beta function. * @b: second parameter of the incomplete beta function. */ double ribeta (double x, double a, double b) { /* declare required variables. */ double bt; /* perform bounds checking on input values. */ if (a <= 0.0 || b <= 0.0 || x < 0.0 || x > 1.0) return NAN; /* these are simple cases. */ if (x == 0.0 || x == 1.0) return x; bt = exp (lgamma (a + b) - lgamma (a) - lgamma (b) + a * log (x) + b * log (1.0 - x)); if (x < (a + 1.0) / (a + b + 2.0)) { return bt * betacf (x, a, b) / a; } else { return 1.0 - bt * betacf (1.0 - x, b, a) / b; } /* this should never occur. */ return NAN; }
double betai(double a, double b, double x){ double bt; if (x<0.0 || x>1.0) { cout << "x not between 0 and 1"; exit(1); } if (x==0.0 || x==1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x<(a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
double ibeta(double a, double b, double x) { double bt; if (x < 0.0 || x > 1.0 || a <= 0.0 || b <= 0.0) return 0; if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(lngamma(a+b)-lngamma(a)-lngamma(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
double betai(double a, double b, double x) { double bt; if (x < 0.0 || x > 1.0) nrerror("Bad x in routine betai"); if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
/** Incomplete beta function for df1b2variable objects. \param a \f$a\f$ \param b \f$b\f$ \param x \f$x\f$ \param maxit Maximum number of iterations for the continued fraction approximation in betacf. \return Incomplete beta function \f$I_x(a,b)\f$ \n\n The implementation of this algorithm was inspired by "Numerical Recipes in C", 2nd edition, Press, Teukolsky, Vetterling, Flannery, chapter 2 */ df1b2variable betai(const df1b2variable & a,const df1b2variable & b,double x, int maxit) { df1b2variable bt; if (x < 0.0 || x > 1.0) cerr << "Bad x in routine betai" << endl; if (x == 0.0 || x == 1.0) bt=double(0.0); else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (value(a)+1.0)/(value(a)+value(b)+2.0)) return bt*betacf(a,b,x,maxit)/a; else return 1.0-bt*betacf(b,a,1.0-x,maxit)/b; }
/** Incomplete beta function for constant objects. \param a \f$a\f$ \param b \f$b\f$ \param x \f$x\f$ \param maxit Maximum number of iterations for the continued fraction approximation in betacf. \return Incomplete beta function \f$I_x(a,b)\f$ ` \n\n The implementation of this algorithm was inspired by "Numerical Recipes in C", 2nd edition, Press, Teukolsky, Vetterling, Flannery, chapter 2 */ double betai(const double a,const double b,const double x,int maxit) { double bt; if (x < 0.0 || x > 1.0) cerr << "Bad x in routine betai" << endl; if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x,maxit)/a; else return 1.0-bt*betacf(b,a,1.0-x,maxit)/b; }
float betai(float a, float b, float x) { float bt; if (x < 0.0 || x > 1.0) fprintf(stdout, "Bad x in routine betai"); if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
double betai (double x, double a, double b) { double bt; if (x < 0.0 || x > 1.0) { output( "x not in [0,1] in betai\n" ); error=1; return 0; } if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
double betai(double a, double b, double x) { double betacf(double a, double b, double x); double bt; if (x < 0.0 || x > 1.0) fatalx( "Bad x in routine betai\n"); if (x==0.0) return 0.0 ; if (x==1.0) return 1.0 ; /* Factors in front of the continued fraction. */ bt=exp(lgamma(a+b)-lgamma(a)-lgamma(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) /* Use continued fraction directly. */ return bt*betacf(a,b,x)/a; else /* Use continued faction after making */ return 1.0-bt*betacf(b,a,1.0-x)/b; /* the symmetry transformation. */ }
double betai(double a, double b, double x) //Returns the incomplete beta function Ix(a, b). { double betacf(double a, double b, double x); double gammln(double xx); double bt; if (x == 0.0 || x == 1.0) bt=0.0; else //Factors in front of the continued fraction. bt=exp(lgamma(a+b)-lgamma(a)-lgamma(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) //Use continued fraction directly. return bt*betacf(a,b,x)/a; else //Use continued fraction after making the symmetry transformation return (1.0-bt*betacf(b,a,1.0-x)/b); }
double betai(double a, double b, double x) { double bt; if ( x < 0.0 || x > 1.0) error("betai: Bad x"); if ( x == 0.0 || x == 1.0) bt = 0.0; else bt = exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a + 1.0)/(a + b + 2.0)) // use continued fraction directly return bt*betacf(a,b,x)/a; else // use continued fraction after making the symmetry transformation return 1.0-bt*betacf(b,a,1.0-x)/b; }
float betai(float a, float b, float x) { float bt; if (isnan(x)) return NAN; if (x < 0.0 || x > 1.0) { nrerror("Bad x in routine BETAI"); return NAN; } if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
double logbetai(double a, double b, double x){ double bt; if(x <= 0.0 || x >= 1.0){ printf("Invalid x in function logbetai\n"); return(0.0); } bt = gammaln(a + b) - gammaln(a) - gammaln(b) + a * log(x) + b * log(1.0 - x); if(x < (a+1.0)/(a+b+2.0)){ return bt + log(betacf(a,b,x)) - log(a); } else{ return log(1.0 - exp(bt) * betacf(b,a,1.0 - x) / b); } }
float betai(float a, float b, float x) { float betacf(float a, float b, float x); float gammln(float xx); void nrerror(char error_text[]); float bt; if (x < 0.0 || x > 1.0) nrerror("Bad x in routine betai"); if (x == 0.0 || x == 1.0) bt=0.0; else bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) return bt*betacf(a,b,x)/a; else return 1.0-bt*betacf(b,a,1.0-x)/b; }
/* Incomplete beta function * ------------------------ * Numerical Recipes pg 227 */ REAL_TYPE betai(REAL_TYPE a, REAL_TYPE b, REAL_TYPE x) throw(std::invalid_argument) { REAL_TYPE bt; if(x<0.00||x>1.00) throw(std::invalid_argument(std::string("betai():x must be between 0.0 and 1.0"))); if(x==0.0 || x==1.0) bt=0.0; else bt=std::exp(gammln(a+b)-gammln(a)-gammln(b)+a*std::log(x)+b*std::log(1.0-x)); if(x< (a+1.0)/(a+b+2.0)) { return (bt*betacf(a,b,x)/a); } else { return(1.0-bt*betacf(b,a,1.0-x)/b); } }
/************************************************************************** * Compute the regularized incomplete beta function. * This makes use of the continued fraction representation. * * See "Numerical Recipes in C" section 6.4 page 226. **************************************************************************/ double betai(double a, double b, double x) { double bt; if (x < 0.0 || x > 1.0) { fprintf(stderr, "Bad x in routine betai"); exit(EXIT_FAILURE); } if (x == 0.0 || x == 1.0) { bt = 0.0; } else { bt = exp(lgamma(a + b) - lgamma(a) - lgamma(b)+ a*log(x)+ b*log(1.0 - x)); } // to ensure the quick convergance of the // continued fraction use the symmetry relation: // I_x(a, b) = 1 - I_(1-x)(b, a) if (x < ((a + 1.0) / (a + b + 2.0))) { return bt * betacf(a, b, x) / a; } else { return 1.0 - bt * betacf(b, a, 1.0 - x) / b; } }
// Returns the incomplete beta function Ix(a; b) float betai(float a, float b, float x) { float bt; if (x < 0.0 || x > 1.0 || a==0.0 || b==0.0) { // cout << "Bad x in routine betai" << '\n'; return -999; } if (x == 0.0 || x == 1.0) bt=0.0; else // Factors in front of the continued fraction. bt = exp(gammaln(a+b)-gammaln(a)-gammaln(b)+a*log(x)+b*log(1.0-x)); if (x < (a+1.0)/(a+b+2.0)) // Use continued fraction directly. return bt*betacf(a,b,x)/a; else // Use continued fraction after making the symmetry // transformation. return 1.0-bt*betacf(b,a,1.0-x)/b; }
/* Returns the incomplete beta function Ix (a, b). */ double betai(double a, double b, double x) { double gammln(double xx); double bt; if (x < 0.0 || x > 1.0){ fprintf( StdErr, "Bad x==%s in routine betai\n", d2str(x, NULL, NULL) ); } if( x== 0.0 || x== 1.0 ){ bt=0.0; } else{ /* Factors in front of the continued fraction. */ bt= exp( gammln(a+b)- gammln(a)- gammln(b)+ a* log(x)+ b* log(1.0-x) ); } if( x< (a+1.0)/(a+b+2.0) ){ /* Use continued fraction directly. */ return( bt* betacf(a,b,x)/ a ); } else{ /* Use continued fraction after making the symmetry transformation. */ return( 1.0- bt* betacf( b,a,1.0-x )/ b ); } }