MATSIM
Bins.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2012 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.analysis;
21 
22 import java.io.BufferedWriter;
23 import java.io.IOException;
24 import java.text.DecimalFormat;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Vector;
28 
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
32 import org.matsim.core.utils.io.IOUtils;
33 
34 public class Bins {
35 
36  protected double interval;
37  protected int numberOfBins;
38  protected double maxVal;
39  protected String desc;
40  protected List<BinEntry> entries = new Vector<BinEntry>();
41  protected double [] bins;
42 
43  private final static Logger log = LogManager.getLogger(Bins.class);
44 
45  public Bins(double interval, double maxVal, String desc) {
46  this.interval = interval;
47  this.maxVal = maxVal;
48  this.numberOfBins = (int)Math.ceil(maxVal / interval);
49  this.desc = desc;
50  this.bins = new double[this.numberOfBins];
51  }
52  public void addValues(double[] values, double[] weights) {
53  for (int index = 0; index < values.length; index++) {
54  this.addVal(values[index], weights[index]);
55  }
56  }
57 
58  public void addVal(double value, double weight) {
59  int index = (int)Math.floor(value / interval);
60  // values > maximum value are assigned to the last bin
61  if (value >= maxVal) {
62  index = this.numberOfBins -1;
63  }
64 
65  // values < 0.0 value are assigned to the first bin
66  if (value < 0.0) {
67  log.error("Value < 0.0 received");
68  index = 0;
69  }
70 
71  this.bins[index] += weight;
72  this.entries.add(new BinEntry(value, weight));
73  }
74 
75  public void clear() {
76  this.entries.clear();
77  this.bins = new double[this.numberOfBins];
78  }
79 
80  public void plotBinnedDistribution(String path, String xLabel, String xUnit) {
81  String [] categories = new String[this.numberOfBins];
82  for (int i = 0; i < this.numberOfBins; i++) {
83  categories[i] = Integer.toString(i);
84  }
85  Double[] values = new Double[this.entries.size()];
86  Double[] weights = new Double[this.entries.size()];
87 
88  for (int index = 0; index < this.entries.size(); index++) {
89  values[index] = this.entries.get(index).getValue();
90  weights[index] = this.entries.get(index).getWeight();
91  }
92 
93  DecimalFormat formatter = new DecimalFormat("0.0000");
94  String s = xLabel + " " +
95  "[interval = " + formatter.format(this.interval) + xUnit + "]" +
96  "[number of entries = " + this.entries.size() + "]" +
97  "[mean = " + formatter.format(this.weightedMean(values, weights)) + xUnit + "]" +
98  "[median = " + formatter.format(this.median(values)) + xUnit + "]" +
99  "[max = " + formatter.format(this.getMax(values)) + xUnit + "]";
100 
101  BarChart chart =
102  new BarChart(desc, s , "#", categories);
103  chart.addSeries("Bin size", this.bins);
104  chart.saveAsPng(path + desc + ".png", 1600, 800);
105 
106  try {
107  BufferedWriter out = IOUtils.getBufferedWriter(path + desc + ".txt");
108  out.write("Bin [interval = " + this.interval + " " + xUnit + "]\t" + "#" + "\n");
109  for (int j = 0; j < bins.length; j++) {
110  out.write(j + "\t" + bins[j] + "\n");
111  }
112  out.flush();
113  out.close();
114  } catch (IOException e) {
115  e.printStackTrace();
116  }
117  }
118  public double[] getBins() {
119  return bins;
120  }
121  public void setBins(double[] bins) {
122  this.bins = bins;
123  }
124 
125  public double getInterval() {
126  return interval;
127  }
128  public void setInterval(double interval) {
129  this.interval = interval;
130  }
131  // ----------------------------------------------------------------------------
132  private double median(Double [] values) {
133  List<Double> list = new Vector<Double>();
134 
135  Collections.addAll(list, values);
136  return median(list);
137  }
138 
139  public double median(List<Double> values) {
140 
141  if (values.size() == 0) return 0.0;
142 
143  Collections.sort(values);
144  if (values.size() % 2 != 0) {
145  return values.get((values.size()+1)/2-1);
146  }
147  else {
148  double lower = values.get(values.size()/2-1);
149  double upper = values.get(values.size()/2);
150  return (lower + upper) / 2.0;
151  }
152  }
153 
154  public double mean(List<Double> values) {
155  double sum = 0.0;
156  int cnt = 0;
157  if (values.size() == 0) return 0.0;
158  for (Double value : values) {
159  sum += value;
160  cnt++;
161  }
162  return sum / cnt;
163  }
164 
165  public double weightedMean(List<Double> values, List<Double> weights) {
166  return weightedMean(values.toArray(new Double[values.size()]), weights.toArray(new Double[weights.size()]));
167  }
168 
169  public double weightedMean(Double[] values, Double[] weights) {
170  double sumValues = 0.0;
171  double sumWeights = 0.0;
172 
173  if (values.length == 0) return 0.0;
174 
175  if (values.length != weights.length ) {
176  log.info("size of weights and values not identical");
177  return -1;
178  }
179  for (int index = 0; index < values.length; index++) {
180  sumValues += (values[index] * weights[index]);
181  sumWeights += weights[index];
182  }
183  return sumValues / sumWeights;
184  }
185 
186  public double getMax(List<Double> values) {
187  return getMax(values.toArray(new Double[values.size()]));
188  }
189 
190  public double getMax(Double[] values) {
191  double maxVal = Double.MIN_VALUE;
192  for (Double v : values) {
193  if (v > maxVal) {
194  maxVal = v;
195  }
196  }
197  return maxVal;
198  }
199 }
void setInterval(double interval)
Definition: Bins.java:128
double getMax(List< Double > values)
Definition: Bins.java:186
void plotBinnedDistribution(String path, String xLabel, String xUnit)
Definition: Bins.java:80
List< BinEntry > entries
Definition: Bins.java:40
double median(Double [] values)
Definition: Bins.java:132
double getMax(Double[] values)
Definition: Bins.java:190
void addValues(double[] values, double[] weights)
Definition: Bins.java:52
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
double weightedMean(Double[] values, Double[] weights)
Definition: Bins.java:169
static final Logger log
Definition: Bins.java:43
double weightedMean(List< Double > values, List< Double > weights)
Definition: Bins.java:165
Definition: BinEntry.java:22
double median(List< Double > values)
Definition: Bins.java:139
Bins(double interval, double maxVal, String desc)
Definition: Bins.java:45
double [] getBins()
Definition: Bins.java:118
void saveAsPng(final String filename, final int width, final int height)
Definition: ChartUtil.java:65
void addSeries(final String title, final double[] values)
Definition: BarChart.java:103
void addVal(double value, double weight)
Definition: Bins.java:58
double mean(List< Double > values)
Definition: Bins.java:154
void setBins(double[] bins)
Definition: Bins.java:121