1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.random;
18
19 import org.hipparchus.distribution.RealDistribution;
20
21 import java.util.SplittableRandom;
22
23 public abstract class AbstractNonUniformGeneratorTest
24 {
25
26
27 private static final int SAMPLE_SIZE = 100000;
28
29
30
31
32
33
34
35 protected abstract NonUniformGenerator buildGenerator(RandomGenerator uniform, double... parameters);
36
37
38
39
40
41
42 protected abstract RealDistribution buildDistribution(double... parameters);
43
44 protected double ksStatistic(final long seed, double... parameters)
45 {
46
47
48 final SplittableRandom seedsGenerator = new SplittableRandom(seed);
49 final RandomGenerator uniform = new Well19937a(seedsGenerator.nextLong());
50 final NonUniformGenerator generator = buildGenerator(uniform, parameters);
51 final RealDistribution distribution = buildDistribution(parameters);
52
53 final KolmogorovSmirnovTest ksTest = new KolmogorovSmirnovTest();
54
55
56 final double[] data = new double[SAMPLE_SIZE];
57 for (int i = 0; i < SAMPLE_SIZE; ++i)
58 {
59 data[i] = generator.nextVariate();
60 }
61
62 return ksTest.kolmogorovSmirnovStatistic(distribution, data);
63
64 }
65
66 protected double mean(final long seed, double... parameters)
67 {
68 final NonUniformGenerator generator = buildGenerator(new Well19937a(seed), parameters);
69 double sum = 0.0;
70 for (int i = 0; i < SAMPLE_SIZE; ++i)
71 {
72 sum += generator.nextVariate();
73 }
74 return sum / SAMPLE_SIZE;
75 }
76
77 protected double variance(final long seed, double... parameters)
78 {
79
80 final NonUniformGenerator generator = buildGenerator(new Well19937a(seed), parameters);
81 double m = 0;
82 double s = 0;
83 for (int i = 0; i < SAMPLE_SIZE; ++i)
84 {
85 final double x = generator.nextVariate();
86 final double oldM = m;
87 m += (x - m) / (i + 1);
88 s += (x - m) * (x - oldM);
89 }
90 return s / (SAMPLE_SIZE - 1);
91 }
92
93 }