Java org.apache.commons.math3.stat.descriptive.DescriptiveStatistics 代码实例

・14 分钟阅读

以下是展示如何使用org.apache.commons.math3.stat.descriptive.DescriptiveStatistics的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Adds the events' (numerical) values to a {@link DescriptiveStatistics}
 * instance.
 * 
 * @return an object maintaining a dataset of values of a single statistical
 *         variable to compute descriptive statistics based on the events'
 *         values
 */
public DescriptiveStatistics statistics() {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (Event<? extends Number> event : this.events) {
                stats.addValue(event.value().doubleValue());
        }
        return stats;
}
 

实例 2


private void calculate() {
        if (!this.calculated) {
                precisionStats = new DescriptiveStatistics();
                recallStats = new DescriptiveStatistics();
                fScoreStats = new DescriptiveStatistics();
                precisionWeightedStats = new DescriptiveStatistics();
                recallWeightedStats = new DescriptiveStatistics();
                fScoreWeightedStats = new DescriptiveStatistics();
                for (E outcome : fScoreCalculators.keySet()) {
                        if (!outcomeCounts.containsKey(outcome))
                                outcomeCounts.put(outcome, 0);
                        int count = outcomeCounts.get(outcome);
                        FScoreCalculator<Boolean> fScoreCalculator = fScoreCalculators.get(outcome);
                        if (count>0) {
                                precisionStats.addValue(fScoreCalculator.getPrecision(true));
                                recallStats.addValue(fScoreCalculator.getRecall(true));
                                fScoreStats.addValue(fScoreCalculator.getFScore(true));
                        }
                        for (int i=0; i<count; i++) {
                                precisionWeightedStats.addValue(fScoreCalculator.getPrecision(true));
                                recallWeightedStats.addValue(fScoreCalculator.getRecall(true));
                                fScoreWeightedStats.addValue(fScoreCalculator.getFScore(true));
                        }
                }       
                this.calculated = true;
        }
}
 

实例 3


/**
 * Stop recording sample measurements.
 */
@Override
public void stopSample()
{
        sampler.setSampling(false);
        DescriptiveStatistics stats = new DescriptiveStatistics();
        // Add the data from the array
        for (int i = 0; i < acceleration.length; i++)
        {
                stats.addValue(acceleration[i]);
        }
        sampleRMS = stats.getStandardDeviation();
        sampleMean = StatUtils.mean(acceleration);
        sampleMax = StatUtils.max(acceleration);
        sampleMin = StatUtils.min(acceleration);
}
 

实例 4


/**
 * Asserts the samples are uniformly distributed.
 * 
 * @param points the samples
 * @param D the dimension of the samples
 */
protected void checkStatistics(double[][] points, int D) {
        for (int i = 0; i < D; i++) {
                DescriptiveStatistics statistics = new DescriptiveStatistics();
                for (int j = 0; j < points.length; j++) {
                        statistics.addValue(points[j][i]);
                }
                testUniformDistribution(0.0, 1.0, statistics);
        }
}
 

实例 5


public static void main(String[] args) throws IOException {
                DescriptiveStatistics stats = new DescriptiveStatistics();
                stats.addValue(.80);
                stats.addValue(.81);
                stats.addValue(.81);
                stats.addValue(.82);
                stats.addValue(.84);
                stats.addValue(.81);
                stats.addValue(.84);
                stats.addValue(.83);
                stats.addValue(.81);
                stats.addValue(.82);
                System.out.println("Mean: " + stats.getMean());
                System.out.println("SD: " + stats.getStandardDeviation());
        }
 

实例 6


private void setParams(int i, double[] params, double[] params_dev, DescriptiveStatistics sample)
{
        if (sample.getN() > 0)
        {
                params[i] = sample.getMean();
                params_dev[i] = sample.getStandardDeviation();
        }
}
 

实例 7


@Override
public void onEvaluationComplete() {
        try {
                LOG.info("##################");
                LOG.info("timeStatsPerLength");
                LOG.info("tlengthtcounttmeantperToken");
                if (writer!=null)
                        writer.write(CSV.format("length") + CSV.format("count") + CSV.format("mean") + CSV.format("perToken") + "n");
                DecimalFormat df = new DecimalFormat("0.00");
                for (int length : timeStatsPerLength.keySet()) {
                        DescriptiveStatistics stats = timeStatsPerLength.get(length);
                        long count = stats.getN();
                        double mean = stats.getMean();
                        double perToken = mean / length;
                        LOG.info("t" + length + "t" + count + "t" + df.format(mean) + "t" + df.format(perToken));
                        if (writer!=null)
                                writer.write(CSV.format(length) + CSV.format(count) + CSV.format(mean) + CSV.format(perToken) + "n");
                }
                if (writer!=null) {
                        writer.flush();
                        writer.close();
                }
                LOG.info("##################");
        } catch (IOException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
        }
}
 

实例 8


public void execute(JMeterTransactions originalJMeterTransactions) {
    Locale.setDefault(Locale.ENGLISH);
    try {
        JMeterTransactions transactions = filterTransactions(originalJMeterTransactions);
        transactionCount = transactions.getTransactionCount();
        DescriptiveStatistics ds = new DescriptiveStatistics();
        for (List<String> transaction : transactions) {
            String elapsed = transaction.get(1);
            ds.addValue(Double.parseDouble(elapsed));
        }
        longestTransactions = transactions.getLongestTransactions();
        double avgRespTime = ds.getMean();
        DecimalFormat df = new DecimalFormat("#.##");
        double roundedAvgRespTime = Double.valueOf(df.format(avgRespTime));
        actualResult = String.format(ACTUAL_RESULT_MESSAGE, roundedAvgRespTime);
        if (roundedAvgRespTime > maxAvgRespTime) {
            result = TestResult.FAIL;
        } else {
            result = TestResult.PASS;
        }
    } catch (Exception e) {
        result = TestResult.IGNORED;
        actualResult = e.getMessage();
    }
}
 

实例 9


public static void trimLargeItems(int[] stringLengths, List<? extends Object> originals) {
  DescriptiveStatistics stats = new DescriptiveStatistics();
  for (int i = 0; i < stringLengths.length; i++) {
    stats.addValue(stringLengths[i]);
  }
  double stdDev = stats.getStandardDeviation();
  double mean = stats.getMean();
  List<Object> toRemove = new ArrayList<Object>();
  for (int i = 0; i < stringLengths.length; i++) {
    double diff = stringLengths[i] - mean;
    if (diff / stdDev > 4d) {
      toRemove.add(originals.get(i));
    }
  }
  for (Object obj : toRemove) {
    originals.remove(obj);
  }
}
 

实例 10


/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }
    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();
    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];
    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 

实例 11


public DescriptiveStatisticsGenerator(String title, String description, DescriptiveStatistics  stats) {
        super(title, description);
        columns = Lists.newArrayList("Stat", "Value");
        lines = Lists.newArrayList("Total", "Min", "Max", "Mean", "Std Dev", "Median");
        double[] values = {stats.getN(), stats.getMin(), stats.getMax(), stats.getMean(),stats.getStandardDeviation(), stats.getPercentile(50)};
        tableContents = Lists.newArrayList();
        for (int i = 0 ;  i < lines.size() ;i++ ){
                double[] v = {values[i]};
                tableContents.add(i,v); 
        }
}
 

实例 12


private static NormalDistribution getDistribution(DescriptiveStatistics stats) {
  NormalDistribution dist = null;
  try {
    dist = new NormalDistribution(stats.getMean(), stats.getStandardDeviation());
  } catch (Exception e) {
    LOGGER.warn("Could not get statistics", e);
  }
  return dist;
}
 

实例 13


/**
 * @param stats the DescriptiveStatistics to calculate the mean put time.
 * @return The estimated put rate based upon the time it takes to put a single
 *         measurement.
 */
private Long calculatePutRate(DescriptiveStatistics stats) {
  double putTime = stats.getMean();
  Long ret = null;
  double numPuts = 1.0 / putTime;
  ret = Math.round(numPuts);
  return ret;
}
 

实例 14


/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }
    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();
    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];
    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 

实例 15


private double computePhi(DescriptiveStatistics samples, long tLast, long tNow) {
    long size = samples.getN();
    long t = tNow - tLast;
    return (size > 0)
           ? PHI_FACTOR * t / samples.getMean()
           : BOOTSTRAP_PHI_VALUE;
}
 
讨论
淘淘あ西西 profile image