Debian Codegraph @

contrib/ms-sbvar/utilities_dw/histogram/dw_histogram.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <math.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "dw_histogram.h"
23 #include "dw_error.h"
24 #include "dw_std.h"
25
26 static void Resize(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals);
27 static void AddObservationVariable(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals);
28 static void AddObservationFixed(PRECISION x, int *low, int *h, int *high, PRECISION min, PRECISION max, int intervals);
29
30 static PRECISION Cumulative(PRECISION level, int low, int *h, PRECISION min, PRECISION max, int intervals, int sample_size);
31 static PRECISION Percentile(PRECISION percentile, int low, int *h, PRECISION min, PRECISION max, int intervals, int sample_size);
32 static TMatrix MakeHistogram(int low, int *h, PRECISION min, PRECISION max,int intervals, int sample_size,
33 PRECISION min_out, PRECISION max_out, int bins);
34 static TMatrix MakeHistogramAuto(int low, int *h, int high, PRECISION min, PRECISION max, int intervals, int sample_size, int bins);
35
36
37 /*******************************************************************************
38 The following set of routines create a matrix of histograms on the fly.
39 *******************************************************************************/
40 /*
41 Assumes
42 rows > 0
43 cols > 0
44 intervals > 0
45 type = HISTOGRAM_FIXED or HISTOGRAM_VARIABLE
46
47 Results
48 Creates and returns a matrix histogram data structure. The size of the
49 matrix is m x n and the number of intervals is intrvls.
50
51 */
52 TMatrixHistogram *CreateMatrixHistogram(int rows, int cols, int intervals, int type)
53 {
54 int i, j;
55 TMatrixHistogram *h;
56
57 if (!(h=(TMatrixHistogram *)dw_malloc(sizeof(TMatrixHistogram)))) dw_Error(MEM_ERR);
58
59 if (!(h->freq=(int***)dw_malloc(rows*sizeof(int**)))) dw_Error(MEM_ERR);
60 for (i=rows-1; i >= 0; i--)
61 {
62 if (!(h->freq[i]=(int**)dw_malloc(cols*sizeof(int*)))) dw_Error(MEM_ERR);
63 for (j=cols-1; j >= 0; j--)
64 if (!(h->freq[i][j]=(int*)dw_malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
65 }
66
67 if (!(h->low=(int**)dw_malloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
68 for (i=rows-1; i >= 0; i--)
69 if (!(h->low[i]=(int*)dw_malloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
70
71 if (!(h->high=(int**)dw_malloc(rows*sizeof(int*)))) dw_Error(MEM_ERR);
72 for (i=rows-1; i >= 0; i--)
73 if (!(h->high[i]=(int*)dw_malloc(cols*sizeof(int)))) dw_Error(MEM_ERR);
74
75 h->Min=CreateMatrix(rows,cols);
76 h->Max=CreateMatrix(rows,cols);
77
78 h->rows=rows;
79 h->cols=cols;
80 h->intervals=intervals;
81 h->sample_size=0;
82 h->type=type;
83
84 return h;
85 }
86
87 void SetMaxMinMatrixHistogram(TMatrix Min, TMatrix Max, TMatrixHistogram *h)
88 {
89 EquateMatrix(h->Min,Min);
90 EquateMatrix(h->Max,Max);
91 h->sample_size=0;
92 }
93
94 void FreeMatrixHistogram(TMatrixHistogram *h)
95 {
96 int i, j;
97 for (i=h->rows-1; i >= 0; i--)
98 {
99 for (j=h->cols-1; j >= 0; j--) dw_free(h->freq[i][j]);
100 dw_free(h->freq[i]);
101 }
102 dw_free(h->freq);
103 for (i=h->rows-1; i >= 0; i--) dw_free(h->low[i]);
104 dw_free(h->low);
105 for (i=h->rows-1; i >= 0; i--) dw_free(h->high[i]);
106 dw_free(h->high);
107 FreeMatrix(h->Min);
108 FreeMatrix(h->Max);
109 dw_free(h);
110 }
111
112 void AddMatrixObservation(TMatrix X, TMatrixHistogram *h)
113 {
114 int i, j, k;
115
116 if ((h->rows != RowM(X)) || (h->cols != ColM(X))) dw_Error(SIZE_ERR);
117
118 if (h->sample_size <= 0)
119 {
120 for (i=h->rows-1; i >= 0; i--)
121 for (j=h->cols-1; j >= 0; j--)
122 {
123 h->low[i][j]=h->high[i][j]=0;
124 for (k=h->intervals-1; k >= 0; k--) h->freq[i][j][k]=0;
125 }
126 if (h->type == HISTOGRAM_VARIABLE)
127 for (i=h->rows-1; i >= 0; i--)
128 for (j=h->cols-1; j >= 0; j--)
129 ElementM(h->Min,i,j)=ElementM(h->Max,i,j)=ElementM(X,i,j);
130 }
131
132 if (h->type == HISTOGRAM_FIXED)
133 for (i=h->rows-1; i >= 0; i--)
134 for (j=h->cols-1; j >= 0; j--)
135 AddObservationFixed(ElementM(X,i,j),h->low[i]+j,h->freq[i][j],h->high[i]+j,ElementM(h->Min,i,j),ElementM(h->Max,i,j),h->intervals);
136 else
137 for (i=h->rows-1; i >= 0; i--)
138 for (j=h->cols-1; j >= 0; j--)
139 AddObservationVariable(ElementM(X,i,j),h->freq[i][j],&ElementM(h->Min,i,j),&ElementM(h->Max,i,j),h->intervals);
140
141 h->sample_size++;
142 }
143
144 TMatrix MatrixPercentile(TMatrix X, PRECISION percentile, TMatrixHistogram *h)
145 {
146 int i, j;
147
148 if (!X)
149 { if (!(X=CreateMatrix(h->rows,h->cols))) return (TMatrix)NULL; }
150 else
151 if ((h->rows != RowM(X)) || (h->cols != ColM(X))) { dw_Error(SIZE_ERR); return (TMatrix)NULL; }
152
153 for (i=h->rows-1; i >= 0; i--)
154 for (j=h->cols-1; j >= 0; j--)
155 ElementM(X,i,j)=Percentile(percentile,h->low[i][j],h->freq[i][j],ElementM(h->Min,i,j),ElementM(h->Max,i,j),h->intervals,h->sample_size);
156
157 return X;
158 }
159
160 /*
161 Returns the probability that an observation is less than or equal to
162 level.
163
164 Assumes
165 For 0 <= i < h->rows and 0 <= j < h->cols, let
166
167 I[i][j][k]=(h->min[i][j] + k*inc[i][j], h->min[i][j] + (k+1)*inc[i][j]),
168
169 where inc[i][j]=(h->max[i][j] - h->min[i][j])/h->samples_size. The
170 distribution is uniform on I[i][k][j] and
171
172 P(h->min[i][j] + k*inc[i][j] < x[i][j] < h->min[i][j] + (k+1)*inc[i][j])
173 = h->freq[i][j][k]/h->sample_size.
174
175 Furthermore,
176
177 P(x[i][j] < h->min[i][j]) = 0 and P(x[i][j] > h->min[i][j]) = 0.
178
179 In addition, if h->type == FIXED, then
180
181 P(x[i][j] = h->min[i][j]) = h->low[i][j]/h->sample_size
182
183 and
184
185 P(x[i][j] = h->min[i][j]) = h->high[i][j]/h->sample_size.
186 */
187 void MatrixCumulative(TMatrix P, TMatrix Level, TMatrixHistogram *h)
188 {
189 int i, j;
190
191 if ((h->rows != RowM(P)) || (h->cols != ColM(P)) ||
192 (h->rows != RowM(Level)) || (h->cols != ColM(Level)))
193 dw_Error(SIZE_ERR);
194
195 for (i=h->rows-1; i >= 0; i--)
196 for (j=h->cols-1; j >= 0; j--)
197 ElementM(P,i,j)=Cumulative(ElementM(Level,i,j),h->low[i][j],h->freq[i][j],ElementM(h->Min,i,j),ElementM(h->Max,i,j),h->intervals,h->sample_size);
198 }
199
200 TMatrix PlotMatrixHistogramAuto(int i, int j, int bins, TMatrixHistogram *h)
201 {
202 return MakeHistogramAuto(h->low[i][j],h->freq[i][j],h->high[i][j],ElementM(h->Min,i,j),ElementM(h->Max,i,j),h->intervals,h->sample_size,bins);
203 }
204
205 TMatrix PlotMatrixHistogram(int i, int j, PRECISION min, PRECISION max, int bins, TMatrixHistogram *h)
206 {
207 return MakeHistogram(h->low[i][j],h->freq[i][j],ElementM(h->Min,i,j),ElementM(h->Max,i,j),h->intervals,h->sample_size,min,max,bins);
208 }
209
210 /*******************************************************************************
211 The following set of routines create a vector of histograms on the fly.
212 *******************************************************************************/
213 TVectorHistogram *CreateVectorHistogram(int dim, int intervals, int type)
214 {
215 int i;
216 TVectorHistogram *h;
217
218 if (!(h=(TVectorHistogram *)dw_malloc(sizeof(TVectorHistogram))))
219 dw_Error(MEM_ERR);
220
221 if (!(h->freq=(int**)dw_malloc(dim*sizeof(int*)))) dw_Error(MEM_ERR);
222 for (i=dim-1; i >= 0; i--)
223 if (!(h->freq[i]=(int*)dw_malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
224
225 if (!(h->low=(int*)dw_malloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
226 if (!(h->high=(int*)dw_malloc(dim*sizeof(int)))) dw_Error(MEM_ERR);
227
228 h->Min=CreateVector(dim);
229 h->Max=CreateVector(dim);
230
231 h->dim=dim;
232 h->intervals=intervals;
233 h->sample_size=0;
234 h->type=type;
235
236 return h;
237 }
238
239 void SetMaxMinVectorHistogram(TVector Min, TVector Max, TVectorHistogram *h)
240 {
241 EquateVector(h->Min,Min);
242 EquateVector(h->Max,Max);
243 h->sample_size=0;
244 }
245
246 void FreeVectorHistogram(TVectorHistogram *h)
247 {
248 int i;
249 for (i=h->dim-1; i >= 0; i--) dw_free(h->freq[i]);
250 dw_free(h->freq);
251 dw_free(h->low);
252 dw_free(h->high);
253 FreeVector(h->Min);
254 FreeVector(h->Max);
255 dw_free(h);
256 }
257
258 void AddVectorObservation(TVector x, TVectorHistogram *h)
259 {
260 int i, k;
261
262 if (h->dim != DimV(x)) dw_Error(SIZE_ERR);
263
264 if (h->sample_size <= 0)
265 {
266 for (i=h->dim-1; i >= 0; i--)
267 {
268 h->low[i]=h->high[i]=0;
269 for (k=h->intervals-1; k >= 0; k--) h->freq[i][k]=0;
270 }
271 if (h->type == HISTOGRAM_VARIABLE)
272 for (i=h->dim-1; i >= 0; i--)
273 ElementV(h->Min,i)=ElementV(h->Max,i)=ElementV(x,i);
274 }
275
276 if (h->type == HISTOGRAM_FIXED)
277 for (i=h->dim-1; i >= 0; i--)
278 AddObservationFixed(ElementV(x,i),h->low+i,h->freq[i],h->high+i,ElementV(h->Min,i),ElementV(h->Max,i),h->intervals);
279 else
280 for (i=h->dim-1; i >= 0; i--)
281 AddObservationVariable(ElementV(x,i),h->freq[i],&ElementV(h->Min,i),&ElementV(h->Max,i),h->intervals);
282
283 h->sample_size++;
284 }
285
286 TVector VectorPercentile(TVector x, PRECISION percentile, TVectorHistogram *h)
287 {
288 int i;
289
290 if (!x)
291 { if (!(x=CreateVector(h->dim))) return (TVector)NULL; }
292 else
293 if (h->dim != DimV(x)) { dw_Error(SIZE_ERR); return (TVector)NULL; }
294
295 for (i=h->dim-1; i >= 0; i--)
296 ElementV(x,i)=Percentile(percentile,h->low[i],h->freq[i],ElementV(h->Min,i),ElementV(h->Max,i),h->intervals,h->sample_size);
297
298 return x;
299 }
300
301
302
303 /*
304 Returns the probability that an observation is less than or equal to
305 level.
306
307 Assumes
308 For 0 <= i < h->dim, let
309
310 I[i][k]=(h->min[i] + k*inc[i], h->min[i] + (k+1)*inc[i]),
311
312 where inc[i]=(h->max[i] - h->min[i])/h->samples_size. The distribution
313 is uniform on I[i][k] and
314
315 P(h->min[i] + k*inc[i] < x[i] < h->min[i] + (k+1)*inc[i])
316 = h->freq[i][k]/h->sample_size.
317
318 Furthermore,
319
320 P(x[i] < h->min[i]) = 0 and P(x[i] > h->min[i]) = 0.
321
322 In addition, if h->type == FIXED, then
323
324 P(x[i] = h->min[i]) = h->low[i]/h->sample_size
325
326 and
327
328 P(x[i] = h->min[i]) = h->high[i]/h->sample_size.
329 */
330 void VectorCumulative(TVector p, TVector level, TVectorHistogram *h)
331 {
332 int i;
333
334 if (h->dim != DimV(p) || (h->dim != DimV(level)))
335 dw_Error(SIZE_ERR);
336
337 for (i=h->dim-1; i >= 0; i--)
338 ElementV(p,i)=Cumulative(ElementV(level,i),h->low[i],h->freq[i],ElementV(h->Min,i),ElementV(h->Max,i),h->intervals,h->sample_size);
339 }
340
341 TMatrix PlotVectorHistogramAuto(int i, int bins, TVectorHistogram *h)
342 {
343 return MakeHistogramAuto(h->low[i],h->freq[i],h->high[i],ElementV(h->Min,i),ElementV(h->Max,i),h->intervals,h->sample_size,bins);
344 }
345
346 TMatrix PlotVectorHistogram(int i, PRECISION min, PRECISION max, int bins, TVectorHistogram *h)
347 {
348 return MakeHistogram(h->low[i],h->freq[i],ElementV(h->Min,i),ElementV(h->Max,i),h->intervals,h->sample_size,min,max,bins);
349 }
350 /*******************************************************************************
351 The following set of routines create a scalar histogram on the fly.
352 *******************************************************************************/
353 /*
354 Assumes
355
356 Results
357 Creates and returns a scalar histogram data structure.
358 */
359 TScalarHistogram *CreateScalarHistogram(int intervals, int type)
360 {
361 TScalarHistogram *h;
362
363 if (!(h=(TScalarHistogram *)dw_malloc(sizeof(TScalarHistogram)))) dw_Error(MEM_ERR);
364
365 if (!(h->freq=(int*)dw_malloc(intervals*sizeof(int)))) dw_Error(MEM_ERR);
366
367 h->intervals=intervals;
368 h->sample_size=0;
369 h->type=type;
370
371 return h;
372 }
373
374 void SetMaxMinScalarHistogram(PRECISION Min, PRECISION Max, TScalarHistogram *h)
375 {
376 h->Min=Min;
377 h->Max=Max;
378 h->sample_size=0;
379 }
380
381 void FreeScalarHistogram(TScalarHistogram *h)
382 {
383 dw_free(h->freq);
384 dw_free(h);
385 }
386
387 void AddScalarObservation(PRECISION x, TScalarHistogram *h)
388 {
389 int k;
390
391 if (h->sample_size <= 0)
392 {
393 h->low=h->high=0;
394 for (k=h->intervals-1; k >= 0; k--) h->freq[k]=0;
395 if (h->type == HISTOGRAM_VARIABLE) h->Min=h->Max=x;
396 }
397
398 if (h->type == HISTOGRAM_FIXED)
399 AddObservationFixed(x,&(h->low),h->freq,&(h->high),h->Min,h->Max,h->intervals);
400 else
401 AddObservationVariable(x,h->freq,&(h->Min),&(h->Max),h->intervals);
402
403 h->sample_size++;
404 }
405
406 PRECISION ScalarPercentile(PRECISION percentile, TScalarHistogram *h)
407 {
408 return Percentile(percentile,h->low,h->freq,h->Min,h->Max,h->intervals,h->sample_size);
409 }
410
411 /*
412 Returns the probability that an observation is less than or equal to
413 level.
414
415 Assumes
416 Let
417
418 I[k]=(h->min + k*inc, h->min + (k+1)*inc),
419
420 where inc=(h->max - h->min)/h->samples_size. The distribution
421 is uniform on I[k] and
422
423 P(h->min + k*inc < x < h->min + (k+1)*inc) = h->freq[k]/h->sample_size.
424
425 Furthermore,
426
427 P(x < h->min) = 0 and P(x > h->min) = 0.
428
429 In addition, if h->type == FIXED, then
430
431 P(x = h->min) = h->low/h->sample_size
432
433 and
434
435 P(x = h->min) = h->high/h->sample_size.
436 */
437 PRECISION ScalarCumulative(PRECISION level, TScalarHistogram *h)
438 {
439 return Cumulative(level,h->low,h->freq,h->Min,h->Max,h->intervals,h->sample_size);
440 }
441
442 TMatrix PlotScalarHistogramAuto(int bins, TScalarHistogram *h)
443 {
444 return MakeHistogramAuto(h->low,h->freq,h->Min,h->high,h->Max,h->intervals,h->sample_size,bins);
445 }
446
447 TMatrix PlotScalarHistogram(PRECISION min, PRECISION max, int bins, TScalarHistogram *h)
448 {
449 return MakeHistogram(h->low,h->freq,h->Min,h->Max,h->intervals,h->sample_size,min,max,bins);
450 }
451
452 /*******************************************************************************/
453 /***************************** Low Level Routines ******************************/
454 /*******************************************************************************/
455 /*
456 Resizes the histogram. After resizing, it is guaranteed that *min <= x <= *max.
457 The type of the histogram must be HISTOGRAM_VARIABLE.
458 */
459 static void Resize(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals)
460 {
461 int i, j, k, m;
462 if (x > *max)
463 if (x - *min >= (PRECISION)intervals*(*max - *min))
464 {
465 for (i=1; i < intervals; i++)
466 {
467 h[0]+=h[i];
468 h[i]=0;
469 }
470 *max=x;
471 }
472 else
473 {
474 m=(int)ceil((x - *min)/(*max - *min));
475 for (i=j=0; i < intervals; j++)
476 for(h[j]=h[i++], k=1; (k < m) && (i < intervals); k++)
477 h[j]+=h[i++];
478 for ( ; j < intervals; j++) h[j]=0;
479 *max=*min + m*(*max - *min);
480 if (x > *max) *max=x;
481 }
482 else
483 if (x < *min)
484 {
485 if (*max - x >= (PRECISION)intervals*(*max - *min))
486 {
487 for (j=intervals-1, i=intervals-2; i >= 0; i--)
488 {
489 h[j]+=h[i];
490 h[i]=0;
491 }
492 *min=x;
493 }
494 else
495 {
496 m=(int)ceil((*max - x)/(*max - *min));
497 for (i=j=intervals-1; i >= 0; j--)
498 for(h[j]=h[i--], k=1; (k < m) && (i >= 0); k++)
499 h[j]+=h[i--];
500 for ( ; j >= 0; j--) h[j]=0;
501 *min=*max - m*(*max - *min);
502 if (x < *min) *min=x;
503 }
504 }
505 }
506
507 /*
508 Adds a observation to the histogram. The type of the histogram must
509 be HISTOGRAM_VARIABLE.
510 */
511 static void AddObservationVariable(PRECISION x, int *h, PRECISION *min, PRECISION *max, int intervals)
512 {
513 int i;
514
515 if ((x < *min) || (x > *max)) Resize(x,h,min,max,intervals);
516
517 if (*max > *min)
518 {
519 i=(int)(intervals*(x - *min)/(*max - *min));
520 h[(i < intervals) ? i : intervals-1]++;
521 }
522 else
523 h[0]++;
524 }
525
526 /*
527 Adds a observation to the histogram. The type of the histogram must
528 be HISTOGRAM_FIXED.
529 */
530 static void AddObservationFixed(PRECISION x, int *low, int *h, int *high, PRECISION min, PRECISION max, int intervals)
531 {
532 PRECISION y=floor(intervals*(x - min)/(max - min));
533 if (y < 0)
534 (*low)++;
535 else
536 if (y < intervals)
537 h[(int)y]++;
538 else
539 (*high)++;
540 }
541
542 /******************************************************************************/
543 /******************************************************************************/
544 /******************************************************************************/
545
546 /*
547 Returns the level such that the probability of observing an observation
548 less than or equal to level is percentile. If there is a point mass at
549 x, and P(y < x) <= percentile <= P(y <= x), then x is returned.
550
551 Assumes
552 Both intervals and sample_size are poitive and low and h[i] are
553 non-negative. Also if
554
555 high = sample_size - (low + h[0] + ... + h[intervals - 1]),
556
557 then high is non-negative.
558
559 If min < max, let inc=(max - min)/intervals and define
560
561 I[k]=(min + k*inc, min + (k+1)*inc),
562
563 The distribution is uniform on I[k] and
564
565 P(min + k*inc < x < min + (k+1)*inc) = h[k]/sample_size.
566
567 Furthermore, there are point masses at min and max with probability
568
569 P(x = min) = low/sample_size
570 and
571 P(x = max) = high/sample_size.
572
573 If min = max, then there is a single point mass at this point.
574 */
575 static PRECISION Percentile(PRECISION percentile, int low, int *h, PRECISION min, PRECISION max, int intervals, int sample_size)
576 {
577 int i;
578 percentile=percentile*sample_size - low;
579 if (percentile <= 0) return min;
580 for (i=0; i < intervals; i++)
581 if (h[i] && (percentile-=h[i]) <= 0)
582 return min + ((PRECISION)(i+1) + percentile/(PRECISION)h[i])*(max - min)/(PRECISION)intervals;
583 return max;
584 }
585
586 /*
587 Returns the probability that an observation is less than or equal to
588 level.
589
590 Assumes
591 Both intervals and sample_size are poitive and low and h[i] are
592 non-negative. Also, if
593
594 high = sample_size - (low + h[0] + ... + h[intervals - 1]),
595
596 then high is non-negative.
597
598 If min < max, let inc=(max - min)/intervals and define
599
600 I[k]=(min + k*inc, min + (k+1)*inc),
601
602 The distribution is uniform on I[k] and
603
604 P(min + k*inc < x < min + (k+1)*inc) = h[k]/sample_size.
605
606 Furthermore, there are point masses at min and max with probability
607
608 P(x = min) = low/sample_size
609 and
610 P(x = max) = high/sample_size.
611
612 If min = max, then there is a single point mass at this point
613 */
614 static PRECISION Cumulative(PRECISION level, int low, int *h, PRECISION min, PRECISION max, int intervals, int sample_size)
615 {
616 PRECISION inc=(max-min)/(PRECISION)intervals;
617 int i, count;
618
619 if (level < min) return 0.0;
620 if (level >= max) return 1.0;
621
622 for (count=low, i=0; i < intervals; count+=h[i++])
623 if ((min+=inc) >= level)
624 return ((PRECISION)count + (PRECISION)h[i]*(level - min + inc)/inc)/(PRECISION)sample_size;
625 return 1.0;
626 }
627
628 /*
629 Returns a histogram over the interval I=[min_out,max_out]. The matrix returned
630 has bins rows and 2 columns. If inc=(max_out - min_out)/bins, then the first
631 element of the ith row is
632
633 min + (i + 0.5)*inc,
634
635 which is the mid-point of the ith interval. The second element is
636
637 P(min + i*inc < x <= min + (i + 1)*inc)/inc,
638
639 which is the average density over the ith interval.
640
641 Assumes
642 Both intervals and sample_size are poitive and low and h[i] are
643 non-negative. Also if
644
645 high = sample_size - (low + h[0] + ... + h[intervals - 1]),
646
647 then high is non-negative.
648
649 If min < max, let inc=(max - min)/intervals and define
650
651 I[k]=(min + k*inc, min + (k+1)*inc),
652
653 The distribution is uniform on I[k] and
654
655 P(min + k*inc < x < min + (k+1)*inc) = h[k]/sample_size.
656
657 Furthermore, there are point masses at min and max with probability
658
659 P(x = min) = low/sample_size
660 and
661 P(x = max) = high/sample_size.
662
663 If min = max, then there is a single point mass at this point.
664 */
665 static TMatrix MakeHistogram(int low, int *h, PRECISION min, PRECISION max,
666 int intervals, int sample_size, PRECISION min_out, PRECISION max_out, int bins)
667 {
668 int i;
669 PRECISION inc, x, cdf_lower, cdf_upper;
670 TMatrix X;
671
672 inc=(max_out-min_out)/(PRECISION)bins;
673
674 if (inc > 0)
675 {
676 X=CreateMatrix(bins,2);
677 x=min_out+inc;
678
679 cdf_lower=Cumulative(min_out,low,h,min,max,intervals,sample_size);
680
681 for (i=0; i < bins; i++)
682 {
683 cdf_upper=Cumulative(x,low,h,min,max,intervals,sample_size);
684
685 ElementM(X,i,0)=x - 0.5*inc;
686 ElementM(X,i,1)=(cdf_upper-cdf_lower)/inc;
687
688 cdf_lower=cdf_upper;
689 x+=inc;
690 }
691 }
692 else
693 return (TMatrix)NULL;
694
695 return X;
696 }
697
698 /*
699 Automatically chooses lenth of interval over which to produce histogram and
700 then calls MakeHistogram().
701 */
702 static TMatrix MakeHistogramAuto(int low, int *h, int high, PRECISION min, PRECISION max, int intervals, int sample_size, int bins)
703 {
704 PRECISION inc=(max-min)/intervals, max_out, min_out;
705 int lo, hi;
706
707 if ((low == sample_size) || (high == sample_size) || (inc <= 0))
708 {
709 min_out=min-0.5;
710 max_out=min+0.5;
711 }
712 else
713 {
714 if (low > 0)
715 lo=-1;
716 else
717 for (lo=0; (lo < intervals) && !h[lo]; lo++);
718
719 if (high > 0)
720 hi=intervals;
721 else
722 for (hi=intervals-1;(hi >= 0) && !h[hi]; hi--);
723
724 if (lo >= 0)
725 {
726 min_out=min+lo*inc;
727 if (hi < intervals)
728 max_out=min+(hi+1)*inc;
729 else
730 {
731 if (bins == 1)
732 max_out=(1.0+SQRT_MACHINE_EPSILON)*max;
733 else
734 max_out=max + (1.0-SQRT_MACHINE_EPSILON)*(max - min_out)/(PRECISION)(bins-1);
735 }
736 }
737 else
738 if (hi < intervals)
739 {
740 max_out=min+(hi+1)*inc;
741 if (bins == 1)
742 min_out=(1.0-SQRT_MACHINE_EPSILON)*min;
743 else
744 min_out=min - (1.0-SQRT_MACHINE_EPSILON)*(max_out - min)/(PRECISION)(bins-1);
745 }
746 else
747 if (bins <= 2)
748 {
749 min_out=(1.0-SQRT_MACHINE_EPSILON)*min;
750 max_out=(1.0+SQRT_MACHINE_EPSILON)*max;
751 }
752 else
753 {
754 inc=(1.0-SQRT_MACHINE_EPSILON)*(max - min)/(PRECISION)(bins-2);
755 min_out=min - inc;
756 max_out=max + inc;
757 }
758 }
759
760 return MakeHistogram(low,h,min,max,intervals,sample_size,min_out,max_out,bins);
761 }
762
763