ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dgamma.cpp
Go to the documentation of this file.
00001 #include "statsLib.h"
00002 
00037 dvariable dgamma( const dvariable &x, const double& a, const double& b )
00038 {
00039   //E(x)=a/b;
00040   //V(x)=a/b^2
00041   if ( a<= 0 || b<=0 )
00042   {
00043     cerr<<"a or b parameters are less than or equal to 0 in"
00044       "dgamma( const dvariable &x, const double a, const double b )\n";
00045     return 0;
00046   }
00047   if ( x<=0 )
00048   {
00049     cerr<<"The x parameter is less than or equal to 0 in"
00050       "dgamma( const dvariable &x, const double a, const double b )\n";
00051     return 0;
00052   }
00053   
00054   return -a*log(b)+gammln(a)-(a-1.)*log(x)+b*x;
00055 }
00056 
00066 dvariable dgamma( const dvariable &x, const dvariable& a, const dvariable &b )
00067 {
00068   //E(x)=a/b;
00069   //V(x)=a/b^2
00070   if ( a<= 0 || b<=0 )
00071   {
00072     cerr<<"a or b parameters are less than or equal to 0 in"
00073       "dgamma( const dvariable &x, const double a, const double b )\n";
00074     return 0;
00075   }
00076   if ( x<=0 )
00077   {
00078     cerr<<"The x parameter is less than or equal to 0 in"
00079       "dgamma( const dvariable &x, const double a, const double b )\n";
00080     return 0;
00081   }
00082   
00083   return -a*log(b)+gammln(a)-(a-1.)*log(x)+b*x;
00084 
00085 }
00086 
00097 dvariable dgamma( const dvector &x, const prevariable& a, const prevariable& b )
00098 {
00099   //E(x)=a/b;
00100   //V(x)=a/b^2
00101   if ( a<= 0 || b<=0 )
00102   {
00103     cerr<<"a or b parameters are less than or equal to 0 in"
00104       "dgamma( const dvariable &x, const double a, const double b )\n";
00105     return 0;
00106   }
00107   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00108   {
00109     if ( x(i)<=0 )
00110     {
00111       cerr<<"The x("<<i<<") parameter is less than or equal to 0 in"
00112         "dgamma( const dvariable &x, const double a, const double b )\n";
00113       return 0;
00114     }
00115   }
00116   return sum(-a*log(b)+gammln(a)-(a-1.)*log(x)+b*x);
00117 
00118 }
00129 dvariable dgamma( const dvector &x, const prevariable& a, const dvar_vector& b )
00130 {
00131   //E(x)=a/b;
00132   //V(x)=a/b^2
00133   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00134   {
00135     if ( a<= 0 || b(i)<=0 )
00136     {
00137       cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
00138         "dgamma( const dvariable &x, const double a, const double b )\n";
00139       return 0;
00140     }
00141   } 
00142   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00143   {
00144     if ( x(i)<=0 )
00145     {
00146       cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
00147         "dgamma( const dvariable &x, const double a, const double b )\n";
00148       return 0;
00149     }
00150   }
00151   return sum(-a*log(b)+gammln(a)-(a-1.)*log(x)+elem_prod(b, x));
00152 }
00163 dvariable dgamma( const dvector &x, const dvar_vector& a, const dvar_vector& b )
00164 {
00165   //E(x)=a/b;
00166   //V(x)=a/b^2
00167   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00168   {
00169     if ( a(i)<= 0 || b(i)<=0 )
00170     {
00171       cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
00172         "dgamma( const dvariable &x, const double a, const double b )\n";
00173       return 0;
00174     }
00175   } 
00176   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00177   {
00178     if ( x(i)<=0 )
00179     {
00180       cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
00181         "dgamma( const dvariable &x, const double a, const double b )\n";
00182       return 0;
00183     }
00184   }
00185   return sum(-elem_prod(a, log(b))+gammln(a)-elem_prod((a-1.), log(x))+elem_prod(b, x));
00186 }
00197 dvariable dgamma( const dvector &x, const dvar_vector& a, const prevariable& b )
00198 {
00199   //E(x)=a/b;
00200   //V(x)=a/b^2
00201   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00202   {
00203     if ( a(i)<= 0 || b<=0 )
00204     {
00205       cerr<<"a or b ("<<i<<") parameters are less than or equal to 0 in"
00206         "dgamma( const dvariable &x, const double a, const double b )\n";
00207       return 0;
00208     }
00209   } 
00210   for(int i=x.indexmin(); i<=x.indexmax(); i++)
00211   {
00212     if ( x(i)<=0 )
00213     {
00214       cerr<<"The x ("<<i<<") parameter is less than or equal to 0 in"
00215         "dgamma( const dvariable &x, const double a, const double b )\n";
00216       return 0;
00217     }
00218   }
00219   return sum(-a*log(b)+gammln(a)-elem_prod((a-1.), log(x))+b*x);
00220 }
00221