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 = new UnivariateFunction() {
52                  public double value(double y) {
53                      return oneOverSqrtPi; // Constant function.
54                  }
55              };
56  
57          final GaussIntegrator integrator = factory.hermite(numPoints);
58          final double result = integrator.integrate(f);
59          final double expected = 1;
60          assertEquals(expected, result, FastMath.ulp(expected));
61      }
62  
63      @Test
64      void testNormalMean() {
65          final double sqrtTwo = FastMath.sqrt(2);
66          final double oneOverSqrtPi = 1 / FastMath.sqrt(Math.PI);
67  
68          final double mu = 12345.6789;
69          final double sigma = 987.654321;
70          final int numPoints = 6;
71  
72          // Change of variable:
73          //   y = (x - mu) / (sqrt(2) *  sigma)
74          // such that the integrand
75          //   x * N(x, mu, sigma)
76          // is transformed to
77          //   f(y) * exp(-y^2)
78          final UnivariateFunction f = new UnivariateFunction() {
79                  public double value(double y) {
80                      return oneOverSqrtPi * (sqrtTwo * sigma * y + mu);
81                  }
82              };
83  
84          final GaussIntegrator integrator = factory.hermite(numPoints);
85          final double result = integrator.integrate(f);
86          final double expected = mu;
87          assertEquals(expected, result, 5 * FastMath.ulp(expected));
88      }
89  
90      @Test
91      void testNormalVariance() {
92          final double twoOverSqrtPi = 2 / FastMath.sqrt(Math.PI);
93  
94          final double sigma = 987.654321;
95          final double sigma2 = sigma * sigma;
96          final int numPoints = 5;
97  
98          // Change of variable:
99          //   y = (x - mu) / (sqrt(2) *  sigma)
100         // such that the integrand
101         //   (x - mu)^2 * N(x, mu, sigma)
102         // is transformed to
103         //   f(y) * exp(-y^2)
104         final UnivariateFunction f = new UnivariateFunction() {
105                 public double value(double y) {
106                     return twoOverSqrtPi * sigma2 * y * y;
107                 }
108             };
109 
110         final GaussIntegrator integrator = factory.hermite(numPoints);
111         final double result = integrator.integrate(f);
112         final double expected = sigma2;
113         assertEquals(expected, result, 10 * FastMath.ulp(expected));
114     }
115 }