ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dnbinom.cpp
Go to the documentation of this file.
1 #include <df1b2fun.h>
2 #include <adrndeff.h>
12 dvariable dnbinom(const double& x, const prevariable& mu, const prevariable& k)
13 {
14  //x is the observed count
15  //mu is the predicted mean
16  //k is the overdispersion parameter
17  if (value(k)<0.0)
18  {
19  cerr<<"k is <=0.0 in dnbinom()";
20  return(0.0);
21  }
23  dvariable loglike;
24 
25  loglike = gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
26 
28  return(-loglike);
29 }
30 
40 df1b2variable dnbinom(const double& x, const df1b2variable& mu, const df1b2variable& k)
41 {
42  //x is the observed count
43  //mu is the predicted mean
44  //k is the overdispersion parameter
45  if (value(k)<0.0)
46  {
47  cerr<<"k is <=0.0 in dnbinom()";
48  return(0.0);
49  }
51  df1b2variable loglike;
52  loglike = gammln(k+x)-gammln(k)-gammln(x+1)+k*log(k)-k*log(mu+k)+x*log(mu)-x*log(mu+k);
53 
55  return(-loglike);
56 }
57 
68 {
69  //the observed counts are in x
70  //mu is the predicted mean
71  //k is the overdispersion parameter
72  if (value(k)<0.0)
73  {
74  cerr<<"k is <=0.0 in dnbinom()";
75  return(0.0);
76  }
78  int i,imin,imax;
79  imin=x.indexmin();
80  imax=x.indexmax();
81  df1b2variable loglike;
82  loglike=0.;
83  for(i = imin; i<=imax; i++)
84  {
85  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);
86  }
88  return(-loglike);
89 }
99 df1b2variable dnbinom(const dvector& x, const df1b2vector& mu, const df1b2vector& k)
100 {
101  //the observed counts are in x
102  //mu is the predicted mean
103  //k is the overdispersion parameter
104 
106  int i,imin,imax;
107  imin=x.indexmin();
108  imax=x.indexmax();
109  df1b2variable loglike;
110  loglike=0.;
111 
112  for(i = imin; i<=imax; i++)
113  {
114  if (value(k(i))<0.0)
115  {
116  cerr<<"k("<<i<<") is <=0.0 in dnbinom()";
117  return(0.0);
118  }
119  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));
120  }
122  return(-loglike);
123 }
134 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const prevariable& k)
135 {
136  //the observed counts are in x
137  //mu is the predicted mean
138  //k is the overdispersion parameter
139  if (value(k)<0.0)
140  {
141  cerr<<"k is <=0.0 in dnbinom()";
142  return(0.0);
143  }
145  int i,imin,imax;
146  imin=x.indexmin();
147  imax=x.indexmax();
148  dvariable loglike = 0.;
149 
150  for(i = imin; i<=imax; i++)
151  {
152  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);
153  }
155  return(-loglike);
156 }
166 dvariable dnbinom(const dvector& x, const dvar_vector& mu, const dvar_vector& k)
167 {
168  //the observed counts are in x
169  //mu is the predicted mean
170  //k is the overdispersion parameter
172  int i,imin,imax;
173  imin=x.indexmin();
174  imax=x.indexmax();
175  dvariable loglike;
176  loglike=0.;
177 
178  for(i = imin; i<=imax; i++)
179  {
180  if (value(k(i))<0.0)
181  {
182  cerr<<"k("<<i<<") is <=0.0 in dnbinom()";
183  return(0.0);
184  }
185 
186  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));
187  }
189  return(-loglike);
190 }
191 
192 
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
dvariable dnbinom(const double &x, const prevariable &mu, const prevariable &k)
negative log likelihood of negative binomial with mean=mu and variance = mu + mu^2 /k ...
Definition: dnbinom.cpp:12
Description not yet available.
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