ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dnbinom.cpp
Go to the documentation of this file.
00001 #include <df1b2fun.h>
00002 #include <adrndeff.h> 
00012 dvariable dnbinom(const double& x, const prevariable& mu, const prevariable& k)
00013 {
00014   //x is the observed count
00015   //mu is the predicted mean
00016   //k is the overdispersion parameter
00017   if (value(k)<0.0)
00018   {
00019     cerr<<"k is <=0.0 in dnbinom()";
00020     return(0.0);
00021   }
00022   RETURN_ARRAYS_INCREMENT();
00023   dvariable loglike;
00024 
00025   loglike = gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
00026 
00027   RETURN_ARRAYS_DECREMENT();
00028   return(-loglike);
00029 }
00030 
00040 df1b2variable dnbinom(const double& x, const df1b2variable& mu, const df1b2variable& k)
00041 {
00042   //x is the observed count
00043   //mu is the predicted mean
00044   //k is the overdispersion parameter
00045   if (value(k)<0.0)
00046   {
00047     cerr<<"k is <=0.0 in dnbinom()";
00048     return(0.0);
00049   }
00050   RETURN_ARRAYS_INCREMENT();
00051   df1b2variable loglike;
00052   loglike = gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
00053 
00054   RETURN_ARRAYS_DECREMENT();
00055   return(-loglike);
00056 }
00057 
00067 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k)
00068 {
00069   //the observed counts are in x
00070   //mu is the predicted mean
00071   //k is the overdispersion parameter
00072   if (value(k)<0.0)
00073   {
00074     cerr<<"k is <=0.0 in dnbinom()";
00075     return(0.0);
00076   }
00077   RETURN_ARRAYS_INCREMENT();
00078   int i,imin,imax;
00079   imin=x.indexmin();
00080   imax=x.indexmax();
00081   df1b2variable loglike;
00082   loglike=0.;
00083   for(i = imin; i<=imax; i++)
00084   { 
00085     loglike += gammln(k+x(i))-gammln(k)-gammln(x(i)+1)+k*log(k)-k*log(mu(i)+k)+x(i)*log(mu(i))-x(i)*log(mu(i)+k);
00086   }
00087   RETURN_ARRAYS_DECREMENT();
00088   return(-loglike);
00089 }
00099 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k)
00100 {
00101   //the observed counts are in x
00102   //mu is the predicted mean
00103   //k is the overdispersion parameter
00104 
00105   RETURN_ARRAYS_INCREMENT();
00106   int i,imin,imax;
00107   imin=x.indexmin();
00108   imax=x.indexmax();
00109   df1b2variable loglike;
00110   loglike=0.;
00111  
00112   for(i = imin; i<=imax; i++)
00113   {
00114     if (value(k(i))<0.0)
00115     {
00116       cerr<<"k("<<i<<") is <=0.0 in dnbinom()";
00117       return(0.0);
00118     }
00119     loglike += gammln(k(i)+x(i))-gammln(k(i))-gammln(x(i)+1)+k(i)*log(k(i))-k(i)*log(mu(i)+k(i))+x(i)*log(mu(i))-x(i)*log(mu(i)+k(i));
00120   }
00121   RETURN_ARRAYS_DECREMENT();
00122   return(-loglike);
00123 }
00134 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const prevariable& k)
00135 {
00136   //the observed counts are in x
00137   //mu is the predicted mean
00138   //k is the overdispersion parameter
00139   if (value(k)<0.0)
00140   {
00141     cerr<<"k is <=0.0 in dnbinom()";
00142     return(0.0);
00143   }
00144   RETURN_ARRAYS_INCREMENT();
00145   int i,imin,imax;
00146   imin=x.indexmin();
00147   imax=x.indexmax();
00148   dvariable loglike = 0.;
00149 
00150   for(i = imin; i<=imax; i++)
00151   {
00152     loglike += gammln(k+x(i))-gammln(k)-gammln(x(i)+1)+k*log(k)-k*log(mu(i)+k)+x(i)*log(mu(i))-x(i)*log(mu(i)+k);
00153   }
00154   RETURN_ARRAYS_DECREMENT();
00155   return(-loglike);
00156 }
00166 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k)
00167 {
00168   //the observed counts are in x
00169   //mu is the predicted mean
00170   //k is the overdispersion parameter
00171   RETURN_ARRAYS_INCREMENT();
00172   int i,imin,imax;
00173   imin=x.indexmin();
00174   imax=x.indexmax();
00175   dvariable loglike;
00176   loglike=0.;
00177 
00178   for(i = imin; i<=imax; i++)
00179   {
00180     if (value(k(i))<0.0)
00181     {
00182       cerr<<"k("<<i<<") is <=0.0 in dnbinom()";
00183       return(0.0);
00184     }
00185     
00186     loglike += gammln(k(i)+x(i))-gammln(k(i))-gammln(x(i)+1)+k(i)*log(k(i))-k(i)*log(mu(i)+k(i))+x(i)*log(mu(i))-x(i)*log(mu(i)+k(i));
00187   }
00188   RETURN_ARRAYS_DECREMENT();
00189   return(-loglike);
00190 }
00191 
00192