Search in sources :

Example 1 with TDistributionImpl

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

the class NormalPeriodPriorDistribution method calculateLogPosteriorPredictiveProbability.

public double calculateLogPosteriorPredictiveProbability(double value) {
    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]);
        out = Math.log(tDist.density(scaledValue));
    } else {
        out = NormalDistribution.logPdf(scaledValue, 0, 1);
    }
    return out;
}
Also used : TDistributionImpl(org.apache.commons.math.distribution.TDistributionImpl)

Example 2 with TDistributionImpl

use of org.apache.commons.math.distribution.TDistributionImpl 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 TDistributionImpl

use of org.apache.commons.math.distribution.TDistributionImpl 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 TDistributionImpl

use of org.apache.commons.math.distribution.TDistributionImpl 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

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