org.apache.commons.math3.exception.ConvergenceException Java Examples

The following examples show how to use org.apache.commons.math3.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: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    /* long multiplication to avoid overflow (double not used due to efficiency
     * and to avoid precision loss)
     */
    final long n1n2prod = (long) n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    // No try-catch or advertised exception because args are valid
    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example #2
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Ignore@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 #3
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Ignore@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 #4
Source File: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    /* long multiplication to avoid overflow (double not used due to efficiency
     * and to avoid precision loss)
     */
    final long n1n2prod = (long) n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    // No try-catch or advertised exception because args are valid
    // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
    final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example #5
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {

    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       problem.getTarget(),
                       new Weight(new double[] { 1, 1, 1 }),
                       new InitialGuess(new double[] { 0, 0, 0 }));
}
 
Example #6
Source File: PolynomialFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void checkUnsolvableProblem(MultivariateVectorOptimizer optimizer,
                                    boolean solvable) {
    Random randomizer = new Random(1248788532l);
    for (int degree = 0; degree < 10; ++degree) {
        PolynomialFunction p = buildRandomPolynomial(degree, randomizer);

        PolynomialFitter fitter = new PolynomialFitter(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 {
            final double[] init = new double[degree + 1];
            fitter.fit(init);
            Assert.assertTrue(solvable || (degree == 0));
        } catch(ConvergenceException e) {
            Assert.assertTrue((! solvable) && (degree > 0));
        }
    }
}
 
Example #7
Source File: Elixir_0022_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final double n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example #8
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #9
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Ignore@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 #10
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=ConvergenceException.class)
public void testMoreEstimatedParametersSimple() {
    /*
     * Exception is expected with this optimizer
     */
    super.testMoreEstimatedParametersSimple();
}
 
Example #11
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDigitLimitConstructor() throws ConvergenceException {
    assertFraction(2, 5, new BigFraction(0.4, 9));
    assertFraction(2, 5, new BigFraction(0.4, 99));
    assertFraction(2, 5, new BigFraction(0.4, 999));

    assertFraction(3, 5, new BigFraction(0.6152, 9));
    assertFraction(8, 13, new BigFraction(0.6152, 99));
    assertFraction(510, 829, new BigFraction(0.6152, 999));
    assertFraction(769, 1250, new BigFraction(0.6152, 9999));
}
 
Example #12
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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 #13
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.OneWayAnova#anovaTest(Collection,double)
 *
 * @since 1.2
 */
public static boolean oneWayAnovaTest(final Collection<double[]> categoryData,
                                      final double alpha)
    throws NullArgumentException, DimensionMismatchException,
    OutOfRangeException, ConvergenceException, MaxCountExceededException {
    return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha);
}
 
Example #14
Source File: FractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEpsilonLimitConstructor() throws ConvergenceException  {
    assertFraction(2, 5, new Fraction(0.4, 1.0e-5, 100));

    assertFraction(3, 5,      new Fraction(0.6152, 0.02, 100));
    assertFraction(8, 13,     new Fraction(0.6152, 1.0e-3, 100));
    assertFraction(251, 408,  new Fraction(0.6152, 1.0e-4, 100));
    assertFraction(251, 408,  new Fraction(0.6152, 1.0e-5, 100));
    assertFraction(510, 829,  new Fraction(0.6152, 1.0e-6, 100));
    assertFraction(769, 1250, new Fraction(0.6152, 1.0e-7, 100));
}
 
Example #15
Source File: FractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEpsilonLimitConstructor() throws ConvergenceException  {
    assertFraction(2, 5, new Fraction(0.4, 1.0e-5, 100));

    assertFraction(3, 5,      new Fraction(0.6152, 0.02, 100));
    assertFraction(8, 13,     new Fraction(0.6152, 1.0e-3, 100));
    assertFraction(251, 408,  new Fraction(0.6152, 1.0e-4, 100));
    assertFraction(251, 408,  new Fraction(0.6152, 1.0e-5, 100));
    assertFraction(510, 829,  new Fraction(0.6152, 1.0e-6, 100));
    assertFraction(769, 1250, new Fraction(0.6152, 1.0e-7, 100));
}
 
