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:eu.crydee.alignment.aligner.ae.MetricsOneVsOneC.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    try {//from   w  w w .j ava  2s  .c  o  m
        String template = IOUtils.toString(getClass()
                .getResourceAsStream("/eu/crydee/alignment/aligner/ae/" + "metrics-one-vs-one-template.html"));
        template = template.replace("@@TITLE@@",
                "Metrics comparator" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        template = template.replace("@@LEFTALGO@@", leftAlgoName);
        template = template.replace("@@RIGHTALGO@@", rightAlgoName);
        StringBuilder sb = new StringBuilder();
        sb.append("<table class=\"table table-condensed\">\n").append("            <thead>\n")
                .append("                <tr>\n").append("                    <th>Document\\Metric</th>\n");
        for (String key : keys) {
            sb.append("                    <th colspan=\"2\">").append(methodsMetadata.get(key).getRight())
                    .append("</th>\n");
        }
        sb.append("                <tr>\n").append("            </thead>\n").append("            <tbody>\n")
                .append("                <tr>\n").append("                    <td>\n")
                .append("                        <strong>Total</strong>\n")
                .append("                    </td>\n");
        for (String key : keys) {
            SummaryStatistics ss1 = new SummaryStatistics(), ss2 = new SummaryStatistics();
            List<Pair<Double, Double>> column = results.column(key).values().stream().peek(p -> {
                ss1.addValue(p.getLeft());
                ss2.addValue(p.getRight());
            }).collect(Collectors.toList());
            boolean significant = TestUtils.pairedTTest(column.stream().mapToDouble(p -> p.getLeft()).toArray(),
                    column.stream().mapToDouble(p -> p.getRight()).toArray(), 0.05);
            double mean1 = ss1.getMean(), mean2 = ss2.getMean();
            boolean above = mean1 > mean2;
            String summary1 = String.format("%.3f", mean1) + "<small class=\"text-muted\">" + ""
                    + String.format("%.3f", ss1.getStandardDeviation()) + "</small>",
                    summary2 = String.format("%.3f", mean2) + "<small class=\"text-muted\">" + ""
                            + String.format("%.3f", ss2.getStandardDeviation()) + "</small>";
            sb.append("                    <td class=\"")
                    .append(significant ? (above ? "success" : "danger") : "warning").append("\">")
                    .append(summary1).append("</td>\n");
            sb.append("                    <td class=\"")
                    .append(significant ? (!above ? "success" : "danger") : "warning").append("\">")
                    .append(summary2).append("</td>\n");
        }
        sb.append("                </tr>\n");
        SortedSet<String> rows = new TreeSet<>(results.rowKeySet());
        for (String row : rows) {
            sb.append("                <tr>\n").append("                    <td>").append(row)
                    .append("</td>\n");
            for (String key : keys) {
                Pair<Double, Double> r = results.get(row, key);
                sb.append("                    <td>").append(String.format("%.3f", r.getLeft()))
                        .append("</td>\n").append("                    <td>")
                        .append(String.format("%.3f", r.getRight())).append("</td>\n");

            }
            sb.append("                </tr>\n");
        }
        sb.append("            </tbody>\n").append("        </table>");
        FileUtils.write(new File(htmlFilepath), template.replace("@@TABLE@@", sb.toString()),
                StandardCharsets.UTF_8);
    } catch (IOException ex) {
        logger.error("IO problem with the HTML output.");
        throw new AnalysisEngineProcessException(ex);
    }
}

From source file:com.github.steveash.jg2p.aligntag.AlignTagTrainer.java

private void trainExamples(InstanceList examples) throws IOException {
    Pipe pipe = examples.getPipe();

    TransducerTrainer trainer;/*from  ww  w  .j a  va  2  s  . c  o m*/

    int round = 0;
    CrossValidationIterator trials = new CrossValidationIterator(examples, 4, new Random(123321123));
    SummaryStatistics overall = new SummaryStatistics();

    while (trials.hasNext()) {
        log.info("Starting training round {}...", round);
        InstanceList[] split = trials.next();
        InstanceList trainData = split[0];
        InstanceList testData = split[1];

        trainer = trainOnce(pipe, trainData);
        double thisAccuracy = evaluateOnce(round, trainData, testData, trainer);
        overall.addValue(thisAccuracy);

        round += 1;
    }

    log.info("Training on whole data...");
    trainer = trainOnce(pipe, examples);
    writeModel(trainer);

    log.info("Done! overall " + overall.getMean() + " stddev " + overall.getStandardDeviation());
}

From source file:gov.llnl.lc.infiniband.opensm.plugin.data.RT_NodeBalance.java

