Java Code Examples for org.apache.commons.math3.distribution.TDistribution#inverseCumulativeProbability()

The following examples show how to use org.apache.commons.math3.distribution.TDistribution#inverseCumulativeProbability() . 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
Source File: XDataFrameLeastSquares.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the T-stats and the P-Value for all regression parameters
 */
private void computeParameterSignificance(RealVector betaVector) {
    try {
        final double residualDF = frame.rows().count() - (regressors.size() + 1);
        final TDistribution distribution = new TDistribution(residualDF);
        final double interceptParam = betaVector.getEntry(0);
        final double interceptStdError = intercept.data().getDouble(0, Field.STD_ERROR);
        final double interceptTStat = interceptParam / interceptStdError;
        final double interceptPValue = distribution.cumulativeProbability(-Math.abs(interceptTStat)) * 2d;
        final double interceptCI = interceptStdError * distribution.inverseCumulativeProbability(1d - alpha / 2d);
        this.intercept.data().setDouble(0, Field.PARAMETER, interceptParam);
        this.intercept.data().setDouble(0, Field.T_STAT, interceptTStat);
        this.intercept.data().setDouble(0, Field.P_VALUE, interceptPValue);
        this.intercept.data().setDouble(0, Field.CI_LOWER, interceptParam - interceptCI);
        this.intercept.data().setDouble(0, Field.CI_UPPER, interceptParam + interceptCI);
        final int offset = hasIntercept() ? 1 : 0;
        for (int i=0; i<regressors.size(); ++i) {
            final C regressor = regressors.get(i);
            final double betaParam = betaVector.getEntry(i + offset);
            final double betaStdError = betas.data().getDouble(regressor, Field.STD_ERROR);
            final double tStat = betaParam / betaStdError;
            final double pValue = distribution.cumulativeProbability(-Math.abs(tStat)) * 2d;
            final double betaCI = betaStdError * distribution.inverseCumulativeProbability(1d - alpha / 2d);
            this.betas.data().setDouble(regressor, Field.PARAMETER, betaParam);
            this.betas.data().setDouble(regressor, Field.T_STAT, tStat);
            this.betas.data().setDouble(regressor, Field.P_VALUE, pValue);
            this.betas.data().setDouble(regressor, Field.CI_LOWER, betaParam - betaCI);
            this.betas.data().setDouble(regressor, Field.CI_UPPER, betaParam + betaCI);
        }
    } catch (Exception ex) {
        throw new DataFrameException("Failed to compute regression coefficient t-stats and p-values", ex);
    }
}
 
Example 2
Source File: EstimateRepairability.java    From BART with MIT License 5 votes vote down vote up
private static double calcMeanCI(SummaryStatistics stats, double level) {
    try {
        TDistribution tDist = new TDistribution(stats.getN() - 1);
        double critVal = tDist.inverseCumulativeProbability(1.0 - (1 - level) / 2);
        return critVal * stats.getStandardDeviation() / Math.sqrt(stats.getN());
    } catch (MathIllegalArgumentException e) {
        return Double.NaN;
    }
}
 
Example 3
Source File: ConfidenceInterval.java    From rival with Apache License 2.0 5 votes vote down vote up
/**
 * Adapted from https://gist.github.com/gcardone/5536578.
 *
 * @param alpha probability of incorrectly rejecting the null hypothesis (1
 * - confidence_level)
 * @param df degrees of freedom
 * @param n number of observations
 * @param std standard deviation
 * @param mean mean
 * @return array with the confidence interval: [mean - margin of error, mean
 * + margin of error]
 */
public static double[] getConfidenceInterval(final double alpha, final int df, final int n, final double std, final double mean) {
    // Create T Distribution with df degrees of freedom
    TDistribution tDist = new TDistribution(df);
    // Calculate critical value
    double critVal = tDist.inverseCumulativeProbability(1.0 - alpha);
    // Calculate confidence interval
    double ci = critVal * std / Math.sqrt(n);
    double lower = mean - ci;
    double upper = mean + ci;
    double[] interval = new double[]{lower, upper};
    return interval;
}
 
Example 4
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha)
throws OutOfRangeException {
    if (n < 3) {
        return Double.NaN;
    }
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    // No advertised NotStrictlyPositiveException here - will return NaN above
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 5
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha) {
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 6
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha) {
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 7
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha)
throws OutOfRangeException {
    if (n < 3) {
        return Double.NaN;
    }
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    // No advertised NotStrictlyPositiveException here - will return NaN above
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 8
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha) {
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 9
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha)
throws OutOfRangeException {
    if (n < 3) {
        return Double.NaN;
    }
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    // No advertised NotStrictlyPositiveException here - will return NaN above
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 10
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha)
throws OutOfRangeException {
    if (n < 3) {
        return Double.NaN;
    }
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    // No advertised NotStrictlyPositiveException here - will return NaN above
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}
 
Example 11
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the half-width of a (100-100*alpha)% confidence interval for
 * the slope estimate.
 * <p>
 * The (100-100*alpha)% confidence interval is </p>
 * <p>
 * <code>(getSlope() - getSlopeConfidenceInterval(),
 * getSlope() + getSlopeConfidenceInterval())</code></p>
 * <p>
 * To request, for example, a 99% confidence interval, use
 * <code>alpha = .01</code></p>
 * <p>
 * <strong>Usage Note</strong>:<br>
 * The validity of this statistic depends on the assumption that the
 * observations included in the model are drawn from a
 * <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
 * Bivariate Normal Distribution</a>.</p>
 * <p>
 * <strong> Preconditions:</strong><ul>
 * <li>If there are fewer that <strong>three</strong> observations in the
 * model, or if there is no variation in x, this returns
 * <code>Double.NaN</code>.
 * </li>
 * <li><code>(0 < alpha < 1)</code>; otherwise an
 * <code>OutOfRangeException</code> is thrown.
 * </li></ul></p>
 *
 * @param alpha the desired significance level
 * @return half-width of 95% confidence interval for the slope estimate
 * @throws OutOfRangeException if the confidence interval can not be computed.
 */
public double getSlopeConfidenceInterval(final double alpha)
throws OutOfRangeException {
    if (n < 3) {
        return Double.NaN;
    }
    if (alpha >= 1 || alpha <= 0) {
        throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
                                      alpha, 0, 1);
    }
    // No advertised NotStrictlyPositiveException here - will return NaN above
    TDistribution distribution = new TDistribution(n - 2);
    return getSlopeStdErr() *
        distribution.inverseCumulativeProbability(1d - alpha / 2d);
}