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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.distribution.continuous;
24  
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  
32  /**
33   * Test cases for {@link TriangularDistribution}.
34   */
35  public class TriangularDistributionTest extends RealDistributionAbstractTest {
36  
37      // --- Override tolerance -------------------------------------------------
38  
39      @BeforeEach
40      @Override
41      public void setUp() {
42          super.setUp();
43          setTolerance(1e-4);
44      }
45  
46      //--- Implementations for abstract methods --------------------------------
47  
48      /**
49       * Creates the default triangular distribution instance to use in tests.
50       */
51      @Override
52      public TriangularDistribution makeDistribution() {
53          // Left side 5 wide, right side 10 wide.
54          return new TriangularDistribution(-3, 2, 12);
55      }
56  
57      /**
58       * Creates the default cumulative probability distribution test input
59       * values.
60       */
61      @Override
62      public double[] makeCumulativeTestPoints() {
63          return new double[] { -3.0001,                 // below lower limit
64                                -3.0,                    // at lower limit
65                                -2.0, -1.0, 0.0, 1.0,    // on lower side
66                                2.0,                     // at mode
67                                3.0, 4.0, 10.0, 11.0,    // on upper side
68                                12.0,                    // at upper limit
69                                12.0001                  // above upper limit
70                              };
71      }
72  
73      /**
74       * Creates the default cumulative probability density test expected values.
75       */
76      @Override
77      public double[] makeCumulativeTestValues() {
78          // Top at 2 / (b - a) = 2 / (12 - -3) = 2 / 15 = 7.5
79          // Area left  = 7.5 * 5  * 0.5 = 18.75 (1/3 of the total area)
80          // Area right = 7.5 * 10 * 0.5 = 37.5  (2/3 of the total area)
81          // Area total = 18.75 + 37.5 = 56.25
82          // Derivative left side = 7.5 / 5 = 1.5
83          // Derivative right side = -7.5 / 10 = -0.75
84          double third = 1 / 3.0;
85          double left = 18.75;
86          double area = 56.25;
87          return new double[] { 0.0,
88                                0.0,
89                                0.75 / area, 3 / area, 6.75 / area, 12 / area,
90                                third,
91                                (left + 7.125) / area, (left + 13.5) / area,
92                                (left + 36) / area, (left + 37.125) / area,
93                                1.0,
94                                1.0
95                              };
96      }
97  
98      /**
99       * Creates the default inverse cumulative probability distribution test
100      * input values.
101      */
102     @Override
103     public double[] makeInverseCumulativeTestPoints() {
104         // Exclude the points outside the limits, as they have cumulative
105         // probability of zero and one, meaning the inverse returns the
106         // limits and not the points outside the limits.
107         double[] points = makeCumulativeTestValues();
108         double[] points2 = new double[points.length-2];
109         System.arraycopy(points, 1, points2, 0, points2.length);
110         return points2;
111         //return Arrays.copyOfRange(points, 1, points.length - 1);
112     }
113 
114     /**
115      * Creates the default inverse cumulative probability density test expected
116      * values.
117      */
118     @Override
119     public double[] makeInverseCumulativeTestValues() {
120         // Exclude the points outside the limits, as they have cumulative
121         // probability of zero and one, meaning the inverse returns the
122         // limits and not the points outside the limits.
123         double[] points = makeCumulativeTestPoints();
124         double[] points2 = new double[points.length-2];
125         System.arraycopy(points, 1, points2, 0, points2.length);
126         return points2;
127         //return Arrays.copyOfRange(points, 1, points.length - 1);
128     }
129 
130     /** Creates the default probability density test expected values. */
131     @Override
132     public double[] makeDensityTestValues() {
133         return new double[] { 0,
134                               0,
135                               2 / 75.0, 4 / 75.0, 6 / 75.0, 8 / 75.0,
136                               10 / 75.0,
137                               9 / 75.0, 8 / 75.0, 2 / 75.0, 1 / 75.0,
138                               0,
139                               0
140                             };
141     }
142 
143     //--- Additional test cases -----------------------------------------------
144 
145     /** Test lower bound getter. */
146     @Test
147     void testGetLowerBound() {
148         TriangularDistribution distribution = makeDistribution();
149         assertEquals(-3.0, distribution.getSupportLowerBound(), 0);
150     }
151 
152     /** Test upper bound getter. */
153     @Test
154     void testGetUpperBound() {
155         TriangularDistribution distribution = makeDistribution();
156         assertEquals(12.0, distribution.getSupportUpperBound(), 0);
157     }
158 
159     /** Test pre-condition for equal lower/upper limit. */
160     @Test
161     void testPreconditions1() {
162         assertThrows(MathIllegalArgumentException.class, () -> {
163             new TriangularDistribution(0, 0, 0);
164         });
165     }
166 
167     /** Test pre-condition for lower limit larger than upper limit. */
168     @Test
169     void testPreconditions2() {
170         assertThrows(MathIllegalArgumentException.class, () -> {
171             new TriangularDistribution(1, 1, 0);
172         });
173     }
174 
175     /** Test pre-condition for mode larger than upper limit. */
176     @Test
177     void testPreconditions3() {
178         assertThrows(MathIllegalArgumentException.class, () -> {
179             new TriangularDistribution(0, 2, 1);
180         });
181     }
182 
183     /** Test pre-condition for mode smaller than lower limit. */
184     @Test
185     void testPreconditions4() {
186         assertThrows(MathIllegalArgumentException.class, () -> {
187             new TriangularDistribution(2, 1, 3);
188         });
189     }
190 
191     /** Test mean/variance. */
192     @Test
193     void testMeanVariance() {
194         TriangularDistribution dist;
195 
196         dist = new TriangularDistribution(0, 0.5, 1.0);
197         assertEquals(0.5, dist.getNumericalMean(), 0);
198         assertEquals(dist.getNumericalVariance(), 1 / 24.0, 0);
199 
200         dist = new TriangularDistribution(0, 1, 1);
201         assertEquals(dist.getNumericalMean(), 2 / 3.0, 0);
202         assertEquals(dist.getNumericalVariance(), 1 / 18.0, 0);
203 
204         dist = new TriangularDistribution(-3, 2, 12);
205         assertEquals(dist.getNumericalMean(), 3 + (2 / 3.0), 0);
206         assertEquals(dist.getNumericalVariance(), 175 / 18.0, 0);
207     }
208 }