IT++ Logo
histogram.h
Go to the documentation of this file.
1 
29 #ifndef HISTOGRAM_H
30 #define HISTOGRAM_H
31 
32 #include <itpp/base/mat.h>
33 
34 
35 namespace itpp
36 {
37 
40 
74 template<typename Num_T>
75 class Histogram
76 {
77 public:
80  Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100);
82  ~Histogram() {};
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 
123 private:
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 
140 template<class Num_T>
141 inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins)
142 
143 {
144  setup(from, to, n_bins);
145 }
146 
147 template<class Num_T>
148 inline 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 
163 template<class Num_T>
164 inline 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 
185 template<class Num_T>
187 {
188  for (int i = 0; i < values.length(); i++)
189  update(values(i));
190 }
191 
192 template<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 
200 template<class Num_T>
201 inline vec Histogram<Num_T>::get_pdf() const
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 
209 template<class Num_T>
210 inline vec Histogram<Num_T>::get_cdf() const
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 trials_num() const
Current trials counter.
Definition: histogram.h:121
Num_T get_bin_right(int ix) const
Access to right boundary of single bin.
Definition: histogram.h:111
Num_T get_bin_center(int ix) const
Access to bin center (single bin)
Definition: histogram.h:103
Matrix Class (Templated)
Definition: factory.h:41
Histogram computation class.
Definition: histogram.h:75
Vec< Num_T > get_bin_lefts() const
Access to left boundary of bin intervals (all bins)
Definition: histogram.h:105
vec get_cdf() const
Experimental Cumulative Density Function (CDF) computation.
Definition: histogram.h:210
ivec get_bins() const
Access to histogram as a vector.
Definition: histogram.h:99
void update(Num_T value)
Histogram update.
Definition: histogram.h:164
Vec< Num_T > get_bin_centers() const
Access to bin center values (all bins)
Definition: histogram.h:101
int get_bin(int ix) const
Access to single bin counter.
Definition: histogram.h:97
Vec< Num_T > get_bin_rights() const
Access to right boundary of bin intervals (all bins)
Definition: histogram.h:109
int cols() const
The number of columns.
Definition: mat.h:235
Matrix Class Definitions.
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition: matfunc.h:157
itpp namespace
Definition: itmex.h:36
int length() const
The size of the vector.
Definition: vec.h:269
int bins_num() const
Current number of bins.
Definition: histogram.h:119
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition: specmat.cpp:106
Num_T get_bin_left(int ix) const
Access to left boundary of single bin.
Definition: histogram.h:107
vec get_pdf() const
Experimental Probability Density Function (PDF) computation.
Definition: histogram.h:201
int rows() const
The number of rows.
Definition: mat.h:237
Vector Class (Templated)
Definition: factory.h:42
Histogram(Num_T from=Num_T(0), Num_T to=Num_T(99), int n_bins=100)
Definition: histogram.h:141
T to(double x)
Convert double to T.
void reset()
Bins reset, so accumulation can be restarted.
Definition: histogram.h:95
void setup(Num_T from, Num_T to, int n_bins)
Histogram setup.
Definition: histogram.h:148
~Histogram()
Default destructor.
Definition: histogram.h:82

Generated on Thu Jun 21 2018 16:06:18 for IT++ by Doxygen 1.8.13