View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * The class <code>StableRandomGeneratorTest</code> contains tests for the class
32   * {@link StableRandomGenerator}
33   *
34   */
35  public class StableRandomGeneratorTest {
36  
37      private RandomGenerator rg = new Well19937c(100);
38      private final static int sampleSize = 10000;
39  
40      /**
41       * Run the double nextDouble() method test Due to leptokurtic property the
42       * acceptance range is widened.
43       *
44       * TODO: verify that tolerance this wide is really OK
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       * If alpha = 2, than it must be Gaussian distribution
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       * If alpha = 1, than it must be Cauchy distribution
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          // Standard Cauchy distribution should have zero median and mode
85          double median = UnitTestUtils.median(values);
86          Assert.assertEquals(0.0, median, 0.2);
87      }
88  
89      /**
90       * Input parameter range tests
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 }