org.apache.commons.math3.stat.ranking.NaNStrategy Java Examples

The following examples show how to use org.apache.commons.math3.stat.ranking.NaNStrategy. 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: PercentileTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNanStrategySpecific() {
    double[] specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    Assert.assertTrue(Double.isNaN(new Percentile(50d).withEstimationType(Percentile.EstimationType.LEGACY).withNaNStrategy(NaNStrategy.MAXIMAL).evaluate(specialValues, 3, 3)));
    Assert.assertEquals(2d,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_1).withNaNStrategy(NaNStrategy.REMOVED).evaluate(specialValues),0d);
    Assert.assertEquals(Double.NaN,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_5).withNaNStrategy(NaNStrategy.REMOVED).evaluate(new double[] {Double.NaN,Double.NaN,Double.NaN}),0d);
    Assert.assertEquals(50d,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_7).withNaNStrategy(NaNStrategy.MINIMAL).evaluate(new double[] {50d,50d,50d},1,2),0d);
    
    specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN };
    Assert.assertEquals(3.5,new Percentile().evaluate(specialValues, 3, 4),0d);
    Assert.assertEquals(4d,new Percentile().evaluate(specialValues, 4, 3),0d);
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 2)));
    
    specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN, 5d, 6d };
    Assert.assertEquals(4.5,new Percentile().evaluate(specialValues, 3, 6),0d);
    Assert.assertEquals(5d,new Percentile().evaluate(specialValues, 4, 5),0d);
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 2)));
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 1)));
    Assert.assertEquals(5.5,new Percentile().evaluate(specialValues, 5, 4),0d);
}
 
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: PercentileTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNanStrategySpecific() {
    double[] specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    Assert.assertTrue(Double.isNaN(new Percentile(50d).withEstimationType(Percentile.EstimationType.LEGACY).withNaNStrategy(NaNStrategy.MAXIMAL).evaluate(specialValues, 3, 3)));
    Assert.assertEquals(2d,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_1).withNaNStrategy(NaNStrategy.REMOVED).evaluate(specialValues),0d);
    Assert.assertEquals(Double.NaN,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_5).withNaNStrategy(NaNStrategy.REMOVED).evaluate(new double[] {Double.NaN,Double.NaN,Double.NaN}),0d);
    Assert.assertEquals(50d,new Percentile(50d).withEstimationType(Percentile.EstimationType.R_7).withNaNStrategy(NaNStrategy.MINIMAL).evaluate(new double[] {50d,50d,50d},1,2),0d);
    
    specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN };
    Assert.assertEquals(3.5,new Percentile().evaluate(specialValues, 3, 4),0d);
    Assert.assertEquals(4d,new Percentile().evaluate(specialValues, 4, 3),0d);
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 2)));
    
    specialValues = new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN, 5d, 6d };
    Assert.assertEquals(4.5,new Percentile().evaluate(specialValues, 3, 6),0d);
    Assert.assertEquals(5d,new Percentile().evaluate(specialValues, 4, 5),0d);
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 2)));
    Assert.assertTrue(Double.isNaN(new Percentile().evaluate(specialValues, 5, 1)));
    Assert.assertEquals(5.5,new Percentile().evaluate(specialValues, 5, 4),0d);
}
 
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: 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 #8
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 #9
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 #10
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 #11
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReplaceNanInRange() {
    final double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN, 5d,
            7d, Double.NaN, 8d};
    Assert.assertEquals(/*Double.NaN*/3.5,new Percentile(50d).evaluate(specialValues),0d);
    reset (50, Percentile.EstimationType.R_1);
    Assert.assertEquals(3d, getUnivariateStatistic().evaluate(specialValues),0d);
    reset (50, Percentile.EstimationType.R_2);
    Assert.assertEquals(3.5d, getUnivariateStatistic().evaluate(specialValues),0d);
    Assert.assertEquals(Double.POSITIVE_INFINITY,new Percentile(70)
                                    .withNaNStrategy(NaNStrategy.MAXIMAL)
                                    .evaluate(specialValues),0d);
}
 
Example #12
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAllTechniquesSpecialValuesWithNaNStrategy() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    try {
        new Percentile(50d).withEstimationType(Percentile.EstimationType.LEGACY).withNaNStrategy(null);
        Assert.fail("Expecting NullArgumentArgumentException "
                + "for null Nan Strategy");
    } catch (NullArgumentException ex) {
        // expected
    }
    //This is as per each type's default NaNStrategy
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_6, 2.0 },
            { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 }}, 50d, 0d);

    //This is as per MAXIMAL and hence the values tend a +0.5 upward
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.5 }, { Percentile.EstimationType.R_3, 2.0 },
            { Percentile.EstimationType.R_4, 2.0 }, { Percentile.EstimationType.R_5, 2.5 }, { Percentile.EstimationType.R_6, 2.5 },
            { Percentile.EstimationType.R_7, 2.5 }, { Percentile.EstimationType.R_8, 2.5 }, { Percentile.EstimationType.R_9, 2.5 }}, 50d, 0d,
            NaNStrategy.MAXIMAL);

    //This is as per MINIMAL and hence the values tend a -0.5 downward
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 1.5d }, { Percentile.EstimationType.R_1, 1.0 }, { Percentile.EstimationType.R_2, 1.5 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.0 }, { Percentile.EstimationType.R_5, 1.5 }, { Percentile.EstimationType.R_6, 1.5 },
            { Percentile.EstimationType.R_7, 1.5 }, { Percentile.EstimationType.R_8, 1.5 }, { Percentile.EstimationType.R_9, 1.5 }}, 50d, 0d,
            NaNStrategy.MINIMAL);

    //This is as per REMOVED as here Percentile.Type.CM changed its value from default
    //while rest of Estimation types were anyways defaulted to REMOVED
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.0 }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_6, 2.0 },
            { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 }}, 50d, 0d,
            NaNStrategy.REMOVED);
}
 
Example #13
Source File: Percentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a Percentile with the specific quantile value,
 * {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
 *
 * @param quantile the quantile to be computed
 * @param estimationType one of the percentile {@link EstimationType  estimation types}
 * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
 * @param kthSelector a {@link KthSelector} to use for pivoting during search
 * @throws MathIllegalArgumentException if p is not within (0,100]
 * @throws NullArgumentException if type or NaNStrategy passed is null
 */
protected Percentile(final double quantile,
                     final EstimationType estimationType,
                     final NaNStrategy nanStrategy,
                     final KthSelector kthSelector)
    throws MathIllegalArgumentException {
    setQuantile(quantile);
    cachedPivots = null;
    MathUtils.checkNotNull(estimationType);
    MathUtils.checkNotNull(nanStrategy);
    MathUtils.checkNotNull(kthSelector);
    this.estimationType = estimationType;
    this.nanStrategy = nanStrategy;
    this.kthSelector = kthSelector;
}
 
Example #14
Source File: XDataFrameRank.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the rank array for the values specified
 * @param values    the values to rank
 * @return          the ranks of input array
 */
static double[] rank(double[] values) {
    final NaNStrategy nanStrategy = (NaNStrategy)optionsMap.get(NaNStrategy.class).get(DataFrameOptions.getNanStrategy());
    final TiesStrategy tieStrategy = (TiesStrategy)optionsMap.get(TiesStrategy.class).get(DataFrameOptions.getTieStrategy());
    if (nanStrategy == null) throw new DataFrameException("Unsupported NaN strategy specified: " + DataFrameOptions.getNanStrategy());
    if (tieStrategy == null) throw new DataFrameException("Unsupported tie strategy specified: " + DataFrameOptions.getTieStrategy());
    final NaturalRanking ranking = new NaturalRanking(nanStrategy, tieStrategy);
    return ranking.rank(values);
}
 
Example #15
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNanStrategyFailed() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    new Percentile(50d).
    withEstimationType(Percentile.EstimationType.R_9).
    withNaNStrategy(NaNStrategy.FAILED).
    evaluate(specialValues);
}
 
