1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.sampling.distribution;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import org.apache.commons.rng.UniformRandomProvider;
23 import org.apache.commons.rng.sampling.RandomAssert;
24
25
26
27
28 public final class ContinuousSamplersList {
29
30 private static final List<ContinuousSamplerTestData> LIST = new ArrayList<>();
31
32 static {
33 try {
34
35
36
37
38
39 org.apache.commons.math3.random.RandomGenerator unusedRng = null;
40
41
42
43
44 final double meanNormal = -123.45;
45 final double sigmaNormal = 6.789;
46 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
47 RandomAssert.createRNG());
48
49 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
50 new BoxMullerGaussianSampler(RandomAssert.createRNG(), meanNormal, sigmaNormal));
51
52 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
53 GaussianSampler.of(new BoxMullerNormalizedGaussianSampler(RandomAssert.createRNG()),
54 meanNormal, sigmaNormal));
55
56 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
57 GaussianSampler.of(new MarsagliaNormalizedGaussianSampler(RandomAssert.createRNG()),
58 meanNormal, sigmaNormal));
59
60 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
61 GaussianSampler.of(new ZigguratNormalizedGaussianSampler(RandomAssert.createRNG()),
62 meanNormal, sigmaNormal));
63
64 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, meanNormal, sigmaNormal),
65 GaussianSampler.of(ZigguratSampler.NormalizedGaussian.of(RandomAssert.createRNG()),
66 meanNormal, sigmaNormal));
67
68
69 final double alphaBeta = 4.3;
70 final double betaBeta = 2.1;
71 add(LIST, new org.apache.commons.math3.distribution.BetaDistribution(unusedRng, alphaBeta, betaBeta),
72 RandomAssert.createRNG());
73
74 add(LIST, new org.apache.commons.math3.distribution.BetaDistribution(unusedRng, alphaBeta, betaBeta),
75 ChengBetaSampler.of(RandomAssert.createRNG(), alphaBeta, betaBeta));
76 add(LIST, new org.apache.commons.math3.distribution.BetaDistribution(unusedRng, betaBeta, alphaBeta),
77 ChengBetaSampler.of(RandomAssert.createRNG(), betaBeta, alphaBeta));
78
79 final double alphaBetaAlt = 0.5678;
80 final double betaBetaAlt = 0.1234;
81 add(LIST, new org.apache.commons.math3.distribution.BetaDistribution(unusedRng, alphaBetaAlt, betaBetaAlt),
82 ChengBetaSampler.of(RandomAssert.createRNG(), alphaBetaAlt, betaBetaAlt));
83 add(LIST, new org.apache.commons.math3.distribution.BetaDistribution(unusedRng, betaBetaAlt, alphaBetaAlt),
84 ChengBetaSampler.of(RandomAssert.createRNG(), betaBetaAlt, alphaBetaAlt));
85
86
87 final double medianCauchy = 0.123;
88 final double scaleCauchy = 4.5;
89 add(LIST, new org.apache.commons.math3.distribution.CauchyDistribution(unusedRng, medianCauchy, scaleCauchy),
90 RandomAssert.createRNG());
91
92
93 final int dofChi2 = 12;
94 add(LIST, new org.apache.commons.math3.distribution.ChiSquaredDistribution(unusedRng, dofChi2),
95 RandomAssert.createRNG());
96
97
98 final double meanExp = 3.45;
99 add(LIST, new org.apache.commons.math3.distribution.ExponentialDistribution(unusedRng, meanExp),
100 RandomAssert.createRNG());
101
102 add(LIST, new org.apache.commons.math3.distribution.ExponentialDistribution(unusedRng, meanExp),
103 AhrensDieterExponentialSampler.of(RandomAssert.createRNG(), meanExp));
104
105 add(LIST, new org.apache.commons.math3.distribution.ExponentialDistribution(unusedRng, meanExp),
106 ZigguratSampler.Exponential.of(RandomAssert.createRNG(), meanExp));
107
108
109 final int numDofF = 4;
110 final int denomDofF = 7;
111 add(LIST, new org.apache.commons.math3.distribution.FDistribution(unusedRng, numDofF, denomDofF),
112 RandomAssert.createRNG());
113
114
115 final double alphaGammaSmallerThanOne = 0.1234;
116 final double alphaGammaLargerThanOne = 2.345;
117 final double thetaGamma = 3.456;
118 add(LIST, new org.apache.commons.math3.distribution.GammaDistribution(unusedRng, alphaGammaLargerThanOne, thetaGamma),
119 RandomAssert.createRNG());
120
121 add(LIST, new org.apache.commons.math3.distribution.GammaDistribution(unusedRng, alphaGammaSmallerThanOne, thetaGamma),
122 AhrensDieterMarsagliaTsangGammaSampler.of(RandomAssert.createRNG(),
123 alphaGammaSmallerThanOne, thetaGamma));
124
125 add(LIST, new org.apache.commons.math3.distribution.GammaDistribution(unusedRng, alphaGammaLargerThanOne, thetaGamma),
126 AhrensDieterMarsagliaTsangGammaSampler.of(RandomAssert.createRNG(),
127 alphaGammaLargerThanOne, thetaGamma));
128
129
130 final double muGumbel = -4.56;
131 final double betaGumbel = 0.123;
132 add(LIST, new org.apache.commons.math3.distribution.GumbelDistribution(unusedRng, muGumbel, betaGumbel),
133 RandomAssert.createRNG());
134
135
136 final double muLaplace = 12.3;
137 final double betaLaplace = 5.6;
138 add(LIST, new org.apache.commons.math3.distribution.LaplaceDistribution(unusedRng, muLaplace, betaLaplace),
139 RandomAssert.createRNG());
140
141
142 final double muLevy = -1.098;
143 final double cLevy = 0.76;
144 add(LIST, new org.apache.commons.math3.distribution.LevyDistribution(unusedRng, muLevy, cLevy),
145 RandomAssert.createRNG());
146
147 add(LIST, new org.apache.commons.math3.distribution.LevyDistribution(unusedRng, muLevy, cLevy),
148 LevySampler.of(RandomAssert.createRNG(), muLevy, cLevy));
149 add(LIST, new org.apache.commons.math3.distribution.LevyDistribution(unusedRng, 0.0, 1.0),
150 LevySampler.of(RandomAssert.createRNG(), 0.0, 1.0));
151
152
153 final double muLogNormal = 2.345;
154 final double sigmaLogNormal = 0.1234;
155 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal, sigmaLogNormal),
156 RandomAssert.createRNG());
157
158 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal, sigmaLogNormal),
159 new BoxMullerLogNormalSampler(RandomAssert.createRNG(), muLogNormal, sigmaLogNormal));
160
161 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal, sigmaLogNormal),
162 LogNormalSampler.of(new BoxMullerNormalizedGaussianSampler(RandomAssert.createRNG()),
163 muLogNormal, sigmaLogNormal));
164
165 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal, sigmaLogNormal),
166 LogNormalSampler.of(new MarsagliaNormalizedGaussianSampler(RandomAssert.createRNG()),
167 muLogNormal, sigmaLogNormal));
168
169 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal, sigmaLogNormal),
170 LogNormalSampler.of(new ZigguratNormalizedGaussianSampler(RandomAssert.createRNG()),
171 muLogNormal, sigmaLogNormal));
172
173 final double muLogNormal2 = -1.1;
174 final double sigmaLogNormal2 = 2.3;
175 add(LIST, new org.apache.commons.math3.distribution.LogNormalDistribution(unusedRng, muLogNormal2, sigmaLogNormal2),
176 LogNormalSampler.of(new ZigguratNormalizedGaussianSampler(RandomAssert.createRNG()),
177 muLogNormal2, sigmaLogNormal2));
178
179
180 final double muLogistic = -123.456;
181 final double sLogistic = 7.89;
182 add(LIST, new org.apache.commons.math3.distribution.LogisticDistribution(unusedRng, muLogistic, sLogistic),
183 RandomAssert.createRNG());
184
185
186 final double muNakagami = 78.9;
187 final double omegaNakagami = 23.4;
188 final double inverseAbsoluteAccuracyNakagami = org.apache.commons.math3.distribution.NakagamiDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
189 add(LIST, new org.apache.commons.math3.distribution.NakagamiDistribution(unusedRng, muNakagami, omegaNakagami, inverseAbsoluteAccuracyNakagami),
190 RandomAssert.createRNG());
191
192
193 final double scalePareto = 23.45;
194 final double shapePareto = 0.1234;
195 add(LIST, new org.apache.commons.math3.distribution.ParetoDistribution(unusedRng, scalePareto, shapePareto),
196 RandomAssert.createRNG());
197
198 add(LIST, new org.apache.commons.math3.distribution.ParetoDistribution(unusedRng, scalePareto, shapePareto),
199 InverseTransformParetoSampler.of(RandomAssert.createRNG(), scalePareto, shapePareto));
200
201 final double shapePareto2 = 12.34;
202 add(LIST, new org.apache.commons.math3.distribution.ParetoDistribution(unusedRng, scalePareto, shapePareto2),
203 InverseTransformParetoSampler.of(RandomAssert.createRNG(), scalePareto, shapePareto2));
204
205
206
207 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, 0, Math.sqrt(2)),
208 StableSampler.of(RandomAssert.createRNG(), 2, 0));
209 add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(unusedRng, 3.4, 0.75 * Math.sqrt(2)),
210 StableSampler.of(RandomAssert.createRNG(), 2, 0, 0.75, 3.4));
211
212 add(LIST, new org.apache.commons.math3.distribution.CauchyDistribution(unusedRng, 0.87, 2.73),
213 StableSampler.of(RandomAssert.createRNG(), 1, 0, 2.73, 0.87));
214
215
216
217
218 add(LIST, new org.apache.commons.math3.distribution.LevyDistribution(unusedRng, -1.23 - 5.7, 5.7),
219 StableSampler.of(RandomAssert.createRNG(), 0.5, 1.0, 5.7, -1.23));
220
221
222 add(LIST, new org.apache.commons.math3.distribution.LevyDistribution(unusedRng, -1.0, 1.0),
223 StableSampler.of(RandomAssert.createRNG(), 0.5, 1.0, 1.0, 0.0));
224
225
226
227
228
229 add(LIST, new double[] {-8.95069776039550, -7.89186827865320, -7.25070352695719, -6.71497820795024,
230 -6.19542020516881, -5.63245847779003, -4.94643432673952, -3.95462242999135,
231 -1.90020994991840, Double.POSITIVE_INFINITY},
232 StableSampler.of(RandomAssert.createRNG(), 1.3, 0.4, 1.5, -6.4));
233
234 add(LIST, new double[] {-1.60557902637291, 1.45715153372767, 2.39577970333297, 2.86274746879986,
235 3.15907259287483, 3.38633464572309, 3.60858199662215, 3.96001854555454, 5.16261950198042,
236 Double.POSITIVE_INFINITY},
237 StableSampler.of(RandomAssert.createRNG(), 0.8, -0.3, 0.75, 3.25));
238
239 add(LIST, new double[] {-2.08189340389400, -0.990511737972781, -0.539025554211755, -0.204710171216492,
240 0.120388569770401, 0.497197960523146, 1.01228394387185, 1.89061920660563, 4.20559140293206,
241 Double.POSITIVE_INFINITY},
242 StableSampler.of(RandomAssert.createRNG(), 1.0, 0.3));
243
244 add(LIST, new double[] {-2.29713832179280, -1.26781259700375, -0.739212223404616, -0.346771353386198,
245 0.00000000000000, 0.346771353386198, 0.739212223404616, 1.26781259700376, 2.29713832179280,
246 Double.POSITIVE_INFINITY},
247 StableSampler.of(RandomAssert.createRNG(), 1.3, 0.0));
248
249
250
251 add(LIST, new double[] {-14345498.0855558, -4841.68845914421, -22.6430159400915, -0.194461655962062,
252 0.299822962206354E-1, 0.316768853375197E-1, 0.519382255860847E-1, 21.8595769961580,
253 147637.033822552, Double.POSITIVE_INFINITY},
254 StableSampler.of(RandomAssert.createRNG(), 0.1, -0.2));
255
256
257 final double dofT = 0.76543;
258 add(LIST, new org.apache.commons.math3.distribution.TDistribution(unusedRng, dofT),
259 RandomAssert.createRNG());
260
261 add(LIST, new org.apache.commons.math3.distribution.TDistribution(unusedRng, dofT),
262 TSampler.of(RandomAssert.createRNG(), dofT));
263
264 final double dofTlarge = 30;
265 add(LIST, new org.apache.commons.math3.distribution.TDistribution(unusedRng, dofTlarge),
266 TSampler.of(RandomAssert.createRNG(), dofTlarge));
267
268
269
270 final double dofTHuge = 1e15;
271 add(LIST, new double[] {-1.2815515655446015, -0.84162123357291463, -0.52440051270804089,
272 -0.25334710313579983, 0, 0.25334710313579983, 0.52440051270804089, 0.84162123357291474,
273 1.2815515655446015, Double.POSITIVE_INFINITY},
274 TSampler.of(RandomAssert.createRNG(), dofTHuge));
275
276
277 final double aTriangle = -0.76543;
278 final double cTriangle = -0.65432;
279 final double bTriangle = -0.54321;
280 add(LIST, new org.apache.commons.math3.distribution.TriangularDistribution(unusedRng, aTriangle, cTriangle, bTriangle),
281 RandomAssert.createRNG());
282
283
284 final double loUniform = -1.098;
285 final double hiUniform = 0.76;
286 add(LIST, new org.apache.commons.math3.distribution.UniformRealDistribution(unusedRng, loUniform, hiUniform),
287 RandomAssert.createRNG());
288
289 add(LIST, new org.apache.commons.math3.distribution.UniformRealDistribution(unusedRng, loUniform, hiUniform),
290 ContinuousUniformSampler.of(RandomAssert.createRNG(), loUniform, hiUniform));
291
292
293 final double alphaWeibull = 678.9;
294 final double betaWeibull = 98.76;
295 add(LIST, new org.apache.commons.math3.distribution.WeibullDistribution(unusedRng, alphaWeibull, betaWeibull),
296 RandomAssert.createRNG());
297 } catch (Exception e) {
298
299 System.err.println("Unexpected exception while creating the list of samplers: " + e);
300 e.printStackTrace(System.err);
301
302 throw new RuntimeException(e);
303 }
304 }
305
306
307
308
309 private ContinuousSamplersList() {}
310
311
312
313
314
315
316 private static void add(List<ContinuousSamplerTestData> list,
317 final org.apache.commons.math3.distribution.RealDistribution dist,
318 UniformRandomProvider rng) {
319 final ContinuousSampler inverseMethodSampler =
320 InverseTransformContinuousSampler.of(rng,
321 new ContinuousInverseCumulativeProbabilityFunction() {
322 @Override
323 public double inverseCumulativeProbability(double p) {
324 return dist.inverseCumulativeProbability(p);
325 }
326 @Override
327 public String toString() {
328 return dist.toString();
329 }
330 });
331 list.add(new ContinuousSamplerTestData(inverseMethodSampler,
332 getDeciles(dist)));
333 }
334
335
336
337
338
339
340 private static void add(List<ContinuousSamplerTestData> list,
341 final org.apache.commons.math3.distribution.RealDistribution dist,
342 final ContinuousSampler sampler) {
343 list.add(new ContinuousSamplerTestData(sampler,
344 getDeciles(dist)));
345 }
346
347
348
349
350
351
352 private static void add(List<ContinuousSamplerTestData> list,
353 final double[] deciles,
354 final ContinuousSampler sampler) {
355 list.add(new ContinuousSamplerTestData(sampler,
356 deciles));
357 }
358
359
360
361
362
363
364
365 public static Iterable<ContinuousSamplerTestData> list() {
366 return Collections.unmodifiableList(LIST);
367 }
368
369
370
371
372
373 private static double[] getDeciles(org.apache.commons.math3.distribution.RealDistribution dist) {
374 final int last = 9;
375 final double[] deciles = new double[last];
376 final double ten = 10;
377 for (int i = 0; i < last; i++) {
378 deciles[i] = dist.inverseCumulativeProbability((i + 1) / ten);
379 }
380 return deciles;
381 }
382 }