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.exception.MathIllegalArgumentException;
29  import org.hipparchus.exception.NullArgumentException;
30  import org.hipparchus.util.FastMath;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  
36  /**
37   * Test for class {@link Logistic}.
38   */
39  class LogisticTest {
40      private final double EPS = Math.ulp(1d);
41  
42      @Test
43      void testPreconditions1() {
44          assertThrows(MathIllegalArgumentException.class, () -> new Logistic(1, 0, 1, 1, 0, -1));
45      }
46  
47      @Test
48      void testPreconditions2() {
49          assertThrows(MathIllegalArgumentException.class, () -> new Logistic(1, 0, 1, 1, 0, 0));
50      }
51  
52      @Test
53      void testCompareSigmoid() {
54          final UnivariateFunction sig = new Sigmoid();
55          final UnivariateFunction sigL = new Logistic(1, 0, 1, 1, 0, 1);
56  
57          final double min = -2;
58          final double max = 2;
59          final int n = 100;
60          final double delta = (max - min) / n;
61          for (int i = 0; i < n; i++) {
62              final double x = min + i * delta;
63              assertEquals(sig.value(x), sigL.value(x), EPS, "x=" + x);
64          }
65      }
66  
67      @Test
68      void testSomeValues() {
69          final double k = 4;
70          final double m = 5;
71          final double b = 2;
72          final double q = 3;
73          final double a = -1;
74          final double n = 2;
75  
76          final UnivariateFunction f = new Logistic(k, m, b, q, a, n);
77  
78          double x;
79          x = m;
80          assertEquals(a + (k - a) / FastMath.sqrt(1 + q), f.value(x), EPS, "x=" + x);
81  
82          x = Double.NEGATIVE_INFINITY;
83          assertEquals(a, f.value(x), EPS, "x=" + x);
84  
85          x = Double.POSITIVE_INFINITY;
86          assertEquals(k, f.value(x), EPS, "x=" + x);
87      }
88  
89      @Test
90      void testCompareDerivativeSigmoid() {
91          final double k = 3;
92          final double a = 2;
93  
94          final Logistic f = new Logistic(k, 0, 1, 1, a, 1);
95          final Sigmoid g = new Sigmoid(a, k);
96  
97          final double min = -10;
98          final double max = 10;
99          final double n = 20;
100         final double delta = (max - min) / n;
101         final DSFactory factory = new DSFactory(1, 5);
102         for (int i = 0; i < n; i++) {
103             final DerivativeStructure x = factory.variable(0, min + i * delta);
104             for (int order = 0; order <= x.getOrder(); ++order) {
105                 assertEquals(g.value(x).getPartialDerivative(order),
106                                     f.value(x).getPartialDerivative(order),
107                                     3.0e-15,
108                                     "x=" + x.getValue());
109             }
110         }
111     }
112 
113     @Test
114     void testParametricUsage1() {
115         assertThrows(NullArgumentException.class, () -> {
116             final Logistic.Parametric g = new Logistic.Parametric();
117             g.value(0, null);
118         });
119     }
120 
121     @Test
122     void testParametricUsage2() {
123         assertThrows(MathIllegalArgumentException.class, () -> {
124             final Logistic.Parametric g = new Logistic.Parametric();
125             g.value(0, new double[]{0});
126         });
127     }
128 
129     @Test
130     void testParametricUsage3() {
131         assertThrows(NullArgumentException.class, () -> {
132             final Logistic.Parametric g = new Logistic.Parametric();
133             g.gradient(0, null);
134         });
135     }
136 
137     @Test
138     void testParametricUsage4() {
139         assertThrows(MathIllegalArgumentException.class, () -> {
140             final Logistic.Parametric g = new Logistic.Parametric();
141             g.gradient(0, new double[]{0});
142         });
143     }
144 
145     @Test
146     void testParametricUsage5() {
147         assertThrows(MathIllegalArgumentException.class, () -> {
148             final Logistic.Parametric g = new Logistic.Parametric();
149             g.value(0, new double[]{1, 0, 1, 1, 0, 0});
150         });
151     }
152 
153     @Test
154     void testParametricUsage6() {
155         assertThrows(MathIllegalArgumentException.class, () -> {
156             final Logistic.Parametric g = new Logistic.Parametric();
157             g.gradient(0, new double[]{1, 0, 1, 1, 0, 0});
158         });
159     }
160 
161     @Test
162     void testGradientComponent0Component4() {
163         final double k = 3;
164         final double a = 2;
165 
166         final Logistic.Parametric f = new Logistic.Parametric();
167         // Compare using the "Sigmoid" function.
168         final Sigmoid.Parametric g = new Sigmoid.Parametric();
169 
170         final double x = 0.12345;
171         final double[] gf = f.gradient(x, new double[] {k, 0, 1, 1, a, 1});
172         final double[] gg = g.gradient(x, new double[] {a, k});
173 
174         assertEquals(gg[0], gf[4], EPS);
175         assertEquals(gg[1], gf[0], EPS);
176     }
177 
178     @Test
179     void testGradientComponent5() {
180         final double m = 1.2;
181         final double k = 3.4;
182         final double a = 2.3;
183         final double q = 0.567;
184         final double b = -FastMath.log(q);
185         final double n = 3.4;
186 
187         final Logistic.Parametric f = new Logistic.Parametric();
188 
189         final double x = m - 1;
190         final double qExp1 = 2;
191 
192         final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n});
193 
194         assertEquals((k - a) * FastMath.log(qExp1) / (n * n * FastMath.pow(qExp1, 1 / n)),
195                             gf[5], EPS);
196     }
197 
198     @Test
199     void testGradientComponent1Component2Component3() {
200         final double m = 1.2;
201         final double k = 3.4;
202         final double a = 2.3;
203         final double b = 0.567;
204         final double q = 1 / FastMath.exp(b * m);
205         final double n = 3.4;
206 
207         final Logistic.Parametric f = new Logistic.Parametric();
208 
209         final double x = 0;
210         final double qExp1 = 2;
211 
212         final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n});
213 
214         final double factor = (a - k) / (n * FastMath.pow(qExp1, 1 / n + 1));
215         assertEquals(factor * b, gf[1], EPS);
216         assertEquals(factor * m, gf[2], EPS);
217         assertEquals(factor / q, gf[3], EPS);
218     }
219 }