From f3b02ccea3be696a8fc0d3f6d0cabcc7b718eb97 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Mon, 26 Feb 2007 22:59:45 +0000 Subject: [PATCH] added the estimation package from Mantissa git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@512061 13f79535-47bb-0310-9956-ffa450edef68 --- .../math}/estimation/EstimatedParameter.java | 2 +- .../math}/estimation/EstimationException.java | 47 ++-- .../math}/estimation/EstimationProblem.java | 2 +- .../commons/math}/estimation/Estimator.java | 2 +- .../estimation/GaussNewtonEstimator.java | 73 ++--- .../LevenbergMarquardtEstimator.java | 2 +- .../math}/estimation/WeightedMeasurement.java | 2 +- .../commons/math}/estimation/package.html | 0 .../estimation/LeastSquaresEstimator.java | 73 ----- ...spaceroots_mantissa_estimation_classes.png | Bin 3376 -> 0 bytes .../mantissa/estimation/AllTests.java | 39 --- .../estimation/GaussNewtonEstimatorTest.java | 262 ------------------ .../estimation/EstimatedParameterTest.java | 4 +- .../LevenbergMarquardtEstimatorTest.java | 12 +- .../commons/math}/estimation/MinpackTest.java | 8 +- .../estimation/WeightedMeasurementTest.java | 5 +- 16 files changed, 87 insertions(+), 446 deletions(-) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/EstimatedParameter.java (98%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/EstimationException.java (53%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/EstimationProblem.java (98%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/Estimator.java (98%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/GaussNewtonEstimator.java (79%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/LevenbergMarquardtEstimator.java (99%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/WeightedMeasurement.java (99%) rename src/{mantissa/src/org/spaceroots/mantissa => java/org/apache/commons/math}/estimation/package.html (100%) delete mode 100644 src/mantissa/src/org/spaceroots/mantissa/estimation/LeastSquaresEstimator.java delete mode 100644 src/mantissa/src/org/spaceroots/mantissa/estimation/doc-files/org_spaceroots_mantissa_estimation_classes.png delete mode 100644 src/mantissa/tests-src/org/spaceroots/mantissa/estimation/AllTests.java delete mode 100644 src/mantissa/tests-src/org/spaceroots/mantissa/estimation/GaussNewtonEstimatorTest.java rename src/{mantissa/tests-src/org/spaceroots/mantissa => test/org/apache/commons/math}/estimation/EstimatedParameterTest.java (95%) rename src/{mantissa/tests-src/org/spaceroots/mantissa => test/org/apache/commons/math}/estimation/LevenbergMarquardtEstimatorTest.java (98%) rename src/{mantissa/tests-src/org/spaceroots/mantissa => test/org/apache/commons/math}/estimation/MinpackTest.java (99%) rename src/{mantissa/tests-src/org/spaceroots/mantissa => test/org/apache/commons/math}/estimation/WeightedMeasurementTest.java (95%) diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimatedParameter.java b/src/java/org/apache/commons/math/estimation/EstimatedParameter.java similarity index 98% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/EstimatedParameter.java rename to src/java/org/apache/commons/math/estimation/EstimatedParameter.java index a219d9f7d..93927b35e 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimatedParameter.java +++ b/src/java/org/apache/commons/math/estimation/EstimatedParameter.java @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.io.Serializable; diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationException.java b/src/java/org/apache/commons/math/estimation/EstimationException.java similarity index 53% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationException.java rename to src/java/org/apache/commons/math/estimation/EstimationException.java index cf36df248..68db1eb3e 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationException.java +++ b/src/java/org/apache/commons/math/estimation/EstimationException.java @@ -15,9 +15,9 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; -import org.spaceroots.mantissa.MantissaException; +import org.apache.commons.math.MathException; /** This class represents exceptions thrown by the estimation solvers. @@ -27,33 +27,26 @@ import org.spaceroots.mantissa.MantissaException; */ public class EstimationException - extends MantissaException { +extends MathException { - /** Simple constructor. - * Build an exception by translating the specified message - * @param message message to translate - */ - public EstimationException(String message) { - super(message); - } + /** Serializable version identifier. */ + private static final long serialVersionUID = -7414806622114810487L; - /** Simple constructor. - * Build an exception by translating and formating a message - * @param specifier format specifier (to be translated) - * @param parts to insert in the format (no translation) - */ - public EstimationException(String specifier, String[] parts) { - super(specifier, parts); - } + /** Simple constructor. + * Build an exception by translating and formating a message + * @param specifier format specifier (to be translated) + * @param parts to insert in the format (no translation) + */ + public EstimationException(String specifier, String[] parts) { + super(specifier, parts); + } - /** Simple constructor. - * Build an exception from a cause - * @param cause cause of this exception - */ - public EstimationException(Throwable cause) { - super(cause); - } - - private static final long serialVersionUID = 1613719630569355278L; + /** Simple constructor. + * Build an exception from a cause + * @param cause cause of this exception + */ + public EstimationException(Throwable cause) { + super(cause); + } } diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationProblem.java b/src/java/org/apache/commons/math/estimation/EstimationProblem.java similarity index 98% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationProblem.java rename to src/java/org/apache/commons/math/estimation/EstimationProblem.java index 13cefe6c3..d0efc5a37 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/EstimationProblem.java +++ b/src/java/org/apache/commons/math/estimation/EstimationProblem.java @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; /** This interface represents an estimation problem. diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/Estimator.java b/src/java/org/apache/commons/math/estimation/Estimator.java similarity index 98% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/Estimator.java rename to src/java/org/apache/commons/math/estimation/Estimator.java index 8410f16f6..d6f6023df 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/Estimator.java +++ b/src/java/org/apache/commons/math/estimation/Estimator.java @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; /** This interface represents solvers for estimation problems. diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/GaussNewtonEstimator.java b/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java similarity index 79% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/GaussNewtonEstimator.java rename to src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java index bd53894ea..1ff7faca1 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/GaussNewtonEstimator.java +++ b/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java @@ -15,14 +15,13 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.io.Serializable; -import org.spaceroots.mantissa.linalg.Matrix; -import org.spaceroots.mantissa.linalg.GeneralMatrix; -import org.spaceroots.mantissa.linalg.SymetricalMatrix; -import org.spaceroots.mantissa.linalg.SingularMatrixException; +import org.apache.commons.math.linear.InvalidMatrixException; +import org.apache.commons.math.linear.RealMatrix; +import org.apache.commons.math.linear.RealMatrixImpl; /** This class implements a solver for estimation problems. @@ -67,18 +66,13 @@ public class GaussNewtonEstimator * Jn and Jn-1 are the current and * preceding criterion value (square sum of the weighted residuals * of considered measurements). - * @param epsilon threshold under which the matrix of the linearized - * problem is considered singular (see {@link - * org.spaceroots.mantissa.linalg.SquareMatrix#solve(Matrix,double) - * SquareMatrix.solve}). */ + */ public GaussNewtonEstimator(int maxIterations, double convergence, - double steadyStateThreshold, - double epsilon) { + double steadyStateThreshold) { this.maxIterations = maxIterations; this.steadyStateThreshold = steadyStateThreshold; this.convergence = convergence; - this.epsilon = epsilon; } /** Solve an estimation problem using a least squares criterion. @@ -153,40 +147,52 @@ public class GaussNewtonEstimator WeightedMeasurement[] measurements = problem.getMeasurements(); // build the linear problem - GeneralMatrix b = new GeneralMatrix(parameters.length, 1); - SymetricalMatrix a = new SymetricalMatrix(parameters.length); + RealMatrix b = new RealMatrixImpl(parameters.length, 1); + RealMatrix a = new RealMatrixImpl(parameters.length, parameters.length); + double[] grad = new double[parameters.length]; + RealMatrixImpl bDecrement = new RealMatrixImpl(parameters.length, 1); + double[][] bDecrementData = bDecrement.getDataRef(); + RealMatrixImpl wGradGradT = new RealMatrixImpl(parameters.length, parameters.length); + double[][] wggData = wGradGradT.getDataRef(); for (int i = 0; i < measurements.length; ++i) { - if (! measurements [i].isIgnored()) { - double weight = measurements[i].getWeight(); - double residual = measurements[i].getResidual(); + if (! measurements [i].isIgnored()) { + + double weight = measurements[i].getWeight(); + double residual = measurements[i].getResidual(); + + // compute the normal equation + for (int j = 0; j < parameters.length; ++j) { + grad[j] = measurements[i].getPartial(parameters[j]); + bDecrementData[j][0] = weight * residual * grad[j]; + } + + // build the contribution matrix for measurement i + for (int k = 0; k < parameters.length; ++k) { + double[] wggRow = wggData[k]; + double gk = grad[k]; + for (int l = 0; l < parameters.length; ++l) { + wggRow[l] = weight * gk * grad[l]; + } + } + + // update the matrices + a = a.add(wGradGradT); + b = b.add(bDecrement); - // compute the normal equation - double[] grad = new double[parameters.length]; - Matrix bDecrement = new GeneralMatrix(parameters.length, 1); - for (int j = 0; j < parameters.length; ++j) { - grad[j] = measurements[i].getPartial(parameters[j]); - bDecrement.setElement(j, 0, weight * residual * grad[j]); } - - // update the matrices - a.selfAddWAAt(weight, grad); - b.selfAdd(bDecrement); - - } } try { // solve the linearized least squares problem - Matrix dX = a.solve(b, epsilon); + RealMatrix dX = a.solve(b); // update the estimated parameters for (int i = 0; i < parameters.length; ++i) { - parameters[i].setEstimate(parameters[i].getEstimate() - + dX.getElement(i, 0)); + parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i, 0)); } - } catch(SingularMatrixException e) { + } catch(InvalidMatrixException e) { throw new EstimationException(e); } @@ -223,7 +229,6 @@ public class GaussNewtonEstimator private int maxIterations; private double steadyStateThreshold; private double convergence; - private double epsilon; private static final long serialVersionUID = -7606628156644194170L; diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimator.java b/src/java/org/apache/commons/math/estimation/LevenbergMarquardtEstimator.java similarity index 99% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimator.java rename to src/java/org/apache/commons/math/estimation/LevenbergMarquardtEstimator.java index 6fa6b2dd8..5c031b0ff 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimator.java +++ b/src/java/org/apache/commons/math/estimation/LevenbergMarquardtEstimator.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.io.Serializable; import java.util.Arrays; diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/WeightedMeasurement.java b/src/java/org/apache/commons/math/estimation/WeightedMeasurement.java similarity index 99% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/WeightedMeasurement.java rename to src/java/org/apache/commons/math/estimation/WeightedMeasurement.java index 09ce16663..8cbe8ae13 100644 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/WeightedMeasurement.java +++ b/src/java/org/apache/commons/math/estimation/WeightedMeasurement.java @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.io.Serializable; diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/package.html b/src/java/org/apache/commons/math/estimation/package.html similarity index 100% rename from src/mantissa/src/org/spaceroots/mantissa/estimation/package.html rename to src/java/org/apache/commons/math/estimation/package.html diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/LeastSquaresEstimator.java b/src/mantissa/src/org/spaceroots/mantissa/estimation/LeastSquaresEstimator.java deleted file mode 100644 index 3b3e4de9a..000000000 --- a/src/mantissa/src/org/spaceroots/mantissa/estimation/LeastSquaresEstimator.java +++ /dev/null @@ -1,73 +0,0 @@ -// 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.spaceroots.mantissa.estimation; - -import java.io.Serializable; - -/** This class implements a solver for estimation problems. - * @deprecated this class has been replaced by the {@link - * org.spaceroots.mantissa.estimation.GaussNewtonEstimator GaussNewtonEstimator} - * class. It is now a simple wrapper delegating everything to {@link - * org.spaceroots.mantissa.estimation.GaussNewtonEstimator GaussNewtonEstimator} - * @version $Id: LeastSquaresEstimator.java 1705 2006-09-17 19:57:39Z luc $ - * @author L. Maisonobe - */ -public class LeastSquaresEstimator implements Estimator, Serializable { - - /** Simple constructor. - * @see org.spaceroots.mantissa.estimation.GaussNewtonEstimator#GaussNewtonEstimator(int, - * double, double, double) - */ - public LeastSquaresEstimator(int maxIterations, - double convergence, - double steadyStateThreshold, - double epsilon) { - estimator = new GaussNewtonEstimator(maxIterations, - convergence, - steadyStateThreshold, - epsilon); - } - - /** Solve an estimation problem using a least squares criterion. - * @see org.spaceroots.mantissa.estimation.GaussNewtonEstimator#estimate - */ - public void estimate(EstimationProblem problem) - throws EstimationException { - estimator.estimate(problem); - } - - /** Estimate the solution of a linear least square problem. - * @see org.spaceroots.mantissa.estimation.GaussNewtonEstimator#linearEstimate - */ - public void linearEstimate(EstimationProblem problem) - throws EstimationException { - estimator.linearEstimate(problem); - } - - /** Get the Root Mean Square value. - * @see org.spaceroots.mantissa.estimation.GaussNewtonEstimator#getRMS - */ - public double getRMS(EstimationProblem problem) { - return estimator.getRMS(problem); - } - - private GaussNewtonEstimator estimator; - - private static final long serialVersionUID = -7542643494637247770L; - -} diff --git a/src/mantissa/src/org/spaceroots/mantissa/estimation/doc-files/org_spaceroots_mantissa_estimation_classes.png b/src/mantissa/src/org/spaceroots/mantissa/estimation/doc-files/org_spaceroots_mantissa_estimation_classes.png deleted file mode 100644 index dafb88e09895506e8496449f4801670cafb4062a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3376 zcmaJ^cTm&Y5)REm81+&@5tJe-`jdb(>4Jpb1x#oGlp;}zbc8_A(1N0fsB}UVypSSQ zkY*%^f{1hw3B5>B0)Yg0@p@<8`|Hi@IlKG)an6~Yv)}F}Sz8+Mit&O#AYKz=16vS? z69NKp_;7Qvk?ogNAK8lAThBrd1R|z_S?)Y+pWoWt4z|C)zq-0g)zV@cY;2zjD*ql_ z!L~SpZ7r{YF3%2m{dPRej0`}DJQBui=Wu{AA_xQm^Z#BPU&hfaHh3`D!~%Bk3zy&# zsGLow(vK+X8RtM)R+E*g1>qXv+ zK_+Kr$`(b7*Cdg9oKqsrpUEB&2Nqtd6FO~{89z8x`U_Xt{C!gj-K*?U(H&?Q)B5RM zGV<6-WVVcb3}&4N9{axnBRtpcxbX? z+^DY58uID-amrg;mH`uYAN}747vZLYI%$mW!ag!?9FX7g_z`y3TFvROw=HP%?;|SV zq-bHxX+%^GY)#+aUm3*?29jj69VSugud(G)W!#$ zbPE+j@3PwTkt~;inJ0qu4_#JA9G>Kz%pM@ET|uaszOiuz$A}5V%FaI0$p!Yt76bDD zn!ADGR}qt>E?=0z$zb57cuIq&<0U;IP*;Gd;8(ZfanNf7xAdLRP?9_h)*FUN`DS&v z#U&WI-x&ljp06voHYVX8iEbY8c4GpOVhYv3S?=+X@5(#8q0z^;ABA*r;7A>`E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src/org/spaceroots/mantissa/estimation/AllTests.java b/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/AllTests.java deleted file mode 100644 index 21e90d36d..000000000 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/AllTests.java +++ /dev/null @@ -1,39 +0,0 @@ -// 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.spaceroots.mantissa.estimation; - -import junit.framework.Test; -import junit.framework.TestSuite; - -public class AllTests { - - public static Test suite() { - - TestSuite suite = new TestSuite("org.spaceroots.mantissa.estimation"); - - suite.addTest(EstimatedParameterTest.suite()); - suite.addTest(WeightedMeasurementTest.suite()); - suite.addTest(GaussNewtonEstimatorTest.suite()); - suite.addTest(LevenbergMarquardtEstimatorTest.suite()); - suite.addTest(MinpackTest.suite()); - - return suite; - - } - -} diff --git a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/GaussNewtonEstimatorTest.java b/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/GaussNewtonEstimatorTest.java deleted file mode 100644 index c55dea959..000000000 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/GaussNewtonEstimatorTest.java +++ /dev/null @@ -1,262 +0,0 @@ -// 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.spaceroots.mantissa.estimation; - -import java.util.Random; -import junit.framework.*; - -public class GaussNewtonEstimatorTest - extends TestCase - implements EstimationProblem { - - public GaussNewtonEstimatorTest(String name) { - super(name); - } - - public void testNoMeasurementError() - throws EstimationException { - initRandomizedGrid(2.3); - initProblem(0.0); - GaussNewtonEstimator estimator = - new GaussNewtonEstimator(100, 1.0e-7, 1.0e-10, 1.0e-10); - estimator.estimate(this); - checkGrid(0.01); - } - - public void testSmallMeasurementError() - throws EstimationException { - initRandomizedGrid(2.3); - initProblem(0.02); - GaussNewtonEstimator estimator = - new GaussNewtonEstimator(100, 1.0e-7, 1.0e-10, 1.0e-10); - estimator.estimate(this); - checkGrid(0.1); - } - - public void testNoError() - throws EstimationException { - initRandomizedGrid(0.0); - initProblem(0.0); - GaussNewtonEstimator estimator = - new GaussNewtonEstimator(100, 1.0e-7, 1.0e-10, 1.0e-10); - estimator.estimate(this); - checkGrid(1.0e-10); - } - - public void testUnsolvableProblem() { - - initRandomizedGrid(2.3); - initProblem(0.0); - - // reduce the number of measurements below the limit threshold - int unknowns = unboundPars.length; - WeightedMeasurement[] reducedSet = new WeightedMeasurement[unknowns - 1]; - for (int i = 0; i < reducedSet.length; ++i) { - reducedSet[i] = measurements[i]; - } - measurements = reducedSet; - - boolean gotIt = false; - try { - GaussNewtonEstimator estimator = - new GaussNewtonEstimator(100, 1.0e-7, 1.0e-10, 1.0e-10); - estimator.estimate(this); - } catch(EstimationException e) { - gotIt = true; - } - - assertTrue(gotIt); - - } - - public static Test suite() { - return new TestSuite(GaussNewtonEstimatorTest.class); - } - - public void setUp() { - initPerfectGrid(5); - } - - public void tearDown() { - perfectPars = null; - randomizedPars = null; - unboundPars = null; - measurements = null; - } - - private void initPerfectGrid(int gridSize) { - perfectPars = new EstimatedParameter[gridSize * gridSize * 2]; - - int k = 0; - for (int i = 0; i < gridSize; ++i) { - for (int j = 0; j < gridSize; ++j) { - - String name = Integer.toString(k); - perfectPars[2 * k] = new EstimatedParameter("x" + name, i); - perfectPars[2 * k + 1] = new EstimatedParameter("y" + name, j); - ++k; - } - } - - } - - private void initRandomizedGrid(double initialGuessError) { - Random randomizer = new Random(2353995334l); - randomizedPars = new EstimatedParameter[perfectPars.length]; - - // add an error to every point coordinate - for (int k = 0; k < randomizedPars.length; ++k) { - String name = perfectPars[k].getName(); - double value = perfectPars[k].getEstimate(); - double error = randomizer.nextGaussian() * initialGuessError; - randomizedPars[k] = new EstimatedParameter(name, value + error); - } - - } - - private void initProblem(double measurementError) { - - int pointsNumber = randomizedPars.length / 2; - int measurementsNumber = pointsNumber * (pointsNumber - 1) / 2; - measurements = new WeightedMeasurement[measurementsNumber]; - - Random randomizer = new Random(5785631926l); - - // for the test, we consider that the perfect grid is the reality - // and that the randomized grid is the first (wrong) estimate. - int i = 0; - for (int l = 0; l < (pointsNumber - 1); ++l) { - for (int m = l + 1; m < pointsNumber; ++m) { - // perfect measurements on the real data - double dx = perfectPars[2 * l].getEstimate() - - perfectPars[2 * m].getEstimate(); - double dy = perfectPars[2 * l + 1].getEstimate() - - perfectPars[2 * m + 1].getEstimate(); - double d = Math.sqrt(dx * dx + dy * dy); - - // adding a noise to the measurements - d += randomizer.nextGaussian() * measurementError; - - // add the measurement to the current problem - measurements[i++] = new Distance(1.0, d, - randomizedPars[2 * l], - randomizedPars[2 * l + 1], - randomizedPars[2 * m], - randomizedPars[2 * m + 1]); - - } - } - - // fix three values in the randomized grid and bind them (there - // are two abscissas and one ordinate, so if there were no error - // at all, the estimated grid should be correctly centered on the - // perfect grid) - int oddNumber = 2 * (randomizedPars.length / 4) - 1; - for (int k = 0; k < 2 * oddNumber + 1; k += oddNumber) { - randomizedPars[k].setEstimate(perfectPars[k].getEstimate()); - randomizedPars[k].setBound(true); - } - - // store the unbound parameters in a specific table - unboundPars = new EstimatedParameter[randomizedPars.length - 3]; - for (int src = 0, dst = 0; src < randomizedPars.length; ++src) { - if (! randomizedPars[src].isBound()) { - unboundPars[dst++] = randomizedPars[src]; - } - } - - } - - private void checkGrid(double threshold) { - - double rms = 0; - for (int i = 0; i < perfectPars.length; ++i) { - rms += perfectPars[i].getEstimate() - randomizedPars[i].getEstimate(); - } - rms = Math.sqrt(rms / perfectPars.length); - - assertTrue(rms <= threshold); - - } - - private static class Distance extends WeightedMeasurement { - - public Distance(double weight, double measuredValue, - EstimatedParameter x1, EstimatedParameter y1, - EstimatedParameter x2, EstimatedParameter y2) { - super(weight, measuredValue); - this.x1 = x1; - this.y1 = y1; - this.x2 = x2; - this.y2 = y2; - } - - public double getTheoreticalValue() { - double dx = x2.getEstimate() - x1.getEstimate(); - double dy = y2.getEstimate() - y1.getEstimate(); - return Math.sqrt(dx * dx + dy * dy); - } - - public double getPartial(EstimatedParameter p) { - - // first quick answer for most parameters - if ((p != x1) && (p != y1) && (p != x2) && (p != y2)) { - return 0.0; - } - - // compute the value now as we know we depend on the specified parameter - double distance = getTheoreticalValue(); - - if (p == x1) { - return (x1.getEstimate() - x2.getEstimate()) / distance; - } else if (p == x2) { - return (x2.getEstimate() - x1.getEstimate()) / distance; - } else if (p == y1) { - return (y1.getEstimate() - y2.getEstimate()) / distance; - } else { - return (y2.getEstimate() - y1.getEstimate()) / distance; - } - - } - - private EstimatedParameter x1; - private EstimatedParameter y1; - private EstimatedParameter x2; - private EstimatedParameter y2; - private static final long serialVersionUID = 4090004243280980746L; - - } - - public WeightedMeasurement[] getMeasurements() { - return (WeightedMeasurement[]) measurements.clone(); - } - - public EstimatedParameter[] getUnboundParameters() { - return (EstimatedParameter[]) unboundPars.clone(); - } - - public EstimatedParameter[] getAllParameters() { - return (EstimatedParameter[]) randomizedPars.clone(); - } - - private EstimatedParameter[] perfectPars; - private EstimatedParameter[] randomizedPars; - private EstimatedParameter[] unboundPars; - private WeightedMeasurement[] measurements; - -} diff --git a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/EstimatedParameterTest.java b/src/test/org/apache/commons/math/estimation/EstimatedParameterTest.java similarity index 95% rename from src/mantissa/tests-src/org/spaceroots/mantissa/estimation/EstimatedParameterTest.java rename to src/test/org/apache/commons/math/estimation/EstimatedParameterTest.java index e79e9edba..c5fb019a0 100644 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/EstimatedParameterTest.java +++ b/src/test/org/apache/commons/math/estimation/EstimatedParameterTest.java @@ -15,7 +15,9 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; + +import org.apache.commons.math.estimation.EstimatedParameter; import junit.framework.*; diff --git a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimatorTest.java b/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java similarity index 98% rename from src/mantissa/tests-src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimatorTest.java rename to src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java index 8984fba0e..d745a543b 100644 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimatorTest.java +++ b/src/test/org/apache/commons/math/estimation/LevenbergMarquardtEstimatorTest.java @@ -15,13 +15,19 @@ * limitations under the License. */ -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.util.ArrayList; -import java.util.IdentityHashMap; +import java.util.HashMap; import java.util.Iterator; import java.util.Set; +import org.apache.commons.math.estimation.EstimatedParameter; +import org.apache.commons.math.estimation.EstimationException; +import org.apache.commons.math.estimation.EstimationProblem; +import org.apache.commons.math.estimation.LevenbergMarquardtEstimator; +import org.apache.commons.math.estimation.WeightedMeasurement; + import junit.framework.*; /** @@ -519,7 +525,7 @@ public class LevenbergMarquardtEstimatorTest } public EstimatedParameter[] getAllParameters() { - IdentityHashMap map = new IdentityHashMap(); + HashMap map = new HashMap(); for (int i = 0; i < measurements.length; ++i) { EstimatedParameter[] parameters = measurements[i].getParameters(); for (int j = 0; j < parameters.length; ++j) { diff --git a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/MinpackTest.java b/src/test/org/apache/commons/math/estimation/MinpackTest.java similarity index 99% rename from src/mantissa/tests-src/org/spaceroots/mantissa/estimation/MinpackTest.java rename to src/test/org/apache/commons/math/estimation/MinpackTest.java index 3c453220f..3b39090de 100644 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/MinpackTest.java +++ b/src/test/org/apache/commons/math/estimation/MinpackTest.java @@ -1,7 +1,13 @@ -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; import java.util.Arrays; +import org.apache.commons.math.estimation.EstimatedParameter; +import org.apache.commons.math.estimation.EstimationException; +import org.apache.commons.math.estimation.EstimationProblem; +import org.apache.commons.math.estimation.LevenbergMarquardtEstimator; +import org.apache.commons.math.estimation.WeightedMeasurement; + import junit.framework.*; /** diff --git a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/WeightedMeasurementTest.java b/src/test/org/apache/commons/math/estimation/WeightedMeasurementTest.java similarity index 95% rename from src/mantissa/tests-src/org/spaceroots/mantissa/estimation/WeightedMeasurementTest.java rename to src/test/org/apache/commons/math/estimation/WeightedMeasurementTest.java index f27bd7ea9..57b25fbd3 100644 --- a/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/WeightedMeasurementTest.java +++ b/src/test/org/apache/commons/math/estimation/WeightedMeasurementTest.java @@ -15,7 +15,10 @@ // specific language governing permissions and limitations // under the License. -package org.spaceroots.mantissa.estimation; +package org.apache.commons.math.estimation; + +import org.apache.commons.math.estimation.EstimatedParameter; +import org.apache.commons.math.estimation.WeightedMeasurement; import junit.framework.*;