private boolean getPortsExceedingSubscription(BinList<RT_Port> caBins, SummaryStatistics stats) {
    // create the normal boundaries, anything outside of these are considered "out of balance"
    double stdDev = stats.getStandardDeviation() > RT_Balance.DEVIATION_LIMIT ? stats.getStandardDeviation()
            : RT_Balance.DEVIATION_LIMIT;
    double maxPaths = stats.getMean() + stdDev;
    double minPaths = stats.getMean() - stdDev;

    int k = 0;/*  w  w  w .j  ava  2s  .co m*/
    for (ArrayList<RT_Port> caList : caBins) {
        if (caList != null) {
            double nPaths = Double.parseDouble((caBins.getKey(k)).trim());
            if (nPaths > maxPaths) {
                // add all of these port numbers to the OverSubscribed list
                for (RT_Port p : caList)
                    OverSubscribedPorts.add(p.getPortNumber());
            } else if (nPaths < minPaths) {
                // add all of these port numbers to the UnderSubscribed list
                for (RT_Port p : caList)
                    UnderSubscribedPorts.add(p.getPortNumber());
            }
        }
        k++;
    }
    return true;
}

From source file:model.experiments.stickyprices.StickyPricesCSVPrinter.java

private static void badlyOptimizedNoInventorySupplyChain(int seed, final float proportionalGain,
        final float integralGain, final int speed, File csvFileToWrite, final boolean tuning) {
    final MacroII macroII = new MacroII(seed);
    final OneLinkSupplyChainScenarioWithCheatingBuyingPrice scenario1 = new OneLinkSupplyChainScenarioWithCheatingBuyingPrice(
            macroII) {//w w  w  . j  av  a2  s.  c o  m

        @Override
        protected void buildBeefSalesPredictor(SalesDepartment dept) {
            FixedDecreaseSalesPredictor predictor = SalesPredictor.Factory
                    .newSalesPredictor(FixedDecreaseSalesPredictor.class, dept);
            predictor.setDecrementDelta(2);
            dept.setPredictorStrategy(predictor);

        }

        @Override
        public void buildFoodPurchasesPredictor(PurchasesDepartment department) {
            department.setPredictor(new FixedIncreasePurchasesPredictor(0));

        }

        @Override
        protected SalesDepartment createSalesDepartment(Firm firm, Market goodmarket) {
            SalesDepartment department = super.createSalesDepartment(firm, goodmarket);
            if (goodmarket.getGoodType().equals(OneLinkSupplyChainScenario.OUTPUT_GOOD)) {
                department.setPredictorStrategy(new FixedDecreaseSalesPredictor(0));
            } else {
                final InventoryBufferSalesControl askPricingStrategy = new InventoryBufferSalesControl(
                        department, 10, 200, macroII, proportionalGain, integralGain, 0, macroII.getRandom());
                askPricingStrategy.setSpeed(speed);
                if (tuning)
                    askPricingStrategy.decorateController(pidController -> {
                        final PIDStickinessHillClimberTuner pidStickinessSalesTuner = new PIDStickinessHillClimberTuner(
                                department.getModel(), pidController, department, 1000);
                        pidStickinessSalesTuner.setStepSize(5);
                        pidStickinessSalesTuner.attachWriter(Paths.get("runs", "rawdata", "stickLog.csv"));
                        //                   pidStickinessSalesTuner.setObservationsBeforeTuning(5000);
                        //                    pidStickinessSalesTuner.setLogToWrite();
                        return pidStickinessSalesTuner;
                    });
                department.setAskPricingStrategy(askPricingStrategy);

            }
            return department;
        }

        @Override
        protected PurchasesDepartment createPurchaseDepartment(Blueprint blueprint, Firm firm) {
            final PurchasesDepartment purchaseDepartment = super.createPurchaseDepartment(blueprint, firm);
            if (purchaseDepartment != null)
                purchaseDepartment.setPriceAverager(new LastClosingPriceEcho());
            return purchaseDepartment;
        }

        @Override
        protected HumanResources createPlant(Blueprint blueprint, Firm firm, Market laborMarket) {
            HumanResources hr = super.createPlant(blueprint, firm, laborMarket);
            hr.setPriceAverager(new LastClosingPriceEcho());

            if (blueprint.getOutputs().containsKey(OneLinkSupplyChainScenario.INPUT_GOOD)) {
                hr.setPredictor(new FixedIncreasePurchasesPredictor(1));
            }
            if (blueprint.getOutputs().containsKey(OneLinkSupplyChainScenario.OUTPUT_GOOD)) {
                hr.setPredictor(new FixedIncreasePurchasesPredictor(0));
            }
            return hr;
        }
    };
    scenario1.setControlType(MarginalMaximizer.class);
    scenario1.setSalesDepartmentType(SalesDepartmentOneAtATime.class);
    scenario1.setBeefPriceFilterer(null);

    //competition!
    scenario1.setNumberOfBeefProducers(1);
    scenario1.setNumberOfFoodProducers(5);

    //no delay

    //add csv writer if needed
    if (csvFileToWrite != null)
        DailyStatCollector.addDailyStatCollectorToModel(csvFileToWrite, macroII);

    macroII.setScenario(scenario1);
    macroII.start();

    while (macroII.schedule.getTime() < 14000) {
        macroII.schedule.step(macroII);
        printProgressBar(14001, (int) macroII.schedule.getSteps(), 100);
        System.out.println(macroII.getMarket(OneLinkSupplyChainScenario.OUTPUT_GOOD)
                .getLatestObservation(MarketDataType.VOLUME_PRODUCED));
    }

    SummaryStatistics averageFoodPrice = new SummaryStatistics();
    SummaryStatistics averageBeefProduced = new SummaryStatistics();
    SummaryStatistics averageBeefPrice = new SummaryStatistics();
    for (int j = 0; j < 1000; j++) {
        //make the model run one more day:
        macroII.schedule.step(macroII);
        averageFoodPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.OUTPUT_GOOD)
                .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
        averageBeefProduced
                .addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD).getYesterdayVolume());
        averageBeefPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD)
                .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
    }

    System.out.println("seed: " + seed);
    System.out.println("beef price: " + averageBeefPrice.getMean());
    System.out.println("food price: " + averageFoodPrice.getMean());
    System.out.println("produced: " + averageBeefProduced.getMean());
    System.out.println();

}

