3 #include "../NumConstants.h" 4 #include "../VectorTools.h" 18 vector<size_t> sample(n);
19 for (
size_t i = 0; i < n; ++i)
24 for (
size_t j = 0; test& (j < probs.size()); ++j)
26 cumprob += probs[j] / s;
35 sample[i] = probs.size();
56 rgdbl[1] = dblAlpha - 1.0;
57 rgdbl[2] = (dblAlpha - (1.0 / (6.0 * dblAlpha))) / rgdbl[1];
58 rgdbl[3] = 2.0 / rgdbl[1];
59 rgdbl[4] = rgdbl[3] + 2.0;
60 rgdbl[5] = 1.0 / sqrt(dblAlpha);
71 dblRand1 = dblRand2 + rgdbl[5] * (1.0 - 1.86 * dblRand1);
73 while (!(0.0 < dblRand1 && dblRand1 < 1.0));
75 double dblTemp = rgdbl[2] * dblRand2 / dblRand1;
77 if (rgdbl[3] * dblRand1 + dblTemp + 1.0 / dblTemp <= rgdbl[4] ||
78 rgdbl[3] * log(dblRand1) + dblTemp - log(dblTemp) < 1.0)
80 return dblTemp * rgdbl[1];
93 const double dblexp = exp(1.0);
96 double dblRand0 = giveRandomNumberBetweenZeroAndEntry(1.0);
97 double dblRand1 = giveRandomNumberBetweenZeroAndEntry(1.0);
98 if (dblRand0 <= (dblexp / (dblAlpha + dblexp)))
100 dblTemp = pow(((dblAlpha + dblexp) * dblRand0) /
101 dblexp, 1.0 / dblAlpha);
102 if (dblRand1 <= exp(-1.0 * dblTemp))
107 dblTemp = -1.0 * log((dblAlpha + dblexp) * (1.0 - dblRand0) / (dblAlpha * dblexp));
108 if (dblRand1 <= pow(dblTemp, dblAlpha - 1.0))
124 double a0 = -.322232431088, a1 = -1, a2 = -.342242088547, a3 = -.0204231210245;
125 double a4 = -.453642210148e-4, b0 = .0993484626060, b1 = .588581570495;
126 double b2 = .531103462366, b3 = .103537752850, b4 = .0038560700634;
127 double y, z = 0, p = prob, p1;
129 p1 = (p < 0.5 ? p : 1 - p);
133 y = sqrt (log(1 / (p1 * p1)));
134 z = y + ((((y * a4 + a3) * y + a2) * y + a1) * y + a0) / ((((y * b4 + b3) * y + b2) * y + b1) * y + b0);
135 return p < 0.5 ? -z : z;
146 double p = alpha, g = ln_gamma_alpha;
147 double accurate = 1e-8, overflow = 1e30;
148 double factor, gin = 0, rn = 0, a = 0, b = 0, an = 0, dif = 0, term = 0;
149 vector<double> pn(6);
156 factor = exp(p * log(x) - x - g);
160 gin = 1; term = 1; rn = p;
163 term *= x / rn; gin += term;
171 a = 1 - p; b = a + x + 1; term = 0;
172 pn[0] = 1; pn[1] = x; pn[2] = x + 1; pn[3] = x * b;
175 a++; b += 2; term++; an = a * term;
176 for (i = 0; i < 2; i++)
178 pn[i + 4] = b * pn[i + 2] - an * pn[i];
182 rn = pn[4] / pn[5]; dif = fabs(gin - rn);
185 if (dif <= accurate * rn)
190 for (i = 0; i < 4; i++)
194 if (fabs(pn[4]) < overflow)
196 for (i = 0; i < 4; i++)
202 gin = 1 - factor * gin;
212 double e = .5e-6, aa = .6931471805, p = prob, g;
213 double xx, c, ch, a = 0, q = 0, p1 = 0, p2 = 0, t = 0, x = 0, b = 0, s1, s2, s3, s4, s5, s6;
215 if (p < .000002 || p > .999998 || v <= 0)
219 xx = v / 2; c = xx - 1;
220 if (v >= -1.24 * log(p))
223 ch = pow((p * xx * exp(g + xx * aa)), 1 / xx);
230 ch = 0.4; a = log(1 - p);
232 q = ch; p1 = 1 + ch * (4.67 + ch); p2 = ch * (6.73 + ch * (6.66 + ch));
233 t = -0.5 + (4.67 + 2 * ch) / p1 - (6.73 + ch * (13.32 + 3 * ch)) / p2;
234 ch -= (1 - exp(a + g + .5 * ch + c * aa) * p2 / p1) / t;
235 if (fabs(q / ch - 1) - .01 <= 0)
242 p1 = 0.222222 / v; ch = v * pow((x * sqrt(p1) + 1 - p1), 3.0);
243 if (ch > 2.2 * v + 6)
244 ch = -2 * (log(1 - p) - c * log(.5 * ch) + g);
246 q = ch; p1 = .5 * ch;
247 if ((t = incompleteGamma (p1, xx, g)) < 0)
249 std::cerr <<
"err IncompleteGamma" << std::endl;
253 t = p2 * exp(xx * aa + g + p1 - c * log(ch));
254 b = t / ch; a = 0.5 * t - b * c;
256 s1 = (210 + a * (140 + a * (105 + a * (84 + a * (70 + 60 * a))))) / 420;
257 s2 = (420 + a * (735 + a * (966 + a * (1141 + 1278 * a)))) / 2520;
258 s3 = (210 + a * (462 + a * (707 + 932 * a))) / 2520;
259 s4 = (252 + a * (672 + 1182 * a) + c * (294 + a * (889 + 1740 * a))) / 5040;
260 s5 = (84 + 264 * a + c * (175 + 606 * a)) / 2520;
261 s6 = (120 + c * (346 + 127 * c)) / 5040;
262 ch += t * (1 + 0.5 * t * s1 - b * c * (s1 - b * (s2 - b * (s3 - b * (s4 - b * (s5 - b * s6))))));
263 if (fabs(q / ch - 1) > e)
278 const static double a[5] = {
279 2.2352520354606839287,
280 161.02823106855587881,
281 1067.6894854603709582,
282 18154.981253343561249,
283 0.065682337918207449113
285 const static double b[4] = {
286 47.20258190468824187,
287 976.09855173777669322,
288 10260.932208618978205,
289 45507.789335026729956
291 const static double c[9] = {
292 0.39894151208813466764,
293 8.8831497943883759412,
294 93.506656132177855979,
295 597.27027639480026226,
296 2494.5375852903726711,
297 6848.1904505362823326,
298 11602.651437647350124,
299 9842.7148383839780218,
300 1.0765576773720192317e-8
302 const static double d[8] = {
303 22.266688044328115691,
304 235.38790178262499861,
305 1519.377599407554805,
306 6485.558298266760755,
307 18615.571640885098091,
308 34900.952721145977266,
309 38912.003286093271411,
310 19685.429676859990727
312 const static double p[6] = {
314 0.1274011611602473639,
315 0.022235277870649807,
316 0.001421619193227893466,
317 2.9112874951168792e-5,
318 0.02307344176494017303
320 const static double q[5] = {
322 0.468238212480865118,
323 0.0659881378689285515,
324 0.00378239633202758244,
325 7.29751555083966205e-5
328 double xden, xnum, temp, del, eps, xsq, y, cum;
341 for (i = 0; i < 3; ++i)
343 xnum = (xnum + a[i]) * xsq;
344 xden = (xden + b[i]) * xsq;
350 temp = x * (xnum + a[3]) / (xden + b[3]);
353 else if (y <= sqrt(32))
359 for (i = 0; i < 7; ++i)
361 xnum = (xnum + c[i]) * y;
362 xden = (xden + d[i]) * y;
364 temp = (xnum + c[7]) / (xden + d[7]);
366 xsq = trunc(y * 16) / 16;
367 del = (y - xsq) * (y + xsq);
368 cum = exp(-xsq * xsq * 0.5) * exp(-del * 0.5) * temp;
373 else if (-37.5193 < x && x < 8.2924)
378 for (i = 0; i < 4; ++i)
380 xnum = (xnum + p[i]) * xsq;
381 xden = (xden + q[i]) * xsq;
383 temp = xsq * (xnum + p[4]) / (xden + q[4]);
384 temp = (1 / sqrt(2 * M_PI) - temp) / y;
386 xsq = trunc(x * 16) / 16;
387 del = (x - xsq) * (x + xsq);
389 cum = exp(-xsq * xsq * 0.5) * exp(-del * 0.5) * temp;
407 return lnGamma(alpha) + lnGamma(beta) - lnGamma(alpha + beta);
433 double fpu = 3e-308, acu_min = 1e-300, lower = fpu, upper = 1 - 2.22e-16;
435 int swap_tail, i_pb, i_inn, niterations = 2000;
436 double a, adj, g, h, pp, prev = 0, qq, r, s, t, tx = 0, w, y, yprev;
439 if (prob < 0 || prob > 1)
440 throw Exception(
"RandomTools::qBeta. Prob muwt be between 0 and 1.");
442 throw Exception(
"RandomTools::qBeta. Alpha and beta should be positive numbers.");
447 if (prob == 0 || prob == 1)
450 double lnbeta = lnBeta(p, q);
455 a = prob; pp = p; qq = q; swap_tail = 0;
459 a = 1. - prob; pp = q; qq = p; swap_tail = 1;
463 r = sqrt(-log(a * a));
464 y = r - (2.30753 + 0.27061 * r) / (1. + (0.99229 + 0.04481 * r) * r);
466 if (pp > 1. && qq > 1.)
468 r = (y * y - 3.) / 6.;
469 s = 1. / (pp * 2. - 1.);
470 t = 1. / (qq * 2. - 1.);
472 w = y * sqrt(h + r) / h - (t - s) * (r + 5. / 6. - 2. / (3. * h));
473 xinbta = pp / (pp + qq * exp(w + w));
479 t = r * pow(1. - t + y * sqrt(t), 3.);
481 xinbta = 1. - exp((log((1. - a) * qq) + lnbeta) / qq);
484 t = (4. * pp + r - 2.) / t;
486 xinbta = exp((log(a * pp) + lnbeta) / pp);
488 xinbta = 1. - 2. / (t + 1.);
502 if (xinbta <= lower || xinbta >= upper)
503 xinbta = (a + .5) / 2;
512 acu = pow(10., -13. - 2.5 / (pp * pp) - 0.5 / (a * a));
513 acu = max(acu, acu_min);
515 for (i_pb = 0; i_pb < niterations; i_pb++)
517 y = pBeta(xinbta, pp, qq);
519 exp(lnbeta + r * log(xinbta) + t * log(1. - xinbta));
521 prev = max(fabs(adj), fpu);
522 for (i_inn = 0, g = 1; i_inn < niterations; i_inn++)
525 if (fabs(adj) < prev)
528 if (tx >= 0. && tx <= 1.)
530 if (prev <= acu || fabs(y) <= acu)
532 if (tx != 0. && tx != 1.)
538 if (fabs(tx - xinbta) < fpu)
545 return swap_tail ? 1. - xinbta : xinbta;
562 big = 4.503599627370496e15;
563 biginv = 2.22044604925031308085e-16;
564 maxgam = 171.624376956302725;
568 if ((alpha <= 0) || (beta <= 0))
569 throw Exception(
"RandomTools::incompleteBeta not valid with non-positive parameters");
571 if ((x < 0) || (x > 1))
572 throw Exception(
"RandomTools::incompleteBeta out of bounds limit");
581 if ((beta * x <= 1.0) && (x <= 0.95))
583 return incompletebetaps(alpha, beta, x, maxgam);
587 if (x > alpha / (alpha + beta))
600 if (flag == 1 && (beta * x <= 1.0) && (x <= 0.95) )
602 t = incompletebetaps(alpha, beta, x, maxgam);
609 y = x * (alpha + beta - 2.0) - (alpha - 1.0);
612 w = incompletebetafe(alpha, beta, x, big, biginv);
616 w = incompletebetafe2(alpha, beta, x, big, biginv) / xc;
620 if ( (alpha + beta < maxgam) && (fabs(y) < maxlog) && (fabs(t) < maxlog) )
623 t = t * pow(x, alpha);
626 t = t * exp(lnGamma(alpha + beta) - (lnGamma(alpha) + lnGamma(beta)));
637 y = y + t + lnGamma(alpha + beta) - lnGamma(alpha) - lnGamma(beta);
638 y = y + log(w / alpha);
706 xk = -x * k1 * k2 / (k3 * k4);
707 pk = pkm1 + pkm2 * xk;
708 qk = qkm1 + qkm2 * xk;
713 xk = x * k5 * k6 / (k7 * k8);
714 pk = pkm1 + pkm2 * xk;
715 qk = qkm1 + qkm2 * xk;
726 t = fabs((ans - r) / r);
745 if (fabs(qk) + fabs(pk) > big)
747 pkm2 = pkm2 * biginv;
748 pkm1 = pkm1 * biginv;
749 qkm2 = qkm2 * biginv;
750 qkm1 = qkm1 * biginv;
752 if ((fabs(qk) < biginv) || (fabs(pk) < biginv))
819 xk = -z * k1 * k2 / (k3 * k4);
820 pk = pkm1 + pkm2 * xk;
821 qk = qkm1 + qkm2 * xk;
826 xk = z * k5 * k6 / (k7 * k8);
827 pk = pkm1 + pkm2 * xk;
828 qk = qkm1 + qkm2 * xk;
839 t = fabs((ans - r) / r);
858 if (fabs(qk) + fabs(pk) > big)
860 pkm2 = pkm2 * biginv;
861 pkm1 = pkm1 * biginv;
862 qkm2 = qkm2 * biginv;
863 qkm1 = qkm1 * biginv;
865 if ((fabs(qk) < biginv) || (fabs(pk) < biginv))
920 t = exp(lnGamma(a + b) - (lnGamma(a) + lnGamma(b)));
921 s = s * t * pow(x, a);
925 t = lnGamma(a + b) - lnGamma(a) - lnGamma(b) + u + log(s);
static double VERY_TINY()
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)