Example usage for org.apache.commons.math3.stat.descriptive SummaryStatistics getMean

List of usage examples for org.apache.commons.math3.stat.descriptive SummaryStatistics getMean

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.descriptive SummaryStatistics getMean.

Prototype

public double getMean() 

Source Link

Document

Returns the mean of the values that have been added.

Usage

From source file:net.tradelib.ratio.SharpeRatio.java

/**
 * @brief Computes the Sharpe ratio for a list of returns.
 * /*from  w  w w .j  a v  a  2 s  . c o m*/
 * @param returns The returns
 * @param rf The risk free average return
 * 
 * @return The Sharpe ratio
 */
public static double value(List<Double> returns, double rf) {
    SummaryStatistics ss = new SummaryStatistics();
    returns.forEach((xx) -> ss.addValue(xx - rf));

    return ss.getMean() / ss.getStandardDeviation();
}

From source file:net.tradelib.ratio.SortinoRatio.java

/**
 * @brief Computes the Sortino ratio for a list of returns.
 * /*from   w ww  .ja va 2  s . c o  m*/
 * @param returns The returns
 * @param rf The risk free average return
 * @param multiplier Mainly used to compute the Sortino ratio
 *                   for the opposite returns, i.e. short strategy.
 * 
 * @return The Sortino ratio. Double.MAX_VALUE is returned if there
 *         are no negative returns after applying the multiplier (i.e.
 *         the divisor is 0).
 */
public static double value(List<Double> returns, double rf, double multiplier) {
    SummaryStatistics fullStats = new SummaryStatistics();
    SummaryStatistics downStats = new SummaryStatistics();
    for (int ii = 0; ii < returns.size(); ++ii) {
        double dd = (returns.get(ii) - rf) * multiplier;
        fullStats.addValue(dd);
        if (dd < rf)
            downStats.addValue(dd);
        else
            downStats.addValue(0);
    }

    if (downStats.getN() == 0)
        return Double.MAX_VALUE;

    return fullStats.getMean() / downStats.getStandardDeviation();
}

From source file:org.alfresco.bm.report.CSVReporter.java

protected void export(Writer writer) throws Exception {
    // TODO: Get any nodes
    String notes = "";

    writeTestDetails(writer, notes);//from  www  .j av  a 2  s. c  o  m

    writer.write(",,");
    writer.write("Event Name,Total Count,Success Count,Failure Count,Success Rate (%),"
            + "Min (ms), Max (ms), Arithmetic Mean (ms), Standard Deviation (ms)");
    writer.write(NEW_LINE);
    TreeMap<String, ResultSummary> summaries = collateResults(true);
    for (Map.Entry<String, ResultSummary> entry : summaries.entrySet()) {
        writer.write(",,");
        String eventName = entry.getKey();
        ResultSummary summary = entry.getValue();
        SummaryStatistics statsSuccess = summary.getStats(true);
        SummaryStatistics statsFail = summary.getStats(false);
        // Event Name
        writer.write(String.format("%s,", eventName));
        // Total Count
        writer.write(String.format("%6d,", summary.getTotalResults()));
        // Success Count
        writer.write(String.format("%6d,", statsSuccess.getN()));
        // Failure Count
        writer.write(String.format("%6d,", statsFail.getN()));
        // Success Rate (%)
        writer.write(String.format("%3.1f,", summary.getSuccessPercentage()));
        // Min (ms)
        writer.write(String.format("%10d,", (long) statsSuccess.getMin()));
        // Max (ms)
        writer.write(String.format("%10d,", (long) statsSuccess.getMax()));
        // Arithmetic Mean (ms)
        writer.write(String.format("%10d,", (long) statsSuccess.getMean()));
        // Standard Deviation (ms)
        writer.write(String.format("%10d%s", (long) statsSuccess.getStandardDeviation(), NEW_LINE));
    }
    // Done

}

From source file:org.alfresco.bm.report.XLSXReporter.java