From source file:com.itemanalysis.psychometrics.irt.estimation.MarginalMaximumLikelihoodEstimation.java

/**
 *
 *///from w  w w .j  ava 2  s. co  m
public void computeG2ItemFit(int nbins, int minExpectedCount) {
    IrtExaminee irtExaminee = new IrtExaminee("", irm);
    double[] eapEstimate = new double[responseVector.length];
    SummaryStatistics stats = new SummaryStatistics();

    //First loop over response vectors
    for (int i = 0; i < responseVector.length; i++) {
        //EAP estimate of ability
        irtExaminee.setResponseVector(responseVector[i]);
        eapEstimate[i] = irtExaminee.eapEstimate(latentDistribution);

        for (int N = 0; N < responseVector[i].getFrequency(); N++) {//Expand response vectors
            stats.addValue(eapEstimate[i]);
        }
    }

    DefaultLinearTransformation linearTransformation = new DefaultLinearTransformation(stats.getMean(),
            latentDistribution.getMean(), stats.getStandardDeviation(),
            latentDistribution.getStandardDeviation());

    //Create fit statistic objects
    itemFit = new ItemFitG2[nItems];

    double lower = linearTransformation.transform(stats.getMin()) - .01;//Subtract a small number to ensure lowest theta is counted
    double upper = linearTransformation.transform(stats.getMax()) + .01;//Add a small number to ensure largest theta is counted
    Cut thetaCut = new Cut(lower, upper, nbins);

    for (int j = 0; j < nItems; j++) {
        itemFit[j] = new ItemFitG2(irm[j], thetaCut, minExpectedCount);
    }

    //Second loop over response vectors
    //Increment fit statistics
    double A = linearTransformation.getScale();
    for (int i = 0; i < responseVector.length; i++) {
        //Estimate EAP standard deviation should be the same as the standard deviation
        //of the latent distribution used to estimate the item parameters.
        eapEstimate[i] = eapEstimate[i] * A;

        for (int N = 0; N < responseVector[i].getFrequency(); N++) {//Expand table
            for (int j = 0; j < nItems; j++) {
                ((ItemFitG2) itemFit[j]).increment(eapEstimate[i], responseVector[i].getResponseAt(j));
            }
        }
    }

    //        System.out.println(thetaCut.toString());

    //Compute Item Fit
    for (int j = 0; j < nItems; j++) {
        itemFit[j].compute();
    }

}

From source file:net.lizalab.util.RdRandRandomTest.java

/**
 * Runs the mean test for the specified instance of Random.
 * @param random The RNG instance to test.
 * @param doAssert Flag to indicate whether result should be asserted or simply logged.
 *//*from w w w  .  j a va  2  s.c om*/
