Example usage for org.apache.commons.math.distribution TDistributionImpl TDistributionImpl

List of usage examples for org.apache.commons.math.distribution TDistributionImpl TDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution TDistributionImpl TDistributionImpl.

Prototype

public TDistributionImpl(double degreesOfFreedom) 

Source Link

Document

Create a t distribution using the given degrees of freedom.

Usage

From source file:desmoj.core.statistic.ConfidenceCalculator.java

/**
 * Constructor for a ConfidenceCalculator object that is connected to a
 * <code>ValueSupplier</code>. The confidence level is set to 0.95.
 * /*from ww  w.  j av  a2s.  co m*/
 * @param ownerModel
 *            Model : The model this ConfidenceCalculator is associated to
 * @param name
 *            java.lang.String : The name of this ConfidenceCalculator
 *            object
 * @param valSup
 *            ValueSupplier : The ValueSupplier providing the value for this
 *            ConfidenceCalculator. The given ValueSupplier will be observed
 *            by this ConfidenceCalculator object.
 * @param showInReport
 *            boolean : Flag for showing the report about this
 *            ConfidenceCalculator.
 * @param showInTrace
 *            boolean : Flag for showing the trace output of this
 *            ConfidenceCalculator.
 */
public ConfidenceCalculator(Model ownerModel, String name, ValueSupplier valSup, boolean showInReport,
        boolean showInTrace) {
    // call the constructor of ValueStatistics
    super(ownerModel, name, valSup, showInReport, showInTrace);
    _tDistribution = new TDistributionImpl(1);
    _confidenceLevel = 0.95;
}

From source file:geogebra.common.kernel.statistics.AlgoTMeanEstimate.java

private double getMarginOfError(double sd, double n, double confLevel) throws MathException {
    tDist = new TDistributionImpl(n - 1);
    double a = tDist.inverseCumulativeProbability((confLevel + 1d) / 2);
    return a * sd / Math.sqrt(n);
}

From source file:boa.aggregators.ConfidenceIntervalAggregator.java

/** {@inheritDoc} */
@Override/*from  www  .  j  av  a2s.co  m*/
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) {
    }
}

From source file:com.opengamma.analytics.math.regression.OrdinaryLeastSquaresRegression.java

private LeastSquaresRegressionResult getResultWithStatistics(final double[][] x, final double[] y,
        final double[] betas, final double[] yModel, final DoubleMatrix2D transpose,
        final DoubleMatrix2D matrix, final boolean useIntercept) {
    double yMean = 0.;
    for (final double y1 : y) {
        yMean += y1;//from   w ww .  ja v  a  2 s .co  m
    }
    yMean /= y.length;
    double totalSumOfSquares = 0.;
    double errorSumOfSquares = 0.;
    final int n = x.length;
    final int k = betas.length;
    final double[] residuals = new double[n];
    final double[] stdErrorBetas = new double[k];
    final double[] tStats = new double[k];
    final double[] pValues = new double[k];
    for (int i = 0; i < n; i++) {
        totalSumOfSquares += (y[i] - yMean) * (y[i] - yMean);
        residuals[i] = y[i] - yModel[i];
        errorSumOfSquares += residuals[i] * residuals[i];
    }
    final double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares;
    final double[][] covarianceBetas = convertArray(
            _algebra.inverse(_algebra.mult(transpose, matrix)).toArray());
    final double rSquared = regressionSumOfSquares / totalSumOfSquares;
    final double adjustedRSquared = 1. - (1 - rSquared) * (n - 1.) / (n - k);
    final double meanSquareError = errorSumOfSquares / (n - k);
    final ContinuousDistribution studentT = new TDistributionImpl(n - k);
    // final ProbabilityDistribution<Double> studentT = new
    // StudentTDistribution(n - k);
    for (int i = 0; i < k; i++) {
        stdErrorBetas[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]);
        tStats[i] = betas[i] / stdErrorBetas[i];
        try {
            pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i]));
        } catch (final org.apache.commons.math.MathException e) {
            throw new com.opengamma.analytics.math.MathException(e);
        }
    }
    return new LeastSquaresRegressionResult(betas, residuals, meanSquareError, stdErrorBetas, rSquared,
            adjustedRSquared, tStats, pValues, useIntercept);
}

From source file:desmoj.core.statistic.ConfidenceCalculator.java

