ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
betacf_val.hpp
Go to the documentation of this file.
1 /* Copy-pasted from Numerical Recipies, and modified:
2 
3  * Search-replace 'float' -> 'Float'
4  * Add template<Float>
5  * Replace 'nrerror' with 'error' (to use R's error function)
6 
7 */
8 #include <math.h>
9 //#define EPS 3.0e-7
10 #define EPS 1.0e-9
11 #define FPMIN 1.0e-30
12 template<class Float>
13 Float betacf(Float a, Float b, Float x, int MAXIT)
14 //Used by betai : Evaluates continued fraction for incomplete beta
15 //function by modified Lentz’s method (§5.2).
16 {
17  int m,m2;
18  Float aa,c,d,del,h,qab,qam,qap;
19  qab=a+b; //These q’s will be used in factors that occur
20  qap=a+1.0; //in the coefficients (6.4.6).
21  qam=a-1.0;
22  c=1.0;
23  // First step of Lentz’s method.
24  d=1.0-qab*x/qap;
25  if (fabs(d) < FPMIN) d=FPMIN;
26  d=1.0/d;
27  h=d;
28  for (m=1;m<=MAXIT;m++) {
29  m2=2*m;
30  aa=m*(b-m)*x/((qam+m2)*(a+m2));
31  d=1.0+aa*d;
32  //One step (the even one) of the recurrence.
33  if (fabs(d) < FPMIN) d=FPMIN;
34  c=1.0+aa/c;
35  if (fabs(c) < FPMIN) c=FPMIN;
36  d=1.0/d;
37  h *= d*c;
38  aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
39  d=1.0+aa*d;
40  //Next step of the recurrence (the odd one).
41  if (fabs(d) < FPMIN) d=FPMIN;
42  c=1.0+aa/c;
43  if (fabs(c) < FPMIN) c=FPMIN;
44  d=1.0/d;
45  del=d*c;
46  h *= del;
47  if (fabs(del-1.0) < EPS) break;
48  //Are we done?
49  }
50  if (m > MAXIT){ cerr<<"a or b too big, or MAXIT too small in betacf"<<endl;}
51  return h;
52 }
#define EPS
Definition: betacf_val.hpp:10
#define x
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
prnstream & endl(prnstream &)
#define MAXIT
#define FPMIN
Definition: betacf_val.hpp:11
Float betacf(Float a, Float b, Float x, int MAXIT)
Definition: betacf_val.hpp:13