private void meanTest(Random random, boolean doAssert) {
    final String methodName = "meanTest : ";

    ndigits = new int[10];
    // Initialize the array
    for (int i = 0; i < 10; i++) {
        ndigits[i] = 0;
    }
    double x;
    int n;

    SummaryStatistics stats = new SummaryStatistics();

    long start = System.currentTimeMillis();
    // Test the random number generator a whole lot
    for (long i = 0; i < values; i++) {
        // generate a new random number between 0 and 9
        x = random.nextDouble() * 10.0;
        n = (int) x;
        stats.addValue(n);
        // count the digits in the random number
        ndigits[n]++;
    }
    long end = System.currentTimeMillis();

    LOGGER.info("{} Time: {}ms", methodName, end - start);
    LOGGER.info("{} Distribution:", methodName);
    // Print the results
    for (int i = 0; i < 10; i++) {
        LOGGER.info("{} {}: {}", methodName, i, ndigits[i]);
    }
    double sampleMean = stats.getMean();
    double sampleVar = stats.getVariance();
    double meanDiff = (sampleMean - mean) / mean;
    LOGGER.info("{} mean: {}, diff: {}", methodName, sampleMean, meanDiff);
    LOGGER.info("{} sd: {}", methodName, stats.getStandardDeviation());
    LOGGER.info("{} var: {}", methodName, sampleVar);
    // Verify results.
    boolean varResult = sampleVar < var;
    String varMsg = "Random Variance exceeds max expectation!";
    boolean ci99Result = sampleMean >= smLowerRng3SD && sampleMean <= smUpperRng3SD;
    String ci99Msg = "Random 99.7% CI test failed!";
    boolean ci95Result = sampleMean >= smLowerRng2SD && sampleMean <= smUpperRng2SD;
    String ci95Msg = "Random 95% CI test failed!";
    if (doAssert) {
        assertTrue(varMsg, varResult);
        assertTrue(ci99Msg, ci99Result);
        assertTrue(ci95Msg, ci95Result);
    } else {
        if (!varResult) {
            LOGGER.warn("{} {}", methodName, varMsg);
        }
        if (!ci99Result) {
            LOGGER.warn("{} {}", methodName, ci99Msg);
        }
        if (!ci95Result) {
            LOGGER.warn("{} {}", methodName, ci95Msg);
        }
    }
}

From source file:agents.firm.sales.pricing.pid.SimpleStockSellerIntegrativeTest.java

@Test
public void testAlreadyLearnedCompetitive() {
    int competitors = 4;

    for (int i = 0; i < 5; i++) {

        final MacroII macroII = new MacroII(System.currentTimeMillis()); //1387582416533
        final TripolistScenario scenario1 = new TripolistScenario(macroII);

        scenario1.setSalesDepartmentType(SalesDepartmentOneAtATime.class);
        scenario1.setAskPricingStrategy(SimpleStockSeller.class);
        scenario1.setControlType(//from w w w.ja  va2 s .  c o m
                MonopolistScenario.MonopolistScenarioIntegratedControlEnum.MARGINAL_PLANT_CONTROL);
        scenario1.setAdditionalCompetitors(competitors);
        scenario1.setWorkersToBeRehiredEveryDay(true);
        scenario1.setDemandIntercept(102);

        //          scenario1.setSalesPricePreditorStrategy(FixedDecreaseSalesPredictor.class);

        //assign scenario
        macroII.setScenario(scenario1);

        macroII.start();

        macroII.schedule.step(macroII);
        for (Firm firm : scenario1.getCompetitors()) {
            for (HumanResources hr : firm.getHRs())
                hr.setPredictor(new FixedIncreasePurchasesPredictor(0));
            firm.getSalesDepartment(UndifferentiatedGoodType.GENERIC)
                    .setPredictorStrategy(new FixedDecreaseSalesPredictor(0));
        }

        while (macroII.schedule.getTime() < 10000) {
            macroII.schedule.step(macroII);

        }

        SummaryStatistics prices = new SummaryStatistics();
        SummaryStatistics quantities = new SummaryStatistics();
        SummaryStatistics target = new SummaryStatistics();
        for (int j = 0; j < 500; j++) {
            macroII.schedule.step(macroII);
            //                    assert !Float.isNaN(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayAveragePrice());
            prices.addValue(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayAveragePrice());
            quantities.addValue(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayVolume());

            for (EconomicAgent agent : macroII.getMarket(UndifferentiatedGoodType.GENERIC).getSellers()) {
                SalesDepartment department = ((Firm) agent)
                        .getSalesDepartment(UndifferentiatedGoodType.GENERIC);
                target.addValue(macroII.getMarket(UndifferentiatedGoodType.GENERIC).getTodayVolume());
            }

        }

        System.out.println(prices.getMean() + " - " + quantities.getMean() + "/" + target.getMean() + "----"
                + macroII.seed() + " | "
                + macroII.getMarket(UndifferentiatedGoodType.GENERIC).getLastDaysAveragePrice());
        System.out.println("standard deviations: price : " + prices.getStandardDeviation() + " , quantity: "
                + quantities.getStandardDeviation());

        assertEquals(prices.getMean(), 58, 5);
        //                assertTrue(String.valueOf(prices.getStandardDeviation()),prices.getStandardDeviation() < 5.5);
        assertEquals(quantities.getMean(), 44, 5);
        //                assertTrue(String.valueOf(prices.getStandardDeviation()),quantities.getStandardDeviation() < 5.5);

    }

}

