Geant4  10.02.p03
CLHEP::RandBinomial Class Reference

#include <RandBinomial.h>

Inheritance diagram for CLHEP::RandBinomial:
Collaboration diagram for CLHEP::RandBinomial:

Public Member Functions

 RandBinomial (HepRandomEngine &anEngine, long n=1, double p=0.5)
 
 RandBinomial (HepRandomEngine *anEngine, long n=1, double p=0.5)
 
virtual ~RandBinomial ()
 
double fire ()
 
double fire (long n, double p)
 
void fireArray (const int size, double *vect)
 
void fireArray (const int size, double *vect, long n, double p)
 
double operator() ()
 
double operator() (long n, double p)
 
std::ostream & put (std::ostream &os) const
 
std::istream & get (std::istream &is)
 
std::string name () const
 
HepRandomEngineengine ()
 
- Public Member Functions inherited from CLHEP::HepRandom
 HepRandom ()
 
 HepRandom (long seed)
 
 HepRandom (HepRandomEngine &algorithm)
 
 HepRandom (HepRandomEngine *algorithm)
 
virtual ~HepRandom ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
double flat (HepRandomEngine *theNewEngine)
 
void flatArray (HepRandomEngine *theNewEngine, const int size, double *vect)
 

Static Public Member Functions

static double shoot ()
 
static double shoot (long n, double p)
 
static void shootArray (const int size, double *vect, long n=1, double p=0.5)
 
static double shoot (HepRandomEngine *anEngine)
 
static double shoot (HepRandomEngine *anEngine, long n, double p)
 
static void shootArray (HepRandomEngine *anEngine, const int size, double *vect, long n=1, double p=0.5)
 
static std::string distributionName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandom
static void setTheSeed (long seed, int lux=3)
 
static long getTheSeed ()
 
static void setTheSeeds (const long *seeds, int aux=-1)
 
static const long * getTheSeeds ()
 
static void getTheTableSeeds (long *seeds, int index)
 
static HepRandomgetTheGenerator ()
 
static void setTheEngine (HepRandomEngine *theNewEngine)
 
static HepRandomEnginegetTheEngine ()
 
static void saveEngineStatus (const char filename[]="Config.conf")
 
static void restoreEngineStatus (const char filename[]="Config.conf")
 
static std::ostream & saveFullState (std::ostream &os)
 
static std::istream & restoreFullState (std::istream &is)
 
static std::ostream & saveDistState (std::ostream &os)
 
static std::istream & restoreDistState (std::istream &is)
 
static std::ostream & saveStaticRandomStates (std::ostream &os)
 
static std::istream & restoreStaticRandomStates (std::istream &is)
 
static void showEngineStatus ()
 
static int createInstance ()
 
static std::string distributionName ()
 

Static Private Member Functions

static double genBinomial (HepRandomEngine *anEngine, long n, double p)
 

Private Attributes

std::shared_ptr< HepRandomEnginelocalEngine
 
long defaultN
 
double defaultP
 

Additional Inherited Members

- Static Protected Attributes inherited from CLHEP::HepRandom
static const long seedTable [215][2]
 

Detailed Description

Author

Definition at line 37 of file RandBinomial.h.

Constructor & Destructor Documentation

◆ RandBinomial() [1/2]

CLHEP::RandBinomial::RandBinomial ( HepRandomEngine anEngine,
long  n = 1,
double  p = 0.5 
)
inline

◆ RandBinomial() [2/2]

CLHEP::RandBinomial::RandBinomial ( HepRandomEngine anEngine,
long  n = 1,
double  p = 0.5 
)
inline

◆ ~RandBinomial()

CLHEP::RandBinomial::~RandBinomial ( )
virtual

Definition at line 30 of file RandBinomial.cc.

30  {
31 }

Member Function Documentation

◆ distributionName()

static std::string CLHEP::RandBinomial::distributionName ( )
inlinestatic

Definition at line 99 of file RandBinomial.h.

99 {return "RandBinomial";}
Here is the call graph for this function:

◆ engine()

HepRandomEngine & CLHEP::RandBinomial::engine ( )
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 28 of file RandBinomial.cc.

28 {return *localEngine;}
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandBinomial.h:106

◆ fire() [1/2]

double CLHEP::RandBinomial::fire ( )
inline
Here is the caller graph for this function:

◆ fire() [2/2]

double CLHEP::RandBinomial::fire ( long  n,
double  p 
)

Definition at line 43 of file RandBinomial.cc.

