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  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Test cases for {@link ParetoDistribution}.
35   */
36  public class ParetoDistributionTest extends RealDistributionAbstractTest {
37  
38      //-------------- Implementations for abstract methods -----------------------
39  
40      /** Creates the default real distribution instance to use in tests. */
41      @Override
42      public ParetoDistribution makeDistribution() {
43          return new ParetoDistribution(2.1, 1.4);
44      }
45  
46      /** Creates the default cumulative probability distribution test input values */
47      @Override
48      public double[] makeCumulativeTestPoints() {
49          // quantiles computed using R
50          return new double[] { -2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559,
51                                +6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244 };
52      }
53  
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] { 0, 0, 0, 0, 0, 0.791089998892, 0.730456085931, 0.689667290488, 0.645278794701, 0.578763688757 };
58      }
59  
60      /** Creates the default probability density test expected values */
61      @Override
62      public double[] makeDensityTestValues() {
63          return new double[] { 0, 0, 0, 0, 0, 0.0455118580441, 0.070444173646, 0.0896924681582, 0.112794186114, 0.151439332084 };
64      }
65  
66      /**
67       * Creates the default inverse cumulative probability distribution test input values.
68       */
69      @Override
70      public double[] makeInverseCumulativeTestPoints() {
71          // Exclude the test points less than zero, as they have cumulative
72          // probability of zero, meaning the inverse returns zero, and not the
73          // points less than zero.
74          double[] points = makeCumulativeTestValues();
75          double[] points2 = new double[points.length - 5];
76          System.arraycopy(points, 5, points2, 0, points.length - 5);
77          return points2;
78      }
79  
80      /**
81       * Creates the default inverse cumulative probability test expected values.
82       */
83      @Override
84      public double[] makeInverseCumulativeTestValues() {
85          // Exclude the test points less than zero, as they have cumulative
86          // probability of zero, meaning the inverse returns zero, and not the
87          // points less than zero.
88          double[] points = makeCumulativeTestPoints();
89          double[] points2 = new double[points.length - 5];
90          System.arraycopy(points, 5, points2, 0, points.length - 5);
91          return points2;
92      }
93  
94      // --------------------- Override tolerance  --------------
95      @BeforeEach
96      @Override
97      public void setUp() {
98          super.setUp();
99          setTolerance(1e-9);
100     }
101 
102     //---------------------------- Additional test cases -------------------------
103 
104     private void verifyQuantiles() {
105         ParetoDistribution distribution = (ParetoDistribution)getDistribution();
106         double mu = distribution.getScale();
107         double sigma = distribution.getShape();
108         setCumulativeTestPoints( new double[] { mu - 2 *sigma,  mu - sigma,
109                                                 mu,             mu + sigma,
110                                                 mu + 2 * sigma, mu + 3 * sigma,
111                                                 mu + 4 * sigma, mu + 5 * sigma });
112         verifyCumulativeProbabilities();
113     }
114 
115     @Test
116     void testQuantiles() {
117         setCumulativeTestValues(new double[] {0, 0, 0, 0.510884134236, 0.694625688662, 0.785201995008, 0.837811522357, 0.871634279326});
118         setDensityTestValues(new double[] {0, 0, 0.666666666, 0.195646346305, 0.0872498032394, 0.0477328899983, 0.0294888141169, 0.0197485724114});
119         verifyQuantiles();
120         verifyDensities();
121 
122         setDistribution(new ParetoDistribution(1, 1));
123         setCumulativeTestValues(new double[] {0, 0, 0, 0.5, 0.666666666667, 0.75, 0.8, 0.833333333333});
124         setDensityTestValues(new double[] {0, 0, 1.0, 0.25, 0.111111111111, 0.0625, 0.04, 0.0277777777778});
125         verifyQuantiles();
126         verifyDensities();
127 
128         setDistribution(new ParetoDistribution(0.1, 0.1));
129         setCumulativeTestValues(new double[] {0, 0, 0, 0.0669670084632, 0.104041540159, 0.129449436704, 0.148660077479, 0.164041197922});
130         setDensityTestValues(new double[] {0, 0, 1.0, 0.466516495768, 0.298652819947, 0.217637640824, 0.170267984504, 0.139326467013});
131         verifyQuantiles();
132         verifyDensities();
133     }
134 
135     @Test
136     void testInverseCumulativeProbabilityExtremes() {
137         setInverseCumulativeTestPoints(new double[] {0, 1});
138         setInverseCumulativeTestValues(new double[] {2.1, Double.POSITIVE_INFINITY});
139         verifyInverseCumulativeProbabilities();
140     }
141 
142     @Test
143     void testGetScale() {
144         ParetoDistribution distribution = (ParetoDistribution)getDistribution();
145         assertEquals(2.1, distribution.getScale(), 0);
146     }
147 
148     @Test
149     void testGetShape() {
150         ParetoDistribution distribution = (ParetoDistribution)getDistribution();
151         assertEquals(1.4, distribution.getShape(), 0);
152     }
153 
154     @Test
155     void testPreconditions() {
156         assertThrows(MathIllegalArgumentException.class, () -> {
157             new ParetoDistribution(1, 0);
158         });
159     }
160 
161     @Test
162     void testDensity() {
163         double [] x = new double[]{-2, -1, 0, 1, 2};
164         // R 2.14: print(dpareto(c(-2,-1,0,1,2), scale=1, shape=1), digits=10)
165         checkDensity(1, 1, x, new double[] { 0.00, 0.00, 0.00, 1.00, 0.25 });
166         // R 2.14: print(dpareto(c(-2,-1,0,1,2), scale=1.1, shape=1), digits=10)
167         checkDensity(1.1, 1, x, new double[] { 0.000, 0.000, 0.000, 0.000, 0.275 });
168     }
169 
170     private void checkDensity(double scale, double shape, double[] x,
171         double[] expected) {
172         ParetoDistribution d = new ParetoDistribution(scale, shape);
173         for (int i = 0; i < x.length; i++) {
174             assertEquals(expected[i], d.density(x[i]), 1e-9);
175         }
176     }
177 
178     /**
179      * Check to make sure top-coding of extreme values works correctly.
180      */
181     @Test
182     void testExtremeValues() {
183         ParetoDistribution d = new ParetoDistribution(1, 1);
184         for (int i = 0; i < 1e5; i++) { // make sure no convergence exception
185             double upperTail = d.cumulativeProbability(i);
186             if (i <= 1000) { // make sure not top-coded
187                 assertTrue(upperTail < 1.0d);
188             }
189             else { // make sure top coding not reversed
190                 assertTrue(upperTail > 0.999);
191             }
192         }
193 
194         assertEquals(1, d.cumulativeProbability(Double.MAX_VALUE), 0);
195         assertEquals(0, d.cumulativeProbability(-Double.MAX_VALUE), 0);
196         assertEquals(1, d.cumulativeProbability(Double.POSITIVE_INFINITY), 0);
197         assertEquals(0, d.cumulativeProbability(Double.NEGATIVE_INFINITY), 0);
198     }
199 
200     @Test
201     void testMeanVariance() {
202         final double tol = 1e-9;
203         ParetoDistribution dist;
204 
205         dist = new ParetoDistribution(1, 1);
206         assertEquals(Double.POSITIVE_INFINITY, dist.getNumericalMean(), tol);
207         assertEquals(Double.POSITIVE_INFINITY, dist.getNumericalVariance(), tol);
208 
209         dist = new ParetoDistribution(2.2, 2.4);
210         assertEquals(3.771428571428, dist.getNumericalMean(), tol);
211         assertEquals(14.816326530, dist.getNumericalVariance(), tol);
212     }
213 }