Example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getKurtosis

List of usage examples for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getKurtosis

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getKurtosis.

Prototype

public double getKurtosis() 

Source Link

Document

Returns the Kurtosis of the available values.

Usage

From source file:com.itemanalysis.jmetrik.stats.descriptives.DescriptiveAnalysis.java

public void publishTable(VariableAttributes v) {
    TextTable table = null;// w w  w .j  a va2 s  . c o  m
    TextTableColumnFormat[] cformats = new TextTableColumnFormat[2];
    cformats[0] = new TextTableColumnFormat();
    cformats[0].setStringFormat(15, TextTableColumnFormat.OutputAlignment.LEFT);
    cformats[1] = new TextTableColumnFormat();
    cformats[1].setDoubleFormat(10, 4, TextTableColumnFormat.OutputAlignment.RIGHT);

    DescriptiveStatistics temp = data.get(v);
    table = new TextTable();
    table.addAllColumnFormats(cformats, 17);
    table.getRowAt(0).addHeader(0, 2, v.getName().toString(), TextTablePosition.CENTER);
    table.getRowAt(1).addHorizontalRule(0, 2, "=");
    table.getRowAt(2).addHeader(0, 1, "Statistic", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(1, 1, "Value", TextTablePosition.CENTER);
    table.getRowAt(3).addHorizontalRule(0, 2, "-");

    table.addStringAt(4, 0, "N");
    table.addDoubleAt(4, 1, maxProgress);
    table.addStringAt(5, 0, "Valid N");
    table.addDoubleAt(5, 1, temp.getN());
    table.addStringAt(6, 0, "Min");
    table.addDoubleAt(6, 1, temp.getMin());
    table.addStringAt(7, 0, "Max");
    table.addDoubleAt(7, 1, temp.getMax());
    table.addStringAt(8, 0, "Mean");
    table.addDoubleAt(8, 1, temp.getMean());
    table.addStringAt(9, 0, "Std. Dev.");
    table.addDoubleAt(9, 1, temp.getStandardDeviation());
    table.addStringAt(10, 0, "Skewness");
    table.addDoubleAt(10, 1, temp.getSkewness());
    table.addStringAt(11, 0, "Kurtosis");
    table.addDoubleAt(11, 1, temp.getKurtosis());
    table.addStringAt(12, 0, "First Quartile");
    table.addDoubleAt(12, 1, temp.getPercentile(25));
    table.addStringAt(13, 0, "Median");
    table.addDoubleAt(13, 1, temp.getPercentile(50));
    table.addStringAt(14, 0, "Third Quartile");
    table.addDoubleAt(14, 1, temp.getPercentile(75));
    table.addStringAt(15, 0, "IQR");
    table.addDoubleAt(15, 1, temp.getPercentile(75) - temp.getPercentile(25));
    table.getRowAt(16).addHorizontalRule(0, 2, "=");

    publish(table.toString() + "\n");

}

From source file:info.financialecology.finance.utilities.datastruct.VersatileTimeSeries.java

/**
 * Computes the unbiased excess kurtosis of the data points in this time series.
 * /*from w w w.j  a v a 2s  .co  m*/
 * @return the unbiased excess kurtosis of this time series
 * @see <a href=http://tinyurl.com/d4cajfw>Unbiased excess kurtosis</a>
 */
public double operatorUnbiasedExcessKurtosis() {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    for (int i = 0; i < this.getItemCount(); i++)
        stats.addValue(getValue(i).doubleValue());

    return stats.getKurtosis();
}

From source file:org.apache.metron.common.math.stats.OnlineStatisticsProviderTest.java

public static void validateStatisticsProvider(StatisticsProvider statsProvider, SummaryStatistics summaryStats,
        DescriptiveStatistics stats) {
    //N/*from  w w  w .ja v  a2  s  .c  om*/
    Assert.assertEquals(statsProvider.getCount(), stats.getN());
    //sum
    Assert.assertEquals(statsProvider.getSum(), stats.getSum(), 1e-3);
    //sum of squares
    Assert.assertEquals(statsProvider.getSumSquares(), stats.getSumsq(), 1e-3);
    //sum of squares
    Assert.assertEquals(statsProvider.getSumLogs(), summaryStats.getSumOfLogs(), 1e-3);
    //Mean
    Assert.assertEquals(statsProvider.getMean(), stats.getMean(), 1e-3);
    //Quadratic Mean
    Assert.assertEquals(statsProvider.getQuadraticMean(), summaryStats.getQuadraticMean(), 1e-3);
    //SD
    Assert.assertEquals(statsProvider.getStandardDeviation(), stats.getStandardDeviation(), 1e-3);
    //Variance
    Assert.assertEquals(statsProvider.getVariance(), stats.getVariance(), 1e-3);
    //Min
    Assert.assertEquals(statsProvider.getMin(), stats.getMin(), 1e-3);
    //Max
    Assert.assertEquals(statsProvider.getMax(), stats.getMax(), 1e-3);

    //Kurtosis
    Assert.assertEquals(stats.getKurtosis(), statsProvider.getKurtosis(), 1e-3);

    //Skewness
    Assert.assertEquals(stats.getSkewness(), statsProvider.getSkewness(), 1e-3);
    for (double d = 10.0; d < 100.0; d += 10) {
        //This is a sketch, so we're a bit more forgiving here in our choice of \epsilon.
        Assert.assertEquals("Percentile mismatch for " + d + "th %ile", statsProvider.getPercentile(d),
                stats.getPercentile(d), 1e-2);
    }
}

From source file:org.apache.solr.client.solrj.io.eval.DescribeEvaluator.java

@Override
public Object doWork(Object value) throws IOException {

    if (!(value instanceof List<?>)) {
        throw new IOException(
                String.format(Locale.ROOT, "Invalid expression %s - expecting a numeric list but found %s",
                        toExpression(constructingFactory), value.getClass().getSimpleName()));
    }/*  ww  w  .  j  a v  a 2  s  .  c  o m*/

    // we know each value is a BigDecimal or a list of BigDecimals
    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
    ((List<?>) value).stream().mapToDouble(innerValue -> ((BigDecimal) innerValue).doubleValue())
            .forEach(innerValue -> descriptiveStatistics.addValue(innerValue));

    Map<String, Number> map = new HashMap<>();
    map.put("max", descriptiveStatistics.getMax());
    map.put("mean", descriptiveStatistics.getMean());
    map.put("min", descriptiveStatistics.getMin());
    map.put("stdev", descriptiveStatistics.getStandardDeviation());
    map.put("sum", descriptiveStatistics.getSum());
    map.put("N", descriptiveStatistics.getN());
    map.put("var", descriptiveStatistics.getVariance());
    map.put("kurtosis", descriptiveStatistics.getKurtosis());
    map.put("skewness", descriptiveStatistics.getSkewness());
    map.put("popVar", descriptiveStatistics.getPopulationVariance());
    map.put("geometricMean", descriptiveStatistics.getGeometricMean());
    map.put("sumsq", descriptiveStatistics.getSumsq());

    return new Tuple(map);
}

From source file:org.deidentifier.arx.aggregates.StatisticsBuilder.java

/**
 * Returns summary statistics for all attributes.
 * /*from  w  w w . j av a  2  s  . co m*/
 * @param listwiseDeletion A flag enabling list-wise deletion
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Map<String, StatisticsSummary<?>> getSummaryStatistics(boolean listwiseDeletion) {

    // Reset stop flag
    interrupt.value = false;

    Map<String, DescriptiveStatistics> statistics = new HashMap<String, DescriptiveStatistics>();
    Map<String, StatisticsSummaryOrdinal> ordinal = new HashMap<String, StatisticsSummaryOrdinal>();
    Map<String, DataScale> scales = new HashMap<String, DataScale>();
    Map<String, GeometricMean> geomean = new HashMap<String, GeometricMean>();

    // Detect scales
    for (int col = 0; col < handle.getNumColumns(); col++) {

        // Meta
        String attribute = handle.getAttributeName(col);
        DataType<?> type = handle.getDataType(attribute);

        // Scale
        DataScale scale = type.getDescription().getScale();

        // Try to replace nominal scale with ordinal scale based on base data type
        if (scale == DataScale.NOMINAL && handle.getGeneralization(attribute) != 0) {
            if (!(handle.getBaseDataType(attribute) instanceof ARXString) && getHierarchy(col, true) != null) {
                scale = DataScale.ORDINAL;
            }
        }

        // Store
        scales.put(attribute, scale);
        statistics.put(attribute, new DescriptiveStatistics());
        geomean.put(attribute, new GeometricMean());
        ordinal.put(attribute, getSummaryStatisticsOrdinal(handle.getGeneralization(attribute),
                handle.getDataType(attribute), handle.getBaseDataType(attribute), getHierarchy(col, true)));
    }

    // Compute summary statistics
    for (int row = 0; row < handle.getNumRows(); row++) {

        // Check, if we should include this row
        boolean include = true;
        if (listwiseDeletion) {
            for (int col = 0; col < handle.getNumColumns(); col++) {
                if (handle.isOutlier(row) || DataType.isNull(handle.getValue(row, col))) {
                    include = false;
                    break;
                }
            }
        }

        // Check
        checkInterrupt();

        // If yes, add
        if (include) {

            // For each column
            for (int col = 0; col < handle.getNumColumns(); col++) {

                // Meta
                String value = handle.getValue(row, col);
                String attribute = handle.getAttributeName(col);
                DataType<?> type = handle.getDataType(attribute);

                // Analyze
                if (!DataType.isAny(value) && !DataType.isNull(value)) {
                    ordinal.get(attribute).addValue(value);
                    if (type instanceof DataTypeWithRatioScale) {
                        double doubleValue = ((DataTypeWithRatioScale) type).toDouble(type.parse(value));
                        statistics.get(attribute).addValue(doubleValue);
                        geomean.get(attribute).increment(doubleValue + 1d);
                    }
                }
            }
        }
    }

    // Convert
    Map<String, StatisticsSummary<?>> result = new HashMap<String, StatisticsSummary<?>>();
    for (int col = 0; col < handle.getNumColumns(); col++) {

        // Check
        checkInterrupt();

        // Depending on scale
        String attribute = handle.getAttributeName(col);
        DataScale scale = scales.get(attribute);
        DataType<T> type = (DataType<T>) handle.getDataType(attribute);
        ordinal.get(attribute).analyze();
        if (scale == DataScale.NOMINAL) {
            StatisticsSummaryOrdinal stats = ordinal.get(attribute);
            result.put(attribute, new StatisticsSummary<T>(DataScale.NOMINAL, stats.getNumberOfMeasures(),
                    stats.getMode(), type.parse(stats.getMode())));
        } else if (scale == DataScale.ORDINAL) {
            StatisticsSummaryOrdinal stats = ordinal.get(attribute);
            result.put(attribute,
                    new StatisticsSummary<T>(DataScale.ORDINAL, stats.getNumberOfMeasures(), stats.getMode(),
                            type.parse(stats.getMode()), stats.getMedian(), type.parse(stats.getMedian()),
                            stats.getMin(), type.parse(stats.getMin()), stats.getMax(),
                            type.parse(stats.getMax())));
        } else if (scale == DataScale.INTERVAL) {
            StatisticsSummaryOrdinal stats = ordinal.get(attribute);
            DescriptiveStatistics stats2 = statistics.get(attribute);
            boolean isPeriod = type.getDescription().getWrappedClass() == Date.class;

            // TODO: Something is wrong with commons math's kurtosis
            double kurtosis = stats2.getKurtosis();
            kurtosis = kurtosis < 0d ? Double.NaN : kurtosis;
            double range = stats2.getMax() - stats2.getMin();
            double stddev = Math.sqrt(stats2.getVariance());

            result.put(attribute, new StatisticsSummary<T>(DataScale.INTERVAL, stats.getNumberOfMeasures(),
                    stats.getMode(), type.parse(stats.getMode()), stats.getMedian(),
                    type.parse(stats.getMedian()), stats.getMin(), type.parse(stats.getMin()), stats.getMax(),
                    type.parse(stats.getMax()), toString(type, stats2.getMean(), false, false),
                    toValue(type, stats2.getMean()), stats2.getMean(),
                    toString(type, stats2.getVariance(), isPeriod, true), toValue(type, stats2.getVariance()),
                    stats2.getVariance(), toString(type, stats2.getPopulationVariance(), isPeriod, true),
                    toValue(type, stats2.getPopulationVariance()), stats2.getPopulationVariance(),
                    toString(type, stddev, isPeriod, false), toValue(type, stddev), stddev,
                    toString(type, range, isPeriod, false), toValue(type, range),
                    stats2.getMax() - stats2.getMin(), toString(type, kurtosis, isPeriod, false),
                    toValue(type, kurtosis), kurtosis));
        } else if (scale == DataScale.RATIO) {
            StatisticsSummaryOrdinal stats = ordinal.get(attribute);
            DescriptiveStatistics stats2 = statistics.get(attribute);
            GeometricMean geo = geomean.get(attribute);

            // TODO: Something is wrong with commons math's kurtosis
            double kurtosis = stats2.getKurtosis();
            kurtosis = kurtosis < 0d ? Double.NaN : kurtosis;
            double range = stats2.getMax() - stats2.getMin();
            double stddev = Math.sqrt(stats2.getVariance());

            result.put(attribute, new StatisticsSummary<T>(DataScale.RATIO, stats.getNumberOfMeasures(),
                    stats.getMode(), type.parse(stats.getMode()), stats.getMedian(),
                    type.parse(stats.getMedian()), stats.getMin(), type.parse(stats.getMin()), stats.getMax(),
                    type.parse(stats.getMax()), toString(type, stats2.getMean(), false, false),
                    toValue(type, stats2.getMean()), stats2.getMean(),
                    toString(type, stats2.getVariance(), false, false), toValue(type, stats2.getVariance()),
                    stats2.getVariance(), toString(type, stats2.getPopulationVariance(), false, false),
                    toValue(type, stats2.getPopulationVariance()), stats2.getPopulationVariance(),
                    toString(type, stddev, false, false), toValue(type, stddev), stddev,
                    toString(type, range, false, false), toValue(type, range), range,
                    toString(type, kurtosis, false, false), toValue(type, kurtosis), kurtosis,
                    toString(type, geo.getResult() - 1d, false, false), toValue(type, geo.getResult() - 1d),
                    stats2.getGeometricMean()));
        }
    }

    return result;
}

From source file:org.jenetics.stat.DoubleMomentStatisticsTest.java

@Test(dataProvider = "sampleCounts")
public void summary(final Integer sampleCounts, final Double epsilon) {
    final List<Double> numbers = numbers(sampleCounts);

    final DescriptiveStatistics expected = new DescriptiveStatistics();
    numbers.forEach(expected::addValue);

    final DoubleMomentStatistics summary = numbers.stream()
            .collect(toDoubleMomentStatistics(Double::doubleValue));

    Assert.assertEquals(summary.getCount(), numbers.size());
    assertEqualsDouble(min(summary.getMin()), expected.getMin(), 0.0);
    assertEqualsDouble(max(summary.getMax()), expected.getMax(), 0.0);
    assertEqualsDouble(summary.getSum(), expected.getSum(), epsilon);
    assertEqualsDouble(summary.getMean(), expected.getMean(), epsilon);
    assertEqualsDouble(summary.getVariance(), expected.getVariance(), epsilon);
    assertEqualsDouble(summary.getSkewness(), expected.getSkewness(), epsilon);
    assertEqualsDouble(summary.getKurtosis(), expected.getKurtosis(), epsilon);
}

From source file:org.jenetics.stat.DoubleMomentStatisticsTest.java

@Test(dataProvider = "parallelSampleCounts")
public void parallelSummary(final Integer sampleCounts, final Double epsilon) {
    final List<Double> numbers = numbers(sampleCounts);

    final DescriptiveStatistics expected = new DescriptiveStatistics();
    numbers.forEach(expected::addValue);

    final DoubleMomentStatistics summary = numbers.parallelStream()
            .collect(toDoubleMomentStatistics(Double::doubleValue));

    Assert.assertEquals(summary.getCount(), numbers.size());
    assertEqualsDouble(min(summary.getMin()), expected.getMin(), 0.0);
    assertEqualsDouble(max(summary.getMax()), expected.getMax(), 0.0);
    assertEqualsDouble(summary.getSum(), expected.getSum(), epsilon);
    assertEqualsDouble(summary.getMean(), expected.getMean(), epsilon);
    assertEqualsDouble(summary.getVariance(), expected.getVariance(), epsilon);
    assertEqualsDouble(summary.getSkewness(), expected.getSkewness(), epsilon);
    assertEqualsDouble(summary.getKurtosis(), expected.getKurtosis(), epsilon);
}

From source file:org.jenetics.stat.IntMomentStatisticsTest.java

@Test(dataProvider = "sampleCounts")
public void summary(final Integer sampleCounts, final Double epsilon) {
    final List<Integer> numbers = numbers(sampleCounts);

    final DescriptiveStatistics expected = new DescriptiveStatistics();
    numbers.forEach(expected::addValue);

    final IntMomentStatistics summary = numbers.stream().collect(toIntMomentStatistics(Integer::intValue));

    Assert.assertEquals(summary.getCount(), numbers.size());
    assertEqualsDouble(min(summary.getMin()), expected.getMin(), 0.0);
    assertEqualsDouble(max(summary.getMax()), expected.getMax(), 0.0);
    assertEqualsDouble(summary.getSum(), expected.getSum(), epsilon);
    assertEqualsDouble(summary.getMean(), expected.getMean(), epsilon);
    assertEqualsDouble(summary.getVariance(), expected.getVariance(), epsilon);
    assertEqualsDouble(summary.getSkewness(), expected.getSkewness(), epsilon);
    assertEqualsDouble(summary.getKurtosis(), expected.getKurtosis(), epsilon);
}

From source file:org.jenetics.stat.IntMomentStatisticsTest.java

@Test(dataProvider = "parallelSampleCounts")
public void parallelSummary(final Integer sampleCounts, final Double epsilon) {
    final List<Integer> numbers = numbers(sampleCounts);

    final DescriptiveStatistics expected = new DescriptiveStatistics();
    numbers.forEach(expected::addValue);

    final IntMomentStatistics summary = numbers.parallelStream()
            .collect(toIntMomentStatistics(Integer::intValue));

    Assert.assertEquals(summary.getCount(), numbers.size());
    assertEqualsDouble(min(summary.getMin()), expected.getMin(), 0.0);
    assertEqualsDouble(max(summary.getMax()), expected.getMax(), 0.0);
    assertEqualsDouble(summary.getSum(), expected.getSum(), epsilon);
    assertEqualsDouble(summary.getMean(), expected.getMean(), epsilon);
    assertEqualsDouble(summary.getVariance(), expected.getVariance(), epsilon);
    assertEqualsDouble(summary.getSkewness(), expected.getSkewness(), epsilon);
    assertEqualsDouble(summary.getKurtosis(), expected.getKurtosis(), epsilon);
}

From source file:org.jenetics.stat.LongMomentStatisticsTest.java

@Test(dataProvider = "sampleCounts")
public void summary(final Integer sampleCounts, final Double epsilon) {
    final List<Long> numbers = numbers(sampleCounts);

    final DescriptiveStatistics expected = new DescriptiveStatistics();
    numbers.forEach(expected::addValue);

    final LongMomentStatistics summary = numbers.stream().collect(toLongMomentStatistics(Long::longValue));

    Assert.assertEquals(summary.getCount(), numbers.size());
    assertEqualsDouble(min(summary.getMin()), expected.getMin(), 0.0);
    assertEqualsDouble(max(summary.getMax()), expected.getMax(), 0.0);
    assertEqualsDouble(summary.getSum(), expected.getSum(), epsilon);
    assertEqualsDouble(summary.getMean(), expected.getMean(), epsilon);
    assertEqualsDouble(summary.getVariance(), expected.getVariance(), epsilon);
    assertEqualsDouble(summary.getSkewness(), expected.getSkewness(), epsilon);
    assertEqualsDouble(summary.getKurtosis(), expected.getKurtosis(), epsilon);
}