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

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

Introduction

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

Prototype

public void addValue(double v) 

Source Link

Document

Adds the value to the dataset.

Usage

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);// www  . j  a  v  a  2 s. c o 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());
                }
            }
        }

    }
}

From source file:org.jax.haplotype.analysis.PhylogenySignificanceTester.java

/**
 * Recursive function that fills in the list of significance values
 * @param phylogenyEdge/*  www. j  av a 2s .  c o  m*/
 *          the edge to test
 * @return
 *          the cumulative strain set
 */
private PhylogenyTreeEdgeWithRealValue testMultipleResponseSignificanceRecursive(
        PhylogenyTreeEdge phylogenyEdge, Map<String, List<Double>> phenotypeData) {
    PhylogenyTreeNode node = phylogenyEdge.getNode();

    Set<String> cumulativeBranchStrains = new HashSet<String>();
    List<PhylogenyTreeEdge> childEdgesWithPValue = new ArrayList<PhylogenyTreeEdge>(
            node.getChildEdges().size());
    for (PhylogenyTreeEdge edge : node.getChildEdges()) {
        PhylogenyTreeEdgeWithRealValue newEdgeWithPValue = this.testMultipleResponseSignificanceRecursive(edge,
                phenotypeData);
        childEdgesWithPValue.add(newEdgeWithPValue);

        // TODO make me more efficient
        // This is really inefficient. We're repeating a lot of
        // work that was done in the recursive calls here...
        cumulativeBranchStrains.addAll(newEdgeWithPValue.getNode().getAllStrains());
    }

    cumulativeBranchStrains.addAll(node.getStrains());

    double significanceValue = 1.0;
    int inPhyloCount = cumulativeBranchStrains.size();
    if (inPhyloCount >= 2 && (phenotypeData.size() - inPhyloCount) >= 2) {
        DescriptiveStatistics inPhyloStats = new DescriptiveStatistics();
        DescriptiveStatistics outPhyloStats = new DescriptiveStatistics();

        for (Entry<String, List<Double>> entry : phenotypeData.entrySet()) {
            if (cumulativeBranchStrains.contains(entry.getKey())) {
                inPhyloStats.addValue(StatisticUtilities.calculateMean(entry.getValue()));
            } else {
                outPhyloStats.addValue(StatisticUtilities.calculateMean(entry.getValue()));
            }
        }

        try {
            significanceValue = this.tTest.tTest(inPhyloStats, outPhyloStats);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "t test failed", ex);
        }
    }
    PhylogenyTreeNode newNode = new PhylogenyTreeNode(childEdgesWithPValue, node.getStrains());
    return new PhylogenyTreeEdgeWithRealValue(phylogenyEdge.getSdpBits(), newNode,
            phylogenyEdge.getEdgeLength(), significanceValue);
}

From source file:org.jax.haplotype.analysis.StrainBinaryPartitionSignificanceTester.java

/**
 * Test the significance of the given responses
 * @param strainPartitions//from  ww  w.  j  a  va2s .co m
 *          the partitions to test
 * @param strainResponses
 *          the responses
 * @return
 *          test p-values. this will be an array as long as the
 *          input partitions
 */
public double[] tTestSingleResponseSignificance(List<? extends BinaryStrainPartition> strainPartitions,
        double[] strainResponses) {
    double[] significanceValues = new double[strainPartitions.size()];
    for (int currPartitionIndex = 0; currPartitionIndex < strainPartitions.size(); currPartitionIndex++) {
        BinaryStrainPartition currPartition = strainPartitions.get(currPartitionIndex);

        // segregate the responses
        BitSet currStrainBitSet = currPartition.getStrainBitSet();
        int currStrainCount = currStrainBitSet.cardinality();
        double[] insidePartitionResponses = new double[currStrainCount];
        double[] outsidePartitionResponses = new double[strainResponses.length - currStrainCount];

        int currInsidePartitionCursor = 0;
        int currOutsidePartitionCursor = 0;
        for (int responseIndex = 0; responseIndex < strainResponses.length; responseIndex++) {
            if (currStrainBitSet.get(responseIndex)) {
                // this response is in the partition
                insidePartitionResponses[currInsidePartitionCursor] = strainResponses[responseIndex];
                currInsidePartitionCursor++;
            } else {
                // this response is outside the partition
                outsidePartitionResponses[currOutsidePartitionCursor] = strainResponses[responseIndex];
                currOutsidePartitionCursor++;
            }
        }

        assert currInsidePartitionCursor == insidePartitionResponses.length;
        assert currOutsidePartitionCursor == outsidePartitionResponses.length;

        // perform t-test on segregated responses
        if (insidePartitionResponses.length <= 2 || outsidePartitionResponses.length <= 2) {
            significanceValues[currPartitionIndex] = 1.0;
        } else {
            DescriptiveStatistics insidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currInsideResponseValue : insidePartitionResponses) {
                insidePartitionResponseSummary.addValue(currInsideResponseValue);
            }

            DescriptiveStatistics outsidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currOutsideResponseValue : outsidePartitionResponses) {
                outsidePartitionResponseSummary.addValue(currOutsideResponseValue);
            }

            try {
                double pValue = this.tTester.tTest(insidePartitionResponseSummary,
                        outsidePartitionResponseSummary);

                significanceValues[currPartitionIndex] = pValue;
            } catch (MathException ex) {
                throw new IllegalStateException(ex);
            }
        }
    }

    return significanceValues;
}

