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 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   * Test cases for CauchyDistribution.
34   */
35  public class CauchyDistributionTest extends RealDistributionAbstractTest {
36  
37      // --------------------- Override tolerance  --------------
38      protected double defaultTolerance = 1e-9;
39      @Override
40      public void setUp() {
41          super.setUp();
42          setTolerance(defaultTolerance);
43      }
44  
45      //-------------- Implementations for abstract methods -----------------------
46  
47      /** Creates the default continuous distribution instance to use in tests. */
48      @Override
49      public CauchyDistribution makeDistribution() {
50          return new CauchyDistribution(1.2, 2.1);
51      }
52  
53      /** Creates the default cumulative probability distribution test input values */
54      @Override
55      public double[] makeCumulativeTestPoints() {
56          // quantiles computed using R 2.9.2
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      /** Creates the default cumulative probability density test expected values */
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      /** Creates the default probability density test expected values */
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      //---------------------------- Additional test cases -------------------------
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             // Expected.
108         }
109         try {
110             new CauchyDistribution(0, -1);
111             fail("Cannot have negative scale");
112         } catch (MathIllegalArgumentException ex) {
113             // Expected.
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 }