Example #16
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 #17
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 #18
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Simple test assertion utility method
 *
 * @param data input data
 * @param map of expected result against a {@link EstimationType}
 * @param p the quantile to compute for
 * @param tolerance the tolerance of difference allowed
 * @param nanStrategy NaNStrategy to be passed
 */
protected void testAssertMappedValues(double[] data, Object[][] map,
                                      Double p, Double tolerance, NaNStrategy nanStrategy) {
    for (Object[] o : map) {
        Percentile.EstimationType e = (Percentile.EstimationType) o[0];
        double expected = (Double) o[1];
        try {
            double result = new Percentile(p).withEstimationType(e).withNaNStrategy(nanStrategy).evaluate(data);
            Assert.assertEquals("expected[" + e + "] = " + expected + " but was = " + result,
                                expected, result, tolerance);
        } catch(Exception ex) {
            Assert.fail("Exception occured for estimation type " + e + ":" + ex.getLocalizedMessage());
        }
    }
}
 
Example #19
Source File: Percentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a Percentile with the specific quantile value,
 * {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
 *
 * @param quantile the quantile to be computed
 * @param estimationType one of the percentile {@link EstimationType  estimation types}
 * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
 * @param kthSelector a {@link KthSelector} to use for pivoting during search
 * @throws MathIllegalArgumentException if p is not within (0,100]
 * @throws NullArgumentException if type or NaNStrategy passed is null
 */
protected Percentile(final double quantile,
                     final EstimationType estimationType,
                     final NaNStrategy nanStrategy,
                     final KthSelector kthSelector)
    throws MathIllegalArgumentException {
    setQuantile(quantile);
    cachedPivots = null;
    MathUtils.checkNotNull(estimationType);
    MathUtils.checkNotNull(nanStrategy);
    MathUtils.checkNotNull(kthSelector);
    this.estimationType = estimationType;
    this.nanStrategy = nanStrategy;
    this.kthSelector = kthSelector;
}
 
Example #20
Source File: Percentile.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
@Override
public double getValue() {
    return new org.apache.commons.math3.stat.descriptive.rank.Percentile(nth * 100)
        .withEstimationType(org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationType.R_7)
        .withNaNStrategy(NaNStrategy.FIXED)
        .evaluate(values, 0, n);
}
 
Example #21
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 #22
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NotANumberException.class)
public void testNanStrategyFailed() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    new Percentile(50d).
    withEstimationType(Percentile.EstimationType.R_9).
    withNaNStrategy(NaNStrategy.FAILED).
    evaluate(specialValues);
}
 
Example #23
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 #24
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Simple test assertion utility method
 *
 * @param data input data
 * @param map of expected result against a {@link EstimationType}
 * @param p the quantile to compute for
 * @param tolerance the tolerance of difference allowed
 * @param nanStrategy NaNStrategy to be passed
 */
protected void testAssertMappedValues(double[] data, Object[][] map,
                                      Double p, Double tolerance, NaNStrategy nanStrategy) {
    for (Object[] o : map) {
        Percentile.EstimationType e = (Percentile.EstimationType) o[0];
        double expected = (Double) o[1];
        try {
            double result = new Percentile(p).withEstimationType(e).withNaNStrategy(nanStrategy).evaluate(data);
            Assert.assertEquals("expected[" + e + "] = " + expected + " but was = " + result,
                                expected, result, tolerance);
        } catch(Exception ex) {
            Assert.fail("Exception occured for estimation type " + e + ":" + ex.getLocalizedMessage());
        }
    }
}
 
