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  
23  package org.hipparchus.analysis.function;
24  
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.differentiation.DSFactory;
27  import org.hipparchus.analysis.differentiation.DerivativeStructure;
28  import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
29  import org.hipparchus.exception.MathIllegalArgumentException;
30  import org.hipparchus.exception.NullArgumentException;
31  import org.hipparchus.util.FastMath;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  /**
36   * Test for class {@link Gaussian}.
37   */
38  public class GaussianTest {
39      private final double EPS = Math.ulp(1d);
40  
41      @Test(expected=MathIllegalArgumentException.class)
42      public void testPreconditions() {
43          new Gaussian(1, 2, -1);
44      }
45  
46      @Test
47      public void testSomeValues() {
48          final UnivariateFunction f = new Gaussian();
49  
50          Assert.assertEquals(1 / FastMath.sqrt(2 * Math.PI), f.value(0), EPS);
51      }
52  
53      @Test
54      public void testLargeArguments() {
55          final UnivariateFunction f = new Gaussian();
56  
57          Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), 0);
58          Assert.assertEquals(0, f.value(-Double.MAX_VALUE), 0);
59          Assert.assertEquals(0, f.value(-1e2), 0);
60          Assert.assertEquals(0, f.value(1e2), 0);
61          Assert.assertEquals(0, f.value(Double.MAX_VALUE), 0);
62          Assert.assertEquals(0, f.value(Double.POSITIVE_INFINITY), 0);
63      }
64  
65      @Test
66      public void testDerivatives() {
67          final UnivariateDifferentiableFunction gaussian = new Gaussian(2.0, 0.9, 3.0);
68          final DerivativeStructure dsX = new DSFactory(1, 4).variable(0, 1.1);
69          final DerivativeStructure dsY = gaussian.value(dsX);
70          Assert.assertEquals( 1.9955604901712128349,   dsY.getValue(),              EPS);
71          Assert.assertEquals(-0.044345788670471396332, dsY.getPartialDerivative(1), EPS);
72          Assert.assertEquals(-0.22074348138190206174,  dsY.getPartialDerivative(2), EPS);
73          Assert.assertEquals( 0.014760030401924800557, dsY.getPartialDerivative(3), EPS);
74          Assert.assertEquals( 0.073253159785035691678, dsY.getPartialDerivative(4), EPS);
75      }
76  
77      @Test
78      public void testDerivativeLargeArguments() {
79          final Gaussian f = new Gaussian(0, 1e-50);
80  
81          DSFactory factory = new DSFactory(1, 1);
82          Assert.assertEquals(0, f.value(factory.variable(0, Double.NEGATIVE_INFINITY)).getPartialDerivative(1), 0);
83          Assert.assertEquals(0, f.value(factory.variable(0, -Double.MAX_VALUE)).getPartialDerivative(1), 0);
84          Assert.assertEquals(0, f.value(factory.variable(0, -1e50)).getPartialDerivative(1), 0);
85          Assert.assertEquals(0, f.value(factory.variable(0, -1e2)).getPartialDerivative(1), 0);
86          Assert.assertEquals(0, f.value(factory.variable(0, 1e2)).getPartialDerivative(1), 0);
87          Assert.assertEquals(0, f.value(factory.variable(0, 1e50)).getPartialDerivative(1), 0);
88          Assert.assertEquals(0, f.value(factory.variable(0, Double.MAX_VALUE)).getPartialDerivative(1), 0);
89          Assert.assertEquals(0, f.value(factory.variable(0, Double.POSITIVE_INFINITY)).getPartialDerivative(1), 0);
90      }
91  
92      @Test
93      public void testDerivativesNaN() {
94          final Gaussian f = new Gaussian(0, 1e-50);
95          final DerivativeStructure fx = f.value(new DSFactory(1, 5).variable(0, Double.NaN));
96          for (int i = 0; i <= fx.getOrder(); ++i) {
97              Assert.assertTrue(Double.isNaN(fx.getPartialDerivative(i)));
98          }
99      }
100 
101     @Test(expected=NullArgumentException.class)
102     public void testParametricUsage1() {
103         final Gaussian.Parametric g = new Gaussian.Parametric();
104         g.value(0, null);
105     }
106 
107     @Test(expected=MathIllegalArgumentException.class)
108     public void testParametricUsage2() {
109         final Gaussian.Parametric g = new Gaussian.Parametric();
110         g.value(0, new double[] {0});
111     }
112 
113     @Test(expected=MathIllegalArgumentException.class)
114     public void testParametricUsage3() {
115         final Gaussian.Parametric g = new Gaussian.Parametric();
116         g.value(0, new double[] {0, 1, 0});
117     }
118 
119     @Test(expected=NullArgumentException.class)
120     public void testParametricUsage4() {
121         final Gaussian.Parametric g = new Gaussian.Parametric();
122         g.gradient(0, null);
123     }
124 
125     @Test(expected=MathIllegalArgumentException.class)
126     public void testParametricUsage5() {
127         final Gaussian.Parametric g = new Gaussian.Parametric();
128         g.gradient(0, new double[] {0});
129     }
130 
131     @Test(expected=MathIllegalArgumentException.class)
132     public void testParametricUsage6() {
133         final Gaussian.Parametric g = new Gaussian.Parametric();
134         g.gradient(0, new double[] {0, 1, 0});
135     }
136 
137     @Test
138     public void testParametricValue() {
139         final double norm = 2;
140         final double mean = 3;
141         final double sigma = 4;
142         final Gaussian f = new Gaussian(norm, mean, sigma);
143 
144         final Gaussian.Parametric g = new Gaussian.Parametric();
145         Assert.assertEquals(f.value(-1), g.value(-1, new double[] {norm, mean, sigma}), 0);
146         Assert.assertEquals(f.value(0), g.value(0, new double[] {norm, mean, sigma}), 0);
147         Assert.assertEquals(f.value(2), g.value(2, new double[] {norm, mean, sigma}), 0);
148     }
149 
150     @Test
151     public void testParametricGradient() {
152         final double norm = 2;
153         final double mean = 3;
154         final double sigma = 4;
155         final Gaussian.Parametric f = new Gaussian.Parametric();
156 
157         final double x = 1;
158         final double[] grad = f.gradient(1, new double[] {norm, mean, sigma});
159         final double diff = x - mean;
160         final double n = FastMath.exp(-diff * diff / (2 * sigma * sigma));
161         Assert.assertEquals(n, grad[0], EPS);
162         final double m = norm * n * diff / (sigma * sigma);
163         Assert.assertEquals(m, grad[1], EPS);
164         final double s = m * diff / sigma;
165         Assert.assertEquals(s, grad[2], EPS);
166     }
167 }