/**
 * Create a 'Summary' sheet containing the table of averages
 *//*from ww w.  j a v  a2  s . c o m*/
private void createSummarySheet(XSSFWorkbook workbook) throws IOException, NotFoundException {
    DBObject testRunObj = getTestService().getTestRunMetadata(test, run);

    // Create the sheet
    XSSFSheet sheet = workbook.createSheet("Summary");

    // Create the fonts we need
    Font fontBold = workbook.createFont();
    fontBold.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Create the styles we need
    XSSFCellStyle summaryDataStyle = sheet.getWorkbook().createCellStyle();
    summaryDataStyle.setAlignment(HorizontalAlignment.RIGHT);
    XSSFCellStyle headerStyle = sheet.getWorkbook().createCellStyle();
    headerStyle.setAlignment(HorizontalAlignment.RIGHT);
    headerStyle.setFont(fontBold);

    XSSFRow row = null;
    int rowCount = 0;
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("Name:");
        row.getCell(0).setCellStyle(headerStyle);
        row.getCell(1).setCellValue(title);
        row.getCell(1).setCellStyle(summaryDataStyle);
    }
    row = sheet.createRow(rowCount++);
    {
        String description = (String) testRunObj.get(FIELD_DESCRIPTION);
        description = description == null ? "" : description;
        row.getCell(0).setCellValue("Description:");
        row.getCell(0).setCellStyle(headerStyle);
        row.getCell(1).setCellValue(description);
        row.getCell(1).setCellStyle(summaryDataStyle);
    }
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("Progress (%):");
        row.getCell(0).setCellStyle(headerStyle);
        Double progress = (Double) testRunObj.get(FIELD_PROGRESS);
        progress = progress == null ? 0.0 : progress;
        row.getCell(1).setCellValue(progress * 100);
        row.getCell(1).setCellType(XSSFCell.CELL_TYPE_NUMERIC);
        row.getCell(1).setCellStyle(summaryDataStyle);
    }
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("State:");
        row.getCell(0).setCellStyle(headerStyle);
        String state = (String) testRunObj.get(FIELD_STATE);
        if (state != null) {
            row.getCell(1).setCellValue(state);
            row.getCell(1).setCellStyle(summaryDataStyle);
        }
    }
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("Started:");
        row.getCell(0).setCellStyle(headerStyle);
        Long time = (Long) testRunObj.get(FIELD_STARTED);
        if (time > 0) {
            row.getCell(1).setCellValue(FastDateFormat
                    .getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM).format(time));
            row.getCell(1).setCellStyle(summaryDataStyle);
        }
    }
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("Finished:");
        row.getCell(0).setCellStyle(headerStyle);
        Long time = (Long) testRunObj.get(FIELD_COMPLETED);
        if (time > 0) {
            row.getCell(1).setCellValue(FastDateFormat
                    .getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM).format(time));
            row.getCell(1).setCellStyle(summaryDataStyle);
        }
    }
    row = sheet.createRow(rowCount++);
    {
        row.getCell(0).setCellValue("Duration:");
        row.getCell(0).setCellStyle(headerStyle);
        Long time = (Long) testRunObj.get(FIELD_DURATION);
        if (time > 0) {
            row.getCell(1).setCellValue(DurationFormatUtils.formatDurationHMS(time));
            row.getCell(1).setCellStyle(summaryDataStyle);
        }
    }

    rowCount++;
    rowCount++;
    // Create a header row
    row = sheet.createRow(rowCount++); // Header row
    String[] headers = new String[] { "Event Name", "Total Count", "Success Count", "Failure Count",
            "Success Rate (%)", "Min (ms)", "Max (ms)", "Arithmetic Mean (ms)", "Standard Deviation (ms)" };
    int columnCount = 0;
    for (String header : headers) {
        XSSFCell cell = row.getCell(columnCount++);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(header);
    }
    // Grab results and output them
    columnCount = 0;
    TreeMap<String, ResultSummary> summaries = collateResults(true);
    for (Map.Entry<String, ResultSummary> entry : summaries.entrySet()) {
        // Reset column count
        columnCount = 0;

        row = sheet.createRow(rowCount++);
        String eventName = entry.getKey();
        ResultSummary summary = entry.getValue();
        SummaryStatistics statsSuccess = summary.getStats(true);
        SummaryStatistics statsFail = summary.getStats(false);
        // Event Name
        row.getCell(columnCount++).setCellValue(eventName);
        // Total Count
        row.getCell(columnCount++).setCellValue(summary.getTotalResults());
        // Success Count
        row.getCell(columnCount++).setCellValue(statsSuccess.getN());
        // Failure Count
        row.getCell(columnCount++).setCellValue(statsFail.getN());
        // Success Rate (%)
        row.getCell(columnCount++).setCellValue(summary.getSuccessPercentage());
        // Min (ms)
        row.getCell(columnCount++).setCellValue((long) statsSuccess.getMin());
        // Max (ms)
        row.getCell(columnCount++).setCellValue((long) statsSuccess.getMax());
        // Arithmetic Mean (ms)
        row.getCell(columnCount++).setCellValue((long) statsSuccess.getMean());
        // Standard Deviation (ms)
        row.getCell(columnCount++).setCellValue((long) statsSuccess.getStandardDeviation());
    }

    // Auto-size the columns
    for (int i = 0; i < 10; i++) {
        sheet.autoSizeColumn(i);
    }
    sheet.setColumnWidth(1, 5120);

    // Printing
    PrintSetup ps = sheet.getPrintSetup();
    sheet.setAutobreaks(true);
    ps.setFitWidth((short) 1);
    ps.setLandscape(true);

    // Header and footer
    sheet.getHeader().setCenter(title);
}

