1 2 /* Web Polygraph http://www.web-polygraph.org/ 3 * Copyright 2003-2011 The Measurement Factory 4 * Licensed under the Apache License, Version 2.0 */ 5 6 #ifndef POLYGRAPH__XSTD_RNDDISTRS_H 7 #define POLYGRAPH__XSTD_RNDDISTRS_H 8 9 #include "xstd/Rnd.h" 10 11 // constant(mean) distribution 12 class ConstDistr: public RndDistr 13 { 14 public: ConstDistr(RndGen * aGen,double aMean)15 ConstDistr(RndGen *aGen, double aMean): RndDistr(aGen), theMean(aMean) {} 16 pdfName()17 virtual const char *pdfName() const { return "const"; } 18 mean()19 virtual double mean() const { return theMean; } sdev()20 virtual double sdev() const { return 0; } 21 trial()22 virtual double trial() { return theMean; } 23 24 protected: 25 double theMean; 26 }; 27 28 29 // uniform[lo,hi) distribution 30 class UnifDistr: public RndDistr 31 { 32 public: 33 UnifDistr(RndGen *aGen, double aLo, double aHi); 34 pdfName()35 virtual const char *pdfName() const { return "unif"; } 36 mean()37 virtual double mean() const { return (theLo+theHi)/2; } sdev()38 virtual double sdev() const { return range()/sqrt(12.); } range()39 double range() const { return theHi-theLo; } 40 trial()41 virtual double trial() { return theLo + range()*theGen->trial(); } 42 43 virtual ostream &print(ostream &os, ArgPrinter p = &RndDistr_DefArgPrinter) const; 44 45 protected: 46 double theLo; 47 double theHi; 48 }; 49 50 // exponential(mean) distribution 51 class ExpDistr: public RndDistr 52 { 53 public: ExpDistr(RndGen * aGen,double aMean)54 ExpDistr(RndGen *aGen, double aMean): RndDistr(aGen), theMean(aMean) {} 55 pdfName()56 virtual const char *pdfName() const { return "exp"; } 57 mean()58 virtual double mean() const { return theMean; } sdev()59 virtual double sdev() const { return theMean; } 60 61 virtual double trial(); 62 63 protected: 64 double theMean; 65 }; 66 67 // normal(mean, sdev) distribution 68 class NormDistr: public RndDistr 69 { 70 public: NormDistr(RndGen * aGen,double aMean,double aSDev)71 NormDistr(RndGen *aGen, double aMean, double aSDev): RndDistr(aGen), theMean(aMean), theSDev(aSDev) {} 72 pdfName()73 virtual const char *pdfName() const { return "norm"; } 74 mean()75 virtual double mean() const { return theMean; } sdev()76 virtual double sdev() const { return theSDev; } 77 78 virtual double trial(); 79 80 virtual ostream &print(ostream &os, ArgPrinter p = &RndDistr_DefArgPrinter) const; 81 82 protected: 83 double theMean; 84 double theSDev; 85 }; 86 87 // lognormal(mu, sigmaSq) distribution 88 class LognDistr: public NormDistr 89 { 90 public: 91 // creates using mean/sdev 92 static LognDistr *ViaMean(RndGen *aGen, double aMean, double aSDev); 93 // creates using mu/sigma 94 static LognDistr *ViaMu(RndGen *aGen, double aMu, double aSigmaSq); 95 96 public: pdfName()97 virtual const char *pdfName() const { return "logn"; } 98 99 virtual double mean() const; 100 virtual double sdev() const; 101 102 virtual double trial(); 103 104 virtual ostream &print(ostream &os, ArgPrinter p = &RndDistr_DefArgPrinter) const; 105 106 protected: 107 LognDistr(RndGen *generator, double aMu, double aSigmaSq, double aMean, double aSDev); 108 109 protected: 110 double theRealMean; 111 double theRealSDev; 112 }; 113 114 // classic Zipf(#objects) distribution (exponent == 1) 115 class ZipfDistr: public RndDistr 116 { 117 public: ZipfDistr(RndGen * aGen,int aWorldCap)118 ZipfDistr(RndGen *aGen, int aWorldCap): RndDistr(aGen), theWorldCap(aWorldCap) {} 119 pdfName()120 virtual const char *pdfName() const { return "zipf"; } 121 mean()122 virtual double mean() const { return -1; } // infinite mean sdev()123 virtual double sdev() const { return 0; } // infinite dev? 124 double omega() const; 125 126 virtual double trial(); 127 int ltrial(int min, int max); 128 129 virtual ostream &print(ostream &os, ArgPrinter p = &RndDistr_DefArgPrinter) const; 130 131 protected: 132 int theWorldCap; 133 }; 134 135 // generates sequential ids: 1, 2, 3, ... 136 class SeqDistr: public RndDistr 137 { 138 public: 139 SeqDistr(RndGen *aGen, int aFirstId = 1); 140 pdfName()141 virtual const char *pdfName() const { return "seq"; } 142 mean()143 virtual double mean() const { return -1; } // infinite mean sdev()144 virtual double sdev() const { return 0; } // infinite dev? 145 146 virtual double trial(); 147 148 virtual ostream &print(ostream &os, ArgPrinter p = &RndDistr_DefArgPrinter) const; 149 150 protected: 151 int theFirstId; 152 int theLastId; 153 }; 154 155 #endif 156