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 static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.junit.Test;
31
32
33
34
35 public class CauchyDistributionTest extends RealDistributionAbstractTest {
36
37
38 protected double defaultTolerance = 1e-9;
39 @Override
40 public void setUp() {
41 super.setUp();
42 setTolerance(defaultTolerance);
43 }
44
45
46
47
48 @Override
49 public CauchyDistribution makeDistribution() {
50 return new CauchyDistribution(1.2, 2.1);
51 }
52
53
54 @Override
55 public double[] makeCumulativeTestPoints() {
56
57 return new double[] {-667.24856187, -65.6230835029, -25.4830299460, -12.0588781808,
58 -5.26313542807, 669.64856187, 68.0230835029, 27.8830299460, 14.4588781808, 7.66313542807};
59 }
60
61
62 @Override
63 public double[] makeCumulativeTestValues() {
64 return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999,
65 0.990, 0.975, 0.950, 0.900};
66 }
67
68
69 @Override
70 public double[] makeDensityTestValues() {
71 return new double[] {
72 1.49599158008e-06, 0.000149550440335, 0.000933076881878,
73 0.00370933207799, 0.0144742330437, 1.49599158008e-06,
74 0.000149550440335, 0.000933076881878, 0.00370933207799,
75 0.0144742330437
76 };
77 }
78
79
80
81 @Test
82 public void testInverseCumulativeProbabilityExtremes() {
83 setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
84 setInverseCumulativeTestValues(
85 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
86 verifyInverseCumulativeProbabilities();
87 }
88
89 @Test
90 public void testMedian() {
91 CauchyDistribution distribution = (CauchyDistribution) getDistribution();
92 assertEquals(1.2, distribution.getMedian(), 0.0);
93 }
94
95 @Test
96 public void testScale() {
97 CauchyDistribution distribution = (CauchyDistribution) getDistribution();
98 assertEquals(2.1, distribution.getScale(), 0.0);
99 }
100
101 @Test
102 public void testPreconditions() {
103 try {
104 new CauchyDistribution(0, 0);
105 fail("Cannot have zero scale");
106 } catch (MathIllegalArgumentException ex) {
107
108 }
109 try {
110 new CauchyDistribution(0, -1);
111 fail("Cannot have negative scale");
112 } catch (MathIllegalArgumentException ex) {
113
114 }
115 }
116
117 @Test
118 public void testMoments() {
119 CauchyDistribution dist;
120
121 dist = new CauchyDistribution(10.2, 0.15);
122 assertTrue(Double.isNaN(dist.getNumericalMean()));
123 assertTrue(Double.isNaN(dist.getNumericalVariance()));
124
125 dist = new CauchyDistribution(23.12, 2.12);
126 assertTrue(Double.isNaN(dist.getNumericalMean()));
127 assertTrue(Double.isNaN(dist.getNumericalVariance()));
128 }
129 }