43  {
44  return genBinomial( localEngine.get(), n, p );
45 }
Char_t n[5]
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandBinomial.h:106
static double genBinomial(HepRandomEngine *anEngine, long n, double p)
Here is the call graph for this function:

◆ fireArray() [1/2]

void CLHEP::RandBinomial::fireArray ( const int  size,
double *  vect 
)

Definition at line 62 of file RandBinomial.cc.

63 {
64  for( double* v = vect; v != vect+size; ++v )
65  *v = fire(defaultN,defaultP);
66 }
Here is the call graph for this function:

◆ fireArray() [2/2]

void CLHEP::RandBinomial::fireArray ( const int  size,
double *  vect,
long  n,
double  p 
)

Definition at line 68 of file RandBinomial.cc.

70 {
71  for( double* v = vect; v != vect+size; ++v )
72  *v = fire(n,p);
73 }
Char_t n[5]
Here is the call graph for this function:

◆ genBinomial()

double CLHEP::RandBinomial::genBinomial ( HepRandomEngine anEngine,
long  n,
double  p 
)
staticprivate

Definition at line 126 of file RandBinomial.cc.

127 {
128 /******************************************************************
129  * *
130  * Binomial-Distribution - Acceptance Rejection/Inversion *
131  * *
132  ******************************************************************
133  * *
134  * Acceptance Rejection method combined with Inversion for *
135  * generating Binomial random numbers with parameters *
136  * n (number of trials) and p (probability of success). *
137  * For min(n*p,n*(1-p)) < 10 the Inversion method is applied: *
138  * The random numbers are generated via sequential search, *
139  * starting at the lowest index k=0. The cumulative probabilities *
140  * are avoided by using the technique of chop-down. *
141  * For min(n*p,n*(1-p)) >= 10 Acceptance Rejection is used: *
142  * The algorithm is based on a hat-function which is uniform in *
143  * the centre region and exponential in the tails. *
144  * A triangular immediate acceptance region in the centre speeds *
145  * up the generation of binomial variates. *
146  * If candidate k is near the mode, f(k) is computed recursively *
147  * starting at the mode m. *
148  * The acceptance test by Stirling's formula is modified *
149  * according to W. Hoermann (1992): The generation of binomial *
150  * random variates, to appear in J. Statist. Comput. Simul. *
151  * If p < .5 the algorithm is applied to parameters n, p. *
152  * Otherwise p is replaced by 1-p, and k is replaced by n - k. *
153  * *
154  ******************************************************************
155  * *
156  * FUNCTION: - btpec samples a random number from the binomial *
157  * distribution with parameters n and p and is *
158  * valid for n*min(p,1-p) > 0. *
159  * REFERENCE: - V. Kachitvichyanukul, B.W. Schmeiser (1988): *
160  * Binomial random variate generation, *
161  * Communications of the ACM 31, 216-222. *
162  * SUBPROGRAMS: - StirlingCorrection() *
163  * ... Correction term of the Stirling *
164  * approximation for log(k!) *
165  * (series in 1/k or table values *
166  * for small k) with long int k *
167  * - anEngine ... Pointer to a (0,1)-Uniform *
168  * engine *
169  * *
170  * Implemented by H. Zechner and P. Busswald, September 1992 *
171  ******************************************************************/
172 
173 #define C1_3 0.33333333333333333
174 #define C5_8 0.62500000000000000
175 #define C1_6 0.16666666666666667
176 #define DMAX_KM 20L
177 
178  static CLHEP_THREAD_LOCAL long int n_last = -1L, n_prev = -1L;
179  static CLHEP_THREAD_LOCAL double par,np,p0,q,p_last = -1.0, p_prev = -1.0;
180  static CLHEP_THREAD_LOCAL long b,m,nm;
181  static CLHEP_THREAD_LOCAL double pq, rc, ss, xm, xl, xr, ll, lr, c,
182  p1, p2, p3, p4, ch;
183 
184  long bh,i, K, Km, nK;
185  double f, rm, U, V, X, T, E;
186 
187  if (n != n_last || p != p_last) // set-up
188  {
189  n_last = n;
190  p_last = p;
191  par=std::min(p,1.0-p);
192  q=1.0-par;
193  np = n*par;
194 
195 // Check for invalid input values
196 
197  if( np <= 0.0 ) return (-1.0);
198 
199  rm = np + par;
200  m = (long int) rm; // mode, integer
201  if (np<10)
202  {
203  p0=std::exp(n*std::log(q)); // Chop-down
204  bh=(long int)(np+10.0*std::sqrt(np*q));
205  b=std::min(n,bh);
206  }
207  else
208  {
209  rc = (n + 1.0) * (pq = par / q); // recurr. relat.
210  ss = np * q; // variance
211  i = (long int) (2.195*std::sqrt(ss) - 4.6*q); // i = p1 - 0.5
212  xm = m + 0.5;
213  xl = (double) (m - i); // limit left
214  xr = (double) (m + i + 1L); // limit right
215  f = (rm - xl) / (rm - xl*par); ll = f * (1.0 + 0.5*f);
216  f = (xr - rm) / (xr * q); lr = f * (1.0 + 0.5*f);
217  c = 0.134 + 20.5/(15.3 + (double) m); // parallelogram
218  // height
219  p1 = i + 0.5;
220  p2 = p1 * (1.0 + c + c); // probabilities
221  p3 = p2 + c/ll; // of regions 1-4
222  p4 = p3 + c/lr;
223  }
224  }
225  if( np <= 0.0 ) return (-1.0);
226  if (np<10) //Inversion Chop-down
227  {
228  double pk;
229 
230  K=0;
231  pk=p0;
232  U=anEngine->flat();
233  while (U>pk)
234  {
235  ++K;
236  if (K>b)
237  {
238  U=anEngine->flat();
239  K=0;
240  pk=p0;
241  }
242  else
243  {
244  U-=pk;
245  pk=(double)(((n-K+1)*par*pk)/(K*q));
246  }
247  }
248  return ((p>0.5) ? (double)(n-K):(double)K);
249  }
250 
251  for (;;)
252  {
253  V = anEngine->flat();
254  if ((U = anEngine->flat() * p4) <= p1) // triangular region
255  {
256  K=(long int) (xm - U + p1*V);
257  return ((p>0.5) ? (double)(n-K):(double)K); // immediate accept
258  }
259  if (U <= p2) // parallelogram
260  {
261  X = xl + (U - p1)/c;
262  if ((V = V*c + 1.0 - std::fabs(xm - X)/p1) >= 1.0) continue;
263  K = (long int) X;
264  }
265  else if (U <= p3) // left tail
266  {
267  if ((X = xl + std::log(V)/ll) < 0.0) continue;
268  K = (long int) X;
269  V *= (U - p2) * ll;
270  }
271  else // right tail
272  {
273  if ((K = (long int) (xr - std::log(V)/lr)) > n) continue;
274  V *= (U - p3) * lr;
275  }
276 
277  // acceptance test : two cases, depending on |K - m|
278  if ((Km = std::labs(K - m)) <= DMAX_KM || Km + Km + 2L >= ss)
279  {
280 
281  // computation of p(K) via recurrence relationship from the mode
282  f = 1.0; // f(m)
283  if (m < K)
284  {
285  for (i = m; i < K; )
286  {
287  if ((f *= (rc / ++i - pq)) < V) break; // multiply f
288  }
289  }
290  else
291  {
292  for (i = K; i < m; )
293  {
294  if ((V *= (rc / ++i - pq)) > f) break; // multiply V
295  }
296  }
297  if (V <= f) break; // acceptance test
298  }
299  else
300  {
301 
302  // lower and upper squeeze tests, based on lower bounds for log p(K)
303  V = std::log(V);
304  T = - Km * Km / (ss + ss);
305  E = (Km / ss) * ((Km * (Km * C1_3 + C5_8) + C1_6) / ss + 0.5);
306  if (V <= T - E) break;
307  if (V <= T + E)
308  {
309  if (n != n_prev || par != p_prev)
310  {
311  n_prev = n;
312  p_prev = par;
313 
314  nm = n - m + 1L;
315  ch = xm * std::log((m + 1.0)/(pq * nm)) +
317  }
318  nK = n - K + 1L;
319 
320  // computation of log f(K) via Stirling's formula
321  // final acceptance-rejection test
322  if (V <= ch + (n + 1.0)*std::log((double) nm / (double) nK) +
323  (K + 0.5)*std::log(nK * pq / (K + 1.0)) -
324  StirlingCorrection(K + 1L) - StirlingCorrection(nK)) break;
325  }
326  }
327  }
328  return ((p>0.5) ? (double)(n-K):(double)K);
329 }
static const double m
static const double nm
Definition: SystemOfUnits.h:91
#define C1_3
static double StirlingCorrection(long int k)
Definition: RandBinomial.cc:93
Float_t X
Char_t n[5]
static const double L
#define CLHEP_THREAD_LOCAL
Definition: thread_local.h:25
#define DMAX_KM
#define C5_8
#define C1_6
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

