Search in sources :

Example 31 with MathContext

use of java.math.MathContext in project GDSC-SMLM by aherbert.

the class SCMOSLikelihoodWrapperTest method canComputePValue.

private void canComputePValue(BaseNonLinearFunction nlf) {
    System.out.println(nlf.name);
    int n = maxx * maxx;
    double[] a = new double[] { 1 };
    // Simulate sCMOS camera
    nlf.initialise(a);
    RandomDataGenerator rdg = new RandomDataGenerator(new Well19937c(30051977));
    double[] k = Utils.newArray(n, 0, 1.0);
    for (int i = 0; i < n; i++) {
        double u = nlf.eval(i);
        if (u > 0)
            u = rdg.nextPoisson(u);
        k[i] = u * g[i] + rdg.nextGaussian(o[i], sd[i]);
    }
    SCMOSLikelihoodWrapper f = new SCMOSLikelihoodWrapper(nlf, a, k, n, var, g, o);
    double oll = f.computeObservedLikelihood();
    double oll2 = 0;
    double[] op = new double[n];
    for (int j = 0; j < n; j++) {
        op[j] = SCMOSLikelihoodWrapper.likelihood((k[j] - o[j]) / g[j], var[j], g[j], o[j], k[j]);
        oll2 -= Math.log(op[j]);
    }
    System.out.printf("oll=%f, oll2=%f\n", oll, oll2);
    Assert.assertEquals("Observed Log-likelihood", oll2, oll, oll2 * 1e-10);
    double min = Double.POSITIVE_INFINITY;
    double mina = 0;
    for (int i = 5; i <= 15; i++) {
        a[0] = (double) i / 10;
        double ll = f.likelihood(a);
        double llr = f.computeLogLikelihoodRatio(ll);
        BigDecimal product = new BigDecimal(1);
        double ll2 = 0;
        for (int j = 0; j < n; j++) {
            double p1 = SCMOSLikelihoodWrapper.likelihood(nlf.eval(j), var[j], g[j], o[j], k[j]);
            ll2 -= Math.log(p1);
            double ratio = p1 / op[j];
            product = product.multiply(new BigDecimal(ratio));
        }
        double llr2 = -2 * Math.log(product.doubleValue());
        double q = f.computeQValue(ll);
        System.out.printf("a=%f, ll=%f, ll2=%f, llr=%f, llr2=%f, product=%s, p=%f\n", a[0], ll, ll2, llr, llr2, product.round(new MathContext(4)).toString(), q);
        if (min > ll) {
            min = ll;
            mina = a[0];
        }
        // too small to store in a double.
        if (product.doubleValue() > 0)
            Assert.assertEquals("Log-likelihood", llr, llr2, llr * 1e-10);
    }
    Assert.assertEquals("min", 1, mina, 0);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Well19937c(org.apache.commons.math3.random.Well19937c) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 32 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class OldBigDecimalArithmeticTest method testDivideINonTrivial.

public void testDivideINonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    mc = new MathContext(17, RoundingMode.FLOOR);
    a = new BigDecimal("12345678901234567E1234");
    b = new BigDecimal("1.23456789012345679");
    assertEquals("incorrect value", "1E+1250", a.round(mc).divide(b.round(mc)).toString());
    res = a.divide(b, BigDecimal.ROUND_FLOOR);
    assertEquals("incorrect value", "9.999999999999999E+1249", res.toString());
    a = new BigDecimal("1234567890123456789012345678.9012395");
    b = new BigDecimal("6172839450617283945061728394.5061975");
    res = a.divide(b, BigDecimal.ROUND_UNNECESSARY);
    assertEquals("incorrect value", "0.2000000", res.toString());
    a = new BigDecimal("1234567890123456789012345678.9012395");
    b = new BigDecimal("1000000000000000090000000000.0000005");
    try {
        res = a.divide(b, BigDecimal.ROUND_UNNECESSARY);
        fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 33 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class OldBigDecimalCompareTest method testNegateMathContextPositive.

public void testNegateMathContextPositive() {
    String a = "92948782094488478231212478987482988429808779810457634781384756794987";
    MathContext mc = new MathContext(37, RoundingMode.FLOOR);
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), 41);
    BigDecimal res = aNumber.negate(mc);
    assertEquals("incorrect value", "-929487820944884782312124789.8748298843", res.toString());
    assertEquals("incorrect scale", 10, res.scale());
}
Also used : BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 34 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class OldBigDecimalConstructorsTest method testConstrLongMathContext.

/**
     * new BigDecimal(long, MathContext)
     */
public void testConstrLongMathContext() {
    long a = 4576578677732546982L;
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "45766";
    int resScale = -14;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
    // Now test more than just RoundingMode.CEILING
    //
    BigDecimal bd;
    mc = new MathContext(15, RoundingMode.UP);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678902E+16", bd.toString());
    mc = new MathContext(12, RoundingMode.DOWN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678E+16", bd.toString());
    mc = new MathContext(15, RoundingMode.CEILING);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678902E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345678901E+16", bd.toString());
    mc = new MathContext(12, RoundingMode.FLOOR);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.89012345678E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.89012345679E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_EVEN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890135L, mc);
    assertEquals("incorrect value", "-7.890123456789014E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_UP);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789013E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789013E+16", bd.toString());
    mc = new MathContext(16, RoundingMode.HALF_DOWN);
    bd = new BigDecimal(78901234567890125L, mc);
    assertEquals("incorrect value", "7.890123456789012E+16", bd.toString());
    bd = new BigDecimal(-78901234567890125L, mc);
    assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    mc = new MathContext(8, RoundingMode.UNNECESSARY);
    try {
        bd = new BigDecimal(78901234567890125L, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    try {
        bd = new BigDecimal(-78901234567890125L, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 35 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class OldBigDecimalConstructorsTest method testConstrBigIntegerScaleMathContext_AndroidFailure.

public void testConstrBigIntegerScaleMathContext_AndroidFailure() {
    MathContext mc;
    BigDecimal bd;
    mc = new MathContext(17, RoundingMode.FLOOR);
    bd = new BigDecimal(new BigInteger("123456789012345678"), 3, mc);
    assertEquals("incorrect value", "123456789012345.67", bd.toString());
}
Also used : BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Aggregations

MathContext (java.math.MathContext)237 BigDecimal (java.math.BigDecimal)224 RoundingMode (java.math.RoundingMode)73 BigInteger (java.math.BigInteger)71 List (java.util.List)9 Test (org.junit.Test)8 BigDecimalMath (ch.obermuhlner.math.big.BigDecimalMath)7 Arrays (java.util.Arrays)7 Function (java.util.function.Function)7 StopWatch (ch.obermuhlner.math.big.example.StopWatch)3 IOException (java.io.IOException)3 Random (java.util.Random)3 Context (ch.obermuhlner.math.big.BigFloat.Context)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 DataCell (org.knime.core.data.DataCell)2