本文整理汇总了Java中org.apache.commons.math3.stat.ranking.NaNStrategy类的典型用法代码示例。如果您正苦于以下问题:Java NaNStrategy类的具体用法?Java NaNStrategy怎么用?Java NaNStrategy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NaNStrategy类属于org.apache.commons.math3.stat.ranking包,在下文中一共展示了NaNStrategy类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: correlation
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* 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));
}
}
示例2: testNanStrategySpecific
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例3: testMath891Matrix
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例4: getPercentile
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* Get the percentile value of the data
*
* @param percentile
* The percentile
* @return the percentile value
*/
public float getPercentile(double percentile)
{
// Check the input
if (percentile <= 0)
percentile = Double.MIN_NORMAL;
if (percentile > 100)
percentile = 100;
// The data should not have NaN so we ignore them for speed.
final Percentile p = new Percentile(percentile).withNaNStrategy(NaNStrategy.FIXED);
final int size = width * height;
final double[] values = new double[size];
for (int i = 0; i < size; i++)
values[i] = data[i];
return (float) p.evaluate(values);
}
示例5: getValue
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例6: rank
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* 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);
}
示例7: Percentile
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* 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;
}
示例8: before
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* 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());
}
示例9: testReplaceNanInRange
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例10: testPercentileCopy
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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());
}
示例11: testNanStrategyFailed
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例12: testAllTechniquesSpecialValuesWithNaNStrategy
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}
示例13: testAssertMappedValues
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
/**
* 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());
}
}
}
示例14: testMath891Array
import org.apache.commons.math3.stat.ranking.NaNStrategy; //导入依赖的package包/类
@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);
}