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    *      http://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.apache.commons.rng.sampling.distribution;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.stream.Collectors;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.MethodSource;
26  
27  /**
28   * Tests for random deviates generators.
29   */
30  class ContinuousSamplerParametricTest {
31      private static Iterable<ContinuousSamplerTestData> getSamplerTestData() {
32          return ContinuousSamplersList.list();
33      }
34  
35      @ParameterizedTest
36      @MethodSource("getSamplerTestData")
37      void testSampling(ContinuousSamplerTestData data) {
38          check(20000, data.getSampler(), data.getDeciles());
39      }
40  
41      /**
42       * Performs a chi-square test of homogeneity of the observed
43       * distribution with the expected distribution.
44       * Tests are performed at the 1% level and an average failure rate
45       * higher than 5% causes the test case to fail.
46       *
47       * @param sampler Sampler.
48       * @param sampleSize Number of random values to generate.
49       * @param deciles Deciles.
50       */
51      private static void check(long sampleSize,
52                                ContinuousSampler sampler,
53                                double[] deciles) {
54          final int numTests = 50;
55  
56          // Do not change (statistical test assumes that dof = 9).
57          final int numBins = 10; // dof = numBins - 1
58  
59          // Run the tests.
60          int numFailures = 0;
61  
62          final double[] expected = new double[numBins];
63          Arrays.fill(expected, sampleSize / (double) numBins);
64  
65          final long[] observed = new long[numBins];
66          // Chi-square critical value with 9 degrees of freedom
67          // and 1% significance level.
68          final double chi2CriticalValue = 21.665994333461924;
69  
70          // For storing chi2 larger than the critical value.
71          final List<Double> failedStat = new ArrayList<>();
72          try {
73              final int lastDecileIndex = numBins - 1;
74              for (int i = 0; i < numTests; i++) {
75                  Arrays.fill(observed, 0);
76                  SAMPLE: for (long j = 0; j < sampleSize; j++) {
77                      final double value = sampler.sample();
78  
79                      for (int k = 0; k < lastDecileIndex; k++) {
80                          if (value < deciles[k]) {
81                              ++observed[k];
82                              continue SAMPLE;
83                          }
84                      }
85                      ++observed[lastDecileIndex];
86                  }
87  
88                  // Compute chi-square.
89                  double chi2 = 0;
90                  for (int k = 0; k < numBins; k++) {
91                      final double diff = observed[k] - expected[k];
92                      chi2 += diff * diff / expected[k];
93                  }
94  
95                  // Statistics check.
96                  if (chi2 > chi2CriticalValue) {
97                      failedStat.add(chi2);
98                      ++numFailures;
99                  }
100             }
101         } catch (Exception e) {
102             // Should never happen.
103             throw new RuntimeException("Unexpected", e);
104         }
105 
106         // The expected number of failed tests can be modelled as a Binomial distribution
107         // B(n, p) with n=50, p=0.01 (50 tests with a 1% significance level).
108         // The cumulative probability of the number of failed tests (X) is:
109         // x     P(X>x)
110         // 1     0.0894
111         // 2     0.0138
112         // 3     0.0016
113 
114         if (numFailures > 3) { // Test will fail with 0.16% probability
115             Assertions.fail(String.format(
116                     "%s: Too many failures for sample size = %d " +
117                     "(%d out of %d tests failed, chi2 > %.3f=%s)",
118                     sampler, sampleSize, numFailures, numTests, chi2CriticalValue,
119                     failedStat.stream().map(d -> String.format("%.3f", d))
120                               .collect(Collectors.joining(", ", "[", "]"))));
121         }
122     }
123 }