6#ifndef THEORETICA_HISTOGRAM_H
7#define THEORETICA_HISTOGRAM_H
9#ifndef THEORETICA_NO_PRINT
35 std::vector<unsigned int> bin_counts {};
72 : N(0), value_max(-
inf()), value_min(
inf()), run_average(0), run_tss(0) {
74 bin_counts.resize(bin_count);
75 this->range_max = range_max;
76 this->range_min = range_min;
87 template<
typename Dataset, enable_vector<Dataset> = true>
88 histogram(
const Dataset& data,
unsigned int bin_count = 0) {
92 value_max = range_max;
93 value_min = range_min;
106 for (
size_t i = 0; i < N; ++i)
107 bin_counts[
index(data[i])]++;
117 if(x < range_min || x > range_max)
121 const real tmp = run_average;
122 run_average = tmp + (x - tmp) / (N + 1);
123 run_tss += (x - tmp) * (x - run_average);
125 value_max = value_max < x ? x : value_max;
126 value_min = value_min > x ? x : value_min;
128 bin_counts[
index(x)]++;
144 return bin_counts.size() - 1;
147 (x - range_min) / (range_max - range_min)
159 return vec2({range_min, range_max});
181 inline std::vector<unsigned int>
bins()
const {
234 const std::vector<unsigned int>& bin_counts,
238 this->bin_counts = bin_counts;
239 this->range_min =
range[0];
240 this->range_max =
range[1];
242 this->run_average = run_average;
243 this->run_tss = run_tss;
244 this->value_min = value_min;
245 this->value_max = value_max;
259 if (x < range_min || x > range_max)
262 return bin_counts[
index(x)];
271 return bin_counts[i];
278#ifndef THEORETICA_NO_PRINT
289 const std::string& separator =
" ",
290 bool normalized =
true,
291 bool lower_extreme =
false)
const {
296 std::stringstream res;
297 const real width =
abs(range_max - range_min) / bin_counts.size();
303 for (
size_t i = 0; i < bin_counts.size(); ++i) {
305 res << (range_min + (i + mult) * width) << separator;
308 res << (bin_counts[i] / (
real) N) << std::endl;
310 res << bin_counts[i] << std::endl;
318 inline operator std::string() {
326 std::ostream& out,
const histogram& obj) {
Histogram class with running statistics, can be constructed from the parameters of the bins or from a...
Definition histogram.h:28
real range_lower() const
Return the lower extreme of the histogram range.
Definition histogram.h:187
void rebuild(const std::vector< unsigned int > &bin_counts, const vec2 &range, size_t N, real run_average, real run_tss, real value_min, real value_max)
Rebuild the histogram from its parameters, including the bin counts and the running statistics (used ...
Definition histogram.h:233
histogram(unsigned int bin_count, real range_min, real range_max)
Construct the histogram from the number of bins and the range.
Definition histogram.h:71
real min() const
Get the smallest data point of the histogram.
Definition histogram.h:209
unsigned int index(real x) const
Find the bin index corresponding to a given data point.
Definition histogram.h:141
friend std::ostream & operator<<(std::ostream &out, const histogram &obj)
Stream the histogram in string representation to an output stream (std::ostream).
Definition histogram.h:325
real max() const
Get the biggest data point of the histogram.
Definition histogram.h:201
histogram()=default
Default constructor, creates an empty histogram with no bins and NaN range.
real operator()(real x)
Evaluate the histogram like a step function which is zero outside the range of the histogram.
Definition histogram.h:257
vec2 range() const
Get the histogram range as a vector of two elements, containing the lower and upper extremes.
Definition histogram.h:158
real tss() const
Get the total sum of squares (TSS) computed using Welford's one-pass method.
Definition histogram.h:226
real range_upper() const
Return the upper extreme of the histogram range.
Definition histogram.h:193
unsigned int operator[](unsigned int i) const
Get the number of elements in the i-th bin.
Definition histogram.h:270
std::string to_string(const std::string &separator=" ", bool normalized=true, bool lower_extreme=false) const
Convert the histogram to string representation.
Definition histogram.h:288
unsigned int number() const
Get the number of data points inside the histogram.
Definition histogram.h:170
histogram(const Dataset &data, unsigned int bin_count=0)
Construct the histogram from a set of data points, with the given number of bins.
Definition histogram.h:88
void insert(real x)
Insert a new data point inside the histogram, updating the running statistics and the corresponding b...
Definition histogram.h:115
real mean() const
Get the mean value of the histogram data.
Definition histogram.h:217
std::vector< unsigned int > bins() const
Get a vector containing the bin counts of each bin.
Definition histogram.h:181
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Operations on datasets and generic ordered sets of numbers.
real stdev(const histogram &h)
Compute the standard deviation of the values of a histogram.
Definition histogram.h:366
real total_sum_squares(const Dataset &X)
Compute the total sum of squares (TSS) of a given dataset as using Welford's one-pass method.
Definition statistics.h:116
real variance(const histogram &h)
Compute the variance of the values of a histogram.
Definition histogram.h:354
real mean(const histogram &h)
Compute the mean of the values of a histogram.
Definition histogram.h:342
real tss(const histogram &h)
Compute the total sum of squares of the values of the histogram.
Definition histogram.h:348
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:347
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
double real
A real number, defined as a floating point type.
Definition constants.h:207
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:326
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ DivByZero
Division by zero.
Definition error.h:27
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
vec< real, 2 > vec2
A 2-dimensional vector with real elements.
Definition algebra_types.h:39
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:96
TH_CONSTEXPR int floor(real x)
Compute the floor of x, as the maximum integer number that is smaller than x.
Definition real_analysis.h:271