View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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  package org.hipparchus.distribution.continuous;
19  
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assertions.fail;
28  
29  /**
30   * Test cases for HalfCauchyDistribution.
31   */
32  public class HalfCauchyDistributionTest extends RealDistributionAbstractTest {
33  
34      // --------------------- Override tolerance  --------------
35      protected double defaultTolerance = 1e-9;
36  
37      @BeforeEach
38      @Override
39      public void setUp() {
40          super.setUp();
41          setTolerance(defaultTolerance);
42      }
43  
44      //-------------- Implementations for abstract methods -----------------------
45  
46      /* Creates the default continuous distribution instance to use in tests. */
47      /* The scale parameter is set to 1.0 to allow a comparison with the SciPy implementation */
48      @Override
49      public HalfCauchyDistribution makeDistribution() {
50          return new HalfCauchyDistribution(1.0);
51      }
52  
53      /** Creates the default cumulative probability distribution test input values */
54      @Override
55      public double[] makeCumulativeTestPoints() {
56          // quantiles computed using Scipy
57          return new double[] {1.5707976187243667e-03, 1.5709255323664916e-02, 3.9290107007669640e-02,
58                               7.8701706824618439e-02, 1.5838444032453627e-01, 6.3661924876873445e+02,
59                               6.3656741162871697e+01, 2.5451699579357040e+01, 1.2706204736174696e+01,
60                               6.3137515146750411e+00};
61      }
62  
63      /** Creates the default cumulative probability density test expected values */
64      @Override
65      public double[] makeCumulativeTestValues() {
66          return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900};
67      }
68  
69      /** Creates the default probability density test expected values */
70      @Override
71      public double[] makeDensityTestValues() {
72          // Computed with Scipy
73          return new double[] {6.3661820157254645e-01, 6.3646270565375884e-01, 6.3563852921903075e-01,
74                               6.3270084946368599e-01, 6.2104057764005360e-01, 1.5707950348670535e-06,
75                               1.5706671382255882e-04, 9.8124314855064783e-04, 3.9189229038953119e-03,
76                               1.5579194727527890e-02 };
77      }
78  
79      //---------------------------- Additional test cases -------------------------
80  
81      @Test
82      void testInverseCumulativeProbabilityExtremes() {
83          setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
84          setInverseCumulativeTestValues(
85                  new double[] {0.0, Double.POSITIVE_INFINITY});
86          verifyInverseCumulativeProbabilities();
87      }
88  
89      @Test
90      void testScale() {
91          HalfCauchyDistribution distribution;
92          distribution = new HalfCauchyDistribution(2.1);
93          assertEquals(2.1, distribution.getScale(), 0.0);
94      }
95  
96      @Test
97      void testPreconditions() {
98          try {
99              new HalfCauchyDistribution(0);
100             fail("Cannot have zero scale");
101         } catch (MathIllegalArgumentException ex) {
102             // Expected.
103         }
104         try {
105             new HalfCauchyDistribution(-1);
106             fail("Cannot have negative scale");
107         } catch (MathIllegalArgumentException ex) {
108             // Expected.
109         }
110     }
111 
112     @Test
113     void testMoments() {
114         HalfCauchyDistribution dist;
115 
116         dist = new HalfCauchyDistribution(0.15);
117         assertTrue(Double.isNaN(dist.getNumericalMean()));
118         assertTrue(Double.isNaN(dist.getNumericalVariance()));
119 
120         dist = new HalfCauchyDistribution(2.12);
121         assertTrue(Double.isNaN(dist.getNumericalMean()));
122         assertTrue(Double.isNaN(dist.getNumericalVariance()));
123     }
124 
125     @Test
126     public void testNegative() {
127         final HalfCauchyDistribution dist = new HalfCauchyDistribution(0.15);
128         Assertions.assertEquals(0.0, dist.density(-0.5), 1.0e-15);
129         Assertions.assertEquals(Double.NEGATIVE_INFINITY, dist.logDensity(-0.5), 1.0);
130         Assertions.assertEquals(0.0, dist.cumulativeProbability(-0.5), 1.0e-15);
131     }
132 
133     @Test
134     public void testInvariants() {
135         final HalfCauchyDistribution dist = new HalfCauchyDistribution(1.0);
136         Assertions.assertEquals(0.0, dist.getSupportLowerBound(), 1.0e-15);
137         Assertions.assertEquals(Double.POSITIVE_INFINITY, dist.getSupportUpperBound(), 1.0);
138         Assertions.assertTrue(Double.isNaN(dist.getMedian()));
139         Assertions.assertTrue(dist.isSupportConnected());
140 
141     }
142 
143 }
144