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

The following examples show how to use org.apache.commons.math3.distribution.RealDistribution#cumulativeProbability() . 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: KolmogorovSmirnovTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where
 * \(F\) is the distribution (cdf) function associated with {@code distribution}, \(n\) is the
 * length of {@code data} and \(F_n\) is the empirical distribution that puts mass \(1/n\) at
 * each of the values in {@code data}.
 *
 * @param distribution reference distribution
 * @param data sample being evaluated
 * @return Kolmogorov-Smirnov statistic \(D_n\)
 * @throws InsufficientDataException if {@code data} does not have length at least 2
 * @throws NullArgumentException if {@code data} is null
 */
public double kolmogorovSmirnovStatistic(RealDistribution distribution, double[] data) {
    checkArray(data);
    final int n = data.length;
    final double nd = n;
    final double[] dataCopy = new double[n];
    System.arraycopy(data, 0, dataCopy, 0, n);
    Arrays.sort(dataCopy);
    double d = 0d;
    for (int i = 1; i <= n; i++) {
        final double yi = distribution.cumulativeProbability(dataCopy[i - 1]);
        final double currD = FastMath.max(yi - (i - 1) / nd, i / nd - yi);
        if (currD > d) {
            d = currD;
        }
    }
    return d;
}
 
Example 2
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
 *
 * @since 3.1
 */
public double cumulativeProbability(double x) {
    if (x < min) {
        return 0d;
    } else if (x >= max) {
        return 1d;
    }
    final int binIndex = findBin(x);
    final double pBminus = pBminus(binIndex);
    final double pB = pB(binIndex);
    final double[] binBounds = getUpperBounds();
    final double kB = kB(binIndex);
    final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
    final RealDistribution kernel = k(x);
    final double withinBinCum =
        (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
    return pBminus + pB * withinBinCum;
}
 
Example 3
Source File: KolmogorovSmirnovTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where
 * \(F\) is the distribution (cdf) function associated with {@code distribution}, \(n\) is the
 * length of {@code data} and \(F_n\) is the empirical distribution that puts mass \(1/n\) at
 * each of the values in {@code data}.
 *
 * @param distribution reference distribution
 * @param data sample being evaluated
 * @return Kolmogorov-Smirnov statistic \(D_n\)
 * @throws InsufficientDataException if {@code data} does not have length at least 2
 * @throws NullArgumentException if {@code data} is null
 */
public double kolmogorovSmirnovStatistic(RealDistribution distribution, double[] data) {
    checkArray(data);
    final int n = data.length;
    final double nd = n;
    final double[] dataCopy = new double[n];
    System.arraycopy(data, 0, dataCopy, 0, n);
    Arrays.sort(dataCopy);
    double d = 0d;
    for (int i = 1; i <= n; i++) {
        final double yi = distribution.cumulativeProbability(dataCopy[i - 1]);
        final double currD = FastMath.max(yi - (i - 1) / nd, i / nd - yi);
        if (currD > d) {
            d = currD;
        }
    }
    return d;
}
 
Example 4
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeDensityTestValues() {
    final double[] testPoints = getCumulativeTestPoints();
    final double[] densityValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double density = kernel.density(testPoints[i]);
        densityValues[i] = density * (bin == 0 ? firstBinMass : binMass) / withinBinKernelMass;   
    }
    return densityValues;
}
 
Example 5
Source File: RealDistributionComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void addCDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
    // generates Log data
    List<Number> xData = new ArrayList<Number>();
    List<Number> yData = new ArrayList<Number>();
    int samples = 100;
    double stepSize = (upperBound - lowerBound) / (double) samples;
    for (double x = lowerBound; x <= upperBound; x += stepSize) {
      double density = distribution.cumulativeProbability(x);
      if (! Double.isInfinite(density) && ! Double.isNaN(density)) {
          xData.add(x);
          yData.add(density);
      }
    }

    Series series = chart.addSeries(desc, xData, yData);
    series.setMarker(SeriesMarker.NONE);
    series.setLineStyle(new BasicStroke(1.2f));
}
 
Example 6
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeCumulativeTestValues() {
    /* 
     * Bins should be [0, 10], (10, 20], ..., (9990, 10000]
     * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)...
     * Each bin should have mass 10/10000 = .001
     */
    final double[] testPoints = getCumulativeTestPoints();
    final double[] cumValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        // Compute bMinus = sum or mass of bins below the bin containing the point
        // First bin has mass 11 / 10000, the rest have mass 10 / 10000.
        final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
        cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
    }
    return cumValues;
}
 
Example 7
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
 *
 * @since 3.1
 */
