IT++ 4.3.1
histogram.h
Go to the documentation of this file.
1
28
29#ifndef HISTOGRAM_H
30#define HISTOGRAM_H
31
32#include <itpp/base/mat.h>
33
34
35namespace itpp
36{
37
40
74template<typename Num_T>
76{
77public:
80 Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100);
83
85 void setup(Num_T from, Num_T to, int n_bins);
86
88 void update(Num_T value);
90 void update(Vec<Num_T> values);
92 void update(Mat<Num_T> values);
93
95 void reset() { trials_cnt = 0; bins.zeros(); };
97 int get_bin(int ix) const { return bins(ix); };
99 ivec get_bins() const { return bins; };
101 Vec<Num_T> get_bin_centers() const { return center_vals; };
103 Num_T get_bin_center(int ix) const { return center_vals(ix); };
105 Vec<Num_T> get_bin_lefts() const { return lo_vals; };
107 Num_T get_bin_left(int ix) const { return lo_vals(ix); };
109 Vec<Num_T> get_bin_rights() const { return hi_vals; };
111 Num_T get_bin_right(int ix) const { return hi_vals(ix); };
112
114 vec get_pdf() const;
116 vec get_cdf() const;
117
119 int bins_num() const { return num_bins; };
121 int trials_num() const {return trials_cnt;};
122
123private:
125 int num_bins;
127 Num_T step;
129 Vec<Num_T> lo_vals;
131 Vec<Num_T> hi_vals;
133 Vec<Num_T> center_vals;
135 ivec bins;
137 int trials_cnt;
138};
139
140template<class Num_T>
141inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins)
142
143{
144 setup(from, to, n_bins);
145}
146
147template<class Num_T>
148inline void Histogram<Num_T>::setup(Num_T from, Num_T to, int n_bins)
149{
150 num_bins = n_bins;
151 lo_vals.set_size(n_bins);
152 hi_vals.set_size(n_bins);
153 center_vals.set_size(n_bins);
154 bins.set_size(n_bins);
155 trials_cnt = 0;
156 step = (to - from) / (num_bins - 1);
157 center_vals = linspace(from, to, num_bins);
158 lo_vals = center_vals - step / 2;
159 hi_vals = center_vals + step / 2;
160 reset();
161}
162
163template<class Num_T>
164inline void Histogram<Num_T>::update(Num_T value)
165{
166 // search for the corresponding bin using dichotomy approach
167 int start = 0;
168 int end = num_bins - 1;
169 int test = (start + end) / 2;
170
171 while (start < end) {
172 if (value < lo_vals(test))
173 end = test - 1;
174 else if (value >= hi_vals(test))
175 start = test + 1;
176 else
177 break;
178 test = (start + end) / 2;
179 };
180
181 bins(test)++;
182 trials_cnt++;
183}
184
185template<class Num_T>
187{
188 for (int i = 0; i < values.length(); i++)
189 update(values(i));
190}
191
192template<class Num_T>
194{
195 for (int i = 0; i < values.rows(); i++)
196 for (int j = 0; j < values.cols(); j++)
197 update(values(i, j));
198}
199
200template<class Num_T>
202{
203 vec pdf(num_bins);
204 for (int j = 0; j < num_bins; j++)
205 pdf(j) = static_cast<double>(bins(j)) / trials_cnt;
206 return pdf;
207}
208
209template<class Num_T>
211{
212 ivec tmp = cumsum(bins);
213 vec cdf(num_bins);
214 for (int j = 0; j < num_bins; j++)
215 cdf(j) = static_cast<double>(tmp(j)) / trials_cnt;
216 return cdf;
217}
218
220
221} // namespace itpp
222
223#endif // #ifndef HISTOGRAM_H
int bins_num() const
Current number of bins.
Definition histogram.h:119
void reset()
Bins reset, so accumulation can be restarted.
Definition histogram.h:95
~Histogram()
Default destructor.
Definition histogram.h:82
Vec< Num_T > get_bin_rights() const
Access to right boundary of bin intervals (all bins)
Definition histogram.h:109
Vec< Num_T > get_bin_centers() const
Access to bin center values (all bins)
Definition histogram.h:101
Num_T get_bin_center(int ix) const
Access to bin center (single bin)
Definition histogram.h:103
Num_T get_bin_right(int ix) const
Access to right boundary of single bin.
Definition histogram.h:111
Vec< Num_T > get_bin_lefts() const
Access to left boundary of bin intervals (all bins)
Definition histogram.h:105
ivec get_bins() const
Access to histogram as a vector.
Definition histogram.h:99
int trials_num() const
Current trials counter.
Definition histogram.h:121
Num_T get_bin_left(int ix) const
Access to left boundary of single bin.
Definition histogram.h:107
int get_bin(int ix) const
Access to single bin counter.
Definition histogram.h:97
Matrix Class (Templated)
Definition mat.h:202
int rows() const
The number of rows.
Definition mat.h:237
int cols() const
The number of columns.
Definition mat.h:235
Vector Class (Templated)
Definition vec.h:245
int length() const
The size of the vector.
Definition vec.h:269
T to(double x)
Convert double to T.
vec get_cdf() const
Experimental Cumulative Density Function (CDF) computation.
Definition histogram.h:210
vec get_pdf() const
Experimental Probability Density Function (PDF) computation.
Definition histogram.h:201
void setup(Num_T from, Num_T to, int n_bins)
Histogram setup.
Definition histogram.h:148
Histogram(Num_T from=Num_T(0), Num_T to=Num_T(99), int n_bins=100)
Definition histogram.h:141
void update(Num_T value)
Histogram update.
Definition histogram.h:164
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition matfunc.h:157
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition specmat.cpp:106
Matrix Class Definitions.
itpp namespace
Definition itmex.h:37