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