ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2invcumdbeta.cpp
Go to the documentation of this file.
1 
6 #include <df1b2fun.h>
7 #include <admodel.h>
8 #include <df3fun.h>
9 #include "df33fun.h"
10 #define MAXIT 100
11 #define EPS 1.0e-9
12 //#define EPS 3.0e-7
13 #define FPMIN 1.0e-30
15  const df1b2variable& b,const df1b2variable& x);
17  const df1b2variable& b,const df1b2variable& x,double eps);
18 
21  const df3_three_variable& b,const df3_three_variable& x);
23  const df3_three_variable& b, double x);
25  const df3_three_variable& b,const df3_three_variable& x,int maxit);
27  const df3_three_variable& b, double x,int maxit);
28 
29 /*
30  static double lnbeta(double a,double b)
31  {
32  return gammln(a)+gammln(b)-gammln(a+b);
33  }
34 
35  static int sgn(double z)
36  {
37  if (z>=0)
38  return 1;
39  else
40  return -1;
41  }
42 */
43 
44  double inv_cumd_beta(double _a,double _b,double _y);
45  double inv_cumd_beta_stable(double _a,double _b,double _y,double eps);
46 
48  const df1b2variable& _b,const df1b2variable& _y,double eps)
49  {
53 
54  double eps1=1.0-eps;
55  // this gets the inverse without derivatives
56  double ca=value(a);
57  double cb=value(b);
58  double cx=inv_cumd_beta_stable(ca,cb,value(y),eps);
59 
63 
64  // this gets the derivatives for the function itself
65 
66  df3_three_variable z=(betai(va,vb,vx,25)-betai(va,vb,eps,25))/
67  (betai(va,vb,eps1,25)-betai(va,vb,eps,25));
68 
69  // now solve for the derivatves of the inverse function
70  double F_x=1.0/(*z.get_u_x());
71  double F_y=-F_x* *z.get_u_y();
72  double F_z=-F_x* *z.get_u_z();
73 
74  double F_xx=-F_x* *z.get_u_xx()/square(*z.get_u_x());
75 
76  double F_xy=-(F_xx * *z.get_u_x() * *z.get_u_y() + F_x * *z.get_u_xy())/
77  *z.get_u_x();
78 
79  double F_xz=-(F_xx * *z.get_u_x() * *z.get_u_z() + F_x * *z.get_u_xz())/
80  *z.get_u_x();
81  double F_yy=-(F_xx * square(*z.get_u_y())
82  +2.0* F_xy* *z.get_u_y()
83  +F_x * *z.get_u_yy());
84 
85  double F_yz=-( F_xx * *z.get_u_y() * *z.get_u_z()
86  + F_x * *z.get_u_yz()
87  + F_xy * *z.get_u_z()
88  + F_xz * *z.get_u_y());
89 
90  double F_zz=-(F_xx * square(*z.get_u_z())
91  +2.0* F_xz* *z.get_u_z()
92  +F_x * *z.get_u_zz());
93 
94  double F_xxx=-(F_x* *z.get_u_xxx()
95  +3.0*F_xx* *z.get_u_x() * *z.get_u_xx())
96  /cube(*z.get_u_x());
97  double F_xxy=-(F_xxx * square(*z.get_u_x())* *z.get_u_y()
98  + 2.0*F_xx* *z.get_u_x()* *z.get_u_xy()
99  + F_xx* *z.get_u_xx() * *z.get_u_y()
100  + F_xy * *z.get_u_xx()
101  + F_x * *z.get_u_xxy())/
102  square(*z.get_u_x());
103  double F_xxz=-(F_xxx * square(*z.get_u_x())* *z.get_u_z()
104  + 2.0*F_xx* *z.get_u_x()* *z.get_u_xz()
105  + F_xx* *z.get_u_xx() * *z.get_u_z()
106  + F_xz * *z.get_u_xx()
107  + F_x * *z.get_u_xxz())/
108  square(*z.get_u_x());
109  double F_xyy=-(F_xxx* *z.get_u_x() *square(*z.get_u_y())
110  +2.0* F_xx * *z.get_u_xy() * *z.get_u_y()
111  +2.0*F_xxy * *z.get_u_x() * *z.get_u_y()
112  + 2.0*F_xy * *z.get_u_xy()
113  + F_xx * *z.get_u_x() * *z.get_u_yy()
114  + F_x * *z.get_u_xyy())/
115  *z.get_u_x();
116  double F_xyz=-(F_xxx* *z.get_u_x() * *z.get_u_y() * *z.get_u_z()
117 
118  +F_xx * *z.get_u_xy() * *z.get_u_z()
119  +F_xx * *z.get_u_xz() * *z.get_u_y()
120  +F_xx * *z.get_u_x() * *z.get_u_yz()
121 
122  +F_xxy * *z.get_u_x() * *z.get_u_z()
123  +F_xxz * *z.get_u_x() * *z.get_u_y()
124 
125  + F_xy * *z.get_u_xz()
126  + F_xz * *z.get_u_xy()
127 
128  + F_x * *z.get_u_xyz())/
129  *z.get_u_x();
130 
131  double F_xzz=-(F_xxx* *z.get_u_x() *square(*z.get_u_z())
132  +2.0* F_xx * *z.get_u_xz() * *z.get_u_z()
133  +2.0*F_xxz * *z.get_u_x() * *z.get_u_z()
134  + 2.0*F_xz * *z.get_u_xz()
135  + F_xx * *z.get_u_x() * *z.get_u_zz()
136  + F_x * *z.get_u_xzz())/
137  *z.get_u_x();
138  double F_yyy=-(F_xxx*cube(*z.get_u_y())+
139  +3.0*F_xxy*square(*z.get_u_y())
140  +3.0*F_xx* *z.get_u_y() * *z.get_u_yy()
141  +3.0*F_xy* *z.get_u_yy()
142  +3.0*F_xyy * *z.get_u_y()
143  +F_x * *z.get_u_yyy());
144 
145  double F_yyz=-(F_xxx * square(*z.get_u_y())* *z.get_u_z()
146  +2.0*F_xxy * *z.get_u_y() * *z.get_u_z()
147  +2.0*F_xyz* *z.get_u_y()
148  +F_xxz*square(*z.get_u_y())
149  +2.0*F_xx* *z.get_u_y() * *z.get_u_yz()
150  +F_xx* *z.get_u_z() * *z.get_u_yy()
151  +2.0*F_xy* *z.get_u_yz()
152  +F_xz * *z.get_u_yy()
153  +F_xyy * *z.get_u_z()
154  +F_x * *z.get_u_yyz());
155  double F_yzz=-(F_xxx * square(*z.get_u_z())* *z.get_u_y()
156  +2.0*F_xxz * *z.get_u_z() * *z.get_u_y()
157  +2.0*F_xyz* *z.get_u_z()
158  +F_xxy*square(*z.get_u_z())
159  +2.0*F_xx* *z.get_u_z() * *z.get_u_yz()
160  +F_xx* *z.get_u_y() * *z.get_u_zz()
161  +2.0*F_xz* *z.get_u_yz()
162  // +F_xy * *z.get_u_zz()* *z.get_u_y() //** got it!
163  +F_xy * *z.get_u_zz() //**
164  +F_xzz * *z.get_u_y()
165  +F_x * *z.get_u_yzz());
166 
167  double F_zzz=-(F_xxx*cube(*z.get_u_z())+
168  +3.0*F_xxz*square(*z.get_u_z())
169  +3.0*F_xx* *z.get_u_z() * *z.get_u_zz()
170  +3.0*F_xz* *z.get_u_zz()
171  +3.0*F_xzz * *z.get_u_z()
172  +F_x * *z.get_u_zzz());
173 
174  df1b2variable tmp;
175  double * xd=_y.get_u_dot();
176  double * yd=_a.get_u_dot();
177  double * zd=_b.get_u_dot();
178  double * tmpd=tmp.get_u_dot();
179  *tmp.get_u()=cx;
180  for (unsigned int i=0;i<df1b2variable::nvar;i++)
181  {
182  *tmpd++ = F_x * *xd++ + F_y * *yd++ + F_z * *zd++;
183  }
185  {
186  f1b2gradlist->write_pass1(&_y,&_a,&_b,&tmp,
187  F_x,F_y,F_z,
188  F_xx,F_xy,F_xz,F_yy,F_yz,F_zz,
189  F_xxx,F_xxy,F_xxz,F_xyy,F_xyz,F_xzz,F_yyy,F_yyz,F_yzz,F_zzz);
190  }
191  // cout<<endl<<"-----------"<<endl;
192  // cout<<_a<<"\t"<<_b<<"\t"<<_y<<endl;
193  // cout<<F_x<<"\t"<<F_y<<"\t"<<F_z<<endl;
194  // cout<<F_xx<<"\t"<<F_xy<<"\t"<<F_xz<<"\t"<<F_yy<<"\t"<<F_yz<<"\t"<<F_zz<<endl;
195  // cout<<F_xxx<<"\t"<<F_xxy<<"\t"<<F_xxz<<"\t"<<F_xyy<<"\t"<<F_xyz<<"\t"<<F_xzz<<"\t"<<F_yyy<<"\t"<<F_yyz<<"\t"<<F_yzz<<"\t"<<F_zzz<<endl;
196  // cout<<endl<<"-----------"<<endl;
197  // ad_exit(1);
198  return tmp;
199  }
200 
213  const df3_three_variable& b,const df3_three_variable& x,int maxit)
214  {
216 
217  if (value(x) < 0.0 || value(x) > 1.0)
218  cerr << "Bad x in routine betai" << endl;
219  if (value(x) == 0.0 || value(x) == 1.0) bt=0.0;
220  else
221  bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x));
222  if (value(x) < (value(a)+1.0)/(value(a)+value(b)+2.0))
223  return bt*betacf(a,b,x)/a;
224  else
225  return 1.0-bt*betacf(b,a,1.0-x)/b;
226  }
227 
229  const df3_three_variable& b, double x,int maxit)
230  {
232 
233  if (x < 0.0 || x > 1.0) cerr << "Bad x in routine betai" << endl;
234  if (x == 0.0 || x == 1.0) bt=0.0;
235  else
236  bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x));
237  if (x < (value(a)+1.0)/(value(a)+value(b)+2.0))
238  return bt*betacf(a,b,x)/a;
239  else
240  return 1.0-bt*betacf(b,a,1.0-x)/b;
241  }
242 
244  const df3_three_variable& b, double x)
245  {
246  int m,m2;
247  df3_three_variable aa,c,d,del,h,qab,qam,qap;
248 
249  qab=a+b;
250  qap=a+1.0;
251  qam=a-1.0;
252  c=1.0;
253  d=1.0-qab*x/qap;
254  if (fabs(value(d)) < FPMIN) d=FPMIN;
255  d=1.0/d;
256  h=d;
257  for (m=1;m<=MAXIT;m++)
258  {
259  m2=2*m;
260  aa=m*(b-m)*x/((qam+m2)*(a+m2));
261  d=1.0+aa*d;
262  if (fabs(value(d)) < FPMIN) d=FPMIN;
263  c=1.0+aa/c;
264  if (fabs(value(c)) < FPMIN) c=FPMIN;
265  d=1.0/d;
266  h *= d*c;
267  aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
268  d=1.0+aa*d;
269  if (fabs(value(d)) < FPMIN) d=FPMIN;
270  c=1.0+aa/c;
271  if (fabs(value(c)) < FPMIN) c=FPMIN;
272  d=1.0/d;
273 
274  del=d*c;
275  h *= del;
276  if (fabs(value(del)-1.0) < EPS) break;
277  }
278  if (m > MAXIT)
279  {
280  cerr << "mum interations exceeded " << endl;
281  ad_exit(1);
282  }
283  return h;
284  }
285 
297  const df3_three_variable& b, const df3_three_variable& x)
298  {
299  int m,m2;
300  df3_three_variable aa,c,d,del,h,qab,qam,qap;
301 
302  qab=a+b;
303  qap=a+1.0;
304  qam=a-1.0;
305  c=1.0;
306  d=1.0-qab*x/qap;
307  if (fabs(value(d)) < FPMIN) d=FPMIN;
308  d=1.0/d;
309  h=d;
310  for (m=1;m<=MAXIT;m++)
311  {
312  m2=2*m;
313  aa=m*(b-m)*x/((qam+m2)*(a+m2));
314  d=1.0+aa*d;
315  if (fabs(value(d)) < FPMIN) d=FPMIN;
316  c=1.0+aa/c;
317  if (fabs(value(c)) < FPMIN) c=FPMIN;
318  d=1.0/d;
319  h *= d*c;
320  aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
321  d=1.0+aa*d;
322  if (fabs(value(d)) < FPMIN) d=FPMIN;
323  c=1.0+aa/c;
324  if (fabs(value(c)) < FPMIN) c=FPMIN;
325  d=1.0/d;
326 
327  del=d*c;
328  h *= del;
329  if (fabs(value(del)-1.0) < EPS) break;
330  }
331  if (m > MAXIT)
332  {
333  cerr << "mum interations exceeded " << endl;
334  ad_exit(1);
335  }
336  return h;
337  }
338 
340 {
342  const double lpp =0.9189385332046727417803297;
343  int n=7;
344  const double c[9]={0.99999999999980993,
345  676.5203681218851,
346  -1259.1392167224028,
347  771.32342877765313,
348  -176.61502916214059,
349  12.507343278686905,
350  -0.13857109526572012,
351  9.9843695780195716e-6,
352  1.5056327351493116e-7};
353  df3_three_variable z=_z-1.0;
354  x=c[0];
355  for (int i=1;i<=n+1;i++)
356  {
357  df3_three_variable zinv=1.0/(z+i);
358  x+=c[i]*zinv;
359  }
360  df3_three_variable t=z+n+0.5;
361  df3_three_variable ans= lpp + (z+0.5)*log(t) -t + log(x);
362  return(ans);
363 }
364 
366 {
367  const double lpi =1.1447298858494001741434272;
368  const double pi =3.1415926535897932384626432;
369  if (value(z)<0.5)
370  {
371  return lpi - log(sin(pi*z)) - gammlnguts(1.0-z);
372  }
373  else
374  {
375  return gammlnguts(z);
376  }
377 }
378 
379 
381  return inv_cumd_beta_stable(a,b,x,eps);
382 }
double * get_u_x() const
Definition: df33fun.h:65
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
Description not yet available.
double * get_u() const
Definition: df1b2fun.h:228
#define EPS
Definition: betacf_val.hpp:10
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
double * get_u_xxy() const
Definition: df33fun.h:75
double inv_cumd_beta(double _a, double _b, double _y)
double * get_u_zz() const
Definition: df33fun.h:73
double * get_u_yzz() const
Definition: df33fun.h:82
double qbeta(double x, double a, double b, double eps)
#define FPMIN
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
double * get_u_xyy() const
Definition: df33fun.h:77
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
double * get_u_xz() const
Definition: df33fun.h:70
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
Description not yet available.
Definition: df1b2fun.h:266
double inv_cumd_beta_stable(double a, double b, double y, double eps)
double * get_u_dot() const
Definition: df1b2fun.h:229
prnstream & endl(prnstream &)
#define MAXIT
double * get_u_yz() const
Definition: df33fun.h:72
Description not yet available.
Definition: df33fun.h:53
static int no_derivatives
Definition: df1b2fun.h:759
double * get_u_xx() const
Definition: df33fun.h:68
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
double * get_u_xy() const
Definition: df33fun.h:69
double * get_u_z() const
Definition: df33fun.h:67
int write_pass1(const df1b2variable *px, const df1b2variable *py, df1b2variable *pz, df1b2function2 *pf)
Description not yet available.
Definition: df1b2f12.cpp:17
Float betacf(Float a, Float b, Float x, int MAXIT)
Definition: betacf_val.hpp:13
Description not yet available.
Definition: df33fun.h:109
static unsigned int nvar
Definition: df1b2fun.h:290
double eps
Definition: ftweak.cpp:13
double * get_u_yy() const
Definition: df33fun.h:71
double * get_u_yyy() const
Definition: df33fun.h:80
double * get_u_y() const
Definition: df33fun.h:66
Description not yet available.
double * get_u_xzz() const
Definition: df33fun.h:79
double * get_u_zzz() const
Definition: df33fun.h:83
double * get_u_xyz() const
Definition: df33fun.h:78
double * get_u_xxz() const
Definition: df33fun.h:76
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double * get_u_xxx() const
Definition: df33fun.h:74
static dvariable gammlnguts(const prevariable &_z)
Definition: combv.cpp:249
double * get_u_yyz() const
Definition: df33fun.h:81
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