commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1074950 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/function/Sigmoid.java test/java/org/apache/commons/math/analysis/function/SigmoidTest.java
Date Sat, 26 Feb 2011 22:58:59 GMT
Author: erans
Date: Sat Feb 26 22:58:59 2011
New Revision: 1074950

URL: http://svn.apache.org/viewvc?rev=1074950&view=rev
Log:
MATH-503
Added derivative and parametric version of the function.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sigmoid.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SigmoidTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sigmoid.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sigmoid.java?rev=1074950&r1=1074949&r2=1074950&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sigmoid.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sigmoid.java
Sat Feb 26 22:58:59 2011
@@ -18,20 +18,146 @@
 package org.apache.commons.math.analysis.function;
 
 import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
+import org.apache.commons.math.analysis.ParametricUnivariateRealFunction;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.util.FastMath;
 
 /**
  * <a href="http://en.wikipedia.org/wiki/Sigmoid_function">
- *  Sigmoid</a> function.
+ *  sigmoid</a> function.
+ * It is the inverse of the {@link Logit logit} function.
  * A more flexible version, the generalised logistic, is implemented
  * by the {@link Logistic} class.
  *
  * @version $Revision$ $Date$
  * @since 3.0
  */
-public class Sigmoid implements UnivariateRealFunction {
+public class Sigmoid implements DifferentiableUnivariateRealFunction {
+    /** Lower asymptote. */
+    private final double lo;
+    /** Higher asymptote. */
+    private final double hi;
+
+    /**
+     * Usual sigmoid function, where the lower asymptote is 0 and the higher
+     * asymptote is 1.
+     */
+    public Sigmoid() {
+        this(0, 1);
+    }
+
+    /**
+     * Sigmoid function.
+     *
+     * @param lo Lower asymptote.
+     * @param hi Higher asymptote.
+     */
+    public Sigmoid(double lo,
+                   double hi) {
+        this.lo = lo;
+        this.hi = hi;
+    }
+
+    /** {@inheritDoc} */
+    public UnivariateRealFunction derivative() {
+        return new UnivariateRealFunction() {
+            /** {@inheritDoc} */
+            public double value(double x) {
+                final double exp = FastMath.exp(-x);
+                if (Double.isInfinite(exp)) {
+                    // Avoid returning NaN in case of overflow.
+                    return 0;
+                }
+                final double exp1 = 1 + exp;
+                return (hi - lo) * exp / (exp1 * exp1);
+            }
+        };
+    }
+
     /** {@inheritDoc} */
     public double value(double x) {
-        return 1 / (1 + FastMath.exp(-x));
+        return value(x, lo, hi);
+    }
+
+    /**
+     * Parametric function where the input array contains the parameters of
+     * the logit function, ordered as follows:
+     * <ul>
+     *  <li>Lower asymptote</li>
+     *  <li>Higher asymptote</li>
+     * </ul>
+     */
+    public static class Parametric implements ParametricUnivariateRealFunction {
+        /**
+         * Computes the value of the sigmoid at {@code x}.
+         *
+         * @param x Value for which the function must be computed.
+         * @param param Values of lower asymptote and higher asymptote.
+         * @return the value of the function.
+         * @throws NullArgumentException if {@code param} is {@code null}.
+         * @throws DimensionMismatchException if the size of {@code param} is
+         * not 2.
+         */
+        public double value(double x,
+                            double[] param) {
+            validateParameters(param);
+            return Sigmoid.value(x, param[0], param[1]);
+        }
+
+        /**
+         * Computes the value of the gradient at {@code x}.
+         * The components of the gradient vector are the partial
+         * derivatives of the function with respect to each of the
+         * <em>parameters</em> (lower asymptote and higher asymptote).
+         *
+         * @param x Value at which the gradient must be computed.
+         * @param param Values for lower asymptote and higher asymptote.
+         * @return the gradient vector at {@code x}.
+         * @throws NullArgumentException if {@code param} is {@code null}.
+         * @throws DimensionMismatchException if the size of {@code param} is
+         * not 2.
+         */
+        public double[] gradient(double x, double[] param) {
+            validateParameters(param);
+
+            final double lo = param[0];
+            final double hi = param[1];
+            final double invExp1 = 1 / (1 + FastMath.exp(-x));
+
+            return new double[] { 1 - invExp1, invExp1 };
+        }
+
+        /**
+         * Validates parameters to ensure they are appropriate for the evaluation of
+         * the {@link #value(double,double[])} and {@link #gradient(double,double[])}
+         * methods.
+         *
+         * @param param Values for lower and higher asymptotes.
+         * @throws NullArgumentException if {@code param} is {@code null}.
+         * @throws DimensionMismatchException if the size of {@code param} is
+         * not 2.
+         */
+        private void validateParameters(double[] param) {
+            if (param == null) {
+                throw new NullArgumentException();
+            }
+            if (param.length != 2) {
+                throw new DimensionMismatchException(param.length, 2);
+            }
+        }
+    }
+
+    /**
+     * @param x Value at which to compute the sigmoid.
+     * @param lo Lower asymptote.
+     * @param hi Higher asymptote.
+     * @return the value of the sigmoid function at {@code x}.
+     */
+    private static double value(double x,
+                                double lo,
+                                double hi) {
+        return lo + (hi - lo) / (1 + FastMath.exp(-x));
     }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SigmoidTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SigmoidTest.java?rev=1074950&r1=1074949&r2=1074950&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SigmoidTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SigmoidTest.java
Sat Feb 26 22:58:59 2011
@@ -18,6 +18,8 @@
 package org.apache.commons.math.analysis.function;
 
 import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -32,14 +34,67 @@ public class SigmoidTest {
     public void testSomeValues() {
         final UnivariateRealFunction f = new Sigmoid();
 
-        double x;
-        x = 0;
-        Assert.assertEquals("x=" + x, 0.5, f.value(x), EPS);
+        Assert.assertEquals(0.5, f.value(0), EPS);
+        Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), EPS);
+        Assert.assertEquals(1, f.value(Double.POSITIVE_INFINITY), EPS);
+    }
+
+    @Test
+    public void testDerivative() {
+        final Sigmoid f = new Sigmoid();
+        final UnivariateRealFunction dfdx = f.derivative();
+
+        Assert.assertEquals(0.25, dfdx.value(0), 0);
+    }
+
+    @Test
+    public void testDerivativeLargeArguments() {
+        final Sigmoid f = new Sigmoid(1, 2);
+        final UnivariateRealFunction dfdx = f.derivative();
+
+        Assert.assertEquals(0, dfdx.value(Double.NEGATIVE_INFINITY), 0);
+        Assert.assertEquals(0, dfdx.value(-Double.MAX_VALUE), 0);
+        Assert.assertEquals(0, dfdx.value(-1e50), 0);
+        Assert.assertEquals(0, dfdx.value(-1e3), 0);
+        Assert.assertEquals(0, dfdx.value(1e3), 0);
+        Assert.assertEquals(0, dfdx.value(1e50), 0);
+        Assert.assertEquals(0, dfdx.value(Double.MAX_VALUE), 0);
+        Assert.assertEquals(0, dfdx.value(Double.POSITIVE_INFINITY), 0);        
+    }
 
-        x = Double.NEGATIVE_INFINITY;
-        Assert.assertEquals("x=" + x, 0, f.value(x), EPS);
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage1() {
+        final Sigmoid.Parametric g = new Sigmoid.Parametric();
+        g.value(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage2() {
+        final Sigmoid.Parametric g = new Sigmoid.Parametric();
+        g.value(0, new double[] {0});
+    }
+
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage3() {
+        final Sigmoid.Parametric g = new Sigmoid.Parametric();
+        g.gradient(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage4() {
+        final Sigmoid.Parametric g = new Sigmoid.Parametric();
+        g.gradient(0, new double[] {0});
+    }
 
-        x = Double.POSITIVE_INFINITY;
-        Assert.assertEquals("x=" + x, 1, f.value(x), EPS);
+    @Test
+    public void testParametricValue() {
+        final double lo = 2;
+        final double hi = 3;
+        final Sigmoid f = new Sigmoid(lo, hi);
+
+        final Sigmoid.Parametric g = new Sigmoid.Parametric();
+        Assert.assertEquals(f.value(-1), g.value(-1, new double[] {lo, hi}), 0);
+        Assert.assertEquals(f.value(0), g.value(0, new double[] {lo, hi}), 0);
+        Assert.assertEquals(f.value(2), g.value(2, new double[] {lo, hi}), 0);
     }
 }



Mime
View raw message