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

The following examples show how to use org.apache.commons.math3.distribution.RealDistribution#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: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 2
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the smallest i such that the sum of the masses of the bins
 *  through i is at least p.</li>
 * <li>
 *   Let K be the within-bin kernel distribution for bin i.</br>
 *   Let K(B) be the mass of B under K. <br/>
 *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
 *   mass of the bins below B under K).<br/>
 *   Let P(B) be the probability of bin i.<br/>
 *   Let P(B-) be the sum of the bin masses below bin i. <br/>
 *   Let pCrit = p - P(B-)<br/>
 * <li>Return the inverse of K evaluated at <br/>
 *    K(B-) + pCrit * K(B) / P(B) </li>
 *  </ol></p>
 *
 * @since 3.1
 */
@Override
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
    if (p < 0.0 || p > 1.0) {
        throw new OutOfRangeException(p, 0, 1);
    }

    if (p == 0.0) {
        return getSupportLowerBound();
    }

    if (p == 1.0) {
        return getSupportUpperBound();
    }

    int i = 0;
    while (cumBinP(i) < p) {
        i++;
    }

    final RealDistribution kernel = getKernel(binStats.get(i));
    final double kB = kB(i);
    final double[] binBounds = getUpperBounds();
    final double lower = i == 0 ? min : binBounds[i - 1];
    final double kBminus = kernel.cumulativeProbability(lower);
    final double pB = pB(i);
    final double pBminus = pBminus(i);
    final double pCrit = p - pBminus;
    if (pCrit <= 0) {
        return lower;
    }
    return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
}
 
Example 3
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 4
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 5
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 6
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the smallest i such that the sum of the masses of the bins
 *  through i is at least p.</li>
 * <li>
 *   Let K be the within-bin kernel distribution for bin i.</br>
 *   Let K(B) be the mass of B under K. <br/>
 *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
 *   mass of the bins below B under K).<br/>
 *   Let P(B) be the probability of bin i.<br/>
 *   Let P(B-) be the sum of the bin masses below bin i. <br/>
 *   Let pCrit = p - P(B-)<br/>
 * <li>Return the inverse of K evaluated at <br/>
 *    K(B-) + pCrit * K(B) / P(B) </li>
 *  </ol></p>
 *
 * @since 3.1
 */
@Override
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
    if (p < 0.0 || p > 1.0) {
        throw new OutOfRangeException(p, 0, 1);
    }

    if (p == 0.0) {
        return getSupportLowerBound();
    }

    if (p == 1.0) {
        return getSupportUpperBound();
    }

    int i = 0;
    while (cumBinP(i) < p) {
        i++;
    }

    final RealDistribution kernel = getKernel(binStats.get(i));
    final double kB = kB(i);
    final double[] binBounds = getUpperBounds();
    final double lower = i == 0 ? min : binBounds[i - 1];
    final double kBminus = kernel.cumulativeProbability(lower);
    final double pB = pB(i);
    final double pBminus = pBminus(i);
    final double pCrit = p - pBminus;
    if (pCrit <= 0) {
        return lower;
    }
    return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
}
 
Example 7
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 8
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the smallest i such that the sum of the masses of the bins
 *  through i is at least p.</li>
 * <li>
 *   Let K be the within-bin kernel distribution for bin i.</br>
 *   Let K(B) be the mass of B under K. <br/>
 *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
 *   mass of the bins below B under K).<br/>
 *   Let P(B) be the probability of bin i.<br/>
 *   Let P(B-) be the sum of the bin masses below bin i. <br/>
 *   Let pCrit = p - P(B-)<br/>
 * <li>Return the inverse of K evaluated at <br/>
 *    K(B-) + pCrit * K(B) / P(B) </li>
 *  </ol></p>
 *
 * @since 3.1
 */
