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