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  package org.apache.commons.statistics.distribution;
18  
19  import java.util.stream.Stream;
20  import org.junit.jupiter.params.ParameterizedTest;
21  import org.junit.jupiter.params.provider.Arguments;
22  import org.junit.jupiter.params.provider.MethodSource;
23  
24  /**
25   * Test cases for {@link PascalDistribution}.
26   * Extends {@link BaseDiscreteDistributionTest}. See javadoc of that class for details.
27   */
28  class PascalDistributionTest extends BaseDiscreteDistributionTest {
29      @Override
30      DiscreteDistribution makeDistribution(Object... parameters) {
31          final int r = (Integer) parameters[0];
32          final double p = (Double) parameters[1];
33          return PascalDistribution.of(r, p);
34      }
35  
36  
37      @Override
38      Object[][] makeInvalidParameters() {
39          return new Object[][] {
40              {0, 0.5},
41              {-1, 0.5},
42              {3, -0.1},
43              {3, 0.0},
44              {3, 1.1},
45          };
46      }
47  
48      @Override
49      String[] getParameterNames() {
50          return new String[] {"NumberOfSuccesses", "ProbabilityOfSuccess"};
51      }
52  
53      @Override
54      protected double getRelativeTolerance() {
55          return 5e-15;
56      }
57  
58      //-------------------- Additional test cases -------------------------------
59  
60      @ParameterizedTest
61      @MethodSource
62      void testAdditionalMoments(int r, double p, double mean, double variance) {
63          final PascalDistribution dist = PascalDistribution.of(r, p);
64          testMoments(dist, mean, variance, DoubleTolerances.ulps(1));
65      }
66  
67      static Stream<Arguments> testAdditionalMoments() {
68          return Stream.of(
69              Arguments.of(10, 0.5, (10d * 0.5d) / 0.5, (10d * 0.5d) / (0.5d * 0.5d)),
70              Arguments.of(25, 0.7, (25d * 0.3d) / 0.7, (25d * 0.3d) / (0.7d * 0.7d))
71          );
72      }
73  }