1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.random;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33
34
35 public class StableRandomGeneratorTest {
36
37 private RandomGenerator rg = new Well19937c(100);
38 private final static int sampleSize = 10000;
39
40
41
42
43
44
45
46 @Test
47 public void testNextDouble() {
48 StableRandomGenerator generator = new StableRandomGenerator(rg, 1.3,
49 0.1);
50 double[] sample = new double[2 * sampleSize];
51 for (int i = 0; i < sample.length; ++i) {
52 sample[i] = generator.nextNormalizedDouble();
53 }
54 Assert.assertEquals(0.0, UnitTestUtils.mean(sample), 0.3);
55 }
56
57
58
59
60 @Test
61 public void testGaussianCase() {
62 StableRandomGenerator generator = new StableRandomGenerator(rg, 2d, 0.0);
63
64 double[] sample = new double[sampleSize];
65 for (int i = 0; i < sample.length; ++i) {
66 sample[i] = generator.nextNormalizedDouble();
67 }
68 Assert.assertEquals(0.0, UnitTestUtils.mean(sample), 0.02);
69 Assert.assertEquals(1.0, UnitTestUtils.variance(sample), 0.02);
70 }
71
72
73
74
75 @Test
76 public void testCauchyCase() {
77 StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
78
79 final double[] values = new double[sampleSize];
80 for (int i = 0; i < sampleSize; ++i) {
81 values[i] = generator.nextNormalizedDouble();
82 }
83
84
85 double median = UnitTestUtils.median(values);
86 Assert.assertEquals(0.0, median, 0.2);
87 }
88
89
90
91
92 @Test
93 public void testAlphaRangeBelowZero() {
94 try {
95 new StableRandomGenerator(rg, -1.0, 0.0);
96 Assert.fail("Expected MathIllegalArgumentException");
97 } catch (MathIllegalArgumentException e) {
98 Assert.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_LEFT, e.getSpecifier());
99 Assert.assertEquals(-1.0, ((Double) e.getParts()[0]).doubleValue(), 1.0e-10);
100 }
101 }
102
103 @Test
104 public void testAlphaRangeAboveTwo() {
105 try {
106 new StableRandomGenerator(rg, 3.0, 0.0);
107 Assert.fail("Expected MathIllegalArgumentException");
108 } catch (MathIllegalArgumentException e) {
109 Assert.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_LEFT, e.getSpecifier());
110 Assert.assertEquals(3.0, ((Double) e.getParts()[0]).doubleValue(), 1.0e-10);
111 }
112 }
113
114 @Test
115 public void testBetaRangeBelowMinusOne() {
116 try {
117 new StableRandomGenerator(rg, 1.0, -2.0);
118 Assert.fail("Expected MathIllegalArgumentException");
119 } catch (MathIllegalArgumentException e) {
120 Assert.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, e.getSpecifier());
121 Assert.assertEquals(-2.0, ((Double) e.getParts()[0]).doubleValue(), 1.0e-10);
122 }
123 }
124
125 @Test
126 public void testBetaRangeAboveOne() {
127 try {
128 new StableRandomGenerator(rg, 1.0, 2.0);
129 Assert.fail("Expected MathIllegalArgumentException");
130 } catch (MathIllegalArgumentException e) {
131 Assert.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, e.getSpecifier());
132 Assert.assertEquals(2.0, ((Double) e.getParts()[0]).doubleValue(), 1.0e-10);
133 }
134 }
135 }