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