Java Code Examples for org.apache.commons.math3.util.CombinatoricsUtils#combinationsIterator()

The following examples show how to use org.apache.commons.math3.util.CombinatoricsUtils#combinationsIterator() . 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 5 votes vote down vote up
/**
 * Computes \(P(D_{n,m} > d)\) if {@code strict} is {@code true}; otherwise \(P(D_{n,m} \ge
 * d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic. See
 * {@link #kolmogorovSmirnovStatistic(double[], double[])} for the definition of \(D_{n,m}\).
 * <p>
 * The returned probability is exact, obtained by enumerating all partitions of {@code m + n}
 * into {@code m} and {@code n} sets, computing \(D_{n,m}\) for each partition and counting the
 * number of partitions that yield \(D_{n,m}\) values exceeding (resp. greater than or equal to)
 * {@code d}.
 * </p>
 * <p>
 * <strong>USAGE NOTE</strong>: Since this method enumerates all combinations in \({m+n} \choose
 * {n}\), it is very slow if called for large {@code m, n}. For this reason,
 * {@link #kolmogorovSmirnovTest(double[], double[])} uses this only for {@code m * n < }
 * {@value #SMALL_SAMPLE_PRODUCT}.
 * </p>
 *
 * @param d D-statistic value
 * @param n first sample size
 * @param m second sample size
 * @param strict whether or not the probability to compute is expressed as a strict inequality
 * @return probability that a randomly selected m-n partition of m + n generates \(D_{n,m}\)
 *         greater than (resp. greater than or equal to) {@code d}
 */
public double exactP(double d, int n, int m, boolean strict) {
    Iterator<int[]> combinationsIterator = CombinatoricsUtils.combinationsIterator(n + m, n);
    long tail = 0;
    final double[] nSet = new double[n];
    final double[] mSet = new double[m];
    while (combinationsIterator.hasNext()) {
        // Generate an n-set
        final int[] nSetI = combinationsIterator.next();
        // Copy the n-set to nSet and its complement to mSet
        int j = 0;
        int k = 0;
        for (int i = 0; i < n + m; i++) {
            if (j < n && nSetI[j] == i) {
                nSet[j++] = i;
            } else {
                mSet[k++] = i;
            }
        }
        final double curD = kolmogorovSmirnovStatistic(nSet, mSet);
        if (curD > d) {
            tail++;
        } else if (curD == d && !strict) {
            tail++;
        }
    }
    return (double) tail / (double) CombinatoricsUtils.binomialCoefficient(n + m, n);
}
 
Example 2
Source File: KolmogorovSmirnovTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes \(P(D_{n,m} > d)\) if {@code strict} is {@code true}; otherwise \(P(D_{n,m} \ge
 * d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic. See
 * {@link #kolmogorovSmirnovStatistic(double[], double[])} for the definition of \(D_{n,m}\).
 * <p>
 * The returned probability is exact, obtained by enumerating all partitions of {@code m + n}
 * into {@code m} and {@code n} sets, computing \(D_{n,m}\) for each partition and counting the
 * number of partitions that yield \(D_{n,m}\) values exceeding (resp. greater than or equal to)
 * {@code d}.
 * </p>
 * <p>
 * <strong>USAGE NOTE</strong>: Since this method enumerates all combinations in \({m+n} \choose
 * {n}\), it is very slow if called for large {@code m, n}. For this reason,
 * {@link #kolmogorovSmirnovTest(double[], double[])} uses this only for {@code m * n < }
 * {@value #SMALL_SAMPLE_PRODUCT}.
 * </p>
 *
 * @param d D-statistic value
 * @param n first sample size
 * @param m second sample size
 * @param strict whether or not the probability to compute is expressed as a strict inequality
 * @return probability that a randomly selected m-n partition of m + n generates \(D_{n,m}\)
 *         greater than (resp. greater than or equal to) {@code d}
 */
public double exactP(double d, int n, int m, boolean strict) {
    Iterator<int[]> combinationsIterator = CombinatoricsUtils.combinationsIterator(n + m, n);
    long tail = 0;
    final double[] nSet = new double[n];
    final double[] mSet = new double[m];
    while (combinationsIterator.hasNext()) {
        // Generate an n-set
        final int[] nSetI = combinationsIterator.next();
        // Copy the n-set to nSet and its complement to mSet
        int j = 0;
        int k = 0;
        for (int i = 0; i < n + m; i++) {
            if (j < n && nSetI[j] == i) {
                nSet[j++] = i;
            } else {
                mSet[k++] = i;
            }
        }
        final double curD = kolmogorovSmirnovStatistic(nSet, mSet);
        if (curD > d) {
            tail++;
        } else if (curD == d && !strict) {
            tail++;
        }
    }
    return (double) tail / (double) CombinatoricsUtils.binomialCoefficient(n + m, n);
}
 
Example 3
Source File: ApacheCommonsCombinationGenerator.java    From tutorials with MIT License 5 votes vote down vote up
/** 
 * Print all combinations of r elements from a set
 * @param n - number of elements in set
 * @param r - number of elements in selection
 */
public static void generate(int n, int r) {
    Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(n, r);
    while (iterator.hasNext()) {
        final int[] combination = iterator.next();
        System.out.println(Arrays.toString(combination));
    }
}