From source file:org.apache.cassandra.dht.tokenallocator.TokenAllocation.java

public static String statToString(SummaryStatistics stat) {
    return String.format("max %.2f min %.2f stddev %.4f", stat.getMax() / stat.getMean(),
            stat.getMin() / stat.getMean(), stat.getStandardDeviation());
}

From source file:org.apache.solr.client.solrj.io.eval.HistogramEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {
    if (Arrays.stream(values).anyMatch(item -> null == item)) {
        return null;
    }/*  ww  w. j  a  va  2  s.c  o  m*/

    List<?> sourceValues;
    Integer bins = 10;

    if (values.length >= 1) {
        sourceValues = values[0] instanceof List<?> ? (List<?>) values[0] : Arrays.asList(values[0]);

        if (values.length >= 2) {
            if (values[1] instanceof Number) {
                bins = ((Number) values[1]).intValue();
            } else {
                throw new IOException(String.format(Locale.ROOT,
                        "Invalid expression %s - if second parameter is provided then it must be a valid number but found %s instead",
                        toExpression(constructingFactory), values[1].getClass().getSimpleName()));
            }
        }
    } else {
        throw new IOException(
                String.format(Locale.ROOT, "Invalid expression %s - expecting at least one value but found %d",
                        toExpression(constructingFactory), containedEvaluators.size()));
    }

    EmpiricalDistribution distribution = new EmpiricalDistribution(bins);
    distribution.load(
            ((List<?>) sourceValues).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());
    ;

    List<Tuple> histogramBins = new ArrayList<>();
    for (SummaryStatistics binSummary : distribution.getBinStats()) {
        Map<String, Number> map = new HashMap<>();
        map.put("max", binSummary.getMax());
        map.put("mean", binSummary.getMean());
        map.put("min", binSummary.getMin());
        map.put("stdev", binSummary.getStandardDeviation());
        map.put("sum", binSummary.getSum());
        map.put("N", binSummary.getN());
        map.put("var", binSummary.getVariance());
        map.put("cumProb", distribution.cumulativeProbability(binSummary.getMean()));
        map.put("prob", distribution.probability(binSummary.getMin(), binSummary.getMax()));
        histogramBins.add(new Tuple(map));
    }

    return histogramBins;
}

