Debian Codegraph @

src/ITGSend/newran/tryrand5.cpp

raw symbols SCIP
1 /// \ingroup newran
2 ///@{
3
4 /// \file tryrand5.cpp
5 /// Test of newran - "Vari-" generators.
6
7 #define WANT_STREAM
8 #define WANT_MATH
9
10 #include "include.h"
11 #include "newran.h"
12 #include "format.h"
13 #include "tryrand.h"
14 #include "utility.h"
15
16
17
18 // ****** simple tests of Poisson, binomial and log normal random number generators ******
19
20 void TestVariPoisson(Real mu, int N)
21 {
22 VariPoisson VP; // Poisson RNG
23 Real sum = 0.0, sumsq = 0.0;
24 for (int i = 1; i <= N; ++i)
25 { int x = VP.iNext(mu); Real d = x - mu; sum += d; sumsq += d * d; }
26 Real p_mean = mu;
27 Real p_var = mu;
28 Real s_mean = mu + sum / N;
29 Real s_var = (sumsq - sum * sum / N) / (N - 1);
30 Format F1;
31 F1.FormatType(Format::SIG_FIGS); F1.Precision(5); F1.Width(10); F1.Suffix(" ");
32 Format F2;
33 F2.FormatType(Format::DEC_FIGS); F2.Precision(5); F2.Width(10); F2.Suffix(" ");
34 cout << F1 << p_mean << s_mean;
35 cout << F2 << (s_mean - p_mean) / sqrt(p_var / N);
36 cout << F1 << p_var << s_var;
37 cout << F2 << s_var / p_var;
38 cout << endl;
39 }
40
41 void TestVariBinomial(int n, Real p, int N)
42 {
43 VariBinomial VB; // Binomial RNG
44 Real sum = 0.0, sumsq = 0.0;
45 Real mu = n * p;
46 for (int i = 1; i <= N; ++i)
47 { int x = VB.iNext(n, p); Real d = x - mu; sum += d; sumsq += d * d; }
48 Real p_mean = mu;
49 Real p_var = mu * (1.0 - p);
50 Real s_mean = mu + sum / N;
51 Real s_var = (sumsq - sum * sum / N) / (N - 1);
52 Format F1;
53 F1.FormatType(Format::SIG_FIGS); F1.Precision(5); F1.Width(10); F1.Suffix(" ");
54 Format F2;
55 F2.FormatType(Format::DEC_FIGS); F2.Precision(5); F2.Width(10); F2.Suffix(" ");
56 cout << F1 << p_mean << s_mean;
57 cout << F2 << (s_mean - p_mean) / sqrt(p_var / N);
58 cout << F1 << p_var << s_var;
59 cout << F2 << s_var / p_var;
60 cout << endl;
61 }
62
63 void TestVariLogNormal(double mean, double sd, int N)
64 {
65 VariLogNormal VLN;
66 Real sum = 0.0, sumsq = 0.0;
67 for (int i = 1; i <= N; ++i)
68 {
69 Real x = VLN.Next(mean, sd);
70 Real d = x - mean; sum += d; sumsq += d * d;
71 }
72 Real p_mean = mean;
73 Real p_var = sd * sd;
74 Real s_mean = mean + sum / N;
75 Real s_var = (sumsq - sum * sum / N) / (N - 1);
76 Format F1;
77 F1.FormatType(Format::SIG_FIGS); F1.Precision(5); F1.Width(10); F1.Suffix(" ");
78 Format F2;
79 F2.FormatType(Format::DEC_FIGS); F2.Precision(5); F2.Width(10); F2.Suffix(" ");
80 cout << F1 << p_mean << s_mean;
81 cout << F2 << (s_mean - p_mean) / sqrt(p_var / N);
82 cout << F1 << p_var << s_var;
83 cout << F2 << s_var / p_var;
84 cout << endl;
85 }
86
87 void test5(int N)
88 {
89 cout << endl << endl;
90
91 cout << "Testing VariPoisson" << endl;
92 cout << endl;
93 cout << "No formal tests yet." << endl;
94 cout << "Norm. diff. should be between -2 and 2 in most cases." << endl;
95 cout << "Var ratio should be close to 1." << endl;
96 cout << endl;
97 cout <<
98 " pop. mean smpl. mean norm diff pop. var smpl. var var ratio" <<
99 endl;
100
101 TestVariPoisson(0.25, N);
102 TestVariPoisson(1, N);
103 TestVariPoisson(4, N);
104 TestVariPoisson(10, N);
105 TestVariPoisson(20, N);
106 TestVariPoisson(30, N);
107 TestVariPoisson(39.5, N);
108 TestVariPoisson(40, N);
109 TestVariPoisson(50, N);
110 TestVariPoisson(59.5, N);
111 TestVariPoisson(60, N);
112 TestVariPoisson(60.5, N);
113 TestVariPoisson(99.5, N);
114 TestVariPoisson(100, N);
115 TestVariPoisson(100.5, N);
116 TestVariPoisson(199.5, N);
117 TestVariPoisson(200, N);
118 TestVariPoisson(200.5, N);
119 TestVariPoisson(299.5, N);
120 TestVariPoisson(300, N);
121 TestVariPoisson(300.5, N);
122 TestVariPoisson(399.5, N);
123 TestVariPoisson(400, N);
124 TestVariPoisson(400.5, N);
125 TestVariPoisson(500, N);
126 TestVariPoisson(10000, N);
127 TestVariPoisson(10000.5, N);
128 TestVariPoisson(100000, N);
129 TestVariPoisson(100000.5, N);
130 TestVariPoisson(10000000, N);
131 TestVariPoisson(10000000.5, N);
132
133 cout << endl;
134 cout << "Testing VariBinomial" << endl;
135 cout << endl;
136 cout << "No formal tests yet." << endl;
137 cout << "Norm. diff. should be between -2 and 2 in most cases." << endl;
138 cout << "Var ratio should be close to 1." << endl;
139 cout << endl;
140 cout <<
141 " pop. mean smpl. mean norm diff pop. var smpl. var var ratio" <<
142 endl;
143
144 TestVariBinomial(1, 0.2, N);
145 TestVariBinomial(2, 0.1, N);
146 TestVariBinomial(5, 0.35, N);
147 TestVariBinomial(20, 0.2, N);
148 TestVariBinomial(50, 0.5, N);
149 TestVariBinomial(100, 0.4, N);
150 TestVariBinomial(200, 0.2, N);
151 TestVariBinomial(500, 0.3, N);
152 TestVariBinomial(1000, 0.19, N);
153 TestVariBinomial(1000, 0.21, N);
154 TestVariBinomial(10000, 0.4, N);
155 TestVariBinomial(1000000, 0.1, N);
156 TestVariBinomial(1000000, 0.5, N);
157 TestVariBinomial(1, 0.7, N);
158 TestVariBinomial(2, 0.6, N);
159 TestVariBinomial(5, 0.9, N);
160 TestVariBinomial(20, 0.55, N);
161 TestVariBinomial(50, 0.9, N);
162 TestVariBinomial(100, 0.99, N);
163 TestVariBinomial(200, 0.51, N);
164 TestVariBinomial(500, 0.7, N);
165 TestVariBinomial(1000, 0.81, N);
166 TestVariBinomial(1000, 0.79, N);
167 TestVariBinomial(10000, 0.9, N);
168 TestVariBinomial(1000000, 0.55, N);
169 TestVariBinomial(1000000, 0.7, N);
170
171 cout << endl;
172 cout << "Testing VariLogNormal" << endl;
173 cout << endl;
174 cout << "No formal tests yet." << endl;
175 cout << "Norm. diff. should be between -2 and 2 in most cases." << endl;
176 cout << "Var ratio should be close to 1." << endl;
177 cout << endl;
178 cout <<
179 " pop. mean smpl. mean norm diff pop. var smpl. var var ratio" <<
180 endl;
181
182 TestVariLogNormal(0.25, 0.5, N);
183 TestVariLogNormal( 0.5, 1.5, N);
184 TestVariLogNormal( 1.5, 2.5, N);
185 TestVariLogNormal( 2.0, 1.0, N);
186 TestVariLogNormal( 5.0, 0.1, N);
187
188 cout << endl;
189 }
190
191 ///@}
192