Example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics getGeometricMean

List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics getGeometricMean

Introduction

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

Prototype

public double getGeometricMean() 

Source Link

Document

Returns the <a href="http://www.xycoon.com/geometric_mean.htm"> geometric mean </a> of the available values

Usage

From source file:de.mpicbg.knime.hcs.base.nodes.preproc.ParameterMutualInformation.java

@Override
protected BufferedDataTable[] execute(BufferedDataTable[] inData, ExecutionContext exec) throws Exception {

    BufferedDataTable input = inData[0];

    // Get the parameter and make sure there all double value columns
    List<Attribute> parameters = getParameterList(input);

    // Initialize
    int Np = parameters.size();
    int iterations = Np * 2;
    double[][] mutmatrix = new double[Np][Np];
    double[][] sigmatrix = new double[Np][Np];
    ;//from  w  ww.  j av a  2 s  .  co  m
    double[][] biamatrix = new double[Np][Np];
    ;

    MutualInformation mutualinfo = new MutualInformation();
    mutualinfo.set_base(logbase.getDoubleValue());
    mutualinfo.set_method(method.getStringValue());
    mutualinfo.set_axeslinking(linkaxes.getBooleanValue());
    if (binning.getIntValue() > 0)
        mutualinfo.set_binning(binning.getIntValue());

    // Load data.
    Double[][] table = new Double[Np][input.getRowCount()];
    int j = 0;
    for (DataRow row : input) {
        int i = 0;
        for (Attribute param : parameters) {
            table[i][j] = param.getDoubleAttribute(row);
            i++;
        }
        j++;
        exec.checkCanceled();
    }
    BufTableUtils.updateProgress(exec, Np, iterations);

    // Calculate mutual information
    for (int a = 0; a < Np; a++) {
        mutualinfo.set_xvector(table[a]);

        for (int b = a; b < Np; b++) {
            mutualinfo.set_yvector(table[b]);

            // Calculate the mutual info.
            Double[] res = mutualinfo.calculate();

            // Put it into the output matrix
            mutmatrix[a][b] = res[0];
            mutmatrix[b][a] = res[0];
            sigmatrix[a][b] = res[1];
            sigmatrix[b][a] = res[1];
            biamatrix[a][b] = res[2];
            biamatrix[b][a] = res[2];
        }
        BufTableUtils.updateProgress(exec, Np + a, iterations);
        exec.checkCanceled();
    }

    // Create Tables
    BufferedDataContainer matrixContainer = exec.createDataContainer(new DataTableSpec(getMatrixSpec()));
    BufferedDataContainer listContainer = exec.createDataContainer(new DataTableSpec(getListSpec()));
    double thresh = threshold.getDoubleValue();
    int numListCol = listContainer.getTableSpec().getNumColumns();

    for (int a = 0; a < Np; a++) {

        // Initialize
        DataCell[] matrixCells = new DataCell[Np];
        DataCell[] listCells = new DataCell[numListCol];
        String similars = "";
        DescriptiveStatistics mutstats = new DescriptiveStatistics();
        DescriptiveStatistics sigstats = new DescriptiveStatistics();
        ;
        DescriptiveStatistics biastats = new DescriptiveStatistics();
        ;

        // Create matrix rows and collect values for statistics.
        for (int b = 0; b < Np; b++) {
            matrixCells[b] = new DoubleCell(mutmatrix[a][b]);
            if (a != b) {
                mutstats.addValue(mutmatrix[a][b]);
                sigstats.addValue(sigmatrix[a][b]);
                biastats.addValue(biamatrix[a][b]);
                if (mutmatrix[a][b] > thresh) {
                    similars += parameters.get(b).getName() + ",";
                }
            }
        }

        // Create matrix row
        DataRow matrixRow = new DefaultRow(parameters.get(a).getName(), matrixCells);
        matrixContainer.addRowToTable(matrixRow);

        // Create list row
        listCells[0] = new StringCell(parameters.get(a).getName());
        listCells[1] = new DoubleCell(mutstats.getMin());
        listCells[2] = new DoubleCell(mutstats.getMean());
        listCells[3] = new DoubleCell(mutstats.getMax());
        listCells[4] = new DoubleCell(sigstats.getGeometricMean());
        listCells[5] = new DoubleCell(biastats.getMean());
        listCells[6] = new StringCell(similars);
        DataRow listRow = new DefaultRow("row" + a, listCells);
        listContainer.addRowToTable(listRow);

        exec.checkCanceled();
    }

    matrixContainer.close();
    listContainer.close();
    return new BufferedDataTable[] { listContainer.getTable(), matrixContainer.getTable() };
}

