| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file tryrand1.cpp |
| 5 | /// Test of newran - histograms. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | |
| 10 | #include "include.h" |
| 11 | #include "newran.h" |
| 12 | #include "format.h" |
| 13 | #include "utility.h" |
| 14 | #include "tryrand.h" |
| 15 | |
| 16 | |
| 17 | |
| 18 | Real phi(Real x) // normal density |
| 19 | { return (fabs(x)>8.0) ? 0 : 0.398942280 * exp(-x*x / 2); } |
| 20 | |
| 21 | Real NORMAL10(Real x) { return 0.5*phi(0.5*(x-10.0)); } |
| 22 | |
| 23 | Real UNIF(Real x) { return (x>=0.0 && x<=1.0) ? 1.0 : 0.0; } |
| 24 | |
| 25 | Real TRIANG(Real x) { return (x>1.0) ? 0.0 : 1.0-x; } |
| 26 | |
| 27 | |
| 28 | |
| 29 | void test1(int n) |
| 30 | { |
| 31 | Normal nn; |
| 32 | Uniform uniform; |
| 33 | cout << |
| 34 | "Print 20 N(0,1) random numbers - should be the same as in sample output" << |
| 35 | endl; |
| 36 | { |
| 37 | Format F; F.FormatType(Format::DEC_FIGS); F.Precision(12); F.Width(15); |
| 38 | for (int i=0; i<20; i++) cout << F << nn.Next() << endl; |
| 39 | } |
| 40 | cout << endl; |
| 41 | |
| 42 | cout << "Print histograms of data from a variety distributions" << endl; |
| 43 | cout << "Histograms should be close to those in sample output" << endl; |
| 44 | cout << "s. mean and s. var should be close to p. mean and s. mean" << endl << endl; |
| 45 | |
| 46 | { Constant c(5.0); Histogram(&c, n); } |
| 47 | { Uniform u; Histogram(&u, n); } |
| 48 | { SumRandom sr=uniform(3)-1.5; Histogram(&sr, n); } |
| 49 | { SumRandom sr=uniform-uniform; Histogram(&sr, n); } |
| 50 | { Normal normal; Histogram(&normal, n); } |
| 51 | { Cauchy cauchy; Histogram(&cauchy, n); } |
| 52 | { AsymGenX normal10(NORMAL10, 10.0); Histogram(&normal10, n); } |
| 53 | cout << "Mean and variance should be 10.0 and 4.0" << endl; |
| 54 | { AsymGenX uniform2(UNIF,0.5); Histogram(&uniform2, n); } |
| 55 | cout << "Mean and variance should be 0.5 and 0.083333" << endl; |
| 56 | { SymGenX triang(TRIANG); Histogram(&triang, n); } |
| 57 | cout << "Mean and variance should be 0 and 0.16667" << endl; |
| 58 | { Poisson p(0.25); Histogram(&p, n); } |
| 59 | { Poisson p(10.0); Histogram(&p, n); } |
| 60 | { Poisson p(16.0); Histogram(&p, n); } |
| 61 | { Binomial b(18,0.3); Histogram(&b, n); } |
| 62 | { Binomial b(19,0.3); Histogram(&b, n); } |
| 63 | { Binomial b(20,0.3); Histogram(&b, n); } |
| 64 | { Binomial b(58,0.3); Histogram(&b, n); } |
| 65 | { Binomial b(59,0.3); Histogram(&b, n); } |
| 66 | { Binomial b(60,0.3); Histogram(&b, n); } |
| 67 | { Binomial b(18,0.05); Histogram(&b, n); } |
| 68 | { Binomial b(19,0.05); Histogram(&b, n); } |
| 69 | { Binomial b(20,0.05); Histogram(&b, n); } |
| 70 | { Binomial b(98,0.01); Histogram(&b, n); } |
| 71 | { Binomial b(99,0.01); Histogram(&b, n); } |
| 72 | { Binomial b(100,0.01); Histogram(&b, n); } |
| 73 | { Binomial b(18,0.95); Histogram(&b, n); } |
| 74 | { Binomial b(19,0.95); Histogram(&b, n); } |
| 75 | { Binomial b(20,0.95); Histogram(&b, n); } |
| 76 | { Binomial b(98,0.99); Histogram(&b, n); } |
| 77 | { Binomial b(99,0.99); Histogram(&b, n); } |
| 78 | { Binomial b(100,0.99); Histogram(&b, n); } |
| 79 | { NegativeBinomial nb(100,6.0); Histogram(&nb, n); } |
| 80 | { NegativeBinomial nb(11,9.0); Histogram(&nb, n); } |
| 81 | { NegativeBinomial nb(11,1.9); Histogram(&nb, n); } |
| 82 | { NegativeBinomial nb(11,0.10); Histogram(&nb, n); } |
| 83 | { NegativeBinomial nb(1.5,1.9); Histogram(&nb, n); } |
| 84 | { NegativeBinomial nb(1.0,1.9); Histogram(&nb, n); } |
| 85 | { NegativeBinomial nb(0.3,19); Histogram(&nb, n); } |
| 86 | { NegativeBinomial nb(0.3,1.9); Histogram(&nb, n); } |
| 87 | { NegativeBinomial nb(0.3,0.05); Histogram(&nb, n); } |
| 88 | { NegativeBinomial nb(100.8,0.18); Histogram(&nb, n); } |
| 89 | { ChiSq c(1,2.0); Histogram(&c, n); } |
| 90 | { ChiSq c(2,2.0); Histogram(&c, n); } |
| 91 | { ChiSq c(3,2.0); Histogram(&c, n); } |
| 92 | { ChiSq c(4,2.0); Histogram(&c, n); } |
| 93 | { ChiSq c(1 ); Histogram(&c, n); } |
| 94 | { ChiSq c(2 ); Histogram(&c, n); } |
| 95 | { ChiSq c(3 ); Histogram(&c, n); } |
| 96 | { ChiSq c(4 ); Histogram(&c, n); } |
| 97 | { Gamma g1(1.0); Histogram(&g1, n); } |
| 98 | { Gamma g2(0.5); Histogram(&g2, n); } |
| 99 | { Gamma g3(1.01); Histogram(&g3, n); } |
| 100 | { Gamma g4(2.0); Histogram(&g4, n); } |
| 101 | { Pareto p1(0.5); Histogram(&p1, n); } |
| 102 | { Pareto p2(1.5); Histogram(&p2, n); } |
| 103 | { Pareto p3(2.5); Histogram(&p3, n); } |
| 104 | { Pareto p4(4.5); Histogram(&p4, n); } |
| 105 | Real probs[]={.1,.3,.05,.11,.05,.04,.05,.05,.1,.15}; |
| 106 | Real val[]={2,3,4,6,8,12,16,24,32,48}; |
| 107 | { DiscreteGen discrete(10,probs); Histogram(&discrete, n); } |
| 108 | { DiscreteGen discrete(10,probs,val); Histogram(&discrete, n); } |
| 109 | } |
| 110 | |
| 111 | ///@} |