Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#OBSERVED_COUNTS_ALL_ZERO

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#OBSERVED_COUNTS_ALL_ZERO . 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: ChiSquareTestImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param observed1 array of observed frequency counts of the first data set
 * @param observed2 array of observed frequency counts of the second data set
 * @return chi-square test statistic
 * @throws MathIllegalArgumentException if preconditions are not met
 * @since 1.2
 */
public double chiSquareDataSetsComparison(long[] observed1, long[] observed2) {
    // Make sure lengths are same
    if (observed1.length < 2) {
        throw new DimensionMismatchException(observed1.length, 2);
    }
    if (observed1.length != observed2.length) {
        throw new DimensionMismatchException(observed1.length, observed2.length);
    }

    // Ensure non-negative counts
    checkNonNegative(observed1);
    checkNonNegative(observed2);

    // Compute and compare count sums
    long countSum1 = 0;
    long countSum2 = 0;
    boolean unequalCounts = false;
    double weight = 0.0;
    for (int i = 0; i < observed1.length; i++) {
        countSum1 += observed1[i];
        countSum2 += observed2[i];
    }
    // Ensure neither sample is uniformly 0
    if (countSum1 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 1);
    }
    if (countSum2 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 2);
    }
    // Compare and compute weight only if different
    unequalCounts = countSum1 != countSum2;
    if (unequalCounts) {
        weight = FastMath.sqrt((double) countSum1 / (double) countSum2);
    }
    // Compute ChiSquare statistic
    double sumSq = 0.0d;
    double dev = 0.0d;
    double obs1 = 0.0d;
    double obs2 = 0.0d;
    for (int i = 0; i < observed1.length; i++) {
        if (observed1[i] == 0 && observed2[i] == 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i);
        } else {
            obs1 = observed1[i];
            obs2 = observed2[i];
            if (unequalCounts) { // apply weights
                dev = obs1/weight - obs2 * weight;
            } else {
                dev = obs1 - obs2;
            }
            sumSq += (dev * dev) / (obs1 + obs2);
        }
    }
    return sumSq;
}
 
Example 2
Source File: ChiSquareTestImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param observed1 array of observed frequency counts of the first data set
 * @param observed2 array of observed frequency counts of the second data set
 * @return chi-square test statistic
 * @throws MathIllegalArgumentException if preconditions are not met
 * @since 1.2
 */
public double chiSquareDataSetsComparison(long[] observed1, long[] observed2) {
    // Make sure lengths are same
    if (observed1.length < 2) {
        throw new DimensionMismatchException(observed1.length, 2);
    }
    if (observed1.length != observed2.length) {
        throw new DimensionMismatchException(observed1.length, observed2.length);
    }

    // Ensure non-negative counts
    checkNonNegative(observed1);
    checkNonNegative(observed2);

    // Compute and compare count sums
    long countSum1 = 0;
    long countSum2 = 0;
    boolean unequalCounts = false;
    double weight = 0.0;
    for (int i = 0; i < observed1.length; i++) {
        countSum1 += observed1[i];
        countSum2 += observed2[i];
    }
    // Ensure neither sample is uniformly 0
    if (countSum1 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 1);
    }
    if (countSum2 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 2);
    }
    // Compare and compute weight only if different
    unequalCounts = countSum1 != countSum2;
    if (unequalCounts) {
        weight = FastMath.sqrt((double) countSum1 / (double) countSum2);
    }
    // Compute ChiSquare statistic
    double sumSq = 0.0d;
    double dev = 0.0d;
    double obs1 = 0.0d;
    double obs2 = 0.0d;
    for (int i = 0; i < observed1.length; i++) {
        if (observed1[i] == 0 && observed2[i] == 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i);
        } else {
            obs1 = observed1[i];
            obs2 = observed2[i];
            if (unequalCounts) { // apply weights
                dev = obs1/weight - obs2 * weight;
            } else {
                dev = obs1 - obs2;
            }
            sumSq += (dev * dev) / (obs1 + obs2);
        }
    }
    return sumSq;
}
 
Example 3
Source File: ChiSquareTestImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param observed1 array of observed frequency counts of the first data set
 * @param observed2 array of observed frequency counts of the second data set
 * @return chi-square test statistic
 * @throws MathIllegalArgumentException if preconditions are not met
 * @since 1.2
 */
public double chiSquareDataSetsComparison(long[] observed1, long[] observed2) {
    // Make sure lengths are same
    if (observed1.length < 2) {
        throw new DimensionMismatchException(observed1.length, 2);
    }
    if (observed1.length != observed2.length) {
        throw new DimensionMismatchException(observed1.length, observed2.length);
    }

    // Ensure non-negative counts
    checkNonNegative(observed1);
    checkNonNegative(observed2);

    // Compute and compare count sums
    long countSum1 = 0;
    long countSum2 = 0;
    boolean unequalCounts = false;
    double weight = 0.0;
    for (int i = 0; i < observed1.length; i++) {
        countSum1 += observed1[i];
        countSum2 += observed2[i];
    }
    // Ensure neither sample is uniformly 0
    if (countSum1 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 1);
    }
    if (countSum2 == 0) {
        throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_ALL_ZERO, 2);
    }
    // Compare and compute weight only if different
    unequalCounts = countSum1 != countSum2;
    if (unequalCounts) {
        weight = FastMath.sqrt((double) countSum1 / (double) countSum2);
    }
    // Compute ChiSquare statistic
    double sumSq = 0.0d;
    double dev = 0.0d;
    double obs1 = 0.0d;
    double obs2 = 0.0d;
    for (int i = 0; i < observed1.length; i++) {
        if (observed1[i] == 0 && observed2[i] == 0) {
            throw new MathIllegalArgumentException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i);
        } else {
            obs1 = observed1[i];
            obs2 = observed2[i];
            if (unequalCounts) { // apply weights
                dev = obs1/weight - obs2 * weight;
            } else {
                dev = obs1 - obs2;
            }
            sumSq += (dev * dev) / (obs1 + obs2);
        }
    }
    return sumSq;
}