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

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

Introduction

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

Prototype

public void addValue(double value) 

Source Link

Document

Add a value to the data

Usage

From source file:RealFunctionValidation.java

public static SummaryStatistics assessAccuracy(final Method method, final DataInputStream in,
        final DataOutputStream out)
        throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    if (method.getReturnType() != Double.TYPE) {
        throw new IllegalArgumentException("method must return a double");
    }/*from   w w w .j  a va 2s.c o  m*/

    final Class<?>[] types = method.getParameterTypes();
    for (int i = 0; i < types.length; i++) {
        if (!types[i].isPrimitive()) {
            final StringBuilder builder = new StringBuilder();
            builder.append("argument #").append(i + 1).append(" of method ").append(method.getName())
                    .append("must be of primitive of type");
            throw new IllegalArgumentException(builder.toString());
        }
    }

    final SummaryStatistics stat = new SummaryStatistics();
    final Object[] parameters = new Object[types.length];
    while (true) {
        try {
            for (int i = 0; i < parameters.length; i++) {
                parameters[i] = readAndWritePrimitiveValue(in, out, types[i]);
            }
            final double expected = in.readDouble();
            if (FastMath.abs(expected) > 1E-16) {
                final Object value = method.invoke(null, parameters);
                final double actual = ((Double) value).doubleValue();
                final double err = FastMath.abs(actual - expected);
                final double ulps = err / FastMath.ulp(expected);
                out.writeDouble(expected);
                out.writeDouble(actual);
                out.writeDouble(ulps);
                stat.addValue(ulps);
            }
        } catch (EOFException e) {
            break;
        }
    }
    return stat;
}

From source file:io.fabric8.example.variance.http.VarianceProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    String message = exchange.getIn().getBody(String.class);
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    List<Double> values = objectMapper.readValue(message,
            typeFactory.constructCollectionType(List.class, Double.class));
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(message, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }//from   w ww . j  av  a 2s  . co m
    String variance = Double.toString(summaryStatistics.getVariance());
    exchange.getOut().setBody(variance);
}

From source file:io.fabric8.example.stddev.http.StdDevProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    String message = exchange.getIn().getBody(String.class);
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    List<Double> values = objectMapper.readValue(message,
            typeFactory.constructCollectionType(List.class, Double.class));
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(message, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }// w ww.j  a v a2 s  .  c  o m
    String stdDev = Double.toString(summaryStatistics.getStandardDeviation());
    exchange.getOut().setBody(stdDev);
}

From source file:com.github.rinde.rinsim.util.StochasticSuppliersTest.java

/**
 * Tests whether the rescaling of the mean of a truncated normal distribution
 * is implemented correctly.//from   w ww  .  j  a  va 2s.  co m
 */
@Test
public void testNormalScaleMean() {
    final double[] means = new double[] { 1d, 2d, 3d, 10d, 100d };
    final double[] sds = new double[] { 1d, 1d, 3d, 5d, 100d };

    for (int i = 0; i < means.length; i++) {
        final StochasticSupplier<Double> ss = StochasticSuppliers.normal().mean(means[i]).std(sds[i])
                .lowerBound(0).scaleMean().redrawWhenOutOfBounds().buildDouble();

        final RandomGenerator rng = new MersenneTwister(123);
        final SummaryStatistics stats = new SummaryStatistics();
        for (int j = 0; j < 10000; j++) {
            stats.addValue(ss.get(rng.nextLong()));
        }
        // 1 % deviation from mean is acceptable
        final double allowedDeviation = 0.01 * means[i];
        assertEquals(means[i], stats.getMean(), allowedDeviation);
    }

}

From source file:io.fabric8.example.stddev.http.StdDevProcessorTest.java

@Test
public void testProcess() throws Exception {
    RandomGenerator rg = new JDKRandomGenerator();
    double[] array = new double[10];
    ObjectMapper objectMapper = new ObjectMapper();
    for (int i = 0; i < array.length; i++) {
        array[i] = rg.nextDouble();/*from   w w  w . j  a  va 2  s  .c o  m*/
    }
    String body = objectMapper.writeValueAsString(array);
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(body, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }
    String stdDev = Double.toString(summaryStatistics.getStandardDeviation());

    resultEndpoint.expectedBodiesReceived(stdDev);

    template.sendBody(body);

    resultEndpoint.assertIsSatisfied();
}

From source file:io.fabric8.example.variance.http.VarianceProcessorTest.java

@Test
public void testProcess() throws Exception {
    RandomGenerator rg = new JDKRandomGenerator();
    double[] array = new double[10];
    ObjectMapper objectMapper = new ObjectMapper();
    for (int i = 0; i < array.length; i++) {
        array[i] = rg.nextDouble();/*  w w  w  .  j  av a  2 s  .  c o  m*/
    }
    String body = objectMapper.writeValueAsString(array);
    String expectedBody = "0.0";
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(body, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }
    String variance = Double.toString(summaryStatistics.getVariance());

    resultEndpoint.expectedBodiesReceived(variance);

    template.sendBody(body);

    resultEndpoint.assertIsSatisfied();
}

