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