20 #include <initializer_list> 
   34 #define BOOST_TEST_MODULE ( StatCollector_test ) 
   35 #include <boost/test/unit_test.hpp> 
   47 template <
typename T, 
typename W>
 
   59   BOOST_TEST(stats.
N() == 
n);
 
   64     const Weight_t average = weights / 
n;
 
   71     BOOST_CHECK_THROW(stats.
Average(),  std::range_error);
 
   72     BOOST_CHECK_THROW(stats.
Variance(), std::range_error);
 
   73     BOOST_CHECK_THROW(stats.
RMS(),      std::range_error);
 
   76     const Weight_t average = sum / weights;
 
   79     BOOST_TEST(
double(stats.
Sum()) ==      
double(sum),     0.01% 
tolerance());
 
   80     BOOST_TEST(
double(stats.
SumSq()) ==    
double(sumsq),   0.01% 
tolerance());
 
   83     BOOST_TEST(
double(stats.
RMS()) ==      
double(rms),     0.1% 
tolerance());
 
   87 template <
typename T, 
typename W>
 
  105   BOOST_TEST(stats.
N() ==       
n);
 
  110     const Weight_t average = weights / 
n;
 
  118     BOOST_CHECK_THROW(stats.
AverageX(),          std::range_error);
 
  119     BOOST_CHECK_THROW(stats.
VarianceX(),         std::range_error);
 
  120     BOOST_CHECK_THROW(stats.
RMSx(),              std::range_error);
 
  123     BOOST_CHECK_THROW(stats.
AverageY(),          std::range_error);
 
  124     BOOST_CHECK_THROW(stats.
VarianceY(),         std::range_error);
 
  125     BOOST_CHECK_THROW(stats.
RMSy(),              std::range_error);
 
  127     BOOST_CHECK_THROW(stats.
Covariance(),        std::range_error);
 
  131     const Weight_t averageX = sumX / weights;
 
  132     const Weight_t averageY = sumY / weights;
 
  136     BOOST_TEST(
double(stats.
SumX()) ==              
double(sumX),      0.01% 
tolerance());
 
  137     BOOST_TEST(
double(stats.
SumSqX()) ==            
double(sumsqX),    0.01% 
tolerance());
 
  140     BOOST_TEST(
double(stats.
RMSx()) ==              
double(rmsX),      0.1% 
tolerance());
 
  141     BOOST_TEST(
double(stats.
SumY()) ==              
double(sumY),      0.01% 
tolerance());
 
  142     BOOST_TEST(
double(stats.
SumSqY()) ==            
double(sumsqY),    0.01% 
tolerance());
 
  145     BOOST_TEST(
double(stats.
RMSy()) ==              
double(rmsY),      0.1% 
tolerance());
 
  146     BOOST_TEST(
double(stats.
SumXY()) ==             
double(sumXY),     0.01% 
tolerance());
 
  159 template <
typename T, 
typename W = T>
 
  165   using WeightedData_t = std::vector<std::pair<Data_t, Weight_t>>;
 
  168   std::valarray<Data_t> unweighted_data{
 
  169     Data_t(5), Data_t(7), Data_t(7), Data_t(13)
 
  171   WeightedData_t unweighted_data_weight({
 
  172     { Data_t(5), Weight_t(1) },
 
  173     { Data_t(7), Weight_t(1) },
 
  174     { Data_t(7), Weight_t(1) },
 
  175     { Data_t(13), Weight_t(1) }
 
  178   Weight_t uw_weights = Weight_t(  4.);
 
  179   Weight_t uw_sum     = Weight_t( 32.);
 
  180   Weight_t uw_sumsq   = Weight_t(292.);
 
  181   Weight_t uw_rms     = Weight_t(  3.);
 
  183   WeightedData_t weighted_data({
 
  184     { Data_t(5), Weight_t(1) },
 
  185     { Data_t(7), Weight_t(2) },
 
  186     { Data_t(13), Weight_t(1) }
 
  189   Weight_t w_weights = Weight_t(  4.);
 
  190   Weight_t w_sum     = Weight_t( 32.);
 
  191   Weight_t w_sumsq   = Weight_t(292.);
 
  192   Weight_t w_rms     = Weight_t(  3.);
 
  200   CheckStats<Data_t, Weight_t>(stats, 0, 0., 0., 0., 0. );
 
  210   CheckStats<Data_t, Weight_t>(stats, w_n, w_weights, w_sum, w_sumsq, w_rms);
 
  219   CheckStats<Data_t, Weight_t>(stats, 0, 0., 0., 0., 0. );
 
  223   CheckStats(stats, uw_n, uw_weights, uw_sum, uw_sumsq, uw_rms);
 
  227   stats.add_unweighted(unweighted_data);
 
  228   CheckStats(stats, uw_n, uw_weights, uw_sum, uw_sumsq, uw_rms);
 
  232   stats.add_unweighted(
 
  233     unweighted_data_weight.begin(), unweighted_data_weight.end(),
 
  234     [](std::pair<Data_t, Weight_t> d){ 
return d.first; }
 
  236   CheckStats(stats, uw_n, uw_weights, uw_sum, uw_sumsq, uw_rms);
 
  240   stats.add_unweighted(unweighted_data_weight,
 
  241     [](std::pair<Data_t, Weight_t> d){ 
return d.first; }
 
  243   CheckStats(stats, uw_n, uw_weights, uw_sum, uw_sumsq, uw_rms);
 
  252   stats.add_weighted(weighted_data.begin(), weighted_data.end());
 
  253   CheckStats(stats, w_n, w_weights, w_sum, w_sumsq, w_rms);
 
  257   stats.add_weighted(weighted_data);
 
  258   CheckStats(stats, w_n, w_weights, w_sum, w_sumsq, w_rms);
 
  269 template <
typename T, 
typename W = T>
 
  276   using UnweightedItem_t = std::pair<Data_t, Data_t>;
 
  277   using WeightedItem_t = std::tuple<Data_t, Data_t, Weight_t>;
 
  280   using WeightedData_t = std::vector<WeightedItem_t>;
 
  283   std::vector<UnweightedItem_t> unweighted_data{
 
  284     { Data_t(5),  Data_t(10) },
 
  285     { Data_t(7),  Data_t(14) },
 
  286     { Data_t(7),  Data_t(14) },
 
  287     { Data_t(13), Data_t(26) }
 
  289   WeightedData_t unweighted_data_weight({
 
  290     WeightedItem_t{ Data_t(5),  Data_t(10), Weight_t(1) }, 
 
  291     WeightedItem_t{ Data_t(7),  Data_t(14), Weight_t(1) }, 
 
  292     WeightedItem_t{ Data_t(7),  Data_t(14), Weight_t(1) }, 
 
  293     WeightedItem_t{ Data_t(13), Data_t(26), Weight_t(1) }  
 
  299   Weight_t uw_weights  = Weight_t(    4.);
 
  300   Weight_t uw_sumX     = Weight_t(   32.);
 
  301   Weight_t uw_sumsqX   = Weight_t(  292.);
 
  302   Weight_t uw_rmsX     = Weight_t(    3.);
 
  303   Weight_t uw_sumY     = Weight_t(   64.);
 
  304   Weight_t uw_sumsqY   = Weight_t( 1168.);
 
  305   Weight_t uw_rmsY     = Weight_t(    6.);
 
  306   Weight_t uw_sumXY    = Weight_t(  584.);
 
  307   Weight_t uw_cov      = Weight_t(   18.);
 
  308   Weight_t uw_lin_corr = Weight_t(    1.);
 
  310   WeightedData_t weighted_data({
 
  311     WeightedItem_t{ Data_t(5),  Data_t(10), Weight_t(1) },
 
  312     WeightedItem_t{ Data_t(7),  Data_t(14), Weight_t(2) },
 
  313     WeightedItem_t{ Data_t(13), Data_t(26), Weight_t(1) }
 
  316   Weight_t w_weights  = Weight_t(    4.);
 
  317   Weight_t w_sumX     = Weight_t(   32.);
 
  318   Weight_t w_sumsqX   = Weight_t(  292.);
 
  319   Weight_t w_rmsX     = Weight_t(    3.);
 
  320   Weight_t w_sumY     = Weight_t(   64.);
 
  321   Weight_t w_sumsqY   = Weight_t( 1168.);
 
  322   Weight_t w_rmsY     = Weight_t(    6.);
 
  323   Weight_t w_sumXY    = Weight_t(  584.);
 
  324   Weight_t w_cov      = Weight_t(   18.);
 
  325   Weight_t w_lin_corr = Weight_t(    1.);
 
  333   CheckStats<Data_t, Weight_t>(stats, 0, 0.,
 
  345   for (
auto const& data: weighted_data) {
 
  346     if (std::get<2>(data) == Weight_t(1))
 
  347       stats.
add(std::get<0>(data), std::get<1>(data));
 
  349       stats.add(std::get<0>(data), std::get<1>(data), std::get<2>(data));
 
  353   CheckStats<Data_t, Weight_t>(stats, w_n, w_weights,
 
  354     w_sumX, w_sumsqX, w_rmsX,
 
  355     w_sumY, w_sumsqY, w_rmsY,
 
  356     w_sumXY, w_cov, w_lin_corr
 
  366   CheckStats<Data_t, Weight_t>(stats, 0, 0.,
 
  374   CheckStats<Data_t, Weight_t>(stats, uw_n, uw_weights,
 
  375     uw_sumX, uw_sumsqX, uw_rmsX,
 
  376     uw_sumY, uw_sumsqY, uw_rmsY,
 
  377     uw_sumXY, uw_cov, uw_lin_corr
 
  382   stats.add_unweighted(unweighted_data);
 
  383   CheckStats<Data_t, Weight_t>(stats, uw_n, uw_weights,
 
  384     uw_sumX, uw_sumsqX, uw_rmsX,
 
  385     uw_sumY, uw_sumsqY, uw_rmsY,
 
  386     uw_sumXY, uw_cov, uw_lin_corr
 
  391   stats.add_unweighted(
 
  392     unweighted_data_weight.begin(), unweighted_data_weight.end(),
 
  393     [](WeightedItem_t 
const& d)
 
  394       { 
return UnweightedItem_t{ std::get<0>(d), std::get<1>(d) }; }
 
  396   CheckStats<Data_t, Weight_t>(stats, uw_n, uw_weights,
 
  397     uw_sumX, uw_sumsqX, uw_rmsX,
 
  398     uw_sumY, uw_sumsqY, uw_rmsY,
 
  399     uw_sumXY, uw_cov, uw_lin_corr
 
  404   stats.add_unweighted(unweighted_data_weight,
 
  405     [](WeightedItem_t 
const& d)
 
  406       { 
return UnweightedItem_t{ std::get<0>(d), std::get<1>(d) }; }
 
  408   CheckStats<Data_t, Weight_t>(stats, uw_n, uw_weights,
 
  409     uw_sumX, uw_sumsqX, uw_rmsX,
 
  410     uw_sumY, uw_sumsqY, uw_rmsY,
 
  411     uw_sumXY, uw_cov, uw_lin_corr
 
  421   stats.add_weighted(weighted_data.begin(), weighted_data.end());
 
  423     w_sumX, w_sumsqX, w_rmsX,
 
  424     w_sumY, w_sumsqY, w_rmsY,
 
  425     w_sumXY, w_cov, w_lin_corr
 
  430   stats.add_weighted(weighted_data);
 
  432     w_sumX, w_sumsqX, w_rmsX,
 
  433     w_sumY, w_sumsqY, w_rmsY,
 
  434     w_sumXY, w_cov, w_lin_corr
 
  445 template <
typename T>
 
  450   std::initializer_list<Data_t> more_data{ 7, -20,  44, 78, 121 }; 
 
  453   std::array<Data_t, 5> even_more_data   {{ 7,  -2, 123, 78, 121 }}; 
 
  456   std::unique_ptr<lar::util::MinMaxCollector<Data_t>> collector;
 
  464   BOOST_TEST(!collector->has_data());
 
  466   collector->add(Data_t(10));
 
  468   BOOST_TEST(collector->has_data());
 
  470   BOOST_TEST(collector->min() == Data_t(  10));
 
  471   BOOST_TEST(collector->max() == Data_t(  10));
 
  473   collector->add(more_data);
 
  474   BOOST_TEST(collector->min() == Data_t( -20));
 
  475   BOOST_TEST(collector->max() == Data_t( 121));
 
  477   collector->add(even_more_data.begin(), even_more_data.end());
 
  478   BOOST_TEST(collector->min() == Data_t( -20));
 
  479   BOOST_TEST(collector->max() == Data_t( 123));
 
  487   BOOST_TEST(collector->has_data());
 
  489   collector->add(Data_t(10));
 
  491   BOOST_TEST(collector->has_data());
 
  493   BOOST_TEST(collector->min() == Data_t(  -25));
 
  494   BOOST_TEST(collector->max() == Data_t(  10));
 
  496   collector->add(more_data);
 
  497   BOOST_TEST(collector->min() == Data_t( -25));
 
  498   BOOST_TEST(collector->max() == Data_t( 121));
 
  500   collector->add(even_more_data.begin(), even_more_data.end());
 
  501   BOOST_TEST(collector->min() == Data_t( -25));
 
  502   BOOST_TEST(collector->max() == Data_t( 123));
 
  510   std::array<Data_t, 3> init_data{{ -25, 3, 1 }};
 
  516   BOOST_TEST(collector->has_data());
 
  518   collector->add(Data_t(10));
 
  520   BOOST_TEST(collector->has_data());
 
  522   BOOST_TEST(collector->min() == Data_t( -25));
 
  523   BOOST_TEST(collector->max() == Data_t(  10));
 
  525   collector->add(more_data);
 
  526   BOOST_TEST(collector->min() == Data_t( -25));
 
  527   BOOST_TEST(collector->max() == Data_t( 121));
 
  529   collector->add(even_more_data.begin(), even_more_data.end());
 
  530   BOOST_TEST(collector->min() == Data_t( -25));
 
  531   BOOST_TEST(collector->max() == Data_t( 123));
 
  549   StatCollectorTest<int, int>();
 
  553   StatCollectorTest<int, float>();
 
  557   StatCollectorTest<float, int>();
 
  561   StatCollectorTest<double, double>();
 
  569   StatCollector2DTest<int, int>();
 
  573   StatCollector2DTest<int, float>();
 
  577   StatCollector2DTest<float, int>();
 
  581   StatCollector2DTest<double, double>();
 
  589   MinMaxCollectorTest<int>();
 
  593   MinMaxCollectorTest<double>();
 
void MinMaxCollectorTest()
Tests MinMaxCollector object with a known input. 
void CheckStats(lar::util::StatCollector< T, W > const &stats, int n, W weights, W sum, W sumsq, W rms)
Weight_t SumSqY() const 
Returns the weighted sum of the square of the y values. 
void add(Data_t x, Data_t y, Weight_t weight=Weight_t(1.0))
Adds one entry with specified values and weight. 
Weight_t AverageWeight() const 
Returns the arithmetic average of the weights. 
Weight_t SumXY() const 
Returns the weighted sum of the product of x and y values. 
Weight_t Covariance() const 
Returns the covariance of the (x, y) pair. 
Classes gathering simple statistics. 
Weight_t RMS() const 
Returns the root mean square. 
Weight_t VarianceY() const 
Returns the variance of the y values. 
Weight_t Average() const 
Returns the value average. 
Keeps track of the minimum and maximum value we observed. 
Weight_t Variance() const 
Returns the square of the RMS of the values. 
Weight_t Sum() const 
Returns the weighted sum of the values. 
int N() const 
Returns the number of entries added. 
Weight_t AverageWeight() const 
Returns the arithmetic average of the weights. 
Weight_t AverageY() const 
Returns the y value average. 
Weight_t SumSqX() const 
Returns the weighted sum of the square of the x values. 
auto end(FixedBins< T, C > const &) noexcept
Weight_t AverageX() const 
Returns the x value average. 
Weight_t SumSq() const 
Returns the weighted sum of the square of the values. 
int N() const 
Returns the number of entries added. 
Weight_t SumX() const 
Returns the weighted sum of the x values. 
auto begin(FixedBins< T, C > const &) noexcept
Weight_t SumY() const 
Returns the weighted sum of the y values. 
void StatCollector2DTest()
Tests StatCollector2D object with a known input. 
Collects statistics on two homogeneous quantities (weighted) 
Weight_t LinearCorrelation() const 
Returns the linear correlation. 
Weight_t RMSy() const 
Returns the standard deviation of the y sample. 
Weight_t RMSx() const 
Returns the standard deviation of the x sample. 
Weight_t Weights() const 
Returns the sum of the weights. 
Weight_t VarianceX() const 
Returns the variance of the x values. 
Collects statistics on a single quantity (weighted) 
void StatCollectorTest()
Tests StatCollector object with a known input. 
Weight_t Weights() const 
Returns the sum of the weights. 
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.