ADMB Documentation  11.1.2503
 All Classes Files Functions Variables Typedefs Friends Defines
dzinbinom.cpp
Go to the documentation of this file.
00001 //0<p<1
00002 //0<k
00003 //0<=x
00004 //Aug 1, 2011
00005 //Mollie Brooks
00006 #include <df1b2fun.h>
00007 #include <adrndeff.h> 
00008 
00020 df1b2variable dzinbinom(const double& x, const df1b2variable& mu, const df1b2variable& k, const df1b2variable& p)
00021 {
00022   //the observed counts are in x
00023   //mu is the predicted count
00024   //k is the overdispersion parameter
00025   if (value(k)<0.0)
00026   {
00027     cerr<<"k is <=0.0 in dzinbinom()";
00028     return(0.0);
00029   }
00030   
00031   RETURN_ARRAYS_INCREMENT();
00032   df1b2variable loglike;
00033   if(x==0)
00034   {
00035     loglike=log(p + (1-p)* pow(k/(k+mu),k) );
00036   }
00037   else//x(i)>0
00038   {
00039     loglike=log(1-p) + gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
00040     
00041   }
00042 
00043   RETURN_ARRAYS_DECREMENT();
00044   return(-loglike);
00045 }
00057 dvariable dzinbinom(const double& x, const prevariable& mu, const prevariable& k, const prevariable& p)
00058 {
00059   //the observed counts are in x
00060   //mu is the predicted count
00061   //k is the overdispersion parameter
00062   if (value(k)<0.0)
00063   {
00064     cerr<<"k is <=0.0 in dzinbinom()";
00065     return(0.0);
00066   }
00067   RETURN_ARRAYS_INCREMENT();
00068   dvariable loglike;
00069   if(x==0)
00070   {
00071     loglike=log(p + (1-p)* pow(k/(k+mu),k) );
00072   }
00073   else//x(i)>0
00074   {
00075     loglike=log(1-p) + gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
00076   }
00077 
00078   RETURN_ARRAYS_DECREMENT();
00079   return(-loglike);
00080 }
00092 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2variable& p)
00093 {
00094   //the observed counts are in x
00095   //mu is the predicted count
00096   //k is the overdispersion parameter
00097   if (value(k)<0.0)
00098   {
00099     cerr<<"k is <=0.0 in dzinbinom()";
00100     return(0.0);
00101   }
00102   RETURN_ARRAYS_INCREMENT();
00103   int i,imin,imax;
00104   imin=x.indexmin();
00105   imax=x.indexmax();
00106   df1b2variable loglike;
00107   loglike=0.;
00108   for(i = imin; i<=imax; i++)
00109   {
00110     if(x(i)==0)
00111     {
00112       loglike+=log(p + (1-p)* pow(k/(k+mu(i)),k) );
00113     }
00114     else//x(i)>0
00115     {
00116       loglike+=log(1-p) + 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);
00117       
00118     }
00119   }
00120   RETURN_ARRAYS_DECREMENT();
00121   return(-loglike);
00122 }
00133 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2variable& p)
00134 {
00135   //the observed counts are in x
00136   //mu is the predicted count
00137   //k is the overdispersion parameter
00138   RETURN_ARRAYS_INCREMENT();
00139   int i,imin,imax;
00140   imin=x.indexmin();
00141   imax=x.indexmax();
00142   df1b2variable loglike;
00143   loglike=0.;
00144  
00145   for(i = imin; i<=imax; i++)
00146   {
00147     if (value(k(i))<0.0)
00148     {
00149       cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
00150       return(0.0);
00151     }
00152     if(x(i)==0)
00153     {
00154       loglike+=log(p + (1-p)* pow(k(i)/(k(i)+mu(i)),k(i)) );
00155     }
00156     else//x(i)>0
00157     {
00158       loglike+=log(1-p) + 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));
00159       
00160     }
00161   }
00162   RETURN_ARRAYS_DECREMENT();
00163   return(-loglike);
00164 }
00175 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const prevariable& p)
00176 {
00177   //the observed counts are in x
00178   //mu is the predicted count
00179   //k is the overdispersion parameter
00180   if (value(k)<0.0)
00181   {
00182     cerr<<"k is <=0.0 in dzinbinom()";
00183     return(0.0);
00184   }
00185   RETURN_ARRAYS_INCREMENT();
00186   int i,imin,imax;
00187   imin=x.indexmin();
00188   imax=x.indexmax();
00189   dvariable loglike = 0.;
00190 
00191   for(i = imin; i<=imax; i++)
00192   {
00193     if(x(i)==0)
00194     {
00195       loglike+=log(p + (1-p)* pow(k/(k+mu(i)),k) );
00196     }
00197     else//x(i)>0
00198     {
00199       loglike+=log(1-p) + 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);
00200       
00201     }
00202   }
00203   RETURN_ARRAYS_DECREMENT();
00204   return(-loglike);
00205 }
00216 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const prevariable& p)
00217 {
00218   //the observed counts are in x
00219   //mu is the predicted count
00220   //k is the overdispersion parameter
00221   RETURN_ARRAYS_INCREMENT();
00222   int i,imin,imax;
00223   imin=x.indexmin();
00224   imax=x.indexmax();
00225   dvariable loglike;
00226   loglike=0.;
00227 
00228   for(i = imin; i<=imax; i++)
00229   {
00230     if (value(k(i))<0.0)
00231     {
00232       cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
00233       return(0.0);
00234     }
00235     if(x(i)==0)
00236     {
00237       loglike+=log(p + (1-p)* pow(k(i)/(k(i)+mu(i)),k(i)) );
00238     }
00239     else//x(i)>0
00240     {
00241       loglike+=log(1-p) + 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));
00242       
00243     }
00244   }
00245   RETURN_ARRAYS_DECREMENT();
00246   return(-loglike);
00247 }
00251 
00261 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2vector& p)
00262 {
00263   //the observed counts are in x
00264   //mu is the predicted count
00265   //k is the overdispersion parameter
00266   if (value(k)<0.0)
00267   {
00268     cerr<<"k is <=0.0 in dzinbinom()";
00269     return(0.0);
00270   }
00271   RETURN_ARRAYS_INCREMENT();
00272   int i,imin,imax;
00273   imin=x.indexmin();
00274   imax=x.indexmax();
00275   df1b2variable loglike;
00276   loglike=0.;
00277   for(i = imin; i<=imax; i++)
00278   {
00279     if(x(i)==0)
00280     {
00281       loglike+=log(p(i) + (1-p(i))* pow(k/(k+mu(i)),k) );
00282     }
00283     else//x(i)>0
00284     {
00285       loglike+=log(1-p(i)) + 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);
00286       
00287     }
00288   }
00289   RETURN_ARRAYS_DECREMENT();
00290   return(-loglike);
00291 }
00302 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2vector& p)
00303 {
00304   //the observed counts are in x
00305   //mu is the predicted count
00306   //k is the overdispersion parameter
00307   RETURN_ARRAYS_INCREMENT();
00308   int i,imin,imax;
00309   imin=x.indexmin();
00310   imax=x.indexmax();
00311   df1b2variable loglike;
00312   loglike=0.;
00313  
00314   for(i = imin; i<=imax; i++)
00315   {
00316     if (value(k(i))<0.0)
00317     {
00318       cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
00319       return(0.0);
00320     }
00321     if(x(i)==0)
00322     {
00323       loglike+=log(p(i) + (1-p(i))* pow(k(i)/(k(i)+mu(i)),k(i)) );
00324     }
00325     else//x(i)>0
00326     {
00327       loglike+=log(1-p(i)) + 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));
00328       
00329     }
00330   }
00331   RETURN_ARRAYS_DECREMENT();
00332   return(-loglike);
00333 }
00344 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const dvar_vector& p)
00345 {
00346   //the observed counts are in x
00347   //mu is the predicted count
00348   //k is the overdispersion parameter
00349   if (value(k)<0.0)
00350   {
00351     cerr<<"k is <=0.0 in dzinbinom()";
00352     return(0.0);
00353   }
00354   RETURN_ARRAYS_INCREMENT();
00355   int i,imin,imax;
00356   imin=x.indexmin();
00357   imax=x.indexmax();
00358   dvariable loglike = 0.;
00359 
00360   for(i = imin; i<=imax; i++)
00361   {
00362     if(x(i)==0)
00363     {
00364       loglike+=log(p(i) + (1-p(i))* pow(k/(k+mu(i)),k) );
00365     }
00366     else//x(i)>0
00367     {
00368       loglike+=log(1-p(i)) + 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);
00369       
00370     }
00371   } 
00372   RETURN_ARRAYS_DECREMENT();
00373   return(-loglike);
00374 }
00385 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const dvar_vector& p)
00386 {
00387   //the observed counts are in x
00388   //mu is the predicted count
00389   //k is the overdispersion parameter
00390   RETURN_ARRAYS_INCREMENT();
00391   int i,imin,imax;
00392   imin=x.indexmin();
00393   imax=x.indexmax();
00394   dvariable loglike;
00395   loglike=0.;
00396 
00397   for(i = imin; i<=imax; i++)
00398   {
00399     if (value(k(i))<0.0)
00400     {
00401       cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
00402       return(0.0);
00403     }
00404     if(x(i)==0)
00405     {
00406       loglike+=log(p(i) + (1-p(i))* pow(k(i)/(k(i)+mu(i)),k(i)) );
00407     }
00408     else//x(i)>0
00409     {
00410       loglike+=log(1-p(i)) + 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));
00411       
00412     }
00413   }
00414   RETURN_ARRAYS_DECREMENT();
00415   return(-loglike);
00416 }