Example #16
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEpsilonLimitConstructor() throws ConvergenceException {
    assertFraction(2, 5, new BigFraction(0.4, 1.0e-5, 100));

    assertFraction(3, 5, new BigFraction(0.6152, 0.02, 100));
    assertFraction(8, 13, new BigFraction(0.6152, 1.0e-3, 100));
    assertFraction(251, 408, new BigFraction(0.6152, 1.0e-4, 100));
    assertFraction(251, 408, new BigFraction(0.6152, 1.0e-5, 100));
    assertFraction(510, 829, new BigFraction(0.6152, 1.0e-6, 100));
    assertFraction(769, 1250, new BigFraction(0.6152, 1.0e-7, 100));
}
 
Example #17
Source File: GaussNewtonOptimizerWithCholeskyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testMoreEstimatedParametersSimple() {
    /*
     * Exception is expected with this optimizer
     */
    try {
        super.testMoreEstimatedParametersSimple();
        fail(optimizer);
    } catch (ConvergenceException e) {
        //expected
    }
}
 
Example #18
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #19
Source File: GaussNewtonOptimizerWithLUTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testMoreEstimatedParametersSimple() {
    /*
     * Exception is expected with this optimizer
     */
    try {
        super.testMoreEstimatedParametersSimple();
        fail(optimizer);
    } catch (ConvergenceException e) {
        //expected
    }
}
 
Example #20
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDigitLimitConstructor() throws ConvergenceException {
    assertFraction(2, 5, new BigFraction(0.4, 9));
    assertFraction(2, 5, new BigFraction(0.4, 99));
    assertFraction(2, 5, new BigFraction(0.4, 999));

    assertFraction(3, 5, new BigFraction(0.6152, 9));
    assertFraction(8, 13, new BigFraction(0.6152, 99));
    assertFraction(510, 829, new BigFraction(0.6152, 999));
    assertFraction(769, 1250, new BigFraction(0.6152, 9999));
}
 
Example #21
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected = ConvergenceException.class)
public void testHahn1()
    throws IOException {
    /*
     * TODO This test leads to a singular problem with the Gauss-Newton
     * optimizer. This should be inquired.
     */
    super.testHahn1();
}
 
Example #22
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = ConvergenceException.class)
public void testConvergenceException() {
    // ConvergenceException thrown if fit terminates before threshold met
    double[][] data = getTestSamples();
    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution
        initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);

    // 5 iterations not enough to meet convergence threshold
    fitter.fit(initialMix, 5, 1E-5);
}
 
Example #23
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=ConvergenceException.class)
public void testCircleFittingBadInit() {
    /*
     * This test does not converge with this optimizer.
     */
    super.testCircleFittingBadInit();
}
 
Example #24
Source File: RegulaFalsiSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=ConvergenceException.class)
public void testIssue631() {
    final UnivariateFunction f = new UnivariateFunction() {
            /** {@inheritDoc} */
            public double value(double x) {
                return FastMath.exp(x) - FastMath.pow(Math.PI, 3.0);
            }
        };

    final UnivariateSolver solver = new RegulaFalsiSolver();
    final double root = solver.solve(3624, f, 1, 10);
    Assert.assertEquals(3.4341896575482003, root, 1e-15);
}
 
Example #25
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected = ConvergenceException.class)
public void testMoreEstimatedParametersSimple() {
    /*
     * Exception is expected with this optimizer
     */
    super.testMoreEstimatedParametersSimple();
}
 
Example #26
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=ConvergenceException.class)
public void testCircleFittingBadInit() {
    /*
     * This test does not converge with this optimizer.
     */
    super.testCircleFittingBadInit();
}
 
Example #27
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the point farthest to its cluster center
 *
 * @param clusters the {@link Cluster}s to search
 * @return point farthest to its cluster center
 * @throws ConvergenceException if clusters are all empty
 */
private T getFarthestPoint(final Collection<Cluster<T>> clusters) throws ConvergenceException {

    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 #28
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=ConvergenceException.class)
public void testMoreEstimatedParametersUnsorted() {
    /*
     * Exception is expected with this optimizer
     */
    super.testMoreEstimatedParametersUnsorted();
}
 
Example #29
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.OneWayAnova#anovaTest(Collection,double)
 *
 * @since 1.2
 */
public static boolean oneWayAnovaTest(final Collection<double[]> categoryData,
                                      final double alpha)
    throws NullArgumentException, DimensionMismatchException,
    OutOfRangeException, ConvergenceException, MaxCountExceededException {
    return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha);
}
 
Example #30
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final CentroidCluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final Clusterable center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, 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()));

}