ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dgamma.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 
37 dvariable dgamma( const dvariable &x, const double& a, const double& b )
38 {
39  //E(x)=a/b;
40  //V(x)=a/b^2
41  if ( a<= 0 || b<=0 )
42  {
43  cerr<<"a or b parameters are less than or equal to 0 in"
44  "dgamma( const dvariable &x, const double a, const double b )\n";
45  return 0;
46  }
47  if ( x<=0 )
48  {
49  cerr<<"The x parameter is less than or equal to 0 in"
50  "dgamma( const dvariable &x, const double a, const double b )\n";
51  return 0;
52  }
53 
54  return -a*log(b)+gammln(a)-(a-1.)*log(x)+b*x;
55 }
56 
66 dvariable dgamma( const dvariable &x, const dvariable& a, const dvariable &b )
67 {
68  //E(x)=a/b;
69  //V(x)=a/b^2
70  if ( a<= 0 || b<=0 )
71  {
72  cerr<<"a or b parameters are less than or equal to 0 in"
73  "dgamma( const dvariable &x, const double a, const double b )\n";
74  return 0;
75  }
76  if ( x<=0 )
77  {
78  cerr<<"The x parameter is less than or equal to 0 in"
79  "dgamma( const dvariable &x, const double a, const double b )\n";
80  return 0;
81  }
82 
83  return -a*log(b)+gammln(a)-(a-1.)*log(x)+b*x;
84 
85 }
86 
97 dvariable dgamma( const dvector &x, const prevariable& a, const prevariable& b )
98 {
99  //E(x)=a/b;
100  //V(x)=a/b^2
101  if ( a<= 0 || b<=0 )
102  {
103  cerr<<"a or b parameters are less than or equal to 0 in"
104  "dgamma( const dvariable &x, const double a, const double b )\n";
105  return 0;
106  }
107  for(int i=x.indexmin(); i<=x.indexmax(); i++)
108  {
109  if ( x(i)<=0 )
110  {
111  cerr<<"The x("<<i<<") parameter is less than or equal to 0 in"
112  "dgamma( const dvariable &x, const double a, const double b )\n";
113  return 0;
114  }
115  }
116  return sum(-a*log(b)+gammln(a)-(a-1.)*log(x)+b*x);
117 
118 }
129 dvariable dgamma( const dvector &x, const prevariable& a, const dvar_vector& b )
130 {
131  //E(x)=a/b;
132  //V(x)=a/b^2
133  for(int i=x.indexmin(); i<=x.indexmax(); i++)
134  {
135  if ( a<= 0 || b(i)<=0 )
136  {
137  cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
138  "dgamma( const dvariable &x, const double a, const double b )\n";
139  return 0;
140  }
141  }
142  for(int i=x.indexmin(); i<=x.indexmax(); i++)
143  {
144  if ( x(i)<=0 )
145  {
146  cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
147  "dgamma( const dvariable &x, const double a, const double b )\n";
148  return 0;
149  }
150  }
151  return sum(-a*log(b)+gammln(a)-(a-1.)*log(x)+elem_prod(b, x));
152 }
163 dvariable dgamma( const dvector &x, const dvar_vector& a, const dvar_vector& b )
164 {
165  //E(x)=a/b;
166  //V(x)=a/b^2
167  for(int i=x.indexmin(); i<=x.indexmax(); i++)
168  {
169  if ( a(i)<= 0 || b(i)<=0 )
170  {
171  cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
172  "dgamma( const dvariable &x, const double a, const double b )\n";
173  return 0;
174  }
175  }
176  for(int i=x.indexmin(); i<=x.indexmax(); i++)
177  {
178  if ( x(i)<=0 )
179  {
180  cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
181  "dgamma( const dvariable &x, const double a, const double b )\n";
182  return 0;
183  }
184  }
185  return sum(-elem_prod(a, log(b))+gammln(a)-elem_prod((a-1.), log(x))+elem_prod(b, x));
186 }
197 dvariable dgamma( const dvector &x, const dvar_vector& a, const prevariable& b )
198 {
199  //E(x)=a/b;
200  //V(x)=a/b^2
201  for(int i=x.indexmin(); i<=x.indexmax(); i++)
202  {
203  if ( a(i)<= 0 || b<=0 )
204  {
205  cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
206  "dgamma( const dvariable &x, const double a, const double b )\n";
207  return 0;
208  }
209  }
210  for(int i=x.indexmin(); i<=x.indexmax(); i++)
211  {
212  if ( x(i)<=0 )
213  {
214  cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
215  "dgamma( const dvariable &x, const double a, const double b )\n";
216  return 0;
217  }
218  }
219  return sum(-a*log(b)+gammln(a)-elem_prod((a-1.), log(x))+b*x);
220 }
221 
Base class for dvariable.
Definition: fvar.hpp:1315
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
ADMB variable vector.
Definition: fvar.hpp:2172
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
dvariable dgamma(const dvariable &x, const double &a, const double &b)
Definition: dgamma.cpp:37
Library of statistic functions.
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