1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
35
36 public class ParetoDistributionTest extends RealDistributionAbstractTest {
37
38
39
40
41 @Override
42 public ParetoDistribution makeDistribution() {
43 return new ParetoDistribution(2.1, 1.4);
44 }
45
46
47 @Override
48 public double[] makeCumulativeTestPoints() {
49
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
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
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
68
69 @Override
70 public double[] makeInverseCumulativeTestPoints() {
71
72
73
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
82
83 @Override
84 public double[] makeInverseCumulativeTestValues() {
85
86
87
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
95 @BeforeEach
96 @Override
97 public void setUp() {
98 super.setUp();
99 setTolerance(1e-9);
100 }
101
102
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
165 checkDensity(1, 1, x, new double[] { 0.00, 0.00, 0.00, 1.00, 0.25 });
166
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
180
181 @Test
182 void testExtremeValues() {
183 ParetoDistribution d = new ParetoDistribution(1, 1);
184 for (int i = 0; i < 1e5; i++) {
185 double upperTail = d.cumulativeProbability(i);
186 if (i <= 1000) {
187 assertTrue(upperTail < 1.0d);
188 }
189 else {
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 }