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

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

Introduction

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

Prototype

double getMean();

Source Link

Document

Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> arithmetic mean </a> of the available values

Usage

From source file:com.github.christofluyten.experiment.MeasureGendreau.java

/**
 * @param args//from  w w w. j a v  a2s .  c o m
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    final List<Gendreau06Scenario> scns = new ArrayList<>(
            Gendreau06Parser.parser().addDirectory("files/gendreau2006/requests").parse());

    Collections.sort(scns, new Comparator<Gendreau06Scenario>() {
        @Override
        public int compare(Gendreau06Scenario o1, Gendreau06Scenario o2) {
            final int compare = o1.getProblemClass().getId().compareTo(o2.getProblemClass().getId());
            if (compare == 0) {
                return o1.getProblemInstanceId().compareTo(o2.getProblemInstanceId());
            }
            return compare;
        }
    });

    final List<Map<Property, Object>> propsList = new ArrayList<>();
    for (final Gendreau06Scenario scen : scns) {
        final StatisticalSummary urgency = Metrics.measureUrgency(scen);
        final Multiset<Class<?>> counts = Metrics.getEventTypeCounts(scen);

        final long scenarioLength = scen.getProblemClass().duration * 60000;
        final double dyn = Metrics.measureDynamism(scen, scenarioLength);

        final ImmutableMap<Property, Object> prop = ImmutableMap.<Property, Object>builder()
                .put(Property.PROBLEM_CLASS, scen.getProblemClass().getId())
                .put(Property.INSTANCE_ID, scen.getProblemInstanceId()).put(Property.DYNAMISM, dyn)
                .put(Property.URGENCY_MEAN, urgency.getMean() / 60000d)
                .put(Property.URGENCY_SD, urgency.getStandardDeviation() / 60000d)
                .put(Property.NUM_ORDERS, counts.count(AddParcelEvent.class))
                .put(Property.NUM_VEHICLES, counts.count(AddVehicleEvent.class))
                .putAll(MAP.get(scen.getProblemInstanceId() + scen.getProblemClass().getId())).build();

        propsList.add(prop);
    }

    final File targetFile = new File(PROPS_FILE);
    write(propsList, targetFile, asList(Property.values()));
    System.out.println("Results written to " + targetFile.getAbsolutePath());
}

From source file:joinery.impl.Aggregation.java

@SuppressWarnings("unchecked")
public static <V> DataFrame<V> describe(final DataFrame<V> df) {
    final DataFrame<V> desc = new DataFrame<>();
    for (final Object col : df.columns()) {
        for (final Object row : df.index()) {
            final V value = df.get(row, col);
            if (value instanceof StatisticalSummary) {
                if (!desc.columns().contains(col)) {
                    desc.add(col);//w  w w . j  a  v a2 s .c o m
                    if (desc.isEmpty()) {
                        for (final Object r : df.index()) {
                            for (final Object stat : Arrays.asList("count", "mean", "std", "var", "max",
                                    "min")) {
                                final Object name = name(df, r, stat);
                                desc.append(name, Collections.<V>emptyList());
                            }
                        }
                    }
                }

                final StatisticalSummary summary = StatisticalSummary.class.cast(value);
                desc.set(name(df, row, "count"), col, (V) new Double(summary.getN()));
                desc.set(name(df, row, "mean"), col, (V) new Double(summary.getMean()));
                desc.set(name(df, row, "std"), col, (V) new Double(summary.getStandardDeviation()));
                desc.set(name(df, row, "var"), col, (V) new Double(summary.getVariance()));
                desc.set(name(df, row, "max"), col, (V) new Double(summary.getMax()));
                desc.set(name(df, row, "min"), col, (V) new Double(summary.getMin()));
            }
        }
    }
    return desc;
}

From source file:com.github.rinde.dynurg.Generator.java

static void createScenarios(RandomGenerator rng, GeneratorSettings generatorSettings,
        ScenarioGenerator generator, double dynLb, double dynUb, int levels) {
    final List<Scenario> scenarios = newArrayList();

    final Multimap<Double, Scenario> dynamismScenariosMap = LinkedHashMultimap.create();
    while (scenarios.size() < levels * TARGET_NUM_INSTANCES) {
        final Scenario scen = generator.generate(rng, "temp");
        Metrics.checkTimeWindowStrictness(scen);
        final StatisticalSummary urgency = Metrics.measureUrgency(scen);

        final long expectedUrgency = generatorSettings.urgency * 60000L;
        if (Math.abs(urgency.getMean() - expectedUrgency) < 0.01 && urgency.getStandardDeviation() < 0.01) {

            final int numParcels = Metrics.getEventTypeCounts(scen).count(PDPScenarioEvent.ADD_PARCEL);
            if (numParcels == NUM_ORDERS) {

                final double dynamism = Metrics.measureDynamism(scen, generatorSettings.officeHours);
                System.out.print(String.format("%1.3f ", dynamism));
                if ((dynamism % DYN_STEP_SIZE < DYN_BANDWIDTH
                        || dynamism % DYN_STEP_SIZE > DYN_STEP_SIZE - DYN_BANDWIDTH) && dynamism <= dynUb
                        && dynamism >= dynLb) {

                    final double targetDyn = Math.round(dynamism / DYN_STEP_SIZE) * DYN_STEP_SIZE;

                    final int numInstances = dynamismScenariosMap.get(targetDyn).size();

                    if (numInstances < TARGET_NUM_INSTANCES) {

                        final String instanceId = "#" + Integer.toString(numInstances);
                        dynamismScenariosMap.put(targetDyn, scen);

                        final String problemClassId = String.format("%d-%1.2f",
                                (long) (urgency.getMean() / 60000), targetDyn);
                        System.out.println();
                        System.out.println(" > ACCEPT " + problemClassId);
                        final String fileName = DATASET_DIR + problemClassId + instanceId;
                        try {
                            Files.createParentDirs(new File(fileName));
                            writePropertiesFile(scen, urgency, dynamism, problemClassId, instanceId,
                                    generatorSettings, fileName);
                            MetricsIO.writeLocationList(Metrics.getServicePoints(scen),
                                    new File(fileName + ".points"));
                            MetricsIO.writeTimes(scen.getTimeWindow().end, Metrics.getArrivalTimes(scen),
                                    new File(fileName + ".times"));

                            final ProblemClass pc = new SimpleProblemClass(problemClassId);
                            final Scenario finalScenario = Scenario.builder(pc).copyProperties(scen)
                                    .problemClass(pc).instanceId(instanceId).build();

                            ScenarioIO.write(finalScenario, new File(fileName + ".scen").toPath());
                        } catch (final IOException e) {
                            throw new IllegalStateException(e);
                        }/*  www. j  a  v  a 2  s . c  om*/
                        scenarios.add(scen);
                    }
                }
            }
        }
    }
}

