| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file newran2.cpp |
| 5 | /// Nonuniform random number generators. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | #include "include.h" |
| 10 | |
| 11 | #include "newran.h" |
| 12 | |
| 13 | #ifdef use_namespace |
| 14 | namespace NEWRAN { using namespace RBD_COMMON; } |
| 15 | namespace RBD_LIBRARIES { using namespace NEWRAN; } |
| 16 | namespace NEWRAN { |
| 17 | #endif |
| 18 | |
| 19 | //********* classes which are used internally only ********************** |
| 20 | |
| 21 | /// Chi-square with 1df random number generator. |
| 22 | /// \internal |
| 23 | class ChiSq1 : public Normal // generate non-central chi-square |
| 24 | // rv with 1 degree of freedom |
| 25 | { |
| 26 | Real deltasq; // non-centrality parameter |
| 27 | Real delta; |
| 28 | |
| 29 | public: |
| 30 | ChiSq1(Real); // non-centrality parameter |
| 31 | ExtReal Mean() const { return 1.0+deltasq; } |
| 32 | ExtReal Variance() const { return 2.0+4.0*deltasq; } |
| 33 | Real Next(); |
| 34 | }; |
| 35 | |
| 36 | /// Poisson random number generator, larger mu. |
| 37 | /// \internal |
| 38 | class Poisson1 : public AsymGen // generate poisson rv, large mu |
| 39 | { |
| 40 | Real mu, ln_mu; |
| 41 | public: |
| 42 | Poisson1(Real); // constructor (Real=mean) |
| 43 | Real Density(Real) const; // Poisson density function |
| 44 | Real Next() { return floor(AsymGen::Next()); } |
| 45 | ExtReal Mean() const { return mu; } |
| 46 | ExtReal Variance() const { return mu; } |
| 47 | }; |
| 48 | |
| 49 | /// Poisson random number generator, mu <= 10. |
| 50 | /// \internal |
| 51 | class Poisson2 : public Random // generate poisson rv, large mu |
| 52 | { |
| 53 | DiscreteGen* dg; |
| 54 | public: |
| 55 | Poisson2(Real); // constructor (Real=mean) |
| 56 | ~Poisson2(); |
| 57 | Real Next() { return dg->Next(); } |
| 58 | ExtReal Mean() const { return dg->Mean(); } |
| 59 | ExtReal Variance() const { return dg->Variance(); } |
| 60 | }; |
| 61 | |
| 62 | /// Gamma random number generator, alpha <= 1. |
| 63 | /// \internal |
| 64 | class Gamma1 : public PosGen // generate gamma rv |
| 65 | // shape parameter <= 1 |
| 66 | { |
| 67 | Real ln_gam, ralpha, alpha; |
| 68 | public: |
| 69 | Gamma1(Real); // constructor (Real=shape) |
| 70 | Real Density(Real) const; // gamma density function |
| 71 | Real Next(); // carries out power transform |
| 72 | ExtReal Mean() const { return alpha; } |
| 73 | ExtReal Variance() const { return alpha; } |
| 74 | }; |
| 75 | |
| 76 | /// Weibull::Next() |
| 77 | |
| 78 | Real Weibull :: Next() |
| 79 | { |
| 80 | double rnd; |
| 81 | rnd = U.Next(); |
| 82 | rnd = scale * pow(log(1/rnd),1/shape); //inversa della CDF |
| 83 | return rnd; |
| 84 | } |
| 85 | |
| 86 | /// Extreme_Largest::Next() |
| 87 | |
| 88 | Real Extreme_Largest :: Next() |
| 89 | { |
| 90 | |
| 91 | long double rnd1; |
| 92 | rnd1 = U.Next(); |
| 93 | long double op1=log(double(rnd1)); |
| 94 | rnd1=mu-sigma*(log(double(-op1))); |
| 95 | return rnd1; |
| 96 | } |
| 97 | |
| 98 | /// T-Student::Next() |
| 99 | Real Student::Next() |
| 100 | { |
| 101 | Normal N; |
| 102 | Gamma G(nu*0.5); |
| 103 | Real rnd1 = G.Next(); |
| 104 | Real rnd2 = N.Next(); |
| 105 | Real rnd=mu+(sigma*(rnd2*sqrt(nu/(2*rnd1)))); |
| 106 | return rnd; |
| 107 | } |
| 108 | |
| 109 | /// Exponential::Next() |
| 110 | |
| 111 | Real Exponential :: Next() |
| 112 | { |
| 113 | |
| 114 | double rnd; |
| 115 | rnd = U.Next(); |
| 116 | rnd = mean*log(1/(1-rnd)); |
| 117 | return rnd; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /// Gamma random number generator, alpha > 1. |
| 122 | /// \internal |
| 123 | class Gamma2 : public AsymGen // generate gamma rv |
| 124 | // shape parameter > 1 |
| 125 | { |
| 126 | Real alpha, ln_gam; |
| 127 | public: |
| 128 | Gamma2(Real); // constructor (Real=shape) |
| 129 | Real Density(Real) const; // gamma density function |
| 130 | ExtReal Mean() const { return alpha; } |
| 131 | ExtReal Variance() const { return alpha; } |
| 132 | }; |
| 133 | |
| 134 | /// Binomial random number generator, n > 40. |
| 135 | /// \internal |
| 136 | class Binomial1 : public AsymGen // generate binomial rv, larger n |
| 137 | { |
| 138 | Real p, q, ln_p, ln_q, ln_n_fac; int n; |
| 139 | public: |
| 140 | Binomial1(int nx, Real px); |
| 141 | Real Density(Real) const; |
| 142 | Real Next() { return floor(AsymGen::Next()); } |
| 143 | ExtReal Mean() const { return p * n; } |
| 144 | ExtReal Variance() const { return p * q * n; } |
| 145 | }; |
| 146 | |
| 147 | /// Binomial random number generator, n < 40 or n*p <= 8. |
| 148 | /// \internal |
| 149 | class Binomial2 : public Random // generate binomial rv, smaller n |
| 150 | { |
| 151 | DiscreteGen* dg; |
| 152 | public: |
| 153 | Binomial2(int nx, Real px); |
| 154 | ~Binomial2(); |
| 155 | Real Next() { return dg->Next(); } |
| 156 | ExtReal Mean() const { return dg->Mean(); } |
| 157 | ExtReal Variance() const { return dg->Variance(); } |
| 158 | }; |
| 159 | |
| 160 | //************************ static variables *************************** |
| 161 | |
| 162 | Real Normal::Nxi; |
| 163 | Real* Normal::Nsx; |
| 164 | Real* Normal::Nsfx; |
| 165 | long Normal::count=0; |
| 166 | |
| 167 | //**************************** utilities ****************************** |
| 168 | |
| 169 | inline Real square(Real x) { return x*x; } |
| 170 | inline ExtReal square(const ExtReal& x) { return x*x; } |
| 171 | |
| 172 | static void ErrorNoSpace() { Throw(Bad_alloc("Newran: out of space")); } |
| 173 | |
| 174 | //************************* end of definitions ************************ |
| 175 | |
| 176 | static int Round(Real f) |
| 177 | { |
| 178 | int i = (int)floor(f + 0.5); |
| 179 | if (fabs((Real)i - f) > 0.5) |
| 180 | { |
| 181 | Tracer et("Round "); |
| 182 | Throw(Runtime_error("Cannot convert to integer ")); |
| 183 | } |
| 184 | return i; |
| 185 | } |
| 186 | |
| 187 | PosGen::PosGen() // Constructor |
| 188 | { |
| 189 | #ifdef MONITOR |
| 190 | cout << "constructing PosGen\n"; |
| 191 | #endif |
| 192 | NotReady=true; |
| 193 | } |
| 194 | |
| 195 | PosGen::~PosGen() |
| 196 | { |
| 197 | if (!NotReady) |
| 198 | { |
| 199 | #ifdef MONITOR |
| 200 | cout << "freeing PosGen arrays\n"; |
| 201 | #endif |
| 202 | delete [] sx; delete [] sfx; |
| 203 | } |
| 204 | #ifdef MONITOR |
| 205 | cout << "destructing PosGen\n"; |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | void PosGen::Build(bool sym) // set up arrays |
| 210 | { |
| 211 | #ifdef MONITOR |
| 212 | cout << "building PosGen arrays\n"; |
| 213 | #endif |
| 214 | int i; |
| 215 | NotReady=false; |
| 216 | sx=new Real[60]; sfx=new Real[60]; |
| 217 | if (!sx || !sfx) ErrorNoSpace(); |
| 218 | Real sxi=0.0; Real inc = sym ? 0.01 : 0.02; |
| 219 | for (i=0; i<60; i++) |
| 220 | { |
| 221 | sx[i]=sxi; Real f1=Density(sxi); sfx[i]=f1; |
| 222 | if (f1<=0.0) goto L20; |
| 223 | sxi+=inc/f1; |
| 224 | } |
| 225 | Throw(Runtime_error("Newran: area too large")); |
| 226 | L20: |
| 227 | if (i<50) Throw(Runtime_error("Newran: area too small")); |
| 228 | xi = sym ? 2*i : i; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | Real PosGen::Next() |
| 233 | { |
| 234 | Real ak,y; int ir; |
| 235 | if (NotReady) Build(false); |
| 236 | do |
| 237 | { |
| 238 | Real r1=RNG->Next(); |
| 239 | ir = (int)(r1*xi); Real sxi=sx[ir]; |
| 240 | ak=sxi+(sx[ir+1]-sxi) * RNG->Next(); |
| 241 | y=sfx[ir] * RNG->Next(); |
| 242 | } |
| 243 | while ( y>=sfx[ir+1] && y>=Density(ak) ); |
| 244 | return ak; |
| 245 | } |
| 246 | |
| 247 | Real SymGen::Next() |
| 248 | { |
| 249 | Real s,ak,y; int ir; |
| 250 | if (NotReady) Build(true); |
| 251 | do |
| 252 | { |
| 253 | s=1.0; |
| 254 | Real r1=RNG->Next(); |
| 255 | if (r1>0.5) { s=-1.0; r1=1.0-r1; } |
| 256 | ir = (int)(r1*xi); Real sxi=sx[ir]; |
| 257 | ak=sxi+(sx[ir+1]-sxi)*RNG->Next(); |
| 258 | y=sfx[ir]*RNG->Next(); |
| 259 | } |
| 260 | while ( y>=sfx[ir+1] && y>=Density(ak) ); |
| 261 | return s*ak; |
| 262 | } |
| 263 | |
| 264 | AsymGen::AsymGen(Real modex) // Constructor |
| 265 | { |
| 266 | #ifdef MONITOR |
| 267 | cout << "constructing AsymGen\n"; |
| 268 | #endif |
| 269 | mode=modex; NotReady=true; |
| 270 | } |
| 271 | |
| 272 | void AsymGen::Build() // set up arrays |
| 273 | { |
| 274 | #ifdef MONITOR |
| 275 | cout << "building AsymGen arrays\n"; |
| 276 | #endif |
| 277 | int i; |
| 278 | NotReady=false; |
| 279 | sx=new Real[121]; sfx=new Real[121]; |
| 280 | if (!sx || !sfx) ErrorNoSpace(); |
| 281 | Real sxi=mode; |
| 282 | for (i=0; i<120; i++) |
| 283 | { |
| 284 | sx[i]=sxi; Real f1=Density(sxi); sfx[i]=f1; |
| 285 | if (f1<=0.0) goto L20; |
| 286 | sxi+=0.01/f1; |
| 287 | } |
| 288 | Throw(Runtime_error("Newran: area too large (a)")); |
| 289 | L20: |
| 290 | ic=i-1; sx[120]=sxi; sfx[120]=0.0; |
| 291 | sxi=mode; |
| 292 | for (; i<120; i++) |
| 293 | { |
| 294 | sx[i]=sxi; Real f1=Density(sxi); sfx[i]=f1; |
| 295 | if (f1<=0.0) goto L30; |
| 296 | sxi-=0.01/f1; |
| 297 | } |
| 298 | Throw(Runtime_error("Newran: area too large (b)")); |
| 299 | L30: |
| 300 | if (i<100) Throw(Runtime_error("Newran: area too small")); |
| 301 | xi=i; |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | Real AsymGen::Next() |
| 306 | { |
| 307 | Real ak,y; int ir1; |
| 308 | if (NotReady) Build(); |
| 309 | do |
| 310 | { |
| 311 | Real r1=RNG->Next(); |
| 312 | int ir=(int)(r1*xi); Real sxi=sx[ir]; |
| 313 | ir1 = (ir==ic) ? 120 : ir+1; |
| 314 | ak=sxi+(sx[ir1]-sxi)*RNG->Next(); |
| 315 | y=sfx[ir]*RNG->Next(); |
| 316 | } |
| 317 | while ( y>=sfx[ir1] && y>=Density(ak) ); |
| 318 | return ak; |
| 319 | } |
| 320 | |
| 321 | AsymGen::~AsymGen() |
| 322 | { |
| 323 | if (!NotReady) |
| 324 | { |
| 325 | #ifdef MONITOR |
| 326 | cout << "freeing AsymGen arrays\n"; |
| 327 | #endif |
| 328 | delete [] sx; delete [] sfx; |
| 329 | } |
| 330 | #ifdef MONITOR |
| 331 | cout << "destructing AsymGen\n"; |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | PosGenX::PosGenX(PDF fx) { f=fx; } |
| 336 | |
| 337 | SymGenX::SymGenX(PDF fx) { f=fx; } |
| 338 | |
| 339 | AsymGenX::AsymGenX(PDF fx, Real mx) : AsymGen(mx) { f=fx; } |
| 340 | |
| 341 | |
| 342 | Normal::Normal() |
| 343 | { |
| 344 | if (count) { NotReady=false; xi=Nxi; sx=Nsx; sfx=Nsfx; } |
| 345 | else { Build(true); Nxi=xi; Nsx=sx; Nsfx=sfx; } |
| 346 | count++; |
| 347 | } |
| 348 | |
| 349 | Normal::~Normal() |
| 350 | { |
| 351 | count--; |
| 352 | if (count) NotReady=true; // disable freeing arrays |
| 353 | } |
| 354 | |
| 355 | Real Normal::Density(Real x) const // normal density |
| 356 | { return (fabs(x)>8.0) ? 0 : 0.398942280 * exp(-x*x / 2); } |
| 357 | |
| 358 | ChiSq1::ChiSq1(Real d) : Normal() // chisquare with 1 df |
| 359 | { deltasq=d; delta=sqrt(d); } |
| 360 | |
| 361 | Real ChiSq1::Next() |
| 362 | { Real z=Normal::Next()+delta; return z*z; } |
| 363 | |
| 364 | ChiSq::ChiSq(int df, Real noncen) |
| 365 | { |
| 366 | if (df<=0 || noncen<0.0) Throw(Logic_error("Newran: illegal parameters")); |
| 367 | else if (df==1) { version=0; c1=new ChiSq1(noncen); } |
| 368 | else if (noncen==0.0) |
| 369 | { |
| 370 | if (df==2) { version=1; c1=new Exponential(); } |
| 371 | else { version=2; c1=new Gamma2(0.5*df); } |
| 372 | } |
| 373 | else if (df==2) { version=3; c1=new ChiSq1(noncen/2.0); } |
| 374 | else if (df==3) { version=4; c1=new Exponential(); c2=new ChiSq1(noncen); } |
| 375 | else { version=5; c1=new Gamma2(0.5*(df-1)); c2=new ChiSq1(noncen); } |
| 376 | if (!c1 || (version>3 && !c2)) ErrorNoSpace(); |
| 377 | mean=df+noncen; var=2*df+4.0*noncen; |
| 378 | } |
| 379 | |
| 380 | ChiSq::~ChiSq() { delete c1; if (version>3) delete c2; } |
| 381 | |
| 382 | Real ChiSq::Next() |
| 383 | { |
| 384 | switch(version) |
| 385 | { |
| 386 | case 0: return c1->Next(); |
| 387 | case 1: case 2: return c1->Next()*2.0; |
| 388 | case 3: return c1->Next() + c1->Next(); |
| 389 | case 4: case 5: Real s1 = c1->Next()*2.0; Real s2 = c2->Next(); |
| 390 | return s1 + s2; // this is to make it work with Microsoft VC5 |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | Pareto::Pareto(Real shape) : Shape(shape) |
| 396 | { |
| 397 | if (Shape <= 0) Throw(Logic_error("Newran: illegal parameter")); |
| 398 | RS = -1.0 / Shape; |
| 399 | } |
| 400 | |
| 401 | Real Pareto::Next() |
| 402 | { return pow(RNG->Next(), RS); } |
| 403 | |
| 404 | ExtReal Pareto::Mean() const |
| 405 | { |
| 406 | if (Shape > 1) return Shape/(Shape-1.0); |
| 407 | else return PlusInfinity; |
| 408 | } |
| 409 | |
| 410 | ExtReal Pareto::Variance() const |
| 411 | { |
| 412 | if (Shape > 2) return Shape/(square(Shape-1.0))/(Shape-2.0); |
| 413 | else return PlusInfinity; |
| 414 | } |
| 415 | |
| 416 | Real Cauchy::Density(Real x) const // Cauchy density function |
| 417 | { return (fabs(x)>1.0e15) ? 0 : 0.31830988618 / (1.0+x*x); } |
| 418 | |
| 419 | Poisson1::Poisson1(Real mux) : AsymGen(mux) // Constructor |
| 420 | { mu=mux; ln_mu=log(mu); } |
| 421 | |
| 422 | Real Poisson1::Density(Real x) const // Poisson density function |
| 423 | { |
| 424 | if (x < 0.0) return 0.0; |
| 425 | double ix = floor(x); // use integer part |
| 426 | double l = ln_mu * ix - mu - ln_gamma(1.0 + ix); |
| 427 | return (l < -40.0) ? 0.0 : exp(l); |
| 428 | } |
| 429 | |
| 430 | Binomial1::Binomial1(int nx, Real px) |
| 431 | : AsymGen((nx + 1) * px), p(px), q(1.0 - px), n(nx) |
| 432 | { ln_p = log(p); ln_q = log(q); ln_n_fac = ln_gamma(n+1); } |
| 433 | |
| 434 | Real Binomial1::Density(Real x) const // Binomial density function |
| 435 | { |
| 436 | double ix = floor(x); // use integer part |
| 437 | if (ix < 0.0 || ix > n) return 0.0; |
| 438 | double l = ln_n_fac - ln_gamma(ix+1) - ln_gamma(n-ix+1) |
| 439 | + ix * ln_p + (n-ix) * ln_q; |
| 440 | return (l < -40.0) ? 0.0 : exp(l); |
| 441 | } |
| 442 | |
| 443 | Poisson2::Poisson2(Real mux) |
| 444 | { |
| 445 | Real probs[40]; |
| 446 | probs[0]=exp(-mux); |
| 447 | for (int i=1; i<40; i++) probs[i]=probs[i-1]*mux/i; |
| 448 | dg=new DiscreteGen(40,probs); |
| 449 | if (!dg) ErrorNoSpace(); |
| 450 | } |
| 451 | |
| 452 | Poisson2::~Poisson2() { delete dg; } |
| 453 | |
| 454 | Binomial2::Binomial2(int nx, Real px) |
| 455 | { |
| 456 | Real qx = 1.0 - px; |
| 457 | Real probs[40]; |
| 458 | int k = (int)(nx * px); |
| 459 | probs[k] = exp(ln_gamma(nx+1) - ln_gamma(k+1) - ln_gamma(nx-k+1) |
| 460 | + k * log(px) + (nx-k) * log(qx)); |
| 461 | int i; |
| 462 | int m = (nx >= 40) ? 39 : nx; |
| 463 | for (i=k+1; i<=m; i++) probs[i]=probs[i-1] * px * (nx-i+1) / qx / i; |
| 464 | for (i=k-1; i>=0; i--) probs[i]=probs[i+1] * qx * (i+1) / px / (nx-i); |
| 465 | dg = new DiscreteGen(m + 1, probs); |
| 466 | if (!dg) ErrorNoSpace(); |
| 467 | } |
| 468 | |
| 469 | Binomial2::~Binomial2() { delete dg; } |
| 470 | |
| 471 | Real Exponential::Density(Real x) const // Negative exponential |
| 472 | { return (x > 5*mean || x < 0.0) ? 0.0 : (1/mean)*exp(-(x/mean)); } |
| 473 | |
| 474 | |
| 475 | Poisson::Poisson(Real mu) |
| 476 | { |
| 477 | if (mu <= 8.0) method = new Poisson2(mu); |
| 478 | else method = new Poisson1(mu); |
| 479 | if (!method) ErrorNoSpace(); |
| 480 | } |
| 481 | |
| 482 | Binomial::Binomial(int nx, Real px) |
| 483 | { |
| 484 | if (nx < 40 || nx * px <= 8.0) method = new Binomial2(nx, px); |
| 485 | else method = new Binomial1(nx, px); |
| 486 | if (!method) ErrorNoSpace(); |
| 487 | } |
| 488 | |
| 489 | NegativeBinomial::NegativeBinomial(Real NX, Real PX) |
| 490 | : AsymGen(0.0), N(NX), P(PX), Q(1.0 + PX) |
| 491 | { |
| 492 | p = 1.0 / Q; ln_q = log(1.0 - p); |
| 493 | c = N * log(p) - ln_gamma(N); mode = (N - 1) * P; |
| 494 | if (mode < 1.0) mode = 0.0; |
| 495 | } |
| 496 | |
| 497 | Real NegativeBinomial::Next() { return floor(AsymGen::Next()); } |
| 498 | |
| 499 | Real NegativeBinomial::Density(Real x) const |
| 500 | { |
| 501 | if (x < 0.0) return 0.0; |
| 502 | Real ix = floor(x); |
| 503 | Real l = c + ln_gamma(ix + N) - ln_gamma(ix + 1) + ix * ln_q; |
| 504 | return (l < -40.0) ? 0.0 : exp(l); |
| 505 | } |
| 506 | |
| 507 | Gamma1::Gamma1(Real alphax) // constructor (Real=shape) |
| 508 | { ralpha=1.0/alphax; ln_gam=ln_gamma(alphax+1.0); alpha=alphax; } |
| 509 | |
| 510 | Real Gamma1::Density(Real x) const // density function for |
| 511 | { // transformed gamma |
| 512 | Real l = - pow(x,ralpha) - ln_gam; |
| 513 | return (l < -40.0) ? 0.0 : exp(l); |
| 514 | } |
| 515 | |
| 516 | Real Gamma1::Next() // transform variable |
| 517 | { return pow(PosGen::Next(),ralpha); } |
| 518 | |
| 519 | Gamma2::Gamma2(Real alphax) : AsymGen(alphax-1.0) // constructor (Real=shape) |
| 520 | { alpha=alphax; ln_gam=ln_gamma(alpha); } |
| 521 | |
| 522 | Real Gamma2::Density(Real x) const // gamma density function |
| 523 | { |
| 524 | if (x<=0.0) return 0.0; |
| 525 | double l = (alpha-1.0)*log(x) - x - ln_gam; |
| 526 | return (l < -40.0) ? 0.0 : exp(l); |
| 527 | } |
| 528 | |
| 529 | Gamma::Gamma(Real alpha) // general gamma generator |
| 530 | { |
| 531 | if (alpha<1.0) method = new Gamma1(alpha); |
| 532 | else if (alpha==1.0) method = new Exponential(); |
| 533 | else method = new Gamma2(alpha); |
| 534 | if (!method) ErrorNoSpace(); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | Stable::Stable(Real a, Real bx, Notation n) |
| 539 | : alpha(a), notation(n), piby2(1.570796327), piby4(0.785398163) |
| 540 | { |
| 541 | |
| 542 | if (notation == Kalpha) |
| 543 | { |
| 544 | if (a == 1.0) bprime = bx; |
| 545 | else if (fabs(bx) == 1.0) bprime = (alpha > 1.0) ? -bx : bx; |
| 546 | else bprime = tan(piby2 * k(alpha) * bx) / tan(piby2 * alpha); |
| 547 | beta = bx; |
| 548 | } |
| 549 | else if (notation == Chambers || notation == Standard) bprime = bx; |
| 550 | else if (notation == Default) |
| 551 | { |
| 552 | notation = Chambers; bprime = bx; |
| 553 | if ( bx != 0.0 && alpha != 1 && alpha != 2) |
| 554 | Throw(Logic_error("Newran: must specify Stable notation")); |
| 555 | } |
| 556 | else Throw(Logic_error("Newran: Stable notation not recognised")); |
| 557 | } |
| 558 | |
| 559 | //compute (exp(x) - 1) / x |
| 560 | Real Stable::d2(Real z) |
| 561 | { |
| 562 | const Real p1 = 840.066852536483239; |
| 563 | const Real p2 = 20.0011141589964569; |
| 564 | const Real q1 = 1680.13370507296648; |
| 565 | const Real q2 = 180.013370407390023; |
| 566 | if (fabs(z) <= 0.1) |
| 567 | { |
| 568 | Real zz = z * z; |
| 569 | Real pv = p1 + zz * p2; |
| 570 | return 2.0 * pv / (q1 + zz * (q2 + zz) - z * pv); |
| 571 | } |
| 572 | else return (exp(z) - 1.0) / z; |
| 573 | } |
| 574 | |
| 575 | // compute tan(x) / x |
| 576 | Real Stable::tan2(Real xarg) |
| 577 | { |
| 578 | const Real p0 = 129.221035; |
| 579 | const Real p1 = -8.87662377; |
| 580 | const Real p2 = 0.0528644456; |
| 581 | const Real q0 = 164.529332; |
| 582 | const Real q1 = -45.1320561; |
| 583 | Real x = fabs(xarg); |
| 584 | if (x <= piby4) |
| 585 | { |
| 586 | x /= piby4; |
| 587 | Real xx = x * x; |
| 588 | return (p0 + xx * (p1 + xx * p2)) |
| 589 | / (piby4 * (q0 + xx * (q1 + xx))); |
| 590 | } |
| 591 | else return tan(xarg) / xarg; |
| 592 | } |
| 593 | |
| 594 | Real Stable::k(Real a) { return 1.0 - fabs(1.0 - a); } |
| 595 | |
| 596 | |
| 597 | Real Stable::Next() |
| 598 | { |
| 599 | Real u = RNG->Next(); Real w = -log(RNG->Next()); |
| 600 | Real eps = 1.0 - alpha; |
| 601 | Real phiby2 = piby2 * (u - 0.5); |
| 602 | Real a = tan(phiby2); |
| 603 | Real bb = tan2(eps * phiby2); |
| 604 | Real b = eps * phiby2 * bb; |
| 605 | Real tau = ( eps > -0.99) ? bprime / (tan2(eps * piby2) * piby2) |
| 606 | : bprime * eps * tan(alpha * piby2); |
| 607 | Real a2 = 1.0 - a * a; Real a2p = 1.0 + a * a; |
| 608 | Real b2 = 1.0 - b * b; Real b2p = 1.0 + b * b; |
| 609 | Real z = a2p * (b2 + 2.0 * phiby2 * bb * tau) / (w * a2 * b2p); |
| 610 | Real alogz = log(z) / alpha; |
| 611 | Real d = d2(eps * alogz) * alogz; |
| 612 | Real Sprime = (1.0 + eps * d) * 2.0 * ((a - b) * (1.0 + a * b) |
| 613 | - phiby2 * tau * bb * (b * a2 - 2.0 * a)) / (a2 * b2p) + tau * d; |
| 614 | |
| 615 | if (notation == Chambers || bprime == 0.0 || alpha == 1.0 || alpha == 2.0) |
| 616 | return Sprime; |
| 617 | else if (notation == Standard) return Sprime + bprime * tan(piby2 * alpha); |
| 618 | else |
| 619 | { |
| 620 | Real arg = piby2 * k(alpha) * beta; |
| 621 | return ( Sprime + tan(arg) ) * pow( cos(arg), 1.0 / alpha ); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | ExtReal Stable::Mean() const |
| 626 | { |
| 627 | if (alpha < 1.0) return (bprime == 1.0) ? PlusInfinity : |
| 628 | (bprime == -1.0) ? MinusInfinity : Indefinite; |
| 629 | else if (alpha == 1.0) return Indefinite; |
| 630 | else if (notation == Chambers) return -bprime * tan(piby2 * alpha); |
| 631 | else return 0.0; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | ExtReal Stable::Variance() const |
| 636 | { |
| 637 | return (alpha < 2.0) ? PlusInfinity : 0.5; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | DiscreteGen::DiscreteGen(int n1, Real* prob) // discrete generator |
| 642 | // values on 0,...,n1-1 |
| 643 | { |
| 644 | #ifdef MONITOR |
| 645 | cout << "constructing DiscreteGen\n"; |
| 646 | #endif |
| 647 | Gen(n1, prob); val=0; |
| 648 | mean=0.0; var=0.0; |
| 649 | { for (int i=0; i<n; i++) mean = mean + i*prob[i]; } |
| 650 | { for (int i=0; i<n; i++) var = var + square(i-mean) * prob[i]; } |
| 651 | } |
| 652 | |
| 653 | DiscreteGen::DiscreteGen(int n1, Real* prob, Real* val1) |
| 654 | // discrete generator |
| 655 | // values on *val |
| 656 | { |
| 657 | #ifdef MONITOR |
| 658 | cout << "constructing DiscreteGen\n"; |
| 659 | #endif |
| 660 | Gen(n1, prob); val = new Real[n1]; |
| 661 | if (!val) ErrorNoSpace(); |
| 662 | for (int i=0; i<n1; i++) val[i]=val1[i]; |
| 663 | mean=0.0; var=0.0; |
| 664 | { for (int i=0; i<n; i++) mean = mean + val[i]*prob[i]; } |
| 665 | { for (int i=0; i<n; i++) var = var + square(val[i]-mean)*prob[i]; } |
| 666 | } |
| 667 | |
| 668 | |
| 669 | void DiscreteGen::Gen(int n1, Real* prob) |
| 670 | { |
| 671 | n=n1; // number of values |
| 672 | p=new Real[n]; ialt=new int[n]; |
| 673 | if (!p || !ialt) ErrorNoSpace(); |
| 674 | Real rn = 1.0/n; Real px = 0; int i; |
| 675 | for (i=0; i<n; i++) { p[i]=0.0; ialt[i]=-1; } |
| 676 | for (i=0; i<n; i++) |
| 677 | { |
| 678 | Real pmin=1.0; Real pmax=-1.0; int jmin=-1; int jmax=-1; |
| 679 | for (int j=0; j<n; j++) |
| 680 | { |
| 681 | if (ialt[j]<0) |
| 682 | { |
| 683 | px=prob[j]-p[j]; |
| 684 | if (pmax<=px) { pmax=px; jmax=j; } |
| 685 | if (pmin>=px) { pmin=px; jmin=j; } |
| 686 | } |
| 687 | } |
| 688 | if ((jmax<0) || (jmin<0)) Throw(Runtime_error("Newran: method fails")); |
| 689 | ialt[jmin]=jmax; px=rn-pmin; p[jmax]+=px; px*=n; p[jmin]=px; |
| 690 | if ((px>1.00001)||(px<-.00001)) |
| 691 | Throw(Runtime_error("Newran: probs don't add to 1 (a)")); |
| 692 | } |
| 693 | if (px>0.00001) Throw(Runtime_error("Newran: probs don't add to 1 (b)")); |
| 694 | } |
| 695 | |
| 696 | DiscreteGen::~DiscreteGen() |
| 697 | { |
| 698 | delete [] p; delete [] ialt; delete [] val; |
| 699 | #ifdef MONITOR |
| 700 | cout << "destructing DiscreteGen\n"; |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | Real DiscreteGen::Next() // Next discrete random variable |
| 705 | { |
| 706 | int i = (int)(n*RNG->Next()); if (RNG->Next()<p[i]) i=ialt[i]; |
| 707 | return val ? val[i] : (Real)i; |
| 708 | } |
| 709 | |
| 710 | // log gamma function |
| 711 | |
| 712 | static Real ln_gamma_asymp(Real z); |
| 713 | static Real ln_gamma_near_1(Real z); |
| 714 | static Real ln_gamma_near_2(Real z); |
| 715 | static Real ln_gamma_gt_1(Real z); |
| 716 | |
| 717 | Real gamma_series(long_Real z) |
| 718 | { |
| 719 | const long_Real n0 = 1.846356774800897077637235e6L; |
| 720 | const long_Real n1 = 1.759131712935803984850945e6L; |
| 721 | const long_Real n2 = 7.542124083269936035445648e5L; |
| 722 | const long_Real n3 = 1.916219552338091802379555e5L; |
| 723 | const long_Real n4 = 3.194965924862382624981206e4L; |
| 724 | const long_Real n5 = 3.652838209061050933543152e3L; |
| 725 | const long_Real n6 = 2.900228320454639341680104e2L; |
| 726 | const long_Real n7 = 1.578981962865355560648172e1L; |
| 727 | const long_Real n8 = 0.564145967416346085128381L; |
| 728 | const long_Real n9 = 0.119443368011180931171494e-1L; |
| 729 | const long_Real n10 = 0.113800747608906017093789e-3L; |
| 730 | const long_Real d0 = 3628800; |
| 731 | const long_Real d1 = 10628640; |
| 732 | const long_Real d2 = 12753576; |
| 733 | const long_Real d3 = 8409500; |
| 734 | const long_Real d4 = 3416930; |
| 735 | const long_Real d5 = 902055; |
| 736 | const long_Real d6 = 157773; |
| 737 | const long_Real d7 = 18150; |
| 738 | const long_Real d8 = 1320; |
| 739 | const long_Real d9 = 55; |
| 740 | const long_Real d10 = 1; |
| 741 | |
| 742 | if (z < 10.0) |
| 743 | { |
| 744 | const long_Real num = n0 + z * (n1 + z * (n2 + z * (n3 + z * (n4 + z * (n5 |
| 745 | + z * (n6 + z * (n7 + z * (n8 + z * (n9 + z * n10))))))))); |
| 746 | const long_Real den = d0 + z * (d1 + z * (d2 + z * (d3 + z * (d4 + z * (d5 |
| 747 | + z * (d6 + z * (d7 + z * (d8 + z * (d9 + z))))))))); |
| 748 | return (Real)(num / den); |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | long_Real r = 1.0L / z; |
| 753 | const long_Real num = n10 + r * (n9 + r * (n8 + r * (n7 + r * (n6 + r * (n5 |
| 754 | + r * (n4 + r * (n3 + r * (n2 + r * (n1 + r * n0))))))))); |
| 755 | const long_Real den = d10 + r * (d9 + r * (d8 + r * (d7 + r * (d6 + r * (d5 |
| 756 | + r * (d4 + r * (d3 + r * (d2 + r * (d1 + r * d0))))))))); |
| 757 | return (Real)(num / den); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // version for z > 100 |
| 762 | Real ln_gamma_asymp(Real z) |
| 763 | { |
| 764 | long_Real z2 = z * z; |
| 765 | return |
| 766 | (z - 0.5) * log(z) - z + 0.9189385332046727417L |
| 767 | + (1.0L/12.0L + (-1.0L/360.0L + (1.0L/1260.0L - 1.0L/1680.0L/z2)/z2)/z2)/z; |
| 768 | } |
| 769 | |
| 770 | // version for z > 0.9 |
| 771 | Real ln_gamma_gt_1(Real z) |
| 772 | { |
| 773 | Tracer et("ln_gamma_gt_1 "); |
| 774 | if (z >= 100) return ln_gamma_asymp(z); |
| 775 | if (z >= 2.1) return ln_gamma_lanczos(z); |
| 776 | if (z >= 1.9) return ln_gamma_near_2(z); |
| 777 | if (z >= 1.1) return ln_gamma_lanczos(z); |
| 778 | if (z >= 0.9) return ln_gamma_near_1(z); |
| 779 | Throw(Invalid_argument("Argument less than 1")); |
| 780 | #ifndef UseExceptions |
| 781 | return 0; |
| 782 | #endif |
| 783 | } |
| 784 | |
| 785 | // version for z > 1 - Lanczos method |
| 786 | Real ln_gamma_lanczos(Real z) |
| 787 | { |
| 788 | Real zg5 = gamma_g_value() + z - 0.5; |
| 789 | return log(gamma_series(z - 1.0)) + (z - 0.5) * (log(zg5) - 1.0); |
| 790 | } |
| 791 | |
| 792 | //for z near 1 |
| 793 | Real ln_gamma_near_1(Real z) |
| 794 | { |
| 795 | long_Real z1 = z - 1; |
| 796 | return |
| 797 | z1 * (-0.57721566490153286061L |
| 798 | + z1 * ( 1.64493406684822643647L / 2 |
| 799 | + z1 * (-1.20205690315959428540L / 3 |
| 800 | + z1 * ( 1.08232323371113819152L / 4 |
| 801 | + z1 * (-1.03692775514336992633L / 5 |
| 802 | + z1 * ( 1.01734306198444913971L / 6 |
| 803 | + z1 * (-1.00834927738192282684L / 7 |
| 804 | + z1 * ( 1.00407735619794433938L / 8 |
| 805 | + z1 * (-1.00200838282608221442L / 9 |
| 806 | + z1 * ( 1.00099457512781808534L / 10 |
| 807 | + z1 * (-1.00049418860411946456L / 11 |
| 808 | + z1 * ( 1.00024608655330804830L / 12 |
| 809 | + z1 * (-1.00012271334757848915L / 13 |
| 810 | + z1 * ( 1.00006124813505870483L / 14 |
| 811 | + z1 * (-1.00003058823630702049L / 15 |
| 812 | + z1 * ( 1.00001528225940865187L / 16 |
| 813 | + z1 * (-1.00000763719763789976L / 17 |
| 814 | + z1 * ( 1.00000381729326499984L / 18 |
| 815 | + z1 * (-1.00000190821271655394L / 19 |
| 816 | + z1 * 1.00000095396203387280L / 20 ))))))))))))))))))); |
| 817 | } |
| 818 | |
| 819 | //for z near 2 |
| 820 | Real ln_gamma_near_2(Real z) |
| 821 | { |
| 822 | long_Real z1 = z - 2; |
| 823 | return |
| 824 | z1 * (1 - 0.57721566490153286061L |
| 825 | + z1 * ( 0.64493406684822643647L / 2 |
| 826 | + z1 * (-0.20205690315959428540L / 3 |
| 827 | + z1 * ( 0.08232323371113819152L / 4 |
| 828 | + z1 * (-0.03692775514336992633L / 5 |
| 829 | + z1 * ( 0.01734306198444913971L / 6 |
| 830 | + z1 * (-0.00834927738192282684L / 7 |
| 831 | + z1 * ( 0.00407735619794433938L / 8 |
| 832 | + z1 * (-0.00200838282608221442L / 9 |
| 833 | + z1 * ( 0.00099457512781808534L / 10 |
| 834 | + z1 * (-0.00049418860411946456L / 11 |
| 835 | + z1 * ( 0.00024608655330804830L / 12 |
| 836 | + z1 * (-0.00012271334757848915L / 13 |
| 837 | + z1 * ( 0.00006124813505870483L / 14 |
| 838 | + z1 * (-0.00003058823630702049L / 15))))))))))))))); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | Real ln_gamma(Real z, int& sign) |
| 843 | { |
| 844 | Tracer et("ln_gamma(sign)"); |
| 845 | if (z >= 2.1) { sign = 1; return ln_gamma_gt_1(z); } |
| 846 | if (z >= 1.9) { sign = 1; return ln_gamma_near_2(z); } |
| 847 | if (z >= 1.1) { sign = 1; return ln_gamma_gt_1(z); } |
| 848 | if (z >= 0.9) { sign = 1; return ln_gamma_near_1(z); } |
| 849 | else // Use reflection formula |
| 850 | { |
| 851 | Real cz = ceil(z); Real cz2 = cz / 2.0; |
| 852 | sign = fabs(ceil(cz2) - cz2) > 0.25 ? 1 : -1; |
| 853 | Real fz = cz - z; |
| 854 | if (fz == 0) Throw(Invalid_argument("Non-positive integer argument ")); |
| 855 | Real piz = 3.14159265358979323846 * (1.0 - z); |
| 856 | return |
| 857 | log(piz / sin(3.14159265358979323846 * fz)) - ln_gamma_gt_1(2.0 - z); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | Real ln_gamma(Real z) |
| 862 | { |
| 863 | Tracer et("ln_gamma"); |
| 864 | int sign; |
| 865 | Real lg = ln_gamma(z, sign); |
| 866 | if (sign < 0) Throw(Invalid_argument("Negative gamma value ")); |
| 867 | return lg; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | Real rbd_gamma(Real z) |
| 872 | { |
| 873 | Tracer et("rbd_gamma"); |
| 874 | if (z>=1.0) // Use reflection formula |
| 875 | { |
| 876 | Real zg5 = gamma_g_value() + z - 0.5; |
| 877 | return gamma_series(z - 1.0) |
| 878 | * pow(zg5 / 2.7182818284590452353, z - 0.5); |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | Real cz = ceil(z); Real cz2 = cz / 2.0; |
| 883 | Real fz = cz - z; |
| 884 | if (fz == 0) Throw(Invalid_argument("Non-positive integer argument ")); |
| 885 | Real piz = 3.14159265358979323846 * (1.0 - z); |
| 886 | Real v = (piz / sin(3.14159265358979323846 * fz)) / rbd_gamma(2.0 - z); |
| 887 | return fabs(ceil(cz2) - cz2) > 0.25 ? v : -v; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | |
| 892 | Real NegatedRandom::Next() { return - rv->Next(); } |
| 893 | |
| 894 | ExtReal NegatedRandom::Mean() const { return - rv->Mean(); } |
| 895 | |
| 896 | ExtReal NegatedRandom::Variance() const { return rv->Variance(); } |
| 897 | |
| 898 | Real ScaledRandom::Next() { return rv->Next() * s; } |
| 899 | |
| 900 | ExtReal ScaledRandom::Mean() const { return rv->Mean() * s; } |
| 901 | |
| 902 | ExtReal ScaledRandom::Variance() const { return rv->Variance() * (s*s); } |
| 903 | |
| 904 | Real ShiftedRandom::Next() { return rv->Next() + s; } |
| 905 | |
| 906 | ExtReal ShiftedRandom::Mean() const { return rv->Mean() + s; } |
| 907 | |
| 908 | ExtReal ShiftedRandom::Variance() const { return rv->Variance(); } |
| 909 | |
| 910 | Real ReverseShiftedRandom::Next() { return s - rv->Next(); } |
| 911 | |
| 912 | ExtReal ReverseShiftedRandom::Mean() const { return - rv->Mean() + s; } |
| 913 | |
| 914 | ExtReal ReverseShiftedRandom::Variance() const { return rv->Variance(); } |
| 915 | |
| 916 | Real ReciprocalRandom::Next() { return s / rv->Next(); } |
| 917 | |
| 918 | ExtReal RepeatedRandom::Mean() const { return rv->Mean() * (Real)n; } |
| 919 | |
| 920 | ExtReal RepeatedRandom::Variance() const { return rv->Variance() * (Real)n; } |
| 921 | |
| 922 | RepeatedRandom& Random::operator()(int n) |
| 923 | { |
| 924 | RepeatedRandom* r = new RepeatedRandom(*this, n); |
| 925 | if (!r) ErrorNoSpace(); return *r; |
| 926 | } |
| 927 | |
| 928 | NegatedRandom& operator-(Random& rv) |
| 929 | { |
| 930 | NegatedRandom* r = new NegatedRandom(rv); |
| 931 | if (!r) ErrorNoSpace(); return *r; |
| 932 | } |
| 933 | |
| 934 | ShiftedRandom& operator+(Random& rv, Real s) |
| 935 | { |
| 936 | ShiftedRandom* r = new ShiftedRandom(rv, s); |
| 937 | if (!r) ErrorNoSpace(); return *r; |
| 938 | } |
| 939 | |
| 940 | ShiftedRandom& operator-(Random& rv, Real s) |
| 941 | { |
| 942 | ShiftedRandom* r = new ShiftedRandom(rv, -s); |
| 943 | if (!r) ErrorNoSpace(); return *r; |
| 944 | } |
| 945 | |
| 946 | ScaledRandom& operator*(Random& rv, Real s) |
| 947 | { |
| 948 | ScaledRandom* r = new ScaledRandom(rv, s); |
| 949 | if (!r) ErrorNoSpace(); return *r; |
| 950 | } |
| 951 | |
| 952 | ShiftedRandom& operator+(Real s, Random& rv) |
| 953 | { |
| 954 | ShiftedRandom* r = new ShiftedRandom(rv, s); |
| 955 | if (!r) ErrorNoSpace(); return *r; |
| 956 | } |
| 957 | |
| 958 | ReverseShiftedRandom& operator-(Real s, Random& rv) |
| 959 | { |
| 960 | ReverseShiftedRandom* r = new ReverseShiftedRandom(rv, s); |
| 961 | if (!r) ErrorNoSpace(); return *r; |
| 962 | } |
| 963 | |
| 964 | ScaledRandom& operator*(Real s, Random& rv) |
| 965 | { |
| 966 | ScaledRandom* r = new ScaledRandom(rv, s); |
| 967 | if (!r) ErrorNoSpace(); return *r; |
| 968 | } |
| 969 | |
| 970 | ScaledRandom& operator/(Random& rv, Real s) |
| 971 | { |
| 972 | ScaledRandom* r = new ScaledRandom(rv, 1.0/s); |
| 973 | if (!r) ErrorNoSpace(); return *r; |
| 974 | } |
| 975 | |
| 976 | ReciprocalRandom& operator/(Real s, Random& rv) |
| 977 | { |
| 978 | ReciprocalRandom* r = new ReciprocalRandom(rv, s); |
| 979 | if (!r) ErrorNoSpace(); return *r; |
| 980 | } |
| 981 | |
| 982 | AddedRandom& operator+(Random& rv1, Random& rv2) |
| 983 | { |
| 984 | AddedRandom* r = new AddedRandom(rv1, rv2); |
| 985 | if (!r) ErrorNoSpace(); return *r; |
| 986 | } |
| 987 | |
| 988 | MultipliedRandom& operator*(Random& rv1, Random& rv2) |
| 989 | { |
| 990 | MultipliedRandom* r = new MultipliedRandom(rv1, rv2); |
| 991 | if (!r) ErrorNoSpace(); return *r; |
| 992 | } |
| 993 | |
| 994 | SubtractedRandom& operator-(Random& rv1, Random& rv2) |
| 995 | { |
| 996 | SubtractedRandom* r = new SubtractedRandom(rv1, rv2); |
| 997 | if (!r) ErrorNoSpace(); return *r; |
| 998 | } |
| 999 | |
| 1000 | DividedRandom& operator/(Random& rv1, Random& rv2) |
| 1001 | { |
| 1002 | DividedRandom* r = new DividedRandom(rv1, rv2); |
| 1003 | if (!r) ErrorNoSpace(); return *r; |
| 1004 | } |
| 1005 | |
| 1006 | Real AddedRandom::Next() |
| 1007 | { Real r1 = rv1->Next(); Real r2 = rv2->Next(); return r1 + r2; } |
| 1008 | |
| 1009 | ExtReal AddedRandom::Mean() const { return rv1->Mean() + rv2->Mean() ; } |
| 1010 | |
| 1011 | ExtReal AddedRandom::Variance() const |
| 1012 | { return rv1->Variance() + rv2->Variance() ; } |
| 1013 | |
| 1014 | Real SubtractedRandom::Next() |
| 1015 | { Real r1 = rv1->Next(); Real r2 = rv2->Next(); return r1 - r2; } |
| 1016 | |
| 1017 | ExtReal SubtractedRandom::Mean() const { return rv1->Mean() - rv2->Mean() ; } |
| 1018 | |
| 1019 | ExtReal SubtractedRandom::Variance() const |
| 1020 | { return rv1->Variance() + rv2->Variance() ; } |
| 1021 | |
| 1022 | Real MultipliedRandom::Next() |
| 1023 | { Real r1 = rv1->Next(); Real r2 = rv2->Next(); return r1 * r2; } |
| 1024 | |
| 1025 | ExtReal MultipliedRandom::Mean() const { return rv1->Mean() * rv2->Mean() ; } |
| 1026 | |
| 1027 | ExtReal MultipliedRandom::Variance() const |
| 1028 | { |
| 1029 | ExtReal e1 = square(rv1->Mean()); ExtReal e2 = square(rv2->Mean()); |
| 1030 | ExtReal v1 = rv1->Variance(); ExtReal v2 = rv2->Variance(); |
| 1031 | ExtReal r=v1*v2+v1*e2+e1*v2; |
| 1032 | return r; |
| 1033 | } |
| 1034 | |
| 1035 | Real DividedRandom::Next() |
| 1036 | { Real r1 = rv1->Next(); Real r2 = rv2->Next(); return r1 / r2; } |
| 1037 | |
| 1038 | void Random::load(int*,Real*,Random**) |
| 1039 | { Throw(Logic_error("Newran: illegal combination")); } |
| 1040 | |
| 1041 | void SelectedRandom::load(int* i, Real* probs, Random** rvx) |
| 1042 | { |
| 1043 | probs[*i]=p; rvx[*i]=rv; (*i)++; |
| 1044 | delete this; |
| 1045 | } |
| 1046 | |
| 1047 | Real SelectedRandom::Next() |
| 1048 | { |
| 1049 | Throw(Logic_error("Newran: Next not defined")); |
| 1050 | #ifndef UseExceptions |
| 1051 | return 0; |
| 1052 | #endif |
| 1053 | } |
| 1054 | |
| 1055 | Real AddedSelectedRandom::Next() |
| 1056 | { |
| 1057 | Throw(Logic_error("Newran: Next not defined")); |
| 1058 | #ifndef UseExceptions |
| 1059 | return 0; |
| 1060 | #endif |
| 1061 | } |
| 1062 | |
| 1063 | Real RepeatedRandom::Next() |
| 1064 | { Real sum=0.0; for (int i=0; i<n; i++) sum += rv->Next(); return sum; } |
| 1065 | |
| 1066 | MixedRandom::MixedRandom(int nx, Real* probs, Random** rvx) |
| 1067 | { |
| 1068 | n = nx; |
| 1069 | rv = new Random*[n]; if (!rv) ErrorNoSpace(); |
| 1070 | for (int i=0; i<n; i++) rv[i]=rvx[i]; |
| 1071 | Build(probs); |
| 1072 | } |
| 1073 | |
| 1074 | MixedRandom::MixedRandom(AddedSelectedRandom& sr) |
| 1075 | { |
| 1076 | n = sr.nelems(); // number of terms; |
| 1077 | Real* probs = new Real[n]; rv = new Random*[n]; |
| 1078 | if (!probs || !rv) ErrorNoSpace(); |
| 1079 | int i=0; sr.load(&i,probs,rv); |
| 1080 | Build(probs); delete [] probs; |
| 1081 | } |
| 1082 | |
| 1083 | void MixedRandom::Build(Real* probs) |
| 1084 | { |
| 1085 | int i; |
| 1086 | dg=new DiscreteGen(n,probs); |
| 1087 | if (!dg) ErrorNoSpace(); |
| 1088 | mean=0.0; var=0.0; |
| 1089 | for (i=0; i<n; i++) mean = mean + (rv[i])->Mean()*probs[i]; |
| 1090 | for (i=0; i<n; i++) |
| 1091 | { |
| 1092 | ExtReal sigsq=(rv[i])->Variance(); |
| 1093 | ExtReal mudif=(rv[i])->Mean()-mean; |
| 1094 | var = var + (sigsq+square(mudif))*probs[i]; |
| 1095 | } |
| 1096 | |
| 1097 | } |
| 1098 | |
| 1099 | MixedRandom::~MixedRandom() |
| 1100 | { |
| 1101 | for (int i=0; i<n; i++) rv[i]->tDelete(); |
| 1102 | delete dg; delete [] rv; |
| 1103 | } |
| 1104 | |
| 1105 | Real MixedRandom::Next() |
| 1106 | { int i = (int)(dg->Next()); return (rv[i])->Next(); } |
| 1107 | |
| 1108 | int AddedSelectedRandom::nelems() const |
| 1109 | { return rv1->nelems() + rv2->nelems(); } |
| 1110 | |
| 1111 | void AddedSelectedRandom::load(int* i, Real* probs, Random** rvx) |
| 1112 | { |
| 1113 | rv1->load(i, probs, rvx); rv2->load(i, probs, rvx); |
| 1114 | delete this; |
| 1115 | } |
| 1116 | |
| 1117 | AddedSelectedRandom& operator+(SelectedRandom& rv1, |
| 1118 | SelectedRandom& rv2) |
| 1119 | { |
| 1120 | AddedSelectedRandom* r = new AddedSelectedRandom(rv1, rv2); |
| 1121 | if (!r) ErrorNoSpace(); return *r; |
| 1122 | } |
| 1123 | |
| 1124 | AddedSelectedRandom& operator+(AddedSelectedRandom& rv1, |
| 1125 | SelectedRandom& rv2) |
| 1126 | { |
| 1127 | AddedSelectedRandom* r = new AddedSelectedRandom(rv1, rv2); |
| 1128 | if (!r) ErrorNoSpace(); return *r; |
| 1129 | } |
| 1130 | |
| 1131 | AddedSelectedRandom& operator+(SelectedRandom& rv1, |
| 1132 | AddedSelectedRandom& rv2) |
| 1133 | { |
| 1134 | AddedSelectedRandom* r = new AddedSelectedRandom(rv1, rv2); |
| 1135 | if (!r) ErrorNoSpace(); return *r; |
| 1136 | } |
| 1137 | |
| 1138 | AddedSelectedRandom& operator+(AddedSelectedRandom& rv1, |
| 1139 | AddedSelectedRandom& rv2) |
| 1140 | { |
| 1141 | AddedSelectedRandom* r = new AddedSelectedRandom(rv1, rv2); |
| 1142 | if (!r) ErrorNoSpace(); return *r; |
| 1143 | } |
| 1144 | |
| 1145 | SelectedRandom& Random::operator()(double p) |
| 1146 | { |
| 1147 | SelectedRandom* r = new SelectedRandom(*this, p); |
| 1148 | if (!r) ErrorNoSpace(); return *r; |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | // Identification routines for each class - may not work on all compilers? |
| 1155 | |
| 1156 | const char* Uniform::Name() { return "Uniform"; } |
| 1157 | const char* Constant::Name() { return "Constant"; } |
| 1158 | const char* PosGen::Name() { return "PosGen"; } |
| 1159 | const char* SymGen::Name() { return "SymGen"; } |
| 1160 | const char* AsymGen::Name() { return "AsymGen"; } |
| 1161 | const char* PosGenX::Name() { return "PosGenX"; } |
| 1162 | const char* SymGenX::Name() { return "SymGenX"; } |
| 1163 | const char* AsymGenX::Name() { return "AsymGenX"; } |
| 1164 | const char* Normal::Name() { return "Normal"; } |
| 1165 | const char* ChiSq::Name() { return "ChiSq"; } |
| 1166 | const char* Cauchy::Name() { return "Cauchy"; } |
| 1167 | const char* Exponential::Name() { return "Exponential"; } |
| 1168 | const char* Poisson::Name() { return "Poisson"; } |
| 1169 | const char* Binomial::Name() { return "Binomial"; } |
| 1170 | const char* NegativeBinomial::Name() { return "NegativeBinomial"; } |
| 1171 | const char* Gamma::Name() { return "Gamma"; } |
| 1172 | const char* Pareto::Name() { return "Pareto"; } |
| 1173 | const char* DiscreteGen::Name() { return "DiscreteGen"; } |
| 1174 | const char* SumRandom::Name() { return "SumRandom"; } |
| 1175 | const char* MixedRandom::Name() { return "MixedRandom"; } |
| 1176 | const char* Stable::Name() { return "Stable"; } |
| 1177 | const char* Extreme_Largest::Name() { return "Extreme Largest"; } |
| 1178 | const char* Weibull::Name() { return "Weibull"; } |
| 1179 | const char* Student::Name() { return "T locatio-scale"; } |
| 1180 | |
| 1181 | |
| 1182 | // ********************** permutation generator *************************** |
| 1183 | |
| 1184 | void RandomPermutation::Next(int N, int M, int p[], int start) |
| 1185 | { |
| 1186 | // N = size of urn; M = number of draws |
| 1187 | if (N < M) Throw(Logic_error("Newran: N < M in RandomPermutation")); |
| 1188 | int i; |
| 1189 | int* q = new int [N]; |
| 1190 | if (!q) ErrorNoSpace(); |
| 1191 | for (i = 0; i < N; i++) q[i] = start + i; |
| 1192 | for (i = 0; i < M; i++) |
| 1193 | { |
| 1194 | int k = i + (int)(U.Next() * (N - i)); // uniform on i ... N-1 |
| 1195 | p[i] = q[k]; q[k] = q[i]; // swap i and k terms |
| 1196 | // but put i-th term into p |
| 1197 | } |
| 1198 | delete [] q; |
| 1199 | } |
| 1200 | |
| 1201 | /// Shell sort from Sedgewick |
| 1202 | void ShellSortAscending(int* a, int N) |
| 1203 | { |
| 1204 | int h; |
| 1205 | for (h = 1; h <= N / 9; h = 3*h + 1) {} |
| 1206 | for (; h > 0; h /= 3) for (int i = h; i < N; ++i) |
| 1207 | { |
| 1208 | int v = a[i]; int j = i; |
| 1209 | while (j>=h && a[j-h] > v) { a[j] = a[j-h]; j-= h; } |
| 1210 | a[j] = v; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | void RandomCombination::SortAscending(int n, int gm[]) |
| 1215 | { |
| 1216 | ShellSortAscending(gm, n); |
| 1217 | } |
| 1218 | |
| 1219 | //***************** Generators with variable parameters ******************** |
| 1220 | |
| 1221 | VariPoisson::VariPoisson() : P100(100.0), P200(200.0) {} |
| 1222 | |
| 1223 | Real VariPoisson::Next_very_small(Real mu) |
| 1224 | { |
| 1225 | // mu is expected value of Poisson random number |
| 1226 | // Efficient when mu is small |
| 1227 | // generate a Poisson variable with mean mu |
| 1228 | if (mu == 0) return 0; |
| 1229 | Real t = exp(-mu); int k = 0; Real u = U.Next(); |
| 1230 | for (Real s = t; s < u; s += t) { ++k; t *= mu / k; } |
| 1231 | return k; |
| 1232 | } |
| 1233 | |
| 1234 | Real VariPoisson::Next_small(Real mu) |
| 1235 | { |
| 1236 | // mu is expected value of Poisson random number |
| 1237 | // Efficient when mu is reasonably small |
| 1238 | // generate a Poisson variable with mean mu |
| 1239 | // start iteration at mode |
| 1240 | if (mu < 10) return Next_very_small(mu); |
| 1241 | int k_start = (int)mu; Real u = U.Next(); |
| 1242 | Real t1 = exp(k_start * log(mu) - mu - ln_gamma(k_start+1)); |
| 1243 | if (t1 > u) return k_start; |
| 1244 | int k1 = k_start; int k2 = k_start; Real t2 = t1; Real s = t1; |
| 1245 | for(;;) |
| 1246 | { |
| 1247 | ++k1; t1 *= mu / k1; s += t1; if (s > u) return k1; |
| 1248 | if (k2 > 0) { t2 *= k2 / mu; --k2; s += t2; if (s > u) return k2; } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | Real VariPoisson::Next_large(Real mu) |
| 1253 | { |
| 1254 | // mu is expected value of Poisson random number |
| 1255 | // reasonably accurate when mu is large, but should try to improve |
| 1256 | // generate a Poisson variable with mean mu |
| 1257 | // method is to start with normal variable X and use X with prob 1/3 |
| 1258 | // and X**2 with prob 2/3. In each case X has mean and variance to |
| 1259 | // agree with Poisson rv after adjusting with Sheppard's correction |
| 1260 | |
| 1261 | const Real sc = 1.0 / 12.0; // Sheppard correction |
| 1262 | Real k; |
| 1263 | if (U.Next() > 1.0 / 3.0) |
| 1264 | { |
| 1265 | Real musc = 0.5 * (mu - sc); Real meansq = sqrt(mu * mu - musc); |
| 1266 | Real sigma = sqrt(musc / (mu + meansq)); |
| 1267 | k = floor(square(N.Next() * sigma + sqrt(meansq)) + 0.5); |
| 1268 | } |
| 1269 | else k = floor(N.Next() * sqrt(mu - sc) + mu + 0.5); |
| 1270 | return k < 0 ? 0 : k; |
| 1271 | } |
| 1272 | |
| 1273 | Real VariPoisson::Next(Real mu) |
| 1274 | { |
| 1275 | if (mu <= 0) |
| 1276 | { |
| 1277 | if (mu == 0) return 0; |
| 1278 | else Throw(Logic_error("Newran: illegal parameters")); |
| 1279 | } |
| 1280 | if (mu < 10) return Next_very_small(mu); |
| 1281 | else if (mu < 100) return Next_small(mu); |
| 1282 | else if (mu < 200) |
| 1283 | { |
| 1284 | // do in two statements so order of evaluation is preserved |
| 1285 | int i = (int)P100.Next(); |
| 1286 | return i + Next_small(mu - 100); |
| 1287 | } |
| 1288 | else if (mu < 300) |
| 1289 | { |
| 1290 | // do in two statements so order of evaluation is preserved |
| 1291 | int i = (int)P200.Next(); |
| 1292 | return i + Next_small(mu - 200); |
| 1293 | } |
| 1294 | else return Next_large(mu); |
| 1295 | } |
| 1296 | |
| 1297 | int VariPoisson::iNext(Real mu) |
| 1298 | { Tracer et("VariPoisson::iNext "); return Round(Next(mu)); } |
| 1299 | |
| 1300 | VariBinomial::VariBinomial() {} |
| 1301 | |
| 1302 | Real VariBinomial::Next_very_small(int n, Real p) |
| 1303 | { |
| 1304 | // Efficient when n * p is small |
| 1305 | // generate a Binomial variable with parameters n and p |
| 1306 | |
| 1307 | Real q = 1 - p; if (q == 0) return n; |
| 1308 | Real r = p / q; Real t = pow(q, n); Real s = t; |
| 1309 | Real u = U.Next(); |
| 1310 | for (int k = 0; k <= n; ++k) |
| 1311 | { |
| 1312 | if (s >= u) return k; |
| 1313 | t *= r * (Real)(n - k) / (Real)(k + 1); |
| 1314 | s += t; |
| 1315 | } |
| 1316 | return 0; // can happen only if we have round-off error |
| 1317 | } |
| 1318 | |
| 1319 | Real VariBinomial::Next_small(int n, Real p) |
| 1320 | { |
| 1321 | // Efficient when sqrt(n) * p is small |
| 1322 | // Assume p <= 1/2 |
| 1323 | // same as small but start at mode |
| 1324 | // generate a Binomial variable with parameters n and p |
| 1325 | |
| 1326 | Real q = 1 - p; Real r = p / q; Real u = U.Next(); |
| 1327 | int k_start = (int)( (n * r) / (r + 1) ); |
| 1328 | Real t1 = exp( ln_gamma(n+1) - ln_gamma(k_start+1) - ln_gamma(n-k_start+1) |
| 1329 | + k_start * log(p) + (n-k_start) * log(q) ); |
| 1330 | if (t1 >= u) return k_start; |
| 1331 | Real t2 = t1; Real s = t1; |
| 1332 | int k1 = k_start; int k2 = k_start; |
| 1333 | for (;;) |
| 1334 | { |
| 1335 | t1 *= r * (Real)(n - k1) / (Real)(k1 + 1); ++k1; s += t1; |
| 1336 | if (s >= u) return k1; |
| 1337 | if (k2) |
| 1338 | { |
| 1339 | --k2; t2 *= (Real)(k2 + 1) / (Real)(n - k2) / r; s += t2; |
| 1340 | if (s >= u) return k2; |
| 1341 | } |
| 1342 | else if (k1 == n) return 0; // can happen only if we have round-off error |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | Real VariBinomial::Next(int n, Real p) |
| 1347 | { |
| 1348 | if (p > 0.5) return n - Next(n, 1.0 - p); |
| 1349 | |
| 1350 | if (n <= 0 || p <= 0) |
| 1351 | { |
| 1352 | if (n < 0 || p < 0) Throw(Logic_error("Newran: illegal parameters")); |
| 1353 | else return 0; |
| 1354 | } |
| 1355 | |
| 1356 | Real mean = n * p; |
| 1357 | if (mean <= 10) return Next_very_small(n, p); |
| 1358 | else if (mean <= 200) return Next_small(n, p); |
| 1359 | else return Next_large(n,p); |
| 1360 | |
| 1361 | } |
| 1362 | |
| 1363 | // probably not sufficiently accurate |
| 1364 | Real VariBinomial::Next_large(int n, Real p) |
| 1365 | { |
| 1366 | const Real sc = 1.0 / 12.0; // Sheppard correction |
| 1367 | Real mean = n * p; |
| 1368 | Real sd = sqrt(mean * (1.0 - p) - sc); |
| 1369 | return floor(N.Next() * sd + mean + 0.5); |
| 1370 | } |
| 1371 | |
| 1372 | int VariBinomial::iNext(int n, Real p) |
| 1373 | { Tracer et("VariBinomial::iNext "); return Round(Next(n, p)); } |
| 1374 | |
| 1375 | |
| 1376 | Real VariLogNormal::Next(Real mean, Real sd) |
| 1377 | { |
| 1378 | // should have special version of log for small sd/mean |
| 1379 | Real n_var = log(1 + square(sd / mean)); |
| 1380 | return mean * exp(N.Next() * sqrt(n_var) - 0.5 * n_var); |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | #ifdef use_namespace |
| 1389 | } |
| 1390 | #endif |
| 1391 | |
| 1392 | ///@} |
| 1393 |