Example #25
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAllTechniquesSpecialValuesWithNaNStrategy() {
    double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN };
    try {
        new Percentile(50d).withEstimationType(Percentile.EstimationType.LEGACY).withNaNStrategy(null);
        Assert.fail("Expecting NullArgumentArgumentException "
                + "for null Nan Strategy");
    } catch (NullArgumentException ex) {
        // expected
    }
    //This is as per each type's default NaNStrategy
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_6, 2.0 },
            { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 }}, 50d, 0d);

    //This is as per MAXIMAL and hence the values tend a +0.5 upward
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.5d }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.5 }, { Percentile.EstimationType.R_3, 2.0 },
            { Percentile.EstimationType.R_4, 2.0 }, { Percentile.EstimationType.R_5, 2.5 }, { Percentile.EstimationType.R_6, 2.5 },
            { Percentile.EstimationType.R_7, 2.5 }, { Percentile.EstimationType.R_8, 2.5 }, { Percentile.EstimationType.R_9, 2.5 }}, 50d, 0d,
            NaNStrategy.MAXIMAL);

    //This is as per MINIMAL and hence the values tend a -0.5 downward
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 1.5d }, { Percentile.EstimationType.R_1, 1.0 }, { Percentile.EstimationType.R_2, 1.5 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.0 }, { Percentile.EstimationType.R_5, 1.5 }, { Percentile.EstimationType.R_6, 1.5 },
            { Percentile.EstimationType.R_7, 1.5 }, { Percentile.EstimationType.R_8, 1.5 }, { Percentile.EstimationType.R_9, 1.5 }}, 50d, 0d,
            NaNStrategy.MINIMAL);

    //This is as per REMOVED as here Percentile.Type.CM changed its value from default
    //while rest of Estimation types were anyways defaulted to REMOVED
    testAssertMappedValues(specialValues, new Object[][] {
            { Percentile.EstimationType.LEGACY, 2.0 }, { Percentile.EstimationType.R_1, 2.0 }, { Percentile.EstimationType.R_2, 2.0 }, { Percentile.EstimationType.R_3, 1.0 },
            { Percentile.EstimationType.R_4, 1.5 }, { Percentile.EstimationType.R_5, 2.0 }, { Percentile.EstimationType.R_6, 2.0 },
            { Percentile.EstimationType.R_7, 2.0 }, { Percentile.EstimationType.R_8, 2.0 }, { Percentile.EstimationType.R_9, 2.0 }}, 50d, 0d,
            NaNStrategy.REMOVED);
}
 
Example #26
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 #27
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 #28
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReplaceNanInRange() {
    final double[] specialValues =
            new double[] { 0d, 1d, 2d, 3d, 4d, Double.NaN, Double.NaN, 5d,
            7d, Double.NaN, 8d};
    Assert.assertEquals(/*Double.NaN*/3.5,new Percentile(50d).evaluate(specialValues),0d);
    reset (50, Percentile.EstimationType.R_1);
    Assert.assertEquals(3d, getUnivariateStatistic().evaluate(specialValues),0d);
    reset (50, Percentile.EstimationType.R_2);
    Assert.assertEquals(3.5d, getUnivariateStatistic().evaluate(specialValues),0d);
    Assert.assertEquals(Double.POSITIVE_INFINITY,new Percentile(70)
                                    .withNaNStrategy(NaNStrategy.MAXIMAL)
                                    .evaluate(specialValues),0d);
}
 
Example #29
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPercentileCopy() {
   reset(50d, Percentile.EstimationType.LEGACY);
   final Percentile original = getUnivariateStatistic();
   final Percentile copy = new Percentile(original);
   Assert.assertEquals(original.getNaNStrategy(),copy.getNaNStrategy());
   Assert.assertEquals(original.getQuantile(), copy.getQuantile(),0d);
   Assert.assertEquals(original.getEstimationType(),copy.getEstimationType());
   Assert.assertEquals(NaNStrategy.FIXED, original.getNaNStrategy());
}
 
Example #30
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;
}