commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1332924 - in /commons/proper/math/trunk/src/test: java/org/apache/commons/math3/optimization/general/ resources/org/apache/commons/math3/optimization/general/
Date Wed, 02 May 2012 05:50:59 GMT
Author: celestin
Date: Wed May  2 05:50:59 2012
New Revision: 1332924

URL: http://svn.apache.org/viewvc?rev=1332924&view=rev
Log:
Standard unit test of least-squares optimizers based on NIST's Statistical Reference Datasets
(StRD).

Added:
    commons/proper/math/trunk/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat
Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.java?rev=1332924&r1=1332923&r2=1332924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.java
Wed May  2 05:50:59 2012
@@ -17,6 +17,7 @@
 package org.apache.commons.math3.optimization.general;
 
 import java.awt.geom.Point2D;
+import java.io.IOException;
 import java.io.Serializable;
 import java.util.Arrays;
 
@@ -466,6 +467,43 @@ public abstract class AbstractLeastSquar
         {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526}
     };
 
+    public void doTestStRD(final StatisticalReferenceDataset dataset,
+        final double errParams, final double errParamsSd) {
+        final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
+        final double[] w = new double[dataset.getNumObservations()];
+        Arrays.fill(w, 1.0);
+
+        final double[][] data = dataset.getData();
+        final double[] initial = dataset.getStartingPoint(0);
+        final DifferentiableMultivariateVectorFunction problem;
+        problem = dataset.getLeastSquaresProblem();
+        final PointVectorValuePair optimum;
+        optimum = optimizer.optimize(100, problem, data[1], w, initial);
+
+        final double[] actual = optimum.getPoint();
+        final double[] actualSig = optimizer.guessParametersErrors();
+        for (int i = 0; i < actual.length; i++) {
+            double expected = dataset.getParameter(i);
+            double delta = FastMath.abs(errParams * expected);
+            Assert.assertEquals(dataset.getName() + ", param #" + i,
+                                expected, actual[i], delta);
+            expected = dataset.getParameterStandardDeviation(i);
+            delta = FastMath.abs(errParamsSd * expected);
+            Assert.assertEquals(dataset.getName() + ", sd of param #" + i,
+                                expected, actualSig[i], delta);
+        }
+    }
+
+    @Test
+    public void testKirby2() throws IOException {
+        doTestStRD(StatisticalReferenceDatasetFactory.createKirby2(), 1E-7, 1E-7);
+    }
+
+    @Test
+    public void testHahn1() throws IOException {
+        doTestStRD(StatisticalReferenceDatasetFactory.createHahn1(), 1E-7, 1E-4);
+    }
+
     static class LinearProblem implements DifferentiableMultivariateVectorFunction, Serializable
{
 
         private static final long serialVersionUID = 703247177355019415L;

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.java?rev=1332924&r1=1332923&r2=1332924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.java
Wed May  2 05:50:59 2012
@@ -17,6 +17,8 @@
 
