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