List of usage examples for org.apache.commons.math3.stat.descriptive SummaryStatistics addValue
public void addValue(double value)
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 a v a2s . c om @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: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 ww w . jav a2s. c om 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:ijfx.core.stats.DefaultImageStatisticsService.java
@Override public SummaryStatistics getChannelStatistics(Dataset dataset, int channelPosition) { final SummaryStatistics stats = new SummaryStatistics(); // final Timer timer = timerService.getTimer(this.getClass()); final Cursor<RealType<?>> cursor; if (dataset.dimensionIndex(Axes.CHANNEL) == -1) { cursor = dataset.cursor();// w w w .j a va 2 s . c om cursor.reset(); while (cursor.hasNext()) { cursor.fwd(); stats.addValue(cursor.get().getRealDouble()); } } else { // timer.start(); final IntervalView<RealType<?>> hyperSlice = Views.hyperSlice(dataset, dataset.dimensionIndex(Axes.CHANNEL), channelPosition); cursor = hyperSlice.cursor(); cursor.reset(); while (cursor.hasNext()) { cursor.fwd(); stats.addValue(cursor.get().getRealDouble()); } } return stats; }
From source file:ijfx.ui.plugin.overlay.OverlayPanel.java
protected XYChart.Series<Double, Double> getOverlayHistogram(Overlay overlay) { Timer timer = timerService.getTimer(this.getClass()); timer.start();/* www .ja v a2 s . co m*/ Double[] valueList = statsService.getValueListFromImageDisplay(currentDisplay(), overlay); timer.elapsed("Getting the stats"); SummaryStatistics sumup = new SummaryStatistics(); for (Double v : valueList) { sumup.addValue(v); } timer.elapsed("Building the sumup"); double min = sumup.getMin(); double max = sumup.getMax(); double range = max - min; int bins = 100;//new Double(max - min).intValue(); EmpiricalDistribution distribution = new EmpiricalDistribution(bins); double[] values = ArrayUtils.toPrimitive(valueList); Arrays.parallelSort(values); distribution.load(values); timer.elapsed("Sort and distrubution repartition up"); XYChart.Series<Double, Double> serie = new XYChart.Series<>(); ArrayList<Data<Double, Double>> data = new ArrayList<>(bins); double k = min; for (SummaryStatistics st : distribution.getBinStats()) { data.add(new Data<Double, Double>(k, new Double(st.getN()))); k += range / bins; } serie.getData().clear(); serie.getData().addAll(data); timer.elapsed("Creating charts"); return serie; }
From source file:io.tilt.minka.business.leader.Shepherd.java
private boolean checkHealth(long now, long normalDelay, List<Heartbeat> onTime) { SummaryStatistics stat = new SummaryStatistics(); // there's some hope long lastCreation = onTime.get(0).getCreation().getMillis(); long biggestDistance = 0; for (Heartbeat hb : onTime) { long creation = hb.getCreation().getMillis(); long arriveDelay = now - creation; long distance = creation - lastCreation; stat.addValue(distance); lastCreation = creation;/*from www.ja v a 2 s.c o m*/ biggestDistance = distance > biggestDistance ? distance : biggestDistance; if (logger.isDebugEnabled()) { logger.debug("{}: HB SeqID: {}, Arrive Delay: {}ms, Distance: {}ms, Creation: {}", getClass().getSimpleName(), hb.getSequenceId(), arriveDelay, distance, hb.getCreation()); } } long stdDeviationDelay = (long) Precision.round(stat.getStandardDeviation(), 2); long permittedStdDeviationDistance = (normalDelay * (long) (config.getShepherdHeartbeatMaxDistanceStandardDeviation() * 10d) / 100); /* long permittedBiggestDelay = (normalDelay * (long)(config.getShepherdHeartbeatMaxBiggestDistanceFactor()*10d) / 100); */ final NetworkShardID shardId = onTime.get(0).getShardId(); boolean healthly = stdDeviationDelay < permittedStdDeviationDistance;// && biggestDelay < permittedBiggestDelay; if (logger.isInfoEnabled()) { logger.debug("{}: Shard: {}, {} Standard deviation distance: {}/{}", getClass().getSimpleName(), shardId, healthly ? HEALTH_UP : HEALTH_DOWN, stdDeviationDelay, permittedStdDeviationDistance); } return healthly; }
From source file:com.github.steveash.jg2p.aligntag.AlignTagTrainer.java
private void trainExamples(InstanceList examples) throws IOException { Pipe pipe = examples.getPipe(); TransducerTrainer trainer;// ww w . j a va2 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: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 o m 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:model.experiments.stickyprices.StickyPricesCSVPrinter.java
private static void simpleDelaySweep(int maxDivider, int maxSpeed, int demandDelay, int experimentsPerSetup) throws IOException { CSVWriter writer = new CSVWriter(new FileWriter(Paths.get("runs", "rawdata", "delaySweep.csv").toFile())); writer.writeNext(new String[] { "speed", "divider", "distance", "variance", "success" }); for (int divider = 1; divider < maxDivider; divider++) { //for all speeds for (int speed = 0; speed < maxSpeed; speed++) { SummaryStatistics averageSquaredDistance = new SummaryStatistics(); SummaryStatistics averageVariance = new SummaryStatistics(); int successes = 0; for (int runNumber = 0; runNumber < experimentsPerSetup; runNumber++) { float totalDistance = 0; SummaryStatistics prices = new SummaryStatistics(); //runNumber! final SimpleSellerScenario run = runWithDelay(demandDelay, speed, divider, false, true, runNumber);/*w w w . j a v a2 s . co m*/ final double[] pricesInRun = run.getDepartments().get(0).getData() .getObservationsRecordedTheseDays(SalesDataType.LAST_ASKED_PRICE, 0, 14999); for (double price : pricesInRun) { totalDistance += Math.pow(price - 51, 2); prices.addValue(price); } averageSquaredDistance.addValue(Math.sqrt(totalDistance)); averageVariance.addValue(prices.getVariance()); if (pricesInRun[pricesInRun.length - 1] == 51) successes++; } String[] csvLine = new String[5]; csvLine[0] = String.valueOf(speed); csvLine[1] = String.valueOf(divider); csvLine[2] = String.valueOf(averageSquaredDistance.getMean()); csvLine[3] = String.valueOf(averageVariance.getMean()); csvLine[4] = String.valueOf(successes); writer.writeNext(csvLine); writer.flush(); System.out.println(Arrays.toString(csvLine)); } } }
From source file:fr.inria.eventcloud.benchmarks.radix10_conversion.Radix10ConversionBenchmark.java
private final void test(Run run) { System.out.println("Testing precision with NB_QUADS_TO_READ=" + run.getNbQuadsToRead() + " and PRECISION=" + run.getPrecision());/*from w w w . j a v a 2s. com*/ FileInputStream fis = null; Iterator<Quad> it = null; try { fis = new FileInputStream(this.trigResource); it = RiotReader.createIteratorQuads(fis, Lang.TRIG, null); SummaryStatistics statsWithoutPrefixRemoval = new SummaryStatistics(); SummaryStatistics statsWithPrefixRemoval = new SummaryStatistics(); Stopwatch stopwatch = Stopwatch.createUnstarted(); int i = 1; while (it.hasNext()) { if (i >= run.getNbQuadsToRead()) { break; } Quad quad = it.next(); if (this.enableRdfStats) { // compute stats without applying doping function statsWithoutPrefixRemoval.addValue(size(quad.getGraph())); statsWithoutPrefixRemoval.addValue(size(quad.getSubject())); statsWithoutPrefixRemoval.addValue(size(quad.getPredicate())); statsWithoutPrefixRemoval.addValue(size(quad.getObject())); } String g = SemanticCoordinate.applyDopingFunction(quad.getGraph()); String s = SemanticCoordinate.applyDopingFunction(quad.getSubject()); String p = SemanticCoordinate.applyDopingFunction(quad.getPredicate()); String o = SemanticCoordinate.applyDopingFunction(quad.getObject()); if (this.enableRdfStats) { // compute stats by applying doping function statsWithPrefixRemoval.addValue(g.length()); statsWithPrefixRemoval.addValue(s.length()); statsWithPrefixRemoval.addValue(p.length()); statsWithPrefixRemoval.addValue(o.length()); } long precision = run.getPrecision(); stopwatch.start(); ApfloatUtils.toFloatRadix10(g, precision); ApfloatUtils.toFloatRadix10(s, precision); ApfloatUtils.toFloatRadix10(p, precision); ApfloatUtils.toFloatRadix10(o, precision); stopwatch.stop(); i++; } if (this.enableRdfStats) { System.out.println( " RDF term min size before prefix removal is " + statsWithoutPrefixRemoval.getMin()); System.out.println( " RDF term max size before prefix removal is " + statsWithoutPrefixRemoval.getMax()); System.out.println( " RDF term average size before prefix removal is " + statsWithoutPrefixRemoval.getMean()); System.out .println(" RDF term min size after prefix removal is " + statsWithPrefixRemoval.getMin()); System.out .println(" RDF term max size after prefix removal is " + statsWithPrefixRemoval.getMax()); System.out.println( " RDF term average size after prefix removal is " + statsWithPrefixRemoval.getMean()); } System.out.println("Time to perform radix 10 conversion for " + i + " with precision set to " + run.getPrecision() + " is " + stopwatch.toString() + " --> " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms"); System.out.println(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.milaboratory.core.alignment.KMapper.java
/** * Method used internally./*w ww. j a v a 2 s. co m*/ */ public SummaryStatistics getRecordSizeSummaryStatistics() { SummaryStatistics ss = new SummaryStatistics(); for (int len : lengths) ss.addValue(len); return ss; }