Java Code Examples for org.apache.commons.math.MathException

The following examples show how to use org.apache.commons.math.MathException. These examples are extracted from open source projects. 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
/**
 * Test of interpolator for the sine function.
 * <p>
 * |sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI]
 */
public void testSinFunction() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealInterpolator interpolator = new DividedDifferenceInterpolator();
    double x[], y[], z, expected, result, tolerance;

    // 6 interpolating points on interval [0, 2*PI]
    int n = 6;
    double min = 0.0, max = 2 * Math.PI;
    x = new double[n];
    y = new double[n];
    for (int i = 0; i < n; i++) {
        x[i] = min + i * (max - min) / n;
        y[i] = f.value(x[i]);
    }
    double derivativebound = 1.0;
    UnivariateRealFunction p = interpolator.interpolate(x, y);

    z = Math.PI / 4; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = Math.PI * 1.5; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);
}
 
Example 2
Source Project: astor   Source File: RiddersSolverTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of solver for the exponential function.
 */
public void testExpm1Function() throws MathException {
    UnivariateRealFunction f = new Expm1Function();
    UnivariateRealSolver solver = new RiddersSolver();
    double min, max, expected, result, tolerance;

    min = -1.0; max = 2.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -20.0; max = 10.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -50.0; max = 100.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);
}
 
Example 3
/**
 * For this distribution, X, this method returns the critical point x, such
 * that P(X &lt; x) = <code>p</code>.
 * <p>
 * Returns 0 for p=0 and <code>Double.POSITIVE_INFINITY</code> for p=1.</p>
 *
 * @param p the desired probability
 * @return x, such that P(X &lt; x) = <code>p</code>
 * @throws MathException if the inverse cumulative probability can not be
 *            computed due to convergence or other numerical errors.
 * @throws IllegalArgumentException if p < 0 or p > 1.
 */
@Override
public double inverseCumulativeProbability(double p) throws MathException {
    double ret;

    if (p < 0.0 || p > 1.0) {
        throw MathRuntimeException.createIllegalArgumentException(
              "{0} out of [{1}, {2}] range", p, 0.0, 1.0);
    } else if (p == 1.0) {
        ret = Double.POSITIVE_INFINITY;
    } else {
        ret = -getMean() * Math.log(1.0 - p);
    }

    return ret;
}
 
Example 4
/**
 * For this distribution, X, this method returns P(X &le; x).
 * @param x the value at which the PDF is evaluated.
 * @return PDF for this distribution. 
 * @throws MathException if the cumulative probability can not be
 *            computed due to convergence or other numerical errors.
 */
@Override
public double cumulativeProbability(int x) throws MathException {
    double ret;
    if (x < 0) {
        ret = 0.0;
    } else if (x >= getNumberOfTrials()) {
        ret = 1.0;
    } else {
        ret =
            1.0 - Beta.regularizedBeta(
                    getProbabilityOfSuccess(),
                    x + 1.0,
                    getNumberOfTrials() - x);
    }
    return ret;
}
 
Example 5
/**
 * Test of polynomial for the linear function.
 */
public void testLinearFunction() throws MathException {
    PolynomialFunctionNewtonForm p;
    double coefficients[], z, expected, result, tolerance = 1E-12;

    // p(x) = 1.5x - 4 = 2 + 1.5(x-4)
    double a[] = { 2.0, 1.5 };
    double c[] = { 4.0 };
    p = new PolynomialFunctionNewtonForm(a, c);

    z = 2.0; expected = -1.0; result = p.value(z);
    assertEquals(expected, result, tolerance);

    z = 4.5; expected = 2.75; result = p.value(z);
    assertEquals(expected, result, tolerance);

    z = 6.0; expected = 5.0; result = p.value(z);
    assertEquals(expected, result, tolerance);

    assertEquals(1, p.degree());

    coefficients = p.getCoefficients();
    assertEquals(2, coefficients.length);
    assertEquals(-4.0, coefficients[0], tolerance);
    assertEquals(1.5, coefficients[1], tolerance);
}
 