From source file:com.facebook.presto.AbstractTestQueries.java

@Test
public void testTableSampleBernoulli() throws Exception {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    int total = computeExpected("SELECT orderkey FROM orders", TupleInfo.SINGLE_LONG).getMaterializedTuples()
            .size();/*w  w w.  ja va2  s.  c  o m*/

    for (int i = 0; i < 100; i++) {
        List<MaterializedTuple> values = computeActual("SELECT orderkey FROM ORDERS TABLESAMPLE BERNOULLI (50)")
                .getMaterializedTuples();

        assertEquals(values.size(), ImmutableSet.copyOf(values).size(), "TABLESAMPLE produced duplicate rows");
        stats.addValue(values.size() * 1.0 / total);
    }

    double mean = stats.getGeometricMean();
    assertTrue(mean > 0.45 && mean < 0.55,
            String.format("Expected mean sampling rate to be ~0.5, but was %s", mean));
}

From source file:org.bresearch.websec.test.CommonsMathTest.java

public void test5() {
    final DocumentWordStats docStats = new DocumentWordStats(ConstDoc.CONST_SM);
    final DescriptiveStatistics stats = docStats.mapReduceStats();

    System.out.println("" + stats.getSum());
    System.out.println("" + stats.getMean());
    System.out.println("" + stats.getN());
    System.out.println("" + stats.getGeometricMean());
    System.out.println("" + stats.getMax());

}

From source file:org.datacleaner.beans.NumberAnalyzer.java

@Override
public NumberAnalyzerResult getResult() {
    CrosstabDimension measureDimension = new CrosstabDimension(DIMENSION_MEASURE);
    measureDimension.addCategory(MEASURE_ROW_COUNT);
    measureDimension.addCategory(MEASURE_NULL_COUNT);
    measureDimension.addCategory(MEASURE_HIGHEST_VALUE);
    measureDimension.addCategory(MEASURE_LOWEST_VALUE);
    measureDimension.addCategory(MEASURE_SUM);
    measureDimension.addCategory(MEASURE_MEAN);
    measureDimension.addCategory(MEASURE_GEOMETRIC_MEAN);
    measureDimension.addCategory(MEASURE_STANDARD_DEVIATION);
    measureDimension.addCategory(MEASURE_VARIANCE);
    measureDimension.addCategory(MEASURE_SECOND_MOMENT);
    measureDimension.addCategory(MEASURE_SUM_OF_SQUARES);

    if (descriptiveStatistics) {
        measureDimension.addCategory(MEASURE_MEDIAN);
        measureDimension.addCategory(MEASURE_PERCENTILE25);
        measureDimension.addCategory(MEASURE_PERCENTILE75);
        measureDimension.addCategory(MEASURE_SKEWNESS);
        measureDimension.addCategory(MEASURE_KURTOSIS);
    }//from  w ww. ja v  a 2 s .c  o  m

    CrosstabDimension columnDimension = new CrosstabDimension(DIMENSION_COLUMN);
    for (InputColumn<? extends Number> column : _columns) {
        columnDimension.addCategory(column.getName());
    }

    Crosstab<Number> crosstab = new Crosstab<Number>(Number.class, columnDimension, measureDimension);
    for (InputColumn<? extends Number> column : _columns) {
        CrosstabNavigator<Number> nav = crosstab.navigate().where(columnDimension, column.getName());
        NumberAnalyzerColumnDelegate delegate = _columnDelegates.get(column);

        StatisticalSummary s = delegate.getStatistics();
        int nullCount = delegate.getNullCount();

        nav.where(measureDimension, MEASURE_NULL_COUNT).put(nullCount);

        if (nullCount > 0) {
            addAttachment(nav, delegate.getNullAnnotation(), column);
        }

        int numRows = delegate.getNumRows();
        nav.where(measureDimension, MEASURE_ROW_COUNT).put(numRows);

        long nonNullCount = s.getN();

        if (nonNullCount > 0) {
            final double highestValue = s.getMax();
            final double lowestValue = s.getMin();
            final double sum = s.getSum();
            final double mean = s.getMean();
            final double standardDeviation = s.getStandardDeviation();
            final double variance = s.getVariance();

            final double geometricMean;
            final double secondMoment;
            final double sumOfSquares;
            if (descriptiveStatistics) {
                final DescriptiveStatistics descriptiveStats = (DescriptiveStatistics) s;
                geometricMean = descriptiveStats.getGeometricMean();
                sumOfSquares = descriptiveStats.getSumsq();
                secondMoment = new SecondMoment().evaluate(descriptiveStats.getValues());
            } else {
                final SummaryStatistics summaryStats = (SummaryStatistics) s;
                geometricMean = summaryStats.getGeometricMean();
                secondMoment = summaryStats.getSecondMoment();
                sumOfSquares = summaryStats.getSumsq();
            }

            nav.where(measureDimension, MEASURE_HIGHEST_VALUE).put(highestValue);
            addAttachment(nav, delegate.getMaxAnnotation(), column);

            nav.where(measureDimension, MEASURE_LOWEST_VALUE).put(lowestValue);
            addAttachment(nav, delegate.getMinAnnotation(), column);

            nav.where(measureDimension, MEASURE_SUM).put(sum);
            nav.where(measureDimension, MEASURE_MEAN).put(mean);
            nav.where(measureDimension, MEASURE_GEOMETRIC_MEAN).put(geometricMean);
            nav.where(measureDimension, MEASURE_STANDARD_DEVIATION).put(standardDeviation);
            nav.where(measureDimension, MEASURE_VARIANCE).put(variance);
            nav.where(measureDimension, MEASURE_SUM_OF_SQUARES).put(sumOfSquares);
            nav.where(measureDimension, MEASURE_SECOND_MOMENT).put(secondMoment);

            if (descriptiveStatistics) {
                final DescriptiveStatistics descriptiveStatistics = (DescriptiveStatistics) s;
                final double kurtosis = descriptiveStatistics.getKurtosis();
                final double skewness = descriptiveStatistics.getSkewness();
                final double median = descriptiveStatistics.getPercentile(50.0);
                final double percentile25 = descriptiveStatistics.getPercentile(25.0);
                final double percentile75 = descriptiveStatistics.getPercentile(75.0);

                nav.where(measureDimension, MEASURE_MEDIAN).put(median);
                nav.where(measureDimension, MEASURE_PERCENTILE25).put(percentile25);
                nav.where(measureDimension, MEASURE_PERCENTILE75).put(percentile75);
                nav.where(measureDimension, MEASURE_SKEWNESS).put(skewness);
                nav.where(measureDimension, MEASURE_KURTOSIS).put(kurtosis);
            }
        }
    }
    return new NumberAnalyzerResult(_columns, crosstab);
}