From source file:org.apache.solr.cloud.autoscaling.sim.TestLargeCluster.java

public void benchmarkNodeLost() throws Exception {
    List<String> results = new ArrayList<>();
    for (int wait : renard5x) {
        for (int delay : renard5x) {
            SummaryStatistics totalTime = new SummaryStatistics();
            SummaryStatistics ignoredOurEvents = new SummaryStatistics();
            SummaryStatistics ignoredOtherEvents = new SummaryStatistics();
            SummaryStatistics startedOurEvents = new SummaryStatistics();
            SummaryStatistics startedOtherEvents = new SummaryStatistics();
            for (int i = 0; i < 5; i++) {
                if (cluster != null) {
                    cluster.close();//from   www .  j a  va 2 s.  c o  m
                }
                setupCluster();
                setUp();
                setupTest();
                long total = doTestNodeLost(wait, delay * 1000, 0);
                totalTime.addValue(total);
                // get event counts
                Map<String, Map<String, AtomicInteger>> counts = cluster.simGetEventCounts();
                Map<String, AtomicInteger> map = counts.remove("node_lost_trigger");
                startedOurEvents.addValue(map.getOrDefault("STARTED", ZERO).get());
                ignoredOurEvents.addValue(map.getOrDefault("IGNORED", ZERO).get());
                int otherStarted = 0;
                int otherIgnored = 0;
                for (Map<String, AtomicInteger> m : counts.values()) {
                    otherStarted += m.getOrDefault("STARTED", ZERO).get();
                    otherIgnored += m.getOrDefault("IGNORED", ZERO).get();
                }
                startedOtherEvents.addValue(otherStarted);
                ignoredOtherEvents.addValue(otherIgnored);
            }
            results.add(String.format(Locale.ROOT,
                    "%d\t%d\t%4.0f\t%4.0f\t%4.0f\t%4.0f\t%6.0f\t%6.0f\t%6.0f\t%6.0f\t%6.0f", wait, delay,
                    startedOurEvents.getMean(), ignoredOurEvents.getMean(), startedOtherEvents.getMean(),
                    ignoredOtherEvents.getMean(), totalTime.getMin(), totalTime.getMax(), totalTime.getMean(),
                    totalTime.getStandardDeviation(), totalTime.getVariance()));
        }
    }
    log.info("===== RESULTS ======");
    log.info("waitFor\tdelay\tSTRT\tIGN\toSTRT\toIGN\tmin\tmax\tmean\tstdev\tvar");
    results.forEach(s -> log.info(s));
}

From source file:org.apache.tika.eval.AbstractProfiler.java

/**
 * Checks to see if metadata is null or content is empty (null or only whitespace).
 * If any of these, then this does no processing, and the fileId is not
 * entered into the content table.//from ww  w. j av a2  s. co m
 *
 * @param fileId
 * @param m
 * @param fieldName
 * @param contentsTable
 */