Example 6
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 * @throws MathException if an error occurs computing the p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) throws MathException {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

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

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 7
Source Project: astor   Source File: RiddersSolverTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the deprecated APIs.
 */
@Deprecated
public void testDeprecated() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealSolver solver = new RiddersSolver(f);
    double min, max, expected, result, tolerance;

    min = 3.0; max = 4.0; expected = Math.PI;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(min, max);
    assertEquals(expected, result, tolerance);

    min = -1.0; max = 1.5; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(min, max);
    assertEquals(expected, result, tolerance);
}
 
Example 8
Source Project: astor   Source File: SimpsonIntegratorTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of integrator for the quintic function.
 */
public void testQuinticFunction() throws MathException {
    UnivariateRealFunction f = new QuinticFunction();
    UnivariateRealIntegrator integrator = new SimpsonIntegrator();
    double min, max, expected, result, tolerance;

    min = 0; max = 1; expected = -1.0/48;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);

    min = 0; max = 0.5; expected = 11.0/768;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);
}
 
Example 9
Source Project: astor   Source File: LoessInterpolator.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute a loess fit on the data at the original abscissae.
 *
 * @param xval the arguments for the interpolation points
 * @param yval the values for the interpolation points
 * @return values of the loess fit at corresponding original abscissae
 * @throws MathException if some of the following conditions are false:
 * <ul>
 * <li> Arguments and values are of the same size that is greater than zero</li>
 * <li> The arguments are in a strictly increasing order</li>
 * <li> All arguments and values are finite real numbers</li>
 * </ul>
 */
public final double[] smooth(final double[] xval, final double[] yval)
        throws MathException {
    if (xval.length != yval.length) {
        throw new MathException(
                "Loess expects the abscissa and ordinate arrays " +
                "to be of the same size, " +
                "but got {0} abscissae and {1} ordinatae",
                xval.length, yval.length);
    }

    final double[] unitWeights = new double[xval.length];
    Arrays.fill(unitWeights, 1.0);

    return smooth(xval, yval, unitWeights);
}
 
Example 10
Source Project: astor   Source File: ListUnivariateImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public double getElement(int index) {

    double value = Double.NaN;

    int calcIndex = index;

    if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
        windowSize < list.size())
    {
        calcIndex = (list.size() - windowSize) + index;
    }

    
    try {
        value = transformer.transform(list.get(calcIndex));
    } catch (MathException e) {
        e.printStackTrace();
    }
    
    return value;
}
 
Example 11
Source Project: astor   Source File: PolynomialFunctionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * tests the firstDerivative function by comparison
 *
 * <p>This will test the functions
 * <tt>f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6</tt>
 * and <tt>h(x) = 6x - 4</tt>
 */
public void testMath341() throws MathException {
    double[] f_coeff = { 3.0, 6.0, -2.0, 1.0 };
    double[] g_coeff = { 6.0, -4.0, 3.0 };
    double[] h_coeff = { -4.0, 6.0 };

    PolynomialFunction f = new PolynomialFunction( f_coeff );
    PolynomialFunction g = new PolynomialFunction( g_coeff );
    PolynomialFunction h = new PolynomialFunction( h_coeff );

    // compare f' = g
    assertEquals( f.derivative().value(0.0), g.value(0.0), tolerance );
    assertEquals( f.derivative().value(1.0), g.value(1.0), tolerance );
    assertEquals( f.derivative().value(100.0), g.value(100.0), tolerance );
    assertEquals( f.derivative().value(4.1), g.value(4.1), tolerance );
    assertEquals( f.derivative().value(-3.25), g.value(-3.25), tolerance );

    // compare g' = h
    assertEquals( g.derivative().value(Math.PI), h.value(Math.PI), tolerance );
    assertEquals( g.derivative().value(Math.E),  h.value(Math.E),  tolerance );
}
 
Example 12
/**
 * Test of polynomial for the linear function.
 */
public void testLinearFunction() throws MathException {
    PolynomialFunctionNewtonForm p;
    double coefficients[], z, expected, result, tolerance = 1E-12;

    // p(x) = 1.5x - 4 = 2 + 1.5(x-4)
    double a[] = { 2.0, 1.5 };
    double c[] = { 4.0 };
    p = new PolynomialFunctionNewtonForm(a, c);

    z = 2.0; expected = -1.0; result = p.value(z);
    assertEquals(expected, result, tolerance);

    z = 4.5; expected = 2.75; result = p.value(z);
    assertEquals(expected, result, tolerance);

    z = 6.0; expected = 5.0; result = p.value(z);
    assertEquals(expected, result, tolerance);

    assertEquals(1, p.degree());

    coefficients = p.getCoefficients();
    assertEquals(2, coefficients.length);
    assertEquals(-4.0, coefficients[0], tolerance);
    assertEquals(1.5, coefficients[1], tolerance);
}
 
