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    *      http://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.apache.commons.statistics.distribution;
19  
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Test cases for {@link LogNormalDistribution}.
25   * Extends {@link BaseContinuousDistributionTest}. See javadoc of that class for details.
26   */
27  class LogNormalDistributionTest extends BaseContinuousDistributionTest {
28      @Override
29      ContinuousDistribution makeDistribution(Object... parameters) {
30          final double mu = (Double) parameters[0];
31          final double sigma = (Double) parameters[1];
32          return LogNormalDistribution.of(mu, sigma);
33      }
34  
35      @Override
36      Object[][] makeInvalidParameters() {
37          return new Object[][] {
38              {0.0, 0.0},
39              {0.0, -0.1}
40          };
41      }
42  
43      @Override
44      String[] getParameterNames() {
45          return new String[] {"Mu", "Sigma"};
46      }
47  
48      @Override
49      protected double getRelativeTolerance() {
50          return 5e-15;
51      }
52  
53      //-------------------- Additional test cases -------------------------------
54  
55      @Test
56      void testCumulativeProbabilityExtremes() {
57          // Use a small shape parameter so that we can exceed 40 * shape
58          testCumulativeProbability(LogNormalDistribution.of(1, 0.0001),
59                                    new double[] {0.5, 10},
60                                    new double[] {0, 1.0},
61                                    DoubleTolerances.equals());
62      }
63  
64      @Test
65      void testSurvivalProbabilityExtremes() {
66          // Use a small shape parameter so that we can exceed 40 * shape
67          testSurvivalProbability(LogNormalDistribution.of(1, 0.0001),
68                                  new double[] {0.5, 10},
69                                  new double[] {1.0, 0.0},
70                                  DoubleTolerances.equals());
71      }
72  
73      /**
74       * Check to make sure top-coding of extreme values works correctly.
75       * Verifies fixes for JIRA MATH-167, MATH-414
76       */
77      @Test
78      void testExtremeValues() {
79          final LogNormalDistribution dist = LogNormalDistribution.of(0, 1);
80          for (int i = 0; i < 1e5; i++) { // make sure no convergence exception
81              final double upperTail = dist.cumulativeProbability(i);
82              if (i <= 72) { // make sure not top-coded
83                  Assertions.assertTrue(upperTail < 1.0d);
84              } else { // make sure top coding not reversed
85                  Assertions.assertTrue(upperTail > 0.99999);
86              }
87          }
88  
89          Assertions.assertEquals(1, dist.cumulativeProbability(Double.MAX_VALUE));
90          Assertions.assertEquals(0, dist.cumulativeProbability(-Double.MAX_VALUE));
91          Assertions.assertEquals(1, dist.cumulativeProbability(Double.POSITIVE_INFINITY));
92          Assertions.assertEquals(0, dist.cumulativeProbability(Double.NEGATIVE_INFINITY));
93      }
94  
95      @Test
96      void testTinyVariance() {
97          final LogNormalDistribution dist = LogNormalDistribution.of(0, 1e-9);
98          final double t = dist.getVariance();
99          Assertions.assertEquals(1e-18, t, 1e-20);
100     }
101 }