|
Java example source code file (PolynomialFitterTest.java)
The PolynomialFitterTest.java Java example source code/* * 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.apache.commons.math3.fitting; import java.util.Random; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction.Parametric; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; import org.apache.commons.math3.optim.SimpleVectorValueChecker; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.TestUtils; import org.junit.Test; import org.junit.Assert; /** * Test for class {@link CurveFitter} where the function to fit is a * polynomial. */ @Deprecated public class PolynomialFitterTest { @Test public void testFit() { final RealDistribution rng = new UniformRealDistribution(-100, 100); rng.reseedRandomGenerator(64925784252L); final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer(); final PolynomialFitter fitter = new PolynomialFitter(optim); final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 final PolynomialFunction f = new PolynomialFunction(coeff); // Collect data from a known polynomial. for (int i = 0; i < 100; i++) { final double x = rng.sample(); fitter.addObservedPoint(x, f.value(x)); } // Start fit from initial guesses that are far from the optimal values. final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 }); TestUtils.assertEquals("best != coeff", coeff, best, 1e-12); } @Test public void testNoError() { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); Assert.assertEquals(0.0, error, 1.0e-6); } } } @Test public void testSmallError() { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.1); } } Assert.assertTrue(maxError > 0.01); } @Test public void testMath798() { final double tol = 1e-14; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 3; final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], tol); } } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * But in the test case, the real problem is that the tolerance is way too * stringent. */ @Test(expected=TooManyEvaluationsException.class) public void testMath798WithToleranceTooLow() { final double tol = 1e-100; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. @SuppressWarnings("unused") final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * Even if the real problem is that the tolerance is way too stringent, it * is possible to get the best solution so far, i.e. a checker will return * the point when the maximum iteration count has been reached. */ @Test public void testMath798WithToleranceTooLowButNoException() { final double tol = 1e-100; final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol, maxEval); final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], 1e-15); } } /** * @param optimizer Optimizer. * @param maxEval Maximum number of function evaluations. * @param init First guess. * @return the solution found by the given optimizer. */ private double[] doMath798(MultivariateVectorOptimizer optimizer, int maxEval, double[] init) { final CurveFitter<Parametric> fitter = new CurveFitter Other Java examples (source code examples)Here is a short list of links related to this Java PolynomialFitterTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.