1 #ifndef jb_histogram_hpp 2 #define jb_histogram_hpp 10 #include <type_traits> 45 template <
typename binning_strategy_t,
typename counter_type_t =
unsigned int>
75 check_constraints checker;
100 throw std::invalid_argument(
"Cannot estimate mean on an empty histogram");
110 sample_type a =
binning_.histogram_min();
111 for (std::size_t i = 0; i !=
bins_.size(); ++i) {
112 sample_type b =
binning_.bin2sample(i + 1);
138 throw std::invalid_argument(
139 "Cannot estimate quantile for empty histogram");
141 if (q < 0 or q > 1.0) {
142 throw std::invalid_argument(
"Quantile value outside 0 <= q <= 1 range");
144 std::uint64_t cum_samples = 0;
146 if (bin_samples and q <=
double(cum_samples + bin_samples) /
nsamples()) {
147 double s = double(bin_samples) /
nsamples();
148 double y_a = double(cum_samples) /
nsamples();
152 for (std::size_t i = 0; i !=
bins_.size(); ++i) {
153 cum_samples += bin_samples;
154 bin_samples =
bins_[i];
155 if (bin_samples and q <=
double(cum_samples + bin_samples) /
nsamples()) {
156 double s = double(bin_samples) /
nsamples();
157 double y_a = double(cum_samples) /
nsamples();
162 cum_samples += bin_samples;
164 if (bin_samples and q <
double(cum_samples + bin_samples) /
nsamples()) {
165 double s = double(bin_samples) /
nsamples();
166 double y_a = double(cum_samples) /
nsamples();
216 }
else if (t <
binning_.histogram_min()) {
226 *
this = std::move(fresh);
241 sample_type
midpoint(sample_type
const& a, sample_type
const& b)
const {
260 template <
typename binning_strategy,
typename counter_type>
267 std::is_integral<counter_type>::value,
268 "The counter_type must be an integral type");
272 decltype(histogram_min_return_type()), sample_type>::value,
273 "The binning_strategy must provide a min() function, " 274 "and it must return a type compatible with sample_type.");
277 decltype(histogram_max_return_type()), sample_type>::value,
278 "The binning_strategy must provide a max() function, " 279 "and it must return a type compatible with sample_type.");
283 decltype(theoretical_min_return_type()), sample_type>::value,
284 "The binning_strategy must provide a theoretical_min() function, " 285 "and it must return a type compatible with sample_type.");
288 decltype(theoretical_max_return_type()), sample_type>::value,
289 "The binning_strategy must provide a theoretical_max() function, " 290 "and it must return a type compatible with sample_type.");
294 decltype(interpolate_return_type()), sample_type>::value,
295 "The binning_strategy must provide a interpolate() function, " 296 "and it must return a type compatible with sample_type.");
300 decltype(sample2bin_return_type()), std::size_t>::value,
301 "The binning_strategy must provide a sample2bin() function, " 302 "and it must return a type compatible with std::size_t.");
305 decltype(bin2sample_return_type()), sample_type>::value,
306 "The binning_strategy must provide a bin2sample() function, " 307 "and it must return a type compatible with sample_type.");
310 1 ==
sizeof(decltype(has_less_than(std::declval<sample_type>()))),
311 "The sample_type must have a < operator.");
314 decltype(has_less_than_or_equal(std::declval<sample_type>()))),
315 "The sample_type must have a <= operator.");
318 auto histogram_min_return_type()
319 -> decltype(std::declval<const binning_strategy>().histogram_min());
320 auto histogram_max_return_type()
321 -> decltype(std::declval<const binning_strategy>().histogram_max());
323 auto theoretical_min_return_type()
324 -> decltype(std::declval<const binning_strategy>().theoretical_min());
325 auto theoretical_max_return_type()
326 -> decltype(std::declval<const binning_strategy>().theoretical_max());
328 auto sample2bin_return_type()
329 -> decltype(std::declval<const binning_strategy>().sample2bin(
330 std::declval<const sample_type>()));
331 auto bin2sample_return_type() -> decltype(
332 std::declval<const binning_strategy>().bin2sample(std::size_t(0)));
334 auto interpolate_return_type()
335 -> decltype(std::declval<const binning_strategy>().interpolate(
336 std::declval<const sample_type>(), std::declval<const sample_type>(),
337 double(1.0),
double(1.0),
double(0.5)));
346 error has_less_than(...);
347 auto has_less_than(sample_type
const& t)
348 -> decltype(static_cast<void>(t < t),
char(0));
349 error has_less_than_or_equal(...);
350 auto has_less_than_or_equal(sample_type
const& t)
351 -> decltype(static_cast<void>(t <= t),
char(0));
356 #endif // jb_histogram_hpp counter_type_t counter_type
void sample(sample_type const &t)
Record a new sample.
std::uint64_t underflow_count_
std::vector< counter_type > counters
The type used to store the bins.
binning_strategy::sample_type sample_type
void reset()
Reset all counters.
binning_strategy binning_
sample_type observed_max_
std::size_t nbins() const
Compute the maximum number of bins that might be needed.
A histogram class with controllable binning and range strategy.
A simple class to capture summary information about a histogram.
histogram< binning_strategy > histo
std::uint64_t overflow_count_
sample_type observed_min_
sample_type estimated_quantile(double q) const
Estimate a quantile of the sample distribution.
sample_type observed_max() const
Return the largest sample value observed to this point.
std::uint64_t underflow_count() const
Return the number of samples smaller than the histogram range.
binning_strategy_t binning_strategy
histo::sample_type sample_type
sample_type midpoint(sample_type const &a, sample_type const &b) const
Estimate a midpoint.
histogram_summary summary() const
Return a simple summary.
An object to create a SFINAE condition.
sample_type observed_min() const
Return the smallest sample value observed to this point.
sample_type estimated_mean() const
Estimate the mean of the sample distribution.
Verify the constraints on the histogram template class template parameters, and generate better error...
std::uint64_t nsamples() const
Return the number of samples observed to this point.
void weighted_sample(sample_type const &t, counter_type weight)
Record a new sample.
std::uint64_t overflow_count() const
Return the number of samples larger than the histogram range.
histogram(binning_strategy const &mapping=binning_strategy())
Construct a histogram given a mapping strategy.
The top-level namespace for the JayBeams library.