Example 13
Source Project: astor   Source File: BrentMinimizerTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQuinticMax() throws MathException {
    // The quintic function has zeros at 0, +-0.5 and +-1.
    // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
    UnivariateRealFunction f = new QuinticFunction();
    UnivariateRealOptimizer minimizer = new BrentOptimizer();
    assertEquals(0.27195613, minimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3), 1.0e-8);
    minimizer.setMaximalIterationCount(30);
    try {
        minimizer.optimize(f, GoalType.MAXIMIZE, 0.2, 0.3);
        fail("an exception should have been thrown");
    } catch (MaxIterationsExceededException miee) {
        // expected
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example 14
Source Project: astor   Source File: MullerSolverTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of solver for the exponential function.
 * <p>
 * It takes 10 to 15 iterations for the last two tests to converge.
 * In fact, if not for the bisection alternative, the solver would
 * exceed the default maximal iteration of 100.
 */
public void testExpm1Function() throws MathException {
    UnivariateRealFunction f = new Expm1Function();
    UnivariateRealSolver solver = new MullerSolver();
    double min, max, expected, result, tolerance;

    min = -1.0; max = 2.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -20.0; max = 10.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -50.0; max = 100.0; expected = 0.0;
    tolerance = Math.max(solver.getAbsoluteAccuracy(),
                Math.abs(expected * solver.getRelativeAccuracy()));
    result = solver.solve(f, min, max);
    assertEquals(expected, result, tolerance);
}
 
Example 15
Source Project: astor   Source File: LoessInterpolatorTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath296withWeights() throws MathException {
    double[] xval = {
            0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
             1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0};
    double[] yval = {
            0.47, 0.48, 0.55, 0.56, -0.08, -0.04, -0.07, -0.07,
            -0.56, -0.46, -0.56, -0.52, -3.03, -3.08, -3.09,
            -3.04, 3.54, 3.46, 3.36, 3.35};
    double[] weights = {
            1,1,1,1,1,1,1,1,1,1,
            1,1,0,0,1,1,0,0,1,1};
    // Output from R, rounded to .001
    double[] yref = {
            0.478, 0.492, 0.484, 0.320, 0.179, -0.003, -0.088, -0.209,
            -0.327, -0.455, -0.518, -0.537, -1.492, -2.115, -3.09, -3.04,
            -3.0, 0.155, 1.752, 3.35};
    LoessInterpolator li = new LoessInterpolator(0.3, 4, 1e-12);
    double[] res = li.smooth(xval, yval,weights);
    Assert.assertEquals(xval.length, res.length);
    for(int i = 0; i < res.length; ++i) {
        Assert.assertEquals(yref[i], res[i], 0.05);
    }
}
 
Example 16
Source Project: astor   Source File: BeanListUnivariateImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param propertyName Name of Property
 */
public void setPropertyName(String propertyName) {
	this.propertyName = propertyName;
	this.transformer = new NumberTransformer() {

		/**
		 * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
		 */
		public double transform(final Object o) throws MathException {
			try {
				return (
					(Number) PropertyUtils.getProperty(
						o,
						getPropertyName()))
					.doubleValue();
			} catch (IllegalAccessException e) {
				throw new MathException(
					"IllegalAccessException in Transformation: "
						+ e.getMessage(),
					e);
			} catch (InvocationTargetException e) {
				throw new MathException(
					"InvocationTargetException in Transformation: "
						+ e.getMessage(),
					e);
			} catch (NoSuchMethodException e) {
				throw new MathException(
					"oSuchMethodException in Transformation: "
						+ e.getMessage(),
					e);
			}
		}
	};
}
 
Example 17
/** test A = LLT */
@Test
public void testAEqualLLT() throws MathException {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    CholeskyDecomposition llt = new CholeskyDecompositionImpl(matrix);
    RealMatrix l  = llt.getL();
    RealMatrix lt = llt.getLT();
    double norm = l.multiply(lt).subtract(matrix).getNorm();
    assertEquals(0, norm, 1.0e-15);
}
 
Example 18
Source Project: astor   Source File: ChiSquareTestImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param counts array representation of 2-way table
 * @param alpha significance level of the test
 * @return true iff null hypothesis can be rejected with confidence
 * 1 - alpha
 * @throws IllegalArgumentException if preconditions are not met
 * @throws MathException if an error occurs performing the test
 */
public boolean chiSquareTest(long[][] counts, double alpha)
throws IllegalArgumentException, MathException {
    if ((alpha <= 0) || (alpha > 0.5)) {
        throw MathRuntimeException.createIllegalArgumentException(
              "out of bounds significance level {0}, must be between {1} and {2}",
              alpha, 0.0, 0.5);
    }
    return chiSquareTest(counts) < alpha;
}
 
Example 19
Source Project: astor   Source File: LoessInterpolatorTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testOnOnePoint() throws MathException {
    double[] xval = {0.5};
    double[] yval = {0.7};
    double[] res = new LoessInterpolator().smooth(xval, yval);
    Assert.assertEquals(1, res.length);
    Assert.assertEquals(0.7, res[0], 0.0);
}
 
Example 20
Source Project: astor   Source File: GammaTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void testRegularizedGamma(double expected, double a, double x) {
    try {
        double actualP = Gamma.regularizedGammaP(a, x);
        double actualQ = Gamma.regularizedGammaQ(a, x);
        TestUtils.assertEquals(expected, actualP, 10e-15);
        TestUtils.assertEquals(actualP, 1.0 - actualQ, 10e-15);
    } catch(MathException ex){
        fail(ex.getMessage());
    }
}
 
Example 21
/**
 * Test of interpolator for the exponential function.
 * <p>
 * |expm1^(n)(zeta)| <= e, zeta in [-1, 1]
 */
public void testExpm1Function() throws MathException {
    UnivariateRealFunction f = new Expm1Function();
    UnivariateRealInterpolator interpolator = new DividedDifferenceInterpolator();
    double x[], y[], z, expected, result, tolerance;

    // 5 interpolating points on interval [-1, 1]
    int n = 5;
    double min = -1.0, max = 1.0;
    x = new double[n];
    y = new double[n];
    for (int i = 0; i < n; i++) {
        x[i] = min + i * (max - min) / n;
        y[i] = f.value(x[i]);
    }
    double derivativebound = Math.E;
    UnivariateRealFunction p = interpolator.interpolate(x, y);

    z = 0.0; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = 0.5; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = -0.5; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);
}
 
