Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#INSUFFICIENT_DIMENSION

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#INSUFFICIENT_DIMENSION . 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: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        double[] x = xArray;
        double[] y = yArray;
        if (rankingAlgorithm instanceof NaturalRanking &&
            NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
            final Set<Integer> nanPositions = new HashSet<Integer>();

            nanPositions.addAll(getNaNPositions(xArray));
            nanPositions.addAll(getNaNPositions(yArray));

            x = removeValues(xArray, nanPositions);
            y = removeValues(yArray, nanPositions);
        }
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y));
    }
}
 
Example 2
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the sum of the (signed) differences between corresponding elements of the
 * input arrays -- i.e., sum(sample1[i] - sample2[i]).
 *
 * @param sample1  the first array
 * @param sample2  the second array
 * @return sum of paired differences
 * @throws DimensionMismatchException if the arrays do not have the same
 * (positive) length.
 * @throws NoDataException if the sample arrays are empty.
 */
public static double sumDifference(final double[] sample1, final double[] sample2)
throws DimensionMismatchException, NoDataException {
    int n = sample1.length;
    if (n != sample2.length) {
        throw new DimensionMismatchException(n, sample2.length);
    }
    if (n <= 0) {
        throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION);
    }
    double result = 0;
    for (int i = 0; i < n; i++) {
        result += sample1[i] - sample2[i];
    }
    return result;
}
 
Example 3
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the sum of the (signed) differences between corresponding elements of the
 * input arrays -- i.e., sum(sample1[i] - sample2[i]).
 *
 * @param sample1  the first array
 * @param sample2  the second array
 * @return sum of paired differences
 * @throws DimensionMismatchException if the arrays do not have the same
 * (positive) length.
 * @throws NoDataException if the sample arrays are empty.
 */
public static double sumDifference(final double[] sample1, final double[] sample2)
throws DimensionMismatchException, NoDataException {
    int n = sample1.length;
    if (n != sample2.length) {
        throw new DimensionMismatchException(n, sample2.length);
    }
    if (n <= 0) {
        throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION);
    }
    double result = 0;
    for (int i = 0; i < n; i++) {
        result += sample1[i] - sample2[i];
    }
    return result;
}
 
Example 4
Source File: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        double[] x = xArray;
        double[] y = yArray;
        if (rankingAlgorithm instanceof NaturalRanking &&
            NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
            final Set<Integer> nanPositions = new HashSet<Integer>();

            nanPositions.addAll(getNaNPositions(xArray));
            nanPositions.addAll(getNaNPositions(yArray));

            x = removeValues(xArray, nanPositions);
            y = removeValues(yArray, nanPositions);
        }
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y));
    }
}
 
Example 5
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the sum of the (signed) differences between corresponding elements of the
 * input arrays -- i.e., sum(sample1[i] - sample2[i]).
 *
 * @param sample1  the first array
 * @param sample2  the second array
 * @return sum of paired differences
 * @throws DimensionMismatchException if the arrays do not have the same
 * (positive) length.
 * @throws NoDataException if the sample arrays are empty.
 */
public static double sumDifference(final double[] sample1, final double[] sample2)
throws DimensionMismatchException, NoDataException {
    int n = sample1.length;
    if (n != sample2.length) {
        throw new DimensionMismatchException(n, sample2.length);
    }
    if (n <= 0) {
        throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION);
    }
    double result = 0;
    for (int i = 0; i < n; i++) {
        result += sample1[i] - sample2[i];
    }
    return result;
}
 
Example 6
Source File: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        double[] x = xArray;
        double[] y = yArray;
        if (rankingAlgorithm instanceof NaturalRanking &&
            NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) {
            final Set<Integer> nanPositions = new HashSet<Integer>();

            nanPositions.addAll(getNaNPositions(xArray));
            nanPositions.addAll(getNaNPositions(yArray));

            x = removeValues(xArray, nanPositions);
            y = removeValues(yArray, nanPositions);
        }
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y));
    }
}
 
Example 7
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between two arrays.
 *
 * <p>Throws MathIllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2.  Returns {@code NaN} if either of the arrays
 * has zero variance (i.e., if one of the arrays does not contain at least two distinct
 * values).</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example 8
