📄 summarystatistics.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.math.stat.descriptive;import java.io.Serializable;import org.apache.commons.discovery.tools.DiscoverClass;import org.apache.commons.math.stat.descriptive.moment.GeometricMean;import org.apache.commons.math.stat.descriptive.moment.Mean;import org.apache.commons.math.stat.descriptive.moment.SecondMoment;import org.apache.commons.math.stat.descriptive.moment.Variance;import org.apache.commons.math.stat.descriptive.rank.Max;import org.apache.commons.math.stat.descriptive.rank.Min;import org.apache.commons.math.stat.descriptive.summary.Sum;import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;import org.apache.commons.math.util.MathUtils;/** * <p>Computes summary statistics for a stream of data values added using the * {@link #addValue(double) addValue} method. The data values are not stored in * memory, so this class can be used to compute statistics for very large * data streams.</p> * * <p>The {@link StorelessUnivariateStatistic} instances used to maintain * summary state and compute statistics are configurable via setters. * For example, the default implementation for the variance can be overridden by * calling {@link #setVarianceImpl(StorelessUnivariateStatistic)}. Actual * parameters to these methods must implement the * {@link StorelessUnivariateStatistic} interface and configuration must be * completed before <code>addValue</code> is called. No configuration is * necessary to use the default, commons-math provided implementations.</p> * * <p>Note: This class is not thread-safe. Use * {@link SynchronizedSummaryStatistics} if concurrent access from multiple * threads is required.</p> * * @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $ */public class SummaryStatistics implements StatisticalSummary, Serializable { /** Serialization UID */ private static final long serialVersionUID = -3346512372447011854L; /** * Create an instance of a <code>SummaryStatistics</code> * * @param cls the type of <code>SummaryStatistics</code> object to * create. * @return a new instance. * @deprecated to be removed in commons-math 2.0 * @throws InstantiationException is thrown if the object can not be * created. * @throws IllegalAccessException is thrown if the type's default * constructor is not accessible. */ public static SummaryStatistics newInstance(Class cls) throws InstantiationException, IllegalAccessException { return (SummaryStatistics)cls.newInstance(); } /** * Create an instance of a <code>SummaryStatistics</code> * * @return a new SummaryStatistics instance. * @deprecated to be removed in commons-math 2.0 */ public static SummaryStatistics newInstance() { SummaryStatistics instance = null; try { DiscoverClass dc = new DiscoverClass(); instance = (SummaryStatistics) dc.newInstance( SummaryStatistics.class, "org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl"); } catch(Throwable t) { return new SummaryStatisticsImpl(); } return instance; } /** * Construct a SummaryStatistics instance */ public SummaryStatistics() { } /** count of values that have been added */ protected long n = 0; /** SecondMoment is used to compute the mean and variance */ protected SecondMoment secondMoment = new SecondMoment(); /** sum of values that have been added */ protected Sum sum = new Sum(); /** sum of the square of each value that has been added */ protected SumOfSquares sumsq = new SumOfSquares(); /** min of values that have been added */ protected Min min = new Min(); /** max of values that have been added */ protected Max max = new Max(); /** sumLog of values that have been added */ protected SumOfLogs sumLog = new SumOfLogs(); /** geoMean of values that have been added */ protected GeometricMean geoMean = new GeometricMean(sumLog); /** mean of values that have been added */ protected Mean mean = new Mean(); /** variance of values that have been added */ protected Variance variance = new Variance(); /** Sum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumImpl = sum; /** Sum of squares statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumsqImpl = sumsq; /** Minimum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic minImpl = min; /** Maximum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic maxImpl = max; /** Sum of log statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumLogImpl = sumLog; /** Geometric mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic geoMeanImpl = geoMean; /** Mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic meanImpl = mean; /** Variance statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic varianceImpl = variance; /** * Return a {@link StatisticalSummaryValues} instance reporting current * statistics. * * @return Current values of statistics */ public StatisticalSummary getSummary() { return new StatisticalSummaryValues(getMean(), getVariance(), getN(), getMax(), getMin(), getSum()); } /** * Add a value to the data * * @param value the value to add */ public void addValue(double value) { sumImpl.increment(value); sumsqImpl.increment(value); minImpl.increment(value); maxImpl.increment(value); sumLogImpl.increment(value); secondMoment.increment(value); // If mean, variance or geomean have been overridden, // need to increment these if (!(meanImpl instanceof Mean)) { meanImpl.increment(value); } if (!(varianceImpl instanceof Variance)) { varianceImpl.increment(value); } if (!(geoMeanImpl instanceof GeometricMean)) { geoMeanImpl.increment(value); } n++; } /** * Returns the number of available values * @return The number of available values */ public long getN() { return n; } /** * Returns the sum of the values that have been added * @return The sum or <code>Double.NaN</code> if no values have been added */ public double getSum() { return sumImpl.getResult(); } /** * Returns the sum of the squares of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return The sum of squares */ public double getSumsq() { return sumsqImpl.getResult(); } /** * Returns the mean of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the mean */ public double getMean() { if (mean == meanImpl) { return new Mean(secondMoment).getResult(); } else { return meanImpl.getResult(); } } /** * Returns the standard deviation of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the standard deviation */ public double getStandardDeviation() { double stdDev = Double.NaN; if (getN() > 0) { if (getN() > 1) { stdDev = Math.sqrt(getVariance()); } else { stdDev = 0.0; } } return (stdDev); } /** * Returns the variance of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the variance */ public double getVariance() { if (varianceImpl == variance) { return new Variance(secondMoment).getResult(); } else { return varianceImpl.getResult(); } } /** * Returns the maximum of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the maximum */ public double getMax() { return maxImpl.getResult(); } /** * Returns the minimum of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the minimum */ public double getMin() { return minImpl.getResult(); } /** * Returns the geometric mean of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the geometric mean */ public double getGeometricMean() { return geoMeanImpl.getResult(); } /** * Returns the sum of the logs of the values that have been added. * <p> * Double.NaN is returned if no values have been added.</p> * * @return the sum of logs * @since 1.2 */ public double getSumOfLogs() { return sumLogImpl.getResult(); } /** * Generates a text report displaying * summary statistics from values that * have been added.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -