Public Types | Public Member Functions | Protected Types | Protected Attributes | Private Types | List of all members
lar::util::StatCollector< T, W > Class Template Reference

Collects statistics on a single quantity (weighted) More...

#include <StatCollector.h>

Inheritance diagram for lar::util::StatCollector< T, W >:
lar::util::details::WeightTracker< W >

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...
 

Detailed Description

template<typename T, typename W = T>
class lar::util::StatCollector< T, W >

Collects statistics on a single quantity (weighted)


Template Parameters
Ttype of the quantity
Wtype of the weight (as T by default)

This is a convenience class, as easy to use as:

stat.add(3.0, 2.0);
stat.add(4.0, 2.0);
stat.add(5.0, 1.0);
std::cout << "Statistics from " << stat.N() << " entries: "
<< stat.Average() << std::endl;

or also

std::vector<std::pair<double, double>> values({
{ 3.0, 2.0 },
{ 4.0, 2.0 },
{ 5.0, 1.0 },
});
stat.add_weighted(values.begin(), values.end());
std::cout << "Statistics from " << stat.N() << " entries: "
<< stat.Average() << std::endl;

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:

// fill the values, shifted
for (auto element: elements)
sc.add(element - elements[0], (*weight)++);
auto sum_weights = sc.Weights();
auto sum_values = sc.Sum() - elements[0] * sc.Weights();
auto sum_values2 = sc.SumSq() + sqr(elements[0]) * sc.Weights()
+ 2. * elements[0] * sc.Sum();
auto average = sc.Average() + elements[0];
auto variance = sc.Variance();

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.

Member Typedef Documentation

template<typename T, typename W = T>
using lar::util::StatCollector< T, W >::Base_t = details::WeightTracker<W>
private

Definition at line 244 of file StatCollector.h.

template<typename T, typename W = T>
using lar::util::StatCollector< T, W >::Data_t = T

type of the data

Definition at line 248 of file StatCollector.h.

template<typename T, typename W = T>
using lar::util::StatCollector< T, W >::This_t = StatCollector<T, W>

this type

Definition at line 247 of file StatCollector.h.

template<typename T, typename W = T>
using lar::util::StatCollector< T, W >::Variable_t = details::DataTracker2<Data_t, Weight_t>
protected

Definition at line 437 of file StatCollector.h.

template<typename T, typename W = T>
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.

Member Function Documentation

template<typename T , typename W >
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.

856 {
858  x.add(value, weight);
859 } // StatCollector<T, W>::add()
void add(Data_t v, Weight_t w)
Adds the specified weight to the statistics.
void add(Weight_t weight)
Adds the specified weight to the statistics.
Definition: StatCollector.h:53
weight
Definition: test.py:257
Variable_t x
accumulator for variable x
template<typename T, typename W = T>
template<typename Iter >
void lar::util::StatCollector< T, W >::add_unweighted ( Iter  begin,
Iter  end 
)
inline

Adds entries from a sequence with weight 1.

Template Parameters
Iterforward iterator to the elements to be added
Parameters
beginiterator pointing to the first element to be added
enditerator 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.

270  { add_unweighted(begin, end, identity()); }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void add_unweighted(Iter begin, Iter end)
Adds entries from a sequence with weight 1.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
template<typename T , typename W >
template<typename Iter , typename Pred >
void lar::util::StatCollector< T, W >::add_unweighted ( Iter  begin,
Iter  end,
Pred  extractor 
)

Adds entries from a sequence with weight 1.

Template Parameters
Iterforward iterator to the elements to be added
Preda predicate to extract the element from iterator value
Parameters
beginiterator pointing to the first element to be added
enditerator pointing after the last element to be added
extractorthe 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.

866 {
867  std::for_each
868  (begin, end, [this, extractor](auto item) { this->add(extractor(item)); });
869 } // StatCollector<T, W>::add_unweighted(Iter, Iter, Pred)
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.
template<typename T, typename W = T>
template<typename Cont , typename Pred >
void lar::util::StatCollector< T, W >::add_unweighted ( Cont  cont,
Pred  extractor 
)
inline

Adds all entries from a container, with weight 1.

Template Parameters
Conttype of container of the elements to be added
Preda predicate to extract the element from iterator value
Parameters
contcontainer of the elements to be added
extractorthe 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.

304  { add_unweighted(std::begin(cont), std::end(cont), extractor); }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void add_unweighted(Iter begin, Iter end)
Adds entries from a sequence with weight 1.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
template<typename T, typename W = T>
template<typename Cont >
void lar::util::StatCollector< T, W >::add_unweighted ( Cont  cont)
inline

Adds all entries from a container, with weight 1.

Template Parameters
Conttype of container of the elements to be added
Parameters
contcontainer 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.

317  { add_unweighted(std::begin(cont), std::end(cont)); }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void add_unweighted(Iter begin, Iter end)
Adds entries from a sequence with weight 1.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
template<typename T , typename W >
template<typename VIter , typename WIter , typename VPred , typename WPred >
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.

Template Parameters
VIterforward iterator to the elements to be added
WIterforward iterator to the weights
VPreda predicate to extract the element from iterator value
WPreda predicate to extract the weight from iterator value
Parameters
begin_valueiterator pointing to the first element to be added
end_valueiterator pointing after the last element to be added
begin_weightiterator pointing to the weight of first element
value_extractorpredicate extracting the value to be inserted
weight_extractorpredicate 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:

Data_t VPred::operator() (typename VIter::value_type);
Weight_t WPred::operator() (typename WIter::value_type);

Definition at line 874 of file StatCollector.h.

