List of usage examples for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMean
public double getMean()
From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java
public static double getDirectDistanceMean(List<DynModeTrip> trips) { DescriptiveStatistics directDistanceStats = new DescriptiveStatistics(); for (DynModeTrip trip : trips) { if (trip.getToLinkId() == null) { continue; }/*from w w w . j av a 2s.c o m*/ directDistanceStats.addValue(trip.getUnsharedDistanceEstimate_m()); } return directDistanceStats.getMean(); }
From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java
public static void analyseWaitTimes(String fileName, List<DynModeTrip> trips, int binsize_s) { Collections.sort(trips);/*from w w w.j a va 2s . c om*/ if (trips.size() == 0) return; int startTime = ((int) (trips.get(0).getDepartureTime() / binsize_s)) * binsize_s; int endTime = ((int) (trips.get(trips.size() - 1).getDepartureTime() / binsize_s) + binsize_s) * binsize_s; Map<Double, List<DynModeTrip>> splitTrips = splitTripsIntoBins(trips, startTime, endTime, binsize_s); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(2); format.setGroupingUsed(false); SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); BufferedWriter bw = IOUtils.getBufferedWriter(fileName + ".csv"); TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeriesCollection datasetrequ = new TimeSeriesCollection(); TimeSeries averageWaitC = new TimeSeries("average"); TimeSeries medianWait = new TimeSeries("median"); TimeSeries p_5Wait = new TimeSeries("5th percentile"); TimeSeries p_95Wait = new TimeSeries("95th percentile"); TimeSeries requests = new TimeSeries("Ride requests"); try { bw.write("timebin;trips;average_wait;min;p_5;p_25;median;p_75;p_95;max"); for (Entry<Double, List<DynModeTrip>> e : splitTrips.entrySet()) { long rides = 0; double averageWait = 0; double min = 0; double p_5 = 0; double p_25 = 0; double median = 0; double p_75 = 0; double p_95 = 0; double max = 0; if (!e.getValue().isEmpty()) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (DynModeTrip t : e.getValue()) { stats.addValue(t.getWaitTime()); } rides = stats.getN(); averageWait = stats.getMean(); min = stats.getMin(); p_5 = stats.getPercentile(5); p_25 = stats.getPercentile(25); median = stats.getPercentile(50); p_75 = stats.getPercentile(75); p_95 = stats.getPercentile(95); max = stats.getMax(); } Minute h = new Minute(sdf2.parse(Time.writeTime(e.getKey()))); medianWait.addOrUpdate(h, Double.valueOf(median)); averageWaitC.addOrUpdate(h, Double.valueOf(averageWait)); p_5Wait.addOrUpdate(h, Double.valueOf(p_5)); p_95Wait.addOrUpdate(h, Double.valueOf(p_95)); requests.addOrUpdate(h, rides * 3600. / binsize_s);// normalised [req/h] bw.newLine(); bw.write(Time.writeTime(e.getKey()) + ";" + rides + ";" + format.format(averageWait) + ";" + format.format(min) + ";" + format.format(p_5) + ";" + format.format(p_25) + ";" + format.format(median) + ";" + format.format(p_75) + ";" + format.format(p_95) + ";" + format.format(max)); } bw.flush(); bw.close(); dataset.addSeries(averageWaitC); dataset.addSeries(medianWait); dataset.addSeries(p_5Wait); dataset.addSeries(p_95Wait); datasetrequ.addSeries(requests); JFreeChart chart = chartProfile(splitTrips.size(), dataset, "Waiting times", "Wait time (s)"); JFreeChart chart2 = chartProfile(splitTrips.size(), datasetrequ, "Ride requests per hour", "Requests per hour (req/h)"); ChartSaveUtils.saveAsPNG(chart, fileName, 1500, 1000); ChartSaveUtils.saveAsPNG(chart2, fileName + "_requests", 1500, 1000); } catch (IOException | ParseException e) { e.printStackTrace(); } }
From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java
/** * @param vehicleDistances//from w w w . ja v a2 s . c o m * @param string * @return */ public static String summarizeVehicles(Map<Id<Vehicle>, double[]> vehicleDistances, String del) { DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(2); format.setGroupingUsed(false); DescriptiveStatistics driven = new DescriptiveStatistics(); DescriptiveStatistics revenue = new DescriptiveStatistics(); DescriptiveStatistics occupied = new DescriptiveStatistics(); DescriptiveStatistics empty = new DescriptiveStatistics(); for (double[] dist : vehicleDistances.values()) { driven.addValue(dist[0]); revenue.addValue(dist[1]); occupied.addValue(dist[2]); double emptyD = dist[0] - dist[2]; empty.addValue(emptyD); } double d_r_d_t = revenue.getSum() / driven.getSum(); // bw.write("iteration;vehicles;totalDistance;totalEmptyDistance;emptyRatio;totalRevenueDistance;averageDrivenDistance;averageEmptyDistance;averageRevenueDistance"); String result = vehicleDistances.size() + del + format.format(driven.getSum()) + del + format.format(empty.getSum()) + del + format.format(empty.getSum() / driven.getSum()) + del + format.format(revenue.getSum()) + del + format.format(driven.getMean()) + del + format.format(empty.getMean()) + del + format.format(revenue.getMean()) + del + format.format(d_r_d_t); return result; }
From source file:org.matsim.contrib.taxi.util.stats.TaxiStatsWriter.java
private void addStats(CSVLineBuilder lineBuilder, String format1, String format2, DescriptiveStatistics stats) { lineBuilder.addf(format1, stats.getMean()).// addf(format1, stats.getStandardDeviation()).// addEmpty().// addf(format2, stats.getMin()). // addf(format2, stats.getPercentile(2)). // addf(format2, stats.getPercentile(5)). // addf(format2, stats.getPercentile(25)). // addf(format2, stats.getPercentile(50)). // addf(format2, stats.getPercentile(75)). // addf(format2, stats.getPercentile(95)). // addf(format2, stats.getPercentile(98)). // addf(format2, stats.getMax()); }
From source file:org.moeaframework.core.PRNGTest.java
/** * Asserts that the statistical distribution satisfies the properties of a * real-valued uniform distribution between {@code min} and {@code max}. * //from w w w . j a va 2s. co m * @param min the minimum bounds of the uniform distribution * @param max the maximum bounds of the uniform distribution * @param statistics the captures statistics of a sampled distribution */ private void testUniformDistribution(double min, double max, DescriptiveStatistics statistics) { Assert.assertEquals((min + max) / 2.0, statistics.getMean(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(Math.pow(max - min, 2.0) / 12.0, statistics.getVariance(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(0.0, statistics.getSkewness(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(-6.0 / 5.0, statistics.getKurtosis(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(min, statistics.getMin(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(max, statistics.getMax(), TestThresholds.STATISTICS_EPS); }
From source file:org.moeaframework.core.PRNGTest.java
/** * Asserts that the statistical distribution satisfies the properties of an * integer-valued uniform distribution between {@code min} and {@code max}. * /*from w ww .j a v a2s. co m*/ * @param min the minimum bounds of the uniform distribution * @param max the maximum bounds of the uniform distribution * @param statistics the captures statistics of a sampled distribution */ private void testUniformDistribution(int min, int max, DescriptiveStatistics statistics) { int n = max - min + 1; int nn = n * n; Assert.assertEquals((min + max) / 2.0, statistics.getMean(), TestThresholds.STATISTICS_EPS); Assert.assertEquals((nn - 1) / 12.0, statistics.getVariance(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(0.0, statistics.getSkewness(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(-(6.0 * (nn + 1)) / (5.0 * (nn - 1)), statistics.getKurtosis(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(min, statistics.getMin(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(max, statistics.getMax(), TestThresholds.STATISTICS_EPS); }
From source file:org.moeaframework.core.PRNGTest.java
/** * Asserts that the statistical distribution satisfies the properties of a * Gaussian distribution with the specified mean and standard deviation. * /*from w w w.j a v a2 s . co m*/ * @param mu the mean value of the distribution * @param sigma the standard deviation of the distribution * @param statistics the captures statistics of a sampled distribution */ private void testGaussianDistribution(double mu, double sigma, DescriptiveStatistics statistics) { Assert.assertEquals(mu, statistics.getMean(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(sigma * sigma, statistics.getVariance(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(0.0, statistics.getSkewness(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(0.0, statistics.getKurtosis(), TestThresholds.STATISTICS_EPS); }
From source file:org.moeaframework.TestUtils.java
/** * Asserts that the statistical distribution satisfies the properties of an * integer-valued uniform distribution between {@code min} and {@code max}. * /* w ww .j av a 2 s.c o m*/ * @param min the minimum bounds of the uniform distribution * @param max the maximum bounds of the uniform distribution * @param statistics the captures statistics of a sampled distribution */ public static void assertUniformDistribution(int min, int max, DescriptiveStatistics statistics) { int n = max - min + 1; int nn = n * n; assertEquals((min + max) / 2.0, statistics.getMean()); assertEquals((nn - 1) / 12.0, statistics.getVariance()); assertEquals(0.0, statistics.getSkewness()); assertEquals(-(6.0 * (nn + 1)) / (5.0 * (nn - 1)), statistics.getKurtosis()); assertEquals(min, statistics.getMin()); assertEquals(max, statistics.getMax()); }
From source file:org.moeaframework.util.sequence.SequenceTest.java
/** * Asserts that the collected statistics appear to be from a uniform * distribution.//from w ww. j a va2s .c o m * * @param min the minimum value * @param max the maximum value * @param statistics the collected statistics */ public void testUniformDistribution(double min, double max, DescriptiveStatistics statistics) { Assert.assertEquals((min + max) / 2.0, statistics.getMean(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(Math.pow(max - min, 2.0) / 12.0, statistics.getVariance(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(0.0, statistics.getSkewness(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(-6.0 / 5.0, statistics.getKurtosis(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(min, statistics.getMin(), TestThresholds.STATISTICS_EPS); Assert.assertEquals(max, statistics.getMax(), TestThresholds.STATISTICS_EPS); }
From source file:org.mot.common.math.CalculatorFactory.java
public Double getAverage(Double[] data, Integer windowSize) { // Create a DescriptiveStats instance and set the window size to 100 DescriptiveStatistics stats = new DescriptiveStatistics(); stats.setWindowSize(windowSize);/*w w w . java 2 s .c om*/ for (int i = 0; i < data.length; i++) { if (data[i] != null) { stats.addValue(data[i]); } else { break; } } return stats.getMean(); }