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.exception.DimensionMismatchException;
23  import org.apache.commons.math4.legacy.exception.NullArgumentException;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  /**
28   * Test for class {@link Sigmoid}.
29   */
30  public class SigmoidTest {
31      private final double EPS = Math.ulp(1d);
32  
33      @Test
34      public void testSomeValues() {
35          final UnivariateFunction f = new Sigmoid();
36  
37          Assert.assertEquals(0.5, f.value(0), EPS);
38          Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), EPS);
39          Assert.assertEquals(1, f.value(Double.POSITIVE_INFINITY), EPS);
40      }
41  
42      @Test
43      public void testDerivative() {
44          final Sigmoid f = new Sigmoid();
45          final DerivativeStructure f0 = f.value(new DerivativeStructure(1, 1, 0, 0.0));
46  
47          Assert.assertEquals(0.25, f0.getPartialDerivative(1), 0);
48      }
49  
50      @Test
51      public void testDerivativesHighOrder() {
52          DerivativeStructure s = new Sigmoid(1, 3).value(new DerivativeStructure(1, 5, 0, 1.2));
53          Assert.assertEquals(2.5370495669980352859, s.getPartialDerivative(0), 5.0e-16);
54          Assert.assertEquals(0.35578888129361140441, s.getPartialDerivative(1), 6.0e-17);
55          Assert.assertEquals(-0.19107626464144938116,  s.getPartialDerivative(2), 6.0e-17);
56          Assert.assertEquals(-0.02396830286286711696,  s.getPartialDerivative(3), 4.0e-17);
57          Assert.assertEquals(0.21682059798981049049,   s.getPartialDerivative(4), 3.0e-17);
58          Assert.assertEquals(-0.19186320234632658055,  s.getPartialDerivative(5), 2.0e-16);
59      }
60  
61      @Test
62      public void testDerivativeLargeArguments() {
63          final Sigmoid f = new Sigmoid(1, 2);
64  
65          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.NEGATIVE_INFINITY)).getPartialDerivative(1), 0);
66          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -Double.MAX_VALUE)).getPartialDerivative(1), 0);
67          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e50)).getPartialDerivative(1), 0);
68          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e3)).getPartialDerivative(1), 0);
69          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e3)).getPartialDerivative(1), 0);
70          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e50)).getPartialDerivative(1), 0);
71          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.MAX_VALUE)).getPartialDerivative(1), 0);
72          Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.POSITIVE_INFINITY)).getPartialDerivative(1), 0);
73      }
74  
75      @Test(expected=NullArgumentException.class)
76      public void testParametricUsage1() {
77          final Sigmoid.Parametric g = new Sigmoid.Parametric();
78          g.value(0, null);
79      }
80  
81      @Test(expected=DimensionMismatchException.class)
82      public void testParametricUsage2() {
83          final Sigmoid.Parametric g = new Sigmoid.Parametric();
84          g.value(0, new double[] {0});
85      }
86  
87      @Test(expected=NullArgumentException.class)
88      public void testParametricUsage3() {
89          final Sigmoid.Parametric g = new Sigmoid.Parametric();
90          g.gradient(0, null);
91      }
92  
93      @Test(expected=DimensionMismatchException.class)
94      public void testParametricUsage4() {
95          final Sigmoid.Parametric g = new Sigmoid.Parametric();
96          g.gradient(0, new double[] {0});
97      }
98  
99      @Test
100     public void testParametricValue() {
101         final double lo = 2;
102         final double hi = 3;
103         final Sigmoid f = new Sigmoid(lo, hi);
104 
105         final Sigmoid.Parametric g = new Sigmoid.Parametric();
106         Assert.assertEquals(f.value(-1), g.value(-1, new double[] {lo, hi}), 0);
107         Assert.assertEquals(f.value(0), g.value(0, new double[] {lo, hi}), 0);
108         Assert.assertEquals(f.value(2), g.value(2, new double[] {lo, hi}), 0);
109     }
110 }