879  {
880  while (begin_value != end_value) {
881  add(value_extractor(*begin_value), weight_extractor(*begin_weight));
882  ++begin_value;
883  ++begin_weight;
884  } // while
885 } // StatCollector<T, W>::add_weighted(VIter, VIter, WIter, VPred, WPred)
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.
template<typename T , typename W >
template<typename Iter >
void lar::util::StatCollector< T, W >::add_weighted ( Iter  begin,
Iter  end 
)

Adds entries from a sequence with individually specified weights.

Template Parameters
Iterforward iterator to (value, weight) pairs to be added
Parameters
beginiterator pointing to the first element to be added
enditerator 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.

890  {
891 
892  std::for_each(begin, end, [this](auto p) { this->add(p.first, p.second); });
893 
894 } // StatCollector<T, W>::add_weighted(Iter, Iter)
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
p
Definition: test.py:223
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.
template<typename T, typename W = T>
template<typename Cont >
void lar::util::StatCollector< T, W >::add_weighted ( Cont  cont)
inline

Adds entries from a sequence with individually specified weights.

Template Parameters
Conttype of container of (value, weight) pairs to be added
Parameters
contcontainer 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.

381  { add_weighted(std::begin(cont), std::end(cont)); }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
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.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
template<typename T , typename W >
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::Average ( ) const

Returns the value average.

Returns
the value average
Exceptions
std::range_errorif the total weight is 0 (usually: no data)

Definition at line 907 of file StatCollector.h.

908 {
909  if (Weights() == Weight_t(0))
910  throw std::range_error("StatCollector<>::Average(): divide by 0");
911  return Sum() / Weights();
912 } // StatCollector<T, W>::Average()
typename Base_t::Weight_t Weight_t
type of the weight
Weight_t Sum() const
Returns the weighted sum of the values.
Weight_t Weights() const
Returns the sum of the weights.
template<typename T, typename W = T>
Weight_t lar::util::StatCollector< T, W >::AverageWeight ( ) const
inline

Returns the arithmetic average of the weights.

Returns
the weight average
Exceptions
std::range_errorif no entry was added

Definition at line 431 of file StatCollector.h.

431 { return Base_t::AverageWeight(); }
Weight_t AverageWeight() const
Returns the arithmetic average of the weights.
template<typename T , typename W >
void lar::util::StatCollector< T, W >::clear ( )
inline

Clears all the statistics.

Definition at line 899 of file StatCollector.h.

899  {
900  Base_t::clear();
901  x.clear();
902 } // StatCollector<T, W>::clear()
void clear()
Resets the count.
Definition: StatCollector.h:56
void clear()
Resets the count.
Variable_t x
accumulator for variable x
template<typename T, typename W = T>
int lar::util::StatCollector< T, W >::N ( void  ) const
inline

Returns the number of entries added.

Definition at line 392 of file StatCollector.h.

392 { return Base_t::N(); }
int N() const
Returns the number of entries added.
Definition: StatCollector.h:59
template<typename T , typename W >
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::RMS ( ) const

Returns the root mean square.

Returns
the RMS of the values
Exceptions
std::range_errorif the total weight is 0 (see Variance())
std::range_errorif Variance() is negative (due to rounding errors)

Definition at line 927 of file StatCollector.h.

928 {
929  const Weight_t rms2 = Variance();
930  if (rms2 < Weight_t(0)) return 0.;
931 // throw std::range_error("StatCollector<>::RMS(): negative RMS^2");
932  return std::sqrt(rms2);
933 } // StatCollector<T, W>::RMS()
typename Base_t::Weight_t Weight_t
type of the weight
Weight_t Variance() const
Returns the square of the RMS of the values.
template<typename T, typename W = T>
Weight_t lar::util::StatCollector< T, W >::Sum ( ) const
inline

Returns the weighted sum of the values.

Definition at line 398 of file StatCollector.h.

398 { return x.Sum(); }
Weight_t Sum(unsigned int n) const
Returns the sum of the values to the power n (1 <= n <= 2, no check)
Variable_t x
accumulator for variable x
template<typename T, typename W = T>
Weight_t lar::util::StatCollector< T, W >::SumSq ( ) const
inline

Returns the weighted sum of the square of the values.

Definition at line 401 of file StatCollector.h.

401 { return x.SumSq(); }
Weight_t SumSq() const
Returns the weighted sum of the square of the entries.
Variable_t x
accumulator for variable x
template<typename T , typename W >
lar::util::StatCollector< T, W >::Weight_t lar::util::StatCollector< T, W >::Variance ( ) const

Returns the square of the RMS of the values.

Returns
the square of the RMS of the values
Exceptions
std::range_errorif the total weight is 0 (usually: no data)

Definition at line 917 of file StatCollector.h.

918 {
919  if (Weights() == Weight_t(0))
920  throw std::range_error("StatCollector<>::Variance(): divide by 0");
921  return std::max(Weight_t(0), (SumSq() - sqr(Sum()) / Weights()) / Weights());
922 } // StatCollector<T, W>::Variance()
static constexpr V sqr(V const &v)
Returns the square of the specified value.
Definition: StatCollector.h:74
typename Base_t::Weight_t Weight_t
type of the weight
Weight_t Sum() const
Returns the weighted sum of the values.
Weight_t SumSq() const
Returns the weighted sum of the square of the values.
static int max(int a, int b)
Weight_t Weights() const
Returns the sum of the weights.
template<typename T, typename W = T>
Weight_t lar::util::StatCollector< T, W >::Weights ( ) const
inline

Returns the sum of the weights.

Definition at line 395 of file StatCollector.h.

395 { return Base_t::Weights(); }
Weight_t Weights() const
Returns the sum of the weights.
Definition: StatCollector.h:62

Member Data Documentation

template<typename T, typename W = T>
Variable_t lar::util::StatCollector< T, W >::x
protected

accumulator for variable x

Definition at line 439 of file StatCollector.h.


The documentation for this class was generated from the following file: