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  
21  import java.util.SplittableRandom;
22  
23  public abstract class AbstractNonUniformGeneratorTest
24  {
25  
26      /** Sample size for generators. */
27      private static final int SAMPLE_SIZE = 100000;
28  
29      /**
30       * Build a generator for non-uniform variates.
31       * @param uniform underlying generator for uniform variates
32       * @param parameters non-uniform distribution parameters
33       * @return generator for non-uniform variates
34       */
35      protected abstract NonUniformGenerator buildGenerator(RandomGenerator uniform, double... parameters);
36  
37      /**
38       * Build non-uniform distribution.
39       * @param parameters non-uniform distribution parameters
40       * @return non-uniform distribution
41       */
42      protected abstract RealDistribution buildDistribution(double... parameters);
43  
44      protected double ksStatistic(final long seed, double... parameters)
45      {
46  
47          // build generator and distribution
48          final SplittableRandom    seedsGenerator = new SplittableRandom(seed);
49          final RandomGenerator     uniform        = new Well19937a(seedsGenerator.nextLong());
50          final NonUniformGenerator generator      = buildGenerator(uniform, parameters);
51          final RealDistribution    distribution   = buildDistribution(parameters);
52  
53          final KolmogorovSmirnovTest ksTest = new KolmogorovSmirnovTest();
54  
55          // generate sample
56          final double[] data = new double[SAMPLE_SIZE];
57          for (int i = 0; i < SAMPLE_SIZE; ++i)
58          {
59              data[i] = generator.nextVariate();
60          }
61  
62          return ksTest.kolmogorovSmirnovStatistic(distribution, data);
63  
64      }
65  
66      protected double mean(final long seed, double... parameters)
67      {
68          final NonUniformGenerator generator = buildGenerator(new Well19937a(seed), parameters);
69          double sum = 0.0;
70          for (int i = 0; i < SAMPLE_SIZE; ++i)
71          {
72              sum += generator.nextVariate();
73          }
74          return sum / SAMPLE_SIZE;
75      }
76  
77      protected double variance(final long seed, double... parameters)
78      {
79          // compute variance using one-pass Welford method
80          final NonUniformGenerator generator = buildGenerator(new Well19937a(seed), parameters);
81          double m = 0;
82          double s = 0;
83          for (int i = 0; i < SAMPLE_SIZE; ++i)
84          {
85              final double x = generator.nextVariate();
86              final double oldM = m;
87              m += (x - m) / (i + 1);
88              s += (x - m) * (x - oldM);
89          }
90          return s / (SAMPLE_SIZE - 1);
91      }
92  
93  }