From source file:model.scenario.OneLinkSupplyChainResult.java

public static OneLinkSupplyChainResult beefMonopolistOneRun(long random, float divideMonopolistGainsByThis,
        int monopolistSpeed, final boolean beefLearned, final boolean foodLearned,
        Function<SalesDepartment, AskPricingStrategy> woodPricingFactory,
        Function<SalesDepartment, AskPricingStrategy> furniturePricingFactory, File csvFileToWrite,
        File logFileToWrite, Path regressionLogToWrite) {
    final MacroII macroII = new MacroII(random);
    final OneLinkSupplyChainScenarioWithCheatingBuyingPrice scenario1 = new OneLinkSupplyChainScenarioWithCheatingBuyingPrice(
            macroII) {/*  w w  w .j  a  va2 s.c om*/

        @Override
        protected void buildBeefSalesPredictor(SalesDepartment dept) {
            if (beefLearned) {
                FixedDecreaseSalesPredictor predictor = SalesPredictor.Factory
                        .newSalesPredictor(FixedDecreaseSalesPredictor.class, dept);
                predictor.setDecrementDelta(2);
                dept.setPredictorStrategy(predictor);
            } else {
                assert dept.getPredictorStrategy() instanceof ErrorCorrectingSalesPredictor; //assuming here nothing has been changed and we are still dealing with recursive sale predictors
                try {
                    if (regressionLogToWrite != null)
                        ((ErrorCorrectingSalesPredictor) dept.getPredictorStrategy())
                                .setDebugWriter(regressionLogToWrite);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void buildFoodPurchasesPredictor(PurchasesDepartment department) {
            if (foodLearned)
                department.setPredictor(new FixedIncreasePurchasesPredictor(0));

        }

        @Override
        protected SalesDepartment createSalesDepartment(Firm firm, Market goodmarket) {
            SalesDepartment department = super.createSalesDepartment(firm, goodmarket);
            if (goodmarket.getGoodType().equals(OneLinkSupplyChainScenario.OUTPUT_GOOD)) {
                if (foodLearned)
                    department.setPredictorStrategy(new FixedDecreaseSalesPredictor(0));
            }
            return department;
        }

        @Override
        protected HumanResources createPlant(Blueprint blueprint, Firm firm, Market laborMarket) {
            HumanResources hr = super.createPlant(blueprint, firm, laborMarket);
            if (blueprint.getOutputs().containsKey(OneLinkSupplyChainScenario.INPUT_GOOD)) {
                if (beefLearned) {
                    hr.setPredictor(new FixedIncreasePurchasesPredictor(1));
                }
            }
            if (blueprint.getOutputs().containsKey(OneLinkSupplyChainScenario.OUTPUT_GOOD)) {
                if (foodLearned)
                    hr.setPredictor(new FixedIncreasePurchasesPredictor(0));
            }
            return hr;
        }
    };
    scenario1.setControlType(MarginalMaximizer.class);
    scenario1.setSalesDepartmentType(SalesDepartmentOneAtATime.class);
    scenario1.setBeefPriceFilterer(null);

    if (woodPricingFactory != null)
        scenario1.setBeefPricingFactory(woodPricingFactory);
    if (furniturePricingFactory != null)
        scenario1.setFoodPricingFactory(furniturePricingFactory);

    //competition!
    scenario1.setNumberOfBeefProducers(1);
    scenario1.setBeefTargetInventory(100);
    scenario1.setFoodTargetInventory(100);
    scenario1.setNumberOfFoodProducers(5);

    scenario1.setDivideProportionalGainByThis(divideMonopolistGainsByThis);
    scenario1.setDivideIntegrativeGainByThis(divideMonopolistGainsByThis);
    //no delay
    scenario1.setBeefPricingSpeed(monopolistSpeed);

    //add csv writer if needed
    if (csvFileToWrite != null)
        DailyStatCollector.addDailyStatCollectorToModel(csvFileToWrite, macroII);

    macroII.setScenario(scenario1);
    macroII.start();
    macroII.schedule.step(macroII);

    if (logFileToWrite != null)
        scenario1.getMarkets().get(OneLinkSupplyChainScenario.INPUT_GOOD).getSellers().iterator().next()
                .addLogEventListener(new LogToFile(logFileToWrite, LogLevel.DEBUG, macroII));

    while (macroII.schedule.getTime() < 14000) {
        macroII.schedule.step(macroII);

    }

    SummaryStatistics averageFoodPrice = new SummaryStatistics();
    SummaryStatistics averageBeefProduced = new SummaryStatistics();
    SummaryStatistics averageBeefPrice = new SummaryStatistics();
    SummaryStatistics averageSalesSlope = new SummaryStatistics();
    SummaryStatistics averageHrSlope = new SummaryStatistics();
    final Firm monopolist = (Firm) scenario1.getMarkets().get(OneLinkSupplyChainScenario.INPUT_GOOD)
            .getSellers().iterator().next();
    for (int j = 0; j < 1000; j++) {
        //make the model run one more day:
        macroII.schedule.step(macroII);

        averageFoodPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.OUTPUT_GOOD)
                .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
        averageBeefProduced
                .addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD).getYesterdayVolume());
        averageBeefPrice.addValue(macroII.getMarket(OneLinkSupplyChainScenario.INPUT_GOOD)
                .getLatestObservation(MarketDataType.AVERAGE_CLOSING_PRICE));
        averageSalesSlope.addValue(monopolist.getSalesDepartment(OneLinkSupplyChainScenario.INPUT_GOOD)
                .getLatestObservation(SalesDataType.PREDICTED_DEMAND_SLOPE));
        averageHrSlope.addValue(monopolist.getHRs().iterator().next()
                .getLatestObservation(PurchasesDataType.PREDICTED_SUPPLY_SLOPE));
    }

    System.out.println("seed: " + random);
    System.out.println("beef price: " + averageBeefPrice.getMean());
    System.out.println("food price: " + averageFoodPrice.getMean());
    System.out.println("produced: " + averageBeefProduced.getMean());
    extractAndPrintSlopesOfBeefSellers(macroII);
    System.out.println();

    macroII.finish();

    return new OneLinkSupplyChainResult(averageBeefPrice.getMean(), averageFoodPrice.getMean(),
            averageBeefProduced.getMean(), macroII);

}

