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.NakagamiDistribution;
21 import org.hipparchus.special.Gamma;
22 import org.hipparchus.util.FastMath;
23 import org.junit.jupiter.api.Assertions;
24 import org.junit.jupiter.api.Test;
25
26 import java.util.SplittableRandom;
27
28 public class NakagamiGeneratorTest extends AbstractNonUniformGeneratorTest
29 {
30
31
32 @Override
33 protected NonUniformGenerator buildGenerator(final RandomGenerator uniform, final double... parameters)
34 {
35 return new NakagamiGenerator(uniform, parameters[0], parameters[1]);
36 }
37
38
39 @Override
40 protected RealDistribution buildDistribution(double... parameters)
41 {
42 return new NakagamiDistribution(parameters[0], parameters[1]);
43 }
44
45 @Test
46 public void testDistribution()
47 {
48 Assertions.assertEquals(0.0, ksStatistic(0x099a9800e248b9f4L, 3.0, 2.0), 3.3e-3);
49 }
50
51 @Test
52 public void testMean()
53 {
54 final SplittableRandom seedsGenerator = new SplittableRandom(0x7b2173aba272b5e7L);
55 for (double m = 0.75; m < 5.0; m += 0.125)
56 {
57 for (double omega = 1.0; omega < 4.0; omega += 0.5)
58 {
59 final double mean = mean(seedsGenerator.nextLong(), m, omega);
60 final double r = Gamma.gamma(m + 0.5) / Gamma.gamma(m);
61 final double theoretical = r * FastMath.sqrt(omega / m);
62 Assertions.assertEquals(theoretical, mean, 0.004 * theoretical);
63 }
64 }
65 }
66
67 @Test
68 public void testVariance()
69 {
70 final SplittableRandom seedsGenerator = new SplittableRandom(0x748ffa6ce93daaebL);
71 for (double m = 0.75; m < 5.0; m += 0.125)
72 {
73 for (double omega = 1.0; omega < 4.0; omega += 0.5)
74 {
75 final double variance = variance(seedsGenerator.nextLong(), m, omega);
76 final double r = Gamma.gamma(m + 0.5) / Gamma.gamma(m);
77 final double theoretical = omega * (1 - r * r / m);
78 Assertions.assertEquals(theoretical, variance, 0.014 * theoretical);
79 }
80 }
81 }
82
83 }