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  package org.hipparchus.analysis.integration.gauss;
23  
24  import org.hipparchus.analysis.UnivariateFunction;
25  import org.hipparchus.util.FastMath;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  
30  /**
31   * Test of the {@link HermiteRuleFactory}.
32   *
33   */
34  class HermiteTest {
35      private static final GaussIntegratorFactory factory = new GaussIntegratorFactory();
36  
37      @Test
38      void testNormalDistribution() {
39          final double oneOverSqrtPi = 1 / FastMath.sqrt(Math.PI);
40  
41          // By defintion, Gauss-Hermite quadrature readily provides the
42          // integral of the normal distribution density.
43          final int numPoints = 1;
44  
45          // Change of variable:
46          //   y = (x - mu) / (sqrt(2) *  sigma)
47          // such that the integrand
48          //   N(x, mu, sigma)
49          // is transformed to
50          //   f(y) * exp(-y^2)
51          final UnivariateFunction f = y -> {
52              return oneOverSqrtPi; // Constant function.
53          };
54  
55          final GaussIntegrator integrator = factory.hermite(numPoints);
56          final double result = integrator.integrate(f);
57          final double expected = 1;
58          assertEquals(expected, result, FastMath.ulp(expected));
59      }
60  
61      @Test
62      void testNormalMean() {
63          final double sqrtTwo = FastMath.sqrt(2);
64          final double oneOverSqrtPi = 1 / FastMath.sqrt(Math.PI);
65  
66          final double mu = 12345.6789;
67          final double sigma = 987.654321;
68          final int numPoints = 6;
69  
70          // Change of variable:
71          //   y = (x - mu) / (sqrt(2) *  sigma)
72          // such that the integrand
73          //   x * N(x, mu, sigma)
74          // is transformed to
75          //   f(y) * exp(-y^2)
76          final UnivariateFunction f = y -> oneOverSqrtPi * (sqrtTwo * sigma * y + mu);
77  
78          final GaussIntegrator integrator = factory.hermite(numPoints);
79          final double result = integrator.integrate(f);
80          final double expected = mu;
81          assertEquals(expected, result, 5 * FastMath.ulp(expected));
82      }
83  
84      @Test
85      void testNormalVariance() {
86          final double twoOverSqrtPi = 2 / FastMath.sqrt(Math.PI);
87  
88          final double sigma = 987.654321;
89          final double sigma2 = sigma * sigma;
90          final int numPoints = 5;
91  
92          // Change of variable:
93          //   y = (x - mu) / (sqrt(2) *  sigma)
94          // such that the integrand
95          //   (x - mu)^2 * N(x, mu, sigma)
96          // is transformed to
97          //   f(y) * exp(-y^2)
98          final UnivariateFunction f = y -> twoOverSqrtPi * sigma2 * y * y;
99  
100         final GaussIntegrator integrator = factory.hermite(numPoints);
101         final double result = integrator.integrate(f);
102         final double expected = sigma2;
103         assertEquals(expected, result, 10 * FastMath.ulp(expected));
104     }
105 }