CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RandPoissonQ.cc
Go to the documentation of this file.
1//
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- RandPoissonQ ---
7// class implementation file
8// -----------------------------------------------------------------------
9
10// =======================================================================
11// M. Fischler - Implemented new, much faster table-driven algorithm
12// applicable for mu < 100
13// - Implemented "quick()" methods, shich are the same as the
14// new methods for mu < 100 and are a skew-corrected gaussian
15// approximation for large mu.
16// M. Fischler - Removed mean=100 from the table-driven set, since it
17// uses a value just off the end of the table. (April 2004)
18// M Fischler - put and get to/from streams 12/15/04
19// M Fischler - Utilize RandGaussQ rather than RandGauss, as clearly
20// intended by the inclusion of RandGaussQ.h. Using RandGauss
21// introduces a subtle trap in that the state of RandPoissonQ
22// can never be properly captured without also saveing the
23// state of RandGauss! RandGaussQ is, on the other hand,
24// stateless except for the engine used.
25// M Fischler - Modified use of wrong engine when shoot (anEngine, mean)
26// is called. This flaw was preventing any hope of proper
27// saving and restoring in the instance cases.
28// M Fischler - fireArray using defaultMean 2/10/05
29// M Fischler - put/get to/from streams uses pairs of ulongs when
30// + storing doubles avoid problems with precision
31// 4/14/05
32// M Fischler - Modified use of shoot (mean) instead of
33// shoot(getLocalEngine(), mean) when fire(mean) is called.
34// This flaw was causing bad "cross-talk" between modules
35// in CMS, where one used its own engine, and the other
36// used the static generator. 10/18/07
37//
38// =======================================================================
39
40#include "CLHEP/Random/defs.h"
41#include "CLHEP/Random/RandPoissonQ.h"
42#include "CLHEP/Random/RandGaussQ.h"
43#include "CLHEP/Random/DoubConv.h"
44#include "CLHEP/Random/Stat.h"
45#include "CLHEP/Utility/thread_local.h"
46#include <cmath> // for std::pow()
47#include <iostream>
48#include <string>
49#include <vector>
50
51namespace CLHEP {
52
53std::string RandPoissonQ::name() const {return "RandPoissonQ";}
55
56// Initialization of static data: Note that this is all const static data,
57// so that saveEngineStatus properly saves all needed information.
58
59 // The following MUST MATCH the corresponding values used (in
60 // poissonTables.cc) when poissonTables.cdat was created.
61
62const double RandPoissonQ::FIRST_MU = 10;// lowest mu value in table
63const double RandPoissonQ::LAST_MU = 95;// highest mu value
64const double RandPoissonQ::S = 5; // Spacing between mu values
65const int RandPoissonQ::BELOW = 30; // Starting point for N is at mu - BELOW
66const int RandPoissonQ::ENTRIES = 51; // Number of entries in each mu row
67
68const double RandPoissonQ::MAXIMUM_POISSON_DEVIATE = 2.0E9;
69 // Careful -- this is NOT the maximum number that can be held in
70 // a long. It actually should be some large number of sigma below
71 // that.
72
73 // Here comes the big (9K bytes) table, kept in a file of
74 // ENTRIES * (FIRST_MU - LAST_MU + 1)/S doubles
75
76static const double poissonTables [ 51 * ( (95-10)/5 + 1 ) ] = {
77#include "poissonTables.cdat"
78};
79
80//
81// Constructors and destructors:
82//
83
85}
86
87void RandPoissonQ::setupForDefaultMu() {
88
89 // The following are useful for quick approximation, for large mu
90
91 double sig2 = defaultMean * (.9998654 - .08346/defaultMean);
92 sigma = std::sqrt(sig2);
93 // sigma for the Guassian which approximates the Poisson -- naively
94 // sqrt (defaultMean).
95 //
96 // The multiplier corrects for fact that discretization of the form
97 // [gaussian+.5] increases the second moment by a small amount.
98
99 double t = 1./(sig2);
100
101 a2 = t/6 + t*t/324;
102 a1 = std::sqrt (1-2*a2*a2*sig2);
103 a0 = defaultMean + .5 - sig2 * a2;
104
105 // The formula will be a0 + a1*x + a2*x*x where x has 2nd moment of sigma.
106 // The coeffeicients are chosen to match the first THREE moments of the
107 // true Poisson distribution.
108 //
109 // Actually, if the correction for discretization were not needed, then
110 // a2 could be taken one order higher by adding t*t*t/5832. However,
111 // the discretization correction is not perfect, leading to inaccuracy
112 // on the order to 1/mu**2, so adding a third term is overkill.
113
114} // setupForDefaultMu()
115
116
117//
118// fire, quick, operator(), and shoot methods:
119//
120
121long RandPoissonQ::shoot(double xm) {
122 return shoot(getTheEngine(), xm);
123}
124
126 return (double) fire();
127}
128
129double RandPoissonQ::operator()( double mean ) {
130 return (double) fire(mean);
131}
132
133long RandPoissonQ::fire(double mean) {
134 return shoot(getLocalEngine(), mean);
135}
136
138 if ( defaultMean < LAST_MU + S ) {
139 return poissonDeviateSmall ( getLocalEngine(), defaultMean );
140 } else {
141 return poissonDeviateQuick ( getLocalEngine(), a0, a1, a2, sigma );
142 }
143} // fire()
144
145long RandPoissonQ::shoot(HepRandomEngine* anEngine, double mean) {
146
147 // The following variables, static to this method, apply to the
148 // last time a large mean was supplied; they obviate certain calculations
149 // if consecutive calls use the same mean.
150
151 static CLHEP_THREAD_LOCAL double lastLargeMean = -1.; // Mean from previous shoot
152 // requiring poissonDeviateQuick()
153 static CLHEP_THREAD_LOCAL double lastA0;
154 static CLHEP_THREAD_LOCAL double lastA1;
155 static CLHEP_THREAD_LOCAL double lastA2;
156 static CLHEP_THREAD_LOCAL double lastSigma;
157
158 if ( mean < LAST_MU + S ) {
159 return poissonDeviateSmall ( anEngine, mean );
160 } else {
161 if ( mean != lastLargeMean ) {
162 // Compute the coefficients defining the quadratic transformation from a
163 // Gaussian to a Poisson for this mean. Also save these for next time.
164 double sig2 = mean * (.9998654 - .08346/mean);
165 lastSigma = std::sqrt(sig2);
166 double t = 1./sig2;
167 lastA2 = t*(1./6.) + t*t*(1./324.);
168 lastA1 = std::sqrt (1-2*lastA2*lastA2*sig2);
169 lastA0 = mean + .5 - sig2 * lastA2;
170 }
171 return poissonDeviateQuick ( anEngine, lastA0, lastA1, lastA2, lastSigma );
172 }
173
174} // shoot (anEngine, mean)
175
176void RandPoissonQ::shootArray(const int size, long* vect, double m) {
177 for( long* v = vect; v != vect + size; ++v )
178 *v = shoot(m);
179 // Note: We could test for m > 100, and if it is, precompute a0, a1, a2,
180 // and sigma and call the appropriate form of poissonDeviateQuick.
181 // But since those are cached anyway, not much time would be saved.
182}
183
184void RandPoissonQ::fireArray(const int size, long* vect, double m) {
185 for( long* v = vect; v != vect + size; ++v )
186 *v = fire( m );
187}
188
189void RandPoissonQ::fireArray(const int size, long* vect) {
190 for( long* v = vect; v != vect + size; ++v )
191 *v = fire( defaultMean );
192}
193
194
195// Quick Poisson deviate algorithm used by quick for large mu:
196
197long RandPoissonQ::poissonDeviateQuick ( HepRandomEngine *e, double mu ) {
198
199 // Compute the coefficients defining the quadratic transformation from a
200 // Gaussian to a Poisson:
201
202 double sig2 = mu * (.9998654 - .08346/mu);
203 double sig = std::sqrt(sig2);
204 // The multiplier corrects for fact that discretization of the form
205 // [gaussian+.5] increases the second moment by a small amount.
206
207 double t = 1./sig2;
208
209 double sa2 = t*(1./6.) + t*t*(1./324.);
210 double sa1 = std::sqrt (1-2*sa2*sa2*sig2);
211 double sa0 = mu + .5 - sig2 * sa2;
212
213 // The formula will be sa0 + sa1*x + sa2*x*x where x has sigma of sq.
214 // The coeffeicients are chosen to match the first THREE moments of the
215 // true Poisson distribution.
216
217 return poissonDeviateQuick ( e, sa0, sa1, sa2, sig );
218}
219
220
221long RandPoissonQ::poissonDeviateQuick ( HepRandomEngine *e,
222 double A0, double A1, double A2, double sig) {
223//
224// Quick Poisson deviate algorithm used by quick for large mu:
225//
226// The principle: For very large mu, a poisson distribution can be approximated
227// by a gaussian: return the integer part of mu + .5 + g where g is a unit
228// normal. However, this yelds a miserable approximation at values as
229// "large" as 100. The primary problem is that the poisson distribution is
230// supposed to have a skew of 1/mu**2, and the zero skew of the Guassian
231// leads to errors of order as big as 1/mu**2.
232//
233// We substitute for the gaussian a quadratic function of that gaussian random.
234// The expression looks very nearly like mu + .5 - 1/6 + g + g**2/(6*mu).
235// The small positive quadratic term causes the resulting variate to have
236// a positive skew; the -1/6 constant term is there to correct for this bias
237// in the mean. By adjusting these two and the linear term, we can match the
238// first three moments to high accuracy in 1/mu.
239//
240// The sigma used is not precisely sqrt(mu) since a rounded-off Gaussian
241// has a second moment which is slightly larger than that of the Gaussian.
242// To compensate, sig is multiplied by a factor which is slightly less than 1.
243
244 // double g = RandGauss::shootQuick( e ); // TEMPORARY MOD:
245 double g = RandGaussQ::shoot( e ); // Unit normal
246 g *= sig;
247 double p = A2*g*g + A1*g + A0;
248 if ( p < 0 ) return 0; // Shouldn't ever possibly happen since
249 // mean should not be less than 100, but
250 // we check due to paranoia.
252
253 return long(p);
254
255} // poissonDeviateQuick ()
256
257
258
259long RandPoissonQ::poissonDeviateSmall (HepRandomEngine * e, double mean) {
260 long N1;
261 long N2;
262 // The following are for later use to form a secondary random s:
263 double rRange; // This will hold the interval between cdf for the
264 // computed N1 and cdf for N1+1.
265 double rRemainder = 0; // This will hold the length into that interval.
266
267 // Coming in, mean should not be more than LAST_MU + S. However, we will
268 // be paranoid and test for this:
269
270 if ( mean > LAST_MU + S ) {
271 return RandPoisson::shoot(e, mean);
272 }
273
274 if (mean <= 0) {
275 return 0; // Perhaps we ought to balk harder here!
276 }
277
278 // >>> 1 <<<
279 // Generate the first random, which we always will need.
280
281 double r = e->flat();
282
283 // >>> 2 <<<
284 // For small mean, below the start of the tables,
285 // do the series for cdf directly.
286
287 // In this case, since we know the series will terminate relatively quickly,
288 // almost alwaye use a precomputed 1/N array without fear of overrunning it.
289
290 static const double oneOverN[50] =
291 { 0, 1., 1/2., 1/3., 1/4., 1/5., 1/6., 1/7., 1/8., 1/9.,
292 1/10., 1/11., 1/12., 1/13., 1/14., 1/15., 1/16., 1/17., 1/18., 1/19.,
293 1/20., 1/21., 1/22., 1/23., 1/24., 1/25., 1/26., 1/27., 1/28., 1/29.,
294 1/30., 1/31., 1/32., 1/33., 1/34., 1/35., 1/36., 1/37., 1/38., 1/39.,
295 1/40., 1/41., 1/42., 1/43., 1/44., 1/45., 1/46., 1/47., 1/48., 1/49. };
296
297
298 if ( mean < FIRST_MU ) {
299
300 long N = 0;
301 double term = std::exp(-mean);
302 double cdf = term;
303
304 if ( r < (1 - 1.0E-9) ) {
305 //
306 // **** This is a normal path: ****
307 //
308 // Except when r is very close to 1, it is certain that we will exceed r
309 // before the 30-th term in the series, so a simple while loop is OK.
310 const double* oneOverNptr = oneOverN;
311 while( cdf <= r ) {
312 N++ ;
313 oneOverNptr++;
314 term *= ( mean * (*oneOverNptr) );
315 cdf += term;
316 }
317 return N;
318 //
319 // **** ****
320 //
321 } else { // r is almost 1...
322 // For r very near to 1 we would have to check that we don't fall
323 // off the end of the table of 1/N. Since this is very rare, we just
324 // ignore the table and do the identical while loop, using explicit
325 // division.
326 double cdf0;
327 while ( cdf <= r ) {
328 N++ ;
329 term *= ( mean / N );
330 cdf0 = cdf;
331 cdf += term;
332 if (cdf == cdf0) break; // Can't happen, but just in case...
333 }
334 return N;
335 } // end of if ( r compared to (1 - 1.0E-9) )
336
337 } // End of the code for mean < FIRST_MU
338
339 // >>> 3 <<<
340 // Find the row of the tables corresponding to the highest tabulated mu
341 // which is no greater than our actual mean.
342
343 int rowNumber = int((mean - FIRST_MU)/S);
344 const double * cdfs = &poissonTables [rowNumber*ENTRIES];
345 double mu = FIRST_MU + rowNumber*S;
346 double deltaMu = mean - mu;
347 int Nmin = int(mu - BELOW);
348 if (Nmin < 1) Nmin = 1;
349 int Nmax = Nmin + (ENTRIES - 1);
350
351
352 // >>> 4 <<<
353 // If r is less that the smallest entry in the row, then
354 // generate the deviate directly from the series.
355
356 if ( r < cdfs[0] ) {
357
358 // In this case, we are tempted to use the actual mean, and not
359 // generate a second deviate to account for the leftover part mean - mu.
360 // That would be an error, generating a distribution with enough excess
361 // at Nmin + (mean-mu)/2 to be detectable in 4,000,000 trials.
362
363 // Since this case is very rare (never more than .2% of the r values)
364 // and can happen where N will be large (up to 65 for the mu=95 row)
365 // we use explicit division so as to avoid having to worry about running
366 // out of oneOverN table.
367
368 long N = 0;
369 double term = std::exp(-mu);
370 double cdf = term;
371 double cdf0;
372
373 while(cdf <= r) {
374 N++ ;
375 term *= ( mu / N );
376 cdf0 = cdf;
377 cdf += term;
378 if (cdf == cdf0) break; // Can't happen, but just in case...
379 }
380 N1 = N;
381 // std::cout << r << " " << N << " ";
382 // DBG_small = true;
383 rRange = 0; // In this case there is always a second r needed
384
385 } // end of small-r case
386
387
388 // >>> 5 <<<
389 // Assuming r lies within the scope of the row for this mu, find the
390 // largest entry not greater than r. N1 is the N corresponding to the
391 // index a.
392
393 else if ( r < cdfs[ENTRIES-1] ) { // r is also >= cdfs[0]
394
395 //
396 // **** This is the normal code path ****
397 //
398
399 int a = 0; // Highest value of index such that cdfs[a]
400 // is known NOT to be greater than r.
401 int b = ENTRIES - 1; // Lowest value of index such that cdfs[b] is
402 // known to exeed r.
403
404 while (b != (a+1) ) {
405 int c = (a+b+1)>>1;
406 if (r > cdfs[c]) {
407 a = c;
408 } else {
409 b = c;
410 }
411 }
412
413 N1 = Nmin + a;
414 rRange = cdfs[a+1] - cdfs[a];
415 rRemainder = r - cdfs[a];
416
417 //
418 // **** ****
419 //
420
421 } // end of medium-r (normal) case
422
423
424 // >>> 6 <<<
425 // If r exceeds the greatest entry in the table for this mu, then start
426 // from that cdf, and use the series to compute from there until r is
427 // exceeded.
428
429 else { // if ( r >= cdfs[ENTRIES-1] ) {
430
431 // Here, division must be done explicitly, and we must also protect against
432 // roundoff preventing termination.
433
434 //
435 //+++ cdfs[ENTRIES-1] is exp(-mu) sum (mu**m/m! , m=0 to Nmax)
436 //+++ (where Nmax = mu - BELOW + ENTRIES - 1)
437 //+++ cdfs[ENTRIES-1]-cdfs[ENTRIES-2] is exp(-mu) mu**(Nmax)/(Nmax)!
438 //+++ If the sum up to k-1 <= r < sum up to k, then N = k-1
439 //+++ Consider k = Nmax in the above statement:
440 //+++ If cdfs[ENTRIES-2] <= r < cdfs[ENTRIES-1], N would be Nmax-1
441 //+++ But here r >= cdfs[ENTRIES-1] so N >= Nmax
442 //
443
444 // Erroneous:
445 //+++ cdfs[ENTRIES-1] is exp(-mu) sum (mu**m/m! , m=0 to Nmax-1)
446 //+++ cdfs[ENTRIES-1]-cdfs[ENTRIES-2] is exp(-mu) mu**(Nmax-1)/(Nmax-1)!
447 //+++ If a sum up to k-1 <= r < sum up to k, then N = k-1
448 //+++ So if cdfs[ENTRIES-1] were > r, N would be Nmax-1 (or less)
449 //+++ But here r >= cdfs[ENTRIES-1] so N >= Nmax
450 //
451
452 // std::cout << "r = " << r << " mu = " << mu << "\n";
453 long N = Nmax -1;
454 double cdf = cdfs[ENTRIES-1];
455 double term = cdf - cdfs[ENTRIES-2];
456 double cdf0;
457 while(cdf <= r) {
458 N++ ;
459 // std::cout << " N " << N << " term " <<
460 // term << " cdf " << cdf << "\n";
461 term *= ( mu / N );
462 cdf0 = cdf;
463 cdf += term;
464 if (cdf == cdf0) break; // If term gets so small cdf stops increasing,
465 // terminate using that value of N since we
466 // would never reach r.
467 }
468 N1 = N;
469 rRange = 0; // We can't validly omit the second true random
470
471 // N = Nmax -1;
472 // cdf = cdfs[ENTRIES-1];
473 // term = cdf - cdfs[ENTRIES-2];
474 // for (int isxz=0; isxz < 100; isxz++) {
475 // N++ ;
476 // term *= ( mu / N );
477 // cdf0 = cdf;
478 // cdf += term;
479 // }
480 // std::cout.precision(20);
481 // std::cout << "Final sum is " << cdf << "\n";
482
483 } // end of large-r case
484
485
486
487 // >>> 7 <<<
488 // Form a second random, s, based on the position of r within the range
489 // of this table entry to the next entry.
490
491 // However, if this range is very small, then we lose too many bits of
492 // randomness. In that situation, we generate a second random for s.
493
494 double s;
495
496 static const double MINRANGE = .01; // Sacrifice up to two digits of
497 // randomness when using r to produce
498 // a second random s. Leads to up to
499 // .09 extra randoms each time.
500
501 if ( rRange > MINRANGE ) {
502 //
503 // **** This path taken 90% of the time ****
504 //
505 s = rRemainder / rRange;
506 } else {
507 s = e->flat(); // extra true random needed about one time in 10.
508 }
509
510 // >>> 8 <<<
511 // Use the direct summation method to form a second poisson deviate N2
512 // from deltaMu and s.
513
514 N2 = 0;
515 double term = std::exp(-deltaMu);
516 double cdf = term;
517
518 if ( s < (1 - 1.0E-10) ) {
519 //
520 // This is the normal path:
521 //
522 const double* oneOverNptr = oneOverN;
523 while( cdf <= s ) {
524 N2++ ;
525 oneOverNptr++;
526 term *= ( deltaMu * (*oneOverNptr) );
527 cdf += term;
528 }
529 } else { // s is almost 1...
530 while( cdf <= s ) {
531 N2++ ;
532 term *= ( deltaMu / N2 );
533 cdf += term;
534 }
535 } // end of if ( s compared to (1 - 1.0E-10) )
536
537 // >>> 9 <<<
538 // The result is the sum of those two deviates
539
540 // if (DBG_small) {
541 // std::cout << N2 << " " << N1+N2 << "\n";
542 // DBG_small = false;
543 // }
544
545 return N1 + N2;
546
547} // poissonDeviate()
548
549std::ostream & RandPoissonQ::put ( std::ostream & os ) const {
550 long pr=os.precision(20);
551 std::vector<unsigned long> t(2);
552 os << " " << name() << "\n";
553 os << "Uvec" << "\n";
554 t = DoubConv::dto2longs(a0);
555 os << a0 << " " << t[0] << " " << t[1] << "\n";
556 t = DoubConv::dto2longs(a1);
557 os << a1 << " " << t[0] << " " << t[1] << "\n";
558 t = DoubConv::dto2longs(a2);
559 os << a2 << " " << t[0] << " " << t[1] << "\n";
560 t = DoubConv::dto2longs(sigma);
561 os << sigma << " " << t[0] << " " << t[1] << "\n";
563 os.precision(pr);
564 return os;
565#ifdef REMOVED
566 int pr=os.precision(20);
567 os << " " << name() << "\n";
568 os << a0 << " " << a1 << " " << a2 << "\n";
569 os << sigma << "\n";
571 os.precision(pr);
572 return os;
573#endif
574}
575
576std::istream & RandPoissonQ::get ( std::istream & is ) {
577 std::string inName;
578 is >> inName;
579 if (inName != name()) {
580 is.clear(std::ios::badbit | is.rdstate());
581 std::cerr << "Mismatch when expecting to read state of a "
582 << name() << " distribution\n"
583 << "Name found was " << inName
584 << "\nistream is left in the badbit state\n";
585 return is;
586 }
587 if (possibleKeywordInput(is, "Uvec", a0)) {
588 std::vector<unsigned long> t(2);
589 is >> a0 >> t[0] >> t[1]; a0 = DoubConv::longs2double(t);
590 is >> a1 >> t[0] >> t[1]; a1 = DoubConv::longs2double(t);
591 is >> a2 >> t[0] >> t[1]; a2 = DoubConv::longs2double(t);
592 is >> sigma >> t[0] >> t[1]; sigma = DoubConv::longs2double(t);
594 return is;
595 }
596 // is >> a0 encompassed by possibleKeywordInput
597 is >> a1 >> a2 >> sigma;
599 return is;
600}
601
602} // namespace CLHEP
603
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
static HepRandomEngine * getTheEngine()
Definition: Random.cc:270
static double shoot()
static void shootArray(const int size, long *vect, double mean=1.0)
std::ostream & put(std::ostream &os) const
void fireArray(const int size, long *vect)
std::istream & get(std::istream &is)
HepRandomEngine & engine()
Definition: RandPoissonQ.cc:54
std::string name() const
Definition: RandPoissonQ.cc:53
static const double MAXIMUM_POISSON_DEVIATE
Definition: RandPoissonQ.h:110
virtual ~RandPoissonQ()
Definition: RandPoissonQ.cc:84
static long shoot(double mean=1.0)
std::ostream & put(std::ostream &os) const
Definition: RandPoisson.cc:285
HepRandomEngine * getLocalEngine()
static long shoot(double mean=1.0)
Definition: RandPoisson.cc:95
std::istream & get(std::istream &is)
Definition: RandPoisson.cc:314
HepRandomEngine & engine()
Definition: RandPoisson.cc:40
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168
int g(shared_ptr< X >)