Example 22
Source Project: astor   Source File: NormalDistributionImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * For this distribution, X, this method returns P(X &lt; <code>x</code>).
 * @param x the value at which the CDF is evaluated.
 * @return CDF evaluted at <code>x</code>.
 * @throws MathException if the algorithm fails to converge; unless
 * x is more than 20 standard deviations from the mean, in which case the
 * convergence exception is caught and 0 or 1 is returned.
 */
public double cumulativeProbability(double x) throws MathException {
    try {
        return 0.5 * (1.0 + Erf.erf((x - mean) /
                (standardDeviation * Math.sqrt(2.0))));
    } catch (MaxIterationsExceededException ex) {
        if (x < (mean - 20 * standardDeviation)) { // JDK 1.5 blows at 38
            return 0.0d;
        } else if (x > (mean + 20 * standardDeviation)) {
            return 1.0d;
        } else {
            throw ex;
        }
    }
}
 
Example 23
/**
 * Test of interpolator for the exponential function.
 * <p>
 * |expm1^(n)(zeta)| <= e, zeta in [-1, 1]
 */
public void testExpm1Function() throws MathException {
    UnivariateRealFunction f = new Expm1Function();
    UnivariateRealInterpolator interpolator = new NevilleInterpolator();
    double x[], y[], z, expected, result, tolerance;

    // 5 interpolating points on interval [-1, 1]
    int n = 5;
    double min = -1.0, max = 1.0;
    x = new double[n];
    y = new double[n];
    for (int i = 0; i < n; i++) {
        x[i] = min + i * (max - min) / n;
        y[i] = f.value(x[i]);
    }
    double derivativebound = Math.E;
    UnivariateRealFunction p = interpolator.interpolate(x, y);

    z = 0.0; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = 0.5; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = -0.5; expected = f.value(z); result = p.value(z);
    tolerance = Math.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);
}
 
