commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1071430 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/function/Gaussian.java test/java/org/apache/commons/math/analysis/function/GaussianTest.java
Date Wed, 16 Feb 2011 22:48:43 GMT
Author: erans
Date: Wed Feb 16 22:48:43 2011
New Revision: 1071430

URL: http://svn.apache.org/viewvc?rev=1071430&view=rev
Log:
MATH-514
Added a "Parametric" inner class.

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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java?rev=1071430&r1=1071429&r2=1071430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
Wed Feb 16 22:48:43 2011
@@ -19,7 +19,10 @@ package org.apache.commons.math.analysis
 
 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.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.util.FastMath;
 
 /**
@@ -78,8 +81,7 @@ public class Gaussian implements Differe
 
     /** {@inheritDoc} */
     public double value(double x) {
-        final double diff = x - mean;
-        return norm * FastMath.exp(-diff * diff * i2s2);
+        return value(x - mean, norm, i2s2);
     }
 
     /** {@inheritDoc} */
@@ -88,7 +90,7 @@ public class Gaussian implements Differe
             /** {@inheritDoc} */
             public double value(double x) {
                 final double diff = x - mean;
-                final double g = Gaussian.this.value(x);
+                final double g = Gaussian.value(diff, norm, i2s2);
 
                 if (g == 0) {
                     // Avoid returning NaN in case of overflow.
@@ -99,4 +101,99 @@ public class Gaussian implements Differe
             }
         };
     }
+
+    /**
+     * Parametric function where the input array contains the parameters of
+     * the Gaussian, ordered as follows:
+     * <ul>
+     *  <li>Norm</li>
+     *  <li>Mean</li>
+     *  <li>Standard deviation</li>
+     * </ul>
+     */
+    public static class Parametric implements ParametricUnivariateRealFunction {
+        /**
+         * Computes the value of the Gaussian at {@code x}.
+         *
+         * @param x Value for which the function must be computed.
+         * @param param Values of norm, mean and standard deviation.
+         * @return the value of the function.
+         * @throws NullArgumentException if {@code param} is {@code null}.
+         * @throws DimensionMismatchException if the size of {@code param} is
+         * not 3.
+         * @throws NotStrictlyPositiveException if {@code param[2]} is negative.
+         */
+        public double value(double x,
+                            double[] param) {
+            validateParameters(param);
+
+            final double diff = x - param[1];
+            final double i2s2 = 1 / (2 * param[2] * param[2]);
+            return Gaussian.value(diff, param[0], i2s2);
+        }
+
+        /**
+         * 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> (norm, mean and standard deviation).
+         *
+         * @param x Value at which the gradient must be computed.
+         * @param param Values of norm, mean and standard deviation.
+         * @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 3.
+         * @throws NotStrictlyPositiveException if {@code param[2]} is negative.
+         */
+        public double[] gradient(double x, double[] param) {
+            validateParameters(param);
+
+            final double norm = param[0];
+            final double diff = x - param[1];
+            final double sigma = param[2];
+            final double i2s2 = 1 / (2 * sigma * sigma);
+
+            final double n = Gaussian.value(diff, 1, i2s2);
+            final double m = norm * n * 2 * i2s2 * diff;
+            final double s = m * diff / sigma;
+
+            return new double[] { n, m, s };
+        }
+
+        /**
+         * 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 of norm, mean and standard deviation.
+         * @throws NullArgumentException if {@code param} is {@code null}.
+         * @throws DimensionMismatchException if the size of {@code param} is
+         * not 3.
+         * @throws NotStrictlyPositiveException if {@code param[2]} is negative.
+         */
+        private void validateParameters(double[] param) {
+            if (param == null) {
+                throw new NullArgumentException();
+            }
+            if (param.length != 3) {
+                throw new DimensionMismatchException(param.length, 3);
+            }
+            if (param[2] <= 0) {
+                throw new NotStrictlyPositiveException(param[2]);
+            }
+        }
+    }
+
+    /**
+     * @param xMinusMean {@code x - mean}.
+     * @param norm Normalization factor.
+     * @param i2s2 Inverse of twice the square of the standard deviation.
+     * @return the value of the Gaussian at {@code x}.
+     */
+    private static double value(double xMinusMean,
+                                double norm,
+                                double i2s2) {
+        return norm * FastMath.exp(-xMinusMean * xMinusMean * i2s2);
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java?rev=1071430&r1=1071429&r2=1071430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
Wed Feb 16 22:48:43 2011
@@ -19,6 +19,8 @@ package org.apache.commons.math.analysis
 
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.util.FastMath;
 
 import org.junit.Assert;
@@ -84,4 +86,71 @@ public class GaussianTest {
 
         Assert.assertTrue(Double.isNaN(dfdx.value(Double.NaN)));
     }
+
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage1() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.value(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage2() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.value(0, new double[] {0});
+    }
+
+    @Test(expected=NotStrictlyPositiveException.class)
+    public void testParametricUsage3() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.value(0, new double[] {0, 1, 0});
+    }
+
+    @Test(expected=NullArgumentException.class)
+    public void testParametricUsage4() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.gradient(0, null);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testParametricUsage5() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.gradient(0, new double[] {0});
+    }
+
+    @Test(expected=NotStrictlyPositiveException.class)
+    public void testParametricUsage6() {
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        g.gradient(0, new double[] {0, 1, 0});
+    }
+
+    @Test
+    public void testParametricValue() {
+        final double norm = 2;
+        final double mean = 3;
+        final double sigma = 4;
+        final Gaussian f = new Gaussian(norm, mean, sigma);
+
+        final Gaussian.Parametric g = new Gaussian.Parametric();
+        Assert.assertEquals(f.value(-1), g.value(-1, new double[] {norm, mean, sigma}), 0);
+        Assert.assertEquals(f.value(0), g.value(0, new double[] {norm, mean, sigma}), 0);
+        Assert.assertEquals(f.value(2), g.value(2, new double[] {norm, mean, sigma}), 0);
+    }
+
+    @Test
+    public void testParametricGradient() {
+        final double norm = 2;
+        final double mean = 3;
+        final double sigma = 4;
+        final Gaussian.Parametric f = new Gaussian.Parametric();
+
+        final double x = 1;
+        final double[] grad = f.gradient(1, new double[] {norm, mean, sigma});
+        final double diff = x - mean;
+        final double n = FastMath.exp(-diff * diff / (2 * sigma * sigma));
+        Assert.assertEquals(n, grad[0], EPS);
+        final double m = norm * n * diff / (sigma * sigma);
+        Assert.assertEquals(m, grad[1], EPS);
+        final double s = m * diff / sigma;
+        Assert.assertEquals(s, grad[2], EPS);
+    }
 }



Mime
View raw message