histogram.h
Go to the documentation of this file.
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include <itpp/base/mat.h>
namespace itpp
{
template<typename Num_T>
class Histogram
{
public:
Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100);
~Histogram() {};
void setup(Num_T from, Num_T to, int n_bins);
void update(Num_T value);
void update(Vec<Num_T> values);
void update(Mat<Num_T> values);
void reset() { trials_cnt = 0; bins.zeros(); };
int get_bin(int ix) const { return bins(ix); };
ivec get_bins() const { return bins; };
Vec<Num_T> get_bin_centers() const { return center_vals; };
Num_T get_bin_center(int ix) const { return center_vals(ix); };
Vec<Num_T> get_bin_lefts() const { return lo_vals; };
Num_T get_bin_left(int ix) const { return lo_vals(ix); };
Vec<Num_T> get_bin_rights() const { return hi_vals; };
Num_T get_bin_right(int ix) const { return hi_vals(ix); };
vec get_pdf() const;
vec get_cdf() const;
int bins_num() const { return num_bins; };
int trials_num() const {return trials_cnt;};
private:
int num_bins;
Num_T step;
Vec<Num_T> lo_vals;
Vec<Num_T> hi_vals;
Vec<Num_T> center_vals;
ivec bins;
int trials_cnt;
};
template<class Num_T>
inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins)
{
setup(from, to, n_bins);
}
template<class Num_T>
inline void Histogram<Num_T>::setup(Num_T from, Num_T to, int n_bins)
{
num_bins = n_bins;
lo_vals.set_size(n_bins);
hi_vals.set_size(n_bins);
center_vals.set_size(n_bins);
bins.set_size(n_bins);
trials_cnt = 0;
step = (to - from) / (num_bins - 1);
center_vals = linspace(from, to, num_bins);
lo_vals = center_vals - step / 2;
hi_vals = center_vals + step / 2;
reset();
}
template<class Num_T>
inline void Histogram<Num_T>::update(Num_T value)
{
// search for the corresponding bin using dichotomy approach
int start = 0;
int end = num_bins - 1;
int test = (start + end) / 2;
while (start < end) {
if (value < lo_vals(test))
end = test - 1;
else if (value >= hi_vals(test))
start = test + 1;
else
break;
test = (start + end) / 2;
};
bins(test)++;
trials_cnt++;
}
template<class Num_T>
inline void Histogram<Num_T>::update(Vec<Num_T> values)
{
for (int i = 0; i < values.length(); i++)
update(values(i));
}
template<class Num_T>
inline void Histogram<Num_T>::update(Mat<Num_T> values)
{
for (int i = 0; i < values.rows(); i++)
for (int j = 0; j < values.cols(); j++)
update(values(i, j));
}
template<class Num_T>
inline vec Histogram<Num_T>::get_pdf() const
{
vec pdf(num_bins);
for (int j = 0; j < num_bins; j++)
pdf(j) = static_cast<double>(bins(j)) / trials_cnt;
return pdf;
}
template<class Num_T>
inline vec Histogram<Num_T>::get_cdf() const
{
ivec tmp = cumsum(bins);
vec cdf(num_bins);
for (int j = 0; j < num_bins; j++)
cdf(j) = static_cast<double>(tmp(j)) / trials_cnt;
return cdf;
}
} // namespace itpp
#endif // #ifndef HISTOGRAM_H
itpp::Vec::length
int length() const
The size of the vector.
Definition: vec.h:269
itpp::Histogram::get_bin_centers
Vec< Num_T > get_bin_centers() const
Access to bin center values (all bins)
Definition: histogram.h:101
itpp::Histogram::get_bin
int get_bin(int ix) const
Access to single bin counter.
Definition: histogram.h:97
itpp::Histogram::Histogram
Histogram(Num_T from=Num_T(0), Num_T to=Num_T(99), int n_bins=100)
Definition: histogram.h:141
itpp::Histogram::bins_num
int bins_num() const
Current number of bins.
Definition: histogram.h:119
itpp::Histogram::update
void update(Num_T value)
Histogram update.
Definition: histogram.h:164
itpp::Histogram::trials_num
int trials_num() const
Current trials counter.
Definition: histogram.h:121
itpp
itpp namespace
Definition: itmex.h:36
itpp::Histogram
Histogram computation class.
Definition: histogram.h:75
mat.h
Matrix Class Definitions.
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Histogram::get_bin_left
Num_T get_bin_left(int ix) const
Access to left boundary of single bin.
Definition: histogram.h:107
itpp::Histogram::get_bin_center
Num_T get_bin_center(int ix) const
Access to bin center (single bin)
Definition: histogram.h:103
itpp::Histogram::get_bin_right
Num_T get_bin_right(int ix) const
Access to right boundary of single bin.
Definition: histogram.h:111
itpp::Histogram::get_pdf
vec get_pdf() const
Experimental Probability Density Function (PDF) computation.
Definition: histogram.h:201
itpp::linspace
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition: specmat.cpp:106
itpp::Mat::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Histogram::get_bins
ivec get_bins() const
Access to histogram as a vector.
Definition: histogram.h:99
itpp::to
T to(double x)
Convert double to T.
Definition: fix_functions.h:365
itpp::Mat
Matrix Class (Templated)
Definition: factory.h:41
itpp::cumsum
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition: matfunc.h:157
itpp::Histogram::~Histogram
~Histogram()
Default destructor.
Definition: histogram.h:82
itpp::Histogram::setup
void setup(Num_T from, Num_T to, int n_bins)
Histogram setup.
Definition: histogram.h:148
itpp::Histogram::get_bin_rights
Vec< Num_T > get_bin_rights() const
Access to right boundary of bin intervals (all bins)
Definition: histogram.h:109
itpp::Vec
Vector Class (Templated)
Definition: factory.h:42
itpp::Histogram::get_bin_lefts
Vec< Num_T > get_bin_lefts() const
Access to left boundary of bin intervals (all bins)
Definition: histogram.h:105
itpp::Histogram::reset
void reset()
Bins reset, so accumulation can be restarted.
Definition: histogram.h:95
itpp::Histogram::get_cdf
vec get_cdf() const
Experimental Cumulative Density Function (CDF) computation.
Definition: histogram.h:210

Generated on Mon Mar 23 2020 06:18:03 for IT++ by Doxygen 1.8.17