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  
18  package org.apache.commons.statistics.distribution;
19  
20  import java.util.stream.Stream;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.params.ParameterizedTest;
23  import org.junit.jupiter.params.provider.Arguments;
24  import org.junit.jupiter.params.provider.CsvSource;
25  import org.junit.jupiter.params.provider.MethodSource;
26  
27  /**
28   * Test cases for {@link TriangularDistribution}.
29   * Extends {@link BaseContinuousDistributionTest}. See javadoc of that class for details.
30   */
31  class TriangularDistributionTest extends BaseContinuousDistributionTest {
32      @Override
33      ContinuousDistribution makeDistribution(Object... parameters) {
34          final double lower = (Double) parameters[0];
35          final double mode = (Double) parameters[1];
36          final double upper = (Double) parameters[2];
37          return TriangularDistribution.of(lower, mode, upper);
38      }
39  
40  
41      @Override
42      Object[][] makeInvalidParameters() {
43          return new Object[][] {
44              {0.0, 0.0, 0.0},
45              // 1.0, 2.0, 3 is OK - move points to incorrect locations
46              {4.0, 2.0, 3.0},
47              {3.0, 2.0, 3.0},
48              {2.5, 2.0, 3.0},
49              {1.0, 0.0, 3.0},
50              {1.0, 4.0, 3.0},
51              {1.0, 2.0, -1.0},
52              {1.0, 2.0, 1.5},
53          };
54      }
55  
56      @Override
57      String[] getParameterNames() {
58          return new String[] {"SupportLowerBound", "Mode", "SupportUpperBound"};
59      }
60  
61      @Override
62      protected double getRelativeTolerance() {
63          // Tolerance is 4.440892098500626E-15.
64          return 20 * RELATIVE_EPS;
65      }
66  
67      //-------------------- Additional test cases -------------------------------
68  
69      @ParameterizedTest
70      @MethodSource
71      void testAdditionalMoments(double a, double b, double c, double mean, double variance) {
72          final TriangularDistribution dist = TriangularDistribution.of(a, b, c);
73          testMoments(dist, mean, variance, DoubleTolerances.equals());
74      }
75  
76      static Stream<Arguments> testAdditionalMoments() {
77          return Stream.of(
78              Arguments.of(0, 0.5, 1.0, 0.5, 1 / 24.0),
79              Arguments.of(0, 1, 1, 2 / 3.0, 1 / 18.0),
80              Arguments.of(-3, 2, 12, 3 + (2 / 3.0), 175 / 18.0)
81          );
82      }
83  
84      @ParameterizedTest
85      @CsvSource({
86          "1, 2, 3",
87          "0.12, 3.45, 12.56",
88      })
89      void testAdditionalParameterAccessors(double lower, double mode, double upper) {
90          final TriangularDistribution dist = TriangularDistribution.of(lower, mode, upper);
91          Assertions.assertEquals(lower, dist.getSupportLowerBound());
92          Assertions.assertEquals(mode, dist.getMode());
93          Assertions.assertEquals(upper, dist.getSupportUpperBound());
94      }
95  }