Java Code Examples for org.apache.commons.math3.stat.ranking.NaNStrategy#REMOVED

The following examples show how to use org.apache.commons.math3.stat.ranking.NaNStrategy#REMOVED . 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: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 3
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 4
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 5
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
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: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 8
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 9
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath891Array() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
    
    Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
}
 
Example 10
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath891Array() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
    
    Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
}
 
Example 11
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath891Array() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
    
    Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
}
 
Example 12
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Before method to ensure defaults retained
 */
@Before
public void before() {
    quantile         = 95.0;
    type             = Percentile.EstimationType.LEGACY;
    nanStrategy      = NaNStrategy.REMOVED;
    kthSelector      = new KthSelector(new MedianOf3PivotingStrategy());
}
 
Example 13
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Before method to ensure defaults retained
 */
@Before
public void before() {
    quantile         = 95.0;
    type             = Percentile.EstimationType.LEGACY;
    nanStrategy      = NaNStrategy.REMOVED;
    kthSelector      = new KthSelector(new MedianOf3PivotingStrategy());
}
 
Example 14
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testMath891Array() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(ranking);
    
    Assert.assertEquals(0.5, spearman.correlation(xArray, yArray), Double.MIN_VALUE);
}
 
Example 15
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void reset(final double p, final Percentile.EstimationType type) {
    this.quantile = p;
    this.type     = type;
    nanStrategy   = (type == Percentile.EstimationType.LEGACY) ? NaNStrategy.FIXED : NaNStrategy.REMOVED;
}
 
Example 16
Source File: MedianTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Median getTestMedian(EstimationType type) {
    NaNStrategy strategy = (type == LEGACY) ? NaNStrategy.FIXED : NaNStrategy.REMOVED;
    return new Median().withEstimationType(type).withNaNStrategy(strategy);
}
 
Example 17
Source File: MedianTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Median getTestMedian(EstimationType type) {
    NaNStrategy strategy = (type == LEGACY) ? NaNStrategy.FIXED : NaNStrategy.REMOVED;
    return new Median().withEstimationType(type).withNaNStrategy(strategy);
}
 
Example 18
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void reset(final double p, final Percentile.EstimationType type) {
    this.quantile = p;
    this.type     = type;
    nanStrategy   = (type == Percentile.EstimationType.LEGACY) ? NaNStrategy.FIXED : NaNStrategy.REMOVED;
}
 
Example 19
Source File: Percentile.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a Percentile with the specific quantile value and the following
 * <ul>
 *   <li>default method type: {@link EstimationType#LEGACY}</li>
 *   <li>default NaN strategy: {@link NaNStrategy#REMOVED}</li>
 *   <li>a Kth Selector : {@link KthSelector}</li>
 * </ul>
 * @param quantile the quantile
 * @throws MathIllegalArgumentException  if p is not greater than 0 and less
 * than or equal to 100
 */
public Percentile(final double quantile) throws MathIllegalArgumentException {
    this(quantile, EstimationType.LEGACY, NaNStrategy.REMOVED,
         new KthSelector(new MedianOf3PivotingStrategy()));
}
 
Example 20
Source File: Percentile.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a Percentile with the specific quantile value and the following
 * <ul>
 *   <li>default method type: {@link EstimationType#LEGACY}</li>
 *   <li>default NaN strategy: {@link NaNStrategy#REMOVED}</li>
 *   <li>a Kth Selector : {@link KthSelector}</li>
 * </ul>
 * @param quantile the quantile
 * @throws MathIllegalArgumentException  if p is not greater than 0 and less
 * than or equal to 100
 */
public Percentile(final double quantile) throws MathIllegalArgumentException {
    this(quantile, EstimationType.LEGACY, NaNStrategy.REMOVED,
         new KthSelector(new MedianOf3PivotingStrategy()));
}