protected void writeContentData(String fileId, Metadata m, String fieldName, TableInfo contentsTable)
        throws IOException {
    if (m == null) {
        return;
    }
    Map<Cols, String> data = new HashMap<>();
    String content = getContent(m, maxContentLength, data);
    if (content == null || content.trim().length() == 0) {
        return;
    }
    tokenCounter.clear(fieldName);
    tokenCounter.add(fieldName, content);

    data.put(Cols.ID, fileId);
    data.put(Cols.CONTENT_LENGTH, Integer.toString(content.length()));
    langid(m, data);
    String langid = data.get(Cols.LANG_ID_1);
    langid = (langid == null) ? "" : langid;

    writeTokenCounts(data, fieldName, tokenCounter);
    CommonTokenResult commonTokenResult = null;
    try {
        commonTokenResult = commonTokenCountManager.countTokenOverlaps(langid,
                tokenCounter.getTokens(fieldName));
    } catch (IOException e) {
        LOG.error("{}", e.getMessage(), e);
    }
    data.put(Cols.COMMON_TOKENS_LANG, commonTokenResult.getLangCode());
    data.put(Cols.NUM_COMMON_TOKENS, Integer.toString(commonTokenResult.getCommonTokens()));
    TokenStatistics tokenStatistics = tokenCounter.getTokenStatistics(fieldName);
    data.put(Cols.NUM_UNIQUE_TOKENS, Integer.toString(tokenStatistics.getTotalUniqueTokens()));
    data.put(Cols.NUM_TOKENS, Integer.toString(tokenStatistics.getTotalTokens()));
    data.put(Cols.NUM_ALPHABETIC_TOKENS, Integer.toString(commonTokenResult.getAlphabeticTokens()));

    data.put(Cols.TOKEN_ENTROPY_RATE, Double.toString(tokenStatistics.getEntropy()));
    SummaryStatistics summStats = tokenStatistics.getSummaryStatistics();
    data.put(Cols.TOKEN_LENGTH_SUM, Integer.toString((int) summStats.getSum()));

    data.put(Cols.TOKEN_LENGTH_MEAN, Double.toString(summStats.getMean()));

    data.put(Cols.TOKEN_LENGTH_STD_DEV, Double.toString(summStats.getStandardDeviation()));
    unicodeBlocks(m, data);
    try {
        writer.writeRow(contentsTable, data);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.tika.eval.tokens.TokenStatistics.java

@Override
public boolean equals(Object o) {

    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;

    TokenStatistics that = (TokenStatistics) o;

    if (totalTokens != that.totalTokens)
        return false;
    if (totalUniqueTokens != that.totalUniqueTokens)
        return false;
    if (!doubleEquals(that.entropy, entropy))
        return false;
    // Probably incorrect - comparing Object[] arrays with Arrays.equals
    if (!Arrays.equals(topN, that.topN))
        return false;

    SummaryStatistics thatS = ((TokenStatistics) o).summaryStatistics;
    if (summaryStatistics.getN() != thatS.getN())
        return false;

    //if both have n==0, don't bother with the stats
    if (summaryStatistics.getN() == 0L)
        return true;
    //TODO: consider adding others...
    if (!doubleEquals(summaryStatistics.getGeometricMean(), thatS.getGeometricMean()))
        return false;
    if (!doubleEquals(summaryStatistics.getMax(), thatS.getMax()))
        return false;
    if (!doubleEquals(summaryStatistics.getMean(), thatS.getMean()))
        return false;
    if (!doubleEquals(summaryStatistics.getMin(), thatS.getMin()))
        return false;
    if (!doubleEquals(summaryStatistics.getSum(), thatS.getSum()))
        return false;
    if (!doubleEquals(summaryStatistics.getStandardDeviation(), thatS.getStandardDeviation()))
        return false;
    return true;/*from w  ww.  ja  va2s .c  om*/
}

From source file:org.apereo.portal.events.aggr.stat.JpaStatisticalSummary.java

/**
 * Returns true iff <code>object</code> is a
 * <code>SummaryStatistics</code> instance and all statistics have the
 * same values as this.//from w w w  .ja va  2s  .c  om
 * @param object the object to test equality against.
 * @return true if object equals this
 */
@Override
public boolean equals(Object object) {
    if (object == this) {
        return true;
    }
    if (object instanceof SummaryStatistics == false) {
        return false;
    }
    SummaryStatistics stat = (SummaryStatistics) object;
    return Precision.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean())
            && Precision.equalsIncludingNaN(stat.getMax(), getMax())
            && Precision.equalsIncludingNaN(stat.getMean(), getMean())
            && Precision.equalsIncludingNaN(stat.getMin(), getMin())
            && Precision.equalsIncludingNaN(stat.getN(), getN())
            && Precision.equalsIncludingNaN(stat.getSum(), getSum())
            && Precision.equalsIncludingNaN(stat.getSumsq(), getSumsq())
            && Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
}