ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dbinom.cpp
Go to the documentation of this file.
00001 #include <df1b2fun.h>
00002 #include <adrndeff.h> 
00003 #include "statsLib.h"
00004 
00005 
00039 dvariable dbinom(const prevariable& x,const double& n,const double& p)
00040 {
00041   if(p<=0. && p>=1.0)
00042   {
00043     cerr<<"The probability p must be greater than 0 or less than 1 in "
00044       "dbinom(const prevariable& x,const double& n,const double& p)\n";
00045     return(0.0);
00046   }
00047   
00048   RETURN_ARRAYS_INCREMENT();
00049   dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00050   //dvariable tmp = factln(x)-factln(n-x)-factln(n)-x*log(p)-(n-x)*log(1.-p);
00051   RETURN_ARRAYS_DECREMENT();
00052   return(tmp);
00053 }
00063 dvariable dbinom(const prevariable& x,const prevariable& n,const double& p)
00064 {
00065   if(p<=0. && p>=1.0)
00066   {
00067     cerr<<"The probability p must be greater than 0 or less than 1 in "
00068       "dbinom(const prevariable& x,const prevariable& n,const double& p)\n";
00069     return(0.0);
00070   }
00071   
00072   RETURN_ARRAYS_INCREMENT();
00073   dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00074   RETURN_ARRAYS_DECREMENT();
00075   return(tmp);
00076 }
00086 dvariable dbinom(const prevariable& x,const prevariable& n,const prevariable& p)
00087 {
00088   if(p<=0. && p>=1.0)
00089   {
00090     cerr<<"The probability p must be greater than 0 or less than 1 in "
00091       "dbinom(const prevariable& x,const prevariable& n,const prevariable& p)\n";
00092     return(0.0);
00093   }
00094   
00095   RETURN_ARRAYS_INCREMENT();
00096   dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00097   RETURN_ARRAYS_DECREMENT();
00098   return(tmp);
00099 }
00100 
00110 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const dvar_vector& p)
00111 {
00112   if(min(p)<=0. && max(p)>=1.0)
00113   {
00114     cerr<<"The probability p must be greater than 0 or less than 1 in "
00115       "dbinom(const dvar_vector& x,const dvar_vector& n,const dvar_vector& p)\n";
00116     return(0.0);
00117   }
00118   
00119   int i;
00120   int imin = x.indexmin();
00121   int imax = x.indexmax();
00122   
00123   RETURN_ARRAYS_INCREMENT();
00124   dvariable tmp = 0.0;
00125   for(i=imin; i<=imax;i++)
00126   {
00127     tmp += log_comb(n(i),x(i))-x(i)*log(p(i))-(n(i)-x(i))*log(1.-p(i));
00128   }
00129   
00130   RETURN_ARRAYS_DECREMENT();
00131   return(tmp);
00132 }
00133 
00134 dvariable dbinom(const prevariable& x,const dvar_vector& n,const dvar_vector& p)
00135 {
00136   if(min(p)<=0. && max(p)>=1.0)
00137   {
00138     cerr<<"The probability p must be greater than 0 or less than 1 in "
00139       "dbinom(const prevariable& x,const dvar_vector& n,const dvar_vector& p)\n";
00140     return(0.0);
00141   }
00142   
00143   int i;
00144   int imin = n.indexmin();
00145   int imax = n.indexmax();
00146   
00147   RETURN_ARRAYS_INCREMENT();
00148   dvariable tmp = 0.0;
00149   for(i=imin; i<=imax;i++)
00150   {
00151     tmp += log_comb(n(i),x)-x*log(p(i))-(n(i)-x)*log(1.-p(i));
00152   }
00153   
00154   RETURN_ARRAYS_DECREMENT();
00155   return(tmp);
00156 }
00157 dvariable dbinom(const dvar_vector& x,const prevariable& n,const dvar_vector& p)
00158 {
00159   if(min(p)<=0. && max(p)>=1.0)
00160   {
00161     cerr<<"The probability p must be greater than 0 or less than 1 in "
00162       "dbinom(const dvar_vector& x,const prevariable& n,const dvar_vector& p)\n";
00163     return(0.0);
00164   }
00165   
00166   int i;
00167   int imin = x.indexmin();
00168   int imax = x.indexmax();
00169   
00170   RETURN_ARRAYS_INCREMENT();
00171   dvariable tmp = 0.0;
00172   for(i=imin; i<=imax;i++)
00173   {
00174     tmp += log_comb(n,x(i))-x(i)*log(p(i))-(n-x(i))*log(1.-p(i));
00175   }
00176   
00177   RETURN_ARRAYS_DECREMENT();
00178   return(tmp);
00179 }
00180 dvariable dbinom(const dvar_vector& x,const dvar_vector& n,const prevariable& p)
00181 {
00182   if(p<=0. && p>=1.0)
00183   {
00184     cerr<<"The probability p must be greater than 0 or less than 1 in "
00185       "dbinom(const dvar_vector& x,const dvar_vector& n,const prevariable& p)\n";
00186     return(0.0);
00187   }
00188   
00189   int i;
00190   int imin = x.indexmin();
00191   int imax = x.indexmax();
00192   
00193   RETURN_ARRAYS_INCREMENT();
00194   dvariable tmp = 0.0;
00195   for(i=imin; i<=imax;i++)
00196   {
00197     tmp += log_comb(n(i),x(i))-x(i)*log(p)-(n(i)-x(i))*log(1.-p);
00198   }
00199   
00200   RETURN_ARRAYS_DECREMENT();
00201   return(tmp);
00202 }
00203 df1b2variable dbinom(const df1b2variable& x,const double& n,const double& p)
00204 {
00205   if(p<=0. && p>=1.0)
00206   {
00207     cerr<<"The probability p must be greater than 0 or less than 1 in "
00208       "dbinom(const df1b2variable& x,const double& n,const double& p)\n";
00209     return(0.0);
00210   }
00211   
00212   RETURN_ARRAYS_INCREMENT();
00213   df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00214   //df1b2variable tmp = factln(x)-factln(n-x)-factln(n)-x*log(p)-(n-x)*log(1.-p);
00215   RETURN_ARRAYS_DECREMENT();
00216   return(tmp);
00217 }
00227 df1b2variable dbinom(const df1b2variable& x,const df1b2variable& n,const double& p)
00228 {
00229   if(p<=0. && p>=1.0)
00230   {
00231     cerr<<"The probability p must be greater than 0 or less than 1 in "
00232       "dbinom(const df1b2variable& x,const df1b2variable& n,const double& p)\n";
00233     return(0.0);
00234   }
00235   
00236   RETURN_ARRAYS_INCREMENT();
00237   df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00238   RETURN_ARRAYS_DECREMENT();
00239   return(tmp);
00240 }
00250 df1b2variable dbinom(const df1b2variable& x,const df1b2variable& n,const df1b2variable& p)
00251 {
00252   if(value(p)<=0. && value(p)>=1.0)
00253   {
00254     cerr<<"The probability p must be greater than 0 or less than 1 in "
00255       "dbinom(const df1b2variable& x,const df1b2variable& n,const df1b2variable& p)\n";
00256     return(0.0);
00257   }
00258   
00259   RETURN_ARRAYS_INCREMENT();
00260   df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
00261   RETURN_ARRAYS_DECREMENT();
00262   return(tmp);
00263 }
00264 
00274 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2vector& p)
00275 {
00276   if(min(value(p))<=0. && max(value(p))>=1.0)
00277   {
00278     cerr<<"The probability p must be greater than 0 or less than 1 in "
00279       "dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2vector& p)\n";
00280     return(0.0);
00281   }
00282   
00283   int i;
00284   int imin = x.indexmin();
00285   int imax = x.indexmax();
00286   
00287   RETURN_ARRAYS_INCREMENT();
00288   df1b2variable tmp = 0.0;
00289   for(i=imin; i<=imax;i++)
00290   {
00291     tmp += log_comb(n(i),x(i))-x(i)*log(p(i))-(n(i)-x(i))*log(1.-p(i));
00292   }
00293   
00294   RETURN_ARRAYS_DECREMENT();
00295   return(tmp);
00296 }
00297 
00298 df1b2variable dbinom(const df1b2variable& x,const df1b2vector& n,const df1b2vector& p)
00299 {
00300   if(min(value(p))<=0. && max(value(p))>=1.0)
00301   {
00302     cerr<<"The probability p must be greater than 0 or less than 1 in "
00303       "dbinom(const df1b2variable& x,const df1b2vector& n,const df1b2vector& p)\n";
00304     return(0.0);
00305   }
00306   
00307   int i;
00308   int imin = n.indexmin();
00309   int imax = n.indexmax();
00310   
00311   RETURN_ARRAYS_INCREMENT();
00312   df1b2variable tmp = 0.0;
00313   for(i=imin; i<=imax;i++)
00314   {
00315     tmp += log_comb(n(i),x)-x*log(p(i))-(n(i)-x)*log(1.-p(i));
00316   }
00317   
00318   RETURN_ARRAYS_DECREMENT();
00319   return(tmp);
00320 }
00321 df1b2variable dbinom(const df1b2vector& x,const df1b2variable& n,const df1b2vector& p)
00322 {
00323   if(min(value(p))<=0. && max(value(p))>=1.0)
00324   {
00325     cerr<<"The probability p must be greater than 0 or less than 1 in "
00326       "dbinom(const df1b2vector& x,const df1b2variable& n,const df1b2vector& p)\n";
00327     return(0.0);
00328   }
00329   
00330   int i;
00331   int imin = x.indexmin();
00332   int imax = x.indexmax();
00333   
00334   RETURN_ARRAYS_INCREMENT();
00335   df1b2variable tmp = 0.0;
00336   for(i=imin; i<=imax;i++)
00337   {
00338     tmp += log_comb(n,x(i))-x(i)*log(p(i))-(n-x(i))*log(1.-p(i));
00339   }
00340   
00341   RETURN_ARRAYS_DECREMENT();
00342   return(tmp);
00343 }
00344 df1b2variable dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2variable& p)
00345 {
00346   if(value(p)<=0. && value(p)>=1.0)
00347   {
00348     cerr<<"The probability p must be greater than 0 or less than 1 in "
00349       "dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2variable& p)\n";
00350     return(0.0);
00351   }
00352   
00353   int i;
00354   int imin = x.indexmin();
00355   int imax = x.indexmax();
00356   
00357   RETURN_ARRAYS_INCREMENT();
00358   df1b2variable tmp = 0.0;
00359   for(i=imin; i<=imax;i++)
00360   {
00361     tmp += log_comb(n(i),x(i))-x(i)*log(p)-(n(i)-x(i))*log(1.-p);
00362   }
00363   
00364   RETURN_ARRAYS_DECREMENT();
00365   return(tmp);
00366 }