ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dbinom.cpp
Go to the documentation of this file.
1 #include <df1b2fun.h>
2 #include <adrndeff.h>
3 #include "statsLib.h"
4 
5 
39 dvariable dbinom(const prevariable& x,const double& n,const double& p)
40 {
41  if(p<=0. && p>=1.0)
42  {
43  cerr<<"The probability p must be greater than 0 or less than 1 in "
44  "dbinom(const prevariable& x,const double& n,const double& p)\n";
45  return(0.0);
46  }
47 
49  dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
50  //dvariable tmp = factln(x)-factln(n-x)-factln(n)-x*log(p)-(n-x)*log(1.-p);
52  return(tmp);
53 }
63 dvariable dbinom(const prevariable& x,const prevariable& n,const double& p)
64 {
65  if(p<=0. && p>=1.0)
66  {
67  cerr<<"The probability p must be greater than 0 or less than 1 in "
68  "dbinom(const prevariable& x,const prevariable& n,const double& p)\n";
69  return(0.0);
70  }
71 
73  dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
75  return(tmp);
76 }
87 {
88  if(p<=0. && p>=1.0)
89  {
90  cerr<<"The probability p must be greater than 0 or less than 1 in "
91  "dbinom(const prevariable& x,const prevariable& n,const prevariable& p)\n";
92  return(0.0);
93  }
94 
96  dvariable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
98  return(tmp);
99 }
100 
111 {
112  if(min(p)<=0. && max(p)>=1.0)
113  {
114  cerr<<"The probability p must be greater than 0 or less than 1 in "
115  "dbinom(const dvar_vector& x,const dvar_vector& n,const dvar_vector& p)\n";
116  return(0.0);
117  }
118 
119  int i;
120  int imin = x.indexmin();
121  int imax = x.indexmax();
122 
124  dvariable tmp = 0.0;
125  for(i=imin; i<=imax;i++)
126  {
127  tmp += log_comb(n(i),x(i))-x(i)*log(p(i))-(n(i)-x(i))*log(1.-p(i));
128  }
129 
131  return(tmp);
132 }
133 
135 {
136  if(min(p)<=0. && max(p)>=1.0)
137  {
138  cerr<<"The probability p must be greater than 0 or less than 1 in "
139  "dbinom(const prevariable& x,const dvar_vector& n,const dvar_vector& p)\n";
140  return(0.0);
141  }
142 
143  int i;
144  int imin = n.indexmin();
145  int imax = n.indexmax();
146 
148  dvariable tmp = 0.0;
149  for(i=imin; i<=imax;i++)
150  {
151  tmp += log_comb(n(i),x)-x*log(p(i))-(n(i)-x)*log(1.-p(i));
152  }
153 
155  return(tmp);
156 }
158 {
159  if(min(p)<=0. && max(p)>=1.0)
160  {
161  cerr<<"The probability p must be greater than 0 or less than 1 in "
162  "dbinom(const dvar_vector& x,const prevariable& n,const dvar_vector& p)\n";
163  return(0.0);
164  }
165 
166  int i;
167  int imin = x.indexmin();
168  int imax = x.indexmax();
169 
171  dvariable tmp = 0.0;
172  for(i=imin; i<=imax;i++)
173  {
174  tmp += log_comb(n,x(i))-x(i)*log(p(i))-(n-x(i))*log(1.-p(i));
175  }
176 
178  return(tmp);
179 }
181 {
182  if(p<=0. && p>=1.0)
183  {
184  cerr<<"The probability p must be greater than 0 or less than 1 in "
185  "dbinom(const dvar_vector& x,const dvar_vector& n,const prevariable& p)\n";
186  return(0.0);
187  }
188 
189  int i;
190  int imin = x.indexmin();
191  int imax = x.indexmax();
192 
194  dvariable tmp = 0.0;
195  for(i=imin; i<=imax;i++)
196  {
197  tmp += log_comb(n(i),x(i))-x(i)*log(p)-(n(i)-x(i))*log(1.-p);
198  }
199 
201  return(tmp);
202 }
203 df1b2variable dbinom(const df1b2variable& x,const double& n,const double& p)
204 {
205  if(p<=0. && p>=1.0)
206  {
207  cerr<<"The probability p must be greater than 0 or less than 1 in "
208  "dbinom(const df1b2variable& x,const double& n,const double& p)\n";
209  return(0.0);
210  }
211 
213  df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
214  //df1b2variable tmp = factln(x)-factln(n-x)-factln(n)-x*log(p)-(n-x)*log(1.-p);
216  return(tmp);
217 }
227 df1b2variable dbinom(const df1b2variable& x,const df1b2variable& n,const double& p)
228 {
229  if(p<=0. && p>=1.0)
230  {
231  cerr<<"The probability p must be greater than 0 or less than 1 in "
232  "dbinom(const df1b2variable& x,const df1b2variable& n,const double& p)\n";
233  return(0.0);
234  }
235 
237  df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
239  return(tmp);
240 }
251 {
252  if(value(p)<=0. && value(p)>=1.0)
253  {
254  cerr<<"The probability p must be greater than 0 or less than 1 in "
255  "dbinom(const df1b2variable& x,const df1b2variable& n,const df1b2variable& p)\n";
256  return(0.0);
257  }
258 
260  df1b2variable tmp = -log_comb(n,x)-x*log(p)-(n-x)*log(1.-p);
262  return(tmp);
263 }
264 
275 {
276  if(min(value(p))<=0. && max(value(p))>=1.0)
277  {
278  cerr<<"The probability p must be greater than 0 or less than 1 in "
279  "dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2vector& p)\n";
280  return(0.0);
281  }
282 
283  int i;
284  int imin = x.indexmin();
285  int imax = x.indexmax();
286 
288  df1b2variable tmp = 0.0;
289  for(i=imin; i<=imax;i++)
290  {
291  tmp += log_comb(n(i),x(i))-x(i)*log(p(i))-(n(i)-x(i))*log(1.-p(i));
292  }
293 
295  return(tmp);
296 }
297 
299 {
300  if(min(value(p))<=0. && max(value(p))>=1.0)
301  {
302  cerr<<"The probability p must be greater than 0 or less than 1 in "
303  "dbinom(const df1b2variable& x,const df1b2vector& n,const df1b2vector& p)\n";
304  return(0.0);
305  }
306 
307  int i;
308  int imin = n.indexmin();
309  int imax = n.indexmax();
310 
312  df1b2variable tmp = 0.0;
313  for(i=imin; i<=imax;i++)
314  {
315  tmp += log_comb(n(i),x)-x*log(p(i))-(n(i)-x)*log(1.-p(i));
316  }
317 
319  return(tmp);
320 }
322 {
323  if(min(value(p))<=0. && max(value(p))>=1.0)
324  {
325  cerr<<"The probability p must be greater than 0 or less than 1 in "
326  "dbinom(const df1b2vector& x,const df1b2variable& n,const df1b2vector& p)\n";
327  return(0.0);
328  }
329 
330  int i;
331  int imin = x.indexmin();
332  int imax = x.indexmax();
333 
335  df1b2variable tmp = 0.0;
336  for(i=imin; i<=imax;i++)
337  {
338  tmp += log_comb(n,x(i))-x(i)*log(p(i))-(n-x(i))*log(1.-p(i));
339  }
340 
342  return(tmp);
343 }
345 {
346  if(value(p)<=0. && value(p)>=1.0)
347  {
348  cerr<<"The probability p must be greater than 0 or less than 1 in "
349  "dbinom(const df1b2vector& x,const df1b2vector& n,const df1b2variable& p)\n";
350  return(0.0);
351  }
352 
353  int i;
354  int imin = x.indexmin();
355  int imax = x.indexmax();
356 
358  df1b2variable tmp = 0.0;
359  for(i=imin; i<=imax;i++)
360  {
361  tmp += log_comb(n(i),x(i))-x(i)*log(p)-(n(i)-x(i))*log(1.-p);
362  }
363 
365  return(tmp);
366 }
Base class for dvariable.
Definition: fvar.hpp:1315
double log_comb(double n, double k)
Log of the binomial coefficent; i.e log of &#39;n choose k&#39;.
Definition: combc.cpp:27
Description not yet available.
void RETURN_ARRAYS_DECREMENT(void)
Decrements gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:507
#define x
Description not yet available.
Definition: df1b2fun.h:953
int indexmin(void) const
Definition: df1b2fun.h:969
ADMB variable vector.
Definition: fvar.hpp:2172
int imax
Definition: fvar.hpp:3640
Description not yet available.
Definition: df1b2fun.h:266
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmin() const
Definition: fvar.hpp:2287
Library of statistic functions.
int indexmax(void) const
Definition: df1b2fun.h:970
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
dvariable dbinom(const prevariable &x, const double &n, const double &p)
Definition: dbinom.cpp:39
#define max(a, b)
Definition: cbivnorm.cpp:189
int indexmax() const
Definition: fvar.hpp:2292
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