19#include "CLHEP/Random/RandBinomial.h"
20#include "CLHEP/Random/defs.h"
21#include "CLHEP/Random/DoubConv.h"
22#include "CLHEP/Utility/thread_local.h"
38 return genBinomial( anEngine, n, p );
43 return genBinomial( anEngine, n, p );
47 return genBinomial( localEngine.get(), n, p );
53 for(
double* v = vect; v != vect+size; ++v )
58 const int size,
double* vect,
61 for(
double* v = vect; v != vect+size; ++v )
62 *v =
shoot(anEngine,n,p);
67 for(
double* v = vect; v != vect+size; ++v )
68 *v =
fire(defaultN,defaultP);
74 for(
double* v = vect; v != vect+size; ++v )
96static double StirlingCorrection(
long int k)
98 #define C1 8.33333333333333333e-02
99 #define C3 -2.77777777777777778e-03
100 #define C5 7.93650793650793651e-04
101 #define C7 -5.95238095238095238e-04
103 static const double c[31] = { 0.0,
104 8.106146679532726e-02, 4.134069595540929e-02,
105 2.767792568499834e-02, 2.079067210376509e-02,
106 1.664469118982119e-02, 1.387612882307075e-02,
107 1.189670994589177e-02, 1.041126526197209e-02,
108 9.255462182712733e-03, 8.330563433362871e-03,
109 7.573675487951841e-03, 6.942840107209530e-03,
110 6.408994188004207e-03, 5.951370112758848e-03,
111 5.554733551962801e-03, 5.207655919609640e-03,
112 4.901395948434738e-03, 4.629153749334029e-03,
113 4.385560249232324e-03, 4.166319691996922e-03,
114 3.967954218640860e-03, 3.787618068444430e-03,
115 3.622960224683090e-03, 3.472021382978770e-03,
116 3.333155636728090e-03, 3.204970228055040e-03,
117 3.086278682608780e-03, 2.976063983550410e-03,
118 2.873449362352470e-03, 2.777674929752690e-03,
123 r = 1.0 / (
double) k; rr = r * r;
124 return(r*(
C1 + rr*(
C3 + rr*(
C5 + rr*
C7))));
129double RandBinomial::genBinomial( HepRandomEngine *anEngine,
long n,
double p )
176#define C1_3 0.33333333333333333
177#define C5_8 0.62500000000000000
178#define C1_6 0.16666666666666667
181 static CLHEP_THREAD_LOCAL
long int n_last = -1L, n_prev = -1L;
182 static CLHEP_THREAD_LOCAL
double par,np,p0,q,p_last = -1.0, p_prev = -1.0;
183 static CLHEP_THREAD_LOCAL
long b,m,nm;
184 static CLHEP_THREAD_LOCAL
double pq, rc, ss, xm, xl, xr, ll, lr, c,
187 long bh,i, K, Km, nK;
188 double f, rm, U, V,
X, T, E;
190 if (n != n_last || p != p_last)
194 par=std::min(p,1.0-p);
200 if( np <= 0.0 )
return (-1.0);
206 p0=std::exp(n*std::log(q));
207 bh=(
long int)(np+10.0*std::sqrt(np*q));
212 rc = (
n + 1.0) * (pq = par / q);
214 i = (
long int) (2.195*std::sqrt(ss) - 4.6*q);
217 xr = (
double) (m + i + 1L);
218 f = (rm - xl) / (rm - xl*par); ll =
f * (1.0 + 0.5*
f);
219 f = (xr - rm) / (xr * q); lr =
f * (1.0 + 0.5*
f);
220 c = 0.134 + 20.5/(15.3 + (
double) m);
223 p2 = p1 * (1.0 + c + c);
228 if( np <= 0.0 )
return (-1.0);
248 pk=(
double)(((n-K+1)*par*pk)/(K*q));
256 V = anEngine->flat();
257 if ((U = anEngine->flat() * p4) <= p1)
259 K=(
long int) (xm - U + p1*V);
265 if ((V = V*c + 1.0 - std::fabs(xm -
X)/p1) >= 1.0)
continue;
270 if ((
X = xl + std::log(V)/ll) < 0.0)
continue;
276 if ((K = (
long int) (xr - std::log(V)/lr)) >
n)
continue;
281 if ((Km = std::labs(K - m)) <=
DMAX_KM || Km + Km + 2L >= ss)
290 if ((
f *= (rc / ++i - pq)) < V)
break;
297 if ((V *= (rc / ++i - pq)) >
f)
break;
307 T = - Km * Km / (ss + ss);
308 E = (Km / ss) * ((Km * (Km *
C1_3 +
C5_8) +
C1_6) / ss + 0.5);
309 if (V <= T - E)
break;
312 if (n != n_prev || par != p_prev)
318 ch = xm * std::log((m + 1.0)/(pq * nm)) +
319 StirlingCorrection(m + 1L) + StirlingCorrection(nm);
325 if (V <= ch + (n + 1.0)*std::log((
double) nm / (
double) nK) +
326 (K + 0.5)*std::log(nK * pq / (K + 1.0)) -
327 StirlingCorrection(K + 1L) - StirlingCorrection(nK))
break;
331 return ((p>0.5) ? (
double)(n-K):(
double)K);
335 long pr=os.precision(20);
336 std::vector<unsigned long> t(2);
337 os <<
" " <<
name() <<
"\n";
338 os <<
"Uvec" <<
"\n";
340 os << defaultN <<
" " << defaultP <<
" " << t[0] <<
" " << t[1] <<
"\n";
344 long pr=os.precision(20);
345 os <<
" " <<
name() <<
"\n";
346 os << defaultN <<
" " << defaultP <<
"\n";
355 if (inName !=
name()) {
356 is.clear(std::ios::badbit | is.rdstate());
357 std::cerr <<
"Mismatch when expecting to read state of a "
358 <<
name() <<
" distribution\n"
359 <<
"Name found was " << inName
360 <<
"\nistream is left in the badbit state\n";
364 std::vector<unsigned long> t(2);
365 is >> defaultN >> defaultP;
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
static HepRandomEngine * getTheEngine()
HepRandomEngine & engine()
void fireArray(const int size, double *vect)
static void shootArray(const int size, double *vect, long n=1, double p=0.5)
std::ostream & put(std::ostream &os) const
std::istream & get(std::istream &is)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)