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.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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }