22 #ifndef CRNSTATISTICSAMPLE_HEADER
23 #define CRNSTATISTICSAMPLE_HEADER
40 class UnivariateGaussianMixture;
41 class MultivariateGaussianMixture;
57 template<
typename T>
inline T
Max(
const std::vector<T> &v) {
return *std::max_element(v.begin(), v.end()); }
59 template<
typename T>
inline T
Max(
const std::vector<std::vector<T>> &m)
61 auto ma = m.front().front();
62 for (
const auto &row : m)
64 auto rmax = *std::max_element(row.begin(), row.end());
65 if (rmax > ma) ma = rmax;
70 template<
typename T>
inline T
Min(
const std::vector<T> &v) {
return *std::min_element(v.begin(), v.end()); }
72 template<
typename T>
inline T
Min(
const std::vector<std::vector<T>> &m)
74 auto mi = m.front().front();
75 for (
const auto &row : m)
77 auto rmin = *std::max_element(row.begin(), row.end());
78 if (rmin < mi) mi = rmin;
83 template<
typename T>
inline std::tuple<T, T>
MinMax(
const std::vector<T> &v)
85 auto res = std::minmax_element(v.begin(), v.end());
86 return std::make_tuple(*res.first, *res.second);
89 template<
typename T>
inline std::tuple<T, T>
MinMax(
const std::vector<std::vector<T>> &m)
91 auto mi = m.front().front();
92 auto ma = m.front().front();
93 for (
const auto &row : m)
95 auto rmM = std::minmax_element(row.begin(), row.end());
96 if (*rmM.first < mi) mi = *rmM.first;
97 if (*rmM.second > ma) ma = *rmM.second;
99 return std::make_tuple(mi, ma);
102 template<
typename T>
inline size_t Argmax(
const std::vector<T> &v) {
return std::max_element(v.begin(), v.end()) - v.begin(); }
104 template<
typename T>
inline size_t Argmin(
const std::vector<T> &v) {
return std::min_element(v.begin(), v.end()) - v.begin(); }
106 template<
typename T>
size_t ColumnArgmax(
const std::vector<std::vector<T>> &m,
size_t col)
108 auto ma = m.front().front();
109 auto index = size_t(0);
110 for (
size_t tmp = 1; tmp < m.size(); ++tmp)
111 if (m[tmp][col] > ma)
119 template<
typename T>
size_t ColumnArgmin(
const std::vector<std::vector<T>> &m,
size_t col)
121 auto mi = m.front().front();
122 auto index = size_t(0);
123 for (
size_t tmp = 1; tmp < m.size(); ++tmp)
124 if (m[tmp][col] < mi)
133 double Mean(
const std::vector<double> &v);
142 template<
typename ITER>
typename std::iterator_traits<ITER>::value_type
Mean(ITER be, ITER en)
144 using value_type =
typename std::iterator_traits<ITER>::value_type;
147 return value_type(cumul);
157 const auto s = double(std::distance(be, en));
158 auto m = std::accumulate(be, en, 0.0);
165 for (
auto it = be; it != en; ++it)
166 m +=
double(*it) / s;
172 std::vector<double>
MeanPattern(
const std::vector<std::vector<double>> &m);
181 template<
typename ITER> std::vector<double>
MeanPattern(ITER it_begin, ITER it_end)
184 auto dimension = it_begin->first.size();
185 auto pattern = std::vector<double>(dimension, 0.0);
187 for (
auto it = it_begin; it != it_end; ++it)
189 const auto& pat = it->first;
191 if (pat.size() == dimension)
193 const auto weight = double(it->second);
195 for (
size_t k = 0; k < dimension; ++k)
196 pattern[k] += weight * pat[k];
204 for (
size_t k = 0; k < dimension; ++k)
205 if (std::isinf(pattern[k]))
212 for (
size_t k = 0; k < dimension; ++k)
213 pattern[k] /= cardinal;
216 for (
size_t k = 0; k < dimension; ++k)
219 for (
auto it = it_begin; it != it_end; ++it)
220 if (it->first.size() == dimension)
222 const auto scale = double(it->second) / double(cardinal);
223 const auto& pt = it->first;
225 for (
size_t k = 0; k < dimension; ++k)
226 pattern[k] += pt[k] * scale;
236 double Variance(
const std::vector<double> &v);
238 std::vector<std::vector<double>>
MakeCovariance(
const std::vector<std::vector<double>> &m);
240 template<
typename ITER> std::vector<std::vector<double>>
MakeCovariance(ITER it_begin, ITER it_end)
243 const auto dim = it_begin->first.size();
244 auto cov = std::vector<std::vector<double>>(dim, std::vector<double>(dim));
246 for (
auto it = it_begin; it != it_end; ++it)
247 card +=
double(it->second);
249 for (
size_t i = 0; i < dim; ++i)
250 for (
auto j = i; j < dim; ++j)
252 for (
auto it = it_begin; it != it_end; ++it)
253 cov[i][j] += it->first[i] * it->first[j] *
double(it->second);
255 cov[i][j] /= double(card);
258 cov[j][i] = cov[i][j];
265 std::tuple<double, double, double>
MeanVarDev(
const std::vector<double> &v);
273 template<
typename ITER> std::tuple<double, double, double>
MeanVarDev(ITER it_begin, ITER it_end)
280 for (
auto it = it_begin; it != it_end; ++it)
282 auto val = it->first;
283 auto cnt = it->second;
286 m_2 +=
Sqr(val) * cnt;
300 for (
auto it = it_begin; it != it_end; ++it)
302 auto val = it->first;
303 auto cnt = it->second;
306 m_2 +=
Sqr(val) * cnt / s;
310 if (!std::isinf(m_2))
312 auto var = m_2 -
Sqr(m);
313 return std::make_tuple(m, var, sqrt(var));
319 for (
auto it = it_begin; it != it_end; ++it)
320 var +=
Sqr(it->first - m) * it->second;
322 if (!std::isinf(var))
328 for (
auto it = it_begin; it != it_end; ++it)
329 var +=
Sqr(it->first - m) * it->second / s;
332 return std::make_tuple(m, var, sqrt(var));
337 std::vector<double>
Quantiles(
const std::vector<double> &v,
size_t q,
bool sort_flag =
true);
343 std::sort(sv.begin(), sv.end());
344 return sv[sv.size() / 2];
348 template<
typename T>
inline bool AllEqual(
const std::vector<T> &v) {
return std::all_of(v.begin(), v.end(), [&v](
const T &x){
return x == v.front(); }); }
350 template<
typename T>
inline bool AllEqual(
const std::vector<std::vector<T>> &m)
352 auto refval = m.front().front();
353 for (
const auto &row : m)
354 if (std::any_of(row.begin(), row.end(), [refval](
const T &x){
return x != refval; }))
360 Histogram
MakeHistogram(
const std::vector<double> &v,
size_t nb_bins);
375 MultivariateGaussianMixture
MakeGaussianMixtureModel(
const std::vector<std::vector<double>> &patterns,
size_t nb_seeds = 2);
T MedianValue(const std::vector< T > &v)
Median value.
typename TypeInfo< T >::SumType SumType
size_t Argmin(const std::vector< T > &v)
Return index of a minimal.
size_t ColumnArgmax(const std::vector< std::vector< T >> &m, size_t col)
Return index of a maximal on a column.
double Variance(const std::vector< double > &v)
Return variance of sample.
std::vector< double > MeanPattern(const std::vector< std::vector< double >> &m)
Return mean pattern of sample.
size_t ColumnArgmin(const std::vector< std::vector< T >> &m, size_t col)
Return index of a minimal on a column.
const T & Max(const T &a, const T &b)
Returns the max of two values.
Histogram MakeHistogramSquareRoot(const std::vector< double > &v)
Returns count histogram (#bins = sqrt(pop) )
std::vector< double > Quantiles(const std::vector< double > &v, size_t q, bool sort_flag=true)
Return quantile values of sample.
bool AllEqual(const std::vector< T > &v)
Test if all data values are equal.
Histogram MakeHistogramSturges(const std::vector< double > &v)
Returns count histogram (#bins = 1+log_2(pop) )
std::vector< std::vector< double > > MakeCovariance(const std::vector< std::vector< double >> &m)
Return covariance for sample.
size_t Argmax(const std::vector< T > &v)
Return index of a maximal.
double MeanAsDouble(ITER be, ITER en)
Return mean value of sample as a double value.
double Mean(const std::vector< double > &v)
Return mean value of sample.
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
double StdDeviation(const std::vector< double > &v)
Return deviation of sample.
UnivariateGaussianMixture MakeGaussianMixtureModel(const std::vector< double > &v, size_t nb_seeds=2)
Return Gaussian mixture model modeling current (univariate) sample.
const T & Min(const T &a, const T &b)
Returns the min of two values.
std::pair< T, T > MinMax(const Image< T > &img, CMP cmp=CMP{})
Returns min and max pixel values.
Histogram MakeHistogramScott(const std::vector< double > &v)
Returns count histogram (bin width = 3.5 * stddev / pop^(1/3))
Histogram MakeHistogramRice(const std::vector< double > &v)
Returns count histogram (#bins = 2n^(1/3) )
Histogram MakeHistogramFreedmanDiaconis(const std::vector< double > &v, bool sort_flag=true)
Returns count histogram (bin width = 2 * IQR(v) / pop^(1/3))
std::tuple< double, double, double > MeanVarDev(const std::vector< double > &v)
Return mean, variance and standard deviation of sample.
Histogram MakeHistogram(const Image< T > &img, typename std::enable_if< std::is_arithmetic< T >::value >::type *dummy=nullptr)