You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2015/04/09 14:38:57 UTC
[math] MATH-1206
Repository: commons-math
Updated Branches:
refs/heads/master c768ed307 -> 0a499402d
MATH-1206
New API methods in "LeastSquaresProblem.Evaluation" class.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0a499402
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0a499402
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0a499402
Branch: refs/heads/master
Commit: 0a499402d707bc8cf775d7f9b3840780a7401f7d
Parents: c768ed3
Author: Gilles <er...@apache.org>
Authored: Thu Apr 9 14:37:42 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Thu Apr 9 14:37:42 2015 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
.../leastsquares/AbstractEvaluation.java | 20 ++++++++++++------
.../leastsquares/LeastSquaresProblem.java | 22 ++++++++++++++++++--
.../math4/fitting/leastsquares/OptimumImpl.java | 10 +++++++++
.../leastsquares/EvaluationRmsCheckerTest.java | 8 +++++++
.../LevenbergMarquardtOptimizerTest.java | 19 +++++++++++++++++
6 files changed, 74 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 485458b..d20a18b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>
<release version="4.0" date="XXXX-XX-XX" description="">
+ <action dev="erans" type="update" issue="MATH-1206">
+ Added new API methods in "LeastSquares.Evaluation" (package "o.a.c.m.fitting.leastsquares").
+ </action>
<action dev="erans" type="update" issue="MATH-1210">
"QRDecomposition": include information about the condition that
triggers a "SingularMatrixException".
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/main/java/org/apache/commons/math4/fitting/leastsquares/AbstractEvaluation.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/AbstractEvaluation.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/AbstractEvaluation.java
index 2326b38..29e5065 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/AbstractEvaluation.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/AbstractEvaluation.java
@@ -40,8 +40,8 @@ public abstract class AbstractEvaluation implements Evaluation {
/**
* Constructor.
*
- * @param observationSize the number of observation. Needed for {@link
- * #getRMS()}.
+ * @param observationSize the number of observations.
+ * Needed for {@link #getRMS()} and {@link #getReducedChiSquare()}.
*/
AbstractEvaluation(final int observationSize) {
this.observationSize = observationSize;
@@ -74,14 +74,22 @@ public abstract class AbstractEvaluation implements Evaluation {
/** {@inheritDoc} */
public double getRMS() {
- final double cost = this.getCost();
- return FastMath.sqrt(cost * cost / this.observationSize);
+ return FastMath.sqrt(getReducedChiSquare(1));
}
/** {@inheritDoc} */
public double getCost() {
- final ArrayRealVector r = new ArrayRealVector(this.getResiduals());
- return FastMath.sqrt(r.dotProduct(r));
+ return FastMath.sqrt(getChiSquare());
}
+ /** {@inheritDoc} */
+ public double getChiSquare() {
+ final ArrayRealVector r = new ArrayRealVector(getResiduals());
+ return r.dotProduct(r);
+ }
+
+ /** {@inheritDoc} */
+ public double getReducedChiSquare(int numberOfFittedParameters) {
+ return getChiSquare() / (observationSize - numberOfFittedParameters + 1);
+ }
}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresProblem.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresProblem.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresProblem.java
index dfc5b5e..353c925 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresProblem.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/LeastSquaresProblem.java
@@ -86,7 +86,6 @@ public interface LeastSquaresProblem extends OptimizationProblem<LeastSquaresPro
* way for the caller to specify that the result of this computation should be
* considered meaningless, and thus trigger an exception.
*
- *
* @param threshold Singularity threshold.
* @return the covariance matrix.
* @throws org.apache.commons.math4.linear.SingularMatrixException
@@ -100,7 +99,6 @@ public interface LeastSquaresProblem extends OptimizationProblem<LeastSquaresPro
* matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} is the optimized
* value of the {@code i}-th parameter, and {@code C} is the covariance matrix.
*
- *
* @param covarianceSingularityThreshold Singularity threshold (see {@link
* #getCovariances(double) computeCovariances}).
* @return an estimate of the standard deviation of the optimized parameters
@@ -128,13 +126,33 @@ public interface LeastSquaresProblem extends OptimizationProblem<LeastSquaresPro
/**
* Get the cost.
+ * It is the square-root of the {@link #getChiSquare() objective function}.
*
* @return the cost.
* @see #getResiduals()
+ * @see #getChiSquare()
*/
double getCost();
/**
+ * Get the sum of the squares of the residuals.
+ *
+ * @return the cost.
+ * @see #getResiduals()
+ * @see #getCost()
+ */
+ double getChiSquare();
+
+ /**
+ * Get the reduced chi-square.
+ *
+ * @param n Number of fitted parameters.
+ * @return the sum of the squares of the residuals divided by the number
+ * of degrees of freedom.
+ */
+ double getReducedChiSquare(int n);
+
+ /**
* Get the weighted residuals. The residual is the difference between the
* observed (target) values and the model (objective function) value. There is one
* residual for each element of the vector-valued function. The raw residuals are
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/main/java/org/apache/commons/math4/fitting/leastsquares/OptimumImpl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/OptimumImpl.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/OptimumImpl.java
index e01b750..ddd478d 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/OptimumImpl.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/OptimumImpl.java
@@ -86,6 +86,16 @@ class OptimumImpl implements Optimum {
}
/** {@inheritDoc} */
+ public double getChiSquare() {
+ return value.getChiSquare();
+ }
+
+ /** {@inheritDoc} */
+ public double getReducedChiSquare(int n) {
+ return value.getReducedChiSquare(n);
+ }
+
+ /** {@inheritDoc} */
public RealVector getResiduals() {
return value.getResiduals();
}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/test/java/org/apache/commons/math4/fitting/leastsquares/EvaluationRmsCheckerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/fitting/leastsquares/EvaluationRmsCheckerTest.java b/src/test/java/org/apache/commons/math4/fitting/leastsquares/EvaluationRmsCheckerTest.java
index d98dcaa..767c04e 100644
--- a/src/test/java/org/apache/commons/math4/fitting/leastsquares/EvaluationRmsCheckerTest.java
+++ b/src/test/java/org/apache/commons/math4/fitting/leastsquares/EvaluationRmsCheckerTest.java
@@ -74,6 +74,14 @@ public class EvaluationRmsCheckerTest {
return 0;
}
+ public double getChiSquare() {
+ return 0;
+ }
+
+ public double getReducedChiSquare(int n) {
+ return 0;
+ }
+
public RealVector getResiduals() {
return null;
}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0a499402/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java b/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
index 73b443d..6198eb7 100644
--- a/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
+++ b/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
@@ -233,6 +233,25 @@ public class LevenbergMarquardtOptimizerTest
FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
}
}
+
+ // Check various measures of goodness-of-fit.
+ final double chi2 = optimum.getChiSquare();
+ final double cost = optimum.getCost();
+ final double rms = optimum.getRMS();
+ final double reducedChi2 = optimum.getReducedChiSquare(start.length);
+
+ // XXX Values computed by the CM code: It would be better to compare
+ // with the results from another library.
+ final double expectedChi2 = 66.07852350839286;
+ final double expectedReducedChi2 = 1.2014277001525975;
+ final double expectedCost = 8.128869755900439;
+ final double expectedRms = 1.0582887010256337;
+
+ final double tol = 1e14;
+ Assert.assertEquals(expectedChi2, chi2, tol);
+ Assert.assertEquals(expectedReducedChi2, reducedChi2, tol);
+ Assert.assertEquals(expectedCost, cost, tol);
+ Assert.assertEquals(expectedRms, rms, tol);
}
@Test