From source file:net.adamjak.thomas.graph.application.run.TestRunner.java

private void save(Map<String, Object> results, boolean rawData) {
    SnarkTestTypes testType = (SnarkTestTypes) results.get("testType");

    if (this.outputFile.getName().split("\\.")[this.outputFile.getName().split("\\.").length - 1].toLowerCase()
            .equals("ods")) {

        String[] columnNames;/*from  ww w.ja v a 2 s. c o  m*/
        Object[][] data;

        if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            columnNames = String.valueOf("Algorithm,Graph ID,Avarage time,Standard deviation,Minimum,Maximum")
                    .split(",");
            data = new Object[graphTestResult[0].length][6];

            for (int cls = 0; cls < graphTestResult[0][0].length; cls++) {
                Class<?> c = (Class<?>) graphTestResult[0][0][cls].getValue("algorithmClass");

                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    SummaryStatistics summaryStatistics = new SummaryStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        summaryStatistics
                                .addValue((double) graphTestResult[run][graph][cls].getValue("timeInSeconds"));
                    }

                    data[graph][0] = c.getSimpleName();
                    data[graph][1] = graph;
                    data[graph][2] = summaryStatistics.getMean();
                    data[graph][3] = summaryStatistics.getStandardDeviation();
                    data[graph][4] = summaryStatistics.getMin();
                    data[graph][5] = summaryStatistics.getMax();
                }
            }
        } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            columnNames = String
                    .valueOf("Graph ID,Start vertex,Avarage time,Standard deviation,Minimum,Maximum")
                    .split(",");
            data = new Object[graphTestResult[0].length][6];

            for (int vid = 0; vid < graphTestResult[0][0].length; vid++) {
                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    SummaryStatistics summaryStatistics = new SummaryStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        summaryStatistics
                                .addValue((double) graphTestResult[run][graph][vid].getValue("timeInSeconds"));
                    }

                    data[graph][0] = graph;
                    data[graph][1] = vid;
                    data[graph][2] = summaryStatistics.getMean();
                    data[graph][3] = summaryStatistics.getStandardDeviation();
                    data[graph][4] = summaryStatistics.getMin();
                    data[graph][5] = summaryStatistics.getMax();
                }
            }
        } else {
            GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

            columnNames = String.valueOf("Graph ID,Avarage time,Standard deviation,Minimum,Maximum").split(",");
            data = new Object[graphTestResult[0].length][5];

            for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                SummaryStatistics summaryStatistics = new SummaryStatistics();

                for (int run = 0; run < graphTestResult.length; run++) {
                    summaryStatistics.addValue((double) graphTestResult[run][graph].getValue("timeInSeconds"));
                }

                data[graph][0] = graph;
                data[graph][1] = summaryStatistics.getMean();
                data[graph][2] = summaryStatistics.getStandardDeviation();
                data[graph][3] = summaryStatistics.getMin();
                data[graph][4] = summaryStatistics.getMax();
            }
        }

        try {
            SpreadSheet.createEmpty(new JTable(data, columnNames).getModel()).saveAs(outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (rawData == true) {
            if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                columnNames = String.valueOf("Class,Run,Graph,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length
                        * graphTestResult[0][0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            data[row][0] = graphTestResult[i][j][k].getValue("algorithmClass");
                            data[row][1] = i;
                            data[row][2] = j;
                            data[row][3] = graphTestResult[i][j][k].getValue("time");
                            row++;
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Vertex,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length
                        * graphTestResult[0][0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            data[row][0] = i;
                            data[row][1] = j;
                            data[row][2] = k;
                            data[row][3] = graphTestResult[i][j][k].getValue("time");
                            row++;
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ALGORITHM_COMPARATION) {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Time,Class").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length][4];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        data[row][0] = i;
                        data[row][1] = j;
                        data[row][2] = graphTestResult[i][j].getValue("time");
                        data[row][3] = ((Class<?>) graphTestResult[i][j]
                                .getValue(GraphTestResult.SNARK_TESTER_CLASS_KEY)).getSimpleName();
                        row++;
                    }
                }
            } else {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                columnNames = String.valueOf("Run,Graph,Time").split(",");
                data = new Object[graphTestResult.length * graphTestResult[0].length][3];

                int row = 0;
                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        data[row][0] = i;
                        data[row][1] = j;
                        data[row][2] = graphTestResult[i][j].getValue("time");
                        row++;
                    }
                }
            }

            try {
                SpreadSheet.createEmpty(new JTable(data, columnNames).getModel()).saveAs(outputFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        StringBuilder sbData = new StringBuilder();

        if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            sbData.append(",,All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Graph ID,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int cls = 0; cls < graphTestResult[0][0].length; cls++) {
                Class<?> c = (Class<?>) graphTestResult[0][0][cls].getValue("algorithmClass");

                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        descriptiveStatistics
                                .addValue((double) graphTestResult[run][graph][cls].getValue("timeInSeconds"));
                    }

                    DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                            .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                    sbData.append(c.getSimpleName());
                    sbData.append(",");
                    sbData.append(graph);
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append("\n");
                }
            }
        } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
            GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

            sbData.append(",,All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Start vertex,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int vid = 0; vid < graphTestResult[0][0].length; vid++) {
                for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                    DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                    for (int run = 0; run < graphTestResult.length; run++) {
                        descriptiveStatistics
                                .addValue((double) graphTestResult[run][graph][vid].getValue("timeInSeconds"));
                    }

                    DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                            .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                    sbData.append(graph);
                    sbData.append(",");
                    sbData.append(vid);
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatistics.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                    sbData.append(",");
                    sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                    sbData.append(",");
                    sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                            StatisticsUtils.NormCritical.U0050));
                    sbData.append("\n");
                }
            }
        } else {

            GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

            sbData.append(",All data,,,,,Data without extremes,,,,,\n");
            sbData.append(
                    "Graph ID,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval,Avarage time,Standard deviation,Minimum,Maximum,Confidence Interval\n");

            for (int graph = 0; graph < graphTestResult[0].length; graph++) {
                DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

                for (int run = 0; run < graphTestResult.length; run++) {
                    descriptiveStatistics
                            .addValue((double) graphTestResult[run][graph].getValue("timeInSeconds"));
                }

                DescriptiveStatistics descriptiveStatisticsWithoutExtremes = StatisticsUtils
                        .statisticsWithoutExtremes(descriptiveStatistics, StatisticsUtils.GrubbsLevel.L005);

                sbData.append(graph);
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMean());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getStandardDeviation());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMin());
                sbData.append(",");
                sbData.append(descriptiveStatistics.getMax());
                sbData.append(",");
                sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatistics,
                        StatisticsUtils.NormCritical.U0050));
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMean());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getStandardDeviation());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMin());
                sbData.append(",");
                sbData.append(descriptiveStatisticsWithoutExtremes.getMax());
                sbData.append(",");
                sbData.append(StatisticsUtils.getConfidenceInterval(descriptiveStatisticsWithoutExtremes,
                        StatisticsUtils.NormCritical.U0050));
                sbData.append("\n");
            }

        }

        this.saveStringIntoFile(this.outputFile, sbData.toString());

        if (rawData == true) {
            StringBuilder sbRawData = new StringBuilder();

            if (testType == SnarkTestTypes.ALL_ALGORITHMS) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                sbRawData.append("Class,Run,Graph,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            sbRawData.append(graphTestResult[i][j][k].getValue("algorithmClass"));
                            sbRawData.append(",");
                            sbRawData.append(i);
                            sbRawData.append(",");
                            sbRawData.append(j);
                            sbRawData.append(",");
                            sbRawData.append(graphTestResult[i][j][k].getValue("time"));
                            sbRawData.append("\n");
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ONE_ALGORITHM_START_IN_EVERY_VERTEX) {
                GraphTestResult[][][] graphTestResult = (GraphTestResult[][][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Vertex,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        for (int k = 0; k < graphTestResult[i][j].length; k++) {
                            sbRawData.append(i);
                            sbRawData.append(",");
                            sbRawData.append(j);
                            sbRawData.append(",");
                            sbRawData.append(k);
                            sbRawData.append(",");
                            sbRawData.append(graphTestResult[i][j][k].getValue("time"));
                            sbRawData.append("\n");
                        }
                    }
                }
            } else if (testType == SnarkTestTypes.ALGORITHM_COMPARATION) {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Time,Class\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        sbRawData.append(i);
                        sbRawData.append(",");
                        sbRawData.append(j);
                        sbRawData.append(",");
                        sbRawData.append(graphTestResult[i][j].getValue("time"));
                        sbRawData.append(",");
                        sbRawData.append(((Class<?>) graphTestResult[i][j]
                                .getValue(GraphTestResult.SNARK_TESTER_CLASS_KEY)).getSimpleName());
                        sbRawData.append("\n");
                    }
                }
            } else {
                GraphTestResult[][] graphTestResult = (GraphTestResult[][]) results.get("resultsData");

                sbRawData.append("Run,Graph,Time\n");

                for (int i = 0; i < graphTestResult.length; i++) {
                    for (int j = 0; j < graphTestResult[i].length; j++) {
                        sbRawData.append(i);
                        sbRawData.append(",");
                        sbRawData.append(j);
                        sbRawData.append(",");
                        sbRawData.append(graphTestResult[i][j].getValue("time"));
                        sbRawData.append("\n");
                    }
                }
            }

            this.saveStringIntoFile(new File(this.outputFile.getParent(), "raw_" + this.outputFile.getName()),
                    sbRawData.toString());
        }
    }
}