Source File: StorelessBivariateCovariance.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the current covariance estimate.
 *
 * @return the current covariance
 * @throws NumberIsTooSmallException if the number of observations
 * is &lt; 2
 */
public double getResult() throws NumberIsTooSmallException {
    if (n < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                            n, 2, true);
    }
    if (biasCorrected) {
        return covarianceNumerator / (n - 1d);
    } else {
        return covarianceNumerator / n;
    }
}
 
Example 9
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example 10
Source File: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(xArray),
                rankingAlgorithm.rank(yArray));
    }
}
 
Example 11
Source File: StorelessBivariateCovariance.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the current covariance estimate.
 *
 * @return the current covariance
 * @throws NumberIsTooSmallException if the number of observations
 * is &lt; 2
 */
public double getResult() throws NumberIsTooSmallException {
    if (n < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                            n, 2, true);
    }
    if (biasCorrected) {
        return covarianceNumerator / (n - 1d);
    } else {
        return covarianceNumerator / n;
    }
}
 
Example 12
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example 13
Source File: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(xArray),
                rankingAlgorithm.rank(yArray));
    }
}
 
Example 14
Source File: StatUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the sum of the (signed) differences between corresponding elements of the
 * input arrays -- i.e., sum(sample1[i] - sample2[i]).
 *
 * @param sample1  the first array
 * @param sample2  the second array
 * @return sum of paired differences
 * @throws DimensionMismatchException if the arrays do not have the same
 * (positive) length.
 * @throws NoDataException if the sample arrays are empty.
 */
public static double sumDifference(final double[] sample1, final double[] sample2) {
    int n = sample1.length;
    if (n != sample2.length) {
        throw new DimensionMismatchException(n, sample2.length);
    }
    if (n <= 0) {
        throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION);
    }
    double result = 0;
    for (int i = 0; i < n; i++) {
        result += sample1[i] - sample2[i];
    }
    return result;
}
 
Example 15
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example 16
Source File: StorelessBivariateCovariance.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the current covariance estimate.
 *
 * @return the current covariance
 * @throws NumberIsTooSmallException if the number of observations
 * is &lt; 2
 */
public double getResult() throws NumberIsTooSmallException {
    if (n < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                            n, 2, true);
    }
    if (biasCorrected) {
        return covarianceNumerator / (n - 1d);
    } else {
        return covarianceNumerator / n;
    }
}
 
Example 17
Source File: StatUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the sum of the (signed) differences between corresponding elements of the
 * input arrays -- i.e., sum(sample1[i] - sample2[i]).
 *
 * @param sample1  the first array
 * @param sample2  the second array
 * @return sum of paired differences
 * @throws DimensionMismatchException if the arrays do not have the same
 * (positive) length.
 * @throws NoDataException if the sample arrays are empty.
 */
public static double sumDifference(final double[] sample1, final double[] sample2) {
    int n = sample1.length;
    if (n != sample2.length) {
        throw new DimensionMismatchException(n, sample2.length);
    }
    if (n <= 0) {
        throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION);
    }
    double result = 0;
    for (int i = 0; i < n; i++) {
        result += sample1[i] - sample2[i];
    }
    return result;
}
 
Example 18
Source File: SpearmansCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Spearman's rank correlation coefficient between the two arrays.
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Spearman's rank correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if the array length is less than 2
 */
public double correlation(final double[] xArray, final double[] yArray) {
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(xArray),
                rankingAlgorithm.rank(yArray));
    }
}
 
Example 19
Source File: StorelessBivariateCovariance.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the current covariance estimate.
 *
 * @return the current covariance
 * @throws NumberIsTooSmallException if the number of observations
 * is &lt; 2
 */
public double getResult() throws NumberIsTooSmallException {
    if (n < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                            n, 2, true);
    }
    if (biasCorrected) {
        return covarianceNumerator / (n - 1d);
    } else {
        return covarianceNumerator / n;
    }
}
 
Example 20
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between two arrays.
 *
 * <p>Throws MathIllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2.  Returns {@code NaN} if either of the arrays
 * has zero variance (i.e., if one of the arrays does not contain at least two distinct
 * values).</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}