ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vcumdbetainv.cpp
Go to the documentation of this file.
1 
7 #include <admodel.h>
8 #include "df12fun.h"
9 
10 //#define ADUNCONST(type,obj) type & obj = (type&) _##obj;
11 
12 static double lnbeta(double a,double b)
13 {
14  return gammln(a)+gammln(b)-gammln(a+b);
15 }
16 
18  const df1_two_variable& b,double x,int maxit=100);
19 
21  const prevariable& _y,double eps)
22 {
26 
27  double eps1=1.0-eps;
28  // this gets the inverse without derivatives
29  double ca=value(a);
30  double cb=value(b);
31  double cx=inv_cumd_beta_stable(ca,cb,value(y),eps);
32 
33  init_df1_two_variable va(_a);
34  init_df1_two_variable vb(_b);
35 
36  // this gets the derivatives for the function itself
37 
38  df1_two_variable z=(betai(va,vb,cx)-betai(va,vb,eps))/
39  (betai(va,vb,eps1)-betai(va,vb,eps));
40 
41  double dga=*z.get_u_x();
42  double dgb=*z.get_u_y();
43 
44  double denom=1.0/(betai(ca,cb,eps1)-betai(ca,cb,eps));
45  double dgx=pow(cx,value(a)-1.0)*pow(1.0-cx,value(b)-1.0)/
46  exp(lnbeta(value(a),value(b)))*denom;
47 
48  // now solve for the derivatves of the inverse function
49 
50  double dfx=1.0/dgx;
51  double dfa=-dfx*dga;
52  double dfb=-dfx*dgb;
53 
54  dvariable tmp;
55  value(tmp)=cx;
56 
58  &(value(tmp)) ,&(value(_a)),dfa ,&(value(_b)),dfb ,&(value(_y)),dfx);
59 
60  return tmp;
61 }
62 
64  double x);
65 
66 
68  const df1_two_variable& b,double x,int maxit)
69 {
71 
72  if (x < 0.0 || x > 1.0) cerr << "Bad x in routine betai" << endl;
73  double z=1.0-x;
74  if (x == 0.0 || x==1.0)
75  bt=0.0;
76  else
77  {
78  bt=exp(gammln(a+b) -gammln(a) -gammln(b) +a*log(x) +b*log(z));
79  }
80  if (x < (value(a)+1.0)/(value(a)+value(b)+2.0))
81  return bt*betacf(a,b,x)/a;
82  else
83  return 1.0-bt*betacf(b,a,1.0-x)/b;
84 }
85 
86 /*
87 main()
88 {
89  double a,b,x;
90 
91  a=2.0;
92  b=3.0;
93  x=.80;
94  do
95  {
96  cin >> a;
97  cin >> b;
98  cin >> x;
99  if (x<0 ) ad_exit(1);
100  independent_variables xx(1,3);
101  dvector g(1,3);
102  dvector gg(1,3);
103  xx(1)=a;
104  xx(2)=b;
105  xx(3)=x;
106  gradient_structure gs(10000);
107  {
108  dvar_vector vx=dvar_vector(xx);
109  dvariable y=inv_cumd_beta(vx(1),vx(2),vx(3));
110  cout << y << endl;
111  gradcalc(3,g);
112  double h=1.e-6;
113  gg(1)=(inv_cumd_beta(a+h,b,x)-inv_cumd_beta(a-h,b,x))/(2.0*h);
114  gg(2)=(inv_cumd_beta(a,b+h,x)-inv_cumd_beta(a,b-h,x))/(2.0*h);
115  gg(3)=(inv_cumd_beta(a,b,x+h)-inv_cumd_beta(a,b,x-h))/(2.0*h);
116  cout << g << endl << gg << " " << endl << g-gg << endl;
117  }
118  {
119  dvar_vector vx=dvar_vector(xx);
120  dvariable y=inv_cumd_beta_stable(vx(1),vx(2),vx(3),1.e-7);
121  cout << y << endl;
122  gradcalc(3,g);
123  double h=1.e-6;
124  gg(1)=(inv_cumd_beta_stable(a+h,b,x,1.e-7)-inv_cumd_beta_stable(a-h,b,x,1.e-7))/
125  (2.0*h);
126  gg(2)=(inv_cumd_beta_stable(a,b+h,x,1.e-7)-inv_cumd_beta_stable(a,b-h,x,1.e-7))/
127  (2.0*h);
128  gg(3)=(inv_cumd_beta_stable(a,b,x+h,1.e-7)-inv_cumd_beta_stable(a,b,x-h,1.e-7))/
129  (2.0*h);
130  cout << g << endl << gg << " " << endl << g-gg << endl;
131  }
132  }
133  while(1);
134 }
135 */
136 
137 const double tiny=1.0e-8;
138 const double maxn=150;
139 const double lowerbd=1.0e-40;
140 
142  double x)
143 {
145  df1_two_variable aa;
147  df1_two_variable up;
148  df1_two_variable yy;
149  df1_two_variable um;
151  df1_two_variable ssum;
152 
153  up=a+1.0;
154  um=a-1.0;
155  v=1.0;
156  ssum=a+b;
157  d=1.0-ssum*x/up;
158  if (fabs(value(d)) < lowerbd) d=lowerbd;
159  d=1.0/d;
160  h=d;
161  int m;
162  for (m=1;m<=maxn;m++)
163  {
164  int m2=2*m;
165  aa=m*(b-m)*x/((um+m2)*(a+m2));
166  d=1.0+aa*d;
167  if (fabs(value(d)) < lowerbd) d=lowerbd;
168  v=1.0+aa/v;
169  if (fabs(value(v)) < lowerbd) v=lowerbd;
170  d=1.0/d;
171  h *= d*v;
172  aa = -(a+m)*(ssum+m)*x/((a+m2)*(up+m2));
173  d=1.0+aa*d;
174  if (fabs(value(d)) < lowerbd) d=lowerbd;
175  v=1.0+aa/v;
176  if (fabs(value(v)) < lowerbd) v=lowerbd;
177  d=1.0/d;
178 
179  yy=d*v;
180  h *= yy;
181  if (fabs(value(yy)-1.0) < tiny) break;
182  }
183  if (m > maxn)
184  {
185  cerr << "num interations exceeded " << endl;
186  ad_exit(1);
187  }
188  return h;
189 }
190 
191 /*
192 
193 df1_two_variable gammln(const df1_two_variable& xx)
194 {
195  df1_two_variable x,tmp,ser;
196  static double cof[6]={76.18009173,-86.50532033,24.01409822,
197  -1.231739516,0.120858003e-2,-0.536382e-5};
198  int j;
199  x=xx-1.0;
200  tmp=x+5.5;
201  tmp -= (x+0.5)*log(tmp);
202  ser=1.0;
203  for (j=0;j<=5;j++)
204  {
205  x += 1.0;
206  ser += cof[j]/x;
207  }
208  return -tmp+log(2.50662827465*ser);
209 }
210 */
211 
212 
214 {
216  const double lpp =0.9189385332046727417803297;
217  int n=7;
218  const double c[9]={0.99999999999980993,
219  676.5203681218851,
220  -1259.1392167224028,
221  771.32342877765313,
222  -176.61502916214059,
223  12.507343278686905,
224  -0.13857109526572012,
225  9.9843695780195716e-6,
226  1.5056327351493116e-7};
227  df1_two_variable z=_z-1.0;
228  x=c[0];
229  for (int i=1;i<=n+1;i++)
230  {
231  df1_two_variable zinv=1.0/(z+i);
232  x+=c[i]*zinv;
233  }
234  df1_two_variable t=z+n+0.5;
235  df1_two_variable ans= lpp + (z+0.5)*log(t) -t + log(x);
236  return(ans);
237 }
238 
240 {
241  const double lpi =1.1447298858494001741434272;
242  const double pi =3.1415926535897932384626432;
243  if (value(z)<0.5)
244  {
245  return lpi - log(sin(pi*z)) - gammlnguts(1.0-z);
246  }
247  else
248  {
249  return gammlnguts(z);
250  }
251 }
252 
254  return inv_cumd_beta_stable(a,b,x,eps);
255 }
Base class for dvariable.
Definition: fvar.hpp:1315
Description not yet available.
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
double qbeta(double x, double a, double b, double eps)
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
void default_evaluation3ind(void)
Description not yet available.
Definition: def_eval.cpp:174
double inv_cumd_beta_stable(double a, double b, double y, double eps)
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
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
const double maxn
double betai(const double a, const double b, const double x, int maxit)
Incomplete beta function for constant objects.
Definition: cbetai.cpp:21
const double lowerbd
static double lnbeta(double a, double b)
const double tiny
Float betacf(Float a, Float b, Float x, int MAXIT)
Definition: betacf_val.hpp:13
double eps
Definition: ftweak.cpp:13
double * get_u_y() const
Definition: df12fun.h:62
Description not yet available.
Definition: df12fun.h:54
Description not yet available.
Definition: df12fun.h:82
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static dvariable gammlnguts(const prevariable &_z)
Definition: combv.cpp:249
static _THREAD grad_stack * GRAD_STACK1
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
double * get_u_x() const
Definition: df12fun.h:61
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17