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