@Override
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
    if (p < 0.0 || p > 1.0) {
        throw new OutOfRangeException(p, 0, 1);
    }

    if (p == 0.0) {
        return getSupportLowerBound();
    }

    if (p == 1.0) {
        return getSupportUpperBound();
    }

    int i = 0;
    while (cumBinP(i) < p) {
        i++;
    }

    final RealDistribution kernel = getKernel(binStats.get(i));
    final double kB = kB(i);
    final double[] binBounds = getUpperBounds();
    final double lower = i == 0 ? min : binBounds[i - 1];
    final double kBminus = kernel.cumulativeProbability(lower);
    final double pB = pB(i);
    final double pBminus = pBminus(i);
    final double pCrit = p - pBminus;
    if (pCrit <= 0) {
        return lower;
    }
    return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
}
 
Example 9
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 10
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 11
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) throws Exception {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example 12
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the smallest i such that the sum of the masses of the bins
 *  through i is at least p.</li>
 * <li>
 *   Let K be the within-bin kernel distribution for bin i.</br>
 *   Let K(B) be the mass of B under K. <br/>
 *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
 *   mass of the bins below B under K).<br/>
 *   Let P(B) be the probability of bin i.<br/>
 *   Let P(B-) be the sum of the bin masses below bin i. <br/>
 *   Let pCrit = p - P(B-)<br/>
 * <li>Return the inverse of K evaluated at <br/>
 *    K(B-) + pCrit * K(B) / P(B) </li>
 *  </ol></p>
 *
 * @since 3.1
 */
public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
    if (p < 0.0 || p > 1.0) {
        throw new OutOfRangeException(p, 0, 1);
    }

    if (p == 0.0) {
        return getSupportLowerBound();
    }

    if (p == 1.0) {
        return getSupportUpperBound();
    }

    int i = 0;
    while (cumBinP(i) < p) {
        i++;
    }

    final RealDistribution kernel = getKernel(binStats.get(i));
    final double kB = kB(i);
    final double[] binBounds = getUpperBounds();
    final double lower = i == 0 ? min : binBounds[i - 1];
    final double kBminus = kernel.cumulativeProbability(lower);
    final double pB = pB(i);
    final double pBminus = pBminus(i);
    final double pCrit = p - pBminus;
    if (pCrit <= 0) {
        return lower;
    }
    return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
}
 
Example 13
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @throws MathIllegalArgumentException if the underlynig distribution throws one
 * @since 2.2
 * @deprecated use the distribution's sample() method
 */
@Deprecated
public double nextInversionDeviate(RealDistribution distribution)
    throws MathIllegalArgumentException {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 14
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @throws MathIllegalArgumentException if the underlynig distribution throws one
 * @since 2.2
 * @deprecated use the distribution's sample() method
 */
@Deprecated
public double nextInversionDeviate(RealDistribution distribution)
    throws MathIllegalArgumentException {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 15
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @throws MathIllegalArgumentException if the underlynig distribution throws one
 * @since 2.2
 * @deprecated use the distribution's sample() method
 */
@Deprecated
public double nextInversionDeviate(RealDistribution distribution)
    throws MathIllegalArgumentException {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 16
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @throws MathIllegalArgumentException if the underlynig distribution throws one
 * @since 2.2
 * @deprecated use the distribution's sample() method
 */
@Deprecated
public double nextInversionDeviate(RealDistribution distribution)
    throws MathIllegalArgumentException {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 17
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @throws MathIllegalArgumentException if the underlynig distribution throws one
 * @since 2.2
 * @deprecated use the distribution's sample() method
 */
public double nextInversionDeviate(RealDistribution distribution)
    throws MathIllegalArgumentException {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 18
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @since 2.2
 */
public double nextInversionDeviate(RealDistribution distribution) {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 19
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generate a random deviate from the given distribution using the
 * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 *
 * @param distribution Continuous distribution to generate a random value from
 * @return a random value sampled from the given distribution
 * @since 2.2
 */
public double nextInversionDeviate(RealDistribution distribution) {
    return distribution.inverseCumulativeProbability(nextUniform(0, 1));

}
 
Example 20
Source File: DistributionUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Percent point function (inverse of cdf) at q.
 * @param dis Distribution.
 * @param q Lower tail probability
 * @return PMF value.
 */
public static double ppf(RealDistribution dis, Number q){
    return dis.inverseCumulativeProbability(q.doubleValue());
}