From source file:io.fabric8.example.stddev.msg.StdDevProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    System.err.println("STD DEV GOT EXCHANGE " + exchange);
    String message = exchange.getIn().getBody(String.class);
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    List<Double> values = objectMapper.readValue(message,
            typeFactory.constructCollectionType(List.class, Double.class));
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(message, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }/*w  w w.j  a  v  a2 s  .  co m*/
    String stdDev = Double.toString(summaryStatistics.getStandardDeviation());

    ActiveMQDestination replyTo = exchange.getIn().getHeader("JMSReplyTo", ActiveMQDestination.class);
    final String messageId = exchange.getIn().getHeader("JMSMessageID", String.class);

    if (replyTo != null) {
        Exchange copy = new DefaultExchange(exchange);
        copy.setPattern(ExchangePattern.InOnly);
        copy.getIn().setHeader(Variables.CORRELATION_HEADER, messageId);
        copy.getIn().setBody(stdDev);
        producerTemplate.send("jms:queue:" + replyTo.getPhysicalName(), copy);
    }
}

From source file:io.fabric8.example.variance.msg.VarianceProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    System.err.println("VARIANCE GOT EXCHANGE " + exchange);
    String message = exchange.getIn().getBody(String.class);
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    List<Double> values = objectMapper.readValue(message,
            typeFactory.constructCollectionType(List.class, Double.class));
    SummaryStatistics summaryStatistics = new SummaryStatistics();
    List<Double> list = new ObjectMapper().readValue(message, List.class);
    for (Double value : list) {
        summaryStatistics.addValue(value);
    }/*from   w w  w.j  a  v  a  2s  .co  m*/
    String variance = Double.toString(summaryStatistics.getVariance());
    ActiveMQDestination replyTo = exchange.getIn().getHeader("JMSReplyTo", ActiveMQDestination.class);
    final String messageId = exchange.getIn().getHeader("JMSMessageID", String.class);

    if (replyTo != null) {
        Exchange copy = new DefaultExchange(exchange);
        copy.setPattern(ExchangePattern.InOnly);
        copy.getIn().setHeader(Variables.CORRELATION_HEADER, messageId);
        copy.getIn().setBody(variance);
        producerTemplate.send("jms:queue:" + replyTo.getPhysicalName(), copy);
        System.err.println("REPLIED TO " + "jms:queue:" + replyTo.getPhysicalName());
    } else {
        System.err.println("REPLOY NOT SET for exchange: " + exchange.getIn().getHeaders());
    }
}

From source file:net.recommenders.rival.evaluation.statistics.StandardError.java

/**
 * Implements equation (8.13) from "Elementary Statistics: A Problem Solving
 * Approach 4th Edition", Andrew L. Comrey, Howard B. Lee
 *
 * @return the standard error as the ratio of the standard deviation divided
 * by the sqrt(number of users) of the distribution of difference scores.
 *//*  w  ww .  j a va 2  s.co  m*/
public double getStandardError() {
    Set<V> overlap = new HashSet<V>(baselineMetricPerDimension.keySet());
    overlap.retainAll(testMetricPerDimension.keySet());

    // paired or matched samples --> analyse distribution of difference scores
    SummaryStatistics differences = new SummaryStatistics();
    for (V key : overlap) {
        double diff = baselineMetricPerDimension.get(key) - testMetricPerDimension.get(key);
        differences.addValue(diff);
    }

    double e = differences.getStandardDeviation() / Math.sqrt(differences.getN());
    return e;
}

From source file:eu.crydee.alignment.aligner.ae.MetricsSummaryAE.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    try {//from   w w  w  .j  ava2s  .com
        String template = IOUtils.toString(getClass()
                .getResourceAsStream("/eu/crydee/alignment/aligner/ae/" + "metrics-summarizer-template.html"));
        String titledTemplate = template.replace("@@TITLE@@",
                "Metrics summarizer" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        StringBuilder sb = new StringBuilder();
        sb.append("<table class=\"table table-striped ").append("table-condensed\">\n")
                .append("            <thead>\n").append("                <tr>\n")
                .append("                    <th>City\\Metric</th>\n");
        for (String key : keys) {
            sb.append("                    <th>").append(methodsMetadata.get(key).getRight()).append("</th>\n");
        }
        sb.append("                <tr>\n").append("            </thead>\n").append("            <tbody>\n");
        for (String ele : results.rowKeySet()) {
            sb.append("                <tr>\n").append("                    <td>").append(ele)
                    .append("</td>\n");
            Map<String, Samples> metricResults = results.row(ele);
            for (String key : keys) {
                Samples samples = metricResults.get(key);
                SummaryStatistics ss = new SummaryStatistics();
                samples.samples.forEach(d -> ss.addValue(d));
                double mean = ss.getMean();
                boolean significant = TestUtils.tTest(samples.mu,
                        ArrayUtils.toPrimitive(samples.samples.toArray(new Double[0])), 0.05),
                        above = samples.mu > mean;
                String summary = String.format("%.3f", samples.mu) + " <small class=\"text-muted\">"
                        + String.format("%.3f", ss.getMean()) + ""
                        + String.format("%.3f", ss.getStandardDeviation()) + "</small>";
                logger.info(ele + "\t" + key + "\t" + summary + "\t" + significant);
                sb.append("                    <td class=\"")
                        .append(significant ? (above ? "success" : "danger") : "warning").append("\">")
                        .append(summary).append("</td>\n");
            }
            sb.append("                </tr>\n");
        }
        sb.append("            </tbody>\n").append("        </table>");
        FileUtils.write(new File(htmlFilepath), titledTemplate.replace("@@TABLE@@", sb.toString()),
                StandardCharsets.UTF_8);
    } catch (IOException ex) {
        logger.error("IO problem with the HTML output.");
        throw new AnalysisEngineProcessException(ex);
    }
}