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.fail;
27  
28  /**
29   * Test cases for InvGammaDistribution.
30   */
31  public class InvGammaDistributionTest extends RealDistributionAbstractTest {
32  
33      //-------------- Implementations for abstract methods -----------------------
34  
35      /** Creates the default continuous distribution instance to use in tests. */
36      @Override
37      public InvGammaDistribution makeDistribution() {
38          return new InvGammaDistribution(4.0, 2.0);
39      }
40      /* Reference values for the given distribution
41      0.15311308632333426 0.001                0.067305117533028799
42      0.19910170259800344 0.01                 0.36987481092972235
43      0.22812110266103314 0.025000000000000001 0.67229999183907063
44      0.25794281611455883 0.050000000000000003 1.002262087716445
45      0.29936610417768517 0.10000000000000001  1.3916447406386885
46      4.6668737274551964  0.999                0.00078473049996328028
47      2.4293995643995729  0.98999999999999999  0.013833804188980926
48      1.8350890379656435  0.97499999999999998  0.043088414094508649
49      1.4637876535641741  0.94999999999999996  0.10120309476293882
50      1.1462831783710461  0.90000000000000002  0.23537771521330381
51      */
52      /** Creates the default cumulative probability distribution test input values */
53      @Override
54      public double[] makeCumulativeTestPoints() {
55          // Computed with Boost/1.74.0
56          return new double[] {0.15311308632333426, 0.19910170259800344, 0.2281211026610331, 0.25794281611455883,
57                               0.29936610417768517, 4.6668737274551964, 2.4293995643995729, 1.8350890379656435,
58                               1.4637876535641741, 1.1462831783710461};
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, 0.990, 0.975, 0.950, 0.900};
65      }
66  
67      /** Creates the default probability density test expected values */
68      @Override
69      public double[] makeDensityTestValues() {
70          // Computed with Boost/1.74.0
71          return new double[] {0.067305117533028799, 0.36987481092972235, 0.67229999183907063, 1.002262087716445,
72                               1.3916447406386885, 0.00078473049996328028, 0.013833804188980926, 0.043088414094508649,
73                               0.10120309476293882, 0.23537771521330381};
74      }
75  
76      // --------------------- Override tolerance  --------------
77      @BeforeEach
78      @Override
79      public void setUp() {
80          super.setUp();
81          setTolerance(1e-9);
82      }
83  
84      //---------------------------- Additional test cases -------------------------
85      @Test
86      void testParameterAccessors() {
87          InvGammaDistribution distribution = (InvGammaDistribution) getDistribution();
88          assertEquals(4.0, distribution.getShape(), 0);
89          assertEquals(2.0, distribution.getScale(), 0);
90      }
91  
92      @Test
93      void testPreconditions() {
94          try {
95              new InvGammaDistribution(0, 1);
96              fail("Expecting MathIllegalArgumentException for alpha = 0");
97          } catch (MathIllegalArgumentException ex) {
98              // Expected.
99          }
100         try {
101             new InvGammaDistribution(1, 0);
102             fail("Expecting MathIllegalArgumentException for alpha = 0");
103         } catch (MathIllegalArgumentException ex) {
104             // Expected.
105         }
106     }
107 
108     @Test
109     void testProbabilities() {
110         // Computed with Boost 1.83.0
111         testProbability(0.500,  4.0, 2.0, 0.43347012036670896);
112         testProbability(1.000,  4.0, 2.0, 0.85712346049854704);
113         testProbability(15.50,  4.0, 2.0, 0.99998958045696096);
114     }
115 
116     private void testProbability(double x, double a, double b, double expected) {
117         InvGammaDistribution distribution = new InvGammaDistribution( a, b );
118         double actual = distribution.cumulativeProbability(x);
119         assertEquals(expected, actual, 10e-4, "probability for " + x);
120     }
121 
122 
123     @Test
124     void testDensity() {
125         double[] x = new double[]{0.1, 0.5, 1, 2, 5};
126         // Computed with Boost 1.83.0
127         checkDensity(4.0, 2.0, x, new double[]{0.00054964096598361537, 1.5629345185053167, 0.36089408863096717, 0.030656620097620192, 0.00057200643928374557});
128     }
129 
130     private void checkDensity(double alpha, double beta, double[] x, double[] expected) {
131         InvGammaDistribution d = new InvGammaDistribution(alpha, beta);
132         for (int i = 0; i < x.length; i++) {
133             assertEquals(expected[i], d.density(x[i]), 1e-5);
134         }
135     }
136 
137     @Test
138     void testInverseCumulativeProbabilityExtremes() {
139         setInverseCumulativeTestPoints(new double[] {0, 1});
140         setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
141         verifyInverseCumulativeProbabilities();
142     }
143 
144     @Test
145     void testMoments() {
146         final double tol = 1.e-9;
147         InvGammaDistribution dist;
148 
149         double shape = 3.0;
150         double scale = 2.0;
151         double one   = 1.0;
152         dist = new InvGammaDistribution(shape,scale);
153         assertEquals(one, dist.getNumericalMean(), tol);
154         assertEquals(one, dist.getNumericalVariance(), tol);
155 
156         scale        = 4.0;
157         double two   = 2.0;
158         double four  = 4.0;
159         dist = new InvGammaDistribution(shape,scale);
160         assertEquals(two,dist.getNumericalMean(),tol);
161         assertEquals(four,dist.getNumericalVariance(),tol);
162     }
163 
164     @Test
165     public void testNegative() {
166         final InvGammaDistribution dist = new InvGammaDistribution(3.0, 2.0);
167         Assertions.assertEquals(0.0, dist.density(-0.5), 1.0e-15);
168         Assertions.assertEquals(Double.NEGATIVE_INFINITY, dist.logDensity(-0.5), 1.0);
169         Assertions.assertEquals(0.0, dist.cumulativeProbability(-0.5), 1.0e-15);
170         Assertions.assertEquals(1.0, dist.getNumericalMean(), 1.0e-15);
171         Assertions.assertTrue(Double.isNaN(new InvGammaDistribution(0.999, 2.0).getNumericalMean()));
172         Assertions.assertTrue(Double.isNaN(new InvGammaDistribution(1.999, 2.0).getNumericalVariance()));
173     }
174 
175     @Test
176     public void testInvariants() {
177         final InvGammaDistribution dist = new InvGammaDistribution(3.0, 2.0);
178         Assertions.assertEquals(0.0, dist.getSupportLowerBound(), 1.0e-15);
179         Assertions.assertEquals(Double.POSITIVE_INFINITY, dist.getSupportUpperBound(), 1.0);
180         Assertions.assertTrue(dist.isSupportConnected());
181 
182     }
183 
184 }
185