commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1074954 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/function/Logit.java test/java/org/apache/commons/math/analysis/function/LogitTest.java
Date Sat, 26 Feb 2011 23:07:49 GMT
Author: erans
Date: Sat Feb 26 23:07:49 2011
New Revision: 1074954

URL: http://svn.apache.org/viewvc?rev=1074954&view=rev
Log:
MATH-503
"Logit" function.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java
  (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java
  (with props)

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java?rev=1074954&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java
Sat Feb 26 23:07:49 2011
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.exception.OutOfRangeException;
+import org.apache.commons.math.util.FastMath;
+
+/**
+ * <a href="http://en.wikipedia.org/wiki/Logit">
+ *  logit</a> function.
+ * It is the inverse of the {@link Sigmoid sigmoid} function.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class Logit implements DifferentiableUnivariateRealFunction {
+    /** Lower bound. */
+    private final double lo;
+    /** Higher bound. */
+    private final double hi;
+
+    /**
+     * Usual logit function, where the lower bound is 0 and the higher
+     * bound is 1.
+     */
+    public Logit() {
+        this(0, 1);
+    }
+
+    /**
+     * Logit function.
+     *
+     * @param lo Lower bound of the function domain.
+     * @param hi Higher bound of the function domain.
+     */
+    public Logit(double lo,
+                 double hi) {
+        this.lo = lo;
+        this.hi = hi;
+    }
+
+    /** {@inheritDoc} */
+    public double value(double x) {
+        return value(x, lo, hi);
+    }
+
+    /** {@inheritDoc} */
+    public UnivariateRealFunction derivative() {
+        return new UnivariateRealFunction() {
+            /** {@inheritDoc} */
+            public double value(double x) {
+                return (hi - lo) / ((x - lo) * (hi - x));
+            }
+        };
+    }
+
+    /**
+     * Parametric function where the input array contains the parameters of
+     * the logit function, ordered as follows:
+     * <ul>
+     *  <li>Lower bound</li>
+     *  <li>Higher bound</li>
+     * </ul>
+     */
+    public static class Parametric implements ParametricUnivariateRealFunction {
+        /**
+         * Computes the value of the logit at {@code x}.
+         *
+         * @param x Value for which the function must be computed.
+         * @param param Values of lower bound and higher bounds.
+         * @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 Logit.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 bound and higher bound).
+         *
+         * @param x Value at which the gradient must be computed.
+         * @param param Values for lower and higher bounds.
+         * @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];
+
+            return new double[] { 1 / (lo - x), 1 / (hi - x) };
+        }
+
+        /**
+         * 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 bounds.
+         * @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 logit.
+     * @param lo Lower bound.
+     * @param hi Higher bound.
+     * @return the value of the logit function at {@code x}.
+     */
+    private static double value(double x,
+                                double lo,
+                                double hi) {
+        if (x < lo || x > hi) {
+            throw new OutOfRangeException(x, lo, hi);
+        }
+        return FastMath.log((x - lo) / (hi - x));
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Logit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java?rev=1074954&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java
(added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java
Sat Feb 26 23:07:49 2011
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.analysis.function;
+
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.FunctionUtils;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.exception.OutOfRangeException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for class {@link Logit}.
+ */
+public class LogitTest {
+    private final double EPS = Math.ulp(1d);
+
+    @Test(expected=OutOfRangeException.class)
+    public void testPreconditions1() {
+        final double lo = -1;
+        final double hi = 2;
+        final UnivariateRealFunction f = new Logit(lo, hi);
+
+        f.value(lo - 1);
+    }
+    @Test(expected=OutOfRangeException.class)
+    public void testPreconditions2() {
+        final double lo = -1;
+        final double hi = 2;
+        final UnivariateRealFunction f = new Logit(lo, hi);
+
+        f.value(hi + 1);
+    }
+
+    @Test
+    public void testSomeValues() {
+        final double lo = 1;
+        final double hi = 2;
+        final UnivariateRealFunction f = new Logit(lo, hi);
+
+        Assert.assertEquals(Double.NEGATIVE_INFINITY, f.value(1), EPS);
+        Assert.assertEquals(Double.POSITIVE_INFINITY, f.value(2), EPS);
+        Assert.assertEquals(0, f.value(1.5), EPS);
+    }
+
+    @Test
+    public void testDerivative() {
+        final double lo = 1;
+        final double hi = 2;
+        final Logit f = new Logit(lo, hi);
+        final UnivariateRealFunction dfdx = f.derivative();
+
+        Assert.assertEquals(4, dfdx.value(1.5), EPS);
+    }
+
+    @Test
+    public void testDerivativeLargeArguments() {
+        final Logit f = new Logit(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(-1e155), 0);
+        Assert.assertEquals(0, dfdx.value(1e155), 0);
+        Assert.assertEquals(0, dfdx.value(Double.MAX_VALUE), 0);
+        Assert.assertEquals(0, dfdx.value(Double.POSITIVE_INFINITY), 0);        
+    }
+
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage1() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.value(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage2() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.value(0, new double[] {0});
+    }
+
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage3() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.gradient(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage4() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.gradient(0, new double[] {0});
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testParametricUsage5() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.value(-1, new double[] {0, 1});
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testParametricUsage6() {
+        final Logit.Parametric g = new Logit.Parametric();
+        g.value(2, new double[] {0, 1});
+    }
+
+    @Test
+    public void testParametricValue() {
+        final double lo = 2;
+        final double hi = 3;
+        final Logit f = new Logit(lo, hi);
+
+        final Logit.Parametric g = new Logit.Parametric();
+        Assert.assertEquals(f.value(2), g.value(2, new double[] {lo, hi}), 0);
+        Assert.assertEquals(f.value(2.34567), g.value(2.34567, new double[] {lo, hi}), 0);
+        Assert.assertEquals(f.value(3), g.value(3, new double[] {lo, hi}), 0);
+    }
+
+    @Test
+    public void testValueWithInverseFunction() {
+        final double lo = 2;
+        final double hi = 3;
+        final Logit f = new Logit(lo, hi);
+        final Sigmoid g = new Sigmoid(lo, hi);
+        final UnivariateRealFunction id = FunctionUtils.compose(g, f);
+        
+        for (int i = 0; i < 10; i++) {
+            final double x = lo + Math.random() * (hi - lo);
+            Assert.assertEquals(x, id.value(x), EPS);
+        }
+
+        Assert.assertEquals(lo, id.value(lo), EPS);
+        Assert.assertEquals(hi, id.value(hi), EPS);
+    }
+
+    @Test
+    public void testDerivativeWithInverseFunction() {
+        final double lo = 2;
+        final double hi = 3;
+        final Logit f = new Logit(lo, hi);
+        final UnivariateRealFunction dfdx = f.derivative();
+        final Sigmoid g = new Sigmoid(lo, hi);
+        final UnivariateRealFunction dgdx = g.derivative();
+        final UnivariateRealFunction chain
+            = FunctionUtils.compose(new Inverse(), FunctionUtils.compose(dgdx, f));
+        
+        for (int i = 0; i < 10; i++) {
+            final double x = lo + Math.random() * (hi - lo);
+            Assert.assertEquals(dfdx.value(x), chain.value(x), 1e-13);
+        }
+
+        Assert.assertEquals(dfdx.value(lo), chain.value(lo), EPS);
+        Assert.assertEquals(dfdx.value(hi), chain.value(hi), EPS);
+    }
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/LogitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message