SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TallyHistogram.java
1package umontreal.ssj.stat;
2
3/*
4 * Class: TallyHistogram
5 * Description: A Tally that also builds a Histogram
6 * Environment: Java
7 * Software: SSJ
8 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
9 * Organization: DIRO, Universite de Montreal
10 * @author Richard Simard and Pierre L'Ecuyer
11 * @since January 2011
12 *
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *
26 */
27
28import umontreal.ssj.util.PrintfFormat;
29
44public class TallyHistogram extends Tally {
45 protected int numBins; // number of bins
46 protected int[] count; // counter for number of values in bin[i].
47 protected int leftCount; // Count values that are less than a.
48 protected int rightCount; // Count values that are larger than b.
49 protected double m_h; // width of 1 bin
50 protected double m_a; // left boundary of first bin
51 protected double m_b; // right boundary of last bin
52
63 public TallyHistogram(double a, double b, int numBins) {
64 super();
65 init(a, b, numBins);
66 }
67
76 public TallyHistogram(String name, double a, double b, int numBins) {
77 super(name);
78 init(a, b, numBins);
79 }
80
89 public void init(double a, double b, int numBins) {
90 /*
91 * The counters count[0] to count[s-1] contains the number of observations
92 * falling in the interval [a, b]. leftCount is the number of observations < a,
93 * and rightCount is the number of observations > b.
94 */
95 super.init();
96 if (b <= a)
97 throw new IllegalArgumentException(" b <= a");
98 count = new int[numBins];
99 this.numBins = numBins;
100 m_h = (b - a) / (double) numBins;
101 m_a = a;
102 m_b = b;
103 leftCount = rightCount = 0;
104 for (int i = 0; i < numBins; i++)
105 count[i] = 0;
106 }
107
113 public void init() {
114 super.init();
115 leftCount = rightCount = 0;
116 for (int i = 0; i < numBins; i++)
117 count[i] = 0;
118 }
119
123 public void fillFromArray(double[] obs, int numObs) {
124 init();
125 for (int i = 0; i < numObs; i++)
126 add(obs[i]);
127 }
128
132 public void fillFromArray(double[] obs) {
133 fillFromArray(obs, obs.length);
134 }
135
140 fillFromArray(ts.getArray(), ts.numberObs());
141 }
142
151 public void add(double x) {
152 super.add(x);
153 if (x < m_a)
154 ++leftCount;
155 else if (x > m_b)
156 ++rightCount;
157 else {
158 int i = (int) ((x - m_a) / m_h);
159 ++count[i];
160 }
161 }
162
168 TallyHistogram image = (TallyHistogram) super.clone();
169 int i = 0;
170 int j = numBins - 1; // last bin in the initial histogram
171 int cpL = 0; // number of empty bins from left initialized to zero
172 int cpR = 0; // number of empty bins from right initialized to zero
173 while (count[i] == 0) {
174 i++;
175 cpL++;
176 }
177 while (count[j] == 0) {
178 j--;
179 cpR++;
180 }
181 int[] coco = new int[numBins - cpL - cpR];
182 System.arraycopy(count, i, coco, 0, j - i + 1);
183 image.count = coco;
184 image.m_h = m_h;
185 image.m_a = m_a + (cpL * m_h);
186 image.m_b = m_b - (cpR * m_h);
187 image.numBins = numBins - cpL - cpR;
188 image.leftCount = leftCount;
189 image.rightCount = rightCount;
190 return image;
191 }
192
202 if (this.numBins != other.numBins)
203 throw new IllegalArgumentException("different number of bin in two histogram to merge");
204 TallyHistogram image = (TallyHistogram) super.clone();
205 int[] countNew = new int[numBins];
206 System.arraycopy(count, 0, countNew, 0, numBins);
207 int coOther[] = other.getCounters();
208 for (int i = 0; i < countNew.length; i++)
209 countNew[i] = countNew[i] + coOther[i];
210 image.count = countNew;
211 image.leftCount = leftCount + other.leftCount;
212 image.rightCount = rightCount + other.rightCount;
213 image.m_h = m_h;
214 image.m_a = m_a;
215 image.m_b = m_b;
216 image.numBins = numBins;
217 return image;
218
219 }
220
228 TallyHistogram image = (TallyHistogram) super.clone();
229 int numBinsNew = (int) Math.ceil((double) numBins / (double) g);
230 int[] countNew = new int[numBinsNew];
231 int b = 0;
232 for (int j = 0; j < numBinsNew - 1; j++) {
233 for (int i = b; i < b + g; i++)
234 countNew[j] += count[i];
235 b = b + g;
236 }
237 while (b < numBins - 1) {
238 countNew[numBinsNew - 1] += count[b];
239 b++;
240 }
241 image.count = countNew;
242 image.m_h = m_h * g;
243 image.m_a = m_a;
244 image.m_b = m_h * numBinsNew;
245 image.numBins = numBinsNew;
246 image.leftCount = leftCount;
247 image.rightCount = rightCount;
248 return image;
249 }
250
257 public int[] getCounters() {
258 return count;
259 }
260
266 public int getNumBins() {
267 return numBins;
268 }
269
275 public double getA() {
276 return m_a;
277 }
278
284 public double getB() {
285 return m_b;
286 }
287
293 public double getH() {
294 return m_h;
295 }
296
308 int total = 0;
309 for (int num : count) {
310 total += num;
311 }
312 return (double) total / (double) (total + leftCount + rightCount);
313 }
314
319 TallyHistogram image = (TallyHistogram) super.clone();
320 int[] coco = new int[numBins];
321 System.arraycopy(count, 0, coco, 0, numBins);
322 image.count = coco;
323 image.leftCount = leftCount;
324 image.rightCount = rightCount;
325 image.m_h = m_h;
326 image.m_a = m_a;
327 image.m_b = m_b;
328 image.numBins = numBins;
329 return image;
330 }
331
335 public String toString() {
336 StringBuffer sb = new StringBuffer();
337 sb.append("---------------------------------------" + PrintfFormat.NEWLINE);
338 sb.append(name + PrintfFormat.NEWLINE);
339 sb.append("Interval = [ " + m_a + ", " + m_b + " ]" + PrintfFormat.NEWLINE);
340 sb.append("Number of bins = " + numBins + PrintfFormat.NEWLINE);
341 sb.append(PrintfFormat.NEWLINE + "Counters = {" + PrintfFormat.NEWLINE);
342 sb.append(" (-inf, " + PrintfFormat.f(6, 3, m_a) + ") " + leftCount + PrintfFormat.NEWLINE);
343 for (int i = 0; i < numBins; i++) {
344 double a = m_a + (i - 1) * m_h;
345 double b = m_a + i * m_h;
346 sb.append(" (" + PrintfFormat.f(6, 3, a) + ", " + PrintfFormat.f(6, 3, b) + ") " + count[i]
348 }
349 sb.append(" (" + PrintfFormat.f(6, 3, m_b) + ", inf) " + rightCount + PrintfFormat.NEWLINE);
350 sb.append("}" + PrintfFormat.NEWLINE);
351 return sb.toString();
352 }
353
354}
TallyHistogram(double a, double b, int numBins)
Constructs a TallyHistogram statistical probe.
double getProportionInBoundaries()
Returns the proportion of the collected observations that lie within the boundaries.
TallyHistogram aggregateBins(int g)
Merges bins by groups of size .
TallyHistogram(String name, double a, double b, int numBins)
Constructs a new TallyHistogram statistical probe with name name.
TallyHistogram trimHistogram()
Remove empty bins in the tails (left and right), without changing the bin size.
int getNumBins()
Returns the number of bins .
double getH()
Returns the width of the bins.
int[] getCounters()
Returns the array of bin counters.
TallyHistogram addHistograms(TallyHistogram other)
Merges this histogram with the other histogram, by adding the bin counts of the two histograms.
void init()
Initializes all the counters and accumulators, including those of the Tally object.
void fillFromTallyStore(TallyStore ts)
Fills this object from the observations in a TallyStore object.
double getA()
Returns the left boundary of the interval .
void add(double x)
Gives a new observation to the statistical probe.
void fillFromArray(double[] obs, int numObs)
Fills this object from the first numObs observations in array obs.
void init(double a, double b, int numBins)
Initializes this object.
double getB()
Returns the right boundary of the interval .
TallyHistogram clone()
Clones this object and the array that stores the counters.
String toString()
Returns the bin counters as a String.
void fillFromArray(double[] obs)
Fills this object from the entire array obs.
This class is a variant of Tally for which the individual observations are stored in a list implement...
double[] getArray()
Returns the observations stored in this probe.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143
Tally()
Constructs a new unnamed Tally statistical probe.
Definition Tally.java:66
This class acts like a StringBuffer which defines new types of append methods.
static String f(double x)
Same as f(0, 6, x).
static final String NEWLINE
End-of-line symbol or line separator.