std::istream & CLHEP::RandBinomial::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 342 of file RandBinomial.cc.

342  {
343  std::string inName;
344  is >> inName;
345  if (inName != name()) {
346  is.clear(std::ios::badbit | is.rdstate());
347  std::cerr << "Mismatch when expecting to read state of a "
348  << name() << " distribution\n"
349  << "Name found was " << inName
350  << "\nistream is left in the badbit state\n";
351  return is;
352  }
353  if (possibleKeywordInput(is, "Uvec", defaultN)) {
354  std::vector<unsigned long> t(2);
355  is >> defaultN >> defaultP;
356  is >> t[0] >> t[1]; defaultP = DoubConv::longs2double(t);
357  return is;
358  }
359  // is >> defaultN encompassed by possibleKeywordInput
360  is >> defaultP;
361  return is;
362 }
std::string name() const
Definition: RandBinomial.cc:27
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114
Here is the call graph for this function:

◆ name()

std::string CLHEP::RandBinomial::name ( ) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 27 of file RandBinomial.cc.

27 {return "RandBinomial";}
Here is the caller graph for this function:

◆ operator()() [1/2]

double CLHEP::RandBinomial::operator() ( )
inlinevirtual

Reimplemented from CLHEP::HepRandom.

◆ operator()() [2/2]