From source file:org.fusesource.eca.processor.StatisticsCalculator.java

protected void process(StatisticsType type, Number value, ObjectNode statsNode) throws Exception {
    EventCache<Number> cache = this.eventCache;
    if (value != null && cache != null) {
        cache.add(value);//from   www. j  a v  a2s. co  m
        if (type.equals(StatisticsType.RATE)) {
            calculateRate(statsNode);
        } else {
            List<Number> list = this.eventCache.getWindow();
            DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
            if (list != null && !list.isEmpty()) {
                for (Number number : list) {
                    descriptiveStatistics.addValue(number.doubleValue());
                }
                switch (type) {
                case MEAN:
                    statsNode.put("mean", descriptiveStatistics.getMean());
                    break;
                case GEOMETRIC_MEAN:
                    statsNode.put("gemetric mean", descriptiveStatistics.getGeometricMean());
                    break;
                case STDDEV:
                    statsNode.put("std-dev", descriptiveStatistics.getStandardDeviation());
                    break;
                case MIN:
                    statsNode.put("minimum", descriptiveStatistics.getMin());
                    break;
                case MAX:
                    statsNode.put("maximum", descriptiveStatistics.getMax());
                    break;
                case SKEWNESS:
                    statsNode.put("skewness", descriptiveStatistics.getSkewness());
                    break;
                case KUTOSIS:
                    statsNode.put("kurtosis", descriptiveStatistics.getKurtosis());
                    break;
                case VARIANCE:
                    statsNode.put("variance", descriptiveStatistics.getVariance());
                    break;
                case COUNT:
                    statsNode.put("count", list.size());
                default:
                    statsNode.put("number", descriptiveStatistics.getN());
                    statsNode.put("mean", descriptiveStatistics.getMean());
                    statsNode.put("gemetric mean", descriptiveStatistics.getGeometricMean());
                    statsNode.put("minimum", descriptiveStatistics.getMin());
                    statsNode.put("maximum", descriptiveStatistics.getMax());
                    statsNode.put("std-dev", descriptiveStatistics.getStandardDeviation());
                    statsNode.put("median", descriptiveStatistics.getPercentile(50));
                    statsNode.put("skewness", descriptiveStatistics.getSkewness());
                    statsNode.put("kurtosis", descriptiveStatistics.getKurtosis());
                    statsNode.put("variance", descriptiveStatistics.getVariance());
                    calculateRate(statsNode);
                    statsNode.put("count", list.size());
                }
            }
        }

    }
}