IT++
4.3.1
Toggle main menu visibility
itpp
stat
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
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>
186
inline
void
Histogram<Num_T>::update
(
Vec<Num_T>
values)
187
{
188
for
(
int
i = 0; i < values.
length
(); i++)
189
update
(values(i));
190
}
191
192
template
<
class
Num_T>
193
inline
void
Histogram<Num_T>::update
(
Mat<Num_T>
values)
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
itpp::Histogram::bins_num
int bins_num() const
Current number of bins.
Definition
histogram.h:119
itpp::Histogram::reset
void reset()
Bins reset, so accumulation can be restarted.
Definition
histogram.h:95
itpp::Histogram::~Histogram
~Histogram()
Default destructor.
Definition
histogram.h:82
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::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_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_bin_lefts
Vec< Num_T > get_bin_lefts() const
Access to left boundary of bin intervals (all bins).
Definition
histogram.h:105
itpp::Histogram::get_bins
ivec get_bins() const
Access to histogram as a vector.
Definition
histogram.h:99
itpp::Histogram::trials_num
int trials_num() const
Current trials counter.
Definition
histogram.h:121
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
int get_bin(int ix) const
Access to single bin counter.
Definition
histogram.h:97
itpp::Mat
Matrix Class (Templated).
Definition
mat.h:202
itpp::Mat::rows
int rows() const
The number of rows.
Definition
mat.h:237
itpp::Mat::cols
int cols() const
The number of columns.
Definition
mat.h:235
itpp::Vec
Vector Class (Templated).
Definition
vec.h:245
itpp::Vec::length
int length() const
The size of the vector.
Definition
vec.h:269
itpp::to
T to(double x)
Convert double to T.
Definition
fix_functions.h:365
itpp::Histogram::get_cdf
vec get_cdf() const
Experimental Cumulative Density Function (CDF) computation.
Definition
histogram.h:210
itpp::Histogram::get_pdf
vec get_pdf() const
Experimental Probability Density Function (PDF) computation.
Definition
histogram.h:201
itpp::Histogram::setup
void setup(Num_T from, Num_T to, int n_bins)
Histogram setup.
Definition
histogram.h:148
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::update
void update(Num_T value)
Histogram update.
Definition
histogram.h:164
itpp::cumsum
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition
matfunc.h:157
itpp::linspace
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition
specmat.cpp:106
mat.h
Matrix Class Definitions.
itpp
itpp namespace
Definition
itmex.h:37
Generated by
1.17.0