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 import org.hipparchus.distribution.continuous.GammaDistribution;
21 import org.hipparchus.exception.LocalizedCoreFormats;
22 import org.hipparchus.exception.MathIllegalStateException;
23 import org.junit.jupiter.api.Assertions;
24 import org.junit.jupiter.api.Test;
25
26 import java.util.SplittableRandom;
27
28 public class GammaGeneratorTest extends AbstractNonUniformGeneratorTest
29 {
30
31
32 @Override
33 protected NonUniformGenerator buildGenerator(final RandomGenerator uniform,
34 final double... parameters)
35 {
36 return new GammaGenerator(uniform, parameters[0], parameters[1]);
37 }
38
39
40 @Override
41 protected RealDistribution buildDistribution(double... parameters)
42 {
43 return new GammaDistribution(parameters[0], parameters[1]);
44 }
45
46 @Test
47 public void testDistributionGreaterThan1()
48 {
49 Assertions.assertEquals(0.0, ksStatistic(0x0678dad954a74560L, 3.0, 2.0), 2.1e-3);
50 }
51
52 @Test
53 public void testDistributionSmallerThan1()
54 {
55 Assertions.assertEquals(0.0, ksStatistic(0x35a7b28cf4ddc7c7L, 0.25, 3.0), 2.0e-3);
56 }
57
58 @Test
59 public void testMean()
60 {
61 final SplittableRandom seedsGenerator = new SplittableRandom(0x79e52d38915419bbL);
62 for (double alpha = 0.75; alpha < 5.0; alpha += 0.125)
63 {
64 for (double theta = 1.0; theta < 4.0; theta += 0.5)
65 {
66 final double mean = mean(seedsGenerator.nextLong(), alpha, theta);
67 final double theoretical = alpha * theta;
68 Assertions.assertEquals(theoretical, mean, 0.0078 * theoretical);
69 }
70 }
71 }
72
73 @Test
74 public void testVariance()
75 {
76 final SplittableRandom seedsGenerator = new SplittableRandom(0x14fec21eec8186ccL);
77 for (double alpha = 0.75; alpha < 5.0; alpha += 0.125)
78 {
79 for (double theta = 1.0; theta < 4.0; theta += 0.5)
80 {
81 final double variance = variance(seedsGenerator.nextLong(), alpha, theta);
82 final double theoretical = alpha * theta * theta;
83 Assertions.assertEquals(theoretical, variance, 0.021 * theoretical);
84 }
85 }
86 }
87
88 @Test
89 public void testMaxRejection()
90 {
91 try {
92 new GammaGenerator(new RandomAdaptorTest.ConstantGenerator(1.23), 2.3, 4.5).nextVariate();
93 Assertions.fail("an exception should have been thrown");
94 } catch (MathIllegalStateException mise) {
95 Assertions.assertEquals(LocalizedCoreFormats.INTERNAL_ERROR, mise.getSpecifier());
96 }
97 }
98
99 }