SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ScaledHistogram.java
1package umontreal.ssj.stat;
2
3/*
4 * Class: ScaledHistogram
5 * Description: A histogram whose bin heights are arbitrary real numbers
6 * Environment: Java
7 * Software: SSJ
8 * Copyright (C) 2016 Pierre L'Ecuyer and Universite de Montreal
9 * Organization: DIRO, Universite de Montreal
10 * @author Pierre L'Ecuyer and Mamadou Thiongane
11 * @since December 2016
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
28// import java.util.logging.Logger;
29// import umontreal.ssj.util.PrintfFormat;
30
48
49public class ScaledHistogram {
50 protected int numBins; // number of bins
51 protected double m_h; // width of each bin
52 protected double m_a; // left boundary of first bin
53 protected double m_b; // right boundary of last bin
54 protected double[] height; // rescaled counters: height[j] is the height of bin j.
55 protected double integral; // Total area under the histogram, = (b-a) x sum of heights.
56
57 private ScaledHistogram() {
58 }
59
69 public ScaledHistogram(double a, double b, int numBins) {
70 init(a, b, numBins);
71 }
72
77 public ScaledHistogram(TallyHistogram hist, double integral) {
78 init(hist, integral);
79 }
80
90 public void init(double a, double b, int numBins) {
91 if (b <= a)
92 throw new IllegalArgumentException(" b <= a");
93 this.numBins = numBins;
94 m_h = (b - a) / numBins;
95 m_a = a;
96 m_b = b;
97 height = new double[numBins];
98 for (int i = 0; i < numBins; i++)
99 height[i] = 0;
100 integral = 0.0;
101 }
102
108 public void init(TallyHistogram hist, double integral) {
109 m_a = hist.getA();
110 m_b = hist.getB();
111 m_h = hist.getH();
112 numBins = hist.numBins;
113 height = new double[numBins];
114 this.integral = integral;
115 int count[] = hist.getCounters();
116 double scaleFactor = integral / (hist.numberObs() * m_h);
117 for (int i = 0; i < numBins; i++)
118 height[i] = count[i] * scaleFactor;
119 }
120
124 public void init() {
125 for (int i = 0; i < numBins; i++)
126 height[i] = 0.0;
127 }
128
133 public void rescale(double integral) {
134 double scaleFactor = integral / this.integral;
135 for (int i = 0; i < numBins; i++)
136 height[i] *= scaleFactor;
137 this.integral = integral;
138 }
139
148 public ScaledHistogram averageShiftedHistogram(int r) {
149 ScaledHistogram image = clone();
150 double[] heightNew = image.getHeights();
151 double rscale = 1.0 / (r * r); // Rescaling to be made for each bin.
152 double sum = 0.0;
153 for (int k = 0; k < numBins; k++) {
154 heightNew[k] = r * height[k];
155 for (int ell = 1; ell < r; ell++) {
156 if (k - ell >= 0)
157 heightNew[k] += (r - ell) * height[k - ell];
158 if (k + ell < numBins)
159 heightNew[k] += (r - ell) * height[k + ell];
160 }
161 heightNew[k] *= rscale;
162 sum += heightNew[k];
163 }
164 image.height = heightNew;
165 image.integral = sum * m_h;
166 return image;
167 }
168
175 public ScaledHistogram averageShiftedHistogramTrunc(int r) {
176 ScaledHistogram image = clone();
177 double[] heightNew = image.getHeights();
178 // double rscale = 1.0 / (r * r); // Rescaling to be made for each bin.
179 double sum = 0.0;
180 for (int k = 0; k < numBins; k++) {
181 heightNew[k] = r * height[k]; //
182 int weight = r;
183 for (int ell = 1; ell < r; ell++) {
184 if (k - ell >= 0) {
185 heightNew[k] += (r - ell) * height[k - ell];
186 weight += r - ell;
187 }
188 if (k + ell < numBins) {
189 heightNew[k] += (r - ell) * height[k + ell];
190 weight += r - ell;
191 }
192 }
193 heightNew[k] *= 1.0 / (double) weight;
194 sum += heightNew[k];
195 }
196 image.height = heightNew;
197 image.integral = sum * m_h;
198 return image;
199 }
200
208 public ScaledHistogram averageShiftedHistogram(int r, double[] w) {
209 ScaledHistogram image = clone();
210 double[] heightNew = image.getHeights();
211 double weight = w[0];
212 for (int ell = 1; ell < r; ell++)
213 weight += 2.0 * w[ell];
214 double rscale = 1.0 / weight; // Rescaling factor for each bin.
215 double sum = 0.0;
216 for (int k = 0; k < numBins; k++) {
217 heightNew[k] = w[0] * height[k]; //
218 for (int ell = 1; ell < r; ell++) {
219 if (k - ell >= 0) {
220 heightNew[k] += w[ell] * height[k - ell];
221 }
222 if (k + ell < numBins) {
223 heightNew[k] += w[ell] * height[k + ell];
224 }
225 }
226 heightNew[k] *= rscale;
227 sum += heightNew[k];
228 }
229 image.height = heightNew;
230 image.integral = sum * m_h;
231 return image;
232 }
233
242 public ScaledHistogram averageShiftedHistogramTrunc(int r, double[] w) {
243 ScaledHistogram image = clone();
244 double[] heightNew = image.getHeights();
245 // double rscale = 1.0 / (r * r); // Rescaling to be made for each bin.
246 double sum = 0.0;
247 for (int k = 0; k < numBins; k++) {
248 heightNew[k] = w[0] * height[k]; //
249 double weight = w[0];
250 for (int ell = 1; ell < r; ell++) {
251 if (k - ell >= 0) {
252 heightNew[k] += w[ell] * height[k - ell];
253 weight += w[ell];
254 }
255 if (k + ell < numBins) {
256 heightNew[k] += w[ell] * height[k + ell];
257 weight += w[ell];
258 }
259 }
260 heightNew[k] *= 1.0 / weight;
261 sum += heightNew[k];
262 }
263 image.height = heightNew;
264 image.integral = sum * m_h;
265 return image;
266 }
267
268 /*
269 * public ScaledHistogram averageShiftedHistogram1 (int r) { // if (numBins % r
270 * != 0) // throw new
271 * IllegalArgumentException("r must divide the number of bins.");
272 * ScaledHistogram image = clone(); double[] heightNew = image.getHeights();
273 * double rscale = 1.0 / (r * r); // Rescaling to be made for each bin. double
274 * sum = 0.0; double S1[] = new double[numBins]; double S2[] = new
275 * double[numBins]; S1[0] = height[0]; S2[0] = 0.0; heightNew[0] = r * S1[0];
276 * for (int ell = 1; ell < Math.min(r, numBins); ell++) { S2[0] += height[ell];
277 * heightNew[0] += (r-ell) * height[ell]; } for (int k = 1; k < numBins; k++) {
278 * for (int ell = 1; ell < r; ell++) { S1[k] = S1[k-1] + height[k]; if (k >= r)
279 * S1[k] -= height[k-r]; S2[k] = S2[k-1] - height[k]; if (k+r-1 < numBins) S2[k]
280 * += height[k+r-1]; heightNew[k] = heightNew[k-1] + S2[k-1] - S1[k-1]; }
281 * heightNew[k] *= rscale; sum += heightNew[k]; } image.height = heightNew;
282 * image.integral = sum*m_h; return image; }
283 */
284
289 public ScaledHistogram averageShiftedHistogram1(int r) {
290 ScaledHistogram image = clone();
291 double[] heightNew = image.getHeights();
292 double rscale = 1.0 / (r * r); // Rescaling to be made for each bin.
293 double sum = 0.0;
294 double S1[] = new double[numBins];
295 double S2[] = new double[numBins];
296 S1[0] = height[0];
297 S2[0] = 0.0;
298 heightNew[0] = r * S1[0];
299 for (int ell = 1; ell <= Math.min(r, numBins); ell++) {
300 S2[0] += height[ell];
301 heightNew[0] += (r - ell) * height[ell];
302 }
303 for (int k = 2; k <= numBins; k++) {
304 S1[k - 1] = S1[k - 2] + height[k - 1];
305 if (k >= r)
306 S1[k - 1] -= height[k - r];
307 S2[k - 1] = S2[k - 2] - height[k - 1];
308 if (k + r < numBins)
309 S2[k - 1] += height[k + r - 1];
310 heightNew[k - 1] = heightNew[k - 2] + S2[k - 2] - S1[k - 2];
311 }
312 for (int k = 0; k < numBins; k++) {
313 heightNew[k] *= rscale;
314 sum += heightNew[k];
315 }
316 image.height = heightNew;
317 image.integral = sum * m_h;
318 return image;
319 }
320
328 public int getNumBins() {
329 return numBins;
330 }
331
337 public double getA() {
338 return m_a;
339 }
340
346 public double getB() {
347 return m_b;
348 }
349
353 public double[] getHeights() {
354 return height;
355 }
356
360 public double getIntegral() {
361 return integral;
362 }
363
368 public double ISEvsU01() {
369 double sum = 0.0;
370 for (int j = 0; j < numBins; ++j)
371 sum += (height[j] - 1.0) * (height[j] - 1.0);
372 return sum / numBins;
373 }
374
381 public double ISEvsU01polygonal() {
382 double w0[] = new double[numBins]; // histogram shifted down to zero mean.
383 for (int j = 0; j < numBins; ++j) {
384 w0[j] = height[j] - 1.0;
385 }
386 double a, b;
387 double sum = 0.5 * (w0[0] * w0[0] + w0[numBins - 1] * w0[numBins - 1]);
388 for (int j = 0; j < numBins - 2; ++j) {
389 a = w0[j];
390 b = w0[j + 1];
391 sum += 0.3333333333333333333 * (b * b + a * a + a * b);
392 }
393 return sum / (double) numBins;
394 }
395
399 public ScaledHistogram clone() {
400 ScaledHistogram image = new ScaledHistogram();
401 image.numBins = numBins;
402 image.m_h = m_h;
403 image.m_a = m_a;
404 image.m_b = m_b;
405 image.height = new double[numBins];
406 image.integral = integral;
407 for (int j = 1; j < numBins; ++j)
408 image.height[j] = height[j];
409 return image;
410 }
411
412}
ScaledHistogram averageShiftedHistogram1(int r)
This is supposed to be a faster implementation of averageShiftedHistogram(r).
double[] getHeights()
return the array counts of the histogram.
ScaledHistogram averageShiftedHistogramTrunc(int r, double[] w)
Similar to averageShiftedHistogramTrunc, except that uses a weighted average.
double ISEvsU01()
Computes and returns the integrated square error (ISE) of a histogram w.r.t.
void init(double a, double b, int numBins)
Initializes the ScaledHistogram so it covers the interval , which is divided into numBins bins of equ...
ScaledHistogram averageShiftedHistogram(int r)
Returns an ASH-transformed version of this scaled histogram.
ScaledHistogram averageShiftedHistogramTrunc(int r)
Similar to averageShiftedHistogram, except that it assumes that the density is over a close interval ...
ScaledHistogram(TallyHistogram hist, double integral)
Constructs a ScaledHistogram from hist by normalizing the bin counts so that the integral of the hist...
double getB()
Returns the right boundary of interval .
ScaledHistogram clone()
Clones this object and the array which stores the counters.
double ISEvsU01polygonal()
Computes and returns the ISE of a polygonal density w.r.t.
void init(TallyHistogram hist, double integral)
Initializes this ScaledHistogram using the TallyHistogram hist.
double getIntegral()
return the integral the histogram.
ScaledHistogram averageShiftedHistogram(int r, double[] w)
Similar to averageShiftedHistogram, except that uses a weighted average.
void init()
Initializes all the heights (frequencies) to 0.
ScaledHistogram(double a, double b, int numBins)
Constructs a ScaledHistogram over the interval , which is divided into numBins bins of equal width.
void rescale(double integral)
Rescales the histogram by renormalizing the frequencies so its integral has the value specified by in...
int getNumBins()
Returns the number of bins dividing the interval.
double getA()
Returns the left boundary of interval .
double getH()
Returns the width of the bins.
int[] getCounters()
Returns the array of bin counters.
double getA()
Returns the left boundary of the interval .
double getB()
Returns the right boundary of the interval .
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143