3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_MATH_STATS_STATISTIC_H
7#define ELEMENTA_MATH_STATS_STATISTIC_H
57template <
class DatumIterator>
61template <
class DatumIterator>
63 {
return(
static_cast<StaDatum>(*ti)); }
74template <
class Policy>
88 getstadatum_direct<TIt>);
102 {
return(v < prev ? v : prev); }
114 {
return(v > prev ? v : prev); }
128 getstadatum_direct<TIt>);
132 ++n;
return(1.0/
static_cast<StaDatum>(n) *
133 (v +
static_cast<StaDatum>(n-1) * prev)); }
154 getstadatum_direct<TIt>);
192template <
class Calc >
202 void reset(
void) { calculated_ =
false; domsize_ = 0; }
205 bool calculated(
void)
const noexcept {
return(calculated_); }
223 getstadatum_direct<TIt>);
237 void checkCalculated(
void)
const
262 getstadatum_direct<TIt>)
263 {
stamin.calculate(first,pastend,getdatum);
264 stamax.calculate(first,pastend,getdatum); }
288 getstadatum_direct<TIt>)
289 {
stamean.calculate(first,pastend,getdatum);
290 stavar.calculate(first,pastend,getdatum); }
316 getstadatum_direct<TIt>)
317 {
stamin.calculate(first,pastend,getdatum);
318 stamax.calculate(first,pastend,getdatum);
319 stamean.calculate(first,pastend,getdatum);
320 stavar.calculate(first,pastend,getdatum); }
348template <
class Calc >
361 if (first == pastend)
ELE_CODE_INVARG(
"Cannot calculate statistic on empty "
363 value_ = calculator_.calc(first,pastend,domsize_,getdatum);
372 if (domsize_ == std::numeric_limits<SampleSize>::max())
382 value_= calculator_.incCalc(prev_,v,domsize_);
397StaDatum StaPol_BaseOnInc::calc(
const TIt & first,
const TIt & pastend,
405 while (it != pastend)
407 v = incCalc(v,getdatum(it),n);
422StaDatum StaPol_Mean::calc(
const TIt & first,
const TIt & pastend,
423 SampleSize & n,
const GetStaDatumFun<TIt> & getdatum)
429 while (it != pastend)
435 return(s /
static_cast<StaDatum>(n));
446StaDatum StaPol_Var::calc(
const TIt & first,
const TIt & pastend,
447 SampleSize & n,
const GetStaDatumFun<TIt> & getdatum)
449 n = meanstat_->domainSize();
450 if (n < 2)
return(0);
456 while (it0 != pastend)
458 aux = getdatum(it0) - mu;
462 prev_ = s /
static_cast<StaDatum>(n-1);
return(prev_);
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
A group of statistics consisting of mean and var.
A group of statistics consisting of min and max.
A group of statistics consisting of min, max, mean and var.
An incomplete base policy for Statistic that uses incCalc() to do calcs.
Policy for Statistic that calculates the maximum.
Policy for Statistic that calculates the mean.
Policy for Statistic that calculates the minimum.
Policy for Statistic that calculates the variance.
A statistic, i.e., a numerical function of a sample of data.
Statistic< StaPol_Max > stamax
The max statistic.
Statistic< StaPol_Var > stavar
The var statistic.
Statistic(const Calc c)
Constructor: a statistic not calculated ever.
void update(StaDatum v)
Update statistic on all members of the group at once.
StaDatum update(StaDatum v)
Calculate the statistic incrementally from its previous value.
size_t SampleSize
The number of elements in a sample.
void calculate(const TIt &first, const TIt &pastend, const GetStaDatumFun< TIt > &getdatum=getstadatum_direct< TIt >)
Calculate the statistic on all members of the group at once.
Statistic< StaPol_Mean > stamean
The mean statistic.
void update(StaDatum v)
Update statistic on all members of the group at once.
void reset(void)
Set the statistic to the previous-to-calculated state.
Statistic< StaPol_Max > stamax
The max statistic.
void calculate(const TIt &first, const TIt &pastend, const GetStaDatumFun< TIt > &getdatum=getstadatum_direct< TIt >)
Calculate the statistic on all members of the group at once.
void reset(void)
Reset all statistics.
StaDatum calculate(const TIt &first, const TIt &pastend, const GetStaDatumFun< TIt > &getdatum=getstadatum_direct< TIt >)
Calculate the statistic on a sample and return its value.
StaDatum previous(void) const
Return the value of the statistic previous to the current one.
SampleSize domainSize(void) const noexcept
Return the number of data that has produced the current value.
Statistic< StaPol_Min > stamin
The min statistic.
StaDatum value(void) const
Return its current calculated value, or throw if none.
Statistic< StaPol_Var > stavar
The var statistic.
Statistic< StaPol_Mean > stamean
The mean statistic.
bool calculated(void) const noexcept
Return whether the statistic has been calculated.
constexpr StaDatum getstadatum_direct(const DatumIterator &ti)
A simple GetStaDatumFun that gets directly the datum from the iterator.
std::function< StaDatum(const DatumIterator &) > GetStaDatumFun
A function type that gets a datum from an iterator.
void calculate(const TIt &first, const TIt &pastend, const GetStaDatumFun< TIt > &getdatum=getstadatum_direct< TIt >)
Calculate the statistic on all members of the group at once.
double StaDatum
Type of data to allow for statistic calculation.
void reset(void)
Reset all statistics.
void reset(void)
Reset all statistics.
StaPol_Var(Statistic< StaPol_Mean > &meanstat)
Construct the policy for using incremental calculations.
void update(StaDatum v)
Update statistic on all members of the group at once.
Statistic< StaPol_Min > stamin
The min statistic.