From source file:model.utilities.pid.decorator.AutotunerTest.java

private PIDAutotuner runLearningExperimentWithUnknownDeadTime(MersenneTwisterFast random,
        float proportionalParameter, float integrativeParameter, Supplier<Double> noiseMaker,
        DynamicProcess systemDynamic) throws FileNotFoundException {
    PIDAutotuner controller = new PIDAutotuner(
            new PIDController(proportionalParameter, integrativeParameter, 0));
    controller.setAfterHowManyDaysShouldTune(1001);
    int target = 10;
    if (noiseMaker != null)
        systemDynamic.setRandomNoise(noiseMaker);

    //create the regression too

    //output starts at intercept
    float output = 0;
    //delayed input, useful for learning

    SummaryStatistics errorBeforeTuning = new SummaryStatistics();

    SummaryStatistics errorAfterTuning = new SummaryStatistics();
    SummaryStatistics finalError = new SummaryStatistics();

    for (int step = 0; step < 2000; step++) {

        //PID step
        controller.adjust(new ControllerInput(target, output), true, mock(MacroII.class), null,
                ActionOrder.DAWN);/*from w  w  w. j  a va 2 s . co m*/

        //process reacts
        float input = controller.getCurrentMV();
        assert !Float.isNaN(input);
        assert !Float.isInfinite(input);
        output = (float) systemDynamic.newStep(input);

        if (step <= 1000)
            errorBeforeTuning.addValue(Math.pow(target - output, 2));
        else
            errorAfterTuning.addValue(Math.pow(target - output, 2));
        if (step > 1900)
            finalError.addValue(Math.pow(target - output, 2));

        //shock target with 10%
        if (random.nextBoolean(.10)) {
            if (random.nextBoolean())
                target++;
            else
                target = Math.max(target - 1, 0);
        }

    }
    System.out.println("errors: " + errorBeforeTuning.getMean() + " --- " + errorAfterTuning.getMean());
    System.out.println("final error: " + finalError.getMean());
    System.out.println("regression: " + controller.getRegression());

    RegressionStatics.tracksAcceptably(controller.getRegression(), systemDynamic,
            RegressionStatics.MAXIMUM_ABS_ERROR, 100,
            Math.max(controller.getCurrentMV(), RegressionStatics.FIXED_INPUT));
    Assert.assertTrue(errorAfterTuning.getMean() < errorBeforeTuning.getMean());
    //either have a very low error, or at least have improved by a factor of over 100
    Assert.assertTrue(finalError.getMean() < 10 || finalError.getMean() < errorBeforeTuning.getMean() / 100);

    return controller;

}