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