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