commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1569361 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/fitting/leastsquares/ test/java/org/apache/commons/math3/fitting/leastsquares/
Date Tue, 18 Feb 2014 14:33:42 GMT
Author: luc
Date: Tue Feb 18 14:33:41 2014
New Revision: 1569361

URL: http://svn.apache.org/r1569361
Log:
Expand Evaluation tests, use weighted values.
    
Added more test cases to EvaluationTest to test the other methods of
DenseWeightedEvaluation/UnweightedEvaluation.
    
Fixed a bug in DenseWeightedEvaluation.computeValue() where the the
weights were not applied to the return value.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/DenseWeightedEvaluation.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/DenseWeightedEvaluation.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/DenseWeightedEvaluation.java?rev=1569361&r1=1569360&r2=1569361&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/DenseWeightedEvaluation.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/DenseWeightedEvaluation.java
Tue Feb 18 14:33:41 2014
@@ -42,6 +42,11 @@ class DenseWeightedEvaluation extends Ab
         return this.weightSqrt.operate(this.unweighted.computeResiduals());
     }
 
+    /** {@inheritDoc} */
+    public RealVector computeValue() {
+        return this.weightSqrt.operate(unweighted.computeValue());
+    }
+
     /* delegate */
 
     /** {@inheritDoc} */
@@ -49,8 +54,4 @@ class DenseWeightedEvaluation extends Ab
         return unweighted.getPoint();
     }
 
-    /** {@inheritDoc} */
-    public RealVector computeValue() {
-        return unweighted.computeValue();
-    }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.java?rev=1569361&r1=1569360&r2=1569361&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblem.java
Tue Feb 18 14:33:41 2014
@@ -92,9 +92,9 @@ public interface LeastSquaresProblem ext
         double computeRMS();
 
         /**
-         * Computes the objective (model) function value.
+         * Computes the weighted objective (model) function value.
          *
-         * @return the objective function value at the specified point.
+         * @return the weighted objective function value at the specified point.
          */
         RealVector computeValue();
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java?rev=1569361&r1=1569360&r2=1569361&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
Tue Feb 18 14:33:41 2014
@@ -13,15 +13,23 @@
  */
 package org.apache.commons.math3.fitting.leastsquares;
 
+import org.apache.commons.math3.TestUtils;
 import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
+import org.apache.commons.math3.linear.ArrayRealVector;
 import org.apache.commons.math3.linear.DiagonalMatrix;
+import org.apache.commons.math3.linear.MatrixUtils;
+import org.apache.commons.math3.linear.RealMatrix;
 import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.linear.SingularMatrixException;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Pair;
+import org.apache.commons.math3.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Arrays;
+
 /**
  * The only features tested here are utility methods defined
  * in {@link LeastSquaresProblem.Evaluation} that compute the
@@ -51,6 +59,102 @@ public class EvaluationTest {
     }
 
     @Test
+    public void testComputeResiduals() {
+        //setup
+        RealVector point = new ArrayRealVector(2);
+        Evaluation evaluation = new LeastSquaresBuilder()
+                .target(new ArrayRealVector(new double[]{3,-1}))
+                .model(new MultivariateJacobianFunction() {
+                    public Pair<RealVector, RealMatrix> value(RealVector point) {
+                        return new Pair<RealVector, RealMatrix>(
+                                new ArrayRealVector(new double[]{1, 2}),
+                                MatrixUtils.createRealIdentityMatrix(2)
+                        );
+                    }
+                })
+                .weight(MatrixUtils.createRealIdentityMatrix(2))
+                .build()
+                .evaluate(point);
+
+        //action + verify
+        Assert.assertArrayEquals(
+                evaluation.computeResiduals().toArray(),
+                new double[]{2, -3},
+                Precision.EPSILON);
+    }
+
+    @Test
+    public void testComputeCovariance() throws IOException {
+        //setup
+        RealVector point = new ArrayRealVector(2);
+        Evaluation evaluation = new LeastSquaresBuilder()
+                .model(new MultivariateJacobianFunction() {
+                    public Pair<RealVector, RealMatrix> value(RealVector point) {
+                        return new Pair<RealVector, RealMatrix>(
+                                new ArrayRealVector(2),
+                                MatrixUtils.createRealDiagonalMatrix(new double[]{1, 1e-2})
+                        );
+                    }
+                })
+                .weight(MatrixUtils.createRealDiagonalMatrix(new double[]{1, 1}))
+                .target(new ArrayRealVector(2))
+                .build()
+                .evaluate(point);
+
+        //action
+        TestUtils.assertEquals(
+                "covariance",
+                evaluation.computeCovariances(FastMath.nextAfter(1e-4, 0.0)),
+                MatrixUtils.createRealMatrix(new double[][]{{1, 0}, {0, 1e4}}),
+                Precision.EPSILON
+        );
+
+        //singularity fail
+        try {
+            evaluation.computeCovariances(FastMath.nextAfter(1e-4, 1.0));
+            Assert.fail("Expected Exception");
+        } catch (SingularMatrixException e) {
+            //expected
+        }
+    }
+
+    @Test
+    public void testComputeValueAndJacobian() {
+        //setup
+        final RealVector point = new ArrayRealVector(new double[]{1, 2});
+        Evaluation evaluation = new LeastSquaresBuilder()
+                .weight(new DiagonalMatrix(new double[]{16, 4}))
+                .model(new MultivariateJacobianFunction() {
+                    public Pair<RealVector, RealMatrix> value(RealVector actualPoint)
{
+                        //verify correct values passed in
+                        Assert.assertArrayEquals(
+                                point.toArray(), actualPoint.toArray(), Precision.EPSILON);
+                        //return values
+                        return new Pair<RealVector, RealMatrix>(
+                                new ArrayRealVector(new double[]{3, 4}),
+                                MatrixUtils.createRealMatrix(new double[][]{{5, 6}, {7, 8}})
+                        );
+                    }
+                })
+                .target(new double[2])
+                .build()
+                .evaluate(point);
+
+        //action
+        RealVector value = evaluation.computeValue();
+        RealMatrix jacobian = evaluation.computeJacobian();
+
+        //verify
+        Assert.assertArrayEquals(evaluation.getPoint().toArray(), point.toArray(), 0);
+        Assert.assertArrayEquals(new double[]{12, 8}, value.toArray(), Precision.EPSILON);
+        TestUtils.assertEquals(
+                "jacobian",
+                jacobian,
+                MatrixUtils.createRealMatrix(new double[][]{{20, 24},{14, 16}}),
+                Precision.EPSILON);
+    }
+
+    @Test
     public void testComputeCost() throws IOException {
         final StatisticalReferenceDataset dataset
             = StatisticalReferenceDatasetFactory.createKirby2();



Mime
View raw message