-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSimpleHistogram.java
More file actions
265 lines (210 loc) · 6.88 KB
/
SimpleHistogram.java
File metadata and controls
265 lines (210 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
* and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 1990-2008,
*/
package org.javasim.stats;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* A simple histogram with a set number of buckets.
*/
public class SimpleHistogram extends PrecisionHistogram
{
/**
* Create with 'nbuckets' evenly distributed over the range 'min' to 'max'.
*/
public SimpleHistogram(double min, double max, long nbuckets)
{
if (min < max)
{
minIndex = min;
maxIndex = max;
}
else
{
minIndex = max;
maxIndex = min;
}
if (nbuckets > 0)
numberBuckets = nbuckets;
else
nbuckets = 1;
width = (max - min) / numberBuckets;
reset();
}
/**
* Create a number of buckets with width 'w' evenly distributed over the
* range 'min' to 'max'.
*/
public SimpleHistogram(double min, double max, double w)
{
if (min < max)
{
minIndex = min;
maxIndex = max;
}
else
{
minIndex = max;
maxIndex = min;
}
if (w > 0)
width = w;
else
width = 2.0;
numberBuckets = (long) ((max - min) / width);
if ((max - min) / width - numberBuckets > 0)
numberBuckets++;
reset();
}
/**
* @param value add to the histogram. If it is outside the range of the histogram
* then raise an exception, otherwise find the appropriate bucket and
* increment it.
*/
public void setValue (double value) throws IllegalArgumentException
{
if ((value < minIndex) || (value > maxIndex))
throw new IllegalArgumentException("Value " + value
+ " is beyond histogram range [ " + minIndex + ", "
+ maxIndex + " ]");
for (Bucket ptr = Head; ptr != null; ptr = ptr.cdr())
{
double bucketValue = ptr.name();
if ((value == bucketValue) || (value <= bucketValue + width))
{
super.setValue(ptr.name());
return;
}
}
// shouldn't get here!!
throw new IllegalArgumentException("Something went wrong with "
+ value);
}
/**
* Empty the histogram. Always keep the number of buckets that
* were originally specified though.
*/
public void reset ()
{
double value = minIndex;
super.reset();
// pre-create buckets with given width
for (int i = 0; i < numberBuckets; value += width, i++)
create(value);
}
/**
* Get the number of entries in bucket 'name'.
*
* @param name the id of the bucket.
* @return the number of entries in the bucket.
*/
public double sizeByName (double name) throws IllegalArgumentException
{
if ((name < minIndex) || (name > maxIndex))
throw new IllegalArgumentException("Argument out of range.");
for (Bucket ptr = Head; ptr != null; ptr = ptr.cdr())
{
double bucketValue = ptr.name();
if ((name == bucketValue) || (name <= bucketValue + width))
return ptr.size();
}
throw new IllegalArgumentException("Name " + name + " out of range.");
}
/**
* @return the width of each bucket.
*/
public double Width ()
{
return width;
}
/**
* Print out information about the histogram.
*/
public void print ()
{
System.out.println("SimpleHistogram Data:");
System.out.println("Maximum index range : " + maxIndex);
System.out.println("Minimum index range : " + minIndex);
System.out.println("Number of buckets : " + numberBuckets);
System.out.println("Width of each bucket : " + width);
super.print();
}
/**
* Save the state of the histogram to the file named 'fileName'.
*
* @param fileName the file to use.
* @return <code>true</code> if it succeeded, <code>false</code> otherwise.
*/
public boolean saveState (String fileName) throws IOException
{
FileOutputStream f = new FileOutputStream(fileName);
DataOutputStream oFile = new DataOutputStream(f);
boolean res = saveState(oFile);
f.close();
return res;
}
/**
* Save the state of the histogram to the stream 'oFile'.
*
* @param oFile the stream to use.
* @return <code>true</code> if it succeeded, <code>false</code> otherwise.
*/
public boolean saveState (DataOutputStream oFile) throws IOException
{
oFile.writeDouble(minIndex);
oFile.writeDouble(maxIndex);
oFile.writeDouble(width);
oFile.writeLong(numberBuckets);
return super.saveState(oFile);
}
/**
* Restore the histogram state from the file 'fileName'.
*
* @param fileName the file to use.
* @return <code>true</code> if it succeeded, <code>false</code> otherwise.
*/
public boolean restoreState (String fileName) throws FileNotFoundException,
IOException
{
FileInputStream f = new FileInputStream(fileName);
DataInputStream iFile = new DataInputStream(f);
boolean res = restoreState(iFile);
f.close();
return res;
}
/**
* Restore the histogram state from the stream 'iFile'.
*
* @param iFile the stream to use.
* @return <code>true</code> if it succeeded, <code>false</code> otherwise.
*/
public boolean restoreState (DataInputStream iFile) throws IOException
{
minIndex = iFile.readDouble();
maxIndex = iFile.readDouble();
width = iFile.readDouble();
numberBuckets = iFile.readLong();
return super.restoreState(iFile);
}
private double minIndex;
private double maxIndex;
private double width;
private long numberBuckets;
}