/**
 * Constructor for a ConfidenceCalculator object that has no connection to a
 * <code>ValueSupplier</code>. The confidence level is set to 0.95.
 * /*from w w  w  . ja  v a  2 s  .  c o  m*/
 * @param ownerModel
 *            Model : The model this ConfidenceCalculator is associated to
 * @param name
 *            java.lang.String : The name of this ConfidenceCalculator
 *            object
 * @param showInReport
 *            boolean : Flag for showing the report about this
 *            ConfidenceCalculator.
 * @param showInTrace
 *            boolean : Flag for showing the trace output of this
 *            ConfidenceCalculator.
 */
public ConfidenceCalculator(Model ownerModel, String name, boolean showInReport, boolean showInTrace) {
    // call the constructor of ValueStatistics
    super(ownerModel, name, showInReport, showInTrace);
    _tDistribution = new TDistributionImpl(1);
    _confidenceLevel = 0.95;
}

From source file:dr.evomodel.epidemiology.casetocase.periodpriors.NormalPeriodPriorDistribution.java

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;/*from   w  w  w  .ja va2  s  . co  m*/

    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;
}

From source file:com.opengamma.analytics.math.regression.WeightedLeastSquaresRegression.java

private LeastSquaresRegressionResult getResultWithStatistics(final double[][] x, final double[][] w,
        final double[] y, final double[] betas, final double[] yModel, final DoubleMatrix2D transpose,
        final DoubleMatrix2D matrix, final boolean useIntercept) {
    double yMean = 0.;
    for (final double y1 : y) {
        yMean += y1;/*from   w  w  w  .  j av  a 2 s. c  o  m*/
    }
    yMean /= y.length;
    double totalSumOfSquares = 0.;
    double errorSumOfSquares = 0.;
    final int n = x.length;
    final int k = betas.length;
    final double[] residuals = new double[n];
    final double[] standardErrorsOfBeta = new double[k];
    final double[] tStats = new double[k];
    final double[] pValues = new double[k];
    for (int i = 0; i < n; i++) {
        totalSumOfSquares += w[i][i] * (y[i] - yMean) * (y[i] - yMean);
        residuals[i] = y[i] - yModel[i];
        errorSumOfSquares += w[i][i] * residuals[i] * residuals[i];
    }
    final double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares;
    final double[][] covarianceBetas = convertArray(
            _algebra.inverse(_algebra.mult(transpose, matrix)).toArray());
    final double rSquared = regressionSumOfSquares / totalSumOfSquares;
    final double adjustedRSquared = 1. - (1 - rSquared) * (n - 1) / (n - k);
    final double meanSquareError = errorSumOfSquares / (n - k);
    final ContinuousDistribution studentT = new TDistributionImpl(n - k);
    for (int i = 0; i < k; i++) {
        standardErrorsOfBeta[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]);
        tStats[i] = betas[i] / standardErrorsOfBeta[i];
        try {
            pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i]));
        } catch (final org.apache.commons.math.MathException e) {
            throw new com.opengamma.analytics.math.MathException(e);
        }
    }
    return new WeightedLeastSquaresRegressionResult(betas, residuals, meanSquareError, standardErrorsOfBeta,
            rSquared, adjustedRSquared, tStats, pValues, useIntercept);
}

From source file:boa.aggregators.StatisticsAggregator.java

/** {@inheritDoc} */
@Override//from ww w  .  jav a  2  s  .  com
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);
}

From source file:dr.evomodel.epidemiology.casetocase.periodpriors.NormalPeriodPriorDistribution.java

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;/* w  ww . j a v a 2  s . c o  m*/

    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;
}

From source file:geogebra.kernel.statistics.AlgoTMean2Estimate.java

/**
 * Computes margin of error for 2-sample t-estimate; 
 * this is the half-width of the confidence interval
 * //  w  w w. j  a  v  a 2  s  . com
 * @param v1 first sample variance
 * @param v2 second sample variance
 * @param n1 first sample n
 * @param n2 second sample n
 * @param confLevel confidence level
 * @return margin of error for 2 mean interval estimate
 * @throws MathException
 */
private double getMarginOfError(double v1, double n1, double v2, double n2, double confLevel, boolean pooled)
        throws MathException {

    if (pooled) {

        double pooledVariance = ((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 2);
        double se = Math.sqrt(pooledVariance * (1d / n1 + 1d / n2));
        tDist = new TDistributionImpl(getDegreeOfFreedom(v1, v2, n1, n2, pooled));
        double a = tDist.inverseCumulativeProbability((confLevel + 1d) / 2);
        return a * se;

    } else {

        double se = Math.sqrt((v1 / n1) + (v2 / n2));
        tDist = new TDistributionImpl(getDegreeOfFreedom(v1, v2, n1, n2, pooled));
        double a = tDist.inverseCumulativeProbability((confLevel + 1d) / 2);
        return a * se;
    }

}