ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df3gammp.cpp
Go to the documentation of this file.
1 
7 #include <df1b2fun.h>
8 #define ITMAX 100
9 #define EPS 1.0e-9
10 //#define EPS 3.0e-7
11 #define FPMIN 1.0e-30
12 double get_values(double x,double y,int print_switch);
13 
14 
16  const df1b2variable& _xtau)
17 {
20  init_df3_two_variable mu(xmu);
21  init_df3_two_variable tau(xtau);
22  *mu.get_u_x()=1.0;
23  *tau.get_u_y()=1.0;
24  if (value(tau)-1.0<0.0)
25  {
26  cerr << "tau <=1 in log_negbinomial_density " << endl;
27  ad_exit(1);
28  }
29  df3_two_variable r=mu/(1.e-120+(tau-1.0));
30  df3_two_variable tmp;
31  tmp=gammln(x+r)-gammln(r) -gammln(x+1)
32  +r*log(r)+x*log(mu)-(r+x)*log(r+mu);
33  df1b2variable tmp1;
34  tmp1=tmp;
35  return tmp1;
36 }
37 
47 {
48  df3_two_variable x,tmp,ser,tmp1;
49  static double cof[6]={76.18009173,-86.50532033,24.01409822,
50  -1.231739516,0.120858003e-2,-0.536382e-5};
51  int j;
52  x=xx-1.0;
53  tmp=x+5.5;
54  tmp -= (x+0.5)*log(tmp);
55  ser=1.0;
56  for (j=0;j<=5;j++)
57  {
58  x += 1.0;
59  ser += cof[j]/x;
60  }
61  return -tmp+log(2.50662827465*ser);
62 }
63 
64 
71 void gcf(const df3_two_variable& _gammcf,const df3_two_variable& a,
72  const df3_two_variable& x,const df3_two_variable& _gln)
73 {
76  int i;
77  df3_two_variable an,b,c,d,del,h;
78 
79  gln=gammln(a);
80  b=x+1.0-a;
81  c=1.0/FPMIN;
82  d=1.0/b;
83  h=d;
84  for (i=1;i<=ITMAX;i++) {
85  an = -i*(i-a);
86  b += 2.0;
87  d=an*d+b;
88  if (fabs(value(d)) < FPMIN) d=FPMIN;
89  c=b+an/c;
90  if (fabs(value(c)) < FPMIN) c=FPMIN;
91  d=1.0/d;
92  del=d*c;
93  h *= del;
94  if (fabs(value(del)-1.0) < EPS) break;
95  }
96  if (i > ITMAX)
97  cerr << "a too large, ITMAX too small in gcf" << endl;
98  gammcf=exp(-x+a*log(x)-(gln))*h;
99 }
100 
107 void gser(const df3_two_variable& _gamser,const df3_two_variable& a,
108  const df3_two_variable& x,const df3_two_variable& _gln)
109 {
110  int n;
113  df3_two_variable sum,del,ap;
114 
115  gln=gammln(a);
116 
117  if (value(x) <= 0.0) {
118  if (value(x) < 0.0)
119  cerr << "x less than 0 in routine gser" << endl;
120  gamser=0.0;
121  return;
122  }
123  else
124  {
125  ap=a;
126  del=sum=1.0/a;
127  for (n=1;n<=ITMAX;n++) {
128  ap+=1.0;
129  del *= x/ap;
130  sum += del;
131  if (fabs(value(del)) < fabs(value(sum))*EPS) {
132  gamser=sum*exp(-x+a*log(x)-(gln));
133  return;
134  }
135  }
136  cerr << "a too large, ITMAX too small in routine gser" << endl;
137  return;
138  }
139 }
140 
141 
143  const df3_two_variable& a)
144 {
145  df3_two_variable gamser,gammcf,gln;
146 
147  if (value(x) < 0.0 || value(a) <= 0.0)
148  cerr << "Invalid arguments in routine gammp" << endl;
149  if (value(x) < (value(a)+1.0)) {
150  gser(gamser,a,x,gln);
151  return gamser;
152  } else {
153  gcf(gammcf,a,x,gln);
154  return 1.0-gammcf;
155  }
156 }
158  const df3_two_variable& a)
159 {
160  df3_two_variable tmp;
161  if (value(x)<=0)
162  return 0.5*exp(x);
163  else
164  return 1.0-0.5*exp(-x);
165 }
167  const df3_two_variable& a)
168 {
169  return atan(x/a);
170 }
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define EPS
Definition: betacf_val.hpp:10
Description not yet available.
Definition: df32fun.h:55
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Description not yet available.
Definition: df32fun.h:130
#define ITMAX
$Id$
Definition: df3gammp.cpp:8
double cumd_gamma(double x, double a)
Definition: cgamdev.cpp:38
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
Description not yet available.
Definition: df1b2fun.h:266
double * get_u_y(void) const
Definition: df32fun.h:74
double * get_u_x(void) const
Definition: df32fun.h:70
prnstream & endl(prnstream &)
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
void gcf(double &gammcf, double a, double x, double &gln)
Incomplete gamma function.
Definition: cgamdev.cpp:59
void gser(double &gamser, double a, double x, double &gln)
Incomplete gamma function.
Definition: cgamdev.cpp:92
#define FPMIN
Definition: df3gammp.cpp:11
double get_values(double x, double y, int print_switch)
double cumd_exponential(double x)
Description not yet available.
Definition: ccumdexp.cpp:26
Description not yet available.
double cumd_cauchy(const double &x)
Description not yet available.
Definition: cumd_cau.cpp:17
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double log_negbinomial_density(double x, double mu, double tau)
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13