org.apache.commons.math.exception.ConvergenceException Java Examples
The following examples show how to use
org.apache.commons.math.exception.ConvergenceException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testTrivial() throws FunctionEvaluationException { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); VectorialPointValuePair optimum = optimizer.optimize(problem, problem.target, new double[] { 1 }, new double[] { 0 }); assertEquals(0, optimizer.getRMS(), 1.0e-10); try { optimizer.guessParametersErrors(); fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } catch (Exception e) { fail("wrong exception caught"); } assertEquals(1.5, optimum.getPoint()[0], 1.0e-10); assertEquals(3.0, optimum.getValue()[0], 1.0e-10); }
Example #2
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMoreEstimatedParametersSimple() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); Assert.fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #3
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNonInversible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); Assert.fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #4
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMoreEstimatedParametersUnsorted() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, new double[] { 2, 2, 2, 2, 2, 2 }); Assert.fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #5
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMath199() { try { QuadraticProblem problem = new QuadraticProblem(); problem.addPoint (0, -3.182591015485607); problem.addPoint (1, -2.5581184967730577); problem.addPoint (2, -2.1488478161387325); problem.addPoint (3, -1.9122489313410047); problem.addPoint (4, 1.7785661310051026); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(100, 1e-10, 1e-10, 1e-10, 0); optimizer.optimize(100, problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 0.0, 4.4e-323, 1.0, 4.4e-323, 0.0 }, new double[] { 0, 0, 0 }); Assert.fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } }
Example #6
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testNonInversible() throws FunctionEvaluationException { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6); try { optimizer.getCovariances(); fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } catch (Exception e) { fail("wrong exception caught"); } }
Example #7
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testNonInversible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #8
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersSimple() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #9
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersUnsorted() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, new double[] { 2, 2, 2, 2, 2, 2 }); fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #10
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMath199() { try { QuadraticProblem problem = new QuadraticProblem(); problem.addPoint (0, -3.182591015485607); problem.addPoint (1, -2.5581184967730577); problem.addPoint (2, -2.1488478161387325); problem.addPoint (3, -1.9122489313410047); problem.addPoint (4, 1.7785661310051026); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(100, 1e-10, 1e-10, 1e-10, 0); optimizer.optimize(100, problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 0.0, 4.4e-323, 1.0, 4.4e-323, 0.0 }, new double[] { 0, 0, 0 }); fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } }
Example #11
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
private void checkUnsolvableProblem(DifferentiableMultivariateVectorialOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { fitter.fit(); Assert.assertTrue(solvable || (degree == 0)); } catch(ConvergenceException e) { Assert.assertTrue((! solvable) && (degree > 0)); } } }
Example #12
Source File: LevenbergMarquardtOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMath199() throws FunctionEvaluationException { try { QuadraticProblem problem = new QuadraticProblem(); problem.addPoint (0, -3.182591015485607); problem.addPoint (1, -2.5581184967730577); problem.addPoint (2, -2.1488478161387325); problem.addPoint (3, -1.9122489313410047); problem.addPoint (4, 1.7785661310051026); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); optimizer.setQRRankingThreshold(0); optimizer.optimize(problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 0.0, 4.4e-323, 1.0, 4.4e-323, 0.0 }, new double[] { 0, 0, 0 }); fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } }
Example #13
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxEvaluations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, new double[] { 2, 2, 2, 2, 2, 2 }); fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } catch (Exception e) { fail("wrong exception type caught"); } }
Example #14
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setMaxEvaluations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } catch (Exception e) { fail("wrong exception type caught"); } }
Example #15
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testMoreEstimatedParametersSimple() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); Assert.fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #16
Source File: GaussNewtonOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNonInversible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(true); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); try { optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); Assert.fail("an exception should have been caught"); } catch (ConvergenceException ee) { // expected behavior } }
Example #17
Source File: MultiStartDifferentiableMultivariateVectorialOptimizerTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test(expected = ConvergenceException.class) public void testNoOptimum() throws FunctionEvaluationException, OptimizationException { DifferentiableMultivariateVectorialOptimizer underlyingOptimizer = new GaussNewtonOptimizer(true); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(12373523445l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultiStartDifferentiableMultivariateVectorialOptimizer optimizer = new MultiStartDifferentiableMultivariateVectorialOptimizer(underlyingOptimizer, 10, generator); optimizer.setMaxEvaluations(100); optimizer.setConvergenceChecker(new SimpleVectorialValueChecker(1.0e-6, 1.0e-6)); optimizer.optimize(new DifferentiableMultivariateVectorialFunction() { public MultivariateMatrixFunction jacobian() { return null; } public double[] value(double[] point) throws FunctionEvaluationException { throw new FunctionEvaluationException(point[0]); } }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 }); }
Example #18
Source File: PolynomialFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
private void checkUnsolvableProblem(DifferentiableMultivariateVectorialOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(degree, optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { fitter.fit(); assertTrue(solvable || (degree == 0)); } catch(ConvergenceException e) { assertTrue((! solvable) && (degree > 0)); } } }
Example #19
Source File: Cardumen_00265_s.java From coming with MIT License | 5 votes |
/** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center */ private T getFarthestPoint(final Collection<Cluster<T>> clusters) { double maxDistance = Double.NEGATIVE_INFINITY; Cluster<T> selectedCluster = null; int selectedPoint = -1; for (final Cluster<T> cluster : clusters) { // get the farthest point final T center = cluster.getCenter(); final List<T> points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = points.get(i).distanceFrom(center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); }
Example #20
Source File: Cardumen_00119_t.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public UnivariateRealPointValuePair optimize(final FUNC f, final GoalType goal, final double min, final double max, final double startValue) throws FunctionEvaluationException { optima = new UnivariateRealPointValuePair[starts]; totalEvaluations = 0; // Multi-start loop. for (int i = 0; i < starts; ++i) { try { final double bound1 = (i == 0) ? min : min + generator.nextDouble() * (max - min); final double bound2 = (i == 0) ? max : min + generator.nextDouble() * (max - min); optima[i] = optimizer.optimize(f, goal, org.apache.commons.math.util.FastMath.max(min, min), org.apache.commons.math.util.FastMath.max(bound1, bound2)); } catch (FunctionEvaluationException fee) { optima[i] = null; } catch (ConvergenceException ce) { optima[i] = null; } final int usedEvaluations = optimizer.getEvaluations(); optimizer.setMaxEvaluations(optimizer.getMaxEvaluations() - usedEvaluations); totalEvaluations += usedEvaluations; } sortPairs(goal); if (optima[0] == null) { throw new ConvergenceException(LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT, starts); } // Return the point with the best objective function value. return optima[0]; }
Example #21
Source File: Cardumen_00119_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public UnivariateRealPointValuePair optimize(final FUNC f, final GoalType goal, final double min, final double max, final double startValue) throws FunctionEvaluationException { optima = new UnivariateRealPointValuePair[starts]; totalEvaluations = 0; // Multi-start loop. for (int i = 0; i < starts; ++i) { try { final double bound1 = (i == 0) ? min : min + generator.nextDouble() * (max - min); final double bound2 = (i == 0) ? max : min + generator.nextDouble() * (max - min); optima[i] = optimizer.optimize(f, goal, FastMath.min(bound1, bound2), FastMath.max(bound1, bound2)); } catch (FunctionEvaluationException fee) { optima[i] = null; } catch (ConvergenceException ce) { optima[i] = null; } final int usedEvaluations = optimizer.getEvaluations(); optimizer.setMaxEvaluations(optimizer.getMaxEvaluations() - usedEvaluations); totalEvaluations += usedEvaluations; } sortPairs(goal); if (optima[0] == null) { throw new ConvergenceException(LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT, starts); } // Return the point with the best objective function value. return optima[0]; }
Example #22
Source File: Elixir_0028_t.java From coming with MIT License | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters) { double maxVariance = Double.NEGATIVE_INFINITY; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #23
Source File: Cardumen_00265_s.java From coming with MIT License | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestNumberCluster(final Collection<Cluster<T>> clusters) { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #24
Source File: Elixir_0028_s.java From coming with MIT License | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters) { double maxVariance = Double.NEGATIVE_INFINITY; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #25
Source File: Elixir_0028_s.java From coming with MIT License | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestNumberCluster(final Collection<Cluster<T>> clusters) { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #26
Source File: KMeansPlusPlusClusterer.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center */ private T getFarthestPoint(final Collection<Cluster<T>> clusters) { double maxDistance = Double.NEGATIVE_INFINITY; Cluster<T> selectedCluster = null; int selectedPoint = -1; for (final Cluster<T> cluster : clusters) { // get the farthest point final T center = cluster.getCenter(); final List<T> points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = points.get(i).distanceFrom(center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); }
Example #27
Source File: KMeansPlusPlusClusterer.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestNumberCluster(final Collection<Cluster<T>> clusters) { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #28
Source File: AbstractLeastSquaresOptimizer.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Guess the errors in optimized parameters. * <p>Guessing is covariance-based, it only gives rough order of magnitude.</p> * @return errors in optimized parameters * @exception FunctionEvaluationException if the function jacobian cannot b evaluated * @exception ConvergenceException if the covariances matrix cannot be computed * or the number of degrees of freedom is not positive (number of measurements * lesser or equal to number of parameters) */ public double[] guessParametersErrors() throws FunctionEvaluationException { if (rows <= cols) { throw new ConvergenceException(LocalizedFormats.NO_DEGREES_OF_FREEDOM, rows, cols); } double[] errors = new double[cols]; final double c = FastMath.sqrt(getChiSquare() / (rows - cols)); double[][] covar = getCovariances(); for (int i = 0; i < errors.length; ++i) { errors[i] = FastMath.sqrt(covar[i][i]) * c; } return errors; }
Example #29
Source File: KMeansPlusPlusClusterer.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster */ private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters) { double maxVariance = Double.NEGATIVE_INFINITY; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
Example #30
Source File: AbstractLeastSquaresOptimizer.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get the covariance matrix of optimized parameters. * @return covariance matrix * @exception FunctionEvaluationException if the function jacobian cannot * be evaluated * @exception ConvergenceException if the covariance matrix * cannot be computed (singular problem) */ public double[][] getCovariances() throws FunctionEvaluationException { // set up the jacobian updateJacobian(); // compute transpose(J).J, avoiding building big intermediate matrices double[][] jTj = new double[cols][cols]; for (int i = 0; i < cols; ++i) { for (int j = i; j < cols; ++j) { double sum = 0; for (int k = 0; k < rows; ++k) { sum += weightedResidualJacobian[k][i] * weightedResidualJacobian[k][j]; } jTj[i][j] = sum; jTj[j][i] = sum; } } try { // compute the covariances matrix RealMatrix inverse = new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse(); return inverse.getData(); } catch (InvalidMatrixException ime) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM); } }