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