public double cumulativeProbability(double x) {
    if (x < min) {
        return 0d;
    } else if (x >= max) {
        return 1d;
    }
    final int binIndex = findBin(x);
    final double pBminus = pBminus(binIndex);
    final double pB = pB(binIndex);
    final double[] binBounds = getUpperBounds();
    final double kB = kB(binIndex);
    final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
    final RealDistribution kernel = k(x);
    final double withinBinCum =
        (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
    return pBminus + pB * withinBinCum;
}
 
Example 8
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
 *
 * @since 3.1
 */
public double cumulativeProbability(double x) {
    if (x < min) {
        return 0d;
    } else if (x >= max) {
        return 1d;
    }
    final int binIndex = findBin(x);
    final double pBminus = pBminus(binIndex);
    final double pB = pB(binIndex);
    final double[] binBounds = getUpperBounds();
    final double kB = kB(binIndex);
    final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
    final RealDistribution kernel = k(x);
    final double withinBinCum =
        (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
    return pBminus + pB * withinBinCum;
}
 
Example 9
Source File: RealDistributionComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void addCDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
    // generates Log data
    List<Number> xData = new ArrayList<Number>();
    List<Number> yData = new ArrayList<Number>();
    int samples = 100;
    double stepSize = (upperBound - lowerBound) / (double) samples;
    for (double x = lowerBound; x <= upperBound; x += stepSize) {
      double density = distribution.cumulativeProbability(x);
      if (! Double.isInfinite(density) && ! Double.isNaN(density)) {
          xData.add(x);
          yData.add(density);
      }
    }

    Series series = chart.addSeries(desc, xData, yData);
    series.setMarker(SeriesMarker.NONE);
    series.setLineStyle(new BasicStroke(1.2f));
}
 
Example 10
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeCumulativeTestValues() {
    /* 
     * Bins should be [0, 10], (10, 20], ..., (9990, 10000]
     * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)...
     * Each bin should have mass 10/10000 = .001
     */
    final double[] testPoints = getCumulativeTestPoints();
    final double[] cumValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        // Compute bMinus = sum or mass of bins below the bin containing the point
        // First bin has mass 11 / 10000, the rest have mass 10 / 10000.
        final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
        cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
    }
    return cumValues;
}
 
Example 11
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeDensityTestValues() {
    final double[] testPoints = getCumulativeTestPoints();
    final double[] densityValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double density = kernel.density(testPoints[i]);
        densityValues[i] = density * (bin == 0 ? firstBinMass : binMass) / withinBinKernelMass;   
    }
    return densityValues;
}
 
Example 12
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
 *
 * @since 3.1
 */
public double cumulativeProbability(double x) {
    if (x < min) {
        return 0d;
    } else if (x >= max) {
        return 1d;
    }
    final int binIndex = findBin(x);
    final double pBminus = pBminus(binIndex);
    final double pB = pB(binIndex);
    final double[] binBounds = getUpperBounds();
    final double kB = kB(binIndex);
    final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
    final RealDistribution kernel = k(x);
    final double withinBinCum =
        (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
    return pBminus + pB * withinBinCum;
}
 
Example 13
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 14
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Mass of bin i under the within-bin kernel of the bin.
 *
 * @param i index of the bin
 * @return the difference in the within-bin kernel cdf between the
 * upper and lower endpoints of bin i
 */
@SuppressWarnings("deprecation")
private double kB(int i) {
    final double[] binBounds = getUpperBounds();
    final RealDistribution kernel = getKernel(binStats.get(i));
    return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
        kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
}
 
Example 15
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 16
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Mass of bin i under the within-bin kernel of the bin.
 *
 * @param i index of the bin
 * @return the difference in the within-bin kernel cdf between the
 * upper and lower endpoints of bin i
 */
@SuppressWarnings("deprecation")
private double kB(int i) {
    final double[] binBounds = getUpperBounds();
    final RealDistribution kernel = getKernel(binStats.get(i));
    return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
        kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
}
 
Example 17
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Mass of bin i under the within-bin kernel of the bin.
 *
 * @param i index of the bin
 * @return the difference in the within-bin kernel cdf between the
 * upper and lower endpoints of bin i
 */
@SuppressWarnings("deprecation")
private double kB(int i) {
    final double[] binBounds = getUpperBounds();
    final RealDistribution kernel = getKernel(binStats.get(i));
    return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
        kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
}
 
Example 18
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Mass of bin i under the within-bin kernel of the bin.
 *
 * @param i index of the bin
 * @return the difference in the within-bin kernel cdf between the
 * upper and lower endpoints of bin i
 */
@SuppressWarnings("deprecation")
private double kB(int i) {
    final double[] binBounds = getUpperBounds();
    final RealDistribution kernel = getKernel(binStats.get(i));
    return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
        kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
}
 
Example 19
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 20
Source File: DistributionUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Cumulative distribution function at x
 * @param dis Distribution.
 * @param x X.
 * @return Cumulative distribution value.
 */
public static double cdf(RealDistribution dis, Number x){
    return dis.cumulativeProbability(x.doubleValue());
}