Example usage for org.apache.commons.math3.distribution EnumeratedRealDistribution sample

List of usage examples for org.apache.commons.math3.distribution EnumeratedRealDistribution sample

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution EnumeratedRealDistribution sample.

Prototype

public double[] sample(int sampleSize) 

Source Link

Document

The default implementation generates the sample by calling #sample() in a loop.

Usage

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/**
 * Estimates the <i>p-value</i> of a two-sample
 * <a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
 * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same
 * probability distribution. This method estimates the p-value by repeatedly sampling sets of size
 * {@code x.length} and {@code y.length} from the empirical distribution of the combined sample.
 * When {@code strict} is true, this is equivalent to the algorithm implemented in the R function
 * {@code ks.boot}, described in <pre>
 * Jasjeet S. Sekhon. 2011. 'Multivariate and Propensity Score Matching
 * Software with Automated Balance Optimization: The Matching package for R.'
 * Journal of Statistical Software, 42(7): 1-52.
 * </pre>/*from   w  ww  .  ja v  a2  s.c  o  m*/
 * @param x first sample
 * @param y second sample
 * @param iterations number of bootstrap resampling iterations
 * @param strict whether or not the null hypothesis is expressed as a strict inequality
 * @return estimated p-value
 */
public double bootstrap(double[] x, double[] y, int iterations, boolean strict) {
    final int xLength = x.length;
    final int yLength = y.length;
    final double[] combined = new double[xLength + yLength];
    System.arraycopy(x, 0, combined, 0, xLength);
    System.arraycopy(y, 0, combined, xLength, yLength);
    final EnumeratedRealDistribution dist = new EnumeratedRealDistribution(rng, combined);
    final long d = integralKolmogorovSmirnovStatistic(x, y);
    int greaterCount = 0;
    int equalCount = 0;
    double[] curX;
    double[] curY;
    long curD;
    for (int i = 0; i < iterations; i++) {
        curX = dist.sample(xLength);
        curY = dist.sample(yLength);
        curD = integralKolmogorovSmirnovStatistic(curX, curY);
        if (curD > d) {
            greaterCount++;
        } else if (curD == d) {
            equalCount++;
        }
    }
    return strict ? greaterCount / (double) iterations : (greaterCount + equalCount) / (double) iterations;
}