From source file:com.github.rinde.dynurg.Generator.java

static void writePropertiesFile(Scenario scen, StatisticalSummary urgency, double dynamism,
        String problemClassId, String instanceId, GeneratorSettings settings, String fileName) {
    final DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecondMillis();

    final ImmutableMap.Builder<String, Object> properties = ImmutableMap.<String, Object>builder()
            .put("problem_class", problemClassId).put("id", instanceId).put("dynamism", dynamism)
            .put("urgency_mean", urgency.getMean()).put("urgency_sd", urgency.getStandardDeviation())
            .put("creation_date", formatter.print(System.currentTimeMillis()))
            .put("creator", System.getProperty("user.name")).put("day_length", settings.dayLength)
            .put("office_opening_hours", settings.officeHours);

    properties.putAll(settings.properties);

    final ImmutableMultiset<Enum<?>> eventTypes = Metrics.getEventTypeCounts(scen);
    for (final Multiset.Entry<Enum<?>> en : eventTypes.entrySet()) {
        properties.put(en.getElement().name(), en.getCount());
    }// ww w  .j ava  2 s . c om

    try {
        Files.write(Joiner.on("\n").withKeyValueSeparator(" = ").join(properties.build()),
                new File(fileName + ".properties"), Charsets.UTF_8);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.github.rinde.datgen.pdptw.ScenarioCreator.java

@Nullable
@Override/* w  w w.  j a  va 2s .  c o  m*/
public GeneratedScenario call() throws Exception {
    final RandomGenerator rng = new MersenneTwister(getSeed());
    final Scenario scen = getGenerator().generate(rng, Long.toString(getId()));

    Metrics.checkTimeWindowStrictness(scen);

    // check that urgency matches expected urgency
    final StatisticalSummary urgency = Metrics.measureUrgency(scen);
    final long expectedUrgency = getSettings().getUrgency();
    if (!(Math.abs(urgency.getMean() - expectedUrgency) < URGENCY_THRESHOLD
            && urgency.getStandardDeviation() < URGENCY_THRESHOLD)) {
        System.out.println("Urgency too strict?");
        return null;
    }

    // check num orders
    final int numParcels = Metrics.getEventTypeCounts(scen).count(AddParcelEvent.class);
    if (numParcels != getSettings().getNumOrders()) {
        System.out.println("Parcels wrong number!");
        return null;
    }

    // check if dynamism fits in a bin
    final double dynamism = Metrics.measureDynamism(scen, getSettings().getOfficeHours());
    @Nullable
    final Double dynamismBin = getSettings().getDynamismRangeCenters().get(dynamism);
    if (dynamismBin == null) {
        System.out.println("Dynamism " + dynamism + " is too strict");
        return null;
    }

    return GeneratedScenario.create(scen, getSettings(), getId(), getSeed(), dynamismBin, dynamism);
}

From source file:embedded2.ESecure.TTest.java

/**
 * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">
 * t statistic </a> to use in comparing the mean of the dataset described by
 * <code>sampleStats</code> to <code>mu</code>.
 * <p>/*from   ww w . j a  v  a2  s.  co  m*/
 * This statistic can be used to perform a one sample t-test for the mean.
 * </p><p>
 * <strong>Preconditions</strong>: <ul>
 * <li><code>observed.getN() &ge; 2</code>.
 * </li></ul></p>
 *
 * @param mu comparison constant
 * @param sampleStats DescriptiveStatistics holding sample summary statitstics
 * @return t statistic
 * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
 * @throws NumberIsTooSmallException if the number of samples is &lt; 2
 */
public static double t(final double mu, final StatisticalSummary sampleStats)
        throws NullArgumentException, NumberIsTooSmallException {

    checkSampleData(sampleStats);
    return t(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN());

}

From source file:embedded2.ESecure.TTest.java

/**
 * Returns the <i>observed significance level</i>, or
 * <i>p-value</i>, associated with a one-sample, two-tailed t-test
 * comparing the mean of the dataset described by <code>sampleStats</code>
 * with the constant <code>mu</code>.
 * <p>/*from ww w.  ja  v a 2  s . c o  m*/
 * The number returned is the smallest significance level
 * at which one can reject the null hypothesis that the mean equals
 * <code>mu</code> in favor of the two-sided alternative that the mean
 * is different from <code>mu</code>. For a one-sided test, divide the
 * returned value by 2.</p>
 * <p>
 * <strong>Usage Note:</strong><br>
 * The validity of the test depends on the assumptions of the parametric
 * t-test procedure, as discussed
 * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
 * here</a></p>
 * <p>
 * <strong>Preconditions</strong>: <ul>
 * <li>The sample must contain at least 2 observations.
 * </li></ul></p>
 *
 * @param mu constant value to compare sample mean against
 * @param sampleStats StatisticalSummary describing sample data
 * @return p-value
 * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
 * @throws NumberIsTooSmallException if the number of samples is &lt; 2
 * @throws MaxCountExceededException if an error occurs computing the p-value
 */
public static double tTest(final double mu, final StatisticalSummary sampleStats)
        throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException {

    checkSampleData(sampleStats);
    return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN());

}

From source file:embedded2.ESecure.TTest.java

/**
 * Computes a 2-sample t statistic </a>, comparing the means of the datasets
 * described by two {@link StatisticalSummary} instances, without the
 * assumption of equal subpopulation variances.  Use
 * {@link #homoscedasticT(StatisticalSummary, StatisticalSummary)} to
 * compute a t-statistic under the equal variances assumption.
 * <p>//from   w  ww  .j a v  a  2 s  . c  om
 * This statistic can be used to perform a two-sample t-test to compare
 * sample means.</p>
 * <p>
 * The returned  t-statistic is</p>
 * <p>
 * &nbsp;&nbsp; <code>  t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code>
 * </p><p>
 * where <strong><code>n1</code></strong> is the size of the first sample;
 * <strong><code> n2</code></strong> is the size of the second sample;
 * <strong><code> m1</code></strong> is the mean of the first sample;
 * <strong><code> m2</code></strong> is the mean of the second sample
 * <strong><code> var1</code></strong> is the variance of the first sample;
 * <strong><code> var2</code></strong> is the variance of the second sample
 * </p><p>
 * <strong>Preconditions</strong>: <ul>
 * <li>The datasets described by the two Univariates must each contain
 * at least 2 observations.
 * </li></ul></p>
 *
 * @param sampleStats1 StatisticalSummary describing data from the first sample
 * @param sampleStats2 StatisticalSummary describing data from the second sample
 * @return t statistic
 * @throws NullArgumentException if the sample statistics are <code>null</code>
 * @throws NumberIsTooSmallException if the number of samples is &lt; 2
 */
public static double t(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2)
        throws NullArgumentException, NumberIsTooSmallException {

    checkSampleData(sampleStats1);
    checkSampleData(sampleStats2);
    return t(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(),
            sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN());

}

From source file:embedded2.ESecure.TTest.java

/**
 * Computes a 2-sample t statistic, comparing the means of the datasets
 * described by two {@link StatisticalSummary} instances, under the
 * assumption of equal subpopulation variances.  To compute a t-statistic
 * without the equal variances assumption, use
 * {@link #t(StatisticalSummary, StatisticalSummary)}.
 * <p>/*from w w  w  . j  a  v a2  s .  c  om*/
 * This statistic can be used to perform a (homoscedastic) two-sample
 * t-test to compare sample means.</p>
 * <p>
 * The t-statistic returned is</p>
 * <p>
 * &nbsp;&nbsp;<code>  t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code>
 * </p><p>
 * where <strong><code>n1</code></strong> is the size of first sample;
 * <strong><code> n2</code></strong> is the size of second sample;
 * <strong><code> m1</code></strong> is the mean of first sample;
 * <strong><code> m2</code></strong> is the mean of second sample
 * and <strong><code>var</code></strong> is the pooled variance estimate:
 * </p><p>
 * <code>var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))</code>
 * </p><p>
 * with <strong><code>var1</code></strong> the variance of the first sample and
 * <strong><code>var2</code></strong> the variance of the second sample.
 * </p><p>
 * <strong>Preconditions</strong>: <ul>
 * <li>The datasets described by the two Univariates must each contain
 * at least 2 observations.
 * </li></ul></p>
 *
 * @param sampleStats1 StatisticalSummary describing data from the first sample
 * @param sampleStats2 StatisticalSummary describing data from the second sample
 * @return t statistic
 * @throws NullArgumentException if the sample statistics are <code>null</code>
 * @throws NumberIsTooSmallException if the number of samples is &lt; 2
 */
public static double homoscedasticT(final StatisticalSummary sampleStats1,
        final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException {

    checkSampleData(sampleStats1);
    checkSampleData(sampleStats2);
    return homoscedasticT(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(),
            sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN());

}

From source file:ijfx.core.stats.DefaultImageStatisticsService.java

@Override
public Map<String, Double> summaryStatisticsToMap(StatisticalSummary summaryStats) {

    Map<String, Double> statistics = new HashMap<>();
    statistics.put(LBL_MEAN, summaryStats.getMean());
    statistics.put(LBL_MIN, summaryStats.getMin());
    statistics.put(LBL_MAX, summaryStats.getMax());
    statistics.put(LBL_SD, summaryStats.getStandardDeviation());
    statistics.put(LBL_VARIANCE, summaryStats.getVariance());
    statistics.put(LBL_PIXEL_COUNT, (double) summaryStats.getN());
    return statistics;
}