 package org.apache.commons.math3.optimization.general;
 
+import java.io.IOException;
+
 import org.apache.commons.math3.exception.ConvergenceException;
 import org.apache.commons.math3.exception.TooManyEvaluationsException;
 import org.apache.commons.math3.optimization.SimpleVectorValueChecker;
@@ -131,8 +133,19 @@ public class GaussNewtonOptimizerTest
     @Test(expected=ConvergenceException.class)
     public void testCircleFittingBadInit() {
         /*
-         * This test does not converge with this optimizer
+         * This test does not converge with this optimizer.
          */
         super.testCircleFittingBadInit();
     }
+
+    @Override
+    @Test(expected = ConvergenceException.class)
+    public void testHahn1()
+        throws IOException {
+        /*
+         * TODO This test leads to a singular problem with the Gauss-Newton
+         * optimizer. This should be inquired.
+         */
+        super.testHahn1();
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.java?rev=1332924&r1=1332923&r2=1332924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.java
Wed May  2 05:50:59 2012
@@ -152,6 +152,41 @@ public class StatisticalReferenceDataset
         return dataset;
     }
 
+    public static StatisticalReferenceDataset createLanczos1()
+        throws IOException {
+        final BufferedReader in =
+            createBufferedReaderFromResource("Lanczos1.dat");
+        StatisticalReferenceDataset dataset = null;
+        try {
+            dataset = new StatisticalReferenceDataset(in) {
+
+                @Override
+                public double getModelValue(final double x, final double[] a) {
+                    System.out.println(a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]);
+                    return a[0] * FastMath.exp(-a[3] * x) +
+                           a[1] * FastMath.exp(-a[4] * x) +
+                           a[2] * FastMath.exp(-a[5] * x);
+                }
+
+                @Override
+                public double[] getModelDerivatives(final double x,
+                    final double[] a) {
+                    final double[] dy = new double[6];
+                    dy[0] = FastMath.exp(-x * a[3]);
+                    dy[1] = FastMath.exp(-x * a[4]);
+                    dy[2] = FastMath.exp(-x * a[5]);
+                    dy[3] = -x * a[0] * dy[0];
+                    dy[4] = -x * a[1] * dy[1];
+                    dy[5] = -x * a[2] * dy[2];
+                    return dy;
+                }
+            };
+        } finally {
+            in.close();
+        }
+        return dataset;
+    }
+
     /**
      * Returns an array with all available reference datasets.
      *

Added: commons/proper/math/trunk/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat?rev=1332924&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat
(added)
+++ commons/proper/math/trunk/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat
Wed May  2 05:50:59 2012
@@ -0,0 +1,84 @@
+NIST/ITL StRD
+Dataset Name:  Lanczos1          (Lanczos1.dat)
+
+File Format:   ASCII
+               Starting Values   (lines 41 to 46)
+               Certified Values  (lines 41 to 51)
+               Data              (lines 61 to 84)
+
+Procedure:     Nonlinear Least Squares Regression
+
+Description:   These data are taken from an example discussed in
+               Lanczos (1956).  The data were generated to 14-digits
+               of accuracy using
+               f(x) = 0.0951*exp(-x) + 0.8607*exp(-3*x) 
+                                     + 1.5576*exp(-5*x).
+
+
+Reference:     Lanczos, C. (1956).
+               Applied Analysis.
+               Englewood Cliffs, NJ:  Prentice Hall, pp. 272-280.
+
+
+
+
+Data:          1 Response  (y)
+               1 Predictor (x)
+               24 Observations
+               Average Level of Difficulty
+               Generated Data
+
+Model:         Exponential Class
+               6 Parameters (b1 to b6)
+
+               y = b1*exp(-b2*x) + b3*exp(-b4*x) + b5*exp(-b6*x)  +  e
+
+
+ 
+          Starting values                  Certified Values
+ 
+        Start 1     Start 2           Parameter     Standard Deviation
+  b1 =   1.2         0.5           9.5100000027E-02  5.3347304234E-11
+  b2 =   0.3         0.7           1.0000000001E+00  2.7473038179E-10
+  b3 =   5.6         3.6           8.6070000013E-01  1.3576062225E-10
+  b4 =   5.5         4.2           3.0000000002E+00  3.3308253069E-10
+  b5 =   6.5         4             1.5575999998E+00  1.8815731448E-10
+  b6 =   7.6         6.3           5.0000000001E+00  1.1057500538E-10
+
+Residual Sum of Squares:                    1.4307867721E-25
+Residual Standard Deviation:                8.9156129349E-14
+Degrees of Freedom:                                18
+Number of Observations:                            24
+
+
+
+
+
+
+
+
+Data:   y                   x
+       2.513400000000E+00  0.000000000000E+00
+       2.044333373291E+00  5.000000000000E-02
+       1.668404436564E+00  1.000000000000E-01
+       1.366418021208E+00  1.500000000000E-01
+       1.123232487372E+00  2.000000000000E-01
+       9.268897180037E-01  2.500000000000E-01
+       7.679338563728E-01  3.000000000000E-01
+       6.388775523106E-01  3.500000000000E-01
+       5.337835317402E-01  4.000000000000E-01
+       4.479363617347E-01  4.500000000000E-01
+       3.775847884350E-01  5.000000000000E-01
+       3.197393199326E-01  5.500000000000E-01
+       2.720130773746E-01  6.000000000000E-01
+       2.324965529032E-01  6.500000000000E-01
+       1.996589546065E-01  7.000000000000E-01
+       1.722704126914E-01  7.500000000000E-01
+       1.493405660168E-01  8.000000000000E-01
+       1.300700206922E-01  8.500000000000E-01
+       1.138119324644E-01  9.000000000000E-01
+       1.000415587559E-01  9.500000000000E-01
+       8.833209084540E-02  1.000000000000E+00
+       7.833544019350E-02  1.050000000000E+00
+       6.976693743449E-02  1.100000000000E+00
+       6.239312536719E-02  1.150000000000E+00



Mime
View raw message