Collects statistics on a single quantity (weighted) More...
#include <StatCollector.h>
Public Types | |
using | This_t = StatCollector< T, W > |
this type More... | |
using | Data_t = T |
type of the data More... | |
using | Weight_t = typename Base_t::Weight_t |
type of the weight More... | |
Public Member Functions | |
void | clear () |
Clears all the statistics. More... | |
Add elements | |
void | add (Data_t value, Weight_t weight=Weight_t(1.0)) |
Adds one entry with specified value and weight. More... | |
template<typename Iter > | |
void | add_unweighted (Iter begin, Iter end) |
Adds entries from a sequence with weight 1. More... | |
template<typename Iter , typename Pred > | |
void | add_unweighted (Iter begin, Iter end, Pred extractor) |
Adds entries from a sequence with weight 1. More... | |
template<typename Cont , typename Pred > | |
void | add_unweighted (Cont cont, Pred extractor) |
Adds all entries from a container, with weight 1. More... | |
template<typename Cont > | |
void | add_unweighted (Cont cont) |
Adds all entries from a container, with weight 1. More... | |
template<typename VIter , typename WIter , typename VPred , typename WPred = identity> | |
void | add_weighted (VIter begin_value, VIter end_value, WIter begin_weight, VPred value_extractor, WPred weight_extractor=WPred()) |
Adds entries from a sequence with individually specified weights. More... | |
template<typename Iter > | |
void | add_weighted (Iter begin, Iter end) |
Adds entries from a sequence with individually specified weights. More... | |
template<typename Cont > | |
void | add_weighted (Cont cont) |
Adds entries from a sequence with individually specified weights. More... | |
Statistic retrieval | |
int | N () const |
Returns the number of entries added. More... | |
Weight_t | Weights () const |
Returns the sum of the weights. More... | |
Weight_t | Sum () const |
Returns the weighted sum of the values. More... | |
Weight_t | SumSq () const |
Returns the weighted sum of the square of the values. More... | |
Weight_t | Average () const |
Returns the value average. More... | |
Weight_t | Variance () const |
Returns the square of the RMS of the values. More... | |
Weight_t | RMS () const |
Returns the root mean square. More... | |
Weight_t | AverageWeight () const |
Returns the arithmetic average of the weights. More... | |
Protected Types | |
using | Variable_t = details::DataTracker2< Data_t, Weight_t > |
Protected Types inherited from lar::util::details::WeightTracker< W > | |
using | Weight_t = W |
type of the weight More... | |
Protected Attributes | |
Variable_t | x |
accumulator for variable x More... | |
Protected Attributes inherited from lar::util::details::WeightTracker< W > | |
int | n = 0 |
number of added entries More... | |
Weight_t | w = Weight_t(0) |
total weight More... | |
Private Types | |
using | Base_t = details::WeightTracker< W > |
Additional Inherited Members | |
Protected Member Functions inherited from lar::util::details::WeightTracker< W > | |
void | add (Weight_t weight) |
Adds the specified weight to the statistics. More... | |
void | clear () |
Resets the count. More... | |
int | N () const |
Returns the number of entries added. More... | |
Weight_t | Weights () const |
Returns the sum of the weights. More... | |
Weight_t | AverageWeight () const |
Returns the arithmetic average of the weights. More... | |
Static Protected Member Functions inherited from lar::util::details::WeightTracker< W > | |
template<typename V > | |
static constexpr V | sqr (V const &v) |
Returns the square of the specified value. More... | |
Collects statistics on a single quantity (weighted)
T | type of the quantity |
W | type of the weight (as T by default) |
This is a convenience class, as easy to use as:
or also
that should both print: "Statistics from 3 entries: 3.8".
Other functions are available allowing addition of weighted and unweighted data from collections. For additional examples, see the unit test StatCollector_test.cc .
StatCollector::Variance() is known to be very sensitive to rounding errors, since it uses the formula E[x^2] - E^2[x]. If the variance is effectively small the formula can become negative. In case of negative value produced by the variance formula, the Variance() method will round up to zero. If you know roughly the average of the items you are add()ing, you can reduce the rounding error by subtracting it from the input value; you'll have to shift the average back, while the variance will not be affected; also the sums will be shifted. Example:
A small variance implies values of similar magnitude, and therefore subtracting any single one of them (in the example, the first one) is more effective than it seems. As a rule of thumb, if you are collecting statistics for elements with N significant bits, a 2N significant bits is recommended for statistics collection. That means double
for float
, and long double
for double
(although usually long double
is only marginally more precise than double
, typically 25 or 50% more).
Definition at line 243 of file StatCollector.h.
|
private |
Definition at line 244 of file StatCollector.h.
using lar::util::StatCollector< T, W >::Data_t = T |
type of the data
Definition at line 248 of file StatCollector.h.
using lar::util::StatCollector< T, W >::This_t = StatCollector<T, W> |
this type
Definition at line 247 of file StatCollector.h.
|
protected |
Definition at line 437 of file StatCollector.h.
using lar::util::StatCollector< T, W >::Weight_t = typename Base_t::Weight_t |
type of the weight
Definition at line 249 of file StatCollector.h.
void lar::util::StatCollector< T, W >::add | ( | Data_t | value, |
Weight_t | weight = Weight_t(1.0) |
||
) |
Adds one entry with specified value and weight.
Definition at line 855 of file StatCollector.h.
|
inline |
Adds entries from a sequence with weight 1.
Iter | forward iterator to the elements to be added |
begin | iterator pointing to the first element to be added |
end | iterator pointing after the last element to be added |
The value pointed by the iterator must be convertible to the Data_t type.
Definition at line 269 of file StatCollector.h.
void lar::util::StatCollector< T, W >::add_unweighted | ( | Iter | begin, |
Iter | end, | ||
Pred | extractor | ||
) |
Adds entries from a sequence with weight 1.
Iter | forward iterator to the elements to be added |
Pred | a predicate to extract the element from iterator value |
begin | iterator pointing to the first element to be added |
end | iterator pointing after the last element to be added |
extractor | the predicate extracting the value to be inserted |
The predicate is required to react to a call like with:
Data_t Pred::operator() (typename Iter::value_type);
Definition at line 865 of file StatCollector.h.
|
inline |
Adds all entries from a container, with weight 1.
Cont | type of container of the elements to be added |
Pred | a predicate to extract the element from iterator value |
cont | container of the elements to be added |
extractor | the predicate extracting the value to be inserted |
The predicate is required to react to a call like with:
Data_t Pred::operator() (typename Cont::value_type);
The container must support the range-based for loop syntax, that is is must have std::begin<Cont>() and std::end<Cont>() defined.
Definition at line 303 of file StatCollector.h.
|
inline |
Adds all entries from a container, with weight 1.
Cont | type of container of the elements to be added |
cont | container of the elements to be added |
The container must support the range-based for loop syntax, that is is must have std::begin<Cont>() and std::end<Cont>() defined. The value in the container must be convertible to the Data_t type.
Definition at line 316 of file StatCollector.h.
void lar::util::StatCollector< T, W >::add_weighted | ( | VIter | begin_value, |
VIter | end_value, | ||
WIter | begin_weight, | ||
VPred | value_extractor, | ||
WPred | weight_extractor = WPred() |
||
) |
Adds entries from a sequence with individually specified weights.
VIter | forward iterator to the elements to be added |
WIter | forward iterator to the weights |
VPred | a predicate to extract the element from iterator value |
WPred | a predicate to extract the weight from iterator value |
begin_value | iterator pointing to the first element to be added |
end_value | iterator pointing after the last element to be added |
begin_weight | iterator pointing to the weight of first element |
value_extractor | predicate extracting the value to be inserted |
weight_extractor | predicate extracting the value to be inserted |
Each value is added with the weight pointed by the matching element in the list pointed by begin_weight: the element *(begin_value)
will have weight *(begin_weight)
, the next element *(begin_value + 1)
will have weight *(begin_weight + 1)
, etc.
The predicates are required to react to a call like with:
Definition at line 874 of file StatCollector.h.
void lar::util::StatCollector< T, W >::add_weighted | ( | Iter | begin, |
Iter | end | ||
) |
Adds entries from a sequence with individually specified weights.
Iter | forward iterator to (value, weight) pairs to be added |
begin | iterator pointing to the first element to be added |
end | iterator pointing after the last element to be added |
The value pointed by the iterator must be a pair with first element convertible to the Data_t and the second element convertible to Weight_t. For more complicate structures, use the version with two predicates (using the weight iterator the same as the value iterator).
Definition at line 890 of file StatCollector.h.
|
inline |
Adds entries from a sequence with individually specified weights.
Cont | type of container of (value, weight) pairs to be added |
cont | container of (value, weight) pairs to be added |
The values in the container must be pairs with first element convertible to the Data_t and the second element convertible to Weight_t.
Definition at line 380 of file StatCollector.h.
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::Average | ( | ) | const |
Returns the value average.
std::range_error | if the total weight is 0 (usually: no data) |
Definition at line 907 of file StatCollector.h.
|
inline |
Returns the arithmetic average of the weights.
std::range_error | if no entry was added |
Definition at line 431 of file StatCollector.h.
|
inline |
Clears all the statistics.
Definition at line 899 of file StatCollector.h.
|
inline |
Returns the number of entries added.
Definition at line 392 of file StatCollector.h.
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::RMS | ( | ) | const |
Returns the root mean square.
std::range_error | if the total weight is 0 (see Variance()) |
std::range_error | if Variance() is negative (due to rounding errors) |
Definition at line 927 of file StatCollector.h.
|
inline |
Returns the weighted sum of the values.
Definition at line 398 of file StatCollector.h.
|
inline |
Returns the weighted sum of the square of the values.
Definition at line 401 of file StatCollector.h.
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::Variance | ( | ) | const |
Returns the square of the RMS of the values.
std::range_error | if the total weight is 0 (usually: no data) |
Definition at line 917 of file StatCollector.h.
|
inline |
Returns the sum of the weights.
Definition at line 395 of file StatCollector.h.
|
protected |
accumulator for variable x
Definition at line 439 of file StatCollector.h.