ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dranpois.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2009-2012 ADMB Foundation
6  */
11 #include "fvar.hpp"
12 /*
13 #define IM1 2147483563
14 #define IM2 2147483399
15 #define AM (1.0/IM1)
16 #define IMM1 (IM1-1)
17 #define IA1 40014
18 #define IA2 40692
19 #define IQ1 53668
20 #define IQ2 52774
21 #define IR1 12211
22 #define IR2 3791
23 #define NTAB 32
24 #define NDIV (1+IMM1/NTAB)
25 #define EPS 1.2e-7
26 #define RNMX (1.0-EPS)
27 */
28 
37 double randpoisson(double xm, const random_number_generator& rng)
38 {
39  double gammln(double xx);
40  static double sq,alxm,g,oldm=(-1.0);
41 
42  double em,t,y;
43 
44  if (xm < 12.0) {
45  if (xm != oldm) {
46  oldm=xm;
47  g=exp(-xm);
48  }
49  em = -1;
50  t=1.0;
51  do {
52  ++em;
53  //t *= ran1(idum);
54  t*= y=((random_number_generator&) rng).better_rand();
55  } while (t > g);
56  } else {
57  if (xm != oldm) {
58  oldm=xm;
59  sq=sqrt(2.0*xm);
60  alxm=log(xm);
61  g=xm*alxm-gammln(xm+1.0);
62  }
63  do {
64  do {
65  //y=tan(PI*ran1(idum));
67  em=sq*y+xm;
68  } while (em < 0.0);
69  em=floor(em);
70  t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
71  } while (((random_number_generator&) rng).better_rand() > t);
72  //} while (ran1(idum) > t);
73  }
74  return em;
75 }
76 #undef PI
77 
85 void dvector::fill_randpoisson(double lambda,
86  const random_number_generator& rng)
87  {
88  for (int i=indexmin(); i<=indexmax(); i++)
89  {
90  elem(i)=randpoisson(lambda,rng);
91  }
92  }
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
double & elem(int i)
Definition: dvector.h:152
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void fill_randpoisson(double lambda, const random_number_generator &rng)
Fill vector with Poisson pseudorandom numbers.
Definition: dranpois.cpp:85
Description not yet available.
Definition: fvar.hpp:7951
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
double randpoisson(double xm, const random_number_generator &rng)
Poisson random deviates.
Definition: dranpois.cpp:37
double better_rand(long int &idum)
Description not yet available.
Definition: bet_rand.cpp:18
#define PI
Definition: fvar.hpp:95
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13