double CLHEP::RandBinomial::operator() ( long  n,
double  p 
)
inline

◆ put()

std::ostream & CLHEP::RandBinomial::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 331 of file RandBinomial.cc.

331  {
332  int pr=os.precision(20);
333  std::vector<unsigned long> t(2);
334  os << " " << name() << "\n";
335  os << "Uvec" << "\n";
337  os << defaultN << " " << defaultP << " " << t[0] << " " << t[1] << "\n";
338  os.precision(pr);
339  return os;
340 }
std::string name() const
Definition: RandBinomial.cc:27
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
Here is the call graph for this function:

◆ shoot() [1/4]

static double CLHEP::RandBinomial::shoot ( )
inlinestatic
Here is the caller graph for this function:

◆ shoot() [2/4]

double CLHEP::RandBinomial::shoot ( long  n,
double  p 
)
static

Definition at line 38 of file RandBinomial.cc.

38  {
39  HepRandomEngine *anEngine = HepRandom::getTheEngine();
40  return genBinomial( anEngine, n, p );
41 }
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
Char_t n[5]
static double genBinomial(HepRandomEngine *anEngine, long n, double p)
Here is the call graph for this function:

◆ shoot() [3/4]

static double CLHEP::RandBinomial::shoot ( HepRandomEngine anEngine)
inlinestatic

◆ shoot() [4/4]

double CLHEP::RandBinomial::shoot ( HepRandomEngine anEngine,
long  n,
double  p 
)
static

Definition at line 33 of file RandBinomial.cc.

34  {
35  return genBinomial( anEngine, n, p );
36 }
Char_t n[5]
static double genBinomial(HepRandomEngine *anEngine, long n, double p)
Here is the call graph for this function:

◆ shootArray() [1/2]

void CLHEP::RandBinomial::shootArray ( const int  size,
double *  vect,
long  n = 1,
double  p = 0.5 
)
static

Definition at line 47 of file RandBinomial.cc.

49 {
50  for( double* v = vect; v != vect+size; ++v )
51  *v = shoot(n,p);
52 }
static double shoot()
Char_t n[5]
Here is the call graph for this function:

◆ shootArray() [2/2]

void CLHEP::RandBinomial::shootArray ( HepRandomEngine anEngine,
const int  size,
double *  vect,
long  n = 1,
double  p = 0.5 
)
static

Definition at line 54 of file RandBinomial.cc.

57 {
58  for( double* v = vect; v != vect+size; ++v )
59  *v = shoot(anEngine,n,p);
60 }
static double shoot()
Char_t n[5]
Here is the call graph for this function:

Member Data Documentation

◆ defaultN

long CLHEP::RandBinomial::defaultN
private

Definition at line 107 of file RandBinomial.h.

◆ defaultP

double CLHEP::RandBinomial::defaultP
private

Definition at line 108 of file RandBinomial.h.

◆ localEngine

std::shared_ptr<HepRandomEngine> CLHEP::RandBinomial::localEngine
private

Definition at line 106 of file RandBinomial.h.


The documentation for this class was generated from the following files: