/** * Returns a boolean indicating whether an append request can be sent to the member. * * @return Indicates whether an append request can be sent to the member. */ public boolean canAppend() { return appending == 0 || (appendSucceeded && appending < MAX_APPENDS && System.currentTimeMillis() - (timeStats.getMean() / MAX_APPENDS) >= appendTime); }
/** * Computes the phi value from the given samples. * <p> * The original phi value in Hayashibara's paper is calculated based on a normal distribution. * Here, we calculate it based on an exponential distribution. * * @param samples the samples from which to compute phi * @param lastHeartbeat the last heartbeat * @param currentTime the current time * @return phi */ private double computePhi(DescriptiveStatistics samples, long lastHeartbeat, long currentTime) { long size = samples.getN(); long t = currentTime - lastHeartbeat; return (size > 0) ? phiFactor * t / samples.getMean() : 100; }
/** * filter snapshots using statistically significant points only * * @param selectedLists list of snapshots */ private void statBasedSelectMetricHistory(final LinkedList<InMemoryHistoryNode> selectedLists) throws ClassCastException { logger.debug("selecting snapshots which are far away from mean value"); final DescriptiveStatistics descStats = getDescriptiveStatistics(selectedLists); final Double mean = descStats.getMean(); final Double std = descStats.getStandardDeviation(); final Iterator<InMemoryHistoryNode> ite = selectedLists.iterator(); while (ite.hasNext()) { final InMemoryHistoryNode currentNode = ite.next(); final double value = ((Number) currentNode.getValue()).doubleValue(); // remove all elements which lies in 95% value band if (value < mean + this.standardDeviationFactor * std && value > mean - this.standardDeviationFactor * std) { ite.remove(); } } }
/** * Produce results in printable CSV format, separated by separator. * @param fmt text formatter - destination of formatted results. * @param name benchmark name * @param stats benchmark data * @param separator field separator */ private void displayCSV(@NotNull Formatter fmt, @NotNull String name, @NotNull DescriptiveStatistics stats, @NotNull String separator) { double mean = stats.getMean(); double err = stats.getStandardDeviation() / mean * 100; long conv = scale.toNanos(1); fmt.format("%s%s%g%s%g%s%g%s%g%s%g%n", name, separator, mean / conv, separator, median(stats) / conv, separator, stats.getMin() / conv, separator, stats.getMax() / conv, separator, err); }
/** * Produce printable result * @param fmt text formatter - destination of formatted results. * @param name benchmark name * @param stats benchmark data */ private void displayStats(@NotNull Formatter fmt, @NotNull String name, @NotNull DescriptiveStatistics stats) { double mean = stats.getMean(); double err = stats.getStandardDeviation() / mean * 100; long conv = scale.toNanos(1); fmt.format("%-30s %-8.4g %-8.4g %-8.4g %-8.4g %-8.4g%n", name, mean / conv, median(stats) / conv, stats.getMin() / conv, stats.getMax() / conv, err); }
/** * Generates a text report displaying univariate statistics from values * that have been added. Each statistic is displayed on a separate * line. * * @return String with line feeds displaying statistics */ @Override public String toString() { StringBuilder outBuffer = new StringBuilder(); String endl = "\n"; outBuffer.append("DescriptiveStatistics:").append(endl); outBuffer.append("n: ").append(getN()).append(endl); outBuffer.append("min: ").append(getMin()).append(endl); outBuffer.append("max: ").append(getMax()).append(endl); outBuffer.append("mean: ").append(getMean()).append(endl); outBuffer.append("std dev: ").append(getStandardDeviation()) .append(endl); try { // No catch for MIAE because actual parameter is valid below outBuffer.append("median: ").append(getPercentile(50)).append(endl); } catch (MathIllegalStateException ex) { outBuffer.append("median: unavailable").append(endl); } outBuffer.append("skewness: ").append(getSkewness()).append(endl); outBuffer.append("kurtosis: ").append(getKurtosis()).append(endl); return outBuffer.toString(); }
/** * Get new statistics that excludes values beyond mean +/- 2 * stdev * * @param data Source data * @return new {@link @DescriptiveStatistics objects with sanitized data} */ private static DescriptiveStatistics sanitize(@NotNull DescriptiveStatistics data) { double meanValue = data.getMean(); double delta = MARGIN * meanValue; double minVal = meanValue - delta; double maxVal = meanValue + delta; return new DescriptiveStatistics(Arrays.stream(data.getValues()) .filter(x -> x > minVal && x < maxVal) .toArray()); }
static SimpleStats from(DescriptiveStatistics stats) { SimpleStats ss = new SimpleStats(); ss._min = stats.getMin(); ss._max = stats.getMax(); ss._mean = stats.getMean(); ss._std = stats.getStandardDeviation(); ss._median = stats.getPercentile(50); return ss; }
/** * 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; }
System.out.println("Compilation took " + stats.getMean() + "ms ± " + stats.getStandardDeviation() + "ms"); FileWriter wrt = new FileWriter(new File("target/compilation-stats.csv"), true); wrt.append(String.format("%s;%s;%s\n", GROOVY_VERSION, stats.getMean(), stats.getStandardDeviation())); wrt.close();
private void logStats(Set<?> set, double[] raw) { DescriptiveStatistics descStats = new DescriptiveStatistics(raw); Log.i( TAG, String.format( Locale.US, STAT_FORMAT, set.getClass().getSimpleName(), set.size(), descStats.getN(), Math.round(descStats.getMin()), Math.round(descStats.getMax()), Math.round(descStats.getMean()), descStats.getStandardDeviation(), descStats.getVariance() ) ); } }
@Test(dataProvider = "provideStandardErrors") public void testMultiplePositions(double maxStandardError) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (int i = 0; i < 500; ++i) { int uniques = ThreadLocalRandom.current().nextInt(getUniqueValuesCount()) + 1; List<Object> values = createRandomSample(uniques, (int) (uniques * 1.5)); long actual = estimateGroupByCount(values, maxStandardError); double error = (actual - uniques) * 1.0 / uniques; stats.addValue(error); } assertLessThan(stats.getMean(), 1.0e-2); assertLessThan(stats.getStandardDeviation(), 1.0e-2 + maxStandardError); }
@Override public double getMean() { return descStats.getMean(); }
@Override public void run() { DescriptiveStatistics stats = limiter.getRateStatsSinceLastReport(); if (stats != null) { log.info(String.format("Requests rate stats: count: %d, min: %f, max: %f, mean: %f, std: %f, sum: %f", stats.getN(), stats.getMin(), stats.getMax(), stats.getMean(), stats.getStandardDeviation(), stats.getSum())); } stats = limiter.getUnusedPermitsSinceLastReport(); if (stats != null) { log.info(String.format("Unused permits rate stats: count: %d, min: %f, max: %f, mean: %f, std: %f, sum: %f", stats.getN(), stats.getMin(), stats.getMax(), stats.getMean(), stats.getStandardDeviation(), stats.getSum())); } if (this.policy instanceof QPSPolicy) { QPSPolicy qpsPolicy = (QPSPolicy) this.policy; DynamicTokenBucket dynamicTokenBucket = qpsPolicy.getTokenBucket(); TokenBucket tokenBucket = dynamicTokenBucket.getTokenBucket(); log.info("Stored tokens: " + tokenBucket.getStoredTokens()); } } }
private String getSummary(DescriptiveStatistics stats) { return String.format("Mean: %d, Std Dev: %d", (int)stats.getMean(), (int)Math.sqrt(stats.getVariance())); }
public static String describe(DescriptiveStatistics stats, Double[] percentiles){ StringBuilder sb = new StringBuilder(); sb.append(String.format("round: mean of %dms [+-%d], measured %d rounds;\n", (long)stats.getMean(), (long)stats.getStandardDeviation(), stats.getN() )); sb.append("\tMin - " + (long)stats.getMin() + "\n"); for(double pctile : percentiles) { sb.append("\t" + pctile + " - " + stats.getPercentile(pctile) + "\n"); } sb.append("\tMax - " + (long)stats.getMax()); return sb.toString(); }
@Override public void accept(Writable writable) { if(pw.isPresent()) { List<String> parts = new ArrayList<>(); parts.add("" + writable.getDate().getTime()); for (Results r : writable.getResults()) { parts.add(r.getName()); parts.add(r.getEps() == null?"":(r.getEps() + "")); if (r.getHistory().isPresent()) { parts.add("" + (int) r.getHistory().get().getMean()); parts.add("" + (int) Math.sqrt(r.getHistory().get().getVariance())); } else { parts.add(""); parts.add(""); } } pw.get().println(Joiner.on(",").join(parts)); pw.get().flush(); } }
@Override public String toString() { MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this) .add("numResults", stats.getN()) .add("minTime", stats.getMin()) .add("meanTime", stats.getMean()) .add("maxTime", stats.getMax()) .add("25th", stats.getPercentile(25)) .add("50th", stats.getPercentile(50)) .add("75th", stats.getPercentile(75)) .add("90th", stats.getPercentile(90)) .add("95th", stats.getPercentile(95)) .add("99th", stats.getPercentile(99)) .add("99.9th", stats.getPercentile(99.9)) .add("99.99th", stats.getPercentile(99.99)) .add("traces", traces); return helper.toString(); } }
public void printResults() { printf("%-20s%20s%20s%20s\n", "Test Name", format.name + " Avg", "StdDev", "95% Interval"); for (int i = 0; i < numTests; i++) { double mean = stats[i].getMean() / format.divisor; double stddev = stats[i].getStandardDeviation() / format.divisor; double diff = 1.96 * stddev / MathUtils.sqrt(stats[i].getN()); printf("%-20s%20.3f%20.3f (%7.3f,%7.3f)\n", getTestName(i), mean, stddev, mean - diff, mean + diff); } }
@Test(groups = "slow") public void testError() throws Exception { DescriptiveStatistics stats = new DescriptiveStatistics(); int buckets = 2048; for (int i = 0; i < 10000; ++i) { HyperLogLog estimator = new HyperLogLog(buckets); Set<Long> randomSet = makeRandomSet(5 * buckets); for (Long value : randomSet) { estimator.add(value); } double error = (estimator.estimate() - randomSet.size()) * 1.0 / randomSet.size(); stats.addValue(error); } assertTrue(stats.getMean() < 1e-2); assertTrue(stats.getStandardDeviation() < 1.04 / Math.sqrt(buckets)); }