Search in sources :

Example 1 with MathException

use of org.apache.commons.math.MathException in project beast-mcmc by beast-dev.

the class GammaDistribution method testQuantileCM.

private static void testQuantileCM(double y, double shape, double scale) {
    long time = System.currentTimeMillis();
    double value = 0;
    try {
        for (int i = 0; i < 1000; i++) {
            value = (new org.apache.commons.math.distribution.GammaDistributionImpl(shape, scale)).inverseCumulativeProbability(y);
        }
        value = (new org.apache.commons.math.distribution.GammaDistributionImpl(shape, scale)).inverseCumulativeProbability(y);
    } catch (MathException e) {
        e.printStackTrace();
    }
    long elapsed = System.currentTimeMillis() - time;
    System.out.println("commons.maths inverseCDF, " + y + ", for shape=" + shape + ", scale=" + scale + " : " + value + ", time=" + elapsed + "ms");
}
Also used : GammaDistributionImpl(org.apache.commons.math.distribution.GammaDistributionImpl) MathException(org.apache.commons.math.MathException)

Example 2 with MathException

use of org.apache.commons.math.MathException in project beast-mcmc by beast-dev.

the class NormalPeriodPriorDistribution method calculateLogPosteriorPredictiveCDF.

public double calculateLogPosteriorPredictiveCDF(double value, boolean upperTail) {
    double mean = currentParameters[0];
    double sd = Math.sqrt(currentParameters[3] * (currentParameters[1] + 1) / (currentParameters[2] * currentParameters[1]));
    double scaledValue = (value - mean) / sd;
    double out;
    if (2 * currentParameters[2] <= normalApproximationThreshold) {
        TDistributionImpl tDist = new TDistributionImpl(2 * currentParameters[2]);
        try {
            out = upperTail ? Math.log(tDist.cumulativeProbability(-scaledValue)) : Math.log(tDist.cumulativeProbability(scaledValue));
        } catch (MathException e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        out = upperTail ? NormalDistribution.standardCDF(-scaledValue, true) : NormalDistribution.standardCDF(scaledValue, true);
    }
    return out;
}
Also used : TDistributionImpl(org.apache.commons.math.distribution.TDistributionImpl) MathException(org.apache.commons.math.MathException)

Example 3 with MathException

use of org.apache.commons.math.MathException in project compiler by boalang.

the class StatisticsAggregator method finish.

/** {@inheritDoc} */
@Override
public void finish() throws IOException, InterruptedException {
    if (this.isCombining()) {
        String s = "";
        for (final Long key : map.keySet()) s += key + ":" + map.get(key) + ";";
        this.collect(s, null);
        return;
    }
    float median = 0;
    long medianPos = count / 2L;
    long curPos = 0;
    long prevPos = 0;
    long prevKey = 0;
    for (final Long key : map.keySet()) {
        curPos = prevPos + map.get(key);
        if (prevPos <= medianPos && medianPos < curPos) {
            if (curPos % 2 == 0 && prevPos == medianPos)
                median = (float) (key + prevKey) / 2.0f;
            else
                median = key;
            break;
        }
        prevKey = key;
        prevPos = curPos;
    }
    double s1 = 0;
    double s2 = 0;
    double s3 = 0;
    double s4 = 0;
    final SummaryStatistics summaryStatistics = new SummaryStatistics();
    for (final Long key : map.keySet()) {
        s1 += key * map.get(key);
        s2 += key * key * map.get(key);
        s3 += key * key * key * map.get(key);
        s4 += key * key * key * key * map.get(key);
        for (int i = 0; i < map.get(key); i++) summaryStatistics.addValue(key);
    }
    final double mean = s1 / (double) count;
    final double var = s2 / (double) (count - 1) - s1 * s1 / (double) (count * (count - 1));
    final double stdev = Math.sqrt(var);
    final double skewness = (s3 - 3 * s1 * s2 / (double) count + s1 * s1 * s1 * 2 / (count * count)) / (count * stdev * var);
    final double kurtosis = (s4 - s3 * s1 * 4 / count + s2 * s1 * s1 * 6 / (double) (count * count) - s1 * s1 * s1 * s1 * 3 / (double) (count * count * count)) / (count * var * var);
    double ci = 0.0;
    try {
        final TDistributionImpl tDist = new TDistributionImpl(summaryStatistics.getN() - 1);
        final double a = tDist.inverseCumulativeProbability(1.0 - 0.025);
        ci = a * summaryStatistics.getStandardDeviation() / Math.sqrt(summaryStatistics.getN());
    } catch (final MathException e) {
    }
    this.collect(s1 + ", " + mean + ", " + median + ", " + stdev + ", " + var + ", " + kurtosis + ", " + skewness + ", " + ci);
}
Also used : TDistributionImpl(org.apache.commons.math.distribution.TDistributionImpl) MathException(org.apache.commons.math.MathException) SummaryStatistics(org.apache.commons.math.stat.descriptive.SummaryStatistics)

Example 4 with MathException

use of org.apache.commons.math.MathException in project compiler by boalang.

the class ConfidenceIntervalAggregator method finish.

/** {@inheritDoc} */
@Override
public void finish() throws IOException, InterruptedException {
    if (this.isCombining()) {
        String s = "";
        for (final Long key : map.keySet()) s += key + ":" + map.get(key) + ";";
        this.collect(s, null);
        return;
    }
    try {
        final SummaryStatistics summaryStatistics = new SummaryStatistics();
        for (final Long key : map.keySet()) for (int i = 0; i < map.get(key); i++) summaryStatistics.addValue(key);
        final double a = new TDistributionImpl(summaryStatistics.getN() - 1).inverseCumulativeProbability(1.0 - n / 200.0);
        this.collect(a * summaryStatistics.getStandardDeviation() / Math.sqrt(summaryStatistics.getN()));
    } catch (final MathException e) {
    }
}
Also used : TDistributionImpl(org.apache.commons.math.distribution.TDistributionImpl) MathException(org.apache.commons.math.MathException) SummaryStatistics(org.apache.commons.math.stat.descriptive.SummaryStatistics)

Aggregations

MathException (org.apache.commons.math.MathException)4 TDistributionImpl (org.apache.commons.math.distribution.TDistributionImpl)3 SummaryStatistics (org.apache.commons.math.stat.descriptive.SummaryStatistics)2 GammaDistributionImpl (org.apache.commons.math.distribution.GammaDistributionImpl)1