View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }