Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

newran.h

Go to the documentation of this file.
00001 // newran.h ------------------------------------------------------------
00002 
00003 // NEWRAN02
00004 
00005 #ifndef NEWRAN_LIB
00006 #define NEWRAN_LIB 0
00007 // #define MONITOR
00008 
00009 //******************* utilities and definitions *************************
00010 
00011 #include "include.h"
00012 #include "myexcept.h"
00013 #include "extreal.h"
00014 
00015 #ifdef use_namespace
00016 namespace NEWRAN { using namespace RBD_COMMON; }
00017 namespace RBD_LIBRARIES { using namespace NEWRAN; }
00018 namespace NEWRAN {
00019 #endif
00020 
00021 typedef Real (*PDF)(Real);                // probability density function
00022 
00023 Real ln_gamma(Real);                      // log gamma function
00024 
00025 //**************** uniform random number generator **********************
00026 
00027 class RepeatedRandom;
00028 class SelectedRandom;
00029 
00030 class Random                              // uniform random number generator
00031 {
00032    static double seed;                    // seed
00033    //static unsigned long iseed;          // for Mother
00034    Real Buffer[128];               // for mixing random numbers
00035    Real Raw();                     // unmixed random numbers
00036    void operator=(const Random&) {}       // private so can't access
00037 
00038 public:
00039    static void Set(double s);             // set seed (0 < seed < 1)
00040    double Get();                   // get seed
00041    virtual Real Next();                   // get new value
00042    virtual char* Name();                  // identification
00043    virtual Real Density(Real) const;      // used by PosGen & Asymgen
00044    Random();                            // do nothing
00045    virtual ~Random() {}                   // make destructors virtual
00046    SelectedRandom& operator()(double);    // used by MixedRandom
00047    RepeatedRandom& operator()(int);       // for making combinations
00048    virtual ExtReal Mean() const { return 0.5; }
00049                                           // mean of distribution
00050    virtual ExtReal Variance() const { return 1.0/12.0; }
00051                                           // variance of distribution
00052    virtual void tDelete() {}              // delete components of sum
00053    virtual int nelems() const { return 1; }
00054                                           // used by MixedRandom
00055    virtual void load(int*,Real*,Random**);
00056    friend class RandomPermutation;
00057 
00058    virtual Random *Getrv( void )        {       return 0;       }
00059 };
00060 
00061 
00062 //****************** uniform random number generator *********************
00063 
00064 class Uniform : public Random
00065 {
00066    void operator=(const Uniform&) {}      // private so can't access
00067 
00068 public:
00069    char* Name();                          // identification
00070    Uniform() {}                           // set value
00071    Real Next() { return Random::Next(); }
00072    ExtReal Mean() const { return 0.5; }
00073    ExtReal Variance() const { return 1.0/12.0; }
00074    Real Density(Real x) const { return (x < 0.0 || x > 1.0) ? 0 : 1.0; }
00075 };
00076 
00077 
00078 //************************* return constant ******************************
00079 
00080 class Constant : public Random
00081 {
00082    void operator=(const Constant&) {}     // private so can't access
00083    Real value;                            // value to be returned
00084 
00085 public:
00086    char* Name();                          // identification
00087    Constant(Real v) { value=v; }          // set value
00088    Real Next() { return value; }
00089    ExtReal Mean() const { return value; }
00090    ExtReal Variance() const { return 0.0; }
00091 };
00092 
00093 //**************** positive random number generator **********************
00094 
00095 class PosGen : public Random              // generate positive rv
00096 {
00097    void operator=(const PosGen&) {}       // private so can't access
00098 
00099 protected:
00100    Real xi, *sx, *sfx;
00101    bool NotReady;
00102    void Build(bool);                      // called on first call to Next
00103 
00104 public:
00105    char* Name();                          // identification
00106    PosGen();                              // constructor
00107    virtual ~PosGen();                             // destructor
00108    Real Next();                           // to get a single new value
00109    ExtReal Mean() const { return (ExtReal)Missing; }
00110    ExtReal Variance() const { return (ExtReal)Missing; }
00111 };
00112 
00113 //**************** symmetric random number generator **********************
00114 
00115 class SymGen : public PosGen              // generate symmetric rv
00116 {
00117    void operator=(const SymGen&) {}       // private so can't access
00118 
00119 public:
00120    char* Name();                          // identification
00121    Real Next();                           // to get a single new value
00122 };
00123 
00124 //**************** normal random number generator **********************
00125 
00126 class Normal : public SymGen              // generate standard normal rv
00127 {
00128    void operator=(const Normal&) {}       // private so can't access
00129    static Real Nxi, *Nsx, *Nsfx;          // so we need initialise only once
00130    static long count;                     // assume initialised to 0
00131 
00132 public:
00133    char* Name();                          // identification
00134    Normal();
00135    virtual ~Normal();
00136    Real Density(Real) const;              // normal density function
00137    ExtReal Mean() const { return 0.0; }
00138    ExtReal Variance() const { return 1.0; }
00139 };
00140 
00141 //*********** chi-square random number generator **********************
00142 
00143 class ChiSq : public Random               // generate non-central chi-sq rv
00144 {
00145    void operator=(const ChiSq&) {}        // private so can't access
00146    Random* c1;                            // pointers to generators
00147    Random* c2;                            // pointers to generators
00148    int version;                           // indicates method of generation
00149    Real mean, var;
00150 
00151 public:
00152    char* Name();                          // identification
00153    ChiSq(int, Real=0.0);                  // df and non-centrality parameter
00154    virtual ~ChiSq();
00155    ExtReal Mean() const { return mean; }
00156    ExtReal Variance() const { return var; }
00157    Real Next();
00158 };
00159 
00160 //**************** Cauchy random number generator **********************
00161 
00162 class Cauchy : public SymGen              // generate standard cauchy rv
00163 {
00164    void operator=(const Cauchy&) {}       // private so can't access
00165 
00166 public:
00167    char* Name();                          // identification
00168    Real Density(Real) const;              // Cauchy density function
00169    ExtReal Mean() const { return Indefinite; }
00170    ExtReal Variance() const { return PlusInfinity; }
00171 };
00172 
00173 //**************** Exponential random number generator **********************
00174 
00175 class Exponential : public PosGen         // generate standard exponential rv
00176 {
00177    void operator=(const Exponential&) {}  // private so can't access
00178 
00179 public:
00180    char* Name();                          // identification
00181    Real Density(Real) const;              // Exponential density function
00182    ExtReal Mean() const { return 1.0; }
00183    ExtReal Variance() const { return 1.0; }
00184 };
00185 
00186 //**************** asymmetric random number generator **********************
00187 
00188 class AsymGen : public Random             // generate asymmetric rv
00189 {
00190    void operator=(const AsymGen&) {}      // private so can't access
00191    Real xi, *sx, *sfx; int ic;
00192    bool NotReady;
00193    void Build();                          // called on first call to Next
00194 
00195 protected:
00196    Real mode;
00197 
00198 public:
00199    char* Name();                          // identification
00200    AsymGen(Real);                         // constructor (Real=mode)
00201    virtual ~AsymGen();                            // destructor
00202    Real Next();                           // to get a single new value
00203    ExtReal Mean() const { return (ExtReal)Missing; }
00204    ExtReal Variance() const { return (ExtReal)Missing; }
00205 };
00206 
00207 //**************** Gamma random number generator **********************
00208 
00209 class Gamma : public Random               // generate gamma rv
00210 {
00211    void operator=(const Gamma&) {}        // private so can't access
00212    Random* method;
00213 
00214 public:
00215    char* Name();                          // identification
00216    Gamma(Real);                           // constructor (Real=shape)
00217    virtual ~Gamma() 
00218    { 
00219    #ifdef MONITOR
00220       tron << "destructing Gamma\n";
00221    #endif
00222            delete method; 
00223    }
00224    Real Next() { return method->Next(); }
00225    ExtReal Mean() const { return method->Mean(); }
00226    ExtReal Variance() const { return method->Variance(); }
00227 };
00228 
00229 //**************** Generators with pointers to pdf ***********************
00230 
00231 class PosGenX : public PosGen
00232 {
00233    void operator=(const PosGenX&) {}      // private so can't access
00234    PDF f;
00235 
00236 public:
00237    char* Name();                          // identification
00238    PosGenX(PDF fx);
00239    Real Density(Real x) const { return (*f)(x); }
00240 };
00241 
00242 class SymGenX : public SymGen
00243 {
00244    void operator=(const SymGenX&) {}      // private so can't access
00245    PDF f;
00246 
00247 public:
00248    char* Name();                          // identification
00249    SymGenX(PDF fx);
00250    Real Density(Real x) const { return (*f)(x); }
00251 };
00252 
00253 class AsymGenX : public AsymGen
00254 {
00255    void operator=(const AsymGenX&) {}     // private so can't access
00256    PDF f;
00257 
00258 public:
00259    char* Name();                          // identification
00260    AsymGenX(PDF fx, Real mx);
00261    Real Density(Real x) const { return (*f)(x); }
00262 };
00263 
00264 //***************** Pareto random number generator ************************
00265 
00266 class Pareto : public Random
00267 // Use definition of Kotz and Johnson: "Continuous univariate distributions 1",
00268 // chapter 19 with k = 1.
00269 {
00270    void operator=(const Pareto&) {}       // private so can't access
00271    Real Shape, RS;
00272 
00273 public:
00274    char* Name();                          // identification
00275    Pareto(Real shape);                    // constructor (Real=shape)
00276    virtual ~Pareto() {}
00277    Real Next();
00278    ExtReal Mean() const;
00279    ExtReal Variance() const;
00280 };
00281 
00282 
00283 //**************** discrete random number generator **********************
00284 
00285 class DiscreteGen : public Random         // discrete random generator
00286 {
00287    void operator=(const DiscreteGen&) {}  // private so can't access
00288    Real *p; int *ialt; int n; Real *val;
00289    void Gen(int, Real*);                  // called by constructors
00290    Real mean, var;                        // calculated by the constructor
00291 
00292 public:
00293    char* Name();                          // identification
00294    DiscreteGen(int,Real*);                // constructor
00295    DiscreteGen(int,Real*,Real*);          // constructor
00296    virtual ~DiscreteGen();                        // destructor
00297    Real Next();                           // new single value
00298    ExtReal Mean() const { return mean; }
00299    ExtReal Variance() const { return var; }
00300 };
00301 
00302 //***************** Poisson random number generator *******************
00303 
00304 class Poisson : public Random             // generate poisson rv
00305 {
00306    void operator=(const Poisson&) {}      // private so can't access
00307    Random* method;
00308 
00309 public:
00310    char* Name();                          // identification
00311    Poisson(Real);                         // constructor (Real=mean)
00312    virtual ~Poisson() 
00313    { 
00314    #ifdef MONITOR
00315       tron << "destructing Poisson\n";
00316    #endif
00317            delete method; 
00318    }
00319    Real Next() { return method->Next(); }
00320    ExtReal Mean() const { return method->Mean(); }
00321    ExtReal Variance() const { return method->Variance(); }
00322 };
00323 
00324 //***************** binomial random number generator *******************
00325 
00326 class Binomial : public Random            // generate binomial rv
00327 {
00328    void operator=(const Binomial&) {}     // private so can't access
00329    Random* method;
00330 
00331 public:
00332    char* Name();                          // identification
00333    Binomial(int p, Real n);               // constructor (int=n, Real=p)
00334    virtual ~Binomial() 
00335    { 
00336    #ifdef MONITOR
00337       tron << "destructing Binomial\n";
00338    #endif
00339            delete method; 
00340    }
00341    Real Next() { return method->Next(); }
00342    ExtReal Mean() const { return method->Mean(); }
00343    ExtReal Variance() const { return method->Variance(); }
00344 };
00345 
00346 //************** negative binomial random number generator *****************
00347 
00348 class NegativeBinomial : public AsymGen   // generate negative binomial rv
00349 {
00350    Real N, P, Q, p, ln_q, c;
00351 
00352 public:
00353    char* Name();
00354    NegativeBinomial(Real NX, Real PX);    // constructor
00355    Real Density(Real) const;              // Negative binomial density function
00356    Real Next();
00357    ExtReal Mean() const { return N * P; }
00358    ExtReal Variance() const { return N * P * Q; }
00359 };
00360 
00361 
00362 
00363 //************************ sum of random numbers ************************
00364 
00365 class NegatedRandom : public Random
00366 {
00367 protected:
00368    Random* rv;
00369    void tDelete() 
00370    { 
00371    #ifdef MONITOR
00372       tron << "destructing NegatedRandom\n";
00373    #endif
00374            rv->tDelete(); delete this; 
00375    }
00376 
00377    NegatedRandom(Random& rvx) : rv(&rvx) {}
00378 
00379 public:
00380    Real Next();
00381    ExtReal Mean() const;
00382    ExtReal Variance() const;
00383    virtual Random *Getrv( void )        {       return rv;      }
00384    friend NegatedRandom& operator-(Random&);
00385 };
00386 
00387 class ScaledRandom : public Random
00388 {
00389 protected:
00390    Random* rv; Real s;
00391    ScaledRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}
00392    void tDelete() 
00393    { 
00394    #ifdef MONITOR
00395       tron << "destructing ScaledRandom\n";
00396    #endif
00397            rv->tDelete(); delete this; 
00398    }
00399 
00400 public:
00401    Real Next();
00402    ExtReal Mean() const;
00403    ExtReal Variance() const;
00404    friend ScaledRandom& operator*(Real, Random&);
00405    friend ScaledRandom& operator*(Random&, Real);
00406    friend ScaledRandom& operator/(Random&, Real);
00407 };
00408 
00409 class ReciprocalRandom : public Random
00410 {
00411 protected:
00412    Random* rv; Real s;
00413    ReciprocalRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}
00414    void tDelete() 
00415    { 
00416    #ifdef MONITOR
00417       tron << "destructing ReciprocalRandom\n";
00418    #endif
00419            rv->tDelete(); delete this; 
00420    }
00421 
00422 public:
00423    Real Next();
00424    ExtReal Mean() const { return Missing; }
00425    ExtReal Variance() const { return Missing; }
00426    friend ReciprocalRandom& operator/(Real, Random&);
00427 };
00428 
00429 class ShiftedRandom : public ScaledRandom
00430 {
00431    ShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}
00432 
00433 public:
00434    Real Next();
00435    ExtReal Mean() const;
00436    ExtReal Variance() const;
00437    friend ShiftedRandom& operator+(Real, Random&);
00438    friend ShiftedRandom& operator+(Random&, Real);
00439    friend ShiftedRandom& operator-(Random&, Real);
00440 };
00441 
00442 class ReverseShiftedRandom : public ScaledRandom
00443 {
00444    ReverseShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}
00445 
00446 public:
00447    Real Next();
00448    ExtReal Mean() const;
00449    ExtReal Variance() const;
00450    friend ReverseShiftedRandom& operator-(Real, Random&);
00451 };
00452 
00453 class RepeatedRandom : public Random
00454 {
00455    Random* rv; int n;
00456    void tDelete() 
00457    { 
00458    #ifdef MONITOR
00459       tron << "destructing RepeatedRandom\n";
00460    #endif
00461            rv->tDelete(); delete this; 
00462    }
00463    RepeatedRandom(Random& rvx, int nx)  : rv(&rvx), n(nx) {}
00464 
00465 public:
00466    Real Next();
00467    ExtReal Mean() const;
00468    ExtReal Variance() const;
00469    friend class Random;
00470 };
00471 
00472 class MultipliedRandom : public Random
00473 {
00474 protected:
00475    Random* rv1; Random* rv2;
00476    void tDelete() 
00477    { 
00478    #ifdef MONITOR
00479       tron << "destructing MultipliedRandom\n";
00480    #endif
00481            rv1->tDelete(); rv2->tDelete(); delete this; 
00482    }
00483    MultipliedRandom(Random& rv1x, Random& rv2x) : rv1(&rv1x), rv2(&rv2x) {}
00484 
00485 public:
00486    Real Next();
00487    ExtReal Mean() const;
00488    ExtReal Variance() const;
00489    friend MultipliedRandom& operator*(Random&, Random&);
00490 };
00491 
00492 class AddedRandom : public MultipliedRandom
00493 {
00494    AddedRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) {}
00495 
00496 public:
00497    Real Next();
00498    ExtReal Mean() const;
00499    ExtReal Variance() const;
00500    friend AddedRandom& operator+(Random&, Random&);
00501 };
00502 
00503 class OredRandom : public MultipliedRandom
00504 {
00505    OredRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) , i( 0 ) {}
00506 
00507    int  i;
00508 public:
00509    Real Next();
00510    ExtReal Mean() const;
00511    ExtReal Variance() const;
00512    friend OredRandom& operator|(Random&, Random&);
00513 };
00514 
00515 class SubtractedRandom : public MultipliedRandom
00516 {
00517    SubtractedRandom(Random& rv1x, Random& rv2x)
00518       : MultipliedRandom(rv1x, rv2x) {}
00519 
00520 public:
00521    Real Next();
00522    ExtReal Mean() const;
00523    ExtReal Variance() const;
00524    friend SubtractedRandom& operator-(Random&, Random&);
00525 };
00526 
00527 class DividedRandom : public MultipliedRandom
00528 {
00529    DividedRandom(Random& rv1x, Random& rv2x)
00530       : MultipliedRandom(rv1x, rv2x) {}
00531 
00532 public:
00533    Real Next();
00534    ExtReal Mean() const { return Missing; }
00535    ExtReal Variance() const { return Missing; }
00536    friend DividedRandom& operator/(Random&, Random&);
00537 };
00538 
00539 class SumRandom : public Random           // sum of random variables
00540 {
00541    void operator=(const SumRandom&) {}    // private so can't access
00542    Random* rv;
00543 
00544 public:
00545    char* Name();                          // identification
00546    SumRandom( Random &rvx ) : rv(&rvx) {}
00547    SumRandom(NegatedRandom& rvx) : rv(&rvx) {}
00548    SumRandom(AddedRandom& rvx) : rv(&rvx) {}
00549    SumRandom(MultipliedRandom& rvx) : rv(&rvx) {}
00550    SumRandom(SubtractedRandom& rvx) : rv(&rvx) {}
00551    SumRandom(DividedRandom& rvx) : rv(&rvx) {}
00552    SumRandom(ShiftedRandom& rvx) : rv(&rvx) {}
00553    SumRandom(ReverseShiftedRandom& rvx) : rv(&rvx) {}
00554    SumRandom(ScaledRandom& rvx) : rv(&rvx) {}
00555    SumRandom(ReciprocalRandom& rvx) : rv(&rvx) {}
00556    SumRandom(RepeatedRandom& rvx) : rv(&rvx) {}
00557    Real Next() 
00558    { 
00559            return rv->Next(); 
00560    }
00561    ExtReal Mean() const { return rv->Mean(); }
00562    ExtReal Variance() const { return rv->Variance(); }
00563    virtual ~SumRandom() 
00564    { 
00565    #ifdef MONITOR
00566       tron << "destructing SumRandom\n";
00567    #endif
00568            rv->tDelete();
00569            rv = 0;
00570    }
00571 };
00572 
00573 //******************** mixtures of random numbers ************************
00574 
00575 
00576 class SelectedRandom : public Random
00577 {
00578    friend class Random;
00579    Random* rv; Real p;
00580    SelectedRandom(Random& rvx, Real px) : rv(&rvx), p(px) {}
00581    void tDelete() 
00582    { 
00583    #ifdef MONITOR
00584       tron << "destructing SelectedRandom\n";
00585    #endif
00586            rv->tDelete(); delete this; 
00587    }
00588 
00589 public:
00590    void load(int*, Real*, Random**);
00591    Real Next();
00592 };
00593 
00594 class AddedSelectedRandom : public Random
00595 {
00596    friend class Random;
00597 
00598 protected:
00599    Random* rv1; Random* rv2;
00600    AddedSelectedRandom(Random& rv1x, Random& rv2x)
00601       : rv1(&rv1x), rv2(&rv2x) {}
00602    void tDelete() { 
00603    #ifdef MONITOR
00604       tron << "destructing AddedSelectedRandom\n";
00605    #endif
00606            rv1->tDelete(); rv2->tDelete(); 
00607            rv1 = 0;
00608            rv2 = 0;
00609            delete this; 
00610    }
00611 
00612 public:
00613    int nelems() const;
00614    void load(int*, Real*, Random**);
00615    Real Next();
00616    friend AddedSelectedRandom& operator+(SelectedRandom&,
00617       SelectedRandom&);
00618    friend AddedSelectedRandom& operator+(AddedSelectedRandom&,
00619       SelectedRandom&);
00620    friend AddedSelectedRandom& operator+(SelectedRandom&,
00621       AddedSelectedRandom&);
00622    friend AddedSelectedRandom& operator+(AddedSelectedRandom&,
00623       AddedSelectedRandom&);
00624 };
00625 
00626 class MixedRandom : public Random         // mixtures of random numbers
00627 {
00628    void operator=(const MixedRandom&) {}  // private so can't access
00629    int n;                                 // number of components
00630    DiscreteGen* dg;                       // discrete mixing distribution
00631    Random** rv;                           // array of pointers to rvs
00632    ExtReal mean, var;
00633    void Build(Real*);                     // used by constructors
00634 
00635 public:
00636    char* Name();                          // identification
00637    MixedRandom(int, Real*, Random**);
00638    MixedRandom(AddedSelectedRandom&);
00639    virtual ~MixedRandom();
00640    Real Next();
00641    ExtReal Mean() const { return mean; }
00642    ExtReal Variance() const { return var; }
00643 };
00644 
00645 //******************* Permutation generator *******************************
00646 
00647 class RandomPermutation                   // generate permutation of integers
00648 {
00649    Random U;
00650 
00651 public:
00652    void Next(int N, int M, int p[], int start = 0);
00653                                           // select permutation of M numbers
00654                                           // from start, ..., start+N-1
00655                                           // results to p
00656    void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }
00657 };
00658 
00659 class RandomCombination : public RandomPermutation
00660                                           // generate combination of integers
00661                                           // sorted - ascending
00662 {
00663    void SortAscending(int n, int gm[]);
00664 
00665 public:
00666    void Next(int N, int M, int p[], int start = 0)
00667       { RandomPermutation::Next(N, M, p, start); SortAscending(M, p); }
00668    void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }
00669 };
00670 
00671 #include <vector>
00672 
00673 class OredVectRandom : public std::vector<Random *> , public Random     {
00674 private:
00675         int     ati;
00676 public:
00677         OredVectRandom( void ) : ati( -1 )      {}
00678         ~OredVectRandom( void )
00679         {}
00680 
00681    Real Next()
00682    {
00683            if( ++ati >= (int)size() )
00684                    ati = 0;
00685            return at( ati )->Next();
00686    }
00687 
00688    ExtReal Mean() const { return 0.0; }
00689    ExtReal Variance() const { return 0.0; }
00690 };
00691 
00692 
00693 
00694 #ifdef use_namespace
00695 }
00696 #endif
00697 
00698 #endif

Generated on Fri Dec 5 04:06:00 2003 for Borqueror by doxygen 1.3.3