Java Code Examples for org.apache.commons.math3.stat.descriptive.DescriptiveStatistics#getPercentile()

The following examples show how to use org.apache.commons.math3.stat.descriptive.DescriptiveStatistics#getPercentile() . 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: StlDecomposition.java    From stl-java with Apache License 2.0 6 votes vote down vote up
/**
 * Computes robustness weights using bisquare weight function.
 *
 * @param remainder
 *  The remainder, series - trend - seasonal.
 * @return
 *  A new array containing the robustness weights.
 */
private double[] robustnessWeights(double[] remainder) {
  // Compute "h" = 6 median(|R_v|)
  double[] absRemainder = new double[remainder.length];
  for (int i = 0; i < remainder.length; i++) {
    absRemainder[i] = Math.abs(remainder[i]);
  }
  DescriptiveStatistics stats = new DescriptiveStatistics(absRemainder);
  double outlierThreshold = 6 * stats.getPercentile(50);

  // Compute robustness weights
  double[] robustness = new double[remainder.length];
  for (int i = 0; i < remainder.length; i++) {
    robustness[i] = biSquareWeight(absRemainder[i] / outlierThreshold);
  }

  return robustness;
}
 
Example 2
Source File: ExecuteOutlierErrors.java    From BART with MIT License 6 votes vote down vote up
private List<OutlierCellChange> generateOutliers(List<Cell> originalDistribution, double[] distribution, double percentage, EGTask task) {
    List<OutlierCellChange> outliers = new ArrayList<OutlierCellChange>();
    DescriptiveStatistics stats = new DescriptiveStatistics(distribution);
    if (task.getConfiguration().isDebug()) System.out.println(printStat(stats));

    double q1 = stats.getPercentile(25);
    double q3 = stats.getPercentile(75);
    double iqr = q3 - q1;
    // using Tukey’s Outlier rule
    // outlier if value < q1 - 1.5 * iqr or value > q3 + 1.5 * iqr
    double minRange = q1 - 1.5 * iqr;
    double maxRange = q3 + 1.5 * iqr;
    for (Cell cell : originalDistribution) {
        if (BartUtility.pickRandom(percentage)) {
            OutlierCellChange cellChange = buildCellChange(cell, task, minRange, maxRange, q1, q3);
            outliers.add(cellChange);
        }
    }
    return outliers;
}
 
Example 3
Source File: ExecuteOutlierErrors.java    From BART with MIT License 6 votes vote down vote up
private String printStat(DescriptiveStatistics stats) {
    double mean = stats.getMean();
    double std = stats.getStandardDeviation();
    double median = stats.getPercentile(50);
    double q1 = stats.getPercentile(25);
    double q3 = stats.getPercentile(75);
    double iqr = q3 - q1;
    double trimmedMean = (q1 + q3 + 2 * median) / 4;
    double skewness = stats.getSkewness();

    StringBuilder sb = new StringBuilder();
    sb.append(" *** Distribution Analysis ***").append("\n")
            .append("\tMean= ").append(mean).append("\n")
            .append("\tStd= ").append(std).append("\n")
            .append("\tMedian= ").append(median).append("\n")
            .append("\tQ1= ").append(q1).append("\tQ3=").append(q3).append("\tIQR=").append(iqr).append("\n")
            .append("\tTrimmed Mean= ").append(trimmedMean).append("\n")
            .append("\tSkewness= ").append(skewness).append("\n");
    return sb.toString();
}
 
Example 4
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 5
Source File: BuyZoneOptimizer.java    From bateman with MIT License 5 votes vote down vote up
public static double getMedianHighOpenSpread(String symbol, int days) throws Exception {
    YahooQuoteFetcher yahooFetcher = new YahooQuoteFetcher();
    String quoteStr = yahooFetcher.fetchQuotes(symbol, days, 60*60*24);
    List<Quote> dailyQuoteList = yahooFetcher.parseQuotes(quoteStr, 60*60*24);
    
    DescriptiveStatistics stats = new DescriptiveStatistics();
    
    for(Quote quote : dailyQuoteList) {
        stats.addValue(quote.getHigh().subtract(quote.getOpen()).doubleValue());
    }
    
    return stats.getPercentile(50);
}
 
