SDDSlib
Loading...
Searching...
No Matches
makeHistogram.c File Reference

Compiles histograms from data points. More...

#include "mdb.h"

Go to the source code of this file.

Functions

long make_histogram (double *hist, long n_bins, double lo, double hi, double *data, int64_t n_pts, long new_start)
 Compiles a histogram from data points.
 
long make_histogram_weighted (double *hist, long n_bins, double lo, double hi, double *data, long n_pts, long new_start, double *weight)
 Compiles a weighted histogram from data points.
 
long computeMode (double *result, double *data, long pts, double binSize, long bins)
 Computes the mode of a dataset using histogram binning.
 

Detailed Description

Compiles histograms from data points.

This file contains functions to compile histograms from data points. It provides both standard and weighted histogram functions, as well as a function to compute the mode of a dataset.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file makeHistogram.c.

Function Documentation

◆ computeMode()

long computeMode ( double * result,
double * data,
long pts,
double binSize,
long bins )

Computes the mode of a dataset using histogram binning.

Parameters
resultPointer to store the computed mode value.
dataPointer to the data array.
ptsNumber of data points.
binSizeSize of each histogram bin. If greater than 0, determines the bin size; otherwise, the number of bins is used.
binsNumber of bins in the histogram.
Returns
1 on success,
-1 if invalid binSize and bins parameters,
-2 if pts <= 0,
-3 if data is NULL,
-4 if result is NULL.

Definition at line 115 of file makeHistogram.c.

115 {
116 double min, max;
117 int64_t imin, imax;
118 double *histogram;
119
120 if ((binSize <= 0 && bins <= 2) || (binSize > 0 && bins > 2))
121 return -1;
122 if (pts <= 0)
123 return -2;
124 if (!data)
125 return -3;
126 if (!result)
127 return -4;
128 if (pts == 1) {
129 *result = data[0];
130 return 1;
131 }
132 find_min_max(&min, &max, data, pts);
133 /* add buffer bins and compute bin size or number of bins */
134 if (binSize > 0) {
135 max += binSize;
136 min -= binSize;
137 bins = (max - min) / binSize + 0.5;
138 } else {
139 binSize = (max - min) / bins;
140 max += binSize;
141 min -= binSize;
142 bins += 2;
143 binSize = (max - min) / bins;
144 }
145 if (!(histogram = malloc(sizeof(*histogram) * bins)))
146 bomb("memory allocation failure (computeMode)", NULL);
147 make_histogram(histogram, bins, min, max, data, pts, 1);
148 index_min_max(&imin, &imax, histogram, bins);
149 free(histogram);
150 *result = (imax + 0.5) * binSize + min;
151 return 1;
152}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
int index_min_max(int64_t *imin, int64_t *imax, double *list, int64_t n)
Finds the indices of the minimum and maximum values in a list of doubles.
Definition findMinMax.c:116
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
Definition findMinMax.c:33
long make_histogram(double *hist, long n_bins, double lo, double hi, double *data, int64_t n_pts, long new_start)
Compiles a histogram from data points.

◆ make_histogram()

long make_histogram ( double * hist,
long n_bins,
double lo,
double hi,
double * data,
int64_t n_pts,
long new_start )

Compiles a histogram from data points.

Parameters
histPointer to the histogram array to be filled.
n_binsNumber of bins in the histogram.
loLower bound of the histogram range.
hiUpper bound of the histogram range.
dataPointer to the data array.
n_ptsNumber of data points.
new_startFlag indicating whether to initialize the histogram (1 to initialize, 0 to accumulate).
Returns
Returns the total number of points binned.

Definition at line 33 of file makeHistogram.c.

35 {
36 static long bin;
37 static int64_t i;
38 static double bin_size, dbin;
39
40 if (new_start) {
41 bin_size = (hi - lo) / n_bins;
42 for (i = 0; i < n_bins; i++)
43 hist[i] = 0;
44 }
45
46 for (i = 0; i < n_pts; i++) {
47 bin = (dbin = (data[i] - lo) / bin_size);
48 if (dbin < 0)
49 continue;
50 if (bin < 0 || bin >= n_bins)
51 continue;
52 hist[bin] += 1;
53 }
54
55 for (i = bin = 0; i < n_bins; i++)
56 bin += hist[i];
57
58 return (bin);
59}

◆ make_histogram_weighted()

long make_histogram_weighted ( double * hist,
long n_bins,
double lo,
double hi,
double * data,
long n_pts,
long new_start,
double * weight )

Compiles a weighted histogram from data points.

Parameters
histPointer to the histogram array to be filled.
n_binsNumber of bins in the histogram.
loLower bound of the histogram range.
hiUpper bound of the histogram range.
dataPointer to the data array.
n_ptsNumber of data points.
new_startFlag indicating whether to initialize the histogram (1 to initialize, 0 to accumulate).
weightPointer to the weights array corresponding to each data point.
Returns
Returns the total number of points binned.

Definition at line 74 of file makeHistogram.c.

76 {
77 static long bin, i, count;
78 static double bin_size, dbin;
79
80 if (new_start) {
81 count = 0;
82 bin_size = (hi - lo) / n_bins;
83 for (i = 0; i < n_bins; i++)
84 hist[i] = 0;
85 }
86
87 for (i = 0; i < n_pts; i++) {
88 bin = (dbin = (data[i] - lo) / bin_size);
89 if (dbin < 0)
90 continue;
91 if (bin < 0 || bin >= n_bins)
92 continue;
93 hist[bin] += weight[i];
94 count++;
95 }
96
97 return (count);
98}