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.distribution.RealDistribution;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  /**
35   * Test cases for {@link NormalDistribution}.
36   */
37  public class NormalDistributionTest extends RealDistributionAbstractTest {
38  
39      //-------------- Implementations for abstract methods -----------------------
40  
41      /** Creates the default real distribution instance to use in tests. */
42      @Override
43      public NormalDistribution makeDistribution() {
44          return new NormalDistribution(2.1, 1.4);
45      }
46  
47      /** Creates the default cumulative probability distribution test input values */
48      @Override
49      public double[] makeCumulativeTestPoints() {
50          // quantiles computed using R
51          return new double[] {-2.226325228634938d, -1.156887023657177d, -0.643949578356075d, -0.2027950777320613d, 0.305827808237559d,
52                  6.42632522863494d, 5.35688702365718d, 4.843949578356074d, 4.40279507773206d, 3.89417219176244d};
53      }
54  
55      /** Creates the default cumulative probability density test expected values */
56      @Override
57      public double[] makeCumulativeTestValues() {
58          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
59                  0.990d, 0.975d, 0.950d, 0.900d};
60      }
61  
62      /** Creates the default probability density test expected values */
63      @Override
64      public double[] makeDensityTestValues() {
65          return new double[] {0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380,
66                  0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380};
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          NormalDistribution distribution = (NormalDistribution) getDistribution();
83          double mu = distribution.getMean();
84          double sigma = distribution.getStandardDeviation();
85          setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
86                  mu, mu + sigma, mu + 2 * sigma,  mu + 3 * sigma, mu + 4 * sigma,
87                  mu + 5 * sigma});
88          // Quantiles computed using R (same as Mathematica)
89          setCumulativeTestValues(new double[] {0.02275013194817921, 0.158655253931457, 0.5, 0.841344746068543,
90                  0.977249868051821, 0.99865010196837, 0.999968328758167,  0.999999713348428});
91          verifyCumulativeProbabilities();
92      }
93  
94      @Test
95      void testQuantiles() {
96          setDensityTestValues(new double[] {0.0385649760808, 0.172836231799, 0.284958771715, 0.172836231799, 0.0385649760808,
97                  0.00316560600853, 9.55930184035e-05, 1.06194251052e-06});
98          verifyQuantiles();
99          verifyDensities();
100 
101         setDistribution(new NormalDistribution(0, 1));
102         setDensityTestValues(new double[] {0.0539909665132, 0.241970724519, 0.398942280401, 0.241970724519, 0.0539909665132,
103                 0.00443184841194, 0.000133830225765, 1.48671951473e-06});
104         verifyQuantiles();
105         verifyDensities();
106 
107         setDistribution(new NormalDistribution(0, 0.1));
108         setDensityTestValues(new double[] {0.539909665132, 2.41970724519, 3.98942280401, 2.41970724519,
109                 0.539909665132, 0.0443184841194, 0.00133830225765, 1.48671951473e-05});
110         verifyQuantiles();
111         verifyDensities();
112     }
113 
114     @Test
115     void testInverseCumulativeProbabilityExtremes() {
116         setInverseCumulativeTestPoints(new double[] {0, 1});
117         setInverseCumulativeTestValues(
118                 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
119         verifyInverseCumulativeProbabilities();
120     }
121 
122     // MATH-1257
123     @Test
124     void testCumulativeProbability() {
125         final RealDistribution dist = new NormalDistribution(0, 1);
126         double x = -10;
127         double expected = 7.61985e-24;
128         double v = dist.cumulativeProbability(x);
129         double tol = 1e-5;
130         assertEquals(1, v / expected, tol);
131     }
132 
133     @Test
134     void testGetMean() {
135         NormalDistribution distribution = (NormalDistribution) getDistribution();
136         assertEquals(2.1, distribution.getMean(), 0);
137     }
138 
139     @Test
140     void testGetStandardDeviation() {
141         NormalDistribution distribution = (NormalDistribution) getDistribution();
142         assertEquals(1.4, distribution.getStandardDeviation(), 0);
143     }
144 
145     @Test
146     void testPreconditions() {
147         assertThrows(MathIllegalArgumentException.class, () -> {
148             new NormalDistribution(1, 0);
149         });
150     }
151 
152     @Test
153     void testDensity() {
154         double [] x = new double[]{-2, -1, 0, 1, 2};
155         // R 2.5: print(dnorm(c(-2,-1,0,1,2)), digits=10)
156         checkDensity(0, 1, x, new double[]{0.05399096651, 0.24197072452, 0.39894228040, 0.24197072452, 0.05399096651});
157         // R 2.5: print(dnorm(c(-2,-1,0,1,2), mean=1.1), digits=10)
158         checkDensity(1.1, 1, x, new double[]{0.003266819056,0.043983595980,0.217852177033,0.396952547477,0.266085249899});
159     }
160 
161     private void checkDensity(double mean, double sd, double[] x, double[] expected) {
162         NormalDistribution d = new NormalDistribution(mean, sd);
163         for (int i = 0; i < x.length; i++) {
164             assertEquals(expected[i], d.density(x[i]), 1e-9);
165         }
166     }
167 
168     /**
169      * Check to make sure top-coding of extreme values works correctly.
170      * Verifies fixes for JIRA MATH-167, MATH-414
171      */
172     @Test
173     void testExtremeValues() {
174         NormalDistribution distribution = new NormalDistribution(0, 1);
175         for (int i = 0; i < 100; i++) { // make sure no convergence exception
176             double lowerTail = distribution.cumulativeProbability(-i);
177             double upperTail = distribution.cumulativeProbability(i);
178             if (i < 9) { // make sure not top-coded
179                 // For i = 10, due to bad tail precision in erf (MATH-364), 1 is returned
180                 // TODO: once MATH-364 is resolved, replace 9 with 30
181                 assertTrue(lowerTail > 0.0d);
182                 assertTrue(upperTail < 1.0d);
183             }
184             else { // make sure top coding not reversed
185                 assertTrue(lowerTail < 0.00001);
186                 assertTrue(upperTail > 0.99999);
187             }
188         }
189 
190         assertEquals(1, distribution.cumulativeProbability(Double.MAX_VALUE), 0);
191         assertEquals(0, distribution.cumulativeProbability(-Double.MAX_VALUE), 0);
192         assertEquals(1, distribution.cumulativeProbability(Double.POSITIVE_INFINITY), 0);
193         assertEquals(0, distribution.cumulativeProbability(Double.NEGATIVE_INFINITY), 0);
194     }
195 
196     @Test
197     void testMath280() {
198         NormalDistribution normal = new NormalDistribution(0,1);
199         double result = normal.inverseCumulativeProbability(0.9986501019683698);
200         assertEquals(3.0, result, defaultTolerance);
201         result = normal.inverseCumulativeProbability(0.841344746068543);
202         assertEquals(1.0, result, defaultTolerance);
203         result = normal.inverseCumulativeProbability(0.9999683287581673);
204         assertEquals(4.0, result, defaultTolerance);
205         result = normal.inverseCumulativeProbability(0.9772498680518209);
206         assertEquals(2.0, result, defaultTolerance);
207     }
208 
209     @Test
210     void testMoments() {
211         final double tol = 1e-9;
212         NormalDistribution dist;
213 
214         dist = new NormalDistribution(0, 1);
215         assertEquals(0, dist.getNumericalMean(), tol);
216         assertEquals(1, dist.getNumericalVariance(), tol);
217 
218         dist = new NormalDistribution(2.2, 1.4);
219         assertEquals(2.2, dist.getNumericalMean(), tol);
220         assertEquals(dist.getNumericalVariance(), 1.4 * 1.4, tol);
221 
222         dist = new NormalDistribution(-2000.9, 10.4);
223         assertEquals(dist.getNumericalMean(), -2000.9, tol);
224         assertEquals(dist.getNumericalVariance(), 10.4 * 10.4, tol);
225     }
226 }