/usr/include/random/beta.h is in libblitz0-dev 1:0.10-3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | // -*- C++ -*-
// $Id$
/*
* Generate Beta random deviate
*
* Returns a single random deviate from the beta distribution with
* parameters A and B. The density of the beta is
* x^(a-1) * (1-x)^(b-1) / B(a,b) for 0 < x < 1
*
* The mean is a/(a+b).
* The variance is ab/((a+b)^2(a+b+1))
* The rth moment is (a+r-1)^(r)/(a+b+r-1)^(r)
* where a^(r) means a * (a-1) * (a-2) * ... * (a-r+1)
*
* Method
* R. C. H. Cheng
* Generating Beta Variates with Nonintegral Shape Parameters
* Communications of the ACM, 21:317-322 (1978)
* (Algorithms BB and BC)
* http://www.acm.org/pubs/citations/journals/toms/1991-17-1/p98-l_ecuyer/
*
* This class has been adapted from RANDLIB.C 1.3, by
* Barry W. Brown, James Lovato, Kathy Russell, and John Venier.
*
* Adapter's note (TV): This code has gone from Pascal to Fortran to C.
* As a result it is a bit of a mess. Note also that the algorithms were
* originally designed for 32-bit float, and so some of the constants
* below have inadequate precision. This will not cause problems for
* casual use, but if you are generating millions of beta variates and
* rely on some convergence property, you may have want to worry
* about this.
*
* NEEDS_WORK: dig out the original paper and determine these constants
* to precision adequate for 128-bit float.
* NEEDS_WORK: turn this into structured code.
*/
#ifndef BZ_RANDOM_BETA
#define BZ_RANDOM_BETA
#ifndef BZ_RANDOM_UNIFORM
#include <random/uniform.h>
#endif
#ifndef BZ_NUMINQUIRE_H
#include <blitz/numinquire.h>
#endif
BZ_NAMESPACE(ranlib)
template<typename T = double, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class Beta : public UniformOpen<T,IRNG,stateTag>
{
public:
typedef T T_numtype;
Beta(T a, T b)
{
setParameters(a, b);
}
Beta(T a, T b, unsigned int i) : UniformOpen<T, IRNG, stateTag>(i)
{
setParameters(a, b);
}
T random();
void setParameters(T a, T b)
{
aa = a;
bb = b;
infnty = 0.3 * blitz::huge(T());
minlog = 0.085 * blitz::tiny(T());
expmax = log(infnty);
}
protected:
T ranf()
{
return UniformOpen<T,IRNG,stateTag>::random();
}
T aa, bb;
T infnty, minlog, expmax;
};
template<typename T, typename IRNG, typename stateTag>
T Beta<T,IRNG,stateTag>::random()
{
/* JJV changed expmax (log(1.0E38)==87.49823), and added minlog */
// TV: The original code had infnty = 1.0E38, minlog = 1.0E-37.
// I have changed these to 0.3 * huge and 8.5 * tiny. For IEEE
// float this works out to 1.020847E38 and 0.9991702E-37.
// I changed expmax from 87.49823 to log(infnty), which works out
// to 87.518866 for float.
static T olda = minlog;
static T oldb = minlog;
static T genbet,a,alpha,b,beta,delta,gamma,k1,k2,r,s,t,u1,u2,v,w,y,z;
static long qsame;
// This code determines if the aa and bb parameters are unchanged.
// If so, some code can be skipped.
qsame = (olda == aa) && (oldb == bb);
if (!qsame)
{
BZPRECHECK((aa > minlog) && (bb > minlog),
"Beta RNG: parameters must be >= " << minlog << endl
<< "Parameters are aa=" << aa << " and bb=" << bb << endl);
olda = aa;
oldb = bb;
}
if (!(min(aa,bb) > 1.0))
goto S100;
/*
Alborithm BB
Initialize
*/
if(qsame) goto S30;
a = min(aa,bb);
b = max(aa,bb);
alpha = a+b;
beta = sqrt((alpha-2.0)/(2.0*a*b-alpha));
gamma = a+1.0/beta;
S30:
S40:
u1 = ranf();
/*
Step 1
*/
u2 = ranf();
v = beta*log(u1/(1.0-u1));
/* JJV altered this */
if(v > expmax) goto S55;
/*
* JJV added checker to see if a*exp(v) will overflow
* JJV S50 _was_ w = a*exp(v); also note here a > 1.0
*/
w = exp(v);
if(w > infnty/a) goto S55;
w *= a;
goto S60;
S55:
w = infnty;
S60:
z = pow(u1,2.0)*u2;
r = gamma*v-1.3862944;
s = a+r-w;
/*
Step 2
*/
if(s+2.609438 >= 5.0*z) goto S70;
/*
Step 3
*/
t = log(z);
if(s > t) goto S70;
/*
* Step 4
*
* JJV added checker to see if log(alpha/(b+w)) will
* JJV overflow. If so, we count the log as -INF, and
* JJV consequently evaluate conditional as true, i.e.
* JJV the algorithm rejects the trial and starts over
* JJV May not need this here since alpha > 2.0
*/
if(alpha/(b+w) < minlog) goto S40;
if(r+alpha*log(alpha/(b+w)) < t) goto S40;
S70:
/*
Step 5
*/
if(!(aa == a)) goto S80;
genbet = w/(b+w);
goto S90;
S80:
genbet = b/(b+w);
S90:
goto S230;
S100:
/*
Algorithm BC
Initialize
*/
if(qsame) goto S110;
a = max(aa,bb);
b = min(aa,bb);
alpha = a+b;
beta = 1.0/b;
delta = 1.0+a-b;
k1 = delta*(1.38889E-2+4.16667E-2*b)/(a*beta-0.777778);
k2 = 0.25+(0.5+0.25/delta)*b;
S110:
S120:
u1 = ranf();
/*
Step 1
*/
u2 = ranf();
if(u1 >= 0.5) goto S130;
/*
Step 2
*/
y = u1*u2;
z = u1*y;
if(0.25*u2+z-y >= k1) goto S120;
goto S170;
S130:
/*
Step 3
*/
z = pow(u1,2.0)*u2;
if(!(z <= 0.25)) goto S160;
v = beta*log(u1/(1.0-u1));
/*
* JJV instead of checking v > expmax at top, I will check
* JJV if a < 1, then check the appropriate values
*/
if(a > 1.0) goto S135;
/* JJV a < 1 so it can help out if exp(v) would overflow */
if(v > expmax) goto S132;
w = a*exp(v);
goto S200;
S132:
w = v + log(a);
if(w > expmax) goto S140;
w = exp(w);
goto S200;
S135:
/* JJV in this case a > 1 */
if(v > expmax) goto S140;
w = exp(v);
if(w > infnty/a) goto S140;
w *= a;
goto S200;
S140:
w = infnty;
goto S200;
/*
* JJV old code
* if(!(v > expmax)) goto S140;
* w = infnty;
* goto S150;
*S140:
* w = a*exp(v);
*S150:
* goto S200;
*/
S160:
if(z >= k2) goto S120;
S170:
/*
Step 4
Step 5
*/
v = beta*log(u1/(1.0-u1));
/* JJV same kind of checking as above */
if(a > 1.0) goto S175;
/* JJV a < 1 so it can help out if exp(v) would overflow */
if(v > expmax) goto S172;
w = a*exp(v);
goto S190;
S172:
w = v + log(a);
if(w > expmax) goto S180;
w = exp(w);
goto S190;
S175:
/* JJV in this case a > 1.0 */
if(v > expmax) goto S180;
w = exp(v);
if(w > infnty/a) goto S180;
w *= a;
goto S190;
S180:
w = infnty;
/*
* JJV old code
* if(!(v > expmax)) goto S180;
* w = infnty;
* goto S190;
*S180:
* w = a*exp(v);
*/
S190:
/*
* JJV here we also check to see if log overlows; if so, we treat it
* JJV as -INF, which means condition is true, i.e. restart
*/
if(alpha/(b+w) < minlog) goto S120;
if(alpha*(log(alpha/(b+w))+v)-1.3862944 < log(z)) goto S120;
S200:
/*
Step 6
*/
if(!(a == aa)) goto S210;
genbet = w/(b+w);
goto S220;
S210:
genbet = b/(b+w);
S230:
S220:
return genbet;
}
BZ_NAMESPACE_END
#endif // BZ_RANDOM_BETA
|