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.hipparchus.util.FastMath;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertThrows;
28  
29  /**
30   * Test cases for {@link HalfNormalDistribution}.
31   */
32  public class HalfNormalDistributionTest extends RealDistributionAbstractTest {
33  
34      //-------------- Implementations for abstract methods -----------------------
35  
36      /* Creates the default real distribution instance to use in tests. */
37      /* The scale parameter is set to 1.0 to allow a comparison with the SciPy implementation */
38      @Override
39      public HalfNormalDistribution makeDistribution() {
40          return new HalfNormalDistribution(1.0);
41      }
42  
43      /** Creates the default cumulative probability distribution test input values */
44      @Override
45      public double[] makeCumulativeTestPoints() {
46          // quantiles computed using SciPy
47          return new double[] { 1.2533144654324165e-03, 1.2533469508069276e-02, 3.1337982021426479e-02,
48                                6.2706777943213846e-02, 1.2566134685507416e-01, 3.2905267314919255e+00,
49                                2.5758293035489004e+00, 2.2414027276049469e+00, 1.9599639845400540e+00,
50                                1.6448536269514722e+00};
51      }
52  
53      /** Creates the default cumulative probability density test expected values */
54      @Override
55      public double[] makeCumulativeTestValues() {
56          return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900};
57      }
58  
59      /** Creates the default probability density test expected values */
60      @Override
61      public double[] makeDensityTestValues() {
62          // Computed with SciPy
63          return new double[] {0.7978839341457147, 0.7978218942756774, 0.7974928680849483,
64                               0.7963174049782371, 0.7916097569752335, 0.0035543806938539,
65                               0.0289194860538348, 0.0647168003177488, 0.1168901396100708,
66                               0.2062712807507428};
67      }
68  
69      // --------------------- Override tolerance  --------------
70      protected double defaultTolerance = 1e-9;
71  
72      @BeforeEach
73      @Override
74      public void setUp() {
75          super.setUp();
76          setTolerance(defaultTolerance);
77      }
78  
79      //---------------------------- Additional test cases -------------------------
80  
81      private void verifyQuantiles() {
82          HalfNormalDistribution distribution = (HalfNormalDistribution) getDistribution();
83          double mu = distribution.getMean();
84          double sigma = distribution.getStandardDeviation();
85          setCumulativeTestPoints( new double[] {mu - sigma, mu, mu + sigma, mu + 2 * sigma,
86                                                 mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma});
87          // Quantiles computed using SciPy
88          setCumulativeTestValues(new double[] {0.1546652072068792, 0.5750625163166381,
89                                                0.8386946523497458, 0.9548769004203032, 0.9908477853495347,
90                                                0.9986686071007622, 0.9998621173822465});
91          verifyCumulativeProbabilities();
92      }
93  
94      @Test
95      void testQuantiles() {
96          verifyQuantiles();
97      }
98  
99  
100     @Test
101     void testInverseCumulativeProbabilityExtremes() {
102         setInverseCumulativeTestPoints(new double[] {0, 1});
103         setInverseCumulativeTestValues(
104                 new double[] {0.0, Double.POSITIVE_INFINITY});
105         verifyInverseCumulativeProbabilities();
106     }
107 
108     @Test
109     void testGetMean() {
110         double mean;
111         mean = FastMath.sqrt(2.0)/FastMath.sqrt(FastMath.PI);
112         HalfNormalDistribution distribution = (HalfNormalDistribution) getDistribution();
113         assertEquals(mean, distribution.getMean(), 0);
114     }
115 
116     @Test
117     void testGetStandardDeviation() {
118         double sd;
119         sd = FastMath.sqrt(1.0-2.0/FastMath.PI);
120         HalfNormalDistribution distribution = (HalfNormalDistribution) getDistribution();
121         assertEquals(sd, distribution.getStandardDeviation(), 0);
122     }
123 
124     @Test
125     void testPreconditions() {
126         assertThrows(MathIllegalArgumentException.class, () -> new HalfNormalDistribution(0));
127     }
128 
129     @Test
130     void testDensity() {
131         double [] x = new double[]{0.5, 1.0, 2.0, 4.0};
132         checkDensity(1.0, x, new double[]{7.0413065352859905e-01, 4.8394144903828673e-01, 1.0798193302637613e-01,
133                                           2.6766045152977074e-04});
134     }
135 
136     private void checkDensity(double scale, double[] x, double[] expected) {
137         HalfNormalDistribution d = new HalfNormalDistribution(scale);
138         for (int i = 0; i < x.length; i++) {
139             assertEquals(expected[i], d.density(x[i]), 1e-9);
140         }
141     }
142 
143     @Test
144     void testMoments() {
145         final double tol = 1e-9;
146         double mean;
147         double sd;
148         HalfNormalDistribution dist;
149 
150         dist = new HalfNormalDistribution(1.0);
151         mean = FastMath.sqrt(2.0)/FastMath.sqrt(FastMath.PI);
152         sd = FastMath.sqrt(1.0-2.0/FastMath.PI);
153         assertEquals(mean, dist.getNumericalMean(), tol);
154         assertEquals(sd*sd, dist.getNumericalVariance(), tol);
155 
156         dist = new HalfNormalDistribution(1.4);
157         mean = 1.4*FastMath.sqrt(2.0)/FastMath.sqrt(FastMath.PI);
158         sd = 1.4*FastMath.sqrt(1.0-2.0/FastMath.PI);
159         assertEquals(mean, dist.getNumericalMean(), tol);
160         assertEquals(dist.getNumericalVariance(), sd * sd, tol);
161 
162         dist = new HalfNormalDistribution(10.4);
163         mean = 10.4*FastMath.sqrt(2.0)/FastMath.sqrt(FastMath.PI);
164         sd = 10.4*FastMath.sqrt(1.0-2.0/FastMath.PI);
165         assertEquals(dist.getNumericalMean(), mean, tol);
166         assertEquals(dist.getNumericalVariance(), sd * sd, tol);
167     }
168 
169     @Test
170     public void testNegative() {
171         final HalfNormalDistribution dist = new HalfNormalDistribution(1.0);
172         Assertions.assertEquals(0.0, dist.density(-0.5), 1.0e-15);
173         Assertions.assertEquals(Double.NEGATIVE_INFINITY, dist.logDensity(-0.5), 1.0);
174         Assertions.assertEquals(0.0, dist.cumulativeProbability(-0.5), 1.0e-15);
175         Assertions.assertEquals(0.0, dist.probability(-0.5, 1.0), 1.0e-15);
176         Assertions.assertEquals(0.0, dist.probability(1.0, -0.5), 1.0e-15);
177         Assertions.assertEquals(0.0, dist.probability(-0.5, -0.5), 1.0e-15);
178     }
179 
180     @Test
181     public void testInvariants() {
182         final HalfNormalDistribution dist = new HalfNormalDistribution(1.0);
183         Assertions.assertEquals(0.0, dist.getSupportLowerBound(), 1.0e-15);
184         Assertions.assertEquals(Double.POSITIVE_INFINITY, dist.getSupportUpperBound(), 1.0);
185         Assertions.assertTrue(dist.isSupportConnected());
186 
187     }
188 
189 }
190