ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dzinbinom.cpp
Go to the documentation of this file.
1 //0<p<1
2 //0<k
3 //0<=x
4 //Aug 1, 2011
5 //Mollie Brooks
6 #include <df1b2fun.h>
7 #include <adrndeff.h>
8 
20 df1b2variable dzinbinom(const double& x, const df1b2variable& mu, const df1b2variable& k, const df1b2variable& p)
21 {
22  //the observed counts are in x
23  //mu is the predicted count
24  //k is the overdispersion parameter
25  if (value(k)<0.0)
26  {
27  cerr<<"k is <=0.0 in dzinbinom()";
28  return(0.0);
29  }
30 
32  df1b2variable loglike;
33  if(x==0)
34  {
35  loglike=log(p + (1-p)* pow(k/(k+mu),k) );
36  }
37  else//x(i)>0
38  {
39  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);
40 
41  }
42 
44  return(-loglike);
45 }
57 dvariable dzinbinom(const double& x, const prevariable& mu, const prevariable& k, const prevariable& p)
58 {
59  //the observed counts are in x
60  //mu is the predicted count
61  //k is the overdispersion parameter
62  if (value(k)<0.0)
63  {
64  cerr<<"k is <=0.0 in dzinbinom()";
65  return(0.0);
66  }
68  dvariable loglike;
69  if(x==0)
70  {
71  loglike=log(p + (1-p)* pow(k/(k+mu),k) );
72  }
73  else//x(i)>0
74  {
75  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);
76  }
77 
79  return(-loglike);
80 }
92 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2variable& p)
93 {
94  //the observed counts are in x
95  //mu is the predicted count
96  //k is the overdispersion parameter
97  if (value(k)<0.0)
98  {
99  cerr<<"k is <=0.0 in dzinbinom()";
100  return(0.0);
101  }
103  int i,imin,imax;
104  imin=x.indexmin();
105  imax=x.indexmax();
106  df1b2variable loglike;
107  loglike=0.;
108  for(i = imin; i<=imax; i++)
109  {
110  if(x(i)==0)
111  {
112  loglike+=log(p + (1-p)* pow(k/(k+mu(i)),k) );
113  }
114  else//x(i)>0
115  {
116  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);
117 
118  }
119  }
121  return(-loglike);
122 }
133 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2variable& p)
134 {
135  //the observed counts are in x
136  //mu is the predicted count
137  //k is the overdispersion parameter
139  int i,imin,imax;
140  imin=x.indexmin();
141  imax=x.indexmax();
142  df1b2variable loglike;
143  loglike=0.;
144 
145  for(i = imin; i<=imax; i++)
146  {
147  if (value(k(i))<0.0)
148  {
149  cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
150  return(0.0);
151  }
152  if(x(i)==0)
153  {
154  loglike+=log(p + (1-p)* pow(k(i)/(k(i)+mu(i)),k(i)) );
155  }
156  else//x(i)>0
157  {
158  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));
159 
160  }
161  }
163  return(-loglike);
164 }
175 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const prevariable& p)
176 {
177  //the observed counts are in x
178  //mu is the predicted count
179  //k is the overdispersion parameter
180  if (value(k)<0.0)
181  {
182  cerr<<"k is <=0.0 in dzinbinom()";
183  return(0.0);
184  }
186  int i,imin,imax;
187  imin=x.indexmin();
188  imax=x.indexmax();
189  dvariable loglike = 0.;
190 
191  for(i = imin; i<=imax; i++)
192  {
193  if(x(i)==0)
194  {
195  loglike+=log(p + (1-p)* pow(k/(k+mu(i)),k) );
196  }
197  else//x(i)>0
198  {
199  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);
200 
201  }
202  }
204  return(-loglike);
205 }
216 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const prevariable& p)
217 {
218  //the observed counts are in x
219  //mu is the predicted count
220  //k is the overdispersion parameter
222  int i,imin,imax;
223  imin=x.indexmin();
224  imax=x.indexmax();
225  dvariable loglike;
226  loglike=0.;
227 
228  for(i = imin; i<=imax; i++)
229  {
230  if (value(k(i))<0.0)
231  {
232  cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
233  return(0.0);
234  }
235  if(x(i)==0)
236  {
237  loglike+=log(p + (1-p)* pow(k(i)/(k(i)+mu(i)),k(i)) );
238  }
239  else//x(i)>0
240  {
241  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));
242 
243  }
244  }
246  return(-loglike);
247 }
251 
261 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2variable& k, const df1b2vector& p)
262 {
263  //the observed counts are in x
264  //mu is the predicted count
265  //k is the overdispersion parameter
266  if (value(k)<0.0)
267  {
268  cerr<<"k is <=0.0 in dzinbinom()";
269  return(0.0);
270  }
272  int i,imin,imax;
273  imin=x.indexmin();
274  imax=x.indexmax();
275  df1b2variable loglike;
276  loglike=0.;
277  for(i = imin; i<=imax; i++)
278  {
279  if(x(i)==0)
280  {
281  loglike+=log(p(i) + (1-p(i))* pow(k/(k+mu(i)),k) );
282  }
283  else//x(i)>0
284  {
285  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);
286 
287  }
288  }
290  return(-loglike);
291 }
302 df1b2variable dzinbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k, const df1b2vector& p)
303 {
304  //the observed counts are in x
305  //mu is the predicted count
306  //k is the overdispersion parameter
308  int i,imin,imax;
309  imin=x.indexmin();
310  imax=x.indexmax();
311  df1b2variable loglike;
312  loglike=0.;
313 
314  for(i = imin; i<=imax; i++)
315  {
316  if (value(k(i))<0.0)
317  {
318  cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
319  return(0.0);
320  }
321  if(x(i)==0)
322  {
323  loglike+=log(p(i) + (1-p(i))* pow(k(i)/(k(i)+mu(i)),k(i)) );
324  }
325  else//x(i)>0
326  {
327  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));
328 
329  }
330  }
332  return(-loglike);
333 }
344 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const prevariable& k, const dvar_vector& p)
345 {
346  //the observed counts are in x
347  //mu is the predicted count
348  //k is the overdispersion parameter
349  if (value(k)<0.0)
350  {
351  cerr<<"k is <=0.0 in dzinbinom()";
352  return(0.0);
353  }
355  int i,imin,imax;
356  imin=x.indexmin();
357  imax=x.indexmax();
358  dvariable loglike = 0.;
359 
360  for(i = imin; i<=imax; i++)
361  {
362  if(x(i)==0)
363  {
364  loglike+=log(p(i) + (1-p(i))* pow(k/(k+mu(i)),k) );
365  }
366  else//x(i)>0
367  {
368  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);
369 
370  }
371  }
373  return(-loglike);
374 }
385 dvariable dzinbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k, const dvar_vector& p)
386 {
387  //the observed counts are in x
388  //mu is the predicted count
389  //k is the overdispersion parameter
391  int i,imin,imax;
392  imin=x.indexmin();
393  imax=x.indexmax();
394  dvariable loglike;
395  loglike=0.;
396 
397  for(i = imin; i<=imax; i++)
398  {
399  if (value(k(i))<0.0)
400  {
401  cerr<<"k("<<i<<") is <=0.0 in dzinbinom()";
402  return(0.0);
403  }
404  if(x(i)==0)
405  {
406  loglike+=log(p(i) + (1-p(i))* pow(k(i)/(k(i)+mu(i)),k(i)) );
407  }
408  else//x(i)>0
409  {
410  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));
411 
412  }
413  }
415  return(-loglike);
416 }
Base class for dvariable.
Definition: fvar.hpp:1315
Description not yet available.
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
void RETURN_ARRAYS_DECREMENT(void)
Decrements gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:507
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
Description not yet available.
Definition: df1b2fun.h:953
ADMB variable vector.
Definition: fvar.hpp:2172
Description not yet available.
Definition: df1b2fun.h:266
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
df1b2variable dzinbinom(const double &x, const df1b2variable &mu, const df1b2variable &k, const df1b2variable &p)
ecologically parametarized negative binomial with zero inflation
Definition: dzinbinom.cpp:20
void RETURN_ARRAYS_INCREMENT(void)
Increments gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:474
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
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
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17