Example 24
public void testLargeMeanCumulativeProbability() {
    PoissonDistribution dist = new PoissonDistributionImpl(1.0);
    double mean = 1.0;
    while (mean <= 10000000.0) {
        dist.setMean(mean);

        double x = mean * 2.0;
        double dx = x / 10.0;
        double p = Double.NaN;
        double sigma = Math.sqrt(mean);
        while (x >= 0) {
            try {
                p = dist.cumulativeProbability(x);
                assertFalse("NaN cumulative probability returned for mean = " +
                        mean + " x = " + x,Double.isNaN(p));
                if (x > mean - 2 * sigma) {
                    assertTrue("Zero cum probaility returned for mean = " +
                            mean + " x = " + x, p > 0);
                }
            } catch (MathException ex) {
                fail("mean of " + mean + " and x of " + x + " caused " + ex.getMessage());
            }
            x -= dx;
        }
 
        mean *= 10.0;
    }
}
 
Example 25
Source Project: astor   Source File: BetaTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void testRegularizedBeta(double expected, double x, double a,
    double b)
{
    try {
        double actual = Beta.regularizedBeta(x, a, b);
        TestUtils.assertEquals(expected, actual, 10e-15);
    } catch(MathException ex){
        fail(ex.getMessage());
    }
}
 
Example 26
/** test dimensions */
@Test
public void testDimensions() throws MathException {
    CholeskyDecomposition llt =
        new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(testData));
    assertEquals(testData.length, llt.getL().getRowDimension());
    assertEquals(testData.length, llt.getL().getColumnDimension());
    assertEquals(testData.length, llt.getLT().getRowDimension());
    assertEquals(testData.length, llt.getLT().getColumnDimension());
}
 
Example 27
/**
 * The probability distribution function P(X <= x) for a Poisson distribution.
 * 
 * @param x the value at which the PDF is evaluated.
 * @return Poisson distribution function evaluated at x
 * @throws MathException if the cumulative probability can not be
 *            computed due to convergence or other numerical errors.
 */
@Override
public double cumulativeProbability(int x) throws MathException {
    if (x < 0) {
        return 0;
    }
    if (x == Integer.MAX_VALUE) {
        return 1;
    }
    return Gamma.regularizedGammaQ((double)x + 1, mean, 
            1E-12, Integer.MAX_VALUE);
}
 
Example 28
/** test non-symmetric matrix */
@Test(expected = NotSymmetricMatrixException.class)
public void testNotSymmetricMatrixException() throws MathException {
    double[][] changed = testData.clone();
    changed[0][changed[0].length - 1] += 1.0e-5;
    new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(changed));
}
 
Example 29
/**
 * The probability distribution function P(X <= x) for a Poisson
 * distribution.
 *
 * @param x the value at which the PDF is evaluated.
 * @return Poisson distribution function evaluated at x
 * @throws MathException if the cumulative probability can not be computed
 *             due to convergence or other numerical errors.
 */
@Override
public double cumulativeProbability(int x) throws MathException {
    if (x < 0) {
        return 0;
    }
    if (x == Integer.MAX_VALUE) {
        return 1;
    }
    return Gamma.regularizedGammaQ((double) x + 1, mean, 1E-12,
            Integer.MAX_VALUE);
}
 
Example 30
Source Project: astor   Source File: BrentMinimizerTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testQuinticMin() throws MathException {
    // The quintic function has zeros at 0, +-0.5 and +-1.
    // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
    UnivariateRealFunction f = new QuinticFunction();
    UnivariateRealOptimizer minimizer = new BrentOptimizer();
    assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2), 1.0e-8);
    assertEquals( 0.82221643, minimizer.optimize(f, GoalType.MINIMIZE,  0.3,  0.9), 1.0e-8);
    assertTrue(minimizer.getIterationCount() <= 50);

    // search in a large interval
    assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -1.0, 0.2), 1.0e-8);
    assertTrue(minimizer.getIterationCount() <= 50);

}