From source file:org.jax.haplotype.analysis.StrainBinaryPartitionSignificanceTester.java

/**
 * Test the significance of the given responses
 * @param genomicPartitions/*from  w ww.  j  a v a  2 s  .  c  o m*/
 *          the partitions to test
 * @param strainResponses
 *          the responses
 * @return
 *          significance values. this will be an array as long as the
 *          input partitions
 */
public double[] normalizedTestSingleResponseSignificance(
        List<? extends PartitionedIntervalSet> genomicPartitions, double[] strainResponses) {
    double[] sinificanceValues = new double[genomicPartitions.size()];
    for (int currPartitionIndex = 0; currPartitionIndex < genomicPartitions.size(); currPartitionIndex++) {
        BinaryStrainPartition currPartition = genomicPartitions.get(currPartitionIndex);

        // segregate the responses
        BitSet currPartitionStrainBitSet = currPartition.getStrainBitSet();
        int currPartitionChromoCount = currPartitionStrainBitSet.cardinality();
        double[] insidePartitionResponses = new double[currPartitionChromoCount];
        double[] outsidePartitionResponses = new double[strainResponses.length - currPartitionChromoCount];

        int currInsidePartitionCursor = 0;
        int currOutsidePartitionCursor = 0;
        for (int responseIndex = 0; responseIndex < strainResponses.length; responseIndex++) {
            if (currPartitionStrainBitSet.get(responseIndex)) {
                // this response is in the partition
                insidePartitionResponses[currInsidePartitionCursor] = strainResponses[responseIndex];
                currInsidePartitionCursor++;
            } else {
                // this response is outside the partition
                outsidePartitionResponses[currOutsidePartitionCursor] = strainResponses[responseIndex];
                currOutsidePartitionCursor++;
            }
        }

        assert currInsidePartitionCursor == insidePartitionResponses.length;
        assert currOutsidePartitionCursor == outsidePartitionResponses.length;

        // perform t-test on segregated responses
        if (insidePartitionResponses.length <= 2 || outsidePartitionResponses.length <= 2) {
            sinificanceValues[currPartitionIndex] = 1.0;
        } else {
            DescriptiveStatistics insidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currInsideResponseValue : insidePartitionResponses) {
                insidePartitionResponseSummary.addValue(currInsideResponseValue);
            }

            DescriptiveStatistics outsidePartitionResponseSummary = new DescriptiveStatistics();
            for (double currOutsideResponseValue : outsidePartitionResponses) {
                outsidePartitionResponseSummary.addValue(currOutsideResponseValue);
            }

            try {
                double pValue = this.tTester.tTest(insidePartitionResponseSummary,
                        outsidePartitionResponseSummary);

                // reduce the pValue relative to cumulative extent
                pValue /= genomicPartitions.get(currPartitionIndex).getCumulativeExtentInBasePairs();

                sinificanceValues[currPartitionIndex] = pValue;
            } catch (MathException ex) {
                throw new IllegalStateException(ex);
            }
        }
    }

    return sinificanceValues;
}

From source file:org.jgap.gp.function.Mean.java

@Override
public double execute_double(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_double(n, i, args));
    }/*from ww  w  . ja va  2  s. com*/
    return stats.getMean();
}

From source file:org.jgap.gp.function.statistics.Kurtosis.java

@Override
public double execute_double(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_double(n, i, args));
    }// w  w  w.jav a2s. co  m
    return stats.getKurtosis();
}

From source file:org.jgap.gp.function.statistics.Kurtosis.java

@Override
public float execute_float(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_float(n, i, args));
    }/*w  w w .j  a v  a2  s  .c o m*/
    return (float) stats.getKurtosis();
}

From source file:org.jgap.gp.function.statistics.Skewness.java

@Override
public double execute_double(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_double(n, i, args));
    }//from  w ww .ja v  a2 s.c o m
    return stats.getSkewness();
}

From source file:org.jgap.gp.function.statistics.Skewness.java

@Override
public float execute_float(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_float(n, i, args));
    }/*w w w.  j  av  a  2s .c om*/
    return (float) stats.getSkewness();
}

From source file:org.jgap.gp.function.statistics.StandardDeviation.java

@Override
public double execute_double(ProgramChromosome c, int n, Object[] args) {
    int size = size();
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < size; i++) {
        stats.addValue(c.execute_double(n, i, args));
    }//w w  w  .ja v a 2 s .c o  m
    return stats.getStandardDeviation();
}