ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ccumdbetainv.cpp
Go to the documentation of this file.
1 
6 #include <admodel.h>
7 
8 static double lnbeta(double a,double b)
9 {
10  return gammln(a)+gammln(b)-gammln(a+b);
11 }
12 
13 /*
14 static int sgn(double z)
15 {
16  if (z>=0)
17  return 1;
18  else
19  return -1;
20 }
21 */
22 
23 double old_inv_cumd_beta_stable(double a,double b,double y,double eps)
24 {
25  double eps1=1.0-eps;
26 
27  int icount=0;
28  if (y<0.0 || y>1.0 || a<=0.0 || b<=0.0 )
29  {
30  cerr << "Illegal value in inv_cumd_beta" << endl;
31  cerr<<"y: "<<y<<endl;
32  cerr<<"a: "<<a<<endl;
33  cerr<<"b: "<<b<<endl;
34  ad_exit(1);
35  }
36 
37  double mu=a/(a+b);
38  double x=mu;
39  if (x<=eps)
40  x=1.1*eps;
41 
42  if(x>=eps1)
43  x=eps1-0.1*eps;
44 
45  double s=log(x/(1.0-x));
46  double lower=log(eps/eps1);
47  double upper=-lower; // bracket the minimum
48 
49  double bet=exp(lnbeta(a,b)); // normalizing constant
50  double denom=1.0/(betai(a,b,eps1)-betai(a,b,eps));
51  double finit=betai(a,b,x)-betai(a,b,eps)*denom;
52 
53  if (finit>y)
54  {
55  upper=s;
56  }
57  else if (finit<y)
58  {
59  lower=s;
60  }
61 
62  double d=0.0;
63  do
64  {
65  double f,dx; // der of x wrt s
66  x=1.0/(1.0+exp(-s)); //transform from s to x
67 
68  f=(betai(a,b,x)-betai(a,b,eps))*denom;
69 
70  dx=exp(-s)/square(1+exp(-s)); // der of x wrt s
71 
72  d=y-f;
73 
74  if (d<0) // x is too large
75  {
76  if (s<upper)
77  upper=s;
78  }
79  else if (d>0) // x is too small
80  {
81  if (s>lower)
82  {
83  lower=s;
84  }
85  }
86 
87  double xa1=pow(x,a-1);
88  double xb1;
89  xb1=pow(1-x,b-1);
90 
91  double fp=(xa1*xb1/bet)*dx*denom; // derivative of cumulative dist fun
92  // wrt x
93  double h=d/fp;
94 
95  double stry=s+h;
96  if (h<0.0)
97  {
98  if (stry<lower)
99  s=lower+0.5*(s-lower);
100  else
101  s=stry;
102  }
103  else
104  if (h>0.0)
105  {
106  if (stry>upper)
107  s=s+0.5*(upper-x);
108  else
109  s=stry;
110  }
111  icount++;
112  if (icount>15) break;
113  }
114  while(fabs(d)>1.e-12);
115 
116  return x;
117 }
118 
119 
120 double inv_cumd_beta_stable(double a,double b,double y,double eps)
121 {
122  //double eps1=1.0-eps;
123 
124  int icount=0;
125  if (y<0.0 || y>1.0 || a<=0.0 || b<=0.0 )
126  {
127  cerr << "Illegal value in inv_cumd_beta" << endl;
128  cerr<<"y: "<<y<<endl;
129  cerr<<"a: "<<a<<endl;
130  cerr<<"b: "<<b<<endl;
131  ad_exit(1);
132  }
133 
134  double mu=a/(a+b);
135  double x=mu;
136 
137  double lower=0.0;
138  double upper=1.0; // bracket the minimum
139 
140  double bet=exp(lnbeta(a,b)); // normalizing constant
141  double finit=betai(a,b,x);
142 
143  if (finit>y)
144  {
145  upper=x;
146  }
147  else if (finit<y)
148  {
149  lower=x;
150  }
151 
152  double d=0.0;
153  do
154  {
155  double f; // der of x wrt s
156  f=betai(a,b,x);
157  d=y-f;
158  if (d<0) // x is too large
159  {
160  if (x<upper)
161  upper=x;
162  }
163  else if (d>0) // x is too small
164  {
165  if (x>lower)
166  {
167  lower=x;
168  }
169  }
170  double xa1=pow(x,a-1);
171  double xb1=pow(1-x,b-1);
172 
173  double fp=(xa1*xb1/bet); // derivative of cumulative dist fun wrt x
174 
175  double h=d/fp;
176  double stry=x+h;
177  if (h<0.0)
178  {
179  if (stry<lower)
180  x=lower+0.5*(x-lower);
181  else
182  x=stry;
183  }
184  else
185  if (h>0.0)
186  {
187  if (stry>upper)
188  x=x+0.5*(upper-x);
189  else
190  x=stry;
191  }
192  icount++;
193  if (icount>50) break;
194  }
195  while(fabs(d)>1.e-16);
196  return x;
197 }
198 
199 
200 double qbeta(double x, double a, double b, double eps){
201  return inv_cumd_beta_stable(a,b,x,eps);
202 }
203 
204 
205 
206 /*
207 main()
208 {
209  double eps=0.000001;
210  do
211  {
212  double a,b,y;
213  cin >> a;
214  cin >> b;
215  cin >> y;
216  double x1=inv_cumd_beta(a,b,y);
217  double x2=inv_cumd_beta_stable(a,b,y,eps);
218  cout << x1 << " " << x2 << endl;
219  }
220  while(1);
221 }
222 */
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define x
double qbeta(double x, double a, double b, double eps)
double old_inv_cumd_beta_stable(double a, double b, double y, double eps)
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
double inv_cumd_beta_stable(double a, double b, double y, double eps)
prnstream & endl(prnstream &)
Description not yet available.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
double betai(const double a, const double b, const double x, int maxit)
Incomplete beta function for constant objects.
Definition: cbetai.cpp:21
static double lnbeta(double a, double b)
Definition: ccumdbetainv.cpp:8
double eps
Definition: ftweak.cpp:13
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17