34 using namespace std::placeholders;
42 const auto s = double(v.size());
43 auto m = std::accumulate(v.begin(), v.end(), 0.0);
62 auto mp = std::vector<double>(m.front().size(), 0.0);
64 for (
const auto &row : m)
65 for (
auto k :
Range(row))
69 for (
auto k :
Range(mp))
70 if (std::isinf(mp[k]))
77 std::transform(mp.begin(), mp.end(), mp.begin(), std::bind(std::divides<double>{}, _1, double(m.size())));
80 auto s = double(m.size());
81 std::vector<double>(m.front().size(), 0.0).swap(mp);
83 for (
const auto &row : m)
84 for (
auto k :
Range(row))
106 const auto m =
Mean(v);
108 const auto s = double(v.size());
113 if (!std::isinf(var))
119 var +=
Sqr(d - m) / s;
131 const auto card = m.size();
132 const auto dim = m.front().size();
133 auto cov = std::vector<std::vector<double>>(dim, std::vector<double>(dim));
135 for (
size_t i = 0; i < dim; ++i)
136 for (
auto j = i; j < dim; ++j)
138 for (
size_t k = 0; k < card; ++k)
139 cov[i][j] += m[k][i] * m[k][j];
140 cov[i][j] /= double(card);
143 cov[j][i] = cov[i][j];
157 const auto s = double(v.size());
182 if (!std::isinf(m_2))
184 auto var = m_2 -
Sqr(m);
185 return std::make_tuple(m, var, sqrt(var));
193 if (!std::isinf(var))
199 var +=
Sqr(x - m) / s;
201 return std::make_tuple(m, var, sqrt(var));
216 std::vector<double>
crn::Quantiles(
const std::vector<double> &v,
size_t q,
bool sort_flag)
218 if ((q < 3) || (q > v.size()))
219 throw ExceptionDomain(
StringUTF8(
"StatisticSample::Quantiles(const std::vector<double>&, size_t, bool): ") +
_(
"Illegal range."));
221 auto qt = std::vector<double>(q - 1);
222 const auto jump = v.size() / q;
228 std::sort(w.begin(), w.end());
229 for(
size_t k = 0; k < q - 1; ++k)
236 for(
size_t k = 0; k < q - 1; ++k)
253 auto left_bound = std::get<0>(mM);
254 auto right_bound = std::get<1>(mM);
255 auto range = right_bound - left_bound;
256 auto delta = range / double(nb_bins);
259 h.SetBin(0, (
unsigned int)v.size());
263 auto id = size_t((d - left_bound) / delta);
295 return MakeHistogram(v,
size_t(2 * pow(
double(v.size()), 1.0 / 3.0)));
304 const auto delta = 3.5 *
StdDeviation(v) / pow(
double(v.size()), 1.0 / 3.0);
305 const auto& mM =
MinMax(v);
306 return MakeHistogram(v, 1 +
size_t((std::get<1>(mM) - std::get<0>(mM)) / delta));
315 const auto& qt =
Quantiles(v,
size_t(4), sort_flag);
316 const auto delta = 2 * (qt.back() - qt.front()) / pow(
double(v.size()), 1.0 / 3.0);
317 const auto& mM =
MinMax(v);
318 return MakeHistogram(v, 1 +
size_t((std::get<1>(mM) - std::get<0>(mM)) / delta));
336 gaussian_mixture_model.
EM(v, nb_seeds);
338 return gaussian_mixture_model;
355 gaussian_mixture_model.
EM(patterns, (
unsigned int)(nb_seeds));
357 return gaussian_mixture_model;
ScalarRange< T > Range(T b, T e)
Creates a range [[b, e[[.
Multivariate gaussian mixture.
double Variance(const std::vector< double > &v)
Return variance of sample.
Univariate Gaussian distribution.
std::vector< double > MeanPattern(const std::vector< std::vector< double >> &m)
Return mean pattern of sample.
unsigned int EM(const MatrixDouble &patterns, size_t nbSeeds=2, double epsilon=std::numeric_limits< double >::epsilon(), size_t MaximalIterations=100)
Expectation Maximization.
Histogram MakeHistogramSquareRoot(const std::vector< double > &v)
Returns count histogram (#bins = sqrt(pop) )
void AddMember(UnivariateGaussianPDF pdf, double Weight)
Adds a density function.
std::vector< double > Quantiles(const std::vector< double > &v, size_t q, bool sort_flag=true)
Return quantile values of sample.
Histogram MakeHistogramSturges(const std::vector< double > &v)
Returns count histogram (#bins = 1+log_2(pop) )
void AddMember(const MultivariateGaussianPDF &pdf, double Weight)
Adds a density function.
std::vector< std::vector< double > > MakeCovariance(const std::vector< std::vector< double >> &m)
Return covariance for sample.
unsigned int EM(const MatrixDouble &patterns, size_t nbSeeds=2, double epsilon=std::numeric_limits< double >::epsilon(), size_t maximalIterations=100)
Expectation Maximization.
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.
Univariate Gaussian mixture.
UnivariateGaussianMixture MakeGaussianMixtureModel(const std::vector< double > &v, size_t nb_seeds=2)
Return Gaussian mixture model modeling current (univariate) sample.
Mother class for integer histograms.
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))
Square double matrix class.
A character string class.
Multivariate Gaussian distribution.
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)