Example 6
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 7
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 8
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 9
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 10
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 11
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 12
Source File: StableRandomGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If alpha = 1, than it must be Cauchy distribution
 */
@Test
public void testCauchyCase() {
    StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0);
    DescriptiveStatistics summary = new DescriptiveStatistics();

    for (int i = 0; i < sampleSize; ++i) {
        double sample = generator.nextNormalizedDouble();
        summary.addValue(sample);
    }

    // Standard Cauchy distribution should have zero median and mode
    double median = summary.getPercentile(50);
    Assert.assertEquals(0.0, median, 0.2);
}
 
Example 13
Source File: NoiseExp.java    From data-polygamy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public double getIQR(ArrayList<Float> arrayList) {
    double[] vals = new double[arrayList.size()];
    for (int i = 0; i < arrayList.size(); i++) {
        vals[i] = (double)arrayList.get(i);
    }
    DescriptiveStatistics ds = new DescriptiveStatistics(vals);
    double fq = ds.getPercentile(25);
    double tq = ds.getPercentile(75);
    return (tq - fq);
}
 
Example 14
Source File: Median.java    From data-polygamy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public float getResult() {
    if (count == 0)
        return Float.NaN;
    
    double[] primitiveValues = new double[floatValues.size()];
    for (int i = 0; i < floatValues.size(); i++)
        primitiveValues[i] = floatValues.get(i);
    DescriptiveStatistics stats = new DescriptiveStatistics(primitiveValues);
    
    return (float) stats.getPercentile(50);
}
 
Example 15
Source File: DescriptiveStats.java    From Java-Data-Science-Cookbook with MIT License 5 votes vote down vote up
public void getDescStats(double[] values){
	DescriptiveStatistics stats = new DescriptiveStatistics();
	for( int i = 0; i < values.length; i++) {
	        stats.addValue(values[i]);
	}
	double mean = stats.getMean();
	double std = stats.getStandardDeviation();
	double median = stats.getPercentile(50);
	System.out.println(mean + "\t" + std + "\t" + median);
}
 
Example 16
Source File: MathUtil.java    From kruize with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("SameParameterValue")
public static double getPercentile(DescriptiveStatistics descriptiveStatistics, double percentile)
{
    return descriptiveStatistics.getPercentile(percentile);
}
 
Example 17
Source File: RespTimeMedianTest.java    From lightning with MIT License 4 votes vote down vote up
@Override
protected int calculateNumericResult(DescriptiveStatistics ds) {
    return (int) ds.getPercentile(50);
}
 
Example 18
Source File: RespTimeNthPercentileTest.java    From lightning with MIT License 4 votes vote down vote up
@Override
protected int calculateNumericResult(DescriptiveStatistics ds) {
    ds.setPercentileImpl(new Percentile().withEstimationType(Percentile.EstimationType.R_3));
    return actualResult = (int) ds.getPercentile((double) percentile);
}
 
Example 19
Source File: MathUtil.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public static double median(double[] arr){
    DescriptiveStatistics stats = new DescriptiveStatistics(arr);
    return stats.getPercentile(50);
}
 
Example 20
Source File: VecUtils.java    From clust4j with Apache License 2.0 2 votes vote down vote up
/**
 * Compute the interquartile range in a vector
 * @param a
 * @throws IllegalArgumentException if the input vector is empty
 * @return the interquartile range
 */
public static double iqr(final double[] a) {
	checkDims(a);
	DescriptiveStatistics d = new DescriptiveStatistics(a);
	return d.getPercentile(75) - d.getPercentile(25);
}